]> git.saurik.com Git - apple/xnu.git/blame - bsd/vfs/vfs_cluster.c
xnu-1456.1.26.tar.gz
[apple/xnu.git] / bsd / vfs / vfs_cluster.c
CommitLineData
1c79356b 1/*
b0d623f7 2 * Copyright (c) 2000-2008 Apple Inc. All rights reserved.
5d5c5d0d 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 5 *
2d21ac55
A
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
8f6c56a5 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
8f6c56a5 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
27 */
28/* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
29/*
30 * Copyright (c) 1993
31 * The Regents of the University of California. All rights reserved.
32 *
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
35 * are met:
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * 3. All advertising materials mentioning features or use of this software
42 * must display the following acknowledgement:
43 * This product includes software developed by the University of
44 * California, Berkeley and its contributors.
45 * 4. Neither the name of the University nor the names of its contributors
46 * may be used to endorse or promote products derived from this software
47 * without specific prior written permission.
48 *
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 * SUCH DAMAGE.
60 *
61 * @(#)vfs_cluster.c 8.10 (Berkeley) 3/28/95
62 */
63
64#include <sys/param.h>
91447636
A
65#include <sys/proc_internal.h>
66#include <sys/buf_internal.h>
67#include <sys/mount_internal.h>
68#include <sys/vnode_internal.h>
1c79356b
A
69#include <sys/trace.h>
70#include <sys/malloc.h>
55e303ae
A
71#include <sys/time.h>
72#include <sys/kernel.h>
1c79356b 73#include <sys/resourcevar.h>
91447636 74#include <sys/uio_internal.h>
1c79356b 75#include <libkern/libkern.h>
55e303ae 76#include <machine/machine_routines.h>
1c79356b 77
91447636 78#include <sys/ubc_internal.h>
2d21ac55 79#include <vm/vnode_pager.h>
1c79356b 80
55e303ae
A
81#include <mach/mach_types.h>
82#include <mach/memory_object_types.h>
91447636
A
83#include <mach/vm_map.h>
84#include <mach/upl.h>
85
86#include <vm/vm_kern.h>
87#include <vm/vm_map.h>
88#include <vm/vm_pageout.h>
55e303ae 89
1c79356b 90#include <sys/kdebug.h>
b0d623f7
A
91#include <libkern/OSAtomic.h>
92
93#if 0
94#undef KERNEL_DEBUG
95#define KERNEL_DEBUG KERNEL_DEBUG_CONSTANT
96#endif
97
1c79356b 98
2d21ac55 99#define CL_READ 0x01
b0d623f7 100#define CL_WRITE 0x02
cf7d32b8
A
101#define CL_ASYNC 0x04
102#define CL_COMMIT 0x08
2d21ac55
A
103#define CL_PAGEOUT 0x10
104#define CL_AGE 0x20
105#define CL_NOZERO 0x40
106#define CL_PAGEIN 0x80
107#define CL_DEV_MEMORY 0x100
108#define CL_PRESERVE 0x200
109#define CL_THROTTLE 0x400
110#define CL_KEEPCACHED 0x800
111#define CL_DIRECT_IO 0x1000
112#define CL_PASSIVE 0x2000
b0d623f7
A
113#define CL_IOSTREAMING 0x4000
114
115#define MAX_VECTOR_UPL_ELEMENTS 8
116#define MAX_VECTOR_UPL_SIZE (2 * MAX_UPL_SIZE) * PAGE_SIZE
b4c24cb9 117
b0d623f7
A
118extern upl_t vector_upl_create(vm_offset_t);
119extern boolean_t vector_upl_is_valid(upl_t);
120extern boolean_t vector_upl_set_subupl(upl_t,upl_t, u_int32_t);
121extern void vector_upl_set_pagelist(upl_t);
122extern void vector_upl_set_iostate(upl_t, upl_t, vm_offset_t, u_int32_t);
d7e50217 123
b4c24cb9 124struct clios {
d7e50217
A
125 u_int io_completed; /* amount of io that has currently completed */
126 u_int io_issued; /* amount of io that was successfully issued */
127 int io_error; /* error code of first error encountered */
128 int io_wanted; /* someone is sleeping waiting for a change in state */
b4c24cb9
A
129};
130
91447636
A
131static lck_grp_t *cl_mtx_grp;
132static lck_attr_t *cl_mtx_attr;
133static lck_grp_attr_t *cl_mtx_grp_attr;
134static lck_mtx_t *cl_mtxp;
135
136
2d21ac55
A
137#define IO_UNKNOWN 0
138#define IO_DIRECT 1
139#define IO_CONTIG 2
140#define IO_COPY 3
141
142#define PUSH_DELAY 0x01
143#define PUSH_ALL 0x02
144#define PUSH_SYNC 0x04
145
146
147static void cluster_EOT(buf_t cbp_head, buf_t cbp_tail, int zero_offset);
148static void cluster_wait_IO(buf_t cbp_head, int async);
149static void cluster_complete_transaction(buf_t *cbp_head, void *callback_arg, int *retval, int flags, int needwait);
150
151static int cluster_io_type(struct uio *uio, int *io_type, u_int32_t *io_length, u_int32_t min_length);
152
91447636 153static int cluster_io(vnode_t vp, upl_t upl, vm_offset_t upl_offset, off_t f_offset, int non_rounded_size,
2d21ac55
A
154 int flags, buf_t real_bp, struct clios *iostate, int (*)(buf_t, void *), void *callback_arg);
155static int cluster_iodone(buf_t bp, void *callback_arg);
156static int cluster_ioerror(upl_t upl, int upl_offset, int abort_size, int error, int io_flags);
b0d623f7 157static int cluster_hard_throttle_on(vnode_t vp, uint32_t);
91447636 158
2d21ac55
A
159static void cluster_syncup(vnode_t vp, off_t newEOF, int (*)(buf_t, void *), void *callback_arg);
160
b0d623f7 161static void cluster_read_upl_release(upl_t upl, int start_pg, int last_pg, int take_reference);
2d21ac55
A
162static int cluster_copy_ubc_data_internal(vnode_t vp, struct uio *uio, int *io_resid, int mark_dirty, int take_reference);
163
164static int cluster_read_copy(vnode_t vp, struct uio *uio, u_int32_t io_req_size, off_t filesize, int flags,
165 int (*)(buf_t, void *), void *callback_arg);
166static int cluster_read_direct(vnode_t vp, struct uio *uio, off_t filesize, int *read_type, u_int32_t *read_length,
167 int flags, int (*)(buf_t, void *), void *callback_arg);
168static int cluster_read_contig(vnode_t vp, struct uio *uio, off_t filesize, int *read_type, u_int32_t *read_length,
169 int (*)(buf_t, void *), void *callback_arg, int flags);
1c79356b 170
2d21ac55
A
171static int cluster_write_copy(vnode_t vp, struct uio *uio, u_int32_t io_req_size, off_t oldEOF, off_t newEOF,
172 off_t headOff, off_t tailOff, int flags, int (*)(buf_t, void *), void *callback_arg);
173static int cluster_write_direct(vnode_t vp, struct uio *uio, off_t oldEOF, off_t newEOF,
174 int *write_type, u_int32_t *write_length, int flags, int (*)(buf_t, void *), void *callback_arg);
175static int cluster_write_contig(vnode_t vp, struct uio *uio, off_t newEOF,
176 int *write_type, u_int32_t *write_length, int (*)(buf_t, void *), void *callback_arg, int bflag);
91447636 177
2d21ac55 178static int cluster_align_phys_io(vnode_t vp, struct uio *uio, addr64_t usr_paddr, u_int32_t xsize, int flags, int (*)(buf_t, void *), void *callback_arg);
91447636 179
2d21ac55
A
180static int cluster_read_prefetch(vnode_t vp, off_t f_offset, u_int size, off_t filesize, int (*callback)(buf_t, void *), void *callback_arg, int bflag);
181static void cluster_read_ahead(vnode_t vp, struct cl_extent *extent, off_t filesize, struct cl_readahead *ra, int (*callback)(buf_t, void *), void *callback_arg, int bflag);
91447636 182
2d21ac55 183static int cluster_push_now(vnode_t vp, struct cl_extent *, off_t EOF, int flags, int (*)(buf_t, void *), void *callback_arg);
55e303ae 184
2d21ac55
A
185static int cluster_try_push(struct cl_writebehind *, vnode_t vp, off_t EOF, int push_flag, int (*)(buf_t, void *), void *callback_arg);
186
187static void sparse_cluster_switch(struct cl_writebehind *, vnode_t vp, off_t EOF, int (*)(buf_t, void *), void *callback_arg);
b0d623f7
A
188static void sparse_cluster_push(void **cmapp, vnode_t vp, off_t EOF, int push_flag, int (*)(buf_t, void *), void *callback_arg);
189static void sparse_cluster_add(void **cmapp, vnode_t vp, struct cl_extent *, off_t EOF, int (*)(buf_t, void *), void *callback_arg);
2d21ac55
A
190
191static kern_return_t vfs_drt_mark_pages(void **cmapp, off_t offset, u_int length, u_int *setcountp);
55e303ae
A
192static kern_return_t vfs_drt_get_cluster(void **cmapp, off_t *offsetp, u_int *lengthp);
193static kern_return_t vfs_drt_control(void **cmapp, int op_type);
194
9bccf70c 195
2d21ac55
A
196/*
197 * limit the internal I/O size so that we
198 * can represent it in a 32 bit int
199 */
b0d623f7
A
200#define MAX_IO_REQUEST_SIZE (1024 * 1024 * 512)
201#define MAX_IO_CONTIG_SIZE (MAX_UPL_SIZE * PAGE_SIZE)
202#define MAX_VECTS 16
2d21ac55
A
203#define MIN_DIRECT_WRITE_SIZE (4 * PAGE_SIZE)
204
b0d623f7
A
205#define IO_SCALE(vp, base) (vp->v_mount->mnt_ioscale * base)
206#define MAX_CLUSTER_SIZE(vp) (cluster_max_io_size(vp->v_mount, CL_WRITE))
207#define MAX_PREFETCH(vp, io_size) (io_size * IO_SCALE(vp, 3))
cf7d32b8
A
208
209
2d21ac55
A
210int speculative_reads_disabled = 0;
211
1c79356b
A
212/*
213 * throttle the number of async writes that
214 * can be outstanding on a single vnode
215 * before we issue a synchronous write
216 */
91447636 217#define HARD_THROTTLE_MAXCNT 0
b0d623f7 218#define HARD_THROTTLE_MAXSIZE (32 * 1024)
55e303ae
A
219
220int hard_throttle_on_root = 0;
221struct timeval priority_IO_timestamp_for_root;
222
223
91447636
A
224void
225cluster_init(void) {
2d21ac55 226 /*
91447636
A
227 * allocate lock group attribute and group
228 */
2d21ac55 229 cl_mtx_grp_attr = lck_grp_attr_alloc_init();
91447636
A
230 cl_mtx_grp = lck_grp_alloc_init("cluster I/O", cl_mtx_grp_attr);
231
232 /*
233 * allocate the lock attribute
234 */
235 cl_mtx_attr = lck_attr_alloc_init();
91447636
A
236
237 /*
238 * allocate and initialize mutex's used to protect updates and waits
239 * on the cluster_io context
240 */
241 cl_mtxp = lck_mtx_alloc_init(cl_mtx_grp, cl_mtx_attr);
242
243 if (cl_mtxp == NULL)
244 panic("cluster_init: failed to allocate cl_mtxp");
245}
246
247
cf7d32b8
A
248uint32_t
249cluster_max_io_size(mount_t mp, int type)
250{
b0d623f7
A
251 uint32_t max_io_size;
252 uint32_t segcnt;
253 uint32_t maxcnt;
254
255 switch(type) {
256
257 case CL_READ:
258 segcnt = mp->mnt_segreadcnt;
259 maxcnt = mp->mnt_maxreadcnt;
260 break;
261 case CL_WRITE:
262 segcnt = mp->mnt_segwritecnt;
263 maxcnt = mp->mnt_maxwritecnt;
264 break;
265 default:
266 segcnt = min(mp->mnt_segreadcnt, mp->mnt_segwritecnt);
267 maxcnt = min(mp->mnt_maxreadcnt, mp->mnt_maxwritecnt);
268 break;
269 }
cf7d32b8
A
270 if (segcnt > MAX_UPL_SIZE) {
271 /*
272 * don't allow a size beyond the max UPL size we can create
273 */
274 segcnt = MAX_UPL_SIZE;
275 }
276 max_io_size = min((segcnt * PAGE_SIZE), maxcnt);
277
278 if (max_io_size < (MAX_UPL_TRANSFER * PAGE_SIZE)) {
279 /*
280 * don't allow a size smaller than the old fixed limit
281 */
282 max_io_size = (MAX_UPL_TRANSFER * PAGE_SIZE);
283 } else {
284 /*
285 * make sure the size specified is a multiple of PAGE_SIZE
286 */
287 max_io_size &= ~PAGE_MASK;
288 }
289 return (max_io_size);
290}
291
292
293
91447636
A
294
295#define CLW_ALLOCATE 0x01
296#define CLW_RETURNLOCKED 0x02
2d21ac55
A
297#define CLW_IONOCACHE 0x04
298#define CLW_IOPASSIVE 0x08
299
91447636
A
300/*
301 * if the read ahead context doesn't yet exist,
302 * allocate and initialize it...
303 * the vnode lock serializes multiple callers
304 * during the actual assignment... first one
305 * to grab the lock wins... the other callers
306 * will release the now unnecessary storage
307 *
308 * once the context is present, try to grab (but don't block on)
309 * the lock associated with it... if someone
310 * else currently owns it, than the read
311 * will run without read-ahead. this allows
312 * multiple readers to run in parallel and
313 * since there's only 1 read ahead context,
314 * there's no real loss in only allowing 1
315 * reader to have read-ahead enabled.
316 */
317static struct cl_readahead *
318cluster_get_rap(vnode_t vp)
319{
320 struct ubc_info *ubc;
321 struct cl_readahead *rap;
322
323 ubc = vp->v_ubcinfo;
324
325 if ((rap = ubc->cl_rahead) == NULL) {
326 MALLOC_ZONE(rap, struct cl_readahead *, sizeof *rap, M_CLRDAHEAD, M_WAITOK);
327
328 bzero(rap, sizeof *rap);
329 rap->cl_lastr = -1;
330 lck_mtx_init(&rap->cl_lockr, cl_mtx_grp, cl_mtx_attr);
331
332 vnode_lock(vp);
333
334 if (ubc->cl_rahead == NULL)
335 ubc->cl_rahead = rap;
336 else {
337 lck_mtx_destroy(&rap->cl_lockr, cl_mtx_grp);
338 FREE_ZONE((void *)rap, sizeof *rap, M_CLRDAHEAD);
2d21ac55 339 rap = ubc->cl_rahead;
91447636
A
340 }
341 vnode_unlock(vp);
342 }
343 if (lck_mtx_try_lock(&rap->cl_lockr) == TRUE)
344 return(rap);
345
346 return ((struct cl_readahead *)NULL);
347}
348
349
350/*
351 * if the write behind context doesn't yet exist,
352 * and CLW_ALLOCATE is specified, allocate and initialize it...
353 * the vnode lock serializes multiple callers
354 * during the actual assignment... first one
355 * to grab the lock wins... the other callers
356 * will release the now unnecessary storage
357 *
358 * if CLW_RETURNLOCKED is set, grab (blocking if necessary)
359 * the lock associated with the write behind context before
360 * returning
361 */
362
363static struct cl_writebehind *
364cluster_get_wbp(vnode_t vp, int flags)
365{
366 struct ubc_info *ubc;
367 struct cl_writebehind *wbp;
368
369 ubc = vp->v_ubcinfo;
370
371 if ((wbp = ubc->cl_wbehind) == NULL) {
372
373 if ( !(flags & CLW_ALLOCATE))
374 return ((struct cl_writebehind *)NULL);
375
376 MALLOC_ZONE(wbp, struct cl_writebehind *, sizeof *wbp, M_CLWRBEHIND, M_WAITOK);
377
378 bzero(wbp, sizeof *wbp);
379 lck_mtx_init(&wbp->cl_lockw, cl_mtx_grp, cl_mtx_attr);
380
381 vnode_lock(vp);
382
383 if (ubc->cl_wbehind == NULL)
384 ubc->cl_wbehind = wbp;
385 else {
386 lck_mtx_destroy(&wbp->cl_lockw, cl_mtx_grp);
387 FREE_ZONE((void *)wbp, sizeof *wbp, M_CLWRBEHIND);
2d21ac55 388 wbp = ubc->cl_wbehind;
91447636
A
389 }
390 vnode_unlock(vp);
391 }
392 if (flags & CLW_RETURNLOCKED)
393 lck_mtx_lock(&wbp->cl_lockw);
394
395 return (wbp);
396}
397
398
2d21ac55
A
399static void
400cluster_syncup(vnode_t vp, off_t newEOF, int (*callback)(buf_t, void *), void *callback_arg)
401{
402 struct cl_writebehind *wbp;
403
404 if ((wbp = cluster_get_wbp(vp, 0)) != NULL) {
405
406 if (wbp->cl_number) {
407 lck_mtx_lock(&wbp->cl_lockw);
408
409 cluster_try_push(wbp, vp, newEOF, PUSH_ALL | PUSH_SYNC, callback, callback_arg);
410
411 lck_mtx_unlock(&wbp->cl_lockw);
412 }
413 }
414}
415
416
55e303ae 417static int
b0d623f7 418cluster_hard_throttle_on(vnode_t vp, uint32_t hard_throttle)
55e303ae 419{
b0d623f7
A
420 struct uthread *ut;
421
422 if (hard_throttle) {
423 static struct timeval hard_throttle_maxelapsed = { 0, 200000 };
55e303ae 424
b0d623f7
A
425 if (vp->v_mount->mnt_kern_flag & MNTK_ROOTDEV) {
426 struct timeval elapsed;
55e303ae 427
b0d623f7
A
428 if (hard_throttle_on_root)
429 return(1);
55e303ae 430
b0d623f7
A
431 microuptime(&elapsed);
432 timevalsub(&elapsed, &priority_IO_timestamp_for_root);
55e303ae 433
b0d623f7
A
434 if (timevalcmp(&elapsed, &hard_throttle_maxelapsed, <))
435 return(1);
436 }
55e303ae 437 }
593a1d5f 438 if (throttle_get_io_policy(&ut) == IOPOL_THROTTLE) {
b0d623f7 439 if (throttle_io_will_be_throttled(-1, vp->v_mount)) {
593a1d5f
A
440 return(1);
441 }
442 }
55e303ae
A
443 return(0);
444}
445
1c79356b
A
446
447static int
2d21ac55
A
448cluster_ioerror(upl_t upl, int upl_offset, int abort_size, int error, int io_flags)
449{
450 int upl_abort_code = 0;
451 int page_in = 0;
452 int page_out = 0;
453
454 if (io_flags & B_PHYS)
455 /*
456 * direct write of any flavor, or a direct read that wasn't aligned
457 */
458 ubc_upl_commit_range(upl, upl_offset, abort_size, UPL_COMMIT_FREE_ON_EMPTY);
459 else {
460 if (io_flags & B_PAGEIO) {
461 if (io_flags & B_READ)
462 page_in = 1;
463 else
464 page_out = 1;
465 }
466 if (io_flags & B_CACHE)
467 /*
468 * leave pages in the cache unchanged on error
469 */
470 upl_abort_code = UPL_ABORT_FREE_ON_EMPTY;
471 else if (page_out && (error != ENXIO))
472 /*
473 * transient error... leave pages unchanged
474 */
475 upl_abort_code = UPL_ABORT_FREE_ON_EMPTY;
476 else if (page_in)
477 upl_abort_code = UPL_ABORT_FREE_ON_EMPTY | UPL_ABORT_ERROR;
478 else
479 upl_abort_code = UPL_ABORT_FREE_ON_EMPTY | UPL_ABORT_DUMP_PAGES;
480
481 ubc_upl_abort_range(upl, upl_offset, abort_size, upl_abort_code);
482 }
483 return (upl_abort_code);
484}
485
486
487static int
488cluster_iodone(buf_t bp, void *callback_arg)
1c79356b 489{
91447636
A
490 int b_flags;
491 int error;
492 int total_size;
493 int total_resid;
494 int upl_offset;
495 int zero_offset;
2d21ac55
A
496 int pg_offset = 0;
497 int commit_size = 0;
498 int upl_flags = 0;
499 int transaction_size = 0;
91447636
A
500 upl_t upl;
501 buf_t cbp;
502 buf_t cbp_head;
503 buf_t cbp_next;
504 buf_t real_bp;
505 struct clios *iostate;
2d21ac55 506 boolean_t transaction_complete = FALSE;
91447636
A
507
508 cbp_head = (buf_t)(bp->b_trans_head);
1c79356b
A
509
510 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 20)) | DBG_FUNC_START,
b0d623f7 511 cbp_head, bp->b_lblkno, bp->b_bcount, bp->b_flags, 0);
1c79356b
A
512
513 for (cbp = cbp_head; cbp; cbp = cbp->b_trans_next) {
514 /*
515 * all I/O requests that are part of this transaction
516 * have to complete before we can process it
517 */
518 if ( !(cbp->b_flags & B_DONE)) {
519
520 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 20)) | DBG_FUNC_END,
b0d623f7 521 cbp_head, cbp, cbp->b_bcount, cbp->b_flags, 0);
1c79356b 522
2d21ac55 523 return 0;
1c79356b 524 }
2d21ac55
A
525 if (cbp->b_flags & B_EOT)
526 transaction_complete = TRUE;
527 }
528 if (transaction_complete == FALSE) {
529 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 20)) | DBG_FUNC_END,
b0d623f7 530 cbp_head, 0, 0, 0, 0);
2d21ac55
A
531
532 return 0;
1c79356b
A
533 }
534 error = 0;
535 total_size = 0;
536 total_resid = 0;
537
538 cbp = cbp_head;
539 upl_offset = cbp->b_uploffset;
91447636 540 upl = cbp->b_upl;
1c79356b
A
541 b_flags = cbp->b_flags;
542 real_bp = cbp->b_real_bp;
9bccf70c 543 zero_offset= cbp->b_validend;
b4c24cb9 544 iostate = (struct clios *)cbp->b_iostate;
1c79356b 545
91447636
A
546 if (real_bp)
547 real_bp->b_dev = cbp->b_dev;
548
1c79356b 549 while (cbp) {
1c79356b
A
550 if ((cbp->b_flags & B_ERROR) && error == 0)
551 error = cbp->b_error;
552
553 total_resid += cbp->b_resid;
554 total_size += cbp->b_bcount;
555
556 cbp_next = cbp->b_trans_next;
557
2d21ac55
A
558 if (cbp_next == NULL)
559 /*
560 * compute the overall size of the transaction
561 * in case we created one that has 'holes' in it
562 * 'total_size' represents the amount of I/O we
563 * did, not the span of the transaction w/r to the UPL
564 */
565 transaction_size = cbp->b_uploffset + cbp->b_bcount - upl_offset;
566
567 if (cbp != cbp_head)
568 free_io_buf(cbp);
1c79356b
A
569
570 cbp = cbp_next;
571 }
2d21ac55
A
572 if (error == 0 && total_resid)
573 error = EIO;
574
575 if (error == 0) {
576 int (*cliodone_func)(buf_t, void *) = (int (*)(buf_t, void *))(cbp_head->b_cliodone);
577
578 if (cliodone_func != NULL) {
579 cbp_head->b_bcount = transaction_size;
580
581 error = (*cliodone_func)(cbp_head, callback_arg);
582 }
583 }
b4c24cb9
A
584 if (zero_offset)
585 cluster_zero(upl, zero_offset, PAGE_SIZE - (zero_offset & PAGE_MASK), real_bp);
586
2d21ac55
A
587 free_io_buf(cbp_head);
588
b4c24cb9 589 if (iostate) {
91447636
A
590 int need_wakeup = 0;
591
d7e50217
A
592 /*
593 * someone has issued multiple I/Os asynchrounsly
594 * and is waiting for them to complete (streaming)
595 */
2d21ac55 596 lck_mtx_lock_spin(cl_mtxp);
91447636 597
d7e50217
A
598 if (error && iostate->io_error == 0)
599 iostate->io_error = error;
9bccf70c 600
b4c24cb9
A
601 iostate->io_completed += total_size;
602
603 if (iostate->io_wanted) {
d7e50217
A
604 /*
605 * someone is waiting for the state of
606 * this io stream to change
607 */
b4c24cb9 608 iostate->io_wanted = 0;
91447636 609 need_wakeup = 1;
b4c24cb9 610 }
91447636
A
611 lck_mtx_unlock(cl_mtxp);
612
613 if (need_wakeup)
614 wakeup((caddr_t)&iostate->io_wanted);
b4c24cb9 615 }
1c79356b
A
616
617 if (b_flags & B_COMMIT_UPL) {
91447636 618
2d21ac55
A
619 pg_offset = upl_offset & PAGE_MASK;
620 commit_size = (pg_offset + transaction_size + (PAGE_SIZE - 1)) & ~PAGE_MASK;
1c79356b 621
2d21ac55
A
622 if (error)
623 upl_flags = cluster_ioerror(upl, upl_offset - pg_offset, commit_size, error, b_flags);
624 else {
625 upl_flags = UPL_COMMIT_FREE_ON_EMPTY;
1c79356b 626
91447636 627 if ((b_flags & B_PHYS) && (b_flags & B_READ))
2d21ac55 628 upl_flags |= UPL_COMMIT_SET_DIRTY;
55e303ae 629
1c79356b 630 if (b_flags & B_AGE)
2d21ac55 631 upl_flags |= UPL_COMMIT_INACTIVATE;
1c79356b 632
2d21ac55 633 ubc_upl_commit_range(upl, upl_offset - pg_offset, commit_size, upl_flags);
1c79356b 634 }
91447636 635 }
2d21ac55
A
636 if ((b_flags & B_NEED_IODONE) && real_bp) {
637 if (error) {
638 real_bp->b_flags |= B_ERROR;
639 real_bp->b_error = error;
640 }
641 real_bp->b_resid = total_resid;
642
643 buf_biodone(real_bp);
644 }
645 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 20)) | DBG_FUNC_END,
b0d623f7 646 upl, upl_offset - pg_offset, commit_size, (error << 24) | upl_flags, 0);
1c79356b
A
647
648 return (error);
649}
650
651
b0d623f7
A
652uint32_t
653cluster_hard_throttle_limit(vnode_t vp, uint32_t *limit, uint32_t hard_throttle)
654{
655 if (cluster_hard_throttle_on(vp, hard_throttle)) {
656 *limit = HARD_THROTTLE_MAXSIZE;
657 return 1;
658 }
659 return 0;
660}
661
662
91447636 663void
b0d623f7 664cluster_zero(upl_t upl, upl_offset_t upl_offset, int size, buf_t bp)
1c79356b 665{
1c79356b 666
55e303ae 667 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 23)) | DBG_FUNC_START,
b0d623f7 668 upl_offset, size, bp, 0, 0);
9bccf70c 669
91447636 670 if (bp == NULL || bp->b_datap == 0) {
2d21ac55
A
671 upl_page_info_t *pl;
672 addr64_t zero_addr;
9bccf70c 673
55e303ae
A
674 pl = ubc_upl_pageinfo(upl);
675
2d21ac55
A
676 if (upl_device_page(pl) == TRUE) {
677 zero_addr = ((addr64_t)upl_phys_page(pl, 0) << 12) + upl_offset;
678
679 bzero_phys_nc(zero_addr, size);
680 } else {
681 while (size) {
682 int page_offset;
683 int page_index;
684 int zero_cnt;
55e303ae 685
2d21ac55
A
686 page_index = upl_offset / PAGE_SIZE;
687 page_offset = upl_offset & PAGE_MASK;
55e303ae 688
2d21ac55
A
689 zero_addr = ((addr64_t)upl_phys_page(pl, page_index) << 12) + page_offset;
690 zero_cnt = min(PAGE_SIZE - page_offset, size);
55e303ae 691
2d21ac55 692 bzero_phys(zero_addr, zero_cnt);
55e303ae 693
2d21ac55
A
694 size -= zero_cnt;
695 upl_offset += zero_cnt;
696 }
55e303ae 697 }
1c79356b 698 } else
91447636 699 bzero((caddr_t)((vm_offset_t)bp->b_datap + upl_offset), size);
1c79356b 700
55e303ae
A
701 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 23)) | DBG_FUNC_END,
702 upl_offset, size, 0, 0, 0);
1c79356b
A
703}
704
91447636 705
2d21ac55
A
706static void
707cluster_EOT(buf_t cbp_head, buf_t cbp_tail, int zero_offset)
708{
709 cbp_head->b_validend = zero_offset;
710 cbp_tail->b_flags |= B_EOT;
711}
712
713static void
714cluster_wait_IO(buf_t cbp_head, int async)
715{
716 buf_t cbp;
717
718 if (async) {
719 /*
720 * async callback completion will not normally
721 * generate a wakeup upon I/O completion...
722 * by setting BL_WANTED, we will force a wakeup
723 * to occur as any outstanding I/Os complete...
724 * I/Os already completed will have BL_CALLDONE already
725 * set and we won't block in buf_biowait_callback..
726 * note that we're actually waiting for the bp to have
727 * completed the callback function... only then
728 * can we safely take back ownership of the bp
729 * need the main buf mutex in order to safely
730 * update b_lflags
731 */
732 buf_list_lock();
733
734 for (cbp = cbp_head; cbp; cbp = cbp->b_trans_next)
735 cbp->b_lflags |= BL_WANTED;
736
737 buf_list_unlock();
738 }
739 for (cbp = cbp_head; cbp; cbp = cbp->b_trans_next) {
740 if (async)
741 buf_biowait_callback(cbp);
742 else
743 buf_biowait(cbp);
744 }
745}
746
747static void
748cluster_complete_transaction(buf_t *cbp_head, void *callback_arg, int *retval, int flags, int needwait)
749{
750 buf_t cbp;
751 int error;
752
753 /*
754 * cluster_complete_transaction will
755 * only be called if we've issued a complete chain in synchronous mode
756 * or, we've already done a cluster_wait_IO on an incomplete chain
757 */
758 if (needwait) {
759 for (cbp = *cbp_head; cbp; cbp = cbp->b_trans_next)
760 buf_biowait(cbp);
761 }
762 error = cluster_iodone(*cbp_head, callback_arg);
763
764 if ( !(flags & CL_ASYNC) && error && *retval == 0) {
765 if (((flags & (CL_PAGEOUT | CL_KEEPCACHED)) != CL_PAGEOUT) || (error != ENXIO))
766 *retval = error;
767 }
768 *cbp_head = (buf_t)NULL;
769}
770
771
1c79356b 772static int
91447636 773cluster_io(vnode_t vp, upl_t upl, vm_offset_t upl_offset, off_t f_offset, int non_rounded_size,
2d21ac55 774 int flags, buf_t real_bp, struct clios *iostate, int (*callback)(buf_t, void *), void *callback_arg)
1c79356b 775{
91447636
A
776 buf_t cbp;
777 u_int size;
778 u_int io_size;
779 int io_flags;
780 int bmap_flags;
781 int error = 0;
782 int retval = 0;
783 buf_t cbp_head = NULL;
784 buf_t cbp_tail = NULL;
785 int trans_count = 0;
2d21ac55 786 int max_trans_count;
91447636
A
787 u_int pg_count;
788 int pg_offset;
789 u_int max_iosize;
790 u_int max_vectors;
791 int priv;
792 int zero_offset = 0;
793 int async_throttle = 0;
794 mount_t mp;
2d21ac55
A
795 vm_offset_t upl_end_offset;
796 boolean_t need_EOT = FALSE;
797
798 /*
799 * we currently don't support buffers larger than a page
800 */
801 if (real_bp && non_rounded_size > PAGE_SIZE)
802 panic("%s(): Called with real buffer of size %d bytes which "
803 "is greater than the maximum allowed size of "
804 "%d bytes (the system PAGE_SIZE).\n",
805 __FUNCTION__, non_rounded_size, PAGE_SIZE);
91447636
A
806
807 mp = vp->v_mount;
808
2d21ac55
A
809 /*
810 * we don't want to do any funny rounding of the size for IO requests
811 * coming through the DIRECT or CONTIGUOUS paths... those pages don't
812 * belong to us... we can't extend (nor do we need to) the I/O to fill
813 * out a page
814 */
815 if (mp->mnt_devblocksize > 1 && !(flags & (CL_DEV_MEMORY | CL_DIRECT_IO))) {
91447636
A
816 /*
817 * round the requested size up so that this I/O ends on a
818 * page boundary in case this is a 'write'... if the filesystem
819 * has blocks allocated to back the page beyond the EOF, we want to
820 * make sure to write out the zero's that are sitting beyond the EOF
821 * so that in case the filesystem doesn't explicitly zero this area
822 * if a hole is created via a lseek/write beyond the current EOF,
823 * it will return zeros when it's read back from the disk. If the
824 * physical allocation doesn't extend for the whole page, we'll
825 * only write/read from the disk up to the end of this allocation
826 * via the extent info returned from the VNOP_BLOCKMAP call.
827 */
828 pg_offset = upl_offset & PAGE_MASK;
55e303ae 829
91447636
A
830 size = (((non_rounded_size + pg_offset) + (PAGE_SIZE - 1)) & ~PAGE_MASK) - pg_offset;
831 } else {
832 /*
833 * anyone advertising a blocksize of 1 byte probably
834 * can't deal with us rounding up the request size
835 * AFP is one such filesystem/device
836 */
837 size = non_rounded_size;
838 }
2d21ac55
A
839 upl_end_offset = upl_offset + size;
840
841 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 22)) | DBG_FUNC_START, (int)f_offset, size, upl_offset, flags, 0);
842
843 /*
844 * Set the maximum transaction size to the maximum desired number of
845 * buffers.
846 */
847 max_trans_count = 8;
848 if (flags & CL_DEV_MEMORY)
849 max_trans_count = 16;
55e303ae 850
0b4e3aa0 851 if (flags & CL_READ) {
2d21ac55 852 io_flags = B_READ;
91447636 853 bmap_flags = VNODE_READ;
0b4e3aa0 854
91447636
A
855 max_iosize = mp->mnt_maxreadcnt;
856 max_vectors = mp->mnt_segreadcnt;
0b4e3aa0 857 } else {
2d21ac55 858 io_flags = B_WRITE;
91447636 859 bmap_flags = VNODE_WRITE;
1c79356b 860
91447636
A
861 max_iosize = mp->mnt_maxwritecnt;
862 max_vectors = mp->mnt_segwritecnt;
0b4e3aa0 863 }
91447636
A
864 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 22)) | DBG_FUNC_NONE, max_iosize, max_vectors, mp->mnt_devblocksize, 0, 0);
865
55e303ae 866 /*
91447636
A
867 * make sure the maximum iosize is a
868 * multiple of the page size
55e303ae
A
869 */
870 max_iosize &= ~PAGE_MASK;
871
2d21ac55
A
872 /*
873 * Ensure the maximum iosize is sensible.
874 */
875 if (!max_iosize)
876 max_iosize = PAGE_SIZE;
877
55e303ae 878 if (flags & CL_THROTTLE) {
b0d623f7 879 if ( !(flags & CL_PAGEOUT) && cluster_hard_throttle_on(vp, 1)) {
55e303ae
A
880 if (max_iosize > HARD_THROTTLE_MAXSIZE)
881 max_iosize = HARD_THROTTLE_MAXSIZE;
882 async_throttle = HARD_THROTTLE_MAXCNT;
2d21ac55
A
883 } else {
884 if ( (flags & CL_DEV_MEMORY) )
b0d623f7 885 async_throttle = IO_SCALE(vp, VNODE_ASYNC_THROTTLE);
2d21ac55
A
886 else {
887 u_int max_cluster;
cf7d32b8
A
888 u_int max_cluster_size;
889 u_int max_prefetch;
2d21ac55 890
b0d623f7
A
891 max_cluster_size = MAX_CLUSTER_SIZE(vp);
892 max_prefetch = MAX_PREFETCH(vp, cluster_max_io_size(vp->v_mount, CL_READ));
893
cf7d32b8 894 if (max_iosize > max_cluster_size)
b0d623f7 895 max_cluster = max_cluster_size;
2d21ac55
A
896 else
897 max_cluster = max_iosize;
898
899 if (size < max_cluster)
900 max_cluster = size;
901
b0d623f7 902 async_throttle = min(IO_SCALE(vp, VNODE_ASYNC_THROTTLE), (max_prefetch / max_cluster) - 1);
2d21ac55
A
903 }
904 }
55e303ae 905 }
1c79356b
A
906 if (flags & CL_AGE)
907 io_flags |= B_AGE;
91447636
A
908 if (flags & (CL_PAGEIN | CL_PAGEOUT))
909 io_flags |= B_PAGEIO;
b0d623f7
A
910 if (flags & (CL_IOSTREAMING))
911 io_flags |= B_IOSTREAMING;
b4c24cb9
A
912 if (flags & CL_COMMIT)
913 io_flags |= B_COMMIT_UPL;
914 if (flags & CL_PRESERVE)
915 io_flags |= B_PHYS;
91447636
A
916 if (flags & CL_KEEPCACHED)
917 io_flags |= B_CACHE;
2d21ac55
A
918 if (flags & CL_PASSIVE)
919 io_flags |= B_PASSIVE;
920 if (vp->v_flag & VSYSTEM)
921 io_flags |= B_META;
1c79356b 922
9bccf70c 923 if ((flags & CL_READ) && ((upl_offset + non_rounded_size) & PAGE_MASK) && (!(flags & CL_NOZERO))) {
1c79356b
A
924 /*
925 * then we are going to end up
926 * with a page that we can't complete (the file size wasn't a multiple
927 * of PAGE_SIZE and we're trying to read to the end of the file
928 * so we'll go ahead and zero out the portion of the page we can't
929 * read in from the file
930 */
9bccf70c 931 zero_offset = upl_offset + non_rounded_size;
1c79356b
A
932 }
933 while (size) {
91447636
A
934 daddr64_t blkno;
935 daddr64_t lblkno;
2d21ac55 936 u_int io_size_wanted;
b0d623f7 937 size_t io_size_tmp;
1c79356b 938
0b4e3aa0
A
939 if (size > max_iosize)
940 io_size = max_iosize;
1c79356b
A
941 else
942 io_size = size;
2d21ac55
A
943
944 io_size_wanted = io_size;
b0d623f7 945 io_size_tmp = (size_t)io_size;
91447636 946
b0d623f7 947 if ((error = VNOP_BLOCKMAP(vp, f_offset, io_size, &blkno, &io_size_tmp, NULL, bmap_flags, NULL)))
1c79356b 948 break;
2d21ac55 949
b0d623f7 950 if (io_size_tmp > io_size_wanted)
2d21ac55 951 io_size = io_size_wanted;
b0d623f7
A
952 else
953 io_size = (u_int)io_size_tmp;
2d21ac55 954
91447636
A
955 if (real_bp && (real_bp->b_blkno == real_bp->b_lblkno))
956 real_bp->b_blkno = blkno;
1c79356b
A
957
958 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 24)) | DBG_FUNC_NONE,
2d21ac55 959 (int)f_offset, (int)(blkno>>32), (int)blkno, io_size, 0);
1c79356b 960
91447636
A
961 if (io_size == 0) {
962 /*
963 * vnop_blockmap didn't return an error... however, it did
964 * return an extent size of 0 which means we can't
965 * make forward progress on this I/O... a hole in the
966 * file would be returned as a blkno of -1 with a non-zero io_size
967 * a real extent is returned with a blkno != -1 and a non-zero io_size
968 */
969 error = EINVAL;
970 break;
971 }
972 if ( !(flags & CL_READ) && blkno == -1) {
2d21ac55
A
973 off_t e_offset;
974 int pageout_flags;
91447636 975
b0d623f7
A
976 if(upl_get_internal_vectorupl(upl))
977 panic("Vector UPLs should not take this code-path\n");
91447636
A
978 /*
979 * we're writing into a 'hole'
980 */
0b4e3aa0 981 if (flags & CL_PAGEOUT) {
91447636
A
982 /*
983 * if we got here via cluster_pageout
984 * then just error the request and return
985 * the 'hole' should already have been covered
986 */
0b4e3aa0
A
987 error = EINVAL;
988 break;
91447636 989 }
91447636
A
990 /*
991 * we can get here if the cluster code happens to
992 * pick up a page that was dirtied via mmap vs
993 * a 'write' and the page targets a 'hole'...
994 * i.e. the writes to the cluster were sparse
995 * and the file was being written for the first time
996 *
997 * we can also get here if the filesystem supports
998 * 'holes' that are less than PAGE_SIZE.... because
999 * we can't know if the range in the page that covers
1000 * the 'hole' has been dirtied via an mmap or not,
1001 * we have to assume the worst and try to push the
1002 * entire page to storage.
1003 *
1004 * Try paging out the page individually before
1005 * giving up entirely and dumping it (the pageout
1006 * path will insure that the zero extent accounting
1007 * has been taken care of before we get back into cluster_io)
2d21ac55
A
1008 *
1009 * go direct to vnode_pageout so that we don't have to
1010 * unbusy the page from the UPL... we used to do this
1011 * so that we could call ubc_sync_range, but that results
1012 * in a potential deadlock if someone else races us to acquire
1013 * that page and wins and in addition needs one of the pages
1014 * we're continuing to hold in the UPL
0b4e3aa0 1015 */
2d21ac55 1016 pageout_flags = UPL_MSYNC | UPL_VNODE_PAGER | UPL_NESTED_PAGEOUT;
91447636 1017
2d21ac55
A
1018 if ( !(flags & CL_ASYNC))
1019 pageout_flags |= UPL_IOSYNC;
1020 if ( !(flags & CL_COMMIT))
1021 pageout_flags |= UPL_NOCOMMIT;
1022
1023 if (cbp_head) {
1024 buf_t last_cbp;
1025
1026 /*
1027 * first we have to wait for the the current outstanding I/Os
1028 * to complete... EOT hasn't been set yet on this transaction
1029 * so the pages won't be released just because all of the current
1030 * I/O linked to this transaction has completed...
1031 */
1032 cluster_wait_IO(cbp_head, (flags & CL_ASYNC));
1033
1034 /*
1035 * we've got a transcation that
1036 * includes the page we're about to push out through vnode_pageout...
1037 * find the last bp in the list which will be the one that
1038 * includes the head of this page and round it's iosize down
1039 * to a page boundary...
1040 */
1041 for (last_cbp = cbp = cbp_head; cbp->b_trans_next; cbp = cbp->b_trans_next)
1042 last_cbp = cbp;
1043
1044 cbp->b_bcount &= ~PAGE_MASK;
1045
1046 if (cbp->b_bcount == 0) {
1047 /*
1048 * this buf no longer has any I/O associated with it
1049 */
1050 free_io_buf(cbp);
1051
1052 if (cbp == cbp_head) {
1053 /*
1054 * the buf we just freed was the only buf in
1055 * this transaction... so there's no I/O to do
1056 */
1057 cbp_head = NULL;
1058 } else {
1059 /*
1060 * remove the buf we just freed from
1061 * the transaction list
1062 */
1063 last_cbp->b_trans_next = NULL;
1064 cbp_tail = last_cbp;
1065 }
1066 }
1067 if (cbp_head) {
1068 /*
1069 * there was more to the current transaction
1070 * than just the page we are pushing out via vnode_pageout...
1071 * mark it as finished and complete it... we've already
1072 * waited for the I/Os to complete above in the call to cluster_wait_IO
1073 */
1074 cluster_EOT(cbp_head, cbp_tail, 0);
91447636 1075
2d21ac55
A
1076 cluster_complete_transaction(&cbp_head, callback_arg, &retval, flags, 0);
1077
1078 trans_count = 0;
1079 }
1080 }
1081 if (vnode_pageout(vp, upl, trunc_page(upl_offset), trunc_page_64(f_offset), PAGE_SIZE, pageout_flags, NULL) != PAGER_SUCCESS) {
91447636 1082 error = EINVAL;
0b4e3aa0 1083 break;
91447636 1084 }
2d21ac55 1085 e_offset = round_page_64(f_offset + 1);
91447636
A
1086 io_size = e_offset - f_offset;
1087
1088 f_offset += io_size;
1089 upl_offset += io_size;
1090
1091 if (size >= io_size)
1092 size -= io_size;
1093 else
1094 size = 0;
1095 /*
1096 * keep track of how much of the original request
1097 * that we've actually completed... non_rounded_size
1098 * may go negative due to us rounding the request
1099 * to a page size multiple (i.e. size > non_rounded_size)
1100 */
1101 non_rounded_size -= io_size;
1102
1103 if (non_rounded_size <= 0) {
1104 /*
1105 * we've transferred all of the data in the original
1106 * request, but we were unable to complete the tail
1107 * of the last page because the file didn't have
1108 * an allocation to back that portion... this is ok.
1109 */
1110 size = 0;
1111 }
0b4e3aa0 1112 continue;
1c79356b 1113 }
91447636 1114 lblkno = (daddr64_t)(f_offset / PAGE_SIZE_64);
1c79356b
A
1115 /*
1116 * we have now figured out how much I/O we can do - this is in 'io_size'
1c79356b
A
1117 * pg_offset is the starting point in the first page for the I/O
1118 * pg_count is the number of full and partial pages that 'io_size' encompasses
1119 */
1c79356b 1120 pg_offset = upl_offset & PAGE_MASK;
1c79356b 1121
0b4e3aa0 1122 if (flags & CL_DEV_MEMORY) {
0b4e3aa0
A
1123 /*
1124 * treat physical requests as one 'giant' page
1125 */
1126 pg_count = 1;
55e303ae
A
1127 } else
1128 pg_count = (io_size + pg_offset + (PAGE_SIZE - 1)) / PAGE_SIZE;
1129
91447636 1130 if ((flags & CL_READ) && blkno == -1) {
2d21ac55 1131 vm_offset_t commit_offset;
9bccf70c 1132 int bytes_to_zero;
2d21ac55 1133 int complete_transaction_now = 0;
9bccf70c 1134
1c79356b
A
1135 /*
1136 * if we're reading and blkno == -1, then we've got a
1137 * 'hole' in the file that we need to deal with by zeroing
1138 * out the affected area in the upl
1139 */
2d21ac55 1140 if (io_size >= (u_int)non_rounded_size) {
9bccf70c
A
1141 /*
1142 * if this upl contains the EOF and it is not a multiple of PAGE_SIZE
1143 * than 'zero_offset' will be non-zero
91447636 1144 * if the 'hole' returned by vnop_blockmap extends all the way to the eof
9bccf70c
A
1145 * (indicated by the io_size finishing off the I/O request for this UPL)
1146 * than we're not going to issue an I/O for the
1147 * last page in this upl... we need to zero both the hole and the tail
1148 * of the page beyond the EOF, since the delayed zero-fill won't kick in
1149 */
2d21ac55
A
1150 bytes_to_zero = non_rounded_size;
1151 if (!(flags & CL_NOZERO))
1152 bytes_to_zero = (((upl_offset + io_size) + (PAGE_SIZE - 1)) & ~PAGE_MASK) - upl_offset;
1c79356b 1153
9bccf70c
A
1154 zero_offset = 0;
1155 } else
1156 bytes_to_zero = io_size;
1c79356b 1157
2d21ac55
A
1158 pg_count = 0;
1159
1160 cluster_zero(upl, upl_offset, bytes_to_zero, real_bp);
9bccf70c 1161
2d21ac55
A
1162 if (cbp_head) {
1163 int pg_resid;
1164
9bccf70c
A
1165 /*
1166 * if there is a current I/O chain pending
1167 * then the first page of the group we just zero'd
1168 * will be handled by the I/O completion if the zero
1169 * fill started in the middle of the page
1170 */
2d21ac55
A
1171 commit_offset = (upl_offset + (PAGE_SIZE - 1)) & ~PAGE_MASK;
1172
1173 pg_resid = commit_offset - upl_offset;
1174
1175 if (bytes_to_zero >= pg_resid) {
1176 /*
1177 * the last page of the current I/O
1178 * has been completed...
1179 * compute the number of fully zero'd
1180 * pages that are beyond it
1181 * plus the last page if its partial
1182 * and we have no more I/O to issue...
1183 * otherwise a partial page is left
1184 * to begin the next I/O
1185 */
1186 if ((int)io_size >= non_rounded_size)
1187 pg_count = (bytes_to_zero - pg_resid + (PAGE_SIZE - 1)) / PAGE_SIZE;
1188 else
1189 pg_count = (bytes_to_zero - pg_resid) / PAGE_SIZE;
1190
1191 complete_transaction_now = 1;
1192 }
1193 } else {
9bccf70c 1194 /*
2d21ac55
A
1195 * no pending I/O to deal with
1196 * so, commit all of the fully zero'd pages
1197 * plus the last page if its partial
1198 * and we have no more I/O to issue...
1199 * otherwise a partial page is left
1200 * to begin the next I/O
9bccf70c 1201 */
2d21ac55
A
1202 if ((int)io_size >= non_rounded_size)
1203 pg_count = (pg_offset + bytes_to_zero + (PAGE_SIZE - 1)) / PAGE_SIZE;
1c79356b 1204 else
2d21ac55 1205 pg_count = (pg_offset + bytes_to_zero) / PAGE_SIZE;
9bccf70c 1206
2d21ac55
A
1207 commit_offset = upl_offset & ~PAGE_MASK;
1208 }
1209 if ( (flags & CL_COMMIT) && pg_count) {
1210 ubc_upl_commit_range(upl, commit_offset, pg_count * PAGE_SIZE,
1211 UPL_COMMIT_CLEAR_DIRTY | UPL_COMMIT_FREE_ON_EMPTY);
1c79356b
A
1212 }
1213 upl_offset += io_size;
1214 f_offset += io_size;
1215 size -= io_size;
2d21ac55 1216
91447636
A
1217 /*
1218 * keep track of how much of the original request
1219 * that we've actually completed... non_rounded_size
1220 * may go negative due to us rounding the request
1221 * to a page size multiple (i.e. size > non_rounded_size)
1222 */
1223 non_rounded_size -= io_size;
1c79356b 1224
91447636
A
1225 if (non_rounded_size <= 0) {
1226 /*
1227 * we've transferred all of the data in the original
1228 * request, but we were unable to complete the tail
1229 * of the last page because the file didn't have
1230 * an allocation to back that portion... this is ok.
1231 */
1232 size = 0;
1233 }
2d21ac55
A
1234 if (cbp_head && (complete_transaction_now || size == 0)) {
1235 cluster_wait_IO(cbp_head, (flags & CL_ASYNC));
9bccf70c 1236
2d21ac55
A
1237 cluster_EOT(cbp_head, cbp_tail, size == 0 ? zero_offset : 0);
1238
1239 cluster_complete_transaction(&cbp_head, callback_arg, &retval, flags, 0);
1240
1241 trans_count = 0;
1242 }
1243 continue;
1c79356b 1244 }
55e303ae 1245 if (pg_count > max_vectors) {
91447636 1246 if (((pg_count - max_vectors) * PAGE_SIZE) > io_size) {
55e303ae
A
1247 io_size = PAGE_SIZE - pg_offset;
1248 pg_count = 1;
91447636
A
1249 } else {
1250 io_size -= (pg_count - max_vectors) * PAGE_SIZE;
55e303ae 1251 pg_count = max_vectors;
91447636 1252 }
1c79356b 1253 }
2d21ac55
A
1254 /*
1255 * If the transaction is going to reach the maximum number of
1256 * desired elements, truncate the i/o to the nearest page so
1257 * that the actual i/o is initiated after this buffer is
1258 * created and added to the i/o chain.
1259 *
1260 * I/O directed to physically contiguous memory
1261 * doesn't have a requirement to make sure we 'fill' a page
1262 */
1263 if ( !(flags & CL_DEV_MEMORY) && trans_count >= max_trans_count &&
1264 ((upl_offset + io_size) & PAGE_MASK)) {
1265 vm_offset_t aligned_ofs;
1266
1267 aligned_ofs = (upl_offset + io_size) & ~PAGE_MASK;
1268 /*
1269 * If the io_size does not actually finish off even a
1270 * single page we have to keep adding buffers to the
1271 * transaction despite having reached the desired limit.
1272 *
1273 * Eventually we get here with the page being finished
1274 * off (and exceeded) and then we truncate the size of
1275 * this i/o request so that it is page aligned so that
1276 * we can finally issue the i/o on the transaction.
1277 */
1278 if (aligned_ofs > upl_offset) {
1279 io_size = aligned_ofs - upl_offset;
1280 pg_count--;
1281 }
1282 }
1c79356b 1283
91447636 1284 if ( !(mp->mnt_kern_flag & MNTK_VIRTUALDEV))
55e303ae
A
1285 /*
1286 * if we're not targeting a virtual device i.e. a disk image
1287 * it's safe to dip into the reserve pool since real devices
1288 * can complete this I/O request without requiring additional
1289 * bufs from the alloc_io_buf pool
1290 */
1291 priv = 1;
1292 else if ((flags & CL_ASYNC) && !(flags & CL_PAGEOUT))
1293 /*
1294 * Throttle the speculative IO
1295 */
0b4e3aa0
A
1296 priv = 0;
1297 else
1298 priv = 1;
1299
1300 cbp = alloc_io_buf(vp, priv);
1c79356b 1301
55e303ae 1302 if (flags & CL_PAGEOUT) {
91447636
A
1303 u_int i;
1304
55e303ae 1305 for (i = 0; i < pg_count; i++) {
91447636
A
1306 if (buf_invalblkno(vp, lblkno + i, 0) == EBUSY)
1307 panic("BUSY bp found in cluster_io");
1c79356b 1308 }
1c79356b 1309 }
b4c24cb9 1310 if (flags & CL_ASYNC) {
2d21ac55 1311 if (buf_setcallback(cbp, (void *)cluster_iodone, callback_arg))
91447636 1312 panic("buf_setcallback failed\n");
b4c24cb9 1313 }
2d21ac55 1314 cbp->b_cliodone = (void *)callback;
1c79356b
A
1315 cbp->b_flags |= io_flags;
1316
1317 cbp->b_lblkno = lblkno;
1318 cbp->b_blkno = blkno;
1319 cbp->b_bcount = io_size;
1c79356b 1320
91447636
A
1321 if (buf_setupl(cbp, upl, upl_offset))
1322 panic("buf_setupl failed\n");
1323
1324 cbp->b_trans_next = (buf_t)NULL;
1325
1326 if ((cbp->b_iostate = (void *)iostate))
d7e50217
A
1327 /*
1328 * caller wants to track the state of this
1329 * io... bump the amount issued against this stream
1330 */
b4c24cb9
A
1331 iostate->io_issued += io_size;
1332
91447636 1333 if (flags & CL_READ) {
1c79356b 1334 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 26)) | DBG_FUNC_NONE,
91447636
A
1335 (int)cbp->b_lblkno, (int)cbp->b_blkno, upl_offset, io_size, 0);
1336 }
1337 else {
1c79356b 1338 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 27)) | DBG_FUNC_NONE,
91447636
A
1339 (int)cbp->b_lblkno, (int)cbp->b_blkno, upl_offset, io_size, 0);
1340 }
1c79356b
A
1341
1342 if (cbp_head) {
1343 cbp_tail->b_trans_next = cbp;
1344 cbp_tail = cbp;
1345 } else {
1346 cbp_head = cbp;
1347 cbp_tail = cbp;
2d21ac55
A
1348
1349 if ( (cbp_head->b_real_bp = real_bp) ) {
1350 cbp_head->b_flags |= B_NEED_IODONE;
1351 real_bp = (buf_t)NULL;
1352 }
1c79356b 1353 }
2d21ac55
A
1354 *(buf_t *)(&cbp->b_trans_head) = cbp_head;
1355
91447636 1356 trans_count++;
1c79356b
A
1357
1358 upl_offset += io_size;
1359 f_offset += io_size;
1360 size -= io_size;
91447636
A
1361 /*
1362 * keep track of how much of the original request
1363 * that we've actually completed... non_rounded_size
1364 * may go negative due to us rounding the request
1365 * to a page size multiple (i.e. size > non_rounded_size)
1366 */
1367 non_rounded_size -= io_size;
1c79356b 1368
91447636
A
1369 if (non_rounded_size <= 0) {
1370 /*
1371 * we've transferred all of the data in the original
1372 * request, but we were unable to complete the tail
1373 * of the last page because the file didn't have
1374 * an allocation to back that portion... this is ok.
1375 */
1376 size = 0;
1377 }
2d21ac55
A
1378 if (size == 0) {
1379 /*
1380 * we have no more I/O to issue, so go
1381 * finish the final transaction
1382 */
1383 need_EOT = TRUE;
1384 } else if ( ((flags & CL_DEV_MEMORY) || (upl_offset & PAGE_MASK) == 0) &&
1385 ((flags & CL_ASYNC) || trans_count > max_trans_count) ) {
1c79356b 1386 /*
2d21ac55
A
1387 * I/O directed to physically contiguous memory...
1388 * which doesn't have a requirement to make sure we 'fill' a page
1389 * or...
1c79356b
A
1390 * the current I/O we've prepared fully
1391 * completes the last page in this request
2d21ac55
A
1392 * and ...
1393 * it's either an ASYNC request or
9bccf70c 1394 * we've already accumulated more than 8 I/O's into
2d21ac55
A
1395 * this transaction so mark it as complete so that
1396 * it can finish asynchronously or via the cluster_complete_transaction
1397 * below if the request is synchronous
1c79356b 1398 */
2d21ac55
A
1399 need_EOT = TRUE;
1400 }
1401 if (need_EOT == TRUE)
1402 cluster_EOT(cbp_head, cbp_tail, size == 0 ? zero_offset : 0);
1c79356b 1403
2d21ac55
A
1404 if (flags & CL_THROTTLE)
1405 (void)vnode_waitforwrites(vp, async_throttle, 0, 0, "cluster_io");
1c79356b 1406
2d21ac55
A
1407 if ( !(io_flags & B_READ))
1408 vnode_startwrite(vp);
9bccf70c 1409
2d21ac55
A
1410 (void) VNOP_STRATEGY(cbp);
1411
1412 if (need_EOT == TRUE) {
1413 if ( !(flags & CL_ASYNC))
1414 cluster_complete_transaction(&cbp_head, callback_arg, &retval, flags, 1);
9bccf70c 1415
2d21ac55 1416 need_EOT = FALSE;
91447636 1417 trans_count = 0;
2d21ac55 1418 cbp_head = NULL;
1c79356b 1419 }
2d21ac55 1420 }
1c79356b 1421 if (error) {
0b4e3aa0
A
1422 int abort_size;
1423
b4c24cb9
A
1424 io_size = 0;
1425
2d21ac55
A
1426 if (cbp_head) {
1427 /*
1428 * first wait until all of the outstanding I/O
1429 * for this partial transaction has completed
1430 */
1431 cluster_wait_IO(cbp_head, (flags & CL_ASYNC));
0b4e3aa0 1432
2d21ac55
A
1433 /*
1434 * Rewind the upl offset to the beginning of the
1435 * transaction.
1436 */
1437 upl_offset = cbp_head->b_uploffset;
1438
1439 for (cbp = cbp_head; cbp;) {
1440 buf_t cbp_next;
1441
1442 size += cbp->b_bcount;
1443 io_size += cbp->b_bcount;
1444
1445 cbp_next = cbp->b_trans_next;
1446 free_io_buf(cbp);
1447 cbp = cbp_next;
1448 }
1c79356b 1449 }
b4c24cb9 1450 if (iostate) {
91447636
A
1451 int need_wakeup = 0;
1452
d7e50217
A
1453 /*
1454 * update the error condition for this stream
1455 * since we never really issued the io
1456 * just go ahead and adjust it back
1457 */
2d21ac55 1458 lck_mtx_lock_spin(cl_mtxp);
91447636 1459
d7e50217 1460 if (iostate->io_error == 0)
b4c24cb9 1461 iostate->io_error = error;
b4c24cb9
A
1462 iostate->io_issued -= io_size;
1463
1464 if (iostate->io_wanted) {
d7e50217
A
1465 /*
1466 * someone is waiting for the state of
1467 * this io stream to change
1468 */
b4c24cb9 1469 iostate->io_wanted = 0;
2d21ac55 1470 need_wakeup = 1;
b4c24cb9 1471 }
91447636
A
1472 lck_mtx_unlock(cl_mtxp);
1473
1474 if (need_wakeup)
1475 wakeup((caddr_t)&iostate->io_wanted);
b4c24cb9 1476 }
1c79356b 1477 if (flags & CL_COMMIT) {
2d21ac55 1478 int upl_flags;
1c79356b 1479
2d21ac55
A
1480 pg_offset = upl_offset & PAGE_MASK;
1481 abort_size = (upl_end_offset - upl_offset + PAGE_MASK) & ~PAGE_MASK;
1482
1483 upl_flags = cluster_ioerror(upl, upl_offset - pg_offset, abort_size, error, io_flags);
1484
1c79356b 1485 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 28)) | DBG_FUNC_NONE,
b0d623f7 1486 upl, upl_offset - pg_offset, abort_size, (error << 24) | upl_flags, 0);
1c79356b
A
1487 }
1488 if (retval == 0)
1489 retval = error;
2d21ac55
A
1490 } else if (cbp_head)
1491 panic("%s(): cbp_head is not NULL.\n", __FUNCTION__);
1492
1493 if (real_bp) {
1494 /*
1495 * can get here if we either encountered an error
1496 * or we completely zero-filled the request and
1497 * no I/O was issued
1498 */
1499 if (error) {
1500 real_bp->b_flags |= B_ERROR;
1501 real_bp->b_error = error;
1502 }
1503 buf_biodone(real_bp);
1c79356b 1504 }
2d21ac55 1505 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 22)) | DBG_FUNC_END, (int)f_offset, size, upl_offset, retval, 0);
1c79356b
A
1506
1507 return (retval);
1508}
1509
b0d623f7
A
1510#define reset_vector_run_state() \
1511 issueVectorUPL = vector_upl_offset = vector_upl_index = vector_upl_iosize = vector_upl_size = 0;
1512
1513static int
1514vector_cluster_io(vnode_t vp, upl_t vector_upl, vm_offset_t vector_upl_offset, off_t v_upl_uio_offset, int vector_upl_iosize,
1515 int io_flag, buf_t real_bp, struct clios *iostate, int (*callback)(buf_t, void *), void *callback_arg)
1516{
1517 vector_upl_set_pagelist(vector_upl);
1518
1519 if(io_flag & CL_READ) {
1520 if(vector_upl_offset == 0 && ((vector_upl_iosize & PAGE_MASK)==0))
1521 io_flag &= ~CL_PRESERVE; /*don't zero fill*/
1522 else
1523 io_flag |= CL_PRESERVE; /*zero fill*/
1524 }
1525 return (cluster_io(vp, vector_upl, vector_upl_offset, v_upl_uio_offset, vector_upl_iosize, io_flag, real_bp, iostate, callback, callback_arg));
1526
1527}
1c79356b
A
1528
1529static int
2d21ac55 1530cluster_read_prefetch(vnode_t vp, off_t f_offset, u_int size, off_t filesize, int (*callback)(buf_t, void *), void *callback_arg, int bflag)
1c79356b 1531{
55e303ae 1532 int pages_in_prefetch;
1c79356b
A
1533
1534 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 49)) | DBG_FUNC_START,
1535 (int)f_offset, size, (int)filesize, 0, 0);
1536
1537 if (f_offset >= filesize) {
1538 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 49)) | DBG_FUNC_END,
1539 (int)f_offset, 0, 0, 0, 0);
1540 return(0);
1541 }
9bccf70c
A
1542 if ((off_t)size > (filesize - f_offset))
1543 size = filesize - f_offset;
55e303ae 1544 pages_in_prefetch = (size + (PAGE_SIZE - 1)) / PAGE_SIZE;
1c79356b 1545
2d21ac55 1546 advisory_read_ext(vp, filesize, f_offset, size, callback, callback_arg, bflag);
1c79356b
A
1547
1548 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 49)) | DBG_FUNC_END,
55e303ae 1549 (int)f_offset + size, pages_in_prefetch, 0, 1, 0);
1c79356b 1550
55e303ae 1551 return (pages_in_prefetch);
1c79356b
A
1552}
1553
1554
1555
1556static void
2d21ac55
A
1557cluster_read_ahead(vnode_t vp, struct cl_extent *extent, off_t filesize, struct cl_readahead *rap, int (*callback)(buf_t, void *), void *callback_arg,
1558 int bflag)
1c79356b 1559{
91447636
A
1560 daddr64_t r_addr;
1561 off_t f_offset;
1562 int size_of_prefetch;
b0d623f7 1563 u_int max_prefetch;
91447636 1564
1c79356b
A
1565
1566 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 48)) | DBG_FUNC_START,
91447636 1567 (int)extent->b_addr, (int)extent->e_addr, (int)rap->cl_lastr, 0, 0);
1c79356b 1568
91447636 1569 if (extent->b_addr == rap->cl_lastr && extent->b_addr == extent->e_addr) {
1c79356b 1570 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 48)) | DBG_FUNC_END,
91447636 1571 rap->cl_ralen, (int)rap->cl_maxra, (int)rap->cl_lastr, 0, 0);
1c79356b
A
1572 return;
1573 }
2d21ac55 1574 if (rap->cl_lastr == -1 || (extent->b_addr != rap->cl_lastr && extent->b_addr != (rap->cl_lastr + 1))) {
91447636
A
1575 rap->cl_ralen = 0;
1576 rap->cl_maxra = 0;
1c79356b
A
1577
1578 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 48)) | DBG_FUNC_END,
91447636 1579 rap->cl_ralen, (int)rap->cl_maxra, (int)rap->cl_lastr, 1, 0);
1c79356b
A
1580
1581 return;
1582 }
b0d623f7 1583 max_prefetch = MAX_PREFETCH(vp, cluster_max_io_size(vp->v_mount, CL_READ));
cf7d32b8 1584
91447636 1585 if (extent->e_addr < rap->cl_maxra) {
cf7d32b8 1586 if ((rap->cl_maxra - extent->e_addr) > ((max_prefetch / PAGE_SIZE) / 4)) {
1c79356b
A
1587
1588 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 48)) | DBG_FUNC_END,
91447636 1589 rap->cl_ralen, (int)rap->cl_maxra, (int)rap->cl_lastr, 2, 0);
1c79356b
A
1590 return;
1591 }
1592 }
91447636
A
1593 r_addr = max(extent->e_addr, rap->cl_maxra) + 1;
1594 f_offset = (off_t)(r_addr * PAGE_SIZE_64);
1c79356b 1595
55e303ae
A
1596 size_of_prefetch = 0;
1597
1598 ubc_range_op(vp, f_offset, f_offset + PAGE_SIZE_64, UPL_ROP_PRESENT, &size_of_prefetch);
1599
1600 if (size_of_prefetch) {
1601 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 48)) | DBG_FUNC_END,
91447636 1602 rap->cl_ralen, (int)rap->cl_maxra, (int)rap->cl_lastr, 3, 0);
55e303ae
A
1603 return;
1604 }
9bccf70c 1605 if (f_offset < filesize) {
91447636 1606 daddr64_t read_size;
55e303ae 1607
cf7d32b8 1608 rap->cl_ralen = rap->cl_ralen ? min(max_prefetch / PAGE_SIZE, rap->cl_ralen << 1) : 1;
55e303ae 1609
91447636
A
1610 read_size = (extent->e_addr + 1) - extent->b_addr;
1611
1612 if (read_size > rap->cl_ralen) {
cf7d32b8
A
1613 if (read_size > max_prefetch / PAGE_SIZE)
1614 rap->cl_ralen = max_prefetch / PAGE_SIZE;
91447636
A
1615 else
1616 rap->cl_ralen = read_size;
1617 }
2d21ac55 1618 size_of_prefetch = cluster_read_prefetch(vp, f_offset, rap->cl_ralen * PAGE_SIZE, filesize, callback, callback_arg, bflag);
1c79356b 1619
9bccf70c 1620 if (size_of_prefetch)
91447636 1621 rap->cl_maxra = (r_addr + size_of_prefetch) - 1;
9bccf70c 1622 }
1c79356b 1623 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 48)) | DBG_FUNC_END,
91447636 1624 rap->cl_ralen, (int)rap->cl_maxra, (int)rap->cl_lastr, 4, 0);
1c79356b
A
1625}
1626
2d21ac55 1627
9bccf70c 1628int
b0d623f7 1629cluster_pageout(vnode_t vp, upl_t upl, upl_offset_t upl_offset, off_t f_offset,
91447636 1630 int size, off_t filesize, int flags)
2d21ac55
A
1631{
1632 return cluster_pageout_ext(vp, upl, upl_offset, f_offset, size, filesize, flags, NULL, NULL);
1633
1634}
1635
1636
1637int
b0d623f7 1638cluster_pageout_ext(vnode_t vp, upl_t upl, upl_offset_t upl_offset, off_t f_offset,
2d21ac55 1639 int size, off_t filesize, int flags, int (*callback)(buf_t, void *), void *callback_arg)
1c79356b
A
1640{
1641 int io_size;
55e303ae 1642 int rounded_size;
1c79356b 1643 off_t max_size;
55e303ae
A
1644 int local_flags;
1645
1646 if (vp->v_mount->mnt_kern_flag & MNTK_VIRTUALDEV)
1647 /*
1648 * if we know we're issuing this I/O to a virtual device (i.e. disk image)
1649 * then we don't want to enforce this throttle... if we do, we can
1650 * potentially deadlock since we're stalling the pageout thread at a time
1651 * when the disk image might need additional memory (which won't be available
1652 * if the pageout thread can't run)... instead we'll just depend on the throttle
1653 * that the pageout thread now has in place to deal with external files
1654 */
1655 local_flags = CL_PAGEOUT;
1656 else
1657 local_flags = CL_PAGEOUT | CL_THROTTLE;
1c79356b
A
1658
1659 if ((flags & UPL_IOSYNC) == 0)
1660 local_flags |= CL_ASYNC;
1661 if ((flags & UPL_NOCOMMIT) == 0)
1662 local_flags |= CL_COMMIT;
91447636
A
1663 if ((flags & UPL_KEEPCACHED))
1664 local_flags |= CL_KEEPCACHED;
1c79356b 1665
1c79356b
A
1666
1667 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 52)) | DBG_FUNC_NONE,
1668 (int)f_offset, size, (int)filesize, local_flags, 0);
1669
1670 /*
1671 * If they didn't specify any I/O, then we are done...
1672 * we can't issue an abort because we don't know how
1673 * big the upl really is
1674 */
1675 if (size <= 0)
1676 return (EINVAL);
1677
1678 if (vp->v_mount->mnt_flag & MNT_RDONLY) {
1679 if (local_flags & CL_COMMIT)
9bccf70c 1680 ubc_upl_abort_range(upl, upl_offset, size, UPL_ABORT_FREE_ON_EMPTY);
1c79356b
A
1681 return (EROFS);
1682 }
1683 /*
1684 * can't page-in from a negative offset
1685 * or if we're starting beyond the EOF
1686 * or if the file offset isn't page aligned
1687 * or the size requested isn't a multiple of PAGE_SIZE
1688 */
1689 if (f_offset < 0 || f_offset >= filesize ||
1690 (f_offset & PAGE_MASK_64) || (size & PAGE_MASK)) {
0b4e3aa0
A
1691 if (local_flags & CL_COMMIT)
1692 ubc_upl_abort_range(upl, upl_offset, size, UPL_ABORT_FREE_ON_EMPTY);
1c79356b
A
1693 return (EINVAL);
1694 }
1695 max_size = filesize - f_offset;
1696
1697 if (size < max_size)
1698 io_size = size;
1699 else
9bccf70c 1700 io_size = max_size;
1c79356b 1701
55e303ae 1702 rounded_size = (io_size + (PAGE_SIZE - 1)) & ~PAGE_MASK;
1c79356b 1703
55e303ae 1704 if (size > rounded_size) {
0b4e3aa0 1705 if (local_flags & CL_COMMIT)
55e303ae 1706 ubc_upl_abort_range(upl, upl_offset + rounded_size, size - rounded_size,
1c79356b
A
1707 UPL_ABORT_FREE_ON_EMPTY);
1708 }
91447636 1709 return (cluster_io(vp, upl, upl_offset, f_offset, io_size,
2d21ac55 1710 local_flags, (buf_t)NULL, (struct clios *)NULL, callback, callback_arg));
1c79356b
A
1711}
1712
2d21ac55 1713
9bccf70c 1714int
b0d623f7 1715cluster_pagein(vnode_t vp, upl_t upl, upl_offset_t upl_offset, off_t f_offset,
91447636 1716 int size, off_t filesize, int flags)
2d21ac55
A
1717{
1718 return cluster_pagein_ext(vp, upl, upl_offset, f_offset, size, filesize, flags, NULL, NULL);
1719}
1720
1721
1722int
b0d623f7 1723cluster_pagein_ext(vnode_t vp, upl_t upl, upl_offset_t upl_offset, off_t f_offset,
2d21ac55 1724 int size, off_t filesize, int flags, int (*callback)(buf_t, void *), void *callback_arg)
1c79356b
A
1725{
1726 u_int io_size;
9bccf70c 1727 int rounded_size;
1c79356b
A
1728 off_t max_size;
1729 int retval;
1730 int local_flags = 0;
1c79356b 1731
9bccf70c
A
1732 if (upl == NULL || size < 0)
1733 panic("cluster_pagein: NULL upl passed in");
1c79356b 1734
9bccf70c
A
1735 if ((flags & UPL_IOSYNC) == 0)
1736 local_flags |= CL_ASYNC;
1c79356b 1737 if ((flags & UPL_NOCOMMIT) == 0)
9bccf70c 1738 local_flags |= CL_COMMIT;
b0d623f7
A
1739 if (flags & UPL_IOSTREAMING)
1740 local_flags |= CL_IOSTREAMING;
9bccf70c 1741
1c79356b
A
1742
1743 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 56)) | DBG_FUNC_NONE,
1744 (int)f_offset, size, (int)filesize, local_flags, 0);
1745
1746 /*
1747 * can't page-in from a negative offset
1748 * or if we're starting beyond the EOF
1749 * or if the file offset isn't page aligned
1750 * or the size requested isn't a multiple of PAGE_SIZE
1751 */
1752 if (f_offset < 0 || f_offset >= filesize ||
9bccf70c
A
1753 (f_offset & PAGE_MASK_64) || (size & PAGE_MASK) || (upl_offset & PAGE_MASK)) {
1754 if (local_flags & CL_COMMIT)
1755 ubc_upl_abort_range(upl, upl_offset, size, UPL_ABORT_FREE_ON_EMPTY | UPL_ABORT_ERROR);
1c79356b
A
1756 return (EINVAL);
1757 }
1758 max_size = filesize - f_offset;
1759
1760 if (size < max_size)
1761 io_size = size;
1762 else
9bccf70c 1763 io_size = max_size;
1c79356b 1764
9bccf70c 1765 rounded_size = (io_size + (PAGE_SIZE - 1)) & ~PAGE_MASK;
1c79356b 1766
9bccf70c
A
1767 if (size > rounded_size && (local_flags & CL_COMMIT))
1768 ubc_upl_abort_range(upl, upl_offset + rounded_size,
55e303ae 1769 size - rounded_size, UPL_ABORT_FREE_ON_EMPTY | UPL_ABORT_ERROR);
9bccf70c 1770
91447636 1771 retval = cluster_io(vp, upl, upl_offset, f_offset, io_size,
2d21ac55 1772 local_flags | CL_READ | CL_PAGEIN, (buf_t)NULL, (struct clios *)NULL, callback, callback_arg);
1c79356b 1773
1c79356b
A
1774 return (retval);
1775}
1776
2d21ac55 1777
9bccf70c 1778int
91447636 1779cluster_bp(buf_t bp)
2d21ac55
A
1780{
1781 return cluster_bp_ext(bp, NULL, NULL);
1782}
1783
1784
1785int
1786cluster_bp_ext(buf_t bp, int (*callback)(buf_t, void *), void *callback_arg)
1c79356b
A
1787{
1788 off_t f_offset;
1789 int flags;
1790
9bccf70c 1791 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 19)) | DBG_FUNC_START,
b0d623f7 1792 bp, (int)bp->b_lblkno, bp->b_bcount, bp->b_flags, 0);
9bccf70c 1793
1c79356b 1794 if (bp->b_flags & B_READ)
9bccf70c 1795 flags = CL_ASYNC | CL_READ;
1c79356b 1796 else
9bccf70c 1797 flags = CL_ASYNC;
2d21ac55
A
1798 if (bp->b_flags & B_PASSIVE)
1799 flags |= CL_PASSIVE;
1c79356b
A
1800
1801 f_offset = ubc_blktooff(bp->b_vp, bp->b_lblkno);
1802
2d21ac55 1803 return (cluster_io(bp->b_vp, bp->b_upl, 0, f_offset, bp->b_bcount, flags, bp, (struct clios *)NULL, callback, callback_arg));
1c79356b
A
1804}
1805
2d21ac55
A
1806
1807
9bccf70c 1808int
91447636 1809cluster_write(vnode_t vp, struct uio *uio, off_t oldEOF, off_t newEOF, off_t headOff, off_t tailOff, int xflags)
1c79356b 1810{
2d21ac55
A
1811 return cluster_write_ext(vp, uio, oldEOF, newEOF, headOff, tailOff, xflags, NULL, NULL);
1812}
1813
1814
1815int
1816cluster_write_ext(vnode_t vp, struct uio *uio, off_t oldEOF, off_t newEOF, off_t headOff, off_t tailOff,
1817 int xflags, int (*callback)(buf_t, void *), void *callback_arg)
1818{
1819 user_ssize_t cur_resid;
1820 int retval = 0;
1821 int flags;
1822 int zflags;
1823 int bflag;
1824 int write_type = IO_COPY;
1825 u_int32_t write_length;
1c79356b 1826
91447636
A
1827 flags = xflags;
1828
2d21ac55 1829 if (flags & IO_PASSIVE)
b0d623f7 1830 bflag = CL_PASSIVE;
2d21ac55 1831 else
b0d623f7 1832 bflag = 0;
2d21ac55 1833
91447636
A
1834 if (vp->v_flag & VNOCACHE_DATA)
1835 flags |= IO_NOCACHE;
1836
2d21ac55 1837 if (uio == NULL) {
91447636 1838 /*
2d21ac55
A
1839 * no user data...
1840 * this call is being made to zero-fill some range in the file
91447636 1841 */
2d21ac55 1842 retval = cluster_write_copy(vp, NULL, (u_int32_t)0, oldEOF, newEOF, headOff, tailOff, flags, callback, callback_arg);
91447636 1843
2d21ac55 1844 return(retval);
91447636 1845 }
2d21ac55
A
1846 /*
1847 * do a write through the cache if one of the following is true....
1848 * NOCACHE is not true and
1849 * the uio request doesn't target USERSPACE
1850 * otherwise, find out if we want the direct or contig variant for
1851 * the first vector in the uio request
1852 */
1853 if ( (flags & IO_NOCACHE) && UIO_SEG_IS_USER_SPACE(uio->uio_segflg) )
1854 retval = cluster_io_type(uio, &write_type, &write_length, MIN_DIRECT_WRITE_SIZE);
1855
1856 if ( (flags & (IO_TAILZEROFILL | IO_HEADZEROFILL)) && write_type == IO_DIRECT)
1857 /*
1858 * must go through the cached variant in this case
0b4e3aa0 1859 */
2d21ac55 1860 write_type = IO_COPY;
0b4e3aa0 1861
2d21ac55
A
1862 while ((cur_resid = uio_resid(uio)) && uio->uio_offset < newEOF && retval == 0) {
1863
1864 switch (write_type) {
91447636 1865
2d21ac55 1866 case IO_COPY:
91447636 1867 /*
2d21ac55
A
1868 * make sure the uio_resid isn't too big...
1869 * internally, we want to handle all of the I/O in
1870 * chunk sizes that fit in a 32 bit int
91447636 1871 */
2d21ac55 1872 if (cur_resid > (user_ssize_t)(MAX_IO_REQUEST_SIZE)) {
91447636 1873 /*
2d21ac55
A
1874 * we're going to have to call cluster_write_copy
1875 * more than once...
1876 *
1877 * only want the last call to cluster_write_copy to
1878 * have the IO_TAILZEROFILL flag set and only the
1879 * first call should have IO_HEADZEROFILL
91447636 1880 */
2d21ac55
A
1881 zflags = flags & ~IO_TAILZEROFILL;
1882 flags &= ~IO_HEADZEROFILL;
91447636 1883
2d21ac55
A
1884 write_length = MAX_IO_REQUEST_SIZE;
1885 } else {
1886 /*
1887 * last call to cluster_write_copy
91447636 1888 */
2d21ac55
A
1889 zflags = flags;
1890
1891 write_length = (u_int32_t)cur_resid;
1892 }
1893 retval = cluster_write_copy(vp, uio, write_length, oldEOF, newEOF, headOff, tailOff, zflags, callback, callback_arg);
1894 break;
91447636 1895
2d21ac55
A
1896 case IO_CONTIG:
1897 zflags = flags & ~(IO_TAILZEROFILL | IO_HEADZEROFILL);
91447636 1898
2d21ac55
A
1899 if (flags & IO_HEADZEROFILL) {
1900 /*
1901 * only do this once per request
91447636 1902 */
2d21ac55 1903 flags &= ~IO_HEADZEROFILL;
91447636 1904
2d21ac55
A
1905 retval = cluster_write_copy(vp, (struct uio *)0, (u_int32_t)0, (off_t)0, uio->uio_offset,
1906 headOff, (off_t)0, zflags | IO_HEADZEROFILL | IO_SYNC, callback, callback_arg);
1907 if (retval)
1908 break;
91447636 1909 }
2d21ac55
A
1910 retval = cluster_write_contig(vp, uio, newEOF, &write_type, &write_length, callback, callback_arg, bflag);
1911
1912 if (retval == 0 && (flags & IO_TAILZEROFILL) && uio_resid(uio) == 0) {
1913 /*
1914 * we're done with the data from the user specified buffer(s)
1915 * and we've been requested to zero fill at the tail
1916 * treat this as an IO_HEADZEROFILL which doesn't require a uio
1917 * by rearranging the args and passing in IO_HEADZEROFILL
91447636 1918 */
2d21ac55
A
1919 retval = cluster_write_copy(vp, (struct uio *)0, (u_int32_t)0, (off_t)0, tailOff, uio->uio_offset,
1920 (off_t)0, zflags | IO_HEADZEROFILL | IO_SYNC, callback, callback_arg);
1921 }
1922 break;
91447636 1923
2d21ac55
A
1924 case IO_DIRECT:
1925 /*
1926 * cluster_write_direct is never called with IO_TAILZEROFILL || IO_HEADZEROFILL
1927 */
1928 retval = cluster_write_direct(vp, uio, oldEOF, newEOF, &write_type, &write_length, flags, callback, callback_arg);
1929 break;
91447636 1930
2d21ac55
A
1931 case IO_UNKNOWN:
1932 retval = cluster_io_type(uio, &write_type, &write_length, MIN_DIRECT_WRITE_SIZE);
1933 break;
1934 }
b0d623f7
A
1935 /*
1936 * in case we end up calling cluster_write_copy (from cluster_write_direct)
1937 * multiple times to service a multi-vector request that is not aligned properly
1938 * we need to update the oldEOF so that we
1939 * don't zero-fill the head of a page if we've successfully written
1940 * data to that area... 'cluster_write_copy' will zero-fill the head of a
1941 * page that is beyond the oldEOF if the write is unaligned... we only
1942 * want that to happen for the very first page of the cluster_write,
1943 * NOT the first page of each vector making up a multi-vector write.
1944 */
1945 if (uio->uio_offset > oldEOF)
1946 oldEOF = uio->uio_offset;
2d21ac55
A
1947 }
1948 return (retval);
1c79356b
A
1949}
1950
b4c24cb9 1951
9bccf70c 1952static int
2d21ac55
A
1953cluster_write_direct(vnode_t vp, struct uio *uio, off_t oldEOF, off_t newEOF, int *write_type, u_int32_t *write_length,
1954 int flags, int (*callback)(buf_t, void *), void *callback_arg)
1c79356b
A
1955{
1956 upl_t upl;
1957 upl_page_info_t *pl;
1c79356b 1958 vm_offset_t upl_offset;
b0d623f7 1959 vm_offset_t vector_upl_offset = 0;
2d21ac55
A
1960 u_int32_t io_req_size;
1961 u_int32_t offset_in_file;
1962 u_int32_t offset_in_iovbase;
b0d623f7
A
1963 u_int32_t io_size;
1964 int io_flag = 0;
1965 upl_size_t upl_size, vector_upl_size = 0;
2d21ac55
A
1966 vm_size_t upl_needed_size;
1967 mach_msg_type_number_t pages_in_pl;
1c79356b
A
1968 int upl_flags;
1969 kern_return_t kret;
2d21ac55 1970 mach_msg_type_number_t i;
1c79356b 1971 int force_data_sync;
2d21ac55
A
1972 int retval = 0;
1973 int first_IO = 1;
d7e50217 1974 struct clios iostate;
2d21ac55
A
1975 user_addr_t iov_base;
1976 u_int32_t mem_alignment_mask;
1977 u_int32_t devblocksize;
b0d623f7 1978 u_int32_t max_upl_size;
cf7d32b8 1979
b0d623f7
A
1980 u_int32_t vector_upl_iosize = 0;
1981 int issueVectorUPL = 0,useVectorUPL = (uio->uio_iovcnt > 1);
1982 off_t v_upl_uio_offset = 0;
1983 int vector_upl_index=0;
1984 upl_t vector_upl = NULL;
cf7d32b8 1985
1c79356b
A
1986
1987 /*
1988 * When we enter this routine, we know
1c79356b
A
1989 * -- the resid will not exceed iov_len
1990 */
2d21ac55
A
1991 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 75)) | DBG_FUNC_START,
1992 (int)uio->uio_offset, *write_length, (int)newEOF, 0, 0);
91447636 1993
b0d623f7
A
1994 max_upl_size = cluster_max_io_size(vp->v_mount, CL_WRITE);
1995
1996 io_flag = CL_ASYNC | CL_PRESERVE | CL_COMMIT | CL_THROTTLE | CL_DIRECT_IO;
1997
1998 if (flags & IO_PASSIVE)
1999 io_flag |= CL_PASSIVE;
2000
d7e50217
A
2001 iostate.io_completed = 0;
2002 iostate.io_issued = 0;
2003 iostate.io_error = 0;
2004 iostate.io_wanted = 0;
2005
2d21ac55
A
2006 mem_alignment_mask = (u_int32_t)vp->v_mount->mnt_alignmentmask;
2007 devblocksize = (u_int32_t)vp->v_mount->mnt_devblocksize;
2008
2009 if (devblocksize == 1) {
2010 /*
2011 * the AFP client advertises a devblocksize of 1
2012 * however, its BLOCKMAP routine maps to physical
2013 * blocks that are PAGE_SIZE in size...
2014 * therefore we can't ask for I/Os that aren't page aligned
2015 * or aren't multiples of PAGE_SIZE in size
2016 * by setting devblocksize to PAGE_SIZE, we re-instate
2017 * the old behavior we had before the mem_alignment_mask
2018 * changes went in...
2019 */
2020 devblocksize = PAGE_SIZE;
2021 }
2022
2023next_dwrite:
2024 io_req_size = *write_length;
2025 iov_base = uio_curriovbase(uio);
cc9f6e38 2026
2d21ac55
A
2027 offset_in_file = (u_int32_t)uio->uio_offset & PAGE_MASK;
2028 offset_in_iovbase = (u_int32_t)iov_base & mem_alignment_mask;
1c79356b 2029
2d21ac55
A
2030 if (offset_in_file || offset_in_iovbase) {
2031 /*
2032 * one of the 2 important offsets is misaligned
2033 * so fire an I/O through the cache for this entire vector
2034 */
2035 goto wait_for_dwrites;
2036 }
2037 if (iov_base & (devblocksize - 1)) {
2038 /*
2039 * the offset in memory must be on a device block boundary
2040 * so that we can guarantee that we can generate an
2041 * I/O that ends on a page boundary in cluster_io
2042 */
2043 goto wait_for_dwrites;
2044 }
1c79356b 2045
2d21ac55
A
2046 while (io_req_size >= PAGE_SIZE && uio->uio_offset < newEOF && retval == 0) {
2047
2048 if (first_IO) {
2049 cluster_syncup(vp, newEOF, callback, callback_arg);
2050 first_IO = 0;
2051 }
2052 io_size = io_req_size & ~PAGE_MASK;
cc9f6e38
A
2053 iov_base = uio_curriovbase(uio);
2054
cf7d32b8
A
2055 if (io_size > max_upl_size)
2056 io_size = max_upl_size;
2d21ac55 2057
b0d623f7
A
2058 if(useVectorUPL && (iov_base & PAGE_MASK)) {
2059 /*
2060 * We have an iov_base that's not page-aligned.
2061 * Issue all I/O's that have been collected within
2062 * this Vectored UPL.
2063 */
2064 if(vector_upl_index) {
2065 retval = vector_cluster_io(vp, vector_upl, vector_upl_offset, v_upl_uio_offset, vector_upl_iosize, io_flag, (buf_t)NULL, &iostate, callback, callback_arg);
2066 reset_vector_run_state();
2067 }
2068
2069 /*
2070 * After this point, if we are using the Vector UPL path and the base is
2071 * not page-aligned then the UPL with that base will be the first in the vector UPL.
2072 */
2073 }
2074
2d21ac55 2075 upl_offset = (vm_offset_t)((u_int32_t)iov_base & PAGE_MASK);
d7e50217
A
2076 upl_needed_size = (upl_offset + io_size + (PAGE_SIZE -1)) & ~PAGE_MASK;
2077
2078 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 76)) | DBG_FUNC_START,
cc9f6e38 2079 (int)upl_offset, upl_needed_size, (int)iov_base, io_size, 0);
d7e50217
A
2080
2081 for (force_data_sync = 0; force_data_sync < 3; force_data_sync++) {
2082 pages_in_pl = 0;
2083 upl_size = upl_needed_size;
2084 upl_flags = UPL_FILE_IO | UPL_COPYOUT_FROM | UPL_NO_SYNC |
55e303ae 2085 UPL_CLEAN_IN_PLACE | UPL_SET_INTERNAL | UPL_SET_LITE | UPL_SET_IO_WIRE;
d7e50217
A
2086
2087 kret = vm_map_get_upl(current_map(),
cc9f6e38 2088 (vm_map_offset_t)(iov_base & ~((user_addr_t)PAGE_MASK)),
d7e50217
A
2089 &upl_size,
2090 &upl,
2091 NULL,
2092 &pages_in_pl,
2093 &upl_flags,
2094 force_data_sync);
2095
2096 if (kret != KERN_SUCCESS) {
2097 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 76)) | DBG_FUNC_END,
2098 0, 0, 0, kret, 0);
d7e50217 2099 /*
2d21ac55 2100 * failed to get pagelist
d7e50217
A
2101 *
2102 * we may have already spun some portion of this request
2103 * off as async requests... we need to wait for the I/O
2104 * to complete before returning
2105 */
2d21ac55 2106 goto wait_for_dwrites;
d7e50217
A
2107 }
2108 pl = UPL_GET_INTERNAL_PAGE_LIST(upl);
2109 pages_in_pl = upl_size / PAGE_SIZE;
1c79356b 2110
d7e50217
A
2111 for (i = 0; i < pages_in_pl; i++) {
2112 if (!upl_valid_page(pl, i))
2113 break;
2114 }
2115 if (i == pages_in_pl)
2116 break;
1c79356b 2117
d7e50217
A
2118 /*
2119 * didn't get all the pages back that we
2120 * needed... release this upl and try again
2121 */
2d21ac55 2122 ubc_upl_abort(upl, 0);
1c79356b 2123 }
d7e50217
A
2124 if (force_data_sync >= 3) {
2125 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 76)) | DBG_FUNC_END,
2126 i, pages_in_pl, upl_size, kret, 0);
d7e50217
A
2127 /*
2128 * for some reason, we couldn't acquire a hold on all
2129 * the pages needed in the user's address space
2130 *
2131 * we may have already spun some portion of this request
2132 * off as async requests... we need to wait for the I/O
2133 * to complete before returning
2134 */
2d21ac55 2135 goto wait_for_dwrites;
1c79356b 2136 }
0b4e3aa0 2137
d7e50217
A
2138 /*
2139 * Consider the possibility that upl_size wasn't satisfied.
2140 */
2d21ac55
A
2141 if (upl_size < upl_needed_size) {
2142 if (upl_size && upl_offset == 0)
2143 io_size = upl_size;
2144 else
2145 io_size = 0;
2146 }
d7e50217 2147 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 76)) | DBG_FUNC_END,
cc9f6e38 2148 (int)upl_offset, upl_size, (int)iov_base, io_size, 0);
1c79356b 2149
d7e50217 2150 if (io_size == 0) {
2d21ac55 2151 ubc_upl_abort(upl, 0);
d7e50217
A
2152 /*
2153 * we may have already spun some portion of this request
2154 * off as async requests... we need to wait for the I/O
2155 * to complete before returning
2156 */
2d21ac55 2157 goto wait_for_dwrites;
d7e50217 2158 }
b0d623f7
A
2159
2160 if(useVectorUPL) {
2161 vm_offset_t end_off = ((iov_base + io_size) & PAGE_MASK);
2162 if(end_off)
2163 issueVectorUPL = 1;
2164 /*
2165 * After this point, if we are using a vector UPL, then
2166 * either all the UPL elements end on a page boundary OR
2167 * this UPL is the last element because it does not end
2168 * on a page boundary.
2169 */
2170 }
2d21ac55 2171
d7e50217
A
2172 /*
2173 * Now look for pages already in the cache
2174 * and throw them away.
55e303ae
A
2175 * uio->uio_offset is page aligned within the file
2176 * io_size is a multiple of PAGE_SIZE
d7e50217 2177 */
55e303ae 2178 ubc_range_op(vp, uio->uio_offset, uio->uio_offset + io_size, UPL_ROP_DUMP, NULL);
1c79356b 2179
d7e50217
A
2180 /*
2181 * we want push out these writes asynchronously so that we can overlap
2182 * the preparation of the next I/O
2183 * if there are already too many outstanding writes
2184 * wait until some complete before issuing the next
2185 */
b0d623f7 2186 if (iostate.io_issued > iostate.io_completed) {
91447636 2187
b0d623f7 2188 lck_mtx_lock(cl_mtxp);
cf7d32b8 2189
b0d623f7 2190 while ((iostate.io_issued - iostate.io_completed) > (max_upl_size * IO_SCALE(vp, 2))) {
cf7d32b8 2191
b0d623f7
A
2192 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 95)) | DBG_FUNC_START,
2193 iostate.io_issued, iostate.io_completed, max_upl_size * IO_SCALE(vp, 2), 0, 0);
cf7d32b8 2194
b0d623f7
A
2195 iostate.io_wanted = 1;
2196 msleep((caddr_t)&iostate.io_wanted, cl_mtxp, PRIBIO + 1, "cluster_write_direct", NULL);
91447636 2197
b0d623f7
A
2198 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 95)) | DBG_FUNC_END,
2199 iostate.io_issued, iostate.io_completed, max_upl_size * IO_SCALE(vp, 2), 0, 0);
2200 }
2201 lck_mtx_unlock(cl_mtxp);
2202 }
d7e50217
A
2203 if (iostate.io_error) {
2204 /*
2205 * one of the earlier writes we issued ran into a hard error
2206 * don't issue any more writes, cleanup the UPL
2207 * that was just created but not used, then
2208 * go wait for all writes that are part of this stream
2209 * to complete before returning the error to the caller
2210 */
2d21ac55 2211 ubc_upl_abort(upl, 0);
1c79356b 2212
2d21ac55 2213 goto wait_for_dwrites;
d7e50217 2214 }
1c79356b 2215
d7e50217
A
2216 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 77)) | DBG_FUNC_START,
2217 (int)upl_offset, (int)uio->uio_offset, io_size, io_flag, 0);
1c79356b 2218
b0d623f7
A
2219 if(!useVectorUPL)
2220 retval = cluster_io(vp, upl, upl_offset, uio->uio_offset,
2d21ac55 2221 io_size, io_flag, (buf_t)NULL, &iostate, callback, callback_arg);
7b1edb79 2222
b0d623f7
A
2223 else {
2224 if(!vector_upl_index) {
2225 vector_upl = vector_upl_create(upl_offset);
2226 v_upl_uio_offset = uio->uio_offset;
2227 vector_upl_offset = upl_offset;
2228 }
2229
2230 vector_upl_set_subupl(vector_upl,upl,upl_size);
2231 vector_upl_set_iostate(vector_upl, upl, vector_upl_size, upl_size);
2232 vector_upl_index++;
2233 vector_upl_iosize += io_size;
2234 vector_upl_size += upl_size;
2235
2236 if(issueVectorUPL || vector_upl_index == MAX_VECTOR_UPL_ELEMENTS || vector_upl_size >= MAX_VECTOR_UPL_SIZE) {
2237 retval = vector_cluster_io(vp, vector_upl, vector_upl_offset, v_upl_uio_offset, vector_upl_iosize, io_flag, (buf_t)NULL, &iostate, callback, callback_arg);
2238 reset_vector_run_state();
2239 }
2240 }
2241
2d21ac55
A
2242 /*
2243 * update the uio structure to
2244 * reflect the I/O that we just issued
2245 */
cc9f6e38 2246 uio_update(uio, (user_size_t)io_size);
1c79356b 2247
b0d623f7
A
2248 /*
2249 * in case we end up calling through to cluster_write_copy to finish
2250 * the tail of this request, we need to update the oldEOF so that we
2251 * don't zero-fill the head of a page if we've successfully written
2252 * data to that area... 'cluster_write_copy' will zero-fill the head of a
2253 * page that is beyond the oldEOF if the write is unaligned... we only
2254 * want that to happen for the very first page of the cluster_write,
2255 * NOT the first page of each vector making up a multi-vector write.
2256 */
2257 if (uio->uio_offset > oldEOF)
2258 oldEOF = uio->uio_offset;
2259
2d21ac55
A
2260 io_req_size -= io_size;
2261
d7e50217 2262 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 77)) | DBG_FUNC_END,
2d21ac55 2263 (int)upl_offset, (int)uio->uio_offset, io_req_size, retval, 0);
1c79356b
A
2264
2265 } /* end while */
2266
2d21ac55 2267 if (retval == 0 && iostate.io_error == 0 && io_req_size == 0) {
91447636 2268
2d21ac55
A
2269 retval = cluster_io_type(uio, write_type, write_length, MIN_DIRECT_WRITE_SIZE);
2270
2271 if (retval == 0 && *write_type == IO_DIRECT) {
2272
2273 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 75)) | DBG_FUNC_NONE,
2274 (int)uio->uio_offset, *write_length, (int)newEOF, 0, 0);
2275
2276 goto next_dwrite;
2277 }
2278 }
2279
2280wait_for_dwrites:
b0d623f7
A
2281
2282 if(retval == 0 && iostate.io_error == 0 && useVectorUPL && vector_upl_index) {
2283 retval = vector_cluster_io(vp, vector_upl, vector_upl_offset, v_upl_uio_offset, vector_upl_iosize, io_flag, (buf_t)NULL, &iostate, callback, callback_arg);
2284 reset_vector_run_state();
2285 }
2286
2287 if (iostate.io_issued > iostate.io_completed) {
2d21ac55
A
2288 /*
2289 * make sure all async writes issued as part of this stream
2290 * have completed before we return
2291 */
2292 lck_mtx_lock(cl_mtxp);
91447636 2293
2d21ac55 2294 while (iostate.io_issued != iostate.io_completed) {
cf7d32b8 2295 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 95)) | DBG_FUNC_START,
b0d623f7 2296 iostate.io_issued, iostate.io_completed, 0, 0, 0);
cf7d32b8 2297
2d21ac55
A
2298 iostate.io_wanted = 1;
2299 msleep((caddr_t)&iostate.io_wanted, cl_mtxp, PRIBIO + 1, "cluster_write_direct", NULL);
cf7d32b8 2300
b0d623f7
A
2301 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 95)) | DBG_FUNC_END,
2302 iostate.io_issued, iostate.io_completed, 0, 0, 0);
2d21ac55
A
2303 }
2304 lck_mtx_unlock(cl_mtxp);
2305 }
d7e50217 2306 if (iostate.io_error)
2d21ac55
A
2307 retval = iostate.io_error;
2308
2309 if (io_req_size && retval == 0) {
2310 /*
2311 * we couldn't handle the tail of this request in DIRECT mode
2312 * so fire it through the copy path
2313 *
2314 * note that flags will never have IO_HEADZEROFILL or IO_TAILZEROFILL set
2315 * so we can just pass 0 in for the headOff and tailOff
2316 */
b0d623f7
A
2317 if (uio->uio_offset > oldEOF)
2318 oldEOF = uio->uio_offset;
2319
2d21ac55 2320 retval = cluster_write_copy(vp, uio, io_req_size, oldEOF, newEOF, (off_t)0, (off_t)0, flags, callback, callback_arg);
1c79356b 2321
2d21ac55
A
2322 *write_type = IO_UNKNOWN;
2323 }
1c79356b 2324 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 75)) | DBG_FUNC_END,
2d21ac55 2325 (int)uio->uio_offset, io_req_size, retval, 4, 0);
1c79356b 2326
2d21ac55 2327 return (retval);
1c79356b
A
2328}
2329
b4c24cb9 2330
9bccf70c 2331static int
2d21ac55
A
2332cluster_write_contig(vnode_t vp, struct uio *uio, off_t newEOF, int *write_type, u_int32_t *write_length,
2333 int (*callback)(buf_t, void *), void *callback_arg, int bflag)
0b4e3aa0 2334{
b4c24cb9 2335 upl_page_info_t *pl;
2d21ac55
A
2336 addr64_t src_paddr = 0;
2337 upl_t upl[MAX_VECTS];
0b4e3aa0 2338 vm_offset_t upl_offset;
2d21ac55
A
2339 u_int32_t tail_size = 0;
2340 u_int32_t io_size;
2341 u_int32_t xsize;
b0d623f7 2342 upl_size_t upl_size;
2d21ac55
A
2343 vm_size_t upl_needed_size;
2344 mach_msg_type_number_t pages_in_pl;
0b4e3aa0
A
2345 int upl_flags;
2346 kern_return_t kret;
2d21ac55 2347 struct clios iostate;
0b4e3aa0 2348 int error = 0;
2d21ac55
A
2349 int cur_upl = 0;
2350 int num_upl = 0;
2351 int n;
cc9f6e38 2352 user_addr_t iov_base;
2d21ac55
A
2353 u_int32_t devblocksize;
2354 u_int32_t mem_alignment_mask;
0b4e3aa0
A
2355
2356 /*
2357 * When we enter this routine, we know
2d21ac55
A
2358 * -- the io_req_size will not exceed iov_len
2359 * -- the target address is physically contiguous
0b4e3aa0 2360 */
2d21ac55 2361 cluster_syncup(vp, newEOF, callback, callback_arg);
0b4e3aa0 2362
2d21ac55
A
2363 devblocksize = (u_int32_t)vp->v_mount->mnt_devblocksize;
2364 mem_alignment_mask = (u_int32_t)vp->v_mount->mnt_alignmentmask;
91447636 2365
2d21ac55
A
2366 iostate.io_completed = 0;
2367 iostate.io_issued = 0;
2368 iostate.io_error = 0;
2369 iostate.io_wanted = 0;
2370
2371next_cwrite:
2372 io_size = *write_length;
91447636 2373
cc9f6e38
A
2374 iov_base = uio_curriovbase(uio);
2375
2d21ac55 2376 upl_offset = (vm_offset_t)((u_int32_t)iov_base & PAGE_MASK);
0b4e3aa0
A
2377 upl_needed_size = upl_offset + io_size;
2378
2379 pages_in_pl = 0;
2380 upl_size = upl_needed_size;
9bccf70c 2381 upl_flags = UPL_FILE_IO | UPL_COPYOUT_FROM | UPL_NO_SYNC |
55e303ae 2382 UPL_CLEAN_IN_PLACE | UPL_SET_INTERNAL | UPL_SET_LITE | UPL_SET_IO_WIRE;
0b4e3aa0
A
2383
2384 kret = vm_map_get_upl(current_map(),
cc9f6e38 2385 (vm_map_offset_t)(iov_base & ~((user_addr_t)PAGE_MASK)),
2d21ac55 2386 &upl_size, &upl[cur_upl], NULL, &pages_in_pl, &upl_flags, 0);
0b4e3aa0 2387
b4c24cb9
A
2388 if (kret != KERN_SUCCESS) {
2389 /*
2d21ac55 2390 * failed to get pagelist
b4c24cb9 2391 */
2d21ac55
A
2392 error = EINVAL;
2393 goto wait_for_cwrites;
b4c24cb9 2394 }
2d21ac55
A
2395 num_upl++;
2396
0b4e3aa0
A
2397 /*
2398 * Consider the possibility that upl_size wasn't satisfied.
0b4e3aa0 2399 */
b4c24cb9 2400 if (upl_size < upl_needed_size) {
2d21ac55
A
2401 /*
2402 * This is a failure in the physical memory case.
2403 */
2404 error = EINVAL;
2405 goto wait_for_cwrites;
b4c24cb9 2406 }
2d21ac55 2407 pl = ubc_upl_pageinfo(upl[cur_upl]);
0b4e3aa0 2408
cc9f6e38 2409 src_paddr = ((addr64_t)upl_phys_page(pl, 0) << 12) + (addr64_t)upl_offset;
0b4e3aa0 2410
b4c24cb9 2411 while (((uio->uio_offset & (devblocksize - 1)) || io_size < devblocksize) && io_size) {
2d21ac55 2412 u_int32_t head_size;
0b4e3aa0 2413
2d21ac55 2414 head_size = devblocksize - (u_int32_t)(uio->uio_offset & (devblocksize - 1));
0b4e3aa0 2415
b4c24cb9
A
2416 if (head_size > io_size)
2417 head_size = io_size;
2418
2d21ac55 2419 error = cluster_align_phys_io(vp, uio, src_paddr, head_size, 0, callback, callback_arg);
b4c24cb9 2420
2d21ac55
A
2421 if (error)
2422 goto wait_for_cwrites;
b4c24cb9 2423
b4c24cb9
A
2424 upl_offset += head_size;
2425 src_paddr += head_size;
2426 io_size -= head_size;
2d21ac55
A
2427
2428 iov_base += head_size;
2429 }
2430 if ((u_int32_t)iov_base & mem_alignment_mask) {
2431 /*
2432 * request doesn't set up on a memory boundary
2433 * the underlying DMA engine can handle...
2434 * return an error instead of going through
2435 * the slow copy path since the intent of this
2436 * path is direct I/O from device memory
2437 */
2438 error = EINVAL;
2439 goto wait_for_cwrites;
0b4e3aa0 2440 }
2d21ac55 2441
b4c24cb9
A
2442 tail_size = io_size & (devblocksize - 1);
2443 io_size -= tail_size;
2444
2d21ac55
A
2445 while (io_size && error == 0) {
2446
2447 if (io_size > MAX_IO_CONTIG_SIZE)
2448 xsize = MAX_IO_CONTIG_SIZE;
2449 else
2450 xsize = io_size;
2451 /*
2452 * request asynchronously so that we can overlap
2453 * the preparation of the next I/O... we'll do
2454 * the commit after all the I/O has completed
2455 * since its all issued against the same UPL
2456 * if there are already too many outstanding writes
2457 * wait until some have completed before issuing the next
b4c24cb9 2458 */
b0d623f7 2459 if (iostate.io_issued > iostate.io_completed) {
2d21ac55
A
2460 lck_mtx_lock(cl_mtxp);
2461
b0d623f7 2462 while ((iostate.io_issued - iostate.io_completed) > (MAX_IO_CONTIG_SIZE * IO_SCALE(vp, 2))) {
cf7d32b8 2463
b0d623f7
A
2464 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 95)) | DBG_FUNC_START,
2465 iostate.io_issued, iostate.io_completed, MAX_IO_CONTIG_SIZE * IO_SCALE(vp, 2), 0, 0);
cf7d32b8 2466
2d21ac55
A
2467 iostate.io_wanted = 1;
2468 msleep((caddr_t)&iostate.io_wanted, cl_mtxp, PRIBIO + 1, "cluster_write_contig", NULL);
cf7d32b8 2469
b0d623f7
A
2470 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 95)) | DBG_FUNC_END,
2471 iostate.io_issued, iostate.io_completed, MAX_IO_CONTIG_SIZE * IO_SCALE(vp, 2), 0, 0);
2d21ac55
A
2472 }
2473 lck_mtx_unlock(cl_mtxp);
2474 }
2475 if (iostate.io_error) {
2476 /*
2477 * one of the earlier writes we issued ran into a hard error
2478 * don't issue any more writes...
2479 * go wait for all writes that are part of this stream
2480 * to complete before returning the error to the caller
2481 */
2482 goto wait_for_cwrites;
2483 }
b4c24cb9 2484 /*
2d21ac55 2485 * issue an asynchronous write to cluster_io
b4c24cb9 2486 */
2d21ac55
A
2487 error = cluster_io(vp, upl[cur_upl], upl_offset, uio->uio_offset,
2488 xsize, CL_DEV_MEMORY | CL_ASYNC | bflag, (buf_t)NULL, (struct clios *)&iostate, callback, callback_arg);
cc9f6e38 2489
2d21ac55
A
2490 if (error == 0) {
2491 /*
2492 * The cluster_io write completed successfully,
2493 * update the uio structure
2494 */
2495 uio_update(uio, (user_size_t)xsize);
b4c24cb9 2496
2d21ac55
A
2497 upl_offset += xsize;
2498 src_paddr += xsize;
2499 io_size -= xsize;
2500 }
b4c24cb9 2501 }
cf7d32b8 2502 if (error == 0 && iostate.io_error == 0 && tail_size == 0 && num_upl < MAX_VECTS) {
2d21ac55
A
2503
2504 error = cluster_io_type(uio, write_type, write_length, 0);
2505
2506 if (error == 0 && *write_type == IO_CONTIG) {
2507 cur_upl++;
2508 goto next_cwrite;
2509 }
2510 } else
2511 *write_type = IO_UNKNOWN;
2512
2513wait_for_cwrites:
b4c24cb9 2514 /*
2d21ac55
A
2515 * make sure all async writes that are part of this stream
2516 * have completed before we proceed
2517 */
b0d623f7
A
2518 if (iostate.io_issued > iostate.io_completed) {
2519
2520 lck_mtx_lock(cl_mtxp);
cf7d32b8 2521
b0d623f7
A
2522 while (iostate.io_issued != iostate.io_completed) {
2523 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 95)) | DBG_FUNC_START,
2524 iostate.io_issued, iostate.io_completed, 0, 0, 0);
cf7d32b8 2525
b0d623f7
A
2526 iostate.io_wanted = 1;
2527 msleep((caddr_t)&iostate.io_wanted, cl_mtxp, PRIBIO + 1, "cluster_write_contig", NULL);
2d21ac55 2528
b0d623f7
A
2529 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 95)) | DBG_FUNC_END,
2530 iostate.io_issued, iostate.io_completed, 0, 0, 0);
2531 }
2532 lck_mtx_unlock(cl_mtxp);
2533 }
2d21ac55
A
2534 if (iostate.io_error)
2535 error = iostate.io_error;
2536
2537 if (error == 0 && tail_size)
2538 error = cluster_align_phys_io(vp, uio, src_paddr, tail_size, 0, callback, callback_arg);
2539
2540 for (n = 0; n < num_upl; n++)
2541 /*
2542 * just release our hold on each physically contiguous
2543 * region without changing any state
2544 */
2545 ubc_upl_abort(upl[n], 0);
0b4e3aa0
A
2546
2547 return (error);
2548}
2549
b4c24cb9 2550
b0d623f7
A
2551/*
2552 * need to avoid a race between an msync of a range of pages dirtied via mmap
2553 * vs a filesystem such as HFS deciding to write a 'hole' to disk via cluster_write's
2554 * zerofill mechanism before it has seen the VNOP_PAGEOUTs for the pages being msync'd
2555 *
2556 * we should never force-zero-fill pages that are already valid in the cache...
2557 * the entire page contains valid data (either from disk, zero-filled or dirtied
2558 * via an mmap) so we can only do damage by trying to zero-fill
2559 *
2560 */
2561static int
2562cluster_zero_range(upl_t upl, upl_page_info_t *pl, int flags, int io_offset, off_t zero_off, off_t upl_f_offset, int bytes_to_zero)
2563{
2564 int zero_pg_index;
2565 boolean_t need_cluster_zero = TRUE;
2566
2567 if ((flags & (IO_NOZEROVALID | IO_NOZERODIRTY))) {
2568
2569 bytes_to_zero = min(bytes_to_zero, PAGE_SIZE - (int)(zero_off & PAGE_MASK_64));
2570 zero_pg_index = (int)((zero_off - upl_f_offset) / PAGE_SIZE_64);
2571
2572 if (upl_valid_page(pl, zero_pg_index)) {
2573 /*
2574 * never force zero valid pages - dirty or clean
2575 * we'll leave these in the UPL for cluster_write_copy to deal with
2576 */
2577 need_cluster_zero = FALSE;
2578 }
2579 }
2580 if (need_cluster_zero == TRUE)
2581 cluster_zero(upl, io_offset, bytes_to_zero, NULL);
2582
2583 return (bytes_to_zero);
2584}
2585
2586
9bccf70c 2587static int
2d21ac55
A
2588cluster_write_copy(vnode_t vp, struct uio *uio, u_int32_t io_req_size, off_t oldEOF, off_t newEOF, off_t headOff,
2589 off_t tailOff, int flags, int (*callback)(buf_t, void *), void *callback_arg)
1c79356b
A
2590{
2591 upl_page_info_t *pl;
2592 upl_t upl;
91447636 2593 vm_offset_t upl_offset = 0;
2d21ac55 2594 vm_size_t upl_size;
1c79356b
A
2595 off_t upl_f_offset;
2596 int pages_in_upl;
2597 int start_offset;
2598 int xfer_resid;
2599 int io_size;
1c79356b
A
2600 int io_offset;
2601 int bytes_to_zero;
2602 int bytes_to_move;
2603 kern_return_t kret;
2604 int retval = 0;
91447636 2605 int io_resid;
1c79356b
A
2606 long long total_size;
2607 long long zero_cnt;
2608 off_t zero_off;
2609 long long zero_cnt1;
2610 off_t zero_off1;
91447636 2611 struct cl_extent cl;
91447636 2612 struct cl_writebehind *wbp;
2d21ac55 2613 int bflag;
b0d623f7
A
2614 u_int max_cluster_pgcount;
2615 u_int max_io_size;
1c79356b
A
2616
2617 if (uio) {
2618 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 40)) | DBG_FUNC_START,
2d21ac55 2619 (int)uio->uio_offset, io_req_size, (int)oldEOF, (int)newEOF, 0);
1c79356b 2620
2d21ac55 2621 io_resid = io_req_size;
1c79356b
A
2622 } else {
2623 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 40)) | DBG_FUNC_START,
2624 0, 0, (int)oldEOF, (int)newEOF, 0);
2625
91447636 2626 io_resid = 0;
1c79356b 2627 }
b0d623f7
A
2628 if (flags & IO_PASSIVE)
2629 bflag = CL_PASSIVE;
2630 else
2631 bflag = 0;
2632
1c79356b
A
2633 zero_cnt = 0;
2634 zero_cnt1 = 0;
91447636
A
2635 zero_off = 0;
2636 zero_off1 = 0;
1c79356b 2637
cf7d32b8
A
2638 max_cluster_pgcount = MAX_CLUSTER_SIZE(vp) / PAGE_SIZE;
2639 max_io_size = cluster_max_io_size(vp->v_mount, CL_WRITE);
2640
1c79356b
A
2641 if (flags & IO_HEADZEROFILL) {
2642 /*
2643 * some filesystems (HFS is one) don't support unallocated holes within a file...
2644 * so we zero fill the intervening space between the old EOF and the offset
2645 * where the next chunk of real data begins.... ftruncate will also use this
2646 * routine to zero fill to the new EOF when growing a file... in this case, the
2647 * uio structure will not be provided
2648 */
2649 if (uio) {
2650 if (headOff < uio->uio_offset) {
2651 zero_cnt = uio->uio_offset - headOff;
2652 zero_off = headOff;
2653 }
2654 } else if (headOff < newEOF) {
2655 zero_cnt = newEOF - headOff;
2656 zero_off = headOff;
2657 }
b0d623f7
A
2658 } else {
2659 if (uio && uio->uio_offset > oldEOF) {
2660 zero_off = uio->uio_offset & ~PAGE_MASK_64;
2661
2662 if (zero_off >= oldEOF) {
2663 zero_cnt = uio->uio_offset - zero_off;
2664
2665 flags |= IO_HEADZEROFILL;
2666 }
2667 }
1c79356b
A
2668 }
2669 if (flags & IO_TAILZEROFILL) {
2670 if (uio) {
2d21ac55 2671 zero_off1 = uio->uio_offset + io_req_size;
1c79356b
A
2672
2673 if (zero_off1 < tailOff)
2674 zero_cnt1 = tailOff - zero_off1;
2675 }
b0d623f7
A
2676 } else {
2677 if (uio && newEOF > oldEOF) {
2678 zero_off1 = uio->uio_offset + io_req_size;
2679
2680 if (zero_off1 == newEOF && (zero_off1 & PAGE_MASK_64)) {
2681 zero_cnt1 = PAGE_SIZE_64 - (zero_off1 & PAGE_MASK_64);
2682
2683 flags |= IO_TAILZEROFILL;
2684 }
2685 }
1c79356b 2686 }
55e303ae 2687 if (zero_cnt == 0 && uio == (struct uio *) 0) {
91447636
A
2688 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 40)) | DBG_FUNC_END,
2689 retval, 0, 0, 0, 0);
2690 return (0);
55e303ae 2691 }
1c79356b 2692
91447636 2693 while ((total_size = (io_resid + zero_cnt + zero_cnt1)) && retval == 0) {
1c79356b
A
2694 /*
2695 * for this iteration of the loop, figure out where our starting point is
2696 */
2697 if (zero_cnt) {
2698 start_offset = (int)(zero_off & PAGE_MASK_64);
2699 upl_f_offset = zero_off - start_offset;
91447636 2700 } else if (io_resid) {
1c79356b
A
2701 start_offset = (int)(uio->uio_offset & PAGE_MASK_64);
2702 upl_f_offset = uio->uio_offset - start_offset;
2703 } else {
2704 start_offset = (int)(zero_off1 & PAGE_MASK_64);
2705 upl_f_offset = zero_off1 - start_offset;
2706 }
2707 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 46)) | DBG_FUNC_NONE,
2708 (int)zero_off, (int)zero_cnt, (int)zero_off1, (int)zero_cnt1, 0);
2709
cf7d32b8
A
2710 if (total_size > max_io_size)
2711 total_size = max_io_size;
1c79356b 2712
91447636 2713 cl.b_addr = (daddr64_t)(upl_f_offset / PAGE_SIZE_64);
55e303ae 2714
2d21ac55 2715 if (uio && ((flags & (IO_SYNC | IO_HEADZEROFILL | IO_TAILZEROFILL)) == 0)) {
55e303ae 2716 /*
91447636 2717 * assumption... total_size <= io_resid
55e303ae
A
2718 * because IO_HEADZEROFILL and IO_TAILZEROFILL not set
2719 */
cf7d32b8 2720 if ((start_offset + total_size) > max_io_size)
55e303ae
A
2721 total_size -= start_offset;
2722 xfer_resid = total_size;
2723
2d21ac55 2724 retval = cluster_copy_ubc_data_internal(vp, uio, &xfer_resid, 1, 1);
b0d623f7 2725
55e303ae
A
2726 if (retval)
2727 break;
2728
2d21ac55 2729 io_resid -= (total_size - xfer_resid);
55e303ae
A
2730 total_size = xfer_resid;
2731 start_offset = (int)(uio->uio_offset & PAGE_MASK_64);
2732 upl_f_offset = uio->uio_offset - start_offset;
2733
2734 if (total_size == 0) {
2735 if (start_offset) {
2736 /*
2737 * the write did not finish on a page boundary
2738 * which will leave upl_f_offset pointing to the
2739 * beginning of the last page written instead of
2740 * the page beyond it... bump it in this case
2741 * so that the cluster code records the last page
2742 * written as dirty
2743 */
2744 upl_f_offset += PAGE_SIZE_64;
2745 }
2746 upl_size = 0;
2747
2748 goto check_cluster;
2749 }
2750 }
1c79356b
A
2751 /*
2752 * compute the size of the upl needed to encompass
2753 * the requested write... limit each call to cluster_io
0b4e3aa0
A
2754 * to the maximum UPL size... cluster_io will clip if
2755 * this exceeds the maximum io_size for the device,
2756 * make sure to account for
1c79356b
A
2757 * a starting offset that's not page aligned
2758 */
2759 upl_size = (start_offset + total_size + (PAGE_SIZE - 1)) & ~PAGE_MASK;
2760
cf7d32b8
A
2761 if (upl_size > max_io_size)
2762 upl_size = max_io_size;
1c79356b
A
2763
2764 pages_in_upl = upl_size / PAGE_SIZE;
2765 io_size = upl_size - start_offset;
2766
2767 if ((long long)io_size > total_size)
2768 io_size = total_size;
2769
55e303ae
A
2770 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 41)) | DBG_FUNC_START, upl_size, io_size, total_size, 0, 0);
2771
1c79356b 2772
91447636
A
2773 /*
2774 * Gather the pages from the buffer cache.
2775 * The UPL_WILL_MODIFY flag lets the UPL subsystem know
2776 * that we intend to modify these pages.
2777 */
0b4e3aa0 2778 kret = ubc_create_upl(vp,
91447636
A
2779 upl_f_offset,
2780 upl_size,
2781 &upl,
2782 &pl,
b0d623f7 2783 UPL_SET_LITE | (( uio!=NULL && (uio->uio_flags & UIO_FLAGS_IS_COMPRESSED_FILE)) ? 0 : UPL_WILL_MODIFY));
1c79356b 2784 if (kret != KERN_SUCCESS)
2d21ac55 2785 panic("cluster_write_copy: failed to get pagelist");
1c79356b 2786
55e303ae 2787 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 41)) | DBG_FUNC_END,
b0d623f7 2788 upl, (int)upl_f_offset, start_offset, 0, 0);
1c79356b 2789
b0d623f7 2790 if (start_offset && upl_f_offset < oldEOF && !upl_valid_page(pl, 0)) {
0b4e3aa0 2791 int read_size;
1c79356b 2792
0b4e3aa0 2793 /*
1c79356b
A
2794 * we're starting in the middle of the first page of the upl
2795 * and the page isn't currently valid, so we're going to have
2796 * to read it in first... this is a synchronous operation
2797 */
2798 read_size = PAGE_SIZE;
2799
b0d623f7
A
2800 if ((upl_f_offset + read_size) > oldEOF)
2801 read_size = oldEOF - upl_f_offset;
9bccf70c 2802
91447636 2803 retval = cluster_io(vp, upl, 0, upl_f_offset, read_size,
2d21ac55 2804 CL_READ | bflag, (buf_t)NULL, (struct clios *)NULL, callback, callback_arg);
1c79356b 2805 if (retval) {
0b4e3aa0 2806 /*
1c79356b
A
2807 * we had an error during the read which causes us to abort
2808 * the current cluster_write request... before we do, we need
2809 * to release the rest of the pages in the upl without modifying
2810 * there state and mark the failed page in error
2811 */
935ed37a 2812 ubc_upl_abort_range(upl, 0, PAGE_SIZE, UPL_ABORT_DUMP_PAGES|UPL_ABORT_FREE_ON_EMPTY);
91447636
A
2813
2814 if (upl_size > PAGE_SIZE)
2815 ubc_upl_abort_range(upl, 0, upl_size, UPL_ABORT_FREE_ON_EMPTY);
1c79356b
A
2816
2817 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 45)) | DBG_FUNC_NONE,
b0d623f7 2818 upl, 0, 0, retval, 0);
1c79356b
A
2819 break;
2820 }
2821 }
2822 if ((start_offset == 0 || upl_size > PAGE_SIZE) && ((start_offset + io_size) & PAGE_MASK)) {
2823 /*
2824 * the last offset we're writing to in this upl does not end on a page
2825 * boundary... if it's not beyond the old EOF, then we'll also need to
2826 * pre-read this page in if it isn't already valid
2827 */
2828 upl_offset = upl_size - PAGE_SIZE;
2829
2830 if ((upl_f_offset + start_offset + io_size) < oldEOF &&
2831 !upl_valid_page(pl, upl_offset / PAGE_SIZE)) {
2832 int read_size;
2833
2834 read_size = PAGE_SIZE;
2835
b0d623f7
A
2836 if ((off_t)(upl_f_offset + upl_offset + read_size) > oldEOF)
2837 read_size = oldEOF - (upl_f_offset + upl_offset);
9bccf70c 2838
91447636 2839 retval = cluster_io(vp, upl, upl_offset, upl_f_offset + upl_offset, read_size,
2d21ac55 2840 CL_READ | bflag, (buf_t)NULL, (struct clios *)NULL, callback, callback_arg);
1c79356b 2841 if (retval) {
0b4e3aa0 2842 /*
1c79356b 2843 * we had an error during the read which causes us to abort
0b4e3aa0
A
2844 * the current cluster_write request... before we do, we
2845 * need to release the rest of the pages in the upl without
2846 * modifying there state and mark the failed page in error
1c79356b 2847 */
935ed37a 2848 ubc_upl_abort_range(upl, upl_offset, PAGE_SIZE, UPL_ABORT_DUMP_PAGES|UPL_ABORT_FREE_ON_EMPTY);
91447636
A
2849
2850 if (upl_size > PAGE_SIZE)
2851 ubc_upl_abort_range(upl, 0, upl_size, UPL_ABORT_FREE_ON_EMPTY);
1c79356b
A
2852
2853 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 45)) | DBG_FUNC_NONE,
b0d623f7 2854 upl, 0, 0, retval, 0);
1c79356b
A
2855 break;
2856 }
2857 }
2858 }
1c79356b
A
2859 xfer_resid = io_size;
2860 io_offset = start_offset;
2861
2862 while (zero_cnt && xfer_resid) {
2863
2864 if (zero_cnt < (long long)xfer_resid)
2865 bytes_to_zero = zero_cnt;
2866 else
2867 bytes_to_zero = xfer_resid;
2868
b0d623f7 2869 bytes_to_zero = cluster_zero_range(upl, pl, flags, io_offset, zero_off, upl_f_offset, bytes_to_zero);
9bccf70c 2870
1c79356b
A
2871 xfer_resid -= bytes_to_zero;
2872 zero_cnt -= bytes_to_zero;
2873 zero_off += bytes_to_zero;
2874 io_offset += bytes_to_zero;
2875 }
91447636 2876 if (xfer_resid && io_resid) {
2d21ac55
A
2877 u_int32_t io_requested;
2878
91447636 2879 bytes_to_move = min(io_resid, xfer_resid);
2d21ac55 2880 io_requested = bytes_to_move;
1c79356b 2881
2d21ac55 2882 retval = cluster_copy_upl_data(uio, upl, io_offset, (int *)&io_requested);
9bccf70c 2883
1c79356b 2884 if (retval) {
9bccf70c 2885 ubc_upl_abort_range(upl, 0, upl_size, UPL_ABORT_DUMP_PAGES | UPL_ABORT_FREE_ON_EMPTY);
1c79356b
A
2886
2887 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 45)) | DBG_FUNC_NONE,
b0d623f7 2888 upl, 0, 0, retval, 0);
1c79356b 2889 } else {
2d21ac55 2890 io_resid -= bytes_to_move;
1c79356b
A
2891 xfer_resid -= bytes_to_move;
2892 io_offset += bytes_to_move;
2893 }
2894 }
2895 while (xfer_resid && zero_cnt1 && retval == 0) {
2896
2897 if (zero_cnt1 < (long long)xfer_resid)
2898 bytes_to_zero = zero_cnt1;
2899 else
2900 bytes_to_zero = xfer_resid;
2901
b0d623f7
A
2902 bytes_to_zero = cluster_zero_range(upl, pl, flags, io_offset, zero_off1, upl_f_offset, bytes_to_zero);
2903
1c79356b
A
2904 xfer_resid -= bytes_to_zero;
2905 zero_cnt1 -= bytes_to_zero;
2906 zero_off1 += bytes_to_zero;
2907 io_offset += bytes_to_zero;
2908 }
1c79356b 2909 if (retval == 0) {
9bccf70c 2910 int cl_index;
2d21ac55 2911 int ret_cluster_try_push;
1c79356b
A
2912
2913 io_size += start_offset;
2914
2d21ac55 2915 if ((upl_f_offset + io_size) >= newEOF && (u_int)io_size < upl_size) {
1c79356b
A
2916 /*
2917 * if we're extending the file with this write
2918 * we'll zero fill the rest of the page so that
2919 * if the file gets extended again in such a way as to leave a
2920 * hole starting at this EOF, we'll have zero's in the correct spot
2921 */
55e303ae 2922 cluster_zero(upl, io_size, upl_size - io_size, NULL);
1c79356b 2923 }
935ed37a
A
2924 /*
2925 * release the upl now if we hold one since...
2926 * 1) pages in it may be present in the sparse cluster map
2927 * and may span 2 separate buckets there... if they do and
2928 * we happen to have to flush a bucket to make room and it intersects
2929 * this upl, a deadlock may result on page BUSY
2930 * 2) we're delaying the I/O... from this point forward we're just updating
2931 * the cluster state... no need to hold the pages, so commit them
2932 * 3) IO_SYNC is set...
2933 * because we had to ask for a UPL that provides currenty non-present pages, the
2934 * UPL has been automatically set to clear the dirty flags (both software and hardware)
2935 * upon committing it... this is not the behavior we want since it's possible for
2936 * pages currently present as part of a mapped file to be dirtied while the I/O is in flight.
2937 * we'll pick these pages back up later with the correct behavior specified.
2938 * 4) we don't want to hold pages busy in a UPL and then block on the cluster lock... if a flush
2939 * of this vnode is in progress, we will deadlock if the pages being flushed intersect the pages
2940 * we hold since the flushing context is holding the cluster lock.
2941 */
2942 ubc_upl_commit_range(upl, 0, upl_size,
2943 UPL_COMMIT_SET_DIRTY | UPL_COMMIT_INACTIVATE | UPL_COMMIT_FREE_ON_EMPTY);
2944check_cluster:
2945 /*
2946 * calculate the last logical block number
2947 * that this delayed I/O encompassed
2948 */
2949 cl.e_addr = (daddr64_t)((upl_f_offset + (off_t)upl_size) / PAGE_SIZE_64);
2950
b0d623f7 2951 if (flags & IO_SYNC) {
9bccf70c
A
2952 /*
2953 * if the IO_SYNC flag is set than we need to
2954 * bypass any clusters and immediately issue
2955 * the I/O
2956 */
2957 goto issue_io;
b0d623f7 2958 }
91447636
A
2959 /*
2960 * take the lock to protect our accesses
2961 * of the writebehind and sparse cluster state
2962 */
2963 wbp = cluster_get_wbp(vp, CLW_ALLOCATE | CLW_RETURNLOCKED);
2964
91447636 2965 if (wbp->cl_scmap) {
55e303ae 2966
91447636 2967 if ( !(flags & IO_NOCACHE)) {
55e303ae
A
2968 /*
2969 * we've fallen into the sparse
2970 * cluster method of delaying dirty pages
55e303ae 2971 */
b0d623f7 2972 sparse_cluster_add(&(wbp->cl_scmap), vp, &cl, newEOF, callback, callback_arg);
91447636
A
2973
2974 lck_mtx_unlock(&wbp->cl_lockw);
55e303ae
A
2975
2976 continue;
2977 }
2978 /*
2979 * must have done cached writes that fell into
2980 * the sparse cluster mechanism... we've switched
2981 * to uncached writes on the file, so go ahead
2982 * and push whatever's in the sparse map
2983 * and switch back to normal clustering
55e303ae 2984 */
91447636 2985 wbp->cl_number = 0;
935ed37a 2986
b0d623f7 2987 sparse_cluster_push(&(wbp->cl_scmap), vp, newEOF, PUSH_ALL, callback, callback_arg);
55e303ae
A
2988 /*
2989 * no clusters of either type present at this point
2990 * so just go directly to start_new_cluster since
2991 * we know we need to delay this I/O since we've
2992 * already released the pages back into the cache
2993 * to avoid the deadlock with sparse_cluster_push
2994 */
2995 goto start_new_cluster;
2996 }
91447636 2997 if (wbp->cl_number == 0)
9bccf70c
A
2998 /*
2999 * no clusters currently present
3000 */
3001 goto start_new_cluster;
1c79356b 3002
91447636 3003 for (cl_index = 0; cl_index < wbp->cl_number; cl_index++) {
1c79356b 3004 /*
55e303ae
A
3005 * check each cluster that we currently hold
3006 * try to merge some or all of this write into
3007 * one or more of the existing clusters... if
3008 * any portion of the write remains, start a
3009 * new cluster
1c79356b 3010 */
91447636 3011 if (cl.b_addr >= wbp->cl_clusters[cl_index].b_addr) {
9bccf70c
A
3012 /*
3013 * the current write starts at or after the current cluster
3014 */
cf7d32b8 3015 if (cl.e_addr <= (wbp->cl_clusters[cl_index].b_addr + max_cluster_pgcount)) {
1c79356b
A
3016 /*
3017 * we have a write that fits entirely
3018 * within the existing cluster limits
3019 */
91447636 3020 if (cl.e_addr > wbp->cl_clusters[cl_index].e_addr)
1c79356b 3021 /*
9bccf70c 3022 * update our idea of where the cluster ends
1c79356b 3023 */
91447636 3024 wbp->cl_clusters[cl_index].e_addr = cl.e_addr;
9bccf70c 3025 break;
1c79356b 3026 }
cf7d32b8 3027 if (cl.b_addr < (wbp->cl_clusters[cl_index].b_addr + max_cluster_pgcount)) {
1c79356b
A
3028 /*
3029 * we have a write that starts in the middle of the current cluster
55e303ae
A
3030 * but extends beyond the cluster's limit... we know this because
3031 * of the previous checks
3032 * we'll extend the current cluster to the max
91447636 3033 * and update the b_addr for the current write to reflect that
55e303ae
A
3034 * the head of it was absorbed into this cluster...
3035 * note that we'll always have a leftover tail in this case since
3036 * full absorbtion would have occurred in the clause above
1c79356b 3037 */
cf7d32b8 3038 wbp->cl_clusters[cl_index].e_addr = wbp->cl_clusters[cl_index].b_addr + max_cluster_pgcount;
55e303ae 3039
91447636 3040 cl.b_addr = wbp->cl_clusters[cl_index].e_addr;
1c79356b
A
3041 }
3042 /*
55e303ae
A
3043 * we come here for the case where the current write starts
3044 * beyond the limit of the existing cluster or we have a leftover
3045 * tail after a partial absorbtion
9bccf70c
A
3046 *
3047 * in either case, we'll check the remaining clusters before
3048 * starting a new one
1c79356b 3049 */
9bccf70c 3050 } else {
1c79356b 3051 /*
55e303ae 3052 * the current write starts in front of the cluster we're currently considering
1c79356b 3053 */
cf7d32b8 3054 if ((wbp->cl_clusters[cl_index].e_addr - cl.b_addr) <= max_cluster_pgcount) {
1c79356b 3055 /*
55e303ae
A
3056 * we can just merge the new request into
3057 * this cluster and leave it in the cache
3058 * since the resulting cluster is still
3059 * less than the maximum allowable size
1c79356b 3060 */
91447636 3061 wbp->cl_clusters[cl_index].b_addr = cl.b_addr;
1c79356b 3062
91447636 3063 if (cl.e_addr > wbp->cl_clusters[cl_index].e_addr) {
9bccf70c
A
3064 /*
3065 * the current write completely
55e303ae 3066 * envelops the existing cluster and since
cf7d32b8 3067 * each write is limited to at most max_cluster_pgcount pages
55e303ae
A
3068 * we can just use the start and last blocknos of the write
3069 * to generate the cluster limits
9bccf70c 3070 */
91447636 3071 wbp->cl_clusters[cl_index].e_addr = cl.e_addr;
9bccf70c
A
3072 }
3073 break;
1c79356b 3074 }
9bccf70c 3075
1c79356b 3076 /*
9bccf70c
A
3077 * if we were to combine this write with the current cluster
3078 * we would exceed the cluster size limit.... so,
3079 * let's see if there's any overlap of the new I/O with
55e303ae
A
3080 * the cluster we're currently considering... in fact, we'll
3081 * stretch the cluster out to it's full limit and see if we
3082 * get an intersection with the current write
9bccf70c 3083 *
1c79356b 3084 */
cf7d32b8 3085 if (cl.e_addr > wbp->cl_clusters[cl_index].e_addr - max_cluster_pgcount) {
1c79356b 3086 /*
55e303ae
A
3087 * the current write extends into the proposed cluster
3088 * clip the length of the current write after first combining it's
3089 * tail with the newly shaped cluster
1c79356b 3090 */
cf7d32b8 3091 wbp->cl_clusters[cl_index].b_addr = wbp->cl_clusters[cl_index].e_addr - max_cluster_pgcount;
55e303ae 3092
91447636 3093 cl.e_addr = wbp->cl_clusters[cl_index].b_addr;
55e303ae 3094 }
9bccf70c
A
3095 /*
3096 * if we get here, there was no way to merge
55e303ae
A
3097 * any portion of this write with this cluster
3098 * or we could only merge part of it which
3099 * will leave a tail...
9bccf70c
A
3100 * we'll check the remaining clusters before starting a new one
3101 */
1c79356b 3102 }
9bccf70c 3103 }
91447636 3104 if (cl_index < wbp->cl_number)
9bccf70c 3105 /*
55e303ae
A
3106 * we found an existing cluster(s) that we
3107 * could entirely merge this I/O into
9bccf70c
A
3108 */
3109 goto delay_io;
3110
2d21ac55 3111 if (wbp->cl_number < MAX_CLUSTERS)
9bccf70c
A
3112 /*
3113 * we didn't find an existing cluster to
3114 * merge into, but there's room to start
1c79356b
A
3115 * a new one
3116 */
9bccf70c 3117 goto start_new_cluster;
1c79356b 3118
9bccf70c
A
3119 /*
3120 * no exisitng cluster to merge with and no
3121 * room to start a new one... we'll try
55e303ae
A
3122 * pushing one of the existing ones... if none of
3123 * them are able to be pushed, we'll switch
3124 * to the sparse cluster mechanism
91447636 3125 * cluster_try_push updates cl_number to the
55e303ae
A
3126 * number of remaining clusters... and
3127 * returns the number of currently unused clusters
9bccf70c 3128 */
2d21ac55
A
3129 ret_cluster_try_push = 0;
3130
3131 /*
3132 * if writes are not deferred, call cluster push immediately
3133 */
91447636 3134 if (!((unsigned int)vfs_flags(vp->v_mount) & MNT_DEFWRITE)) {
91447636 3135
2d21ac55 3136 ret_cluster_try_push = cluster_try_push(wbp, vp, newEOF, (flags & IO_NOCACHE) ? 0 : PUSH_DELAY, callback, callback_arg);
91447636 3137 }
9bccf70c 3138
2d21ac55
A
3139 /*
3140 * execute following regardless of writes being deferred or not
3141 */
91447636 3142 if (ret_cluster_try_push == 0) {
55e303ae
A
3143 /*
3144 * no more room in the normal cluster mechanism
3145 * so let's switch to the more expansive but expensive
3146 * sparse mechanism....
55e303ae 3147 */
2d21ac55 3148 sparse_cluster_switch(wbp, vp, newEOF, callback, callback_arg);
b0d623f7 3149 sparse_cluster_add(&(wbp->cl_scmap), vp, &cl, newEOF, callback, callback_arg);
91447636
A
3150
3151 lck_mtx_unlock(&wbp->cl_lockw);
55e303ae
A
3152
3153 continue;
9bccf70c 3154 }
55e303ae
A
3155 /*
3156 * we pushed one cluster successfully, so we must be sequentially writing this file
3157 * otherwise, we would have failed and fallen into the sparse cluster support
2d21ac55
A
3158 * so let's take the opportunity to push out additional clusters...
3159 * this will give us better I/O locality if we're in a copy loop
3160 * (i.e. we won't jump back and forth between the read and write points
55e303ae 3161 */
91447636 3162 if (!((unsigned int)vfs_flags(vp->v_mount) & MNT_DEFWRITE)) {
2d21ac55
A
3163 while (wbp->cl_number)
3164 cluster_try_push(wbp, vp, newEOF, 0, callback, callback_arg);
91447636 3165 }
55e303ae 3166
9bccf70c 3167start_new_cluster:
91447636
A
3168 wbp->cl_clusters[wbp->cl_number].b_addr = cl.b_addr;
3169 wbp->cl_clusters[wbp->cl_number].e_addr = cl.e_addr;
9bccf70c 3170
2d21ac55
A
3171 wbp->cl_clusters[wbp->cl_number].io_flags = 0;
3172
91447636 3173 if (flags & IO_NOCACHE)
2d21ac55
A
3174 wbp->cl_clusters[wbp->cl_number].io_flags |= CLW_IONOCACHE;
3175
3176 if (bflag & CL_PASSIVE)
3177 wbp->cl_clusters[wbp->cl_number].io_flags |= CLW_IOPASSIVE;
3178
91447636 3179 wbp->cl_number++;
55e303ae 3180delay_io:
91447636
A
3181 lck_mtx_unlock(&wbp->cl_lockw);
3182
9bccf70c
A
3183 continue;
3184issue_io:
3185 /*
935ed37a 3186 * we don't hold the lock at this point
91447636 3187 *
935ed37a 3188 * we've already dropped the current upl, so pick it back up with COPYOUT_FROM set
91447636 3189 * so that we correctly deal with a change in state of the hardware modify bit...
2d21ac55
A
3190 * we do this via cluster_push_now... by passing along the IO_SYNC flag, we force
3191 * cluster_push_now to wait until all the I/Os have completed... cluster_push_now is also
91447636 3192 * responsible for generating the correct sized I/O(s)
9bccf70c 3193 */
2d21ac55 3194 retval = cluster_push_now(vp, &cl, newEOF, flags, callback, callback_arg);
1c79356b
A
3195 }
3196 }
2d21ac55 3197 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 40)) | DBG_FUNC_END, retval, 0, io_resid, 0, 0);
1c79356b
A
3198
3199 return (retval);
3200}
3201
2d21ac55
A
3202
3203
9bccf70c 3204int
91447636 3205cluster_read(vnode_t vp, struct uio *uio, off_t filesize, int xflags)
1c79356b 3206{
2d21ac55
A
3207 return cluster_read_ext(vp, uio, filesize, xflags, NULL, NULL);
3208}
3209
3210
3211int
3212cluster_read_ext(vnode_t vp, struct uio *uio, off_t filesize, int xflags, int (*callback)(buf_t, void *), void *callback_arg)
3213{
3214 int retval = 0;
3215 int flags;
3216 user_ssize_t cur_resid;
3217 u_int32_t io_size;
3218 u_int32_t read_length = 0;
3219 int read_type = IO_COPY;
1c79356b 3220
91447636 3221 flags = xflags;
1c79356b 3222
91447636
A
3223 if (vp->v_flag & VNOCACHE_DATA)
3224 flags |= IO_NOCACHE;
2d21ac55 3225 if ((vp->v_flag & VRAOFF) || speculative_reads_disabled)
91447636
A
3226 flags |= IO_RAOFF;
3227
2d21ac55
A
3228 /*
3229 * do a read through the cache if one of the following is true....
3230 * NOCACHE is not true
3231 * the uio request doesn't target USERSPACE
3232 * otherwise, find out if we want the direct or contig variant for
3233 * the first vector in the uio request
3234 */
3235 if ( (flags & IO_NOCACHE) && UIO_SEG_IS_USER_SPACE(uio->uio_segflg) )
3236 retval = cluster_io_type(uio, &read_type, &read_length, 0);
cc9f6e38 3237
2d21ac55 3238 while ((cur_resid = uio_resid(uio)) && uio->uio_offset < filesize && retval == 0) {
91447636 3239
2d21ac55
A
3240 switch (read_type) {
3241
3242 case IO_COPY:
91447636 3243 /*
2d21ac55
A
3244 * make sure the uio_resid isn't too big...
3245 * internally, we want to handle all of the I/O in
3246 * chunk sizes that fit in a 32 bit int
91447636 3247 */
2d21ac55
A
3248 if (cur_resid > (user_ssize_t)(MAX_IO_REQUEST_SIZE))
3249 io_size = MAX_IO_REQUEST_SIZE;
3250 else
3251 io_size = (u_int32_t)cur_resid;
91447636 3252
2d21ac55
A
3253 retval = cluster_read_copy(vp, uio, io_size, filesize, flags, callback, callback_arg);
3254 break;
1c79356b 3255
2d21ac55
A
3256 case IO_DIRECT:
3257 retval = cluster_read_direct(vp, uio, filesize, &read_type, &read_length, flags, callback, callback_arg);
3258 break;
91447636 3259
2d21ac55
A
3260 case IO_CONTIG:
3261 retval = cluster_read_contig(vp, uio, filesize, &read_type, &read_length, callback, callback_arg, flags);
3262 break;
3263
3264 case IO_UNKNOWN:
3265 retval = cluster_io_type(uio, &read_type, &read_length, 0);
3266 break;
3267 }
3268 }
3269 return (retval);
3270}
91447636 3271
91447636 3272
91447636 3273
2d21ac55 3274static void
b0d623f7 3275cluster_read_upl_release(upl_t upl, int start_pg, int last_pg, int take_reference)
2d21ac55
A
3276{
3277 int range;
3278 int abort_flags = UPL_ABORT_FREE_ON_EMPTY;
1c79356b 3279
2d21ac55 3280 if ((range = last_pg - start_pg)) {
b0d623f7 3281 if (take_reference)
2d21ac55
A
3282 abort_flags |= UPL_ABORT_REFERENCE;
3283
3284 ubc_upl_abort_range(upl, start_pg * PAGE_SIZE, range * PAGE_SIZE, abort_flags);
3285 }
1c79356b
A
3286}
3287
2d21ac55 3288
9bccf70c 3289static int
2d21ac55 3290cluster_read_copy(vnode_t vp, struct uio *uio, u_int32_t io_req_size, off_t filesize, int flags, int (*callback)(buf_t, void *), void *callback_arg)
1c79356b
A
3291{
3292 upl_page_info_t *pl;
3293 upl_t upl;
3294 vm_offset_t upl_offset;
b0d623f7 3295 u_int32_t upl_size;
1c79356b
A
3296 off_t upl_f_offset;
3297 int start_offset;
3298 int start_pg;
3299 int last_pg;
91447636 3300 int uio_last = 0;
1c79356b
A
3301 int pages_in_upl;
3302 off_t max_size;
55e303ae
A
3303 off_t last_ioread_offset;
3304 off_t last_request_offset;
1c79356b 3305 kern_return_t kret;
1c79356b
A
3306 int error = 0;
3307 int retval = 0;
2d21ac55
A
3308 u_int32_t size_of_prefetch;
3309 u_int32_t xsize;
3310 u_int32_t io_size;
cf7d32b8 3311 u_int32_t max_rd_size;
b0d623f7
A
3312 u_int32_t max_io_size;
3313 u_int32_t max_prefetch;
55e303ae
A
3314 u_int rd_ahead_enabled = 1;
3315 u_int prefetch_enabled = 1;
91447636
A
3316 struct cl_readahead * rap;
3317 struct clios iostate;
3318 struct cl_extent extent;
2d21ac55
A
3319 int bflag;
3320 int take_reference = 1;
3321 struct uthread *ut;
3322 int policy = IOPOL_DEFAULT;
3323
b0d623f7
A
3324
3325 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 32)) | DBG_FUNC_START,
3326 (int)uio->uio_offset, io_req_size, (int)filesize, flags, 0);
3327
2d21ac55
A
3328 policy = current_proc()->p_iopol_disk;
3329
3330 ut = get_bsdthread_info(current_thread());
3331
3332 if (ut->uu_iopol_disk != IOPOL_DEFAULT)
3333 policy = ut->uu_iopol_disk;
3334
b0d623f7 3335 if (policy == IOPOL_THROTTLE || (flags & IO_NOCACHE))
2d21ac55
A
3336 take_reference = 0;
3337
3338 if (flags & IO_PASSIVE)
cf7d32b8 3339 bflag = CL_PASSIVE;
2d21ac55 3340 else
b0d623f7 3341 bflag = 0;
cf7d32b8 3342
b0d623f7
A
3343 max_io_size = cluster_max_io_size(vp->v_mount, CL_READ);
3344 max_prefetch = MAX_PREFETCH(vp, max_io_size);
3345 max_rd_size = max_prefetch;
55e303ae 3346
2d21ac55 3347 last_request_offset = uio->uio_offset + io_req_size;
55e303ae 3348
b0d623f7
A
3349 if (last_request_offset > filesize)
3350 last_request_offset = filesize;
3351
2d21ac55 3352 if ((flags & (IO_RAOFF|IO_NOCACHE)) || ((last_request_offset & ~PAGE_MASK_64) == (uio->uio_offset & ~PAGE_MASK_64))) {
55e303ae 3353 rd_ahead_enabled = 0;
91447636
A
3354 rap = NULL;
3355 } else {
b0d623f7 3356 if (cluster_hard_throttle_on(vp, 1)) {
91447636
A
3357 rd_ahead_enabled = 0;
3358 prefetch_enabled = 0;
55e303ae 3359
91447636 3360 max_rd_size = HARD_THROTTLE_MAXSIZE;
b0d623f7
A
3361 } else if (policy == IOPOL_THROTTLE) {
3362 rd_ahead_enabled = 0;
3363 prefetch_enabled = 0;
91447636
A
3364 }
3365 if ((rap = cluster_get_rap(vp)) == NULL)
3366 rd_ahead_enabled = 0;
b0d623f7
A
3367 else {
3368 extent.b_addr = uio->uio_offset / PAGE_SIZE_64;
3369 extent.e_addr = (last_request_offset - 1) / PAGE_SIZE_64;
3370 }
55e303ae 3371 }
91447636 3372 if (rap != NULL && rap->cl_ralen && (rap->cl_lastr == extent.b_addr || (rap->cl_lastr + 1) == extent.b_addr)) {
55e303ae
A
3373 /*
3374 * determine if we already have a read-ahead in the pipe courtesy of the
3375 * last read systemcall that was issued...
3376 * if so, pick up it's extent to determine where we should start
3377 * with respect to any read-ahead that might be necessary to
3378 * garner all the data needed to complete this read systemcall
3379 */
91447636 3380 last_ioread_offset = (rap->cl_maxra * PAGE_SIZE_64) + PAGE_SIZE_64;
1c79356b 3381
55e303ae
A
3382 if (last_ioread_offset < uio->uio_offset)
3383 last_ioread_offset = (off_t)0;
3384 else if (last_ioread_offset > last_request_offset)
3385 last_ioread_offset = last_request_offset;
3386 } else
3387 last_ioread_offset = (off_t)0;
1c79356b 3388
2d21ac55 3389 while (io_req_size && uio->uio_offset < filesize && retval == 0) {
b0d623f7
A
3390
3391 max_size = filesize - uio->uio_offset;
1c79356b 3392
2d21ac55
A
3393 if ((off_t)(io_req_size) < max_size)
3394 io_size = io_req_size;
1c79356b
A
3395 else
3396 io_size = max_size;
9bccf70c 3397
91447636 3398 if (!(flags & IO_NOCACHE)) {
1c79356b 3399
55e303ae 3400 while (io_size) {
2d21ac55
A
3401 u_int32_t io_resid;
3402 u_int32_t io_requested;
1c79356b 3403
55e303ae
A
3404 /*
3405 * if we keep finding the pages we need already in the cache, then
2d21ac55 3406 * don't bother to call cluster_read_prefetch since it costs CPU cycles
55e303ae
A
3407 * to determine that we have all the pages we need... once we miss in
3408 * the cache and have issued an I/O, than we'll assume that we're likely
3409 * to continue to miss in the cache and it's to our advantage to try and prefetch
3410 */
3411 if (last_request_offset && last_ioread_offset && (size_of_prefetch = (last_request_offset - last_ioread_offset))) {
3412 if ((last_ioread_offset - uio->uio_offset) <= max_rd_size && prefetch_enabled) {
3413 /*
3414 * we've already issued I/O for this request and
3415 * there's still work to do and
3416 * our prefetch stream is running dry, so issue a
3417 * pre-fetch I/O... the I/O latency will overlap
3418 * with the copying of the data
3419 */
3420 if (size_of_prefetch > max_rd_size)
3421 size_of_prefetch = max_rd_size;
1c79356b 3422
2d21ac55 3423 size_of_prefetch = cluster_read_prefetch(vp, last_ioread_offset, size_of_prefetch, filesize, callback, callback_arg, bflag);
1c79356b 3424
55e303ae
A
3425 last_ioread_offset += (off_t)(size_of_prefetch * PAGE_SIZE);
3426
3427 if (last_ioread_offset > last_request_offset)
3428 last_ioread_offset = last_request_offset;
3429 }
3430 }
3431 /*
3432 * limit the size of the copy we're about to do so that
3433 * we can notice that our I/O pipe is running dry and
3434 * get the next I/O issued before it does go dry
3435 */
cf7d32b8
A
3436 if (last_ioread_offset && io_size > (max_io_size / 4))
3437 io_resid = (max_io_size / 4);
55e303ae
A
3438 else
3439 io_resid = io_size;
1c79356b 3440
55e303ae 3441 io_requested = io_resid;
1c79356b 3442
b0d623f7 3443 retval = cluster_copy_ubc_data_internal(vp, uio, (int *)&io_resid, 0, last_ioread_offset == 0 ? take_reference : 0);
2d21ac55
A
3444
3445 xsize = io_requested - io_resid;
1c79356b 3446
2d21ac55
A
3447 io_size -= xsize;
3448 io_req_size -= xsize;
1c79356b 3449
55e303ae
A
3450 if (retval || io_resid)
3451 /*
3452 * if we run into a real error or
3453 * a page that is not in the cache
3454 * we need to leave streaming mode
3455 */
3456 break;
3457
b0d623f7 3458 if (rd_ahead_enabled && (io_size == 0 || last_ioread_offset == last_request_offset)) {
55e303ae
A
3459 /*
3460 * we're already finished the I/O for this read request
3461 * let's see if we should do a read-ahead
3462 */
2d21ac55 3463 cluster_read_ahead(vp, &extent, filesize, rap, callback, callback_arg, bflag);
55e303ae 3464 }
1c79356b 3465 }
1c79356b
A
3466 if (retval)
3467 break;
1c79356b 3468 if (io_size == 0) {
91447636
A
3469 if (rap != NULL) {
3470 if (extent.e_addr < rap->cl_lastr)
3471 rap->cl_maxra = 0;
3472 rap->cl_lastr = extent.e_addr;
3473 }
1c79356b
A
3474 break;
3475 }
b0d623f7
A
3476 /*
3477 * recompute max_size since cluster_copy_ubc_data_internal
3478 * may have advanced uio->uio_offset
3479 */
3480 max_size = filesize - uio->uio_offset;
1c79356b 3481 }
b0d623f7
A
3482 /*
3483 * compute the size of the upl needed to encompass
3484 * the requested read... limit each call to cluster_io
3485 * to the maximum UPL size... cluster_io will clip if
3486 * this exceeds the maximum io_size for the device,
3487 * make sure to account for
3488 * a starting offset that's not page aligned
3489 */
3490 start_offset = (int)(uio->uio_offset & PAGE_MASK_64);
3491 upl_f_offset = uio->uio_offset - (off_t)start_offset;
3492
55e303ae
A
3493 if (io_size > max_rd_size)
3494 io_size = max_rd_size;
3495
1c79356b 3496 upl_size = (start_offset + io_size + (PAGE_SIZE - 1)) & ~PAGE_MASK;
55e303ae 3497
2d21ac55 3498 if (flags & IO_NOCACHE) {
cf7d32b8
A
3499 if (upl_size > max_io_size)
3500 upl_size = max_io_size;
2d21ac55 3501 } else {
cf7d32b8
A
3502 if (upl_size > max_io_size / 4)
3503 upl_size = max_io_size / 4;
2d21ac55 3504 }
1c79356b
A
3505 pages_in_upl = upl_size / PAGE_SIZE;
3506
3507 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 33)) | DBG_FUNC_START,
b0d623f7 3508 upl, (int)upl_f_offset, upl_size, start_offset, 0);
1c79356b 3509
0b4e3aa0 3510 kret = ubc_create_upl(vp,
91447636
A
3511 upl_f_offset,
3512 upl_size,
3513 &upl,
3514 &pl,
2d21ac55 3515 UPL_FILE_IO | UPL_SET_LITE);
1c79356b 3516 if (kret != KERN_SUCCESS)
2d21ac55 3517 panic("cluster_read_copy: failed to get pagelist");
1c79356b 3518
1c79356b 3519 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 33)) | DBG_FUNC_END,
b0d623f7 3520 upl, (int)upl_f_offset, upl_size, start_offset, 0);
1c79356b
A
3521
3522 /*
3523 * scan from the beginning of the upl looking for the first
3524 * non-valid page.... this will become the first page in
3525 * the request we're going to make to 'cluster_io'... if all
3526 * of the pages are valid, we won't call through to 'cluster_io'
3527 */
3528 for (start_pg = 0; start_pg < pages_in_upl; start_pg++) {
3529 if (!upl_valid_page(pl, start_pg))
3530 break;
3531 }
3532
3533 /*
3534 * scan from the starting invalid page looking for a valid
3535 * page before the end of the upl is reached, if we
3536 * find one, then it will be the last page of the request to
3537 * 'cluster_io'
3538 */
3539 for (last_pg = start_pg; last_pg < pages_in_upl; last_pg++) {
3540 if (upl_valid_page(pl, last_pg))
3541 break;
3542 }
55e303ae
A
3543 iostate.io_completed = 0;
3544 iostate.io_issued = 0;
3545 iostate.io_error = 0;
3546 iostate.io_wanted = 0;
1c79356b
A
3547
3548 if (start_pg < last_pg) {
3549 /*
3550 * we found a range of 'invalid' pages that must be filled
3551 * if the last page in this range is the last page of the file
3552 * we may have to clip the size of it to keep from reading past
3553 * the end of the last physical block associated with the file
3554 */
3555 upl_offset = start_pg * PAGE_SIZE;
3556 io_size = (last_pg - start_pg) * PAGE_SIZE;
3557
b0d623f7 3558 if ((off_t)(upl_f_offset + upl_offset + io_size) > filesize)
1c79356b 3559 io_size = filesize - (upl_f_offset + upl_offset);
9bccf70c 3560
1c79356b 3561 /*
55e303ae 3562 * issue an asynchronous read to cluster_io
1c79356b
A
3563 */
3564
3565 error = cluster_io(vp, upl, upl_offset, upl_f_offset + upl_offset,
2d21ac55 3566 io_size, CL_READ | CL_ASYNC | bflag, (buf_t)NULL, &iostate, callback, callback_arg);
1c79356b
A
3567 }
3568 if (error == 0) {
3569 /*
3570 * if the read completed successfully, or there was no I/O request
55e303ae
A
3571 * issued, than copy the data into user land via 'cluster_upl_copy_data'
3572 * we'll first add on any 'valid'
1c79356b
A
3573 * pages that were present in the upl when we acquired it.
3574 */
3575 u_int val_size;
1c79356b
A
3576
3577 for (uio_last = last_pg; uio_last < pages_in_upl; uio_last++) {
3578 if (!upl_valid_page(pl, uio_last))
3579 break;
3580 }
2d21ac55
A
3581 if (uio_last < pages_in_upl) {
3582 /*
3583 * there were some invalid pages beyond the valid pages
3584 * that we didn't issue an I/O for, just release them
3585 * unchanged now, so that any prefetch/readahed can
3586 * include them
3587 */
3588 ubc_upl_abort_range(upl, uio_last * PAGE_SIZE,
3589 (pages_in_upl - uio_last) * PAGE_SIZE, UPL_ABORT_FREE_ON_EMPTY);
3590 }
3591
1c79356b 3592 /*
2d21ac55 3593 * compute size to transfer this round, if io_req_size is
55e303ae 3594 * still non-zero after this attempt, we'll loop around and
1c79356b
A
3595 * set up for another I/O.
3596 */
3597 val_size = (uio_last * PAGE_SIZE) - start_offset;
3598
55e303ae 3599 if (val_size > max_size)
1c79356b
A
3600 val_size = max_size;
3601
2d21ac55
A
3602 if (val_size > io_req_size)
3603 val_size = io_req_size;
1c79356b 3604
2d21ac55 3605 if ((uio->uio_offset + val_size) > last_ioread_offset)
55e303ae 3606 last_ioread_offset = uio->uio_offset + val_size;
1c79356b 3607
55e303ae 3608 if ((size_of_prefetch = (last_request_offset - last_ioread_offset)) && prefetch_enabled) {
1c79356b 3609
2d21ac55
A
3610 if ((last_ioread_offset - (uio->uio_offset + val_size)) <= upl_size) {
3611 /*
3612 * if there's still I/O left to do for this request, and...
3613 * we're not in hard throttle mode, and...
3614 * we're close to using up the previous prefetch, then issue a
3615 * new pre-fetch I/O... the I/O latency will overlap
3616 * with the copying of the data
3617 */
3618 if (size_of_prefetch > max_rd_size)
3619 size_of_prefetch = max_rd_size;
3620
3621 size_of_prefetch = cluster_read_prefetch(vp, last_ioread_offset, size_of_prefetch, filesize, callback, callback_arg, bflag);
3622
3623 last_ioread_offset += (off_t)(size_of_prefetch * PAGE_SIZE);
55e303ae 3624
2d21ac55
A
3625 if (last_ioread_offset > last_request_offset)
3626 last_ioread_offset = last_request_offset;
3627 }
1c79356b 3628
55e303ae
A
3629 } else if ((uio->uio_offset + val_size) == last_request_offset) {
3630 /*
3631 * this transfer will finish this request, so...
3632 * let's try to read ahead if we're in
3633 * a sequential access pattern and we haven't
3634 * explicitly disabled it
3635 */
3636 if (rd_ahead_enabled)
2d21ac55 3637 cluster_read_ahead(vp, &extent, filesize, rap, callback, callback_arg, bflag);
91447636
A
3638
3639 if (rap != NULL) {
3640 if (extent.e_addr < rap->cl_lastr)
3641 rap->cl_maxra = 0;
3642 rap->cl_lastr = extent.e_addr;
3643 }
9bccf70c 3644 }
b0d623f7 3645 if (iostate.io_issued > iostate.io_completed) {
91447636 3646
b0d623f7 3647 lck_mtx_lock(cl_mtxp);
cf7d32b8 3648
b0d623f7
A
3649 while (iostate.io_issued != iostate.io_completed) {
3650 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 95)) | DBG_FUNC_START,
3651 iostate.io_issued, iostate.io_completed, 0, 0, 0);
cf7d32b8 3652
b0d623f7
A
3653 iostate.io_wanted = 1;
3654 msleep((caddr_t)&iostate.io_wanted, cl_mtxp, PRIBIO + 1, "cluster_read_copy", NULL);
91447636 3655
b0d623f7
A
3656 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 95)) | DBG_FUNC_END,
3657 iostate.io_issued, iostate.io_completed, 0, 0, 0);
3658 }
3659 lck_mtx_unlock(cl_mtxp);
3660 }
55e303ae
A
3661 if (iostate.io_error)
3662 error = iostate.io_error;
2d21ac55
A
3663 else {
3664 u_int32_t io_requested;
3665
3666 io_requested = val_size;
3667
3668 retval = cluster_copy_upl_data(uio, upl, start_offset, (int *)&io_requested);
3669
3670 io_req_size -= (val_size - io_requested);
3671 }
1c79356b
A
3672 }
3673 if (start_pg < last_pg) {
3674 /*
3675 * compute the range of pages that we actually issued an I/O for
3676 * and either commit them as valid if the I/O succeeded
2d21ac55
A
3677 * or abort them if the I/O failed or we're not supposed to
3678 * keep them in the cache
1c79356b
A
3679 */
3680 io_size = (last_pg - start_pg) * PAGE_SIZE;
3681
b0d623f7 3682 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 35)) | DBG_FUNC_START, upl, start_pg * PAGE_SIZE, io_size, error, 0);
1c79356b 3683
91447636 3684 if (error || (flags & IO_NOCACHE))
0b4e3aa0 3685 ubc_upl_abort_range(upl, start_pg * PAGE_SIZE, io_size,
2d21ac55 3686 UPL_ABORT_DUMP_PAGES | UPL_ABORT_FREE_ON_EMPTY);
b0d623f7
A
3687 else {
3688 int commit_flags = UPL_COMMIT_CLEAR_DIRTY | UPL_COMMIT_FREE_ON_EMPTY;
3689
3690 if (take_reference)
3691 commit_flags |= UPL_COMMIT_INACTIVATE;
3692 else
3693 commit_flags |= UPL_COMMIT_SPECULATE;
1c79356b 3694
b0d623f7
A
3695 ubc_upl_commit_range(upl, start_pg * PAGE_SIZE, io_size, commit_flags);
3696 }
3697 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 35)) | DBG_FUNC_END, upl, start_pg * PAGE_SIZE, io_size, error, 0);
1c79356b
A
3698 }
3699 if ((last_pg - start_pg) < pages_in_upl) {
1c79356b
A
3700 /*
3701 * the set of pages that we issued an I/O for did not encompass
3702 * the entire upl... so just release these without modifying
55e303ae 3703 * their state
1c79356b
A
3704 */
3705 if (error)
9bccf70c 3706 ubc_upl_abort_range(upl, 0, upl_size, UPL_ABORT_FREE_ON_EMPTY);
1c79356b 3707 else {
1c79356b 3708
2d21ac55 3709 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 35)) | DBG_FUNC_START,
b0d623f7 3710 upl, -1, pages_in_upl - (last_pg - start_pg), 0, 0);
2d21ac55
A
3711
3712 /*
3713 * handle any valid pages at the beginning of
3714 * the upl... release these appropriately
3715 */
b0d623f7 3716 cluster_read_upl_release(upl, 0, start_pg, take_reference);
2d21ac55
A
3717
3718 /*
3719 * handle any valid pages immediately after the
3720 * pages we issued I/O for... ... release these appropriately
3721 */
b0d623f7 3722 cluster_read_upl_release(upl, last_pg, uio_last, take_reference);
2d21ac55 3723
b0d623f7 3724 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 35)) | DBG_FUNC_END, upl, -1, -1, 0, 0);
1c79356b
A
3725 }
3726 }
3727 if (retval == 0)
3728 retval = error;
91447636 3729
2d21ac55 3730 if (io_req_size) {
b0d623f7 3731 if (cluster_hard_throttle_on(vp, 1)) {
91447636
A
3732 rd_ahead_enabled = 0;
3733 prefetch_enabled = 0;
3734
3735 max_rd_size = HARD_THROTTLE_MAXSIZE;
3736 } else {
2d21ac55
A
3737 if (max_rd_size == HARD_THROTTLE_MAXSIZE) {
3738 /*
3739 * coming out of throttled state
3740 */
b0d623f7
A
3741 if (policy != IOPOL_THROTTLE) {
3742 if (rap != NULL)
3743 rd_ahead_enabled = 1;
3744 prefetch_enabled = 1;
3745 }
cf7d32b8 3746 max_rd_size = max_prefetch;
2d21ac55
A
3747 last_ioread_offset = 0;
3748 }
91447636
A
3749 }
3750 }
3751 }
3752 if (rap != NULL) {
3753 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 32)) | DBG_FUNC_END,
2d21ac55 3754 (int)uio->uio_offset, io_req_size, rap->cl_lastr, retval, 0);
91447636
A
3755
3756 lck_mtx_unlock(&rap->cl_lockr);
3757 } else {
3758 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 32)) | DBG_FUNC_END,
2d21ac55 3759 (int)uio->uio_offset, io_req_size, 0, retval, 0);
1c79356b
A
3760 }
3761
3762 return (retval);
3763}
3764
b4c24cb9 3765
9bccf70c 3766static int
2d21ac55
A
3767cluster_read_direct(vnode_t vp, struct uio *uio, off_t filesize, int *read_type, u_int32_t *read_length,
3768 int flags, int (*callback)(buf_t, void *), void *callback_arg)
1c79356b
A
3769{
3770 upl_t upl;
3771 upl_page_info_t *pl;
2d21ac55 3772 off_t max_io_size;
b0d623f7
A
3773 vm_offset_t upl_offset, vector_upl_offset = 0;
3774 upl_size_t upl_size, vector_upl_size = 0;
2d21ac55
A
3775 vm_size_t upl_needed_size;
3776 unsigned int pages_in_pl;
1c79356b
A
3777 int upl_flags;
3778 kern_return_t kret;
2d21ac55 3779 unsigned int i;
1c79356b 3780 int force_data_sync;
1c79356b 3781 int retval = 0;
91447636
A
3782 int no_zero_fill = 0;
3783 int abort_flag = 0;
2d21ac55
A
3784 int io_flag = 0;
3785 int misaligned = 0;
d7e50217 3786 struct clios iostate;
2d21ac55
A
3787 user_addr_t iov_base;
3788 u_int32_t io_req_size;
3789 u_int32_t offset_in_file;
3790 u_int32_t offset_in_iovbase;
3791 u_int32_t io_size;
3792 u_int32_t io_min;
3793 u_int32_t xsize;
3794 u_int32_t devblocksize;
3795 u_int32_t mem_alignment_mask;
b0d623f7
A
3796 u_int32_t max_upl_size;
3797 u_int32_t max_rd_size;
3798 u_int32_t max_rd_ahead;
cf7d32b8 3799
b0d623f7
A
3800 u_int32_t vector_upl_iosize = 0;
3801 int issueVectorUPL = 0,useVectorUPL = (uio->uio_iovcnt > 1);
3802 off_t v_upl_uio_offset = 0;
3803 int vector_upl_index=0;
3804 upl_t vector_upl = NULL;
cf7d32b8 3805
b0d623f7
A
3806 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 70)) | DBG_FUNC_START,
3807 (int)uio->uio_offset, (int)filesize, *read_type, *read_length, 0);
cf7d32b8 3808
b0d623f7 3809 max_upl_size = cluster_max_io_size(vp->v_mount, CL_READ);
2d21ac55 3810
b0d623f7
A
3811 max_rd_size = max_upl_size;
3812 max_rd_ahead = max_rd_size * IO_SCALE(vp, 2);
1c79356b 3813
b0d623f7
A
3814 io_flag = CL_COMMIT | CL_READ | CL_ASYNC | CL_NOZERO | CL_DIRECT_IO;
3815 if (flags & IO_PASSIVE)
3816 io_flag |= CL_PASSIVE;
1c79356b 3817
d7e50217
A
3818 iostate.io_completed = 0;
3819 iostate.io_issued = 0;
3820 iostate.io_error = 0;
3821 iostate.io_wanted = 0;
3822
2d21ac55
A
3823 devblocksize = (u_int32_t)vp->v_mount->mnt_devblocksize;
3824 mem_alignment_mask = (u_int32_t)vp->v_mount->mnt_alignmentmask;
3825
3826 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 70)) | DBG_FUNC_NONE,
3827 (int)devblocksize, (int)mem_alignment_mask, 0, 0, 0);
3828
3829 if (devblocksize == 1) {
3830 /*
3831 * the AFP client advertises a devblocksize of 1
3832 * however, its BLOCKMAP routine maps to physical
3833 * blocks that are PAGE_SIZE in size...
3834 * therefore we can't ask for I/Os that aren't page aligned
3835 * or aren't multiples of PAGE_SIZE in size
3836 * by setting devblocksize to PAGE_SIZE, we re-instate
3837 * the old behavior we had before the mem_alignment_mask
3838 * changes went in...
3839 */
3840 devblocksize = PAGE_SIZE;
3841 }
3842next_dread:
3843 io_req_size = *read_length;
3844 iov_base = uio_curriovbase(uio);
3845
3846 max_io_size = filesize - uio->uio_offset;
3847
3848 if ((off_t)io_req_size > max_io_size)
3849 io_req_size = max_io_size;
3850
3851 offset_in_file = (u_int32_t)uio->uio_offset & (devblocksize - 1);
3852 offset_in_iovbase = (u_int32_t)iov_base & mem_alignment_mask;
3853
3854 if (offset_in_file || offset_in_iovbase) {
3855 /*
3856 * one of the 2 important offsets is misaligned
3857 * so fire an I/O through the cache for this entire vector
3858 */
3859 misaligned = 1;
3860 }
3861 if (iov_base & (devblocksize - 1)) {
3862 /*
3863 * the offset in memory must be on a device block boundary
3864 * so that we can guarantee that we can generate an
3865 * I/O that ends on a page boundary in cluster_io
3866 */
3867 misaligned = 1;
3868 }
3869 /*
3870 * When we get to this point, we know...
3871 * -- the offset into the file is on a devblocksize boundary
3872 */
3873
3874 while (io_req_size && retval == 0) {
3875 u_int32_t io_start;
1c79356b 3876
b0d623f7 3877 if (cluster_hard_throttle_on(vp, 1)) {
91447636
A
3878 max_rd_size = HARD_THROTTLE_MAXSIZE;
3879 max_rd_ahead = HARD_THROTTLE_MAXSIZE - 1;
3880 } else {
cf7d32b8 3881 max_rd_size = max_upl_size;
b0d623f7 3882 max_rd_ahead = max_rd_size * IO_SCALE(vp, 2);
91447636 3883 }
2d21ac55 3884 io_start = io_size = io_req_size;
1c79356b 3885
d7e50217
A
3886 /*
3887 * First look for pages already in the cache
3888 * and move them to user space.
2d21ac55
A
3889 *
3890 * cluster_copy_ubc_data returns the resid
3891 * in io_size
d7e50217 3892 */
2d21ac55 3893 retval = cluster_copy_ubc_data_internal(vp, uio, (int *)&io_size, 0, 0);
1c79356b 3894
2d21ac55
A
3895 /*
3896 * calculate the number of bytes actually copied
3897 * starting size - residual
3898 */
3899 xsize = io_start - io_size;
3900
3901 io_req_size -= xsize;
3902
b0d623f7
A
3903 if(useVectorUPL && (xsize || (iov_base & PAGE_MASK))) {
3904 /*
3905 * We found something in the cache or we have an iov_base that's not
3906 * page-aligned.
3907 *
3908 * Issue all I/O's that have been collected within this Vectored UPL.
3909 */
3910 if(vector_upl_index) {
3911 retval = vector_cluster_io(vp, vector_upl, vector_upl_offset, v_upl_uio_offset, vector_upl_iosize, io_flag, (buf_t)NULL, &iostate, callback, callback_arg);
3912 reset_vector_run_state();
3913 }
3914
3915 if(xsize)
3916 useVectorUPL = 0;
3917
3918 /*
3919 * After this point, if we are using the Vector UPL path and the base is
3920 * not page-aligned then the UPL with that base will be the first in the vector UPL.
3921 */
3922 }
3923
2d21ac55
A
3924 /*
3925 * check to see if we are finished with this request...
3926 */
3927 if (io_req_size == 0 || misaligned) {
3928 /*
3929 * see if there's another uio vector to
3930 * process that's of type IO_DIRECT
3931 *
3932 * break out of while loop to get there
d7e50217 3933 */
2d21ac55 3934 break;
0b4e3aa0 3935 }
d7e50217 3936 /*
2d21ac55 3937 * assume the request ends on a device block boundary
d7e50217 3938 */
2d21ac55
A
3939 io_min = devblocksize;
3940
3941 /*
3942 * we can handle I/O's in multiples of the device block size
3943 * however, if io_size isn't a multiple of devblocksize we
3944 * want to clip it back to the nearest page boundary since
3945 * we are going to have to go through cluster_read_copy to
3946 * deal with the 'overhang'... by clipping it to a PAGE_SIZE
3947 * multiple, we avoid asking the drive for the same physical
3948 * blocks twice.. once for the partial page at the end of the
3949 * request and a 2nd time for the page we read into the cache
3950 * (which overlaps the end of the direct read) in order to
3951 * get at the overhang bytes
3952 */
3953 if (io_size & (devblocksize - 1)) {
3954 /*
3955 * request does NOT end on a device block boundary
3956 * so clip it back to a PAGE_SIZE boundary
3957 */
3958 io_size &= ~PAGE_MASK;
3959 io_min = PAGE_SIZE;
3960 }
3961 if (retval || io_size < io_min) {
3962 /*
3963 * either an error or we only have the tail left to
3964 * complete via the copy path...
d7e50217
A
3965 * we may have already spun some portion of this request
3966 * off as async requests... we need to wait for the I/O
3967 * to complete before returning
3968 */
2d21ac55 3969 goto wait_for_dreads;
d7e50217 3970 }
2d21ac55
A
3971 if ((xsize = io_size) > max_rd_size)
3972 xsize = max_rd_size;
55e303ae 3973
d7e50217 3974 io_size = 0;
1c79356b 3975
2d21ac55 3976 ubc_range_op(vp, uio->uio_offset, uio->uio_offset + xsize, UPL_ROP_ABSENT, (int *)&io_size);
55e303ae 3977
2d21ac55 3978 if (io_size == 0) {
d7e50217 3979 /*
2d21ac55
A
3980 * a page must have just come into the cache
3981 * since the first page in this range is no
3982 * longer absent, go back and re-evaluate
d7e50217 3983 */
2d21ac55
A
3984 continue;
3985 }
cc9f6e38 3986 iov_base = uio_curriovbase(uio);
1c79356b 3987
2d21ac55 3988 upl_offset = (vm_offset_t)((u_int32_t)iov_base & PAGE_MASK);
d7e50217 3989 upl_needed_size = (upl_offset + io_size + (PAGE_SIZE -1)) & ~PAGE_MASK;
1c79356b 3990
d7e50217 3991 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 72)) | DBG_FUNC_START,
cc9f6e38 3992 (int)upl_offset, upl_needed_size, (int)iov_base, io_size, 0);
1c79356b 3993
91447636
A
3994 if (upl_offset == 0 && ((io_size & PAGE_MASK) == 0)) {
3995 no_zero_fill = 1;
3996 abort_flag = UPL_ABORT_DUMP_PAGES | UPL_ABORT_FREE_ON_EMPTY;
3997 } else {
3998 no_zero_fill = 0;
3999 abort_flag = UPL_ABORT_FREE_ON_EMPTY;
4000 }
d7e50217
A
4001 for (force_data_sync = 0; force_data_sync < 3; force_data_sync++) {
4002 pages_in_pl = 0;
4003 upl_size = upl_needed_size;
55e303ae 4004 upl_flags = UPL_FILE_IO | UPL_NO_SYNC | UPL_SET_INTERNAL | UPL_SET_LITE | UPL_SET_IO_WIRE;
1c79356b 4005
91447636
A
4006 if (no_zero_fill)
4007 upl_flags |= UPL_NOZEROFILL;
4008 if (force_data_sync)
4009 upl_flags |= UPL_FORCE_DATA_SYNC;
4010
91447636 4011 kret = vm_map_create_upl(current_map(),
cc9f6e38 4012 (vm_map_offset_t)(iov_base & ~((user_addr_t)PAGE_MASK)),
91447636 4013 &upl_size, &upl, NULL, &pages_in_pl, &upl_flags);
1c79356b 4014
d7e50217
A
4015 if (kret != KERN_SUCCESS) {
4016 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 72)) | DBG_FUNC_END,
4017 (int)upl_offset, upl_size, io_size, kret, 0);
d7e50217 4018 /*
2d21ac55 4019 * failed to get pagelist
d7e50217
A
4020 *
4021 * we may have already spun some portion of this request
4022 * off as async requests... we need to wait for the I/O
4023 * to complete before returning
4024 */
2d21ac55 4025 goto wait_for_dreads;
d7e50217
A
4026 }
4027 pages_in_pl = upl_size / PAGE_SIZE;
4028 pl = UPL_GET_INTERNAL_PAGE_LIST(upl);
1c79356b 4029
d7e50217
A
4030 for (i = 0; i < pages_in_pl; i++) {
4031 if (!upl_valid_page(pl, i))
4032 break;
4033 }
4034 if (i == pages_in_pl)
4035 break;
0b4e3aa0 4036
2d21ac55 4037 ubc_upl_abort(upl, abort_flag);
1c79356b 4038 }
d7e50217
A
4039 if (force_data_sync >= 3) {
4040 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 72)) | DBG_FUNC_END,
4041 (int)upl_offset, upl_size, io_size, kret, 0);
1c79356b 4042
2d21ac55 4043 goto wait_for_dreads;
d7e50217
A
4044 }
4045 /*
4046 * Consider the possibility that upl_size wasn't satisfied.
4047 */
2d21ac55
A
4048 if (upl_size < upl_needed_size) {
4049 if (upl_size && upl_offset == 0)
4050 io_size = upl_size;
4051 else
4052 io_size = 0;
4053 }
d7e50217 4054 if (io_size == 0) {
2d21ac55
A
4055 ubc_upl_abort(upl, abort_flag);
4056 goto wait_for_dreads;
d7e50217
A
4057 }
4058 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 72)) | DBG_FUNC_END,
4059 (int)upl_offset, upl_size, io_size, kret, 0);
1c79356b 4060
b0d623f7
A
4061 if(useVectorUPL) {
4062 vm_offset_t end_off = ((iov_base + io_size) & PAGE_MASK);
4063 if(end_off)
4064 issueVectorUPL = 1;
4065 /*
4066 * After this point, if we are using a vector UPL, then
4067 * either all the UPL elements end on a page boundary OR
4068 * this UPL is the last element because it does not end
4069 * on a page boundary.
4070 */
4071 }
4072
d7e50217
A
4073 /*
4074 * request asynchronously so that we can overlap
4075 * the preparation of the next I/O
4076 * if there are already too many outstanding reads
4077 * wait until some have completed before issuing the next read
4078 */
b0d623f7 4079 if (iostate.io_issued > iostate.io_completed) {
91447636 4080
b0d623f7 4081 lck_mtx_lock(cl_mtxp);
cf7d32b8 4082
b0d623f7
A
4083 while ((iostate.io_issued - iostate.io_completed) > max_rd_ahead) {
4084 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 95)) | DBG_FUNC_START,
4085 iostate.io_issued, iostate.io_completed, max_rd_ahead, 0, 0);
cf7d32b8 4086
b0d623f7
A
4087 iostate.io_wanted = 1;
4088 msleep((caddr_t)&iostate.io_wanted, cl_mtxp, PRIBIO + 1, "cluster_read_direct", NULL);
4089
4090 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 95)) | DBG_FUNC_END,
4091 iostate.io_issued, iostate.io_completed, max_rd_ahead, 0, 0);
4092 }
4093 lck_mtx_unlock(cl_mtxp);
4094 }
d7e50217
A
4095 if (iostate.io_error) {
4096 /*
4097 * one of the earlier reads we issued ran into a hard error
4098 * don't issue any more reads, cleanup the UPL
4099 * that was just created but not used, then
4100 * go wait for any other reads to complete before
4101 * returning the error to the caller
4102 */
2d21ac55 4103 ubc_upl_abort(upl, abort_flag);
1c79356b 4104
2d21ac55 4105 goto wait_for_dreads;
d7e50217
A
4106 }
4107 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 73)) | DBG_FUNC_START,
b0d623f7 4108 upl, (int)upl_offset, (int)uio->uio_offset, io_size, 0);
1c79356b 4109
2d21ac55 4110
b0d623f7
A
4111 if(!useVectorUPL) {
4112 if (no_zero_fill)
4113 io_flag &= ~CL_PRESERVE;
4114 else
4115 io_flag |= CL_PRESERVE;
4116
4117 retval = cluster_io(vp, upl, upl_offset, uio->uio_offset, io_size, io_flag, (buf_t)NULL, &iostate, callback, callback_arg);
4118
4119 } else {
1c79356b 4120
b0d623f7
A
4121 if(!vector_upl_index) {
4122 vector_upl = vector_upl_create(upl_offset);
4123 v_upl_uio_offset = uio->uio_offset;
4124 vector_upl_offset = upl_offset;
4125 }
4126
4127 vector_upl_set_subupl(vector_upl,upl, upl_size);
4128 vector_upl_set_iostate(vector_upl, upl, vector_upl_size, upl_size);
4129 vector_upl_index++;
4130 vector_upl_size += upl_size;
4131 vector_upl_iosize += io_size;
4132
4133 if(issueVectorUPL || vector_upl_index == MAX_VECTOR_UPL_ELEMENTS || vector_upl_size >= MAX_VECTOR_UPL_SIZE) {
4134 retval = vector_cluster_io(vp, vector_upl, vector_upl_offset, v_upl_uio_offset, vector_upl_iosize, io_flag, (buf_t)NULL, &iostate, callback, callback_arg);
4135 reset_vector_run_state();
4136 }
4137 }
d7e50217
A
4138 /*
4139 * update the uio structure
4140 */
cc9f6e38 4141 uio_update(uio, (user_size_t)io_size);
1c79356b 4142
2d21ac55
A
4143 io_req_size -= io_size;
4144
d7e50217 4145 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 73)) | DBG_FUNC_END,
b0d623f7 4146 upl, (int)uio->uio_offset, io_req_size, retval, 0);
1c79356b
A
4147
4148 } /* end while */
4149
2d21ac55 4150 if (retval == 0 && iostate.io_error == 0 && io_req_size == 0 && uio->uio_offset < filesize) {
91447636 4151
2d21ac55
A
4152 retval = cluster_io_type(uio, read_type, read_length, 0);
4153
4154 if (retval == 0 && *read_type == IO_DIRECT) {
4155
4156 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 70)) | DBG_FUNC_NONE,
4157 (int)uio->uio_offset, (int)filesize, *read_type, *read_length, 0);
4158
4159 goto next_dread;
4160 }
4161 }
4162
4163wait_for_dreads:
b0d623f7
A
4164
4165 if(retval == 0 && iostate.io_error == 0 && useVectorUPL && vector_upl_index) {
4166 retval = vector_cluster_io(vp, vector_upl, vector_upl_offset, v_upl_uio_offset, vector_upl_iosize, io_flag, (buf_t)NULL, &iostate, callback, callback_arg);
4167 reset_vector_run_state();
4168 }
4169 /*
4170 * make sure all async reads that are part of this stream
4171 * have completed before we return
4172 */
4173 if (iostate.io_issued > iostate.io_completed) {
4174
2d21ac55
A
4175 lck_mtx_lock(cl_mtxp);
4176
4177 while (iostate.io_issued != iostate.io_completed) {
b0d623f7
A
4178 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 95)) | DBG_FUNC_START,
4179 iostate.io_issued, iostate.io_completed, 0, 0, 0);
cf7d32b8 4180
2d21ac55
A
4181 iostate.io_wanted = 1;
4182 msleep((caddr_t)&iostate.io_wanted, cl_mtxp, PRIBIO + 1, "cluster_read_direct", NULL);
cf7d32b8 4183
b0d623f7
A
4184 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 95)) | DBG_FUNC_END,
4185 iostate.io_issued, iostate.io_completed, 0, 0, 0);
2d21ac55
A
4186 }
4187 lck_mtx_unlock(cl_mtxp);
4188 }
d7e50217 4189 if (iostate.io_error)
2d21ac55
A
4190 retval = iostate.io_error;
4191
4192 if (io_req_size && retval == 0) {
4193 /*
4194 * we couldn't handle the tail of this request in DIRECT mode
4195 * so fire it through the copy path
4196 */
4197 retval = cluster_read_copy(vp, uio, io_req_size, filesize, flags, callback, callback_arg);
1c79356b 4198
2d21ac55
A
4199 *read_type = IO_UNKNOWN;
4200 }
1c79356b 4201 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 70)) | DBG_FUNC_END,
2d21ac55 4202 (int)uio->uio_offset, (int)uio_resid(uio), io_req_size, retval, 0);
1c79356b
A
4203
4204 return (retval);
4205}
4206
4207
9bccf70c 4208static int
2d21ac55
A
4209cluster_read_contig(vnode_t vp, struct uio *uio, off_t filesize, int *read_type, u_int32_t *read_length,
4210 int (*callback)(buf_t, void *), void *callback_arg, int flags)
0b4e3aa0 4211{
b4c24cb9 4212 upl_page_info_t *pl;
2d21ac55 4213 upl_t upl[MAX_VECTS];
0b4e3aa0 4214 vm_offset_t upl_offset;
2d21ac55 4215 addr64_t dst_paddr = 0;
cc9f6e38 4216 user_addr_t iov_base;
2d21ac55 4217 off_t max_size;
b0d623f7 4218 upl_size_t upl_size;
2d21ac55
A
4219 vm_size_t upl_needed_size;
4220 mach_msg_type_number_t pages_in_pl;
0b4e3aa0
A
4221 int upl_flags;
4222 kern_return_t kret;
b4c24cb9 4223 struct clios iostate;
2d21ac55
A
4224 int error= 0;
4225 int cur_upl = 0;
4226 int num_upl = 0;
4227 int n;
4228 u_int32_t xsize;
4229 u_int32_t io_size;
4230 u_int32_t devblocksize;
4231 u_int32_t mem_alignment_mask;
4232 u_int32_t tail_size = 0;
4233 int bflag;
4234
4235 if (flags & IO_PASSIVE)
b0d623f7 4236 bflag = CL_PASSIVE;
2d21ac55 4237 else
b0d623f7 4238 bflag = 0;
0b4e3aa0
A
4239
4240 /*
4241 * When we enter this routine, we know
2d21ac55
A
4242 * -- the read_length will not exceed the current iov_len
4243 * -- the target address is physically contiguous for read_length
0b4e3aa0 4244 */
2d21ac55 4245 cluster_syncup(vp, filesize, callback, callback_arg);
0b4e3aa0 4246
2d21ac55
A
4247 devblocksize = (u_int32_t)vp->v_mount->mnt_devblocksize;
4248 mem_alignment_mask = (u_int32_t)vp->v_mount->mnt_alignmentmask;
91447636 4249
2d21ac55
A
4250 iostate.io_completed = 0;
4251 iostate.io_issued = 0;
4252 iostate.io_error = 0;
4253 iostate.io_wanted = 0;
4254
4255next_cread:
4256 io_size = *read_length;
0b4e3aa0
A
4257
4258 max_size = filesize - uio->uio_offset;
4259
2d21ac55 4260 if (io_size > max_size)
b4c24cb9 4261 io_size = max_size;
0b4e3aa0 4262
2d21ac55
A
4263 iov_base = uio_curriovbase(uio);
4264
4265 upl_offset = (vm_offset_t)((u_int32_t)iov_base & PAGE_MASK);
0b4e3aa0
A
4266 upl_needed_size = upl_offset + io_size;
4267
4268 pages_in_pl = 0;
4269 upl_size = upl_needed_size;
55e303ae 4270 upl_flags = UPL_FILE_IO | UPL_NO_SYNC | UPL_CLEAN_IN_PLACE | UPL_SET_INTERNAL | UPL_SET_LITE | UPL_SET_IO_WIRE;
0b4e3aa0 4271
2d21ac55
A
4272
4273 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 92)) | DBG_FUNC_START,
4274 (int)upl_offset, (int)upl_size, (int)iov_base, io_size, 0);
4275
0b4e3aa0 4276 kret = vm_map_get_upl(current_map(),
cc9f6e38 4277 (vm_map_offset_t)(iov_base & ~((user_addr_t)PAGE_MASK)),
2d21ac55
A
4278 &upl_size, &upl[cur_upl], NULL, &pages_in_pl, &upl_flags, 0);
4279
4280 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 92)) | DBG_FUNC_END,
4281 (int)upl_offset, upl_size, io_size, kret, 0);
0b4e3aa0 4282
b4c24cb9
A
4283 if (kret != KERN_SUCCESS) {
4284 /*
2d21ac55 4285 * failed to get pagelist
b4c24cb9 4286 */
2d21ac55
A
4287 error = EINVAL;
4288 goto wait_for_creads;
b4c24cb9 4289 }
2d21ac55
A
4290 num_upl++;
4291
b4c24cb9
A
4292 if (upl_size < upl_needed_size) {
4293 /*
4294 * The upl_size wasn't satisfied.
4295 */
2d21ac55
A
4296 error = EINVAL;
4297 goto wait_for_creads;
b4c24cb9 4298 }
2d21ac55 4299 pl = ubc_upl_pageinfo(upl[cur_upl]);
b4c24cb9 4300
cc9f6e38 4301 dst_paddr = ((addr64_t)upl_phys_page(pl, 0) << 12) + (addr64_t)upl_offset;
0b4e3aa0 4302
b4c24cb9 4303 while (((uio->uio_offset & (devblocksize - 1)) || io_size < devblocksize) && io_size) {
2d21ac55 4304 u_int32_t head_size;
b4c24cb9 4305
2d21ac55 4306 head_size = devblocksize - (u_int32_t)(uio->uio_offset & (devblocksize - 1));
b4c24cb9
A
4307
4308 if (head_size > io_size)
4309 head_size = io_size;
4310
2d21ac55 4311 error = cluster_align_phys_io(vp, uio, dst_paddr, head_size, CL_READ, callback, callback_arg);
b4c24cb9 4312
2d21ac55
A
4313 if (error)
4314 goto wait_for_creads;
b4c24cb9 4315
b4c24cb9
A
4316 upl_offset += head_size;
4317 dst_paddr += head_size;
4318 io_size -= head_size;
2d21ac55
A
4319
4320 iov_base += head_size;
4321 }
4322 if ((u_int32_t)iov_base & mem_alignment_mask) {
4323 /*
4324 * request doesn't set up on a memory boundary
4325 * the underlying DMA engine can handle...
4326 * return an error instead of going through
4327 * the slow copy path since the intent of this
4328 * path is direct I/O to device memory
4329 */
4330 error = EINVAL;
4331 goto wait_for_creads;
b4c24cb9 4332 }
2d21ac55 4333
b4c24cb9 4334 tail_size = io_size & (devblocksize - 1);
b4c24cb9 4335
2d21ac55 4336 io_size -= tail_size;
b4c24cb9
A
4337
4338 while (io_size && error == 0) {
b4c24cb9 4339
2d21ac55
A
4340 if (io_size > MAX_IO_CONTIG_SIZE)
4341 xsize = MAX_IO_CONTIG_SIZE;
b4c24cb9
A
4342 else
4343 xsize = io_size;
4344 /*
4345 * request asynchronously so that we can overlap
4346 * the preparation of the next I/O... we'll do
4347 * the commit after all the I/O has completed
4348 * since its all issued against the same UPL
4349 * if there are already too many outstanding reads
d7e50217 4350 * wait until some have completed before issuing the next
b4c24cb9 4351 */
b0d623f7 4352 if (iostate.io_issued > iostate.io_completed) {
2d21ac55 4353 lck_mtx_lock(cl_mtxp);
b4c24cb9 4354
b0d623f7
A
4355 while ((iostate.io_issued - iostate.io_completed) > (MAX_IO_CONTIG_SIZE * IO_SCALE(vp, 2))) {
4356 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 95)) | DBG_FUNC_START,
4357 iostate.io_issued, iostate.io_completed, MAX_IO_CONTIG_SIZE * IO_SCALE(vp, 2), 0, 0);
cf7d32b8 4358
2d21ac55
A
4359 iostate.io_wanted = 1;
4360 msleep((caddr_t)&iostate.io_wanted, cl_mtxp, PRIBIO + 1, "cluster_read_contig", NULL);
cf7d32b8 4361
b0d623f7
A
4362 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 95)) | DBG_FUNC_END,
4363 iostate.io_issued, iostate.io_completed, MAX_IO_CONTIG_SIZE * IO_SCALE(vp, 2), 0, 0);
2d21ac55
A
4364 }
4365 lck_mtx_unlock(cl_mtxp);
4366 }
4367 if (iostate.io_error) {
4368 /*
4369 * one of the earlier reads we issued ran into a hard error
4370 * don't issue any more reads...
4371 * go wait for any other reads to complete before
4372 * returning the error to the caller
4373 */
4374 goto wait_for_creads;
4375 }
4376 error = cluster_io(vp, upl[cur_upl], upl_offset, uio->uio_offset, xsize,
4377 CL_READ | CL_NOZERO | CL_DEV_MEMORY | CL_ASYNC | bflag,
4378 (buf_t)NULL, &iostate, callback, callback_arg);
b4c24cb9
A
4379 /*
4380 * The cluster_io read was issued successfully,
4381 * update the uio structure
4382 */
4383 if (error == 0) {
cc9f6e38
A
4384 uio_update(uio, (user_size_t)xsize);
4385
4386 dst_paddr += xsize;
4387 upl_offset += xsize;
4388 io_size -= xsize;
b4c24cb9
A
4389 }
4390 }
2d21ac55
A
4391 if (error == 0 && iostate.io_error == 0 && tail_size == 0 && num_upl < MAX_VECTS && uio->uio_offset < filesize) {
4392
4393 error = cluster_io_type(uio, read_type, read_length, 0);
4394
4395 if (error == 0 && *read_type == IO_CONTIG) {
4396 cur_upl++;
4397 goto next_cread;
4398 }
4399 } else
4400 *read_type = IO_UNKNOWN;
4401
4402wait_for_creads:
0b4e3aa0 4403 /*
d7e50217
A
4404 * make sure all async reads that are part of this stream
4405 * have completed before we proceed
0b4e3aa0 4406 */
b0d623f7 4407 if (iostate.io_issued > iostate.io_completed) {
91447636 4408
b0d623f7 4409 lck_mtx_lock(cl_mtxp);
cf7d32b8 4410
b0d623f7
A
4411 while (iostate.io_issued != iostate.io_completed) {
4412 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 95)) | DBG_FUNC_START,
4413 iostate.io_issued, iostate.io_completed, 0, 0, 0);
cf7d32b8 4414
b0d623f7
A
4415 iostate.io_wanted = 1;
4416 msleep((caddr_t)&iostate.io_wanted, cl_mtxp, PRIBIO + 1, "cluster_read_contig", NULL);
91447636 4417
b0d623f7
A
4418 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 95)) | DBG_FUNC_END,
4419 iostate.io_issued, iostate.io_completed, 0, 0, 0);
4420 }
4421 lck_mtx_unlock(cl_mtxp);
4422 }
91447636 4423 if (iostate.io_error)
b4c24cb9 4424 error = iostate.io_error;
91447636 4425
b4c24cb9 4426 if (error == 0 && tail_size)
2d21ac55 4427 error = cluster_align_phys_io(vp, uio, dst_paddr, tail_size, CL_READ, callback, callback_arg);
0b4e3aa0 4428
2d21ac55
A
4429 for (n = 0; n < num_upl; n++)
4430 /*
4431 * just release our hold on each physically contiguous
4432 * region without changing any state
4433 */
4434 ubc_upl_abort(upl[n], 0);
0b4e3aa0
A
4435
4436 return (error);
4437}
1c79356b 4438
b4c24cb9 4439
2d21ac55
A
4440static int
4441cluster_io_type(struct uio *uio, int *io_type, u_int32_t *io_length, u_int32_t min_length)
4442{
4443 user_size_t iov_len;
4444 user_addr_t iov_base = 0;
4445 upl_t upl;
b0d623f7 4446 upl_size_t upl_size;
2d21ac55
A
4447 int upl_flags;
4448 int retval = 0;
4449
4450 /*
4451 * skip over any emtpy vectors
4452 */
4453 uio_update(uio, (user_size_t)0);
4454
4455 iov_len = uio_curriovlen(uio);
4456
b0d623f7 4457 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 94)) | DBG_FUNC_START, uio, (int)iov_len, 0, 0, 0);
2d21ac55
A
4458
4459 if (iov_len) {
4460 iov_base = uio_curriovbase(uio);
4461 /*
4462 * make sure the size of the vector isn't too big...
4463 * internally, we want to handle all of the I/O in
4464 * chunk sizes that fit in a 32 bit int
4465 */
4466 if (iov_len > (user_size_t)MAX_IO_REQUEST_SIZE)
4467 upl_size = MAX_IO_REQUEST_SIZE;
4468 else
4469 upl_size = (u_int32_t)iov_len;
4470
4471 upl_flags = UPL_QUERY_OBJECT_TYPE;
4472
4473 if ((vm_map_get_upl(current_map(),
4474 (vm_map_offset_t)(iov_base & ~((user_addr_t)PAGE_MASK)),
4475 &upl_size, &upl, NULL, NULL, &upl_flags, 0)) != KERN_SUCCESS) {
4476 /*
4477 * the user app must have passed in an invalid address
4478 */
4479 retval = EFAULT;
4480 }
4481 if (upl_size == 0)
4482 retval = EFAULT;
4483
4484 *io_length = upl_size;
4485
4486 if (upl_flags & UPL_PHYS_CONTIG)
4487 *io_type = IO_CONTIG;
4488 else if (iov_len >= min_length)
4489 *io_type = IO_DIRECT;
4490 else
4491 *io_type = IO_COPY;
4492 } else {
4493 /*
4494 * nothing left to do for this uio
4495 */
4496 *io_length = 0;
4497 *io_type = IO_UNKNOWN;
4498 }
b0d623f7 4499 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 94)) | DBG_FUNC_END, iov_base, *io_type, *io_length, retval, 0);
2d21ac55
A
4500
4501 return (retval);
4502}
4503
4504
1c79356b
A
4505/*
4506 * generate advisory I/O's in the largest chunks possible
4507 * the completed pages will be released into the VM cache
4508 */
9bccf70c 4509int
91447636 4510advisory_read(vnode_t vp, off_t filesize, off_t f_offset, int resid)
2d21ac55
A
4511{
4512 return advisory_read_ext(vp, filesize, f_offset, resid, NULL, NULL, CL_PASSIVE);
4513}
4514
4515int
4516advisory_read_ext(vnode_t vp, off_t filesize, off_t f_offset, int resid, int (*callback)(buf_t, void *), void *callback_arg, int bflag)
1c79356b 4517{
1c79356b
A
4518 upl_page_info_t *pl;
4519 upl_t upl;
4520 vm_offset_t upl_offset;
b0d623f7 4521 int upl_size;
1c79356b
A
4522 off_t upl_f_offset;
4523 int start_offset;
4524 int start_pg;
4525 int last_pg;
4526 int pages_in_upl;
4527 off_t max_size;
4528 int io_size;
4529 kern_return_t kret;
4530 int retval = 0;
9bccf70c 4531 int issued_io;
55e303ae 4532 int skip_range;
b0d623f7
A
4533 uint32_t max_io_size;
4534
4535
91447636 4536 if ( !UBCINFOEXISTS(vp))
1c79356b
A
4537 return(EINVAL);
4538
ca66cea6
A
4539 if (resid < 0)
4540 return(EINVAL);
4541
cf7d32b8 4542 max_io_size = cluster_max_io_size(vp->v_mount, CL_READ);
b0d623f7 4543
1c79356b 4544 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 60)) | DBG_FUNC_START,
b0d623f7 4545 (int)f_offset, resid, (int)filesize, 0, 0);
1c79356b
A
4546
4547 while (resid && f_offset < filesize && retval == 0) {
4548 /*
4549 * compute the size of the upl needed to encompass
4550 * the requested read... limit each call to cluster_io
0b4e3aa0
A
4551 * to the maximum UPL size... cluster_io will clip if
4552 * this exceeds the maximum io_size for the device,
4553 * make sure to account for
1c79356b
A
4554 * a starting offset that's not page aligned
4555 */
4556 start_offset = (int)(f_offset & PAGE_MASK_64);
4557 upl_f_offset = f_offset - (off_t)start_offset;
4558 max_size = filesize - f_offset;
4559
4560 if (resid < max_size)
4561 io_size = resid;
4562 else
4563 io_size = max_size;
4564
4565 upl_size = (start_offset + io_size + (PAGE_SIZE - 1)) & ~PAGE_MASK;
cf7d32b8
A
4566 if ((uint32_t)upl_size > max_io_size)
4567 upl_size = max_io_size;
55e303ae
A
4568
4569 skip_range = 0;
4570 /*
4571 * return the number of contiguously present pages in the cache
4572 * starting at upl_f_offset within the file
4573 */
4574 ubc_range_op(vp, upl_f_offset, upl_f_offset + upl_size, UPL_ROP_PRESENT, &skip_range);
4575
4576 if (skip_range) {
4577 /*
4578 * skip over pages already present in the cache
4579 */
4580 io_size = skip_range - start_offset;
4581
4582 f_offset += io_size;
4583 resid -= io_size;
4584
4585 if (skip_range == upl_size)
4586 continue;
4587 /*
4588 * have to issue some real I/O
4589 * at this point, we know it's starting on a page boundary
4590 * because we've skipped over at least the first page in the request
4591 */
4592 start_offset = 0;
4593 upl_f_offset += skip_range;
4594 upl_size -= skip_range;
4595 }
1c79356b
A
4596 pages_in_upl = upl_size / PAGE_SIZE;
4597
55e303ae 4598 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 61)) | DBG_FUNC_START,
b0d623f7 4599 upl, (int)upl_f_offset, upl_size, start_offset, 0);
55e303ae 4600
0b4e3aa0 4601 kret = ubc_create_upl(vp,
91447636
A
4602 upl_f_offset,
4603 upl_size,
4604 &upl,
4605 &pl,
4606 UPL_RET_ONLY_ABSENT | UPL_SET_LITE);
1c79356b 4607 if (kret != KERN_SUCCESS)
9bccf70c
A
4608 return(retval);
4609 issued_io = 0;
1c79356b
A
4610
4611 /*
9bccf70c
A
4612 * before we start marching forward, we must make sure we end on
4613 * a present page, otherwise we will be working with a freed
4614 * upl
1c79356b 4615 */
9bccf70c
A
4616 for (last_pg = pages_in_upl - 1; last_pg >= 0; last_pg--) {
4617 if (upl_page_present(pl, last_pg))
4618 break;
1c79356b 4619 }
9bccf70c 4620 pages_in_upl = last_pg + 1;
1c79356b 4621
1c79356b 4622
55e303ae 4623 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 61)) | DBG_FUNC_END,
b0d623f7 4624 upl, (int)upl_f_offset, upl_size, start_offset, 0);
9bccf70c
A
4625
4626
4627 for (last_pg = 0; last_pg < pages_in_upl; ) {
1c79356b 4628 /*
9bccf70c
A
4629 * scan from the beginning of the upl looking for the first
4630 * page that is present.... this will become the first page in
4631 * the request we're going to make to 'cluster_io'... if all
4632 * of the pages are absent, we won't call through to 'cluster_io'
1c79356b 4633 */
9bccf70c
A
4634 for (start_pg = last_pg; start_pg < pages_in_upl; start_pg++) {
4635 if (upl_page_present(pl, start_pg))
4636 break;
1c79356b 4637 }
1c79356b 4638
1c79356b 4639 /*
9bccf70c
A
4640 * scan from the starting present page looking for an absent
4641 * page before the end of the upl is reached, if we
4642 * find one, then it will terminate the range of pages being
4643 * presented to 'cluster_io'
1c79356b 4644 */
9bccf70c
A
4645 for (last_pg = start_pg; last_pg < pages_in_upl; last_pg++) {
4646 if (!upl_page_present(pl, last_pg))
4647 break;
4648 }
4649
4650 if (last_pg > start_pg) {
4651 /*
4652 * we found a range of pages that must be filled
4653 * if the last page in this range is the last page of the file
4654 * we may have to clip the size of it to keep from reading past
4655 * the end of the last physical block associated with the file
4656 */
4657 upl_offset = start_pg * PAGE_SIZE;
4658 io_size = (last_pg - start_pg) * PAGE_SIZE;
4659
b0d623f7 4660 if ((off_t)(upl_f_offset + upl_offset + io_size) > filesize)
9bccf70c
A
4661 io_size = filesize - (upl_f_offset + upl_offset);
4662
4663 /*
4664 * issue an asynchronous read to cluster_io
4665 */
91447636 4666 retval = cluster_io(vp, upl, upl_offset, upl_f_offset + upl_offset, io_size,
2d21ac55 4667 CL_ASYNC | CL_READ | CL_COMMIT | CL_AGE | bflag, (buf_t)NULL, (struct clios *)NULL, callback, callback_arg);
1c79356b 4668
9bccf70c
A
4669 issued_io = 1;
4670 }
1c79356b 4671 }
9bccf70c
A
4672 if (issued_io == 0)
4673 ubc_upl_abort(upl, 0);
4674
4675 io_size = upl_size - start_offset;
1c79356b
A
4676
4677 if (io_size > resid)
4678 io_size = resid;
4679 f_offset += io_size;
4680 resid -= io_size;
4681 }
9bccf70c 4682
1c79356b
A
4683 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 60)) | DBG_FUNC_END,
4684 (int)f_offset, resid, retval, 0, 0);
4685
4686 return(retval);
4687}
4688
4689
9bccf70c 4690int
91447636 4691cluster_push(vnode_t vp, int flags)
2d21ac55
A
4692{
4693 return cluster_push_ext(vp, flags, NULL, NULL);
4694}
4695
4696
4697int
4698cluster_push_ext(vnode_t vp, int flags, int (*callback)(buf_t, void *), void *callback_arg)
9bccf70c 4699{
91447636 4700 int retval;
b0d623f7 4701 int my_sparse_wait = 0;
91447636 4702 struct cl_writebehind *wbp;
9bccf70c 4703
91447636 4704 if ( !UBCINFOEXISTS(vp)) {
b0d623f7 4705 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 53)) | DBG_FUNC_NONE, vp, flags, 0, -1, 0);
91447636
A
4706 return (0);
4707 }
4708 /* return if deferred write is set */
4709 if (((unsigned int)vfs_flags(vp->v_mount) & MNT_DEFWRITE) && (flags & IO_DEFWRITE)) {
4710 return (0);
4711 }
4712 if ((wbp = cluster_get_wbp(vp, CLW_RETURNLOCKED)) == NULL) {
b0d623f7 4713 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 53)) | DBG_FUNC_NONE, vp, flags, 0, -2, 0);
91447636
A
4714 return (0);
4715 }
4716 if (wbp->cl_number == 0 && wbp->cl_scmap == NULL) {
4717 lck_mtx_unlock(&wbp->cl_lockw);
9bccf70c 4718
b0d623f7 4719 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 53)) | DBG_FUNC_NONE, vp, flags, 0, -3, 0);
91447636
A
4720 return(0);
4721 }
9bccf70c 4722 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 53)) | DBG_FUNC_START,
b0d623f7
A
4723 wbp->cl_scmap, wbp->cl_number, flags, 0, 0);
4724
4725 /*
4726 * if we have an fsync in progress, we don't want to allow any additional
4727 * sync/fsync/close(s) to occur until it finishes.
4728 * note that its possible for writes to continue to occur to this file
4729 * while we're waiting and also once the fsync starts to clean if we're
4730 * in the sparse map case
4731 */
4732 while (wbp->cl_sparse_wait) {
4733 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 97)) | DBG_FUNC_START, vp, 0, 0, 0, 0);
4734
4735 msleep((caddr_t)&wbp->cl_sparse_wait, &wbp->cl_lockw, PRIBIO + 1, "cluster_push_ext", NULL);
4736
4737 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 97)) | DBG_FUNC_END, vp, 0, 0, 0, 0);
4738 }
4739 if (flags & IO_SYNC) {
4740 my_sparse_wait = 1;
4741 wbp->cl_sparse_wait = 1;
9bccf70c 4742
b0d623f7
A
4743 /*
4744 * this is an fsync (or equivalent)... we must wait for any existing async
4745 * cleaning operations to complete before we evaulate the current state
4746 * and finish cleaning... this insures that all writes issued before this
4747 * fsync actually get cleaned to the disk before this fsync returns
4748 */
4749 while (wbp->cl_sparse_pushes) {
4750 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 98)) | DBG_FUNC_START, vp, 0, 0, 0, 0);
4751
4752 msleep((caddr_t)&wbp->cl_sparse_pushes, &wbp->cl_lockw, PRIBIO + 1, "cluster_push_ext", NULL);
4753
4754 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 98)) | DBG_FUNC_END, vp, 0, 0, 0, 0);
4755 }
4756 }
91447636 4757 if (wbp->cl_scmap) {
b0d623f7
A
4758 void *scmap;
4759
4760 if (wbp->cl_sparse_pushes < SPARSE_PUSH_LIMIT) {
4761
4762 scmap = wbp->cl_scmap;
4763 wbp->cl_scmap = NULL;
4764
4765 wbp->cl_sparse_pushes++;
4766
4767 lck_mtx_unlock(&wbp->cl_lockw);
4768
4769 sparse_cluster_push(&scmap, vp, ubc_getsize(vp), PUSH_ALL | IO_PASSIVE, callback, callback_arg);
4770
4771 lck_mtx_lock(&wbp->cl_lockw);
9bccf70c 4772
b0d623f7
A
4773 wbp->cl_sparse_pushes--;
4774
4775 if (wbp->cl_sparse_wait && wbp->cl_sparse_pushes == 0)
4776 wakeup((caddr_t)&wbp->cl_sparse_pushes);
4777 } else {
4778 sparse_cluster_push(&(wbp->cl_scmap), vp, ubc_getsize(vp), PUSH_ALL | IO_PASSIVE, callback, callback_arg);
4779 }
55e303ae 4780 retval = 1;
b0d623f7 4781 } else {
2d21ac55 4782 retval = cluster_try_push(wbp, vp, ubc_getsize(vp), PUSH_ALL | IO_PASSIVE, callback, callback_arg);
b0d623f7 4783 }
91447636
A
4784 lck_mtx_unlock(&wbp->cl_lockw);
4785
4786 if (flags & IO_SYNC)
2d21ac55 4787 (void)vnode_waitforwrites(vp, 0, 0, 0, "cluster_push");
9bccf70c 4788
b0d623f7
A
4789 if (my_sparse_wait) {
4790 /*
4791 * I'm the owner of the serialization token
4792 * clear it and wakeup anyone that is waiting
4793 * for me to finish
4794 */
4795 lck_mtx_lock(&wbp->cl_lockw);
4796
4797 wbp->cl_sparse_wait = 0;
4798 wakeup((caddr_t)&wbp->cl_sparse_wait);
4799
4800 lck_mtx_unlock(&wbp->cl_lockw);
4801 }
55e303ae 4802 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 53)) | DBG_FUNC_END,
b0d623f7 4803 wbp->cl_scmap, wbp->cl_number, retval, 0, 0);
9bccf70c 4804
55e303ae
A
4805 return (retval);
4806}
9bccf70c 4807
9bccf70c 4808
91447636
A
4809__private_extern__ void
4810cluster_release(struct ubc_info *ubc)
55e303ae 4811{
91447636
A
4812 struct cl_writebehind *wbp;
4813 struct cl_readahead *rap;
4814
4815 if ((wbp = ubc->cl_wbehind)) {
9bccf70c 4816
b0d623f7 4817 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 81)) | DBG_FUNC_START, ubc, wbp->cl_scmap, 0, 0, 0);
91447636
A
4818
4819 if (wbp->cl_scmap)
4820 vfs_drt_control(&(wbp->cl_scmap), 0);
4821 } else {
b0d623f7 4822 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 81)) | DBG_FUNC_START, ubc, 0, 0, 0, 0);
91447636 4823 }
9bccf70c 4824
91447636 4825 rap = ubc->cl_rahead;
55e303ae 4826
91447636
A
4827 if (wbp != NULL) {
4828 lck_mtx_destroy(&wbp->cl_lockw, cl_mtx_grp);
4829 FREE_ZONE((void *)wbp, sizeof *wbp, M_CLWRBEHIND);
4830 }
4831 if ((rap = ubc->cl_rahead)) {
4832 lck_mtx_destroy(&rap->cl_lockr, cl_mtx_grp);
4833 FREE_ZONE((void *)rap, sizeof *rap, M_CLRDAHEAD);
55e303ae 4834 }
91447636
A
4835 ubc->cl_rahead = NULL;
4836 ubc->cl_wbehind = NULL;
4837
b0d623f7 4838 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 81)) | DBG_FUNC_END, ubc, rap, wbp, 0, 0);
91447636
A
4839}
4840
4841
9bccf70c 4842static int
2d21ac55 4843cluster_try_push(struct cl_writebehind *wbp, vnode_t vp, off_t EOF, int push_flag, int (*callback)(buf_t, void *), void *callback_arg)
9bccf70c
A
4844{
4845 int cl_index;
4846 int cl_index1;
4847 int min_index;
4848 int cl_len;
55e303ae 4849 int cl_pushed = 0;
91447636 4850 struct cl_wextent l_clusters[MAX_CLUSTERS];
b0d623f7
A
4851 u_int max_cluster_pgcount;
4852
4853
4854 max_cluster_pgcount = MAX_CLUSTER_SIZE(vp) / PAGE_SIZE;
9bccf70c 4855 /*
91447636
A
4856 * the write behind context exists and has
4857 * already been locked...
2d21ac55
A
4858 */
4859 if (wbp->cl_number == 0)
4860 /*
4861 * no clusters to push
4862 * return number of empty slots
4863 */
4864 return (MAX_CLUSTERS);
4865
4866 /*
9bccf70c 4867 * make a local 'sorted' copy of the clusters
91447636 4868 * and clear wbp->cl_number so that new clusters can
9bccf70c
A
4869 * be developed
4870 */
91447636
A
4871 for (cl_index = 0; cl_index < wbp->cl_number; cl_index++) {
4872 for (min_index = -1, cl_index1 = 0; cl_index1 < wbp->cl_number; cl_index1++) {
4873 if (wbp->cl_clusters[cl_index1].b_addr == wbp->cl_clusters[cl_index1].e_addr)
9bccf70c
A
4874 continue;
4875 if (min_index == -1)
4876 min_index = cl_index1;
91447636 4877 else if (wbp->cl_clusters[cl_index1].b_addr < wbp->cl_clusters[min_index].b_addr)
9bccf70c
A
4878 min_index = cl_index1;
4879 }
4880 if (min_index == -1)
4881 break;
b0d623f7 4882
91447636
A
4883 l_clusters[cl_index].b_addr = wbp->cl_clusters[min_index].b_addr;
4884 l_clusters[cl_index].e_addr = wbp->cl_clusters[min_index].e_addr;
2d21ac55 4885 l_clusters[cl_index].io_flags = wbp->cl_clusters[min_index].io_flags;
9bccf70c 4886
91447636 4887 wbp->cl_clusters[min_index].b_addr = wbp->cl_clusters[min_index].e_addr;
9bccf70c 4888 }
91447636
A
4889 wbp->cl_number = 0;
4890
4891 cl_len = cl_index;
9bccf70c 4892
2d21ac55 4893 if ( (push_flag & PUSH_DELAY) && cl_len == MAX_CLUSTERS ) {
55e303ae
A
4894 int i;
4895
4896 /*
4897 * determine if we appear to be writing the file sequentially
4898 * if not, by returning without having pushed any clusters
4899 * we will cause this vnode to be pushed into the sparse cluster mechanism
4900 * used for managing more random I/O patterns
4901 *
4902 * we know that we've got all clusters currently in use and the next write doesn't fit into one of them...
2d21ac55 4903 * that's why we're in try_push with PUSH_DELAY...
55e303ae
A
4904 *
4905 * check to make sure that all the clusters except the last one are 'full'... and that each cluster
4906 * is adjacent to the next (i.e. we're looking for sequential writes) they were sorted above
91447636
A
4907 * so we can just make a simple pass through, up to, but not including the last one...
4908 * note that e_addr is not inclusive, so it will be equal to the b_addr of the next cluster if they
55e303ae
A
4909 * are sequential
4910 *
4911 * we let the last one be partial as long as it was adjacent to the previous one...
4912 * we need to do this to deal with multi-threaded servers that might write an I/O or 2 out
4913 * of order... if this occurs at the tail of the last cluster, we don't want to fall into the sparse cluster world...
4914 */
4915 for (i = 0; i < MAX_CLUSTERS - 1; i++) {
cf7d32b8 4916 if ((l_clusters[i].e_addr - l_clusters[i].b_addr) != max_cluster_pgcount)
55e303ae 4917 goto dont_try;
91447636 4918 if (l_clusters[i].e_addr != l_clusters[i+1].b_addr)
55e303ae
A
4919 goto dont_try;
4920 }
4921 }
4922 for (cl_index = 0; cl_index < cl_len; cl_index++) {
2d21ac55
A
4923 int flags;
4924 struct cl_extent cl;
91447636 4925
9bccf70c 4926 /*
91447636 4927 * try to push each cluster in turn...
9bccf70c 4928 */
2d21ac55 4929 if (l_clusters[cl_index].io_flags & CLW_IONOCACHE)
91447636
A
4930 flags = IO_NOCACHE;
4931 else
4932 flags = 0;
2d21ac55
A
4933
4934 if ((l_clusters[cl_index].io_flags & CLW_IOPASSIVE) || (push_flag & IO_PASSIVE))
4935 flags |= IO_PASSIVE;
4936
4937 if (push_flag & PUSH_SYNC)
4938 flags |= IO_SYNC;
4939
91447636
A
4940 cl.b_addr = l_clusters[cl_index].b_addr;
4941 cl.e_addr = l_clusters[cl_index].e_addr;
9bccf70c 4942
2d21ac55 4943 cluster_push_now(vp, &cl, EOF, flags, callback, callback_arg);
9bccf70c 4944
91447636
A
4945 l_clusters[cl_index].b_addr = 0;
4946 l_clusters[cl_index].e_addr = 0;
4947
4948 cl_pushed++;
4949
2d21ac55 4950 if ( !(push_flag & PUSH_ALL) )
91447636 4951 break;
9bccf70c 4952 }
55e303ae 4953dont_try:
9bccf70c
A
4954 if (cl_len > cl_pushed) {
4955 /*
4956 * we didn't push all of the clusters, so
4957 * lets try to merge them back in to the vnode
4958 */
91447636 4959 if ((MAX_CLUSTERS - wbp->cl_number) < (cl_len - cl_pushed)) {
9bccf70c
A
4960 /*
4961 * we picked up some new clusters while we were trying to
91447636
A
4962 * push the old ones... this can happen because I've dropped
4963 * the vnode lock... the sum of the
9bccf70c 4964 * leftovers plus the new cluster count exceeds our ability
55e303ae 4965 * to represent them, so switch to the sparse cluster mechanism
91447636
A
4966 *
4967 * collect the active public clusters...
9bccf70c 4968 */
2d21ac55 4969 sparse_cluster_switch(wbp, vp, EOF, callback, callback_arg);
55e303ae
A
4970
4971 for (cl_index = 0, cl_index1 = 0; cl_index < cl_len; cl_index++) {
91447636 4972 if (l_clusters[cl_index].b_addr == l_clusters[cl_index].e_addr)
9bccf70c 4973 continue;
91447636
A
4974 wbp->cl_clusters[cl_index1].b_addr = l_clusters[cl_index].b_addr;
4975 wbp->cl_clusters[cl_index1].e_addr = l_clusters[cl_index].e_addr;
2d21ac55 4976 wbp->cl_clusters[cl_index1].io_flags = l_clusters[cl_index].io_flags;
9bccf70c 4977
55e303ae 4978 cl_index1++;
9bccf70c 4979 }
55e303ae
A
4980 /*
4981 * update the cluster count
4982 */
91447636 4983 wbp->cl_number = cl_index1;
55e303ae
A
4984
4985 /*
4986 * and collect the original clusters that were moved into the
4987 * local storage for sorting purposes
4988 */
2d21ac55 4989 sparse_cluster_switch(wbp, vp, EOF, callback, callback_arg);
55e303ae 4990
9bccf70c
A
4991 } else {
4992 /*
4993 * we've got room to merge the leftovers back in
4994 * just append them starting at the next 'hole'
91447636 4995 * represented by wbp->cl_number
9bccf70c 4996 */
91447636
A
4997 for (cl_index = 0, cl_index1 = wbp->cl_number; cl_index < cl_len; cl_index++) {
4998 if (l_clusters[cl_index].b_addr == l_clusters[cl_index].e_addr)
9bccf70c
A
4999 continue;
5000
91447636
A
5001 wbp->cl_clusters[cl_index1].b_addr = l_clusters[cl_index].b_addr;
5002 wbp->cl_clusters[cl_index1].e_addr = l_clusters[cl_index].e_addr;
2d21ac55 5003 wbp->cl_clusters[cl_index1].io_flags = l_clusters[cl_index].io_flags;
9bccf70c 5004
9bccf70c
A
5005 cl_index1++;
5006 }
5007 /*
5008 * update the cluster count
5009 */
91447636 5010 wbp->cl_number = cl_index1;
9bccf70c
A
5011 }
5012 }
2d21ac55 5013 return (MAX_CLUSTERS - wbp->cl_number);
9bccf70c
A
5014}
5015
5016
5017
5018static int
2d21ac55 5019cluster_push_now(vnode_t vp, struct cl_extent *cl, off_t EOF, int flags, int (*callback)(buf_t, void *), void *callback_arg)
1c79356b 5020{
1c79356b
A
5021 upl_page_info_t *pl;
5022 upl_t upl;
5023 vm_offset_t upl_offset;
5024 int upl_size;
5025 off_t upl_f_offset;
5026 int pages_in_upl;
5027 int start_pg;
5028 int last_pg;
5029 int io_size;
5030 int io_flags;
55e303ae 5031 int upl_flags;
2d21ac55 5032 int bflag;
1c79356b 5033 int size;
91447636
A
5034 int error = 0;
5035 int retval;
1c79356b
A
5036 kern_return_t kret;
5037
2d21ac55
A
5038 if (flags & IO_PASSIVE)
5039 bflag = CL_PASSIVE;
5040 else
5041 bflag = 0;
1c79356b 5042
9bccf70c 5043 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 51)) | DBG_FUNC_START,
91447636 5044 (int)cl->b_addr, (int)cl->e_addr, (int)EOF, flags, 0);
9bccf70c 5045
91447636 5046 if ((pages_in_upl = (int)(cl->e_addr - cl->b_addr)) == 0) {
9bccf70c 5047 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 51)) | DBG_FUNC_END, 1, 0, 0, 0, 0);
1c79356b 5048
91447636 5049 return (0);
9bccf70c 5050 }
1c79356b 5051 upl_size = pages_in_upl * PAGE_SIZE;
91447636 5052 upl_f_offset = (off_t)(cl->b_addr * PAGE_SIZE_64);
1c79356b 5053
9bccf70c
A
5054 if (upl_f_offset + upl_size >= EOF) {
5055
5056 if (upl_f_offset >= EOF) {
5057 /*
5058 * must have truncated the file and missed
5059 * clearing a dangling cluster (i.e. it's completely
5060 * beyond the new EOF
5061 */
5062 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 51)) | DBG_FUNC_END, 1, 1, 0, 0, 0);
5063
91447636 5064 return(0);
9bccf70c
A
5065 }
5066 size = EOF - upl_f_offset;
1c79356b 5067
55e303ae 5068 upl_size = (size + (PAGE_SIZE - 1)) & ~PAGE_MASK;
9bccf70c 5069 pages_in_upl = upl_size / PAGE_SIZE;
55e303ae 5070 } else
9bccf70c 5071 size = upl_size;
55e303ae
A
5072
5073 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 41)) | DBG_FUNC_START, upl_size, size, 0, 0, 0);
5074
91447636
A
5075 /*
5076 * by asking for UPL_COPYOUT_FROM and UPL_RET_ONLY_DIRTY, we get the following desirable behavior
5077 *
5078 * - only pages that are currently dirty are returned... these are the ones we need to clean
5079 * - the hardware dirty bit is cleared when the page is gathered into the UPL... the software dirty bit is set
5080 * - if we have to abort the I/O for some reason, the software dirty bit is left set since we didn't clean the page
5081 * - when we commit the page, the software dirty bit is cleared... the hardware dirty bit is untouched so that if
5082 * someone dirties this page while the I/O is in progress, we don't lose track of the new state
5083 *
5084 * when the I/O completes, we no longer ask for an explicit clear of the DIRTY state (either soft or hard)
5085 */
5086
5087 if ((vp->v_flag & VNOCACHE_DATA) || (flags & IO_NOCACHE))
55e303ae
A
5088 upl_flags = UPL_COPYOUT_FROM | UPL_RET_ONLY_DIRTY | UPL_SET_LITE | UPL_WILL_BE_DUMPED;
5089 else
5090 upl_flags = UPL_COPYOUT_FROM | UPL_RET_ONLY_DIRTY | UPL_SET_LITE;
5091
0b4e3aa0
A
5092 kret = ubc_create_upl(vp,
5093 upl_f_offset,
5094 upl_size,
5095 &upl,
9bccf70c 5096 &pl,
55e303ae 5097 upl_flags);
1c79356b
A
5098 if (kret != KERN_SUCCESS)
5099 panic("cluster_push: failed to get pagelist");
5100
b0d623f7 5101 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 41)) | DBG_FUNC_END, upl, upl_f_offset, 0, 0, 0);
9bccf70c 5102
55e303ae
A
5103 /*
5104 * since we only asked for the dirty pages back
5105 * it's possible that we may only get a few or even none, so...
5106 * before we start marching forward, we must make sure we know
5107 * where the last present page is in the UPL, otherwise we could
5108 * end up working with a freed upl due to the FREE_ON_EMPTY semantics
5109 * employed by commit_range and abort_range.
5110 */
5111 for (last_pg = pages_in_upl - 1; last_pg >= 0; last_pg--) {
5112 if (upl_page_present(pl, last_pg))
5113 break;
9bccf70c 5114 }
55e303ae 5115 pages_in_upl = last_pg + 1;
1c79356b 5116
55e303ae
A
5117 if (pages_in_upl == 0) {
5118 ubc_upl_abort(upl, 0);
1c79356b 5119
55e303ae 5120 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 51)) | DBG_FUNC_END, 1, 2, 0, 0, 0);
91447636 5121 return(0);
55e303ae
A
5122 }
5123
5124 for (last_pg = 0; last_pg < pages_in_upl; ) {
5125 /*
5126 * find the next dirty page in the UPL
5127 * this will become the first page in the
5128 * next I/O to generate
5129 */
1c79356b 5130 for (start_pg = last_pg; start_pg < pages_in_upl; start_pg++) {
55e303ae 5131 if (upl_dirty_page(pl, start_pg))
1c79356b 5132 break;
55e303ae
A
5133 if (upl_page_present(pl, start_pg))
5134 /*
5135 * RET_ONLY_DIRTY will return non-dirty 'precious' pages
5136 * just release these unchanged since we're not going
5137 * to steal them or change their state
5138 */
5139 ubc_upl_abort_range(upl, start_pg * PAGE_SIZE, PAGE_SIZE, UPL_ABORT_FREE_ON_EMPTY);
1c79356b 5140 }
55e303ae
A
5141 if (start_pg >= pages_in_upl)
5142 /*
5143 * done... no more dirty pages to push
5144 */
5145 break;
5146 if (start_pg > last_pg)
5147 /*
5148 * skipped over some non-dirty pages
5149 */
5150 size -= ((start_pg - last_pg) * PAGE_SIZE);
1c79356b 5151
55e303ae
A
5152 /*
5153 * find a range of dirty pages to write
5154 */
1c79356b 5155 for (last_pg = start_pg; last_pg < pages_in_upl; last_pg++) {
55e303ae 5156 if (!upl_dirty_page(pl, last_pg))
1c79356b
A
5157 break;
5158 }
5159 upl_offset = start_pg * PAGE_SIZE;
5160
5161 io_size = min(size, (last_pg - start_pg) * PAGE_SIZE);
5162
2d21ac55 5163 io_flags = CL_THROTTLE | CL_COMMIT | CL_AGE | bflag;
91447636
A
5164
5165 if ( !(flags & IO_SYNC))
5166 io_flags |= CL_ASYNC;
5167
5168 retval = cluster_io(vp, upl, upl_offset, upl_f_offset + upl_offset, io_size,
2d21ac55 5169 io_flags, (buf_t)NULL, (struct clios *)NULL, callback, callback_arg);
1c79356b 5170
91447636
A
5171 if (error == 0 && retval)
5172 error = retval;
1c79356b
A
5173
5174 size -= io_size;
5175 }
9bccf70c
A
5176 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 51)) | DBG_FUNC_END, 1, 3, 0, 0, 0);
5177
91447636 5178 return(error);
1c79356b 5179}
b4c24cb9
A
5180
5181
91447636
A
5182/*
5183 * sparse_cluster_switch is called with the write behind lock held
5184 */
5185static void
2d21ac55 5186sparse_cluster_switch(struct cl_writebehind *wbp, vnode_t vp, off_t EOF, int (*callback)(buf_t, void *), void *callback_arg)
b4c24cb9 5187{
91447636 5188 int cl_index;
b4c24cb9 5189
b0d623f7 5190 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 78)) | DBG_FUNC_START, vp, wbp->cl_scmap, 0, 0, 0);
91447636
A
5191
5192 for (cl_index = 0; cl_index < wbp->cl_number; cl_index++) {
5193 int flags;
5194 struct cl_extent cl;
5195
5196 for (cl.b_addr = wbp->cl_clusters[cl_index].b_addr; cl.b_addr < wbp->cl_clusters[cl_index].e_addr; cl.b_addr++) {
b4c24cb9 5197
2d21ac55 5198 if (ubc_page_op(vp, (off_t)(cl.b_addr * PAGE_SIZE_64), 0, NULL, &flags) == KERN_SUCCESS) {
91447636
A
5199 if (flags & UPL_POP_DIRTY) {
5200 cl.e_addr = cl.b_addr + 1;
b4c24cb9 5201
b0d623f7 5202 sparse_cluster_add(&(wbp->cl_scmap), vp, &cl, EOF, callback, callback_arg);
91447636 5203 }
55e303ae
A
5204 }
5205 }
5206 }
91447636
A
5207 wbp->cl_number = 0;
5208
b0d623f7 5209 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 78)) | DBG_FUNC_END, vp, wbp->cl_scmap, 0, 0, 0);
55e303ae
A
5210}
5211
5212
91447636 5213/*
b0d623f7
A
5214 * sparse_cluster_push must be called with the write-behind lock held if the scmap is
5215 * still associated with the write-behind context... however, if the scmap has been disassociated
5216 * from the write-behind context (the cluster_push case), the wb lock is not held
91447636
A
5217 */
5218static void
b0d623f7 5219sparse_cluster_push(void **scmap, vnode_t vp, off_t EOF, int push_flag, int (*callback)(buf_t, void *), void *callback_arg)
55e303ae 5220{
91447636
A
5221 struct cl_extent cl;
5222 off_t offset;
5223 u_int length;
55e303ae 5224
b0d623f7 5225 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 79)) | DBG_FUNC_START, vp, (*scmap), 0, push_flag, 0);
55e303ae 5226
2d21ac55 5227 if (push_flag & PUSH_ALL)
b0d623f7 5228 vfs_drt_control(scmap, 1);
55e303ae
A
5229
5230 for (;;) {
b0d623f7 5231 if (vfs_drt_get_cluster(scmap, &offset, &length) != KERN_SUCCESS)
55e303ae 5232 break;
55e303ae 5233
91447636
A
5234 cl.b_addr = (daddr64_t)(offset / PAGE_SIZE_64);
5235 cl.e_addr = (daddr64_t)((offset + length) / PAGE_SIZE_64);
5236
2d21ac55
A
5237 cluster_push_now(vp, &cl, EOF, push_flag & IO_PASSIVE, callback, callback_arg);
5238
2d21ac55 5239 if ( !(push_flag & PUSH_ALL) )
55e303ae
A
5240 break;
5241 }
b0d623f7 5242 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 79)) | DBG_FUNC_END, vp, (*scmap), 0, 0, 0);
55e303ae
A
5243}
5244
5245
91447636
A
5246/*
5247 * sparse_cluster_add is called with the write behind lock held
5248 */
5249static void
b0d623f7 5250sparse_cluster_add(void **scmap, vnode_t vp, struct cl_extent *cl, off_t EOF, int (*callback)(buf_t, void *), void *callback_arg)
55e303ae 5251{
91447636
A
5252 u_int new_dirty;
5253 u_int length;
5254 off_t offset;
55e303ae 5255
b0d623f7 5256 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 80)) | DBG_FUNC_START, (*scmap), 0, cl->b_addr, (int)cl->e_addr, 0);
55e303ae 5257
91447636
A
5258 offset = (off_t)(cl->b_addr * PAGE_SIZE_64);
5259 length = ((u_int)(cl->e_addr - cl->b_addr)) * PAGE_SIZE;
55e303ae 5260
b0d623f7 5261 while (vfs_drt_mark_pages(scmap, offset, length, &new_dirty) != KERN_SUCCESS) {
55e303ae
A
5262 /*
5263 * no room left in the map
5264 * only a partial update was done
5265 * push out some pages and try again
5266 */
b0d623f7 5267 sparse_cluster_push(scmap, vp, EOF, 0, callback, callback_arg);
55e303ae
A
5268
5269 offset += (new_dirty * PAGE_SIZE_64);
5270 length -= (new_dirty * PAGE_SIZE);
5271 }
b0d623f7 5272 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 80)) | DBG_FUNC_END, vp, (*scmap), 0, 0, 0);
55e303ae
A
5273}
5274
5275
5276static int
2d21ac55 5277cluster_align_phys_io(vnode_t vp, struct uio *uio, addr64_t usr_paddr, u_int32_t xsize, int flags, int (*callback)(buf_t, void *), void *callback_arg)
55e303ae 5278{
55e303ae
A
5279 upl_page_info_t *pl;
5280 upl_t upl;
5281 addr64_t ubc_paddr;
5282 kern_return_t kret;
5283 int error = 0;
91447636
A
5284 int did_read = 0;
5285 int abort_flags;
5286 int upl_flags;
2d21ac55
A
5287 int bflag;
5288
5289 if (flags & IO_PASSIVE)
5290 bflag = CL_PASSIVE;
5291 else
5292 bflag = 0;
55e303ae 5293
91447636 5294 upl_flags = UPL_SET_LITE;
2d21ac55
A
5295
5296 if ( !(flags & CL_READ) ) {
91447636
A
5297 /*
5298 * "write" operation: let the UPL subsystem know
5299 * that we intend to modify the buffer cache pages
5300 * we're gathering.
5301 */
5302 upl_flags |= UPL_WILL_MODIFY;
2d21ac55
A
5303 } else {
5304 /*
5305 * indicate that there is no need to pull the
5306 * mapping for this page... we're only going
5307 * to read from it, not modify it.
5308 */
5309 upl_flags |= UPL_FILE_IO;
91447636 5310 }
55e303ae
A
5311 kret = ubc_create_upl(vp,
5312 uio->uio_offset & ~PAGE_MASK_64,
5313 PAGE_SIZE,
5314 &upl,
5315 &pl,
91447636 5316 upl_flags);
55e303ae
A
5317
5318 if (kret != KERN_SUCCESS)
5319 return(EINVAL);
5320
5321 if (!upl_valid_page(pl, 0)) {
5322 /*
5323 * issue a synchronous read to cluster_io
5324 */
91447636 5325 error = cluster_io(vp, upl, 0, uio->uio_offset & ~PAGE_MASK_64, PAGE_SIZE,
2d21ac55 5326 CL_READ | bflag, (buf_t)NULL, (struct clios *)NULL, callback, callback_arg);
55e303ae 5327 if (error) {
b4c24cb9
A
5328 ubc_upl_abort_range(upl, 0, PAGE_SIZE, UPL_ABORT_DUMP_PAGES | UPL_ABORT_FREE_ON_EMPTY);
5329
5330 return(error);
5331 }
91447636 5332 did_read = 1;
b4c24cb9 5333 }
55e303ae 5334 ubc_paddr = ((addr64_t)upl_phys_page(pl, 0) << 12) + (addr64_t)(uio->uio_offset & PAGE_MASK_64);
b4c24cb9 5335
55e303ae
A
5336/*
5337 * NOTE: There is no prototype for the following in BSD. It, and the definitions
5338 * of the defines for cppvPsrc, cppvPsnk, cppvFsnk, and cppvFsrc will be found in
5339 * osfmk/ppc/mappings.h. They are not included here because there appears to be no
5340 * way to do so without exporting them to kexts as well.
5341 */
de355530 5342 if (flags & CL_READ)
55e303ae
A
5343// copypv(ubc_paddr, usr_paddr, xsize, cppvPsrc | cppvPsnk | cppvFsnk); /* Copy physical to physical and flush the destination */
5344 copypv(ubc_paddr, usr_paddr, xsize, 2 | 1 | 4); /* Copy physical to physical and flush the destination */
de355530 5345 else
4a249263
A
5346// copypv(usr_paddr, ubc_paddr, xsize, cppvPsrc | cppvPsnk | cppvFsrc); /* Copy physical to physical and flush the source */
5347 copypv(usr_paddr, ubc_paddr, xsize, 2 | 1 | 8); /* Copy physical to physical and flush the source */
55e303ae
A
5348
5349 if ( !(flags & CL_READ) || (upl_valid_page(pl, 0) && upl_dirty_page(pl, 0))) {
5350 /*
5351 * issue a synchronous write to cluster_io
5352 */
91447636 5353 error = cluster_io(vp, upl, 0, uio->uio_offset & ~PAGE_MASK_64, PAGE_SIZE,
2d21ac55 5354 bflag, (buf_t)NULL, (struct clios *)NULL, callback, callback_arg);
de355530 5355 }
2d21ac55 5356 if (error == 0)
cc9f6e38
A
5357 uio_update(uio, (user_size_t)xsize);
5358
91447636
A
5359 if (did_read)
5360 abort_flags = UPL_ABORT_FREE_ON_EMPTY;
5361 else
5362 abort_flags = UPL_ABORT_FREE_ON_EMPTY | UPL_ABORT_DUMP_PAGES;
5363
5364 ubc_upl_abort_range(upl, 0, PAGE_SIZE, abort_flags);
55e303ae
A
5365
5366 return (error);
5367}
5368
5369
5370
5371int
2d21ac55 5372cluster_copy_upl_data(struct uio *uio, upl_t upl, int upl_offset, int *io_resid)
55e303ae
A
5373{
5374 int pg_offset;
5375 int pg_index;
5376 int csize;
5377 int segflg;
5378 int retval = 0;
2d21ac55 5379 int xsize;
55e303ae 5380 upl_page_info_t *pl;
55e303ae 5381
2d21ac55
A
5382 xsize = *io_resid;
5383
55e303ae 5384 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 34)) | DBG_FUNC_START,
2d21ac55 5385 (int)uio->uio_offset, upl_offset, xsize, 0, 0);
55e303ae
A
5386
5387 segflg = uio->uio_segflg;
5388
5389 switch(segflg) {
5390
91447636
A
5391 case UIO_USERSPACE32:
5392 case UIO_USERISPACE32:
5393 uio->uio_segflg = UIO_PHYS_USERSPACE32;
5394 break;
5395
55e303ae
A
5396 case UIO_USERSPACE:
5397 case UIO_USERISPACE:
5398 uio->uio_segflg = UIO_PHYS_USERSPACE;
5399 break;
5400
91447636
A
5401 case UIO_USERSPACE64:
5402 case UIO_USERISPACE64:
5403 uio->uio_segflg = UIO_PHYS_USERSPACE64;
5404 break;
5405
55e303ae
A
5406 case UIO_SYSSPACE:
5407 uio->uio_segflg = UIO_PHYS_SYSSPACE;
5408 break;
91447636 5409
55e303ae
A
5410 }
5411 pl = ubc_upl_pageinfo(upl);
5412
5413 pg_index = upl_offset / PAGE_SIZE;
5414 pg_offset = upl_offset & PAGE_MASK;
5415 csize = min(PAGE_SIZE - pg_offset, xsize);
5416
5417 while (xsize && retval == 0) {
5418 addr64_t paddr;
5419
5420 paddr = ((addr64_t)upl_phys_page(pl, pg_index) << 12) + pg_offset;
de355530 5421
55e303ae
A
5422 retval = uiomove64(paddr, csize, uio);
5423
5424 pg_index += 1;
5425 pg_offset = 0;
5426 xsize -= csize;
5427 csize = min(PAGE_SIZE, xsize);
5428 }
2d21ac55
A
5429 *io_resid = xsize;
5430
55e303ae
A
5431 uio->uio_segflg = segflg;
5432
55e303ae 5433 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 34)) | DBG_FUNC_END,
2d21ac55 5434 (int)uio->uio_offset, xsize, retval, segflg, 0);
55e303ae
A
5435
5436 return (retval);
5437}
5438
5439
5440int
91447636 5441cluster_copy_ubc_data(vnode_t vp, struct uio *uio, int *io_resid, int mark_dirty)
2d21ac55
A
5442{
5443
5444 return (cluster_copy_ubc_data_internal(vp, uio, io_resid, mark_dirty, 1));
5445}
5446
5447
5448static int
5449cluster_copy_ubc_data_internal(vnode_t vp, struct uio *uio, int *io_resid, int mark_dirty, int take_reference)
55e303ae
A
5450{
5451 int segflg;
5452 int io_size;
5453 int xsize;
5454 int start_offset;
55e303ae
A
5455 int retval = 0;
5456 memory_object_control_t control;
55e303ae 5457
2d21ac55 5458 io_size = *io_resid;
55e303ae
A
5459
5460 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 34)) | DBG_FUNC_START,
2d21ac55 5461 (int)uio->uio_offset, 0, io_size, 0, 0);
55e303ae
A
5462
5463 control = ubc_getobject(vp, UBC_FLAGS_NONE);
2d21ac55 5464
55e303ae
A
5465 if (control == MEMORY_OBJECT_CONTROL_NULL) {
5466 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 34)) | DBG_FUNC_END,
2d21ac55 5467 (int)uio->uio_offset, io_size, retval, 3, 0);
55e303ae
A
5468
5469 return(0);
5470 }
55e303ae
A
5471 segflg = uio->uio_segflg;
5472
5473 switch(segflg) {
5474
91447636
A
5475 case UIO_USERSPACE32:
5476 case UIO_USERISPACE32:
5477 uio->uio_segflg = UIO_PHYS_USERSPACE32;
5478 break;
5479
5480 case UIO_USERSPACE64:
5481 case UIO_USERISPACE64:
5482 uio->uio_segflg = UIO_PHYS_USERSPACE64;
5483 break;
5484
55e303ae
A
5485 case UIO_USERSPACE:
5486 case UIO_USERISPACE:
5487 uio->uio_segflg = UIO_PHYS_USERSPACE;
5488 break;
5489
5490 case UIO_SYSSPACE:
5491 uio->uio_segflg = UIO_PHYS_SYSSPACE;
5492 break;
5493 }
55e303ae 5494
91447636
A
5495 if ( (io_size = *io_resid) ) {
5496 start_offset = (int)(uio->uio_offset & PAGE_MASK_64);
5497 xsize = uio_resid(uio);
55e303ae 5498
2d21ac55
A
5499 retval = memory_object_control_uiomove(control, uio->uio_offset - start_offset, uio,
5500 start_offset, io_size, mark_dirty, take_reference);
91447636
A
5501 xsize -= uio_resid(uio);
5502 io_size -= xsize;
55e303ae
A
5503 }
5504 uio->uio_segflg = segflg;
5505 *io_resid = io_size;
5506
55e303ae 5507 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 34)) | DBG_FUNC_END,
2d21ac55 5508 (int)uio->uio_offset, io_size, retval, 0x80000000 | segflg, 0);
55e303ae
A
5509
5510 return(retval);
5511}
5512
5513
5514int
91447636 5515is_file_clean(vnode_t vp, off_t filesize)
55e303ae
A
5516{
5517 off_t f_offset;
5518 int flags;
5519 int total_dirty = 0;
5520
5521 for (f_offset = 0; f_offset < filesize; f_offset += PAGE_SIZE_64) {
2d21ac55 5522 if (ubc_page_op(vp, f_offset, 0, NULL, &flags) == KERN_SUCCESS) {
55e303ae
A
5523 if (flags & UPL_POP_DIRTY) {
5524 total_dirty++;
5525 }
5526 }
5527 }
5528 if (total_dirty)
5529 return(EINVAL);
5530
5531 return (0);
5532}
5533
5534
5535
5536/*
5537 * Dirty region tracking/clustering mechanism.
5538 *
5539 * This code (vfs_drt_*) provides a mechanism for tracking and clustering
5540 * dirty regions within a larger space (file). It is primarily intended to
5541 * support clustering in large files with many dirty areas.
5542 *
5543 * The implementation assumes that the dirty regions are pages.
5544 *
5545 * To represent dirty pages within the file, we store bit vectors in a
5546 * variable-size circular hash.
5547 */
5548
5549/*
5550 * Bitvector size. This determines the number of pages we group in a
5551 * single hashtable entry. Each hashtable entry is aligned to this
5552 * size within the file.
5553 */
5554#define DRT_BITVECTOR_PAGES 256
5555
5556/*
5557 * File offset handling.
5558 *
5559 * DRT_ADDRESS_MASK is dependent on DRT_BITVECTOR_PAGES;
5560 * the correct formula is (~(DRT_BITVECTOR_PAGES * PAGE_SIZE) - 1)
5561 */
5562#define DRT_ADDRESS_MASK (~((1 << 20) - 1))
5563#define DRT_ALIGN_ADDRESS(addr) ((addr) & DRT_ADDRESS_MASK)
5564
5565/*
5566 * Hashtable address field handling.
5567 *
5568 * The low-order bits of the hashtable address are used to conserve
5569 * space.
5570 *
5571 * DRT_HASH_COUNT_MASK must be large enough to store the range
5572 * 0-DRT_BITVECTOR_PAGES inclusive, as well as have one value
5573 * to indicate that the bucket is actually unoccupied.
5574 */
5575#define DRT_HASH_GET_ADDRESS(scm, i) ((scm)->scm_hashtable[(i)].dhe_control & DRT_ADDRESS_MASK)
5576#define DRT_HASH_SET_ADDRESS(scm, i, a) \
5577 do { \
5578 (scm)->scm_hashtable[(i)].dhe_control = \
5579 ((scm)->scm_hashtable[(i)].dhe_control & ~DRT_ADDRESS_MASK) | DRT_ALIGN_ADDRESS(a); \
5580 } while (0)
5581#define DRT_HASH_COUNT_MASK 0x1ff
5582#define DRT_HASH_GET_COUNT(scm, i) ((scm)->scm_hashtable[(i)].dhe_control & DRT_HASH_COUNT_MASK)
5583#define DRT_HASH_SET_COUNT(scm, i, c) \
5584 do { \
5585 (scm)->scm_hashtable[(i)].dhe_control = \
5586 ((scm)->scm_hashtable[(i)].dhe_control & ~DRT_HASH_COUNT_MASK) | ((c) & DRT_HASH_COUNT_MASK); \
5587 } while (0)
5588#define DRT_HASH_CLEAR(scm, i) \
5589 do { \
5590 (scm)->scm_hashtable[(i)].dhe_control = 0; \
5591 } while (0)
5592#define DRT_HASH_VACATE(scm, i) DRT_HASH_SET_COUNT((scm), (i), DRT_HASH_COUNT_MASK)
5593#define DRT_HASH_VACANT(scm, i) (DRT_HASH_GET_COUNT((scm), (i)) == DRT_HASH_COUNT_MASK)
5594#define DRT_HASH_COPY(oscm, oi, scm, i) \
5595 do { \
5596 (scm)->scm_hashtable[(i)].dhe_control = (oscm)->scm_hashtable[(oi)].dhe_control; \
5597 DRT_BITVECTOR_COPY(oscm, oi, scm, i); \
5598 } while(0);
5599
5600
5601/*
5602 * Hash table moduli.
5603 *
5604 * Since the hashtable entry's size is dependent on the size of
5605 * the bitvector, and since the hashtable size is constrained to
5606 * both being prime and fitting within the desired allocation
5607 * size, these values need to be manually determined.
5608 *
5609 * For DRT_BITVECTOR_SIZE = 256, the entry size is 40 bytes.
5610 *
5611 * The small hashtable allocation is 1024 bytes, so the modulus is 23.
5612 * The large hashtable allocation is 16384 bytes, so the modulus is 401.
5613 */
5614#define DRT_HASH_SMALL_MODULUS 23
5615#define DRT_HASH_LARGE_MODULUS 401
5616
5617#define DRT_SMALL_ALLOCATION 1024 /* 104 bytes spare */
5618#define DRT_LARGE_ALLOCATION 16384 /* 344 bytes spare */
5619
5620/* *** nothing below here has secret dependencies on DRT_BITVECTOR_PAGES *** */
5621
5622/*
5623 * Hashtable bitvector handling.
5624 *
5625 * Bitvector fields are 32 bits long.
5626 */
5627
5628#define DRT_HASH_SET_BIT(scm, i, bit) \
5629 (scm)->scm_hashtable[(i)].dhe_bitvector[(bit) / 32] |= (1 << ((bit) % 32))
5630
5631#define DRT_HASH_CLEAR_BIT(scm, i, bit) \
5632 (scm)->scm_hashtable[(i)].dhe_bitvector[(bit) / 32] &= ~(1 << ((bit) % 32))
5633
5634#define DRT_HASH_TEST_BIT(scm, i, bit) \
5635 ((scm)->scm_hashtable[(i)].dhe_bitvector[(bit) / 32] & (1 << ((bit) % 32)))
5636
5637#define DRT_BITVECTOR_CLEAR(scm, i) \
5638 bzero(&(scm)->scm_hashtable[(i)].dhe_bitvector[0], (DRT_BITVECTOR_PAGES / 32) * sizeof(u_int32_t))
5639
5640#define DRT_BITVECTOR_COPY(oscm, oi, scm, i) \
5641 bcopy(&(oscm)->scm_hashtable[(oi)].dhe_bitvector[0], \
5642 &(scm)->scm_hashtable[(i)].dhe_bitvector[0], \
5643 (DRT_BITVECTOR_PAGES / 32) * sizeof(u_int32_t))
5644
5645
5646
5647/*
5648 * Hashtable entry.
5649 */
5650struct vfs_drt_hashentry {
5651 u_int64_t dhe_control;
5652 u_int32_t dhe_bitvector[DRT_BITVECTOR_PAGES / 32];
5653};
5654
5655/*
5656 * Dirty Region Tracking structure.
5657 *
5658 * The hashtable is allocated entirely inside the DRT structure.
5659 *
5660 * The hash is a simple circular prime modulus arrangement, the structure
5661 * is resized from small to large if it overflows.
5662 */
5663
5664struct vfs_drt_clustermap {
5665 u_int32_t scm_magic; /* sanity/detection */
5666#define DRT_SCM_MAGIC 0x12020003
5667 u_int32_t scm_modulus; /* current ring size */
5668 u_int32_t scm_buckets; /* number of occupied buckets */
5669 u_int32_t scm_lastclean; /* last entry we cleaned */
5670 u_int32_t scm_iskips; /* number of slot skips */
5671
5672 struct vfs_drt_hashentry scm_hashtable[0];
5673};
5674
5675
5676#define DRT_HASH(scm, addr) ((addr) % (scm)->scm_modulus)
5677#define DRT_HASH_NEXT(scm, addr) (((addr) + 1) % (scm)->scm_modulus)
5678
5679/*
5680 * Debugging codes and arguments.
5681 */
5682#define DRT_DEBUG_EMPTYFREE (FSDBG_CODE(DBG_FSRW, 82)) /* nil */
5683#define DRT_DEBUG_RETCLUSTER (FSDBG_CODE(DBG_FSRW, 83)) /* offset, length */
5684#define DRT_DEBUG_ALLOC (FSDBG_CODE(DBG_FSRW, 84)) /* copycount */
5685#define DRT_DEBUG_INSERT (FSDBG_CODE(DBG_FSRW, 85)) /* offset, iskip */
5686#define DRT_DEBUG_MARK (FSDBG_CODE(DBG_FSRW, 86)) /* offset, length,
5687 * dirty */
5688 /* 0, setcount */
5689 /* 1 (clean, no map) */
5690 /* 2 (map alloc fail) */
5691 /* 3, resid (partial) */
5692#define DRT_DEBUG_6 (FSDBG_CODE(DBG_FSRW, 87))
5693#define DRT_DEBUG_SCMDATA (FSDBG_CODE(DBG_FSRW, 88)) /* modulus, buckets,
5694 * lastclean, iskips */
5695
5696
55e303ae
A
5697static kern_return_t vfs_drt_alloc_map(struct vfs_drt_clustermap **cmapp);
5698static kern_return_t vfs_drt_free_map(struct vfs_drt_clustermap *cmap);
5699static kern_return_t vfs_drt_search_index(struct vfs_drt_clustermap *cmap,
5700 u_int64_t offset, int *indexp);
5701static kern_return_t vfs_drt_get_index(struct vfs_drt_clustermap **cmapp,
5702 u_int64_t offset,
5703 int *indexp,
5704 int recursed);
5705static kern_return_t vfs_drt_do_mark_pages(
5706 void **cmapp,
5707 u_int64_t offset,
5708 u_int length,
2d21ac55 5709 u_int *setcountp,
55e303ae
A
5710 int dirty);
5711static void vfs_drt_trace(
5712 struct vfs_drt_clustermap *cmap,
5713 int code,
5714 int arg1,
5715 int arg2,
5716 int arg3,
5717 int arg4);
5718
5719
5720/*
5721 * Allocate and initialise a sparse cluster map.
5722 *
5723 * Will allocate a new map, resize or compact an existing map.
5724 *
5725 * XXX we should probably have at least one intermediate map size,
5726 * as the 1:16 ratio seems a bit drastic.
5727 */
5728static kern_return_t
5729vfs_drt_alloc_map(struct vfs_drt_clustermap **cmapp)
5730{
5731 struct vfs_drt_clustermap *cmap, *ocmap;
5732 kern_return_t kret;
5733 u_int64_t offset;
2d21ac55
A
5734 u_int32_t i;
5735 int nsize, active_buckets, index, copycount;
55e303ae
A
5736
5737 ocmap = NULL;
5738 if (cmapp != NULL)
5739 ocmap = *cmapp;
5740
5741 /*
5742 * Decide on the size of the new map.
5743 */
5744 if (ocmap == NULL) {
5745 nsize = DRT_HASH_SMALL_MODULUS;
5746 } else {
5747 /* count the number of active buckets in the old map */
5748 active_buckets = 0;
5749 for (i = 0; i < ocmap->scm_modulus; i++) {
5750 if (!DRT_HASH_VACANT(ocmap, i) &&
5751 (DRT_HASH_GET_COUNT(ocmap, i) != 0))
5752 active_buckets++;
5753 }
5754 /*
5755 * If we're currently using the small allocation, check to
5756 * see whether we should grow to the large one.
5757 */
5758 if (ocmap->scm_modulus == DRT_HASH_SMALL_MODULUS) {
5759 /* if the ring is nearly full */
5760 if (active_buckets > (DRT_HASH_SMALL_MODULUS - 5)) {
5761 nsize = DRT_HASH_LARGE_MODULUS;
5762 } else {
5763 nsize = DRT_HASH_SMALL_MODULUS;
5764 }
5765 } else {
5766 /* already using the large modulus */
5767 nsize = DRT_HASH_LARGE_MODULUS;
5768 /*
5769 * If the ring is completely full, there's
5770 * nothing useful for us to do. Behave as
5771 * though we had compacted into the new
5772 * array and return.
5773 */
5774 if (active_buckets >= DRT_HASH_LARGE_MODULUS)
5775 return(KERN_SUCCESS);
5776 }
5777 }
5778
5779 /*
5780 * Allocate and initialise the new map.
5781 */
5782
5783 kret = kmem_alloc(kernel_map, (vm_offset_t *)&cmap,
5784 (nsize == DRT_HASH_SMALL_MODULUS) ? DRT_SMALL_ALLOCATION : DRT_LARGE_ALLOCATION);
5785 if (kret != KERN_SUCCESS)
5786 return(kret);
5787 cmap->scm_magic = DRT_SCM_MAGIC;
5788 cmap->scm_modulus = nsize;
5789 cmap->scm_buckets = 0;
5790 cmap->scm_lastclean = 0;
5791 cmap->scm_iskips = 0;
5792 for (i = 0; i < cmap->scm_modulus; i++) {
5793 DRT_HASH_CLEAR(cmap, i);
5794 DRT_HASH_VACATE(cmap, i);
5795 DRT_BITVECTOR_CLEAR(cmap, i);
5796 }
5797
5798 /*
5799 * If there's an old map, re-hash entries from it into the new map.
5800 */
5801 copycount = 0;
5802 if (ocmap != NULL) {
5803 for (i = 0; i < ocmap->scm_modulus; i++) {
5804 /* skip empty buckets */
5805 if (DRT_HASH_VACANT(ocmap, i) ||
5806 (DRT_HASH_GET_COUNT(ocmap, i) == 0))
5807 continue;
5808 /* get new index */
5809 offset = DRT_HASH_GET_ADDRESS(ocmap, i);
5810 kret = vfs_drt_get_index(&cmap, offset, &index, 1);
5811 if (kret != KERN_SUCCESS) {
5812 /* XXX need to bail out gracefully here */
5813 panic("vfs_drt: new cluster map mysteriously too small");
2d21ac55 5814 index = 0;
55e303ae
A
5815 }
5816 /* copy */
5817 DRT_HASH_COPY(ocmap, i, cmap, index);
5818 copycount++;
5819 }
5820 }
5821
5822 /* log what we've done */
5823 vfs_drt_trace(cmap, DRT_DEBUG_ALLOC, copycount, 0, 0, 0);
5824
5825 /*
5826 * It's important to ensure that *cmapp always points to
5827 * a valid map, so we must overwrite it before freeing
5828 * the old map.
5829 */
5830 *cmapp = cmap;
5831 if (ocmap != NULL) {
5832 /* emit stats into trace buffer */
5833 vfs_drt_trace(ocmap, DRT_DEBUG_SCMDATA,
5834 ocmap->scm_modulus,
5835 ocmap->scm_buckets,
5836 ocmap->scm_lastclean,
5837 ocmap->scm_iskips);
5838
5839 vfs_drt_free_map(ocmap);
5840 }
5841 return(KERN_SUCCESS);
5842}
5843
5844
5845/*
5846 * Free a sparse cluster map.
5847 */
5848static kern_return_t
5849vfs_drt_free_map(struct vfs_drt_clustermap *cmap)
5850{
55e303ae
A
5851 kmem_free(kernel_map, (vm_offset_t)cmap,
5852 (cmap->scm_modulus == DRT_HASH_SMALL_MODULUS) ? DRT_SMALL_ALLOCATION : DRT_LARGE_ALLOCATION);
5853 return(KERN_SUCCESS);
5854}
5855
5856
5857/*
5858 * Find the hashtable slot currently occupied by an entry for the supplied offset.
5859 */
5860static kern_return_t
5861vfs_drt_search_index(struct vfs_drt_clustermap *cmap, u_int64_t offset, int *indexp)
5862{
2d21ac55
A
5863 int index;
5864 u_int32_t i;
55e303ae
A
5865
5866 offset = DRT_ALIGN_ADDRESS(offset);
5867 index = DRT_HASH(cmap, offset);
5868
5869 /* traverse the hashtable */
5870 for (i = 0; i < cmap->scm_modulus; i++) {
5871
5872 /*
5873 * If the slot is vacant, we can stop.
5874 */
5875 if (DRT_HASH_VACANT(cmap, index))
5876 break;
5877
5878 /*
5879 * If the address matches our offset, we have success.
5880 */
5881 if (DRT_HASH_GET_ADDRESS(cmap, index) == offset) {
5882 *indexp = index;
5883 return(KERN_SUCCESS);
5884 }
5885
5886 /*
5887 * Move to the next slot, try again.
5888 */
5889 index = DRT_HASH_NEXT(cmap, index);
5890 }
5891 /*
5892 * It's not there.
5893 */
5894 return(KERN_FAILURE);
5895}
5896
5897/*
5898 * Find the hashtable slot for the supplied offset. If we haven't allocated
5899 * one yet, allocate one and populate the address field. Note that it will
5900 * not have a nonzero page count and thus will still technically be free, so
5901 * in the case where we are called to clean pages, the slot will remain free.
5902 */
5903static kern_return_t
5904vfs_drt_get_index(struct vfs_drt_clustermap **cmapp, u_int64_t offset, int *indexp, int recursed)
5905{
5906 struct vfs_drt_clustermap *cmap;
5907 kern_return_t kret;
2d21ac55
A
5908 u_int32_t index;
5909 u_int32_t i;
55e303ae
A
5910
5911 cmap = *cmapp;
5912
5913 /* look for an existing entry */
5914 kret = vfs_drt_search_index(cmap, offset, indexp);
5915 if (kret == KERN_SUCCESS)
5916 return(kret);
5917
5918 /* need to allocate an entry */
5919 offset = DRT_ALIGN_ADDRESS(offset);
5920 index = DRT_HASH(cmap, offset);
5921
5922 /* scan from the index forwards looking for a vacant slot */
5923 for (i = 0; i < cmap->scm_modulus; i++) {
5924 /* slot vacant? */
5925 if (DRT_HASH_VACANT(cmap, index) || DRT_HASH_GET_COUNT(cmap,index) == 0) {
5926 cmap->scm_buckets++;
5927 if (index < cmap->scm_lastclean)
5928 cmap->scm_lastclean = index;
5929 DRT_HASH_SET_ADDRESS(cmap, index, offset);
5930 DRT_HASH_SET_COUNT(cmap, index, 0);
5931 DRT_BITVECTOR_CLEAR(cmap, index);
5932 *indexp = index;
5933 vfs_drt_trace(cmap, DRT_DEBUG_INSERT, (int)offset, i, 0, 0);
5934 return(KERN_SUCCESS);
5935 }
5936 cmap->scm_iskips += i;
5937 index = DRT_HASH_NEXT(cmap, index);
5938 }
5939
5940 /*
5941 * We haven't found a vacant slot, so the map is full. If we're not
5942 * already recursed, try reallocating/compacting it.
5943 */
5944 if (recursed)
5945 return(KERN_FAILURE);
5946 kret = vfs_drt_alloc_map(cmapp);
5947 if (kret == KERN_SUCCESS) {
5948 /* now try to insert again */
5949 kret = vfs_drt_get_index(cmapp, offset, indexp, 1);
5950 }
5951 return(kret);
5952}
5953
5954/*
5955 * Implementation of set dirty/clean.
5956 *
5957 * In the 'clean' case, not finding a map is OK.
5958 */
5959static kern_return_t
5960vfs_drt_do_mark_pages(
5961 void **private,
5962 u_int64_t offset,
5963 u_int length,
2d21ac55 5964 u_int *setcountp,
55e303ae
A
5965 int dirty)
5966{
5967 struct vfs_drt_clustermap *cmap, **cmapp;
5968 kern_return_t kret;
5969 int i, index, pgoff, pgcount, setcount, ecount;
5970
5971 cmapp = (struct vfs_drt_clustermap **)private;
5972 cmap = *cmapp;
5973
5974 vfs_drt_trace(cmap, DRT_DEBUG_MARK | DBG_FUNC_START, (int)offset, (int)length, dirty, 0);
5975
5976 if (setcountp != NULL)
5977 *setcountp = 0;
5978
5979 /* allocate a cluster map if we don't already have one */
5980 if (cmap == NULL) {
5981 /* no cluster map, nothing to clean */
5982 if (!dirty) {
5983 vfs_drt_trace(cmap, DRT_DEBUG_MARK | DBG_FUNC_END, 1, 0, 0, 0);
5984 return(KERN_SUCCESS);
5985 }
5986 kret = vfs_drt_alloc_map(cmapp);
5987 if (kret != KERN_SUCCESS) {
5988 vfs_drt_trace(cmap, DRT_DEBUG_MARK | DBG_FUNC_END, 2, 0, 0, 0);
5989 return(kret);
5990 }
5991 }
5992 setcount = 0;
5993
5994 /*
5995 * Iterate over the length of the region.
5996 */
5997 while (length > 0) {
5998 /*
5999 * Get the hashtable index for this offset.
6000 *
6001 * XXX this will add blank entries if we are clearing a range
6002 * that hasn't been dirtied.
6003 */
6004 kret = vfs_drt_get_index(cmapp, offset, &index, 0);
6005 cmap = *cmapp; /* may have changed! */
6006 /* this may be a partial-success return */
6007 if (kret != KERN_SUCCESS) {
6008 if (setcountp != NULL)
6009 *setcountp = setcount;
6010 vfs_drt_trace(cmap, DRT_DEBUG_MARK | DBG_FUNC_END, 3, (int)length, 0, 0);
6011
6012 return(kret);
6013 }
6014
6015 /*
6016 * Work out how many pages we're modifying in this
6017 * hashtable entry.
6018 */
6019 pgoff = (offset - DRT_ALIGN_ADDRESS(offset)) / PAGE_SIZE;
6020 pgcount = min((length / PAGE_SIZE), (DRT_BITVECTOR_PAGES - pgoff));
6021
6022 /*
6023 * Iterate over pages, dirty/clearing as we go.
6024 */
6025 ecount = DRT_HASH_GET_COUNT(cmap, index);
6026 for (i = 0; i < pgcount; i++) {
6027 if (dirty) {
6028 if (!DRT_HASH_TEST_BIT(cmap, index, pgoff + i)) {
6029 DRT_HASH_SET_BIT(cmap, index, pgoff + i);
6030 ecount++;
6031 setcount++;
6032 }
6033 } else {
6034 if (DRT_HASH_TEST_BIT(cmap, index, pgoff + i)) {
6035 DRT_HASH_CLEAR_BIT(cmap, index, pgoff + i);
6036 ecount--;
6037 setcount++;
6038 }
6039 }
6040 }
6041 DRT_HASH_SET_COUNT(cmap, index, ecount);
91447636 6042
55e303ae
A
6043 offset += pgcount * PAGE_SIZE;
6044 length -= pgcount * PAGE_SIZE;
6045 }
6046 if (setcountp != NULL)
6047 *setcountp = setcount;
6048
6049 vfs_drt_trace(cmap, DRT_DEBUG_MARK | DBG_FUNC_END, 0, setcount, 0, 0);
6050
6051 return(KERN_SUCCESS);
6052}
6053
6054/*
6055 * Mark a set of pages as dirty/clean.
6056 *
6057 * This is a public interface.
6058 *
6059 * cmapp
6060 * Pointer to storage suitable for holding a pointer. Note that
6061 * this must either be NULL or a value set by this function.
6062 *
6063 * size
6064 * Current file size in bytes.
6065 *
6066 * offset
6067 * Offset of the first page to be marked as dirty, in bytes. Must be
6068 * page-aligned.
6069 *
6070 * length
6071 * Length of dirty region, in bytes. Must be a multiple of PAGE_SIZE.
6072 *
6073 * setcountp
6074 * Number of pages newly marked dirty by this call (optional).
6075 *
6076 * Returns KERN_SUCCESS if all the pages were successfully marked.
6077 */
6078static kern_return_t
2d21ac55 6079vfs_drt_mark_pages(void **cmapp, off_t offset, u_int length, u_int *setcountp)
55e303ae
A
6080{
6081 /* XXX size unused, drop from interface */
6082 return(vfs_drt_do_mark_pages(cmapp, offset, length, setcountp, 1));
6083}
6084
91447636 6085#if 0
55e303ae
A
6086static kern_return_t
6087vfs_drt_unmark_pages(void **cmapp, off_t offset, u_int length)
6088{
6089 return(vfs_drt_do_mark_pages(cmapp, offset, length, NULL, 0));
6090}
91447636 6091#endif
55e303ae
A
6092
6093/*
6094 * Get a cluster of dirty pages.
6095 *
6096 * This is a public interface.
6097 *
6098 * cmapp
6099 * Pointer to storage managed by drt_mark_pages. Note that this must
6100 * be NULL or a value set by drt_mark_pages.
6101 *
6102 * offsetp
6103 * Returns the byte offset into the file of the first page in the cluster.
6104 *
6105 * lengthp
6106 * Returns the length in bytes of the cluster of dirty pages.
6107 *
6108 * Returns success if a cluster was found. If KERN_FAILURE is returned, there
6109 * are no dirty pages meeting the minmum size criteria. Private storage will
6110 * be released if there are no more dirty pages left in the map
6111 *
6112 */
6113static kern_return_t
6114vfs_drt_get_cluster(void **cmapp, off_t *offsetp, u_int *lengthp)
6115{
6116 struct vfs_drt_clustermap *cmap;
6117 u_int64_t offset;
6118 u_int length;
2d21ac55
A
6119 u_int32_t j;
6120 int index, i, fs, ls;
55e303ae
A
6121
6122 /* sanity */
6123 if ((cmapp == NULL) || (*cmapp == NULL))
6124 return(KERN_FAILURE);
6125 cmap = *cmapp;
6126
6127 /* walk the hashtable */
6128 for (offset = 0, j = 0; j < cmap->scm_modulus; offset += (DRT_BITVECTOR_PAGES * PAGE_SIZE), j++) {
6129 index = DRT_HASH(cmap, offset);
6130
6131 if (DRT_HASH_VACANT(cmap, index) || (DRT_HASH_GET_COUNT(cmap, index) == 0))
6132 continue;
6133
6134 /* scan the bitfield for a string of bits */
6135 fs = -1;
6136
6137 for (i = 0; i < DRT_BITVECTOR_PAGES; i++) {
6138 if (DRT_HASH_TEST_BIT(cmap, index, i)) {
6139 fs = i;
6140 break;
6141 }
6142 }
6143 if (fs == -1) {
6144 /* didn't find any bits set */
6145 panic("vfs_drt: entry summary count > 0 but no bits set in map");
6146 }
6147 for (ls = 0; i < DRT_BITVECTOR_PAGES; i++, ls++) {
6148 if (!DRT_HASH_TEST_BIT(cmap, index, i))
6149 break;
6150 }
6151
6152 /* compute offset and length, mark pages clean */
6153 offset = DRT_HASH_GET_ADDRESS(cmap, index) + (PAGE_SIZE * fs);
6154 length = ls * PAGE_SIZE;
6155 vfs_drt_do_mark_pages(cmapp, offset, length, NULL, 0);
6156 cmap->scm_lastclean = index;
6157
6158 /* return successful */
6159 *offsetp = (off_t)offset;
6160 *lengthp = length;
6161
6162 vfs_drt_trace(cmap, DRT_DEBUG_RETCLUSTER, (int)offset, (int)length, 0, 0);
6163 return(KERN_SUCCESS);
6164 }
6165 /*
6166 * We didn't find anything... hashtable is empty
6167 * emit stats into trace buffer and
6168 * then free it
6169 */
6170 vfs_drt_trace(cmap, DRT_DEBUG_SCMDATA,
6171 cmap->scm_modulus,
6172 cmap->scm_buckets,
6173 cmap->scm_lastclean,
6174 cmap->scm_iskips);
6175
6176 vfs_drt_free_map(cmap);
6177 *cmapp = NULL;
6178
6179 return(KERN_FAILURE);
6180}
6181
6182
6183static kern_return_t
6184vfs_drt_control(void **cmapp, int op_type)
6185{
6186 struct vfs_drt_clustermap *cmap;
6187
6188 /* sanity */
6189 if ((cmapp == NULL) || (*cmapp == NULL))
6190 return(KERN_FAILURE);
6191 cmap = *cmapp;
6192
6193 switch (op_type) {
6194 case 0:
6195 /* emit stats into trace buffer */
6196 vfs_drt_trace(cmap, DRT_DEBUG_SCMDATA,
6197 cmap->scm_modulus,
6198 cmap->scm_buckets,
6199 cmap->scm_lastclean,
6200 cmap->scm_iskips);
6201
6202 vfs_drt_free_map(cmap);
6203 *cmapp = NULL;
6204 break;
6205
6206 case 1:
6207 cmap->scm_lastclean = 0;
6208 break;
6209 }
6210 return(KERN_SUCCESS);
6211}
6212
6213
6214
6215/*
6216 * Emit a summary of the state of the clustermap into the trace buffer
6217 * along with some caller-provided data.
6218 */
91447636 6219#if KDEBUG
55e303ae 6220static void
91447636 6221vfs_drt_trace(__unused struct vfs_drt_clustermap *cmap, int code, int arg1, int arg2, int arg3, int arg4)
55e303ae
A
6222{
6223 KERNEL_DEBUG(code, arg1, arg2, arg3, arg4, 0);
6224}
91447636
A
6225#else
6226static void
6227vfs_drt_trace(__unused struct vfs_drt_clustermap *cmap, __unused int code,
6228 __unused int arg1, __unused int arg2, __unused int arg3,
6229 __unused int arg4)
6230{
6231}
6232#endif
55e303ae 6233
91447636 6234#if 0
55e303ae
A
6235/*
6236 * Perform basic sanity check on the hash entry summary count
6237 * vs. the actual bits set in the entry.
6238 */
6239static void
6240vfs_drt_sanity(struct vfs_drt_clustermap *cmap)
6241{
6242 int index, i;
6243 int bits_on;
6244
6245 for (index = 0; index < cmap->scm_modulus; index++) {
6246 if (DRT_HASH_VACANT(cmap, index))
6247 continue;
6248
6249 for (bits_on = 0, i = 0; i < DRT_BITVECTOR_PAGES; i++) {
6250 if (DRT_HASH_TEST_BIT(cmap, index, i))
6251 bits_on++;
6252 }
6253 if (bits_on != DRT_HASH_GET_COUNT(cmap, index))
6254 panic("bits_on = %d, index = %d\n", bits_on, index);
6255 }
b4c24cb9 6256}
91447636 6257#endif