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>
21 #include "transaction.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)
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))
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 "
57 memset(&item->generation_v2, 0,
58 sizeof(*item) - offsetof(struct btrfs_root_item,
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
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;
81 search_key.objectid = objectid;
82 search_key.type = BTRFS_ROOT_ITEM_KEY;
83 search_key.offset = (u64)-1;
85 path = btrfs_alloc_path();
88 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
93 if (path->slots[0] == 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) {
106 btrfs_read_root_item(l, slot, item);
108 memcpy(key, &found_key, sizeof(found_key));
112 btrfs_free_path(path);
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
131 struct btrfs_path *path;
132 struct extent_buffer *l;
138 path = btrfs_alloc_path();
142 ret = btrfs_search_slot(trans, root, key, path, 0, 1);
144 btrfs_abort_transaction(trans, root, ret);
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);
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,
171 btrfs_abort_transaction(trans, root, ret);
175 ret = btrfs_del_item(trans, root, path);
177 btrfs_abort_transaction(trans, root, ret);
180 btrfs_release_path(path);
181 ret = btrfs_insert_empty_item(trans, root, path,
184 btrfs_abort_transaction(trans, root, ret);
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
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]);
201 btrfs_free_path(path);
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;
230 struct extent_buffer *leaf;
233 key.objectid = objectid;
234 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
236 path = btrfs_alloc_path();
241 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
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);
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)
260 if (key.objectid < objectid)
263 if (key.objectid > objectid)
266 ri = btrfs_item_ptr(leaf, slot, struct btrfs_root_item);
267 if (btrfs_disk_root_refs(leaf, ri) != 0)
270 memcpy(&found_key, &key, sizeof(key));
272 btrfs_release_path(path);
274 btrfs_read_fs_root_no_radix(root->fs_info->tree_root,
276 if (IS_ERR(dead_root)) {
277 ret = PTR_ERR(dead_root);
281 ret = btrfs_add_dead_root(dead_root);
291 btrfs_free_path(path);
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;
305 path = btrfs_alloc_path();
309 key.objectid = BTRFS_ORPHAN_OBJECTID;
310 key.type = BTRFS_ORPHAN_ITEM_KEY;
313 root_key.type = BTRFS_ROOT_ITEM_KEY;
314 root_key.offset = (u64)-1;
317 ret = btrfs_search_slot(NULL, tree_root, &key, path, 0, 0);
323 leaf = path->nodes[0];
324 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
325 ret = btrfs_next_leaf(tree_root, path);
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)
340 root_key.objectid = key.offset;
343 root = btrfs_read_fs_root_no_name(tree_root->fs_info,
349 if (ret != -ENOENT) {
354 ret = btrfs_find_dead_roots(tree_root, root_key.objectid);
361 btrfs_free_path(path);
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;
371 struct btrfs_root_item *ri;
372 struct extent_buffer *leaf;
374 path = btrfs_alloc_path();
377 ret = btrfs_search_slot(trans, root, key, path, -1, 1);
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);
387 btrfs_free_path(path);
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;
405 path = btrfs_alloc_path();
409 key.objectid = root_id;
410 key.type = BTRFS_ROOT_BACKREF_KEY;
413 ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1);
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);
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;
443 btrfs_free_path(path);
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;
454 key.objectid = root_id;
455 key.type = BTRFS_ROOT_REF_KEY;
458 ret = btrfs_search_slot(NULL, tree_root, &key, path, 0, 0);
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;
484 struct btrfs_path *path;
485 struct btrfs_root_ref *ref;
486 struct extent_buffer *leaf;
489 path = btrfs_alloc_path();
493 key.objectid = root_id;
494 key.type = BTRFS_ROOT_BACKREF_KEY;
497 ret = btrfs_insert_empty_item(trans, tree_root, path, &key,
498 sizeof(*ref) + name_len);
500 btrfs_abort_transaction(trans, tree_root, ret);
501 btrfs_free_path(path);
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;
522 btrfs_free_path(path);
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);