blob: 155961c7b4d55784c56d3c043a3ca1db0b552aaa [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
184 if (root->ref_cows == 0 && !is_extent)
Chris Mason6702ed42007-08-07 16:15:09 -0400185 goto out;
Chris Mason5f39d392007-10-15 16:14:19 -0400186
Chris Mason9afbb0b2008-02-15 13:19:35 -0500187 if (btrfs_test_opt(root, SSD))
188 goto out;
189
Chris Mason6702ed42007-08-07 16:15:09 -0400190 path = btrfs_alloc_path();
191 if (!path)
192 return -ENOMEM;
193
Chris Mason5f39d392007-10-15 16:14:19 -0400194 level = btrfs_header_level(root->node);
Chris Mason6702ed42007-08-07 16:15:09 -0400195 orig_level = level;
Chris Mason0f1ebbd2007-10-15 16:19:46 -0400196
Chris Mason6702ed42007-08-07 16:15:09 -0400197 if (level == 0) {
198 goto out;
199 }
200 if (root->defrag_progress.objectid == 0) {
Chris Mason0ef3e662008-05-24 14:04:53 -0400201 u32 nritems;
202
203 nritems = btrfs_header_nritems(root->node);
204 root->defrag_max.objectid = 0;
205 /* from above we know this is not a leaf */
206 btrfs_node_key_to_cpu(root->node, &root->defrag_max,
207 nritems - 1);
Chris Mason5f39d392007-10-15 16:14:19 -0400208 extent_buffer_get(root->node);
Chris Mason6702ed42007-08-07 16:15:09 -0400209 ret = btrfs_cow_block(trans, root, root->node, NULL, 0, &tmp);
210 BUG_ON(ret);
Chris Mason6702ed42007-08-07 16:15:09 -0400211 path->nodes[level] = root->node;
212 path->slots[level] = 0;
Chris Masone9d0b132007-08-10 14:06:19 -0400213 if (is_extent)
214 btrfs_extent_post_op(trans, root);
Chris Mason6702ed42007-08-07 16:15:09 -0400215 } else {
216 level = root->defrag_level;
217 path->lowest_level = level;
218 wret = btrfs_search_slot(trans, root, &root->defrag_progress,
219 path, 0, 1);
220
Chris Masone9d0b132007-08-10 14:06:19 -0400221 if (is_extent)
222 btrfs_extent_post_op(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -0400223
Chris Mason6702ed42007-08-07 16:15:09 -0400224 if (wret < 0) {
225 ret = wret;
226 goto out;
227 }
Chris Mason5f39d392007-10-15 16:14:19 -0400228
Chris Mason6702ed42007-08-07 16:15:09 -0400229 while(level > 0 && !path->nodes[level])
230 level--;
Chris Mason5f39d392007-10-15 16:14:19 -0400231
Chris Mason6702ed42007-08-07 16:15:09 -0400232 if (!path->nodes[level]) {
233 ret = 0;
234 goto out;
235 }
236 }
237
238 while(1) {
Chris Masone9d0b132007-08-10 14:06:19 -0400239 wret = defrag_walk_down(trans, root, path, &level, cache_only,
240 &last_ret);
Chris Mason6702ed42007-08-07 16:15:09 -0400241 if (wret > 0)
242 break;
243 if (wret < 0)
244 ret = wret;
245
246 wret = defrag_walk_up(trans, root, path, &level, cache_only);
247 if (wret > 0)
248 break;
249 if (wret < 0)
250 ret = wret;
Chris Mason081e9572007-11-06 10:26:24 -0500251 else
252 ret = -EAGAIN;
Chris Mason409eb952007-08-08 20:17:12 -0400253 break;
Chris Mason6702ed42007-08-07 16:15:09 -0400254 }
255 for (i = 0; i <= orig_level; i++) {
256 if (path->nodes[i]) {
Chris Mason5f39d392007-10-15 16:14:19 -0400257 free_extent_buffer(path->nodes[i]);
Chris Mason0f827312007-10-15 16:18:56 -0400258 path->nodes[i] = NULL;
Chris Mason6702ed42007-08-07 16:15:09 -0400259 }
260 }
261out:
262 if (path)
263 btrfs_free_path(path);
Chris Mason0ef3e662008-05-24 14:04:53 -0400264 if (ret == -EAGAIN) {
265 if (root->defrag_max.objectid > root->defrag_progress.objectid)
266 goto done;
267 if (root->defrag_max.type > root->defrag_progress.type)
268 goto done;
269 if (root->defrag_max.offset > root->defrag_progress.offset)
270 goto done;
271 ret = 0;
272 }
273done:
Chris Mason6702ed42007-08-07 16:15:09 -0400274 if (ret != -EAGAIN) {
275 memset(&root->defrag_progress, 0,
276 sizeof(root->defrag_progress));
277 }
278 return ret;
279}