blob: 5085e9e693b931942ce838dd929a314f2e404673 [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 Mason6702ed42007-08-07 16:15:09 -040054 int ret = 0;
Chris Masone9d0b132007-08-10 14:06:19 -040055 int is_extent = 0;
Chris Mason6702ed42007-08-07 16:15:09 -040056
57 WARN_ON(*level < 0);
58 WARN_ON(*level >= BTRFS_MAX_LEVEL);
59
Chris Masone9d0b132007-08-10 14:06:19 -040060 if (root->fs_info->extent_root == root)
61 is_extent = 1;
62
Chris Masonf84a8b32007-11-06 10:26:29 -050063 if (*level == 1 && cache_only && path->nodes[1] &&
64 !btrfs_buffer_defrag(path->nodes[1])) {
65 goto out;
66 }
Chris Mason6702ed42007-08-07 16:15:09 -040067 while(*level > 0) {
68 WARN_ON(*level < 0);
69 WARN_ON(*level >= BTRFS_MAX_LEVEL);
70 cur = path->nodes[*level];
71
72 if (!cache_only && *level > 1 && path->slots[*level] == 0)
Chris Mason5f39d392007-10-15 16:14:19 -040073 reada_defrag(root, cur);
Chris Mason6702ed42007-08-07 16:15:09 -040074
Chris Mason5f39d392007-10-15 16:14:19 -040075 if (btrfs_header_level(cur) != *level)
Chris Mason6702ed42007-08-07 16:15:09 -040076 WARN_ON(1);
77
78 if (path->slots[*level] >=
Chris Mason5f39d392007-10-15 16:14:19 -040079 btrfs_header_nritems(cur))
Chris Mason6702ed42007-08-07 16:15:09 -040080 break;
81
82 if (*level == 1) {
Chris Mason7bb86312007-12-11 09:25:06 -050083 WARN_ON(btrfs_header_generation(path->nodes[*level]) !=
84 trans->transid);
Chris Mason6702ed42007-08-07 16:15:09 -040085 ret = btrfs_realloc_node(trans, root,
86 path->nodes[*level],
Chris Masona6b6e752007-10-15 16:22:39 -040087 path->slots[*level],
88 cache_only, last_ret,
89 &root->defrag_progress);
Chris Masone9d0b132007-08-10 14:06:19 -040090 if (is_extent)
91 btrfs_extent_post_op(trans, root);
92
Chris Mason6702ed42007-08-07 16:15:09 -040093 break;
94 }
Chris Masondb945352007-10-15 16:15:53 -040095 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
Chris Mason6702ed42007-08-07 16:15:09 -040096
97 if (cache_only) {
Chris Masondb945352007-10-15 16:15:53 -040098 next = btrfs_find_tree_block(root, bytenr,
99 btrfs_level_size(root, *level - 1));
Chris Masoncf786e72007-10-15 16:22:11 -0400100 if (!next || !btrfs_buffer_uptodate(next) ||
101 !btrfs_buffer_defrag(next)) {
Chris Mason5f39d392007-10-15 16:14:19 -0400102 free_extent_buffer(next);
Chris Mason6702ed42007-08-07 16:15:09 -0400103 path->slots[*level]++;
104 continue;
105 }
106 } else {
Chris Masondb945352007-10-15 16:15:53 -0400107 next = read_tree_block(root, bytenr,
Chris Masonca7a79a2008-05-12 12:59:19 -0400108 btrfs_level_size(root, *level - 1),
109 btrfs_node_ptr_generation(cur,
110 path->slots[*level]));
Chris Mason6702ed42007-08-07 16:15:09 -0400111 }
112 ret = btrfs_cow_block(trans, root, next, path->nodes[*level],
113 path->slots[*level], &next);
114 BUG_ON(ret);
Chris Masone9d0b132007-08-10 14:06:19 -0400115 if (is_extent)
116 btrfs_extent_post_op(trans, root);
117
Chris Mason6702ed42007-08-07 16:15:09 -0400118 WARN_ON(*level <= 0);
119 if (path->nodes[*level-1])
Chris Mason5f39d392007-10-15 16:14:19 -0400120 free_extent_buffer(path->nodes[*level-1]);
Chris Mason6702ed42007-08-07 16:15:09 -0400121 path->nodes[*level-1] = next;
Chris Mason5f39d392007-10-15 16:14:19 -0400122 *level = btrfs_header_level(next);
Chris Mason6702ed42007-08-07 16:15:09 -0400123 path->slots[*level] = 0;
124 }
125 WARN_ON(*level < 0);
126 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason6b800532007-10-15 16:17:34 -0400127
128 btrfs_clear_buffer_defrag(path->nodes[*level]);
Chris Masonf84a8b32007-11-06 10:26:29 -0500129out:
Chris Mason5f39d392007-10-15 16:14:19 -0400130 free_extent_buffer(path->nodes[*level]);
Chris Mason6702ed42007-08-07 16:15:09 -0400131 path->nodes[*level] = NULL;
132 *level += 1;
Chris Masona6b6e752007-10-15 16:22:39 -0400133 WARN_ON(ret && ret != -EAGAIN);
134 return ret;
Chris Mason6702ed42007-08-07 16:15:09 -0400135}
136
137static int defrag_walk_up(struct btrfs_trans_handle *trans,
138 struct btrfs_root *root,
139 struct btrfs_path *path, int *level,
140 int cache_only)
141{
142 int i;
143 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -0400144 struct extent_buffer *node;
Chris Mason6702ed42007-08-07 16:15:09 -0400145
146 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
147 slot = path->slots[i];
Chris Mason5f39d392007-10-15 16:14:19 -0400148 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
Chris Mason6702ed42007-08-07 16:15:09 -0400149 path->slots[i]++;
150 *level = i;
Chris Mason5f39d392007-10-15 16:14:19 -0400151 node = path->nodes[i];
Chris Mason6702ed42007-08-07 16:15:09 -0400152 WARN_ON(i == 0);
Chris Mason5f39d392007-10-15 16:14:19 -0400153 btrfs_node_key_to_cpu(node, &root->defrag_progress,
154 path->slots[i]);
Chris Mason6702ed42007-08-07 16:15:09 -0400155 root->defrag_level = i;
156 return 0;
157 } else {
Chris Mason6b800532007-10-15 16:17:34 -0400158 btrfs_clear_buffer_defrag(path->nodes[*level]);
Chris Mason5f39d392007-10-15 16:14:19 -0400159 free_extent_buffer(path->nodes[*level]);
Chris Mason6702ed42007-08-07 16:15:09 -0400160 path->nodes[*level] = NULL;
161 *level = i + 1;
162 }
163 }
164 return 1;
165}
166
167int btrfs_defrag_leaves(struct btrfs_trans_handle *trans,
168 struct btrfs_root *root, int cache_only)
169{
170 struct btrfs_path *path = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400171 struct extent_buffer *tmp;
Chris Mason6702ed42007-08-07 16:15:09 -0400172 int ret = 0;
173 int wret;
174 int level;
175 int orig_level;
176 int i;
Chris Masone9d0b132007-08-10 14:06:19 -0400177 int is_extent = 0;
178 u64 last_ret = 0;
Chris Mason6702ed42007-08-07 16:15:09 -0400179
Chris Masone9d0b132007-08-10 14:06:19 -0400180 if (root->fs_info->extent_root == root)
181 is_extent = 1;
182
183 if (root->ref_cows == 0 && !is_extent)
Chris Mason6702ed42007-08-07 16:15:09 -0400184 goto out;
Chris Mason5f39d392007-10-15 16:14:19 -0400185
Chris Mason9afbb0b2008-02-15 13:19:35 -0500186 if (btrfs_test_opt(root, SSD))
187 goto out;
188
Chris Mason6702ed42007-08-07 16:15:09 -0400189 path = btrfs_alloc_path();
190 if (!path)
191 return -ENOMEM;
192
Chris Mason5f39d392007-10-15 16:14:19 -0400193 level = btrfs_header_level(root->node);
Chris Mason6702ed42007-08-07 16:15:09 -0400194 orig_level = level;
Chris Mason0f1ebbd2007-10-15 16:19:46 -0400195
Chris Mason6702ed42007-08-07 16:15:09 -0400196 if (level == 0) {
197 goto out;
198 }
199 if (root->defrag_progress.objectid == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -0400200 extent_buffer_get(root->node);
Chris Mason6702ed42007-08-07 16:15:09 -0400201 ret = btrfs_cow_block(trans, root, root->node, NULL, 0, &tmp);
202 BUG_ON(ret);
Chris Mason6702ed42007-08-07 16:15:09 -0400203 path->nodes[level] = root->node;
204 path->slots[level] = 0;
Chris Masone9d0b132007-08-10 14:06:19 -0400205 if (is_extent)
206 btrfs_extent_post_op(trans, root);
Chris Mason6702ed42007-08-07 16:15:09 -0400207 } else {
208 level = root->defrag_level;
209 path->lowest_level = level;
210 wret = btrfs_search_slot(trans, root, &root->defrag_progress,
211 path, 0, 1);
212
Chris Masone9d0b132007-08-10 14:06:19 -0400213 if (is_extent)
214 btrfs_extent_post_op(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -0400215
Chris Mason6702ed42007-08-07 16:15:09 -0400216 if (wret < 0) {
217 ret = wret;
218 goto out;
219 }
Chris Mason5f39d392007-10-15 16:14:19 -0400220
Chris Mason6702ed42007-08-07 16:15:09 -0400221 while(level > 0 && !path->nodes[level])
222 level--;
Chris Mason5f39d392007-10-15 16:14:19 -0400223
Chris Mason6702ed42007-08-07 16:15:09 -0400224 if (!path->nodes[level]) {
225 ret = 0;
226 goto out;
227 }
228 }
229
230 while(1) {
Chris Masone9d0b132007-08-10 14:06:19 -0400231 wret = defrag_walk_down(trans, root, path, &level, cache_only,
232 &last_ret);
Chris Mason6702ed42007-08-07 16:15:09 -0400233 if (wret > 0)
234 break;
235 if (wret < 0)
236 ret = wret;
237
238 wret = defrag_walk_up(trans, root, path, &level, cache_only);
239 if (wret > 0)
240 break;
241 if (wret < 0)
242 ret = wret;
Chris Mason081e9572007-11-06 10:26:24 -0500243 else
244 ret = -EAGAIN;
Chris Mason409eb952007-08-08 20:17:12 -0400245 break;
Chris Mason6702ed42007-08-07 16:15:09 -0400246 }
247 for (i = 0; i <= orig_level; i++) {
248 if (path->nodes[i]) {
Chris Mason5f39d392007-10-15 16:14:19 -0400249 free_extent_buffer(path->nodes[i]);
Chris Mason0f827312007-10-15 16:18:56 -0400250 path->nodes[i] = NULL;
Chris Mason6702ed42007-08-07 16:15:09 -0400251 }
252 }
253out:
254 if (path)
255 btrfs_free_path(path);
256 if (ret != -EAGAIN) {
257 memset(&root->defrag_progress, 0,
258 sizeof(root->defrag_progress));
259 }
260 return ret;
261}