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