Home | History | Annotate | Line # | Download | only in lfs
lfs_bio.c revision 1.52
      1 /*	$NetBSD: lfs_bio.c,v 1.52 2002/12/28 14:39:08 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.52 2002/12/28 14:39:08 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 vnode op. (EVOPRESTART?)
    189  */
    190 int
    191 lfs_reserveavail(struct lfs *fs, struct vnode *vp, struct vnode *vp2, int fsb)
    192 {
    193 	CLEANERINFO *cip;
    194 	struct buf *bp;
    195 	int error, slept;
    196 
    197 	slept = 0;
    198 	while (fsb > 0 && !lfs_fits(fs, fsb + fs->lfs_ravail) &&
    199 	    vp != fs->lfs_unlockvp) {
    200 #if 0
    201 		/*
    202 		 * XXX ideally, we should unlock vnodes here
    203 		 * because we might sleep very long time.
    204 		 */
    205 		VOP_UNLOCK(vp, 0);
    206 		if (vp2 != NULL) {
    207 			VOP_UNLOCK(vp2, 0);
    208 		}
    209 #else
    210 		/*
    211 		 * XXX since we'll sleep for cleaner with vnode lock holding,
    212 		 * deadlock will occur if cleaner wants to acquire the vnode
    213 		 * lock.
    214 		 * (eg. lfs_markv -> lfs_fastvget -> getnewvnode -> vclean)
    215 		 */
    216 #endif
    217 
    218 		if (!slept) {
    219 #ifdef DEBUG
    220 			printf("lfs_reserve: waiting for %ld (bfree = %d,"
    221 			       " est_bfree = %d)\n",
    222 			       fsb + fs->lfs_ravail, fs->lfs_bfree,
    223 			       LFS_EST_BFREE(fs));
    224 #endif
    225 		}
    226 		++slept;
    227 
    228 		/* Wake up the cleaner */
    229 		LFS_CLEANERINFO(cip, fs, bp);
    230 		LFS_SYNC_CLEANERINFO(cip, fs, bp, 0);
    231 		wakeup(&lfs_allclean_wakeup);
    232 		wakeup(&fs->lfs_nextseg);
    233 
    234 		error = tsleep(&fs->lfs_avail, PCATCH | PUSER, "lfs_reserve",
    235 			       0);
    236 #if 0
    237 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX use lockstatus */
    238 		vn_lock(vp2, LK_EXCLUSIVE | LK_RETRY); /* XXX use lockstatus */
    239 #endif
    240 		if (error)
    241 			return error;
    242 	}
    243 #ifdef DEBUG
    244 	if (slept)
    245 		printf("lfs_reserve: woke up\n");
    246 #endif
    247 	fs->lfs_ravail += fsb;
    248 
    249 	return 0;
    250 }
    251 
    252 #ifdef DIAGNOSTIC
    253 int lfs_rescount;
    254 int lfs_rescountdirop;
    255 #endif
    256 
    257 int
    258 lfs_reserve(struct lfs *fs, struct vnode *vp, struct vnode *vp2, int fsb)
    259 {
    260 	int error;
    261 	int cantwait;
    262 
    263 	KASSERT(fsb < 0 || VOP_ISLOCKED(vp));
    264 	KASSERT(vp2 == NULL || fsb < 0 || VOP_ISLOCKED(vp2));
    265 
    266 	/*
    267 	 * XXX
    268 	 * vref vnodes here so that cleaner doesn't try to reuse them.
    269 	 */
    270 	lfs_vref(vp);
    271 	if (vp2 != NULL) {
    272 		lfs_vref(vp2);
    273 	}
    274 
    275 	cantwait = (VTOI(vp)->i_flag & IN_ADIROP);
    276 #ifdef DIAGNOSTIC
    277 	if (cantwait) {
    278 		if (fsb > 0)
    279 			lfs_rescountdirop++;
    280 		else if (fsb < 0)
    281 			lfs_rescountdirop--;
    282 		if (lfs_rescountdirop < 0)
    283 			panic("lfs_rescountdirop");
    284 	}
    285 	else {
    286 		if (fsb > 0)
    287 			lfs_rescount++;
    288 		else if (fsb < 0)
    289 			lfs_rescount--;
    290 		if (lfs_rescount < 0)
    291 			panic("lfs_rescount");
    292 	}
    293 #endif
    294 	if (cantwait)
    295 		return 0;
    296 
    297 	error = lfs_reserveavail(fs, vp, vp2, fsb);
    298 	if (error)
    299 		goto done;
    300 
    301 	/*
    302 	 * XXX just a guess. should be more precise.
    303 	 */
    304 	error = lfs_reservebuf(fs, vp, vp2,
    305 	    fragstoblks(fs, fsb), fsbtob(fs, fsb));
    306 	if (error)
    307 		lfs_reserveavail(fs, vp, vp2, -fsb);
    308 
    309 done:
    310 	lfs_vunref(vp);
    311 	if (vp2 != NULL) {
    312 		lfs_vunref(vp2);
    313 	}
    314 
    315 	return error;
    316 }
    317 
    318 int
    319 lfs_bwrite(void *v)
    320 {
    321 	struct vop_bwrite_args /* {
    322 		struct buf *a_bp;
    323 	} */ *ap = v;
    324 	struct buf *bp = ap->a_bp;
    325 
    326 #ifdef DIAGNOSTIC
    327         if (VTOI(bp->b_vp)->i_lfs->lfs_ronly == 0 && (bp->b_flags & B_ASYNC)) {
    328 		panic("bawrite LFS buffer");
    329 	}
    330 #endif /* DIAGNOSTIC */
    331 	return lfs_bwrite_ext(bp,0);
    332 }
    333 
    334 /*
    335  * Determine if there is enough room currently available to write fsb
    336  * blocks.  We need enough blocks for the new blocks, the current
    337  * inode blocks (including potentially the ifile inode), a summary block,
    338  * and the segment usage table, plus an ifile block.
    339  */
    340 int
    341 lfs_fits(struct lfs *fs, int fsb)
    342 {
    343 	int needed;
    344 
    345 	needed = fsb + btofsb(fs, fs->lfs_sumsize) +
    346 		 ((howmany(fs->lfs_uinodes + 1, INOPB(fs)) + fs->lfs_segtabsz +
    347 		   1) << (fs->lfs_blktodb - fs->lfs_fsbtodb));
    348 
    349 	if (needed >= fs->lfs_avail) {
    350 #ifdef DEBUG
    351 		printf("lfs_fits: no fit: fsb = %d, uinodes = %d, "
    352 		       "needed = %d, avail = %d\n",
    353 		       fsb, fs->lfs_uinodes, needed, fs->lfs_avail);
    354 #endif
    355 		return 0;
    356 	}
    357 	return 1;
    358 }
    359 
    360 int
    361 lfs_availwait(struct lfs *fs, int fsb)
    362 {
    363 	int error;
    364 	CLEANERINFO *cip;
    365 	struct buf *cbp;
    366 
    367 	while (!lfs_fits(fs, fsb)) {
    368 		/*
    369 		 * Out of space, need cleaner to run.
    370 		 * Update the cleaner info, then wake it up.
    371 		 * Note the cleanerinfo block is on the ifile
    372 		 * so it CANT_WAIT.
    373 		 */
    374 		LFS_CLEANERINFO(cip, fs, cbp);
    375 		LFS_SYNC_CLEANERINFO(cip, fs, cbp, 0);
    376 
    377 		printf("lfs_availwait: out of available space, "
    378 		       "waiting on cleaner\n");
    379 
    380 		wakeup(&lfs_allclean_wakeup);
    381 		wakeup(&fs->lfs_nextseg);
    382 #ifdef DIAGNOSTIC
    383 		if (fs->lfs_seglock && fs->lfs_lockpid == curproc->p_pid)
    384 			panic("lfs_availwait: deadlock");
    385 #endif
    386 		error = tsleep(&fs->lfs_avail, PCATCH | PUSER, "cleaner", 0);
    387 		if (error)
    388 			return (error);
    389 	}
    390 	return 0;
    391 }
    392 
    393 int
    394 lfs_bwrite_ext(struct buf *bp, int flags)
    395 {
    396 	struct lfs *fs;
    397 	struct inode *ip;
    398 	int fsb, s;
    399 
    400 	KASSERT(bp->b_flags & B_BUSY);
    401 	KASSERT(flags & BW_CLEAN || !(bp->b_flags & B_CALL));
    402 
    403 	/*
    404 	 * Don't write *any* blocks if we're mounted read-only.
    405 	 * In particular the cleaner can't write blocks either.
    406 	 */
    407         if (VTOI(bp->b_vp)->i_lfs->lfs_ronly) {
    408 		bp->b_flags &= ~(B_DELWRI | B_READ | B_ERROR);
    409 		LFS_UNLOCK_BUF(bp);
    410 		if (bp->b_flags & B_CALL)
    411 			bp->b_flags &= ~B_BUSY;
    412 		else
    413 			brelse(bp);
    414 		return EROFS;
    415 	}
    416 
    417 	/*
    418 	 * Set the delayed write flag and use reassignbuf to move the buffer
    419 	 * from the clean list to the dirty one.
    420 	 *
    421 	 * Set the B_LOCKED flag and unlock the buffer, causing brelse to move
    422 	 * the buffer onto the LOCKED free list.  This is necessary, otherwise
    423 	 * getnewbuf() would try to reclaim the buffers using bawrite, which
    424 	 * isn't going to work.
    425 	 *
    426 	 * XXX we don't let meta-data writes run out of space because they can
    427 	 * come from the segment writer.  We need to make sure that there is
    428 	 * enough space reserved so that there's room to write meta-data
    429 	 * blocks.
    430 	 */
    431 	if (!(bp->b_flags & B_LOCKED)) {
    432 		fs = VFSTOUFS(bp->b_vp->v_mount)->um_lfs;
    433 		fsb = fragstofsb(fs, numfrags(fs, bp->b_bcount));
    434 
    435 		ip = VTOI(bp->b_vp);
    436 		if (flags & BW_CLEAN) {
    437 			LFS_SET_UINO(ip, IN_CLEANING);
    438 		} else {
    439 			LFS_SET_UINO(ip, IN_MODIFIED);
    440 			if (bp->b_lblkno >= 0)
    441 				LFS_SET_UINO(ip, IN_UPDATE);
    442 		}
    443 		fs->lfs_avail -= fsb;
    444 		bp->b_flags |= B_DELWRI;
    445 
    446 		LFS_LOCK_BUF(bp);
    447 		bp->b_flags &= ~(B_READ | B_DONE | B_ERROR);
    448 		s = splbio();
    449 		reassignbuf(bp, bp->b_vp);
    450 		splx(s);
    451 	}
    452 
    453 	if (bp->b_flags & B_CALL)
    454 		bp->b_flags &= ~B_BUSY;
    455 	else
    456 		brelse(bp);
    457 
    458 	return (0);
    459 }
    460 
    461 void
    462 lfs_flush_fs(struct lfs *fs, int flags)
    463 {
    464 	if (fs->lfs_ronly == 0 && fs->lfs_dirops == 0)
    465 	{
    466 		/* disallow dirops during flush */
    467 		fs->lfs_writer++;
    468 
    469 		/*
    470 		 * We set the queue to 0 here because we
    471 		 * are about to write all the dirty
    472 		 * buffers we have.  If more come in
    473 		 * while we're writing the segment, they
    474 		 * may not get written, so we want the
    475 		 * count to reflect these new writes
    476 		 * after the segwrite completes.
    477 		 */
    478 		if (lfs_dostats)
    479 			++lfs_stats.flush_invoked;
    480 		lfs_segwrite(fs->lfs_ivnode->v_mount, flags);
    481 
    482 		/* XXX KS - allow dirops again */
    483 		if (--fs->lfs_writer == 0)
    484 			wakeup(&fs->lfs_dirops);
    485 	}
    486 }
    487 
    488 /*
    489  * XXX
    490  * This routine flushes buffers out of the B_LOCKED queue when LFS has too
    491  * many locked down.  Eventually the pageout daemon will simply call LFS
    492  * when pages need to be reclaimed.  Note, we have one static count of locked
    493  * buffers, so we can't have more than a single file system.  To make this
    494  * work for multiple file systems, put the count into the mount structure.
    495  */
    496 void
    497 lfs_flush(struct lfs *fs, int flags)
    498 {
    499 	struct mount *mp, *nmp;
    500 
    501 	if (lfs_dostats)
    502 		++lfs_stats.write_exceeded;
    503 	if (lfs_writing && flags == 0) {/* XXX flags */
    504 #ifdef DEBUG_LFS
    505 		printf("lfs_flush: not flushing because another flush is active\n");
    506 #endif
    507 		return;
    508 	}
    509 	lfs_writing = 1;
    510 
    511 	simple_lock(&mountlist_slock);
    512 	for (mp = mountlist.cqh_first; mp != (void *)&mountlist; mp = nmp) {
    513 		if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock)) {
    514 			nmp = mp->mnt_list.cqe_next;
    515 			continue;
    516 		}
    517 		if (strncmp(&mp->mnt_stat.f_fstypename[0], MOUNT_LFS, MFSNAMELEN) == 0)
    518 			lfs_flush_fs(((struct ufsmount *)mp->mnt_data)->ufsmount_u.lfs, flags);
    519 		simple_lock(&mountlist_slock);
    520 		nmp = mp->mnt_list.cqe_next;
    521 		vfs_unbusy(mp);
    522 	}
    523 	simple_unlock(&mountlist_slock);
    524 
    525 	LFS_DEBUG_COUNTLOCKED("flush");
    526 
    527 	lfs_writing = 0;
    528 }
    529 
    530 #define INOCOUNT(fs) howmany((fs)->lfs_uinodes, INOPB(fs))
    531 #define INOBYTES(fs) ((fs)->lfs_uinodes * DINODE_SIZE)
    532 
    533 int
    534 lfs_check(struct vnode *vp, ufs_daddr_t blkno, int flags)
    535 {
    536 	int error;
    537 	struct lfs *fs;
    538 	struct inode *ip;
    539 	extern int lfs_dirvcount;
    540 
    541 	error = 0;
    542 	ip = VTOI(vp);
    543 
    544 	/* If out of buffers, wait on writer */
    545 	/* XXX KS - if it's the Ifile, we're probably the cleaner! */
    546 	if (ip->i_number == LFS_IFILE_INUM)
    547 		return 0;
    548 	/* If we're being called from inside a dirop, don't sleep */
    549 	if (ip->i_flag & IN_ADIROP)
    550 		return 0;
    551 
    552 	fs = ip->i_lfs;
    553 
    554 	/*
    555 	 * If we would flush below, but dirops are active, sleep.
    556 	 * Note that a dirop cannot ever reach this code!
    557 	 */
    558 	while (fs->lfs_dirops > 0 &&
    559 	       (locked_queue_count + INOCOUNT(fs) > LFS_MAX_BUFS ||
    560                 locked_queue_bytes + INOBYTES(fs) > LFS_MAX_BYTES ||
    561                 lfs_dirvcount > LFS_MAXDIROP || fs->lfs_diropwait > 0))
    562 	{
    563 		++fs->lfs_diropwait;
    564 		tsleep(&fs->lfs_writer, PRIBIO+1, "bufdirop", 0);
    565 		--fs->lfs_diropwait;
    566 	}
    567 
    568 	if (locked_queue_count + INOCOUNT(fs) > LFS_MAX_BUFS ||
    569 	    locked_queue_bytes + INOBYTES(fs) > LFS_MAX_BYTES ||
    570 	    lfs_dirvcount > LFS_MAXDIROP || fs->lfs_diropwait > 0)
    571 	{
    572 		++fs->lfs_writer;
    573 		lfs_flush(fs, flags);
    574 		if (--fs->lfs_writer == 0)
    575 			wakeup(&fs->lfs_dirops);
    576 	}
    577 
    578 	while (locked_queue_count + INOCOUNT(fs) > LFS_WAIT_BUFS
    579 	       || locked_queue_bytes + INOBYTES(fs) > LFS_WAIT_BYTES)
    580 	{
    581 		if (lfs_dostats)
    582 			++lfs_stats.wait_exceeded;
    583 #ifdef DEBUG_LFS
    584 		printf("lfs_check: waiting: count=%d, bytes=%ld\n",
    585 			locked_queue_count, locked_queue_bytes);
    586 #endif
    587 		error = tsleep(&locked_queue_count, PCATCH | PUSER,
    588 			       "buffers", hz * LFS_BUFWAIT);
    589 		if (error != EWOULDBLOCK)
    590 			break;
    591 		/*
    592 		 * lfs_flush might not flush all the buffers, if some of the
    593 		 * inodes were locked or if most of them were Ifile blocks
    594 		 * and we weren't asked to checkpoint.  Try flushing again
    595 		 * to keep us from blocking indefinitely.
    596 		 */
    597 		if (locked_queue_count + INOCOUNT(fs) > LFS_MAX_BUFS ||
    598 		    locked_queue_bytes + INOBYTES(fs) > LFS_MAX_BYTES)
    599 		{
    600 			++fs->lfs_writer;
    601 			lfs_flush(fs, flags | SEGM_CKP);
    602 			if (--fs->lfs_writer == 0)
    603 				wakeup(&fs->lfs_dirops);
    604 		}
    605 	}
    606 	return (error);
    607 }
    608 
    609 /*
    610  * Allocate a new buffer header.
    611  */
    612 #ifdef MALLOCLOG
    613 # define DOMALLOC(S, T, F) _malloc((S), (T), (F), file, line)
    614 struct buf *
    615 lfs_newbuf_malloclog(struct lfs *fs, struct vnode *vp, ufs_daddr_t daddr, size_t size, char *file, int line)
    616 #else
    617 # define DOMALLOC(S, T, F) malloc((S), (T), (F))
    618 struct buf *
    619 lfs_newbuf(struct lfs *fs, struct vnode *vp, ufs_daddr_t daddr, size_t size)
    620 #endif
    621 {
    622 	struct buf *bp;
    623 	size_t nbytes;
    624 	int s;
    625 
    626 	nbytes = roundup(size, fsbtob(fs, 1));
    627 
    628 	bp = DOMALLOC(sizeof(struct buf), M_SEGMENT, M_WAITOK);
    629 	bzero(bp, sizeof(struct buf));
    630 	if (nbytes) {
    631 		bp->b_data = DOMALLOC(nbytes, M_SEGMENT, M_WAITOK);
    632 		bzero(bp->b_data, nbytes);
    633 	}
    634 #ifdef DIAGNOSTIC
    635 	if (vp == NULL)
    636 		panic("vp is NULL in lfs_newbuf");
    637 	if (bp == NULL)
    638 		panic("bp is NULL after malloc in lfs_newbuf");
    639 #endif
    640 	s = splbio();
    641 	bgetvp(vp, bp);
    642 	splx(s);
    643 
    644 	bp->b_saveaddr = (caddr_t)fs;
    645 	bp->b_bufsize = size;
    646 	bp->b_bcount = size;
    647 	bp->b_lblkno = daddr;
    648 	bp->b_blkno = daddr;
    649 	bp->b_error = 0;
    650 	bp->b_resid = 0;
    651 	bp->b_iodone = lfs_callback;
    652 	bp->b_flags |= B_BUSY | B_CALL | B_NOCACHE;
    653 
    654 	return (bp);
    655 }
    656 
    657 #ifdef MALLOCLOG
    658 # define DOFREE(A, T) _free((A), (T), file, line)
    659 void
    660 lfs_freebuf_malloclog(struct buf *bp, char *file, int line)
    661 #else
    662 # define DOFREE(A, T) free((A), (T))
    663 void
    664 lfs_freebuf(struct buf *bp)
    665 #endif
    666 {
    667 	int s;
    668 
    669 	s = splbio();
    670 	if (bp->b_vp)
    671 		brelvp(bp);
    672 	splx(s);
    673 	if (!(bp->b_flags & B_INVAL)) { /* B_INVAL indicates a "fake" buffer */
    674 		DOFREE(bp->b_data, M_SEGMENT);
    675 		bp->b_data = NULL;
    676 	}
    677 	DOFREE(bp, M_SEGMENT);
    678 }
    679 
    680 /*
    681  * Definitions for the buffer free lists.
    682  */
    683 #define BQUEUES		4		/* number of free buffer queues */
    684 
    685 #define BQ_LOCKED	0		/* super-blocks &c */
    686 #define BQ_LRU		1		/* lru, useful buffers */
    687 #define BQ_AGE		2		/* rubbish */
    688 #define BQ_EMPTY	3		/* buffer headers with no memory */
    689 
    690 extern TAILQ_HEAD(bqueues, buf) bufqueues[BQUEUES];
    691 
    692 /*
    693  * Return a count of buffers on the "locked" queue.
    694  * Don't count malloced buffers, since they don't detract from the total.
    695  */
    696 void
    697 lfs_countlocked(int *count, long *bytes, char *msg)
    698 {
    699 	struct buf *bp;
    700 	int n = 0;
    701 	long int size = 0L;
    702 
    703 	for (bp = bufqueues[BQ_LOCKED].tqh_first; bp;
    704 	    bp = bp->b_freelist.tqe_next) {
    705 		if (bp->b_flags & B_CALL) /* Malloced buffer */
    706 			continue;
    707 		n++;
    708 		size += bp->b_bufsize;
    709 #ifdef DEBUG_LOCKED_LIST
    710 		if (n > nbuf)
    711 			panic("lfs_countlocked: this can't happen: more"
    712 			      " buffers locked than exist");
    713 #endif
    714 	}
    715 #ifdef DEBUG_LOCKED_LIST
    716 	/* Theoretically this function never really does anything */
    717 	if (n != *count)
    718 		printf("lfs_countlocked: %s: adjusted buf count from %d to %d\n",
    719 		       msg, *count, n);
    720 	if (size != *bytes)
    721 		printf("lfs_countlocked: %s: adjusted byte count from %ld to %ld\n",
    722 		       msg, *bytes, size);
    723 #endif
    724 	*count = n;
    725 	*bytes = size;
    726 	return;
    727 }
    728