blob: 3ba4df20c913fe77ac910cec7fb369ae29081370 [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;
16 struct block_device *bdev;
17};
18
Chris Mason8352d8a2007-04-12 10:43:05 -040019int btrfs_insert_dev_radix(struct btrfs_root *root,
20 struct block_device *bdev,
21 u64 block_start,
22 u64 num_blocks)
23{
24 struct dev_lookup *lookup;
25 char b[BDEVNAME_SIZE];
26 int ret;
27
28 lookup = kmalloc(sizeof(*lookup), GFP_NOFS);
29 if (!lookup)
30 return -ENOMEM;
31 lookup->block_start = block_start;
32 lookup->num_blocks = num_blocks;
33 lookup->bdev = bdev;
34printk("inserting %s into dev radix %Lu %Lu\n", bdevname(bdev, b), block_start, num_blocks);
35
36 ret = radix_tree_insert(&root->fs_info->dev_radix, block_start +
37 num_blocks - 1, lookup);
38 return ret;
39}
40
Chris Mason7eccb902007-04-11 15:53:25 -040041u64 bh_blocknr(struct buffer_head *bh)
42{
43 int blkbits = bh->b_page->mapping->host->i_blkbits;
44 u64 blocknr = bh->b_page->index << (PAGE_CACHE_SHIFT - blkbits);
45 unsigned long offset;
46
47 if (PageHighMem(bh->b_page))
48 offset = (unsigned long)bh->b_data;
49 else
50 offset = bh->b_data - (char *)page_address(bh->b_page);
51 blocknr += offset >> (PAGE_CACHE_SHIFT - blkbits);
52 return blocknr;
53}
54
Chris Masone20d96d2007-03-22 12:13:20 -040055static int check_tree_block(struct btrfs_root *root, struct buffer_head *buf)
Chris Masoneb60cea2007-02-02 09:18:22 -050056{
Chris Masone20d96d2007-03-22 12:13:20 -040057 struct btrfs_node *node = btrfs_buffer_node(buf);
Chris Mason7eccb902007-04-11 15:53:25 -040058 if (bh_blocknr(buf) != btrfs_header_blocknr(&node->header)) {
Chris Mason8352d8a2007-04-12 10:43:05 -040059 printk(KERN_CRIT "bh_blocknr(buf) is %Lu, header is %Lu\n",
60 bh_blocknr(buf), btrfs_header_blocknr(&node->header));
Chris Mason9a8dd152007-02-23 08:38:36 -050061 BUG();
Chris Masond98237b2007-03-28 13:57:48 -040062 }
Chris Mason9a8dd152007-02-23 08:38:36 -050063 return 0;
Chris Masoneb60cea2007-02-02 09:18:22 -050064}
65
Chris Masond98237b2007-03-28 13:57:48 -040066struct buffer_head *btrfs_find_tree_block(struct btrfs_root *root, u64 blocknr)
Chris Masoned2ff2c2007-03-01 18:59:40 -050067{
Chris Masond98237b2007-03-28 13:57:48 -040068 struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
69 int blockbits = root->fs_info->sb->s_blocksize_bits;
70 unsigned long index = blocknr >> (PAGE_CACHE_SHIFT - blockbits);
71 struct page *page;
72 struct buffer_head *bh;
73 struct buffer_head *head;
74 struct buffer_head *ret = NULL;
75
Chris Mason2c90e5d2007-04-02 10:50:19 -040076
Chris Masond98237b2007-03-28 13:57:48 -040077 page = find_lock_page(mapping, index);
78 if (!page)
79 return NULL;
80
81 if (!page_has_buffers(page))
82 goto out_unlock;
83
84 head = page_buffers(page);
85 bh = head;
86 do {
Chris Mason7eccb902007-04-11 15:53:25 -040087 if (buffer_mapped(bh) && bh_blocknr(bh) == blocknr) {
Chris Masond98237b2007-03-28 13:57:48 -040088 ret = bh;
89 get_bh(bh);
90 goto out_unlock;
91 }
92 bh = bh->b_this_page;
93 } while (bh != head);
94out_unlock:
95 unlock_page(page);
Chris Masond6025572007-03-30 14:27:56 -040096 if (ret) {
Chris Mason22b0ebd2007-03-30 08:47:31 -040097 touch_buffer(ret);
Chris Masond6025572007-03-30 14:27:56 -040098 }
Chris Masond98237b2007-03-28 13:57:48 -040099 page_cache_release(page);
100 return ret;
Chris Masoned2ff2c2007-03-01 18:59:40 -0500101}
102
Chris Mason8352d8a2007-04-12 10:43:05 -0400103int btrfs_map_bh_to_logical(struct btrfs_root *root, struct buffer_head *bh,
Chris Mason7eccb902007-04-11 15:53:25 -0400104 u64 logical)
105{
106 struct dev_lookup *lookup[2];
Chris Mason7eccb902007-04-11 15:53:25 -0400107
108 int ret;
109
110 root = root->fs_info->dev_root;
111 ret = radix_tree_gang_lookup(&root->fs_info->dev_radix,
112 (void **)lookup,
113 (unsigned long)logical,
114 ARRAY_SIZE(lookup));
115 if (ret == 0 || lookup[0]->block_start > logical ||
116 lookup[0]->block_start + lookup[0]->num_blocks <= logical) {
117 ret = -ENOENT;
118 goto out;
119 }
120 bh->b_bdev = lookup[0]->bdev;
121 bh->b_blocknr = logical - lookup[0]->block_start;
Chris Mason7eccb902007-04-11 15:53:25 -0400122 set_buffer_mapped(bh);
123 ret = 0;
124out:
125 return ret;
126}
127
Chris Masond98237b2007-03-28 13:57:48 -0400128struct buffer_head *btrfs_find_create_tree_block(struct btrfs_root *root,
129 u64 blocknr)
Chris Masoneb60cea2007-02-02 09:18:22 -0500130{
Chris Masond98237b2007-03-28 13:57:48 -0400131 struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
132 int blockbits = root->fs_info->sb->s_blocksize_bits;
133 unsigned long index = blocknr >> (PAGE_CACHE_SHIFT - blockbits);
134 struct page *page;
135 struct buffer_head *bh;
136 struct buffer_head *head;
137 struct buffer_head *ret = NULL;
Chris Mason7eccb902007-04-11 15:53:25 -0400138 int err;
Chris Masond98237b2007-03-28 13:57:48 -0400139 u64 first_block = index << (PAGE_CACHE_SHIFT - blockbits);
Chris Mason22b0ebd2007-03-30 08:47:31 -0400140
Chris Masond98237b2007-03-28 13:57:48 -0400141 page = grab_cache_page(mapping, index);
142 if (!page)
143 return NULL;
144
Chris Masond98237b2007-03-28 13:57:48 -0400145 if (!page_has_buffers(page))
146 create_empty_buffers(page, root->fs_info->sb->s_blocksize, 0);
147 head = page_buffers(page);
148 bh = head;
149 do {
150 if (!buffer_mapped(bh)) {
Chris Mason8352d8a2007-04-12 10:43:05 -0400151 err = btrfs_map_bh_to_logical(root, bh, first_block);
Chris Mason7eccb902007-04-11 15:53:25 -0400152 BUG_ON(err);
Chris Masond98237b2007-03-28 13:57:48 -0400153 }
Chris Mason7eccb902007-04-11 15:53:25 -0400154 if (bh_blocknr(bh) == blocknr) {
Chris Masond98237b2007-03-28 13:57:48 -0400155 ret = bh;
156 get_bh(bh);
157 goto out_unlock;
158 }
159 bh = bh->b_this_page;
160 first_block++;
161 } while (bh != head);
162out_unlock:
163 unlock_page(page);
Chris Mason22b0ebd2007-03-30 08:47:31 -0400164 if (ret)
165 touch_buffer(ret);
Chris Masond98237b2007-03-28 13:57:48 -0400166 page_cache_release(page);
167 return ret;
Chris Masone20d96d2007-03-22 12:13:20 -0400168}
Chris Mason123abc82007-03-14 14:14:43 -0400169
Chris Masond98237b2007-03-28 13:57:48 -0400170static int btree_get_block(struct inode *inode, sector_t iblock,
171 struct buffer_head *bh, int create)
172{
Chris Mason7eccb902007-04-11 15:53:25 -0400173 int err;
174 struct btrfs_root *root = BTRFS_I(bh->b_page->mapping->host)->root;
Chris Mason8352d8a2007-04-12 10:43:05 -0400175 err = btrfs_map_bh_to_logical(root, bh, iblock);
Chris Mason7eccb902007-04-11 15:53:25 -0400176 return err;
Chris Masond98237b2007-03-28 13:57:48 -0400177}
178
Chris Masonf254e522007-03-29 15:15:27 -0400179int btrfs_csum_data(struct btrfs_root * root, char *data, size_t len,
180 char *result)
Chris Mason87cbda52007-03-28 19:44:27 -0400181{
Chris Mason87cbda52007-03-28 19:44:27 -0400182 struct scatterlist sg;
183 struct crypto_hash *tfm = root->fs_info->hash_tfm;
184 struct hash_desc desc;
185 int ret;
Chris Mason87cbda52007-03-28 19:44:27 -0400186
187 desc.tfm = tfm;
188 desc.flags = 0;
Chris Masonf254e522007-03-29 15:15:27 -0400189 sg_init_one(&sg, data, len);
Chris Mason87cbda52007-03-28 19:44:27 -0400190 spin_lock(&root->fs_info->hash_lock);
Chris Mason22b0ebd2007-03-30 08:47:31 -0400191 ret = crypto_hash_digest(&desc, &sg, 1, result);
Chris Mason87cbda52007-03-28 19:44:27 -0400192 spin_unlock(&root->fs_info->hash_lock);
193 if (ret) {
194 printk("sha256 digest failed\n");
195 }
Chris Masonf254e522007-03-29 15:15:27 -0400196 return ret;
197}
198static int csum_tree_block(struct btrfs_root *root, struct buffer_head *bh,
199 int verify)
200{
201 char result[BTRFS_CSUM_SIZE];
202 int ret;
203 struct btrfs_node *node;
204
205 ret = btrfs_csum_data(root, bh->b_data + BTRFS_CSUM_SIZE,
206 bh->b_size - BTRFS_CSUM_SIZE, result);
207 if (ret)
208 return ret;
Chris Mason87cbda52007-03-28 19:44:27 -0400209 if (verify) {
Chris Masonf254e522007-03-29 15:15:27 -0400210 if (memcmp(bh->b_data, result, BTRFS_CSUM_SIZE)) {
Chris Mason7eccb902007-04-11 15:53:25 -0400211 printk("checksum verify failed on %Lu\n",
212 bh_blocknr(bh));
Chris Masonf254e522007-03-29 15:15:27 -0400213 return 1;
214 }
215 } else {
216 node = btrfs_buffer_node(bh);
Chris Mason22b0ebd2007-03-30 08:47:31 -0400217 memcpy(node->header.csum, result, BTRFS_CSUM_SIZE);
Chris Masonf254e522007-03-29 15:15:27 -0400218 }
Chris Mason87cbda52007-03-28 19:44:27 -0400219 return 0;
220}
221
Chris Masond98237b2007-03-28 13:57:48 -0400222static int btree_writepage(struct page *page, struct writeback_control *wbc)
223{
Chris Mason87cbda52007-03-28 19:44:27 -0400224 struct buffer_head *bh;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400225 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
Chris Mason87cbda52007-03-28 19:44:27 -0400226 struct buffer_head *head;
Chris Mason87cbda52007-03-28 19:44:27 -0400227 if (!page_has_buffers(page)) {
228 create_empty_buffers(page, root->fs_info->sb->s_blocksize,
229 (1 << BH_Dirty)|(1 << BH_Uptodate));
230 }
231 head = page_buffers(page);
232 bh = head;
233 do {
234 if (buffer_dirty(bh))
235 csum_tree_block(root, bh, 0);
236 bh = bh->b_this_page;
237 } while (bh != head);
Chris Masond98237b2007-03-28 13:57:48 -0400238 return block_write_full_page(page, btree_get_block, wbc);
239}
240
241static int btree_readpage(struct file * file, struct page * page)
242{
243 return block_read_full_page(page, btree_get_block);
244}
245
246static struct address_space_operations btree_aops = {
247 .readpage = btree_readpage,
248 .writepage = btree_writepage,
249 .sync_page = block_sync_page,
250};
251
Chris Masone20d96d2007-03-22 12:13:20 -0400252struct buffer_head *read_tree_block(struct btrfs_root *root, u64 blocknr)
253{
Chris Masond98237b2007-03-28 13:57:48 -0400254 struct buffer_head *bh = NULL;
Chris Masone20d96d2007-03-22 12:13:20 -0400255
Chris Masond98237b2007-03-28 13:57:48 -0400256 bh = btrfs_find_create_tree_block(root, blocknr);
257 if (!bh)
258 return bh;
Chris Mason9d642722007-04-03 11:43:19 -0400259 if (buffer_uptodate(bh))
260 goto uptodate;
Chris Masond98237b2007-03-28 13:57:48 -0400261 lock_buffer(bh);
262 if (!buffer_uptodate(bh)) {
263 get_bh(bh);
264 bh->b_end_io = end_buffer_read_sync;
265 submit_bh(READ, bh);
266 wait_on_buffer(bh);
267 if (!buffer_uptodate(bh))
268 goto fail;
Chris Mason87cbda52007-03-28 19:44:27 -0400269 csum_tree_block(root, bh, 1);
Chris Masond98237b2007-03-28 13:57:48 -0400270 } else {
271 unlock_buffer(bh);
272 }
Chris Mason9d642722007-04-03 11:43:19 -0400273uptodate:
Chris Masond98237b2007-03-28 13:57:48 -0400274 if (check_tree_block(root, bh))
Chris Masoncfaa7292007-02-21 17:04:57 -0500275 BUG();
Chris Masond98237b2007-03-28 13:57:48 -0400276 return bh;
277fail:
278 brelse(bh);
279 return NULL;
Chris Masoneb60cea2007-02-02 09:18:22 -0500280}
281
Chris Masone089f052007-03-16 16:20:31 -0400282int dirty_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Masone20d96d2007-03-22 12:13:20 -0400283 struct buffer_head *buf)
Chris Masoned2ff2c2007-03-01 18:59:40 -0500284{
Chris Masond6025572007-03-30 14:27:56 -0400285 WARN_ON(atomic_read(&buf->b_count) == 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400286 mark_buffer_dirty(buf);
Chris Masoned2ff2c2007-03-01 18:59:40 -0500287 return 0;
288}
289
Chris Masone089f052007-03-16 16:20:31 -0400290int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Masone20d96d2007-03-22 12:13:20 -0400291 struct buffer_head *buf)
Chris Masoned2ff2c2007-03-01 18:59:40 -0500292{
Chris Masond6025572007-03-30 14:27:56 -0400293 WARN_ON(atomic_read(&buf->b_count) == 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400294 clear_buffer_dirty(buf);
Chris Masoned2ff2c2007-03-01 18:59:40 -0500295 return 0;
296}
297
Chris Mason2c90e5d2007-04-02 10:50:19 -0400298static int __setup_root(int blocksize,
Chris Mason9f5fae22007-03-20 14:38:32 -0400299 struct btrfs_root *root,
300 struct btrfs_fs_info *fs_info,
Chris Masone20d96d2007-03-22 12:13:20 -0400301 u64 objectid)
Chris Masond97e63b2007-02-20 16:40:44 -0500302{
Chris Masoncfaa7292007-02-21 17:04:57 -0500303 root->node = NULL;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400304 root->inode = NULL;
Chris Masona28ec192007-03-06 20:08:01 -0500305 root->commit_root = NULL;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400306 root->blocksize = blocksize;
Chris Mason123abc82007-03-14 14:14:43 -0400307 root->ref_cows = 0;
Chris Mason9f5fae22007-03-20 14:38:32 -0400308 root->fs_info = fs_info;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400309 root->objectid = objectid;
310 root->last_trans = 0;
Chris Mason1b05da22007-04-10 12:13:09 -0400311 root->highest_inode = 0;
312 root->last_inode_alloc = 0;
Chris Mason3768f362007-03-13 16:47:54 -0400313 memset(&root->root_key, 0, sizeof(root->root_key));
314 memset(&root->root_item, 0, sizeof(root->root_item));
315 return 0;
316}
317
Chris Mason2c90e5d2007-04-02 10:50:19 -0400318static int find_and_setup_root(int blocksize,
Chris Mason9f5fae22007-03-20 14:38:32 -0400319 struct btrfs_root *tree_root,
320 struct btrfs_fs_info *fs_info,
321 u64 objectid,
Chris Masone20d96d2007-03-22 12:13:20 -0400322 struct btrfs_root *root)
Chris Mason3768f362007-03-13 16:47:54 -0400323{
324 int ret;
325
Chris Mason2c90e5d2007-04-02 10:50:19 -0400326 __setup_root(blocksize, root, fs_info, objectid);
Chris Mason3768f362007-03-13 16:47:54 -0400327 ret = btrfs_find_last_root(tree_root, objectid,
328 &root->root_item, &root->root_key);
329 BUG_ON(ret);
330
331 root->node = read_tree_block(root,
332 btrfs_root_blocknr(&root->root_item));
Chris Mason3768f362007-03-13 16:47:54 -0400333 BUG_ON(!root->node);
Chris Masond97e63b2007-02-20 16:40:44 -0500334 return 0;
335}
336
Chris Mason0f7d52f2007-04-09 10:42:37 -0400337struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
338 struct btrfs_key *location)
339{
340 struct btrfs_root *root;
341 struct btrfs_root *tree_root = fs_info->tree_root;
342 struct btrfs_path *path;
343 struct btrfs_leaf *l;
Chris Mason1b05da22007-04-10 12:13:09 -0400344 u64 highest_inode;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400345 int ret = 0;
346
347printk("read_fs_root looking for %Lu %Lu %u\n", location->objectid, location->offset, location->flags);
Chris Mason2619ba12007-04-10 16:58:11 -0400348 root = radix_tree_lookup(&fs_info->fs_roots_radix,
349 (unsigned long)location->objectid);
350 if (root) {
351printk("found %p in cache\n", root);
352 return root;
353 }
Chris Mason0f7d52f2007-04-09 10:42:37 -0400354 root = kmalloc(sizeof(*root), GFP_NOFS);
355 if (!root) {
356printk("failed1\n");
357 return ERR_PTR(-ENOMEM);
358 }
359 if (location->offset == (u64)-1) {
360 ret = find_and_setup_root(fs_info->sb->s_blocksize,
361 fs_info->tree_root, fs_info,
362 location->objectid, root);
363 if (ret) {
364printk("failed2\n");
365 kfree(root);
366 return ERR_PTR(ret);
367 }
368 goto insert;
369 }
370
371 __setup_root(fs_info->sb->s_blocksize, root, fs_info,
372 location->objectid);
373
374 path = btrfs_alloc_path();
375 BUG_ON(!path);
376 ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
377 if (ret != 0) {
378printk("internal search_slot gives us %d\n", ret);
379 if (ret > 0)
380 ret = -ENOENT;
381 goto out;
382 }
383 l = btrfs_buffer_leaf(path->nodes[0]);
384 memcpy(&root->root_item,
385 btrfs_item_ptr(l, path->slots[0], struct btrfs_root_item),
386 sizeof(root->root_item));
387 memcpy(&root->root_key, location, sizeof(*location));
388 ret = 0;
389out:
390 btrfs_release_path(root, path);
391 btrfs_free_path(path);
392 if (ret) {
393 kfree(root);
394 return ERR_PTR(ret);
395 }
396 root->node = read_tree_block(root,
397 btrfs_root_blocknr(&root->root_item));
398 BUG_ON(!root->node);
399insert:
400printk("inserting %p\n", root);
401 root->ref_cows = 1;
Chris Mason2619ba12007-04-10 16:58:11 -0400402 ret = radix_tree_insert(&fs_info->fs_roots_radix,
403 (unsigned long)root->root_key.objectid,
Chris Mason0f7d52f2007-04-09 10:42:37 -0400404 root);
405 if (ret) {
406printk("radix_tree_insert gives us %d\n", ret);
407 brelse(root->node);
408 kfree(root);
409 return ERR_PTR(ret);
410 }
Chris Mason1b05da22007-04-10 12:13:09 -0400411 ret = btrfs_find_highest_inode(root, &highest_inode);
412 if (ret == 0) {
413 root->highest_inode = highest_inode;
414 root->last_inode_alloc = highest_inode;
415printk("highest inode is %Lu\n", highest_inode);
416 }
Chris Mason0f7d52f2007-04-09 10:42:37 -0400417printk("all worked\n");
418 return root;
419}
420
Chris Mason8352d8a2007-04-12 10:43:05 -0400421int btrfs_open_disk(struct btrfs_root *root, u64 block_start, u64 num_blocks,
422 char *filename, int name_len)
423{
424 char *null_filename;
425 struct block_device *bdev;
426 int ret;
427
428 if (block_start == 0) {
429printk("skipping disk with block_start == 0\n");
430return 0;
431 }
432 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);
444 ret = btrfs_insert_dev_radix(root, bdev, block_start, num_blocks);
445 BUG_ON(ret);
446 ret = 0;
447out:
448 kfree(null_filename);
449 return ret;
450}
451
452static int read_device_info(struct btrfs_root *root)
453{
454 struct btrfs_path *path;
455 int ret;
456 struct btrfs_key key;
457 struct btrfs_leaf *leaf;
458 struct btrfs_device_item *dev_item;
459 int nritems;
460 int slot;
461
462 root = root->fs_info->dev_root;
463
464 path = btrfs_alloc_path();
465 if (!path)
466 return -ENOMEM;
467 key.objectid = 0;
468 key.offset = 0;
469 key.flags = 0;
470 btrfs_set_key_type(&key, BTRFS_DEV_ITEM_KEY);
471
472 mutex_lock(&root->fs_info->fs_mutex);
473 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
474 leaf = btrfs_buffer_leaf(path->nodes[0]);
475 nritems = btrfs_header_nritems(&leaf->header);
476 while(1) {
477 slot = path->slots[0];
478 if (slot >= nritems) {
479 ret = btrfs_next_leaf(root, path);
480 if (ret)
481 break;
482 leaf = btrfs_buffer_leaf(path->nodes[0]);
483 nritems = btrfs_header_nritems(&leaf->header);
484 slot = path->slots[0];
485 }
486 btrfs_disk_key_to_cpu(&key, &leaf->items[slot].key);
487 if (btrfs_key_type(&key) != BTRFS_DEV_ITEM_KEY) {
488 path->slots[0]++;
489 continue;
490 }
491 dev_item = btrfs_item_ptr(leaf, slot, struct btrfs_device_item);
492printk("found key %Lu %Lu\n", key.objectid, key.offset);
493 ret = btrfs_open_disk(root, key.objectid, key.offset,
494 (char *)(dev_item + 1),
495 btrfs_device_pathlen(dev_item));
496 BUG_ON(ret);
497 path->slots[0]++;
498 }
499 btrfs_free_path(path);
500 mutex_unlock(&root->fs_info->fs_mutex);
501 return 0;
502}
503
Chris Mason2c90e5d2007-04-02 10:50:19 -0400504struct btrfs_root *open_ctree(struct super_block *sb)
Chris Masoneb60cea2007-02-02 09:18:22 -0500505{
Chris Masone20d96d2007-03-22 12:13:20 -0400506 struct btrfs_root *extent_root = kmalloc(sizeof(struct btrfs_root),
507 GFP_NOFS);
Chris Mason0bd93ba2007-04-11 13:57:44 -0400508 struct btrfs_root *dev_root = kmalloc(sizeof(struct btrfs_root),
509 GFP_NOFS);
Chris Masone20d96d2007-03-22 12:13:20 -0400510 struct btrfs_root *tree_root = kmalloc(sizeof(struct btrfs_root),
511 GFP_NOFS);
Chris Masone20d96d2007-03-22 12:13:20 -0400512 struct btrfs_fs_info *fs_info = kmalloc(sizeof(*fs_info),
513 GFP_NOFS);
Chris Masoneb60cea2007-02-02 09:18:22 -0500514 int ret;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400515 struct btrfs_super_block *disk_super;
Chris Mason7eccb902007-04-11 15:53:25 -0400516 struct dev_lookup *dev_lookup;
Chris Masoneb60cea2007-02-02 09:18:22 -0500517
Chris Mason8ef97622007-03-26 10:15:30 -0400518 init_bit_radix(&fs_info->pinned_radix);
519 init_bit_radix(&fs_info->pending_del_radix);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400520 INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_NOFS);
Chris Mason7eccb902007-04-11 15:53:25 -0400521 INIT_RADIX_TREE(&fs_info->dev_radix, GFP_NOFS);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400522 sb_set_blocksize(sb, 4096);
Chris Mason9f5fae22007-03-20 14:38:32 -0400523 fs_info->running_transaction = NULL;
Chris Mason9f5fae22007-03-20 14:38:32 -0400524 fs_info->tree_root = tree_root;
525 fs_info->extent_root = extent_root;
Chris Mason0bd93ba2007-04-11 13:57:44 -0400526 fs_info->dev_root = dev_root;
Chris Masone20d96d2007-03-22 12:13:20 -0400527 fs_info->sb = sb;
Chris Masond98237b2007-03-28 13:57:48 -0400528 fs_info->btree_inode = new_inode(sb);
529 fs_info->btree_inode->i_ino = 1;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400530 fs_info->btree_inode->i_nlink = 1;
Chris Masond98237b2007-03-28 13:57:48 -0400531 fs_info->btree_inode->i_size = sb->s_bdev->bd_inode->i_size;
532 fs_info->btree_inode->i_mapping->a_ops = &btree_aops;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400533 BTRFS_I(fs_info->btree_inode)->root = tree_root;
534 memset(&BTRFS_I(fs_info->btree_inode)->location, 0,
535 sizeof(struct btrfs_key));
Chris Mason22b0ebd2007-03-30 08:47:31 -0400536 insert_inode_hash(fs_info->btree_inode);
Chris Masond98237b2007-03-28 13:57:48 -0400537 mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS);
Chris Mason87cbda52007-03-28 19:44:27 -0400538 fs_info->hash_tfm = crypto_alloc_hash("sha256", 0, CRYPTO_ALG_ASYNC);
Chris Mason30ae8462007-03-29 09:59:15 -0400539 spin_lock_init(&fs_info->hash_lock);
Chris Mason30ae8462007-03-29 09:59:15 -0400540 if (!fs_info->hash_tfm || IS_ERR(fs_info->hash_tfm)) {
Chris Mason87cbda52007-03-28 19:44:27 -0400541 printk("failed to allocate sha256 hash\n");
542 return NULL;
543 }
Chris Mason79154b12007-03-22 15:59:16 -0400544 mutex_init(&fs_info->trans_mutex);
Chris Masond561c022007-03-23 19:47:49 -0400545 mutex_init(&fs_info->fs_mutex);
Chris Mason9f5fae22007-03-20 14:38:32 -0400546 memset(&fs_info->current_insert, 0, sizeof(fs_info->current_insert));
547 memset(&fs_info->last_insert, 0, sizeof(fs_info->last_insert));
Chris Mason3768f362007-03-13 16:47:54 -0400548
Chris Mason0bd93ba2007-04-11 13:57:44 -0400549 __setup_root(sb->s_blocksize, dev_root,
550 fs_info, BTRFS_DEV_TREE_OBJECTID);
551
Chris Mason2c90e5d2007-04-02 10:50:19 -0400552 __setup_root(sb->s_blocksize, tree_root,
553 fs_info, BTRFS_ROOT_TREE_OBJECTID);
Chris Mason7eccb902007-04-11 15:53:25 -0400554
555 dev_lookup = kmalloc(sizeof(*dev_lookup), GFP_NOFS);
556 dev_lookup->block_start = 0;
557 dev_lookup->num_blocks = (u32)-2;
558 dev_lookup->bdev = sb->s_bdev;
559 ret = radix_tree_insert(&fs_info->dev_radix, (u32)-2, dev_lookup);
560 BUG_ON(ret);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400561 fs_info->sb_buffer = read_tree_block(tree_root,
562 BTRFS_SUPER_INFO_OFFSET /
563 sb->s_blocksize);
Chris Masond98237b2007-03-28 13:57:48 -0400564
Chris Mason0f7d52f2007-04-09 10:42:37 -0400565 if (!fs_info->sb_buffer)
Chris Masond98237b2007-03-28 13:57:48 -0400566 return NULL;
Chris Masond98237b2007-03-28 13:57:48 -0400567 disk_super = (struct btrfs_super_block *)fs_info->sb_buffer->b_data;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400568 if (!btrfs_super_root(disk_super))
Chris Mason2c90e5d2007-04-02 10:50:19 -0400569 return NULL;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400570
Chris Mason8352d8a2007-04-12 10:43:05 -0400571 i_size_write(fs_info->btree_inode,
572 btrfs_super_total_blocks(disk_super) <<
573 fs_info->btree_inode->i_blkbits);
574
Chris Mason7eccb902007-04-11 15:53:25 -0400575 radix_tree_delete(&fs_info->dev_radix, (u32)-2);
576 dev_lookup->block_start = btrfs_super_device_block_start(disk_super);
577 dev_lookup->num_blocks = btrfs_super_device_num_blocks(disk_super);
578 ret = radix_tree_insert(&fs_info->dev_radix,
579 dev_lookup->block_start +
Chris Mason8352d8a2007-04-12 10:43:05 -0400580 dev_lookup->num_blocks - 1, dev_lookup);
Chris Mason7eccb902007-04-11 15:53:25 -0400581 BUG_ON(ret);
582
Chris Masond98237b2007-03-28 13:57:48 -0400583 fs_info->disk_super = disk_super;
Chris Mason8352d8a2007-04-12 10:43:05 -0400584
Chris Mason0bd93ba2007-04-11 13:57:44 -0400585 dev_root->node = read_tree_block(tree_root,
586 btrfs_super_device_root(disk_super));
Chris Mason8352d8a2007-04-12 10:43:05 -0400587
588 ret = read_device_info(dev_root);
589 BUG_ON(ret);
590
Chris Masone20d96d2007-03-22 12:13:20 -0400591 tree_root->node = read_tree_block(tree_root,
592 btrfs_super_root(disk_super));
Chris Mason3768f362007-03-13 16:47:54 -0400593 BUG_ON(!tree_root->node);
594
Chris Mason2c90e5d2007-04-02 10:50:19 -0400595 mutex_lock(&fs_info->fs_mutex);
596 ret = find_and_setup_root(sb->s_blocksize, tree_root, fs_info,
Chris Masone20d96d2007-03-22 12:13:20 -0400597 BTRFS_EXTENT_TREE_OBJECTID, extent_root);
Chris Mason3768f362007-03-13 16:47:54 -0400598 BUG_ON(ret);
599
Chris Mason0f7d52f2007-04-09 10:42:37 -0400600 fs_info->generation = btrfs_super_generation(disk_super) + 1;
Chris Masond6e4a422007-04-06 15:37:36 -0400601 memset(&fs_info->kobj, 0, sizeof(fs_info->kobj));
602 kobj_set_kset_s(fs_info, btrfs_subsys);
603 kobject_set_name(&fs_info->kobj, "%s", sb->s_id);
604 kobject_register(&fs_info->kobj);
Chris Mason5be6f7f2007-04-05 13:35:25 -0400605 mutex_unlock(&fs_info->fs_mutex);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400606 return tree_root;
Chris Masoneb60cea2007-02-02 09:18:22 -0500607}
608
Chris Masone089f052007-03-16 16:20:31 -0400609int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason79154b12007-03-22 15:59:16 -0400610 *root)
Chris Masoncfaa7292007-02-21 17:04:57 -0500611{
Chris Masond5719762007-03-23 10:01:08 -0400612 struct buffer_head *bh = root->fs_info->sb_buffer;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400613
Chris Masond5719762007-03-23 10:01:08 -0400614 btrfs_set_super_root(root->fs_info->disk_super,
Chris Mason7eccb902007-04-11 15:53:25 -0400615 bh_blocknr(root->fs_info->tree_root->node));
Chris Masond5719762007-03-23 10:01:08 -0400616 lock_buffer(bh);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400617 WARN_ON(atomic_read(&bh->b_count) < 1);
Chris Masond5719762007-03-23 10:01:08 -0400618 clear_buffer_dirty(bh);
Chris Mason87cbda52007-03-28 19:44:27 -0400619 csum_tree_block(root, bh, 0);
Chris Masond5719762007-03-23 10:01:08 -0400620 bh->b_end_io = end_buffer_write_sync;
621 get_bh(bh);
622 submit_bh(WRITE, bh);
623 wait_on_buffer(bh);
624 if (!buffer_uptodate(bh)) {
625 WARN_ON(1);
626 return -EIO;
Chris Masoncfaa7292007-02-21 17:04:57 -0500627 }
628 return 0;
629}
630
Chris Mason2619ba12007-04-10 16:58:11 -0400631static int free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
632{
633 radix_tree_delete(&fs_info->fs_roots_radix,
634 (unsigned long)root->root_key.objectid);
635 if (root->inode)
636 iput(root->inode);
637 if (root->node)
638 brelse(root->node);
639 if (root->commit_root)
640 brelse(root->commit_root);
641 kfree(root);
642 return 0;
643}
644
Chris Mason0f7d52f2007-04-09 10:42:37 -0400645int del_fs_roots(struct btrfs_fs_info *fs_info)
646{
647 int ret;
648 struct btrfs_root *gang[8];
649 int i;
650
651 while(1) {
652 ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix,
653 (void **)gang, 0,
654 ARRAY_SIZE(gang));
655 if (!ret)
656 break;
Chris Mason2619ba12007-04-10 16:58:11 -0400657 for (i = 0; i < ret; i++)
658 free_fs_root(fs_info, gang[i]);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400659 }
660 return 0;
661}
Chris Mason7eccb902007-04-11 15:53:25 -0400662static int free_dev_radix(struct btrfs_fs_info *fs_info)
663{
664 struct dev_lookup *lookup[8];
665 struct block_device *super_bdev = fs_info->sb->s_bdev;
666 int ret;
667 int i;
668 while(1) {
669 ret = radix_tree_gang_lookup(&fs_info->dev_radix,
670 (void **)lookup, 0,
671 ARRAY_SIZE(lookup));
672 if (!ret)
673 break;
674 for (i = 0; i < ret; i++) {
675 if (lookup[i]->bdev != super_bdev)
676 close_bdev_excl(lookup[i]->bdev);
677 radix_tree_delete(&fs_info->dev_radix,
678 lookup[i]->block_start +
Chris Mason8352d8a2007-04-12 10:43:05 -0400679 lookup[i]->num_blocks - 1);
Chris Mason7eccb902007-04-11 15:53:25 -0400680 kfree(lookup[i]);
681 }
682 }
683 return 0;
684}
Chris Mason0f7d52f2007-04-09 10:42:37 -0400685
Chris Masone20d96d2007-03-22 12:13:20 -0400686int close_ctree(struct btrfs_root *root)
Chris Masoneb60cea2007-02-02 09:18:22 -0500687{
Chris Mason3768f362007-03-13 16:47:54 -0400688 int ret;
Chris Masone089f052007-03-16 16:20:31 -0400689 struct btrfs_trans_handle *trans;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400690 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone089f052007-03-16 16:20:31 -0400691
Chris Mason0f7d52f2007-04-09 10:42:37 -0400692 mutex_lock(&fs_info->fs_mutex);
Chris Mason79154b12007-03-22 15:59:16 -0400693 trans = btrfs_start_transaction(root, 1);
694 btrfs_commit_transaction(trans, root);
695 /* run commit again to drop the original snapshot */
696 trans = btrfs_start_transaction(root, 1);
697 btrfs_commit_transaction(trans, root);
698 ret = btrfs_write_and_wait_transaction(NULL, root);
Chris Mason9f5fae22007-03-20 14:38:32 -0400699 BUG_ON(ret);
Chris Mason79154b12007-03-22 15:59:16 -0400700 write_ctree_super(NULL, root);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400701 mutex_unlock(&fs_info->fs_mutex);
Chris Masoned2ff2c2007-03-01 18:59:40 -0500702
Chris Mason0f7d52f2007-04-09 10:42:37 -0400703 if (fs_info->extent_root->node)
704 btrfs_block_release(fs_info->extent_root,
705 fs_info->extent_root->node);
Chris Mason0bd93ba2007-04-11 13:57:44 -0400706 if (fs_info->dev_root->node)
707 btrfs_block_release(fs_info->dev_root,
708 fs_info->dev_root->node);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400709 if (fs_info->tree_root->node)
710 btrfs_block_release(fs_info->tree_root,
711 fs_info->tree_root->node);
712 btrfs_block_release(root, fs_info->sb_buffer);
713 crypto_free_hash(fs_info->hash_tfm);
714 truncate_inode_pages(fs_info->btree_inode->i_mapping, 0);
715 iput(fs_info->btree_inode);
Chris Mason7eccb902007-04-11 15:53:25 -0400716
717 free_dev_radix(fs_info);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400718 del_fs_roots(fs_info);
719 kfree(fs_info->extent_root);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400720 kfree(fs_info->tree_root);
721 kobject_unregister(&fs_info->kobj);
Chris Masoneb60cea2007-02-02 09:18:22 -0500722 return 0;
723}
724
Chris Masone20d96d2007-03-22 12:13:20 -0400725void btrfs_block_release(struct btrfs_root *root, struct buffer_head *buf)
Chris Masoneb60cea2007-02-02 09:18:22 -0500726{
Chris Mason7cfcc172007-04-02 14:53:59 -0400727 brelse(buf);
Chris Masoneb60cea2007-02-02 09:18:22 -0500728}
729