2 * Copyright (c) 2000-2002 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
22 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
24 * Copyright (c) 1994 Christopher G. Demetriou
25 * Copyright (c) 1982, 1986, 1989, 1993
26 * The Regents of the University of California. All rights reserved.
27 * (c) UNIX System Laboratories, Inc.
28 * All or some portions of this file are derived from material licensed
29 * to the University of California by American Telephone and Telegraph
30 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
31 * the permission of UNIX System Laboratories, Inc.
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * 3. All advertising materials mentioning features or use of this software
42 * must display the following acknowledgement:
43 * This product includes software developed by the University of
44 * California, Berkeley and its contributors.
45 * 4. Neither the name of the University nor the names of its contributors
46 * may be used to endorse or promote products derived from this software
47 * without specific prior written permission.
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 * The NEXTSTEP Software License Agreement specifies the terms
62 * and conditions for redistribution.
64 * @(#)vfs_bio.c 8.6 (Berkeley) 1/11/94
70 * Bach: The Design of the UNIX Operating System (Prentice Hall, 1986)
71 * Leffler, et al.: The Design and Implementation of the 4.3BSD
72 * UNIX Operating System (Addison Welley, 1989)
74 #define ZALLOC_METADATA 1
76 #include <sys/param.h>
77 #include <sys/systm.h>
80 #include <sys/vnode.h>
81 #include <sys/mount.h>
82 #include <sys/trace.h>
83 #include <sys/malloc.h>
84 #include <sys/resourcevar.h>
85 #include <miscfs/specfs/specdev.h>
87 #include <vm/vm_pageout.h>
89 #include <kern/assert.h>
90 #endif /* DIAGNOSTIC */
91 #include <kern/task.h>
92 #include <kern/zalloc.h>
94 #include <sys/kdebug.h>
96 extern void bufqinc(int q
);
97 extern void bufqdec(int q
);
98 extern void bufq_balance_thread_init();
100 extern void reassignbuf(struct buf
*, struct vnode
*);
101 static struct buf
*getnewbuf(int slpflag
, int slptimeo
, int *queue
);
103 extern int niobuf
; /* The number of IO buffer headers for cluster IO */
106 /* zone allocated buffer headers */
107 static zone_t buf_hdr_zone
;
108 static int buf_hdr_count
;
111 struct proc
*traceproc
;
112 int tracewhich
, tracebuf
[TRCSIZ
];
114 char traceflags
[TR_NFLAGS
];
118 * Definitions for the buffer hash lists.
120 #define BUFHASH(dvp, lbn) \
121 (&bufhashtbl[((long)(dvp) / sizeof(*(dvp)) + (int)(lbn)) & bufhash])
122 LIST_HEAD(bufhashhdr
, buf
) *bufhashtbl
, invalhash
;
125 /* Definitions for the buffer stats. */
126 struct bufstats bufstats
;
128 /* Number of delayed write buffers */
132 * Insq/Remq for the buffer hash lists.
135 #define binshash(bp, dp) LIST_INSERT_HEAD(dp, bp, b_hash)
136 #define bremhash(bp) LIST_REMOVE(bp, b_hash)
140 TAILQ_HEAD(ioqueue
, buf
) iobufqueue
;
141 TAILQ_HEAD(bqueues
, buf
) bufqueues
[BQUEUES
];
142 static int needbuffer
;
143 static int need_iobuffer
;
146 * Insq/Remq for the buffer free lists.
148 #define binsheadfree(bp, dp, whichq) do { \
149 TAILQ_INSERT_HEAD(dp, bp, b_freelist); \
151 (bp)->b_whichq = whichq; \
152 (bp)->b_timestamp = time.tv_sec; \
155 #define binstailfree(bp, dp, whichq) do { \
156 TAILQ_INSERT_TAIL(dp, bp, b_freelist); \
158 (bp)->b_whichq = whichq; \
159 (bp)->b_timestamp = time.tv_sec; \
162 #define BHASHENTCHECK(bp) \
163 if ((bp)->b_hash.le_prev != (struct buf **)0xdeadbeef) \
164 panic("%x: b_hash.le_prev is not deadbeef", (bp));
166 #define BLISTNONE(bp) \
167 (bp)->b_hash.le_next = (struct buf *)0; \
168 (bp)->b_hash.le_prev = (struct buf **)0xdeadbeef;
170 simple_lock_data_t bufhashlist_slock
; /* lock on buffer hash list */
172 /* number of per vnode, "in flight" buffer writes */
173 #define BUFWRITE_THROTTLE 9
176 * Time in seconds before a buffer on a list is
177 * considered as a stale buffer
179 #define LRU_IS_STALE 120 /* default value for the LRU */
180 #define AGE_IS_STALE 60 /* default value for the AGE */
181 #define META_IS_STALE 180 /* default value for the BQ_META */
183 int lru_is_stale
= LRU_IS_STALE
;
184 int age_is_stale
= AGE_IS_STALE
;
185 int meta_is_stale
= META_IS_STALE
;
189 blistenterhead(struct bufhashhdr
* head
, struct buf
* bp
)
191 if ((bp
->b_hash
.le_next
= (head
)->lh_first
) != NULL
)
192 (head
)->lh_first
->b_hash
.le_prev
= &(bp
)->b_hash
.le_next
;
193 (head
)->lh_first
= bp
;
194 bp
->b_hash
.le_prev
= &(head
)->lh_first
;
195 if (bp
->b_hash
.le_prev
== (struct buf
**)0xdeadbeef)
196 panic("blistenterhead: le_prev is deadbeef");
203 binshash(struct buf
*bp
, struct bufhashhdr
*dp
)
209 simple_lock(&bufhashlist_slock
);
211 if(incore(bp
->b_vp
, bp
->b_lblkno
)) {
212 panic("adding to queue already existing element");
218 for(; nbp
!= NULL
; nbp
= nbp
->b_hash
.le_next
) {
220 panic("buf already in hashlist");
224 LIST_INSERT_HEAD(dp
, bp
, b_hash
);
226 blistenterhead(dp
, bp
);
228 simple_unlock(&bufhashlist_slock
);
232 bremhash(struct buf
*bp
)
236 simple_lock(&bufhashlist_slock
);
237 if (bp
->b_hash
.le_prev
== (struct buf
**)0xdeadbeef)
238 panic("bremhash le_prev is deadbeef");
239 if (bp
->b_hash
.le_next
== bp
)
240 panic("bremhash: next points to self");
242 if (bp
->b_hash
.le_next
!= NULL
)
243 bp
->b_hash
.le_next
->b_hash
.le_prev
= bp
->b_hash
.le_prev
;
244 *bp
->b_hash
.le_prev
= (bp
)->b_hash
.le_next
;
245 simple_unlock(&bufhashlist_slock
);
252 * Remove a buffer from the free list it's on
258 struct bqueues
*dp
= NULL
;
262 * We only calculate the head of the freelist when removing
263 * the last element of the list as that is the only time that
264 * it is needed (e.g. to reset the tail pointer).
266 * NB: This makes an assumption about how tailq's are implemented.
268 if (bp
->b_freelist
.tqe_next
== NULL
) {
269 for (dp
= bufqueues
; dp
< &bufqueues
[BQUEUES
]; dp
++)
270 if (dp
->tqh_last
== &bp
->b_freelist
.tqe_next
)
272 if (dp
== &bufqueues
[BQUEUES
])
273 panic("bremfree: lost tail");
275 TAILQ_REMOVE(dp
, bp
, b_freelist
);
276 whichq
= bp
->b_whichq
;
282 static __inline__
void
283 bufhdrinit(struct buf
*bp
)
285 bzero((char *)bp
, sizeof *bp
);
287 bp
->b_rcred
= NOCRED
;
288 bp
->b_wcred
= NOCRED
;
289 bp
->b_vnbufs
.le_next
= NOLIST
;
290 bp
->b_flags
= B_INVAL
;
296 * Initialize buffers and hash links for buffers.
301 register struct buf
*bp
;
302 register struct bqueues
*dp
;
306 static void bufzoneinit();
307 static void bcleanbuf_thread_init();
309 /* Initialize the buffer queues ('freelists') and the hash table */
310 for (dp
= bufqueues
; dp
< &bufqueues
[BQUEUES
]; dp
++)
312 bufhashtbl
= hashinit(nbuf
, M_CACHE
, &bufhash
);
314 simple_lock_init(&bufhashlist_slock
);
316 metabuf
= nbuf
/8; /* reserved for meta buf */
318 /* Initialize the buffer headers */
319 for (i
= 0; i
< nbuf
; i
++) {
324 * metabuf buffer headers on the meta-data list and
325 * rest of the buffer headers on the empty list
333 dp
= &bufqueues
[whichq
];
334 binsheadfree(bp
, dp
, whichq
);
335 binshash(bp
, &invalhash
);
338 for (; i
< nbuf
+ niobuf
; i
++) {
341 binsheadfree(bp
, &iobufqueue
, -1);
344 printf("using %d buffer headers and %d cluster IO buffer headers\n",
347 /* Set up zones used by the buffer cache */
350 /* start the bcleanbuf() thread */
351 bcleanbuf_thread_init();
354 /* create a thread to do dynamic buffer queue balancing */
355 bufq_balance_thread_init();
361 bio_doread(vp
, blkno
, size
, cred
, async
, queuetype
)
369 register struct buf
*bp
;
370 struct proc
*p
= current_proc();
372 bp
= getblk(vp
, blkno
, size
, 0, 0, queuetype
);
375 * If buffer does not have data valid, start a read.
376 * Note that if buffer is B_INVAL, getblk() won't return it.
377 * Therefore, it's valid if it's I/O has completed or been delayed.
379 if (!ISSET(bp
->b_flags
, (B_DONE
| B_DELWRI
))) {
380 /* Start I/O for the buffer (keeping credentials). */
381 SET(bp
->b_flags
, B_READ
| async
);
382 if (cred
!= NOCRED
&& bp
->b_rcred
== NOCRED
) {
384 * NFS has embedded ucred.
385 * Can not crhold() here as that causes zone corruption
387 bp
->b_rcred
= crdup(cred
);
391 trace(TR_BREADMISS
, pack(vp
, size
), blkno
);
393 /* Pay for the read. */
395 p
->p_stats
->p_ru
.ru_inblock
++; /* XXX */
400 trace(TR_BREADHIT
, pack(vp
, size
), blkno
);
406 * This algorithm described in Bach (p.54).
409 bread(vp
, blkno
, size
, cred
, bpp
)
416 register struct buf
*bp
;
418 /* Get buffer for block. */
419 bp
= *bpp
= bio_doread(vp
, blkno
, size
, cred
, 0, BLK_READ
);
421 /* Wait for the read to complete, and return result. */
422 return (biowait(bp
));
426 * Read a disk block. [bread() for meta-data]
427 * This algorithm described in Bach (p.54).
430 meta_bread(vp
, blkno
, size
, cred
, bpp
)
437 register struct buf
*bp
;
439 /* Get buffer for block. */
440 bp
= *bpp
= bio_doread(vp
, blkno
, size
, cred
, 0, BLK_META
);
442 /* Wait for the read to complete, and return result. */
443 return (biowait(bp
));
447 * Read-ahead multiple disk blocks. The first is sync, the rest async.
448 * Trivial modification to the breada algorithm presented in Bach (p.55).
451 breadn(vp
, blkno
, size
, rablks
, rasizes
, nrablks
, cred
, bpp
)
453 daddr_t blkno
; int size
;
454 daddr_t rablks
[]; int rasizes
[];
459 register struct buf
*bp
;
462 bp
= *bpp
= bio_doread(vp
, blkno
, size
, cred
, 0, BLK_READ
);
465 * For each of the read-ahead blocks, start a read, if necessary.
467 for (i
= 0; i
< nrablks
; i
++) {
468 /* If it's in the cache, just go on to next one. */
469 if (incore(vp
, rablks
[i
]))
472 /* Get a buffer for the read-ahead block */
473 (void) bio_doread(vp
, rablks
[i
], rasizes
[i
], cred
, B_ASYNC
, BLK_READ
);
476 /* Otherwise, we had to start a read for it; wait until it's valid. */
477 return (biowait(bp
));
481 * Read with single-block read-ahead. Defined in Bach (p.55), but
482 * implemented as a call to breadn().
483 * XXX for compatibility with old file systems.
486 breada(vp
, blkno
, size
, rablkno
, rabsize
, cred
, bpp
)
488 daddr_t blkno
; int size
;
489 daddr_t rablkno
; int rabsize
;
494 return (breadn(vp
, blkno
, size
, &rablkno
, &rabsize
, 1, cred
, bpp
));
498 * Block write. Described in Bach (p.56)
504 int rv
, sync
, wasdelayed
;
505 struct proc
*p
= current_proc();
510 struct vnode
*vp
= bp
->b_vp
;
512 /* Remember buffer type, to switch on it later. */
513 sync
= !ISSET(bp
->b_flags
, B_ASYNC
);
514 wasdelayed
= ISSET(bp
->b_flags
, B_DELWRI
);
515 CLR(bp
->b_flags
, (B_READ
| B_DONE
| B_ERROR
| B_DELWRI
));
521 * If not synchronous, pay for the I/O operation and make
522 * sure the buf is on the correct vnode queue. We have
523 * to do this now, because if we don't, the vnode may not
524 * be properly notified that its I/O has completed.
530 p
->p_stats
->p_ru
.ru_oublock
++; /* XXX */
533 trace(TR_BUFWRITE
, pack(vp
, bp
->b_bcount
), bp
->b_lblkno
);
535 /* Initiate disk write. Make sure the appropriate party is charged. */
536 SET(bp
->b_flags
, B_WRITEINPROG
);
543 * If I/O was synchronous, wait for it to complete.
548 * Pay for the I/O operation, if it's not been paid for, and
549 * make sure it's on the correct vnode queue. (async operatings
550 * were payed for above.)
556 p
->p_stats
->p_ru
.ru_oublock
++; /* XXX */
558 /* Release the buffer. */
569 struct vop_bwrite_args
*ap
;
571 return (bwrite(ap
->a_bp
));
577 * The buffer is marked dirty, but is not queued for I/O.
578 * This routine should be used when the buffer is expected
579 * to be modified again soon, typically a small write that
580 * partially fills a buffer.
582 * NB: magnetic tapes cannot be delayed; they must be
583 * written in the order that the writes are requested.
585 * Described in Leffler, et al. (pp. 208-213).
587 * Note: With the abilitty to allocate additional buffer
588 * headers, we can get in to the situation where "too" many
589 * bdwrite()s can create situation where the kernel can create
590 * buffers faster than the disks can service. Doing a bawrite() in
591 * cases were we have "too many" outstanding bdwrite()s avoids that.
597 struct proc
*p
= current_proc();
598 struct vnode
*vp
= bp
->b_vp
;
601 * If the block hasn't been seen before:
602 * (1) Mark it as having been seen,
603 * (2) Charge for the write.
604 * (3) Make sure it's on its vnode's correct block list,
606 if (!ISSET(bp
->b_flags
, B_DELWRI
)) {
607 SET(bp
->b_flags
, B_DELWRI
);
609 p
->p_stats
->p_ru
.ru_oublock
++; /* XXX */
615 /* If this is a tape block, write it the block now. */
616 if (ISSET(bp
->b_flags
, B_TAPE
)) {
623 * If the vnode has "too many" write operations in progress
624 * wait for them to finish the IO
626 while (vp
->v_numoutput
>= BUFWRITE_THROTTLE
) {
627 vp
->v_flag
|= VTHROTTLED
;
628 (void)tsleep((caddr_t
)&vp
->v_numoutput
, PRIBIO
+ 1, "bdwrite", 0);
632 * If we have too many delayed write buffers,
633 * more than we can "safely" handle, just fall back to
634 * doing the async write
637 panic("bdwrite: Negative nbdwrite");
639 if (nbdwrite
> ((nbuf
/4)*3)) {
644 /* Otherwise, the "write" is done, so mark and release the buffer. */
645 SET(bp
->b_flags
, B_DONE
);
650 * Asynchronous block write; just an asynchronous bwrite().
652 * Note: With the abilitty to allocate additional buffer
653 * headers, we can get in to the situation where "too" many
654 * bawrite()s can create situation where the kernel can create
655 * buffers faster than the disks can service.
656 * We limit the number of "in flight" writes a vnode can have to
663 struct vnode
*vp
= bp
->b_vp
;
667 * If the vnode has "too many" write operations in progress
668 * wait for them to finish the IO
670 while (vp
->v_numoutput
>= BUFWRITE_THROTTLE
) {
671 vp
->v_flag
|= VTHROTTLED
;
672 (void)tsleep((caddr_t
)&vp
->v_numoutput
, PRIBIO
+ 1, "bawrite", 0);
676 SET(bp
->b_flags
, B_ASYNC
);
681 * Release a buffer on to the free lists.
682 * Described in Bach (p. 46).
688 struct bqueues
*bufq
;
692 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 388)) | DBG_FUNC_START
,
693 bp
->b_lblkno
* PAGE_SIZE
, (int)bp
, (int)bp
->b_data
,
696 trace(TR_BRELSE
, pack(bp
->b_vp
, bp
->b_bufsize
), bp
->b_lblkno
);
698 /* IO is done. Cleanup the UPL state */
699 if (!ISSET(bp
->b_flags
, B_META
)
700 && UBCINFOEXISTS(bp
->b_vp
) && bp
->b_bufsize
) {
705 if ( !ISSET(bp
->b_flags
, B_PAGELIST
)) {
706 if ( !ISSET(bp
->b_flags
, B_INVAL
)) {
707 kret
= ubc_create_upl(bp
->b_vp
,
708 ubc_blktooff(bp
->b_vp
, bp
->b_lblkno
),
713 if (kret
!= KERN_SUCCESS
)
714 panic("brelse: Failed to get pagelists");
716 upl_ubc_alias_set(upl
, bp
, 5);
717 #endif /* UBC_DEBUG */
721 upl
= bp
->b_pagelist
;
722 kret
= ubc_upl_unmap(upl
);
724 if (kret
!= KERN_SUCCESS
)
725 panic("kernel_upl_unmap failed");
729 if (bp
->b_flags
& (B_ERROR
| B_INVAL
)) {
730 if (bp
->b_flags
& (B_READ
| B_INVAL
))
731 upl_flags
= UPL_ABORT_DUMP_PAGES
;
734 ubc_upl_abort(upl
, upl_flags
);
736 if (ISSET(bp
->b_flags
, B_NEEDCOMMIT
))
737 upl_flags
= UPL_COMMIT_CLEAR_DIRTY
;
738 else if (ISSET(bp
->b_flags
, B_DELWRI
| B_WASDIRTY
))
739 upl_flags
= UPL_COMMIT_SET_DIRTY
;
741 upl_flags
= UPL_COMMIT_CLEAR_DIRTY
;
742 ubc_upl_commit_range(upl
, 0, bp
->b_bufsize
, upl_flags
|
743 UPL_COMMIT_INACTIVATE
| UPL_COMMIT_FREE_ON_EMPTY
);
746 CLR(bp
->b_flags
, B_PAGELIST
);
751 if(ISSET(bp
->b_flags
, B_PAGELIST
))
752 panic("brelse: pagelist set for non VREG; vp=%x", bp
->b_vp
);
755 /* Wake up any processes waiting for any buffer to become free. */
761 /* Wake up any proceeses waiting for _this_ buffer to become free. */
762 if (ISSET(bp
->b_flags
, B_WANTED
)) {
763 CLR(bp
->b_flags
, B_WANTED
);
767 /* Block disk interrupts. */
771 * Determine which queue the buffer should be on, then put it there.
774 /* If it's locked, don't report an error; try again later. */
775 if (ISSET(bp
->b_flags
, (B_LOCKED
|B_ERROR
)) == (B_LOCKED
|B_ERROR
))
776 CLR(bp
->b_flags
, B_ERROR
);
778 /* If it's not cacheable, or an error, mark it invalid. */
779 if (ISSET(bp
->b_flags
, (B_NOCACHE
|B_ERROR
)))
780 SET(bp
->b_flags
, B_INVAL
);
782 if ((bp
->b_bufsize
<= 0) || ISSET(bp
->b_flags
, B_INVAL
)) {
784 * If it's invalid or empty, dissociate it from its vnode
785 * and put on the head of the appropriate queue.
789 if (ISSET(bp
->b_flags
, B_DELWRI
)) {
790 CLR(bp
->b_flags
, B_DELWRI
);
793 if (bp
->b_bufsize
<= 0)
794 whichq
= BQ_EMPTY
; /* no data */
796 whichq
= BQ_AGE
; /* invalid data */
798 bufq
= &bufqueues
[whichq
];
799 binsheadfree(bp
, bufq
, whichq
);
802 * It has valid data. Put it on the end of the appropriate
803 * queue, so that it'll stick around for as long as possible.
805 if (ISSET(bp
->b_flags
, B_LOCKED
))
806 whichq
= BQ_LOCKED
; /* locked in core */
807 else if (ISSET(bp
->b_flags
, B_META
))
808 whichq
= BQ_META
; /* meta-data */
809 else if (ISSET(bp
->b_flags
, B_AGE
))
810 whichq
= BQ_AGE
; /* stale but valid data */
812 whichq
= BQ_LRU
; /* valid data */
814 bufq
= &bufqueues
[whichq
];
815 binstailfree(bp
, bufq
, whichq
);
818 /* Unlock the buffer. */
819 CLR(bp
->b_flags
, (B_AGE
| B_ASYNC
| B_BUSY
| B_NOCACHE
));
821 /* Allow disk interrupts. */
824 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 388)) | DBG_FUNC_END
,
825 (int)bp
, (int)bp
->b_data
, bp
->b_flags
, 0, 0);
829 * Determine if a block is in the cache.
830 * Just look on what would be its hash chain. If it's there, return
831 * a pointer to it, unless it's marked invalid. If it's marked invalid,
832 * we normally don't return the buffer, unless the caller explicitly
843 bp
= BUFHASH(vp
, blkno
)->lh_first
;
845 /* Search hash chain */
846 for (; bp
!= NULL
; bp
= bp
->b_hash
.le_next
, bufseen
++) {
847 if (bp
->b_lblkno
== blkno
&& bp
->b_vp
== vp
&&
848 !ISSET(bp
->b_flags
, B_INVAL
))
851 panic("walked more than nbuf in incore");
859 /* XXX FIXME -- Update the comment to reflect the UBC changes (please) -- */
861 * Get a block of requested size that is associated with
862 * a given vnode and block offset. If it is found in the
863 * block cache, mark it as having been found, make it busy
864 * and return it. Otherwise, return an empty block of the
865 * correct size. It is up to the caller to insure that the
866 * cached blocks be of the correct size.
869 getblk(vp
, blkno
, size
, slpflag
, slptimeo
, operation
)
870 register struct vnode
*vp
;
872 int size
, slpflag
, slptimeo
, operation
;
882 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 386)) | DBG_FUNC_START
,
883 blkno
* PAGE_SIZE
, size
, operation
, 0, 0);
887 if (bp
= incore(vp
, blkno
)) {
888 /* Found in the Buffer Cache */
889 if (ISSET(bp
->b_flags
, B_BUSY
)) {
895 SET(bp
->b_flags
, B_WANTED
);
896 bufstats
.bufs_busyincore
++;
897 err
= tsleep(bp
, slpflag
| (PRIBIO
+ 1), "getblk",
901 * Callers who call with PCATCH or timeout are
902 * willing to deal with the NULL pointer
904 if (err
&& ((slpflag
& PCATCH
) ||
905 ((err
== EWOULDBLOCK
) && slptimeo
)))
912 /* pagein operation must not use getblk */
913 panic("getblk: pagein for incore busy buffer");
919 /* pageout operation must not use getblk */
920 panic("getblk: pageout for incore busy buffer");
926 panic("getblk: %d unknown operation 1", operation
);
932 SET(bp
->b_flags
, (B_BUSY
| B_CACHE
));
934 bufstats
.bufs_incore
++;
938 if (ISSET(bp
->b_flags
, B_PAGELIST
))
939 panic("pagelist buffer is not busy");
944 if (UBCISVALID(bp
->b_vp
) && bp
->b_bufsize
) {
945 kret
= ubc_create_upl(vp
,
946 ubc_blktooff(vp
, bp
->b_lblkno
),
951 if (kret
!= KERN_SUCCESS
)
952 panic("Failed to get pagelists");
954 SET(bp
->b_flags
, B_PAGELIST
);
955 bp
->b_pagelist
= upl
;
957 if (!upl_valid_page(pl
, 0)) {
958 if (vp
->v_tag
!= VT_NFS
)
959 panic("getblk: incore buffer without valid page");
960 CLR(bp
->b_flags
, B_CACHE
);
963 if (upl_dirty_page(pl
, 0))
964 SET(bp
->b_flags
, B_WASDIRTY
);
966 CLR(bp
->b_flags
, B_WASDIRTY
);
968 kret
= ubc_upl_map(upl
, (vm_address_t
*)&(bp
->b_data
));
969 if (kret
!= KERN_SUCCESS
) {
970 panic("getblk: ubc_upl_map() failed with (%d)",
973 if (bp
->b_data
== 0) panic("ubc_upl_map mapped 0");
979 * VM is not involved in IO for the meta data
980 * buffer already has valid data
983 panic("bp->b_data null incore buf=%x", bp
);
988 panic("getblk: paging operation 1");
992 panic("getblk: %d unknown operation 2", operation
);
997 } else { /* not incore() */
998 int queue
= BQ_EMPTY
; /* Start with no preference */
1001 if ((operation
== BLK_META
) || (UBCINVALID(vp
)) ||
1002 !(UBCINFOEXISTS(vp
))) {
1003 operation
= BLK_META
;
1005 if ((bp
= getnewbuf(slpflag
, slptimeo
, &queue
)) == NULL
)
1007 if (incore(vp
, blkno
)) {
1008 SET(bp
->b_flags
, B_INVAL
);
1009 binshash(bp
, &invalhash
);
1015 * if it is meta, the queue may be set to other
1016 * type so reset as well as mark it to be B_META
1017 * so that when buffer is released it will goto META queue
1018 * Also, if the vnode is not VREG, then it is META
1020 if (operation
== BLK_META
) {
1021 SET(bp
->b_flags
, B_META
);
1025 * Insert in the hash so that incore() can find it
1027 binshash(bp
, BUFHASH(vp
, blkno
));
1031 switch (operation
) {
1033 /* buffer data is invalid */
1035 #if !ZALLOC_METADATA
1037 panic("bp->b_data is not nul; %x",bp
);
1038 kret
= kmem_alloc(kernel_map
,
1039 &bp
->b_data
, bp
->b_bufsize
);
1040 if (kret
!= KERN_SUCCESS
)
1041 panic("getblk: kmem_alloc() returned %d", kret
);
1042 #endif /* ZALLOC_METADATA */
1045 panic("bp->b_data is null %x",bp
);
1047 bp
->b_blkno
= bp
->b_lblkno
= blkno
;
1050 bufstats
.bufs_miss
++;
1052 if (bp
->b_data
== 0)
1053 panic("b_data is 0: 2");
1055 /* wakeup the buffer */
1056 CLR(bp
->b_flags
, B_WANTED
);
1063 if (ISSET(bp
->b_flags
, B_PAGELIST
))
1064 panic("B_PAGELIST in bp=%x",bp
);
1066 kret
= ubc_create_upl(vp
,
1067 ubc_blktooff(vp
, blkno
),
1072 if (kret
!= KERN_SUCCESS
)
1073 panic("Failed to get pagelists");
1076 upl_ubc_alias_set(upl
, bp
, 4);
1077 #endif /* UBC_DEBUG */
1078 bp
->b_blkno
= bp
->b_lblkno
= blkno
;
1079 bp
->b_pagelist
= upl
;
1081 SET(bp
->b_flags
, B_PAGELIST
);
1083 if (upl_valid_page(pl
, 0)) {
1084 SET(bp
->b_flags
, B_CACHE
| B_DONE
);
1085 bufstats
.bufs_vmhits
++;
1087 pagedirty
= upl_dirty_page(pl
, 0);
1090 SET(bp
->b_flags
, B_WASDIRTY
);
1092 if (vp
->v_tag
== VT_NFS
) {
1099 f_offset
= ubc_blktooff(vp
, blkno
);
1101 if (f_offset
> vp
->v_ubcinfo
->ui_size
) {
1102 CLR(bp
->b_flags
, (B_CACHE
|B_DONE
|B_WASDIRTY
));
1106 valid_size
= min(((unsigned int)(vp
->v_ubcinfo
->ui_size
- f_offset
)), PAGE_SIZE
);
1107 bp
->b_validend
= valid_size
;
1110 bp
->b_dirtyend
= valid_size
;
1114 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 386)) | DBG_FUNC_NONE
,
1115 bp
->b_validend
, bp
->b_dirtyend
,
1116 (int)vp
->v_ubcinfo
->ui_size
, 0, 0);
1124 bp
->b_validend
= bp
->b_bcount
;
1125 bp
->b_dirtyend
= bp
->b_bcount
;
1128 bp
->b_validend
= bp
->b_bcount
;
1132 if (error
= VOP_BMAP(vp
, bp
->b_lblkno
, NULL
, &bp
->b_blkno
, NULL
)) {
1133 panic("VOP_BMAP failed in getblk");
1136 * XXX: We probably should invalidate the VM Page
1138 bp
->b_error
= error
;
1139 SET(bp
->b_flags
, (B_ERROR
| B_INVAL
));
1140 /* undo B_DONE that was set before upl_commit() */
1141 CLR(bp
->b_flags
, B_DONE
);
1146 bufstats
.bufs_miss
++;
1148 kret
= ubc_upl_map(upl
, (vm_address_t
*)&(bp
->b_data
));
1149 if (kret
!= KERN_SUCCESS
) {
1150 panic("getblk: ubc_upl_map() "
1151 "failed with (%d)", kret
);
1153 if (bp
->b_data
== 0) panic("kernel_upl_map mapped 0");
1163 panic("getblk: paging operation 2");
1166 panic("getblk: %d unknown operation 3", operation
);
1172 if (bp
->b_data
== NULL
)
1173 panic("getblk: bp->b_addr is null");
1175 if (bp
->b_bufsize
& 0xfff) {
1177 if (ISSET(bp
->b_flags
, B_META
) && (bp
->b_bufsize
& 0x1ff))
1178 #endif /* ZALLOC_METADATA */
1179 panic("getblk: bp->b_bufsize = %d", bp
->b_bufsize
);
1182 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 386)) | DBG_FUNC_END
,
1183 (int)bp
, (int)bp
->b_data
, bp
->b_flags
, 3, 0);
1189 * Get an empty, disassociated buffer of given size.
1196 int queue
= BQ_EMPTY
;
1197 #if !ZALLOC_METADATA
1199 vm_size_t desired_size
= roundup(size
, CLBYTES
);
1201 if (desired_size
> MAXBSIZE
)
1202 panic("geteblk: buffer larger than MAXBSIZE requested");
1203 #endif /* ZALLOC_METADATA */
1205 while ((bp
= getnewbuf(0, 0, &queue
)) == 0)
1208 SET(bp
->b_flags
, (B_META
|B_INVAL
));
1210 SET(bp
->b_flags
, B_INVAL
);
1211 #endif /* ZALLOC_METADATA */
1214 assert(queue
== BQ_EMPTY
);
1215 #endif /* DIAGNOSTIC */
1216 /* XXX need to implement logic to deal with other queues */
1218 #if !ZALLOC_METADATA
1219 /* Empty buffer - allocate pages */
1220 kret
= kmem_alloc_aligned(kernel_map
, &bp
->b_data
, desired_size
);
1221 if (kret
!= KERN_SUCCESS
)
1222 panic("geteblk: kmem_alloc_aligned returned %d", kret
);
1223 #endif /* ZALLOC_METADATA */
1225 binshash(bp
, &invalhash
);
1227 bufstats
.bufs_eblk
++;
1234 * Zones for the meta data buffers
1238 #define MAXMETA 4096
1240 struct meta_zone_entry
{
1247 struct meta_zone_entry meta_zones
[] = {
1248 {NULL
, (MINMETA
* 1), 128 * (MINMETA
* 1), "buf.512" },
1249 {NULL
, (MINMETA
* 2), 64 * (MINMETA
* 2), "buf.1024" },
1250 {NULL
, (MINMETA
* 3), 16 * (MINMETA
* 3), "buf.1536" },
1251 {NULL
, (MINMETA
* 4), 16 * (MINMETA
* 4), "buf.2048" },
1252 {NULL
, (MINMETA
* 5), 16 * (MINMETA
* 5), "buf.2560" },
1253 {NULL
, (MINMETA
* 6), 16 * (MINMETA
* 6), "buf.3072" },
1254 {NULL
, (MINMETA
* 7), 16 * (MINMETA
* 7), "buf.3584" },
1255 {NULL
, (MINMETA
* 8), 512 * (MINMETA
* 8), "buf.4096" },
1256 {NULL
, 0, 0, "" } /* End */
1258 #endif /* ZALLOC_METADATA */
1261 * Initialize the meta data zones
1269 for (i
= 0; meta_zones
[i
].mz_size
!= 0; i
++) {
1270 meta_zones
[i
].mz_zone
=
1271 zinit(meta_zones
[i
].mz_size
,
1272 meta_zones
[i
].mz_max
,
1274 meta_zones
[i
].mz_name
);
1276 #endif /* ZALLOC_METADATA */
1277 buf_hdr_zone
= zinit(sizeof(struct buf
), 32, PAGE_SIZE
, "buf headers");
1282 getbufzone(size_t size
)
1287 panic("getbufzone: incorect size = %d", size
);
1289 i
= (size
/ 512) - 1;
1290 return (meta_zones
[i
].mz_zone
);
1292 #endif /* ZALLOC_METADATA */
1295 * With UBC, there is no need to expand / shrink the file data
1296 * buffer. The VM uses the same pages, hence no waste.
1297 * All the file data buffers can have one size.
1298 * In fact expand / shrink would be an expensive operation.
1300 * Only exception to this is meta-data buffers. Most of the
1301 * meta data operations are smaller than PAGE_SIZE. Having the
1302 * meta-data buffers grow and shrink as needed, optimizes use
1303 * of the kernel wired memory.
1311 vm_size_t desired_size
;
1313 desired_size
= roundup(size
, CLBYTES
);
1315 if(desired_size
< PAGE_SIZE
)
1316 desired_size
= PAGE_SIZE
;
1317 if (desired_size
> MAXBSIZE
)
1318 panic("allocbuf: buffer larger than MAXBSIZE requested");
1321 if (ISSET(bp
->b_flags
, B_META
)) {
1324 size_t nsize
= roundup(size
, MINMETA
);
1327 vm_offset_t elem
= (vm_offset_t
)bp
->b_data
;
1329 if (ISSET(bp
->b_flags
, B_ZALLOC
))
1330 if (bp
->b_bufsize
<= MAXMETA
) {
1331 if (bp
->b_bufsize
< nsize
) {
1332 /* reallocate to a bigger size */
1333 desired_size
= nsize
;
1335 zprev
= getbufzone(bp
->b_bufsize
);
1336 z
= getbufzone(nsize
);
1337 bp
->b_data
= (caddr_t
)zalloc(z
);
1339 panic("allocbuf: zalloc() returned NULL");
1340 bcopy(elem
, bp
->b_data
, bp
->b_bufsize
);
1343 desired_size
= bp
->b_bufsize
;
1346 panic("allocbuf: B_ZALLOC set incorrectly");
1348 if (bp
->b_bufsize
< desired_size
) {
1349 /* reallocate to a bigger size */
1350 kret
= kmem_alloc(kernel_map
, &bp
->b_data
, desired_size
);
1351 if (kret
!= KERN_SUCCESS
)
1352 panic("allocbuf: kmem_alloc() returned %d", kret
);
1354 panic("allocbuf: null b_data");
1355 bcopy(elem
, bp
->b_data
, bp
->b_bufsize
);
1356 kmem_free(kernel_map
, elem
, bp
->b_bufsize
);
1358 desired_size
= bp
->b_bufsize
;
1361 /* new allocation */
1362 if (nsize
<= MAXMETA
) {
1363 desired_size
= nsize
;
1364 z
= getbufzone(nsize
);
1365 bp
->b_data
= (caddr_t
)zalloc(z
);
1367 panic("allocbuf: zalloc() returned NULL 2");
1368 SET(bp
->b_flags
, B_ZALLOC
);
1370 kret
= kmem_alloc(kernel_map
, &bp
->b_data
, desired_size
);
1371 if (kret
!= KERN_SUCCESS
)
1372 panic("allocbuf: kmem_alloc() 2 returned %d", kret
);
1374 panic("allocbuf: null b_data 2");
1379 if (ISSET(bp
->b_flags
, B_META
) && (bp
->b_data
== 0))
1380 panic("allocbuf: bp->b_data is NULL");
1381 #endif /* ZALLOC_METADATA */
1383 bp
->b_bufsize
= desired_size
;
1384 bp
->b_bcount
= size
;
1388 * Get a new buffer from one of the free lists.
1390 * Request for a queue is passes in. The queue from which the buffer was taken
1391 * from is returned. Out of range queue requests get BQ_EMPTY. Request for
1392 * BQUEUE means no preference. Use heuristics in that case.
1393 * Heuristics is as follows:
1394 * Try BQ_AGE, BQ_LRU, BQ_EMPTY, BQ_META in that order.
1395 * If none available block till one is made available.
1396 * If buffers available on both BQ_AGE and BQ_LRU, check the timestamps.
1397 * Pick the most stale buffer.
1398 * If found buffer was marked delayed write, start the async. write
1399 * and restart the search.
1400 * Initialize the fields and disassociate the buffer from the vnode.
1401 * Remove the buffer from the hash. Return the buffer and the queue
1402 * on which it was found.
1406 getnewbuf(slpflag
, slptimeo
, queue
)
1407 int slpflag
, slptimeo
;
1410 register struct buf
*bp
;
1411 register struct buf
*lru_bp
;
1412 register struct buf
*age_bp
;
1413 register struct buf
*meta_bp
;
1414 register int age_time
, lru_time
, bp_time
, meta_time
;
1417 int req
= *queue
; /* save it for restarts */
1422 /* invalid request gets empty queue */
1423 if ((*queue
> BQUEUES
) || (*queue
< 0)
1424 || (*queue
== BQ_LAUNDRY
) || (*queue
== BQ_LOCKED
))
1427 /* (*queue == BQUEUES) means no preference */
1428 if (*queue
!= BQUEUES
) {
1429 /* Try for the requested queue first */
1430 bp
= bufqueues
[*queue
].tqh_first
;
1435 /* Unable to use requested queue */
1436 age_bp
= bufqueues
[BQ_AGE
].tqh_first
;
1437 lru_bp
= bufqueues
[BQ_LRU
].tqh_first
;
1438 meta_bp
= bufqueues
[BQ_META
].tqh_first
;
1440 if (!age_bp
&& !lru_bp
&& !meta_bp
) { /* Unavailble on AGE or LRU */
1441 /* Try the empty list first */
1442 bp
= bufqueues
[BQ_EMPTY
].tqh_first
;
1448 /* Create a new temparory buffer header */
1449 bp
= (struct buf
*)zalloc(buf_hdr_zone
);
1454 binshash(bp
, &invalhash
);
1455 SET(bp
->b_flags
, B_HDRALLOC
);
1457 binsheadfree(bp
, &bufqueues
[BQ_EMPTY
], BQ_EMPTY
);
1462 /* Log this error condition */
1463 printf("getnewbuf: No useful buffers");
1465 /* wait for a free buffer of any kind */
1467 bufstats
.bufs_sleeps
++;
1468 tsleep(&needbuffer
, slpflag
|(PRIBIO
+1), "getnewbuf", slptimeo
);
1473 /* Buffer available either on AGE or LRU or META */
1477 /* Buffer available either on AGE or LRU */
1481 } else if (!lru_bp
) {
1484 } else { /* buffer available on both AGE and LRU */
1485 age_time
= time
.tv_sec
- age_bp
->b_timestamp
;
1486 lru_time
= time
.tv_sec
- lru_bp
->b_timestamp
;
1487 if ((age_time
< 0) || (lru_time
< 0)) { /* time set backwards */
1491 * we should probably re-timestamp eveything in the
1492 * queues at this point with the current time
1495 if ((lru_time
>= lru_is_stale
) && (age_time
< age_is_stale
)) {
1505 if (!bp
) { /* Neither on AGE nor on LRU */
1508 } else if (meta_bp
) {
1509 bp_time
= time
.tv_sec
- bp
->b_timestamp
;
1510 meta_time
= time
.tv_sec
- meta_bp
->b_timestamp
;
1512 if (!(bp_time
< 0) && !(meta_time
< 0)) {
1513 /* time not set backwards */
1515 bp_is_stale
= (*queue
== BQ_LRU
) ?
1516 lru_is_stale
: age_is_stale
;
1518 if ((meta_time
>= meta_is_stale
) &&
1519 (bp_time
< bp_is_stale
)) {
1527 panic("getnewbuf: null bp");
1530 if (bp
->b_hash
.le_prev
== (struct buf
**)0xdeadbeef)
1531 panic("getnewbuf: le_prev is deadbeef");
1533 if(ISSET(bp
->b_flags
, B_BUSY
))
1534 panic("getnewbuf reusing BUSY buf");
1537 if (bcleanbuf(bp
)) {
1538 /* bawrite() issued, buffer not ready */
1546 #include <mach/mach_types.h>
1547 #include <mach/memory_object_types.h>
1551 * Returns 0 is buffer is ready to use,
1552 * Returns 1 if issued a bawrite() to indicate
1553 * that the buffer is not ready.
1556 bcleanbuf(struct buf
*bp
)
1564 /* Remove from the queue */
1567 /* Buffer is no longer on free lists. */
1568 SET(bp
->b_flags
, B_BUSY
);
1570 /* Check whether the buffer header was "allocated" */
1571 if (ISSET(bp
->b_flags
, B_HDRALLOC
))
1574 if (bp
->b_hash
.le_prev
== (struct buf
**)0xdeadbeef)
1575 panic("bcleanbuf: le_prev is deadbeef");
1578 * If buffer was a delayed write, start the IO by queuing
1579 * it on the LAUNDRY queue, and return 1
1581 if (ISSET(bp
->b_flags
, B_DELWRI
)) {
1583 binstailfree(bp
, &bufqueues
[BQ_LAUNDRY
], BQ_LAUNDRY
);
1585 wakeup(&blaundrycnt
);
1596 if (ISSET(bp
->b_flags
, B_META
)) {
1598 vm_offset_t elem
= (vm_offset_t
)bp
->b_data
;
1600 panic("bcleanbuf: NULL bp->b_data B_META buffer");
1602 if (ISSET(bp
->b_flags
, B_ZALLOC
)) {
1603 if (bp
->b_bufsize
<= MAXMETA
) {
1606 z
= getbufzone(bp
->b_bufsize
);
1607 bp
->b_data
= (caddr_t
)0xdeadbeef;
1609 CLR(bp
->b_flags
, B_ZALLOC
);
1611 panic("bcleanbuf: B_ZALLOC set incorrectly");
1613 bp
->b_data
= (caddr_t
)0xdeadbeef;
1614 kmem_free(kernel_map
, elem
, bp
->b_bufsize
);
1617 if (bp
->b_data
== 0)
1618 panic("bcleanbuf: bp->b_data == NULL for B_META buffer");
1620 kmem_free(kernel_map
, bp
->b_data
, bp
->b_bufsize
);
1621 #endif /* ZALLOC_METADATA */
1624 trace(TR_BRELSE
, pack(bp
->b_vp
, bp
->b_bufsize
), bp
->b_lblkno
);
1626 /* disassociate us from our vnode, if we had one... */
1629 /* clear out various other fields */
1632 bp
->b_flags
= B_BUSY
;
1634 SET(bp
->b_flags
, B_HDRALLOC
);
1636 bp
->b_blkno
= bp
->b_lblkno
= 0;
1641 bp
->b_dirtyoff
= bp
->b_dirtyend
= 0;
1642 bp
->b_validoff
= bp
->b_validend
= 0;
1644 /* nuke any credentials we were holding */
1646 if (cred
!= NOCRED
) {
1647 bp
->b_rcred
= NOCRED
;
1651 if (cred
!= NOCRED
) {
1652 bp
->b_wcred
= NOCRED
;
1661 * Wait for operations on the buffer to complete.
1662 * When they do, extract and return the I/O's error value.
1669 upl_page_info_t
*pl
;
1674 while (!ISSET(bp
->b_flags
, B_DONE
))
1675 tsleep(bp
, PRIBIO
+ 1, "biowait", 0);
1678 /* check for interruption of I/O (e.g. via NFS), then errors. */
1679 if (ISSET(bp
->b_flags
, B_EINTR
)) {
1680 CLR(bp
->b_flags
, B_EINTR
);
1682 } else if (ISSET(bp
->b_flags
, B_ERROR
))
1683 return (bp
->b_error
? bp
->b_error
: EIO
);
1689 * Mark I/O complete on a buffer.
1691 * If a callback has been requested, e.g. the pageout
1692 * daemon, do so. Otherwise, awaken waiting processes.
1694 * [ Leffler, et al., says on p.247:
1695 * "This routine wakes up the blocked process, frees the buffer
1696 * for an asynchronous write, or, for a request by the pagedaemon
1697 * process, invokes a procedure specified in the buffer structure" ]
1699 * In real life, the pagedaemon (or other system processes) wants
1700 * to do async stuff to, and doesn't want the buffer brelse()'d.
1701 * (for swap pager, that puts swap buffers on the free lists (!!!),
1702 * for the vn device, that puts malloc'd buffers on the free lists!)
1708 boolean_t funnel_state
;
1711 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
1713 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 387)) | DBG_FUNC_START
,
1714 (int)bp
, (int)bp
->b_data
, bp
->b_flags
, 0, 0);
1716 if (ISSET(bp
->b_flags
, B_DONE
))
1717 panic("biodone already");
1718 SET(bp
->b_flags
, B_DONE
); /* note that it's done */
1720 * I/O was done, so don't believe
1721 * the DIRTY state from VM anymore
1723 CLR(bp
->b_flags
, B_WASDIRTY
);
1725 if (!ISSET(bp
->b_flags
, B_READ
) && !ISSET(bp
->b_flags
, B_RAW
))
1726 vwakeup(bp
); /* wake up reader */
1728 /* Wakeup the throttled write operations as needed */
1731 && (vp
->v_flag
& VTHROTTLED
)
1732 && (vp
->v_numoutput
<= (BUFWRITE_THROTTLE
/ 3))) {
1733 vp
->v_flag
&= ~VTHROTTLED
;
1734 wakeup((caddr_t
)&vp
->v_numoutput
);
1737 if (ISSET(bp
->b_flags
, B_CALL
)) { /* if necessary, call out */
1738 CLR(bp
->b_flags
, B_CALL
); /* but note callout done */
1739 (*bp
->b_iodone
)(bp
);
1740 } else if (ISSET(bp
->b_flags
, B_ASYNC
)) /* if async, release it */
1742 else { /* or just wakeup the buffer */
1743 CLR(bp
->b_flags
, B_WANTED
);
1747 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 387)) | DBG_FUNC_END
,
1748 (int)bp
, (int)bp
->b_data
, bp
->b_flags
, 0, 0);
1750 thread_funnel_set(kernel_flock
, funnel_state
);
1754 * Return a count of buffers on the "locked" queue.
1759 register struct buf
*bp
;
1762 for (bp
= bufqueues
[BQ_LOCKED
].tqh_first
; bp
;
1763 bp
= bp
->b_freelist
.tqe_next
)
1769 * Return a count of 'busy' buffers. Used at the time of shutdown.
1772 count_busy_buffers()
1774 register struct buf
*bp
;
1775 register int nbusy
= 0;
1777 for (bp
= &buf
[nbuf
]; --bp
>= buf
; )
1778 if ((bp
->b_flags
& (B_BUSY
|B_INVAL
)) == B_BUSY
)
1783 #if 1 /*DIAGNOSTIC */
1785 * Print out statistics on the current allocation of the buffer pool.
1786 * Can be enabled to print out on every ``sync'' by setting "syncprt"
1787 * in vfs_syscalls.c using sysctl.
1793 register struct buf
*bp
;
1794 register struct bqueues
*dp
;
1795 int counts
[MAXBSIZE
/CLBYTES
+1];
1796 static char *bname
[BQUEUES
] =
1797 { "LOCKED", "LRU", "AGE", "EMPTY", "META", "LAUNDRY" };
1799 for (dp
= bufqueues
, i
= 0; dp
< &bufqueues
[BQUEUES
]; dp
++, i
++) {
1801 for (j
= 0; j
<= MAXBSIZE
/CLBYTES
; j
++)
1804 for (bp
= dp
->tqh_first
; bp
; bp
= bp
->b_freelist
.tqe_next
) {
1805 counts
[bp
->b_bufsize
/CLBYTES
]++;
1809 printf("%s: total-%d", bname
[i
], count
);
1810 for (j
= 0; j
<= MAXBSIZE
/CLBYTES
; j
++)
1812 printf(", %d-%d", j
* CLBYTES
, counts
[j
]);
1816 #endif /* DIAGNOSTIC */
1818 #define NRESERVEDIOBUFS 16
1821 alloc_io_buf(vp
, priv
)
1825 register struct buf
*bp
;
1830 while (niobuf
- NRESERVEDIOBUFS
< bufstats
.bufs_iobufinuse
&& !priv
) {
1832 bufstats
.bufs_iobufsleeps
++;
1833 (void) tsleep(&need_iobuffer
, (PRIBIO
+1), "alloc_io_buf", 0);
1836 while ((bp
= iobufqueue
.tqh_first
) == NULL
) {
1838 bufstats
.bufs_iobufsleeps
++;
1839 (void) tsleep(&need_iobuffer
, (PRIBIO
+1), "alloc_io_buf1", 0);
1842 TAILQ_REMOVE(&iobufqueue
, bp
, b_freelist
);
1843 bp
->b_timestamp
= 0;
1845 /* clear out various fields */
1846 bp
->b_flags
= B_BUSY
;
1847 bp
->b_blkno
= bp
->b_lblkno
= 0;
1855 if (vp
->v_type
== VBLK
|| vp
->v_type
== VCHR
)
1856 bp
->b_dev
= vp
->v_rdev
;
1859 bufstats
.bufs_iobufinuse
++;
1860 if (bufstats
.bufs_iobufinuse
> bufstats
.bufs_iobufmax
)
1861 bufstats
.bufs_iobufmax
= bufstats
.bufs_iobufinuse
;
1874 /* put buffer back on the head of the iobufqueue */
1876 bp
->b_flags
= B_INVAL
;
1878 binsheadfree(bp
, &iobufqueue
, -1);
1880 /* Wake up any processes waiting for any buffer to become free. */
1881 if (need_iobuffer
) {
1883 wakeup(&need_iobuffer
);
1885 bufstats
.bufs_iobufinuse
--;
1890 /* not hookedup yet */
1892 /* XXX move this to a separate file */
1894 * Dynamic Scaling of the Buffer Queues
1897 typedef long long blsize_t
;
1899 blsize_t MAXNBUF
; /* initialize to (mem_size / PAGE_SIZE) */
1900 /* Global tunable limits */
1901 blsize_t nbufh
; /* number of buffer headers */
1902 blsize_t nbuflow
; /* minimum number of buffer headers required */
1903 blsize_t nbufhigh
; /* maximum number of buffer headers allowed */
1904 blsize_t nbuftarget
; /* preferred number of buffer headers */
1909 * 1. 0 < nbuflow <= nbufh <= nbufhigh
1910 * 2. nbufhigh <= MAXNBUF
1911 * 3. 0 < nbuflow <= nbuftarget <= nbufhigh
1912 * 4. nbufh can not be set by sysctl().
1915 /* Per queue tunable limits */
1918 blsize_t bl_nlow
; /* minimum number of buffer headers required */
1919 blsize_t bl_num
; /* number of buffer headers on the queue */
1920 blsize_t bl_nlhigh
; /* maximum number of buffer headers allowed */
1921 blsize_t bl_target
; /* preferred number of buffer headers */
1922 long bl_stale
; /* Seconds after which a buffer is considered stale */
1928 * 1. 0 <= bl_nlow <= bl_num <= bl_nlhigh
1929 * 2. bl_nlhigh <= MAXNBUF
1930 * 3. bufqlim[BQ_META].bl_nlow != 0
1931 * 4. bufqlim[BQ_META].bl_nlow > (number of possible concurrent
1932 * file system IO operations)
1933 * 5. bl_num can not be set by sysctl().
1934 * 6. bl_nhigh <= nbufhigh
1940 * Defining it blsize_t as long permits 2^31 buffer headers per queue.
1941 * Which can describe (2^31 * PAGE_SIZE) memory per queue.
1943 * These limits are exported to by means of sysctl().
1944 * It was decided to define blsize_t as a 64 bit quantity.
1945 * This will make sure that we will not be required to change it
1946 * as long as we do not exceed 64 bit address space for the kernel.
1948 * low and high numbers parameters initialized at compile time
1949 * and boot arguments can be used to override them. sysctl()
1950 * would not change the value. sysctl() can get all the values
1951 * but can set only target. num is the current level.
1953 * Advantages of having a "bufqscan" thread doing the balancing are,
1954 * Keep enough bufs on BQ_EMPTY.
1955 * getnewbuf() by default will always select a buffer from the BQ_EMPTY.
1956 * getnewbuf() perfoms best if a buffer was found there.
1957 * Also this minimizes the possibility of starting IO
1958 * from getnewbuf(). That's a performance win, too.
1960 * Localize complex logic [balancing as well as time aging]
1963 * Simplify getnewbuf() logic by elimination of time aging code.
1969 * The goal of the dynamic scaling of the buffer queues to to keep
1970 * the size of the LRU close to bl_target. Buffers on a queue would
1973 * There would be a thread which will be responsible for "balancing"
1974 * the buffer cache queues.
1976 * The scan order would be: AGE, LRU, META, EMPTY.
1979 long bufqscanwait
= 0;
1981 extern void bufqscan_thread();
1982 extern int balancebufq(int q
);
1983 extern int btrimempty(int n
);
1984 extern int initbufqscan(void);
1985 extern int nextbufq(int q
);
1986 extern void buqlimprt(int all
);
1989 bufq_balance_thread_init()
1992 if (bufqscanwait
++ == 0) {
1995 /* Initalize globals */
1996 MAXNBUF
= (mem_size
/ PAGE_SIZE
);
1998 nbuflow
= min(nbufh
, 100);
1999 nbufhigh
= min(MAXNBUF
, max(nbufh
, 2048));
2000 nbuftarget
= (mem_size
>> 5) / PAGE_SIZE
;
2001 nbuftarget
= max(nbuflow
, nbuftarget
);
2002 nbuftarget
= min(nbufhigh
, nbuftarget
);
2005 * Initialize the bufqlim
2009 bufqlim
[BQ_LOCKED
].bl_nlow
= 0;
2010 bufqlim
[BQ_LOCKED
].bl_nlhigh
= 32;
2011 bufqlim
[BQ_LOCKED
].bl_target
= 0;
2012 bufqlim
[BQ_LOCKED
].bl_stale
= 30;
2015 bufqlim
[BQ_LRU
].bl_nlow
= 0;
2016 bufqlim
[BQ_LRU
].bl_nlhigh
= nbufhigh
/4;
2017 bufqlim
[BQ_LRU
].bl_target
= nbuftarget
/4;
2018 bufqlim
[BQ_LRU
].bl_stale
= LRU_IS_STALE
;
2021 bufqlim
[BQ_AGE
].bl_nlow
= 0;
2022 bufqlim
[BQ_AGE
].bl_nlhigh
= nbufhigh
/4;
2023 bufqlim
[BQ_AGE
].bl_target
= nbuftarget
/4;
2024 bufqlim
[BQ_AGE
].bl_stale
= AGE_IS_STALE
;
2027 bufqlim
[BQ_EMPTY
].bl_nlow
= 0;
2028 bufqlim
[BQ_EMPTY
].bl_nlhigh
= nbufhigh
/4;
2029 bufqlim
[BQ_EMPTY
].bl_target
= nbuftarget
/4;
2030 bufqlim
[BQ_EMPTY
].bl_stale
= 600000;
2033 bufqlim
[BQ_META
].bl_nlow
= 0;
2034 bufqlim
[BQ_META
].bl_nlhigh
= nbufhigh
/4;
2035 bufqlim
[BQ_META
].bl_target
= nbuftarget
/4;
2036 bufqlim
[BQ_META
].bl_stale
= META_IS_STALE
;
2039 bufqlim
[BQ_LOCKED
].bl_nlow
= 0;
2040 bufqlim
[BQ_LOCKED
].bl_nlhigh
= 32;
2041 bufqlim
[BQ_LOCKED
].bl_target
= 0;
2042 bufqlim
[BQ_LOCKED
].bl_stale
= 30;
2047 /* create worker thread */
2048 kernel_thread(kernel_task
, bufqscan_thread
);
2051 /* The workloop for the buffer balancing thread */
2055 boolean_t funnel_state
;
2058 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
2062 int q
; /* buffer queue to process */
2064 for (q
= initbufqscan(); q
; ) {
2065 moretodo
|= balancebufq(q
);
2074 (void)tsleep((void *)&bufqscanwait
, PRIBIO
, "bufqscanwait", 60 * hz
);
2078 (void) thread_funnel_set(kernel_flock
, FALSE
);
2081 /* Seed for the buffer queue balancing */
2085 /* Start with AGE queue */
2089 /* Pick next buffer queue to balance */
2093 int order
[] = { BQ_AGE
, BQ_LRU
, BQ_META
, BQ_EMPTY
, 0 };
2100 /* function to balance the buffer queues */
2108 /* reject invalid q */
2109 if ((q
< 0) || (q
>= BQUEUES
))
2112 /* LOCKED or LAUNDRY queue MUST not be balanced */
2113 if ((q
== BQ_LOCKED
) || (q
== BQ_LAUNDRY
))
2116 n
= (bufqlim
[q
].bl_num
- bufqlim
[q
].bl_target
);
2118 /* If queue has less than target nothing more to do */
2123 /* Balance only a small amount (12.5%) at a time */
2127 /* EMPTY queue needs special handling */
2128 if (q
== BQ_EMPTY
) {
2129 moretodo
|= btrimempty(n
);
2133 for (; n
> 0; n
--) {
2134 struct buf
*bp
= bufqueues
[q
].tqh_first
;
2138 /* check if it's stale */
2139 if ((time
.tv_sec
- bp
->b_timestamp
) > bufqlim
[q
].bl_stale
) {
2140 if (bcleanbuf(bp
)) {
2141 /* bawrite() issued, bp not ready */
2144 /* release the cleaned buffer to BQ_EMPTY */
2145 SET(bp
->b_flags
, B_INVAL
);
2161 * When struct buf are allocated dynamically, this would
2162 * reclaim upto 'n' struct buf from the empty queue.
2171 if ((q
< 0) || (q
>= BQUEUES
))
2174 bufqlim
[q
].bl_num
++;
2181 if ((q
< 0) || (q
>= BQUEUES
))
2184 bufqlim
[q
].bl_num
--;
2192 static char *bname
[BQUEUES
] =
2193 { "LOCKED", "LRU", "AGE", "EMPTY", "META", "LAUNDRY" };
2196 for (i
= 0; i
< BQUEUES
; i
++) {
2197 printf("%s : ", bname
[i
]);
2198 printf("min = %d, ", (long)bufqlim
[i
].bl_nlow
);
2199 printf("cur = %d, ", (long)bufqlim
[i
].bl_num
);
2200 printf("max = %d, ", (long)bufqlim
[i
].bl_nlhigh
);
2201 printf("target = %d, ", (long)bufqlim
[i
].bl_target
);
2202 printf("stale after %d seconds\n", bufqlim
[i
].bl_stale
);
2205 for (i
= 0; i
< BQUEUES
; i
++) {
2206 printf("%s : ", bname
[i
]);
2207 printf("cur = %d, ", (long)bufqlim
[i
].bl_num
);
2212 * If the getnewbuf() calls bcleanbuf() on the same thread
2213 * there is a potential for stack overrun and deadlocks.
2214 * So we always handoff the work to worker thread for completion
2218 bcleanbuf_thread_init()
2220 static void bcleanbuf_thread();
2222 /* create worker thread */
2223 kernel_thread(kernel_task
, bcleanbuf_thread
);
2229 boolean_t funnel_state
;
2232 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
2235 while (blaundrycnt
== 0)
2236 (void)tsleep((void *)&blaundrycnt
, PRIBIO
, "blaundry", 60 * hz
);
2237 bp
= TAILQ_FIRST(&bufqueues
[BQ_LAUNDRY
]);
2238 /* Remove from the queue */
2246 (void) thread_funnel_set(kernel_flock
, funnel_state
);