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