Merge branch 'for-linus' of git://git390.marist.edu/pub/scm/linux-2.6
[safe/jmp/linux-2.6] / fs / btrfs / tree-defrag.c
index cc2650b..f7ac8e0 100644 (file)
 #include "transaction.h"
 #include "locking.h"
 
+/* defrag all the leaves in a given btree.  If cache_only == 1, don't read
+ * things from disk, otherwise read all the leaves and try to get key order to
+ * better reflect disk order
+ */
+
 int btrfs_defrag_leaves(struct btrfs_trans_handle *trans,
                        struct btrfs_root *root, int cache_only)
 {
@@ -61,14 +66,15 @@ int btrfs_defrag_leaves(struct btrfs_trans_handle *trans,
        level = btrfs_header_level(root->node);
        orig_level = level;
 
-       if (level == 0) {
+       if (level == 0)
                goto out;
-       }
+
        if (root->defrag_progress.objectid == 0) {
                struct extent_buffer *root_node;
                u32 nritems;
 
                root_node = btrfs_lock_root_node(root);
+               btrfs_set_lock_blocking(root_node);
                nritems = btrfs_header_nritems(root_node);
                root->defrag_max.objectid = 0;
                /* from above we know this is not a leaf */
@@ -81,12 +87,12 @@ int btrfs_defrag_leaves(struct btrfs_trans_handle *trans,
                memcpy(&key, &root->defrag_progress, sizeof(key));
        }
 
-       path->lowest_level = 1;
        path->keep_locks = 1;
        if (cache_only)
                min_trans = root->defrag_trans_start;
 
-       ret = btrfs_search_forward(root, &key, path, cache_only, min_trans);
+       ret = btrfs_search_forward(root, &key, NULL, path,
+                                  cache_only, min_trans);
        if (ret < 0)
                goto out;
        if (ret > 0) {
@@ -111,19 +117,15 @@ int btrfs_defrag_leaves(struct btrfs_trans_handle *trans,
                                 path->nodes[1], 0,
                                 cache_only, &last_ret,
                                 &root->defrag_progress);
-       WARN_ON(ret && ret != -EAGAIN);
+       if (ret) {
+               WARN_ON(ret == -EAGAIN);
+               goto out;
+       }
        if (next_key_ret == 0) {
                memcpy(&root->defrag_progress, &key, sizeof(key));
                ret = -EAGAIN;
        }
-
-       btrfs_release_path(root, path);
-       if (is_extent)
-               btrfs_extent_post_op(trans, root);
 out:
-       if (is_extent)
-               mutex_unlock(&root->fs_info->alloc_mutex);
-
        if (path)
                btrfs_free_path(path);
        if (ret == -EAGAIN) {