btrfs-progs: set generation_v2 any time we write a new 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
73         path = btrfs_alloc_path();
74         BUG_ON(!path);
75         ret = btrfs_search_slot(trans, root, key, path, 0, 1);
76         if (ret < 0)
77                 goto out;
78         BUG_ON(ret != 0);
79         l = path->nodes[0];
80         slot = path->slots[0];
81         ptr = btrfs_item_ptr_offset(l, slot);
82         write_extent_buffer(l, item, ptr, sizeof(*item));
83         btrfs_mark_buffer_dirty(path->nodes[0]);
84 out:
85         btrfs_release_path(root, path);
86         btrfs_free_path(path);
87         return ret;
88 }
89
90 int btrfs_insert_root(struct btrfs_trans_handle *trans, struct btrfs_root
91                       *root, struct btrfs_key *key, struct btrfs_root_item
92                       *item)
93 {
94         int ret;
95
96         /*
97          * Make sure generation v1 and v2 match. See update_root for details.
98          */
99         btrfs_set_root_generation_v2(item, btrfs_root_generation(item));
100         ret = btrfs_insert_item(trans, root, key, item, sizeof(*item));
101         return ret;
102 }
103
104 #if 0
105 int btrfs_find_dead_roots(struct btrfs_root *root, u64 objectid,
106                           struct btrfs_root *latest)
107 {
108         struct btrfs_root *dead_root;
109         struct btrfs_item *item;
110         struct btrfs_root_item *ri;
111         struct btrfs_key key;
112         struct btrfs_path *path;
113         int ret;
114         u32 nritems;
115         struct extent_buffer *leaf;
116         int slot;
117
118         key.objectid = objectid;
119         btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
120         key.offset = 0;
121         path = btrfs_alloc_path();
122         if (!path)
123                 return -ENOMEM;
124         ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
125         if (ret < 0)
126                 goto err;
127         while(1) {
128                 leaf = path->nodes[0];
129                 nritems = btrfs_header_nritems(leaf);
130                 slot = path->slots[0];
131                 if (slot >= nritems) {
132                         ret = btrfs_next_leaf(root, path);
133                         if (ret)
134                                 break;
135                         leaf = path->nodes[0];
136                         nritems = btrfs_header_nritems(leaf);
137                         slot = path->slots[0];
138                 }
139                 item = btrfs_item_nr(leaf, slot);
140                 btrfs_item_key_to_cpu(leaf, &key, slot);
141                 if (btrfs_key_type(&key) != BTRFS_ROOT_ITEM_KEY)
142                         goto next;
143
144                 if (key.objectid < objectid)
145                         goto next;
146
147                 if (key.objectid > objectid)
148                         break;
149
150                 ri = btrfs_item_ptr(leaf, slot, struct btrfs_root_item);
151                 if (btrfs_disk_root_refs(leaf, ri) != 0)
152                         goto next;
153
154                 dead_root = btrfs_read_fs_root_no_radix(root->fs_info, &key);
155                 if (IS_ERR(dead_root)) {
156                         ret = PTR_ERR(dead_root);
157                         goto err;
158                 }
159
160                 ret = btrfs_add_dead_root(dead_root, latest,
161                                           &root->fs_info->dead_roots);
162                 if (ret)
163                         goto err;
164 next:
165                 slot++;
166                 path->slots[0]++;
167         }
168         ret = 0;
169 err:
170         btrfs_free_path(path);
171         return ret;
172 }
173 #endif
174
175 int btrfs_del_root(struct btrfs_trans_handle *trans, struct btrfs_root *root,
176                    struct btrfs_key *key)
177 {
178         struct btrfs_path *path;
179         int ret;
180         u32 refs;
181         struct btrfs_root_item *ri;
182         struct extent_buffer *leaf;
183
184         path = btrfs_alloc_path();
185         BUG_ON(!path);
186         ret = btrfs_search_slot(trans, root, key, path, -1, 1);
187         if (ret < 0)
188                 goto out;
189         BUG_ON(ret != 0);
190         leaf = path->nodes[0];
191         ri = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_item);
192
193         refs = btrfs_disk_root_refs(leaf, ri);
194         BUG_ON(refs != 0);
195         ret = btrfs_del_item(trans, root, path);
196 out:
197         btrfs_release_path(root, path);
198         btrfs_free_path(path);
199         return ret;
200 }
201
202 /*
203  * add a btrfs_root_ref item.  type is either BTRFS_ROOT_REF_KEY
204  * or BTRFS_ROOT_BACKREF_KEY.
205  *
206  * The dirid, sequence, name and name_len refer to the directory entry
207  * that is referencing the root.
208  *
209  * For a forward ref, the root_id is the id of the tree referencing
210  * the root and ref_id is the id of the subvol  or snapshot.
211  *
212  * For a back ref the root_id is the id of the subvol or snapshot and
213  * ref_id is the id of the tree referencing it.
214  */
215 int btrfs_add_root_ref(struct btrfs_trans_handle *trans,
216                        struct btrfs_root *tree_root,
217                        u64 root_id, u8 type, u64 ref_id,
218                        u64 dirid, u64 sequence,
219                        const char *name, int name_len)
220 {
221         struct btrfs_key key;
222         int ret;
223         struct btrfs_path *path;
224         struct btrfs_root_ref *ref;
225         struct extent_buffer *leaf;
226         unsigned long ptr;
227
228
229         path = btrfs_alloc_path();
230
231         key.objectid = root_id;
232         key.type = type;
233         key.offset = ref_id;
234
235         ret = btrfs_insert_empty_item(trans, tree_root, path, &key,
236                                       sizeof(*ref) + name_len);
237         BUG_ON(ret);
238
239         leaf = path->nodes[0];
240         ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
241         btrfs_set_root_ref_dirid(leaf, ref, dirid);
242         btrfs_set_root_ref_sequence(leaf, ref, sequence);
243         btrfs_set_root_ref_name_len(leaf, ref, name_len);
244         ptr = (unsigned long)(ref + 1);
245         write_extent_buffer(leaf, name, ptr, name_len);
246         btrfs_mark_buffer_dirty(leaf);
247
248         btrfs_free_path(path);
249         return ret;
250 }