Home | History | Annotate | Line # | Download | only in lfs
lfs_subr.c revision 1.50.2.6
      1 /*	$NetBSD: lfs_subr.c,v 1.50.2.6 2006/08/10 12:16:46 tron 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_subr.c	8.4 (Berkeley) 5/8/95
     67  */
     68 
     69 #include <sys/cdefs.h>
     70 __KERNEL_RCSID(0, "$NetBSD: lfs_subr.c,v 1.50.2.6 2006/08/10 12:16:46 tron Exp $");
     71 
     72 #include <sys/param.h>
     73 #include <sys/systm.h>
     74 #include <sys/namei.h>
     75 #include <sys/vnode.h>
     76 #include <sys/buf.h>
     77 #include <sys/mount.h>
     78 #include <sys/malloc.h>
     79 #include <sys/proc.h>
     80 
     81 #include <ufs/ufs/inode.h>
     82 #include <ufs/lfs/lfs.h>
     83 #include <ufs/lfs/lfs_extern.h>
     84 
     85 #include <uvm/uvm.h>
     86 
     87 /*
     88  * Return buffer with the contents of block "offset" from the beginning of
     89  * directory "ip".  If "res" is non-zero, fill it in with a pointer to the
     90  * remaining space in the directory.
     91  */
     92 int
     93 lfs_blkatoff(void *v)
     94 {
     95 	struct vop_blkatoff_args /* {
     96 		struct vnode *a_vp;
     97 		off_t a_offset;
     98 		char **a_res;
     99 		struct buf **a_bpp;
    100 		} */ *ap = v;
    101 	struct lfs *fs;
    102 	struct inode *ip;
    103 	struct buf *bp;
    104 	daddr_t lbn;
    105 	int bsize, error;
    106 
    107 	ip = VTOI(ap->a_vp);
    108 	fs = ip->i_lfs;
    109 	lbn = lblkno(fs, ap->a_offset);
    110 	bsize = blksize(fs, ip, lbn);
    111 
    112 	*ap->a_bpp = NULL;
    113 	if ((error = bread(ap->a_vp, lbn, bsize, NOCRED, &bp)) != 0) {
    114 		brelse(bp);
    115 		return (error);
    116 	}
    117 	if (ap->a_res)
    118 		*ap->a_res = (char *)bp->b_data + blkoff(fs, ap->a_offset);
    119 	*ap->a_bpp = bp;
    120 	return (0);
    121 }
    122 
    123 #ifdef DEBUG
    124 const char *lfs_res_names[LFS_NB_COUNT] = {
    125 	"summary",
    126 	"superblock",
    127 	"file block",
    128 	"cluster",
    129 	"clean",
    130 	"blkiov",
    131 };
    132 #endif
    133 
    134 int lfs_res_qty[LFS_NB_COUNT] = {
    135 	LFS_N_SUMMARIES,
    136 	LFS_N_SBLOCKS,
    137 	LFS_N_IBLOCKS,
    138 	LFS_N_CLUSTERS,
    139 	LFS_N_CLEAN,
    140 	LFS_N_BLKIOV,
    141 };
    142 
    143 void
    144 lfs_setup_resblks(struct lfs *fs)
    145 {
    146 	int i, j;
    147 	int maxbpp;
    148 
    149 	ASSERT_NO_SEGLOCK(fs);
    150 	fs->lfs_resblk = (res_t *)malloc(LFS_N_TOTAL * sizeof(res_t), M_SEGMENT,
    151 					  M_WAITOK);
    152 	for (i = 0; i < LFS_N_TOTAL; i++) {
    153 		fs->lfs_resblk[i].inuse = 0;
    154 		fs->lfs_resblk[i].p = NULL;
    155 	}
    156 	for (i = 0; i < LFS_RESHASH_WIDTH; i++)
    157 		LIST_INIT(fs->lfs_reshash + i);
    158 
    159 	/*
    160 	 * These types of allocations can be larger than a page,
    161 	 * so we can't use the pool subsystem for them.
    162 	 */
    163 	for (i = 0, j = 0; j < LFS_N_SUMMARIES; j++, i++)
    164 		fs->lfs_resblk[i].size = fs->lfs_sumsize;
    165 	for (j = 0; j < LFS_N_SBLOCKS; j++, i++)
    166 		fs->lfs_resblk[i].size = LFS_SBPAD;
    167 	for (j = 0; j < LFS_N_IBLOCKS; j++, i++)
    168 		fs->lfs_resblk[i].size = fs->lfs_bsize;
    169 	for (j = 0; j < LFS_N_CLUSTERS; j++, i++)
    170 		fs->lfs_resblk[i].size = MAXPHYS;
    171 	for (j = 0; j < LFS_N_CLEAN; j++, i++)
    172 		fs->lfs_resblk[i].size = MAXPHYS;
    173 	for (j = 0; j < LFS_N_BLKIOV; j++, i++)
    174 		fs->lfs_resblk[i].size = LFS_MARKV_MAXBLKCNT * sizeof(BLOCK_INFO);
    175 
    176 	for (i = 0; i < LFS_N_TOTAL; i++) {
    177 		fs->lfs_resblk[i].p = malloc(fs->lfs_resblk[i].size,
    178 					     M_SEGMENT, M_WAITOK);
    179 	}
    180 
    181 	/*
    182 	 * Initialize pools for small types (XXX is BPP small?)
    183 	 */
    184 	pool_init(&fs->lfs_clpool, sizeof(struct lfs_cluster), 0, 0, 0,
    185 		"lfsclpl", &pool_allocator_nointr);
    186 	pool_init(&fs->lfs_segpool, sizeof(struct segment), 0, 0, 0,
    187 		"lfssegpool", &pool_allocator_nointr);
    188 	maxbpp = ((fs->lfs_sumsize - SEGSUM_SIZE(fs)) / sizeof(int32_t) + 2);
    189 	maxbpp = MIN(maxbpp, segsize(fs) / fs->lfs_fsize + 2);
    190 	pool_init(&fs->lfs_bpppool, maxbpp * sizeof(struct buf *), 0, 0, 0,
    191 		"lfsbpppl", &pool_allocator_nointr);
    192 }
    193 
    194 void
    195 lfs_free_resblks(struct lfs *fs)
    196 {
    197 	int i;
    198 
    199 	pool_destroy(&fs->lfs_bpppool);
    200 	pool_destroy(&fs->lfs_segpool);
    201 	pool_destroy(&fs->lfs_clpool);
    202 
    203 	simple_lock(&fs->lfs_interlock);
    204 	for (i = 0; i < LFS_N_TOTAL; i++) {
    205 		while (fs->lfs_resblk[i].inuse)
    206 			ltsleep(&fs->lfs_resblk, PRIBIO + 1, "lfs_free", 0,
    207 				&fs->lfs_interlock);
    208 		if (fs->lfs_resblk[i].p != NULL)
    209 			free(fs->lfs_resblk[i].p, M_SEGMENT);
    210 	}
    211 	free(fs->lfs_resblk, M_SEGMENT);
    212 	simple_unlock(&fs->lfs_interlock);
    213 }
    214 
    215 static unsigned int
    216 lfs_mhash(void *vp)
    217 {
    218 	return (unsigned int)(((unsigned long)vp) >> 2) % LFS_RESHASH_WIDTH;
    219 }
    220 
    221 /*
    222  * Return memory of the given size for the given purpose, or use one of a
    223  * number of spare last-resort buffers, if malloc returns NULL.
    224  */
    225 void *
    226 lfs_malloc(struct lfs *fs, size_t size, int type)
    227 {
    228 	struct lfs_res_blk *re;
    229 	void *r;
    230 	int i, s, start;
    231 	unsigned int h;
    232 
    233 	ASSERT_MAYBE_SEGLOCK(fs);
    234 	r = NULL;
    235 
    236 	/* If no mem allocated for this type, it just waits */
    237 	if (lfs_res_qty[type] == 0) {
    238 		r = malloc(size, M_SEGMENT, M_WAITOK);
    239 		return r;
    240 	}
    241 
    242 	/* Otherwise try a quick malloc, and if it works, great */
    243 	if ((r = malloc(size, M_SEGMENT, M_NOWAIT)) != NULL) {
    244 		return r;
    245 	}
    246 
    247 	/*
    248 	 * If malloc returned NULL, we are forced to use one of our
    249 	 * reserve blocks.  We have on hand at least one summary block,
    250 	 * at least one cluster block, at least one superblock,
    251 	 * and several indirect blocks.
    252 	 */
    253 
    254 	simple_lock(&fs->lfs_interlock);
    255 	/* skip over blocks of other types */
    256 	for (i = 0, start = 0; i < type; i++)
    257 		start += lfs_res_qty[i];
    258 	while (r == NULL) {
    259 		for (i = 0; i < lfs_res_qty[type]; i++) {
    260 			if (fs->lfs_resblk[start + i].inuse == 0) {
    261 				re = fs->lfs_resblk + start + i;
    262 				re->inuse = 1;
    263 				r = re->p;
    264 				KASSERT(re->size >= size);
    265 				h = lfs_mhash(r);
    266 				s = splbio();
    267 				LIST_INSERT_HEAD(&fs->lfs_reshash[h], re, res);
    268 				splx(s);
    269 				simple_unlock(&fs->lfs_interlock);
    270 				return r;
    271 			}
    272 		}
    273 		DLOG((DLOG_MALLOC, "sleeping on %s (%d)\n",
    274 		      lfs_res_names[type], lfs_res_qty[type]));
    275 		ltsleep(&fs->lfs_resblk, PVM, "lfs_malloc", 0,
    276 			&fs->lfs_interlock);
    277 		DLOG((DLOG_MALLOC, "done sleeping on %s\n",
    278 		      lfs_res_names[type]));
    279 	}
    280 	/* NOTREACHED */
    281 	simple_unlock(&fs->lfs_interlock);
    282 	return r;
    283 }
    284 
    285 void
    286 lfs_free(struct lfs *fs, void *p, int type)
    287 {
    288 	int s;
    289 	unsigned int h;
    290 	res_t *re;
    291 #ifdef DEBUG
    292 	int i;
    293 #endif
    294 
    295 	ASSERT_MAYBE_SEGLOCK(fs);
    296 	h = lfs_mhash(p);
    297 	simple_lock(&fs->lfs_interlock);
    298 	s = splbio();
    299 	LIST_FOREACH(re, &fs->lfs_reshash[h], res) {
    300 		if (re->p == p) {
    301 			KASSERT(re->inuse == 1);
    302 			LIST_REMOVE(re, res);
    303 			re->inuse = 0;
    304 			wakeup(&fs->lfs_resblk);
    305 			splx(s);
    306 			simple_unlock(&fs->lfs_interlock);
    307 			return;
    308 		}
    309 	}
    310 #ifdef DEBUG
    311 	for (i = 0; i < LFS_N_TOTAL; i++) {
    312 		if (fs->lfs_resblk[i].p == p)
    313 			panic("lfs_free: inconsistent reserved block");
    314 	}
    315 #endif
    316 	splx(s);
    317 	simple_unlock(&fs->lfs_interlock);
    318 
    319 	/*
    320 	 * If we didn't find it, free it.
    321 	 */
    322 	free(p, M_SEGMENT);
    323 }
    324 
    325 /*
    326  * lfs_seglock --
    327  *	Single thread the segment writer.
    328  */
    329 int
    330 lfs_seglock(struct lfs *fs, unsigned long flags)
    331 {
    332 	struct segment *sp;
    333 
    334 	simple_lock(&fs->lfs_interlock);
    335 	if (fs->lfs_seglock) {
    336 		if (fs->lfs_lockpid == curproc->p_pid &&
    337 		    fs->lfs_locklwp == curlwp->l_lid) {
    338 			simple_unlock(&fs->lfs_interlock);
    339 			++fs->lfs_seglock;
    340 			fs->lfs_sp->seg_flags |= flags;
    341 			return 0;
    342 		} else if (flags & SEGM_PAGEDAEMON) {
    343 			simple_unlock(&fs->lfs_interlock);
    344 			return EWOULDBLOCK;
    345 		} else {
    346 			while (fs->lfs_seglock) {
    347 				(void)ltsleep(&fs->lfs_seglock, PRIBIO + 1,
    348 					"lfs seglock", 0, &fs->lfs_interlock);
    349 			}
    350 		}
    351 	}
    352 
    353 	fs->lfs_seglock = 1;
    354 	fs->lfs_lockpid = curproc->p_pid;
    355 	fs->lfs_locklwp = curlwp->l_lid;
    356 	simple_unlock(&fs->lfs_interlock);
    357 	fs->lfs_cleanind = 0;
    358 
    359 #ifdef DEBUG
    360 	LFS_ENTER_LOG("seglock", __FILE__, __LINE__, 0, flags, curproc->p_pid);
    361 #endif
    362 	/* Drain fragment size changes out */
    363 	lockmgr(&fs->lfs_fraglock, LK_EXCLUSIVE, 0);
    364 
    365 	sp = fs->lfs_sp = pool_get(&fs->lfs_segpool, PR_WAITOK);
    366 	sp->bpp = pool_get(&fs->lfs_bpppool, PR_WAITOK);
    367 	sp->seg_flags = flags;
    368 	sp->vp = NULL;
    369 	sp->seg_iocount = 0;
    370 	(void) lfs_initseg(fs);
    371 
    372 	/*
    373 	 * Keep a cumulative count of the outstanding I/O operations.  If the
    374 	 * disk drive catches up with us it could go to zero before we finish,
    375 	 * so we artificially increment it by one until we've scheduled all of
    376 	 * the writes we intend to do.
    377 	 */
    378 	simple_lock(&fs->lfs_interlock);
    379 	++fs->lfs_iocount;
    380 	simple_unlock(&fs->lfs_interlock);
    381 	return 0;
    382 }
    383 
    384 static void lfs_unmark_dirop(struct lfs *);
    385 
    386 static void
    387 lfs_unmark_dirop(struct lfs *fs)
    388 {
    389 	struct inode *ip, *nip;
    390 	struct vnode *vp;
    391 	int doit;
    392 
    393 	ASSERT_NO_SEGLOCK(fs);
    394 	simple_lock(&fs->lfs_interlock);
    395 	doit = !(fs->lfs_flags & LFS_UNDIROP);
    396 	if (doit)
    397 		fs->lfs_flags |= LFS_UNDIROP;
    398 	if (!doit) {
    399 		simple_unlock(&fs->lfs_interlock);
    400 		return;
    401 	}
    402 
    403 	for (ip = TAILQ_FIRST(&fs->lfs_dchainhd); ip != NULL; ip = nip) {
    404 		nip = TAILQ_NEXT(ip, i_lfs_dchain);
    405 		simple_unlock(&fs->lfs_interlock);
    406 		vp = ITOV(ip);
    407 
    408 		simple_lock(&vp->v_interlock);
    409 		if (VOP_ISLOCKED(vp) &&
    410 			   vp->v_lock.lk_lockholder != curproc->p_pid) {
    411 			simple_lock(&fs->lfs_interlock);
    412 			simple_unlock(&vp->v_interlock);
    413 			continue;
    414 		}
    415 		if ((VTOI(vp)->i_flag & IN_ADIROP) == 0) {
    416 			simple_lock(&fs->lfs_interlock);
    417 			simple_lock(&lfs_subsys_lock);
    418 			--lfs_dirvcount;
    419 			simple_unlock(&lfs_subsys_lock);
    420 			--fs->lfs_dirvcount;
    421 			vp->v_flag &= ~VDIROP;
    422 			TAILQ_REMOVE(&fs->lfs_dchainhd, ip, i_lfs_dchain);
    423 			simple_unlock(&fs->lfs_interlock);
    424 			wakeup(&lfs_dirvcount);
    425 			simple_unlock(&vp->v_interlock);
    426 			simple_lock(&fs->lfs_interlock);
    427 			fs->lfs_unlockvp = vp;
    428 			simple_unlock(&fs->lfs_interlock);
    429 			vrele(vp);
    430 			simple_lock(&fs->lfs_interlock);
    431 			fs->lfs_unlockvp = NULL;
    432 			simple_unlock(&fs->lfs_interlock);
    433 		} else
    434 			simple_unlock(&vp->v_interlock);
    435 		simple_lock(&fs->lfs_interlock);
    436 	}
    437 
    438 	fs->lfs_flags &= ~LFS_UNDIROP;
    439 	simple_unlock(&fs->lfs_interlock);
    440 	wakeup(&fs->lfs_flags);
    441 }
    442 
    443 static void
    444 lfs_auto_segclean(struct lfs *fs)
    445 {
    446 	int i, error, s, waited;
    447 
    448 	ASSERT_SEGLOCK(fs);
    449 	/*
    450 	 * Now that we've swapped lfs_activesb, but while we still
    451 	 * hold the segment lock, run through the segment list marking
    452 	 * the empty ones clean.
    453 	 * XXX - do we really need to do them all at once?
    454 	 */
    455 	waited = 0;
    456 	for (i = 0; i < fs->lfs_nseg; i++) {
    457 		if ((fs->lfs_suflags[0][i] &
    458 		     (SEGUSE_ACTIVE | SEGUSE_DIRTY | SEGUSE_EMPTY)) ==
    459 		    (SEGUSE_DIRTY | SEGUSE_EMPTY) &&
    460 		    (fs->lfs_suflags[1][i] &
    461 		     (SEGUSE_ACTIVE | SEGUSE_DIRTY | SEGUSE_EMPTY)) ==
    462 		    (SEGUSE_DIRTY | SEGUSE_EMPTY)) {
    463 
    464 			/* Make sure the sb is written before we clean */
    465 			simple_lock(&fs->lfs_interlock);
    466 			s = splbio();
    467 			while (waited == 0 && fs->lfs_sbactive)
    468 				ltsleep(&fs->lfs_sbactive, PRIBIO+1, "lfs asb",
    469 					0, &fs->lfs_interlock);
    470 			splx(s);
    471 			simple_unlock(&fs->lfs_interlock);
    472 			waited = 1;
    473 
    474 			if ((error = lfs_do_segclean(fs, i)) != 0) {
    475 				DLOG((DLOG_CLEAN, "lfs_auto_segclean: lfs_do_segclean returned %d for seg %d\n", error, i));
    476 			}
    477 		}
    478 		fs->lfs_suflags[1 - fs->lfs_activesb][i] =
    479 			fs->lfs_suflags[fs->lfs_activesb][i];
    480 	}
    481 }
    482 
    483 /*
    484  * lfs_segunlock --
    485  *	Single thread the segment writer.
    486  */
    487 void
    488 lfs_segunlock(struct lfs *fs)
    489 {
    490 	struct segment *sp;
    491 	unsigned long sync, ckp;
    492 	struct buf *bp;
    493 	int do_unmark_dirop = 0;
    494 
    495 	sp = fs->lfs_sp;
    496 
    497 	simple_lock(&fs->lfs_interlock);
    498 	LOCK_ASSERT(LFS_SEGLOCK_HELD(fs));
    499 	if (fs->lfs_seglock == 1) {
    500 		if ((sp->seg_flags & (SEGM_PROT | SEGM_CLEAN)) == 0 &&
    501 		    LFS_STARVED_FOR_SEGS(fs) == 0)
    502 			do_unmark_dirop = 1;
    503 		simple_unlock(&fs->lfs_interlock);
    504 		sync = sp->seg_flags & SEGM_SYNC;
    505 		ckp = sp->seg_flags & SEGM_CKP;
    506 
    507 		/* We should have a segment summary, and nothing else */
    508 		KASSERT(sp->cbpp == sp->bpp + 1);
    509 
    510 		/* Free allocated segment summary */
    511 		fs->lfs_offset -= btofsb(fs, fs->lfs_sumsize);
    512 		bp = *sp->bpp;
    513 		lfs_freebuf(fs, bp);
    514 
    515 		pool_put(&fs->lfs_bpppool, sp->bpp);
    516 		sp->bpp = NULL;
    517 
    518 		/*
    519 		 * If we're not sync, we're done with sp, get rid of it.
    520 		 * Otherwise, we keep a local copy around but free
    521 		 * fs->lfs_sp so another process can use it (we have to
    522 		 * wait but they don't have to wait for us).
    523 		 */
    524 		if (!sync)
    525 			pool_put(&fs->lfs_segpool, sp);
    526 		fs->lfs_sp = NULL;
    527 
    528 		/*
    529 		 * If the I/O count is non-zero, sleep until it reaches zero.
    530 		 * At the moment, the user's process hangs around so we can
    531 		 * sleep.
    532 		 */
    533 		simple_lock(&fs->lfs_interlock);
    534 		if (--fs->lfs_iocount == 0)
    535 			LFS_DEBUG_COUNTLOCKED("lfs_segunlock");
    536 		if (fs->lfs_iocount <= 1)
    537 			wakeup(&fs->lfs_iocount);
    538 		simple_unlock(&fs->lfs_interlock);
    539 		/*
    540 		 * If we're not checkpointing, we don't have to block
    541 		 * other processes to wait for a synchronous write
    542 		 * to complete.
    543 		 */
    544 		if (!ckp) {
    545 #ifdef DEBUG
    546 			LFS_ENTER_LOG("segunlock_std", __FILE__, __LINE__, 0, 0, curproc->p_pid);
    547 #endif
    548 			simple_lock(&fs->lfs_interlock);
    549 			--fs->lfs_seglock;
    550 			fs->lfs_lockpid = 0;
    551 			fs->lfs_locklwp = 0;
    552 			simple_unlock(&fs->lfs_interlock);
    553 			wakeup(&fs->lfs_seglock);
    554 		}
    555 		/*
    556 		 * We let checkpoints happen asynchronously.  That means
    557 		 * that during recovery, we have to roll forward between
    558 		 * the two segments described by the first and second
    559 		 * superblocks to make sure that the checkpoint described
    560 		 * by a superblock completed.
    561 		 */
    562 		simple_lock(&fs->lfs_interlock);
    563 		while (ckp && sync && fs->lfs_iocount)
    564 			(void)ltsleep(&fs->lfs_iocount, PRIBIO + 1,
    565 				      "lfs_iocount", 0, &fs->lfs_interlock);
    566 		while (sync && sp->seg_iocount) {
    567 			(void)ltsleep(&sp->seg_iocount, PRIBIO + 1,
    568 				     "seg_iocount", 0, &fs->lfs_interlock);
    569 			DLOG((DLOG_SEG, "sleeping on iocount %x == %d\n", sp, sp->seg_iocount));
    570 		}
    571 		simple_unlock(&fs->lfs_interlock);
    572 		if (sync)
    573 			pool_put(&fs->lfs_segpool, sp);
    574 
    575 		if (ckp) {
    576 			fs->lfs_nactive = 0;
    577 			/* If we *know* everything's on disk, write both sbs */
    578 			/* XXX should wait for this one	 */
    579 			if (sync)
    580 				lfs_writesuper(fs, fs->lfs_sboffs[fs->lfs_activesb]);
    581 			lfs_writesuper(fs, fs->lfs_sboffs[1 - fs->lfs_activesb]);
    582 			if (!(fs->lfs_ivnode->v_mount->mnt_iflag & IMNT_UNMOUNT)) {
    583 				lfs_auto_segclean(fs);
    584 				/* If sync, we can clean the remainder too */
    585 				if (sync)
    586 					lfs_auto_segclean(fs);
    587 			}
    588 			fs->lfs_activesb = 1 - fs->lfs_activesb;
    589 #ifdef DEBUG
    590 			LFS_ENTER_LOG("segunlock_ckp", __FILE__, __LINE__, 0, 0, curproc->p_pid);
    591 #endif
    592 			simple_lock(&fs->lfs_interlock);
    593 			--fs->lfs_seglock;
    594 			fs->lfs_lockpid = 0;
    595 			fs->lfs_locklwp = 0;
    596 			simple_unlock(&fs->lfs_interlock);
    597 			wakeup(&fs->lfs_seglock);
    598 		}
    599 		/* Reenable fragment size changes */
    600 		lockmgr(&fs->lfs_fraglock, LK_RELEASE, 0);
    601 		if (do_unmark_dirop)
    602 			lfs_unmark_dirop(fs);
    603 	} else if (fs->lfs_seglock == 0) {
    604 		simple_unlock(&fs->lfs_interlock);
    605 		panic ("Seglock not held");
    606 	} else {
    607 		--fs->lfs_seglock;
    608 		simple_unlock(&fs->lfs_interlock);
    609 	}
    610 }
    611 
    612 /*
    613  * drain dirops and start writer.
    614  */
    615 int
    616 lfs_writer_enter(struct lfs *fs, const char *wmesg)
    617 {
    618 	int error = 0;
    619 
    620 	ASSERT_MAYBE_SEGLOCK(fs);
    621 	simple_lock(&fs->lfs_interlock);
    622 
    623 	/* disallow dirops during flush */
    624 	fs->lfs_writer++;
    625 
    626 	while (fs->lfs_dirops > 0) {
    627 		++fs->lfs_diropwait;
    628 		error = ltsleep(&fs->lfs_writer, PRIBIO+1, wmesg, 0,
    629 				&fs->lfs_interlock);
    630 		--fs->lfs_diropwait;
    631 	}
    632 
    633 	if (error)
    634 		fs->lfs_writer--;
    635 
    636 	simple_unlock(&fs->lfs_interlock);
    637 
    638 	return error;
    639 }
    640 
    641 void
    642 lfs_writer_leave(struct lfs *fs)
    643 {
    644 	boolean_t dowakeup;
    645 
    646 	ASSERT_MAYBE_SEGLOCK(fs);
    647 	simple_lock(&fs->lfs_interlock);
    648 	dowakeup = !(--fs->lfs_writer);
    649 	simple_unlock(&fs->lfs_interlock);
    650 	if (dowakeup)
    651 		wakeup(&fs->lfs_dirops);
    652 }
    653 
    654 /*
    655  * Unlock, wait for the cleaner, then relock to where we were before.
    656  * To be used only at a fairly high level, to address a paucity of free
    657  * segments propagated back from lfs_gop_write().
    658  */
    659 void
    660 lfs_segunlock_relock(struct lfs *fs)
    661 {
    662 	int n = fs->lfs_seglock;
    663 	u_int16_t seg_flags;
    664 
    665 	if (n == 0)
    666 		return;
    667 
    668 	/* Write anything we've already gathered to disk */
    669 	lfs_writeseg(fs, fs->lfs_sp);
    670 
    671 	/* Save segment flags for later */
    672 	seg_flags = fs->lfs_sp->seg_flags;
    673 
    674 	fs->lfs_sp->seg_flags |= SEGM_PROT; /* Don't unmark dirop nodes */
    675 	while(fs->lfs_seglock)
    676 		lfs_segunlock(fs);
    677 
    678 	/* Wait for the cleaner */
    679 	lfs_wakeup_cleaner(fs);
    680 	simple_lock(&fs->lfs_interlock);
    681 	while (LFS_STARVED_FOR_SEGS(fs))
    682 		ltsleep(&fs->lfs_avail, PRIBIO, "relock", 0,
    683 			&fs->lfs_interlock);
    684 	simple_unlock(&fs->lfs_interlock);
    685 
    686 	/* Put the segment lock back the way it was. */
    687 	while(n--)
    688 		lfs_seglock(fs, seg_flags);
    689 
    690 	return;
    691 }
    692 
    693 /*
    694  * Wake up the cleaner, provided that nowrap is not set.
    695  */
    696 void
    697 lfs_wakeup_cleaner(struct lfs *fs)
    698 {
    699 	if (fs->lfs_nowrap > 0)
    700 		return;
    701 
    702 	wakeup(&fs->lfs_nextseg);
    703 	wakeup(&lfs_allclean_wakeup);
    704 }
    705