blob: c872a7e67abd8e06adcd5fa8e10af1579c0914a0 [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
19u64 bh_blocknr(struct buffer_head *bh)
20{
21 int blkbits = bh->b_page->mapping->host->i_blkbits;
22 u64 blocknr = bh->b_page->index << (PAGE_CACHE_SHIFT - blkbits);
23 unsigned long offset;
24
25 if (PageHighMem(bh->b_page))
26 offset = (unsigned long)bh->b_data;
27 else
28 offset = bh->b_data - (char *)page_address(bh->b_page);
29 blocknr += offset >> (PAGE_CACHE_SHIFT - blkbits);
30 return blocknr;
31}
32
Chris Masone20d96d2007-03-22 12:13:20 -040033static int check_tree_block(struct btrfs_root *root, struct buffer_head *buf)
Chris Masoneb60cea2007-02-02 09:18:22 -050034{
Chris Masone20d96d2007-03-22 12:13:20 -040035 struct btrfs_node *node = btrfs_buffer_node(buf);
Chris Mason7eccb902007-04-11 15:53:25 -040036 if (bh_blocknr(buf) != btrfs_header_blocknr(&node->header)) {
Chris Mason9a8dd152007-02-23 08:38:36 -050037 BUG();
Chris Masond98237b2007-03-28 13:57:48 -040038 }
Chris Mason9a8dd152007-02-23 08:38:36 -050039 return 0;
Chris Masoneb60cea2007-02-02 09:18:22 -050040}
41
Chris Masond98237b2007-03-28 13:57:48 -040042struct buffer_head *btrfs_find_tree_block(struct btrfs_root *root, u64 blocknr)
Chris Masoned2ff2c2007-03-01 18:59:40 -050043{
Chris Masond98237b2007-03-28 13:57:48 -040044 struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
45 int blockbits = root->fs_info->sb->s_blocksize_bits;
46 unsigned long index = blocknr >> (PAGE_CACHE_SHIFT - blockbits);
47 struct page *page;
48 struct buffer_head *bh;
49 struct buffer_head *head;
50 struct buffer_head *ret = NULL;
51
Chris Mason2c90e5d2007-04-02 10:50:19 -040052
Chris Masond98237b2007-03-28 13:57:48 -040053 page = find_lock_page(mapping, index);
54 if (!page)
55 return NULL;
56
57 if (!page_has_buffers(page))
58 goto out_unlock;
59
60 head = page_buffers(page);
61 bh = head;
62 do {
Chris Mason7eccb902007-04-11 15:53:25 -040063 if (buffer_mapped(bh) && bh_blocknr(bh) == blocknr) {
Chris Masond98237b2007-03-28 13:57:48 -040064 ret = bh;
65 get_bh(bh);
66 goto out_unlock;
67 }
68 bh = bh->b_this_page;
69 } while (bh != head);
70out_unlock:
71 unlock_page(page);
Chris Masond6025572007-03-30 14:27:56 -040072 if (ret) {
Chris Mason22b0ebd2007-03-30 08:47:31 -040073 touch_buffer(ret);
Chris Masond6025572007-03-30 14:27:56 -040074 }
Chris Masond98237b2007-03-28 13:57:48 -040075 page_cache_release(page);
76 return ret;
Chris Masoned2ff2c2007-03-01 18:59:40 -050077}
78
Chris Mason7eccb902007-04-11 15:53:25 -040079static int map_bh_to_logical(struct btrfs_root *root, struct buffer_head *bh,
80 u64 logical)
81{
82 struct dev_lookup *lookup[2];
83 char b[BDEVNAME_SIZE];
84
85 int ret;
86
87 root = root->fs_info->dev_root;
88 ret = radix_tree_gang_lookup(&root->fs_info->dev_radix,
89 (void **)lookup,
90 (unsigned long)logical,
91 ARRAY_SIZE(lookup));
92 if (ret == 0 || lookup[0]->block_start > logical ||
93 lookup[0]->block_start + lookup[0]->num_blocks <= logical) {
94 ret = -ENOENT;
95 goto out;
96 }
97 bh->b_bdev = lookup[0]->bdev;
98 bh->b_blocknr = logical - lookup[0]->block_start;
99printk("logical mapping %Lu to %lu bdev %s\n", logical, bh->b_blocknr, bdevname(bh->b_bdev, b));
100 set_buffer_mapped(bh);
101 ret = 0;
102out:
103 return ret;
104}
105
Chris Masond98237b2007-03-28 13:57:48 -0400106struct buffer_head *btrfs_find_create_tree_block(struct btrfs_root *root,
107 u64 blocknr)
Chris Masoneb60cea2007-02-02 09:18:22 -0500108{
Chris Masond98237b2007-03-28 13:57:48 -0400109 struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
110 int blockbits = root->fs_info->sb->s_blocksize_bits;
111 unsigned long index = blocknr >> (PAGE_CACHE_SHIFT - blockbits);
112 struct page *page;
113 struct buffer_head *bh;
114 struct buffer_head *head;
115 struct buffer_head *ret = NULL;
Chris Mason7eccb902007-04-11 15:53:25 -0400116 int err;
Chris Masond98237b2007-03-28 13:57:48 -0400117 u64 first_block = index << (PAGE_CACHE_SHIFT - blockbits);
Chris Mason22b0ebd2007-03-30 08:47:31 -0400118
Chris Masond98237b2007-03-28 13:57:48 -0400119 page = grab_cache_page(mapping, index);
120 if (!page)
121 return NULL;
122
Chris Masond98237b2007-03-28 13:57:48 -0400123 if (!page_has_buffers(page))
124 create_empty_buffers(page, root->fs_info->sb->s_blocksize, 0);
125 head = page_buffers(page);
126 bh = head;
127 do {
128 if (!buffer_mapped(bh)) {
Chris Mason7eccb902007-04-11 15:53:25 -0400129 err = map_bh_to_logical(root, bh, first_block);
130 BUG_ON(err);
Chris Masond98237b2007-03-28 13:57:48 -0400131 }
Chris Mason7eccb902007-04-11 15:53:25 -0400132 if (bh_blocknr(bh) == blocknr) {
Chris Masond98237b2007-03-28 13:57:48 -0400133 ret = bh;
134 get_bh(bh);
135 goto out_unlock;
136 }
137 bh = bh->b_this_page;
138 first_block++;
139 } while (bh != head);
140out_unlock:
141 unlock_page(page);
Chris Mason22b0ebd2007-03-30 08:47:31 -0400142 if (ret)
143 touch_buffer(ret);
Chris Masond98237b2007-03-28 13:57:48 -0400144 page_cache_release(page);
145 return ret;
Chris Masone20d96d2007-03-22 12:13:20 -0400146}
Chris Mason123abc82007-03-14 14:14:43 -0400147
Chris Masond98237b2007-03-28 13:57:48 -0400148static int btree_get_block(struct inode *inode, sector_t iblock,
149 struct buffer_head *bh, int create)
150{
Chris Mason7eccb902007-04-11 15:53:25 -0400151 int err;
152 struct btrfs_root *root = BTRFS_I(bh->b_page->mapping->host)->root;
153 err = map_bh_to_logical(root, bh, iblock);
154 return err;
Chris Masond98237b2007-03-28 13:57:48 -0400155}
156
Chris Masonf254e522007-03-29 15:15:27 -0400157int btrfs_csum_data(struct btrfs_root * root, char *data, size_t len,
158 char *result)
Chris Mason87cbda52007-03-28 19:44:27 -0400159{
Chris Mason87cbda52007-03-28 19:44:27 -0400160 struct scatterlist sg;
161 struct crypto_hash *tfm = root->fs_info->hash_tfm;
162 struct hash_desc desc;
163 int ret;
Chris Mason87cbda52007-03-28 19:44:27 -0400164
165 desc.tfm = tfm;
166 desc.flags = 0;
Chris Masonf254e522007-03-29 15:15:27 -0400167 sg_init_one(&sg, data, len);
Chris Mason87cbda52007-03-28 19:44:27 -0400168 spin_lock(&root->fs_info->hash_lock);
Chris Mason22b0ebd2007-03-30 08:47:31 -0400169 ret = crypto_hash_digest(&desc, &sg, 1, result);
Chris Mason87cbda52007-03-28 19:44:27 -0400170 spin_unlock(&root->fs_info->hash_lock);
171 if (ret) {
172 printk("sha256 digest failed\n");
173 }
Chris Masonf254e522007-03-29 15:15:27 -0400174 return ret;
175}
176static int csum_tree_block(struct btrfs_root *root, struct buffer_head *bh,
177 int verify)
178{
179 char result[BTRFS_CSUM_SIZE];
180 int ret;
181 struct btrfs_node *node;
182
183 ret = btrfs_csum_data(root, bh->b_data + BTRFS_CSUM_SIZE,
184 bh->b_size - BTRFS_CSUM_SIZE, result);
185 if (ret)
186 return ret;
Chris Mason87cbda52007-03-28 19:44:27 -0400187 if (verify) {
Chris Masonf254e522007-03-29 15:15:27 -0400188 if (memcmp(bh->b_data, result, BTRFS_CSUM_SIZE)) {
Chris Mason7eccb902007-04-11 15:53:25 -0400189 printk("checksum verify failed on %Lu\n",
190 bh_blocknr(bh));
Chris Masonf254e522007-03-29 15:15:27 -0400191 return 1;
192 }
193 } else {
194 node = btrfs_buffer_node(bh);
Chris Mason22b0ebd2007-03-30 08:47:31 -0400195 memcpy(node->header.csum, result, BTRFS_CSUM_SIZE);
Chris Masonf254e522007-03-29 15:15:27 -0400196 }
Chris Mason87cbda52007-03-28 19:44:27 -0400197 return 0;
198}
199
Chris Masond98237b2007-03-28 13:57:48 -0400200static int btree_writepage(struct page *page, struct writeback_control *wbc)
201{
Chris Mason87cbda52007-03-28 19:44:27 -0400202 struct buffer_head *bh;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400203 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
Chris Mason87cbda52007-03-28 19:44:27 -0400204 struct buffer_head *head;
Chris Mason87cbda52007-03-28 19:44:27 -0400205 if (!page_has_buffers(page)) {
206 create_empty_buffers(page, root->fs_info->sb->s_blocksize,
207 (1 << BH_Dirty)|(1 << BH_Uptodate));
208 }
209 head = page_buffers(page);
210 bh = head;
211 do {
212 if (buffer_dirty(bh))
213 csum_tree_block(root, bh, 0);
214 bh = bh->b_this_page;
215 } while (bh != head);
Chris Masond98237b2007-03-28 13:57:48 -0400216 return block_write_full_page(page, btree_get_block, wbc);
217}
218
219static int btree_readpage(struct file * file, struct page * page)
220{
221 return block_read_full_page(page, btree_get_block);
222}
223
224static struct address_space_operations btree_aops = {
225 .readpage = btree_readpage,
226 .writepage = btree_writepage,
227 .sync_page = block_sync_page,
228};
229
Chris Masone20d96d2007-03-22 12:13:20 -0400230struct buffer_head *read_tree_block(struct btrfs_root *root, u64 blocknr)
231{
Chris Masond98237b2007-03-28 13:57:48 -0400232 struct buffer_head *bh = NULL;
Chris Masone20d96d2007-03-22 12:13:20 -0400233
Chris Masond98237b2007-03-28 13:57:48 -0400234 bh = btrfs_find_create_tree_block(root, blocknr);
235 if (!bh)
236 return bh;
Chris Mason9d642722007-04-03 11:43:19 -0400237 if (buffer_uptodate(bh))
238 goto uptodate;
Chris Masond98237b2007-03-28 13:57:48 -0400239 lock_buffer(bh);
240 if (!buffer_uptodate(bh)) {
241 get_bh(bh);
242 bh->b_end_io = end_buffer_read_sync;
243 submit_bh(READ, bh);
244 wait_on_buffer(bh);
245 if (!buffer_uptodate(bh))
246 goto fail;
Chris Mason87cbda52007-03-28 19:44:27 -0400247 csum_tree_block(root, bh, 1);
Chris Masond98237b2007-03-28 13:57:48 -0400248 } else {
249 unlock_buffer(bh);
250 }
Chris Mason9d642722007-04-03 11:43:19 -0400251uptodate:
Chris Masond98237b2007-03-28 13:57:48 -0400252 if (check_tree_block(root, bh))
Chris Masoncfaa7292007-02-21 17:04:57 -0500253 BUG();
Chris Masond98237b2007-03-28 13:57:48 -0400254 return bh;
255fail:
256 brelse(bh);
257 return NULL;
Chris Masoneb60cea2007-02-02 09:18:22 -0500258}
259
Chris Masone089f052007-03-16 16:20:31 -0400260int dirty_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Masone20d96d2007-03-22 12:13:20 -0400261 struct buffer_head *buf)
Chris Masoned2ff2c2007-03-01 18:59:40 -0500262{
Chris Masond6025572007-03-30 14:27:56 -0400263 WARN_ON(atomic_read(&buf->b_count) == 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400264 mark_buffer_dirty(buf);
Chris Masoned2ff2c2007-03-01 18:59:40 -0500265 return 0;
266}
267
Chris Masone089f052007-03-16 16:20:31 -0400268int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Masone20d96d2007-03-22 12:13:20 -0400269 struct buffer_head *buf)
Chris Masoned2ff2c2007-03-01 18:59:40 -0500270{
Chris Masond6025572007-03-30 14:27:56 -0400271 WARN_ON(atomic_read(&buf->b_count) == 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400272 clear_buffer_dirty(buf);
Chris Masoned2ff2c2007-03-01 18:59:40 -0500273 return 0;
274}
275
Chris Mason2c90e5d2007-04-02 10:50:19 -0400276static int __setup_root(int blocksize,
Chris Mason9f5fae22007-03-20 14:38:32 -0400277 struct btrfs_root *root,
278 struct btrfs_fs_info *fs_info,
Chris Masone20d96d2007-03-22 12:13:20 -0400279 u64 objectid)
Chris Masond97e63b2007-02-20 16:40:44 -0500280{
Chris Masoncfaa7292007-02-21 17:04:57 -0500281 root->node = NULL;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400282 root->inode = NULL;
Chris Masona28ec192007-03-06 20:08:01 -0500283 root->commit_root = NULL;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400284 root->blocksize = blocksize;
Chris Mason123abc82007-03-14 14:14:43 -0400285 root->ref_cows = 0;
Chris Mason9f5fae22007-03-20 14:38:32 -0400286 root->fs_info = fs_info;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400287 root->objectid = objectid;
288 root->last_trans = 0;
Chris Mason1b05da22007-04-10 12:13:09 -0400289 root->highest_inode = 0;
290 root->last_inode_alloc = 0;
Chris Mason3768f362007-03-13 16:47:54 -0400291 memset(&root->root_key, 0, sizeof(root->root_key));
292 memset(&root->root_item, 0, sizeof(root->root_item));
293 return 0;
294}
295
Chris Mason2c90e5d2007-04-02 10:50:19 -0400296static int find_and_setup_root(int blocksize,
Chris Mason9f5fae22007-03-20 14:38:32 -0400297 struct btrfs_root *tree_root,
298 struct btrfs_fs_info *fs_info,
299 u64 objectid,
Chris Masone20d96d2007-03-22 12:13:20 -0400300 struct btrfs_root *root)
Chris Mason3768f362007-03-13 16:47:54 -0400301{
302 int ret;
303
Chris Mason2c90e5d2007-04-02 10:50:19 -0400304 __setup_root(blocksize, root, fs_info, objectid);
Chris Mason3768f362007-03-13 16:47:54 -0400305 ret = btrfs_find_last_root(tree_root, objectid,
306 &root->root_item, &root->root_key);
307 BUG_ON(ret);
308
309 root->node = read_tree_block(root,
310 btrfs_root_blocknr(&root->root_item));
Chris Mason3768f362007-03-13 16:47:54 -0400311 BUG_ON(!root->node);
Chris Masond97e63b2007-02-20 16:40:44 -0500312 return 0;
313}
314
Chris Mason0f7d52f2007-04-09 10:42:37 -0400315struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
316 struct btrfs_key *location)
317{
318 struct btrfs_root *root;
319 struct btrfs_root *tree_root = fs_info->tree_root;
320 struct btrfs_path *path;
321 struct btrfs_leaf *l;
Chris Mason1b05da22007-04-10 12:13:09 -0400322 u64 highest_inode;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400323 int ret = 0;
324
325printk("read_fs_root looking for %Lu %Lu %u\n", location->objectid, location->offset, location->flags);
Chris Mason2619ba12007-04-10 16:58:11 -0400326 root = radix_tree_lookup(&fs_info->fs_roots_radix,
327 (unsigned long)location->objectid);
328 if (root) {
329printk("found %p in cache\n", root);
330 return root;
331 }
Chris Mason0f7d52f2007-04-09 10:42:37 -0400332 root = kmalloc(sizeof(*root), GFP_NOFS);
333 if (!root) {
334printk("failed1\n");
335 return ERR_PTR(-ENOMEM);
336 }
337 if (location->offset == (u64)-1) {
338 ret = find_and_setup_root(fs_info->sb->s_blocksize,
339 fs_info->tree_root, fs_info,
340 location->objectid, root);
341 if (ret) {
342printk("failed2\n");
343 kfree(root);
344 return ERR_PTR(ret);
345 }
346 goto insert;
347 }
348
349 __setup_root(fs_info->sb->s_blocksize, root, fs_info,
350 location->objectid);
351
352 path = btrfs_alloc_path();
353 BUG_ON(!path);
354 ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
355 if (ret != 0) {
356printk("internal search_slot gives us %d\n", ret);
357 if (ret > 0)
358 ret = -ENOENT;
359 goto out;
360 }
361 l = btrfs_buffer_leaf(path->nodes[0]);
362 memcpy(&root->root_item,
363 btrfs_item_ptr(l, path->slots[0], struct btrfs_root_item),
364 sizeof(root->root_item));
365 memcpy(&root->root_key, location, sizeof(*location));
366 ret = 0;
367out:
368 btrfs_release_path(root, path);
369 btrfs_free_path(path);
370 if (ret) {
371 kfree(root);
372 return ERR_PTR(ret);
373 }
374 root->node = read_tree_block(root,
375 btrfs_root_blocknr(&root->root_item));
376 BUG_ON(!root->node);
377insert:
378printk("inserting %p\n", root);
379 root->ref_cows = 1;
Chris Mason2619ba12007-04-10 16:58:11 -0400380 ret = radix_tree_insert(&fs_info->fs_roots_radix,
381 (unsigned long)root->root_key.objectid,
Chris Mason0f7d52f2007-04-09 10:42:37 -0400382 root);
383 if (ret) {
384printk("radix_tree_insert gives us %d\n", ret);
385 brelse(root->node);
386 kfree(root);
387 return ERR_PTR(ret);
388 }
Chris Mason1b05da22007-04-10 12:13:09 -0400389 ret = btrfs_find_highest_inode(root, &highest_inode);
390 if (ret == 0) {
391 root->highest_inode = highest_inode;
392 root->last_inode_alloc = highest_inode;
393printk("highest inode is %Lu\n", highest_inode);
394 }
Chris Mason0f7d52f2007-04-09 10:42:37 -0400395printk("all worked\n");
396 return root;
397}
398
Chris Mason2c90e5d2007-04-02 10:50:19 -0400399struct btrfs_root *open_ctree(struct super_block *sb)
Chris Masoneb60cea2007-02-02 09:18:22 -0500400{
Chris Masone20d96d2007-03-22 12:13:20 -0400401 struct btrfs_root *extent_root = kmalloc(sizeof(struct btrfs_root),
402 GFP_NOFS);
Chris Mason0bd93ba2007-04-11 13:57:44 -0400403 struct btrfs_root *dev_root = kmalloc(sizeof(struct btrfs_root),
404 GFP_NOFS);
Chris Masone20d96d2007-03-22 12:13:20 -0400405 struct btrfs_root *tree_root = kmalloc(sizeof(struct btrfs_root),
406 GFP_NOFS);
Chris Masone20d96d2007-03-22 12:13:20 -0400407 struct btrfs_fs_info *fs_info = kmalloc(sizeof(*fs_info),
408 GFP_NOFS);
Chris Masoneb60cea2007-02-02 09:18:22 -0500409 int ret;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400410 struct btrfs_super_block *disk_super;
Chris Mason7eccb902007-04-11 15:53:25 -0400411 struct dev_lookup *dev_lookup;
Chris Masoneb60cea2007-02-02 09:18:22 -0500412
Chris Mason8ef97622007-03-26 10:15:30 -0400413 init_bit_radix(&fs_info->pinned_radix);
414 init_bit_radix(&fs_info->pending_del_radix);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400415 INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_NOFS);
Chris Mason7eccb902007-04-11 15:53:25 -0400416 INIT_RADIX_TREE(&fs_info->dev_radix, GFP_NOFS);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400417 sb_set_blocksize(sb, 4096);
Chris Mason9f5fae22007-03-20 14:38:32 -0400418 fs_info->running_transaction = NULL;
Chris Mason9f5fae22007-03-20 14:38:32 -0400419 fs_info->tree_root = tree_root;
420 fs_info->extent_root = extent_root;
Chris Mason0bd93ba2007-04-11 13:57:44 -0400421 fs_info->dev_root = dev_root;
Chris Masone20d96d2007-03-22 12:13:20 -0400422 fs_info->sb = sb;
Chris Masond98237b2007-03-28 13:57:48 -0400423 fs_info->btree_inode = new_inode(sb);
424 fs_info->btree_inode->i_ino = 1;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400425 fs_info->btree_inode->i_nlink = 1;
Chris Masond98237b2007-03-28 13:57:48 -0400426 fs_info->btree_inode->i_size = sb->s_bdev->bd_inode->i_size;
427 fs_info->btree_inode->i_mapping->a_ops = &btree_aops;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400428 BTRFS_I(fs_info->btree_inode)->root = tree_root;
429 memset(&BTRFS_I(fs_info->btree_inode)->location, 0,
430 sizeof(struct btrfs_key));
Chris Mason22b0ebd2007-03-30 08:47:31 -0400431 insert_inode_hash(fs_info->btree_inode);
Chris Masond98237b2007-03-28 13:57:48 -0400432 mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS);
Chris Mason87cbda52007-03-28 19:44:27 -0400433 fs_info->hash_tfm = crypto_alloc_hash("sha256", 0, CRYPTO_ALG_ASYNC);
Chris Mason30ae8462007-03-29 09:59:15 -0400434 spin_lock_init(&fs_info->hash_lock);
Chris Mason30ae8462007-03-29 09:59:15 -0400435 if (!fs_info->hash_tfm || IS_ERR(fs_info->hash_tfm)) {
Chris Mason87cbda52007-03-28 19:44:27 -0400436 printk("failed to allocate sha256 hash\n");
437 return NULL;
438 }
Chris Mason79154b12007-03-22 15:59:16 -0400439 mutex_init(&fs_info->trans_mutex);
Chris Masond561c022007-03-23 19:47:49 -0400440 mutex_init(&fs_info->fs_mutex);
Chris Mason9f5fae22007-03-20 14:38:32 -0400441 memset(&fs_info->current_insert, 0, sizeof(fs_info->current_insert));
442 memset(&fs_info->last_insert, 0, sizeof(fs_info->last_insert));
Chris Mason3768f362007-03-13 16:47:54 -0400443
Chris Mason0bd93ba2007-04-11 13:57:44 -0400444 __setup_root(sb->s_blocksize, dev_root,
445 fs_info, BTRFS_DEV_TREE_OBJECTID);
446
Chris Mason2c90e5d2007-04-02 10:50:19 -0400447 __setup_root(sb->s_blocksize, tree_root,
448 fs_info, BTRFS_ROOT_TREE_OBJECTID);
Chris Mason7eccb902007-04-11 15:53:25 -0400449
450 dev_lookup = kmalloc(sizeof(*dev_lookup), GFP_NOFS);
451 dev_lookup->block_start = 0;
452 dev_lookup->num_blocks = (u32)-2;
453 dev_lookup->bdev = sb->s_bdev;
454 ret = radix_tree_insert(&fs_info->dev_radix, (u32)-2, dev_lookup);
455 BUG_ON(ret);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400456 fs_info->sb_buffer = read_tree_block(tree_root,
457 BTRFS_SUPER_INFO_OFFSET /
458 sb->s_blocksize);
Chris Masond98237b2007-03-28 13:57:48 -0400459
Chris Mason0f7d52f2007-04-09 10:42:37 -0400460 if (!fs_info->sb_buffer)
Chris Masond98237b2007-03-28 13:57:48 -0400461 return NULL;
Chris Masond98237b2007-03-28 13:57:48 -0400462 disk_super = (struct btrfs_super_block *)fs_info->sb_buffer->b_data;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400463 if (!btrfs_super_root(disk_super))
Chris Mason2c90e5d2007-04-02 10:50:19 -0400464 return NULL;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400465
Chris Mason7eccb902007-04-11 15:53:25 -0400466 radix_tree_delete(&fs_info->dev_radix, (u32)-2);
467 dev_lookup->block_start = btrfs_super_device_block_start(disk_super);
468 dev_lookup->num_blocks = btrfs_super_device_num_blocks(disk_super);
469 ret = radix_tree_insert(&fs_info->dev_radix,
470 dev_lookup->block_start +
471 dev_lookup->num_blocks, dev_lookup);
472 BUG_ON(ret);
473
Chris Masond98237b2007-03-28 13:57:48 -0400474 fs_info->disk_super = disk_super;
Chris Mason0bd93ba2007-04-11 13:57:44 -0400475 dev_root->node = read_tree_block(tree_root,
476 btrfs_super_device_root(disk_super));
Chris Masone20d96d2007-03-22 12:13:20 -0400477 tree_root->node = read_tree_block(tree_root,
478 btrfs_super_root(disk_super));
Chris Mason3768f362007-03-13 16:47:54 -0400479 BUG_ON(!tree_root->node);
480
Chris Mason2c90e5d2007-04-02 10:50:19 -0400481 mutex_lock(&fs_info->fs_mutex);
482 ret = find_and_setup_root(sb->s_blocksize, tree_root, fs_info,
Chris Masone20d96d2007-03-22 12:13:20 -0400483 BTRFS_EXTENT_TREE_OBJECTID, extent_root);
Chris Mason3768f362007-03-13 16:47:54 -0400484 BUG_ON(ret);
485
Chris Mason0f7d52f2007-04-09 10:42:37 -0400486 fs_info->generation = btrfs_super_generation(disk_super) + 1;
Chris Masond6e4a422007-04-06 15:37:36 -0400487 memset(&fs_info->kobj, 0, sizeof(fs_info->kobj));
488 kobj_set_kset_s(fs_info, btrfs_subsys);
489 kobject_set_name(&fs_info->kobj, "%s", sb->s_id);
490 kobject_register(&fs_info->kobj);
Chris Mason5be6f7f2007-04-05 13:35:25 -0400491 mutex_unlock(&fs_info->fs_mutex);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400492 return tree_root;
Chris Masoneb60cea2007-02-02 09:18:22 -0500493}
494
Chris Masone089f052007-03-16 16:20:31 -0400495int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason79154b12007-03-22 15:59:16 -0400496 *root)
Chris Masoncfaa7292007-02-21 17:04:57 -0500497{
Chris Masond5719762007-03-23 10:01:08 -0400498 struct buffer_head *bh = root->fs_info->sb_buffer;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400499
Chris Masond5719762007-03-23 10:01:08 -0400500 btrfs_set_super_root(root->fs_info->disk_super,
Chris Mason7eccb902007-04-11 15:53:25 -0400501 bh_blocknr(root->fs_info->tree_root->node));
Chris Masond5719762007-03-23 10:01:08 -0400502 lock_buffer(bh);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400503 WARN_ON(atomic_read(&bh->b_count) < 1);
Chris Masond5719762007-03-23 10:01:08 -0400504 clear_buffer_dirty(bh);
Chris Mason87cbda52007-03-28 19:44:27 -0400505 csum_tree_block(root, bh, 0);
Chris Masond5719762007-03-23 10:01:08 -0400506 bh->b_end_io = end_buffer_write_sync;
507 get_bh(bh);
508 submit_bh(WRITE, bh);
509 wait_on_buffer(bh);
510 if (!buffer_uptodate(bh)) {
511 WARN_ON(1);
512 return -EIO;
Chris Masoncfaa7292007-02-21 17:04:57 -0500513 }
514 return 0;
515}
516
Chris Mason2619ba12007-04-10 16:58:11 -0400517static int free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
518{
519 radix_tree_delete(&fs_info->fs_roots_radix,
520 (unsigned long)root->root_key.objectid);
521 if (root->inode)
522 iput(root->inode);
523 if (root->node)
524 brelse(root->node);
525 if (root->commit_root)
526 brelse(root->commit_root);
527 kfree(root);
528 return 0;
529}
530
Chris Mason0f7d52f2007-04-09 10:42:37 -0400531int del_fs_roots(struct btrfs_fs_info *fs_info)
532{
533 int ret;
534 struct btrfs_root *gang[8];
535 int i;
536
537 while(1) {
538 ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix,
539 (void **)gang, 0,
540 ARRAY_SIZE(gang));
541 if (!ret)
542 break;
Chris Mason2619ba12007-04-10 16:58:11 -0400543 for (i = 0; i < ret; i++)
544 free_fs_root(fs_info, gang[i]);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400545 }
546 return 0;
547}
Chris Mason7eccb902007-04-11 15:53:25 -0400548static int free_dev_radix(struct btrfs_fs_info *fs_info)
549{
550 struct dev_lookup *lookup[8];
551 struct block_device *super_bdev = fs_info->sb->s_bdev;
552 int ret;
553 int i;
554 while(1) {
555 ret = radix_tree_gang_lookup(&fs_info->dev_radix,
556 (void **)lookup, 0,
557 ARRAY_SIZE(lookup));
558 if (!ret)
559 break;
560 for (i = 0; i < ret; i++) {
561 if (lookup[i]->bdev != super_bdev)
562 close_bdev_excl(lookup[i]->bdev);
563 radix_tree_delete(&fs_info->dev_radix,
564 lookup[i]->block_start +
565 lookup[i]->num_blocks);
566 kfree(lookup[i]);
567 }
568 }
569 return 0;
570}
Chris Mason0f7d52f2007-04-09 10:42:37 -0400571
Chris Masone20d96d2007-03-22 12:13:20 -0400572int close_ctree(struct btrfs_root *root)
Chris Masoneb60cea2007-02-02 09:18:22 -0500573{
Chris Mason3768f362007-03-13 16:47:54 -0400574 int ret;
Chris Masone089f052007-03-16 16:20:31 -0400575 struct btrfs_trans_handle *trans;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400576 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone089f052007-03-16 16:20:31 -0400577
Chris Mason0f7d52f2007-04-09 10:42:37 -0400578 mutex_lock(&fs_info->fs_mutex);
Chris Mason79154b12007-03-22 15:59:16 -0400579 trans = btrfs_start_transaction(root, 1);
580 btrfs_commit_transaction(trans, root);
581 /* run commit again to drop the original snapshot */
582 trans = btrfs_start_transaction(root, 1);
583 btrfs_commit_transaction(trans, root);
584 ret = btrfs_write_and_wait_transaction(NULL, root);
Chris Mason9f5fae22007-03-20 14:38:32 -0400585 BUG_ON(ret);
Chris Mason79154b12007-03-22 15:59:16 -0400586 write_ctree_super(NULL, root);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400587 mutex_unlock(&fs_info->fs_mutex);
Chris Masoned2ff2c2007-03-01 18:59:40 -0500588
Chris Mason0f7d52f2007-04-09 10:42:37 -0400589 if (fs_info->extent_root->node)
590 btrfs_block_release(fs_info->extent_root,
591 fs_info->extent_root->node);
Chris Mason0bd93ba2007-04-11 13:57:44 -0400592 if (fs_info->dev_root->node)
593 btrfs_block_release(fs_info->dev_root,
594 fs_info->dev_root->node);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400595 if (fs_info->tree_root->node)
596 btrfs_block_release(fs_info->tree_root,
597 fs_info->tree_root->node);
598 btrfs_block_release(root, fs_info->sb_buffer);
599 crypto_free_hash(fs_info->hash_tfm);
600 truncate_inode_pages(fs_info->btree_inode->i_mapping, 0);
601 iput(fs_info->btree_inode);
Chris Mason7eccb902007-04-11 15:53:25 -0400602
603 free_dev_radix(fs_info);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400604 del_fs_roots(fs_info);
605 kfree(fs_info->extent_root);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400606 kfree(fs_info->tree_root);
607 kobject_unregister(&fs_info->kobj);
Chris Masoneb60cea2007-02-02 09:18:22 -0500608 return 0;
609}
610
Chris Masone20d96d2007-03-22 12:13:20 -0400611void btrfs_block_release(struct btrfs_root *root, struct buffer_head *buf)
Chris Masoneb60cea2007-02-02 09:18:22 -0500612{
Chris Mason7cfcc172007-04-02 14:53:59 -0400613 brelse(buf);
Chris Masoneb60cea2007-02-02 09:18:22 -0500614}
615