Fix man page headers to include the correct program name.
[btrfs-progs-unstable.git] / root-tree.c
blob782472ca937ebd20b9e8da6c0fa0262dd3c5ff94
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 "ctree.h"
20 #include "transaction.h"
21 #include "disk-io.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;
31 int ret;
32 int slot;
34 search_key.objectid = objectid;
35 search_key.type = BTRFS_ROOT_ITEM_KEY;
36 search_key.offset = (u64)-1;
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;
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 = 1;
51 goto out;
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;
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)
67 struct btrfs_path *path;
68 struct extent_buffer *l;
69 int ret;
70 int slot;
71 unsigned long ptr;
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;
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)
94 int ret;
95 ret = btrfs_insert_item(trans, root, key, item, sizeof(*item));
96 return ret;
99 #if 0
100 int btrfs_find_dead_roots(struct btrfs_root *root, u64 objectid,
101 struct btrfs_root *latest)
103 struct btrfs_root *dead_root;
104 struct btrfs_item *item;
105 struct btrfs_root_item *ri;
106 struct btrfs_key key;
107 struct btrfs_path *path;
108 int ret;
109 u32 nritems;
110 struct extent_buffer *leaf;
111 int slot;
113 key.objectid = objectid;
114 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
115 key.offset = 0;
116 path = btrfs_alloc_path();
117 if (!path)
118 return -ENOMEM;
119 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
120 if (ret < 0)
121 goto err;
122 while(1) {
123 leaf = path->nodes[0];
124 nritems = btrfs_header_nritems(leaf);
125 slot = path->slots[0];
126 if (slot >= nritems) {
127 ret = btrfs_next_leaf(root, path);
128 if (ret)
129 break;
130 leaf = path->nodes[0];
131 nritems = btrfs_header_nritems(leaf);
132 slot = path->slots[0];
134 item = btrfs_item_nr(leaf, slot);
135 btrfs_item_key_to_cpu(leaf, &key, slot);
136 if (btrfs_key_type(&key) != BTRFS_ROOT_ITEM_KEY)
137 goto next;
139 if (key.objectid < objectid)
140 goto next;
142 if (key.objectid > objectid)
143 break;
145 ri = btrfs_item_ptr(leaf, slot, struct btrfs_root_item);
146 if (btrfs_disk_root_refs(leaf, ri) != 0)
147 goto next;
149 dead_root = btrfs_read_fs_root_no_radix(root->fs_info, &key);
150 if (IS_ERR(dead_root)) {
151 ret = PTR_ERR(dead_root);
152 goto err;
155 ret = btrfs_add_dead_root(dead_root, latest,
156 &root->fs_info->dead_roots);
157 if (ret)
158 goto err;
159 next:
160 slot++;
161 path->slots[0]++;
163 ret = 0;
164 err:
165 btrfs_free_path(path);
166 return ret;
168 #endif
170 int btrfs_del_root(struct btrfs_trans_handle *trans, struct btrfs_root *root,
171 struct btrfs_key *key)
173 struct btrfs_path *path;
174 int ret;
175 u32 refs;
176 struct btrfs_root_item *ri;
177 struct extent_buffer *leaf;
179 path = btrfs_alloc_path();
180 BUG_ON(!path);
181 ret = btrfs_search_slot(trans, root, key, path, -1, 1);
182 if (ret < 0)
183 goto out;
184 if (ret) {
185 btrfs_print_leaf(root, path->nodes[0]);
186 printk("failed to del %llu %u %llu\n",
187 (unsigned long long)key->objectid,
188 key->type,
189 (unsigned long long)key->offset);
192 BUG_ON(ret != 0);
193 leaf = path->nodes[0];
194 ri = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_item);
196 refs = btrfs_disk_root_refs(leaf, ri);
197 BUG_ON(refs != 0);
198 ret = btrfs_del_item(trans, root, path);
199 out:
200 btrfs_release_path(root, path);
201 btrfs_free_path(path);
202 return ret;
206 * add a btrfs_root_ref item. type is either BTRFS_ROOT_REF_KEY
207 * or BTRFS_ROOT_BACKREF_KEY.
209 * The dirid, sequence, name and name_len refer to the directory entry
210 * that is referencing the root.
212 * For a forward ref, the root_id is the id of the tree referencing
213 * the root and ref_id is the id of the subvol or snapshot.
215 * For a back ref the root_id is the id of the subvol or snapshot and
216 * ref_id is the id of the tree referencing it.
218 int btrfs_add_root_ref(struct btrfs_trans_handle *trans,
219 struct btrfs_root *tree_root,
220 u64 root_id, u8 type, u64 ref_id,
221 u64 dirid, u64 sequence,
222 const char *name, int name_len)
224 struct btrfs_key key;
225 int ret;
226 struct btrfs_path *path;
227 struct btrfs_root_ref *ref;
228 struct extent_buffer *leaf;
229 unsigned long ptr;
232 path = btrfs_alloc_path();
234 key.objectid = root_id;
235 key.type = type;
236 key.offset = ref_id;
238 ret = btrfs_insert_empty_item(trans, tree_root, path, &key,
239 sizeof(*ref) + name_len);
240 BUG_ON(ret);
242 leaf = path->nodes[0];
243 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
244 btrfs_set_root_ref_dirid(leaf, ref, dirid);
245 btrfs_set_root_ref_sequence(leaf, ref, sequence);
246 btrfs_set_root_ref_name_len(leaf, ref, name_len);
247 ptr = (unsigned long)(ref + 1);
248 write_extent_buffer(leaf, name, ptr, name_len);
249 btrfs_mark_buffer_dirty(leaf);
251 btrfs_free_path(path);
252 return ret;