blob: 3feac2f28628fc1503baf5bd82bc64d42a42da48 [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 Mason6702ed42007-08-07 16:15:09 -040061 while(*level > 0) {
62 WARN_ON(*level < 0);
63 WARN_ON(*level >= BTRFS_MAX_LEVEL);
64 cur = path->nodes[*level];
65
66 if (!cache_only && *level > 1 && path->slots[*level] == 0)
Chris Mason5f39d392007-10-15 16:14:19 -040067 reada_defrag(root, cur);
Chris Mason6702ed42007-08-07 16:15:09 -040068
Chris Mason5f39d392007-10-15 16:14:19 -040069 if (btrfs_header_level(cur) != *level)
Chris Mason6702ed42007-08-07 16:15:09 -040070 WARN_ON(1);
71
72 if (path->slots[*level] >=
Chris Mason5f39d392007-10-15 16:14:19 -040073 btrfs_header_nritems(cur))
Chris Mason6702ed42007-08-07 16:15:09 -040074 break;
75
76 if (*level == 1) {
77 ret = btrfs_realloc_node(trans, root,
78 path->nodes[*level],
Chris Masone9d0b132007-08-10 14:06:19 -040079 cache_only, last_ret);
80 if (is_extent)
81 btrfs_extent_post_op(trans, root);
82
Chris Mason6702ed42007-08-07 16:15:09 -040083 break;
84 }
Chris Masondb945352007-10-15 16:15:53 -040085 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
Chris Mason6702ed42007-08-07 16:15:09 -040086
87 if (cache_only) {
Chris Masondb945352007-10-15 16:15:53 -040088 next = btrfs_find_tree_block(root, bytenr,
89 btrfs_level_size(root, *level - 1));
Chris Mason5f39d392007-10-15 16:14:19 -040090 /* FIXME, test for defrag */
91 if (!next || !btrfs_buffer_uptodate(next)) {
92 free_extent_buffer(next);
Chris Mason6702ed42007-08-07 16:15:09 -040093 path->slots[*level]++;
94 continue;
95 }
96 } else {
Chris Masondb945352007-10-15 16:15:53 -040097 next = read_tree_block(root, bytenr,
98 btrfs_level_size(root, *level - 1));
Chris Mason6702ed42007-08-07 16:15:09 -040099 }
100 ret = btrfs_cow_block(trans, root, next, path->nodes[*level],
101 path->slots[*level], &next);
102 BUG_ON(ret);
Chris Masone9d0b132007-08-10 14:06:19 -0400103 ret = btrfs_realloc_node(trans, root, next, cache_only,
104 last_ret);
Chris Mason6702ed42007-08-07 16:15:09 -0400105 BUG_ON(ret);
Chris Masone9d0b132007-08-10 14:06:19 -0400106
107 if (is_extent)
108 btrfs_extent_post_op(trans, root);
109
Chris Mason6702ed42007-08-07 16:15:09 -0400110 WARN_ON(*level <= 0);
111 if (path->nodes[*level-1])
Chris Mason5f39d392007-10-15 16:14:19 -0400112 free_extent_buffer(path->nodes[*level-1]);
Chris Mason6702ed42007-08-07 16:15:09 -0400113 path->nodes[*level-1] = next;
Chris Mason5f39d392007-10-15 16:14:19 -0400114 *level = btrfs_header_level(next);
Chris Mason6702ed42007-08-07 16:15:09 -0400115 path->slots[*level] = 0;
116 }
117 WARN_ON(*level < 0);
118 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason5f39d392007-10-15 16:14:19 -0400119#if 0
Chris Mason86479a02007-09-10 19:58:16 -0400120 clear_buffer_defrag(path->nodes[*level]);
121 clear_buffer_defrag_done(path->nodes[*level]);
Chris Mason5f39d392007-10-15 16:14:19 -0400122#endif
123 free_extent_buffer(path->nodes[*level]);
Chris Mason6702ed42007-08-07 16:15:09 -0400124 path->nodes[*level] = NULL;
125 *level += 1;
126 WARN_ON(ret);
127 return 0;
128}
129
130static int defrag_walk_up(struct btrfs_trans_handle *trans,
131 struct btrfs_root *root,
132 struct btrfs_path *path, int *level,
133 int cache_only)
134{
135 int i;
136 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -0400137 struct extent_buffer *node;
Chris Mason6702ed42007-08-07 16:15:09 -0400138
139 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
140 slot = path->slots[i];
Chris Mason5f39d392007-10-15 16:14:19 -0400141 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
Chris Mason6702ed42007-08-07 16:15:09 -0400142 path->slots[i]++;
143 *level = i;
Chris Mason5f39d392007-10-15 16:14:19 -0400144 node = path->nodes[i];
Chris Mason6702ed42007-08-07 16:15:09 -0400145 WARN_ON(i == 0);
Chris Mason5f39d392007-10-15 16:14:19 -0400146 btrfs_node_key_to_cpu(node, &root->defrag_progress,
147 path->slots[i]);
Chris Mason6702ed42007-08-07 16:15:09 -0400148 root->defrag_level = i;
149 return 0;
150 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400151 /*
Chris Masonf2183bd2007-08-10 14:42:37 -0400152 clear_buffer_defrag(path->nodes[*level]);
Chris Mason86479a02007-09-10 19:58:16 -0400153 clear_buffer_defrag_done(path->nodes[*level]);
Chris Mason5f39d392007-10-15 16:14:19 -0400154 */
155 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;
188 if (level == 0) {
189 goto out;
190 }
191 if (root->defrag_progress.objectid == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -0400192 extent_buffer_get(root->node);
Chris Mason6702ed42007-08-07 16:15:09 -0400193 ret = btrfs_cow_block(trans, root, root->node, NULL, 0, &tmp);
194 BUG_ON(ret);
Chris Masone9d0b132007-08-10 14:06:19 -0400195 ret = btrfs_realloc_node(trans, root, root->node, cache_only,
196 &last_ret);
Chris Mason6702ed42007-08-07 16:15:09 -0400197 BUG_ON(ret);
198 path->nodes[level] = root->node;
199 path->slots[level] = 0;
Chris Masone9d0b132007-08-10 14:06:19 -0400200 if (is_extent)
201 btrfs_extent_post_op(trans, root);
Chris Mason6702ed42007-08-07 16:15:09 -0400202 } else {
203 level = root->defrag_level;
204 path->lowest_level = level;
205 wret = btrfs_search_slot(trans, root, &root->defrag_progress,
206 path, 0, 1);
207
Chris Masone9d0b132007-08-10 14:06:19 -0400208 if (is_extent)
209 btrfs_extent_post_op(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -0400210
Chris Mason6702ed42007-08-07 16:15:09 -0400211 if (wret < 0) {
212 ret = wret;
213 goto out;
214 }
Chris Mason5f39d392007-10-15 16:14:19 -0400215
Chris Mason6702ed42007-08-07 16:15:09 -0400216 while(level > 0 && !path->nodes[level])
217 level--;
Chris Mason5f39d392007-10-15 16:14:19 -0400218
Chris Mason6702ed42007-08-07 16:15:09 -0400219 if (!path->nodes[level]) {
220 ret = 0;
221 goto out;
222 }
223 }
224
225 while(1) {
Chris Masone9d0b132007-08-10 14:06:19 -0400226 wret = defrag_walk_down(trans, root, path, &level, cache_only,
227 &last_ret);
Chris Mason6702ed42007-08-07 16:15:09 -0400228 if (wret > 0)
229 break;
230 if (wret < 0)
231 ret = wret;
232
233 wret = defrag_walk_up(trans, root, path, &level, cache_only);
234 if (wret > 0)
235 break;
236 if (wret < 0)
237 ret = wret;
Chris Mason409eb952007-08-08 20:17:12 -0400238 ret = -EAGAIN;
239 break;
Chris Mason6702ed42007-08-07 16:15:09 -0400240 }
241 for (i = 0; i <= orig_level; i++) {
242 if (path->nodes[i]) {
Chris Mason5f39d392007-10-15 16:14:19 -0400243 free_extent_buffer(path->nodes[i]);
Chris Mason6702ed42007-08-07 16:15:09 -0400244 path->nodes[i] = 0;
245 }
246 }
247out:
248 if (path)
249 btrfs_free_path(path);
250 if (ret != -EAGAIN) {
251 memset(&root->defrag_progress, 0,
252 sizeof(root->defrag_progress));
253 }
254 return ret;
255}