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.
20 #include "transaction.h"
22 #include "print-tree.h"
24 int btrfs_find_last_root(struct btrfs_root *root, u64 objectid,
25 struct btrfs_root_item *item, struct btrfs_key *key)
27 struct btrfs_path *path;
28 struct btrfs_key search_key;
29 struct btrfs_key found_key;
30 struct extent_buffer *l;
34 search_key.objectid = objectid;
35 search_key.type = BTRFS_ROOT_ITEM_KEY;
36 search_key.offset = (u64)-1;
38 path = btrfs_alloc_path();
40 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 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) {
53 read_extent_buffer(l, item, btrfs_item_ptr_offset(l, slot),
55 memcpy(key, &found_key, sizeof(found_key));
58 btrfs_release_path(root, path);
59 btrfs_free_path(path);
63 int btrfs_update_root(struct btrfs_trans_handle *trans, struct btrfs_root
64 *root, struct btrfs_key *key, struct btrfs_root_item
67 struct btrfs_path *path;
68 struct extent_buffer *l;
74 path = btrfs_alloc_path();
76 ret = btrfs_search_slot(trans, root, key, path, 0, 1);
81 slot = path->slots[0];
82 ptr = btrfs_item_ptr_offset(l, slot);
83 old_len = btrfs_item_size_nr(l, slot);
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.
90 if (old_len < sizeof(*item)) {
91 btrfs_release_path(root, path);
92 ret = btrfs_search_slot(trans, root, key, path,
98 ret = btrfs_del_item(trans, root, path);
102 btrfs_release_path(root, path);
103 ret = btrfs_insert_empty_item(trans, root, path,
109 slot = path->slots[0];
110 ptr = btrfs_item_ptr_offset(l, slot);
114 * Update generation_v2 so at the next mount we know the new root
117 btrfs_set_root_generation_v2(item, btrfs_root_generation(item));
119 write_extent_buffer(l, item, ptr, sizeof(*item));
120 btrfs_mark_buffer_dirty(path->nodes[0]);
122 btrfs_release_path(root, path);
123 btrfs_free_path(path);
127 int btrfs_insert_root(struct btrfs_trans_handle *trans, struct btrfs_root
128 *root, struct btrfs_key *key, struct btrfs_root_item
134 * Make sure generation v1 and v2 match. See update_root for details.
136 btrfs_set_root_generation_v2(item, btrfs_root_generation(item));
137 ret = btrfs_insert_item(trans, root, key, item, sizeof(*item));
141 int btrfs_del_root(struct btrfs_trans_handle *trans, struct btrfs_root *root,
142 struct btrfs_key *key)
144 struct btrfs_path *path;
147 struct btrfs_root_item *ri;
148 struct extent_buffer *leaf;
150 path = btrfs_alloc_path();
152 ret = btrfs_search_slot(trans, root, key, path, -1, 1);
156 leaf = path->nodes[0];
157 ri = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_item);
159 refs = btrfs_disk_root_refs(leaf, ri);
161 ret = btrfs_del_item(trans, root, path);
163 btrfs_release_path(root, path);
164 btrfs_free_path(path);
169 * add a btrfs_root_ref item. type is either BTRFS_ROOT_REF_KEY
170 * or BTRFS_ROOT_BACKREF_KEY.
172 * The dirid, sequence, name and name_len refer to the directory entry
173 * that is referencing the root.
175 * For a forward ref, the root_id is the id of the tree referencing
176 * the root and ref_id is the id of the subvol or snapshot.
178 * For a back ref the root_id is the id of the subvol or snapshot and
179 * ref_id is the id of the tree referencing it.
181 int btrfs_add_root_ref(struct btrfs_trans_handle *trans,
182 struct btrfs_root *tree_root,
183 u64 root_id, u8 type, u64 ref_id,
184 u64 dirid, u64 sequence,
185 const char *name, int name_len)
187 struct btrfs_key key;
189 struct btrfs_path *path;
190 struct btrfs_root_ref *ref;
191 struct extent_buffer *leaf;
195 path = btrfs_alloc_path();
197 key.objectid = root_id;
201 ret = btrfs_insert_empty_item(trans, tree_root, path, &key,
202 sizeof(*ref) + name_len);
205 leaf = path->nodes[0];
206 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
207 btrfs_set_root_ref_dirid(leaf, ref, dirid);
208 btrfs_set_root_ref_sequence(leaf, ref, sequence);
209 btrfs_set_root_ref_name_len(leaf, ref, name_len);
210 ptr = (unsigned long)(ref + 1);
211 write_extent_buffer(leaf, name, ptr, name_len);
212 btrfs_mark_buffer_dirty(leaf);
214 btrfs_free_path(path);