btrfs-progs: convert: move wipe_one_reserved_range
authorDavid Sterba <dsterba@suse.com>
Tue, 23 Aug 2016 15:44:36 +0000 (17:44 +0200)
committerDavid Sterba <dsterba@suse.com>
Wed, 24 Aug 2016 12:37:36 +0000 (14:37 +0200)
Signed-off-by: David Sterba <dsterba@suse.com>
btrfs-convert.c

index e80ea3b..cac7d42 100644 (file)
@@ -706,6 +706,109 @@ expand_after:
 }
 
 /*
+ * Remove one reserve range from given cache tree
+ * if min_stripe_size is non-zero, it will ensure for split case,
+ * all its split cache extent is no smaller than @min_strip_size / 2.
+ */
+static int wipe_one_reserved_range(struct cache_tree *tree,
+                                  u64 start, u64 len, u64 min_stripe_size,
+                                  int ensure_size)
+{
+       struct cache_extent *cache;
+       int ret;
+
+       BUG_ON(ensure_size && min_stripe_size == 0);
+       /*
+        * The logical here is simplified to handle special cases only
+        * So we don't need to consider merge case for ensure_size
+        */
+       BUG_ON(min_stripe_size && (min_stripe_size < len * 2 ||
+              min_stripe_size / 2 < BTRFS_STRIPE_LEN));
+
+       /* Also, wipe range should already be aligned */
+       BUG_ON(start != round_down(start, BTRFS_STRIPE_LEN) ||
+              start + len != round_up(start + len, BTRFS_STRIPE_LEN));
+
+       min_stripe_size /= 2;
+
+       cache = lookup_cache_extent(tree, start, len);
+       if (!cache)
+               return 0;
+
+       if (start <= cache->start) {
+               /*
+                *      |--------cache---------|
+                * |-wipe-|
+                */
+               BUG_ON(start + len <= cache->start);
+
+               /*
+                * The wipe size is smaller than min_stripe_size / 2,
+                * so the result length should still meet min_stripe_size
+                * And no need to do alignment
+                */
+               cache->size -= (start + len - cache->start);
+               if (cache->size == 0) {
+                       remove_cache_extent(tree, cache);
+                       free(cache);
+                       return 0;
+               }
+
+               BUG_ON(ensure_size && cache->size < min_stripe_size);
+
+               cache->start = start + len;
+               return 0;
+       } else if (start > cache->start && start + len < cache->start +
+                  cache->size) {
+               /*
+                * |-------cache-----|
+                *      |-wipe-|
+                */
+               u64 old_start = cache->start;
+               u64 old_len = cache->size;
+               u64 insert_start = start + len;
+               u64 insert_len;
+
+               cache->size = start - cache->start;
+               /* Expand the leading half part if needed */
+               if (ensure_size && cache->size < min_stripe_size) {
+                       ret = _expand_extent_cache(tree, cache,
+                                       min_stripe_size, 1);
+                       if (ret < 0)
+                               return ret;
+               }
+
+               /* And insert the new one */
+               insert_len = old_start + old_len - start - len;
+               ret = add_merge_cache_extent(tree, insert_start, insert_len);
+               if (ret < 0)
+                       return ret;
+
+               /* Expand the last half part if needed */
+               if (ensure_size && insert_len < min_stripe_size) {
+                       cache = lookup_cache_extent(tree, insert_start,
+                                                   insert_len);
+                       if (!cache || cache->start != insert_start ||
+                           cache->size != insert_len)
+                               return -ENOENT;
+                       ret = _expand_extent_cache(tree, cache,
+                                       min_stripe_size, 0);
+               }
+
+               return ret;
+       }
+       /*
+        * |----cache-----|
+        *              |--wipe-|
+        * Wipe len should be small enough and no need to expand the
+        * remaining extent
+        */
+       cache->size = start - cache->start;
+       BUG_ON(ensure_size && cache->size < min_stripe_size);
+       return 0;
+}
+
+/*
  * Open Ext2fs in readonly mode, read block allocation bitmap and
  * inode bitmap into memory.
  */
@@ -2047,109 +2150,6 @@ static int convert_open_fs(const char *devname,
 }
 
 /*
- * Remove one reserve range from given cache tree
- * if min_stripe_size is non-zero, it will ensure for split case,
- * all its split cache extent is no smaller than @min_strip_size / 2.
- */
-static int wipe_one_reserved_range(struct cache_tree *tree,
-                                  u64 start, u64 len, u64 min_stripe_size,
-                                  int ensure_size)
-{
-       struct cache_extent *cache;
-       int ret;
-
-       BUG_ON(ensure_size && min_stripe_size == 0);
-       /*
-        * The logical here is simplified to handle special cases only
-        * So we don't need to consider merge case for ensure_size
-        */
-       BUG_ON(min_stripe_size && (min_stripe_size < len * 2 ||
-              min_stripe_size / 2 < BTRFS_STRIPE_LEN));
-
-       /* Also, wipe range should already be aligned */
-       BUG_ON(start != round_down(start, BTRFS_STRIPE_LEN) ||
-              start + len != round_up(start + len, BTRFS_STRIPE_LEN));
-
-       min_stripe_size /= 2;
-
-       cache = lookup_cache_extent(tree, start, len);
-       if (!cache)
-               return 0;
-
-       if (start <= cache->start) {
-               /*
-                *      |--------cache---------|
-                * |-wipe-|
-                */
-               BUG_ON(start + len <= cache->start);
-
-               /*
-                * The wipe size is smaller than min_stripe_size / 2,
-                * so the result length should still meet min_stripe_size
-                * And no need to do alignment
-                */
-               cache->size -= (start + len - cache->start);
-               if (cache->size == 0) {
-                       remove_cache_extent(tree, cache);
-                       free(cache);
-                       return 0;
-               }
-
-               BUG_ON(ensure_size && cache->size < min_stripe_size);
-
-               cache->start = start + len;
-               return 0;
-       } else if (start > cache->start && start + len < cache->start +
-                  cache->size) {
-               /*
-                * |-------cache-----|
-                *      |-wipe-|
-                */
-               u64 old_start = cache->start;
-               u64 old_len = cache->size;
-               u64 insert_start = start + len;
-               u64 insert_len;
-
-               cache->size = start - cache->start;
-               /* Expand the leading half part if needed */
-               if (ensure_size && cache->size < min_stripe_size) {
-                       ret = _expand_extent_cache(tree, cache,
-                                       min_stripe_size, 1);
-                       if (ret < 0)
-                               return ret;
-               }
-
-               /* And insert the new one */
-               insert_len = old_start + old_len - start - len;
-               ret = add_merge_cache_extent(tree, insert_start, insert_len);
-               if (ret < 0)
-                       return ret;
-
-               /* Expand the last half part if needed */
-               if (ensure_size && insert_len < min_stripe_size) {
-                       cache = lookup_cache_extent(tree, insert_start,
-                                                   insert_len);
-                       if (!cache || cache->start != insert_start ||
-                           cache->size != insert_len)
-                               return -ENOENT;
-                       ret = _expand_extent_cache(tree, cache,
-                                       min_stripe_size, 0);
-               }
-
-               return ret;
-       }
-       /*
-        * |----cache-----|
-        *              |--wipe-|
-        * Wipe len should be small enough and no need to expand the
-        * remaining extent
-        */
-       cache->size = start - cache->start;
-       BUG_ON(ensure_size && cache->size < min_stripe_size);
-       return 0;
-}
-
-/*
  * Remove reserved ranges from given cache_tree
  *
  * It will remove the following ranges