lfs_bio.c revision 1.65 1 /* $NetBSD: lfs_bio.c,v 1.65 2003/04/02 10:39:40 fvdl 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. All advertising materials mentioning features or use of this software
51 * must display the following acknowledgement:
52 * This product includes software developed by the University of
53 * California, Berkeley and its contributors.
54 * 4. Neither the name of the University nor the names of its contributors
55 * may be used to endorse or promote products derived from this software
56 * without specific prior written permission.
57 *
58 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
59 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
62 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
63 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
64 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
66 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
67 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68 * SUCH DAMAGE.
69 *
70 * @(#)lfs_bio.c 8.10 (Berkeley) 6/10/95
71 */
72
73 #include <sys/cdefs.h>
74 __KERNEL_RCSID(0, "$NetBSD: lfs_bio.c,v 1.65 2003/04/02 10:39:40 fvdl Exp $");
75
76 #include <sys/param.h>
77 #include <sys/systm.h>
78 #include <sys/proc.h>
79 #include <sys/buf.h>
80 #include <sys/vnode.h>
81 #include <sys/resourcevar.h>
82 #include <sys/mount.h>
83 #include <sys/kernel.h>
84
85 #include <ufs/ufs/inode.h>
86 #include <ufs/ufs/ufsmount.h>
87 #include <ufs/ufs/ufs_extern.h>
88
89 #include <ufs/lfs/lfs.h>
90 #include <ufs/lfs/lfs_extern.h>
91
92 #include <uvm/uvm.h>
93
94 /* Macros to clear/set/test flags. */
95 # define SET(t, f) (t) |= (f)
96 # define CLR(t, f) (t) &= ~(f)
97 # define ISSET(t, f) ((t) & (f))
98
99 /*
100 * LFS block write function.
101 *
102 * XXX
103 * No write cost accounting is done.
104 * This is almost certainly wrong for synchronous operations and NFS.
105 */
106 int locked_queue_count = 0; /* Count of locked-down buffers. */
107 long locked_queue_bytes = 0L; /* Total size of locked buffers. */
108 int lfs_subsys_pages = 0L; /* Total number LFS-written pages */
109 int lfs_writing = 0; /* Set if already kicked off a writer
110 because of buffer space */
111 /* Lock for lfs_subsys_pages */
112 struct simplelock lfs_subsys_lock = SIMPLELOCK_INITIALIZER;
113
114 extern int lfs_dostats;
115 extern int lfs_do_flush;
116
117 /*
118 * reserved number/bytes of locked buffers
119 */
120 int locked_queue_rcount = 0;
121 long locked_queue_rbytes = 0L;
122
123 int lfs_fits_buf(struct lfs *, int, int);
124 int lfs_reservebuf(struct lfs *, struct vnode *vp, struct vnode *vp2,
125 int, int);
126 int lfs_reserveavail(struct lfs *, struct vnode *vp, struct vnode *vp2, int);
127
128 int
129 lfs_fits_buf(struct lfs *fs, int n, int bytes)
130 {
131 int count_fit =
132 (locked_queue_count + locked_queue_rcount + n < LFS_WAIT_BUFS);
133 int bytes_fit =
134 (locked_queue_bytes + locked_queue_rbytes + bytes < LFS_WAIT_BYTES);
135
136 #ifdef DEBUG_LFS
137 if (!count_fit) {
138 printf("lfs_fits_buf: no fit count: %d + %d + %d >= %d\n",
139 locked_queue_count, locked_queue_rcount,
140 n, LFS_WAIT_BUFS);
141 }
142 if (!bytes_fit) {
143 printf("lfs_fits_buf: no fit bytes: %ld + %ld + %d >= %d\n",
144 locked_queue_bytes, locked_queue_rbytes,
145 bytes, LFS_WAIT_BYTES);
146 }
147 #endif /* DEBUG_LFS */
148
149 return (count_fit && bytes_fit);
150 }
151
152 /* ARGSUSED */
153 int
154 lfs_reservebuf(struct lfs *fs, struct vnode *vp, struct vnode *vp2,
155 int n, int bytes)
156 {
157 KASSERT(locked_queue_rcount >= 0);
158 KASSERT(locked_queue_rbytes >= 0);
159
160 while (n > 0 && !lfs_fits_buf(fs, n, bytes)) {
161 int error;
162
163 ++fs->lfs_writer;
164 lfs_flush(fs, 0);
165 if (--fs->lfs_writer == 0)
166 wakeup(&fs->lfs_dirops);
167
168 error = tsleep(&locked_queue_count, PCATCH | PUSER,
169 "lfsresbuf", hz * LFS_BUFWAIT);
170 if (error && error != EWOULDBLOCK)
171 return error;
172 }
173
174 locked_queue_rcount += n;
175 locked_queue_rbytes += bytes;
176
177 KASSERT(locked_queue_rcount >= 0);
178 KASSERT(locked_queue_rbytes >= 0);
179
180 return 0;
181 }
182
183 /*
184 * Try to reserve some blocks, prior to performing a sensitive operation that
185 * requires the vnode lock to be honored. If there is not enough space, give
186 * up the vnode lock temporarily and wait for the space to become available.
187 *
188 * Called with vp locked. (Note nowever that if fsb < 0, vp is ignored.)
189 *
190 * XXX YAMT - it isn't safe to unlock vp here
191 * because the node might be modified while we sleep.
192 * (eg. cached states like i_offset might be stale,
193 * the vnode might be truncated, etc..)
194 * maybe we should have a way to restart the vnodeop (EVOPRESTART?)
195 * or rearrange vnodeop interface to leave vnode locking to file system
196 * specific code so that each file systems can have their own vnode locking and
197 * vnode re-using strategies.
198 */
199 int
200 lfs_reserveavail(struct lfs *fs, struct vnode *vp, struct vnode *vp2, int fsb)
201 {
202 CLEANERINFO *cip;
203 struct buf *bp;
204 int error, slept;
205
206 slept = 0;
207 while (fsb > 0 && !lfs_fits(fs, fsb + fs->lfs_ravail)) {
208 #if 0
209 /*
210 * XXX ideally, we should unlock vnodes here
211 * because we might sleep very long time.
212 */
213 VOP_UNLOCK(vp, 0);
214 if (vp2 != NULL) {
215 VOP_UNLOCK(vp2, 0);
216 }
217 #else
218 /*
219 * XXX since we'll sleep for cleaner with vnode lock holding,
220 * deadlock will occur if cleaner tries to lock the vnode.
221 * (eg. lfs_markv -> lfs_fastvget -> getnewvnode -> vclean)
222 */
223 #endif
224
225 if (!slept) {
226 #ifdef DEBUG
227 printf("lfs_reserve: waiting for %ld (bfree = %d,"
228 " est_bfree = %d)\n",
229 fsb + fs->lfs_ravail, fs->lfs_bfree,
230 LFS_EST_BFREE(fs));
231 #endif
232 }
233 ++slept;
234
235 /* Wake up the cleaner */
236 LFS_CLEANERINFO(cip, fs, bp);
237 LFS_SYNC_CLEANERINFO(cip, fs, bp, 0);
238 wakeup(&lfs_allclean_wakeup);
239 wakeup(&fs->lfs_nextseg);
240
241 error = tsleep(&fs->lfs_avail, PCATCH | PUSER, "lfs_reserve",
242 0);
243 #if 0
244 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX use lockstatus */
245 vn_lock(vp2, LK_EXCLUSIVE | LK_RETRY); /* XXX use lockstatus */
246 #endif
247 if (error)
248 return error;
249 }
250 #ifdef DEBUG
251 if (slept)
252 printf("lfs_reserve: woke up\n");
253 #endif
254 fs->lfs_ravail += fsb;
255
256 return 0;
257 }
258
259 #ifdef DIAGNOSTIC
260 int lfs_rescount;
261 int lfs_rescountdirop;
262 #endif
263
264 int
265 lfs_reserve(struct lfs *fs, struct vnode *vp, struct vnode *vp2, int fsb)
266 {
267 int error;
268 int cantwait;
269
270 KASSERT(fsb < 0 || VOP_ISLOCKED(vp));
271 KASSERT(vp2 == NULL || fsb < 0 || VOP_ISLOCKED(vp2));
272 KASSERT(vp2 == NULL || !(VTOI(vp2)->i_flag & IN_ADIROP));
273 KASSERT(vp2 == NULL || vp2 != fs->lfs_unlockvp);
274
275 cantwait = (VTOI(vp)->i_flag & IN_ADIROP) || fs->lfs_unlockvp == vp;
276 #ifdef DIAGNOSTIC
277 if (cantwait) {
278 if (fsb > 0)
279 lfs_rescountdirop++;
280 else if (fsb < 0)
281 lfs_rescountdirop--;
282 if (lfs_rescountdirop < 0)
283 panic("lfs_rescountdirop");
284 }
285 else {
286 if (fsb > 0)
287 lfs_rescount++;
288 else if (fsb < 0)
289 lfs_rescount--;
290 if (lfs_rescount < 0)
291 panic("lfs_rescount");
292 }
293 #endif
294 if (cantwait)
295 return 0;
296
297 /*
298 * XXX
299 * vref vnodes here so that cleaner doesn't try to reuse them.
300 * (see XXX comment in lfs_reserveavail)
301 */
302 lfs_vref(vp);
303 if (vp2 != NULL) {
304 lfs_vref(vp2);
305 }
306
307 error = lfs_reserveavail(fs, vp, vp2, fsb);
308 if (error)
309 goto done;
310
311 /*
312 * XXX just a guess. should be more precise.
313 */
314 error = lfs_reservebuf(fs, vp, vp2,
315 fragstoblks(fs, fsb), fsbtob(fs, fsb));
316 if (error)
317 lfs_reserveavail(fs, vp, vp2, -fsb);
318
319 done:
320 lfs_vunref(vp);
321 if (vp2 != NULL) {
322 lfs_vunref(vp2);
323 }
324
325 return error;
326 }
327
328 int
329 lfs_bwrite(void *v)
330 {
331 struct vop_bwrite_args /* {
332 struct buf *a_bp;
333 } */ *ap = v;
334 struct buf *bp = ap->a_bp;
335
336 #ifdef DIAGNOSTIC
337 if (VTOI(bp->b_vp)->i_lfs->lfs_ronly == 0 && (bp->b_flags & B_ASYNC)) {
338 panic("bawrite LFS buffer");
339 }
340 #endif /* DIAGNOSTIC */
341 return lfs_bwrite_ext(bp,0);
342 }
343
344 /*
345 * Determine if there is enough room currently available to write fsb
346 * blocks. We need enough blocks for the new blocks, the current
347 * inode blocks (including potentially the ifile inode), a summary block,
348 * and the segment usage table, plus an ifile block.
349 */
350 int
351 lfs_fits(struct lfs *fs, int fsb)
352 {
353 int needed;
354
355 needed = fsb + btofsb(fs, fs->lfs_sumsize) +
356 ((howmany(fs->lfs_uinodes + 1, INOPB(fs)) + fs->lfs_segtabsz +
357 1) << (fs->lfs_blktodb - fs->lfs_fsbtodb));
358
359 if (needed >= fs->lfs_avail) {
360 #ifdef DEBUG
361 printf("lfs_fits: no fit: fsb = %d, uinodes = %d, "
362 "needed = %d, avail = %d\n",
363 fsb, fs->lfs_uinodes, needed, fs->lfs_avail);
364 #endif
365 return 0;
366 }
367 return 1;
368 }
369
370 int
371 lfs_availwait(struct lfs *fs, int fsb)
372 {
373 int error;
374 CLEANERINFO *cip;
375 struct buf *cbp;
376
377 /* Push cleaner blocks through regardless */
378 simple_lock(&fs->lfs_interlock);
379 if (fs->lfs_seglock &&
380 fs->lfs_lockpid == curproc->p_pid &&
381 fs->lfs_sp->seg_flags & (SEGM_CLEAN | SEGM_FORCE_CKP)) {
382 simple_unlock(&fs->lfs_interlock);
383 return 0;
384 }
385 simple_unlock(&fs->lfs_interlock);
386
387 while (!lfs_fits(fs, fsb)) {
388 /*
389 * Out of space, need cleaner to run.
390 * Update the cleaner info, then wake it up.
391 * Note the cleanerinfo block is on the ifile
392 * so it CANT_WAIT.
393 */
394 LFS_CLEANERINFO(cip, fs, cbp);
395 LFS_SYNC_CLEANERINFO(cip, fs, cbp, 0);
396
397 printf("lfs_availwait: out of available space, "
398 "waiting on cleaner\n");
399
400 wakeup(&lfs_allclean_wakeup);
401 wakeup(&fs->lfs_nextseg);
402 #ifdef DIAGNOSTIC
403 if (fs->lfs_seglock && fs->lfs_lockpid == curproc->p_pid)
404 panic("lfs_availwait: deadlock");
405 #endif
406 error = tsleep(&fs->lfs_avail, PCATCH | PUSER, "cleaner", 0);
407 if (error)
408 return (error);
409 }
410 return 0;
411 }
412
413 int
414 lfs_bwrite_ext(struct buf *bp, int flags)
415 {
416 struct lfs *fs;
417 struct inode *ip;
418 int fsb, s;
419
420 KASSERT(bp->b_flags & B_BUSY);
421 KASSERT(flags & BW_CLEAN || !LFS_IS_MALLOC_BUF(bp));
422
423 /*
424 * Don't write *any* blocks if we're mounted read-only.
425 * In particular the cleaner can't write blocks either.
426 */
427 if (VTOI(bp->b_vp)->i_lfs->lfs_ronly) {
428 bp->b_flags &= ~(B_DELWRI | B_READ | B_ERROR);
429 LFS_UNLOCK_BUF(bp);
430 if (LFS_IS_MALLOC_BUF(bp))
431 bp->b_flags &= ~B_BUSY;
432 else
433 brelse(bp);
434 return EROFS;
435 }
436
437 /*
438 * Set the delayed write flag and use reassignbuf to move the buffer
439 * from the clean list to the dirty one.
440 *
441 * Set the B_LOCKED flag and unlock the buffer, causing brelse to move
442 * the buffer onto the LOCKED free list. This is necessary, otherwise
443 * getnewbuf() would try to reclaim the buffers using bawrite, which
444 * isn't going to work.
445 *
446 * XXX we don't let meta-data writes run out of space because they can
447 * come from the segment writer. We need to make sure that there is
448 * enough space reserved so that there's room to write meta-data
449 * blocks.
450 */
451 if (!(bp->b_flags & B_LOCKED)) {
452 fs = VFSTOUFS(bp->b_vp->v_mount)->um_lfs;
453 fsb = fragstofsb(fs, numfrags(fs, bp->b_bcount));
454
455 ip = VTOI(bp->b_vp);
456 if (flags & BW_CLEAN) {
457 LFS_SET_UINO(ip, IN_CLEANING);
458 } else {
459 LFS_SET_UINO(ip, IN_MODIFIED);
460 if (bp->b_lblkno >= 0)
461 LFS_SET_UINO(ip, IN_UPDATE);
462 }
463 fs->lfs_avail -= fsb;
464 bp->b_flags |= B_DELWRI;
465
466 LFS_LOCK_BUF(bp);
467 bp->b_flags &= ~(B_READ | B_DONE | B_ERROR);
468 s = splbio();
469 reassignbuf(bp, bp->b_vp);
470 splx(s);
471 }
472
473 if (bp->b_flags & B_CALL)
474 bp->b_flags &= ~B_BUSY;
475 else
476 brelse(bp);
477
478 return (0);
479 }
480
481 void
482 lfs_flush_fs(struct lfs *fs, int flags)
483 {
484 if (fs->lfs_ronly)
485 return;
486
487 /* disallow dirops during flush */
488 fs->lfs_writer++;
489
490 /* drain dirops */
491 while (fs->lfs_dirops > 0) {
492 ++fs->lfs_diropwait;
493 tsleep(&fs->lfs_writer, PRIBIO+1, "fldirop", 0);
494 --fs->lfs_diropwait;
495 }
496
497 if (lfs_dostats)
498 ++lfs_stats.flush_invoked;
499 lfs_segwrite(fs->lfs_ivnode->v_mount, flags);
500
501 /* allow dirops again */
502 if (--fs->lfs_writer == 0)
503 wakeup(&fs->lfs_dirops);
504 }
505
506 /*
507 * XXX
508 * This routine flushes buffers out of the B_LOCKED queue when LFS has too
509 * many locked down. Eventually the pageout daemon will simply call LFS
510 * when pages need to be reclaimed. Note, we have one static count of locked
511 * buffers, so we can't have more than a single file system. To make this
512 * work for multiple file systems, put the count into the mount structure.
513 */
514 void
515 lfs_flush(struct lfs *fs, int flags)
516 {
517 struct mount *mp, *nmp;
518
519 if (lfs_dostats)
520 ++lfs_stats.write_exceeded;
521 if (lfs_writing && flags == 0) {/* XXX flags */
522 #ifdef DEBUG_LFS
523 printf("lfs_flush: not flushing because another flush is active\n");
524 #endif
525 return;
526 }
527 /* XXX MP */
528 while (lfs_writing && (flags & SEGM_WRITERD))
529 ltsleep(&lfs_writing, PRIBIO + 1, "lfsflush", 0, 0);
530 lfs_writing = 1;
531
532 simple_lock(&lfs_subsys_lock);
533 lfs_subsys_pages = 0; /* XXXUBC need a better way to count this */
534 simple_unlock(&lfs_subsys_lock);
535 wakeup(&lfs_subsys_pages);
536
537 simple_lock(&mountlist_slock);
538 for (mp = mountlist.cqh_first; mp != (void *)&mountlist; mp = nmp) {
539 if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock)) {
540 nmp = mp->mnt_list.cqe_next;
541 continue;
542 }
543 if (strncmp(&mp->mnt_stat.f_fstypename[0], MOUNT_LFS, MFSNAMELEN) == 0)
544 lfs_flush_fs(((struct ufsmount *)mp->mnt_data)->ufsmount_u.lfs, flags);
545 simple_lock(&mountlist_slock);
546 nmp = mp->mnt_list.cqe_next;
547 vfs_unbusy(mp);
548 }
549 simple_unlock(&mountlist_slock);
550 LFS_DEBUG_COUNTLOCKED("flush");
551
552 lfs_writing = 0;
553 wakeup(&lfs_writing);
554 }
555
556 #define INOCOUNT(fs) howmany((fs)->lfs_uinodes, INOPB(fs))
557 #define INOBYTES(fs) ((fs)->lfs_uinodes * sizeof (struct ufs1_dinode))
558
559 int
560 lfs_check(struct vnode *vp, daddr_t blkno, int flags)
561 {
562 int error;
563 struct lfs *fs;
564 struct inode *ip;
565 extern int lfs_dirvcount;
566
567 error = 0;
568 ip = VTOI(vp);
569
570 /* If out of buffers, wait on writer */
571 /* XXX KS - if it's the Ifile, we're probably the cleaner! */
572 if (ip->i_number == LFS_IFILE_INUM)
573 return 0;
574 /* If we're being called from inside a dirop, don't sleep */
575 if (ip->i_flag & IN_ADIROP)
576 return 0;
577
578 fs = ip->i_lfs;
579
580 /*
581 * If we would flush below, but dirops are active, sleep.
582 * Note that a dirop cannot ever reach this code!
583 */
584 simple_lock(&lfs_subsys_lock);
585 while (fs->lfs_dirops > 0 &&
586 (locked_queue_count + INOCOUNT(fs) > LFS_MAX_BUFS ||
587 locked_queue_bytes + INOBYTES(fs) > LFS_MAX_BYTES ||
588 lfs_subsys_pages > LFS_MAX_PAGES ||
589 lfs_dirvcount > LFS_MAX_DIROP || fs->lfs_diropwait > 0))
590 {
591 ++fs->lfs_diropwait;
592 ltsleep(&fs->lfs_writer, PRIBIO+1, "bufdirop", 0,
593 &lfs_subsys_lock);
594 --fs->lfs_diropwait;
595 }
596
597 #ifdef DEBUG_LFS_FLUSH
598 if (locked_queue_count + INOCOUNT(fs) > LFS_MAX_BUFS)
599 printf("lqc = %d, max %d\n", locked_queue_count + INOCOUNT(fs),
600 LFS_MAX_BUFS);
601 if (locked_queue_bytes + INOBYTES(fs) > LFS_MAX_BYTES)
602 printf("lqb = %ld, max %d\n", locked_queue_bytes + INOBYTES(fs),
603 LFS_MAX_BYTES);
604 if (lfs_subsys_pages > LFS_MAX_PAGES)
605 printf("lssp = %d, max %d\n", lfs_subsys_pages, LFS_MAX_PAGES);
606 if (lfs_dirvcount > LFS_MAX_DIROP)
607 printf("ldvc = %d, max %d\n", lfs_dirvcount, LFS_MAX_DIROP);
608 if (fs->lfs_diropwait > 0)
609 printf("ldvw = %d\n", fs->lfs_diropwait);
610 #endif
611
612 if (locked_queue_count + INOCOUNT(fs) > LFS_MAX_BUFS ||
613 locked_queue_bytes + INOBYTES(fs) > LFS_MAX_BYTES ||
614 lfs_subsys_pages > LFS_MAX_PAGES ||
615 lfs_dirvcount > LFS_MAX_DIROP || fs->lfs_diropwait > 0)
616 {
617 simple_unlock(&lfs_subsys_lock);
618 lfs_flush(fs, flags);
619 simple_lock(&lfs_subsys_lock);
620 }
621
622 while (locked_queue_count + INOCOUNT(fs) > LFS_WAIT_BUFS ||
623 locked_queue_bytes + INOBYTES(fs) > LFS_WAIT_BYTES ||
624 lfs_subsys_pages > LFS_WAIT_PAGES ||
625 lfs_dirvcount > LFS_MAX_DIROP)
626 {
627 simple_unlock(&lfs_subsys_lock);
628 if (lfs_dostats)
629 ++lfs_stats.wait_exceeded;
630 #ifdef DEBUG_LFS
631 printf("lfs_check: waiting: count=%d, bytes=%ld\n",
632 locked_queue_count, locked_queue_bytes);
633 #endif
634 error = tsleep(&locked_queue_count, PCATCH | PUSER,
635 "buffers", hz * LFS_BUFWAIT);
636 if (error != EWOULDBLOCK) {
637 simple_lock(&lfs_subsys_lock);
638 break;
639 }
640 /*
641 * lfs_flush might not flush all the buffers, if some of the
642 * inodes were locked or if most of them were Ifile blocks
643 * and we weren't asked to checkpoint. Try flushing again
644 * to keep us from blocking indefinitely.
645 */
646 if (locked_queue_count + INOCOUNT(fs) > LFS_MAX_BUFS ||
647 locked_queue_bytes + INOBYTES(fs) > LFS_MAX_BYTES)
648 {
649 lfs_flush(fs, flags | SEGM_CKP);
650 }
651 simple_lock(&lfs_subsys_lock);
652 }
653 simple_unlock(&lfs_subsys_lock);
654 return (error);
655 }
656
657 /*
658 * Allocate a new buffer header.
659 */
660 struct buf *
661 lfs_newbuf(struct lfs *fs, struct vnode *vp, daddr_t daddr, size_t size, int type)
662 {
663 struct buf *bp;
664 size_t nbytes;
665 int s;
666
667 nbytes = roundup(size, fsbtob(fs, 1));
668
669 s = splbio();
670 bp = pool_get(&bufpool, PR_WAITOK);
671 splx(s);
672 memset(bp, 0, sizeof(struct buf));
673 BUF_INIT(bp);
674 if (nbytes) {
675 bp->b_data = lfs_malloc(fs, nbytes, type);
676 /* memset(bp->b_data, 0, nbytes); */
677 }
678 #ifdef DIAGNOSTIC
679 if (vp == NULL)
680 panic("vp is NULL in lfs_newbuf");
681 if (bp == NULL)
682 panic("bp is NULL after malloc in lfs_newbuf");
683 #endif
684 s = splbio();
685 bgetvp(vp, bp);
686 splx(s);
687
688 bp->b_saveaddr = (caddr_t)fs;
689 bp->b_bufsize = size;
690 bp->b_bcount = size;
691 bp->b_lblkno = daddr;
692 bp->b_blkno = daddr;
693 bp->b_error = 0;
694 bp->b_resid = 0;
695 bp->b_iodone = lfs_callback;
696 bp->b_flags |= B_BUSY | B_CALL | B_NOCACHE;
697
698 return (bp);
699 }
700
701 void
702 lfs_freebuf(struct lfs *fs, struct buf *bp)
703 {
704 int s;
705
706 s = splbio();
707 if (bp->b_vp)
708 brelvp(bp);
709 if (!(bp->b_flags & B_INVAL)) { /* B_INVAL indicates a "fake" buffer */
710 lfs_free(fs, bp->b_data, LFS_NB_UNKNOWN);
711 bp->b_data = NULL;
712 }
713 pool_put(&bufpool, bp);
714 splx(s);
715 }
716
717 /*
718 * Definitions for the buffer free lists.
719 */
720 #define BQUEUES 4 /* number of free buffer queues */
721
722 #define BQ_LOCKED 0 /* super-blocks &c */
723 #define BQ_LRU 1 /* lru, useful buffers */
724 #define BQ_AGE 2 /* rubbish */
725 #define BQ_EMPTY 3 /* buffer headers with no memory */
726
727 extern TAILQ_HEAD(bqueues, buf) bufqueues[BQUEUES];
728
729 /*
730 * Return a count of buffers on the "locked" queue.
731 * Don't count malloced buffers, since they don't detract from the total.
732 */
733 void
734 lfs_countlocked(int *count, long *bytes, char *msg)
735 {
736 struct buf *bp;
737 int n = 0;
738 long int size = 0L;
739
740 for (bp = bufqueues[BQ_LOCKED].tqh_first; bp;
741 bp = bp->b_freelist.tqe_next) {
742 if (bp->b_flags & B_CALL)
743 continue;
744 n++;
745 size += bp->b_bufsize;
746 #ifdef DEBUG_LOCKED_LIST
747 if (n > nbuf)
748 panic("lfs_countlocked: this can't happen: more"
749 " buffers locked than exist");
750 #endif
751 }
752 #ifdef DEBUG_LOCKED_LIST
753 /* Theoretically this function never really does anything */
754 if (n != *count)
755 printf("lfs_countlocked: %s: adjusted buf count from %d to %d\n",
756 msg, *count, n);
757 if (size != *bytes)
758 printf("lfs_countlocked: %s: adjusted byte count from %ld to %ld\n",
759 msg, *bytes, size);
760 #endif
761 *count = n;
762 *bytes = size;
763 return;
764 }
765