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