xref: /original-bsd/sys/ufs/lfs/lfs_bio.c (revision 58b1b499)
1 /*
2  * Copyright (c) 1991, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  *
7  *	@(#)lfs_bio.c	8.6 (Berkeley) 03/30/95
8  */
9 
10 #include <sys/param.h>
11 #include <sys/proc.h>
12 #include <sys/buf.h>
13 #include <sys/vnode.h>
14 #include <sys/resourcevar.h>
15 #include <sys/mount.h>
16 #include <sys/kernel.h>
17 
18 #include <ufs/ufs/quota.h>
19 #include <ufs/ufs/inode.h>
20 #include <ufs/ufs/ufsmount.h>
21 
22 #include <ufs/lfs/lfs.h>
23 #include <ufs/lfs/lfs_extern.h>
24 
25 /*
26  * LFS block write function.
27  *
28  * XXX
29  * No write cost accounting is done.
30  * This is almost certainly wrong for synchronous operations and NFS.
31  */
32 int	lfs_allclean_wakeup;		/* Cleaner wakeup address. */
33 int	locked_queue_count;		/* XXX Count of locked-down buffers. */
34 int	lfs_writing;			/* Set if already kicked off a writer
35 					   because of buffer space */
36 /*
37 #define WRITE_THRESHHOLD	((nbuf >> 2) - 10)
38 #define WAIT_THRESHHOLD		((nbuf >> 1) - 10)
39 */
40 #define WAIT_THRESHHOLD         (nbuf - (nbuf >> 2) - 10)
41 #define WRITE_THRESHHOLD        ((nbuf >> 1) - 10)
42 #define LFS_BUFWAIT	2
43 
44 int
45 lfs_bwrite(ap)
46 	struct vop_bwrite_args /* {
47 		struct buf *a_bp;
48 	} */ *ap;
49 {
50 	register struct buf *bp = ap->a_bp;
51 	struct lfs *fs;
52 	struct inode *ip;
53 	int error, s;
54 
55 	/*
56 	 * Set the delayed write flag and use reassignbuf to move the buffer
57 	 * from the clean list to the dirty one.
58 	 *
59 	 * Set the B_LOCKED flag and unlock the buffer, causing brelse to move
60 	 * the buffer onto the LOCKED free list.  This is necessary, otherwise
61 	 * getnewbuf() would try to reclaim the buffers using bawrite, which
62 	 * isn't going to work.
63 	 *
64 	 * XXX we don't let meta-data writes run out of space because they can
65 	 * come from the segment writer.  We need to make sure that there is
66 	 * enough space reserved so that there's room to write meta-data
67 	 * blocks.
68 	 */
69 	if (!(bp->b_flags & B_LOCKED)) {
70 		fs = VFSTOUFS(bp->b_vp->v_mount)->um_lfs;
71 		while (!LFS_FITS(fs, fsbtodb(fs, 1)) && !IS_IFILE(bp) &&
72 		    bp->b_lblkno > 0) {
73 			/* Out of space, need cleaner to run */
74 			wakeup(&lfs_allclean_wakeup);
75 			if (error = tsleep(&fs->lfs_avail, PCATCH | PUSER,
76 			    "cleaner", NULL)) {
77 				brelse(bp);
78 				return (error);
79 			}
80 		}
81 		ip = VTOI((bp)->b_vp);
82 		if (!(ip->i_flag & IN_MODIFIED))
83 			++fs->lfs_uinodes;
84 		ip->i_flag |= IN_CHANGE | IN_MODIFIED | IN_UPDATE;
85 		fs->lfs_avail -= fsbtodb(fs, 1);
86 		++locked_queue_count;
87 		bp->b_flags |= B_DELWRI | B_LOCKED;
88 		bp->b_flags &= ~(B_READ | B_ERROR);
89 		s = splbio();
90 		reassignbuf(bp, bp->b_vp);
91 		splx(s);
92 	}
93 	brelse(bp);
94 	return (0);
95 }
96 
97 /*
98  * XXX
99  * This routine flushes buffers out of the B_LOCKED queue when LFS has too
100  * many locked down.  Eventually the pageout daemon will simply call LFS
101  * when pages need to be reclaimed.  Note, we have one static count of locked
102  * buffers, so we can't have more than a single file system.  To make this
103  * work for multiple file systems, put the count into the mount structure.
104  */
105 void
106 lfs_flush()
107 {
108 	register struct mount *mp;
109 
110 #ifdef DOSTATS
111 	++lfs_stats.write_exceeded;
112 #endif
113 	if (lfs_writing)
114 		return;
115 	lfs_writing = 1;
116 	for (mp = mountlist.tqh_first; mp != NULL; mp = mp->mnt_list.tqe_next) {
117 		/* The lock check below is to avoid races with unmount. */
118 		if (mp->mnt_stat.f_type == lfs_mount_type &&
119 		    (mp->mnt_flag & (MNT_MLOCK|MNT_RDONLY|MNT_UNMOUNT)) == 0 &&
120 		    !((((struct ufsmount *)mp->mnt_data))->ufsmount_u.lfs)->lfs_dirops ) {
121 			/*
122 			 * We set the queue to 0 here because we are about to
123 			 * write all the dirty buffers we have.  If more come
124 			 * in while we're writing the segment, they may not
125 			 * get written, so we want the count to reflect these
126 			 * new writes after the segwrite completes.
127 			 */
128 #ifdef DOSTATS
129 			++lfs_stats.flush_invoked;
130 #endif
131 			lfs_segwrite(mp, 0);
132 		}
133 	}
134 	lfs_writing = 0;
135 }
136 
137 int
138 lfs_check(vp, blkno)
139 	struct vnode *vp;
140 	ufs_daddr_t blkno;
141 {
142 	extern int lfs_allclean_wakeup;
143 	int error;
144 
145 	error = 0;
146 	if (incore(vp, blkno))
147 		return (0);
148 	if (locked_queue_count > WRITE_THRESHHOLD)
149 		lfs_flush();
150 
151 	/* If out of buffers, wait on writer */
152 	while (locked_queue_count > WAIT_THRESHHOLD) {
153 #ifdef DOSTATS
154 	    ++lfs_stats.wait_exceeded;
155 #endif
156 	    error = tsleep(&locked_queue_count, PCATCH | PUSER, "buffers",
157 	        hz * LFS_BUFWAIT);
158 	}
159 
160 	return (error);
161 }
162