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