Btrfs: transaction handles everywhere
[linux-2.6-block.git] / fs / btrfs / root-tree.c
CommitLineData
3768f368
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
9int btrfs_find_last_root(struct btrfs_root *root, u64 objectid,
10 struct btrfs_root_item *item, struct btrfs_key *key)
11{
12 struct btrfs_path path;
13 struct btrfs_key search_key;
14 struct btrfs_leaf *l;
15 int ret;
16 int slot;
17
18 search_key.objectid = objectid;
19 search_key.flags = (u32)-1;
20 search_key.offset = (u32)-1;
21
22 btrfs_init_path(&path);
e089f05c 23 ret = btrfs_search_slot(NULL, root, &search_key, &path, 0, 0);
3768f368
CM
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;
62e2749e 30 if (btrfs_disk_key_objectid(&l->items[slot].key) != objectid) {
3768f368
CM
31 ret = 1;
32 goto out;
33 }
123abc88 34 memcpy(item, btrfs_item_ptr(l, slot, struct btrfs_root_item),
3768f368
CM
35 sizeof(*item));
36 btrfs_disk_key_to_cpu(key, &l->items[slot].key);
37 btrfs_release_path(root, &path);
38 ret = 0;
39out:
40 return ret;
41}
42
e089f05c
CM
43int btrfs_update_root(struct btrfs_trans_handle *trans, struct btrfs_root
44 *root, struct btrfs_key *key, struct btrfs_root_item
45 *item)
3768f368
CM
46{
47 struct btrfs_path path;
48 struct btrfs_leaf *l;
49 int ret;
50 int slot;
51
52 btrfs_init_path(&path);
e089f05c 53 ret = btrfs_search_slot(trans, root, key, &path, 0, 1);
3768f368
CM
54 if (ret < 0)
55 goto out;
56 BUG_ON(ret != 0);
57 l = &path.nodes[0]->leaf;
58 slot = path.slots[0];
123abc88 59 memcpy(btrfs_item_ptr(l, slot, struct btrfs_root_item), item,
3768f368
CM
60 sizeof(*item));
61out:
62 btrfs_release_path(root, &path);
63 return ret;
64}
65
e089f05c
CM
66int btrfs_insert_root(struct btrfs_trans_handle *trans, struct btrfs_root
67 *root, struct btrfs_key *key, struct btrfs_root_item
68 *item)
3768f368
CM
69{
70 int ret;
e089f05c 71 ret = btrfs_insert_item(trans, root, key, item, sizeof(*item));
3768f368
CM
72 BUG_ON(ret);
73 return ret;
74}
75
e089f05c
CM
76int btrfs_del_root(struct btrfs_trans_handle *trans, struct btrfs_root *root,
77 struct btrfs_key *key)
3768f368
CM
78{
79 struct btrfs_path path;
80 int ret;
81
82 btrfs_init_path(&path);
e089f05c 83 ret = btrfs_search_slot(trans, root, key, &path, -1, 1);
3768f368
CM
84 if (ret < 0)
85 goto out;
86 BUG_ON(ret != 0);
e089f05c 87 ret = btrfs_del_item(trans, root, &path);
3768f368
CM
88out:
89 btrfs_release_path(root, &path);
90 return ret;
91}