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