lfs_subr.c revision 1.97.10.1 1 /* $NetBSD: lfs_subr.c,v 1.97.10.1 2020/02/29 20:21:11 ad 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.97.10.1 2020/02/29 20:21:11 ad 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, *marker;
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 mutex_exit(&lfs_lock);
353
354 if (!doit)
355 return;
356
357 marker = pool_get(&lfs_inode_pool, PR_WAITOK);
358 KASSERT(fs != NULL);
359 memset(marker, 0, sizeof(*marker));
360 marker->inode_ext.lfs = pool_get(&lfs_inoext_pool, PR_WAITOK);
361 memset(marker->inode_ext.lfs, 0, sizeof(*marker->inode_ext.lfs));
362 marker->i_state |= IN_MARKER;
363
364 mutex_enter(&lfs_lock);
365 TAILQ_INSERT_HEAD(&fs->lfs_dchainhd, marker, i_lfs_dchain);
366 while ((ip = TAILQ_NEXT(marker, i_lfs_dchain)) != NULL) {
367 TAILQ_REMOVE(&fs->lfs_dchainhd, marker, i_lfs_dchain);
368 TAILQ_INSERT_AFTER(&fs->lfs_dchainhd, ip, marker,
369 i_lfs_dchain);
370 if (ip->i_state & IN_MARKER)
371 continue;
372 vp = ITOV(ip);
373 if ((ip->i_state & (IN_ADIROP | IN_CDIROP)) == IN_CDIROP) {
374 --lfs_dirvcount;
375 --fs->lfs_dirvcount;
376 vp->v_uflag &= ~VU_DIROP;
377 TAILQ_REMOVE(&fs->lfs_dchainhd, ip, i_lfs_dchain);
378 wakeup(&lfs_dirvcount);
379 fs->lfs_unlockvp = vp;
380 mutex_exit(&lfs_lock);
381 vrele(vp);
382 mutex_enter(&lfs_lock);
383 fs->lfs_unlockvp = NULL;
384 ip->i_state &= ~IN_CDIROP;
385 }
386 }
387 TAILQ_REMOVE(&fs->lfs_dchainhd, marker, i_lfs_dchain);
388 fs->lfs_flags &= ~LFS_UNDIROP;
389 wakeup(&fs->lfs_flags);
390 mutex_exit(&lfs_lock);
391
392 pool_put(&lfs_inoext_pool, marker->inode_ext.lfs);
393 pool_put(&lfs_inode_pool, marker);
394 }
395
396 static void
397 lfs_auto_segclean(struct lfs *fs)
398 {
399 int i, error, waited;
400
401 ASSERT_SEGLOCK(fs);
402 /*
403 * Now that we've swapped lfs_activesb, but while we still
404 * hold the segment lock, run through the segment list marking
405 * the empty ones clean.
406 * XXX - do we really need to do them all at once?
407 */
408 waited = 0;
409 for (i = 0; i < lfs_sb_getnseg(fs); i++) {
410 if ((fs->lfs_suflags[0][i] &
411 (SEGUSE_ACTIVE | SEGUSE_DIRTY | SEGUSE_EMPTY)) ==
412 (SEGUSE_DIRTY | SEGUSE_EMPTY) &&
413 (fs->lfs_suflags[1][i] &
414 (SEGUSE_ACTIVE | SEGUSE_DIRTY | SEGUSE_EMPTY)) ==
415 (SEGUSE_DIRTY | SEGUSE_EMPTY)) {
416
417 /* Make sure the sb is written before we clean */
418 mutex_enter(&lfs_lock);
419 while (waited == 0 && fs->lfs_sbactive)
420 mtsleep(&fs->lfs_sbactive, PRIBIO+1, "lfs asb",
421 0, &lfs_lock);
422 mutex_exit(&lfs_lock);
423 waited = 1;
424
425 if ((error = lfs_do_segclean(fs, i)) != 0) {
426 DLOG((DLOG_CLEAN, "lfs_auto_segclean: lfs_do_segclean returned %d for seg %d\n", error, i));
427 }
428 }
429 fs->lfs_suflags[1 - fs->lfs_activesb][i] =
430 fs->lfs_suflags[fs->lfs_activesb][i];
431 }
432 }
433
434 /*
435 * lfs_segunlock --
436 * Single thread the segment writer.
437 */
438 void
439 lfs_segunlock(struct lfs *fs)
440 {
441 struct segment *sp;
442 unsigned long sync, ckp;
443 struct buf *bp;
444 int do_unmark_dirop = 0;
445
446 sp = fs->lfs_sp;
447
448 mutex_enter(&lfs_lock);
449
450 if (!LFS_SEGLOCK_HELD(fs))
451 panic("lfs seglock not held");
452
453 if (fs->lfs_seglock == 1) {
454 if ((sp->seg_flags & (SEGM_PROT | SEGM_CLEAN)) == 0)
455 do_unmark_dirop = 1;
456 mutex_exit(&lfs_lock);
457 sync = sp->seg_flags & SEGM_SYNC;
458 ckp = sp->seg_flags & SEGM_CKP;
459
460 /* We should have a segment summary, and nothing else */
461 KASSERT(sp->cbpp == sp->bpp + 1);
462
463 /* Free allocated segment summary */
464 lfs_sb_suboffset(fs, lfs_btofsb(fs, lfs_sb_getsumsize(fs)));
465 bp = *sp->bpp;
466 lfs_freebuf(fs, bp);
467
468 pool_put(&fs->lfs_bpppool, sp->bpp);
469 sp->bpp = NULL;
470
471 /*
472 * If we're not sync, we're done with sp, get rid of it.
473 * Otherwise, we keep a local copy around but free
474 * fs->lfs_sp so another process can use it (we have to
475 * wait but they don't have to wait for us).
476 */
477 if (!sync)
478 pool_put(&fs->lfs_segpool, sp);
479 fs->lfs_sp = NULL;
480
481 /*
482 * If the I/O count is non-zero, sleep until it reaches zero.
483 * At the moment, the user's process hangs around so we can
484 * sleep.
485 */
486 mutex_enter(&lfs_lock);
487 if (--fs->lfs_iocount <= 1)
488 wakeup(&fs->lfs_iocount);
489 mutex_exit(&lfs_lock);
490
491 /*
492 * If we're not checkpointing, we don't have to block
493 * other processes to wait for a synchronous write
494 * to complete.
495 */
496 if (!ckp) {
497 LFS_ENTER_LOG("segunlock_std", __FILE__, __LINE__, 0, 0, curproc->p_pid);
498
499 mutex_enter(&lfs_lock);
500 --fs->lfs_seglock;
501 fs->lfs_lockpid = 0;
502 fs->lfs_locklwp = 0;
503 mutex_exit(&lfs_lock);
504 wakeup(&fs->lfs_seglock);
505 }
506 /*
507 * We let checkpoints happen asynchronously. That means
508 * that during recovery, we have to roll forward between
509 * the two segments described by the first and second
510 * superblocks to make sure that the checkpoint described
511 * by a superblock completed.
512 */
513 mutex_enter(&lfs_lock);
514 while (ckp && sync && fs->lfs_iocount) {
515 (void)mtsleep(&fs->lfs_iocount, PRIBIO + 1,
516 "lfs_iocount", 0, &lfs_lock);
517 DLOG((DLOG_SEG, "sleeping on iocount %x == %d\n", fs, fs->lfs_iocount));
518 }
519 while (sync && sp->seg_iocount) {
520 (void)mtsleep(&sp->seg_iocount, PRIBIO + 1,
521 "seg_iocount", 0, &lfs_lock);
522 DLOG((DLOG_SEG, "sleeping on iocount %x == %d\n", sp, sp->seg_iocount));
523 }
524 mutex_exit(&lfs_lock);
525 if (sync)
526 pool_put(&fs->lfs_segpool, sp);
527
528 if (ckp) {
529 fs->lfs_nactive = 0;
530 /* If we *know* everything's on disk, write both sbs */
531 /* XXX should wait for this one */
532 if (sync)
533 lfs_writesuper(fs, lfs_sb_getsboff(fs, fs->lfs_activesb));
534 lfs_writesuper(fs, lfs_sb_getsboff(fs, 1 - fs->lfs_activesb));
535 if (!(fs->lfs_ivnode->v_mount->mnt_iflag & IMNT_UNMOUNT)) {
536 lfs_auto_segclean(fs);
537 /* If sync, we can clean the remainder too */
538 if (sync)
539 lfs_auto_segclean(fs);
540 }
541 fs->lfs_activesb = 1 - fs->lfs_activesb;
542
543 LFS_ENTER_LOG("segunlock_ckp", __FILE__, __LINE__, 0, 0, curproc->p_pid);
544
545 mutex_enter(&lfs_lock);
546 --fs->lfs_seglock;
547 fs->lfs_lockpid = 0;
548 fs->lfs_locklwp = 0;
549 mutex_exit(&lfs_lock);
550 wakeup(&fs->lfs_seglock);
551 }
552 /* Reenable fragment size changes */
553 rw_exit(&fs->lfs_fraglock);
554 if (do_unmark_dirop)
555 lfs_unmark_dirop(fs);
556 } else {
557 --fs->lfs_seglock;
558 KASSERT(fs->lfs_seglock != 0);
559 mutex_exit(&lfs_lock);
560 }
561 }
562
563 /*
564 * Drain dirops and start writer.
565 *
566 * No simple_locks are held when we enter and none are held when we return.
567 */
568 void
569 lfs_writer_enter(struct lfs *fs, const char *wmesg)
570 {
571 int error __diagused;
572
573 ASSERT_NO_SEGLOCK(fs);
574 mutex_enter(&lfs_lock);
575
576 /* disallow dirops during flush */
577 fs->lfs_writer++;
578
579 while (fs->lfs_dirops > 0) {
580 ++fs->lfs_diropwait;
581 error = mtsleep(&fs->lfs_writer, PRIBIO+1, wmesg, 0,
582 &lfs_lock);
583 KASSERT(error == 0);
584 --fs->lfs_diropwait;
585 }
586
587 mutex_exit(&lfs_lock);
588 }
589
590 int
591 lfs_writer_tryenter(struct lfs *fs)
592 {
593 int writer_set;
594
595 ASSERT_MAYBE_SEGLOCK(fs);
596 mutex_enter(&lfs_lock);
597 writer_set = (fs->lfs_dirops == 0);
598 if (writer_set)
599 fs->lfs_writer++;
600 mutex_exit(&lfs_lock);
601
602 return writer_set;
603 }
604
605 void
606 lfs_writer_leave(struct lfs *fs)
607 {
608 bool dowakeup;
609
610 ASSERT_MAYBE_SEGLOCK(fs);
611 mutex_enter(&lfs_lock);
612 dowakeup = !(--fs->lfs_writer);
613 if (dowakeup)
614 cv_broadcast(&fs->lfs_diropscv);
615 mutex_exit(&lfs_lock);
616 }
617
618 /*
619 * Unlock, wait for the cleaner, then relock to where we were before.
620 * To be used only at a fairly high level, to address a paucity of free
621 * segments propagated back from lfs_gop_write().
622 */
623 void
624 lfs_segunlock_relock(struct lfs *fs)
625 {
626 int n = fs->lfs_seglock;
627 u_int16_t seg_flags;
628 CLEANERINFO *cip;
629 struct buf *bp;
630
631 if (n == 0)
632 return;
633
634 /* Write anything we've already gathered to disk */
635 lfs_writeseg(fs, fs->lfs_sp);
636
637 /* Tell cleaner */
638 LFS_CLEANERINFO(cip, fs, bp);
639 lfs_ci_setflags(fs, cip,
640 lfs_ci_getflags(fs, cip) | LFS_CLEANER_MUST_CLEAN);
641 LFS_SYNC_CLEANERINFO(cip, fs, bp, 1);
642
643 /* Save segment flags for later */
644 seg_flags = fs->lfs_sp->seg_flags;
645
646 fs->lfs_sp->seg_flags |= SEGM_PROT; /* Don't unmark dirop nodes */
647 while(fs->lfs_seglock)
648 lfs_segunlock(fs);
649
650 /* Wait for the cleaner */
651 lfs_wakeup_cleaner(fs);
652 mutex_enter(&lfs_lock);
653 while (LFS_STARVED_FOR_SEGS(fs))
654 mtsleep(&fs->lfs_availsleep, PRIBIO, "relock", 0,
655 &lfs_lock);
656 mutex_exit(&lfs_lock);
657
658 /* Put the segment lock back the way it was. */
659 while(n--)
660 lfs_seglock(fs, seg_flags);
661
662 /* Cleaner can relax now */
663 LFS_CLEANERINFO(cip, fs, bp);
664 lfs_ci_setflags(fs, cip,
665 lfs_ci_getflags(fs, cip) & ~LFS_CLEANER_MUST_CLEAN);
666 LFS_SYNC_CLEANERINFO(cip, fs, bp, 1);
667
668 return;
669 }
670
671 /*
672 * Wake up the cleaner, provided that nowrap is not set.
673 */
674 void
675 lfs_wakeup_cleaner(struct lfs *fs)
676 {
677 if (fs->lfs_nowrap > 0)
678 return;
679
680 cv_broadcast(&fs->lfs_nextsegsleep);
681 cv_broadcast(&lfs_allclean_wakeup);
682 }
683