blob: 88bcdd33f56e8e8ee0aef742f480a0e88bfc4841 [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
24int btrfs_find_last_root(struct btrfs_root *root, u64 objectid,
25 struct btrfs_root_item *item, struct btrfs_key *key)
26{
Chris Mason5caf2a02007-04-02 11:20:42 -040027 struct btrfs_path *path;
Chris Mason3768f362007-03-13 16:47:54 -040028 struct btrfs_key search_key;
Chris Mason5f39d392007-10-15 16:14:19 -040029 struct btrfs_key found_key;
30 struct extent_buffer *l;
Chris Mason3768f362007-03-13 16:47:54 -040031 int ret;
32 int slot;
33
34 search_key.objectid = objectid;
Chris Mason5f39d392007-10-15 16:14:19 -040035 search_key.type = (u8)-1;
Chris Mason5eda7b52007-06-22 14:16:25 -040036 search_key.offset = (u64)-1;
Chris Mason3768f362007-03-13 16:47:54 -040037
Chris Mason5caf2a02007-04-02 11:20:42 -040038 path = btrfs_alloc_path();
39 BUG_ON(!path);
Chris Mason5caf2a02007-04-02 11:20:42 -040040 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
Chris Mason3768f362007-03-13 16:47:54 -040041 if (ret < 0)
42 goto out;
Chris Mason5f39d392007-10-15 16:14:19 -040043
Chris Mason3768f362007-03-13 16:47:54 -040044 BUG_ON(ret == 0);
Chris Mason5f39d392007-10-15 16:14:19 -040045 l = path->nodes[0];
Chris Mason5caf2a02007-04-02 11:20:42 -040046 BUG_ON(path->slots[0] == 0);
47 slot = path->slots[0] - 1;
Chris Mason5f39d392007-10-15 16:14:19 -040048 btrfs_item_key_to_cpu(l, &found_key, slot);
49 if (found_key.objectid != objectid) {
Chris Mason3768f362007-03-13 16:47:54 -040050 ret = 1;
51 goto out;
52 }
Chris Mason5f39d392007-10-15 16:14:19 -040053 read_extent_buffer(l, item, btrfs_item_ptr_offset(l, slot),
54 sizeof(*item));
55 memcpy(key, &found_key, sizeof(found_key));
Chris Mason3768f362007-03-13 16:47:54 -040056 ret = 0;
57out:
Chris Mason5caf2a02007-04-02 11:20:42 -040058 btrfs_release_path(root, path);
59 btrfs_free_path(path);
Chris Mason3768f362007-03-13 16:47:54 -040060 return ret;
61}
62
Chris Masone089f052007-03-16 16:20:31 -040063int btrfs_update_root(struct btrfs_trans_handle *trans, struct btrfs_root
64 *root, struct btrfs_key *key, struct btrfs_root_item
65 *item)
Chris Mason3768f362007-03-13 16:47:54 -040066{
Chris Mason5caf2a02007-04-02 11:20:42 -040067 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -040068 struct extent_buffer *l;
Chris Mason3768f362007-03-13 16:47:54 -040069 int ret;
70 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -040071 unsigned long ptr;
Chris Mason3768f362007-03-13 16:47:54 -040072
Chris Mason5caf2a02007-04-02 11:20:42 -040073 path = btrfs_alloc_path();
74 BUG_ON(!path);
Chris Mason5caf2a02007-04-02 11:20:42 -040075 ret = btrfs_search_slot(trans, root, key, path, 0, 1);
Chris Mason3768f362007-03-13 16:47:54 -040076 if (ret < 0)
77 goto out;
78 BUG_ON(ret != 0);
Chris Mason5f39d392007-10-15 16:14:19 -040079 l = path->nodes[0];
Chris Mason5caf2a02007-04-02 11:20:42 -040080 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -040081 ptr = btrfs_item_ptr_offset(l, slot);
82 write_extent_buffer(l, item, ptr, sizeof(*item));
Chris Mason5caf2a02007-04-02 11:20:42 -040083 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason3768f362007-03-13 16:47:54 -040084out:
Chris Mason5caf2a02007-04-02 11:20:42 -040085 btrfs_release_path(root, path);
86 btrfs_free_path(path);
Chris Mason3768f362007-03-13 16:47:54 -040087 return ret;
88}
89
Chris Masone089f052007-03-16 16:20:31 -040090int btrfs_insert_root(struct btrfs_trans_handle *trans, struct btrfs_root
91 *root, struct btrfs_key *key, struct btrfs_root_item
92 *item)
Chris Mason3768f362007-03-13 16:47:54 -040093{
94 int ret;
Chris Masone089f052007-03-16 16:20:31 -040095 ret = btrfs_insert_item(trans, root, key, item, sizeof(*item));
Chris Mason3768f362007-03-13 16:47:54 -040096 return ret;
97}
98
Chris Mason5ce14bb2007-09-11 11:15:39 -040099int btrfs_find_dead_roots(struct btrfs_root *root, u64 objectid,
100 struct btrfs_root *latest)
Chris Mason5eda7b52007-06-22 14:16:25 -0400101{
102 struct btrfs_root *dead_root;
103 struct btrfs_item *item;
104 struct btrfs_root_item *ri;
105 struct btrfs_key key;
106 struct btrfs_path *path;
107 int ret;
108 u32 nritems;
Chris Mason5f39d392007-10-15 16:14:19 -0400109 struct extent_buffer *leaf;
Chris Mason5eda7b52007-06-22 14:16:25 -0400110 int slot;
111
Chris Mason5ce14bb2007-09-11 11:15:39 -0400112 key.objectid = objectid;
Chris Mason5eda7b52007-06-22 14:16:25 -0400113 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
114 key.offset = 0;
115 path = btrfs_alloc_path();
116 if (!path)
117 return -ENOMEM;
118 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
119 if (ret < 0)
120 goto err;
121 while(1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400122 leaf = path->nodes[0];
123 nritems = btrfs_header_nritems(leaf);
Chris Mason5eda7b52007-06-22 14:16:25 -0400124 slot = path->slots[0];
125 if (slot >= nritems) {
126 ret = btrfs_next_leaf(root, path);
127 if (ret)
128 break;
Chris Mason5f39d392007-10-15 16:14:19 -0400129 leaf = path->nodes[0];
130 nritems = btrfs_header_nritems(leaf);
Chris Mason5eda7b52007-06-22 14:16:25 -0400131 slot = path->slots[0];
132 }
Chris Mason5f39d392007-10-15 16:14:19 -0400133 item = btrfs_item_nr(leaf, slot);
134 btrfs_item_key_to_cpu(leaf, &key, slot);
Chris Mason5eda7b52007-06-22 14:16:25 -0400135 if (btrfs_key_type(&key) != BTRFS_ROOT_ITEM_KEY)
136 goto next;
Chris Mason5ce14bb2007-09-11 11:15:39 -0400137
138 if (key.objectid < objectid)
139 goto next;
140
141 if (key.objectid > objectid)
142 break;
143
Chris Mason5eda7b52007-06-22 14:16:25 -0400144 ri = btrfs_item_ptr(leaf, slot, struct btrfs_root_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400145 if (btrfs_disk_root_refs(leaf, ri) != 0)
Chris Mason5eda7b52007-06-22 14:16:25 -0400146 goto next;
Chris Mason5ce14bb2007-09-11 11:15:39 -0400147
Chris Mason5eda7b52007-06-22 14:16:25 -0400148 dead_root = btrfs_read_fs_root_no_radix(root->fs_info, &key);
Aneesha1f39632007-07-11 10:03:27 -0400149 if (IS_ERR(dead_root)) {
150 ret = PTR_ERR(dead_root);
Chris Mason5eda7b52007-06-22 14:16:25 -0400151 goto err;
152 }
Chris Mason5ce14bb2007-09-11 11:15:39 -0400153
154 ret = btrfs_add_dead_root(dead_root, latest,
Chris Mason5eda7b52007-06-22 14:16:25 -0400155 &root->fs_info->dead_roots);
156 if (ret)
157 goto err;
158next:
159 slot++;
160 path->slots[0]++;
161 }
162 ret = 0;
163err:
164 btrfs_free_path(path);
165 return ret;
166}
167
Chris Masone089f052007-03-16 16:20:31 -0400168int btrfs_del_root(struct btrfs_trans_handle *trans, struct btrfs_root *root,
169 struct btrfs_key *key)
Chris Mason3768f362007-03-13 16:47:54 -0400170{
Chris Mason5caf2a02007-04-02 11:20:42 -0400171 struct btrfs_path *path;
Chris Mason3768f362007-03-13 16:47:54 -0400172 int ret;
Chris Masonc5739bb2007-04-10 09:27:04 -0400173 u32 refs;
174 struct btrfs_root_item *ri;
Chris Mason5f39d392007-10-15 16:14:19 -0400175 struct extent_buffer *leaf;
Chris Mason3768f362007-03-13 16:47:54 -0400176
Chris Mason5caf2a02007-04-02 11:20:42 -0400177 path = btrfs_alloc_path();
178 BUG_ON(!path);
Chris Mason5caf2a02007-04-02 11:20:42 -0400179 ret = btrfs_search_slot(trans, root, key, path, -1, 1);
Chris Mason3768f362007-03-13 16:47:54 -0400180 if (ret < 0)
181 goto out;
182 BUG_ON(ret != 0);
Chris Mason5f39d392007-10-15 16:14:19 -0400183 leaf = path->nodes[0];
184 ri = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_item);
Chris Masonc5739bb2007-04-10 09:27:04 -0400185
Chris Mason5f39d392007-10-15 16:14:19 -0400186 refs = btrfs_disk_root_refs(leaf, ri);
Chris Mason5eda7b52007-06-22 14:16:25 -0400187 BUG_ON(refs != 0);
188 ret = btrfs_del_item(trans, root, path);
Chris Mason3768f362007-03-13 16:47:54 -0400189out:
Chris Mason5caf2a02007-04-02 11:20:42 -0400190 btrfs_release_path(root, path);
191 btrfs_free_path(path);
Chris Mason3768f362007-03-13 16:47:54 -0400192 return ret;
193}