Merge master.kernel.org:/home/rmk/linux-2.6-arm
[pandora-kernel.git] / fs / ntfs / runlist.c
index 396d767..f5b2ac9 100644 (file)
@@ -35,7 +35,7 @@ static inline void ntfs_rl_mm(runlist_element *base, int dst, int src,
                int size)
 {
        if (likely((dst != src) && (size > 0)))
-               memmove(base + dst, base + src, size * sizeof (*base));
+               memmove(base + dst, base + src, size * sizeof(*base));
 }
 
 /**
@@ -94,6 +94,51 @@ static inline runlist_element *ntfs_rl_realloc(runlist_element *rl,
        return new_rl;
 }
 
+/**
+ * ntfs_rl_realloc_nofail - Reallocate memory for runlists
+ * @rl:                original runlist
+ * @old_size:  number of runlist elements in the original runlist @rl
+ * @new_size:  number of runlist elements we need space for
+ *
+ * As the runlists grow, more memory will be required.  To prevent the
+ * kernel having to allocate and reallocate large numbers of small bits of
+ * memory, this function returns an entire page of memory.
+ *
+ * This function guarantees that the allocation will succeed.  It will sleep
+ * for as long as it takes to complete the allocation.
+ *
+ * It is up to the caller to serialize access to the runlist @rl.
+ *
+ * N.B.  If the new allocation doesn't require a different number of pages in
+ *       memory, the function will return the original pointer.
+ *
+ * On success, return a pointer to the newly allocated, or recycled, memory.
+ * On error, return -errno. The following error codes are defined:
+ *     -ENOMEM - Not enough memory to allocate runlist array.
+ *     -EINVAL - Invalid parameters were passed in.
+ */
+static inline runlist_element *ntfs_rl_realloc_nofail(runlist_element *rl,
+               int old_size, int new_size)
+{
+       runlist_element *new_rl;
+
+       old_size = PAGE_ALIGN(old_size * sizeof(*rl));
+       new_size = PAGE_ALIGN(new_size * sizeof(*rl));
+       if (old_size == new_size)
+               return rl;
+
+       new_rl = ntfs_malloc_nofs_nofail(new_size);
+       BUG_ON(!new_rl);
+
+       if (likely(rl != NULL)) {
+               if (unlikely(old_size > new_size))
+                       old_size = new_size;
+               memcpy(new_rl, rl, old_size);
+               ntfs_free(rl);
+       }
+       return new_rl;
+}
+
 /**
  * ntfs_are_rl_mergeable - test if two runlists can be joined together
  * @dst:       original runlist
@@ -497,6 +542,7 @@ runlist_element *ntfs_runlists_merge(runlist_element *drl,
                        /* Scan to the end of the source runlist. */
                        for (dend = 0; likely(drl[dend].length); dend++)
                                ;
+                       dend++;
                        drl = ntfs_rl_realloc(drl, dend, dend + 1);
                        if (IS_ERR(drl))
                                return drl;
@@ -566,8 +612,8 @@ runlist_element *ntfs_runlists_merge(runlist_element *drl,
                 ((drl[dins].vcn + drl[dins].length) <=      /* End of hole   */
                  (srl[send - 1].vcn + srl[send - 1].length)));
 
-       /* Or we'll lose an end marker */
-       if (start && finish && (drl[dins].length == 0))
+       /* Or we will lose an end marker. */
+       if (finish && !drl[dins].length)
                ss++;
        if (marker && (drl[dins].vcn + drl[dins].length > srl[send - 1].vcn))
                finish = FALSE;
