Home | History | Annotate | Line # | Download | only in lfs
lfs_bio.c revision 1.61
      1 /*	$NetBSD: lfs_bio.c,v 1.61 2003/02/20 04:27:23 perseant Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1999, 2000, 2001, 2002, 2003 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.61 2003/02/20 04:27:23 perseant 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 <ufs/lfs/lfs.h>
     90 #include <ufs/lfs/lfs_extern.h>
     91 
     92 #include <uvm/uvm.h>
     93 
     94 /* Macros to clear/set/test flags. */
     95 # define	SET(t, f)	(t) |= (f)
     96 # define	CLR(t, f)	(t) &= ~(f)
     97 # define	ISSET(t, f)	((t) & (f))
     98 
     99 /*
    100  * LFS block write function.
    101  *
    102  * XXX
    103  * No write cost accounting is done.
    104  * This is almost certainly wrong for synchronous operations and NFS.
    105  */
    106 int	locked_queue_count   = 0;	/* Count of locked-down buffers. */
    107 long	locked_queue_bytes   = 0L;	/* Total size of locked buffers. */
    108 int	lfs_subsys_pages     = 0L;	/* Total number LFS-written pages */
    109 int	lfs_writing	     = 0;	/* Set if already kicked off a writer
    110 					   because of buffer space */
    111 struct simplelock lfs_subsys_lock;	/* Lock on subsys_pages */
    112 extern int lfs_dostats;
    113 extern int lfs_do_flush;
    114 
    115 /*
    116  * reserved number/bytes of locked buffers
    117  */
    118 int locked_queue_rcount = 0;
    119 long locked_queue_rbytes = 0L;
    120 
    121 int lfs_fits_buf(struct lfs *, int, int);
    122 int lfs_reservebuf(struct lfs *, struct vnode *vp, struct vnode *vp2,
    123     int, int);
    124 int lfs_reserveavail(struct lfs *, struct vnode *vp, struct vnode *vp2, int);
    125 
    126 int
    127 lfs_fits_buf(struct lfs *fs, int n, int bytes)
    128 {
    129 	int count_fit =
    130 	    (locked_queue_count + locked_queue_rcount + n < LFS_WAIT_BUFS);
    131 	int bytes_fit =
    132 	    (locked_queue_bytes + locked_queue_rbytes + bytes < LFS_WAIT_BYTES);
    133 
    134 #ifdef DEBUG_LFS
    135 	if (!count_fit) {
    136 		printf("lfs_fits_buf: no fit count: %d + %d + %d >= %d\n",
    137 			locked_queue_count, locked_queue_rcount,
    138 			n, LFS_WAIT_BUFS);
    139 	}
    140 	if (!bytes_fit) {
    141 		printf("lfs_fits_buf: no fit bytes: %ld + %ld + %d >= %d\n",
    142 			locked_queue_bytes, locked_queue_rbytes,
    143 			bytes, LFS_WAIT_BYTES);
    144 	}
    145 #endif /* DEBUG_LFS */
    146 
    147 	return (count_fit && bytes_fit);
    148 }
    149 
    150 /* ARGSUSED */
    151 int
    152 lfs_reservebuf(struct lfs *fs, struct vnode *vp, struct vnode *vp2,
    153     int n, int bytes)
    154 {
    155 	KASSERT(locked_queue_rcount >= 0);
    156 	KASSERT(locked_queue_rbytes >= 0);
    157 
    158 	while (n > 0 && !lfs_fits_buf(fs, n, bytes)) {
    159 		int error;
    160 
    161 		++fs->lfs_writer;
    162 		lfs_flush(fs, 0);
    163 		if (--fs->lfs_writer == 0)
    164 			wakeup(&fs->lfs_dirops);
    165 
    166 		error = tsleep(&locked_queue_count, PCATCH | PUSER,
    167 		    "lfsresbuf", hz * LFS_BUFWAIT);
    168 		if (error && error != EWOULDBLOCK)
    169 			return error;
    170 	}
    171 
    172 	locked_queue_rcount += n;
    173 	locked_queue_rbytes += bytes;
    174 
    175 	KASSERT(locked_queue_rcount >= 0);
    176 	KASSERT(locked_queue_rbytes >= 0);
    177 
    178 	return 0;
    179 }
    180 
    181 /*
    182  * Try to reserve some blocks, prior to performing a sensitive operation that
    183  * requires the vnode lock to be honored.  If there is not enough space, give
    184  * up the vnode lock temporarily and wait for the space to become available.
    185  *
    186  * Called with vp locked.  (Note nowever that if fsb < 0, vp is ignored.)
    187  *
    188  * XXX YAMT - it isn't safe to unlock vp here
    189  * because the node might be modified while we sleep.
    190  * (eg. cached states like i_offset might be stale,
    191  *  the vnode might be truncated, etc..)
    192  * maybe we should have a way to restart the vnodeop (EVOPRESTART?)
    193  * or rearrange vnodeop interface to leave vnode locking to file system
    194  * specific code so that each file systems can have their own vnode locking and
    195  * vnode re-using strategies.
    196  */
    197 int
    198 lfs_reserveavail(struct lfs *fs, struct vnode *vp, struct vnode *vp2, int fsb)
    199 {
    200 	CLEANERINFO *cip;
    201 	struct buf *bp;
    202 	int error, slept;
    203 
    204 	slept = 0;
    205 	while (fsb > 0 && !lfs_fits(fs, fsb + fs->lfs_ravail)) {
    206 #if 0
    207 		/*
    208 		 * XXX ideally, we should unlock vnodes here
    209 		 * because we might sleep very long time.
    210 		 */
    211 		VOP_UNLOCK(vp, 0);
    212 		if (vp2 != NULL) {
    213 			VOP_UNLOCK(vp2, 0);
    214 		}
    215 #else
    216 		/*
    217 		 * XXX since we'll sleep for cleaner with vnode lock holding,
    218 		 * deadlock will occur if cleaner tries to lock the vnode.
    219 		 * (eg. lfs_markv -> lfs_fastvget -> getnewvnode -> vclean)
    220 		 */
    221 #endif
    222 
    223 		if (!slept) {
    224 #ifdef DEBUG
    225 			printf("lfs_reserve: waiting for %ld (bfree = %d,"
    226 			       " est_bfree = %d)\n",
    227 			       fsb + fs->lfs_ravail, fs->lfs_bfree,
    228 			       LFS_EST_BFREE(fs));
    229 #endif
    230 		}
    231 		++slept;
    232 
    233 		/* Wake up the cleaner */
    234 		LFS_CLEANERINFO(cip, fs, bp);
    235 		LFS_SYNC_CLEANERINFO(cip, fs, bp, 0);
    236 		wakeup(&lfs_allclean_wakeup);
    237 		wakeup(&fs->lfs_nextseg);
    238 
    239 		error = tsleep(&fs->lfs_avail, PCATCH | PUSER, "lfs_reserve",
    240 			       0);
    241 #if 0
    242 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX use lockstatus */
    243 		vn_lock(vp2, LK_EXCLUSIVE | LK_RETRY); /* XXX use lockstatus */
    244 #endif
    245 		if (error)
    246 			return error;
    247 	}
    248 #ifdef DEBUG
    249 	if (slept)
    250 		printf("lfs_reserve: woke up\n");
    251 #endif
    252 	fs->lfs_ravail += fsb;
    253 
    254 	return 0;
    255 }
    256 
    257 #ifdef DIAGNOSTIC
    258 int lfs_rescount;
    259 int lfs_rescountdirop;
    260 #endif
    261 
    262 int
    263 lfs_reserve(struct lfs *fs, struct vnode *vp, struct vnode *vp2, int fsb)
    264 {
    265 	int error;
    266 	int cantwait;
    267 
    268 	KASSERT(fsb < 0 || VOP_ISLOCKED(vp));
    269 	KASSERT(vp2 == NULL || fsb < 0 || VOP_ISLOCKED(vp2));
    270 	KASSERT(vp2 == NULL || !(VTOI(vp2)->i_flag & IN_ADIROP));
    271 	KASSERT(vp2 == NULL || vp2 != fs->lfs_unlockvp);
    272 
    273 	cantwait = (VTOI(vp)->i_flag & IN_ADIROP) || fs->lfs_unlockvp == vp;
    274 #ifdef DIAGNOSTIC
    275 	if (cantwait) {
    276 		if (fsb > 0)
    277 			lfs_rescountdirop++;
    278 		else if (fsb < 0)
    279 			lfs_rescountdirop--;
    280 		if (lfs_rescountdirop < 0)
    281 			panic("lfs_rescountdirop");
    282 	}
    283 	else {
    284 		if (fsb > 0)
    285 			lfs_rescount++;
    286 		else if (fsb < 0)
    287 			lfs_rescount--;
    288 		if (lfs_rescount < 0)
    289 			panic("lfs_rescount");
    290 	}
    291 #endif
    292 	if (cantwait)
    293 		return 0;
    294 
    295 	/*
    296 	 * XXX
    297 	 * vref vnodes here so that cleaner doesn't try to reuse them.
    298 	 * (see XXX comment in lfs_reserveavail)
    299 	 */
    300 	lfs_vref(vp);
    301 	if (vp2 != NULL) {
    302 		lfs_vref(vp2);
    303 	}
    304 
    305 	error = lfs_reserveavail(fs, vp, vp2, fsb);
    306 	if (error)
    307 		goto done;
    308 
    309 	/*
    310 	 * XXX just a guess. should be more precise.
    311 	 */
    312 	error = lfs_reservebuf(fs, vp, vp2,
    313 	    fragstoblks(fs, fsb), fsbtob(fs, fsb));
    314 	if (error)
    315 		lfs_reserveavail(fs, vp, vp2, -fsb);
    316 
    317 done:
    318 	lfs_vunref(vp);
    319 	if (vp2 != NULL) {
    320 		lfs_vunref(vp2);
    321 	}
    322 
    323 	return error;
    324 }
    325 
    326 int
    327 lfs_bwrite(void *v)
    328 {
    329 	struct vop_bwrite_args /* {
    330 		struct buf *a_bp;
    331 	} */ *ap = v;
    332 	struct buf *bp = ap->a_bp;
    333 
    334 #ifdef DIAGNOSTIC
    335 	if (VTOI(bp->b_vp)->i_lfs->lfs_ronly == 0 && (bp->b_flags & B_ASYNC)) {
    336 		panic("bawrite LFS buffer");
    337 	}
    338 #endif /* DIAGNOSTIC */
    339 	return lfs_bwrite_ext(bp,0);
    340 }
    341 
    342 /*
    343  * Determine if there is enough room currently available to write fsb
    344  * blocks.  We need enough blocks for the new blocks, the current
    345  * inode blocks (including potentially the ifile inode), a summary block,
    346  * and the segment usage table, plus an ifile block.
    347  */
    348 int
    349 lfs_fits(struct lfs *fs, int fsb)
    350 {
    351 	int needed;
    352 
    353 	needed = fsb + btofsb(fs, fs->lfs_sumsize) +
    354 		 ((howmany(fs->lfs_uinodes + 1, INOPB(fs)) + fs->lfs_segtabsz +
    355 		   1) << (fs->lfs_blktodb - fs->lfs_fsbtodb));
    356 
    357 	if (needed >= fs->lfs_avail) {
    358 #ifdef DEBUG
    359 		printf("lfs_fits: no fit: fsb = %d, uinodes = %d, "
    360 		       "needed = %d, avail = %d\n",
    361 		       fsb, fs->lfs_uinodes, needed, fs->lfs_avail);
    362 #endif
    363 		return 0;
    364 	}
    365 	return 1;
    366 }
    367 
    368 int
    369 lfs_availwait(struct lfs *fs, int fsb)
    370 {
    371 	int error;
    372 	CLEANERINFO *cip;
    373 	struct buf *cbp;
    374 
    375 	while (!lfs_fits(fs, fsb)) {
    376 		/*
    377 		 * Out of space, need cleaner to run.
    378 		 * Update the cleaner info, then wake it up.
    379 		 * Note the cleanerinfo block is on the ifile
    380 		 * so it CANT_WAIT.
    381 		 */
    382 		LFS_CLEANERINFO(cip, fs, cbp);
    383 		LFS_SYNC_CLEANERINFO(cip, fs, cbp, 0);
    384 
    385 		printf("lfs_availwait: out of available space, "
    386 		       "waiting on cleaner\n");
    387 
    388 		wakeup(&lfs_allclean_wakeup);
    389 		wakeup(&fs->lfs_nextseg);
    390 #ifdef DIAGNOSTIC
    391 		if (fs->lfs_seglock && fs->lfs_lockpid == curproc->p_pid)
    392 			panic("lfs_availwait: deadlock");
    393 #endif
    394 		error = tsleep(&fs->lfs_avail, PCATCH | PUSER, "cleaner", 0);
    395 		if (error)
    396 			return (error);
    397 	}
    398 	return 0;
    399 }
    400 
    401 int
    402 lfs_bwrite_ext(struct buf *bp, int flags)
    403 {
    404 	struct lfs *fs;
    405 	struct inode *ip;
    406 	int fsb, s;
    407 
    408 	KASSERT(bp->b_flags & B_BUSY);
    409 	KASSERT(flags & BW_CLEAN || !LFS_IS_MALLOC_BUF(bp));
    410 
    411 	/*
    412 	 * Don't write *any* blocks if we're mounted read-only.
    413 	 * In particular the cleaner can't write blocks either.
    414 	 */
    415 	if (VTOI(bp->b_vp)->i_lfs->lfs_ronly) {
    416 		bp->b_flags &= ~(B_DELWRI | B_READ | B_ERROR);
    417 		LFS_UNLOCK_BUF(bp);
    418 		if (LFS_IS_MALLOC_BUF(bp))
    419 			bp->b_flags &= ~B_BUSY;
    420 		else
    421 			brelse(bp);
    422 		return EROFS;
    423 	}
    424 
    425 	/*
    426 	 * Set the delayed write flag and use reassignbuf to move the buffer
    427 	 * from the clean list to the dirty one.
    428 	 *
    429 	 * Set the B_LOCKED flag and unlock the buffer, causing brelse to move
    430 	 * the buffer onto the LOCKED free list.  This is necessary, otherwise
    431 	 * getnewbuf() would try to reclaim the buffers using bawrite, which
    432 	 * isn't going to work.
    433 	 *
    434 	 * XXX we don't let meta-data writes run out of space because they can
    435 	 * come from the segment writer.  We need to make sure that there is
    436 	 * enough space reserved so that there's room to write meta-data
    437 	 * blocks.
    438 	 */
    439 	if (!(bp->b_flags & B_LOCKED)) {
    440 		fs = VFSTOUFS(bp->b_vp->v_mount)->um_lfs;
    441 		fsb = fragstofsb(fs, numfrags(fs, bp->b_bcount));
    442 
    443 		ip = VTOI(bp->b_vp);
    444 		if (flags & BW_CLEAN) {
    445 			LFS_SET_UINO(ip, IN_CLEANING);
    446 		} else {
    447 			LFS_SET_UINO(ip, IN_MODIFIED);
    448 			if (bp->b_lblkno >= 0)
    449 				LFS_SET_UINO(ip, IN_UPDATE);
    450 		}
    451 		fs->lfs_avail -= fsb;
    452 		bp->b_flags |= B_DELWRI;
    453 
    454 		LFS_LOCK_BUF(bp);
    455 		bp->b_flags &= ~(B_READ | B_DONE | B_ERROR);
    456 		s = splbio();
    457 		reassignbuf(bp, bp->b_vp);
    458 		splx(s);
    459 	}
    460 
    461 	if (bp->b_flags & B_CALL)
    462 		bp->b_flags &= ~B_BUSY;
    463 	else
    464 		brelse(bp);
    465 
    466 	return (0);
    467 }
    468 
    469 void
    470 lfs_flush_fs(struct lfs *fs, int flags)
    471 {
    472 	if (fs->lfs_ronly)
    473 		return;
    474 
    475 	/* disallow dirops during flush */
    476 	fs->lfs_writer++;
    477 
    478 	/* drain dirops */
    479 	while (fs->lfs_dirops > 0) {
    480 		++fs->lfs_diropwait;
    481 		tsleep(&fs->lfs_writer, PRIBIO+1, "fldirop", 0);
    482 		--fs->lfs_diropwait;
    483 	}
    484 
    485 	if (lfs_dostats)
    486 		++lfs_stats.flush_invoked;
    487 	lfs_segwrite(fs->lfs_ivnode->v_mount, flags);
    488 
    489 	/* allow dirops again */
    490 	if (--fs->lfs_writer == 0)
    491 		wakeup(&fs->lfs_dirops);
    492 }
    493 
    494 /*
    495  * XXX
    496  * This routine flushes buffers out of the B_LOCKED queue when LFS has too
    497  * many locked down.  Eventually the pageout daemon will simply call LFS
    498  * when pages need to be reclaimed.  Note, we have one static count of locked
    499  * buffers, so we can't have more than a single file system.  To make this
    500  * work for multiple file systems, put the count into the mount structure.
    501  */
    502 void
    503 lfs_flush(struct lfs *fs, int flags)
    504 {
    505 	struct mount *mp, *nmp;
    506 
    507 	if (lfs_dostats)
    508 		++lfs_stats.write_exceeded;
    509 	if (lfs_writing && flags == 0) {/* XXX flags */
    510 #ifdef DEBUG_LFS
    511 		printf("lfs_flush: not flushing because another flush is active\n");
    512 #endif
    513 		return;
    514 	}
    515 	/* XXX MP */
    516 	while (lfs_writing && (flags & SEGM_WRITERD))
    517 		ltsleep(&lfs_writing, PRIBIO + 1, "lfsflush", 0, 0);
    518 	lfs_writing = 1;
    519 
    520 	lfs_subsys_pages = 0; /* XXXUBC need a better way to count this */
    521 	wakeup(&lfs_subsys_pages);
    522 
    523 	simple_lock(&mountlist_slock);
    524 	for (mp = mountlist.cqh_first; mp != (void *)&mountlist; mp = nmp) {
    525 		if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock)) {
    526 			nmp = mp->mnt_list.cqe_next;
    527 			continue;
    528 		}
    529 		if (strncmp(&mp->mnt_stat.f_fstypename[0], MOUNT_LFS, MFSNAMELEN) == 0)
    530 			lfs_flush_fs(((struct ufsmount *)mp->mnt_data)->ufsmount_u.lfs, flags);
    531 		simple_lock(&mountlist_slock);
    532 		nmp = mp->mnt_list.cqe_next;
    533 		vfs_unbusy(mp);
    534 	}
    535 	simple_unlock(&mountlist_slock);
    536 	LFS_DEBUG_COUNTLOCKED("flush");
    537 
    538 	lfs_writing = 0;
    539 	wakeup(&lfs_writing);
    540 }
    541 
    542 #define INOCOUNT(fs) howmany((fs)->lfs_uinodes, INOPB(fs))
    543 #define INOBYTES(fs) ((fs)->lfs_uinodes * DINODE_SIZE)
    544 
    545 int
    546 lfs_check(struct vnode *vp, daddr_t blkno, int flags)
    547 {
    548 	int error;
    549 	struct lfs *fs;
    550 	struct inode *ip;
    551 	extern int lfs_dirvcount;
    552 
    553 	error = 0;
    554 	ip = VTOI(vp);
    555 
    556 	/* If out of buffers, wait on writer */
    557 	/* XXX KS - if it's the Ifile, we're probably the cleaner! */
    558 	if (ip->i_number == LFS_IFILE_INUM)
    559 		return 0;
    560 	/* If we're being called from inside a dirop, don't sleep */
    561 	if (ip->i_flag & IN_ADIROP)
    562 		return 0;
    563 
    564 	fs = ip->i_lfs;
    565 
    566 	/*
    567 	 * If we would flush below, but dirops are active, sleep.
    568 	 * Note that a dirop cannot ever reach this code!
    569 	 */
    570 	while (fs->lfs_dirops > 0 &&
    571 	       (locked_queue_count + INOCOUNT(fs) > LFS_MAX_BUFS ||
    572 		locked_queue_bytes + INOBYTES(fs) > LFS_MAX_BYTES ||
    573 		lfs_subsys_pages > LFS_MAX_PAGES ||
    574 		lfs_dirvcount > LFS_MAX_DIROP || fs->lfs_diropwait > 0))
    575 	{
    576 		++fs->lfs_diropwait;
    577 		tsleep(&fs->lfs_writer, PRIBIO+1, "bufdirop", 0);
    578 		--fs->lfs_diropwait;
    579 	}
    580 
    581 #ifdef DEBUG_LFS_FLUSH
    582 	if (locked_queue_count + INOCOUNT(fs) > LFS_MAX_BUFS)
    583 		printf("lqc = %d, max %d\n", locked_queue_count + INOCOUNT(fs),
    584 			LFS_MAX_BUFS);
    585 	if (locked_queue_bytes + INOBYTES(fs) > LFS_MAX_BYTES)
    586 		printf("lqb = %ld, max %d\n", locked_queue_bytes + INOBYTES(fs),
    587 			LFS_MAX_BYTES);
    588 	if (lfs_subsys_pages > LFS_MAX_PAGES)
    589 		printf("lssp = %d, max %d\n", lfs_subsys_pages, LFS_MAX_PAGES);
    590 	if (lfs_dirvcount > LFS_MAX_DIROP)
    591 		printf("ldvc = %d, max %d\n", lfs_dirvcount, LFS_MAX_DIROP);
    592 	if (fs->lfs_diropwait > 0)
    593 		printf("ldvw = %d\n", fs->lfs_diropwait);
    594 #endif
    595 	if (locked_queue_count + INOCOUNT(fs) > LFS_MAX_BUFS ||
    596 	    locked_queue_bytes + INOBYTES(fs) > LFS_MAX_BYTES ||
    597 	    lfs_subsys_pages > LFS_MAX_PAGES ||
    598 	    lfs_dirvcount > LFS_MAX_DIROP || fs->lfs_diropwait > 0)
    599 	{
    600 		lfs_flush(fs, flags);
    601 	}
    602 
    603 	while (locked_queue_count + INOCOUNT(fs) > LFS_WAIT_BUFS ||
    604 		locked_queue_bytes + INOBYTES(fs) > LFS_WAIT_BYTES ||
    605 		lfs_subsys_pages > LFS_WAIT_PAGES ||
    606 		lfs_dirvcount > LFS_MAX_DIROP)
    607 	{
    608 		if (lfs_dostats)
    609 			++lfs_stats.wait_exceeded;
    610 #ifdef DEBUG_LFS
    611 		printf("lfs_check: waiting: count=%d, bytes=%ld\n",
    612 			locked_queue_count, locked_queue_bytes);
    613 #endif
    614 		error = tsleep(&locked_queue_count, PCATCH | PUSER,
    615 			       "buffers", hz * LFS_BUFWAIT);
    616 		if (error != EWOULDBLOCK)
    617 			break;
    618 		/*
    619 		 * lfs_flush might not flush all the buffers, if some of the
    620 		 * inodes were locked or if most of them were Ifile blocks
    621 		 * and we weren't asked to checkpoint.	Try flushing again
    622 		 * to keep us from blocking indefinitely.
    623 		 */
    624 		if (locked_queue_count + INOCOUNT(fs) > LFS_MAX_BUFS ||
    625 		    locked_queue_bytes + INOBYTES(fs) > LFS_MAX_BYTES)
    626 		{
    627 			lfs_flush(fs, flags | SEGM_CKP);
    628 		}
    629 	}
    630 	return (error);
    631 }
    632 
    633 /*
    634  * Allocate a new buffer header.
    635  */
    636 struct buf *
    637 lfs_newbuf(struct lfs *fs, struct vnode *vp, daddr_t daddr, size_t size, int type)
    638 {
    639 	struct buf *bp;
    640 	size_t nbytes;
    641 	int s;
    642 
    643 	nbytes = roundup(size, fsbtob(fs, 1));
    644 
    645 	s = splbio();
    646 	bp = pool_get(&bufpool, PR_WAITOK);
    647 	splx(s);
    648 	memset(bp, 0, sizeof(struct buf));
    649 	simple_lock_init(&bp->b_interlock);
    650 	if (nbytes) {
    651 		bp->b_data = lfs_malloc(fs, nbytes, type);
    652 		/* memset(bp->b_data, 0, nbytes); */
    653 	}
    654 #ifdef DIAGNOSTIC
    655 	if (vp == NULL)
    656 		panic("vp is NULL in lfs_newbuf");
    657 	if (bp == NULL)
    658 		panic("bp is NULL after malloc in lfs_newbuf");
    659 #endif
    660 	simple_lock_init(&bp->b_interlock);
    661 	s = splbio();
    662 	bgetvp(vp, bp);
    663 	splx(s);
    664 
    665 	bp->b_saveaddr = (caddr_t)fs;
    666 	bp->b_bufsize = size;
    667 	bp->b_bcount = size;
    668 	bp->b_lblkno = daddr;
    669 	bp->b_blkno = daddr;
    670 	bp->b_error = 0;
    671 	bp->b_resid = 0;
    672 	bp->b_iodone = lfs_callback;
    673 	bp->b_flags |= B_BUSY | B_CALL | B_NOCACHE;
    674 
    675 	return (bp);
    676 }
    677 
    678 void
    679 lfs_freebuf(struct lfs *fs, struct buf *bp)
    680 {
    681 	int s;
    682 
    683 	s = splbio();
    684 	if (bp->b_vp)
    685 		brelvp(bp);
    686 	if (!(bp->b_flags & B_INVAL)) { /* B_INVAL indicates a "fake" buffer */
    687 		lfs_free(fs, bp->b_data, LFS_NB_UNKNOWN);
    688 		bp->b_data = NULL;
    689 	}
    690 	pool_put(&bufpool, bp);
    691 	splx(s);
    692 }
    693 
    694 /*
    695  * Definitions for the buffer free lists.
    696  */
    697 #define BQUEUES		4		/* number of free buffer queues */
    698 
    699 #define BQ_LOCKED	0		/* super-blocks &c */
    700 #define BQ_LRU		1		/* lru, useful buffers */
    701 #define BQ_AGE		2		/* rubbish */
    702 #define BQ_EMPTY	3		/* buffer headers with no memory */
    703 
    704 extern TAILQ_HEAD(bqueues, buf) bufqueues[BQUEUES];
    705 
    706 /*
    707  * Return a count of buffers on the "locked" queue.
    708  * Don't count malloced buffers, since they don't detract from the total.
    709  */
    710 void
    711 lfs_countlocked(int *count, long *bytes, char *msg)
    712 {
    713 	struct buf *bp;
    714 	int n = 0;
    715 	long int size = 0L;
    716 
    717 	for (bp = bufqueues[BQ_LOCKED].tqh_first; bp;
    718 	    bp = bp->b_freelist.tqe_next) {
    719 		if (bp->b_flags & B_CALL)
    720 			continue;
    721 		n++;
    722 		size += bp->b_bufsize;
    723 #ifdef DEBUG_LOCKED_LIST
    724 		if (n > nbuf)
    725 			panic("lfs_countlocked: this can't happen: more"
    726 			      " buffers locked than exist");
    727 #endif
    728 	}
    729 #ifdef DEBUG_LOCKED_LIST
    730 	/* Theoretically this function never really does anything */
    731 	if (n != *count)
    732 		printf("lfs_countlocked: %s: adjusted buf count from %d to %d\n",
    733 		       msg, *count, n);
    734 	if (size != *bytes)
    735 		printf("lfs_countlocked: %s: adjusted byte count from %ld to %ld\n",
    736 		       msg, *bytes, size);
    737 #endif
    738 	*count = n;
    739 	*bytes = size;
    740 	return;
    741 }
    742