kernel - B_IODEBUG -> B_IOISSUED
[dragonfly.git] / sys / kern / vfs_cluster.c
CommitLineData
984263bc
MD
1/*-
2 * Copyright (c) 1993
3 * The Regents of the University of California. All rights reserved.
4 * Modifications/enhancements:
5 * Copyright (c) 1995 John S. Dyson. All rights reserved.
38a4b308 6 * Copyright (c) 2012-2013 Matthew Dillon. All rights reserved.
984263bc
MD
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
dc71b7ab 16 * 3. Neither the name of the University nor the names of its contributors
984263bc
MD
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
984263bc
MD
31 */
32
33#include "opt_debug_cluster.h"
34
35#include <sys/param.h>
36#include <sys/systm.h>
37#include <sys/kernel.h>
38#include <sys/proc.h>
39#include <sys/buf.h>
40#include <sys/vnode.h>
41#include <sys/malloc.h>
42#include <sys/mount.h>
43#include <sys/resourcevar.h>
44#include <sys/vmmeter.h>
45#include <vm/vm.h>
46#include <vm/vm_object.h>
47#include <vm/vm_page.h>
48#include <sys/sysctl.h>
54341a3b 49
3020e3be 50#include <sys/buf2.h>
12e4aaff 51#include <vm/vm_page2.h>
984263bc 52
e54488bb
MD
53#include <machine/limits.h>
54
38a4b308
MD
55/*
56 * Cluster tracking cache - replaces the original vnode v_* fields which had
57 * limited utility and were not MP safe.
58 *
59 * The cluster tracking cache is a simple 4-way set-associative non-chained
60 * cache. It is capable of tracking up to four zones separated by 1MB or
61 * more per vnode.
62 *
63 * NOTE: We want this structure to be cache-line friendly so the iterator
64 * is embedded rather than in a separate array.
65 *
66 * NOTE: A cluster cache entry can become stale when a vnode is recycled.
67 * For now we treat the values as heuristical but also self-consistent.
68 * i.e. the values cannot be completely random and cannot be SMP unsafe
69 * or the cluster code might end-up clustering non-contiguous buffers
70 * at the wrong offsets.
71 */
72struct cluster_cache {
73 struct vnode *vp;
74 u_int locked;
75 off_t v_lastw; /* last write (write cluster) */
76 off_t v_cstart; /* start block of cluster */
77 off_t v_lasta; /* last allocation */
78 u_int v_clen; /* length of current cluster */
79 u_int iterator;
80} __cachealign;
81
82typedef struct cluster_cache cluster_cache_t;
83
84#define CLUSTER_CACHE_SIZE 512
85#define CLUSTER_CACHE_MASK (CLUSTER_CACHE_SIZE - 1)
86
87#define CLUSTER_ZONE ((off_t)(1024 * 1024))
88
89cluster_cache_t cluster_array[CLUSTER_CACHE_SIZE];
90
984263bc
MD
91#if defined(CLUSTERDEBUG)
92#include <sys/sysctl.h>
93static int rcluster= 0;
94SYSCTL_INT(_debug, OID_AUTO, rcluster, CTLFLAG_RW, &rcluster, 0, "");
95#endif
96
d1cd9d97 97static MALLOC_DEFINE(M_SEGMENT, "cluster_save", "cluster_save buffer");
984263bc
MD
98
99static struct cluster_save *
38a4b308
MD
100 cluster_collectbufs (cluster_cache_t *cc, struct vnode *vp,
101 struct buf *last_bp, int blksize);
984263bc 102static struct buf *
54078292 103 cluster_rbuild (struct vnode *vp, off_t filesize, off_t loffset,
e92ca23a 104 off_t doffset, int blksize, int run,
cb1fa82f 105 struct buf *fbp, int *srp);
81b5c339 106static void cluster_callback (struct bio *);
cf1bb2a8 107static void cluster_setram (struct buf *);
cb1fa82f 108static void cluster_clrram (struct buf *);
9de13b88
MD
109static int cluster_wbuild(struct vnode *vp, struct buf **bpp, int blksize,
110 off_t start_loffset, int bytes);
984263bc
MD
111
112static int write_behind = 1;
093e85dc
SG
113SYSCTL_INT(_vfs, OID_AUTO, write_behind, CTLFLAG_RW, &write_behind, 0,
114 "Cluster write-behind setting");
504ea70e
MD
115static quad_t write_behind_minfilesize = 10 * 1024 * 1024;
116SYSCTL_QUAD(_vfs, OID_AUTO, write_behind_minfilesize, CTLFLAG_RW,
117 &write_behind_minfilesize, 0, "Cluster write-behind setting");
364c022c 118static int max_readahead = 2 * 1024 * 1024;
093e85dc
SG
119SYSCTL_INT(_vfs, OID_AUTO, max_readahead, CTLFLAG_RW, &max_readahead, 0,
120 "Limit in bytes for desired cluster read-ahead");
984263bc
MD
121
122extern vm_page_t bogus_page;
123
124extern int cluster_pbuf_freecnt;
125
cb1fa82f
MD
126/*
127 * nblks is our cluster_rbuild request size. The approximate number of
128 * physical read-ahead requests is maxra / nblks. The physical request
129 * size is limited by the device (maxrbuild). We also do not want to make
130 * the request size too big or it will mess up the B_RAM streaming.
131 */
132static __inline
133int
134calc_rbuild_reqsize(int maxra, int maxrbuild)
135{
136 int nblks;
137
138 if ((nblks = maxra / 4) > maxrbuild)
139 nblks = maxrbuild;
140 if (nblks < 1)
141 nblks = maxra;
142 return nblks;
143}
144
38a4b308
MD
145/*
146 * Acquire/release cluster cache (can return dummy entry)
147 */
148static
149cluster_cache_t *
150cluster_getcache(cluster_cache_t *dummy, struct vnode *vp, off_t loffset)
151{
152 cluster_cache_t *cc;
153 size_t hv;
154 int i;
155 int xact;
156
157 hv = (size_t)(intptr_t)vp ^ (size_t)(intptr_t)vp / sizeof(*vp);
158 hv &= CLUSTER_CACHE_MASK & ~3;
159 cc = &cluster_array[hv];
160
161 xact = -1;
162 for (i = 0; i < 4; ++i) {
163 if (cc[i].vp != vp)
164 continue;
165 if (((cc[i].v_cstart ^ loffset) & ~(CLUSTER_ZONE - 1)) == 0) {
166 xact = i;
167 break;
168 }
169 }
170 if (xact >= 0 && atomic_swap_int(&cc[xact].locked, 1) == 0) {
171 if (cc[xact].vp == vp &&
172 ((cc[i].v_cstart ^ loffset) & ~(CLUSTER_ZONE - 1)) == 0) {
173 return(&cc[xact]);
174 }
175 atomic_swap_int(&cc[xact].locked, 0);
176 }
177
178 /*
179 * New entry. If we can't acquire the cache line then use the
180 * passed-in dummy element and reset all fields.
181 *
182 * When we are able to acquire the cache line we only clear the
183 * fields if the vp does not match. This allows us to multi-zone
184 * a vp and for excessive zones / partial clusters to be retired.
185 */
186 i = cc->iterator++ & 3;
187 cc += i;
188 if (atomic_swap_int(&cc->locked, 1) != 0) {
189 cc = dummy;
190 cc->locked = 1;
191 cc->vp = NULL;
192 }
193 if (cc->vp != vp) {
194 cc->vp = vp;
195 cc->v_lasta = 0;
196 cc->v_clen = 0;
197 cc->v_cstart = 0;
198 cc->v_lastw = 0;
199 }
200 return(cc);
201}
202
203static
204void
205cluster_putcache(cluster_cache_t *cc)
206{
207 atomic_swap_int(&cc->locked, 0);
208}
209
984263bc 210/*
dbb11a6e
MD
211 * This replaces bread(), providing a synchronous read of the requested
212 * buffer plus asynchronous read-ahead within the specified bounds.
213 *
214 * The caller may pre-populate *bpp if it already has the requested buffer
215 * in-hand, else must set *bpp to NULL. Note that the cluster_read() inline
216 * sets *bpp to NULL and then calls cluster_readx() for compatibility.
364c022c
MD
217 *
218 * filesize - read-ahead @ blksize will not cross this boundary
219 * loffset - loffset for returned *bpp
220 * blksize - blocksize for returned *bpp and read-ahead bps
221 * minreq - minimum (not a hard minimum) in bytes, typically reflects
222 * a higher level uio resid.
223 * maxreq - maximum (sequential heuristic) in bytes (highet typ ~2MB)
224 * bpp - return buffer (*bpp) for (loffset,blksize)
984263bc
MD
225 */
226int
54341a3b 227cluster_readx(struct vnode *vp, off_t filesize, off_t loffset,
364c022c 228 int blksize, size_t minreq, size_t maxreq, struct buf **bpp)
984263bc
MD
229{
230 struct buf *bp, *rbp, *reqbp;
54078292
MD
231 off_t origoffset;
232 off_t doffset;
233 int error;
984263bc 234 int i;
364c022c
MD
235 int maxra;
236 int maxrbuild;
cb1fa82f 237 int sr;
984263bc 238
cb1fa82f 239 sr = 0;
984263bc
MD
240
241 /*
364c022c
MD
242 * Calculate the desired read-ahead in blksize'd blocks (maxra).
243 * To do this we calculate maxreq.
6b84c93e 244 *
364c022c
MD
245 * maxreq typically starts out as a sequential heuristic. If the
246 * high level uio/resid is bigger (minreq), we pop maxreq up to
247 * minreq. This represents the case where random I/O is being
248 * performed by the userland is issuing big read()'s.
6b84c93e 249 *
364c022c
MD
250 * Then we limit maxreq to max_readahead to ensure it is a reasonable
251 * value.
252 *
b28ad496 253 * Finally we must ensure that (loffset + maxreq) does not cross the
364c022c
MD
254 * boundary (filesize) for the current blocksize. If we allowed it
255 * to cross we could end up with buffers past the boundary with the
256 * wrong block size (HAMMER large-data areas use mixed block sizes).
b28ad496 257 * minreq is also absolutely limited to filesize.
984263bc 258 */
364c022c
MD
259 if (maxreq < minreq)
260 maxreq = minreq;
b28ad496
MD
261 /* minreq not used beyond this point */
262
364c022c
MD
263 if (maxreq > max_readahead) {
264 maxreq = max_readahead;
265 if (maxreq > 16 * 1024 * 1024)
266 maxreq = 16 * 1024 * 1024;
267 }
268 if (maxreq < blksize)
269 maxreq = blksize;
270 if (loffset + maxreq > filesize) {
271 if (loffset > filesize)
272 maxreq = 0;
273 else
274 maxreq = filesize - loffset;
275 }
276
277 maxra = (int)(maxreq / blksize);
984263bc
MD
278
279 /*
ae8e83e6 280 * Get the requested block.
984263bc 281 */
54341a3b
MD
282 if (*bpp)
283 reqbp = bp = *bpp;
284 else
285 *bpp = reqbp = bp = getblk(vp, loffset, blksize, 0, 0);
54078292 286 origoffset = loffset;
984263bc 287
364c022c
MD
288 /*
289 * Calculate the maximum cluster size for a single I/O, used
290 * by cluster_rbuild().
291 */
292 maxrbuild = vmaxiosize(vp) / blksize;
293
984263bc
MD
294 /*
295 * if it is in the cache, then check to see if the reads have been
296 * sequential. If they have, then try some read-ahead, otherwise
297 * back-off on prospective read-aheads.
298 */
299 if (bp->b_flags & B_CACHE) {
6b84c93e
MD
300 /*
301 * Not sequential, do not do any read-ahead
302 */
364c022c 303 if (maxra <= 1)
984263bc 304 return 0;
6b84c93e
MD
305
306 /*
307 * No read-ahead mark, do not do any read-ahead
308 * yet.
309 */
310 if ((bp->b_flags & B_RAM) == 0)
984263bc 311 return 0;
b1c20cfa 312
6b84c93e
MD
313 /*
314 * We hit a read-ahead-mark, figure out how much read-ahead
315 * to do (maxra) and where to start (loffset).
316 *
cb1fa82f
MD
317 * Typically the way this works is that B_RAM is set in the
318 * middle of the cluster and triggers an overlapping
319 * read-ahead of 1/2 a cluster more blocks. This ensures
320 * that the cluster read-ahead scales with the read-ahead
321 * count and is thus better-able to absorb the caller's
322 * latency.
6b84c93e 323 *
cb1fa82f
MD
324 * Estimate where the next unread block will be by assuming
325 * that the B_RAM's are placed at the half-way point.
6b84c93e
MD
326 */
327 bp->b_flags &= ~B_RAM;
328
cb1fa82f
MD
329 i = maxra / 2;
330 rbp = findblk(vp, loffset + i * blksize, FINDBLK_TEST);
331 if (rbp == NULL || (rbp->b_flags & B_CACHE) == 0) {
332 while (i) {
333 --i;
334 rbp = findblk(vp, loffset + i * blksize,
335 FINDBLK_TEST);
336 if (rbp) {
337 ++i;
338 break;
339 }
340 }
341 } else {
342 while (i < maxra) {
343 rbp = findblk(vp, loffset + i * blksize,
344 FINDBLK_TEST);
345 if (rbp == NULL)
346 break;
347 ++i;
984263bc 348 }
984263bc 349 }
364c022c
MD
350
351 /*
352 * We got everything or everything is in the cache, no
353 * point continuing.
354 */
6b84c93e
MD
355 if (i >= maxra)
356 return 0;
616dd1e9
MD
357
358 /*
359 * Calculate where to start the read-ahead and how much
360 * to do. Generally speaking we want to read-ahead by
361 * (maxra) when we've found a read-ahead mark. We do
362 * not want to reduce maxra here as it will cause
363 * successive read-ahead I/O's to be smaller and smaller.
cf83ee2c
MD
364 *
365 * However, we have to make sure we don't break the
366 * filesize limitation for the clustered operation.
616dd1e9 367 */
6b84c93e 368 loffset += i * blksize;
984263bc 369 reqbp = bp = NULL;
cf83ee2c
MD
370
371 if (loffset >= filesize)
372 return 0;
373 if (loffset + maxra * blksize > filesize) {
374 maxreq = filesize - loffset;
375 maxra = (int)(maxreq / blksize);
376 }
cb1fa82f
MD
377
378 /*
379 * Set RAM on first read-ahead block since we still have
380 * approximate maxra/2 blocks ahead of us that are already
381 * cached or in-progress.
382 */
383 sr = 1;
984263bc 384 } else {
cb1fa82f
MD
385 /*
386 * Start block is not valid, we will want to do a
387 * full read-ahead.
388 */
4d8329e1 389 __debugvar off_t firstread = bp->b_loffset;
54078292 390 int nblks;
984263bc 391
ae8e83e6
MD
392 /*
393 * Set-up synchronous read for bp.
394 */
395 bp->b_cmd = BUF_CMD_READ;
396 bp->b_bio1.bio_done = biodone_sync;
397 bp->b_bio1.bio_flags |= BIO_SYNC;
398
81b5c339
MD
399 KASSERT(firstread != NOOFFSET,
400 ("cluster_read: no buffer offset"));
54078292 401
cb1fa82f
MD
402 nblks = calc_rbuild_reqsize(maxra, maxrbuild);
403
364c022c 404 /*
cb1fa82f 405 * Set RAM half-way through the full-cluster.
364c022c 406 */
cb1fa82f 407 sr = (maxra + 1) / 2;
364c022c
MD
408
409 if (nblks > 1) {
410 int burstbytes;
984263bc 411
e92ca23a
MD
412 error = VOP_BMAP(vp, loffset, &doffset,
413 &burstbytes, NULL, BUF_CMD_READ);
984263bc
MD
414 if (error)
415 goto single_block_read;
364c022c
MD
416 if (nblks > burstbytes / blksize)
417 nblks = burstbytes / blksize;
54078292 418 if (doffset == NOOFFSET)
984263bc 419 goto single_block_read;
364c022c 420 if (nblks <= 1)
984263bc 421 goto single_block_read;
984263bc 422
54078292 423 bp = cluster_rbuild(vp, filesize, loffset,
cb1fa82f 424 doffset, blksize, nblks, bp, &sr);
54078292 425 loffset += bp->b_bufsize;
364c022c 426 maxra -= bp->b_bufsize / blksize;
984263bc
MD
427 } else {
428single_block_read:
429 /*
364c022c 430 * If it isn't in the cache, then get a chunk from
984263bc
MD
431 * disk if sequential, otherwise just get the block.
432 */
e92ca23a 433 loffset += blksize;
364c022c 434 --maxra;
984263bc
MD
435 }
436 }
437
984263bc 438 /*
ae8e83e6
MD
439 * If B_CACHE was not set issue bp. bp will either be an
440 * asynchronous cluster buf or a synchronous single-buf.
441 * If it is a single buf it will be the same as reqbp.
442 *
443 * NOTE: Once an async cluster buf is issued bp becomes invalid.
984263bc
MD
444 */
445 if (bp) {
446#if defined(CLUSTERDEBUG)
447 if (rcluster)
364c022c
MD
448 kprintf("S(%012jx,%d,%d)\n",
449 (intmax_t)bp->b_loffset, bp->b_bcount, maxra);
984263bc 450#endif
10f3fee5
MD
451 if ((bp->b_flags & B_CLUSTER) == 0)
452 vfs_busy_pages(vp, bp);
984263bc 453 bp->b_flags &= ~(B_ERROR|B_INVAL);
81b5c339 454 vn_strategy(vp, &bp->b_bio1);
ae8e83e6 455 /* bp invalid now */
dbb11a6e 456 bp = NULL;
984263bc
MD
457 }
458
cb1fa82f
MD
459#if defined(CLUSTERDEBUG)
460 if (rcluster)
461 kprintf("cluster_rd %016jx/%d maxra=%d sr=%d\n",
462 loffset, blksize, maxra, sr);
463#endif
464
984263bc 465 /*
bfda7080 466 * If we have been doing sequential I/O, then do some read-ahead.
6b84c93e
MD
467 * The code above us should have positioned us at the next likely
468 * offset.
0728eafc
MD
469 *
470 * Only mess with buffers which we can immediately lock. HAMMER
471 * will do device-readahead irrespective of what the blocks
472 * represent.
cb1fa82f
MD
473 *
474 * Set B_RAM on the first buffer (the next likely offset needing
475 * read-ahead), under the assumption that there are still
476 * approximately maxra/2 blocks good ahead of us.
984263bc 477 */
cb1fa82f 478 while (maxra > 0) {
bfda7080 479 int burstbytes;
364c022c 480 int nblks;
bfda7080 481
b77cfc40
MD
482 rbp = getblk(vp, loffset, blksize,
483 GETBLK_SZMATCH|GETBLK_NOWAIT, 0);
cb1fa82f
MD
484#if defined(CLUSTERDEBUG)
485 if (rcluster) {
486 kprintf("read-ahead %016jx rbp=%p ",
487 loffset, rbp);
488 }
489#endif
b77cfc40
MD
490 if (rbp == NULL)
491 goto no_read_ahead;
bfda7080 492 if ((rbp->b_flags & B_CACHE)) {
984263bc 493 bqrelse(rbp);
bfda7080
SS
494 goto no_read_ahead;
495 }
496
ac7ffc8a 497 /*
cb1fa82f
MD
498 * If BMAP is not supported or has an issue, we still do
499 * (maxra) read-ahead, but we do not try to use rbuild.
ac7ffc8a 500 */
cb1fa82f
MD
501 error = VOP_BMAP(vp, loffset, &doffset,
502 &burstbytes, NULL, BUF_CMD_READ);
503 if (error || doffset == NOOFFSET) {
504 nblks = 1;
505 doffset = NOOFFSET;
506 } else {
507 nblks = calc_rbuild_reqsize(maxra, maxrbuild);
508 if (nblks > burstbytes / blksize)
509 nblks = burstbytes / blksize;
bfda7080 510 }
ae8e83e6 511 rbp->b_cmd = BUF_CMD_READ;
ae8e83e6 512
364c022c 513 if (nblks > 1) {
bfda7080 514 rbp = cluster_rbuild(vp, filesize, loffset,
e92ca23a 515 doffset, blksize,
cb1fa82f 516 nblks, rbp, &sr);
984263bc 517 } else {
bfda7080 518 rbp->b_bio2.bio_offset = doffset;
cb1fa82f
MD
519 if (--sr == 0)
520 cluster_setram(rbp);
bfda7080 521 }
364c022c 522
bfda7080 523 rbp->b_flags &= ~(B_ERROR|B_INVAL);
10f3fee5 524
bfda7080
SS
525 if ((rbp->b_flags & B_CLUSTER) == 0)
526 vfs_busy_pages(vp, rbp);
ae8e83e6 527 BUF_KERNPROC(rbp);
6b84c93e
MD
528 loffset += rbp->b_bufsize;
529 maxra -= rbp->b_bufsize / blksize;
bfda7080 530 vn_strategy(vp, &rbp->b_bio1);
ae8e83e6 531 /* rbp invalid now */
984263bc 532 }
bfda7080 533
ae8e83e6
MD
534 /*
535 * Wait for our original buffer to complete its I/O. reqbp will
536 * be NULL if the original buffer was B_CACHE. We are returning
537 * (*bpp) which is the same as reqbp when reqbp != NULL.
538 */
539no_read_ahead:
540 if (reqbp) {
541 KKASSERT(reqbp->b_bio1.bio_flags & BIO_SYNC);
542 error = biowait(&reqbp->b_bio1, "clurd");
cb1fa82f
MD
543 } else {
544 error = 0;
ae8e83e6
MD
545 }
546 return (error);
984263bc
MD
547}
548
dbb11a6e
MD
549/*
550 * This replaces breadcb(), providing an asynchronous read of the requested
551 * buffer with a callback, plus an asynchronous read-ahead within the
552 * specified bounds.
553 *
554 * The callback must check whether BIO_DONE is set in the bio and issue
555 * the bpdone(bp, 0) if it isn't. The callback is responsible for clearing
556 * BIO_DONE and disposing of the I/O (bqrelse()ing it).
557 *
558 * filesize - read-ahead @ blksize will not cross this boundary
559 * loffset - loffset for returned *bpp
560 * blksize - blocksize for returned *bpp and read-ahead bps
561 * minreq - minimum (not a hard minimum) in bytes, typically reflects
562 * a higher level uio resid.
563 * maxreq - maximum (sequential heuristic) in bytes (highet typ ~2MB)
564 * bpp - return buffer (*bpp) for (loffset,blksize)
565 */
566void
567cluster_readcb(struct vnode *vp, off_t filesize, off_t loffset,
568 int blksize, size_t minreq, size_t maxreq,
569 void (*func)(struct bio *), void *arg)
570{
571 struct buf *bp, *rbp, *reqbp;
572 off_t origoffset;
573 off_t doffset;
574 int i;
575 int maxra;
576 int maxrbuild;
cb1fa82f
MD
577 int sr;
578
579 sr = 0;
dbb11a6e
MD
580
581 /*
582 * Calculate the desired read-ahead in blksize'd blocks (maxra).
583 * To do this we calculate maxreq.
584 *
585 * maxreq typically starts out as a sequential heuristic. If the
586 * high level uio/resid is bigger (minreq), we pop maxreq up to
587 * minreq. This represents the case where random I/O is being
588 * performed by the userland is issuing big read()'s.
589 *
590 * Then we limit maxreq to max_readahead to ensure it is a reasonable
591 * value.
592 *
593 * Finally we must ensure that (loffset + maxreq) does not cross the
594 * boundary (filesize) for the current blocksize. If we allowed it
595 * to cross we could end up with buffers past the boundary with the
596 * wrong block size (HAMMER large-data areas use mixed block sizes).
597 * minreq is also absolutely limited to filesize.
598 */
599 if (maxreq < minreq)
600 maxreq = minreq;
601 /* minreq not used beyond this point */
602
603 if (maxreq > max_readahead) {
604 maxreq = max_readahead;
605 if (maxreq > 16 * 1024 * 1024)
606 maxreq = 16 * 1024 * 1024;
607 }
608 if (maxreq < blksize)
609 maxreq = blksize;
610 if (loffset + maxreq > filesize) {
611 if (loffset > filesize)
612 maxreq = 0;
613 else
614 maxreq = filesize - loffset;
615 }
616
617 maxra = (int)(maxreq / blksize);
618
619 /*
620 * Get the requested block.
621 */
622 reqbp = bp = getblk(vp, loffset, blksize, 0, 0);
623 origoffset = loffset;
624
625 /*
626 * Calculate the maximum cluster size for a single I/O, used
627 * by cluster_rbuild().
628 */
629 maxrbuild = vmaxiosize(vp) / blksize;
630
631 /*
632 * if it is in the cache, then check to see if the reads have been
633 * sequential. If they have, then try some read-ahead, otherwise
634 * back-off on prospective read-aheads.
635 */
636 if (bp->b_flags & B_CACHE) {
637 /*
638 * Setup for func() call whether we do read-ahead or not.
639 */
640 bp->b_bio1.bio_caller_info1.ptr = arg;
641 bp->b_bio1.bio_flags |= BIO_DONE;
642
643 /*
644 * Not sequential, do not do any read-ahead
645 */
646 if (maxra <= 1)
647 goto no_read_ahead;
648
649 /*
650 * No read-ahead mark, do not do any read-ahead
651 * yet.
652 */
653 if ((bp->b_flags & B_RAM) == 0)
654 goto no_read_ahead;
655 bp->b_flags &= ~B_RAM;
656
657 /*
658 * We hit a read-ahead-mark, figure out how much read-ahead
659 * to do (maxra) and where to start (loffset).
660 *
661 * Shortcut the scan. Typically the way this works is that
662 * we've built up all the blocks inbetween except for the
663 * last in previous iterations, so if the second-to-last
664 * block is present we just skip ahead to it.
665 *
666 * This algorithm has O(1) cpu in the steady state no
667 * matter how large maxra is.
668 */
669 if (findblk(vp, loffset + (maxra - 2) * blksize, FINDBLK_TEST))
670 i = maxra - 1;
671 else
672 i = 1;
673 while (i < maxra) {
674 if (findblk(vp, loffset + i * blksize,
675 FINDBLK_TEST) == NULL) {
676 break;
677 }
678 ++i;
679 }
680
681 /*
682 * We got everything or everything is in the cache, no
683 * point continuing.
684 */
685 if (i >= maxra)
686 goto no_read_ahead;
687
688 /*
689 * Calculate where to start the read-ahead and how much
690 * to do. Generally speaking we want to read-ahead by
691 * (maxra) when we've found a read-ahead mark. We do
692 * not want to reduce maxra here as it will cause
693 * successive read-ahead I/O's to be smaller and smaller.
694 *
695 * However, we have to make sure we don't break the
696 * filesize limitation for the clustered operation.
697 */
698 loffset += i * blksize;
699 bp = NULL;
700 /* leave reqbp intact to force function callback */
701
702 if (loffset >= filesize)
703 goto no_read_ahead;
704 if (loffset + maxra * blksize > filesize) {
705 maxreq = filesize - loffset;
706 maxra = (int)(maxreq / blksize);
707 }
cb1fa82f 708 sr = 1;
dbb11a6e 709 } else {
cb1fa82f
MD
710 /*
711 * bp is not valid, no prior cluster in progress so get a
712 * full cluster read-ahead going.
713 */
dbb11a6e
MD
714 __debugvar off_t firstread = bp->b_loffset;
715 int nblks;
cb1fa82f 716 int error;
dbb11a6e
MD
717
718 /*
719 * Set-up synchronous read for bp.
720 */
721 bp->b_flags &= ~(B_ERROR | B_EINTR | B_INVAL);
722 bp->b_cmd = BUF_CMD_READ;
723 bp->b_bio1.bio_done = func;
724 bp->b_bio1.bio_caller_info1.ptr = arg;
725 BUF_KERNPROC(bp);
726 reqbp = NULL; /* don't func() reqbp, it's running async */
727
728 KASSERT(firstread != NOOFFSET,
729 ("cluster_read: no buffer offset"));
730
731 /*
732 * nblks is our cluster_rbuild request size, limited
733 * primarily by the device.
734 */
cb1fa82f
MD
735 nblks = calc_rbuild_reqsize(maxra, maxrbuild);
736
737 /*
738 * Set RAM half-way through the full-cluster.
739 */
740 sr = (maxra + 1) / 2;
dbb11a6e
MD
741
742 if (nblks > 1) {
743 int burstbytes;
744
cb1fa82f
MD
745 error = VOP_BMAP(vp, loffset, &doffset,
746 &burstbytes, NULL, BUF_CMD_READ);
747 if (error)
dbb11a6e
MD
748 goto single_block_read;
749 if (nblks > burstbytes / blksize)
750 nblks = burstbytes / blksize;
751 if (doffset == NOOFFSET)
752 goto single_block_read;
753 if (nblks <= 1)
754 goto single_block_read;
755
756 bp = cluster_rbuild(vp, filesize, loffset,
cb1fa82f 757 doffset, blksize, nblks, bp, &sr);
dbb11a6e
MD
758 loffset += bp->b_bufsize;
759 maxra -= bp->b_bufsize / blksize;
760 } else {
761single_block_read:
762 /*
763 * If it isn't in the cache, then get a chunk from
764 * disk if sequential, otherwise just get the block.
765 */
dbb11a6e
MD
766 loffset += blksize;
767 --maxra;
768 }
769 }
770
771 /*
772 * If bp != NULL then B_CACHE was *NOT* set and bp must be issued.
773 * bp will either be an asynchronous cluster buf or an asynchronous
774 * single-buf.
775 *
776 * NOTE: Once an async cluster buf is issued bp becomes invalid.
777 */
778 if (bp) {
779#if defined(CLUSTERDEBUG)
780 if (rcluster)
781 kprintf("S(%012jx,%d,%d)\n",
782 (intmax_t)bp->b_loffset, bp->b_bcount, maxra);
783#endif
784 if ((bp->b_flags & B_CLUSTER) == 0)
785 vfs_busy_pages(vp, bp);
786 bp->b_flags &= ~(B_ERROR|B_INVAL);
787 vn_strategy(vp, &bp->b_bio1);
788 /* bp invalid now */
789 bp = NULL;
790 }
791
cb1fa82f
MD
792#if defined(CLUSTERDEBUG)
793 if (rcluster)
794 kprintf("cluster_rd %016jx/%d maxra=%d sr=%d\n",
795 loffset, blksize, maxra, sr);
796#endif
797
dbb11a6e
MD
798 /*
799 * If we have been doing sequential I/O, then do some read-ahead.
800 * The code above us should have positioned us at the next likely
801 * offset.
802 *
803 * Only mess with buffers which we can immediately lock. HAMMER
804 * will do device-readahead irrespective of what the blocks
805 * represent.
806 */
807 while (maxra > 0) {
808 int burstbytes;
cb1fa82f 809 int error;
dbb11a6e
MD
810 int nblks;
811
812 rbp = getblk(vp, loffset, blksize,
813 GETBLK_SZMATCH|GETBLK_NOWAIT, 0);
814 if (rbp == NULL)
815 goto no_read_ahead;
816 if ((rbp->b_flags & B_CACHE)) {
817 bqrelse(rbp);
818 goto no_read_ahead;
819 }
820
821 /*
cb1fa82f
MD
822 * If BMAP is not supported or has an issue, we still do
823 * (maxra) read-ahead, but we do not try to use rbuild.
dbb11a6e 824 */
cb1fa82f
MD
825 error = VOP_BMAP(vp, loffset, &doffset,
826 &burstbytes, NULL, BUF_CMD_READ);
827 if (error || doffset == NOOFFSET) {
828 nblks = 1;
829 doffset = NOOFFSET;
830 } else {
831 nblks = calc_rbuild_reqsize(maxra, maxrbuild);
832 if (nblks > burstbytes / blksize)
833 nblks = burstbytes / blksize;
dbb11a6e 834 }
dbb11a6e 835 rbp->b_cmd = BUF_CMD_READ;
dbb11a6e
MD
836
837 if (nblks > 1) {
838 rbp = cluster_rbuild(vp, filesize, loffset,
839 doffset, blksize,
cb1fa82f 840 nblks, rbp, &sr);
dbb11a6e
MD
841 } else {
842 rbp->b_bio2.bio_offset = doffset;
cb1fa82f
MD
843 if (--sr == 0)
844 cluster_setram(rbp);
dbb11a6e
MD
845 }
846
847 rbp->b_flags &= ~(B_ERROR|B_INVAL);
848
849 if ((rbp->b_flags & B_CLUSTER) == 0)
850 vfs_busy_pages(vp, rbp);
851 BUF_KERNPROC(rbp);
852 loffset += rbp->b_bufsize;
853 maxra -= rbp->b_bufsize / blksize;
854 vn_strategy(vp, &rbp->b_bio1);
855 /* rbp invalid now */
856 }
857
858 /*
859 * If reqbp is non-NULL it had B_CACHE set and we issue the
860 * function callback synchronously.
861 *
862 * Note that we may start additional asynchronous I/O before doing
863 * the func() callback for the B_CACHE case
864 */
865no_read_ahead:
866 if (reqbp)
867 func(&reqbp->b_bio1);
868}
869
984263bc
MD
870/*
871 * If blocks are contiguous on disk, use this to provide clustered
872 * read ahead. We will read as many blocks as possible sequentially
873 * and then parcel them up into logical blocks in the buffer hash table.
ae8e83e6
MD
874 *
875 * This function either returns a cluster buf or it returns fbp. fbp is
876 * already expected to be set up as a synchronous or asynchronous request.
877 *
878 * If a cluster buf is returned it will always be async.
cb1fa82f
MD
879 *
880 * (*srp) counts down original blocks to determine where B_RAM should be set.
881 * Set B_RAM when *srp drops to 0. If (*srp) starts at 0, B_RAM will not be
882 * set on any buffer. Make sure B_RAM is cleared on any other buffers to
883 * prevent degenerate read-aheads from being generated.
984263bc
MD
884 */
885static struct buf *
ae8e83e6 886cluster_rbuild(struct vnode *vp, off_t filesize, off_t loffset, off_t doffset,
cb1fa82f 887 int blksize, int run, struct buf *fbp, int *srp)
984263bc
MD
888{
889 struct buf *bp, *tbp;
54078292
MD
890 off_t boffset;
891 int i, j;
2ec4b00d 892 int maxiosize = vmaxiosize(vp);
984263bc 893
984263bc
MD
894 /*
895 * avoid a division
896 */
e92ca23a 897 while (loffset + run * blksize > filesize) {
984263bc
MD
898 --run;
899 }
900
6260e485 901 tbp = fbp;
54078292 902 tbp->b_bio2.bio_offset = doffset;
10f3fee5
MD
903 if((tbp->b_flags & B_MALLOC) ||
904 ((tbp->b_flags & B_VMIO) == 0) || (run <= 1)) {
cb1fa82f
MD
905 if (--*srp == 0)
906 cluster_setram(tbp);
907 else
908 cluster_clrram(tbp);
984263bc 909 return tbp;
10f3fee5 910 }
984263bc 911
9a82e536 912 bp = trypbuf_kva(&cluster_pbuf_freecnt);
ae8e83e6 913 if (bp == NULL) {
984263bc 914 return tbp;
ae8e83e6 915 }
984263bc
MD
916
917 /*
918 * We are synthesizing a buffer out of vm_page_t's, but
919 * if the block size is not page aligned then the starting
920 * address may not be either. Inherit the b_data offset
921 * from the original buffer.
922 */
923 bp->b_data = (char *)((vm_offset_t)bp->b_data |
924 ((vm_offset_t)tbp->b_data & PAGE_MASK));
ae8e83e6 925 bp->b_flags |= B_CLUSTER | B_VMIO;
10f3fee5 926 bp->b_cmd = BUF_CMD_READ;
ae8e83e6 927 bp->b_bio1.bio_done = cluster_callback; /* default to async */
81b5c339
MD
928 bp->b_bio1.bio_caller_info1.cluster_head = NULL;
929 bp->b_bio1.bio_caller_info2.cluster_tail = NULL;
54078292 930 bp->b_loffset = loffset;
e92ca23a 931 bp->b_bio2.bio_offset = doffset;
81b5c339
MD
932 KASSERT(bp->b_loffset != NOOFFSET,
933 ("cluster_rbuild: no buffer offset"));
984263bc 934
984263bc
MD
935 bp->b_bcount = 0;
936 bp->b_bufsize = 0;
54f51aeb 937 bp->b_xio.xio_npages = 0;
984263bc 938
e92ca23a 939 for (boffset = doffset, i = 0; i < run; ++i, boffset += blksize) {
10f3fee5 940 if (i) {
54f51aeb 941 if ((bp->b_xio.xio_npages * PAGE_SIZE) +
e92ca23a 942 round_page(blksize) > maxiosize) {
984263bc
MD
943 break;
944 }
945
946 /*
947 * Shortcut some checks and try to avoid buffers that
948 * would block in the lock. The same checks have to
949 * be made again after we officially get the buffer.
950 */
b77cfc40
MD
951 tbp = getblk(vp, loffset + i * blksize, blksize,
952 GETBLK_SZMATCH|GETBLK_NOWAIT, 0);
953 if (tbp == NULL)
954 break;
955 for (j = 0; j < tbp->b_xio.xio_npages; j++) {
956 if (tbp->b_xio.xio_pages[j]->valid)
984263bc
MD
957 break;
958 }
b77cfc40
MD
959 if (j != tbp->b_xio.xio_npages) {
960 bqrelse(tbp);
961 break;
962 }
984263bc
MD
963
964 /*
965 * Stop scanning if the buffer is fuly valid
966 * (marked B_CACHE), or locked (may be doing a
967 * background write), or if the buffer is not
968 * VMIO backed. The clustering code can only deal
969 * with VMIO-backed buffers.
970 */
971 if ((tbp->b_flags & (B_CACHE|B_LOCKED)) ||
27bc0cb1
MD
972 (tbp->b_flags & B_VMIO) == 0 ||
973 (LIST_FIRST(&tbp->b_dep) != NULL &&
974 buf_checkread(tbp))
975 ) {
984263bc
MD
976 bqrelse(tbp);
977 break;
978 }
979
980 /*
981 * The buffer must be completely invalid in order to
982 * take part in the cluster. If it is partially valid
983 * then we stop.
984 */
54f51aeb
HP
985 for (j = 0;j < tbp->b_xio.xio_npages; j++) {
986 if (tbp->b_xio.xio_pages[j]->valid)
984263bc
MD
987 break;
988 }
54f51aeb 989 if (j != tbp->b_xio.xio_npages) {
984263bc
MD
990 bqrelse(tbp);
991 break;
992 }
993
b86460bf
MD
994 /*
995 * Depress the priority of buffers not explicitly
996 * requested.
997 */
e92ca23a 998 /* tbp->b_flags |= B_AGE; */
b86460bf 999
984263bc 1000 /*
984263bc
MD
1001 * Set the block number if it isn't set, otherwise
1002 * if it is make sure it matches the block number we
1003 * expect.
1004 */
54078292
MD
1005 if (tbp->b_bio2.bio_offset == NOOFFSET) {
1006 tbp->b_bio2.bio_offset = boffset;
1007 } else if (tbp->b_bio2.bio_offset != boffset) {
984263bc
MD
1008 brelse(tbp);
1009 break;
1010 }
1011 }
ae8e83e6 1012
cb1fa82f
MD
1013 /*
1014 * Set B_RAM if (*srp) is 1. B_RAM is only set on one buffer
1015 * in the cluster, including potentially the first buffer
1016 * once we start streaming the read-aheads.
1017 */
1018 if (--*srp == 0)
1019 cluster_setram(tbp);
1020 else
1021 cluster_clrram(tbp);
1022
984263bc 1023 /*
ae8e83e6
MD
1024 * The passed-in tbp (i == 0) will already be set up for
1025 * async or sync operation. All other tbp's acquire in
1026 * our loop are set up for async operation.
984263bc 1027 */
10f3fee5 1028 tbp->b_cmd = BUF_CMD_READ;
984263bc 1029 BUF_KERNPROC(tbp);
81b5c339 1030 cluster_append(&bp->b_bio1, tbp);
54078292 1031 for (j = 0; j < tbp->b_xio.xio_npages; ++j) {
984263bc 1032 vm_page_t m;
b12defdc 1033
54f51aeb 1034 m = tbp->b_xio.xio_pages[j];
b12defdc 1035 vm_page_busy_wait(m, FALSE, "clurpg");
984263bc 1036 vm_page_io_start(m);
b12defdc 1037 vm_page_wakeup(m);
984263bc 1038 vm_object_pip_add(m->object, 1);
54f51aeb
HP
1039 if ((bp->b_xio.xio_npages == 0) ||
1040 (bp->b_xio.xio_pages[bp->b_xio.xio_npages-1] != m)) {
1041 bp->b_xio.xio_pages[bp->b_xio.xio_npages] = m;
1042 bp->b_xio.xio_npages++;
984263bc
MD
1043 }
1044 if ((m->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL)
54f51aeb 1045 tbp->b_xio.xio_pages[j] = bogus_page;
984263bc
MD
1046 }
1047 /*
1048 * XXX shouldn't this be += size for both, like in
1049 * cluster_wbuild()?
1050 *
1051 * Don't inherit tbp->b_bufsize as it may be larger due to
1052 * a non-page-aligned size. Instead just aggregate using
1053 * 'size'.
1054 */
e92ca23a
MD
1055 if (tbp->b_bcount != blksize)
1056 kprintf("warning: tbp->b_bcount wrong %d vs %d\n", tbp->b_bcount, blksize);
1057 if (tbp->b_bufsize != blksize)
1058 kprintf("warning: tbp->b_bufsize wrong %d vs %d\n", tbp->b_bufsize, blksize);
1059 bp->b_bcount += blksize;
1060 bp->b_bufsize += blksize;
984263bc
MD
1061 }
1062
1063 /*
1064 * Fully valid pages in the cluster are already good and do not need
1065 * to be re-read from disk. Replace the page with bogus_page
1066 */
54f51aeb
HP
1067 for (j = 0; j < bp->b_xio.xio_npages; j++) {
1068 if ((bp->b_xio.xio_pages[j]->valid & VM_PAGE_BITS_ALL) ==
984263bc 1069 VM_PAGE_BITS_ALL) {
54f51aeb 1070 bp->b_xio.xio_pages[j] = bogus_page;
984263bc
MD
1071 }
1072 }
312dcd01 1073 if (bp->b_bufsize > bp->b_kvasize) {
54078292 1074 panic("cluster_rbuild: b_bufsize(%d) > b_kvasize(%d)",
984263bc 1075 bp->b_bufsize, bp->b_kvasize);
312dcd01 1076 }
984263bc 1077 pmap_qenter(trunc_page((vm_offset_t) bp->b_data),
54f51aeb 1078 (vm_page_t *)bp->b_xio.xio_pages, bp->b_xio.xio_npages);
ae8e83e6 1079 BUF_KERNPROC(bp);
984263bc
MD
1080 return (bp);
1081}
1082
1083/*
1084 * Cleanup after a clustered read or write.
1085 * This is complicated by the fact that any of the buffers might have
1086 * extra memory (if there were no empty buffer headers at allocbuf time)
1087 * that we will need to shift around.
81b5c339
MD
1088 *
1089 * The returned bio is &bp->b_bio1
984263bc 1090 */
59b728a7 1091static void
81b5c339 1092cluster_callback(struct bio *bio)
984263bc 1093{
81b5c339
MD
1094 struct buf *bp = bio->bio_buf;
1095 struct buf *tbp;
984263bc
MD
1096 int error = 0;
1097
1098 /*
9a71d53f
MD
1099 * Must propogate errors to all the components. A short read (EOF)
1100 * is a critical error.
984263bc 1101 */
9a71d53f 1102 if (bp->b_flags & B_ERROR) {
984263bc 1103 error = bp->b_error;
9a71d53f
MD
1104 } else if (bp->b_bcount != bp->b_bufsize) {
1105 panic("cluster_callback: unexpected EOF on cluster %p!", bio);
1106 }
984263bc 1107
ffd3e597
MD
1108 pmap_qremove(trunc_page((vm_offset_t) bp->b_data),
1109 bp->b_xio.xio_npages);
984263bc
MD
1110 /*
1111 * Move memory from the large cluster buffer into the component
81b5c339
MD
1112 * buffers and mark IO as done on these. Since the memory map
1113 * is the same, no actual copying is required.
984263bc 1114 */
81b5c339
MD
1115 while ((tbp = bio->bio_caller_info1.cluster_head) != NULL) {
1116 bio->bio_caller_info1.cluster_head = tbp->b_cluster_next;
984263bc 1117 if (error) {
3b2afb67 1118 tbp->b_flags |= B_ERROR | B_IOISSUED;
984263bc
MD
1119 tbp->b_error = error;
1120 } else {
1121 tbp->b_dirtyoff = tbp->b_dirtyend = 0;
1122 tbp->b_flags &= ~(B_ERROR|B_INVAL);
3b2afb67 1123 tbp->b_flags |= B_IOISSUED;
984263bc
MD
1124 /*
1125 * XXX the bdwrite()/bqrelse() issued during
1126 * cluster building clears B_RELBUF (see bqrelse()
1127 * comment). If direct I/O was specified, we have
1128 * to restore it here to allow the buffer and VM
1129 * to be freed.
1130 */
1131 if (tbp->b_flags & B_DIRECT)
1132 tbp->b_flags |= B_RELBUF;
ffd3e597
MD
1133
1134 /*
1135 * XXX I think biodone() below will do this, but do
1136 * it here anyway for consistency.
1137 */
1138 if (tbp->b_cmd == BUF_CMD_WRITE)
1139 bundirty(tbp);
984263bc 1140 }
81b5c339 1141 biodone(&tbp->b_bio1);
984263bc
MD
1142 }
1143 relpbuf(bp, &cluster_pbuf_freecnt);
1144}
1145
1146/*
504ea70e 1147 * Implement modified write build for cluster.
984263bc 1148 *
504ea70e
MD
1149 * write_behind = 0 write behind disabled
1150 * write_behind = 1 write behind normal (default)
1151 * write_behind = 2 write behind backed-off
984263bc 1152 *
504ea70e
MD
1153 * In addition, write_behind is only activated for files that have
1154 * grown past a certain size (default 10MB). Otherwise temporary files
1155 * wind up generating a lot of unnecessary disk I/O.
984263bc 1156 */
984263bc 1157static __inline int
e92ca23a 1158cluster_wbuild_wb(struct vnode *vp, int blksize, off_t start_loffset, int len)
984263bc
MD
1159{
1160 int r = 0;
1161
1162 switch(write_behind) {
1163 case 2:
54078292 1164 if (start_loffset < len)
984263bc 1165 break;
54078292 1166 start_loffset -= len;
984263bc
MD
1167 /* fall through */
1168 case 1:
504ea70e
MD
1169 if (vp->v_filesize >= write_behind_minfilesize) {
1170 r = cluster_wbuild(vp, NULL, blksize,
1171 start_loffset, len);
1172 }
984263bc
MD
1173 /* fall through */
1174 default:
1175 /* fall through */
1176 break;
1177 }
1178 return(r);
1179}
1180
1181/*
1182 * Do clustered write for FFS.
1183 *
1184 * Three cases:
1185 * 1. Write is not sequential (write asynchronously)
1186 * Write is sequential:
1187 * 2. beginning of cluster - begin cluster
1188 * 3. middle of a cluster - add to cluster
1189 * 4. end of a cluster - asynchronously write cluster
38a4b308
MD
1190 *
1191 * WARNING! vnode fields are not locked and must ONLY be used heuristically.
984263bc
MD
1192 */
1193void
e92ca23a 1194cluster_write(struct buf *bp, off_t filesize, int blksize, int seqcount)
984263bc
MD
1195{
1196 struct vnode *vp;
54078292 1197 off_t loffset;
984263bc 1198 int maxclen, cursize;
984263bc 1199 int async;
38a4b308
MD
1200 cluster_cache_t dummy;
1201 cluster_cache_t *cc;
984263bc
MD
1202
1203 vp = bp->b_vp;
e92ca23a 1204 if (vp->v_type == VREG)
984263bc 1205 async = vp->v_mount->mnt_flag & MNT_ASYNC;
e92ca23a 1206 else
984263bc 1207 async = 0;
54078292 1208 loffset = bp->b_loffset;
81b5c339
MD
1209 KASSERT(bp->b_loffset != NOOFFSET,
1210 ("cluster_write: no buffer offset"));
984263bc 1211
38a4b308
MD
1212 cc = cluster_getcache(&dummy, vp, loffset);
1213
1214 /*
1215 * Initialize vnode to beginning of file.
1216 */
54078292 1217 if (loffset == 0)
38a4b308 1218 cc->v_lasta = cc->v_clen = cc->v_cstart = cc->v_lastw = 0;
984263bc 1219
38a4b308 1220 if (cc->v_clen == 0 || loffset != cc->v_lastw + blksize ||
54078292 1221 bp->b_bio2.bio_offset == NOOFFSET ||
38a4b308 1222 (bp->b_bio2.bio_offset != cc->v_lasta + blksize)) {
2ec4b00d 1223 maxclen = vmaxiosize(vp);
38a4b308 1224 if (cc->v_clen != 0) {
984263bc
MD
1225 /*
1226 * Next block is not sequential.
1227 *
1228 * If we are not writing at end of file, the process
1229 * seeked to another point in the file since its last
1230 * write, or we have reached our maximum cluster size,
1231 * then push the previous cluster. Otherwise try
1232 * reallocating to make it sequential.
1233 *
1234 * Change to algorithm: only push previous cluster if
1235 * it was sequential from the point of view of the
1236 * seqcount heuristic, otherwise leave the buffer
1237 * intact so we can potentially optimize the I/O
1238 * later on in the buf_daemon or update daemon
1239 * flush.
1240 */
38a4b308 1241 cursize = cc->v_lastw - cc->v_cstart + blksize;
9de13b88 1242 if (bp->b_loffset + blksize < filesize ||
38a4b308
MD
1243 loffset != cc->v_lastw + blksize ||
1244 cc->v_clen <= cursize) {
984263bc 1245 if (!async && seqcount > 0) {
e92ca23a 1246 cluster_wbuild_wb(vp, blksize,
38a4b308 1247 cc->v_cstart, cursize);
984263bc
MD
1248 }
1249 } else {
1250 struct buf **bpp, **endbp;
1251 struct cluster_save *buflist;
1252
38a4b308
MD
1253 buflist = cluster_collectbufs(cc, vp,
1254 bp, blksize);
984263bc
MD
1255 endbp = &buflist->bs_children
1256 [buflist->bs_nchildren - 1];
1257 if (VOP_REALLOCBLKS(vp, buflist)) {
1258 /*
1259 * Failed, push the previous cluster
1260 * if *really* writing sequentially
1261 * in the logical file (seqcount > 1),
1262 * otherwise delay it in the hopes that
1263 * the low level disk driver can
1264 * optimize the write ordering.
38a4b308
MD
1265 *
1266 * NOTE: We do not brelse the last
1267 * element which is bp, and we
1268 * do not return here.
984263bc
MD
1269 */
1270 for (bpp = buflist->bs_children;
1271 bpp < endbp; bpp++)
1272 brelse(*bpp);
efda3bd0 1273 kfree(buflist, M_SEGMENT);
984263bc
MD
1274 if (seqcount > 1) {
1275 cluster_wbuild_wb(vp,
38a4b308 1276 blksize, cc->v_cstart,
984263bc
MD
1277 cursize);
1278 }
1279 } else {
1280 /*
1281 * Succeeded, keep building cluster.
1282 */
1283 for (bpp = buflist->bs_children;
1284 bpp <= endbp; bpp++)
1285 bdwrite(*bpp);
efda3bd0 1286 kfree(buflist, M_SEGMENT);
38a4b308
MD
1287 cc->v_lastw = loffset;
1288 cc->v_lasta = bp->b_bio2.bio_offset;
1289 cluster_putcache(cc);
984263bc
MD
1290 return;
1291 }
1292 }
1293 }
1294 /*
1295 * Consider beginning a cluster. If at end of file, make
1296 * cluster as large as possible, otherwise find size of
1297 * existing cluster.
1298 */
1299 if ((vp->v_type == VREG) &&
9de13b88 1300 bp->b_loffset + blksize < filesize &&
54078292 1301 (bp->b_bio2.bio_offset == NOOFFSET) &&
e92ca23a 1302 (VOP_BMAP(vp, loffset, &bp->b_bio2.bio_offset, &maxclen, NULL, BUF_CMD_WRITE) ||
54078292 1303 bp->b_bio2.bio_offset == NOOFFSET)) {
b642a6c1 1304 bdwrite(bp);
38a4b308
MD
1305 cc->v_clen = 0;
1306 cc->v_lasta = bp->b_bio2.bio_offset;
1307 cc->v_cstart = loffset + blksize;
1308 cc->v_lastw = loffset;
1309 cluster_putcache(cc);
984263bc
MD
1310 return;
1311 }
e92ca23a 1312 if (maxclen > blksize)
38a4b308 1313 cc->v_clen = maxclen - blksize;
54078292 1314 else
38a4b308
MD
1315 cc->v_clen = 0;
1316 if (!async && cc->v_clen == 0) { /* I/O not contiguous */
1317 cc->v_cstart = loffset + blksize;
b642a6c1 1318 bdwrite(bp);
984263bc 1319 } else { /* Wait for rest of cluster */
38a4b308 1320 cc->v_cstart = loffset;
984263bc
MD
1321 bdwrite(bp);
1322 }
38a4b308 1323 } else if (loffset == cc->v_cstart + cc->v_clen) {
984263bc
MD
1324 /*
1325 * At end of cluster, write it out if seqcount tells us we
1326 * are operating sequentially, otherwise let the buf or
1327 * update daemon handle it.
1328 */
1329 bdwrite(bp);
1330 if (seqcount > 1)
38a4b308
MD
1331 cluster_wbuild_wb(vp, blksize, cc->v_cstart,
1332 cc->v_clen + blksize);
1333 cc->v_clen = 0;
1334 cc->v_cstart = loffset + blksize;
b642a6c1
MD
1335 } else if (vm_page_count_severe() &&
1336 bp->b_loffset + blksize < filesize) {
984263bc 1337 /*
b642a6c1
MD
1338 * We are low on memory, get it going NOW. However, do not
1339 * try to push out a partial block at the end of the file
1340 * as this could lead to extremely non-optimal write activity.
984263bc
MD
1341 */
1342 bawrite(bp);
1343 } else {
1344 /*
1345 * In the middle of a cluster, so just delay the I/O for now.
1346 */
1347 bdwrite(bp);
1348 }
38a4b308
MD
1349 cc->v_lastw = loffset;
1350 cc->v_lasta = bp->b_bio2.bio_offset;
1351 cluster_putcache(cc);
984263bc
MD
1352}
1353
9de13b88
MD
1354/*
1355 * This is the clustered version of bawrite(). It works similarly to
1356 * cluster_write() except I/O on the buffer is guaranteed to occur.
1357 */
1358int
1359cluster_awrite(struct buf *bp)
1360{
1361 int total;
1362
1363 /*
1364 * Don't bother if it isn't clusterable.
1365 */
1366 if ((bp->b_flags & B_CLUSTEROK) == 0 ||
1367 bp->b_vp == NULL ||
1368 (bp->b_vp->v_flag & VOBJBUF) == 0) {
1369 total = bp->b_bufsize;
1370 bawrite(bp);
1371 return (total);
1372 }
1373
1374 total = cluster_wbuild(bp->b_vp, &bp, bp->b_bufsize,
1375 bp->b_loffset, vmaxiosize(bp->b_vp));
1376 if (bp)
1377 bawrite(bp);
1378
1379 return total;
1380}
984263bc
MD
1381
1382/*
1383 * This is an awful lot like cluster_rbuild...wish they could be combined.
1384 * The last lbn argument is the current block on which I/O is being
1385 * performed. Check to see that it doesn't fall in the middle of
1386 * the current block (if last_bp == NULL).
9de13b88
MD
1387 *
1388 * cluster_wbuild() normally does not guarantee anything. If bpp is
1389 * non-NULL and cluster_wbuild() is able to incorporate it into the
1390 * I/O it will set *bpp to NULL, otherwise it will leave it alone and
1391 * the caller must dispose of *bpp.
984263bc 1392 */
9de13b88
MD
1393static int
1394cluster_wbuild(struct vnode *vp, struct buf **bpp,
1395 int blksize, off_t start_loffset, int bytes)
984263bc
MD
1396{
1397 struct buf *bp, *tbp;
e43a034f 1398 int i, j;
984263bc 1399 int totalwritten = 0;
9de13b88 1400 int must_initiate;
2ec4b00d 1401 int maxiosize = vmaxiosize(vp);
984263bc 1402
54078292 1403 while (bytes > 0) {
984263bc 1404 /*
9de13b88
MD
1405 * If the buffer matches the passed locked & removed buffer
1406 * we used the passed buffer (which might not be B_DELWRI).
1407 *
1408 * Otherwise locate the buffer and determine if it is
1409 * compatible.
984263bc 1410 */
9de13b88
MD
1411 if (bpp && (*bpp)->b_loffset == start_loffset) {
1412 tbp = *bpp;
1413 *bpp = NULL;
1414 bpp = NULL;
1415 } else {
1416 tbp = findblk(vp, start_loffset, FINDBLK_NBLOCK);
1417 if (tbp == NULL ||
1418 (tbp->b_flags & (B_LOCKED | B_INVAL | B_DELWRI)) !=
1419 B_DELWRI ||
1420 (LIST_FIRST(&tbp->b_dep) && buf_checkwrite(tbp))) {
1421 if (tbp)
1422 BUF_UNLOCK(tbp);
1423 start_loffset += blksize;
1424 bytes -= blksize;
1425 continue;
1426 }
1427 bremfree(tbp);
984263bc 1428 }
10f3fee5 1429 KKASSERT(tbp->b_cmd == BUF_CMD_DONE);
984263bc
MD
1430
1431 /*
1432 * Extra memory in the buffer, punt on this buffer.
1433 * XXX we could handle this in most cases, but we would
1434 * have to push the extra memory down to after our max
1435 * possible cluster size and then potentially pull it back
1436 * up if the cluster was terminated prematurely--too much
1437 * hassle.
1438 */
1439 if (((tbp->b_flags & (B_CLUSTEROK|B_MALLOC)) != B_CLUSTEROK) ||
b1c20cfa
MD
1440 (tbp->b_bcount != tbp->b_bufsize) ||
1441 (tbp->b_bcount != blksize) ||
1442 (bytes == blksize) ||
9a82e536 1443 ((bp = getpbuf_kva(&cluster_pbuf_freecnt)) == NULL)) {
984263bc
MD
1444 totalwritten += tbp->b_bufsize;
1445 bawrite(tbp);
e92ca23a
MD
1446 start_loffset += blksize;
1447 bytes -= blksize;
984263bc
MD
1448 continue;
1449 }
1450
1451 /*
9a71d53f
MD
1452 * Set up the pbuf. Track our append point with b_bcount
1453 * and b_bufsize. b_bufsize is not used by the device but
1454 * our caller uses it to loop clusters and we use it to
1455 * detect a premature EOF on the block device.
984263bc 1456 */
984263bc
MD
1457 bp->b_bcount = 0;
1458 bp->b_bufsize = 0;
54f51aeb 1459 bp->b_xio.xio_npages = 0;
81b5c339 1460 bp->b_loffset = tbp->b_loffset;
54078292 1461 bp->b_bio2.bio_offset = tbp->b_bio2.bio_offset;
984263bc
MD
1462
1463 /*
1464 * We are synthesizing a buffer out of vm_page_t's, but
1465 * if the block size is not page aligned then the starting
1466 * address may not be either. Inherit the b_data offset
1467 * from the original buffer.
1468 */
1469 bp->b_data = (char *)((vm_offset_t)bp->b_data |
1470 ((vm_offset_t)tbp->b_data & PAGE_MASK));
10f3fee5 1471 bp->b_flags &= ~B_ERROR;
4414f2c9 1472 bp->b_flags |= B_CLUSTER | B_BNOCLIP |
f2d7fcf0 1473 (tbp->b_flags & (B_VMIO | B_NEEDCOMMIT));
81b5c339
MD
1474 bp->b_bio1.bio_caller_info1.cluster_head = NULL;
1475 bp->b_bio1.bio_caller_info2.cluster_tail = NULL;
b1c20cfa 1476
984263bc
MD
1477 /*
1478 * From this location in the file, scan forward to see
1479 * if there are buffers with adjacent data that need to
1480 * be written as well.
9de13b88
MD
1481 *
1482 * IO *must* be initiated on index 0 at this point
1483 * (particularly when called from cluster_awrite()).
984263bc 1484 */
e92ca23a 1485 for (i = 0; i < bytes; (i += blksize), (start_loffset += blksize)) {
9de13b88
MD
1486 if (i == 0) {
1487 must_initiate = 1;
1488 } else {
1489 /*
1490 * Not first buffer.
1491 */
1492 must_initiate = 0;
b1c20cfa
MD
1493 tbp = findblk(vp, start_loffset,
1494 FINDBLK_NBLOCK);
984263bc 1495 /*
b1c20cfa
MD
1496 * Buffer not found or could not be locked
1497 * non-blocking.
984263bc 1498 */
b1c20cfa 1499 if (tbp == NULL)
984263bc 1500 break;
984263bc
MD
1501
1502 /*
1503 * If it IS in core, but has different
b1c20cfa
MD
1504 * characteristics, then don't cluster
1505 * with it.
984263bc
MD
1506 */
1507 if ((tbp->b_flags & (B_VMIO | B_CLUSTEROK |
b1c20cfa
MD
1508 B_INVAL | B_DELWRI | B_NEEDCOMMIT))
1509 != (B_DELWRI | B_CLUSTEROK |
1510 (bp->b_flags & (B_VMIO | B_NEEDCOMMIT))) ||
9de13b88 1511 (tbp->b_flags & B_LOCKED)
b1c20cfa
MD
1512 ) {
1513 BUF_UNLOCK(tbp);
984263bc
MD
1514 break;
1515 }
1516
1517 /*
1518 * Check that the combined cluster
1519 * would make sense with regard to pages
1520 * and would not be too large
9de13b88
MD
1521 *
1522 * WARNING! buf_checkwrite() must be the last
1523 * check made. If it returns 0 then
1524 * we must initiate the I/O.
984263bc 1525 */
e92ca23a 1526 if ((tbp->b_bcount != blksize) ||
54078292
MD
1527 ((bp->b_bio2.bio_offset + i) !=
1528 tbp->b_bio2.bio_offset) ||
54f51aeb 1529 ((tbp->b_xio.xio_npages + bp->b_xio.xio_npages) >
9de13b88
MD
1530 (maxiosize / PAGE_SIZE)) ||
1531 (LIST_FIRST(&tbp->b_dep) &&
1532 buf_checkwrite(tbp))
1533 ) {
984263bc 1534 BUF_UNLOCK(tbp);
984263bc
MD
1535 break;
1536 }
9de13b88
MD
1537 if (LIST_FIRST(&tbp->b_dep))
1538 must_initiate = 1;
984263bc
MD
1539 /*
1540 * Ok, it's passed all the tests,
1541 * so remove it from the free list
1542 * and mark it busy. We will use it.
1543 */
1544 bremfree(tbp);
10f3fee5 1545 KKASSERT(tbp->b_cmd == BUF_CMD_DONE);
9de13b88 1546 }
81b5c339 1547
984263bc
MD
1548 /*
1549 * If the IO is via the VM then we do some
1550 * special VM hackery (yuck). Since the buffer's
1551 * block size may not be page-aligned it is possible
1552 * for a page to be shared between two buffers. We
1553 * have to get rid of the duplication when building
1554 * the cluster.
1555 */
1556 if (tbp->b_flags & B_VMIO) {
1557 vm_page_t m;
1558
9de13b88
MD
1559 /*
1560 * Try to avoid deadlocks with the VM system.
1561 * However, we cannot abort the I/O if
1562 * must_initiate is non-zero.
1563 */
1564 if (must_initiate == 0) {
1565 for (j = 0;
1566 j < tbp->b_xio.xio_npages;
1567 ++j) {
54f51aeb 1568 m = tbp->b_xio.xio_pages[j];
984263bc
MD
1569 if (m->flags & PG_BUSY) {
1570 bqrelse(tbp);
1571 goto finishcluster;
1572 }
1573 }
1574 }
1575
54078292 1576 for (j = 0; j < tbp->b_xio.xio_npages; ++j) {
54f51aeb 1577 m = tbp->b_xio.xio_pages[j];
b12defdc 1578 vm_page_busy_wait(m, FALSE, "clurpg");
984263bc 1579 vm_page_io_start(m);
b12defdc 1580 vm_page_wakeup(m);
984263bc 1581 vm_object_pip_add(m->object, 1);
54f51aeb
HP
1582 if ((bp->b_xio.xio_npages == 0) ||
1583 (bp->b_xio.xio_pages[bp->b_xio.xio_npages - 1] != m)) {
1584 bp->b_xio.xio_pages[bp->b_xio.xio_npages] = m;
1585 bp->b_xio.xio_npages++;
984263bc
MD
1586 }
1587 }
1588 }
e92ca23a
MD
1589 bp->b_bcount += blksize;
1590 bp->b_bufsize += blksize;
984263bc 1591
ffd3e597
MD
1592 /*
1593 * NOTE: see bwrite/bawrite code for why we no longer
1594 * undirty tbp here.
1595 *
1596 * bundirty(tbp); REMOVED
1597 */
10f3fee5 1598 tbp->b_flags &= ~B_ERROR;
10f3fee5 1599 tbp->b_cmd = BUF_CMD_WRITE;
984263bc 1600 BUF_KERNPROC(tbp);
81b5c339 1601 cluster_append(&bp->b_bio1, tbp);
2aee763b
MD
1602
1603 /*
1604 * check for latent dependencies to be handled
1605 */
408357d8
MD
1606 if (LIST_FIRST(&tbp->b_dep) != NULL)
1607 buf_start(tbp);
984263bc
MD
1608 }
1609 finishcluster:
9de13b88
MD
1610 pmap_qenter(trunc_page((vm_offset_t)bp->b_data),
1611 (vm_page_t *)bp->b_xio.xio_pages,
1612 bp->b_xio.xio_npages);
312dcd01 1613 if (bp->b_bufsize > bp->b_kvasize) {
9de13b88
MD
1614 panic("cluster_wbuild: b_bufsize(%d) "
1615 "> b_kvasize(%d)\n",
1616 bp->b_bufsize, bp->b_kvasize);
312dcd01 1617 }
984263bc
MD
1618 totalwritten += bp->b_bufsize;
1619 bp->b_dirtyoff = 0;
1620 bp->b_dirtyend = bp->b_bufsize;
ae8e83e6 1621 bp->b_bio1.bio_done = cluster_callback;
10f3fee5 1622 bp->b_cmd = BUF_CMD_WRITE;
ae8e83e6 1623
10f3fee5 1624 vfs_busy_pages(vp, bp);
77912481 1625 bsetrunningbufspace(bp, bp->b_bufsize);
ae8e83e6 1626 BUF_KERNPROC(bp);
a8f169e2 1627 vn_strategy(vp, &bp->b_bio1);
984263bc 1628
54078292 1629 bytes -= i;
984263bc
MD
1630 }
1631 return totalwritten;
1632}
1633
1634/*
47269f33
MD
1635 * Collect together all the buffers in a cluster, plus add one
1636 * additional buffer passed-in.
1637 *
1638 * Only pre-existing buffers whos block size matches blksize are collected.
1639 * (this is primarily because HAMMER1 uses varying block sizes and we don't
1640 * want to override its choices).
65ec5030
MD
1641 *
1642 * This code will not try to collect buffers that it cannot lock, otherwise
1643 * it might deadlock against SMP-friendly filesystems.
984263bc
MD
1644 */
1645static struct cluster_save *
38a4b308
MD
1646cluster_collectbufs(cluster_cache_t *cc, struct vnode *vp,
1647 struct buf *last_bp, int blksize)
984263bc
MD
1648{
1649 struct cluster_save *buflist;
1650 struct buf *bp;
54078292 1651 off_t loffset;
984263bc 1652 int i, len;
47269f33
MD
1653 int j;
1654 int k;
984263bc 1655
38a4b308
MD
1656 len = (int)(cc->v_lastw - cc->v_cstart + blksize) / blksize;
1657 KKASSERT(len > 0);
77652cad 1658 buflist = kmalloc(sizeof(struct buf *) * (len + 1) + sizeof(*buflist),
54078292 1659 M_SEGMENT, M_WAITOK);
984263bc
MD
1660 buflist->bs_nchildren = 0;
1661 buflist->bs_children = (struct buf **) (buflist + 1);
38a4b308 1662 for (loffset = cc->v_cstart, i = 0, j = 0;
47269f33
MD
1663 i < len;
1664 (loffset += blksize), i++) {
1665 bp = getcacheblk(vp, loffset,
65ec5030
MD
1666 last_bp->b_bcount, GETBLK_SZMATCH |
1667 GETBLK_NOWAIT);
984263bc 1668 buflist->bs_children[i] = bp;
47269f33
MD
1669 if (bp == NULL) {
1670 j = i + 1;
1671 } else if (bp->b_bio2.bio_offset == NOOFFSET) {
08daea96 1672 VOP_BMAP(bp->b_vp, bp->b_loffset,
e92ca23a
MD
1673 &bp->b_bio2.bio_offset,
1674 NULL, NULL, BUF_CMD_WRITE);
54078292 1675 }
984263bc 1676 }
47269f33
MD
1677
1678 /*
1679 * Get rid of gaps
1680 */
1681 for (k = 0; k < j; ++k) {
1682 if (buflist->bs_children[k]) {
1683 bqrelse(buflist->bs_children[k]);
1684 buflist->bs_children[k] = NULL;
1685 }
1686 }
1687 if (j != 0) {
1688 if (j != i) {
1689 bcopy(buflist->bs_children + j,
1690 buflist->bs_children + 0,
1691 sizeof(buflist->bs_children[0]) * (i - j));
1692 }
1693 i -= j;
1694 }
984263bc 1695 buflist->bs_children[i] = bp = last_bp;
54078292 1696 if (bp->b_bio2.bio_offset == NOOFFSET) {
e92ca23a
MD
1697 VOP_BMAP(bp->b_vp, bp->b_loffset, &bp->b_bio2.bio_offset,
1698 NULL, NULL, BUF_CMD_WRITE);
54078292 1699 }
984263bc
MD
1700 buflist->bs_nchildren = i + 1;
1701 return (buflist);
1702}
81b5c339
MD
1703
1704void
1705cluster_append(struct bio *bio, struct buf *tbp)
1706{
1707 tbp->b_cluster_next = NULL;
1708 if (bio->bio_caller_info1.cluster_head == NULL) {
1709 bio->bio_caller_info1.cluster_head = tbp;
1710 bio->bio_caller_info2.cluster_tail = tbp;
1711 } else {
1712 bio->bio_caller_info2.cluster_tail->b_cluster_next = tbp;
1713 bio->bio_caller_info2.cluster_tail = tbp;
1714 }
1715}
1716
cf1bb2a8
MD
1717static
1718void
cb1fa82f 1719cluster_setram(struct buf *bp)
cf1bb2a8
MD
1720{
1721 bp->b_flags |= B_RAM;
1722 if (bp->b_xio.xio_npages)
1723 vm_page_flag_set(bp->b_xio.xio_pages[0], PG_RAM);
1724}
cb1fa82f
MD
1725
1726static
1727void
1728cluster_clrram(struct buf *bp)
1729{
1730 bp->b_flags &= ~B_RAM;
1731 if (bp->b_xio.xio_npages)
1732 vm_page_flag_clear(bp->b_xio.xio_pages[0], PG_RAM);
1733}