lfs_bio.c revision 1.93 1 /* $NetBSD: lfs_bio.c,v 1.93 2006/05/14 21:32:45 elad 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_bio.c 8.10 (Berkeley) 6/10/95
67 */
68
69 #include <sys/cdefs.h>
70 __KERNEL_RCSID(0, "$NetBSD: lfs_bio.c,v 1.93 2006/05/14 21:32:45 elad Exp $");
71
72 #include <sys/param.h>
73 #include <sys/systm.h>
74 #include <sys/proc.h>
75 #include <sys/buf.h>
76 #include <sys/vnode.h>
77 #include <sys/resourcevar.h>
78 #include <sys/mount.h>
79 #include <sys/kernel.h>
80 #include <sys/kauth.h>
81
82 #include <ufs/ufs/inode.h>
83 #include <ufs/ufs/ufsmount.h>
84 #include <ufs/ufs/ufs_extern.h>
85
86 #include <ufs/lfs/lfs.h>
87 #include <ufs/lfs/lfs_extern.h>
88
89 #include <uvm/uvm.h>
90
91 /*
92 * LFS block write function.
93 *
94 * XXX
95 * No write cost accounting is done.
96 * This is almost certainly wrong for synchronous operations and NFS.
97 *
98 * protected by lfs_subsys_lock.
99 */
100 int locked_queue_count = 0; /* Count of locked-down buffers. */
101 long locked_queue_bytes = 0L; /* Total size of locked buffers. */
102 int lfs_subsys_pages = 0L; /* Total number LFS-written pages */
103 int lfs_fs_pagetrip = 0; /* # of pages to trip per-fs write */
104 int lfs_writing = 0; /* Set if already kicked off a writer
105 because of buffer space */
106 /* Lock for aboves */
107 struct simplelock lfs_subsys_lock = SIMPLELOCK_INITIALIZER;
108
109 extern int lfs_dostats;
110
111 /*
112 * reserved number/bytes of locked buffers
113 */
114 int locked_queue_rcount = 0;
115 long locked_queue_rbytes = 0L;
116
117 int lfs_fits_buf(struct lfs *, int, int);
118 int lfs_reservebuf(struct lfs *, struct vnode *vp, struct vnode *vp2,
119 int, int);
120 int lfs_reserveavail(struct lfs *, struct vnode *vp, struct vnode *vp2, int);
121
122 int
123 lfs_fits_buf(struct lfs *fs, int n, int bytes)
124 {
125 int count_fit, bytes_fit;
126
127 ASSERT_NO_SEGLOCK(fs);
128 LOCK_ASSERT(simple_lock_held(&lfs_subsys_lock));
129
130 count_fit =
131 (locked_queue_count + locked_queue_rcount + n < LFS_WAIT_BUFS);
132 bytes_fit =
133 (locked_queue_bytes + locked_queue_rbytes + bytes < LFS_WAIT_BYTES);
134
135 #ifdef DEBUG
136 if (!count_fit) {
137 DLOG((DLOG_AVAIL, "lfs_fits_buf: no fit count: %d + %d + %d >= %d\n",
138 locked_queue_count, locked_queue_rcount,
139 n, LFS_WAIT_BUFS));
140 }
141 if (!bytes_fit) {
142 DLOG((DLOG_AVAIL, "lfs_fits_buf: no fit bytes: %ld + %ld + %d >= %ld\n",
143 locked_queue_bytes, locked_queue_rbytes,
144 bytes, LFS_WAIT_BYTES));
145 }
146 #endif /* DEBUG */
147
148 return (count_fit && bytes_fit);
149 }
150
151 /* ARGSUSED */
152 int
153 lfs_reservebuf(struct lfs *fs, struct vnode *vp, struct vnode *vp2,
154 int n, int bytes)
155 {
156 ASSERT_MAYBE_SEGLOCK(fs);
157 KASSERT(locked_queue_rcount >= 0);
158 KASSERT(locked_queue_rbytes >= 0);
159
160 simple_lock(&lfs_subsys_lock);
161 while (n > 0 && !lfs_fits_buf(fs, n, bytes)) {
162 int error;
163
164 lfs_flush(fs, 0, 0);
165
166 error = ltsleep(&locked_queue_count, PCATCH | PUSER,
167 "lfsresbuf", hz * LFS_BUFWAIT, &lfs_subsys_lock);
168 if (error && error != EWOULDBLOCK) {
169 simple_unlock(&lfs_subsys_lock);
170 return error;
171 }
172 }
173
174 locked_queue_rcount += n;
175 locked_queue_rbytes += bytes;
176
177 simple_unlock(&lfs_subsys_lock);
178
179 KASSERT(locked_queue_rcount >= 0);
180 KASSERT(locked_queue_rbytes >= 0);
181
182 return 0;
183 }
184
185 /*
186 * Try to reserve some blocks, prior to performing a sensitive operation that
187 * requires the vnode lock to be honored. If there is not enough space, give
188 * up the vnode lock temporarily and wait for the space to become available.
189 *
190 * Called with vp locked. (Note nowever that if fsb < 0, vp is ignored.)
191 *
192 * XXX YAMT - it isn't safe to unlock vp here
193 * because the node might be modified while we sleep.
194 * (eg. cached states like i_offset might be stale,
195 * the vnode might be truncated, etc..)
196 * maybe we should have a way to restart the vnodeop (EVOPRESTART?)
197 * or rearrange vnodeop interface to leave vnode locking to file system
198 * specific code so that each file systems can have their own vnode locking and
199 * vnode re-using strategies.
200 */
201 int
202 lfs_reserveavail(struct lfs *fs, struct vnode *vp, struct vnode *vp2, int fsb)
203 {
204 CLEANERINFO *cip;
205 struct buf *bp;
206 int error, slept;
207
208 ASSERT_MAYBE_SEGLOCK(fs);
209 slept = 0;
210 simple_lock(&fs->lfs_interlock);
211 while (fsb > 0 && !lfs_fits(fs, fsb + fs->lfs_ravail + fs->lfs_favail)) {
212 simple_unlock(&fs->lfs_interlock);
213 #if 0
214 /*
215 * XXX ideally, we should unlock vnodes here
216 * because we might sleep very long time.
217 */
218 VOP_UNLOCK(vp, 0);
219 if (vp2 != NULL) {
220 VOP_UNLOCK(vp2, 0);
221 }
222 #else
223 /*
224 * XXX since we'll sleep for cleaner with vnode lock holding,
225 * deadlock will occur if cleaner tries to lock the vnode.
226 * (eg. lfs_markv -> lfs_fastvget -> getnewvnode -> vclean)
227 */
228 #endif
229
230 if (!slept) {
231 DLOG((DLOG_AVAIL, "lfs_reserve: waiting for %ld (bfree = %d,"
232 " est_bfree = %d)\n",
233 fsb + fs->lfs_ravail + fs->lfs_favail,
234 fs->lfs_bfree, LFS_EST_BFREE(fs)));
235 }
236 ++slept;
237
238 /* Wake up the cleaner */
239 LFS_CLEANERINFO(cip, fs, bp);
240 LFS_SYNC_CLEANERINFO(cip, fs, bp, 0);
241 wakeup(&lfs_allclean_wakeup);
242 wakeup(&fs->lfs_nextseg);
243
244 simple_lock(&fs->lfs_interlock);
245 /* Cleaner might have run while we were reading, check again */
246 if (lfs_fits(fs, fsb + fs->lfs_ravail + fs->lfs_favail))
247 break;
248
249 error = ltsleep(&fs->lfs_avail, PCATCH | PUSER, "lfs_reserve",
250 0, &fs->lfs_interlock);
251 #if 0
252 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX use lockstatus */
253 vn_lock(vp2, LK_EXCLUSIVE | LK_RETRY); /* XXX use lockstatus */
254 #endif
255 if (error) {
256 return error;
257 simple_unlock(&fs->lfs_interlock);
258 }
259 }
260 #ifdef DEBUG
261 if (slept) {
262 DLOG((DLOG_AVAIL, "lfs_reserve: woke up\n"));
263 }
264 #endif
265 fs->lfs_ravail += fsb;
266 simple_unlock(&fs->lfs_interlock);
267
268 return 0;
269 }
270
271 #ifdef DIAGNOSTIC
272 int lfs_rescount;
273 int lfs_rescountdirop;
274 #endif
275
276 int
277 lfs_reserve(struct lfs *fs, struct vnode *vp, struct vnode *vp2, int fsb)
278 {
279 int error;
280 int cantwait;
281
282 ASSERT_MAYBE_SEGLOCK(fs);
283 if (vp2) {
284 /* Make sure we're not in the process of reclaiming vp2 */
285 simple_lock(&fs->lfs_interlock);
286 while(fs->lfs_flags & LFS_UNDIROP) {
287 ltsleep(&fs->lfs_flags, PRIBIO + 1, "lfsrundirop", 0,
288 &fs->lfs_interlock);
289 }
290 simple_unlock(&fs->lfs_interlock);
291 }
292
293 KASSERT(fsb < 0 || VOP_ISLOCKED(vp));
294 KASSERT(vp2 == NULL || fsb < 0 || VOP_ISLOCKED(vp2));
295 KASSERT(vp2 == NULL || !(VTOI(vp2)->i_flag & IN_ADIROP));
296 KASSERT(vp2 == NULL || vp2 != fs->lfs_unlockvp);
297
298 cantwait = (VTOI(vp)->i_flag & IN_ADIROP) || fs->lfs_unlockvp == vp;
299 #ifdef DIAGNOSTIC
300 if (cantwait) {
301 if (fsb > 0)
302 lfs_rescountdirop++;
303 else if (fsb < 0)
304 lfs_rescountdirop--;
305 if (lfs_rescountdirop < 0)
306 panic("lfs_rescountdirop");
307 }
308 else {
309 if (fsb > 0)
310 lfs_rescount++;
311 else if (fsb < 0)
312 lfs_rescount--;
313 if (lfs_rescount < 0)
314 panic("lfs_rescount");
315 }
316 #endif
317 if (cantwait)
318 return 0;
319
320 /*
321 * XXX
322 * vref vnodes here so that cleaner doesn't try to reuse them.
323 * (see XXX comment in lfs_reserveavail)
324 */
325 lfs_vref(vp);
326 if (vp2 != NULL) {
327 lfs_vref(vp2);
328 }
329
330 error = lfs_reserveavail(fs, vp, vp2, fsb);
331 if (error)
332 goto done;
333
334 /*
335 * XXX just a guess. should be more precise.
336 */
337 error = lfs_reservebuf(fs, vp, vp2,
338 fragstoblks(fs, fsb), fsbtob(fs, fsb));
339 if (error)
340 lfs_reserveavail(fs, vp, vp2, -fsb);
341
342 done:
343 lfs_vunref(vp);
344 if (vp2 != NULL) {
345 lfs_vunref(vp2);
346 }
347
348 return error;
349 }
350
351 int
352 lfs_bwrite(void *v)
353 {
354 struct vop_bwrite_args /* {
355 struct buf *a_bp;
356 } */ *ap = v;
357 struct buf *bp = ap->a_bp;
358
359 #ifdef DIAGNOSTIC
360 if (VTOI(bp->b_vp)->i_lfs->lfs_ronly == 0 && (bp->b_flags & B_ASYNC)) {
361 panic("bawrite LFS buffer");
362 }
363 #endif /* DIAGNOSTIC */
364 return lfs_bwrite_ext(bp, 0);
365 }
366
367 /*
368 * Determine if there is enough room currently available to write fsb
369 * blocks. We need enough blocks for the new blocks, the current
370 * inode blocks (including potentially the ifile inode), a summary block,
371 * and the segment usage table, plus an ifile block.
372 */
373 int
374 lfs_fits(struct lfs *fs, int fsb)
375 {
376 int needed;
377
378 ASSERT_NO_SEGLOCK(fs);
379 needed = fsb + btofsb(fs, fs->lfs_sumsize) +
380 ((howmany(fs->lfs_uinodes + 1, INOPB(fs)) + fs->lfs_segtabsz +
381 1) << (fs->lfs_blktodb - fs->lfs_fsbtodb));
382
383 if (needed >= fs->lfs_avail) {
384 #ifdef DEBUG
385 DLOG((DLOG_AVAIL, "lfs_fits: no fit: fsb = %ld, uinodes = %ld, "
386 "needed = %ld, avail = %ld\n",
387 (long)fsb, (long)fs->lfs_uinodes, (long)needed,
388 (long)fs->lfs_avail));
389 #endif
390 return 0;
391 }
392 return 1;
393 }
394
395 int
396 lfs_availwait(struct lfs *fs, int fsb)
397 {
398 int error;
399 CLEANERINFO *cip;
400 struct buf *cbp;
401
402 ASSERT_NO_SEGLOCK(fs);
403 /* Push cleaner blocks through regardless */
404 simple_lock(&fs->lfs_interlock);
405 if (LFS_SEGLOCK_HELD(fs) &&
406 fs->lfs_sp->seg_flags & (SEGM_CLEAN | SEGM_FORCE_CKP)) {
407 simple_unlock(&fs->lfs_interlock);
408 return 0;
409 }
410 simple_unlock(&fs->lfs_interlock);
411
412 while (!lfs_fits(fs, fsb)) {
413 /*
414 * Out of space, need cleaner to run.
415 * Update the cleaner info, then wake it up.
416 * Note the cleanerinfo block is on the ifile
417 * so it CANT_WAIT.
418 */
419 LFS_CLEANERINFO(cip, fs, cbp);
420 LFS_SYNC_CLEANERINFO(cip, fs, cbp, 0);
421
422 #ifdef DEBUG
423 DLOG((DLOG_AVAIL, "lfs_availwait: out of available space, "
424 "waiting on cleaner\n"));
425 #endif
426
427 wakeup(&lfs_allclean_wakeup);
428 wakeup(&fs->lfs_nextseg);
429 #ifdef DIAGNOSTIC
430 if (LFS_SEGLOCK_HELD(fs))
431 panic("lfs_availwait: deadlock");
432 #endif
433 error = tsleep(&fs->lfs_avail, PCATCH | PUSER, "cleaner", 0);
434 if (error)
435 return (error);
436 }
437 return 0;
438 }
439
440 int
441 lfs_bwrite_ext(struct buf *bp, int flags)
442 {
443 struct lfs *fs;
444 struct inode *ip;
445 int fsb, s;
446
447 fs = VFSTOUFS(bp->b_vp->v_mount)->um_lfs;
448
449 ASSERT_MAYBE_SEGLOCK(fs);
450 KASSERT(bp->b_flags & B_BUSY);
451 KASSERT(flags & BW_CLEAN || !LFS_IS_MALLOC_BUF(bp));
452 KASSERT((bp->b_flags & (B_DELWRI|B_LOCKED)) != B_DELWRI);
453 KASSERT((bp->b_flags & (B_DELWRI|B_LOCKED)) != B_LOCKED);
454
455 /*
456 * Don't write *any* blocks if we're mounted read-only, or
457 * if we are "already unmounted".
458 *
459 * In particular the cleaner can't write blocks either.
460 */
461 if (fs->lfs_ronly || (fs->lfs_pflags & LFS_PF_CLEAN)) {
462 bp->b_flags &= ~(B_DELWRI | B_READ | B_ERROR);
463 LFS_UNLOCK_BUF(bp);
464 if (LFS_IS_MALLOC_BUF(bp))
465 bp->b_flags &= ~B_BUSY;
466 else
467 brelse(bp);
468 return (fs->lfs_ronly ? EROFS : 0);
469 }
470
471 /*
472 * Set the delayed write flag and use reassignbuf to move the buffer
473 * from the clean list to the dirty one.
474 *
475 * Set the B_LOCKED flag and unlock the buffer, causing brelse to move
476 * the buffer onto the LOCKED free list. This is necessary, otherwise
477 * getnewbuf() would try to reclaim the buffers using bawrite, which
478 * isn't going to work.
479 *
480 * XXX we don't let meta-data writes run out of space because they can
481 * come from the segment writer. We need to make sure that there is
482 * enough space reserved so that there's room to write meta-data
483 * blocks.
484 */
485 if (!(bp->b_flags & B_LOCKED)) {
486 fsb = fragstofsb(fs, numfrags(fs, bp->b_bcount));
487
488 ip = VTOI(bp->b_vp);
489 if (flags & BW_CLEAN) {
490 LFS_SET_UINO(ip, IN_CLEANING);
491 } else {
492 LFS_SET_UINO(ip, IN_MODIFIED);
493 }
494 fs->lfs_avail -= fsb;
495 bp->b_flags |= B_DELWRI;
496
497 LFS_LOCK_BUF(bp);
498 bp->b_flags &= ~(B_READ | B_DONE | B_ERROR);
499 s = splbio();
500 reassignbuf(bp, bp->b_vp);
501 splx(s);
502 }
503
504 if (bp->b_flags & B_CALL)
505 bp->b_flags &= ~B_BUSY;
506 else
507 brelse(bp);
508
509 return (0);
510 }
511
512 /*
513 * Called and return with the lfs_interlock held, but the lfs_subsys_lock
514 * not held.
515 */
516 void
517 lfs_flush_fs(struct lfs *fs, int flags)
518 {
519 ASSERT_NO_SEGLOCK(fs);
520 LOCK_ASSERT(simple_lock_held(&fs->lfs_interlock));
521 LOCK_ASSERT(!simple_lock_held(&lfs_subsys_lock));
522 if (fs->lfs_ronly)
523 return;
524
525 simple_lock(&lfs_subsys_lock);
526 if (lfs_dostats)
527 ++lfs_stats.flush_invoked;
528 simple_unlock(&lfs_subsys_lock);
529
530 simple_unlock(&fs->lfs_interlock);
531 lfs_writer_enter(fs, "fldirop");
532 lfs_segwrite(fs->lfs_ivnode->v_mount, flags);
533 lfs_writer_leave(fs);
534 simple_lock(&fs->lfs_interlock);
535 fs->lfs_favail = 0; /* XXX */
536 }
537
538 /*
539 * This routine initiates segment writes when LFS is consuming too many
540 * resources. Ideally the pageout daemon would be able to direct LFS
541 * more subtly.
542 * XXX We have one static count of locked buffers;
543 * XXX need to think more about the multiple filesystem case.
544 *
545 * Called and return with lfs_subsys_lock held.
546 * If fs != NULL, we hold the segment lock for fs.
547 */
548 void
549 lfs_flush(struct lfs *fs, int flags, int only_onefs)
550 {
551 extern u_int64_t locked_fakequeue_count;
552 struct mount *mp, *nmp;
553 struct lfs *tfs;
554
555 LOCK_ASSERT(simple_lock_held(&lfs_subsys_lock));
556 KDASSERT(fs == NULL || !LFS_SEGLOCK_HELD(fs));
557
558 if (lfs_dostats)
559 ++lfs_stats.write_exceeded;
560 /* XXX should we include SEGM_CKP here? */
561 if (lfs_writing && !(flags & SEGM_SYNC)) {
562 DLOG((DLOG_FLUSH, "lfs_flush: not flushing because another flush is active\n"));
563 return;
564 }
565 while (lfs_writing)
566 ltsleep(&lfs_writing, PRIBIO + 1, "lfsflush", 0,
567 &lfs_subsys_lock);
568 lfs_writing = 1;
569
570 simple_unlock(&lfs_subsys_lock);
571
572 if (only_onefs) {
573 KASSERT(fs != NULL);
574 if (vfs_busy(fs->lfs_ivnode->v_mount, LK_NOWAIT,
575 &mountlist_slock))
576 goto errout;
577 simple_lock(&fs->lfs_interlock);
578 lfs_flush_fs(fs, flags);
579 simple_unlock(&fs->lfs_interlock);
580 vfs_unbusy(fs->lfs_ivnode->v_mount);
581 } else {
582 locked_fakequeue_count = 0;
583 simple_lock(&mountlist_slock);
584 for (mp = CIRCLEQ_FIRST(&mountlist); mp != (void *)&mountlist;
585 mp = nmp) {
586 if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock)) {
587 DLOG((DLOG_FLUSH, "lfs_flush: fs vfs_busy\n"));
588 nmp = CIRCLEQ_NEXT(mp, mnt_list);
589 continue;
590 }
591 if (strncmp(&mp->mnt_stat.f_fstypename[0], MOUNT_LFS,
592 MFSNAMELEN) == 0) {
593 tfs = VFSTOUFS(mp)->um_lfs;
594 simple_lock(&tfs->lfs_interlock);
595 lfs_flush_fs(tfs, flags);
596 simple_unlock(&tfs->lfs_interlock);
597 }
598 simple_lock(&mountlist_slock);
599 nmp = CIRCLEQ_NEXT(mp, mnt_list);
600 vfs_unbusy(mp);
601 }
602 simple_unlock(&mountlist_slock);
603 }
604 LFS_DEBUG_COUNTLOCKED("flush");
605 wakeup(&lfs_subsys_pages);
606
607 errout:
608 simple_lock(&lfs_subsys_lock);
609 KASSERT(lfs_writing);
610 lfs_writing = 0;
611 wakeup(&lfs_writing);
612 }
613
614 #define INOCOUNT(fs) howmany((fs)->lfs_uinodes, INOPB(fs))
615 #define INOBYTES(fs) ((fs)->lfs_uinodes * sizeof (struct ufs1_dinode))
616
617 /*
618 * make sure that we don't have too many locked buffers.
619 * flush buffers if needed.
620 */
621 int
622 lfs_check(struct vnode *vp, daddr_t blkno, int flags)
623 {
624 int error;
625 struct lfs *fs;
626 struct inode *ip;
627 extern pid_t lfs_writer_daemon;
628
629 error = 0;
630 ip = VTOI(vp);
631
632 /* If out of buffers, wait on writer */
633 /* XXX KS - if it's the Ifile, we're probably the cleaner! */
634 if (ip->i_number == LFS_IFILE_INUM)
635 return 0;
636 /* If we're being called from inside a dirop, don't sleep */
637 if (ip->i_flag & IN_ADIROP)
638 return 0;
639
640 fs = ip->i_lfs;
641
642 ASSERT_NO_SEGLOCK(fs);
643 LOCK_ASSERT(!simple_lock_held(&fs->lfs_interlock));
644
645 /*
646 * If we would flush below, but dirops are active, sleep.
647 * Note that a dirop cannot ever reach this code!
648 */
649 simple_lock(&fs->lfs_interlock);
650 simple_lock(&lfs_subsys_lock);
651 while (fs->lfs_dirops > 0 &&
652 (locked_queue_count + INOCOUNT(fs) > LFS_MAX_BUFS ||
653 locked_queue_bytes + INOBYTES(fs) > LFS_MAX_BYTES ||
654 lfs_subsys_pages > LFS_MAX_PAGES ||
655 fs->lfs_dirvcount > LFS_MAX_FSDIROP(fs) ||
656 lfs_dirvcount > LFS_MAX_DIROP || fs->lfs_diropwait > 0))
657 {
658 simple_unlock(&lfs_subsys_lock);
659 ++fs->lfs_diropwait;
660 ltsleep(&fs->lfs_writer, PRIBIO+1, "bufdirop", 0,
661 &fs->lfs_interlock);
662 --fs->lfs_diropwait;
663 simple_lock(&lfs_subsys_lock);
664 }
665
666 #ifdef DEBUG
667 if (locked_queue_count + INOCOUNT(fs) > LFS_MAX_BUFS)
668 DLOG((DLOG_FLUSH, "lfs_check: lqc = %d, max %d\n",
669 locked_queue_count + INOCOUNT(fs), LFS_MAX_BUFS));
670 if (locked_queue_bytes + INOBYTES(fs) > LFS_MAX_BYTES)
671 DLOG((DLOG_FLUSH, "lfs_check: lqb = %ld, max %ld\n",
672 locked_queue_bytes + INOBYTES(fs), LFS_MAX_BYTES));
673 if (lfs_subsys_pages > LFS_MAX_PAGES)
674 DLOG((DLOG_FLUSH, "lfs_check: lssp = %d, max %d\n",
675 lfs_subsys_pages, LFS_MAX_PAGES));
676 if (lfs_fs_pagetrip && fs->lfs_pages > lfs_fs_pagetrip)
677 DLOG((DLOG_FLUSH, "lfs_check: fssp = %d, trip at %d\n",
678 fs->lfs_pages, lfs_fs_pagetrip));
679 if (lfs_dirvcount > LFS_MAX_DIROP)
680 DLOG((DLOG_FLUSH, "lfs_check: ldvc = %d, max %d\n",
681 lfs_dirvcount, LFS_MAX_DIROP));
682 if (fs->lfs_dirvcount > LFS_MAX_FSDIROP(fs))
683 DLOG((DLOG_FLUSH, "lfs_check: lfdvc = %d, max %d\n",
684 fs->lfs_dirvcount, LFS_MAX_FSDIROP(fs)));
685 if (fs->lfs_diropwait > 0)
686 DLOG((DLOG_FLUSH, "lfs_check: ldvw = %d\n",
687 fs->lfs_diropwait));
688 #endif
689
690 if (locked_queue_count + INOCOUNT(fs) > LFS_MAX_BUFS ||
691 locked_queue_bytes + INOBYTES(fs) > LFS_MAX_BYTES ||
692 lfs_subsys_pages > LFS_MAX_PAGES ||
693 fs->lfs_dirvcount > LFS_MAX_FSDIROP(fs) ||
694 lfs_dirvcount > LFS_MAX_DIROP || fs->lfs_diropwait > 0) {
695 simple_unlock(&fs->lfs_interlock);
696 lfs_flush(fs, flags, 0);
697 } else if (lfs_fs_pagetrip && fs->lfs_pages > lfs_fs_pagetrip) {
698 /*
699 * If we didn't flush the whole thing, some filesystems
700 * still might want to be flushed.
701 */
702 ++fs->lfs_pdflush;
703 wakeup(&lfs_writer_daemon);
704 simple_unlock(&fs->lfs_interlock);
705 } else
706 simple_unlock(&fs->lfs_interlock);
707
708 while (locked_queue_count + INOCOUNT(fs) > LFS_WAIT_BUFS ||
709 locked_queue_bytes + INOBYTES(fs) > LFS_WAIT_BYTES ||
710 lfs_subsys_pages > LFS_WAIT_PAGES ||
711 fs->lfs_dirvcount > LFS_MAX_FSDIROP(fs) ||
712 lfs_dirvcount > LFS_MAX_DIROP) {
713
714 if (lfs_dostats)
715 ++lfs_stats.wait_exceeded;
716 DLOG((DLOG_AVAIL, "lfs_check: waiting: count=%d, bytes=%ld\n",
717 locked_queue_count, locked_queue_bytes));
718 error = ltsleep(&locked_queue_count, PCATCH | PUSER,
719 "buffers", hz * LFS_BUFWAIT, &lfs_subsys_lock);
720 if (error != EWOULDBLOCK)
721 break;
722
723 /*
724 * lfs_flush might not flush all the buffers, if some of the
725 * inodes were locked or if most of them were Ifile blocks
726 * and we weren't asked to checkpoint. Try flushing again
727 * to keep us from blocking indefinitely.
728 */
729 if (locked_queue_count + INOCOUNT(fs) > LFS_MAX_BUFS ||
730 locked_queue_bytes + INOBYTES(fs) > LFS_MAX_BYTES) {
731 lfs_flush(fs, flags | SEGM_CKP, 0);
732 }
733 }
734 simple_unlock(&lfs_subsys_lock);
735 return (error);
736 }
737
738 /*
739 * Allocate a new buffer header.
740 */
741 struct buf *
742 lfs_newbuf(struct lfs *fs, struct vnode *vp, daddr_t daddr, size_t size, int type)
743 {
744 struct buf *bp;
745 size_t nbytes;
746 int s;
747
748 ASSERT_MAYBE_SEGLOCK(fs);
749 nbytes = roundup(size, fsbtob(fs, 1));
750
751 bp = getiobuf();
752 if (nbytes) {
753 bp->b_data = lfs_malloc(fs, nbytes, type);
754 /* memset(bp->b_data, 0, nbytes); */
755 }
756 #ifdef DIAGNOSTIC
757 if (vp == NULL)
758 panic("vp is NULL in lfs_newbuf");
759 if (bp == NULL)
760 panic("bp is NULL after malloc in lfs_newbuf");
761 #endif
762 bp->b_vp = NULL;
763 s = splbio();
764 bgetvp(vp, bp);
765 splx(s);
766
767 bp->b_bufsize = size;
768 bp->b_bcount = size;
769 bp->b_lblkno = daddr;
770 bp->b_blkno = daddr;
771 bp->b_error = 0;
772 bp->b_resid = 0;
773 bp->b_iodone = lfs_callback;
774 bp->b_flags = B_BUSY | B_CALL | B_NOCACHE;
775 bp->b_private = fs;
776
777 return (bp);
778 }
779
780 void
781 lfs_freebuf(struct lfs *fs, struct buf *bp)
782 {
783 int s;
784
785 s = splbio();
786 if (bp->b_vp)
787 brelvp(bp);
788 if (!(bp->b_flags & B_INVAL)) { /* B_INVAL indicates a "fake" buffer */
789 lfs_free(fs, bp->b_data, LFS_NB_UNKNOWN);
790 bp->b_data = NULL;
791 }
792 splx(s);
793 putiobuf(bp);
794 }
795
796 /*
797 * Definitions for the buffer free lists.
798 */
799 #define BQUEUES 4 /* number of free buffer queues */
800
801 #define BQ_LOCKED 0 /* super-blocks &c */
802 #define BQ_LRU 1 /* lru, useful buffers */
803 #define BQ_AGE 2 /* rubbish */
804 #define BQ_EMPTY 3 /* buffer headers with no memory */
805
806 extern TAILQ_HEAD(bqueues, buf) bufqueues[BQUEUES];
807 extern struct simplelock bqueue_slock;
808
809 /*
810 * Count buffers on the "locked" queue, and compare it to a pro-forma count.
811 * Don't count malloced buffers, since they don't detract from the total.
812 */
813 void
814 lfs_countlocked(int *count, long *bytes, const char *msg)
815 {
816 struct buf *bp;
817 int n = 0;
818 long int size = 0L;
819 int s;
820
821 s = splbio();
822 simple_lock(&bqueue_slock);
823 TAILQ_FOREACH(bp, &bufqueues[BQ_LOCKED], b_freelist) {
824 KASSERT(!(bp->b_flags & B_CALL));
825 n++;
826 size += bp->b_bufsize;
827 #ifdef DIAGNOSTIC
828 if (n > nbuf)
829 panic("lfs_countlocked: this can't happen: more"
830 " buffers locked than exist");
831 #endif
832 }
833 /*
834 * Theoretically this function never really does anything.
835 * Give a warning if we have to fix the accounting.
836 */
837 if (n != *count)
838 DLOG((DLOG_LLIST, "lfs_countlocked: %s: adjusted buf count"
839 " from %d to %d\n", msg, *count, n));
840 if (size != *bytes)
841 DLOG((DLOG_LLIST, "lfs_countlocked: %s: adjusted byte count"
842 " from %ld to %ld\n", msg, *bytes, size));
843 *count = n;
844 *bytes = size;
845 simple_unlock(&bqueue_slock);
846 splx(s);
847 return;
848 }
849