kernel - Cluster fixes + Enable clustering for HAMMER1
[dragonfly.git] / sys / kern / vfs_bio.c
CommitLineData
984263bc
MD
1/*
2 * Copyright (c) 1994,1997 John S. Dyson
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice immediately at the beginning of the file, without modification,
10 * this list of conditions, and the following disclaimer.
11 * 2. Absolutely no warranty of function or purpose is made by the author
12 * John S. Dyson.
13 *
14 * $FreeBSD: src/sys/kern/vfs_bio.c,v 1.242.2.20 2003/05/28 18:38:10 alc Exp $
15 */
16
17/*
18 * this file contains a new buffer I/O scheme implementing a coherent
19 * VM object and buffer cache scheme. Pains have been taken to make
20 * sure that the performance degradation associated with schemes such
21 * as this is not realized.
22 *
23 * Author: John S. Dyson
24 * Significant help during the development and debugging phases
25 * had been provided by David Greenman, also of the FreeBSD core team.
26 *
27 * see man buf(9) for more info.
28 */
29
30#include <sys/param.h>
31#include <sys/systm.h>
32#include <sys/buf.h>
33#include <sys/conf.h>
3b48c3c1 34#include <sys/devicestat.h>
984263bc
MD
35#include <sys/eventhandler.h>
36#include <sys/lock.h>
37#include <sys/malloc.h>
38#include <sys/mount.h>
39#include <sys/kernel.h>
40#include <sys/kthread.h>
41#include <sys/proc.h>
42#include <sys/reboot.h>
43#include <sys/resourcevar.h>
44#include <sys/sysctl.h>
45#include <sys/vmmeter.h>
46#include <sys/vnode.h>
8c72e3d5 47#include <sys/dsched.h>
984263bc
MD
48#include <vm/vm.h>
49#include <vm/vm_param.h>
50#include <vm/vm_kern.h>
51#include <vm/vm_pageout.h>
52#include <vm/vm_page.h>
53#include <vm/vm_object.h>
54#include <vm/vm_extern.h>
55#include <vm/vm_map.h>
c504e38e
MD
56#include <vm/vm_pager.h>
57#include <vm/swap_pager.h>
654a39f0 58
3020e3be 59#include <sys/buf2.h>
654a39f0 60#include <sys/thread2.h>
f832287e 61#include <sys/spinlock2.h>
684a93c4 62#include <sys/mplock2.h>
12e4aaff 63#include <vm/vm_page2.h>
984263bc 64
135bd6a8
MD
65#include "opt_ddb.h"
66#ifdef DDB
67#include <ddb/ddb.h>
68#endif
69
b3098c79
HP
70/*
71 * Buffer queues.
72 */
b3098c79
HP
73enum bufq_type {
74 BQUEUE_NONE, /* not on any queue */
75 BQUEUE_LOCKED, /* locked buffers */
76 BQUEUE_CLEAN, /* non-B_DELWRI buffers */
77 BQUEUE_DIRTY, /* B_DELWRI buffers */
4b958e7b 78 BQUEUE_DIRTY_HW, /* B_DELWRI buffers - heavy weight */
b3098c79 79 BQUEUE_EMPTYKVA, /* empty buffer headers with KVA assignment */
4b958e7b
MD
80 BQUEUE_EMPTY, /* empty buffer headers */
81
82 BUFFER_QUEUES /* number of buffer queues */
b3098c79 83};
4b958e7b
MD
84
85typedef enum bufq_type bufq_type_t;
86
79eae878 87#define BD_WAKE_SIZE 16384
c4df9635
MD
88#define BD_WAKE_MASK (BD_WAKE_SIZE - 1)
89
b3098c79 90TAILQ_HEAD(bqueues, buf) bufqueues[BUFFER_QUEUES];
77912481
MD
91static struct spinlock bufqspin = SPINLOCK_INITIALIZER(&bufqspin);
92static struct spinlock bufcspin = SPINLOCK_INITIALIZER(&bufcspin);
b3098c79 93
984263bc
MD
94static MALLOC_DEFINE(M_BIOBUF, "BIO buffer", "BIO buffer");
95
984263bc 96struct buf *buf; /* buffer header pool */
984263bc 97
c8e4131d 98static void vfs_clean_pages(struct buf *bp);
cb1cf930 99static void vfs_clean_one_page(struct buf *bp, int pageno, vm_page_t m);
3df0509c 100#if 0
0a8aee15 101static void vfs_dirty_one_page(struct buf *bp, int pageno, vm_page_t m);
3df0509c 102#endif
984263bc 103static void vfs_vmio_release(struct buf *bp);
9adb9c71 104static int flushbufqueues(struct buf *marker, bufq_type_t q);
4ecf7cc9 105static vm_page_t bio_page_alloc(vm_object_t obj, vm_pindex_t pg, int deficit);
984263bc 106
868d24af 107static void bd_signal(int totalspace);
4b958e7b
MD
108static void buf_daemon(void);
109static void buf_daemon_hw(void);
c4df9635 110
984263bc
MD
111/*
112 * bogus page -- for I/O to/from partially complete buffers
113 * this is a temporary solution to the problem, but it is not
114 * really that bad. it would be better to split the buffer
115 * for input in the case of buffers partially already in memory,
116 * but the code is intricate enough already.
117 */
118vm_page_t bogus_page;
a0c36a34 119
460426e6
MD
120/*
121 * These are all static, but make the ones we export globals so we do
122 * not need to use compiler magic.
123 */
3583bbb4
MD
124long bufspace; /* locked by buffer_map */
125long maxbufspace;
126static long bufmallocspace; /* atomic ops */
127long maxbufmallocspace, lobufspace, hibufspace;
984263bc 128static int bufreusecnt, bufdefragcnt, buffreekvacnt;
3583bbb4
MD
129static long lorunningspace;
130static long hirunningspace;
77912481 131static int runningbufreq; /* locked by bufcspin */
3583bbb4 132static long dirtybufspace; /* locked by bufcspin */
77912481 133static int dirtybufcount; /* locked by bufcspin */
3583bbb4 134static long dirtybufspacehw; /* locked by bufcspin */
77912481 135static int dirtybufcounthw; /* locked by bufcspin */
3583bbb4 136static long runningbufspace; /* locked by bufcspin */
77912481 137static int runningbufcount; /* locked by bufcspin */
3583bbb4
MD
138long lodirtybufspace;
139long hidirtybufspace;
984263bc
MD
140static int getnewbufcalls;
141static int getnewbufrestarts;
4ecf7cc9 142static int recoverbufcalls;
77912481
MD
143static int needsbuffer; /* locked by bufcspin */
144static int bd_request; /* locked by bufcspin */
145static int bd_request_hw; /* locked by bufcspin */
c4df9635
MD
146static u_int bd_wake_ary[BD_WAKE_SIZE];
147static u_int bd_wake_index;
d300946f 148static u_int vm_cycle_point = 40; /* 23-36 will migrate more act->inact */
8ae5c7e0 149static int debug_commit;
f832287e 150
4ecf7cc9
MD
151static struct thread *bufdaemon_td;
152static struct thread *bufdaemonhw_td;
59a5a4e8
MD
153static u_int lowmempgallocs;
154static u_int lowmempgfails;
4ecf7cc9 155
3f779080
HP
156/*
157 * Sysctls for operational control of the buffer cache.
158 */
3583bbb4 159SYSCTL_LONG(_vfs, OID_AUTO, lodirtybufspace, CTLFLAG_RW, &lodirtybufspace, 0,
3f779080 160 "Number of dirty buffers to flush before bufdaemon becomes inactive");
3583bbb4 161SYSCTL_LONG(_vfs, OID_AUTO, hidirtybufspace, CTLFLAG_RW, &hidirtybufspace, 0,
bb606263 162 "High watermark used to trigger explicit flushing of dirty buffers");
3583bbb4 163SYSCTL_LONG(_vfs, OID_AUTO, lorunningspace, CTLFLAG_RW, &lorunningspace, 0,
3f779080 164 "Minimum amount of buffer space required for active I/O");
3583bbb4 165SYSCTL_LONG(_vfs, OID_AUTO, hirunningspace, CTLFLAG_RW, &hirunningspace, 0,
3f779080 166 "Maximum amount of buffer space to usable for active I/O");
59a5a4e8
MD
167SYSCTL_UINT(_vfs, OID_AUTO, lowmempgallocs, CTLFLAG_RW, &lowmempgallocs, 0,
168 "Page allocations done during periods of very low free memory");
169SYSCTL_UINT(_vfs, OID_AUTO, lowmempgfails, CTLFLAG_RW, &lowmempgfails, 0,
170 "Page allocations which failed during periods of very low free memory");
0e8bd897
MD
171SYSCTL_UINT(_vfs, OID_AUTO, vm_cycle_point, CTLFLAG_RW, &vm_cycle_point, 0,
172 "Recycle pages to active or inactive queue transition pt 0-64");
3f779080
HP
173/*
174 * Sysctls determining current state of the buffer cache.
175 */
17cde63e
MD
176SYSCTL_INT(_vfs, OID_AUTO, nbuf, CTLFLAG_RD, &nbuf, 0,
177 "Total number of buffers in buffer cache");
3583bbb4 178SYSCTL_LONG(_vfs, OID_AUTO, dirtybufspace, CTLFLAG_RD, &dirtybufspace, 0,
70ac7d6c 179 "Pending bytes of dirty buffers (all)");
3583bbb4 180SYSCTL_LONG(_vfs, OID_AUTO, dirtybufspacehw, CTLFLAG_RD, &dirtybufspacehw, 0,
70ac7d6c
MD
181 "Pending bytes of dirty buffers (heavy weight)");
182SYSCTL_INT(_vfs, OID_AUTO, dirtybufcount, CTLFLAG_RD, &dirtybufcount, 0,
183 "Pending number of dirty buffers");
184SYSCTL_INT(_vfs, OID_AUTO, dirtybufcounthw, CTLFLAG_RD, &dirtybufcounthw, 0,
4b958e7b 185 "Pending number of dirty buffers (heavy weight)");
3583bbb4 186SYSCTL_LONG(_vfs, OID_AUTO, runningbufspace, CTLFLAG_RD, &runningbufspace, 0,
bb606263 187 "I/O bytes currently in progress due to asynchronous writes");
1b30fbcc
MD
188SYSCTL_INT(_vfs, OID_AUTO, runningbufcount, CTLFLAG_RD, &runningbufcount, 0,
189 "I/O buffers currently in progress due to asynchronous writes");
3583bbb4 190SYSCTL_LONG(_vfs, OID_AUTO, maxbufspace, CTLFLAG_RD, &maxbufspace, 0,
3f779080 191 "Hard limit on maximum amount of memory usable for buffer space");
3583bbb4 192SYSCTL_LONG(_vfs, OID_AUTO, hibufspace, CTLFLAG_RD, &hibufspace, 0,
3f779080 193 "Soft limit on maximum amount of memory usable for buffer space");
3583bbb4 194SYSCTL_LONG(_vfs, OID_AUTO, lobufspace, CTLFLAG_RD, &lobufspace, 0,
3f779080 195 "Minimum amount of memory to reserve for system buffer space");
3583bbb4 196SYSCTL_LONG(_vfs, OID_AUTO, bufspace, CTLFLAG_RD, &bufspace, 0,
3f779080 197 "Amount of memory available for buffers");
3583bbb4 198SYSCTL_LONG(_vfs, OID_AUTO, maxmallocbufspace, CTLFLAG_RD, &maxbufmallocspace,
bb606263 199 0, "Maximum amount of memory reserved for buffers using malloc");
3583bbb4 200SYSCTL_LONG(_vfs, OID_AUTO, bufmallocspace, CTLFLAG_RD, &bufmallocspace, 0,
3f779080
HP
201 "Amount of memory left for buffers using malloc-scheme");
202SYSCTL_INT(_vfs, OID_AUTO, getnewbufcalls, CTLFLAG_RD, &getnewbufcalls, 0,
203 "New buffer header acquisition requests");
204SYSCTL_INT(_vfs, OID_AUTO, getnewbufrestarts, CTLFLAG_RD, &getnewbufrestarts,
205 0, "New buffer header acquisition restarts");
4ecf7cc9
MD
206SYSCTL_INT(_vfs, OID_AUTO, recoverbufcalls, CTLFLAG_RD, &recoverbufcalls, 0,
207 "Recover VM space in an emergency");
3f779080 208SYSCTL_INT(_vfs, OID_AUTO, bufdefragcnt, CTLFLAG_RD, &bufdefragcnt, 0,
bb606263 209 "Buffer acquisition restarts due to fragmented buffer map");
3f779080
HP
210SYSCTL_INT(_vfs, OID_AUTO, buffreekvacnt, CTLFLAG_RD, &buffreekvacnt, 0,
211 "Amount of time KVA space was deallocated in an arbitrary buffer");
212SYSCTL_INT(_vfs, OID_AUTO, bufreusecnt, CTLFLAG_RD, &bufreusecnt, 0,
213 "Amount of time buffer re-use operations were successful");
8ae5c7e0 214SYSCTL_INT(_vfs, OID_AUTO, debug_commit, CTLFLAG_RW, &debug_commit, 0, "");
306ab3cb
HP
215SYSCTL_INT(_debug_sizeof, OID_AUTO, buf, CTLFLAG_RD, 0, sizeof(struct buf),
216 "sizeof(struct buf)");
984263bc 217
984263bc
MD
218char *buf_wmesg = BUF_WMESG;
219
984263bc 220#define VFS_BIO_NEED_ANY 0x01 /* any freeable buffer */
c4df9635 221#define VFS_BIO_NEED_UNUSED02 0x02
868d24af 222#define VFS_BIO_NEED_UNUSED04 0x04
984263bc
MD
223#define VFS_BIO_NEED_BUFSPACE 0x08 /* wait for buf space, lo hysteresis */
224
225/*
3f779080 226 * bufspacewakeup:
984263bc
MD
227 *
228 * Called when buffer space is potentially available for recovery.
229 * getnewbuf() will block on this flag when it is unable to free
230 * sufficient buffer space. Buffer space becomes recoverable when
231 * bp's get placed back in the queues.
232 */
984263bc
MD
233static __inline void
234bufspacewakeup(void)
235{
236 /*
237 * If someone is waiting for BUF space, wake them up. Even
238 * though we haven't freed the kva space yet, the waiting
239 * process will be able to now.
240 */
287a8577 241 spin_lock(&bufcspin);
984263bc
MD
242 if (needsbuffer & VFS_BIO_NEED_BUFSPACE) {
243 needsbuffer &= ~VFS_BIO_NEED_BUFSPACE;
287a8577 244 spin_unlock(&bufcspin);
984263bc 245 wakeup(&needsbuffer);
77912481 246 } else {
287a8577 247 spin_unlock(&bufcspin);
984263bc
MD
248 }
249}
250
251/*
3f779080
HP
252 * runningbufwakeup:
253 *
254 * Accounting for I/O in progress.
984263bc
MD
255 *
256 */
257static __inline void
258runningbufwakeup(struct buf *bp)
259{
3583bbb4
MD
260 long totalspace;
261 long limit;
868d24af
MD
262
263 if ((totalspace = bp->b_runningbufspace) != 0) {
287a8577 264 spin_lock(&bufcspin);
77912481
MD
265 runningbufspace -= totalspace;
266 --runningbufcount;
984263bc 267 bp->b_runningbufspace = 0;
4afeea0d
MD
268
269 /*
270 * see waitrunningbufspace() for limit test.
271 */
3038a8ca 272 limit = hirunningspace * 3 / 6;
4afeea0d 273 if (runningbufreq && runningbufspace <= limit) {
984263bc 274 runningbufreq = 0;
287a8577 275 spin_unlock(&bufcspin);
984263bc 276 wakeup(&runningbufreq);
77912481 277 } else {
287a8577 278 spin_unlock(&bufcspin);
984263bc 279 }
868d24af 280 bd_signal(totalspace);
984263bc
MD
281 }
282}
283
284/*
3f779080 285 * bufcountwakeup:
984263bc
MD
286 *
287 * Called when a buffer has been added to one of the free queues to
288 * account for the buffer and to wakeup anyone waiting for free buffers.
289 * This typically occurs when large amounts of metadata are being handled
290 * by the buffer cache ( else buffer space runs out first, usually ).
b1c20cfa
MD
291 *
292 * MPSAFE
984263bc 293 */
984263bc
MD
294static __inline void
295bufcountwakeup(void)
296{
287a8577 297 spin_lock(&bufcspin);
984263bc
MD
298 if (needsbuffer) {
299 needsbuffer &= ~VFS_BIO_NEED_ANY;
287a8577 300 spin_unlock(&bufcspin);
984263bc 301 wakeup(&needsbuffer);
77912481 302 } else {
287a8577 303 spin_unlock(&bufcspin);
984263bc
MD
304 }
305}
306
307/*
3f779080 308 * waitrunningbufspace()
984263bc 309 *
3038a8ca
MD
310 * If runningbufspace exceeds 4/6 hirunningspace we block until
311 * runningbufspace drops to 3/6 hirunningspace. We also block if another
312 * thread blocked here in order to be fair, even if runningbufspace
313 * is now lower than the limit.
984263bc 314 *
cd083340 315 * The caller may be using this function to block in a tight loop, we
3038a8ca
MD
316 * must block while runningbufspace is greater than at least
317 * hirunningspace * 3 / 6.
984263bc 318 */
aa1bfd98 319void
4afeea0d 320waitrunningbufspace(void)
984263bc 321{
3583bbb4 322 long limit = hirunningspace * 4 / 6;
cd083340 323
3038a8ca
MD
324 if (runningbufspace > limit || runningbufreq) {
325 spin_lock(&bufcspin);
326 while (runningbufspace > limit || runningbufreq) {
327 runningbufreq = 1;
77912481 328 ssleep(&runningbufreq, &bufcspin, 0, "wdrn1", 0);
e43a034f 329 }
287a8577 330 spin_unlock(&bufcspin);
984263bc
MD
331 }
332}
333
334/*
ae8e83e6
MD
335 * buf_dirty_count_severe:
336 *
337 * Return true if we have too many dirty buffers.
338 */
339int
340buf_dirty_count_severe(void)
341{
342 return (runningbufspace + dirtybufspace >= hidirtybufspace ||
343 dirtybufcount >= nbuf / 2);
344}
345
346/*
4afeea0d
MD
347 * Return true if the amount of running I/O is severe and BIOQ should
348 * start bursting.
349 */
350int
351buf_runningbufspace_severe(void)
352{
77912481 353 return (runningbufspace >= hirunningspace * 4 / 6);
4afeea0d
MD
354}
355
356/*
3f779080 357 * vfs_buf_test_cache:
984263bc 358 *
cb1cf930
MD
359 * Called when a buffer is extended. This function clears the B_CACHE
360 * bit if the newly extended portion of the buffer does not contain
361 * valid data.
362 *
363 * NOTE! Dirty VM pages are not processed into dirty (B_DELWRI) buffer
364 * cache buffers. The VM pages remain dirty, as someone had mmap()'d
365 * them while a clean buffer was present.
984263bc
MD
366 */
367static __inline__
368void
369vfs_buf_test_cache(struct buf *bp,
370 vm_ooffset_t foff, vm_offset_t off, vm_offset_t size,
371 vm_page_t m)
372{
373 if (bp->b_flags & B_CACHE) {
374 int base = (foff + off) & PAGE_MASK;
375 if (vm_page_is_valid(m, base, size) == 0)
376 bp->b_flags &= ~B_CACHE;
377 }
378}
379
3f779080 380/*
cd083340 381 * bd_speedup()
4b958e7b 382 *
cd083340
MD
383 * Spank the buf_daemon[_hw] if the total dirty buffer space exceeds the
384 * low water mark.
b1c20cfa
MD
385 *
386 * MPSAFE
3f779080 387 */
984263bc
MD
388static __inline__
389void
c4df9635 390bd_speedup(void)
984263bc 391{
70ac7d6c 392 if (dirtybufspace < lodirtybufspace && dirtybufcount < nbuf / 2)
cd083340
MD
393 return;
394
395 if (bd_request == 0 &&
70ac7d6c
MD
396 (dirtybufspace - dirtybufspacehw > lodirtybufspace / 2 ||
397 dirtybufcount - dirtybufcounthw >= nbuf / 2)) {
287a8577 398 spin_lock(&bufcspin);
984263bc 399 bd_request = 1;
287a8577 400 spin_unlock(&bufcspin);
984263bc
MD
401 wakeup(&bd_request);
402 }
cd083340 403 if (bd_request_hw == 0 &&
70ac7d6c
MD
404 (dirtybufspacehw > lodirtybufspace / 2 ||
405 dirtybufcounthw >= nbuf / 2)) {
287a8577 406 spin_lock(&bufcspin);
4b958e7b 407 bd_request_hw = 1;
287a8577 408 spin_unlock(&bufcspin);
4b958e7b
MD
409 wakeup(&bd_request_hw);
410 }
984263bc
MD
411}
412
413/*
c4df9635 414 * bd_heatup()
3f779080 415 *
c4df9635
MD
416 * Get the buf_daemon heated up when the number of running and dirty
417 * buffers exceeds the mid-point.
b1c20cfa 418 *
79eae878
MD
419 * Return the total number of dirty bytes past the second mid point
420 * as a measure of how much excess dirty data there is in the system.
421 *
b1c20cfa 422 * MPSAFE
984263bc 423 */
c4df9635
MD
424int
425bd_heatup(void)
426{
3583bbb4
MD
427 long mid1;
428 long mid2;
429 long totalspace;
984263bc 430
868d24af 431 mid1 = lodirtybufspace + (hidirtybufspace - lodirtybufspace) / 2;
c4df9635 432
868d24af 433 totalspace = runningbufspace + dirtybufspace;
70ac7d6c 434 if (totalspace >= mid1 || dirtybufcount >= nbuf / 2) {
c4df9635 435 bd_speedup();
868d24af
MD
436 mid2 = mid1 + (hidirtybufspace - mid1) / 2;
437 if (totalspace >= mid2)
438 return(totalspace - mid2);
c4df9635
MD
439 }
440 return(0);
441}
442
443/*
444 * bd_wait()
445 *
868d24af
MD
446 * Wait for the buffer cache to flush (totalspace) bytes worth of
447 * buffers, then return.
c4df9635
MD
448 *
449 * Regardless this function blocks while the number of dirty buffers
868d24af 450 * exceeds hidirtybufspace.
b1c20cfa
MD
451 *
452 * MPSAFE
c4df9635 453 */
984263bc 454void
868d24af 455bd_wait(int totalspace)
984263bc 456{
c4df9635 457 u_int i;
868d24af 458 int count;
c4df9635 459
4ecf7cc9
MD
460 if (curthread == bufdaemonhw_td || curthread == bufdaemon_td)
461 return;
462
868d24af 463 while (totalspace > 0) {
c4df9635 464 bd_heatup();
868d24af
MD
465 if (totalspace > runningbufspace + dirtybufspace)
466 totalspace = runningbufspace + dirtybufspace;
467 count = totalspace / BKVASIZE;
c4df9635
MD
468 if (count >= BD_WAKE_SIZE)
469 count = BD_WAKE_SIZE - 1;
b1c20cfa 470
287a8577 471 spin_lock(&bufcspin);
c4df9635
MD
472 i = (bd_wake_index + count) & BD_WAKE_MASK;
473 ++bd_wake_ary[i];
77912481
MD
474
475 /*
476 * This is not a strict interlock, so we play a bit loose
477 * with locking access to dirtybufspace*
478 */
ae8e83e6 479 tsleep_interlock(&bd_wake_ary[i], 0);
287a8577 480 spin_unlock(&bufcspin);
d9345d3a 481 tsleep(&bd_wake_ary[i], PINTERLOCKED, "flstik", hz);
c4df9635 482
868d24af 483 totalspace = runningbufspace + dirtybufspace - hidirtybufspace;
c4df9635
MD
484 }
485}
486
487/*
488 * bd_signal()
489 *
868d24af
MD
490 * This function is called whenever runningbufspace or dirtybufspace
491 * is reduced. Track threads waiting for run+dirty buffer I/O
c4df9635 492 * complete.
b1c20cfa
MD
493 *
494 * MPSAFE
c4df9635
MD
495 */
496static void
868d24af 497bd_signal(int totalspace)
c4df9635
MD
498{
499 u_int i;
500
b1c20cfa
MD
501 if (totalspace > 0) {
502 if (totalspace > BKVASIZE * BD_WAKE_SIZE)
503 totalspace = BKVASIZE * BD_WAKE_SIZE;
287a8577 504 spin_lock(&bufcspin);
b1c20cfa
MD
505 while (totalspace > 0) {
506 i = bd_wake_index++;
507 i &= BD_WAKE_MASK;
508 if (bd_wake_ary[i]) {
509 bd_wake_ary[i] = 0;
287a8577 510 spin_unlock(&bufcspin);
b1c20cfa 511 wakeup(&bd_wake_ary[i]);
287a8577 512 spin_lock(&bufcspin);
b1c20cfa
MD
513 }
514 totalspace -= BKVASIZE;
868d24af 515 }
287a8577 516 spin_unlock(&bufcspin);
c4df9635 517 }
984263bc
MD
518}
519
520/*
a9a20f98
MD
521 * BIO tracking support routines.
522 *
523 * Release a ref on a bio_track. Wakeup requests are atomically released
524 * along with the last reference so bk_active will never wind up set to
525 * only 0x80000000.
526 *
527 * MPSAFE
528 */
529static
530void
531bio_track_rel(struct bio_track *track)
532{
533 int active;
534 int desired;
535
536 /*
537 * Shortcut
538 */
539 active = track->bk_active;
540 if (active == 1 && atomic_cmpset_int(&track->bk_active, 1, 0))
541 return;
542
543 /*
544 * Full-on. Note that the wait flag is only atomically released on
545 * the 1->0 count transition.
e7edae1e
MD
546 *
547 * We check for a negative count transition using bit 30 since bit 31
548 * has a different meaning.
a9a20f98
MD
549 */
550 for (;;) {
551 desired = (active & 0x7FFFFFFF) - 1;
552 if (desired)
553 desired |= active & 0x80000000;
554 if (atomic_cmpset_int(&track->bk_active, active, desired)) {
e7edae1e 555 if (desired & 0x40000000)
a9a20f98
MD
556 panic("bio_track_rel: bad count: %p\n", track);
557 if (active & 0x80000000)
558 wakeup(track);
559 break;
560 }
561 active = track->bk_active;
562 }
563}
564
565/*
566 * Wait for the tracking count to reach 0.
567 *
568 * Use atomic ops such that the wait flag is only set atomically when
569 * bk_active is non-zero.
570 *
571 * MPSAFE
572 */
573int
574bio_track_wait(struct bio_track *track, int slp_flags, int slp_timo)
575{
576 int active;
577 int desired;
578 int error;
579
580 /*
581 * Shortcut
582 */
583 if (track->bk_active == 0)
584 return(0);
585
586 /*
587 * Full-on. Note that the wait flag may only be atomically set if
588 * the active count is non-zero.
bbdc6499
MD
589 *
590 * NOTE: We cannot optimize active == desired since a wakeup could
591 * clear active prior to our tsleep_interlock().
a9a20f98 592 */
a9a20f98
MD
593 error = 0;
594 while ((active = track->bk_active) != 0) {
8bbb2fba 595 cpu_ccfence();
a9a20f98 596 desired = active | 0x80000000;
ae8e83e6 597 tsleep_interlock(track, slp_flags);
bbdc6499 598 if (atomic_cmpset_int(&track->bk_active, active, desired)) {
d9345d3a 599 error = tsleep(track, slp_flags | PINTERLOCKED,
bbdc6499 600 "trwait", slp_timo);
a9a20f98
MD
601 if (error)
602 break;
603 }
604 }
a9a20f98
MD
605 return (error);
606}
607
608/*
3f779080
HP
609 * bufinit:
610 *
611 * Load time initialisation of the buffer cache, called from machine
612 * dependant initialization code.
613 */
984263bc
MD
614void
615bufinit(void)
616{
617 struct buf *bp;
b8bb0773 618 vm_offset_t bogus_offset;
984263bc
MD
619 int i;
620
984263bc
MD
621 /* next, make a null set of free lists */
622 for (i = 0; i < BUFFER_QUEUES; i++)
623 TAILQ_INIT(&bufqueues[i]);
624
625 /* finally, initialize each buffer header and stick on empty q */
626 for (i = 0; i < nbuf; i++) {
627 bp = &buf[i];
628 bzero(bp, sizeof *bp);
629 bp->b_flags = B_INVAL; /* we're just an empty header */
10f3fee5 630 bp->b_cmd = BUF_CMD_DONE;
b3098c79 631 bp->b_qindex = BQUEUE_EMPTY;
81b5c339 632 initbufbio(bp);
54f51aeb 633 xio_init(&bp->b_xio);
408357d8 634 buf_dep_init(bp);
b3098c79 635 TAILQ_INSERT_TAIL(&bufqueues[BQUEUE_EMPTY], bp, b_freelist);
984263bc
MD
636 }
637
638 /*
639 * maxbufspace is the absolute maximum amount of buffer space we are
640 * allowed to reserve in KVM and in real terms. The absolute maximum
641 * is nominally used by buf_daemon. hibufspace is the nominal maximum
642 * used by most other processes. The differential is required to
643 * ensure that buf_daemon is able to run when other processes might
644 * be blocked waiting for buffer space.
645 *
646 * maxbufspace is based on BKVASIZE. Allocating buffers larger then
647 * this may result in KVM fragmentation which is not handled optimally
648 * by the system.
649 */
3583bbb4 650 maxbufspace = (long)nbuf * BKVASIZE;
984263bc
MD
651 hibufspace = imax(3 * maxbufspace / 4, maxbufspace - MAXBSIZE * 10);
652 lobufspace = hibufspace - MAXBSIZE;
653
654 lorunningspace = 512 * 1024;
4afeea0d 655 /* hirunningspace -- see below */
984263bc 656
868d24af
MD
657 /*
658 * Limit the amount of malloc memory since it is wired permanently
659 * into the kernel space. Even though this is accounted for in
660 * the buffer allocation, we don't want the malloced region to grow
661 * uncontrolled. The malloc scheme improves memory utilization
662 * significantly on average (small) directories.
663 */
984263bc
MD
664 maxbufmallocspace = hibufspace / 20;
665
868d24af
MD
666 /*
667 * Reduce the chance of a deadlock occuring by limiting the number
668 * of delayed-write dirty buffers we allow to stack up.
4afeea0d
MD
669 *
670 * We don't want too much actually queued to the device at once
671 * (XXX this needs to be per-mount!), because the buffers will
672 * wind up locked for a very long period of time while the I/O
673 * drains.
868d24af 674 */
4afeea0d
MD
675 hidirtybufspace = hibufspace / 2; /* dirty + running */
676 hirunningspace = hibufspace / 16; /* locked & queued to device */
677 if (hirunningspace < 1024 * 1024)
678 hirunningspace = 1024 * 1024;
679
868d24af
MD
680 dirtybufspace = 0;
681 dirtybufspacehw = 0;
984263bc 682
868d24af 683 lodirtybufspace = hidirtybufspace / 2;
984263bc 684
868d24af
MD
685 /*
686 * Maximum number of async ops initiated per buf_daemon loop. This is
687 * somewhat of a hack at the moment, we really need to limit ourselves
688 * based on the number of bytes of I/O in-transit that were initiated
689 * from buf_daemon.
690 */
984263bc 691
e4846942 692 bogus_offset = kmem_alloc_pageable(&kernel_map, PAGE_SIZE);
b12defdc 693 vm_object_hold(&kernel_object);
c439ad8f 694 bogus_page = vm_page_alloc(&kernel_object,
e4846942
MD
695 (bogus_offset >> PAGE_SHIFT),
696 VM_ALLOC_NORMAL);
b12defdc 697 vm_object_drop(&kernel_object);
12e4aaff 698 vmstats.v_wire_count++;
984263bc
MD
699
700}
701
702/*
b5d7061d
MD
703 * Initialize the embedded bio structures, typically used by
704 * deprecated code which tries to allocate its own struct bufs.
81b5c339
MD
705 */
706void
707initbufbio(struct buf *bp)
708{
709 bp->b_bio1.bio_buf = bp;
710 bp->b_bio1.bio_prev = NULL;
81b5c339
MD
711 bp->b_bio1.bio_offset = NOOFFSET;
712 bp->b_bio1.bio_next = &bp->b_bio2;
713 bp->b_bio1.bio_done = NULL;
ae8e83e6 714 bp->b_bio1.bio_flags = 0;
81b5c339
MD
715
716 bp->b_bio2.bio_buf = bp;
717 bp->b_bio2.bio_prev = &bp->b_bio1;
81b5c339
MD
718 bp->b_bio2.bio_offset = NOOFFSET;
719 bp->b_bio2.bio_next = NULL;
720 bp->b_bio2.bio_done = NULL;
ae8e83e6 721 bp->b_bio2.bio_flags = 0;
b5d7061d
MD
722
723 BUF_LOCKINIT(bp);
81b5c339
MD
724}
725
726/*
727 * Reinitialize the embedded bio structures as well as any additional
728 * translation cache layers.
729 */
730void
731reinitbufbio(struct buf *bp)
732{
733 struct bio *bio;
734
735 for (bio = &bp->b_bio1; bio; bio = bio->bio_next) {
736 bio->bio_done = NULL;
81b5c339
MD
737 bio->bio_offset = NOOFFSET;
738 }
739}
740
741/*
b5d7061d
MD
742 * Undo the effects of an initbufbio().
743 */
744void
745uninitbufbio(struct buf *bp)
746{
747 dsched_exit_buf(bp);
748 BUF_LOCKFREE(bp);
749}
750
751/*
81b5c339
MD
752 * Push another BIO layer onto an existing BIO and return it. The new
753 * BIO layer may already exist, holding cached translation data.
754 */
755struct bio *
756push_bio(struct bio *bio)
757{
758 struct bio *nbio;
759
760 if ((nbio = bio->bio_next) == NULL) {
761 int index = bio - &bio->bio_buf->b_bio_array[0];
bbd44c71 762 if (index >= NBUF_BIO - 1) {
81b5c339
MD
763 panic("push_bio: too many layers bp %p\n",
764 bio->bio_buf);
765 }
766 nbio = &bio->bio_buf->b_bio_array[index + 1];
767 bio->bio_next = nbio;
768 nbio->bio_prev = bio;
769 nbio->bio_buf = bio->bio_buf;
81b5c339
MD
770 nbio->bio_offset = NOOFFSET;
771 nbio->bio_done = NULL;
772 nbio->bio_next = NULL;
773 }
774 KKASSERT(nbio->bio_done == NULL);
775 return(nbio);
776}
777
b77cfc40
MD
778/*
779 * Pop a BIO translation layer, returning the previous layer. The
780 * must have been previously pushed.
781 */
782struct bio *
81b5c339
MD
783pop_bio(struct bio *bio)
784{
b77cfc40 785 return(bio->bio_prev);
81b5c339
MD
786}
787
788void
789clearbiocache(struct bio *bio)
790{
791 while (bio) {
81b5c339
MD
792 bio->bio_offset = NOOFFSET;
793 bio = bio->bio_next;
794 }
795}
796
797/*
3f779080
HP
798 * bfreekva:
799 *
800 * Free the KVA allocation for buffer 'bp'.
984263bc 801 *
e43a034f 802 * Must be called from a critical section as this is the only locking for
984263bc
MD
803 * buffer_map.
804 *
805 * Since this call frees up buffer space, we call bufspacewakeup().
b1c20cfa
MD
806 *
807 * MPALMOSTSAFE
984263bc
MD
808 */
809static void
312dcd01 810bfreekva(struct buf *bp)
984263bc 811{
a108bf71
MD
812 int count;
813
984263bc
MD
814 if (bp->b_kvasize) {
815 ++buffreekvacnt;
a108bf71 816 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
e4846942 817 vm_map_lock(&buffer_map);
984263bc 818 bufspace -= bp->b_kvasize;
e4846942 819 vm_map_delete(&buffer_map,
984263bc 820 (vm_offset_t) bp->b_kvabase,
a108bf71
MD
821 (vm_offset_t) bp->b_kvabase + bp->b_kvasize,
822 &count
984263bc 823 );
e4846942 824 vm_map_unlock(&buffer_map);
a108bf71 825 vm_map_entry_release(count);
984263bc 826 bp->b_kvasize = 0;
9a82e536 827 bp->b_kvabase = NULL;
984263bc
MD
828 bufspacewakeup();
829 }
830}
831
832/*
3f779080 833 * bremfree:
984263bc
MD
834 *
835 * Remove the buffer from the appropriate free list.
836 */
c3d1e862
MD
837static __inline void
838_bremfree(struct buf *bp)
984263bc 839{
b3098c79 840 if (bp->b_qindex != BQUEUE_NONE) {
77bb9400
MD
841 KASSERT(BUF_REFCNTNB(bp) == 1,
842 ("bremfree: bp %p not locked",bp));
984263bc 843 TAILQ_REMOVE(&bufqueues[bp->b_qindex], bp, b_freelist);
b3098c79 844 bp->b_qindex = BQUEUE_NONE;
984263bc 845 } else {
77bb9400 846 if (BUF_REFCNTNB(bp) <= 1)
984263bc
MD
847 panic("bremfree: removing a buffer not on a queue");
848 }
c3d1e862 849}
984263bc 850
c3d1e862
MD
851void
852bremfree(struct buf *bp)
853{
287a8577 854 spin_lock(&bufqspin);
c3d1e862 855 _bremfree(bp);
287a8577 856 spin_unlock(&bufqspin);
984263bc
MD
857}
858
b1c20cfa 859static void
c3d1e862
MD
860bremfree_locked(struct buf *bp)
861{
862 _bremfree(bp);
863}
984263bc
MD
864
865/*
c1c3e862
MD
866 * This version of bread issues any required I/O asyncnronously and
867 * makes a callback on completion.
868 *
869 * The callback must check whether BIO_DONE is set in the bio and issue
870 * the bpdone(bp, 0) if it isn't. The callback is responsible for clearing
871 * BIO_DONE and disposing of the I/O (bqrelse()ing it).
872 */
873void
874breadcb(struct vnode *vp, off_t loffset, int size,
875 void (*func)(struct bio *), void *arg)
876{
877 struct buf *bp;
878
879 bp = getblk(vp, loffset, size, 0, 0);
880
881 /* if not found in cache, do some I/O */
882 if ((bp->b_flags & B_CACHE) == 0) {
883 bp->b_flags &= ~(B_ERROR | B_EINTR | B_INVAL);
884 bp->b_cmd = BUF_CMD_READ;
885 bp->b_bio1.bio_done = func;
886 bp->b_bio1.bio_caller_info1.ptr = arg;
887 vfs_busy_pages(vp, bp);
888 BUF_KERNPROC(bp);
889 vn_strategy(vp, &bp->b_bio1);
890 } else if (func) {
386355f1
MD
891 /*
892 * Since we are issuing the callback synchronously it cannot
893 * race the BIO_DONE, so no need for atomic ops here.
894 */
c1c3e862
MD
895 /*bp->b_bio1.bio_done = func;*/
896 bp->b_bio1.bio_caller_info1.ptr = arg;
897 bp->b_bio1.bio_flags |= BIO_DONE;
898 func(&bp->b_bio1);
899 } else {
900 bqrelse(bp);
901 }
902}
903
904/*
54341a3b 905 * breadnx() - Terminal function for bread() and breadn().
3f779080 906 *
54341a3b
MD
907 * This function will start asynchronous I/O on read-ahead blocks as well
908 * as satisfy the primary request.
b1c20cfa 909 *
54341a3b
MD
910 * We must clear B_ERROR and B_INVAL prior to initiating I/O. If B_CACHE is
911 * set, the buffer is valid and we do not have to do anything.
984263bc
MD
912 */
913int
54341a3b 914breadnx(struct vnode *vp, off_t loffset, int size, off_t *raoffset,
c8e4131d 915 int *rabsize, int cnt, struct buf **bpp)
984263bc
MD
916{
917 struct buf *bp, *rabp;
918 int i;
919 int rv = 0, readwait = 0;
920
54341a3b
MD
921 if (*bpp)
922 bp = *bpp;
923 else
924 *bpp = bp = getblk(vp, loffset, size, 0, 0);
984263bc
MD
925
926 /* if not found in cache, do some I/O */
927 if ((bp->b_flags & B_CACHE) == 0) {
ae8e83e6 928 bp->b_flags &= ~(B_ERROR | B_EINTR | B_INVAL);
10f3fee5 929 bp->b_cmd = BUF_CMD_READ;
ae8e83e6
MD
930 bp->b_bio1.bio_done = biodone_sync;
931 bp->b_bio1.bio_flags |= BIO_SYNC;
10f3fee5 932 vfs_busy_pages(vp, bp);
81b5c339 933 vn_strategy(vp, &bp->b_bio1);
984263bc
MD
934 ++readwait;
935 }
936
54078292
MD
937 for (i = 0; i < cnt; i++, raoffset++, rabsize++) {
938 if (inmem(vp, *raoffset))
984263bc 939 continue;
54078292 940 rabp = getblk(vp, *raoffset, *rabsize, 0, 0);
984263bc
MD
941
942 if ((rabp->b_flags & B_CACHE) == 0) {
ae8e83e6 943 rabp->b_flags &= ~(B_ERROR | B_EINTR | B_INVAL);
10f3fee5
MD
944 rabp->b_cmd = BUF_CMD_READ;
945 vfs_busy_pages(vp, rabp);
984263bc 946 BUF_KERNPROC(rabp);
81b5c339 947 vn_strategy(vp, &rabp->b_bio1);
984263bc
MD
948 } else {
949 brelse(rabp);
950 }
951 }
b1c20cfa 952 if (readwait)
ae8e83e6 953 rv = biowait(&bp->b_bio1, "biord");
984263bc
MD
954 return (rv);
955}
956
957/*
3f779080
HP
958 * bwrite:
959 *
ae8e83e6
MD
960 * Synchronous write, waits for completion.
961 *
3f779080
HP
962 * Write, release buffer on completion. (Done by iodone
963 * if async). Do not bother writing anything if the buffer
964 * is invalid.
965 *
966 * Note that we set B_CACHE here, indicating that buffer is
967 * fully valid and thus cacheable. This is true even of NFS
968 * now so we set it generally. This could be set either here
969 * or in biodone() since the I/O is synchronous. We put it
970 * here.
984263bc
MD
971 */
972int
c8e4131d 973bwrite(struct buf *bp)
984263bc 974{
ae8e83e6 975 int error;
984263bc
MD
976
977 if (bp->b_flags & B_INVAL) {
978 brelse(bp);
979 return (0);
980 }
77bb9400 981 if (BUF_REFCNTNB(bp) == 0)
984263bc 982 panic("bwrite: buffer is not busy???");
984263bc
MD
983
984 /* Mark the buffer clean */
985 bundirty(bp);
986
ae8e83e6 987 bp->b_flags &= ~(B_ERROR | B_EINTR);
6bae6177 988 bp->b_flags |= B_CACHE;
10f3fee5 989 bp->b_cmd = BUF_CMD_WRITE;
ae8e83e6
MD
990 bp->b_bio1.bio_done = biodone_sync;
991 bp->b_bio1.bio_flags |= BIO_SYNC;
10f3fee5 992 vfs_busy_pages(bp->b_vp, bp);
984263bc
MD
993
994 /*
9a71d53f
MD
995 * Normal bwrites pipeline writes. NOTE: b_bufsize is only
996 * valid for vnode-backed buffers.
984263bc 997 */
77912481 998 bsetrunningbufspace(bp, bp->b_bufsize);
81b5c339 999 vn_strategy(bp->b_vp, &bp->b_bio1);
ae8e83e6
MD
1000 error = biowait(&bp->b_bio1, "biows");
1001 brelse(bp);
77912481 1002
ae8e83e6
MD
1003 return (error);
1004}
984263bc 1005
ae8e83e6
MD
1006/*
1007 * bawrite:
1008 *
1009 * Asynchronous write. Start output on a buffer, but do not wait for
1010 * it to complete. The buffer is released when the output completes.
1011 *
1012 * bwrite() ( or the VOP routine anyway ) is responsible for handling
1013 * B_INVAL buffers. Not us.
1014 */
1015void
1016bawrite(struct buf *bp)
1017{
1018 if (bp->b_flags & B_INVAL) {
984263bc 1019 brelse(bp);
ae8e83e6
MD
1020 return;
1021 }
1022 if (BUF_REFCNTNB(bp) == 0)
1023 panic("bwrite: buffer is not busy???");
1024
1025 /* Mark the buffer clean */
1026 bundirty(bp);
1027
1028 bp->b_flags &= ~(B_ERROR | B_EINTR);
1029 bp->b_flags |= B_CACHE;
1030 bp->b_cmd = BUF_CMD_WRITE;
1031 KKASSERT(bp->b_bio1.bio_done == NULL);
1032 vfs_busy_pages(bp->b_vp, bp);
1033
1034 /*
1035 * Normal bwrites pipeline writes. NOTE: b_bufsize is only
1036 * valid for vnode-backed buffers.
1037 */
77912481 1038 bsetrunningbufspace(bp, bp->b_bufsize);
ae8e83e6
MD
1039 BUF_KERNPROC(bp);
1040 vn_strategy(bp->b_vp, &bp->b_bio1);
1041}
1042
1043/*
1044 * bowrite:
1045 *
1046 * Ordered write. Start output on a buffer, and flag it so that the
1047 * device will write it in the order it was queued. The buffer is
1048 * released when the output completes. bwrite() ( or the VOP routine
1049 * anyway ) is responsible for handling B_INVAL buffers.
1050 */
1051int
1052bowrite(struct buf *bp)
1053{
1054 bp->b_flags |= B_ORDERED;
1055 bawrite(bp);
984263bc
MD
1056 return (0);
1057}
1058
984263bc 1059/*
3f779080
HP
1060 * bdwrite:
1061 *
1062 * Delayed write. (Buffer is marked dirty). Do not bother writing
1063 * anything if the buffer is marked invalid.
984263bc 1064 *
3f779080
HP
1065 * Note that since the buffer must be completely valid, we can safely
1066 * set B_CACHE. In fact, we have to set B_CACHE here rather then in
1067 * biodone() in order to prevent getblk from writing the buffer
1068 * out synchronously.
984263bc
MD
1069 */
1070void
493c516a 1071bdwrite(struct buf *bp)
984263bc 1072{
77bb9400 1073 if (BUF_REFCNTNB(bp) == 0)
984263bc
MD
1074 panic("bdwrite: buffer is not busy");
1075
1076 if (bp->b_flags & B_INVAL) {
1077 brelse(bp);
1078 return;
1079 }
1080 bdirty(bp);
1081
8c72e3d5
AH
1082 if (dsched_is_clear_buf_priv(bp))
1083 dsched_new_buf(bp);
1084
984263bc
MD
1085 /*
1086 * Set B_CACHE, indicating that the buffer is fully valid. This is
1087 * true even of NFS now.
1088 */
1089 bp->b_flags |= B_CACHE;
1090
1091 /*
1092 * This bmap keeps the system from needing to do the bmap later,
1093 * perhaps when the system is attempting to do a sync. Since it
1094 * is likely that the indirect block -- or whatever other datastructure
1095 * that the filesystem needs is still in memory now, it is a good
1096 * thing to do this. Note also, that if the pageout daemon is
1097 * requesting a sync -- there might not be enough memory to do
1098 * the bmap then... So, this is important to do.
1099 */
54078292 1100 if (bp->b_bio2.bio_offset == NOOFFSET) {
08daea96 1101 VOP_BMAP(bp->b_vp, bp->b_loffset, &bp->b_bio2.bio_offset,
e92ca23a 1102 NULL, NULL, BUF_CMD_WRITE);
984263bc
MD
1103 }
1104
1105 /*
cb1cf930
MD
1106 * Because the underlying pages may still be mapped and
1107 * writable trying to set the dirty buffer (b_dirtyoff/end)
1108 * range here will be inaccurate.
1109 *
1110 * However, we must still clean the pages to satisfy the
1111 * vnode_pager and pageout daemon, so theythink the pages
1112 * have been "cleaned". What has really occured is that
1113 * they've been earmarked for later writing by the buffer
1114 * cache.
1115 *
1116 * So we get the b_dirtyoff/end update but will not actually
1117 * depend on it (NFS that is) until the pages are busied for
1118 * writing later on.
984263bc
MD
1119 */
1120 vfs_clean_pages(bp);
1121 bqrelse(bp);
1122
1123 /*
984263bc
MD
1124 * note: we cannot initiate I/O from a bdwrite even if we wanted to,
1125 * due to the softdep code.
1126 */
1127}
1128
1129/*
0a8aee15
MD
1130 * Fake write - return pages to VM system as dirty, leave the buffer clean.
1131 * This is used by tmpfs.
1132 *
1133 * It is important for any VFS using this routine to NOT use it for
1134 * IO_SYNC or IO_ASYNC operations which occur when the system really
1135 * wants to flush VM pages to backing store.
1136 */
1137void
1138buwrite(struct buf *bp)
1139{
1140 vm_page_t m;
1141 int i;
1142
1143 /*
1144 * Only works for VMIO buffers. If the buffer is already
1145 * marked for delayed-write we can't avoid the bdwrite().
1146 */
1147 if ((bp->b_flags & B_VMIO) == 0 || (bp->b_flags & B_DELWRI)) {
1148 bdwrite(bp);
1149 return;
1150 }
1151
1152 /*
3df0509c 1153 * Mark as needing a commit.
0a8aee15
MD
1154 */
1155 for (i = 0; i < bp->b_xio.xio_npages; i++) {
1156 m = bp->b_xio.xio_pages[i];
3df0509c 1157 vm_page_need_commit(m);
0a8aee15
MD
1158 }
1159 bqrelse(bp);
1160}
1161
1162/*
3f779080 1163 * bdirty:
984263bc 1164 *
10f3fee5
MD
1165 * Turn buffer into delayed write request by marking it B_DELWRI.
1166 * B_RELBUF and B_NOCACHE must be cleared.
984263bc 1167 *
10f3fee5
MD
1168 * We reassign the buffer to itself to properly update it in the
1169 * dirty/clean lists.
984263bc 1170 *
e43a034f 1171 * Must be called from a critical section.
b3098c79 1172 * The buffer must be on BQUEUE_NONE.
984263bc
MD
1173 */
1174void
493c516a 1175bdirty(struct buf *bp)
984263bc 1176{
55b50bd5
MD
1177 KASSERT(bp->b_qindex == BQUEUE_NONE,
1178 ("bdirty: buffer %p still on queue %d", bp, bp->b_qindex));
69f8c926 1179 if (bp->b_flags & B_NOCACHE) {
6ea70f76 1180 kprintf("bdirty: clearing B_NOCACHE on buf %p\n", bp);
69f8c926
MD
1181 bp->b_flags &= ~B_NOCACHE;
1182 }
1183 if (bp->b_flags & B_INVAL) {
6ea70f76 1184 kprintf("bdirty: warning, dirtying invalid buffer %p\n", bp);
69f8c926 1185 }
10f3fee5 1186 bp->b_flags &= ~B_RELBUF;
984263bc
MD
1187
1188 if ((bp->b_flags & B_DELWRI) == 0) {
c5724852 1189 lwkt_gettoken(&bp->b_vp->v_token);
10f3fee5 1190 bp->b_flags |= B_DELWRI;
1f1ea522 1191 reassignbuf(bp);
c5724852 1192 lwkt_reltoken(&bp->b_vp->v_token);
77912481 1193
287a8577 1194 spin_lock(&bufcspin);
77912481 1195 ++dirtybufcount;
868d24af 1196 dirtybufspace += bp->b_bufsize;
70ac7d6c 1197 if (bp->b_flags & B_HEAVY) {
77912481
MD
1198 ++dirtybufcounthw;
1199 dirtybufspacehw += bp->b_bufsize;
70ac7d6c 1200 }
287a8577 1201 spin_unlock(&bufcspin);
77912481 1202
c4df9635 1203 bd_heatup();
984263bc
MD
1204 }
1205}
1206
1207/*
4b958e7b
MD
1208 * Set B_HEAVY, indicating that this is a heavy-weight buffer that
1209 * needs to be flushed with a different buf_daemon thread to avoid
1210 * deadlocks. B_HEAVY also imposes restrictions in getnewbuf().
1211 */
1212void
1213bheavy(struct buf *bp)
1214{
1215 if ((bp->b_flags & B_HEAVY) == 0) {
1216 bp->b_flags |= B_HEAVY;
70ac7d6c 1217 if (bp->b_flags & B_DELWRI) {
287a8577 1218 spin_lock(&bufcspin);
77912481
MD
1219 ++dirtybufcounthw;
1220 dirtybufspacehw += bp->b_bufsize;
287a8577 1221 spin_unlock(&bufcspin);
70ac7d6c 1222 }
4b958e7b
MD
1223 }
1224}
1225
1226/*
3f779080 1227 * bundirty:
984263bc
MD
1228 *
1229 * Clear B_DELWRI for buffer.
1230 *
e43a034f 1231 * Must be called from a critical section.
eaaadca0 1232 *
b3098c79 1233 * The buffer is typically on BQUEUE_NONE but there is one case in
eaaadca0
MD
1234 * brelse() that calls this function after placing the buffer on
1235 * a different queue.
b1c20cfa
MD
1236 *
1237 * MPSAFE
984263bc 1238 */
984263bc 1239void
493c516a 1240bundirty(struct buf *bp)
984263bc 1241{
984263bc 1242 if (bp->b_flags & B_DELWRI) {
c5724852 1243 lwkt_gettoken(&bp->b_vp->v_token);
984263bc 1244 bp->b_flags &= ~B_DELWRI;
1f1ea522 1245 reassignbuf(bp);
c5724852 1246 lwkt_reltoken(&bp->b_vp->v_token);
77912481 1247
287a8577 1248 spin_lock(&bufcspin);
77912481
MD
1249 --dirtybufcount;
1250 dirtybufspace -= bp->b_bufsize;
70ac7d6c 1251 if (bp->b_flags & B_HEAVY) {
77912481
MD
1252 --dirtybufcounthw;
1253 dirtybufspacehw -= bp->b_bufsize;
70ac7d6c 1254 }
287a8577 1255 spin_unlock(&bufcspin);
77912481 1256
868d24af 1257 bd_signal(bp->b_bufsize);
984263bc
MD
1258 }
1259 /*
1260 * Since it is now being written, we can clear its deferred write flag.
1261 */
1262 bp->b_flags &= ~B_DEFERRED;
1263}
1264
1265/*
77912481
MD
1266 * Set the b_runningbufspace field, used to track how much I/O is
1267 * in progress at any given moment.
1268 */
1269void
1270bsetrunningbufspace(struct buf *bp, int bytes)
1271{
1272 bp->b_runningbufspace = bytes;
1273 if (bytes) {
287a8577 1274 spin_lock(&bufcspin);
77912481
MD
1275 runningbufspace += bytes;
1276 ++runningbufcount;
287a8577 1277 spin_unlock(&bufcspin);
77912481
MD
1278 }
1279}
1280
1281/*
3f779080 1282 * brelse:
984263bc
MD
1283 *
1284 * Release a busy buffer and, if requested, free its resources. The
1285 * buffer will be stashed in the appropriate bufqueue[] allowing it
1286 * to be accessed later as a cache entity or reused for other purposes.
b1c20cfa
MD
1287 *
1288 * MPALMOSTSAFE
984263bc
MD
1289 */
1290void
c8e4131d 1291brelse(struct buf *bp)
984263bc 1292{
9188c711
MD
1293#ifdef INVARIANTS
1294 int saved_flags = bp->b_flags;
1295#endif
1296
984263bc
MD
1297 KASSERT(!(bp->b_flags & (B_CLUSTER|B_PAGING)), ("brelse: inappropriate B_PAGING or B_CLUSTER bp %p", bp));
1298
135bd6a8
MD
1299 /*
1300 * If B_NOCACHE is set we are being asked to destroy the buffer and
1301 * its backing store. Clear B_DELWRI.
1302 *
1303 * B_NOCACHE is set in two cases: (1) when the caller really wants
1304 * to destroy the buffer and backing store and (2) when the caller
1305 * wants to destroy the buffer and backing store after a write
1306 * completes.
1307 */
1308 if ((bp->b_flags & (B_NOCACHE|B_DELWRI)) == (B_NOCACHE|B_DELWRI)) {
1309 bundirty(bp);
69f8c926
MD
1310 }
1311
78a9b77f 1312 if ((bp->b_flags & (B_INVAL | B_DELWRI)) == B_DELWRI) {
984263bc 1313 /*
78a9b77f
MD
1314 * A re-dirtied buffer is only subject to destruction
1315 * by B_INVAL. B_ERROR and B_NOCACHE are ignored.
984263bc 1316 */
78a9b77f 1317 /* leave buffer intact */
10f3fee5 1318 } else if ((bp->b_flags & (B_NOCACHE | B_INVAL | B_ERROR)) ||
78a9b77f 1319 (bp->b_bufsize <= 0)) {
984263bc 1320 /*
78a9b77f
MD
1321 * Either a failed read or we were asked to free or not
1322 * cache the buffer. This path is reached with B_DELWRI
1323 * set only if B_INVAL is already set. B_NOCACHE governs
1324 * backing store destruction.
408357d8
MD
1325 *
1326 * NOTE: HAMMER will set B_LOCKED in buf_deallocate if the
1327 * buffer cannot be immediately freed.
984263bc
MD
1328 */
1329 bp->b_flags |= B_INVAL;
77912481 1330 if (LIST_FIRST(&bp->b_dep) != NULL)
408357d8 1331 buf_deallocate(bp);
984263bc 1332 if (bp->b_flags & B_DELWRI) {
287a8577 1333 spin_lock(&bufcspin);
77912481
MD
1334 --dirtybufcount;
1335 dirtybufspace -= bp->b_bufsize;
70ac7d6c 1336 if (bp->b_flags & B_HEAVY) {
77912481
MD
1337 --dirtybufcounthw;
1338 dirtybufspacehw -= bp->b_bufsize;
70ac7d6c 1339 }
287a8577 1340 spin_unlock(&bufcspin);
77912481 1341
868d24af 1342 bd_signal(bp->b_bufsize);
984263bc 1343 }
10f3fee5 1344 bp->b_flags &= ~(B_DELWRI | B_CACHE);
984263bc
MD
1345 }
1346
1347 /*
283b9448
MD
1348 * We must clear B_RELBUF if B_DELWRI or B_LOCKED is set,
1349 * or if b_refs is non-zero.
1350 *
408357d8
MD
1351 * If vfs_vmio_release() is called with either bit set, the
1352 * underlying pages may wind up getting freed causing a previous
1353 * write (bdwrite()) to get 'lost' because pages associated with
1354 * a B_DELWRI bp are marked clean. Pages associated with a
1355 * B_LOCKED buffer may be mapped by the filesystem.
4b958e7b
MD
1356 *
1357 * If we want to release the buffer ourselves (rather then the
1358 * originator asking us to release it), give the originator a
1359 * chance to countermand the release by setting B_LOCKED.
984263bc
MD
1360 *
1361 * We still allow the B_INVAL case to call vfs_vmio_release(), even
1362 * if B_DELWRI is set.
1363 *
1364 * If B_DELWRI is not set we may have to set B_RELBUF if we are low
1365 * on pages to return pages to the VM page queues.
1366 */
283b9448 1367 if ((bp->b_flags & (B_DELWRI | B_LOCKED)) || bp->b_refs) {
984263bc 1368 bp->b_flags &= ~B_RELBUF;
55b50bd5 1369 } else if (vm_page_count_min(0)) {
77912481 1370 if (LIST_FIRST(&bp->b_dep) != NULL)
78a9b77f 1371 buf_deallocate(bp); /* can set B_LOCKED */
4b958e7b
MD
1372 if (bp->b_flags & (B_DELWRI | B_LOCKED))
1373 bp->b_flags &= ~B_RELBUF;
1374 else
1375 bp->b_flags |= B_RELBUF;
1376 }
984263bc
MD
1377
1378 /*
78a9b77f
MD
1379 * Make sure b_cmd is clear. It may have already been cleared by
1380 * biodone().
1381 *
9188c711
MD
1382 * At this point destroying the buffer is governed by the B_INVAL
1383 * or B_RELBUF flags.
1384 */
10f3fee5 1385 bp->b_cmd = BUF_CMD_DONE;
aa166ad1 1386 dsched_exit_buf(bp);
9188c711
MD
1387
1388 /*
135bd6a8
MD
1389 * VMIO buffer rundown. Make sure the VM page array is restored
1390 * after an I/O may have replaces some of the pages with bogus pages
1391 * in order to not destroy dirty pages in a fill-in read.
1392 *
1393 * Note that due to the code above, if a buffer is marked B_DELWRI
1394 * then the B_RELBUF and B_NOCACHE bits will always be clear.
1395 * B_INVAL may still be set, however.
984263bc 1396 *
135bd6a8
MD
1397 * For clean buffers, B_INVAL or B_RELBUF will destroy the buffer
1398 * but not the backing store. B_NOCACHE will destroy the backing
1399 * store.
984263bc 1400 *
135bd6a8
MD
1401 * Note that dirty NFS buffers contain byte-granular write ranges
1402 * and should not be destroyed w/ B_INVAL even if the backing store
1403 * is left intact.
984263bc 1404 */
135bd6a8 1405 if (bp->b_flags & B_VMIO) {
9188c711
MD
1406 /*
1407 * Rundown for VMIO buffers which are not dirty NFS buffers.
1408 */
984263bc
MD
1409 int i, j, resid;
1410 vm_page_t m;
1411 off_t foff;
1412 vm_pindex_t poff;
1413 vm_object_t obj;
1414 struct vnode *vp;
1415
1416 vp = bp->b_vp;
1417
1418 /*
1419 * Get the base offset and length of the buffer. Note that
1420 * in the VMIO case if the buffer block size is not
1421 * page-aligned then b_data pointer may not be page-aligned.
236b2b9f 1422 * But our b_xio.xio_pages array *IS* page aligned.
984263bc
MD
1423 *
1424 * block sizes less then DEV_BSIZE (usually 512) are not
1425 * supported due to the page granularity bits (m->valid,
1426 * m->dirty, etc...).
1427 *
1428 * See man buf(9) for more information
1429 */
1430
1431 resid = bp->b_bufsize;
81b5c339 1432 foff = bp->b_loffset;
984263bc 1433
54f51aeb
HP
1434 for (i = 0; i < bp->b_xio.xio_npages; i++) {
1435 m = bp->b_xio.xio_pages[i];
984263bc
MD
1436 vm_page_flag_clear(m, PG_ZERO);
1437 /*
1438 * If we hit a bogus page, fixup *all* of them
06ecca5a
MD
1439 * now. Note that we left these pages wired
1440 * when we removed them so they had better exist,
1441 * and they cannot be ripped out from under us so
e43a034f 1442 * no critical section protection is necessary.
984263bc
MD
1443 */
1444 if (m == bogus_page) {
7540ab49 1445 obj = vp->v_object;
81b5c339 1446 poff = OFF_TO_IDX(bp->b_loffset);
984263bc 1447
b12defdc 1448 vm_object_hold(obj);
54f51aeb 1449 for (j = i; j < bp->b_xio.xio_npages; j++) {
984263bc
MD
1450 vm_page_t mtmp;
1451
54f51aeb 1452 mtmp = bp->b_xio.xio_pages[j];
984263bc
MD
1453 if (mtmp == bogus_page) {
1454 mtmp = vm_page_lookup(obj, poff + j);
1455 if (!mtmp) {
fc92d4aa 1456 panic("brelse: page missing");
984263bc 1457 }
54f51aeb 1458 bp->b_xio.xio_pages[j] = mtmp;
984263bc
MD
1459 }
1460 }
e158420c 1461 bp->b_flags &= ~B_HASBOGUS;
b12defdc 1462 vm_object_drop(obj);
984263bc
MD
1463
1464 if ((bp->b_flags & B_INVAL) == 0) {
54f51aeb
HP
1465 pmap_qenter(trunc_page((vm_offset_t)bp->b_data),
1466 bp->b_xio.xio_pages, bp->b_xio.xio_npages);
984263bc 1467 }
54f51aeb 1468 m = bp->b_xio.xio_pages[i];
984263bc 1469 }
8d429613
MD
1470
1471 /*
1472 * Invalidate the backing store if B_NOCACHE is set
1473 * (e.g. used with vinvalbuf()). If this is NFS
1474 * we impose a requirement that the block size be
1475 * a multiple of PAGE_SIZE and create a temporary
1476 * hack to basically invalidate the whole page. The
1477 * problem is that NFS uses really odd buffer sizes
1478 * especially when tracking piecemeal writes and
1479 * it also vinvalbuf()'s a lot, which would result
1480 * in only partial page validation and invalidation
1481 * here. If the file page is mmap()'d, however,
1482 * all the valid bits get set so after we invalidate
1483 * here we would end up with weird m->valid values
1484 * like 0xfc. nfs_getpages() can't handle this so
1485 * we clear all the valid bits for the NFS case
1486 * instead of just some of them.
1487 *
1488 * The real bug is the VM system having to set m->valid
1489 * to VM_PAGE_BITS_ALL for faulted-in pages, which
1490 * itself is an artifact of the whole 512-byte
1491 * granular mess that exists to support odd block
1492 * sizes and UFS meta-data block sizes (e.g. 6144).
1493 * A complete rewrite is required.
cb1cf930
MD
1494 *
1495 * XXX
8d429613 1496 */
984263bc
MD
1497 if (bp->b_flags & (B_NOCACHE|B_ERROR)) {
1498 int poffset = foff & PAGE_MASK;
8d429613
MD
1499 int presid;
1500
1501 presid = PAGE_SIZE - poffset;
1502 if (bp->b_vp->v_tag == VT_NFS &&
1503 bp->b_vp->v_type == VREG) {
1504 ; /* entire page */
1505 } else if (presid > resid) {
1506 presid = resid;
1507 }
984263bc
MD
1508 KASSERT(presid >= 0, ("brelse: extra page"));
1509 vm_page_set_invalid(m, poffset, presid);
c504e38e
MD
1510
1511 /*
1512 * Also make sure any swap cache is removed
1513 * as it is now stale (HAMMER in particular
1514 * uses B_NOCACHE to deal with buffer
1515 * aliasing).
1516 */
1517 swap_pager_unswapped(m);
984263bc
MD
1518 }
1519 resid -= PAGE_SIZE - (foff & PAGE_MASK);
1520 foff = (foff + PAGE_SIZE) & ~(off_t)PAGE_MASK;
1521 }
984263bc
MD
1522 if (bp->b_flags & (B_INVAL | B_RELBUF))
1523 vfs_vmio_release(bp);
9188c711
MD
1524 } else {
1525 /*
1526 * Rundown for non-VMIO buffers.
1527 */
1528 if (bp->b_flags & (B_INVAL | B_RELBUF)) {
9188c711
MD
1529 if (bp->b_bufsize)
1530 allocbuf(bp, 0);
f9a11477 1531 KKASSERT (LIST_FIRST(&bp->b_dep) == NULL);
9188c711
MD
1532 if (bp->b_vp)
1533 brelvp(bp);
1534 }
984263bc
MD
1535 }
1536
b3098c79 1537 if (bp->b_qindex != BQUEUE_NONE)
984263bc 1538 panic("brelse: free buffer onto another queue???");
77bb9400 1539 if (BUF_REFCNTNB(bp) > 1) {
984263bc
MD
1540 /* Temporary panic to verify exclusive locking */
1541 /* This panic goes away when we allow shared refs */
1542 panic("brelse: multiple refs");
b1c20cfa 1543 /* NOT REACHED */
984263bc
MD
1544 return;
1545 }
1546
9188c711
MD
1547 /*
1548 * Figure out the correct queue to place the cleaned up buffer on.
1549 * Buffers placed in the EMPTY or EMPTYKVA had better already be
1550 * disassociated from their vnode.
1551 */
287a8577 1552 spin_lock(&bufqspin);
408357d8
MD
1553 if (bp->b_flags & B_LOCKED) {
1554 /*
27bc0cb1
MD
1555 * Buffers that are locked are placed in the locked queue
1556 * immediately, regardless of their state.
408357d8 1557 */
27bc0cb1
MD
1558 bp->b_qindex = BQUEUE_LOCKED;
1559 TAILQ_INSERT_TAIL(&bufqueues[BQUEUE_LOCKED], bp, b_freelist);
408357d8 1560 } else if (bp->b_bufsize == 0) {
9188c711
MD
1561 /*
1562 * Buffers with no memory. Due to conditionals near the top
1563 * of brelse() such buffers should probably already be
1564 * marked B_INVAL and disassociated from their vnode.
1565 */
984263bc 1566 bp->b_flags |= B_INVAL;
54078292 1567 KASSERT(bp->b_vp == NULL, ("bp1 %p flags %08x/%08x vnode %p unexpectededly still associated!", bp, saved_flags, bp->b_flags, bp->b_vp));
1f1ea522 1568 KKASSERT((bp->b_flags & B_HASHED) == 0);
984263bc 1569 if (bp->b_kvasize) {
b3098c79 1570 bp->b_qindex = BQUEUE_EMPTYKVA;
984263bc 1571 } else {
b3098c79 1572 bp->b_qindex = BQUEUE_EMPTY;
984263bc
MD
1573 }
1574 TAILQ_INSERT_HEAD(&bufqueues[bp->b_qindex], bp, b_freelist);
78a9b77f 1575 } else if (bp->b_flags & (B_INVAL | B_NOCACHE | B_RELBUF)) {
9188c711
MD
1576 /*
1577 * Buffers with junk contents. Again these buffers had better
1578 * already be disassociated from their vnode.
1579 */
54078292 1580 KASSERT(bp->b_vp == NULL, ("bp2 %p flags %08x/%08x vnode %p unexpectededly still associated!", bp, saved_flags, bp->b_flags, bp->b_vp));
1f1ea522 1581 KKASSERT((bp->b_flags & B_HASHED) == 0);
984263bc 1582 bp->b_flags |= B_INVAL;
b3098c79
HP
1583 bp->b_qindex = BQUEUE_CLEAN;
1584 TAILQ_INSERT_HEAD(&bufqueues[BQUEUE_CLEAN], bp, b_freelist);
984263bc 1585 } else {
9188c711
MD
1586 /*
1587 * Remaining buffers. These buffers are still associated with
1588 * their vnode.
1589 */
b86460bf 1590 switch(bp->b_flags & (B_DELWRI|B_HEAVY)) {
984263bc 1591 case B_DELWRI:
b3098c79
HP
1592 bp->b_qindex = BQUEUE_DIRTY;
1593 TAILQ_INSERT_TAIL(&bufqueues[BQUEUE_DIRTY], bp, b_freelist);
984263bc 1594 break;
4b958e7b
MD
1595 case B_DELWRI | B_HEAVY:
1596 bp->b_qindex = BQUEUE_DIRTY_HW;
1597 TAILQ_INSERT_TAIL(&bufqueues[BQUEUE_DIRTY_HW], bp,
1598 b_freelist);
1599 break;
984263bc 1600 default:
b86460bf
MD
1601 /*
1602 * NOTE: Buffers are always placed at the end of the
1603 * queue. If B_AGE is not set the buffer will cycle
1604 * through the queue twice.
1605 */
b3098c79
HP
1606 bp->b_qindex = BQUEUE_CLEAN;
1607 TAILQ_INSERT_TAIL(&bufqueues[BQUEUE_CLEAN], bp, b_freelist);
984263bc
MD
1608 break;
1609 }
1610 }
287a8577 1611 spin_unlock(&bufqspin);
984263bc
MD
1612
1613 /*
1614 * If B_INVAL, clear B_DELWRI. We've already placed the buffer
1615 * on the correct queue.
1616 */
1617 if ((bp->b_flags & (B_INVAL|B_DELWRI)) == (B_INVAL|B_DELWRI))
1618 bundirty(bp);
1619
1620 /*
868d24af
MD
1621 * The bp is on an appropriate queue unless locked. If it is not
1622 * locked or dirty we can wakeup threads waiting for buffer space.
1623 *
984263bc
MD
1624 * We've already handled the B_INVAL case ( B_DELWRI will be clear
1625 * if B_INVAL is set ).
1626 */
408357d8 1627 if ((bp->b_flags & (B_LOCKED|B_DELWRI)) == 0)
984263bc
MD
1628 bufcountwakeup();
1629
1630 /*
1631 * Something we can maybe free or reuse
1632 */
1633 if (bp->b_bufsize || bp->b_kvasize)
1634 bufspacewakeup();
1635
69f8c926
MD
1636 /*
1637 * Clean up temporary flags and unlock the buffer.
1638 */
ae8e83e6 1639 bp->b_flags &= ~(B_ORDERED | B_NOCACHE | B_RELBUF | B_DIRECT);
69f8c926 1640 BUF_UNLOCK(bp);
984263bc
MD
1641}
1642
1643/*
3f779080
HP
1644 * bqrelse:
1645 *
1646 * Release a buffer back to the appropriate queue but do not try to free
1647 * it. The buffer is expected to be used again soon.
984263bc 1648 *
3f779080
HP
1649 * bqrelse() is used by bdwrite() to requeue a delayed write, and used by
1650 * biodone() to requeue an async I/O on completion. It is also used when
1651 * known good buffers need to be requeued but we think we may need the data
1652 * again soon.
984263bc 1653 *
3f779080 1654 * XXX we should be able to leave the B_RELBUF hint set on completion.
b1c20cfa
MD
1655 *
1656 * MPSAFE
984263bc
MD
1657 */
1658void
c8e4131d 1659bqrelse(struct buf *bp)
984263bc 1660{
984263bc
MD
1661 KASSERT(!(bp->b_flags & (B_CLUSTER|B_PAGING)), ("bqrelse: inappropriate B_PAGING or B_CLUSTER bp %p", bp));
1662
b3098c79 1663 if (bp->b_qindex != BQUEUE_NONE)
984263bc 1664 panic("bqrelse: free buffer onto another queue???");
77bb9400 1665 if (BUF_REFCNTNB(bp) > 1) {
984263bc
MD
1666 /* do not release to free list */
1667 panic("bqrelse: multiple refs");
984263bc
MD
1668 return;
1669 }
c3d1e862 1670
0e8bd897
MD
1671 buf_act_advance(bp);
1672
287a8577 1673 spin_lock(&bufqspin);
984263bc 1674 if (bp->b_flags & B_LOCKED) {
5e23ca53
MD
1675 /*
1676 * Locked buffers are released to the locked queue. However,
1677 * if the buffer is dirty it will first go into the dirty
1678 * queue and later on after the I/O completes successfully it
1679 * will be released to the locked queue.
1680 */
27bc0cb1
MD
1681 bp->b_qindex = BQUEUE_LOCKED;
1682 TAILQ_INSERT_TAIL(&bufqueues[BQUEUE_LOCKED], bp, b_freelist);
984263bc 1683 } else if (bp->b_flags & B_DELWRI) {
4b958e7b
MD
1684 bp->b_qindex = (bp->b_flags & B_HEAVY) ?
1685 BQUEUE_DIRTY_HW : BQUEUE_DIRTY;
1686 TAILQ_INSERT_TAIL(&bufqueues[bp->b_qindex], bp, b_freelist);
55b50bd5 1687 } else if (vm_page_count_min(0)) {
984263bc
MD
1688 /*
1689 * We are too low on memory, we have to try to free the
1690 * buffer (most importantly: the wired pages making up its
1691 * backing store) *now*.
1692 */
287a8577 1693 spin_unlock(&bufqspin);
984263bc
MD
1694 brelse(bp);
1695 return;
1696 } else {
b3098c79
HP
1697 bp->b_qindex = BQUEUE_CLEAN;
1698 TAILQ_INSERT_TAIL(&bufqueues[BQUEUE_CLEAN], bp, b_freelist);
984263bc 1699 }
287a8577 1700 spin_unlock(&bufqspin);
984263bc
MD
1701
1702 if ((bp->b_flags & B_LOCKED) == 0 &&
408357d8 1703 ((bp->b_flags & B_INVAL) || (bp->b_flags & B_DELWRI) == 0)) {
984263bc
MD
1704 bufcountwakeup();
1705 }
1706
1707 /*
1708 * Something we can maybe free or reuse.
1709 */
1710 if (bp->b_bufsize && !(bp->b_flags & B_DELWRI))
1711 bufspacewakeup();
1712
9188c711
MD
1713 /*
1714 * Final cleanup and unlock. Clear bits that are only used while a
1715 * buffer is actively locked.
1716 */
ae8e83e6 1717 bp->b_flags &= ~(B_ORDERED | B_NOCACHE | B_RELBUF);
aa166ad1 1718 dsched_exit_buf(bp);
9188c711 1719 BUF_UNLOCK(bp);
984263bc
MD
1720}
1721
3f779080 1722/*
283b9448
MD
1723 * Hold a buffer, preventing it from being reused. This will prevent
1724 * normal B_RELBUF operations on the buffer but will not prevent B_INVAL
1725 * operations. If a B_INVAL operation occurs the buffer will remain held
1726 * but the underlying pages may get ripped out.
1727 *
1728 * These functions are typically used in VOP_READ/VOP_WRITE functions
1729 * to hold a buffer during a copyin or copyout, preventing deadlocks
1730 * or recursive lock panics when read()/write() is used over mmap()'d
1731 * space.
1732 *
1733 * NOTE: bqhold() requires that the buffer be locked at the time of the
1734 * hold. bqdrop() has no requirements other than the buffer having
1735 * previously been held.
1736 */
1737void
1738bqhold(struct buf *bp)
1739{
1740 atomic_add_int(&bp->b_refs, 1);
1741}
1742
1743void
1744bqdrop(struct buf *bp)
1745{
1746 KKASSERT(bp->b_refs > 0);
1747 atomic_add_int(&bp->b_refs, -1);
1748}
1749
1750/*
55b50bd5
MD
1751 * Return backing pages held by the buffer 'bp' back to the VM system.
1752 * This routine is called when the bp is invalidated, released, or
1753 * reused.
1754 *
1755 * The KVA mapping (b_data) for the underlying pages is removed by
1756 * this function.
1757 *
1758 * WARNING! This routine is integral to the low memory critical path
1759 * when a buffer is B_RELBUF'd. If the system has a severe page
1760 * deficit we need to get the page(s) onto the PQ_FREE or PQ_CACHE
1761 * queues so they can be reused in the current pageout daemon
1762 * pass.
3f779080 1763 */
984263bc 1764static void
493c516a 1765vfs_vmio_release(struct buf *bp)
984263bc 1766{
e43a034f 1767 int i;
984263bc
MD
1768 vm_page_t m;
1769
54f51aeb
HP
1770 for (i = 0; i < bp->b_xio.xio_npages; i++) {
1771 m = bp->b_xio.xio_pages[i];
1772 bp->b_xio.xio_pages[i] = NULL;
0e8bd897 1773
55b50bd5
MD
1774 /*
1775 * We need to own the page in order to safely unwire it.
1776 */
b12defdc
MD
1777 vm_page_busy_wait(m, FALSE, "vmiopg");
1778
984263bc 1779 /*
b8a41159
MD
1780 * The VFS is telling us this is not a meta-data buffer
1781 * even if it is backed by a block device.
1782 */
1783 if (bp->b_flags & B_NOTMETA)
1784 vm_page_flag_set(m, PG_NOTMETA);
1785
1786 /*
0e8bd897
MD
1787 * This is a very important bit of code. We try to track
1788 * VM page use whether the pages are wired into the buffer
1789 * cache or not. While wired into the buffer cache the
1790 * bp tracks the act_count.
1791 *
1792 * We can choose to place unwired pages on the inactive
1793 * queue (0) or active queue (1). If we place too many
1794 * on the active queue the queue will cycle the act_count
1795 * on pages we'd like to keep, just from single-use pages
1796 * (such as when doing a tar-up or file scan).
984263bc 1797 */
0e8bd897
MD
1798 if (bp->b_act_count < vm_cycle_point)
1799 vm_page_unwire(m, 0);
1800 else
1801 vm_page_unwire(m, 1);
1802
984263bc 1803 /*
55b50bd5 1804 * If the wire_count has dropped to 0 we may need to take
3df0509c
MD
1805 * further action before unbusying the page.
1806 *
1807 * WARNING: vm_page_try_*() also checks PG_NEED_COMMIT for us.
984263bc 1808 */
984263bc
MD
1809 if (m->wire_count == 0) {
1810 vm_page_flag_clear(m, PG_ZERO);
55b50bd5 1811
ae8e83e6 1812 if (bp->b_flags & B_DIRECT) {
55b50bd5
MD
1813 /*
1814 * Attempt to free the page if B_DIRECT is
1815 * set, the caller does not desire the page
1816 * to be cached.
1817 */
b12defdc 1818 vm_page_wakeup(m);
984263bc 1819 vm_page_try_to_free(m);
17c8b5ef 1820 } else if ((bp->b_flags & B_NOTMETA) ||
55b50bd5
MD
1821 vm_page_count_min(0)) {
1822 /*
1823 * Attempt to move the page to PQ_CACHE
1824 * if B_NOTMETA is set. This flag is set
1825 * by HAMMER to remove one of the two pages
1826 * present when double buffering is enabled.
1827 *
1828 * Attempt to move the page to PQ_CACHE
1829 * If we have a severe page deficit. This
1830 * will cause buffer cache operations related
1831 * to pageouts to recycle the related pages
1832 * in order to avoid a low memory deadlock.
1833 */
0e8bd897 1834 m->act_count = bp->b_act_count;
b12defdc 1835 vm_page_wakeup(m);
984263bc 1836 vm_page_try_to_cache(m);
0e8bd897 1837 } else {
55b50bd5
MD
1838 /*
1839 * Nominal case, leave the page on the
1840 * queue the original unwiring placed it on
1841 * (active or inactive).
1842 */
0e8bd897 1843 m->act_count = bp->b_act_count;
b12defdc 1844 vm_page_wakeup(m);
984263bc 1845 }
b12defdc
MD
1846 } else {
1847 vm_page_wakeup(m);
984263bc
MD
1848 }
1849 }
77912481
MD
1850
1851 pmap_qremove(trunc_page((vm_offset_t) bp->b_data),
1852 bp->b_xio.xio_npages);
984263bc
MD
1853 if (bp->b_bufsize) {
1854 bufspacewakeup();
1855 bp->b_bufsize = 0;
1856 }
54f51aeb 1857 bp->b_xio.xio_npages = 0;
984263bc 1858 bp->b_flags &= ~B_VMIO;
f9a11477 1859 KKASSERT (LIST_FIRST(&bp->b_dep) == NULL);
77912481 1860 if (bp->b_vp)
984263bc
MD
1861 brelvp(bp);
1862}
1863
1864/*
3f779080 1865 * getnewbuf:
984263bc
MD
1866 *
1867 * Find and initialize a new buffer header, freeing up existing buffers
1868 * in the bufqueues as necessary. The new buffer is returned locked.
1869 *
1870 * Important: B_INVAL is not set. If the caller wishes to throw the
1871 * buffer away, the caller must set B_INVAL prior to calling brelse().
1872 *
1873 * We block if:
1874 * We have insufficient buffer headers
1875 * We have insufficient buffer space
1876 * buffer_map is too fragmented ( space reservation fails )
1877 * If we have to flush dirty buffers ( but we try to avoid this )
1878 *
1879 * To avoid VFS layer recursion we do not flush dirty buffers ourselves.
1880 * Instead we ask the buf daemon to do it for us. We attempt to
1881 * avoid piecemeal wakeups of the pageout daemon.
b1c20cfa
MD
1882 *
1883 * MPALMOSTSAFE
984263bc 1884 */
e0fb398b 1885struct buf *
4b958e7b 1886getnewbuf(int blkflags, int slptimeo, int size, int maxsize)
984263bc
MD
1887{
1888 struct buf *bp;
1889 struct buf *nbp;
1890 int defrag = 0;
1891 int nqindex;
4b958e7b 1892 int slpflags = (blkflags & GETBLK_PCATCH) ? PCATCH : 0;
984263bc
MD
1893 static int flushingbufs;
1894
1895 /*
1896 * We can't afford to block since we might be holding a vnode lock,
1897 * which may prevent system daemons from running. We deal with
1898 * low-memory situations by proactively returning memory and running
1899 * async I/O rather then sync I/O.
1900 */
1901
1902 ++getnewbufcalls;
1903 --getnewbufrestarts;
1904restart:
1905 ++getnewbufrestarts;
1906
1907 /*
1908 * Setup for scan. If we do not have enough free buffers,
1909 * we setup a degenerate case that immediately fails. Note
1910 * that if we are specially marked process, we are allowed to
1911 * dip into our reserves.
1912 *
1913 * The scanning sequence is nominally: EMPTY->EMPTYKVA->CLEAN
1914 *
1915 * We start with EMPTYKVA. If the list is empty we backup to EMPTY.
1916 * However, there are a number of cases (defragging, reusing, ...)
1917 * where we cannot backup.
1918 */
b3098c79 1919 nqindex = BQUEUE_EMPTYKVA;
287a8577 1920 spin_lock(&bufqspin);
b3098c79 1921 nbp = TAILQ_FIRST(&bufqueues[BQUEUE_EMPTYKVA]);
984263bc
MD
1922
1923 if (nbp == NULL) {
1924 /*
1925 * If no EMPTYKVA buffers and we are either
1926 * defragging or reusing, locate a CLEAN buffer
1927 * to free or reuse. If bufspace useage is low
1928 * skip this step so we can allocate a new buffer.
1929 */
1930 if (defrag || bufspace >= lobufspace) {
b3098c79
HP
1931 nqindex = BQUEUE_CLEAN;
1932 nbp = TAILQ_FIRST(&bufqueues[BQUEUE_CLEAN]);
984263bc
MD
1933 }
1934
1935 /*
1936 * If we could not find or were not allowed to reuse a
1937 * CLEAN buffer, check to see if it is ok to use an EMPTY
1938 * buffer. We can only use an EMPTY buffer if allocating
1939 * its KVA would not otherwise run us out of buffer space.
1940 */
1941 if (nbp == NULL && defrag == 0 &&
1942 bufspace + maxsize < hibufspace) {
b3098c79
HP
1943 nqindex = BQUEUE_EMPTY;
1944 nbp = TAILQ_FIRST(&bufqueues[BQUEUE_EMPTY]);
984263bc
MD
1945 }
1946 }
1947
1948 /*
1949 * Run scan, possibly freeing data and/or kva mappings on the fly
1950 * depending.
c3d1e862 1951 *
77912481 1952 * WARNING! bufqspin is held!
984263bc 1953 */
984263bc
MD
1954 while ((bp = nbp) != NULL) {
1955 int qindex = nqindex;
1956
b86460bf
MD
1957 nbp = TAILQ_NEXT(bp, b_freelist);
1958
1959 /*
1960 * BQUEUE_CLEAN - B_AGE special case. If not set the bp
1961 * cycles through the queue twice before being selected.
1962 */
1963 if (qindex == BQUEUE_CLEAN &&
1964 (bp->b_flags & B_AGE) == 0 && nbp) {
1965 bp->b_flags |= B_AGE;
1966 TAILQ_REMOVE(&bufqueues[qindex], bp, b_freelist);
1967 TAILQ_INSERT_TAIL(&bufqueues[qindex], bp, b_freelist);
1968 continue;
1969 }
1970
984263bc
MD
1971 /*
1972 * Calculate next bp ( we can only use it if we do not block
1973 * or do other fancy things ).
1974 */
b86460bf 1975 if (nbp == NULL) {
984263bc 1976 switch(qindex) {
b3098c79
HP
1977 case BQUEUE_EMPTY:
1978 nqindex = BQUEUE_EMPTYKVA;
1979 if ((nbp = TAILQ_FIRST(&bufqueues[BQUEUE_EMPTYKVA])))
984263bc
MD
1980 break;
1981 /* fall through */
b3098c79
HP
1982 case BQUEUE_EMPTYKVA:
1983 nqindex = BQUEUE_CLEAN;
1984 if ((nbp = TAILQ_FIRST(&bufqueues[BQUEUE_CLEAN])))
984263bc
MD
1985 break;
1986 /* fall through */
b3098c79 1987 case BQUEUE_CLEAN:
984263bc
MD
1988 /*
1989 * nbp is NULL.
1990 */
1991 break;
1992 }
1993 }
1994
1995 /*
1996 * Sanity Checks
1997 */
7e8888ce
MD
1998 KASSERT(bp->b_qindex == qindex,
1999 ("getnewbuf: inconsistent queue %d bp %p", qindex, bp));
984263bc
MD
2000
2001 /*
2002 * Note: we no longer distinguish between VMIO and non-VMIO
2003 * buffers.
2004 */
77912481
MD
2005 KASSERT((bp->b_flags & B_DELWRI) == 0,
2006 ("delwri buffer %p found in queue %d", bp, qindex));
984263bc 2007
77912481
MD
2008 /*
2009 * Do not try to reuse a buffer with a non-zero b_refs.
2010 * This is an unsynchronized test. A synchronized test
2011 * is also performed after we lock the buffer.
2012 */
2013 if (bp->b_refs)
2014 continue;
984263bc
MD
2015
2016 /*
2017 * If we are defragging then we need a buffer with
2018 * b_kvasize != 0. XXX this situation should no longer
2019 * occur, if defrag is non-zero the buffer's b_kvasize
2020 * should also be non-zero at this point. XXX
2021 */
2022 if (defrag && bp->b_kvasize == 0) {
6ea70f76 2023 kprintf("Warning: defrag empty buffer %p\n", bp);
984263bc
MD
2024 continue;
2025 }
2026
2027 /*
2028 * Start freeing the bp. This is somewhat involved. nbp
b3098c79 2029 * remains valid only for BQUEUE_EMPTY[KVA] bp's. Buffers
9188c711
MD
2030 * on the clean list must be disassociated from their
2031 * current vnode. Buffers on the empty[kva] lists have
2032 * already been disassociated.
283b9448
MD
2033 *
2034 * b_refs is checked after locking along with queue changes.
2035 * We must check here to deal with zero->nonzero transitions
2036 * made by the owner of the buffer lock, which is used by
2037 * VFS's to hold the buffer while issuing an unlocked
2038 * uiomove()s. We cannot invalidate the buffer's pages
2039 * for this case. Once we successfully lock a buffer the
2040 * only 0->1 transitions of b_refs will occur via findblk().
2041 *
2042 * We must also check for queue changes after successful
2043 * locking as the current lock holder may dispose of the
2044 * buffer and change its queue.
984263bc 2045 */
d9dba6f6 2046 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
287a8577 2047 spin_unlock(&bufqspin);
7e8888ce 2048 tsleep(&bd_request, 0, "gnbxxx", (hz + 99) / 100);
d9dba6f6
MD
2049 goto restart;
2050 }
283b9448 2051 if (bp->b_qindex != qindex || bp->b_refs) {
287a8577 2052 spin_unlock(&bufqspin);
d9dba6f6
MD
2053 BUF_UNLOCK(bp);
2054 goto restart;
2055 }
c3d1e862 2056 bremfree_locked(bp);
287a8577 2057 spin_unlock(&bufqspin);
984263bc 2058
408357d8
MD
2059 /*
2060 * Dependancies must be handled before we disassociate the
2061 * vnode.
2062 *
2063 * NOTE: HAMMER will set B_LOCKED if the buffer cannot
2064 * be immediately disassociated. HAMMER then becomes
2065 * responsible for releasing the buffer.
c3d1e862 2066 *
77912481 2067 * NOTE: bufqspin is UNLOCKED now.
408357d8
MD
2068 */
2069 if (LIST_FIRST(&bp->b_dep) != NULL) {
2070 buf_deallocate(bp);
2071 if (bp->b_flags & B_LOCKED) {
2072 bqrelse(bp);
2073 goto restart;
2074 }
4b958e7b 2075 KKASSERT(LIST_FIRST(&bp->b_dep) == NULL);
408357d8
MD
2076 }
2077
b3098c79 2078 if (qindex == BQUEUE_CLEAN) {
77912481 2079 if (bp->b_flags & B_VMIO)
984263bc 2080 vfs_vmio_release(bp);
984263bc
MD
2081 if (bp->b_vp)
2082 brelvp(bp);
2083 }
2084
2085 /*
2086 * NOTE: nbp is now entirely invalid. We can only restart
2087 * the scan from this point on.
2088 *
2089 * Get the rest of the buffer freed up. b_kva* is still
2090 * valid after this operation.
2091 */
283b9448
MD
2092 KASSERT(bp->b_vp == NULL,
2093 ("bp3 %p flags %08x vnode %p qindex %d "
2094 "unexpectededly still associated!",
2095 bp, bp->b_flags, bp->b_vp, qindex));
1f1ea522 2096 KKASSERT((bp->b_flags & B_HASHED) == 0);
984263bc 2097
06ecca5a 2098 /*
e43a034f
MD
2099 * critical section protection is not required when
2100 * scrapping a buffer's contents because it is already
2101 * wired.
06ecca5a 2102 */
77912481 2103 if (bp->b_bufsize)
984263bc
MD
2104 allocbuf(bp, 0);
2105
4414f2c9 2106 bp->b_flags = B_BNOCLIP;
10f3fee5 2107 bp->b_cmd = BUF_CMD_DONE;
984263bc 2108 bp->b_vp = NULL;
984263bc
MD
2109 bp->b_error = 0;
2110 bp->b_resid = 0;
2111 bp->b_bcount = 0;
54f51aeb 2112 bp->b_xio.xio_npages = 0;
984263bc 2113 bp->b_dirtyoff = bp->b_dirtyend = 0;
0e8bd897 2114 bp->b_act_count = ACT_INIT;
81b5c339 2115 reinitbufbio(bp);
b86460bf 2116 KKASSERT(LIST_FIRST(&bp->b_dep) == NULL);
408357d8 2117 buf_dep_init(bp);
4b958e7b
MD
2118 if (blkflags & GETBLK_BHEAVY)
2119 bp->b_flags |= B_HEAVY;
984263bc
MD
2120
2121 /*
2122 * If we are defragging then free the buffer.
2123 */
2124 if (defrag) {
2125 bp->b_flags |= B_INVAL;
2126 bfreekva(bp);
2127 brelse(bp);
2128 defrag = 0;
2129 goto restart;
2130 }
2131
2132 /*
2133 * If we are overcomitted then recover the buffer and its
2134 * KVM space. This occurs in rare situations when multiple
2135 * processes are blocked in getnewbuf() or allocbuf().
2136 */
2137 if (bufspace >= hibufspace)
2138 flushingbufs = 1;
2139 if (flushingbufs && bp->b_kvasize != 0) {
2140 bp->b_flags |= B_INVAL;
2141 bfreekva(bp);
2142 brelse(bp);
2143 goto restart;
2144 }
2145 if (bufspace < lobufspace)
2146 flushingbufs = 0;
77912481
MD
2147
2148 /*
283b9448
MD
2149 * b_refs can transition to a non-zero value while we hold
2150 * the buffer locked due to a findblk(). Our brelvp() above
2151 * interlocked any future possible transitions due to
2152 * findblk()s.
2153 *
2154 * If we find b_refs to be non-zero we can destroy the
2155 * buffer's contents but we cannot yet reuse the buffer.
77912481
MD
2156 */
2157 if (bp->b_refs) {
2158 bp->b_flags |= B_INVAL;
2159 bfreekva(bp);
2160 brelse(bp);
2161 goto restart;
2162 }
984263bc 2163 break;
77912481 2164 /* NOT REACHED, bufqspin not held */
984263bc
MD
2165 }
2166
2167 /*
2168 * If we exhausted our list, sleep as appropriate. We may have to
2169 * wakeup various daemons and write out some dirty buffers.
2170 *
2171 * Generally we are sleeping due to insufficient buffer space.
c3d1e862 2172 *
77912481 2173 * NOTE: bufqspin is held if bp is NULL, else it is not held.
984263bc 2174 */
984263bc
MD
2175 if (bp == NULL) {
2176 int flags;
2177 char *waitmsg;
2178
287a8577 2179 spin_unlock(&bufqspin);
984263bc
MD
2180 if (defrag) {
2181 flags = VFS_BIO_NEED_BUFSPACE;
2182 waitmsg = "nbufkv";
2183 } else if (bufspace >= hibufspace) {
2184 waitmsg = "nbufbs";
2185 flags = VFS_BIO_NEED_BUFSPACE;
2186 } else {
2187 waitmsg = "newbuf";
2188 flags = VFS_BIO_NEED_ANY;
2189 }
2190
4b958e7b 2191 bd_speedup(); /* heeeelp */
287a8577 2192 spin_lock(&bufcspin);
77912481 2193 needsbuffer |= flags;
984263bc 2194 while (needsbuffer & flags) {
77912481
MD
2195 if (ssleep(&needsbuffer, &bufcspin,
2196 slpflags, waitmsg, slptimeo)) {
287a8577 2197 spin_unlock(&bufcspin);
984263bc 2198 return (NULL);
77912481 2199 }
984263bc 2200 }
287a8577 2201 spin_unlock(&bufcspin);
984263bc
MD
2202 } else {
2203 /*
2204 * We finally have a valid bp. We aren't quite out of the
2205 * woods, we still have to reserve kva space. In order
2206 * to keep fragmentation sane we only allocate kva in
2207 * BKVASIZE chunks.
c3d1e862 2208 *
77912481 2209 * (bufqspin is not held)
984263bc
MD
2210 */
2211 maxsize = (maxsize + BKVAMASK) & ~BKVAMASK;
2212
2213 if (maxsize != bp->b_kvasize) {
2214 vm_offset_t addr = 0;
a108bf71 2215 int count;
984263bc
MD
2216
2217 bfreekva(bp);
2218
a108bf71 2219 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
e4846942 2220 vm_map_lock(&buffer_map);
984263bc 2221
e4846942
MD
2222 if (vm_map_findspace(&buffer_map,
2223 vm_map_min(&buffer_map), maxsize,
c809941b 2224 maxsize, 0, &addr)) {
984263bc 2225 /*
3f779080 2226 * Uh oh. Buffer map is too fragmented. We
984263bc
MD
2227 * must defragment the map.
2228 */
e4846942 2229 vm_map_unlock(&buffer_map);
a108bf71 2230 vm_map_entry_release(count);
984263bc
MD
2231 ++bufdefragcnt;
2232 defrag = 1;
2233 bp->b_flags |= B_INVAL;
2234 brelse(bp);
2235 goto restart;
2236 }
2237 if (addr) {
e4846942 2238 vm_map_insert(&buffer_map, &count,
a108bf71 2239 NULL, 0,
984263bc 2240 addr, addr + maxsize,
1b874851
MD
2241 VM_MAPTYPE_NORMAL,
2242 VM_PROT_ALL, VM_PROT_ALL,
2243 MAP_NOFAULT);
984263bc
MD
2244
2245 bp->b_kvabase = (caddr_t) addr;
2246 bp->b_kvasize = maxsize;
2247 bufspace += bp->b_kvasize;
2248 ++bufreusecnt;
2249 }
e4846942 2250 vm_map_unlock(&buffer_map);
a108bf71 2251 vm_map_entry_release(count);
984263bc
MD
2252 }
2253 bp->b_data = bp->b_kvabase;
2254 }
2255 return(bp);
2256}
2257
55b50bd5 2258#if 0
984263bc 2259/*
4ecf7cc9
MD
2260 * This routine is called in an emergency to recover VM pages from the
2261 * buffer cache by cashing in clean buffers. The idea is to recover
2262 * enough pages to be able to satisfy a stuck bio_page_alloc().
77912481 2263 *
55b50bd5
MD
2264 * XXX Currently not implemented. This function can wind up deadlocking
2265 * against another thread holding one or more of the backing pages busy.
4ecf7cc9
MD
2266 */
2267static int
2268recoverbufpages(void)
2269{
2270 struct buf *bp;
2271 int bytes = 0;
2272
2273 ++recoverbufcalls;
2274
287a8577 2275 spin_lock(&bufqspin);
4ecf7cc9
MD
2276 while (bytes < MAXBSIZE) {
2277 bp = TAILQ_FIRST(&bufqueues[BQUEUE_CLEAN]);
2278 if (bp == NULL)
2279 break;
2280
2281 /*
2282 * BQUEUE_CLEAN - B_AGE special case. If not set the bp
2283 * cycles through the queue twice before being selected.
2284 */
2285 if ((bp->b_flags & B_AGE) == 0 && TAILQ_NEXT(bp, b_freelist)) {
2286 bp->b_flags |= B_AGE;
2287 TAILQ_REMOVE(&bufqueues[BQUEUE_CLEAN], bp, b_freelist);
2288 TAILQ_INSERT_TAIL(&bufqueues[BQUEUE_CLEAN],
2289 bp, b_freelist);
2290 continue;
2291 }
2292
2293 /*
2294 * Sanity Checks
2295 */
2296 KKASSERT(bp->b_qindex == BQUEUE_CLEAN);
2297 KKASSERT((bp->b_flags & B_DELWRI) == 0);
2298
2299 /*
2300 * Start freeing the bp. This is somewhat involved.
2301 *
2302 * Buffers on the clean list must be disassociated from
2303 * their current vnode
2304 */
2305
2306 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
77912481
MD
2307 kprintf("recoverbufpages: warning, locked buf %p, "
2308 "race corrected\n",
2309 bp);
2310 ssleep(&bd_request, &bufqspin, 0, "gnbxxx", hz / 100);
4ecf7cc9
MD
2311 continue;
2312 }
2313 if (bp->b_qindex != BQUEUE_CLEAN) {
77912481
MD
2314 kprintf("recoverbufpages: warning, BUF_LOCK blocked "
2315 "unexpectedly on buf %p index %d, race "
2316 "corrected\n",
2317 bp, bp->b_qindex);
4ecf7cc9
MD
2318 BUF_UNLOCK(bp);
2319 continue;
2320 }
c3d1e862 2321 bremfree_locked(bp);
287a8577 2322 spin_unlock(&bufqspin);
4ecf7cc9
MD
2323
2324 /*
9adb9c71
MD
2325 * Sanity check. Only BQUEUE_DIRTY[_HW] employs markers.
2326 */
2327 KKASSERT((bp->b_flags & B_MARKER) == 0);
2328
2329 /*
4ecf7cc9
MD
2330 * Dependancies must be handled before we disassociate the
2331 * vnode.
2332 *
2333 * NOTE: HAMMER will set B_LOCKED if the buffer cannot
2334 * be immediately disassociated. HAMMER then becomes
2335 * responsible for releasing the buffer.
2336 */
2337 if (LIST_FIRST(&bp->b_dep) != NULL) {
2338 buf_deallocate(bp);
2339 if (bp->b_flags & B_LOCKED) {
2340 bqrelse(bp);
287a8577 2341 spin_lock(&bufqspin);
4ecf7cc9
MD
2342 continue;
2343 }
2344 KKASSERT(LIST_FIRST(&bp->b_dep) == NULL);
2345 }
2346
2347 bytes += bp->b_bufsize;
2348
2349 if (bp->b_flags & B_VMIO) {
4ecf7cc9
MD
2350 bp->b_flags |= B_DIRECT; /* try to free pages */
2351 vfs_vmio_release(bp);
2352 }
2353 if (bp->b_vp)
2354 brelvp(bp);
2355
2356 KKASSERT(bp->b_vp == NULL);
2357 KKASSERT((bp->b_flags & B_HASHED) == 0);
2358
2359 /*
2360 * critical section protection is not required when
2361 * scrapping a buffer's contents because it is already
2362 * wired.
2363 */
2364 if (bp->b_bufsize)
2365 allocbuf(bp, 0);
2366
2367 bp->b_flags = B_BNOCLIP;
2368 bp->b_cmd = BUF_CMD_DONE;
2369 bp->b_vp = NULL;
2370 bp->b_error = 0;
2371 bp->b_resid = 0;
2372 bp->b_bcount = 0;
2373 bp->b_xio.xio_npages = 0;
2374 bp->b_dirtyoff = bp->b_dirtyend = 0;
2375 reinitbufbio(bp);
2376 KKASSERT(LIST_FIRST(&bp->b_dep) == NULL);
2377 buf_dep_init(bp);
2378 bp->b_flags |= B_INVAL;
2379 /* bfreekva(bp); */
2380 brelse(bp);
287a8577 2381 spin_lock(&bufqspin);
4ecf7cc9 2382 }
287a8577 2383 spin_unlock(&bufqspin);
4ecf7cc9
MD
2384 return(bytes);
2385}
55b50bd5 2386#endif
4ecf7cc9
MD
2387
2388/*
3f779080 2389 * buf_daemon:
984263bc 2390 *
3f779080 2391 * Buffer flushing daemon. Buffers are normally flushed by the
984263bc
MD
2392 * update daemon but if it cannot keep up this process starts to
2393 * take the load in an attempt to prevent getnewbuf() from blocking.
4b958e7b
MD
2394 *
2395 * Once a flush is initiated it does not stop until the number
2396 * of buffers falls below lodirtybuffers, but we will wake up anyone
2397 * waiting at the mid-point.
984263bc 2398 */
984263bc
MD
2399static struct kproc_desc buf_kp = {
2400 "bufdaemon",
2401 buf_daemon,
4b958e7b
MD
2402 &bufdaemon_td
2403};
2404SYSINIT(bufdaemon, SI_SUB_KTHREAD_BUF, SI_ORDER_FIRST,
2405 kproc_start, &buf_kp)
2406
2407static struct kproc_desc bufhw_kp = {
2408 "bufdaemon_hw",
2409 buf_daemon_hw,
2410 &bufdaemonhw_td
984263bc 2411};
4b958e7b
MD
2412SYSINIT(bufdaemon_hw, SI_SUB_KTHREAD_BUF, SI_ORDER_FIRST,
2413 kproc_start, &bufhw_kp)
984263bc 2414
cd8ab232
MD
2415/*
2416 * MPSAFE thread
2417 */
984263bc 2418static void
dca23730
VS
2419buf_daemon1(struct thread *td, int queue, int (*buf_limit_fn)(long),
2420 int *bd_req)
984263bc 2421{
3583bbb4 2422 long limit;
9adb9c71
MD
2423 struct buf *marker;
2424
2425 marker = kmalloc(sizeof(*marker), M_BIOBUF, M_WAITOK | M_ZERO);
2426 marker->b_flags |= B_MARKER;
2427 marker->b_qindex = BQUEUE_NONE;
cd083340 2428
984263bc
MD
2429 /*
2430 * This process needs to be suspended prior to shutdown sync.
2431 */
bc6dffab 2432 EVENTHANDLER_REGISTER(shutdown_pre_sync, shutdown_kproc,
dca23730 2433 td, SHUTDOWN_PRI_LAST);
4ecf7cc9 2434 curthread->td_flags |= TDF_SYSTHREAD;
984263bc
MD
2435
2436 /*
2437 * This process is allowed to take the buffer cache to the limit
2438 */
984263bc 2439 for (;;) {
0cfcada1 2440 kproc_suspend_loop();
984263bc
MD
2441
2442 /*
4afeea0d
MD
2443 * Do the flush as long as the number of dirty buffers
2444 * (including those running) exceeds lodirtybufspace.
2445 *
2446 * When flushing limit running I/O to hirunningspace
984263bc
MD
2447 * Do the flush. Limit the amount of in-transit I/O we
2448 * allow to build up, otherwise we would completely saturate
2449 * the I/O system. Wakeup any waiting processes before we
2450 * normally would so they can run in parallel with our drain.
cd083340
MD
2451 *
2452 * Our aggregate normal+HW lo water mark is lodirtybufspace,
2453 * but because we split the operation into two threads we
2454 * have to cut it in half for each thread.
984263bc 2455 */
4afeea0d 2456 waitrunningbufspace();
cd083340 2457 limit = lodirtybufspace / 2;
dca23730 2458 while (buf_limit_fn(limit)) {
9adb9c71 2459 if (flushbufqueues(marker, queue) == 0)
984263bc 2460 break;
4afeea0d
MD
2461 if (runningbufspace < hirunningspace)
2462 continue;
2463 waitrunningbufspace();
1b30fbcc 2464 }
984263bc
MD
2465
2466 /*
cd083340
MD
2467 * We reached our low water mark, reset the
2468 * request and sleep until we are needed again.
2469 * The sleep is just so the suspend code works.
984263bc 2470 */
287a8577 2471 spin_lock(&bufcspin);
dca23730
VS
2472 if (*bd_req == 0)
2473 ssleep(bd_req, &bufcspin, 0, "psleep", hz);
2474 *bd_req = 0;
287a8577 2475 spin_unlock(&bufcspin);
984263bc 2476 }
9adb9c71
MD
2477 /* NOT REACHED */
2478 /*kfree(marker, M_BIOBUF);*/
984263bc
MD
2479}
2480
dca23730
VS
2481static int
2482buf_daemon_limit(long limit)
4b958e7b 2483{
dca23730
VS
2484 return (runningbufspace + dirtybufspace > limit ||
2485 dirtybufcount - dirtybufcounthw >= nbuf / 2);
2486}
4b958e7b 2487
dca23730
VS
2488static int
2489buf_daemon_hw_limit(long limit)
2490{
2491 return (runningbufspace + dirtybufspacehw > limit ||
2492 dirtybufcounthw >= nbuf / 2);
2493}
4b958e7b 2494
dca23730
VS
2495static void
2496buf_daemon(void)
2497{
2498 buf_daemon1(bufdaemon_td, BQUEUE_DIRTY, buf_daemon_limit,
2499 &bd_request);
2500}
4b958e7b 2501
dca23730
VS
2502static void
2503buf_daemon_hw(void)
2504{
2505 buf_daemon1(bufdaemonhw_td, BQUEUE_DIRTY_HW, buf_daemon_hw_limit,
2506 &bd_request_hw);
4b958e7b
MD
2507}
2508
984263bc 2509/*
3f779080 2510 * flushbufqueues:
984263bc
MD
2511 *
2512 * Try to flush a buffer in the dirty queue. We must be careful to
2513 * free up B_INVAL buffers instead of write them, which NFS is
2514 * particularly sensitive to.
b86460bf
MD
2515 *
2516 * B_RELBUF may only be set by VFSs. We do set B_AGE to indicate
2517 * that we really want to try to get the buffer out and reuse it
2518 * due to the write load on the machine.
c5724852
MD
2519 *
2520 * We must lock the buffer in order to check its validity before we
2521 * can mess with its contents. bufqspin isn't enough.
984263bc 2522 */
984263bc 2523static int
9adb9c71 2524flushbufqueues(struct buf *marker, bufq_type_t q)
984263bc
MD
2525{
2526 struct buf *bp;
2527 int r = 0;
c3d1e862 2528
9adb9c71
MD
2529 KKASSERT(marker->b_qindex == BQUEUE_NONE);
2530 KKASSERT(marker->b_flags & B_MARKER);
2531
2532 /*
2533 * Spinlock needed to perform operations on the queue and may be
2534 * held through a non-blocking BUF_LOCK(), but cannot be held when
2535 * BUF_UNLOCK()ing or through any other major operation.
2536 */
287a8577 2537 spin_lock(&bufqspin);
9adb9c71
MD
2538 marker->b_qindex = q;
2539 TAILQ_INSERT_HEAD(&bufqueues[q], marker, b_freelist);
2540 bp = marker;
984263bc 2541
9adb9c71
MD
2542 while ((bp = TAILQ_NEXT(bp, b_freelist)) != NULL) {
2543 /*
2544 * NOTE: spinlock is always held at the top of the loop
2545 */
2546 if (bp->b_flags & B_MARKER)
2547 continue;
c5724852
MD
2548 if ((bp->b_flags & B_DELWRI) == 0) {
2549 kprintf("Unexpected clean buffer %p\n", bp);
c5724852
MD
2550 continue;
2551 }
9adb9c71 2552 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT))
c5724852 2553 continue;
c5724852 2554 KKASSERT(bp->b_qindex == q);
b86460bf 2555
c5724852 2556 /*
9adb9c71
MD
2557 * Once the buffer is locked we will have no choice but to
2558 * unlock the spinlock around a later BUF_UNLOCK and re-set
2559 * bp = marker when looping. Move the marker now to make
2560 * things easier.
2561 */
2562 TAILQ_REMOVE(&bufqueues[q], marker, b_freelist);
2563 TAILQ_INSERT_AFTER(&bufqueues[q], bp, marker, b_freelist);
2564
2565 /*
c5724852
MD
2566 * Must recheck B_DELWRI after successfully locking
2567 * the buffer.
2568 */
2569 if ((bp->b_flags & B_DELWRI) == 0) {
9adb9c71 2570 spin_unlock(&bufqspin);
c5724852 2571 BUF_UNLOCK(bp);
9adb9c71
MD
2572 spin_lock(&bufqspin);
2573 bp = marker;
c5724852
MD
2574 continue;
2575 }
6f68d895 2576
9adb9c71
MD
2577 /*
2578 * Remove the buffer from its queue. We still own the
2579 * spinlock here.
2580 */
2581 _bremfree(bp);
2582
2583 /*
2584 * Disposing of an invalid buffer counts as a flush op
2585 */
c5724852 2586 if (bp->b_flags & B_INVAL) {
287a8577 2587 spin_unlock(&bufqspin);
c5724852 2588 brelse(bp);
9adb9c71 2589 spin_lock(&bufqspin);
c5724852
MD
2590 ++r;
2591 break;
984263bc 2592 }
c5724852 2593
9adb9c71
MD
2594 /*
2595 * Release the spinlock for the more complex ops we
2596 * are now going to do.
2597 */
8bbb2fba 2598 spin_unlock(&bufqspin);
fc9ed34d 2599 lwkt_yield();
8bbb2fba 2600
9adb9c71
MD
2601 /*
2602 * This is a bit messy
2603 */
c5724852
MD
2604 if (LIST_FIRST(&bp->b_dep) != NULL &&
2605 (bp->b_flags & B_DEFERRED) == 0 &&
2606 buf_countdeps(bp, 0)) {
8bbb2fba 2607 spin_lock(&bufqspin);
c5724852 2608 TAILQ_INSERT_TAIL(&bufqueues[q], bp, b_freelist);
9adb9c71 2609 bp->b_qindex = q;
c5724852 2610 bp->b_flags |= B_DEFERRED;
9adb9c71 2611 spin_unlock(&bufqspin);
c5724852 2612 BUF_UNLOCK(bp);
9adb9c71
MD
2613 spin_lock(&bufqspin);
2614 bp = marker;
c5724852
MD
2615 continue;
2616 }
2617
2618 /*
9adb9c71
MD
2619 * spinlock not held here.
2620 *
c5724852
MD
2621 * If the buffer has a dependancy, buf_checkwrite() must
2622 * also return 0 for us to be able to initate the write.
2623 *
2624 * If the buffer is flagged B_ERROR it may be requeued
2625 * over and over again, we try to avoid a live lock.
2626 *
2627 * NOTE: buf_checkwrite is MPSAFE.
2628 */
9d4e78c7 2629 bremfree(bp);
c5724852 2630 if (LIST_FIRST(&bp->b_dep) != NULL && buf_checkwrite(bp)) {
c5724852
MD
2631 brelse(bp);
2632 } else if (bp->b_flags & B_ERROR) {
2633 tsleep(bp, 0, "bioer", 1);
2634 bp->b_flags &= ~B_AGE;
9d4e78c7 2635 cluster_awrite(bp);
c5724852
MD
2636 } else {
2637 bp->b_flags |= B_AGE;
9d4e78c7 2638 cluster_awrite(bp);
c5724852 2639 }
9adb9c71 2640 spin_lock(&bufqspin);
c5724852
MD
2641 ++r;
2642 break;
984263bc 2643 }
9adb9c71
MD
2644 TAILQ_REMOVE(&bufqueues[q], marker, b_freelist);
2645 marker->b_qindex = BQUEUE_NONE;
2646 spin_unlock(&bufqspin);
2647
984263bc
MD
2648 return (r);
2649}
2650
2651/*
3f779080
HP
2652 * inmem:
2653 *
2654 * Returns true if no I/O is needed to access the associated VM object.
1f1ea522 2655 * This is like findblk except it also hunts around in the VM system for
3f779080 2656 * the data.
06ecca5a 2657 *
3f779080
HP
2658 * Note that we ignore vm_page_free() races from interrupts against our
2659 * lookup, since if the caller is not protected our return value will not
2660 * be any more valid then otherwise once we exit the critical section.
984263bc 2661 */
984263bc 2662int
54078292 2663inmem(struct vnode *vp, off_t loffset)
984263bc
MD
2664{
2665 vm_object_t obj;
2666 vm_offset_t toff, tinc, size;
2667 vm_page_t m;
b12defdc 2668 int res = 1;
984263bc 2669
b1c20cfa 2670 if (findblk(vp, loffset, FINDBLK_TEST))
984263bc
MD
2671 return 1;
2672 if (vp->v_mount == NULL)
2673 return 0;
7540ab49
MD
2674 if ((obj = vp->v_object) == NULL)
2675 return 0;
984263bc
MD
2676
2677 size = PAGE_SIZE;
2678 if (size > vp->v_mount->mnt_stat.f_iosize)
2679 size = vp->v_mount->mnt_stat.f_iosize;
984263bc 2680
b12defdc 2681 vm_object_hold(obj);
984263bc 2682 for (toff = 0; toff < vp->v_mount->mnt_stat.f_iosize; toff += tinc) {
54078292 2683 m = vm_page_lookup(obj, OFF_TO_IDX(loffset + toff));
b12defdc
MD
2684 if (m == NULL) {
2685 res = 0;
2686 break;
2687 }
984263bc 2688 tinc = size;
54078292
MD
2689 if (tinc > PAGE_SIZE - ((toff + loffset) & PAGE_MASK))
2690 tinc = PAGE_SIZE - ((toff + loffset) & PAGE_MASK);
984263bc 2691 if (vm_page_is_valid(m,
b12defdc
MD
2692 (vm_offset_t) ((toff + loffset) & PAGE_MASK), tinc) == 0) {
2693 res = 0;
2694 break;
2695 }
984263bc 2696 }
b12defdc
MD
2697 vm_object_drop(obj);
2698 return (res);
984263bc
MD
2699}
2700
2701/*
1f1ea522
MD
2702 * findblk:
2703 *
b1c20cfa
MD
2704 * Locate and return the specified buffer. Unless flagged otherwise,
2705 * a locked buffer will be returned if it exists or NULL if it does not.
0202303b 2706 *
ae8e83e6
MD
2707 * findblk()'d buffers are still on the bufqueues and if you intend
2708 * to use your (locked NON-TEST) buffer you need to bremfree(bp)
2709 * and possibly do other stuff to it.
2710 *
b1c20cfa
MD
2711 * FINDBLK_TEST - Do not lock the buffer. The caller is responsible
2712 * for locking the buffer and ensuring that it remains
2713 * the desired buffer after locking.
0202303b 2714 *
b1c20cfa
MD
2715 * FINDBLK_NBLOCK - Lock the buffer non-blocking. If we are unable
2716 * to acquire the lock we return NULL, even if the
2717 * buffer exists.
2718 *
283b9448
MD
2719 * FINDBLK_REF - Returns the buffer ref'd, which prevents normal
2720 * reuse by getnewbuf() but does not prevent
2721 * disassociation (B_INVAL). Used to avoid deadlocks
77912481
MD
2722 * against random (vp,loffset)s due to reassignment.
2723 *
c0885fab
MD
2724 * (0) - Lock the buffer blocking.
2725 *
b1c20cfa 2726 * MPSAFE
1f1ea522
MD
2727 */
2728struct buf *
b1c20cfa 2729findblk(struct vnode *vp, off_t loffset, int flags)
1f1ea522
MD
2730{
2731 struct buf *bp;
b1c20cfa
MD
2732 int lkflags;
2733
2734 lkflags = LK_EXCLUSIVE;
2735 if (flags & FINDBLK_NBLOCK)
2736 lkflags |= LK_NOWAIT;
1f1ea522 2737
b1c20cfa 2738 for (;;) {
77912481
MD
2739 /*
2740 * Lookup. Ref the buf while holding v_token to prevent
2741 * reuse (but does not prevent diassociation).
2742 */
54341a3b 2743 lwkt_gettoken_shared(&vp->v_token);
b1c20cfa 2744 bp = buf_rb_hash_RB_LOOKUP(&vp->v_rbhash_tree, loffset);
77912481
MD
2745 if (bp == NULL) {
2746 lwkt_reltoken(&vp->v_token);
2747 return(NULL);
2748 }
283b9448 2749 bqhold(bp);
3b998fa9 2750 lwkt_reltoken(&vp->v_token);
77912481
MD
2751
2752 /*
2753 * If testing only break and return bp, do not lock.
2754 */
2755 if (flags & FINDBLK_TEST)
b1c20cfa 2756 break;
77912481
MD
2757
2758 /*
2759 * Lock the buffer, return an error if the lock fails.
2760 * (only FINDBLK_NBLOCK can cause the lock to fail).
2761 */
c0885fab 2762 if (BUF_LOCK(bp, lkflags)) {
77912481
MD
2763 atomic_subtract_int(&bp->b_refs, 1);
2764 /* bp = NULL; not needed */
2765 return(NULL);
b1c20cfa 2766 }
77912481
MD
2767
2768 /*
2769 * Revalidate the locked buf before allowing it to be
2770 * returned.
2771 */
b1c20cfa
MD
2772 if (bp->b_vp == vp && bp->b_loffset == loffset)
2773 break;
77912481 2774 atomic_subtract_int(&bp->b_refs, 1);
b1c20cfa
MD
2775 BUF_UNLOCK(bp);
2776 }
77912481
MD
2777
2778 /*
2779 * Success
2780 */
2781 if ((flags & FINDBLK_REF) == 0)
2782 atomic_subtract_int(&bp->b_refs, 1);
1f1ea522
MD
2783 return(bp);
2784}
2785
2786/*
c0885fab
MD
2787 * getcacheblk:
2788 *
2789 * Similar to getblk() except only returns the buffer if it is
2790 * B_CACHE and requires no other manipulation. Otherwise NULL
2791 * is returned.
2792 *
2793 * If B_RAM is set the buffer might be just fine, but we return
2794 * NULL anyway because we want the code to fall through to the
2795 * cluster read. Otherwise read-ahead breaks.
72d6a027
MD
2796 *
2797 * If blksize is 0 the buffer cache buffer must already be fully
2798 * cached.
2799 *
2800 * If blksize is non-zero getblk() will be used, allowing a buffer
2801 * to be reinstantiated from its VM backing store. The buffer must
2802 * still be fully cached after reinstantiation to be returned.
c0885fab
MD
2803 */
2804struct buf *
9d4e78c7 2805getcacheblk(struct vnode *vp, off_t loffset, int blksize, int blkflags)
c0885fab
MD
2806{
2807 struct buf *bp;
9d4e78c7 2808 int fndflags = (blkflags & GETBLK_NOWAIT) ? FINDBLK_NBLOCK : 0;
c0885fab 2809
72d6a027 2810 if (blksize) {
9d4e78c7 2811 bp = getblk(vp, loffset, blksize, blkflags, 0);
72d6a027
MD
2812 if (bp) {
2813 if ((bp->b_flags & (B_INVAL | B_CACHE | B_RAM)) ==
2814 B_CACHE) {
2815 bp->b_flags &= ~B_AGE;
2816 } else {
2817 brelse(bp);
2818 bp = NULL;
2819 }
2820 }
2821 } else {
9d4e78c7 2822 bp = findblk(vp, loffset, fndflags);
72d6a027
MD
2823 if (bp) {
2824 if ((bp->b_flags & (B_INVAL | B_CACHE | B_RAM)) ==
2825 B_CACHE) {
2826 bp->b_flags &= ~B_AGE;
2827 bremfree(bp);
2828 } else {
2829 BUF_UNLOCK(bp);
2830 bp = NULL;
2831 }
c0885fab
MD
2832 }
2833 }
2834 return (bp);
2835}
2836
2837/*
3f779080 2838 * getblk:
984263bc
MD
2839 *
2840 * Get a block given a specified block and offset into a file/device.
10f3fee5
MD
2841 * B_INVAL may or may not be set on return. The caller should clear
2842 * B_INVAL prior to initiating a READ.
984263bc 2843 *
77bb9400
MD
2844 * IT IS IMPORTANT TO UNDERSTAND THAT IF YOU CALL GETBLK() AND B_CACHE
2845 * IS NOT SET, YOU MUST INITIALIZE THE RETURNED BUFFER, ISSUE A READ,
2846 * OR SET B_INVAL BEFORE RETIRING IT. If you retire a getblk'd buffer
2847 * without doing any of those things the system will likely believe
2848 * the buffer to be valid (especially if it is not B_VMIO), and the
2849 * next getblk() will return the buffer with B_CACHE set.
2850 *
984263bc
MD
2851 * For a non-VMIO buffer, B_CACHE is set to the opposite of B_INVAL for
2852 * an existing buffer.
2853 *
2854 * For a VMIO buffer, B_CACHE is modified according to the backing VM.
2855 * If getblk()ing a previously 0-sized invalid buffer, B_CACHE is set
2856 * and then cleared based on the backing VM. If the previous buffer is
2857 * non-0-sized but invalid, B_CACHE will be cleared.
2858 *
2859 * If getblk() must create a new buffer, the new buffer is returned with
2860 * both B_INVAL and B_CACHE clear unless it is a VMIO buffer, in which
2861 * case it is returned with B_INVAL clear and B_CACHE set based on the
2862 * backing VM.
2863 *
62cfda27 2864 * getblk() also forces a bwrite() for any B_DELWRI buffer whos
984263bc
MD
2865 * B_CACHE bit is clear.
2866 *
2867 * What this means, basically, is that the caller should use B_CACHE to
2868 * determine whether the buffer is fully valid or not and should clear
2869 * B_INVAL prior to issuing a read. If the caller intends to validate
2870 * the buffer by loading its data area with something, the caller needs
2871 * to clear B_INVAL. If the caller does this without issuing an I/O,
2872 * the caller should set B_CACHE ( as an optimization ), else the caller
2873 * should issue the I/O and biodone() will set B_CACHE if the I/O was
2874 * a write attempt or if it was a successfull read. If the caller
2875 * intends to issue a READ, the caller must clear B_INVAL and B_ERROR
2876 * prior to issuing the READ. biodone() will *not* clear B_INVAL.
4b958e7b
MD
2877 *
2878 * getblk flags:
2879 *
2880 * GETBLK_PCATCH - catch signal if blocked, can cause NULL return
2881 * GETBLK_BHEAVY - heavy-weight buffer cache buffer
b1c20cfa
MD
2882 *
2883 * MPALMOSTSAFE
984263bc
MD
2884 */
2885struct buf *
4b958e7b 2886getblk(struct vnode *vp, off_t loffset, int size, int blkflags, int slptimeo)
984263bc
MD
2887{
2888 struct buf *bp;
4b958e7b 2889 int slpflags = (blkflags & GETBLK_PCATCH) ? PCATCH : 0;
e92ca23a 2890 int error;
b1c20cfa 2891 int lkflags;
984263bc
MD
2892
2893 if (size > MAXBSIZE)
fc92d4aa 2894 panic("getblk: size(%d) > MAXBSIZE(%d)", size, MAXBSIZE);
7540ab49
MD
2895 if (vp->v_object == NULL)
2896 panic("getblk: vnode %p has no object!", vp);
984263bc 2897
984263bc 2898loop:
77912481 2899 if ((bp = findblk(vp, loffset, FINDBLK_REF | FINDBLK_TEST)) != NULL) {
984263bc 2900 /*
a0da602d 2901 * The buffer was found in the cache, but we need to lock it.
77912481
MD
2902 * We must acquire a ref on the bp to prevent reuse, but
2903 * this will not prevent disassociation (brelvp()) so we
2904 * must recheck (vp,loffset) after acquiring the lock.
2905 *
2906 * Without the ref the buffer could potentially be reused
2907 * before we acquire the lock and create a deadlock
2908 * situation between the thread trying to reuse the buffer
2909 * and us due to the fact that we would wind up blocking
2910 * on a random (vp,loffset).
984263bc 2911 */
984263bc 2912 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
77912481 2913 if (blkflags & GETBLK_NOWAIT) {
283b9448 2914 bqdrop(bp);
b77cfc40 2915 return(NULL);
77912481 2916 }
b1c20cfa 2917 lkflags = LK_EXCLUSIVE | LK_SLEEPFAIL;
4b958e7b 2918 if (blkflags & GETBLK_PCATCH)
f2770c70 2919 lkflags |= LK_PCATCH;
e92ca23a
MD
2920 error = BUF_TIMELOCK(bp, lkflags, "getblk", slptimeo);
2921 if (error) {
283b9448 2922 bqdrop(bp);
e92ca23a
MD
2923 if (error == ENOLCK)
2924 goto loop;
e92ca23a 2925 return (NULL);
f2770c70 2926 }
b1c20cfa 2927 /* buffer may have changed on us */
984263bc 2928 }
283b9448 2929 bqdrop(bp);
984263bc
MD
2930
2931 /*
a0da602d
MD
2932 * Once the buffer has been locked, make sure we didn't race
2933 * a buffer recyclement. Buffers that are no longer hashed
2934 * will have b_vp == NULL, so this takes care of that check
2935 * as well.
2936 */
54078292 2937 if (bp->b_vp != vp || bp->b_loffset != loffset) {
973c11b9
MD
2938 kprintf("Warning buffer %p (vp %p loffset %lld) "
2939 "was recycled\n",
2940 bp, vp, (long long)loffset);
a9518ecf 2941 BUF_UNLOCK(bp);
a0da602d
MD
2942 goto loop;
2943 }
2944
2945 /*
b77cfc40
MD
2946 * If SZMATCH any pre-existing buffer must be of the requested
2947 * size or NULL is returned. The caller absolutely does not
2948 * want getblk() to bwrite() the buffer on a size mismatch.
2949 */
2950 if ((blkflags & GETBLK_SZMATCH) && size != bp->b_bcount) {
2951 BUF_UNLOCK(bp);
b77cfc40
MD
2952 return(NULL);
2953 }
2954
2955 /*
4baec531
MD
2956 * All vnode-based buffers must be backed by a VM object.
2957 */
2958 KKASSERT(bp->b_flags & B_VMIO);
10f3fee5 2959 KKASSERT(bp->b_cmd == BUF_CMD_DONE);
b86460bf 2960 bp->b_flags &= ~B_AGE;
4baec531
MD
2961
2962 /*
a0da602d
MD
2963 * Make sure that B_INVAL buffers do not have a cached
2964 * block number translation.
2965 */
54078292 2966 if ((bp->b_flags & B_INVAL) && (bp->b_bio2.bio_offset != NOOFFSET)) {
973c11b9
MD
2967 kprintf("Warning invalid buffer %p (vp %p loffset %lld)"
2968 " did not have cleared bio_offset cache\n",
2969 bp, vp, (long long)loffset);
81b5c339 2970 clearbiocache(&bp->b_bio2);
a0da602d
MD
2971 }
2972
2973 /*
984263bc 2974 * The buffer is locked. B_CACHE is cleared if the buffer is
4baec531 2975 * invalid.
984263bc
MD
2976 */
2977 if (bp->b_flags & B_INVAL)
2978 bp->b_flags &= ~B_CACHE;
984263bc
MD
2979 bremfree(bp);
2980
2981 /*
4baec531
MD
2982 * Any size inconsistancy with a dirty buffer or a buffer
2983 * with a softupdates dependancy must be resolved. Resizing
2984 * the buffer in such circumstances can lead to problems.
cb1cf930
MD
2985 *
2986 * Dirty or dependant buffers are written synchronously.
2987 * Other types of buffers are simply released and
2988 * reconstituted as they may be backed by valid, dirty VM
2989 * pages (but not marked B_DELWRI).
2990 *
2991 * NFS NOTE: NFS buffers which straddle EOF are oddly-sized
2992 * and may be left over from a prior truncation (and thus
2993 * no longer represent the actual EOF point), so we
2994 * definitely do not want to B_NOCACHE the backing store.
984263bc 2995 */
4baec531
MD
2996 if (size != bp->b_bcount) {
2997 if (bp->b_flags & B_DELWRI) {
cb1cf930 2998 bp->b_flags |= B_RELBUF;
62cfda27 2999 bwrite(bp);
4baec531 3000 } else if (LIST_FIRST(&bp->b_dep)) {
cb1cf930 3001 bp->b_flags |= B_RELBUF;
62cfda27 3002 bwrite(bp);
4baec531
MD
3003 } else {
3004 bp->b_flags |= B_RELBUF;
3005 brelse(bp);
984263bc 3006 }
4baec531 3007 goto loop;
984263bc 3008 }
4baec531 3009 KKASSERT(size <= bp->b_kvasize);
81b5c339
MD
3010 KASSERT(bp->b_loffset != NOOFFSET,
3011 ("getblk: no buffer offset"));
984263bc
MD
3012
3013 /*
3014 * A buffer with B_DELWRI set and B_CACHE clear must
3015 * be committed before we can return the buffer in
3016 * order to prevent the caller from issuing a read
3017 * ( due to B_CACHE not being set ) and overwriting
3018 * it.
3019 *
3020 * Most callers, including NFS and FFS, need this to
3021 * operate properly either because they assume they
3022 * can issue a read if B_CACHE is not set, or because
3023 * ( for example ) an uncached B_DELWRI might loop due
3024 * to softupdates re-dirtying the buffer. In the latter
3025 * case, B_CACHE is set after the first write completes,
3026 * preventing further loops.
3027 *
3028 * NOTE! b*write() sets B_CACHE. If we cleared B_CACHE
3029 * above while extending the buffer, we cannot allow the
3030 * buffer to remain with B_CACHE set after the write
3031 * completes or it will represent a corrupt state. To
3032 * deal with this we set B_NOCACHE to scrap the buffer
3033 * after the write.
3034 *
cb1cf930
MD
3035 * XXX Should this be B_RELBUF instead of B_NOCACHE?
3036 * I'm not even sure this state is still possible
3037 * now that getblk() writes out any dirty buffers
3038 * on size changes.
3039 *
984263bc
MD
3040 * We might be able to do something fancy, like setting
3041 * B_CACHE in bwrite() except if B_DELWRI is already set,
3042 * so the below call doesn't set B_CACHE, but that gets real
3043 * confusing. This is much easier.
3044 */
3045
3046 if ((bp->b_flags & (B_CACHE|B_DELWRI)) == B_DELWRI) {
cb1cf930
MD
3047 kprintf("getblk: Warning, bp %p loff=%jx DELWRI set "
3048 "and CACHE clear, b_flags %08x\n",
3049 bp, (intmax_t)bp->b_loffset, bp->b_flags);
984263bc 3050 bp->b_flags |= B_NOCACHE;
62cfda27 3051 bwrite(bp);
984263bc
MD
3052 goto loop;
3053 }
984263bc
MD
3054 } else {
3055 /*
3056 * Buffer is not in-core, create new buffer. The buffer
3057 * returned by getnewbuf() is locked. Note that the returned
3058 * buffer is also considered valid (not marked B_INVAL).
21ab32bd
MD
3059 *
3060 * Calculating the offset for the I/O requires figuring out
3061 * the block size. We use DEV_BSIZE for VBLK or VCHR and
3062 * the mount's f_iosize otherwise. If the vnode does not
3063 * have an associated mount we assume that the passed size is
3064 * the block size.
3065 *
3066 * Note that vn_isdisk() cannot be used here since it may
3067 * return a failure for numerous reasons. Note that the
3068 * buffer size may be larger then the block size (the caller
3069 * will use block numbers with the proper multiple). Beware
3070 * of using any v_* fields which are part of unions. In
3071 * particular, in DragonFly the mount point overloading
1d505369
MD
3072 * mechanism uses the namecache only and the underlying
3073 * directory vnode is not a special case.
984263bc 3074 */
7540ab49 3075 int bsize, maxsize;
984263bc 3076
21ab32bd 3077 if (vp->v_type == VBLK || vp->v_type == VCHR)
984263bc 3078 bsize = DEV_BSIZE;
984263bc
MD
3079 else if (vp->v_mount)
3080 bsize = vp->v_mount->mnt_stat.f_iosize;
3081 else
3082 bsize = size;
3083
7540ab49 3084 maxsize = size + (loffset & PAGE_MASK);
984263bc
MD
3085 maxsize = imax(maxsize, bsize);
3086
b1c20cfa
MD
3087 bp = getnewbuf(blkflags, slptimeo, size, maxsize);
3088 if (bp == NULL) {
3089 if (slpflags || slptimeo)
984263bc 3090 return NULL;
984263bc
MD
3091 goto loop;
3092 }
3093
3094 /*
b1c20cfa
MD
3095 * Atomically insert the buffer into the hash, so that it can
3096 * be found by findblk().
3097 *
3098 * If bgetvp() returns non-zero a collision occured, and the
3099 * bp will not be associated with the vnode.
1f1ea522
MD
3100 *
3101 * Make sure the translation layer has been cleared.
984263bc 3102 */
54078292
MD
3103 bp->b_loffset = loffset;
3104 bp->b_bio2.bio_offset = NOOFFSET;
1f1ea522 3105 /* bp->b_bio2.bio_next = NULL; */
984263bc 3106
7608650f 3107 if (bgetvp(vp, bp, size)) {
b1c20cfa
MD
3108 bp->b_flags |= B_INVAL;
3109 brelse(bp);
3110 goto loop;
3111 }
984263bc
MD
3112
3113 /*
4baec531 3114 * All vnode-based buffers must be backed by a VM object.
984263bc 3115 */
4baec531
MD
3116 KKASSERT(vp->v_object != NULL);
3117 bp->b_flags |= B_VMIO;
10f3fee5 3118 KKASSERT(bp->b_cmd == BUF_CMD_DONE);
984263bc
MD
3119
3120 allocbuf(bp, size);
984263bc 3121 }
8c72e3d5 3122 KKASSERT(dsched_is_clear_buf_priv(bp));
984263bc
MD
3123 return (bp);
3124}
3125
3126/*
5e23ca53
MD
3127 * regetblk(bp)
3128 *
27bc0cb1
MD
3129 * Reacquire a buffer that was previously released to the locked queue,
3130 * or reacquire a buffer which is interlocked by having bioops->io_deallocate
3131 * set B_LOCKED (which handles the acquisition race).
5e23ca53 3132 *
27bc0cb1
MD
3133 * To this end, either B_LOCKED must be set or the dependancy list must be
3134 * non-empty.
b1c20cfa
MD
3135 *
3136 * MPSAFE
5e23ca53
MD
3137 */
3138void
3139regetblk(struct buf *bp)
3140{
27bc0cb1 3141 KKASSERT((bp->b_flags & B_LOCKED) || LIST_FIRST(&bp->b_dep) != NULL);
5e23ca53 3142 BUF_LOCK(bp, LK_EXCLUSIVE | LK_RETRY);
5e23ca53 3143 bremfree(bp);
5e23ca53
MD
3144}
3145
3146/*
3f779080
HP
3147 * geteblk:
3148 *
3149 * Get an empty, disassociated buffer of given size. The buffer is
3150 * initially set to B_INVAL.
06ecca5a 3151 *
3f779080
HP
3152 * critical section protection is not required for the allocbuf()
3153 * call because races are impossible here.
b1c20cfa
MD
3154 *
3155 * MPALMOSTSAFE
984263bc
MD
3156 */
3157struct buf *
3158geteblk(int size)
3159{
3160 struct buf *bp;
984263bc
MD
3161 int maxsize;
3162
3163 maxsize = (size + BKVAMASK) & ~BKVAMASK;
3164
4090d6ff 3165 while ((bp = getnewbuf(0, 0, size, maxsize)) == NULL)
e43a034f 3166 ;
984263bc
MD
3167 allocbuf(bp, size);
3168 bp->b_flags |= B_INVAL; /* b_dep cleared by getnewbuf() */
8c72e3d5 3169 KKASSERT(dsched_is_clear_buf_priv(bp));
984263bc
MD
3170 return (bp);
3171}
3172
3173
3174/*
3f779080 3175 * allocbuf:
984263bc 3176 *
3f779080
HP
3177 * This code constitutes the buffer memory from either anonymous system
3178 * memory (in the case of non-VMIO operations) or from an associated
3179 * VM object (in the case of VMIO operations). This code is able to
3180 * resize a buffer up or down.
984263bc 3181 *
3f779080
HP
3182 * Note that this code is tricky, and has many complications to resolve
3183 * deadlock or inconsistant data situations. Tread lightly!!!
3184 * There are B_CACHE and B_DELWRI interactions that must be dealt with by
77912481
MD
3185 * the caller. Calling this code willy nilly can result in the loss of
3186 * data.
06ecca5a 3187 *
3f779080
HP
3188 * allocbuf() only adjusts B_CACHE for VMIO buffers. getblk() deals with
3189 * B_CACHE for the non-VMIO case.
3190 *
3191 * This routine does not need to be called from a critical section but you
3192 * must own the buffer.
b1c20cfa 3193 *
77912481 3194 * MPSAFE
984263bc 3195 */
984263bc
MD
3196int
3197allocbuf(struct buf *bp, int size)
3198{
3199 int newbsize, mbsize;
3200 int i;
3201
3202 if (BUF_REFCNT(bp) == 0)
3203 panic("allocbuf: buffer not busy");
3204
3205 if (bp->b_kvasize < size)
3206 panic("allocbuf: buffer too small");
3207
3208 if ((bp->b_flags & B_VMIO) == 0) {
3209 caddr_t origbuf;
3210 int origbufsize;
3211 /*
3212 * Just get anonymous memory from the kernel. Don't
3213 * mess with B_CACHE.
3214 */
3215 mbsize = (size + DEV_BSIZE - 1) & ~(DEV_BSIZE - 1);
984263bc
MD
3216 if (bp->b_flags & B_MALLOC)
3217 newbsize = mbsize;
3218 else
984263bc
MD
3219 newbsize = round_page(size);
3220
3221 if (newbsize < bp->b_bufsize) {
984263bc 3222 /*
312dcd01 3223 * Malloced buffers are not shrunk
984263bc
MD
3224 */
3225 if (bp->b_flags & B_MALLOC) {
3226 if (newbsize) {
3227 bp->b_bcount = size;
3228 } else {
efda3bd0 3229 kfree(bp->b_data, M_BIOBUF);
984263bc 3230 if (bp->b_bufsize) {
3583bbb4 3231 atomic_subtract_long(&bufmallocspace, bp->b_bufsize);
984263bc
MD
3232 bufspacewakeup();
3233 bp->b_bufsize = 0;
3234 }
3235 bp->b_data = bp->b_kvabase;
3236 bp->b_bcount = 0;
3237 bp->b_flags &= ~B_MALLOC;
3238 }
3239 return 1;
3240 }
984263bc
MD
3241 vm_hold_free_pages(
3242 bp,
3243 (vm_offset_t) bp->b_data + newbsize,
3244 (vm_offset_t) bp->b_data + bp->b_bufsize);
3245 } else if (newbsize > bp->b_bufsize) {
984263bc
MD
3246 /*
3247 * We only use malloced memory on the first allocation.
3248 * and revert to page-allocated memory when the buffer
3249 * grows.
3250 */
4baec531 3251 if ((bufmallocspace < maxbufmallocspace) &&
984263bc
MD
3252 (bp->b_bufsize == 0) &&
3253 (mbsize <= PAGE_SIZE/2)) {
3254
efda3bd0 3255 bp->b_data = kmalloc(mbsize, M_BIOBUF, M_WAITOK);
984263bc
MD
3256 bp->b_bufsize = mbsize;
3257 bp->b_bcount = size;
3258 bp->b_flags |= B_MALLOC;
3583bbb4 3259 atomic_add_long(&bufmallocspace, mbsize);
984263bc
MD
3260 return 1;
3261 }
984263bc
MD
3262 origbuf = NULL;
3263 origbufsize = 0;
984263bc 3264 /*
4baec531
MD
3265 * If the buffer is growing on its other-than-first
3266 * allocation, then we revert to the page-allocation
3267 * scheme.
984263bc
MD
3268 */
3269 if (bp->b_flags & B_MALLOC) {
3270 origbuf = bp->b_data;
3271 origbufsize = bp->b_bufsize;
3272 bp->b_data = bp->b_kvabase;
3273 if (bp->b_bufsize) {
3583bbb4
MD
3274 atomic_subtract_long(&bufmallocspace,
3275 bp->b_bufsize);
984263bc
MD
3276 bufspacewakeup();
3277 bp->b_bufsize = 0;
3278 }
3279 bp->b_flags &= ~B_MALLOC;
3280 newbsize = round_page(newbsize);
3281 }
984263bc
MD
3282 vm_hold_load_pages(
3283 bp,
3284 (vm_offset_t) bp->b_data + bp->b_bufsize,
3285 (vm_offset_t) bp->b_data + newbsize);
984263bc
MD
3286 if (origbuf) {
3287 bcopy(origbuf, bp->b_data, origbufsize);
efda3bd0 3288 kfree(origbuf, M_BIOBUF);
984263bc 3289 }
984263bc
MD
3290 }
3291 } else {
3292 vm_page_t m;
3293 int desiredpages;
3294
3295 newbsize = (size + DEV_BSIZE - 1) & ~(DEV_BSIZE - 1);
4baec531
MD
3296 desiredpages = ((int)(bp->b_loffset & PAGE_MASK) +
3297 newbsize + PAGE_MASK) >> PAGE_SHIFT;
3298 KKASSERT(desiredpages <= XIO_INTERNAL_PAGES);
984263bc 3299
984263bc
MD
3300 if (bp->b_flags & B_MALLOC)
3301 panic("allocbuf: VMIO buffer can't be malloced");
984263bc
MD
3302 /*
3303 * Set B_CACHE initially if buffer is 0 length or will become
3304 * 0-length.
3305 */
3306 if (size == 0 || bp->b_bufsize == 0)
3307 bp->b_flags |= B_CACHE;
3308
3309 if (newbsize < bp->b_bufsize) {
3310 /*
3311 * DEV_BSIZE aligned new buffer size is less then the
3312 * DEV_BSIZE aligned existing buffer size. Figure out
3313 * if we have to remove any pages.
3314 */
54f51aeb
HP
3315 if (desiredpages < bp->b_xio.xio_npages) {
3316 for (i = desiredpages; i < bp->b_xio.xio_npages; i++) {
984263bc
MD
3317 /*
3318 * the page is not freed here -- it
3319 * is the responsibility of
3320 * vnode_pager_setsize
3321 */
54f51aeb 3322 m = bp->b_xio.xio_pages[i];
984263bc
MD
3323 KASSERT(m != bogus_page,
3324 ("allocbuf: bogus page found"));
b12defdc 3325 vm_page_busy_wait(m, TRUE, "biodep");
54f51aeb 3326 bp->b_xio.xio_pages[i] = NULL;
984263bc 3327 vm_page_unwire(m, 0);
b12defdc 3328 vm_page_wakeup(m);
984263bc
MD
3329 }
3330 pmap_qremove((vm_offset_t) trunc_page((vm_offset_t)bp->b_data) +
54f51aeb
HP
3331 (desiredpages << PAGE_SHIFT), (bp->b_xio.xio_npages - desiredpages));
3332 bp->b_xio.xio_npages = desiredpages;
984263bc
MD
3333 }
3334 } else if (size > bp->b_bcount) {
3335 /*
3336 * We are growing the buffer, possibly in a
3337 * byte-granular fashion.
3338 */
3339 struct vnode *vp;
3340 vm_object_t obj;
3341 vm_offset_t toff;
3342 vm_offset_t tinc;
3343
3344 /*
3345 * Step 1, bring in the VM pages from the object,
3346 * allocating them if necessary. We must clear
3347 * B_CACHE if these pages are not valid for the
3348 * range covered by the buffer.
06ecca5a 3349 *
e43a034f
MD
3350 * critical section protection is required to protect
3351 * against interrupts unbusying and freeing pages
3352 * between our vm_page_lookup() and our
3353 * busycheck/wiring call.
984263bc 3354 */
984263bc 3355 vp = bp->b_vp;
7540ab49 3356 obj = vp->v_object;
984263bc 3357
b12defdc 3358 vm_object_hold(obj);
54f51aeb 3359 while (bp->b_xio.xio_npages < desiredpages) {
984263bc
MD
3360 vm_page_t m;
3361 vm_pindex_t pi;
b12defdc
MD
3362 int error;
3363
3364 pi = OFF_TO_IDX(bp->b_loffset) +
3365 bp->b_xio.xio_npages;
984263bc 3366
b12defdc
MD
3367 /*
3368 * Blocking on m->busy might lead to a
3369 * deadlock:
3370 *
3371 * vm_fault->getpages->cluster_read->allocbuf
3372 */
3373 m = vm_page_lookup_busy_try(obj, pi, FALSE,
3374 &error);
3375</