| 1 | #include <linux/module.h> |
| 2 | #include <linux/fs.h> |
| 3 | #include <linux/blkdev.h> |
| 4 | #include <linux/crypto.h> |
| 5 | #include <linux/scatterlist.h> |
| 6 | #include <linux/swap.h> |
| 7 | #include <linux/radix-tree.h> |
| 8 | #include "ctree.h" |
| 9 | #include "disk-io.h" |
| 10 | #include "transaction.h" |
| 11 | #include "btrfs_inode.h" |
| 12 | |
| 13 | struct dev_lookup { |
| 14 | u64 block_start; |
| 15 | u64 num_blocks; |
| 16 | struct block_device *bdev; |
| 17 | }; |
| 18 | |
| 19 | u64 bh_blocknr(struct buffer_head *bh) |
| 20 | { |
| 21 | int blkbits = bh->b_page->mapping->host->i_blkbits; |
| 22 | u64 blocknr = bh->b_page->index << (PAGE_CACHE_SHIFT - blkbits); |
| 23 | unsigned long offset; |
| 24 | |
| 25 | if (PageHighMem(bh->b_page)) |
| 26 | offset = (unsigned long)bh->b_data; |
| 27 | else |
| 28 | offset = bh->b_data - (char *)page_address(bh->b_page); |
| 29 | blocknr += offset >> (PAGE_CACHE_SHIFT - blkbits); |
| 30 | return blocknr; |
| 31 | } |
| 32 | |
| 33 | static int check_tree_block(struct btrfs_root *root, struct buffer_head *buf) |
| 34 | { |
| 35 | struct btrfs_node *node = btrfs_buffer_node(buf); |
| 36 | if (bh_blocknr(buf) != btrfs_header_blocknr(&node->header)) { |
| 37 | BUG(); |
| 38 | } |
| 39 | return 0; |
| 40 | } |
| 41 | |
| 42 | struct buffer_head *btrfs_find_tree_block(struct btrfs_root *root, u64 blocknr) |
| 43 | { |
| 44 | struct address_space *mapping = root->fs_info->btree_inode->i_mapping; |
| 45 | int blockbits = root->fs_info->sb->s_blocksize_bits; |
| 46 | unsigned long index = blocknr >> (PAGE_CACHE_SHIFT - blockbits); |
| 47 | struct page *page; |
| 48 | struct buffer_head *bh; |
| 49 | struct buffer_head *head; |
| 50 | struct buffer_head *ret = NULL; |
| 51 | |
| 52 | |
| 53 | page = find_lock_page(mapping, index); |
| 54 | if (!page) |
| 55 | return NULL; |
| 56 | |
| 57 | if (!page_has_buffers(page)) |
| 58 | goto out_unlock; |
| 59 | |
| 60 | head = page_buffers(page); |
| 61 | bh = head; |
| 62 | do { |
| 63 | if (buffer_mapped(bh) && bh_blocknr(bh) == blocknr) { |
| 64 | ret = bh; |
| 65 | get_bh(bh); |
| 66 | goto out_unlock; |
| 67 | } |
| 68 | bh = bh->b_this_page; |
| 69 | } while (bh != head); |
| 70 | out_unlock: |
| 71 | unlock_page(page); |
| 72 | if (ret) { |
| 73 | touch_buffer(ret); |
| 74 | } |
| 75 | page_cache_release(page); |
| 76 | return ret; |
| 77 | } |
| 78 | |
| 79 | static int map_bh_to_logical(struct btrfs_root *root, struct buffer_head *bh, |
| 80 | u64 logical) |
| 81 | { |
| 82 | struct dev_lookup *lookup[2]; |
| 83 | char b[BDEVNAME_SIZE]; |
| 84 | |
| 85 | int ret; |
| 86 | |
| 87 | root = root->fs_info->dev_root; |
| 88 | ret = radix_tree_gang_lookup(&root->fs_info->dev_radix, |
| 89 | (void **)lookup, |
| 90 | (unsigned long)logical, |
| 91 | ARRAY_SIZE(lookup)); |
| 92 | if (ret == 0 || lookup[0]->block_start > logical || |
| 93 | lookup[0]->block_start + lookup[0]->num_blocks <= logical) { |
| 94 | ret = -ENOENT; |
| 95 | goto out; |
| 96 | } |
| 97 | bh->b_bdev = lookup[0]->bdev; |
| 98 | bh->b_blocknr = logical - lookup[0]->block_start; |
| 99 | printk("logical mapping %Lu to %lu bdev %s\n", logical, bh->b_blocknr, bdevname(bh->b_bdev, b)); |
| 100 | set_buffer_mapped(bh); |
| 101 | ret = 0; |
| 102 | out: |
| 103 | return ret; |
| 104 | } |
| 105 | |
| 106 | struct buffer_head *btrfs_find_create_tree_block(struct btrfs_root *root, |
| 107 | u64 blocknr) |
| 108 | { |
| 109 | struct address_space *mapping = root->fs_info->btree_inode->i_mapping; |
| 110 | int blockbits = root->fs_info->sb->s_blocksize_bits; |
| 111 | unsigned long index = blocknr >> (PAGE_CACHE_SHIFT - blockbits); |
| 112 | struct page *page; |
| 113 | struct buffer_head *bh; |
| 114 | struct buffer_head *head; |
| 115 | struct buffer_head *ret = NULL; |
| 116 | int err; |
| 117 | u64 first_block = index << (PAGE_CACHE_SHIFT - blockbits); |
| 118 | |
| 119 | page = grab_cache_page(mapping, index); |
| 120 | if (!page) |
| 121 | return NULL; |
| 122 | |
| 123 | if (!page_has_buffers(page)) |
| 124 | create_empty_buffers(page, root->fs_info->sb->s_blocksize, 0); |
| 125 | head = page_buffers(page); |
| 126 | bh = head; |
| 127 | do { |
| 128 | if (!buffer_mapped(bh)) { |
| 129 | err = map_bh_to_logical(root, bh, first_block); |
| 130 | BUG_ON(err); |
| 131 | } |
| 132 | if (bh_blocknr(bh) == blocknr) { |
| 133 | ret = bh; |
| 134 | get_bh(bh); |
| 135 | goto out_unlock; |
| 136 | } |
| 137 | bh = bh->b_this_page; |
| 138 | first_block++; |
| 139 | } while (bh != head); |
| 140 | out_unlock: |
| 141 | unlock_page(page); |
| 142 | if (ret) |
| 143 | touch_buffer(ret); |
| 144 | page_cache_release(page); |
| 145 | return ret; |
| 146 | } |
| 147 | |
| 148 | static int btree_get_block(struct inode *inode, sector_t iblock, |
| 149 | struct buffer_head *bh, int create) |
| 150 | { |
| 151 | int err; |
| 152 | struct btrfs_root *root = BTRFS_I(bh->b_page->mapping->host)->root; |
| 153 | err = map_bh_to_logical(root, bh, iblock); |
| 154 | return err; |
| 155 | } |
| 156 | |
| 157 | int btrfs_csum_data(struct btrfs_root * root, char *data, size_t len, |
| 158 | char *result) |
| 159 | { |
| 160 | struct scatterlist sg; |
| 161 | struct crypto_hash *tfm = root->fs_info->hash_tfm; |
| 162 | struct hash_desc desc; |
| 163 | int ret; |
| 164 | |
| 165 | desc.tfm = tfm; |
| 166 | desc.flags = 0; |
| 167 | sg_init_one(&sg, data, len); |
| 168 | spin_lock(&root->fs_info->hash_lock); |
| 169 | ret = crypto_hash_digest(&desc, &sg, 1, result); |
| 170 | spin_unlock(&root->fs_info->hash_lock); |
| 171 | if (ret) { |
| 172 | printk("sha256 digest failed\n"); |
| 173 | } |
| 174 | return ret; |
| 175 | } |
| 176 | static int csum_tree_block(struct btrfs_root *root, struct buffer_head *bh, |
| 177 | int verify) |
| 178 | { |
| 179 | char result[BTRFS_CSUM_SIZE]; |
| 180 | int ret; |
| 181 | struct btrfs_node *node; |
| 182 | |
| 183 | ret = btrfs_csum_data(root, bh->b_data + BTRFS_CSUM_SIZE, |
| 184 | bh->b_size - BTRFS_CSUM_SIZE, result); |
| 185 | if (ret) |
| 186 | return ret; |
| 187 | if (verify) { |
| 188 | if (memcmp(bh->b_data, result, BTRFS_CSUM_SIZE)) { |
| 189 | printk("checksum verify failed on %Lu\n", |
| 190 | bh_blocknr(bh)); |
| 191 | return 1; |
| 192 | } |
| 193 | } else { |
| 194 | node = btrfs_buffer_node(bh); |
| 195 | memcpy(node->header.csum, result, BTRFS_CSUM_SIZE); |
| 196 | } |
| 197 | return 0; |
| 198 | } |
| 199 | |
| 200 | static int btree_writepage(struct page *page, struct writeback_control *wbc) |
| 201 | { |
| 202 | struct buffer_head *bh; |
| 203 | struct btrfs_root *root = BTRFS_I(page->mapping->host)->root; |
| 204 | struct buffer_head *head; |
| 205 | if (!page_has_buffers(page)) { |
| 206 | create_empty_buffers(page, root->fs_info->sb->s_blocksize, |
| 207 | (1 << BH_Dirty)|(1 << BH_Uptodate)); |
| 208 | } |
| 209 | head = page_buffers(page); |
| 210 | bh = head; |
| 211 | do { |
| 212 | if (buffer_dirty(bh)) |
| 213 | csum_tree_block(root, bh, 0); |
| 214 | bh = bh->b_this_page; |
| 215 | } while (bh != head); |
| 216 | return block_write_full_page(page, btree_get_block, wbc); |
| 217 | } |
| 218 | |
| 219 | static int btree_readpage(struct file * file, struct page * page) |
| 220 | { |
| 221 | return block_read_full_page(page, btree_get_block); |
| 222 | } |
| 223 | |
| 224 | static struct address_space_operations btree_aops = { |
| 225 | .readpage = btree_readpage, |
| 226 | .writepage = btree_writepage, |
| 227 | .sync_page = block_sync_page, |
| 228 | }; |
| 229 | |
| 230 | struct buffer_head *read_tree_block(struct btrfs_root *root, u64 blocknr) |
| 231 | { |
| 232 | struct buffer_head *bh = NULL; |
| 233 | |
| 234 | bh = btrfs_find_create_tree_block(root, blocknr); |
| 235 | if (!bh) |
| 236 | return bh; |
| 237 | if (buffer_uptodate(bh)) |
| 238 | goto uptodate; |
| 239 | 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 | csum_tree_block(root, bh, 1); |
| 248 | } else { |
| 249 | unlock_buffer(bh); |
| 250 | } |
| 251 | uptodate: |
| 252 | if (check_tree_block(root, bh)) |
| 253 | BUG(); |
| 254 | return bh; |
| 255 | fail: |
| 256 | brelse(bh); |
| 257 | return NULL; |
| 258 | } |
| 259 | |
| 260 | int dirty_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
| 261 | struct buffer_head *buf) |
| 262 | { |
| 263 | WARN_ON(atomic_read(&buf->b_count) == 0); |
| 264 | mark_buffer_dirty(buf); |
| 265 | return 0; |
| 266 | } |
| 267 | |
| 268 | int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
| 269 | struct buffer_head *buf) |
| 270 | { |
| 271 | WARN_ON(atomic_read(&buf->b_count) == 0); |
| 272 | clear_buffer_dirty(buf); |
| 273 | return 0; |
| 274 | } |
| 275 | |
| 276 | static int __setup_root(int blocksize, |
| 277 | struct btrfs_root *root, |
| 278 | struct btrfs_fs_info *fs_info, |
| 279 | u64 objectid) |
| 280 | { |
| 281 | root->node = NULL; |
| 282 | root->inode = NULL; |
| 283 | root->commit_root = NULL; |
| 284 | root->blocksize = blocksize; |
| 285 | root->ref_cows = 0; |
| 286 | root->fs_info = fs_info; |
| 287 | root->objectid = objectid; |
| 288 | root->last_trans = 0; |
| 289 | root->highest_inode = 0; |
| 290 | root->last_inode_alloc = 0; |
| 291 | memset(&root->root_key, 0, sizeof(root->root_key)); |
| 292 | memset(&root->root_item, 0, sizeof(root->root_item)); |
| 293 | return 0; |
| 294 | } |
| 295 | |
| 296 | static int find_and_setup_root(int blocksize, |
| 297 | struct btrfs_root *tree_root, |
| 298 | struct btrfs_fs_info *fs_info, |
| 299 | u64 objectid, |
| 300 | struct btrfs_root *root) |
| 301 | { |
| 302 | int ret; |
| 303 | |
| 304 | __setup_root(blocksize, root, fs_info, objectid); |
| 305 | ret = btrfs_find_last_root(tree_root, objectid, |
| 306 | &root->root_item, &root->root_key); |
| 307 | BUG_ON(ret); |
| 308 | |
| 309 | root->node = read_tree_block(root, |
| 310 | btrfs_root_blocknr(&root->root_item)); |
| 311 | BUG_ON(!root->node); |
| 312 | return 0; |
| 313 | } |
| 314 | |
| 315 | struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info, |
| 316 | struct btrfs_key *location) |
| 317 | { |
| 318 | struct btrfs_root *root; |
| 319 | struct btrfs_root *tree_root = fs_info->tree_root; |
| 320 | struct btrfs_path *path; |
| 321 | struct btrfs_leaf *l; |
| 322 | u64 highest_inode; |
| 323 | int ret = 0; |
| 324 | |
| 325 | printk("read_fs_root looking for %Lu %Lu %u\n", location->objectid, location->offset, location->flags); |
| 326 | root = radix_tree_lookup(&fs_info->fs_roots_radix, |
| 327 | (unsigned long)location->objectid); |
| 328 | if (root) { |
| 329 | printk("found %p in cache\n", root); |
| 330 | return root; |
| 331 | } |
| 332 | root = kmalloc(sizeof(*root), GFP_NOFS); |
| 333 | if (!root) { |
| 334 | printk("failed1\n"); |
| 335 | return ERR_PTR(-ENOMEM); |
| 336 | } |
| 337 | if (location->offset == (u64)-1) { |
| 338 | ret = find_and_setup_root(fs_info->sb->s_blocksize, |
| 339 | fs_info->tree_root, fs_info, |
| 340 | location->objectid, root); |
| 341 | if (ret) { |
| 342 | printk("failed2\n"); |
| 343 | 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) { |
| 356 | printk("internal search_slot gives us %d\n", ret); |
| 357 | if (ret > 0) |
| 358 | ret = -ENOENT; |
| 359 | goto out; |
| 360 | } |
| 361 | l = btrfs_buffer_leaf(path->nodes[0]); |
| 362 | memcpy(&root->root_item, |
| 363 | btrfs_item_ptr(l, path->slots[0], struct btrfs_root_item), |
| 364 | sizeof(root->root_item)); |
| 365 | memcpy(&root->root_key, location, sizeof(*location)); |
| 366 | ret = 0; |
| 367 | out: |
| 368 | btrfs_release_path(root, path); |
| 369 | btrfs_free_path(path); |
| 370 | if (ret) { |
| 371 | kfree(root); |
| 372 | return ERR_PTR(ret); |
| 373 | } |
| 374 | root->node = read_tree_block(root, |
| 375 | btrfs_root_blocknr(&root->root_item)); |
| 376 | BUG_ON(!root->node); |
| 377 | insert: |
| 378 | printk("inserting %p\n", root); |
| 379 | root->ref_cows = 1; |
| 380 | ret = radix_tree_insert(&fs_info->fs_roots_radix, |
| 381 | (unsigned long)root->root_key.objectid, |
| 382 | root); |
| 383 | if (ret) { |
| 384 | printk("radix_tree_insert gives us %d\n", ret); |
| 385 | brelse(root->node); |
| 386 | kfree(root); |
| 387 | return ERR_PTR(ret); |
| 388 | } |
| 389 | ret = btrfs_find_highest_inode(root, &highest_inode); |
| 390 | if (ret == 0) { |
| 391 | root->highest_inode = highest_inode; |
| 392 | root->last_inode_alloc = highest_inode; |
| 393 | printk("highest inode is %Lu\n", highest_inode); |
| 394 | } |
| 395 | printk("all worked\n"); |
| 396 | return root; |
| 397 | } |
| 398 | |
| 399 | struct btrfs_root *open_ctree(struct super_block *sb) |
| 400 | { |
| 401 | struct btrfs_root *extent_root = kmalloc(sizeof(struct btrfs_root), |
| 402 | GFP_NOFS); |
| 403 | struct btrfs_root *dev_root = kmalloc(sizeof(struct btrfs_root), |
| 404 | GFP_NOFS); |
| 405 | struct btrfs_root *tree_root = kmalloc(sizeof(struct btrfs_root), |
| 406 | GFP_NOFS); |
| 407 | struct btrfs_fs_info *fs_info = kmalloc(sizeof(*fs_info), |
| 408 | GFP_NOFS); |
| 409 | int ret; |
| 410 | struct btrfs_super_block *disk_super; |
| 411 | struct dev_lookup *dev_lookup; |
| 412 | |
| 413 | init_bit_radix(&fs_info->pinned_radix); |
| 414 | init_bit_radix(&fs_info->pending_del_radix); |
| 415 | INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_NOFS); |
| 416 | INIT_RADIX_TREE(&fs_info->dev_radix, GFP_NOFS); |
| 417 | sb_set_blocksize(sb, 4096); |
| 418 | fs_info->running_transaction = NULL; |
| 419 | fs_info->tree_root = tree_root; |
| 420 | fs_info->extent_root = extent_root; |
| 421 | fs_info->dev_root = dev_root; |
| 422 | fs_info->sb = sb; |
| 423 | fs_info->btree_inode = new_inode(sb); |
| 424 | fs_info->btree_inode->i_ino = 1; |
| 425 | fs_info->btree_inode->i_nlink = 1; |
| 426 | fs_info->btree_inode->i_size = sb->s_bdev->bd_inode->i_size; |
| 427 | fs_info->btree_inode->i_mapping->a_ops = &btree_aops; |
| 428 | BTRFS_I(fs_info->btree_inode)->root = tree_root; |
| 429 | memset(&BTRFS_I(fs_info->btree_inode)->location, 0, |
| 430 | sizeof(struct btrfs_key)); |
| 431 | insert_inode_hash(fs_info->btree_inode); |
| 432 | mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS); |
| 433 | fs_info->hash_tfm = crypto_alloc_hash("sha256", 0, CRYPTO_ALG_ASYNC); |
| 434 | spin_lock_init(&fs_info->hash_lock); |
| 435 | if (!fs_info->hash_tfm || IS_ERR(fs_info->hash_tfm)) { |
| 436 | printk("failed to allocate sha256 hash\n"); |
| 437 | return NULL; |
| 438 | } |
| 439 | mutex_init(&fs_info->trans_mutex); |
| 440 | mutex_init(&fs_info->fs_mutex); |
| 441 | memset(&fs_info->current_insert, 0, sizeof(fs_info->current_insert)); |
| 442 | memset(&fs_info->last_insert, 0, sizeof(fs_info->last_insert)); |
| 443 | |
| 444 | __setup_root(sb->s_blocksize, dev_root, |
| 445 | fs_info, BTRFS_DEV_TREE_OBJECTID); |
| 446 | |
| 447 | __setup_root(sb->s_blocksize, tree_root, |
| 448 | fs_info, BTRFS_ROOT_TREE_OBJECTID); |
| 449 | |
| 450 | dev_lookup = kmalloc(sizeof(*dev_lookup), GFP_NOFS); |
| 451 | dev_lookup->block_start = 0; |
| 452 | dev_lookup->num_blocks = (u32)-2; |
| 453 | dev_lookup->bdev = sb->s_bdev; |
| 454 | ret = radix_tree_insert(&fs_info->dev_radix, (u32)-2, dev_lookup); |
| 455 | BUG_ON(ret); |
| 456 | fs_info->sb_buffer = read_tree_block(tree_root, |
| 457 | BTRFS_SUPER_INFO_OFFSET / |
| 458 | sb->s_blocksize); |
| 459 | |
| 460 | if (!fs_info->sb_buffer) |
| 461 | return NULL; |
| 462 | disk_super = (struct btrfs_super_block *)fs_info->sb_buffer->b_data; |
| 463 | if (!btrfs_super_root(disk_super)) |
| 464 | return NULL; |
| 465 | |
| 466 | radix_tree_delete(&fs_info->dev_radix, (u32)-2); |
| 467 | dev_lookup->block_start = btrfs_super_device_block_start(disk_super); |
| 468 | dev_lookup->num_blocks = btrfs_super_device_num_blocks(disk_super); |
| 469 | ret = radix_tree_insert(&fs_info->dev_radix, |
| 470 | dev_lookup->block_start + |
| 471 | dev_lookup->num_blocks, dev_lookup); |
| 472 | BUG_ON(ret); |
| 473 | |
| 474 | fs_info->disk_super = disk_super; |
| 475 | dev_root->node = read_tree_block(tree_root, |
| 476 | btrfs_super_device_root(disk_super)); |
| 477 | tree_root->node = read_tree_block(tree_root, |
| 478 | btrfs_super_root(disk_super)); |
| 479 | BUG_ON(!tree_root->node); |
| 480 | |
| 481 | mutex_lock(&fs_info->fs_mutex); |
| 482 | ret = find_and_setup_root(sb->s_blocksize, tree_root, fs_info, |
| 483 | BTRFS_EXTENT_TREE_OBJECTID, extent_root); |
| 484 | BUG_ON(ret); |
| 485 | |
| 486 | fs_info->generation = btrfs_super_generation(disk_super) + 1; |
| 487 | memset(&fs_info->kobj, 0, sizeof(fs_info->kobj)); |
| 488 | kobj_set_kset_s(fs_info, btrfs_subsys); |
| 489 | kobject_set_name(&fs_info->kobj, "%s", sb->s_id); |
| 490 | kobject_register(&fs_info->kobj); |
| 491 | mutex_unlock(&fs_info->fs_mutex); |
| 492 | return tree_root; |
| 493 | } |
| 494 | |
| 495 | int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root |
| 496 | *root) |
| 497 | { |
| 498 | struct buffer_head *bh = root->fs_info->sb_buffer; |
| 499 | |
| 500 | btrfs_set_super_root(root->fs_info->disk_super, |
| 501 | bh_blocknr(root->fs_info->tree_root->node)); |
| 502 | lock_buffer(bh); |
| 503 | WARN_ON(atomic_read(&bh->b_count) < 1); |
| 504 | clear_buffer_dirty(bh); |
| 505 | csum_tree_block(root, bh, 0); |
| 506 | bh->b_end_io = end_buffer_write_sync; |
| 507 | get_bh(bh); |
| 508 | submit_bh(WRITE, bh); |
| 509 | wait_on_buffer(bh); |
| 510 | if (!buffer_uptodate(bh)) { |
| 511 | WARN_ON(1); |
| 512 | return -EIO; |
| 513 | } |
| 514 | return 0; |
| 515 | } |
| 516 | |
| 517 | static int free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root) |
| 518 | { |
| 519 | radix_tree_delete(&fs_info->fs_roots_radix, |
| 520 | (unsigned long)root->root_key.objectid); |
| 521 | if (root->inode) |
| 522 | iput(root->inode); |
| 523 | if (root->node) |
| 524 | brelse(root->node); |
| 525 | if (root->commit_root) |
| 526 | brelse(root->commit_root); |
| 527 | kfree(root); |
| 528 | return 0; |
| 529 | } |
| 530 | |
| 531 | int del_fs_roots(struct btrfs_fs_info *fs_info) |
| 532 | { |
| 533 | int ret; |
| 534 | struct btrfs_root *gang[8]; |
| 535 | int i; |
| 536 | |
| 537 | while(1) { |
| 538 | ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix, |
| 539 | (void **)gang, 0, |
| 540 | ARRAY_SIZE(gang)); |
| 541 | if (!ret) |
| 542 | break; |
| 543 | for (i = 0; i < ret; i++) |
| 544 | free_fs_root(fs_info, gang[i]); |
| 545 | } |
| 546 | return 0; |
| 547 | } |
| 548 | static int free_dev_radix(struct btrfs_fs_info *fs_info) |
| 549 | { |
| 550 | struct dev_lookup *lookup[8]; |
| 551 | struct block_device *super_bdev = fs_info->sb->s_bdev; |
| 552 | int ret; |
| 553 | int i; |
| 554 | while(1) { |
| 555 | ret = radix_tree_gang_lookup(&fs_info->dev_radix, |
| 556 | (void **)lookup, 0, |
| 557 | ARRAY_SIZE(lookup)); |
| 558 | if (!ret) |
| 559 | break; |
| 560 | for (i = 0; i < ret; i++) { |
| 561 | if (lookup[i]->bdev != super_bdev) |
| 562 | close_bdev_excl(lookup[i]->bdev); |
| 563 | radix_tree_delete(&fs_info->dev_radix, |
| 564 | lookup[i]->block_start + |
| 565 | lookup[i]->num_blocks); |
| 566 | kfree(lookup[i]); |
| 567 | } |
| 568 | } |
| 569 | return 0; |
| 570 | } |
| 571 | |
| 572 | int close_ctree(struct btrfs_root *root) |
| 573 | { |
| 574 | int ret; |
| 575 | struct btrfs_trans_handle *trans; |
| 576 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 577 | |
| 578 | mutex_lock(&fs_info->fs_mutex); |
| 579 | 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); |
| 585 | BUG_ON(ret); |
| 586 | write_ctree_super(NULL, root); |
| 587 | mutex_unlock(&fs_info->fs_mutex); |
| 588 | |
| 589 | if (fs_info->extent_root->node) |
| 590 | btrfs_block_release(fs_info->extent_root, |
| 591 | fs_info->extent_root->node); |
| 592 | if (fs_info->dev_root->node) |
| 593 | btrfs_block_release(fs_info->dev_root, |
| 594 | fs_info->dev_root->node); |
| 595 | if (fs_info->tree_root->node) |
| 596 | btrfs_block_release(fs_info->tree_root, |
| 597 | fs_info->tree_root->node); |
| 598 | btrfs_block_release(root, fs_info->sb_buffer); |
| 599 | crypto_free_hash(fs_info->hash_tfm); |
| 600 | truncate_inode_pages(fs_info->btree_inode->i_mapping, 0); |
| 601 | iput(fs_info->btree_inode); |
| 602 | |
| 603 | free_dev_radix(fs_info); |
| 604 | del_fs_roots(fs_info); |
| 605 | kfree(fs_info->extent_root); |
| 606 | kfree(fs_info->tree_root); |
| 607 | kobject_unregister(&fs_info->kobj); |
| 608 | return 0; |
| 609 | } |
| 610 | |
| 611 | void btrfs_block_release(struct btrfs_root *root, struct buffer_head *buf) |
| 612 | { |
| 613 | brelse(buf); |
| 614 | } |
| 615 | |