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