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