btrfs: rename tree-defrag.c to defrag.c
authorJosef Bacik <josef@toxicpanda.com>
Wed, 26 Oct 2022 19:08:22 +0000 (15:08 -0400)
committerDavid Sterba <dsterba@suse.com>
Mon, 5 Dec 2022 17:00:45 +0000 (18:00 +0100)
This currently has only one helper in it, and it's for tree based
defrag.  We have the various defrag code in 3 different places, so
rename this to defrag.c.  Followup patches will move the code into this
new file.

Reviewed-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>
Signed-off-by: Josef Bacik <josef@toxicpanda.com>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
fs/btrfs/Makefile
fs/btrfs/defrag.c [new file with mode: 0644]
fs/btrfs/tree-defrag.c [deleted file]

index bb170c9d030db5b7dad96f48ae7fd35d7a562e62..84fb3b4c35b0743ec592562cc37166cafc234db1 100644 (file)
@@ -23,7 +23,7 @@ obj-$(CONFIG_BTRFS_FS) := btrfs.o
 
 btrfs-y += super.o ctree.o extent-tree.o print-tree.o root-tree.o dir-item.o \
           file-item.o inode-item.o disk-io.o \
-          transaction.o inode.o file.o tree-defrag.o \
+          transaction.o inode.o file.o defrag.o \
           extent_map.o sysfs.o accessors.o xattr.o ordered-data.o \
           extent_io.o volumes.o async-thread.o ioctl.o locking.o orphan.o \
           export.o tree-log.o free-space-cache.o zlib.o lzo.o zstd.o \
