btrfs-progs: update generation_v2 in btrfs_update_root
[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
44         BUG_ON(ret == 0);
45         l = path->nodes[0];
46         BUG_ON(path->slots[0] == 0);
47         slot = path->slots[0] - 1;
48         btrfs_item_key_to_cpu(l, &found_key, slot);
49         if (found_key.objectid != objectid) {
50                 ret = -ENOENT;
51                 goto out;
52         }
53         read_extent_buffer(l, item, btrfs_item_ptr_offset(l, slot),
54                            sizeof(*item));
55         memcpy(key, &found_key, sizeof(found_key));
56         ret = 0;
57 out:
58         btrfs_release_path(root, path);
59         btrfs_free_path(path);
60         return ret;
61 }
62
63 int btrfs_update_root(struct btrfs_trans_handle *trans, struct btrfs_root
64                       *root, struct btrfs_key *key, struct btrfs_root_item
65                       *item)
66 {
67         struct btrfs_path *path;
68         struct extent_buffer *l;
69         int ret;
70         int slot;
71         unsigned long ptr;
72         u32 old_len;
73
74         path = btrfs_alloc_path();
75         BUG_ON(!path);
76         ret = btrfs_search_slot(trans, root, key, path, 0, 1);
77         if (ret < 0)
78                 goto out;
79         BUG_ON(ret != 0);
80         l = path->nodes[0];
81         slot = path->slots[0];
82         ptr = btrfs_item_ptr_offset(l, slot);
83         old_len = btrfs_item_size_nr(l, slot);
84
85         /*
86          * If this is the first time we update the root item which originated
87          * from an older kernel, we need to enlarge the item size to make room
88          * for the added fields.
89          */
90         if (old_len < sizeof(*item)) {
91                 btrfs_release_path(root, path);
92                 ret = btrfs_search_slot(trans, root, key, path,
93                                 -1, 1);
94                 if (ret < 0) {
95                         goto out;
96                 }
97
98                 ret = btrfs_del_item(trans, root, path);
99                 if (ret < 0) {
100                         goto out;
101                 }
102                 btrfs_release_path(root, path);
103                 ret = btrfs_insert_empty_item(trans, root, path,
104                                 key, sizeof(*item));
105                 if (ret < 0) {
106                         goto out;
107                 }
108                 l = path->nodes[0];
109                 slot = path->slots[0];
110                 ptr = btrfs_item_ptr_offset(l, slot);
111         }
112
113         /*
114          * Update generation_v2 so at the next mount we know the new root
115          * fields are valid.
116          */
117         btrfs_set_root_generation_v2(item, btrfs_root_generation(item));
118
119         write_extent_buffer(l, item, ptr, sizeof(*item));
120         btrfs_mark_buffer_dirty(path->nodes[0]);
121 out:
122         btrfs_release_path(root, path);
123         btrfs_free_path(path);
124         return ret;
125 }
126
127 int btrfs_insert_root(struct btrfs_trans_handle *trans, struct btrfs_root
128                       *root, struct btrfs_key *key, struct btrfs_root_item
129                       *item)
130 {
131         int ret;
132
133         /*
134          * Make sure generation v1 and v2 match. See update_root for details.
135          */
136         btrfs_set_root_generation_v2(item, btrfs_root_generation(item));
137         ret = btrfs_insert_item(trans, root, key, item, sizeof(*item));
138         return ret;
139 }
140
141 #if 0
142 int btrfs_find_dead_roots(struct btrfs_root *root, u64 objectid,
143                           struct btrfs_root *latest)
144 {
145         struct btrfs_root *dead_root;
146         struct btrfs_item *item;
147         struct btrfs_root_item *ri;
148         struct btrfs_key key;
149         struct btrfs_path *path;
150         int ret;
151         u32 nritems;
152         struct extent_buffer *leaf;
153         int slot;
154
155         key.objectid = objectid;
156         btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
157         key.offset = 0;
158         path = btrfs_alloc_path();
159         if (!path)
160                 return -ENOMEM;
161         ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
162         if (ret < 0)
163                 goto err;
164         while(1) {
165                 leaf = path->nodes[0];
166                 nritems = btrfs_header_nritems(leaf);
167                 slot = path->slots[0];
168                 if (slot >= nritems) {
169                         ret = btrfs_next_leaf(root, path);
170                         if (ret)
171                                 break;
172                         leaf = path->nodes[0];
173                         nritems = btrfs_header_nritems(leaf);
174                         slot = path->slots[0];
175                 }
176                 item = btrfs_item_nr(leaf, slot);
177                 btrfs_item_key_to_cpu(leaf, &key, slot);
178                 if (btrfs_key_type(&key) != BTRFS_ROOT_ITEM_KEY)
179                         goto next;
180
181                 if (key.objectid < objectid)
182                         goto next;
183
184                 if (key.objectid > objectid)
185                         break;
186
187                 ri = btrfs_item_ptr(leaf, slot, struct btrfs_root_item);
188                 if (btrfs_disk_root_refs(leaf, ri) != 0)
189                         goto next;
190
191                 dead_root = btrfs_read_fs_root_no_radix(root->fs_info, &key);
192                 if (IS_ERR(dead_root)) {
193                         ret = PTR_ERR(dead_root);
194                         goto err;
195                 }
196
197                 ret = btrfs_add_dead_root(dead_root, latest,
198                                           &root->fs_info->dead_roots);
199                 if (ret)
200                         goto err;
201 next:
202                 slot++;
203                 path->slots[0]++;
204         }
205         ret = 0;
206 err:
207         btrfs_free_path(path);
208         return ret;
209 }
210 #endif
211
212 int btrfs_del_root(struct btrfs_trans_handle *trans, struct btrfs_root *root,
213                    struct btrfs_key *key)
214 {
215         struct btrfs_path *path;
216         int ret;
217         u32 refs;
218         struct btrfs_root_item *ri;
219         struct extent_buffer *leaf;
220
221         path = btrfs_alloc_path();
222         BUG_ON(!path);
223         ret = btrfs_search_slot(trans, root, key, path, -1, 1);
224         if (ret < 0)
225                 goto out;
226         BUG_ON(ret != 0);
227         leaf = path->nodes[0];
228         ri = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_item);
229
230         refs = btrfs_disk_root_refs(leaf, ri);
231         BUG_ON(refs != 0);
232         ret = btrfs_del_item(trans, root, path);
233 out:
234         btrfs_release_path(root, path);
235         btrfs_free_path(path);
236         return ret;
237 }
238
239 /*
240  * add a btrfs_root_ref item.  type is either BTRFS_ROOT_REF_KEY
241  * or BTRFS_ROOT_BACKREF_KEY.
242  *
243  * The dirid, sequence, name and name_len refer to the directory entry
244  * that is referencing the root.
245  *
246  * For a forward ref, the root_id is the id of the tree referencing
247  * the root and ref_id is the id of the subvol  or snapshot.
248  *
249  * For a back ref the root_id is the id of the subvol or snapshot and
250  * ref_id is the id of the tree referencing it.
251  */
252 int btrfs_add_root_ref(struct btrfs_trans_handle *trans,
253                        struct btrfs_root *tree_root,
254                        u64 root_id, u8 type, u64 ref_id,
255                        u64 dirid, u64 sequence,
256                        const char *name, int name_len)
257 {
258         struct btrfs_key key;
259         int ret;
260         struct btrfs_path *path;
261         struct btrfs_root_ref *ref;
262         struct extent_buffer *leaf;
263         unsigned long ptr;
264
265
266         path = btrfs_alloc_path();
267
268         key.objectid = root_id;
269         key.type = type;
270         key.offset = ref_id;
271
272         ret = btrfs_insert_empty_item(trans, tree_root, path, &key,
273                                       sizeof(*ref) + name_len);
274         BUG_ON(ret);
275
276         leaf = path->nodes[0];
277         ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
278         btrfs_set_root_ref_dirid(leaf, ref, dirid);
279         btrfs_set_root_ref_sequence(leaf, ref, sequence);
280         btrfs_set_root_ref_name_len(leaf, ref, name_len);
281         ptr = (unsigned long)(ref + 1);
282         write_extent_buffer(leaf, name, ptr, name_len);
283         btrfs_mark_buffer_dirty(leaf);
284
285         btrfs_free_path(path);
286         return ret;
287 }