blob: 9cccecc0f431c7771e891b824fa66db7365cd815 [file] [log] [blame]
Chris Mason3768f362007-03-13 16:47:54 -04001#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);
Chris Masone089f052007-03-16 16:20:31 -040023 ret = btrfs_search_slot(NULL, root, &search_key, &path, 0, 0);
Chris Mason3768f362007-03-13 16:47:54 -040024 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;
Chris Mason62e27492007-03-15 12:56:47 -040030 if (btrfs_disk_key_objectid(&l->items[slot].key) != objectid) {
Chris Mason3768f362007-03-13 16:47:54 -040031 ret = 1;
32 goto out;
33 }
Chris Mason123abc82007-03-14 14:14:43 -040034 memcpy(item, btrfs_item_ptr(l, slot, struct btrfs_root_item),
Chris Mason3768f362007-03-13 16:47:54 -040035 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
Chris Masone089f052007-03-16 16:20:31 -040043int btrfs_update_root(struct btrfs_trans_handle *trans, struct btrfs_root
44 *root, struct btrfs_key *key, struct btrfs_root_item
45 *item)
Chris Mason3768f362007-03-13 16:47:54 -040046{
47 struct btrfs_path path;
48 struct btrfs_leaf *l;
49 int ret;
50 int slot;
51
52 btrfs_init_path(&path);
Chris Masone089f052007-03-16 16:20:31 -040053 ret = btrfs_search_slot(trans, root, key, &path, 0, 1);
Chris Mason3768f362007-03-13 16:47:54 -040054 if (ret < 0)
55 goto out;
56 BUG_ON(ret != 0);
57 l = &path.nodes[0]->leaf;
58 slot = path.slots[0];
Chris Mason123abc82007-03-14 14:14:43 -040059 memcpy(btrfs_item_ptr(l, slot, struct btrfs_root_item), item,
Chris Mason3768f362007-03-13 16:47:54 -040060 sizeof(*item));
61out:
62 btrfs_release_path(root, &path);
63 return ret;
64}
65
Chris Masone089f052007-03-16 16:20:31 -040066int btrfs_insert_root(struct btrfs_trans_handle *trans, struct btrfs_root
67 *root, struct btrfs_key *key, struct btrfs_root_item
68 *item)
Chris Mason3768f362007-03-13 16:47:54 -040069{
70 int ret;
Chris Masone089f052007-03-16 16:20:31 -040071 ret = btrfs_insert_item(trans, root, key, item, sizeof(*item));
Chris Mason3768f362007-03-13 16:47:54 -040072 BUG_ON(ret);
73 return ret;
74}
75
Chris Masone089f052007-03-16 16:20:31 -040076int btrfs_del_root(struct btrfs_trans_handle *trans, struct btrfs_root *root,
77 struct btrfs_key *key)
Chris Mason3768f362007-03-13 16:47:54 -040078{
79 struct btrfs_path path;
80 int ret;
81
82 btrfs_init_path(&path);
Chris Masone089f052007-03-16 16:20:31 -040083 ret = btrfs_search_slot(trans, root, key, &path, -1, 1);
Chris Mason3768f362007-03-13 16:47:54 -040084 if (ret < 0)
85 goto out;
86 BUG_ON(ret != 0);
Chris Masone089f052007-03-16 16:20:31 -040087 ret = btrfs_del_item(trans, root, &path);
Chris Mason3768f362007-03-13 16:47:54 -040088out:
89 btrfs_release_path(root, &path);
90 return ret;
91}