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