lfs_bio.c revision 1.95 1 /* $NetBSD: lfs_bio.c,v 1.95 2006/09/15 15:51:12 yamt 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.95 2006/09/15 15:51:12 yamt 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 lfs_wakeup_cleaner(fs);
242
243 simple_lock(&fs->lfs_interlock);
244 /* Cleaner might have run while we were reading, check again */
245 if (lfs_fits(fs, fsb + fs->lfs_ravail + fs->lfs_favail))
246 break;
247
248 error = ltsleep(&fs->lfs_avail, PCATCH | PUSER, "lfs_reserve",
249 0, &fs->lfs_interlock);
250 #if 0
251 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX use lockstatus */
252 vn_lock(vp2, LK_EXCLUSIVE | LK_RETRY); /* XXX use lockstatus */
253 #endif
254 if (error) {
255 simple_unlock(&fs->lfs_interlock);
256 return error;
257 }
258 }
259 #ifdef DEBUG
260 if (slept) {
261 DLOG((DLOG_AVAIL, "lfs_reserve: woke up\n"));
262 }
263 #endif
264 fs->lfs_ravail += fsb;
265 simple_unlock(&fs->lfs_interlock);
266
267 return 0;
268 }
269
270 #ifdef DIAGNOSTIC
271 int lfs_rescount;
272 int lfs_rescountdirop;
273 #endif
274
275 int
276 lfs_reserve(struct lfs *fs, struct vnode *vp, struct vnode *vp2, int fsb)
277 {
278 int error;
279 int cantwait;
280
281 ASSERT_MAYBE_SEGLOCK(fs);
282 if (vp2) {
283 /* Make sure we're not in the process of reclaiming vp2 */
284 simple_lock(&fs->lfs_interlock);
285 while(fs->lfs_flags & LFS_UNDIROP) {
286 ltsleep(&fs->lfs_flags, PRIBIO + 1, "lfsrundirop", 0,
287 &fs->lfs_interlock);
288 }
289 simple_unlock(&fs->lfs_interlock);
290 }
291
292 KASSERT(fsb < 0 || VOP_ISLOCKED(vp));
293 KASSERT(vp2 == NULL || fsb < 0 || VOP_ISLOCKED(vp2));
294 KASSERT(vp2 == NULL || !(VTOI(vp2)->i_flag & IN_ADIROP));
295 KASSERT(vp2 == NULL || vp2 != fs->lfs_unlockvp);
296
297 cantwait = (VTOI(vp)->i_flag & IN_ADIROP) || fs->lfs_unlockvp == vp;
298 #ifdef DIAGNOSTIC
299 if (cantwait) {
300 if (fsb > 0)
301 lfs_rescountdirop++;
302 else if (fsb < 0)
303 lfs_rescountdirop--;
304 if (lfs_rescountdirop < 0)
305 panic("lfs_rescountdirop");
306 }
307 else {
308 if (fsb > 0)
309 lfs_rescount++;
310 else if (fsb < 0)
311 lfs_rescount--;
312 if (lfs_rescount < 0)
313 panic("lfs_rescount");
314 }
315 #endif
316 if (cantwait)
317 return 0;
318
319 /*
320 * XXX
321 * vref vnodes here so that cleaner doesn't try to reuse them.
322 * (see XXX comment in lfs_reserveavail)
323 */
324 lfs_vref(vp);
325 if (vp2 != NULL) {
326 lfs_vref(vp2);
327 }
328
329 error = lfs_reserveavail(fs, vp, vp2, fsb);
330 if (error)
331 goto done;
332
333 /*
334 * XXX just a guess. should be more precise.
335 */
336 error = lfs_reservebuf(fs, vp, vp2,
337 fragstoblks(fs, fsb), fsbtob(fs, fsb));
338 if (error)
339 lfs_reserveavail(fs, vp, vp2, -fsb);
340
341 done:
342 lfs_vunref(vp);
343 if (vp2 != NULL) {
344 lfs_vunref(vp2);
345 }
346
347 return error;
348 }
349
350 int
351 lfs_bwrite(void *v)
352 {
353 struct vop_bwrite_args /* {
354 struct buf *a_bp;
355 } */ *ap = v;
356 struct buf *bp = ap->a_bp;
357
358 #ifdef DIAGNOSTIC
359 if (VTOI(bp->b_vp)->i_lfs->lfs_ronly == 0 && (bp->b_flags & B_ASYNC)) {
360 panic("bawrite LFS buffer");
361 }
362 #endif /* DIAGNOSTIC */
363 return lfs_bwrite_ext(bp, 0);
364 }
365
366 /*
367 * Determine if there is enough room currently available to write fsb
368 * blocks. We need enough blocks for the new blocks, the current
369 * inode blocks (including potentially the ifile inode), a summary block,
370 * and the segment usage table, plus an ifile block.
371 */
372 int
373 lfs_fits(struct lfs *fs, int fsb)
374 {
375 int needed;
376
377 ASSERT_NO_SEGLOCK(fs);
378 needed = fsb + btofsb(fs, fs->lfs_sumsize) +
379 ((howmany(fs->lfs_uinodes + 1, INOPB(fs)) + fs->lfs_segtabsz +
380 1) << (fs->lfs_blktodb - fs->lfs_fsbtodb));
381
382 if (needed >= fs->lfs_avail) {
383 #ifdef DEBUG
384 DLOG((DLOG_AVAIL, "lfs_fits: no fit: fsb = %ld, uinodes = %ld, "
385 "needed = %ld, avail = %ld\n",
386 (long)fsb, (long)fs->lfs_uinodes, (long)needed,
387 (long)fs->lfs_avail));
388 #endif
389 return 0;
390 }
391 return 1;
392 }
393
394 int
395 lfs_availwait(struct lfs *fs, int fsb)
396 {
397 int error;
398 CLEANERINFO *cip;
399 struct buf *cbp;
400
401 ASSERT_NO_SEGLOCK(fs);
402 /* Push cleaner blocks through regardless */
403 simple_lock(&fs->lfs_interlock);
404 if (LFS_SEGLOCK_HELD(fs) &&
405 fs->lfs_sp->seg_flags & (SEGM_CLEAN | SEGM_FORCE_CKP)) {
406 simple_unlock(&fs->lfs_interlock);
407 return 0;
408 }
409 simple_unlock(&fs->lfs_interlock);
410
411 while (!lfs_fits(fs, fsb)) {
412 /*
413 * Out of space, need cleaner to run.
414 * Update the cleaner info, then wake it up.
415 * Note the cleanerinfo block is on the ifile
416 * so it CANT_WAIT.
417 */
418 LFS_CLEANERINFO(cip, fs, cbp);
419 LFS_SYNC_CLEANERINFO(cip, fs, cbp, 0);
420
421 #ifdef DEBUG
422 DLOG((DLOG_AVAIL, "lfs_availwait: out of available space, "
423 "waiting on cleaner\n"));
424 #endif
425
426 lfs_wakeup_cleaner(fs);
427 #ifdef DIAGNOSTIC
428 if (LFS_SEGLOCK_HELD(fs))
429 panic("lfs_availwait: deadlock");
430 #endif
431 error = tsleep(&fs->lfs_avail, PCATCH | PUSER, "cleaner", 0);
432 if (error)
433 return (error);
434 }
435 return 0;
436 }
437
438 int
439 lfs_bwrite_ext(struct buf *bp, int flags)
440 {
441 struct lfs *fs;
442 struct inode *ip;
443 int fsb, s;
444
445 fs = VFSTOUFS(bp->b_vp->v_mount)->um_lfs;
446
447 ASSERT_MAYBE_SEGLOCK(fs);
448 KASSERT(bp->b_flags & B_BUSY);
449 KASSERT(flags & BW_CLEAN || !LFS_IS_MALLOC_BUF(bp));
450 KASSERT((bp->b_flags & (B_DELWRI|B_LOCKED)) != B_DELWRI);
451 KASSERT((bp->b_flags & (B_DELWRI|B_LOCKED)) != B_LOCKED);
452
453 /*
454 * Don't write *any* blocks if we're mounted read-only, or
455 * if we are "already unmounted".
456 *
457 * In particular the cleaner can't write blocks either.
458 */
459 if (fs->lfs_ronly || (fs->lfs_pflags & LFS_PF_CLEAN)) {
460 bp->b_flags &= ~(B_DELWRI | B_READ | B_ERROR);
461 LFS_UNLOCK_BUF(bp);
462 if (LFS_IS_MALLOC_BUF(bp))
463 bp->b_flags &= ~B_BUSY;
464 else
465 brelse(bp);
466 return (fs->lfs_ronly ? EROFS : 0);
467 }
468
469 /*
470 * Set the delayed write flag and use reassignbuf to move the buffer
471 * from the clean list to the dirty one.
472 *
473 * Set the B_LOCKED flag and unlock the buffer, causing brelse to move
474 * the buffer onto the LOCKED free list. This is necessary, otherwise
475 * getnewbuf() would try to reclaim the buffers using bawrite, which
476 * isn't going to work.
477 *
478 * XXX we don't let meta-data writes run out of space because they can
479 * come from the segment writer. We need to make sure that there is
480 * enough space reserved so that there's room to write meta-data
481 * blocks.
482 */
483 if (!(bp->b_flags & B_LOCKED)) {
484 fsb = fragstofsb(fs, numfrags(fs, bp->b_bcount));
485
486 ip = VTOI(bp->b_vp);
487 if (flags & BW_CLEAN) {
488 LFS_SET_UINO(ip, IN_CLEANING);
489 } else {
490 LFS_SET_UINO(ip, IN_MODIFIED);
491 }
492 fs->lfs_avail -= fsb;
493 bp->b_flags |= B_DELWRI;
494
495 LFS_LOCK_BUF(bp);
496 bp->b_flags &= ~(B_READ | B_DONE | B_ERROR);
497 s = splbio();
498 reassignbuf(bp, bp->b_vp);
499 splx(s);
500 }
501
502 if (bp->b_flags & B_CALL)
503 bp->b_flags &= ~B_BUSY;
504 else
505 brelse(bp);
506
507 return (0);
508 }
509
510 /*
511 * Called and return with the lfs_interlock held, but the lfs_subsys_lock
512 * not held.
513 */
514 void
515 lfs_flush_fs(struct lfs *fs, int flags)
516 {
517 ASSERT_NO_SEGLOCK(fs);
518 LOCK_ASSERT(simple_lock_held(&fs->lfs_interlock));
519 LOCK_ASSERT(!simple_lock_held(&lfs_subsys_lock));
520 if (fs->lfs_ronly)
521 return;
522
523 simple_lock(&lfs_subsys_lock);
524 if (lfs_dostats)
525 ++lfs_stats.flush_invoked;
526 simple_unlock(&lfs_subsys_lock);
527
528 simple_unlock(&fs->lfs_interlock);
529 lfs_writer_enter(fs, "fldirop");
530 lfs_segwrite(fs->lfs_ivnode->v_mount, flags);
531 lfs_writer_leave(fs);
532 simple_lock(&fs->lfs_interlock);
533 fs->lfs_favail = 0; /* XXX */
534 }
535
536 /*
537 * This routine initiates segment writes when LFS is consuming too many
538 * resources. Ideally the pageout daemon would be able to direct LFS
539 * more subtly.
540 * XXX We have one static count of locked buffers;
541 * XXX need to think more about the multiple filesystem case.
542 *
543 * Called and return with lfs_subsys_lock held.
544 * If fs != NULL, we hold the segment lock for fs.
545 */
546 void
547 lfs_flush(struct lfs *fs, int flags, int only_onefs)
548 {
549 extern u_int64_t locked_fakequeue_count;
550 struct mount *mp, *nmp;
551 struct lfs *tfs;
552
553 LOCK_ASSERT(simple_lock_held(&lfs_subsys_lock));
554 KDASSERT(fs == NULL || !LFS_SEGLOCK_HELD(fs));
555
556 if (lfs_dostats)
557 ++lfs_stats.write_exceeded;
558 /* XXX should we include SEGM_CKP here? */
559 if (lfs_writing && !(flags & SEGM_SYNC)) {
560 DLOG((DLOG_FLUSH, "lfs_flush: not flushing because another flush is active\n"));
561 return;
562 }
563 while (lfs_writing)
564 ltsleep(&lfs_writing, PRIBIO + 1, "lfsflush", 0,
565 &lfs_subsys_lock);
566 lfs_writing = 1;
567
568 simple_unlock(&lfs_subsys_lock);
569
570 if (only_onefs) {
571 KASSERT(fs != NULL);
572 if (vfs_busy(fs->lfs_ivnode->v_mount, LK_NOWAIT,
573 &mountlist_slock))
574 goto errout;
575 simple_lock(&fs->lfs_interlock);
576 lfs_flush_fs(fs, flags);
577 simple_unlock(&fs->lfs_interlock);
578 vfs_unbusy(fs->lfs_ivnode->v_mount);
579 } else {
580 locked_fakequeue_count = 0;
581 simple_lock(&mountlist_slock);
582 for (mp = CIRCLEQ_FIRST(&mountlist); mp != (void *)&mountlist;
583 mp = nmp) {
584 if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock)) {
585 DLOG((DLOG_FLUSH, "lfs_flush: fs vfs_busy\n"));
586 nmp = CIRCLEQ_NEXT(mp, mnt_list);
587 continue;
588 }
589 if (strncmp(&mp->mnt_stat.f_fstypename[0], MOUNT_LFS,
590 MFSNAMELEN) == 0) {
591 tfs = VFSTOUFS(mp)->um_lfs;
592 simple_lock(&tfs->lfs_interlock);
593 lfs_flush_fs(tfs, flags);
594 simple_unlock(&tfs->lfs_interlock);
595 }
596 simple_lock(&mountlist_slock);
597 nmp = CIRCLEQ_NEXT(mp, mnt_list);
598 vfs_unbusy(mp);
599 }
600 simple_unlock(&mountlist_slock);
601 }
602 LFS_DEBUG_COUNTLOCKED("flush");
603 wakeup(&lfs_subsys_pages);
604
605 errout:
606 simple_lock(&lfs_subsys_lock);
607 KASSERT(lfs_writing);
608 lfs_writing = 0;
609 wakeup(&lfs_writing);
610 }
611
612 #define INOCOUNT(fs) howmany((fs)->lfs_uinodes, INOPB(fs))
613 #define INOBYTES(fs) ((fs)->lfs_uinodes * sizeof (struct ufs1_dinode))
614
615 /*
616 * make sure that we don't have too many locked buffers.
617 * flush buffers if needed.
618 */
619 int
620 lfs_check(struct vnode *vp, daddr_t blkno, int flags)
621 {
622 int error;
623 struct lfs *fs;
624 struct inode *ip;
625 extern pid_t lfs_writer_daemon;
626
627 error = 0;
628 ip = VTOI(vp);
629
630 /* If out of buffers, wait on writer */
631 /* XXX KS - if it's the Ifile, we're probably the cleaner! */
632 if (ip->i_number == LFS_IFILE_INUM)
633 return 0;
634 /* If we're being called from inside a dirop, don't sleep */
635 if (ip->i_flag & IN_ADIROP)
636 return 0;
637
638 fs = ip->i_lfs;
639
640 ASSERT_NO_SEGLOCK(fs);
641 LOCK_ASSERT(!simple_lock_held(&fs->lfs_interlock));
642
643 /*
644 * If we would flush below, but dirops are active, sleep.
645 * Note that a dirop cannot ever reach this code!
646 */
647 simple_lock(&fs->lfs_interlock);
648 simple_lock(&lfs_subsys_lock);
649 while (fs->lfs_dirops > 0 &&
650 (locked_queue_count + INOCOUNT(fs) > LFS_MAX_BUFS ||
651 locked_queue_bytes + INOBYTES(fs) > LFS_MAX_BYTES ||
652 lfs_subsys_pages > LFS_MAX_PAGES ||
653 fs->lfs_dirvcount > LFS_MAX_FSDIROP(fs) ||
654 lfs_dirvcount > LFS_MAX_DIROP || fs->lfs_diropwait > 0))
655 {
656 simple_unlock(&lfs_subsys_lock);
657 ++fs->lfs_diropwait;
658 ltsleep(&fs->lfs_writer, PRIBIO+1, "bufdirop", 0,
659 &fs->lfs_interlock);
660 --fs->lfs_diropwait;
661 simple_lock(&lfs_subsys_lock);
662 }
663
664 #ifdef DEBUG
665 if (locked_queue_count + INOCOUNT(fs) > LFS_MAX_BUFS)
666 DLOG((DLOG_FLUSH, "lfs_check: lqc = %d, max %d\n",
667 locked_queue_count + INOCOUNT(fs), LFS_MAX_BUFS));
668 if (locked_queue_bytes + INOBYTES(fs) > LFS_MAX_BYTES)
669 DLOG((DLOG_FLUSH, "lfs_check: lqb = %ld, max %ld\n",
670 locked_queue_bytes + INOBYTES(fs), LFS_MAX_BYTES));
671 if (lfs_subsys_pages > LFS_MAX_PAGES)
672 DLOG((DLOG_FLUSH, "lfs_check: lssp = %d, max %d\n",
673 lfs_subsys_pages, LFS_MAX_PAGES));
674 if (lfs_fs_pagetrip && fs->lfs_pages > lfs_fs_pagetrip)
675 DLOG((DLOG_FLUSH, "lfs_check: fssp = %d, trip at %d\n",
676 fs->lfs_pages, lfs_fs_pagetrip));
677 if (lfs_dirvcount > LFS_MAX_DIROP)
678 DLOG((DLOG_FLUSH, "lfs_check: ldvc = %d, max %d\n",
679 lfs_dirvcount, LFS_MAX_DIROP));
680 if (fs->lfs_dirvcount > LFS_MAX_FSDIROP(fs))
681 DLOG((DLOG_FLUSH, "lfs_check: lfdvc = %d, max %d\n",
682 fs->lfs_dirvcount, LFS_MAX_FSDIROP(fs)));
683 if (fs->lfs_diropwait > 0)
684 DLOG((DLOG_FLUSH, "lfs_check: ldvw = %d\n",
685 fs->lfs_diropwait));
686 #endif
687
688 if (locked_queue_count + INOCOUNT(fs) > LFS_MAX_BUFS ||
689 locked_queue_bytes + INOBYTES(fs) > LFS_MAX_BYTES ||
690 lfs_subsys_pages > LFS_MAX_PAGES ||
691 fs->lfs_dirvcount > LFS_MAX_FSDIROP(fs) ||
692 lfs_dirvcount > LFS_MAX_DIROP || fs->lfs_diropwait > 0) {
693 simple_unlock(&fs->lfs_interlock);
694 lfs_flush(fs, flags, 0);
695 } else if (lfs_fs_pagetrip && fs->lfs_pages > lfs_fs_pagetrip) {
696 /*
697 * If we didn't flush the whole thing, some filesystems
698 * still might want to be flushed.
699 */
700 ++fs->lfs_pdflush;
701 wakeup(&lfs_writer_daemon);
702 simple_unlock(&fs->lfs_interlock);
703 } else
704 simple_unlock(&fs->lfs_interlock);
705
706 while (locked_queue_count + INOCOUNT(fs) > LFS_WAIT_BUFS ||
707 locked_queue_bytes + INOBYTES(fs) > LFS_WAIT_BYTES ||
708 lfs_subsys_pages > LFS_WAIT_PAGES ||
709 fs->lfs_dirvcount > LFS_MAX_FSDIROP(fs) ||
710 lfs_dirvcount > LFS_MAX_DIROP) {
711
712 if (lfs_dostats)
713 ++lfs_stats.wait_exceeded;
714 DLOG((DLOG_AVAIL, "lfs_check: waiting: count=%d, bytes=%ld\n",
715 locked_queue_count, locked_queue_bytes));
716 error = ltsleep(&locked_queue_count, PCATCH | PUSER,
717 "buffers", hz * LFS_BUFWAIT, &lfs_subsys_lock);
718 if (error != EWOULDBLOCK)
719 break;
720
721 /*
722 * lfs_flush might not flush all the buffers, if some of the
723 * inodes were locked or if most of them were Ifile blocks
724 * and we weren't asked to checkpoint. Try flushing again
725 * to keep us from blocking indefinitely.
726 */
727 if (locked_queue_count + INOCOUNT(fs) > LFS_MAX_BUFS ||
728 locked_queue_bytes + INOBYTES(fs) > LFS_MAX_BYTES) {
729 lfs_flush(fs, flags | SEGM_CKP, 0);
730 }
731 }
732 simple_unlock(&lfs_subsys_lock);
733 return (error);
734 }
735
736 /*
737 * Allocate a new buffer header.
738 */
739 struct buf *
740 lfs_newbuf(struct lfs *fs, struct vnode *vp, daddr_t daddr, size_t size, int type)
741 {
742 struct buf *bp;
743 size_t nbytes;
744 int s;
745
746 ASSERT_MAYBE_SEGLOCK(fs);
747 nbytes = roundup(size, fsbtob(fs, 1));
748
749 bp = getiobuf();
750 if (nbytes) {
751 bp->b_data = lfs_malloc(fs, nbytes, type);
752 /* memset(bp->b_data, 0, nbytes); */
753 }
754 #ifdef DIAGNOSTIC
755 if (vp == NULL)
756 panic("vp is NULL in lfs_newbuf");
757 if (bp == NULL)
758 panic("bp is NULL after malloc in lfs_newbuf");
759 #endif
760 bp->b_vp = NULL;
761 s = splbio();
762 bgetvp(vp, bp);
763 splx(s);
764
765 bp->b_bufsize = size;
766 bp->b_bcount = size;
767 bp->b_lblkno = daddr;
768 bp->b_blkno = daddr;
769 bp->b_error = 0;
770 bp->b_resid = 0;
771 bp->b_iodone = lfs_callback;
772 bp->b_flags = B_BUSY | B_CALL | B_NOCACHE;
773 bp->b_private = fs;
774
775 return (bp);
776 }
777
778 void
779 lfs_freebuf(struct lfs *fs, struct buf *bp)
780 {
781 int s;
782
783 s = splbio();
784 if (bp->b_vp)
785 brelvp(bp);
786 if (!(bp->b_flags & B_INVAL)) { /* B_INVAL indicates a "fake" buffer */
787 lfs_free(fs, bp->b_data, LFS_NB_UNKNOWN);
788 bp->b_data = NULL;
789 }
790 splx(s);
791 putiobuf(bp);
792 }
793
794 /*
795 * Definitions for the buffer free lists.
796 */
797 #define BQUEUES 4 /* number of free buffer queues */
798
799 #define BQ_LOCKED 0 /* super-blocks &c */
800 #define BQ_LRU 1 /* lru, useful buffers */
801 #define BQ_AGE 2 /* rubbish */
802 #define BQ_EMPTY 3 /* buffer headers with no memory */
803
804 extern TAILQ_HEAD(bqueues, buf) bufqueues[BQUEUES];
805 extern struct simplelock bqueue_slock;
806
807 /*
808 * Count buffers on the "locked" queue, and compare it to a pro-forma count.
809 * Don't count malloced buffers, since they don't detract from the total.
810 */
811 void
812 lfs_countlocked(int *count, long *bytes, const char *msg)
813 {
814 struct buf *bp;
815 int n = 0;
816 long int size = 0L;
817 int s;
818
819 s = splbio();
820 simple_lock(&bqueue_slock);
821 TAILQ_FOREACH(bp, &bufqueues[BQ_LOCKED], b_freelist) {
822 KASSERT(!(bp->b_flags & B_CALL));
823 n++;
824 size += bp->b_bufsize;
825 #ifdef DIAGNOSTIC
826 if (n > nbuf)
827 panic("lfs_countlocked: this can't happen: more"
828 " buffers locked than exist");
829 #endif
830 }
831 /*
832 * Theoretically this function never really does anything.
833 * Give a warning if we have to fix the accounting.
834 */
835 if (n != *count)
836 DLOG((DLOG_LLIST, "lfs_countlocked: %s: adjusted buf count"
837 " from %d to %d\n", msg, *count, n));
838 if (size != *bytes)
839 DLOG((DLOG_LLIST, "lfs_countlocked: %s: adjusted byte count"
840 " from %ld to %ld\n", msg, *bytes, size));
841 *count = n;
842 *bytes = size;
843 simple_unlock(&bqueue_slock);
844 splx(s);
845 return;
846 }
847
848 int
849 lfs_wait_pages(void)
850 {
851 int active, inactive;
852
853 uvm_estimatepageable(&active, &inactive);
854 return LFS_WAIT_RESOURCE(active + inactive + uvmexp.free, 1);
855 }
856
857 int
858 lfs_max_pages(void)
859 {
860 int active, inactive;
861
862 uvm_estimatepageable(&active, &inactive);
863 return LFS_MAX_RESOURCE(active + inactive + uvmexp.free, 1);
864 }
865