Commit | Line | Data |
---|---|---|
6cbd5570 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 | ||
ec6b910f | 19 | #include <linux/sched.h> |
fec577fb CM |
20 | #include "ctree.h" |
21 | #include "disk-io.h" | |
22 | #include "print-tree.h" | |
e089f05c | 23 | #include "transaction.h" |
fec577fb | 24 | |
e089f05c CM |
25 | static int finish_current_insert(struct btrfs_trans_handle *trans, struct |
26 | btrfs_root *extent_root); | |
e20d96d6 CM |
27 | static int del_pending_extents(struct btrfs_trans_handle *trans, struct |
28 | btrfs_root *extent_root); | |
fec577fb | 29 | |
e37c9e69 CM |
30 | static int cache_block_group(struct btrfs_root *root, |
31 | struct btrfs_block_group_cache *block_group) | |
32 | { | |
33 | struct btrfs_path *path; | |
34 | int ret; | |
35 | struct btrfs_key key; | |
5f39d397 | 36 | struct extent_buffer *leaf; |
f510cfec | 37 | struct extent_map_tree *free_space_cache; |
e37c9e69 | 38 | int slot; |
e37c9e69 CM |
39 | u64 last = 0; |
40 | u64 hole_size; | |
7d7d6068 | 41 | u64 first_free; |
e37c9e69 CM |
42 | int found = 0; |
43 | ||
44 | root = root->fs_info->extent_root; | |
f510cfec | 45 | free_space_cache = &root->fs_info->free_space_cache; |
e37c9e69 CM |
46 | |
47 | if (block_group->cached) | |
48 | return 0; | |
f510cfec | 49 | |
e37c9e69 CM |
50 | path = btrfs_alloc_path(); |
51 | if (!path) | |
52 | return -ENOMEM; | |
7d7d6068 | 53 | |
2cc58cf2 | 54 | path->reada = 2; |
7d7d6068 | 55 | first_free = block_group->key.objectid; |
e37c9e69 | 56 | key.objectid = block_group->key.objectid; |
e37c9e69 | 57 | key.offset = 0; |
7d7d6068 | 58 | |
e37c9e69 CM |
59 | btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY); |
60 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
7d7d6068 | 61 | |
e37c9e69 CM |
62 | if (ret < 0) |
63 | return ret; | |
7d7d6068 | 64 | |
e37c9e69 CM |
65 | if (ret && path->slots[0] > 0) |
66 | path->slots[0]--; | |
7d7d6068 | 67 | |
e37c9e69 | 68 | while(1) { |
5f39d397 | 69 | leaf = path->nodes[0]; |
e37c9e69 | 70 | slot = path->slots[0]; |
5f39d397 | 71 | if (slot >= btrfs_header_nritems(leaf)) { |
e37c9e69 | 72 | ret = btrfs_next_leaf(root, path); |
54aa1f4d CM |
73 | if (ret < 0) |
74 | goto err; | |
de428b63 | 75 | if (ret == 0) { |
e37c9e69 | 76 | continue; |
de428b63 | 77 | } else { |
e37c9e69 CM |
78 | break; |
79 | } | |
80 | } | |
7d7d6068 | 81 | |
5f39d397 | 82 | btrfs_item_key_to_cpu(leaf, &key, slot); |
7d7d6068 Y |
83 | if (key.objectid < block_group->key.objectid) { |
84 | if (key.objectid + key.offset > first_free) | |
85 | first_free = key.objectid + key.offset; | |
86 | goto next; | |
87 | } | |
88 | ||
e37c9e69 CM |
89 | if (key.objectid >= block_group->key.objectid + |
90 | block_group->key.offset) { | |
e37c9e69 CM |
91 | break; |
92 | } | |
7d7d6068 | 93 | |
e37c9e69 CM |
94 | if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) { |
95 | if (!found) { | |
7d7d6068 | 96 | last = first_free; |
e37c9e69 | 97 | found = 1; |
e37c9e69 | 98 | } |
f510cfec CM |
99 | if (key.objectid > last) { |
100 | hole_size = key.objectid - last; | |
101 | set_extent_dirty(free_space_cache, last, | |
102 | last + hole_size - 1, | |
103 | GFP_NOFS); | |
7d7d6068 Y |
104 | } |
105 | last = key.objectid + key.offset; | |
e37c9e69 | 106 | } |
7d7d6068 | 107 | next: |
e37c9e69 CM |
108 | path->slots[0]++; |
109 | } | |
110 | ||
7d7d6068 Y |
111 | if (!found) |
112 | last = first_free; | |
113 | if (block_group->key.objectid + | |
114 | block_group->key.offset > last) { | |
115 | hole_size = block_group->key.objectid + | |
116 | block_group->key.offset - last; | |
f510cfec CM |
117 | set_extent_dirty(free_space_cache, last, |
118 | last + hole_size - 1, GFP_NOFS); | |
7d7d6068 | 119 | } |
e37c9e69 | 120 | block_group->cached = 1; |
54aa1f4d | 121 | err: |
e37c9e69 CM |
122 | btrfs_free_path(path); |
123 | return 0; | |
124 | } | |
125 | ||
5276aeda CM |
126 | struct btrfs_block_group_cache *btrfs_lookup_block_group(struct |
127 | btrfs_fs_info *info, | |
128 | u64 blocknr) | |
be744175 CM |
129 | { |
130 | struct btrfs_block_group_cache *block_group; | |
131 | int ret; | |
132 | ||
133 | ret = radix_tree_gang_lookup(&info->block_group_radix, | |
134 | (void **)&block_group, | |
135 | blocknr, 1); | |
136 | if (ret) { | |
3e1ad54f | 137 | if (block_group->key.objectid <= blocknr && blocknr <= |
be744175 CM |
138 | block_group->key.objectid + block_group->key.offset) |
139 | return block_group; | |
140 | } | |
141 | ret = radix_tree_gang_lookup(&info->block_group_data_radix, | |
142 | (void **)&block_group, | |
143 | blocknr, 1); | |
144 | if (ret) { | |
3e1ad54f | 145 | if (block_group->key.objectid <= blocknr && blocknr <= |
be744175 CM |
146 | block_group->key.objectid + block_group->key.offset) |
147 | return block_group; | |
148 | } | |
be744175 CM |
149 | return NULL; |
150 | } | |
151 | ||
e37c9e69 CM |
152 | static u64 find_search_start(struct btrfs_root *root, |
153 | struct btrfs_block_group_cache **cache_ret, | |
f510cfec | 154 | u64 search_start, int num, int data) |
e37c9e69 | 155 | { |
e37c9e69 CM |
156 | int ret; |
157 | struct btrfs_block_group_cache *cache = *cache_ret; | |
158 | u64 last = max(search_start, cache->key.objectid); | |
f510cfec CM |
159 | u64 start = 0; |
160 | u64 end = 0; | |
e37c9e69 | 161 | |
e37c9e69 | 162 | again: |
54aa1f4d CM |
163 | ret = cache_block_group(root, cache); |
164 | if (ret) | |
165 | goto out; | |
e37c9e69 | 166 | while(1) { |
f510cfec CM |
167 | ret = find_first_extent_bit(&root->fs_info->free_space_cache, |
168 | last, &start, &end, EXTENT_DIRTY); | |
169 | if (ret) | |
e37c9e69 | 170 | goto out; |
f510cfec CM |
171 | |
172 | start = max(last, start); | |
173 | last = end + 1; | |
174 | if (end + 1 - start < num) | |
175 | continue; | |
176 | if (start + num > cache->key.objectid + cache->key.offset) | |
e37c9e69 | 177 | goto new_group; |
f510cfec | 178 | return start; |
e37c9e69 CM |
179 | } |
180 | out: | |
181 | return max(cache->last_alloc, search_start); | |
182 | ||
183 | new_group: | |
5276aeda CM |
184 | cache = btrfs_lookup_block_group(root->fs_info, |
185 | last + cache->key.offset - 1); | |
e37c9e69 CM |
186 | if (!cache) { |
187 | return max((*cache_ret)->last_alloc, search_start); | |
188 | } | |
189 | cache = btrfs_find_block_group(root, cache, | |
f510cfec | 190 | last + cache->key.offset - 1, data, 0); |
e37c9e69 CM |
191 | *cache_ret = cache; |
192 | goto again; | |
193 | } | |
194 | ||
84f54cfa CM |
195 | static u64 div_factor(u64 num, int factor) |
196 | { | |
197 | num *= factor; | |
198 | do_div(num, 10); | |
199 | return num; | |
200 | } | |
201 | ||
31f3c99b CM |
202 | struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root, |
203 | struct btrfs_block_group_cache | |
be744175 | 204 | *hint, u64 search_start, |
de428b63 | 205 | int data, int owner) |
cd1bc465 CM |
206 | { |
207 | struct btrfs_block_group_cache *cache[8]; | |
31f3c99b | 208 | struct btrfs_block_group_cache *found_group = NULL; |
cd1bc465 | 209 | struct btrfs_fs_info *info = root->fs_info; |
be744175 | 210 | struct radix_tree_root *radix; |
1e2677e0 | 211 | struct radix_tree_root *swap_radix; |
cd1bc465 | 212 | u64 used; |
31f3c99b CM |
213 | u64 last = 0; |
214 | u64 hint_last; | |
cd1bc465 CM |
215 | int i; |
216 | int ret; | |
31f3c99b | 217 | int full_search = 0; |
de428b63 | 218 | int factor = 8; |
1e2677e0 | 219 | int data_swap = 0; |
de428b63 CM |
220 | |
221 | if (!owner) | |
222 | factor = 5; | |
be744175 | 223 | |
1e2677e0 | 224 | if (data) { |
be744175 | 225 | radix = &info->block_group_data_radix; |
1e2677e0 CM |
226 | swap_radix = &info->block_group_radix; |
227 | } else { | |
be744175 | 228 | radix = &info->block_group_radix; |
1e2677e0 CM |
229 | swap_radix = &info->block_group_data_radix; |
230 | } | |
be744175 CM |
231 | |
232 | if (search_start) { | |
233 | struct btrfs_block_group_cache *shint; | |
5276aeda | 234 | shint = btrfs_lookup_block_group(info, search_start); |
e9fe395e | 235 | if (shint && shint->data == data) { |
be744175 CM |
236 | used = btrfs_block_group_used(&shint->item); |
237 | if (used + shint->pinned < | |
84f54cfa | 238 | div_factor(shint->key.offset, factor)) { |
be744175 CM |
239 | return shint; |
240 | } | |
241 | } | |
242 | } | |
243 | if (hint && hint->data == data) { | |
31f3c99b | 244 | used = btrfs_block_group_used(&hint->item); |
84f54cfa CM |
245 | if (used + hint->pinned < |
246 | div_factor(hint->key.offset, factor)) { | |
31f3c99b CM |
247 | return hint; |
248 | } | |
84f54cfa | 249 | if (used >= div_factor(hint->key.offset, 8)) { |
be744175 CM |
250 | radix_tree_tag_clear(radix, |
251 | hint->key.objectid + | |
252 | hint->key.offset - 1, | |
253 | BTRFS_BLOCK_GROUP_AVAIL); | |
254 | } | |
8d7be552 | 255 | last = hint->key.offset * 3; |
be744175 | 256 | if (hint->key.objectid >= last) |
e37c9e69 CM |
257 | last = max(search_start + hint->key.offset - 1, |
258 | hint->key.objectid - last); | |
be744175 CM |
259 | else |
260 | last = hint->key.objectid + hint->key.offset; | |
31f3c99b CM |
261 | hint_last = last; |
262 | } else { | |
e37c9e69 CM |
263 | if (hint) |
264 | hint_last = max(hint->key.objectid, search_start); | |
265 | else | |
266 | hint_last = search_start; | |
267 | ||
268 | last = hint_last; | |
31f3c99b | 269 | } |
cd1bc465 | 270 | while(1) { |
be744175 | 271 | ret = radix_tree_gang_lookup_tag(radix, (void **)cache, |
cd1bc465 | 272 | last, ARRAY_SIZE(cache), |
31f3c99b | 273 | BTRFS_BLOCK_GROUP_AVAIL); |
cd1bc465 CM |
274 | if (!ret) |
275 | break; | |
276 | for (i = 0; i < ret; i++) { | |
be08c1b9 CM |
277 | last = cache[i]->key.objectid + |
278 | cache[i]->key.offset; | |
cd1bc465 | 279 | used = btrfs_block_group_used(&cache[i]->item); |
be744175 | 280 | if (used + cache[i]->pinned < |
84f54cfa | 281 | div_factor(cache[i]->key.offset, factor)) { |
31f3c99b CM |
282 | found_group = cache[i]; |
283 | goto found; | |
cd1bc465 | 284 | } |
84f54cfa | 285 | if (used >= div_factor(cache[i]->key.offset, 8)) { |
be744175 CM |
286 | radix_tree_tag_clear(radix, |
287 | cache[i]->key.objectid + | |
288 | cache[i]->key.offset - 1, | |
289 | BTRFS_BLOCK_GROUP_AVAIL); | |
290 | } | |
cd1bc465 | 291 | } |
de428b63 | 292 | cond_resched(); |
cd1bc465 | 293 | } |
31f3c99b CM |
294 | last = hint_last; |
295 | again: | |
cd1bc465 | 296 | while(1) { |
be744175 CM |
297 | ret = radix_tree_gang_lookup(radix, (void **)cache, |
298 | last, ARRAY_SIZE(cache)); | |
cd1bc465 CM |
299 | if (!ret) |
300 | break; | |
301 | for (i = 0; i < ret; i++) { | |
be08c1b9 CM |
302 | last = cache[i]->key.objectid + |
303 | cache[i]->key.offset; | |
cd1bc465 | 304 | used = btrfs_block_group_used(&cache[i]->item); |
be744175 | 305 | if (used + cache[i]->pinned < cache[i]->key.offset) { |
31f3c99b CM |
306 | found_group = cache[i]; |
307 | goto found; | |
cd1bc465 | 308 | } |
be744175 CM |
309 | if (used >= cache[i]->key.offset) { |
310 | radix_tree_tag_clear(radix, | |
311 | cache[i]->key.objectid + | |
312 | cache[i]->key.offset - 1, | |
313 | BTRFS_BLOCK_GROUP_AVAIL); | |
314 | } | |
cd1bc465 | 315 | } |
de428b63 | 316 | cond_resched(); |
cd1bc465 | 317 | } |
31f3c99b | 318 | if (!full_search) { |
be744175 | 319 | last = search_start; |
31f3c99b CM |
320 | full_search = 1; |
321 | goto again; | |
322 | } | |
1e2677e0 CM |
323 | if (!data_swap) { |
324 | struct radix_tree_root *tmp = radix; | |
325 | data_swap = 1; | |
326 | radix = swap_radix; | |
327 | swap_radix = tmp; | |
328 | last = search_start; | |
329 | goto again; | |
330 | } | |
31f3c99b | 331 | if (!found_group) { |
be744175 | 332 | ret = radix_tree_gang_lookup(radix, |
31f3c99b | 333 | (void **)&found_group, 0, 1); |
1e2677e0 CM |
334 | if (ret == 0) { |
335 | ret = radix_tree_gang_lookup(swap_radix, | |
336 | (void **)&found_group, | |
337 | 0, 1); | |
338 | } | |
31f3c99b CM |
339 | BUG_ON(ret != 1); |
340 | } | |
be744175 | 341 | found: |
31f3c99b | 342 | return found_group; |
cd1bc465 CM |
343 | } |
344 | ||
b18c6685 CM |
345 | int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, |
346 | struct btrfs_root *root, | |
347 | u64 blocknr, u64 num_blocks) | |
02217ed2 | 348 | { |
5caf2a00 | 349 | struct btrfs_path *path; |
02217ed2 | 350 | int ret; |
e2fa7227 | 351 | struct btrfs_key key; |
5f39d397 | 352 | struct extent_buffer *l; |
234b63a0 | 353 | struct btrfs_extent_item *item; |
cf27e1ee | 354 | u32 refs; |
037e6390 | 355 | |
5caf2a00 | 356 | path = btrfs_alloc_path(); |
54aa1f4d CM |
357 | if (!path) |
358 | return -ENOMEM; | |
26b8003f | 359 | |
02217ed2 | 360 | key.objectid = blocknr; |
62e2749e | 361 | btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY); |
6407bf6d | 362 | key.offset = num_blocks; |
5caf2a00 | 363 | ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path, |
9f5fae2f | 364 | 0, 1); |
54aa1f4d CM |
365 | if (ret < 0) |
366 | return ret; | |
a429e513 | 367 | if (ret != 0) { |
a28ec197 | 368 | BUG(); |
a429e513 | 369 | } |
02217ed2 | 370 | BUG_ON(ret != 0); |
5f39d397 | 371 | l = path->nodes[0]; |
5caf2a00 | 372 | item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item); |
5f39d397 CM |
373 | refs = btrfs_extent_refs(l, item); |
374 | btrfs_set_extent_refs(l, item, refs + 1); | |
5caf2a00 | 375 | btrfs_mark_buffer_dirty(path->nodes[0]); |
a28ec197 | 376 | |
5caf2a00 CM |
377 | btrfs_release_path(root->fs_info->extent_root, path); |
378 | btrfs_free_path(path); | |
9f5fae2f | 379 | finish_current_insert(trans, root->fs_info->extent_root); |
e20d96d6 | 380 | del_pending_extents(trans, root->fs_info->extent_root); |
02217ed2 CM |
381 | return 0; |
382 | } | |
383 | ||
e9d0b13b CM |
384 | int btrfs_extent_post_op(struct btrfs_trans_handle *trans, |
385 | struct btrfs_root *root) | |
386 | { | |
387 | finish_current_insert(trans, root->fs_info->extent_root); | |
388 | del_pending_extents(trans, root->fs_info->extent_root); | |
389 | return 0; | |
390 | } | |
391 | ||
b18c6685 CM |
392 | static int lookup_extent_ref(struct btrfs_trans_handle *trans, |
393 | struct btrfs_root *root, u64 blocknr, | |
394 | u64 num_blocks, u32 *refs) | |
a28ec197 | 395 | { |
5caf2a00 | 396 | struct btrfs_path *path; |
a28ec197 | 397 | int ret; |
e2fa7227 | 398 | struct btrfs_key key; |
5f39d397 | 399 | struct extent_buffer *l; |
234b63a0 | 400 | struct btrfs_extent_item *item; |
5caf2a00 CM |
401 | |
402 | path = btrfs_alloc_path(); | |
a28ec197 | 403 | key.objectid = blocknr; |
6407bf6d | 404 | key.offset = num_blocks; |
62e2749e | 405 | btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY); |
5caf2a00 | 406 | ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path, |
9f5fae2f | 407 | 0, 0); |
54aa1f4d CM |
408 | if (ret < 0) |
409 | goto out; | |
5f39d397 CM |
410 | if (ret != 0) { |
411 | btrfs_print_leaf(root, path->nodes[0]); | |
412 | printk("failed to find block number %Lu\n", blocknr); | |
a28ec197 | 413 | BUG(); |
5f39d397 CM |
414 | } |
415 | l = path->nodes[0]; | |
5caf2a00 | 416 | item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item); |
5f39d397 | 417 | *refs = btrfs_extent_refs(l, item); |
54aa1f4d | 418 | out: |
5caf2a00 | 419 | btrfs_free_path(path); |
a28ec197 CM |
420 | return 0; |
421 | } | |
422 | ||
c5739bba CM |
423 | int btrfs_inc_root_ref(struct btrfs_trans_handle *trans, |
424 | struct btrfs_root *root) | |
425 | { | |
5f39d397 CM |
426 | return btrfs_inc_extent_ref(trans, root, |
427 | extent_buffer_blocknr(root->node), 1); | |
c5739bba CM |
428 | } |
429 | ||
e089f05c | 430 | int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
5f39d397 | 431 | struct extent_buffer *buf) |
02217ed2 CM |
432 | { |
433 | u64 blocknr; | |
5f39d397 CM |
434 | u32 nritems; |
435 | struct btrfs_key key; | |
6407bf6d | 436 | struct btrfs_file_extent_item *fi; |
02217ed2 | 437 | int i; |
6407bf6d CM |
438 | int leaf; |
439 | int ret; | |
54aa1f4d CM |
440 | int faili; |
441 | int err; | |
a28ec197 | 442 | |
3768f368 | 443 | if (!root->ref_cows) |
a28ec197 | 444 | return 0; |
5f39d397 CM |
445 | |
446 | leaf = btrfs_is_leaf(buf); | |
447 | nritems = btrfs_header_nritems(buf); | |
448 | for (i = 0; i < nritems; i++) { | |
6407bf6d | 449 | if (leaf) { |
3a686375 | 450 | u64 disk_blocknr; |
5f39d397 CM |
451 | btrfs_item_key_to_cpu(buf, &key, i); |
452 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) | |
6407bf6d | 453 | continue; |
5f39d397 | 454 | fi = btrfs_item_ptr(buf, i, |
6407bf6d | 455 | struct btrfs_file_extent_item); |
5f39d397 | 456 | if (btrfs_file_extent_type(buf, fi) == |
236454df CM |
457 | BTRFS_FILE_EXTENT_INLINE) |
458 | continue; | |
5f39d397 | 459 | disk_blocknr = btrfs_file_extent_disk_blocknr(buf, fi); |
3a686375 CM |
460 | if (disk_blocknr == 0) |
461 | continue; | |
462 | ret = btrfs_inc_extent_ref(trans, root, disk_blocknr, | |
5f39d397 | 463 | btrfs_file_extent_disk_num_blocks(buf, fi)); |
54aa1f4d CM |
464 | if (ret) { |
465 | faili = i; | |
466 | goto fail; | |
467 | } | |
6407bf6d | 468 | } else { |
5f39d397 | 469 | blocknr = btrfs_node_blockptr(buf, i); |
b18c6685 | 470 | ret = btrfs_inc_extent_ref(trans, root, blocknr, 1); |
54aa1f4d CM |
471 | if (ret) { |
472 | faili = i; | |
473 | goto fail; | |
474 | } | |
6407bf6d | 475 | } |
02217ed2 CM |
476 | } |
477 | return 0; | |
54aa1f4d | 478 | fail: |
ccd467d6 | 479 | WARN_ON(1); |
54aa1f4d CM |
480 | for (i =0; i < faili; i++) { |
481 | if (leaf) { | |
482 | u64 disk_blocknr; | |
5f39d397 CM |
483 | btrfs_item_key_to_cpu(buf, &key, i); |
484 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) | |
54aa1f4d | 485 | continue; |
5f39d397 | 486 | fi = btrfs_item_ptr(buf, i, |
54aa1f4d | 487 | struct btrfs_file_extent_item); |
5f39d397 | 488 | if (btrfs_file_extent_type(buf, fi) == |
54aa1f4d CM |
489 | BTRFS_FILE_EXTENT_INLINE) |
490 | continue; | |
5f39d397 | 491 | disk_blocknr = btrfs_file_extent_disk_blocknr(buf, fi); |
54aa1f4d CM |
492 | if (disk_blocknr == 0) |
493 | continue; | |
494 | err = btrfs_free_extent(trans, root, disk_blocknr, | |
5f39d397 CM |
495 | btrfs_file_extent_disk_num_blocks(buf, |
496 | fi), 0); | |
54aa1f4d CM |
497 | BUG_ON(err); |
498 | } else { | |
5f39d397 | 499 | blocknr = btrfs_node_blockptr(buf, i); |
54aa1f4d CM |
500 | err = btrfs_free_extent(trans, root, blocknr, 1, 0); |
501 | BUG_ON(err); | |
502 | } | |
503 | } | |
504 | return ret; | |
02217ed2 CM |
505 | } |
506 | ||
9078a3e1 CM |
507 | static int write_one_cache_group(struct btrfs_trans_handle *trans, |
508 | struct btrfs_root *root, | |
509 | struct btrfs_path *path, | |
510 | struct btrfs_block_group_cache *cache) | |
511 | { | |
512 | int ret; | |
513 | int pending_ret; | |
514 | struct btrfs_root *extent_root = root->fs_info->extent_root; | |
5f39d397 CM |
515 | unsigned long bi; |
516 | struct extent_buffer *leaf; | |
9078a3e1 | 517 | |
9078a3e1 | 518 | ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1); |
54aa1f4d CM |
519 | if (ret < 0) |
520 | goto fail; | |
9078a3e1 | 521 | BUG_ON(ret); |
5f39d397 CM |
522 | |
523 | leaf = path->nodes[0]; | |
524 | bi = btrfs_item_ptr_offset(leaf, path->slots[0]); | |
525 | write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item)); | |
526 | btrfs_mark_buffer_dirty(leaf); | |
9078a3e1 | 527 | btrfs_release_path(extent_root, path); |
54aa1f4d | 528 | fail: |
9078a3e1 CM |
529 | finish_current_insert(trans, extent_root); |
530 | pending_ret = del_pending_extents(trans, extent_root); | |
531 | if (ret) | |
532 | return ret; | |
533 | if (pending_ret) | |
534 | return pending_ret; | |
be744175 CM |
535 | if (cache->data) |
536 | cache->last_alloc = cache->first_free; | |
9078a3e1 CM |
537 | return 0; |
538 | ||
539 | } | |
540 | ||
be744175 CM |
541 | static int write_dirty_block_radix(struct btrfs_trans_handle *trans, |
542 | struct btrfs_root *root, | |
543 | struct radix_tree_root *radix) | |
9078a3e1 CM |
544 | { |
545 | struct btrfs_block_group_cache *cache[8]; | |
546 | int ret; | |
547 | int err = 0; | |
548 | int werr = 0; | |
9078a3e1 CM |
549 | int i; |
550 | struct btrfs_path *path; | |
54aa1f4d | 551 | unsigned long off = 0; |
9078a3e1 CM |
552 | |
553 | path = btrfs_alloc_path(); | |
554 | if (!path) | |
555 | return -ENOMEM; | |
556 | ||
557 | while(1) { | |
558 | ret = radix_tree_gang_lookup_tag(radix, (void **)cache, | |
54aa1f4d | 559 | off, ARRAY_SIZE(cache), |
9078a3e1 CM |
560 | BTRFS_BLOCK_GROUP_DIRTY); |
561 | if (!ret) | |
562 | break; | |
563 | for (i = 0; i < ret; i++) { | |
9078a3e1 CM |
564 | err = write_one_cache_group(trans, root, |
565 | path, cache[i]); | |
54aa1f4d CM |
566 | /* |
567 | * if we fail to write the cache group, we want | |
568 | * to keep it marked dirty in hopes that a later | |
569 | * write will work | |
570 | */ | |
571 | if (err) { | |
9078a3e1 | 572 | werr = err; |
54aa1f4d CM |
573 | off = cache[i]->key.objectid + |
574 | cache[i]->key.offset; | |
575 | continue; | |
576 | } | |
577 | ||
578 | radix_tree_tag_clear(radix, cache[i]->key.objectid + | |
579 | cache[i]->key.offset - 1, | |
580 | BTRFS_BLOCK_GROUP_DIRTY); | |
9078a3e1 CM |
581 | } |
582 | } | |
583 | btrfs_free_path(path); | |
584 | return werr; | |
585 | } | |
586 | ||
be744175 CM |
587 | int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans, |
588 | struct btrfs_root *root) | |
589 | { | |
590 | int ret; | |
591 | int ret2; | |
592 | ret = write_dirty_block_radix(trans, root, | |
593 | &root->fs_info->block_group_radix); | |
594 | ret2 = write_dirty_block_radix(trans, root, | |
595 | &root->fs_info->block_group_data_radix); | |
596 | if (ret) | |
597 | return ret; | |
598 | if (ret2) | |
599 | return ret2; | |
600 | return 0; | |
601 | } | |
602 | ||
9078a3e1 CM |
603 | static int update_block_group(struct btrfs_trans_handle *trans, |
604 | struct btrfs_root *root, | |
1e2677e0 CM |
605 | u64 blocknr, u64 num, int alloc, int mark_free, |
606 | int data) | |
9078a3e1 CM |
607 | { |
608 | struct btrfs_block_group_cache *cache; | |
609 | struct btrfs_fs_info *info = root->fs_info; | |
610 | u64 total = num; | |
611 | u64 old_val; | |
612 | u64 block_in_group; | |
1e2677e0 | 613 | int ret; |
3e1ad54f | 614 | |
9078a3e1 | 615 | while(total) { |
5276aeda | 616 | cache = btrfs_lookup_block_group(info, blocknr); |
3e1ad54f | 617 | if (!cache) { |
9078a3e1 | 618 | return -1; |
cd1bc465 | 619 | } |
9078a3e1 CM |
620 | block_in_group = blocknr - cache->key.objectid; |
621 | WARN_ON(block_in_group > cache->key.offset); | |
3e1ad54f | 622 | radix_tree_tag_set(cache->radix, cache->key.objectid + |
be744175 | 623 | cache->key.offset - 1, |
9078a3e1 CM |
624 | BTRFS_BLOCK_GROUP_DIRTY); |
625 | ||
626 | old_val = btrfs_block_group_used(&cache->item); | |
627 | num = min(total, cache->key.offset - block_in_group); | |
cd1bc465 | 628 | if (alloc) { |
cd1bc465 CM |
629 | if (blocknr > cache->last_alloc) |
630 | cache->last_alloc = blocknr; | |
1e2677e0 | 631 | if (cache->data != data && |
84f54cfa | 632 | old_val < (cache->key.offset >> 1)) { |
1e2677e0 CM |
633 | cache->data = data; |
634 | radix_tree_delete(cache->radix, | |
635 | cache->key.objectid + | |
636 | cache->key.offset - 1); | |
637 | ||
638 | if (data) { | |
639 | cache->radix = | |
640 | &info->block_group_data_radix; | |
641 | cache->item.flags |= | |
642 | BTRFS_BLOCK_GROUP_DATA; | |
643 | } else { | |
644 | cache->radix = &info->block_group_radix; | |
645 | cache->item.flags &= | |
646 | ~BTRFS_BLOCK_GROUP_DATA; | |
647 | } | |
648 | ret = radix_tree_insert(cache->radix, | |
649 | cache->key.objectid + | |
650 | cache->key.offset - 1, | |
651 | (void *)cache); | |
652 | } | |
653 | old_val += num; | |
cd1bc465 | 654 | } else { |
9078a3e1 | 655 | old_val -= num; |
cd1bc465 CM |
656 | if (blocknr < cache->first_free) |
657 | cache->first_free = blocknr; | |
f510cfec CM |
658 | if (mark_free) { |
659 | set_extent_dirty(&info->free_space_cache, | |
660 | blocknr, blocknr + num - 1, | |
661 | GFP_NOFS); | |
e37c9e69 | 662 | } |
84f54cfa CM |
663 | if (old_val < (cache->key.offset >> 1) && |
664 | old_val + num >= (cache->key.offset >> 1)) { | |
e37c9e69 CM |
665 | radix_tree_tag_set(cache->radix, |
666 | cache->key.objectid + | |
667 | cache->key.offset - 1, | |
668 | BTRFS_BLOCK_GROUP_AVAIL); | |
669 | } | |
cd1bc465 | 670 | } |
9078a3e1 | 671 | btrfs_set_block_group_used(&cache->item, old_val); |
e37c9e69 CM |
672 | total -= num; |
673 | blocknr += num; | |
9078a3e1 CM |
674 | } |
675 | return 0; | |
676 | } | |
677 | ||
ccd467d6 CM |
678 | int btrfs_copy_pinned(struct btrfs_root *root, struct radix_tree_root *copy) |
679 | { | |
680 | unsigned long gang[8]; | |
681 | u64 last = 0; | |
682 | struct radix_tree_root *pinned_radix = &root->fs_info->pinned_radix; | |
683 | int ret; | |
684 | int i; | |
685 | ||
686 | while(1) { | |
687 | ret = find_first_radix_bit(pinned_radix, gang, last, | |
688 | ARRAY_SIZE(gang)); | |
689 | if (!ret) | |
690 | break; | |
691 | for (i = 0 ; i < ret; i++) { | |
692 | set_radix_bit(copy, gang[i]); | |
693 | last = gang[i] + 1; | |
694 | } | |
695 | } | |
26b8003f CM |
696 | ret = find_first_radix_bit(&root->fs_info->extent_ins_radix, gang, 0, |
697 | ARRAY_SIZE(gang)); | |
698 | WARN_ON(ret); | |
ccd467d6 CM |
699 | return 0; |
700 | } | |
701 | ||
702 | int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, | |
703 | struct btrfs_root *root, | |
704 | struct radix_tree_root *unpin_radix) | |
a28ec197 | 705 | { |
8ef97622 | 706 | unsigned long gang[8]; |
be744175 | 707 | struct btrfs_block_group_cache *block_group; |
88fd146c | 708 | u64 first = 0; |
a28ec197 CM |
709 | int ret; |
710 | int i; | |
8ef97622 | 711 | struct radix_tree_root *pinned_radix = &root->fs_info->pinned_radix; |
f510cfec CM |
712 | struct extent_map_tree *free_space_cache; |
713 | ||
714 | free_space_cache = &root->fs_info->free_space_cache; | |
a28ec197 CM |
715 | |
716 | while(1) { | |
ccd467d6 | 717 | ret = find_first_radix_bit(unpin_radix, gang, 0, |
8ef97622 | 718 | ARRAY_SIZE(gang)); |
a28ec197 CM |
719 | if (!ret) |
720 | break; | |
88fd146c | 721 | if (!first) |
8ef97622 | 722 | first = gang[0]; |
0579da42 | 723 | for (i = 0; i < ret; i++) { |
8ef97622 | 724 | clear_radix_bit(pinned_radix, gang[i]); |
ccd467d6 | 725 | clear_radix_bit(unpin_radix, gang[i]); |
5276aeda CM |
726 | block_group = btrfs_lookup_block_group(root->fs_info, |
727 | gang[i]); | |
be744175 CM |
728 | if (block_group) { |
729 | WARN_ON(block_group->pinned == 0); | |
730 | block_group->pinned--; | |
731 | if (gang[i] < block_group->last_alloc) | |
732 | block_group->last_alloc = gang[i]; | |
f510cfec CM |
733 | if (!block_group->data) { |
734 | set_extent_dirty(free_space_cache, | |
735 | gang[i], gang[i], | |
736 | GFP_NOFS); | |
737 | } | |
be744175 | 738 | } |
0579da42 | 739 | } |
a28ec197 CM |
740 | } |
741 | return 0; | |
742 | } | |
743 | ||
e089f05c CM |
744 | static int finish_current_insert(struct btrfs_trans_handle *trans, struct |
745 | btrfs_root *extent_root) | |
037e6390 | 746 | { |
e2fa7227 | 747 | struct btrfs_key ins; |
234b63a0 | 748 | struct btrfs_extent_item extent_item; |
037e6390 CM |
749 | int i; |
750 | int ret; | |
26b8003f CM |
751 | int err; |
752 | unsigned long gang[8]; | |
1261ec42 | 753 | struct btrfs_fs_info *info = extent_root->fs_info; |
037e6390 | 754 | |
5f39d397 | 755 | btrfs_set_stack_extent_refs(&extent_item, 1); |
037e6390 | 756 | ins.offset = 1; |
62e2749e | 757 | btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY); |
5f39d397 CM |
758 | btrfs_set_stack_extent_owner(&extent_item, |
759 | extent_root->root_key.objectid); | |
037e6390 | 760 | |
26b8003f CM |
761 | while(1) { |
762 | ret = find_first_radix_bit(&info->extent_ins_radix, gang, 0, | |
763 | ARRAY_SIZE(gang)); | |
764 | if (!ret) | |
765 | break; | |
766 | ||
767 | for (i = 0; i < ret; i++) { | |
768 | ins.objectid = gang[i]; | |
769 | err = btrfs_insert_item(trans, extent_root, &ins, | |
770 | &extent_item, | |
771 | sizeof(extent_item)); | |
772 | clear_radix_bit(&info->extent_ins_radix, gang[i]); | |
773 | WARN_ON(err); | |
774 | } | |
037e6390 | 775 | } |
037e6390 CM |
776 | return 0; |
777 | } | |
778 | ||
8ef97622 | 779 | static int pin_down_block(struct btrfs_root *root, u64 blocknr, int pending) |
e20d96d6 CM |
780 | { |
781 | int err; | |
5f39d397 | 782 | struct extent_buffer *buf; |
8ef97622 | 783 | |
f4b9aa8d | 784 | if (!pending) { |
5f39d397 CM |
785 | buf = btrfs_find_tree_block(root, blocknr); |
786 | if (buf) { | |
787 | if (btrfs_buffer_uptodate(buf)) { | |
2c90e5d6 CM |
788 | u64 transid = |
789 | root->fs_info->running_transaction->transid; | |
5f39d397 CM |
790 | if (btrfs_header_generation(buf) == transid) { |
791 | free_extent_buffer(buf); | |
2c90e5d6 CM |
792 | return 0; |
793 | } | |
f4b9aa8d | 794 | } |
5f39d397 | 795 | free_extent_buffer(buf); |
8ef97622 | 796 | } |
8ef97622 | 797 | err = set_radix_bit(&root->fs_info->pinned_radix, blocknr); |
be744175 CM |
798 | if (!err) { |
799 | struct btrfs_block_group_cache *cache; | |
5276aeda CM |
800 | cache = btrfs_lookup_block_group(root->fs_info, |
801 | blocknr); | |
be744175 CM |
802 | if (cache) |
803 | cache->pinned++; | |
804 | } | |
f4b9aa8d CM |
805 | } else { |
806 | err = set_radix_bit(&root->fs_info->pending_del_radix, blocknr); | |
807 | } | |
be744175 | 808 | BUG_ON(err < 0); |
e20d96d6 CM |
809 | return 0; |
810 | } | |
811 | ||
fec577fb | 812 | /* |
a28ec197 | 813 | * remove an extent from the root, returns 0 on success |
fec577fb | 814 | */ |
e089f05c | 815 | static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root |
e37c9e69 CM |
816 | *root, u64 blocknr, u64 num_blocks, int pin, |
817 | int mark_free) | |
a28ec197 | 818 | { |
5caf2a00 | 819 | struct btrfs_path *path; |
e2fa7227 | 820 | struct btrfs_key key; |
1261ec42 CM |
821 | struct btrfs_fs_info *info = root->fs_info; |
822 | struct btrfs_root *extent_root = info->extent_root; | |
5f39d397 | 823 | struct extent_buffer *leaf; |
a28ec197 | 824 | int ret; |
234b63a0 | 825 | struct btrfs_extent_item *ei; |
cf27e1ee | 826 | u32 refs; |
037e6390 | 827 | |
a28ec197 | 828 | key.objectid = blocknr; |
62e2749e | 829 | btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY); |
a28ec197 CM |
830 | key.offset = num_blocks; |
831 | ||
5caf2a00 | 832 | path = btrfs_alloc_path(); |
54aa1f4d CM |
833 | if (!path) |
834 | return -ENOMEM; | |
5f26f772 | 835 | |
54aa1f4d CM |
836 | ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1); |
837 | if (ret < 0) | |
838 | return ret; | |
839 | BUG_ON(ret); | |
5f39d397 CM |
840 | |
841 | leaf = path->nodes[0]; | |
842 | ei = btrfs_item_ptr(leaf, path->slots[0], | |
123abc88 | 843 | struct btrfs_extent_item); |
5f39d397 CM |
844 | refs = btrfs_extent_refs(leaf, ei); |
845 | BUG_ON(refs == 0); | |
846 | refs -= 1; | |
847 | btrfs_set_extent_refs(leaf, ei, refs); | |
848 | btrfs_mark_buffer_dirty(leaf); | |
849 | ||
cf27e1ee | 850 | if (refs == 0) { |
58176a96 | 851 | u64 super_blocks_used, root_blocks_used; |
78fae27e CM |
852 | |
853 | if (pin) { | |
8ef97622 | 854 | ret = pin_down_block(root, blocknr, 0); |
78fae27e CM |
855 | BUG_ON(ret); |
856 | } | |
857 | ||
58176a96 | 858 | /* block accounting for super block */ |
4b52dff6 CM |
859 | super_blocks_used = btrfs_super_blocks_used(&info->super_copy); |
860 | btrfs_set_super_blocks_used(&info->super_copy, | |
1261ec42 | 861 | super_blocks_used - num_blocks); |
58176a96 JB |
862 | |
863 | /* block accounting for root item */ | |
5f39d397 CM |
864 | root_blocks_used = btrfs_root_used(&root->root_item); |
865 | btrfs_set_root_used(&root->root_item, | |
58176a96 JB |
866 | root_blocks_used - num_blocks); |
867 | ||
5caf2a00 | 868 | ret = btrfs_del_item(trans, extent_root, path); |
54aa1f4d CM |
869 | if (ret) { |
870 | return ret; | |
871 | } | |
e37c9e69 | 872 | ret = update_block_group(trans, root, blocknr, num_blocks, 0, |
1e2677e0 | 873 | mark_free, 0); |
9078a3e1 | 874 | BUG_ON(ret); |
a28ec197 | 875 | } |
5caf2a00 | 876 | btrfs_free_path(path); |
e089f05c | 877 | finish_current_insert(trans, extent_root); |
a28ec197 CM |
878 | return ret; |
879 | } | |
880 | ||
a28ec197 CM |
881 | /* |
882 | * find all the blocks marked as pending in the radix tree and remove | |
883 | * them from the extent map | |
884 | */ | |
e089f05c CM |
885 | static int del_pending_extents(struct btrfs_trans_handle *trans, struct |
886 | btrfs_root *extent_root) | |
a28ec197 CM |
887 | { |
888 | int ret; | |
e20d96d6 CM |
889 | int wret; |
890 | int err = 0; | |
8ef97622 | 891 | unsigned long gang[4]; |
a28ec197 | 892 | int i; |
8ef97622 CM |
893 | struct radix_tree_root *pending_radix; |
894 | struct radix_tree_root *pinned_radix; | |
be744175 | 895 | struct btrfs_block_group_cache *cache; |
8ef97622 CM |
896 | |
897 | pending_radix = &extent_root->fs_info->pending_del_radix; | |
898 | pinned_radix = &extent_root->fs_info->pinned_radix; | |
a28ec197 CM |
899 | |
900 | while(1) { | |
e37c9e69 | 901 | ret = find_first_radix_bit(pending_radix, gang, 0, |
8ef97622 | 902 | ARRAY_SIZE(gang)); |
a28ec197 CM |
903 | if (!ret) |
904 | break; | |
905 | for (i = 0; i < ret; i++) { | |
8ef97622 | 906 | wret = set_radix_bit(pinned_radix, gang[i]); |
be744175 | 907 | if (wret == 0) { |
5276aeda CM |
908 | cache = |
909 | btrfs_lookup_block_group(extent_root->fs_info, | |
be744175 CM |
910 | gang[i]); |
911 | if (cache) | |
912 | cache->pinned++; | |
913 | } | |
914 | if (wret < 0) { | |
915 | printk(KERN_CRIT "set_radix_bit, err %d\n", | |
916 | wret); | |
917 | BUG_ON(wret < 0); | |
918 | } | |
8ef97622 CM |
919 | wret = clear_radix_bit(pending_radix, gang[i]); |
920 | BUG_ON(wret); | |
d5719762 | 921 | wret = __free_extent(trans, extent_root, |
e37c9e69 | 922 | gang[i], 1, 0, 0); |
e20d96d6 CM |
923 | if (wret) |
924 | err = wret; | |
fec577fb CM |
925 | } |
926 | } | |
e20d96d6 | 927 | return err; |
fec577fb CM |
928 | } |
929 | ||
930 | /* | |
931 | * remove an extent from the root, returns 0 on success | |
932 | */ | |
e089f05c CM |
933 | int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root |
934 | *root, u64 blocknr, u64 num_blocks, int pin) | |
fec577fb | 935 | { |
9f5fae2f | 936 | struct btrfs_root *extent_root = root->fs_info->extent_root; |
fec577fb CM |
937 | int pending_ret; |
938 | int ret; | |
a28ec197 | 939 | |
fec577fb | 940 | if (root == extent_root) { |
8ef97622 | 941 | pin_down_block(root, blocknr, 1); |
fec577fb CM |
942 | return 0; |
943 | } | |
e37c9e69 | 944 | ret = __free_extent(trans, root, blocknr, num_blocks, pin, pin == 0); |
e20d96d6 | 945 | pending_ret = del_pending_extents(trans, root->fs_info->extent_root); |
fec577fb CM |
946 | return ret ? ret : pending_ret; |
947 | } | |
948 | ||
949 | /* | |
950 | * walks the btree of allocated extents and find a hole of a given size. | |
951 | * The key ins is changed to record the hole: | |
952 | * ins->objectid == block start | |
62e2749e | 953 | * ins->flags = BTRFS_EXTENT_ITEM_KEY |
fec577fb CM |
954 | * ins->offset == number of blocks |
955 | * Any available blocks before search_start are skipped. | |
956 | */ | |
e089f05c | 957 | static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root |
6702ed49 CM |
958 | *orig_root, u64 num_blocks, u64 empty_size, |
959 | u64 search_start, u64 search_end, u64 hint_block, | |
f2654de4 CM |
960 | struct btrfs_key *ins, u64 exclude_start, |
961 | u64 exclude_nr, int data) | |
fec577fb | 962 | { |
5caf2a00 | 963 | struct btrfs_path *path; |
e2fa7227 | 964 | struct btrfs_key key; |
fec577fb CM |
965 | int ret; |
966 | u64 hole_size = 0; | |
967 | int slot = 0; | |
e20d96d6 | 968 | u64 last_block = 0; |
037e6390 | 969 | u64 test_block; |
be744175 | 970 | u64 orig_search_start = search_start; |
fec577fb | 971 | int start_found; |
5f39d397 | 972 | struct extent_buffer *l; |
9f5fae2f | 973 | struct btrfs_root * root = orig_root->fs_info->extent_root; |
f2458e1d | 974 | struct btrfs_fs_info *info = root->fs_info; |
0579da42 | 975 | int total_needed = num_blocks; |
e20d96d6 | 976 | int level; |
be08c1b9 | 977 | struct btrfs_block_group_cache *block_group; |
be744175 | 978 | int full_scan = 0; |
fbdc762b | 979 | int wrapped = 0; |
f510cfec | 980 | u64 cached_search_start = 0; |
fec577fb | 981 | |
26b8003f | 982 | WARN_ON(num_blocks < 1); |
b1a4d965 CM |
983 | btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY); |
984 | ||
5f39d397 CM |
985 | level = btrfs_header_level(root->node); |
986 | ||
3e1ad54f | 987 | if (search_end == (u64)-1) |
4b52dff6 | 988 | search_end = btrfs_super_total_blocks(&info->super_copy); |
fbdc762b | 989 | if (hint_block) { |
5276aeda | 990 | block_group = btrfs_lookup_block_group(info, hint_block); |
be744175 | 991 | block_group = btrfs_find_block_group(root, block_group, |
fbdc762b | 992 | hint_block, data, 1); |
be744175 CM |
993 | } else { |
994 | block_group = btrfs_find_block_group(root, | |
995 | trans->block_group, 0, | |
de428b63 | 996 | data, 1); |
be744175 CM |
997 | } |
998 | ||
6702ed49 | 999 | total_needed += empty_size; |
e011599b CM |
1000 | path = btrfs_alloc_path(); |
1001 | ||
be744175 | 1002 | check_failed: |
f510cfec CM |
1003 | search_start = find_search_start(root, &block_group, |
1004 | search_start, total_needed, data); | |
1005 | cached_search_start = search_start; | |
e37c9e69 | 1006 | |
5caf2a00 | 1007 | btrfs_init_path(path); |
fec577fb CM |
1008 | ins->objectid = search_start; |
1009 | ins->offset = 0; | |
fec577fb | 1010 | start_found = 0; |
2cc58cf2 | 1011 | path->reada = 2; |
e37c9e69 | 1012 | |
5caf2a00 | 1013 | ret = btrfs_search_slot(trans, root, ins, path, 0, 0); |
0f70abe2 CM |
1014 | if (ret < 0) |
1015 | goto error; | |
aa5d6bed | 1016 | |
e37c9e69 | 1017 | if (path->slots[0] > 0) { |
5caf2a00 | 1018 | path->slots[0]--; |
e37c9e69 CM |
1019 | } |
1020 | ||
5f39d397 CM |
1021 | l = path->nodes[0]; |
1022 | btrfs_item_key_to_cpu(l, &key, path->slots[0]); | |
1023 | ||
e37c9e69 CM |
1024 | /* |
1025 | * a rare case, go back one key if we hit a block group item | |
1026 | * instead of an extent item | |
1027 | */ | |
1028 | if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY && | |
1029 | key.objectid + key.offset >= search_start) { | |
1030 | ins->objectid = key.objectid; | |
1031 | ins->offset = key.offset - 1; | |
1032 | btrfs_release_path(root, path); | |
1033 | ret = btrfs_search_slot(trans, root, ins, path, 0, 0); | |
1034 | if (ret < 0) | |
1035 | goto error; | |
1036 | ||
1037 | if (path->slots[0] > 0) { | |
1038 | path->slots[0]--; | |
1039 | } | |
1040 | } | |
0579da42 | 1041 | |
fec577fb | 1042 | while (1) { |
5f39d397 | 1043 | l = path->nodes[0]; |
5caf2a00 | 1044 | slot = path->slots[0]; |
5f39d397 | 1045 | if (slot >= btrfs_header_nritems(l)) { |
5caf2a00 | 1046 | ret = btrfs_next_leaf(root, path); |
fec577fb CM |
1047 | if (ret == 0) |
1048 | continue; | |
0f70abe2 CM |
1049 | if (ret < 0) |
1050 | goto error; | |
fec577fb CM |
1051 | if (!start_found) { |
1052 | ins->objectid = search_start; | |
3e1ad54f | 1053 | ins->offset = search_end - search_start; |
fec577fb CM |
1054 | start_found = 1; |
1055 | goto check_pending; | |
1056 | } | |
1057 | ins->objectid = last_block > search_start ? | |
1058 | last_block : search_start; | |
3e1ad54f | 1059 | ins->offset = search_end - ins->objectid; |
fec577fb CM |
1060 | goto check_pending; |
1061 | } | |
e37c9e69 | 1062 | |
5f39d397 | 1063 | btrfs_item_key_to_cpu(l, &key, slot); |
e37c9e69 CM |
1064 | if (key.objectid >= search_start && key.objectid > last_block && |
1065 | start_found) { | |
1066 | if (last_block < search_start) | |
1067 | last_block = search_start; | |
1068 | hole_size = key.objectid - last_block; | |
1069 | if (hole_size >= num_blocks) { | |
1070 | ins->objectid = last_block; | |
1071 | ins->offset = hole_size; | |
1072 | goto check_pending; | |
0579da42 | 1073 | } |
fec577fb | 1074 | } |
e37c9e69 CM |
1075 | |
1076 | if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY) | |
1077 | goto next; | |
1078 | ||
0579da42 | 1079 | start_found = 1; |
e2fa7227 | 1080 | last_block = key.objectid + key.offset; |
f510cfec | 1081 | |
fbdc762b | 1082 | if (!full_scan && last_block >= block_group->key.objectid + |
be744175 CM |
1083 | block_group->key.offset) { |
1084 | btrfs_release_path(root, path); | |
1085 | search_start = block_group->key.objectid + | |
1086 | block_group->key.offset * 2; | |
1087 | goto new_group; | |
1088 | } | |
9078a3e1 | 1089 | next: |
5caf2a00 | 1090 | path->slots[0]++; |
de428b63 | 1091 | cond_resched(); |
fec577fb | 1092 | } |
fec577fb CM |
1093 | check_pending: |
1094 | /* we have to make sure we didn't find an extent that has already | |
1095 | * been allocated by the map tree or the original allocation | |
1096 | */ | |
5caf2a00 | 1097 | btrfs_release_path(root, path); |
fec577fb | 1098 | BUG_ON(ins->objectid < search_start); |
e37c9e69 | 1099 | |
cf67582b CM |
1100 | if (ins->objectid + num_blocks >= search_end) |
1101 | goto enospc; | |
1102 | ||
037e6390 | 1103 | for (test_block = ins->objectid; |
f2458e1d | 1104 | test_block < ins->objectid + num_blocks; test_block++) { |
26b8003f CM |
1105 | if (test_radix_bit(&info->pinned_radix, test_block) || |
1106 | test_radix_bit(&info->extent_ins_radix, test_block)) { | |
037e6390 | 1107 | search_start = test_block + 1; |
be744175 | 1108 | goto new_group; |
fec577fb CM |
1109 | } |
1110 | } | |
f2654de4 CM |
1111 | if (exclude_nr > 0 && (ins->objectid + num_blocks > exclude_start && |
1112 | ins->objectid < exclude_start + exclude_nr)) { | |
1113 | search_start = exclude_start + exclude_nr; | |
1114 | goto new_group; | |
1115 | } | |
e37c9e69 | 1116 | if (!data) { |
5276aeda | 1117 | block_group = btrfs_lookup_block_group(info, ins->objectid); |
26b8003f CM |
1118 | if (block_group) |
1119 | trans->block_group = block_group; | |
f2458e1d | 1120 | } |
037e6390 | 1121 | ins->offset = num_blocks; |
5caf2a00 | 1122 | btrfs_free_path(path); |
f510cfec CM |
1123 | if (0 && ins->objectid != cached_search_start) { |
1124 | printk("\tcached was %Lu found %Lu\n", cached_search_start, ins->objectid); | |
1125 | } | |
fec577fb | 1126 | return 0; |
be744175 CM |
1127 | |
1128 | new_group: | |
3e1ad54f | 1129 | if (search_start + num_blocks >= search_end) { |
cf67582b | 1130 | enospc: |
be744175 | 1131 | search_start = orig_search_start; |
fbdc762b CM |
1132 | if (full_scan) { |
1133 | ret = -ENOSPC; | |
1134 | goto error; | |
1135 | } | |
6702ed49 CM |
1136 | if (wrapped) { |
1137 | if (!full_scan) | |
1138 | total_needed -= empty_size; | |
fbdc762b | 1139 | full_scan = 1; |
6702ed49 | 1140 | } else |
fbdc762b | 1141 | wrapped = 1; |
be744175 | 1142 | } |
5276aeda | 1143 | block_group = btrfs_lookup_block_group(info, search_start); |
fbdc762b | 1144 | cond_resched(); |
be744175 CM |
1145 | if (!full_scan) |
1146 | block_group = btrfs_find_block_group(root, block_group, | |
de428b63 | 1147 | search_start, data, 0); |
be744175 CM |
1148 | goto check_failed; |
1149 | ||
0f70abe2 | 1150 | error: |
5caf2a00 CM |
1151 | btrfs_release_path(root, path); |
1152 | btrfs_free_path(path); | |
0f70abe2 | 1153 | return ret; |
fec577fb | 1154 | } |
fec577fb CM |
1155 | /* |
1156 | * finds a free extent and does all the dirty work required for allocation | |
1157 | * returns the key for the extent through ins, and a tree buffer for | |
1158 | * the first block of the extent through buf. | |
1159 | * | |
1160 | * returns 0 if everything worked, non-zero otherwise. | |
1161 | */ | |
4d775673 CM |
1162 | int btrfs_alloc_extent(struct btrfs_trans_handle *trans, |
1163 | struct btrfs_root *root, u64 owner, | |
6702ed49 | 1164 | u64 num_blocks, u64 empty_size, u64 hint_block, |
be08c1b9 | 1165 | u64 search_end, struct btrfs_key *ins, int data) |
fec577fb CM |
1166 | { |
1167 | int ret; | |
1168 | int pending_ret; | |
58176a96 | 1169 | u64 super_blocks_used, root_blocks_used; |
fbdc762b | 1170 | u64 search_start = 0; |
1261ec42 CM |
1171 | struct btrfs_fs_info *info = root->fs_info; |
1172 | struct btrfs_root *extent_root = info->extent_root; | |
234b63a0 | 1173 | struct btrfs_extent_item extent_item; |
037e6390 | 1174 | |
5f39d397 CM |
1175 | btrfs_set_stack_extent_refs(&extent_item, 1); |
1176 | btrfs_set_stack_extent_owner(&extent_item, owner); | |
fec577fb | 1177 | |
26b8003f | 1178 | WARN_ON(num_blocks < 1); |
6702ed49 CM |
1179 | ret = find_free_extent(trans, root, num_blocks, empty_size, |
1180 | search_start, search_end, hint_block, ins, | |
26b8003f CM |
1181 | trans->alloc_exclude_start, |
1182 | trans->alloc_exclude_nr, data); | |
ccd467d6 | 1183 | BUG_ON(ret); |
f2654de4 CM |
1184 | if (ret) |
1185 | return ret; | |
fec577fb | 1186 | |
58176a96 | 1187 | /* block accounting for super block */ |
4b52dff6 CM |
1188 | super_blocks_used = btrfs_super_blocks_used(&info->super_copy); |
1189 | btrfs_set_super_blocks_used(&info->super_copy, super_blocks_used + | |
1261ec42 | 1190 | num_blocks); |
26b8003f | 1191 | |
58176a96 | 1192 | /* block accounting for root item */ |
5f39d397 CM |
1193 | root_blocks_used = btrfs_root_used(&root->root_item); |
1194 | btrfs_set_root_used(&root->root_item, root_blocks_used + | |
58176a96 JB |
1195 | num_blocks); |
1196 | ||
f510cfec CM |
1197 | clear_extent_dirty(&root->fs_info->free_space_cache, |
1198 | ins->objectid, ins->objectid + ins->offset - 1, | |
1199 | GFP_NOFS); | |
1200 | ||
26b8003f CM |
1201 | if (root == extent_root) { |
1202 | BUG_ON(num_blocks != 1); | |
1203 | set_radix_bit(&root->fs_info->extent_ins_radix, ins->objectid); | |
1204 | goto update_block; | |
1205 | } | |
1206 | ||
1207 | WARN_ON(trans->alloc_exclude_nr); | |
1208 | trans->alloc_exclude_start = ins->objectid; | |
1209 | trans->alloc_exclude_nr = ins->offset; | |
e089f05c CM |
1210 | ret = btrfs_insert_item(trans, extent_root, ins, &extent_item, |
1211 | sizeof(extent_item)); | |
037e6390 | 1212 | |
26b8003f CM |
1213 | trans->alloc_exclude_start = 0; |
1214 | trans->alloc_exclude_nr = 0; | |
1215 | ||
ccd467d6 | 1216 | BUG_ON(ret); |
e089f05c | 1217 | finish_current_insert(trans, extent_root); |
e20d96d6 | 1218 | pending_ret = del_pending_extents(trans, extent_root); |
f510cfec | 1219 | |
e37c9e69 | 1220 | if (ret) { |
037e6390 | 1221 | return ret; |
e37c9e69 CM |
1222 | } |
1223 | if (pending_ret) { | |
037e6390 | 1224 | return pending_ret; |
e37c9e69 | 1225 | } |
26b8003f CM |
1226 | |
1227 | update_block: | |
1e2677e0 CM |
1228 | ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0, |
1229 | data); | |
fabb5681 | 1230 | BUG_ON(ret); |
037e6390 | 1231 | return 0; |
fec577fb CM |
1232 | } |
1233 | ||
1234 | /* | |
1235 | * helper function to allocate a block for a given tree | |
1236 | * returns the tree buffer or NULL. | |
1237 | */ | |
5f39d397 CM |
1238 | struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans, |
1239 | struct btrfs_root *root, u64 hint, | |
1240 | u64 empty_size) | |
fec577fb | 1241 | { |
e2fa7227 | 1242 | struct btrfs_key ins; |
fec577fb | 1243 | int ret; |
5f39d397 | 1244 | struct extent_buffer *buf; |
fec577fb | 1245 | |
4d775673 | 1246 | ret = btrfs_alloc_extent(trans, root, root->root_key.objectid, |
e9fe395e | 1247 | 1, empty_size, hint, (u64)-1, &ins, 0); |
fec577fb | 1248 | if (ret) { |
54aa1f4d CM |
1249 | BUG_ON(ret > 0); |
1250 | return ERR_PTR(ret); | |
fec577fb | 1251 | } |
d98237b3 | 1252 | buf = btrfs_find_create_tree_block(root, ins.objectid); |
54aa1f4d CM |
1253 | if (!buf) { |
1254 | btrfs_free_extent(trans, root, ins.objectid, 1, 0); | |
1255 | return ERR_PTR(-ENOMEM); | |
1256 | } | |
5f39d397 | 1257 | btrfs_set_buffer_uptodate(buf); |
f510cfec | 1258 | buf->alloc_addr = (unsigned long)__builtin_return_address(0); |
5f39d397 CM |
1259 | set_extent_dirty(&trans->transaction->dirty_pages, buf->start, |
1260 | buf->start + buf->len - 1, GFP_NOFS); | |
1261 | /* | |
090d1875 | 1262 | set_buffer_checked(buf); |
f2183bde | 1263 | set_buffer_defrag(buf); |
5f39d397 CM |
1264 | */ |
1265 | /* FIXME!!!!!!!!!!!!!!!! | |
1266 | set_radix_bit(&trans->transaction->dirty_pages, buf->pages[0]->index); | |
1267 | */ | |
d3c2fdcf | 1268 | trans->blocks_used++; |
fec577fb CM |
1269 | return buf; |
1270 | } | |
a28ec197 | 1271 | |
6407bf6d | 1272 | static int drop_leaf_ref(struct btrfs_trans_handle *trans, |
5f39d397 | 1273 | struct btrfs_root *root, struct extent_buffer *leaf) |
6407bf6d | 1274 | { |
5f39d397 | 1275 | struct btrfs_key key; |
6407bf6d CM |
1276 | struct btrfs_file_extent_item *fi; |
1277 | int i; | |
1278 | int nritems; | |
1279 | int ret; | |
1280 | ||
5f39d397 CM |
1281 | BUG_ON(!btrfs_is_leaf(leaf)); |
1282 | nritems = btrfs_header_nritems(leaf); | |
6407bf6d | 1283 | for (i = 0; i < nritems; i++) { |
3a686375 | 1284 | u64 disk_blocknr; |
5f39d397 CM |
1285 | |
1286 | btrfs_item_key_to_cpu(leaf, &key, i); | |
1287 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) | |
6407bf6d CM |
1288 | continue; |
1289 | fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item); | |
5f39d397 CM |
1290 | if (btrfs_file_extent_type(leaf, fi) == |
1291 | BTRFS_FILE_EXTENT_INLINE) | |
236454df | 1292 | continue; |
6407bf6d CM |
1293 | /* |
1294 | * FIXME make sure to insert a trans record that | |
1295 | * repeats the snapshot del on crash | |
1296 | */ | |
5f39d397 | 1297 | disk_blocknr = btrfs_file_extent_disk_blocknr(leaf, fi); |
3a686375 CM |
1298 | if (disk_blocknr == 0) |
1299 | continue; | |
1300 | ret = btrfs_free_extent(trans, root, disk_blocknr, | |
5f39d397 | 1301 | btrfs_file_extent_disk_num_blocks(leaf, fi), 0); |
6407bf6d CM |
1302 | BUG_ON(ret); |
1303 | } | |
1304 | return 0; | |
1305 | } | |
1306 | ||
e011599b | 1307 | static void reada_walk_down(struct btrfs_root *root, |
5f39d397 | 1308 | struct extent_buffer *node) |
e011599b CM |
1309 | { |
1310 | int i; | |
1311 | u32 nritems; | |
1312 | u64 blocknr; | |
1313 | int ret; | |
1314 | u32 refs; | |
1315 | ||
5f39d397 | 1316 | nritems = btrfs_header_nritems(node); |
e011599b CM |
1317 | for (i = 0; i < nritems; i++) { |
1318 | blocknr = btrfs_node_blockptr(node, i); | |
1319 | ret = lookup_extent_ref(NULL, root, blocknr, 1, &refs); | |
1320 | BUG_ON(ret); | |
1321 | if (refs != 1) | |
1322 | continue; | |
409eb95d | 1323 | mutex_unlock(&root->fs_info->fs_mutex); |
e011599b | 1324 | ret = readahead_tree_block(root, blocknr); |
409eb95d CM |
1325 | cond_resched(); |
1326 | mutex_lock(&root->fs_info->fs_mutex); | |
e011599b CM |
1327 | if (ret) |
1328 | break; | |
1329 | } | |
1330 | } | |
1331 | ||
9aca1d51 CM |
1332 | /* |
1333 | * helper function for drop_snapshot, this walks down the tree dropping ref | |
1334 | * counts as it goes. | |
1335 | */ | |
e089f05c CM |
1336 | static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root |
1337 | *root, struct btrfs_path *path, int *level) | |
20524f02 | 1338 | { |
5f39d397 CM |
1339 | struct extent_buffer *next; |
1340 | struct extent_buffer *cur; | |
20524f02 CM |
1341 | u64 blocknr; |
1342 | int ret; | |
1343 | u32 refs; | |
1344 | ||
5caf2a00 CM |
1345 | WARN_ON(*level < 0); |
1346 | WARN_ON(*level >= BTRFS_MAX_LEVEL); | |
5f39d397 CM |
1347 | ret = lookup_extent_ref(trans, root, |
1348 | extent_buffer_blocknr(path->nodes[*level]), | |
1349 | 1, &refs); | |
20524f02 CM |
1350 | BUG_ON(ret); |
1351 | if (refs > 1) | |
1352 | goto out; | |
e011599b | 1353 | |
9aca1d51 CM |
1354 | /* |
1355 | * walk down to the last node level and free all the leaves | |
1356 | */ | |
6407bf6d | 1357 | while(*level >= 0) { |
5caf2a00 CM |
1358 | WARN_ON(*level < 0); |
1359 | WARN_ON(*level >= BTRFS_MAX_LEVEL); | |
20524f02 | 1360 | cur = path->nodes[*level]; |
e011599b CM |
1361 | |
1362 | if (*level > 0 && path->slots[*level] == 0) | |
5f39d397 | 1363 | reada_walk_down(root, cur); |
e011599b | 1364 | |
5f39d397 | 1365 | if (btrfs_header_level(cur) != *level) |
2c90e5d6 | 1366 | WARN_ON(1); |
e011599b | 1367 | |
7518a238 | 1368 | if (path->slots[*level] >= |
5f39d397 | 1369 | btrfs_header_nritems(cur)) |
20524f02 | 1370 | break; |
6407bf6d CM |
1371 | if (*level == 0) { |
1372 | ret = drop_leaf_ref(trans, root, cur); | |
1373 | BUG_ON(ret); | |
1374 | break; | |
1375 | } | |
5f39d397 | 1376 | blocknr = btrfs_node_blockptr(cur, path->slots[*level]); |
b18c6685 | 1377 | ret = lookup_extent_ref(trans, root, blocknr, 1, &refs); |
6407bf6d CM |
1378 | BUG_ON(ret); |
1379 | if (refs != 1) { | |
20524f02 | 1380 | path->slots[*level]++; |
e089f05c | 1381 | ret = btrfs_free_extent(trans, root, blocknr, 1, 1); |
20524f02 CM |
1382 | BUG_ON(ret); |
1383 | continue; | |
1384 | } | |
e9d0b13b | 1385 | next = btrfs_find_tree_block(root, blocknr); |
5f39d397 CM |
1386 | if (!next || !btrfs_buffer_uptodate(next)) { |
1387 | free_extent_buffer(next); | |
e9d0b13b CM |
1388 | mutex_unlock(&root->fs_info->fs_mutex); |
1389 | next = read_tree_block(root, blocknr); | |
1390 | mutex_lock(&root->fs_info->fs_mutex); | |
1391 | ||
1392 | /* we dropped the lock, check one more time */ | |
1393 | ret = lookup_extent_ref(trans, root, blocknr, 1, &refs); | |
1394 | BUG_ON(ret); | |
1395 | if (refs != 1) { | |
1396 | path->slots[*level]++; | |
5f39d397 | 1397 | free_extent_buffer(next); |
e9d0b13b CM |
1398 | ret = btrfs_free_extent(trans, root, |
1399 | blocknr, 1, 1); | |
1400 | BUG_ON(ret); | |
1401 | continue; | |
1402 | } | |
1403 | } | |
5caf2a00 | 1404 | WARN_ON(*level <= 0); |
83e15a28 | 1405 | if (path->nodes[*level-1]) |
5f39d397 | 1406 | free_extent_buffer(path->nodes[*level-1]); |
20524f02 | 1407 | path->nodes[*level-1] = next; |
5f39d397 | 1408 | *level = btrfs_header_level(next); |
20524f02 CM |
1409 | path->slots[*level] = 0; |
1410 | } | |
1411 | out: | |
5caf2a00 CM |
1412 | WARN_ON(*level < 0); |
1413 | WARN_ON(*level >= BTRFS_MAX_LEVEL); | |
6407bf6d | 1414 | ret = btrfs_free_extent(trans, root, |
5f39d397 CM |
1415 | extent_buffer_blocknr(path->nodes[*level]), 1, 1); |
1416 | free_extent_buffer(path->nodes[*level]); | |
20524f02 CM |
1417 | path->nodes[*level] = NULL; |
1418 | *level += 1; | |
1419 | BUG_ON(ret); | |
1420 | return 0; | |
1421 | } | |
1422 | ||
9aca1d51 CM |
1423 | /* |
1424 | * helper for dropping snapshots. This walks back up the tree in the path | |
1425 | * to find the first node higher up where we haven't yet gone through | |
1426 | * all the slots | |
1427 | */ | |
e089f05c CM |
1428 | static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root |
1429 | *root, struct btrfs_path *path, int *level) | |
20524f02 CM |
1430 | { |
1431 | int i; | |
1432 | int slot; | |
1433 | int ret; | |
9f3a7427 CM |
1434 | struct btrfs_root_item *root_item = &root->root_item; |
1435 | ||
234b63a0 | 1436 | for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) { |
20524f02 | 1437 | slot = path->slots[i]; |
5f39d397 CM |
1438 | if (slot < btrfs_header_nritems(path->nodes[i]) - 1) { |
1439 | struct extent_buffer *node; | |
1440 | struct btrfs_disk_key disk_key; | |
1441 | node = path->nodes[i]; | |
20524f02 CM |
1442 | path->slots[i]++; |
1443 | *level = i; | |
9f3a7427 | 1444 | WARN_ON(*level == 0); |
5f39d397 | 1445 | btrfs_node_key(node, &disk_key, path->slots[i]); |
9f3a7427 | 1446 | memcpy(&root_item->drop_progress, |
5f39d397 | 1447 | &disk_key, sizeof(disk_key)); |
9f3a7427 | 1448 | root_item->drop_level = i; |
20524f02 CM |
1449 | return 0; |
1450 | } else { | |
e089f05c | 1451 | ret = btrfs_free_extent(trans, root, |
5f39d397 CM |
1452 | extent_buffer_blocknr(path->nodes[*level]), |
1453 | 1, 1); | |
6407bf6d | 1454 | BUG_ON(ret); |
5f39d397 | 1455 | free_extent_buffer(path->nodes[*level]); |
83e15a28 | 1456 | path->nodes[*level] = NULL; |
20524f02 | 1457 | *level = i + 1; |
20524f02 CM |
1458 | } |
1459 | } | |
1460 | return 1; | |
1461 | } | |
1462 | ||
9aca1d51 CM |
1463 | /* |
1464 | * drop the reference count on the tree rooted at 'snap'. This traverses | |
1465 | * the tree freeing any blocks that have a ref count of zero after being | |
1466 | * decremented. | |
1467 | */ | |
e089f05c | 1468 | int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root |
9f3a7427 | 1469 | *root) |
20524f02 | 1470 | { |
3768f368 | 1471 | int ret = 0; |
9aca1d51 | 1472 | int wret; |
20524f02 | 1473 | int level; |
5caf2a00 | 1474 | struct btrfs_path *path; |
20524f02 CM |
1475 | int i; |
1476 | int orig_level; | |
9f3a7427 | 1477 | struct btrfs_root_item *root_item = &root->root_item; |
20524f02 | 1478 | |
5caf2a00 CM |
1479 | path = btrfs_alloc_path(); |
1480 | BUG_ON(!path); | |
20524f02 | 1481 | |
5f39d397 | 1482 | level = btrfs_header_level(root->node); |
20524f02 | 1483 | orig_level = level; |
9f3a7427 CM |
1484 | if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) { |
1485 | path->nodes[level] = root->node; | |
f510cfec | 1486 | extent_buffer_get(root->node); |
9f3a7427 CM |
1487 | path->slots[level] = 0; |
1488 | } else { | |
1489 | struct btrfs_key key; | |
5f39d397 CM |
1490 | struct btrfs_disk_key found_key; |
1491 | struct extent_buffer *node; | |
6702ed49 | 1492 | |
9f3a7427 | 1493 | btrfs_disk_key_to_cpu(&key, &root_item->drop_progress); |
6702ed49 CM |
1494 | level = root_item->drop_level; |
1495 | path->lowest_level = level; | |
9f3a7427 | 1496 | wret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
6702ed49 | 1497 | if (wret < 0) { |
9f3a7427 CM |
1498 | ret = wret; |
1499 | goto out; | |
1500 | } | |
5f39d397 CM |
1501 | node = path->nodes[level]; |
1502 | btrfs_node_key(node, &found_key, path->slots[level]); | |
1503 | WARN_ON(memcmp(&found_key, &root_item->drop_progress, | |
1504 | sizeof(found_key))); | |
9f3a7427 | 1505 | } |
20524f02 | 1506 | while(1) { |
5caf2a00 | 1507 | wret = walk_down_tree(trans, root, path, &level); |
9aca1d51 | 1508 | if (wret > 0) |
20524f02 | 1509 | break; |
9aca1d51 CM |
1510 | if (wret < 0) |
1511 | ret = wret; | |
1512 | ||
5caf2a00 | 1513 | wret = walk_up_tree(trans, root, path, &level); |
9aca1d51 | 1514 | if (wret > 0) |
20524f02 | 1515 | break; |
9aca1d51 CM |
1516 | if (wret < 0) |
1517 | ret = wret; | |
409eb95d | 1518 | ret = -EAGAIN; |
409eb95d | 1519 | break; |
20524f02 | 1520 | } |
83e15a28 | 1521 | for (i = 0; i <= orig_level; i++) { |
5caf2a00 | 1522 | if (path->nodes[i]) { |
5f39d397 | 1523 | free_extent_buffer(path->nodes[i]); |
6702ed49 | 1524 | path->nodes[i] = 0; |
83e15a28 | 1525 | } |
20524f02 | 1526 | } |
9f3a7427 | 1527 | out: |
5caf2a00 | 1528 | btrfs_free_path(path); |
9aca1d51 | 1529 | return ret; |
20524f02 | 1530 | } |
9078a3e1 | 1531 | |
be744175 | 1532 | static int free_block_group_radix(struct radix_tree_root *radix) |
9078a3e1 CM |
1533 | { |
1534 | int ret; | |
1535 | struct btrfs_block_group_cache *cache[8]; | |
1536 | int i; | |
1537 | ||
1538 | while(1) { | |
be744175 | 1539 | ret = radix_tree_gang_lookup(radix, (void **)cache, 0, |
9078a3e1 CM |
1540 | ARRAY_SIZE(cache)); |
1541 | if (!ret) | |
1542 | break; | |
1543 | for (i = 0; i < ret; i++) { | |
be744175 | 1544 | radix_tree_delete(radix, cache[i]->key.objectid + |
9078a3e1 CM |
1545 | cache[i]->key.offset - 1); |
1546 | kfree(cache[i]); | |
1547 | } | |
1548 | } | |
1549 | return 0; | |
1550 | } | |
1551 | ||
be744175 CM |
1552 | int btrfs_free_block_groups(struct btrfs_fs_info *info) |
1553 | { | |
1554 | int ret; | |
1555 | int ret2; | |
f510cfec CM |
1556 | u64 start; |
1557 | u64 end; | |
be744175 CM |
1558 | |
1559 | ret = free_block_group_radix(&info->block_group_radix); | |
1560 | ret2 = free_block_group_radix(&info->block_group_data_radix); | |
1561 | if (ret) | |
1562 | return ret; | |
1563 | if (ret2) | |
1564 | return ret2; | |
e37c9e69 CM |
1565 | |
1566 | while(1) { | |
f510cfec CM |
1567 | ret = find_first_extent_bit(&info->free_space_cache, 0, |
1568 | &start, &end, EXTENT_DIRTY); | |
1569 | if (ret) | |
e37c9e69 | 1570 | break; |
f510cfec CM |
1571 | clear_extent_dirty(&info->free_space_cache, start, |
1572 | end, GFP_NOFS); | |
e37c9e69 | 1573 | } |
be744175 CM |
1574 | return 0; |
1575 | } | |
1576 | ||
9078a3e1 CM |
1577 | int btrfs_read_block_groups(struct btrfs_root *root) |
1578 | { | |
1579 | struct btrfs_path *path; | |
1580 | int ret; | |
1581 | int err = 0; | |
9078a3e1 | 1582 | struct btrfs_block_group_cache *cache; |
be744175 CM |
1583 | struct btrfs_fs_info *info = root->fs_info; |
1584 | struct radix_tree_root *radix; | |
9078a3e1 CM |
1585 | struct btrfs_key key; |
1586 | struct btrfs_key found_key; | |
5f39d397 | 1587 | struct extent_buffer *leaf; |
84f54cfa | 1588 | u64 group_size_blocks; |
31f3c99b | 1589 | u64 used; |
9078a3e1 | 1590 | |
84f54cfa CM |
1591 | group_size_blocks = BTRFS_BLOCK_GROUP_SIZE >> |
1592 | root->fs_info->sb->s_blocksize_bits; | |
be744175 | 1593 | root = info->extent_root; |
9078a3e1 CM |
1594 | key.objectid = 0; |
1595 | key.offset = group_size_blocks; | |
9078a3e1 CM |
1596 | btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY); |
1597 | ||
1598 | path = btrfs_alloc_path(); | |
1599 | if (!path) | |
1600 | return -ENOMEM; | |
1601 | ||
1602 | while(1) { | |
be744175 | 1603 | ret = btrfs_search_slot(NULL, info->extent_root, |
9078a3e1 CM |
1604 | &key, path, 0, 0); |
1605 | if (ret != 0) { | |
1606 | err = ret; | |
1607 | break; | |
1608 | } | |
5f39d397 CM |
1609 | leaf = path->nodes[0]; |
1610 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | |
9078a3e1 CM |
1611 | cache = kmalloc(sizeof(*cache), GFP_NOFS); |
1612 | if (!cache) { | |
1613 | err = -1; | |
1614 | break; | |
1615 | } | |
3e1ad54f | 1616 | |
5f39d397 CM |
1617 | read_extent_buffer(leaf, &cache->item, |
1618 | btrfs_item_ptr_offset(leaf, path->slots[0]), | |
1619 | sizeof(cache->item)); | |
1620 | if (cache->item.flags & BTRFS_BLOCK_GROUP_DATA) { | |
3e1ad54f | 1621 | radix = &info->block_group_data_radix; |
1e2677e0 CM |
1622 | cache->data = 1; |
1623 | } else { | |
3e1ad54f | 1624 | radix = &info->block_group_radix; |
1e2677e0 CM |
1625 | cache->data = 0; |
1626 | } | |
3e1ad54f | 1627 | |
9078a3e1 | 1628 | memcpy(&cache->key, &found_key, sizeof(found_key)); |
31f3c99b CM |
1629 | cache->last_alloc = cache->key.objectid; |
1630 | cache->first_free = cache->key.objectid; | |
be744175 | 1631 | cache->pinned = 0; |
e37c9e69 CM |
1632 | cache->cached = 0; |
1633 | ||
3e1ad54f CM |
1634 | cache->radix = radix; |
1635 | ||
9078a3e1 CM |
1636 | key.objectid = found_key.objectid + found_key.offset; |
1637 | btrfs_release_path(root, path); | |
5f39d397 | 1638 | |
be744175 | 1639 | ret = radix_tree_insert(radix, found_key.objectid + |
9078a3e1 CM |
1640 | found_key.offset - 1, |
1641 | (void *)cache); | |
1642 | BUG_ON(ret); | |
5f39d397 | 1643 | used = btrfs_block_group_used(&cache->item); |
84f54cfa | 1644 | if (used < div_factor(key.offset, 8)) { |
be744175 | 1645 | radix_tree_tag_set(radix, found_key.objectid + |
31f3c99b CM |
1646 | found_key.offset - 1, |
1647 | BTRFS_BLOCK_GROUP_AVAIL); | |
1648 | } | |
9078a3e1 | 1649 | if (key.objectid >= |
4b52dff6 | 1650 | btrfs_super_total_blocks(&info->super_copy)) |
9078a3e1 CM |
1651 | break; |
1652 | } | |
1653 | ||
1654 | btrfs_free_path(path); | |
1655 | return 0; | |
1656 | } |