Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs
[linux-2.6.git] / fs / btrfs / root-tree.c
blob5bf1ed57f178ea60b7ca8e98aceb6710fc9f55c4
1 /*
2 * Copyright (C) 2007 Oracle. All rights reserved.
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.
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.
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.
19 #include <linux/uuid.h>
20 #include "ctree.h"
21 #include "transaction.h"
22 #include "disk-io.h"
23 #include "print-tree.h"
26 * Read a root item from the tree. In case we detect a root item smaller then
27 * sizeof(root_item), we know it's an old version of the root structure and
28 * initialize all new fields to zero. The same happens if we detect mismatching
29 * generation numbers as then we know the root was once mounted with an older
30 * kernel that was not aware of the root item structure change.
32 void btrfs_read_root_item(struct extent_buffer *eb, int slot,
33 struct btrfs_root_item *item)
35 uuid_le uuid;
36 int len;
37 int need_reset = 0;
39 len = btrfs_item_size_nr(eb, slot);
40 read_extent_buffer(eb, item, btrfs_item_ptr_offset(eb, slot),
41 min_t(int, len, (int)sizeof(*item)));
42 if (len < sizeof(*item))
43 need_reset = 1;
44 if (!need_reset && btrfs_root_generation(item)
45 != btrfs_root_generation_v2(item)) {
46 if (btrfs_root_generation_v2(item) != 0) {
47 printk(KERN_WARNING "btrfs: mismatching "
48 "generation and generation_v2 "
49 "found in root item. This root "
50 "was probably mounted with an "
51 "older kernel. Resetting all "
52 "new fields.\n");
54 need_reset = 1;
56 if (need_reset) {
57 memset(&item->generation_v2, 0,
58 sizeof(*item) - offsetof(struct btrfs_root_item,
59 generation_v2));
61 uuid_le_gen(&uuid);
62 memcpy(item->uuid, uuid.b, BTRFS_UUID_SIZE);
67 * lookup the root with the highest offset for a given objectid. The key we do
68 * find is copied into 'key'. If we find something return 0, otherwise 1, < 0
69 * on error.
71 int btrfs_find_last_root(struct btrfs_root *root, u64 objectid,
72 struct btrfs_root_item *item, struct btrfs_key *key)
74 struct btrfs_path *path;
75 struct btrfs_key search_key;
76 struct btrfs_key found_key;
77 struct extent_buffer *l;
78 int ret;
79 int slot;
81 search_key.objectid = objectid;
82 search_key.type = BTRFS_ROOT_ITEM_KEY;
83 search_key.offset = (u64)-1;
85 path = btrfs_alloc_path();
86 if (!path)
87 return -ENOMEM;
88 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
89 if (ret < 0)
90 goto out;
92 BUG_ON(ret == 0);
93 if (path->slots[0] == 0) {
94 ret = 1;
95 goto out;
97 l = path->nodes[0];
98 slot = path->slots[0] - 1;
99 btrfs_item_key_to_cpu(l, &found_key, slot);
100 if (found_key.objectid != objectid ||
101 found_key.type != BTRFS_ROOT_ITEM_KEY) {
102 ret = 1;
103 goto out;
105 if (item)
106 btrfs_read_root_item(l, slot, item);
107 if (key)
108 memcpy(key, &found_key, sizeof(found_key));
110 ret = 0;
111 out:
112 btrfs_free_path(path);
113 return ret;
116 void btrfs_set_root_node(struct btrfs_root_item *item,
117 struct extent_buffer *node)
119 btrfs_set_root_bytenr(item, node->start);
120 btrfs_set_root_level(item, btrfs_header_level(node));
121 btrfs_set_root_generation(item, btrfs_header_generation(node));
125 * copy the data in 'item' into the btree
127 int btrfs_update_root(struct btrfs_trans_handle *trans, struct btrfs_root
128 *root, struct btrfs_key *key, struct btrfs_root_item
129 *item)
131 struct btrfs_path *path;
132 struct extent_buffer *l;
133 int ret;
134 int slot;
135 unsigned long ptr;
136 int old_len;
138 path = btrfs_alloc_path();
139 if (!path)
140 return -ENOMEM;
142 ret = btrfs_search_slot(trans, root, key, path, 0, 1);
143 if (ret < 0) {
144 btrfs_abort_transaction(trans, root, ret);
145 goto out;
148 if (ret != 0) {
149 btrfs_print_leaf(root, path->nodes[0]);
150 printk(KERN_CRIT "unable to update root key %llu %u %llu\n",
151 (unsigned long long)key->objectid, key->type,
152 (unsigned long long)key->offset);
153 BUG_ON(1);
156 l = path->nodes[0];
157 slot = path->slots[0];
158 ptr = btrfs_item_ptr_offset(l, slot);
159 old_len = btrfs_item_size_nr(l, slot);
162 * If this is the first time we update the root item which originated
163 * from an older kernel, we need to enlarge the item size to make room
164 * for the added fields.
166 if (old_len < sizeof(*item)) {
167 btrfs_release_path(path);
168 ret = btrfs_search_slot(trans, root, key, path,
169 -1, 1);
170 if (ret < 0) {
171 btrfs_abort_transaction(trans, root, ret);
172 goto out;
175 ret = btrfs_del_item(trans, root, path);
176 if (ret < 0) {
177 btrfs_abort_transaction(trans, root, ret);
178 goto out;
180 btrfs_release_path(path);
181 ret = btrfs_insert_empty_item(trans, root, path,
182 key, sizeof(*item));
183 if (ret < 0) {
184 btrfs_abort_transaction(trans, root, ret);
185 goto out;
187 l = path->nodes[0];
188 slot = path->slots[0];
189 ptr = btrfs_item_ptr_offset(l, slot);
193 * Update generation_v2 so at the next mount we know the new root
194 * fields are valid.
196 btrfs_set_root_generation_v2(item, btrfs_root_generation(item));
198 write_extent_buffer(l, item, ptr, sizeof(*item));
199 btrfs_mark_buffer_dirty(path->nodes[0]);
200 out:
201 btrfs_free_path(path);
202 return ret;
205 int btrfs_insert_root(struct btrfs_trans_handle *trans, struct btrfs_root *root,
206 struct btrfs_key *key, struct btrfs_root_item *item)
209 * Make sure generation v1 and v2 match. See update_root for details.
211 btrfs_set_root_generation_v2(item, btrfs_root_generation(item));
212 return btrfs_insert_item(trans, root, key, item, sizeof(*item));
216 * at mount time we want to find all the old transaction snapshots that were in
217 * the process of being deleted if we crashed. This is any root item with an
218 * offset lower than the latest root. They need to be queued for deletion to
219 * finish what was happening when we crashed.
221 int btrfs_find_dead_roots(struct btrfs_root *root, u64 objectid)
223 struct btrfs_root *dead_root;
224 struct btrfs_root_item *ri;
225 struct btrfs_key key;
226 struct btrfs_key found_key;
227 struct btrfs_path *path;
228 int ret;
229 u32 nritems;
230 struct extent_buffer *leaf;
231 int slot;
233 key.objectid = objectid;
234 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
235 key.offset = 0;
236 path = btrfs_alloc_path();
237 if (!path)
238 return -ENOMEM;
240 again:
241 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
242 if (ret < 0)
243 goto err;
244 while (1) {
245 leaf = path->nodes[0];
246 nritems = btrfs_header_nritems(leaf);
247 slot = path->slots[0];
248 if (slot >= nritems) {
249 ret = btrfs_next_leaf(root, path);
250 if (ret)
251 break;
252 leaf = path->nodes[0];
253 nritems = btrfs_header_nritems(leaf);
254 slot = path->slots[0];
256 btrfs_item_key_to_cpu(leaf, &key, slot);
257 if (btrfs_key_type(&key) != BTRFS_ROOT_ITEM_KEY)
258 goto next;
260 if (key.objectid < objectid)
261 goto next;
263 if (key.objectid > objectid)
264 break;
266 ri = btrfs_item_ptr(leaf, slot, struct btrfs_root_item);
267 if (btrfs_disk_root_refs(leaf, ri) != 0)
268 goto next;
270 memcpy(&found_key, &key, sizeof(key));
271 key.offset++;
272 btrfs_release_path(path);
273 dead_root =
274 btrfs_read_fs_root_no_radix(root->fs_info->tree_root,
275 &found_key);
276 if (IS_ERR(dead_root)) {
277 ret = PTR_ERR(dead_root);
278 goto err;
281 ret = btrfs_add_dead_root(dead_root);
282 if (ret)
283 goto err;
284 goto again;
285 next:
286 slot++;
287 path->slots[0]++;
289 ret = 0;
290 err:
291 btrfs_free_path(path);
292 return ret;
295 int btrfs_find_orphan_roots(struct btrfs_root *tree_root)
297 struct extent_buffer *leaf;
298 struct btrfs_path *path;
299 struct btrfs_key key;
300 struct btrfs_key root_key;
301 struct btrfs_root *root;
302 int err = 0;
303 int ret;
305 path = btrfs_alloc_path();
306 if (!path)
307 return -ENOMEM;
309 key.objectid = BTRFS_ORPHAN_OBJECTID;
310 key.type = BTRFS_ORPHAN_ITEM_KEY;
311 key.offset = 0;
313 root_key.type = BTRFS_ROOT_ITEM_KEY;
314 root_key.offset = (u64)-1;
316 while (1) {
317 ret = btrfs_search_slot(NULL, tree_root, &key, path, 0, 0);
318 if (ret < 0) {
319 err = ret;
320 break;
323 leaf = path->nodes[0];
324 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
325 ret = btrfs_next_leaf(tree_root, path);
326 if (ret < 0)
327 err = ret;
328 if (ret != 0)
329 break;
330 leaf = path->nodes[0];
333 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
334 btrfs_release_path(path);
336 if (key.objectid != BTRFS_ORPHAN_OBJECTID ||
337 key.type != BTRFS_ORPHAN_ITEM_KEY)
338 break;
340 root_key.objectid = key.offset;
341 key.offset++;
343 root = btrfs_read_fs_root_no_name(tree_root->fs_info,
344 &root_key);
345 if (!IS_ERR(root))
346 continue;
348 ret = PTR_ERR(root);
349 if (ret != -ENOENT) {
350 err = ret;
351 break;
354 ret = btrfs_find_dead_roots(tree_root, root_key.objectid);
355 if (ret) {
356 err = ret;
357 break;
361 btrfs_free_path(path);
362 return err;
365 /* drop the root item for 'key' from 'root' */
366 int btrfs_del_root(struct btrfs_trans_handle *trans, struct btrfs_root *root,
367 struct btrfs_key *key)
369 struct btrfs_path *path;
370 int ret;
371 struct btrfs_root_item *ri;
372 struct extent_buffer *leaf;
374 path = btrfs_alloc_path();
375 if (!path)
376 return -ENOMEM;
377 ret = btrfs_search_slot(trans, root, key, path, -1, 1);
378 if (ret < 0)
379 goto out;
381 BUG_ON(ret != 0);
382 leaf = path->nodes[0];
383 ri = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_item);
385 ret = btrfs_del_item(trans, root, path);
386 out:
387 btrfs_free_path(path);
388 return ret;
391 int btrfs_del_root_ref(struct btrfs_trans_handle *trans,
392 struct btrfs_root *tree_root,
393 u64 root_id, u64 ref_id, u64 dirid, u64 *sequence,
394 const char *name, int name_len)
397 struct btrfs_path *path;
398 struct btrfs_root_ref *ref;
399 struct extent_buffer *leaf;
400 struct btrfs_key key;
401 unsigned long ptr;
402 int err = 0;
403 int ret;
405 path = btrfs_alloc_path();
406 if (!path)
407 return -ENOMEM;
409 key.objectid = root_id;
410 key.type = BTRFS_ROOT_BACKREF_KEY;
411 key.offset = ref_id;
412 again:
413 ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1);
414 BUG_ON(ret < 0);
415 if (ret == 0) {
416 leaf = path->nodes[0];
417 ref = btrfs_item_ptr(leaf, path->slots[0],
418 struct btrfs_root_ref);
420 WARN_ON(btrfs_root_ref_dirid(leaf, ref) != dirid);
421 WARN_ON(btrfs_root_ref_name_len(leaf, ref) != name_len);
422 ptr = (unsigned long)(ref + 1);
423 WARN_ON(memcmp_extent_buffer(leaf, name, ptr, name_len));
424 *sequence = btrfs_root_ref_sequence(leaf, ref);
426 ret = btrfs_del_item(trans, tree_root, path);
427 if (ret) {
428 err = ret;
429 goto out;
431 } else
432 err = -ENOENT;
434 if (key.type == BTRFS_ROOT_BACKREF_KEY) {
435 btrfs_release_path(path);
436 key.objectid = ref_id;
437 key.type = BTRFS_ROOT_REF_KEY;
438 key.offset = root_id;
439 goto again;
442 out:
443 btrfs_free_path(path);
444 return err;
447 int btrfs_find_root_ref(struct btrfs_root *tree_root,
448 struct btrfs_path *path,
449 u64 root_id, u64 ref_id)
451 struct btrfs_key key;
452 int ret;
454 key.objectid = root_id;
455 key.type = BTRFS_ROOT_REF_KEY;
456 key.offset = ref_id;
458 ret = btrfs_search_slot(NULL, tree_root, &key, path, 0, 0);
459 return ret;
463 * add a btrfs_root_ref item. type is either BTRFS_ROOT_REF_KEY
464 * or BTRFS_ROOT_BACKREF_KEY.
466 * The dirid, sequence, name and name_len refer to the directory entry
467 * that is referencing the root.
469 * For a forward ref, the root_id is the id of the tree referencing
470 * the root and ref_id is the id of the subvol or snapshot.
472 * For a back ref the root_id is the id of the subvol or snapshot and
473 * ref_id is the id of the tree referencing it.
475 * Will return 0, -ENOMEM, or anything from the CoW path
477 int btrfs_add_root_ref(struct btrfs_trans_handle *trans,
478 struct btrfs_root *tree_root,
479 u64 root_id, u64 ref_id, u64 dirid, u64 sequence,
480 const char *name, int name_len)
482 struct btrfs_key key;
483 int ret;
484 struct btrfs_path *path;
485 struct btrfs_root_ref *ref;
486 struct extent_buffer *leaf;
487 unsigned long ptr;
489 path = btrfs_alloc_path();
490 if (!path)
491 return -ENOMEM;
493 key.objectid = root_id;
494 key.type = BTRFS_ROOT_BACKREF_KEY;
495 key.offset = ref_id;
496 again:
497 ret = btrfs_insert_empty_item(trans, tree_root, path, &key,
498 sizeof(*ref) + name_len);
499 if (ret) {
500 btrfs_abort_transaction(trans, tree_root, ret);
501 btrfs_free_path(path);
502 return ret;
505 leaf = path->nodes[0];
506 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
507 btrfs_set_root_ref_dirid(leaf, ref, dirid);
508 btrfs_set_root_ref_sequence(leaf, ref, sequence);
509 btrfs_set_root_ref_name_len(leaf, ref, name_len);
510 ptr = (unsigned long)(ref + 1);
511 write_extent_buffer(leaf, name, ptr, name_len);
512 btrfs_mark_buffer_dirty(leaf);
514 if (key.type == BTRFS_ROOT_BACKREF_KEY) {
515 btrfs_release_path(path);
516 key.objectid = ref_id;
517 key.type = BTRFS_ROOT_REF_KEY;
518 key.offset = root_id;
519 goto again;
522 btrfs_free_path(path);
523 return 0;
527 * Old btrfs forgets to init root_item->flags and root_item->byte_limit
528 * for subvolumes. To work around this problem, we steal a bit from
529 * root_item->inode_item->flags, and use it to indicate if those fields
530 * have been properly initialized.
532 void btrfs_check_and_init_root_item(struct btrfs_root_item *root_item)
534 u64 inode_flags = le64_to_cpu(root_item->inode.flags);
536 if (!(inode_flags & BTRFS_INODE_ROOT_ITEM_INIT)) {
537 inode_flags |= BTRFS_INODE_ROOT_ITEM_INIT;
538 root_item->inode.flags = cpu_to_le64(inode_flags);
539 root_item->flags = 0;
540 root_item->byte_limit = 0;
544 void btrfs_update_root_times(struct btrfs_trans_handle *trans,
545 struct btrfs_root *root)
547 struct btrfs_root_item *item = &root->root_item;
548 struct timespec ct = CURRENT_TIME;
550 spin_lock(&root->root_item_lock);
551 item->ctransid = cpu_to_le64(trans->transid);
552 item->ctime.sec = cpu_to_le64(ct.tv_sec);
553 item->ctime.nsec = cpu_to_le32(ct.tv_nsec);
554 spin_unlock(&root->root_item_lock);