]> bbs.cooldavid.org Git - net-next-2.6.git/blame - fs/btrfs/disk-io.c
Btrfs: add compat ioctl
[net-next-2.6.git] / fs / btrfs / disk-io.c
CommitLineData
e20d96d6
CM
1#include <linux/module.h>
2#include <linux/fs.h>
d98237b3 3#include <linux/blkdev.h>
87cbda5c
CM
4#include <linux/crypto.h>
5#include <linux/scatterlist.h>
22b0ebda 6#include <linux/swap.h>
0f7d52f4 7#include <linux/radix-tree.h>
35b7e476 8#include <linux/writeback.h>
eb60ceac
CM
9#include "ctree.h"
10#include "disk-io.h"
e089f05c 11#include "transaction.h"
0f7d52f4 12#include "btrfs_inode.h"
eb60ceac 13
7eccb903
CM
14struct dev_lookup {
15 u64 block_start;
16 u64 num_blocks;
b4100d64 17 u64 device_id;
7eccb903
CM
18 struct block_device *bdev;
19};
20
8352d8a4
CM
21int btrfs_insert_dev_radix(struct btrfs_root *root,
22 struct block_device *bdev,
b4100d64 23 u64 device_id,
8352d8a4
CM
24 u64 block_start,
25 u64 num_blocks)
26{
27 struct dev_lookup *lookup;
8352d8a4
CM
28 int ret;
29
30 lookup = kmalloc(sizeof(*lookup), GFP_NOFS);
31 if (!lookup)
32 return -ENOMEM;
33 lookup->block_start = block_start;
34 lookup->num_blocks = num_blocks;
35 lookup->bdev = bdev;
b4100d64 36 lookup->device_id = device_id;
8352d8a4
CM
37
38 ret = radix_tree_insert(&root->fs_info->dev_radix, block_start +
39 num_blocks - 1, lookup);
40 return ret;
41}
42
7eccb903
CM
43u64 bh_blocknr(struct buffer_head *bh)
44{
45 int blkbits = bh->b_page->mapping->host->i_blkbits;
46 u64 blocknr = bh->b_page->index << (PAGE_CACHE_SHIFT - blkbits);
47 unsigned long offset;
48
49 if (PageHighMem(bh->b_page))
50 offset = (unsigned long)bh->b_data;
51 else
52 offset = bh->b_data - (char *)page_address(bh->b_page);
53 blocknr += offset >> (PAGE_CACHE_SHIFT - blkbits);
54 return blocknr;
55}
56
e20d96d6 57static int check_tree_block(struct btrfs_root *root, struct buffer_head *buf)
eb60ceac 58{
e20d96d6 59 struct btrfs_node *node = btrfs_buffer_node(buf);
7eccb903 60 if (bh_blocknr(buf) != btrfs_header_blocknr(&node->header)) {
8352d8a4
CM
61 printk(KERN_CRIT "bh_blocknr(buf) is %Lu, header is %Lu\n",
62 bh_blocknr(buf), btrfs_header_blocknr(&node->header));
9a8dd150 63 BUG();
d98237b3 64 }
9a8dd150 65 return 0;
eb60ceac
CM
66}
67
d98237b3
CM
68struct buffer_head *btrfs_find_tree_block(struct btrfs_root *root, u64 blocknr)
69{
70 struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
71 int blockbits = root->fs_info->sb->s_blocksize_bits;
72 unsigned long index = blocknr >> (PAGE_CACHE_SHIFT - blockbits);
73 struct page *page;
74 struct buffer_head *bh;
75 struct buffer_head *head;
76 struct buffer_head *ret = NULL;
77
2c90e5d6 78
d98237b3
CM
79 page = find_lock_page(mapping, index);
80 if (!page)
81 return NULL;
82
83 if (!page_has_buffers(page))
84 goto out_unlock;
85
86 head = page_buffers(page);
87 bh = head;
88 do {
7eccb903 89 if (buffer_mapped(bh) && bh_blocknr(bh) == blocknr) {
d98237b3
CM
90 ret = bh;
91 get_bh(bh);
92 goto out_unlock;
93 }
94 bh = bh->b_this_page;
95 } while (bh != head);
96out_unlock:
97 unlock_page(page);
98 page_cache_release(page);
99 return ret;
100}
101
8352d8a4 102int btrfs_map_bh_to_logical(struct btrfs_root *root, struct buffer_head *bh,
7eccb903
CM
103 u64 logical)
104{
105 struct dev_lookup *lookup[2];
7eccb903
CM
106
107 int ret;
108
236454df
CM
109 if (logical == 0) {
110 bh->b_bdev = NULL;
111 bh->b_blocknr = 0;
112 set_buffer_mapped(bh);
113 return 0;
114 }
7eccb903
CM
115 root = root->fs_info->dev_root;
116 ret = radix_tree_gang_lookup(&root->fs_info->dev_radix,
117 (void **)lookup,
118 (unsigned long)logical,
119 ARRAY_SIZE(lookup));
120 if (ret == 0 || lookup[0]->block_start > logical ||
121 lookup[0]->block_start + lookup[0]->num_blocks <= logical) {
122 ret = -ENOENT;
123 goto out;
124 }
125 bh->b_bdev = lookup[0]->bdev;
126 bh->b_blocknr = logical - lookup[0]->block_start;
7eccb903
CM
127 set_buffer_mapped(bh);
128 ret = 0;
129out:
130 return ret;
131}
132
d98237b3
CM
133struct buffer_head *btrfs_find_create_tree_block(struct btrfs_root *root,
134 u64 blocknr)
135{
136 struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
137 int blockbits = root->fs_info->sb->s_blocksize_bits;
138 unsigned long index = blocknr >> (PAGE_CACHE_SHIFT - blockbits);
139 struct page *page;
140 struct buffer_head *bh;
141 struct buffer_head *head;
142 struct buffer_head *ret = NULL;
7eccb903 143 int err;
d98237b3 144 u64 first_block = index << (PAGE_CACHE_SHIFT - blockbits);
22b0ebda 145
d98237b3
CM
146 page = grab_cache_page(mapping, index);
147 if (!page)
148 return NULL;
149
d98237b3
CM
150 if (!page_has_buffers(page))
151 create_empty_buffers(page, root->fs_info->sb->s_blocksize, 0);
152 head = page_buffers(page);
153 bh = head;
154 do {
155 if (!buffer_mapped(bh)) {
8352d8a4 156 err = btrfs_map_bh_to_logical(root, bh, first_block);
7eccb903 157 BUG_ON(err);
d98237b3 158 }
7eccb903 159 if (bh_blocknr(bh) == blocknr) {
d98237b3
CM
160 ret = bh;
161 get_bh(bh);
162 goto out_unlock;
163 }
164 bh = bh->b_this_page;
165 first_block++;
166 } while (bh != head);
167out_unlock:
168 unlock_page(page);
22b0ebda
CM
169 if (ret)
170 touch_buffer(ret);
d98237b3
CM
171 page_cache_release(page);
172 return ret;
173}
174
d98237b3
CM
175static int btree_get_block(struct inode *inode, sector_t iblock,
176 struct buffer_head *bh, int create)
177{
7eccb903
CM
178 int err;
179 struct btrfs_root *root = BTRFS_I(bh->b_page->mapping->host)->root;
8352d8a4 180 err = btrfs_map_bh_to_logical(root, bh, iblock);
7eccb903 181 return err;
d98237b3
CM
182}
183
f254e52c
CM
184int btrfs_csum_data(struct btrfs_root * root, char *data, size_t len,
185 char *result)
87cbda5c 186{
87cbda5c
CM
187 struct scatterlist sg;
188 struct crypto_hash *tfm = root->fs_info->hash_tfm;
189 struct hash_desc desc;
190 int ret;
87cbda5c
CM
191
192 desc.tfm = tfm;
193 desc.flags = 0;
f254e52c 194 sg_init_one(&sg, data, len);
87cbda5c 195 spin_lock(&root->fs_info->hash_lock);
22b0ebda 196 ret = crypto_hash_digest(&desc, &sg, 1, result);
87cbda5c
CM
197 spin_unlock(&root->fs_info->hash_lock);
198 if (ret) {
509659cd 199 printk("digest failed\n");
87cbda5c 200 }
f254e52c
CM
201 return ret;
202}
203static int csum_tree_block(struct btrfs_root *root, struct buffer_head *bh,
204 int verify)
205{
509659cd 206 char result[BTRFS_CRC32_SIZE];
f254e52c
CM
207 int ret;
208 struct btrfs_node *node;
209
210 ret = btrfs_csum_data(root, bh->b_data + BTRFS_CSUM_SIZE,
211 bh->b_size - BTRFS_CSUM_SIZE, result);
212 if (ret)
213 return ret;
87cbda5c 214 if (verify) {
509659cd 215 if (memcmp(bh->b_data, result, BTRFS_CRC32_SIZE)) {
7eccb903
CM
216 printk("checksum verify failed on %Lu\n",
217 bh_blocknr(bh));
f254e52c
CM
218 return 1;
219 }
220 } else {
221 node = btrfs_buffer_node(bh);
509659cd 222 memcpy(node->header.csum, result, BTRFS_CRC32_SIZE);
f254e52c 223 }
87cbda5c
CM
224 return 0;
225}
226
d98237b3 227static int btree_writepage(struct page *page, struct writeback_control *wbc)
ed2ff2cb 228{
87cbda5c 229 struct buffer_head *bh;
0f7d52f4 230 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
87cbda5c 231 struct buffer_head *head;
87cbda5c
CM
232 if (!page_has_buffers(page)) {
233 create_empty_buffers(page, root->fs_info->sb->s_blocksize,
234 (1 << BH_Dirty)|(1 << BH_Uptodate));
235 }
236 head = page_buffers(page);
237 bh = head;
238 do {
239 if (buffer_dirty(bh))
240 csum_tree_block(root, bh, 0);
241 bh = bh->b_this_page;
242 } while (bh != head);
d98237b3 243 return block_write_full_page(page, btree_get_block, wbc);
ed2ff2cb
CM
244}
245
d98237b3 246static int btree_readpage(struct file * file, struct page * page)
eb60ceac 247{
d98237b3 248 return block_read_full_page(page, btree_get_block);
eb60ceac
CM
249}
250
d98237b3
CM
251static struct address_space_operations btree_aops = {
252 .readpage = btree_readpage,
253 .writepage = btree_writepage,
254 .sync_page = block_sync_page,
255};
256
090d1875
CM
257int readahead_tree_block(struct btrfs_root *root, u64 blocknr)
258{
259 struct buffer_head *bh = NULL;
de428b63 260 int ret = 0;
090d1875
CM
261
262 bh = btrfs_find_create_tree_block(root, blocknr);
263 if (!bh)
264 return 0;
de428b63
CM
265 if (buffer_uptodate(bh)) {
266 ret = 1;
090d1875 267 goto done;
de428b63
CM
268 }
269 if (test_set_buffer_locked(bh)) {
270 ret = 1;
090d1875 271 goto done;
de428b63 272 }
090d1875
CM
273 if (!buffer_uptodate(bh)) {
274 get_bh(bh);
275 bh->b_end_io = end_buffer_read_sync;
276 submit_bh(READ, bh);
277 } else {
278 unlock_buffer(bh);
de428b63 279 ret = 1;
090d1875
CM
280 }
281done:
282 brelse(bh);
de428b63 283 return ret;
090d1875
CM
284}
285
e20d96d6 286struct buffer_head *read_tree_block(struct btrfs_root *root, u64 blocknr)
eb60ceac 287{
d98237b3 288 struct buffer_head *bh = NULL;
eb60ceac 289
d98237b3
CM
290 bh = btrfs_find_create_tree_block(root, blocknr);
291 if (!bh)
292 return bh;
9d64272c
CM
293 if (buffer_uptodate(bh))
294 goto uptodate;
d98237b3
CM
295 lock_buffer(bh);
296 if (!buffer_uptodate(bh)) {
297 get_bh(bh);
298 bh->b_end_io = end_buffer_read_sync;
299 submit_bh(READ, bh);
300 wait_on_buffer(bh);
301 if (!buffer_uptodate(bh))
302 goto fail;
303 } else {
304 unlock_buffer(bh);
305 }
9d64272c 306uptodate:
090d1875
CM
307 if (!buffer_checked(bh)) {
308 csum_tree_block(root, bh, 1);
309 set_buffer_checked(bh);
310 }
d98237b3 311 if (check_tree_block(root, bh))
cfaa7295 312 BUG();
d98237b3
CM
313 return bh;
314fail:
315 brelse(bh);
316 return NULL;
eb60ceac
CM
317}
318
e089f05c 319int dirty_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
e20d96d6 320 struct buffer_head *buf)
ed2ff2cb 321{
d6025579 322 WARN_ON(atomic_read(&buf->b_count) == 0);
e20d96d6 323 mark_buffer_dirty(buf);
ed2ff2cb
CM
324 return 0;
325}
326
e089f05c 327int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
e20d96d6 328 struct buffer_head *buf)
ed2ff2cb 329{
d6025579 330 WARN_ON(atomic_read(&buf->b_count) == 0);
e20d96d6 331 clear_buffer_dirty(buf);
ed2ff2cb
CM
332 return 0;
333}
334
2c90e5d6 335static int __setup_root(int blocksize,
9f5fae2f
CM
336 struct btrfs_root *root,
337 struct btrfs_fs_info *fs_info,
e20d96d6 338 u64 objectid)
d97e63b6 339{
cfaa7295 340 root->node = NULL;
0f7d52f4 341 root->inode = NULL;
a28ec197 342 root->commit_root = NULL;
2c90e5d6 343 root->blocksize = blocksize;
123abc88 344 root->ref_cows = 0;
9f5fae2f 345 root->fs_info = fs_info;
0f7d52f4
CM
346 root->objectid = objectid;
347 root->last_trans = 0;
1b05da2e
CM
348 root->highest_inode = 0;
349 root->last_inode_alloc = 0;
3768f368
CM
350 memset(&root->root_key, 0, sizeof(root->root_key));
351 memset(&root->root_item, 0, sizeof(root->root_item));
4d775673 352 root->root_key.objectid = objectid;
3768f368
CM
353 return 0;
354}
355
2c90e5d6 356static int find_and_setup_root(int blocksize,
9f5fae2f
CM
357 struct btrfs_root *tree_root,
358 struct btrfs_fs_info *fs_info,
359 u64 objectid,
e20d96d6 360 struct btrfs_root *root)
3768f368
CM
361{
362 int ret;
363
2c90e5d6 364 __setup_root(blocksize, root, fs_info, objectid);
3768f368
CM
365 ret = btrfs_find_last_root(tree_root, objectid,
366 &root->root_item, &root->root_key);
367 BUG_ON(ret);
368
369 root->node = read_tree_block(root,
370 btrfs_root_blocknr(&root->root_item));
3768f368 371 BUG_ON(!root->node);
d97e63b6
CM
372 return 0;
373}
374
0f7d52f4
CM
375struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
376 struct btrfs_key *location)
377{
378 struct btrfs_root *root;
379 struct btrfs_root *tree_root = fs_info->tree_root;
380 struct btrfs_path *path;
381 struct btrfs_leaf *l;
1b05da2e 382 u64 highest_inode;
0f7d52f4
CM
383 int ret = 0;
384
385printk("read_fs_root looking for %Lu %Lu %u\n", location->objectid, location->offset, location->flags);
2619ba1f
CM
386 root = radix_tree_lookup(&fs_info->fs_roots_radix,
387 (unsigned long)location->objectid);
388 if (root) {
389printk("found %p in cache\n", root);
390 return root;
391 }
0f7d52f4
CM
392 root = kmalloc(sizeof(*root), GFP_NOFS);
393 if (!root) {
394printk("failed1\n");
395 return ERR_PTR(-ENOMEM);
396 }
397 if (location->offset == (u64)-1) {
398 ret = find_and_setup_root(fs_info->sb->s_blocksize,
399 fs_info->tree_root, fs_info,
400 location->objectid, root);
401 if (ret) {
402printk("failed2\n");
403 kfree(root);
404 return ERR_PTR(ret);
405 }
406 goto insert;
407 }
408
409 __setup_root(fs_info->sb->s_blocksize, root, fs_info,
410 location->objectid);
411
412 path = btrfs_alloc_path();
413 BUG_ON(!path);
414 ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
415 if (ret != 0) {
416printk("internal search_slot gives us %d\n", ret);
417 if (ret > 0)
418 ret = -ENOENT;
419 goto out;
420 }
421 l = btrfs_buffer_leaf(path->nodes[0]);
422 memcpy(&root->root_item,
423 btrfs_item_ptr(l, path->slots[0], struct btrfs_root_item),
424 sizeof(root->root_item));
425 memcpy(&root->root_key, location, sizeof(*location));
426 ret = 0;
427out:
428 btrfs_release_path(root, path);
429 btrfs_free_path(path);
430 if (ret) {
431 kfree(root);
432 return ERR_PTR(ret);
433 }
434 root->node = read_tree_block(root,
435 btrfs_root_blocknr(&root->root_item));
436 BUG_ON(!root->node);
437insert:
438printk("inserting %p\n", root);
439 root->ref_cows = 1;
2619ba1f
CM
440 ret = radix_tree_insert(&fs_info->fs_roots_radix,
441 (unsigned long)root->root_key.objectid,
0f7d52f4
CM
442 root);
443 if (ret) {
444printk("radix_tree_insert gives us %d\n", ret);
445 brelse(root->node);
446 kfree(root);
447 return ERR_PTR(ret);
448 }
1b05da2e
CM
449 ret = btrfs_find_highest_inode(root, &highest_inode);
450 if (ret == 0) {
451 root->highest_inode = highest_inode;
452 root->last_inode_alloc = highest_inode;
453printk("highest inode is %Lu\n", highest_inode);
454 }
0f7d52f4
CM
455printk("all worked\n");
456 return root;
457}
458
b4100d64
CM
459static int btrfs_open_disk(struct btrfs_root *root, u64 device_id,
460 u64 block_start, u64 num_blocks,
461 char *filename, int name_len)
8352d8a4
CM
462{
463 char *null_filename;
464 struct block_device *bdev;
465 int ret;
466
8352d8a4
CM
467 null_filename = kmalloc(name_len + 1, GFP_NOFS);
468 if (!null_filename)
469 return -ENOMEM;
470 memcpy(null_filename, filename, name_len);
471 null_filename[name_len] = '\0';
472
473 bdev = open_bdev_excl(null_filename, O_RDWR, root->fs_info->sb);
474 if (IS_ERR(bdev)) {
475 ret = PTR_ERR(bdev);
476 goto out;
477 }
478 set_blocksize(bdev, root->fs_info->sb->s_blocksize);
b4100d64
CM
479 ret = btrfs_insert_dev_radix(root, bdev, device_id,
480 block_start, num_blocks);
8352d8a4
CM
481 BUG_ON(ret);
482 ret = 0;
483out:
484 kfree(null_filename);
485 return ret;
486}
487
488static int read_device_info(struct btrfs_root *root)
489{
490 struct btrfs_path *path;
491 int ret;
492 struct btrfs_key key;
493 struct btrfs_leaf *leaf;
494 struct btrfs_device_item *dev_item;
495 int nritems;
496 int slot;
497
498 root = root->fs_info->dev_root;
499
500 path = btrfs_alloc_path();
501 if (!path)
502 return -ENOMEM;
503 key.objectid = 0;
504 key.offset = 0;
505 key.flags = 0;
506 btrfs_set_key_type(&key, BTRFS_DEV_ITEM_KEY);
507
508 mutex_lock(&root->fs_info->fs_mutex);
509 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
510 leaf = btrfs_buffer_leaf(path->nodes[0]);
511 nritems = btrfs_header_nritems(&leaf->header);
512 while(1) {
513 slot = path->slots[0];
514 if (slot >= nritems) {
515 ret = btrfs_next_leaf(root, path);
516 if (ret)
517 break;
518 leaf = btrfs_buffer_leaf(path->nodes[0]);
519 nritems = btrfs_header_nritems(&leaf->header);
520 slot = path->slots[0];
521 }
522 btrfs_disk_key_to_cpu(&key, &leaf->items[slot].key);
523 if (btrfs_key_type(&key) != BTRFS_DEV_ITEM_KEY) {
524 path->slots[0]++;
525 continue;
526 }
527 dev_item = btrfs_item_ptr(leaf, slot, struct btrfs_device_item);
528printk("found key %Lu %Lu\n", key.objectid, key.offset);
b4100d64
CM
529 if (btrfs_device_id(dev_item) !=
530 btrfs_super_device_id(root->fs_info->disk_super)) {
531 ret = btrfs_open_disk(root, btrfs_device_id(dev_item),
532 key.objectid, key.offset,
533 (char *)(dev_item + 1),
534 btrfs_device_pathlen(dev_item));
535 BUG_ON(ret);
536 }
8352d8a4
CM
537 path->slots[0]++;
538 }
539 btrfs_free_path(path);
540 mutex_unlock(&root->fs_info->fs_mutex);
541 return 0;
542}
543
2c90e5d6 544struct btrfs_root *open_ctree(struct super_block *sb)
2e635a27 545{
e20d96d6
CM
546 struct btrfs_root *extent_root = kmalloc(sizeof(struct btrfs_root),
547 GFP_NOFS);
0bd93ba0
CM
548 struct btrfs_root *dev_root = kmalloc(sizeof(struct btrfs_root),
549 GFP_NOFS);
e20d96d6
CM
550 struct btrfs_root *tree_root = kmalloc(sizeof(struct btrfs_root),
551 GFP_NOFS);
e20d96d6
CM
552 struct btrfs_fs_info *fs_info = kmalloc(sizeof(*fs_info),
553 GFP_NOFS);
eb60ceac 554 int ret;
2c90e5d6 555 struct btrfs_super_block *disk_super;
7eccb903 556 struct dev_lookup *dev_lookup;
eb60ceac 557
8ef97622
CM
558 init_bit_radix(&fs_info->pinned_radix);
559 init_bit_radix(&fs_info->pending_del_radix);
e37c9e69 560 init_bit_radix(&fs_info->extent_map_radix);
0f7d52f4 561 INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_NOFS);
7eccb903 562 INIT_RADIX_TREE(&fs_info->dev_radix, GFP_NOFS);
9078a3e1 563 INIT_RADIX_TREE(&fs_info->block_group_radix, GFP_KERNEL);
be744175 564 INIT_RADIX_TREE(&fs_info->block_group_data_radix, GFP_KERNEL);
8fd17795 565 INIT_LIST_HEAD(&fs_info->trans_list);
2c90e5d6 566 sb_set_blocksize(sb, 4096);
9f5fae2f 567 fs_info->running_transaction = NULL;
9f5fae2f
CM
568 fs_info->tree_root = tree_root;
569 fs_info->extent_root = extent_root;
0bd93ba0 570 fs_info->dev_root = dev_root;
e20d96d6 571 fs_info->sb = sb;
d98237b3
CM
572 fs_info->btree_inode = new_inode(sb);
573 fs_info->btree_inode->i_ino = 1;
2c90e5d6 574 fs_info->btree_inode->i_nlink = 1;
d98237b3
CM
575 fs_info->btree_inode->i_size = sb->s_bdev->bd_inode->i_size;
576 fs_info->btree_inode->i_mapping->a_ops = &btree_aops;
e66f709b 577 fs_info->do_barriers = 1;
f2458e1d
CM
578 fs_info->extent_tree_insert_nr = 0;
579 fs_info->extent_tree_prealloc_nr = 0;
08607c1b 580 INIT_DELAYED_WORK(&fs_info->trans_work, btrfs_transaction_cleaner);
0f7d52f4
CM
581 BTRFS_I(fs_info->btree_inode)->root = tree_root;
582 memset(&BTRFS_I(fs_info->btree_inode)->location, 0,
583 sizeof(struct btrfs_key));
22b0ebda 584 insert_inode_hash(fs_info->btree_inode);
d98237b3 585 mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS);
509659cd 586 fs_info->hash_tfm = crypto_alloc_hash("crc32c", 0, CRYPTO_ALG_ASYNC);
30ae8467 587 spin_lock_init(&fs_info->hash_lock);
30ae8467 588 if (!fs_info->hash_tfm || IS_ERR(fs_info->hash_tfm)) {
509659cd 589 printk("failed to allocate digest hash\n");
87cbda5c
CM
590 return NULL;
591 }
79154b1b 592 mutex_init(&fs_info->trans_mutex);
d561c025 593 mutex_init(&fs_info->fs_mutex);
3768f368 594
0bd93ba0
CM
595 __setup_root(sb->s_blocksize, dev_root,
596 fs_info, BTRFS_DEV_TREE_OBJECTID);
597
2c90e5d6
CM
598 __setup_root(sb->s_blocksize, tree_root,
599 fs_info, BTRFS_ROOT_TREE_OBJECTID);
7eccb903
CM
600
601 dev_lookup = kmalloc(sizeof(*dev_lookup), GFP_NOFS);
602 dev_lookup->block_start = 0;
603 dev_lookup->num_blocks = (u32)-2;
604 dev_lookup->bdev = sb->s_bdev;
b4100d64 605 dev_lookup->device_id = 0;
7eccb903
CM
606 ret = radix_tree_insert(&fs_info->dev_radix, (u32)-2, dev_lookup);
607 BUG_ON(ret);
2c90e5d6
CM
608 fs_info->sb_buffer = read_tree_block(tree_root,
609 BTRFS_SUPER_INFO_OFFSET /
610 sb->s_blocksize);
d98237b3 611
0f7d52f4 612 if (!fs_info->sb_buffer)
d98237b3 613 return NULL;
d98237b3 614 disk_super = (struct btrfs_super_block *)fs_info->sb_buffer->b_data;
0f7d52f4 615 if (!btrfs_super_root(disk_super))
2c90e5d6 616 return NULL;
0f7d52f4 617
8352d8a4
CM
618 i_size_write(fs_info->btree_inode,
619 btrfs_super_total_blocks(disk_super) <<
620 fs_info->btree_inode->i_blkbits);
621
7eccb903
CM
622 radix_tree_delete(&fs_info->dev_radix, (u32)-2);
623 dev_lookup->block_start = btrfs_super_device_block_start(disk_super);
624 dev_lookup->num_blocks = btrfs_super_device_num_blocks(disk_super);
b4100d64
CM
625 dev_lookup->device_id = btrfs_super_device_id(disk_super);
626
7eccb903
CM
627 ret = radix_tree_insert(&fs_info->dev_radix,
628 dev_lookup->block_start +
8352d8a4 629 dev_lookup->num_blocks - 1, dev_lookup);
7eccb903
CM
630 BUG_ON(ret);
631
d98237b3 632 fs_info->disk_super = disk_super;
8352d8a4 633
0bd93ba0
CM
634 dev_root->node = read_tree_block(tree_root,
635 btrfs_super_device_root(disk_super));
8352d8a4
CM
636
637 ret = read_device_info(dev_root);
638 BUG_ON(ret);
639
e20d96d6
CM
640 tree_root->node = read_tree_block(tree_root,
641 btrfs_super_root(disk_super));
3768f368
CM
642 BUG_ON(!tree_root->node);
643
2c90e5d6
CM
644 mutex_lock(&fs_info->fs_mutex);
645 ret = find_and_setup_root(sb->s_blocksize, tree_root, fs_info,
e20d96d6 646 BTRFS_EXTENT_TREE_OBJECTID, extent_root);
3768f368
CM
647 BUG_ON(ret);
648
9078a3e1
CM
649 btrfs_read_block_groups(extent_root);
650
0f7d52f4 651 fs_info->generation = btrfs_super_generation(disk_super) + 1;
5be6f7f1 652 mutex_unlock(&fs_info->fs_mutex);
0f7d52f4 653 return tree_root;
eb60ceac
CM
654}
655
e089f05c 656int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root
79154b1b 657 *root)
eb60ceac 658{
e66f709b 659 int ret;
d5719762 660 struct buffer_head *bh = root->fs_info->sb_buffer;
2c90e5d6 661
d5719762 662 btrfs_set_super_root(root->fs_info->disk_super,
7eccb903 663 bh_blocknr(root->fs_info->tree_root->node));
d5719762 664 lock_buffer(bh);
2c90e5d6 665 WARN_ON(atomic_read(&bh->b_count) < 1);
d5719762 666 clear_buffer_dirty(bh);
87cbda5c 667 csum_tree_block(root, bh, 0);
d5719762
CM
668 bh->b_end_io = end_buffer_write_sync;
669 get_bh(bh);
e66f709b
CM
670 if (root->fs_info->do_barriers)
671 ret = submit_bh(WRITE_BARRIER, bh);
672 else
673 ret = submit_bh(WRITE, bh);
674 if (ret == -EOPNOTSUPP) {
675 set_buffer_uptodate(bh);
676 root->fs_info->do_barriers = 0;
677 ret = submit_bh(WRITE, bh);
678 }
d5719762
CM
679 wait_on_buffer(bh);
680 if (!buffer_uptodate(bh)) {
681 WARN_ON(1);
682 return -EIO;
cfaa7295
CM
683 }
684 return 0;
685}
686
2619ba1f
CM
687static int free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
688{
689 radix_tree_delete(&fs_info->fs_roots_radix,
690 (unsigned long)root->root_key.objectid);
691 if (root->inode)
692 iput(root->inode);
693 if (root->node)
694 brelse(root->node);
695 if (root->commit_root)
696 brelse(root->commit_root);
697 kfree(root);
698 return 0;
699}
700
35b7e476 701static int del_fs_roots(struct btrfs_fs_info *fs_info)
0f7d52f4
CM
702{
703 int ret;
704 struct btrfs_root *gang[8];
705 int i;
706
707 while(1) {
708 ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix,
709 (void **)gang, 0,
710 ARRAY_SIZE(gang));
711 if (!ret)
712 break;
2619ba1f
CM
713 for (i = 0; i < ret; i++)
714 free_fs_root(fs_info, gang[i]);
0f7d52f4
CM
715 }
716 return 0;
717}
b4100d64 718
7eccb903
CM
719static int free_dev_radix(struct btrfs_fs_info *fs_info)
720{
721 struct dev_lookup *lookup[8];
722 struct block_device *super_bdev = fs_info->sb->s_bdev;
723 int ret;
724 int i;
725 while(1) {
726 ret = radix_tree_gang_lookup(&fs_info->dev_radix,
727 (void **)lookup, 0,
728 ARRAY_SIZE(lookup));
729 if (!ret)
730 break;
731 for (i = 0; i < ret; i++) {
732 if (lookup[i]->bdev != super_bdev)
733 close_bdev_excl(lookup[i]->bdev);
734 radix_tree_delete(&fs_info->dev_radix,
735 lookup[i]->block_start +
8352d8a4 736 lookup[i]->num_blocks - 1);
7eccb903
CM
737 kfree(lookup[i]);
738 }
739 }
740 return 0;
741}
0f7d52f4 742
e20d96d6 743int close_ctree(struct btrfs_root *root)
cfaa7295 744{
3768f368 745 int ret;
e089f05c 746 struct btrfs_trans_handle *trans;
0f7d52f4 747 struct btrfs_fs_info *fs_info = root->fs_info;
e089f05c 748
08607c1b 749 btrfs_transaction_flush_work(root);
0f7d52f4 750 mutex_lock(&fs_info->fs_mutex);
79154b1b
CM
751 trans = btrfs_start_transaction(root, 1);
752 btrfs_commit_transaction(trans, root);
753 /* run commit again to drop the original snapshot */
754 trans = btrfs_start_transaction(root, 1);
755 btrfs_commit_transaction(trans, root);
756 ret = btrfs_write_and_wait_transaction(NULL, root);
3768f368 757 BUG_ON(ret);
79154b1b 758 write_ctree_super(NULL, root);
0f7d52f4
CM
759 mutex_unlock(&fs_info->fs_mutex);
760
761 if (fs_info->extent_root->node)
762 btrfs_block_release(fs_info->extent_root,
763 fs_info->extent_root->node);
0bd93ba0
CM
764 if (fs_info->dev_root->node)
765 btrfs_block_release(fs_info->dev_root,
766 fs_info->dev_root->node);
0f7d52f4
CM
767 if (fs_info->tree_root->node)
768 btrfs_block_release(fs_info->tree_root,
769 fs_info->tree_root->node);
770 btrfs_block_release(root, fs_info->sb_buffer);
771 crypto_free_hash(fs_info->hash_tfm);
772 truncate_inode_pages(fs_info->btree_inode->i_mapping, 0);
773 iput(fs_info->btree_inode);
7eccb903
CM
774
775 free_dev_radix(fs_info);
9078a3e1 776 btrfs_free_block_groups(root->fs_info);
0f7d52f4
CM
777 del_fs_roots(fs_info);
778 kfree(fs_info->extent_root);
0f7d52f4 779 kfree(fs_info->tree_root);
eb60ceac
CM
780 return 0;
781}
782
e20d96d6 783void btrfs_block_release(struct btrfs_root *root, struct buffer_head *buf)
eb60ceac 784{
7cfcc17e 785 brelse(buf);
eb60ceac
CM
786}
787
35b7e476
CM
788void btrfs_btree_balance_dirty(struct btrfs_root *root)
789{
790 balance_dirty_pages_ratelimited(root->fs_info->btree_inode->i_mapping);
791}