diff options
author | Chris Mason <chris.mason@oracle.com> | 2007-10-15 16:22:39 -0400 |
---|---|---|
committer | Chris Mason <chris.mason@oracle.com> | 2008-09-25 11:03:57 -0400 |
commit | a6b6e75e096f436f0cc56edf5bca96301e194491 (patch) | |
tree | 03751594c29a7d70d317689fd6b5e44ee023633e | |
parent | ff79f8190b6e955ff7a71faf804a3017d526e657 (diff) |
Btrfs: Defrag only leaves, and only when the parent node has a single objectid
This allows us to defrag huge directories, but skip the expensive defrag
case in more common usage, where it does not help as much.
Signed-off-by: Chris Mason <chris.mason@oracle.com>
-rw-r--r-- | fs/btrfs/ctree.c | 22 | ||||
-rw-r--r-- | fs/btrfs/ctree.h | 3 | ||||
-rw-r--r-- | fs/btrfs/transaction.c | 2 | ||||
-rw-r--r-- | fs/btrfs/tree-defrag.c | 36 |
4 files changed, 23 insertions, 40 deletions
diff --git a/fs/btrfs/ctree.c b/fs/btrfs/ctree.c index 984f4745440e..74fec6b83a8b 100644 --- a/fs/btrfs/ctree.c +++ b/fs/btrfs/ctree.c | |||
@@ -16,6 +16,7 @@ | |||
16 | * Boston, MA 021110-1307, USA. | 16 | * Boston, MA 021110-1307, USA. |
17 | */ | 17 | */ |
18 | 18 | ||
19 | #include <linux/sched.h> | ||
19 | #include "ctree.h" | 20 | #include "ctree.h" |
20 | #include "disk-io.h" | 21 | #include "disk-io.h" |
21 | #include "transaction.h" | 22 | #include "transaction.h" |
@@ -190,7 +191,8 @@ static int should_defrag_leaf(struct extent_buffer *leaf) | |||
190 | 191 | ||
191 | int btrfs_realloc_node(struct btrfs_trans_handle *trans, | 192 | int btrfs_realloc_node(struct btrfs_trans_handle *trans, |
192 | struct btrfs_root *root, struct extent_buffer *parent, | 193 | struct btrfs_root *root, struct extent_buffer *parent, |
193 | int cache_only, u64 *last_ret) | 194 | int start_slot, int cache_only, u64 *last_ret, |
195 | struct btrfs_key *progress) | ||
194 | { | 196 | { |
195 | struct extent_buffer *cur; | 197 | struct extent_buffer *cur; |
196 | struct extent_buffer *tmp; | 198 | struct extent_buffer *tmp; |
@@ -199,7 +201,6 @@ int btrfs_realloc_node(struct btrfs_trans_handle *trans, | |||
199 | u64 last_block = 0; | 201 | u64 last_block = 0; |
200 | u64 other; | 202 | u64 other; |
201 | u32 parent_nritems; | 203 | u32 parent_nritems; |
202 | int start_slot; | ||
203 | int end_slot; | 204 | int end_slot; |
204 | int i; | 205 | int i; |
205 | int err = 0; | 206 | int err = 0; |
@@ -221,15 +222,24 @@ int btrfs_realloc_node(struct btrfs_trans_handle *trans, | |||
221 | 222 | ||
222 | parent_nritems = btrfs_header_nritems(parent); | 223 | parent_nritems = btrfs_header_nritems(parent); |
223 | blocksize = btrfs_level_size(root, parent_level - 1); | 224 | blocksize = btrfs_level_size(root, parent_level - 1); |
224 | |||
225 | start_slot = 0; | ||
226 | end_slot = parent_nritems; | 225 | end_slot = parent_nritems; |
227 | 226 | ||
228 | if (parent_nritems == 1) | 227 | if (parent_nritems == 1) |
229 | return 0; | 228 | return 0; |
230 | 229 | ||
230 | if (root != root->fs_info->extent_root) { | ||
231 | struct btrfs_key first_key; | ||
232 | struct btrfs_key last_key; | ||
233 | |||
234 | btrfs_node_key_to_cpu(parent, &first_key, 0); | ||
235 | btrfs_node_key_to_cpu(parent, &last_key, parent_nritems - 1); | ||
236 | if (first_key.objectid != last_key.objectid) | ||
237 | return 0; | ||
238 | } | ||
239 | |||
231 | for (i = start_slot; i < end_slot; i++) { | 240 | for (i = start_slot; i < end_slot; i++) { |
232 | int close = 1; | 241 | int close = 1; |
242 | |||
233 | blocknr = btrfs_node_blockptr(parent, i); | 243 | blocknr = btrfs_node_blockptr(parent, i); |
234 | if (last_block == 0) | 244 | if (last_block == 0) |
235 | last_block = blocknr; | 245 | last_block = blocknr; |
@@ -898,7 +908,7 @@ static void reada_for_search(struct btrfs_root *root, struct btrfs_path *path, | |||
898 | u32 blocksize; | 908 | u32 blocksize; |
899 | u32 nscan = 0; | 909 | u32 nscan = 0; |
900 | 910 | ||
901 | if (level == 0) | 911 | if (level != 1) |
902 | return; | 912 | return; |
903 | 913 | ||
904 | if (!path->nodes[level]) | 914 | if (!path->nodes[level]) |
@@ -2370,7 +2380,7 @@ int btrfs_del_item(struct btrfs_trans_handle *trans, struct btrfs_root *root, | |||
2370 | } | 2380 | } |
2371 | 2381 | ||
2372 | /* delete the leaf if it is mostly empty */ | 2382 | /* delete the leaf if it is mostly empty */ |
2373 | if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) { | 2383 | if (0 && used < BTRFS_LEAF_DATA_SIZE(root) / 3) { |
2374 | /* push_leaf_left fixes the path. | 2384 | /* push_leaf_left fixes the path. |
2375 | * make sure the path still points to our leaf | 2385 | * make sure the path still points to our leaf |
2376 | * for possible call to del_ptr below | 2386 | * for possible call to del_ptr below |
diff --git a/fs/btrfs/ctree.h b/fs/btrfs/ctree.h index 1af0a966f85b..37bccb1a9a75 100644 --- a/fs/btrfs/ctree.h +++ b/fs/btrfs/ctree.h | |||
@@ -913,7 +913,8 @@ int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root | |||
913 | ins_len, int cow); | 913 | ins_len, int cow); |
914 | int btrfs_realloc_node(struct btrfs_trans_handle *trans, | 914 | int btrfs_realloc_node(struct btrfs_trans_handle *trans, |
915 | struct btrfs_root *root, struct extent_buffer *parent, | 915 | struct btrfs_root *root, struct extent_buffer *parent, |
916 | int cache_only, u64 *last_ret); | 916 | int start_slot, int cache_only, u64 *last_ret, |
917 | struct btrfs_key *progress); | ||
917 | void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p); | 918 | void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p); |
918 | struct btrfs_path *btrfs_alloc_path(void); | 919 | struct btrfs_path *btrfs_alloc_path(void); |
919 | void btrfs_free_path(struct btrfs_path *p); | 920 | void btrfs_free_path(struct btrfs_path *p); |
diff --git a/fs/btrfs/transaction.c b/fs/btrfs/transaction.c index 67e4aca36a62..bdfe05cf26c6 100644 --- a/fs/btrfs/transaction.c +++ b/fs/btrfs/transaction.c | |||
@@ -353,7 +353,6 @@ int btrfs_defrag_root(struct btrfs_root *root, int cacheonly) | |||
353 | 353 | ||
354 | if (root->defrag_running) | 354 | if (root->defrag_running) |
355 | return 0; | 355 | return 0; |
356 | |||
357 | trans = btrfs_start_transaction(root, 1); | 356 | trans = btrfs_start_transaction(root, 1); |
358 | while (1) { | 357 | while (1) { |
359 | root->defrag_running = 1; | 358 | root->defrag_running = 1; |
@@ -361,7 +360,6 @@ int btrfs_defrag_root(struct btrfs_root *root, int cacheonly) | |||
361 | nr = trans->blocks_used; | 360 | nr = trans->blocks_used; |
362 | btrfs_end_transaction(trans, root); | 361 | btrfs_end_transaction(trans, root); |
363 | mutex_unlock(&info->fs_mutex); | 362 | mutex_unlock(&info->fs_mutex); |
364 | |||
365 | btrfs_btree_balance_dirty(info->tree_root, nr); | 363 | btrfs_btree_balance_dirty(info->tree_root, nr); |
366 | cond_resched(); | 364 | cond_resched(); |
367 | 365 | ||
diff --git a/fs/btrfs/tree-defrag.c b/fs/btrfs/tree-defrag.c index f86eccf511b9..b02355a7b143 100644 --- a/fs/btrfs/tree-defrag.c +++ b/fs/btrfs/tree-defrag.c | |||
@@ -76,7 +76,9 @@ static int defrag_walk_down(struct btrfs_trans_handle *trans, | |||
76 | if (*level == 1) { | 76 | if (*level == 1) { |
77 | ret = btrfs_realloc_node(trans, root, | 77 | ret = btrfs_realloc_node(trans, root, |
78 | path->nodes[*level], | 78 | path->nodes[*level], |
79 | cache_only, last_ret); | 79 | path->slots[*level], |
80 | cache_only, last_ret, | ||
81 | &root->defrag_progress); | ||
80 | if (is_extent) | 82 | if (is_extent) |
81 | btrfs_extent_post_op(trans, root); | 83 | btrfs_extent_post_op(trans, root); |
82 | 84 | ||
@@ -100,10 +102,6 @@ static int defrag_walk_down(struct btrfs_trans_handle *trans, | |||
100 | ret = btrfs_cow_block(trans, root, next, path->nodes[*level], | 102 | ret = btrfs_cow_block(trans, root, next, path->nodes[*level], |
101 | path->slots[*level], &next); | 103 | path->slots[*level], &next); |
102 | BUG_ON(ret); | 104 | BUG_ON(ret); |
103 | ret = btrfs_realloc_node(trans, root, next, cache_only, | ||
104 | last_ret); | ||
105 | BUG_ON(ret); | ||
106 | |||
107 | if (is_extent) | 105 | if (is_extent) |
108 | btrfs_extent_post_op(trans, root); | 106 | btrfs_extent_post_op(trans, root); |
109 | 107 | ||
@@ -122,8 +120,8 @@ static int defrag_walk_down(struct btrfs_trans_handle *trans, | |||
122 | free_extent_buffer(path->nodes[*level]); | 120 | free_extent_buffer(path->nodes[*level]); |
123 | path->nodes[*level] = NULL; | 121 | path->nodes[*level] = NULL; |
124 | *level += 1; | 122 | *level += 1; |
125 | WARN_ON(ret); | 123 | WARN_ON(ret && ret != -EAGAIN); |
126 | return 0; | 124 | return ret; |
127 | } | 125 | } |
128 | 126 | ||
129 | static int defrag_walk_up(struct btrfs_trans_handle *trans, | 127 | static int defrag_walk_up(struct btrfs_trans_handle *trans, |
@@ -147,27 +145,6 @@ static int defrag_walk_up(struct btrfs_trans_handle *trans, | |||
147 | root->defrag_level = i; | 145 | root->defrag_level = i; |
148 | return 0; | 146 | return 0; |
149 | } else { | 147 | } else { |
150 | if (*level > 1 && path->nodes[*level] != root->node && | ||
151 | btrfs_buffer_defrag(path->nodes[*level])) { | ||
152 | struct extent_buffer *next; | ||
153 | u64 last; | ||
154 | int ret; | ||
155 | ret = btrfs_cow_block(trans, root, | ||
156 | path->nodes[*level], | ||
157 | path->nodes[*level + 1], | ||
158 | path->slots[*level + 1], | ||
159 | &next); | ||
160 | BUG_ON(ret); | ||
161 | path->nodes[*level] = next; | ||
162 | last = next->start; | ||
163 | ret = btrfs_realloc_node(trans, root, next, | ||
164 | cache_only, &last); | ||
165 | BUG_ON(ret); | ||
166 | |||
167 | if (root == root->fs_info->extent_root) | ||
168 | btrfs_extent_post_op(trans, root); | ||
169 | } | ||
170 | |||
171 | btrfs_clear_buffer_defrag(path->nodes[*level]); | 148 | btrfs_clear_buffer_defrag(path->nodes[*level]); |
172 | free_extent_buffer(path->nodes[*level]); | 149 | free_extent_buffer(path->nodes[*level]); |
173 | path->nodes[*level] = NULL; | 150 | path->nodes[*level] = NULL; |
@@ -211,9 +188,6 @@ int btrfs_defrag_leaves(struct btrfs_trans_handle *trans, | |||
211 | extent_buffer_get(root->node); | 188 | extent_buffer_get(root->node); |
212 | ret = btrfs_cow_block(trans, root, root->node, NULL, 0, &tmp); | 189 | ret = btrfs_cow_block(trans, root, root->node, NULL, 0, &tmp); |
213 | BUG_ON(ret); | 190 | BUG_ON(ret); |
214 | ret = btrfs_realloc_node(trans, root, root->node, cache_only, | ||
215 | &last_ret); | ||
216 | BUG_ON(ret); | ||
217 | path->nodes[level] = root->node; | 191 | path->nodes[level] = root->node; |
218 | path->slots[level] = 0; | 192 | path->slots[level] = 0; |
219 | if (is_extent) | 193 | if (is_extent) |