+ retval = uiomove64(paddr, csize, uio);
+
+ pg_index += 1;
+ pg_offset = 0;
+ xsize -= csize;
+ csize = min(PAGE_SIZE, xsize);
+ }
+ uio->uio_segflg = segflg;
+
+ if (funnel_state == TRUE)
+ thread_funnel_set(kernel_flock, TRUE);
+
+ KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 34)) | DBG_FUNC_END,
+ (int)uio->uio_offset, uio->uio_resid, retval, segflg, 0);
+
+ return (retval);
+}
+
+
+int
+cluster_copy_ubc_data(struct vnode *vp, struct uio *uio, int *io_resid, int mark_dirty)
+{
+ int segflg;
+ int io_size;
+ int xsize;
+ int start_offset;
+ off_t f_offset;
+ int retval = 0;
+ memory_object_control_t control;
+ int op_flags = UPL_POP_SET | UPL_POP_BUSY;
+ boolean_t funnel_state = FALSE;
+
+
+ KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 34)) | DBG_FUNC_START,
+ (int)uio->uio_offset, uio->uio_resid, 0, *io_resid, 0);
+
+ control = ubc_getobject(vp, UBC_FLAGS_NONE);
+ if (control == MEMORY_OBJECT_CONTROL_NULL) {
+ KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 34)) | DBG_FUNC_END,
+ (int)uio->uio_offset, uio->uio_resid, retval, 3, 0);
+
+ return(0);
+ }
+ if (mark_dirty)
+ op_flags |= UPL_POP_DIRTY;
+
+ segflg = uio->uio_segflg;
+
+ switch(segflg) {
+
+ case UIO_USERSPACE:
+ case UIO_USERISPACE:
+ uio->uio_segflg = UIO_PHYS_USERSPACE;
+ break;
+
+ case UIO_SYSSPACE:
+ uio->uio_segflg = UIO_PHYS_SYSSPACE;
+ break;
+ }
+ io_size = *io_resid;
+ start_offset = (int)(uio->uio_offset & PAGE_MASK_64);
+ f_offset = uio->uio_offset - start_offset;
+ xsize = min(PAGE_SIZE - start_offset, io_size);
+
+ while (io_size && retval == 0) {
+ ppnum_t pgframe;
+
+ if (ubc_page_op_with_control(control, f_offset, op_flags, &pgframe, 0) != KERN_SUCCESS)
+ break;
+
+ if (funnel_state == FALSE && io_size >= (16 * 1024))
+ funnel_state = thread_funnel_set(kernel_flock, FALSE);
+
+ retval = uiomove64((addr64_t)(((addr64_t)pgframe << 12) + start_offset), xsize, uio);
+
+ ubc_page_op_with_control(control, f_offset, UPL_POP_CLR | UPL_POP_BUSY, 0, 0);
+
+ io_size -= xsize;
+ start_offset = 0;
+ f_offset = uio->uio_offset;
+ xsize = min(PAGE_SIZE, io_size);
+ }
+ uio->uio_segflg = segflg;
+ *io_resid = io_size;
+
+ if (funnel_state == TRUE)
+ thread_funnel_set(kernel_flock, TRUE);
+
+ KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 34)) | DBG_FUNC_END,
+ (int)uio->uio_offset, uio->uio_resid, retval, 0x80000000 | segflg, 0);
+
+ return(retval);
+}
+
+
+int
+is_file_clean(struct vnode *vp, off_t filesize)
+{
+ off_t f_offset;
+ int flags;
+ int total_dirty = 0;
+
+ for (f_offset = 0; f_offset < filesize; f_offset += PAGE_SIZE_64) {
+ if (ubc_page_op(vp, f_offset, 0, 0, &flags) == KERN_SUCCESS) {
+ if (flags & UPL_POP_DIRTY) {
+ total_dirty++;
+ }
+ }
+ }
+ if (total_dirty)
+ return(EINVAL);
+
+ return (0);
+}
+
+
+
+/*
+ * Dirty region tracking/clustering mechanism.
+ *
+ * This code (vfs_drt_*) provides a mechanism for tracking and clustering
+ * dirty regions within a larger space (file). It is primarily intended to
+ * support clustering in large files with many dirty areas.
+ *
+ * The implementation assumes that the dirty regions are pages.
+ *
+ * To represent dirty pages within the file, we store bit vectors in a
+ * variable-size circular hash.
+ */
+
+/*
+ * Bitvector size. This determines the number of pages we group in a
+ * single hashtable entry. Each hashtable entry is aligned to this
+ * size within the file.
+ */
+#define DRT_BITVECTOR_PAGES 256
+
+/*
+ * File offset handling.
+ *
+ * DRT_ADDRESS_MASK is dependent on DRT_BITVECTOR_PAGES;
+ * the correct formula is (~(DRT_BITVECTOR_PAGES * PAGE_SIZE) - 1)
+ */
+#define DRT_ADDRESS_MASK (~((1 << 20) - 1))
+#define DRT_ALIGN_ADDRESS(addr) ((addr) & DRT_ADDRESS_MASK)
+
+/*
+ * Hashtable address field handling.
+ *
+ * The low-order bits of the hashtable address are used to conserve
+ * space.
+ *
+ * DRT_HASH_COUNT_MASK must be large enough to store the range
+ * 0-DRT_BITVECTOR_PAGES inclusive, as well as have one value
+ * to indicate that the bucket is actually unoccupied.
+ */
+#define DRT_HASH_GET_ADDRESS(scm, i) ((scm)->scm_hashtable[(i)].dhe_control & DRT_ADDRESS_MASK)
+#define DRT_HASH_SET_ADDRESS(scm, i, a) \
+ do { \
+ (scm)->scm_hashtable[(i)].dhe_control = \
+ ((scm)->scm_hashtable[(i)].dhe_control & ~DRT_ADDRESS_MASK) | DRT_ALIGN_ADDRESS(a); \
+ } while (0)
+#define DRT_HASH_COUNT_MASK 0x1ff
+#define DRT_HASH_GET_COUNT(scm, i) ((scm)->scm_hashtable[(i)].dhe_control & DRT_HASH_COUNT_MASK)
+#define DRT_HASH_SET_COUNT(scm, i, c) \
+ do { \
+ (scm)->scm_hashtable[(i)].dhe_control = \
+ ((scm)->scm_hashtable[(i)].dhe_control & ~DRT_HASH_COUNT_MASK) | ((c) & DRT_HASH_COUNT_MASK); \
+ } while (0)
+#define DRT_HASH_CLEAR(scm, i) \
+ do { \
+ (scm)->scm_hashtable[(i)].dhe_control = 0; \
+ } while (0)
+#define DRT_HASH_VACATE(scm, i) DRT_HASH_SET_COUNT((scm), (i), DRT_HASH_COUNT_MASK)
+#define DRT_HASH_VACANT(scm, i) (DRT_HASH_GET_COUNT((scm), (i)) == DRT_HASH_COUNT_MASK)
+#define DRT_HASH_COPY(oscm, oi, scm, i) \
+ do { \
+ (scm)->scm_hashtable[(i)].dhe_control = (oscm)->scm_hashtable[(oi)].dhe_control; \
+ DRT_BITVECTOR_COPY(oscm, oi, scm, i); \
+ } while(0);
+
+
+/*
+ * Hash table moduli.
+ *
+ * Since the hashtable entry's size is dependent on the size of
+ * the bitvector, and since the hashtable size is constrained to
+ * both being prime and fitting within the desired allocation
+ * size, these values need to be manually determined.
+ *
+ * For DRT_BITVECTOR_SIZE = 256, the entry size is 40 bytes.
+ *
+ * The small hashtable allocation is 1024 bytes, so the modulus is 23.
+ * The large hashtable allocation is 16384 bytes, so the modulus is 401.
+ */
+#define DRT_HASH_SMALL_MODULUS 23
+#define DRT_HASH_LARGE_MODULUS 401
+
+#define DRT_SMALL_ALLOCATION 1024 /* 104 bytes spare */
+#define DRT_LARGE_ALLOCATION 16384 /* 344 bytes spare */
+
+/* *** nothing below here has secret dependencies on DRT_BITVECTOR_PAGES *** */
+
+/*
+ * Hashtable bitvector handling.
+ *
+ * Bitvector fields are 32 bits long.
+ */
+
+#define DRT_HASH_SET_BIT(scm, i, bit) \
+ (scm)->scm_hashtable[(i)].dhe_bitvector[(bit) / 32] |= (1 << ((bit) % 32))
+
+#define DRT_HASH_CLEAR_BIT(scm, i, bit) \
+ (scm)->scm_hashtable[(i)].dhe_bitvector[(bit) / 32] &= ~(1 << ((bit) % 32))
+
+#define DRT_HASH_TEST_BIT(scm, i, bit) \
+ ((scm)->scm_hashtable[(i)].dhe_bitvector[(bit) / 32] & (1 << ((bit) % 32)))
+
+#define DRT_BITVECTOR_CLEAR(scm, i) \
+ bzero(&(scm)->scm_hashtable[(i)].dhe_bitvector[0], (DRT_BITVECTOR_PAGES / 32) * sizeof(u_int32_t))
+
+#define DRT_BITVECTOR_COPY(oscm, oi, scm, i) \
+ bcopy(&(oscm)->scm_hashtable[(oi)].dhe_bitvector[0], \
+ &(scm)->scm_hashtable[(i)].dhe_bitvector[0], \
+ (DRT_BITVECTOR_PAGES / 32) * sizeof(u_int32_t))
+
+
+
+/*
+ * Hashtable entry.
+ */
+struct vfs_drt_hashentry {
+ u_int64_t dhe_control;
+ u_int32_t dhe_bitvector[DRT_BITVECTOR_PAGES / 32];
+};
+
+/*
+ * Dirty Region Tracking structure.
+ *
+ * The hashtable is allocated entirely inside the DRT structure.
+ *
+ * The hash is a simple circular prime modulus arrangement, the structure
+ * is resized from small to large if it overflows.
+ */
+
+struct vfs_drt_clustermap {
+ u_int32_t scm_magic; /* sanity/detection */
+#define DRT_SCM_MAGIC 0x12020003
+ u_int32_t scm_modulus; /* current ring size */
+ u_int32_t scm_buckets; /* number of occupied buckets */
+ u_int32_t scm_lastclean; /* last entry we cleaned */
+ u_int32_t scm_iskips; /* number of slot skips */
+
+ struct vfs_drt_hashentry scm_hashtable[0];
+};
+
+
+#define DRT_HASH(scm, addr) ((addr) % (scm)->scm_modulus)
+#define DRT_HASH_NEXT(scm, addr) (((addr) + 1) % (scm)->scm_modulus)
+
+/*
+ * Debugging codes and arguments.
+ */
+#define DRT_DEBUG_EMPTYFREE (FSDBG_CODE(DBG_FSRW, 82)) /* nil */
+#define DRT_DEBUG_RETCLUSTER (FSDBG_CODE(DBG_FSRW, 83)) /* offset, length */
+#define DRT_DEBUG_ALLOC (FSDBG_CODE(DBG_FSRW, 84)) /* copycount */
+#define DRT_DEBUG_INSERT (FSDBG_CODE(DBG_FSRW, 85)) /* offset, iskip */
+#define DRT_DEBUG_MARK (FSDBG_CODE(DBG_FSRW, 86)) /* offset, length,
+ * dirty */
+ /* 0, setcount */
+ /* 1 (clean, no map) */
+ /* 2 (map alloc fail) */
+ /* 3, resid (partial) */
+#define DRT_DEBUG_6 (FSDBG_CODE(DBG_FSRW, 87))
+#define DRT_DEBUG_SCMDATA (FSDBG_CODE(DBG_FSRW, 88)) /* modulus, buckets,
+ * lastclean, iskips */
+
+
+static void vfs_drt_sanity(struct vfs_drt_clustermap *cmap);
+static kern_return_t vfs_drt_alloc_map(struct vfs_drt_clustermap **cmapp);
+static kern_return_t vfs_drt_free_map(struct vfs_drt_clustermap *cmap);
+static kern_return_t vfs_drt_search_index(struct vfs_drt_clustermap *cmap,
+ u_int64_t offset, int *indexp);
+static kern_return_t vfs_drt_get_index(struct vfs_drt_clustermap **cmapp,
+ u_int64_t offset,
+ int *indexp,
+ int recursed);
+static kern_return_t vfs_drt_do_mark_pages(
+ void **cmapp,
+ u_int64_t offset,
+ u_int length,
+ int *setcountp,
+ int dirty);
+static void vfs_drt_trace(
+ struct vfs_drt_clustermap *cmap,
+ int code,
+ int arg1,
+ int arg2,
+ int arg3,
+ int arg4);
+
+
+/*
+ * Allocate and initialise a sparse cluster map.
+ *
+ * Will allocate a new map, resize or compact an existing map.
+ *
+ * XXX we should probably have at least one intermediate map size,
+ * as the 1:16 ratio seems a bit drastic.
+ */
+static kern_return_t
+vfs_drt_alloc_map(struct vfs_drt_clustermap **cmapp)
+{
+ struct vfs_drt_clustermap *cmap, *ocmap;
+ kern_return_t kret;
+ u_int64_t offset;
+ int nsize, i, active_buckets, index, copycount;
+
+ ocmap = NULL;
+ if (cmapp != NULL)
+ ocmap = *cmapp;
+
+ /*
+ * Decide on the size of the new map.
+ */
+ if (ocmap == NULL) {
+ nsize = DRT_HASH_SMALL_MODULUS;
+ } else {
+ /* count the number of active buckets in the old map */
+ active_buckets = 0;
+ for (i = 0; i < ocmap->scm_modulus; i++) {
+ if (!DRT_HASH_VACANT(ocmap, i) &&
+ (DRT_HASH_GET_COUNT(ocmap, i) != 0))
+ active_buckets++;
+ }
+ /*
+ * If we're currently using the small allocation, check to
+ * see whether we should grow to the large one.
+ */
+ if (ocmap->scm_modulus == DRT_HASH_SMALL_MODULUS) {
+ /* if the ring is nearly full */
+ if (active_buckets > (DRT_HASH_SMALL_MODULUS - 5)) {
+ nsize = DRT_HASH_LARGE_MODULUS;
+ } else {
+ nsize = DRT_HASH_SMALL_MODULUS;
+ }
+ } else {
+ /* already using the large modulus */
+ nsize = DRT_HASH_LARGE_MODULUS;
+ /*
+ * If the ring is completely full, there's
+ * nothing useful for us to do. Behave as
+ * though we had compacted into the new
+ * array and return.
+ */
+ if (active_buckets >= DRT_HASH_LARGE_MODULUS)
+ return(KERN_SUCCESS);
+ }
+ }
+
+ /*
+ * Allocate and initialise the new map.
+ */
+
+ kret = kmem_alloc(kernel_map, (vm_offset_t *)&cmap,
+ (nsize == DRT_HASH_SMALL_MODULUS) ? DRT_SMALL_ALLOCATION : DRT_LARGE_ALLOCATION);
+ if (kret != KERN_SUCCESS)
+ return(kret);
+ cmap->scm_magic = DRT_SCM_MAGIC;
+ cmap->scm_modulus = nsize;
+ cmap->scm_buckets = 0;
+ cmap->scm_lastclean = 0;
+ cmap->scm_iskips = 0;
+ for (i = 0; i < cmap->scm_modulus; i++) {
+ DRT_HASH_CLEAR(cmap, i);
+ DRT_HASH_VACATE(cmap, i);
+ DRT_BITVECTOR_CLEAR(cmap, i);
+ }
+
+ /*
+ * If there's an old map, re-hash entries from it into the new map.
+ */
+ copycount = 0;
+ if (ocmap != NULL) {
+ for (i = 0; i < ocmap->scm_modulus; i++) {
+ /* skip empty buckets */
+ if (DRT_HASH_VACANT(ocmap, i) ||
+ (DRT_HASH_GET_COUNT(ocmap, i) == 0))
+ continue;
+ /* get new index */
+ offset = DRT_HASH_GET_ADDRESS(ocmap, i);
+ kret = vfs_drt_get_index(&cmap, offset, &index, 1);
+ if (kret != KERN_SUCCESS) {
+ /* XXX need to bail out gracefully here */
+ panic("vfs_drt: new cluster map mysteriously too small");
+ }
+ /* copy */
+ DRT_HASH_COPY(ocmap, i, cmap, index);
+ copycount++;
+ }
+ }
+
+ /* log what we've done */
+ vfs_drt_trace(cmap, DRT_DEBUG_ALLOC, copycount, 0, 0, 0);
+
+ /*
+ * It's important to ensure that *cmapp always points to
+ * a valid map, so we must overwrite it before freeing
+ * the old map.
+ */
+ *cmapp = cmap;
+ if (ocmap != NULL) {
+ /* emit stats into trace buffer */
+ vfs_drt_trace(ocmap, DRT_DEBUG_SCMDATA,
+ ocmap->scm_modulus,
+ ocmap->scm_buckets,
+ ocmap->scm_lastclean,
+ ocmap->scm_iskips);
+
+ vfs_drt_free_map(ocmap);
+ }
+ return(KERN_SUCCESS);
+}
+
+
+/*
+ * Free a sparse cluster map.
+ */
+static kern_return_t
+vfs_drt_free_map(struct vfs_drt_clustermap *cmap)
+{
+ kern_return_t ret;
+
+ kmem_free(kernel_map, (vm_offset_t)cmap,
+ (cmap->scm_modulus == DRT_HASH_SMALL_MODULUS) ? DRT_SMALL_ALLOCATION : DRT_LARGE_ALLOCATION);
+ return(KERN_SUCCESS);
+}
+
+
+/*
+ * Find the hashtable slot currently occupied by an entry for the supplied offset.
+ */
+static kern_return_t
+vfs_drt_search_index(struct vfs_drt_clustermap *cmap, u_int64_t offset, int *indexp)
+{
+ kern_return_t kret;
+ int index, i, tries;
+
+ offset = DRT_ALIGN_ADDRESS(offset);
+ index = DRT_HASH(cmap, offset);
+
+ /* traverse the hashtable */
+ for (i = 0; i < cmap->scm_modulus; i++) {
+
+ /*
+ * If the slot is vacant, we can stop.
+ */
+ if (DRT_HASH_VACANT(cmap, index))
+ break;
+
+ /*
+ * If the address matches our offset, we have success.
+ */
+ if (DRT_HASH_GET_ADDRESS(cmap, index) == offset) {
+ *indexp = index;
+ return(KERN_SUCCESS);
+ }
+
+ /*
+ * Move to the next slot, try again.
+ */
+ index = DRT_HASH_NEXT(cmap, index);
+ }
+ /*
+ * It's not there.
+ */
+ return(KERN_FAILURE);
+}
+
+/*
+ * Find the hashtable slot for the supplied offset. If we haven't allocated
+ * one yet, allocate one and populate the address field. Note that it will
+ * not have a nonzero page count and thus will still technically be free, so
+ * in the case where we are called to clean pages, the slot will remain free.
+ */
+static kern_return_t
+vfs_drt_get_index(struct vfs_drt_clustermap **cmapp, u_int64_t offset, int *indexp, int recursed)
+{
+ struct vfs_drt_clustermap *cmap;
+ kern_return_t kret;
+ int index, i;
+
+ cmap = *cmapp;
+
+ /* look for an existing entry */
+ kret = vfs_drt_search_index(cmap, offset, indexp);
+ if (kret == KERN_SUCCESS)
+ return(kret);
+
+ /* need to allocate an entry */
+ offset = DRT_ALIGN_ADDRESS(offset);
+ index = DRT_HASH(cmap, offset);
+
+ /* scan from the index forwards looking for a vacant slot */
+ for (i = 0; i < cmap->scm_modulus; i++) {
+ /* slot vacant? */
+ if (DRT_HASH_VACANT(cmap, index) || DRT_HASH_GET_COUNT(cmap,index) == 0) {
+ cmap->scm_buckets++;
+ if (index < cmap->scm_lastclean)
+ cmap->scm_lastclean = index;
+ DRT_HASH_SET_ADDRESS(cmap, index, offset);
+ DRT_HASH_SET_COUNT(cmap, index, 0);
+ DRT_BITVECTOR_CLEAR(cmap, index);
+ *indexp = index;
+ vfs_drt_trace(cmap, DRT_DEBUG_INSERT, (int)offset, i, 0, 0);
+ return(KERN_SUCCESS);
+ }
+ cmap->scm_iskips += i;
+ index = DRT_HASH_NEXT(cmap, index);
+ }
+
+ /*
+ * We haven't found a vacant slot, so the map is full. If we're not
+ * already recursed, try reallocating/compacting it.
+ */
+ if (recursed)
+ return(KERN_FAILURE);
+ kret = vfs_drt_alloc_map(cmapp);
+ if (kret == KERN_SUCCESS) {
+ /* now try to insert again */
+ kret = vfs_drt_get_index(cmapp, offset, indexp, 1);
+ }
+ return(kret);
+}
+
+/*
+ * Implementation of set dirty/clean.
+ *
+ * In the 'clean' case, not finding a map is OK.
+ */
+static kern_return_t
+vfs_drt_do_mark_pages(
+ void **private,
+ u_int64_t offset,
+ u_int length,
+ int *setcountp,
+ int dirty)
+{
+ struct vfs_drt_clustermap *cmap, **cmapp;
+ kern_return_t kret;
+ int i, index, pgoff, pgcount, setcount, ecount;
+
+ cmapp = (struct vfs_drt_clustermap **)private;
+ cmap = *cmapp;
+
+ vfs_drt_trace(cmap, DRT_DEBUG_MARK | DBG_FUNC_START, (int)offset, (int)length, dirty, 0);
+
+ if (setcountp != NULL)
+ *setcountp = 0;
+
+ /* allocate a cluster map if we don't already have one */
+ if (cmap == NULL) {
+ /* no cluster map, nothing to clean */
+ if (!dirty) {
+ vfs_drt_trace(cmap, DRT_DEBUG_MARK | DBG_FUNC_END, 1, 0, 0, 0);
+ return(KERN_SUCCESS);
+ }
+ kret = vfs_drt_alloc_map(cmapp);
+ if (kret != KERN_SUCCESS) {
+ vfs_drt_trace(cmap, DRT_DEBUG_MARK | DBG_FUNC_END, 2, 0, 0, 0);
+ return(kret);
+ }
+ }
+ setcount = 0;
+
+ /*
+ * Iterate over the length of the region.
+ */
+ while (length > 0) {
+ /*
+ * Get the hashtable index for this offset.
+ *
+ * XXX this will add blank entries if we are clearing a range
+ * that hasn't been dirtied.
+ */
+ kret = vfs_drt_get_index(cmapp, offset, &index, 0);
+ cmap = *cmapp; /* may have changed! */
+ /* this may be a partial-success return */
+ if (kret != KERN_SUCCESS) {
+ if (setcountp != NULL)
+ *setcountp = setcount;
+ vfs_drt_trace(cmap, DRT_DEBUG_MARK | DBG_FUNC_END, 3, (int)length, 0, 0);
+
+ return(kret);
+ }
+
+ /*
+ * Work out how many pages we're modifying in this
+ * hashtable entry.
+ */
+ pgoff = (offset - DRT_ALIGN_ADDRESS(offset)) / PAGE_SIZE;
+ pgcount = min((length / PAGE_SIZE), (DRT_BITVECTOR_PAGES - pgoff));
+
+ /*
+ * Iterate over pages, dirty/clearing as we go.
+ */
+ ecount = DRT_HASH_GET_COUNT(cmap, index);
+ for (i = 0; i < pgcount; i++) {
+ if (dirty) {
+ if (!DRT_HASH_TEST_BIT(cmap, index, pgoff + i)) {
+ DRT_HASH_SET_BIT(cmap, index, pgoff + i);
+ ecount++;
+ setcount++;
+ }
+ } else {
+ if (DRT_HASH_TEST_BIT(cmap, index, pgoff + i)) {
+ DRT_HASH_CLEAR_BIT(cmap, index, pgoff + i);
+ ecount--;
+ setcount++;
+ }
+ }
+ }
+ DRT_HASH_SET_COUNT(cmap, index, ecount);
+next:
+ offset += pgcount * PAGE_SIZE;
+ length -= pgcount * PAGE_SIZE;
+ }
+ if (setcountp != NULL)
+ *setcountp = setcount;
+
+ vfs_drt_trace(cmap, DRT_DEBUG_MARK | DBG_FUNC_END, 0, setcount, 0, 0);
+
+ return(KERN_SUCCESS);
+}
+
+/*
+ * Mark a set of pages as dirty/clean.
+ *
+ * This is a public interface.
+ *
+ * cmapp
+ * Pointer to storage suitable for holding a pointer. Note that
+ * this must either be NULL or a value set by this function.
+ *
+ * size
+ * Current file size in bytes.
+ *
+ * offset
+ * Offset of the first page to be marked as dirty, in bytes. Must be
+ * page-aligned.
+ *
+ * length
+ * Length of dirty region, in bytes. Must be a multiple of PAGE_SIZE.
+ *
+ * setcountp
+ * Number of pages newly marked dirty by this call (optional).
+ *
+ * Returns KERN_SUCCESS if all the pages were successfully marked.
+ */
+static kern_return_t
+vfs_drt_mark_pages(void **cmapp, off_t offset, u_int length, int *setcountp)
+{
+ /* XXX size unused, drop from interface */
+ return(vfs_drt_do_mark_pages(cmapp, offset, length, setcountp, 1));
+}
+
+static kern_return_t
+vfs_drt_unmark_pages(void **cmapp, off_t offset, u_int length)
+{
+ return(vfs_drt_do_mark_pages(cmapp, offset, length, NULL, 0));
+}
+
+/*
+ * Get a cluster of dirty pages.
+ *
+ * This is a public interface.
+ *
+ * cmapp
+ * Pointer to storage managed by drt_mark_pages. Note that this must
+ * be NULL or a value set by drt_mark_pages.
+ *
+ * offsetp
+ * Returns the byte offset into the file of the first page in the cluster.
+ *
+ * lengthp
+ * Returns the length in bytes of the cluster of dirty pages.
+ *
+ * Returns success if a cluster was found. If KERN_FAILURE is returned, there
+ * are no dirty pages meeting the minmum size criteria. Private storage will
+ * be released if there are no more dirty pages left in the map
+ *
+ */
+static kern_return_t
+vfs_drt_get_cluster(void **cmapp, off_t *offsetp, u_int *lengthp)
+{
+ struct vfs_drt_clustermap *cmap;
+ u_int64_t offset;
+ u_int length;
+ int index, i, j, fs, ls;
+
+ /* sanity */
+ if ((cmapp == NULL) || (*cmapp == NULL))
+ return(KERN_FAILURE);
+ cmap = *cmapp;
+
+ /* walk the hashtable */
+ for (offset = 0, j = 0; j < cmap->scm_modulus; offset += (DRT_BITVECTOR_PAGES * PAGE_SIZE), j++) {
+ index = DRT_HASH(cmap, offset);
+
+ if (DRT_HASH_VACANT(cmap, index) || (DRT_HASH_GET_COUNT(cmap, index) == 0))
+ continue;
+
+ /* scan the bitfield for a string of bits */
+ fs = -1;
+
+ for (i = 0; i < DRT_BITVECTOR_PAGES; i++) {
+ if (DRT_HASH_TEST_BIT(cmap, index, i)) {
+ fs = i;
+ break;
+ }
+ }
+ if (fs == -1) {
+ /* didn't find any bits set */
+ panic("vfs_drt: entry summary count > 0 but no bits set in map");
+ }
+ for (ls = 0; i < DRT_BITVECTOR_PAGES; i++, ls++) {
+ if (!DRT_HASH_TEST_BIT(cmap, index, i))
+ break;
+ }
+
+ /* compute offset and length, mark pages clean */
+ offset = DRT_HASH_GET_ADDRESS(cmap, index) + (PAGE_SIZE * fs);
+ length = ls * PAGE_SIZE;
+ vfs_drt_do_mark_pages(cmapp, offset, length, NULL, 0);
+ cmap->scm_lastclean = index;
+
+ /* return successful */
+ *offsetp = (off_t)offset;
+ *lengthp = length;
+
+ vfs_drt_trace(cmap, DRT_DEBUG_RETCLUSTER, (int)offset, (int)length, 0, 0);
+ return(KERN_SUCCESS);
+ }
+ /*
+ * We didn't find anything... hashtable is empty
+ * emit stats into trace buffer and
+ * then free it
+ */
+ vfs_drt_trace(cmap, DRT_DEBUG_SCMDATA,
+ cmap->scm_modulus,
+ cmap->scm_buckets,
+ cmap->scm_lastclean,
+ cmap->scm_iskips);
+
+ vfs_drt_free_map(cmap);
+ *cmapp = NULL;
+
+ return(KERN_FAILURE);
+}
+
+
+static kern_return_t
+vfs_drt_control(void **cmapp, int op_type)
+{
+ struct vfs_drt_clustermap *cmap;
+
+ /* sanity */
+ if ((cmapp == NULL) || (*cmapp == NULL))
+ return(KERN_FAILURE);
+ cmap = *cmapp;
+
+ switch (op_type) {
+ case 0:
+ /* emit stats into trace buffer */
+ vfs_drt_trace(cmap, DRT_DEBUG_SCMDATA,
+ cmap->scm_modulus,
+ cmap->scm_buckets,
+ cmap->scm_lastclean,
+ cmap->scm_iskips);
+
+ vfs_drt_free_map(cmap);
+ *cmapp = NULL;
+ break;
+
+ case 1:
+ cmap->scm_lastclean = 0;
+ break;
+ }
+ return(KERN_SUCCESS);
+}
+
+
+
+/*
+ * Emit a summary of the state of the clustermap into the trace buffer
+ * along with some caller-provided data.
+ */
+static void
+vfs_drt_trace(struct vfs_drt_clustermap *cmap, int code, int arg1, int arg2, int arg3, int arg4)
+{
+ KERNEL_DEBUG(code, arg1, arg2, arg3, arg4, 0);
+}
+
+/*
+ * Perform basic sanity check on the hash entry summary count
+ * vs. the actual bits set in the entry.
+ */
+static void
+vfs_drt_sanity(struct vfs_drt_clustermap *cmap)
+{
+ int index, i;
+ int bits_on;
+
+ for (index = 0; index < cmap->scm_modulus; index++) {
+ if (DRT_HASH_VACANT(cmap, index))
+ continue;
+
+ for (bits_on = 0, i = 0; i < DRT_BITVECTOR_PAGES; i++) {
+ if (DRT_HASH_TEST_BIT(cmap, index, i))
+ bits_on++;
+ }
+ if (bits_on != DRT_HASH_GET_COUNT(cmap, index))
+ panic("bits_on = %d, index = %d\n", bits_on, index);
+ }