blob: 08f19ec8809281e556ac5f4dbc6024478418d707 [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;
Chris Masond6667462008-01-03 14:51:00 -050078
79 if (ret != 0) {
80 btrfs_print_leaf(root, path->nodes[0]);
81 printk("unable to update root key %Lu %u %Lu\n",
82 key->objectid, key->type, key->offset);
83 BUG_ON(1);
84 }
85
Chris Mason5f39d392007-10-15 16:14:19 -040086 l = path->nodes[0];
Chris Mason5caf2a02007-04-02 11:20:42 -040087 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -040088 ptr = btrfs_item_ptr_offset(l, slot);
89 write_extent_buffer(l, item, ptr, sizeof(*item));
Chris Mason5caf2a02007-04-02 11:20:42 -040090 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason3768f362007-03-13 16:47:54 -040091out:
Chris Mason5caf2a02007-04-02 11:20:42 -040092 btrfs_release_path(root, path);
93 btrfs_free_path(path);
Chris Mason3768f362007-03-13 16:47:54 -040094 return ret;
95}
96
Chris Masone089f052007-03-16 16:20:31 -040097int btrfs_insert_root(struct btrfs_trans_handle *trans, struct btrfs_root
98 *root, struct btrfs_key *key, struct btrfs_root_item
99 *item)
Chris Mason3768f362007-03-13 16:47:54 -0400100{
101 int ret;
Chris Masone089f052007-03-16 16:20:31 -0400102 ret = btrfs_insert_item(trans, root, key, item, sizeof(*item));
Chris Mason3768f362007-03-13 16:47:54 -0400103 return ret;
104}
105
Chris Mason5ce14bb2007-09-11 11:15:39 -0400106int btrfs_find_dead_roots(struct btrfs_root *root, u64 objectid,
107 struct btrfs_root *latest)
Chris Mason5eda7b52007-06-22 14:16:25 -0400108{
109 struct btrfs_root *dead_root;
110 struct btrfs_item *item;
111 struct btrfs_root_item *ri;
112 struct btrfs_key key;
113 struct btrfs_path *path;
114 int ret;
115 u32 nritems;
Chris Mason5f39d392007-10-15 16:14:19 -0400116 struct extent_buffer *leaf;
Chris Mason5eda7b52007-06-22 14:16:25 -0400117 int slot;
118
Chris Mason5ce14bb2007-09-11 11:15:39 -0400119 key.objectid = objectid;
Chris Mason5eda7b52007-06-22 14:16:25 -0400120 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
121 key.offset = 0;
122 path = btrfs_alloc_path();
123 if (!path)
124 return -ENOMEM;
125 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
126 if (ret < 0)
127 goto err;
128 while(1) {
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 if (slot >= nritems) {
133 ret = btrfs_next_leaf(root, path);
134 if (ret)
135 break;
Chris Mason5f39d392007-10-15 16:14:19 -0400136 leaf = path->nodes[0];
137 nritems = btrfs_header_nritems(leaf);
Chris Mason5eda7b52007-06-22 14:16:25 -0400138 slot = path->slots[0];
139 }
Chris Mason5f39d392007-10-15 16:14:19 -0400140 item = btrfs_item_nr(leaf, slot);
141 btrfs_item_key_to_cpu(leaf, &key, slot);
Chris Mason5eda7b52007-06-22 14:16:25 -0400142 if (btrfs_key_type(&key) != BTRFS_ROOT_ITEM_KEY)
143 goto next;
Chris Mason5ce14bb2007-09-11 11:15:39 -0400144
145 if (key.objectid < objectid)
146 goto next;
147
148 if (key.objectid > objectid)
149 break;
150
Chris Mason5eda7b52007-06-22 14:16:25 -0400151 ri = btrfs_item_ptr(leaf, slot, struct btrfs_root_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400152 if (btrfs_disk_root_refs(leaf, ri) != 0)
Chris Mason5eda7b52007-06-22 14:16:25 -0400153 goto next;
Chris Mason5ce14bb2007-09-11 11:15:39 -0400154
Chris Mason5eda7b52007-06-22 14:16:25 -0400155 dead_root = btrfs_read_fs_root_no_radix(root->fs_info, &key);
Aneesha1f39632007-07-11 10:03:27 -0400156 if (IS_ERR(dead_root)) {
157 ret = PTR_ERR(dead_root);
Chris Mason5eda7b52007-06-22 14:16:25 -0400158 goto err;
159 }
Chris Mason5ce14bb2007-09-11 11:15:39 -0400160
161 ret = btrfs_add_dead_root(dead_root, latest,
Chris Mason5eda7b52007-06-22 14:16:25 -0400162 &root->fs_info->dead_roots);
163 if (ret)
164 goto err;
165next:
166 slot++;
167 path->slots[0]++;
168 }
169 ret = 0;
170err:
171 btrfs_free_path(path);
172 return ret;
173}
174
Chris Masone089f052007-03-16 16:20:31 -0400175int btrfs_del_root(struct btrfs_trans_handle *trans, struct btrfs_root *root,
176 struct btrfs_key *key)
Chris Mason3768f362007-03-13 16:47:54 -0400177{
Chris Mason5caf2a02007-04-02 11:20:42 -0400178 struct btrfs_path *path;
Chris Mason3768f362007-03-13 16:47:54 -0400179 int ret;
Chris Masonc5739bb2007-04-10 09:27:04 -0400180 u32 refs;
181 struct btrfs_root_item *ri;
Chris Mason5f39d392007-10-15 16:14:19 -0400182 struct extent_buffer *leaf;
Chris Mason3768f362007-03-13 16:47:54 -0400183
Chris Mason5caf2a02007-04-02 11:20:42 -0400184 path = btrfs_alloc_path();
185 BUG_ON(!path);
Chris Mason5caf2a02007-04-02 11:20:42 -0400186 ret = btrfs_search_slot(trans, root, key, path, -1, 1);
Chris Mason3768f362007-03-13 16:47:54 -0400187 if (ret < 0)
188 goto out;
Chris Masonedbd8d42007-12-21 16:27:24 -0500189 if (ret) {
190btrfs_print_leaf(root, path->nodes[0]);
191printk("failed to del %Lu %u %Lu\n", key->objectid, key->type, key->offset);
192
193 }
Chris Mason3768f362007-03-13 16:47:54 -0400194 BUG_ON(ret != 0);
Chris Mason5f39d392007-10-15 16:14:19 -0400195 leaf = path->nodes[0];
196 ri = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_item);
Chris Masonc5739bb2007-04-10 09:27:04 -0400197
Chris Mason5f39d392007-10-15 16:14:19 -0400198 refs = btrfs_disk_root_refs(leaf, ri);
Chris Mason5eda7b52007-06-22 14:16:25 -0400199 BUG_ON(refs != 0);
200 ret = btrfs_del_item(trans, root, path);
Chris Mason3768f362007-03-13 16:47:54 -0400201out:
Chris Mason5caf2a02007-04-02 11:20:42 -0400202 btrfs_release_path(root, path);
203 btrfs_free_path(path);
Chris Mason3768f362007-03-13 16:47:54 -0400204 return ret;
205}