lfs_bio.c revision 1.58 1 /* $NetBSD: lfs_bio.c,v 1.58 2003/02/17 23:48:17 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.58 2003/02/17 23:48:17 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 while (!lfs_fits(fs, fsb)) {
376 /*
377 * Out of space, need cleaner to run.
378 * Update the cleaner info, then wake it up.
379 * Note the cleanerinfo block is on the ifile
380 * so it CANT_WAIT.
381 */
382 LFS_CLEANERINFO(cip, fs, cbp);
383 LFS_SYNC_CLEANERINFO(cip, fs, cbp, 0);
384
385 printf("lfs_availwait: out of available space, "
386 "waiting on cleaner\n");
387
388 wakeup(&lfs_allclean_wakeup);
389 wakeup(&fs->lfs_nextseg);
390 #ifdef DIAGNOSTIC
391 if (fs->lfs_seglock && fs->lfs_lockpid == curproc->p_pid)
392 panic("lfs_availwait: deadlock");
393 #endif
394 error = tsleep(&fs->lfs_avail, PCATCH | PUSER, "cleaner", 0);
395 if (error)
396 return (error);
397 }
398 return 0;
399 }
400
401 int
402 lfs_bwrite_ext(struct buf *bp, int flags)
403 {
404 struct lfs *fs;
405 struct inode *ip;
406 int fsb, s;
407
408 KASSERT(bp->b_flags & B_BUSY);
409 KASSERT(flags & BW_CLEAN || !LFS_IS_MALLOC_BUF(bp));
410
411 /*
412 * Don't write *any* blocks if we're mounted read-only.
413 * In particular the cleaner can't write blocks either.
414 */
415 if (VTOI(bp->b_vp)->i_lfs->lfs_ronly) {
416 bp->b_flags &= ~(B_DELWRI | B_READ | B_ERROR);
417 LFS_UNLOCK_BUF(bp);
418 if (LFS_IS_MALLOC_BUF(bp))
419 bp->b_flags &= ~B_BUSY;
420 else
421 brelse(bp);
422 return EROFS;
423 }
424
425 /*
426 * Set the delayed write flag and use reassignbuf to move the buffer
427 * from the clean list to the dirty one.
428 *
429 * Set the B_LOCKED flag and unlock the buffer, causing brelse to move
430 * the buffer onto the LOCKED free list. This is necessary, otherwise
431 * getnewbuf() would try to reclaim the buffers using bawrite, which
432 * isn't going to work.
433 *
434 * XXX we don't let meta-data writes run out of space because they can
435 * come from the segment writer. We need to make sure that there is
436 * enough space reserved so that there's room to write meta-data
437 * blocks.
438 */
439 if (!(bp->b_flags & B_LOCKED)) {
440 fs = VFSTOUFS(bp->b_vp->v_mount)->um_lfs;
441 fsb = fragstofsb(fs, numfrags(fs, bp->b_bcount));
442
443 ip = VTOI(bp->b_vp);
444 if (flags & BW_CLEAN) {
445 LFS_SET_UINO(ip, IN_CLEANING);
446 } else {
447 LFS_SET_UINO(ip, IN_MODIFIED);
448 if (bp->b_lblkno >= 0)
449 LFS_SET_UINO(ip, IN_UPDATE);
450 }
451 fs->lfs_avail -= fsb;
452 bp->b_flags |= B_DELWRI;
453
454 LFS_LOCK_BUF(bp);
455 bp->b_flags &= ~(B_READ | B_DONE | B_ERROR);
456 s = splbio();
457 reassignbuf(bp, bp->b_vp);
458 splx(s);
459 }
460
461 if (bp->b_flags & B_CALL)
462 bp->b_flags &= ~B_BUSY;
463 else
464 brelse(bp);
465
466 return (0);
467 }
468
469 void
470 lfs_flush_fs(struct lfs *fs, int flags)
471 {
472 if (fs->lfs_ronly)
473 return;
474
475 /* disallow dirops during flush */
476 fs->lfs_writer++;
477
478 /* drain dirops */
479 while (fs->lfs_dirops > 0) {
480 ++fs->lfs_diropwait;
481 tsleep(&fs->lfs_writer, PRIBIO+1, "fldirop", 0);
482 --fs->lfs_diropwait;
483 }
484
485 if (lfs_dostats)
486 ++lfs_stats.flush_invoked;
487 lfs_segwrite(fs->lfs_ivnode->v_mount, flags);
488
489 /* allow dirops again */
490 if (--fs->lfs_writer == 0)
491 wakeup(&fs->lfs_dirops);
492 }
493
494 /*
495 * XXX
496 * This routine flushes buffers out of the B_LOCKED queue when LFS has too
497 * many locked down. Eventually the pageout daemon will simply call LFS
498 * when pages need to be reclaimed. Note, we have one static count of locked
499 * buffers, so we can't have more than a single file system. To make this
500 * work for multiple file systems, put the count into the mount structure.
501 */
502 void
503 lfs_flush(struct lfs *fs, int flags)
504 {
505 struct mount *mp, *nmp;
506
507 if (lfs_dostats)
508 ++lfs_stats.write_exceeded;
509 if (lfs_writing && flags == 0) {/* XXX flags */
510 #ifdef DEBUG_LFS
511 printf("lfs_flush: not flushing because another flush is active\n");
512 #endif
513 return;
514 }
515 lfs_writing = 1;
516
517 lfs_subsys_pages = 0; /* XXXUBC need a better way to count this */
518 wakeup(&lfs_subsys_pages);
519
520 simple_lock(&mountlist_slock);
521 for (mp = mountlist.cqh_first; mp != (void *)&mountlist; mp = nmp) {
522 if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock)) {
523 nmp = mp->mnt_list.cqe_next;
524 continue;
525 }
526 if (strncmp(&mp->mnt_stat.f_fstypename[0], MOUNT_LFS, MFSNAMELEN) == 0)
527 lfs_flush_fs(((struct ufsmount *)mp->mnt_data)->ufsmount_u.lfs, flags);
528 simple_lock(&mountlist_slock);
529 nmp = mp->mnt_list.cqe_next;
530 vfs_unbusy(mp);
531 }
532 simple_unlock(&mountlist_slock);
533 LFS_DEBUG_COUNTLOCKED("flush");
534
535 lfs_writing = 0;
536 }
537
538 #define INOCOUNT(fs) howmany((fs)->lfs_uinodes, INOPB(fs))
539 #define INOBYTES(fs) ((fs)->lfs_uinodes * DINODE_SIZE)
540
541 int
542 lfs_check(struct vnode *vp, daddr_t blkno, int flags)
543 {
544 int error;
545 struct lfs *fs;
546 struct inode *ip;
547 extern int lfs_dirvcount;
548
549 error = 0;
550 ip = VTOI(vp);
551
552 /* If out of buffers, wait on writer */
553 /* XXX KS - if it's the Ifile, we're probably the cleaner! */
554 if (ip->i_number == LFS_IFILE_INUM)
555 return 0;
556 /* If we're being called from inside a dirop, don't sleep */
557 if (ip->i_flag & IN_ADIROP)
558 return 0;
559
560 fs = ip->i_lfs;
561
562 /*
563 * If we would flush below, but dirops are active, sleep.
564 * Note that a dirop cannot ever reach this code!
565 */
566 while (fs->lfs_dirops > 0 &&
567 (locked_queue_count + INOCOUNT(fs) > LFS_MAX_BUFS ||
568 locked_queue_bytes + INOBYTES(fs) > LFS_MAX_BYTES ||
569 lfs_subsys_pages > LFS_MAX_PAGES ||
570 lfs_dirvcount > LFS_MAX_DIROP || fs->lfs_diropwait > 0))
571 {
572 ++fs->lfs_diropwait;
573 tsleep(&fs->lfs_writer, PRIBIO+1, "bufdirop", 0);
574 --fs->lfs_diropwait;
575 }
576
577 #ifdef DEBUG_LFS_FLUSH
578 if (locked_queue_count + INOCOUNT(fs) > LFS_MAX_BUFS)
579 printf("lqc = %d, max %d\n", locked_queue_count + INOCOUNT(fs),
580 LFS_MAX_BUFS);
581 if (locked_queue_bytes + INOBYTES(fs) > LFS_MAX_BYTES)
582 printf("lqb = %ld, max %d\n", locked_queue_bytes + INOBYTES(fs),
583 LFS_MAX_BYTES);
584 if (lfs_subsys_pages > LFS_MAX_PAGES)
585 printf("lssp = %d, max %d\n", lfs_subsys_pages, LFS_MAX_PAGES);
586 if (lfs_dirvcount > LFS_MAX_DIROP)
587 printf("ldvc = %d, max %d\n", lfs_dirvcount, LFS_MAX_DIROP);
588 if (fs->lfs_diropwait > 0)
589 printf("ldvw = %d\n", fs->lfs_diropwait);
590 #endif
591 if (locked_queue_count + INOCOUNT(fs) > LFS_MAX_BUFS ||
592 locked_queue_bytes + INOBYTES(fs) > LFS_MAX_BYTES ||
593 lfs_subsys_pages > LFS_MAX_PAGES ||
594 lfs_dirvcount > LFS_MAX_DIROP || fs->lfs_diropwait > 0)
595 {
596 lfs_flush(fs, flags);
597 }
598
599 while (locked_queue_count + INOCOUNT(fs) > LFS_WAIT_BUFS ||
600 locked_queue_bytes + INOBYTES(fs) > LFS_WAIT_BYTES ||
601 lfs_subsys_pages > LFS_WAIT_PAGES ||
602 lfs_dirvcount > LFS_MAX_DIROP)
603 {
604 if (lfs_dostats)
605 ++lfs_stats.wait_exceeded;
606 #ifdef DEBUG_LFS
607 printf("lfs_check: waiting: count=%d, bytes=%ld\n",
608 locked_queue_count, locked_queue_bytes);
609 #endif
610 error = tsleep(&locked_queue_count, PCATCH | PUSER,
611 "buffers", hz * LFS_BUFWAIT);
612 if (error != EWOULDBLOCK)
613 break;
614 /*
615 * lfs_flush might not flush all the buffers, if some of the
616 * inodes were locked or if most of them were Ifile blocks
617 * and we weren't asked to checkpoint. Try flushing again
618 * to keep us from blocking indefinitely.
619 */
620 if (locked_queue_count + INOCOUNT(fs) > LFS_MAX_BUFS ||
621 locked_queue_bytes + INOBYTES(fs) > LFS_MAX_BYTES)
622 {
623 lfs_flush(fs, flags | SEGM_CKP);
624 }
625 }
626 return (error);
627 }
628
629 /*
630 * Allocate a new buffer header.
631 */
632 struct buf *
633 lfs_newbuf(struct lfs *fs, struct vnode *vp, daddr_t daddr, size_t size, int type)
634 {
635 struct buf *bp;
636 size_t nbytes;
637 int s;
638
639 nbytes = roundup(size, fsbtob(fs, 1));
640
641 s = splbio();
642 bp = pool_get(&bufpool, PR_WAITOK);
643 splx(s);
644 memset(bp, 0, sizeof(struct buf));
645 if (nbytes) {
646 bp->b_data = lfs_malloc(fs, nbytes, type);
647 /* memset(bp->b_data, 0, nbytes); */
648 }
649 #ifdef DIAGNOSTIC
650 if (vp == NULL)
651 panic("vp is NULL in lfs_newbuf");
652 if (bp == NULL)
653 panic("bp is NULL after malloc in lfs_newbuf");
654 #endif
655 simple_lock_init(&bp->b_interlock);
656 s = splbio();
657 bgetvp(vp, bp);
658 splx(s);
659
660 bp->b_saveaddr = (caddr_t)fs;
661 bp->b_bufsize = size;
662 bp->b_bcount = size;
663 bp->b_lblkno = daddr;
664 bp->b_blkno = daddr;
665 bp->b_error = 0;
666 bp->b_resid = 0;
667 bp->b_iodone = lfs_callback;
668 bp->b_flags |= B_BUSY | B_CALL | B_NOCACHE;
669
670 return (bp);
671 }
672
673 void
674 lfs_freebuf(struct lfs *fs, struct buf *bp)
675 {
676 int s;
677
678 s = splbio();
679 if (bp->b_vp)
680 brelvp(bp);
681 if (!(bp->b_flags & B_INVAL)) { /* B_INVAL indicates a "fake" buffer */
682 lfs_free(fs, bp->b_data, LFS_NB_UNKNOWN);
683 bp->b_data = NULL;
684 }
685 pool_put(&bufpool, bp);
686 splx(s);
687 }
688
689 /*
690 * Definitions for the buffer free lists.
691 */
692 #define BQUEUES 4 /* number of free buffer queues */
693
694 #define BQ_LOCKED 0 /* super-blocks &c */
695 #define BQ_LRU 1 /* lru, useful buffers */
696 #define BQ_AGE 2 /* rubbish */
697 #define BQ_EMPTY 3 /* buffer headers with no memory */
698
699 extern TAILQ_HEAD(bqueues, buf) bufqueues[BQUEUES];
700
701 /*
702 * Return a count of buffers on the "locked" queue.
703 * Don't count malloced buffers, since they don't detract from the total.
704 */
705 void
706 lfs_countlocked(int *count, long *bytes, char *msg)
707 {
708 struct buf *bp;
709 int n = 0;
710 long int size = 0L;
711
712 for (bp = bufqueues[BQ_LOCKED].tqh_first; bp;
713 bp = bp->b_freelist.tqe_next) {
714 if (bp->b_flags & B_CALL)
715 continue;
716 n++;
717 size += bp->b_bufsize;
718 #ifdef DEBUG_LOCKED_LIST
719 if (n > nbuf)
720 panic("lfs_countlocked: this can't happen: more"
721 " buffers locked than exist");
722 #endif
723 }
724 #ifdef DEBUG_LOCKED_LIST
725 /* Theoretically this function never really does anything */
726 if (n != *count)
727 printf("lfs_countlocked: %s: adjusted buf count from %d to %d\n",
728 msg, *count, n);
729 if (size != *bytes)
730 printf("lfs_countlocked: %s: adjusted byte count from %ld to %ld\n",
731 msg, *bytes, size);
732 #endif
733 *count = n;
734 *bytes = size;
735 return;
736 }
737