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); | |
5f26f772 | 227 | |
5caf2a00 | 228 | ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1); |
a28ec197 | 229 | if (ret) { |
2e635a27 | 230 | printk("failed to find %Lu\n", key.objectid); |
234b63a0 | 231 | btrfs_print_tree(extent_root, extent_root->node); |
2e635a27 | 232 | printk("failed to find %Lu\n", key.objectid); |
a28ec197 CM |
233 | BUG(); |
234 | } | |
5caf2a00 | 235 | ei = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0], |
123abc88 | 236 | struct btrfs_extent_item); |
a28ec197 | 237 | BUG_ON(ei->refs == 0); |
cf27e1ee CM |
238 | refs = btrfs_extent_refs(ei) - 1; |
239 | btrfs_set_extent_refs(ei, refs); | |
5caf2a00 | 240 | btrfs_mark_buffer_dirty(path->nodes[0]); |
cf27e1ee | 241 | if (refs == 0) { |
1261ec42 | 242 | u64 super_blocks_used; |
78fae27e CM |
243 | |
244 | if (pin) { | |
8ef97622 | 245 | ret = pin_down_block(root, blocknr, 0); |
78fae27e CM |
246 | BUG_ON(ret); |
247 | } | |
248 | ||
1261ec42 CM |
249 | super_blocks_used = btrfs_super_blocks_used(info->disk_super); |
250 | btrfs_set_super_blocks_used(info->disk_super, | |
251 | super_blocks_used - num_blocks); | |
5caf2a00 | 252 | ret = btrfs_del_item(trans, extent_root, path); |
a28ec197 CM |
253 | if (ret) |
254 | BUG(); | |
255 | } | |
5caf2a00 CM |
256 | btrfs_release_path(extent_root, path); |
257 | btrfs_free_path(path); | |
e089f05c | 258 | finish_current_insert(trans, extent_root); |
a28ec197 CM |
259 | return ret; |
260 | } | |
261 | ||
a28ec197 CM |
262 | /* |
263 | * find all the blocks marked as pending in the radix tree and remove | |
264 | * them from the extent map | |
265 | */ | |
e089f05c CM |
266 | static int del_pending_extents(struct btrfs_trans_handle *trans, struct |
267 | btrfs_root *extent_root) | |
a28ec197 CM |
268 | { |
269 | int ret; | |
e20d96d6 CM |
270 | int wret; |
271 | int err = 0; | |
8ef97622 | 272 | unsigned long gang[4]; |
a28ec197 | 273 | int i; |
8ef97622 CM |
274 | struct radix_tree_root *pending_radix; |
275 | struct radix_tree_root *pinned_radix; | |
276 | ||
277 | pending_radix = &extent_root->fs_info->pending_del_radix; | |
278 | pinned_radix = &extent_root->fs_info->pinned_radix; | |
a28ec197 CM |
279 | |
280 | while(1) { | |
8ef97622 CM |
281 | ret = find_first_radix_bit(pending_radix, gang, |
282 | ARRAY_SIZE(gang)); | |
a28ec197 CM |
283 | if (!ret) |
284 | break; | |
285 | for (i = 0; i < ret; i++) { | |
8ef97622 CM |
286 | wret = set_radix_bit(pinned_radix, gang[i]); |
287 | BUG_ON(wret); | |
288 | wret = clear_radix_bit(pending_radix, gang[i]); | |
289 | BUG_ON(wret); | |
d5719762 | 290 | wret = __free_extent(trans, extent_root, |
8ef97622 | 291 | gang[i], 1, 0); |
e20d96d6 CM |
292 | if (wret) |
293 | err = wret; | |
fec577fb CM |
294 | } |
295 | } | |
e20d96d6 | 296 | return err; |
fec577fb CM |
297 | } |
298 | ||
299 | /* | |
300 | * remove an extent from the root, returns 0 on success | |
301 | */ | |
e089f05c CM |
302 | int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root |
303 | *root, u64 blocknr, u64 num_blocks, int pin) | |
fec577fb | 304 | { |
9f5fae2f | 305 | struct btrfs_root *extent_root = root->fs_info->extent_root; |
fec577fb CM |
306 | int pending_ret; |
307 | int ret; | |
a28ec197 | 308 | |
fec577fb | 309 | if (root == extent_root) { |
8ef97622 | 310 | pin_down_block(root, blocknr, 1); |
fec577fb CM |
311 | return 0; |
312 | } | |
78fae27e | 313 | ret = __free_extent(trans, root, blocknr, num_blocks, pin); |
e20d96d6 | 314 | pending_ret = del_pending_extents(trans, root->fs_info->extent_root); |
fec577fb CM |
315 | return ret ? ret : pending_ret; |
316 | } | |
317 | ||
318 | /* | |
319 | * walks the btree of allocated extents and find a hole of a given size. | |
320 | * The key ins is changed to record the hole: | |
321 | * ins->objectid == block start | |
62e2749e | 322 | * ins->flags = BTRFS_EXTENT_ITEM_KEY |
fec577fb CM |
323 | * ins->offset == number of blocks |
324 | * Any available blocks before search_start are skipped. | |
325 | */ | |
e089f05c CM |
326 | static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root |
327 | *orig_root, u64 num_blocks, u64 search_start, u64 | |
328 | search_end, struct btrfs_key *ins) | |
fec577fb | 329 | { |
5caf2a00 | 330 | struct btrfs_path *path; |
e2fa7227 | 331 | struct btrfs_key key; |
fec577fb CM |
332 | int ret; |
333 | u64 hole_size = 0; | |
334 | int slot = 0; | |
e20d96d6 | 335 | u64 last_block = 0; |
037e6390 | 336 | u64 test_block; |
fec577fb | 337 | int start_found; |
234b63a0 | 338 | struct btrfs_leaf *l; |
9f5fae2f | 339 | struct btrfs_root * root = orig_root->fs_info->extent_root; |
0579da42 | 340 | int total_needed = num_blocks; |
e20d96d6 | 341 | int level; |
fec577fb | 342 | |
b1a4d965 CM |
343 | path = btrfs_alloc_path(); |
344 | ins->flags = 0; | |
345 | btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY); | |
346 | ||
e20d96d6 CM |
347 | level = btrfs_header_level(btrfs_buffer_header(root->node)); |
348 | total_needed += (level + 1) * 3; | |
b1a4d965 CM |
349 | if (root->fs_info->last_insert.objectid == 0 && search_end == (u64)-1) { |
350 | struct btrfs_disk_key *last_key; | |
351 | btrfs_init_path(path); | |
352 | ins->objectid = (u64)-1; | |
353 | ins->offset = (u64)-1; | |
354 | ret = btrfs_search_slot(trans, root, ins, path, 0, 0); | |
355 | if (ret < 0) | |
356 | goto error; | |
357 | BUG_ON(ret == 0); | |
358 | if (path->slots[0] > 0) | |
359 | path->slots[0]--; | |
360 | l = btrfs_buffer_leaf(path->nodes[0]); | |
361 | last_key = &l->items[path->slots[0]].key; | |
362 | search_start = btrfs_disk_key_objectid(last_key); | |
363 | } | |
9f5fae2f CM |
364 | if (root->fs_info->last_insert.objectid > search_start) |
365 | search_start = root->fs_info->last_insert.objectid; | |
62e2749e | 366 | |
fec577fb | 367 | check_failed: |
5caf2a00 | 368 | btrfs_init_path(path); |
fec577fb CM |
369 | ins->objectid = search_start; |
370 | ins->offset = 0; | |
fec577fb | 371 | start_found = 0; |
5caf2a00 | 372 | ret = btrfs_search_slot(trans, root, ins, path, 0, 0); |
0f70abe2 CM |
373 | if (ret < 0) |
374 | goto error; | |
aa5d6bed | 375 | |
5caf2a00 CM |
376 | if (path->slots[0] > 0) |
377 | path->slots[0]--; | |
0579da42 | 378 | |
fec577fb | 379 | while (1) { |
5caf2a00 CM |
380 | l = btrfs_buffer_leaf(path->nodes[0]); |
381 | slot = path->slots[0]; | |
7518a238 | 382 | if (slot >= btrfs_header_nritems(&l->header)) { |
5caf2a00 | 383 | ret = btrfs_next_leaf(root, path); |
fec577fb CM |
384 | if (ret == 0) |
385 | continue; | |
0f70abe2 CM |
386 | if (ret < 0) |
387 | goto error; | |
fec577fb CM |
388 | if (!start_found) { |
389 | ins->objectid = search_start; | |
037e6390 | 390 | ins->offset = (u64)-1; |
fec577fb CM |
391 | start_found = 1; |
392 | goto check_pending; | |
393 | } | |
394 | ins->objectid = last_block > search_start ? | |
395 | last_block : search_start; | |
037e6390 | 396 | ins->offset = (u64)-1; |
fec577fb CM |
397 | goto check_pending; |
398 | } | |
e2fa7227 CM |
399 | btrfs_disk_key_to_cpu(&key, &l->items[slot].key); |
400 | if (key.objectid >= search_start) { | |
fec577fb | 401 | if (start_found) { |
0579da42 CM |
402 | if (last_block < search_start) |
403 | last_block = search_start; | |
e2fa7227 | 404 | hole_size = key.objectid - last_block; |
037e6390 | 405 | if (hole_size > total_needed) { |
fec577fb | 406 | ins->objectid = last_block; |
037e6390 | 407 | ins->offset = hole_size; |
fec577fb CM |
408 | goto check_pending; |
409 | } | |
0579da42 | 410 | } |
fec577fb | 411 | } |
0579da42 | 412 | start_found = 1; |
e2fa7227 | 413 | last_block = key.objectid + key.offset; |
5caf2a00 | 414 | path->slots[0]++; |
fec577fb CM |
415 | } |
416 | // FIXME -ENOSPC | |
417 | check_pending: | |
418 | /* we have to make sure we didn't find an extent that has already | |
419 | * been allocated by the map tree or the original allocation | |
420 | */ | |
5caf2a00 | 421 | btrfs_release_path(root, path); |
fec577fb | 422 | BUG_ON(ins->objectid < search_start); |
037e6390 CM |
423 | for (test_block = ins->objectid; |
424 | test_block < ins->objectid + total_needed; test_block++) { | |
8ef97622 | 425 | if (test_radix_bit(&root->fs_info->pinned_radix, |
9f5fae2f | 426 | test_block)) { |
037e6390 | 427 | search_start = test_block + 1; |
fec577fb CM |
428 | goto check_failed; |
429 | } | |
430 | } | |
9f5fae2f CM |
431 | BUG_ON(root->fs_info->current_insert.offset); |
432 | root->fs_info->current_insert.offset = total_needed - num_blocks; | |
433 | root->fs_info->current_insert.objectid = ins->objectid + num_blocks; | |
434 | root->fs_info->current_insert.flags = 0; | |
435 | root->fs_info->last_insert.objectid = ins->objectid; | |
037e6390 | 436 | ins->offset = num_blocks; |
5caf2a00 | 437 | btrfs_free_path(path); |
fec577fb | 438 | return 0; |
0f70abe2 | 439 | error: |
5caf2a00 CM |
440 | btrfs_release_path(root, path); |
441 | btrfs_free_path(path); | |
0f70abe2 | 442 | return ret; |
fec577fb CM |
443 | } |
444 | ||
fec577fb CM |
445 | /* |
446 | * finds a free extent and does all the dirty work required for allocation | |
447 | * returns the key for the extent through ins, and a tree buffer for | |
448 | * the first block of the extent through buf. | |
449 | * | |
450 | * returns 0 if everything worked, non-zero otherwise. | |
451 | */ | |
dee26a9f | 452 | int btrfs_alloc_extent(struct btrfs_trans_handle *trans, struct btrfs_root |
e089f05c CM |
453 | *root, u64 num_blocks, u64 search_start, u64 |
454 | search_end, u64 owner, struct btrfs_key *ins) | |
fec577fb CM |
455 | { |
456 | int ret; | |
457 | int pending_ret; | |
1261ec42 CM |
458 | u64 super_blocks_used; |
459 | struct btrfs_fs_info *info = root->fs_info; | |
460 | struct btrfs_root *extent_root = info->extent_root; | |
234b63a0 | 461 | struct btrfs_extent_item extent_item; |
037e6390 | 462 | |
cf27e1ee CM |
463 | btrfs_set_extent_refs(&extent_item, 1); |
464 | btrfs_set_extent_owner(&extent_item, owner); | |
fec577fb | 465 | |
037e6390 | 466 | if (root == extent_root) { |
9f5fae2f | 467 | BUG_ON(extent_root->fs_info->current_insert.offset == 0); |
037e6390 | 468 | BUG_ON(num_blocks != 1); |
9f5fae2f CM |
469 | BUG_ON(extent_root->fs_info->current_insert.flags == |
470 | extent_root->fs_info->current_insert.offset); | |
037e6390 | 471 | ins->offset = 1; |
9f5fae2f CM |
472 | ins->objectid = extent_root->fs_info->current_insert.objectid + |
473 | extent_root->fs_info->current_insert.flags++; | |
fec577fb CM |
474 | return 0; |
475 | } | |
e089f05c | 476 | ret = find_free_extent(trans, root, num_blocks, search_start, |
037e6390 CM |
477 | search_end, ins); |
478 | if (ret) | |
479 | return ret; | |
fec577fb | 480 | |
1261ec42 CM |
481 | super_blocks_used = btrfs_super_blocks_used(info->disk_super); |
482 | btrfs_set_super_blocks_used(info->disk_super, super_blocks_used + | |
483 | num_blocks); | |
e089f05c CM |
484 | ret = btrfs_insert_item(trans, extent_root, ins, &extent_item, |
485 | sizeof(extent_item)); | |
037e6390 | 486 | |
e089f05c | 487 | finish_current_insert(trans, extent_root); |
e20d96d6 | 488 | pending_ret = del_pending_extents(trans, extent_root); |
037e6390 CM |
489 | if (ret) |
490 | return ret; | |
491 | if (pending_ret) | |
492 | return pending_ret; | |
493 | return 0; | |
fec577fb CM |
494 | } |
495 | ||
496 | /* | |
497 | * helper function to allocate a block for a given tree | |
498 | * returns the tree buffer or NULL. | |
499 | */ | |
e20d96d6 | 500 | struct buffer_head *btrfs_alloc_free_block(struct btrfs_trans_handle *trans, |
e089f05c | 501 | struct btrfs_root *root) |
fec577fb | 502 | { |
e2fa7227 | 503 | struct btrfs_key ins; |
fec577fb | 504 | int ret; |
e20d96d6 | 505 | struct buffer_head *buf; |
fec577fb | 506 | |
dee26a9f | 507 | ret = btrfs_alloc_extent(trans, root, 1, 0, (unsigned long)-1, |
e20d96d6 | 508 | btrfs_header_parentid(btrfs_buffer_header(root->node)), &ins); |
fec577fb CM |
509 | if (ret) { |
510 | BUG(); | |
511 | return NULL; | |
512 | } | |
d98237b3 | 513 | buf = btrfs_find_create_tree_block(root, ins.objectid); |
df2ce34c | 514 | set_buffer_uptodate(buf); |
fec577fb CM |
515 | return buf; |
516 | } | |
a28ec197 | 517 | |
6407bf6d CM |
518 | static int drop_leaf_ref(struct btrfs_trans_handle *trans, |
519 | struct btrfs_root *root, struct buffer_head *cur) | |
520 | { | |
521 | struct btrfs_disk_key *key; | |
522 | struct btrfs_leaf *leaf; | |
523 | struct btrfs_file_extent_item *fi; | |
524 | int i; | |
525 | int nritems; | |
526 | int ret; | |
527 | ||
528 | BUG_ON(!btrfs_is_leaf(btrfs_buffer_node(cur))); | |
529 | leaf = btrfs_buffer_leaf(cur); | |
530 | nritems = btrfs_header_nritems(&leaf->header); | |
531 | for (i = 0; i < nritems; i++) { | |
532 | key = &leaf->items[i].key; | |
533 | if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY) | |
534 | continue; | |
535 | fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item); | |
536 | /* | |
537 | * FIXME make sure to insert a trans record that | |
538 | * repeats the snapshot del on crash | |
539 | */ | |
540 | ret = btrfs_free_extent(trans, root, | |
541 | btrfs_file_extent_disk_blocknr(fi), | |
542 | btrfs_file_extent_disk_num_blocks(fi), | |
543 | 0); | |
544 | BUG_ON(ret); | |
545 | } | |
546 | return 0; | |
547 | } | |
548 | ||
9aca1d51 CM |
549 | /* |
550 | * helper function for drop_snapshot, this walks down the tree dropping ref | |
551 | * counts as it goes. | |
552 | */ | |
e089f05c CM |
553 | static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root |
554 | *root, struct btrfs_path *path, int *level) | |
20524f02 | 555 | { |
e20d96d6 CM |
556 | struct buffer_head *next; |
557 | struct buffer_head *cur; | |
20524f02 CM |
558 | u64 blocknr; |
559 | int ret; | |
560 | u32 refs; | |
561 | ||
5caf2a00 CM |
562 | WARN_ON(*level < 0); |
563 | WARN_ON(*level >= BTRFS_MAX_LEVEL); | |
e20d96d6 | 564 | ret = lookup_block_ref(trans, root, path->nodes[*level]->b_blocknr, |
6407bf6d | 565 | 1, &refs); |
20524f02 CM |
566 | BUG_ON(ret); |
567 | if (refs > 1) | |
568 | goto out; | |
9aca1d51 CM |
569 | /* |
570 | * walk down to the last node level and free all the leaves | |
571 | */ | |
6407bf6d | 572 | while(*level >= 0) { |
5caf2a00 CM |
573 | WARN_ON(*level < 0); |
574 | WARN_ON(*level >= BTRFS_MAX_LEVEL); | |
20524f02 | 575 | cur = path->nodes[*level]; |
2c90e5d6 CM |
576 | if (btrfs_header_level(btrfs_buffer_header(cur)) != *level) |
577 | WARN_ON(1); | |
7518a238 | 578 | if (path->slots[*level] >= |
e20d96d6 | 579 | btrfs_header_nritems(btrfs_buffer_header(cur))) |
20524f02 | 580 | break; |
6407bf6d CM |
581 | if (*level == 0) { |
582 | ret = drop_leaf_ref(trans, root, cur); | |
583 | BUG_ON(ret); | |
584 | break; | |
585 | } | |
e20d96d6 CM |
586 | blocknr = btrfs_node_blockptr(btrfs_buffer_node(cur), |
587 | path->slots[*level]); | |
6407bf6d CM |
588 | ret = lookup_block_ref(trans, root, blocknr, 1, &refs); |
589 | BUG_ON(ret); | |
590 | if (refs != 1) { | |
20524f02 | 591 | path->slots[*level]++; |
e089f05c | 592 | ret = btrfs_free_extent(trans, root, blocknr, 1, 1); |
20524f02 CM |
593 | BUG_ON(ret); |
594 | continue; | |
595 | } | |
20524f02 | 596 | next = read_tree_block(root, blocknr); |
5caf2a00 | 597 | WARN_ON(*level <= 0); |
83e15a28 | 598 | if (path->nodes[*level-1]) |
234b63a0 | 599 | btrfs_block_release(root, path->nodes[*level-1]); |
20524f02 | 600 | path->nodes[*level-1] = next; |
e20d96d6 | 601 | *level = btrfs_header_level(btrfs_buffer_header(next)); |
20524f02 CM |
602 | path->slots[*level] = 0; |
603 | } | |
604 | out: | |
5caf2a00 CM |
605 | WARN_ON(*level < 0); |
606 | WARN_ON(*level >= BTRFS_MAX_LEVEL); | |
6407bf6d CM |
607 | ret = btrfs_free_extent(trans, root, |
608 | path->nodes[*level]->b_blocknr, 1, 1); | |
234b63a0 | 609 | btrfs_block_release(root, path->nodes[*level]); |
20524f02 CM |
610 | path->nodes[*level] = NULL; |
611 | *level += 1; | |
612 | BUG_ON(ret); | |
613 | return 0; | |
614 | } | |
615 | ||
9aca1d51 CM |
616 | /* |
617 | * helper for dropping snapshots. This walks back up the tree in the path | |
618 | * to find the first node higher up where we haven't yet gone through | |
619 | * all the slots | |
620 | */ | |
e089f05c CM |
621 | static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root |
622 | *root, struct btrfs_path *path, int *level) | |
20524f02 CM |
623 | { |
624 | int i; | |
625 | int slot; | |
626 | int ret; | |
234b63a0 | 627 | for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) { |
20524f02 | 628 | slot = path->slots[i]; |
e20d96d6 CM |
629 | if (slot < btrfs_header_nritems( |
630 | btrfs_buffer_header(path->nodes[i])) - 1) { | |
20524f02 CM |
631 | path->slots[i]++; |
632 | *level = i; | |
633 | return 0; | |
634 | } else { | |
e089f05c | 635 | ret = btrfs_free_extent(trans, root, |
e20d96d6 | 636 | path->nodes[*level]->b_blocknr, |
e089f05c | 637 | 1, 1); |
6407bf6d | 638 | BUG_ON(ret); |
234b63a0 | 639 | btrfs_block_release(root, path->nodes[*level]); |
83e15a28 | 640 | path->nodes[*level] = NULL; |
20524f02 | 641 | *level = i + 1; |
20524f02 CM |
642 | } |
643 | } | |
644 | return 1; | |
645 | } | |
646 | ||
9aca1d51 CM |
647 | /* |
648 | * drop the reference count on the tree rooted at 'snap'. This traverses | |
649 | * the tree freeing any blocks that have a ref count of zero after being | |
650 | * decremented. | |
651 | */ | |
e089f05c | 652 | int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root |
e20d96d6 | 653 | *root, struct buffer_head *snap) |
20524f02 | 654 | { |
3768f368 | 655 | int ret = 0; |
9aca1d51 | 656 | int wret; |
20524f02 | 657 | int level; |
5caf2a00 | 658 | struct btrfs_path *path; |
20524f02 CM |
659 | int i; |
660 | int orig_level; | |
661 | ||
5caf2a00 CM |
662 | path = btrfs_alloc_path(); |
663 | BUG_ON(!path); | |
664 | btrfs_init_path(path); | |
20524f02 | 665 | |
e20d96d6 | 666 | level = btrfs_header_level(btrfs_buffer_header(snap)); |
20524f02 | 667 | orig_level = level; |
5caf2a00 CM |
668 | path->nodes[level] = snap; |
669 | path->slots[level] = 0; | |
20524f02 | 670 | while(1) { |
5caf2a00 | 671 | wret = walk_down_tree(trans, root, path, &level); |
9aca1d51 | 672 | if (wret > 0) |
20524f02 | 673 | break; |
9aca1d51 CM |
674 | if (wret < 0) |
675 | ret = wret; | |
676 | ||
5caf2a00 | 677 | wret = walk_up_tree(trans, root, path, &level); |
9aca1d51 | 678 | if (wret > 0) |
20524f02 | 679 | break; |
9aca1d51 CM |
680 | if (wret < 0) |
681 | ret = wret; | |
20524f02 | 682 | } |
83e15a28 | 683 | for (i = 0; i <= orig_level; i++) { |
5caf2a00 CM |
684 | if (path->nodes[i]) { |
685 | btrfs_block_release(root, path->nodes[i]); | |
83e15a28 | 686 | } |
20524f02 | 687 | } |
5caf2a00 | 688 | btrfs_free_path(path); |
9aca1d51 | 689 | return ret; |
20524f02 | 690 | } |