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