Home | History | Annotate | Line # | Download | only in lfs
lfs_bio.c revision 1.89
      1 /*	$NetBSD: lfs_bio.c,v 1.89 2006/01/06 09:21:44 yamt 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. Neither the name of the University nor the names of its contributors
     51  *    may be used to endorse or promote products derived from this software
     52  *    without specific prior written permission.
     53  *
     54  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     55  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     56  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     57  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     58  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     59  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     60  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     61  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     62  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     63  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     64  * SUCH DAMAGE.
     65  *
     66  *	@(#)lfs_bio.c	8.10 (Berkeley) 6/10/95
     67  */
     68 
     69 #include <sys/cdefs.h>
     70 __KERNEL_RCSID(0, "$NetBSD: lfs_bio.c,v 1.89 2006/01/06 09:21:44 yamt Exp $");
     71 
     72 #include <sys/param.h>
     73 #include <sys/systm.h>
     74 #include <sys/proc.h>
     75 #include <sys/buf.h>
     76 #include <sys/vnode.h>
     77 #include <sys/resourcevar.h>
     78 #include <sys/mount.h>
     79 #include <sys/kernel.h>
     80 
     81 #include <ufs/ufs/inode.h>
     82 #include <ufs/ufs/ufsmount.h>
     83 #include <ufs/ufs/ufs_extern.h>
     84 
     85 #include <ufs/lfs/lfs.h>
     86 #include <ufs/lfs/lfs_extern.h>
     87 
     88 #include <uvm/uvm.h>
     89 
     90 /* Macros to clear/set/test flags. */
     91 # define	SET(t, f)	(t) |= (f)
     92 # define	CLR(t, f)	(t) &= ~(f)
     93 # define	ISSET(t, f)	((t) & (f))
     94 
     95 /*
     96  * LFS block write function.
     97  *
     98  * XXX
     99  * No write cost accounting is done.
    100  * This is almost certainly wrong for synchronous operations and NFS.
    101  *
    102  * protected by lfs_subsys_lock.
    103  */
    104 int	locked_queue_count   = 0;	/* Count of locked-down buffers. */
    105 long	locked_queue_bytes   = 0L;	/* Total size of locked buffers. */
    106 int	lfs_subsys_pages     = 0L;	/* Total number LFS-written pages */
    107 int	lfs_fs_pagetrip	     = 0;	/* # of pages to trip per-fs write */
    108 int	lfs_writing	     = 0;	/* Set if already kicked off a writer
    109 					   because of buffer space */
    110 /* Lock for aboves */
    111 struct simplelock lfs_subsys_lock = SIMPLELOCK_INITIALIZER;
    112 
    113 extern int lfs_dostats;
    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, bytes_fit;
    130 
    131 	ASSERT_NO_SEGLOCK(fs);
    132 	LOCK_ASSERT(simple_lock_held(&lfs_subsys_lock));
    133 
    134 	count_fit =
    135 	    (locked_queue_count + locked_queue_rcount + n < LFS_WAIT_BUFS);
    136 	bytes_fit =
    137 	    (locked_queue_bytes + locked_queue_rbytes + bytes < LFS_WAIT_BYTES);
    138 
    139 #ifdef DEBUG
    140 	if (!count_fit) {
    141 		DLOG((DLOG_AVAIL, "lfs_fits_buf: no fit count: %d + %d + %d >= %d\n",
    142 		      locked_queue_count, locked_queue_rcount,
    143 		      n, LFS_WAIT_BUFS));
    144 	}
    145 	if (!bytes_fit) {
    146 		DLOG((DLOG_AVAIL, "lfs_fits_buf: no fit bytes: %ld + %ld + %d >= %ld\n",
    147 		      locked_queue_bytes, locked_queue_rbytes,
    148 		      bytes, LFS_WAIT_BYTES));
    149 	}
    150 #endif /* DEBUG */
    151 
    152 	return (count_fit && bytes_fit);
    153 }
    154 
    155 /* ARGSUSED */
    156 int
    157 lfs_reservebuf(struct lfs *fs, struct vnode *vp, struct vnode *vp2,
    158     int n, int bytes)
    159 {
    160 	ASSERT_MAYBE_SEGLOCK(fs);
    161 	KASSERT(locked_queue_rcount >= 0);
    162 	KASSERT(locked_queue_rbytes >= 0);
    163 
    164 	simple_lock(&lfs_subsys_lock);
    165 	while (n > 0 && !lfs_fits_buf(fs, n, bytes)) {
    166 		int error;
    167 
    168 		lfs_flush(fs, 0, 0);
    169 
    170 		error = ltsleep(&locked_queue_count, PCATCH | PUSER,
    171 		    "lfsresbuf", hz * LFS_BUFWAIT, &lfs_subsys_lock);
    172 		if (error && error != EWOULDBLOCK) {
    173 			simple_unlock(&lfs_subsys_lock);
    174 			return error;
    175 		}
    176 	}
    177 
    178 	locked_queue_rcount += n;
    179 	locked_queue_rbytes += bytes;
    180 
    181 	simple_unlock(&lfs_subsys_lock);
    182 
    183 	KASSERT(locked_queue_rcount >= 0);
    184 	KASSERT(locked_queue_rbytes >= 0);
    185 
    186 	return 0;
    187 }
    188 
    189 /*
    190  * Try to reserve some blocks, prior to performing a sensitive operation that
    191  * requires the vnode lock to be honored.  If there is not enough space, give
    192  * up the vnode lock temporarily and wait for the space to become available.
    193  *
    194  * Called with vp locked.  (Note nowever that if fsb < 0, vp is ignored.)
    195  *
    196  * XXX YAMT - it isn't safe to unlock vp here
    197  * because the node might be modified while we sleep.
    198  * (eg. cached states like i_offset might be stale,
    199  *  the vnode might be truncated, etc..)
    200  * maybe we should have a way to restart the vnodeop (EVOPRESTART?)
    201  * or rearrange vnodeop interface to leave vnode locking to file system
    202  * specific code so that each file systems can have their own vnode locking and
    203  * vnode re-using strategies.
    204  */
    205 int
    206 lfs_reserveavail(struct lfs *fs, struct vnode *vp, struct vnode *vp2, int fsb)
    207 {
    208 	CLEANERINFO *cip;
    209 	struct buf *bp;
    210 	int error, slept;
    211 
    212 	ASSERT_MAYBE_SEGLOCK(fs);
    213 	slept = 0;
    214 	simple_lock(&fs->lfs_interlock);
    215 	while (fsb > 0 && !lfs_fits(fs, fsb + fs->lfs_ravail + fs->lfs_favail)) {
    216 		simple_unlock(&fs->lfs_interlock);
    217 #if 0
    218 		/*
    219 		 * XXX ideally, we should unlock vnodes here
    220 		 * because we might sleep very long time.
    221 		 */
    222 		VOP_UNLOCK(vp, 0);
    223 		if (vp2 != NULL) {
    224 			VOP_UNLOCK(vp2, 0);
    225 		}
    226 #else
    227 		/*
    228 		 * XXX since we'll sleep for cleaner with vnode lock holding,
    229 		 * deadlock will occur if cleaner tries to lock the vnode.
    230 		 * (eg. lfs_markv -> lfs_fastvget -> getnewvnode -> vclean)
    231 		 */
    232 #endif
    233 
    234 		if (!slept) {
    235 			DLOG((DLOG_AVAIL, "lfs_reserve: waiting for %ld (bfree = %d,"
    236 			      " est_bfree = %d)\n",
    237 			      fsb + fs->lfs_ravail + fs->lfs_favail,
    238 			      fs->lfs_bfree, LFS_EST_BFREE(fs)));
    239 		}
    240 		++slept;
    241 
    242 		/* Wake up the cleaner */
    243 		LFS_CLEANERINFO(cip, fs, bp);
    244 		LFS_SYNC_CLEANERINFO(cip, fs, bp, 0);
    245 		wakeup(&lfs_allclean_wakeup);
    246 		wakeup(&fs->lfs_nextseg);
    247 
    248 		simple_lock(&fs->lfs_interlock);
    249 		/* Cleaner might have run while we were reading, check again */
    250 		if (lfs_fits(fs, fsb + fs->lfs_ravail + fs->lfs_favail))
    251 			break;
    252 
    253 		error = ltsleep(&fs->lfs_avail, PCATCH | PUSER, "lfs_reserve",
    254 				0, &fs->lfs_interlock);
    255 #if 0
    256 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX use lockstatus */
    257 		vn_lock(vp2, LK_EXCLUSIVE | LK_RETRY); /* XXX use lockstatus */
    258 #endif
    259 		if (error)
    260 			return error;
    261 	}
    262 #ifdef DEBUG
    263 	if (slept) {
    264 		DLOG((DLOG_AVAIL, "lfs_reserve: woke up\n"));
    265 	}
    266 #endif
    267 	fs->lfs_ravail += fsb;
    268 	simple_unlock(&fs->lfs_interlock);
    269 
    270 	return 0;
    271 }
    272 
    273 #ifdef DIAGNOSTIC
    274 int lfs_rescount;
    275 int lfs_rescountdirop;
    276 #endif
    277 
    278 int
    279 lfs_reserve(struct lfs *fs, struct vnode *vp, struct vnode *vp2, int fsb)
    280 {
    281 	int error;
    282 	int cantwait;
    283 
    284 	ASSERT_MAYBE_SEGLOCK(fs);
    285 	if (vp2) {
    286 		/* Make sure we're not in the process of reclaiming vp2 */
    287 		simple_lock(&fs->lfs_interlock);
    288 		while(fs->lfs_flags & LFS_UNDIROP) {
    289 			ltsleep(&fs->lfs_flags, PRIBIO + 1, "lfsrundirop", 0,
    290 			    &fs->lfs_interlock);
    291 		}
    292 		simple_unlock(&fs->lfs_interlock);
    293 	}
    294 
    295 	KASSERT(fsb < 0 || VOP_ISLOCKED(vp));
    296 	KASSERT(vp2 == NULL || fsb < 0 || VOP_ISLOCKED(vp2));
    297 	KASSERT(vp2 == NULL || !(VTOI(vp2)->i_flag & IN_ADIROP));
    298 	KASSERT(vp2 == NULL || vp2 != fs->lfs_unlockvp);
    299 
    300 	cantwait = (VTOI(vp)->i_flag & IN_ADIROP) || fs->lfs_unlockvp == vp;
    301 #ifdef DIAGNOSTIC
    302 	if (cantwait) {
    303 		if (fsb > 0)
    304 			lfs_rescountdirop++;
    305 		else if (fsb < 0)
    306 			lfs_rescountdirop--;
    307 		if (lfs_rescountdirop < 0)
    308 			panic("lfs_rescountdirop");
    309 	}
    310 	else {
    311 		if (fsb > 0)
    312 			lfs_rescount++;
    313 		else if (fsb < 0)
    314 			lfs_rescount--;
    315 		if (lfs_rescount < 0)
    316 			panic("lfs_rescount");
    317 	}
    318 #endif
    319 	if (cantwait)
    320 		return 0;
    321 
    322 	/*
    323 	 * XXX
    324 	 * vref vnodes here so that cleaner doesn't try to reuse them.
    325 	 * (see XXX comment in lfs_reserveavail)
    326 	 */
    327 	lfs_vref(vp);
    328 	if (vp2 != NULL) {
    329 		lfs_vref(vp2);
    330 	}
    331 
    332 	error = lfs_reserveavail(fs, vp, vp2, fsb);
    333 	if (error)
    334 		goto done;
    335 
    336 	/*
    337 	 * XXX just a guess. should be more precise.
    338 	 */
    339 	error = lfs_reservebuf(fs, vp, vp2,
    340 	    fragstoblks(fs, fsb), fsbtob(fs, fsb));
    341 	if (error)
    342 		lfs_reserveavail(fs, vp, vp2, -fsb);
    343 
    344 done:
    345 	lfs_vunref(vp);
    346 	if (vp2 != NULL) {
    347 		lfs_vunref(vp2);
    348 	}
    349 
    350 	return error;
    351 }
    352 
    353 int
    354 lfs_bwrite(void *v)
    355 {
    356 	struct vop_bwrite_args /* {
    357 		struct buf *a_bp;
    358 	} */ *ap = v;
    359 	struct buf *bp = ap->a_bp;
    360 
    361 #ifdef DIAGNOSTIC
    362 	if (VTOI(bp->b_vp)->i_lfs->lfs_ronly == 0 && (bp->b_flags & B_ASYNC)) {
    363 		panic("bawrite LFS buffer");
    364 	}
    365 #endif /* DIAGNOSTIC */
    366 	return lfs_bwrite_ext(bp, 0);
    367 }
    368 
    369 /*
    370  * Determine if there is enough room currently available to write fsb
    371  * blocks.  We need enough blocks for the new blocks, the current
    372  * inode blocks (including potentially the ifile inode), a summary block,
    373  * and the segment usage table, plus an ifile block.
    374  */
    375 int
    376 lfs_fits(struct lfs *fs, int fsb)
    377 {
    378 	int needed;
    379 
    380 	ASSERT_NO_SEGLOCK(fs);
    381 	needed = fsb + btofsb(fs, fs->lfs_sumsize) +
    382 		 ((howmany(fs->lfs_uinodes + 1, INOPB(fs)) + fs->lfs_segtabsz +
    383 		   1) << (fs->lfs_blktodb - fs->lfs_fsbtodb));
    384 
    385 	if (needed >= fs->lfs_avail) {
    386 #ifdef DEBUG
    387 		DLOG((DLOG_AVAIL, "lfs_fits: no fit: fsb = %ld, uinodes = %ld, "
    388 		      "needed = %ld, avail = %ld\n",
    389 		      (long)fsb, (long)fs->lfs_uinodes, (long)needed,
    390 		      (long)fs->lfs_avail));
    391 #endif
    392 		return 0;
    393 	}
    394 	return 1;
    395 }
    396 
    397 int
    398 lfs_availwait(struct lfs *fs, int fsb)
    399 {
    400 	int error;
    401 	CLEANERINFO *cip;
    402 	struct buf *cbp;
    403 
    404 	ASSERT_NO_SEGLOCK(fs);
    405 	/* Push cleaner blocks through regardless */
    406 	simple_lock(&fs->lfs_interlock);
    407 	if (LFS_SEGLOCK_HELD(fs) &&
    408 	    fs->lfs_sp->seg_flags & (SEGM_CLEAN | SEGM_FORCE_CKP)) {
    409 		simple_unlock(&fs->lfs_interlock);
    410 		return 0;
    411 	}
    412 	simple_unlock(&fs->lfs_interlock);
    413 
    414 	while (!lfs_fits(fs, fsb)) {
    415 		/*
    416 		 * Out of space, need cleaner to run.
    417 		 * Update the cleaner info, then wake it up.
    418 		 * Note the cleanerinfo block is on the ifile
    419 		 * so it CANT_WAIT.
    420 		 */
    421 		LFS_CLEANERINFO(cip, fs, cbp);
    422 		LFS_SYNC_CLEANERINFO(cip, fs, cbp, 0);
    423 
    424 #ifdef DEBUG
    425 		DLOG((DLOG_AVAIL, "lfs_availwait: out of available space, "
    426 		      "waiting on cleaner\n"));
    427 #endif
    428 
    429 		wakeup(&lfs_allclean_wakeup);
    430 		wakeup(&fs->lfs_nextseg);
    431 #ifdef DIAGNOSTIC
    432 		if (LFS_SEGLOCK_HELD(fs))
    433 			panic("lfs_availwait: deadlock");
    434 #endif
    435 		error = tsleep(&fs->lfs_avail, PCATCH | PUSER, "cleaner", 0);
    436 		if (error)
    437 			return (error);
    438 	}
    439 	return 0;
    440 }
    441 
    442 int
    443 lfs_bwrite_ext(struct buf *bp, int flags)
    444 {
    445 	struct lfs *fs;
    446 	struct inode *ip;
    447 	int fsb, s;
    448 
    449 	fs = VFSTOUFS(bp->b_vp->v_mount)->um_lfs;
    450 
    451 	ASSERT_MAYBE_SEGLOCK(fs);
    452 	KASSERT(bp->b_flags & B_BUSY);
    453 	KASSERT(flags & BW_CLEAN || !LFS_IS_MALLOC_BUF(bp));
    454 	KASSERT((bp->b_flags & (B_DELWRI|B_LOCKED)) != B_DELWRI);
    455 	KASSERT((bp->b_flags & (B_DELWRI|B_LOCKED)) != B_LOCKED);
    456 
    457 	/*
    458 	 * Don't write *any* blocks if we're mounted read-only, or
    459 	 * if we are "already unmounted".
    460 	 *
    461 	 * In particular the cleaner can't write blocks either.
    462 	 */
    463 	if (fs->lfs_ronly || (fs->lfs_pflags & LFS_PF_CLEAN)) {
    464 		bp->b_flags &= ~(B_DELWRI | B_READ | B_ERROR);
    465 		LFS_UNLOCK_BUF(bp);
    466 		if (LFS_IS_MALLOC_BUF(bp))
    467 			bp->b_flags &= ~B_BUSY;
    468 		else
    469 			brelse(bp);
    470 		return (fs->lfs_ronly ? EROFS : 0);
    471 	}
    472 
    473 	/*
    474 	 * Set the delayed write flag and use reassignbuf to move the buffer
    475 	 * from the clean list to the dirty one.
    476 	 *
    477 	 * Set the B_LOCKED flag and unlock the buffer, causing brelse to move
    478 	 * the buffer onto the LOCKED free list.  This is necessary, otherwise
    479 	 * getnewbuf() would try to reclaim the buffers using bawrite, which
    480 	 * isn't going to work.
    481 	 *
    482 	 * XXX we don't let meta-data writes run out of space because they can
    483 	 * come from the segment writer.  We need to make sure that there is
    484 	 * enough space reserved so that there's room to write meta-data
    485 	 * blocks.
    486 	 */
    487 	if (!(bp->b_flags & B_LOCKED)) {
    488 		fsb = fragstofsb(fs, numfrags(fs, bp->b_bcount));
    489 
    490 		ip = VTOI(bp->b_vp);
    491 		if (flags & BW_CLEAN) {
    492 			LFS_SET_UINO(ip, IN_CLEANING);
    493 		} else {
    494 			LFS_SET_UINO(ip, IN_MODIFIED);
    495 		}
    496 		fs->lfs_avail -= fsb;
    497 		bp->b_flags |= B_DELWRI;
    498 
    499 		LFS_LOCK_BUF(bp);
    500 		bp->b_flags &= ~(B_READ | B_DONE | B_ERROR);
    501 		s = splbio();
    502 		reassignbuf(bp, bp->b_vp);
    503 		splx(s);
    504 	}
    505 
    506 	if (bp->b_flags & B_CALL)
    507 		bp->b_flags &= ~B_BUSY;
    508 	else
    509 		brelse(bp);
    510 
    511 	return (0);
    512 }
    513 
    514 /*
    515  * Called and return with the lfs_interlock held, but the lfs_subsys_lock
    516  * not held.
    517  */
    518 void
    519 lfs_flush_fs(struct lfs *fs, int flags)
    520 {
    521 	ASSERT_NO_SEGLOCK(fs);
    522 	LOCK_ASSERT(simple_lock_held(&fs->lfs_interlock));
    523 	LOCK_ASSERT(!simple_lock_held(&lfs_subsys_lock));
    524 	if (fs->lfs_ronly)
    525 		return;
    526 
    527 	simple_lock(&lfs_subsys_lock);
    528 	if (lfs_dostats)
    529 		++lfs_stats.flush_invoked;
    530 	simple_unlock(&lfs_subsys_lock);
    531 
    532 	simple_unlock(&fs->lfs_interlock);
    533 	lfs_writer_enter(fs, "fldirop");
    534 	lfs_segwrite(fs->lfs_ivnode->v_mount, flags);
    535 	lfs_writer_leave(fs);
    536 	simple_lock(&fs->lfs_interlock);
    537 	fs->lfs_favail = 0; /* XXX */
    538 }
    539 
    540 /*
    541  * This routine initiates segment writes when LFS is consuming too many
    542  * resources.  Ideally the pageout daemon would be able to direct LFS
    543  * more subtly.
    544  * XXX We have one static count of locked buffers;
    545  * XXX need to think more about the multiple filesystem case.
    546  *
    547  * Called and return with lfs_subsys_lock held.
    548  * If fs != NULL, we hold the segment lock for fs.
    549  */
    550 void
    551 lfs_flush(struct lfs *fs, int flags, int only_onefs)
    552 {
    553 	extern u_int64_t locked_fakequeue_count;
    554 	struct mount *mp, *nmp;
    555 	struct lfs *tfs;
    556 
    557 	LOCK_ASSERT(simple_lock_held(&lfs_subsys_lock));
    558 	KDASSERT(fs == NULL || !LFS_SEGLOCK_HELD(fs));
    559 
    560 	if (lfs_dostats)
    561 		++lfs_stats.write_exceeded;
    562 	/* XXX should we include SEGM_CKP here? */
    563 	if (lfs_writing && !(flags & SEGM_SYNC)) {
    564 		DLOG((DLOG_FLUSH, "lfs_flush: not flushing because another flush is active\n"));
    565 		return;
    566 	}
    567 	while (lfs_writing)
    568 		ltsleep(&lfs_writing, PRIBIO + 1, "lfsflush", 0,
    569 			&lfs_subsys_lock);
    570 	lfs_writing = 1;
    571 
    572 	simple_unlock(&lfs_subsys_lock);
    573 
    574 	if (only_onefs) {
    575 		KASSERT(fs != NULL);
    576 		if (vfs_busy(fs->lfs_ivnode->v_mount, LK_NOWAIT,
    577 			     &mountlist_slock))
    578 			goto errout;
    579 		simple_lock(&fs->lfs_interlock);
    580 		lfs_flush_fs(fs, flags);
    581 		simple_unlock(&fs->lfs_interlock);
    582 		vfs_unbusy(fs->lfs_ivnode->v_mount);
    583 	} else {
    584 		locked_fakequeue_count = 0;
    585 		simple_lock(&mountlist_slock);
    586 		for (mp = CIRCLEQ_FIRST(&mountlist); mp != (void *)&mountlist;
    587 		     mp = nmp) {
    588 			if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock)) {
    589 				DLOG((DLOG_FLUSH, "lfs_flush: fs vfs_busy\n"));
    590 				nmp = CIRCLEQ_NEXT(mp, mnt_list);
    591 				continue;
    592 			}
    593 			if (strncmp(&mp->mnt_stat.f_fstypename[0], MOUNT_LFS,
    594 			    MFSNAMELEN) == 0) {
    595 				tfs = VFSTOUFS(mp)->um_lfs;
    596 				simple_lock(&tfs->lfs_interlock);
    597 				lfs_flush_fs(tfs, flags);
    598 				simple_unlock(&tfs->lfs_interlock);
    599 			}
    600 			simple_lock(&mountlist_slock);
    601 			nmp = CIRCLEQ_NEXT(mp, mnt_list);
    602 			vfs_unbusy(mp);
    603 		}
    604 		simple_unlock(&mountlist_slock);
    605 	}
    606 	LFS_DEBUG_COUNTLOCKED("flush");
    607 	wakeup(&lfs_subsys_pages);
    608 
    609     errout:
    610 	simple_lock(&lfs_subsys_lock);
    611 	KASSERT(lfs_writing);
    612 	lfs_writing = 0;
    613 	wakeup(&lfs_writing);
    614 }
    615 
    616 #define INOCOUNT(fs) howmany((fs)->lfs_uinodes, INOPB(fs))
    617 #define INOBYTES(fs) ((fs)->lfs_uinodes * sizeof (struct ufs1_dinode))
    618 
    619 /*
    620  * make sure that we don't have too many locked buffers.
    621  * flush buffers if needed.
    622  */
    623 int
    624 lfs_check(struct vnode *vp, daddr_t blkno, int flags)
    625 {
    626 	int error;
    627 	struct lfs *fs;
    628 	struct inode *ip;
    629 	extern pid_t lfs_writer_daemon;
    630 
    631 	error = 0;
    632 	ip = VTOI(vp);
    633 
    634 	/* If out of buffers, wait on writer */
    635 	/* XXX KS - if it's the Ifile, we're probably the cleaner! */
    636 	if (ip->i_number == LFS_IFILE_INUM)
    637 		return 0;
    638 	/* If we're being called from inside a dirop, don't sleep */
    639 	if (ip->i_flag & IN_ADIROP)
    640 		return 0;
    641 
    642 	fs = ip->i_lfs;
    643 
    644 	ASSERT_NO_SEGLOCK(fs);
    645 	LOCK_ASSERT(!simple_lock_held(&fs->lfs_interlock));
    646 
    647 	/*
    648 	 * If we would flush below, but dirops are active, sleep.
    649 	 * Note that a dirop cannot ever reach this code!
    650 	 */
    651 	simple_lock(&fs->lfs_interlock);
    652 	simple_lock(&lfs_subsys_lock);
    653 	while (fs->lfs_dirops > 0 &&
    654 	       (locked_queue_count + INOCOUNT(fs) > LFS_MAX_BUFS ||
    655 		locked_queue_bytes + INOBYTES(fs) > LFS_MAX_BYTES ||
    656 		lfs_subsys_pages > LFS_MAX_PAGES ||
    657 		lfs_dirvcount > LFS_MAX_DIROP || fs->lfs_diropwait > 0))
    658 	{
    659 		simple_unlock(&lfs_subsys_lock);
    660 		++fs->lfs_diropwait;
    661 		ltsleep(&fs->lfs_writer, PRIBIO+1, "bufdirop", 0,
    662 			&fs->lfs_interlock);
    663 		--fs->lfs_diropwait;
    664 		simple_lock(&lfs_subsys_lock);
    665 	}
    666 
    667 #ifdef DEBUG
    668 	if (locked_queue_count + INOCOUNT(fs) > LFS_MAX_BUFS)
    669 		DLOG((DLOG_FLUSH, "lfs_check: lqc = %d, max %d\n",
    670 		      locked_queue_count + INOCOUNT(fs), LFS_MAX_BUFS));
    671 	if (locked_queue_bytes + INOBYTES(fs) > LFS_MAX_BYTES)
    672 		DLOG((DLOG_FLUSH, "lfs_check: lqb = %ld, max %ld\n",
    673 		      locked_queue_bytes + INOBYTES(fs), LFS_MAX_BYTES));
    674 	if (lfs_subsys_pages > LFS_MAX_PAGES)
    675 		DLOG((DLOG_FLUSH, "lfs_check: lssp = %d, max %d\n",
    676 		      lfs_subsys_pages, LFS_MAX_PAGES));
    677 	if (lfs_fs_pagetrip && fs->lfs_pages > lfs_fs_pagetrip)
    678 		DLOG((DLOG_FLUSH, "lfs_check: fssp = %d, trip at %d\n",
    679 		      fs->lfs_pages, lfs_fs_pagetrip));
    680 	if (lfs_dirvcount > LFS_MAX_DIROP)
    681 		DLOG((DLOG_FLUSH, "lfs_check: ldvc = %d, max %d\n",
    682 		      lfs_dirvcount, LFS_MAX_DIROP));
    683 	if (fs->lfs_diropwait > 0)
    684 		DLOG((DLOG_FLUSH, "lfs_check: ldvw = %d\n",
    685 		      fs->lfs_diropwait));
    686 #endif
    687 
    688 	if (locked_queue_count + INOCOUNT(fs) > LFS_MAX_BUFS ||
    689 	    locked_queue_bytes + INOBYTES(fs) > LFS_MAX_BYTES ||
    690 	    lfs_subsys_pages > LFS_MAX_PAGES ||
    691 	    lfs_dirvcount > LFS_MAX_DIROP || fs->lfs_diropwait > 0) {
    692 		simple_unlock(&fs->lfs_interlock);
    693 		lfs_flush(fs, flags, 0);
    694 	} else if (lfs_fs_pagetrip && fs->lfs_pages > lfs_fs_pagetrip) {
    695 		/*
    696 		 * If we didn't flush the whole thing, some filesystems
    697 		 * still might want to be flushed.
    698 		 */
    699 		++fs->lfs_pdflush;
    700 		wakeup(&lfs_writer_daemon);
    701 		simple_unlock(&fs->lfs_interlock);
    702 	} else
    703 		simple_unlock(&fs->lfs_interlock);
    704 
    705 	while (locked_queue_count + INOCOUNT(fs) > LFS_WAIT_BUFS ||
    706 		locked_queue_bytes + INOBYTES(fs) > LFS_WAIT_BYTES ||
    707 		lfs_subsys_pages > LFS_WAIT_PAGES ||
    708 		lfs_dirvcount > LFS_MAX_DIROP) {
    709 
    710 		if (lfs_dostats)
    711 			++lfs_stats.wait_exceeded;
    712 		DLOG((DLOG_AVAIL, "lfs_check: waiting: count=%d, bytes=%ld\n",
    713 		      locked_queue_count, locked_queue_bytes));
    714 		error = ltsleep(&locked_queue_count, PCATCH | PUSER,
    715 				"buffers", hz * LFS_BUFWAIT, &lfs_subsys_lock);
    716 		if (error != EWOULDBLOCK)
    717 			break;
    718 
    719 		/*
    720 		 * lfs_flush might not flush all the buffers, if some of the
    721 		 * inodes were locked or if most of them were Ifile blocks
    722 		 * and we weren't asked to checkpoint.	Try flushing again
    723 		 * to keep us from blocking indefinitely.
    724 		 */
    725 		if (locked_queue_count + INOCOUNT(fs) > LFS_MAX_BUFS ||
    726 		    locked_queue_bytes + INOBYTES(fs) > LFS_MAX_BYTES) {
    727 			lfs_flush(fs, flags | SEGM_CKP, 0);
    728 		}
    729 	}
    730 	simple_unlock(&lfs_subsys_lock);
    731 	return (error);
    732 }
    733 
    734 /*
    735  * Allocate a new buffer header.
    736  */
    737 struct buf *
    738 lfs_newbuf(struct lfs *fs, struct vnode *vp, daddr_t daddr, size_t size, int type)
    739 {
    740 	struct buf *bp;
    741 	size_t nbytes;
    742 	int s;
    743 
    744 	ASSERT_MAYBE_SEGLOCK(fs);
    745 	nbytes = roundup(size, fsbtob(fs, 1));
    746 
    747 	bp = getiobuf();
    748 	if (nbytes) {
    749 		bp->b_data = lfs_malloc(fs, nbytes, type);
    750 		/* memset(bp->b_data, 0, nbytes); */
    751 	}
    752 #ifdef DIAGNOSTIC
    753 	if (vp == NULL)
    754 		panic("vp is NULL in lfs_newbuf");
    755 	if (bp == NULL)
    756 		panic("bp is NULL after malloc in lfs_newbuf");
    757 #endif
    758 	bp->b_vp = NULL;
    759 	s = splbio();
    760 	bgetvp(vp, bp);
    761 	splx(s);
    762 
    763 	bp->b_bufsize = size;
    764 	bp->b_bcount = size;
    765 	bp->b_lblkno = daddr;
    766 	bp->b_blkno = daddr;
    767 	bp->b_error = 0;
    768 	bp->b_resid = 0;
    769 	bp->b_iodone = lfs_callback;
    770 	bp->b_flags = B_BUSY | B_CALL | B_NOCACHE;
    771 	bp->b_private = fs;
    772 
    773 	return (bp);
    774 }
    775 
    776 void
    777 lfs_freebuf(struct lfs *fs, struct buf *bp)
    778 {
    779 	int s;
    780 
    781 	s = splbio();
    782 	if (bp->b_vp)
    783 		brelvp(bp);
    784 	if (!(bp->b_flags & B_INVAL)) { /* B_INVAL indicates a "fake" buffer */
    785 		lfs_free(fs, bp->b_data, LFS_NB_UNKNOWN);
    786 		bp->b_data = NULL;
    787 	}
    788 	splx(s);
    789 	putiobuf(bp);
    790 }
    791 
    792 /*
    793  * Definitions for the buffer free lists.
    794  */
    795 #define BQUEUES		4		/* number of free buffer queues */
    796 
    797 #define BQ_LOCKED	0		/* super-blocks &c */
    798 #define BQ_LRU		1		/* lru, useful buffers */
    799 #define BQ_AGE		2		/* rubbish */
    800 #define BQ_EMPTY	3		/* buffer headers with no memory */
    801 
    802 extern TAILQ_HEAD(bqueues, buf) bufqueues[BQUEUES];
    803 extern struct simplelock bqueue_slock;
    804 
    805 /*
    806  * Count buffers on the "locked" queue, and compare it to a pro-forma count.
    807  * Don't count malloced buffers, since they don't detract from the total.
    808  */
    809 void
    810 lfs_countlocked(int *count, long *bytes, const char *msg)
    811 {
    812 	struct buf *bp;
    813 	int n = 0;
    814 	long int size = 0L;
    815 	int s;
    816 
    817 	s = splbio();
    818 	simple_lock(&bqueue_slock);
    819 	TAILQ_FOREACH(bp, &bufqueues[BQ_LOCKED], b_freelist) {
    820 		KASSERT(!(bp->b_flags & B_CALL));
    821 		n++;
    822 		size += bp->b_bufsize;
    823 #ifdef DIAGNOSTIC
    824 		if (n > nbuf)
    825 			panic("lfs_countlocked: this can't happen: more"
    826 			      " buffers locked than exist");
    827 #endif
    828 	}
    829 	/*
    830 	 * Theoretically this function never really does anything.
    831 	 * Give a warning if we have to fix the accounting.
    832 	 */
    833 	if (n != *count)
    834 		DLOG((DLOG_LLIST, "lfs_countlocked: %s: adjusted buf count"
    835 		      " from %d to %d\n", msg, *count, n));
    836 	if (size != *bytes)
    837 		DLOG((DLOG_LLIST, "lfs_countlocked: %s: adjusted byte count"
    838 		      " from %ld to %ld\n", msg, *bytes, size));
    839 	*count = n;
    840 	*bytes = size;
    841 	simple_unlock(&bqueue_slock);
    842 	splx(s);
    843 	return;
    844 }
    845