blob: fab851d853836434a0541435b701548c4763f05f [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;
Chris Masonca7a79a2008-05-12 12:59:19 -040031 u64 gen;
Chris Masondb945352007-10-15 16:15:53 -040032 u32 blocksize;
Chris Mason6702ed42007-08-07 16:15:09 -040033 int ret;
34
Chris Masondb945352007-10-15 16:15:53 -040035 blocksize = btrfs_level_size(root, btrfs_header_level(node) - 1);
Chris Mason5f39d392007-10-15 16:14:19 -040036 nritems = btrfs_header_nritems(node);
Chris Mason6702ed42007-08-07 16:15:09 -040037 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -040038 bytenr = btrfs_node_blockptr(node, i);
Chris Masonca7a79a2008-05-12 12:59:19 -040039 gen = btrfs_node_ptr_generation(node, i);
40 ret = readahead_tree_block(root, bytenr, blocksize, gen);
Chris Mason6702ed42007-08-07 16:15:09 -040041 if (ret)
42 break;
43 }
44}
45
46static int defrag_walk_down(struct btrfs_trans_handle *trans,
47 struct btrfs_root *root,
48 struct btrfs_path *path, int *level,
Chris Masone9d0b132007-08-10 14:06:19 -040049 int cache_only, u64 *last_ret)
Chris Mason6702ed42007-08-07 16:15:09 -040050{
Chris Mason5f39d392007-10-15 16:14:19 -040051 struct extent_buffer *next;
52 struct extent_buffer *cur;
Chris Masondb945352007-10-15 16:15:53 -040053 u64 bytenr;
Chris Mason1259ab72008-05-12 13:39:03 -040054 u64 ptr_gen;
Chris Mason6702ed42007-08-07 16:15:09 -040055 int ret = 0;
Chris Masone9d0b132007-08-10 14:06:19 -040056 int is_extent = 0;
Chris Mason6702ed42007-08-07 16:15:09 -040057
58 WARN_ON(*level < 0);
59 WARN_ON(*level >= BTRFS_MAX_LEVEL);
60
Chris Masone9d0b132007-08-10 14:06:19 -040061 if (root->fs_info->extent_root == root)
62 is_extent = 1;
63
Chris Masonf84a8b32007-11-06 10:26:29 -050064 if (*level == 1 && cache_only && path->nodes[1] &&
65 !btrfs_buffer_defrag(path->nodes[1])) {
66 goto out;
67 }
Chris Mason6702ed42007-08-07 16:15:09 -040068 while(*level > 0) {
69 WARN_ON(*level < 0);
70 WARN_ON(*level >= BTRFS_MAX_LEVEL);
71 cur = path->nodes[*level];
72
73 if (!cache_only && *level > 1 && path->slots[*level] == 0)
Chris Mason5f39d392007-10-15 16:14:19 -040074 reada_defrag(root, cur);
Chris Mason6702ed42007-08-07 16:15:09 -040075
Chris Mason5f39d392007-10-15 16:14:19 -040076 if (btrfs_header_level(cur) != *level)
Chris Mason6702ed42007-08-07 16:15:09 -040077 WARN_ON(1);
78
79 if (path->slots[*level] >=
Chris Mason5f39d392007-10-15 16:14:19 -040080 btrfs_header_nritems(cur))
Chris Mason6702ed42007-08-07 16:15:09 -040081 break;
82
83 if (*level == 1) {
Chris Mason7bb86312007-12-11 09:25:06 -050084 WARN_ON(btrfs_header_generation(path->nodes[*level]) !=
85 trans->transid);
Chris Mason6702ed42007-08-07 16:15:09 -040086 ret = btrfs_realloc_node(trans, root,
87 path->nodes[*level],
Chris Masona6b6e752007-10-15 16:22:39 -040088 path->slots[*level],
89 cache_only, last_ret,
90 &root->defrag_progress);
Chris Masone9d0b132007-08-10 14:06:19 -040091 if (is_extent)
92 btrfs_extent_post_op(trans, root);
93
Chris Mason6702ed42007-08-07 16:15:09 -040094 break;
95 }
Chris Masondb945352007-10-15 16:15:53 -040096 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
Chris Mason1259ab72008-05-12 13:39:03 -040097 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
Chris Mason6702ed42007-08-07 16:15:09 -040098
99 if (cache_only) {
Chris Masondb945352007-10-15 16:15:53 -0400100 next = btrfs_find_tree_block(root, bytenr,
101 btrfs_level_size(root, *level - 1));
Chris Mason1259ab72008-05-12 13:39:03 -0400102 if (!next || !btrfs_buffer_uptodate(next, ptr_gen) ||
Chris Masoncf786e72007-10-15 16:22:11 -0400103 !btrfs_buffer_defrag(next)) {
Chris Mason5f39d392007-10-15 16:14:19 -0400104 free_extent_buffer(next);
Chris Mason6702ed42007-08-07 16:15:09 -0400105 path->slots[*level]++;
106 continue;
107 }
108 } else {
Chris Masondb945352007-10-15 16:15:53 -0400109 next = read_tree_block(root, bytenr,
Chris Masonca7a79a2008-05-12 12:59:19 -0400110 btrfs_level_size(root, *level - 1),
Chris Mason1259ab72008-05-12 13:39:03 -0400111 ptr_gen);
Chris Mason6702ed42007-08-07 16:15:09 -0400112 }
113 ret = btrfs_cow_block(trans, root, next, path->nodes[*level],
114 path->slots[*level], &next);
115 BUG_ON(ret);
Chris Masone9d0b132007-08-10 14:06:19 -0400116 if (is_extent)
117 btrfs_extent_post_op(trans, root);
118
Chris Mason6702ed42007-08-07 16:15:09 -0400119 WARN_ON(*level <= 0);
120 if (path->nodes[*level-1])
Chris Mason5f39d392007-10-15 16:14:19 -0400121 free_extent_buffer(path->nodes[*level-1]);
Chris Mason6702ed42007-08-07 16:15:09 -0400122 path->nodes[*level-1] = next;
Chris Mason5f39d392007-10-15 16:14:19 -0400123 *level = btrfs_header_level(next);
Chris Mason6702ed42007-08-07 16:15:09 -0400124 path->slots[*level] = 0;
125 }
126 WARN_ON(*level < 0);
127 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason6b800532007-10-15 16:17:34 -0400128
129 btrfs_clear_buffer_defrag(path->nodes[*level]);
Chris Masonf84a8b32007-11-06 10:26:29 -0500130out:
Chris Mason5f39d392007-10-15 16:14:19 -0400131 free_extent_buffer(path->nodes[*level]);
Chris Mason6702ed42007-08-07 16:15:09 -0400132 path->nodes[*level] = NULL;
133 *level += 1;
Chris Masona6b6e752007-10-15 16:22:39 -0400134 WARN_ON(ret && ret != -EAGAIN);
135 return ret;
Chris Mason6702ed42007-08-07 16:15:09 -0400136}
137
138static int defrag_walk_up(struct btrfs_trans_handle *trans,
139 struct btrfs_root *root,
140 struct btrfs_path *path, int *level,
141 int cache_only)
142{
143 int i;
144 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -0400145 struct extent_buffer *node;
Chris Mason6702ed42007-08-07 16:15:09 -0400146
147 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
148 slot = path->slots[i];
Chris Mason5f39d392007-10-15 16:14:19 -0400149 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
Chris Mason6702ed42007-08-07 16:15:09 -0400150 path->slots[i]++;
151 *level = i;
Chris Mason5f39d392007-10-15 16:14:19 -0400152 node = path->nodes[i];
Chris Mason6702ed42007-08-07 16:15:09 -0400153 WARN_ON(i == 0);
Chris Mason5f39d392007-10-15 16:14:19 -0400154 btrfs_node_key_to_cpu(node, &root->defrag_progress,
155 path->slots[i]);
Chris Mason6702ed42007-08-07 16:15:09 -0400156 root->defrag_level = i;
157 return 0;
158 } else {
Chris Mason6b800532007-10-15 16:17:34 -0400159 btrfs_clear_buffer_defrag(path->nodes[*level]);
Chris Mason5f39d392007-10-15 16:14:19 -0400160 free_extent_buffer(path->nodes[*level]);
Chris Mason6702ed42007-08-07 16:15:09 -0400161 path->nodes[*level] = NULL;
162 *level = i + 1;
163 }
164 }
165 return 1;
166}
167
168int btrfs_defrag_leaves(struct btrfs_trans_handle *trans,
169 struct btrfs_root *root, int cache_only)
170{
171 struct btrfs_path *path = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400172 struct extent_buffer *tmp;
Chris Mason6702ed42007-08-07 16:15:09 -0400173 int ret = 0;
174 int wret;
175 int level;
176 int orig_level;
177 int i;
Chris Masone9d0b132007-08-10 14:06:19 -0400178 int is_extent = 0;
179 u64 last_ret = 0;
Chris Mason6702ed42007-08-07 16:15:09 -0400180
Chris Masone9d0b132007-08-10 14:06:19 -0400181 if (root->fs_info->extent_root == root)
182 is_extent = 1;
183
Chris Mason925baed2008-06-25 16:01:30 -0400184 goto out;
185
Chris Masone9d0b132007-08-10 14:06:19 -0400186 if (root->ref_cows == 0 && !is_extent)
Chris Mason6702ed42007-08-07 16:15:09 -0400187 goto out;
Chris Mason5f39d392007-10-15 16:14:19 -0400188
Chris Mason9afbb0b2008-02-15 13:19:35 -0500189 if (btrfs_test_opt(root, SSD))
190 goto out;
191
Chris Mason6702ed42007-08-07 16:15:09 -0400192 path = btrfs_alloc_path();
193 if (!path)
194 return -ENOMEM;
195
Chris Mason5f39d392007-10-15 16:14:19 -0400196 level = btrfs_header_level(root->node);
Chris Mason6702ed42007-08-07 16:15:09 -0400197 orig_level = level;
Chris Mason0f1ebbd2007-10-15 16:19:46 -0400198
Chris Mason6702ed42007-08-07 16:15:09 -0400199 if (level == 0) {
200 goto out;
201 }
202 if (root->defrag_progress.objectid == 0) {
Chris Mason0ef3e662008-05-24 14:04:53 -0400203 u32 nritems;
204
205 nritems = btrfs_header_nritems(root->node);
206 root->defrag_max.objectid = 0;
207 /* from above we know this is not a leaf */
208 btrfs_node_key_to_cpu(root->node, &root->defrag_max,
209 nritems - 1);
Chris Mason5f39d392007-10-15 16:14:19 -0400210 extent_buffer_get(root->node);
Chris Mason6702ed42007-08-07 16:15:09 -0400211 ret = btrfs_cow_block(trans, root, root->node, NULL, 0, &tmp);
212 BUG_ON(ret);
Chris Mason6702ed42007-08-07 16:15:09 -0400213 path->nodes[level] = root->node;
214 path->slots[level] = 0;
Chris Masone9d0b132007-08-10 14:06:19 -0400215 if (is_extent)
216 btrfs_extent_post_op(trans, root);
Chris Mason6702ed42007-08-07 16:15:09 -0400217 } else {
218 level = root->defrag_level;
219 path->lowest_level = level;
220 wret = btrfs_search_slot(trans, root, &root->defrag_progress,
221 path, 0, 1);
222
Chris Masone9d0b132007-08-10 14:06:19 -0400223 if (is_extent)
224 btrfs_extent_post_op(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -0400225
Chris Mason6702ed42007-08-07 16:15:09 -0400226 if (wret < 0) {
227 ret = wret;
228 goto out;
229 }
Chris Mason5f39d392007-10-15 16:14:19 -0400230
Chris Mason6702ed42007-08-07 16:15:09 -0400231 while(level > 0 && !path->nodes[level])
232 level--;
Chris Mason5f39d392007-10-15 16:14:19 -0400233
Chris Mason6702ed42007-08-07 16:15:09 -0400234 if (!path->nodes[level]) {
235 ret = 0;
236 goto out;
237 }
238 }
239
240 while(1) {
Chris Masone9d0b132007-08-10 14:06:19 -0400241 wret = defrag_walk_down(trans, root, path, &level, cache_only,
242 &last_ret);
Chris Mason6702ed42007-08-07 16:15:09 -0400243 if (wret > 0)
244 break;
245 if (wret < 0)
246 ret = wret;
247
248 wret = defrag_walk_up(trans, root, path, &level, cache_only);
249 if (wret > 0)
250 break;
251 if (wret < 0)
252 ret = wret;
Chris Mason081e9572007-11-06 10:26:24 -0500253 else
254 ret = -EAGAIN;
Chris Mason409eb952007-08-08 20:17:12 -0400255 break;
Chris Mason6702ed42007-08-07 16:15:09 -0400256 }
257 for (i = 0; i <= orig_level; i++) {
258 if (path->nodes[i]) {
Chris Mason5f39d392007-10-15 16:14:19 -0400259 free_extent_buffer(path->nodes[i]);
Chris Mason0f827312007-10-15 16:18:56 -0400260 path->nodes[i] = NULL;
Chris Mason6702ed42007-08-07 16:15:09 -0400261 }
262 }
263out:
264 if (path)
265 btrfs_free_path(path);
Chris Mason0ef3e662008-05-24 14:04:53 -0400266 if (ret == -EAGAIN) {
267 if (root->defrag_max.objectid > root->defrag_progress.objectid)
268 goto done;
269 if (root->defrag_max.type > root->defrag_progress.type)
270 goto done;
271 if (root->defrag_max.offset > root->defrag_progress.offset)
272 goto done;
273 ret = 0;
274 }
275done:
Chris Mason6702ed42007-08-07 16:15:09 -0400276 if (ret != -EAGAIN) {
277 memset(&root->defrag_progress, 0,
278 sizeof(root->defrag_progress));
279 }
280 return ret;
281}