Deleted Added
full compact
vfs_cluster.c (254945) vfs_cluster.c (283735)
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 * 4. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 *
31 * @(#)vfs_cluster.c 8.7 (Berkeley) 2/13/94
32 */
33
34#include <sys/cdefs.h>
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 * 4. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 *
31 * @(#)vfs_cluster.c 8.7 (Berkeley) 2/13/94
32 */
33
34#include <sys/cdefs.h>
35__FBSDID("$FreeBSD: head/sys/kern/vfs_cluster.c 254945 2013-08-27 01:31:12Z kib $");
35__FBSDID("$FreeBSD: head/sys/kern/vfs_cluster.c 283735 2015-05-29 13:24:17Z kib $");
36
37#include "opt_debug_cluster.h"
38
39#include <sys/param.h>
40#include <sys/systm.h>
41#include <sys/kernel.h>
42#include <sys/proc.h>
43#include <sys/bio.h>
44#include <sys/buf.h>
45#include <sys/vnode.h>
46#include <sys/malloc.h>
47#include <sys/mount.h>
48#include <sys/resourcevar.h>
49#include <sys/rwlock.h>
50#include <sys/vmmeter.h>
51#include <vm/vm.h>
52#include <vm/vm_object.h>
53#include <vm/vm_page.h>
54#include <sys/sysctl.h>
55
56#if defined(CLUSTERDEBUG)
57static int rcluster= 0;
58SYSCTL_INT(_debug, OID_AUTO, rcluster, CTLFLAG_RW, &rcluster, 0,
59 "Debug VFS clustering code");
60#endif
61
62static MALLOC_DEFINE(M_SEGMENT, "cl_savebuf", "cluster_save buffer");
63
64static struct cluster_save *cluster_collectbufs(struct vnode *vp,
65 struct buf *last_bp, int gbflags);
66static struct buf *cluster_rbuild(struct vnode *vp, u_quad_t filesize,
67 daddr_t lbn, daddr_t blkno, long size, int run, int gbflags,
68 struct buf *fbp);
69static void cluster_callback(struct buf *);
70
71static int write_behind = 1;
72SYSCTL_INT(_vfs, OID_AUTO, write_behind, CTLFLAG_RW, &write_behind, 0,
73 "Cluster write-behind; 0: disable, 1: enable, 2: backed off");
74
75static int read_max = 64;
76SYSCTL_INT(_vfs, OID_AUTO, read_max, CTLFLAG_RW, &read_max, 0,
77 "Cluster read-ahead max block count");
78
79static int read_min = 1;
80SYSCTL_INT(_vfs, OID_AUTO, read_min, CTLFLAG_RW, &read_min, 0,
81 "Cluster read min block count");
82
83/* Page expended to mark partially backed buffers */
84extern vm_page_t bogus_page;
85
86/*
87 * Read data to a buf, including read-ahead if we find this to be beneficial.
88 * cluster_read replaces bread.
89 */
90int
91cluster_read(struct vnode *vp, u_quad_t filesize, daddr_t lblkno, long size,
92 struct ucred *cred, long totread, int seqcount, int gbflags,
93 struct buf **bpp)
94{
95 struct buf *bp, *rbp, *reqbp;
96 struct bufobj *bo;
97 daddr_t blkno, origblkno;
98 int maxra, racluster;
99 int error, ncontig;
100 int i;
101
102 error = 0;
103 bo = &vp->v_bufobj;
104 if (!unmapped_buf_allowed)
105 gbflags &= ~GB_UNMAPPED;
106
107 /*
108 * Try to limit the amount of read-ahead by a few
109 * ad-hoc parameters. This needs work!!!
110 */
111 racluster = vp->v_mount->mnt_iosize_max / size;
112 maxra = seqcount;
113 maxra = min(read_max, maxra);
114 maxra = min(nbuf/8, maxra);
115 if (((u_quad_t)(lblkno + maxra + 1) * size) > filesize)
116 maxra = (filesize / size) - lblkno;
117
118 /*
119 * get the requested block
120 */
121 *bpp = reqbp = bp = getblk(vp, lblkno, size, 0, 0, gbflags);
122 origblkno = lblkno;
123
124 /*
125 * if it is in the cache, then check to see if the reads have been
126 * sequential. If they have, then try some read-ahead, otherwise
127 * back-off on prospective read-aheads.
128 */
129 if (bp->b_flags & B_CACHE) {
130 if (!seqcount) {
131 return 0;
132 } else if ((bp->b_flags & B_RAM) == 0) {
133 return 0;
134 } else {
135 bp->b_flags &= ~B_RAM;
136 BO_RLOCK(bo);
137 for (i = 1; i < maxra; i++) {
138 /*
139 * Stop if the buffer does not exist or it
140 * is invalid (about to go away?)
141 */
142 rbp = gbincore(&vp->v_bufobj, lblkno+i);
143 if (rbp == NULL || (rbp->b_flags & B_INVAL))
144 break;
145
146 /*
147 * Set another read-ahead mark so we know
148 * to check again. (If we can lock the
149 * buffer without waiting)
150 */
151 if ((((i % racluster) == (racluster - 1)) ||
152 (i == (maxra - 1)))
153 && (0 == BUF_LOCK(rbp,
154 LK_EXCLUSIVE | LK_NOWAIT, NULL))) {
155 rbp->b_flags |= B_RAM;
156 BUF_UNLOCK(rbp);
157 }
158 }
159 BO_RUNLOCK(bo);
160 if (i >= maxra) {
161 return 0;
162 }
163 lblkno += i;
164 }
165 reqbp = bp = NULL;
166 /*
167 * If it isn't in the cache, then get a chunk from
168 * disk if sequential, otherwise just get the block.
169 */
170 } else {
171 off_t firstread = bp->b_offset;
172 int nblks;
173 long minread;
174
175 KASSERT(bp->b_offset != NOOFFSET,
176 ("cluster_read: no buffer offset"));
177
178 ncontig = 0;
179
180 /*
181 * Adjust totread if needed
182 */
183 minread = read_min * size;
184 if (minread > totread)
185 totread = minread;
186
187 /*
188 * Compute the total number of blocks that we should read
189 * synchronously.
190 */
191 if (firstread + totread > filesize)
192 totread = filesize - firstread;
193 nblks = howmany(totread, size);
194 if (nblks > racluster)
195 nblks = racluster;
196
197 /*
198 * Now compute the number of contiguous blocks.
199 */
200 if (nblks > 1) {
201 error = VOP_BMAP(vp, lblkno, NULL,
202 &blkno, &ncontig, NULL);
203 /*
204 * If this failed to map just do the original block.
205 */
206 if (error || blkno == -1)
207 ncontig = 0;
208 }
209
210 /*
211 * If we have contiguous data available do a cluster
212 * otherwise just read the requested block.
213 */
214 if (ncontig) {
215 /* Account for our first block. */
216 ncontig = min(ncontig + 1, nblks);
217 if (ncontig < nblks)
218 nblks = ncontig;
219 bp = cluster_rbuild(vp, filesize, lblkno,
220 blkno, size, nblks, gbflags, bp);
221 lblkno += (bp->b_bufsize / size);
222 } else {
223 bp->b_flags |= B_RAM;
224 bp->b_iocmd = BIO_READ;
225 lblkno += 1;
226 }
227 }
228
229 /*
230 * handle the synchronous read so that it is available ASAP.
231 */
232 if (bp) {
233 if ((bp->b_flags & B_CLUSTER) == 0) {
234 vfs_busy_pages(bp, 0);
235 }
236 bp->b_flags &= ~B_INVAL;
237 bp->b_ioflags &= ~BIO_ERROR;
238 if ((bp->b_flags & B_ASYNC) || bp->b_iodone != NULL)
239 BUF_KERNPROC(bp);
240 bp->b_iooffset = dbtob(bp->b_blkno);
241 bstrategy(bp);
242 curthread->td_ru.ru_inblock++;
243 }
244
245 /*
246 * If we have been doing sequential I/O, then do some read-ahead.
247 */
248 while (lblkno < (origblkno + maxra)) {
249 error = VOP_BMAP(vp, lblkno, NULL, &blkno, &ncontig, NULL);
250 if (error)
251 break;
252
253 if (blkno == -1)
254 break;
255
256 /*
257 * We could throttle ncontig here by maxra but we might as
258 * well read the data if it is contiguous. We're throttled
259 * by racluster anyway.
260 */
261 if (ncontig) {
262 ncontig = min(ncontig + 1, racluster);
263 rbp = cluster_rbuild(vp, filesize, lblkno, blkno,
264 size, ncontig, gbflags, NULL);
265 lblkno += (rbp->b_bufsize / size);
266 if (rbp->b_flags & B_DELWRI) {
267 bqrelse(rbp);
268 continue;
269 }
270 } else {
271 rbp = getblk(vp, lblkno, size, 0, 0, gbflags);
272 lblkno += 1;
273 if (rbp->b_flags & B_DELWRI) {
274 bqrelse(rbp);
275 continue;
276 }
277 rbp->b_flags |= B_ASYNC | B_RAM;
278 rbp->b_iocmd = BIO_READ;
279 rbp->b_blkno = blkno;
280 }
281 if (rbp->b_flags & B_CACHE) {
282 rbp->b_flags &= ~B_ASYNC;
283 bqrelse(rbp);
284 continue;
285 }
286 if ((rbp->b_flags & B_CLUSTER) == 0) {
287 vfs_busy_pages(rbp, 0);
288 }
289 rbp->b_flags &= ~B_INVAL;
290 rbp->b_ioflags &= ~BIO_ERROR;
291 if ((rbp->b_flags & B_ASYNC) || rbp->b_iodone != NULL)
292 BUF_KERNPROC(rbp);
293 rbp->b_iooffset = dbtob(rbp->b_blkno);
294 bstrategy(rbp);
295 curthread->td_ru.ru_inblock++;
296 }
297
298 if (reqbp)
299 return (bufwait(reqbp));
300 else
301 return (error);
302}
303
304/*
305 * If blocks are contiguous on disk, use this to provide clustered
306 * read ahead. We will read as many blocks as possible sequentially
307 * and then parcel them up into logical blocks in the buffer hash table.
308 */
309static struct buf *
310cluster_rbuild(struct vnode *vp, u_quad_t filesize, daddr_t lbn,
311 daddr_t blkno, long size, int run, int gbflags, struct buf *fbp)
312{
36
37#include "opt_debug_cluster.h"
38
39#include <sys/param.h>
40#include <sys/systm.h>
41#include <sys/kernel.h>
42#include <sys/proc.h>
43#include <sys/bio.h>
44#include <sys/buf.h>
45#include <sys/vnode.h>
46#include <sys/malloc.h>
47#include <sys/mount.h>
48#include <sys/resourcevar.h>
49#include <sys/rwlock.h>
50#include <sys/vmmeter.h>
51#include <vm/vm.h>
52#include <vm/vm_object.h>
53#include <vm/vm_page.h>
54#include <sys/sysctl.h>
55
56#if defined(CLUSTERDEBUG)
57static int rcluster= 0;
58SYSCTL_INT(_debug, OID_AUTO, rcluster, CTLFLAG_RW, &rcluster, 0,
59 "Debug VFS clustering code");
60#endif
61
62static MALLOC_DEFINE(M_SEGMENT, "cl_savebuf", "cluster_save buffer");
63
64static struct cluster_save *cluster_collectbufs(struct vnode *vp,
65 struct buf *last_bp, int gbflags);
66static struct buf *cluster_rbuild(struct vnode *vp, u_quad_t filesize,
67 daddr_t lbn, daddr_t blkno, long size, int run, int gbflags,
68 struct buf *fbp);
69static void cluster_callback(struct buf *);
70
71static int write_behind = 1;
72SYSCTL_INT(_vfs, OID_AUTO, write_behind, CTLFLAG_RW, &write_behind, 0,
73 "Cluster write-behind; 0: disable, 1: enable, 2: backed off");
74
75static int read_max = 64;
76SYSCTL_INT(_vfs, OID_AUTO, read_max, CTLFLAG_RW, &read_max, 0,
77 "Cluster read-ahead max block count");
78
79static int read_min = 1;
80SYSCTL_INT(_vfs, OID_AUTO, read_min, CTLFLAG_RW, &read_min, 0,
81 "Cluster read min block count");
82
83/* Page expended to mark partially backed buffers */
84extern vm_page_t bogus_page;
85
86/*
87 * Read data to a buf, including read-ahead if we find this to be beneficial.
88 * cluster_read replaces bread.
89 */
90int
91cluster_read(struct vnode *vp, u_quad_t filesize, daddr_t lblkno, long size,
92 struct ucred *cred, long totread, int seqcount, int gbflags,
93 struct buf **bpp)
94{
95 struct buf *bp, *rbp, *reqbp;
96 struct bufobj *bo;
97 daddr_t blkno, origblkno;
98 int maxra, racluster;
99 int error, ncontig;
100 int i;
101
102 error = 0;
103 bo = &vp->v_bufobj;
104 if (!unmapped_buf_allowed)
105 gbflags &= ~GB_UNMAPPED;
106
107 /*
108 * Try to limit the amount of read-ahead by a few
109 * ad-hoc parameters. This needs work!!!
110 */
111 racluster = vp->v_mount->mnt_iosize_max / size;
112 maxra = seqcount;
113 maxra = min(read_max, maxra);
114 maxra = min(nbuf/8, maxra);
115 if (((u_quad_t)(lblkno + maxra + 1) * size) > filesize)
116 maxra = (filesize / size) - lblkno;
117
118 /*
119 * get the requested block
120 */
121 *bpp = reqbp = bp = getblk(vp, lblkno, size, 0, 0, gbflags);
122 origblkno = lblkno;
123
124 /*
125 * if it is in the cache, then check to see if the reads have been
126 * sequential. If they have, then try some read-ahead, otherwise
127 * back-off on prospective read-aheads.
128 */
129 if (bp->b_flags & B_CACHE) {
130 if (!seqcount) {
131 return 0;
132 } else if ((bp->b_flags & B_RAM) == 0) {
133 return 0;
134 } else {
135 bp->b_flags &= ~B_RAM;
136 BO_RLOCK(bo);
137 for (i = 1; i < maxra; i++) {
138 /*
139 * Stop if the buffer does not exist or it
140 * is invalid (about to go away?)
141 */
142 rbp = gbincore(&vp->v_bufobj, lblkno+i);
143 if (rbp == NULL || (rbp->b_flags & B_INVAL))
144 break;
145
146 /*
147 * Set another read-ahead mark so we know
148 * to check again. (If we can lock the
149 * buffer without waiting)
150 */
151 if ((((i % racluster) == (racluster - 1)) ||
152 (i == (maxra - 1)))
153 && (0 == BUF_LOCK(rbp,
154 LK_EXCLUSIVE | LK_NOWAIT, NULL))) {
155 rbp->b_flags |= B_RAM;
156 BUF_UNLOCK(rbp);
157 }
158 }
159 BO_RUNLOCK(bo);
160 if (i >= maxra) {
161 return 0;
162 }
163 lblkno += i;
164 }
165 reqbp = bp = NULL;
166 /*
167 * If it isn't in the cache, then get a chunk from
168 * disk if sequential, otherwise just get the block.
169 */
170 } else {
171 off_t firstread = bp->b_offset;
172 int nblks;
173 long minread;
174
175 KASSERT(bp->b_offset != NOOFFSET,
176 ("cluster_read: no buffer offset"));
177
178 ncontig = 0;
179
180 /*
181 * Adjust totread if needed
182 */
183 minread = read_min * size;
184 if (minread > totread)
185 totread = minread;
186
187 /*
188 * Compute the total number of blocks that we should read
189 * synchronously.
190 */
191 if (firstread + totread > filesize)
192 totread = filesize - firstread;
193 nblks = howmany(totread, size);
194 if (nblks > racluster)
195 nblks = racluster;
196
197 /*
198 * Now compute the number of contiguous blocks.
199 */
200 if (nblks > 1) {
201 error = VOP_BMAP(vp, lblkno, NULL,
202 &blkno, &ncontig, NULL);
203 /*
204 * If this failed to map just do the original block.
205 */
206 if (error || blkno == -1)
207 ncontig = 0;
208 }
209
210 /*
211 * If we have contiguous data available do a cluster
212 * otherwise just read the requested block.
213 */
214 if (ncontig) {
215 /* Account for our first block. */
216 ncontig = min(ncontig + 1, nblks);
217 if (ncontig < nblks)
218 nblks = ncontig;
219 bp = cluster_rbuild(vp, filesize, lblkno,
220 blkno, size, nblks, gbflags, bp);
221 lblkno += (bp->b_bufsize / size);
222 } else {
223 bp->b_flags |= B_RAM;
224 bp->b_iocmd = BIO_READ;
225 lblkno += 1;
226 }
227 }
228
229 /*
230 * handle the synchronous read so that it is available ASAP.
231 */
232 if (bp) {
233 if ((bp->b_flags & B_CLUSTER) == 0) {
234 vfs_busy_pages(bp, 0);
235 }
236 bp->b_flags &= ~B_INVAL;
237 bp->b_ioflags &= ~BIO_ERROR;
238 if ((bp->b_flags & B_ASYNC) || bp->b_iodone != NULL)
239 BUF_KERNPROC(bp);
240 bp->b_iooffset = dbtob(bp->b_blkno);
241 bstrategy(bp);
242 curthread->td_ru.ru_inblock++;
243 }
244
245 /*
246 * If we have been doing sequential I/O, then do some read-ahead.
247 */
248 while (lblkno < (origblkno + maxra)) {
249 error = VOP_BMAP(vp, lblkno, NULL, &blkno, &ncontig, NULL);
250 if (error)
251 break;
252
253 if (blkno == -1)
254 break;
255
256 /*
257 * We could throttle ncontig here by maxra but we might as
258 * well read the data if it is contiguous. We're throttled
259 * by racluster anyway.
260 */
261 if (ncontig) {
262 ncontig = min(ncontig + 1, racluster);
263 rbp = cluster_rbuild(vp, filesize, lblkno, blkno,
264 size, ncontig, gbflags, NULL);
265 lblkno += (rbp->b_bufsize / size);
266 if (rbp->b_flags & B_DELWRI) {
267 bqrelse(rbp);
268 continue;
269 }
270 } else {
271 rbp = getblk(vp, lblkno, size, 0, 0, gbflags);
272 lblkno += 1;
273 if (rbp->b_flags & B_DELWRI) {
274 bqrelse(rbp);
275 continue;
276 }
277 rbp->b_flags |= B_ASYNC | B_RAM;
278 rbp->b_iocmd = BIO_READ;
279 rbp->b_blkno = blkno;
280 }
281 if (rbp->b_flags & B_CACHE) {
282 rbp->b_flags &= ~B_ASYNC;
283 bqrelse(rbp);
284 continue;
285 }
286 if ((rbp->b_flags & B_CLUSTER) == 0) {
287 vfs_busy_pages(rbp, 0);
288 }
289 rbp->b_flags &= ~B_INVAL;
290 rbp->b_ioflags &= ~BIO_ERROR;
291 if ((rbp->b_flags & B_ASYNC) || rbp->b_iodone != NULL)
292 BUF_KERNPROC(rbp);
293 rbp->b_iooffset = dbtob(rbp->b_blkno);
294 bstrategy(rbp);
295 curthread->td_ru.ru_inblock++;
296 }
297
298 if (reqbp)
299 return (bufwait(reqbp));
300 else
301 return (error);
302}
303
304/*
305 * If blocks are contiguous on disk, use this to provide clustered
306 * read ahead. We will read as many blocks as possible sequentially
307 * and then parcel them up into logical blocks in the buffer hash table.
308 */
309static struct buf *
310cluster_rbuild(struct vnode *vp, u_quad_t filesize, daddr_t lbn,
311 daddr_t blkno, long size, int run, int gbflags, struct buf *fbp)
312{
313 struct bufobj *bo;
314 struct buf *bp, *tbp;
315 daddr_t bn;
316 off_t off;
317 long tinc, tsize;
318 int i, inc, j, k, toff;
319
320 KASSERT(size == vp->v_mount->mnt_stat.f_iosize,
321 ("cluster_rbuild: size %ld != f_iosize %jd\n",
322 size, (intmax_t)vp->v_mount->mnt_stat.f_iosize));
323
324 /*
325 * avoid a division
326 */
327 while ((u_quad_t) size * (lbn + run) > filesize) {
328 --run;
329 }
330
331 if (fbp) {
332 tbp = fbp;
333 tbp->b_iocmd = BIO_READ;
334 } else {
335 tbp = getblk(vp, lbn, size, 0, 0, gbflags);
336 if (tbp->b_flags & B_CACHE)
337 return tbp;
338 tbp->b_flags |= B_ASYNC | B_RAM;
339 tbp->b_iocmd = BIO_READ;
340 }
341 tbp->b_blkno = blkno;
342 if( (tbp->b_flags & B_MALLOC) ||
343 ((tbp->b_flags & B_VMIO) == 0) || (run <= 1) )
344 return tbp;
345
346 bp = trypbuf(&cluster_pbuf_freecnt);
347 if (bp == 0)
348 return tbp;
349
350 /*
351 * We are synthesizing a buffer out of vm_page_t's, but
352 * if the block size is not page aligned then the starting
353 * address may not be either. Inherit the b_data offset
354 * from the original buffer.
355 */
356 bp->b_flags = B_ASYNC | B_CLUSTER | B_VMIO;
357 if ((gbflags & GB_UNMAPPED) != 0) {
358 bp->b_flags |= B_UNMAPPED;
359 bp->b_data = unmapped_buf;
360 } else {
361 bp->b_data = (char *)((vm_offset_t)bp->b_data |
362 ((vm_offset_t)tbp->b_data & PAGE_MASK));
363 }
364 bp->b_iocmd = BIO_READ;
365 bp->b_iodone = cluster_callback;
366 bp->b_blkno = blkno;
367 bp->b_lblkno = lbn;
368 bp->b_offset = tbp->b_offset;
369 KASSERT(bp->b_offset != NOOFFSET, ("cluster_rbuild: no buffer offset"));
370 pbgetvp(vp, bp);
371
372 TAILQ_INIT(&bp->b_cluster.cluster_head);
373
374 bp->b_bcount = 0;
375 bp->b_bufsize = 0;
376 bp->b_npages = 0;
377
378 inc = btodb(size);
313 struct buf *bp, *tbp;
314 daddr_t bn;
315 off_t off;
316 long tinc, tsize;
317 int i, inc, j, k, toff;
318
319 KASSERT(size == vp->v_mount->mnt_stat.f_iosize,
320 ("cluster_rbuild: size %ld != f_iosize %jd\n",
321 size, (intmax_t)vp->v_mount->mnt_stat.f_iosize));
322
323 /*
324 * avoid a division
325 */
326 while ((u_quad_t) size * (lbn + run) > filesize) {
327 --run;
328 }
329
330 if (fbp) {
331 tbp = fbp;
332 tbp->b_iocmd = BIO_READ;
333 } else {
334 tbp = getblk(vp, lbn, size, 0, 0, gbflags);
335 if (tbp->b_flags & B_CACHE)
336 return tbp;
337 tbp->b_flags |= B_ASYNC | B_RAM;
338 tbp->b_iocmd = BIO_READ;
339 }
340 tbp->b_blkno = blkno;
341 if( (tbp->b_flags & B_MALLOC) ||
342 ((tbp->b_flags & B_VMIO) == 0) || (run <= 1) )
343 return tbp;
344
345 bp = trypbuf(&cluster_pbuf_freecnt);
346 if (bp == 0)
347 return tbp;
348
349 /*
350 * We are synthesizing a buffer out of vm_page_t's, but
351 * if the block size is not page aligned then the starting
352 * address may not be either. Inherit the b_data offset
353 * from the original buffer.
354 */
355 bp->b_flags = B_ASYNC | B_CLUSTER | B_VMIO;
356 if ((gbflags & GB_UNMAPPED) != 0) {
357 bp->b_flags |= B_UNMAPPED;
358 bp->b_data = unmapped_buf;
359 } else {
360 bp->b_data = (char *)((vm_offset_t)bp->b_data |
361 ((vm_offset_t)tbp->b_data & PAGE_MASK));
362 }
363 bp->b_iocmd = BIO_READ;
364 bp->b_iodone = cluster_callback;
365 bp->b_blkno = blkno;
366 bp->b_lblkno = lbn;
367 bp->b_offset = tbp->b_offset;
368 KASSERT(bp->b_offset != NOOFFSET, ("cluster_rbuild: no buffer offset"));
369 pbgetvp(vp, bp);
370
371 TAILQ_INIT(&bp->b_cluster.cluster_head);
372
373 bp->b_bcount = 0;
374 bp->b_bufsize = 0;
375 bp->b_npages = 0;
376
377 inc = btodb(size);
379 bo = &vp->v_bufobj;
380 for (bn = blkno, i = 0; i < run; ++i, bn += inc) {
381 if (i == 0) {
382 VM_OBJECT_WLOCK(tbp->b_bufobj->bo_object);
383 vfs_drain_busy_pages(tbp);
384 vm_object_pip_add(tbp->b_bufobj->bo_object,
385 tbp->b_npages);
386 for (k = 0; k < tbp->b_npages; k++)
387 vm_page_sbusy(tbp->b_pages[k]);
388 VM_OBJECT_WUNLOCK(tbp->b_bufobj->bo_object);
389 } else {
390 if ((bp->b_npages * PAGE_SIZE) +
391 round_page(size) > vp->v_mount->mnt_iosize_max) {
392 break;
393 }
394
395 tbp = getblk(vp, lbn + i, size, 0, 0, GB_LOCK_NOWAIT |
396 (gbflags & GB_UNMAPPED));
397
398 /* Don't wait around for locked bufs. */
399 if (tbp == NULL)
400 break;
401
402 /*
403 * Stop scanning if the buffer is fully valid
404 * (marked B_CACHE), or locked (may be doing a
405 * background write), or if the buffer is not
406 * VMIO backed. The clustering code can only deal
407 * with VMIO-backed buffers. The bo lock is not
408 * required for the BKGRDINPROG check since it
409 * can not be set without the buf lock.
410 */
411 if ((tbp->b_vflags & BV_BKGRDINPROG) ||
412 (tbp->b_flags & B_CACHE) ||
413 (tbp->b_flags & B_VMIO) == 0) {
414 bqrelse(tbp);
415 break;
416 }
417
418 /*
419 * The buffer must be completely invalid in order to
420 * take part in the cluster. If it is partially valid
421 * then we stop.
422 */
423 off = tbp->b_offset;
424 tsize = size;
425 VM_OBJECT_WLOCK(tbp->b_bufobj->bo_object);
426 for (j = 0; tsize > 0; j++) {
427 toff = off & PAGE_MASK;
428 tinc = tsize;
429 if (toff + tinc > PAGE_SIZE)
430 tinc = PAGE_SIZE - toff;
431 VM_OBJECT_ASSERT_WLOCKED(tbp->b_pages[j]->object);
432 if ((tbp->b_pages[j]->valid &
433 vm_page_bits(toff, tinc)) != 0)
434 break;
435 if (vm_page_xbusied(tbp->b_pages[j]))
436 break;
437 vm_object_pip_add(tbp->b_bufobj->bo_object, 1);
438 vm_page_sbusy(tbp->b_pages[j]);
439 off += tinc;
440 tsize -= tinc;
441 }
442 if (tsize > 0) {
443clean_sbusy:
444 vm_object_pip_add(tbp->b_bufobj->bo_object, -j);
445 for (k = 0; k < j; k++)
446 vm_page_sunbusy(tbp->b_pages[k]);
447 VM_OBJECT_WUNLOCK(tbp->b_bufobj->bo_object);
448 bqrelse(tbp);
449 break;
450 }
451 VM_OBJECT_WUNLOCK(tbp->b_bufobj->bo_object);
452
453 /*
454 * Set a read-ahead mark as appropriate
455 */
456 if ((fbp && (i == 1)) || (i == (run - 1)))
457 tbp->b_flags |= B_RAM;
458
459 /*
460 * Set the buffer up for an async read (XXX should
461 * we do this only if we do not wind up brelse()ing?).
462 * Set the block number if it isn't set, otherwise
463 * if it is make sure it matches the block number we
464 * expect.
465 */
466 tbp->b_flags |= B_ASYNC;
467 tbp->b_iocmd = BIO_READ;
468 if (tbp->b_blkno == tbp->b_lblkno) {
469 tbp->b_blkno = bn;
470 } else if (tbp->b_blkno != bn) {
471 VM_OBJECT_WLOCK(tbp->b_bufobj->bo_object);
472 goto clean_sbusy;
473 }
474 }
475 /*
476 * XXX fbp from caller may not be B_ASYNC, but we are going
477 * to biodone() it in cluster_callback() anyway
478 */
479 BUF_KERNPROC(tbp);
480 TAILQ_INSERT_TAIL(&bp->b_cluster.cluster_head,
481 tbp, b_cluster.cluster_entry);
482 VM_OBJECT_WLOCK(tbp->b_bufobj->bo_object);
483 for (j = 0; j < tbp->b_npages; j += 1) {
484 vm_page_t m;
485 m = tbp->b_pages[j];
486 if ((bp->b_npages == 0) ||
487 (bp->b_pages[bp->b_npages-1] != m)) {
488 bp->b_pages[bp->b_npages] = m;
489 bp->b_npages++;
490 }
491 if (m->valid == VM_PAGE_BITS_ALL)
492 tbp->b_pages[j] = bogus_page;
493 }
494 VM_OBJECT_WUNLOCK(tbp->b_bufobj->bo_object);
495 /*
496 * Don't inherit tbp->b_bufsize as it may be larger due to
497 * a non-page-aligned size. Instead just aggregate using
498 * 'size'.
499 */
500 if (tbp->b_bcount != size)
501 printf("warning: tbp->b_bcount wrong %ld vs %ld\n", tbp->b_bcount, size);
502 if (tbp->b_bufsize != size)
503 printf("warning: tbp->b_bufsize wrong %ld vs %ld\n", tbp->b_bufsize, size);
504 bp->b_bcount += size;
505 bp->b_bufsize += size;
506 }
507
508 /*
509 * Fully valid pages in the cluster are already good and do not need
510 * to be re-read from disk. Replace the page with bogus_page
511 */
512 VM_OBJECT_WLOCK(bp->b_bufobj->bo_object);
513 for (j = 0; j < bp->b_npages; j++) {
514 VM_OBJECT_ASSERT_WLOCKED(bp->b_pages[j]->object);
515 if (bp->b_pages[j]->valid == VM_PAGE_BITS_ALL)
516 bp->b_pages[j] = bogus_page;
517 }
518 VM_OBJECT_WUNLOCK(bp->b_bufobj->bo_object);
519 if (bp->b_bufsize > bp->b_kvasize)
520 panic("cluster_rbuild: b_bufsize(%ld) > b_kvasize(%d)\n",
521 bp->b_bufsize, bp->b_kvasize);
522 bp->b_kvasize = bp->b_bufsize;
523
524 if ((bp->b_flags & B_UNMAPPED) == 0) {
525 pmap_qenter(trunc_page((vm_offset_t) bp->b_data),
526 (vm_page_t *)bp->b_pages, bp->b_npages);
527 }
528 return (bp);
529}
530
531/*
532 * Cleanup after a clustered read or write.
533 * This is complicated by the fact that any of the buffers might have
534 * extra memory (if there were no empty buffer headers at allocbuf time)
535 * that we will need to shift around.
536 */
537static void
538cluster_callback(bp)
539 struct buf *bp;
540{
541 struct buf *nbp, *tbp;
542 int error = 0;
543
544 /*
545 * Must propogate errors to all the components.
546 */
547 if (bp->b_ioflags & BIO_ERROR)
548 error = bp->b_error;
549
550 if ((bp->b_flags & B_UNMAPPED) == 0) {
551 pmap_qremove(trunc_page((vm_offset_t) bp->b_data),
552 bp->b_npages);
553 }
554 /*
555 * Move memory from the large cluster buffer into the component
556 * buffers and mark IO as done on these.
557 */
558 for (tbp = TAILQ_FIRST(&bp->b_cluster.cluster_head);
559 tbp; tbp = nbp) {
560 nbp = TAILQ_NEXT(&tbp->b_cluster, cluster_entry);
561 if (error) {
562 tbp->b_ioflags |= BIO_ERROR;
563 tbp->b_error = error;
564 } else {
565 tbp->b_dirtyoff = tbp->b_dirtyend = 0;
566 tbp->b_flags &= ~B_INVAL;
567 tbp->b_ioflags &= ~BIO_ERROR;
568 /*
569 * XXX the bdwrite()/bqrelse() issued during
570 * cluster building clears B_RELBUF (see bqrelse()
571 * comment). If direct I/O was specified, we have
572 * to restore it here to allow the buffer and VM
573 * to be freed.
574 */
575 if (tbp->b_flags & B_DIRECT)
576 tbp->b_flags |= B_RELBUF;
577 }
578 bufdone(tbp);
579 }
580 pbrelvp(bp);
581 relpbuf(bp, &cluster_pbuf_freecnt);
582}
583
584/*
585 * cluster_wbuild_wb:
586 *
587 * Implement modified write build for cluster.
588 *
589 * write_behind = 0 write behind disabled
590 * write_behind = 1 write behind normal (default)
591 * write_behind = 2 write behind backed-off
592 */
593
594static __inline int
595cluster_wbuild_wb(struct vnode *vp, long size, daddr_t start_lbn, int len,
596 int gbflags)
597{
598 int r = 0;
599
600 switch (write_behind) {
601 case 2:
602 if (start_lbn < len)
603 break;
604 start_lbn -= len;
605 /* FALLTHROUGH */
606 case 1:
607 r = cluster_wbuild(vp, size, start_lbn, len, gbflags);
608 /* FALLTHROUGH */
609 default:
610 /* FALLTHROUGH */
611 break;
612 }
613 return(r);
614}
615
616/*
617 * Do clustered write for FFS.
618 *
619 * Three cases:
620 * 1. Write is not sequential (write asynchronously)
621 * Write is sequential:
622 * 2. beginning of cluster - begin cluster
623 * 3. middle of a cluster - add to cluster
624 * 4. end of a cluster - asynchronously write cluster
625 */
626void
627cluster_write(struct vnode *vp, struct buf *bp, u_quad_t filesize, int seqcount,
628 int gbflags)
629{
630 daddr_t lbn;
631 int maxclen, cursize;
632 int lblocksize;
633 int async;
634
635 if (!unmapped_buf_allowed)
636 gbflags &= ~GB_UNMAPPED;
637
638 if (vp->v_type == VREG) {
639 async = DOINGASYNC(vp);
640 lblocksize = vp->v_mount->mnt_stat.f_iosize;
641 } else {
642 async = 0;
643 lblocksize = bp->b_bufsize;
644 }
645 lbn = bp->b_lblkno;
646 KASSERT(bp->b_offset != NOOFFSET, ("cluster_write: no buffer offset"));
647
648 /* Initialize vnode to beginning of file. */
649 if (lbn == 0)
650 vp->v_lasta = vp->v_clen = vp->v_cstart = vp->v_lastw = 0;
651
652 if (vp->v_clen == 0 || lbn != vp->v_lastw + 1 ||
653 (bp->b_blkno != vp->v_lasta + btodb(lblocksize))) {
654 maxclen = vp->v_mount->mnt_iosize_max / lblocksize - 1;
655 if (vp->v_clen != 0) {
656 /*
657 * Next block is not sequential.
658 *
659 * If we are not writing at end of file, the process
660 * seeked to another point in the file since its last
661 * write, or we have reached our maximum cluster size,
662 * then push the previous cluster. Otherwise try
663 * reallocating to make it sequential.
664 *
665 * Change to algorithm: only push previous cluster if
666 * it was sequential from the point of view of the
667 * seqcount heuristic, otherwise leave the buffer
668 * intact so we can potentially optimize the I/O
669 * later on in the buf_daemon or update daemon
670 * flush.
671 */
672 cursize = vp->v_lastw - vp->v_cstart + 1;
673 if (((u_quad_t) bp->b_offset + lblocksize) != filesize ||
674 lbn != vp->v_lastw + 1 || vp->v_clen <= cursize) {
675 if (!async && seqcount > 0) {
676 cluster_wbuild_wb(vp, lblocksize,
677 vp->v_cstart, cursize, gbflags);
678 }
679 } else {
680 struct buf **bpp, **endbp;
681 struct cluster_save *buflist;
682
683 buflist = cluster_collectbufs(vp, bp, gbflags);
684 endbp = &buflist->bs_children
685 [buflist->bs_nchildren - 1];
686 if (VOP_REALLOCBLKS(vp, buflist)) {
687 /*
688 * Failed, push the previous cluster
689 * if *really* writing sequentially
690 * in the logical file (seqcount > 1),
691 * otherwise delay it in the hopes that
692 * the low level disk driver can
693 * optimize the write ordering.
694 */
695 for (bpp = buflist->bs_children;
696 bpp < endbp; bpp++)
697 brelse(*bpp);
698 free(buflist, M_SEGMENT);
699 if (seqcount > 1) {
700 cluster_wbuild_wb(vp,
701 lblocksize, vp->v_cstart,
702 cursize, gbflags);
703 }
704 } else {
705 /*
706 * Succeeded, keep building cluster.
707 */
708 for (bpp = buflist->bs_children;
709 bpp <= endbp; bpp++)
710 bdwrite(*bpp);
711 free(buflist, M_SEGMENT);
712 vp->v_lastw = lbn;
713 vp->v_lasta = bp->b_blkno;
714 return;
715 }
716 }
717 }
718 /*
719 * Consider beginning a cluster. If at end of file, make
720 * cluster as large as possible, otherwise find size of
721 * existing cluster.
722 */
723 if ((vp->v_type == VREG) &&
724 ((u_quad_t) bp->b_offset + lblocksize) != filesize &&
725 (bp->b_blkno == bp->b_lblkno) &&
726 (VOP_BMAP(vp, lbn, NULL, &bp->b_blkno, &maxclen, NULL) ||
727 bp->b_blkno == -1)) {
728 bawrite(bp);
729 vp->v_clen = 0;
730 vp->v_lasta = bp->b_blkno;
731 vp->v_cstart = lbn + 1;
732 vp->v_lastw = lbn;
733 return;
734 }
735 vp->v_clen = maxclen;
736 if (!async && maxclen == 0) { /* I/O not contiguous */
737 vp->v_cstart = lbn + 1;
738 bawrite(bp);
739 } else { /* Wait for rest of cluster */
740 vp->v_cstart = lbn;
741 bdwrite(bp);
742 }
743 } else if (lbn == vp->v_cstart + vp->v_clen) {
744 /*
745 * At end of cluster, write it out if seqcount tells us we
746 * are operating sequentially, otherwise let the buf or
747 * update daemon handle it.
748 */
749 bdwrite(bp);
750 if (seqcount > 1) {
751 cluster_wbuild_wb(vp, lblocksize, vp->v_cstart,
752 vp->v_clen + 1, gbflags);
753 }
754 vp->v_clen = 0;
755 vp->v_cstart = lbn + 1;
756 } else if (vm_page_count_severe()) {
757 /*
758 * We are low on memory, get it going NOW
759 */
760 bawrite(bp);
761 } else {
762 /*
763 * In the middle of a cluster, so just delay the I/O for now.
764 */
765 bdwrite(bp);
766 }
767 vp->v_lastw = lbn;
768 vp->v_lasta = bp->b_blkno;
769}
770
771
772/*
773 * This is an awful lot like cluster_rbuild...wish they could be combined.
774 * The last lbn argument is the current block on which I/O is being
775 * performed. Check to see that it doesn't fall in the middle of
776 * the current block (if last_bp == NULL).
777 */
778int
779cluster_wbuild(struct vnode *vp, long size, daddr_t start_lbn, int len,
780 int gbflags)
781{
782 struct buf *bp, *tbp;
783 struct bufobj *bo;
784 int i, j;
785 int totalwritten = 0;
786 int dbsize = btodb(size);
787
788 if (!unmapped_buf_allowed)
789 gbflags &= ~GB_UNMAPPED;
790
791 bo = &vp->v_bufobj;
792 while (len > 0) {
793 /*
794 * If the buffer is not delayed-write (i.e. dirty), or it
795 * is delayed-write but either locked or inval, it cannot
796 * partake in the clustered write.
797 */
798 BO_LOCK(bo);
799 if ((tbp = gbincore(&vp->v_bufobj, start_lbn)) == NULL ||
800 (tbp->b_vflags & BV_BKGRDINPROG)) {
801 BO_UNLOCK(bo);
802 ++start_lbn;
803 --len;
804 continue;
805 }
806 if (BUF_LOCK(tbp,
807 LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK, BO_LOCKPTR(bo))) {
808 ++start_lbn;
809 --len;
810 continue;
811 }
812 if ((tbp->b_flags & (B_INVAL | B_DELWRI)) != B_DELWRI) {
813 BUF_UNLOCK(tbp);
814 ++start_lbn;
815 --len;
816 continue;
817 }
818 if (tbp->b_pin_count > 0) {
819 BUF_UNLOCK(tbp);
820 ++start_lbn;
821 --len;
822 continue;
823 }
824 bremfree(tbp);
825 tbp->b_flags &= ~B_DONE;
826
827 /*
828 * Extra memory in the buffer, punt on this buffer.
829 * XXX we could handle this in most cases, but we would
830 * have to push the extra memory down to after our max
831 * possible cluster size and then potentially pull it back
832 * up if the cluster was terminated prematurely--too much
833 * hassle.
834 */
835 if (((tbp->b_flags & (B_CLUSTEROK | B_MALLOC | B_VMIO)) !=
836 (B_CLUSTEROK | B_VMIO)) ||
837 (tbp->b_bcount != tbp->b_bufsize) ||
838 (tbp->b_bcount != size) ||
839 (len == 1) ||
840 ((bp = (vp->v_vflag & VV_MD) != 0 ?
841 trypbuf(&cluster_pbuf_freecnt) :
842 getpbuf(&cluster_pbuf_freecnt)) == NULL)) {
843 totalwritten += tbp->b_bufsize;
844 bawrite(tbp);
845 ++start_lbn;
846 --len;
847 continue;
848 }
849
850 /*
851 * We got a pbuf to make the cluster in.
852 * so initialise it.
853 */
854 TAILQ_INIT(&bp->b_cluster.cluster_head);
855 bp->b_bcount = 0;
856 bp->b_bufsize = 0;
857 bp->b_npages = 0;
858 if (tbp->b_wcred != NOCRED)
859 bp->b_wcred = crhold(tbp->b_wcred);
860
861 bp->b_blkno = tbp->b_blkno;
862 bp->b_lblkno = tbp->b_lblkno;
863 bp->b_offset = tbp->b_offset;
864
865 /*
866 * We are synthesizing a buffer out of vm_page_t's, but
867 * if the block size is not page aligned then the starting
868 * address may not be either. Inherit the b_data offset
869 * from the original buffer.
870 */
871 if ((gbflags & GB_UNMAPPED) == 0 ||
872 (tbp->b_flags & B_VMIO) == 0) {
873 bp->b_data = (char *)((vm_offset_t)bp->b_data |
874 ((vm_offset_t)tbp->b_data & PAGE_MASK));
875 } else {
876 bp->b_flags |= B_UNMAPPED;
877 bp->b_data = unmapped_buf;
878 }
879 bp->b_flags |= B_CLUSTER | (tbp->b_flags & (B_VMIO |
880 B_NEEDCOMMIT));
881 bp->b_iodone = cluster_callback;
882 pbgetvp(vp, bp);
883 /*
884 * From this location in the file, scan forward to see
885 * if there are buffers with adjacent data that need to
886 * be written as well.
887 */
888 for (i = 0; i < len; ++i, ++start_lbn) {
889 if (i != 0) { /* If not the first buffer */
890 /*
891 * If the adjacent data is not even in core it
892 * can't need to be written.
893 */
894 BO_LOCK(bo);
895 if ((tbp = gbincore(bo, start_lbn)) == NULL ||
896 (tbp->b_vflags & BV_BKGRDINPROG)) {
897 BO_UNLOCK(bo);
898 break;
899 }
900
901 /*
902 * If it IS in core, but has different
903 * characteristics, or is locked (which
904 * means it could be undergoing a background
905 * I/O or be in a weird state), then don't
906 * cluster with it.
907 */
908 if (BUF_LOCK(tbp,
909 LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK,
910 BO_LOCKPTR(bo)))
911 break;
912
913 if ((tbp->b_flags & (B_VMIO | B_CLUSTEROK |
914 B_INVAL | B_DELWRI | B_NEEDCOMMIT))
915 != (B_DELWRI | B_CLUSTEROK |
916 (bp->b_flags & (B_VMIO | B_NEEDCOMMIT))) ||
917 tbp->b_wcred != bp->b_wcred) {
918 BUF_UNLOCK(tbp);
919 break;
920 }
921
922 /*
923 * Check that the combined cluster
924 * would make sense with regard to pages
925 * and would not be too large
926 */
927 if ((tbp->b_bcount != size) ||
928 ((bp->b_blkno + (dbsize * i)) !=
929 tbp->b_blkno) ||
930 ((tbp->b_npages + bp->b_npages) >
931 (vp->v_mount->mnt_iosize_max / PAGE_SIZE))) {
932 BUF_UNLOCK(tbp);
933 break;
934 }
935
936 /*
937 * Do not pull in pinned buffers.
938 */
939 if (tbp->b_pin_count > 0) {
940 BUF_UNLOCK(tbp);
941 break;
942 }
943
944 /*
945 * Ok, it's passed all the tests,
946 * so remove it from the free list
947 * and mark it busy. We will use it.
948 */
949 bremfree(tbp);
950 tbp->b_flags &= ~B_DONE;
951 } /* end of code for non-first buffers only */
952 /*
953 * If the IO is via the VM then we do some
954 * special VM hackery (yuck). Since the buffer's
955 * block size may not be page-aligned it is possible
956 * for a page to be shared between two buffers. We
957 * have to get rid of the duplication when building
958 * the cluster.
959 */
960 if (tbp->b_flags & B_VMIO) {
961 vm_page_t m;
962
963 VM_OBJECT_WLOCK(tbp->b_bufobj->bo_object);
964 if (i == 0) {
965 vfs_drain_busy_pages(tbp);
966 } else { /* if not first buffer */
967 for (j = 0; j < tbp->b_npages; j += 1) {
968 m = tbp->b_pages[j];
969 if (vm_page_xbusied(m)) {
970 VM_OBJECT_WUNLOCK(
971 tbp->b_object);
972 bqrelse(tbp);
973 goto finishcluster;
974 }
975 }
976 }
977 for (j = 0; j < tbp->b_npages; j += 1) {
978 m = tbp->b_pages[j];
979 vm_page_sbusy(m);
980 vm_object_pip_add(m->object, 1);
981 if ((bp->b_npages == 0) ||
982 (bp->b_pages[bp->b_npages - 1] != m)) {
983 bp->b_pages[bp->b_npages] = m;
984 bp->b_npages++;
985 }
986 }
987 VM_OBJECT_WUNLOCK(tbp->b_bufobj->bo_object);
988 }
989 bp->b_bcount += size;
990 bp->b_bufsize += size;
991 /*
992 * If any of the clustered buffers have their
993 * B_BARRIER flag set, transfer that request to
994 * the cluster.
995 */
996 bp->b_flags |= (tbp->b_flags & B_BARRIER);
997 tbp->b_flags &= ~(B_DONE | B_BARRIER);
998 tbp->b_flags |= B_ASYNC;
999 tbp->b_ioflags &= ~BIO_ERROR;
1000 tbp->b_iocmd = BIO_WRITE;
1001 bundirty(tbp);
1002 reassignbuf(tbp); /* put on clean list */
1003 bufobj_wref(tbp->b_bufobj);
1004 BUF_KERNPROC(tbp);
1005 TAILQ_INSERT_TAIL(&bp->b_cluster.cluster_head,
1006 tbp, b_cluster.cluster_entry);
1007 }
1008 finishcluster:
1009 if ((bp->b_flags & B_UNMAPPED) == 0) {
1010 pmap_qenter(trunc_page((vm_offset_t) bp->b_data),
1011 (vm_page_t *)bp->b_pages, bp->b_npages);
1012 }
1013 if (bp->b_bufsize > bp->b_kvasize)
1014 panic(
1015 "cluster_wbuild: b_bufsize(%ld) > b_kvasize(%d)\n",
1016 bp->b_bufsize, bp->b_kvasize);
1017 bp->b_kvasize = bp->b_bufsize;
1018 totalwritten += bp->b_bufsize;
1019 bp->b_dirtyoff = 0;
1020 bp->b_dirtyend = bp->b_bufsize;
1021 bawrite(bp);
1022
1023 len -= i;
1024 }
1025 return totalwritten;
1026}
1027
1028/*
1029 * Collect together all the buffers in a cluster.
1030 * Plus add one additional buffer.
1031 */
1032static struct cluster_save *
1033cluster_collectbufs(struct vnode *vp, struct buf *last_bp, int gbflags)
1034{
1035 struct cluster_save *buflist;
1036 struct buf *bp;
1037 daddr_t lbn;
1038 int i, len;
1039
1040 len = vp->v_lastw - vp->v_cstart + 1;
1041 buflist = malloc(sizeof(struct buf *) * (len + 1) + sizeof(*buflist),
1042 M_SEGMENT, M_WAITOK);
1043 buflist->bs_nchildren = 0;
1044 buflist->bs_children = (struct buf **) (buflist + 1);
1045 for (lbn = vp->v_cstart, i = 0; i < len; lbn++, i++) {
1046 (void)bread_gb(vp, lbn, last_bp->b_bcount, NOCRED,
1047 gbflags, &bp);
1048 buflist->bs_children[i] = bp;
1049 if (bp->b_blkno == bp->b_lblkno)
1050 VOP_BMAP(vp, bp->b_lblkno, NULL, &bp->b_blkno,
1051 NULL, NULL);
1052 }
1053 buflist->bs_children[i] = bp = last_bp;
1054 if (bp->b_blkno == bp->b_lblkno)
1055 VOP_BMAP(vp, bp->b_lblkno, NULL, &bp->b_blkno, NULL, NULL);
1056 buflist->bs_nchildren = i + 1;
1057 return (buflist);
1058}
378 for (bn = blkno, i = 0; i < run; ++i, bn += inc) {
379 if (i == 0) {
380 VM_OBJECT_WLOCK(tbp->b_bufobj->bo_object);
381 vfs_drain_busy_pages(tbp);
382 vm_object_pip_add(tbp->b_bufobj->bo_object,
383 tbp->b_npages);
384 for (k = 0; k < tbp->b_npages; k++)
385 vm_page_sbusy(tbp->b_pages[k]);
386 VM_OBJECT_WUNLOCK(tbp->b_bufobj->bo_object);
387 } else {
388 if ((bp->b_npages * PAGE_SIZE) +
389 round_page(size) > vp->v_mount->mnt_iosize_max) {
390 break;
391 }
392
393 tbp = getblk(vp, lbn + i, size, 0, 0, GB_LOCK_NOWAIT |
394 (gbflags & GB_UNMAPPED));
395
396 /* Don't wait around for locked bufs. */
397 if (tbp == NULL)
398 break;
399
400 /*
401 * Stop scanning if the buffer is fully valid
402 * (marked B_CACHE), or locked (may be doing a
403 * background write), or if the buffer is not
404 * VMIO backed. The clustering code can only deal
405 * with VMIO-backed buffers. The bo lock is not
406 * required for the BKGRDINPROG check since it
407 * can not be set without the buf lock.
408 */
409 if ((tbp->b_vflags & BV_BKGRDINPROG) ||
410 (tbp->b_flags & B_CACHE) ||
411 (tbp->b_flags & B_VMIO) == 0) {
412 bqrelse(tbp);
413 break;
414 }
415
416 /*
417 * The buffer must be completely invalid in order to
418 * take part in the cluster. If it is partially valid
419 * then we stop.
420 */
421 off = tbp->b_offset;
422 tsize = size;
423 VM_OBJECT_WLOCK(tbp->b_bufobj->bo_object);
424 for (j = 0; tsize > 0; j++) {
425 toff = off & PAGE_MASK;
426 tinc = tsize;
427 if (toff + tinc > PAGE_SIZE)
428 tinc = PAGE_SIZE - toff;
429 VM_OBJECT_ASSERT_WLOCKED(tbp->b_pages[j]->object);
430 if ((tbp->b_pages[j]->valid &
431 vm_page_bits(toff, tinc)) != 0)
432 break;
433 if (vm_page_xbusied(tbp->b_pages[j]))
434 break;
435 vm_object_pip_add(tbp->b_bufobj->bo_object, 1);
436 vm_page_sbusy(tbp->b_pages[j]);
437 off += tinc;
438 tsize -= tinc;
439 }
440 if (tsize > 0) {
441clean_sbusy:
442 vm_object_pip_add(tbp->b_bufobj->bo_object, -j);
443 for (k = 0; k < j; k++)
444 vm_page_sunbusy(tbp->b_pages[k]);
445 VM_OBJECT_WUNLOCK(tbp->b_bufobj->bo_object);
446 bqrelse(tbp);
447 break;
448 }
449 VM_OBJECT_WUNLOCK(tbp->b_bufobj->bo_object);
450
451 /*
452 * Set a read-ahead mark as appropriate
453 */
454 if ((fbp && (i == 1)) || (i == (run - 1)))
455 tbp->b_flags |= B_RAM;
456
457 /*
458 * Set the buffer up for an async read (XXX should
459 * we do this only if we do not wind up brelse()ing?).
460 * Set the block number if it isn't set, otherwise
461 * if it is make sure it matches the block number we
462 * expect.
463 */
464 tbp->b_flags |= B_ASYNC;
465 tbp->b_iocmd = BIO_READ;
466 if (tbp->b_blkno == tbp->b_lblkno) {
467 tbp->b_blkno = bn;
468 } else if (tbp->b_blkno != bn) {
469 VM_OBJECT_WLOCK(tbp->b_bufobj->bo_object);
470 goto clean_sbusy;
471 }
472 }
473 /*
474 * XXX fbp from caller may not be B_ASYNC, but we are going
475 * to biodone() it in cluster_callback() anyway
476 */
477 BUF_KERNPROC(tbp);
478 TAILQ_INSERT_TAIL(&bp->b_cluster.cluster_head,
479 tbp, b_cluster.cluster_entry);
480 VM_OBJECT_WLOCK(tbp->b_bufobj->bo_object);
481 for (j = 0; j < tbp->b_npages; j += 1) {
482 vm_page_t m;
483 m = tbp->b_pages[j];
484 if ((bp->b_npages == 0) ||
485 (bp->b_pages[bp->b_npages-1] != m)) {
486 bp->b_pages[bp->b_npages] = m;
487 bp->b_npages++;
488 }
489 if (m->valid == VM_PAGE_BITS_ALL)
490 tbp->b_pages[j] = bogus_page;
491 }
492 VM_OBJECT_WUNLOCK(tbp->b_bufobj->bo_object);
493 /*
494 * Don't inherit tbp->b_bufsize as it may be larger due to
495 * a non-page-aligned size. Instead just aggregate using
496 * 'size'.
497 */
498 if (tbp->b_bcount != size)
499 printf("warning: tbp->b_bcount wrong %ld vs %ld\n", tbp->b_bcount, size);
500 if (tbp->b_bufsize != size)
501 printf("warning: tbp->b_bufsize wrong %ld vs %ld\n", tbp->b_bufsize, size);
502 bp->b_bcount += size;
503 bp->b_bufsize += size;
504 }
505
506 /*
507 * Fully valid pages in the cluster are already good and do not need
508 * to be re-read from disk. Replace the page with bogus_page
509 */
510 VM_OBJECT_WLOCK(bp->b_bufobj->bo_object);
511 for (j = 0; j < bp->b_npages; j++) {
512 VM_OBJECT_ASSERT_WLOCKED(bp->b_pages[j]->object);
513 if (bp->b_pages[j]->valid == VM_PAGE_BITS_ALL)
514 bp->b_pages[j] = bogus_page;
515 }
516 VM_OBJECT_WUNLOCK(bp->b_bufobj->bo_object);
517 if (bp->b_bufsize > bp->b_kvasize)
518 panic("cluster_rbuild: b_bufsize(%ld) > b_kvasize(%d)\n",
519 bp->b_bufsize, bp->b_kvasize);
520 bp->b_kvasize = bp->b_bufsize;
521
522 if ((bp->b_flags & B_UNMAPPED) == 0) {
523 pmap_qenter(trunc_page((vm_offset_t) bp->b_data),
524 (vm_page_t *)bp->b_pages, bp->b_npages);
525 }
526 return (bp);
527}
528
529/*
530 * Cleanup after a clustered read or write.
531 * This is complicated by the fact that any of the buffers might have
532 * extra memory (if there were no empty buffer headers at allocbuf time)
533 * that we will need to shift around.
534 */
535static void
536cluster_callback(bp)
537 struct buf *bp;
538{
539 struct buf *nbp, *tbp;
540 int error = 0;
541
542 /*
543 * Must propogate errors to all the components.
544 */
545 if (bp->b_ioflags & BIO_ERROR)
546 error = bp->b_error;
547
548 if ((bp->b_flags & B_UNMAPPED) == 0) {
549 pmap_qremove(trunc_page((vm_offset_t) bp->b_data),
550 bp->b_npages);
551 }
552 /*
553 * Move memory from the large cluster buffer into the component
554 * buffers and mark IO as done on these.
555 */
556 for (tbp = TAILQ_FIRST(&bp->b_cluster.cluster_head);
557 tbp; tbp = nbp) {
558 nbp = TAILQ_NEXT(&tbp->b_cluster, cluster_entry);
559 if (error) {
560 tbp->b_ioflags |= BIO_ERROR;
561 tbp->b_error = error;
562 } else {
563 tbp->b_dirtyoff = tbp->b_dirtyend = 0;
564 tbp->b_flags &= ~B_INVAL;
565 tbp->b_ioflags &= ~BIO_ERROR;
566 /*
567 * XXX the bdwrite()/bqrelse() issued during
568 * cluster building clears B_RELBUF (see bqrelse()
569 * comment). If direct I/O was specified, we have
570 * to restore it here to allow the buffer and VM
571 * to be freed.
572 */
573 if (tbp->b_flags & B_DIRECT)
574 tbp->b_flags |= B_RELBUF;
575 }
576 bufdone(tbp);
577 }
578 pbrelvp(bp);
579 relpbuf(bp, &cluster_pbuf_freecnt);
580}
581
582/*
583 * cluster_wbuild_wb:
584 *
585 * Implement modified write build for cluster.
586 *
587 * write_behind = 0 write behind disabled
588 * write_behind = 1 write behind normal (default)
589 * write_behind = 2 write behind backed-off
590 */
591
592static __inline int
593cluster_wbuild_wb(struct vnode *vp, long size, daddr_t start_lbn, int len,
594 int gbflags)
595{
596 int r = 0;
597
598 switch (write_behind) {
599 case 2:
600 if (start_lbn < len)
601 break;
602 start_lbn -= len;
603 /* FALLTHROUGH */
604 case 1:
605 r = cluster_wbuild(vp, size, start_lbn, len, gbflags);
606 /* FALLTHROUGH */
607 default:
608 /* FALLTHROUGH */
609 break;
610 }
611 return(r);
612}
613
614/*
615 * Do clustered write for FFS.
616 *
617 * Three cases:
618 * 1. Write is not sequential (write asynchronously)
619 * Write is sequential:
620 * 2. beginning of cluster - begin cluster
621 * 3. middle of a cluster - add to cluster
622 * 4. end of a cluster - asynchronously write cluster
623 */
624void
625cluster_write(struct vnode *vp, struct buf *bp, u_quad_t filesize, int seqcount,
626 int gbflags)
627{
628 daddr_t lbn;
629 int maxclen, cursize;
630 int lblocksize;
631 int async;
632
633 if (!unmapped_buf_allowed)
634 gbflags &= ~GB_UNMAPPED;
635
636 if (vp->v_type == VREG) {
637 async = DOINGASYNC(vp);
638 lblocksize = vp->v_mount->mnt_stat.f_iosize;
639 } else {
640 async = 0;
641 lblocksize = bp->b_bufsize;
642 }
643 lbn = bp->b_lblkno;
644 KASSERT(bp->b_offset != NOOFFSET, ("cluster_write: no buffer offset"));
645
646 /* Initialize vnode to beginning of file. */
647 if (lbn == 0)
648 vp->v_lasta = vp->v_clen = vp->v_cstart = vp->v_lastw = 0;
649
650 if (vp->v_clen == 0 || lbn != vp->v_lastw + 1 ||
651 (bp->b_blkno != vp->v_lasta + btodb(lblocksize))) {
652 maxclen = vp->v_mount->mnt_iosize_max / lblocksize - 1;
653 if (vp->v_clen != 0) {
654 /*
655 * Next block is not sequential.
656 *
657 * If we are not writing at end of file, the process
658 * seeked to another point in the file since its last
659 * write, or we have reached our maximum cluster size,
660 * then push the previous cluster. Otherwise try
661 * reallocating to make it sequential.
662 *
663 * Change to algorithm: only push previous cluster if
664 * it was sequential from the point of view of the
665 * seqcount heuristic, otherwise leave the buffer
666 * intact so we can potentially optimize the I/O
667 * later on in the buf_daemon or update daemon
668 * flush.
669 */
670 cursize = vp->v_lastw - vp->v_cstart + 1;
671 if (((u_quad_t) bp->b_offset + lblocksize) != filesize ||
672 lbn != vp->v_lastw + 1 || vp->v_clen <= cursize) {
673 if (!async && seqcount > 0) {
674 cluster_wbuild_wb(vp, lblocksize,
675 vp->v_cstart, cursize, gbflags);
676 }
677 } else {
678 struct buf **bpp, **endbp;
679 struct cluster_save *buflist;
680
681 buflist = cluster_collectbufs(vp, bp, gbflags);
682 endbp = &buflist->bs_children
683 [buflist->bs_nchildren - 1];
684 if (VOP_REALLOCBLKS(vp, buflist)) {
685 /*
686 * Failed, push the previous cluster
687 * if *really* writing sequentially
688 * in the logical file (seqcount > 1),
689 * otherwise delay it in the hopes that
690 * the low level disk driver can
691 * optimize the write ordering.
692 */
693 for (bpp = buflist->bs_children;
694 bpp < endbp; bpp++)
695 brelse(*bpp);
696 free(buflist, M_SEGMENT);
697 if (seqcount > 1) {
698 cluster_wbuild_wb(vp,
699 lblocksize, vp->v_cstart,
700 cursize, gbflags);
701 }
702 } else {
703 /*
704 * Succeeded, keep building cluster.
705 */
706 for (bpp = buflist->bs_children;
707 bpp <= endbp; bpp++)
708 bdwrite(*bpp);
709 free(buflist, M_SEGMENT);
710 vp->v_lastw = lbn;
711 vp->v_lasta = bp->b_blkno;
712 return;
713 }
714 }
715 }
716 /*
717 * Consider beginning a cluster. If at end of file, make
718 * cluster as large as possible, otherwise find size of
719 * existing cluster.
720 */
721 if ((vp->v_type == VREG) &&
722 ((u_quad_t) bp->b_offset + lblocksize) != filesize &&
723 (bp->b_blkno == bp->b_lblkno) &&
724 (VOP_BMAP(vp, lbn, NULL, &bp->b_blkno, &maxclen, NULL) ||
725 bp->b_blkno == -1)) {
726 bawrite(bp);
727 vp->v_clen = 0;
728 vp->v_lasta = bp->b_blkno;
729 vp->v_cstart = lbn + 1;
730 vp->v_lastw = lbn;
731 return;
732 }
733 vp->v_clen = maxclen;
734 if (!async && maxclen == 0) { /* I/O not contiguous */
735 vp->v_cstart = lbn + 1;
736 bawrite(bp);
737 } else { /* Wait for rest of cluster */
738 vp->v_cstart = lbn;
739 bdwrite(bp);
740 }
741 } else if (lbn == vp->v_cstart + vp->v_clen) {
742 /*
743 * At end of cluster, write it out if seqcount tells us we
744 * are operating sequentially, otherwise let the buf or
745 * update daemon handle it.
746 */
747 bdwrite(bp);
748 if (seqcount > 1) {
749 cluster_wbuild_wb(vp, lblocksize, vp->v_cstart,
750 vp->v_clen + 1, gbflags);
751 }
752 vp->v_clen = 0;
753 vp->v_cstart = lbn + 1;
754 } else if (vm_page_count_severe()) {
755 /*
756 * We are low on memory, get it going NOW
757 */
758 bawrite(bp);
759 } else {
760 /*
761 * In the middle of a cluster, so just delay the I/O for now.
762 */
763 bdwrite(bp);
764 }
765 vp->v_lastw = lbn;
766 vp->v_lasta = bp->b_blkno;
767}
768
769
770/*
771 * This is an awful lot like cluster_rbuild...wish they could be combined.
772 * The last lbn argument is the current block on which I/O is being
773 * performed. Check to see that it doesn't fall in the middle of
774 * the current block (if last_bp == NULL).
775 */
776int
777cluster_wbuild(struct vnode *vp, long size, daddr_t start_lbn, int len,
778 int gbflags)
779{
780 struct buf *bp, *tbp;
781 struct bufobj *bo;
782 int i, j;
783 int totalwritten = 0;
784 int dbsize = btodb(size);
785
786 if (!unmapped_buf_allowed)
787 gbflags &= ~GB_UNMAPPED;
788
789 bo = &vp->v_bufobj;
790 while (len > 0) {
791 /*
792 * If the buffer is not delayed-write (i.e. dirty), or it
793 * is delayed-write but either locked or inval, it cannot
794 * partake in the clustered write.
795 */
796 BO_LOCK(bo);
797 if ((tbp = gbincore(&vp->v_bufobj, start_lbn)) == NULL ||
798 (tbp->b_vflags & BV_BKGRDINPROG)) {
799 BO_UNLOCK(bo);
800 ++start_lbn;
801 --len;
802 continue;
803 }
804 if (BUF_LOCK(tbp,
805 LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK, BO_LOCKPTR(bo))) {
806 ++start_lbn;
807 --len;
808 continue;
809 }
810 if ((tbp->b_flags & (B_INVAL | B_DELWRI)) != B_DELWRI) {
811 BUF_UNLOCK(tbp);
812 ++start_lbn;
813 --len;
814 continue;
815 }
816 if (tbp->b_pin_count > 0) {
817 BUF_UNLOCK(tbp);
818 ++start_lbn;
819 --len;
820 continue;
821 }
822 bremfree(tbp);
823 tbp->b_flags &= ~B_DONE;
824
825 /*
826 * Extra memory in the buffer, punt on this buffer.
827 * XXX we could handle this in most cases, but we would
828 * have to push the extra memory down to after our max
829 * possible cluster size and then potentially pull it back
830 * up if the cluster was terminated prematurely--too much
831 * hassle.
832 */
833 if (((tbp->b_flags & (B_CLUSTEROK | B_MALLOC | B_VMIO)) !=
834 (B_CLUSTEROK | B_VMIO)) ||
835 (tbp->b_bcount != tbp->b_bufsize) ||
836 (tbp->b_bcount != size) ||
837 (len == 1) ||
838 ((bp = (vp->v_vflag & VV_MD) != 0 ?
839 trypbuf(&cluster_pbuf_freecnt) :
840 getpbuf(&cluster_pbuf_freecnt)) == NULL)) {
841 totalwritten += tbp->b_bufsize;
842 bawrite(tbp);
843 ++start_lbn;
844 --len;
845 continue;
846 }
847
848 /*
849 * We got a pbuf to make the cluster in.
850 * so initialise it.
851 */
852 TAILQ_INIT(&bp->b_cluster.cluster_head);
853 bp->b_bcount = 0;
854 bp->b_bufsize = 0;
855 bp->b_npages = 0;
856 if (tbp->b_wcred != NOCRED)
857 bp->b_wcred = crhold(tbp->b_wcred);
858
859 bp->b_blkno = tbp->b_blkno;
860 bp->b_lblkno = tbp->b_lblkno;
861 bp->b_offset = tbp->b_offset;
862
863 /*
864 * We are synthesizing a buffer out of vm_page_t's, but
865 * if the block size is not page aligned then the starting
866 * address may not be either. Inherit the b_data offset
867 * from the original buffer.
868 */
869 if ((gbflags & GB_UNMAPPED) == 0 ||
870 (tbp->b_flags & B_VMIO) == 0) {
871 bp->b_data = (char *)((vm_offset_t)bp->b_data |
872 ((vm_offset_t)tbp->b_data & PAGE_MASK));
873 } else {
874 bp->b_flags |= B_UNMAPPED;
875 bp->b_data = unmapped_buf;
876 }
877 bp->b_flags |= B_CLUSTER | (tbp->b_flags & (B_VMIO |
878 B_NEEDCOMMIT));
879 bp->b_iodone = cluster_callback;
880 pbgetvp(vp, bp);
881 /*
882 * From this location in the file, scan forward to see
883 * if there are buffers with adjacent data that need to
884 * be written as well.
885 */
886 for (i = 0; i < len; ++i, ++start_lbn) {
887 if (i != 0) { /* If not the first buffer */
888 /*
889 * If the adjacent data is not even in core it
890 * can't need to be written.
891 */
892 BO_LOCK(bo);
893 if ((tbp = gbincore(bo, start_lbn)) == NULL ||
894 (tbp->b_vflags & BV_BKGRDINPROG)) {
895 BO_UNLOCK(bo);
896 break;
897 }
898
899 /*
900 * If it IS in core, but has different
901 * characteristics, or is locked (which
902 * means it could be undergoing a background
903 * I/O or be in a weird state), then don't
904 * cluster with it.
905 */
906 if (BUF_LOCK(tbp,
907 LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK,
908 BO_LOCKPTR(bo)))
909 break;
910
911 if ((tbp->b_flags & (B_VMIO | B_CLUSTEROK |
912 B_INVAL | B_DELWRI | B_NEEDCOMMIT))
913 != (B_DELWRI | B_CLUSTEROK |
914 (bp->b_flags & (B_VMIO | B_NEEDCOMMIT))) ||
915 tbp->b_wcred != bp->b_wcred) {
916 BUF_UNLOCK(tbp);
917 break;
918 }
919
920 /*
921 * Check that the combined cluster
922 * would make sense with regard to pages
923 * and would not be too large
924 */
925 if ((tbp->b_bcount != size) ||
926 ((bp->b_blkno + (dbsize * i)) !=
927 tbp->b_blkno) ||
928 ((tbp->b_npages + bp->b_npages) >
929 (vp->v_mount->mnt_iosize_max / PAGE_SIZE))) {
930 BUF_UNLOCK(tbp);
931 break;
932 }
933
934 /*
935 * Do not pull in pinned buffers.
936 */
937 if (tbp->b_pin_count > 0) {
938 BUF_UNLOCK(tbp);
939 break;
940 }
941
942 /*
943 * Ok, it's passed all the tests,
944 * so remove it from the free list
945 * and mark it busy. We will use it.
946 */
947 bremfree(tbp);
948 tbp->b_flags &= ~B_DONE;
949 } /* end of code for non-first buffers only */
950 /*
951 * If the IO is via the VM then we do some
952 * special VM hackery (yuck). Since the buffer's
953 * block size may not be page-aligned it is possible
954 * for a page to be shared between two buffers. We
955 * have to get rid of the duplication when building
956 * the cluster.
957 */
958 if (tbp->b_flags & B_VMIO) {
959 vm_page_t m;
960
961 VM_OBJECT_WLOCK(tbp->b_bufobj->bo_object);
962 if (i == 0) {
963 vfs_drain_busy_pages(tbp);
964 } else { /* if not first buffer */
965 for (j = 0; j < tbp->b_npages; j += 1) {
966 m = tbp->b_pages[j];
967 if (vm_page_xbusied(m)) {
968 VM_OBJECT_WUNLOCK(
969 tbp->b_object);
970 bqrelse(tbp);
971 goto finishcluster;
972 }
973 }
974 }
975 for (j = 0; j < tbp->b_npages; j += 1) {
976 m = tbp->b_pages[j];
977 vm_page_sbusy(m);
978 vm_object_pip_add(m->object, 1);
979 if ((bp->b_npages == 0) ||
980 (bp->b_pages[bp->b_npages - 1] != m)) {
981 bp->b_pages[bp->b_npages] = m;
982 bp->b_npages++;
983 }
984 }
985 VM_OBJECT_WUNLOCK(tbp->b_bufobj->bo_object);
986 }
987 bp->b_bcount += size;
988 bp->b_bufsize += size;
989 /*
990 * If any of the clustered buffers have their
991 * B_BARRIER flag set, transfer that request to
992 * the cluster.
993 */
994 bp->b_flags |= (tbp->b_flags & B_BARRIER);
995 tbp->b_flags &= ~(B_DONE | B_BARRIER);
996 tbp->b_flags |= B_ASYNC;
997 tbp->b_ioflags &= ~BIO_ERROR;
998 tbp->b_iocmd = BIO_WRITE;
999 bundirty(tbp);
1000 reassignbuf(tbp); /* put on clean list */
1001 bufobj_wref(tbp->b_bufobj);
1002 BUF_KERNPROC(tbp);
1003 TAILQ_INSERT_TAIL(&bp->b_cluster.cluster_head,
1004 tbp, b_cluster.cluster_entry);
1005 }
1006 finishcluster:
1007 if ((bp->b_flags & B_UNMAPPED) == 0) {
1008 pmap_qenter(trunc_page((vm_offset_t) bp->b_data),
1009 (vm_page_t *)bp->b_pages, bp->b_npages);
1010 }
1011 if (bp->b_bufsize > bp->b_kvasize)
1012 panic(
1013 "cluster_wbuild: b_bufsize(%ld) > b_kvasize(%d)\n",
1014 bp->b_bufsize, bp->b_kvasize);
1015 bp->b_kvasize = bp->b_bufsize;
1016 totalwritten += bp->b_bufsize;
1017 bp->b_dirtyoff = 0;
1018 bp->b_dirtyend = bp->b_bufsize;
1019 bawrite(bp);
1020
1021 len -= i;
1022 }
1023 return totalwritten;
1024}
1025
1026/*
1027 * Collect together all the buffers in a cluster.
1028 * Plus add one additional buffer.
1029 */
1030static struct cluster_save *
1031cluster_collectbufs(struct vnode *vp, struct buf *last_bp, int gbflags)
1032{
1033 struct cluster_save *buflist;
1034 struct buf *bp;
1035 daddr_t lbn;
1036 int i, len;
1037
1038 len = vp->v_lastw - vp->v_cstart + 1;
1039 buflist = malloc(sizeof(struct buf *) * (len + 1) + sizeof(*buflist),
1040 M_SEGMENT, M_WAITOK);
1041 buflist->bs_nchildren = 0;
1042 buflist->bs_children = (struct buf **) (buflist + 1);
1043 for (lbn = vp->v_cstart, i = 0; i < len; lbn++, i++) {
1044 (void)bread_gb(vp, lbn, last_bp->b_bcount, NOCRED,
1045 gbflags, &bp);
1046 buflist->bs_children[i] = bp;
1047 if (bp->b_blkno == bp->b_lblkno)
1048 VOP_BMAP(vp, bp->b_lblkno, NULL, &bp->b_blkno,
1049 NULL, NULL);
1050 }
1051 buflist->bs_children[i] = bp = last_bp;
1052 if (bp->b_blkno == bp->b_lblkno)
1053 VOP_BMAP(vp, bp->b_lblkno, NULL, &bp->b_blkno, NULL, NULL);
1054 buflist->bs_nchildren = i + 1;
1055 return (buflist);
1056}