vfs_bio.c revision 1.92 1 1.92 yamt /* $NetBSD: vfs_bio.c,v 1.92 2003/04/09 12:55:51 yamt 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.92 yamt __KERNEL_RCSID(0, "$NetBSD: vfs_bio.c,v 1.92 2003/04/09 12:55:51 yamt 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.91 thorpej BUF_INIT(bp);
179 1.31 cgd bp->b_dev = NODEV;
180 1.31 cgd bp->b_vnbufs.le_next = NOLIST;
181 1.31 cgd bp->b_data = buffers + i * MAXBSIZE;
182 1.31 cgd if (i < residual)
183 1.71 thorpej bp->b_bufsize = (base + 1) * PAGE_SIZE;
184 1.31 cgd else
185 1.71 thorpej bp->b_bufsize = base * PAGE_SIZE;
186 1.31 cgd bp->b_flags = B_INVAL;
187 1.31 cgd dp = bp->b_bufsize ? &bufqueues[BQ_AGE] : &bufqueues[BQ_EMPTY];
188 1.31 cgd binsheadfree(bp, dp);
189 1.31 cgd binshash(bp, &invalhash);
190 1.31 cgd }
191 1.31 cgd }
192 1.31 cgd
193 1.40 christos static __inline struct buf *
194 1.34 mycroft bio_doread(vp, blkno, size, cred, async)
195 1.31 cgd struct vnode *vp;
196 1.31 cgd daddr_t blkno;
197 1.31 cgd int size;
198 1.31 cgd struct ucred *cred;
199 1.34 mycroft int async;
200 1.31 cgd {
201 1.66 augustss struct buf *bp;
202 1.86 thorpej struct lwp *l = (curlwp != NULL ? curlwp : &lwp0); /* XXX */
203 1.86 thorpej struct proc *p = l->l_proc;
204 1.31 cgd
205 1.34 mycroft bp = getblk(vp, blkno, size, 0, 0);
206 1.31 cgd
207 1.86 thorpej #ifdef DIAGNOSTIC
208 1.86 thorpej if (bp == NULL) {
209 1.86 thorpej panic("bio_doread: no such buf");
210 1.86 thorpej }
211 1.86 thorpej #endif
212 1.86 thorpej
213 1.31 cgd /*
214 1.34 mycroft * If buffer does not have data valid, start a read.
215 1.31 cgd * Note that if buffer is B_INVAL, getblk() won't return it.
216 1.87 pk * Therefore, it's valid if its I/O has completed or been delayed.
217 1.31 cgd */
218 1.34 mycroft if (!ISSET(bp->b_flags, (B_DONE | B_DELWRI))) {
219 1.73 chs /* Start I/O for the buffer. */
220 1.34 mycroft SET(bp->b_flags, B_READ | async);
221 1.34 mycroft VOP_STRATEGY(bp);
222 1.31 cgd
223 1.34 mycroft /* Pay for the read. */
224 1.49 cgd p->p_stats->p_ru.ru_inblock++;
225 1.34 mycroft } else if (async) {
226 1.34 mycroft brelse(bp);
227 1.31 cgd }
228 1.31 cgd
229 1.34 mycroft return (bp);
230 1.34 mycroft }
231 1.34 mycroft
232 1.34 mycroft /*
233 1.34 mycroft * Read a disk block.
234 1.34 mycroft * This algorithm described in Bach (p.54).
235 1.34 mycroft */
236 1.40 christos int
237 1.34 mycroft bread(vp, blkno, size, cred, bpp)
238 1.34 mycroft struct vnode *vp;
239 1.34 mycroft daddr_t blkno;
240 1.34 mycroft int size;
241 1.34 mycroft struct ucred *cred;
242 1.34 mycroft struct buf **bpp;
243 1.34 mycroft {
244 1.66 augustss struct buf *bp;
245 1.34 mycroft
246 1.34 mycroft /* Get buffer for block. */
247 1.34 mycroft bp = *bpp = bio_doread(vp, blkno, size, cred, 0);
248 1.31 cgd
249 1.80 chs /* Wait for the read to complete, and return result. */
250 1.31 cgd return (biowait(bp));
251 1.31 cgd }
252 1.31 cgd
253 1.31 cgd /*
254 1.31 cgd * Read-ahead multiple disk blocks. The first is sync, the rest async.
255 1.31 cgd * Trivial modification to the breada algorithm presented in Bach (p.55).
256 1.31 cgd */
257 1.40 christos int
258 1.31 cgd breadn(vp, blkno, size, rablks, rasizes, nrablks, cred, bpp)
259 1.31 cgd struct vnode *vp;
260 1.31 cgd daddr_t blkno; int size;
261 1.31 cgd daddr_t rablks[]; int rasizes[];
262 1.31 cgd int nrablks;
263 1.31 cgd struct ucred *cred;
264 1.31 cgd struct buf **bpp;
265 1.31 cgd {
266 1.66 augustss struct buf *bp;
267 1.31 cgd int i;
268 1.31 cgd
269 1.34 mycroft bp = *bpp = bio_doread(vp, blkno, size, cred, 0);
270 1.31 cgd
271 1.31 cgd /*
272 1.31 cgd * For each of the read-ahead blocks, start a read, if necessary.
273 1.31 cgd */
274 1.31 cgd for (i = 0; i < nrablks; i++) {
275 1.31 cgd /* If it's in the cache, just go on to next one. */
276 1.31 cgd if (incore(vp, rablks[i]))
277 1.31 cgd continue;
278 1.31 cgd
279 1.31 cgd /* Get a buffer for the read-ahead block */
280 1.34 mycroft (void) bio_doread(vp, rablks[i], rasizes[i], cred, B_ASYNC);
281 1.31 cgd }
282 1.31 cgd
283 1.80 chs /* Otherwise, we had to start a read for it; wait until it's valid. */
284 1.31 cgd return (biowait(bp));
285 1.31 cgd }
286 1.31 cgd
287 1.31 cgd /*
288 1.31 cgd * Read with single-block read-ahead. Defined in Bach (p.55), but
289 1.31 cgd * implemented as a call to breadn().
290 1.31 cgd * XXX for compatibility with old file systems.
291 1.31 cgd */
292 1.40 christos int
293 1.31 cgd breada(vp, blkno, size, rablkno, rabsize, cred, bpp)
294 1.31 cgd struct vnode *vp;
295 1.31 cgd daddr_t blkno; int size;
296 1.31 cgd daddr_t rablkno; int rabsize;
297 1.31 cgd struct ucred *cred;
298 1.31 cgd struct buf **bpp;
299 1.31 cgd {
300 1.34 mycroft
301 1.31 cgd return (breadn(vp, blkno, size, &rablkno, &rabsize, 1, cred, bpp));
302 1.31 cgd }
303 1.31 cgd
304 1.31 cgd /*
305 1.31 cgd * Block write. Described in Bach (p.56)
306 1.31 cgd */
307 1.40 christos int
308 1.31 cgd bwrite(bp)
309 1.31 cgd struct buf *bp;
310 1.31 cgd {
311 1.44 pk int rv, sync, wasdelayed, s;
312 1.86 thorpej struct lwp *l = (curlwp != NULL ? curlwp : &lwp0); /* XXX */
313 1.86 thorpej struct proc *p = l->l_proc;
314 1.59 fvdl struct vnode *vp;
315 1.59 fvdl struct mount *mp;
316 1.31 cgd
317 1.87 pk KASSERT(ISSET(bp->b_flags, B_BUSY));
318 1.87 pk
319 1.76 chs vp = bp->b_vp;
320 1.76 chs if (vp != NULL) {
321 1.76 chs if (vp->v_type == VBLK)
322 1.76 chs mp = vp->v_specmountpoint;
323 1.76 chs else
324 1.76 chs mp = vp->v_mount;
325 1.76 chs } else {
326 1.76 chs mp = NULL;
327 1.76 chs }
328 1.76 chs
329 1.38 cgd /*
330 1.38 cgd * Remember buffer type, to switch on it later. If the write was
331 1.38 cgd * synchronous, but the file system was mounted with MNT_ASYNC,
332 1.38 cgd * convert it to a delayed write.
333 1.38 cgd * XXX note that this relies on delayed tape writes being converted
334 1.38 cgd * to async, not sync writes (which is safe, but ugly).
335 1.38 cgd */
336 1.31 cgd sync = !ISSET(bp->b_flags, B_ASYNC);
337 1.76 chs if (sync && mp != NULL && ISSET(mp->mnt_flag, MNT_ASYNC)) {
338 1.37 cgd bdwrite(bp);
339 1.37 cgd return (0);
340 1.37 cgd }
341 1.46 mycroft
342 1.59 fvdl /*
343 1.59 fvdl * Collect statistics on synchronous and asynchronous writes.
344 1.59 fvdl * Writes to block devices are charged to their associated
345 1.59 fvdl * filesystem (if any).
346 1.59 fvdl */
347 1.76 chs if (mp != NULL) {
348 1.76 chs if (sync)
349 1.76 chs mp->mnt_stat.f_syncwrites++;
350 1.59 fvdl else
351 1.76 chs mp->mnt_stat.f_asyncwrites++;
352 1.59 fvdl }
353 1.59 fvdl
354 1.31 cgd wasdelayed = ISSET(bp->b_flags, B_DELWRI);
355 1.31 cgd
356 1.44 pk s = splbio();
357 1.87 pk simple_lock(&bp->b_interlock);
358 1.46 mycroft
359 1.60 fvdl CLR(bp->b_flags, (B_READ | B_DONE | B_ERROR | B_DELWRI));
360 1.60 fvdl
361 1.46 mycroft /*
362 1.46 mycroft * Pay for the I/O operation and make sure the buf is on the correct
363 1.46 mycroft * vnode queue.
364 1.46 mycroft */
365 1.46 mycroft if (wasdelayed)
366 1.46 mycroft reassignbuf(bp, bp->b_vp);
367 1.46 mycroft else
368 1.49 cgd p->p_stats->p_ru.ru_oublock++;
369 1.32 mycroft
370 1.31 cgd /* Initiate disk write. Make sure the appropriate party is charged. */
371 1.87 pk V_INCR_NUMOUTPUT(bp->b_vp);
372 1.87 pk simple_unlock(&bp->b_interlock);
373 1.44 pk splx(s);
374 1.46 mycroft
375 1.31 cgd VOP_STRATEGY(bp);
376 1.31 cgd
377 1.34 mycroft if (sync) {
378 1.46 mycroft /* If I/O was synchronous, wait for it to complete. */
379 1.31 cgd rv = biowait(bp);
380 1.31 cgd
381 1.34 mycroft /* Release the buffer. */
382 1.31 cgd brelse(bp);
383 1.34 mycroft
384 1.34 mycroft return (rv);
385 1.34 mycroft } else {
386 1.34 mycroft return (0);
387 1.31 cgd }
388 1.31 cgd }
389 1.31 cgd
390 1.31 cgd int
391 1.40 christos vn_bwrite(v)
392 1.40 christos void *v;
393 1.31 cgd {
394 1.40 christos struct vop_bwrite_args *ap = v;
395 1.34 mycroft
396 1.31 cgd return (bwrite(ap->a_bp));
397 1.31 cgd }
398 1.31 cgd
399 1.31 cgd /*
400 1.31 cgd * Delayed write.
401 1.31 cgd *
402 1.31 cgd * The buffer is marked dirty, but is not queued for I/O.
403 1.31 cgd * This routine should be used when the buffer is expected
404 1.31 cgd * to be modified again soon, typically a small write that
405 1.31 cgd * partially fills a buffer.
406 1.31 cgd *
407 1.31 cgd * NB: magnetic tapes cannot be delayed; they must be
408 1.31 cgd * written in the order that the writes are requested.
409 1.31 cgd *
410 1.31 cgd * Described in Leffler, et al. (pp. 208-213).
411 1.31 cgd */
412 1.31 cgd void
413 1.31 cgd bdwrite(bp)
414 1.31 cgd struct buf *bp;
415 1.31 cgd {
416 1.86 thorpej struct lwp *l = (curlwp != NULL ? curlwp : &lwp0); /* XXX */
417 1.86 thorpej struct proc *p = l->l_proc;
418 1.85 gehenna const struct bdevsw *bdev;
419 1.45 pk int s;
420 1.31 cgd
421 1.87 pk KASSERT(ISSET(bp->b_flags, B_BUSY));
422 1.87 pk
423 1.46 mycroft /* If this is a tape block, write the block now. */
424 1.90 pk bdev = bdevsw_lookup(bp->b_dev);
425 1.90 pk if (bdev != NULL && bdev->d_type == D_TAPE) {
426 1.90 pk bawrite(bp);
427 1.90 pk return;
428 1.46 mycroft }
429 1.46 mycroft
430 1.31 cgd /*
431 1.31 cgd * If the block hasn't been seen before:
432 1.31 cgd * (1) Mark it as having been seen,
433 1.45 pk * (2) Charge for the write,
434 1.45 pk * (3) Make sure it's on its vnode's correct block list.
435 1.31 cgd */
436 1.60 fvdl s = splbio();
437 1.87 pk simple_lock(&bp->b_interlock);
438 1.60 fvdl
439 1.31 cgd if (!ISSET(bp->b_flags, B_DELWRI)) {
440 1.31 cgd SET(bp->b_flags, B_DELWRI);
441 1.49 cgd p->p_stats->p_ru.ru_oublock++;
442 1.31 cgd reassignbuf(bp, bp->b_vp);
443 1.31 cgd }
444 1.31 cgd
445 1.31 cgd /* Otherwise, the "write" is done, so mark and release the buffer. */
446 1.92 yamt CLR(bp->b_flags, B_DONE);
447 1.87 pk simple_unlock(&bp->b_interlock);
448 1.60 fvdl splx(s);
449 1.60 fvdl
450 1.31 cgd brelse(bp);
451 1.31 cgd }
452 1.31 cgd
453 1.31 cgd /*
454 1.31 cgd * Asynchronous block write; just an asynchronous bwrite().
455 1.31 cgd */
456 1.31 cgd void
457 1.31 cgd bawrite(bp)
458 1.31 cgd struct buf *bp;
459 1.31 cgd {
460 1.87 pk int s;
461 1.31 cgd
462 1.87 pk KASSERT(ISSET(bp->b_flags, B_BUSY));
463 1.87 pk
464 1.87 pk s = splbio();
465 1.87 pk simple_lock(&bp->b_interlock);
466 1.31 cgd SET(bp->b_flags, B_ASYNC);
467 1.87 pk simple_unlock(&bp->b_interlock);
468 1.87 pk splx(s);
469 1.31 cgd VOP_BWRITE(bp);
470 1.31 cgd }
471 1.31 cgd
472 1.31 cgd /*
473 1.59 fvdl * Same as first half of bdwrite, mark buffer dirty, but do not release it.
474 1.88 pk * Call at splbio() and with the buffer interlock locked.
475 1.88 pk * Note: called only from biodone() through ffs softdep's bioops.io_complete()
476 1.59 fvdl */
477 1.59 fvdl void
478 1.59 fvdl bdirty(bp)
479 1.59 fvdl struct buf *bp;
480 1.59 fvdl {
481 1.86 thorpej struct lwp *l = (curlwp != NULL ? curlwp : &lwp0); /* XXX */
482 1.86 thorpej struct proc *p = l->l_proc;
483 1.59 fvdl
484 1.87 pk KASSERT(ISSET(bp->b_flags, B_BUSY));
485 1.88 pk LOCK_ASSERT(simple_lock_held(&bp->b_interlock));
486 1.61 fvdl
487 1.61 fvdl CLR(bp->b_flags, B_AGE);
488 1.60 fvdl
489 1.59 fvdl if (!ISSET(bp->b_flags, B_DELWRI)) {
490 1.59 fvdl SET(bp->b_flags, B_DELWRI);
491 1.59 fvdl p->p_stats->p_ru.ru_oublock++;
492 1.59 fvdl reassignbuf(bp, bp->b_vp);
493 1.59 fvdl }
494 1.59 fvdl }
495 1.59 fvdl
496 1.59 fvdl /*
497 1.31 cgd * Release a buffer on to the free lists.
498 1.31 cgd * Described in Bach (p. 46).
499 1.31 cgd */
500 1.31 cgd void
501 1.31 cgd brelse(bp)
502 1.31 cgd struct buf *bp;
503 1.31 cgd {
504 1.31 cgd struct bqueues *bufq;
505 1.31 cgd int s;
506 1.31 cgd
507 1.73 chs KASSERT(ISSET(bp->b_flags, B_BUSY));
508 1.73 chs
509 1.87 pk /* Block disk interrupts. */
510 1.87 pk s = splbio();
511 1.87 pk simple_lock(&bqueue_slock);
512 1.87 pk simple_lock(&bp->b_interlock);
513 1.87 pk
514 1.31 cgd /* Wake up any processes waiting for any buffer to become free. */
515 1.31 cgd if (needbuffer) {
516 1.31 cgd needbuffer = 0;
517 1.31 cgd wakeup(&needbuffer);
518 1.31 cgd }
519 1.31 cgd
520 1.31 cgd /* Wake up any proceeses waiting for _this_ buffer to become free. */
521 1.31 cgd if (ISSET(bp->b_flags, B_WANTED)) {
522 1.57 mycroft CLR(bp->b_flags, B_WANTED|B_AGE);
523 1.31 cgd wakeup(bp);
524 1.31 cgd }
525 1.31 cgd
526 1.31 cgd /*
527 1.31 cgd * Determine which queue the buffer should be on, then put it there.
528 1.31 cgd */
529 1.31 cgd
530 1.31 cgd /* If it's locked, don't report an error; try again later. */
531 1.31 cgd if (ISSET(bp->b_flags, (B_LOCKED|B_ERROR)) == (B_LOCKED|B_ERROR))
532 1.31 cgd CLR(bp->b_flags, B_ERROR);
533 1.31 cgd
534 1.31 cgd /* If it's not cacheable, or an error, mark it invalid. */
535 1.31 cgd if (ISSET(bp->b_flags, (B_NOCACHE|B_ERROR)))
536 1.31 cgd SET(bp->b_flags, B_INVAL);
537 1.31 cgd
538 1.50 mycroft if (ISSET(bp->b_flags, B_VFLUSH)) {
539 1.50 mycroft /*
540 1.50 mycroft * This is a delayed write buffer that was just flushed to
541 1.50 mycroft * disk. It is still on the LRU queue. If it's become
542 1.50 mycroft * invalid, then we need to move it to a different queue;
543 1.50 mycroft * otherwise leave it in its current position.
544 1.50 mycroft */
545 1.50 mycroft CLR(bp->b_flags, B_VFLUSH);
546 1.50 mycroft if (!ISSET(bp->b_flags, B_ERROR|B_INVAL|B_LOCKED|B_AGE))
547 1.50 mycroft goto already_queued;
548 1.50 mycroft else
549 1.50 mycroft bremfree(bp);
550 1.50 mycroft }
551 1.50 mycroft
552 1.31 cgd if ((bp->b_bufsize <= 0) || ISSET(bp->b_flags, B_INVAL)) {
553 1.31 cgd /*
554 1.31 cgd * If it's invalid or empty, dissociate it from its vnode
555 1.31 cgd * and put on the head of the appropriate queue.
556 1.31 cgd */
557 1.59 fvdl if (LIST_FIRST(&bp->b_dep) != NULL && bioops.io_deallocate)
558 1.59 fvdl (*bioops.io_deallocate)(bp);
559 1.59 fvdl CLR(bp->b_flags, B_DONE|B_DELWRI);
560 1.59 fvdl if (bp->b_vp) {
561 1.59 fvdl reassignbuf(bp, bp->b_vp);
562 1.31 cgd brelvp(bp);
563 1.59 fvdl }
564 1.31 cgd if (bp->b_bufsize <= 0)
565 1.31 cgd /* no data */
566 1.31 cgd bufq = &bufqueues[BQ_EMPTY];
567 1.31 cgd else
568 1.31 cgd /* invalid data */
569 1.31 cgd bufq = &bufqueues[BQ_AGE];
570 1.31 cgd binsheadfree(bp, bufq);
571 1.31 cgd } else {
572 1.31 cgd /*
573 1.31 cgd * It has valid data. Put it on the end of the appropriate
574 1.31 cgd * queue, so that it'll stick around for as long as possible.
575 1.67 fvdl * If buf is AGE, but has dependencies, must put it on last
576 1.67 fvdl * bufqueue to be scanned, ie LRU. This protects against the
577 1.67 fvdl * livelock where BQ_AGE only has buffers with dependencies,
578 1.67 fvdl * and we thus never get to the dependent buffers in BQ_LRU.
579 1.31 cgd */
580 1.31 cgd if (ISSET(bp->b_flags, B_LOCKED))
581 1.31 cgd /* locked in core */
582 1.31 cgd bufq = &bufqueues[BQ_LOCKED];
583 1.67 fvdl else if (!ISSET(bp->b_flags, B_AGE))
584 1.31 cgd /* valid data */
585 1.31 cgd bufq = &bufqueues[BQ_LRU];
586 1.67 fvdl else {
587 1.67 fvdl /* stale but valid data */
588 1.67 fvdl int has_deps;
589 1.67 fvdl
590 1.67 fvdl if (LIST_FIRST(&bp->b_dep) != NULL &&
591 1.67 fvdl bioops.io_countdeps)
592 1.67 fvdl has_deps = (*bioops.io_countdeps)(bp, 0);
593 1.67 fvdl else
594 1.67 fvdl has_deps = 0;
595 1.67 fvdl bufq = has_deps ? &bufqueues[BQ_LRU] :
596 1.67 fvdl &bufqueues[BQ_AGE];
597 1.67 fvdl }
598 1.31 cgd binstailfree(bp, bufq);
599 1.31 cgd }
600 1.31 cgd
601 1.50 mycroft already_queued:
602 1.31 cgd /* Unlock the buffer. */
603 1.83 hannken CLR(bp->b_flags, B_AGE|B_ASYNC|B_BUSY|B_NOCACHE);
604 1.73 chs SET(bp->b_flags, B_CACHE);
605 1.31 cgd
606 1.31 cgd /* Allow disk interrupts. */
607 1.87 pk simple_unlock(&bp->b_interlock);
608 1.87 pk simple_unlock(&bqueue_slock);
609 1.31 cgd splx(s);
610 1.31 cgd }
611 1.31 cgd
612 1.31 cgd /*
613 1.31 cgd * Determine if a block is in the cache.
614 1.31 cgd * Just look on what would be its hash chain. If it's there, return
615 1.31 cgd * a pointer to it, unless it's marked invalid. If it's marked invalid,
616 1.31 cgd * we normally don't return the buffer, unless the caller explicitly
617 1.31 cgd * wants us to.
618 1.31 cgd */
619 1.31 cgd struct buf *
620 1.31 cgd incore(vp, blkno)
621 1.31 cgd struct vnode *vp;
622 1.31 cgd daddr_t blkno;
623 1.31 cgd {
624 1.31 cgd struct buf *bp;
625 1.31 cgd
626 1.31 cgd /* Search hash chain */
627 1.84 matt LIST_FOREACH(bp, BUFHASH(vp, blkno), b_hash) {
628 1.31 cgd if (bp->b_lblkno == blkno && bp->b_vp == vp &&
629 1.31 cgd !ISSET(bp->b_flags, B_INVAL))
630 1.31 cgd return (bp);
631 1.31 cgd }
632 1.31 cgd
633 1.73 chs return (NULL);
634 1.31 cgd }
635 1.31 cgd
636 1.31 cgd /*
637 1.31 cgd * Get a block of requested size that is associated with
638 1.31 cgd * a given vnode and block offset. If it is found in the
639 1.31 cgd * block cache, mark it as having been found, make it busy
640 1.31 cgd * and return it. Otherwise, return an empty block of the
641 1.31 cgd * correct size. It is up to the caller to insure that the
642 1.31 cgd * cached blocks be of the correct size.
643 1.31 cgd */
644 1.31 cgd struct buf *
645 1.31 cgd getblk(vp, blkno, size, slpflag, slptimeo)
646 1.66 augustss struct vnode *vp;
647 1.31 cgd daddr_t blkno;
648 1.31 cgd int size, slpflag, slptimeo;
649 1.31 cgd {
650 1.31 cgd struct buf *bp;
651 1.31 cgd int s, err;
652 1.31 cgd
653 1.39 cgd start:
654 1.87 pk s = splbio();
655 1.87 pk simple_lock(&bqueue_slock);
656 1.73 chs bp = incore(vp, blkno);
657 1.73 chs if (bp != NULL) {
658 1.87 pk simple_lock(&bp->b_interlock);
659 1.31 cgd if (ISSET(bp->b_flags, B_BUSY)) {
660 1.87 pk simple_unlock(&bqueue_slock);
661 1.73 chs if (curproc == uvm.pagedaemon_proc) {
662 1.87 pk simple_unlock(&bp->b_interlock);
663 1.73 chs splx(s);
664 1.73 chs return NULL;
665 1.73 chs }
666 1.31 cgd SET(bp->b_flags, B_WANTED);
667 1.87 pk err = ltsleep(bp, slpflag | (PRIBIO + 1) | PNORELOCK,
668 1.87 pk "getblk", slptimeo, &bp->b_interlock);
669 1.31 cgd splx(s);
670 1.31 cgd if (err)
671 1.31 cgd return (NULL);
672 1.31 cgd goto start;
673 1.31 cgd }
674 1.57 mycroft #ifdef DIAGNOSTIC
675 1.78 chs if (ISSET(bp->b_flags, B_DONE|B_DELWRI) &&
676 1.78 chs bp->b_bcount < size && vp->v_type != VBLK)
677 1.73 chs panic("getblk: block size invariant failed");
678 1.57 mycroft #endif
679 1.73 chs SET(bp->b_flags, B_BUSY);
680 1.73 chs bremfree(bp);
681 1.73 chs } else {
682 1.87 pk if ((bp = getnewbuf(slpflag, slptimeo)) == NULL) {
683 1.87 pk simple_unlock(&bqueue_slock);
684 1.87 pk splx(s);
685 1.31 cgd goto start;
686 1.87 pk }
687 1.73 chs
688 1.73 chs binshash(bp, BUFHASH(vp, blkno));
689 1.64 thorpej bp->b_blkno = bp->b_lblkno = bp->b_rawblkno = blkno;
690 1.31 cgd bgetvp(vp, bp);
691 1.31 cgd }
692 1.87 pk simple_unlock(&bp->b_interlock);
693 1.87 pk simple_unlock(&bqueue_slock);
694 1.87 pk splx(s);
695 1.39 cgd allocbuf(bp, size);
696 1.31 cgd return (bp);
697 1.31 cgd }
698 1.31 cgd
699 1.31 cgd /*
700 1.31 cgd * Get an empty, disassociated buffer of given size.
701 1.31 cgd */
702 1.31 cgd struct buf *
703 1.31 cgd geteblk(size)
704 1.31 cgd int size;
705 1.31 cgd {
706 1.31 cgd struct buf *bp;
707 1.87 pk int s;
708 1.31 cgd
709 1.87 pk s = splbio();
710 1.87 pk simple_lock(&bqueue_slock);
711 1.31 cgd while ((bp = getnewbuf(0, 0)) == 0)
712 1.31 cgd ;
713 1.87 pk
714 1.31 cgd SET(bp->b_flags, B_INVAL);
715 1.31 cgd binshash(bp, &invalhash);
716 1.87 pk simple_unlock(&bqueue_slock);
717 1.87 pk simple_unlock(&bp->b_interlock);
718 1.87 pk splx(s);
719 1.31 cgd allocbuf(bp, size);
720 1.31 cgd return (bp);
721 1.31 cgd }
722 1.31 cgd
723 1.31 cgd /*
724 1.31 cgd * Expand or contract the actual memory allocated to a buffer.
725 1.31 cgd *
726 1.31 cgd * If the buffer shrinks, data is lost, so it's up to the
727 1.31 cgd * caller to have written it out *first*; this routine will not
728 1.31 cgd * start a write. If the buffer grows, it's the callers
729 1.31 cgd * responsibility to fill out the buffer's additional contents.
730 1.31 cgd */
731 1.40 christos void
732 1.31 cgd allocbuf(bp, size)
733 1.31 cgd struct buf *bp;
734 1.31 cgd int size;
735 1.31 cgd {
736 1.73 chs struct buf *nbp;
737 1.73 chs vsize_t desired_size;
738 1.73 chs int s;
739 1.31 cgd
740 1.69 chs desired_size = round_page((vsize_t)size);
741 1.31 cgd if (desired_size > MAXBSIZE)
742 1.31 cgd panic("allocbuf: buffer larger than MAXBSIZE requested");
743 1.31 cgd
744 1.31 cgd if (bp->b_bufsize == desired_size)
745 1.31 cgd goto out;
746 1.31 cgd
747 1.31 cgd /*
748 1.31 cgd * If the buffer is smaller than the desired size, we need to snarf
749 1.31 cgd * it from other buffers. Get buffers (via getnewbuf()), and
750 1.31 cgd * steal their pages.
751 1.31 cgd */
752 1.31 cgd while (bp->b_bufsize < desired_size) {
753 1.31 cgd int amt;
754 1.31 cgd
755 1.31 cgd /* find a buffer */
756 1.87 pk s = splbio();
757 1.87 pk simple_lock(&bqueue_slock);
758 1.31 cgd while ((nbp = getnewbuf(0, 0)) == NULL)
759 1.31 cgd ;
760 1.73 chs
761 1.34 mycroft SET(nbp->b_flags, B_INVAL);
762 1.34 mycroft binshash(nbp, &invalhash);
763 1.31 cgd
764 1.87 pk simple_unlock(&nbp->b_interlock);
765 1.87 pk simple_unlock(&bqueue_slock);
766 1.87 pk splx(s);
767 1.87 pk
768 1.31 cgd /* and steal its pages, up to the amount we need */
769 1.31 cgd amt = min(nbp->b_bufsize, (desired_size - bp->b_bufsize));
770 1.31 cgd pagemove((nbp->b_data + nbp->b_bufsize - amt),
771 1.40 christos bp->b_data + bp->b_bufsize, amt);
772 1.31 cgd bp->b_bufsize += amt;
773 1.31 cgd nbp->b_bufsize -= amt;
774 1.31 cgd
775 1.31 cgd /* reduce transfer count if we stole some data */
776 1.31 cgd if (nbp->b_bcount > nbp->b_bufsize)
777 1.31 cgd nbp->b_bcount = nbp->b_bufsize;
778 1.31 cgd
779 1.31 cgd #ifdef DIAGNOSTIC
780 1.31 cgd if (nbp->b_bufsize < 0)
781 1.31 cgd panic("allocbuf: negative bufsize");
782 1.31 cgd #endif
783 1.31 cgd brelse(nbp);
784 1.31 cgd }
785 1.31 cgd
786 1.31 cgd /*
787 1.31 cgd * If we want a buffer smaller than the current size,
788 1.31 cgd * shrink this buffer. Grab a buf head from the EMPTY queue,
789 1.31 cgd * move a page onto it, and put it on front of the AGE queue.
790 1.31 cgd * If there are no free buffer headers, leave the buffer alone.
791 1.31 cgd */
792 1.31 cgd if (bp->b_bufsize > desired_size) {
793 1.31 cgd s = splbio();
794 1.87 pk simple_lock(&bqueue_slock);
795 1.84 matt if ((nbp = TAILQ_FIRST(&bufqueues[BQ_EMPTY])) == NULL) {
796 1.31 cgd /* No free buffer head */
797 1.87 pk simple_unlock(&bqueue_slock);
798 1.31 cgd splx(s);
799 1.31 cgd goto out;
800 1.31 cgd }
801 1.87 pk /* No need to lock nbp since it came from the empty queue */
802 1.31 cgd bremfree(nbp);
803 1.87 pk SET(nbp->b_flags, B_BUSY | B_INVAL);
804 1.87 pk simple_unlock(&bqueue_slock);
805 1.31 cgd splx(s);
806 1.31 cgd
807 1.31 cgd /* move the page to it and note this change */
808 1.31 cgd pagemove(bp->b_data + desired_size,
809 1.31 cgd nbp->b_data, bp->b_bufsize - desired_size);
810 1.31 cgd nbp->b_bufsize = bp->b_bufsize - desired_size;
811 1.31 cgd bp->b_bufsize = desired_size;
812 1.31 cgd nbp->b_bcount = 0;
813 1.31 cgd
814 1.31 cgd /* release the newly-filled buffer and leave */
815 1.31 cgd brelse(nbp);
816 1.31 cgd }
817 1.31 cgd
818 1.31 cgd out:
819 1.31 cgd bp->b_bcount = size;
820 1.31 cgd }
821 1.31 cgd
822 1.31 cgd /*
823 1.31 cgd * Find a buffer which is available for use.
824 1.31 cgd * Select something from a free list.
825 1.31 cgd * Preference is to AGE list, then LRU list.
826 1.87 pk *
827 1.87 pk * Called with buffer queues locked.
828 1.87 pk * Return buffer locked.
829 1.31 cgd */
830 1.31 cgd struct buf *
831 1.31 cgd getnewbuf(slpflag, slptimeo)
832 1.31 cgd int slpflag, slptimeo;
833 1.31 cgd {
834 1.66 augustss struct buf *bp;
835 1.31 cgd
836 1.31 cgd start:
837 1.87 pk LOCK_ASSERT(simple_lock_held(&bqueue_slock));
838 1.87 pk
839 1.84 matt if ((bp = TAILQ_FIRST(&bufqueues[BQ_AGE])) != NULL ||
840 1.84 matt (bp = TAILQ_FIRST(&bufqueues[BQ_LRU])) != NULL) {
841 1.87 pk simple_lock(&bp->b_interlock);
842 1.31 cgd bremfree(bp);
843 1.31 cgd } else {
844 1.31 cgd /* wait for a free buffer of any kind */
845 1.31 cgd needbuffer = 1;
846 1.87 pk ltsleep(&needbuffer, slpflag|(PRIBIO+1),
847 1.87 pk "getnewbuf", slptimeo, &bqueue_slock);
848 1.73 chs return (NULL);
849 1.31 cgd }
850 1.31 cgd
851 1.50 mycroft if (ISSET(bp->b_flags, B_VFLUSH)) {
852 1.50 mycroft /*
853 1.50 mycroft * This is a delayed write buffer being flushed to disk. Make
854 1.50 mycroft * sure it gets aged out of the queue when it's finished, and
855 1.50 mycroft * leave it off the LRU queue.
856 1.50 mycroft */
857 1.50 mycroft CLR(bp->b_flags, B_VFLUSH);
858 1.50 mycroft SET(bp->b_flags, B_AGE);
859 1.87 pk simple_unlock(&bp->b_interlock);
860 1.50 mycroft goto start;
861 1.50 mycroft }
862 1.50 mycroft
863 1.31 cgd /* Buffer is no longer on free lists. */
864 1.31 cgd SET(bp->b_flags, B_BUSY);
865 1.31 cgd
866 1.75 chs /*
867 1.75 chs * If buffer was a delayed write, start it and return NULL
868 1.75 chs * (since we might sleep while starting the write).
869 1.75 chs */
870 1.31 cgd if (ISSET(bp->b_flags, B_DELWRI)) {
871 1.50 mycroft /*
872 1.50 mycroft * This buffer has gone through the LRU, so make sure it gets
873 1.50 mycroft * reused ASAP.
874 1.50 mycroft */
875 1.50 mycroft SET(bp->b_flags, B_AGE);
876 1.87 pk simple_unlock(&bp->b_interlock);
877 1.89 pk simple_unlock(&bqueue_slock);
878 1.50 mycroft bawrite(bp);
879 1.89 pk simple_lock(&bqueue_slock);
880 1.75 chs return (NULL);
881 1.31 cgd }
882 1.31 cgd
883 1.31 cgd /* disassociate us from our vnode, if we had one... */
884 1.31 cgd if (bp->b_vp)
885 1.31 cgd brelvp(bp);
886 1.31 cgd
887 1.59 fvdl if (LIST_FIRST(&bp->b_dep) != NULL && bioops.io_deallocate)
888 1.59 fvdl (*bioops.io_deallocate)(bp);
889 1.59 fvdl
890 1.31 cgd /* clear out various other fields */
891 1.31 cgd bp->b_flags = B_BUSY;
892 1.31 cgd bp->b_dev = NODEV;
893 1.64 thorpej bp->b_blkno = bp->b_lblkno = bp->b_rawblkno = 0;
894 1.31 cgd bp->b_iodone = 0;
895 1.31 cgd bp->b_error = 0;
896 1.31 cgd bp->b_resid = 0;
897 1.31 cgd bp->b_bcount = 0;
898 1.31 cgd
899 1.34 mycroft bremhash(bp);
900 1.31 cgd return (bp);
901 1.31 cgd }
902 1.31 cgd
903 1.31 cgd /*
904 1.31 cgd * Wait for operations on the buffer to complete.
905 1.31 cgd * When they do, extract and return the I/O's error value.
906 1.31 cgd */
907 1.31 cgd int
908 1.31 cgd biowait(bp)
909 1.31 cgd struct buf *bp;
910 1.31 cgd {
911 1.87 pk int s, error;
912 1.59 fvdl
913 1.31 cgd s = splbio();
914 1.87 pk simple_lock(&bp->b_interlock);
915 1.80 chs while (!ISSET(bp->b_flags, B_DONE | B_DELWRI))
916 1.87 pk ltsleep(bp, PRIBIO + 1, "biowait", 0, &bp->b_interlock);
917 1.31 cgd
918 1.31 cgd /* check for interruption of I/O (e.g. via NFS), then errors. */
919 1.31 cgd if (ISSET(bp->b_flags, B_EINTR)) {
920 1.31 cgd CLR(bp->b_flags, B_EINTR);
921 1.87 pk error = EINTR;
922 1.31 cgd } else if (ISSET(bp->b_flags, B_ERROR))
923 1.87 pk error = bp->b_error ? bp->b_error : EIO;
924 1.31 cgd else
925 1.87 pk error = 0;
926 1.87 pk
927 1.87 pk simple_unlock(&bp->b_interlock);
928 1.87 pk splx(s);
929 1.87 pk return (error);
930 1.31 cgd }
931 1.31 cgd
932 1.31 cgd /*
933 1.31 cgd * Mark I/O complete on a buffer.
934 1.31 cgd *
935 1.31 cgd * If a callback has been requested, e.g. the pageout
936 1.31 cgd * daemon, do so. Otherwise, awaken waiting processes.
937 1.31 cgd *
938 1.31 cgd * [ Leffler, et al., says on p.247:
939 1.31 cgd * "This routine wakes up the blocked process, frees the buffer
940 1.31 cgd * for an asynchronous write, or, for a request by the pagedaemon
941 1.31 cgd * process, invokes a procedure specified in the buffer structure" ]
942 1.31 cgd *
943 1.31 cgd * In real life, the pagedaemon (or other system processes) wants
944 1.31 cgd * to do async stuff to, and doesn't want the buffer brelse()'d.
945 1.31 cgd * (for swap pager, that puts swap buffers on the free lists (!!!),
946 1.31 cgd * for the vn device, that puts malloc'd buffers on the free lists!)
947 1.31 cgd */
948 1.31 cgd void
949 1.31 cgd biodone(bp)
950 1.31 cgd struct buf *bp;
951 1.31 cgd {
952 1.60 fvdl int s = splbio();
953 1.60 fvdl
954 1.87 pk simple_lock(&bp->b_interlock);
955 1.31 cgd if (ISSET(bp->b_flags, B_DONE))
956 1.31 cgd panic("biodone already");
957 1.31 cgd SET(bp->b_flags, B_DONE); /* note that it's done */
958 1.31 cgd
959 1.59 fvdl if (LIST_FIRST(&bp->b_dep) != NULL && bioops.io_complete)
960 1.59 fvdl (*bioops.io_complete)(bp);
961 1.59 fvdl
962 1.31 cgd if (!ISSET(bp->b_flags, B_READ)) /* wake up reader */
963 1.31 cgd vwakeup(bp);
964 1.31 cgd
965 1.87 pk /*
966 1.87 pk * If necessary, call out. Unlock the buffer before calling
967 1.87 pk * iodone() as the buffer isn't valid any more when it return.
968 1.87 pk */
969 1.87 pk if (ISSET(bp->b_flags, B_CALL)) {
970 1.31 cgd CLR(bp->b_flags, B_CALL); /* but note callout done */
971 1.87 pk simple_unlock(&bp->b_interlock);
972 1.31 cgd (*bp->b_iodone)(bp);
973 1.59 fvdl } else {
974 1.87 pk if (ISSET(bp->b_flags, B_ASYNC)) { /* if async, release */
975 1.87 pk simple_unlock(&bp->b_interlock);
976 1.59 fvdl brelse(bp);
977 1.87 pk } else { /* or just wakeup the buffer */
978 1.59 fvdl CLR(bp->b_flags, B_WANTED);
979 1.59 fvdl wakeup(bp);
980 1.87 pk simple_unlock(&bp->b_interlock);
981 1.59 fvdl }
982 1.31 cgd }
983 1.60 fvdl
984 1.60 fvdl splx(s);
985 1.31 cgd }
986 1.31 cgd
987 1.31 cgd /*
988 1.31 cgd * Return a count of buffers on the "locked" queue.
989 1.31 cgd */
990 1.31 cgd int
991 1.31 cgd count_lock_queue()
992 1.31 cgd {
993 1.66 augustss struct buf *bp;
994 1.66 augustss int n = 0;
995 1.31 cgd
996 1.87 pk simple_lock(&bqueue_slock);
997 1.84 matt TAILQ_FOREACH(bp, &bufqueues[BQ_LOCKED], b_freelist)
998 1.31 cgd n++;
999 1.87 pk simple_unlock(&bqueue_slock);
1000 1.31 cgd return (n);
1001 1.31 cgd }
1002 1.31 cgd
1003 1.36 cgd #ifdef DEBUG
1004 1.31 cgd /*
1005 1.31 cgd * Print out statistics on the current allocation of the buffer pool.
1006 1.31 cgd * Can be enabled to print out on every ``sync'' by setting "syncprt"
1007 1.31 cgd * in vfs_syscalls.c using sysctl.
1008 1.31 cgd */
1009 1.31 cgd void
1010 1.31 cgd vfs_bufstats()
1011 1.31 cgd {
1012 1.31 cgd int s, i, j, count;
1013 1.66 augustss struct buf *bp;
1014 1.66 augustss struct bqueues *dp;
1015 1.72 simonb int counts[(MAXBSIZE / PAGE_SIZE) + 1];
1016 1.31 cgd static char *bname[BQUEUES] = { "LOCKED", "LRU", "AGE", "EMPTY" };
1017 1.71 thorpej
1018 1.31 cgd for (dp = bufqueues, i = 0; dp < &bufqueues[BQUEUES]; dp++, i++) {
1019 1.31 cgd count = 0;
1020 1.71 thorpej for (j = 0; j <= MAXBSIZE/PAGE_SIZE; j++)
1021 1.31 cgd counts[j] = 0;
1022 1.31 cgd s = splbio();
1023 1.84 matt TAILQ_FOREACH(bp, dp, b_freelist) {
1024 1.71 thorpej counts[bp->b_bufsize/PAGE_SIZE]++;
1025 1.31 cgd count++;
1026 1.31 cgd }
1027 1.31 cgd splx(s);
1028 1.48 christos printf("%s: total-%d", bname[i], count);
1029 1.71 thorpej for (j = 0; j <= MAXBSIZE/PAGE_SIZE; j++)
1030 1.31 cgd if (counts[j] != 0)
1031 1.71 thorpej printf(", %d-%d", j * PAGE_SIZE, counts[j]);
1032 1.48 christos printf("\n");
1033 1.31 cgd }
1034 1.31 cgd }
1035 1.36 cgd #endif /* DEBUG */
1036