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