Home | History | Annotate | Line # | Download | only in lfs
lfs_subr.c revision 1.57
      1 /*	$NetBSD: lfs_subr.c,v 1.57 2006/03/24 20:05:32 perseant Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1999, 2000, 2001, 2002, 2003 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Konrad E. Schroder <perseant (at) hhhh.org>.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the NetBSD
     21  *	Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 /*
     39  * Copyright (c) 1991, 1993
     40  *	The Regents of the University of California.  All rights reserved.
     41  *
     42  * Redistribution and use in source and binary forms, with or without
     43  * modification, are permitted provided that the following conditions
     44  * are met:
     45  * 1. Redistributions of source code must retain the above copyright
     46  *    notice, this list of conditions and the following disclaimer.
     47  * 2. Redistributions in binary form must reproduce the above copyright
     48  *    notice, this list of conditions and the following disclaimer in the
     49  *    documentation and/or other materials provided with the distribution.
     50  * 3. 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.57 2006/03/24 20:05:32 perseant 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 #ifdef DEBUG
     88 const char *lfs_res_names[LFS_NB_COUNT] = {
     89 	"summary",
     90 	"superblock",
     91 	"file block",
     92 	"cluster",
     93 	"clean",
     94 	"blkiov",
     95 };
     96 #endif
     97 
     98 int lfs_res_qty[LFS_NB_COUNT] = {
     99 	LFS_N_SUMMARIES,
    100 	LFS_N_SBLOCKS,
    101 	LFS_N_IBLOCKS,
    102 	LFS_N_CLUSTERS,
    103 	LFS_N_CLEAN,
    104 	LFS_N_BLKIOV,
    105 };
    106 
    107 void
    108 lfs_setup_resblks(struct lfs *fs)
    109 {
    110 	int i, j;
    111 	int maxbpp;
    112 
    113 	ASSERT_NO_SEGLOCK(fs);
    114 	fs->lfs_resblk = (res_t *)malloc(LFS_N_TOTAL * sizeof(res_t), M_SEGMENT,
    115 					  M_WAITOK);
    116 	for (i = 0; i < LFS_N_TOTAL; i++) {
    117 		fs->lfs_resblk[i].inuse = 0;
    118 		fs->lfs_resblk[i].p = NULL;
    119 	}
    120 	for (i = 0; i < LFS_RESHASH_WIDTH; i++)
    121 		LIST_INIT(fs->lfs_reshash + i);
    122 
    123 	/*
    124 	 * These types of allocations can be larger than a page,
    125 	 * so we can't use the pool subsystem for them.
    126 	 */
    127 	for (i = 0, j = 0; j < LFS_N_SUMMARIES; j++, i++)
    128 		fs->lfs_resblk[i].size = fs->lfs_sumsize;
    129 	for (j = 0; j < LFS_N_SBLOCKS; j++, i++)
    130 		fs->lfs_resblk[i].size = LFS_SBPAD;
    131 	for (j = 0; j < LFS_N_IBLOCKS; j++, i++)
    132 		fs->lfs_resblk[i].size = fs->lfs_bsize;
    133 	for (j = 0; j < LFS_N_CLUSTERS; j++, i++)
    134 		fs->lfs_resblk[i].size = MAXPHYS;
    135 	for (j = 0; j < LFS_N_CLEAN; j++, i++)
    136 		fs->lfs_resblk[i].size = MAXPHYS;
    137 	for (j = 0; j < LFS_N_BLKIOV; j++, i++)
    138 		fs->lfs_resblk[i].size = LFS_MARKV_MAXBLKCNT * sizeof(BLOCK_INFO);
    139 
    140 	for (i = 0; i < LFS_N_TOTAL; i++) {
    141 		fs->lfs_resblk[i].p = malloc(fs->lfs_resblk[i].size,
    142 					     M_SEGMENT, M_WAITOK);
    143 	}
    144 
    145 	/*
    146 	 * Initialize pools for small types (XXX is BPP small?)
    147 	 */
    148 	pool_init(&fs->lfs_clpool, sizeof(struct lfs_cluster), 0, 0, 0,
    149 		"lfsclpl", &pool_allocator_nointr);
    150 	pool_init(&fs->lfs_segpool, sizeof(struct segment), 0, 0, 0,
    151 		"lfssegpool", &pool_allocator_nointr);
    152 	maxbpp = ((fs->lfs_sumsize - SEGSUM_SIZE(fs)) / sizeof(int32_t) + 2);
    153 	maxbpp = MIN(maxbpp, segsize(fs) / fs->lfs_fsize + 2);
    154 	pool_init(&fs->lfs_bpppool, maxbpp * sizeof(struct buf *), 0, 0, 0,
    155 		"lfsbpppl", &pool_allocator_nointr);
    156 }
    157 
    158 void
    159 lfs_free_resblks(struct lfs *fs)
    160 {
    161 	int i;
    162 
    163 	pool_destroy(&fs->lfs_bpppool);
    164 	pool_destroy(&fs->lfs_segpool);
    165 	pool_destroy(&fs->lfs_clpool);
    166 
    167 	simple_lock(&fs->lfs_interlock);
    168 	for (i = 0; i < LFS_N_TOTAL; i++) {
    169 		while (fs->lfs_resblk[i].inuse)
    170 			ltsleep(&fs->lfs_resblk, PRIBIO + 1, "lfs_free", 0,
    171 				&fs->lfs_interlock);
    172 		if (fs->lfs_resblk[i].p != NULL)
    173 			free(fs->lfs_resblk[i].p, M_SEGMENT);
    174 	}
    175 	free(fs->lfs_resblk, M_SEGMENT);
    176 	simple_unlock(&fs->lfs_interlock);
    177 }
    178 
    179 static unsigned int
    180 lfs_mhash(void *vp)
    181 {
    182 	return (unsigned int)(((unsigned long)vp) >> 2) % LFS_RESHASH_WIDTH;
    183 }
    184 
    185 /*
    186  * Return memory of the given size for the given purpose, or use one of a
    187  * number of spare last-resort buffers, if malloc returns NULL.
    188  */
    189 void *
    190 lfs_malloc(struct lfs *fs, size_t size, int type)
    191 {
    192 	struct lfs_res_blk *re;
    193 	void *r;
    194 	int i, s, start;
    195 	unsigned int h;
    196 
    197 	ASSERT_MAYBE_SEGLOCK(fs);
    198 	r = NULL;
    199 
    200 	/* If no mem allocated for this type, it just waits */
    201 	if (lfs_res_qty[type] == 0) {
    202 		r = malloc(size, M_SEGMENT, M_WAITOK);
    203 		return r;
    204 	}
    205 
    206 	/* Otherwise try a quick malloc, and if it works, great */
    207 	if ((r = malloc(size, M_SEGMENT, M_NOWAIT)) != NULL) {
    208 		return r;
    209 	}
    210 
    211 	/*
    212 	 * If malloc returned NULL, we are forced to use one of our
    213 	 * reserve blocks.  We have on hand at least one summary block,
    214 	 * at least one cluster block, at least one superblock,
    215 	 * and several indirect blocks.
    216 	 */
    217 
    218 	simple_lock(&fs->lfs_interlock);
    219 	/* skip over blocks of other types */
    220 	for (i = 0, start = 0; i < type; i++)
    221 		start += lfs_res_qty[i];
    222 	while (r == NULL) {
    223 		for (i = 0; i < lfs_res_qty[type]; i++) {
    224 			if (fs->lfs_resblk[start + i].inuse == 0) {
    225 				re = fs->lfs_resblk + start + i;
    226 				re->inuse = 1;
    227 				r = re->p;
    228 				KASSERT(re->size >= size);
    229 				h = lfs_mhash(r);
    230 				s = splbio();
    231 				LIST_INSERT_HEAD(&fs->lfs_reshash[h], re, res);
    232 				splx(s);
    233 				simple_unlock(&fs->lfs_interlock);
    234 				return r;
    235 			}
    236 		}
    237 		DLOG((DLOG_MALLOC, "sleeping on %s (%d)\n",
    238 		      lfs_res_names[type], lfs_res_qty[type]));
    239 		ltsleep(&fs->lfs_resblk, PVM, "lfs_malloc", 0,
    240 			&fs->lfs_interlock);
    241 		DLOG((DLOG_MALLOC, "done sleeping on %s\n",
    242 		      lfs_res_names[type]));
    243 	}
    244 	/* NOTREACHED */
    245 	simple_unlock(&fs->lfs_interlock);
    246 	return r;
    247 }
    248 
    249 void
    250 lfs_free(struct lfs *fs, void *p, int type)
    251 {
    252 	int s;
    253 	unsigned int h;
    254 	res_t *re;
    255 #ifdef DEBUG
    256 	int i;
    257 #endif
    258 
    259 	ASSERT_MAYBE_SEGLOCK(fs);
    260 	h = lfs_mhash(p);
    261 	simple_lock(&fs->lfs_interlock);
    262 	s = splbio();
    263 	LIST_FOREACH(re, &fs->lfs_reshash[h], res) {
    264 		if (re->p == p) {
    265 			KASSERT(re->inuse == 1);
    266 			LIST_REMOVE(re, res);
    267 			re->inuse = 0;
    268 			wakeup(&fs->lfs_resblk);
    269 			splx(s);
    270 			simple_unlock(&fs->lfs_interlock);
    271 			return;
    272 		}
    273 	}
    274 #ifdef DEBUG
    275 	for (i = 0; i < LFS_N_TOTAL; i++) {
    276 		if (fs->lfs_resblk[i].p == p)
    277 			panic("lfs_free: inconsistent reserved block");
    278 	}
    279 #endif
    280 	splx(s);
    281 	simple_unlock(&fs->lfs_interlock);
    282 
    283 	/*
    284 	 * If we didn't find it, free it.
    285 	 */
    286 	free(p, M_SEGMENT);
    287 }
    288 
    289 /*
    290  * lfs_seglock --
    291  *	Single thread the segment writer.
    292  */
    293 int
    294 lfs_seglock(struct lfs *fs, unsigned long flags)
    295 {
    296 	struct segment *sp;
    297 
    298 	simple_lock(&fs->lfs_interlock);
    299 	if (fs->lfs_seglock) {
    300 		if (fs->lfs_lockpid == curproc->p_pid) {
    301 			simple_unlock(&fs->lfs_interlock);
    302 			++fs->lfs_seglock;
    303 			fs->lfs_sp->seg_flags |= flags;
    304 			return 0;
    305 		} else if (flags & SEGM_PAGEDAEMON) {
    306 			simple_unlock(&fs->lfs_interlock);
    307 			return EWOULDBLOCK;
    308 		} else {
    309 			while (fs->lfs_seglock) {
    310 				(void)ltsleep(&fs->lfs_seglock, PRIBIO + 1,
    311 					"lfs seglock", 0, &fs->lfs_interlock);
    312 			}
    313 		}
    314 	}
    315 
    316 	fs->lfs_seglock = 1;
    317 	fs->lfs_lockpid = curproc->p_pid;
    318 	simple_unlock(&fs->lfs_interlock);
    319 	fs->lfs_cleanind = 0;
    320 
    321 #ifdef DEBUG
    322 	LFS_ENTER_LOG("seglock", __FILE__, __LINE__, 0, flags, curproc->p_pid);
    323 #endif
    324 	/* Drain fragment size changes out */
    325 	lockmgr(&fs->lfs_fraglock, LK_EXCLUSIVE, 0);
    326 
    327 	sp = fs->lfs_sp = pool_get(&fs->lfs_segpool, PR_WAITOK);
    328 	sp->bpp = pool_get(&fs->lfs_bpppool, PR_WAITOK);
    329 	sp->seg_flags = flags;
    330 	sp->vp = NULL;
    331 	sp->seg_iocount = 0;
    332 	(void) lfs_initseg(fs);
    333 
    334 	/*
    335 	 * Keep a cumulative count of the outstanding I/O operations.  If the
    336 	 * disk drive catches up with us it could go to zero before we finish,
    337 	 * so we artificially increment it by one until we've scheduled all of
    338 	 * the writes we intend to do.
    339 	 */
    340 	simple_lock(&fs->lfs_interlock);
    341 	++fs->lfs_iocount;
    342 	simple_unlock(&fs->lfs_interlock);
    343 	return 0;
    344 }
    345 
    346 static void lfs_unmark_dirop(struct lfs *);
    347 
    348 static void
    349 lfs_unmark_dirop(struct lfs *fs)
    350 {
    351 	struct inode *ip, *nip;
    352 	struct vnode *vp;
    353 	int doit;
    354 
    355 	ASSERT_NO_SEGLOCK(fs);
    356 	simple_lock(&fs->lfs_interlock);
    357 	doit = !(fs->lfs_flags & LFS_UNDIROP);
    358 	if (doit)
    359 		fs->lfs_flags |= LFS_UNDIROP;
    360 	if (!doit) {
    361 		simple_unlock(&fs->lfs_interlock);
    362 		return;
    363 	}
    364 
    365 	for (ip = TAILQ_FIRST(&fs->lfs_dchainhd); ip != NULL; ip = nip) {
    366 		nip = TAILQ_NEXT(ip, i_lfs_dchain);
    367 		simple_unlock(&fs->lfs_interlock);
    368 		vp = ITOV(ip);
    369 
    370 		simple_lock(&vp->v_interlock);
    371 		if (VOP_ISLOCKED(vp) &&
    372 			   vp->v_lock.lk_lockholder != curproc->p_pid) {
    373 			simple_lock(&fs->lfs_interlock);
    374 			simple_unlock(&vp->v_interlock);
    375 			continue;
    376 		}
    377 		if ((VTOI(vp)->i_flag & IN_ADIROP) == 0) {
    378 			simple_lock(&fs->lfs_interlock);
    379 			simple_lock(&lfs_subsys_lock);
    380 			--lfs_dirvcount;
    381 			simple_unlock(&lfs_subsys_lock);
    382 			vp->v_flag &= ~VDIROP;
    383 			TAILQ_REMOVE(&fs->lfs_dchainhd, ip, i_lfs_dchain);
    384 			simple_unlock(&fs->lfs_interlock);
    385 			wakeup(&lfs_dirvcount);
    386 			simple_unlock(&vp->v_interlock);
    387 			simple_lock(&fs->lfs_interlock);
    388 			fs->lfs_unlockvp = vp;
    389 			simple_unlock(&fs->lfs_interlock);
    390 			vrele(vp);
    391 			simple_lock(&fs->lfs_interlock);
    392 			fs->lfs_unlockvp = NULL;
    393 			simple_unlock(&fs->lfs_interlock);
    394 		} else
    395 			simple_unlock(&vp->v_interlock);
    396 		simple_lock(&fs->lfs_interlock);
    397 	}
    398 
    399 	fs->lfs_flags &= ~LFS_UNDIROP;
    400 	simple_unlock(&fs->lfs_interlock);
    401 	wakeup(&fs->lfs_flags);
    402 }
    403 
    404 static void
    405 lfs_auto_segclean(struct lfs *fs)
    406 {
    407 	int i, error, s, waited;
    408 
    409 	ASSERT_SEGLOCK(fs);
    410 	/*
    411 	 * Now that we've swapped lfs_activesb, but while we still
    412 	 * hold the segment lock, run through the segment list marking
    413 	 * the empty ones clean.
    414 	 * XXX - do we really need to do them all at once?
    415 	 */
    416 	waited = 0;
    417 	for (i = 0; i < fs->lfs_nseg; i++) {
    418 		if ((fs->lfs_suflags[0][i] &
    419 		     (SEGUSE_ACTIVE | SEGUSE_DIRTY | SEGUSE_EMPTY)) ==
    420 		    (SEGUSE_DIRTY | SEGUSE_EMPTY) &&
    421 		    (fs->lfs_suflags[1][i] &
    422 		     (SEGUSE_ACTIVE | SEGUSE_DIRTY | SEGUSE_EMPTY)) ==
    423 		    (SEGUSE_DIRTY | SEGUSE_EMPTY)) {
    424 
    425 			/* Make sure the sb is written before we clean */
    426 			simple_lock(&fs->lfs_interlock);
    427 			s = splbio();
    428 			while (waited == 0 && fs->lfs_sbactive)
    429 				ltsleep(&fs->lfs_sbactive, PRIBIO+1, "lfs asb",
    430 					0, &fs->lfs_interlock);
    431 			splx(s);
    432 			simple_unlock(&fs->lfs_interlock);
    433 			waited = 1;
    434 
    435 			if ((error = lfs_do_segclean(fs, i)) != 0) {
    436 				DLOG((DLOG_CLEAN, "lfs_auto_segclean: lfs_do_segclean returned %d for seg %d\n", error, i));
    437 			}
    438 		}
    439 		fs->lfs_suflags[1 - fs->lfs_activesb][i] =
    440 			fs->lfs_suflags[fs->lfs_activesb][i];
    441 	}
    442 }
    443 
    444 /*
    445  * lfs_segunlock --
    446  *	Single thread the segment writer.
    447  */
    448 void
    449 lfs_segunlock(struct lfs *fs)
    450 {
    451 	struct segment *sp;
    452 	unsigned long sync, ckp;
    453 	struct buf *bp;
    454 	int do_unmark_dirop = 0;
    455 
    456 	sp = fs->lfs_sp;
    457 
    458 	simple_lock(&fs->lfs_interlock);
    459 	LOCK_ASSERT(LFS_SEGLOCK_HELD(fs));
    460 	if (fs->lfs_seglock == 1) {
    461 		if ((sp->seg_flags & (SEGM_PROT | SEGM_CLEAN)) == 0 &&
    462 		    LFS_STARVED_FOR_SEGS(fs) == 0)
    463 			do_unmark_dirop = 1;
    464 		simple_unlock(&fs->lfs_interlock);
    465 		sync = sp->seg_flags & SEGM_SYNC;
    466 		ckp = sp->seg_flags & SEGM_CKP;
    467 
    468 		/* We should have a segment summary, and nothing else */
    469 		KASSERT(sp->cbpp == sp->bpp + 1);
    470 
    471 		/* Free allocated segment summary */
    472 		fs->lfs_offset -= btofsb(fs, fs->lfs_sumsize);
    473 		bp = *sp->bpp;
    474 		lfs_freebuf(fs, bp);
    475 
    476 		pool_put(&fs->lfs_bpppool, sp->bpp);
    477 		sp->bpp = NULL;
    478 
    479 		/*
    480 		 * If we're not sync, we're done with sp, get rid of it.
    481 		 * Otherwise, we keep a local copy around but free
    482 		 * fs->lfs_sp so another process can use it (we have to
    483 		 * wait but they don't have to wait for us).
    484 		 */
    485 		if (!sync)
    486 			pool_put(&fs->lfs_segpool, sp);
    487 		fs->lfs_sp = NULL;
    488 
    489 		/*
    490 		 * If the I/O count is non-zero, sleep until it reaches zero.
    491 		 * At the moment, the user's process hangs around so we can
    492 		 * sleep.
    493 		 */
    494 		simple_lock(&fs->lfs_interlock);
    495 		if (--fs->lfs_iocount == 0)
    496 			LFS_DEBUG_COUNTLOCKED("lfs_segunlock");
    497 		if (fs->lfs_iocount <= 1)
    498 			wakeup(&fs->lfs_iocount);
    499 		simple_unlock(&fs->lfs_interlock);
    500 		/*
    501 		 * If we're not checkpointing, we don't have to block
    502 		 * other processes to wait for a synchronous write
    503 		 * to complete.
    504 		 */
    505 		if (!ckp) {
    506 #ifdef DEBUG
    507 			LFS_ENTER_LOG("segunlock_std", __FILE__, __LINE__, 0, 0, curproc->p_pid);
    508 #endif
    509 			simple_lock(&fs->lfs_interlock);
    510 			--fs->lfs_seglock;
    511 			fs->lfs_lockpid = 0;
    512 			simple_unlock(&fs->lfs_interlock);
    513 			wakeup(&fs->lfs_seglock);
    514 		}
    515 		/*
    516 		 * We let checkpoints happen asynchronously.  That means
    517 		 * that during recovery, we have to roll forward between
    518 		 * the two segments described by the first and second
    519 		 * superblocks to make sure that the checkpoint described
    520 		 * by a superblock completed.
    521 		 */
    522 		simple_lock(&fs->lfs_interlock);
    523 		while (ckp && sync && fs->lfs_iocount)
    524 			(void)ltsleep(&fs->lfs_iocount, PRIBIO + 1,
    525 				      "lfs_iocount", 0, &fs->lfs_interlock);
    526 		while (sync && sp->seg_iocount) {
    527 			(void)ltsleep(&sp->seg_iocount, PRIBIO + 1,
    528 				     "seg_iocount", 0, &fs->lfs_interlock);
    529 			DLOG((DLOG_SEG, "sleeping on iocount %x == %d\n", sp, sp->seg_iocount));
    530 		}
    531 		simple_unlock(&fs->lfs_interlock);
    532 		if (sync)
    533 			pool_put(&fs->lfs_segpool, sp);
    534 
    535 		if (ckp) {
    536 			fs->lfs_nactive = 0;
    537 			/* If we *know* everything's on disk, write both sbs */
    538 			/* XXX should wait for this one	 */
    539 			if (sync)
    540 				lfs_writesuper(fs, fs->lfs_sboffs[fs->lfs_activesb]);
    541 			lfs_writesuper(fs, fs->lfs_sboffs[1 - fs->lfs_activesb]);
    542 			if (!(fs->lfs_ivnode->v_mount->mnt_iflag & IMNT_UNMOUNT)) {
    543 				lfs_auto_segclean(fs);
    544 				/* If sync, we can clean the remainder too */
    545 				if (sync)
    546 					lfs_auto_segclean(fs);
    547 			}
    548 			fs->lfs_activesb = 1 - fs->lfs_activesb;
    549 #ifdef DEBUG
    550 			LFS_ENTER_LOG("segunlock_ckp", __FILE__, __LINE__, 0, 0, curproc->p_pid);
    551 #endif
    552 			simple_lock(&fs->lfs_interlock);
    553 			--fs->lfs_seglock;
    554 			fs->lfs_lockpid = 0;
    555 			simple_unlock(&fs->lfs_interlock);
    556 			wakeup(&fs->lfs_seglock);
    557 		}
    558 		/* Reenable fragment size changes */
    559 		lockmgr(&fs->lfs_fraglock, LK_RELEASE, 0);
    560 		if (do_unmark_dirop)
    561 			lfs_unmark_dirop(fs);
    562 	} else if (fs->lfs_seglock == 0) {
    563 		simple_unlock(&fs->lfs_interlock);
    564 		panic ("Seglock not held");
    565 	} else {
    566 		--fs->lfs_seglock;
    567 		simple_unlock(&fs->lfs_interlock);
    568 	}
    569 }
    570 
    571 /*
    572  * drain dirops and start writer.
    573  */
    574 int
    575 lfs_writer_enter(struct lfs *fs, const char *wmesg)
    576 {
    577 	int error = 0;
    578 
    579 	ASSERT_MAYBE_SEGLOCK(fs);
    580 	simple_lock(&fs->lfs_interlock);
    581 
    582 	/* disallow dirops during flush */
    583 	fs->lfs_writer++;
    584 
    585 	while (fs->lfs_dirops > 0) {
    586 		++fs->lfs_diropwait;
    587 		error = ltsleep(&fs->lfs_writer, PRIBIO+1, wmesg, 0,
    588 				&fs->lfs_interlock);
    589 		--fs->lfs_diropwait;
    590 	}
    591 
    592 	if (error)
    593 		fs->lfs_writer--;
    594 
    595 	simple_unlock(&fs->lfs_interlock);
    596 
    597 	return error;
    598 }
    599 
    600 void
    601 lfs_writer_leave(struct lfs *fs)
    602 {
    603 	boolean_t dowakeup;
    604 
    605 	ASSERT_MAYBE_SEGLOCK(fs);
    606 	simple_lock(&fs->lfs_interlock);
    607 	dowakeup = !(--fs->lfs_writer);
    608 	simple_unlock(&fs->lfs_interlock);
    609 	if (dowakeup)
    610 		wakeup(&fs->lfs_dirops);
    611 }
    612 
    613 /*
    614  * Unlock, wait for the cleaner, then relock to where we were before.
    615  * To be used only at a fairly high level, to address a paucity of free
    616  * segments propagated back from lfs_gop_write().
    617  */
    618 void
    619 lfs_segunlock_relock(struct lfs *fs)
    620 {
    621 	int n = fs->lfs_seglock;
    622 	u_int16_t seg_flags;
    623 
    624 	if (n == 0)
    625 		return;
    626 
    627 	/* Write anything we've already gathered to disk */
    628 	lfs_writeseg(fs, fs->lfs_sp);
    629 
    630 	/* Save segment flags for later */
    631 	seg_flags = fs->lfs_sp->seg_flags;
    632 
    633 	fs->lfs_sp->seg_flags |= SEGM_PROT; /* Don't unmark dirop nodes */
    634 	while(fs->lfs_seglock)
    635 		lfs_segunlock(fs);
    636 
    637 	/* Wait for the cleaner */
    638 	wakeup(&lfs_allclean_wakeup);
    639 	wakeup(&fs->lfs_nextseg);
    640 	simple_lock(&fs->lfs_interlock);
    641 	while (LFS_STARVED_FOR_SEGS(fs))
    642 		ltsleep(&fs->lfs_avail, PRIBIO, "relock", 0,
    643 			&fs->lfs_interlock);
    644 	simple_unlock(&fs->lfs_interlock);
    645 
    646 	/* Put the segment lock back the way it was. */
    647 	while(n--)
    648 		lfs_seglock(fs, seg_flags);
    649 
    650 	return;
    651 }
    652