]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - fs/btrfs/extent-tree.c
Btrfs: struct item endian fixes
[mirror_ubuntu-bionic-kernel.git] / fs / btrfs / extent-tree.c
CommitLineData
fec577fb
CM
1#include <stdio.h>
2#include <stdlib.h>
3#include "kerncompat.h"
4#include "radix-tree.h"
5#include "ctree.h"
6#include "disk-io.h"
7#include "print-tree.h"
8
037e6390 9static int find_free_extent(struct ctree_root *orig_root, u64 num_blocks,
e2fa7227
CM
10 u64 search_start, u64 search_end,
11 struct btrfs_key *ins);
037e6390
CM
12static int finish_current_insert(struct ctree_root *extent_root);
13static int run_pending(struct ctree_root *extent_root);
14
fec577fb
CM
15/*
16 * pending extents are blocks that we're trying to allocate in the extent
17 * map while trying to grow the map because of other allocations. To avoid
18 * recursing, they are tagged in the radix tree and cleaned up after
19 * other allocations are done. The pending tag is also used in the same
20 * manner for deletes.
21 */
037e6390 22#define CTREE_EXTENT_PENDING_DEL 0
fec577fb 23
02217ed2
CM
24static int inc_block_ref(struct ctree_root *root, u64 blocknr)
25{
26 struct ctree_path path;
27 int ret;
e2fa7227 28 struct btrfs_key key;
02217ed2
CM
29 struct leaf *l;
30 struct extent_item *item;
e2fa7227 31 struct btrfs_key ins;
037e6390
CM
32
33 find_free_extent(root->extent_root, 0, 0, (u64)-1, &ins);
02217ed2
CM
34 init_path(&path);
35 key.objectid = blocknr;
36 key.flags = 0;
37 key.offset = 1;
38 ret = search_slot(root->extent_root, &key, &path, 0, 1);
a28ec197
CM
39 if (ret != 0)
40 BUG();
02217ed2
CM
41 BUG_ON(ret != 0);
42 l = &path.nodes[0]->leaf;
0783fcfc
CM
43 item = (struct extent_item *)(l->data + btrfs_item_offset(l->items +
44 path.slots[0]));
02217ed2 45 item->refs++;
a28ec197 46
02217ed2
CM
47 BUG_ON(list_empty(&path.nodes[0]->dirty));
48 release_path(root->extent_root, &path);
037e6390
CM
49 finish_current_insert(root->extent_root);
50 run_pending(root->extent_root);
02217ed2
CM
51 return 0;
52}
53
20524f02 54static int lookup_block_ref(struct ctree_root *root, u64 blocknr, u32 *refs)
a28ec197
CM
55{
56 struct ctree_path path;
57 int ret;
e2fa7227 58 struct btrfs_key key;
a28ec197
CM
59 struct leaf *l;
60 struct extent_item *item;
61 init_path(&path);
62 key.objectid = blocknr;
63 key.flags = 0;
64 key.offset = 1;
65 ret = search_slot(root->extent_root, &key, &path, 0, 0);
66 if (ret != 0)
67 BUG();
68 l = &path.nodes[0]->leaf;
69 item = (struct extent_item *)(l->data +
0783fcfc
CM
70 btrfs_item_offset(l->items +
71 path.slots[0]));
a28ec197
CM
72 *refs = item->refs;
73 release_path(root->extent_root, &path);
74 return 0;
75}
76
02217ed2
CM
77int btrfs_inc_ref(struct ctree_root *root, struct tree_buffer *buf)
78{
79 u64 blocknr;
80 int i;
a28ec197
CM
81
82 if (root == root->extent_root)
83 return 0;
7518a238 84 if (btrfs_is_leaf(&buf->node))
a28ec197
CM
85 return 0;
86
7518a238 87 for (i = 0; i < btrfs_header_nritems(&buf->node.header); i++) {
02217ed2
CM
88 blocknr = buf->node.blockptrs[i];
89 inc_block_ref(root, blocknr);
90 }
91 return 0;
92}
93
a28ec197
CM
94int btrfs_finish_extent_commit(struct ctree_root *root)
95{
96 struct ctree_root *extent_root = root->extent_root;
97 unsigned long gang[8];
98 int ret;
99 int i;
100
101 while(1) {
102 ret = radix_tree_gang_lookup(&extent_root->pinned_radix,
103 (void **)gang, 0,
104 ARRAY_SIZE(gang));
105 if (!ret)
106 break;
0579da42 107 for (i = 0; i < ret; i++) {
a28ec197 108 radix_tree_delete(&extent_root->pinned_radix, gang[i]);
0579da42 109 }
a28ec197 110 }
0579da42
CM
111 extent_root->last_insert.objectid = 0;
112 extent_root->last_insert.offset = 0;
a28ec197
CM
113 return 0;
114}
115
037e6390
CM
116static int finish_current_insert(struct ctree_root *extent_root)
117{
e2fa7227 118 struct btrfs_key ins;
037e6390
CM
119 struct extent_item extent_item;
120 int i;
121 int ret;
122
123 extent_item.refs = 1;
7518a238
CM
124 extent_item.owner =
125 btrfs_header_parentid(&extent_root->node->node.header);
037e6390
CM
126 ins.offset = 1;
127 ins.flags = 0;
128
129 for (i = 0; i < extent_root->current_insert.flags; i++) {
130 ins.objectid = extent_root->current_insert.objectid + i;
131 ret = insert_item(extent_root, &ins, &extent_item,
132 sizeof(extent_item));
133 BUG_ON(ret);
134 }
135 extent_root->current_insert.offset = 0;
136 return 0;
137}
138
fec577fb 139/*
a28ec197 140 * remove an extent from the root, returns 0 on success
fec577fb 141 */
a28ec197
CM
142int __free_extent(struct ctree_root *root, u64 blocknr, u64 num_blocks)
143{
144 struct ctree_path path;
e2fa7227 145 struct btrfs_key key;
a28ec197
CM
146 struct ctree_root *extent_root = root->extent_root;
147 int ret;
0783fcfc 148 struct btrfs_item *item;
a28ec197 149 struct extent_item *ei;
e2fa7227 150 struct btrfs_key ins;
037e6390 151
a28ec197
CM
152 key.objectid = blocknr;
153 key.flags = 0;
154 key.offset = num_blocks;
155
037e6390 156 find_free_extent(root, 0, 0, (u64)-1, &ins);
a28ec197
CM
157 init_path(&path);
158 ret = search_slot(extent_root, &key, &path, -1, 1);
159 if (ret) {
160 printf("failed to find %Lu\n", key.objectid);
161 print_tree(extent_root, extent_root->node);
162 printf("failed to find %Lu\n", key.objectid);
163 BUG();
164 }
165 item = path.nodes[0]->leaf.items + path.slots[0];
0783fcfc
CM
166 ei = (struct extent_item *)(path.nodes[0]->leaf.data +
167 btrfs_item_offset(item));
a28ec197
CM
168 BUG_ON(ei->refs == 0);
169 ei->refs--;
170 if (ei->refs == 0) {
171 if (root == extent_root) {
172 int err;
173 radix_tree_preload(GFP_KERNEL);
174 err = radix_tree_insert(&extent_root->pinned_radix,
175 blocknr, (void *)blocknr);
176 BUG_ON(err);
177 radix_tree_preload_end();
178 }
179 ret = del_item(extent_root, &path);
0579da42
CM
180 if (root != extent_root &&
181 extent_root->last_insert.objectid < blocknr)
182 extent_root->last_insert.objectid = blocknr;
a28ec197
CM
183 if (ret)
184 BUG();
185 }
186 release_path(extent_root, &path);
037e6390 187 finish_current_insert(extent_root);
a28ec197
CM
188 return ret;
189}
190
a28ec197
CM
191/*
192 * find all the blocks marked as pending in the radix tree and remove
193 * them from the extent map
194 */
195static int del_pending_extents(struct ctree_root *extent_root)
196{
197 int ret;
198 struct tree_buffer *gang[4];
199 int i;
200
201 while(1) {
202 ret = radix_tree_gang_lookup_tag(&extent_root->cache_radix,
203 (void **)gang, 0,
204 ARRAY_SIZE(gang),
205 CTREE_EXTENT_PENDING_DEL);
206 if (!ret)
207 break;
208 for (i = 0; i < ret; i++) {
209 ret = __free_extent(extent_root, gang[i]->blocknr, 1);
fec577fb
CM
210 radix_tree_tag_clear(&extent_root->cache_radix,
211 gang[i]->blocknr,
a28ec197 212 CTREE_EXTENT_PENDING_DEL);
fec577fb
CM
213 tree_block_release(extent_root, gang[i]);
214 }
215 }
216 return 0;
217}
218
a28ec197
CM
219static int run_pending(struct ctree_root *extent_root)
220{
221 while(radix_tree_tagged(&extent_root->cache_radix,
037e6390 222 CTREE_EXTENT_PENDING_DEL))
a28ec197 223 del_pending_extents(extent_root);
a28ec197
CM
224 return 0;
225}
226
227
fec577fb
CM
228/*
229 * remove an extent from the root, returns 0 on success
230 */
231int free_extent(struct ctree_root *root, u64 blocknr, u64 num_blocks)
232{
e2fa7227 233 struct btrfs_key key;
fec577fb
CM
234 struct ctree_root *extent_root = root->extent_root;
235 struct tree_buffer *t;
236 int pending_ret;
237 int ret;
a28ec197 238
fec577fb 239 if (root == extent_root) {
a28ec197 240 t = find_tree_block(root, blocknr);
037e6390 241 radix_tree_tag_set(&root->cache_radix, blocknr,
a28ec197 242 CTREE_EXTENT_PENDING_DEL);
fec577fb
CM
243 return 0;
244 }
a28ec197
CM
245 key.objectid = blocknr;
246 key.flags = 0;
247 key.offset = num_blocks;
248 ret = __free_extent(root, blocknr, num_blocks);
249 pending_ret = run_pending(root->extent_root);
fec577fb
CM
250 return ret ? ret : pending_ret;
251}
252
253/*
254 * walks the btree of allocated extents and find a hole of a given size.
255 * The key ins is changed to record the hole:
256 * ins->objectid == block start
257 * ins->flags = 0
258 * ins->offset == number of blocks
259 * Any available blocks before search_start are skipped.
260 */
0f70abe2 261static int find_free_extent(struct ctree_root *orig_root, u64 num_blocks,
e2fa7227
CM
262 u64 search_start, u64 search_end,
263 struct btrfs_key *ins)
fec577fb
CM
264{
265 struct ctree_path path;
e2fa7227 266 struct btrfs_key key;
fec577fb
CM
267 int ret;
268 u64 hole_size = 0;
269 int slot = 0;
270 u64 last_block;
037e6390 271 u64 test_block;
fec577fb
CM
272 int start_found;
273 struct leaf *l;
274 struct ctree_root * root = orig_root->extent_root;
0579da42 275 int total_needed = num_blocks;
fec577fb 276
7518a238 277 total_needed += (btrfs_header_level(&root->node->node.header) + 1) * 3;
0579da42
CM
278 if (root->last_insert.objectid > search_start)
279 search_start = root->last_insert.objectid;
fec577fb
CM
280check_failed:
281 init_path(&path);
282 ins->objectid = search_start;
283 ins->offset = 0;
284 ins->flags = 0;
285 start_found = 0;
02217ed2 286 ret = search_slot(root, ins, &path, 0, 0);
0f70abe2
CM
287 if (ret < 0)
288 goto error;
aa5d6bed 289
0579da42
CM
290 if (path.slots[0] > 0)
291 path.slots[0]--;
292
fec577fb
CM
293 while (1) {
294 l = &path.nodes[0]->leaf;
295 slot = path.slots[0];
7518a238 296 if (slot >= btrfs_header_nritems(&l->header)) {
fec577fb
CM
297 ret = next_leaf(root, &path);
298 if (ret == 0)
299 continue;
0f70abe2
CM
300 if (ret < 0)
301 goto error;
fec577fb
CM
302 if (!start_found) {
303 ins->objectid = search_start;
037e6390 304 ins->offset = (u64)-1;
fec577fb
CM
305 start_found = 1;
306 goto check_pending;
307 }
308 ins->objectid = last_block > search_start ?
309 last_block : search_start;
037e6390 310 ins->offset = (u64)-1;
fec577fb
CM
311 goto check_pending;
312 }
e2fa7227
CM
313 btrfs_disk_key_to_cpu(&key, &l->items[slot].key);
314 if (key.objectid >= search_start) {
fec577fb 315 if (start_found) {
0579da42
CM
316 if (last_block < search_start)
317 last_block = search_start;
e2fa7227 318 hole_size = key.objectid - last_block;
037e6390 319 if (hole_size > total_needed) {
fec577fb 320 ins->objectid = last_block;
037e6390 321 ins->offset = hole_size;
fec577fb
CM
322 goto check_pending;
323 }
0579da42 324 }
fec577fb 325 }
0579da42 326 start_found = 1;
e2fa7227 327 last_block = key.objectid + key.offset;
fec577fb
CM
328 path.slots[0]++;
329 }
330 // FIXME -ENOSPC
331check_pending:
332 /* we have to make sure we didn't find an extent that has already
333 * been allocated by the map tree or the original allocation
334 */
335 release_path(root, &path);
336 BUG_ON(ins->objectid < search_start);
037e6390
CM
337 for (test_block = ins->objectid;
338 test_block < ins->objectid + total_needed; test_block++) {
339 if (radix_tree_lookup(&root->pinned_radix, test_block)) {
340 search_start = test_block + 1;
fec577fb
CM
341 goto check_failed;
342 }
343 }
037e6390 344 BUG_ON(root->current_insert.offset);
0579da42 345 root->current_insert.offset = total_needed - num_blocks;
037e6390
CM
346 root->current_insert.objectid = ins->objectid + num_blocks;
347 root->current_insert.flags = 0;
0579da42 348 root->last_insert.objectid = ins->objectid;
037e6390 349 ins->offset = num_blocks;
fec577fb 350 return 0;
0f70abe2
CM
351error:
352 release_path(root, &path);
353 return ret;
fec577fb
CM
354}
355
fec577fb
CM
356/*
357 * finds a free extent and does all the dirty work required for allocation
358 * returns the key for the extent through ins, and a tree buffer for
359 * the first block of the extent through buf.
360 *
361 * returns 0 if everything worked, non-zero otherwise.
362 */
363int alloc_extent(struct ctree_root *root, u64 num_blocks, u64 search_start,
e2fa7227 364 u64 search_end, u64 owner, struct btrfs_key *ins)
fec577fb
CM
365{
366 int ret;
367 int pending_ret;
037e6390 368 struct ctree_root *extent_root = root->extent_root;
fec577fb 369 struct extent_item extent_item;
037e6390 370
fec577fb
CM
371 extent_item.refs = 1;
372 extent_item.owner = owner;
373
037e6390
CM
374 if (root == extent_root) {
375 BUG_ON(extent_root->current_insert.offset == 0);
376 BUG_ON(num_blocks != 1);
377 BUG_ON(extent_root->current_insert.flags ==
378 extent_root->current_insert.offset);
379 ins->offset = 1;
380 ins->objectid = extent_root->current_insert.objectid +
381 extent_root->current_insert.flags++;
fec577fb
CM
382 return 0;
383 }
037e6390
CM
384 ret = find_free_extent(root, num_blocks, search_start,
385 search_end, ins);
386 if (ret)
387 return ret;
fec577fb 388
037e6390
CM
389 ret = insert_item(extent_root, ins, &extent_item,
390 sizeof(extent_item));
391
392 finish_current_insert(extent_root);
393 pending_ret = run_pending(extent_root);
394 if (ret)
395 return ret;
396 if (pending_ret)
397 return pending_ret;
398 return 0;
fec577fb
CM
399}
400
401/*
402 * helper function to allocate a block for a given tree
403 * returns the tree buffer or NULL.
404 */
405struct tree_buffer *alloc_free_block(struct ctree_root *root)
406{
e2fa7227 407 struct btrfs_key ins;
fec577fb 408 int ret;
037e6390 409 struct tree_buffer *buf;
fec577fb
CM
410
411 ret = alloc_extent(root, 1, 0, (unsigned long)-1,
7518a238 412 btrfs_header_parentid(&root->node->node.header),
037e6390 413 &ins);
fec577fb
CM
414 if (ret) {
415 BUG();
416 return NULL;
417 }
037e6390
CM
418 buf = find_tree_block(root, ins.objectid);
419 dirty_tree_block(root, buf);
fec577fb
CM
420 return buf;
421}
a28ec197 422
20524f02
CM
423int walk_down_tree(struct ctree_root *root, struct ctree_path *path, int *level)
424{
425 struct tree_buffer *next;
426 struct tree_buffer *cur;
427 u64 blocknr;
428 int ret;
429 u32 refs;
430
431 ret = lookup_block_ref(root, path->nodes[*level]->blocknr, &refs);
432 BUG_ON(ret);
433 if (refs > 1)
434 goto out;
435 while(*level > 0) {
436 cur = path->nodes[*level];
7518a238
CM
437 if (path->slots[*level] >=
438 btrfs_header_nritems(&cur->node.header))
20524f02
CM
439 break;
440 blocknr = cur->node.blockptrs[path->slots[*level]];
441 ret = lookup_block_ref(root, blocknr, &refs);
442 if (refs != 1 || *level == 1) {
443 path->slots[*level]++;
444 ret = free_extent(root, blocknr, 1);
445 BUG_ON(ret);
446 continue;
447 }
448 BUG_ON(ret);
449 next = read_tree_block(root, blocknr);
83e15a28 450 if (path->nodes[*level-1])
20524f02 451 tree_block_release(root, path->nodes[*level-1]);
20524f02 452 path->nodes[*level-1] = next;
7518a238 453 *level = btrfs_header_level(&next->node.header);
20524f02
CM
454 path->slots[*level] = 0;
455 }
456out:
457 ret = free_extent(root, path->nodes[*level]->blocknr, 1);
83e15a28 458 tree_block_release(root, path->nodes[*level]);
20524f02
CM
459 path->nodes[*level] = NULL;
460 *level += 1;
461 BUG_ON(ret);
462 return 0;
463}
464
465int walk_up_tree(struct ctree_root *root, struct ctree_path *path, int *level)
466{
467 int i;
468 int slot;
469 int ret;
470 for(i = *level; i < MAX_LEVEL - 1 && path->nodes[i]; i++) {
471 slot = path->slots[i];
7518a238
CM
472 if (slot <
473 btrfs_header_nritems(&path->nodes[i]->node.header)- 1) {
20524f02
CM
474 path->slots[i]++;
475 *level = i;
476 return 0;
477 } else {
478 ret = free_extent(root,
479 path->nodes[*level]->blocknr, 1);
83e15a28
CM
480 tree_block_release(root, path->nodes[*level]);
481 path->nodes[*level] = NULL;
20524f02
CM
482 *level = i + 1;
483 BUG_ON(ret);
484 }
485 }
486 return 1;
487}
488
489int btrfs_drop_snapshot(struct ctree_root *root, struct tree_buffer *snap)
490{
491 int ret;
492 int level;
493 struct ctree_path path;
494 int i;
495 int orig_level;
496
497 init_path(&path);
498
7518a238 499 level = btrfs_header_level(&snap->node.header);
20524f02
CM
500 orig_level = level;
501 path.nodes[level] = snap;
502 path.slots[level] = 0;
503 while(1) {
504 ret = walk_down_tree(root, &path, &level);
505 if (ret > 0)
506 break;
507 ret = walk_up_tree(root, &path, &level);
508 if (ret > 0)
509 break;
510 }
83e15a28
CM
511 for (i = 0; i <= orig_level; i++) {
512 if (path.nodes[i]) {
20524f02 513 tree_block_release(root, path.nodes[i]);
83e15a28 514 }
20524f02
CM
515 }
516
517 return 0;
518}