vfs_bio.c revision 1.191 1 1.191 yamt /* $NetBSD: vfs_bio.c,v 1.191 2008/03/23 10:33:15 yamt Exp $ */
2 1.183 ad
3 1.183 ad /*-
4 1.183 ad * Copyright (c) 2007 The NetBSD Foundation, Inc.
5 1.183 ad * All rights reserved.
6 1.183 ad *
7 1.183 ad * This code is derived from software contributed to The NetBSD Foundation
8 1.183 ad * by Andrew Doran.
9 1.183 ad *
10 1.183 ad * Redistribution and use in source and binary forms, with or without
11 1.183 ad * modification, are permitted provided that the following conditions
12 1.183 ad * are met:
13 1.183 ad * 1. Redistributions of source code must retain the above copyright
14 1.183 ad * notice, this list of conditions and the following disclaimer.
15 1.183 ad * 2. Redistributions in binary form must reproduce the above copyright
16 1.183 ad * notice, this list of conditions and the following disclaimer in the
17 1.183 ad * documentation and/or other materials provided with the distribution.
18 1.183 ad * 3. All advertising materials mentioning features or use of this software
19 1.183 ad * must display the following acknowledgement:
20 1.183 ad * This product includes software developed by the NetBSD
21 1.183 ad * Foundation, Inc. and its contributors.
22 1.183 ad * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.183 ad * contributors may be used to endorse or promote products derived
24 1.183 ad * from this software without specific prior written permission.
25 1.183 ad *
26 1.183 ad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.183 ad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.183 ad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.183 ad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.183 ad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.183 ad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.183 ad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.183 ad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.183 ad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.183 ad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.183 ad * POSSIBILITY OF SUCH DAMAGE.
37 1.183 ad */
38 1.31 cgd
39 1.31 cgd /*-
40 1.31 cgd * Copyright (c) 1982, 1986, 1989, 1993
41 1.31 cgd * The Regents of the University of California. All rights reserved.
42 1.31 cgd * (c) UNIX System Laboratories, Inc.
43 1.31 cgd * All or some portions of this file are derived from material licensed
44 1.31 cgd * to the University of California by American Telephone and Telegraph
45 1.31 cgd * Co. or Unix System Laboratories, Inc. and are reproduced herein with
46 1.31 cgd * the permission of UNIX System Laboratories, Inc.
47 1.31 cgd *
48 1.31 cgd * Redistribution and use in source and binary forms, with or without
49 1.31 cgd * modification, are permitted provided that the following conditions
50 1.31 cgd * are met:
51 1.31 cgd * 1. Redistributions of source code must retain the above copyright
52 1.31 cgd * notice, this list of conditions and the following disclaimer.
53 1.31 cgd * 2. Redistributions in binary form must reproduce the above copyright
54 1.31 cgd * notice, this list of conditions and the following disclaimer in the
55 1.31 cgd * documentation and/or other materials provided with the distribution.
56 1.93 agc * 3. Neither the name of the University nor the names of its contributors
57 1.93 agc * may be used to endorse or promote products derived from this software
58 1.93 agc * without specific prior written permission.
59 1.93 agc *
60 1.93 agc * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
61 1.93 agc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
62 1.93 agc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
63 1.93 agc * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
64 1.93 agc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
65 1.93 agc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
66 1.93 agc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
67 1.93 agc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
68 1.93 agc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
69 1.93 agc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
70 1.93 agc * SUCH DAMAGE.
71 1.93 agc *
72 1.93 agc * @(#)vfs_bio.c 8.6 (Berkeley) 1/11/94
73 1.93 agc */
74 1.93 agc
75 1.93 agc /*-
76 1.93 agc * Copyright (c) 1994 Christopher G. Demetriou
77 1.93 agc *
78 1.93 agc * Redistribution and use in source and binary forms, with or without
79 1.93 agc * modification, are permitted provided that the following conditions
80 1.93 agc * are met:
81 1.93 agc * 1. Redistributions of source code must retain the above copyright
82 1.93 agc * notice, this list of conditions and the following disclaimer.
83 1.93 agc * 2. Redistributions in binary form must reproduce the above copyright
84 1.93 agc * notice, this list of conditions and the following disclaimer in the
85 1.93 agc * documentation and/or other materials provided with the distribution.
86 1.31 cgd * 3. All advertising materials mentioning features or use of this software
87 1.31 cgd * must display the following acknowledgement:
88 1.31 cgd * This product includes software developed by the University of
89 1.31 cgd * California, Berkeley and its contributors.
90 1.31 cgd * 4. Neither the name of the University nor the names of its contributors
91 1.31 cgd * may be used to endorse or promote products derived from this software
92 1.31 cgd * without specific prior written permission.
93 1.31 cgd *
94 1.31 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
95 1.31 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
96 1.31 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
97 1.31 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
98 1.31 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
99 1.31 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
100 1.31 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
101 1.31 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
102 1.31 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
103 1.31 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
104 1.31 cgd * SUCH DAMAGE.
105 1.31 cgd *
106 1.31 cgd * @(#)vfs_bio.c 8.6 (Berkeley) 1/11/94
107 1.31 cgd */
108 1.31 cgd
109 1.31 cgd /*
110 1.31 cgd * Some references:
111 1.31 cgd * Bach: The Design of the UNIX Operating System (Prentice Hall, 1986)
112 1.31 cgd * Leffler, et al.: The Design and Implementation of the 4.3BSD
113 1.31 cgd * UNIX Operating System (Addison Welley, 1989)
114 1.31 cgd */
115 1.77 lukem
116 1.178 dsl #include <sys/cdefs.h>
117 1.191 yamt __KERNEL_RCSID(0, "$NetBSD: vfs_bio.c,v 1.191 2008/03/23 10:33:15 yamt Exp $");
118 1.178 dsl
119 1.159 uwe #include "fs_ffs.h"
120 1.100 pk #include "opt_bufcache.h"
121 1.81 matt
122 1.31 cgd #include <sys/param.h>
123 1.31 cgd #include <sys/systm.h>
124 1.100 pk #include <sys/kernel.h>
125 1.31 cgd #include <sys/proc.h>
126 1.31 cgd #include <sys/buf.h>
127 1.31 cgd #include <sys/vnode.h>
128 1.31 cgd #include <sys/mount.h>
129 1.31 cgd #include <sys/malloc.h>
130 1.31 cgd #include <sys/resourcevar.h>
131 1.100 pk #include <sys/sysctl.h>
132 1.35 mycroft #include <sys/conf.h>
133 1.160 elad #include <sys/kauth.h>
134 1.183 ad #include <sys/intr.h>
135 1.183 ad #include <sys/cpu.h>
136 1.40 christos
137 1.73 chs #include <uvm/uvm.h>
138 1.71 thorpej
139 1.59 fvdl #include <miscfs/specfs/specdev.h>
140 1.59 fvdl
141 1.100 pk #ifndef BUFPAGES
142 1.100 pk # define BUFPAGES 0
143 1.100 pk #endif
144 1.100 pk
145 1.100 pk #ifdef BUFCACHE
146 1.100 pk # if (BUFCACHE < 5) || (BUFCACHE > 95)
147 1.100 pk # error BUFCACHE is not between 5 and 95
148 1.100 pk # endif
149 1.100 pk #else
150 1.114 tls # define BUFCACHE 15
151 1.100 pk #endif
152 1.100 pk
153 1.100 pk u_int nbuf; /* XXX - for softdep_lockedbufs */
154 1.100 pk u_int bufpages = BUFPAGES; /* optional hardwired count */
155 1.100 pk u_int bufcache = BUFCACHE; /* max % of RAM to use for buffer cache */
156 1.100 pk
157 1.130 yamt /* Function prototypes */
158 1.131 yamt struct bqueue;
159 1.130 yamt
160 1.135 enami static void buf_setwm(void);
161 1.130 yamt static int buf_trim(void);
162 1.130 yamt static void *bufpool_page_alloc(struct pool *, int);
163 1.130 yamt static void bufpool_page_free(struct pool *, void *);
164 1.183 ad static buf_t *bio_doread(struct vnode *, daddr_t, int,
165 1.160 elad kauth_cred_t, int);
166 1.183 ad static buf_t *getnewbuf(int, int, int);
167 1.130 yamt static int buf_lotsfree(void);
168 1.130 yamt static int buf_canrelease(void);
169 1.183 ad static u_long buf_mempoolidx(u_long);
170 1.183 ad static u_long buf_roundsize(u_long);
171 1.183 ad static void *buf_malloc(size_t);
172 1.170 christos static void buf_mrelease(void *, size_t);
173 1.183 ad static void binsheadfree(buf_t *, struct bqueue *);
174 1.183 ad static void binstailfree(buf_t *, struct bqueue *);
175 1.130 yamt int count_lock_queue(void); /* XXX */
176 1.130 yamt #ifdef DEBUG
177 1.183 ad static int checkfreelist(buf_t *, struct bqueue *);
178 1.130 yamt #endif
179 1.183 ad static void biointr(void *);
180 1.183 ad static void biodone2(buf_t *);
181 1.183 ad static void bref(buf_t *);
182 1.183 ad static void brele(buf_t *);
183 1.100 pk
184 1.31 cgd /*
185 1.31 cgd * Definitions for the buffer hash lists.
186 1.31 cgd */
187 1.31 cgd #define BUFHASH(dvp, lbn) \
188 1.73 chs (&bufhashtbl[(((long)(dvp) >> 8) + (int)(lbn)) & bufhash])
189 1.31 cgd LIST_HEAD(bufhashhdr, buf) *bufhashtbl, invalhash;
190 1.31 cgd u_long bufhash;
191 1.189 matt struct bqueue bufqueues[BQUEUES];
192 1.189 matt const struct bio_ops *bioopsp; /* I/O operation notification */
193 1.183 ad
194 1.183 ad static kcondvar_t needbuffer_cv;
195 1.31 cgd
196 1.31 cgd /*
197 1.87 pk * Buffer queue lock.
198 1.87 pk */
199 1.183 ad kmutex_t bufcache_lock;
200 1.183 ad kmutex_t buffer_lock;
201 1.87 pk
202 1.183 ad /* Software ISR for completed transfers. */
203 1.183 ad static void *biodone_sih;
204 1.153 yamt
205 1.183 ad /* Buffer pool for I/O buffers. */
206 1.183 ad static pool_cache_t buf_cache;
207 1.183 ad static pool_cache_t bufio_cache;
208 1.65 thorpej
209 1.100 pk /* XXX - somewhat gross.. */
210 1.100 pk #if MAXBSIZE == 0x2000
211 1.158 tls #define NMEMPOOLS 5
212 1.100 pk #elif MAXBSIZE == 0x4000
213 1.158 tls #define NMEMPOOLS 6
214 1.100 pk #elif MAXBSIZE == 0x8000
215 1.158 tls #define NMEMPOOLS 7
216 1.100 pk #else
217 1.158 tls #define NMEMPOOLS 8
218 1.100 pk #endif
219 1.100 pk
220 1.158 tls #define MEMPOOL_INDEX_OFFSET 9 /* smallest pool is 512 bytes */
221 1.100 pk #if (1 << (NMEMPOOLS + MEMPOOL_INDEX_OFFSET - 1)) != MAXBSIZE
222 1.100 pk #error update vfs_bio buffer memory parameters
223 1.100 pk #endif
224 1.100 pk
225 1.100 pk /* Buffer memory pools */
226 1.101 thorpej static struct pool bmempools[NMEMPOOLS];
227 1.100 pk
228 1.191 yamt static struct vm_map *buf_map;
229 1.100 pk
230 1.100 pk /*
231 1.100 pk * Buffer memory pool allocator.
232 1.100 pk */
233 1.101 thorpej static void *
234 1.166 yamt bufpool_page_alloc(struct pool *pp, int flags)
235 1.100 pk {
236 1.111 yamt
237 1.144 yamt return (void *)uvm_km_alloc(buf_map,
238 1.144 yamt MAXBSIZE, MAXBSIZE,
239 1.144 yamt ((flags & PR_WAITOK) ? 0 : UVM_KMF_NOWAIT | UVM_KMF_TRYLOCK)
240 1.144 yamt | UVM_KMF_WIRED);
241 1.100 pk }
242 1.100 pk
243 1.101 thorpej static void
244 1.166 yamt bufpool_page_free(struct pool *pp, void *v)
245 1.100 pk {
246 1.144 yamt
247 1.144 yamt uvm_km_free(buf_map, (vaddr_t)v, MAXBSIZE, UVM_KMF_WIRED);
248 1.100 pk }
249 1.100 pk
250 1.101 thorpej static struct pool_allocator bufmempool_allocator = {
251 1.162 christos .pa_alloc = bufpool_page_alloc,
252 1.162 christos .pa_free = bufpool_page_free,
253 1.162 christos .pa_pagesz = MAXBSIZE,
254 1.100 pk };
255 1.100 pk
256 1.100 pk /* Buffer memory management variables */
257 1.183 ad u_long bufmem_valimit;
258 1.183 ad u_long bufmem_hiwater;
259 1.183 ad u_long bufmem_lowater;
260 1.183 ad u_long bufmem;
261 1.100 pk
262 1.100 pk /*
263 1.100 pk * MD code can call this to set a hard limit on the amount
264 1.100 pk * of virtual memory used by the buffer cache.
265 1.100 pk */
266 1.101 thorpej int
267 1.101 thorpej buf_setvalimit(vsize_t sz)
268 1.100 pk {
269 1.100 pk
270 1.100 pk /* We need to accommodate at least NMEMPOOLS of MAXBSIZE each */
271 1.100 pk if (sz < NMEMPOOLS * MAXBSIZE)
272 1.100 pk return EINVAL;
273 1.100 pk
274 1.100 pk bufmem_valimit = sz;
275 1.100 pk return 0;
276 1.100 pk }
277 1.100 pk
278 1.135 enami static void
279 1.135 enami buf_setwm(void)
280 1.135 enami {
281 1.135 enami
282 1.135 enami bufmem_hiwater = buf_memcalc();
283 1.135 enami /* lowater is approx. 2% of memory (with bufcache = 15) */
284 1.135 enami #define BUFMEM_WMSHIFT 3
285 1.135 enami #define BUFMEM_HIWMMIN (64 * 1024 << BUFMEM_WMSHIFT)
286 1.135 enami if (bufmem_hiwater < BUFMEM_HIWMMIN)
287 1.135 enami /* Ensure a reasonable minimum value */
288 1.135 enami bufmem_hiwater = BUFMEM_HIWMMIN;
289 1.135 enami bufmem_lowater = bufmem_hiwater >> BUFMEM_WMSHIFT;
290 1.135 enami }
291 1.135 enami
292 1.99 dbj #ifdef DEBUG
293 1.99 dbj int debug_verify_freelist = 0;
294 1.131 yamt static int
295 1.183 ad checkfreelist(buf_t *bp, struct bqueue *dp)
296 1.99 dbj {
297 1.183 ad buf_t *b;
298 1.183 ad
299 1.183 ad if (!debug_verify_freelist)
300 1.183 ad return 1;
301 1.131 yamt
302 1.131 yamt TAILQ_FOREACH(b, &dp->bq_queue, b_freelist) {
303 1.100 pk if (b == bp)
304 1.100 pk return 1;
305 1.100 pk }
306 1.183 ad
307 1.100 pk return 0;
308 1.99 dbj }
309 1.99 dbj #endif
310 1.99 dbj
311 1.131 yamt /*
312 1.131 yamt * Insq/Remq for the buffer hash lists.
313 1.131 yamt * Call with buffer queue locked.
314 1.131 yamt */
315 1.183 ad static void
316 1.183 ad binsheadfree(buf_t *bp, struct bqueue *dp)
317 1.131 yamt {
318 1.131 yamt
319 1.131 yamt KASSERT(bp->b_freelistindex == -1);
320 1.131 yamt TAILQ_INSERT_HEAD(&dp->bq_queue, bp, b_freelist);
321 1.131 yamt dp->bq_bytes += bp->b_bufsize;
322 1.131 yamt bp->b_freelistindex = dp - bufqueues;
323 1.131 yamt }
324 1.131 yamt
325 1.183 ad static void
326 1.183 ad binstailfree(buf_t *bp, struct bqueue *dp)
327 1.131 yamt {
328 1.131 yamt
329 1.131 yamt KASSERT(bp->b_freelistindex == -1);
330 1.131 yamt TAILQ_INSERT_TAIL(&dp->bq_queue, bp, b_freelist);
331 1.131 yamt dp->bq_bytes += bp->b_bufsize;
332 1.131 yamt bp->b_freelistindex = dp - bufqueues;
333 1.131 yamt }
334 1.131 yamt
335 1.31 cgd void
336 1.183 ad bremfree(buf_t *bp)
337 1.31 cgd {
338 1.131 yamt struct bqueue *dp;
339 1.131 yamt int bqidx = bp->b_freelistindex;
340 1.94 yamt
341 1.183 ad KASSERT(mutex_owned(&bufcache_lock));
342 1.31 cgd
343 1.131 yamt KASSERT(bqidx != -1);
344 1.131 yamt dp = &bufqueues[bqidx];
345 1.183 ad KDASSERT(checkfreelist(bp, dp));
346 1.131 yamt KASSERT(dp->bq_bytes >= bp->b_bufsize);
347 1.131 yamt TAILQ_REMOVE(&dp->bq_queue, bp, b_freelist);
348 1.131 yamt dp->bq_bytes -= bp->b_bufsize;
349 1.183 ad
350 1.183 ad /* For the sysctl helper. */
351 1.183 ad if (bp == dp->bq_marker)
352 1.183 ad dp->bq_marker = NULL;
353 1.183 ad
354 1.131 yamt #if defined(DIAGNOSTIC)
355 1.131 yamt bp->b_freelistindex = -1;
356 1.131 yamt #endif /* defined(DIAGNOSTIC) */
357 1.31 cgd }
358 1.31 cgd
359 1.183 ad /*
360 1.183 ad * Add a reference to an buffer structure that came from buf_cache.
361 1.183 ad */
362 1.183 ad static inline void
363 1.183 ad bref(buf_t *bp)
364 1.183 ad {
365 1.183 ad
366 1.183 ad KASSERT(mutex_owned(&bufcache_lock));
367 1.183 ad KASSERT(bp->b_refcnt > 0);
368 1.183 ad
369 1.183 ad bp->b_refcnt++;
370 1.183 ad }
371 1.183 ad
372 1.183 ad /*
373 1.183 ad * Free an unused buffer structure that came from buf_cache.
374 1.183 ad */
375 1.183 ad static inline void
376 1.183 ad brele(buf_t *bp)
377 1.183 ad {
378 1.183 ad
379 1.183 ad KASSERT(mutex_owned(&bufcache_lock));
380 1.183 ad KASSERT(bp->b_refcnt > 0);
381 1.183 ad
382 1.183 ad if (bp->b_refcnt-- == 1) {
383 1.183 ad buf_destroy(bp);
384 1.183 ad #ifdef DEBUG
385 1.183 ad memset((char *)bp, 0, sizeof(*bp));
386 1.183 ad #endif
387 1.183 ad pool_cache_put(buf_cache, bp);
388 1.183 ad }
389 1.183 ad }
390 1.183 ad
391 1.101 thorpej u_long
392 1.101 thorpej buf_memcalc(void)
393 1.100 pk {
394 1.100 pk u_long n;
395 1.100 pk
396 1.100 pk /*
397 1.100 pk * Determine the upper bound of memory to use for buffers.
398 1.100 pk *
399 1.100 pk * - If bufpages is specified, use that as the number
400 1.100 pk * pages.
401 1.100 pk *
402 1.100 pk * - Otherwise, use bufcache as the percentage of
403 1.100 pk * physical memory.
404 1.100 pk */
405 1.100 pk if (bufpages != 0) {
406 1.100 pk n = bufpages;
407 1.100 pk } else {
408 1.100 pk if (bufcache < 5) {
409 1.100 pk printf("forcing bufcache %d -> 5", bufcache);
410 1.100 pk bufcache = 5;
411 1.100 pk }
412 1.100 pk if (bufcache > 95) {
413 1.100 pk printf("forcing bufcache %d -> 95", bufcache);
414 1.100 pk bufcache = 95;
415 1.100 pk }
416 1.100 pk n = physmem / 100 * bufcache;
417 1.100 pk }
418 1.100 pk
419 1.100 pk n <<= PAGE_SHIFT;
420 1.100 pk if (bufmem_valimit != 0 && n > bufmem_valimit)
421 1.100 pk n = bufmem_valimit;
422 1.100 pk
423 1.100 pk return (n);
424 1.100 pk }
425 1.100 pk
426 1.31 cgd /*
427 1.31 cgd * Initialize buffers and hash links for buffers.
428 1.31 cgd */
429 1.31 cgd void
430 1.101 thorpej bufinit(void)
431 1.31 cgd {
432 1.131 yamt struct bqueue *dp;
433 1.127 thorpej int use_std;
434 1.100 pk u_int i;
435 1.100 pk
436 1.183 ad mutex_init(&bufcache_lock, MUTEX_DEFAULT, IPL_NONE);
437 1.183 ad mutex_init(&buffer_lock, MUTEX_DEFAULT, IPL_NONE);
438 1.183 ad cv_init(&needbuffer_cv, "needbuf");
439 1.183 ad
440 1.100 pk /*
441 1.100 pk * Initialize buffer cache memory parameters.
442 1.100 pk */
443 1.100 pk bufmem = 0;
444 1.135 enami buf_setwm();
445 1.100 pk
446 1.100 pk if (bufmem_valimit != 0) {
447 1.100 pk vaddr_t minaddr = 0, maxaddr;
448 1.100 pk buf_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
449 1.169 thorpej bufmem_valimit, 0, false, 0);
450 1.100 pk if (buf_map == NULL)
451 1.100 pk panic("bufinit: cannot allocate submap");
452 1.100 pk } else
453 1.100 pk buf_map = kernel_map;
454 1.65 thorpej
455 1.100 pk /* On "small" machines use small pool page sizes where possible */
456 1.127 thorpej use_std = (physmem < atop(16*1024*1024));
457 1.127 thorpej
458 1.127 thorpej /*
459 1.127 thorpej * Also use them on systems that can map the pool pages using
460 1.127 thorpej * a direct-mapped segment.
461 1.127 thorpej */
462 1.127 thorpej #ifdef PMAP_MAP_POOLPAGE
463 1.127 thorpej use_std = 1;
464 1.127 thorpej #endif
465 1.100 pk
466 1.183 ad buf_cache = pool_cache_init(sizeof(buf_t), 0, 0, 0,
467 1.183 ad "bufpl", NULL, IPL_SOFTBIO, NULL, NULL, NULL);
468 1.183 ad bufio_cache = pool_cache_init(sizeof(buf_t), 0, 0, 0,
469 1.183 ad "biopl", NULL, IPL_BIO, NULL, NULL, NULL);
470 1.176 pooka
471 1.161 yamt bufmempool_allocator.pa_backingmap = buf_map;
472 1.100 pk for (i = 0; i < NMEMPOOLS; i++) {
473 1.100 pk struct pool_allocator *pa;
474 1.100 pk struct pool *pp = &bmempools[i];
475 1.100 pk u_int size = 1 << (i + MEMPOOL_INDEX_OFFSET);
476 1.100 pk char *name = malloc(8, M_TEMP, M_WAITOK);
477 1.165 christos if (__predict_true(size >= 1024))
478 1.165 christos (void)snprintf(name, 8, "buf%dk", size / 1024);
479 1.165 christos else
480 1.165 christos (void)snprintf(name, 8, "buf%db", size);
481 1.127 thorpej pa = (size <= PAGE_SIZE && use_std)
482 1.100 pk ? &pool_allocator_nointr
483 1.100 pk : &bufmempool_allocator;
484 1.171 ad pool_init(pp, size, 0, 0, 0, name, pa, IPL_NONE);
485 1.100 pk pool_setlowat(pp, 1);
486 1.126 thorpej pool_sethiwat(pp, 1);
487 1.100 pk }
488 1.100 pk
489 1.100 pk /* Initialize the buffer queues */
490 1.131 yamt for (dp = bufqueues; dp < &bufqueues[BQUEUES]; dp++) {
491 1.131 yamt TAILQ_INIT(&dp->bq_queue);
492 1.131 yamt dp->bq_bytes = 0;
493 1.131 yamt }
494 1.100 pk
495 1.100 pk /*
496 1.100 pk * Estimate hash table size based on the amount of memory we
497 1.100 pk * intend to use for the buffer cache. The average buffer
498 1.100 pk * size is dependent on our clients (i.e. filesystems).
499 1.100 pk *
500 1.100 pk * For now, use an empirical 3K per buffer.
501 1.100 pk */
502 1.100 pk nbuf = (bufmem_hiwater / 1024) / 3;
503 1.70 ad bufhashtbl = hashinit(nbuf, HASH_LIST, M_CACHE, M_WAITOK, &bufhash);
504 1.100 pk }
505 1.100 pk
506 1.183 ad void
507 1.183 ad bufinit2(void)
508 1.183 ad {
509 1.183 ad
510 1.183 ad biodone_sih = softint_establish(SOFTINT_BIO | SOFTINT_MPSAFE, biointr,
511 1.183 ad NULL);
512 1.183 ad if (biodone_sih == NULL)
513 1.183 ad panic("bufinit2: can't establish soft interrupt");
514 1.183 ad }
515 1.183 ad
516 1.100 pk static int
517 1.100 pk buf_lotsfree(void)
518 1.100 pk {
519 1.114 tls int try, thresh;
520 1.128 hannken
521 1.128 hannken /* Always allocate if doing copy on write */
522 1.172 yamt if (curlwp->l_pflag & LP_UFSCOW)
523 1.128 hannken return 1;
524 1.114 tls
525 1.122 simonb /* Always allocate if less than the low water mark. */
526 1.122 simonb if (bufmem < bufmem_lowater)
527 1.114 tls return 1;
528 1.142 perry
529 1.122 simonb /* Never allocate if greater than the high water mark. */
530 1.122 simonb if (bufmem > bufmem_hiwater)
531 1.122 simonb return 0;
532 1.114 tls
533 1.115 tls /* If there's anything on the AGE list, it should be eaten. */
534 1.131 yamt if (TAILQ_FIRST(&bufqueues[BQ_AGE].bq_queue) != NULL)
535 1.115 tls return 0;
536 1.115 tls
537 1.122 simonb /*
538 1.122 simonb * The probabily of getting a new allocation is inversely
539 1.122 simonb * proportional to the current size of the cache, using
540 1.122 simonb * a granularity of 16 steps.
541 1.122 simonb */
542 1.114 tls try = random() & 0x0000000fL;
543 1.114 tls
544 1.122 simonb /* Don't use "16 * bufmem" here to avoid a 32-bit overflow. */
545 1.136 enami thresh = (bufmem - bufmem_lowater) /
546 1.136 enami ((bufmem_hiwater - bufmem_lowater) / 16);
547 1.114 tls
548 1.136 enami if (try >= thresh)
549 1.114 tls return 1;
550 1.114 tls
551 1.122 simonb /* Otherwise don't allocate. */
552 1.114 tls return 0;
553 1.100 pk }
554 1.100 pk
555 1.100 pk /*
556 1.116 yamt * Return estimate of bytes we think need to be
557 1.100 pk * released to help resolve low memory conditions.
558 1.116 yamt *
559 1.183 ad * => called with bufcache_lock held.
560 1.100 pk */
561 1.100 pk static int
562 1.100 pk buf_canrelease(void)
563 1.100 pk {
564 1.115 tls int pagedemand, ninvalid = 0;
565 1.115 tls
566 1.183 ad KASSERT(mutex_owned(&bufcache_lock));
567 1.116 yamt
568 1.118 dan if (bufmem < bufmem_lowater)
569 1.118 dan return 0;
570 1.118 dan
571 1.141 tls if (bufmem > bufmem_hiwater)
572 1.141 tls return bufmem - bufmem_hiwater;
573 1.141 tls
574 1.131 yamt ninvalid += bufqueues[BQ_AGE].bq_bytes;
575 1.100 pk
576 1.115 tls pagedemand = uvmexp.freetarg - uvmexp.free;
577 1.115 tls if (pagedemand < 0)
578 1.115 tls return ninvalid;
579 1.115 tls return MAX(ninvalid, MIN(2 * MAXBSIZE,
580 1.115 tls MIN((bufmem - bufmem_lowater) / 16, pagedemand * PAGE_SIZE)));
581 1.100 pk }
582 1.100 pk
583 1.100 pk /*
584 1.100 pk * Buffer memory allocation helper functions
585 1.100 pk */
586 1.183 ad static u_long
587 1.101 thorpej buf_mempoolidx(u_long size)
588 1.100 pk {
589 1.100 pk u_int n = 0;
590 1.100 pk
591 1.100 pk size -= 1;
592 1.100 pk size >>= MEMPOOL_INDEX_OFFSET;
593 1.100 pk while (size) {
594 1.100 pk size >>= 1;
595 1.100 pk n += 1;
596 1.100 pk }
597 1.100 pk if (n >= NMEMPOOLS)
598 1.100 pk panic("buf mem pool index %d", n);
599 1.100 pk return n;
600 1.100 pk }
601 1.100 pk
602 1.183 ad static u_long
603 1.101 thorpej buf_roundsize(u_long size)
604 1.100 pk {
605 1.100 pk /* Round up to nearest power of 2 */
606 1.100 pk return (1 << (buf_mempoolidx(size) + MEMPOOL_INDEX_OFFSET));
607 1.100 pk }
608 1.100 pk
609 1.183 ad static void *
610 1.101 thorpej buf_malloc(size_t size)
611 1.100 pk {
612 1.100 pk u_int n = buf_mempoolidx(size);
613 1.170 christos void *addr;
614 1.100 pk
615 1.100 pk while (1) {
616 1.100 pk addr = pool_get(&bmempools[n], PR_NOWAIT);
617 1.100 pk if (addr != NULL)
618 1.100 pk break;
619 1.100 pk
620 1.100 pk /* No memory, see if we can free some. If so, try again */
621 1.183 ad mutex_enter(&bufcache_lock);
622 1.183 ad if (buf_drain(1) > 0) {
623 1.183 ad mutex_exit(&bufcache_lock);
624 1.100 pk continue;
625 1.183 ad }
626 1.183 ad
627 1.183 ad if (curlwp == uvm.pagedaemon_lwp) {
628 1.183 ad mutex_exit(&bufcache_lock);
629 1.183 ad return NULL;
630 1.183 ad }
631 1.100 pk
632 1.100 pk /* Wait for buffers to arrive on the LRU queue */
633 1.183 ad cv_timedwait(&needbuffer_cv, &bufcache_lock, hz / 4);
634 1.183 ad mutex_exit(&bufcache_lock);
635 1.31 cgd }
636 1.100 pk
637 1.100 pk return addr;
638 1.100 pk }
639 1.100 pk
640 1.101 thorpej static void
641 1.170 christos buf_mrelease(void *addr, size_t size)
642 1.100 pk {
643 1.100 pk
644 1.100 pk pool_put(&bmempools[buf_mempoolidx(size)], addr);
645 1.31 cgd }
646 1.31 cgd
647 1.130 yamt /*
648 1.130 yamt * bread()/breadn() helper.
649 1.130 yamt */
650 1.183 ad static buf_t *
651 1.166 yamt bio_doread(struct vnode *vp, daddr_t blkno, int size, kauth_cred_t cred,
652 1.166 yamt int async)
653 1.31 cgd {
654 1.183 ad buf_t *bp;
655 1.123 christos struct mount *mp;
656 1.31 cgd
657 1.34 mycroft bp = getblk(vp, blkno, size, 0, 0);
658 1.31 cgd
659 1.86 thorpej #ifdef DIAGNOSTIC
660 1.86 thorpej if (bp == NULL) {
661 1.86 thorpej panic("bio_doread: no such buf");
662 1.86 thorpej }
663 1.86 thorpej #endif
664 1.86 thorpej
665 1.31 cgd /*
666 1.34 mycroft * If buffer does not have data valid, start a read.
667 1.183 ad * Note that if buffer is BC_INVAL, getblk() won't return it.
668 1.87 pk * Therefore, it's valid if its I/O has completed or been delayed.
669 1.31 cgd */
670 1.183 ad if (!ISSET(bp->b_oflags, (BO_DONE | BO_DELWRI))) {
671 1.73 chs /* Start I/O for the buffer. */
672 1.34 mycroft SET(bp->b_flags, B_READ | async);
673 1.108 yamt if (async)
674 1.108 yamt BIO_SETPRIO(bp, BPRIO_TIMELIMITED);
675 1.108 yamt else
676 1.108 yamt BIO_SETPRIO(bp, BPRIO_TIMECRITICAL);
677 1.112 hannken VOP_STRATEGY(vp, bp);
678 1.31 cgd
679 1.34 mycroft /* Pay for the read. */
680 1.172 yamt curproc->p_stats->p_ru.ru_inblock++;
681 1.183 ad } else if (async)
682 1.179 ad brelse(bp, 0);
683 1.31 cgd
684 1.124 yamt if (vp->v_type == VBLK)
685 1.124 yamt mp = vp->v_specmountpoint;
686 1.124 yamt else
687 1.124 yamt mp = vp->v_mount;
688 1.123 christos
689 1.123 christos /*
690 1.123 christos * Collect statistics on synchronous and asynchronous reads.
691 1.123 christos * Reads from block devices are charged to their associated
692 1.123 christos * filesystem (if any).
693 1.123 christos */
694 1.123 christos if (mp != NULL) {
695 1.123 christos if (async == 0)
696 1.123 christos mp->mnt_stat.f_syncreads++;
697 1.123 christos else
698 1.123 christos mp->mnt_stat.f_asyncreads++;
699 1.123 christos }
700 1.123 christos
701 1.34 mycroft return (bp);
702 1.34 mycroft }
703 1.34 mycroft
704 1.34 mycroft /*
705 1.34 mycroft * Read a disk block.
706 1.34 mycroft * This algorithm described in Bach (p.54).
707 1.34 mycroft */
708 1.40 christos int
709 1.160 elad bread(struct vnode *vp, daddr_t blkno, int size, kauth_cred_t cred,
710 1.183 ad buf_t **bpp)
711 1.34 mycroft {
712 1.183 ad buf_t *bp;
713 1.34 mycroft
714 1.34 mycroft /* Get buffer for block. */
715 1.34 mycroft bp = *bpp = bio_doread(vp, blkno, size, cred, 0);
716 1.31 cgd
717 1.80 chs /* Wait for the read to complete, and return result. */
718 1.31 cgd return (biowait(bp));
719 1.31 cgd }
720 1.31 cgd
721 1.31 cgd /*
722 1.31 cgd * Read-ahead multiple disk blocks. The first is sync, the rest async.
723 1.31 cgd * Trivial modification to the breada algorithm presented in Bach (p.55).
724 1.31 cgd */
725 1.40 christos int
726 1.101 thorpej breadn(struct vnode *vp, daddr_t blkno, int size, daddr_t *rablks,
727 1.183 ad int *rasizes, int nrablks, kauth_cred_t cred, buf_t **bpp)
728 1.31 cgd {
729 1.183 ad buf_t *bp;
730 1.31 cgd int i;
731 1.31 cgd
732 1.34 mycroft bp = *bpp = bio_doread(vp, blkno, size, cred, 0);
733 1.31 cgd
734 1.31 cgd /*
735 1.31 cgd * For each of the read-ahead blocks, start a read, if necessary.
736 1.31 cgd */
737 1.183 ad mutex_enter(&bufcache_lock);
738 1.31 cgd for (i = 0; i < nrablks; i++) {
739 1.31 cgd /* If it's in the cache, just go on to next one. */
740 1.31 cgd if (incore(vp, rablks[i]))
741 1.31 cgd continue;
742 1.31 cgd
743 1.31 cgd /* Get a buffer for the read-ahead block */
744 1.183 ad mutex_exit(&bufcache_lock);
745 1.34 mycroft (void) bio_doread(vp, rablks[i], rasizes[i], cred, B_ASYNC);
746 1.183 ad mutex_enter(&bufcache_lock);
747 1.31 cgd }
748 1.183 ad mutex_exit(&bufcache_lock);
749 1.31 cgd
750 1.80 chs /* Otherwise, we had to start a read for it; wait until it's valid. */
751 1.31 cgd return (biowait(bp));
752 1.31 cgd }
753 1.31 cgd
754 1.31 cgd /*
755 1.31 cgd * Read with single-block read-ahead. Defined in Bach (p.55), but
756 1.31 cgd * implemented as a call to breadn().
757 1.31 cgd * XXX for compatibility with old file systems.
758 1.31 cgd */
759 1.40 christos int
760 1.101 thorpej breada(struct vnode *vp, daddr_t blkno, int size, daddr_t rablkno,
761 1.183 ad int rabsize, kauth_cred_t cred, buf_t **bpp)
762 1.31 cgd {
763 1.34 mycroft
764 1.142 perry return (breadn(vp, blkno, size, &rablkno, &rabsize, 1, cred, bpp));
765 1.31 cgd }
766 1.31 cgd
767 1.31 cgd /*
768 1.31 cgd * Block write. Described in Bach (p.56)
769 1.31 cgd */
770 1.40 christos int
771 1.183 ad bwrite(buf_t *bp)
772 1.31 cgd {
773 1.183 ad int rv, sync, wasdelayed;
774 1.59 fvdl struct vnode *vp;
775 1.59 fvdl struct mount *mp;
776 1.31 cgd
777 1.183 ad KASSERT(ISSET(bp->b_cflags, BC_BUSY));
778 1.87 pk
779 1.76 chs vp = bp->b_vp;
780 1.76 chs if (vp != NULL) {
781 1.183 ad KASSERT(bp->b_objlock == &vp->v_interlock);
782 1.76 chs if (vp->v_type == VBLK)
783 1.76 chs mp = vp->v_specmountpoint;
784 1.76 chs else
785 1.76 chs mp = vp->v_mount;
786 1.76 chs } else {
787 1.76 chs mp = NULL;
788 1.76 chs }
789 1.76 chs
790 1.38 cgd /*
791 1.38 cgd * Remember buffer type, to switch on it later. If the write was
792 1.38 cgd * synchronous, but the file system was mounted with MNT_ASYNC,
793 1.142 perry * convert it to a delayed write.
794 1.38 cgd * XXX note that this relies on delayed tape writes being converted
795 1.38 cgd * to async, not sync writes (which is safe, but ugly).
796 1.38 cgd */
797 1.31 cgd sync = !ISSET(bp->b_flags, B_ASYNC);
798 1.76 chs if (sync && mp != NULL && ISSET(mp->mnt_flag, MNT_ASYNC)) {
799 1.37 cgd bdwrite(bp);
800 1.37 cgd return (0);
801 1.37 cgd }
802 1.46 mycroft
803 1.59 fvdl /*
804 1.59 fvdl * Collect statistics on synchronous and asynchronous writes.
805 1.59 fvdl * Writes to block devices are charged to their associated
806 1.59 fvdl * filesystem (if any).
807 1.59 fvdl */
808 1.76 chs if (mp != NULL) {
809 1.76 chs if (sync)
810 1.76 chs mp->mnt_stat.f_syncwrites++;
811 1.59 fvdl else
812 1.76 chs mp->mnt_stat.f_asyncwrites++;
813 1.59 fvdl }
814 1.59 fvdl
815 1.46 mycroft /*
816 1.46 mycroft * Pay for the I/O operation and make sure the buf is on the correct
817 1.46 mycroft * vnode queue.
818 1.46 mycroft */
819 1.184 ad bp->b_error = 0;
820 1.184 ad wasdelayed = ISSET(bp->b_oflags, BO_DELWRI);
821 1.183 ad CLR(bp->b_flags, B_READ);
822 1.184 ad if (wasdelayed) {
823 1.184 ad mutex_enter(&bufcache_lock);
824 1.184 ad mutex_enter(bp->b_objlock);
825 1.184 ad CLR(bp->b_oflags, BO_DONE | BO_DELWRI);
826 1.46 mycroft reassignbuf(bp, bp->b_vp);
827 1.184 ad mutex_exit(&bufcache_lock);
828 1.184 ad } else {
829 1.172 yamt curproc->p_stats->p_ru.ru_oublock++;
830 1.184 ad mutex_enter(bp->b_objlock);
831 1.184 ad CLR(bp->b_oflags, BO_DONE | BO_DELWRI);
832 1.184 ad }
833 1.183 ad if (vp != NULL)
834 1.183 ad vp->v_numoutput++;
835 1.183 ad mutex_exit(bp->b_objlock);
836 1.32 mycroft
837 1.183 ad /* Initiate disk write. */
838 1.108 yamt if (sync)
839 1.108 yamt BIO_SETPRIO(bp, BPRIO_TIMECRITICAL);
840 1.108 yamt else
841 1.108 yamt BIO_SETPRIO(bp, BPRIO_TIMELIMITED);
842 1.108 yamt
843 1.112 hannken VOP_STRATEGY(vp, bp);
844 1.31 cgd
845 1.34 mycroft if (sync) {
846 1.46 mycroft /* If I/O was synchronous, wait for it to complete. */
847 1.31 cgd rv = biowait(bp);
848 1.31 cgd
849 1.34 mycroft /* Release the buffer. */
850 1.179 ad brelse(bp, 0);
851 1.34 mycroft
852 1.34 mycroft return (rv);
853 1.34 mycroft } else {
854 1.34 mycroft return (0);
855 1.31 cgd }
856 1.31 cgd }
857 1.31 cgd
858 1.31 cgd int
859 1.101 thorpej vn_bwrite(void *v)
860 1.31 cgd {
861 1.40 christos struct vop_bwrite_args *ap = v;
862 1.34 mycroft
863 1.31 cgd return (bwrite(ap->a_bp));
864 1.31 cgd }
865 1.31 cgd
866 1.31 cgd /*
867 1.31 cgd * Delayed write.
868 1.31 cgd *
869 1.31 cgd * The buffer is marked dirty, but is not queued for I/O.
870 1.31 cgd * This routine should be used when the buffer is expected
871 1.31 cgd * to be modified again soon, typically a small write that
872 1.31 cgd * partially fills a buffer.
873 1.31 cgd *
874 1.31 cgd * NB: magnetic tapes cannot be delayed; they must be
875 1.31 cgd * written in the order that the writes are requested.
876 1.31 cgd *
877 1.31 cgd * Described in Leffler, et al. (pp. 208-213).
878 1.31 cgd */
879 1.31 cgd void
880 1.183 ad bdwrite(buf_t *bp)
881 1.31 cgd {
882 1.183 ad
883 1.183 ad KASSERT(ISSET(bp->b_cflags, BC_BUSY));
884 1.31 cgd
885 1.46 mycroft /* If this is a tape block, write the block now. */
886 1.173 ad if (bdev_type(bp->b_dev) == D_TAPE) {
887 1.90 pk bawrite(bp);
888 1.90 pk return;
889 1.46 mycroft }
890 1.46 mycroft
891 1.31 cgd /*
892 1.31 cgd * If the block hasn't been seen before:
893 1.31 cgd * (1) Mark it as having been seen,
894 1.45 pk * (2) Charge for the write,
895 1.45 pk * (3) Make sure it's on its vnode's correct block list.
896 1.31 cgd */
897 1.183 ad KASSERT(bp->b_vp == NULL || bp->b_objlock == &bp->b_vp->v_interlock);
898 1.97 dbj
899 1.183 ad if (!ISSET(bp->b_oflags, BO_DELWRI)) {
900 1.184 ad mutex_enter(&bufcache_lock);
901 1.184 ad mutex_enter(bp->b_objlock);
902 1.183 ad SET(bp->b_oflags, BO_DELWRI);
903 1.172 yamt curproc->p_stats->p_ru.ru_oublock++;
904 1.31 cgd reassignbuf(bp, bp->b_vp);
905 1.184 ad mutex_exit(&bufcache_lock);
906 1.184 ad } else {
907 1.184 ad mutex_enter(bp->b_objlock);
908 1.31 cgd }
909 1.31 cgd /* Otherwise, the "write" is done, so mark and release the buffer. */
910 1.183 ad CLR(bp->b_oflags, BO_DONE);
911 1.183 ad mutex_exit(bp->b_objlock);
912 1.60 fvdl
913 1.179 ad brelse(bp, 0);
914 1.31 cgd }
915 1.31 cgd
916 1.31 cgd /*
917 1.31 cgd * Asynchronous block write; just an asynchronous bwrite().
918 1.31 cgd */
919 1.31 cgd void
920 1.183 ad bawrite(buf_t *bp)
921 1.31 cgd {
922 1.31 cgd
923 1.183 ad KASSERT(ISSET(bp->b_cflags, BC_BUSY));
924 1.87 pk
925 1.31 cgd SET(bp->b_flags, B_ASYNC);
926 1.31 cgd VOP_BWRITE(bp);
927 1.31 cgd }
928 1.31 cgd
929 1.31 cgd /*
930 1.59 fvdl * Same as first half of bdwrite, mark buffer dirty, but do not release it.
931 1.183 ad * Call with the buffer interlock held.
932 1.183 ad *
933 1.183 ad * Note: called only from biodone() through ffs softdep's io_complete()
934 1.59 fvdl */
935 1.59 fvdl void
936 1.183 ad bdirty(buf_t *bp)
937 1.59 fvdl {
938 1.59 fvdl
939 1.183 ad KASSERT(mutex_owned(&bufcache_lock));
940 1.183 ad KASSERT(bp->b_objlock == &bp->b_vp->v_interlock);
941 1.183 ad KASSERT(mutex_owned(bp->b_objlock));
942 1.183 ad KASSERT(ISSET(bp->b_cflags, BC_BUSY));
943 1.61 fvdl
944 1.183 ad CLR(bp->b_cflags, BC_AGE);
945 1.60 fvdl
946 1.183 ad if (!ISSET(bp->b_oflags, BO_DELWRI)) {
947 1.183 ad SET(bp->b_oflags, BO_DELWRI);
948 1.172 yamt curproc->p_stats->p_ru.ru_oublock++;
949 1.59 fvdl reassignbuf(bp, bp->b_vp);
950 1.59 fvdl }
951 1.59 fvdl }
952 1.59 fvdl
953 1.183 ad
954 1.59 fvdl /*
955 1.31 cgd * Release a buffer on to the free lists.
956 1.31 cgd * Described in Bach (p. 46).
957 1.31 cgd */
958 1.31 cgd void
959 1.183 ad brelsel(buf_t *bp, int set)
960 1.31 cgd {
961 1.131 yamt struct bqueue *bufq;
962 1.183 ad struct vnode *vp;
963 1.31 cgd
964 1.183 ad KASSERT(mutex_owned(&bufcache_lock));
965 1.97 dbj
966 1.183 ad SET(bp->b_cflags, set);
967 1.179 ad
968 1.183 ad KASSERT(ISSET(bp->b_cflags, BC_BUSY));
969 1.183 ad KASSERT(bp->b_iodone == NULL);
970 1.87 pk
971 1.31 cgd /* Wake up any processes waiting for any buffer to become free. */
972 1.183 ad cv_signal(&needbuffer_cv);
973 1.31 cgd
974 1.183 ad /* Wake up any proceeses waiting for _this_ buffer to become */
975 1.183 ad if (ISSET(bp->b_cflags, BC_WANTED) != 0) {
976 1.183 ad CLR(bp->b_cflags, BC_WANTED|BC_AGE);
977 1.183 ad cv_broadcast(&bp->b_busy);
978 1.31 cgd }
979 1.31 cgd
980 1.31 cgd /*
981 1.31 cgd * Determine which queue the buffer should be on, then put it there.
982 1.31 cgd */
983 1.31 cgd
984 1.31 cgd /* If it's locked, don't report an error; try again later. */
985 1.187 ad if (ISSET(bp->b_flags, B_LOCKED))
986 1.174 ad bp->b_error = 0;
987 1.31 cgd
988 1.31 cgd /* If it's not cacheable, or an error, mark it invalid. */
989 1.183 ad if (ISSET(bp->b_cflags, BC_NOCACHE) || bp->b_error != 0)
990 1.183 ad SET(bp->b_cflags, BC_INVAL);
991 1.31 cgd
992 1.183 ad if (ISSET(bp->b_cflags, BC_VFLUSH)) {
993 1.50 mycroft /*
994 1.50 mycroft * This is a delayed write buffer that was just flushed to
995 1.50 mycroft * disk. It is still on the LRU queue. If it's become
996 1.50 mycroft * invalid, then we need to move it to a different queue;
997 1.50 mycroft * otherwise leave it in its current position.
998 1.50 mycroft */
999 1.183 ad CLR(bp->b_cflags, BC_VFLUSH);
1000 1.187 ad if (!ISSET(bp->b_cflags, BC_INVAL|BC_AGE) &&
1001 1.187 ad !ISSET(bp->b_flags, B_LOCKED) && bp->b_error == 0) {
1002 1.183 ad KDASSERT(checkfreelist(bp, &bufqueues[BQ_LRU]));
1003 1.50 mycroft goto already_queued;
1004 1.99 dbj } else {
1005 1.50 mycroft bremfree(bp);
1006 1.99 dbj }
1007 1.50 mycroft }
1008 1.99 dbj
1009 1.183 ad KDASSERT(checkfreelist(bp, &bufqueues[BQ_AGE]));
1010 1.183 ad KDASSERT(checkfreelist(bp, &bufqueues[BQ_LRU]));
1011 1.183 ad KDASSERT(checkfreelist(bp, &bufqueues[BQ_LOCKED]));
1012 1.50 mycroft
1013 1.183 ad if ((bp->b_bufsize <= 0) || ISSET(bp->b_cflags, BC_INVAL)) {
1014 1.31 cgd /*
1015 1.31 cgd * If it's invalid or empty, dissociate it from its vnode
1016 1.31 cgd * and put on the head of the appropriate queue.
1017 1.31 cgd */
1018 1.183 ad if (bioopsp != NULL)
1019 1.183 ad (*bioopsp->io_deallocate)(bp);
1020 1.183 ad
1021 1.183 ad mutex_enter(bp->b_objlock);
1022 1.183 ad CLR(bp->b_oflags, BO_DONE|BO_DELWRI);
1023 1.183 ad if ((vp = bp->b_vp) != NULL) {
1024 1.183 ad KASSERT(bp->b_objlock == &vp->v_interlock);
1025 1.59 fvdl reassignbuf(bp, bp->b_vp);
1026 1.31 cgd brelvp(bp);
1027 1.183 ad mutex_exit(&vp->v_interlock);
1028 1.183 ad } else {
1029 1.183 ad KASSERT(bp->b_objlock == &buffer_lock);
1030 1.183 ad mutex_exit(bp->b_objlock);
1031 1.59 fvdl }
1032 1.183 ad
1033 1.31 cgd if (bp->b_bufsize <= 0)
1034 1.31 cgd /* no data */
1035 1.100 pk goto already_queued;
1036 1.31 cgd else
1037 1.31 cgd /* invalid data */
1038 1.31 cgd bufq = &bufqueues[BQ_AGE];
1039 1.31 cgd binsheadfree(bp, bufq);
1040 1.183 ad } else {
1041 1.31 cgd /*
1042 1.31 cgd * It has valid data. Put it on the end of the appropriate
1043 1.31 cgd * queue, so that it'll stick around for as long as possible.
1044 1.67 fvdl * If buf is AGE, but has dependencies, must put it on last
1045 1.67 fvdl * bufqueue to be scanned, ie LRU. This protects against the
1046 1.67 fvdl * livelock where BQ_AGE only has buffers with dependencies,
1047 1.67 fvdl * and we thus never get to the dependent buffers in BQ_LRU.
1048 1.31 cgd */
1049 1.187 ad if (ISSET(bp->b_flags, B_LOCKED)) {
1050 1.31 cgd /* locked in core */
1051 1.31 cgd bufq = &bufqueues[BQ_LOCKED];
1052 1.183 ad } else if (!ISSET(bp->b_cflags, BC_AGE)) {
1053 1.31 cgd /* valid data */
1054 1.31 cgd bufq = &bufqueues[BQ_LRU];
1055 1.183 ad } else {
1056 1.67 fvdl /* stale but valid data */
1057 1.67 fvdl int has_deps;
1058 1.67 fvdl
1059 1.183 ad if (bioopsp != NULL)
1060 1.183 ad has_deps = (*bioopsp->io_countdeps)(bp, 0);
1061 1.67 fvdl else
1062 1.67 fvdl has_deps = 0;
1063 1.67 fvdl bufq = has_deps ? &bufqueues[BQ_LRU] :
1064 1.67 fvdl &bufqueues[BQ_AGE];
1065 1.67 fvdl }
1066 1.31 cgd binstailfree(bp, bufq);
1067 1.31 cgd }
1068 1.50 mycroft already_queued:
1069 1.31 cgd /* Unlock the buffer. */
1070 1.183 ad CLR(bp->b_cflags, BC_AGE|BC_BUSY|BC_NOCACHE);
1071 1.183 ad CLR(bp->b_flags, B_ASYNC);
1072 1.31 cgd
1073 1.183 ad if (bp->b_bufsize <= 0)
1074 1.183 ad brele(bp);
1075 1.183 ad }
1076 1.183 ad
1077 1.183 ad void
1078 1.183 ad brelse(buf_t *bp, int set)
1079 1.183 ad {
1080 1.183 ad
1081 1.183 ad mutex_enter(&bufcache_lock);
1082 1.183 ad brelsel(bp, set);
1083 1.183 ad mutex_exit(&bufcache_lock);
1084 1.31 cgd }
1085 1.31 cgd
1086 1.31 cgd /*
1087 1.31 cgd * Determine if a block is in the cache.
1088 1.31 cgd * Just look on what would be its hash chain. If it's there, return
1089 1.31 cgd * a pointer to it, unless it's marked invalid. If it's marked invalid,
1090 1.31 cgd * we normally don't return the buffer, unless the caller explicitly
1091 1.31 cgd * wants us to.
1092 1.31 cgd */
1093 1.183 ad buf_t *
1094 1.101 thorpej incore(struct vnode *vp, daddr_t blkno)
1095 1.31 cgd {
1096 1.183 ad buf_t *bp;
1097 1.183 ad
1098 1.183 ad KASSERT(mutex_owned(&bufcache_lock));
1099 1.31 cgd
1100 1.31 cgd /* Search hash chain */
1101 1.84 matt LIST_FOREACH(bp, BUFHASH(vp, blkno), b_hash) {
1102 1.31 cgd if (bp->b_lblkno == blkno && bp->b_vp == vp &&
1103 1.183 ad !ISSET(bp->b_cflags, BC_INVAL)) {
1104 1.183 ad KASSERT(bp->b_objlock == &vp->v_interlock);
1105 1.183 ad return (bp);
1106 1.183 ad }
1107 1.31 cgd }
1108 1.31 cgd
1109 1.73 chs return (NULL);
1110 1.31 cgd }
1111 1.31 cgd
1112 1.31 cgd /*
1113 1.31 cgd * Get a block of requested size that is associated with
1114 1.31 cgd * a given vnode and block offset. If it is found in the
1115 1.31 cgd * block cache, mark it as having been found, make it busy
1116 1.31 cgd * and return it. Otherwise, return an empty block of the
1117 1.31 cgd * correct size. It is up to the caller to insure that the
1118 1.31 cgd * cached blocks be of the correct size.
1119 1.31 cgd */
1120 1.183 ad buf_t *
1121 1.101 thorpej getblk(struct vnode *vp, daddr_t blkno, int size, int slpflag, int slptimeo)
1122 1.31 cgd {
1123 1.183 ad int err, preserve;
1124 1.183 ad buf_t *bp;
1125 1.183 ad
1126 1.183 ad mutex_enter(&bufcache_lock);
1127 1.183 ad loop:
1128 1.73 chs bp = incore(vp, blkno);
1129 1.73 chs if (bp != NULL) {
1130 1.188 ad err = bbusy(bp, ((slpflag & PCATCH) != 0), slptimeo, NULL);
1131 1.183 ad if (err != 0) {
1132 1.183 ad if (err == EPASSTHROUGH)
1133 1.183 ad goto loop;
1134 1.183 ad mutex_exit(&bufcache_lock);
1135 1.183 ad return (NULL);
1136 1.31 cgd }
1137 1.57 mycroft #ifdef DIAGNOSTIC
1138 1.183 ad if (ISSET(bp->b_oflags, BO_DONE|BO_DELWRI) &&
1139 1.78 chs bp->b_bcount < size && vp->v_type != VBLK)
1140 1.73 chs panic("getblk: block size invariant failed");
1141 1.57 mycroft #endif
1142 1.73 chs bremfree(bp);
1143 1.100 pk preserve = 1;
1144 1.73 chs } else {
1145 1.183 ad if ((bp = getnewbuf(slpflag, slptimeo, 0)) == NULL)
1146 1.183 ad goto loop;
1147 1.183 ad
1148 1.183 ad if (incore(vp, blkno) != NULL) {
1149 1.183 ad /* The block has come into memory in the meantime. */
1150 1.183 ad brelsel(bp, 0);
1151 1.183 ad goto loop;
1152 1.87 pk }
1153 1.73 chs
1154 1.183 ad LIST_INSERT_HEAD(BUFHASH(vp, blkno), bp, b_hash);
1155 1.64 thorpej bp->b_blkno = bp->b_lblkno = bp->b_rawblkno = blkno;
1156 1.183 ad mutex_enter(&vp->v_interlock);
1157 1.31 cgd bgetvp(vp, bp);
1158 1.183 ad mutex_exit(&vp->v_interlock);
1159 1.100 pk preserve = 0;
1160 1.31 cgd }
1161 1.183 ad mutex_exit(&bufcache_lock);
1162 1.183 ad
1163 1.96 yamt /*
1164 1.187 ad * LFS can't track total size of B_LOCKED buffer (locked_queue_bytes)
1165 1.96 yamt * if we re-size buffers here.
1166 1.96 yamt */
1167 1.187 ad if (ISSET(bp->b_flags, B_LOCKED)) {
1168 1.96 yamt KASSERT(bp->b_bufsize >= size);
1169 1.96 yamt } else {
1170 1.183 ad if (allocbuf(bp, size, preserve)) {
1171 1.183 ad mutex_enter(&bufcache_lock);
1172 1.183 ad LIST_REMOVE(bp, b_hash);
1173 1.183 ad mutex_exit(&bufcache_lock);
1174 1.183 ad brelse(bp, BC_INVAL);
1175 1.183 ad return NULL;
1176 1.183 ad }
1177 1.96 yamt }
1178 1.108 yamt BIO_SETPRIO(bp, BPRIO_DEFAULT);
1179 1.31 cgd return (bp);
1180 1.31 cgd }
1181 1.31 cgd
1182 1.31 cgd /*
1183 1.31 cgd * Get an empty, disassociated buffer of given size.
1184 1.31 cgd */
1185 1.183 ad buf_t *
1186 1.101 thorpej geteblk(int size)
1187 1.31 cgd {
1188 1.183 ad buf_t *bp;
1189 1.183 ad int error;
1190 1.31 cgd
1191 1.183 ad mutex_enter(&bufcache_lock);
1192 1.183 ad while ((bp = getnewbuf(0, 0, 0)) == NULL)
1193 1.31 cgd ;
1194 1.87 pk
1195 1.183 ad SET(bp->b_cflags, BC_INVAL);
1196 1.183 ad LIST_INSERT_HEAD(&invalhash, bp, b_hash);
1197 1.183 ad mutex_exit(&bufcache_lock);
1198 1.109 yamt BIO_SETPRIO(bp, BPRIO_DEFAULT);
1199 1.183 ad error = allocbuf(bp, size, 0);
1200 1.183 ad KASSERT(error == 0);
1201 1.31 cgd return (bp);
1202 1.31 cgd }
1203 1.31 cgd
1204 1.31 cgd /*
1205 1.31 cgd * Expand or contract the actual memory allocated to a buffer.
1206 1.31 cgd *
1207 1.31 cgd * If the buffer shrinks, data is lost, so it's up to the
1208 1.31 cgd * caller to have written it out *first*; this routine will not
1209 1.31 cgd * start a write. If the buffer grows, it's the callers
1210 1.31 cgd * responsibility to fill out the buffer's additional contents.
1211 1.31 cgd */
1212 1.183 ad int
1213 1.183 ad allocbuf(buf_t *bp, int size, int preserve)
1214 1.31 cgd {
1215 1.100 pk vsize_t oldsize, desired_size;
1216 1.170 christos void *addr;
1217 1.183 ad int delta;
1218 1.31 cgd
1219 1.100 pk desired_size = buf_roundsize(size);
1220 1.31 cgd if (desired_size > MAXBSIZE)
1221 1.100 pk printf("allocbuf: buffer larger than MAXBSIZE requested");
1222 1.31 cgd
1223 1.100 pk bp->b_bcount = size;
1224 1.100 pk
1225 1.100 pk oldsize = bp->b_bufsize;
1226 1.100 pk if (oldsize == desired_size)
1227 1.183 ad return 0;
1228 1.31 cgd
1229 1.31 cgd /*
1230 1.100 pk * If we want a buffer of a different size, re-allocate the
1231 1.100 pk * buffer's memory; copy old content only if needed.
1232 1.31 cgd */
1233 1.100 pk addr = buf_malloc(desired_size);
1234 1.183 ad if (addr == NULL)
1235 1.183 ad return ENOMEM;
1236 1.100 pk if (preserve)
1237 1.100 pk memcpy(addr, bp->b_data, MIN(oldsize,desired_size));
1238 1.100 pk if (bp->b_data != NULL)
1239 1.100 pk buf_mrelease(bp->b_data, oldsize);
1240 1.100 pk bp->b_data = addr;
1241 1.100 pk bp->b_bufsize = desired_size;
1242 1.31 cgd
1243 1.31 cgd /*
1244 1.183 ad * Update overall buffer memory counter (protected by bufcache_lock)
1245 1.31 cgd */
1246 1.100 pk delta = (long)desired_size - (long)oldsize;
1247 1.100 pk
1248 1.183 ad mutex_enter(&bufcache_lock);
1249 1.100 pk if ((bufmem += delta) > bufmem_hiwater) {
1250 1.100 pk /*
1251 1.100 pk * Need to trim overall memory usage.
1252 1.100 pk */
1253 1.100 pk while (buf_canrelease()) {
1254 1.154 yamt if (curcpu()->ci_schedstate.spc_flags &
1255 1.154 yamt SPCF_SHOULDYIELD) {
1256 1.183 ad mutex_exit(&bufcache_lock);
1257 1.168 ad preempt();
1258 1.183 ad mutex_enter(&bufcache_lock);
1259 1.154 yamt }
1260 1.100 pk if (buf_trim() == 0)
1261 1.100 pk break;
1262 1.31 cgd }
1263 1.31 cgd }
1264 1.183 ad mutex_exit(&bufcache_lock);
1265 1.183 ad return 0;
1266 1.31 cgd }
1267 1.31 cgd
1268 1.31 cgd /*
1269 1.31 cgd * Find a buffer which is available for use.
1270 1.31 cgd * Select something from a free list.
1271 1.142 perry * Preference is to AGE list, then LRU list.
1272 1.87 pk *
1273 1.183 ad * Called with the buffer queues locked.
1274 1.87 pk * Return buffer locked.
1275 1.31 cgd */
1276 1.183 ad buf_t *
1277 1.101 thorpej getnewbuf(int slpflag, int slptimeo, int from_bufq)
1278 1.31 cgd {
1279 1.183 ad buf_t *bp;
1280 1.183 ad struct vnode *vp;
1281 1.31 cgd
1282 1.183 ad start:
1283 1.183 ad KASSERT(mutex_owned(&bufcache_lock));
1284 1.87 pk
1285 1.100 pk /*
1286 1.183 ad * Get a new buffer from the pool.
1287 1.100 pk */
1288 1.183 ad if (!from_bufq && buf_lotsfree()) {
1289 1.183 ad mutex_exit(&bufcache_lock);
1290 1.183 ad bp = pool_cache_get(buf_cache, PR_NOWAIT);
1291 1.183 ad if (bp != NULL) {
1292 1.183 ad memset((char *)bp, 0, sizeof(*bp));
1293 1.183 ad buf_init(bp);
1294 1.183 ad bp->b_dev = NODEV;
1295 1.183 ad bp->b_vnbufs.le_next = NOLIST;
1296 1.183 ad bp->b_cflags = BC_BUSY;
1297 1.183 ad bp->b_refcnt = 1;
1298 1.183 ad mutex_enter(&bufcache_lock);
1299 1.131 yamt #if defined(DIAGNOSTIC)
1300 1.183 ad bp->b_freelistindex = -1;
1301 1.131 yamt #endif /* defined(DIAGNOSTIC) */
1302 1.183 ad return (bp);
1303 1.183 ad }
1304 1.183 ad mutex_enter(&bufcache_lock);
1305 1.100 pk }
1306 1.100 pk
1307 1.131 yamt if ((bp = TAILQ_FIRST(&bufqueues[BQ_AGE].bq_queue)) != NULL ||
1308 1.131 yamt (bp = TAILQ_FIRST(&bufqueues[BQ_LRU].bq_queue)) != NULL) {
1309 1.31 cgd bremfree(bp);
1310 1.31 cgd } else {
1311 1.134 enami /*
1312 1.134 enami * XXX: !from_bufq should be removed.
1313 1.134 enami */
1314 1.173 ad if (!from_bufq || curlwp != uvm.pagedaemon_lwp) {
1315 1.134 enami /* wait for a free buffer of any kind */
1316 1.183 ad if ((slpflag & PCATCH) != 0)
1317 1.183 ad (void)cv_timedwait_sig(&needbuffer_cv,
1318 1.183 ad &bufcache_lock, slptimeo);
1319 1.183 ad else
1320 1.183 ad (void)cv_timedwait(&needbuffer_cv,
1321 1.183 ad &bufcache_lock, slptimeo);
1322 1.134 enami }
1323 1.73 chs return (NULL);
1324 1.31 cgd }
1325 1.31 cgd
1326 1.100 pk #ifdef DIAGNOSTIC
1327 1.100 pk if (bp->b_bufsize <= 0)
1328 1.100 pk panic("buffer %p: on queue but empty", bp);
1329 1.100 pk #endif
1330 1.100 pk
1331 1.183 ad if (ISSET(bp->b_cflags, BC_VFLUSH)) {
1332 1.50 mycroft /*
1333 1.50 mycroft * This is a delayed write buffer being flushed to disk. Make
1334 1.50 mycroft * sure it gets aged out of the queue when it's finished, and
1335 1.50 mycroft * leave it off the LRU queue.
1336 1.50 mycroft */
1337 1.183 ad CLR(bp->b_cflags, BC_VFLUSH);
1338 1.183 ad SET(bp->b_cflags, BC_AGE);
1339 1.50 mycroft goto start;
1340 1.50 mycroft }
1341 1.50 mycroft
1342 1.31 cgd /* Buffer is no longer on free lists. */
1343 1.183 ad SET(bp->b_cflags, BC_BUSY);
1344 1.31 cgd
1345 1.75 chs /*
1346 1.75 chs * If buffer was a delayed write, start it and return NULL
1347 1.75 chs * (since we might sleep while starting the write).
1348 1.75 chs */
1349 1.183 ad if (ISSET(bp->b_oflags, BO_DELWRI)) {
1350 1.50 mycroft /*
1351 1.50 mycroft * This buffer has gone through the LRU, so make sure it gets
1352 1.50 mycroft * reused ASAP.
1353 1.50 mycroft */
1354 1.183 ad SET(bp->b_cflags, BC_AGE);
1355 1.183 ad mutex_exit(&bufcache_lock);
1356 1.50 mycroft bawrite(bp);
1357 1.183 ad mutex_enter(&bufcache_lock);
1358 1.75 chs return (NULL);
1359 1.31 cgd }
1360 1.31 cgd
1361 1.183 ad vp = bp->b_vp;
1362 1.183 ad if (bioopsp != NULL)
1363 1.183 ad (*bioopsp->io_deallocate)(bp);
1364 1.59 fvdl
1365 1.31 cgd /* clear out various other fields */
1366 1.183 ad bp->b_cflags = BC_BUSY;
1367 1.183 ad bp->b_oflags = 0;
1368 1.183 ad bp->b_flags = 0;
1369 1.31 cgd bp->b_dev = NODEV;
1370 1.183 ad bp->b_blkno = 0;
1371 1.183 ad bp->b_lblkno = 0;
1372 1.183 ad bp->b_rawblkno = 0;
1373 1.31 cgd bp->b_iodone = 0;
1374 1.31 cgd bp->b_error = 0;
1375 1.31 cgd bp->b_resid = 0;
1376 1.31 cgd bp->b_bcount = 0;
1377 1.142 perry
1378 1.183 ad LIST_REMOVE(bp, b_hash);
1379 1.183 ad
1380 1.183 ad /* Disassociate us from our vnode, if we had one... */
1381 1.183 ad if (vp != NULL) {
1382 1.183 ad mutex_enter(&vp->v_interlock);
1383 1.183 ad brelvp(bp);
1384 1.183 ad mutex_exit(&vp->v_interlock);
1385 1.183 ad }
1386 1.183 ad
1387 1.142 perry return (bp);
1388 1.31 cgd }
1389 1.31 cgd
1390 1.31 cgd /*
1391 1.100 pk * Attempt to free an aged buffer off the queues.
1392 1.183 ad * Called with queue lock held.
1393 1.100 pk * Returns the amount of buffer memory freed.
1394 1.100 pk */
1395 1.130 yamt static int
1396 1.101 thorpej buf_trim(void)
1397 1.100 pk {
1398 1.183 ad buf_t *bp;
1399 1.100 pk long size = 0;
1400 1.100 pk
1401 1.183 ad KASSERT(mutex_owned(&bufcache_lock));
1402 1.183 ad
1403 1.100 pk /* Instruct getnewbuf() to get buffers off the queues */
1404 1.101 thorpej if ((bp = getnewbuf(PCATCH, 1, 1)) == NULL)
1405 1.100 pk return 0;
1406 1.100 pk
1407 1.183 ad KASSERT((bp->b_cflags & BC_WANTED) == 0);
1408 1.100 pk size = bp->b_bufsize;
1409 1.100 pk bufmem -= size;
1410 1.100 pk if (size > 0) {
1411 1.100 pk buf_mrelease(bp->b_data, size);
1412 1.100 pk bp->b_bcount = bp->b_bufsize = 0;
1413 1.100 pk }
1414 1.100 pk /* brelse() will return the buffer to the global buffer pool */
1415 1.183 ad brelsel(bp, 0);
1416 1.100 pk return size;
1417 1.100 pk }
1418 1.100 pk
1419 1.101 thorpej int
1420 1.101 thorpej buf_drain(int n)
1421 1.100 pk {
1422 1.183 ad int size = 0, sz;
1423 1.100 pk
1424 1.183 ad KASSERT(mutex_owned(&bufcache_lock));
1425 1.116 yamt
1426 1.134 enami while (size < n && bufmem > bufmem_lowater) {
1427 1.134 enami sz = buf_trim();
1428 1.134 enami if (sz <= 0)
1429 1.134 enami break;
1430 1.134 enami size += sz;
1431 1.134 enami }
1432 1.114 tls
1433 1.100 pk return size;
1434 1.100 pk }
1435 1.100 pk
1436 1.100 pk /*
1437 1.31 cgd * Wait for operations on the buffer to complete.
1438 1.31 cgd * When they do, extract and return the I/O's error value.
1439 1.31 cgd */
1440 1.31 cgd int
1441 1.183 ad biowait(buf_t *bp)
1442 1.31 cgd {
1443 1.142 perry
1444 1.183 ad mutex_enter(bp->b_objlock);
1445 1.183 ad while (!ISSET(bp->b_oflags, BO_DONE | BO_DELWRI))
1446 1.183 ad cv_wait(&bp->b_done, bp->b_objlock);
1447 1.183 ad mutex_exit(bp->b_objlock);
1448 1.183 ad
1449 1.183 ad return bp->b_error;
1450 1.31 cgd }
1451 1.31 cgd
1452 1.31 cgd /*
1453 1.31 cgd * Mark I/O complete on a buffer.
1454 1.31 cgd *
1455 1.31 cgd * If a callback has been requested, e.g. the pageout
1456 1.31 cgd * daemon, do so. Otherwise, awaken waiting processes.
1457 1.31 cgd *
1458 1.31 cgd * [ Leffler, et al., says on p.247:
1459 1.31 cgd * "This routine wakes up the blocked process, frees the buffer
1460 1.31 cgd * for an asynchronous write, or, for a request by the pagedaemon
1461 1.31 cgd * process, invokes a procedure specified in the buffer structure" ]
1462 1.31 cgd *
1463 1.31 cgd * In real life, the pagedaemon (or other system processes) wants
1464 1.31 cgd * to do async stuff to, and doesn't want the buffer brelse()'d.
1465 1.31 cgd * (for swap pager, that puts swap buffers on the free lists (!!!),
1466 1.31 cgd * for the vn device, that puts malloc'd buffers on the free lists!)
1467 1.31 cgd */
1468 1.31 cgd void
1469 1.183 ad biodone(buf_t *bp)
1470 1.183 ad {
1471 1.183 ad int s;
1472 1.183 ad
1473 1.183 ad KASSERT(!ISSET(bp->b_oflags, BO_DONE));
1474 1.183 ad
1475 1.183 ad if (cpu_intr_p()) {
1476 1.183 ad /* From interrupt mode: defer to a soft interrupt. */
1477 1.183 ad s = splvm();
1478 1.183 ad TAILQ_INSERT_TAIL(&curcpu()->ci_data.cpu_biodone, bp, b_actq);
1479 1.183 ad softint_schedule(biodone_sih);
1480 1.183 ad splx(s);
1481 1.183 ad } else {
1482 1.183 ad /* Process now - the buffer may be freed soon. */
1483 1.183 ad biodone2(bp);
1484 1.183 ad }
1485 1.183 ad }
1486 1.183 ad
1487 1.183 ad static void
1488 1.183 ad biodone2(buf_t *bp)
1489 1.31 cgd {
1490 1.183 ad void (*callout)(buf_t *);
1491 1.183 ad
1492 1.183 ad if (bioopsp != NULL)
1493 1.183 ad (*bioopsp->io_complete)(bp);
1494 1.60 fvdl
1495 1.183 ad mutex_enter(bp->b_objlock);
1496 1.183 ad /* Note that the transfer is done. */
1497 1.183 ad if (ISSET(bp->b_oflags, BO_DONE))
1498 1.183 ad panic("biodone2 already");
1499 1.186 hannken CLR(bp->b_flags, B_COWDONE);
1500 1.183 ad SET(bp->b_oflags, BO_DONE);
1501 1.108 yamt BIO_SETPRIO(bp, BPRIO_DEFAULT);
1502 1.31 cgd
1503 1.183 ad /* Wake up waiting writers. */
1504 1.183 ad if (!ISSET(bp->b_flags, B_READ))
1505 1.31 cgd vwakeup(bp);
1506 1.31 cgd
1507 1.183 ad if ((callout = bp->b_iodone) != NULL) {
1508 1.183 ad /* Note callout done, then call out. */
1509 1.183 ad KERNEL_LOCK(1, NULL); /* XXXSMP */
1510 1.183 ad bp->b_iodone = NULL;
1511 1.183 ad mutex_exit(bp->b_objlock);
1512 1.183 ad (*callout)(bp);
1513 1.183 ad KERNEL_UNLOCK_ONE(NULL); /* XXXSMP */
1514 1.183 ad } else if (ISSET(bp->b_flags, B_ASYNC)) {
1515 1.183 ad /* If async, release. */
1516 1.183 ad mutex_exit(bp->b_objlock);
1517 1.183 ad brelse(bp, 0);
1518 1.59 fvdl } else {
1519 1.183 ad /* Otherwise just wake up waiters in biowait(). */
1520 1.183 ad cv_broadcast(&bp->b_done);
1521 1.183 ad mutex_exit(bp->b_objlock);
1522 1.31 cgd }
1523 1.183 ad }
1524 1.183 ad
1525 1.183 ad static void
1526 1.183 ad biointr(void *cookie)
1527 1.183 ad {
1528 1.183 ad struct cpu_info *ci;
1529 1.183 ad buf_t *bp;
1530 1.183 ad int s;
1531 1.183 ad
1532 1.183 ad ci = curcpu();
1533 1.60 fvdl
1534 1.183 ad while (!TAILQ_EMPTY(&ci->ci_data.cpu_biodone)) {
1535 1.183 ad KASSERT(curcpu() == ci);
1536 1.183 ad
1537 1.183 ad s = splvm();
1538 1.183 ad bp = TAILQ_FIRST(&ci->ci_data.cpu_biodone);
1539 1.183 ad TAILQ_REMOVE(&ci->ci_data.cpu_biodone, bp, b_actq);
1540 1.183 ad splx(s);
1541 1.183 ad
1542 1.183 ad biodone2(bp);
1543 1.183 ad }
1544 1.31 cgd }
1545 1.31 cgd
1546 1.31 cgd /*
1547 1.31 cgd * Return a count of buffers on the "locked" queue.
1548 1.31 cgd */
1549 1.31 cgd int
1550 1.101 thorpej count_lock_queue(void)
1551 1.31 cgd {
1552 1.183 ad buf_t *bp;
1553 1.66 augustss int n = 0;
1554 1.31 cgd
1555 1.183 ad mutex_enter(&bufcache_lock);
1556 1.131 yamt TAILQ_FOREACH(bp, &bufqueues[BQ_LOCKED].bq_queue, b_freelist)
1557 1.31 cgd n++;
1558 1.183 ad mutex_exit(&bufcache_lock);
1559 1.31 cgd return (n);
1560 1.31 cgd }
1561 1.31 cgd
1562 1.100 pk /*
1563 1.100 pk * Wait for all buffers to complete I/O
1564 1.100 pk * Return the number of "stuck" buffers.
1565 1.100 pk */
1566 1.100 pk int
1567 1.100 pk buf_syncwait(void)
1568 1.100 pk {
1569 1.183 ad buf_t *bp;
1570 1.183 ad int iter, nbusy, nbusy_prev = 0, dcount, ihash;
1571 1.100 pk
1572 1.100 pk dcount = 10000;
1573 1.100 pk for (iter = 0; iter < 20;) {
1574 1.183 ad mutex_enter(&bufcache_lock);
1575 1.100 pk nbusy = 0;
1576 1.100 pk for (ihash = 0; ihash < bufhash+1; ihash++) {
1577 1.100 pk LIST_FOREACH(bp, &bufhashtbl[ihash], b_hash) {
1578 1.183 ad if ((bp->b_cflags & (BC_BUSY|BC_INVAL)) == BC_BUSY)
1579 1.183 ad nbusy += ((bp->b_flags & B_READ) == 0);
1580 1.100 pk /*
1581 1.100 pk * With soft updates, some buffers that are
1582 1.100 pk * written will be remarked as dirty until other
1583 1.100 pk * buffers are written.
1584 1.100 pk */
1585 1.100 pk if (bp->b_vp && bp->b_vp->v_mount
1586 1.100 pk && (bp->b_vp->v_mount->mnt_flag & MNT_SOFTDEP)
1587 1.183 ad && (bp->b_oflags & BO_DELWRI)) {
1588 1.100 pk bremfree(bp);
1589 1.183 ad bp->b_cflags |= BC_BUSY;
1590 1.100 pk nbusy++;
1591 1.183 ad mutex_exit(&bufcache_lock);
1592 1.100 pk bawrite(bp);
1593 1.100 pk if (dcount-- <= 0) {
1594 1.100 pk printf("softdep ");
1595 1.100 pk goto fail;
1596 1.100 pk }
1597 1.183 ad mutex_enter(&bufcache_lock);
1598 1.100 pk }
1599 1.100 pk }
1600 1.100 pk }
1601 1.183 ad mutex_exit(&bufcache_lock);
1602 1.100 pk
1603 1.100 pk if (nbusy == 0)
1604 1.100 pk break;
1605 1.100 pk if (nbusy_prev == 0)
1606 1.100 pk nbusy_prev = nbusy;
1607 1.100 pk printf("%d ", nbusy);
1608 1.100 pk tsleep(&nbusy, PRIBIO, "bflush",
1609 1.100 pk (iter == 0) ? 1 : hz / 25 * iter);
1610 1.100 pk if (nbusy >= nbusy_prev) /* we didn't flush anything */
1611 1.100 pk iter++;
1612 1.100 pk else
1613 1.100 pk nbusy_prev = nbusy;
1614 1.100 pk }
1615 1.100 pk
1616 1.100 pk if (nbusy) {
1617 1.100 pk fail:;
1618 1.100 pk #if defined(DEBUG) || defined(DEBUG_HALT_BUSY)
1619 1.100 pk printf("giving up\nPrinting vnodes for busy buffers\n");
1620 1.100 pk for (ihash = 0; ihash < bufhash+1; ihash++) {
1621 1.100 pk LIST_FOREACH(bp, &bufhashtbl[ihash], b_hash) {
1622 1.183 ad if ((bp->b_cflags & (BC_BUSY|BC_INVAL)) == BC_BUSY &&
1623 1.183 ad (bp->b_flags & B_READ) == 0)
1624 1.100 pk vprint(NULL, bp->b_vp);
1625 1.100 pk }
1626 1.100 pk }
1627 1.100 pk #endif
1628 1.100 pk }
1629 1.100 pk
1630 1.100 pk return nbusy;
1631 1.100 pk }
1632 1.100 pk
1633 1.117 atatat static void
1634 1.183 ad sysctl_fillbuf(buf_t *i, struct buf_sysctl *o)
1635 1.117 atatat {
1636 1.117 atatat
1637 1.183 ad o->b_flags = i->b_flags | i->b_cflags | i->b_oflags;
1638 1.117 atatat o->b_error = i->b_error;
1639 1.117 atatat o->b_prio = i->b_prio;
1640 1.117 atatat o->b_dev = i->b_dev;
1641 1.117 atatat o->b_bufsize = i->b_bufsize;
1642 1.117 atatat o->b_bcount = i->b_bcount;
1643 1.117 atatat o->b_resid = i->b_resid;
1644 1.182 ad o->b_addr = PTRTOUINT64(i->b_data);
1645 1.117 atatat o->b_blkno = i->b_blkno;
1646 1.117 atatat o->b_rawblkno = i->b_rawblkno;
1647 1.117 atatat o->b_iodone = PTRTOUINT64(i->b_iodone);
1648 1.117 atatat o->b_proc = PTRTOUINT64(i->b_proc);
1649 1.117 atatat o->b_vp = PTRTOUINT64(i->b_vp);
1650 1.117 atatat o->b_saveaddr = PTRTOUINT64(i->b_saveaddr);
1651 1.117 atatat o->b_lblkno = i->b_lblkno;
1652 1.117 atatat }
1653 1.117 atatat
1654 1.100 pk #define KERN_BUFSLOP 20
1655 1.100 pk static int
1656 1.100 pk sysctl_dobuf(SYSCTLFN_ARGS)
1657 1.100 pk {
1658 1.183 ad buf_t *bp;
1659 1.117 atatat struct buf_sysctl bs;
1660 1.183 ad struct bqueue *bq;
1661 1.100 pk char *dp;
1662 1.117 atatat u_int i, op, arg;
1663 1.117 atatat size_t len, needed, elem_size, out_size;
1664 1.183 ad int error, elem_count, retries;
1665 1.117 atatat
1666 1.117 atatat if (namelen == 1 && name[0] == CTL_QUERY)
1667 1.146 atatat return (sysctl_query(SYSCTLFN_CALL(rnode)));
1668 1.117 atatat
1669 1.117 atatat if (namelen != 4)
1670 1.117 atatat return (EINVAL);
1671 1.100 pk
1672 1.183 ad retries = 100;
1673 1.183 ad retry:
1674 1.100 pk dp = oldp;
1675 1.117 atatat len = (oldp != NULL) ? *oldlenp : 0;
1676 1.117 atatat op = name[0];
1677 1.117 atatat arg = name[1];
1678 1.117 atatat elem_size = name[2];
1679 1.117 atatat elem_count = name[3];
1680 1.117 atatat out_size = MIN(sizeof(bs), elem_size);
1681 1.117 atatat
1682 1.117 atatat /*
1683 1.117 atatat * at the moment, these are just "placeholders" to make the
1684 1.117 atatat * API for retrieving kern.buf data more extensible in the
1685 1.117 atatat * future.
1686 1.117 atatat *
1687 1.117 atatat * XXX kern.buf currently has "netbsd32" issues. hopefully
1688 1.117 atatat * these will be resolved at a later point.
1689 1.117 atatat */
1690 1.117 atatat if (op != KERN_BUF_ALL || arg != KERN_BUF_ALL ||
1691 1.117 atatat elem_size < 1 || elem_count < 0)
1692 1.117 atatat return (EINVAL);
1693 1.117 atatat
1694 1.100 pk error = 0;
1695 1.100 pk needed = 0;
1696 1.185 ad sysctl_unlock();
1697 1.183 ad mutex_enter(&bufcache_lock);
1698 1.100 pk for (i = 0; i < BQUEUES; i++) {
1699 1.183 ad bq = &bufqueues[i];
1700 1.183 ad TAILQ_FOREACH(bp, &bq->bq_queue, b_freelist) {
1701 1.183 ad bq->bq_marker = bp;
1702 1.117 atatat if (len >= elem_size && elem_count > 0) {
1703 1.117 atatat sysctl_fillbuf(bp, &bs);
1704 1.183 ad mutex_exit(&bufcache_lock);
1705 1.117 atatat error = copyout(&bs, dp, out_size);
1706 1.183 ad mutex_enter(&bufcache_lock);
1707 1.100 pk if (error)
1708 1.183 ad break;
1709 1.183 ad if (bq->bq_marker != bp) {
1710 1.183 ad /*
1711 1.183 ad * This sysctl node is only for
1712 1.183 ad * statistics. Retry; if the
1713 1.183 ad * queue keeps changing, then
1714 1.183 ad * bail out.
1715 1.183 ad */
1716 1.183 ad if (retries-- == 0) {
1717 1.183 ad error = EAGAIN;
1718 1.183 ad break;
1719 1.183 ad }
1720 1.183 ad mutex_exit(&bufcache_lock);
1721 1.183 ad goto retry;
1722 1.183 ad }
1723 1.100 pk dp += elem_size;
1724 1.100 pk len -= elem_size;
1725 1.100 pk }
1726 1.117 atatat if (elem_count > 0) {
1727 1.117 atatat needed += elem_size;
1728 1.117 atatat if (elem_count != INT_MAX)
1729 1.117 atatat elem_count--;
1730 1.117 atatat }
1731 1.100 pk }
1732 1.183 ad if (error != 0)
1733 1.183 ad break;
1734 1.100 pk }
1735 1.183 ad mutex_exit(&bufcache_lock);
1736 1.185 ad sysctl_relock();
1737 1.100 pk
1738 1.117 atatat *oldlenp = needed;
1739 1.117 atatat if (oldp == NULL)
1740 1.183 ad *oldlenp += KERN_BUFSLOP * sizeof(buf_t);
1741 1.100 pk
1742 1.100 pk return (error);
1743 1.100 pk }
1744 1.100 pk
1745 1.100 pk static int
1746 1.183 ad sysctl_bufvm_update(SYSCTLFN_ARGS)
1747 1.100 pk {
1748 1.183 ad int t, error, rv;
1749 1.100 pk struct sysctlnode node;
1750 1.100 pk
1751 1.100 pk node = *rnode;
1752 1.100 pk node.sysctl_data = &t;
1753 1.143 chs t = *(int *)rnode->sysctl_data;
1754 1.100 pk error = sysctl_lookup(SYSCTLFN_CALL(&node));
1755 1.100 pk if (error || newp == NULL)
1756 1.100 pk return (error);
1757 1.100 pk
1758 1.143 chs if (t < 0)
1759 1.143 chs return EINVAL;
1760 1.183 ad if (rnode->sysctl_data == &bufcache) {
1761 1.183 ad if (t > 100)
1762 1.183 ad return (EINVAL);
1763 1.183 ad bufcache = t;
1764 1.183 ad buf_setwm();
1765 1.183 ad } else if (rnode->sysctl_data == &bufmem_lowater) {
1766 1.143 chs if (bufmem_hiwater - t < 16)
1767 1.136 enami return (EINVAL);
1768 1.100 pk bufmem_lowater = t;
1769 1.117 atatat } else if (rnode->sysctl_data == &bufmem_hiwater) {
1770 1.143 chs if (t - bufmem_lowater < 16)
1771 1.136 enami return (EINVAL);
1772 1.100 pk bufmem_hiwater = t;
1773 1.100 pk } else
1774 1.100 pk return (EINVAL);
1775 1.100 pk
1776 1.183 ad /* Drain until below new high water mark */
1777 1.185 ad sysctl_unlock();
1778 1.183 ad mutex_enter(&bufcache_lock);
1779 1.183 ad while ((t = bufmem - bufmem_hiwater) >= 0) {
1780 1.183 ad rv = buf_drain(t / (2 * 1024));
1781 1.183 ad if (rv <= 0)
1782 1.183 ad break;
1783 1.183 ad }
1784 1.183 ad mutex_exit(&bufcache_lock);
1785 1.185 ad sysctl_relock();
1786 1.100 pk
1787 1.100 pk return 0;
1788 1.100 pk }
1789 1.100 pk
1790 1.100 pk SYSCTL_SETUP(sysctl_kern_buf_setup, "sysctl kern.buf subtree setup")
1791 1.100 pk {
1792 1.100 pk
1793 1.119 atatat sysctl_createv(clog, 0, NULL, NULL,
1794 1.119 atatat CTLFLAG_PERMANENT,
1795 1.104 atatat CTLTYPE_NODE, "kern", NULL,
1796 1.104 atatat NULL, 0, NULL, 0,
1797 1.104 atatat CTL_KERN, CTL_EOL);
1798 1.119 atatat sysctl_createv(clog, 0, NULL, NULL,
1799 1.119 atatat CTLFLAG_PERMANENT,
1800 1.125 atatat CTLTYPE_NODE, "buf",
1801 1.125 atatat SYSCTL_DESCR("Kernel buffer cache information"),
1802 1.100 pk sysctl_dobuf, 0, NULL, 0,
1803 1.100 pk CTL_KERN, KERN_BUF, CTL_EOL);
1804 1.104 atatat }
1805 1.104 atatat
1806 1.104 atatat SYSCTL_SETUP(sysctl_vm_buf_setup, "sysctl vm.buf* subtree setup")
1807 1.104 atatat {
1808 1.104 atatat
1809 1.119 atatat sysctl_createv(clog, 0, NULL, NULL,
1810 1.119 atatat CTLFLAG_PERMANENT,
1811 1.104 atatat CTLTYPE_NODE, "vm", NULL,
1812 1.104 atatat NULL, 0, NULL, 0,
1813 1.104 atatat CTL_VM, CTL_EOL);
1814 1.100 pk
1815 1.119 atatat sysctl_createv(clog, 0, NULL, NULL,
1816 1.119 atatat CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1817 1.125 atatat CTLTYPE_INT, "bufcache",
1818 1.139 jrf SYSCTL_DESCR("Percentage of physical memory to use for "
1819 1.125 atatat "buffer cache"),
1820 1.183 ad sysctl_bufvm_update, 0, &bufcache, 0,
1821 1.117 atatat CTL_VM, CTL_CREATE, CTL_EOL);
1822 1.119 atatat sysctl_createv(clog, 0, NULL, NULL,
1823 1.122 simonb CTLFLAG_PERMANENT|CTLFLAG_READONLY,
1824 1.183 ad CTLTYPE_INT, "bufmem",
1825 1.139 jrf SYSCTL_DESCR("Amount of kernel memory used by buffer "
1826 1.125 atatat "cache"),
1827 1.122 simonb NULL, 0, &bufmem, 0,
1828 1.122 simonb CTL_VM, CTL_CREATE, CTL_EOL);
1829 1.122 simonb sysctl_createv(clog, 0, NULL, NULL,
1830 1.119 atatat CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1831 1.183 ad CTLTYPE_INT, "bufmem_lowater",
1832 1.125 atatat SYSCTL_DESCR("Minimum amount of kernel memory to "
1833 1.125 atatat "reserve for buffer cache"),
1834 1.117 atatat sysctl_bufvm_update, 0, &bufmem_lowater, 0,
1835 1.117 atatat CTL_VM, CTL_CREATE, CTL_EOL);
1836 1.119 atatat sysctl_createv(clog, 0, NULL, NULL,
1837 1.119 atatat CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1838 1.183 ad CTLTYPE_INT, "bufmem_hiwater",
1839 1.125 atatat SYSCTL_DESCR("Maximum amount of kernel memory to use "
1840 1.125 atatat "for buffer cache"),
1841 1.117 atatat sysctl_bufvm_update, 0, &bufmem_hiwater, 0,
1842 1.117 atatat CTL_VM, CTL_CREATE, CTL_EOL);
1843 1.100 pk }
1844 1.100 pk
1845 1.36 cgd #ifdef DEBUG
1846 1.31 cgd /*
1847 1.31 cgd * Print out statistics on the current allocation of the buffer pool.
1848 1.31 cgd * Can be enabled to print out on every ``sync'' by setting "syncprt"
1849 1.31 cgd * in vfs_syscalls.c using sysctl.
1850 1.31 cgd */
1851 1.31 cgd void
1852 1.101 thorpej vfs_bufstats(void)
1853 1.31 cgd {
1854 1.183 ad int i, j, count;
1855 1.183 ad buf_t *bp;
1856 1.131 yamt struct bqueue *dp;
1857 1.72 simonb int counts[(MAXBSIZE / PAGE_SIZE) + 1];
1858 1.145 christos static const char *bname[BQUEUES] = { "LOCKED", "LRU", "AGE" };
1859 1.71 thorpej
1860 1.31 cgd for (dp = bufqueues, i = 0; dp < &bufqueues[BQUEUES]; dp++, i++) {
1861 1.31 cgd count = 0;
1862 1.71 thorpej for (j = 0; j <= MAXBSIZE/PAGE_SIZE; j++)
1863 1.31 cgd counts[j] = 0;
1864 1.131 yamt TAILQ_FOREACH(bp, &dp->bq_queue, b_freelist) {
1865 1.71 thorpej counts[bp->b_bufsize/PAGE_SIZE]++;
1866 1.31 cgd count++;
1867 1.31 cgd }
1868 1.48 christos printf("%s: total-%d", bname[i], count);
1869 1.71 thorpej for (j = 0; j <= MAXBSIZE/PAGE_SIZE; j++)
1870 1.31 cgd if (counts[j] != 0)
1871 1.71 thorpej printf(", %d-%d", j * PAGE_SIZE, counts[j]);
1872 1.48 christos printf("\n");
1873 1.31 cgd }
1874 1.31 cgd }
1875 1.36 cgd #endif /* DEBUG */
1876 1.149 yamt
1877 1.150 yamt /* ------------------------------ */
1878 1.150 yamt
1879 1.183 ad buf_t *
1880 1.183 ad getiobuf(struct vnode *vp, bool waitok)
1881 1.149 yamt {
1882 1.183 ad buf_t *bp;
1883 1.149 yamt
1884 1.183 ad bp = pool_cache_get(bufio_cache, (waitok ? PR_WAITOK : PR_NOWAIT));
1885 1.183 ad if (bp == NULL)
1886 1.183 ad return bp;
1887 1.149 yamt
1888 1.183 ad buf_init(bp);
1889 1.149 yamt
1890 1.183 ad if ((bp->b_vp = vp) == NULL)
1891 1.183 ad bp->b_objlock = &buffer_lock;
1892 1.183 ad else
1893 1.183 ad bp->b_objlock = &vp->v_interlock;
1894 1.183 ad
1895 1.183 ad return bp;
1896 1.149 yamt }
1897 1.149 yamt
1898 1.149 yamt void
1899 1.183 ad putiobuf(buf_t *bp)
1900 1.149 yamt {
1901 1.149 yamt
1902 1.183 ad buf_destroy(bp);
1903 1.183 ad pool_cache_put(bufio_cache, bp);
1904 1.149 yamt }
1905 1.152 yamt
1906 1.152 yamt /*
1907 1.152 yamt * nestiobuf_iodone: b_iodone callback for nested buffers.
1908 1.152 yamt */
1909 1.152 yamt
1910 1.167 reinoud void
1911 1.183 ad nestiobuf_iodone(buf_t *bp)
1912 1.152 yamt {
1913 1.183 ad buf_t *mbp = bp->b_private;
1914 1.152 yamt int error;
1915 1.155 reinoud int donebytes;
1916 1.152 yamt
1917 1.155 reinoud KASSERT(bp->b_bcount <= bp->b_bufsize);
1918 1.152 yamt KASSERT(mbp != bp);
1919 1.155 reinoud
1920 1.155 reinoud error = 0;
1921 1.183 ad if (bp->b_error == 0 &&
1922 1.183 ad (bp->b_bcount < bp->b_bufsize || bp->b_resid > 0)) {
1923 1.155 reinoud /*
1924 1.155 reinoud * Not all got transfered, raise an error. We have no way to
1925 1.155 reinoud * propagate these conditions to mbp.
1926 1.155 reinoud */
1927 1.155 reinoud error = EIO;
1928 1.152 yamt }
1929 1.155 reinoud
1930 1.156 yamt donebytes = bp->b_bufsize;
1931 1.155 reinoud
1932 1.152 yamt putiobuf(bp);
1933 1.152 yamt nestiobuf_done(mbp, donebytes, error);
1934 1.152 yamt }
1935 1.152 yamt
1936 1.152 yamt /*
1937 1.152 yamt * nestiobuf_setup: setup a "nested" buffer.
1938 1.152 yamt *
1939 1.152 yamt * => 'mbp' is a "master" buffer which is being divided into sub pieces.
1940 1.190 yamt * => 'bp' should be a buffer allocated by getiobuf.
1941 1.152 yamt * => 'offset' is a byte offset in the master buffer.
1942 1.152 yamt * => 'size' is a size in bytes of this nested buffer.
1943 1.152 yamt */
1944 1.152 yamt
1945 1.152 yamt void
1946 1.183 ad nestiobuf_setup(buf_t *mbp, buf_t *bp, int offset, size_t size)
1947 1.152 yamt {
1948 1.152 yamt const int b_read = mbp->b_flags & B_READ;
1949 1.152 yamt struct vnode *vp = mbp->b_vp;
1950 1.152 yamt
1951 1.152 yamt KASSERT(mbp->b_bcount >= offset + size);
1952 1.152 yamt bp->b_vp = vp;
1953 1.183 ad bp->b_objlock = mbp->b_objlock;
1954 1.183 ad bp->b_cflags = BC_BUSY;
1955 1.183 ad bp->b_flags = B_ASYNC | b_read;
1956 1.152 yamt bp->b_iodone = nestiobuf_iodone;
1957 1.170 christos bp->b_data = (char *)mbp->b_data + offset;
1958 1.152 yamt bp->b_resid = bp->b_bcount = size;
1959 1.152 yamt bp->b_bufsize = bp->b_bcount;
1960 1.152 yamt bp->b_private = mbp;
1961 1.152 yamt BIO_COPYPRIO(bp, mbp);
1962 1.152 yamt if (!b_read && vp != NULL) {
1963 1.183 ad mutex_enter(&vp->v_interlock);
1964 1.183 ad vp->v_numoutput++;
1965 1.183 ad mutex_exit(&vp->v_interlock);
1966 1.152 yamt }
1967 1.152 yamt }
1968 1.152 yamt
1969 1.152 yamt /*
1970 1.152 yamt * nestiobuf_done: propagate completion to the master buffer.
1971 1.152 yamt *
1972 1.152 yamt * => 'donebytes' specifies how many bytes in the 'mbp' is completed.
1973 1.152 yamt * => 'error' is an errno(2) that 'donebytes' has been completed with.
1974 1.152 yamt */
1975 1.152 yamt
1976 1.152 yamt void
1977 1.183 ad nestiobuf_done(buf_t *mbp, int donebytes, int error)
1978 1.152 yamt {
1979 1.152 yamt
1980 1.152 yamt if (donebytes == 0) {
1981 1.152 yamt return;
1982 1.152 yamt }
1983 1.183 ad mutex_enter(mbp->b_objlock);
1984 1.152 yamt KASSERT(mbp->b_resid >= donebytes);
1985 1.152 yamt mbp->b_resid -= donebytes;
1986 1.183 ad mbp->b_error = error;
1987 1.152 yamt if (mbp->b_resid == 0) {
1988 1.183 ad mutex_exit(mbp->b_objlock);
1989 1.183 ad biodone(mbp);
1990 1.183 ad } else
1991 1.183 ad mutex_exit(mbp->b_objlock);
1992 1.183 ad }
1993 1.183 ad
1994 1.183 ad void
1995 1.183 ad buf_init(buf_t *bp)
1996 1.183 ad {
1997 1.183 ad
1998 1.183 ad LIST_INIT(&bp->b_dep);
1999 1.183 ad cv_init(&bp->b_busy, "biolock");
2000 1.183 ad cv_init(&bp->b_done, "biowait");
2001 1.183 ad bp->b_dev = NODEV;
2002 1.183 ad bp->b_error = 0;
2003 1.183 ad bp->b_flags = 0;
2004 1.183 ad bp->b_cflags = 0;
2005 1.183 ad bp->b_oflags = 0;
2006 1.183 ad bp->b_objlock = &buffer_lock;
2007 1.183 ad bp->b_iodone = NULL;
2008 1.183 ad BIO_SETPRIO(bp, BPRIO_DEFAULT);
2009 1.183 ad }
2010 1.183 ad
2011 1.183 ad void
2012 1.183 ad buf_destroy(buf_t *bp)
2013 1.183 ad {
2014 1.183 ad
2015 1.183 ad cv_destroy(&bp->b_done);
2016 1.183 ad cv_destroy(&bp->b_busy);
2017 1.183 ad }
2018 1.183 ad
2019 1.183 ad int
2020 1.188 ad bbusy(buf_t *bp, bool intr, int timo, kmutex_t *interlock)
2021 1.183 ad {
2022 1.183 ad int error;
2023 1.183 ad
2024 1.183 ad KASSERT(mutex_owned(&bufcache_lock));
2025 1.183 ad
2026 1.183 ad if ((bp->b_cflags & BC_BUSY) != 0) {
2027 1.183 ad if (curlwp == uvm.pagedaemon_lwp)
2028 1.183 ad return EDEADLK;
2029 1.183 ad bp->b_cflags |= BC_WANTED;
2030 1.183 ad bref(bp);
2031 1.188 ad if (interlock != NULL)
2032 1.188 ad mutex_exit(interlock);
2033 1.183 ad if (intr) {
2034 1.183 ad error = cv_timedwait_sig(&bp->b_busy, &bufcache_lock,
2035 1.183 ad timo);
2036 1.183 ad } else {
2037 1.183 ad error = cv_timedwait(&bp->b_busy, &bufcache_lock,
2038 1.183 ad timo);
2039 1.152 yamt }
2040 1.183 ad brele(bp);
2041 1.188 ad if (interlock != NULL)
2042 1.188 ad mutex_enter(interlock);
2043 1.183 ad if (error != 0)
2044 1.183 ad return error;
2045 1.183 ad return EPASSTHROUGH;
2046 1.152 yamt }
2047 1.183 ad bp->b_cflags |= BC_BUSY;
2048 1.183 ad
2049 1.183 ad return 0;
2050 1.152 yamt }
2051