add GPLv2
[platform/upstream/btrfs-progs.git] / root-tree.c
1 /*
2  * Copyright (C) 2007 Oracle.  All rights reserved.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public
6  * License v2 as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public
14  * License along with this program; if not, write to the
15  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16  * Boston, MA 021110-1307, USA.
17  */
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include "kerncompat.h"
22 #include "radix-tree.h"
23 #include "ctree.h"
24 #include "disk-io.h"
25 #include "print-tree.h"
26
27 int btrfs_find_last_root(struct btrfs_root *root, u64 objectid,
28                         struct btrfs_root_item *item, struct btrfs_key *key)
29 {
30         struct btrfs_path path;
31         struct btrfs_key search_key;
32         struct btrfs_leaf *l;
33         int ret;
34         int slot;
35
36         search_key.objectid = objectid;
37         search_key.flags = (u32)-1;
38         search_key.offset = (u32)-1;
39
40         btrfs_init_path(&path);
41         ret = btrfs_search_slot(NULL, root, &search_key, &path, 0, 0);
42         if (ret < 0)
43                 goto out;
44         BUG_ON(ret == 0);
45         l = &path.nodes[0]->leaf;
46         BUG_ON(path.slots[0] == 0);
47         slot = path.slots[0] - 1;
48         if (btrfs_disk_key_objectid(&l->items[slot].key) != objectid) {
49                 ret = 1;
50                 goto out;
51         }
52         memcpy(item, btrfs_item_ptr(l, slot, struct btrfs_root_item),
53                 sizeof(*item));
54         btrfs_disk_key_to_cpu(key, &l->items[slot].key);
55         btrfs_release_path(root, &path);
56         ret = 0;
57 out:
58         return ret;
59 }
60
61 int btrfs_update_root(struct btrfs_trans_handle *trans, struct btrfs_root
62                       *root, struct btrfs_key *key, struct btrfs_root_item
63                       *item)
64 {
65         struct btrfs_path path;
66         struct btrfs_leaf *l;
67         int ret;
68         int slot;
69
70         btrfs_init_path(&path);
71         ret = btrfs_search_slot(trans, root, key, &path, 0, 1);
72         if (ret < 0)
73                 goto out;
74         BUG_ON(ret != 0);
75         l = &path.nodes[0]->leaf;
76         slot = path.slots[0];
77         memcpy(btrfs_item_ptr(l, slot, struct btrfs_root_item), item,
78                 sizeof(*item));
79 out:
80         btrfs_release_path(root, &path);
81         return ret;
82 }
83
84 int btrfs_insert_root(struct btrfs_trans_handle *trans, struct btrfs_root
85                       *root, struct btrfs_key *key, struct btrfs_root_item
86                       *item)
87 {
88         int ret;
89         ret = btrfs_insert_item(trans, root, key, item, sizeof(*item));
90         BUG_ON(ret);
91         return ret;
92 }
93
94 int btrfs_del_root(struct btrfs_trans_handle *trans, struct btrfs_root *root,
95                    struct btrfs_key *key)
96 {
97         struct btrfs_path path;
98         int ret;
99
100         btrfs_init_path(&path);
101         ret = btrfs_search_slot(trans, root, key, &path, -1, 1);
102         if (ret < 0)
103                 goto out;
104         BUG_ON(ret != 0);
105         ret = btrfs_del_item(trans, root, &path);
106 out:
107         btrfs_release_path(root, &path);
108         return ret;
109 }