@@ -621,11 +667,8 @@ runlist_element *ntfs_runlists_merge(runlist_element *drl,
                        if (drl[ds].lcn != LCN_RL_NOT_MAPPED) {
                                /* Add an unmapped runlist element. */
                                if (!slots) {
-                                       /* FIXME/TODO: We need to have the
-                                        * extra memory already! (AIA) */
-                                       drl = ntfs_rl_realloc(drl, ds, ds + 2);
-                                       if (!drl)
-                                               goto critical_error;
+                                       drl = ntfs_rl_realloc_nofail(drl, ds,
+                                                       ds + 2);
                                        slots = 2;
                                }
                                ds++;
@@ -640,13 +683,8 @@ runlist_element *ntfs_runlists_merge(runlist_element *drl,
                        drl[ds].length = marker_vcn - drl[ds].vcn;
                        /* Finally add the ENOENT terminator. */
                        ds++;
-                       if (!slots) {
-                               /* FIXME/TODO: We need to have the extra
-                                * memory already! (AIA) */
-                               drl = ntfs_rl_realloc(drl, ds, ds + 1);
-                               if (!drl)
-                                       goto critical_error;
-                       }
+                       if (!slots)
+                               drl = ntfs_rl_realloc_nofail(drl, ds, ds + 1);
                        drl[ds].vcn = marker_vcn;
                        drl[ds].lcn = LCN_ENOENT;
                        drl[ds].length = (s64)0;
@@ -659,11 +697,6 @@ finished:
        ntfs_debug("Merged runlist:");
        ntfs_debug_dump_runlist(drl);
        return drl;
-
-critical_error:
-       /* Critical error! We cannot afford to fail here. */
-       ntfs_error(NULL, "Critical error! Not enough memory.");
-       panic("NTFS: Cannot continue.");
 }
 
 /**
@@ -727,6 +760,9 @@ runlist_element *ntfs_mapping_pairs_decompress(const ntfs_volume *vol,
                ntfs_error(vol->sb, "Corrupt attribute.");
                return ERR_PTR(-EIO);
        }
+       /* If the mapping pairs array is valid but empty, nothing to do. */
+       if (!vcn && !*buf)
+               return old_rl;
        /* Current position in runlist array. */
        rlpos = 0;
        /* Allocate first page and set current runlist size to one page. */
@@ -1048,10 +1084,17 @@ static inline int ntfs_get_nr_significant_bytes(const s64 n)
  * ntfs_get_size_for_mapping_pairs - get bytes needed for mapping pairs array
  * @vol:       ntfs volume (needed for the ntfs version)
  * @rl:                locked runlist to determine the size of the mapping pairs of
- * @start_vcn: vcn at which to start the mapping pairs array
+ * @first_vcn: first vcn which to include in the mapping pairs array
+ * @last_vcn:  last vcn which to include in the mapping pairs array
  *
  * Walk the locked runlist @rl and calculate the size in bytes of the mapping
- * pairs array corresponding to the runlist @rl, starting at vcn @start_vcn.
+ * pairs array corresponding to the runlist @rl, starting at vcn @first_vcn and
+ * finishing with vcn @last_vcn.
+ *
+ * A @last_vcn of -1 means end of runlist and in that case the size of the
+ * mapping pairs array corresponding to the runlist starting at vcn @first_vcn
+ * and finishing at the end of the runlist is determined.
+ *
  * This for example allows us to allocate a buffer of the right size when
  * building the mapping pairs array.
  *
@@ -1067,34 +1110,50 @@ static inline int ntfs_get_nr_significant_bytes(const s64 n)
  *         remains locked throughout, and is left locked upon return.
  */
 int ntfs_get_size_for_mapping_pairs(const ntfs_volume *vol,
-               const runlist_element *rl, const VCN start_vcn)
+               const runlist_element *rl, const VCN first_vcn,
+               const VCN last_vcn)
 {
        LCN prev_lcn;
        int rls;
+       BOOL the_end = FALSE;
 
-       BUG_ON(start_vcn < 0);
+       BUG_ON(first_vcn < 0);
+       BUG_ON(last_vcn < -1);
+       BUG_ON(last_vcn >= 0 && first_vcn > last_vcn);
        if (!rl) {
-               BUG_ON(start_vcn);
+               BUG_ON(first_vcn);
+               BUG_ON(last_vcn > 0);
                return 1;
        }
-       /* Skip to runlist element containing @start_vcn. */
-       while (rl->length && start_vcn >= rl[1].vcn)
+       /* Skip to runlist element containing @first_vcn. */
+       while (rl->length && first_vcn >= rl[1].vcn)
                rl++;
-       if ((!rl->length && start_vcn > rl->vcn) || start_vcn < rl->vcn)
+       if (unlikely((!rl->length && first_vcn > rl->vcn) ||
+                       first_vcn < rl->vcn))
                return -EINVAL;
        prev_lcn = 0;
        /* Always need the termining zero byte. */
        rls = 1;
        /* Do the first partial run if present. */
-       if (start_vcn > rl->vcn) {
-               s64 delta;
+       if (first_vcn > rl->vcn) {
+               s64 delta, length = rl->length;
 
                /* We know rl->length != 0 already. */
-               if (rl->length < 0 || rl->lcn < LCN_HOLE)
+               if (unlikely(length < 0 || rl->lcn < LCN_HOLE))
                        goto err_out;
-               delta = start_vcn - rl->vcn;
+               /*
+                * If @stop_vcn is given and finishes inside this run, cap the
+                * run length.
+                */
+               if (unlikely(last_vcn >= 0 && rl[1].vcn > last_vcn)) {
+                       s64 s1 = last_vcn + 1;
+                       if (unlikely(rl[1].vcn > s1))
+                               length = s1 - rl->vcn;
+                       the_end = TRUE;
+               }
+               delta = first_vcn - rl->vcn;
                /* Header byte + length. */
-               rls += 1 + ntfs_get_nr_significant_bytes(rl->length - delta);
+               rls += 1 + ntfs_get_nr_significant_bytes(length - delta);
                /*
                 * If the logical cluster number (lcn) denotes a hole and we
                 * are on NTFS 3.0+, we don't store it at all, i.e. we need
@@ -1102,9 +1161,9 @@ int ntfs_get_size_for_mapping_pairs(const ntfs_volume *vol,
                 * Note: this assumes that on NTFS 1.2-, holes are stored with
                 * an lcn of -1 and not a delta_lcn of -1 (unless both are -1).
                 */
-               if (rl->lcn >= 0 || vol->major_ver < 3) {
+               if (likely(rl->lcn >= 0 || vol->major_ver < 3)) {
                        prev_lcn = rl->lcn;
-                       if (rl->lcn >= 0)
+                       if (likely(rl->lcn >= 0))
                                prev_lcn += delta;
                        /* Change in lcn. */
                        rls += ntfs_get_nr_significant_bytes(prev_lcn);
@@ -1113,11 +1172,23 @@ int ntfs_get_size_for_mapping_pairs(const ntfs_volume *vol,
                rl++;
        }
        /* Do the full runs. */
-       for (; rl->length; rl++) {
-               if (rl->length < 0 || rl->lcn < LCN_HOLE)
+       for (; rl->length && !the_end; rl++) {
+               s64 length = rl->length;
+
+               if (unlikely(length < 0 || rl->lcn < LCN_HOLE))
                        goto err_out;
+               /*
+                * If @stop_vcn is given and finishes inside this run, cap the
+                * run length.
+                */
+               if (unlikely(last_vcn >= 0 && rl[1].vcn > last_vcn)) {
+                       s64 s1 = last_vcn + 1;
+                       if (unlikely(rl[1].vcn > s1))
+                               length = s1 - rl->vcn;
+                       the_end = TRUE;
+               }
                /* Header byte + length. */
-               rls += 1 + ntfs_get_nr_significant_bytes(rl->length);
+               rls += 1 + ntfs_get_nr_significant_bytes(length);
                /*
                 * If the logical cluster number (lcn) denotes a hole and we
                 * are on NTFS 3.0+, we don't store it at all, i.e. we need
@@ -1125,7 +1196,7 @@ int ntfs_get_size_for_mapping_pairs(const ntfs_volume *vol,
                 * Note: this assumes that on NTFS 1.2-, holes are stored with
                 * an lcn of -1 and not a delta_lcn of -1 (unless both are -1).
                 */
-               if (rl->lcn >= 0 || vol->major_ver < 3) {
+               if (likely(rl->lcn >= 0 || vol->major_ver < 3)) {
                        /* Change in lcn. */
                        rls += ntfs_get_nr_significant_bytes(rl->lcn -
                                        prev_lcn);
@@ -1168,7 +1239,7 @@ static inline int ntfs_write_significant_bytes(s8 *dst, const s8 *dst_max,
 
        i = 0;
        do {
-               if (dst > dst_max)
+               if (unlikely(dst > dst_max))
                        goto err_out;
                *dst++ = l & 0xffll;
                l >>= 8;
@@ -1177,12 +1248,12 @@ static inline int ntfs_write_significant_bytes(s8 *dst, const s8 *dst_max,
        j = (n >> 8 * (i - 1)) & 0xff;
        /* If the sign bit is wrong, we need an extra byte. */
        if (n < 0 && j >= 0) {
-               if (dst > dst_max)
+               if (unlikely(dst > dst_max))
                        goto err_out;
                i++;
                *dst = (s8)-1;
        } else if (n > 0 && j < 0) {
-               if (dst > dst_max)
+               if (unlikely(dst > dst_max))
                        goto err_out;
                i++;
                *dst = (s8)0;
@@ -1198,13 +1269,18 @@ err_out:
  * @dst:       destination buffer to which to write the mapping pairs array
  * @dst_len:   size of destination buffer @dst in bytes
  * @rl:                locked runlist for which to build the mapping pairs array
- * @start_vcn: vcn at which to start the mapping pairs array
+ * @first_vcn: first vcn which to include in the mapping pairs array
+ * @last_vcn:  last vcn which to include in the mapping pairs array
  * @stop_vcn:  first vcn outside destination buffer on success or -ENOSPC
  *
  * Create the mapping pairs array from the locked runlist @rl, starting at vcn
- * @start_vcn and save the array in @dst.  @dst_len is the size of @dst in
- * bytes and it should be at least equal to the value obtained by calling
- * ntfs_get_size_for_mapping_pairs().
+ * @first_vcn and finishing with vcn @last_vcn and save the array in @dst.
+ * @dst_len is the size of @dst in bytes and it should be at least equal to the
+ * value obtained by calling ntfs_get_size_for_mapping_pairs().
+ *
+ * A @last_vcn of -1 means end of runlist and in that case the mapping pairs
+ * array corresponding to the runlist starting at vcn @first_vcn and finishing
+ * at the end of the runlist is created.
  *
  * If @rl is NULL, just write a single terminator byte to @dst.
  *
@@ -1213,7 +1289,7 @@ err_out:
  * been filled with all the mapping pairs that will fit, thus it can be treated
  * as partial success, in that a new attribute extent needs to be created or
  * the next extent has to be used and the mapping pairs build has to be
- * continued with @start_vcn set to *@stop_vcn.
+ * continued with @first_vcn set to *@stop_vcn.
  *
  * Return 0 on success and -errno on error.  The following error codes are
  * defined:
@@ -1227,27 +1303,32 @@ err_out:
  */
 int ntfs_mapping_pairs_build(const ntfs_volume *vol, s8 *dst,
                const int dst_len, const runlist_element *rl,
-               const VCN start_vcn, VCN *const stop_vcn)
+               const VCN first_vcn, const VCN last_vcn, VCN *const stop_vcn)
 {
        LCN prev_lcn;
        s8 *dst_max, *dst_next;
        int err = -ENOSPC;
+       BOOL the_end = FALSE;
        s8 len_len, lcn_len;
 
-       BUG_ON(start_vcn < 0);
+       BUG_ON(first_vcn < 0);
+       BUG_ON(last_vcn < -1);
+       BUG_ON(last_vcn >= 0 && first_vcn > last_vcn);
        BUG_ON(dst_len < 1);
        if (!rl) {
-               BUG_ON(start_vcn);
+               BUG_ON(first_vcn);
+               BUG_ON(last_vcn > 0);
                if (stop_vcn)
                        *stop_vcn = 0;
                /* Terminator byte. */
                *dst = 0;
                return 0;
        }
-       /* Skip to runlist element containing @start_vcn. */
-       while (rl->length && start_vcn >= rl[1].vcn)
+       /* Skip to runlist element containing @first_vcn. */
+       while (rl->length && first_vcn >= rl[1].vcn)
                rl++;
-       if ((!rl->length && start_vcn > rl->vcn) || start_vcn < rl->vcn)
+       if (unlikely((!rl->length && first_vcn > rl->vcn) ||
+                       first_vcn < rl->vcn))
                return -EINVAL;
        /*
         * @dst_max is used for bounds checking in
@@ -1256,17 +1337,27 @@ int ntfs_mapping_pairs_build(const ntfs_volume *vol, s8 *dst,
        dst_max = dst + dst_len - 1;
        prev_lcn = 0;
        /* Do the first partial run if present. */
-       if (start_vcn > rl->vcn) {
-               s64 delta;
+       if (first_vcn > rl->vcn) {
+               s64 delta, length = rl->length;
 
                /* We know rl->length != 0 already. */
-               if (rl->length < 0 || rl->lcn < LCN_HOLE)
+               if (unlikely(length < 0 || rl->lcn < LCN_HOLE))
                        goto err_out;
-               delta = start_vcn - rl->vcn;
+               /*
+                * If @stop_vcn is given and finishes inside this run, cap the
+                * run length.
+                */
+               if (unlikely(last_vcn >= 0 && rl[1].vcn > last_vcn)) {
+                       s64 s1 = last_vcn + 1;
+                       if (unlikely(rl[1].vcn > s1))
+                               length = s1 - rl->vcn;
+                       the_end = TRUE;
+               }
+               delta = first_vcn - rl->vcn;
                /* Write length. */
                len_len = ntfs_write_significant_bytes(dst + 1, dst_max,
-                               rl->length - delta);
-               if (len_len < 0)
+                               length - delta);
+               if (unlikely(len_len < 0))
                        goto size_err;
                /*
                 * If the logical cluster number (lcn) denotes a hole and we
@@ -1277,19 +1368,19 @@ int ntfs_mapping_pairs_build(const ntfs_volume *vol, s8 *dst,
                 * case on NT4. - We assume that we just need to write the lcn
                 * change until someone tells us otherwise... (AIA)
                 */
-               if (rl->lcn >= 0 || vol->major_ver < 3) {
+               if (likely(rl->lcn >= 0 || vol->major_ver < 3)) {
                        prev_lcn = rl->lcn;
-                       if (rl->lcn >= 0)
+                       if (likely(rl->lcn >= 0))
                                prev_lcn += delta;
                        /* Write change in lcn. */
                        lcn_len = ntfs_write_significant_bytes(dst + 1 +
                                        len_len, dst_max, prev_lcn);
-                       if (lcn_len < 0)
+                       if (unlikely(lcn_len < 0))
                                goto size_err;
                } else
                        lcn_len = 0;
                dst_next = dst + len_len + lcn_len + 1;
-               if (dst_next > dst_max)
+               if (unlikely(dst_next > dst_max))
                        goto size_err;
                /* Update header byte. */
                *dst = lcn_len << 4 | len_len;
@@ -1299,13 +1390,25 @@ int ntfs_mapping_pairs_build(const ntfs_volume *vol, s8 *dst,
                rl++;
        }
        /* Do the full runs. */
-       for (; rl->length; rl++) {
-               if (rl->length < 0 || rl->lcn < LCN_HOLE)
+       for (; rl->length && !the_end; rl++) {
+               s64 length = rl->length;
+
+               if (unlikely(length < 0 || rl->lcn < LCN_HOLE))
                        goto err_out;
+               /*
+                * If @stop_vcn is given and finishes inside this run, cap the
+                * run length.
+                */
+               if (unlikely(last_vcn >= 0 && rl[1].vcn > last_vcn)) {
+                       s64 s1 = last_vcn + 1;
+                       if (unlikely(rl[1].vcn > s1))
+                               length = s1 - rl->vcn;
+                       the_end = TRUE;
+               }
                /* Write length. */
                len_len = ntfs_write_significant_bytes(dst + 1, dst_max,
-                               rl->length);
-               if (len_len < 0)
+                               length);
+               if (unlikely(len_len < 0))
                        goto size_err;
                /*
                 * If the logical cluster number (lcn) denotes a hole and we
@@ -1316,17 +1419,17 @@ int ntfs_mapping_pairs_build(const ntfs_volume *vol, s8 *dst,
                 * case on NT4. - We assume that we just need to write the lcn
                 * change until someone tells us otherwise... (AIA)
                 */
-               if (rl->lcn >= 0 || vol->major_ver < 3) {
+               if (likely(rl->lcn >= 0 || vol->major_ver < 3)) {
                        /* Write change in lcn. */
                        lcn_len = ntfs_write_significant_bytes(dst + 1 +
                                        len_len, dst_max, rl->lcn - prev_lcn);
-                       if (lcn_len < 0)
+                       if (unlikely(lcn_len < 0))
                                goto size_err;
                        prev_lcn = rl->lcn;
                } else
                        lcn_len = 0;
                dst_next = dst + len_len + lcn_len + 1;
-               if (dst_next > dst_max)
+               if (unlikely(dst_next > dst_max))
                        goto size_err;
                /* Update header byte. */
                *dst = lcn_len << 4 | len_len;
@@ -1352,6 +1455,7 @@ err_out:
 
 /**
  * ntfs_rl_truncate_nolock - truncate a runlist starting at a specified vcn
+ * @vol:       ntfs volume (needed for error output)
  * @runlist:   runlist to truncate
  * @new_length:        the new length of the runlist in VCNs
  *
@@ -1359,12 +1463,16 @@ err_out:
  * holding the runlist elements to a length of @new_length VCNs.
  *
  * If @new_length lies within the runlist, the runlist elements with VCNs of
- * @new_length and above are discarded.
+ * @new_length and above are discarded.  As a special case if @new_length is
+ * zero, the runlist is discarded and set to NULL.
  *
  * If @new_length lies beyond the runlist, a sparse runlist element is added to
  * the end of the runlist @runlist or if the last runlist element is a sparse
  * one already, this is extended.
  *
+ * Note, no checking is done for unmapped runlist elements.  It is assumed that
+ * the caller has mapped any elements that need to be mapped already.
+ *
  * Return 0 on success and -errno on error.
  *
  * Locking: The caller must hold @runlist->lock for writing.
@@ -1379,6 +1487,13 @@ int ntfs_rl_truncate_nolock(const ntfs_volume *vol, runlist *const runlist,
        BUG_ON(!runlist);
        BUG_ON(new_length < 0);
        rl = runlist->rl;
+       if (!new_length) {
+               ntfs_debug("Freeing runlist.");
+               runlist->rl = NULL;
+               if (rl)
+                       ntfs_free(rl);
+               return 0;
+       }
        if (unlikely(!rl)) {
                /*
                 * Create a runlist consisting of a sparse runlist element of
@@ -1486,4 +1601,288 @@ int ntfs_rl_truncate_nolock(const ntfs_volume *vol, runlist *const runlist,
        return 0;
 }
 
+/**
+ * ntfs_rl_punch_nolock - punch a hole into a runlist
+ * @vol:       ntfs volume (needed for error output)
+ * @runlist:   runlist to punch a hole into
+ * @start:     starting VCN of the hole to be created
+ * @length:    size of the hole to be created in units of clusters
+ *
+ * Punch a hole into the runlist @runlist starting at VCN @start and of size
+ * @length clusters.
+ *
+ * Return 0 on success and -errno on error, in which case @runlist has not been
+ * modified.
+ *
+ * If @start and/or @start + @length are outside the runlist return error code
+ * -ENOENT.
+ *
+ * If the runlist contains unmapped or error elements between @start and @start
+ * + @length return error code -EINVAL.
+ *
+ * Locking: The caller must hold @runlist->lock for writing.
+ */
+int ntfs_rl_punch_nolock(const ntfs_volume *vol, runlist *const runlist,
+               const VCN start, const s64 length)
+{
+       const VCN end = start + length;
+       s64 delta;
+       runlist_element *rl, *rl_end, *rl_real_end, *trl;
+       int old_size;
+       BOOL lcn_fixup = FALSE;
+
+       ntfs_debug("Entering for start 0x%llx, length 0x%llx.",
+                       (long long)start, (long long)length);
+       BUG_ON(!runlist);
+       BUG_ON(start < 0);
+       BUG_ON(length < 0);
+       BUG_ON(end < 0);
+       rl = runlist->rl;
+       if (unlikely(!rl)) {
+               if (likely(!start && !length))
+                       return 0;
+               return -ENOENT;
+       }
+       /* Find @start in the runlist. */
+       while (likely(rl->length && start >= rl[1].vcn))
+               rl++;
+       rl_end = rl;
+       /* Find @end in the runlist. */
+       while (likely(rl_end->length && end >= rl_end[1].vcn)) {
+               /* Verify there are no unmapped or error elements. */
+               if (unlikely(rl_end->lcn < LCN_HOLE))
+                       return -EINVAL;
+               rl_end++;
+       }
+       /* Check the last element. */
+       if (unlikely(rl_end->length && rl_end->lcn < LCN_HOLE))
+               return -EINVAL;
+       /* This covers @start being out of bounds, too. */
+       if (!rl_end->length && end > rl_end->vcn)
+               return -ENOENT;
+       if (!length)
+               return 0;
+       if (!rl->length)
+               return -ENOENT;
+       rl_real_end = rl_end;
+       /* Determine the runlist size. */
+       while (likely(rl_real_end->length))
+               rl_real_end++;
+       old_size = rl_real_end - runlist->rl + 1;
+       /* If @start is in a hole simply extend the hole. */
+       if (rl->lcn == LCN_HOLE) {
+               /*
+                * If both @start and @end are in the same sparse run, we are
+                * done.
+                */
+               if (end <= rl[1].vcn) {
+                       ntfs_debug("Done (requested hole is already sparse).");
+                       return 0;
+               }
+extend_hole:
+               /* Extend the hole. */
+               rl->length = end - rl->vcn;
+               /* If @end is in a hole, merge it with the current one. */
+               if (rl_end->lcn == LCN_HOLE) {
+                       rl_end++;
+                       rl->length = rl_end->vcn - rl->vcn;
+               }
+               /* We have done the hole.  Now deal with the remaining tail. */
+               rl++;
+               /* Cut out all runlist elements up to @end. */
+               if (rl < rl_end)
+                       memmove(rl, rl_end, (rl_real_end - rl_end + 1) *
+                                       sizeof(*rl));
+               /* Adjust the beginning of the tail if necessary. */
+               if (end > rl->vcn) {
+                       s64 delta = end - rl->vcn;
+                       rl->vcn = end;
+                       rl->length -= delta;
+                       /* Only adjust the lcn if it is real. */
+                       if (rl->lcn >= 0)
+                               rl->lcn += delta;
+               }
+shrink_allocation:
+               /* Reallocate memory if the allocation changed. */
+               if (rl < rl_end) {
+                       rl = ntfs_rl_realloc(runlist->rl, old_size,
+                                       old_size - (rl_end - rl));
+                       if (IS_ERR(rl))
+                               ntfs_warning(vol->sb, "Failed to shrink "
+                                               "runlist buffer.  This just "
+                                               "wastes a bit of memory "
+                                               "temporarily so we ignore it "
+                                               "and return success.");
+                       else
+                               runlist->rl = rl;
+               }
+               ntfs_debug("Done (extend hole).");
+               return 0;
+       }
+       /*
+        * If @start is at the beginning of a run things are easier as there is
+        * no need to split the first run.
+        */
+       if (start == rl->vcn) {
+               /*
+                * @start is at the beginning of a run.
+                *
+                * If the previous run is sparse, extend its hole.
+                *
+                * If @end is not in the same run, switch the run to be sparse
+                * and extend the newly created hole.
+                *
+                * Thus both of these cases reduce the problem to the above
+                * case of "@start is in a hole".
+                */
+               if (rl > runlist->rl && (rl - 1)->lcn == LCN_HOLE) {
+                       rl--;
+                       goto extend_hole;
+               }
+               if (end >= rl[1].vcn) {
+                       rl->lcn = LCN_HOLE;
+                       goto extend_hole;
+               }
+               /*
+                * The final case is when @end is in the same run as @start.
+                * For this need to split the run into two.  One run for the
+                * sparse region between the beginning of the old run, i.e.
+                * @start, and @end and one for the remaining non-sparse
+                * region, i.e. between @end and the end of the old run.
+                */
+               trl = ntfs_rl_realloc(runlist->rl, old_size, old_size + 1);
+               if (IS_ERR(trl))
+                       goto enomem_out;
+               old_size++;
+               if (runlist->rl != trl) {
+                       rl = trl + (rl - runlist->rl);
+                       rl_end = trl + (rl_end - runlist->rl);
+                       rl_real_end = trl + (rl_real_end - runlist->rl);
+                       runlist->rl = trl;
+               }
+split_end:
+               /* Shift all the runs up by one. */
+               memmove(rl + 1, rl, (rl_real_end - rl + 1) * sizeof(*rl));
+               /* Finally, setup the two split runs. */
+               rl->lcn = LCN_HOLE;
+               rl->length = length;
+               rl++;
+               rl->vcn += length;
+               /* Only adjust the lcn if it is real. */
+               if (rl->lcn >= 0 || lcn_fixup)
+                       rl->lcn += length;
+               rl->length -= length;
+               ntfs_debug("Done (split one).");
+               return 0;
+       }
+       /*
+        * @start is neither in a hole nor at the beginning of a run.
+        *
+        * If @end is in a hole, things are easier as simply truncating the run
+        * @start is in to end at @start - 1, deleting all runs after that up
+        * to @end, and finally extending the beginning of the run @end is in
+        * to be @start is all that is needed.
+        */
+       if (rl_end->lcn == LCN_HOLE) {
+               /* Truncate the run containing @start. */
+               rl->length = start - rl->vcn;
+               rl++;
+               /* Cut out all runlist elements up to @end. */
+               if (rl < rl_end)
+                       memmove(rl, rl_end, (rl_real_end - rl_end + 1) *
+                                       sizeof(*rl));
+               /* Extend the beginning of the run @end is in to be @start. */
+               rl->vcn = start;
+               rl->length = rl[1].vcn - start;
+               goto shrink_allocation;
+       }
+       /* 
+        * If @end is not in a hole there are still two cases to distinguish.
+        * Either @end is or is not in the same run as @start.
+        *
+        * The second case is easier as it can be reduced to an already solved
+        * problem by truncating the run @start is in to end at @start - 1.
+        * Then, if @end is in the next run need to split the run into a sparse
+        * run followed by a non-sparse run (already covered above) and if @end
+        * is not in the next run switching it to be sparse, again reduces the
+        * problem to the already covered case of "@start is in a hole".
+        */
+       if (end >= rl[1].vcn) {
+               /*
+                * If @end is not in the next run, reduce the problem to the
+                * case of "@start is in a hole".
+                */
+               if (rl[1].length && end >= rl[2].vcn) {
+                       /* Truncate the run containing @start. */
+                       rl->length = start - rl->vcn;
+                       rl++;
+                       rl->vcn = start;
+                       rl->lcn = LCN_HOLE;
+                       goto extend_hole;
+               }
+               trl = ntfs_rl_realloc(runlist->rl, old_size, old_size + 1);
+               if (IS_ERR(trl))
+                       goto enomem_out;
+               old_size++;
+               if (runlist->rl != trl) {
+                       rl = trl + (rl - runlist->rl);
+                       rl_end = trl + (rl_end - runlist->rl);
+                       rl_real_end = trl + (rl_real_end - runlist->rl);
+                       runlist->rl = trl;
+               }
+               /* Truncate the run containing @start. */
+               rl->length = start - rl->vcn;
+               rl++;
+               /*
+                * @end is in the next run, reduce the problem to the case
+                * where "@start is at the beginning of a run and @end is in
+                * the same run as @start".
+                */
+               delta = rl->vcn - start;
+               rl->vcn = start;
+               if (rl->lcn >= 0) {
+                       rl->lcn -= delta;
+                       /* Need this in case the lcn just became negative. */
+                       lcn_fixup = TRUE;
+               }
+               rl->length += delta;
+               goto split_end;
+       }
+       /*
+        * The first case from above, i.e. @end is in the same run as @start.
+        * We need to split the run into three.  One run for the non-sparse
+        * region between the beginning of the old run and @start, one for the
+        * sparse region between @start and @end, and one for the remaining
+        * non-sparse region, i.e. between @end and the end of the old run.
+        */
+       trl = ntfs_rl_realloc(runlist->rl, old_size, old_size + 2);
+       if (IS_ERR(trl))
+               goto enomem_out;
+       old_size += 2;
+       if (runlist->rl != trl) {
+               rl = trl + (rl - runlist->rl);
+               rl_end = trl + (rl_end - runlist->rl);
+               rl_real_end = trl + (rl_real_end - runlist->rl);
+               runlist->rl = trl;
+       }
+       /* Shift all the runs up by two. */
+       memmove(rl + 2, rl, (rl_real_end - rl + 1) * sizeof(*rl));
+       /* Finally, setup the three split runs. */
+       rl->length = start - rl->vcn;
+       rl++;
+       rl->vcn = start;
+       rl->lcn = LCN_HOLE;
+       rl->length = length;
+       rl++;
+       delta = end - rl->vcn;
+       rl->vcn = end;
+       rl->lcn += delta;
+       rl->length -= delta;
+       ntfs_debug("Done (split both).");
+       return 0;
+enomem_out:
+       ntfs_error(vol->sb, "Not enough memory to extend runlist buffer.");
+       return -ENOMEM;
+}
+
 #endif /* NTFS_RW */