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