2 * Copyright (C) 2007 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
19 #include <linux/sched.h>
22 #include "print-tree.h"
23 #include "transaction.h"
25 static void reada_defrag(struct btrfs_root *root,
26 struct extent_buffer *node)
34 blocksize = btrfs_level_size(root, btrfs_header_level(node) - 1);
35 nritems = btrfs_header_nritems(node);
36 for (i = 0; i < nritems; i++) {
37 bytenr = btrfs_node_blockptr(node, i);
38 ret = readahead_tree_block(root, bytenr, blocksize);
44 static int defrag_walk_down(struct btrfs_trans_handle *trans,
45 struct btrfs_root *root,
46 struct btrfs_path *path, int *level,
47 int cache_only, u64 *last_ret)
49 struct extent_buffer *next;
50 struct extent_buffer *cur;
56 WARN_ON(*level >= BTRFS_MAX_LEVEL);
58 if (root->fs_info->extent_root == root)
61 if (*level == 1 && cache_only && path->nodes[1] &&
62 !btrfs_buffer_defrag(path->nodes[1])) {
67 WARN_ON(*level >= BTRFS_MAX_LEVEL);
68 cur = path->nodes[*level];
70 if (!cache_only && *level > 1 && path->slots[*level] == 0)
71 reada_defrag(root, cur);
73 if (btrfs_header_level(cur) != *level)
76 if (path->slots[*level] >=
77 btrfs_header_nritems(cur))
81 WARN_ON(btrfs_header_generation(path->nodes[*level]) !=
83 ret = btrfs_realloc_node(trans, root,
87 &root->defrag_progress);
89 btrfs_extent_post_op(trans, root);
93 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
96 next = btrfs_find_tree_block(root, bytenr,
97 btrfs_level_size(root, *level - 1));
98 if (!next || !btrfs_buffer_uptodate(next) ||
99 !btrfs_buffer_defrag(next)) {
100 free_extent_buffer(next);
101 path->slots[*level]++;
104 btrfs_verify_block_csum(root, next);
106 next = read_tree_block(root, bytenr,
107 btrfs_level_size(root, *level - 1));
109 ret = btrfs_cow_block(trans, root, next, path->nodes[*level],
110 path->slots[*level], &next);
113 btrfs_extent_post_op(trans, root);
115 WARN_ON(*level <= 0);
116 if (path->nodes[*level-1])
117 free_extent_buffer(path->nodes[*level-1]);
118 path->nodes[*level-1] = next;
119 *level = btrfs_header_level(next);
120 path->slots[*level] = 0;
123 WARN_ON(*level >= BTRFS_MAX_LEVEL);
125 btrfs_clear_buffer_defrag(path->nodes[*level]);
127 free_extent_buffer(path->nodes[*level]);
128 path->nodes[*level] = NULL;
130 WARN_ON(ret && ret != -EAGAIN);
134 static int defrag_walk_up(struct btrfs_trans_handle *trans,
135 struct btrfs_root *root,
136 struct btrfs_path *path, int *level,
141 struct extent_buffer *node;
143 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
144 slot = path->slots[i];
145 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
148 node = path->nodes[i];
150 btrfs_node_key_to_cpu(node, &root->defrag_progress,
152 root->defrag_level = i;
155 btrfs_clear_buffer_defrag(path->nodes[*level]);
156 free_extent_buffer(path->nodes[*level]);
157 path->nodes[*level] = NULL;
164 int btrfs_defrag_leaves(struct btrfs_trans_handle *trans,
165 struct btrfs_root *root, int cache_only)
167 struct btrfs_path *path = NULL;
168 struct extent_buffer *tmp;
177 if (root->fs_info->extent_root == root)
180 if (root->ref_cows == 0 && !is_extent)
183 if (btrfs_test_opt(root, SSD))
186 path = btrfs_alloc_path();
190 level = btrfs_header_level(root->node);
196 if (root->defrag_progress.objectid == 0) {
197 extent_buffer_get(root->node);
198 ret = btrfs_cow_block(trans, root, root->node, NULL, 0, &tmp);
200 path->nodes[level] = root->node;
201 path->slots[level] = 0;
203 btrfs_extent_post_op(trans, root);
205 level = root->defrag_level;
206 path->lowest_level = level;
207 wret = btrfs_search_slot(trans, root, &root->defrag_progress,
211 btrfs_extent_post_op(trans, root);
218 while(level > 0 && !path->nodes[level])
221 if (!path->nodes[level]) {
228 wret = defrag_walk_down(trans, root, path, &level, cache_only,
235 wret = defrag_walk_up(trans, root, path, &level, cache_only);
244 for (i = 0; i <= orig_level; i++) {
245 if (path->nodes[i]) {
246 free_extent_buffer(path->nodes[i]);
247 path->nodes[i] = NULL;
252 btrfs_free_path(path);
253 if (ret != -EAGAIN) {
254 memset(&root->defrag_progress, 0,
255 sizeof(root->defrag_progress));