blob: a2a3f529cada902ab00724406854c8b9b5fdb730 [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
111 root = root->fs_info->dev_root;
112 ret = radix_tree_gang_lookup(&root->fs_info->dev_radix,
113 (void **)lookup,
114 (unsigned long)logical,
115 ARRAY_SIZE(lookup));
116 if (ret == 0 || lookup[0]->block_start > logical ||
117 lookup[0]->block_start + lookup[0]->num_blocks <= logical) {
118 ret = -ENOENT;
119 goto out;
120 }
121 bh->b_bdev = lookup[0]->bdev;
122 bh->b_blocknr = logical - lookup[0]->block_start;
Chris Mason7eccb902007-04-11 15:53:25 -0400123 set_buffer_mapped(bh);
124 ret = 0;
125out:
126 return ret;
127}
128
Chris Masond98237b2007-03-28 13:57:48 -0400129struct buffer_head *btrfs_find_create_tree_block(struct btrfs_root *root,
130 u64 blocknr)
Chris Masoneb60cea2007-02-02 09:18:22 -0500131{
Chris Masond98237b2007-03-28 13:57:48 -0400132 struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
133 int blockbits = root->fs_info->sb->s_blocksize_bits;
134 unsigned long index = blocknr >> (PAGE_CACHE_SHIFT - blockbits);
135 struct page *page;
136 struct buffer_head *bh;
137 struct buffer_head *head;
138 struct buffer_head *ret = NULL;
Chris Mason7eccb902007-04-11 15:53:25 -0400139 int err;
Chris Masond98237b2007-03-28 13:57:48 -0400140 u64 first_block = index << (PAGE_CACHE_SHIFT - blockbits);
Chris Mason22b0ebd2007-03-30 08:47:31 -0400141
Chris Masond98237b2007-03-28 13:57:48 -0400142 page = grab_cache_page(mapping, index);
143 if (!page)
144 return NULL;
145
Chris Masond98237b2007-03-28 13:57:48 -0400146 if (!page_has_buffers(page))
147 create_empty_buffers(page, root->fs_info->sb->s_blocksize, 0);
148 head = page_buffers(page);
149 bh = head;
150 do {
151 if (!buffer_mapped(bh)) {
Chris Mason8352d8a2007-04-12 10:43:05 -0400152 err = btrfs_map_bh_to_logical(root, bh, first_block);
Chris Mason7eccb902007-04-11 15:53:25 -0400153 BUG_ON(err);
Chris Masond98237b2007-03-28 13:57:48 -0400154 }
Chris Mason7eccb902007-04-11 15:53:25 -0400155 if (bh_blocknr(bh) == blocknr) {
Chris Masond98237b2007-03-28 13:57:48 -0400156 ret = bh;
157 get_bh(bh);
158 goto out_unlock;
159 }
160 bh = bh->b_this_page;
161 first_block++;
162 } while (bh != head);
163out_unlock:
164 unlock_page(page);
Chris Mason22b0ebd2007-03-30 08:47:31 -0400165 if (ret)
166 touch_buffer(ret);
Chris Masond98237b2007-03-28 13:57:48 -0400167 page_cache_release(page);
168 return ret;
Chris Masone20d96d2007-03-22 12:13:20 -0400169}
Chris Mason123abc82007-03-14 14:14:43 -0400170
Chris Masond98237b2007-03-28 13:57:48 -0400171static int btree_get_block(struct inode *inode, sector_t iblock,
172 struct buffer_head *bh, int create)
173{
Chris Mason7eccb902007-04-11 15:53:25 -0400174 int err;
175 struct btrfs_root *root = BTRFS_I(bh->b_page->mapping->host)->root;
Chris Mason8352d8a2007-04-12 10:43:05 -0400176 err = btrfs_map_bh_to_logical(root, bh, iblock);
Chris Mason7eccb902007-04-11 15:53:25 -0400177 return err;
Chris Masond98237b2007-03-28 13:57:48 -0400178}
179
Chris Masonf254e522007-03-29 15:15:27 -0400180int btrfs_csum_data(struct btrfs_root * root, char *data, size_t len,
181 char *result)
Chris Mason87cbda52007-03-28 19:44:27 -0400182{
Chris Mason87cbda52007-03-28 19:44:27 -0400183 struct scatterlist sg;
184 struct crypto_hash *tfm = root->fs_info->hash_tfm;
185 struct hash_desc desc;
186 int ret;
Chris Mason87cbda52007-03-28 19:44:27 -0400187
188 desc.tfm = tfm;
189 desc.flags = 0;
Chris Masonf254e522007-03-29 15:15:27 -0400190 sg_init_one(&sg, data, len);
Chris Mason87cbda52007-03-28 19:44:27 -0400191 spin_lock(&root->fs_info->hash_lock);
Chris Mason22b0ebd2007-03-30 08:47:31 -0400192 ret = crypto_hash_digest(&desc, &sg, 1, result);
Chris Mason87cbda52007-03-28 19:44:27 -0400193 spin_unlock(&root->fs_info->hash_lock);
194 if (ret) {
195 printk("sha256 digest failed\n");
196 }
Chris Masonf254e522007-03-29 15:15:27 -0400197 return ret;
198}
199static int csum_tree_block(struct btrfs_root *root, struct buffer_head *bh,
200 int verify)
201{
202 char result[BTRFS_CSUM_SIZE];
203 int ret;
204 struct btrfs_node *node;
205
206 ret = btrfs_csum_data(root, bh->b_data + BTRFS_CSUM_SIZE,
207 bh->b_size - BTRFS_CSUM_SIZE, result);
208 if (ret)
209 return ret;
Chris Mason87cbda52007-03-28 19:44:27 -0400210 if (verify) {
Chris Masonf254e522007-03-29 15:15:27 -0400211 if (memcmp(bh->b_data, result, BTRFS_CSUM_SIZE)) {
Chris Mason7eccb902007-04-11 15:53:25 -0400212 printk("checksum verify failed on %Lu\n",
213 bh_blocknr(bh));
Chris Masonf254e522007-03-29 15:15:27 -0400214 return 1;
215 }
216 } else {
217 node = btrfs_buffer_node(bh);
Chris Mason22b0ebd2007-03-30 08:47:31 -0400218 memcpy(node->header.csum, result, BTRFS_CSUM_SIZE);
Chris Masonf254e522007-03-29 15:15:27 -0400219 }
Chris Mason87cbda52007-03-28 19:44:27 -0400220 return 0;
221}
222
Chris Masond98237b2007-03-28 13:57:48 -0400223static int btree_writepage(struct page *page, struct writeback_control *wbc)
224{
Chris Mason87cbda52007-03-28 19:44:27 -0400225 struct buffer_head *bh;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400226 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
Chris Mason87cbda52007-03-28 19:44:27 -0400227 struct buffer_head *head;
Chris Mason87cbda52007-03-28 19:44:27 -0400228 if (!page_has_buffers(page)) {
229 create_empty_buffers(page, root->fs_info->sb->s_blocksize,
230 (1 << BH_Dirty)|(1 << BH_Uptodate));
231 }
232 head = page_buffers(page);
233 bh = head;
234 do {
235 if (buffer_dirty(bh))
236 csum_tree_block(root, bh, 0);
237 bh = bh->b_this_page;
238 } while (bh != head);
Chris Masond98237b2007-03-28 13:57:48 -0400239 return block_write_full_page(page, btree_get_block, wbc);
240}
241
242static int btree_readpage(struct file * file, struct page * page)
243{
244 return block_read_full_page(page, btree_get_block);
245}
246
247static struct address_space_operations btree_aops = {
248 .readpage = btree_readpage,
249 .writepage = btree_writepage,
250 .sync_page = block_sync_page,
251};
252
Chris Masone20d96d2007-03-22 12:13:20 -0400253struct buffer_head *read_tree_block(struct btrfs_root *root, u64 blocknr)
254{
Chris Masond98237b2007-03-28 13:57:48 -0400255 struct buffer_head *bh = NULL;
Chris Masone20d96d2007-03-22 12:13:20 -0400256
Chris Masond98237b2007-03-28 13:57:48 -0400257 bh = btrfs_find_create_tree_block(root, blocknr);
258 if (!bh)
259 return bh;
Chris Mason9d642722007-04-03 11:43:19 -0400260 if (buffer_uptodate(bh))
261 goto uptodate;
Chris Masond98237b2007-03-28 13:57:48 -0400262 lock_buffer(bh);
263 if (!buffer_uptodate(bh)) {
264 get_bh(bh);
265 bh->b_end_io = end_buffer_read_sync;
266 submit_bh(READ, bh);
267 wait_on_buffer(bh);
268 if (!buffer_uptodate(bh))
269 goto fail;
Chris Mason87cbda52007-03-28 19:44:27 -0400270 csum_tree_block(root, bh, 1);
Chris Masond98237b2007-03-28 13:57:48 -0400271 } else {
272 unlock_buffer(bh);
273 }
Chris Mason9d642722007-04-03 11:43:19 -0400274uptodate:
Chris Masond98237b2007-03-28 13:57:48 -0400275 if (check_tree_block(root, bh))
Chris Masoncfaa7292007-02-21 17:04:57 -0500276 BUG();
Chris Masond98237b2007-03-28 13:57:48 -0400277 return bh;
278fail:
279 brelse(bh);
280 return NULL;
Chris Masoneb60cea2007-02-02 09:18:22 -0500281}
282
Chris Masone089f052007-03-16 16:20:31 -0400283int dirty_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Masone20d96d2007-03-22 12:13:20 -0400284 struct buffer_head *buf)
Chris Masoned2ff2c2007-03-01 18:59:40 -0500285{
Chris Masond6025572007-03-30 14:27:56 -0400286 WARN_ON(atomic_read(&buf->b_count) == 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400287 mark_buffer_dirty(buf);
Chris Masoned2ff2c2007-03-01 18:59:40 -0500288 return 0;
289}
290
Chris Masone089f052007-03-16 16:20:31 -0400291int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Masone20d96d2007-03-22 12:13:20 -0400292 struct buffer_head *buf)
Chris Masoned2ff2c2007-03-01 18:59:40 -0500293{
Chris Masond6025572007-03-30 14:27:56 -0400294 WARN_ON(atomic_read(&buf->b_count) == 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400295 clear_buffer_dirty(buf);
Chris Masoned2ff2c2007-03-01 18:59:40 -0500296 return 0;
297}
298
Chris Mason2c90e5d2007-04-02 10:50:19 -0400299static int __setup_root(int blocksize,
Chris Mason9f5fae22007-03-20 14:38:32 -0400300 struct btrfs_root *root,
301 struct btrfs_fs_info *fs_info,
Chris Masone20d96d2007-03-22 12:13:20 -0400302 u64 objectid)
Chris Masond97e63b2007-02-20 16:40:44 -0500303{
Chris Masoncfaa7292007-02-21 17:04:57 -0500304 root->node = NULL;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400305 root->inode = NULL;
Chris Masona28ec192007-03-06 20:08:01 -0500306 root->commit_root = NULL;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400307 root->blocksize = blocksize;
Chris Mason123abc82007-03-14 14:14:43 -0400308 root->ref_cows = 0;
Chris Mason9f5fae22007-03-20 14:38:32 -0400309 root->fs_info = fs_info;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400310 root->objectid = objectid;
311 root->last_trans = 0;
Chris Mason1b05da22007-04-10 12:13:09 -0400312 root->highest_inode = 0;
313 root->last_inode_alloc = 0;
Chris Mason3768f362007-03-13 16:47:54 -0400314 memset(&root->root_key, 0, sizeof(root->root_key));
315 memset(&root->root_item, 0, sizeof(root->root_item));
316 return 0;
317}
318
Chris Mason2c90e5d2007-04-02 10:50:19 -0400319static int find_and_setup_root(int blocksize,
Chris Mason9f5fae22007-03-20 14:38:32 -0400320 struct btrfs_root *tree_root,
321 struct btrfs_fs_info *fs_info,
322 u64 objectid,
Chris Masone20d96d2007-03-22 12:13:20 -0400323 struct btrfs_root *root)
Chris Mason3768f362007-03-13 16:47:54 -0400324{
325 int ret;
326
Chris Mason2c90e5d2007-04-02 10:50:19 -0400327 __setup_root(blocksize, root, fs_info, objectid);
Chris Mason3768f362007-03-13 16:47:54 -0400328 ret = btrfs_find_last_root(tree_root, objectid,
329 &root->root_item, &root->root_key);
330 BUG_ON(ret);
331
332 root->node = read_tree_block(root,
333 btrfs_root_blocknr(&root->root_item));
Chris Mason3768f362007-03-13 16:47:54 -0400334 BUG_ON(!root->node);
Chris Masond97e63b2007-02-20 16:40:44 -0500335 return 0;
336}
337
Chris Mason0f7d52f2007-04-09 10:42:37 -0400338struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
339 struct btrfs_key *location)
340{
341 struct btrfs_root *root;
342 struct btrfs_root *tree_root = fs_info->tree_root;
343 struct btrfs_path *path;
344 struct btrfs_leaf *l;
Chris Mason1b05da22007-04-10 12:13:09 -0400345 u64 highest_inode;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400346 int ret = 0;
347
348printk("read_fs_root looking for %Lu %Lu %u\n", location->objectid, location->offset, location->flags);
Chris Mason2619ba12007-04-10 16:58:11 -0400349 root = radix_tree_lookup(&fs_info->fs_roots_radix,
350 (unsigned long)location->objectid);
351 if (root) {
352printk("found %p in cache\n", root);
353 return root;
354 }
Chris Mason0f7d52f2007-04-09 10:42:37 -0400355 root = kmalloc(sizeof(*root), GFP_NOFS);
356 if (!root) {
357printk("failed1\n");
358 return ERR_PTR(-ENOMEM);
359 }
360 if (location->offset == (u64)-1) {
361 ret = find_and_setup_root(fs_info->sb->s_blocksize,
362 fs_info->tree_root, fs_info,
363 location->objectid, root);
364 if (ret) {
365printk("failed2\n");
366 kfree(root);
367 return ERR_PTR(ret);
368 }
369 goto insert;
370 }
371
372 __setup_root(fs_info->sb->s_blocksize, root, fs_info,
373 location->objectid);
374
375 path = btrfs_alloc_path();
376 BUG_ON(!path);
377 ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
378 if (ret != 0) {
379printk("internal search_slot gives us %d\n", ret);
380 if (ret > 0)
381 ret = -ENOENT;
382 goto out;
383 }
384 l = btrfs_buffer_leaf(path->nodes[0]);
385 memcpy(&root->root_item,
386 btrfs_item_ptr(l, path->slots[0], struct btrfs_root_item),
387 sizeof(root->root_item));
388 memcpy(&root->root_key, location, sizeof(*location));
389 ret = 0;
390out:
391 btrfs_release_path(root, path);
392 btrfs_free_path(path);
393 if (ret) {
394 kfree(root);
395 return ERR_PTR(ret);
396 }
397 root->node = read_tree_block(root,
398 btrfs_root_blocknr(&root->root_item));
399 BUG_ON(!root->node);
400insert:
401printk("inserting %p\n", root);
402 root->ref_cows = 1;
Chris Mason2619ba12007-04-10 16:58:11 -0400403 ret = radix_tree_insert(&fs_info->fs_roots_radix,
404 (unsigned long)root->root_key.objectid,
Chris Mason0f7d52f2007-04-09 10:42:37 -0400405 root);
406 if (ret) {
407printk("radix_tree_insert gives us %d\n", ret);
408 brelse(root->node);
409 kfree(root);
410 return ERR_PTR(ret);
411 }
Chris Mason1b05da22007-04-10 12:13:09 -0400412 ret = btrfs_find_highest_inode(root, &highest_inode);
413 if (ret == 0) {
414 root->highest_inode = highest_inode;
415 root->last_inode_alloc = highest_inode;
416printk("highest inode is %Lu\n", highest_inode);
417 }
Chris Mason0f7d52f2007-04-09 10:42:37 -0400418printk("all worked\n");
419 return root;
420}
421
Chris Masonb4100d62007-04-12 12:14:00 -0400422static int btrfs_open_disk(struct btrfs_root *root, u64 device_id,
423 u64 block_start, u64 num_blocks,
424 char *filename, int name_len)
Chris Mason8352d8a2007-04-12 10:43:05 -0400425{
426 char *null_filename;
427 struct block_device *bdev;
428 int ret;
429
Chris Mason8352d8a2007-04-12 10:43:05 -0400430 null_filename = kmalloc(name_len + 1, GFP_NOFS);
431 if (!null_filename)
432 return -ENOMEM;
433 memcpy(null_filename, filename, name_len);
434 null_filename[name_len] = '\0';
435
436 bdev = open_bdev_excl(null_filename, O_RDWR, root->fs_info->sb);
437 if (IS_ERR(bdev)) {
438 ret = PTR_ERR(bdev);
439 goto out;
440 }
441 set_blocksize(bdev, root->fs_info->sb->s_blocksize);
Chris Masonb4100d62007-04-12 12:14:00 -0400442 ret = btrfs_insert_dev_radix(root, bdev, device_id,
443 block_start, num_blocks);
Chris Mason8352d8a2007-04-12 10:43:05 -0400444 BUG_ON(ret);
445 ret = 0;
446out:
447 kfree(null_filename);
448 return ret;
449}
450
451static int read_device_info(struct btrfs_root *root)
452{
453 struct btrfs_path *path;
454 int ret;
455 struct btrfs_key key;
456 struct btrfs_leaf *leaf;
457 struct btrfs_device_item *dev_item;
458 int nritems;
459 int slot;
460
461 root = root->fs_info->dev_root;
462
463 path = btrfs_alloc_path();
464 if (!path)
465 return -ENOMEM;
466 key.objectid = 0;
467 key.offset = 0;
468 key.flags = 0;
469 btrfs_set_key_type(&key, BTRFS_DEV_ITEM_KEY);
470
471 mutex_lock(&root->fs_info->fs_mutex);
472 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
473 leaf = btrfs_buffer_leaf(path->nodes[0]);
474 nritems = btrfs_header_nritems(&leaf->header);
475 while(1) {
476 slot = path->slots[0];
477 if (slot >= nritems) {
478 ret = btrfs_next_leaf(root, path);
479 if (ret)
480 break;
481 leaf = btrfs_buffer_leaf(path->nodes[0]);
482 nritems = btrfs_header_nritems(&leaf->header);
483 slot = path->slots[0];
484 }
485 btrfs_disk_key_to_cpu(&key, &leaf->items[slot].key);
486 if (btrfs_key_type(&key) != BTRFS_DEV_ITEM_KEY) {
487 path->slots[0]++;
488 continue;
489 }
490 dev_item = btrfs_item_ptr(leaf, slot, struct btrfs_device_item);
491printk("found key %Lu %Lu\n", key.objectid, key.offset);
Chris Masonb4100d62007-04-12 12:14:00 -0400492 if (btrfs_device_id(dev_item) !=
493 btrfs_super_device_id(root->fs_info->disk_super)) {
494 ret = btrfs_open_disk(root, btrfs_device_id(dev_item),
495 key.objectid, key.offset,
496 (char *)(dev_item + 1),
497 btrfs_device_pathlen(dev_item));
498 BUG_ON(ret);
499 }
Chris Mason8352d8a2007-04-12 10:43:05 -0400500 path->slots[0]++;
501 }
502 btrfs_free_path(path);
503 mutex_unlock(&root->fs_info->fs_mutex);
504 return 0;
505}
506
Chris Mason2c90e5d2007-04-02 10:50:19 -0400507struct btrfs_root *open_ctree(struct super_block *sb)
Chris Masoneb60cea2007-02-02 09:18:22 -0500508{
Chris Masone20d96d2007-03-22 12:13:20 -0400509 struct btrfs_root *extent_root = kmalloc(sizeof(struct btrfs_root),
510 GFP_NOFS);
Chris Mason0bd93ba2007-04-11 13:57:44 -0400511 struct btrfs_root *dev_root = kmalloc(sizeof(struct btrfs_root),
512 GFP_NOFS);
Chris Masone20d96d2007-03-22 12:13:20 -0400513 struct btrfs_root *tree_root = kmalloc(sizeof(struct btrfs_root),
514 GFP_NOFS);
Chris Masone20d96d2007-03-22 12:13:20 -0400515 struct btrfs_fs_info *fs_info = kmalloc(sizeof(*fs_info),
516 GFP_NOFS);
Chris Masoneb60cea2007-02-02 09:18:22 -0500517 int ret;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400518 struct btrfs_super_block *disk_super;
Chris Mason7eccb902007-04-11 15:53:25 -0400519 struct dev_lookup *dev_lookup;
Chris Masoneb60cea2007-02-02 09:18:22 -0500520
Chris Mason8ef97622007-03-26 10:15:30 -0400521 init_bit_radix(&fs_info->pinned_radix);
522 init_bit_radix(&fs_info->pending_del_radix);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400523 INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_NOFS);
Chris Mason7eccb902007-04-11 15:53:25 -0400524 INIT_RADIX_TREE(&fs_info->dev_radix, GFP_NOFS);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400525 sb_set_blocksize(sb, 4096);
Chris Mason9f5fae22007-03-20 14:38:32 -0400526 fs_info->running_transaction = NULL;
Chris Mason9f5fae22007-03-20 14:38:32 -0400527 fs_info->tree_root = tree_root;
528 fs_info->extent_root = extent_root;
Chris Mason0bd93ba2007-04-11 13:57:44 -0400529 fs_info->dev_root = dev_root;
Chris Masone20d96d2007-03-22 12:13:20 -0400530 fs_info->sb = sb;
Chris Masond98237b2007-03-28 13:57:48 -0400531 fs_info->btree_inode = new_inode(sb);
532 fs_info->btree_inode->i_ino = 1;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400533 fs_info->btree_inode->i_nlink = 1;
Chris Masond98237b2007-03-28 13:57:48 -0400534 fs_info->btree_inode->i_size = sb->s_bdev->bd_inode->i_size;
535 fs_info->btree_inode->i_mapping->a_ops = &btree_aops;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400536 BTRFS_I(fs_info->btree_inode)->root = tree_root;
537 memset(&BTRFS_I(fs_info->btree_inode)->location, 0,
538 sizeof(struct btrfs_key));
Chris Mason22b0ebd2007-03-30 08:47:31 -0400539 insert_inode_hash(fs_info->btree_inode);
Chris Masond98237b2007-03-28 13:57:48 -0400540 mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS);
Chris Mason87cbda52007-03-28 19:44:27 -0400541 fs_info->hash_tfm = crypto_alloc_hash("sha256", 0, CRYPTO_ALG_ASYNC);
Chris Mason30ae8462007-03-29 09:59:15 -0400542 spin_lock_init(&fs_info->hash_lock);
Chris Mason30ae8462007-03-29 09:59:15 -0400543 if (!fs_info->hash_tfm || IS_ERR(fs_info->hash_tfm)) {
Chris Mason87cbda52007-03-28 19:44:27 -0400544 printk("failed to allocate sha256 hash\n");
545 return NULL;
546 }
Chris Mason79154b12007-03-22 15:59:16 -0400547 mutex_init(&fs_info->trans_mutex);
Chris Masond561c022007-03-23 19:47:49 -0400548 mutex_init(&fs_info->fs_mutex);
Chris Mason9f5fae22007-03-20 14:38:32 -0400549 memset(&fs_info->current_insert, 0, sizeof(fs_info->current_insert));
550 memset(&fs_info->last_insert, 0, sizeof(fs_info->last_insert));
Chris Mason3768f362007-03-13 16:47:54 -0400551
Chris Mason0bd93ba2007-04-11 13:57:44 -0400552 __setup_root(sb->s_blocksize, dev_root,
553 fs_info, BTRFS_DEV_TREE_OBJECTID);
554
Chris Mason2c90e5d2007-04-02 10:50:19 -0400555 __setup_root(sb->s_blocksize, tree_root,
556 fs_info, BTRFS_ROOT_TREE_OBJECTID);
Chris Mason7eccb902007-04-11 15:53:25 -0400557
558 dev_lookup = kmalloc(sizeof(*dev_lookup), GFP_NOFS);
559 dev_lookup->block_start = 0;
560 dev_lookup->num_blocks = (u32)-2;
561 dev_lookup->bdev = sb->s_bdev;
Chris Masonb4100d62007-04-12 12:14:00 -0400562 dev_lookup->device_id = 0;
Chris Mason7eccb902007-04-11 15:53:25 -0400563 ret = radix_tree_insert(&fs_info->dev_radix, (u32)-2, dev_lookup);
564 BUG_ON(ret);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400565 fs_info->sb_buffer = read_tree_block(tree_root,
566 BTRFS_SUPER_INFO_OFFSET /
567 sb->s_blocksize);
Chris Masond98237b2007-03-28 13:57:48 -0400568
Chris Mason0f7d52f2007-04-09 10:42:37 -0400569 if (!fs_info->sb_buffer)
Chris Masond98237b2007-03-28 13:57:48 -0400570 return NULL;
Chris Masond98237b2007-03-28 13:57:48 -0400571 disk_super = (struct btrfs_super_block *)fs_info->sb_buffer->b_data;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400572 if (!btrfs_super_root(disk_super))
Chris Mason2c90e5d2007-04-02 10:50:19 -0400573 return NULL;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400574
Chris Mason8352d8a2007-04-12 10:43:05 -0400575 i_size_write(fs_info->btree_inode,
576 btrfs_super_total_blocks(disk_super) <<
577 fs_info->btree_inode->i_blkbits);
578
Chris Mason7eccb902007-04-11 15:53:25 -0400579 radix_tree_delete(&fs_info->dev_radix, (u32)-2);
580 dev_lookup->block_start = btrfs_super_device_block_start(disk_super);
581 dev_lookup->num_blocks = btrfs_super_device_num_blocks(disk_super);
Chris Masonb4100d62007-04-12 12:14:00 -0400582 dev_lookup->device_id = btrfs_super_device_id(disk_super);
583
Chris Mason7eccb902007-04-11 15:53:25 -0400584 ret = radix_tree_insert(&fs_info->dev_radix,
585 dev_lookup->block_start +
Chris Mason8352d8a2007-04-12 10:43:05 -0400586 dev_lookup->num_blocks - 1, dev_lookup);
Chris Mason7eccb902007-04-11 15:53:25 -0400587 BUG_ON(ret);
588
Chris Masond98237b2007-03-28 13:57:48 -0400589 fs_info->disk_super = disk_super;
Chris Mason8352d8a2007-04-12 10:43:05 -0400590
Chris Mason0bd93ba2007-04-11 13:57:44 -0400591 dev_root->node = read_tree_block(tree_root,
592 btrfs_super_device_root(disk_super));
Chris Mason8352d8a2007-04-12 10:43:05 -0400593
594 ret = read_device_info(dev_root);
595 BUG_ON(ret);
596
Chris Masone20d96d2007-03-22 12:13:20 -0400597 tree_root->node = read_tree_block(tree_root,
598 btrfs_super_root(disk_super));
Chris Mason3768f362007-03-13 16:47:54 -0400599 BUG_ON(!tree_root->node);
600
Chris Mason2c90e5d2007-04-02 10:50:19 -0400601 mutex_lock(&fs_info->fs_mutex);
602 ret = find_and_setup_root(sb->s_blocksize, tree_root, fs_info,
Chris Masone20d96d2007-03-22 12:13:20 -0400603 BTRFS_EXTENT_TREE_OBJECTID, extent_root);
Chris Mason3768f362007-03-13 16:47:54 -0400604 BUG_ON(ret);
605
Chris Mason0f7d52f2007-04-09 10:42:37 -0400606 fs_info->generation = btrfs_super_generation(disk_super) + 1;
Chris Masond6e4a422007-04-06 15:37:36 -0400607 memset(&fs_info->kobj, 0, sizeof(fs_info->kobj));
608 kobj_set_kset_s(fs_info, btrfs_subsys);
609 kobject_set_name(&fs_info->kobj, "%s", sb->s_id);
610 kobject_register(&fs_info->kobj);
Chris Mason5be6f7f2007-04-05 13:35:25 -0400611 mutex_unlock(&fs_info->fs_mutex);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400612 return tree_root;
Chris Masoneb60cea2007-02-02 09:18:22 -0500613}
614
Chris Masone089f052007-03-16 16:20:31 -0400615int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason79154b12007-03-22 15:59:16 -0400616 *root)
Chris Masoncfaa7292007-02-21 17:04:57 -0500617{
Chris Masond5719762007-03-23 10:01:08 -0400618 struct buffer_head *bh = root->fs_info->sb_buffer;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400619
Chris Masond5719762007-03-23 10:01:08 -0400620 btrfs_set_super_root(root->fs_info->disk_super,
Chris Mason7eccb902007-04-11 15:53:25 -0400621 bh_blocknr(root->fs_info->tree_root->node));
Chris Masond5719762007-03-23 10:01:08 -0400622 lock_buffer(bh);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400623 WARN_ON(atomic_read(&bh->b_count) < 1);
Chris Masond5719762007-03-23 10:01:08 -0400624 clear_buffer_dirty(bh);
Chris Mason87cbda52007-03-28 19:44:27 -0400625 csum_tree_block(root, bh, 0);
Chris Masond5719762007-03-23 10:01:08 -0400626 bh->b_end_io = end_buffer_write_sync;
627 get_bh(bh);
628 submit_bh(WRITE, bh);
629 wait_on_buffer(bh);
630 if (!buffer_uptodate(bh)) {
631 WARN_ON(1);
632 return -EIO;
Chris Masoncfaa7292007-02-21 17:04:57 -0500633 }
634 return 0;
635}
636
Chris Mason2619ba12007-04-10 16:58:11 -0400637static int free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
638{
639 radix_tree_delete(&fs_info->fs_roots_radix,
640 (unsigned long)root->root_key.objectid);
641 if (root->inode)
642 iput(root->inode);
643 if (root->node)
644 brelse(root->node);
645 if (root->commit_root)
646 brelse(root->commit_root);
647 kfree(root);
648 return 0;
649}
650
Chris Mason0f7d52f2007-04-09 10:42:37 -0400651int del_fs_roots(struct btrfs_fs_info *fs_info)
652{
653 int ret;
654 struct btrfs_root *gang[8];
655 int i;
656
657 while(1) {
658 ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix,
659 (void **)gang, 0,
660 ARRAY_SIZE(gang));
661 if (!ret)
662 break;
Chris Mason2619ba12007-04-10 16:58:11 -0400663 for (i = 0; i < ret; i++)
664 free_fs_root(fs_info, gang[i]);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400665 }
666 return 0;
667}
Chris Masonb4100d62007-04-12 12:14:00 -0400668
Chris Mason7eccb902007-04-11 15:53:25 -0400669static int free_dev_radix(struct btrfs_fs_info *fs_info)
670{
671 struct dev_lookup *lookup[8];
672 struct block_device *super_bdev = fs_info->sb->s_bdev;
673 int ret;
674 int i;
675 while(1) {
676 ret = radix_tree_gang_lookup(&fs_info->dev_radix,
677 (void **)lookup, 0,
678 ARRAY_SIZE(lookup));
679 if (!ret)
680 break;
681 for (i = 0; i < ret; i++) {
682 if (lookup[i]->bdev != super_bdev)
683 close_bdev_excl(lookup[i]->bdev);
684 radix_tree_delete(&fs_info->dev_radix,
685 lookup[i]->block_start +
Chris Mason8352d8a2007-04-12 10:43:05 -0400686 lookup[i]->num_blocks - 1);
Chris Mason7eccb902007-04-11 15:53:25 -0400687 kfree(lookup[i]);
688 }
689 }
690 return 0;
691}
Chris Mason0f7d52f2007-04-09 10:42:37 -0400692
Chris Masone20d96d2007-03-22 12:13:20 -0400693int close_ctree(struct btrfs_root *root)
Chris Masoneb60cea2007-02-02 09:18:22 -0500694{
Chris Mason3768f362007-03-13 16:47:54 -0400695 int ret;
Chris Masone089f052007-03-16 16:20:31 -0400696 struct btrfs_trans_handle *trans;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400697 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone089f052007-03-16 16:20:31 -0400698
Chris Mason0f7d52f2007-04-09 10:42:37 -0400699 mutex_lock(&fs_info->fs_mutex);
Chris Mason79154b12007-03-22 15:59:16 -0400700 trans = btrfs_start_transaction(root, 1);
701 btrfs_commit_transaction(trans, root);
702 /* run commit again to drop the original snapshot */
703 trans = btrfs_start_transaction(root, 1);
704 btrfs_commit_transaction(trans, root);
705 ret = btrfs_write_and_wait_transaction(NULL, root);
Chris Mason9f5fae22007-03-20 14:38:32 -0400706 BUG_ON(ret);
Chris Mason79154b12007-03-22 15:59:16 -0400707 write_ctree_super(NULL, root);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400708 mutex_unlock(&fs_info->fs_mutex);
Chris Masoned2ff2c2007-03-01 18:59:40 -0500709
Chris Mason0f7d52f2007-04-09 10:42:37 -0400710 if (fs_info->extent_root->node)
711 btrfs_block_release(fs_info->extent_root,
712 fs_info->extent_root->node);
Chris Mason0bd93ba2007-04-11 13:57:44 -0400713 if (fs_info->dev_root->node)
714 btrfs_block_release(fs_info->dev_root,
715 fs_info->dev_root->node);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400716 if (fs_info->tree_root->node)
717 btrfs_block_release(fs_info->tree_root,
718 fs_info->tree_root->node);
719 btrfs_block_release(root, fs_info->sb_buffer);
720 crypto_free_hash(fs_info->hash_tfm);
721 truncate_inode_pages(fs_info->btree_inode->i_mapping, 0);
722 iput(fs_info->btree_inode);
Chris Mason7eccb902007-04-11 15:53:25 -0400723
724 free_dev_radix(fs_info);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400725 del_fs_roots(fs_info);
726 kfree(fs_info->extent_root);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400727 kfree(fs_info->tree_root);
728 kobject_unregister(&fs_info->kobj);
Chris Masoneb60cea2007-02-02 09:18:22 -0500729 return 0;
730}
731
Chris Masone20d96d2007-03-22 12:13:20 -0400732void btrfs_block_release(struct btrfs_root *root, struct buffer_head *buf)
Chris Masoneb60cea2007-02-02 09:18:22 -0500733{
Chris Mason7cfcc172007-04-02 14:53:59 -0400734 brelse(buf);
Chris Masoneb60cea2007-02-02 09:18:22 -0500735}
736