blob: 96bf3ef3a798f17f42c551ff23ecc226a2c0e406 [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 Mason8352d8a2007-04-12 10:43:05 -040023 printk(KERN_CRIT "bh_blocknr(buf) is %Lu, header is %Lu\n",
24 bh_blocknr(buf), btrfs_header_blocknr(&node->header));
Chris Mason39279cc2007-06-12 06:35:45 -040025 return 1;
Chris Masond98237b2007-03-28 13:57:48 -040026 }
Chris Mason9a8dd152007-02-23 08:38:36 -050027 return 0;
Chris Masoneb60cea2007-02-02 09:18:22 -050028}
29
Chris Masond98237b2007-03-28 13:57:48 -040030struct buffer_head *btrfs_find_tree_block(struct btrfs_root *root, u64 blocknr)
Chris Masoned2ff2c2007-03-01 18:59:40 -050031{
Chris Masond98237b2007-03-28 13:57:48 -040032 struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
33 int blockbits = root->fs_info->sb->s_blocksize_bits;
34 unsigned long index = blocknr >> (PAGE_CACHE_SHIFT - blockbits);
35 struct page *page;
36 struct buffer_head *bh;
37 struct buffer_head *head;
38 struct buffer_head *ret = NULL;
39
Chris Mason2c90e5d2007-04-02 10:50:19 -040040
Chris Masond98237b2007-03-28 13:57:48 -040041 page = find_lock_page(mapping, index);
42 if (!page)
43 return NULL;
44
45 if (!page_has_buffers(page))
46 goto out_unlock;
47
48 head = page_buffers(page);
49 bh = head;
50 do {
Chris Mason7eccb902007-04-11 15:53:25 -040051 if (buffer_mapped(bh) && bh_blocknr(bh) == blocknr) {
Chris Masond98237b2007-03-28 13:57:48 -040052 ret = bh;
53 get_bh(bh);
54 goto out_unlock;
55 }
56 bh = bh->b_this_page;
57 } while (bh != head);
58out_unlock:
59 unlock_page(page);
60 page_cache_release(page);
61 return ret;
Chris Masoned2ff2c2007-03-01 18:59:40 -050062}
63
Chris Mason8352d8a2007-04-12 10:43:05 -040064int btrfs_map_bh_to_logical(struct btrfs_root *root, struct buffer_head *bh,
Chris Mason7eccb902007-04-11 15:53:25 -040065 u64 logical)
66{
Chris Mason236454d2007-04-19 13:37:44 -040067 if (logical == 0) {
68 bh->b_bdev = NULL;
69 bh->b_blocknr = 0;
70 set_buffer_mapped(bh);
Chris Mason0cf6c622007-06-09 09:22:25 -040071 } else {
72 map_bh(bh, root->fs_info->sb, logical);
Chris Mason236454d2007-04-19 13:37:44 -040073 }
Chris Mason0cf6c622007-06-09 09:22:25 -040074 return 0;
Chris Mason7eccb902007-04-11 15:53:25 -040075}
76
Chris Masond98237b2007-03-28 13:57:48 -040077struct buffer_head *btrfs_find_create_tree_block(struct btrfs_root *root,
78 u64 blocknr)
Chris Masoneb60cea2007-02-02 09:18:22 -050079{
Chris Masond98237b2007-03-28 13:57:48 -040080 struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
81 int blockbits = root->fs_info->sb->s_blocksize_bits;
82 unsigned long index = blocknr >> (PAGE_CACHE_SHIFT - blockbits);
83 struct page *page;
84 struct buffer_head *bh;
85 struct buffer_head *head;
86 struct buffer_head *ret = NULL;
Chris Mason7eccb902007-04-11 15:53:25 -040087 int err;
Chris Masond98237b2007-03-28 13:57:48 -040088 u64 first_block = index << (PAGE_CACHE_SHIFT - blockbits);
Chris Mason22b0ebd2007-03-30 08:47:31 -040089
Chris Masond98237b2007-03-28 13:57:48 -040090 page = grab_cache_page(mapping, index);
91 if (!page)
92 return NULL;
93
Chris Masond98237b2007-03-28 13:57:48 -040094 if (!page_has_buffers(page))
95 create_empty_buffers(page, root->fs_info->sb->s_blocksize, 0);
96 head = page_buffers(page);
97 bh = head;
98 do {
99 if (!buffer_mapped(bh)) {
Chris Mason8352d8a2007-04-12 10:43:05 -0400100 err = btrfs_map_bh_to_logical(root, bh, first_block);
Chris Mason7eccb902007-04-11 15:53:25 -0400101 BUG_ON(err);
Chris Masond98237b2007-03-28 13:57:48 -0400102 }
Chris Mason7eccb902007-04-11 15:53:25 -0400103 if (bh_blocknr(bh) == blocknr) {
Chris Masond98237b2007-03-28 13:57:48 -0400104 ret = bh;
105 get_bh(bh);
106 goto out_unlock;
107 }
108 bh = bh->b_this_page;
109 first_block++;
110 } while (bh != head);
111out_unlock:
112 unlock_page(page);
Chris Mason22b0ebd2007-03-30 08:47:31 -0400113 if (ret)
114 touch_buffer(ret);
Chris Masond98237b2007-03-28 13:57:48 -0400115 page_cache_release(page);
116 return ret;
Chris Masone20d96d2007-03-22 12:13:20 -0400117}
Chris Mason123abc82007-03-14 14:14:43 -0400118
Chris Masond98237b2007-03-28 13:57:48 -0400119static int btree_get_block(struct inode *inode, sector_t iblock,
120 struct buffer_head *bh, int create)
121{
Chris Mason7eccb902007-04-11 15:53:25 -0400122 int err;
123 struct btrfs_root *root = BTRFS_I(bh->b_page->mapping->host)->root;
Chris Mason8352d8a2007-04-12 10:43:05 -0400124 err = btrfs_map_bh_to_logical(root, bh, iblock);
Chris Mason7eccb902007-04-11 15:53:25 -0400125 return err;
Chris Masond98237b2007-03-28 13:57:48 -0400126}
127
Chris Masonf254e522007-03-29 15:15:27 -0400128int btrfs_csum_data(struct btrfs_root * root, char *data, size_t len,
129 char *result)
Chris Mason87cbda52007-03-28 19:44:27 -0400130{
Chris Mason87cbda52007-03-28 19:44:27 -0400131 struct scatterlist sg;
132 struct crypto_hash *tfm = root->fs_info->hash_tfm;
133 struct hash_desc desc;
134 int ret;
Chris Mason87cbda52007-03-28 19:44:27 -0400135
136 desc.tfm = tfm;
137 desc.flags = 0;
Chris Masonf254e522007-03-29 15:15:27 -0400138 sg_init_one(&sg, data, len);
Chris Mason87cbda52007-03-28 19:44:27 -0400139 spin_lock(&root->fs_info->hash_lock);
Chris Mason22b0ebd2007-03-30 08:47:31 -0400140 ret = crypto_hash_digest(&desc, &sg, 1, result);
Chris Mason87cbda52007-03-28 19:44:27 -0400141 spin_unlock(&root->fs_info->hash_lock);
142 if (ret) {
Chris Mason509659c2007-05-10 12:36:17 -0400143 printk("digest failed\n");
Chris Mason87cbda52007-03-28 19:44:27 -0400144 }
Chris Masonf254e522007-03-29 15:15:27 -0400145 return ret;
146}
147static int csum_tree_block(struct btrfs_root *root, struct buffer_head *bh,
148 int verify)
149{
Chris Mason509659c2007-05-10 12:36:17 -0400150 char result[BTRFS_CRC32_SIZE];
Chris Masonf254e522007-03-29 15:15:27 -0400151 int ret;
152 struct btrfs_node *node;
153
154 ret = btrfs_csum_data(root, bh->b_data + BTRFS_CSUM_SIZE,
155 bh->b_size - BTRFS_CSUM_SIZE, result);
156 if (ret)
157 return ret;
Chris Mason87cbda52007-03-28 19:44:27 -0400158 if (verify) {
Chris Mason509659c2007-05-10 12:36:17 -0400159 if (memcmp(bh->b_data, result, BTRFS_CRC32_SIZE)) {
Chris Mason7eccb902007-04-11 15:53:25 -0400160 printk("checksum verify failed on %Lu\n",
161 bh_blocknr(bh));
Chris Masonf254e522007-03-29 15:15:27 -0400162 return 1;
163 }
164 } else {
165 node = btrfs_buffer_node(bh);
Chris Mason509659c2007-05-10 12:36:17 -0400166 memcpy(node->header.csum, result, BTRFS_CRC32_SIZE);
Chris Masonf254e522007-03-29 15:15:27 -0400167 }
Chris Mason87cbda52007-03-28 19:44:27 -0400168 return 0;
169}
170
Chris Masond98237b2007-03-28 13:57:48 -0400171static int btree_writepage(struct page *page, struct writeback_control *wbc)
172{
Chris Mason87cbda52007-03-28 19:44:27 -0400173 struct buffer_head *bh;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400174 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
Chris Mason87cbda52007-03-28 19:44:27 -0400175 struct buffer_head *head;
Chris Mason87cbda52007-03-28 19:44:27 -0400176 if (!page_has_buffers(page)) {
177 create_empty_buffers(page, root->fs_info->sb->s_blocksize,
178 (1 << BH_Dirty)|(1 << BH_Uptodate));
179 }
180 head = page_buffers(page);
181 bh = head;
182 do {
183 if (buffer_dirty(bh))
184 csum_tree_block(root, bh, 0);
185 bh = bh->b_this_page;
186 } while (bh != head);
Chris Masond98237b2007-03-28 13:57:48 -0400187 return block_write_full_page(page, btree_get_block, wbc);
188}
189
190static int btree_readpage(struct file * file, struct page * page)
191{
192 return block_read_full_page(page, btree_get_block);
193}
194
195static struct address_space_operations btree_aops = {
196 .readpage = btree_readpage,
197 .writepage = btree_writepage,
198 .sync_page = block_sync_page,
199};
200
Chris Mason090d1872007-05-01 08:53:32 -0400201int readahead_tree_block(struct btrfs_root *root, u64 blocknr)
202{
203 struct buffer_head *bh = NULL;
Chris Masonde428b62007-05-18 13:28:27 -0400204 int ret = 0;
Chris Mason090d1872007-05-01 08:53:32 -0400205
206 bh = btrfs_find_create_tree_block(root, blocknr);
207 if (!bh)
208 return 0;
Chris Masonde428b62007-05-18 13:28:27 -0400209 if (buffer_uptodate(bh)) {
210 ret = 1;
Chris Mason090d1872007-05-01 08:53:32 -0400211 goto done;
Chris Masonde428b62007-05-18 13:28:27 -0400212 }
213 if (test_set_buffer_locked(bh)) {
214 ret = 1;
Chris Mason090d1872007-05-01 08:53:32 -0400215 goto done;
Chris Masonde428b62007-05-18 13:28:27 -0400216 }
Chris Mason090d1872007-05-01 08:53:32 -0400217 if (!buffer_uptodate(bh)) {
218 get_bh(bh);
219 bh->b_end_io = end_buffer_read_sync;
220 submit_bh(READ, bh);
221 } else {
222 unlock_buffer(bh);
Chris Masonde428b62007-05-18 13:28:27 -0400223 ret = 1;
Chris Mason090d1872007-05-01 08:53:32 -0400224 }
225done:
226 brelse(bh);
Chris Masonde428b62007-05-18 13:28:27 -0400227 return ret;
Chris Mason090d1872007-05-01 08:53:32 -0400228}
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;
247 } else {
248 unlock_buffer(bh);
249 }
Chris Mason9d642722007-04-03 11:43:19 -0400250uptodate:
Chris Mason090d1872007-05-01 08:53:32 -0400251 if (!buffer_checked(bh)) {
252 csum_tree_block(root, bh, 1);
253 set_buffer_checked(bh);
254 }
Chris Masond98237b2007-03-28 13:57:48 -0400255 if (check_tree_block(root, bh))
Chris Mason39279cc2007-06-12 06:35:45 -0400256 goto fail;
Chris Masond98237b2007-03-28 13:57:48 -0400257 return bh;
258fail:
259 brelse(bh);
260 return NULL;
Chris Masoneb60cea2007-02-02 09:18:22 -0500261}
262
Chris Masone089f052007-03-16 16:20:31 -0400263int dirty_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Masone20d96d2007-03-22 12:13:20 -0400264 struct buffer_head *buf)
Chris Masoned2ff2c2007-03-01 18:59:40 -0500265{
Chris Masond6025572007-03-30 14:27:56 -0400266 WARN_ON(atomic_read(&buf->b_count) == 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400267 mark_buffer_dirty(buf);
Chris Masoned2ff2c2007-03-01 18:59:40 -0500268 return 0;
269}
270
Chris Masone089f052007-03-16 16:20:31 -0400271int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Masone20d96d2007-03-22 12:13:20 -0400272 struct buffer_head *buf)
Chris Masoned2ff2c2007-03-01 18:59:40 -0500273{
Chris Masond6025572007-03-30 14:27:56 -0400274 WARN_ON(atomic_read(&buf->b_count) == 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400275 clear_buffer_dirty(buf);
Chris Masoned2ff2c2007-03-01 18:59:40 -0500276 return 0;
277}
278
Chris Mason2c90e5d2007-04-02 10:50:19 -0400279static int __setup_root(int blocksize,
Chris Mason9f5fae22007-03-20 14:38:32 -0400280 struct btrfs_root *root,
281 struct btrfs_fs_info *fs_info,
Chris Masone20d96d2007-03-22 12:13:20 -0400282 u64 objectid)
Chris Masond97e63b2007-02-20 16:40:44 -0500283{
Chris Masoncfaa7292007-02-21 17:04:57 -0500284 root->node = NULL;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400285 root->inode = NULL;
Chris Masona28ec192007-03-06 20:08:01 -0500286 root->commit_root = NULL;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400287 root->blocksize = blocksize;
Chris Mason123abc82007-03-14 14:14:43 -0400288 root->ref_cows = 0;
Chris Mason9f5fae22007-03-20 14:38:32 -0400289 root->fs_info = fs_info;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400290 root->objectid = objectid;
291 root->last_trans = 0;
Chris Mason1b05da22007-04-10 12:13:09 -0400292 root->highest_inode = 0;
293 root->last_inode_alloc = 0;
Chris Mason3768f362007-03-13 16:47:54 -0400294 memset(&root->root_key, 0, sizeof(root->root_key));
295 memset(&root->root_item, 0, sizeof(root->root_item));
Chris Mason4d775672007-04-20 20:23:12 -0400296 root->root_key.objectid = objectid;
Chris Mason3768f362007-03-13 16:47:54 -0400297 return 0;
298}
299
Chris Mason2c90e5d2007-04-02 10:50:19 -0400300static int find_and_setup_root(int blocksize,
Chris Mason9f5fae22007-03-20 14:38:32 -0400301 struct btrfs_root *tree_root,
302 struct btrfs_fs_info *fs_info,
303 u64 objectid,
Chris Masone20d96d2007-03-22 12:13:20 -0400304 struct btrfs_root *root)
Chris Mason3768f362007-03-13 16:47:54 -0400305{
306 int ret;
307
Chris Mason2c90e5d2007-04-02 10:50:19 -0400308 __setup_root(blocksize, root, fs_info, objectid);
Chris Mason3768f362007-03-13 16:47:54 -0400309 ret = btrfs_find_last_root(tree_root, objectid,
310 &root->root_item, &root->root_key);
311 BUG_ON(ret);
312
313 root->node = read_tree_block(root,
314 btrfs_root_blocknr(&root->root_item));
Chris Mason3768f362007-03-13 16:47:54 -0400315 BUG_ON(!root->node);
Chris Masond97e63b2007-02-20 16:40:44 -0500316 return 0;
317}
318
Chris Mason0f7d52f2007-04-09 10:42:37 -0400319struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
320 struct btrfs_key *location)
321{
322 struct btrfs_root *root;
323 struct btrfs_root *tree_root = fs_info->tree_root;
324 struct btrfs_path *path;
325 struct btrfs_leaf *l;
Chris Mason1b05da22007-04-10 12:13:09 -0400326 u64 highest_inode;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400327 int ret = 0;
328
Chris Mason2619ba12007-04-10 16:58:11 -0400329 root = radix_tree_lookup(&fs_info->fs_roots_radix,
330 (unsigned long)location->objectid);
Chris Mason0cf6c622007-06-09 09:22:25 -0400331 if (root)
Chris Mason2619ba12007-04-10 16:58:11 -0400332 return root;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400333 root = kmalloc(sizeof(*root), GFP_NOFS);
Chris Mason0cf6c622007-06-09 09:22:25 -0400334 if (!root)
Chris Mason0f7d52f2007-04-09 10:42:37 -0400335 return ERR_PTR(-ENOMEM);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400336 if (location->offset == (u64)-1) {
337 ret = find_and_setup_root(fs_info->sb->s_blocksize,
338 fs_info->tree_root, fs_info,
339 location->objectid, root);
340 if (ret) {
Chris Mason0f7d52f2007-04-09 10:42:37 -0400341 kfree(root);
342 return ERR_PTR(ret);
343 }
344 goto insert;
345 }
346
347 __setup_root(fs_info->sb->s_blocksize, root, fs_info,
348 location->objectid);
349
350 path = btrfs_alloc_path();
351 BUG_ON(!path);
352 ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
353 if (ret != 0) {
Chris Mason0f7d52f2007-04-09 10:42:37 -0400354 if (ret > 0)
355 ret = -ENOENT;
356 goto out;
357 }
358 l = btrfs_buffer_leaf(path->nodes[0]);
359 memcpy(&root->root_item,
360 btrfs_item_ptr(l, path->slots[0], struct btrfs_root_item),
361 sizeof(root->root_item));
362 memcpy(&root->root_key, location, sizeof(*location));
363 ret = 0;
364out:
365 btrfs_release_path(root, path);
366 btrfs_free_path(path);
367 if (ret) {
368 kfree(root);
369 return ERR_PTR(ret);
370 }
371 root->node = read_tree_block(root,
372 btrfs_root_blocknr(&root->root_item));
373 BUG_ON(!root->node);
374insert:
Chris Mason0f7d52f2007-04-09 10:42:37 -0400375 root->ref_cows = 1;
Chris Mason2619ba12007-04-10 16:58:11 -0400376 ret = radix_tree_insert(&fs_info->fs_roots_radix,
377 (unsigned long)root->root_key.objectid,
Chris Mason0f7d52f2007-04-09 10:42:37 -0400378 root);
379 if (ret) {
Chris Mason0f7d52f2007-04-09 10:42:37 -0400380 brelse(root->node);
381 kfree(root);
382 return ERR_PTR(ret);
383 }
Chris Mason1b05da22007-04-10 12:13:09 -0400384 ret = btrfs_find_highest_inode(root, &highest_inode);
385 if (ret == 0) {
386 root->highest_inode = highest_inode;
387 root->last_inode_alloc = highest_inode;
Chris Mason1b05da22007-04-10 12:13:09 -0400388 }
Chris Mason0f7d52f2007-04-09 10:42:37 -0400389 return root;
390}
391
Chris Mason2c90e5d2007-04-02 10:50:19 -0400392struct btrfs_root *open_ctree(struct super_block *sb)
Chris Masoneb60cea2007-02-02 09:18:22 -0500393{
Chris Masone20d96d2007-03-22 12:13:20 -0400394 struct btrfs_root *extent_root = kmalloc(sizeof(struct btrfs_root),
395 GFP_NOFS);
396 struct btrfs_root *tree_root = kmalloc(sizeof(struct btrfs_root),
397 GFP_NOFS);
Chris Masone20d96d2007-03-22 12:13:20 -0400398 struct btrfs_fs_info *fs_info = kmalloc(sizeof(*fs_info),
399 GFP_NOFS);
Chris Masoneb60cea2007-02-02 09:18:22 -0500400 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -0400401 int err = -EIO;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400402 struct btrfs_super_block *disk_super;
Chris Masoneb60cea2007-02-02 09:18:22 -0500403
Chris Mason39279cc2007-06-12 06:35:45 -0400404 if (!extent_root || !tree_root || !fs_info) {
405 err = -ENOMEM;
406 goto fail;
407 }
Chris Mason8ef97622007-03-26 10:15:30 -0400408 init_bit_radix(&fs_info->pinned_radix);
409 init_bit_radix(&fs_info->pending_del_radix);
Chris Masone37c9e62007-05-09 20:13:14 -0400410 init_bit_radix(&fs_info->extent_map_radix);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400411 INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -0400412 INIT_RADIX_TREE(&fs_info->block_group_radix, GFP_KERNEL);
Chris Masonbe744172007-05-06 10:15:01 -0400413 INIT_RADIX_TREE(&fs_info->block_group_data_radix, GFP_KERNEL);
Chris Mason8fd17792007-04-19 21:01:03 -0400414 INIT_LIST_HEAD(&fs_info->trans_list);
Chris Masonfacda1e2007-06-08 18:11:48 -0400415 INIT_LIST_HEAD(&fs_info->dead_roots);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400416 sb_set_blocksize(sb, 4096);
Chris Mason9f5fae22007-03-20 14:38:32 -0400417 fs_info->running_transaction = NULL;
Chris Mason9f5fae22007-03-20 14:38:32 -0400418 fs_info->tree_root = tree_root;
419 fs_info->extent_root = extent_root;
Chris Masone20d96d2007-03-22 12:13:20 -0400420 fs_info->sb = sb;
Chris Masond98237b2007-03-28 13:57:48 -0400421 fs_info->btree_inode = new_inode(sb);
422 fs_info->btree_inode->i_ino = 1;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400423 fs_info->btree_inode->i_nlink = 1;
Chris Masond98237b2007-03-28 13:57:48 -0400424 fs_info->btree_inode->i_size = sb->s_bdev->bd_inode->i_size;
425 fs_info->btree_inode->i_mapping->a_ops = &btree_aops;
Chris Masone66f7092007-04-20 13:16:02 -0400426 fs_info->do_barriers = 1;
Chris Masonf2458e12007-04-25 15:52:25 -0400427 fs_info->extent_tree_insert_nr = 0;
428 fs_info->extent_tree_prealloc_nr = 0;
Chris Masonfacda1e2007-06-08 18:11:48 -0400429 fs_info->closing = 0;
430
Chris Mason08607c12007-06-08 15:33:54 -0400431 INIT_DELAYED_WORK(&fs_info->trans_work, btrfs_transaction_cleaner);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400432 BTRFS_I(fs_info->btree_inode)->root = tree_root;
433 memset(&BTRFS_I(fs_info->btree_inode)->location, 0,
434 sizeof(struct btrfs_key));
Chris Mason22b0ebd2007-03-30 08:47:31 -0400435 insert_inode_hash(fs_info->btree_inode);
Chris Masond98237b2007-03-28 13:57:48 -0400436 mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS);
Chris Mason509659c2007-05-10 12:36:17 -0400437 fs_info->hash_tfm = crypto_alloc_hash("crc32c", 0, CRYPTO_ALG_ASYNC);
Chris Mason30ae8462007-03-29 09:59:15 -0400438 spin_lock_init(&fs_info->hash_lock);
Chris Mason39279cc2007-06-12 06:35:45 -0400439
Chris Mason30ae8462007-03-29 09:59:15 -0400440 if (!fs_info->hash_tfm || IS_ERR(fs_info->hash_tfm)) {
Chris Mason39279cc2007-06-12 06:35:45 -0400441 printk("btrfs: failed hash setup, modprobe cryptomgr?\n");
442 err = -ENOMEM;
443 goto fail_iput;
Chris Mason87cbda52007-03-28 19:44:27 -0400444 }
Chris Mason79154b12007-03-22 15:59:16 -0400445 mutex_init(&fs_info->trans_mutex);
Chris Masond561c022007-03-23 19:47:49 -0400446 mutex_init(&fs_info->fs_mutex);
Chris Mason3768f362007-03-13 16:47:54 -0400447
Chris Mason2c90e5d2007-04-02 10:50:19 -0400448 __setup_root(sb->s_blocksize, tree_root,
449 fs_info, BTRFS_ROOT_TREE_OBJECTID);
Chris Mason7eccb902007-04-11 15:53:25 -0400450
Chris Mason2c90e5d2007-04-02 10:50:19 -0400451 fs_info->sb_buffer = read_tree_block(tree_root,
452 BTRFS_SUPER_INFO_OFFSET /
453 sb->s_blocksize);
Chris Masond98237b2007-03-28 13:57:48 -0400454
Chris Mason0f7d52f2007-04-09 10:42:37 -0400455 if (!fs_info->sb_buffer)
Chris Mason39279cc2007-06-12 06:35:45 -0400456 goto fail_iput;
Chris Masond98237b2007-03-28 13:57:48 -0400457 disk_super = (struct btrfs_super_block *)fs_info->sb_buffer->b_data;
Chris Mason39279cc2007-06-12 06:35:45 -0400458
Chris Mason0f7d52f2007-04-09 10:42:37 -0400459 if (!btrfs_super_root(disk_super))
Chris Mason39279cc2007-06-12 06:35:45 -0400460 goto fail_sb_buffer;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400461
Chris Mason8352d8a2007-04-12 10:43:05 -0400462 i_size_write(fs_info->btree_inode,
463 btrfs_super_total_blocks(disk_super) <<
464 fs_info->btree_inode->i_blkbits);
465
Chris Masond98237b2007-03-28 13:57:48 -0400466 fs_info->disk_super = disk_super;
Chris Mason39279cc2007-06-12 06:35:45 -0400467
468 if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
469 sizeof(disk_super->magic))) {
470 printk("btrfs: valid FS not found on %s\n", sb->s_id);
471 goto fail_sb_buffer;
472 }
Chris Masone20d96d2007-03-22 12:13:20 -0400473 tree_root->node = read_tree_block(tree_root,
474 btrfs_super_root(disk_super));
Chris Mason39279cc2007-06-12 06:35:45 -0400475 if (!tree_root->node)
476 goto fail_sb_buffer;
Chris Mason3768f362007-03-13 16:47:54 -0400477
Chris Mason2c90e5d2007-04-02 10:50:19 -0400478 mutex_lock(&fs_info->fs_mutex);
479 ret = find_and_setup_root(sb->s_blocksize, tree_root, fs_info,
Chris Masone20d96d2007-03-22 12:13:20 -0400480 BTRFS_EXTENT_TREE_OBJECTID, extent_root);
Chris Mason39279cc2007-06-12 06:35:45 -0400481 if (ret) {
482 mutex_unlock(&fs_info->fs_mutex);
483 goto fail_tree_root;
484 }
Chris Mason3768f362007-03-13 16:47:54 -0400485
Chris Mason9078a3e2007-04-26 16:46:15 -0400486 btrfs_read_block_groups(extent_root);
487
Chris Mason0f7d52f2007-04-09 10:42:37 -0400488 fs_info->generation = btrfs_super_generation(disk_super) + 1;
Chris Mason5be6f7f2007-04-05 13:35:25 -0400489 mutex_unlock(&fs_info->fs_mutex);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400490 return tree_root;
Chris Mason39279cc2007-06-12 06:35:45 -0400491
492fail_tree_root:
493 btrfs_block_release(tree_root, tree_root->node);
494fail_sb_buffer:
495 btrfs_block_release(tree_root, fs_info->sb_buffer);
496fail_iput:
497 iput(fs_info->btree_inode);
498fail:
499 kfree(extent_root);
500 kfree(tree_root);
501 kfree(fs_info);
502 return ERR_PTR(err);
Chris Masoneb60cea2007-02-02 09:18:22 -0500503}
504
Chris Masone089f052007-03-16 16:20:31 -0400505int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason79154b12007-03-22 15:59:16 -0400506 *root)
Chris Masoncfaa7292007-02-21 17:04:57 -0500507{
Chris Masone66f7092007-04-20 13:16:02 -0400508 int ret;
Chris Masond5719762007-03-23 10:01:08 -0400509 struct buffer_head *bh = root->fs_info->sb_buffer;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400510
Chris Masond5719762007-03-23 10:01:08 -0400511 btrfs_set_super_root(root->fs_info->disk_super,
Chris Mason7eccb902007-04-11 15:53:25 -0400512 bh_blocknr(root->fs_info->tree_root->node));
Chris Masond5719762007-03-23 10:01:08 -0400513 lock_buffer(bh);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400514 WARN_ON(atomic_read(&bh->b_count) < 1);
Chris Masond5719762007-03-23 10:01:08 -0400515 clear_buffer_dirty(bh);
Chris Mason87cbda52007-03-28 19:44:27 -0400516 csum_tree_block(root, bh, 0);
Chris Masond5719762007-03-23 10:01:08 -0400517 bh->b_end_io = end_buffer_write_sync;
518 get_bh(bh);
Chris Masone66f7092007-04-20 13:16:02 -0400519 if (root->fs_info->do_barriers)
520 ret = submit_bh(WRITE_BARRIER, bh);
521 else
522 ret = submit_bh(WRITE, bh);
523 if (ret == -EOPNOTSUPP) {
524 set_buffer_uptodate(bh);
525 root->fs_info->do_barriers = 0;
526 ret = submit_bh(WRITE, bh);
527 }
Chris Masond5719762007-03-23 10:01:08 -0400528 wait_on_buffer(bh);
529 if (!buffer_uptodate(bh)) {
530 WARN_ON(1);
531 return -EIO;
Chris Masoncfaa7292007-02-21 17:04:57 -0500532 }
533 return 0;
534}
535
Chris Mason2619ba12007-04-10 16:58:11 -0400536static int free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
537{
538 radix_tree_delete(&fs_info->fs_roots_radix,
539 (unsigned long)root->root_key.objectid);
540 if (root->inode)
541 iput(root->inode);
542 if (root->node)
543 brelse(root->node);
544 if (root->commit_root)
545 brelse(root->commit_root);
546 kfree(root);
547 return 0;
548}
549
Chris Mason35b7e472007-05-02 15:53:43 -0400550static int del_fs_roots(struct btrfs_fs_info *fs_info)
Chris Mason0f7d52f2007-04-09 10:42:37 -0400551{
552 int ret;
553 struct btrfs_root *gang[8];
554 int i;
555
556 while(1) {
557 ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix,
558 (void **)gang, 0,
559 ARRAY_SIZE(gang));
560 if (!ret)
561 break;
Chris Mason2619ba12007-04-10 16:58:11 -0400562 for (i = 0; i < ret; i++)
563 free_fs_root(fs_info, gang[i]);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400564 }
565 return 0;
566}
Chris Masonb4100d62007-04-12 12:14:00 -0400567
Chris Masone20d96d2007-03-22 12:13:20 -0400568int close_ctree(struct btrfs_root *root)
Chris Masoneb60cea2007-02-02 09:18:22 -0500569{
Chris Mason3768f362007-03-13 16:47:54 -0400570 int ret;
Chris Masone089f052007-03-16 16:20:31 -0400571 struct btrfs_trans_handle *trans;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400572 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone089f052007-03-16 16:20:31 -0400573
Chris Masonfacda1e2007-06-08 18:11:48 -0400574 fs_info->closing = 1;
Chris Mason08607c12007-06-08 15:33:54 -0400575 btrfs_transaction_flush_work(root);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400576 mutex_lock(&fs_info->fs_mutex);
Chris Mason79154b12007-03-22 15:59:16 -0400577 trans = btrfs_start_transaction(root, 1);
578 btrfs_commit_transaction(trans, root);
579 /* run commit again to drop the original snapshot */
580 trans = btrfs_start_transaction(root, 1);
581 btrfs_commit_transaction(trans, root);
582 ret = btrfs_write_and_wait_transaction(NULL, root);
Chris Mason9f5fae22007-03-20 14:38:32 -0400583 BUG_ON(ret);
Chris Mason79154b12007-03-22 15:59:16 -0400584 write_ctree_super(NULL, root);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400585 mutex_unlock(&fs_info->fs_mutex);
Chris Masoned2ff2c2007-03-01 18:59:40 -0500586
Chris Mason0f7d52f2007-04-09 10:42:37 -0400587 if (fs_info->extent_root->node)
588 btrfs_block_release(fs_info->extent_root,
589 fs_info->extent_root->node);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400590 if (fs_info->tree_root->node)
591 btrfs_block_release(fs_info->tree_root,
592 fs_info->tree_root->node);
593 btrfs_block_release(root, fs_info->sb_buffer);
594 crypto_free_hash(fs_info->hash_tfm);
595 truncate_inode_pages(fs_info->btree_inode->i_mapping, 0);
596 iput(fs_info->btree_inode);
Chris Mason7eccb902007-04-11 15:53:25 -0400597
Chris Mason9078a3e2007-04-26 16:46:15 -0400598 btrfs_free_block_groups(root->fs_info);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400599 del_fs_roots(fs_info);
600 kfree(fs_info->extent_root);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400601 kfree(fs_info->tree_root);
Chris Masoneb60cea2007-02-02 09:18:22 -0500602 return 0;
603}
604
Chris Masone20d96d2007-03-22 12:13:20 -0400605void btrfs_block_release(struct btrfs_root *root, struct buffer_head *buf)
Chris Masoneb60cea2007-02-02 09:18:22 -0500606{
Chris Mason7cfcc172007-04-02 14:53:59 -0400607 brelse(buf);
Chris Masoneb60cea2007-02-02 09:18:22 -0500608}
609
Chris Mason35b7e472007-05-02 15:53:43 -0400610void btrfs_btree_balance_dirty(struct btrfs_root *root)
611{
612 balance_dirty_pages_ratelimited(root->fs_info->btree_inode->i_mapping);
613}