Btrfs: Add support for online device removal
[safe/jmp/linux-2.6] / fs / btrfs / tree-defrag.c
index f86eccf..256af18 100644 (file)
@@ -58,6 +58,10 @@ static int defrag_walk_down(struct btrfs_trans_handle *trans,
        if (root->fs_info->extent_root == root)
                is_extent = 1;
 
+       if (*level == 1 && cache_only && path->nodes[1] &&
+           !btrfs_buffer_defrag(path->nodes[1])) {
+               goto out;
+       }
        while(*level > 0) {
                WARN_ON(*level < 0);
                WARN_ON(*level >= BTRFS_MAX_LEVEL);
@@ -74,9 +78,13 @@ static int defrag_walk_down(struct btrfs_trans_handle *trans,
                        break;
 
                if (*level == 1) {
+                       WARN_ON(btrfs_header_generation(path->nodes[*level]) !=
+                                                       trans->transid);
                        ret = btrfs_realloc_node(trans, root,
                                                 path->nodes[*level],
-                                                cache_only, last_ret);
+                                                path->slots[*level],
+                                                cache_only, last_ret,
+                                                &root->defrag_progress);
                        if (is_extent)
                                btrfs_extent_post_op(trans, root);
 
@@ -93,6 +101,7 @@ static int defrag_walk_down(struct btrfs_trans_handle *trans,
                                path->slots[*level]++;
                                continue;
                        }
+                       btrfs_verify_block_csum(root, next);
                } else {
                        next = read_tree_block(root, bytenr,
                                       btrfs_level_size(root, *level - 1));
@@ -100,10 +109,6 @@ static int defrag_walk_down(struct btrfs_trans_handle *trans,
                ret = btrfs_cow_block(trans, root, next, path->nodes[*level],
                                      path->slots[*level], &next);
                BUG_ON(ret);
-               ret = btrfs_realloc_node(trans, root, next, cache_only,
-                                        last_ret);
-               BUG_ON(ret);
-
                if (is_extent)
                        btrfs_extent_post_op(trans, root);
 
@@ -118,12 +123,12 @@ static int defrag_walk_down(struct btrfs_trans_handle *trans,
        WARN_ON(*level >= BTRFS_MAX_LEVEL);
 
        btrfs_clear_buffer_defrag(path->nodes[*level]);
-
+out:
        free_extent_buffer(path->nodes[*level]);
        path->nodes[*level] = NULL;
        *level += 1;
-       WARN_ON(ret);
-       return 0;
+       WARN_ON(ret && ret != -EAGAIN);
+       return ret;
 }
 
 static int defrag_walk_up(struct btrfs_trans_handle *trans,
@@ -147,27 +152,6 @@ static int defrag_walk_up(struct btrfs_trans_handle *trans,
                        root->defrag_level = i;
                        return 0;
                } else {
-                       if (*level > 1 && path->nodes[*level] != root->node &&
-                           btrfs_buffer_defrag(path->nodes[*level])) {
-                               struct extent_buffer *next;
-                               u64 last;
-                               int ret;
-                               ret = btrfs_cow_block(trans, root,
-                                                     path->nodes[*level],
-                                                     path->nodes[*level + 1],
-                                                     path->slots[*level + 1],
-                                                     &next);
-                               BUG_ON(ret);
-                               path->nodes[*level] = next;
-                               last = next->start;
-                               ret = btrfs_realloc_node(trans, root, next,
-                                                        cache_only, &last);
-                               BUG_ON(ret);
-
-                               if (root == root->fs_info->extent_root)
-                                       btrfs_extent_post_op(trans, root);
-                       }
-
                        btrfs_clear_buffer_defrag(path->nodes[*level]);
                        free_extent_buffer(path->nodes[*level]);
                        path->nodes[*level] = NULL;
@@ -196,6 +180,9 @@ int btrfs_defrag_leaves(struct btrfs_trans_handle *trans,
        if (root->ref_cows == 0 && !is_extent)
                goto out;
 
+       if (btrfs_test_opt(root, SSD))
+               goto out;
+
        path = btrfs_alloc_path();
        if (!path)
                return -ENOMEM;
@@ -206,14 +193,10 @@ int btrfs_defrag_leaves(struct btrfs_trans_handle *trans,
        if (level == 0) {
                goto out;
        }
-
        if (root->defrag_progress.objectid == 0) {
                extent_buffer_get(root->node);
                ret = btrfs_cow_block(trans, root, root->node, NULL, 0, &tmp);
                BUG_ON(ret);
-               ret = btrfs_realloc_node(trans, root, root->node, cache_only,
-                                        &last_ret);
-               BUG_ON(ret);
                path->nodes[level] = root->node;
                path->slots[level] = 0;
                if (is_extent)
@@ -254,7 +237,8 @@ int btrfs_defrag_leaves(struct btrfs_trans_handle *trans,
                        break;
                if (wret < 0)
                        ret = wret;
-               ret = -EAGAIN;
+               else
+                       ret = -EAGAIN;
                break;
        }
        for (i = 0; i <= orig_level; i++) {