blob: 5c58630dce03e12800e2c3cf52575d65e8d8c96a [file] [log] [blame]
Chris Mason6702ed42007-08-07 16:15:09 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
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.
7 *
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.
12 *
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.
17 */
18
19#include <linux/sched.h>
20#include "ctree.h"
21#include "disk-io.h"
22#include "print-tree.h"
23#include "transaction.h"
24
25static void reada_defrag(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -040026 struct extent_buffer *node)
Chris Mason6702ed42007-08-07 16:15:09 -040027{
28 int i;
29 u32 nritems;
Chris Masondb945352007-10-15 16:15:53 -040030 u64 bytenr;
31 u32 blocksize;
Chris Mason6702ed42007-08-07 16:15:09 -040032 int ret;
33
Chris Masondb945352007-10-15 16:15:53 -040034 blocksize = btrfs_level_size(root, btrfs_header_level(node) - 1);
Chris Mason5f39d392007-10-15 16:14:19 -040035 nritems = btrfs_header_nritems(node);
Chris Mason6702ed42007-08-07 16:15:09 -040036 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -040037 bytenr = btrfs_node_blockptr(node, i);
38 ret = readahead_tree_block(root, bytenr, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -040039 if (ret)
40 break;
41 }
42}
43
44static int defrag_walk_down(struct btrfs_trans_handle *trans,
45 struct btrfs_root *root,
46 struct btrfs_path *path, int *level,
Chris Masone9d0b132007-08-10 14:06:19 -040047 int cache_only, u64 *last_ret)
Chris Mason6702ed42007-08-07 16:15:09 -040048{
Chris Mason5f39d392007-10-15 16:14:19 -040049 struct extent_buffer *next;
50 struct extent_buffer *cur;
Chris Masondb945352007-10-15 16:15:53 -040051 u64 bytenr;
Chris Mason6702ed42007-08-07 16:15:09 -040052 int ret = 0;
Chris Masone9d0b132007-08-10 14:06:19 -040053 int is_extent = 0;
Chris Mason6702ed42007-08-07 16:15:09 -040054
55 WARN_ON(*level < 0);
56 WARN_ON(*level >= BTRFS_MAX_LEVEL);
57
Chris Masone9d0b132007-08-10 14:06:19 -040058 if (root->fs_info->extent_root == root)
59 is_extent = 1;
60
Chris Masonf84a8b32007-11-06 10:26:29 -050061 if (*level == 1 && cache_only && path->nodes[1] &&
62 !btrfs_buffer_defrag(path->nodes[1])) {
63 goto out;
64 }
Chris Mason6702ed42007-08-07 16:15:09 -040065 while(*level > 0) {
66 WARN_ON(*level < 0);
67 WARN_ON(*level >= BTRFS_MAX_LEVEL);
68 cur = path->nodes[*level];
69
70 if (!cache_only && *level > 1 && path->slots[*level] == 0)
Chris Mason5f39d392007-10-15 16:14:19 -040071 reada_defrag(root, cur);
Chris Mason6702ed42007-08-07 16:15:09 -040072
Chris Mason5f39d392007-10-15 16:14:19 -040073 if (btrfs_header_level(cur) != *level)
Chris Mason6702ed42007-08-07 16:15:09 -040074 WARN_ON(1);
75
76 if (path->slots[*level] >=
Chris Mason5f39d392007-10-15 16:14:19 -040077 btrfs_header_nritems(cur))
Chris Mason6702ed42007-08-07 16:15:09 -040078 break;
79
80 if (*level == 1) {
Chris Mason7bb86312007-12-11 09:25:06 -050081 WARN_ON(btrfs_header_generation(path->nodes[*level]) !=
82 trans->transid);
Chris Mason6702ed42007-08-07 16:15:09 -040083 ret = btrfs_realloc_node(trans, root,
84 path->nodes[*level],
Chris Masona6b6e752007-10-15 16:22:39 -040085 path->slots[*level],
86 cache_only, last_ret,
87 &root->defrag_progress);
Chris Masone9d0b132007-08-10 14:06:19 -040088 if (is_extent)
89 btrfs_extent_post_op(trans, root);
90
Chris Mason6702ed42007-08-07 16:15:09 -040091 break;
92 }
Chris Masondb945352007-10-15 16:15:53 -040093 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
Chris Mason6702ed42007-08-07 16:15:09 -040094
95 if (cache_only) {
Chris Masondb945352007-10-15 16:15:53 -040096 next = btrfs_find_tree_block(root, bytenr,
97 btrfs_level_size(root, *level - 1));
Chris Masoncf786e72007-10-15 16:22:11 -040098 if (!next || !btrfs_buffer_uptodate(next) ||
99 !btrfs_buffer_defrag(next)) {
Chris Mason5f39d392007-10-15 16:14:19 -0400100 free_extent_buffer(next);
Chris Mason6702ed42007-08-07 16:15:09 -0400101 path->slots[*level]++;
102 continue;
103 }
104 } else {
Chris Masondb945352007-10-15 16:15:53 -0400105 next = read_tree_block(root, bytenr,
106 btrfs_level_size(root, *level - 1));
Chris Mason6702ed42007-08-07 16:15:09 -0400107 }
108 ret = btrfs_cow_block(trans, root, next, path->nodes[*level],
109 path->slots[*level], &next);
110 BUG_ON(ret);
Chris Masone9d0b132007-08-10 14:06:19 -0400111 if (is_extent)
112 btrfs_extent_post_op(trans, root);
113
Chris Mason6702ed42007-08-07 16:15:09 -0400114 WARN_ON(*level <= 0);
115 if (path->nodes[*level-1])
Chris Mason5f39d392007-10-15 16:14:19 -0400116 free_extent_buffer(path->nodes[*level-1]);
Chris Mason6702ed42007-08-07 16:15:09 -0400117 path->nodes[*level-1] = next;
Chris Mason5f39d392007-10-15 16:14:19 -0400118 *level = btrfs_header_level(next);
Chris Mason6702ed42007-08-07 16:15:09 -0400119 path->slots[*level] = 0;
120 }
121 WARN_ON(*level < 0);
122 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason6b800532007-10-15 16:17:34 -0400123
124 btrfs_clear_buffer_defrag(path->nodes[*level]);
Chris Masonf84a8b32007-11-06 10:26:29 -0500125out:
Chris Mason5f39d392007-10-15 16:14:19 -0400126 free_extent_buffer(path->nodes[*level]);
Chris Mason6702ed42007-08-07 16:15:09 -0400127 path->nodes[*level] = NULL;
128 *level += 1;
Chris Masona6b6e752007-10-15 16:22:39 -0400129 WARN_ON(ret && ret != -EAGAIN);
130 return ret;
Chris Mason6702ed42007-08-07 16:15:09 -0400131}
132
133static int defrag_walk_up(struct btrfs_trans_handle *trans,
134 struct btrfs_root *root,
135 struct btrfs_path *path, int *level,
136 int cache_only)
137{
138 int i;
139 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -0400140 struct extent_buffer *node;
Chris Mason6702ed42007-08-07 16:15:09 -0400141
142 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
143 slot = path->slots[i];
Chris Mason5f39d392007-10-15 16:14:19 -0400144 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
Chris Mason6702ed42007-08-07 16:15:09 -0400145 path->slots[i]++;
146 *level = i;
Chris Mason5f39d392007-10-15 16:14:19 -0400147 node = path->nodes[i];
Chris Mason6702ed42007-08-07 16:15:09 -0400148 WARN_ON(i == 0);
Chris Mason5f39d392007-10-15 16:14:19 -0400149 btrfs_node_key_to_cpu(node, &root->defrag_progress,
150 path->slots[i]);
Chris Mason6702ed42007-08-07 16:15:09 -0400151 root->defrag_level = i;
152 return 0;
153 } else {
Chris Mason6b800532007-10-15 16:17:34 -0400154 btrfs_clear_buffer_defrag(path->nodes[*level]);
Chris Mason5f39d392007-10-15 16:14:19 -0400155 free_extent_buffer(path->nodes[*level]);
Chris Mason6702ed42007-08-07 16:15:09 -0400156 path->nodes[*level] = NULL;
157 *level = i + 1;
158 }
159 }
160 return 1;
161}
162
163int btrfs_defrag_leaves(struct btrfs_trans_handle *trans,
164 struct btrfs_root *root, int cache_only)
165{
166 struct btrfs_path *path = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400167 struct extent_buffer *tmp;
Chris Mason6702ed42007-08-07 16:15:09 -0400168 int ret = 0;
169 int wret;
170 int level;
171 int orig_level;
172 int i;
Chris Masone9d0b132007-08-10 14:06:19 -0400173 int is_extent = 0;
174 u64 last_ret = 0;
Chris Mason6702ed42007-08-07 16:15:09 -0400175
Chris Masone9d0b132007-08-10 14:06:19 -0400176 if (root->fs_info->extent_root == root)
177 is_extent = 1;
178
179 if (root->ref_cows == 0 && !is_extent)
Chris Mason6702ed42007-08-07 16:15:09 -0400180 goto out;
Chris Mason5f39d392007-10-15 16:14:19 -0400181
Chris Mason6702ed42007-08-07 16:15:09 -0400182 path = btrfs_alloc_path();
183 if (!path)
184 return -ENOMEM;
185
Chris Mason5f39d392007-10-15 16:14:19 -0400186 level = btrfs_header_level(root->node);
Chris Mason6702ed42007-08-07 16:15:09 -0400187 orig_level = level;
Chris Mason0f1ebbd2007-10-15 16:19:46 -0400188
Chris Mason6702ed42007-08-07 16:15:09 -0400189 if (level == 0) {
190 goto out;
191 }
192 if (root->defrag_progress.objectid == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -0400193 extent_buffer_get(root->node);
Chris Mason6702ed42007-08-07 16:15:09 -0400194 ret = btrfs_cow_block(trans, root, root->node, NULL, 0, &tmp);
195 BUG_ON(ret);
Chris Mason6702ed42007-08-07 16:15:09 -0400196 path->nodes[level] = root->node;
197 path->slots[level] = 0;
Chris Masone9d0b132007-08-10 14:06:19 -0400198 if (is_extent)
199 btrfs_extent_post_op(trans, root);
Chris Mason6702ed42007-08-07 16:15:09 -0400200 } else {
201 level = root->defrag_level;
202 path->lowest_level = level;
203 wret = btrfs_search_slot(trans, root, &root->defrag_progress,
204 path, 0, 1);
205
Chris Masone9d0b132007-08-10 14:06:19 -0400206 if (is_extent)
207 btrfs_extent_post_op(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -0400208
Chris Mason6702ed42007-08-07 16:15:09 -0400209 if (wret < 0) {
210 ret = wret;
211 goto out;
212 }
Chris Mason5f39d392007-10-15 16:14:19 -0400213
Chris Mason6702ed42007-08-07 16:15:09 -0400214 while(level > 0 && !path->nodes[level])
215 level--;
Chris Mason5f39d392007-10-15 16:14:19 -0400216
Chris Mason6702ed42007-08-07 16:15:09 -0400217 if (!path->nodes[level]) {
218 ret = 0;
219 goto out;
220 }
221 }
222
223 while(1) {
Chris Masone9d0b132007-08-10 14:06:19 -0400224 wret = defrag_walk_down(trans, root, path, &level, cache_only,
225 &last_ret);
Chris Mason6702ed42007-08-07 16:15:09 -0400226 if (wret > 0)
227 break;
228 if (wret < 0)
229 ret = wret;
230
231 wret = defrag_walk_up(trans, root, path, &level, cache_only);
232 if (wret > 0)
233 break;
234 if (wret < 0)
235 ret = wret;
Chris Mason081e9572007-11-06 10:26:24 -0500236 else
237 ret = -EAGAIN;
Chris Mason409eb952007-08-08 20:17:12 -0400238 break;
Chris Mason6702ed42007-08-07 16:15:09 -0400239 }
240 for (i = 0; i <= orig_level; i++) {
241 if (path->nodes[i]) {
Chris Mason5f39d392007-10-15 16:14:19 -0400242 free_extent_buffer(path->nodes[i]);
Chris Mason0f827312007-10-15 16:18:56 -0400243 path->nodes[i] = NULL;
Chris Mason6702ed42007-08-07 16:15:09 -0400244 }
245 }
246out:
247 if (path)
248 btrfs_free_path(path);
249 if (ret != -EAGAIN) {
250 memset(&root->defrag_progress, 0,
251 sizeof(root->defrag_progress));
252 }
253 return ret;
254}