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