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