blob: 8b0eca042ea42cd808c4b9d5be79c85739f1b31a [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 Mason2e635a22007-03-21 11:12:56 -040019#include <linux/module.h>
Chris Mason3768f362007-03-13 16:47:54 -040020#include "ctree.h"
Chris Mason5eda7b52007-06-22 14:16:25 -040021#include "transaction.h"
Chris Mason3768f362007-03-13 16:47:54 -040022#include "disk-io.h"
23#include "print-tree.h"
24
25int btrfs_find_last_root(struct btrfs_root *root, u64 objectid,
26 struct btrfs_root_item *item, struct btrfs_key *key)
27{
Chris Mason5caf2a02007-04-02 11:20:42 -040028 struct btrfs_path *path;
Chris Mason3768f362007-03-13 16:47:54 -040029 struct btrfs_key search_key;
30 struct btrfs_leaf *l;
31 int ret;
32 int slot;
33
34 search_key.objectid = objectid;
35 search_key.flags = (u32)-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;
43 BUG_ON(ret == 0);
Chris Mason5caf2a02007-04-02 11:20:42 -040044 l = btrfs_buffer_leaf(path->nodes[0]);
45 BUG_ON(path->slots[0] == 0);
46 slot = path->slots[0] - 1;
Chris Mason62e27492007-03-15 12:56:47 -040047 if (btrfs_disk_key_objectid(&l->items[slot].key) != objectid) {
Chris Mason3768f362007-03-13 16:47:54 -040048 ret = 1;
49 goto out;
50 }
Chris Mason123abc82007-03-14 14:14:43 -040051 memcpy(item, btrfs_item_ptr(l, slot, struct btrfs_root_item),
Chris Mason3768f362007-03-13 16:47:54 -040052 sizeof(*item));
53 btrfs_disk_key_to_cpu(key, &l->items[slot].key);
Chris Mason3768f362007-03-13 16:47:54 -040054 ret = 0;
55out:
Chris Mason5caf2a02007-04-02 11:20:42 -040056 btrfs_release_path(root, path);
57 btrfs_free_path(path);
Chris Mason3768f362007-03-13 16:47:54 -040058 return ret;
59}
60
Chris Masone089f052007-03-16 16:20:31 -040061int btrfs_update_root(struct btrfs_trans_handle *trans, struct btrfs_root
62 *root, struct btrfs_key *key, struct btrfs_root_item
63 *item)
Chris Mason3768f362007-03-13 16:47:54 -040064{
Chris Mason5caf2a02007-04-02 11:20:42 -040065 struct btrfs_path *path;
Chris Mason3768f362007-03-13 16:47:54 -040066 struct btrfs_leaf *l;
67 int ret;
68 int slot;
Chris Masond6025572007-03-30 14:27:56 -040069 struct btrfs_root_item *update_item;
Chris Mason3768f362007-03-13 16:47:54 -040070
Chris Mason5caf2a02007-04-02 11:20:42 -040071 path = btrfs_alloc_path();
72 BUG_ON(!path);
Chris Mason5caf2a02007-04-02 11:20:42 -040073 ret = btrfs_search_slot(trans, root, key, path, 0, 1);
Chris Mason3768f362007-03-13 16:47:54 -040074 if (ret < 0)
75 goto out;
76 BUG_ON(ret != 0);
Chris Mason5caf2a02007-04-02 11:20:42 -040077 l = btrfs_buffer_leaf(path->nodes[0]);
78 slot = path->slots[0];
Chris Masond6025572007-03-30 14:27:56 -040079 update_item = btrfs_item_ptr(l, slot, struct btrfs_root_item);
80 btrfs_memcpy(root, l, update_item, item, sizeof(*item));
Chris Mason5caf2a02007-04-02 11:20:42 -040081 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason3768f362007-03-13 16:47:54 -040082out:
Chris Mason5caf2a02007-04-02 11:20:42 -040083 btrfs_release_path(root, path);
84 btrfs_free_path(path);
Chris Mason3768f362007-03-13 16:47:54 -040085 return ret;
86}
87
Chris Masone089f052007-03-16 16:20:31 -040088int btrfs_insert_root(struct btrfs_trans_handle *trans, struct btrfs_root
89 *root, struct btrfs_key *key, struct btrfs_root_item
90 *item)
Chris Mason3768f362007-03-13 16:47:54 -040091{
92 int ret;
Chris Masone089f052007-03-16 16:20:31 -040093 ret = btrfs_insert_item(trans, root, key, item, sizeof(*item));
Chris Mason3768f362007-03-13 16:47:54 -040094 return ret;
95}
96
Chris Mason5eda7b52007-06-22 14:16:25 -040097int btrfs_find_dead_roots(struct btrfs_root *root)
98{
99 struct btrfs_root *dead_root;
100 struct btrfs_item *item;
101 struct btrfs_root_item *ri;
102 struct btrfs_key key;
103 struct btrfs_path *path;
104 int ret;
105 u32 nritems;
106 struct btrfs_leaf *leaf;
107 int slot;
108
109 key.objectid = 0;
110 key.flags = 0;
111 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
112 key.offset = 0;
113 path = btrfs_alloc_path();
114 if (!path)
115 return -ENOMEM;
116 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
117 if (ret < 0)
118 goto err;
119 while(1) {
120 leaf = btrfs_buffer_leaf(path->nodes[0]);
121 nritems = btrfs_header_nritems(&leaf->header);
122 slot = path->slots[0];
123 if (slot >= nritems) {
124 ret = btrfs_next_leaf(root, path);
125 if (ret)
126 break;
127 leaf = btrfs_buffer_leaf(path->nodes[0]);
128 nritems = btrfs_header_nritems(&leaf->header);
129 slot = path->slots[0];
130 }
131 item = leaf->items + slot;
132 btrfs_disk_key_to_cpu(&key, &item->key);
133 if (btrfs_key_type(&key) != BTRFS_ROOT_ITEM_KEY)
134 goto next;
135 ri = btrfs_item_ptr(leaf, slot, struct btrfs_root_item);
136 if (btrfs_root_refs(ri) != 0)
137 goto next;
138 dead_root = btrfs_read_fs_root_no_radix(root->fs_info, &key);
139 if (IS_ERR(root)) {
140 ret = PTR_ERR(root);
141 goto err;
142 }
Chris Mason5eda7b52007-06-22 14:16:25 -0400143 ret = btrfs_add_dead_root(dead_root,
144 &root->fs_info->dead_roots);
145 if (ret)
146 goto err;
147next:
148 slot++;
149 path->slots[0]++;
150 }
151 ret = 0;
152err:
153 btrfs_free_path(path);
154 return ret;
155}
156
Chris Masone089f052007-03-16 16:20:31 -0400157int btrfs_del_root(struct btrfs_trans_handle *trans, struct btrfs_root *root,
158 struct btrfs_key *key)
Chris Mason3768f362007-03-13 16:47:54 -0400159{
Chris Mason5caf2a02007-04-02 11:20:42 -0400160 struct btrfs_path *path;
Chris Mason3768f362007-03-13 16:47:54 -0400161 int ret;
Chris Masonc5739bb2007-04-10 09:27:04 -0400162 u32 refs;
163 struct btrfs_root_item *ri;
Chris Mason3768f362007-03-13 16:47:54 -0400164
Chris Mason5caf2a02007-04-02 11:20:42 -0400165 path = btrfs_alloc_path();
166 BUG_ON(!path);
Chris Mason5caf2a02007-04-02 11:20:42 -0400167 ret = btrfs_search_slot(trans, root, key, path, -1, 1);
Chris Mason3768f362007-03-13 16:47:54 -0400168 if (ret < 0)
169 goto out;
170 BUG_ON(ret != 0);
Chris Masonc5739bb2007-04-10 09:27:04 -0400171 ri = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
172 path->slots[0], struct btrfs_root_item);
173
174 refs = btrfs_root_refs(ri);
Chris Mason5eda7b52007-06-22 14:16:25 -0400175 BUG_ON(refs != 0);
176 ret = btrfs_del_item(trans, root, path);
Chris Mason3768f362007-03-13 16:47:54 -0400177out:
Chris Mason5caf2a02007-04-02 11:20:42 -0400178 btrfs_release_path(root, path);
179 btrfs_free_path(path);
Chris Mason3768f362007-03-13 16:47:54 -0400180 return ret;
181}