FreeBSD/Linux Kernel Cross Reference
sys/kern/vfs_bio.c
1 /*-
2 * Copyright (c) 2004 Poul-Henning Kamp
3 * Copyright (c) 1994,1997 John S. Dyson
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28 /*
29 * this file contains a new buffer I/O scheme implementing a coherent
30 * VM object and buffer cache scheme. Pains have been taken to make
31 * sure that the performance degradation associated with schemes such
32 * as this is not realized.
33 *
34 * Author: John S. Dyson
35 * Significant help during the development and debugging phases
36 * had been provided by David Greenman, also of the FreeBSD core team.
37 *
38 * see man buf(9) for more info.
39 */
40
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD: releng/6.1/sys/kern/vfs_bio.c 158179 2006-04-30 16:44:43Z cvs2svn $");
43
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/bio.h>
47 #include <sys/conf.h>
48 #include <sys/buf.h>
49 #include <sys/devicestat.h>
50 #include <sys/eventhandler.h>
51 #include <sys/lock.h>
52 #include <sys/malloc.h>
53 #include <sys/mount.h>
54 #include <sys/mutex.h>
55 #include <sys/kernel.h>
56 #include <sys/kthread.h>
57 #include <sys/proc.h>
58 #include <sys/resourcevar.h>
59 #include <sys/sysctl.h>
60 #include <sys/vmmeter.h>
61 #include <sys/vnode.h>
62 #include <geom/geom.h>
63 #include <vm/vm.h>
64 #include <vm/vm_param.h>
65 #include <vm/vm_kern.h>
66 #include <vm/vm_pageout.h>
67 #include <vm/vm_page.h>
68 #include <vm/vm_object.h>
69 #include <vm/vm_extern.h>
70 #include <vm/vm_map.h>
71 #include "opt_directio.h"
72 #include "opt_swap.h"
73
74 static MALLOC_DEFINE(M_BIOBUF, "BIO buffer", "BIO buffer");
75
76 struct bio_ops bioops; /* I/O operation notification */
77
78 struct buf_ops buf_ops_bio = {
79 .bop_name = "buf_ops_bio",
80 .bop_write = bufwrite,
81 .bop_strategy = bufstrategy,
82 .bop_sync = bufsync,
83 };
84
85 /*
86 * XXX buf is global because kern_shutdown.c and ffs_checkoverlap has
87 * carnal knowledge of buffers. This knowledge should be moved to vfs_bio.c.
88 */
89 struct buf *buf; /* buffer header pool */
90
91 static struct proc *bufdaemonproc;
92
93 static int inmem(struct vnode *vp, daddr_t blkno);
94 static void vm_hold_free_pages(struct buf *bp, vm_offset_t from,
95 vm_offset_t to);
96 static void vm_hold_load_pages(struct buf *bp, vm_offset_t from,
97 vm_offset_t to);
98 static void vfs_page_set_valid(struct buf *bp, vm_ooffset_t off,
99 int pageno, vm_page_t m);
100 static void vfs_clean_pages(struct buf *bp);
101 static void vfs_setdirty(struct buf *bp);
102 static void vfs_vmio_release(struct buf *bp);
103 static int vfs_bio_clcheck(struct vnode *vp, int size,
104 daddr_t lblkno, daddr_t blkno);
105 static int flushbufqueues(int flushdeps);
106 static void buf_daemon(void);
107 static void bremfreel(struct buf *bp);
108
109 int vmiodirenable = TRUE;
110 SYSCTL_INT(_vfs, OID_AUTO, vmiodirenable, CTLFLAG_RW, &vmiodirenable, 0,
111 "Use the VM system for directory writes");
112 int runningbufspace;
113 SYSCTL_INT(_vfs, OID_AUTO, runningbufspace, CTLFLAG_RD, &runningbufspace, 0,
114 "Amount of presently outstanding async buffer io");
115 static int bufspace;
116 SYSCTL_INT(_vfs, OID_AUTO, bufspace, CTLFLAG_RD, &bufspace, 0,
117 "KVA memory used for bufs");
118 static int maxbufspace;
119 SYSCTL_INT(_vfs, OID_AUTO, maxbufspace, CTLFLAG_RD, &maxbufspace, 0,
120 "Maximum allowed value of bufspace (including buf_daemon)");
121 static int bufmallocspace;
122 SYSCTL_INT(_vfs, OID_AUTO, bufmallocspace, CTLFLAG_RD, &bufmallocspace, 0,
123 "Amount of malloced memory for buffers");
124 static int maxbufmallocspace;
125 SYSCTL_INT(_vfs, OID_AUTO, maxmallocbufspace, CTLFLAG_RW, &maxbufmallocspace, 0,
126 "Maximum amount of malloced memory for buffers");
127 static int lobufspace;
128 SYSCTL_INT(_vfs, OID_AUTO, lobufspace, CTLFLAG_RD, &lobufspace, 0,
129 "Minimum amount of buffers we want to have");
130 int hibufspace;
131 SYSCTL_INT(_vfs, OID_AUTO, hibufspace, CTLFLAG_RD, &hibufspace, 0,
132 "Maximum allowed value of bufspace (excluding buf_daemon)");
133 static int bufreusecnt;
134 SYSCTL_INT(_vfs, OID_AUTO, bufreusecnt, CTLFLAG_RW, &bufreusecnt, 0,
135 "Number of times we have reused a buffer");
136 static int buffreekvacnt;
137 SYSCTL_INT(_vfs, OID_AUTO, buffreekvacnt, CTLFLAG_RW, &buffreekvacnt, 0,
138 "Number of times we have freed the KVA space from some buffer");
139 static int bufdefragcnt;
140 SYSCTL_INT(_vfs, OID_AUTO, bufdefragcnt, CTLFLAG_RW, &bufdefragcnt, 0,
141 "Number of times we have had to repeat buffer allocation to defragment");
142 static int lorunningspace;
143 SYSCTL_INT(_vfs, OID_AUTO, lorunningspace, CTLFLAG_RW, &lorunningspace, 0,
144 "Minimum preferred space used for in-progress I/O");
145 static int hirunningspace;
146 SYSCTL_INT(_vfs, OID_AUTO, hirunningspace, CTLFLAG_RW, &hirunningspace, 0,
147 "Maximum amount of space to use for in-progress I/O");
148 static int dirtybufferflushes;
149 SYSCTL_INT(_vfs, OID_AUTO, dirtybufferflushes, CTLFLAG_RW, &dirtybufferflushes,
150 0, "Number of bdwrite to bawrite conversions to limit dirty buffers");
151 static int altbufferflushes;
152 SYSCTL_INT(_vfs, OID_AUTO, altbufferflushes, CTLFLAG_RW, &altbufferflushes,
153 0, "Number of fsync flushes to limit dirty buffers");
154 static int recursiveflushes;
155 SYSCTL_INT(_vfs, OID_AUTO, recursiveflushes, CTLFLAG_RW, &recursiveflushes,
156 0, "Number of flushes skipped due to being recursive");
157 static int numdirtybuffers;
158 SYSCTL_INT(_vfs, OID_AUTO, numdirtybuffers, CTLFLAG_RD, &numdirtybuffers, 0,
159 "Number of buffers that are dirty (has unwritten changes) at the moment");
160 static int lodirtybuffers;
161 SYSCTL_INT(_vfs, OID_AUTO, lodirtybuffers, CTLFLAG_RW, &lodirtybuffers, 0,
162 "How many buffers we want to have free before bufdaemon can sleep");
163 static int hidirtybuffers;
164 SYSCTL_INT(_vfs, OID_AUTO, hidirtybuffers, CTLFLAG_RW, &hidirtybuffers, 0,
165 "When the number of dirty buffers is considered severe");
166 static int dirtybufthresh;
167 SYSCTL_INT(_vfs, OID_AUTO, dirtybufthresh, CTLFLAG_RW, &dirtybufthresh,
168 0, "Number of bdwrite to bawrite conversions to clear dirty buffers");
169 static int numfreebuffers;
170 SYSCTL_INT(_vfs, OID_AUTO, numfreebuffers, CTLFLAG_RD, &numfreebuffers, 0,
171 "Number of free buffers");
172 static int lofreebuffers;
173 SYSCTL_INT(_vfs, OID_AUTO, lofreebuffers, CTLFLAG_RW, &lofreebuffers, 0,
174 "XXX Unused");
175 static int hifreebuffers;
176 SYSCTL_INT(_vfs, OID_AUTO, hifreebuffers, CTLFLAG_RW, &hifreebuffers, 0,
177 "XXX Complicatedly unused");
178 static int getnewbufcalls;
179 SYSCTL_INT(_vfs, OID_AUTO, getnewbufcalls, CTLFLAG_RW, &getnewbufcalls, 0,
180 "Number of calls to getnewbuf");
181 static int getnewbufrestarts;
182 SYSCTL_INT(_vfs, OID_AUTO, getnewbufrestarts, CTLFLAG_RW, &getnewbufrestarts, 0,
183 "Number of times getnewbuf has had to restart a buffer aquisition");
184
185 /*
186 * Wakeup point for bufdaemon, as well as indicator of whether it is already
187 * active. Set to 1 when the bufdaemon is already "on" the queue, 0 when it
188 * is idling.
189 */
190 static int bd_request;
191
192 /*
193 * This lock synchronizes access to bd_request.
194 */
195 static struct mtx bdlock;
196
197 /*
198 * bogus page -- for I/O to/from partially complete buffers
199 * this is a temporary solution to the problem, but it is not
200 * really that bad. it would be better to split the buffer
201 * for input in the case of buffers partially already in memory,
202 * but the code is intricate enough already.
203 */
204 vm_page_t bogus_page;
205
206 /*
207 * Synchronization (sleep/wakeup) variable for active buffer space requests.
208 * Set when wait starts, cleared prior to wakeup().
209 * Used in runningbufwakeup() and waitrunningbufspace().
210 */
211 static int runningbufreq;
212
213 /*
214 * This lock protects the runningbufreq and synchronizes runningbufwakeup and
215 * waitrunningbufspace().
216 */
217 static struct mtx rbreqlock;
218
219 /*
220 * Synchronization (sleep/wakeup) variable for buffer requests.
221 * Can contain the VFS_BIO_NEED flags defined below; setting/clearing is done
222 * by and/or.
223 * Used in numdirtywakeup(), bufspacewakeup(), bufcountwakeup(), bwillwrite(),
224 * getnewbuf(), and getblk().
225 */
226 static int needsbuffer;
227
228 /*
229 * Lock that protects needsbuffer and the sleeps/wakeups surrounding it.
230 */
231 static struct mtx nblock;
232
233 /*
234 * Lock that protects against bwait()/bdone()/B_DONE races.
235 */
236
237 static struct mtx bdonelock;
238
239 /*
240 * Definitions for the buffer free lists.
241 */
242 #define BUFFER_QUEUES 5 /* number of free buffer queues */
243
244 #define QUEUE_NONE 0 /* on no queue */
245 #define QUEUE_CLEAN 1 /* non-B_DELWRI buffers */
246 #define QUEUE_DIRTY 2 /* B_DELWRI buffers */
247 #define QUEUE_EMPTYKVA 3 /* empty buffer headers w/KVA assignment */
248 #define QUEUE_EMPTY 4 /* empty buffer headers */
249
250 /* Queues for free buffers with various properties */
251 static TAILQ_HEAD(bqueues, buf) bufqueues[BUFFER_QUEUES] = { { 0 } };
252
253 /* Lock for the bufqueues */
254 static struct mtx bqlock;
255
256 /*
257 * Single global constant for BUF_WMESG, to avoid getting multiple references.
258 * buf_wmesg is referred from macros.
259 */
260 const char *buf_wmesg = BUF_WMESG;
261
262 #define VFS_BIO_NEED_ANY 0x01 /* any freeable buffer */
263 #define VFS_BIO_NEED_DIRTYFLUSH 0x02 /* waiting for dirty buffer flush */
264 #define VFS_BIO_NEED_FREE 0x04 /* wait for free bufs, hi hysteresis */
265 #define VFS_BIO_NEED_BUFSPACE 0x08 /* wait for buf space, lo hysteresis */
266
267 #ifdef DIRECTIO
268 extern void ffs_rawread_setup(void);
269 #endif /* DIRECTIO */
270 /*
271 * numdirtywakeup:
272 *
273 * If someone is blocked due to there being too many dirty buffers,
274 * and numdirtybuffers is now reasonable, wake them up.
275 */
276
277 static __inline void
278 numdirtywakeup(int level)
279 {
280
281 if (numdirtybuffers <= level) {
282 mtx_lock(&nblock);
283 if (needsbuffer & VFS_BIO_NEED_DIRTYFLUSH) {
284 needsbuffer &= ~VFS_BIO_NEED_DIRTYFLUSH;
285 wakeup(&needsbuffer);
286 }
287 mtx_unlock(&nblock);
288 }
289 }
290
291 /*
292 * bufspacewakeup:
293 *
294 * Called when buffer space is potentially available for recovery.
295 * getnewbuf() will block on this flag when it is unable to free
296 * sufficient buffer space. Buffer space becomes recoverable when
297 * bp's get placed back in the queues.
298 */
299
300 static __inline void
301 bufspacewakeup(void)
302 {
303
304 /*
305 * If someone is waiting for BUF space, wake them up. Even
306 * though we haven't freed the kva space yet, the waiting
307 * process will be able to now.
308 */
309 mtx_lock(&nblock);
310 if (needsbuffer & VFS_BIO_NEED_BUFSPACE) {
311 needsbuffer &= ~VFS_BIO_NEED_BUFSPACE;
312 wakeup(&needsbuffer);
313 }
314 mtx_unlock(&nblock);
315 }
316
317 /*
318 * runningbufwakeup() - in-progress I/O accounting.
319 *
320 */
321 void
322 runningbufwakeup(struct buf *bp)
323 {
324
325 if (bp->b_runningbufspace) {
326 atomic_subtract_int(&runningbufspace, bp->b_runningbufspace);
327 bp->b_runningbufspace = 0;
328 mtx_lock(&rbreqlock);
329 if (runningbufreq && runningbufspace <= lorunningspace) {
330 runningbufreq = 0;
331 wakeup(&runningbufreq);
332 }
333 mtx_unlock(&rbreqlock);
334 }
335 }
336
337 /*
338 * bufcountwakeup:
339 *
340 * Called when a buffer has been added to one of the free queues to
341 * account for the buffer and to wakeup anyone waiting for free buffers.
342 * This typically occurs when large amounts of metadata are being handled
343 * by the buffer cache ( else buffer space runs out first, usually ).
344 */
345
346 static __inline void
347 bufcountwakeup(void)
348 {
349
350 atomic_add_int(&numfreebuffers, 1);
351 mtx_lock(&nblock);
352 if (needsbuffer) {
353 needsbuffer &= ~VFS_BIO_NEED_ANY;
354 if (numfreebuffers >= hifreebuffers)
355 needsbuffer &= ~VFS_BIO_NEED_FREE;
356 wakeup(&needsbuffer);
357 }
358 mtx_unlock(&nblock);
359 }
360
361 /*
362 * waitrunningbufspace()
363 *
364 * runningbufspace is a measure of the amount of I/O currently
365 * running. This routine is used in async-write situations to
366 * prevent creating huge backups of pending writes to a device.
367 * Only asynchronous writes are governed by this function.
368 *
369 * Reads will adjust runningbufspace, but will not block based on it.
370 * The read load has a side effect of reducing the allowed write load.
371 *
372 * This does NOT turn an async write into a sync write. It waits
373 * for earlier writes to complete and generally returns before the
374 * caller's write has reached the device.
375 */
376 void
377 waitrunningbufspace(void)
378 {
379
380 mtx_lock(&rbreqlock);
381 while (runningbufspace > hirunningspace) {
382 ++runningbufreq;
383 msleep(&runningbufreq, &rbreqlock, PVM, "wdrain", 0);
384 }
385 mtx_unlock(&rbreqlock);
386 }
387
388
389 /*
390 * vfs_buf_test_cache:
391 *
392 * Called when a buffer is extended. This function clears the B_CACHE
393 * bit if the newly extended portion of the buffer does not contain
394 * valid data.
395 */
396 static __inline
397 void
398 vfs_buf_test_cache(struct buf *bp,
399 vm_ooffset_t foff, vm_offset_t off, vm_offset_t size,
400 vm_page_t m)
401 {
402
403 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
404 if (bp->b_flags & B_CACHE) {
405 int base = (foff + off) & PAGE_MASK;
406 if (vm_page_is_valid(m, base, size) == 0)
407 bp->b_flags &= ~B_CACHE;
408 }
409 }
410
411 /* Wake up the buffer deamon if necessary */
412 static __inline
413 void
414 bd_wakeup(int dirtybuflevel)
415 {
416
417 mtx_lock(&bdlock);
418 if (bd_request == 0 && numdirtybuffers >= dirtybuflevel) {
419 bd_request = 1;
420 wakeup(&bd_request);
421 }
422 mtx_unlock(&bdlock);
423 }
424
425 /*
426 * bd_speedup - speedup the buffer cache flushing code
427 */
428
429 static __inline
430 void
431 bd_speedup(void)
432 {
433
434 bd_wakeup(1);
435 }
436
437 /*
438 * Calculating buffer cache scaling values and reserve space for buffer
439 * headers. This is called during low level kernel initialization and
440 * may be called more then once. We CANNOT write to the memory area
441 * being reserved at this time.
442 */
443 caddr_t
444 kern_vfs_bio_buffer_alloc(caddr_t v, long physmem_est)
445 {
446
447 /*
448 * physmem_est is in pages. Convert it to kilobytes (assumes
449 * PAGE_SIZE is >= 1K)
450 */
451 physmem_est = physmem_est * (PAGE_SIZE / 1024);
452
453 /*
454 * The nominal buffer size (and minimum KVA allocation) is BKVASIZE.
455 * For the first 64MB of ram nominally allocate sufficient buffers to
456 * cover 1/4 of our ram. Beyond the first 64MB allocate additional
457 * buffers to cover 1/20 of our ram over 64MB. When auto-sizing
458 * the buffer cache we limit the eventual kva reservation to
459 * maxbcache bytes.
460 *
461 * factor represents the 1/4 x ram conversion.
462 */
463 if (nbuf == 0) {
464 int factor = 4 * BKVASIZE / 1024;
465
466 nbuf = 50;
467 if (physmem_est > 4096)
468 nbuf += min((physmem_est - 4096) / factor,
469 65536 / factor);
470 if (physmem_est > 65536)
471 nbuf += (physmem_est - 65536) * 2 / (factor * 5);
472
473 if (maxbcache && nbuf > maxbcache / BKVASIZE)
474 nbuf = maxbcache / BKVASIZE;
475 }
476
477 #if 0
478 /*
479 * Do not allow the buffer_map to be more then 1/2 the size of the
480 * kernel_map.
481 */
482 if (nbuf > (kernel_map->max_offset - kernel_map->min_offset) /
483 (BKVASIZE * 2)) {
484 nbuf = (kernel_map->max_offset - kernel_map->min_offset) /
485 (BKVASIZE * 2);
486 printf("Warning: nbufs capped at %d\n", nbuf);
487 }
488 #endif
489
490 /*
491 * swbufs are used as temporary holders for I/O, such as paging I/O.
492 * We have no less then 16 and no more then 256.
493 */
494 nswbuf = max(min(nbuf/4, 256), 16);
495 #ifdef NSWBUF_MIN
496 if (nswbuf < NSWBUF_MIN)
497 nswbuf = NSWBUF_MIN;
498 #endif
499 #ifdef DIRECTIO
500 ffs_rawread_setup();
501 #endif
502
503 /*
504 * Reserve space for the buffer cache buffers
505 */
506 swbuf = (void *)v;
507 v = (caddr_t)(swbuf + nswbuf);
508 buf = (void *)v;
509 v = (caddr_t)(buf + nbuf);
510
511 return(v);
512 }
513
514 /* Initialize the buffer subsystem. Called before use of any buffers. */
515 void
516 bufinit(void)
517 {
518 struct buf *bp;
519 int i;
520
521 mtx_init(&bqlock, "buf queue lock", NULL, MTX_DEF);
522 mtx_init(&rbreqlock, "runningbufspace lock", NULL, MTX_DEF);
523 mtx_init(&nblock, "needsbuffer lock", NULL, MTX_DEF);
524 mtx_init(&bdlock, "buffer daemon lock", NULL, MTX_DEF);
525 mtx_init(&bdonelock, "bdone lock", NULL, MTX_DEF);
526
527 /* next, make a null set of free lists */
528 for (i = 0; i < BUFFER_QUEUES; i++)
529 TAILQ_INIT(&bufqueues[i]);
530
531 /* finally, initialize each buffer header and stick on empty q */
532 for (i = 0; i < nbuf; i++) {
533 bp = &buf[i];
534 bzero(bp, sizeof *bp);
535 bp->b_flags = B_INVAL; /* we're just an empty header */
536 bp->b_rcred = NOCRED;
537 bp->b_wcred = NOCRED;
538 bp->b_qindex = QUEUE_EMPTY;
539 bp->b_vflags = 0;
540 bp->b_xflags = 0;
541 LIST_INIT(&bp->b_dep);
542 BUF_LOCKINIT(bp);
543 TAILQ_INSERT_TAIL(&bufqueues[QUEUE_EMPTY], bp, b_freelist);
544 }
545
546 /*
547 * maxbufspace is the absolute maximum amount of buffer space we are
548 * allowed to reserve in KVM and in real terms. The absolute maximum
549 * is nominally used by buf_daemon. hibufspace is the nominal maximum
550 * used by most other processes. The differential is required to
551 * ensure that buf_daemon is able to run when other processes might
552 * be blocked waiting for buffer space.
553 *
554 * maxbufspace is based on BKVASIZE. Allocating buffers larger then
555 * this may result in KVM fragmentation which is not handled optimally
556 * by the system.
557 */
558 maxbufspace = nbuf * BKVASIZE;
559 hibufspace = imax(3 * maxbufspace / 4, maxbufspace - MAXBSIZE * 10);
560 lobufspace = hibufspace - MAXBSIZE;
561
562 lorunningspace = 512 * 1024;
563 hirunningspace = 1024 * 1024;
564
565 /*
566 * Limit the amount of malloc memory since it is wired permanently into
567 * the kernel space. Even though this is accounted for in the buffer
568 * allocation, we don't want the malloced region to grow uncontrolled.
569 * The malloc scheme improves memory utilization significantly on average
570 * (small) directories.
571 */
572 maxbufmallocspace = hibufspace / 20;
573
574 /*
575 * Reduce the chance of a deadlock occuring by limiting the number
576 * of delayed-write dirty buffers we allow to stack up.
577 */
578 hidirtybuffers = nbuf / 4 + 20;
579 dirtybufthresh = hidirtybuffers * 9 / 10;
580 numdirtybuffers = 0;
581 /*
582 * To support extreme low-memory systems, make sure hidirtybuffers cannot
583 * eat up all available buffer space. This occurs when our minimum cannot
584 * be met. We try to size hidirtybuffers to 3/4 our buffer space assuming
585 * BKVASIZE'd (8K) buffers.
586 */
587 while (hidirtybuffers * BKVASIZE > 3 * hibufspace / 4) {
588 hidirtybuffers >>= 1;
589 }
590 lodirtybuffers = hidirtybuffers / 2;
591
592 /*
593 * Try to keep the number of free buffers in the specified range,
594 * and give special processes (e.g. like buf_daemon) access to an
595 * emergency reserve.
596 */
597 lofreebuffers = nbuf / 18 + 5;
598 hifreebuffers = 2 * lofreebuffers;
599 numfreebuffers = nbuf;
600
601 /*
602 * Maximum number of async ops initiated per buf_daemon loop. This is
603 * somewhat of a hack at the moment, we really need to limit ourselves
604 * based on the number of bytes of I/O in-transit that were initiated
605 * from buf_daemon.
606 */
607
608 bogus_page = vm_page_alloc(NULL, 0, VM_ALLOC_NOOBJ |
609 VM_ALLOC_NORMAL | VM_ALLOC_WIRED);
610 }
611
612 /*
613 * bfreekva() - free the kva allocation for a buffer.
614 *
615 * Since this call frees up buffer space, we call bufspacewakeup().
616 */
617 static void
618 bfreekva(struct buf *bp)
619 {
620
621 if (bp->b_kvasize) {
622 atomic_add_int(&buffreekvacnt, 1);
623 atomic_subtract_int(&bufspace, bp->b_kvasize);
624 vm_map_lock(buffer_map);
625 vm_map_delete(buffer_map,
626 (vm_offset_t) bp->b_kvabase,
627 (vm_offset_t) bp->b_kvabase + bp->b_kvasize
628 );
629 vm_map_unlock(buffer_map);
630 bp->b_kvasize = 0;
631 bufspacewakeup();
632 }
633 }
634
635 /*
636 * bremfree:
637 *
638 * Mark the buffer for removal from the appropriate free list in brelse.
639 *
640 */
641 void
642 bremfree(struct buf *bp)
643 {
644
645 CTR3(KTR_BUF, "bremfree(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
646 KASSERT(BUF_REFCNT(bp), ("bremfree: buf must be locked."));
647 KASSERT((bp->b_flags & B_REMFREE) == 0,
648 ("bremfree: buffer %p already marked for delayed removal.", bp));
649 KASSERT(bp->b_qindex != QUEUE_NONE,
650 ("bremfree: buffer %p not on a queue.", bp));
651
652 bp->b_flags |= B_REMFREE;
653 /* Fixup numfreebuffers count. */
654 if ((bp->b_flags & B_INVAL) || (bp->b_flags & B_DELWRI) == 0)
655 atomic_subtract_int(&numfreebuffers, 1);
656 }
657
658 /*
659 * bremfreef:
660 *
661 * Force an immediate removal from a free list. Used only in nfs when
662 * it abuses the b_freelist pointer.
663 */
664 void
665 bremfreef(struct buf *bp)
666 {
667 mtx_lock(&bqlock);
668 bremfreel(bp);
669 mtx_unlock(&bqlock);
670 }
671
672 /*
673 * bremfreel:
674 *
675 * Removes a buffer from the free list, must be called with the
676 * bqlock held.
677 */
678 static void
679 bremfreel(struct buf *bp)
680 {
681 CTR3(KTR_BUF, "bremfreel(%p) vp %p flags %X",
682 bp, bp->b_vp, bp->b_flags);
683 KASSERT(BUF_REFCNT(bp), ("bremfreel: buffer %p not locked.", bp));
684 KASSERT(bp->b_qindex != QUEUE_NONE,
685 ("bremfreel: buffer %p not on a queue.", bp));
686 mtx_assert(&bqlock, MA_OWNED);
687
688 TAILQ_REMOVE(&bufqueues[bp->b_qindex], bp, b_freelist);
689 bp->b_qindex = QUEUE_NONE;
690 /*
691 * If this was a delayed bremfree() we only need to remove the buffer
692 * from the queue and return the stats are already done.
693 */
694 if (bp->b_flags & B_REMFREE) {
695 bp->b_flags &= ~B_REMFREE;
696 return;
697 }
698 /*
699 * Fixup numfreebuffers count. If the buffer is invalid or not
700 * delayed-write, the buffer was free and we must decrement
701 * numfreebuffers.
702 */
703 if ((bp->b_flags & B_INVAL) || (bp->b_flags & B_DELWRI) == 0)
704 atomic_subtract_int(&numfreebuffers, 1);
705 }
706
707
708 /*
709 * Get a buffer with the specified data. Look in the cache first. We
710 * must clear BIO_ERROR and B_INVAL prior to initiating I/O. If B_CACHE
711 * is set, the buffer is valid and we do not have to do anything ( see
712 * getblk() ). This is really just a special case of breadn().
713 */
714 int
715 bread(struct vnode * vp, daddr_t blkno, int size, struct ucred * cred,
716 struct buf **bpp)
717 {
718
719 return (breadn(vp, blkno, size, 0, 0, 0, cred, bpp));
720 }
721
722 /*
723 * Operates like bread, but also starts asynchronous I/O on
724 * read-ahead blocks. We must clear BIO_ERROR and B_INVAL prior
725 * to initiating I/O . If B_CACHE is set, the buffer is valid
726 * and we do not have to do anything.
727 */
728 int
729 breadn(struct vnode * vp, daddr_t blkno, int size,
730 daddr_t * rablkno, int *rabsize,
731 int cnt, struct ucred * cred, struct buf **bpp)
732 {
733 struct buf *bp, *rabp;
734 int i;
735 int rv = 0, readwait = 0;
736
737 CTR3(KTR_BUF, "breadn(%p, %jd, %d)", vp, blkno, size);
738 *bpp = bp = getblk(vp, blkno, size, 0, 0, 0);
739
740 /* if not found in cache, do some I/O */
741 if ((bp->b_flags & B_CACHE) == 0) {
742 if (curthread != PCPU_GET(idlethread))
743 curthread->td_proc->p_stats->p_ru.ru_inblock++;
744 bp->b_iocmd = BIO_READ;
745 bp->b_flags &= ~B_INVAL;
746 bp->b_ioflags &= ~BIO_ERROR;
747 if (bp->b_rcred == NOCRED && cred != NOCRED)
748 bp->b_rcred = crhold(cred);
749 vfs_busy_pages(bp, 0);
750 bp->b_iooffset = dbtob(bp->b_blkno);
751 bstrategy(bp);
752 ++readwait;
753 }
754
755 for (i = 0; i < cnt; i++, rablkno++, rabsize++) {
756 if (inmem(vp, *rablkno))
757 continue;
758 rabp = getblk(vp, *rablkno, *rabsize, 0, 0, 0);
759
760 if ((rabp->b_flags & B_CACHE) == 0) {
761 if (curthread != PCPU_GET(idlethread))
762 curthread->td_proc->p_stats->p_ru.ru_inblock++;
763 rabp->b_flags |= B_ASYNC;
764 rabp->b_flags &= ~B_INVAL;
765 rabp->b_ioflags &= ~BIO_ERROR;
766 rabp->b_iocmd = BIO_READ;
767 if (rabp->b_rcred == NOCRED && cred != NOCRED)
768 rabp->b_rcred = crhold(cred);
769 vfs_busy_pages(rabp, 0);
770 BUF_KERNPROC(rabp);
771 rabp->b_iooffset = dbtob(rabp->b_blkno);
772 bstrategy(rabp);
773 } else {
774 brelse(rabp);
775 }
776 }
777
778 if (readwait) {
779 rv = bufwait(bp);
780 }
781 return (rv);
782 }
783
784 /*
785 * Write, release buffer on completion. (Done by iodone
786 * if async). Do not bother writing anything if the buffer
787 * is invalid.
788 *
789 * Note that we set B_CACHE here, indicating that buffer is
790 * fully valid and thus cacheable. This is true even of NFS
791 * now so we set it generally. This could be set either here
792 * or in biodone() since the I/O is synchronous. We put it
793 * here.
794 */
795 int
796 bufwrite(struct buf *bp)
797 {
798 int oldflags;
799
800 CTR3(KTR_BUF, "bufwrite(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
801 if (bp->b_flags & B_INVAL) {
802 brelse(bp);
803 return (0);
804 }
805
806 oldflags = bp->b_flags;
807
808 if (BUF_REFCNT(bp) == 0)
809 panic("bufwrite: buffer is not busy???");
810 KASSERT(!(bp->b_vflags & BV_BKGRDINPROG),
811 ("FFS background buffer should not get here %p", bp));
812
813 /* Mark the buffer clean */
814 bundirty(bp);
815
816 bp->b_flags &= ~B_DONE;
817 bp->b_ioflags &= ~BIO_ERROR;
818 bp->b_flags |= B_CACHE;
819 bp->b_iocmd = BIO_WRITE;
820
821 bufobj_wref(bp->b_bufobj);
822 vfs_busy_pages(bp, 1);
823
824 /*
825 * Normal bwrites pipeline writes
826 */
827 bp->b_runningbufspace = bp->b_bufsize;
828 atomic_add_int(&runningbufspace, bp->b_runningbufspace);
829
830 if (curthread != PCPU_GET(idlethread))
831 curthread->td_proc->p_stats->p_ru.ru_oublock++;
832 if (oldflags & B_ASYNC)
833 BUF_KERNPROC(bp);
834 bp->b_iooffset = dbtob(bp->b_blkno);
835 bstrategy(bp);
836
837 if ((oldflags & B_ASYNC) == 0) {
838 int rtval = bufwait(bp);
839 brelse(bp);
840 return (rtval);
841 } else {
842 /*
843 * don't allow the async write to saturate the I/O
844 * system. We will not deadlock here because
845 * we are blocking waiting for I/O that is already in-progress
846 * to complete. We do not block here if it is the update
847 * or syncer daemon trying to clean up as that can lead
848 * to deadlock.
849 */
850 if ((curthread->td_pflags & TDP_NORUNNINGBUF) == 0)
851 waitrunningbufspace();
852 }
853
854 return (0);
855 }
856
857 /*
858 * Delayed write. (Buffer is marked dirty). Do not bother writing
859 * anything if the buffer is marked invalid.
860 *
861 * Note that since the buffer must be completely valid, we can safely
862 * set B_CACHE. In fact, we have to set B_CACHE here rather then in
863 * biodone() in order to prevent getblk from writing the buffer
864 * out synchronously.
865 */
866 void
867 bdwrite(struct buf *bp)
868 {
869 struct thread *td = curthread;
870 struct vnode *vp;
871 struct buf *nbp;
872 struct bufobj *bo;
873
874 CTR3(KTR_BUF, "bdwrite(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
875 KASSERT(bp->b_bufobj != NULL, ("No b_bufobj %p", bp));
876 KASSERT(BUF_REFCNT(bp) != 0, ("bdwrite: buffer is not busy"));
877
878 if (bp->b_flags & B_INVAL) {
879 brelse(bp);
880 return;
881 }
882
883 /*
884 * If we have too many dirty buffers, don't create any more.
885 * If we are wildly over our limit, then force a complete
886 * cleanup. Otherwise, just keep the situation from getting
887 * out of control. Note that we have to avoid a recursive
888 * disaster and not try to clean up after our own cleanup!
889 */
890 vp = bp->b_vp;
891 bo = bp->b_bufobj;
892 if ((td->td_pflags & TDP_COWINPROGRESS) == 0) {
893 BO_LOCK(bo);
894 if (bo->bo_dirty.bv_cnt > dirtybufthresh + 10) {
895 BO_UNLOCK(bo);
896 (void) VOP_FSYNC(vp, MNT_NOWAIT, td);
897 altbufferflushes++;
898 } else if (bo->bo_dirty.bv_cnt > dirtybufthresh) {
899 /*
900 * Try to find a buffer to flush.
901 */
902 TAILQ_FOREACH(nbp, &bo->bo_dirty.bv_hd, b_bobufs) {
903 if ((nbp->b_vflags & BV_BKGRDINPROG) ||
904 BUF_LOCK(nbp,
905 LK_EXCLUSIVE | LK_NOWAIT, NULL))
906 continue;
907 if (bp == nbp)
908 panic("bdwrite: found ourselves");
909 BO_UNLOCK(bo);
910 /* Don't countdeps with the bo lock held. */
911 if (buf_countdeps(nbp, 0)) {
912 BO_LOCK(bo);
913 BUF_UNLOCK(nbp);
914 continue;
915 }
916 if (nbp->b_flags & B_CLUSTEROK) {
917 vfs_bio_awrite(nbp);
918 } else {
919 bremfree(nbp);
920 bawrite(nbp);
921 }
922 dirtybufferflushes++;
923 break;
924 }
925 if (nbp == NULL)
926 BO_UNLOCK(bo);
927 } else
928 BO_UNLOCK(bo);
929 } else
930 recursiveflushes++;
931
932 bdirty(bp);
933 /*
934 * Set B_CACHE, indicating that the buffer is fully valid. This is
935 * true even of NFS now.
936 */
937 bp->b_flags |= B_CACHE;
938
939 /*
940 * This bmap keeps the system from needing to do the bmap later,
941 * perhaps when the system is attempting to do a sync. Since it
942 * is likely that the indirect block -- or whatever other datastructure
943 * that the filesystem needs is still in memory now, it is a good
944 * thing to do this. Note also, that if the pageout daemon is
945 * requesting a sync -- there might not be enough memory to do
946 * the bmap then... So, this is important to do.
947 */
948 if (vp->v_type != VCHR && bp->b_lblkno == bp->b_blkno) {
949 VOP_BMAP(vp, bp->b_lblkno, NULL, &bp->b_blkno, NULL, NULL);
950 }
951
952 /*
953 * Set the *dirty* buffer range based upon the VM system dirty pages.
954 */
955 vfs_setdirty(bp);
956
957 /*
958 * We need to do this here to satisfy the vnode_pager and the
959 * pageout daemon, so that it thinks that the pages have been
960 * "cleaned". Note that since the pages are in a delayed write
961 * buffer -- the VFS layer "will" see that the pages get written
962 * out on the next sync, or perhaps the cluster will be completed.
963 */
964 vfs_clean_pages(bp);
965 bqrelse(bp);
966
967 /*
968 * Wakeup the buffer flushing daemon if we have a lot of dirty
969 * buffers (midpoint between our recovery point and our stall
970 * point).
971 */
972 bd_wakeup((lodirtybuffers + hidirtybuffers) / 2);
973
974 /*
975 * note: we cannot initiate I/O from a bdwrite even if we wanted to,
976 * due to the softdep code.
977 */
978 }
979
980 /*
981 * bdirty:
982 *
983 * Turn buffer into delayed write request. We must clear BIO_READ and
984 * B_RELBUF, and we must set B_DELWRI. We reassign the buffer to
985 * itself to properly update it in the dirty/clean lists. We mark it
986 * B_DONE to ensure that any asynchronization of the buffer properly
987 * clears B_DONE ( else a panic will occur later ).
988 *
989 * bdirty() is kinda like bdwrite() - we have to clear B_INVAL which
990 * might have been set pre-getblk(). Unlike bwrite/bdwrite, bdirty()
991 * should only be called if the buffer is known-good.
992 *
993 * Since the buffer is not on a queue, we do not update the numfreebuffers
994 * count.
995 *
996 * The buffer must be on QUEUE_NONE.
997 */
998 void
999 bdirty(struct buf *bp)
1000 {
1001
1002 CTR3(KTR_BUF, "bdirty(%p) vp %p flags %X",
1003 bp, bp->b_vp, bp->b_flags);
1004 KASSERT(BUF_REFCNT(bp) == 1, ("bdirty: bp %p not locked",bp));
1005 KASSERT(bp->b_bufobj != NULL, ("No b_bufobj %p", bp));
1006 KASSERT(bp->b_flags & B_REMFREE || bp->b_qindex == QUEUE_NONE,
1007 ("bdirty: buffer %p still on queue %d", bp, bp->b_qindex));
1008 bp->b_flags &= ~(B_RELBUF);
1009 bp->b_iocmd = BIO_WRITE;
1010
1011 if ((bp->b_flags & B_DELWRI) == 0) {
1012 bp->b_flags |= /* XXX B_DONE | */ B_DELWRI;
1013 reassignbuf(bp);
1014 atomic_add_int(&numdirtybuffers, 1);
1015 bd_wakeup((lodirtybuffers + hidirtybuffers) / 2);
1016 }
1017 }
1018
1019 /*
1020 * bundirty:
1021 *
1022 * Clear B_DELWRI for buffer.
1023 *
1024 * Since the buffer is not on a queue, we do not update the numfreebuffers
1025 * count.
1026 *
1027 * The buffer must be on QUEUE_NONE.
1028 */
1029
1030 void
1031 bundirty(struct buf *bp)
1032 {
1033
1034 CTR3(KTR_BUF, "bundirty(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
1035 KASSERT(bp->b_bufobj != NULL, ("No b_bufobj %p", bp));
1036 KASSERT(bp->b_flags & B_REMFREE || bp->b_qindex == QUEUE_NONE,
1037 ("bundirty: buffer %p still on queue %d", bp, bp->b_qindex));
1038 KASSERT(BUF_REFCNT(bp) == 1, ("bundirty: bp %p not locked",bp));
1039
1040 if (bp->b_flags & B_DELWRI) {
1041 bp->b_flags &= ~B_DELWRI;
1042 reassignbuf(bp);
1043 atomic_subtract_int(&numdirtybuffers, 1);
1044 numdirtywakeup(lodirtybuffers);
1045 }
1046 /*
1047 * Since it is now being written, we can clear its deferred write flag.
1048 */
1049 bp->b_flags &= ~B_DEFERRED;
1050 }
1051
1052 /*
1053 * bawrite:
1054 *
1055 * Asynchronous write. Start output on a buffer, but do not wait for
1056 * it to complete. The buffer is released when the output completes.
1057 *
1058 * bwrite() ( or the VOP routine anyway ) is responsible for handling
1059 * B_INVAL buffers. Not us.
1060 */
1061 void
1062 bawrite(struct buf *bp)
1063 {
1064
1065 bp->b_flags |= B_ASYNC;
1066 (void) bwrite(bp);
1067 }
1068
1069 /*
1070 * bwillwrite:
1071 *
1072 * Called prior to the locking of any vnodes when we are expecting to
1073 * write. We do not want to starve the buffer cache with too many
1074 * dirty buffers so we block here. By blocking prior to the locking
1075 * of any vnodes we attempt to avoid the situation where a locked vnode
1076 * prevents the various system daemons from flushing related buffers.
1077 */
1078
1079 void
1080 bwillwrite(void)
1081 {
1082
1083 if (numdirtybuffers >= hidirtybuffers) {
1084 mtx_lock(&nblock);
1085 while (numdirtybuffers >= hidirtybuffers) {
1086 bd_wakeup(1);
1087 needsbuffer |= VFS_BIO_NEED_DIRTYFLUSH;
1088 msleep(&needsbuffer, &nblock,
1089 (PRIBIO + 4), "flswai", 0);
1090 }
1091 mtx_unlock(&nblock);
1092 }
1093 }
1094
1095 /*
1096 * Return true if we have too many dirty buffers.
1097 */
1098 int
1099 buf_dirty_count_severe(void)
1100 {
1101
1102 return(numdirtybuffers >= hidirtybuffers);
1103 }
1104
1105 /*
1106 * brelse:
1107 *
1108 * Release a busy buffer and, if requested, free its resources. The
1109 * buffer will be stashed in the appropriate bufqueue[] allowing it
1110 * to be accessed later as a cache entity or reused for other purposes.
1111 */
1112 void
1113 brelse(struct buf *bp)
1114 {
1115 CTR3(KTR_BUF, "brelse(%p) vp %p flags %X",
1116 bp, bp->b_vp, bp->b_flags);
1117 KASSERT(!(bp->b_flags & (B_CLUSTER|B_PAGING)),
1118 ("brelse: inappropriate B_PAGING or B_CLUSTER bp %p", bp));
1119
1120 if (bp->b_iocmd == BIO_WRITE &&
1121 (bp->b_ioflags & BIO_ERROR) &&
1122 !(bp->b_flags & B_INVAL)) {
1123 /*
1124 * Failed write, redirty. Must clear BIO_ERROR to prevent
1125 * pages from being scrapped. If B_INVAL is set then
1126 * this case is not run and the next case is run to
1127 * destroy the buffer. B_INVAL can occur if the buffer
1128 * is outside the range supported by the underlying device.
1129 */
1130 bp->b_ioflags &= ~BIO_ERROR;
1131 bdirty(bp);
1132 } else if ((bp->b_flags & (B_NOCACHE | B_INVAL)) ||
1133 (bp->b_ioflags & BIO_ERROR) || (bp->b_bufsize <= 0)) {
1134 /*
1135 * Either a failed I/O or we were asked to free or not
1136 * cache the buffer.
1137 */
1138 bp->b_flags |= B_INVAL;
1139 if (LIST_FIRST(&bp->b_dep) != NULL)
1140 buf_deallocate(bp);
1141 if (bp->b_flags & B_DELWRI) {
1142 atomic_subtract_int(&numdirtybuffers, 1);
1143 numdirtywakeup(lodirtybuffers);
1144 }
1145 bp->b_flags &= ~(B_DELWRI | B_CACHE);
1146 if ((bp->b_flags & B_VMIO) == 0) {
1147 if (bp->b_bufsize)
1148 allocbuf(bp, 0);
1149 if (bp->b_vp)
1150 brelvp(bp);
1151 }
1152 }
1153
1154 /*
1155 * We must clear B_RELBUF if B_DELWRI is set. If vfs_vmio_release()
1156 * is called with B_DELWRI set, the underlying pages may wind up
1157 * getting freed causing a previous write (bdwrite()) to get 'lost'
1158 * because pages associated with a B_DELWRI bp are marked clean.
1159 *
1160 * We still allow the B_INVAL case to call vfs_vmio_release(), even
1161 * if B_DELWRI is set.
1162 *
1163 * If B_DELWRI is not set we may have to set B_RELBUF if we are low
1164 * on pages to return pages to the VM page queues.
1165 */
1166 if (bp->b_flags & B_DELWRI)
1167 bp->b_flags &= ~B_RELBUF;
1168 else if (vm_page_count_severe()) {
1169 /*
1170 * XXX This lock may not be necessary since BKGRDINPROG
1171 * cannot be set while we hold the buf lock, it can only be
1172 * cleared if it is already pending.
1173 */
1174 if (bp->b_vp) {
1175 BO_LOCK(bp->b_bufobj);
1176 if (!(bp->b_vflags & BV_BKGRDINPROG))
1177 bp->b_flags |= B_RELBUF;
1178 BO_UNLOCK(bp->b_bufobj);
1179 } else
1180 bp->b_flags |= B_RELBUF;
1181 }
1182
1183 /*
1184 * VMIO buffer rundown. It is not very necessary to keep a VMIO buffer
1185 * constituted, not even NFS buffers now. Two flags effect this. If
1186 * B_INVAL, the struct buf is invalidated but the VM object is kept
1187 * around ( i.e. so it is trivial to reconstitute the buffer later ).
1188 *
1189 * If BIO_ERROR or B_NOCACHE is set, pages in the VM object will be
1190 * invalidated. BIO_ERROR cannot be set for a failed write unless the
1191 * buffer is also B_INVAL because it hits the re-dirtying code above.
1192 *
1193 * Normally we can do this whether a buffer is B_DELWRI or not. If
1194 * the buffer is an NFS buffer, it is tracking piecemeal writes or
1195 * the commit state and we cannot afford to lose the buffer. If the
1196 * buffer has a background write in progress, we need to keep it
1197 * around to prevent it from being reconstituted and starting a second
1198 * background write.
1199 */
1200 if ((bp->b_flags & B_VMIO)
1201 && !(bp->b_vp->v_mount != NULL &&
1202 (bp->b_vp->v_mount->mnt_vfc->vfc_flags & VFCF_NETWORK) != 0 &&
1203 !vn_isdisk(bp->b_vp, NULL) &&
1204 (bp->b_flags & B_DELWRI))
1205 ) {
1206
1207 int i, j, resid;
1208 vm_page_t m;
1209 off_t foff;
1210 vm_pindex_t poff;
1211 vm_object_t obj;
1212
1213 obj = bp->b_bufobj->bo_object;
1214
1215 /*
1216 * Get the base offset and length of the buffer. Note that
1217 * in the VMIO case if the buffer block size is not
1218 * page-aligned then b_data pointer may not be page-aligned.
1219 * But our b_pages[] array *IS* page aligned.
1220 *
1221 * block sizes less then DEV_BSIZE (usually 512) are not
1222 * supported due to the page granularity bits (m->valid,
1223 * m->dirty, etc...).
1224 *
1225 * See man buf(9) for more information
1226 */
1227 resid = bp->b_bufsize;
1228 foff = bp->b_offset;
1229 VM_OBJECT_LOCK(obj);
1230 for (i = 0; i < bp->b_npages; i++) {
1231 int had_bogus = 0;
1232
1233 m = bp->b_pages[i];
1234
1235 /*
1236 * If we hit a bogus page, fixup *all* the bogus pages
1237 * now.
1238 */
1239 if (m == bogus_page) {
1240 poff = OFF_TO_IDX(bp->b_offset);
1241 had_bogus = 1;
1242
1243 for (j = i; j < bp->b_npages; j++) {
1244 vm_page_t mtmp;
1245 mtmp = bp->b_pages[j];
1246 if (mtmp == bogus_page) {
1247 mtmp = vm_page_lookup(obj, poff + j);
1248 if (!mtmp) {
1249 panic("brelse: page missing\n");
1250 }
1251 bp->b_pages[j] = mtmp;
1252 }
1253 }
1254
1255 if ((bp->b_flags & B_INVAL) == 0) {
1256 pmap_qenter(
1257 trunc_page((vm_offset_t)bp->b_data),
1258 bp->b_pages, bp->b_npages);
1259 }
1260 m = bp->b_pages[i];
1261 }
1262 if ((bp->b_flags & B_NOCACHE) ||
1263 (bp->b_ioflags & BIO_ERROR)) {
1264 int poffset = foff & PAGE_MASK;
1265 int presid = resid > (PAGE_SIZE - poffset) ?
1266 (PAGE_SIZE - poffset) : resid;
1267
1268 KASSERT(presid >= 0, ("brelse: extra page"));
1269 vm_page_lock_queues();
1270 vm_page_set_invalid(m, poffset, presid);
1271 vm_page_unlock_queues();
1272 if (had_bogus)
1273 printf("avoided corruption bug in bogus_page/brelse code\n");
1274 }
1275 resid -= PAGE_SIZE - (foff & PAGE_MASK);
1276 foff = (foff + PAGE_SIZE) & ~(off_t)PAGE_MASK;
1277 }
1278 VM_OBJECT_UNLOCK(obj);
1279 if (bp->b_flags & (B_INVAL | B_RELBUF))
1280 vfs_vmio_release(bp);
1281
1282 } else if (bp->b_flags & B_VMIO) {
1283
1284 if (bp->b_flags & (B_INVAL | B_RELBUF)) {
1285 vfs_vmio_release(bp);
1286 }
1287
1288 } else if ((bp->b_flags & (B_INVAL | B_RELBUF)) != 0) {
1289 if (bp->b_bufsize != 0)
1290 allocbuf(bp, 0);
1291 if (bp->b_vp != NULL)
1292 brelvp(bp);
1293 }
1294
1295 if (BUF_REFCNT(bp) > 1) {
1296 /* do not release to free list */
1297 BUF_UNLOCK(bp);
1298 return;
1299 }
1300
1301 /* enqueue */
1302 mtx_lock(&bqlock);
1303 /* Handle delayed bremfree() processing. */
1304 if (bp->b_flags & B_REMFREE)
1305 bremfreel(bp);
1306 if (bp->b_qindex != QUEUE_NONE)
1307 panic("brelse: free buffer onto another queue???");
1308
1309 /* buffers with no memory */
1310 if (bp->b_bufsize == 0) {
1311 bp->b_flags |= B_INVAL;
1312 bp->b_xflags &= ~(BX_BKGRDWRITE | BX_ALTDATA);
1313 if (bp->b_vflags & BV_BKGRDINPROG)
1314 panic("losing buffer 1");
1315 if (bp->b_kvasize) {
1316 bp->b_qindex = QUEUE_EMPTYKVA;
1317 } else {
1318 bp->b_qindex = QUEUE_EMPTY;
1319 }
1320 TAILQ_INSERT_HEAD(&bufqueues[bp->b_qindex], bp, b_freelist);
1321 /* buffers with junk contents */
1322 } else if (bp->b_flags & (B_INVAL | B_NOCACHE | B_RELBUF) ||
1323 (bp->b_ioflags & BIO_ERROR)) {
1324 bp->b_flags |= B_INVAL;
1325 bp->b_xflags &= ~(BX_BKGRDWRITE | BX_ALTDATA);
1326 if (bp->b_vflags & BV_BKGRDINPROG)
1327 panic("losing buffer 2");
1328 bp->b_qindex = QUEUE_CLEAN;
1329 TAILQ_INSERT_HEAD(&bufqueues[QUEUE_CLEAN], bp, b_freelist);
1330 /* remaining buffers */
1331 } else {
1332 if (bp->b_flags & B_DELWRI)
1333 bp->b_qindex = QUEUE_DIRTY;
1334 else
1335 bp->b_qindex = QUEUE_CLEAN;
1336 if (bp->b_flags & B_AGE)
1337 TAILQ_INSERT_HEAD(&bufqueues[bp->b_qindex], bp, b_freelist);
1338 else
1339 TAILQ_INSERT_TAIL(&bufqueues[bp->b_qindex], bp, b_freelist);
1340 }
1341 mtx_unlock(&bqlock);
1342
1343 /*
1344 * If B_INVAL and B_DELWRI is set, clear B_DELWRI. We have already
1345 * placed the buffer on the correct queue. We must also disassociate
1346 * the device and vnode for a B_INVAL buffer so gbincore() doesn't
1347 * find it.
1348 */
1349 if (bp->b_flags & B_INVAL) {
1350 if (bp->b_flags & B_DELWRI)
1351 bundirty(bp);
1352 if (bp->b_vp)
1353 brelvp(bp);
1354 }
1355
1356 /*
1357 * Fixup numfreebuffers count. The bp is on an appropriate queue
1358 * unless locked. We then bump numfreebuffers if it is not B_DELWRI.
1359 * We've already handled the B_INVAL case ( B_DELWRI will be clear
1360 * if B_INVAL is set ).
1361 */
1362
1363 if (!(bp->b_flags & B_DELWRI))
1364 bufcountwakeup();
1365
1366 /*
1367 * Something we can maybe free or reuse
1368 */
1369 if (bp->b_bufsize || bp->b_kvasize)
1370 bufspacewakeup();
1371
1372 bp->b_flags &= ~(B_ASYNC | B_NOCACHE | B_AGE | B_RELBUF | B_DIRECT);
1373 if ((bp->b_flags & B_DELWRI) == 0 && (bp->b_xflags & BX_VNDIRTY))
1374 panic("brelse: not dirty");
1375 /* unlock */
1376 BUF_UNLOCK(bp);
1377 }
1378
1379 /*
1380 * Release a buffer back to the appropriate queue but do not try to free
1381 * it. The buffer is expected to be used again soon.
1382 *
1383 * bqrelse() is used by bdwrite() to requeue a delayed write, and used by
1384 * biodone() to requeue an async I/O on completion. It is also used when
1385 * known good buffers need to be requeued but we think we may need the data
1386 * again soon.
1387 *
1388 * XXX we should be able to leave the B_RELBUF hint set on completion.
1389 */
1390 void
1391 bqrelse(struct buf *bp)
1392 {
1393 CTR3(KTR_BUF, "bqrelse(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
1394 KASSERT(!(bp->b_flags & (B_CLUSTER|B_PAGING)),
1395 ("bqrelse: inappropriate B_PAGING or B_CLUSTER bp %p", bp));
1396
1397 if (BUF_REFCNT(bp) > 1) {
1398 /* do not release to free list */
1399 BUF_UNLOCK(bp);
1400 return;
1401 }
1402 mtx_lock(&bqlock);
1403 /* Handle delayed bremfree() processing. */
1404 if (bp->b_flags & B_REMFREE)
1405 bremfreel(bp);
1406 if (bp->b_qindex != QUEUE_NONE)
1407 panic("bqrelse: free buffer onto another queue???");
1408 /* buffers with stale but valid contents */
1409 if (bp->b_flags & B_DELWRI) {
1410 bp->b_qindex = QUEUE_DIRTY;
1411 TAILQ_INSERT_TAIL(&bufqueues[QUEUE_DIRTY], bp, b_freelist);
1412 } else {
1413 /*
1414 * XXX This lock may not be necessary since BKGRDINPROG
1415 * cannot be set while we hold the buf lock, it can only be
1416 * cleared if it is already pending.
1417 */
1418 BO_LOCK(bp->b_bufobj);
1419 if (!vm_page_count_severe() || bp->b_vflags & BV_BKGRDINPROG) {
1420 BO_UNLOCK(bp->b_bufobj);
1421 bp->b_qindex = QUEUE_CLEAN;
1422 TAILQ_INSERT_TAIL(&bufqueues[QUEUE_CLEAN], bp,
1423 b_freelist);
1424 } else {
1425 /*
1426 * We are too low on memory, we have to try to free
1427 * the buffer (most importantly: the wired pages
1428 * making up its backing store) *now*.
1429 */
1430 BO_UNLOCK(bp->b_bufobj);
1431 mtx_unlock(&bqlock);
1432 brelse(bp);
1433 return;
1434 }
1435 }
1436 mtx_unlock(&bqlock);
1437
1438 if ((bp->b_flags & B_INVAL) || !(bp->b_flags & B_DELWRI))
1439 bufcountwakeup();
1440
1441 /*
1442 * Something we can maybe free or reuse.
1443 */
1444 if (bp->b_bufsize && !(bp->b_flags & B_DELWRI))
1445 bufspacewakeup();
1446
1447 bp->b_flags &= ~(B_ASYNC | B_NOCACHE | B_AGE | B_RELBUF);
1448 if ((bp->b_flags & B_DELWRI) == 0 && (bp->b_xflags & BX_VNDIRTY))
1449 panic("bqrelse: not dirty");
1450 /* unlock */
1451 BUF_UNLOCK(bp);
1452 }
1453
1454 /* Give pages used by the bp back to the VM system (where possible) */
1455 static void
1456 vfs_vmio_release(struct buf *bp)
1457 {
1458 int i;
1459 vm_page_t m;
1460
1461 VM_OBJECT_LOCK(bp->b_bufobj->bo_object);
1462 vm_page_lock_queues();
1463 for (i = 0; i < bp->b_npages; i++) {
1464 m = bp->b_pages[i];
1465 bp->b_pages[i] = NULL;
1466 /*
1467 * In order to keep page LRU ordering consistent, put
1468 * everything on the inactive queue.
1469 */
1470 vm_page_unwire(m, 0);
1471 /*
1472 * We don't mess with busy pages, it is
1473 * the responsibility of the process that
1474 * busied the pages to deal with them.
1475 */
1476 if ((m->flags & PG_BUSY) || (m->busy != 0))
1477 continue;
1478
1479 if (m->wire_count == 0) {
1480 /*
1481 * Might as well free the page if we can and it has
1482 * no valid data. We also free the page if the
1483 * buffer was used for direct I/O
1484 */
1485 if ((bp->b_flags & B_ASYNC) == 0 && !m->valid &&
1486 m->hold_count == 0) {
1487 pmap_remove_all(m);
1488 vm_page_free(m);
1489 } else if (bp->b_flags & B_DIRECT) {
1490 vm_page_try_to_free(m);
1491 } else if (vm_page_count_severe()) {
1492 vm_page_try_to_cache(m);
1493 }
1494 }
1495 }
1496 vm_page_unlock_queues();
1497 VM_OBJECT_UNLOCK(bp->b_bufobj->bo_object);
1498 pmap_qremove(trunc_page((vm_offset_t) bp->b_data), bp->b_npages);
1499
1500 if (bp->b_bufsize) {
1501 bufspacewakeup();
1502 bp->b_bufsize = 0;
1503 }
1504 bp->b_npages = 0;
1505 bp->b_flags &= ~B_VMIO;
1506 if (bp->b_vp)
1507 brelvp(bp);
1508 }
1509
1510 /*
1511 * Check to see if a block at a particular lbn is available for a clustered
1512 * write.
1513 */
1514 static int
1515 vfs_bio_clcheck(struct vnode *vp, int size, daddr_t lblkno, daddr_t blkno)
1516 {
1517 struct buf *bpa;
1518 int match;
1519
1520 match = 0;
1521
1522 /* If the buf isn't in core skip it */
1523 if ((bpa = gbincore(&vp->v_bufobj, lblkno)) == NULL)
1524 return (0);
1525
1526 /* If the buf is busy we don't want to wait for it */
1527 if (BUF_LOCK(bpa, LK_EXCLUSIVE | LK_NOWAIT, NULL) != 0)
1528 return (0);
1529
1530 /* Only cluster with valid clusterable delayed write buffers */
1531 if ((bpa->b_flags & (B_DELWRI | B_CLUSTEROK | B_INVAL)) !=
1532 (B_DELWRI | B_CLUSTEROK))
1533 goto done;
1534
1535 if (bpa->b_bufsize != size)
1536 goto done;
1537
1538 /*
1539 * Check to see if it is in the expected place on disk and that the
1540 * block has been mapped.
1541 */
1542 if ((bpa->b_blkno != bpa->b_lblkno) && (bpa->b_blkno == blkno))
1543 match = 1;
1544 done:
1545 BUF_UNLOCK(bpa);
1546 return (match);
1547 }
1548
1549 /*
1550 * vfs_bio_awrite:
1551 *
1552 * Implement clustered async writes for clearing out B_DELWRI buffers.
1553 * This is much better then the old way of writing only one buffer at
1554 * a time. Note that we may not be presented with the buffers in the
1555 * correct order, so we search for the cluster in both directions.
1556 */
1557 int
1558 vfs_bio_awrite(struct buf *bp)
1559 {
1560 int i;
1561 int j;
1562 daddr_t lblkno = bp->b_lblkno;
1563 struct vnode *vp = bp->b_vp;
1564 int ncl;
1565 int nwritten;
1566 int size;
1567 int maxcl;
1568
1569 /*
1570 * right now we support clustered writing only to regular files. If
1571 * we find a clusterable block we could be in the middle of a cluster
1572 * rather then at the beginning.
1573 */
1574 if ((vp->v_type == VREG) &&
1575 (vp->v_mount != 0) && /* Only on nodes that have the size info */
1576 (bp->b_flags & (B_CLUSTEROK | B_INVAL)) == B_CLUSTEROK) {
1577
1578 size = vp->v_mount->mnt_stat.f_iosize;
1579 maxcl = MAXPHYS / size;
1580
1581 VI_LOCK(vp);
1582 for (i = 1; i < maxcl; i++)
1583 if (vfs_bio_clcheck(vp, size, lblkno + i,
1584 bp->b_blkno + ((i * size) >> DEV_BSHIFT)) == 0)
1585 break;
1586
1587 for (j = 1; i + j <= maxcl && j <= lblkno; j++)
1588 if (vfs_bio_clcheck(vp, size, lblkno - j,
1589 bp->b_blkno - ((j * size) >> DEV_BSHIFT)) == 0)
1590 break;
1591
1592 VI_UNLOCK(vp);
1593 --j;
1594 ncl = i + j;
1595 /*
1596 * this is a possible cluster write
1597 */
1598 if (ncl != 1) {
1599 BUF_UNLOCK(bp);
1600 nwritten = cluster_wbuild(vp, size, lblkno - j, ncl);
1601 return nwritten;
1602 }
1603 }
1604 bremfree(bp);
1605 bp->b_flags |= B_ASYNC;
1606 /*
1607 * default (old) behavior, writing out only one block
1608 *
1609 * XXX returns b_bufsize instead of b_bcount for nwritten?
1610 */
1611 nwritten = bp->b_bufsize;
1612 (void) bwrite(bp);
1613
1614 return nwritten;
1615 }
1616
1617 /*
1618 * getnewbuf:
1619 *
1620 * Find and initialize a new buffer header, freeing up existing buffers
1621 * in the bufqueues as necessary. The new buffer is returned locked.
1622 *
1623 * Important: B_INVAL is not set. If the caller wishes to throw the
1624 * buffer away, the caller must set B_INVAL prior to calling brelse().
1625 *
1626 * We block if:
1627 * We have insufficient buffer headers
1628 * We have insufficient buffer space
1629 * buffer_map is too fragmented ( space reservation fails )
1630 * If we have to flush dirty buffers ( but we try to avoid this )
1631 *
1632 * To avoid VFS layer recursion we do not flush dirty buffers ourselves.
1633 * Instead we ask the buf daemon to do it for us. We attempt to
1634 * avoid piecemeal wakeups of the pageout daemon.
1635 */
1636
1637 static struct buf *
1638 getnewbuf(int slpflag, int slptimeo, int size, int maxsize)
1639 {
1640 struct buf *bp;
1641 struct buf *nbp;
1642 int defrag = 0;
1643 int nqindex;
1644 static int flushingbufs;
1645
1646 /*
1647 * We can't afford to block since we might be holding a vnode lock,
1648 * which may prevent system daemons from running. We deal with
1649 * low-memory situations by proactively returning memory and running
1650 * async I/O rather then sync I/O.
1651 */
1652
1653 atomic_add_int(&getnewbufcalls, 1);
1654 atomic_subtract_int(&getnewbufrestarts, 1);
1655 restart:
1656 atomic_add_int(&getnewbufrestarts, 1);
1657
1658 /*
1659 * Setup for scan. If we do not have enough free buffers,
1660 * we setup a degenerate case that immediately fails. Note
1661 * that if we are specially marked process, we are allowed to
1662 * dip into our reserves.
1663 *
1664 * The scanning sequence is nominally: EMPTY->EMPTYKVA->CLEAN
1665 *
1666 * We start with EMPTYKVA. If the list is empty we backup to EMPTY.
1667 * However, there are a number of cases (defragging, reusing, ...)
1668 * where we cannot backup.
1669 */
1670 mtx_lock(&bqlock);
1671 nqindex = QUEUE_EMPTYKVA;
1672 nbp = TAILQ_FIRST(&bufqueues[QUEUE_EMPTYKVA]);
1673
1674 if (nbp == NULL) {
1675 /*
1676 * If no EMPTYKVA buffers and we are either
1677 * defragging or reusing, locate a CLEAN buffer
1678 * to free or reuse. If bufspace useage is low
1679 * skip this step so we can allocate a new buffer.
1680 */
1681 if (defrag || bufspace >= lobufspace) {
1682 nqindex = QUEUE_CLEAN;
1683 nbp = TAILQ_FIRST(&bufqueues[QUEUE_CLEAN]);
1684 }
1685
1686 /*
1687 * If we could not find or were not allowed to reuse a
1688 * CLEAN buffer, check to see if it is ok to use an EMPTY
1689 * buffer. We can only use an EMPTY buffer if allocating
1690 * its KVA would not otherwise run us out of buffer space.
1691 */
1692 if (nbp == NULL && defrag == 0 &&
1693 bufspace + maxsize < hibufspace) {
1694 nqindex = QUEUE_EMPTY;
1695 nbp = TAILQ_FIRST(&bufqueues[QUEUE_EMPTY]);
1696 }
1697 }
1698
1699 /*
1700 * Run scan, possibly freeing data and/or kva mappings on the fly
1701 * depending.
1702 */
1703
1704 while ((bp = nbp) != NULL) {
1705 int qindex = nqindex;
1706
1707 /*
1708 * Calculate next bp ( we can only use it if we do not block
1709 * or do other fancy things ).
1710 */
1711 if ((nbp = TAILQ_NEXT(bp, b_freelist)) == NULL) {
1712 switch(qindex) {
1713 case QUEUE_EMPTY:
1714 nqindex = QUEUE_EMPTYKVA;
1715 if ((nbp = TAILQ_FIRST(&bufqueues[QUEUE_EMPTYKVA])))
1716 break;
1717 /* FALLTHROUGH */
1718 case QUEUE_EMPTYKVA:
1719 nqindex = QUEUE_CLEAN;
1720 if ((nbp = TAILQ_FIRST(&bufqueues[QUEUE_CLEAN])))
1721 break;
1722 /* FALLTHROUGH */
1723 case QUEUE_CLEAN:
1724 /*
1725 * nbp is NULL.
1726 */
1727 break;
1728 }
1729 }
1730 /*
1731 * If we are defragging then we need a buffer with
1732 * b_kvasize != 0. XXX this situation should no longer
1733 * occur, if defrag is non-zero the buffer's b_kvasize
1734 * should also be non-zero at this point. XXX
1735 */
1736 if (defrag && bp->b_kvasize == 0) {
1737 printf("Warning: defrag empty buffer %p\n", bp);
1738 continue;
1739 }
1740
1741 /*
1742 * Start freeing the bp. This is somewhat involved. nbp
1743 * remains valid only for QUEUE_EMPTY[KVA] bp's.
1744 */
1745 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL) != 0)
1746 continue;
1747 if (bp->b_vp) {
1748 BO_LOCK(bp->b_bufobj);
1749 if (bp->b_vflags & BV_BKGRDINPROG) {
1750 BO_UNLOCK(bp->b_bufobj);
1751 BUF_UNLOCK(bp);
1752 continue;
1753 }
1754 BO_UNLOCK(bp->b_bufobj);
1755 }
1756 CTR6(KTR_BUF,
1757 "getnewbuf(%p) vp %p flags %X kvasize %d bufsize %d "
1758 "queue %d (recycling)", bp, bp->b_vp, bp->b_flags,
1759 bp->b_kvasize, bp->b_bufsize, qindex);
1760
1761 /*
1762 * Sanity Checks
1763 */
1764 KASSERT(bp->b_qindex == qindex, ("getnewbuf: inconsistant queue %d bp %p", qindex, bp));
1765
1766 /*
1767 * Note: we no longer distinguish between VMIO and non-VMIO
1768 * buffers.
1769 */
1770
1771 KASSERT((bp->b_flags & B_DELWRI) == 0, ("delwri buffer %p found in queue %d", bp, qindex));
1772
1773 bremfreel(bp);
1774 mtx_unlock(&bqlock);
1775
1776 if (qindex == QUEUE_CLEAN) {
1777 if (bp->b_flags & B_VMIO) {
1778 bp->b_flags &= ~B_ASYNC;
1779 vfs_vmio_release(bp);
1780 }
1781 if (bp->b_vp)
1782 brelvp(bp);
1783 }
1784
1785 /*
1786 * NOTE: nbp is now entirely invalid. We can only restart
1787 * the scan from this point on.
1788 *
1789 * Get the rest of the buffer freed up. b_kva* is still
1790 * valid after this operation.
1791 */
1792
1793 if (bp->b_rcred != NOCRED) {
1794 crfree(bp->b_rcred);
1795 bp->b_rcred = NOCRED;
1796 }
1797 if (bp->b_wcred != NOCRED) {
1798 crfree(bp->b_wcred);
1799 bp->b_wcred = NOCRED;
1800 }
1801 if (LIST_FIRST(&bp->b_dep) != NULL)
1802 buf_deallocate(bp);
1803 if (bp->b_vflags & BV_BKGRDINPROG)
1804 panic("losing buffer 3");
1805 KASSERT(bp->b_vp == NULL,
1806 ("bp: %p still has vnode %p. qindex: %d",
1807 bp, bp->b_vp, qindex));
1808 KASSERT((bp->b_xflags & (BX_VNCLEAN|BX_VNDIRTY)) == 0,
1809 ("bp: %p still on a buffer list. xflags %X",
1810 bp, bp->b_xflags));
1811
1812 if (bp->b_bufsize)
1813 allocbuf(bp, 0);
1814
1815 bp->b_flags = 0;
1816 bp->b_ioflags = 0;
1817 bp->b_xflags = 0;
1818 bp->b_vflags = 0;
1819 bp->b_vp = NULL;
1820 bp->b_blkno = bp->b_lblkno = 0;
1821 bp->b_offset = NOOFFSET;
1822 bp->b_iodone = 0;
1823 bp->b_error = 0;
1824 bp->b_resid = 0;
1825 bp->b_bcount = 0;
1826 bp->b_npages = 0;
1827 bp->b_dirtyoff = bp->b_dirtyend = 0;
1828 bp->b_bufobj = NULL;
1829
1830 LIST_INIT(&bp->b_dep);
1831
1832 /*
1833 * If we are defragging then free the buffer.
1834 */
1835 if (defrag) {
1836 bp->b_flags |= B_INVAL;
1837 bfreekva(bp);
1838 brelse(bp);
1839 defrag = 0;
1840 goto restart;
1841 }
1842
1843 /*
1844 * If we are overcomitted then recover the buffer and its
1845 * KVM space. This occurs in rare situations when multiple
1846 * processes are blocked in getnewbuf() or allocbuf().
1847 */
1848 if (bufspace >= hibufspace)
1849 flushingbufs = 1;
1850 if (flushingbufs && bp->b_kvasize != 0) {
1851 bp->b_flags |= B_INVAL;
1852 bfreekva(bp);
1853 brelse(bp);
1854 goto restart;
1855 }
1856 if (bufspace < lobufspace)
1857 flushingbufs = 0;
1858 break;
1859 }
1860
1861 /*
1862 * If we exhausted our list, sleep as appropriate. We may have to
1863 * wakeup various daemons and write out some dirty buffers.
1864 *
1865 * Generally we are sleeping due to insufficient buffer space.
1866 */
1867
1868 if (bp == NULL) {
1869 int flags;
1870 char *waitmsg;
1871
1872 if (defrag) {
1873 flags = VFS_BIO_NEED_BUFSPACE;
1874 waitmsg = "nbufkv";
1875 } else if (bufspace >= hibufspace) {
1876 waitmsg = "nbufbs";
1877 flags = VFS_BIO_NEED_BUFSPACE;
1878 } else {
1879 waitmsg = "newbuf";
1880 flags = VFS_BIO_NEED_ANY;
1881 }
1882 mtx_lock(&nblock);
1883 needsbuffer |= flags;
1884 mtx_unlock(&nblock);
1885 mtx_unlock(&bqlock);
1886
1887 bd_speedup(); /* heeeelp */
1888
1889 mtx_lock(&nblock);
1890 while (needsbuffer & flags) {
1891 if (msleep(&needsbuffer, &nblock,
1892 (PRIBIO + 4) | slpflag, waitmsg, slptimeo)) {
1893 mtx_unlock(&nblock);
1894 return (NULL);
1895 }
1896 }
1897 mtx_unlock(&nblock);
1898 } else {
1899 /*
1900 * We finally have a valid bp. We aren't quite out of the
1901 * woods, we still have to reserve kva space. In order
1902 * to keep fragmentation sane we only allocate kva in
1903 * BKVASIZE chunks.
1904 */
1905 maxsize = (maxsize + BKVAMASK) & ~BKVAMASK;
1906
1907 if (maxsize != bp->b_kvasize) {
1908 vm_offset_t addr = 0;
1909
1910 bfreekva(bp);
1911
1912 vm_map_lock(buffer_map);
1913 if (vm_map_findspace(buffer_map,
1914 vm_map_min(buffer_map), maxsize, &addr)) {
1915 /*
1916 * Uh oh. Buffer map is to fragmented. We
1917 * must defragment the map.
1918 */
1919 atomic_add_int(&bufdefragcnt, 1);
1920 vm_map_unlock(buffer_map);
1921 defrag = 1;
1922 bp->b_flags |= B_INVAL;
1923 brelse(bp);
1924 goto restart;
1925 }
1926 if (addr) {
1927 vm_map_insert(buffer_map, NULL, 0,
1928 addr, addr + maxsize,
1929 VM_PROT_ALL, VM_PROT_ALL, MAP_NOFAULT);
1930
1931 bp->b_kvabase = (caddr_t) addr;
1932 bp->b_kvasize = maxsize;
1933 atomic_add_int(&bufspace, bp->b_kvasize);
1934 atomic_add_int(&bufreusecnt, 1);
1935 }
1936 vm_map_unlock(buffer_map);
1937 }
1938 bp->b_saveaddr = bp->b_kvabase;
1939 bp->b_data = bp->b_saveaddr;
1940 }
1941 return(bp);
1942 }
1943
1944 /*
1945 * buf_daemon:
1946 *
1947 * buffer flushing daemon. Buffers are normally flushed by the
1948 * update daemon but if it cannot keep up this process starts to
1949 * take the load in an attempt to prevent getnewbuf() from blocking.
1950 */
1951
1952 static struct kproc_desc buf_kp = {
1953 "bufdaemon",
1954 buf_daemon,
1955 &bufdaemonproc
1956 };
1957 SYSINIT(bufdaemon, SI_SUB_KTHREAD_BUF, SI_ORDER_FIRST, kproc_start, &buf_kp)
1958
1959 static void
1960 buf_daemon()
1961 {
1962 mtx_lock(&Giant);
1963
1964 /*
1965 * This process needs to be suspended prior to shutdown sync.
1966 */
1967 EVENTHANDLER_REGISTER(shutdown_pre_sync, kproc_shutdown, bufdaemonproc,
1968 SHUTDOWN_PRI_LAST);
1969
1970 /*
1971 * This process is allowed to take the buffer cache to the limit
1972 */
1973 curthread->td_pflags |= TDP_NORUNNINGBUF;
1974 mtx_lock(&bdlock);
1975 for (;;) {
1976 bd_request = 0;
1977 mtx_unlock(&bdlock);
1978
1979 kthread_suspend_check(bufdaemonproc);
1980
1981 /*
1982 * Do the flush. Limit the amount of in-transit I/O we
1983 * allow to build up, otherwise we would completely saturate
1984 * the I/O system. Wakeup any waiting processes before we
1985 * normally would so they can run in parallel with our drain.
1986 */
1987 while (numdirtybuffers > lodirtybuffers) {
1988 if (flushbufqueues(0) == 0) {
1989 /*
1990 * Could not find any buffers without rollback
1991 * dependencies, so just write the first one
1992 * in the hopes of eventually making progress.
1993 */
1994 flushbufqueues(1);
1995 break;
1996 }
1997 uio_yield();
1998 }
1999
2000 /*
2001 * Only clear bd_request if we have reached our low water
2002 * mark. The buf_daemon normally waits 1 second and
2003 * then incrementally flushes any dirty buffers that have
2004 * built up, within reason.
2005 *
2006 * If we were unable to hit our low water mark and couldn't
2007 * find any flushable buffers, we sleep half a second.
2008 * Otherwise we loop immediately.
2009 */
2010 mtx_lock(&bdlock);
2011 if (numdirtybuffers <= lodirtybuffers) {
2012 /*
2013 * We reached our low water mark, reset the
2014 * request and sleep until we are needed again.
2015 * The sleep is just so the suspend code works.
2016 */
2017 bd_request = 0;
2018 msleep(&bd_request, &bdlock, PVM, "psleep", hz);
2019 } else {
2020 /*
2021 * We couldn't find any flushable dirty buffers but
2022 * still have too many dirty buffers, we
2023 * have to sleep and try again. (rare)
2024 */
2025 msleep(&bd_request, &bdlock, PVM, "qsleep", hz / 10);
2026 }
2027 }
2028 }
2029
2030 /*
2031 * flushbufqueues:
2032 *
2033 * Try to flush a buffer in the dirty queue. We must be careful to
2034 * free up B_INVAL buffers instead of write them, which NFS is
2035 * particularly sensitive to.
2036 */
2037 static int flushwithdeps = 0;
2038 SYSCTL_INT(_vfs, OID_AUTO, flushwithdeps, CTLFLAG_RW, &flushwithdeps,
2039 0, "Number of buffers flushed with dependecies that require rollbacks");
2040
2041 static int
2042 flushbufqueues(int flushdeps)
2043 {
2044 struct thread *td = curthread;
2045 struct buf sentinel;
2046 struct vnode *vp;
2047 struct mount *mp;
2048 struct buf *bp;
2049 int hasdeps;
2050 int flushed;
2051 int target;
2052
2053 target = numdirtybuffers - lodirtybuffers;
2054 if (flushdeps && target > 2)
2055 target /= 2;
2056 flushed = 0;
2057 bp = NULL;
2058 mtx_lock(&bqlock);
2059 TAILQ_INSERT_TAIL(&bufqueues[QUEUE_DIRTY], &sentinel, b_freelist);
2060 while (flushed != target) {
2061 bp = TAILQ_FIRST(&bufqueues[QUEUE_DIRTY]);
2062 if (bp == &sentinel)
2063 break;
2064 TAILQ_REMOVE(&bufqueues[QUEUE_DIRTY], bp, b_freelist);
2065 TAILQ_INSERT_TAIL(&bufqueues[QUEUE_DIRTY], bp, b_freelist);
2066
2067 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL) != 0)
2068 continue;
2069 BO_LOCK(bp->b_bufobj);
2070 if ((bp->b_vflags & BV_BKGRDINPROG) != 0 ||
2071 (bp->b_flags & B_DELWRI) == 0) {
2072 BO_UNLOCK(bp->b_bufobj);
2073 BUF_UNLOCK(bp);
2074 continue;
2075 }
2076 BO_UNLOCK(bp->b_bufobj);
2077 if (bp->b_flags & B_INVAL) {
2078 bremfreel(bp);
2079 mtx_unlock(&bqlock);
2080 brelse(bp);
2081 flushed++;
2082 numdirtywakeup((lodirtybuffers + hidirtybuffers) / 2);
2083 mtx_lock(&bqlock);
2084 continue;
2085 }
2086
2087 if (LIST_FIRST(&bp->b_dep) != NULL && buf_countdeps(bp, 0)) {
2088 if (flushdeps == 0) {
2089 BUF_UNLOCK(bp);
2090 continue;
2091 }
2092 hasdeps = 1;
2093 } else
2094 hasdeps = 0;
2095 /*
2096 * We must hold the lock on a vnode before writing
2097 * one of its buffers. Otherwise we may confuse, or
2098 * in the case of a snapshot vnode, deadlock the
2099 * system.
2100 *
2101 * The lock order here is the reverse of the normal
2102 * of vnode followed by buf lock. This is ok because
2103 * the NOWAIT will prevent deadlock.
2104 */
2105 vp = bp->b_vp;
2106 if (vn_start_write(vp, &mp, V_NOWAIT) != 0) {
2107 BUF_UNLOCK(bp);
2108 continue;
2109 }
2110 if (vn_lock(vp, LK_EXCLUSIVE | LK_NOWAIT, td) == 0) {
2111 mtx_unlock(&bqlock);
2112 CTR3(KTR_BUF, "flushbufqueue(%p) vp %p flags %X",
2113 bp, bp->b_vp, bp->b_flags);
2114 vfs_bio_awrite(bp);
2115 vn_finished_write(mp);
2116 VOP_UNLOCK(vp, 0, td);
2117 flushwithdeps += hasdeps;
2118 flushed++;
2119 waitrunningbufspace();
2120 numdirtywakeup((lodirtybuffers + hidirtybuffers) / 2);
2121 mtx_lock(&bqlock);
2122 continue;
2123 }
2124 vn_finished_write(mp);
2125 BUF_UNLOCK(bp);
2126 }
2127 TAILQ_REMOVE(&bufqueues[QUEUE_DIRTY], &sentinel, b_freelist);
2128 mtx_unlock(&bqlock);
2129 return (flushed);
2130 }
2131
2132 /*
2133 * Check to see if a block is currently memory resident.
2134 */
2135 struct buf *
2136 incore(struct bufobj *bo, daddr_t blkno)
2137 {
2138 struct buf *bp;
2139
2140 BO_LOCK(bo);
2141 bp = gbincore(bo, blkno);
2142 BO_UNLOCK(bo);
2143 return (bp);
2144 }
2145
2146 /*
2147 * Returns true if no I/O is needed to access the
2148 * associated VM object. This is like incore except
2149 * it also hunts around in the VM system for the data.
2150 */
2151
2152 static int
2153 inmem(struct vnode * vp, daddr_t blkno)
2154 {
2155 vm_object_t obj;
2156 vm_offset_t toff, tinc, size;
2157 vm_page_t m;
2158 vm_ooffset_t off;
2159
2160 ASSERT_VOP_LOCKED(vp, "inmem");
2161
2162 if (incore(&vp->v_bufobj, blkno))
2163 return 1;
2164 if (vp->v_mount == NULL)
2165 return 0;
2166 obj = vp->v_object;
2167 if (obj == NULL)
2168 return (0);
2169
2170 size = PAGE_SIZE;
2171 if (size > vp->v_mount->mnt_stat.f_iosize)
2172 size = vp->v_mount->mnt_stat.f_iosize;
2173 off = (vm_ooffset_t)blkno * (vm_ooffset_t)vp->v_mount->mnt_stat.f_iosize;
2174
2175 VM_OBJECT_LOCK(obj);
2176 for (toff = 0; toff < vp->v_mount->mnt_stat.f_iosize; toff += tinc) {
2177 m = vm_page_lookup(obj, OFF_TO_IDX(off + toff));
2178 if (!m)
2179 goto notinmem;
2180 tinc = size;
2181 if (tinc > PAGE_SIZE - ((toff + off) & PAGE_MASK))
2182 tinc = PAGE_SIZE - ((toff + off) & PAGE_MASK);
2183 if (vm_page_is_valid(m,
2184 (vm_offset_t) ((toff + off) & PAGE_MASK), tinc) == 0)
2185 goto notinmem;
2186 }
2187 VM_OBJECT_UNLOCK(obj);
2188 return 1;
2189
2190 notinmem:
2191 VM_OBJECT_UNLOCK(obj);
2192 return (0);
2193 }
2194
2195 /*
2196 * vfs_setdirty:
2197 *
2198 * Sets the dirty range for a buffer based on the status of the dirty
2199 * bits in the pages comprising the buffer.
2200 *
2201 * The range is limited to the size of the buffer.
2202 *
2203 * This routine is primarily used by NFS, but is generalized for the
2204 * B_VMIO case.
2205 */
2206 static void
2207 vfs_setdirty(struct buf *bp)
2208 {
2209 int i;
2210 vm_object_t object;
2211
2212 /*
2213 * Degenerate case - empty buffer
2214 */
2215
2216 if (bp->b_bufsize == 0)
2217 return;
2218
2219 /*
2220 * We qualify the scan for modified pages on whether the
2221 * object has been flushed yet. The OBJ_WRITEABLE flag
2222 * is not cleared simply by protecting pages off.
2223 */
2224
2225 if ((bp->b_flags & B_VMIO) == 0)
2226 return;
2227
2228 object = bp->b_pages[0]->object;
2229 VM_OBJECT_LOCK(object);
2230 if ((object->flags & OBJ_WRITEABLE) && !(object->flags & OBJ_MIGHTBEDIRTY))
2231 printf("Warning: object %p writeable but not mightbedirty\n", object);
2232 if (!(object->flags & OBJ_WRITEABLE) && (object->flags & OBJ_MIGHTBEDIRTY))
2233 printf("Warning: object %p mightbedirty but not writeable\n", object);
2234
2235 if (object->flags & (OBJ_MIGHTBEDIRTY|OBJ_CLEANING)) {
2236 vm_offset_t boffset;
2237 vm_offset_t eoffset;
2238
2239 vm_page_lock_queues();
2240 /*
2241 * test the pages to see if they have been modified directly
2242 * by users through the VM system.
2243 */
2244 for (i = 0; i < bp->b_npages; i++)
2245 vm_page_test_dirty(bp->b_pages[i]);
2246
2247 /*
2248 * Calculate the encompassing dirty range, boffset and eoffset,
2249 * (eoffset - boffset) bytes.
2250 */
2251
2252 for (i = 0; i < bp->b_npages; i++) {
2253 if (bp->b_pages[i]->dirty)
2254 break;
2255 }
2256 boffset = (i << PAGE_SHIFT) - (bp->b_offset & PAGE_MASK);
2257
2258 for (i = bp->b_npages - 1; i >= 0; --i) {
2259 if (bp->b_pages[i]->dirty) {
2260 break;
2261 }
2262 }
2263 eoffset = ((i + 1) << PAGE_SHIFT) - (bp->b_offset & PAGE_MASK);
2264
2265 vm_page_unlock_queues();
2266 /*
2267 * Fit it to the buffer.
2268 */
2269
2270 if (eoffset > bp->b_bcount)
2271 eoffset = bp->b_bcount;
2272
2273 /*
2274 * If we have a good dirty range, merge with the existing
2275 * dirty range.
2276 */
2277
2278 if (boffset < eoffset) {
2279 if (bp->b_dirtyoff > boffset)
2280 bp->b_dirtyoff = boffset;
2281 if (bp->b_dirtyend < eoffset)
2282 bp->b_dirtyend = eoffset;
2283 }
2284 }
2285 VM_OBJECT_UNLOCK(object);
2286 }
2287
2288 /*
2289 * getblk:
2290 *
2291 * Get a block given a specified block and offset into a file/device.
2292 * The buffers B_DONE bit will be cleared on return, making it almost
2293 * ready for an I/O initiation. B_INVAL may or may not be set on
2294 * return. The caller should clear B_INVAL prior to initiating a
2295 * READ.
2296 *
2297 * For a non-VMIO buffer, B_CACHE is set to the opposite of B_INVAL for
2298 * an existing buffer.
2299 *
2300 * For a VMIO buffer, B_CACHE is modified according to the backing VM.
2301 * If getblk()ing a previously 0-sized invalid buffer, B_CACHE is set
2302 * and then cleared based on the backing VM. If the previous buffer is
2303 * non-0-sized but invalid, B_CACHE will be cleared.
2304 *
2305 * If getblk() must create a new buffer, the new buffer is returned with
2306 * both B_INVAL and B_CACHE clear unless it is a VMIO buffer, in which
2307 * case it is returned with B_INVAL clear and B_CACHE set based on the
2308 * backing VM.
2309 *
2310 * getblk() also forces a bwrite() for any B_DELWRI buffer whos
2311 * B_CACHE bit is clear.
2312 *
2313 * What this means, basically, is that the caller should use B_CACHE to
2314 * determine whether the buffer is fully valid or not and should clear
2315 * B_INVAL prior to issuing a read. If the caller intends to validate
2316 * the buffer by loading its data area with something, the caller needs
2317 * to clear B_INVAL. If the caller does this without issuing an I/O,
2318 * the caller should set B_CACHE ( as an optimization ), else the caller
2319 * should issue the I/O and biodone() will set B_CACHE if the I/O was
2320 * a write attempt or if it was a successfull read. If the caller
2321 * intends to issue a READ, the caller must clear B_INVAL and BIO_ERROR
2322 * prior to issuing the READ. biodone() will *not* clear B_INVAL.
2323 */
2324 struct buf *
2325 getblk(struct vnode * vp, daddr_t blkno, int size, int slpflag, int slptimeo,
2326 int flags)
2327 {
2328 struct buf *bp;
2329 struct bufobj *bo;
2330 int error;
2331
2332 CTR3(KTR_BUF, "getblk(%p, %ld, %d)", vp, (long)blkno, size);
2333 ASSERT_VOP_LOCKED(vp, "getblk");
2334 if (size > MAXBSIZE)
2335 panic("getblk: size(%d) > MAXBSIZE(%d)\n", size, MAXBSIZE);
2336
2337 bo = &vp->v_bufobj;
2338 loop:
2339 /*
2340 * Block if we are low on buffers. Certain processes are allowed
2341 * to completely exhaust the buffer cache.
2342 *
2343 * If this check ever becomes a bottleneck it may be better to
2344 * move it into the else, when gbincore() fails. At the moment
2345 * it isn't a problem.
2346 *
2347 * XXX remove if 0 sections (clean this up after its proven)
2348 */
2349 if (numfreebuffers == 0) {
2350 if (curthread == PCPU_GET(idlethread))
2351 return NULL;
2352 mtx_lock(&nblock);
2353 needsbuffer |= VFS_BIO_NEED_ANY;
2354 mtx_unlock(&nblock);
2355 }
2356
2357 VI_LOCK(vp);
2358 bp = gbincore(bo, blkno);
2359 if (bp != NULL) {
2360 int lockflags;
2361 /*
2362 * Buffer is in-core. If the buffer is not busy, it must
2363 * be on a queue.
2364 */
2365 lockflags = LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK;
2366
2367 if (flags & GB_LOCK_NOWAIT)
2368 lockflags |= LK_NOWAIT;
2369
2370 error = BUF_TIMELOCK(bp, lockflags,
2371 VI_MTX(vp), "getblk", slpflag, slptimeo);
2372
2373 /*
2374 * If we slept and got the lock we have to restart in case
2375 * the buffer changed identities.
2376 */
2377 if (error == ENOLCK)
2378 goto loop;
2379 /* We timed out or were interrupted. */
2380 else if (error)
2381 return (NULL);
2382
2383 /*
2384 * The buffer is locked. B_CACHE is cleared if the buffer is
2385 * invalid. Otherwise, for a non-VMIO buffer, B_CACHE is set
2386 * and for a VMIO buffer B_CACHE is adjusted according to the
2387 * backing VM cache.
2388 */
2389 if (bp->b_flags & B_INVAL)
2390 bp->b_flags &= ~B_CACHE;
2391 else if ((bp->b_flags & (B_VMIO | B_INVAL)) == 0)
2392 bp->b_flags |= B_CACHE;
2393 bremfree(bp);
2394
2395 /*
2396 * check for size inconsistancies for non-VMIO case.
2397 */
2398
2399 if (bp->b_bcount != size) {
2400 if ((bp->b_flags & B_VMIO) == 0 ||
2401 (size > bp->b_kvasize)) {
2402 if (bp->b_flags & B_DELWRI) {
2403 bp->b_flags |= B_NOCACHE;
2404 bwrite(bp);
2405 } else {
2406 if (LIST_FIRST(&bp->b_dep) == NULL) {
2407 bp->b_flags |= B_RELBUF;
2408 brelse(bp);
2409 } else {
2410 bp->b_flags |= B_NOCACHE;
2411 bwrite(bp);
2412 }
2413 }
2414 goto loop;
2415 }
2416 }
2417
2418 /*
2419 * If the size is inconsistant in the VMIO case, we can resize
2420 * the buffer. This might lead to B_CACHE getting set or
2421 * cleared. If the size has not changed, B_CACHE remains
2422 * unchanged from its previous state.
2423 */
2424
2425 if (bp->b_bcount != size)
2426 allocbuf(bp, size);
2427
2428 KASSERT(bp->b_offset != NOOFFSET,
2429 ("getblk: no buffer offset"));
2430
2431 /*
2432 * A buffer with B_DELWRI set and B_CACHE clear must
2433 * be committed before we can return the buffer in
2434 * order to prevent the caller from issuing a read
2435 * ( due to B_CACHE not being set ) and overwriting
2436 * it.
2437 *
2438 * Most callers, including NFS and FFS, need this to
2439 * operate properly either because they assume they
2440 * can issue a read if B_CACHE is not set, or because
2441 * ( for example ) an uncached B_DELWRI might loop due
2442 * to softupdates re-dirtying the buffer. In the latter
2443 * case, B_CACHE is set after the first write completes,
2444 * preventing further loops.
2445 * NOTE! b*write() sets B_CACHE. If we cleared B_CACHE
2446 * above while extending the buffer, we cannot allow the
2447 * buffer to remain with B_CACHE set after the write
2448 * completes or it will represent a corrupt state. To
2449 * deal with this we set B_NOCACHE to scrap the buffer
2450 * after the write.
2451 *
2452 * We might be able to do something fancy, like setting
2453 * B_CACHE in bwrite() except if B_DELWRI is already set,
2454 * so the below call doesn't set B_CACHE, but that gets real
2455 * confusing. This is much easier.
2456 */
2457
2458 if ((bp->b_flags & (B_CACHE|B_DELWRI)) == B_DELWRI) {
2459 bp->b_flags |= B_NOCACHE;
2460 bwrite(bp);
2461 goto loop;
2462 }
2463 bp->b_flags &= ~B_DONE;
2464 } else {
2465 int bsize, maxsize, vmio;
2466 off_t offset;
2467
2468 /*
2469 * Buffer is not in-core, create new buffer. The buffer
2470 * returned by getnewbuf() is locked. Note that the returned
2471 * buffer is also considered valid (not marked B_INVAL).
2472 */
2473 VI_UNLOCK(vp);
2474 /*
2475 * If the user does not want us to create the buffer, bail out
2476 * here.
2477 */
2478 if (flags & GB_NOCREAT)
2479 return NULL;
2480 bsize = bo->bo_bsize;
2481 offset = blkno * bsize;
2482 vmio = vp->v_object != NULL;
2483 maxsize = vmio ? size + (offset & PAGE_MASK) : size;
2484 maxsize = imax(maxsize, bsize);
2485
2486 bp = getnewbuf(slpflag, slptimeo, size, maxsize);
2487 if (bp == NULL) {
2488 if (slpflag || slptimeo)
2489 return NULL;
2490 goto loop;
2491 }
2492
2493 /*
2494 * This code is used to make sure that a buffer is not
2495 * created while the getnewbuf routine is blocked.
2496 * This can be a problem whether the vnode is locked or not.
2497 * If the buffer is created out from under us, we have to
2498 * throw away the one we just created.
2499 *
2500 * Note: this must occur before we associate the buffer
2501 * with the vp especially considering limitations in
2502 * the splay tree implementation when dealing with duplicate
2503 * lblkno's.
2504 */
2505 BO_LOCK(bo);
2506 if (gbincore(bo, blkno)) {
2507 BO_UNLOCK(bo);
2508 bp->b_flags |= B_INVAL;
2509 brelse(bp);
2510 goto loop;
2511 }
2512
2513 /*
2514 * Insert the buffer into the hash, so that it can
2515 * be found by incore.
2516 */
2517 bp->b_blkno = bp->b_lblkno = blkno;
2518 bp->b_offset = offset;
2519
2520 bgetvp(vp, bp);
2521 BO_UNLOCK(bo);
2522
2523 /*
2524 * set B_VMIO bit. allocbuf() the buffer bigger. Since the
2525 * buffer size starts out as 0, B_CACHE will be set by
2526 * allocbuf() for the VMIO case prior to it testing the
2527 * backing store for validity.
2528 */
2529
2530 if (vmio) {
2531 bp->b_flags |= B_VMIO;
2532 #if defined(VFS_BIO_DEBUG)
2533 if (vn_canvmio(vp) != TRUE)
2534 printf("getblk: VMIO on vnode type %d\n",
2535 vp->v_type);
2536 #endif
2537 KASSERT(vp->v_object == bp->b_bufobj->bo_object,
2538 ("ARGH! different b_bufobj->bo_object %p %p %p\n",
2539 bp, vp->v_object, bp->b_bufobj->bo_object));
2540 } else {
2541 bp->b_flags &= ~B_VMIO;
2542 KASSERT(bp->b_bufobj->bo_object == NULL,
2543 ("ARGH! has b_bufobj->bo_object %p %p\n",
2544 bp, bp->b_bufobj->bo_object));
2545 }
2546
2547 allocbuf(bp, size);
2548 bp->b_flags &= ~B_DONE;
2549 }
2550 CTR4(KTR_BUF, "getblk(%p, %ld, %d) = %p", vp, (long)blkno, size, bp);
2551 KASSERT(BUF_REFCNT(bp) == 1, ("getblk: bp %p not locked",bp));
2552 KASSERT(bp->b_bufobj == bo,
2553 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
2554 return (bp);
2555 }
2556
2557 /*
2558 * Get an empty, disassociated buffer of given size. The buffer is initially
2559 * set to B_INVAL.
2560 */
2561 struct buf *
2562 geteblk(int size)
2563 {
2564 struct buf *bp;
2565 int maxsize;
2566
2567 maxsize = (size + BKVAMASK) & ~BKVAMASK;
2568 while ((bp = getnewbuf(0, 0, size, maxsize)) == 0)
2569 continue;
2570 allocbuf(bp, size);
2571 bp->b_flags |= B_INVAL; /* b_dep cleared by getnewbuf() */
2572 KASSERT(BUF_REFCNT(bp) == 1, ("geteblk: bp %p not locked",bp));
2573 return (bp);
2574 }
2575
2576
2577 /*
2578 * This code constitutes the buffer memory from either anonymous system
2579 * memory (in the case of non-VMIO operations) or from an associated
2580 * VM object (in the case of VMIO operations). This code is able to
2581 * resize a buffer up or down.
2582 *
2583 * Note that this code is tricky, and has many complications to resolve
2584 * deadlock or inconsistant data situations. Tread lightly!!!
2585 * There are B_CACHE and B_DELWRI interactions that must be dealt with by
2586 * the caller. Calling this code willy nilly can result in the loss of data.
2587 *
2588 * allocbuf() only adjusts B_CACHE for VMIO buffers. getblk() deals with
2589 * B_CACHE for the non-VMIO case.
2590 */
2591
2592 int
2593 allocbuf(struct buf *bp, int size)
2594 {
2595 int newbsize, mbsize;
2596 int i;
2597
2598 if (BUF_REFCNT(bp) == 0)
2599 panic("allocbuf: buffer not busy");
2600
2601 if (bp->b_kvasize < size)
2602 panic("allocbuf: buffer too small");
2603
2604 if ((bp->b_flags & B_VMIO) == 0) {
2605 caddr_t origbuf;
2606 int origbufsize;
2607 /*
2608 * Just get anonymous memory from the kernel. Don't
2609 * mess with B_CACHE.
2610 */
2611 mbsize = (size + DEV_BSIZE - 1) & ~(DEV_BSIZE - 1);
2612 if (bp->b_flags & B_MALLOC)
2613 newbsize = mbsize;
2614 else
2615 newbsize = round_page(size);
2616
2617 if (newbsize < bp->b_bufsize) {
2618 /*
2619 * malloced buffers are not shrunk
2620 */
2621 if (bp->b_flags & B_MALLOC) {
2622 if (newbsize) {
2623 bp->b_bcount = size;
2624 } else {
2625 free(bp->b_data, M_BIOBUF);
2626 if (bp->b_bufsize) {
2627 atomic_subtract_int(
2628 &bufmallocspace,
2629 bp->b_bufsize);
2630 bufspacewakeup();
2631 bp->b_bufsize = 0;
2632 }
2633 bp->b_saveaddr = bp->b_kvabase;
2634 bp->b_data = bp->b_saveaddr;
2635 bp->b_bcount = 0;
2636 bp->b_flags &= ~B_MALLOC;
2637 }
2638 return 1;
2639 }
2640 vm_hold_free_pages(
2641 bp,
2642 (vm_offset_t) bp->b_data + newbsize,
2643 (vm_offset_t) bp->b_data + bp->b_bufsize);
2644 } else if (newbsize > bp->b_bufsize) {
2645 /*
2646 * We only use malloced memory on the first allocation.
2647 * and revert to page-allocated memory when the buffer
2648 * grows.
2649 */
2650 /*
2651 * There is a potential smp race here that could lead
2652 * to bufmallocspace slightly passing the max. It
2653 * is probably extremely rare and not worth worrying
2654 * over.
2655 */
2656 if ( (bufmallocspace < maxbufmallocspace) &&
2657 (bp->b_bufsize == 0) &&
2658 (mbsize <= PAGE_SIZE/2)) {
2659
2660 bp->b_data = malloc(mbsize, M_BIOBUF, M_WAITOK);
2661 bp->b_bufsize = mbsize;
2662 bp->b_bcount = size;
2663 bp->b_flags |= B_MALLOC;
2664 atomic_add_int(&bufmallocspace, mbsize);
2665 return 1;
2666 }
2667 origbuf = NULL;
2668 origbufsize = 0;
2669 /*
2670 * If the buffer is growing on its other-than-first allocation,
2671 * then we revert to the page-allocation scheme.
2672 */
2673 if (bp->b_flags & B_MALLOC) {
2674 origbuf = bp->b_data;
2675 origbufsize = bp->b_bufsize;
2676 bp->b_data = bp->b_kvabase;
2677 if (bp->b_bufsize) {
2678 atomic_subtract_int(&bufmallocspace,
2679 bp->b_bufsize);
2680 bufspacewakeup();
2681 bp->b_bufsize = 0;
2682 }
2683 bp->b_flags &= ~B_MALLOC;
2684 newbsize = round_page(newbsize);
2685 }
2686 vm_hold_load_pages(
2687 bp,
2688 (vm_offset_t) bp->b_data + bp->b_bufsize,
2689 (vm_offset_t) bp->b_data + newbsize);
2690 if (origbuf) {
2691 bcopy(origbuf, bp->b_data, origbufsize);
2692 free(origbuf, M_BIOBUF);
2693 }
2694 }
2695 } else {
2696 int desiredpages;
2697
2698 newbsize = (size + DEV_BSIZE - 1) & ~(DEV_BSIZE - 1);
2699 desiredpages = (size == 0) ? 0 :
2700 num_pages((bp->b_offset & PAGE_MASK) + newbsize);
2701
2702 if (bp->b_flags & B_MALLOC)
2703 panic("allocbuf: VMIO buffer can't be malloced");
2704 /*
2705 * Set B_CACHE initially if buffer is 0 length or will become
2706 * 0-length.
2707 */
2708 if (size == 0 || bp->b_bufsize == 0)
2709 bp->b_flags |= B_CACHE;
2710
2711 if (newbsize < bp->b_bufsize) {
2712 /*
2713 * DEV_BSIZE aligned new buffer size is less then the
2714 * DEV_BSIZE aligned existing buffer size. Figure out
2715 * if we have to remove any pages.
2716 */
2717 if (desiredpages < bp->b_npages) {
2718 vm_page_t m;
2719
2720 VM_OBJECT_LOCK(bp->b_bufobj->bo_object);
2721 vm_page_lock_queues();
2722 for (i = desiredpages; i < bp->b_npages; i++) {
2723 /*
2724 * the page is not freed here -- it
2725 * is the responsibility of
2726 * vnode_pager_setsize
2727 */
2728 m = bp->b_pages[i];
2729 KASSERT(m != bogus_page,
2730 ("allocbuf: bogus page found"));
2731 while (vm_page_sleep_if_busy(m, TRUE, "biodep"))
2732 vm_page_lock_queues();
2733
2734 bp->b_pages[i] = NULL;
2735 vm_page_unwire(m, 0);
2736 }
2737 vm_page_unlock_queues();
2738 VM_OBJECT_UNLOCK(bp->b_bufobj->bo_object);
2739 pmap_qremove((vm_offset_t) trunc_page((vm_offset_t)bp->b_data) +
2740 (desiredpages << PAGE_SHIFT), (bp->b_npages - desiredpages));
2741 bp->b_npages = desiredpages;
2742 }
2743 } else if (size > bp->b_bcount) {
2744 /*
2745 * We are growing the buffer, possibly in a
2746 * byte-granular fashion.
2747 */
2748 struct vnode *vp;
2749 vm_object_t obj;
2750 vm_offset_t toff;
2751 vm_offset_t tinc;
2752
2753 /*
2754 * Step 1, bring in the VM pages from the object,
2755 * allocating them if necessary. We must clear
2756 * B_CACHE if these pages are not valid for the
2757 * range covered by the buffer.
2758 */
2759
2760 vp = bp->b_vp;
2761 obj = bp->b_bufobj->bo_object;
2762
2763 VM_OBJECT_LOCK(obj);
2764 while (bp->b_npages < desiredpages) {
2765 vm_page_t m;
2766 vm_pindex_t pi;
2767
2768 pi = OFF_TO_IDX(bp->b_offset) + bp->b_npages;
2769 if ((m = vm_page_lookup(obj, pi)) == NULL) {
2770 /*
2771 * note: must allocate system pages
2772 * since blocking here could intefere
2773 * with paging I/O, no matter which
2774 * process we are.
2775 */
2776 m = vm_page_alloc(obj, pi,
2777 VM_ALLOC_NOBUSY | VM_ALLOC_SYSTEM |
2778 VM_ALLOC_WIRED);
2779 if (m == NULL) {
2780 atomic_add_int(&vm_pageout_deficit,
2781 desiredpages - bp->b_npages);
2782 VM_OBJECT_UNLOCK(obj);
2783 VM_WAIT;
2784 VM_OBJECT_LOCK(obj);
2785 } else {
2786 bp->b_flags &= ~B_CACHE;
2787 bp->b_pages[bp->b_npages] = m;
2788 ++bp->b_npages;
2789 }
2790 continue;
2791 }
2792
2793 /*
2794 * We found a page. If we have to sleep on it,
2795 * retry because it might have gotten freed out
2796 * from under us.
2797 *
2798 * We can only test PG_BUSY here. Blocking on
2799 * m->busy might lead to a deadlock:
2800 *
2801 * vm_fault->getpages->cluster_read->allocbuf
2802 *
2803 */
2804 vm_page_lock_queues();
2805 if (vm_page_sleep_if_busy(m, FALSE, "pgtblk"))
2806 continue;
2807
2808 /*
2809 * We have a good page. Should we wakeup the
2810 * page daemon?
2811 */
2812 if ((curproc != pageproc) &&
2813 ((m->queue - m->pc) == PQ_CACHE) &&
2814 ((cnt.v_free_count + cnt.v_cache_count) <
2815 (cnt.v_free_min + cnt.v_cache_min))) {
2816 pagedaemon_wakeup();
2817 }
2818 vm_page_wire(m);
2819 vm_page_unlock_queues();
2820 bp->b_pages[bp->b_npages] = m;
2821 ++bp->b_npages;
2822 }
2823
2824 /*
2825 * Step 2. We've loaded the pages into the buffer,
2826 * we have to figure out if we can still have B_CACHE
2827 * set. Note that B_CACHE is set according to the
2828 * byte-granular range ( bcount and size ), new the
2829 * aligned range ( newbsize ).
2830 *
2831 * The VM test is against m->valid, which is DEV_BSIZE
2832 * aligned. Needless to say, the validity of the data
2833 * needs to also be DEV_BSIZE aligned. Note that this
2834 * fails with NFS if the server or some other client
2835 * extends the file's EOF. If our buffer is resized,
2836 * B_CACHE may remain set! XXX
2837 */
2838
2839 toff = bp->b_bcount;
2840 tinc = PAGE_SIZE - ((bp->b_offset + toff) & PAGE_MASK);
2841
2842 while ((bp->b_flags & B_CACHE) && toff < size) {
2843 vm_pindex_t pi;
2844
2845 if (tinc > (size - toff))
2846 tinc = size - toff;
2847
2848 pi = ((bp->b_offset & PAGE_MASK) + toff) >>
2849 PAGE_SHIFT;
2850
2851 vfs_buf_test_cache(
2852 bp,
2853 bp->b_offset,
2854 toff,
2855 tinc,
2856 bp->b_pages[pi]
2857 );
2858 toff += tinc;
2859 tinc = PAGE_SIZE;
2860 }
2861 VM_OBJECT_UNLOCK(obj);
2862
2863 /*
2864 * Step 3, fixup the KVM pmap. Remember that
2865 * bp->b_data is relative to bp->b_offset, but
2866 * bp->b_offset may be offset into the first page.
2867 */
2868
2869 bp->b_data = (caddr_t)
2870 trunc_page((vm_offset_t)bp->b_data);
2871 pmap_qenter(
2872 (vm_offset_t)bp->b_data,
2873 bp->b_pages,
2874 bp->b_npages
2875 );
2876
2877 bp->b_data = (caddr_t)((vm_offset_t)bp->b_data |
2878 (vm_offset_t)(bp->b_offset & PAGE_MASK));
2879 }
2880 }
2881 if (newbsize < bp->b_bufsize)
2882 bufspacewakeup();
2883 bp->b_bufsize = newbsize; /* actual buffer allocation */
2884 bp->b_bcount = size; /* requested buffer size */
2885 return 1;
2886 }
2887
2888 void
2889 biodone(struct bio *bp)
2890 {
2891 void (*done)(struct bio *);
2892
2893 mtx_lock(&bdonelock);
2894 bp->bio_flags |= BIO_DONE;
2895 done = bp->bio_done;
2896 if (done == NULL)
2897 wakeup(bp);
2898 mtx_unlock(&bdonelock);
2899 if (done != NULL)
2900 done(bp);
2901 }
2902
2903 /*
2904 * Wait for a BIO to finish.
2905 *
2906 * XXX: resort to a timeout for now. The optimal locking (if any) for this
2907 * case is not yet clear.
2908 */
2909 int
2910 biowait(struct bio *bp, const char *wchan)
2911 {
2912
2913 mtx_lock(&bdonelock);
2914 while ((bp->bio_flags & BIO_DONE) == 0)
2915 msleep(bp, &bdonelock, PRIBIO, wchan, hz / 10);
2916 mtx_unlock(&bdonelock);
2917 if (bp->bio_error != 0)
2918 return (bp->bio_error);
2919 if (!(bp->bio_flags & BIO_ERROR))
2920 return (0);
2921 return (EIO);
2922 }
2923
2924 void
2925 biofinish(struct bio *bp, struct devstat *stat, int error)
2926 {
2927
2928 if (error) {
2929 bp->bio_error = error;
2930 bp->bio_flags |= BIO_ERROR;
2931 }
2932 if (stat != NULL)
2933 devstat_end_transaction_bio(stat, bp);
2934 biodone(bp);
2935 }
2936
2937 /*
2938 * bufwait:
2939 *
2940 * Wait for buffer I/O completion, returning error status. The buffer
2941 * is left locked and B_DONE on return. B_EINTR is converted into an EINTR
2942 * error and cleared.
2943 */
2944 int
2945 bufwait(struct buf *bp)
2946 {
2947 if (bp->b_iocmd == BIO_READ)
2948 bwait(bp, PRIBIO, "biord");
2949 else
2950 bwait(bp, PRIBIO, "biowr");
2951 if (bp->b_flags & B_EINTR) {
2952 bp->b_flags &= ~B_EINTR;
2953 return (EINTR);
2954 }
2955 if (bp->b_ioflags & BIO_ERROR) {
2956 return (bp->b_error ? bp->b_error : EIO);
2957 } else {
2958 return (0);
2959 }
2960 }
2961
2962 /*
2963 * Call back function from struct bio back up to struct buf.
2964 */
2965 static void
2966 bufdonebio(struct bio *bip)
2967 {
2968 struct buf *bp;
2969
2970 bp = bip->bio_caller2;
2971 bp->b_resid = bp->b_bcount - bip->bio_completed;
2972 bp->b_resid = bip->bio_resid; /* XXX: remove */
2973 bp->b_ioflags = bip->bio_flags;
2974 bp->b_error = bip->bio_error;
2975 if (bp->b_error)
2976 bp->b_ioflags |= BIO_ERROR;
2977 bufdone(bp);
2978 g_destroy_bio(bip);
2979 }
2980
2981 void
2982 dev_strategy(struct cdev *dev, struct buf *bp)
2983 {
2984 struct cdevsw *csw;
2985 struct bio *bip;
2986
2987 if ((!bp->b_iocmd) || (bp->b_iocmd & (bp->b_iocmd - 1)))
2988 panic("b_iocmd botch");
2989 for (;;) {
2990 bip = g_new_bio();
2991 if (bip != NULL)
2992 break;
2993 /* Try again later */
2994 tsleep(&bp, PRIBIO, "dev_strat", hz/10);
2995 }
2996 bip->bio_cmd = bp->b_iocmd;
2997 bip->bio_offset = bp->b_iooffset;
2998 bip->bio_length = bp->b_bcount;
2999 bip->bio_bcount = bp->b_bcount; /* XXX: remove */
3000 bip->bio_data = bp->b_data;
3001 bip->bio_done = bufdonebio;
3002 bip->bio_caller2 = bp;
3003 bip->bio_dev = dev;
3004 KASSERT(dev->si_refcount > 0,
3005 ("dev_strategy on un-referenced struct cdev *(%s)",
3006 devtoname(dev)));
3007 csw = dev_refthread(dev);
3008 if (csw == NULL) {
3009 bp->b_error = ENXIO;
3010 bp->b_ioflags = BIO_ERROR;
3011 bufdone(bp);
3012 return;
3013 }
3014 (*csw->d_strategy)(bip);
3015 dev_relthread(dev);
3016 }
3017
3018 /*
3019 * bufdone:
3020 *
3021 * Finish I/O on a buffer, optionally calling a completion function.
3022 * This is usually called from an interrupt so process blocking is
3023 * not allowed.
3024 *
3025 * biodone is also responsible for setting B_CACHE in a B_VMIO bp.
3026 * In a non-VMIO bp, B_CACHE will be set on the next getblk()
3027 * assuming B_INVAL is clear.
3028 *
3029 * For the VMIO case, we set B_CACHE if the op was a read and no
3030 * read error occured, or if the op was a write. B_CACHE is never
3031 * set if the buffer is invalid or otherwise uncacheable.
3032 *
3033 * biodone does not mess with B_INVAL, allowing the I/O routine or the
3034 * initiator to leave B_INVAL set to brelse the buffer out of existance
3035 * in the biodone routine.
3036 */
3037 void
3038 bufdone(struct buf *bp)
3039 {
3040 struct bufobj *dropobj;
3041 void (*biodone)(struct buf *);
3042
3043
3044 CTR3(KTR_BUF, "bufdone(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
3045 dropobj = NULL;
3046
3047 KASSERT(BUF_REFCNT(bp) > 0, ("biodone: bp %p not busy %d", bp, BUF_REFCNT(bp)));
3048 KASSERT(!(bp->b_flags & B_DONE), ("biodone: bp %p already done", bp));
3049
3050 runningbufwakeup(bp);
3051 if (bp->b_iocmd == BIO_WRITE)
3052 dropobj = bp->b_bufobj;
3053 /* call optional completion function if requested */
3054 if (bp->b_iodone != NULL) {
3055 biodone = bp->b_iodone;
3056 bp->b_iodone = NULL;
3057 (*biodone) (bp);
3058 if (dropobj)
3059 bufobj_wdrop(dropobj);
3060 return;
3061 }
3062 if (LIST_FIRST(&bp->b_dep) != NULL)
3063 buf_complete(bp);
3064
3065 if (bp->b_flags & B_VMIO) {
3066 int i;
3067 vm_ooffset_t foff;
3068 vm_page_t m;
3069 vm_object_t obj;
3070 int iosize;
3071 struct vnode *vp = bp->b_vp;
3072
3073 obj = bp->b_bufobj->bo_object;
3074
3075 #if defined(VFS_BIO_DEBUG)
3076 mp_fixme("usecount and vflag accessed without locks.");
3077 if (vp->v_usecount == 0) {
3078 panic("biodone: zero vnode ref count");
3079 }
3080
3081 KASSERT(vp->v_object != NULL,
3082 ("biodone: vnode %p has no vm_object", vp));
3083 #endif
3084
3085 foff = bp->b_offset;
3086 KASSERT(bp->b_offset != NOOFFSET,
3087 ("biodone: no buffer offset"));
3088
3089 VM_OBJECT_LOCK(obj);
3090 #if defined(VFS_BIO_DEBUG)
3091 if (obj->paging_in_progress < bp->b_npages) {
3092 printf("biodone: paging in progress(%d) < bp->b_npages(%d)\n",
3093 obj->paging_in_progress, bp->b_npages);
3094 }
3095 #endif
3096
3097 /*
3098 * Set B_CACHE if the op was a normal read and no error
3099 * occured. B_CACHE is set for writes in the b*write()
3100 * routines.
3101 */
3102 iosize = bp->b_bcount - bp->b_resid;
3103 if (bp->b_iocmd == BIO_READ &&
3104 !(bp->b_flags & (B_INVAL|B_NOCACHE)) &&
3105 !(bp->b_ioflags & BIO_ERROR)) {
3106 bp->b_flags |= B_CACHE;
3107 }
3108 vm_page_lock_queues();
3109 for (i = 0; i < bp->b_npages; i++) {
3110 int bogusflag = 0;
3111 int resid;
3112
3113 resid = ((foff + PAGE_SIZE) & ~(off_t)PAGE_MASK) - foff;
3114 if (resid > iosize)
3115 resid = iosize;
3116
3117 /*
3118 * cleanup bogus pages, restoring the originals
3119 */
3120 m = bp->b_pages[i];
3121 if (m == bogus_page) {
3122 bogusflag = 1;
3123 m = vm_page_lookup(obj, OFF_TO_IDX(foff));
3124 if (m == NULL)
3125 panic("biodone: page disappeared!");
3126 bp->b_pages[i] = m;
3127 pmap_qenter(trunc_page((vm_offset_t)bp->b_data), bp->b_pages, bp->b_npages);
3128 }
3129 #if defined(VFS_BIO_DEBUG)
3130 if (OFF_TO_IDX(foff) != m->pindex) {
3131 printf(
3132 "biodone: foff(%jd)/m->pindex(%ju) mismatch\n",
3133 (intmax_t)foff, (uintmax_t)m->pindex);
3134 }
3135 #endif
3136
3137 /*
3138 * In the write case, the valid and clean bits are
3139 * already changed correctly ( see bdwrite() ), so we
3140 * only need to do this here in the read case.
3141 */
3142 if ((bp->b_iocmd == BIO_READ) && !bogusflag && resid > 0) {
3143 vfs_page_set_valid(bp, foff, i, m);
3144 }
3145
3146 /*
3147 * when debugging new filesystems or buffer I/O methods, this
3148 * is the most common error that pops up. if you see this, you
3149 * have not set the page busy flag correctly!!!
3150 */
3151 if (m->busy == 0) {
3152 printf("biodone: page busy < 0, "
3153 "pindex: %d, foff: 0x(%x,%x), "
3154 "resid: %d, index: %d\n",
3155 (int) m->pindex, (int)(foff >> 32),
3156 (int) foff & 0xffffffff, resid, i);
3157 if (!vn_isdisk(vp, NULL))
3158 printf(" iosize: %jd, lblkno: %jd, flags: 0x%x, npages: %d\n",
3159 (intmax_t)bp->b_vp->v_mount->mnt_stat.f_iosize,
3160 (intmax_t) bp->b_lblkno,
3161 bp->b_flags, bp->b_npages);
3162 else
3163 printf(" VDEV, lblkno: %jd, flags: 0x%x, npages: %d\n",
3164 (intmax_t) bp->b_lblkno,
3165 bp->b_flags, bp->b_npages);
3166 printf(" valid: 0x%lx, dirty: 0x%lx, wired: %d\n",
3167 (u_long)m->valid, (u_long)m->dirty,
3168 m->wire_count);
3169 panic("biodone: page busy < 0\n");
3170 }
3171 vm_page_io_finish(m);
3172 vm_object_pip_subtract(obj, 1);
3173 foff = (foff + PAGE_SIZE) & ~(off_t)PAGE_MASK;
3174 iosize -= resid;
3175 }
3176 vm_page_unlock_queues();
3177 vm_object_pip_wakeupn(obj, 0);
3178 VM_OBJECT_UNLOCK(obj);
3179 }
3180
3181 /*
3182 * For asynchronous completions, release the buffer now. The brelse
3183 * will do a wakeup there if necessary - so no need to do a wakeup
3184 * here in the async case. The sync case always needs to do a wakeup.
3185 */
3186
3187 if (bp->b_flags & B_ASYNC) {
3188 if ((bp->b_flags & (B_NOCACHE | B_INVAL | B_RELBUF)) || (bp->b_ioflags & BIO_ERROR))
3189 brelse(bp);
3190 else
3191 bqrelse(bp);
3192 } else
3193 bdone(bp);
3194 if (dropobj)
3195 bufobj_wdrop(dropobj);
3196 }
3197
3198 /*
3199 * This routine is called in lieu of iodone in the case of
3200 * incomplete I/O. This keeps the busy status for pages
3201 * consistant.
3202 */
3203 void
3204 vfs_unbusy_pages(struct buf *bp)
3205 {
3206 int i;
3207 vm_object_t obj;
3208 vm_page_t m;
3209
3210 runningbufwakeup(bp);
3211 if (!(bp->b_flags & B_VMIO))
3212 return;
3213
3214 obj = bp->b_bufobj->bo_object;
3215 VM_OBJECT_LOCK(obj);
3216 vm_page_lock_queues();
3217 for (i = 0; i < bp->b_npages; i++) {
3218 m = bp->b_pages[i];
3219 if (m == bogus_page) {
3220 m = vm_page_lookup(obj, OFF_TO_IDX(bp->b_offset) + i);
3221 if (!m)
3222 panic("vfs_unbusy_pages: page missing\n");
3223 bp->b_pages[i] = m;
3224 pmap_qenter(trunc_page((vm_offset_t)bp->b_data),
3225 bp->b_pages, bp->b_npages);
3226 }
3227 vm_object_pip_subtract(obj, 1);
3228 vm_page_io_finish(m);
3229 }
3230 vm_page_unlock_queues();
3231 vm_object_pip_wakeupn(obj, 0);
3232 VM_OBJECT_UNLOCK(obj);
3233 }
3234
3235 /*
3236 * vfs_page_set_valid:
3237 *
3238 * Set the valid bits in a page based on the supplied offset. The
3239 * range is restricted to the buffer's size.
3240 *
3241 * This routine is typically called after a read completes.
3242 */
3243 static void
3244 vfs_page_set_valid(struct buf *bp, vm_ooffset_t off, int pageno, vm_page_t m)
3245 {
3246 vm_ooffset_t soff, eoff;
3247
3248 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
3249 /*
3250 * Start and end offsets in buffer. eoff - soff may not cross a
3251 * page boundry or cross the end of the buffer. The end of the
3252 * buffer, in this case, is our file EOF, not the allocation size
3253 * of the buffer.
3254 */
3255 soff = off;
3256 eoff = (off + PAGE_SIZE) & ~(off_t)PAGE_MASK;
3257 if (eoff > bp->b_offset + bp->b_bcount)
3258 eoff = bp->b_offset + bp->b_bcount;
3259
3260 /*
3261 * Set valid range. This is typically the entire buffer and thus the
3262 * entire page.
3263 */
3264 if (eoff > soff) {
3265 vm_page_set_validclean(
3266 m,
3267 (vm_offset_t) (soff & PAGE_MASK),
3268 (vm_offset_t) (eoff - soff)
3269 );
3270 }
3271 }
3272
3273 /*
3274 * This routine is called before a device strategy routine.
3275 * It is used to tell the VM system that paging I/O is in
3276 * progress, and treat the pages associated with the buffer
3277 * almost as being PG_BUSY. Also the object paging_in_progress
3278 * flag is handled to make sure that the object doesn't become
3279 * inconsistant.
3280 *
3281 * Since I/O has not been initiated yet, certain buffer flags
3282 * such as BIO_ERROR or B_INVAL may be in an inconsistant state
3283 * and should be ignored.
3284 */
3285 void
3286 vfs_busy_pages(struct buf *bp, int clear_modify)
3287 {
3288 int i, bogus;
3289 vm_object_t obj;
3290 vm_ooffset_t foff;
3291 vm_page_t m;
3292
3293 if (!(bp->b_flags & B_VMIO))
3294 return;
3295
3296 obj = bp->b_bufobj->bo_object;
3297 foff = bp->b_offset;
3298 KASSERT(bp->b_offset != NOOFFSET,
3299 ("vfs_busy_pages: no buffer offset"));
3300 vfs_setdirty(bp);
3301 VM_OBJECT_LOCK(obj);
3302 retry:
3303 vm_page_lock_queues();
3304 for (i = 0; i < bp->b_npages; i++) {
3305 m = bp->b_pages[i];
3306
3307 if (vm_page_sleep_if_busy(m, FALSE, "vbpage"))
3308 goto retry;
3309 }
3310 bogus = 0;
3311 for (i = 0; i < bp->b_npages; i++) {
3312 m = bp->b_pages[i];
3313
3314 if ((bp->b_flags & B_CLUSTER) == 0) {
3315 vm_object_pip_add(obj, 1);
3316 vm_page_io_start(m);
3317 }
3318 /*
3319 * When readying a buffer for a read ( i.e
3320 * clear_modify == 0 ), it is important to do
3321 * bogus_page replacement for valid pages in
3322 * partially instantiated buffers. Partially
3323 * instantiated buffers can, in turn, occur when
3324 * reconstituting a buffer from its VM backing store
3325 * base. We only have to do this if B_CACHE is
3326 * clear ( which causes the I/O to occur in the
3327 * first place ). The replacement prevents the read
3328 * I/O from overwriting potentially dirty VM-backed
3329 * pages. XXX bogus page replacement is, uh, bogus.
3330 * It may not work properly with small-block devices.
3331 * We need to find a better way.
3332 */
3333 pmap_remove_all(m);
3334 if (clear_modify)
3335 vfs_page_set_valid(bp, foff, i, m);
3336 else if (m->valid == VM_PAGE_BITS_ALL &&
3337 (bp->b_flags & B_CACHE) == 0) {
3338 bp->b_pages[i] = bogus_page;
3339 bogus++;
3340 }
3341 foff = (foff + PAGE_SIZE) & ~(off_t)PAGE_MASK;
3342 }
3343 vm_page_unlock_queues();
3344 VM_OBJECT_UNLOCK(obj);
3345 if (bogus)
3346 pmap_qenter(trunc_page((vm_offset_t)bp->b_data),
3347 bp->b_pages, bp->b_npages);
3348 }
3349
3350 /*
3351 * Tell the VM system that the pages associated with this buffer
3352 * are clean. This is used for delayed writes where the data is
3353 * going to go to disk eventually without additional VM intevention.
3354 *
3355 * Note that while we only really need to clean through to b_bcount, we
3356 * just go ahead and clean through to b_bufsize.
3357 */
3358 static void
3359 vfs_clean_pages(struct buf *bp)
3360 {
3361 int i;
3362 vm_ooffset_t foff, noff, eoff;
3363 vm_page_t m;
3364
3365 if (!(bp->b_flags & B_VMIO))
3366 return;
3367
3368 foff = bp->b_offset;
3369 KASSERT(bp->b_offset != NOOFFSET,
3370 ("vfs_clean_pages: no buffer offset"));
3371 VM_OBJECT_LOCK(bp->b_bufobj->bo_object);
3372 vm_page_lock_queues();
3373 for (i = 0; i < bp->b_npages; i++) {
3374 m = bp->b_pages[i];
3375 noff = (foff + PAGE_SIZE) & ~(off_t)PAGE_MASK;
3376 eoff = noff;
3377
3378 if (eoff > bp->b_offset + bp->b_bufsize)
3379 eoff = bp->b_offset + bp->b_bufsize;
3380 vfs_page_set_valid(bp, foff, i, m);
3381 /* vm_page_clear_dirty(m, foff & PAGE_MASK, eoff - foff); */
3382 foff = noff;
3383 }
3384 vm_page_unlock_queues();
3385 VM_OBJECT_UNLOCK(bp->b_bufobj->bo_object);
3386 }
3387
3388 /*
3389 * vfs_bio_set_validclean:
3390 *
3391 * Set the range within the buffer to valid and clean. The range is
3392 * relative to the beginning of the buffer, b_offset. Note that b_offset
3393 * itself may be offset from the beginning of the first page.
3394 *
3395 */
3396
3397 void
3398 vfs_bio_set_validclean(struct buf *bp, int base, int size)
3399 {
3400 int i, n;
3401 vm_page_t m;
3402
3403 if (!(bp->b_flags & B_VMIO))
3404 return;
3405 /*
3406 * Fixup base to be relative to beginning of first page.
3407 * Set initial n to be the maximum number of bytes in the
3408 * first page that can be validated.
3409 */
3410
3411 base += (bp->b_offset & PAGE_MASK);
3412 n = PAGE_SIZE - (base & PAGE_MASK);
3413
3414 VM_OBJECT_LOCK(bp->b_bufobj->bo_object);
3415 vm_page_lock_queues();
3416 for (i = base / PAGE_SIZE; size > 0 && i < bp->b_npages; ++i) {
3417 m = bp->b_pages[i];
3418 if (n > size)
3419 n = size;
3420 vm_page_set_validclean(m, base & PAGE_MASK, n);
3421 base += n;
3422 size -= n;
3423 n = PAGE_SIZE;
3424 }
3425 vm_page_unlock_queues();
3426 VM_OBJECT_UNLOCK(bp->b_bufobj->bo_object);
3427 }
3428
3429 /*
3430 * vfs_bio_clrbuf:
3431 *
3432 * clear a buffer. This routine essentially fakes an I/O, so we need
3433 * to clear BIO_ERROR and B_INVAL.
3434 *
3435 * Note that while we only theoretically need to clear through b_bcount,
3436 * we go ahead and clear through b_bufsize.
3437 */
3438
3439 void
3440 vfs_bio_clrbuf(struct buf *bp)
3441 {
3442 int i, j, mask = 0;
3443 caddr_t sa, ea;
3444
3445 if ((bp->b_flags & (B_VMIO | B_MALLOC)) != B_VMIO) {
3446 clrbuf(bp);
3447 return;
3448 }
3449
3450 bp->b_flags &= ~B_INVAL;
3451 bp->b_ioflags &= ~BIO_ERROR;
3452 VM_OBJECT_LOCK(bp->b_bufobj->bo_object);
3453 if ((bp->b_npages == 1) && (bp->b_bufsize < PAGE_SIZE) &&
3454 (bp->b_offset & PAGE_MASK) == 0) {
3455 if (bp->b_pages[0] == bogus_page)
3456 goto unlock;
3457 mask = (1 << (bp->b_bufsize / DEV_BSIZE)) - 1;
3458 VM_OBJECT_LOCK_ASSERT(bp->b_pages[0]->object, MA_OWNED);
3459 if ((bp->b_pages[0]->valid & mask) == mask)
3460 goto unlock;
3461 if (((bp->b_pages[0]->flags & PG_ZERO) == 0) &&
3462 ((bp->b_pages[0]->valid & mask) == 0)) {
3463 bzero(bp->b_data, bp->b_bufsize);
3464 bp->b_pages[0]->valid |= mask;
3465 goto unlock;
3466 }
3467 }
3468 ea = sa = bp->b_data;
3469 for(i = 0; i < bp->b_npages; i++, sa = ea) {
3470 ea = (caddr_t)trunc_page((vm_offset_t)sa + PAGE_SIZE);
3471 ea = (caddr_t)(vm_offset_t)ulmin(
3472 (u_long)(vm_offset_t)ea,
3473 (u_long)(vm_offset_t)bp->b_data + bp->b_bufsize);
3474 if (bp->b_pages[i] == bogus_page)
3475 continue;
3476 j = ((vm_offset_t)sa & PAGE_MASK) / DEV_BSIZE;
3477 mask = ((1 << ((ea - sa) / DEV_BSIZE)) - 1) << j;
3478 VM_OBJECT_LOCK_ASSERT(bp->b_pages[i]->object, MA_OWNED);
3479 if ((bp->b_pages[i]->valid & mask) == mask)
3480 continue;
3481 if ((bp->b_pages[i]->valid & mask) == 0) {
3482 if ((bp->b_pages[i]->flags & PG_ZERO) == 0)
3483 bzero(sa, ea - sa);
3484 } else {
3485 for (; sa < ea; sa += DEV_BSIZE, j++) {
3486 if (((bp->b_pages[i]->flags & PG_ZERO) == 0) &&
3487 (bp->b_pages[i]->valid & (1 << j)) == 0)
3488 bzero(sa, DEV_BSIZE);
3489 }
3490 }
3491 bp->b_pages[i]->valid |= mask;
3492 }
3493 unlock:
3494 VM_OBJECT_UNLOCK(bp->b_bufobj->bo_object);
3495 bp->b_resid = 0;
3496 }
3497
3498 /*
3499 * vm_hold_load_pages and vm_hold_free_pages get pages into
3500 * a buffers address space. The pages are anonymous and are
3501 * not associated with a file object.
3502 */
3503 static void
3504 vm_hold_load_pages(struct buf *bp, vm_offset_t from, vm_offset_t to)
3505 {
3506 vm_offset_t pg;
3507 vm_page_t p;
3508 int index;
3509
3510 to = round_page(to);
3511 from = round_page(from);
3512 index = (from - trunc_page((vm_offset_t)bp->b_data)) >> PAGE_SHIFT;
3513
3514 VM_OBJECT_LOCK(kernel_object);
3515 for (pg = from; pg < to; pg += PAGE_SIZE, index++) {
3516 tryagain:
3517 /*
3518 * note: must allocate system pages since blocking here
3519 * could intefere with paging I/O, no matter which
3520 * process we are.
3521 */
3522 p = vm_page_alloc(kernel_object,
3523 ((pg - VM_MIN_KERNEL_ADDRESS) >> PAGE_SHIFT),
3524 VM_ALLOC_NOBUSY | VM_ALLOC_SYSTEM | VM_ALLOC_WIRED);
3525 if (!p) {
3526 atomic_add_int(&vm_pageout_deficit,
3527 (to - pg) >> PAGE_SHIFT);
3528 VM_OBJECT_UNLOCK(kernel_object);
3529 VM_WAIT;
3530 VM_OBJECT_LOCK(kernel_object);
3531 goto tryagain;
3532 }
3533 p->valid = VM_PAGE_BITS_ALL;
3534 pmap_qenter(pg, &p, 1);
3535 bp->b_pages[index] = p;
3536 }
3537 VM_OBJECT_UNLOCK(kernel_object);
3538 bp->b_npages = index;
3539 }
3540
3541 /* Return pages associated with this buf to the vm system */
3542 static void
3543 vm_hold_free_pages(struct buf *bp, vm_offset_t from, vm_offset_t to)
3544 {
3545 vm_offset_t pg;
3546 vm_page_t p;
3547 int index, newnpages;
3548
3549 from = round_page(from);
3550 to = round_page(to);
3551 newnpages = index = (from - trunc_page((vm_offset_t)bp->b_data)) >> PAGE_SHIFT;
3552
3553 VM_OBJECT_LOCK(kernel_object);
3554 for (pg = from; pg < to; pg += PAGE_SIZE, index++) {
3555 p = bp->b_pages[index];
3556 if (p && (index < bp->b_npages)) {
3557 if (p->busy) {
3558 printf(
3559 "vm_hold_free_pages: blkno: %jd, lblkno: %jd\n",
3560 (intmax_t)bp->b_blkno,
3561 (intmax_t)bp->b_lblkno);
3562 }
3563 bp->b_pages[index] = NULL;
3564 pmap_qremove(pg, 1);
3565 vm_page_lock_queues();
3566 vm_page_unwire(p, 0);
3567 vm_page_free(p);
3568 vm_page_unlock_queues();
3569 }
3570 }
3571 VM_OBJECT_UNLOCK(kernel_object);
3572 bp->b_npages = newnpages;
3573 }
3574
3575 /*
3576 * Map an IO request into kernel virtual address space.
3577 *
3578 * All requests are (re)mapped into kernel VA space.
3579 * Notice that we use b_bufsize for the size of the buffer
3580 * to be mapped. b_bcount might be modified by the driver.
3581 *
3582 * Note that even if the caller determines that the address space should
3583 * be valid, a race or a smaller-file mapped into a larger space may
3584 * actually cause vmapbuf() to fail, so all callers of vmapbuf() MUST
3585 * check the return value.
3586 */
3587 int
3588 vmapbuf(struct buf *bp)
3589 {
3590 caddr_t addr, kva;
3591 vm_prot_t prot;
3592 int pidx, i;
3593 struct vm_page *m;
3594 struct pmap *pmap = &curproc->p_vmspace->vm_pmap;
3595
3596 if (bp->b_bufsize < 0)
3597 return (-1);
3598 prot = VM_PROT_READ;
3599 if (bp->b_iocmd == BIO_READ)
3600 prot |= VM_PROT_WRITE; /* Less backwards than it looks */
3601 for (addr = (caddr_t)trunc_page((vm_offset_t)bp->b_data), pidx = 0;
3602 addr < bp->b_data + bp->b_bufsize;
3603 addr += PAGE_SIZE, pidx++) {
3604 /*
3605 * Do the vm_fault if needed; do the copy-on-write thing
3606 * when reading stuff off device into memory.
3607 *
3608 * NOTE! Must use pmap_extract() because addr may be in
3609 * the userland address space, and kextract is only guarenteed
3610 * to work for the kernland address space (see: sparc64 port).
3611 */
3612 retry:
3613 if (vm_fault_quick(addr >= bp->b_data ? addr : bp->b_data,
3614 prot) < 0) {
3615 vm_page_lock_queues();
3616 for (i = 0; i < pidx; ++i) {
3617 vm_page_unhold(bp->b_pages[i]);
3618 bp->b_pages[i] = NULL;
3619 }
3620 vm_page_unlock_queues();
3621 return(-1);
3622 }
3623 m = pmap_extract_and_hold(pmap, (vm_offset_t)addr, prot);
3624 if (m == NULL)
3625 goto retry;
3626 bp->b_pages[pidx] = m;
3627 }
3628 if (pidx > btoc(MAXPHYS))
3629 panic("vmapbuf: mapped more than MAXPHYS");
3630 pmap_qenter((vm_offset_t)bp->b_saveaddr, bp->b_pages, pidx);
3631
3632 kva = bp->b_saveaddr;
3633 bp->b_npages = pidx;
3634 bp->b_saveaddr = bp->b_data;
3635 bp->b_data = kva + (((vm_offset_t) bp->b_data) & PAGE_MASK);
3636 return(0);
3637 }
3638
3639 /*
3640 * Free the io map PTEs associated with this IO operation.
3641 * We also invalidate the TLB entries and restore the original b_addr.
3642 */
3643 void
3644 vunmapbuf(struct buf *bp)
3645 {
3646 int pidx;
3647 int npages;
3648
3649 npages = bp->b_npages;
3650 pmap_qremove(trunc_page((vm_offset_t)bp->b_data), npages);
3651 vm_page_lock_queues();
3652 for (pidx = 0; pidx < npages; pidx++)
3653 vm_page_unhold(bp->b_pages[pidx]);
3654 vm_page_unlock_queues();
3655
3656 bp->b_data = bp->b_saveaddr;
3657 }
3658
3659 void
3660 bdone(struct buf *bp)
3661 {
3662
3663 mtx_lock(&bdonelock);
3664 bp->b_flags |= B_DONE;
3665 wakeup(bp);
3666 mtx_unlock(&bdonelock);
3667 }
3668
3669 void
3670 bwait(struct buf *bp, u_char pri, const char *wchan)
3671 {
3672
3673 mtx_lock(&bdonelock);
3674 while ((bp->b_flags & B_DONE) == 0)
3675 msleep(bp, &bdonelock, pri, wchan, 0);
3676 mtx_unlock(&bdonelock);
3677 }
3678
3679 int
3680 bufsync(struct bufobj *bo, int waitfor, struct thread *td)
3681 {
3682
3683 return (VOP_FSYNC(bo->__bo_vnode, waitfor, td));
3684 }
3685
3686 void
3687 bufstrategy(struct bufobj *bo, struct buf *bp)
3688 {
3689 int i = 0;
3690 struct vnode *vp;
3691
3692 vp = bp->b_vp;
3693 KASSERT(vp == bo->bo_private, ("Inconsistent vnode bufstrategy"));
3694 KASSERT(vp->v_type != VCHR && vp->v_type != VBLK,
3695 ("Wrong vnode in bufstrategy(bp=%p, vp=%p)", bp, vp));
3696 i = VOP_STRATEGY(vp, bp);
3697 KASSERT(i == 0, ("VOP_STRATEGY failed bp=%p vp=%p", bp, bp->b_vp));
3698 }
3699
3700 void
3701 bufobj_wrefl(struct bufobj *bo)
3702 {
3703
3704 KASSERT(bo != NULL, ("NULL bo in bufobj_wref"));
3705 ASSERT_BO_LOCKED(bo);
3706 bo->bo_numoutput++;
3707 }
3708
3709 void
3710 bufobj_wref(struct bufobj *bo)
3711 {
3712
3713 KASSERT(bo != NULL, ("NULL bo in bufobj_wref"));
3714 BO_LOCK(bo);
3715 bo->bo_numoutput++;
3716 BO_UNLOCK(bo);
3717 }
3718
3719 void
3720 bufobj_wdrop(struct bufobj *bo)
3721 {
3722
3723 KASSERT(bo != NULL, ("NULL bo in bufobj_wdrop"));
3724 BO_LOCK(bo);
3725 KASSERT(bo->bo_numoutput > 0, ("bufobj_wdrop non-positive count"));
3726 if ((--bo->bo_numoutput == 0) && (bo->bo_flag & BO_WWAIT)) {
3727 bo->bo_flag &= ~BO_WWAIT;
3728 wakeup(&bo->bo_numoutput);
3729 }
3730 BO_UNLOCK(bo);
3731 }
3732
3733 int
3734 bufobj_wwait(struct bufobj *bo, int slpflag, int timeo)
3735 {
3736 int error;
3737
3738 KASSERT(bo != NULL, ("NULL bo in bufobj_wwait"));
3739 ASSERT_BO_LOCKED(bo);
3740 error = 0;
3741 while (bo->bo_numoutput) {
3742 bo->bo_flag |= BO_WWAIT;
3743 error = msleep(&bo->bo_numoutput, BO_MTX(bo),
3744 slpflag | (PRIBIO + 1), "bo_wwait", timeo);
3745 if (error)
3746 break;
3747 }
3748 return (error);
3749 }
3750
3751 #include "opt_ddb.h"
3752 #ifdef DDB
3753 #include <ddb/ddb.h>
3754
3755 /* DDB command to show buffer data */
3756 DB_SHOW_COMMAND(buffer, db_show_buffer)
3757 {
3758 /* get args */
3759 struct buf *bp = (struct buf *)addr;
3760
3761 if (!have_addr) {
3762 db_printf("usage: show buffer <addr>\n");
3763 return;
3764 }
3765
3766 db_printf("buf at %p\n", bp);
3767 db_printf("b_flags = 0x%b\n", (u_int)bp->b_flags, PRINT_BUF_FLAGS);
3768 db_printf(
3769 "b_error = %d, b_bufsize = %ld, b_bcount = %ld, b_resid = %ld\n"
3770 "b_bufobj = (%p), b_data = %p, b_blkno = %jd\n",
3771 bp->b_error, bp->b_bufsize, bp->b_bcount, bp->b_resid,
3772 bp->b_bufobj, bp->b_data, (intmax_t)bp->b_blkno);
3773 if (bp->b_npages) {
3774 int i;
3775 db_printf("b_npages = %d, pages(OBJ, IDX, PA): ", bp->b_npages);
3776 for (i = 0; i < bp->b_npages; i++) {
3777 vm_page_t m;
3778 m = bp->b_pages[i];
3779 db_printf("(%p, 0x%lx, 0x%lx)", (void *)m->object,
3780 (u_long)m->pindex, (u_long)VM_PAGE_TO_PHYS(m));
3781 if ((i + 1) < bp->b_npages)
3782 db_printf(",");
3783 }
3784 db_printf("\n");
3785 }
3786 lockmgr_printinfo(&bp->b_lock);
3787 }
3788
3789 DB_SHOW_COMMAND(lockedbufs, lockedbufs)
3790 {
3791 struct buf *bp;
3792 int i;
3793
3794 for (i = 0; i < nbuf; i++) {
3795 bp = &buf[i];
3796 if (lockcount(&bp->b_lock)) {
3797 db_show_buffer((uintptr_t)bp, 1, 0, NULL);
3798 db_printf("\n");
3799 }
3800 }
3801 }
3802 #endif /* DDB */
Cache object: f2164e9c7c664706ae6b3368d0cffe4b
|