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