blob: 52c83be4b307e570b3fbdd73bcd80cf6ebafd712 [file] [log] [blame]
Chris Mason2e635a22007-03-21 11:12:56 -04001#include <linux/module.h>
Chris Mason3768f362007-03-13 16:47:54 -04002#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);
Chris Masone089f052007-03-16 16:20:31 -040020 ret = btrfs_search_slot(NULL, root, &search_key, &path, 0, 0);
Chris Mason3768f362007-03-13 16:47:54 -040021 if (ret < 0)
22 goto out;
23 BUG_ON(ret == 0);
24 l = &path.nodes[0]->leaf;
25 BUG_ON(path.slots[0] == 0);
26 slot = path.slots[0] - 1;
Chris Mason62e27492007-03-15 12:56:47 -040027 if (btrfs_disk_key_objectid(&l->items[slot].key) != objectid) {
Chris Mason3768f362007-03-13 16:47:54 -040028 ret = 1;
29 goto out;
30 }
Chris Mason123abc82007-03-14 14:14:43 -040031 memcpy(item, btrfs_item_ptr(l, slot, struct btrfs_root_item),
Chris Mason3768f362007-03-13 16:47:54 -040032 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
Chris Masone089f052007-03-16 16:20:31 -040040int btrfs_update_root(struct btrfs_trans_handle *trans, struct btrfs_root
41 *root, struct btrfs_key *key, struct btrfs_root_item
42 *item)
Chris Mason3768f362007-03-13 16:47:54 -040043{
44 struct btrfs_path path;
45 struct btrfs_leaf *l;
46 int ret;
47 int slot;
48
49 btrfs_init_path(&path);
Chris Masone089f052007-03-16 16:20:31 -040050 ret = btrfs_search_slot(trans, root, key, &path, 0, 1);
Chris Mason3768f362007-03-13 16:47:54 -040051 if (ret < 0)
52 goto out;
53 BUG_ON(ret != 0);
54 l = &path.nodes[0]->leaf;
55 slot = path.slots[0];
Chris Mason123abc82007-03-14 14:14:43 -040056 memcpy(btrfs_item_ptr(l, slot, struct btrfs_root_item), item,
Chris Mason3768f362007-03-13 16:47:54 -040057 sizeof(*item));
58out:
59 btrfs_release_path(root, &path);
60 return ret;
61}
62
Chris Masone089f052007-03-16 16:20:31 -040063int btrfs_insert_root(struct btrfs_trans_handle *trans, struct btrfs_root
64 *root, struct btrfs_key *key, struct btrfs_root_item
65 *item)
Chris Mason3768f362007-03-13 16:47:54 -040066{
67 int ret;
Chris Masone089f052007-03-16 16:20:31 -040068 ret = btrfs_insert_item(trans, root, key, item, sizeof(*item));
Chris Mason3768f362007-03-13 16:47:54 -040069 BUG_ON(ret);
70 return ret;
71}
72
Chris Masone089f052007-03-16 16:20:31 -040073int btrfs_del_root(struct btrfs_trans_handle *trans, struct btrfs_root *root,
74 struct btrfs_key *key)
Chris Mason3768f362007-03-13 16:47:54 -040075{
76 struct btrfs_path path;
77 int ret;
78
79 btrfs_init_path(&path);
Chris Masone089f052007-03-16 16:20:31 -040080 ret = btrfs_search_slot(trans, root, key, &path, -1, 1);
Chris Mason3768f362007-03-13 16:47:54 -040081 if (ret < 0)
82 goto out;
83 BUG_ON(ret != 0);
Chris Masone089f052007-03-16 16:20:31 -040084 ret = btrfs_del_item(trans, root, &path);
Chris Mason3768f362007-03-13 16:47:54 -040085out:
86 btrfs_release_path(root, &path);
87 return ret;
88}