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