btrfs_create, btrfs_write_super, btrfs_sync_fs
[deliverable/linux.git] / fs / btrfs / root-tree.c
CommitLineData
2e635a27 1#include <linux/module.h>
3768f368
CM
2#include "ctree.h"
3#include "disk-io.h"
4#include "print-tree.h"
5
6int btrfs_find_last_root(struct btrfs_root *root, u64 objectid,
7 struct btrfs_root_item *item, struct btrfs_key *key)
8{
9 struct btrfs_path path;
10 struct btrfs_key search_key;
11 struct btrfs_leaf *l;
12 int ret;
13 int slot;
14
15 search_key.objectid = objectid;
16 search_key.flags = (u32)-1;
17 search_key.offset = (u32)-1;
18
19 btrfs_init_path(&path);
e089f05c 20 ret = btrfs_search_slot(NULL, root, &search_key, &path, 0, 0);
3768f368
CM
21 if (ret < 0)
22 goto out;
23 BUG_ON(ret == 0);
e20d96d6 24 l = btrfs_buffer_leaf(path.nodes[0]);
3768f368
CM
25 BUG_ON(path.slots[0] == 0);
26 slot = path.slots[0] - 1;
62e2749e 27 if (btrfs_disk_key_objectid(&l->items[slot].key) != objectid) {
3768f368
CM
28 ret = 1;
29 goto out;
30 }
123abc88 31 memcpy(item, btrfs_item_ptr(l, slot, struct btrfs_root_item),
3768f368
CM
32 sizeof(*item));
33 btrfs_disk_key_to_cpu(key, &l->items[slot].key);
34 btrfs_release_path(root, &path);
35 ret = 0;
36out:
37 return ret;
38}
39
e089f05c
CM
40int btrfs_update_root(struct btrfs_trans_handle *trans, struct btrfs_root
41 *root, struct btrfs_key *key, struct btrfs_root_item
42 *item)
3768f368
CM
43{
44 struct btrfs_path path;
45 struct btrfs_leaf *l;
46 int ret;
47 int slot;
48
49 btrfs_init_path(&path);
e089f05c 50 ret = btrfs_search_slot(trans, root, key, &path, 0, 1);
3768f368
CM
51 if (ret < 0)
52 goto out;
53 BUG_ON(ret != 0);
e20d96d6 54 l = btrfs_buffer_leaf(path.nodes[0]);
3768f368 55 slot = path.slots[0];
123abc88 56 memcpy(btrfs_item_ptr(l, slot, struct btrfs_root_item), item,
3768f368 57 sizeof(*item));
d5719762 58 mark_buffer_dirty(path.nodes[0]);
3768f368
CM
59out:
60 btrfs_release_path(root, &path);
61 return ret;
62}
63
e089f05c
CM
64int btrfs_insert_root(struct btrfs_trans_handle *trans, struct btrfs_root
65 *root, struct btrfs_key *key, struct btrfs_root_item
66 *item)
3768f368
CM
67{
68 int ret;
e089f05c 69 ret = btrfs_insert_item(trans, root, key, item, sizeof(*item));
3768f368
CM
70 BUG_ON(ret);
71 return ret;
72}
73
e089f05c
CM
74int btrfs_del_root(struct btrfs_trans_handle *trans, struct btrfs_root *root,
75 struct btrfs_key *key)
3768f368
CM
76{
77 struct btrfs_path path;
78 int ret;
79
80 btrfs_init_path(&path);
e089f05c 81 ret = btrfs_search_slot(trans, root, key, &path, -1, 1);
3768f368
CM
82 if (ret < 0)
83 goto out;
84 BUG_ON(ret != 0);
e089f05c 85 ret = btrfs_del_item(trans, root, &path);
3768f368
CM
86out:
87 btrfs_release_path(root, &path);
88 return ret;
89}
This page took 0.029255 seconds and 5 git commands to generate.