+ KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 95)) | DBG_FUNC_END,
+ iostate->io_issued, iostate->io_completed, target, 0, 0);
+ }
+ lck_mtx_unlock(&iostate->io_mtxp);
+}
+
+static void cluster_handle_associated_upl(struct clios *iostate, upl_t upl,
+ upl_offset_t upl_offset, upl_size_t size)
+{
+ if (!size)
+ return;
+
+ upl_t associated_upl = upl_associated_upl(upl);
+
+ if (!associated_upl)
+ return;
+
+#if 0
+ printf("1: %d %d\n", upl_offset, upl_offset + size);
+#endif
+
+ /*
+ * The associated UPL is page aligned to file offsets whereas the
+ * UPL it's attached to has different alignment requirements. The
+ * upl_offset that we have refers to @upl. The code that follows
+ * has to deal with the first and last pages in this transaction
+ * which might straddle pages in the associated UPL. To keep
+ * track of these pages, we use the mark bits: if the mark bit is
+ * set, we know another transaction has completed its part of that
+ * page and so we can unlock that page here.
+ *
+ * The following illustrates what we have to deal with:
+ *
+ * MEM u <------------ 1 PAGE ------------> e
+ * +-------------+----------------------+-----------------
+ * | |######################|#################
+ * +-------------+----------------------+-----------------
+ * FILE | <--- a ---> o <------------ 1 PAGE ------------>
+ *
+ * So here we show a write to offset @o. The data that is to be
+ * written is in a buffer that is not page aligned; it has offset
+ * @a in the page. The upl that carries the data starts in memory
+ * at @u. The associated upl starts in the file at offset @o. A
+ * transaction will always end on a page boundary (like @e above)
+ * except for the very last transaction in the group. We cannot
+ * unlock the page at @o in the associated upl until both the
+ * transaction ending at @e and the following transaction (that
+ * starts at @e) has completed.
+ */
+
+ /*
+ * We record whether or not the two UPLs are aligned as the mark
+ * bit in the first page of @upl.
+ */
+ upl_page_info_t *pl = UPL_GET_INTERNAL_PAGE_LIST(upl);
+ bool is_unaligned = upl_page_get_mark(pl, 0);
+
+ if (is_unaligned) {
+ upl_page_info_t *assoc_pl = UPL_GET_INTERNAL_PAGE_LIST(associated_upl);
+
+ upl_offset_t upl_end = upl_offset + size;
+ assert(upl_end >= PAGE_SIZE);
+
+ upl_size_t assoc_upl_size = upl_get_size(associated_upl);
+
+ /*
+ * In the very first transaction in the group, upl_offset will
+ * not be page aligned, but after that it will be and in that
+ * case we want the preceding page in the associated UPL hence
+ * the minus one.
+ */
+ assert(upl_offset);
+ if (upl_offset)
+ upl_offset = trunc_page_32(upl_offset - 1);
+
+ lck_mtx_lock_spin(&iostate->io_mtxp);
+
+ // Look at the first page...
+ if (upl_offset
+ && !upl_page_get_mark(assoc_pl, upl_offset >> PAGE_SHIFT)) {
+ /*
+ * The first page isn't marked so let another transaction
+ * completion handle it.
+ */
+ upl_page_set_mark(assoc_pl, upl_offset >> PAGE_SHIFT, true);
+ upl_offset += PAGE_SIZE;