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