Home | History | Annotate | Line # | Download | only in lfs
lfs_bio.c revision 1.55
      1 /*	$NetBSD: lfs_bio.c,v 1.55 2002/12/30 05:34:17 yamt Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1999, 2000 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Konrad E. Schroder <perseant (at) hhhh.org>.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *      This product includes software developed by the NetBSD
     21  *      Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 /*
     39  * Copyright (c) 1991, 1993
     40  *	The Regents of the University of California.  All rights reserved.
     41  *
     42  * Redistribution and use in source and binary forms, with or without
     43  * modification, are permitted provided that the following conditions
     44  * are met:
     45  * 1. Redistributions of source code must retain the above copyright
     46  *    notice, this list of conditions and the following disclaimer.
     47  * 2. Redistributions in binary form must reproduce the above copyright
     48  *    notice, this list of conditions and the following disclaimer in the
     49  *    documentation and/or other materials provided with the distribution.
     50  * 3. All advertising materials mentioning features or use of this software
     51  *    must display the following acknowledgement:
     52  *	This product includes software developed by the University of
     53  *	California, Berkeley and its contributors.
     54  * 4. Neither the name of the University nor the names of its contributors
     55  *    may be used to endorse or promote products derived from this software
     56  *    without specific prior written permission.
     57  *
     58  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     59  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     60  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     61  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     62  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     63  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     64  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     65  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     66  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     67  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     68  * SUCH DAMAGE.
     69  *
     70  *	@(#)lfs_bio.c	8.10 (Berkeley) 6/10/95
     71  */
     72 
     73 #include <sys/cdefs.h>
     74 __KERNEL_RCSID(0, "$NetBSD: lfs_bio.c,v 1.55 2002/12/30 05:34:17 yamt Exp $");
     75 
     76 #include <sys/param.h>
     77 #include <sys/systm.h>
     78 #include <sys/proc.h>
     79 #include <sys/buf.h>
     80 #include <sys/vnode.h>
     81 #include <sys/resourcevar.h>
     82 #include <sys/mount.h>
     83 #include <sys/kernel.h>
     84 
     85 #include <ufs/ufs/inode.h>
     86 #include <ufs/ufs/ufsmount.h>
     87 #include <ufs/ufs/ufs_extern.h>
     88 
     89 #include <sys/malloc.h>
     90 #include <ufs/lfs/lfs.h>
     91 #include <ufs/lfs/lfs_extern.h>
     92 
     93 /* Macros to clear/set/test flags. */
     94 # define	SET(t, f)	(t) |= (f)
     95 # define	CLR(t, f)	(t) &= ~(f)
     96 # define	ISSET(t, f)	((t) & (f))
     97 
     98 /*
     99  * LFS block write function.
    100  *
    101  * XXX
    102  * No write cost accounting is done.
    103  * This is almost certainly wrong for synchronous operations and NFS.
    104  */
    105 int	locked_queue_count   = 0;	/* XXX Count of locked-down buffers. */
    106 long	locked_queue_bytes   = 0L;	/* XXX Total size of locked buffers. */
    107 int	lfs_writing          = 0;	/* Set if already kicked off a writer
    108 					   because of buffer space */
    109 extern int lfs_dostats;
    110 
    111 /*
    112  * reserved number/bytes of locked buffers
    113  */
    114 int locked_queue_rcount = 0;
    115 long locked_queue_rbytes = 0L;
    116 
    117 int lfs_fits_buf(struct lfs *, int, int);
    118 int lfs_reservebuf(struct lfs *, struct vnode *vp, struct vnode *vp2,
    119     int, int);
    120 int lfs_reserveavail(struct lfs *, struct vnode *vp, struct vnode *vp2, int);
    121 
    122 int
    123 lfs_fits_buf(struct lfs *fs, int n, int bytes)
    124 {
    125 	int count_fit =
    126 	    (locked_queue_count + locked_queue_rcount + n < LFS_WAIT_BUFS);
    127 	int bytes_fit =
    128 	    (locked_queue_bytes + locked_queue_rbytes + bytes < LFS_WAIT_BYTES);
    129 
    130 #ifdef DEBUG_LFS
    131 	if (!count_fit) {
    132 		printf("lfs_fits_buf: no fit count: %d + %d + %d >= %d\n",
    133 			locked_queue_count, locked_queue_rcount,
    134 			n, LFS_WAIT_BUFS);
    135 	}
    136 	if (!bytes_fit) {
    137 		printf("lfs_fits_buf: no fit bytes: %ld + %ld + %d >= %d\n",
    138 			locked_queue_bytes, locked_queue_rbytes,
    139 			bytes, LFS_WAIT_BYTES);
    140 	}
    141 #endif /* DEBUG_LFS */
    142 
    143 	return (count_fit && bytes_fit);
    144 }
    145 
    146 /* ARGSUSED */
    147 int
    148 lfs_reservebuf(struct lfs *fs, struct vnode *vp, struct vnode *vp2,
    149     int n, int bytes)
    150 {
    151 	KASSERT(locked_queue_rcount >= 0);
    152 	KASSERT(locked_queue_rbytes >= 0);
    153 
    154 	while (n > 0 && !lfs_fits_buf(fs, n, bytes)) {
    155 		int error;
    156 
    157 		++fs->lfs_writer;
    158 		lfs_flush(fs, 0);
    159 		if (--fs->lfs_writer == 0)
    160 			wakeup(&fs->lfs_dirops);
    161 
    162 		error = tsleep(&locked_queue_count, PCATCH | PUSER,
    163 		    "lfsresbuf", hz * LFS_BUFWAIT);
    164 		if (error && error != EWOULDBLOCK)
    165 			return error;
    166 	}
    167 
    168 	locked_queue_rcount += n;
    169 	locked_queue_rbytes += bytes;
    170 
    171 	KASSERT(locked_queue_rcount >= 0);
    172 	KASSERT(locked_queue_rbytes >= 0);
    173 
    174 	return 0;
    175 }
    176 
    177 /*
    178  * Try to reserve some blocks, prior to performing a sensitive operation that
    179  * requires the vnode lock to be honored.  If there is not enough space, give
    180  * up the vnode lock temporarily and wait for the space to become available.
    181  *
    182  * Called with vp locked.  (Note nowever that if fsb < 0, vp is ignored.)
    183  *
    184  * XXX YAMT - it isn't safe to unlock vp here
    185  * because the node might be modified while we sleep.
    186  * (eg. cached states like i_offset might be stale,
    187  *  the vnode might be truncated, etc..)
    188  * maybe we should have a way to restart the vnodeop (EVOPRESTART?)
    189  * or rearrange vnodeop interface to leave vnode locking to file system
    190  * specific code so that each file systems can have their own vnode locking and
    191  * vnode re-using strategies.
    192  */
    193 int
    194 lfs_reserveavail(struct lfs *fs, struct vnode *vp, struct vnode *vp2, int fsb)
    195 {
    196 	CLEANERINFO *cip;
    197 	struct buf *bp;
    198 	int error, slept;
    199 
    200 	slept = 0;
    201 	while (fsb > 0 && !lfs_fits(fs, fsb + fs->lfs_ravail)) {
    202 #if 0
    203 		/*
    204 		 * XXX ideally, we should unlock vnodes here
    205 		 * because we might sleep very long time.
    206 		 */
    207 		VOP_UNLOCK(vp, 0);
    208 		if (vp2 != NULL) {
    209 			VOP_UNLOCK(vp2, 0);
    210 		}
    211 #else
    212 		/*
    213 		 * XXX since we'll sleep for cleaner with vnode lock holding,
    214 		 * deadlock will occur if cleaner tries to lock the vnode.
    215 		 * (eg. lfs_markv -> lfs_fastvget -> getnewvnode -> vclean)
    216 		 */
    217 #endif
    218 
    219 		if (!slept) {
    220 #ifdef DEBUG
    221 			printf("lfs_reserve: waiting for %ld (bfree = %d,"
    222 			       " est_bfree = %d)\n",
    223 			       fsb + fs->lfs_ravail, fs->lfs_bfree,
    224 			       LFS_EST_BFREE(fs));
    225 #endif
    226 		}
    227 		++slept;
    228 
    229 		/* Wake up the cleaner */
    230 		LFS_CLEANERINFO(cip, fs, bp);
    231 		LFS_SYNC_CLEANERINFO(cip, fs, bp, 0);
    232 		wakeup(&lfs_allclean_wakeup);
    233 		wakeup(&fs->lfs_nextseg);
    234 
    235 		error = tsleep(&fs->lfs_avail, PCATCH | PUSER, "lfs_reserve",
    236 			       0);
    237 #if 0
    238 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX use lockstatus */
    239 		vn_lock(vp2, LK_EXCLUSIVE | LK_RETRY); /* XXX use lockstatus */
    240 #endif
    241 		if (error)
    242 			return error;
    243 	}
    244 #ifdef DEBUG
    245 	if (slept)
    246 		printf("lfs_reserve: woke up\n");
    247 #endif
    248 	fs->lfs_ravail += fsb;
    249 
    250 	return 0;
    251 }
    252 
    253 #ifdef DIAGNOSTIC
    254 int lfs_rescount;
    255 int lfs_rescountdirop;
    256 #endif
    257 
    258 int
    259 lfs_reserve(struct lfs *fs, struct vnode *vp, struct vnode *vp2, int fsb)
    260 {
    261 	int error;
    262 	int cantwait;
    263 
    264 	KASSERT(fsb < 0 || VOP_ISLOCKED(vp));
    265 	KASSERT(vp2 == NULL || fsb < 0 || VOP_ISLOCKED(vp2));
    266 	KASSERT(vp2 == NULL || !(VTOI(vp2)->i_flag & IN_ADIROP));
    267 	KASSERT(vp2 == NULL || vp2 != fs->lfs_unlockvp);
    268 
    269 	cantwait = (VTOI(vp)->i_flag & IN_ADIROP) || fs->lfs_unlockvp == vp;
    270 #ifdef DIAGNOSTIC
    271 	if (cantwait) {
    272 		if (fsb > 0)
    273 			lfs_rescountdirop++;
    274 		else if (fsb < 0)
    275 			lfs_rescountdirop--;
    276 		if (lfs_rescountdirop < 0)
    277 			panic("lfs_rescountdirop");
    278 	}
    279 	else {
    280 		if (fsb > 0)
    281 			lfs_rescount++;
    282 		else if (fsb < 0)
    283 			lfs_rescount--;
    284 		if (lfs_rescount < 0)
    285 			panic("lfs_rescount");
    286 	}
    287 #endif
    288 	if (cantwait)
    289 		return 0;
    290 
    291 	/*
    292 	 * XXX
    293 	 * vref vnodes here so that cleaner doesn't try to reuse them.
    294 	 * (see XXX comment in lfs_reserveavail)
    295 	 */
    296 	lfs_vref(vp);
    297 	if (vp2 != NULL) {
    298 		lfs_vref(vp2);
    299 	}
    300 
    301 	error = lfs_reserveavail(fs, vp, vp2, fsb);
    302 	if (error)
    303 		goto done;
    304 
    305 	/*
    306 	 * XXX just a guess. should be more precise.
    307 	 */
    308 	error = lfs_reservebuf(fs, vp, vp2,
    309 	    fragstoblks(fs, fsb), fsbtob(fs, fsb));
    310 	if (error)
    311 		lfs_reserveavail(fs, vp, vp2, -fsb);
    312 
    313 done:
    314 	lfs_vunref(vp);
    315 	if (vp2 != NULL) {
    316 		lfs_vunref(vp2);
    317 	}
    318 
    319 	return error;
    320 }
    321 
    322 int
    323 lfs_bwrite(void *v)
    324 {
    325 	struct vop_bwrite_args /* {
    326 		struct buf *a_bp;
    327 	} */ *ap = v;
    328 	struct buf *bp = ap->a_bp;
    329 
    330 #ifdef DIAGNOSTIC
    331         if (VTOI(bp->b_vp)->i_lfs->lfs_ronly == 0 && (bp->b_flags & B_ASYNC)) {
    332 		panic("bawrite LFS buffer");
    333 	}
    334 #endif /* DIAGNOSTIC */
    335 	return lfs_bwrite_ext(bp,0);
    336 }
    337 
    338 /*
    339  * Determine if there is enough room currently available to write fsb
    340  * blocks.  We need enough blocks for the new blocks, the current
    341  * inode blocks (including potentially the ifile inode), a summary block,
    342  * and the segment usage table, plus an ifile block.
    343  */
    344 int
    345 lfs_fits(struct lfs *fs, int fsb)
    346 {
    347 	int needed;
    348 
    349 	needed = fsb + btofsb(fs, fs->lfs_sumsize) +
    350 		 ((howmany(fs->lfs_uinodes + 1, INOPB(fs)) + fs->lfs_segtabsz +
    351 		   1) << (fs->lfs_blktodb - fs->lfs_fsbtodb));
    352 
    353 	if (needed >= fs->lfs_avail) {
    354 #ifdef DEBUG
    355 		printf("lfs_fits: no fit: fsb = %d, uinodes = %d, "
    356 		       "needed = %d, avail = %d\n",
    357 		       fsb, fs->lfs_uinodes, needed, fs->lfs_avail);
    358 #endif
    359 		return 0;
    360 	}
    361 	return 1;
    362 }
    363 
    364 int
    365 lfs_availwait(struct lfs *fs, int fsb)
    366 {
    367 	int error;
    368 	CLEANERINFO *cip;
    369 	struct buf *cbp;
    370 
    371 	while (!lfs_fits(fs, fsb)) {
    372 		/*
    373 		 * Out of space, need cleaner to run.
    374 		 * Update the cleaner info, then wake it up.
    375 		 * Note the cleanerinfo block is on the ifile
    376 		 * so it CANT_WAIT.
    377 		 */
    378 		LFS_CLEANERINFO(cip, fs, cbp);
    379 		LFS_SYNC_CLEANERINFO(cip, fs, cbp, 0);
    380 
    381 		printf("lfs_availwait: out of available space, "
    382 		       "waiting on cleaner\n");
    383 
    384 		wakeup(&lfs_allclean_wakeup);
    385 		wakeup(&fs->lfs_nextseg);
    386 #ifdef DIAGNOSTIC
    387 		if (fs->lfs_seglock && fs->lfs_lockpid == curproc->p_pid)
    388 			panic("lfs_availwait: deadlock");
    389 #endif
    390 		error = tsleep(&fs->lfs_avail, PCATCH | PUSER, "cleaner", 0);
    391 		if (error)
    392 			return (error);
    393 	}
    394 	return 0;
    395 }
    396 
    397 int
    398 lfs_bwrite_ext(struct buf *bp, int flags)
    399 {
    400 	struct lfs *fs;
    401 	struct inode *ip;
    402 	int fsb, s;
    403 
    404 	KASSERT(bp->b_flags & B_BUSY);
    405 	KASSERT(flags & BW_CLEAN || !(bp->b_flags & B_CALL));
    406 
    407 	/*
    408 	 * Don't write *any* blocks if we're mounted read-only.
    409 	 * In particular the cleaner can't write blocks either.
    410 	 */
    411         if (VTOI(bp->b_vp)->i_lfs->lfs_ronly) {
    412 		bp->b_flags &= ~(B_DELWRI | B_READ | B_ERROR);
    413 		LFS_UNLOCK_BUF(bp);
    414 		if (bp->b_flags & B_CALL)
    415 			bp->b_flags &= ~B_BUSY;
    416 		else
    417 			brelse(bp);
    418 		return EROFS;
    419 	}
    420 
    421 	/*
    422 	 * Set the delayed write flag and use reassignbuf to move the buffer
    423 	 * from the clean list to the dirty one.
    424 	 *
    425 	 * Set the B_LOCKED flag and unlock the buffer, causing brelse to move
    426 	 * the buffer onto the LOCKED free list.  This is necessary, otherwise
    427 	 * getnewbuf() would try to reclaim the buffers using bawrite, which
    428 	 * isn't going to work.
    429 	 *
    430 	 * XXX we don't let meta-data writes run out of space because they can
    431 	 * come from the segment writer.  We need to make sure that there is
    432 	 * enough space reserved so that there's room to write meta-data
    433 	 * blocks.
    434 	 */
    435 	if (!(bp->b_flags & B_LOCKED)) {
    436 		fs = VFSTOUFS(bp->b_vp->v_mount)->um_lfs;
    437 		fsb = fragstofsb(fs, numfrags(fs, bp->b_bcount));
    438 
    439 		ip = VTOI(bp->b_vp);
    440 		if (flags & BW_CLEAN) {
    441 			LFS_SET_UINO(ip, IN_CLEANING);
    442 		} else {
    443 			LFS_SET_UINO(ip, IN_MODIFIED);
    444 			if (bp->b_lblkno >= 0)
    445 				LFS_SET_UINO(ip, IN_UPDATE);
    446 		}
    447 		fs->lfs_avail -= fsb;
    448 		bp->b_flags |= B_DELWRI;
    449 
    450 		LFS_LOCK_BUF(bp);
    451 		bp->b_flags &= ~(B_READ | B_DONE | B_ERROR);
    452 		s = splbio();
    453 		reassignbuf(bp, bp->b_vp);
    454 		splx(s);
    455 	}
    456 
    457 	if (bp->b_flags & B_CALL)
    458 		bp->b_flags &= ~B_BUSY;
    459 	else
    460 		brelse(bp);
    461 
    462 	return (0);
    463 }
    464 
    465 void
    466 lfs_flush_fs(struct lfs *fs, int flags)
    467 {
    468 	if (fs->lfs_ronly == 0 && fs->lfs_dirops == 0)
    469 	{
    470 		/* disallow dirops during flush */
    471 		fs->lfs_writer++;
    472 
    473 		/*
    474 		 * We set the queue to 0 here because we
    475 		 * are about to write all the dirty
    476 		 * buffers we have.  If more come in
    477 		 * while we're writing the segment, they
    478 		 * may not get written, so we want the
    479 		 * count to reflect these new writes
    480 		 * after the segwrite completes.
    481 		 */
    482 		if (lfs_dostats)
    483 			++lfs_stats.flush_invoked;
    484 		lfs_segwrite(fs->lfs_ivnode->v_mount, flags);
    485 
    486 		/* XXX KS - allow dirops again */
    487 		if (--fs->lfs_writer == 0)
    488 			wakeup(&fs->lfs_dirops);
    489 	}
    490 }
    491 
    492 /*
    493  * XXX
    494  * This routine flushes buffers out of the B_LOCKED queue when LFS has too
    495  * many locked down.  Eventually the pageout daemon will simply call LFS
    496  * when pages need to be reclaimed.  Note, we have one static count of locked
    497  * buffers, so we can't have more than a single file system.  To make this
    498  * work for multiple file systems, put the count into the mount structure.
    499  */
    500 void
    501 lfs_flush(struct lfs *fs, int flags)
    502 {
    503 	struct mount *mp, *nmp;
    504 
    505 	if (lfs_dostats)
    506 		++lfs_stats.write_exceeded;
    507 	if (lfs_writing && flags == 0) {/* XXX flags */
    508 #ifdef DEBUG_LFS
    509 		printf("lfs_flush: not flushing because another flush is active\n");
    510 #endif
    511 		return;
    512 	}
    513 	lfs_writing = 1;
    514 
    515 	simple_lock(&mountlist_slock);
    516 	for (mp = mountlist.cqh_first; mp != (void *)&mountlist; mp = nmp) {
    517 		if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock)) {
    518 			nmp = mp->mnt_list.cqe_next;
    519 			continue;
    520 		}
    521 		if (strncmp(&mp->mnt_stat.f_fstypename[0], MOUNT_LFS, MFSNAMELEN) == 0)
    522 			lfs_flush_fs(((struct ufsmount *)mp->mnt_data)->ufsmount_u.lfs, flags);
    523 		simple_lock(&mountlist_slock);
    524 		nmp = mp->mnt_list.cqe_next;
    525 		vfs_unbusy(mp);
    526 	}
    527 	simple_unlock(&mountlist_slock);
    528 
    529 	LFS_DEBUG_COUNTLOCKED("flush");
    530 
    531 	lfs_writing = 0;
    532 }
    533 
    534 #define INOCOUNT(fs) howmany((fs)->lfs_uinodes, INOPB(fs))
    535 #define INOBYTES(fs) ((fs)->lfs_uinodes * DINODE_SIZE)
    536 
    537 int
    538 lfs_check(struct vnode *vp, ufs_daddr_t blkno, int flags)
    539 {
    540 	int error;
    541 	struct lfs *fs;
    542 	struct inode *ip;
    543 	extern int lfs_dirvcount;
    544 
    545 	error = 0;
    546 	ip = VTOI(vp);
    547 
    548 	/* If out of buffers, wait on writer */
    549 	/* XXX KS - if it's the Ifile, we're probably the cleaner! */
    550 	if (ip->i_number == LFS_IFILE_INUM)
    551 		return 0;
    552 	/* If we're being called from inside a dirop, don't sleep */
    553 	if (ip->i_flag & IN_ADIROP)
    554 		return 0;
    555 
    556 	fs = ip->i_lfs;
    557 
    558 	/*
    559 	 * If we would flush below, but dirops are active, sleep.
    560 	 * Note that a dirop cannot ever reach this code!
    561 	 */
    562 	while (fs->lfs_dirops > 0 &&
    563 	       (locked_queue_count + INOCOUNT(fs) > LFS_MAX_BUFS ||
    564                 locked_queue_bytes + INOBYTES(fs) > LFS_MAX_BYTES ||
    565                 lfs_dirvcount > LFS_MAXDIROP || fs->lfs_diropwait > 0))
    566 	{
    567 		++fs->lfs_diropwait;
    568 		tsleep(&fs->lfs_writer, PRIBIO+1, "bufdirop", 0);
    569 		--fs->lfs_diropwait;
    570 	}
    571 
    572 	if (locked_queue_count + INOCOUNT(fs) > LFS_MAX_BUFS ||
    573 	    locked_queue_bytes + INOBYTES(fs) > LFS_MAX_BYTES ||
    574 	    lfs_dirvcount > LFS_MAXDIROP || fs->lfs_diropwait > 0)
    575 	{
    576 		++fs->lfs_writer;
    577 		lfs_flush(fs, flags);
    578 		if (--fs->lfs_writer == 0)
    579 			wakeup(&fs->lfs_dirops);
    580 	}
    581 
    582 	while (locked_queue_count + INOCOUNT(fs) > LFS_WAIT_BUFS
    583 	       || locked_queue_bytes + INOBYTES(fs) > LFS_WAIT_BYTES)
    584 	{
    585 		if (lfs_dostats)
    586 			++lfs_stats.wait_exceeded;
    587 #ifdef DEBUG_LFS
    588 		printf("lfs_check: waiting: count=%d, bytes=%ld\n",
    589 			locked_queue_count, locked_queue_bytes);
    590 #endif
    591 		error = tsleep(&locked_queue_count, PCATCH | PUSER,
    592 			       "buffers", hz * LFS_BUFWAIT);
    593 		if (error != EWOULDBLOCK)
    594 			break;
    595 		/*
    596 		 * lfs_flush might not flush all the buffers, if some of the
    597 		 * inodes were locked or if most of them were Ifile blocks
    598 		 * and we weren't asked to checkpoint.  Try flushing again
    599 		 * to keep us from blocking indefinitely.
    600 		 */
    601 		if (locked_queue_count + INOCOUNT(fs) > LFS_MAX_BUFS ||
    602 		    locked_queue_bytes + INOBYTES(fs) > LFS_MAX_BYTES)
    603 		{
    604 			++fs->lfs_writer;
    605 			lfs_flush(fs, flags | SEGM_CKP);
    606 			if (--fs->lfs_writer == 0)
    607 				wakeup(&fs->lfs_dirops);
    608 		}
    609 	}
    610 	return (error);
    611 }
    612 
    613 /*
    614  * Allocate a new buffer header.
    615  */
    616 #ifdef MALLOCLOG
    617 # define DOMALLOC(S, T, F) _malloc((S), (T), (F), file, line)
    618 struct buf *
    619 lfs_newbuf_malloclog(struct lfs *fs, struct vnode *vp, ufs_daddr_t daddr, size_t size, char *file, int line)
    620 #else
    621 # define DOMALLOC(S, T, F) malloc((S), (T), (F))
    622 struct buf *
    623 lfs_newbuf(struct lfs *fs, struct vnode *vp, ufs_daddr_t daddr, size_t size)
    624 #endif
    625 {
    626 	struct buf *bp;
    627 	size_t nbytes;
    628 	int s;
    629 
    630 	nbytes = roundup(size, fsbtob(fs, 1));
    631 
    632 	bp = DOMALLOC(sizeof(struct buf), M_SEGMENT, M_WAITOK);
    633 	bzero(bp, sizeof(struct buf));
    634 	if (nbytes) {
    635 		bp->b_data = DOMALLOC(nbytes, M_SEGMENT, M_WAITOK);
    636 		bzero(bp->b_data, nbytes);
    637 	}
    638 #ifdef DIAGNOSTIC
    639 	if (vp == NULL)
    640 		panic("vp is NULL in lfs_newbuf");
    641 	if (bp == NULL)
    642 		panic("bp is NULL after malloc in lfs_newbuf");
    643 #endif
    644 	s = splbio();
    645 	bgetvp(vp, bp);
    646 	splx(s);
    647 
    648 	bp->b_saveaddr = (caddr_t)fs;
    649 	bp->b_bufsize = size;
    650 	bp->b_bcount = size;
    651 	bp->b_lblkno = daddr;
    652 	bp->b_blkno = daddr;
    653 	bp->b_error = 0;
    654 	bp->b_resid = 0;
    655 	bp->b_iodone = lfs_callback;
    656 	bp->b_flags |= B_BUSY | B_CALL | B_NOCACHE;
    657 
    658 	return (bp);
    659 }
    660 
    661 #ifdef MALLOCLOG
    662 # define DOFREE(A, T) _free((A), (T), file, line)
    663 void
    664 lfs_freebuf_malloclog(struct buf *bp, char *file, int line)
    665 #else
    666 # define DOFREE(A, T) free((A), (T))
    667 void
    668 lfs_freebuf(struct buf *bp)
    669 #endif
    670 {
    671 	int s;
    672 
    673 	s = splbio();
    674 	if (bp->b_vp)
    675 		brelvp(bp);
    676 	splx(s);
    677 	if (!(bp->b_flags & B_INVAL)) { /* B_INVAL indicates a "fake" buffer */
    678 		DOFREE(bp->b_data, M_SEGMENT);
    679 		bp->b_data = NULL;
    680 	}
    681 	DOFREE(bp, M_SEGMENT);
    682 }
    683 
    684 /*
    685  * Definitions for the buffer free lists.
    686  */
    687 #define BQUEUES		4		/* number of free buffer queues */
    688 
    689 #define BQ_LOCKED	0		/* super-blocks &c */
    690 #define BQ_LRU		1		/* lru, useful buffers */
    691 #define BQ_AGE		2		/* rubbish */
    692 #define BQ_EMPTY	3		/* buffer headers with no memory */
    693 
    694 extern TAILQ_HEAD(bqueues, buf) bufqueues[BQUEUES];
    695 
    696 /*
    697  * Return a count of buffers on the "locked" queue.
    698  * Don't count malloced buffers, since they don't detract from the total.
    699  */
    700 void
    701 lfs_countlocked(int *count, long *bytes, char *msg)
    702 {
    703 	struct buf *bp;
    704 	int n = 0;
    705 	long int size = 0L;
    706 
    707 	for (bp = bufqueues[BQ_LOCKED].tqh_first; bp;
    708 	    bp = bp->b_freelist.tqe_next) {
    709 		if (bp->b_flags & B_CALL) /* Malloced buffer */
    710 			continue;
    711 		n++;
    712 		size += bp->b_bufsize;
    713 #ifdef DEBUG_LOCKED_LIST
    714 		if (n > nbuf)
    715 			panic("lfs_countlocked: this can't happen: more"
    716 			      " buffers locked than exist");
    717 #endif
    718 	}
    719 #ifdef DEBUG_LOCKED_LIST
    720 	/* Theoretically this function never really does anything */
    721 	if (n != *count)
    722 		printf("lfs_countlocked: %s: adjusted buf count from %d to %d\n",
    723 		       msg, *count, n);
    724 	if (size != *bytes)
    725 		printf("lfs_countlocked: %s: adjusted byte count from %ld to %ld\n",
    726 		       msg, *bytes, size);
    727 #endif
    728 	*count = n;
    729 	*bytes = size;
    730 	return;
    731 }
    732