diff --git a/fs/btrfs/defrag.c b/fs/btrfs/defrag.c
new file mode 100644 (file)
index 0000000..5df6048
--- /dev/null
@@ -0,0 +1,133 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (C) 2007 Oracle.  All rights reserved.
+ */
+
+#include <linux/sched.h>
+#include "ctree.h"
+#include "disk-io.h"
+#include "print-tree.h"
+#include "transaction.h"
+#include "locking.h"
+#include "accessors.h"
+
+/*
+ * Defrag all the leaves in a given btree.
+ * 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)
+{
+       struct btrfs_path *path = NULL;
+       struct btrfs_key key;
+       int ret = 0;
+       int wret;
+       int level;
+       int next_key_ret = 0;
+       u64 last_ret = 0;
+
+       if (!test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
+               goto out;
+
+       path = btrfs_alloc_path();
+       if (!path)
+               return -ENOMEM;
+
+       level = btrfs_header_level(root->node);
+
+       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);
+               nritems = btrfs_header_nritems(root_node);
+               root->defrag_max.objectid = 0;
+               /* from above we know this is not a leaf */
+               btrfs_node_key_to_cpu(root_node, &root->defrag_max,
+                                     nritems - 1);
+               btrfs_tree_unlock(root_node);
+               free_extent_buffer(root_node);
+               memset(&key, 0, sizeof(key));
+       } else {
+               memcpy(&key, &root->defrag_progress, sizeof(key));
+       }
+
+       path->keep_locks = 1;
+
+       ret = btrfs_search_forward(root, &key, path, BTRFS_OLDEST_GENERATION);
+       if (ret < 0)
+               goto out;
+       if (ret > 0) {
+               ret = 0;
+               goto out;
+       }
+       btrfs_release_path(path);
+       /*
+        * We don't need a lock on a leaf. btrfs_realloc_node() will lock all
+        * leafs from path->nodes[1], so set lowest_level to 1 to avoid later
+        * a deadlock (attempting to write lock an already write locked leaf).
+        */
+       path->lowest_level = 1;
+       wret = btrfs_search_slot(trans, root, &key, path, 0, 1);
+
+       if (wret < 0) {
+               ret = wret;
+               goto out;
+       }
+       if (!path->nodes[1]) {
+               ret = 0;
+               goto out;
+       }
+       /*
+        * The node at level 1 must always be locked when our path has
+        * keep_locks set and lowest_level is 1, regardless of the value of
+        * path->slots[1].
+        */
+       BUG_ON(path->locks[1] == 0);
+       ret = btrfs_realloc_node(trans, root,
+                                path->nodes[1], 0,
+                                &last_ret,
+                                &root->defrag_progress);
+       if (ret) {
+               WARN_ON(ret == -EAGAIN);
+               goto out;
+       }
+       /*
+        * Now that we reallocated the node we can find the next key. Note that
+        * btrfs_find_next_key() can release our path and do another search
+        * without COWing, this is because even with path->keep_locks = 1,
+        * btrfs_search_slot() / ctree.c:unlock_up() does not keeps a lock on a
+        * node when path->slots[node_level - 1] does not point to the last
+        * item or a slot beyond the last item (ctree.c:unlock_up()). Therefore
+        * we search for the next key after reallocating our node.
+        */
+       path->slots[1] = btrfs_header_nritems(path->nodes[1]);
+       next_key_ret = btrfs_find_next_key(root, path, &key, 1,
+                                          BTRFS_OLDEST_GENERATION);
+       if (next_key_ret == 0) {
+               memcpy(&root->defrag_progress, &key, sizeof(key));
+               ret = -EAGAIN;
+       }
+out:
+       btrfs_free_path(path);
+       if (ret == -EAGAIN) {
+               if (root->defrag_max.objectid > root->defrag_progress.objectid)
+                       goto done;
+               if (root->defrag_max.type > root->defrag_progress.type)
+                       goto done;
+               if (root->defrag_max.offset > root->defrag_progress.offset)
+                       goto done;
+               ret = 0;
+       }
+done:
+       if (ret != -EAGAIN)
+               memset(&root->defrag_progress, 0,
+                      sizeof(root->defrag_progress));
+
+       return ret;
+}
diff --git a/fs/btrfs/tree-defrag.c b/fs/btrfs/tree-defrag.c
deleted file mode 100644 (file)
index 5df6048..0000000
+++ /dev/null
@@ -1,133 +0,0 @@
-// SPDX-License-Identifier: GPL-2.0
-/*
- * Copyright (C) 2007 Oracle.  All rights reserved.
- */
-
-#include <linux/sched.h>
-#include "ctree.h"
-#include "disk-io.h"
-#include "print-tree.h"
-#include "transaction.h"
-#include "locking.h"
-#include "accessors.h"
-
-/*
- * Defrag all the leaves in a given btree.
- * 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)
-{
-       struct btrfs_path *path = NULL;
-       struct btrfs_key key;
-       int ret = 0;
-       int wret;
-       int level;
-       int next_key_ret = 0;
-       u64 last_ret = 0;
-
-       if (!test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
-               goto out;
-
-       path = btrfs_alloc_path();
-       if (!path)
-               return -ENOMEM;
-
-       level = btrfs_header_level(root->node);
-
-       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);
-               nritems = btrfs_header_nritems(root_node);
-               root->defrag_max.objectid = 0;
-               /* from above we know this is not a leaf */
-               btrfs_node_key_to_cpu(root_node, &root->defrag_max,
-                                     nritems - 1);
-               btrfs_tree_unlock(root_node);
-               free_extent_buffer(root_node);
-               memset(&key, 0, sizeof(key));
-       } else {
-               memcpy(&key, &root->defrag_progress, sizeof(key));
-       }
-
-       path->keep_locks = 1;
-
-       ret = btrfs_search_forward(root, &key, path, BTRFS_OLDEST_GENERATION);
-       if (ret < 0)
-               goto out;
-       if (ret > 0) {
-               ret = 0;
-               goto out;
-       }
-       btrfs_release_path(path);
-       /*
-        * We don't need a lock on a leaf. btrfs_realloc_node() will lock all
-        * leafs from path->nodes[1], so set lowest_level to 1 to avoid later
-        * a deadlock (attempting to write lock an already write locked leaf).
-        */
-       path->lowest_level = 1;
-       wret = btrfs_search_slot(trans, root, &key, path, 0, 1);
-
-       if (wret < 0) {
-               ret = wret;
-               goto out;
-       }
-       if (!path->nodes[1]) {
-               ret = 0;
-               goto out;
-       }
-       /*
-        * The node at level 1 must always be locked when our path has
-        * keep_locks set and lowest_level is 1, regardless of the value of
-        * path->slots[1].
-        */
-       BUG_ON(path->locks[1] == 0);
-       ret = btrfs_realloc_node(trans, root,
-                                path->nodes[1], 0,
-                                &last_ret,
-                                &root->defrag_progress);
-       if (ret) {
-               WARN_ON(ret == -EAGAIN);
-               goto out;
-       }
-       /*
-        * Now that we reallocated the node we can find the next key. Note that
-        * btrfs_find_next_key() can release our path and do another search
-        * without COWing, this is because even with path->keep_locks = 1,
-        * btrfs_search_slot() / ctree.c:unlock_up() does not keeps a lock on a
-        * node when path->slots[node_level - 1] does not point to the last
-        * item or a slot beyond the last item (ctree.c:unlock_up()). Therefore
-        * we search for the next key after reallocating our node.
-        */
-       path->slots[1] = btrfs_header_nritems(path->nodes[1]);
-       next_key_ret = btrfs_find_next_key(root, path, &key, 1,
-                                          BTRFS_OLDEST_GENERATION);
-       if (next_key_ret == 0) {
-               memcpy(&root->defrag_progress, &key, sizeof(key));
-               ret = -EAGAIN;
-       }
-out:
-       btrfs_free_path(path);
-       if (ret == -EAGAIN) {
-               if (root->defrag_max.objectid > root->defrag_progress.objectid)
-                       goto done;
-               if (root->defrag_max.type > root->defrag_progress.type)
-                       goto done;
-               if (root->defrag_max.offset > root->defrag_progress.offset)
-                       goto done;
-               ret = 0;
-       }
-done:
-       if (ret != -EAGAIN)
-               memset(&root->defrag_progress, 0,
-                      sizeof(root->defrag_progress));
-
-       return ret;
-}