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