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