From f7318e2509949aa08d9a4af2862f9c2fa8a89f91 Mon Sep 17 00:00:00 2001 From: David Sterba Date: Tue, 23 Aug 2016 17:44:36 +0200 Subject: btrfs-progs: convert: move wipe_one_reserved_range Signed-off-by: David Sterba --- btrfs-convert.c | 206 ++++++++++++++++++++++++++++---------------------------- 1 file changed, 103 insertions(+), 103 deletions(-) diff --git a/btrfs-convert.c b/btrfs-convert.c index e80ea3b2..cac7d421 100644 --- a/btrfs-convert.c +++ b/btrfs-convert.c @@ -705,6 +705,109 @@ expand_after: return 0; } +/* + * 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. @@ -2046,109 +2149,6 @@ static int convert_open_fs(const char *devname, return -1; } -/* - * 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 * -- cgit v1.2.3