lfs_subr.c revision 1.94
1/*	$NetBSD: lfs_subr.c,v 1.94 2017/06/10 05:29:36 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.94 2017/06/10 05:29:36 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
84const char *lfs_res_names[LFS_NB_COUNT] = {
85	"summary",
86	"superblock",
87	"file block",
88	"cluster",
89	"clean",
90	"blkiov",
91};
92#endif
93
94int 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
103void
104lfs_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
155void
156lfs_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
176static unsigned int
177lfs_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 */
186void *
187lfs_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
244void
245lfs_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	for (int i = 0; i < LFS_N_TOTAL; i++) {
265		KDASSERTMSG(fs->lfs_resblk[i].p == p,
266		    "lfs_free: inconsistent reserved block");
267	}
268
269	mutex_exit(&lfs_lock);
270
271	/*
272	 * If we didn't find it, free it.
273	 */
274	free(p, M_SEGMENT);
275}
276
277/*
278 * lfs_seglock --
279 *	Single thread the segment writer.
280 */
281int
282lfs_seglock(struct lfs *fs, unsigned long flags)
283{
284	struct segment *sp;
285
286	mutex_enter(&lfs_lock);
287	if (fs->lfs_seglock) {
288		if (fs->lfs_lockpid == curproc->p_pid &&
289		    fs->lfs_locklwp == curlwp->l_lid) {
290			++fs->lfs_seglock;
291			fs->lfs_sp->seg_flags |= flags;
292			mutex_exit(&lfs_lock);
293			return 0;
294		} else if (flags & SEGM_PAGEDAEMON) {
295			mutex_exit(&lfs_lock);
296			return EWOULDBLOCK;
297		} else {
298			while (fs->lfs_seglock) {
299				(void)mtsleep(&fs->lfs_seglock, PRIBIO + 1,
300					"lfs_seglock", 0, &lfs_lock);
301			}
302		}
303	}
304
305	fs->lfs_seglock = 1;
306	fs->lfs_lockpid = curproc->p_pid;
307	fs->lfs_locklwp = curlwp->l_lid;
308	mutex_exit(&lfs_lock);
309	fs->lfs_cleanind = 0;
310
311	LFS_ENTER_LOG("seglock", __FILE__, __LINE__, 0, flags, curproc->p_pid);
312
313	/* Drain fragment size changes out */
314	rw_enter(&fs->lfs_fraglock, RW_WRITER);
315
316	sp = fs->lfs_sp = pool_get(&fs->lfs_segpool, PR_WAITOK);
317	sp->bpp = pool_get(&fs->lfs_bpppool, PR_WAITOK);
318	sp->seg_flags = flags;
319	sp->vp = NULL;
320	sp->seg_iocount = 0;
321	(void) lfs_initseg(fs);
322
323	/*
324	 * Keep a cumulative count of the outstanding I/O operations.  If the
325	 * disk drive catches up with us it could go to zero before we finish,
326	 * so we artificially increment it by one until we've scheduled all of
327	 * the writes we intend to do.
328	 */
329	mutex_enter(&lfs_lock);
330	++fs->lfs_iocount;
331	fs->lfs_startseg = lfs_sb_getcurseg(fs);
332	mutex_exit(&lfs_lock);
333	return 0;
334}
335
336static void lfs_unmark_dirop(struct lfs *);
337
338static void
339lfs_unmark_dirop(struct lfs *fs)
340{
341	struct inode *ip, *nip;
342	struct vnode *vp;
343	int doit;
344
345	ASSERT_NO_SEGLOCK(fs);
346	mutex_enter(&lfs_lock);
347	doit = !(fs->lfs_flags & LFS_UNDIROP);
348	if (doit)
349		fs->lfs_flags |= LFS_UNDIROP;
350	if (!doit) {
351		mutex_exit(&lfs_lock);
352		return;
353	}
354
355	for (ip = TAILQ_FIRST(&fs->lfs_dchainhd); ip != NULL; ip = nip) {
356		nip = TAILQ_NEXT(ip, i_lfs_dchain);
357		vp = ITOV(ip);
358		if ((ip->i_state & (IN_ADIROP | IN_CDIROP)) == IN_CDIROP) {
359			--lfs_dirvcount;
360			--fs->lfs_dirvcount;
361			vp->v_uflag &= ~VU_DIROP;
362			TAILQ_REMOVE(&fs->lfs_dchainhd, ip, i_lfs_dchain);
363			wakeup(&lfs_dirvcount);
364			fs->lfs_unlockvp = vp;
365			mutex_exit(&lfs_lock);
366			vrele(vp);
367			mutex_enter(&lfs_lock);
368			fs->lfs_unlockvp = NULL;
369			ip->i_state &= ~IN_CDIROP;
370		}
371	}
372
373	fs->lfs_flags &= ~LFS_UNDIROP;
374	wakeup(&fs->lfs_flags);
375	mutex_exit(&lfs_lock);
376}
377
378static void
379lfs_auto_segclean(struct lfs *fs)
380{
381	int i, error, waited;
382
383	ASSERT_SEGLOCK(fs);
384	/*
385	 * Now that we've swapped lfs_activesb, but while we still
386	 * hold the segment lock, run through the segment list marking
387	 * the empty ones clean.
388	 * XXX - do we really need to do them all at once?
389	 */
390	waited = 0;
391	for (i = 0; i < lfs_sb_getnseg(fs); i++) {
392		if ((fs->lfs_suflags[0][i] &
393		     (SEGUSE_ACTIVE | SEGUSE_DIRTY | SEGUSE_EMPTY)) ==
394		    (SEGUSE_DIRTY | SEGUSE_EMPTY) &&
395		    (fs->lfs_suflags[1][i] &
396		     (SEGUSE_ACTIVE | SEGUSE_DIRTY | SEGUSE_EMPTY)) ==
397		    (SEGUSE_DIRTY | SEGUSE_EMPTY)) {
398
399			/* Make sure the sb is written before we clean */
400			mutex_enter(&lfs_lock);
401			while (waited == 0 && fs->lfs_sbactive)
402				mtsleep(&fs->lfs_sbactive, PRIBIO+1, "lfs asb",
403					0, &lfs_lock);
404			mutex_exit(&lfs_lock);
405			waited = 1;
406
407			if ((error = lfs_do_segclean(fs, i)) != 0) {
408				DLOG((DLOG_CLEAN, "lfs_auto_segclean: lfs_do_segclean returned %d for seg %d\n", error, i));
409			}
410		}
411		fs->lfs_suflags[1 - fs->lfs_activesb][i] =
412			fs->lfs_suflags[fs->lfs_activesb][i];
413	}
414}
415
416/*
417 * lfs_segunlock --
418 *	Single thread the segment writer.
419 */
420void
421lfs_segunlock(struct lfs *fs)
422{
423	struct segment *sp;
424	unsigned long sync, ckp;
425	struct buf *bp;
426	int do_unmark_dirop = 0;
427
428	sp = fs->lfs_sp;
429
430	mutex_enter(&lfs_lock);
431	KASSERT(LFS_SEGLOCK_HELD(fs));
432	if (fs->lfs_seglock == 1) {
433		if ((sp->seg_flags & (SEGM_PROT | SEGM_CLEAN)) == 0)
434			do_unmark_dirop = 1;
435		mutex_exit(&lfs_lock);
436		sync = sp->seg_flags & SEGM_SYNC;
437		ckp = sp->seg_flags & SEGM_CKP;
438
439		/* We should have a segment summary, and nothing else */
440		KASSERT(sp->cbpp == sp->bpp + 1);
441
442		/* Free allocated segment summary */
443		lfs_sb_suboffset(fs, lfs_btofsb(fs, lfs_sb_getsumsize(fs)));
444		bp = *sp->bpp;
445		lfs_freebuf(fs, bp);
446
447		pool_put(&fs->lfs_bpppool, sp->bpp);
448		sp->bpp = NULL;
449
450		/*
451		 * If we're not sync, we're done with sp, get rid of it.
452		 * Otherwise, we keep a local copy around but free
453		 * fs->lfs_sp so another process can use it (we have to
454		 * wait but they don't have to wait for us).
455		 */
456		if (!sync)
457			pool_put(&fs->lfs_segpool, sp);
458		fs->lfs_sp = NULL;
459
460		/*
461		 * If the I/O count is non-zero, sleep until it reaches zero.
462		 * At the moment, the user's process hangs around so we can
463		 * sleep.
464		 */
465		mutex_enter(&lfs_lock);
466		if (--fs->lfs_iocount <= 1)
467			wakeup(&fs->lfs_iocount);
468		mutex_exit(&lfs_lock);
469
470		/*
471		 * If we're not checkpointing, we don't have to block
472		 * other processes to wait for a synchronous write
473		 * to complete.
474		 */
475		if (!ckp) {
476			LFS_ENTER_LOG("segunlock_std", __FILE__, __LINE__, 0, 0, curproc->p_pid);
477
478			mutex_enter(&lfs_lock);
479			--fs->lfs_seglock;
480			fs->lfs_lockpid = 0;
481			fs->lfs_locklwp = 0;
482			mutex_exit(&lfs_lock);
483			wakeup(&fs->lfs_seglock);
484		}
485		/*
486		 * We let checkpoints happen asynchronously.  That means
487		 * that during recovery, we have to roll forward between
488		 * the two segments described by the first and second
489		 * superblocks to make sure that the checkpoint described
490		 * by a superblock completed.
491		 */
492		mutex_enter(&lfs_lock);
493		while (ckp && sync && fs->lfs_iocount) {
494			(void)mtsleep(&fs->lfs_iocount, PRIBIO + 1,
495				      "lfs_iocount", 0, &lfs_lock);
496			DLOG((DLOG_SEG, "sleeping on iocount %x == %d\n", fs, fs->lfs_iocount));
497		}
498		while (sync && sp->seg_iocount) {
499			(void)mtsleep(&sp->seg_iocount, PRIBIO + 1,
500				     "seg_iocount", 0, &lfs_lock);
501			DLOG((DLOG_SEG, "sleeping on iocount %x == %d\n", sp, sp->seg_iocount));
502		}
503		mutex_exit(&lfs_lock);
504		if (sync)
505			pool_put(&fs->lfs_segpool, sp);
506
507		if (ckp) {
508			fs->lfs_nactive = 0;
509			/* If we *know* everything's on disk, write both sbs */
510			/* XXX should wait for this one	 */
511			if (sync)
512				lfs_writesuper(fs, lfs_sb_getsboff(fs, fs->lfs_activesb));
513			lfs_writesuper(fs, lfs_sb_getsboff(fs, 1 - fs->lfs_activesb));
514			if (!(fs->lfs_ivnode->v_mount->mnt_iflag & IMNT_UNMOUNT)) {
515				lfs_auto_segclean(fs);
516				/* If sync, we can clean the remainder too */
517				if (sync)
518					lfs_auto_segclean(fs);
519			}
520			fs->lfs_activesb = 1 - fs->lfs_activesb;
521
522			LFS_ENTER_LOG("segunlock_ckp", __FILE__, __LINE__, 0, 0, curproc->p_pid);
523
524			mutex_enter(&lfs_lock);
525			--fs->lfs_seglock;
526			fs->lfs_lockpid = 0;
527			fs->lfs_locklwp = 0;
528			mutex_exit(&lfs_lock);
529			wakeup(&fs->lfs_seglock);
530		}
531		/* Reenable fragment size changes */
532		rw_exit(&fs->lfs_fraglock);
533		if (do_unmark_dirop)
534			lfs_unmark_dirop(fs);
535	} else if (fs->lfs_seglock == 0) {
536		mutex_exit(&lfs_lock);
537		panic ("Seglock not held");
538	} else {
539		--fs->lfs_seglock;
540		mutex_exit(&lfs_lock);
541	}
542}
543
544/*
545 * Drain dirops and start writer.
546 *
547 * No simple_locks are held when we enter and none are held when we return.
548 */
549int
550lfs_writer_enter(struct lfs *fs, const char *wmesg)
551{
552	int error = 0;
553
554	ASSERT_MAYBE_SEGLOCK(fs);
555	mutex_enter(&lfs_lock);
556
557	/* disallow dirops during flush */
558	fs->lfs_writer++;
559
560	while (fs->lfs_dirops > 0) {
561		++fs->lfs_diropwait;
562		error = mtsleep(&fs->lfs_writer, PRIBIO+1, wmesg, 0,
563				&lfs_lock);
564		--fs->lfs_diropwait;
565	}
566
567	if (error)
568		fs->lfs_writer--;
569
570	mutex_exit(&lfs_lock);
571
572	return error;
573}
574
575void
576lfs_writer_leave(struct lfs *fs)
577{
578	bool dowakeup;
579
580	ASSERT_MAYBE_SEGLOCK(fs);
581	mutex_enter(&lfs_lock);
582	dowakeup = !(--fs->lfs_writer);
583	if (dowakeup)
584		cv_broadcast(&fs->lfs_diropscv);
585	mutex_exit(&lfs_lock);
586}
587
588/*
589 * Unlock, wait for the cleaner, then relock to where we were before.
590 * To be used only at a fairly high level, to address a paucity of free
591 * segments propagated back from lfs_gop_write().
592 */
593void
594lfs_segunlock_relock(struct lfs *fs)
595{
596	int n = fs->lfs_seglock;
597	u_int16_t seg_flags;
598	CLEANERINFO *cip;
599	struct buf *bp;
600
601	if (n == 0)
602		return;
603
604	/* Write anything we've already gathered to disk */
605	lfs_writeseg(fs, fs->lfs_sp);
606
607	/* Tell cleaner */
608	LFS_CLEANERINFO(cip, fs, bp);
609	lfs_ci_setflags(fs, cip,
610			lfs_ci_getflags(fs, cip) | LFS_CLEANER_MUST_CLEAN);
611	LFS_SYNC_CLEANERINFO(cip, fs, bp, 1);
612
613	/* Save segment flags for later */
614	seg_flags = fs->lfs_sp->seg_flags;
615
616	fs->lfs_sp->seg_flags |= SEGM_PROT; /* Don't unmark dirop nodes */
617	while(fs->lfs_seglock)
618		lfs_segunlock(fs);
619
620	/* Wait for the cleaner */
621	lfs_wakeup_cleaner(fs);
622	mutex_enter(&lfs_lock);
623	while (LFS_STARVED_FOR_SEGS(fs))
624		mtsleep(&fs->lfs_availsleep, PRIBIO, "relock", 0,
625			&lfs_lock);
626	mutex_exit(&lfs_lock);
627
628	/* Put the segment lock back the way it was. */
629	while(n--)
630		lfs_seglock(fs, seg_flags);
631
632	/* Cleaner can relax now */
633	LFS_CLEANERINFO(cip, fs, bp);
634	lfs_ci_setflags(fs, cip,
635			lfs_ci_getflags(fs, cip) & ~LFS_CLEANER_MUST_CLEAN);
636	LFS_SYNC_CLEANERINFO(cip, fs, bp, 1);
637
638	return;
639}
640
641/*
642 * Wake up the cleaner, provided that nowrap is not set.
643 */
644void
645lfs_wakeup_cleaner(struct lfs *fs)
646{
647	if (fs->lfs_nowrap > 0)
648		return;
649
650	wakeup(&fs->lfs_nextsegsleep);
651	wakeup(&lfs_allclean_wakeup);
652}
653