Btrfs-progs: just return -ENOENT if we don't find the root item
[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 "ctree.h"
20 #include "transaction.h"
21 #include "disk-io.h"
22 #include "print-tree.h"
23
24 int btrfs_find_last_root(struct btrfs_root *root, u64 objectid,
25                         struct btrfs_root_item *item, struct btrfs_key *key)
26 {
27         struct btrfs_path *path;
28         struct btrfs_key search_key;
29         struct btrfs_key found_key;
30         struct extent_buffer *l;
31         int ret;
32         int slot;
33
34         search_key.objectid = objectid;
35         search_key.type = BTRFS_ROOT_ITEM_KEY;
36         search_key.offset = (u64)-1;
37
38         path = btrfs_alloc_path();
39         BUG_ON(!path);
40         ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
41         if (ret < 0)
42                 goto out;
43         if (path->slots[0] == 0)
44                 return -ENOENT;
45
46         BUG_ON(ret == 0);
47         l = path->nodes[0];
48         slot = path->slots[0] - 1;
49         btrfs_item_key_to_cpu(l, &found_key, slot);
50         if (found_key.objectid != objectid) {
51                 ret = -ENOENT;
52                 goto out;
53         }
54         read_extent_buffer(l, item, btrfs_item_ptr_offset(l, slot),
55                            sizeof(*item));
56         memcpy(key, &found_key, sizeof(found_key));
57         ret = 0;
58 out:
59         btrfs_release_path(path);
60         btrfs_free_path(path);
61         return ret;
62 }
63
64 int btrfs_update_root(struct btrfs_trans_handle *trans, struct btrfs_root
65                       *root, struct btrfs_key *key, struct btrfs_root_item
66                       *item)
67 {
68         struct btrfs_path *path;
69         struct extent_buffer *l;
70         int ret;
71         int slot;
72         unsigned long ptr;
73         u32 old_len;
74
75         path = btrfs_alloc_path();
76         BUG_ON(!path);
77         ret = btrfs_search_slot(trans, root, key, path, 0, 1);
78         if (ret < 0)
79                 goto out;
80         BUG_ON(ret != 0);
81         l = path->nodes[0];
82         slot = path->slots[0];
83         ptr = btrfs_item_ptr_offset(l, slot);
84         old_len = btrfs_item_size_nr(l, slot);
85
86         /*
87          * If this is the first time we update the root item which originated
88          * from an older kernel, we need to enlarge the item size to make room
89          * for the added fields.
90          */
91         if (old_len < sizeof(*item)) {
92                 btrfs_release_path(path);
93                 ret = btrfs_search_slot(trans, root, key, path,
94                                 -1, 1);
95                 if (ret < 0) {
96                         goto out;
97                 }
98
99                 ret = btrfs_del_item(trans, root, path);
100                 if (ret < 0) {
101                         goto out;
102                 }
103                 btrfs_release_path(path);
104                 ret = btrfs_insert_empty_item(trans, root, path,
105                                 key, sizeof(*item));
106                 if (ret < 0) {
107                         goto out;
108                 }
109                 l = path->nodes[0];
110                 slot = path->slots[0];
111                 ptr = btrfs_item_ptr_offset(l, slot);
112         }
113
114         /*
115          * Update generation_v2 so at the next mount we know the new root
116          * fields are valid.
117          */
118         btrfs_set_root_generation_v2(item, btrfs_root_generation(item));
119
120         write_extent_buffer(l, item, ptr, sizeof(*item));
121         btrfs_mark_buffer_dirty(path->nodes[0]);
122 out:
123         btrfs_release_path(path);
124         btrfs_free_path(path);
125         return ret;
126 }
127
128 int btrfs_insert_root(struct btrfs_trans_handle *trans, struct btrfs_root
129                       *root, struct btrfs_key *key, struct btrfs_root_item
130                       *item)
131 {
132         int ret;
133
134         /*
135          * Make sure generation v1 and v2 match. See update_root for details.
136          */
137         btrfs_set_root_generation_v2(item, btrfs_root_generation(item));
138         ret = btrfs_insert_item(trans, root, key, item, sizeof(*item));
139         return ret;
140 }
141
142 /*
143  * add a btrfs_root_ref item.  type is either BTRFS_ROOT_REF_KEY
144  * or BTRFS_ROOT_BACKREF_KEY.
145  *
146  * The dirid, sequence, name and name_len refer to the directory entry
147  * that is referencing the root.
148  *
149  * For a forward ref, the root_id is the id of the tree referencing
150  * the root and ref_id is the id of the subvol  or snapshot.
151  *
152  * For a back ref the root_id is the id of the subvol or snapshot and
153  * ref_id is the id of the tree referencing it.
154  */
155 int btrfs_add_root_ref(struct btrfs_trans_handle *trans,
156                        struct btrfs_root *tree_root,
157                        u64 root_id, u8 type, u64 ref_id,
158                        u64 dirid, u64 sequence,
159                        const char *name, int name_len)
160 {
161         struct btrfs_key key;
162         int ret;
163         struct btrfs_path *path;
164         struct btrfs_root_ref *ref;
165         struct extent_buffer *leaf;
166         unsigned long ptr;
167
168
169         path = btrfs_alloc_path();
170         if (!path)
171                 return -ENOMEM;
172
173         key.objectid = root_id;
174         key.type = type;
175         key.offset = ref_id;
176
177         ret = btrfs_insert_empty_item(trans, tree_root, path, &key,
178                                       sizeof(*ref) + name_len);
179         BUG_ON(ret);
180
181         leaf = path->nodes[0];
182         ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
183         btrfs_set_root_ref_dirid(leaf, ref, dirid);
184         btrfs_set_root_ref_sequence(leaf, ref, sequence);
185         btrfs_set_root_ref_name_len(leaf, ref, name_len);
186         ptr = (unsigned long)(ref + 1);
187         write_extent_buffer(leaf, name, ptr, name_len);
188         btrfs_mark_buffer_dirty(leaf);
189
190         btrfs_free_path(path);
191         return ret;
192 }