kernel - fix 64-bit build
[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.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 * @(#)vfs_cluster.c 8.7 (Berkeley) 2/13/94
36 * $FreeBSD: src/sys/kern/vfs_cluster.c,v 1.92.2.9 2001/11/18 07:10:59 dillon Exp $
b77cfc40 37 * $DragonFly: src/sys/kern/vfs_cluster.c,v 1.40 2008/07/14 03:09:00 dillon Exp $
984263bc
MD
38 */
39
40#include "opt_debug_cluster.h"
41
42#include <sys/param.h>
43#include <sys/systm.h>
44#include <sys/kernel.h>
45#include <sys/proc.h>
46#include <sys/buf.h>
47#include <sys/vnode.h>
48#include <sys/malloc.h>
49#include <sys/mount.h>
50#include <sys/resourcevar.h>
51#include <sys/vmmeter.h>
52#include <vm/vm.h>
53#include <vm/vm_object.h>
54#include <vm/vm_page.h>
55#include <sys/sysctl.h>
3020e3be 56#include <sys/buf2.h>
12e4aaff 57#include <vm/vm_page2.h>
984263bc 58
e54488bb
MD
59#include <machine/limits.h>
60
984263bc
MD
61#if defined(CLUSTERDEBUG)
62#include <sys/sysctl.h>
63static int rcluster= 0;
64SYSCTL_INT(_debug, OID_AUTO, rcluster, CTLFLAG_RW, &rcluster, 0, "");
65#endif
66
d1cd9d97 67static MALLOC_DEFINE(M_SEGMENT, "cluster_save", "cluster_save buffer");
984263bc
MD
68
69static struct cluster_save *
54078292 70 cluster_collectbufs (struct vnode *vp, struct buf *last_bp,
e92ca23a 71 int blksize);
984263bc 72static struct buf *
54078292 73 cluster_rbuild (struct vnode *vp, off_t filesize, off_t loffset,
e92ca23a 74 off_t doffset, int blksize, int run,
ae8e83e6 75 struct buf *fbp);
81b5c339 76static void cluster_callback (struct bio *);
cf1bb2a8 77static void cluster_setram (struct buf *);
984263bc
MD
78
79static int write_behind = 1;
80SYSCTL_INT(_vfs, OID_AUTO, write_behind, CTLFLAG_RW, &write_behind, 0, "");
81
82extern vm_page_t bogus_page;
83
84extern int cluster_pbuf_freecnt;
85
86/*
87 * Maximum number of blocks for read-ahead.
88 */
89#define MAXRA 32
90
91/*
92 * This replaces bread.
93 */
94int
54078292 95cluster_read(struct vnode *vp, off_t filesize, off_t loffset,
e54488bb 96 int blksize, size_t resid, int seqcount, struct buf **bpp)
984263bc
MD
97{
98 struct buf *bp, *rbp, *reqbp;
54078292
MD
99 off_t origoffset;
100 off_t doffset;
101 int error;
984263bc
MD
102 int i;
103 int maxra, racluster;
e54488bb 104 int totread;
984263bc
MD
105
106 error = 0;
e54488bb 107 totread = (resid > INT_MAX) ? INT_MAX : (int)resid;
984263bc
MD
108
109 /*
6b84c93e
MD
110 * racluster - calculate maximum cluster IO size (limited by
111 * backing block device).
112 *
113 * Try to limit the amount of read-ahead by a few ad-hoc parameters.
114 * This needs work!!!
115 *
116 * NOTE! The BMAP operations may involve synchronous I/O so we
117 * really want several cluster IOs in progress to absorb
118 * the time lag.
984263bc 119 */
e92ca23a
MD
120 racluster = vmaxiosize(vp) / blksize;
121 maxra = 2 * racluster + (totread / blksize);
984263bc
MD
122 if (maxra > MAXRA)
123 maxra = MAXRA;
6b84c93e
MD
124 if (maxra > nbuf / 8)
125 maxra = nbuf / 8;
984263bc
MD
126
127 /*
ae8e83e6 128 * Get the requested block.
984263bc 129 */
e92ca23a 130 *bpp = reqbp = bp = getblk(vp, loffset, blksize, 0, 0);
54078292 131 origoffset = loffset;
984263bc
MD
132
133 /*
134 * if it is in the cache, then check to see if the reads have been
135 * sequential. If they have, then try some read-ahead, otherwise
136 * back-off on prospective read-aheads.
137 */
138 if (bp->b_flags & B_CACHE) {
6b84c93e
MD
139 /*
140 * Not sequential, do not do any read-ahead
141 */
69adbed4
MD
142 seqcount -= (bp->b_bufsize + BKVASIZE - 1) / BKVASIZE;
143 if (seqcount <= 0 || maxra == 0)
984263bc 144 return 0;
6b84c93e
MD
145
146 /*
147 * No read-ahead mark, do not do any read-ahead
148 * yet.
149 */
150 if ((bp->b_flags & B_RAM) == 0)
984263bc 151 return 0;
b1c20cfa 152
6b84c93e
MD
153 /*
154 * We hit a read-ahead-mark, figure out how much read-ahead
155 * to do (maxra) and where to start (loffset).
156 *
157 * Shortcut the scan. Typically the way this works is that
158 * we've built up all the blocks inbetween except for the
159 * last in previous iterations, so if the second-to-last
160 * block is present we just skip ahead to it.
161 *
162 * This algorithm has O(1) cpu in the steady state no
163 * matter how large maxra is.
164 */
165 bp->b_flags &= ~B_RAM;
166
167 if (findblk(vp, loffset + (maxra - 2) * blksize, FINDBLK_TEST))
168 i = maxra - 1;
169 else
170 i = 1;
171 while (i < maxra) {
172 if (findblk(vp, loffset + i * blksize,
173 FINDBLK_TEST) == NULL) {
174 break;
984263bc 175 }
6b84c93e 176 ++i;
984263bc 177 }
6b84c93e
MD
178 if (i >= maxra)
179 return 0;
180 maxra -= i;
181 loffset += i * blksize;
984263bc
MD
182 reqbp = bp = NULL;
183 } else {
81b5c339 184 off_t firstread = bp->b_loffset;
54078292 185 int nblks;
984263bc 186
ae8e83e6
MD
187 /*
188 * Set-up synchronous read for bp.
189 */
190 bp->b_cmd = BUF_CMD_READ;
191 bp->b_bio1.bio_done = biodone_sync;
192 bp->b_bio1.bio_flags |= BIO_SYNC;
193
81b5c339
MD
194 KASSERT(firstread != NOOFFSET,
195 ("cluster_read: no buffer offset"));
984263bc 196 if (firstread + totread > filesize)
54078292 197 totread = (int)(filesize - firstread);
e92ca23a 198 nblks = totread / blksize;
54078292
MD
199 if (nblks) {
200 int burstbytes;
201
984263bc
MD
202 if (nblks > racluster)
203 nblks = racluster;
204
e92ca23a
MD
205 error = VOP_BMAP(vp, loffset, &doffset,
206 &burstbytes, NULL, BUF_CMD_READ);
984263bc
MD
207 if (error)
208 goto single_block_read;
54078292 209 if (doffset == NOOFFSET)
984263bc 210 goto single_block_read;
e92ca23a 211 if (burstbytes < blksize * 2)
984263bc 212 goto single_block_read;
e92ca23a
MD
213 if (nblks > burstbytes / blksize)
214 nblks = burstbytes / blksize;
984263bc 215
54078292 216 bp = cluster_rbuild(vp, filesize, loffset,
ae8e83e6 217 doffset, blksize, nblks, bp);
54078292 218 loffset += bp->b_bufsize;
6b84c93e 219 maxra -= (bp->b_bufsize - blksize) / blksize;
984263bc
MD
220 } else {
221single_block_read:
222 /*
223 * if it isn't in the cache, then get a chunk from
224 * disk if sequential, otherwise just get the block.
225 */
cf1bb2a8 226 cluster_setram(bp);
e92ca23a 227 loffset += blksize;
984263bc
MD
228 }
229 }
230
984263bc 231 /*
ae8e83e6
MD
232 * If B_CACHE was not set issue bp. bp will either be an
233 * asynchronous cluster buf or a synchronous single-buf.
234 * If it is a single buf it will be the same as reqbp.
235 *
236 * NOTE: Once an async cluster buf is issued bp becomes invalid.
984263bc
MD
237 */
238 if (bp) {
239#if defined(CLUSTERDEBUG)
240 if (rcluster)
69adbed4 241 kprintf("S(%lld,%d,%d)\n",
54078292 242 bp->b_loffset, bp->b_bcount, seqcount);
984263bc 243#endif
10f3fee5
MD
244 if ((bp->b_flags & B_CLUSTER) == 0)
245 vfs_busy_pages(vp, bp);
984263bc 246 bp->b_flags &= ~(B_ERROR|B_INVAL);
69adbed4 247 seqcount -= (bp->b_bufsize + BKVASIZE - 1) / BKVASIZE;
81b5c339 248 vn_strategy(vp, &bp->b_bio1);
ae8e83e6
MD
249 error = 0;
250 /* bp invalid now */
984263bc
MD
251 }
252
253 /*
bfda7080 254 * If we have been doing sequential I/O, then do some read-ahead.
6b84c93e
MD
255 * The code above us should have positioned us at the next likely
256 * offset.
0728eafc
MD
257 *
258 * Only mess with buffers which we can immediately lock. HAMMER
259 * will do device-readahead irrespective of what the blocks
260 * represent.
984263bc 261 */
69adbed4 262 while (!error && seqcount > 0 && maxra > 0 &&
6b84c93e 263 loffset + blksize <= filesize) {
bfda7080
SS
264 int nblksread;
265 int ntoread;
266 int burstbytes;
ac7ffc8a 267 int tmp_error;
bfda7080 268
b77cfc40
MD
269 rbp = getblk(vp, loffset, blksize,
270 GETBLK_SZMATCH|GETBLK_NOWAIT, 0);
271 if (rbp == NULL)
272 goto no_read_ahead;
bfda7080 273 if ((rbp->b_flags & B_CACHE)) {
984263bc 274 bqrelse(rbp);
bfda7080
SS
275 goto no_read_ahead;
276 }
277
ac7ffc8a
MD
278 /*
279 * An error from the read-ahead bmap has nothing to do
280 * with the caller's original request.
281 */
282 tmp_error = VOP_BMAP(vp, loffset, &doffset,
283 &burstbytes, NULL, BUF_CMD_READ);
284 if (tmp_error || doffset == NOOFFSET) {
bfda7080
SS
285 rbp->b_flags |= B_INVAL;
286 brelse(rbp);
287 rbp = NULL;
288 goto no_read_ahead;
289 }
e92ca23a
MD
290 ntoread = burstbytes / blksize;
291 nblksread = (totread + blksize - 1) / blksize;
bfda7080
SS
292 if (seqcount < nblksread)
293 seqcount = nblksread;
ac7ffc8a 294 if (ntoread > seqcount)
bfda7080
SS
295 ntoread = seqcount;
296
ae8e83e6
MD
297 /*
298 * rbp: async read
299 */
300 rbp->b_cmd = BUF_CMD_READ;
cf1bb2a8
MD
301 /*rbp->b_flags |= B_AGE*/;
302 cluster_setram(rbp);
ae8e83e6 303
bfda7080
SS
304 if (burstbytes) {
305 rbp = cluster_rbuild(vp, filesize, loffset,
e92ca23a 306 doffset, blksize,
ae8e83e6 307 ntoread, rbp);
984263bc 308 } else {
bfda7080
SS
309 rbp->b_bio2.bio_offset = doffset;
310 }
69adbed4 311 seqcount -= (rbp->b_bufsize + BKVASIZE - 1) / BKVASIZE;
984263bc 312#if defined(CLUSTERDEBUG)
bfda7080
SS
313 if (rcluster) {
314 if (bp)
69adbed4 315 kprintf("A+(%lld,%d,%lld,%d) ra=%d\n",
bfda7080
SS
316 rbp->b_loffset, rbp->b_bcount,
317 rbp->b_loffset - origoffset,
69adbed4 318 seqcount, maxra);
bfda7080 319 else
69adbed4 320 kprintf("A-(%lld,%d,%lld,%d) ra=%d\n",
bfda7080
SS
321 rbp->b_loffset, rbp->b_bcount,
322 rbp->b_loffset - origoffset,
69adbed4 323 seqcount, maxra);
bfda7080 324 }
984263bc 325#endif
bfda7080 326 rbp->b_flags &= ~(B_ERROR|B_INVAL);
10f3fee5 327
bfda7080
SS
328 if ((rbp->b_flags & B_CLUSTER) == 0)
329 vfs_busy_pages(vp, rbp);
ae8e83e6 330 BUF_KERNPROC(rbp);
6b84c93e
MD
331 loffset += rbp->b_bufsize;
332 maxra -= rbp->b_bufsize / blksize;
bfda7080 333 vn_strategy(vp, &rbp->b_bio1);
ae8e83e6 334 /* rbp invalid now */
984263bc 335 }
bfda7080 336
ae8e83e6
MD
337 /*
338 * Wait for our original buffer to complete its I/O. reqbp will
339 * be NULL if the original buffer was B_CACHE. We are returning
340 * (*bpp) which is the same as reqbp when reqbp != NULL.
341 */
342no_read_ahead:
343 if (reqbp) {
344 KKASSERT(reqbp->b_bio1.bio_flags & BIO_SYNC);
345 error = biowait(&reqbp->b_bio1, "clurd");
346 }
347 return (error);
984263bc
MD
348}
349
350/*
351 * If blocks are contiguous on disk, use this to provide clustered
352 * read ahead. We will read as many blocks as possible sequentially
353 * and then parcel them up into logical blocks in the buffer hash table.
ae8e83e6
MD
354 *
355 * This function either returns a cluster buf or it returns fbp. fbp is
356 * already expected to be set up as a synchronous or asynchronous request.
357 *
358 * If a cluster buf is returned it will always be async.
984263bc
MD
359 */
360static struct buf *
ae8e83e6
MD
361cluster_rbuild(struct vnode *vp, off_t filesize, off_t loffset, off_t doffset,
362 int blksize, int run, struct buf *fbp)
984263bc
MD
363{
364 struct buf *bp, *tbp;
54078292
MD
365 off_t boffset;
366 int i, j;
2ec4b00d 367 int maxiosize = vmaxiosize(vp);
984263bc 368
984263bc
MD
369 /*
370 * avoid a division
371 */
e92ca23a 372 while (loffset + run * blksize > filesize) {
984263bc
MD
373 --run;
374 }
375
6260e485 376 tbp = fbp;
54078292 377 tbp->b_bio2.bio_offset = doffset;
10f3fee5
MD
378 if((tbp->b_flags & B_MALLOC) ||
379 ((tbp->b_flags & B_VMIO) == 0) || (run <= 1)) {
984263bc 380 return tbp;
10f3fee5 381 }
984263bc 382
9a82e536 383 bp = trypbuf_kva(&cluster_pbuf_freecnt);
ae8e83e6 384 if (bp == NULL) {
984263bc 385 return tbp;
ae8e83e6 386 }
984263bc
MD
387
388 /*
389 * We are synthesizing a buffer out of vm_page_t's, but
390 * if the block size is not page aligned then the starting
391 * address may not be either. Inherit the b_data offset
392 * from the original buffer.
393 */
394 bp->b_data = (char *)((vm_offset_t)bp->b_data |
395 ((vm_offset_t)tbp->b_data & PAGE_MASK));
ae8e83e6 396 bp->b_flags |= B_CLUSTER | B_VMIO;
10f3fee5 397 bp->b_cmd = BUF_CMD_READ;
ae8e83e6 398 bp->b_bio1.bio_done = cluster_callback; /* default to async */
81b5c339
MD
399 bp->b_bio1.bio_caller_info1.cluster_head = NULL;
400 bp->b_bio1.bio_caller_info2.cluster_tail = NULL;
54078292 401 bp->b_loffset = loffset;
e92ca23a 402 bp->b_bio2.bio_offset = doffset;
81b5c339
MD
403 KASSERT(bp->b_loffset != NOOFFSET,
404 ("cluster_rbuild: no buffer offset"));
984263bc 405
984263bc
MD
406 bp->b_bcount = 0;
407 bp->b_bufsize = 0;
54f51aeb 408 bp->b_xio.xio_npages = 0;
984263bc 409
e92ca23a 410 for (boffset = doffset, i = 0; i < run; ++i, boffset += blksize) {
10f3fee5 411 if (i) {
54f51aeb 412 if ((bp->b_xio.xio_npages * PAGE_SIZE) +
e92ca23a 413 round_page(blksize) > maxiosize) {
984263bc
MD
414 break;
415 }
416
417 /*
418 * Shortcut some checks and try to avoid buffers that
419 * would block in the lock. The same checks have to
420 * be made again after we officially get the buffer.
421 */
b77cfc40
MD
422 tbp = getblk(vp, loffset + i * blksize, blksize,
423 GETBLK_SZMATCH|GETBLK_NOWAIT, 0);
424 if (tbp == NULL)
425 break;
426 for (j = 0; j < tbp->b_xio.xio_npages; j++) {
427 if (tbp->b_xio.xio_pages[j]->valid)
984263bc
MD
428 break;
429 }
b77cfc40
MD
430 if (j != tbp->b_xio.xio_npages) {
431 bqrelse(tbp);
432 break;
433 }
984263bc
MD
434
435 /*
436 * Stop scanning if the buffer is fuly valid
437 * (marked B_CACHE), or locked (may be doing a
438 * background write), or if the buffer is not
439 * VMIO backed. The clustering code can only deal
440 * with VMIO-backed buffers.
441 */
442 if ((tbp->b_flags & (B_CACHE|B_LOCKED)) ||
27bc0cb1
MD
443 (tbp->b_flags & B_VMIO) == 0 ||
444 (LIST_FIRST(&tbp->b_dep) != NULL &&
445 buf_checkread(tbp))
446 ) {
984263bc
MD
447 bqrelse(tbp);
448 break;
449 }
450
451 /*
452 * The buffer must be completely invalid in order to
453 * take part in the cluster. If it is partially valid
454 * then we stop.
455 */
54f51aeb
HP
456 for (j = 0;j < tbp->b_xio.xio_npages; j++) {
457 if (tbp->b_xio.xio_pages[j]->valid)
984263bc
MD
458 break;
459 }
54f51aeb 460 if (j != tbp->b_xio.xio_npages) {
984263bc
MD
461 bqrelse(tbp);
462 break;
463 }
464
465 /*
466 * Set a read-ahead mark as appropriate
467 */
6260e485 468 if (i == 1 || i == (run - 1))
cf1bb2a8 469 cluster_setram(tbp);
984263bc 470
b86460bf
MD
471 /*
472 * Depress the priority of buffers not explicitly
473 * requested.
474 */
e92ca23a 475 /* tbp->b_flags |= B_AGE; */
b86460bf 476
984263bc 477 /*
984263bc
MD
478 * Set the block number if it isn't set, otherwise
479 * if it is make sure it matches the block number we
480 * expect.
481 */
54078292
MD
482 if (tbp->b_bio2.bio_offset == NOOFFSET) {
483 tbp->b_bio2.bio_offset = boffset;
484 } else if (tbp->b_bio2.bio_offset != boffset) {
984263bc
MD
485 brelse(tbp);
486 break;
487 }
488 }
ae8e83e6 489
984263bc 490 /*
ae8e83e6
MD
491 * The passed-in tbp (i == 0) will already be set up for
492 * async or sync operation. All other tbp's acquire in
493 * our loop are set up for async operation.
984263bc 494 */
10f3fee5 495 tbp->b_cmd = BUF_CMD_READ;
984263bc 496 BUF_KERNPROC(tbp);
81b5c339 497 cluster_append(&bp->b_bio1, tbp);
54078292 498 for (j = 0; j < tbp->b_xio.xio_npages; ++j) {
984263bc 499 vm_page_t m;
54f51aeb 500 m = tbp->b_xio.xio_pages[j];
984263bc
MD
501 vm_page_io_start(m);
502 vm_object_pip_add(m->object, 1);
54f51aeb
HP
503 if ((bp->b_xio.xio_npages == 0) ||
504 (bp->b_xio.xio_pages[bp->b_xio.xio_npages-1] != m)) {
505 bp->b_xio.xio_pages[bp->b_xio.xio_npages] = m;
506 bp->b_xio.xio_npages++;
984263bc
MD
507 }
508 if ((m->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL)
54f51aeb 509 tbp->b_xio.xio_pages[j] = bogus_page;
984263bc
MD
510 }
511 /*
512 * XXX shouldn't this be += size for both, like in
513 * cluster_wbuild()?
514 *
515 * Don't inherit tbp->b_bufsize as it may be larger due to
516 * a non-page-aligned size. Instead just aggregate using
517 * 'size'.
518 */
e92ca23a
MD
519 if (tbp->b_bcount != blksize)
520 kprintf("warning: tbp->b_bcount wrong %d vs %d\n", tbp->b_bcount, blksize);
521 if (tbp->b_bufsize != blksize)
522 kprintf("warning: tbp->b_bufsize wrong %d vs %d\n", tbp->b_bufsize, blksize);
523 bp->b_bcount += blksize;
524 bp->b_bufsize += blksize;
984263bc
MD
525 }
526
527 /*
528 * Fully valid pages in the cluster are already good and do not need
529 * to be re-read from disk. Replace the page with bogus_page
530 */
54f51aeb
HP
531 for (j = 0; j < bp->b_xio.xio_npages; j++) {
532 if ((bp->b_xio.xio_pages[j]->valid & VM_PAGE_BITS_ALL) ==
984263bc 533 VM_PAGE_BITS_ALL) {
54f51aeb 534 bp->b_xio.xio_pages[j] = bogus_page;
984263bc
MD
535 }
536 }
312dcd01 537 if (bp->b_bufsize > bp->b_kvasize) {
54078292 538 panic("cluster_rbuild: b_bufsize(%d) > b_kvasize(%d)",
984263bc 539 bp->b_bufsize, bp->b_kvasize);
312dcd01 540 }
984263bc 541 pmap_qenter(trunc_page((vm_offset_t) bp->b_data),
54f51aeb 542 (vm_page_t *)bp->b_xio.xio_pages, bp->b_xio.xio_npages);
ae8e83e6 543 BUF_KERNPROC(bp);
984263bc
MD
544 return (bp);
545}
546
547/*
548 * Cleanup after a clustered read or write.
549 * This is complicated by the fact that any of the buffers might have
550 * extra memory (if there were no empty buffer headers at allocbuf time)
551 * that we will need to shift around.
81b5c339
MD
552 *
553 * The returned bio is &bp->b_bio1
984263bc
MD
554 */
555void
81b5c339 556cluster_callback(struct bio *bio)
984263bc 557{
81b5c339
MD
558 struct buf *bp = bio->bio_buf;
559 struct buf *tbp;
984263bc
MD
560 int error = 0;
561
562 /*
9a71d53f
MD
563 * Must propogate errors to all the components. A short read (EOF)
564 * is a critical error.
984263bc 565 */
9a71d53f 566 if (bp->b_flags & B_ERROR) {
984263bc 567 error = bp->b_error;
9a71d53f
MD
568 } else if (bp->b_bcount != bp->b_bufsize) {
569 panic("cluster_callback: unexpected EOF on cluster %p!", bio);
570 }
984263bc 571
54f51aeb 572 pmap_qremove(trunc_page((vm_offset_t) bp->b_data), bp->b_xio.xio_npages);
984263bc
MD
573 /*
574 * Move memory from the large cluster buffer into the component
81b5c339
MD
575 * buffers and mark IO as done on these. Since the memory map
576 * is the same, no actual copying is required.
984263bc 577 */
81b5c339
MD
578 while ((tbp = bio->bio_caller_info1.cluster_head) != NULL) {
579 bio->bio_caller_info1.cluster_head = tbp->b_cluster_next;
984263bc
MD
580 if (error) {
581 tbp->b_flags |= B_ERROR;
582 tbp->b_error = error;
583 } else {
584 tbp->b_dirtyoff = tbp->b_dirtyend = 0;
585 tbp->b_flags &= ~(B_ERROR|B_INVAL);
586 /*
587 * XXX the bdwrite()/bqrelse() issued during
588 * cluster building clears B_RELBUF (see bqrelse()
589 * comment). If direct I/O was specified, we have
590 * to restore it here to allow the buffer and VM
591 * to be freed.
592 */
593 if (tbp->b_flags & B_DIRECT)
594 tbp->b_flags |= B_RELBUF;
595 }
81b5c339 596 biodone(&tbp->b_bio1);
984263bc
MD
597 }
598 relpbuf(bp, &cluster_pbuf_freecnt);
599}
600
601/*
602 * cluster_wbuild_wb:
603 *
604 * Implement modified write build for cluster.
605 *
606 * write_behind = 0 write behind disabled
607 * write_behind = 1 write behind normal (default)
608 * write_behind = 2 write behind backed-off
609 */
610
611static __inline int
e92ca23a 612cluster_wbuild_wb(struct vnode *vp, int blksize, off_t start_loffset, int len)
984263bc
MD
613{
614 int r = 0;
615
616 switch(write_behind) {
617 case 2:
54078292 618 if (start_loffset < len)
984263bc 619 break;
54078292 620 start_loffset -= len;
984263bc
MD
621 /* fall through */
622 case 1:
e92ca23a 623 r = cluster_wbuild(vp, blksize, start_loffset, len);
984263bc
MD
624 /* fall through */
625 default:
626 /* fall through */
627 break;
628 }
629 return(r);
630}
631
632/*
633 * Do clustered write for FFS.
634 *
635 * Three cases:
636 * 1. Write is not sequential (write asynchronously)
637 * Write is sequential:
638 * 2. beginning of cluster - begin cluster
639 * 3. middle of a cluster - add to cluster
640 * 4. end of a cluster - asynchronously write cluster
641 */
642void
e92ca23a 643cluster_write(struct buf *bp, off_t filesize, int blksize, int seqcount)
984263bc
MD
644{
645 struct vnode *vp;
54078292 646 off_t loffset;
984263bc 647 int maxclen, cursize;
984263bc
MD
648 int async;
649
650 vp = bp->b_vp;
e92ca23a 651 if (vp->v_type == VREG)
984263bc 652 async = vp->v_mount->mnt_flag & MNT_ASYNC;
e92ca23a 653 else
984263bc 654 async = 0;
54078292 655 loffset = bp->b_loffset;
81b5c339
MD
656 KASSERT(bp->b_loffset != NOOFFSET,
657 ("cluster_write: no buffer offset"));
984263bc
MD
658
659 /* Initialize vnode to beginning of file. */
54078292 660 if (loffset == 0)
984263bc
MD
661 vp->v_lasta = vp->v_clen = vp->v_cstart = vp->v_lastw = 0;
662
e92ca23a 663 if (vp->v_clen == 0 || loffset != vp->v_lastw + blksize ||
54078292 664 bp->b_bio2.bio_offset == NOOFFSET ||
e92ca23a 665 (bp->b_bio2.bio_offset != vp->v_lasta + blksize)) {
2ec4b00d 666 maxclen = vmaxiosize(vp);
984263bc
MD
667 if (vp->v_clen != 0) {
668 /*
669 * Next block is not sequential.
670 *
671 * If we are not writing at end of file, the process
672 * seeked to another point in the file since its last
673 * write, or we have reached our maximum cluster size,
674 * then push the previous cluster. Otherwise try
675 * reallocating to make it sequential.
676 *
677 * Change to algorithm: only push previous cluster if
678 * it was sequential from the point of view of the
679 * seqcount heuristic, otherwise leave the buffer
680 * intact so we can potentially optimize the I/O
681 * later on in the buf_daemon or update daemon
682 * flush.
683 */
e92ca23a
MD
684 cursize = vp->v_lastw - vp->v_cstart + blksize;
685 if (bp->b_loffset + blksize != filesize ||
686 loffset != vp->v_lastw + blksize || vp->v_clen <= cursize) {
984263bc 687 if (!async && seqcount > 0) {
e92ca23a 688 cluster_wbuild_wb(vp, blksize,
984263bc
MD
689 vp->v_cstart, cursize);
690 }
691 } else {
692 struct buf **bpp, **endbp;
693 struct cluster_save *buflist;
694
e92ca23a 695 buflist = cluster_collectbufs(vp, bp, blksize);
984263bc
MD
696 endbp = &buflist->bs_children
697 [buflist->bs_nchildren - 1];
698 if (VOP_REALLOCBLKS(vp, buflist)) {
699 /*
700 * Failed, push the previous cluster
701 * if *really* writing sequentially
702 * in the logical file (seqcount > 1),
703 * otherwise delay it in the hopes that
704 * the low level disk driver can
705 * optimize the write ordering.
706 */
707 for (bpp = buflist->bs_children;
708 bpp < endbp; bpp++)
709 brelse(*bpp);
efda3bd0 710 kfree(buflist, M_SEGMENT);
984263bc
MD
711 if (seqcount > 1) {
712 cluster_wbuild_wb(vp,
e92ca23a 713 blksize, vp->v_cstart,
984263bc
MD
714 cursize);
715 }
716 } else {
717 /*
718 * Succeeded, keep building cluster.
719 */
720 for (bpp = buflist->bs_children;
721 bpp <= endbp; bpp++)
722 bdwrite(*bpp);
efda3bd0 723 kfree(buflist, M_SEGMENT);
54078292
MD
724 vp->v_lastw = loffset;
725 vp->v_lasta = bp->b_bio2.bio_offset;
984263bc
MD
726 return;
727 }
728 }
729 }
730 /*
731 * Consider beginning a cluster. If at end of file, make
732 * cluster as large as possible, otherwise find size of
733 * existing cluster.
734 */
735 if ((vp->v_type == VREG) &&
e92ca23a 736 bp->b_loffset + blksize != filesize &&
54078292 737 (bp->b_bio2.bio_offset == NOOFFSET) &&
e92ca23a 738 (VOP_BMAP(vp, loffset, &bp->b_bio2.bio_offset, &maxclen, NULL, BUF_CMD_WRITE) ||
54078292 739 bp->b_bio2.bio_offset == NOOFFSET)) {
984263bc
MD
740 bawrite(bp);
741 vp->v_clen = 0;
54078292 742 vp->v_lasta = bp->b_bio2.bio_offset;
e92ca23a 743 vp->v_cstart = loffset + blksize;
54078292 744 vp->v_lastw = loffset;
984263bc
MD
745 return;
746 }
e92ca23a
MD
747 if (maxclen > blksize)
748 vp->v_clen = maxclen - blksize;
54078292
MD
749 else
750 vp->v_clen = 0;
751 if (!async && vp->v_clen == 0) { /* I/O not contiguous */
e92ca23a 752 vp->v_cstart = loffset + blksize;
984263bc
MD
753 bawrite(bp);
754 } else { /* Wait for rest of cluster */
54078292 755 vp->v_cstart = loffset;
984263bc
MD
756 bdwrite(bp);
757 }
54078292 758 } else if (loffset == vp->v_cstart + vp->v_clen) {
984263bc
MD
759 /*
760 * At end of cluster, write it out if seqcount tells us we
761 * are operating sequentially, otherwise let the buf or
762 * update daemon handle it.
763 */
764 bdwrite(bp);
765 if (seqcount > 1)
e92ca23a
MD
766 cluster_wbuild_wb(vp, blksize, vp->v_cstart,
767 vp->v_clen + blksize);
984263bc 768 vp->v_clen = 0;
e92ca23a 769 vp->v_cstart = loffset + blksize;
984263bc
MD
770 } else if (vm_page_count_severe()) {
771 /*
772 * We are low on memory, get it going NOW
773 */
774 bawrite(bp);
775 } else {
776 /*
777 * In the middle of a cluster, so just delay the I/O for now.
778 */
779 bdwrite(bp);
780 }
54078292
MD
781 vp->v_lastw = loffset;
782 vp->v_lasta = bp->b_bio2.bio_offset;
984263bc
MD
783}
784
785
786/*
787 * This is an awful lot like cluster_rbuild...wish they could be combined.
788 * The last lbn argument is the current block on which I/O is being
789 * performed. Check to see that it doesn't fall in the middle of
790 * the current block (if last_bp == NULL).
791 */
792int
e92ca23a 793cluster_wbuild(struct vnode *vp, int blksize, off_t start_loffset, int bytes)
984263bc
MD
794{
795 struct buf *bp, *tbp;
e43a034f 796 int i, j;
984263bc 797 int totalwritten = 0;
2ec4b00d 798 int maxiosize = vmaxiosize(vp);
984263bc 799
54078292 800 while (bytes > 0) {
984263bc
MD
801 /*
802 * If the buffer is not delayed-write (i.e. dirty), or it
803 * is delayed-write but either locked or inval, it cannot
804 * partake in the clustered write.
805 */
b1c20cfa
MD
806 tbp = findblk(vp, start_loffset, FINDBLK_NBLOCK);
807 if (tbp == NULL ||
808 (tbp->b_flags & (B_LOCKED | B_INVAL | B_DELWRI)) != B_DELWRI ||
809 (LIST_FIRST(&tbp->b_dep) && buf_checkwrite(tbp))) {
810 if (tbp)
811 BUF_UNLOCK(tbp);
e92ca23a
MD
812 start_loffset += blksize;
813 bytes -= blksize;
984263bc
MD
814 continue;
815 }
816 bremfree(tbp);
10f3fee5 817 KKASSERT(tbp->b_cmd == BUF_CMD_DONE);
984263bc
MD
818
819 /*
820 * Extra memory in the buffer, punt on this buffer.
821 * XXX we could handle this in most cases, but we would
822 * have to push the extra memory down to after our max
823 * possible cluster size and then potentially pull it back
824 * up if the cluster was terminated prematurely--too much
825 * hassle.
826 */
827 if (((tbp->b_flags & (B_CLUSTEROK|B_MALLOC)) != B_CLUSTEROK) ||
b1c20cfa
MD
828 (tbp->b_bcount != tbp->b_bufsize) ||
829 (tbp->b_bcount != blksize) ||
830 (bytes == blksize) ||
9a82e536 831 ((bp = getpbuf_kva(&cluster_pbuf_freecnt)) == NULL)) {
984263bc
MD
832 totalwritten += tbp->b_bufsize;
833 bawrite(tbp);
e92ca23a
MD
834 start_loffset += blksize;
835 bytes -= blksize;
984263bc
MD
836 continue;
837 }
838
839 /*
9a71d53f
MD
840 * Set up the pbuf. Track our append point with b_bcount
841 * and b_bufsize. b_bufsize is not used by the device but
842 * our caller uses it to loop clusters and we use it to
843 * detect a premature EOF on the block device.
984263bc 844 */
984263bc
MD
845 bp->b_bcount = 0;
846 bp->b_bufsize = 0;
54f51aeb 847 bp->b_xio.xio_npages = 0;
81b5c339 848 bp->b_loffset = tbp->b_loffset;
54078292 849 bp->b_bio2.bio_offset = tbp->b_bio2.bio_offset;
984263bc
MD
850
851 /*
852 * We are synthesizing a buffer out of vm_page_t's, but
853 * if the block size is not page aligned then the starting
854 * address may not be either. Inherit the b_data offset
855 * from the original buffer.
856 */
857 bp->b_data = (char *)((vm_offset_t)bp->b_data |
858 ((vm_offset_t)tbp->b_data & PAGE_MASK));
10f3fee5 859 bp->b_flags &= ~B_ERROR;
4414f2c9 860 bp->b_flags |= B_CLUSTER | B_BNOCLIP |
f2d7fcf0 861 (tbp->b_flags & (B_VMIO | B_NEEDCOMMIT));
81b5c339
MD
862 bp->b_bio1.bio_caller_info1.cluster_head = NULL;
863 bp->b_bio1.bio_caller_info2.cluster_tail = NULL;
b1c20cfa 864
984263bc
MD
865 /*
866 * From this location in the file, scan forward to see
867 * if there are buffers with adjacent data that need to
868 * be written as well.
869 */
e92ca23a 870 for (i = 0; i < bytes; (i += blksize), (start_loffset += blksize)) {
984263bc 871 if (i != 0) { /* If not the first buffer */
b1c20cfa
MD
872 tbp = findblk(vp, start_loffset,
873 FINDBLK_NBLOCK);
984263bc 874 /*
b1c20cfa
MD
875 * Buffer not found or could not be locked
876 * non-blocking.
984263bc 877 */
b1c20cfa 878 if (tbp == NULL)
984263bc 879 break;
984263bc
MD
880
881 /*
882 * If it IS in core, but has different
b1c20cfa
MD
883 * characteristics, then don't cluster
884 * with it.
984263bc
MD
885 */
886 if ((tbp->b_flags & (B_VMIO | B_CLUSTEROK |
b1c20cfa
MD
887 B_INVAL | B_DELWRI | B_NEEDCOMMIT))
888 != (B_DELWRI | B_CLUSTEROK |
889 (bp->b_flags & (B_VMIO | B_NEEDCOMMIT))) ||
984263bc 890 (tbp->b_flags & B_LOCKED) ||
b1c20cfa
MD
891 (LIST_FIRST(&tbp->b_dep) &&
892 buf_checkwrite(tbp))
893 ) {
894 BUF_UNLOCK(tbp);
984263bc
MD
895 break;
896 }
897
898 /*
899 * Check that the combined cluster
900 * would make sense with regard to pages
901 * and would not be too large
902 */
e92ca23a 903 if ((tbp->b_bcount != blksize) ||
54078292
MD
904 ((bp->b_bio2.bio_offset + i) !=
905 tbp->b_bio2.bio_offset) ||
54f51aeb 906 ((tbp->b_xio.xio_npages + bp->b_xio.xio_npages) >
2ec4b00d 907 (maxiosize / PAGE_SIZE))) {
984263bc 908 BUF_UNLOCK(tbp);
984263bc
MD
909 break;
910 }
911 /*
912 * Ok, it's passed all the tests,
913 * so remove it from the free list
914 * and mark it busy. We will use it.
915 */
916 bremfree(tbp);
10f3fee5 917 KKASSERT(tbp->b_cmd == BUF_CMD_DONE);
984263bc 918 } /* end of code for non-first buffers only */
81b5c339 919
984263bc
MD
920 /*
921 * If the IO is via the VM then we do some
922 * special VM hackery (yuck). Since the buffer's
923 * block size may not be page-aligned it is possible
924 * for a page to be shared between two buffers. We
925 * have to get rid of the duplication when building
926 * the cluster.
927 */
928 if (tbp->b_flags & B_VMIO) {
929 vm_page_t m;
930
931 if (i != 0) { /* if not first buffer */
54078292 932 for (j = 0; j < tbp->b_xio.xio_npages; ++j) {
54f51aeb 933 m = tbp->b_xio.xio_pages[j];
984263bc
MD
934 if (m->flags & PG_BUSY) {
935 bqrelse(tbp);
936 goto finishcluster;
937 }
938 }
939 }
940
54078292 941 for (j = 0; j < tbp->b_xio.xio_npages; ++j) {
54f51aeb 942 m = tbp->b_xio.xio_pages[j];
984263bc
MD
943 vm_page_io_start(m);
944 vm_object_pip_add(m->object, 1);
54f51aeb
HP
945 if ((bp->b_xio.xio_npages == 0) ||
946 (bp->b_xio.xio_pages[bp->b_xio.xio_npages - 1] != m)) {
947 bp->b_xio.xio_pages[bp->b_xio.xio_npages] = m;
948 bp->b_xio.xio_npages++;
984263bc
MD
949 }
950 }
951 }
e92ca23a
MD
952 bp->b_bcount += blksize;
953 bp->b_bufsize += blksize;
984263bc 954
984263bc 955 bundirty(tbp);
10f3fee5 956 tbp->b_flags &= ~B_ERROR;
10f3fee5 957 tbp->b_cmd = BUF_CMD_WRITE;
984263bc 958 BUF_KERNPROC(tbp);
81b5c339 959 cluster_append(&bp->b_bio1, tbp);
2aee763b
MD
960
961 /*
962 * check for latent dependencies to be handled
963 */
408357d8
MD
964 if (LIST_FIRST(&tbp->b_dep) != NULL)
965 buf_start(tbp);
984263bc
MD
966 }
967 finishcluster:
968 pmap_qenter(trunc_page((vm_offset_t) bp->b_data),
54f51aeb 969 (vm_page_t *) bp->b_xio.xio_pages, bp->b_xio.xio_npages);
312dcd01 970 if (bp->b_bufsize > bp->b_kvasize) {
984263bc 971 panic(
54078292 972 "cluster_wbuild: b_bufsize(%d) > b_kvasize(%d)\n",
984263bc 973 bp->b_bufsize, bp->b_kvasize);
312dcd01 974 }
984263bc
MD
975 totalwritten += bp->b_bufsize;
976 bp->b_dirtyoff = 0;
977 bp->b_dirtyend = bp->b_bufsize;
ae8e83e6 978 bp->b_bio1.bio_done = cluster_callback;
10f3fee5 979 bp->b_cmd = BUF_CMD_WRITE;
ae8e83e6 980
10f3fee5 981 vfs_busy_pages(vp, bp);
a8f169e2 982 bp->b_runningbufspace = bp->b_bufsize;
1b30fbcc
MD
983 if (bp->b_runningbufspace) {
984 runningbufspace += bp->b_runningbufspace;
985 ++runningbufcount;
986 }
ae8e83e6 987 BUF_KERNPROC(bp);
a8f169e2 988 vn_strategy(vp, &bp->b_bio1);
984263bc 989
54078292 990 bytes -= i;
984263bc
MD
991 }
992 return totalwritten;
993}
994
995/*
996 * Collect together all the buffers in a cluster.
997 * Plus add one additional buffer.
998 */
999static struct cluster_save *
e92ca23a 1000cluster_collectbufs(struct vnode *vp, struct buf *last_bp, int blksize)
984263bc
MD
1001{
1002 struct cluster_save *buflist;
1003 struct buf *bp;
54078292 1004 off_t loffset;
984263bc
MD
1005 int i, len;
1006
e92ca23a 1007 len = (int)(vp->v_lastw - vp->v_cstart + blksize) / blksize;
77652cad 1008 buflist = kmalloc(sizeof(struct buf *) * (len + 1) + sizeof(*buflist),
54078292 1009 M_SEGMENT, M_WAITOK);
984263bc
MD
1010 buflist->bs_nchildren = 0;
1011 buflist->bs_children = (struct buf **) (buflist + 1);
e92ca23a 1012 for (loffset = vp->v_cstart, i = 0; i < len; (loffset += blksize), i++) {
54078292 1013 (void) bread(vp, loffset, last_bp->b_bcount, &bp);
984263bc 1014 buflist->bs_children[i] = bp;
54078292 1015 if (bp->b_bio2.bio_offset == NOOFFSET) {
08daea96 1016 VOP_BMAP(bp->b_vp, bp->b_loffset,
e92ca23a
MD
1017 &bp->b_bio2.bio_offset,
1018 NULL, NULL, BUF_CMD_WRITE);
54078292 1019 }
984263bc
MD
1020 }
1021 buflist->bs_children[i] = bp = last_bp;
54078292 1022 if (bp->b_bio2.bio_offset == NOOFFSET) {
e92ca23a
MD
1023 VOP_BMAP(bp->b_vp, bp->b_loffset, &bp->b_bio2.bio_offset,
1024 NULL, NULL, BUF_CMD_WRITE);
54078292 1025 }
984263bc
MD
1026 buflist->bs_nchildren = i + 1;
1027 return (buflist);
1028}
81b5c339
MD
1029
1030void
1031cluster_append(struct bio *bio, struct buf *tbp)
1032{
1033 tbp->b_cluster_next = NULL;
1034 if (bio->bio_caller_info1.cluster_head == NULL) {
1035 bio->bio_caller_info1.cluster_head = tbp;
1036 bio->bio_caller_info2.cluster_tail = tbp;
1037 } else {
1038 bio->bio_caller_info2.cluster_tail->b_cluster_next = tbp;
1039 bio->bio_caller_info2.cluster_tail = tbp;
1040 }
1041}
1042
cf1bb2a8
MD
1043static
1044void
1045cluster_setram (struct buf *bp)
1046{
1047 bp->b_flags |= B_RAM;
1048 if (bp->b_xio.xio_npages)
1049 vm_page_flag_set(bp->b_xio.xio_pages[0], PG_RAM);
1050}