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