Home | History | Annotate | Line # | Download | only in lfs
lfs_bio.c revision 1.140
      1 /*	$NetBSD: lfs_bio.c,v 1.140 2017/06/08 01:23:01 chs Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1999, 2000, 2001, 2002, 2003, 2008 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  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 /*
     32  * Copyright (c) 1991, 1993
     33  *	The Regents of the University of California.  All rights reserved.
     34  *
     35  * Redistribution and use in source and binary forms, with or without
     36  * modification, are permitted provided that the following conditions
     37  * are met:
     38  * 1. Redistributions of source code must retain the above copyright
     39  *    notice, this list of conditions and the following disclaimer.
     40  * 2. Redistributions in binary form must reproduce the above copyright
     41  *    notice, this list of conditions and the following disclaimer in the
     42  *    documentation and/or other materials provided with the distribution.
     43  * 3. Neither the name of the University nor the names of its contributors
     44  *    may be used to endorse or promote products derived from this software
     45  *    without specific prior written permission.
     46  *
     47  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     48  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     49  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     50  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     51  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     52  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     53  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     54  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     55  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     56  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     57  * SUCH DAMAGE.
     58  *
     59  *	@(#)lfs_bio.c	8.10 (Berkeley) 6/10/95
     60  */
     61 
     62 #include <sys/cdefs.h>
     63 __KERNEL_RCSID(0, "$NetBSD: lfs_bio.c,v 1.140 2017/06/08 01:23:01 chs Exp $");
     64 
     65 #include <sys/param.h>
     66 #include <sys/systm.h>
     67 #include <sys/proc.h>
     68 #include <sys/buf.h>
     69 #include <sys/vnode.h>
     70 #include <sys/resourcevar.h>
     71 #include <sys/mount.h>
     72 #include <sys/kernel.h>
     73 #include <sys/kauth.h>
     74 
     75 #include <ufs/lfs/ulfs_inode.h>
     76 #include <ufs/lfs/ulfsmount.h>
     77 #include <ufs/lfs/ulfs_extern.h>
     78 
     79 #include <ufs/lfs/lfs.h>
     80 #include <ufs/lfs/lfs_accessors.h>
     81 #include <ufs/lfs/lfs_extern.h>
     82 #include <ufs/lfs/lfs_kernel.h>
     83 
     84 #include <uvm/uvm.h>
     85 
     86 /*
     87  * LFS block write function.
     88  *
     89  * XXX
     90  * No write cost accounting is done.
     91  * This is almost certainly wrong for synchronous operations and NFS.
     92  *
     93  * protected by lfs_lock.
     94  */
     95 int	locked_queue_count   = 0;	/* Count of locked-down buffers. */
     96 long	locked_queue_bytes   = 0L;	/* Total size of locked buffers. */
     97 int	lfs_subsys_pages     = 0L;	/* Total number LFS-written pages */
     98 int	lfs_fs_pagetrip	     = 0;	/* # of pages to trip per-fs write */
     99 int	lfs_writing	     = 0;	/* Set if already kicked off a writer
    100 					   because of buffer space */
    101 int	locked_queue_waiters = 0;	/* Number of processes waiting on lq */
    102 
    103 /* Lock and condition variables for above. */
    104 kcondvar_t	locked_queue_cv;
    105 kcondvar_t	lfs_writing_cv;
    106 kmutex_t	lfs_lock;
    107 
    108 extern int lfs_dostats;
    109 
    110 /*
    111  * reserved number/bytes of locked buffers
    112  */
    113 int locked_queue_rcount = 0;
    114 long locked_queue_rbytes = 0L;
    115 
    116 static int lfs_fits_buf(struct lfs *, int, int);
    117 static int lfs_reservebuf(struct lfs *, struct vnode *vp, struct vnode *vp2,
    118     int, int);
    119 static int lfs_reserveavail(struct lfs *, struct vnode *vp, struct vnode *vp2,
    120     int);
    121 
    122 static int
    123 lfs_fits_buf(struct lfs *fs, int n, int bytes)
    124 {
    125 	int count_fit, bytes_fit;
    126 
    127 	ASSERT_NO_SEGLOCK(fs);
    128 	KASSERT(mutex_owned(&lfs_lock));
    129 
    130 	count_fit =
    131 	    (locked_queue_count + locked_queue_rcount + n <= LFS_WAIT_BUFS);
    132 	bytes_fit =
    133 	    (locked_queue_bytes + locked_queue_rbytes + bytes <= LFS_WAIT_BYTES);
    134 
    135 #ifdef DEBUG
    136 	if (!count_fit) {
    137 		DLOG((DLOG_AVAIL, "lfs_fits_buf: no fit count: %d + %d + %d >= %d\n",
    138 		      locked_queue_count, locked_queue_rcount,
    139 		      n, LFS_WAIT_BUFS));
    140 	}
    141 	if (!bytes_fit) {
    142 		DLOG((DLOG_AVAIL, "lfs_fits_buf: no fit bytes: %ld + %ld + %d >= %ld\n",
    143 		      locked_queue_bytes, locked_queue_rbytes,
    144 		      bytes, LFS_WAIT_BYTES));
    145 	}
    146 #endif /* DEBUG */
    147 
    148 	return (count_fit && bytes_fit);
    149 }
    150 
    151 /* ARGSUSED */
    152 static int
    153 lfs_reservebuf(struct lfs *fs, struct vnode *vp,
    154     struct vnode *vp2, int n, int bytes)
    155 {
    156 	int cantwait;
    157 
    158 	ASSERT_MAYBE_SEGLOCK(fs);
    159 	KASSERT(locked_queue_rcount >= 0);
    160 	KASSERT(locked_queue_rbytes >= 0);
    161 
    162 	cantwait = (VTOI(vp)->i_flag & IN_ADIROP) || fs->lfs_unlockvp == vp;
    163 	mutex_enter(&lfs_lock);
    164 	while (!cantwait && n > 0 && !lfs_fits_buf(fs, n, bytes)) {
    165 		int error;
    166 
    167 		lfs_flush(fs, 0, 0);
    168 
    169 		DLOG((DLOG_AVAIL, "lfs_reservebuf: waiting: count=%d, bytes=%ld\n",
    170 		      locked_queue_count, locked_queue_bytes));
    171 		++locked_queue_waiters;
    172 		error = cv_timedwait_sig(&locked_queue_cv, &lfs_lock,
    173 		    hz * LFS_BUFWAIT);
    174 		--locked_queue_waiters;
    175 		if (error && error != EWOULDBLOCK) {
    176 			mutex_exit(&lfs_lock);
    177 			return error;
    178 		}
    179 	}
    180 
    181 	locked_queue_rcount += n;
    182 	locked_queue_rbytes += bytes;
    183 
    184 	if (n < 0 && locked_queue_waiters > 0) {
    185 		DLOG((DLOG_AVAIL, "lfs_reservebuf: broadcast: count=%d, bytes=%ld\n",
    186 		      locked_queue_count, locked_queue_bytes));
    187 		cv_broadcast(&locked_queue_cv);
    188 	}
    189 
    190 	mutex_exit(&lfs_lock);
    191 
    192 	KASSERT(locked_queue_rcount >= 0);
    193 	KASSERT(locked_queue_rbytes >= 0);
    194 
    195 	return 0;
    196 }
    197 
    198 /*
    199  * Try to reserve some blocks, prior to performing a sensitive operation that
    200  * requires the vnode lock to be honored.  If there is not enough space, wait
    201  * for the space to become available.
    202  *
    203  * Called with vp locked.  (Note nowever that if fsb < 0, vp is ignored.)
    204  */
    205 static int
    206 lfs_reserveavail(struct lfs *fs, struct vnode *vp,
    207     struct vnode *vp2, int fsb)
    208 {
    209 	CLEANERINFO *cip;
    210 	struct buf *bp;
    211 	int error, slept;
    212 	int cantwait;
    213 
    214 	ASSERT_MAYBE_SEGLOCK(fs);
    215 	slept = 0;
    216 	mutex_enter(&lfs_lock);
    217 	cantwait = (VTOI(vp)->i_flag & IN_ADIROP) || fs->lfs_unlockvp == vp;
    218 	while (!cantwait && fsb > 0 &&
    219 	       !lfs_fits(fs, fsb + fs->lfs_ravail + fs->lfs_favail)) {
    220 		mutex_exit(&lfs_lock);
    221 
    222 		if (!slept) {
    223 			DLOG((DLOG_AVAIL, "lfs_reserve: waiting for %ld (bfree = %jd,"
    224 			      " est_bfree = %jd)\n",
    225 			      fsb + fs->lfs_ravail + fs->lfs_favail,
    226 			      (intmax_t)lfs_sb_getbfree(fs),
    227 			      (intmax_t)LFS_EST_BFREE(fs)));
    228 		}
    229 		++slept;
    230 
    231 		/* Wake up the cleaner */
    232 		LFS_CLEANERINFO(cip, fs, bp);
    233 		LFS_SYNC_CLEANERINFO(cip, fs, bp, 0);
    234 		lfs_wakeup_cleaner(fs);
    235 
    236 		mutex_enter(&lfs_lock);
    237 		/* Cleaner might have run while we were reading, check again */
    238 		if (lfs_fits(fs, fsb + fs->lfs_ravail + fs->lfs_favail))
    239 			break;
    240 
    241 		error = mtsleep(&fs->lfs_availsleep, PCATCH | PUSER,
    242 				"lfs_reserve", 0, &lfs_lock);
    243 		if (error) {
    244 			mutex_exit(&lfs_lock);
    245 			return error;
    246 		}
    247 	}
    248 #ifdef DEBUG
    249 	if (slept) {
    250 		DLOG((DLOG_AVAIL, "lfs_reserve: woke up\n"));
    251 	}
    252 #endif
    253 	fs->lfs_ravail += fsb;
    254 	mutex_exit(&lfs_lock);
    255 
    256 	return 0;
    257 }
    258 
    259 #ifdef DIAGNOSTIC
    260 int lfs_rescount;
    261 int lfs_rescountdirop;
    262 #endif
    263 
    264 int
    265 lfs_reserve(struct lfs *fs, struct vnode *vp, struct vnode *vp2, int fsb)
    266 {
    267 	int error;
    268 
    269 	ASSERT_MAYBE_SEGLOCK(fs);
    270 	if (vp2) {
    271 		/* Make sure we're not in the process of reclaiming vp2 */
    272 		mutex_enter(&lfs_lock);
    273 		while(fs->lfs_flags & LFS_UNDIROP) {
    274 			mtsleep(&fs->lfs_flags, PRIBIO + 1, "lfsrundirop", 0,
    275 			    &lfs_lock);
    276 		}
    277 		mutex_exit(&lfs_lock);
    278 	}
    279 
    280 	KASSERT(fsb < 0 || VOP_ISLOCKED(vp));
    281 	KASSERT(vp2 == NULL || fsb < 0 || VOP_ISLOCKED(vp2));
    282 	KASSERT(vp2 == NULL || vp2 != fs->lfs_unlockvp);
    283 
    284 #ifdef DIAGNOSTIC
    285 	mutex_enter(&lfs_lock);
    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 	mutex_exit(&lfs_lock);
    293 #endif
    294 
    295 	error = lfs_reserveavail(fs, vp, vp2, fsb);
    296 	if (error)
    297 		return error;
    298 
    299 	/*
    300 	 * XXX just a guess. should be more precise.
    301 	 */
    302 	error = lfs_reservebuf(fs, vp, vp2, fsb, lfs_fsbtob(fs, fsb));
    303 	if (error)
    304 		lfs_reserveavail(fs, vp, vp2, -fsb);
    305 
    306 	return error;
    307 }
    308 
    309 int
    310 lfs_max_bufs(void)
    311 {
    312 
    313 	return LFS_MAX_RESOURCE(buf_nbuf(), 1);
    314 }
    315 
    316 int
    317 lfs_wait_bufs(void)
    318 {
    319 
    320 	return LFS_WAIT_RESOURCE(buf_nbuf(), 1);
    321 }
    322 
    323 int
    324 lfs_bwrite(void *v)
    325 {
    326 	struct vop_bwrite_args /* {
    327 		struct vnode *a_vp;
    328 		struct buf *a_bp;
    329 	} */ *ap = v;
    330 	struct buf *bp = ap->a_bp;
    331 
    332 	KASSERTMSG((VTOI(bp->b_vp)->i_lfs->lfs_ronly ||
    333 		!(bp->b_flags & B_ASYNC)),
    334 	    "bawrite LFS buffer");
    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 	int64_t needed;
    348 
    349 	ASSERT_NO_SEGLOCK(fs);
    350 	needed = fsb + lfs_btofsb(fs, lfs_sb_getsumsize(fs)) +
    351 		 ((howmany(lfs_sb_getuinodes(fs) + 1, LFS_INOPB(fs)) +
    352 		   lfs_sb_getsegtabsz(fs) +
    353 		   1) << (lfs_sb_getbshift(fs) - lfs_sb_getffshift(fs)));
    354 
    355 	if (needed >= lfs_sb_getavail(fs)) {
    356 #ifdef DEBUG
    357 		DLOG((DLOG_AVAIL, "lfs_fits: no fit: fsb = %ld, uinodes = %ld, "
    358 		      "needed = %jd, avail = %jd\n",
    359 		      (long)fsb, (long)lfs_sb_getuinodes(fs), (intmax_t)needed,
    360 		      (intmax_t)lfs_sb_getavail(fs)));
    361 #endif
    362 		return 0;
    363 	}
    364 	return 1;
    365 }
    366 
    367 int
    368 lfs_availwait(struct lfs *fs, int fsb)
    369 {
    370 	int error;
    371 	CLEANERINFO *cip;
    372 	struct buf *cbp;
    373 
    374 	ASSERT_NO_SEGLOCK(fs);
    375 	/* Push cleaner blocks through regardless */
    376 	mutex_enter(&lfs_lock);
    377 	if (LFS_SEGLOCK_HELD(fs) &&
    378 	    fs->lfs_sp->seg_flags & (SEGM_CLEAN | SEGM_FORCE_CKP)) {
    379 		mutex_exit(&lfs_lock);
    380 		return 0;
    381 	}
    382 	mutex_exit(&lfs_lock);
    383 
    384 	while (!lfs_fits(fs, fsb)) {
    385 		/*
    386 		 * Out of space, need cleaner to run.
    387 		 * Update the cleaner info, then wake it up.
    388 		 * Note the cleanerinfo block is on the ifile
    389 		 * so it CANT_WAIT.
    390 		 */
    391 		LFS_CLEANERINFO(cip, fs, cbp);
    392 		LFS_SYNC_CLEANERINFO(cip, fs, cbp, 0);
    393 
    394 #ifdef DEBUG
    395 		DLOG((DLOG_AVAIL, "lfs_availwait: out of available space, "
    396 		      "waiting on cleaner\n"));
    397 #endif
    398 
    399 		lfs_wakeup_cleaner(fs);
    400 		KASSERTMSG(!LFS_SEGLOCK_HELD(fs), "lfs_availwait: deadlock");
    401 		error = tsleep(&fs->lfs_availsleep, PCATCH | PUSER,
    402 			       "cleaner", 0);
    403 		if (error)
    404 			return (error);
    405 	}
    406 	return 0;
    407 }
    408 
    409 int
    410 lfs_bwrite_ext(struct buf *bp, int flags)
    411 {
    412 	struct lfs *fs;
    413 	struct inode *ip;
    414 	struct vnode *vp;
    415 	int fsb;
    416 
    417 	vp = bp->b_vp;
    418 	fs = VFSTOULFS(vp->v_mount)->um_lfs;
    419 
    420 	ASSERT_MAYBE_SEGLOCK(fs);
    421 	KASSERT(bp->b_cflags & BC_BUSY);
    422 	KASSERT(flags & BW_CLEAN || !LFS_IS_MALLOC_BUF(bp));
    423 	KASSERT(((bp->b_oflags | bp->b_flags) & (BO_DELWRI|B_LOCKED))
    424 	    != BO_DELWRI);
    425 
    426 	/*
    427 	 * Don't write *any* blocks if we're mounted read-only, or
    428 	 * if we are "already unmounted".
    429 	 *
    430 	 * In particular the cleaner can't write blocks either.
    431 	 */
    432 	if (fs->lfs_ronly || (lfs_sb_getpflags(fs) & LFS_PF_CLEAN)) {
    433 		bp->b_oflags &= ~BO_DELWRI;
    434 		bp->b_flags |= B_READ; /* XXX is this right? --ks */
    435 		bp->b_error = 0;
    436 		mutex_enter(&bufcache_lock);
    437 		LFS_UNLOCK_BUF(bp);
    438 		if (LFS_IS_MALLOC_BUF(bp))
    439 			bp->b_cflags &= ~BC_BUSY;
    440 		else
    441 			brelsel(bp, 0);
    442 		mutex_exit(&bufcache_lock);
    443 		return (fs->lfs_ronly ? EROFS : 0);
    444 	}
    445 
    446 	/*
    447 	 * Set the delayed write flag and use reassignbuf to move the buffer
    448 	 * from the clean list to the dirty one.
    449 	 *
    450 	 * Set the B_LOCKED flag and unlock the buffer, causing brelse to move
    451 	 * the buffer onto the LOCKED free list.  This is necessary, otherwise
    452 	 * getnewbuf() would try to reclaim the buffers using bawrite, which
    453 	 * isn't going to work.
    454 	 *
    455 	 * XXX we don't let meta-data writes run out of space because they can
    456 	 * come from the segment writer.  We need to make sure that there is
    457 	 * enough space reserved so that there's room to write meta-data
    458 	 * blocks.
    459 	 */
    460 	if ((bp->b_flags & B_LOCKED) == 0) {
    461 		fsb = lfs_numfrags(fs, bp->b_bcount);
    462 
    463 		ip = VTOI(vp);
    464 		mutex_enter(&lfs_lock);
    465 		if (flags & BW_CLEAN) {
    466 			LFS_SET_UINO(ip, IN_CLEANING);
    467 		} else {
    468 			LFS_SET_UINO(ip, IN_MODIFIED);
    469 		}
    470 		mutex_exit(&lfs_lock);
    471 		lfs_sb_subavail(fs, fsb);
    472 
    473 		mutex_enter(&bufcache_lock);
    474 		mutex_enter(vp->v_interlock);
    475 		bp->b_oflags = (bp->b_oflags | BO_DELWRI) & ~BO_DONE;
    476 		LFS_LOCK_BUF(bp);
    477 		bp->b_flags &= ~B_READ;
    478 		bp->b_error = 0;
    479 		reassignbuf(bp, bp->b_vp);
    480 		mutex_exit(vp->v_interlock);
    481 	} else {
    482 		mutex_enter(&bufcache_lock);
    483 	}
    484 
    485 	if (bp->b_iodone != NULL)
    486 		bp->b_cflags &= ~BC_BUSY;
    487 	else
    488 		brelsel(bp, 0);
    489 	mutex_exit(&bufcache_lock);
    490 
    491 	return (0);
    492 }
    493 
    494 /*
    495  * Called and return with the lfs_lock held.
    496  */
    497 void
    498 lfs_flush_fs(struct lfs *fs, int flags)
    499 {
    500 	ASSERT_NO_SEGLOCK(fs);
    501 	KASSERT(mutex_owned(&lfs_lock));
    502 	if (fs->lfs_ronly)
    503 		return;
    504 
    505 	if (lfs_dostats)
    506 		++lfs_stats.flush_invoked;
    507 
    508 	fs->lfs_pdflush = 0;
    509 	mutex_exit(&lfs_lock);
    510 	lfs_writer_enter(fs, "fldirop");
    511 	lfs_segwrite(fs->lfs_ivnode->v_mount, flags);
    512 	lfs_writer_leave(fs);
    513 	mutex_enter(&lfs_lock);
    514 	fs->lfs_favail = 0; /* XXX */
    515 }
    516 
    517 /*
    518  * This routine initiates segment writes when LFS is consuming too many
    519  * resources.  Ideally the pageout daemon would be able to direct LFS
    520  * more subtly.
    521  * XXX We have one static count of locked buffers;
    522  * XXX need to think more about the multiple filesystem case.
    523  *
    524  * Called and return with lfs_lock held.
    525  * If fs != NULL, we hold the segment lock for fs.
    526  */
    527 void
    528 lfs_flush(struct lfs *fs, int flags, int only_onefs)
    529 {
    530 	extern u_int64_t locked_fakequeue_count;
    531 	mount_iterator_t *iter;
    532 	struct mount *mp;
    533 	struct lfs *tfs;
    534 
    535 	KASSERT(mutex_owned(&lfs_lock));
    536 	KDASSERT(fs == NULL || !LFS_SEGLOCK_HELD(fs));
    537 
    538 	if (lfs_dostats)
    539 		++lfs_stats.write_exceeded;
    540 	/* XXX should we include SEGM_CKP here? */
    541 	if (lfs_writing && !(flags & SEGM_SYNC)) {
    542 		DLOG((DLOG_FLUSH, "lfs_flush: not flushing because another flush is active\n"));
    543 		return;
    544 	}
    545 	while (lfs_writing)
    546 		cv_wait(&lfs_writing_cv, &lfs_lock);
    547 	lfs_writing = 1;
    548 
    549 	mutex_exit(&lfs_lock);
    550 
    551 	if (only_onefs) {
    552 		KASSERT(fs != NULL);
    553 		if (vfs_busy(fs->lfs_ivnode->v_mount))
    554 			goto errout;
    555 		mutex_enter(&lfs_lock);
    556 		lfs_flush_fs(fs, flags);
    557 		mutex_exit(&lfs_lock);
    558 		vfs_unbusy(fs->lfs_ivnode->v_mount);
    559 	} else {
    560 		locked_fakequeue_count = 0;
    561 		mountlist_iterator_init(&iter);
    562 		while ((mp = mountlist_iterator_next(iter)) != NULL) {
    563 			if (strncmp(&mp->mnt_stat.f_fstypename[0], MOUNT_LFS,
    564 			    sizeof(mp->mnt_stat.f_fstypename)) == 0) {
    565 				tfs = VFSTOULFS(mp)->um_lfs;
    566 				mutex_enter(&lfs_lock);
    567 				lfs_flush_fs(tfs, flags);
    568 				mutex_exit(&lfs_lock);
    569 			}
    570 		}
    571 		mountlist_iterator_destroy(iter);
    572 	}
    573 	wakeup(&lfs_subsys_pages);
    574 
    575     errout:
    576 	mutex_enter(&lfs_lock);
    577 	KASSERT(lfs_writing);
    578 	lfs_writing = 0;
    579 	wakeup(&lfs_writing);
    580 }
    581 
    582 #define INOCOUNT(fs) howmany(lfs_sb_getuinodes(fs), LFS_INOPB(fs))
    583 #define INOBYTES(fs) (lfs_sb_getuinodes(fs) * DINOSIZE(fs))
    584 
    585 /*
    586  * make sure that we don't have too many locked buffers.
    587  * flush buffers if needed.
    588  */
    589 int
    590 lfs_check(struct vnode *vp, daddr_t blkno, int flags)
    591 {
    592 	int error;
    593 	struct lfs *fs;
    594 	struct inode *ip;
    595 	extern kcondvar_t lfs_writerd_cv;
    596 
    597 	error = 0;
    598 	ip = VTOI(vp);
    599 
    600 	/* If out of buffers, wait on writer */
    601 	/* XXX KS - if it's the Ifile, we're probably the cleaner! */
    602 	if (ip->i_number == LFS_IFILE_INUM)
    603 		return 0;
    604 	/* If we're being called from inside a dirop, don't sleep */
    605 	if (ip->i_flag & IN_ADIROP)
    606 		return 0;
    607 
    608 	fs = ip->i_lfs;
    609 
    610 	ASSERT_NO_SEGLOCK(fs);
    611 
    612 	/*
    613 	 * If we would flush below, but dirops are active, sleep.
    614 	 * Note that a dirop cannot ever reach this code!
    615 	 */
    616 	mutex_enter(&lfs_lock);
    617 	while (fs->lfs_dirops > 0 &&
    618 	       (locked_queue_count + INOCOUNT(fs) > LFS_MAX_BUFS ||
    619 		locked_queue_bytes + INOBYTES(fs) > LFS_MAX_BYTES ||
    620 		lfs_subsys_pages > LFS_MAX_PAGES ||
    621 		fs->lfs_dirvcount > LFS_MAX_FSDIROP(fs) ||
    622 		lfs_dirvcount > LFS_MAX_DIROP || fs->lfs_diropwait > 0))
    623 	{
    624 		++fs->lfs_diropwait;
    625 		mtsleep(&fs->lfs_writer, PRIBIO+1, "bufdirop", 0,
    626 			&lfs_lock);
    627 		--fs->lfs_diropwait;
    628 	}
    629 
    630 #ifdef DEBUG
    631 	if (locked_queue_count + INOCOUNT(fs) > LFS_MAX_BUFS)
    632 		DLOG((DLOG_FLUSH, "lfs_check: lqc = %d, max %d\n",
    633 		      locked_queue_count + INOCOUNT(fs), LFS_MAX_BUFS));
    634 	if (locked_queue_bytes + INOBYTES(fs) > LFS_MAX_BYTES)
    635 		DLOG((DLOG_FLUSH, "lfs_check: lqb = %ld, max %ld\n",
    636 		      locked_queue_bytes + INOBYTES(fs), LFS_MAX_BYTES));
    637 	if (lfs_subsys_pages > LFS_MAX_PAGES)
    638 		DLOG((DLOG_FLUSH, "lfs_check: lssp = %d, max %d\n",
    639 		      lfs_subsys_pages, LFS_MAX_PAGES));
    640 	if (lfs_fs_pagetrip && fs->lfs_pages > lfs_fs_pagetrip)
    641 		DLOG((DLOG_FLUSH, "lfs_check: fssp = %d, trip at %d\n",
    642 		      fs->lfs_pages, lfs_fs_pagetrip));
    643 	if (lfs_dirvcount > LFS_MAX_DIROP)
    644 		DLOG((DLOG_FLUSH, "lfs_check: ldvc = %d, max %d\n",
    645 		      lfs_dirvcount, LFS_MAX_DIROP));
    646 	if (fs->lfs_dirvcount > LFS_MAX_FSDIROP(fs))
    647 		DLOG((DLOG_FLUSH, "lfs_check: lfdvc = %d, max %d\n",
    648 		      fs->lfs_dirvcount, LFS_MAX_FSDIROP(fs)));
    649 	if (fs->lfs_diropwait > 0)
    650 		DLOG((DLOG_FLUSH, "lfs_check: ldvw = %d\n",
    651 		      fs->lfs_diropwait));
    652 #endif
    653 
    654 	/* If there are too many pending dirops, we have to flush them. */
    655 	if (fs->lfs_dirvcount > LFS_MAX_FSDIROP(fs) ||
    656 	    lfs_dirvcount > LFS_MAX_DIROP || fs->lfs_diropwait > 0) {
    657 		mutex_exit(&lfs_lock);
    658 		lfs_flush_dirops(fs);
    659 		mutex_enter(&lfs_lock);
    660 	} else if (locked_queue_count + INOCOUNT(fs) > LFS_MAX_BUFS ||
    661 	    locked_queue_bytes + INOBYTES(fs) > LFS_MAX_BYTES ||
    662 	    lfs_subsys_pages > LFS_MAX_PAGES ||
    663 	    fs->lfs_dirvcount > LFS_MAX_FSDIROP(fs) ||
    664 	    lfs_dirvcount > LFS_MAX_DIROP || fs->lfs_diropwait > 0) {
    665 		lfs_flush(fs, flags, 0);
    666 	} else if (lfs_fs_pagetrip && fs->lfs_pages > lfs_fs_pagetrip) {
    667 		/*
    668 		 * If we didn't flush the whole thing, some filesystems
    669 		 * still might want to be flushed.
    670 		 */
    671 		++fs->lfs_pdflush;
    672 		cv_broadcast(&lfs_writerd_cv);
    673 	}
    674 
    675 	while (locked_queue_count + INOCOUNT(fs) >= LFS_WAIT_BUFS ||
    676 		locked_queue_bytes + INOBYTES(fs) >= LFS_WAIT_BYTES ||
    677 		lfs_subsys_pages > LFS_WAIT_PAGES ||
    678 		fs->lfs_dirvcount > LFS_MAX_FSDIROP(fs) ||
    679 		lfs_dirvcount > LFS_MAX_DIROP) {
    680 
    681 		if (lfs_dostats)
    682 			++lfs_stats.wait_exceeded;
    683 		DLOG((DLOG_AVAIL, "lfs_check: waiting: count=%d, bytes=%ld\n",
    684 		      locked_queue_count, locked_queue_bytes));
    685 		++locked_queue_waiters;
    686 		error = cv_timedwait_sig(&locked_queue_cv, &lfs_lock,
    687 		    hz * LFS_BUFWAIT);
    688 		--locked_queue_waiters;
    689 		if (error != EWOULDBLOCK)
    690 			break;
    691 
    692 		/*
    693 		 * lfs_flush might not flush all the buffers, if some of the
    694 		 * inodes were locked or if most of them were Ifile blocks
    695 		 * and we weren't asked to checkpoint.	Try flushing again
    696 		 * to keep us from blocking indefinitely.
    697 		 */
    698 		if (locked_queue_count + INOCOUNT(fs) >= LFS_MAX_BUFS ||
    699 		    locked_queue_bytes + INOBYTES(fs) >= LFS_MAX_BYTES) {
    700 			lfs_flush(fs, flags | SEGM_CKP, 0);
    701 		}
    702 	}
    703 	mutex_exit(&lfs_lock);
    704 	return (error);
    705 }
    706 
    707 /*
    708  * Allocate a new buffer header.
    709  */
    710 struct buf *
    711 lfs_newbuf(struct lfs *fs, struct vnode *vp, daddr_t daddr, size_t size, int type)
    712 {
    713 	struct buf *bp;
    714 	size_t nbytes;
    715 
    716 	ASSERT_MAYBE_SEGLOCK(fs);
    717 	nbytes = roundup(size, lfs_fsbtob(fs, 1));
    718 
    719 	bp = getiobuf(NULL, true);
    720 	if (nbytes) {
    721 		bp->b_data = lfs_malloc(fs, nbytes, type);
    722 		/* memset(bp->b_data, 0, nbytes); */
    723 	}
    724 	KASSERT(vp != NULL);
    725 	KASSERT(bp != NULL);
    726 
    727 	bp->b_bufsize = size;
    728 	bp->b_bcount = size;
    729 	bp->b_lblkno = daddr;
    730 	bp->b_blkno = daddr;
    731 	bp->b_error = 0;
    732 	bp->b_resid = 0;
    733 	bp->b_iodone = lfs_callback;
    734 	bp->b_cflags = BC_BUSY | BC_NOCACHE;
    735 	bp->b_private = fs;
    736 
    737 	mutex_enter(&bufcache_lock);
    738 	mutex_enter(vp->v_interlock);
    739 	bgetvp(vp, bp);
    740 	mutex_exit(vp->v_interlock);
    741 	mutex_exit(&bufcache_lock);
    742 
    743 	return (bp);
    744 }
    745 
    746 void
    747 lfs_freebuf(struct lfs *fs, struct buf *bp)
    748 {
    749 	struct vnode *vp;
    750 
    751 	if ((vp = bp->b_vp) != NULL) {
    752 		mutex_enter(&bufcache_lock);
    753 		mutex_enter(vp->v_interlock);
    754 		brelvp(bp);
    755 		mutex_exit(vp->v_interlock);
    756 		mutex_exit(&bufcache_lock);
    757 	}
    758 	if (!(bp->b_cflags & BC_INVAL)) { /* BC_INVAL indicates a "fake" buffer */
    759 		lfs_free(fs, bp->b_data, LFS_NB_UNKNOWN);
    760 		bp->b_data = NULL;
    761 	}
    762 	putiobuf(bp);
    763 }
    764 
    765 int
    766 lfs_wait_pages(void)
    767 {
    768 	int active, inactive;
    769 
    770 	uvm_estimatepageable(&active, &inactive);
    771 	return LFS_WAIT_RESOURCE(active + inactive + uvmexp.free, 1);
    772 }
    773 
    774 int
    775 lfs_max_pages(void)
    776 {
    777 	int active, inactive;
    778 
    779 	uvm_estimatepageable(&active, &inactive);
    780 	return LFS_MAX_RESOURCE(active + inactive + uvmexp.free, 1);
    781 }
    782