+ max_iosize = mp->mnt_maxreadcnt;
+ max_vectors = mp->mnt_segreadcnt;
+ } else {
+ io_flags = B_WRITE;
+ bmap_flags = VNODE_WRITE;
+
+ max_iosize = mp->mnt_maxwritecnt;
+ max_vectors = mp->mnt_segwritecnt;
+ }
+ KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 22)) | DBG_FUNC_NONE, max_iosize, max_vectors, mp->mnt_devblocksize, 0, 0);
+
+ /*
+ * make sure the maximum iosize is a
+ * multiple of the page size
+ */
+ max_iosize &= ~PAGE_MASK;
+
+ /*
+ * Ensure the maximum iosize is sensible.
+ */
+ if (!max_iosize) {
+ max_iosize = PAGE_SIZE;
+ }
+
+ if (flags & CL_THROTTLE) {
+ if (!(flags & CL_PAGEOUT) && cluster_is_throttled(vp)) {
+ if (max_iosize > THROTTLE_MAX_IOSIZE) {
+ max_iosize = THROTTLE_MAX_IOSIZE;
+ }
+ async_throttle = THROTTLE_MAXCNT;
+ } else {
+ if ((flags & CL_DEV_MEMORY)) {
+ async_throttle = IO_SCALE(vp, VNODE_ASYNC_THROTTLE);
+ } else {
+ u_int max_cluster;
+ u_int max_cluster_size;
+ u_int scale;
+
+ if (vp->v_mount->mnt_minsaturationbytecount) {
+ max_cluster_size = vp->v_mount->mnt_minsaturationbytecount;
+
+ scale = 1;
+ } else {
+ max_cluster_size = MAX_CLUSTER_SIZE(vp);
+
+ if (disk_conditioner_mount_is_ssd(vp->v_mount)) {
+ scale = WRITE_THROTTLE_SSD;
+ } else {
+ scale = WRITE_THROTTLE;
+ }
+ }
+ if (max_iosize > max_cluster_size) {
+ max_cluster = max_cluster_size;
+ } else {
+ max_cluster = max_iosize;
+ }
+
+ if (size < max_cluster) {
+ max_cluster = size;
+ }
+
+ if (flags & CL_CLOSE) {
+ scale += MAX_CLUSTERS;
+ }
+
+ async_throttle = min(IO_SCALE(vp, VNODE_ASYNC_THROTTLE), ((scale * max_cluster_size) / max_cluster) - 1);
+ }
+ }
+ }
+ if (flags & CL_AGE) {
+ io_flags |= B_AGE;
+ }
+ if (flags & (CL_PAGEIN | CL_PAGEOUT)) {
+ io_flags |= B_PAGEIO;
+ }
+ if (flags & (CL_IOSTREAMING)) {
+ io_flags |= B_IOSTREAMING;
+ }
+ if (flags & CL_COMMIT) {
+ io_flags |= B_COMMIT_UPL;
+ }
+ if (flags & CL_DIRECT_IO) {
+ io_flags |= B_PHYS;
+ }
+ if (flags & (CL_PRESERVE | CL_KEEPCACHED)) {
+ io_flags |= B_CACHE;
+ }
+ if (flags & CL_PASSIVE) {
+ io_flags |= B_PASSIVE;
+ }
+ if (flags & CL_ENCRYPTED) {
+ io_flags |= B_ENCRYPTED_IO;
+ }
+
+ if (vp->v_flag & VSYSTEM) {
+ io_flags |= B_META;
+ }
+
+ if ((flags & CL_READ) && ((upl_offset + non_rounded_size) & PAGE_MASK) && (!(flags & CL_NOZERO))) {
+ /*
+ * then we are going to end up
+ * with a page that we can't complete (the file size wasn't a multiple
+ * of PAGE_SIZE and we're trying to read to the end of the file
+ * so we'll go ahead and zero out the portion of the page we can't
+ * read in from the file
+ */
+ zero_offset = (int)(upl_offset + non_rounded_size);
+ } else if (!ISSET(flags, CL_READ) && ISSET(flags, CL_DIRECT_IO)) {
+ assert(ISSET(flags, CL_COMMIT));
+
+ // For a direct/uncached write, we need to lock pages...
+
+ upl_t cached_upl;
+
+ /*
+ * Create a UPL to lock the pages in the cache whilst the
+ * write is in progress.
+ */
+ ubc_create_upl_kernel(vp, f_offset, non_rounded_size, &cached_upl,
+ NULL, UPL_SET_LITE, VM_KERN_MEMORY_FILE);
+
+ /*
+ * Attach this UPL to the other UPL so that we can find it
+ * later.
+ */
+ upl_set_associated_upl(upl, cached_upl);
+
+ if (upl_offset & PAGE_MASK) {
+ /*
+ * The two UPLs are not aligned, so mark the first page in
+ * @upl so that cluster_handle_associated_upl can handle
+ * it accordingly.
+ */
+ upl_page_info_t *pl = UPL_GET_INTERNAL_PAGE_LIST(upl);
+ upl_page_set_mark(pl, 0, true);
+ }
+ }
+
+ while (size) {
+ daddr64_t blkno;
+ daddr64_t lblkno;
+ u_int io_size_wanted;
+ size_t io_size_tmp;
+
+ if (size > max_iosize) {
+ io_size = max_iosize;
+ } else {
+ io_size = size;
+ }
+
+ io_size_wanted = io_size;
+ io_size_tmp = (size_t)io_size;
+
+ if ((error = VNOP_BLOCKMAP(vp, f_offset, io_size, &blkno, &io_size_tmp, NULL, bmap_flags, NULL))) {
+ break;
+ }
+
+ if (io_size_tmp > io_size_wanted) {
+ io_size = io_size_wanted;
+ } else {
+ io_size = (u_int)io_size_tmp;
+ }
+
+ if (real_bp && (real_bp->b_blkno == real_bp->b_lblkno)) {
+ real_bp->b_blkno = blkno;
+ }
+
+ KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 24)) | DBG_FUNC_NONE,
+ (int)f_offset, (int)(blkno >> 32), (int)blkno, io_size, 0);
+
+ if (io_size == 0) {
+ /*
+ * vnop_blockmap didn't return an error... however, it did
+ * return an extent size of 0 which means we can't
+ * make forward progress on this I/O... a hole in the
+ * file would be returned as a blkno of -1 with a non-zero io_size
+ * a real extent is returned with a blkno != -1 and a non-zero io_size
+ */
+ error = EINVAL;
+ break;
+ }
+ if (!(flags & CL_READ) && blkno == -1) {
+ off_t e_offset;
+ int pageout_flags;
+
+ if (upl_get_internal_vectorupl(upl)) {
+ panic("Vector UPLs should not take this code-path\n");
+ }
+ /*
+ * we're writing into a 'hole'
+ */
+ if (flags & CL_PAGEOUT) {
+ /*
+ * if we got here via cluster_pageout
+ * then just error the request and return
+ * the 'hole' should already have been covered
+ */
+ error = EINVAL;
+ break;
+ }
+ /*
+ * we can get here if the cluster code happens to
+ * pick up a page that was dirtied via mmap vs
+ * a 'write' and the page targets a 'hole'...
+ * i.e. the writes to the cluster were sparse
+ * and the file was being written for the first time
+ *
+ * we can also get here if the filesystem supports
+ * 'holes' that are less than PAGE_SIZE.... because
+ * we can't know if the range in the page that covers
+ * the 'hole' has been dirtied via an mmap or not,
+ * we have to assume the worst and try to push the
+ * entire page to storage.
+ *
+ * Try paging out the page individually before
+ * giving up entirely and dumping it (the pageout
+ * path will insure that the zero extent accounting
+ * has been taken care of before we get back into cluster_io)
+ *
+ * go direct to vnode_pageout so that we don't have to
+ * unbusy the page from the UPL... we used to do this
+ * so that we could call ubc_msync, but that results
+ * in a potential deadlock if someone else races us to acquire
+ * that page and wins and in addition needs one of the pages
+ * we're continuing to hold in the UPL
+ */
+ pageout_flags = UPL_MSYNC | UPL_VNODE_PAGER | UPL_NESTED_PAGEOUT;
+
+ if (!(flags & CL_ASYNC)) {
+ pageout_flags |= UPL_IOSYNC;
+ }
+ if (!(flags & CL_COMMIT)) {
+ pageout_flags |= UPL_NOCOMMIT;
+ }
+
+ if (cbp_head) {
+ buf_t prev_cbp;
+ uint32_t bytes_in_last_page;
+
+ /*
+ * first we have to wait for the the current outstanding I/Os
+ * to complete... EOT hasn't been set yet on this transaction
+ * so the pages won't be released
+ */
+ cluster_wait_IO(cbp_head, (flags & CL_ASYNC));
+
+ bytes_in_last_page = cbp_head->b_uploffset & PAGE_MASK;
+ for (cbp = cbp_head; cbp; cbp = cbp->b_trans_next) {
+ bytes_in_last_page += cbp->b_bcount;
+ }
+ bytes_in_last_page &= PAGE_MASK;
+
+ while (bytes_in_last_page) {
+ /*
+ * we've got a transcation that
+ * includes the page we're about to push out through vnode_pageout...
+ * find the bp's in the list which intersect this page and either
+ * remove them entirely from the transaction (there could be multiple bp's), or
+ * round it's iosize down to the page boundary (there can only be one)...
+ *
+ * find the last bp in the list and act on it
+ */
+ for (prev_cbp = cbp = cbp_head; cbp->b_trans_next; cbp = cbp->b_trans_next) {
+ prev_cbp = cbp;
+ }
+
+ if (bytes_in_last_page >= cbp->b_bcount) {
+ /*
+ * this buf no longer has any I/O associated with it
+ */
+ bytes_in_last_page -= cbp->b_bcount;
+ cbp->b_bcount = 0;
+
+ free_io_buf(cbp);
+
+ if (cbp == cbp_head) {
+ assert(bytes_in_last_page == 0);
+ /*
+ * the buf we just freed was the only buf in
+ * this transaction... so there's no I/O to do
+ */
+ cbp_head = NULL;
+ cbp_tail = NULL;
+ } else {
+ /*
+ * remove the buf we just freed from
+ * the transaction list
+ */
+ prev_cbp->b_trans_next = NULL;
+ cbp_tail = prev_cbp;
+ }
+ } else {
+ /*
+ * this is the last bp that has I/O
+ * intersecting the page of interest
+ * only some of the I/O is in the intersection
+ * so clip the size but keep it in the transaction list
+ */
+ cbp->b_bcount -= bytes_in_last_page;
+ cbp_tail = cbp;
+ bytes_in_last_page = 0;
+ }
+ }
+ if (cbp_head) {
+ /*
+ * there was more to the current transaction
+ * than just the page we are pushing out via vnode_pageout...
+ * mark it as finished and complete it... we've already
+ * waited for the I/Os to complete above in the call to cluster_wait_IO
+ */
+ cluster_EOT(cbp_head, cbp_tail, 0);
+
+ cluster_complete_transaction(&cbp_head, callback_arg, &retval, flags, 0);
+
+ trans_count = 0;
+ }
+ }
+ if (vnode_pageout(vp, upl, (upl_offset_t)trunc_page(upl_offset), trunc_page_64(f_offset), PAGE_SIZE, pageout_flags, NULL) != PAGER_SUCCESS) {
+ error = EINVAL;
+ }
+ e_offset = round_page_64(f_offset + 1);
+ io_size = (u_int)(e_offset - f_offset);
+
+ f_offset += io_size;
+ upl_offset += io_size;
+
+ if (size >= io_size) {
+ size -= io_size;
+ } else {
+ size = 0;
+ }
+ /*
+ * keep track of how much of the original request
+ * that we've actually completed... non_rounded_size
+ * may go negative due to us rounding the request
+ * to a page size multiple (i.e. size > non_rounded_size)
+ */
+ non_rounded_size -= io_size;
+
+ if (non_rounded_size <= 0) {
+ /*
+ * we've transferred all of the data in the original
+ * request, but we were unable to complete the tail
+ * of the last page because the file didn't have
+ * an allocation to back that portion... this is ok.
+ */
+ size = 0;
+ }
+ if (error) {
+ if (size == 0) {
+ flags &= ~CL_COMMIT;
+ }
+ break;
+ }
+ continue;
+ }
+ lblkno = (daddr64_t)(f_offset / 0x1000);
+ /*
+ * we have now figured out how much I/O we can do - this is in 'io_size'
+ * pg_offset is the starting point in the first page for the I/O
+ * pg_count is the number of full and partial pages that 'io_size' encompasses
+ */
+ pg_offset = upl_offset & PAGE_MASK;
+
+ if (flags & CL_DEV_MEMORY) {
+ /*
+ * treat physical requests as one 'giant' page
+ */
+ pg_count = 1;
+ } else {
+ pg_count = (io_size + pg_offset + (PAGE_SIZE - 1)) / PAGE_SIZE;
+ }
+
+ if ((flags & CL_READ) && blkno == -1) {
+ vm_offset_t commit_offset;
+ int bytes_to_zero;
+ int complete_transaction_now = 0;
+
+ /*
+ * if we're reading and blkno == -1, then we've got a
+ * 'hole' in the file that we need to deal with by zeroing
+ * out the affected area in the upl
+ */
+ if (io_size >= (u_int)non_rounded_size) {
+ /*
+ * if this upl contains the EOF and it is not a multiple of PAGE_SIZE
+ * than 'zero_offset' will be non-zero
+ * if the 'hole' returned by vnop_blockmap extends all the way to the eof
+ * (indicated by the io_size finishing off the I/O request for this UPL)
+ * than we're not going to issue an I/O for the
+ * last page in this upl... we need to zero both the hole and the tail
+ * of the page beyond the EOF, since the delayed zero-fill won't kick in
+ */
+ bytes_to_zero = non_rounded_size;
+ if (!(flags & CL_NOZERO)) {
+ bytes_to_zero = (int)((((upl_offset + io_size) + (PAGE_SIZE - 1)) & ~PAGE_MASK) - upl_offset);
+ }
+
+ zero_offset = 0;
+ } else {
+ bytes_to_zero = io_size;
+ }
+
+ pg_count = 0;
+
+ cluster_zero(upl, (upl_offset_t)upl_offset, bytes_to_zero, real_bp);
+
+ if (cbp_head) {
+ int pg_resid;
+
+ /*
+ * if there is a current I/O chain pending
+ * then the first page of the group we just zero'd
+ * will be handled by the I/O completion if the zero
+ * fill started in the middle of the page
+ */
+ commit_offset = (upl_offset + (PAGE_SIZE - 1)) & ~PAGE_MASK;
+
+ pg_resid = (int)(commit_offset - upl_offset);
+
+ if (bytes_to_zero >= pg_resid) {
+ /*
+ * the last page of the current I/O
+ * has been completed...
+ * compute the number of fully zero'd
+ * pages that are beyond it
+ * plus the last page if its partial
+ * and we have no more I/O to issue...
+ * otherwise a partial page is left
+ * to begin the next I/O
+ */
+ if ((int)io_size >= non_rounded_size) {
+ pg_count = (bytes_to_zero - pg_resid + (PAGE_SIZE - 1)) / PAGE_SIZE;
+ } else {
+ pg_count = (bytes_to_zero - pg_resid) / PAGE_SIZE;
+ }
+
+ complete_transaction_now = 1;
+ }
+ } else {
+ /*
+ * no pending I/O to deal with
+ * so, commit all of the fully zero'd pages
+ * plus the last page if its partial
+ * and we have no more I/O to issue...
+ * otherwise a partial page is left
+ * to begin the next I/O
+ */
+ if ((int)io_size >= non_rounded_size) {
+ pg_count = (pg_offset + bytes_to_zero + (PAGE_SIZE - 1)) / PAGE_SIZE;
+ } else {
+ pg_count = (pg_offset + bytes_to_zero) / PAGE_SIZE;
+ }
+
+ commit_offset = upl_offset & ~PAGE_MASK;
+ }
+
+ // Associated UPL is currently only used in the direct write path
+ assert(!upl_associated_upl(upl));
+
+ if ((flags & CL_COMMIT) && pg_count) {
+ ubc_upl_commit_range(upl, (upl_offset_t)commit_offset,
+ pg_count * PAGE_SIZE,
+ UPL_COMMIT_CLEAR_DIRTY | UPL_COMMIT_FREE_ON_EMPTY);
+ }
+ upl_offset += io_size;
+ f_offset += io_size;
+ size -= io_size;
+
+ /*
+ * keep track of how much of the original request
+ * that we've actually completed... non_rounded_size
+ * may go negative due to us rounding the request
+ * to a page size multiple (i.e. size > non_rounded_size)
+ */
+ non_rounded_size -= io_size;
+
+ if (non_rounded_size <= 0) {
+ /*
+ * we've transferred all of the data in the original
+ * request, but we were unable to complete the tail
+ * of the last page because the file didn't have
+ * an allocation to back that portion... this is ok.
+ */
+ size = 0;
+ }
+ if (cbp_head && (complete_transaction_now || size == 0)) {
+ cluster_wait_IO(cbp_head, (flags & CL_ASYNC));
+
+ cluster_EOT(cbp_head, cbp_tail, size == 0 ? zero_offset : 0);
+
+ cluster_complete_transaction(&cbp_head, callback_arg, &retval, flags, 0);
+
+ trans_count = 0;
+ }
+ continue;
+ }
+ if (pg_count > max_vectors) {
+ if (((pg_count - max_vectors) * PAGE_SIZE) > io_size) {
+ io_size = PAGE_SIZE - pg_offset;
+ pg_count = 1;
+ } else {
+ io_size -= (pg_count - max_vectors) * PAGE_SIZE;
+ pg_count = max_vectors;
+ }
+ }
+ /*
+ * If the transaction is going to reach the maximum number of
+ * desired elements, truncate the i/o to the nearest page so
+ * that the actual i/o is initiated after this buffer is
+ * created and added to the i/o chain.
+ *
+ * I/O directed to physically contiguous memory
+ * doesn't have a requirement to make sure we 'fill' a page
+ */
+ if (!(flags & CL_DEV_MEMORY) && trans_count >= max_trans_count &&
+ ((upl_offset + io_size) & PAGE_MASK)) {
+ vm_offset_t aligned_ofs;
+
+ aligned_ofs = (upl_offset + io_size) & ~PAGE_MASK;
+ /*
+ * If the io_size does not actually finish off even a
+ * single page we have to keep adding buffers to the
+ * transaction despite having reached the desired limit.
+ *
+ * Eventually we get here with the page being finished
+ * off (and exceeded) and then we truncate the size of
+ * this i/o request so that it is page aligned so that
+ * we can finally issue the i/o on the transaction.
+ */
+ if (aligned_ofs > upl_offset) {
+ io_size = (u_int)(aligned_ofs - upl_offset);
+ pg_count--;
+ }
+ }
+
+ if (!(mp->mnt_kern_flag & MNTK_VIRTUALDEV)) {
+ /*
+ * if we're not targeting a virtual device i.e. a disk image
+ * it's safe to dip into the reserve pool since real devices
+ * can complete this I/O request without requiring additional
+ * bufs from the alloc_io_buf pool
+ */
+ priv = 1;
+ } else if ((flags & CL_ASYNC) && !(flags & CL_PAGEOUT) && !cbp_head) {
+ /*
+ * Throttle the speculative IO
+ *
+ * We can only throttle this if it is the first iobuf
+ * for the transaction. alloc_io_buf implements
+ * additional restrictions for diskimages anyway.
+ */
+ priv = 0;
+ } else {
+ priv = 1;
+ }
+
+ cbp = alloc_io_buf(vp, priv);
+
+ if (flags & CL_PAGEOUT) {
+ u_int i;
+
+ /*
+ * since blocks are in offsets of 0x1000, scale
+ * iteration to (PAGE_SIZE * pg_count) of blks.
+ */
+ for (i = 0; i < (PAGE_SIZE * pg_count) / 0x1000; i++) {
+ if (buf_invalblkno(vp, lblkno + i, 0) == EBUSY) {
+ panic("BUSY bp found in cluster_io");
+ }
+ }
+ }
+ if (flags & CL_ASYNC) {
+ if (buf_setcallback(cbp, (void *)cluster_iodone, callback_arg)) {
+ panic("buf_setcallback failed\n");
+ }
+ }
+ cbp->b_cliodone = (void *)callback;
+ cbp->b_flags |= io_flags;
+ if (flags & CL_NOCACHE) {
+ cbp->b_attr.ba_flags |= BA_NOCACHE;
+ }
+
+ cbp->b_lblkno = lblkno;
+ cbp->b_blkno = blkno;
+ cbp->b_bcount = io_size;
+
+ if (buf_setupl(cbp, upl, (uint32_t)upl_offset)) {
+ panic("buf_setupl failed\n");
+ }
+#if CONFIG_IOSCHED
+ upl_set_blkno(upl, upl_offset, io_size, blkno);
+#endif
+ cbp->b_trans_next = (buf_t)NULL;
+
+ if ((cbp->b_iostate = (void *)iostate)) {
+ /*
+ * caller wants to track the state of this
+ * io... bump the amount issued against this stream
+ */
+ iostate->io_issued += io_size;
+ }
+
+ if (flags & CL_READ) {
+ KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 26)) | DBG_FUNC_NONE,
+ (int)cbp->b_lblkno, (int)cbp->b_blkno, upl_offset, io_size, 0);
+ } else {
+ KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 27)) | DBG_FUNC_NONE,
+ (int)cbp->b_lblkno, (int)cbp->b_blkno, upl_offset, io_size, 0);
+ }
+
+ if (cbp_head) {
+ cbp_tail->b_trans_next = cbp;
+ cbp_tail = cbp;
+ } else {
+ cbp_head = cbp;
+ cbp_tail = cbp;
+
+ if ((cbp_head->b_real_bp = real_bp)) {
+ real_bp = (buf_t)NULL;
+ }
+ }
+ *(buf_t *)(&cbp->b_trans_head) = cbp_head;
+
+ trans_count++;
+
+ upl_offset += io_size;
+ f_offset += io_size;
+ size -= io_size;
+ /*
+ * keep track of how much of the original request
+ * that we've actually completed... non_rounded_size
+ * may go negative due to us rounding the request
+ * to a page size multiple (i.e. size > non_rounded_size)
+ */
+ non_rounded_size -= io_size;
+
+ if (non_rounded_size <= 0) {
+ /*
+ * we've transferred all of the data in the original
+ * request, but we were unable to complete the tail
+ * of the last page because the file didn't have
+ * an allocation to back that portion... this is ok.
+ */
+ size = 0;
+ }
+ if (size == 0) {
+ /*
+ * we have no more I/O to issue, so go
+ * finish the final transaction
+ */
+ need_EOT = TRUE;
+ } else if (((flags & CL_DEV_MEMORY) || (upl_offset & PAGE_MASK) == 0) &&
+ ((flags & CL_ASYNC) || trans_count > max_trans_count)) {
+ /*
+ * I/O directed to physically contiguous memory...
+ * which doesn't have a requirement to make sure we 'fill' a page
+ * or...
+ * the current I/O we've prepared fully
+ * completes the last page in this request
+ * and ...
+ * it's either an ASYNC request or
+ * we've already accumulated more than 8 I/O's into
+ * this transaction so mark it as complete so that
+ * it can finish asynchronously or via the cluster_complete_transaction
+ * below if the request is synchronous
+ */
+ need_EOT = TRUE;
+ }
+ if (need_EOT == TRUE) {
+ cluster_EOT(cbp_head, cbp_tail, size == 0 ? zero_offset : 0);
+ }
+
+ if (flags & CL_THROTTLE) {
+ (void)vnode_waitforwrites(vp, async_throttle, 0, 0, "cluster_io");
+ }
+
+ if (!(io_flags & B_READ)) {
+ vnode_startwrite(vp);
+ }
+
+ if (flags & CL_RAW_ENCRYPTED) {
+ /*
+ * User requested raw encrypted bytes.
+ * Twiddle the bit in the ba_flags for the buffer
+ */
+ cbp->b_attr.ba_flags |= BA_RAW_ENCRYPTED_IO;
+ }
+
+ (void) VNOP_STRATEGY(cbp);
+
+ if (need_EOT == TRUE) {
+ if (!(flags & CL_ASYNC)) {
+ cluster_complete_transaction(&cbp_head, callback_arg, &retval, flags, 1);
+ }
+
+ need_EOT = FALSE;
+ trans_count = 0;
+ cbp_head = NULL;
+ }
+ }
+ if (error) {
+ int abort_size;
+
+ io_size = 0;
+
+ if (cbp_head) {
+ /*
+ * Wait until all of the outstanding I/O
+ * for this partial transaction has completed
+ */
+ cluster_wait_IO(cbp_head, (flags & CL_ASYNC));
+
+ /*
+ * Rewind the upl offset to the beginning of the
+ * transaction.
+ */
+ upl_offset = cbp_head->b_uploffset;
+ }
+
+ if (ISSET(flags, CL_COMMIT)) {
+ cluster_handle_associated_upl(iostate, upl,
+ (upl_offset_t)upl_offset,
+ (upl_size_t)(upl_end_offset - upl_offset));
+ }
+
+ // Free all the IO buffers in this transaction
+ for (cbp = cbp_head; cbp;) {
+ buf_t cbp_next;
+
+ size += cbp->b_bcount;
+ io_size += cbp->b_bcount;
+
+ cbp_next = cbp->b_trans_next;
+ free_io_buf(cbp);
+ cbp = cbp_next;
+ }
+
+ if (iostate) {
+ int need_wakeup = 0;
+
+ /*
+ * update the error condition for this stream
+ * since we never really issued the io
+ * just go ahead and adjust it back
+ */
+ lck_mtx_lock_spin(&iostate->io_mtxp);
+
+ if (iostate->io_error == 0) {
+ iostate->io_error = error;
+ }
+ iostate->io_issued -= io_size;
+
+ if (iostate->io_wanted) {
+ /*
+ * someone is waiting for the state of
+ * this io stream to change
+ */
+ iostate->io_wanted = 0;
+ need_wakeup = 1;
+ }
+ lck_mtx_unlock(&iostate->io_mtxp);
+
+ if (need_wakeup) {
+ wakeup((caddr_t)&iostate->io_wanted);
+ }
+ }
+
+ if (flags & CL_COMMIT) {
+ int upl_flags;
+
+ pg_offset = upl_offset & PAGE_MASK;
+ abort_size = (int)((upl_end_offset - upl_offset + PAGE_MASK) & ~PAGE_MASK);
+
+ upl_flags = cluster_ioerror(upl, (int)(upl_offset - pg_offset),
+ abort_size, error, io_flags, vp);
+
+ KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 28)) | DBG_FUNC_NONE,
+ upl, upl_offset - pg_offset, abort_size, (error << 24) | upl_flags, 0);
+ }
+ if (retval == 0) {
+ retval = error;
+ }
+ } else if (cbp_head) {
+ panic("%s(): cbp_head is not NULL.\n", __FUNCTION__);
+ }
+
+ if (real_bp) {
+ /*
+ * can get here if we either encountered an error
+ * or we completely zero-filled the request and
+ * no I/O was issued
+ */
+ if (error) {
+ real_bp->b_flags |= B_ERROR;
+ real_bp->b_error = error;
+ }
+ buf_biodone(real_bp);
+ }
+ KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 22)) | DBG_FUNC_END, (int)f_offset, size, upl_offset, retval, 0);
+
+ return retval;
+}
+
+#define reset_vector_run_state() \
+ issueVectorUPL = vector_upl_offset = vector_upl_index = vector_upl_iosize = vector_upl_size = 0;
+
+static int
+vector_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,
+ int io_flag, buf_t real_bp, struct clios *iostate, int (*callback)(buf_t, void *), void *callback_arg)
+{
+ vector_upl_set_pagelist(vector_upl);
+
+ if (io_flag & CL_READ) {
+ if (vector_upl_offset == 0 && ((vector_upl_iosize & PAGE_MASK) == 0)) {
+ io_flag &= ~CL_PRESERVE; /*don't zero fill*/
+ } else {
+ io_flag |= CL_PRESERVE; /*zero fill*/
+ }
+ }
+ return cluster_io(vp, vector_upl, vector_upl_offset, v_upl_uio_offset, vector_upl_iosize, io_flag, real_bp, iostate, callback, callback_arg);
+}
+
+static 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)
+{
+ int pages_in_prefetch;
+
+ KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 49)) | DBG_FUNC_START,
+ (int)f_offset, size, (int)filesize, 0, 0);
+
+ if (f_offset >= filesize) {
+ KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 49)) | DBG_FUNC_END,
+ (int)f_offset, 0, 0, 0, 0);
+ return 0;
+ }
+ if ((off_t)size > (filesize - f_offset)) {
+ size = (u_int)(filesize - f_offset);
+ }
+ pages_in_prefetch = (size + (PAGE_SIZE - 1)) / PAGE_SIZE;
+
+ advisory_read_ext(vp, filesize, f_offset, size, callback, callback_arg, bflag);
+
+ KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 49)) | DBG_FUNC_END,
+ (int)f_offset + size, pages_in_prefetch, 0, 1, 0);
+
+ return pages_in_prefetch;
+}
+
+
+
+static void
+cluster_read_ahead(vnode_t vp, struct cl_extent *extent, off_t filesize, struct cl_readahead *rap, int (*callback)(buf_t, void *), void *callback_arg,
+ int bflag)
+{
+ daddr64_t r_addr;
+ off_t f_offset;
+ int size_of_prefetch;
+ u_int max_prefetch;
+
+
+ KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 48)) | DBG_FUNC_START,
+ (int)extent->b_addr, (int)extent->e_addr, (int)rap->cl_lastr, 0, 0);
+
+ if (extent->b_addr == rap->cl_lastr && extent->b_addr == extent->e_addr) {
+ KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 48)) | DBG_FUNC_END,
+ rap->cl_ralen, (int)rap->cl_maxra, (int)rap->cl_lastr, 0, 0);
+ return;
+ }
+ if (rap->cl_lastr == -1 || (extent->b_addr != rap->cl_lastr && extent->b_addr != (rap->cl_lastr + 1))) {
+ rap->cl_ralen = 0;
+ rap->cl_maxra = 0;
+
+ KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 48)) | DBG_FUNC_END,
+ rap->cl_ralen, (int)rap->cl_maxra, (int)rap->cl_lastr, 1, 0);
+
+ return;
+ }
+ max_prefetch = MAX_PREFETCH(vp, cluster_max_io_size(vp->v_mount, CL_READ), disk_conditioner_mount_is_ssd(vp->v_mount));
+
+ if (max_prefetch > speculative_prefetch_max) {
+ max_prefetch = speculative_prefetch_max;
+ }
+
+ if (max_prefetch <= PAGE_SIZE) {
+ KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 48)) | DBG_FUNC_END,
+ rap->cl_ralen, (int)rap->cl_maxra, (int)rap->cl_lastr, 6, 0);
+ return;
+ }
+ if (extent->e_addr < rap->cl_maxra && rap->cl_ralen >= 4) {
+ if ((rap->cl_maxra - extent->e_addr) > (rap->cl_ralen / 4)) {
+ KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 48)) | DBG_FUNC_END,
+ rap->cl_ralen, (int)rap->cl_maxra, (int)rap->cl_lastr, 2, 0);
+ return;
+ }
+ }
+ r_addr = MAX(extent->e_addr, rap->cl_maxra) + 1;
+ f_offset = (off_t)(r_addr * PAGE_SIZE_64);
+
+ size_of_prefetch = 0;
+
+ ubc_range_op(vp, f_offset, f_offset + PAGE_SIZE_64, UPL_ROP_PRESENT, &size_of_prefetch);
+
+ if (size_of_prefetch) {
+ KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 48)) | DBG_FUNC_END,
+ rap->cl_ralen, (int)rap->cl_maxra, (int)rap->cl_lastr, 3, 0);
+ return;
+ }
+ if (f_offset < filesize) {
+ daddr64_t read_size;
+
+ rap->cl_ralen = rap->cl_ralen ? min(max_prefetch / PAGE_SIZE, rap->cl_ralen << 1) : 1;
+
+ read_size = (extent->e_addr + 1) - extent->b_addr;
+
+ if (read_size > rap->cl_ralen) {
+ if (read_size > max_prefetch / PAGE_SIZE) {
+ rap->cl_ralen = max_prefetch / PAGE_SIZE;
+ } else {
+ rap->cl_ralen = (int)read_size;
+ }
+ }
+ size_of_prefetch = cluster_read_prefetch(vp, f_offset, rap->cl_ralen * PAGE_SIZE, filesize, callback, callback_arg, bflag);
+
+ if (size_of_prefetch) {
+ rap->cl_maxra = (r_addr + size_of_prefetch) - 1;
+ }
+ }
+ KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 48)) | DBG_FUNC_END,
+ rap->cl_ralen, (int)rap->cl_maxra, (int)rap->cl_lastr, 4, 0);
+}
+
+
+int
+cluster_pageout(vnode_t vp, upl_t upl, upl_offset_t upl_offset, off_t f_offset,
+ int size, off_t filesize, int flags)
+{
+ return cluster_pageout_ext(vp, upl, upl_offset, f_offset, size, filesize, flags, NULL, NULL);
+}
+
+
+int
+cluster_pageout_ext(vnode_t vp, upl_t upl, upl_offset_t upl_offset, off_t f_offset,
+ int size, off_t filesize, int flags, int (*callback)(buf_t, void *), void *callback_arg)
+{
+ int io_size;
+ int rounded_size;
+ off_t max_size;
+ int local_flags;
+
+ local_flags = CL_PAGEOUT | CL_THROTTLE;
+
+ if ((flags & UPL_IOSYNC) == 0) {
+ local_flags |= CL_ASYNC;
+ }
+ if ((flags & UPL_NOCOMMIT) == 0) {
+ local_flags |= CL_COMMIT;
+ }
+ if ((flags & UPL_KEEPCACHED)) {
+ local_flags |= CL_KEEPCACHED;
+ }
+ if (flags & UPL_PAGING_ENCRYPTED) {
+ local_flags |= CL_ENCRYPTED;
+ }
+
+
+ KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 52)) | DBG_FUNC_NONE,
+ (int)f_offset, size, (int)filesize, local_flags, 0);
+
+ /*
+ * If they didn't specify any I/O, then we are done...
+ * we can't issue an abort because we don't know how
+ * big the upl really is
+ */
+ if (size <= 0) {
+ return EINVAL;
+ }
+
+ if (vp->v_mount->mnt_flag & MNT_RDONLY) {
+ if (local_flags & CL_COMMIT) {
+ ubc_upl_abort_range(upl, upl_offset, size, UPL_ABORT_FREE_ON_EMPTY);
+ }
+ return EROFS;
+ }
+ /*
+ * can't page-in from a negative offset
+ * or if we're starting beyond the EOF
+ * or if the file offset isn't page aligned
+ * or the size requested isn't a multiple of PAGE_SIZE
+ */
+ if (f_offset < 0 || f_offset >= filesize ||
+ (f_offset & PAGE_MASK_64) || (size & PAGE_MASK)) {
+ if (local_flags & CL_COMMIT) {
+ ubc_upl_abort_range(upl, upl_offset, size, UPL_ABORT_FREE_ON_EMPTY);
+ }
+ return EINVAL;
+ }
+ max_size = filesize - f_offset;
+
+ if (size < max_size) {
+ io_size = size;
+ } else {
+ io_size = (int)max_size;
+ }
+
+ rounded_size = (io_size + (PAGE_SIZE - 1)) & ~PAGE_MASK;
+
+ if (size > rounded_size) {
+ if (local_flags & CL_COMMIT) {
+ ubc_upl_abort_range(upl, upl_offset + rounded_size, size - rounded_size,
+ UPL_ABORT_FREE_ON_EMPTY);
+ }
+ }
+ return cluster_io(vp, upl, upl_offset, f_offset, io_size,
+ local_flags, (buf_t)NULL, (struct clios *)NULL, callback, callback_arg);
+}
+
+
+int
+cluster_pagein(vnode_t vp, upl_t upl, upl_offset_t upl_offset, off_t f_offset,
+ int size, off_t filesize, int flags)
+{
+ return cluster_pagein_ext(vp, upl, upl_offset, f_offset, size, filesize, flags, NULL, NULL);
+}
+
+
+int
+cluster_pagein_ext(vnode_t vp, upl_t upl, upl_offset_t upl_offset, off_t f_offset,
+ int size, off_t filesize, int flags, int (*callback)(buf_t, void *), void *callback_arg)
+{
+ u_int io_size;
+ int rounded_size;
+ off_t max_size;
+ int retval;
+ int local_flags = 0;
+
+ if (upl == NULL || size < 0) {
+ panic("cluster_pagein: NULL upl passed in");
+ }
+
+ if ((flags & UPL_IOSYNC) == 0) {
+ local_flags |= CL_ASYNC;
+ }
+ if ((flags & UPL_NOCOMMIT) == 0) {
+ local_flags |= CL_COMMIT;
+ }
+ if (flags & UPL_IOSTREAMING) {
+ local_flags |= CL_IOSTREAMING;
+ }
+ if (flags & UPL_PAGING_ENCRYPTED) {
+ local_flags |= CL_ENCRYPTED;
+ }
+
+
+ KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 56)) | DBG_FUNC_NONE,
+ (int)f_offset, size, (int)filesize, local_flags, 0);
+
+ /*
+ * can't page-in from a negative offset
+ * or if we're starting beyond the EOF
+ * or if the file offset isn't page aligned
+ * or the size requested isn't a multiple of PAGE_SIZE
+ */
+ if (f_offset < 0 || f_offset >= filesize ||
+ (f_offset & PAGE_MASK_64) || (size & PAGE_MASK) || (upl_offset & PAGE_MASK)) {
+ if (local_flags & CL_COMMIT) {
+ ubc_upl_abort_range(upl, upl_offset, size, UPL_ABORT_FREE_ON_EMPTY | UPL_ABORT_ERROR);
+ }
+ return EINVAL;
+ }
+ max_size = filesize - f_offset;
+
+ if (size < max_size) {
+ io_size = size;
+ } else {
+ io_size = (int)max_size;
+ }
+
+ rounded_size = (io_size + (PAGE_SIZE - 1)) & ~PAGE_MASK;
+
+ if (size > rounded_size && (local_flags & CL_COMMIT)) {
+ ubc_upl_abort_range(upl, upl_offset + rounded_size,
+ size - rounded_size, UPL_ABORT_FREE_ON_EMPTY | UPL_ABORT_ERROR);
+ }
+
+ retval = cluster_io(vp, upl, upl_offset, f_offset, io_size,
+ local_flags | CL_READ | CL_PAGEIN, (buf_t)NULL, (struct clios *)NULL, callback, callback_arg);
+
+ return retval;
+}
+
+
+int
+cluster_bp(buf_t bp)
+{
+ return cluster_bp_ext(bp, NULL, NULL);
+}
+
+
+int
+cluster_bp_ext(buf_t bp, int (*callback)(buf_t, void *), void *callback_arg)
+{
+ off_t f_offset;
+ int flags;
+
+ KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 19)) | DBG_FUNC_START,
+ bp, (int)bp->b_lblkno, bp->b_bcount, bp->b_flags, 0);
+
+ if (bp->b_flags & B_READ) {
+ flags = CL_ASYNC | CL_READ;
+ } else {
+ flags = CL_ASYNC;
+ }
+ if (bp->b_flags & B_PASSIVE) {
+ flags |= CL_PASSIVE;
+ }
+
+ f_offset = ubc_blktooff(bp->b_vp, bp->b_lblkno);
+
+ return cluster_io(bp->b_vp, bp->b_upl, 0, f_offset, bp->b_bcount, flags, bp, (struct clios *)NULL, callback, callback_arg);
+}
+
+
+
+int
+cluster_write(vnode_t vp, struct uio *uio, off_t oldEOF, off_t newEOF, off_t headOff, off_t tailOff, int xflags)
+{
+ return cluster_write_ext(vp, uio, oldEOF, newEOF, headOff, tailOff, xflags, NULL, NULL);
+}
+
+
+int
+cluster_write_ext(vnode_t vp, struct uio *uio, off_t oldEOF, off_t newEOF, off_t headOff, off_t tailOff,
+ int xflags, int (*callback)(buf_t, void *), void *callback_arg)
+{
+ user_ssize_t cur_resid;
+ int retval = 0;
+ int flags;
+ int zflags;
+ int bflag;
+ int write_type = IO_COPY;
+ u_int32_t write_length;
+
+ flags = xflags;
+
+ if (flags & IO_PASSIVE) {
+ bflag = CL_PASSIVE;
+ } else {
+ bflag = 0;
+ }
+
+ if (vp->v_flag & VNOCACHE_DATA) {
+ flags |= IO_NOCACHE;
+ bflag |= CL_NOCACHE;
+ }
+ if (uio == NULL) {
+ /*
+ * no user data...
+ * this call is being made to zero-fill some range in the file
+ */
+ retval = cluster_write_copy(vp, NULL, (u_int32_t)0, oldEOF, newEOF, headOff, tailOff, flags, callback, callback_arg);
+
+ return retval;
+ }
+ /*
+ * do a write through the cache if one of the following is true....
+ * NOCACHE is not true or NODIRECT is true
+ * the uio request doesn't target USERSPACE
+ * otherwise, find out if we want the direct or contig variant for
+ * the first vector in the uio request
+ */
+ if (((flags & (IO_NOCACHE | IO_NODIRECT)) == IO_NOCACHE) && UIO_SEG_IS_USER_SPACE(uio->uio_segflg)) {
+ retval = cluster_io_type(uio, &write_type, &write_length, MIN_DIRECT_WRITE_SIZE);
+ }
+
+ if ((flags & (IO_TAILZEROFILL | IO_HEADZEROFILL)) && write_type == IO_DIRECT) {
+ /*
+ * must go through the cached variant in this case
+ */
+ write_type = IO_COPY;
+ }
+
+ while ((cur_resid = uio_resid(uio)) && uio->uio_offset < newEOF && retval == 0) {
+ switch (write_type) {
+ case IO_COPY:
+ /*
+ * make sure the uio_resid isn't too big...
+ * internally, we want to handle all of the I/O in
+ * chunk sizes that fit in a 32 bit int
+ */
+ if (cur_resid > (user_ssize_t)(MAX_IO_REQUEST_SIZE)) {
+ /*
+ * we're going to have to call cluster_write_copy
+ * more than once...
+ *
+ * only want the last call to cluster_write_copy to
+ * have the IO_TAILZEROFILL flag set and only the
+ * first call should have IO_HEADZEROFILL
+ */
+ zflags = flags & ~IO_TAILZEROFILL;
+ flags &= ~IO_HEADZEROFILL;
+
+ write_length = MAX_IO_REQUEST_SIZE;
+ } else {
+ /*
+ * last call to cluster_write_copy
+ */
+ zflags = flags;
+
+ write_length = (u_int32_t)cur_resid;
+ }
+ retval = cluster_write_copy(vp, uio, write_length, oldEOF, newEOF, headOff, tailOff, zflags, callback, callback_arg);
+ break;
+
+ case IO_CONTIG:
+ zflags = flags & ~(IO_TAILZEROFILL | IO_HEADZEROFILL);
+
+ if (flags & IO_HEADZEROFILL) {
+ /*
+ * only do this once per request
+ */
+ flags &= ~IO_HEADZEROFILL;
+
+ retval = cluster_write_copy(vp, (struct uio *)0, (u_int32_t)0, (off_t)0, uio->uio_offset,
+ headOff, (off_t)0, zflags | IO_HEADZEROFILL | IO_SYNC, callback, callback_arg);
+ if (retval) {
+ break;
+ }
+ }
+ retval = cluster_write_contig(vp, uio, newEOF, &write_type, &write_length, callback, callback_arg, bflag);
+
+ if (retval == 0 && (flags & IO_TAILZEROFILL) && uio_resid(uio) == 0) {
+ /*
+ * we're done with the data from the user specified buffer(s)
+ * and we've been requested to zero fill at the tail
+ * treat this as an IO_HEADZEROFILL which doesn't require a uio
+ * by rearranging the args and passing in IO_HEADZEROFILL
+ */
+ retval = cluster_write_copy(vp, (struct uio *)0, (u_int32_t)0, (off_t)0, tailOff, uio->uio_offset,
+ (off_t)0, zflags | IO_HEADZEROFILL | IO_SYNC, callback, callback_arg);
+ }
+ break;
+
+ case IO_DIRECT:
+ /*
+ * cluster_write_direct is never called with IO_TAILZEROFILL || IO_HEADZEROFILL
+ */
+ retval = cluster_write_direct(vp, uio, oldEOF, newEOF, &write_type, &write_length, flags, callback, callback_arg);
+ break;
+
+ case IO_UNKNOWN:
+ retval = cluster_io_type(uio, &write_type, &write_length, MIN_DIRECT_WRITE_SIZE);
+ break;
+ }
+ /*
+ * in case we end up calling cluster_write_copy (from cluster_write_direct)
+ * multiple times to service a multi-vector request that is not aligned properly
+ * we need to update the oldEOF so that we
+ * don't zero-fill the head of a page if we've successfully written
+ * data to that area... 'cluster_write_copy' will zero-fill the head of a
+ * page that is beyond the oldEOF if the write is unaligned... we only
+ * want that to happen for the very first page of the cluster_write,
+ * NOT the first page of each vector making up a multi-vector write.
+ */
+ if (uio->uio_offset > oldEOF) {
+ oldEOF = uio->uio_offset;
+ }
+ }
+ return retval;
+}
+
+
+static int
+cluster_write_direct(vnode_t vp, struct uio *uio, off_t oldEOF, off_t newEOF, int *write_type, u_int32_t *write_length,
+ int flags, int (*callback)(buf_t, void *), void *callback_arg)
+{
+ upl_t upl;
+ upl_page_info_t *pl;
+ vm_offset_t upl_offset;
+ vm_offset_t vector_upl_offset = 0;
+ u_int32_t io_req_size;
+ u_int32_t offset_in_file;
+ u_int32_t offset_in_iovbase;
+ u_int32_t io_size;
+ int io_flag = 0;
+ upl_size_t upl_size, vector_upl_size = 0;
+ vm_size_t upl_needed_size;
+ mach_msg_type_number_t pages_in_pl;
+ upl_control_flags_t upl_flags;
+ kern_return_t kret;
+ mach_msg_type_number_t i;
+ int force_data_sync;
+ int retval = 0;
+ int first_IO = 1;
+ struct clios iostate;
+ user_addr_t iov_base;
+ u_int32_t mem_alignment_mask;
+ u_int32_t devblocksize;
+ u_int32_t max_io_size;
+ u_int32_t max_upl_size;
+ u_int32_t max_vector_size;
+ u_int32_t bytes_outstanding_limit;
+ boolean_t io_throttled = FALSE;
+
+ u_int32_t vector_upl_iosize = 0;
+ int issueVectorUPL = 0, useVectorUPL = (uio->uio_iovcnt > 1);
+ off_t v_upl_uio_offset = 0;
+ int vector_upl_index = 0;
+ upl_t vector_upl = NULL;
+
+
+ /*
+ * When we enter this routine, we know
+ * -- the resid will not exceed iov_len
+ */
+ KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 75)) | DBG_FUNC_START,
+ (int)uio->uio_offset, *write_length, (int)newEOF, 0, 0);
+
+ assert(vm_map_page_shift(current_map()) >= PAGE_SHIFT);
+
+ max_upl_size = cluster_max_io_size(vp->v_mount, CL_WRITE);
+
+ io_flag = CL_ASYNC | CL_PRESERVE | CL_COMMIT | CL_THROTTLE | CL_DIRECT_IO;
+
+ if (flags & IO_PASSIVE) {
+ io_flag |= CL_PASSIVE;
+ }
+
+ if (flags & IO_NOCACHE) {
+ io_flag |= CL_NOCACHE;
+ }
+
+ if (flags & IO_SKIP_ENCRYPTION) {
+ io_flag |= CL_ENCRYPTED;
+ }
+
+ iostate.io_completed = 0;
+ iostate.io_issued = 0;
+ iostate.io_error = 0;
+ iostate.io_wanted = 0;
+
+ lck_mtx_init(&iostate.io_mtxp, &cl_mtx_grp, LCK_ATTR_NULL);
+
+ mem_alignment_mask = (u_int32_t)vp->v_mount->mnt_alignmentmask;
+ devblocksize = (u_int32_t)vp->v_mount->mnt_devblocksize;
+
+ if (devblocksize == 1) {
+ /*
+ * the AFP client advertises a devblocksize of 1
+ * however, its BLOCKMAP routine maps to physical
+ * blocks that are PAGE_SIZE in size...
+ * therefore we can't ask for I/Os that aren't page aligned
+ * or aren't multiples of PAGE_SIZE in size
+ * by setting devblocksize to PAGE_SIZE, we re-instate
+ * the old behavior we had before the mem_alignment_mask
+ * changes went in...
+ */
+ devblocksize = PAGE_SIZE;
+ }
+
+next_dwrite:
+ io_req_size = *write_length;
+ iov_base = uio_curriovbase(uio);
+
+ offset_in_file = (u_int32_t)uio->uio_offset & PAGE_MASK;
+ offset_in_iovbase = (u_int32_t)iov_base & mem_alignment_mask;
+
+ if (offset_in_file || offset_in_iovbase) {
+ /*
+ * one of the 2 important offsets is misaligned
+ * so fire an I/O through the cache for this entire vector
+ */
+ goto wait_for_dwrites;
+ }
+ if (iov_base & (devblocksize - 1)) {
+ /*
+ * the offset in memory must be on a device block boundary
+ * so that we can guarantee that we can generate an
+ * I/O that ends on a page boundary in cluster_io
+ */
+ goto wait_for_dwrites;
+ }
+
+ task_update_logical_writes(current_task(), (io_req_size & ~PAGE_MASK), TASK_WRITE_IMMEDIATE, vp);
+ while (io_req_size >= PAGE_SIZE && uio->uio_offset < newEOF && retval == 0) {
+ int throttle_type;
+
+ if ((throttle_type = cluster_is_throttled(vp))) {
+ /*
+ * we're in the throttle window, at the very least
+ * we want to limit the size of the I/O we're about
+ * to issue
+ */
+ if ((flags & IO_RETURN_ON_THROTTLE) && throttle_type == THROTTLE_NOW) {
+ /*
+ * we're in the throttle window and at least 1 I/O
+ * has already been issued by a throttleable thread
+ * in this window, so return with EAGAIN to indicate
+ * to the FS issuing the cluster_write call that it
+ * should now throttle after dropping any locks
+ */
+ throttle_info_update_by_mount(vp->v_mount);
+
+ io_throttled = TRUE;
+ goto wait_for_dwrites;
+ }
+ max_vector_size = THROTTLE_MAX_IOSIZE;
+ max_io_size = THROTTLE_MAX_IOSIZE;
+ } else {
+ max_vector_size = MAX_VECTOR_UPL_SIZE;
+ max_io_size = max_upl_size;
+ }
+
+ if (first_IO) {
+ cluster_syncup(vp, newEOF, callback, callback_arg, callback ? PUSH_SYNC : 0);
+ first_IO = 0;
+ }
+ io_size = io_req_size & ~PAGE_MASK;
+ iov_base = uio_curriovbase(uio);
+
+ if (io_size > max_io_size) {
+ io_size = max_io_size;
+ }
+
+ if (useVectorUPL && (iov_base & PAGE_MASK)) {
+ /*
+ * We have an iov_base that's not page-aligned.
+ * Issue all I/O's that have been collected within
+ * this Vectored UPL.
+ */
+ if (vector_upl_index) {
+ 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);
+ reset_vector_run_state();
+ }
+
+ /*
+ * After this point, if we are using the Vector UPL path and the base is
+ * not page-aligned then the UPL with that base will be the first in the vector UPL.
+ */
+ }
+
+ upl_offset = (vm_offset_t)((u_int32_t)iov_base & PAGE_MASK);
+ upl_needed_size = (upl_offset + io_size + (PAGE_SIZE - 1)) & ~PAGE_MASK;
+
+ KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 76)) | DBG_FUNC_START,
+ (int)upl_offset, upl_needed_size, (int)iov_base, io_size, 0);
+
+ vm_map_t map = UIO_SEG_IS_USER_SPACE(uio->uio_segflg) ? current_map() : kernel_map;
+ for (force_data_sync = 0; force_data_sync < 3; force_data_sync++) {
+ pages_in_pl = 0;
+ upl_size = (upl_size_t)upl_needed_size;
+ upl_flags = UPL_FILE_IO | UPL_COPYOUT_FROM | UPL_NO_SYNC |
+ UPL_CLEAN_IN_PLACE | UPL_SET_INTERNAL | UPL_SET_LITE | UPL_SET_IO_WIRE;
+
+ kret = vm_map_get_upl(map,
+ vm_map_trunc_page(iov_base, vm_map_page_mask(map)),
+ &upl_size,
+ &upl,
+ NULL,
+ &pages_in_pl,
+ &upl_flags,
+ VM_KERN_MEMORY_FILE,
+ force_data_sync);
+
+ if (kret != KERN_SUCCESS) {
+ KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 76)) | DBG_FUNC_END,
+ 0, 0, 0, kret, 0);
+ /*
+ * failed to get pagelist
+ *
+ * we may have already spun some portion of this request
+ * off as async requests... we need to wait for the I/O
+ * to complete before returning
+ */
+ goto wait_for_dwrites;
+ }
+ pl = UPL_GET_INTERNAL_PAGE_LIST(upl);
+ pages_in_pl = upl_size / PAGE_SIZE;
+
+ for (i = 0; i < pages_in_pl; i++) {
+ if (!upl_valid_page(pl, i)) {
+ break;
+ }
+ }
+ if (i == pages_in_pl) {
+ break;
+ }
+
+ /*
+ * didn't get all the pages back that we
+ * needed... release this upl and try again
+ */
+ ubc_upl_abort(upl, 0);
+ }
+ if (force_data_sync >= 3) {
+ KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 76)) | DBG_FUNC_END,
+ i, pages_in_pl, upl_size, kret, 0);
+ /*
+ * for some reason, we couldn't acquire a hold on all
+ * the pages needed in the user's address space
+ *
+ * we may have already spun some portion of this request
+ * off as async requests... we need to wait for the I/O
+ * to complete before returning
+ */
+ goto wait_for_dwrites;
+ }
+
+ /*
+ * Consider the possibility that upl_size wasn't satisfied.
+ */
+ if (upl_size < upl_needed_size) {
+ if (upl_size && upl_offset == 0) {
+ io_size = upl_size;
+ } else {
+ io_size = 0;
+ }
+ }
+ KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 76)) | DBG_FUNC_END,
+ (int)upl_offset, upl_size, (int)iov_base, io_size, 0);
+
+ if (io_size == 0) {
+ ubc_upl_abort(upl, 0);
+ /*
+ * we may have already spun some portion of this request
+ * off as async requests... we need to wait for the I/O
+ * to complete before returning
+ */
+ goto wait_for_dwrites;
+ }
+
+ if (useVectorUPL) {
+ vm_offset_t end_off = ((iov_base + io_size) & PAGE_MASK);
+ if (end_off) {
+ issueVectorUPL = 1;
+ }
+ /*
+ * After this point, if we are using a vector UPL, then
+ * either all the UPL elements end on a page boundary OR
+ * this UPL is the last element because it does not end
+ * on a page boundary.
+ */
+ }
+
+ /*
+ * we want push out these writes asynchronously so that we can overlap
+ * the preparation of the next I/O
+ * if there are already too many outstanding writes
+ * wait until some complete before issuing the next
+ */
+ if (vp->v_mount->mnt_minsaturationbytecount) {
+ bytes_outstanding_limit = vp->v_mount->mnt_minsaturationbytecount;
+ } else {
+ bytes_outstanding_limit = max_upl_size * IO_SCALE(vp, 2);
+ }
+
+ cluster_iostate_wait(&iostate, bytes_outstanding_limit, "cluster_write_direct");
+
+ if (iostate.io_error) {
+ /*
+ * one of the earlier writes we issued ran into a hard error
+ * don't issue any more writes, cleanup the UPL
+ * that was just created but not used, then
+ * go wait for all writes that are part of this stream
+ * to complete before returning the error to the caller
+ */
+ ubc_upl_abort(upl, 0);
+
+ goto wait_for_dwrites;
+ }
+
+ KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 77)) | DBG_FUNC_START,
+ (int)upl_offset, (int)uio->uio_offset, io_size, io_flag, 0);
+
+ if (!useVectorUPL) {
+ retval = cluster_io(vp, upl, upl_offset, uio->uio_offset,
+ io_size, io_flag, (buf_t)NULL, &iostate, callback, callback_arg);
+ } else {
+ if (!vector_upl_index) {
+ vector_upl = vector_upl_create(upl_offset);
+ v_upl_uio_offset = uio->uio_offset;
+ vector_upl_offset = upl_offset;
+ }
+
+ vector_upl_set_subupl(vector_upl, upl, upl_size);
+ vector_upl_set_iostate(vector_upl, upl, vector_upl_size, upl_size);
+ vector_upl_index++;
+ vector_upl_iosize += io_size;
+ vector_upl_size += upl_size;
+
+ if (issueVectorUPL || vector_upl_index == MAX_VECTOR_UPL_ELEMENTS || vector_upl_size >= max_vector_size) {
+ 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);
+ reset_vector_run_state();
+ }
+ }
+
+ /*
+ * update the uio structure to
+ * reflect the I/O that we just issued
+ */
+ uio_update(uio, (user_size_t)io_size);
+
+ /*
+ * in case we end up calling through to cluster_write_copy to finish
+ * the tail of this request, we need to update the oldEOF so that we
+ * don't zero-fill the head of a page if we've successfully written
+ * data to that area... 'cluster_write_copy' will zero-fill the head of a
+ * page that is beyond the oldEOF if the write is unaligned... we only
+ * want that to happen for the very first page of the cluster_write,
+ * NOT the first page of each vector making up a multi-vector write.
+ */
+ if (uio->uio_offset > oldEOF) {
+ oldEOF = uio->uio_offset;
+ }
+
+ io_req_size -= io_size;
+
+ KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 77)) | DBG_FUNC_END,
+ (int)upl_offset, (int)uio->uio_offset, io_req_size, retval, 0);
+ } /* end while */
+
+ if (retval == 0 && iostate.io_error == 0 && io_req_size == 0) {
+ retval = cluster_io_type(uio, write_type, write_length, MIN_DIRECT_WRITE_SIZE);
+
+ if (retval == 0 && *write_type == IO_DIRECT) {
+ KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 75)) | DBG_FUNC_NONE,
+ (int)uio->uio_offset, *write_length, (int)newEOF, 0, 0);
+
+ goto next_dwrite;
+ }
+ }
+
+wait_for_dwrites:
+
+ if (retval == 0 && iostate.io_error == 0 && useVectorUPL && vector_upl_index) {
+ 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);
+ reset_vector_run_state();
+ }
+ /*
+ * make sure all async writes issued as part of this stream
+ * have completed before we return
+ */
+ cluster_iostate_wait(&iostate, 0, "cluster_write_direct");
+
+ if (iostate.io_error) {
+ retval = iostate.io_error;
+ }
+
+ lck_mtx_destroy(&iostate.io_mtxp, &cl_mtx_grp);
+
+ if (io_throttled == TRUE && retval == 0) {
+ retval = EAGAIN;
+ }
+
+ if (io_req_size && retval == 0) {
+ /*
+ * we couldn't handle the tail of this request in DIRECT mode
+ * so fire it through the copy path
+ *
+ * note that flags will never have IO_HEADZEROFILL or IO_TAILZEROFILL set
+ * so we can just pass 0 in for the headOff and tailOff
+ */
+ if (uio->uio_offset > oldEOF) {
+ oldEOF = uio->uio_offset;
+ }
+
+ retval = cluster_write_copy(vp, uio, io_req_size, oldEOF, newEOF, (off_t)0, (off_t)0, flags, callback, callback_arg);
+
+ *write_type = IO_UNKNOWN;
+ }
+ KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 75)) | DBG_FUNC_END,
+ (int)uio->uio_offset, io_req_size, retval, 4, 0);
+
+ return retval;
+}
+
+
+static int
+cluster_write_contig(vnode_t vp, struct uio *uio, off_t newEOF, int *write_type, u_int32_t *write_length,
+ int (*callback)(buf_t, void *), void *callback_arg, int bflag)
+{
+ upl_page_info_t *pl;
+ addr64_t src_paddr = 0;
+ upl_t upl[MAX_VECTS];
+ vm_offset_t upl_offset;
+ u_int32_t tail_size = 0;
+ u_int32_t io_size;
+ u_int32_t xsize;
+ upl_size_t upl_size;
+ vm_size_t upl_needed_size;
+ mach_msg_type_number_t pages_in_pl;
+ upl_control_flags_t upl_flags;
+ kern_return_t kret;
+ struct clios iostate;
+ int error = 0;
+ int cur_upl = 0;
+ int num_upl = 0;
+ int n;
+ user_addr_t iov_base;
+ u_int32_t devblocksize;
+ u_int32_t mem_alignment_mask;
+
+ /*
+ * When we enter this routine, we know
+ * -- the io_req_size will not exceed iov_len
+ * -- the target address is physically contiguous
+ */
+ cluster_syncup(vp, newEOF, callback, callback_arg, callback ? PUSH_SYNC : 0);
+
+ devblocksize = (u_int32_t)vp->v_mount->mnt_devblocksize;
+ mem_alignment_mask = (u_int32_t)vp->v_mount->mnt_alignmentmask;
+
+ iostate.io_completed = 0;
+ iostate.io_issued = 0;
+ iostate.io_error = 0;
+ iostate.io_wanted = 0;
+
+ lck_mtx_init(&iostate.io_mtxp, &cl_mtx_grp, LCK_ATTR_NULL);
+
+next_cwrite:
+ io_size = *write_length;
+
+ iov_base = uio_curriovbase(uio);
+
+ upl_offset = (vm_offset_t)((u_int32_t)iov_base & PAGE_MASK);
+ upl_needed_size = upl_offset + io_size;
+
+ pages_in_pl = 0;
+ upl_size = (upl_size_t)upl_needed_size;
+ upl_flags = UPL_FILE_IO | UPL_COPYOUT_FROM | UPL_NO_SYNC |
+ UPL_CLEAN_IN_PLACE | UPL_SET_INTERNAL | UPL_SET_LITE | UPL_SET_IO_WIRE;
+
+ vm_map_t map = UIO_SEG_IS_USER_SPACE(uio->uio_segflg) ? current_map() : kernel_map;
+ kret = vm_map_get_upl(map,
+ vm_map_trunc_page(iov_base, vm_map_page_mask(map)),
+ &upl_size, &upl[cur_upl], NULL, &pages_in_pl, &upl_flags, VM_KERN_MEMORY_FILE, 0);
+
+ if (kret != KERN_SUCCESS) {
+ /*
+ * failed to get pagelist
+ */
+ error = EINVAL;
+ goto wait_for_cwrites;
+ }
+ num_upl++;
+
+ /*
+ * Consider the possibility that upl_size wasn't satisfied.
+ */
+ if (upl_size < upl_needed_size) {
+ /*
+ * This is a failure in the physical memory case.
+ */
+ error = EINVAL;
+ goto wait_for_cwrites;
+ }
+ pl = ubc_upl_pageinfo(upl[cur_upl]);
+
+ src_paddr = ((addr64_t)upl_phys_page(pl, 0) << PAGE_SHIFT) + (addr64_t)upl_offset;
+
+ while (((uio->uio_offset & (devblocksize - 1)) || io_size < devblocksize) && io_size) {
+ u_int32_t head_size;
+
+ head_size = devblocksize - (u_int32_t)(uio->uio_offset & (devblocksize - 1));
+
+ if (head_size > io_size) {
+ head_size = io_size;
+ }
+
+ error = cluster_align_phys_io(vp, uio, src_paddr, head_size, 0, callback, callback_arg);
+
+ if (error) {
+ goto wait_for_cwrites;
+ }
+
+ upl_offset += head_size;
+ src_paddr += head_size;
+ io_size -= head_size;
+
+ iov_base += head_size;
+ }
+ if ((u_int32_t)iov_base & mem_alignment_mask) {
+ /*
+ * request doesn't set up on a memory boundary
+ * the underlying DMA engine can handle...
+ * return an error instead of going through
+ * the slow copy path since the intent of this
+ * path is direct I/O from device memory
+ */
+ error = EINVAL;
+ goto wait_for_cwrites;
+ }
+
+ tail_size = io_size & (devblocksize - 1);
+ io_size -= tail_size;
+
+ while (io_size && error == 0) {
+ if (io_size > MAX_IO_CONTIG_SIZE) {
+ xsize = MAX_IO_CONTIG_SIZE;
+ } else {
+ xsize = io_size;
+ }
+ /*
+ * request asynchronously so that we can overlap
+ * the preparation of the next I/O... we'll do
+ * the commit after all the I/O has completed
+ * since its all issued against the same UPL
+ * if there are already too many outstanding writes
+ * wait until some have completed before issuing the next
+ */
+ cluster_iostate_wait(&iostate, MAX_IO_CONTIG_SIZE * IO_SCALE(vp, 2), "cluster_write_contig");
+
+ if (iostate.io_error) {
+ /*
+ * one of the earlier writes we issued ran into a hard error
+ * don't issue any more writes...
+ * go wait for all writes that are part of this stream
+ * to complete before returning the error to the caller
+ */
+ goto wait_for_cwrites;
+ }
+ /*
+ * issue an asynchronous write to cluster_io
+ */
+ error = cluster_io(vp, upl[cur_upl], upl_offset, uio->uio_offset,
+ xsize, CL_DEV_MEMORY | CL_ASYNC | bflag, (buf_t)NULL, (struct clios *)&iostate, callback, callback_arg);
+
+ if (error == 0) {
+ /*
+ * The cluster_io write completed successfully,
+ * update the uio structure
+ */
+ uio_update(uio, (user_size_t)xsize);
+
+ upl_offset += xsize;
+ src_paddr += xsize;
+ io_size -= xsize;
+ }
+ }
+ if (error == 0 && iostate.io_error == 0 && tail_size == 0 && num_upl < MAX_VECTS) {
+ error = cluster_io_type(uio, write_type, write_length, 0);
+
+ if (error == 0 && *write_type == IO_CONTIG) {
+ cur_upl++;
+ goto next_cwrite;
+ }
+ } else {
+ *write_type = IO_UNKNOWN;
+ }
+
+wait_for_cwrites:
+ /*
+ * make sure all async writes that are part of this stream
+ * have completed before we proceed
+ */
+ cluster_iostate_wait(&iostate, 0, "cluster_write_contig");
+
+ if (iostate.io_error) {
+ error = iostate.io_error;
+ }
+
+ lck_mtx_destroy(&iostate.io_mtxp, &cl_mtx_grp);
+
+ if (error == 0 && tail_size) {
+ error = cluster_align_phys_io(vp, uio, src_paddr, tail_size, 0, callback, callback_arg);
+ }
+
+ for (n = 0; n < num_upl; n++) {
+ /*
+ * just release our hold on each physically contiguous
+ * region without changing any state
+ */
+ ubc_upl_abort(upl[n], 0);
+ }
+
+ return error;
+}
+
+
+/*
+ * need to avoid a race between an msync of a range of pages dirtied via mmap
+ * vs a filesystem such as HFS deciding to write a 'hole' to disk via cluster_write's
+ * zerofill mechanism before it has seen the VNOP_PAGEOUTs for the pages being msync'd
+ *
+ * we should never force-zero-fill pages that are already valid in the cache...
+ * the entire page contains valid data (either from disk, zero-filled or dirtied
+ * via an mmap) so we can only do damage by trying to zero-fill
+ *
+ */
+static int
+cluster_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)
+{
+ int zero_pg_index;
+ boolean_t need_cluster_zero = TRUE;
+
+ if ((flags & (IO_NOZEROVALID | IO_NOZERODIRTY))) {
+ bytes_to_zero = min(bytes_to_zero, PAGE_SIZE - (int)(zero_off & PAGE_MASK_64));
+ zero_pg_index = (int)((zero_off - upl_f_offset) / PAGE_SIZE_64);