transaction handles everywhere
[btrfs-progs-unstable.git] / root-tree.c
blob9cccecc0f431c7771e891b824fa66db7365cd815
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"
9 int btrfs_find_last_root(struct btrfs_root *root, u64 objectid,
10 struct btrfs_root_item *item, struct btrfs_key *key)
12 struct btrfs_path path;
13 struct btrfs_key search_key;
14 struct btrfs_leaf *l;
15 int ret;
16 int slot;
18 search_key.objectid = objectid;
19 search_key.flags = (u32)-1;
20 search_key.offset = (u32)-1;
22 btrfs_init_path(&path);
23 ret = btrfs_search_slot(NULL, root, &search_key, &path, 0, 0);
24 if (ret < 0)
25 goto out;
26 BUG_ON(ret == 0);
27 l = &path.nodes[0]->leaf;
28 BUG_ON(path.slots[0] == 0);
29 slot = path.slots[0] - 1;
30 if (btrfs_disk_key_objectid(&l->items[slot].key) != objectid) {
31 ret = 1;
32 goto out;
34 memcpy(item, btrfs_item_ptr(l, slot, struct btrfs_root_item),
35 sizeof(*item));
36 btrfs_disk_key_to_cpu(key, &l->items[slot].key);
37 btrfs_release_path(root, &path);
38 ret = 0;
39 out:
40 return ret;
43 int btrfs_update_root(struct btrfs_trans_handle *trans, struct btrfs_root
44 *root, struct btrfs_key *key, struct btrfs_root_item
45 *item)
47 struct btrfs_path path;
48 struct btrfs_leaf *l;
49 int ret;
50 int slot;
52 btrfs_init_path(&path);
53 ret = btrfs_search_slot(trans, root, key, &path, 0, 1);
54 if (ret < 0)
55 goto out;
56 BUG_ON(ret != 0);
57 l = &path.nodes[0]->leaf;
58 slot = path.slots[0];
59 memcpy(btrfs_item_ptr(l, slot, struct btrfs_root_item), item,
60 sizeof(*item));
61 out:
62 btrfs_release_path(root, &path);
63 return ret;
66 int btrfs_insert_root(struct btrfs_trans_handle *trans, struct btrfs_root
67 *root, struct btrfs_key *key, struct btrfs_root_item
68 *item)
70 int ret;
71 ret = btrfs_insert_item(trans, root, key, item, sizeof(*item));
72 BUG_ON(ret);
73 return ret;
76 int btrfs_del_root(struct btrfs_trans_handle *trans, struct btrfs_root *root,
77 struct btrfs_key *key)
79 struct btrfs_path path;
80 int ret;
82 btrfs_init_path(&path);
83 ret = btrfs_search_slot(trans, root, key, &path, -1, 1);
84 if (ret < 0)
85 goto out;
86 BUG_ON(ret != 0);
87 ret = btrfs_del_item(trans, root, &path);
88 out:
89 btrfs_release_path(root, &path);
90 return ret;