vfs_bio.c revision 1.43 1 /* $NetBSD: vfs_bio.c,v 1.43 1996/04/22 01:38:59 christos Exp $ */
2
3 /*-
4 * Copyright (c) 1994 Christopher G. Demetriou
5 * Copyright (c) 1982, 1986, 1989, 1993
6 * The Regents of the University of California. All rights reserved.
7 * (c) UNIX System Laboratories, Inc.
8 * All or some portions of this file are derived from material licensed
9 * to the University of California by American Telephone and Telegraph
10 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
11 * the permission of UNIX System Laboratories, Inc.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. All advertising materials mentioning features or use of this software
22 * must display the following acknowledgement:
23 * This product includes software developed by the University of
24 * California, Berkeley and its contributors.
25 * 4. Neither the name of the University nor the names of its contributors
26 * may be used to endorse or promote products derived from this software
27 * without specific prior written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 * SUCH DAMAGE.
40 *
41 * @(#)vfs_bio.c 8.6 (Berkeley) 1/11/94
42 */
43
44 /*
45 * Some references:
46 * Bach: The Design of the UNIX Operating System (Prentice Hall, 1986)
47 * Leffler, et al.: The Design and Implementation of the 4.3BSD
48 * UNIX Operating System (Addison Welley, 1989)
49 */
50
51 #include <sys/param.h>
52 #include <sys/systm.h>
53 #include <sys/proc.h>
54 #include <sys/buf.h>
55 #include <sys/vnode.h>
56 #include <sys/mount.h>
57 #include <sys/trace.h>
58 #include <sys/malloc.h>
59 #include <sys/resourcevar.h>
60 #include <sys/conf.h>
61
62 #include <vm/vm.h>
63
64 /* Macros to clear/set/test flags. */
65 #define SET(t, f) (t) |= (f)
66 #define CLR(t, f) (t) &= ~(f)
67 #define ISSET(t, f) ((t) & (f))
68
69 /*
70 * Definitions for the buffer hash lists.
71 */
72 #define BUFHASH(dvp, lbn) \
73 (&bufhashtbl[((long)(dvp) / sizeof(*(dvp)) + (int)(lbn)) & bufhash])
74 LIST_HEAD(bufhashhdr, buf) *bufhashtbl, invalhash;
75 u_long bufhash;
76
77 /*
78 * Insq/Remq for the buffer hash lists.
79 */
80 #define binshash(bp, dp) LIST_INSERT_HEAD(dp, bp, b_hash)
81 #define bremhash(bp) LIST_REMOVE(bp, b_hash)
82
83 /*
84 * Definitions for the buffer free lists.
85 */
86 #define BQUEUES 4 /* number of free buffer queues */
87
88 #define BQ_LOCKED 0 /* super-blocks &c */
89 #define BQ_LRU 1 /* lru, useful buffers */
90 #define BQ_AGE 2 /* rubbish */
91 #define BQ_EMPTY 3 /* buffer headers with no memory */
92
93 TAILQ_HEAD(bqueues, buf) bufqueues[BQUEUES];
94 int needbuffer;
95
96 /*
97 * Insq/Remq for the buffer free lists.
98 */
99 #define binsheadfree(bp, dp) TAILQ_INSERT_HEAD(dp, bp, b_freelist)
100 #define binstailfree(bp, dp) TAILQ_INSERT_TAIL(dp, bp, b_freelist)
101
102 static __inline struct buf *bio_doread __P((struct vnode *, daddr_t, int,
103 struct ucred *, int));
104 int count_lock_queue __P((void));
105
106 void
107 bremfree(bp)
108 struct buf *bp;
109 {
110 struct bqueues *dp = NULL;
111
112 /*
113 * We only calculate the head of the freelist when removing
114 * the last element of the list as that is the only time that
115 * it is needed (e.g. to reset the tail pointer).
116 *
117 * NB: This makes an assumption about how tailq's are implemented.
118 */
119 if (bp->b_freelist.tqe_next == NULL) {
120 for (dp = bufqueues; dp < &bufqueues[BQUEUES]; dp++)
121 if (dp->tqh_last == &bp->b_freelist.tqe_next)
122 break;
123 if (dp == &bufqueues[BQUEUES])
124 panic("bremfree: lost tail");
125 }
126 TAILQ_REMOVE(dp, bp, b_freelist);
127 }
128
129 /*
130 * Initialize buffers and hash links for buffers.
131 */
132 void
133 bufinit()
134 {
135 register struct buf *bp;
136 struct bqueues *dp;
137 register int i;
138 int base, residual;
139
140 for (dp = bufqueues; dp < &bufqueues[BQUEUES]; dp++)
141 TAILQ_INIT(dp);
142 bufhashtbl = hashinit(nbuf, M_CACHE, &bufhash);
143 base = bufpages / nbuf;
144 residual = bufpages % nbuf;
145 for (i = 0; i < nbuf; i++) {
146 bp = &buf[i];
147 bzero((char *)bp, sizeof *bp);
148 bp->b_dev = NODEV;
149 bp->b_rcred = NOCRED;
150 bp->b_wcred = NOCRED;
151 bp->b_vnbufs.le_next = NOLIST;
152 bp->b_data = buffers + i * MAXBSIZE;
153 if (i < residual)
154 bp->b_bufsize = (base + 1) * CLBYTES;
155 else
156 bp->b_bufsize = base * CLBYTES;
157 bp->b_flags = B_INVAL;
158 dp = bp->b_bufsize ? &bufqueues[BQ_AGE] : &bufqueues[BQ_EMPTY];
159 binsheadfree(bp, dp);
160 binshash(bp, &invalhash);
161 }
162 }
163
164 static __inline struct buf *
165 bio_doread(vp, blkno, size, cred, async)
166 struct vnode *vp;
167 daddr_t blkno;
168 int size;
169 struct ucred *cred;
170 int async;
171 {
172 register struct buf *bp;
173
174 bp = getblk(vp, blkno, size, 0, 0);
175
176 /*
177 * If buffer does not have data valid, start a read.
178 * Note that if buffer is B_INVAL, getblk() won't return it.
179 * Therefore, it's valid if it's I/O has completed or been delayed.
180 */
181 if (!ISSET(bp->b_flags, (B_DONE | B_DELWRI))) {
182 /* Start I/O for the buffer (keeping credentials). */
183 SET(bp->b_flags, B_READ | async);
184 if (cred != NOCRED && bp->b_rcred == NOCRED) {
185 crhold(cred);
186 bp->b_rcred = cred;
187 }
188 VOP_STRATEGY(bp);
189
190 /* Pay for the read. */
191 curproc->p_stats->p_ru.ru_inblock++; /* XXX */
192 } else if (async) {
193 brelse(bp);
194 }
195
196 return (bp);
197 }
198
199 /*
200 * Read a disk block.
201 * This algorithm described in Bach (p.54).
202 */
203 int
204 bread(vp, blkno, size, cred, bpp)
205 struct vnode *vp;
206 daddr_t blkno;
207 int size;
208 struct ucred *cred;
209 struct buf **bpp;
210 {
211 register struct buf *bp;
212
213 /* Get buffer for block. */
214 bp = *bpp = bio_doread(vp, blkno, size, cred, 0);
215
216 /* Wait for the read to complete, and return result. */
217 return (biowait(bp));
218 }
219
220 /*
221 * Read-ahead multiple disk blocks. The first is sync, the rest async.
222 * Trivial modification to the breada algorithm presented in Bach (p.55).
223 */
224 int
225 breadn(vp, blkno, size, rablks, rasizes, nrablks, cred, bpp)
226 struct vnode *vp;
227 daddr_t blkno; int size;
228 daddr_t rablks[]; int rasizes[];
229 int nrablks;
230 struct ucred *cred;
231 struct buf **bpp;
232 {
233 register struct buf *bp;
234 int i;
235
236 bp = *bpp = bio_doread(vp, blkno, size, cred, 0);
237
238 /*
239 * For each of the read-ahead blocks, start a read, if necessary.
240 */
241 for (i = 0; i < nrablks; i++) {
242 /* If it's in the cache, just go on to next one. */
243 if (incore(vp, rablks[i]))
244 continue;
245
246 /* Get a buffer for the read-ahead block */
247 (void) bio_doread(vp, rablks[i], rasizes[i], cred, B_ASYNC);
248 }
249
250 /* Otherwise, we had to start a read for it; wait until it's valid. */
251 return (biowait(bp));
252 }
253
254 /*
255 * Read with single-block read-ahead. Defined in Bach (p.55), but
256 * implemented as a call to breadn().
257 * XXX for compatibility with old file systems.
258 */
259 int
260 breada(vp, blkno, size, rablkno, rabsize, cred, bpp)
261 struct vnode *vp;
262 daddr_t blkno; int size;
263 daddr_t rablkno; int rabsize;
264 struct ucred *cred;
265 struct buf **bpp;
266 {
267
268 return (breadn(vp, blkno, size, &rablkno, &rabsize, 1, cred, bpp));
269 }
270
271 /*
272 * Block write. Described in Bach (p.56)
273 */
274 int
275 bwrite(bp)
276 struct buf *bp;
277 {
278 int rv, sync, wasdelayed;
279
280 /*
281 * Remember buffer type, to switch on it later. If the write was
282 * synchronous, but the file system was mounted with MNT_ASYNC,
283 * convert it to a delayed write.
284 * XXX note that this relies on delayed tape writes being converted
285 * to async, not sync writes (which is safe, but ugly).
286 */
287 sync = !ISSET(bp->b_flags, B_ASYNC);
288 if (sync && bp->b_vp && bp->b_vp->v_mount &&
289 ISSET(bp->b_vp->v_mount->mnt_flag, MNT_ASYNC)) {
290 bdwrite(bp);
291 return (0);
292 }
293 wasdelayed = ISSET(bp->b_flags, B_DELWRI);
294 CLR(bp->b_flags, (B_READ | B_DONE | B_ERROR | B_DELWRI));
295
296 if (!sync) {
297 /*
298 * If not synchronous, pay for the I/O operation and make
299 * sure the buf is on the correct vnode queue. We have
300 * to do this now, because if we don't, the vnode may not
301 * be properly notified that its I/O has completed.
302 */
303 if (wasdelayed)
304 reassignbuf(bp, bp->b_vp);
305 else
306 curproc->p_stats->p_ru.ru_oublock++;
307 }
308
309 /* Initiate disk write. Make sure the appropriate party is charged. */
310 SET(bp->b_flags, B_WRITEINPROG);
311 bp->b_vp->v_numoutput++;
312 VOP_STRATEGY(bp);
313
314 if (sync) {
315 /*
316 * If I/O was synchronous, wait for it to complete.
317 */
318 rv = biowait(bp);
319
320 /*
321 * Pay for the I/O operation, if it's not been paid for, and
322 * make sure it's on the correct vnode queue. (async operatings
323 * were payed for above.)
324 */
325 if (wasdelayed)
326 reassignbuf(bp, bp->b_vp);
327 else
328 curproc->p_stats->p_ru.ru_oublock++;
329
330 /* Release the buffer. */
331 brelse(bp);
332
333 return (rv);
334 } else {
335 return (0);
336 }
337 }
338
339 int
340 vn_bwrite(v)
341 void *v;
342 {
343 struct vop_bwrite_args *ap = v;
344
345 return (bwrite(ap->a_bp));
346 }
347
348 /*
349 * Delayed write.
350 *
351 * The buffer is marked dirty, but is not queued for I/O.
352 * This routine should be used when the buffer is expected
353 * to be modified again soon, typically a small write that
354 * partially fills a buffer.
355 *
356 * NB: magnetic tapes cannot be delayed; they must be
357 * written in the order that the writes are requested.
358 *
359 * Described in Leffler, et al. (pp. 208-213).
360 */
361 void
362 bdwrite(bp)
363 struct buf *bp;
364 {
365
366 /*
367 * If the block hasn't been seen before:
368 * (1) Mark it as having been seen,
369 * (2) Charge for the write.
370 * (3) Make sure it's on its vnode's correct block list,
371 */
372 if (!ISSET(bp->b_flags, B_DELWRI)) {
373 SET(bp->b_flags, B_DELWRI);
374 curproc->p_stats->p_ru.ru_oublock++; /* XXX */
375 reassignbuf(bp, bp->b_vp);
376 }
377
378 /* If this is a tape block, write the block now. */
379 if (bdevsw[major(bp->b_dev)].d_type == D_TAPE) {
380 bawrite(bp);
381 return;
382 }
383
384 /* Otherwise, the "write" is done, so mark and release the buffer. */
385 CLR(bp->b_flags, B_NEEDCOMMIT);
386 SET(bp->b_flags, B_DONE);
387 brelse(bp);
388 }
389
390 /*
391 * Asynchronous block write; just an asynchronous bwrite().
392 */
393 void
394 bawrite(bp)
395 struct buf *bp;
396 {
397
398 SET(bp->b_flags, B_ASYNC);
399 VOP_BWRITE(bp);
400 }
401
402 /*
403 * Release a buffer on to the free lists.
404 * Described in Bach (p. 46).
405 */
406 void
407 brelse(bp)
408 struct buf *bp;
409 {
410 struct bqueues *bufq;
411 int s;
412
413 /* Wake up any processes waiting for any buffer to become free. */
414 if (needbuffer) {
415 needbuffer = 0;
416 wakeup(&needbuffer);
417 }
418
419 /* Wake up any proceeses waiting for _this_ buffer to become free. */
420 if (ISSET(bp->b_flags, B_WANTED)) {
421 CLR(bp->b_flags, B_WANTED);
422 wakeup(bp);
423 }
424
425 /* Block disk interrupts. */
426 s = splbio();
427
428 /*
429 * Determine which queue the buffer should be on, then put it there.
430 */
431
432 /* If it's locked, don't report an error; try again later. */
433 if (ISSET(bp->b_flags, (B_LOCKED|B_ERROR)) == (B_LOCKED|B_ERROR))
434 CLR(bp->b_flags, B_ERROR);
435
436 /* If it's not cacheable, or an error, mark it invalid. */
437 if (ISSET(bp->b_flags, (B_NOCACHE|B_ERROR)))
438 SET(bp->b_flags, B_INVAL);
439
440 if ((bp->b_bufsize <= 0) || ISSET(bp->b_flags, B_INVAL)) {
441 /*
442 * If it's invalid or empty, dissociate it from its vnode
443 * and put on the head of the appropriate queue.
444 */
445 if (bp->b_vp)
446 brelvp(bp);
447 CLR(bp->b_flags, B_DELWRI);
448 if (bp->b_bufsize <= 0)
449 /* no data */
450 bufq = &bufqueues[BQ_EMPTY];
451 else
452 /* invalid data */
453 bufq = &bufqueues[BQ_AGE];
454 binsheadfree(bp, bufq);
455 } else {
456 /*
457 * It has valid data. Put it on the end of the appropriate
458 * queue, so that it'll stick around for as long as possible.
459 */
460 if (ISSET(bp->b_flags, B_LOCKED))
461 /* locked in core */
462 bufq = &bufqueues[BQ_LOCKED];
463 else if (ISSET(bp->b_flags, B_AGE))
464 /* stale but valid data */
465 bufq = &bufqueues[BQ_AGE];
466 else
467 /* valid data */
468 bufq = &bufqueues[BQ_LRU];
469 binstailfree(bp, bufq);
470 }
471
472 /* Unlock the buffer. */
473 CLR(bp->b_flags, (B_AGE | B_ASYNC | B_BUSY | B_NOCACHE));
474
475 /* Allow disk interrupts. */
476 splx(s);
477 }
478
479 /*
480 * Determine if a block is in the cache.
481 * Just look on what would be its hash chain. If it's there, return
482 * a pointer to it, unless it's marked invalid. If it's marked invalid,
483 * we normally don't return the buffer, unless the caller explicitly
484 * wants us to.
485 */
486 struct buf *
487 incore(vp, blkno)
488 struct vnode *vp;
489 daddr_t blkno;
490 {
491 struct buf *bp;
492
493 bp = BUFHASH(vp, blkno)->lh_first;
494
495 /* Search hash chain */
496 for (; bp != NULL; bp = bp->b_hash.le_next) {
497 if (bp->b_lblkno == blkno && bp->b_vp == vp &&
498 !ISSET(bp->b_flags, B_INVAL))
499 return (bp);
500 }
501
502 return (0);
503 }
504
505 /*
506 * Get a block of requested size that is associated with
507 * a given vnode and block offset. If it is found in the
508 * block cache, mark it as having been found, make it busy
509 * and return it. Otherwise, return an empty block of the
510 * correct size. It is up to the caller to insure that the
511 * cached blocks be of the correct size.
512 */
513 struct buf *
514 getblk(vp, blkno, size, slpflag, slptimeo)
515 register struct vnode *vp;
516 daddr_t blkno;
517 int size, slpflag, slptimeo;
518 {
519 struct bufhashhdr *bh;
520 struct buf *bp;
521 int s, err;
522
523 /*
524 * XXX
525 * The following is an inlined version of 'incore()', but with
526 * the 'invalid' test moved to after the 'busy' test. It's
527 * necessary because there are some cases in which the NFS
528 * code sets B_INVAL prior to writing data to the server, but
529 * in which the buffers actually contain valid data. In this
530 * case, we can't allow the system to allocate a new buffer for
531 * the block until the write is finished.
532 */
533 bh = BUFHASH(vp, blkno);
534 start:
535 bp = bh->lh_first;
536 for (; bp != NULL; bp = bp->b_hash.le_next) {
537 if (bp->b_lblkno != blkno || bp->b_vp != vp)
538 continue;
539
540 s = splbio();
541 if (ISSET(bp->b_flags, B_BUSY)) {
542 SET(bp->b_flags, B_WANTED);
543 err = tsleep(bp, slpflag | (PRIBIO + 1), "getblk",
544 slptimeo);
545 splx(s);
546 if (err)
547 return (NULL);
548 goto start;
549 }
550
551 if (!ISSET(bp->b_flags, B_INVAL)) {
552 SET(bp->b_flags, (B_BUSY | B_CACHE));
553 bremfree(bp);
554 splx(s);
555 break;
556 }
557 splx(s);
558 }
559
560 if (bp == NULL) {
561 if ((bp = getnewbuf(slpflag, slptimeo)) == NULL)
562 goto start;
563 binshash(bp, bh);
564 bp->b_blkno = bp->b_lblkno = blkno;
565 s = splbio();
566 bgetvp(vp, bp);
567 splx(s);
568 }
569 allocbuf(bp, size);
570 return (bp);
571 }
572
573 /*
574 * Get an empty, disassociated buffer of given size.
575 */
576 struct buf *
577 geteblk(size)
578 int size;
579 {
580 struct buf *bp;
581
582 while ((bp = getnewbuf(0, 0)) == 0)
583 ;
584 SET(bp->b_flags, B_INVAL);
585 binshash(bp, &invalhash);
586 allocbuf(bp, size);
587
588 return (bp);
589 }
590
591 /*
592 * Expand or contract the actual memory allocated to a buffer.
593 *
594 * If the buffer shrinks, data is lost, so it's up to the
595 * caller to have written it out *first*; this routine will not
596 * start a write. If the buffer grows, it's the callers
597 * responsibility to fill out the buffer's additional contents.
598 */
599 void
600 allocbuf(bp, size)
601 struct buf *bp;
602 int size;
603 {
604 struct buf *nbp;
605 vm_size_t desired_size;
606 int s;
607
608 desired_size = roundup(size, CLBYTES);
609 if (desired_size > MAXBSIZE)
610 panic("allocbuf: buffer larger than MAXBSIZE requested");
611
612 if (bp->b_bufsize == desired_size)
613 goto out;
614
615 /*
616 * If the buffer is smaller than the desired size, we need to snarf
617 * it from other buffers. Get buffers (via getnewbuf()), and
618 * steal their pages.
619 */
620 while (bp->b_bufsize < desired_size) {
621 int amt;
622
623 /* find a buffer */
624 while ((nbp = getnewbuf(0, 0)) == NULL)
625 ;
626 SET(nbp->b_flags, B_INVAL);
627 binshash(nbp, &invalhash);
628
629 /* and steal its pages, up to the amount we need */
630 amt = min(nbp->b_bufsize, (desired_size - bp->b_bufsize));
631 pagemove((nbp->b_data + nbp->b_bufsize - amt),
632 bp->b_data + bp->b_bufsize, amt);
633 bp->b_bufsize += amt;
634 nbp->b_bufsize -= amt;
635
636 /* reduce transfer count if we stole some data */
637 if (nbp->b_bcount > nbp->b_bufsize)
638 nbp->b_bcount = nbp->b_bufsize;
639
640 #ifdef DIAGNOSTIC
641 if (nbp->b_bufsize < 0)
642 panic("allocbuf: negative bufsize");
643 #endif
644
645 brelse(nbp);
646 }
647
648 /*
649 * If we want a buffer smaller than the current size,
650 * shrink this buffer. Grab a buf head from the EMPTY queue,
651 * move a page onto it, and put it on front of the AGE queue.
652 * If there are no free buffer headers, leave the buffer alone.
653 */
654 if (bp->b_bufsize > desired_size) {
655 s = splbio();
656 if ((nbp = bufqueues[BQ_EMPTY].tqh_first) == NULL) {
657 /* No free buffer head */
658 splx(s);
659 goto out;
660 }
661 bremfree(nbp);
662 SET(nbp->b_flags, B_BUSY);
663 splx(s);
664
665 /* move the page to it and note this change */
666 pagemove(bp->b_data + desired_size,
667 nbp->b_data, bp->b_bufsize - desired_size);
668 nbp->b_bufsize = bp->b_bufsize - desired_size;
669 bp->b_bufsize = desired_size;
670 nbp->b_bcount = 0;
671 SET(nbp->b_flags, B_INVAL);
672
673 /* release the newly-filled buffer and leave */
674 brelse(nbp);
675 }
676
677 out:
678 bp->b_bcount = size;
679 }
680
681 /*
682 * Find a buffer which is available for use.
683 * Select something from a free list.
684 * Preference is to AGE list, then LRU list.
685 */
686 struct buf *
687 getnewbuf(slpflag, slptimeo)
688 int slpflag, slptimeo;
689 {
690 register struct buf *bp;
691 int s;
692
693 start:
694 s = splbio();
695 if ((bp = bufqueues[BQ_AGE].tqh_first) != NULL ||
696 (bp = bufqueues[BQ_LRU].tqh_first) != NULL) {
697 bremfree(bp);
698 } else {
699 /* wait for a free buffer of any kind */
700 needbuffer = 1;
701 tsleep(&needbuffer, slpflag|(PRIBIO+1), "getnewbuf", slptimeo);
702 splx(s);
703 return (0);
704 }
705
706 /* Buffer is no longer on free lists. */
707 SET(bp->b_flags, B_BUSY);
708
709 /* If buffer was a delayed write, start it, and go back to the top. */
710 if (ISSET(bp->b_flags, B_DELWRI)) {
711 splx(s);
712 bawrite (bp);
713 goto start;
714 }
715
716 /* disassociate us from our vnode, if we had one... */
717 if (bp->b_vp)
718 brelvp(bp);
719 splx(s);
720
721 /* clear out various other fields */
722 bp->b_flags = B_BUSY;
723 bp->b_dev = NODEV;
724 bp->b_blkno = bp->b_lblkno = 0;
725 bp->b_iodone = 0;
726 bp->b_error = 0;
727 bp->b_resid = 0;
728 bp->b_bcount = 0;
729 bp->b_dirtyoff = bp->b_dirtyend = 0;
730 bp->b_validoff = bp->b_validend = 0;
731
732 /* nuke any credentials we were holding */
733 if (bp->b_rcred != NOCRED) {
734 crfree(bp->b_rcred);
735 bp->b_rcred = NOCRED;
736 }
737 if (bp->b_wcred != NOCRED) {
738 crfree(bp->b_wcred);
739 bp->b_wcred = NOCRED;
740 }
741
742 bremhash(bp);
743 return (bp);
744 }
745
746 /*
747 * Wait for operations on the buffer to complete.
748 * When they do, extract and return the I/O's error value.
749 */
750 int
751 biowait(bp)
752 struct buf *bp;
753 {
754 int s;
755
756 s = splbio();
757 while (!ISSET(bp->b_flags, B_DONE))
758 tsleep(bp, PRIBIO + 1, "biowait", 0);
759 splx(s);
760
761 /* check for interruption of I/O (e.g. via NFS), then errors. */
762 if (ISSET(bp->b_flags, B_EINTR)) {
763 CLR(bp->b_flags, B_EINTR);
764 return (EINTR);
765 } else if (ISSET(bp->b_flags, B_ERROR))
766 return (bp->b_error ? bp->b_error : EIO);
767 else
768 return (0);
769 }
770
771 /*
772 * Mark I/O complete on a buffer.
773 *
774 * If a callback has been requested, e.g. the pageout
775 * daemon, do so. Otherwise, awaken waiting processes.
776 *
777 * [ Leffler, et al., says on p.247:
778 * "This routine wakes up the blocked process, frees the buffer
779 * for an asynchronous write, or, for a request by the pagedaemon
780 * process, invokes a procedure specified in the buffer structure" ]
781 *
782 * In real life, the pagedaemon (or other system processes) wants
783 * to do async stuff to, and doesn't want the buffer brelse()'d.
784 * (for swap pager, that puts swap buffers on the free lists (!!!),
785 * for the vn device, that puts malloc'd buffers on the free lists!)
786 */
787 void
788 biodone(bp)
789 struct buf *bp;
790 {
791 if (ISSET(bp->b_flags, B_DONE))
792 panic("biodone already");
793 SET(bp->b_flags, B_DONE); /* note that it's done */
794
795 if (!ISSET(bp->b_flags, B_READ)) /* wake up reader */
796 vwakeup(bp);
797
798 if (ISSET(bp->b_flags, B_CALL)) { /* if necessary, call out */
799 CLR(bp->b_flags, B_CALL); /* but note callout done */
800 (*bp->b_iodone)(bp);
801 } else if (ISSET(bp->b_flags, B_ASYNC)) /* if async, release it */
802 brelse(bp);
803 else { /* or just wakeup the buffer */
804 CLR(bp->b_flags, B_WANTED);
805 wakeup(bp);
806 }
807 }
808
809 /*
810 * Return a count of buffers on the "locked" queue.
811 */
812 int
813 count_lock_queue()
814 {
815 register struct buf *bp;
816 register int n = 0;
817
818 for (bp = bufqueues[BQ_LOCKED].tqh_first; bp;
819 bp = bp->b_freelist.tqe_next)
820 n++;
821 return (n);
822 }
823
824 #ifdef DEBUG
825 /*
826 * Print out statistics on the current allocation of the buffer pool.
827 * Can be enabled to print out on every ``sync'' by setting "syncprt"
828 * in vfs_syscalls.c using sysctl.
829 */
830 void
831 vfs_bufstats()
832 {
833 int s, i, j, count;
834 register struct buf *bp;
835 register struct bqueues *dp;
836 int counts[MAXBSIZE/CLBYTES+1];
837 static char *bname[BQUEUES] = { "LOCKED", "LRU", "AGE", "EMPTY" };
838
839 for (dp = bufqueues, i = 0; dp < &bufqueues[BQUEUES]; dp++, i++) {
840 count = 0;
841 for (j = 0; j <= MAXBSIZE/CLBYTES; j++)
842 counts[j] = 0;
843 s = splbio();
844 for (bp = dp->tqh_first; bp; bp = bp->b_freelist.tqe_next) {
845 counts[bp->b_bufsize/CLBYTES]++;
846 count++;
847 }
848 splx(s);
849 printf("%s: total-%d", bname[i], count);
850 for (j = 0; j <= MAXBSIZE/CLBYTES; j++)
851 if (counts[j] != 0)
852 printf(", %d-%d", j * CLBYTES, counts[j]);
853 printf("\n");
854 }
855 }
856 #endif /* DEBUG */
857