Commit | Line | Data |
---|---|---|
2e635a27 | 1 | #include <linux/module.h> |
fec577fb CM |
2 | #include "ctree.h" |
3 | #include "disk-io.h" | |
4 | #include "print-tree.h" | |
e089f05c | 5 | #include "transaction.h" |
fec577fb | 6 | |
e089f05c CM |
7 | static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root |
8 | *orig_root, u64 num_blocks, u64 search_start, u64 | |
9 | search_end, struct btrfs_key *ins); | |
10 | static int finish_current_insert(struct btrfs_trans_handle *trans, struct | |
11 | btrfs_root *extent_root); | |
e20d96d6 CM |
12 | static int del_pending_extents(struct btrfs_trans_handle *trans, struct |
13 | btrfs_root *extent_root); | |
fec577fb | 14 | |
e089f05c | 15 | static int inc_block_ref(struct btrfs_trans_handle *trans, struct btrfs_root |
6407bf6d | 16 | *root, u64 blocknr, u64 num_blocks) |
02217ed2 | 17 | { |
5caf2a00 | 18 | struct btrfs_path *path; |
02217ed2 | 19 | int ret; |
e2fa7227 | 20 | struct btrfs_key key; |
234b63a0 CM |
21 | struct btrfs_leaf *l; |
22 | struct btrfs_extent_item *item; | |
e2fa7227 | 23 | struct btrfs_key ins; |
cf27e1ee | 24 | u32 refs; |
037e6390 | 25 | |
9f5fae2f CM |
26 | find_free_extent(trans, root->fs_info->extent_root, 0, 0, (u64)-1, |
27 | &ins); | |
5caf2a00 CM |
28 | path = btrfs_alloc_path(); |
29 | BUG_ON(!path); | |
30 | btrfs_init_path(path); | |
02217ed2 CM |
31 | key.objectid = blocknr; |
32 | key.flags = 0; | |
62e2749e | 33 | btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY); |
6407bf6d | 34 | key.offset = num_blocks; |
5caf2a00 | 35 | ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path, |
9f5fae2f | 36 | 0, 1); |
a28ec197 CM |
37 | if (ret != 0) |
38 | BUG(); | |
02217ed2 | 39 | BUG_ON(ret != 0); |
5caf2a00 CM |
40 | l = btrfs_buffer_leaf(path->nodes[0]); |
41 | item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item); | |
cf27e1ee CM |
42 | refs = btrfs_extent_refs(item); |
43 | btrfs_set_extent_refs(item, refs + 1); | |
5caf2a00 | 44 | btrfs_mark_buffer_dirty(path->nodes[0]); |
a28ec197 | 45 | |
5caf2a00 CM |
46 | btrfs_release_path(root->fs_info->extent_root, path); |
47 | btrfs_free_path(path); | |
9f5fae2f | 48 | finish_current_insert(trans, root->fs_info->extent_root); |
e20d96d6 | 49 | del_pending_extents(trans, root->fs_info->extent_root); |
02217ed2 CM |
50 | return 0; |
51 | } | |
52 | ||
e089f05c | 53 | static int lookup_block_ref(struct btrfs_trans_handle *trans, struct btrfs_root |
6407bf6d | 54 | *root, u64 blocknr, u64 num_blocks, u32 *refs) |
a28ec197 | 55 | { |
5caf2a00 | 56 | struct btrfs_path *path; |
a28ec197 | 57 | int ret; |
e2fa7227 | 58 | struct btrfs_key key; |
234b63a0 CM |
59 | struct btrfs_leaf *l; |
60 | struct btrfs_extent_item *item; | |
5caf2a00 CM |
61 | |
62 | path = btrfs_alloc_path(); | |
63 | btrfs_init_path(path); | |
a28ec197 | 64 | key.objectid = blocknr; |
6407bf6d | 65 | key.offset = num_blocks; |
62e2749e CM |
66 | key.flags = 0; |
67 | btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY); | |
5caf2a00 | 68 | ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path, |
9f5fae2f | 69 | 0, 0); |
a28ec197 CM |
70 | if (ret != 0) |
71 | BUG(); | |
5caf2a00 CM |
72 | l = btrfs_buffer_leaf(path->nodes[0]); |
73 | item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item); | |
cf27e1ee | 74 | *refs = btrfs_extent_refs(item); |
5caf2a00 CM |
75 | btrfs_release_path(root->fs_info->extent_root, path); |
76 | btrfs_free_path(path); | |
a28ec197 CM |
77 | return 0; |
78 | } | |
79 | ||
e089f05c | 80 | int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
e20d96d6 | 81 | struct buffer_head *buf) |
02217ed2 CM |
82 | { |
83 | u64 blocknr; | |
e20d96d6 | 84 | struct btrfs_node *buf_node; |
6407bf6d CM |
85 | struct btrfs_leaf *buf_leaf; |
86 | struct btrfs_disk_key *key; | |
87 | struct btrfs_file_extent_item *fi; | |
02217ed2 | 88 | int i; |
6407bf6d CM |
89 | int leaf; |
90 | int ret; | |
a28ec197 | 91 | |
3768f368 | 92 | if (!root->ref_cows) |
a28ec197 | 93 | return 0; |
e20d96d6 | 94 | buf_node = btrfs_buffer_node(buf); |
6407bf6d CM |
95 | leaf = btrfs_is_leaf(buf_node); |
96 | buf_leaf = btrfs_buffer_leaf(buf); | |
e20d96d6 | 97 | for (i = 0; i < btrfs_header_nritems(&buf_node->header); i++) { |
6407bf6d CM |
98 | if (leaf) { |
99 | key = &buf_leaf->items[i].key; | |
100 | if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY) | |
101 | continue; | |
102 | fi = btrfs_item_ptr(buf_leaf, i, | |
103 | struct btrfs_file_extent_item); | |
104 | ret = inc_block_ref(trans, root, | |
105 | btrfs_file_extent_disk_blocknr(fi), | |
106 | btrfs_file_extent_disk_num_blocks(fi)); | |
107 | BUG_ON(ret); | |
108 | } else { | |
109 | blocknr = btrfs_node_blockptr(buf_node, i); | |
110 | ret = inc_block_ref(trans, root, blocknr, 1); | |
111 | BUG_ON(ret); | |
112 | } | |
02217ed2 CM |
113 | } |
114 | return 0; | |
115 | } | |
116 | ||
e089f05c CM |
117 | int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, struct |
118 | btrfs_root *root) | |
a28ec197 | 119 | { |
8ef97622 | 120 | unsigned long gang[8]; |
88fd146c | 121 | u64 first = 0; |
a28ec197 CM |
122 | int ret; |
123 | int i; | |
8ef97622 | 124 | struct radix_tree_root *pinned_radix = &root->fs_info->pinned_radix; |
a28ec197 CM |
125 | |
126 | while(1) { | |
8ef97622 CM |
127 | ret = find_first_radix_bit(pinned_radix, gang, |
128 | ARRAY_SIZE(gang)); | |
a28ec197 CM |
129 | if (!ret) |
130 | break; | |
88fd146c | 131 | if (!first) |
8ef97622 | 132 | first = gang[0]; |
0579da42 | 133 | for (i = 0; i < ret; i++) { |
8ef97622 | 134 | clear_radix_bit(pinned_radix, gang[i]); |
0579da42 | 135 | } |
a28ec197 | 136 | } |
d5719762 CM |
137 | if (root->fs_info->last_insert.objectid > first) |
138 | root->fs_info->last_insert.objectid = first; | |
9f5fae2f | 139 | root->fs_info->last_insert.offset = 0; |
a28ec197 CM |
140 | return 0; |
141 | } | |
142 | ||
e089f05c CM |
143 | static int finish_current_insert(struct btrfs_trans_handle *trans, struct |
144 | btrfs_root *extent_root) | |
037e6390 | 145 | { |
e2fa7227 | 146 | struct btrfs_key ins; |
234b63a0 | 147 | struct btrfs_extent_item extent_item; |
037e6390 CM |
148 | int i; |
149 | int ret; | |
1261ec42 CM |
150 | u64 super_blocks_used; |
151 | struct btrfs_fs_info *info = extent_root->fs_info; | |
037e6390 | 152 | |
cf27e1ee CM |
153 | btrfs_set_extent_refs(&extent_item, 1); |
154 | btrfs_set_extent_owner(&extent_item, | |
e20d96d6 | 155 | btrfs_header_parentid(btrfs_buffer_header(extent_root->node))); |
037e6390 CM |
156 | ins.offset = 1; |
157 | ins.flags = 0; | |
62e2749e | 158 | btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY); |
037e6390 | 159 | |
9f5fae2f CM |
160 | for (i = 0; i < extent_root->fs_info->current_insert.flags; i++) { |
161 | ins.objectid = extent_root->fs_info->current_insert.objectid + | |
162 | i; | |
1261ec42 CM |
163 | super_blocks_used = btrfs_super_blocks_used(info->disk_super); |
164 | btrfs_set_super_blocks_used(info->disk_super, | |
165 | super_blocks_used + 1); | |
e089f05c CM |
166 | ret = btrfs_insert_item(trans, extent_root, &ins, &extent_item, |
167 | sizeof(extent_item)); | |
037e6390 CM |
168 | BUG_ON(ret); |
169 | } | |
9f5fae2f | 170 | extent_root->fs_info->current_insert.offset = 0; |
037e6390 CM |
171 | return 0; |
172 | } | |
173 | ||
8ef97622 | 174 | static int pin_down_block(struct btrfs_root *root, u64 blocknr, int pending) |
e20d96d6 CM |
175 | { |
176 | int err; | |
78fae27e | 177 | struct btrfs_header *header; |
8ef97622 CM |
178 | struct buffer_head *bh; |
179 | ||
f4b9aa8d | 180 | if (!pending) { |
d98237b3 | 181 | bh = btrfs_find_tree_block(root, blocknr); |
2c90e5d6 CM |
182 | if (bh) { |
183 | if (buffer_uptodate(bh)) { | |
184 | u64 transid = | |
185 | root->fs_info->running_transaction->transid; | |
186 | header = btrfs_buffer_header(bh); | |
187 | if (btrfs_header_generation(header) == | |
188 | transid) { | |
189 | btrfs_block_release(root, bh); | |
190 | return 0; | |
191 | } | |
f4b9aa8d | 192 | } |
d6025579 | 193 | btrfs_block_release(root, bh); |
8ef97622 | 194 | } |
8ef97622 | 195 | err = set_radix_bit(&root->fs_info->pinned_radix, blocknr); |
f4b9aa8d CM |
196 | } else { |
197 | err = set_radix_bit(&root->fs_info->pending_del_radix, blocknr); | |
198 | } | |
8ef97622 | 199 | BUG_ON(err); |
e20d96d6 CM |
200 | return 0; |
201 | } | |
202 | ||
fec577fb | 203 | /* |
a28ec197 | 204 | * remove an extent from the root, returns 0 on success |
fec577fb | 205 | */ |
e089f05c | 206 | static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root |
78fae27e | 207 | *root, u64 blocknr, u64 num_blocks, int pin) |
a28ec197 | 208 | { |
5caf2a00 | 209 | struct btrfs_path *path; |
e2fa7227 | 210 | struct btrfs_key key; |
1261ec42 CM |
211 | struct btrfs_fs_info *info = root->fs_info; |
212 | struct btrfs_root *extent_root = info->extent_root; | |
a28ec197 | 213 | int ret; |
234b63a0 | 214 | struct btrfs_extent_item *ei; |
e2fa7227 | 215 | struct btrfs_key ins; |
cf27e1ee | 216 | u32 refs; |
037e6390 | 217 | |
a28ec197 CM |
218 | key.objectid = blocknr; |
219 | key.flags = 0; | |
62e2749e | 220 | btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY); |
a28ec197 CM |
221 | key.offset = num_blocks; |
222 | ||
e089f05c | 223 | find_free_extent(trans, root, 0, 0, (u64)-1, &ins); |
5caf2a00 CM |
224 | path = btrfs_alloc_path(); |
225 | BUG_ON(!path); | |
226 | btrfs_init_path(path); | |
227 | ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1); | |
a28ec197 | 228 | if (ret) { |
2e635a27 | 229 | printk("failed to find %Lu\n", key.objectid); |
234b63a0 | 230 | btrfs_print_tree(extent_root, extent_root->node); |
2e635a27 | 231 | printk("failed to find %Lu\n", key.objectid); |
a28ec197 CM |
232 | BUG(); |
233 | } | |
5caf2a00 | 234 | ei = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0], |
123abc88 | 235 | struct btrfs_extent_item); |
a28ec197 | 236 | BUG_ON(ei->refs == 0); |
cf27e1ee CM |
237 | refs = btrfs_extent_refs(ei) - 1; |
238 | btrfs_set_extent_refs(ei, refs); | |
5caf2a00 | 239 | btrfs_mark_buffer_dirty(path->nodes[0]); |
cf27e1ee | 240 | if (refs == 0) { |
1261ec42 | 241 | u64 super_blocks_used; |
78fae27e CM |
242 | |
243 | if (pin) { | |
8ef97622 | 244 | ret = pin_down_block(root, blocknr, 0); |
78fae27e CM |
245 | BUG_ON(ret); |
246 | } | |
247 | ||
1261ec42 CM |
248 | super_blocks_used = btrfs_super_blocks_used(info->disk_super); |
249 | btrfs_set_super_blocks_used(info->disk_super, | |
250 | super_blocks_used - num_blocks); | |
5caf2a00 | 251 | ret = btrfs_del_item(trans, extent_root, path); |
a28ec197 CM |
252 | if (ret) |
253 | BUG(); | |
254 | } | |
5caf2a00 CM |
255 | btrfs_release_path(extent_root, path); |
256 | btrfs_free_path(path); | |
e089f05c | 257 | finish_current_insert(trans, extent_root); |
a28ec197 CM |
258 | return ret; |
259 | } | |
260 | ||
a28ec197 CM |
261 | /* |
262 | * find all the blocks marked as pending in the radix tree and remove | |
263 | * them from the extent map | |
264 | */ | |
e089f05c CM |
265 | static int del_pending_extents(struct btrfs_trans_handle *trans, struct |
266 | btrfs_root *extent_root) | |
a28ec197 CM |
267 | { |
268 | int ret; | |
e20d96d6 CM |
269 | int wret; |
270 | int err = 0; | |
8ef97622 | 271 | unsigned long gang[4]; |
a28ec197 | 272 | int i; |
8ef97622 CM |
273 | struct radix_tree_root *pending_radix; |
274 | struct radix_tree_root *pinned_radix; | |
275 | ||
276 | pending_radix = &extent_root->fs_info->pending_del_radix; | |
277 | pinned_radix = &extent_root->fs_info->pinned_radix; | |
a28ec197 CM |
278 | |
279 | while(1) { | |
8ef97622 CM |
280 | ret = find_first_radix_bit(pending_radix, gang, |
281 | ARRAY_SIZE(gang)); | |
a28ec197 CM |
282 | if (!ret) |
283 | break; | |
284 | for (i = 0; i < ret; i++) { | |
8ef97622 CM |
285 | wret = set_radix_bit(pinned_radix, gang[i]); |
286 | BUG_ON(wret); | |
287 | wret = clear_radix_bit(pending_radix, gang[i]); | |
288 | BUG_ON(wret); | |
d5719762 | 289 | wret = __free_extent(trans, extent_root, |
8ef97622 | 290 | gang[i], 1, 0); |
e20d96d6 CM |
291 | if (wret) |
292 | err = wret; | |
fec577fb CM |
293 | } |
294 | } | |
e20d96d6 | 295 | return err; |
fec577fb CM |
296 | } |
297 | ||
298 | /* | |
299 | * remove an extent from the root, returns 0 on success | |
300 | */ | |
e089f05c CM |
301 | int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root |
302 | *root, u64 blocknr, u64 num_blocks, int pin) | |
fec577fb | 303 | { |
9f5fae2f | 304 | struct btrfs_root *extent_root = root->fs_info->extent_root; |
fec577fb CM |
305 | int pending_ret; |
306 | int ret; | |
a28ec197 | 307 | |
fec577fb | 308 | if (root == extent_root) { |
8ef97622 | 309 | pin_down_block(root, blocknr, 1); |
fec577fb CM |
310 | return 0; |
311 | } | |
78fae27e | 312 | ret = __free_extent(trans, root, blocknr, num_blocks, pin); |
e20d96d6 | 313 | pending_ret = del_pending_extents(trans, root->fs_info->extent_root); |
fec577fb CM |
314 | return ret ? ret : pending_ret; |
315 | } | |
316 | ||
317 | /* | |
318 | * walks the btree of allocated extents and find a hole of a given size. | |
319 | * The key ins is changed to record the hole: | |
320 | * ins->objectid == block start | |
62e2749e | 321 | * ins->flags = BTRFS_EXTENT_ITEM_KEY |
fec577fb CM |
322 | * ins->offset == number of blocks |
323 | * Any available blocks before search_start are skipped. | |
324 | */ | |
e089f05c CM |
325 | static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root |
326 | *orig_root, u64 num_blocks, u64 search_start, u64 | |
327 | search_end, struct btrfs_key *ins) | |
fec577fb | 328 | { |
5caf2a00 | 329 | struct btrfs_path *path; |
e2fa7227 | 330 | struct btrfs_key key; |
fec577fb CM |
331 | int ret; |
332 | u64 hole_size = 0; | |
333 | int slot = 0; | |
e20d96d6 | 334 | u64 last_block = 0; |
037e6390 | 335 | u64 test_block; |
fec577fb | 336 | int start_found; |
234b63a0 | 337 | struct btrfs_leaf *l; |
9f5fae2f | 338 | struct btrfs_root * root = orig_root->fs_info->extent_root; |
0579da42 | 339 | int total_needed = num_blocks; |
e20d96d6 | 340 | int level; |
fec577fb | 341 | |
e20d96d6 CM |
342 | level = btrfs_header_level(btrfs_buffer_header(root->node)); |
343 | total_needed += (level + 1) * 3; | |
9f5fae2f CM |
344 | if (root->fs_info->last_insert.objectid > search_start) |
345 | search_start = root->fs_info->last_insert.objectid; | |
62e2749e CM |
346 | |
347 | ins->flags = 0; | |
348 | btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY); | |
5caf2a00 | 349 | path = btrfs_alloc_path(); |
62e2749e | 350 | |
fec577fb | 351 | check_failed: |
5caf2a00 | 352 | btrfs_init_path(path); |
fec577fb CM |
353 | ins->objectid = search_start; |
354 | ins->offset = 0; | |
fec577fb | 355 | start_found = 0; |
5caf2a00 | 356 | ret = btrfs_search_slot(trans, root, ins, path, 0, 0); |
0f70abe2 CM |
357 | if (ret < 0) |
358 | goto error; | |
aa5d6bed | 359 | |
5caf2a00 CM |
360 | if (path->slots[0] > 0) |
361 | path->slots[0]--; | |
0579da42 | 362 | |
fec577fb | 363 | while (1) { |
5caf2a00 CM |
364 | l = btrfs_buffer_leaf(path->nodes[0]); |
365 | slot = path->slots[0]; | |
7518a238 | 366 | if (slot >= btrfs_header_nritems(&l->header)) { |
5caf2a00 | 367 | ret = btrfs_next_leaf(root, path); |
fec577fb CM |
368 | if (ret == 0) |
369 | continue; | |
0f70abe2 CM |
370 | if (ret < 0) |
371 | goto error; | |
fec577fb CM |
372 | if (!start_found) { |
373 | ins->objectid = search_start; | |
037e6390 | 374 | ins->offset = (u64)-1; |
fec577fb CM |
375 | start_found = 1; |
376 | goto check_pending; | |
377 | } | |
378 | ins->objectid = last_block > search_start ? | |
379 | last_block : search_start; | |
037e6390 | 380 | ins->offset = (u64)-1; |
fec577fb CM |
381 | goto check_pending; |
382 | } | |
e2fa7227 CM |
383 | btrfs_disk_key_to_cpu(&key, &l->items[slot].key); |
384 | if (key.objectid >= search_start) { | |
fec577fb | 385 | if (start_found) { |
0579da42 CM |
386 | if (last_block < search_start) |
387 | last_block = search_start; | |
e2fa7227 | 388 | hole_size = key.objectid - last_block; |
037e6390 | 389 | if (hole_size > total_needed) { |
fec577fb | 390 | ins->objectid = last_block; |
037e6390 | 391 | ins->offset = hole_size; |
fec577fb CM |
392 | goto check_pending; |
393 | } | |
0579da42 | 394 | } |
fec577fb | 395 | } |
0579da42 | 396 | start_found = 1; |
e2fa7227 | 397 | last_block = key.objectid + key.offset; |
5caf2a00 | 398 | path->slots[0]++; |
fec577fb CM |
399 | } |
400 | // FIXME -ENOSPC | |
401 | check_pending: | |
402 | /* we have to make sure we didn't find an extent that has already | |
403 | * been allocated by the map tree or the original allocation | |
404 | */ | |
5caf2a00 | 405 | btrfs_release_path(root, path); |
fec577fb | 406 | BUG_ON(ins->objectid < search_start); |
037e6390 CM |
407 | for (test_block = ins->objectid; |
408 | test_block < ins->objectid + total_needed; test_block++) { | |
8ef97622 | 409 | if (test_radix_bit(&root->fs_info->pinned_radix, |
9f5fae2f | 410 | test_block)) { |
037e6390 | 411 | search_start = test_block + 1; |
fec577fb CM |
412 | goto check_failed; |
413 | } | |
414 | } | |
9f5fae2f CM |
415 | BUG_ON(root->fs_info->current_insert.offset); |
416 | root->fs_info->current_insert.offset = total_needed - num_blocks; | |
417 | root->fs_info->current_insert.objectid = ins->objectid + num_blocks; | |
418 | root->fs_info->current_insert.flags = 0; | |
419 | root->fs_info->last_insert.objectid = ins->objectid; | |
037e6390 | 420 | ins->offset = num_blocks; |
5caf2a00 | 421 | btrfs_free_path(path); |
fec577fb | 422 | return 0; |
0f70abe2 | 423 | error: |
5caf2a00 CM |
424 | btrfs_release_path(root, path); |
425 | btrfs_free_path(path); | |
0f70abe2 | 426 | return ret; |
fec577fb CM |
427 | } |
428 | ||
fec577fb CM |
429 | /* |
430 | * finds a free extent and does all the dirty work required for allocation | |
431 | * returns the key for the extent through ins, and a tree buffer for | |
432 | * the first block of the extent through buf. | |
433 | * | |
434 | * returns 0 if everything worked, non-zero otherwise. | |
435 | */ | |
dee26a9f | 436 | int btrfs_alloc_extent(struct btrfs_trans_handle *trans, struct btrfs_root |
e089f05c CM |
437 | *root, u64 num_blocks, u64 search_start, u64 |
438 | search_end, u64 owner, struct btrfs_key *ins) | |
fec577fb CM |
439 | { |
440 | int ret; | |
441 | int pending_ret; | |
1261ec42 CM |
442 | u64 super_blocks_used; |
443 | struct btrfs_fs_info *info = root->fs_info; | |
444 | struct btrfs_root *extent_root = info->extent_root; | |
234b63a0 | 445 | struct btrfs_extent_item extent_item; |
037e6390 | 446 | |
cf27e1ee CM |
447 | btrfs_set_extent_refs(&extent_item, 1); |
448 | btrfs_set_extent_owner(&extent_item, owner); | |
fec577fb | 449 | |
037e6390 | 450 | if (root == extent_root) { |
9f5fae2f | 451 | BUG_ON(extent_root->fs_info->current_insert.offset == 0); |
037e6390 | 452 | BUG_ON(num_blocks != 1); |
9f5fae2f CM |
453 | BUG_ON(extent_root->fs_info->current_insert.flags == |
454 | extent_root->fs_info->current_insert.offset); | |
037e6390 | 455 | ins->offset = 1; |
9f5fae2f CM |
456 | ins->objectid = extent_root->fs_info->current_insert.objectid + |
457 | extent_root->fs_info->current_insert.flags++; | |
fec577fb CM |
458 | return 0; |
459 | } | |
e089f05c | 460 | ret = find_free_extent(trans, root, num_blocks, search_start, |
037e6390 CM |
461 | search_end, ins); |
462 | if (ret) | |
463 | return ret; | |
fec577fb | 464 | |
1261ec42 CM |
465 | super_blocks_used = btrfs_super_blocks_used(info->disk_super); |
466 | btrfs_set_super_blocks_used(info->disk_super, super_blocks_used + | |
467 | num_blocks); | |
e089f05c CM |
468 | ret = btrfs_insert_item(trans, extent_root, ins, &extent_item, |
469 | sizeof(extent_item)); | |
037e6390 | 470 | |
e089f05c | 471 | finish_current_insert(trans, extent_root); |
e20d96d6 | 472 | pending_ret = del_pending_extents(trans, extent_root); |
037e6390 CM |
473 | if (ret) |
474 | return ret; | |
475 | if (pending_ret) | |
476 | return pending_ret; | |
477 | return 0; | |
fec577fb CM |
478 | } |
479 | ||
480 | /* | |
481 | * helper function to allocate a block for a given tree | |
482 | * returns the tree buffer or NULL. | |
483 | */ | |
e20d96d6 | 484 | struct buffer_head *btrfs_alloc_free_block(struct btrfs_trans_handle *trans, |
e089f05c | 485 | struct btrfs_root *root) |
fec577fb | 486 | { |
e2fa7227 | 487 | struct btrfs_key ins; |
fec577fb | 488 | int ret; |
e20d96d6 | 489 | struct buffer_head *buf; |
fec577fb | 490 | |
dee26a9f | 491 | ret = btrfs_alloc_extent(trans, root, 1, 0, (unsigned long)-1, |
e20d96d6 | 492 | btrfs_header_parentid(btrfs_buffer_header(root->node)), &ins); |
fec577fb CM |
493 | if (ret) { |
494 | BUG(); | |
495 | return NULL; | |
496 | } | |
d98237b3 | 497 | buf = btrfs_find_create_tree_block(root, ins.objectid); |
df2ce34c | 498 | set_buffer_uptodate(buf); |
fec577fb CM |
499 | return buf; |
500 | } | |
a28ec197 | 501 | |
6407bf6d CM |
502 | static int drop_leaf_ref(struct btrfs_trans_handle *trans, |
503 | struct btrfs_root *root, struct buffer_head *cur) | |
504 | { | |
505 | struct btrfs_disk_key *key; | |
506 | struct btrfs_leaf *leaf; | |
507 | struct btrfs_file_extent_item *fi; | |
508 | int i; | |
509 | int nritems; | |
510 | int ret; | |
511 | ||
512 | BUG_ON(!btrfs_is_leaf(btrfs_buffer_node(cur))); | |
513 | leaf = btrfs_buffer_leaf(cur); | |
514 | nritems = btrfs_header_nritems(&leaf->header); | |
515 | for (i = 0; i < nritems; i++) { | |
516 | key = &leaf->items[i].key; | |
517 | if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY) | |
518 | continue; | |
519 | fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item); | |
520 | /* | |
521 | * FIXME make sure to insert a trans record that | |
522 | * repeats the snapshot del on crash | |
523 | */ | |
524 | ret = btrfs_free_extent(trans, root, | |
525 | btrfs_file_extent_disk_blocknr(fi), | |
526 | btrfs_file_extent_disk_num_blocks(fi), | |
527 | 0); | |
528 | BUG_ON(ret); | |
529 | } | |
530 | return 0; | |
531 | } | |
532 | ||
9aca1d51 CM |
533 | /* |
534 | * helper function for drop_snapshot, this walks down the tree dropping ref | |
535 | * counts as it goes. | |
536 | */ | |
e089f05c CM |
537 | static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root |
538 | *root, struct btrfs_path *path, int *level) | |
20524f02 | 539 | { |
e20d96d6 CM |
540 | struct buffer_head *next; |
541 | struct buffer_head *cur; | |
20524f02 CM |
542 | u64 blocknr; |
543 | int ret; | |
544 | u32 refs; | |
545 | ||
5caf2a00 CM |
546 | WARN_ON(*level < 0); |
547 | WARN_ON(*level >= BTRFS_MAX_LEVEL); | |
e20d96d6 | 548 | ret = lookup_block_ref(trans, root, path->nodes[*level]->b_blocknr, |
6407bf6d | 549 | 1, &refs); |
20524f02 CM |
550 | BUG_ON(ret); |
551 | if (refs > 1) | |
552 | goto out; | |
9aca1d51 CM |
553 | /* |
554 | * walk down to the last node level and free all the leaves | |
555 | */ | |
6407bf6d | 556 | while(*level >= 0) { |
5caf2a00 CM |
557 | WARN_ON(*level < 0); |
558 | WARN_ON(*level >= BTRFS_MAX_LEVEL); | |
20524f02 | 559 | cur = path->nodes[*level]; |
2c90e5d6 CM |
560 | if (btrfs_header_level(btrfs_buffer_header(cur)) != *level) |
561 | WARN_ON(1); | |
7518a238 | 562 | if (path->slots[*level] >= |
e20d96d6 | 563 | btrfs_header_nritems(btrfs_buffer_header(cur))) |
20524f02 | 564 | break; |
6407bf6d CM |
565 | if (*level == 0) { |
566 | ret = drop_leaf_ref(trans, root, cur); | |
567 | BUG_ON(ret); | |
568 | break; | |
569 | } | |
e20d96d6 CM |
570 | blocknr = btrfs_node_blockptr(btrfs_buffer_node(cur), |
571 | path->slots[*level]); | |
6407bf6d CM |
572 | ret = lookup_block_ref(trans, root, blocknr, 1, &refs); |
573 | BUG_ON(ret); | |
574 | if (refs != 1) { | |
20524f02 | 575 | path->slots[*level]++; |
e089f05c | 576 | ret = btrfs_free_extent(trans, root, blocknr, 1, 1); |
20524f02 CM |
577 | BUG_ON(ret); |
578 | continue; | |
579 | } | |
20524f02 | 580 | next = read_tree_block(root, blocknr); |
5caf2a00 | 581 | WARN_ON(*level <= 0); |
83e15a28 | 582 | if (path->nodes[*level-1]) |
234b63a0 | 583 | btrfs_block_release(root, path->nodes[*level-1]); |
20524f02 | 584 | path->nodes[*level-1] = next; |
e20d96d6 | 585 | *level = btrfs_header_level(btrfs_buffer_header(next)); |
20524f02 CM |
586 | path->slots[*level] = 0; |
587 | } | |
588 | out: | |
5caf2a00 CM |
589 | WARN_ON(*level < 0); |
590 | WARN_ON(*level >= BTRFS_MAX_LEVEL); | |
6407bf6d CM |
591 | ret = btrfs_free_extent(trans, root, |
592 | path->nodes[*level]->b_blocknr, 1, 1); | |
234b63a0 | 593 | btrfs_block_release(root, path->nodes[*level]); |
20524f02 CM |
594 | path->nodes[*level] = NULL; |
595 | *level += 1; | |
596 | BUG_ON(ret); | |
597 | return 0; | |
598 | } | |
599 | ||
9aca1d51 CM |
600 | /* |
601 | * helper for dropping snapshots. This walks back up the tree in the path | |
602 | * to find the first node higher up where we haven't yet gone through | |
603 | * all the slots | |
604 | */ | |
e089f05c CM |
605 | static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root |
606 | *root, struct btrfs_path *path, int *level) | |
20524f02 CM |
607 | { |
608 | int i; | |
609 | int slot; | |
610 | int ret; | |
234b63a0 | 611 | for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) { |
20524f02 | 612 | slot = path->slots[i]; |
e20d96d6 CM |
613 | if (slot < btrfs_header_nritems( |
614 | btrfs_buffer_header(path->nodes[i])) - 1) { | |
20524f02 CM |
615 | path->slots[i]++; |
616 | *level = i; | |
617 | return 0; | |
618 | } else { | |
e089f05c | 619 | ret = btrfs_free_extent(trans, root, |
e20d96d6 | 620 | path->nodes[*level]->b_blocknr, |
e089f05c | 621 | 1, 1); |
6407bf6d | 622 | BUG_ON(ret); |
234b63a0 | 623 | btrfs_block_release(root, path->nodes[*level]); |
83e15a28 | 624 | path->nodes[*level] = NULL; |
20524f02 | 625 | *level = i + 1; |
20524f02 CM |
626 | } |
627 | } | |
628 | return 1; | |
629 | } | |
630 | ||
9aca1d51 CM |
631 | /* |
632 | * drop the reference count on the tree rooted at 'snap'. This traverses | |
633 | * the tree freeing any blocks that have a ref count of zero after being | |
634 | * decremented. | |
635 | */ | |
e089f05c | 636 | int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root |
e20d96d6 | 637 | *root, struct buffer_head *snap) |
20524f02 | 638 | { |
3768f368 | 639 | int ret = 0; |
9aca1d51 | 640 | int wret; |
20524f02 | 641 | int level; |
5caf2a00 | 642 | struct btrfs_path *path; |
20524f02 CM |
643 | int i; |
644 | int orig_level; | |
645 | ||
5caf2a00 CM |
646 | path = btrfs_alloc_path(); |
647 | BUG_ON(!path); | |
648 | btrfs_init_path(path); | |
20524f02 | 649 | |
e20d96d6 | 650 | level = btrfs_header_level(btrfs_buffer_header(snap)); |
20524f02 | 651 | orig_level = level; |
5caf2a00 CM |
652 | path->nodes[level] = snap; |
653 | path->slots[level] = 0; | |
20524f02 | 654 | while(1) { |
5caf2a00 | 655 | wret = walk_down_tree(trans, root, path, &level); |
9aca1d51 | 656 | if (wret > 0) |
20524f02 | 657 | break; |
9aca1d51 CM |
658 | if (wret < 0) |
659 | ret = wret; | |
660 | ||
5caf2a00 | 661 | wret = walk_up_tree(trans, root, path, &level); |
9aca1d51 | 662 | if (wret > 0) |
20524f02 | 663 | break; |
9aca1d51 CM |
664 | if (wret < 0) |
665 | ret = wret; | |
20524f02 | 666 | } |
83e15a28 | 667 | for (i = 0; i <= orig_level; i++) { |
5caf2a00 CM |
668 | if (path->nodes[i]) { |
669 | btrfs_block_release(root, path->nodes[i]); | |
83e15a28 | 670 | } |
20524f02 | 671 | } |
5caf2a00 | 672 | btrfs_free_path(path); |
9aca1d51 | 673 | return ret; |
20524f02 | 674 | } |