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