]>
Commit | Line | Data |
---|---|---|
6702ed49 CM |
1 | /* |
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 | ||
25 | static void reada_defrag(struct btrfs_root *root, | |
5f39d397 | 26 | struct extent_buffer *node) |
6702ed49 CM |
27 | { |
28 | int i; | |
29 | u32 nritems; | |
db94535d CM |
30 | u64 bytenr; |
31 | u32 blocksize; | |
6702ed49 CM |
32 | int ret; |
33 | ||
db94535d | 34 | blocksize = btrfs_level_size(root, btrfs_header_level(node) - 1); |
5f39d397 | 35 | nritems = btrfs_header_nritems(node); |
6702ed49 | 36 | for (i = 0; i < nritems; i++) { |
db94535d CM |
37 | bytenr = btrfs_node_blockptr(node, i); |
38 | ret = readahead_tree_block(root, bytenr, blocksize); | |
6702ed49 CM |
39 | if (ret) |
40 | break; | |
41 | } | |
42 | } | |
43 | ||
44 | static int defrag_walk_down(struct btrfs_trans_handle *trans, | |
45 | struct btrfs_root *root, | |
46 | struct btrfs_path *path, int *level, | |
e9d0b13b | 47 | int cache_only, u64 *last_ret) |
6702ed49 | 48 | { |
5f39d397 CM |
49 | struct extent_buffer *next; |
50 | struct extent_buffer *cur; | |
db94535d | 51 | u64 bytenr; |
6702ed49 | 52 | int ret = 0; |
e9d0b13b | 53 | int is_extent = 0; |
6702ed49 CM |
54 | |
55 | WARN_ON(*level < 0); | |
56 | WARN_ON(*level >= BTRFS_MAX_LEVEL); | |
57 | ||
e9d0b13b CM |
58 | if (root->fs_info->extent_root == root) |
59 | is_extent = 1; | |
60 | ||
6702ed49 CM |
61 | 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) | |
5f39d397 | 67 | reada_defrag(root, cur); |
6702ed49 | 68 | |
5f39d397 | 69 | if (btrfs_header_level(cur) != *level) |
6702ed49 CM |
70 | WARN_ON(1); |
71 | ||
72 | if (path->slots[*level] >= | |
5f39d397 | 73 | btrfs_header_nritems(cur)) |
6702ed49 CM |
74 | break; |
75 | ||
76 | if (*level == 1) { | |
77 | ret = btrfs_realloc_node(trans, root, | |
78 | path->nodes[*level], | |
e9d0b13b CM |
79 | cache_only, last_ret); |
80 | if (is_extent) | |
81 | btrfs_extent_post_op(trans, root); | |
82 | ||
6702ed49 CM |
83 | break; |
84 | } | |
db94535d | 85 | bytenr = btrfs_node_blockptr(cur, path->slots[*level]); |
6702ed49 CM |
86 | |
87 | if (cache_only) { | |
db94535d CM |
88 | next = btrfs_find_tree_block(root, bytenr, |
89 | btrfs_level_size(root, *level - 1)); | |
5f39d397 CM |
90 | /* FIXME, test for defrag */ |
91 | if (!next || !btrfs_buffer_uptodate(next)) { | |
92 | free_extent_buffer(next); | |
6702ed49 CM |
93 | path->slots[*level]++; |
94 | continue; | |
95 | } | |
96 | } else { | |
db94535d CM |
97 | next = read_tree_block(root, bytenr, |
98 | btrfs_level_size(root, *level - 1)); | |
6702ed49 CM |
99 | } |
100 | ret = btrfs_cow_block(trans, root, next, path->nodes[*level], | |
101 | path->slots[*level], &next); | |
102 | BUG_ON(ret); | |
e9d0b13b CM |
103 | ret = btrfs_realloc_node(trans, root, next, cache_only, |
104 | last_ret); | |
6702ed49 | 105 | BUG_ON(ret); |
e9d0b13b CM |
106 | |
107 | if (is_extent) | |
108 | btrfs_extent_post_op(trans, root); | |
109 | ||
6702ed49 CM |
110 | WARN_ON(*level <= 0); |
111 | if (path->nodes[*level-1]) | |
5f39d397 | 112 | free_extent_buffer(path->nodes[*level-1]); |
6702ed49 | 113 | path->nodes[*level-1] = next; |
5f39d397 | 114 | *level = btrfs_header_level(next); |
6702ed49 CM |
115 | path->slots[*level] = 0; |
116 | } | |
117 | WARN_ON(*level < 0); | |
118 | WARN_ON(*level >= BTRFS_MAX_LEVEL); | |
6b80053d CM |
119 | |
120 | btrfs_clear_buffer_defrag(path->nodes[*level]); | |
121 | btrfs_clear_buffer_defrag_done(path->nodes[*level]); | |
122 | ||
5f39d397 | 123 | free_extent_buffer(path->nodes[*level]); |
6702ed49 CM |
124 | path->nodes[*level] = NULL; |
125 | *level += 1; | |
126 | WARN_ON(ret); | |
127 | return 0; | |
128 | } | |
129 | ||
130 | static 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; | |
5f39d397 | 137 | struct extent_buffer *node; |
6702ed49 CM |
138 | |
139 | for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) { | |
140 | slot = path->slots[i]; | |
5f39d397 | 141 | if (slot < btrfs_header_nritems(path->nodes[i]) - 1) { |
6702ed49 CM |
142 | path->slots[i]++; |
143 | *level = i; | |
5f39d397 | 144 | node = path->nodes[i]; |
6702ed49 | 145 | WARN_ON(i == 0); |
5f39d397 CM |
146 | btrfs_node_key_to_cpu(node, &root->defrag_progress, |
147 | path->slots[i]); | |
6702ed49 CM |
148 | root->defrag_level = i; |
149 | return 0; | |
150 | } else { | |
6b80053d CM |
151 | btrfs_clear_buffer_defrag(path->nodes[*level]); |
152 | btrfs_clear_buffer_defrag_done(path->nodes[*level]); | |
5f39d397 | 153 | free_extent_buffer(path->nodes[*level]); |
6702ed49 CM |
154 | path->nodes[*level] = NULL; |
155 | *level = i + 1; | |
156 | } | |
157 | } | |
158 | return 1; | |
159 | } | |
160 | ||
161 | int btrfs_defrag_leaves(struct btrfs_trans_handle *trans, | |
162 | struct btrfs_root *root, int cache_only) | |
163 | { | |
164 | struct btrfs_path *path = NULL; | |
5f39d397 | 165 | struct extent_buffer *tmp; |
6702ed49 CM |
166 | int ret = 0; |
167 | int wret; | |
168 | int level; | |
169 | int orig_level; | |
170 | int i; | |
e9d0b13b CM |
171 | int is_extent = 0; |
172 | u64 last_ret = 0; | |
173 | ||
174 | if (root->fs_info->extent_root == root) | |
175 | is_extent = 1; | |
6702ed49 | 176 | |
e9d0b13b | 177 | if (root->ref_cows == 0 && !is_extent) |
6702ed49 | 178 | goto out; |
5f39d397 | 179 | |
6702ed49 CM |
180 | path = btrfs_alloc_path(); |
181 | if (!path) | |
182 | return -ENOMEM; | |
183 | ||
5f39d397 | 184 | level = btrfs_header_level(root->node); |
6702ed49 CM |
185 | orig_level = level; |
186 | if (level == 0) { | |
187 | goto out; | |
188 | } | |
189 | if (root->defrag_progress.objectid == 0) { | |
5f39d397 | 190 | extent_buffer_get(root->node); |
6702ed49 CM |
191 | ret = btrfs_cow_block(trans, root, root->node, NULL, 0, &tmp); |
192 | BUG_ON(ret); | |
e9d0b13b CM |
193 | ret = btrfs_realloc_node(trans, root, root->node, cache_only, |
194 | &last_ret); | |
6702ed49 CM |
195 | BUG_ON(ret); |
196 | path->nodes[level] = root->node; | |
197 | path->slots[level] = 0; | |
e9d0b13b CM |
198 | if (is_extent) |
199 | btrfs_extent_post_op(trans, root); | |
6702ed49 CM |
200 | } else { |
201 | level = root->defrag_level; | |
202 | path->lowest_level = level; | |
203 | wret = btrfs_search_slot(trans, root, &root->defrag_progress, | |
204 | path, 0, 1); | |
205 | ||
e9d0b13b CM |
206 | if (is_extent) |
207 | btrfs_extent_post_op(trans, root); | |
5f39d397 | 208 | |
6702ed49 CM |
209 | if (wret < 0) { |
210 | ret = wret; | |
211 | goto out; | |
212 | } | |
5f39d397 | 213 | |
6702ed49 CM |
214 | while(level > 0 && !path->nodes[level]) |
215 | level--; | |
5f39d397 | 216 | |
6702ed49 CM |
217 | if (!path->nodes[level]) { |
218 | ret = 0; | |
219 | goto out; | |
220 | } | |
221 | } | |
222 | ||
223 | while(1) { | |
e9d0b13b CM |
224 | wret = defrag_walk_down(trans, root, path, &level, cache_only, |
225 | &last_ret); | |
6702ed49 CM |
226 | if (wret > 0) |
227 | break; | |
228 | if (wret < 0) | |
229 | ret = wret; | |
230 | ||
231 | wret = defrag_walk_up(trans, root, path, &level, cache_only); | |
232 | if (wret > 0) | |
233 | break; | |
234 | if (wret < 0) | |
235 | ret = wret; | |
409eb95d CM |
236 | ret = -EAGAIN; |
237 | break; | |
6702ed49 CM |
238 | } |
239 | for (i = 0; i <= orig_level; i++) { | |
240 | if (path->nodes[i]) { | |
5f39d397 | 241 | free_extent_buffer(path->nodes[i]); |
6702ed49 CM |
242 | path->nodes[i] = 0; |
243 | } | |
244 | } | |
245 | out: | |
246 | if (path) | |
247 | btrfs_free_path(path); | |
248 | if (ret != -EAGAIN) { | |
249 | memset(&root->defrag_progress, 0, | |
250 | sizeof(root->defrag_progress)); | |
251 | } | |
252 | return ret; | |
253 | } |