blob: e3984f902e71decd181998ff0ec5d4de6c7984dd [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
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
Chris Mason3768f362007-03-13 16:47:54 -040019#include "ctree.h"
Chris Mason5eda7b52007-06-22 14:16:25 -040020#include "transaction.h"
Chris Mason3768f362007-03-13 16:47:54 -040021#include "disk-io.h"
22#include "print-tree.h"
23
Chris Masonbf4ef672008-05-08 13:26:18 -040024/*
25 * returns 0 on finding something, 1 if no more roots are there
26 * and < 0 on error
27 */
28int btrfs_search_root(struct btrfs_root *root, u64 search_start,
29 u64 *found_objectid)
30{
31 struct btrfs_path *path;
32 struct btrfs_key search_key;
33 int ret;
34
35 root = root->fs_info->tree_root;
36 search_key.objectid = search_start;
37 search_key.type = (u8)-1;
38 search_key.offset = (u64)-1;
39
40 path = btrfs_alloc_path();
41 BUG_ON(!path);
42again:
43 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
44 if (ret < 0)
45 goto out;
46 if (ret == 0) {
47 ret = 1;
48 goto out;
49 }
50 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
51 ret = btrfs_next_leaf(root, path);
52 if (ret)
53 goto out;
54 }
55 btrfs_item_key_to_cpu(path->nodes[0], &search_key, path->slots[0]);
56 if (search_key.type != BTRFS_ROOT_ITEM_KEY) {
57 search_key.offset++;
58 btrfs_release_path(root, path);
59 goto again;
60 }
61 ret = 0;
62 *found_objectid = search_key.objectid;
63
64out:
65 btrfs_free_path(path);
66 return ret;
67}
68
Chris Mason3768f362007-03-13 16:47:54 -040069int btrfs_find_last_root(struct btrfs_root *root, u64 objectid,
70 struct btrfs_root_item *item, struct btrfs_key *key)
71{
Chris Mason5caf2a02007-04-02 11:20:42 -040072 struct btrfs_path *path;
Chris Mason3768f362007-03-13 16:47:54 -040073 struct btrfs_key search_key;
Chris Mason5f39d392007-10-15 16:14:19 -040074 struct btrfs_key found_key;
75 struct extent_buffer *l;
Chris Mason3768f362007-03-13 16:47:54 -040076 int ret;
77 int slot;
78
79 search_key.objectid = objectid;
Chris Mason5f39d392007-10-15 16:14:19 -040080 search_key.type = (u8)-1;
Chris Mason5eda7b52007-06-22 14:16:25 -040081 search_key.offset = (u64)-1;
Chris Mason3768f362007-03-13 16:47:54 -040082
Chris Mason5caf2a02007-04-02 11:20:42 -040083 path = btrfs_alloc_path();
84 BUG_ON(!path);
Chris Mason5caf2a02007-04-02 11:20:42 -040085 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
Chris Mason3768f362007-03-13 16:47:54 -040086 if (ret < 0)
87 goto out;
Chris Mason5f39d392007-10-15 16:14:19 -040088
Chris Mason3768f362007-03-13 16:47:54 -040089 BUG_ON(ret == 0);
Chris Mason5f39d392007-10-15 16:14:19 -040090 l = path->nodes[0];
Chris Mason5caf2a02007-04-02 11:20:42 -040091 BUG_ON(path->slots[0] == 0);
92 slot = path->slots[0] - 1;
Chris Mason5f39d392007-10-15 16:14:19 -040093 btrfs_item_key_to_cpu(l, &found_key, slot);
94 if (found_key.objectid != objectid) {
Chris Mason3768f362007-03-13 16:47:54 -040095 ret = 1;
96 goto out;
97 }
Chris Mason5f39d392007-10-15 16:14:19 -040098 read_extent_buffer(l, item, btrfs_item_ptr_offset(l, slot),
99 sizeof(*item));
100 memcpy(key, &found_key, sizeof(found_key));
Chris Mason3768f362007-03-13 16:47:54 -0400101 ret = 0;
102out:
Chris Mason5caf2a02007-04-02 11:20:42 -0400103 btrfs_free_path(path);
Chris Mason3768f362007-03-13 16:47:54 -0400104 return ret;
105}
106
Chris Masone089f052007-03-16 16:20:31 -0400107int btrfs_update_root(struct btrfs_trans_handle *trans, struct btrfs_root
108 *root, struct btrfs_key *key, struct btrfs_root_item
109 *item)
Chris Mason3768f362007-03-13 16:47:54 -0400110{
Chris Mason5caf2a02007-04-02 11:20:42 -0400111 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400112 struct extent_buffer *l;
Chris Mason3768f362007-03-13 16:47:54 -0400113 int ret;
114 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -0400115 unsigned long ptr;
Chris Mason3768f362007-03-13 16:47:54 -0400116
Chris Mason5caf2a02007-04-02 11:20:42 -0400117 path = btrfs_alloc_path();
118 BUG_ON(!path);
Chris Mason5caf2a02007-04-02 11:20:42 -0400119 ret = btrfs_search_slot(trans, root, key, path, 0, 1);
Chris Mason3768f362007-03-13 16:47:54 -0400120 if (ret < 0)
121 goto out;
Chris Masond6667462008-01-03 14:51:00 -0500122
123 if (ret != 0) {
124 btrfs_print_leaf(root, path->nodes[0]);
125 printk("unable to update root key %Lu %u %Lu\n",
126 key->objectid, key->type, key->offset);
127 BUG_ON(1);
128 }
129
Chris Mason5f39d392007-10-15 16:14:19 -0400130 l = path->nodes[0];
Chris Mason5caf2a02007-04-02 11:20:42 -0400131 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -0400132 ptr = btrfs_item_ptr_offset(l, slot);
133 write_extent_buffer(l, item, ptr, sizeof(*item));
Chris Mason5caf2a02007-04-02 11:20:42 -0400134 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason3768f362007-03-13 16:47:54 -0400135out:
Chris Mason5caf2a02007-04-02 11:20:42 -0400136 btrfs_release_path(root, path);
137 btrfs_free_path(path);
Chris Mason3768f362007-03-13 16:47:54 -0400138 return ret;
139}
140
Chris Masone089f052007-03-16 16:20:31 -0400141int btrfs_insert_root(struct btrfs_trans_handle *trans, struct btrfs_root
142 *root, struct btrfs_key *key, struct btrfs_root_item
143 *item)
Chris Mason3768f362007-03-13 16:47:54 -0400144{
145 int ret;
Chris Masone089f052007-03-16 16:20:31 -0400146 ret = btrfs_insert_item(trans, root, key, item, sizeof(*item));
Chris Mason3768f362007-03-13 16:47:54 -0400147 return ret;
148}
149
Chris Mason5ce14bb2007-09-11 11:15:39 -0400150int btrfs_find_dead_roots(struct btrfs_root *root, u64 objectid,
151 struct btrfs_root *latest)
Chris Mason5eda7b52007-06-22 14:16:25 -0400152{
153 struct btrfs_root *dead_root;
154 struct btrfs_item *item;
155 struct btrfs_root_item *ri;
156 struct btrfs_key key;
Chris Masona7a16fd2008-06-26 10:34:20 -0400157 struct btrfs_key found_key;
Chris Mason5eda7b52007-06-22 14:16:25 -0400158 struct btrfs_path *path;
159 int ret;
160 u32 nritems;
Chris Mason5f39d392007-10-15 16:14:19 -0400161 struct extent_buffer *leaf;
Chris Mason5eda7b52007-06-22 14:16:25 -0400162 int slot;
163
Chris Mason5ce14bb2007-09-11 11:15:39 -0400164 key.objectid = objectid;
Chris Mason5eda7b52007-06-22 14:16:25 -0400165 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
166 key.offset = 0;
167 path = btrfs_alloc_path();
168 if (!path)
169 return -ENOMEM;
Chris Masona7a16fd2008-06-26 10:34:20 -0400170
171again:
Chris Mason5eda7b52007-06-22 14:16:25 -0400172 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
173 if (ret < 0)
174 goto err;
175 while(1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400176 leaf = path->nodes[0];
177 nritems = btrfs_header_nritems(leaf);
Chris Mason5eda7b52007-06-22 14:16:25 -0400178 slot = path->slots[0];
179 if (slot >= nritems) {
180 ret = btrfs_next_leaf(root, path);
181 if (ret)
182 break;
Chris Mason5f39d392007-10-15 16:14:19 -0400183 leaf = path->nodes[0];
184 nritems = btrfs_header_nritems(leaf);
Chris Mason5eda7b52007-06-22 14:16:25 -0400185 slot = path->slots[0];
186 }
Chris Mason5f39d392007-10-15 16:14:19 -0400187 item = btrfs_item_nr(leaf, slot);
188 btrfs_item_key_to_cpu(leaf, &key, slot);
Chris Mason5eda7b52007-06-22 14:16:25 -0400189 if (btrfs_key_type(&key) != BTRFS_ROOT_ITEM_KEY)
190 goto next;
Chris Mason5ce14bb2007-09-11 11:15:39 -0400191
192 if (key.objectid < objectid)
193 goto next;
194
195 if (key.objectid > objectid)
196 break;
197
Chris Mason5eda7b52007-06-22 14:16:25 -0400198 ri = btrfs_item_ptr(leaf, slot, struct btrfs_root_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400199 if (btrfs_disk_root_refs(leaf, ri) != 0)
Chris Mason5eda7b52007-06-22 14:16:25 -0400200 goto next;
Chris Mason5ce14bb2007-09-11 11:15:39 -0400201
Chris Masona7a16fd2008-06-26 10:34:20 -0400202 memcpy(&found_key, &key, sizeof(key));
203 key.offset++;
204 btrfs_release_path(root, path);
Chris Masone02119d2008-09-05 16:13:11 -0400205 dead_root =
206 btrfs_read_fs_root_no_radix(root->fs_info->tree_root,
207 &found_key);
Aneesha1f39632007-07-11 10:03:27 -0400208 if (IS_ERR(dead_root)) {
209 ret = PTR_ERR(dead_root);
Chris Mason5eda7b52007-06-22 14:16:25 -0400210 goto err;
211 }
Chris Mason5ce14bb2007-09-11 11:15:39 -0400212
Yan Zhengb48652c2008-08-04 23:23:47 -0400213 ret = btrfs_add_dead_root(dead_root, latest);
Chris Mason5eda7b52007-06-22 14:16:25 -0400214 if (ret)
215 goto err;
Chris Masona7a16fd2008-06-26 10:34:20 -0400216 goto again;
Chris Mason5eda7b52007-06-22 14:16:25 -0400217next:
218 slot++;
219 path->slots[0]++;
220 }
221 ret = 0;
222err:
223 btrfs_free_path(path);
224 return ret;
225}
226
Chris Masone089f052007-03-16 16:20:31 -0400227int btrfs_del_root(struct btrfs_trans_handle *trans, struct btrfs_root *root,
228 struct btrfs_key *key)
Chris Mason3768f362007-03-13 16:47:54 -0400229{
Chris Mason5caf2a02007-04-02 11:20:42 -0400230 struct btrfs_path *path;
Chris Mason3768f362007-03-13 16:47:54 -0400231 int ret;
Chris Masonc5739bb2007-04-10 09:27:04 -0400232 u32 refs;
233 struct btrfs_root_item *ri;
Chris Mason5f39d392007-10-15 16:14:19 -0400234 struct extent_buffer *leaf;
Chris Mason3768f362007-03-13 16:47:54 -0400235
Chris Mason5caf2a02007-04-02 11:20:42 -0400236 path = btrfs_alloc_path();
237 BUG_ON(!path);
Chris Mason5caf2a02007-04-02 11:20:42 -0400238 ret = btrfs_search_slot(trans, root, key, path, -1, 1);
Chris Mason3768f362007-03-13 16:47:54 -0400239 if (ret < 0)
240 goto out;
Chris Masonedbd8d42007-12-21 16:27:24 -0500241 if (ret) {
242btrfs_print_leaf(root, path->nodes[0]);
243printk("failed to del %Lu %u %Lu\n", key->objectid, key->type, key->offset);
244
245 }
Chris Mason3768f362007-03-13 16:47:54 -0400246 BUG_ON(ret != 0);
Chris Mason5f39d392007-10-15 16:14:19 -0400247 leaf = path->nodes[0];
248 ri = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_item);
Chris Masonc5739bb2007-04-10 09:27:04 -0400249
Chris Mason5f39d392007-10-15 16:14:19 -0400250 refs = btrfs_disk_root_refs(leaf, ri);
Chris Mason5eda7b52007-06-22 14:16:25 -0400251 BUG_ON(refs != 0);
252 ret = btrfs_del_item(trans, root, path);
Chris Mason3768f362007-03-13 16:47:54 -0400253out:
Chris Mason5caf2a02007-04-02 11:20:42 -0400254 btrfs_release_path(root, path);
255 btrfs_free_path(path);
Chris Mason3768f362007-03-13 16:47:54 -0400256 return ret;
257}