blob: 878101172546370e437783f4cc770b993109ad4b [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 Mason35b7e472007-05-02 15:53:43 -04008#include <linux/writeback.h>
Chris Masoneb60cea2007-02-02 09:18:22 -05009#include "ctree.h"
10#include "disk-io.h"
Chris Masone089f052007-03-16 16:20:31 -040011#include "transaction.h"
Chris Mason0f7d52f2007-04-09 10:42:37 -040012#include "btrfs_inode.h"
Chris Masoneb60cea2007-02-02 09:18:22 -050013
Chris Mason7eccb902007-04-11 15:53:25 -040014u64 bh_blocknr(struct buffer_head *bh)
15{
Chris Mason0cf6c622007-06-09 09:22:25 -040016 return bh->b_blocknr;
Chris Mason7eccb902007-04-11 15:53:25 -040017}
18
Chris Masone20d96d2007-03-22 12:13:20 -040019static int check_tree_block(struct btrfs_root *root, struct buffer_head *buf)
Chris Masoneb60cea2007-02-02 09:18:22 -050020{
Chris Masone20d96d2007-03-22 12:13:20 -040021 struct btrfs_node *node = btrfs_buffer_node(buf);
Chris Mason7eccb902007-04-11 15:53:25 -040022 if (bh_blocknr(buf) != btrfs_header_blocknr(&node->header)) {
Chris Mason5af39812007-06-12 07:50:13 -040023 printk(KERN_CRIT "bh_blocknr(buf) is %llu, header is %llu\n",
24 (unsigned long long)bh_blocknr(buf),
25 (unsigned long long)btrfs_header_blocknr(&node->header));
Chris Mason39279cc2007-06-12 06:35:45 -040026 return 1;
Chris Masond98237b2007-03-28 13:57:48 -040027 }
Chris Mason9a8dd152007-02-23 08:38:36 -050028 return 0;
Chris Masoneb60cea2007-02-02 09:18:22 -050029}
30
Chris Masond98237b2007-03-28 13:57:48 -040031struct buffer_head *btrfs_find_tree_block(struct btrfs_root *root, u64 blocknr)
Chris Masoned2ff2c2007-03-01 18:59:40 -050032{
Chris Masond98237b2007-03-28 13:57:48 -040033 struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
34 int blockbits = root->fs_info->sb->s_blocksize_bits;
35 unsigned long index = blocknr >> (PAGE_CACHE_SHIFT - blockbits);
36 struct page *page;
37 struct buffer_head *bh;
38 struct buffer_head *head;
39 struct buffer_head *ret = NULL;
40
Chris Mason2c90e5d2007-04-02 10:50:19 -040041
Chris Masond98237b2007-03-28 13:57:48 -040042 page = find_lock_page(mapping, index);
43 if (!page)
44 return NULL;
45
46 if (!page_has_buffers(page))
47 goto out_unlock;
48
49 head = page_buffers(page);
50 bh = head;
51 do {
Chris Mason7eccb902007-04-11 15:53:25 -040052 if (buffer_mapped(bh) && bh_blocknr(bh) == blocknr) {
Chris Masond98237b2007-03-28 13:57:48 -040053 ret = bh;
54 get_bh(bh);
55 goto out_unlock;
56 }
57 bh = bh->b_this_page;
58 } while (bh != head);
59out_unlock:
60 unlock_page(page);
61 page_cache_release(page);
62 return ret;
Chris Masoned2ff2c2007-03-01 18:59:40 -050063}
64
Chris Mason8352d8a2007-04-12 10:43:05 -040065int btrfs_map_bh_to_logical(struct btrfs_root *root, struct buffer_head *bh,
Chris Mason7eccb902007-04-11 15:53:25 -040066 u64 logical)
67{
Chris Mason236454d2007-04-19 13:37:44 -040068 if (logical == 0) {
69 bh->b_bdev = NULL;
70 bh->b_blocknr = 0;
71 set_buffer_mapped(bh);
Chris Mason0cf6c622007-06-09 09:22:25 -040072 } else {
73 map_bh(bh, root->fs_info->sb, logical);
Chris Mason236454d2007-04-19 13:37:44 -040074 }
Chris Mason0cf6c622007-06-09 09:22:25 -040075 return 0;
Chris Mason7eccb902007-04-11 15:53:25 -040076}
77
Chris Masond98237b2007-03-28 13:57:48 -040078struct buffer_head *btrfs_find_create_tree_block(struct btrfs_root *root,
79 u64 blocknr)
Chris Masoneb60cea2007-02-02 09:18:22 -050080{
Chris Masond98237b2007-03-28 13:57:48 -040081 struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
82 int blockbits = root->fs_info->sb->s_blocksize_bits;
83 unsigned long index = blocknr >> (PAGE_CACHE_SHIFT - blockbits);
84 struct page *page;
85 struct buffer_head *bh;
86 struct buffer_head *head;
87 struct buffer_head *ret = NULL;
Chris Mason7eccb902007-04-11 15:53:25 -040088 int err;
Chris Masond98237b2007-03-28 13:57:48 -040089 u64 first_block = index << (PAGE_CACHE_SHIFT - blockbits);
Chris Mason22b0ebd2007-03-30 08:47:31 -040090
Chris Masond98237b2007-03-28 13:57:48 -040091 page = grab_cache_page(mapping, index);
92 if (!page)
93 return NULL;
94
Chris Masond98237b2007-03-28 13:57:48 -040095 if (!page_has_buffers(page))
96 create_empty_buffers(page, root->fs_info->sb->s_blocksize, 0);
97 head = page_buffers(page);
98 bh = head;
99 do {
100 if (!buffer_mapped(bh)) {
Chris Mason8352d8a2007-04-12 10:43:05 -0400101 err = btrfs_map_bh_to_logical(root, bh, first_block);
Chris Mason7eccb902007-04-11 15:53:25 -0400102 BUG_ON(err);
Chris Masond98237b2007-03-28 13:57:48 -0400103 }
Chris Mason7eccb902007-04-11 15:53:25 -0400104 if (bh_blocknr(bh) == blocknr) {
Chris Masond98237b2007-03-28 13:57:48 -0400105 ret = bh;
106 get_bh(bh);
107 goto out_unlock;
108 }
109 bh = bh->b_this_page;
110 first_block++;
111 } while (bh != head);
112out_unlock:
113 unlock_page(page);
Chris Mason22b0ebd2007-03-30 08:47:31 -0400114 if (ret)
115 touch_buffer(ret);
Chris Masond98237b2007-03-28 13:57:48 -0400116 page_cache_release(page);
117 return ret;
Chris Masone20d96d2007-03-22 12:13:20 -0400118}
Chris Mason123abc82007-03-14 14:14:43 -0400119
Chris Masond98237b2007-03-28 13:57:48 -0400120static int btree_get_block(struct inode *inode, sector_t iblock,
121 struct buffer_head *bh, int create)
122{
Chris Mason7eccb902007-04-11 15:53:25 -0400123 int err;
124 struct btrfs_root *root = BTRFS_I(bh->b_page->mapping->host)->root;
Chris Mason8352d8a2007-04-12 10:43:05 -0400125 err = btrfs_map_bh_to_logical(root, bh, iblock);
Chris Mason7eccb902007-04-11 15:53:25 -0400126 return err;
Chris Masond98237b2007-03-28 13:57:48 -0400127}
128
Chris Masonf254e522007-03-29 15:15:27 -0400129int btrfs_csum_data(struct btrfs_root * root, char *data, size_t len,
130 char *result)
Chris Mason87cbda52007-03-28 19:44:27 -0400131{
Chris Mason87cbda52007-03-28 19:44:27 -0400132 struct scatterlist sg;
133 struct crypto_hash *tfm = root->fs_info->hash_tfm;
134 struct hash_desc desc;
135 int ret;
Chris Mason87cbda52007-03-28 19:44:27 -0400136
137 desc.tfm = tfm;
138 desc.flags = 0;
Chris Masonf254e522007-03-29 15:15:27 -0400139 sg_init_one(&sg, data, len);
Chris Mason87cbda52007-03-28 19:44:27 -0400140 spin_lock(&root->fs_info->hash_lock);
Chris Mason22b0ebd2007-03-30 08:47:31 -0400141 ret = crypto_hash_digest(&desc, &sg, 1, result);
Chris Mason87cbda52007-03-28 19:44:27 -0400142 spin_unlock(&root->fs_info->hash_lock);
143 if (ret) {
Chris Mason509659c2007-05-10 12:36:17 -0400144 printk("digest failed\n");
Chris Mason87cbda52007-03-28 19:44:27 -0400145 }
Chris Masonf254e522007-03-29 15:15:27 -0400146 return ret;
147}
148static int csum_tree_block(struct btrfs_root *root, struct buffer_head *bh,
149 int verify)
150{
Chris Mason509659c2007-05-10 12:36:17 -0400151 char result[BTRFS_CRC32_SIZE];
Chris Masonf254e522007-03-29 15:15:27 -0400152 int ret;
153 struct btrfs_node *node;
154
155 ret = btrfs_csum_data(root, bh->b_data + BTRFS_CSUM_SIZE,
156 bh->b_size - BTRFS_CSUM_SIZE, result);
157 if (ret)
158 return ret;
Chris Mason87cbda52007-03-28 19:44:27 -0400159 if (verify) {
Chris Mason509659c2007-05-10 12:36:17 -0400160 if (memcmp(bh->b_data, result, BTRFS_CRC32_SIZE)) {
Chris Mason5af39812007-06-12 07:50:13 -0400161 printk("btrfs: %s checksum verify failed on %llu\n",
162 root->fs_info->sb->s_id,
163 (unsigned long long)bh_blocknr(bh));
Chris Masonf254e522007-03-29 15:15:27 -0400164 return 1;
165 }
166 } else {
167 node = btrfs_buffer_node(bh);
Chris Mason509659c2007-05-10 12:36:17 -0400168 memcpy(node->header.csum, result, BTRFS_CRC32_SIZE);
Chris Masonf254e522007-03-29 15:15:27 -0400169 }
Chris Mason87cbda52007-03-28 19:44:27 -0400170 return 0;
171}
172
Chris Masond98237b2007-03-28 13:57:48 -0400173static int btree_writepage(struct page *page, struct writeback_control *wbc)
174{
Chris Mason87cbda52007-03-28 19:44:27 -0400175 struct buffer_head *bh;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400176 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
Chris Mason87cbda52007-03-28 19:44:27 -0400177 struct buffer_head *head;
Chris Mason87cbda52007-03-28 19:44:27 -0400178 if (!page_has_buffers(page)) {
179 create_empty_buffers(page, root->fs_info->sb->s_blocksize,
180 (1 << BH_Dirty)|(1 << BH_Uptodate));
181 }
182 head = page_buffers(page);
183 bh = head;
184 do {
185 if (buffer_dirty(bh))
186 csum_tree_block(root, bh, 0);
187 bh = bh->b_this_page;
188 } while (bh != head);
Chris Masond98237b2007-03-28 13:57:48 -0400189 return block_write_full_page(page, btree_get_block, wbc);
190}
191
192static int btree_readpage(struct file * file, struct page * page)
193{
194 return block_read_full_page(page, btree_get_block);
195}
196
197static struct address_space_operations btree_aops = {
198 .readpage = btree_readpage,
199 .writepage = btree_writepage,
200 .sync_page = block_sync_page,
201};
202
Chris Mason090d1872007-05-01 08:53:32 -0400203int readahead_tree_block(struct btrfs_root *root, u64 blocknr)
204{
205 struct buffer_head *bh = NULL;
Chris Masonde428b62007-05-18 13:28:27 -0400206 int ret = 0;
Chris Mason090d1872007-05-01 08:53:32 -0400207
208 bh = btrfs_find_create_tree_block(root, blocknr);
209 if (!bh)
210 return 0;
Chris Masonde428b62007-05-18 13:28:27 -0400211 if (buffer_uptodate(bh)) {
212 ret = 1;
Chris Mason090d1872007-05-01 08:53:32 -0400213 goto done;
Chris Masonde428b62007-05-18 13:28:27 -0400214 }
215 if (test_set_buffer_locked(bh)) {
216 ret = 1;
Chris Mason090d1872007-05-01 08:53:32 -0400217 goto done;
Chris Masonde428b62007-05-18 13:28:27 -0400218 }
Chris Mason090d1872007-05-01 08:53:32 -0400219 if (!buffer_uptodate(bh)) {
220 get_bh(bh);
221 bh->b_end_io = end_buffer_read_sync;
222 submit_bh(READ, bh);
223 } else {
224 unlock_buffer(bh);
Chris Masonde428b62007-05-18 13:28:27 -0400225 ret = 1;
Chris Mason090d1872007-05-01 08:53:32 -0400226 }
227done:
228 brelse(bh);
Chris Masonde428b62007-05-18 13:28:27 -0400229 return ret;
Chris Mason090d1872007-05-01 08:53:32 -0400230}
231
Chris Masone20d96d2007-03-22 12:13:20 -0400232struct buffer_head *read_tree_block(struct btrfs_root *root, u64 blocknr)
233{
Chris Masond98237b2007-03-28 13:57:48 -0400234 struct buffer_head *bh = NULL;
Chris Masone20d96d2007-03-22 12:13:20 -0400235
Chris Masond98237b2007-03-28 13:57:48 -0400236 bh = btrfs_find_create_tree_block(root, blocknr);
237 if (!bh)
238 return bh;
Chris Mason9d642722007-04-03 11:43:19 -0400239 if (buffer_uptodate(bh))
240 goto uptodate;
Chris Masond98237b2007-03-28 13:57:48 -0400241 lock_buffer(bh);
242 if (!buffer_uptodate(bh)) {
243 get_bh(bh);
244 bh->b_end_io = end_buffer_read_sync;
245 submit_bh(READ, bh);
246 wait_on_buffer(bh);
247 if (!buffer_uptodate(bh))
248 goto fail;
249 } else {
250 unlock_buffer(bh);
251 }
Chris Mason9d642722007-04-03 11:43:19 -0400252uptodate:
Chris Mason090d1872007-05-01 08:53:32 -0400253 if (!buffer_checked(bh)) {
254 csum_tree_block(root, bh, 1);
255 set_buffer_checked(bh);
256 }
Chris Masond98237b2007-03-28 13:57:48 -0400257 if (check_tree_block(root, bh))
Chris Mason39279cc2007-06-12 06:35:45 -0400258 goto fail;
Chris Masond98237b2007-03-28 13:57:48 -0400259 return bh;
260fail:
261 brelse(bh);
262 return NULL;
Chris Masoneb60cea2007-02-02 09:18:22 -0500263}
264
Chris Masone089f052007-03-16 16:20:31 -0400265int dirty_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Masone20d96d2007-03-22 12:13:20 -0400266 struct buffer_head *buf)
Chris Masoned2ff2c2007-03-01 18:59:40 -0500267{
Chris Masond6025572007-03-30 14:27:56 -0400268 WARN_ON(atomic_read(&buf->b_count) == 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400269 mark_buffer_dirty(buf);
Chris Masoned2ff2c2007-03-01 18:59:40 -0500270 return 0;
271}
272
Chris Masone089f052007-03-16 16:20:31 -0400273int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Masone20d96d2007-03-22 12:13:20 -0400274 struct buffer_head *buf)
Chris Masoned2ff2c2007-03-01 18:59:40 -0500275{
Chris Masond6025572007-03-30 14:27:56 -0400276 WARN_ON(atomic_read(&buf->b_count) == 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400277 clear_buffer_dirty(buf);
Chris Masoned2ff2c2007-03-01 18:59:40 -0500278 return 0;
279}
280
Chris Mason2c90e5d2007-04-02 10:50:19 -0400281static int __setup_root(int blocksize,
Chris Mason9f5fae22007-03-20 14:38:32 -0400282 struct btrfs_root *root,
283 struct btrfs_fs_info *fs_info,
Chris Masone20d96d2007-03-22 12:13:20 -0400284 u64 objectid)
Chris Masond97e63b2007-02-20 16:40:44 -0500285{
Chris Masoncfaa7292007-02-21 17:04:57 -0500286 root->node = NULL;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400287 root->inode = NULL;
Chris Masona28ec192007-03-06 20:08:01 -0500288 root->commit_root = NULL;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400289 root->blocksize = blocksize;
Chris Mason123abc82007-03-14 14:14:43 -0400290 root->ref_cows = 0;
Chris Mason9f5fae22007-03-20 14:38:32 -0400291 root->fs_info = fs_info;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400292 root->objectid = objectid;
293 root->last_trans = 0;
Chris Mason1b05da22007-04-10 12:13:09 -0400294 root->highest_inode = 0;
295 root->last_inode_alloc = 0;
Chris Mason3768f362007-03-13 16:47:54 -0400296 memset(&root->root_key, 0, sizeof(root->root_key));
297 memset(&root->root_item, 0, sizeof(root->root_item));
Chris Mason4d775672007-04-20 20:23:12 -0400298 root->root_key.objectid = objectid;
Chris Mason3768f362007-03-13 16:47:54 -0400299 return 0;
300}
301
Chris Mason2c90e5d2007-04-02 10:50:19 -0400302static int find_and_setup_root(int blocksize,
Chris Mason9f5fae22007-03-20 14:38:32 -0400303 struct btrfs_root *tree_root,
304 struct btrfs_fs_info *fs_info,
305 u64 objectid,
Chris Masone20d96d2007-03-22 12:13:20 -0400306 struct btrfs_root *root)
Chris Mason3768f362007-03-13 16:47:54 -0400307{
308 int ret;
309
Chris Mason2c90e5d2007-04-02 10:50:19 -0400310 __setup_root(blocksize, root, fs_info, objectid);
Chris Mason3768f362007-03-13 16:47:54 -0400311 ret = btrfs_find_last_root(tree_root, objectid,
312 &root->root_item, &root->root_key);
313 BUG_ON(ret);
314
315 root->node = read_tree_block(root,
316 btrfs_root_blocknr(&root->root_item));
Chris Mason3768f362007-03-13 16:47:54 -0400317 BUG_ON(!root->node);
Chris Masond97e63b2007-02-20 16:40:44 -0500318 return 0;
319}
320
Chris Mason0f7d52f2007-04-09 10:42:37 -0400321struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
322 struct btrfs_key *location)
323{
324 struct btrfs_root *root;
325 struct btrfs_root *tree_root = fs_info->tree_root;
326 struct btrfs_path *path;
327 struct btrfs_leaf *l;
Chris Mason1b05da22007-04-10 12:13:09 -0400328 u64 highest_inode;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400329 int ret = 0;
330
Chris Mason2619ba12007-04-10 16:58:11 -0400331 root = radix_tree_lookup(&fs_info->fs_roots_radix,
332 (unsigned long)location->objectid);
Chris Mason0cf6c622007-06-09 09:22:25 -0400333 if (root)
Chris Mason2619ba12007-04-10 16:58:11 -0400334 return root;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400335 root = kmalloc(sizeof(*root), GFP_NOFS);
Chris Mason0cf6c622007-06-09 09:22:25 -0400336 if (!root)
Chris Mason0f7d52f2007-04-09 10:42:37 -0400337 return ERR_PTR(-ENOMEM);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400338 if (location->offset == (u64)-1) {
339 ret = find_and_setup_root(fs_info->sb->s_blocksize,
340 fs_info->tree_root, fs_info,
341 location->objectid, root);
342 if (ret) {
Chris Mason0f7d52f2007-04-09 10:42:37 -0400343 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) {
Chris Mason0f7d52f2007-04-09 10:42:37 -0400356 if (ret > 0)
357 ret = -ENOENT;
358 goto out;
359 }
360 l = btrfs_buffer_leaf(path->nodes[0]);
361 memcpy(&root->root_item,
362 btrfs_item_ptr(l, path->slots[0], struct btrfs_root_item),
363 sizeof(root->root_item));
364 memcpy(&root->root_key, location, sizeof(*location));
365 ret = 0;
366out:
367 btrfs_release_path(root, path);
368 btrfs_free_path(path);
369 if (ret) {
370 kfree(root);
371 return ERR_PTR(ret);
372 }
373 root->node = read_tree_block(root,
374 btrfs_root_blocknr(&root->root_item));
375 BUG_ON(!root->node);
376insert:
Chris Mason0f7d52f2007-04-09 10:42:37 -0400377 root->ref_cows = 1;
Chris Mason2619ba12007-04-10 16:58:11 -0400378 ret = radix_tree_insert(&fs_info->fs_roots_radix,
379 (unsigned long)root->root_key.objectid,
Chris Mason0f7d52f2007-04-09 10:42:37 -0400380 root);
381 if (ret) {
Chris Mason0f7d52f2007-04-09 10:42:37 -0400382 brelse(root->node);
383 kfree(root);
384 return ERR_PTR(ret);
385 }
Chris Mason1b05da22007-04-10 12:13:09 -0400386 ret = btrfs_find_highest_inode(root, &highest_inode);
387 if (ret == 0) {
388 root->highest_inode = highest_inode;
389 root->last_inode_alloc = highest_inode;
Chris Mason1b05da22007-04-10 12:13:09 -0400390 }
Chris Mason0f7d52f2007-04-09 10:42:37 -0400391 return root;
392}
393
Chris Mason2c90e5d2007-04-02 10:50:19 -0400394struct btrfs_root *open_ctree(struct super_block *sb)
Chris Masoneb60cea2007-02-02 09:18:22 -0500395{
Chris Masone20d96d2007-03-22 12:13:20 -0400396 struct btrfs_root *extent_root = kmalloc(sizeof(struct btrfs_root),
397 GFP_NOFS);
398 struct btrfs_root *tree_root = kmalloc(sizeof(struct btrfs_root),
399 GFP_NOFS);
Chris Masone20d96d2007-03-22 12:13:20 -0400400 struct btrfs_fs_info *fs_info = kmalloc(sizeof(*fs_info),
401 GFP_NOFS);
Chris Masoneb60cea2007-02-02 09:18:22 -0500402 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -0400403 int err = -EIO;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400404 struct btrfs_super_block *disk_super;
Chris Masoneb60cea2007-02-02 09:18:22 -0500405
Chris Mason39279cc2007-06-12 06:35:45 -0400406 if (!extent_root || !tree_root || !fs_info) {
407 err = -ENOMEM;
408 goto fail;
409 }
Chris Mason8ef97622007-03-26 10:15:30 -0400410 init_bit_radix(&fs_info->pinned_radix);
411 init_bit_radix(&fs_info->pending_del_radix);
Chris Masone37c9e62007-05-09 20:13:14 -0400412 init_bit_radix(&fs_info->extent_map_radix);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400413 INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -0400414 INIT_RADIX_TREE(&fs_info->block_group_radix, GFP_KERNEL);
Chris Masonbe744172007-05-06 10:15:01 -0400415 INIT_RADIX_TREE(&fs_info->block_group_data_radix, GFP_KERNEL);
Chris Mason8fd17792007-04-19 21:01:03 -0400416 INIT_LIST_HEAD(&fs_info->trans_list);
Chris Masonfacda1e2007-06-08 18:11:48 -0400417 INIT_LIST_HEAD(&fs_info->dead_roots);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400418 sb_set_blocksize(sb, 4096);
Chris Mason9f5fae22007-03-20 14:38:32 -0400419 fs_info->running_transaction = NULL;
Chris Mason9f5fae22007-03-20 14:38:32 -0400420 fs_info->tree_root = tree_root;
421 fs_info->extent_root = extent_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 Masone66f7092007-04-20 13:16:02 -0400428 fs_info->do_barriers = 1;
Chris Masonf2458e12007-04-25 15:52:25 -0400429 fs_info->extent_tree_insert_nr = 0;
430 fs_info->extent_tree_prealloc_nr = 0;
Chris Masonfacda1e2007-06-08 18:11:48 -0400431 fs_info->closing = 0;
432
Chris Mason08607c12007-06-08 15:33:54 -0400433 INIT_DELAYED_WORK(&fs_info->trans_work, btrfs_transaction_cleaner);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400434 BTRFS_I(fs_info->btree_inode)->root = tree_root;
435 memset(&BTRFS_I(fs_info->btree_inode)->location, 0,
436 sizeof(struct btrfs_key));
Chris Mason22b0ebd2007-03-30 08:47:31 -0400437 insert_inode_hash(fs_info->btree_inode);
Chris Masond98237b2007-03-28 13:57:48 -0400438 mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS);
Chris Mason509659c2007-05-10 12:36:17 -0400439 fs_info->hash_tfm = crypto_alloc_hash("crc32c", 0, CRYPTO_ALG_ASYNC);
Chris Mason30ae8462007-03-29 09:59:15 -0400440 spin_lock_init(&fs_info->hash_lock);
Chris Mason39279cc2007-06-12 06:35:45 -0400441
Chris Mason30ae8462007-03-29 09:59:15 -0400442 if (!fs_info->hash_tfm || IS_ERR(fs_info->hash_tfm)) {
Chris Mason39279cc2007-06-12 06:35:45 -0400443 printk("btrfs: failed hash setup, modprobe cryptomgr?\n");
444 err = -ENOMEM;
445 goto fail_iput;
Chris Mason87cbda52007-03-28 19:44:27 -0400446 }
Chris Mason79154b12007-03-22 15:59:16 -0400447 mutex_init(&fs_info->trans_mutex);
Chris Masond561c022007-03-23 19:47:49 -0400448 mutex_init(&fs_info->fs_mutex);
Chris Mason3768f362007-03-13 16:47:54 -0400449
Chris Mason2c90e5d2007-04-02 10:50:19 -0400450 __setup_root(sb->s_blocksize, tree_root,
451 fs_info, BTRFS_ROOT_TREE_OBJECTID);
Chris Mason7eccb902007-04-11 15:53:25 -0400452
Chris Mason2c90e5d2007-04-02 10:50:19 -0400453 fs_info->sb_buffer = read_tree_block(tree_root,
454 BTRFS_SUPER_INFO_OFFSET /
455 sb->s_blocksize);
Chris Masond98237b2007-03-28 13:57:48 -0400456
Chris Mason0f7d52f2007-04-09 10:42:37 -0400457 if (!fs_info->sb_buffer)
Chris Mason39279cc2007-06-12 06:35:45 -0400458 goto fail_iput;
Chris Masond98237b2007-03-28 13:57:48 -0400459 disk_super = (struct btrfs_super_block *)fs_info->sb_buffer->b_data;
Chris Mason39279cc2007-06-12 06:35:45 -0400460
Chris Mason0f7d52f2007-04-09 10:42:37 -0400461 if (!btrfs_super_root(disk_super))
Chris Mason39279cc2007-06-12 06:35:45 -0400462 goto fail_sb_buffer;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400463
Chris Mason8352d8a2007-04-12 10:43:05 -0400464 i_size_write(fs_info->btree_inode,
465 btrfs_super_total_blocks(disk_super) <<
466 fs_info->btree_inode->i_blkbits);
467
Chris Masond98237b2007-03-28 13:57:48 -0400468 fs_info->disk_super = disk_super;
Chris Mason39279cc2007-06-12 06:35:45 -0400469
470 if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
471 sizeof(disk_super->magic))) {
472 printk("btrfs: valid FS not found on %s\n", sb->s_id);
473 goto fail_sb_buffer;
474 }
Chris Masone20d96d2007-03-22 12:13:20 -0400475 tree_root->node = read_tree_block(tree_root,
476 btrfs_super_root(disk_super));
Chris Mason39279cc2007-06-12 06:35:45 -0400477 if (!tree_root->node)
478 goto fail_sb_buffer;
Chris Mason3768f362007-03-13 16:47:54 -0400479
Chris Mason2c90e5d2007-04-02 10:50:19 -0400480 mutex_lock(&fs_info->fs_mutex);
481 ret = find_and_setup_root(sb->s_blocksize, tree_root, fs_info,
Chris Masone20d96d2007-03-22 12:13:20 -0400482 BTRFS_EXTENT_TREE_OBJECTID, extent_root);
Chris Mason39279cc2007-06-12 06:35:45 -0400483 if (ret) {
484 mutex_unlock(&fs_info->fs_mutex);
485 goto fail_tree_root;
486 }
Chris Mason3768f362007-03-13 16:47:54 -0400487
Chris Mason9078a3e2007-04-26 16:46:15 -0400488 btrfs_read_block_groups(extent_root);
489
Chris Mason0f7d52f2007-04-09 10:42:37 -0400490 fs_info->generation = btrfs_super_generation(disk_super) + 1;
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 Mason39279cc2007-06-12 06:35:45 -0400493
494fail_tree_root:
495 btrfs_block_release(tree_root, tree_root->node);
496fail_sb_buffer:
497 btrfs_block_release(tree_root, fs_info->sb_buffer);
498fail_iput:
499 iput(fs_info->btree_inode);
500fail:
501 kfree(extent_root);
502 kfree(tree_root);
503 kfree(fs_info);
504 return ERR_PTR(err);
Chris Masoneb60cea2007-02-02 09:18:22 -0500505}
506
Chris Masone089f052007-03-16 16:20:31 -0400507int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason79154b12007-03-22 15:59:16 -0400508 *root)
Chris Masoncfaa7292007-02-21 17:04:57 -0500509{
Chris Masone66f7092007-04-20 13:16:02 -0400510 int ret;
Chris Masond5719762007-03-23 10:01:08 -0400511 struct buffer_head *bh = root->fs_info->sb_buffer;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400512
Chris Masond5719762007-03-23 10:01:08 -0400513 btrfs_set_super_root(root->fs_info->disk_super,
Chris Mason7eccb902007-04-11 15:53:25 -0400514 bh_blocknr(root->fs_info->tree_root->node));
Chris Masond5719762007-03-23 10:01:08 -0400515 lock_buffer(bh);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400516 WARN_ON(atomic_read(&bh->b_count) < 1);
Chris Masond5719762007-03-23 10:01:08 -0400517 clear_buffer_dirty(bh);
Chris Mason87cbda52007-03-28 19:44:27 -0400518 csum_tree_block(root, bh, 0);
Chris Masond5719762007-03-23 10:01:08 -0400519 bh->b_end_io = end_buffer_write_sync;
520 get_bh(bh);
Chris Masone66f7092007-04-20 13:16:02 -0400521 if (root->fs_info->do_barriers)
522 ret = submit_bh(WRITE_BARRIER, bh);
523 else
524 ret = submit_bh(WRITE, bh);
525 if (ret == -EOPNOTSUPP) {
526 set_buffer_uptodate(bh);
527 root->fs_info->do_barriers = 0;
528 ret = submit_bh(WRITE, bh);
529 }
Chris Masond5719762007-03-23 10:01:08 -0400530 wait_on_buffer(bh);
531 if (!buffer_uptodate(bh)) {
532 WARN_ON(1);
533 return -EIO;
Chris Masoncfaa7292007-02-21 17:04:57 -0500534 }
535 return 0;
536}
537
Chris Mason2619ba12007-04-10 16:58:11 -0400538static int free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
539{
540 radix_tree_delete(&fs_info->fs_roots_radix,
541 (unsigned long)root->root_key.objectid);
542 if (root->inode)
543 iput(root->inode);
544 if (root->node)
545 brelse(root->node);
546 if (root->commit_root)
547 brelse(root->commit_root);
548 kfree(root);
549 return 0;
550}
551
Chris Mason35b7e472007-05-02 15:53:43 -0400552static int del_fs_roots(struct btrfs_fs_info *fs_info)
Chris Mason0f7d52f2007-04-09 10:42:37 -0400553{
554 int ret;
555 struct btrfs_root *gang[8];
556 int i;
557
558 while(1) {
559 ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix,
560 (void **)gang, 0,
561 ARRAY_SIZE(gang));
562 if (!ret)
563 break;
Chris Mason2619ba12007-04-10 16:58:11 -0400564 for (i = 0; i < ret; i++)
565 free_fs_root(fs_info, gang[i]);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400566 }
567 return 0;
568}
Chris Masonb4100d62007-04-12 12:14:00 -0400569
Chris Masone20d96d2007-03-22 12:13:20 -0400570int close_ctree(struct btrfs_root *root)
Chris Masoneb60cea2007-02-02 09:18:22 -0500571{
Chris Mason3768f362007-03-13 16:47:54 -0400572 int ret;
Chris Masone089f052007-03-16 16:20:31 -0400573 struct btrfs_trans_handle *trans;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400574 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone089f052007-03-16 16:20:31 -0400575
Chris Masonfacda1e2007-06-08 18:11:48 -0400576 fs_info->closing = 1;
Chris Mason08607c12007-06-08 15:33:54 -0400577 btrfs_transaction_flush_work(root);
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 Mason0f7d52f2007-04-09 10:42:37 -0400592 if (fs_info->tree_root->node)
593 btrfs_block_release(fs_info->tree_root,
594 fs_info->tree_root->node);
595 btrfs_block_release(root, fs_info->sb_buffer);
596 crypto_free_hash(fs_info->hash_tfm);
597 truncate_inode_pages(fs_info->btree_inode->i_mapping, 0);
598 iput(fs_info->btree_inode);
Chris Mason7eccb902007-04-11 15:53:25 -0400599
Chris Mason9078a3e2007-04-26 16:46:15 -0400600 btrfs_free_block_groups(root->fs_info);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400601 del_fs_roots(fs_info);
602 kfree(fs_info->extent_root);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400603 kfree(fs_info->tree_root);
Chris Masoneb60cea2007-02-02 09:18:22 -0500604 return 0;
605}
606
Chris Masone20d96d2007-03-22 12:13:20 -0400607void btrfs_block_release(struct btrfs_root *root, struct buffer_head *buf)
Chris Masoneb60cea2007-02-02 09:18:22 -0500608{
Chris Mason7cfcc172007-04-02 14:53:59 -0400609 brelse(buf);
Chris Masoneb60cea2007-02-02 09:18:22 -0500610}
611
Chris Mason35b7e472007-05-02 15:53:43 -0400612void btrfs_btree_balance_dirty(struct btrfs_root *root)
613{
614 balance_dirty_pages_ratelimited(root->fs_info->btree_inode->i_mapping);
615}