vfs_bio.c revision 1.108 1 /* $NetBSD: vfs_bio.c,v 1.108 2004/01/10 14:39:50 yamt Exp $ */
2
3 /*-
4 * Copyright (c) 1982, 1986, 1989, 1993
5 * The Regents of the University of California. All rights reserved.
6 * (c) UNIX System Laboratories, Inc.
7 * All or some portions of this file are derived from material licensed
8 * to the University of California by American Telephone and Telegraph
9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10 * the permission of UNIX System Laboratories, Inc.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 * @(#)vfs_bio.c 8.6 (Berkeley) 1/11/94
37 */
38
39 /*-
40 * Copyright (c) 1994 Christopher G. Demetriou
41 *
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
44 * are met:
45 * 1. Redistributions of source code must retain the above copyright
46 * notice, this list of conditions and the following disclaimer.
47 * 2. Redistributions in binary form must reproduce the above copyright
48 * notice, this list of conditions and the following disclaimer in the
49 * documentation and/or other materials provided with the distribution.
50 * 3. All advertising materials mentioning features or use of this software
51 * must display the following acknowledgement:
52 * This product includes software developed by the University of
53 * California, Berkeley and its contributors.
54 * 4. Neither the name of the University nor the names of its contributors
55 * may be used to endorse or promote products derived from this software
56 * without specific prior written permission.
57 *
58 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
59 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
62 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
63 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
64 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
66 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
67 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68 * SUCH DAMAGE.
69 *
70 * @(#)vfs_bio.c 8.6 (Berkeley) 1/11/94
71 */
72
73 /*
74 * Some references:
75 * Bach: The Design of the UNIX Operating System (Prentice Hall, 1986)
76 * Leffler, et al.: The Design and Implementation of the 4.3BSD
77 * UNIX Operating System (Addison Welley, 1989)
78 */
79
80 #include "opt_bufcache.h"
81 #include "opt_softdep.h"
82
83 #include <sys/cdefs.h>
84 __KERNEL_RCSID(0, "$NetBSD: vfs_bio.c,v 1.108 2004/01/10 14:39:50 yamt Exp $");
85
86 #include <sys/param.h>
87 #include <sys/systm.h>
88 #include <sys/kernel.h>
89 #include <sys/proc.h>
90 #include <sys/buf.h>
91 #include <sys/vnode.h>
92 #include <sys/mount.h>
93 #include <sys/malloc.h>
94 #include <sys/resourcevar.h>
95 #include <sys/sysctl.h>
96 #include <sys/conf.h>
97
98 #include <uvm/uvm.h>
99
100 #include <miscfs/specfs/specdev.h>
101
102 #ifndef BUFPAGES
103 # define BUFPAGES 0
104 #endif
105
106 #ifdef BUFCACHE
107 # if (BUFCACHE < 5) || (BUFCACHE > 95)
108 # error BUFCACHE is not between 5 and 95
109 # endif
110 #else
111 # define BUFCACHE 20
112 #endif
113
114 u_int nbuf; /* XXX - for softdep_lockedbufs */
115 u_int bufpages = BUFPAGES; /* optional hardwired count */
116 u_int bufcache = BUFCACHE; /* max % of RAM to use for buffer cache */
117
118
119 /* Macros to clear/set/test flags. */
120 #define SET(t, f) (t) |= (f)
121 #define CLR(t, f) (t) &= ~(f)
122 #define ISSET(t, f) ((t) & (f))
123
124 /*
125 * Definitions for the buffer hash lists.
126 */
127 #define BUFHASH(dvp, lbn) \
128 (&bufhashtbl[(((long)(dvp) >> 8) + (int)(lbn)) & bufhash])
129 LIST_HEAD(bufhashhdr, buf) *bufhashtbl, invalhash;
130 u_long bufhash;
131 #ifndef SOFTDEP
132 struct bio_ops bioops; /* I/O operation notification */
133 #endif
134
135 /*
136 * Insq/Remq for the buffer hash lists.
137 */
138 #define binshash(bp, dp) LIST_INSERT_HEAD(dp, bp, b_hash)
139 #define bremhash(bp) LIST_REMOVE(bp, b_hash)
140
141 /*
142 * Definitions for the buffer free lists.
143 */
144 #define BQUEUES 3 /* number of free buffer queues */
145
146 #define BQ_LOCKED 0 /* super-blocks &c */
147 #define BQ_LRU 1 /* lru, useful buffers */
148 #define BQ_AGE 2 /* rubbish */
149
150 TAILQ_HEAD(bqueues, buf) bufqueues[BQUEUES];
151 int needbuffer;
152
153 /*
154 * Buffer queue lock.
155 * Take this lock first if also taking some buffer's b_interlock.
156 */
157 struct simplelock bqueue_slock = SIMPLELOCK_INITIALIZER;
158
159 /*
160 * Buffer pool for I/O buffers.
161 */
162 struct pool bufpool;
163
164 /* XXX - somewhat gross.. */
165 #if MAXBSIZE == 0x2000
166 #define NMEMPOOLS 4
167 #elif MAXBSIZE == 0x4000
168 #define NMEMPOOLS 5
169 #elif MAXBSIZE == 0x8000
170 #define NMEMPOOLS 6
171 #else
172 #define NMEMPOOLS 7
173 #endif
174
175 #define MEMPOOL_INDEX_OFFSET 10 /* smallest pool is 1k */
176 #if (1 << (NMEMPOOLS + MEMPOOL_INDEX_OFFSET - 1)) != MAXBSIZE
177 #error update vfs_bio buffer memory parameters
178 #endif
179
180 /* Buffer memory pools */
181 static struct pool bmempools[NMEMPOOLS];
182
183 struct vm_map *buf_map;
184
185 /*
186 * Buffer memory pool allocator.
187 */
188 static void *
189 bufpool_page_alloc(struct pool *pp, int flags)
190 {
191 return (void *)uvm_km_kmemalloc1(buf_map,
192 uvm.kernel_object, MAXBSIZE, MAXBSIZE,
193 UVM_UNKNOWN_OFFSET,
194 (flags & PR_WAITOK) ? 0
195 : UVM_KMF_NOWAIT);
196 }
197
198 static void
199 bufpool_page_free(struct pool *pp, void *v)
200 {
201 uvm_km_free(buf_map, (vaddr_t)v, MAXBSIZE);
202 }
203
204 static struct pool_allocator bufmempool_allocator = {
205 bufpool_page_alloc, bufpool_page_free, MAXBSIZE,
206 };
207
208 /* Buffer memory management variables */
209 u_long bufmem_valimit;
210 u_long bufmem_hiwater;
211 u_long bufmem_lowater;
212 u_long bufmem;
213
214 /*
215 * MD code can call this to set a hard limit on the amount
216 * of virtual memory used by the buffer cache.
217 */
218 int
219 buf_setvalimit(vsize_t sz)
220 {
221
222 /* We need to accommodate at least NMEMPOOLS of MAXBSIZE each */
223 if (sz < NMEMPOOLS * MAXBSIZE)
224 return EINVAL;
225
226 bufmem_valimit = sz;
227 return 0;
228 }
229
230 static int buf_trim(void);
231
232 /*
233 * bread()/breadn() helper.
234 */
235 static __inline struct buf *bio_doread(struct vnode *, daddr_t, int,
236 struct ucred *, int);
237 int count_lock_queue(void);
238
239 /*
240 * Insq/Remq for the buffer free lists.
241 * Call with buffer queue locked.
242 */
243 #define binsheadfree(bp, dp) TAILQ_INSERT_HEAD(dp, bp, b_freelist)
244 #define binstailfree(bp, dp) TAILQ_INSERT_TAIL(dp, bp, b_freelist)
245
246 #ifdef DEBUG
247 int debug_verify_freelist = 0;
248 static int checkfreelist(struct buf *bp, struct bqueues *dp)
249 {
250 struct buf *b;
251 TAILQ_FOREACH(b, dp, b_freelist) {
252 if (b == bp)
253 return 1;
254 }
255 return 0;
256 }
257 #endif
258
259 void
260 bremfree(struct buf *bp)
261 {
262 struct bqueues *dp = NULL;
263
264 LOCK_ASSERT(simple_lock_held(&bqueue_slock));
265
266 KDASSERT(!debug_verify_freelist ||
267 checkfreelist(bp, &bufqueues[BQ_AGE]) ||
268 checkfreelist(bp, &bufqueues[BQ_LRU]) ||
269 checkfreelist(bp, &bufqueues[BQ_LOCKED]) );
270
271 /*
272 * We only calculate the head of the freelist when removing
273 * the last element of the list as that is the only time that
274 * it is needed (e.g. to reset the tail pointer).
275 *
276 * NB: This makes an assumption about how tailq's are implemented.
277 *
278 * We break the TAILQ abstraction in order to efficiently remove a
279 * buffer from its freelist without having to know exactly which
280 * freelist it is on.
281 */
282 if (TAILQ_NEXT(bp, b_freelist) == NULL) {
283 for (dp = bufqueues; dp < &bufqueues[BQUEUES]; dp++)
284 if (dp->tqh_last == &bp->b_freelist.tqe_next)
285 break;
286 if (dp == &bufqueues[BQUEUES])
287 panic("bremfree: lost tail");
288 }
289 TAILQ_REMOVE(dp, bp, b_freelist);
290 }
291
292 u_long
293 buf_memcalc(void)
294 {
295 u_long n;
296
297 /*
298 * Determine the upper bound of memory to use for buffers.
299 *
300 * - If bufpages is specified, use that as the number
301 * pages.
302 *
303 * - Otherwise, use bufcache as the percentage of
304 * physical memory.
305 */
306 if (bufpages != 0) {
307 n = bufpages;
308 } else {
309 if (bufcache < 5) {
310 printf("forcing bufcache %d -> 5", bufcache);
311 bufcache = 5;
312 }
313 if (bufcache > 95) {
314 printf("forcing bufcache %d -> 95", bufcache);
315 bufcache = 95;
316 }
317 n = physmem / 100 * bufcache;
318 }
319
320 n <<= PAGE_SHIFT;
321 if (bufmem_valimit != 0 && n > bufmem_valimit)
322 n = bufmem_valimit;
323
324 return (n);
325 }
326
327 /*
328 * Initialize buffers and hash links for buffers.
329 */
330 void
331 bufinit(void)
332 {
333 struct bqueues *dp;
334 int smallmem;
335 u_int i;
336
337 /*
338 * Initialize buffer cache memory parameters.
339 */
340 bufmem = 0;
341 bufmem_hiwater = buf_memcalc();
342 /* lowater is approx. 2% of memory (with bufcache=30) */
343 bufmem_lowater = (bufmem_hiwater >> 4);
344 if (bufmem_lowater < 64 * 1024)
345 /* Ensure a reasonable minimum value */
346 bufmem_lowater = 64 * 1024;
347
348 if (bufmem_valimit != 0) {
349 vaddr_t minaddr = 0, maxaddr;
350 buf_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
351 bufmem_valimit, VM_MAP_PAGEABLE,
352 FALSE, 0);
353 if (buf_map == NULL)
354 panic("bufinit: cannot allocate submap");
355 } else
356 buf_map = kernel_map;
357
358 /*
359 * Initialize the buffer pools.
360 */
361 pool_init(&bufpool, sizeof(struct buf), 0, 0, 0, "bufpl", NULL);
362
363 /* On "small" machines use small pool page sizes where possible */
364 smallmem = (physmem < atop(16*1024*1024));
365
366 for (i = 0; i < NMEMPOOLS; i++) {
367 struct pool_allocator *pa;
368 struct pool *pp = &bmempools[i];
369 u_int size = 1 << (i + MEMPOOL_INDEX_OFFSET);
370 char *name = malloc(8, M_TEMP, M_WAITOK);
371 snprintf(name, 8, "buf%dk", 1 << i);
372 pa = (size <= PAGE_SIZE && smallmem)
373 ? &pool_allocator_nointr
374 : &bufmempool_allocator;
375 pool_init(pp, size, 0, 0, PR_IMMEDRELEASE, name, pa);
376 pool_setlowat(pp, 1);
377 }
378
379 /* Initialize the buffer queues */
380 for (dp = bufqueues; dp < &bufqueues[BQUEUES]; dp++)
381 TAILQ_INIT(dp);
382
383 /*
384 * Estimate hash table size based on the amount of memory we
385 * intend to use for the buffer cache. The average buffer
386 * size is dependent on our clients (i.e. filesystems).
387 *
388 * For now, use an empirical 3K per buffer.
389 */
390 nbuf = (bufmem_hiwater / 1024) / 3;
391 bufhashtbl = hashinit(nbuf, HASH_LIST, M_CACHE, M_WAITOK, &bufhash);
392 }
393
394 static int
395 buf_lotsfree(void)
396 {
397 return (bufmem < bufmem_lowater ||
398 (bufmem < bufmem_hiwater && uvmexp.free > 2*uvmexp.freetarg));
399 }
400
401 /*
402 * Return estimate of # of buffers we think need to be
403 * released to help resolve low memory conditions.
404 */
405 static int
406 buf_canrelease(void)
407 {
408 int n;
409
410 if (bufmem < bufmem_lowater)
411 return 0;
412
413 n = uvmexp.freetarg - uvmexp.free;
414 if (n < 0)
415 n = 0;
416 return 2*n;
417 }
418
419 /*
420 * Buffer memory allocation helper functions
421 */
422 static __inline u_long
423 buf_mempoolidx(u_long size)
424 {
425 u_int n = 0;
426
427 size -= 1;
428 size >>= MEMPOOL_INDEX_OFFSET;
429 while (size) {
430 size >>= 1;
431 n += 1;
432 }
433 if (n >= NMEMPOOLS)
434 panic("buf mem pool index %d", n);
435 return n;
436 }
437
438 static __inline u_long
439 buf_roundsize(u_long size)
440 {
441 /* Round up to nearest power of 2 */
442 return (1 << (buf_mempoolidx(size) + MEMPOOL_INDEX_OFFSET));
443 }
444
445 static __inline caddr_t
446 buf_malloc(size_t size)
447 {
448 u_int n = buf_mempoolidx(size);
449 caddr_t addr;
450 int s;
451
452 while (1) {
453 addr = pool_get(&bmempools[n], PR_NOWAIT);
454 if (addr != NULL)
455 break;
456
457 /* No memory, see if we can free some. If so, try again */
458 if (buf_drain(1) > 0)
459 continue;
460
461 /* Wait for buffers to arrive on the LRU queue */
462 s = splbio();
463 simple_lock(&bqueue_slock);
464 needbuffer = 1;
465 ltsleep(&needbuffer, PNORELOCK | (PRIBIO+1),
466 "buf_malloc", 0, &bqueue_slock);
467 splx(s);
468 }
469
470 return addr;
471 }
472
473 static void
474 buf_mrelease(caddr_t addr, size_t size)
475 {
476
477 pool_put(&bmempools[buf_mempoolidx(size)], addr);
478 }
479
480
481 static __inline struct buf *
482 bio_doread(struct vnode *vp, daddr_t blkno, int size, struct ucred *cred,
483 int async)
484 {
485 struct buf *bp;
486 struct lwp *l = (curlwp != NULL ? curlwp : &lwp0); /* XXX */
487 struct proc *p = l->l_proc;
488
489 bp = getblk(vp, blkno, size, 0, 0);
490
491 #ifdef DIAGNOSTIC
492 if (bp == NULL) {
493 panic("bio_doread: no such buf");
494 }
495 #endif
496
497 /*
498 * If buffer does not have data valid, start a read.
499 * Note that if buffer is B_INVAL, getblk() won't return it.
500 * Therefore, it's valid if its I/O has completed or been delayed.
501 */
502 if (!ISSET(bp->b_flags, (B_DONE | B_DELWRI))) {
503 /* Start I/O for the buffer. */
504 SET(bp->b_flags, B_READ | async);
505 if (async)
506 BIO_SETPRIO(bp, BPRIO_TIMELIMITED);
507 else
508 BIO_SETPRIO(bp, BPRIO_TIMECRITICAL);
509 VOP_STRATEGY(bp);
510
511 /* Pay for the read. */
512 p->p_stats->p_ru.ru_inblock++;
513 } else if (async) {
514 brelse(bp);
515 }
516
517 return (bp);
518 }
519
520 /*
521 * Read a disk block.
522 * This algorithm described in Bach (p.54).
523 */
524 int
525 bread(struct vnode *vp, daddr_t blkno, int size, struct ucred *cred,
526 struct buf **bpp)
527 {
528 struct buf *bp;
529
530 /* Get buffer for block. */
531 bp = *bpp = bio_doread(vp, blkno, size, cred, 0);
532
533 /* Wait for the read to complete, and return result. */
534 return (biowait(bp));
535 }
536
537 /*
538 * Read-ahead multiple disk blocks. The first is sync, the rest async.
539 * Trivial modification to the breada algorithm presented in Bach (p.55).
540 */
541 int
542 breadn(struct vnode *vp, daddr_t blkno, int size, daddr_t *rablks,
543 int *rasizes, int nrablks, struct ucred *cred, struct buf **bpp)
544 {
545 struct buf *bp;
546 int i;
547
548 bp = *bpp = bio_doread(vp, blkno, size, cred, 0);
549
550 /*
551 * For each of the read-ahead blocks, start a read, if necessary.
552 */
553 for (i = 0; i < nrablks; i++) {
554 /* If it's in the cache, just go on to next one. */
555 if (incore(vp, rablks[i]))
556 continue;
557
558 /* Get a buffer for the read-ahead block */
559 (void) bio_doread(vp, rablks[i], rasizes[i], cred, B_ASYNC);
560 }
561
562 /* Otherwise, we had to start a read for it; wait until it's valid. */
563 return (biowait(bp));
564 }
565
566 /*
567 * Read with single-block read-ahead. Defined in Bach (p.55), but
568 * implemented as a call to breadn().
569 * XXX for compatibility with old file systems.
570 */
571 int
572 breada(struct vnode *vp, daddr_t blkno, int size, daddr_t rablkno,
573 int rabsize, struct ucred *cred, struct buf **bpp)
574 {
575
576 return (breadn(vp, blkno, size, &rablkno, &rabsize, 1, cred, bpp));
577 }
578
579 /*
580 * Block write. Described in Bach (p.56)
581 */
582 int
583 bwrite(struct buf *bp)
584 {
585 int rv, sync, wasdelayed, s;
586 struct lwp *l = (curlwp != NULL ? curlwp : &lwp0); /* XXX */
587 struct proc *p = l->l_proc;
588 struct vnode *vp;
589 struct mount *mp;
590
591 KASSERT(ISSET(bp->b_flags, B_BUSY));
592
593 vp = bp->b_vp;
594 if (vp != NULL) {
595 if (vp->v_type == VBLK)
596 mp = vp->v_specmountpoint;
597 else
598 mp = vp->v_mount;
599 } else {
600 mp = NULL;
601 }
602
603 /*
604 * Remember buffer type, to switch on it later. If the write was
605 * synchronous, but the file system was mounted with MNT_ASYNC,
606 * convert it to a delayed write.
607 * XXX note that this relies on delayed tape writes being converted
608 * to async, not sync writes (which is safe, but ugly).
609 */
610 sync = !ISSET(bp->b_flags, B_ASYNC);
611 if (sync && mp != NULL && ISSET(mp->mnt_flag, MNT_ASYNC)) {
612 bdwrite(bp);
613 return (0);
614 }
615
616 /*
617 * Collect statistics on synchronous and asynchronous writes.
618 * Writes to block devices are charged to their associated
619 * filesystem (if any).
620 */
621 if (mp != NULL) {
622 if (sync)
623 mp->mnt_stat.f_syncwrites++;
624 else
625 mp->mnt_stat.f_asyncwrites++;
626 }
627
628 s = splbio();
629 simple_lock(&bp->b_interlock);
630
631 wasdelayed = ISSET(bp->b_flags, B_DELWRI);
632
633 CLR(bp->b_flags, (B_READ | B_DONE | B_ERROR | B_DELWRI));
634
635 /*
636 * Pay for the I/O operation and make sure the buf is on the correct
637 * vnode queue.
638 */
639 if (wasdelayed)
640 reassignbuf(bp, bp->b_vp);
641 else
642 p->p_stats->p_ru.ru_oublock++;
643
644 /* Initiate disk write. Make sure the appropriate party is charged. */
645 V_INCR_NUMOUTPUT(bp->b_vp);
646 simple_unlock(&bp->b_interlock);
647 splx(s);
648
649 if (sync)
650 BIO_SETPRIO(bp, BPRIO_TIMECRITICAL);
651 else
652 BIO_SETPRIO(bp, BPRIO_TIMELIMITED);
653
654 VOP_STRATEGY(bp);
655
656 if (sync) {
657 /* If I/O was synchronous, wait for it to complete. */
658 rv = biowait(bp);
659
660 /* Release the buffer. */
661 brelse(bp);
662
663 return (rv);
664 } else {
665 return (0);
666 }
667 }
668
669 int
670 vn_bwrite(void *v)
671 {
672 struct vop_bwrite_args *ap = v;
673
674 return (bwrite(ap->a_bp));
675 }
676
677 /*
678 * Delayed write.
679 *
680 * The buffer is marked dirty, but is not queued for I/O.
681 * This routine should be used when the buffer is expected
682 * to be modified again soon, typically a small write that
683 * partially fills a buffer.
684 *
685 * NB: magnetic tapes cannot be delayed; they must be
686 * written in the order that the writes are requested.
687 *
688 * Described in Leffler, et al. (pp. 208-213).
689 */
690 void
691 bdwrite(struct buf *bp)
692 {
693 struct lwp *l = (curlwp != NULL ? curlwp : &lwp0); /* XXX */
694 struct proc *p = l->l_proc;
695 const struct bdevsw *bdev;
696 int s;
697
698 /* If this is a tape block, write the block now. */
699 bdev = bdevsw_lookup(bp->b_dev);
700 if (bdev != NULL && bdev->d_type == D_TAPE) {
701 bawrite(bp);
702 return;
703 }
704
705 /*
706 * If the block hasn't been seen before:
707 * (1) Mark it as having been seen,
708 * (2) Charge for the write,
709 * (3) Make sure it's on its vnode's correct block list.
710 */
711 s = splbio();
712 simple_lock(&bp->b_interlock);
713
714 KASSERT(ISSET(bp->b_flags, B_BUSY));
715
716 if (!ISSET(bp->b_flags, B_DELWRI)) {
717 SET(bp->b_flags, B_DELWRI);
718 p->p_stats->p_ru.ru_oublock++;
719 reassignbuf(bp, bp->b_vp);
720 }
721
722 /* Otherwise, the "write" is done, so mark and release the buffer. */
723 CLR(bp->b_flags, B_DONE);
724 simple_unlock(&bp->b_interlock);
725 splx(s);
726
727 brelse(bp);
728 }
729
730 /*
731 * Asynchronous block write; just an asynchronous bwrite().
732 */
733 void
734 bawrite(struct buf *bp)
735 {
736 int s;
737
738 s = splbio();
739 simple_lock(&bp->b_interlock);
740
741 KASSERT(ISSET(bp->b_flags, B_BUSY));
742
743 SET(bp->b_flags, B_ASYNC);
744 simple_unlock(&bp->b_interlock);
745 splx(s);
746 VOP_BWRITE(bp);
747 }
748
749 /*
750 * Same as first half of bdwrite, mark buffer dirty, but do not release it.
751 * Call at splbio() and with the buffer interlock locked.
752 * Note: called only from biodone() through ffs softdep's bioops.io_complete()
753 */
754 void
755 bdirty(struct buf *bp)
756 {
757 struct lwp *l = (curlwp != NULL ? curlwp : &lwp0); /* XXX */
758 struct proc *p = l->l_proc;
759
760 LOCK_ASSERT(simple_lock_held(&bp->b_interlock));
761 KASSERT(ISSET(bp->b_flags, B_BUSY));
762
763 CLR(bp->b_flags, B_AGE);
764
765 if (!ISSET(bp->b_flags, B_DELWRI)) {
766 SET(bp->b_flags, B_DELWRI);
767 p->p_stats->p_ru.ru_oublock++;
768 reassignbuf(bp, bp->b_vp);
769 }
770 }
771
772 /*
773 * Release a buffer on to the free lists.
774 * Described in Bach (p. 46).
775 */
776 void
777 brelse(struct buf *bp)
778 {
779 struct bqueues *bufq;
780 int s;
781
782 /* Block disk interrupts. */
783 s = splbio();
784 simple_lock(&bqueue_slock);
785 simple_lock(&bp->b_interlock);
786
787 KASSERT(ISSET(bp->b_flags, B_BUSY));
788 KASSERT(!ISSET(bp->b_flags, B_CALL));
789
790 /* Wake up any processes waiting for any buffer to become free. */
791 if (needbuffer) {
792 needbuffer = 0;
793 wakeup(&needbuffer);
794 }
795
796 /* Wake up any proceeses waiting for _this_ buffer to become free. */
797 if (ISSET(bp->b_flags, B_WANTED)) {
798 CLR(bp->b_flags, B_WANTED|B_AGE);
799 wakeup(bp);
800 }
801
802 /*
803 * Determine which queue the buffer should be on, then put it there.
804 */
805
806 /* If it's locked, don't report an error; try again later. */
807 if (ISSET(bp->b_flags, (B_LOCKED|B_ERROR)) == (B_LOCKED|B_ERROR))
808 CLR(bp->b_flags, B_ERROR);
809
810 /* If it's not cacheable, or an error, mark it invalid. */
811 if (ISSET(bp->b_flags, (B_NOCACHE|B_ERROR)))
812 SET(bp->b_flags, B_INVAL);
813
814 if (ISSET(bp->b_flags, B_VFLUSH)) {
815 /*
816 * This is a delayed write buffer that was just flushed to
817 * disk. It is still on the LRU queue. If it's become
818 * invalid, then we need to move it to a different queue;
819 * otherwise leave it in its current position.
820 */
821 CLR(bp->b_flags, B_VFLUSH);
822 if (!ISSET(bp->b_flags, B_ERROR|B_INVAL|B_LOCKED|B_AGE)) {
823 KDASSERT(!debug_verify_freelist || checkfreelist(bp, &bufqueues[BQ_LRU]));
824 goto already_queued;
825 } else {
826 bremfree(bp);
827 }
828 }
829
830 KDASSERT(!debug_verify_freelist || !checkfreelist(bp, &bufqueues[BQ_AGE]));
831 KDASSERT(!debug_verify_freelist || !checkfreelist(bp, &bufqueues[BQ_LRU]));
832 KDASSERT(!debug_verify_freelist || !checkfreelist(bp, &bufqueues[BQ_LOCKED]));
833
834 if ((bp->b_bufsize <= 0) || ISSET(bp->b_flags, B_INVAL)) {
835 /*
836 * If it's invalid or empty, dissociate it from its vnode
837 * and put on the head of the appropriate queue.
838 */
839 if (LIST_FIRST(&bp->b_dep) != NULL && bioops.io_deallocate)
840 (*bioops.io_deallocate)(bp);
841 CLR(bp->b_flags, B_DONE|B_DELWRI);
842 if (bp->b_vp) {
843 reassignbuf(bp, bp->b_vp);
844 brelvp(bp);
845 }
846 if (bp->b_bufsize <= 0)
847 /* no data */
848 goto already_queued;
849 else
850 /* invalid data */
851 bufq = &bufqueues[BQ_AGE];
852 binsheadfree(bp, bufq);
853 } else {
854 /*
855 * It has valid data. Put it on the end of the appropriate
856 * queue, so that it'll stick around for as long as possible.
857 * If buf is AGE, but has dependencies, must put it on last
858 * bufqueue to be scanned, ie LRU. This protects against the
859 * livelock where BQ_AGE only has buffers with dependencies,
860 * and we thus never get to the dependent buffers in BQ_LRU.
861 */
862 if (ISSET(bp->b_flags, B_LOCKED))
863 /* locked in core */
864 bufq = &bufqueues[BQ_LOCKED];
865 else if (!ISSET(bp->b_flags, B_AGE))
866 /* valid data */
867 bufq = &bufqueues[BQ_LRU];
868 else {
869 /* stale but valid data */
870 int has_deps;
871
872 if (LIST_FIRST(&bp->b_dep) != NULL &&
873 bioops.io_countdeps)
874 has_deps = (*bioops.io_countdeps)(bp, 0);
875 else
876 has_deps = 0;
877 bufq = has_deps ? &bufqueues[BQ_LRU] :
878 &bufqueues[BQ_AGE];
879 }
880 binstailfree(bp, bufq);
881 }
882
883 already_queued:
884 /* Unlock the buffer. */
885 CLR(bp->b_flags, B_AGE|B_ASYNC|B_BUSY|B_NOCACHE);
886 SET(bp->b_flags, B_CACHE);
887
888 /* Allow disk interrupts. */
889 simple_unlock(&bp->b_interlock);
890 simple_unlock(&bqueue_slock);
891 if (bp->b_bufsize <= 0) {
892 #ifdef DEBUG
893 memset((char *)bp, 0, sizeof(*bp));
894 #endif
895 pool_put(&bufpool, bp);
896 }
897 splx(s);
898 }
899
900 /*
901 * Determine if a block is in the cache.
902 * Just look on what would be its hash chain. If it's there, return
903 * a pointer to it, unless it's marked invalid. If it's marked invalid,
904 * we normally don't return the buffer, unless the caller explicitly
905 * wants us to.
906 */
907 struct buf *
908 incore(struct vnode *vp, daddr_t blkno)
909 {
910 struct buf *bp;
911
912 /* Search hash chain */
913 LIST_FOREACH(bp, BUFHASH(vp, blkno), b_hash) {
914 if (bp->b_lblkno == blkno && bp->b_vp == vp &&
915 !ISSET(bp->b_flags, B_INVAL))
916 return (bp);
917 }
918
919 return (NULL);
920 }
921
922 /*
923 * Get a block of requested size that is associated with
924 * a given vnode and block offset. If it is found in the
925 * block cache, mark it as having been found, make it busy
926 * and return it. Otherwise, return an empty block of the
927 * correct size. It is up to the caller to insure that the
928 * cached blocks be of the correct size.
929 */
930 struct buf *
931 getblk(struct vnode *vp, daddr_t blkno, int size, int slpflag, int slptimeo)
932 {
933 struct buf *bp;
934 int s, err;
935 int preserve;
936
937 start:
938 s = splbio();
939 simple_lock(&bqueue_slock);
940 bp = incore(vp, blkno);
941 if (bp != NULL) {
942 simple_lock(&bp->b_interlock);
943 if (ISSET(bp->b_flags, B_BUSY)) {
944 simple_unlock(&bqueue_slock);
945 if (curproc == uvm.pagedaemon_proc) {
946 simple_unlock(&bp->b_interlock);
947 splx(s);
948 return NULL;
949 }
950 SET(bp->b_flags, B_WANTED);
951 err = ltsleep(bp, slpflag | (PRIBIO + 1) | PNORELOCK,
952 "getblk", slptimeo, &bp->b_interlock);
953 splx(s);
954 if (err)
955 return (NULL);
956 goto start;
957 }
958 #ifdef DIAGNOSTIC
959 if (ISSET(bp->b_flags, B_DONE|B_DELWRI) &&
960 bp->b_bcount < size && vp->v_type != VBLK)
961 panic("getblk: block size invariant failed");
962 #endif
963 SET(bp->b_flags, B_BUSY);
964 bremfree(bp);
965 preserve = 1;
966 } else {
967 if ((bp = getnewbuf(slpflag, slptimeo, 0)) == NULL) {
968 simple_unlock(&bqueue_slock);
969 splx(s);
970 goto start;
971 }
972
973 binshash(bp, BUFHASH(vp, blkno));
974 bp->b_blkno = bp->b_lblkno = bp->b_rawblkno = blkno;
975 bgetvp(vp, bp);
976 preserve = 0;
977 }
978 simple_unlock(&bp->b_interlock);
979 simple_unlock(&bqueue_slock);
980 splx(s);
981 /*
982 * LFS can't track total size of B_LOCKED buffer (locked_queue_bytes)
983 * if we re-size buffers here.
984 */
985 if (ISSET(bp->b_flags, B_LOCKED)) {
986 KASSERT(bp->b_bufsize >= size);
987 } else {
988 allocbuf(bp, size, preserve);
989 }
990 BIO_SETPRIO(bp, BPRIO_DEFAULT);
991 return (bp);
992 }
993
994 /*
995 * Get an empty, disassociated buffer of given size.
996 */
997 struct buf *
998 geteblk(int size)
999 {
1000 struct buf *bp;
1001 int s;
1002
1003 s = splbio();
1004 simple_lock(&bqueue_slock);
1005 while ((bp = getnewbuf(0, 0, 0)) == 0)
1006 ;
1007
1008 SET(bp->b_flags, B_INVAL);
1009 binshash(bp, &invalhash);
1010 simple_unlock(&bqueue_slock);
1011 simple_unlock(&bp->b_interlock);
1012 splx(s);
1013 allocbuf(bp, size, 0);
1014 return (bp);
1015 }
1016
1017 /*
1018 * Expand or contract the actual memory allocated to a buffer.
1019 *
1020 * If the buffer shrinks, data is lost, so it's up to the
1021 * caller to have written it out *first*; this routine will not
1022 * start a write. If the buffer grows, it's the callers
1023 * responsibility to fill out the buffer's additional contents.
1024 */
1025 void
1026 allocbuf(struct buf *bp, int size, int preserve)
1027 {
1028 vsize_t oldsize, desired_size;
1029 caddr_t addr;
1030 int s, delta;
1031
1032 desired_size = buf_roundsize(size);
1033 if (desired_size > MAXBSIZE)
1034 printf("allocbuf: buffer larger than MAXBSIZE requested");
1035
1036 bp->b_bcount = size;
1037
1038 oldsize = bp->b_bufsize;
1039 if (oldsize == desired_size)
1040 return;
1041
1042 /*
1043 * If we want a buffer of a different size, re-allocate the
1044 * buffer's memory; copy old content only if needed.
1045 */
1046 addr = buf_malloc(desired_size);
1047 if (preserve)
1048 memcpy(addr, bp->b_data, MIN(oldsize,desired_size));
1049 if (bp->b_data != NULL)
1050 buf_mrelease(bp->b_data, oldsize);
1051 bp->b_data = addr;
1052 bp->b_bufsize = desired_size;
1053
1054 /*
1055 * Update overall buffer memory counter (protected by bqueue_slock)
1056 */
1057 delta = (long)desired_size - (long)oldsize;
1058
1059 s = splbio();
1060 simple_lock(&bqueue_slock);
1061 if ((bufmem += delta) > bufmem_hiwater) {
1062 /*
1063 * Need to trim overall memory usage.
1064 */
1065 while (buf_canrelease()) {
1066 if (buf_trim() == 0)
1067 break;
1068 }
1069 }
1070
1071 simple_unlock(&bqueue_slock);
1072 splx(s);
1073 }
1074
1075 /*
1076 * Find a buffer which is available for use.
1077 * Select something from a free list.
1078 * Preference is to AGE list, then LRU list.
1079 *
1080 * Called at splbio and with buffer queues locked.
1081 * Return buffer locked.
1082 */
1083 struct buf *
1084 getnewbuf(int slpflag, int slptimeo, int from_bufq)
1085 {
1086 struct buf *bp;
1087
1088 start:
1089 LOCK_ASSERT(simple_lock_held(&bqueue_slock));
1090
1091 /*
1092 * Get a new buffer from the pool; but use NOWAIT because
1093 * we have the buffer queues locked.
1094 */
1095 if (buf_lotsfree() && !from_bufq &&
1096 (bp = pool_get(&bufpool, PR_NOWAIT)) != NULL) {
1097 memset((char *)bp, 0, sizeof(*bp));
1098 BUF_INIT(bp);
1099 bp->b_dev = NODEV;
1100 bp->b_vnbufs.le_next = NOLIST;
1101 bp->b_flags = B_BUSY;
1102 simple_lock(&bp->b_interlock);
1103 return (bp);
1104 }
1105
1106 if ((bp = TAILQ_FIRST(&bufqueues[BQ_AGE])) != NULL ||
1107 (bp = TAILQ_FIRST(&bufqueues[BQ_LRU])) != NULL) {
1108 simple_lock(&bp->b_interlock);
1109 bremfree(bp);
1110 } else {
1111 /* wait for a free buffer of any kind */
1112 needbuffer = 1;
1113 ltsleep(&needbuffer, slpflag|(PRIBIO+1),
1114 "getnewbuf", slptimeo, &bqueue_slock);
1115 return (NULL);
1116 }
1117
1118 #ifdef DIAGNOSTIC
1119 if (bp->b_bufsize <= 0)
1120 panic("buffer %p: on queue but empty", bp);
1121 #endif
1122
1123 if (ISSET(bp->b_flags, B_VFLUSH)) {
1124 /*
1125 * This is a delayed write buffer being flushed to disk. Make
1126 * sure it gets aged out of the queue when it's finished, and
1127 * leave it off the LRU queue.
1128 */
1129 CLR(bp->b_flags, B_VFLUSH);
1130 SET(bp->b_flags, B_AGE);
1131 simple_unlock(&bp->b_interlock);
1132 goto start;
1133 }
1134
1135 /* Buffer is no longer on free lists. */
1136 SET(bp->b_flags, B_BUSY);
1137
1138 /*
1139 * If buffer was a delayed write, start it and return NULL
1140 * (since we might sleep while starting the write).
1141 */
1142 if (ISSET(bp->b_flags, B_DELWRI)) {
1143 /*
1144 * This buffer has gone through the LRU, so make sure it gets
1145 * reused ASAP.
1146 */
1147 SET(bp->b_flags, B_AGE);
1148 simple_unlock(&bp->b_interlock);
1149 simple_unlock(&bqueue_slock);
1150 bawrite(bp);
1151 simple_lock(&bqueue_slock);
1152 return (NULL);
1153 }
1154
1155 /* disassociate us from our vnode, if we had one... */
1156 if (bp->b_vp)
1157 brelvp(bp);
1158
1159 if (LIST_FIRST(&bp->b_dep) != NULL && bioops.io_deallocate)
1160 (*bioops.io_deallocate)(bp);
1161
1162 /* clear out various other fields */
1163 bp->b_flags = B_BUSY;
1164 bp->b_dev = NODEV;
1165 bp->b_blkno = bp->b_lblkno = bp->b_rawblkno = 0;
1166 bp->b_iodone = 0;
1167 bp->b_error = 0;
1168 bp->b_resid = 0;
1169 bp->b_bcount = 0;
1170
1171 bremhash(bp);
1172 return (bp);
1173 }
1174
1175 /*
1176 * Attempt to free an aged buffer off the queues.
1177 * Called at splbio and with queue lock held.
1178 * Returns the amount of buffer memory freed.
1179 */
1180 int
1181 buf_trim(void)
1182 {
1183 struct buf *bp;
1184 long size = 0;
1185 int wanted;
1186
1187 /* Instruct getnewbuf() to get buffers off the queues */
1188 if ((bp = getnewbuf(PCATCH, 1, 1)) == NULL)
1189 return 0;
1190
1191 wanted = ISSET(bp->b_flags, B_WANTED);
1192 simple_unlock(&bp->b_interlock);
1193 if (wanted) {
1194 printf("buftrim: got WANTED buffer\n");
1195 SET(bp->b_flags, B_INVAL);
1196 binshash(bp, &invalhash);
1197 simple_unlock(&bqueue_slock);
1198 goto out;
1199 }
1200 size = bp->b_bufsize;
1201 bufmem -= size;
1202 simple_unlock(&bqueue_slock);
1203 if (size > 0) {
1204 buf_mrelease(bp->b_data, size);
1205 bp->b_bcount = bp->b_bufsize = 0;
1206 }
1207
1208 out:
1209 /* brelse() will return the buffer to the global buffer pool */
1210 brelse(bp);
1211 simple_lock(&bqueue_slock);
1212 return size;
1213 }
1214
1215 int
1216 buf_drain(int n)
1217 {
1218 int s, size = 0;
1219
1220 /* If not asked for a specific amount, make our own estimate */
1221 if (n == 0)
1222 n = buf_canrelease();
1223
1224 s = splbio();
1225 simple_lock(&bqueue_slock);
1226 while (n-- > 0 && bufmem > bufmem_lowater)
1227 size += buf_trim();
1228 simple_unlock(&bqueue_slock);
1229 splx(s);
1230 return size;
1231 }
1232
1233 /*
1234 * Wait for operations on the buffer to complete.
1235 * When they do, extract and return the I/O's error value.
1236 */
1237 int
1238 biowait(struct buf *bp)
1239 {
1240 int s, error;
1241
1242 s = splbio();
1243 simple_lock(&bp->b_interlock);
1244 while (!ISSET(bp->b_flags, B_DONE | B_DELWRI))
1245 ltsleep(bp, PRIBIO + 1, "biowait", 0, &bp->b_interlock);
1246
1247 /* check for interruption of I/O (e.g. via NFS), then errors. */
1248 if (ISSET(bp->b_flags, B_EINTR)) {
1249 CLR(bp->b_flags, B_EINTR);
1250 error = EINTR;
1251 } else if (ISSET(bp->b_flags, B_ERROR))
1252 error = bp->b_error ? bp->b_error : EIO;
1253 else
1254 error = 0;
1255
1256 simple_unlock(&bp->b_interlock);
1257 splx(s);
1258 return (error);
1259 }
1260
1261 /*
1262 * Mark I/O complete on a buffer.
1263 *
1264 * If a callback has been requested, e.g. the pageout
1265 * daemon, do so. Otherwise, awaken waiting processes.
1266 *
1267 * [ Leffler, et al., says on p.247:
1268 * "This routine wakes up the blocked process, frees the buffer
1269 * for an asynchronous write, or, for a request by the pagedaemon
1270 * process, invokes a procedure specified in the buffer structure" ]
1271 *
1272 * In real life, the pagedaemon (or other system processes) wants
1273 * to do async stuff to, and doesn't want the buffer brelse()'d.
1274 * (for swap pager, that puts swap buffers on the free lists (!!!),
1275 * for the vn device, that puts malloc'd buffers on the free lists!)
1276 */
1277 void
1278 biodone(struct buf *bp)
1279 {
1280 int s = splbio();
1281
1282 simple_lock(&bp->b_interlock);
1283 if (ISSET(bp->b_flags, B_DONE))
1284 panic("biodone already");
1285 SET(bp->b_flags, B_DONE); /* note that it's done */
1286 BIO_SETPRIO(bp, BPRIO_DEFAULT);
1287
1288 if (LIST_FIRST(&bp->b_dep) != NULL && bioops.io_complete)
1289 (*bioops.io_complete)(bp);
1290
1291 if (!ISSET(bp->b_flags, B_READ)) /* wake up reader */
1292 vwakeup(bp);
1293
1294 /*
1295 * If necessary, call out. Unlock the buffer before calling
1296 * iodone() as the buffer isn't valid any more when it return.
1297 */
1298 if (ISSET(bp->b_flags, B_CALL)) {
1299 CLR(bp->b_flags, B_CALL); /* but note callout done */
1300 simple_unlock(&bp->b_interlock);
1301 (*bp->b_iodone)(bp);
1302 } else {
1303 if (ISSET(bp->b_flags, B_ASYNC)) { /* if async, release */
1304 simple_unlock(&bp->b_interlock);
1305 brelse(bp);
1306 } else { /* or just wakeup the buffer */
1307 CLR(bp->b_flags, B_WANTED);
1308 wakeup(bp);
1309 simple_unlock(&bp->b_interlock);
1310 }
1311 }
1312
1313 splx(s);
1314 }
1315
1316 /*
1317 * Return a count of buffers on the "locked" queue.
1318 */
1319 int
1320 count_lock_queue(void)
1321 {
1322 struct buf *bp;
1323 int n = 0;
1324
1325 simple_lock(&bqueue_slock);
1326 TAILQ_FOREACH(bp, &bufqueues[BQ_LOCKED], b_freelist)
1327 n++;
1328 simple_unlock(&bqueue_slock);
1329 return (n);
1330 }
1331
1332 /*
1333 * Wait for all buffers to complete I/O
1334 * Return the number of "stuck" buffers.
1335 */
1336 int
1337 buf_syncwait(void)
1338 {
1339 struct buf *bp;
1340 int iter, nbusy, nbusy_prev = 0, dcount, s, ihash;
1341
1342 dcount = 10000;
1343 for (iter = 0; iter < 20;) {
1344 s = splbio();
1345 simple_lock(&bqueue_slock);
1346 nbusy = 0;
1347 for (ihash = 0; ihash < bufhash+1; ihash++) {
1348 LIST_FOREACH(bp, &bufhashtbl[ihash], b_hash) {
1349 if ((bp->b_flags & (B_BUSY|B_INVAL|B_READ)) == B_BUSY)
1350 nbusy++;
1351 /*
1352 * With soft updates, some buffers that are
1353 * written will be remarked as dirty until other
1354 * buffers are written.
1355 */
1356 if (bp->b_vp && bp->b_vp->v_mount
1357 && (bp->b_vp->v_mount->mnt_flag & MNT_SOFTDEP)
1358 && (bp->b_flags & B_DELWRI)) {
1359 simple_lock(&bp->b_interlock);
1360 bremfree(bp);
1361 bp->b_flags |= B_BUSY;
1362 nbusy++;
1363 simple_unlock(&bp->b_interlock);
1364 simple_unlock(&bqueue_slock);
1365 bawrite(bp);
1366 if (dcount-- <= 0) {
1367 printf("softdep ");
1368 goto fail;
1369 }
1370 simple_lock(&bqueue_slock);
1371 }
1372 }
1373 }
1374
1375 simple_unlock(&bqueue_slock);
1376 splx(s);
1377
1378 if (nbusy == 0)
1379 break;
1380 if (nbusy_prev == 0)
1381 nbusy_prev = nbusy;
1382 printf("%d ", nbusy);
1383 tsleep(&nbusy, PRIBIO, "bflush",
1384 (iter == 0) ? 1 : hz / 25 * iter);
1385 if (nbusy >= nbusy_prev) /* we didn't flush anything */
1386 iter++;
1387 else
1388 nbusy_prev = nbusy;
1389 }
1390
1391 if (nbusy) {
1392 fail:;
1393 #if defined(DEBUG) || defined(DEBUG_HALT_BUSY)
1394 printf("giving up\nPrinting vnodes for busy buffers\n");
1395 for (ihash = 0; ihash < bufhash+1; ihash++) {
1396 LIST_FOREACH(bp, &bufhashtbl[ihash], b_hash) {
1397 if ((bp->b_flags & (B_BUSY|B_INVAL|B_READ)) == B_BUSY)
1398 vprint(NULL, bp->b_vp);
1399 }
1400 }
1401 #endif
1402 }
1403
1404 return nbusy;
1405 }
1406
1407 #define KERN_BUFSLOP 20
1408 static int
1409 sysctl_dobuf(SYSCTLFN_ARGS)
1410 {
1411 struct buf *bp;
1412 char *dp;
1413 u_int i, elem_size;
1414 size_t len, buflen, needed;
1415 int error, s;
1416
1417 dp = oldp;
1418 len = buflen = oldp != NULL ? *oldlenp : 0;
1419 error = 0;
1420 needed = 0;
1421 elem_size = sizeof(struct buf);
1422
1423 s = splbio();
1424 simple_lock(&bqueue_slock);
1425 for (i = 0; i < BQUEUES; i++) {
1426 TAILQ_FOREACH(bp, &bufqueues[i], b_freelist) {
1427 if (len >= sizeof(elem_size)) {
1428 error = copyout(bp, dp, elem_size);
1429 if (error)
1430 goto cleanup;
1431 dp += elem_size;
1432 len -= elem_size;
1433 }
1434 needed += elem_size;
1435 }
1436 }
1437 cleanup:
1438 simple_unlock(&bqueue_slock);
1439 splx(s);
1440
1441 if (oldp != NULL) {
1442 *oldlenp = (char *)dp - (char *)oldp;
1443 if (needed > *oldlenp)
1444 error = ENOMEM;
1445 } else {
1446 needed += KERN_BUFSLOP;
1447 *oldlenp = needed;
1448 }
1449
1450 return (error);
1451 }
1452
1453 static int sysctlnum_bufcache, sysctlnum_bufmemhiwater, sysctlnum_bufmemlowater;
1454
1455 static int
1456 sysctl_bufvm_update(SYSCTLFN_ARGS)
1457 {
1458 int t, error;
1459 struct sysctlnode node;
1460
1461 node = *rnode;
1462 node.sysctl_data = &t;
1463 t = *(int*)rnode->sysctl_data;
1464 error = sysctl_lookup(SYSCTLFN_CALL(&node));
1465 if (error || newp == NULL)
1466 return (error);
1467
1468 if (rnode->sysctl_num == sysctlnum_bufcache) {
1469 if (t < 0 || t > 100)
1470 return (EINVAL);
1471 bufcache = t;
1472 bufmem_hiwater = buf_memcalc();
1473 bufmem_lowater = (bufmem_hiwater >> 4);
1474 } else if (rnode->sysctl_num == sysctlnum_bufmemlowater) {
1475 bufmem_lowater = t;
1476 } else if (rnode->sysctl_num == sysctlnum_bufmemhiwater) {
1477 bufmem_hiwater = t;
1478 } else
1479 return (EINVAL);
1480
1481 /* Drain until below new high water mark */
1482 while ((t = bufmem - bufmem_hiwater) >= 0) {
1483 if (buf_drain(t / (2*1024)) <= 0)
1484 break;
1485 }
1486
1487 return 0;
1488 }
1489
1490 SYSCTL_SETUP(sysctl_kern_buf_setup, "sysctl kern.buf subtree setup")
1491 {
1492
1493 sysctl_createv(SYSCTL_PERMANENT,
1494 CTLTYPE_NODE, "kern", NULL,
1495 NULL, 0, NULL, 0,
1496 CTL_KERN, CTL_EOL);
1497 sysctl_createv(SYSCTL_PERMANENT,
1498 CTLTYPE_NODE, "buf", NULL,
1499 sysctl_dobuf, 0, NULL, 0,
1500 CTL_KERN, KERN_BUF, CTL_EOL);
1501 }
1502
1503 SYSCTL_SETUP(sysctl_vm_buf_setup, "sysctl vm.buf* subtree setup")
1504 {
1505 struct sysctlnode *rnode;
1506
1507 sysctl_createv(SYSCTL_PERMANENT,
1508 CTLTYPE_NODE, "vm", NULL,
1509 NULL, 0, NULL, 0,
1510 CTL_VM, CTL_EOL);
1511
1512 rnode = NULL;
1513 if (sysctl_createv(SYSCTL_PERMANENT|SYSCTL_READWRITE,
1514 CTLTYPE_INT, "bufcache", &rnode,
1515 sysctl_bufvm_update, 0, &bufcache, 0,
1516 CTL_VM, CTL_CREATE, CTL_EOL) == 0)
1517 sysctlnum_bufcache = rnode->sysctl_num;
1518
1519 rnode = NULL;
1520 if (sysctl_createv(SYSCTL_PERMANENT|SYSCTL_READWRITE,
1521 CTLTYPE_INT, "bufmem_lowater", &rnode,
1522 sysctl_bufvm_update, 0, &bufmem_lowater, 0,
1523 CTL_VM, CTL_CREATE, CTL_EOL) == 0)
1524 sysctlnum_bufmemlowater = rnode->sysctl_num;
1525
1526 rnode = NULL;
1527 if (sysctl_createv(SYSCTL_PERMANENT|SYSCTL_READWRITE,
1528 CTLTYPE_INT, "bufmem_hiwater", &rnode,
1529 sysctl_bufvm_update, 0, &bufmem_hiwater, 0,
1530 CTL_VM, CTL_CREATE, CTL_EOL) == 0)
1531 sysctlnum_bufmemhiwater = rnode->sysctl_num;
1532 }
1533
1534 #ifdef DEBUG
1535 /*
1536 * Print out statistics on the current allocation of the buffer pool.
1537 * Can be enabled to print out on every ``sync'' by setting "syncprt"
1538 * in vfs_syscalls.c using sysctl.
1539 */
1540 void
1541 vfs_bufstats(void)
1542 {
1543 int s, i, j, count;
1544 struct buf *bp;
1545 struct bqueues *dp;
1546 int counts[(MAXBSIZE / PAGE_SIZE) + 1];
1547 static char *bname[BQUEUES] = { "LOCKED", "LRU", "AGE" };
1548
1549 for (dp = bufqueues, i = 0; dp < &bufqueues[BQUEUES]; dp++, i++) {
1550 count = 0;
1551 for (j = 0; j <= MAXBSIZE/PAGE_SIZE; j++)
1552 counts[j] = 0;
1553 s = splbio();
1554 TAILQ_FOREACH(bp, dp, b_freelist) {
1555 counts[bp->b_bufsize/PAGE_SIZE]++;
1556 count++;
1557 }
1558 splx(s);
1559 printf("%s: total-%d", bname[i], count);
1560 for (j = 0; j <= MAXBSIZE/PAGE_SIZE; j++)
1561 if (counts[j] != 0)
1562 printf(", %d-%d", j * PAGE_SIZE, counts[j]);
1563 printf("\n");
1564 }
1565 }
1566 #endif /* DEBUG */
1567