Home | History | Annotate | Line # | Download | only in lfs
lfs_subr.c revision 1.50
      1 /*	$NetBSD: lfs_subr.c,v 1.50 2005/03/08 00:18:20 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.50 2005/03/08 00:18:20 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 /*
     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 char *lfs_res_names[LFS_NB_COUNT] = {
    125 	"summary",
    126 	"superblock",
    127 	"ifile block",
    128 	"cluster",
    129 	"clean",
    130 };
    131 #endif
    132 
    133 int lfs_res_qty[LFS_NB_COUNT] = {
    134 	LFS_N_SUMMARIES,
    135 	LFS_N_SBLOCKS,
    136 	LFS_N_IBLOCKS,
    137 	LFS_N_CLUSTERS,
    138 	LFS_N_CLEAN,
    139 };
    140 
    141 void
    142 lfs_setup_resblks(struct lfs *fs)
    143 {
    144 	int i, j;
    145 	int maxbpp;
    146 
    147 	fs->lfs_resblk = (res_t *)malloc(LFS_N_TOTAL * sizeof(res_t), M_SEGMENT,
    148 					  M_WAITOK);
    149 	for (i = 0; i < LFS_N_TOTAL; i++) {
    150 		fs->lfs_resblk[i].inuse = 0;
    151 		fs->lfs_resblk[i].p = NULL;
    152 	}
    153 	for (i = 0; i < LFS_RESHASH_WIDTH; i++)
    154 		LIST_INIT(fs->lfs_reshash + i);
    155 
    156 	/*
    157 	 * These types of allocations can be larger than a page,
    158 	 * so we can't use the pool subsystem for them.
    159 	 */
    160 	for (i = 0, j = 0; j < LFS_N_SUMMARIES; j++, i++)
    161 		fs->lfs_resblk[i].size = fs->lfs_sumsize;
    162 	for (j = 0; j < LFS_N_SBLOCKS; j++, i++)
    163 		fs->lfs_resblk[i].size = LFS_SBPAD;
    164 	for (j = 0; j < LFS_N_IBLOCKS; j++, i++)
    165 		fs->lfs_resblk[i].size = fs->lfs_bsize;
    166 	for (j = 0; j < LFS_N_CLUSTERS; j++, i++)
    167 		fs->lfs_resblk[i].size = MAXPHYS;
    168 	for (j = 0; j < LFS_N_CLEAN; j++, i++)
    169 		fs->lfs_resblk[i].size = MAXPHYS;
    170 
    171 	for (i = 0; i < LFS_N_TOTAL; i++) {
    172 		fs->lfs_resblk[i].p = malloc(fs->lfs_resblk[i].size,
    173 					     M_SEGMENT, M_WAITOK);
    174 	}
    175 
    176 	/*
    177 	 * Initialize pools for small types (XXX is BPP small?)
    178 	 */
    179 	pool_init(&fs->lfs_clpool, sizeof(struct lfs_cluster), 0, 0, 0,
    180 		"lfsclpl", &pool_allocator_nointr);
    181 	pool_init(&fs->lfs_segpool, sizeof(struct segment), 0, 0, 0,
    182 		"lfssegpool", &pool_allocator_nointr);
    183 	maxbpp = ((fs->lfs_sumsize - SEGSUM_SIZE(fs)) / sizeof(int32_t) + 2);
    184 	maxbpp = MIN(maxbpp, segsize(fs) / fs->lfs_fsize + 2);
    185 	pool_init(&fs->lfs_bpppool, maxbpp * sizeof(struct buf *), 0, 0, 0,
    186 		"lfsbpppl", &pool_allocator_nointr);
    187 }
    188 
    189 void
    190 lfs_free_resblks(struct lfs *fs)
    191 {
    192 	int i;
    193 
    194 	pool_destroy(&fs->lfs_bpppool);
    195 	pool_destroy(&fs->lfs_segpool);
    196 	pool_destroy(&fs->lfs_clpool);
    197 
    198 	for (i = 0; i < LFS_N_TOTAL; i++) {
    199 		while (fs->lfs_resblk[i].inuse)
    200 			tsleep(&fs->lfs_resblk, PRIBIO + 1, "lfs_free", 0);
    201 		if (fs->lfs_resblk[i].p != NULL)
    202 			free(fs->lfs_resblk[i].p, M_SEGMENT);
    203 	}
    204 	free(fs->lfs_resblk, M_SEGMENT);
    205 }
    206 
    207 static unsigned int
    208 lfs_mhash(void *vp)
    209 {
    210 	return (unsigned int)(((unsigned long)vp) >> 2) % LFS_RESHASH_WIDTH;
    211 }
    212 
    213 /*
    214  * Return memory of the given size for the given purpose, or use one of a
    215  * number of spare last-resort buffers, if malloc returns NULL.
    216  */
    217 void *
    218 lfs_malloc(struct lfs *fs, size_t size, int type)
    219 {
    220 	struct lfs_res_blk *re;
    221 	void *r;
    222 	int i, s, start;
    223 	unsigned int h;
    224 
    225 	r = NULL;
    226 
    227 	/* If no mem allocated for this type, it just waits */
    228 	if (lfs_res_qty[type] == 0) {
    229 		r = malloc(size, M_SEGMENT, M_WAITOK);
    230 		return r;
    231 	}
    232 
    233 	/* Otherwise try a quick malloc, and if it works, great */
    234 	if ((r = malloc(size, M_SEGMENT, M_NOWAIT)) != NULL) {
    235 		return r;
    236 	}
    237 
    238 	/*
    239 	 * If malloc returned NULL, we are forced to use one of our
    240 	 * reserve blocks.  We have on hand at least one summary block,
    241 	 * at least one cluster block, at least one superblock,
    242 	 * and several indirect blocks.
    243 	 */
    244 	/* skip over blocks of other types */
    245 	for (i = 0, start = 0; i < type; i++)
    246 		start += lfs_res_qty[i];
    247 	while (r == NULL) {
    248 		for (i = 0; i < lfs_res_qty[type]; i++) {
    249 			if (fs->lfs_resblk[start + i].inuse == 0) {
    250 				re = fs->lfs_resblk + start + i;
    251 				re->inuse = 1;
    252 				r = re->p;
    253 				KASSERT(re->size >= size);
    254 				h = lfs_mhash(r);
    255 				s = splbio();
    256 				LIST_INSERT_HEAD(&fs->lfs_reshash[h], re, res);
    257 				splx(s);
    258 				return r;
    259 			}
    260 		}
    261 		DLOG((DLOG_MALLOC, "sleeping on %s (%d)\n", lfs_res_names[type], lfs_res_qty[type]));
    262 		tsleep(&fs->lfs_resblk, PVM, "lfs_malloc", 0);
    263 		DLOG((DLOG_MALLOC, "done sleeping on %s\n", lfs_res_names[type]));
    264 	}
    265 	/* NOTREACHED */
    266 	return r;
    267 }
    268 
    269 void
    270 lfs_free(struct lfs *fs, void *p, int type)
    271 {
    272 	int s;
    273 	unsigned int h;
    274 	res_t *re;
    275 #ifdef DEBUG
    276 	int i;
    277 #endif
    278 
    279 	h = lfs_mhash(p);
    280 	s = splbio();
    281 	LIST_FOREACH(re, &fs->lfs_reshash[h], res) {
    282 		if (re->p == p) {
    283 			KASSERT(re->inuse == 1);
    284 			LIST_REMOVE(re, res);
    285 			re->inuse = 0;
    286 			wakeup(&fs->lfs_resblk);
    287 			splx(s);
    288 			return;
    289 		}
    290 	}
    291 #ifdef DEBUG
    292 	for (i = 0; i < LFS_N_TOTAL; i++) {
    293 		if (fs->lfs_resblk[i].p == p)
    294 			panic("lfs_free: inconsistent reserved block");
    295 	}
    296 #endif
    297 	splx(s);
    298 
    299 	/*
    300 	 * If we didn't find it, free it.
    301 	 */
    302 	free(p, M_SEGMENT);
    303 }
    304 
    305 /*
    306  * lfs_seglock --
    307  *	Single thread the segment writer.
    308  */
    309 int
    310 lfs_seglock(struct lfs *fs, unsigned long flags)
    311 {
    312 	struct segment *sp;
    313 
    314 	simple_lock(&fs->lfs_interlock);
    315 	if (fs->lfs_seglock) {
    316 		if (fs->lfs_lockpid == curproc->p_pid) {
    317 			simple_unlock(&fs->lfs_interlock);
    318 			++fs->lfs_seglock;
    319 			fs->lfs_sp->seg_flags |= flags;
    320 			return 0;
    321 		} else if (flags & SEGM_PAGEDAEMON) {
    322 			simple_unlock(&fs->lfs_interlock);
    323 			return EWOULDBLOCK;
    324 		} else while (fs->lfs_seglock)
    325 			(void)ltsleep(&fs->lfs_seglock, PRIBIO + 1,
    326 				      "lfs seglock", 0, &fs->lfs_interlock);
    327 	}
    328 
    329 	fs->lfs_seglock = 1;
    330 	fs->lfs_lockpid = curproc->p_pid;
    331 	simple_unlock(&fs->lfs_interlock);
    332 	fs->lfs_cleanind = 0;
    333 
    334 	/* Drain fragment size changes out */
    335 	lockmgr(&fs->lfs_fraglock, LK_EXCLUSIVE, 0);
    336 
    337 	sp = fs->lfs_sp = pool_get(&fs->lfs_segpool, PR_WAITOK);
    338 	sp->bpp = pool_get(&fs->lfs_bpppool, PR_WAITOK);
    339 	sp->seg_flags = flags;
    340 	sp->vp = NULL;
    341 	sp->seg_iocount = 0;
    342 	(void) lfs_initseg(fs);
    343 
    344 	/*
    345 	 * Keep a cumulative count of the outstanding I/O operations.  If the
    346 	 * disk drive catches up with us it could go to zero before we finish,
    347 	 * so we artificially increment it by one until we've scheduled all of
    348 	 * the writes we intend to do.
    349 	 */
    350 	++fs->lfs_iocount;
    351 	return 0;
    352 }
    353 
    354 static void lfs_unmark_dirop(struct lfs *);
    355 
    356 static void
    357 lfs_unmark_dirop(struct lfs *fs)
    358 {
    359 	struct inode *ip, *nip;
    360 	struct vnode *vp;
    361 	int doit;
    362 
    363 	simple_lock(&fs->lfs_interlock);
    364 	doit = !(fs->lfs_flags & LFS_UNDIROP);
    365 	if (doit)
    366 		fs->lfs_flags |= LFS_UNDIROP;
    367 	simple_unlock(&fs->lfs_interlock);
    368 	if (!doit)
    369 		return;
    370 
    371 	for (ip = TAILQ_FIRST(&fs->lfs_dchainhd); ip != NULL; ip = nip) {
    372 		nip = TAILQ_NEXT(ip, i_lfs_dchain);
    373 		vp = ITOV(ip);
    374 
    375 		if (VOP_ISLOCKED(vp) &&
    376 			   vp->v_lock.lk_lockholder != curproc->p_pid) {
    377 			continue;
    378 		}
    379 		if ((VTOI(vp)->i_flag & IN_ADIROP) == 0) {
    380 			--lfs_dirvcount;
    381 			vp->v_flag &= ~VDIROP;
    382 			TAILQ_REMOVE(&fs->lfs_dchainhd, ip, i_lfs_dchain);
    383 			wakeup(&lfs_dirvcount);
    384 			fs->lfs_unlockvp = vp;
    385 			vrele(vp);
    386 			fs->lfs_unlockvp = NULL;
    387 		}
    388 	}
    389 
    390 	simple_lock(&fs->lfs_interlock);
    391 	fs->lfs_flags &= ~LFS_UNDIROP;
    392 	simple_unlock(&fs->lfs_interlock);
    393 }
    394 
    395 static void
    396 lfs_auto_segclean(struct lfs *fs)
    397 {
    398 	int i, error, s, waited;
    399 
    400 	/*
    401 	 * Now that we've swapped lfs_activesb, but while we still
    402 	 * hold the segment lock, run through the segment list marking
    403 	 * the empty ones clean.
    404 	 * XXX - do we really need to do them all at once?
    405 	 */
    406 	waited = 0;
    407 	for (i = 0; i < fs->lfs_nseg; i++) {
    408 		if ((fs->lfs_suflags[0][i] &
    409 		     (SEGUSE_ACTIVE | SEGUSE_DIRTY | SEGUSE_EMPTY)) ==
    410 		    (SEGUSE_DIRTY | SEGUSE_EMPTY) &&
    411 		    (fs->lfs_suflags[1][i] &
    412 		     (SEGUSE_ACTIVE | SEGUSE_DIRTY | SEGUSE_EMPTY)) ==
    413 		    (SEGUSE_DIRTY | SEGUSE_EMPTY)) {
    414 
    415 			/* Make sure the sb is written before we clean */
    416 			s = splbio();
    417 			while (waited == 0 && fs->lfs_sbactive)
    418 				tsleep(&fs->lfs_sbactive, PRIBIO+1, "lfs asb",
    419 					0);
    420 			splx(s);
    421 			waited = 1;
    422 
    423 			if ((error = lfs_do_segclean(fs, i)) != 0) {
    424 				DLOG((DLOG_CLEAN, "lfs_auto_segclean: lfs_do_segclean returned %d for seg %d\n", error, i));
    425 			}
    426 		}
    427 		fs->lfs_suflags[1 - fs->lfs_activesb][i] =
    428 			fs->lfs_suflags[fs->lfs_activesb][i];
    429 	}
    430 }
    431 
    432 /*
    433  * lfs_segunlock --
    434  *	Single thread the segment writer.
    435  */
    436 void
    437 lfs_segunlock(struct lfs *fs)
    438 {
    439 	struct segment *sp;
    440 	unsigned long sync, ckp;
    441 	struct buf *bp;
    442 	int do_unmark_dirop = 0;
    443 
    444 	sp = fs->lfs_sp;
    445 
    446 	simple_lock(&fs->lfs_interlock);
    447 	if (fs->lfs_seglock == 1) {
    448 		if ((sp->seg_flags & SEGM_PROT) == 0)
    449 			do_unmark_dirop = 1;
    450 		simple_unlock(&fs->lfs_interlock);
    451 		sync = sp->seg_flags & SEGM_SYNC;
    452 		ckp = sp->seg_flags & SEGM_CKP;
    453 		if (sp->bpp != sp->cbpp) {
    454 			/* Free allocated segment summary */
    455 			fs->lfs_offset -= btofsb(fs, fs->lfs_sumsize);
    456 			bp = *sp->bpp;
    457 			lfs_freebuf(fs, bp);
    458 		} else
    459 			DLOG((DLOG_SEG, "lfs_segunlock: unlock to 0 with no summary"));
    460 
    461 		pool_put(&fs->lfs_bpppool, sp->bpp);
    462 		sp->bpp = NULL;
    463 
    464 		/*
    465 		 * If we're not sync, we're done with sp, get rid of it.
    466 		 * Otherwise, we keep a local copy around but free
    467 		 * fs->lfs_sp so another process can use it (we have to
    468 		 * wait but they don't have to wait for us).
    469 		 */
    470 		if (!sync)
    471 			pool_put(&fs->lfs_segpool, sp);
    472 		fs->lfs_sp = NULL;
    473 
    474 		/*
    475 		 * If the I/O count is non-zero, sleep until it reaches zero.
    476 		 * At the moment, the user's process hangs around so we can
    477 		 * sleep.
    478 		 */
    479 		if (--fs->lfs_iocount == 0)
    480 			LFS_DEBUG_COUNTLOCKED("lfs_segunlock");
    481 		if (fs->lfs_iocount <= 1)
    482 			wakeup(&fs->lfs_iocount);
    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 			simple_lock(&fs->lfs_interlock);
    490 			--fs->lfs_seglock;
    491 			fs->lfs_lockpid = 0;
    492 			simple_unlock(&fs->lfs_interlock);
    493 			wakeup(&fs->lfs_seglock);
    494 		}
    495 		/*
    496 		 * We let checkpoints happen asynchronously.  That means
    497 		 * that during recovery, we have to roll forward between
    498 		 * the two segments described by the first and second
    499 		 * superblocks to make sure that the checkpoint described
    500 		 * by a superblock completed.
    501 		 */
    502 		while (ckp && sync && fs->lfs_iocount)
    503 			(void)tsleep(&fs->lfs_iocount, PRIBIO + 1,
    504 				     "lfs_iocount", 0);
    505 		while (sync && sp->seg_iocount) {
    506 			(void)tsleep(&sp->seg_iocount, PRIBIO + 1,
    507 				     "seg_iocount", 0);
    508 			DLOG((DLOG_SEG, "sleeping on iocount %x == %d\n", sp, sp->seg_iocount));
    509 		}
    510 		if (sync)
    511 			pool_put(&fs->lfs_segpool, sp);
    512 
    513 		if (ckp) {
    514 			fs->lfs_nactive = 0;
    515 			/* If we *know* everything's on disk, write both sbs */
    516 			/* XXX should wait for this one	 */
    517 			if (sync)
    518 				lfs_writesuper(fs, fs->lfs_sboffs[fs->lfs_activesb]);
    519 			lfs_writesuper(fs, fs->lfs_sboffs[1 - fs->lfs_activesb]);
    520 			if (!(fs->lfs_ivnode->v_mount->mnt_iflag & IMNT_UNMOUNT)) {
    521 				lfs_auto_segclean(fs);
    522 				/* If sync, we can clean the remainder too */
    523 				if (sync)
    524 					lfs_auto_segclean(fs);
    525 			}
    526 			fs->lfs_activesb = 1 - fs->lfs_activesb;
    527 			simple_lock(&fs->lfs_interlock);
    528 			--fs->lfs_seglock;
    529 			fs->lfs_lockpid = 0;
    530 			simple_unlock(&fs->lfs_interlock);
    531 			wakeup(&fs->lfs_seglock);
    532 		}
    533 		/* Reenable fragment size changes */
    534 		lockmgr(&fs->lfs_fraglock, LK_RELEASE, 0);
    535 		if (do_unmark_dirop)
    536 			lfs_unmark_dirop(fs);
    537 	} else if (fs->lfs_seglock == 0) {
    538 		simple_unlock(&fs->lfs_interlock);
    539 		panic ("Seglock not held");
    540 	} else {
    541 		--fs->lfs_seglock;
    542 		simple_unlock(&fs->lfs_interlock);
    543 	}
    544 }
    545 
    546 /*
    547  * drain dirops and start writer.
    548  */
    549 int
    550 lfs_writer_enter(struct lfs *fs, const char *wmesg)
    551 {
    552 	int error = 0;
    553 
    554 	simple_lock(&fs->lfs_interlock);
    555 
    556 	/* disallow dirops during flush */
    557 	fs->lfs_writer++;
    558 
    559 	while (fs->lfs_dirops > 0) {
    560 		++fs->lfs_diropwait;
    561 		error = ltsleep(&fs->lfs_writer, PRIBIO+1, wmesg, 0,
    562 		    &fs->lfs_interlock);
    563 		--fs->lfs_diropwait;
    564 	}
    565 
    566 	if (error)
    567 		fs->lfs_writer--;
    568 
    569 	simple_unlock(&fs->lfs_interlock);
    570 
    571 	return error;
    572 }
    573 
    574 void
    575 lfs_writer_leave(struct lfs *fs)
    576 {
    577 	boolean_t dowakeup;
    578 
    579 	simple_lock(&fs->lfs_interlock);
    580 	dowakeup = !(--fs->lfs_writer);
    581 	simple_unlock(&fs->lfs_interlock);
    582 	if (dowakeup)
    583 		wakeup(&fs->lfs_dirops);
    584 }
    585