blob: 06b969c14625a4abba280ea318d5f181ee4c20ed [file] [log] [blame]
Chris Masone20d96d2007-03-22 12:13:20 -04001#include <linux/module.h>
2#include <linux/fs.h>
Chris Masond98237b2007-03-28 13:57:48 -04003#include <linux/blkdev.h>
Chris Mason87cbda52007-03-28 19:44:27 -04004#include <linux/crypto.h>
5#include <linux/scatterlist.h>
Chris Mason22b0ebd2007-03-30 08:47:31 -04006#include <linux/swap.h>
Chris Mason0f7d52f2007-04-09 10:42:37 -04007#include <linux/radix-tree.h>
Chris Masoneb60cea2007-02-02 09:18:22 -05008#include "ctree.h"
9#include "disk-io.h"
Chris Masone089f052007-03-16 16:20:31 -040010#include "transaction.h"
Chris Mason0f7d52f2007-04-09 10:42:37 -040011#include "btrfs_inode.h"
Chris Masoneb60cea2007-02-02 09:18:22 -050012
Chris Mason7eccb902007-04-11 15:53:25 -040013struct dev_lookup {
14 u64 block_start;
15 u64 num_blocks;
Chris Masonb4100d62007-04-12 12:14:00 -040016 u64 device_id;
Chris Mason7eccb902007-04-11 15:53:25 -040017 struct block_device *bdev;
18};
19
Chris Mason8352d8a2007-04-12 10:43:05 -040020int btrfs_insert_dev_radix(struct btrfs_root *root,
21 struct block_device *bdev,
Chris Masonb4100d62007-04-12 12:14:00 -040022 u64 device_id,
Chris Mason8352d8a2007-04-12 10:43:05 -040023 u64 block_start,
24 u64 num_blocks)
25{
26 struct dev_lookup *lookup;
27 char b[BDEVNAME_SIZE];
28 int ret;
29
30 lookup = kmalloc(sizeof(*lookup), GFP_NOFS);
31 if (!lookup)
32 return -ENOMEM;
33 lookup->block_start = block_start;
34 lookup->num_blocks = num_blocks;
35 lookup->bdev = bdev;
Chris Masonb4100d62007-04-12 12:14:00 -040036 lookup->device_id = device_id;
Chris Mason8352d8a2007-04-12 10:43:05 -040037printk("inserting %s into dev radix %Lu %Lu\n", bdevname(bdev, b), block_start, num_blocks);
38
39 ret = radix_tree_insert(&root->fs_info->dev_radix, block_start +
40 num_blocks - 1, lookup);
41 return ret;
42}
43
Chris Mason7eccb902007-04-11 15:53:25 -040044u64 bh_blocknr(struct buffer_head *bh)
45{
46 int blkbits = bh->b_page->mapping->host->i_blkbits;
47 u64 blocknr = bh->b_page->index << (PAGE_CACHE_SHIFT - blkbits);
48 unsigned long offset;
49
50 if (PageHighMem(bh->b_page))
51 offset = (unsigned long)bh->b_data;
52 else
53 offset = bh->b_data - (char *)page_address(bh->b_page);
54 blocknr += offset >> (PAGE_CACHE_SHIFT - blkbits);
55 return blocknr;
56}
57
Chris Masone20d96d2007-03-22 12:13:20 -040058static int check_tree_block(struct btrfs_root *root, struct buffer_head *buf)
Chris Masoneb60cea2007-02-02 09:18:22 -050059{
Chris Masone20d96d2007-03-22 12:13:20 -040060 struct btrfs_node *node = btrfs_buffer_node(buf);
Chris Mason7eccb902007-04-11 15:53:25 -040061 if (bh_blocknr(buf) != btrfs_header_blocknr(&node->header)) {
Chris Mason8352d8a2007-04-12 10:43:05 -040062 printk(KERN_CRIT "bh_blocknr(buf) is %Lu, header is %Lu\n",
63 bh_blocknr(buf), btrfs_header_blocknr(&node->header));
Chris Mason9a8dd152007-02-23 08:38:36 -050064 BUG();
Chris Masond98237b2007-03-28 13:57:48 -040065 }
Chris Mason9a8dd152007-02-23 08:38:36 -050066 return 0;
Chris Masoneb60cea2007-02-02 09:18:22 -050067}
68
Chris Masond98237b2007-03-28 13:57:48 -040069struct buffer_head *btrfs_find_tree_block(struct btrfs_root *root, u64 blocknr)
Chris Masoned2ff2c2007-03-01 18:59:40 -050070{
Chris Masond98237b2007-03-28 13:57:48 -040071 struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
72 int blockbits = root->fs_info->sb->s_blocksize_bits;
73 unsigned long index = blocknr >> (PAGE_CACHE_SHIFT - blockbits);
74 struct page *page;
75 struct buffer_head *bh;
76 struct buffer_head *head;
77 struct buffer_head *ret = NULL;
78
Chris Mason2c90e5d2007-04-02 10:50:19 -040079
Chris Masond98237b2007-03-28 13:57:48 -040080 page = find_lock_page(mapping, index);
81 if (!page)
82 return NULL;
83
84 if (!page_has_buffers(page))
85 goto out_unlock;
86
87 head = page_buffers(page);
88 bh = head;
89 do {
Chris Mason7eccb902007-04-11 15:53:25 -040090 if (buffer_mapped(bh) && bh_blocknr(bh) == blocknr) {
Chris Masond98237b2007-03-28 13:57:48 -040091 ret = bh;
92 get_bh(bh);
93 goto out_unlock;
94 }
95 bh = bh->b_this_page;
96 } while (bh != head);
97out_unlock:
98 unlock_page(page);
Chris Masond6025572007-03-30 14:27:56 -040099 if (ret) {
Chris Mason22b0ebd2007-03-30 08:47:31 -0400100 touch_buffer(ret);
Chris Masond6025572007-03-30 14:27:56 -0400101 }
Chris Masond98237b2007-03-28 13:57:48 -0400102 page_cache_release(page);
103 return ret;
Chris Masoned2ff2c2007-03-01 18:59:40 -0500104}
105
Chris Mason8352d8a2007-04-12 10:43:05 -0400106int btrfs_map_bh_to_logical(struct btrfs_root *root, struct buffer_head *bh,
Chris Mason7eccb902007-04-11 15:53:25 -0400107 u64 logical)
108{
109 struct dev_lookup *lookup[2];
Chris Mason7eccb902007-04-11 15:53:25 -0400110
111 int ret;
112
113 root = root->fs_info->dev_root;
114 ret = radix_tree_gang_lookup(&root->fs_info->dev_radix,
115 (void **)lookup,
116 (unsigned long)logical,
117 ARRAY_SIZE(lookup));
118 if (ret == 0 || lookup[0]->block_start > logical ||
119 lookup[0]->block_start + lookup[0]->num_blocks <= logical) {
120 ret = -ENOENT;
121 goto out;
122 }
123 bh->b_bdev = lookup[0]->bdev;
124 bh->b_blocknr = logical - lookup[0]->block_start;
Chris Mason7eccb902007-04-11 15:53:25 -0400125 set_buffer_mapped(bh);
126 ret = 0;
127out:
128 return ret;
129}
130
Chris Masond98237b2007-03-28 13:57:48 -0400131struct buffer_head *btrfs_find_create_tree_block(struct btrfs_root *root,
132 u64 blocknr)
Chris Masoneb60cea2007-02-02 09:18:22 -0500133{
Chris Masond98237b2007-03-28 13:57:48 -0400134 struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
135 int blockbits = root->fs_info->sb->s_blocksize_bits;
136 unsigned long index = blocknr >> (PAGE_CACHE_SHIFT - blockbits);
137 struct page *page;
138 struct buffer_head *bh;
139 struct buffer_head *head;
140 struct buffer_head *ret = NULL;
Chris Mason7eccb902007-04-11 15:53:25 -0400141 int err;
Chris Masond98237b2007-03-28 13:57:48 -0400142 u64 first_block = index << (PAGE_CACHE_SHIFT - blockbits);
Chris Mason22b0ebd2007-03-30 08:47:31 -0400143
Chris Masond98237b2007-03-28 13:57:48 -0400144 page = grab_cache_page(mapping, index);
145 if (!page)
146 return NULL;
147
Chris Masond98237b2007-03-28 13:57:48 -0400148 if (!page_has_buffers(page))
149 create_empty_buffers(page, root->fs_info->sb->s_blocksize, 0);
150 head = page_buffers(page);
151 bh = head;
152 do {
153 if (!buffer_mapped(bh)) {
Chris Mason8352d8a2007-04-12 10:43:05 -0400154 err = btrfs_map_bh_to_logical(root, bh, first_block);
Chris Mason7eccb902007-04-11 15:53:25 -0400155 BUG_ON(err);
Chris Masond98237b2007-03-28 13:57:48 -0400156 }
Chris Mason7eccb902007-04-11 15:53:25 -0400157 if (bh_blocknr(bh) == blocknr) {
Chris Masond98237b2007-03-28 13:57:48 -0400158 ret = bh;
159 get_bh(bh);
160 goto out_unlock;
161 }
162 bh = bh->b_this_page;
163 first_block++;
164 } while (bh != head);
165out_unlock:
166 unlock_page(page);
Chris Mason22b0ebd2007-03-30 08:47:31 -0400167 if (ret)
168 touch_buffer(ret);
Chris Masond98237b2007-03-28 13:57:48 -0400169 page_cache_release(page);
170 return ret;
Chris Masone20d96d2007-03-22 12:13:20 -0400171}
Chris Mason123abc82007-03-14 14:14:43 -0400172
Chris Masond98237b2007-03-28 13:57:48 -0400173static int btree_get_block(struct inode *inode, sector_t iblock,
174 struct buffer_head *bh, int create)
175{
Chris Mason7eccb902007-04-11 15:53:25 -0400176 int err;
177 struct btrfs_root *root = BTRFS_I(bh->b_page->mapping->host)->root;
Chris Mason8352d8a2007-04-12 10:43:05 -0400178 err = btrfs_map_bh_to_logical(root, bh, iblock);
Chris Mason7eccb902007-04-11 15:53:25 -0400179 return err;
Chris Masond98237b2007-03-28 13:57:48 -0400180}
181
Chris Masonf254e522007-03-29 15:15:27 -0400182int btrfs_csum_data(struct btrfs_root * root, char *data, size_t len,
183 char *result)
Chris Mason87cbda52007-03-28 19:44:27 -0400184{
Chris Mason87cbda52007-03-28 19:44:27 -0400185 struct scatterlist sg;
186 struct crypto_hash *tfm = root->fs_info->hash_tfm;
187 struct hash_desc desc;
188 int ret;
Chris Mason87cbda52007-03-28 19:44:27 -0400189
190 desc.tfm = tfm;
191 desc.flags = 0;
Chris Masonf254e522007-03-29 15:15:27 -0400192 sg_init_one(&sg, data, len);
Chris Mason87cbda52007-03-28 19:44:27 -0400193 spin_lock(&root->fs_info->hash_lock);
Chris Mason22b0ebd2007-03-30 08:47:31 -0400194 ret = crypto_hash_digest(&desc, &sg, 1, result);
Chris Mason87cbda52007-03-28 19:44:27 -0400195 spin_unlock(&root->fs_info->hash_lock);
196 if (ret) {
197 printk("sha256 digest failed\n");
198 }
Chris Masonf254e522007-03-29 15:15:27 -0400199 return ret;
200}
201static int csum_tree_block(struct btrfs_root *root, struct buffer_head *bh,
202 int verify)
203{
204 char result[BTRFS_CSUM_SIZE];
205 int ret;
206 struct btrfs_node *node;
207
208 ret = btrfs_csum_data(root, bh->b_data + BTRFS_CSUM_SIZE,
209 bh->b_size - BTRFS_CSUM_SIZE, result);
210 if (ret)
211 return ret;
Chris Mason87cbda52007-03-28 19:44:27 -0400212 if (verify) {
Chris Masonf254e522007-03-29 15:15:27 -0400213 if (memcmp(bh->b_data, result, BTRFS_CSUM_SIZE)) {
Chris Mason7eccb902007-04-11 15:53:25 -0400214 printk("checksum verify failed on %Lu\n",
215 bh_blocknr(bh));
Chris Masonf254e522007-03-29 15:15:27 -0400216 return 1;
217 }
218 } else {
219 node = btrfs_buffer_node(bh);
Chris Mason22b0ebd2007-03-30 08:47:31 -0400220 memcpy(node->header.csum, result, BTRFS_CSUM_SIZE);
Chris Masonf254e522007-03-29 15:15:27 -0400221 }
Chris Mason87cbda52007-03-28 19:44:27 -0400222 return 0;
223}
224
Chris Masond98237b2007-03-28 13:57:48 -0400225static int btree_writepage(struct page *page, struct writeback_control *wbc)
226{
Chris Mason87cbda52007-03-28 19:44:27 -0400227 struct buffer_head *bh;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400228 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
Chris Mason87cbda52007-03-28 19:44:27 -0400229 struct buffer_head *head;
Chris Mason87cbda52007-03-28 19:44:27 -0400230 if (!page_has_buffers(page)) {
231 create_empty_buffers(page, root->fs_info->sb->s_blocksize,
232 (1 << BH_Dirty)|(1 << BH_Uptodate));
233 }
234 head = page_buffers(page);
235 bh = head;
236 do {
237 if (buffer_dirty(bh))
238 csum_tree_block(root, bh, 0);
239 bh = bh->b_this_page;
240 } while (bh != head);
Chris Masond98237b2007-03-28 13:57:48 -0400241 return block_write_full_page(page, btree_get_block, wbc);
242}
243
244static int btree_readpage(struct file * file, struct page * page)
245{
246 return block_read_full_page(page, btree_get_block);
247}
248
249static struct address_space_operations btree_aops = {
250 .readpage = btree_readpage,
251 .writepage = btree_writepage,
252 .sync_page = block_sync_page,
253};
254
Chris Masone20d96d2007-03-22 12:13:20 -0400255struct buffer_head *read_tree_block(struct btrfs_root *root, u64 blocknr)
256{
Chris Masond98237b2007-03-28 13:57:48 -0400257 struct buffer_head *bh = NULL;
Chris Masone20d96d2007-03-22 12:13:20 -0400258
Chris Masond98237b2007-03-28 13:57:48 -0400259 bh = btrfs_find_create_tree_block(root, blocknr);
260 if (!bh)
261 return bh;
Chris Mason9d642722007-04-03 11:43:19 -0400262 if (buffer_uptodate(bh))
263 goto uptodate;
Chris Masond98237b2007-03-28 13:57:48 -0400264 lock_buffer(bh);
265 if (!buffer_uptodate(bh)) {
266 get_bh(bh);
267 bh->b_end_io = end_buffer_read_sync;
268 submit_bh(READ, bh);
269 wait_on_buffer(bh);
270 if (!buffer_uptodate(bh))
271 goto fail;
Chris Mason87cbda52007-03-28 19:44:27 -0400272 csum_tree_block(root, bh, 1);
Chris Masond98237b2007-03-28 13:57:48 -0400273 } else {
274 unlock_buffer(bh);
275 }
Chris Mason9d642722007-04-03 11:43:19 -0400276uptodate:
Chris Masond98237b2007-03-28 13:57:48 -0400277 if (check_tree_block(root, bh))
Chris Masoncfaa7292007-02-21 17:04:57 -0500278 BUG();
Chris Masond98237b2007-03-28 13:57:48 -0400279 return bh;
280fail:
281 brelse(bh);
282 return NULL;
Chris Masoneb60cea2007-02-02 09:18:22 -0500283}
284
Chris Masone089f052007-03-16 16:20:31 -0400285int dirty_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Masone20d96d2007-03-22 12:13:20 -0400286 struct buffer_head *buf)
Chris Masoned2ff2c2007-03-01 18:59:40 -0500287{
Chris Masond6025572007-03-30 14:27:56 -0400288 WARN_ON(atomic_read(&buf->b_count) == 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400289 mark_buffer_dirty(buf);
Chris Masoned2ff2c2007-03-01 18:59:40 -0500290 return 0;
291}
292
Chris Masone089f052007-03-16 16:20:31 -0400293int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Masone20d96d2007-03-22 12:13:20 -0400294 struct buffer_head *buf)
Chris Masoned2ff2c2007-03-01 18:59:40 -0500295{
Chris Masond6025572007-03-30 14:27:56 -0400296 WARN_ON(atomic_read(&buf->b_count) == 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400297 clear_buffer_dirty(buf);
Chris Masoned2ff2c2007-03-01 18:59:40 -0500298 return 0;
299}
300
Chris Mason2c90e5d2007-04-02 10:50:19 -0400301static int __setup_root(int blocksize,
Chris Mason9f5fae22007-03-20 14:38:32 -0400302 struct btrfs_root *root,
303 struct btrfs_fs_info *fs_info,
Chris Masone20d96d2007-03-22 12:13:20 -0400304 u64 objectid)
Chris Masond97e63b2007-02-20 16:40:44 -0500305{
Chris Masoncfaa7292007-02-21 17:04:57 -0500306 root->node = NULL;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400307 root->inode = NULL;
Chris Masona28ec192007-03-06 20:08:01 -0500308 root->commit_root = NULL;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400309 root->blocksize = blocksize;
Chris Mason123abc82007-03-14 14:14:43 -0400310 root->ref_cows = 0;
Chris Mason9f5fae22007-03-20 14:38:32 -0400311 root->fs_info = fs_info;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400312 root->objectid = objectid;
313 root->last_trans = 0;
Chris Mason1b05da22007-04-10 12:13:09 -0400314 root->highest_inode = 0;
315 root->last_inode_alloc = 0;
Chris Mason3768f362007-03-13 16:47:54 -0400316 memset(&root->root_key, 0, sizeof(root->root_key));
317 memset(&root->root_item, 0, sizeof(root->root_item));
318 return 0;
319}
320
Chris Mason2c90e5d2007-04-02 10:50:19 -0400321static int find_and_setup_root(int blocksize,
Chris Mason9f5fae22007-03-20 14:38:32 -0400322 struct btrfs_root *tree_root,
323 struct btrfs_fs_info *fs_info,
324 u64 objectid,
Chris Masone20d96d2007-03-22 12:13:20 -0400325 struct btrfs_root *root)
Chris Mason3768f362007-03-13 16:47:54 -0400326{
327 int ret;
328
Chris Mason2c90e5d2007-04-02 10:50:19 -0400329 __setup_root(blocksize, root, fs_info, objectid);
Chris Mason3768f362007-03-13 16:47:54 -0400330 ret = btrfs_find_last_root(tree_root, objectid,
331 &root->root_item, &root->root_key);
332 BUG_ON(ret);
333
334 root->node = read_tree_block(root,
335 btrfs_root_blocknr(&root->root_item));
Chris Mason3768f362007-03-13 16:47:54 -0400336 BUG_ON(!root->node);
Chris Masond97e63b2007-02-20 16:40:44 -0500337 return 0;
338}
339
Chris Mason0f7d52f2007-04-09 10:42:37 -0400340struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
341 struct btrfs_key *location)
342{
343 struct btrfs_root *root;
344 struct btrfs_root *tree_root = fs_info->tree_root;
345 struct btrfs_path *path;
346 struct btrfs_leaf *l;
Chris Mason1b05da22007-04-10 12:13:09 -0400347 u64 highest_inode;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400348 int ret = 0;
349
350printk("read_fs_root looking for %Lu %Lu %u\n", location->objectid, location->offset, location->flags);
Chris Mason2619ba12007-04-10 16:58:11 -0400351 root = radix_tree_lookup(&fs_info->fs_roots_radix,
352 (unsigned long)location->objectid);
353 if (root) {
354printk("found %p in cache\n", root);
355 return root;
356 }
Chris Mason0f7d52f2007-04-09 10:42:37 -0400357 root = kmalloc(sizeof(*root), GFP_NOFS);
358 if (!root) {
359printk("failed1\n");
360 return ERR_PTR(-ENOMEM);
361 }
362 if (location->offset == (u64)-1) {
363 ret = find_and_setup_root(fs_info->sb->s_blocksize,
364 fs_info->tree_root, fs_info,
365 location->objectid, root);
366 if (ret) {
367printk("failed2\n");
368 kfree(root);
369 return ERR_PTR(ret);
370 }
371 goto insert;
372 }
373
374 __setup_root(fs_info->sb->s_blocksize, root, fs_info,
375 location->objectid);
376
377 path = btrfs_alloc_path();
378 BUG_ON(!path);
379 ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
380 if (ret != 0) {
381printk("internal search_slot gives us %d\n", ret);
382 if (ret > 0)
383 ret = -ENOENT;
384 goto out;
385 }
386 l = btrfs_buffer_leaf(path->nodes[0]);
387 memcpy(&root->root_item,
388 btrfs_item_ptr(l, path->slots[0], struct btrfs_root_item),
389 sizeof(root->root_item));
390 memcpy(&root->root_key, location, sizeof(*location));
391 ret = 0;
392out:
393 btrfs_release_path(root, path);
394 btrfs_free_path(path);
395 if (ret) {
396 kfree(root);
397 return ERR_PTR(ret);
398 }
399 root->node = read_tree_block(root,
400 btrfs_root_blocknr(&root->root_item));
401 BUG_ON(!root->node);
402insert:
403printk("inserting %p\n", root);
404 root->ref_cows = 1;
Chris Mason2619ba12007-04-10 16:58:11 -0400405 ret = radix_tree_insert(&fs_info->fs_roots_radix,
406 (unsigned long)root->root_key.objectid,
Chris Mason0f7d52f2007-04-09 10:42:37 -0400407 root);
408 if (ret) {
409printk("radix_tree_insert gives us %d\n", ret);
410 brelse(root->node);
411 kfree(root);
412 return ERR_PTR(ret);
413 }
Chris Mason1b05da22007-04-10 12:13:09 -0400414 ret = btrfs_find_highest_inode(root, &highest_inode);
415 if (ret == 0) {
416 root->highest_inode = highest_inode;
417 root->last_inode_alloc = highest_inode;
418printk("highest inode is %Lu\n", highest_inode);
419 }
Chris Mason0f7d52f2007-04-09 10:42:37 -0400420printk("all worked\n");
421 return root;
422}
423
Chris Masonb4100d62007-04-12 12:14:00 -0400424static int btrfs_open_disk(struct btrfs_root *root, u64 device_id,
425 u64 block_start, u64 num_blocks,
426 char *filename, int name_len)
Chris Mason8352d8a2007-04-12 10:43:05 -0400427{
428 char *null_filename;
429 struct block_device *bdev;
430 int ret;
431
Chris Mason8352d8a2007-04-12 10:43:05 -0400432 null_filename = kmalloc(name_len + 1, GFP_NOFS);
433 if (!null_filename)
434 return -ENOMEM;
435 memcpy(null_filename, filename, name_len);
436 null_filename[name_len] = '\0';
437
438 bdev = open_bdev_excl(null_filename, O_RDWR, root->fs_info->sb);
439 if (IS_ERR(bdev)) {
440 ret = PTR_ERR(bdev);
441 goto out;
442 }
443 set_blocksize(bdev, root->fs_info->sb->s_blocksize);
Chris Masonb4100d62007-04-12 12:14:00 -0400444 ret = btrfs_insert_dev_radix(root, bdev, device_id,
445 block_start, num_blocks);
Chris Mason8352d8a2007-04-12 10:43:05 -0400446 BUG_ON(ret);
447 ret = 0;
448out:
449 kfree(null_filename);
450 return ret;
451}
452
453static int read_device_info(struct btrfs_root *root)
454{
455 struct btrfs_path *path;
456 int ret;
457 struct btrfs_key key;
458 struct btrfs_leaf *leaf;
459 struct btrfs_device_item *dev_item;
460 int nritems;
461 int slot;
462
463 root = root->fs_info->dev_root;
464
465 path = btrfs_alloc_path();
466 if (!path)
467 return -ENOMEM;
468 key.objectid = 0;
469 key.offset = 0;
470 key.flags = 0;
471 btrfs_set_key_type(&key, BTRFS_DEV_ITEM_KEY);
472
473 mutex_lock(&root->fs_info->fs_mutex);
474 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
475 leaf = btrfs_buffer_leaf(path->nodes[0]);
476 nritems = btrfs_header_nritems(&leaf->header);
477 while(1) {
478 slot = path->slots[0];
479 if (slot >= nritems) {
480 ret = btrfs_next_leaf(root, path);
481 if (ret)
482 break;
483 leaf = btrfs_buffer_leaf(path->nodes[0]);
484 nritems = btrfs_header_nritems(&leaf->header);
485 slot = path->slots[0];
486 }
487 btrfs_disk_key_to_cpu(&key, &leaf->items[slot].key);
488 if (btrfs_key_type(&key) != BTRFS_DEV_ITEM_KEY) {
489 path->slots[0]++;
490 continue;
491 }
492 dev_item = btrfs_item_ptr(leaf, slot, struct btrfs_device_item);
493printk("found key %Lu %Lu\n", key.objectid, key.offset);
Chris Masonb4100d62007-04-12 12:14:00 -0400494 if (btrfs_device_id(dev_item) !=
495 btrfs_super_device_id(root->fs_info->disk_super)) {
496 ret = btrfs_open_disk(root, btrfs_device_id(dev_item),
497 key.objectid, key.offset,
498 (char *)(dev_item + 1),
499 btrfs_device_pathlen(dev_item));
500 BUG_ON(ret);
501 }
Chris Mason8352d8a2007-04-12 10:43:05 -0400502 path->slots[0]++;
503 }
504 btrfs_free_path(path);
505 mutex_unlock(&root->fs_info->fs_mutex);
506 return 0;
507}
508
Chris Mason2c90e5d2007-04-02 10:50:19 -0400509struct btrfs_root *open_ctree(struct super_block *sb)
Chris Masoneb60cea2007-02-02 09:18:22 -0500510{
Chris Masone20d96d2007-03-22 12:13:20 -0400511 struct btrfs_root *extent_root = kmalloc(sizeof(struct btrfs_root),
512 GFP_NOFS);
Chris Mason0bd93ba2007-04-11 13:57:44 -0400513 struct btrfs_root *dev_root = kmalloc(sizeof(struct btrfs_root),
514 GFP_NOFS);
Chris Masone20d96d2007-03-22 12:13:20 -0400515 struct btrfs_root *tree_root = kmalloc(sizeof(struct btrfs_root),
516 GFP_NOFS);
Chris Masone20d96d2007-03-22 12:13:20 -0400517 struct btrfs_fs_info *fs_info = kmalloc(sizeof(*fs_info),
518 GFP_NOFS);
Chris Masoneb60cea2007-02-02 09:18:22 -0500519 int ret;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400520 struct btrfs_super_block *disk_super;
Chris Mason7eccb902007-04-11 15:53:25 -0400521 struct dev_lookup *dev_lookup;
Chris Masoneb60cea2007-02-02 09:18:22 -0500522
Chris Mason8ef97622007-03-26 10:15:30 -0400523 init_bit_radix(&fs_info->pinned_radix);
524 init_bit_radix(&fs_info->pending_del_radix);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400525 INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_NOFS);
Chris Mason7eccb902007-04-11 15:53:25 -0400526 INIT_RADIX_TREE(&fs_info->dev_radix, GFP_NOFS);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400527 sb_set_blocksize(sb, 4096);
Chris Mason9f5fae22007-03-20 14:38:32 -0400528 fs_info->running_transaction = NULL;
Chris Mason9f5fae22007-03-20 14:38:32 -0400529 fs_info->tree_root = tree_root;
530 fs_info->extent_root = extent_root;
Chris Mason0bd93ba2007-04-11 13:57:44 -0400531 fs_info->dev_root = dev_root;
Chris Masone20d96d2007-03-22 12:13:20 -0400532 fs_info->sb = sb;
Chris Masond98237b2007-03-28 13:57:48 -0400533 fs_info->btree_inode = new_inode(sb);
534 fs_info->btree_inode->i_ino = 1;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400535 fs_info->btree_inode->i_nlink = 1;
Chris Masond98237b2007-03-28 13:57:48 -0400536 fs_info->btree_inode->i_size = sb->s_bdev->bd_inode->i_size;
537 fs_info->btree_inode->i_mapping->a_ops = &btree_aops;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400538 BTRFS_I(fs_info->btree_inode)->root = tree_root;
539 memset(&BTRFS_I(fs_info->btree_inode)->location, 0,
540 sizeof(struct btrfs_key));
Chris Mason22b0ebd2007-03-30 08:47:31 -0400541 insert_inode_hash(fs_info->btree_inode);
Chris Masond98237b2007-03-28 13:57:48 -0400542 mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS);
Chris Mason87cbda52007-03-28 19:44:27 -0400543 fs_info->hash_tfm = crypto_alloc_hash("sha256", 0, CRYPTO_ALG_ASYNC);
Chris Mason30ae8462007-03-29 09:59:15 -0400544 spin_lock_init(&fs_info->hash_lock);
Chris Mason30ae8462007-03-29 09:59:15 -0400545 if (!fs_info->hash_tfm || IS_ERR(fs_info->hash_tfm)) {
Chris Mason87cbda52007-03-28 19:44:27 -0400546 printk("failed to allocate sha256 hash\n");
547 return NULL;
548 }
Chris Mason79154b12007-03-22 15:59:16 -0400549 mutex_init(&fs_info->trans_mutex);
Chris Masond561c022007-03-23 19:47:49 -0400550 mutex_init(&fs_info->fs_mutex);
Chris Mason9f5fae22007-03-20 14:38:32 -0400551 memset(&fs_info->current_insert, 0, sizeof(fs_info->current_insert));
552 memset(&fs_info->last_insert, 0, sizeof(fs_info->last_insert));
Chris Mason3768f362007-03-13 16:47:54 -0400553
Chris Mason0bd93ba2007-04-11 13:57:44 -0400554 __setup_root(sb->s_blocksize, dev_root,
555 fs_info, BTRFS_DEV_TREE_OBJECTID);
556
Chris Mason2c90e5d2007-04-02 10:50:19 -0400557 __setup_root(sb->s_blocksize, tree_root,
558 fs_info, BTRFS_ROOT_TREE_OBJECTID);
Chris Mason7eccb902007-04-11 15:53:25 -0400559
560 dev_lookup = kmalloc(sizeof(*dev_lookup), GFP_NOFS);
561 dev_lookup->block_start = 0;
562 dev_lookup->num_blocks = (u32)-2;
563 dev_lookup->bdev = sb->s_bdev;
Chris Masonb4100d62007-04-12 12:14:00 -0400564 dev_lookup->device_id = 0;
Chris Mason7eccb902007-04-11 15:53:25 -0400565 ret = radix_tree_insert(&fs_info->dev_radix, (u32)-2, dev_lookup);
566 BUG_ON(ret);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400567 fs_info->sb_buffer = read_tree_block(tree_root,
568 BTRFS_SUPER_INFO_OFFSET /
569 sb->s_blocksize);
Chris Masond98237b2007-03-28 13:57:48 -0400570
Chris Mason0f7d52f2007-04-09 10:42:37 -0400571 if (!fs_info->sb_buffer)
Chris Masond98237b2007-03-28 13:57:48 -0400572 return NULL;
Chris Masond98237b2007-03-28 13:57:48 -0400573 disk_super = (struct btrfs_super_block *)fs_info->sb_buffer->b_data;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400574 if (!btrfs_super_root(disk_super))
Chris Mason2c90e5d2007-04-02 10:50:19 -0400575 return NULL;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400576
Chris Mason8352d8a2007-04-12 10:43:05 -0400577 i_size_write(fs_info->btree_inode,
578 btrfs_super_total_blocks(disk_super) <<
579 fs_info->btree_inode->i_blkbits);
580
Chris Mason7eccb902007-04-11 15:53:25 -0400581 radix_tree_delete(&fs_info->dev_radix, (u32)-2);
582 dev_lookup->block_start = btrfs_super_device_block_start(disk_super);
583 dev_lookup->num_blocks = btrfs_super_device_num_blocks(disk_super);
Chris Masonb4100d62007-04-12 12:14:00 -0400584 dev_lookup->device_id = btrfs_super_device_id(disk_super);
585
Chris Mason7eccb902007-04-11 15:53:25 -0400586 ret = radix_tree_insert(&fs_info->dev_radix,
587 dev_lookup->block_start +
Chris Mason8352d8a2007-04-12 10:43:05 -0400588 dev_lookup->num_blocks - 1, dev_lookup);
Chris Mason7eccb902007-04-11 15:53:25 -0400589 BUG_ON(ret);
590
Chris Masond98237b2007-03-28 13:57:48 -0400591 fs_info->disk_super = disk_super;
Chris Mason8352d8a2007-04-12 10:43:05 -0400592
Chris Mason0bd93ba2007-04-11 13:57:44 -0400593 dev_root->node = read_tree_block(tree_root,
594 btrfs_super_device_root(disk_super));
Chris Mason8352d8a2007-04-12 10:43:05 -0400595
596 ret = read_device_info(dev_root);
597 BUG_ON(ret);
598
Chris Masone20d96d2007-03-22 12:13:20 -0400599 tree_root->node = read_tree_block(tree_root,
600 btrfs_super_root(disk_super));
Chris Mason3768f362007-03-13 16:47:54 -0400601 BUG_ON(!tree_root->node);
602
Chris Mason2c90e5d2007-04-02 10:50:19 -0400603 mutex_lock(&fs_info->fs_mutex);
604 ret = find_and_setup_root(sb->s_blocksize, tree_root, fs_info,
Chris Masone20d96d2007-03-22 12:13:20 -0400605 BTRFS_EXTENT_TREE_OBJECTID, extent_root);
Chris Mason3768f362007-03-13 16:47:54 -0400606 BUG_ON(ret);
607
Chris Mason0f7d52f2007-04-09 10:42:37 -0400608 fs_info->generation = btrfs_super_generation(disk_super) + 1;
Chris Masond6e4a422007-04-06 15:37:36 -0400609 memset(&fs_info->kobj, 0, sizeof(fs_info->kobj));
610 kobj_set_kset_s(fs_info, btrfs_subsys);
611 kobject_set_name(&fs_info->kobj, "%s", sb->s_id);
612 kobject_register(&fs_info->kobj);
Chris Mason5be6f7f2007-04-05 13:35:25 -0400613 mutex_unlock(&fs_info->fs_mutex);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400614 return tree_root;
Chris Masoneb60cea2007-02-02 09:18:22 -0500615}
616
Chris Masone089f052007-03-16 16:20:31 -0400617int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason79154b12007-03-22 15:59:16 -0400618 *root)
Chris Masoncfaa7292007-02-21 17:04:57 -0500619{
Chris Masond5719762007-03-23 10:01:08 -0400620 struct buffer_head *bh = root->fs_info->sb_buffer;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400621
Chris Masond5719762007-03-23 10:01:08 -0400622 btrfs_set_super_root(root->fs_info->disk_super,
Chris Mason7eccb902007-04-11 15:53:25 -0400623 bh_blocknr(root->fs_info->tree_root->node));
Chris Masond5719762007-03-23 10:01:08 -0400624 lock_buffer(bh);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400625 WARN_ON(atomic_read(&bh->b_count) < 1);
Chris Masond5719762007-03-23 10:01:08 -0400626 clear_buffer_dirty(bh);
Chris Mason87cbda52007-03-28 19:44:27 -0400627 csum_tree_block(root, bh, 0);
Chris Masond5719762007-03-23 10:01:08 -0400628 bh->b_end_io = end_buffer_write_sync;
629 get_bh(bh);
630 submit_bh(WRITE, bh);
631 wait_on_buffer(bh);
632 if (!buffer_uptodate(bh)) {
633 WARN_ON(1);
634 return -EIO;
Chris Masoncfaa7292007-02-21 17:04:57 -0500635 }
636 return 0;
637}
638
Chris Mason2619ba12007-04-10 16:58:11 -0400639static int free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
640{
641 radix_tree_delete(&fs_info->fs_roots_radix,
642 (unsigned long)root->root_key.objectid);
643 if (root->inode)
644 iput(root->inode);
645 if (root->node)
646 brelse(root->node);
647 if (root->commit_root)
648 brelse(root->commit_root);
649 kfree(root);
650 return 0;
651}
652
Chris Mason0f7d52f2007-04-09 10:42:37 -0400653int del_fs_roots(struct btrfs_fs_info *fs_info)
654{
655 int ret;
656 struct btrfs_root *gang[8];
657 int i;
658
659 while(1) {
660 ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix,
661 (void **)gang, 0,
662 ARRAY_SIZE(gang));
663 if (!ret)
664 break;
Chris Mason2619ba12007-04-10 16:58:11 -0400665 for (i = 0; i < ret; i++)
666 free_fs_root(fs_info, gang[i]);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400667 }
668 return 0;
669}
Chris Masonb4100d62007-04-12 12:14:00 -0400670
Chris Mason7eccb902007-04-11 15:53:25 -0400671static int free_dev_radix(struct btrfs_fs_info *fs_info)
672{
673 struct dev_lookup *lookup[8];
674 struct block_device *super_bdev = fs_info->sb->s_bdev;
675 int ret;
676 int i;
677 while(1) {
678 ret = radix_tree_gang_lookup(&fs_info->dev_radix,
679 (void **)lookup, 0,
680 ARRAY_SIZE(lookup));
681 if (!ret)
682 break;
683 for (i = 0; i < ret; i++) {
684 if (lookup[i]->bdev != super_bdev)
685 close_bdev_excl(lookup[i]->bdev);
686 radix_tree_delete(&fs_info->dev_radix,
687 lookup[i]->block_start +
Chris Mason8352d8a2007-04-12 10:43:05 -0400688 lookup[i]->num_blocks - 1);
Chris Mason7eccb902007-04-11 15:53:25 -0400689 kfree(lookup[i]);
690 }
691 }
692 return 0;
693}
Chris Mason0f7d52f2007-04-09 10:42:37 -0400694
Chris Masone20d96d2007-03-22 12:13:20 -0400695int close_ctree(struct btrfs_root *root)
Chris Masoneb60cea2007-02-02 09:18:22 -0500696{
Chris Mason3768f362007-03-13 16:47:54 -0400697 int ret;
Chris Masone089f052007-03-16 16:20:31 -0400698 struct btrfs_trans_handle *trans;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400699 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone089f052007-03-16 16:20:31 -0400700
Chris Mason0f7d52f2007-04-09 10:42:37 -0400701 mutex_lock(&fs_info->fs_mutex);
Chris Mason79154b12007-03-22 15:59:16 -0400702 trans = btrfs_start_transaction(root, 1);
703 btrfs_commit_transaction(trans, root);
704 /* run commit again to drop the original snapshot */
705 trans = btrfs_start_transaction(root, 1);
706 btrfs_commit_transaction(trans, root);
707 ret = btrfs_write_and_wait_transaction(NULL, root);
Chris Mason9f5fae22007-03-20 14:38:32 -0400708 BUG_ON(ret);
Chris Mason79154b12007-03-22 15:59:16 -0400709 write_ctree_super(NULL, root);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400710 mutex_unlock(&fs_info->fs_mutex);
Chris Masoned2ff2c2007-03-01 18:59:40 -0500711
Chris Mason0f7d52f2007-04-09 10:42:37 -0400712 if (fs_info->extent_root->node)
713 btrfs_block_release(fs_info->extent_root,
714 fs_info->extent_root->node);
Chris Mason0bd93ba2007-04-11 13:57:44 -0400715 if (fs_info->dev_root->node)
716 btrfs_block_release(fs_info->dev_root,
717 fs_info->dev_root->node);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400718 if (fs_info->tree_root->node)
719 btrfs_block_release(fs_info->tree_root,
720 fs_info->tree_root->node);
721 btrfs_block_release(root, fs_info->sb_buffer);
722 crypto_free_hash(fs_info->hash_tfm);
723 truncate_inode_pages(fs_info->btree_inode->i_mapping, 0);
724 iput(fs_info->btree_inode);
Chris Mason7eccb902007-04-11 15:53:25 -0400725
726 free_dev_radix(fs_info);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400727 del_fs_roots(fs_info);
728 kfree(fs_info->extent_root);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400729 kfree(fs_info->tree_root);
730 kobject_unregister(&fs_info->kobj);
Chris Masoneb60cea2007-02-02 09:18:22 -0500731 return 0;
732}
733
Chris Masone20d96d2007-03-22 12:13:20 -0400734void btrfs_block_release(struct btrfs_root *root, struct buffer_head *buf)
Chris Masoneb60cea2007-02-02 09:18:22 -0500735{
Chris Mason7cfcc172007-04-02 14:53:59 -0400736 brelse(buf);
Chris Masoneb60cea2007-02-02 09:18:22 -0500737}
738