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