blob: e09233262af64a410c1a8077db557ea4ff32e644 [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 Masone20d96d2007-03-22 12:13:20 -040013static int check_tree_block(struct btrfs_root *root, struct buffer_head *buf)
Chris Masoneb60cea2007-02-02 09:18:22 -050014{
Chris Masone20d96d2007-03-22 12:13:20 -040015 struct btrfs_node *node = btrfs_buffer_node(buf);
Chris Masond98237b2007-03-28 13:57:48 -040016 if (buf->b_blocknr != btrfs_header_blocknr(&node->header)) {
Chris Mason9a8dd152007-02-23 08:38:36 -050017 BUG();
Chris Masond98237b2007-03-28 13:57:48 -040018 }
Chris Mason9a8dd152007-02-23 08:38:36 -050019 return 0;
Chris Masoneb60cea2007-02-02 09:18:22 -050020}
21
Chris Masond98237b2007-03-28 13:57:48 -040022struct buffer_head *btrfs_find_tree_block(struct btrfs_root *root, u64 blocknr)
Chris Masoned2ff2c2007-03-01 18:59:40 -050023{
Chris Masond98237b2007-03-28 13:57:48 -040024 struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
25 int blockbits = root->fs_info->sb->s_blocksize_bits;
26 unsigned long index = blocknr >> (PAGE_CACHE_SHIFT - blockbits);
27 struct page *page;
28 struct buffer_head *bh;
29 struct buffer_head *head;
30 struct buffer_head *ret = NULL;
31
Chris Mason2c90e5d2007-04-02 10:50:19 -040032
Chris Masond98237b2007-03-28 13:57:48 -040033 page = find_lock_page(mapping, index);
34 if (!page)
35 return NULL;
36
37 if (!page_has_buffers(page))
38 goto out_unlock;
39
40 head = page_buffers(page);
41 bh = head;
42 do {
43 if (buffer_mapped(bh) && bh->b_blocknr == blocknr) {
44 ret = bh;
45 get_bh(bh);
46 goto out_unlock;
47 }
48 bh = bh->b_this_page;
49 } while (bh != head);
50out_unlock:
51 unlock_page(page);
Chris Masond6025572007-03-30 14:27:56 -040052 if (ret) {
Chris Mason22b0ebd2007-03-30 08:47:31 -040053 touch_buffer(ret);
Chris Masond6025572007-03-30 14:27:56 -040054 }
Chris Masond98237b2007-03-28 13:57:48 -040055 page_cache_release(page);
56 return ret;
Chris Masoned2ff2c2007-03-01 18:59:40 -050057}
58
Chris Masond98237b2007-03-28 13:57:48 -040059struct buffer_head *btrfs_find_create_tree_block(struct btrfs_root *root,
60 u64 blocknr)
Chris Masoneb60cea2007-02-02 09:18:22 -050061{
Chris Masond98237b2007-03-28 13:57:48 -040062 struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
63 int blockbits = root->fs_info->sb->s_blocksize_bits;
64 unsigned long index = blocknr >> (PAGE_CACHE_SHIFT - blockbits);
65 struct page *page;
66 struct buffer_head *bh;
67 struct buffer_head *head;
68 struct buffer_head *ret = NULL;
69 u64 first_block = index << (PAGE_CACHE_SHIFT - blockbits);
Chris Mason22b0ebd2007-03-30 08:47:31 -040070
Chris Masond98237b2007-03-28 13:57:48 -040071 page = grab_cache_page(mapping, index);
72 if (!page)
73 return NULL;
74
Chris Masond98237b2007-03-28 13:57:48 -040075 if (!page_has_buffers(page))
76 create_empty_buffers(page, root->fs_info->sb->s_blocksize, 0);
77 head = page_buffers(page);
78 bh = head;
79 do {
80 if (!buffer_mapped(bh)) {
81 bh->b_bdev = root->fs_info->sb->s_bdev;
82 bh->b_blocknr = first_block;
83 set_buffer_mapped(bh);
84 }
85 if (bh->b_blocknr == blocknr) {
86 ret = bh;
87 get_bh(bh);
88 goto out_unlock;
89 }
90 bh = bh->b_this_page;
91 first_block++;
92 } while (bh != head);
93out_unlock:
94 unlock_page(page);
Chris Mason22b0ebd2007-03-30 08:47:31 -040095 if (ret)
96 touch_buffer(ret);
Chris Masond98237b2007-03-28 13:57:48 -040097 page_cache_release(page);
98 return ret;
Chris Masone20d96d2007-03-22 12:13:20 -040099}
Chris Mason123abc82007-03-14 14:14:43 -0400100
Chris Masond98237b2007-03-28 13:57:48 -0400101static sector_t max_block(struct block_device *bdev)
102{
103 sector_t retval = ~((sector_t)0);
104 loff_t sz = i_size_read(bdev->bd_inode);
105
106 if (sz) {
107 unsigned int size = block_size(bdev);
108 unsigned int sizebits = blksize_bits(size);
109 retval = (sz >> sizebits);
110 }
111 return retval;
112}
113
114static int btree_get_block(struct inode *inode, sector_t iblock,
115 struct buffer_head *bh, int create)
116{
117 if (iblock >= max_block(inode->i_sb->s_bdev)) {
118 if (create)
119 return -EIO;
120
121 /*
122 * for reads, we're just trying to fill a partial page.
123 * return a hole, they will have to call get_block again
124 * before they can fill it, and they will get -EIO at that
125 * time
126 */
127 return 0;
128 }
129 bh->b_bdev = inode->i_sb->s_bdev;
130 bh->b_blocknr = iblock;
131 set_buffer_mapped(bh);
132 return 0;
133}
134
Chris Masonf254e522007-03-29 15:15:27 -0400135int btrfs_csum_data(struct btrfs_root * root, char *data, size_t len,
136 char *result)
Chris Mason87cbda52007-03-28 19:44:27 -0400137{
Chris Mason87cbda52007-03-28 19:44:27 -0400138 struct scatterlist sg;
139 struct crypto_hash *tfm = root->fs_info->hash_tfm;
140 struct hash_desc desc;
141 int ret;
Chris Mason87cbda52007-03-28 19:44:27 -0400142
143 desc.tfm = tfm;
144 desc.flags = 0;
Chris Masonf254e522007-03-29 15:15:27 -0400145 sg_init_one(&sg, data, len);
Chris Mason87cbda52007-03-28 19:44:27 -0400146 spin_lock(&root->fs_info->hash_lock);
Chris Mason22b0ebd2007-03-30 08:47:31 -0400147 ret = crypto_hash_digest(&desc, &sg, 1, result);
Chris Mason87cbda52007-03-28 19:44:27 -0400148 spin_unlock(&root->fs_info->hash_lock);
149 if (ret) {
150 printk("sha256 digest failed\n");
151 }
Chris Masonf254e522007-03-29 15:15:27 -0400152 return ret;
153}
154static int csum_tree_block(struct btrfs_root *root, struct buffer_head *bh,
155 int verify)
156{
157 char result[BTRFS_CSUM_SIZE];
158 int ret;
159 struct btrfs_node *node;
160
161 ret = btrfs_csum_data(root, bh->b_data + BTRFS_CSUM_SIZE,
162 bh->b_size - BTRFS_CSUM_SIZE, result);
163 if (ret)
164 return ret;
Chris Mason87cbda52007-03-28 19:44:27 -0400165 if (verify) {
Chris Masonf254e522007-03-29 15:15:27 -0400166 if (memcmp(bh->b_data, result, BTRFS_CSUM_SIZE)) {
167 printk("checksum verify failed on %lu\n",
168 bh->b_blocknr);
169 return 1;
170 }
171 } else {
172 node = btrfs_buffer_node(bh);
Chris Mason22b0ebd2007-03-30 08:47:31 -0400173 memcpy(node->header.csum, result, BTRFS_CSUM_SIZE);
Chris Masonf254e522007-03-29 15:15:27 -0400174 }
Chris Mason87cbda52007-03-28 19:44:27 -0400175 return 0;
176}
177
Chris Masond98237b2007-03-28 13:57:48 -0400178static int btree_writepage(struct page *page, struct writeback_control *wbc)
179{
Chris Mason87cbda52007-03-28 19:44:27 -0400180 struct buffer_head *bh;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400181 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
Chris Mason87cbda52007-03-28 19:44:27 -0400182 struct buffer_head *head;
Chris Mason87cbda52007-03-28 19:44:27 -0400183 if (!page_has_buffers(page)) {
184 create_empty_buffers(page, root->fs_info->sb->s_blocksize,
185 (1 << BH_Dirty)|(1 << BH_Uptodate));
186 }
187 head = page_buffers(page);
188 bh = head;
189 do {
190 if (buffer_dirty(bh))
191 csum_tree_block(root, bh, 0);
192 bh = bh->b_this_page;
193 } while (bh != head);
Chris Masond98237b2007-03-28 13:57:48 -0400194 return block_write_full_page(page, btree_get_block, wbc);
195}
196
197static int btree_readpage(struct file * file, struct page * page)
198{
199 return block_read_full_page(page, btree_get_block);
200}
201
202static struct address_space_operations btree_aops = {
203 .readpage = btree_readpage,
204 .writepage = btree_writepage,
205 .sync_page = block_sync_page,
206};
207
Chris Masone20d96d2007-03-22 12:13:20 -0400208struct buffer_head *read_tree_block(struct btrfs_root *root, u64 blocknr)
209{
Chris Masond98237b2007-03-28 13:57:48 -0400210 struct buffer_head *bh = NULL;
Chris Masone20d96d2007-03-22 12:13:20 -0400211
Chris Masond98237b2007-03-28 13:57:48 -0400212 bh = btrfs_find_create_tree_block(root, blocknr);
213 if (!bh)
214 return bh;
Chris Mason9d642722007-04-03 11:43:19 -0400215 if (buffer_uptodate(bh))
216 goto uptodate;
Chris Masond98237b2007-03-28 13:57:48 -0400217 lock_buffer(bh);
218 if (!buffer_uptodate(bh)) {
219 get_bh(bh);
220 bh->b_end_io = end_buffer_read_sync;
221 submit_bh(READ, bh);
222 wait_on_buffer(bh);
223 if (!buffer_uptodate(bh))
224 goto fail;
Chris Mason87cbda52007-03-28 19:44:27 -0400225 csum_tree_block(root, bh, 1);
Chris Masond98237b2007-03-28 13:57:48 -0400226 } else {
227 unlock_buffer(bh);
228 }
Chris Mason9d642722007-04-03 11:43:19 -0400229uptodate:
Chris Masond98237b2007-03-28 13:57:48 -0400230 if (check_tree_block(root, bh))
Chris Masoncfaa7292007-02-21 17:04:57 -0500231 BUG();
Chris Masond98237b2007-03-28 13:57:48 -0400232 return bh;
233fail:
234 brelse(bh);
235 return NULL;
Chris Masoneb60cea2007-02-02 09:18:22 -0500236}
237
Chris Masone089f052007-03-16 16:20:31 -0400238int dirty_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Masone20d96d2007-03-22 12:13:20 -0400239 struct buffer_head *buf)
Chris Masoned2ff2c2007-03-01 18:59:40 -0500240{
Chris Masond6025572007-03-30 14:27:56 -0400241 WARN_ON(atomic_read(&buf->b_count) == 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400242 mark_buffer_dirty(buf);
Chris Masoned2ff2c2007-03-01 18:59:40 -0500243 return 0;
244}
245
Chris Masone089f052007-03-16 16:20:31 -0400246int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Masone20d96d2007-03-22 12:13:20 -0400247 struct buffer_head *buf)
Chris Masoned2ff2c2007-03-01 18:59:40 -0500248{
Chris Masond6025572007-03-30 14:27:56 -0400249 WARN_ON(atomic_read(&buf->b_count) == 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400250 clear_buffer_dirty(buf);
Chris Masoned2ff2c2007-03-01 18:59:40 -0500251 return 0;
252}
253
Chris Mason2c90e5d2007-04-02 10:50:19 -0400254static int __setup_root(int blocksize,
Chris Mason9f5fae22007-03-20 14:38:32 -0400255 struct btrfs_root *root,
256 struct btrfs_fs_info *fs_info,
Chris Masone20d96d2007-03-22 12:13:20 -0400257 u64 objectid)
Chris Masond97e63b2007-02-20 16:40:44 -0500258{
Chris Masoncfaa7292007-02-21 17:04:57 -0500259 root->node = NULL;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400260 root->inode = NULL;
Chris Masona28ec192007-03-06 20:08:01 -0500261 root->commit_root = NULL;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400262 root->blocksize = blocksize;
Chris Mason123abc82007-03-14 14:14:43 -0400263 root->ref_cows = 0;
Chris Mason9f5fae22007-03-20 14:38:32 -0400264 root->fs_info = fs_info;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400265 root->objectid = objectid;
266 root->last_trans = 0;
Chris Mason1b05da22007-04-10 12:13:09 -0400267 root->highest_inode = 0;
268 root->last_inode_alloc = 0;
Chris Mason3768f362007-03-13 16:47:54 -0400269 memset(&root->root_key, 0, sizeof(root->root_key));
270 memset(&root->root_item, 0, sizeof(root->root_item));
271 return 0;
272}
273
Chris Mason2c90e5d2007-04-02 10:50:19 -0400274static int find_and_setup_root(int blocksize,
Chris Mason9f5fae22007-03-20 14:38:32 -0400275 struct btrfs_root *tree_root,
276 struct btrfs_fs_info *fs_info,
277 u64 objectid,
Chris Masone20d96d2007-03-22 12:13:20 -0400278 struct btrfs_root *root)
Chris Mason3768f362007-03-13 16:47:54 -0400279{
280 int ret;
281
Chris Mason2c90e5d2007-04-02 10:50:19 -0400282 __setup_root(blocksize, root, fs_info, objectid);
Chris Mason3768f362007-03-13 16:47:54 -0400283 ret = btrfs_find_last_root(tree_root, objectid,
284 &root->root_item, &root->root_key);
285 BUG_ON(ret);
286
287 root->node = read_tree_block(root,
288 btrfs_root_blocknr(&root->root_item));
Chris Mason3768f362007-03-13 16:47:54 -0400289 BUG_ON(!root->node);
Chris Masond97e63b2007-02-20 16:40:44 -0500290 return 0;
291}
292
Chris Mason0f7d52f2007-04-09 10:42:37 -0400293struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
294 struct btrfs_key *location)
295{
296 struct btrfs_root *root;
297 struct btrfs_root *tree_root = fs_info->tree_root;
298 struct btrfs_path *path;
299 struct btrfs_leaf *l;
Chris Mason1b05da22007-04-10 12:13:09 -0400300 u64 highest_inode;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400301 int ret = 0;
302
303printk("read_fs_root looking for %Lu %Lu %u\n", location->objectid, location->offset, location->flags);
Chris Mason2619ba12007-04-10 16:58:11 -0400304 root = radix_tree_lookup(&fs_info->fs_roots_radix,
305 (unsigned long)location->objectid);
306 if (root) {
307printk("found %p in cache\n", root);
308 return root;
309 }
Chris Mason0f7d52f2007-04-09 10:42:37 -0400310 root = kmalloc(sizeof(*root), GFP_NOFS);
311 if (!root) {
312printk("failed1\n");
313 return ERR_PTR(-ENOMEM);
314 }
315 if (location->offset == (u64)-1) {
316 ret = find_and_setup_root(fs_info->sb->s_blocksize,
317 fs_info->tree_root, fs_info,
318 location->objectid, root);
319 if (ret) {
320printk("failed2\n");
321 kfree(root);
322 return ERR_PTR(ret);
323 }
324 goto insert;
325 }
326
327 __setup_root(fs_info->sb->s_blocksize, root, fs_info,
328 location->objectid);
329
330 path = btrfs_alloc_path();
331 BUG_ON(!path);
332 ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
333 if (ret != 0) {
334printk("internal search_slot gives us %d\n", ret);
335 if (ret > 0)
336 ret = -ENOENT;
337 goto out;
338 }
339 l = btrfs_buffer_leaf(path->nodes[0]);
340 memcpy(&root->root_item,
341 btrfs_item_ptr(l, path->slots[0], struct btrfs_root_item),
342 sizeof(root->root_item));
343 memcpy(&root->root_key, location, sizeof(*location));
344 ret = 0;
345out:
346 btrfs_release_path(root, path);
347 btrfs_free_path(path);
348 if (ret) {
349 kfree(root);
350 return ERR_PTR(ret);
351 }
352 root->node = read_tree_block(root,
353 btrfs_root_blocknr(&root->root_item));
354 BUG_ON(!root->node);
355insert:
356printk("inserting %p\n", root);
357 root->ref_cows = 1;
Chris Mason2619ba12007-04-10 16:58:11 -0400358 ret = radix_tree_insert(&fs_info->fs_roots_radix,
359 (unsigned long)root->root_key.objectid,
Chris Mason0f7d52f2007-04-09 10:42:37 -0400360 root);
361 if (ret) {
362printk("radix_tree_insert gives us %d\n", ret);
363 brelse(root->node);
364 kfree(root);
365 return ERR_PTR(ret);
366 }
Chris Mason1b05da22007-04-10 12:13:09 -0400367 ret = btrfs_find_highest_inode(root, &highest_inode);
368 if (ret == 0) {
369 root->highest_inode = highest_inode;
370 root->last_inode_alloc = highest_inode;
371printk("highest inode is %Lu\n", highest_inode);
372 }
Chris Mason0f7d52f2007-04-09 10:42:37 -0400373printk("all worked\n");
374 return root;
375}
376
Chris Mason2c90e5d2007-04-02 10:50:19 -0400377struct btrfs_root *open_ctree(struct super_block *sb)
Chris Masoneb60cea2007-02-02 09:18:22 -0500378{
Chris Masone20d96d2007-03-22 12:13:20 -0400379 struct btrfs_root *extent_root = kmalloc(sizeof(struct btrfs_root),
380 GFP_NOFS);
Chris Mason0bd93ba2007-04-11 13:57:44 -0400381 struct btrfs_root *dev_root = kmalloc(sizeof(struct btrfs_root),
382 GFP_NOFS);
Chris Masone20d96d2007-03-22 12:13:20 -0400383 struct btrfs_root *tree_root = kmalloc(sizeof(struct btrfs_root),
384 GFP_NOFS);
Chris Masone20d96d2007-03-22 12:13:20 -0400385 struct btrfs_fs_info *fs_info = kmalloc(sizeof(*fs_info),
386 GFP_NOFS);
Chris Masoneb60cea2007-02-02 09:18:22 -0500387 int ret;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400388 struct btrfs_super_block *disk_super;
Chris Masoneb60cea2007-02-02 09:18:22 -0500389
Chris Mason8ef97622007-03-26 10:15:30 -0400390 init_bit_radix(&fs_info->pinned_radix);
391 init_bit_radix(&fs_info->pending_del_radix);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400392 INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_NOFS);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400393 sb_set_blocksize(sb, 4096);
Chris Mason9f5fae22007-03-20 14:38:32 -0400394 fs_info->running_transaction = NULL;
Chris Mason9f5fae22007-03-20 14:38:32 -0400395 fs_info->tree_root = tree_root;
396 fs_info->extent_root = extent_root;
Chris Mason0bd93ba2007-04-11 13:57:44 -0400397 fs_info->dev_root = dev_root;
Chris Masone20d96d2007-03-22 12:13:20 -0400398 fs_info->sb = sb;
Chris Masond98237b2007-03-28 13:57:48 -0400399 fs_info->btree_inode = new_inode(sb);
400 fs_info->btree_inode->i_ino = 1;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400401 fs_info->btree_inode->i_nlink = 1;
Chris Masond98237b2007-03-28 13:57:48 -0400402 fs_info->btree_inode->i_size = sb->s_bdev->bd_inode->i_size;
403 fs_info->btree_inode->i_mapping->a_ops = &btree_aops;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400404 BTRFS_I(fs_info->btree_inode)->root = tree_root;
405 memset(&BTRFS_I(fs_info->btree_inode)->location, 0,
406 sizeof(struct btrfs_key));
Chris Mason22b0ebd2007-03-30 08:47:31 -0400407 insert_inode_hash(fs_info->btree_inode);
Chris Masond98237b2007-03-28 13:57:48 -0400408 mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS);
Chris Mason87cbda52007-03-28 19:44:27 -0400409 fs_info->hash_tfm = crypto_alloc_hash("sha256", 0, CRYPTO_ALG_ASYNC);
Chris Mason30ae8462007-03-29 09:59:15 -0400410 spin_lock_init(&fs_info->hash_lock);
Chris Mason30ae8462007-03-29 09:59:15 -0400411 if (!fs_info->hash_tfm || IS_ERR(fs_info->hash_tfm)) {
Chris Mason87cbda52007-03-28 19:44:27 -0400412 printk("failed to allocate sha256 hash\n");
413 return NULL;
414 }
Chris Mason79154b12007-03-22 15:59:16 -0400415 mutex_init(&fs_info->trans_mutex);
Chris Masond561c022007-03-23 19:47:49 -0400416 mutex_init(&fs_info->fs_mutex);
Chris Mason9f5fae22007-03-20 14:38:32 -0400417 memset(&fs_info->current_insert, 0, sizeof(fs_info->current_insert));
418 memset(&fs_info->last_insert, 0, sizeof(fs_info->last_insert));
Chris Mason3768f362007-03-13 16:47:54 -0400419
Chris Mason0bd93ba2007-04-11 13:57:44 -0400420 __setup_root(sb->s_blocksize, dev_root,
421 fs_info, BTRFS_DEV_TREE_OBJECTID);
422
Chris Mason2c90e5d2007-04-02 10:50:19 -0400423 __setup_root(sb->s_blocksize, tree_root,
424 fs_info, BTRFS_ROOT_TREE_OBJECTID);
425 fs_info->sb_buffer = read_tree_block(tree_root,
426 BTRFS_SUPER_INFO_OFFSET /
427 sb->s_blocksize);
Chris Masond98237b2007-03-28 13:57:48 -0400428
Chris Mason0f7d52f2007-04-09 10:42:37 -0400429 if (!fs_info->sb_buffer)
Chris Masond98237b2007-03-28 13:57:48 -0400430 return NULL;
Chris Masond98237b2007-03-28 13:57:48 -0400431 disk_super = (struct btrfs_super_block *)fs_info->sb_buffer->b_data;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400432 if (!btrfs_super_root(disk_super))
Chris Mason2c90e5d2007-04-02 10:50:19 -0400433 return NULL;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400434
Chris Masond98237b2007-03-28 13:57:48 -0400435 fs_info->disk_super = disk_super;
Chris Mason0bd93ba2007-04-11 13:57:44 -0400436 dev_root->node = read_tree_block(tree_root,
437 btrfs_super_device_root(disk_super));
Chris Masone20d96d2007-03-22 12:13:20 -0400438 tree_root->node = read_tree_block(tree_root,
439 btrfs_super_root(disk_super));
Chris Mason3768f362007-03-13 16:47:54 -0400440 BUG_ON(!tree_root->node);
441
Chris Mason2c90e5d2007-04-02 10:50:19 -0400442 mutex_lock(&fs_info->fs_mutex);
443 ret = find_and_setup_root(sb->s_blocksize, tree_root, fs_info,
Chris Masone20d96d2007-03-22 12:13:20 -0400444 BTRFS_EXTENT_TREE_OBJECTID, extent_root);
Chris Mason3768f362007-03-13 16:47:54 -0400445 BUG_ON(ret);
446
Chris Mason0f7d52f2007-04-09 10:42:37 -0400447 fs_info->generation = btrfs_super_generation(disk_super) + 1;
Chris Masond6e4a422007-04-06 15:37:36 -0400448 memset(&fs_info->kobj, 0, sizeof(fs_info->kobj));
449 kobj_set_kset_s(fs_info, btrfs_subsys);
450 kobject_set_name(&fs_info->kobj, "%s", sb->s_id);
451 kobject_register(&fs_info->kobj);
Chris Mason5be6f7f2007-04-05 13:35:25 -0400452 mutex_unlock(&fs_info->fs_mutex);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400453 return tree_root;
Chris Masoneb60cea2007-02-02 09:18:22 -0500454}
455
Chris Masone089f052007-03-16 16:20:31 -0400456int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason79154b12007-03-22 15:59:16 -0400457 *root)
Chris Masoncfaa7292007-02-21 17:04:57 -0500458{
Chris Masond5719762007-03-23 10:01:08 -0400459 struct buffer_head *bh = root->fs_info->sb_buffer;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400460
Chris Masond5719762007-03-23 10:01:08 -0400461 btrfs_set_super_root(root->fs_info->disk_super,
462 root->fs_info->tree_root->node->b_blocknr);
463 lock_buffer(bh);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400464 WARN_ON(atomic_read(&bh->b_count) < 1);
Chris Masond5719762007-03-23 10:01:08 -0400465 clear_buffer_dirty(bh);
Chris Mason87cbda52007-03-28 19:44:27 -0400466 csum_tree_block(root, bh, 0);
Chris Masond5719762007-03-23 10:01:08 -0400467 bh->b_end_io = end_buffer_write_sync;
468 get_bh(bh);
469 submit_bh(WRITE, bh);
470 wait_on_buffer(bh);
471 if (!buffer_uptodate(bh)) {
472 WARN_ON(1);
473 return -EIO;
Chris Masoncfaa7292007-02-21 17:04:57 -0500474 }
475 return 0;
476}
477
Chris Mason2619ba12007-04-10 16:58:11 -0400478static int free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
479{
480 radix_tree_delete(&fs_info->fs_roots_radix,
481 (unsigned long)root->root_key.objectid);
482 if (root->inode)
483 iput(root->inode);
484 if (root->node)
485 brelse(root->node);
486 if (root->commit_root)
487 brelse(root->commit_root);
488 kfree(root);
489 return 0;
490}
491
Chris Mason0f7d52f2007-04-09 10:42:37 -0400492int del_fs_roots(struct btrfs_fs_info *fs_info)
493{
494 int ret;
495 struct btrfs_root *gang[8];
496 int i;
497
498 while(1) {
499 ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix,
500 (void **)gang, 0,
501 ARRAY_SIZE(gang));
502 if (!ret)
503 break;
Chris Mason2619ba12007-04-10 16:58:11 -0400504 for (i = 0; i < ret; i++)
505 free_fs_root(fs_info, gang[i]);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400506 }
507 return 0;
508}
509
Chris Masone20d96d2007-03-22 12:13:20 -0400510int close_ctree(struct btrfs_root *root)
Chris Masoneb60cea2007-02-02 09:18:22 -0500511{
Chris Mason3768f362007-03-13 16:47:54 -0400512 int ret;
Chris Masone089f052007-03-16 16:20:31 -0400513 struct btrfs_trans_handle *trans;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400514 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone089f052007-03-16 16:20:31 -0400515
Chris Mason0f7d52f2007-04-09 10:42:37 -0400516 mutex_lock(&fs_info->fs_mutex);
Chris Mason79154b12007-03-22 15:59:16 -0400517 trans = btrfs_start_transaction(root, 1);
518 btrfs_commit_transaction(trans, root);
519 /* run commit again to drop the original snapshot */
520 trans = btrfs_start_transaction(root, 1);
521 btrfs_commit_transaction(trans, root);
522 ret = btrfs_write_and_wait_transaction(NULL, root);
Chris Mason9f5fae22007-03-20 14:38:32 -0400523 BUG_ON(ret);
Chris Mason79154b12007-03-22 15:59:16 -0400524 write_ctree_super(NULL, root);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400525 mutex_unlock(&fs_info->fs_mutex);
Chris Masoned2ff2c2007-03-01 18:59:40 -0500526
Chris Mason0f7d52f2007-04-09 10:42:37 -0400527 if (fs_info->extent_root->node)
528 btrfs_block_release(fs_info->extent_root,
529 fs_info->extent_root->node);
Chris Mason0bd93ba2007-04-11 13:57:44 -0400530 if (fs_info->dev_root->node)
531 btrfs_block_release(fs_info->dev_root,
532 fs_info->dev_root->node);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400533 if (fs_info->tree_root->node)
534 btrfs_block_release(fs_info->tree_root,
535 fs_info->tree_root->node);
536 btrfs_block_release(root, fs_info->sb_buffer);
537 crypto_free_hash(fs_info->hash_tfm);
538 truncate_inode_pages(fs_info->btree_inode->i_mapping, 0);
539 iput(fs_info->btree_inode);
540 del_fs_roots(fs_info);
541 kfree(fs_info->extent_root);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400542 kfree(fs_info->tree_root);
543 kobject_unregister(&fs_info->kobj);
Chris Masoneb60cea2007-02-02 09:18:22 -0500544 return 0;
545}
546
Chris Masone20d96d2007-03-22 12:13:20 -0400547void btrfs_block_release(struct btrfs_root *root, struct buffer_head *buf)
Chris Masoneb60cea2007-02-02 09:18:22 -0500548{
Chris Mason7cfcc172007-04-02 14:53:59 -0400549 brelse(buf);
Chris Masoneb60cea2007-02-02 09:18:22 -0500550}
551