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