blob: daf019afa0a101fd2bf8997f938a9905f047b8e2 [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;
30 u64 blocknr;
31 int ret;
32
Chris Mason5f39d392007-10-15 16:14:19 -040033 nritems = btrfs_header_nritems(node);
Chris Mason6702ed42007-08-07 16:15:09 -040034 for (i = 0; i < nritems; i++) {
35 blocknr = btrfs_node_blockptr(node, i);
36 ret = readahead_tree_block(root, blocknr);
37 if (ret)
38 break;
39 }
40}
41
42static int defrag_walk_down(struct btrfs_trans_handle *trans,
43 struct btrfs_root *root,
44 struct btrfs_path *path, int *level,
Chris Masone9d0b132007-08-10 14:06:19 -040045 int cache_only, u64 *last_ret)
Chris Mason6702ed42007-08-07 16:15:09 -040046{
Chris Mason5f39d392007-10-15 16:14:19 -040047 struct extent_buffer *next;
48 struct extent_buffer *cur;
Chris Mason6702ed42007-08-07 16:15:09 -040049 u64 blocknr;
50 int ret = 0;
Chris Masone9d0b132007-08-10 14:06:19 -040051 int is_extent = 0;
Chris Mason6702ed42007-08-07 16:15:09 -040052
53 WARN_ON(*level < 0);
54 WARN_ON(*level >= BTRFS_MAX_LEVEL);
55
Chris Masone9d0b132007-08-10 14:06:19 -040056 if (root->fs_info->extent_root == root)
57 is_extent = 1;
58
Chris Mason6702ed42007-08-07 16:15:09 -040059 while(*level > 0) {
60 WARN_ON(*level < 0);
61 WARN_ON(*level >= BTRFS_MAX_LEVEL);
62 cur = path->nodes[*level];
63
64 if (!cache_only && *level > 1 && path->slots[*level] == 0)
Chris Mason5f39d392007-10-15 16:14:19 -040065 reada_defrag(root, cur);
Chris Mason6702ed42007-08-07 16:15:09 -040066
Chris Mason5f39d392007-10-15 16:14:19 -040067 if (btrfs_header_level(cur) != *level)
Chris Mason6702ed42007-08-07 16:15:09 -040068 WARN_ON(1);
69
70 if (path->slots[*level] >=
Chris Mason5f39d392007-10-15 16:14:19 -040071 btrfs_header_nritems(cur))
Chris Mason6702ed42007-08-07 16:15:09 -040072 break;
73
74 if (*level == 1) {
75 ret = btrfs_realloc_node(trans, root,
76 path->nodes[*level],
Chris Masone9d0b132007-08-10 14:06:19 -040077 cache_only, last_ret);
78 if (is_extent)
79 btrfs_extent_post_op(trans, root);
80
Chris Mason6702ed42007-08-07 16:15:09 -040081 break;
82 }
Chris Mason5f39d392007-10-15 16:14:19 -040083 blocknr = btrfs_node_blockptr(cur, path->slots[*level]);
Chris Mason6702ed42007-08-07 16:15:09 -040084
85 if (cache_only) {
86 next = btrfs_find_tree_block(root, blocknr);
Chris Mason5f39d392007-10-15 16:14:19 -040087 /* FIXME, test for defrag */
88 if (!next || !btrfs_buffer_uptodate(next)) {
89 free_extent_buffer(next);
Chris Mason6702ed42007-08-07 16:15:09 -040090 path->slots[*level]++;
91 continue;
92 }
93 } else {
94 next = read_tree_block(root, blocknr);
95 }
96 ret = btrfs_cow_block(trans, root, next, path->nodes[*level],
97 path->slots[*level], &next);
98 BUG_ON(ret);
Chris Masone9d0b132007-08-10 14:06:19 -040099 ret = btrfs_realloc_node(trans, root, next, cache_only,
100 last_ret);
Chris Mason6702ed42007-08-07 16:15:09 -0400101 BUG_ON(ret);
Chris Masone9d0b132007-08-10 14:06:19 -0400102
103 if (is_extent)
104 btrfs_extent_post_op(trans, root);
105
Chris Mason6702ed42007-08-07 16:15:09 -0400106 WARN_ON(*level <= 0);
107 if (path->nodes[*level-1])
Chris Mason5f39d392007-10-15 16:14:19 -0400108 free_extent_buffer(path->nodes[*level-1]);
Chris Mason6702ed42007-08-07 16:15:09 -0400109 path->nodes[*level-1] = next;
Chris Mason5f39d392007-10-15 16:14:19 -0400110 *level = btrfs_header_level(next);
Chris Mason6702ed42007-08-07 16:15:09 -0400111 path->slots[*level] = 0;
112 }
113 WARN_ON(*level < 0);
114 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason5f39d392007-10-15 16:14:19 -0400115#if 0
Chris Mason86479a02007-09-10 19:58:16 -0400116 clear_buffer_defrag(path->nodes[*level]);
117 clear_buffer_defrag_done(path->nodes[*level]);
Chris Mason5f39d392007-10-15 16:14:19 -0400118#endif
119 free_extent_buffer(path->nodes[*level]);
Chris Mason6702ed42007-08-07 16:15:09 -0400120 path->nodes[*level] = NULL;
121 *level += 1;
122 WARN_ON(ret);
123 return 0;
124}
125
126static int defrag_walk_up(struct btrfs_trans_handle *trans,
127 struct btrfs_root *root,
128 struct btrfs_path *path, int *level,
129 int cache_only)
130{
131 int i;
132 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -0400133 struct extent_buffer *node;
Chris Mason6702ed42007-08-07 16:15:09 -0400134
135 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
136 slot = path->slots[i];
Chris Mason5f39d392007-10-15 16:14:19 -0400137 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
Chris Mason6702ed42007-08-07 16:15:09 -0400138 path->slots[i]++;
139 *level = i;
Chris Mason5f39d392007-10-15 16:14:19 -0400140 node = path->nodes[i];
Chris Mason6702ed42007-08-07 16:15:09 -0400141 WARN_ON(i == 0);
Chris Mason5f39d392007-10-15 16:14:19 -0400142 btrfs_node_key_to_cpu(node, &root->defrag_progress,
143 path->slots[i]);
Chris Mason6702ed42007-08-07 16:15:09 -0400144 root->defrag_level = i;
145 return 0;
146 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400147 /*
Chris Masonf2183bd2007-08-10 14:42:37 -0400148 clear_buffer_defrag(path->nodes[*level]);
Chris Mason86479a02007-09-10 19:58:16 -0400149 clear_buffer_defrag_done(path->nodes[*level]);
Chris Mason5f39d392007-10-15 16:14:19 -0400150 */
151 free_extent_buffer(path->nodes[*level]);
Chris Mason6702ed42007-08-07 16:15:09 -0400152 path->nodes[*level] = NULL;
153 *level = i + 1;
154 }
155 }
156 return 1;
157}
158
159int btrfs_defrag_leaves(struct btrfs_trans_handle *trans,
160 struct btrfs_root *root, int cache_only)
161{
162 struct btrfs_path *path = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400163 struct extent_buffer *tmp;
Chris Mason6702ed42007-08-07 16:15:09 -0400164 int ret = 0;
165 int wret;
166 int level;
167 int orig_level;
168 int i;
Chris Masone9d0b132007-08-10 14:06:19 -0400169 int is_extent = 0;
170 u64 last_ret = 0;
Chris Mason6702ed42007-08-07 16:15:09 -0400171
Chris Masone9d0b132007-08-10 14:06:19 -0400172 if (root->fs_info->extent_root == root)
173 is_extent = 1;
174
175 if (root->ref_cows == 0 && !is_extent)
Chris Mason6702ed42007-08-07 16:15:09 -0400176 goto out;
Chris Mason5f39d392007-10-15 16:14:19 -0400177
Chris Mason6702ed42007-08-07 16:15:09 -0400178 path = btrfs_alloc_path();
179 if (!path)
180 return -ENOMEM;
181
Chris Mason5f39d392007-10-15 16:14:19 -0400182 level = btrfs_header_level(root->node);
Chris Mason6702ed42007-08-07 16:15:09 -0400183 orig_level = level;
184 if (level == 0) {
185 goto out;
186 }
187 if (root->defrag_progress.objectid == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -0400188 extent_buffer_get(root->node);
Chris Mason6702ed42007-08-07 16:15:09 -0400189 ret = btrfs_cow_block(trans, root, root->node, NULL, 0, &tmp);
190 BUG_ON(ret);
Chris Masone9d0b132007-08-10 14:06:19 -0400191 ret = btrfs_realloc_node(trans, root, root->node, cache_only,
192 &last_ret);
Chris Mason6702ed42007-08-07 16:15:09 -0400193 BUG_ON(ret);
194 path->nodes[level] = root->node;
195 path->slots[level] = 0;
Chris Masone9d0b132007-08-10 14:06:19 -0400196 if (is_extent)
197 btrfs_extent_post_op(trans, root);
Chris Mason6702ed42007-08-07 16:15:09 -0400198 } else {
199 level = root->defrag_level;
200 path->lowest_level = level;
201 wret = btrfs_search_slot(trans, root, &root->defrag_progress,
202 path, 0, 1);
203
Chris Masone9d0b132007-08-10 14:06:19 -0400204 if (is_extent)
205 btrfs_extent_post_op(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -0400206
Chris Mason6702ed42007-08-07 16:15:09 -0400207 if (wret < 0) {
208 ret = wret;
209 goto out;
210 }
Chris Mason5f39d392007-10-15 16:14:19 -0400211
Chris Mason6702ed42007-08-07 16:15:09 -0400212 while(level > 0 && !path->nodes[level])
213 level--;
Chris Mason5f39d392007-10-15 16:14:19 -0400214
Chris Mason6702ed42007-08-07 16:15:09 -0400215 if (!path->nodes[level]) {
216 ret = 0;
217 goto out;
218 }
219 }
220
221 while(1) {
Chris Masone9d0b132007-08-10 14:06:19 -0400222 wret = defrag_walk_down(trans, root, path, &level, cache_only,
223 &last_ret);
Chris Mason6702ed42007-08-07 16:15:09 -0400224 if (wret > 0)
225 break;
226 if (wret < 0)
227 ret = wret;
228
229 wret = defrag_walk_up(trans, root, path, &level, cache_only);
230 if (wret > 0)
231 break;
232 if (wret < 0)
233 ret = wret;
Chris Mason409eb952007-08-08 20:17:12 -0400234 ret = -EAGAIN;
235 break;
Chris Mason6702ed42007-08-07 16:15:09 -0400236 }
237 for (i = 0; i <= orig_level; i++) {
238 if (path->nodes[i]) {
Chris Mason5f39d392007-10-15 16:14:19 -0400239 free_extent_buffer(path->nodes[i]);
Chris Mason6702ed42007-08-07 16:15:09 -0400240 path->nodes[i] = 0;
241 }
242 }
243out:
244 if (path)
245 btrfs_free_path(path);
246 if (ret != -EAGAIN) {
247 memset(&root->defrag_progress, 0,
248 sizeof(root->defrag_progress));
249 }
250 return ret;
251}