blob: 13046295bf7a0dd7557a8670dd8854fd490caf1f [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;
Chris Mason8352d8a2007-04-12 10:43:05 -040027 int ret;
28
29 lookup = kmalloc(sizeof(*lookup), GFP_NOFS);
30 if (!lookup)
31 return -ENOMEM;
32 lookup->block_start = block_start;
33 lookup->num_blocks = num_blocks;
34 lookup->bdev = bdev;
Chris Masonb4100d62007-04-12 12:14:00 -040035 lookup->device_id = device_id;
Chris Mason8352d8a2007-04-12 10:43:05 -040036
37 ret = radix_tree_insert(&root->fs_info->dev_radix, block_start +
38 num_blocks - 1, lookup);
39 return ret;
40}
41
Chris Mason7eccb902007-04-11 15:53:25 -040042u64 bh_blocknr(struct buffer_head *bh)
43{
44 int blkbits = bh->b_page->mapping->host->i_blkbits;
45 u64 blocknr = bh->b_page->index << (PAGE_CACHE_SHIFT - blkbits);
46 unsigned long offset;
47
48 if (PageHighMem(bh->b_page))
49 offset = (unsigned long)bh->b_data;
50 else
51 offset = bh->b_data - (char *)page_address(bh->b_page);
52 blocknr += offset >> (PAGE_CACHE_SHIFT - blkbits);
53 return blocknr;
54}
55
Chris Masone20d96d2007-03-22 12:13:20 -040056static int check_tree_block(struct btrfs_root *root, struct buffer_head *buf)
Chris Masoneb60cea2007-02-02 09:18:22 -050057{
Chris Masone20d96d2007-03-22 12:13:20 -040058 struct btrfs_node *node = btrfs_buffer_node(buf);
Chris Mason7eccb902007-04-11 15:53:25 -040059 if (bh_blocknr(buf) != btrfs_header_blocknr(&node->header)) {
Chris Mason8352d8a2007-04-12 10:43:05 -040060 printk(KERN_CRIT "bh_blocknr(buf) is %Lu, header is %Lu\n",
61 bh_blocknr(buf), btrfs_header_blocknr(&node->header));
Chris Mason9a8dd152007-02-23 08:38:36 -050062 BUG();
Chris Masond98237b2007-03-28 13:57:48 -040063 }
Chris Mason9a8dd152007-02-23 08:38:36 -050064 return 0;
Chris Masoneb60cea2007-02-02 09:18:22 -050065}
66
Chris Masond98237b2007-03-28 13:57:48 -040067struct buffer_head *btrfs_find_tree_block(struct btrfs_root *root, u64 blocknr)
Chris Masoned2ff2c2007-03-01 18:59:40 -050068{
Chris Masond98237b2007-03-28 13:57:48 -040069 struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
70 int blockbits = root->fs_info->sb->s_blocksize_bits;
71 unsigned long index = blocknr >> (PAGE_CACHE_SHIFT - blockbits);
72 struct page *page;
73 struct buffer_head *bh;
74 struct buffer_head *head;
75 struct buffer_head *ret = NULL;
76
Chris Mason2c90e5d2007-04-02 10:50:19 -040077
Chris Masond98237b2007-03-28 13:57:48 -040078 page = find_lock_page(mapping, index);
79 if (!page)
80 return NULL;
81
82 if (!page_has_buffers(page))
83 goto out_unlock;
84
85 head = page_buffers(page);
86 bh = head;
87 do {
Chris Mason7eccb902007-04-11 15:53:25 -040088 if (buffer_mapped(bh) && bh_blocknr(bh) == blocknr) {
Chris Masond98237b2007-03-28 13:57:48 -040089 ret = bh;
90 get_bh(bh);
91 goto out_unlock;
92 }
93 bh = bh->b_this_page;
94 } while (bh != head);
95out_unlock:
96 unlock_page(page);
Chris Masond6025572007-03-30 14:27:56 -040097 if (ret) {
Chris Mason22b0ebd2007-03-30 08:47:31 -040098 touch_buffer(ret);
Chris Masond6025572007-03-30 14:27:56 -040099 }
Chris Masond98237b2007-03-28 13:57:48 -0400100 page_cache_release(page);
101 return ret;
Chris Masoned2ff2c2007-03-01 18:59:40 -0500102}
103
Chris Mason8352d8a2007-04-12 10:43:05 -0400104int btrfs_map_bh_to_logical(struct btrfs_root *root, struct buffer_head *bh,
Chris Mason7eccb902007-04-11 15:53:25 -0400105 u64 logical)
106{
107 struct dev_lookup *lookup[2];
Chris Mason7eccb902007-04-11 15:53:25 -0400108
109 int ret;
110
Chris Mason236454d2007-04-19 13:37:44 -0400111 if (logical == 0) {
112 bh->b_bdev = NULL;
113 bh->b_blocknr = 0;
114 set_buffer_mapped(bh);
115 return 0;
116 }
Chris Mason7eccb902007-04-11 15:53:25 -0400117 root = root->fs_info->dev_root;
118 ret = radix_tree_gang_lookup(&root->fs_info->dev_radix,
119 (void **)lookup,
120 (unsigned long)logical,
121 ARRAY_SIZE(lookup));
122 if (ret == 0 || lookup[0]->block_start > logical ||
123 lookup[0]->block_start + lookup[0]->num_blocks <= logical) {
124 ret = -ENOENT;
125 goto out;
126 }
127 bh->b_bdev = lookup[0]->bdev;
128 bh->b_blocknr = logical - lookup[0]->block_start;
Chris Mason7eccb902007-04-11 15:53:25 -0400129 set_buffer_mapped(bh);
130 ret = 0;
131out:
132 return ret;
133}
134
Chris Masond98237b2007-03-28 13:57:48 -0400135struct buffer_head *btrfs_find_create_tree_block(struct btrfs_root *root,
136 u64 blocknr)
Chris Masoneb60cea2007-02-02 09:18:22 -0500137{
Chris Masond98237b2007-03-28 13:57:48 -0400138 struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
139 int blockbits = root->fs_info->sb->s_blocksize_bits;
140 unsigned long index = blocknr >> (PAGE_CACHE_SHIFT - blockbits);
141 struct page *page;
142 struct buffer_head *bh;
143 struct buffer_head *head;
144 struct buffer_head *ret = NULL;
Chris Mason7eccb902007-04-11 15:53:25 -0400145 int err;
Chris Masond98237b2007-03-28 13:57:48 -0400146 u64 first_block = index << (PAGE_CACHE_SHIFT - blockbits);
Chris Mason22b0ebd2007-03-30 08:47:31 -0400147
Chris Masond98237b2007-03-28 13:57:48 -0400148 page = grab_cache_page(mapping, index);
149 if (!page)
150 return NULL;
151
Chris Masond98237b2007-03-28 13:57:48 -0400152 if (!page_has_buffers(page))
153 create_empty_buffers(page, root->fs_info->sb->s_blocksize, 0);
154 head = page_buffers(page);
155 bh = head;
156 do {
157 if (!buffer_mapped(bh)) {
Chris Mason8352d8a2007-04-12 10:43:05 -0400158 err = btrfs_map_bh_to_logical(root, bh, first_block);
Chris Mason7eccb902007-04-11 15:53:25 -0400159 BUG_ON(err);
Chris Masond98237b2007-03-28 13:57:48 -0400160 }
Chris Mason7eccb902007-04-11 15:53:25 -0400161 if (bh_blocknr(bh) == blocknr) {
Chris Masond98237b2007-03-28 13:57:48 -0400162 ret = bh;
163 get_bh(bh);
164 goto out_unlock;
165 }
166 bh = bh->b_this_page;
167 first_block++;
168 } while (bh != head);
169out_unlock:
170 unlock_page(page);
Chris Mason22b0ebd2007-03-30 08:47:31 -0400171 if (ret)
172 touch_buffer(ret);
Chris Masond98237b2007-03-28 13:57:48 -0400173 page_cache_release(page);
174 return ret;
Chris Masone20d96d2007-03-22 12:13:20 -0400175}
Chris Mason123abc82007-03-14 14:14:43 -0400176
Chris Masond98237b2007-03-28 13:57:48 -0400177static int btree_get_block(struct inode *inode, sector_t iblock,
178 struct buffer_head *bh, int create)
179{
Chris Mason7eccb902007-04-11 15:53:25 -0400180 int err;
181 struct btrfs_root *root = BTRFS_I(bh->b_page->mapping->host)->root;
Chris Mason8352d8a2007-04-12 10:43:05 -0400182 err = btrfs_map_bh_to_logical(root, bh, iblock);
Chris Mason7eccb902007-04-11 15:53:25 -0400183 return err;
Chris Masond98237b2007-03-28 13:57:48 -0400184}
185
Chris Masonf254e522007-03-29 15:15:27 -0400186int btrfs_csum_data(struct btrfs_root * root, char *data, size_t len,
187 char *result)
Chris Mason87cbda52007-03-28 19:44:27 -0400188{
Chris Mason87cbda52007-03-28 19:44:27 -0400189 struct scatterlist sg;
190 struct crypto_hash *tfm = root->fs_info->hash_tfm;
191 struct hash_desc desc;
192 int ret;
Chris Mason87cbda52007-03-28 19:44:27 -0400193
194 desc.tfm = tfm;
195 desc.flags = 0;
Chris Masonf254e522007-03-29 15:15:27 -0400196 sg_init_one(&sg, data, len);
Chris Mason87cbda52007-03-28 19:44:27 -0400197 spin_lock(&root->fs_info->hash_lock);
Chris Mason22b0ebd2007-03-30 08:47:31 -0400198 ret = crypto_hash_digest(&desc, &sg, 1, result);
Chris Mason87cbda52007-03-28 19:44:27 -0400199 spin_unlock(&root->fs_info->hash_lock);
200 if (ret) {
201 printk("sha256 digest failed\n");
202 }
Chris Masonf254e522007-03-29 15:15:27 -0400203 return ret;
204}
205static int csum_tree_block(struct btrfs_root *root, struct buffer_head *bh,
206 int verify)
207{
208 char result[BTRFS_CSUM_SIZE];
209 int ret;
210 struct btrfs_node *node;
211
212 ret = btrfs_csum_data(root, bh->b_data + BTRFS_CSUM_SIZE,
213 bh->b_size - BTRFS_CSUM_SIZE, result);
214 if (ret)
215 return ret;
Chris Mason87cbda52007-03-28 19:44:27 -0400216 if (verify) {
Chris Masonf254e522007-03-29 15:15:27 -0400217 if (memcmp(bh->b_data, result, BTRFS_CSUM_SIZE)) {
Chris Mason7eccb902007-04-11 15:53:25 -0400218 printk("checksum verify failed on %Lu\n",
219 bh_blocknr(bh));
Chris Masonf254e522007-03-29 15:15:27 -0400220 return 1;
221 }
222 } else {
223 node = btrfs_buffer_node(bh);
Chris Mason22b0ebd2007-03-30 08:47:31 -0400224 memcpy(node->header.csum, result, BTRFS_CSUM_SIZE);
Chris Masonf254e522007-03-29 15:15:27 -0400225 }
Chris Mason87cbda52007-03-28 19:44:27 -0400226 return 0;
227}
228
Chris Masond98237b2007-03-28 13:57:48 -0400229static int btree_writepage(struct page *page, struct writeback_control *wbc)
230{
Chris Mason87cbda52007-03-28 19:44:27 -0400231 struct buffer_head *bh;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400232 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
Chris Mason87cbda52007-03-28 19:44:27 -0400233 struct buffer_head *head;
Chris Mason87cbda52007-03-28 19:44:27 -0400234 if (!page_has_buffers(page)) {
235 create_empty_buffers(page, root->fs_info->sb->s_blocksize,
236 (1 << BH_Dirty)|(1 << BH_Uptodate));
237 }
238 head = page_buffers(page);
239 bh = head;
240 do {
241 if (buffer_dirty(bh))
242 csum_tree_block(root, bh, 0);
243 bh = bh->b_this_page;
244 } while (bh != head);
Chris Masond98237b2007-03-28 13:57:48 -0400245 return block_write_full_page(page, btree_get_block, wbc);
246}
247
248static int btree_readpage(struct file * file, struct page * page)
249{
250 return block_read_full_page(page, btree_get_block);
251}
252
253static struct address_space_operations btree_aops = {
254 .readpage = btree_readpage,
255 .writepage = btree_writepage,
256 .sync_page = block_sync_page,
257};
258
Chris Masone20d96d2007-03-22 12:13:20 -0400259struct buffer_head *read_tree_block(struct btrfs_root *root, u64 blocknr)
260{
Chris Masond98237b2007-03-28 13:57:48 -0400261 struct buffer_head *bh = NULL;
Chris Masone20d96d2007-03-22 12:13:20 -0400262
Chris Masond98237b2007-03-28 13:57:48 -0400263 bh = btrfs_find_create_tree_block(root, blocknr);
264 if (!bh)
265 return bh;
Chris Mason9d642722007-04-03 11:43:19 -0400266 if (buffer_uptodate(bh))
267 goto uptodate;
Chris Masond98237b2007-03-28 13:57:48 -0400268 lock_buffer(bh);
269 if (!buffer_uptodate(bh)) {
270 get_bh(bh);
271 bh->b_end_io = end_buffer_read_sync;
272 submit_bh(READ, bh);
273 wait_on_buffer(bh);
274 if (!buffer_uptodate(bh))
275 goto fail;
Chris Mason87cbda52007-03-28 19:44:27 -0400276 csum_tree_block(root, bh, 1);
Chris Masond98237b2007-03-28 13:57:48 -0400277 } else {
278 unlock_buffer(bh);
279 }
Chris Mason9d642722007-04-03 11:43:19 -0400280uptodate:
Chris Masond98237b2007-03-28 13:57:48 -0400281 if (check_tree_block(root, bh))
Chris Masoncfaa7292007-02-21 17:04:57 -0500282 BUG();
Chris Masond98237b2007-03-28 13:57:48 -0400283 return bh;
284fail:
285 brelse(bh);
286 return NULL;
Chris Masoneb60cea2007-02-02 09:18:22 -0500287}
288
Chris Masone089f052007-03-16 16:20:31 -0400289int dirty_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Masone20d96d2007-03-22 12:13:20 -0400290 struct buffer_head *buf)
Chris Masoned2ff2c2007-03-01 18:59:40 -0500291{
Chris Masond6025572007-03-30 14:27:56 -0400292 WARN_ON(atomic_read(&buf->b_count) == 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400293 mark_buffer_dirty(buf);
Chris Masoned2ff2c2007-03-01 18:59:40 -0500294 return 0;
295}
296
Chris Masone089f052007-03-16 16:20:31 -0400297int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Masone20d96d2007-03-22 12:13:20 -0400298 struct buffer_head *buf)
Chris Masoned2ff2c2007-03-01 18:59:40 -0500299{
Chris Masond6025572007-03-30 14:27:56 -0400300 WARN_ON(atomic_read(&buf->b_count) == 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400301 clear_buffer_dirty(buf);
Chris Masoned2ff2c2007-03-01 18:59:40 -0500302 return 0;
303}
304
Chris Mason2c90e5d2007-04-02 10:50:19 -0400305static int __setup_root(int blocksize,
Chris Mason9f5fae22007-03-20 14:38:32 -0400306 struct btrfs_root *root,
307 struct btrfs_fs_info *fs_info,
Chris Masone20d96d2007-03-22 12:13:20 -0400308 u64 objectid)
Chris Masond97e63b2007-02-20 16:40:44 -0500309{
Chris Masoncfaa7292007-02-21 17:04:57 -0500310 root->node = NULL;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400311 root->inode = NULL;
Chris Masona28ec192007-03-06 20:08:01 -0500312 root->commit_root = NULL;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400313 root->blocksize = blocksize;
Chris Mason123abc82007-03-14 14:14:43 -0400314 root->ref_cows = 0;
Chris Mason9f5fae22007-03-20 14:38:32 -0400315 root->fs_info = fs_info;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400316 root->objectid = objectid;
317 root->last_trans = 0;
Chris Mason1b05da22007-04-10 12:13:09 -0400318 root->highest_inode = 0;
319 root->last_inode_alloc = 0;
Chris Mason3768f362007-03-13 16:47:54 -0400320 memset(&root->root_key, 0, sizeof(root->root_key));
321 memset(&root->root_item, 0, sizeof(root->root_item));
322 return 0;
323}
324
Chris Mason2c90e5d2007-04-02 10:50:19 -0400325static int find_and_setup_root(int blocksize,
Chris Mason9f5fae22007-03-20 14:38:32 -0400326 struct btrfs_root *tree_root,
327 struct btrfs_fs_info *fs_info,
328 u64 objectid,
Chris Masone20d96d2007-03-22 12:13:20 -0400329 struct btrfs_root *root)
Chris Mason3768f362007-03-13 16:47:54 -0400330{
331 int ret;
332
Chris Mason2c90e5d2007-04-02 10:50:19 -0400333 __setup_root(blocksize, root, fs_info, objectid);
Chris Mason3768f362007-03-13 16:47:54 -0400334 ret = btrfs_find_last_root(tree_root, objectid,
335 &root->root_item, &root->root_key);
336 BUG_ON(ret);
337
338 root->node = read_tree_block(root,
339 btrfs_root_blocknr(&root->root_item));
Chris Mason3768f362007-03-13 16:47:54 -0400340 BUG_ON(!root->node);
Chris Masond97e63b2007-02-20 16:40:44 -0500341 return 0;
342}
343
Chris Mason0f7d52f2007-04-09 10:42:37 -0400344struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
345 struct btrfs_key *location)
346{
347 struct btrfs_root *root;
348 struct btrfs_root *tree_root = fs_info->tree_root;
349 struct btrfs_path *path;
350 struct btrfs_leaf *l;
Chris Mason1b05da22007-04-10 12:13:09 -0400351 u64 highest_inode;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400352 int ret = 0;
353
354printk("read_fs_root looking for %Lu %Lu %u\n", location->objectid, location->offset, location->flags);
Chris Mason2619ba12007-04-10 16:58:11 -0400355 root = radix_tree_lookup(&fs_info->fs_roots_radix,
356 (unsigned long)location->objectid);
357 if (root) {
358printk("found %p in cache\n", root);
359 return root;
360 }
Chris Mason0f7d52f2007-04-09 10:42:37 -0400361 root = kmalloc(sizeof(*root), GFP_NOFS);
362 if (!root) {
363printk("failed1\n");
364 return ERR_PTR(-ENOMEM);
365 }
366 if (location->offset == (u64)-1) {
367 ret = find_and_setup_root(fs_info->sb->s_blocksize,
368 fs_info->tree_root, fs_info,
369 location->objectid, root);
370 if (ret) {
371printk("failed2\n");
372 kfree(root);
373 return ERR_PTR(ret);
374 }
375 goto insert;
376 }
377
378 __setup_root(fs_info->sb->s_blocksize, root, fs_info,
379 location->objectid);
380
381 path = btrfs_alloc_path();
382 BUG_ON(!path);
383 ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
384 if (ret != 0) {
385printk("internal search_slot gives us %d\n", ret);
386 if (ret > 0)
387 ret = -ENOENT;
388 goto out;
389 }
390 l = btrfs_buffer_leaf(path->nodes[0]);
391 memcpy(&root->root_item,
392 btrfs_item_ptr(l, path->slots[0], struct btrfs_root_item),
393 sizeof(root->root_item));
394 memcpy(&root->root_key, location, sizeof(*location));
395 ret = 0;
396out:
397 btrfs_release_path(root, path);
398 btrfs_free_path(path);
399 if (ret) {
400 kfree(root);
401 return ERR_PTR(ret);
402 }
403 root->node = read_tree_block(root,
404 btrfs_root_blocknr(&root->root_item));
405 BUG_ON(!root->node);
406insert:
407printk("inserting %p\n", root);
408 root->ref_cows = 1;
Chris Mason2619ba12007-04-10 16:58:11 -0400409 ret = radix_tree_insert(&fs_info->fs_roots_radix,
410 (unsigned long)root->root_key.objectid,
Chris Mason0f7d52f2007-04-09 10:42:37 -0400411 root);
412 if (ret) {
413printk("radix_tree_insert gives us %d\n", ret);
414 brelse(root->node);
415 kfree(root);
416 return ERR_PTR(ret);
417 }
Chris Mason1b05da22007-04-10 12:13:09 -0400418 ret = btrfs_find_highest_inode(root, &highest_inode);
419 if (ret == 0) {
420 root->highest_inode = highest_inode;
421 root->last_inode_alloc = highest_inode;
422printk("highest inode is %Lu\n", highest_inode);
423 }
Chris Mason0f7d52f2007-04-09 10:42:37 -0400424printk("all worked\n");
425 return root;
426}
427
Chris Masonb4100d62007-04-12 12:14:00 -0400428static int btrfs_open_disk(struct btrfs_root *root, u64 device_id,
429 u64 block_start, u64 num_blocks,
430 char *filename, int name_len)
Chris Mason8352d8a2007-04-12 10:43:05 -0400431{
432 char *null_filename;
433 struct block_device *bdev;
434 int ret;
435
Chris Mason8352d8a2007-04-12 10:43:05 -0400436 null_filename = kmalloc(name_len + 1, GFP_NOFS);
437 if (!null_filename)
438 return -ENOMEM;
439 memcpy(null_filename, filename, name_len);
440 null_filename[name_len] = '\0';
441
442 bdev = open_bdev_excl(null_filename, O_RDWR, root->fs_info->sb);
443 if (IS_ERR(bdev)) {
444 ret = PTR_ERR(bdev);
445 goto out;
446 }
447 set_blocksize(bdev, root->fs_info->sb->s_blocksize);
Chris Masonb4100d62007-04-12 12:14:00 -0400448 ret = btrfs_insert_dev_radix(root, bdev, device_id,
449 block_start, num_blocks);
Chris Mason8352d8a2007-04-12 10:43:05 -0400450 BUG_ON(ret);
451 ret = 0;
452out:
453 kfree(null_filename);
454 return ret;
455}
456
457static int read_device_info(struct btrfs_root *root)
458{
459 struct btrfs_path *path;
460 int ret;
461 struct btrfs_key key;
462 struct btrfs_leaf *leaf;
463 struct btrfs_device_item *dev_item;
464 int nritems;
465 int slot;
466
467 root = root->fs_info->dev_root;
468
469 path = btrfs_alloc_path();
470 if (!path)
471 return -ENOMEM;
472 key.objectid = 0;
473 key.offset = 0;
474 key.flags = 0;
475 btrfs_set_key_type(&key, BTRFS_DEV_ITEM_KEY);
476
477 mutex_lock(&root->fs_info->fs_mutex);
478 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
479 leaf = btrfs_buffer_leaf(path->nodes[0]);
480 nritems = btrfs_header_nritems(&leaf->header);
481 while(1) {
482 slot = path->slots[0];
483 if (slot >= nritems) {
484 ret = btrfs_next_leaf(root, path);
485 if (ret)
486 break;
487 leaf = btrfs_buffer_leaf(path->nodes[0]);
488 nritems = btrfs_header_nritems(&leaf->header);
489 slot = path->slots[0];
490 }
491 btrfs_disk_key_to_cpu(&key, &leaf->items[slot].key);
492 if (btrfs_key_type(&key) != BTRFS_DEV_ITEM_KEY) {
493 path->slots[0]++;
494 continue;
495 }
496 dev_item = btrfs_item_ptr(leaf, slot, struct btrfs_device_item);
497printk("found key %Lu %Lu\n", key.objectid, key.offset);
Chris Masonb4100d62007-04-12 12:14:00 -0400498 if (btrfs_device_id(dev_item) !=
499 btrfs_super_device_id(root->fs_info->disk_super)) {
500 ret = btrfs_open_disk(root, btrfs_device_id(dev_item),
501 key.objectid, key.offset,
502 (char *)(dev_item + 1),
503 btrfs_device_pathlen(dev_item));
504 BUG_ON(ret);
505 }
Chris Mason8352d8a2007-04-12 10:43:05 -0400506 path->slots[0]++;
507 }
508 btrfs_free_path(path);
509 mutex_unlock(&root->fs_info->fs_mutex);
510 return 0;
511}
512
Chris Mason2c90e5d2007-04-02 10:50:19 -0400513struct btrfs_root *open_ctree(struct super_block *sb)
Chris Masoneb60cea2007-02-02 09:18:22 -0500514{
Chris Masone20d96d2007-03-22 12:13:20 -0400515 struct btrfs_root *extent_root = kmalloc(sizeof(struct btrfs_root),
516 GFP_NOFS);
Chris Mason0bd93ba2007-04-11 13:57:44 -0400517 struct btrfs_root *dev_root = kmalloc(sizeof(struct btrfs_root),
518 GFP_NOFS);
Chris Masone20d96d2007-03-22 12:13:20 -0400519 struct btrfs_root *tree_root = kmalloc(sizeof(struct btrfs_root),
520 GFP_NOFS);
Chris Masone20d96d2007-03-22 12:13:20 -0400521 struct btrfs_fs_info *fs_info = kmalloc(sizeof(*fs_info),
522 GFP_NOFS);
Chris Masoneb60cea2007-02-02 09:18:22 -0500523 int ret;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400524 struct btrfs_super_block *disk_super;
Chris Mason7eccb902007-04-11 15:53:25 -0400525 struct dev_lookup *dev_lookup;
Chris Masoneb60cea2007-02-02 09:18:22 -0500526
Chris Mason8ef97622007-03-26 10:15:30 -0400527 init_bit_radix(&fs_info->pinned_radix);
528 init_bit_radix(&fs_info->pending_del_radix);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400529 INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_NOFS);
Chris Mason7eccb902007-04-11 15:53:25 -0400530 INIT_RADIX_TREE(&fs_info->dev_radix, GFP_NOFS);
Chris Mason8fd17792007-04-19 21:01:03 -0400531 INIT_LIST_HEAD(&fs_info->trans_list);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400532 sb_set_blocksize(sb, 4096);
Chris Mason9f5fae22007-03-20 14:38:32 -0400533 fs_info->running_transaction = NULL;
Chris Mason9f5fae22007-03-20 14:38:32 -0400534 fs_info->tree_root = tree_root;
535 fs_info->extent_root = extent_root;
Chris Mason0bd93ba2007-04-11 13:57:44 -0400536 fs_info->dev_root = dev_root;
Chris Masone20d96d2007-03-22 12:13:20 -0400537 fs_info->sb = sb;
Chris Masond98237b2007-03-28 13:57:48 -0400538 fs_info->btree_inode = new_inode(sb);
539 fs_info->btree_inode->i_ino = 1;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400540 fs_info->btree_inode->i_nlink = 1;
Chris Masond98237b2007-03-28 13:57:48 -0400541 fs_info->btree_inode->i_size = sb->s_bdev->bd_inode->i_size;
542 fs_info->btree_inode->i_mapping->a_ops = &btree_aops;
Chris Masone66f7092007-04-20 13:16:02 -0400543 fs_info->do_barriers = 1;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400544 BTRFS_I(fs_info->btree_inode)->root = tree_root;
545 memset(&BTRFS_I(fs_info->btree_inode)->location, 0,
546 sizeof(struct btrfs_key));
Chris Mason22b0ebd2007-03-30 08:47:31 -0400547 insert_inode_hash(fs_info->btree_inode);
Chris Masond98237b2007-03-28 13:57:48 -0400548 mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS);
Chris Mason87cbda52007-03-28 19:44:27 -0400549 fs_info->hash_tfm = crypto_alloc_hash("sha256", 0, CRYPTO_ALG_ASYNC);
Chris Mason30ae8462007-03-29 09:59:15 -0400550 spin_lock_init(&fs_info->hash_lock);
Chris Mason30ae8462007-03-29 09:59:15 -0400551 if (!fs_info->hash_tfm || IS_ERR(fs_info->hash_tfm)) {
Chris Mason87cbda52007-03-28 19:44:27 -0400552 printk("failed to allocate sha256 hash\n");
553 return NULL;
554 }
Chris Mason79154b12007-03-22 15:59:16 -0400555 mutex_init(&fs_info->trans_mutex);
Chris Masond561c022007-03-23 19:47:49 -0400556 mutex_init(&fs_info->fs_mutex);
Chris Mason9f5fae22007-03-20 14:38:32 -0400557 memset(&fs_info->current_insert, 0, sizeof(fs_info->current_insert));
558 memset(&fs_info->last_insert, 0, sizeof(fs_info->last_insert));
Chris Mason3768f362007-03-13 16:47:54 -0400559
Chris Mason0bd93ba2007-04-11 13:57:44 -0400560 __setup_root(sb->s_blocksize, dev_root,
561 fs_info, BTRFS_DEV_TREE_OBJECTID);
562
Chris Mason2c90e5d2007-04-02 10:50:19 -0400563 __setup_root(sb->s_blocksize, tree_root,
564 fs_info, BTRFS_ROOT_TREE_OBJECTID);
Chris Mason7eccb902007-04-11 15:53:25 -0400565
566 dev_lookup = kmalloc(sizeof(*dev_lookup), GFP_NOFS);
567 dev_lookup->block_start = 0;
568 dev_lookup->num_blocks = (u32)-2;
569 dev_lookup->bdev = sb->s_bdev;
Chris Masonb4100d62007-04-12 12:14:00 -0400570 dev_lookup->device_id = 0;
Chris Mason7eccb902007-04-11 15:53:25 -0400571 ret = radix_tree_insert(&fs_info->dev_radix, (u32)-2, dev_lookup);
572 BUG_ON(ret);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400573 fs_info->sb_buffer = read_tree_block(tree_root,
574 BTRFS_SUPER_INFO_OFFSET /
575 sb->s_blocksize);
Chris Masond98237b2007-03-28 13:57:48 -0400576
Chris Mason0f7d52f2007-04-09 10:42:37 -0400577 if (!fs_info->sb_buffer)
Chris Masond98237b2007-03-28 13:57:48 -0400578 return NULL;
Chris Masond98237b2007-03-28 13:57:48 -0400579 disk_super = (struct btrfs_super_block *)fs_info->sb_buffer->b_data;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400580 if (!btrfs_super_root(disk_super))
Chris Mason2c90e5d2007-04-02 10:50:19 -0400581 return NULL;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400582
Chris Mason8352d8a2007-04-12 10:43:05 -0400583 i_size_write(fs_info->btree_inode,
584 btrfs_super_total_blocks(disk_super) <<
585 fs_info->btree_inode->i_blkbits);
586
Chris Mason7eccb902007-04-11 15:53:25 -0400587 radix_tree_delete(&fs_info->dev_radix, (u32)-2);
588 dev_lookup->block_start = btrfs_super_device_block_start(disk_super);
589 dev_lookup->num_blocks = btrfs_super_device_num_blocks(disk_super);
Chris Masonb4100d62007-04-12 12:14:00 -0400590 dev_lookup->device_id = btrfs_super_device_id(disk_super);
591
Chris Mason7eccb902007-04-11 15:53:25 -0400592 ret = radix_tree_insert(&fs_info->dev_radix,
593 dev_lookup->block_start +
Chris Mason8352d8a2007-04-12 10:43:05 -0400594 dev_lookup->num_blocks - 1, dev_lookup);
Chris Mason7eccb902007-04-11 15:53:25 -0400595 BUG_ON(ret);
596
Chris Masond98237b2007-03-28 13:57:48 -0400597 fs_info->disk_super = disk_super;
Chris Mason8352d8a2007-04-12 10:43:05 -0400598
Chris Mason0bd93ba2007-04-11 13:57:44 -0400599 dev_root->node = read_tree_block(tree_root,
600 btrfs_super_device_root(disk_super));
Chris Mason8352d8a2007-04-12 10:43:05 -0400601
602 ret = read_device_info(dev_root);
603 BUG_ON(ret);
604
Chris Masone20d96d2007-03-22 12:13:20 -0400605 tree_root->node = read_tree_block(tree_root,
606 btrfs_super_root(disk_super));
Chris Mason3768f362007-03-13 16:47:54 -0400607 BUG_ON(!tree_root->node);
608
Chris Mason2c90e5d2007-04-02 10:50:19 -0400609 mutex_lock(&fs_info->fs_mutex);
610 ret = find_and_setup_root(sb->s_blocksize, tree_root, fs_info,
Chris Masone20d96d2007-03-22 12:13:20 -0400611 BTRFS_EXTENT_TREE_OBJECTID, extent_root);
Chris Mason3768f362007-03-13 16:47:54 -0400612 BUG_ON(ret);
613
Chris Mason0f7d52f2007-04-09 10:42:37 -0400614 fs_info->generation = btrfs_super_generation(disk_super) + 1;
Chris Masond6e4a422007-04-06 15:37:36 -0400615 memset(&fs_info->kobj, 0, sizeof(fs_info->kobj));
616 kobj_set_kset_s(fs_info, btrfs_subsys);
617 kobject_set_name(&fs_info->kobj, "%s", sb->s_id);
618 kobject_register(&fs_info->kobj);
Chris Mason5be6f7f2007-04-05 13:35:25 -0400619 mutex_unlock(&fs_info->fs_mutex);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400620 return tree_root;
Chris Masoneb60cea2007-02-02 09:18:22 -0500621}
622
Chris Masone089f052007-03-16 16:20:31 -0400623int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason79154b12007-03-22 15:59:16 -0400624 *root)
Chris Masoncfaa7292007-02-21 17:04:57 -0500625{
Chris Masone66f7092007-04-20 13:16:02 -0400626 int ret;
Chris Masond5719762007-03-23 10:01:08 -0400627 struct buffer_head *bh = root->fs_info->sb_buffer;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400628
Chris Masond5719762007-03-23 10:01:08 -0400629 btrfs_set_super_root(root->fs_info->disk_super,
Chris Mason7eccb902007-04-11 15:53:25 -0400630 bh_blocknr(root->fs_info->tree_root->node));
Chris Masond5719762007-03-23 10:01:08 -0400631 lock_buffer(bh);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400632 WARN_ON(atomic_read(&bh->b_count) < 1);
Chris Masond5719762007-03-23 10:01:08 -0400633 clear_buffer_dirty(bh);
Chris Mason87cbda52007-03-28 19:44:27 -0400634 csum_tree_block(root, bh, 0);
Chris Masond5719762007-03-23 10:01:08 -0400635 bh->b_end_io = end_buffer_write_sync;
636 get_bh(bh);
Chris Masone66f7092007-04-20 13:16:02 -0400637 if (root->fs_info->do_barriers)
638 ret = submit_bh(WRITE_BARRIER, bh);
639 else
640 ret = submit_bh(WRITE, bh);
641 if (ret == -EOPNOTSUPP) {
642 set_buffer_uptodate(bh);
643 root->fs_info->do_barriers = 0;
644 ret = submit_bh(WRITE, bh);
645 }
Chris Masond5719762007-03-23 10:01:08 -0400646 wait_on_buffer(bh);
647 if (!buffer_uptodate(bh)) {
648 WARN_ON(1);
649 return -EIO;
Chris Masoncfaa7292007-02-21 17:04:57 -0500650 }
651 return 0;
652}
653
Chris Mason2619ba12007-04-10 16:58:11 -0400654static int free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
655{
656 radix_tree_delete(&fs_info->fs_roots_radix,
657 (unsigned long)root->root_key.objectid);
658 if (root->inode)
659 iput(root->inode);
660 if (root->node)
661 brelse(root->node);
662 if (root->commit_root)
663 brelse(root->commit_root);
664 kfree(root);
665 return 0;
666}
667
Chris Mason0f7d52f2007-04-09 10:42:37 -0400668int del_fs_roots(struct btrfs_fs_info *fs_info)
669{
670 int ret;
671 struct btrfs_root *gang[8];
672 int i;
673
674 while(1) {
675 ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix,
676 (void **)gang, 0,
677 ARRAY_SIZE(gang));
678 if (!ret)
679 break;
Chris Mason2619ba12007-04-10 16:58:11 -0400680 for (i = 0; i < ret; i++)
681 free_fs_root(fs_info, gang[i]);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400682 }
683 return 0;
684}
Chris Masonb4100d62007-04-12 12:14:00 -0400685
Chris Mason7eccb902007-04-11 15:53:25 -0400686static int free_dev_radix(struct btrfs_fs_info *fs_info)
687{
688 struct dev_lookup *lookup[8];
689 struct block_device *super_bdev = fs_info->sb->s_bdev;
690 int ret;
691 int i;
692 while(1) {
693 ret = radix_tree_gang_lookup(&fs_info->dev_radix,
694 (void **)lookup, 0,
695 ARRAY_SIZE(lookup));
696 if (!ret)
697 break;
698 for (i = 0; i < ret; i++) {
699 if (lookup[i]->bdev != super_bdev)
700 close_bdev_excl(lookup[i]->bdev);
701 radix_tree_delete(&fs_info->dev_radix,
702 lookup[i]->block_start +
Chris Mason8352d8a2007-04-12 10:43:05 -0400703 lookup[i]->num_blocks - 1);
Chris Mason7eccb902007-04-11 15:53:25 -0400704 kfree(lookup[i]);
705 }
706 }
707 return 0;
708}
Chris Mason0f7d52f2007-04-09 10:42:37 -0400709
Chris Masone20d96d2007-03-22 12:13:20 -0400710int close_ctree(struct btrfs_root *root)
Chris Masoneb60cea2007-02-02 09:18:22 -0500711{
Chris Mason3768f362007-03-13 16:47:54 -0400712 int ret;
Chris Masone089f052007-03-16 16:20:31 -0400713 struct btrfs_trans_handle *trans;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400714 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone089f052007-03-16 16:20:31 -0400715
Chris Mason0f7d52f2007-04-09 10:42:37 -0400716 mutex_lock(&fs_info->fs_mutex);
Chris Mason79154b12007-03-22 15:59:16 -0400717 trans = btrfs_start_transaction(root, 1);
718 btrfs_commit_transaction(trans, root);
719 /* run commit again to drop the original snapshot */
720 trans = btrfs_start_transaction(root, 1);
721 btrfs_commit_transaction(trans, root);
722 ret = btrfs_write_and_wait_transaction(NULL, root);
Chris Mason9f5fae22007-03-20 14:38:32 -0400723 BUG_ON(ret);
Chris Mason79154b12007-03-22 15:59:16 -0400724 write_ctree_super(NULL, root);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400725 mutex_unlock(&fs_info->fs_mutex);
Chris Masoned2ff2c2007-03-01 18:59:40 -0500726
Chris Mason0f7d52f2007-04-09 10:42:37 -0400727 if (fs_info->extent_root->node)
728 btrfs_block_release(fs_info->extent_root,
729 fs_info->extent_root->node);
Chris Mason0bd93ba2007-04-11 13:57:44 -0400730 if (fs_info->dev_root->node)
731 btrfs_block_release(fs_info->dev_root,
732 fs_info->dev_root->node);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400733 if (fs_info->tree_root->node)
734 btrfs_block_release(fs_info->tree_root,
735 fs_info->tree_root->node);
736 btrfs_block_release(root, fs_info->sb_buffer);
737 crypto_free_hash(fs_info->hash_tfm);
738 truncate_inode_pages(fs_info->btree_inode->i_mapping, 0);
739 iput(fs_info->btree_inode);
Chris Mason7eccb902007-04-11 15:53:25 -0400740
741 free_dev_radix(fs_info);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400742 del_fs_roots(fs_info);
743 kfree(fs_info->extent_root);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400744 kfree(fs_info->tree_root);
745 kobject_unregister(&fs_info->kobj);
Chris Masoneb60cea2007-02-02 09:18:22 -0500746 return 0;
747}
748
Chris Masone20d96d2007-03-22 12:13:20 -0400749void btrfs_block_release(struct btrfs_root *root, struct buffer_head *buf)
Chris Masoneb60cea2007-02-02 09:18:22 -0500750{
Chris Mason7cfcc172007-04-02 14:53:59 -0400751 brelse(buf);
Chris Masoneb60cea2007-02-02 09:18:22 -0500752}
753