lfs_bio.c revision 1.103 1 /* $NetBSD: lfs_bio.c,v 1.103 2007/07/29 13:31:14 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 * 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.103 2007/07/29 13:31:14 ad 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,
154 struct vnode *vp2, 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,
203 struct vnode *vp2, int fsb)
204 {
205 CLEANERINFO *cip;
206 struct buf *bp;
207 int error, slept;
208
209 ASSERT_MAYBE_SEGLOCK(fs);
210 slept = 0;
211 simple_lock(&fs->lfs_interlock);
212 while (fsb > 0 && !lfs_fits(fs, fsb + fs->lfs_ravail + fs->lfs_favail)) {
213 simple_unlock(&fs->lfs_interlock);
214 #if 0
215 /*
216 * XXX ideally, we should unlock vnodes here
217 * because we might sleep very long time.
218 */
219 VOP_UNLOCK(vp, 0);
220 if (vp2 != NULL) {
221 VOP_UNLOCK(vp2, 0);
222 }
223 #else
224 /*
225 * XXX since we'll sleep for cleaner with vnode lock holding,
226 * deadlock will occur if cleaner tries to lock the vnode.
227 * (eg. lfs_markv -> lfs_fastvget -> getnewvnode -> vclean)
228 */
229 #endif
230
231 if (!slept) {
232 DLOG((DLOG_AVAIL, "lfs_reserve: waiting for %ld (bfree = %d,"
233 " est_bfree = %d)\n",
234 fsb + fs->lfs_ravail + fs->lfs_favail,
235 fs->lfs_bfree, LFS_EST_BFREE(fs)));
236 }
237 ++slept;
238
239 /* Wake up the cleaner */
240 LFS_CLEANERINFO(cip, fs, bp);
241 LFS_SYNC_CLEANERINFO(cip, fs, bp, 0);
242 lfs_wakeup_cleaner(fs);
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 simple_unlock(&fs->lfs_interlock);
257 return error;
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 lfs_wakeup_cleaner(fs);
428 #ifdef DIAGNOSTIC
429 if (LFS_SEGLOCK_HELD(fs))
430 panic("lfs_availwait: deadlock");
431 #endif
432 error = tsleep(&fs->lfs_avail, PCATCH | PUSER, "cleaner", 0);
433 if (error)
434 return (error);
435 }
436 return 0;
437 }
438
439 int
440 lfs_bwrite_ext(struct buf *bp, int flags)
441 {
442 struct lfs *fs;
443 struct inode *ip;
444 int fsb, s;
445
446 fs = VFSTOUFS(bp->b_vp->v_mount)->um_lfs;
447
448 ASSERT_MAYBE_SEGLOCK(fs);
449 KASSERT(bp->b_flags & B_BUSY);
450 KASSERT(flags & BW_CLEAN || !LFS_IS_MALLOC_BUF(bp));
451 KASSERT((bp->b_flags & (B_DELWRI|B_LOCKED)) != B_DELWRI);
452 KASSERT((bp->b_flags & (B_DELWRI|B_LOCKED)) != B_LOCKED);
453
454 /*
455 * Don't write *any* blocks if we're mounted read-only, or
456 * if we are "already unmounted".
457 *
458 * In particular the cleaner can't write blocks either.
459 */
460 if (fs->lfs_ronly || (fs->lfs_pflags & LFS_PF_CLEAN)) {
461 bp->b_flags &= ~(B_DELWRI | B_READ);
462 bp->b_error = 0;
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);
499 bp->b_error = 0;
500 s = splbio();
501 reassignbuf(bp, bp->b_vp);
502 splx(s);
503 }
504
505 if (bp->b_flags & B_CALL)
506 bp->b_flags &= ~B_BUSY;
507 else
508 brelse(bp);
509
510 return (0);
511 }
512
513 /*
514 * Called and return with the lfs_interlock held, but no other simple_locks
515 * held.
516 */
517 void
518 lfs_flush_fs(struct lfs *fs, int flags)
519 {
520 ASSERT_NO_SEGLOCK(fs);
521 LOCK_ASSERT(simple_lock_held(&fs->lfs_interlock));
522 LOCK_ASSERT(!simple_lock_held(&lfs_subsys_lock));
523 if (fs->lfs_ronly)
524 return;
525
526 simple_lock(&lfs_subsys_lock);
527 if (lfs_dostats)
528 ++lfs_stats.flush_invoked;
529 simple_unlock(&lfs_subsys_lock);
530
531 simple_unlock(&fs->lfs_interlock);
532 lfs_writer_enter(fs, "fldirop");
533 lfs_segwrite(fs->lfs_ivnode->v_mount, flags);
534 lfs_writer_leave(fs);
535 simple_lock(&fs->lfs_interlock);
536 fs->lfs_favail = 0; /* XXX */
537 }
538
539 /*
540 * This routine initiates segment writes when LFS is consuming too many
541 * resources. Ideally the pageout daemon would be able to direct LFS
542 * more subtly.
543 * XXX We have one static count of locked buffers;
544 * XXX need to think more about the multiple filesystem case.
545 *
546 * Called and return with lfs_subsys_lock held.
547 * If fs != NULL, we hold the segment lock for fs.
548 */
549 void
550 lfs_flush(struct lfs *fs, int flags, int only_onefs)
551 {
552 extern u_int64_t locked_fakequeue_count;
553 struct mount *mp, *nmp;
554 struct lfs *tfs;
555
556 LOCK_ASSERT(simple_lock_held(&lfs_subsys_lock));
557 KDASSERT(fs == NULL || !LFS_SEGLOCK_HELD(fs));
558
559 if (lfs_dostats)
560 ++lfs_stats.write_exceeded;
561 /* XXX should we include SEGM_CKP here? */
562 if (lfs_writing && !(flags & SEGM_SYNC)) {
563 DLOG((DLOG_FLUSH, "lfs_flush: not flushing because another flush is active\n"));
564 return;
565 }
566 while (lfs_writing)
567 ltsleep(&lfs_writing, PRIBIO + 1, "lfsflush", 0,
568 &lfs_subsys_lock);
569 lfs_writing = 1;
570
571 simple_unlock(&lfs_subsys_lock);
572
573 if (only_onefs) {
574 KASSERT(fs != NULL);
575 if (vfs_busy(fs->lfs_ivnode->v_mount, LK_NOWAIT,
576 &mountlist_slock))
577 goto errout;
578 simple_lock(&fs->lfs_interlock);
579 lfs_flush_fs(fs, flags);
580 simple_unlock(&fs->lfs_interlock);
581 vfs_unbusy(fs->lfs_ivnode->v_mount);
582 } else {
583 locked_fakequeue_count = 0;
584 simple_lock(&mountlist_slock);
585 for (mp = CIRCLEQ_FIRST(&mountlist); mp != (void *)&mountlist;
586 mp = nmp) {
587 if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock)) {
588 DLOG((DLOG_FLUSH, "lfs_flush: fs vfs_busy\n"));
589 nmp = CIRCLEQ_NEXT(mp, mnt_list);
590 continue;
591 }
592 if (strncmp(mp->mnt_stat.f_fstypename, MOUNT_LFS,
593 sizeof(mp->mnt_stat.f_fstypename)) == 0) {
594 tfs = VFSTOUFS(mp)->um_lfs;
595 simple_lock(&tfs->lfs_interlock);
596 lfs_flush_fs(tfs, flags);
597 simple_unlock(&tfs->lfs_interlock);
598 }
599 simple_lock(&mountlist_slock);
600 nmp = CIRCLEQ_NEXT(mp, mnt_list);
601 vfs_unbusy(mp);
602 }
603 simple_unlock(&mountlist_slock);
604 }
605 LFS_DEBUG_COUNTLOCKED("flush");
606 wakeup(&lfs_subsys_pages);
607
608 errout:
609 simple_lock(&lfs_subsys_lock);
610 KASSERT(lfs_writing);
611 lfs_writing = 0;
612 wakeup(&lfs_writing);
613 }
614
615 #define INOCOUNT(fs) howmany((fs)->lfs_uinodes, INOPB(fs))
616 #define INOBYTES(fs) ((fs)->lfs_uinodes * sizeof (struct ufs1_dinode))
617
618 /*
619 * make sure that we don't have too many locked buffers.
620 * flush buffers if needed.
621 */
622 int
623 lfs_check(struct vnode *vp, daddr_t blkno, int flags)
624 {
625 int error;
626 struct lfs *fs;
627 struct inode *ip;
628 extern pid_t lfs_writer_daemon;
629
630 error = 0;
631 ip = VTOI(vp);
632
633 /* If out of buffers, wait on writer */
634 /* XXX KS - if it's the Ifile, we're probably the cleaner! */
635 if (ip->i_number == LFS_IFILE_INUM)
636 return 0;
637 /* If we're being called from inside a dirop, don't sleep */
638 if (ip->i_flag & IN_ADIROP)
639 return 0;
640
641 fs = ip->i_lfs;
642
643 ASSERT_NO_SEGLOCK(fs);
644 LOCK_ASSERT(!simple_lock_held(&fs->lfs_interlock));
645
646 /*
647 * If we would flush below, but dirops are active, sleep.
648 * Note that a dirop cannot ever reach this code!
649 */
650 simple_lock(&fs->lfs_interlock);
651 simple_lock(&lfs_subsys_lock);
652 while (fs->lfs_dirops > 0 &&
653 (locked_queue_count + INOCOUNT(fs) > LFS_MAX_BUFS ||
654 locked_queue_bytes + INOBYTES(fs) > LFS_MAX_BYTES ||
655 lfs_subsys_pages > LFS_MAX_PAGES ||
656 fs->lfs_dirvcount > LFS_MAX_FSDIROP(fs) ||
657 lfs_dirvcount > LFS_MAX_DIROP || fs->lfs_diropwait > 0))
658 {
659 simple_unlock(&lfs_subsys_lock);
660 ++fs->lfs_diropwait;
661 ltsleep(&fs->lfs_writer, PRIBIO+1, "bufdirop", 0,
662 &fs->lfs_interlock);
663 --fs->lfs_diropwait;
664 simple_lock(&lfs_subsys_lock);
665 }
666
667 #ifdef DEBUG
668 if (locked_queue_count + INOCOUNT(fs) > LFS_MAX_BUFS)
669 DLOG((DLOG_FLUSH, "lfs_check: lqc = %d, max %d\n",
670 locked_queue_count + INOCOUNT(fs), LFS_MAX_BUFS));
671 if (locked_queue_bytes + INOBYTES(fs) > LFS_MAX_BYTES)
672 DLOG((DLOG_FLUSH, "lfs_check: lqb = %ld, max %ld\n",
673 locked_queue_bytes + INOBYTES(fs), LFS_MAX_BYTES));
674 if (lfs_subsys_pages > LFS_MAX_PAGES)
675 DLOG((DLOG_FLUSH, "lfs_check: lssp = %d, max %d\n",
676 lfs_subsys_pages, LFS_MAX_PAGES));
677 if (lfs_fs_pagetrip && fs->lfs_pages > lfs_fs_pagetrip)
678 DLOG((DLOG_FLUSH, "lfs_check: fssp = %d, trip at %d\n",
679 fs->lfs_pages, lfs_fs_pagetrip));
680 if (lfs_dirvcount > LFS_MAX_DIROP)
681 DLOG((DLOG_FLUSH, "lfs_check: ldvc = %d, max %d\n",
682 lfs_dirvcount, LFS_MAX_DIROP));
683 if (fs->lfs_dirvcount > LFS_MAX_FSDIROP(fs))
684 DLOG((DLOG_FLUSH, "lfs_check: lfdvc = %d, max %d\n",
685 fs->lfs_dirvcount, LFS_MAX_FSDIROP(fs)));
686 if (fs->lfs_diropwait > 0)
687 DLOG((DLOG_FLUSH, "lfs_check: ldvw = %d\n",
688 fs->lfs_diropwait));
689 #endif
690
691 /* If there are too many pending dirops, we have to flush them. */
692 if (fs->lfs_dirvcount > LFS_MAX_FSDIROP(fs) ||
693 lfs_dirvcount > LFS_MAX_DIROP || fs->lfs_diropwait > 0) {
694 flags |= SEGM_CKP;
695 }
696
697 if (locked_queue_count + INOCOUNT(fs) > LFS_MAX_BUFS ||
698 locked_queue_bytes + INOBYTES(fs) > LFS_MAX_BYTES ||
699 lfs_subsys_pages > LFS_MAX_PAGES ||
700 fs->lfs_dirvcount > LFS_MAX_FSDIROP(fs) ||
701 lfs_dirvcount > LFS_MAX_DIROP || fs->lfs_diropwait > 0) {
702 simple_unlock(&fs->lfs_interlock);
703 lfs_flush(fs, flags, 0);
704 } else if (lfs_fs_pagetrip && fs->lfs_pages > lfs_fs_pagetrip) {
705 /*
706 * If we didn't flush the whole thing, some filesystems
707 * still might want to be flushed.
708 */
709 ++fs->lfs_pdflush;
710 wakeup(&lfs_writer_daemon);
711 simple_unlock(&fs->lfs_interlock);
712 } else
713 simple_unlock(&fs->lfs_interlock);
714
715 while (locked_queue_count + INOCOUNT(fs) > LFS_WAIT_BUFS ||
716 locked_queue_bytes + INOBYTES(fs) > LFS_WAIT_BYTES ||
717 lfs_subsys_pages > LFS_WAIT_PAGES ||
718 fs->lfs_dirvcount > LFS_MAX_FSDIROP(fs) ||
719 lfs_dirvcount > LFS_MAX_DIROP) {
720
721 if (lfs_dostats)
722 ++lfs_stats.wait_exceeded;
723 DLOG((DLOG_AVAIL, "lfs_check: waiting: count=%d, bytes=%ld\n",
724 locked_queue_count, locked_queue_bytes));
725 error = ltsleep(&locked_queue_count, PCATCH | PUSER,
726 "buffers", hz * LFS_BUFWAIT, &lfs_subsys_lock);
727 if (error != EWOULDBLOCK)
728 break;
729
730 /*
731 * lfs_flush might not flush all the buffers, if some of the
732 * inodes were locked or if most of them were Ifile blocks
733 * and we weren't asked to checkpoint. Try flushing again
734 * to keep us from blocking indefinitely.
735 */
736 if (locked_queue_count + INOCOUNT(fs) > LFS_MAX_BUFS ||
737 locked_queue_bytes + INOBYTES(fs) > LFS_MAX_BYTES) {
738 lfs_flush(fs, flags | SEGM_CKP, 0);
739 }
740 }
741 simple_unlock(&lfs_subsys_lock);
742 return (error);
743 }
744
745 /*
746 * Allocate a new buffer header.
747 */
748 struct buf *
749 lfs_newbuf(struct lfs *fs, struct vnode *vp, daddr_t daddr, size_t size, int type)
750 {
751 struct buf *bp;
752 size_t nbytes;
753 int s;
754
755 ASSERT_MAYBE_SEGLOCK(fs);
756 nbytes = roundup(size, fsbtob(fs, 1));
757
758 bp = getiobuf();
759 if (nbytes) {
760 bp->b_data = lfs_malloc(fs, nbytes, type);
761 /* memset(bp->b_data, 0, nbytes); */
762 }
763 #ifdef DIAGNOSTIC
764 if (vp == NULL)
765 panic("vp is NULL in lfs_newbuf");
766 if (bp == NULL)
767 panic("bp is NULL after malloc in lfs_newbuf");
768 #endif
769 bp->b_vp = NULL;
770 s = splbio();
771 bgetvp(vp, bp);
772 splx(s);
773
774 bp->b_bufsize = size;
775 bp->b_bcount = size;
776 bp->b_lblkno = daddr;
777 bp->b_blkno = daddr;
778 bp->b_error = 0;
779 bp->b_resid = 0;
780 bp->b_iodone = lfs_callback;
781 bp->b_flags = B_BUSY | B_CALL | B_NOCACHE;
782 bp->b_private = fs;
783
784 return (bp);
785 }
786
787 void
788 lfs_freebuf(struct lfs *fs, struct buf *bp)
789 {
790 int s;
791
792 s = splbio();
793 if (bp->b_vp)
794 brelvp(bp);
795 if (!(bp->b_flags & B_INVAL)) { /* B_INVAL indicates a "fake" buffer */
796 lfs_free(fs, bp->b_data, LFS_NB_UNKNOWN);
797 bp->b_data = NULL;
798 }
799 splx(s);
800 putiobuf(bp);
801 }
802
803 /*
804 * Definitions for the buffer free lists.
805 */
806 #define BQUEUES 4 /* number of free buffer queues */
807
808 #define BQ_LOCKED 0 /* super-blocks &c */
809 #define BQ_LRU 1 /* lru, useful buffers */
810 #define BQ_AGE 2 /* rubbish */
811 #define BQ_EMPTY 3 /* buffer headers with no memory */
812
813 extern TAILQ_HEAD(bqueues, buf) bufqueues[BQUEUES];
814 extern struct simplelock bqueue_slock;
815
816 /*
817 * Count buffers on the "locked" queue, and compare it to a pro-forma count.
818 * Don't count malloced buffers, since they don't detract from the total.
819 */
820 void
821 lfs_countlocked(int *count, long *bytes, const char *msg)
822 {
823 struct buf *bp;
824 int n = 0;
825 long int size = 0L;
826 int s;
827
828 s = splbio();
829 simple_lock(&bqueue_slock);
830 TAILQ_FOREACH(bp, &bufqueues[BQ_LOCKED], b_freelist) {
831 KASSERT(!(bp->b_flags & B_CALL));
832 n++;
833 size += bp->b_bufsize;
834 #ifdef DIAGNOSTIC
835 if (n > nbuf)
836 panic("lfs_countlocked: this can't happen: more"
837 " buffers locked than exist");
838 #endif
839 }
840 /*
841 * Theoretically this function never really does anything.
842 * Give a warning if we have to fix the accounting.
843 */
844 if (n != *count) {
845 DLOG((DLOG_LLIST, "lfs_countlocked: %s: adjusted buf count"
846 " from %d to %d\n", msg, *count, n));
847 }
848 if (size != *bytes) {
849 DLOG((DLOG_LLIST, "lfs_countlocked: %s: adjusted byte count"
850 " from %ld to %ld\n", msg, *bytes, size));
851 }
852 *count = n;
853 *bytes = size;
854 simple_unlock(&bqueue_slock);
855 splx(s);
856 return;
857 }
858
859 int
860 lfs_wait_pages(void)
861 {
862 int active, inactive;
863
864 uvm_estimatepageable(&active, &inactive);
865 return LFS_WAIT_RESOURCE(active + inactive + uvmexp.free, 1);
866 }
867
868 int
869 lfs_max_pages(void)
870 {
871 int active, inactive;
872
873 uvm_estimatepageable(&active, &inactive);
874 return LFS_MAX_RESOURCE(active + inactive + uvmexp.free, 1);
875 }
876