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