| 1 | /* |
| 2 | * Copyright (C) 2007 Oracle. All rights reserved. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public |
| 6 | * License v2 as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 | * General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public |
| 14 | * License along with this program; if not, write to the |
| 15 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 16 | * Boston, MA 021110-1307, USA. |
| 17 | */ |
| 18 | |
| 19 | #include <linux/fs.h> |
| 20 | #include <linux/blkdev.h> |
| 21 | #include <linux/crc32c.h> |
| 22 | #include <linux/scatterlist.h> |
| 23 | #include <linux/swap.h> |
| 24 | #include <linux/radix-tree.h> |
| 25 | #include <linux/writeback.h> |
| 26 | #include <linux/buffer_head.h> // for block_sync_page |
| 27 | #include <linux/workqueue.h> |
| 28 | #include "ctree.h" |
| 29 | #include "disk-io.h" |
| 30 | #include "transaction.h" |
| 31 | #include "btrfs_inode.h" |
| 32 | #include "volumes.h" |
| 33 | #include "print-tree.h" |
| 34 | |
| 35 | #if 0 |
| 36 | static int check_tree_block(struct btrfs_root *root, struct extent_buffer *buf) |
| 37 | { |
| 38 | if (extent_buffer_blocknr(buf) != btrfs_header_blocknr(buf)) { |
| 39 | printk(KERN_CRIT "buf blocknr(buf) is %llu, header is %llu\n", |
| 40 | (unsigned long long)extent_buffer_blocknr(buf), |
| 41 | (unsigned long long)btrfs_header_blocknr(buf)); |
| 42 | return 1; |
| 43 | } |
| 44 | return 0; |
| 45 | } |
| 46 | #endif |
| 47 | |
| 48 | static struct extent_io_ops btree_extent_io_ops; |
| 49 | static struct workqueue_struct *end_io_workqueue; |
| 50 | |
| 51 | struct end_io_wq { |
| 52 | struct bio *bio; |
| 53 | bio_end_io_t *end_io; |
| 54 | void *private; |
| 55 | struct btrfs_fs_info *info; |
| 56 | int error; |
| 57 | struct list_head list; |
| 58 | }; |
| 59 | |
| 60 | struct extent_map *btree_get_extent(struct inode *inode, struct page *page, |
| 61 | size_t page_offset, u64 start, u64 len, |
| 62 | int create) |
| 63 | { |
| 64 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 65 | struct extent_map *em; |
| 66 | int ret; |
| 67 | |
| 68 | again: |
| 69 | spin_lock(&em_tree->lock); |
| 70 | em = lookup_extent_mapping(em_tree, start, len); |
| 71 | spin_unlock(&em_tree->lock); |
| 72 | if (em) { |
| 73 | goto out; |
| 74 | } |
| 75 | em = alloc_extent_map(GFP_NOFS); |
| 76 | if (!em) { |
| 77 | em = ERR_PTR(-ENOMEM); |
| 78 | goto out; |
| 79 | } |
| 80 | em->start = 0; |
| 81 | em->len = i_size_read(inode); |
| 82 | em->block_start = 0; |
| 83 | em->bdev = inode->i_sb->s_bdev; |
| 84 | |
| 85 | spin_lock(&em_tree->lock); |
| 86 | ret = add_extent_mapping(em_tree, em); |
| 87 | spin_unlock(&em_tree->lock); |
| 88 | |
| 89 | if (ret == -EEXIST) { |
| 90 | free_extent_map(em); |
| 91 | em = NULL; |
| 92 | goto again; |
| 93 | } else if (ret) { |
| 94 | em = ERR_PTR(ret); |
| 95 | } |
| 96 | out: |
| 97 | return em; |
| 98 | } |
| 99 | |
| 100 | u32 btrfs_csum_data(struct btrfs_root *root, char *data, u32 seed, size_t len) |
| 101 | { |
| 102 | return crc32c(seed, data, len); |
| 103 | } |
| 104 | |
| 105 | void btrfs_csum_final(u32 crc, char *result) |
| 106 | { |
| 107 | *(__le32 *)result = ~cpu_to_le32(crc); |
| 108 | } |
| 109 | |
| 110 | static int csum_tree_block(struct btrfs_root *root, struct extent_buffer *buf, |
| 111 | int verify) |
| 112 | { |
| 113 | char result[BTRFS_CRC32_SIZE]; |
| 114 | unsigned long len; |
| 115 | unsigned long cur_len; |
| 116 | unsigned long offset = BTRFS_CSUM_SIZE; |
| 117 | char *map_token = NULL; |
| 118 | char *kaddr; |
| 119 | unsigned long map_start; |
| 120 | unsigned long map_len; |
| 121 | int err; |
| 122 | u32 crc = ~(u32)0; |
| 123 | |
| 124 | len = buf->len - offset; |
| 125 | while(len > 0) { |
| 126 | err = map_private_extent_buffer(buf, offset, 32, |
| 127 | &map_token, &kaddr, |
| 128 | &map_start, &map_len, KM_USER0); |
| 129 | if (err) { |
| 130 | printk("failed to map extent buffer! %lu\n", |
| 131 | offset); |
| 132 | return 1; |
| 133 | } |
| 134 | cur_len = min(len, map_len - (offset - map_start)); |
| 135 | crc = btrfs_csum_data(root, kaddr + offset - map_start, |
| 136 | crc, cur_len); |
| 137 | len -= cur_len; |
| 138 | offset += cur_len; |
| 139 | unmap_extent_buffer(buf, map_token, KM_USER0); |
| 140 | } |
| 141 | btrfs_csum_final(crc, result); |
| 142 | |
| 143 | if (verify) { |
| 144 | int from_this_trans = 0; |
| 145 | |
| 146 | if (root->fs_info->running_transaction && |
| 147 | btrfs_header_generation(buf) == |
| 148 | root->fs_info->running_transaction->transid) |
| 149 | from_this_trans = 1; |
| 150 | |
| 151 | /* FIXME, this is not good */ |
| 152 | if (memcmp_extent_buffer(buf, result, 0, BTRFS_CRC32_SIZE)) { |
| 153 | u32 val; |
| 154 | u32 found = 0; |
| 155 | memcpy(&found, result, BTRFS_CRC32_SIZE); |
| 156 | |
| 157 | read_extent_buffer(buf, &val, 0, BTRFS_CRC32_SIZE); |
| 158 | WARN_ON(1); |
| 159 | printk("btrfs: %s checksum verify failed on %llu " |
| 160 | "wanted %X found %X from_this_trans %d " |
| 161 | "level %d\n", |
| 162 | root->fs_info->sb->s_id, |
| 163 | buf->start, val, found, from_this_trans, |
| 164 | btrfs_header_level(buf)); |
| 165 | return 1; |
| 166 | } |
| 167 | } else { |
| 168 | write_extent_buffer(buf, result, 0, BTRFS_CRC32_SIZE); |
| 169 | } |
| 170 | return 0; |
| 171 | } |
| 172 | |
| 173 | |
| 174 | int csum_dirty_buffer(struct btrfs_root *root, struct page *page) |
| 175 | { |
| 176 | struct extent_io_tree *tree; |
| 177 | u64 start = (u64)page->index << PAGE_CACHE_SHIFT; |
| 178 | u64 found_start; |
| 179 | int found_level; |
| 180 | unsigned long len; |
| 181 | struct extent_buffer *eb; |
| 182 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
| 183 | |
| 184 | if (page->private == EXTENT_PAGE_PRIVATE) |
| 185 | goto out; |
| 186 | if (!page->private) |
| 187 | goto out; |
| 188 | len = page->private >> 2; |
| 189 | if (len == 0) { |
| 190 | WARN_ON(1); |
| 191 | } |
| 192 | eb = alloc_extent_buffer(tree, start, len, page, GFP_NOFS); |
| 193 | read_extent_buffer_pages(tree, eb, start + PAGE_CACHE_SIZE, 1, |
| 194 | btree_get_extent); |
| 195 | btrfs_clear_buffer_defrag(eb); |
| 196 | found_start = btrfs_header_bytenr(eb); |
| 197 | if (found_start != start) { |
| 198 | printk("warning: eb start incorrect %Lu buffer %Lu len %lu\n", |
| 199 | start, found_start, len); |
| 200 | WARN_ON(1); |
| 201 | goto err; |
| 202 | } |
| 203 | if (eb->first_page != page) { |
| 204 | printk("bad first page %lu %lu\n", eb->first_page->index, |
| 205 | page->index); |
| 206 | WARN_ON(1); |
| 207 | goto err; |
| 208 | } |
| 209 | if (!PageUptodate(page)) { |
| 210 | printk("csum not up to date page %lu\n", page->index); |
| 211 | WARN_ON(1); |
| 212 | goto err; |
| 213 | } |
| 214 | found_level = btrfs_header_level(eb); |
| 215 | spin_lock(&root->fs_info->hash_lock); |
| 216 | btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN); |
| 217 | spin_unlock(&root->fs_info->hash_lock); |
| 218 | csum_tree_block(root, eb, 0); |
| 219 | err: |
| 220 | free_extent_buffer(eb); |
| 221 | out: |
| 222 | return 0; |
| 223 | } |
| 224 | |
| 225 | static int btree_writepage_io_hook(struct page *page, u64 start, u64 end) |
| 226 | { |
| 227 | struct btrfs_root *root = BTRFS_I(page->mapping->host)->root; |
| 228 | |
| 229 | csum_dirty_buffer(root, page); |
| 230 | return 0; |
| 231 | } |
| 232 | |
| 233 | int btree_readpage_end_io_hook(struct page *page, u64 start, u64 end, |
| 234 | struct extent_state *state) |
| 235 | { |
| 236 | struct extent_io_tree *tree; |
| 237 | u64 found_start; |
| 238 | int found_level; |
| 239 | unsigned long len; |
| 240 | struct extent_buffer *eb; |
| 241 | struct btrfs_root *root = BTRFS_I(page->mapping->host)->root; |
| 242 | int ret; |
| 243 | |
| 244 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
| 245 | if (page->private == EXTENT_PAGE_PRIVATE) |
| 246 | goto out; |
| 247 | if (!page->private) |
| 248 | goto out; |
| 249 | len = page->private >> 2; |
| 250 | if (len == 0) { |
| 251 | WARN_ON(1); |
| 252 | } |
| 253 | eb = alloc_extent_buffer(tree, start, len, page, GFP_NOFS); |
| 254 | read_extent_buffer_pages(tree, eb, start + PAGE_CACHE_SIZE, 1, |
| 255 | btree_get_extent); |
| 256 | btrfs_clear_buffer_defrag(eb); |
| 257 | found_start = btrfs_header_bytenr(eb); |
| 258 | if (found_start != start) { |
| 259 | printk("warning: eb start incorrect %Lu buffer %Lu len %lu\n", |
| 260 | start, found_start, len); |
| 261 | WARN_ON(1); |
| 262 | goto err; |
| 263 | } |
| 264 | if (eb->first_page != page) { |
| 265 | printk("bad first page %lu %lu\n", eb->first_page->index, |
| 266 | page->index); |
| 267 | WARN_ON(1); |
| 268 | goto err; |
| 269 | } |
| 270 | found_level = btrfs_header_level(eb); |
| 271 | |
| 272 | ret = csum_tree_block(root, eb, 1); |
| 273 | |
| 274 | end = min_t(u64, eb->len, PAGE_CACHE_SIZE); |
| 275 | end = eb->start + end - 1; |
| 276 | release_extent_buffer_tail_pages(eb); |
| 277 | err: |
| 278 | free_extent_buffer(eb); |
| 279 | out: |
| 280 | return 0; |
| 281 | } |
| 282 | |
| 283 | #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23) |
| 284 | static void end_workqueue_bio(struct bio *bio, int err) |
| 285 | #else |
| 286 | static int end_workqueue_bio(struct bio *bio, |
| 287 | unsigned int bytes_done, int err) |
| 288 | #endif |
| 289 | { |
| 290 | struct end_io_wq *end_io_wq = bio->bi_private; |
| 291 | struct btrfs_fs_info *fs_info; |
| 292 | unsigned long flags; |
| 293 | |
| 294 | #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23) |
| 295 | if (bio->bi_size) |
| 296 | return 1; |
| 297 | #endif |
| 298 | |
| 299 | fs_info = end_io_wq->info; |
| 300 | spin_lock_irqsave(&fs_info->end_io_work_lock, flags); |
| 301 | end_io_wq->error = err; |
| 302 | list_add_tail(&end_io_wq->list, &fs_info->end_io_work_list); |
| 303 | spin_unlock_irqrestore(&fs_info->end_io_work_lock, flags); |
| 304 | queue_work(end_io_workqueue, &fs_info->end_io_work); |
| 305 | |
| 306 | #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23) |
| 307 | return 0; |
| 308 | #endif |
| 309 | } |
| 310 | |
| 311 | static int btree_submit_bio_hook(struct inode *inode, int rw, struct bio *bio) |
| 312 | { |
| 313 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 314 | struct end_io_wq *end_io_wq; |
| 315 | u64 offset; |
| 316 | offset = bio->bi_sector << 9; |
| 317 | |
| 318 | if (rw & (1 << BIO_RW)) { |
| 319 | return btrfs_map_bio(BTRFS_I(inode)->root, rw, bio); |
| 320 | } |
| 321 | |
| 322 | end_io_wq = kmalloc(sizeof(*end_io_wq), GFP_NOFS); |
| 323 | if (!end_io_wq) |
| 324 | return -ENOMEM; |
| 325 | |
| 326 | end_io_wq->private = bio->bi_private; |
| 327 | end_io_wq->end_io = bio->bi_end_io; |
| 328 | end_io_wq->info = root->fs_info; |
| 329 | end_io_wq->error = 0; |
| 330 | end_io_wq->bio = bio; |
| 331 | |
| 332 | bio->bi_private = end_io_wq; |
| 333 | bio->bi_end_io = end_workqueue_bio; |
| 334 | |
| 335 | if (offset == BTRFS_SUPER_INFO_OFFSET) { |
| 336 | bio->bi_bdev = root->fs_info->sb->s_bdev; |
| 337 | submit_bio(rw, bio); |
| 338 | return 0; |
| 339 | } |
| 340 | return btrfs_map_bio(BTRFS_I(inode)->root, rw, bio); |
| 341 | } |
| 342 | |
| 343 | static int btree_writepage(struct page *page, struct writeback_control *wbc) |
| 344 | { |
| 345 | struct extent_io_tree *tree; |
| 346 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
| 347 | return extent_write_full_page(tree, page, btree_get_extent, wbc); |
| 348 | } |
| 349 | |
| 350 | static int btree_writepages(struct address_space *mapping, |
| 351 | struct writeback_control *wbc) |
| 352 | { |
| 353 | struct extent_io_tree *tree; |
| 354 | tree = &BTRFS_I(mapping->host)->io_tree; |
| 355 | if (wbc->sync_mode == WB_SYNC_NONE) { |
| 356 | u64 num_dirty; |
| 357 | u64 start = 0; |
| 358 | unsigned long thresh = 96 * 1024 * 1024; |
| 359 | |
| 360 | if (wbc->for_kupdate) |
| 361 | return 0; |
| 362 | |
| 363 | if (current_is_pdflush()) { |
| 364 | thresh = 96 * 1024 * 1024; |
| 365 | } else { |
| 366 | thresh = 8 * 1024 * 1024; |
| 367 | } |
| 368 | num_dirty = count_range_bits(tree, &start, (u64)-1, |
| 369 | thresh, EXTENT_DIRTY); |
| 370 | if (num_dirty < thresh) { |
| 371 | return 0; |
| 372 | } |
| 373 | } |
| 374 | return extent_writepages(tree, mapping, btree_get_extent, wbc); |
| 375 | } |
| 376 | |
| 377 | int btree_readpage(struct file *file, struct page *page) |
| 378 | { |
| 379 | struct extent_io_tree *tree; |
| 380 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
| 381 | return extent_read_full_page(tree, page, btree_get_extent); |
| 382 | } |
| 383 | |
| 384 | static int btree_releasepage(struct page *page, gfp_t gfp_flags) |
| 385 | { |
| 386 | struct extent_io_tree *tree; |
| 387 | struct extent_map_tree *map; |
| 388 | int ret; |
| 389 | |
| 390 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
| 391 | map = &BTRFS_I(page->mapping->host)->extent_tree; |
| 392 | ret = try_release_extent_mapping(map, tree, page, gfp_flags); |
| 393 | if (ret == 1) { |
| 394 | invalidate_extent_lru(tree, page_offset(page), PAGE_CACHE_SIZE); |
| 395 | ClearPagePrivate(page); |
| 396 | set_page_private(page, 0); |
| 397 | page_cache_release(page); |
| 398 | } |
| 399 | return ret; |
| 400 | } |
| 401 | |
| 402 | static void btree_invalidatepage(struct page *page, unsigned long offset) |
| 403 | { |
| 404 | struct extent_io_tree *tree; |
| 405 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
| 406 | extent_invalidatepage(tree, page, offset); |
| 407 | btree_releasepage(page, GFP_NOFS); |
| 408 | } |
| 409 | |
| 410 | #if 0 |
| 411 | static int btree_writepage(struct page *page, struct writeback_control *wbc) |
| 412 | { |
| 413 | struct buffer_head *bh; |
| 414 | struct btrfs_root *root = BTRFS_I(page->mapping->host)->root; |
| 415 | struct buffer_head *head; |
| 416 | if (!page_has_buffers(page)) { |
| 417 | create_empty_buffers(page, root->fs_info->sb->s_blocksize, |
| 418 | (1 << BH_Dirty)|(1 << BH_Uptodate)); |
| 419 | } |
| 420 | head = page_buffers(page); |
| 421 | bh = head; |
| 422 | do { |
| 423 | if (buffer_dirty(bh)) |
| 424 | csum_tree_block(root, bh, 0); |
| 425 | bh = bh->b_this_page; |
| 426 | } while (bh != head); |
| 427 | return block_write_full_page(page, btree_get_block, wbc); |
| 428 | } |
| 429 | #endif |
| 430 | |
| 431 | static struct address_space_operations btree_aops = { |
| 432 | .readpage = btree_readpage, |
| 433 | .writepage = btree_writepage, |
| 434 | .writepages = btree_writepages, |
| 435 | .releasepage = btree_releasepage, |
| 436 | .invalidatepage = btree_invalidatepage, |
| 437 | .sync_page = block_sync_page, |
| 438 | }; |
| 439 | |
| 440 | int readahead_tree_block(struct btrfs_root *root, u64 bytenr, u32 blocksize) |
| 441 | { |
| 442 | struct extent_buffer *buf = NULL; |
| 443 | struct inode *btree_inode = root->fs_info->btree_inode; |
| 444 | int ret = 0; |
| 445 | |
| 446 | buf = btrfs_find_create_tree_block(root, bytenr, blocksize); |
| 447 | if (!buf) |
| 448 | return 0; |
| 449 | read_extent_buffer_pages(&BTRFS_I(btree_inode)->io_tree, |
| 450 | buf, 0, 0, btree_get_extent); |
| 451 | free_extent_buffer(buf); |
| 452 | return ret; |
| 453 | } |
| 454 | |
| 455 | static int close_all_devices(struct btrfs_fs_info *fs_info) |
| 456 | { |
| 457 | struct list_head *list; |
| 458 | struct list_head *next; |
| 459 | struct btrfs_device *device; |
| 460 | |
| 461 | list = &fs_info->fs_devices->devices; |
| 462 | list_for_each(next, list) { |
| 463 | device = list_entry(next, struct btrfs_device, dev_list); |
| 464 | if (device->bdev && device->bdev != fs_info->sb->s_bdev) |
| 465 | close_bdev_excl(device->bdev); |
| 466 | device->bdev = NULL; |
| 467 | } |
| 468 | return 0; |
| 469 | } |
| 470 | |
| 471 | int btrfs_verify_block_csum(struct btrfs_root *root, |
| 472 | struct extent_buffer *buf) |
| 473 | { |
| 474 | return btrfs_buffer_uptodate(buf); |
| 475 | } |
| 476 | |
| 477 | struct extent_buffer *btrfs_find_tree_block(struct btrfs_root *root, |
| 478 | u64 bytenr, u32 blocksize) |
| 479 | { |
| 480 | struct inode *btree_inode = root->fs_info->btree_inode; |
| 481 | struct extent_buffer *eb; |
| 482 | eb = find_extent_buffer(&BTRFS_I(btree_inode)->io_tree, |
| 483 | bytenr, blocksize, GFP_NOFS); |
| 484 | return eb; |
| 485 | } |
| 486 | |
| 487 | struct extent_buffer *btrfs_find_create_tree_block(struct btrfs_root *root, |
| 488 | u64 bytenr, u32 blocksize) |
| 489 | { |
| 490 | struct inode *btree_inode = root->fs_info->btree_inode; |
| 491 | struct extent_buffer *eb; |
| 492 | |
| 493 | eb = alloc_extent_buffer(&BTRFS_I(btree_inode)->io_tree, |
| 494 | bytenr, blocksize, NULL, GFP_NOFS); |
| 495 | return eb; |
| 496 | } |
| 497 | |
| 498 | |
| 499 | struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr, |
| 500 | u32 blocksize) |
| 501 | { |
| 502 | struct extent_buffer *buf = NULL; |
| 503 | struct inode *btree_inode = root->fs_info->btree_inode; |
| 504 | struct extent_io_tree *io_tree; |
| 505 | int ret; |
| 506 | |
| 507 | io_tree = &BTRFS_I(btree_inode)->io_tree; |
| 508 | |
| 509 | buf = btrfs_find_create_tree_block(root, bytenr, blocksize); |
| 510 | if (!buf) |
| 511 | return NULL; |
| 512 | |
| 513 | ret = read_extent_buffer_pages(&BTRFS_I(btree_inode)->io_tree, buf, 0, |
| 514 | 1, btree_get_extent); |
| 515 | |
| 516 | if (ret == 0) { |
| 517 | buf->flags |= EXTENT_UPTODATE; |
| 518 | } |
| 519 | return buf; |
| 520 | |
| 521 | } |
| 522 | |
| 523 | int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
| 524 | struct extent_buffer *buf) |
| 525 | { |
| 526 | struct inode *btree_inode = root->fs_info->btree_inode; |
| 527 | if (btrfs_header_generation(buf) == |
| 528 | root->fs_info->running_transaction->transid) |
| 529 | clear_extent_buffer_dirty(&BTRFS_I(btree_inode)->io_tree, |
| 530 | buf); |
| 531 | return 0; |
| 532 | } |
| 533 | |
| 534 | int wait_on_tree_block_writeback(struct btrfs_root *root, |
| 535 | struct extent_buffer *buf) |
| 536 | { |
| 537 | struct inode *btree_inode = root->fs_info->btree_inode; |
| 538 | wait_on_extent_buffer_writeback(&BTRFS_I(btree_inode)->io_tree, |
| 539 | buf); |
| 540 | return 0; |
| 541 | } |
| 542 | |
| 543 | static int __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize, |
| 544 | u32 stripesize, struct btrfs_root *root, |
| 545 | struct btrfs_fs_info *fs_info, |
| 546 | u64 objectid) |
| 547 | { |
| 548 | root->node = NULL; |
| 549 | root->inode = NULL; |
| 550 | root->commit_root = NULL; |
| 551 | root->sectorsize = sectorsize; |
| 552 | root->nodesize = nodesize; |
| 553 | root->leafsize = leafsize; |
| 554 | root->stripesize = stripesize; |
| 555 | root->ref_cows = 0; |
| 556 | root->track_dirty = 0; |
| 557 | |
| 558 | root->fs_info = fs_info; |
| 559 | root->objectid = objectid; |
| 560 | root->last_trans = 0; |
| 561 | root->highest_inode = 0; |
| 562 | root->last_inode_alloc = 0; |
| 563 | root->name = NULL; |
| 564 | root->in_sysfs = 0; |
| 565 | |
| 566 | INIT_LIST_HEAD(&root->dirty_list); |
| 567 | memset(&root->root_key, 0, sizeof(root->root_key)); |
| 568 | memset(&root->root_item, 0, sizeof(root->root_item)); |
| 569 | memset(&root->defrag_progress, 0, sizeof(root->defrag_progress)); |
| 570 | memset(&root->root_kobj, 0, sizeof(root->root_kobj)); |
| 571 | init_completion(&root->kobj_unregister); |
| 572 | root->defrag_running = 0; |
| 573 | root->defrag_level = 0; |
| 574 | root->root_key.objectid = objectid; |
| 575 | return 0; |
| 576 | } |
| 577 | |
| 578 | static int find_and_setup_root(struct btrfs_root *tree_root, |
| 579 | struct btrfs_fs_info *fs_info, |
| 580 | u64 objectid, |
| 581 | struct btrfs_root *root) |
| 582 | { |
| 583 | int ret; |
| 584 | u32 blocksize; |
| 585 | |
| 586 | __setup_root(tree_root->nodesize, tree_root->leafsize, |
| 587 | tree_root->sectorsize, tree_root->stripesize, |
| 588 | root, fs_info, objectid); |
| 589 | ret = btrfs_find_last_root(tree_root, objectid, |
| 590 | &root->root_item, &root->root_key); |
| 591 | BUG_ON(ret); |
| 592 | |
| 593 | blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item)); |
| 594 | root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item), |
| 595 | blocksize); |
| 596 | BUG_ON(!root->node); |
| 597 | return 0; |
| 598 | } |
| 599 | |
| 600 | struct btrfs_root *btrfs_read_fs_root_no_radix(struct btrfs_fs_info *fs_info, |
| 601 | struct btrfs_key *location) |
| 602 | { |
| 603 | struct btrfs_root *root; |
| 604 | struct btrfs_root *tree_root = fs_info->tree_root; |
| 605 | struct btrfs_path *path; |
| 606 | struct extent_buffer *l; |
| 607 | u64 highest_inode; |
| 608 | u32 blocksize; |
| 609 | int ret = 0; |
| 610 | |
| 611 | root = kzalloc(sizeof(*root), GFP_NOFS); |
| 612 | if (!root) |
| 613 | return ERR_PTR(-ENOMEM); |
| 614 | if (location->offset == (u64)-1) { |
| 615 | ret = find_and_setup_root(tree_root, fs_info, |
| 616 | location->objectid, root); |
| 617 | if (ret) { |
| 618 | kfree(root); |
| 619 | return ERR_PTR(ret); |
| 620 | } |
| 621 | goto insert; |
| 622 | } |
| 623 | |
| 624 | __setup_root(tree_root->nodesize, tree_root->leafsize, |
| 625 | tree_root->sectorsize, tree_root->stripesize, |
| 626 | root, fs_info, location->objectid); |
| 627 | |
| 628 | path = btrfs_alloc_path(); |
| 629 | BUG_ON(!path); |
| 630 | ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0); |
| 631 | if (ret != 0) { |
| 632 | if (ret > 0) |
| 633 | ret = -ENOENT; |
| 634 | goto out; |
| 635 | } |
| 636 | l = path->nodes[0]; |
| 637 | read_extent_buffer(l, &root->root_item, |
| 638 | btrfs_item_ptr_offset(l, path->slots[0]), |
| 639 | sizeof(root->root_item)); |
| 640 | memcpy(&root->root_key, location, sizeof(*location)); |
| 641 | ret = 0; |
| 642 | out: |
| 643 | btrfs_release_path(root, path); |
| 644 | btrfs_free_path(path); |
| 645 | if (ret) { |
| 646 | kfree(root); |
| 647 | return ERR_PTR(ret); |
| 648 | } |
| 649 | blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item)); |
| 650 | root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item), |
| 651 | blocksize); |
| 652 | BUG_ON(!root->node); |
| 653 | insert: |
| 654 | root->ref_cows = 1; |
| 655 | ret = btrfs_find_highest_inode(root, &highest_inode); |
| 656 | if (ret == 0) { |
| 657 | root->highest_inode = highest_inode; |
| 658 | root->last_inode_alloc = highest_inode; |
| 659 | } |
| 660 | return root; |
| 661 | } |
| 662 | |
| 663 | struct btrfs_root *btrfs_lookup_fs_root(struct btrfs_fs_info *fs_info, |
| 664 | u64 root_objectid) |
| 665 | { |
| 666 | struct btrfs_root *root; |
| 667 | |
| 668 | if (root_objectid == BTRFS_ROOT_TREE_OBJECTID) |
| 669 | return fs_info->tree_root; |
| 670 | if (root_objectid == BTRFS_EXTENT_TREE_OBJECTID) |
| 671 | return fs_info->extent_root; |
| 672 | |
| 673 | root = radix_tree_lookup(&fs_info->fs_roots_radix, |
| 674 | (unsigned long)root_objectid); |
| 675 | return root; |
| 676 | } |
| 677 | |
| 678 | struct btrfs_root *btrfs_read_fs_root_no_name(struct btrfs_fs_info *fs_info, |
| 679 | struct btrfs_key *location) |
| 680 | { |
| 681 | struct btrfs_root *root; |
| 682 | int ret; |
| 683 | |
| 684 | if (location->objectid == BTRFS_ROOT_TREE_OBJECTID) |
| 685 | return fs_info->tree_root; |
| 686 | if (location->objectid == BTRFS_EXTENT_TREE_OBJECTID) |
| 687 | return fs_info->extent_root; |
| 688 | |
| 689 | root = radix_tree_lookup(&fs_info->fs_roots_radix, |
| 690 | (unsigned long)location->objectid); |
| 691 | if (root) |
| 692 | return root; |
| 693 | |
| 694 | root = btrfs_read_fs_root_no_radix(fs_info, location); |
| 695 | if (IS_ERR(root)) |
| 696 | return root; |
| 697 | ret = radix_tree_insert(&fs_info->fs_roots_radix, |
| 698 | (unsigned long)root->root_key.objectid, |
| 699 | root); |
| 700 | if (ret) { |
| 701 | free_extent_buffer(root->node); |
| 702 | kfree(root); |
| 703 | return ERR_PTR(ret); |
| 704 | } |
| 705 | ret = btrfs_find_dead_roots(fs_info->tree_root, |
| 706 | root->root_key.objectid, root); |
| 707 | BUG_ON(ret); |
| 708 | |
| 709 | return root; |
| 710 | } |
| 711 | |
| 712 | struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info, |
| 713 | struct btrfs_key *location, |
| 714 | const char *name, int namelen) |
| 715 | { |
| 716 | struct btrfs_root *root; |
| 717 | int ret; |
| 718 | |
| 719 | root = btrfs_read_fs_root_no_name(fs_info, location); |
| 720 | if (!root) |
| 721 | return NULL; |
| 722 | |
| 723 | if (root->in_sysfs) |
| 724 | return root; |
| 725 | |
| 726 | ret = btrfs_set_root_name(root, name, namelen); |
| 727 | if (ret) { |
| 728 | free_extent_buffer(root->node); |
| 729 | kfree(root); |
| 730 | return ERR_PTR(ret); |
| 731 | } |
| 732 | |
| 733 | ret = btrfs_sysfs_add_root(root); |
| 734 | if (ret) { |
| 735 | free_extent_buffer(root->node); |
| 736 | kfree(root->name); |
| 737 | kfree(root); |
| 738 | return ERR_PTR(ret); |
| 739 | } |
| 740 | root->in_sysfs = 1; |
| 741 | return root; |
| 742 | } |
| 743 | #if 0 |
| 744 | static int add_hasher(struct btrfs_fs_info *info, char *type) { |
| 745 | struct btrfs_hasher *hasher; |
| 746 | |
| 747 | hasher = kmalloc(sizeof(*hasher), GFP_NOFS); |
| 748 | if (!hasher) |
| 749 | return -ENOMEM; |
| 750 | hasher->hash_tfm = crypto_alloc_hash(type, 0, CRYPTO_ALG_ASYNC); |
| 751 | if (!hasher->hash_tfm) { |
| 752 | kfree(hasher); |
| 753 | return -EINVAL; |
| 754 | } |
| 755 | spin_lock(&info->hash_lock); |
| 756 | list_add(&hasher->list, &info->hashers); |
| 757 | spin_unlock(&info->hash_lock); |
| 758 | return 0; |
| 759 | } |
| 760 | #endif |
| 761 | |
| 762 | static int btrfs_congested_fn(void *congested_data, int bdi_bits) |
| 763 | { |
| 764 | struct btrfs_fs_info *info = (struct btrfs_fs_info *)congested_data; |
| 765 | int ret = 0; |
| 766 | struct list_head *cur; |
| 767 | struct btrfs_device *device; |
| 768 | struct backing_dev_info *bdi; |
| 769 | |
| 770 | list_for_each(cur, &info->fs_devices->devices) { |
| 771 | device = list_entry(cur, struct btrfs_device, dev_list); |
| 772 | bdi = blk_get_backing_dev_info(device->bdev); |
| 773 | if (bdi && bdi_congested(bdi, bdi_bits)) { |
| 774 | ret = 1; |
| 775 | break; |
| 776 | } |
| 777 | } |
| 778 | return ret; |
| 779 | } |
| 780 | |
| 781 | void btrfs_unplug_io_fn(struct backing_dev_info *bdi, struct page *page) |
| 782 | { |
| 783 | struct list_head *cur; |
| 784 | struct btrfs_device *device; |
| 785 | struct btrfs_fs_info *info; |
| 786 | |
| 787 | info = (struct btrfs_fs_info *)bdi->unplug_io_data; |
| 788 | list_for_each(cur, &info->fs_devices->devices) { |
| 789 | device = list_entry(cur, struct btrfs_device, dev_list); |
| 790 | bdi = blk_get_backing_dev_info(device->bdev); |
| 791 | if (bdi->unplug_io_fn) { |
| 792 | bdi->unplug_io_fn(bdi, page); |
| 793 | } |
| 794 | } |
| 795 | } |
| 796 | |
| 797 | static int setup_bdi(struct btrfs_fs_info *info, struct backing_dev_info *bdi) |
| 798 | { |
| 799 | bdi_init(bdi); |
| 800 | bdi->ra_pages = default_backing_dev_info.ra_pages * 4; |
| 801 | bdi->state = 0; |
| 802 | bdi->capabilities = default_backing_dev_info.capabilities; |
| 803 | bdi->unplug_io_fn = btrfs_unplug_io_fn; |
| 804 | bdi->unplug_io_data = info; |
| 805 | bdi->congested_fn = btrfs_congested_fn; |
| 806 | bdi->congested_data = info; |
| 807 | return 0; |
| 808 | } |
| 809 | |
| 810 | static int bio_ready_for_csum(struct bio *bio) |
| 811 | { |
| 812 | u64 length = 0; |
| 813 | u64 buf_len = 0; |
| 814 | u64 start = 0; |
| 815 | struct page *page; |
| 816 | struct extent_io_tree *io_tree = NULL; |
| 817 | struct btrfs_fs_info *info = NULL; |
| 818 | struct bio_vec *bvec; |
| 819 | int i; |
| 820 | int ret; |
| 821 | |
| 822 | bio_for_each_segment(bvec, bio, i) { |
| 823 | page = bvec->bv_page; |
| 824 | if (page->private == EXTENT_PAGE_PRIVATE) { |
| 825 | length += bvec->bv_len; |
| 826 | continue; |
| 827 | } |
| 828 | if (!page->private) { |
| 829 | length += bvec->bv_len; |
| 830 | continue; |
| 831 | } |
| 832 | length = bvec->bv_len; |
| 833 | buf_len = page->private >> 2; |
| 834 | start = page_offset(page) + bvec->bv_offset; |
| 835 | io_tree = &BTRFS_I(page->mapping->host)->io_tree; |
| 836 | info = BTRFS_I(page->mapping->host)->root->fs_info; |
| 837 | } |
| 838 | /* are we fully contained in this bio? */ |
| 839 | if (buf_len <= length) |
| 840 | return 1; |
| 841 | |
| 842 | ret = extent_range_uptodate(io_tree, start + length, |
| 843 | start + buf_len - 1); |
| 844 | if (ret == 1) |
| 845 | return ret; |
| 846 | return ret; |
| 847 | } |
| 848 | |
| 849 | #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18) |
| 850 | void btrfs_end_io_csum(void *p) |
| 851 | #else |
| 852 | void btrfs_end_io_csum(struct work_struct *work) |
| 853 | #endif |
| 854 | { |
| 855 | #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18) |
| 856 | struct btrfs_fs_info *fs_info = p; |
| 857 | #else |
| 858 | struct btrfs_fs_info *fs_info = container_of(work, |
| 859 | struct btrfs_fs_info, |
| 860 | end_io_work); |
| 861 | #endif |
| 862 | unsigned long flags; |
| 863 | struct end_io_wq *end_io_wq; |
| 864 | struct bio *bio; |
| 865 | struct list_head *next; |
| 866 | int error; |
| 867 | int was_empty; |
| 868 | |
| 869 | while(1) { |
| 870 | spin_lock_irqsave(&fs_info->end_io_work_lock, flags); |
| 871 | if (list_empty(&fs_info->end_io_work_list)) { |
| 872 | spin_unlock_irqrestore(&fs_info->end_io_work_lock, |
| 873 | flags); |
| 874 | return; |
| 875 | } |
| 876 | next = fs_info->end_io_work_list.next; |
| 877 | list_del(next); |
| 878 | spin_unlock_irqrestore(&fs_info->end_io_work_lock, flags); |
| 879 | |
| 880 | end_io_wq = list_entry(next, struct end_io_wq, list); |
| 881 | |
| 882 | bio = end_io_wq->bio; |
| 883 | if (!bio_ready_for_csum(bio)) { |
| 884 | spin_lock_irqsave(&fs_info->end_io_work_lock, flags); |
| 885 | was_empty = list_empty(&fs_info->end_io_work_list); |
| 886 | list_add_tail(&end_io_wq->list, |
| 887 | &fs_info->end_io_work_list); |
| 888 | spin_unlock_irqrestore(&fs_info->end_io_work_lock, |
| 889 | flags); |
| 890 | if (was_empty) |
| 891 | return; |
| 892 | continue; |
| 893 | } |
| 894 | error = end_io_wq->error; |
| 895 | bio->bi_private = end_io_wq->private; |
| 896 | bio->bi_end_io = end_io_wq->end_io; |
| 897 | kfree(end_io_wq); |
| 898 | bio_endio(bio, error); |
| 899 | } |
| 900 | } |
| 901 | |
| 902 | |
| 903 | struct btrfs_root *open_ctree(struct super_block *sb, |
| 904 | struct btrfs_fs_devices *fs_devices) |
| 905 | { |
| 906 | u32 sectorsize; |
| 907 | u32 nodesize; |
| 908 | u32 leafsize; |
| 909 | u32 blocksize; |
| 910 | u32 stripesize; |
| 911 | struct btrfs_root *extent_root = kmalloc(sizeof(struct btrfs_root), |
| 912 | GFP_NOFS); |
| 913 | struct btrfs_root *tree_root = kmalloc(sizeof(struct btrfs_root), |
| 914 | GFP_NOFS); |
| 915 | struct btrfs_fs_info *fs_info = kzalloc(sizeof(*fs_info), |
| 916 | GFP_NOFS); |
| 917 | struct btrfs_root *chunk_root = kmalloc(sizeof(struct btrfs_root), |
| 918 | GFP_NOFS); |
| 919 | struct btrfs_root *dev_root = kmalloc(sizeof(struct btrfs_root), |
| 920 | GFP_NOFS); |
| 921 | int ret; |
| 922 | int err = -EINVAL; |
| 923 | struct btrfs_super_block *disk_super; |
| 924 | |
| 925 | if (!extent_root || !tree_root || !fs_info) { |
| 926 | err = -ENOMEM; |
| 927 | goto fail; |
| 928 | } |
| 929 | end_io_workqueue = create_workqueue("btrfs-end-io"); |
| 930 | BUG_ON(!end_io_workqueue); |
| 931 | |
| 932 | INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_NOFS); |
| 933 | INIT_LIST_HEAD(&fs_info->trans_list); |
| 934 | INIT_LIST_HEAD(&fs_info->dead_roots); |
| 935 | INIT_LIST_HEAD(&fs_info->hashers); |
| 936 | INIT_LIST_HEAD(&fs_info->end_io_work_list); |
| 937 | spin_lock_init(&fs_info->hash_lock); |
| 938 | spin_lock_init(&fs_info->end_io_work_lock); |
| 939 | spin_lock_init(&fs_info->delalloc_lock); |
| 940 | spin_lock_init(&fs_info->new_trans_lock); |
| 941 | |
| 942 | init_completion(&fs_info->kobj_unregister); |
| 943 | sb_set_blocksize(sb, 4096); |
| 944 | fs_info->tree_root = tree_root; |
| 945 | fs_info->extent_root = extent_root; |
| 946 | fs_info->chunk_root = chunk_root; |
| 947 | fs_info->dev_root = dev_root; |
| 948 | fs_info->fs_devices = fs_devices; |
| 949 | INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots); |
| 950 | INIT_LIST_HEAD(&fs_info->space_info); |
| 951 | btrfs_mapping_init(&fs_info->mapping_tree); |
| 952 | fs_info->sb = sb; |
| 953 | fs_info->max_extent = (u64)-1; |
| 954 | fs_info->max_inline = 8192 * 1024; |
| 955 | setup_bdi(fs_info, &fs_info->bdi); |
| 956 | fs_info->btree_inode = new_inode(sb); |
| 957 | fs_info->btree_inode->i_ino = 1; |
| 958 | fs_info->btree_inode->i_nlink = 1; |
| 959 | fs_info->btree_inode->i_size = sb->s_bdev->bd_inode->i_size; |
| 960 | fs_info->btree_inode->i_mapping->a_ops = &btree_aops; |
| 961 | fs_info->btree_inode->i_mapping->backing_dev_info = &fs_info->bdi; |
| 962 | |
| 963 | extent_io_tree_init(&BTRFS_I(fs_info->btree_inode)->io_tree, |
| 964 | fs_info->btree_inode->i_mapping, |
| 965 | GFP_NOFS); |
| 966 | extent_map_tree_init(&BTRFS_I(fs_info->btree_inode)->extent_tree, |
| 967 | GFP_NOFS); |
| 968 | |
| 969 | BTRFS_I(fs_info->btree_inode)->io_tree.ops = &btree_extent_io_ops; |
| 970 | |
| 971 | extent_io_tree_init(&fs_info->free_space_cache, |
| 972 | fs_info->btree_inode->i_mapping, GFP_NOFS); |
| 973 | extent_io_tree_init(&fs_info->block_group_cache, |
| 974 | fs_info->btree_inode->i_mapping, GFP_NOFS); |
| 975 | extent_io_tree_init(&fs_info->pinned_extents, |
| 976 | fs_info->btree_inode->i_mapping, GFP_NOFS); |
| 977 | extent_io_tree_init(&fs_info->pending_del, |
| 978 | fs_info->btree_inode->i_mapping, GFP_NOFS); |
| 979 | extent_io_tree_init(&fs_info->extent_ins, |
| 980 | fs_info->btree_inode->i_mapping, GFP_NOFS); |
| 981 | fs_info->do_barriers = 1; |
| 982 | |
| 983 | INIT_WORK(&fs_info->end_io_work, btrfs_end_io_csum); |
| 984 | #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18) |
| 985 | INIT_WORK(&fs_info->trans_work, btrfs_transaction_cleaner, fs_info); |
| 986 | #else |
| 987 | INIT_DELAYED_WORK(&fs_info->trans_work, btrfs_transaction_cleaner); |
| 988 | #endif |
| 989 | BTRFS_I(fs_info->btree_inode)->root = tree_root; |
| 990 | memset(&BTRFS_I(fs_info->btree_inode)->location, 0, |
| 991 | sizeof(struct btrfs_key)); |
| 992 | insert_inode_hash(fs_info->btree_inode); |
| 993 | mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS); |
| 994 | |
| 995 | mutex_init(&fs_info->trans_mutex); |
| 996 | mutex_init(&fs_info->fs_mutex); |
| 997 | |
| 998 | #if 0 |
| 999 | ret = add_hasher(fs_info, "crc32c"); |
| 1000 | if (ret) { |
| 1001 | printk("btrfs: failed hash setup, modprobe cryptomgr?\n"); |
| 1002 | err = -ENOMEM; |
| 1003 | goto fail_iput; |
| 1004 | } |
| 1005 | #endif |
| 1006 | __setup_root(4096, 4096, 4096, 4096, tree_root, |
| 1007 | fs_info, BTRFS_ROOT_TREE_OBJECTID); |
| 1008 | |
| 1009 | fs_info->sb_buffer = read_tree_block(tree_root, |
| 1010 | BTRFS_SUPER_INFO_OFFSET, |
| 1011 | 4096); |
| 1012 | |
| 1013 | if (!fs_info->sb_buffer) |
| 1014 | goto fail_iput; |
| 1015 | |
| 1016 | read_extent_buffer(fs_info->sb_buffer, &fs_info->super_copy, 0, |
| 1017 | sizeof(fs_info->super_copy)); |
| 1018 | |
| 1019 | read_extent_buffer(fs_info->sb_buffer, fs_info->fsid, |
| 1020 | (unsigned long)btrfs_super_fsid(fs_info->sb_buffer), |
| 1021 | BTRFS_FSID_SIZE); |
| 1022 | |
| 1023 | disk_super = &fs_info->super_copy; |
| 1024 | if (!btrfs_super_root(disk_super)) |
| 1025 | goto fail_sb_buffer; |
| 1026 | |
| 1027 | if (btrfs_super_num_devices(disk_super) != fs_devices->num_devices) { |
| 1028 | printk("Btrfs: wanted %llu devices, but found %llu\n", |
| 1029 | (unsigned long long)btrfs_super_num_devices(disk_super), |
| 1030 | (unsigned long long)fs_devices->num_devices); |
| 1031 | goto fail_sb_buffer; |
| 1032 | } |
| 1033 | nodesize = btrfs_super_nodesize(disk_super); |
| 1034 | leafsize = btrfs_super_leafsize(disk_super); |
| 1035 | sectorsize = btrfs_super_sectorsize(disk_super); |
| 1036 | stripesize = btrfs_super_stripesize(disk_super); |
| 1037 | tree_root->nodesize = nodesize; |
| 1038 | tree_root->leafsize = leafsize; |
| 1039 | tree_root->sectorsize = sectorsize; |
| 1040 | tree_root->stripesize = stripesize; |
| 1041 | sb_set_blocksize(sb, sectorsize); |
| 1042 | |
| 1043 | i_size_write(fs_info->btree_inode, |
| 1044 | btrfs_super_total_bytes(disk_super)); |
| 1045 | |
| 1046 | if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC, |
| 1047 | sizeof(disk_super->magic))) { |
| 1048 | printk("btrfs: valid FS not found on %s\n", sb->s_id); |
| 1049 | goto fail_sb_buffer; |
| 1050 | } |
| 1051 | |
| 1052 | mutex_lock(&fs_info->fs_mutex); |
| 1053 | |
| 1054 | ret = btrfs_read_sys_array(tree_root); |
| 1055 | BUG_ON(ret); |
| 1056 | |
| 1057 | blocksize = btrfs_level_size(tree_root, |
| 1058 | btrfs_super_chunk_root_level(disk_super)); |
| 1059 | |
| 1060 | __setup_root(nodesize, leafsize, sectorsize, stripesize, |
| 1061 | chunk_root, fs_info, BTRFS_CHUNK_TREE_OBJECTID); |
| 1062 | |
| 1063 | chunk_root->node = read_tree_block(chunk_root, |
| 1064 | btrfs_super_chunk_root(disk_super), |
| 1065 | blocksize); |
| 1066 | BUG_ON(!chunk_root->node); |
| 1067 | |
| 1068 | ret = btrfs_read_chunk_tree(chunk_root); |
| 1069 | BUG_ON(ret); |
| 1070 | |
| 1071 | blocksize = btrfs_level_size(tree_root, |
| 1072 | btrfs_super_root_level(disk_super)); |
| 1073 | |
| 1074 | |
| 1075 | tree_root->node = read_tree_block(tree_root, |
| 1076 | btrfs_super_root(disk_super), |
| 1077 | blocksize); |
| 1078 | if (!tree_root->node) |
| 1079 | goto fail_sb_buffer; |
| 1080 | |
| 1081 | |
| 1082 | ret = find_and_setup_root(tree_root, fs_info, |
| 1083 | BTRFS_EXTENT_TREE_OBJECTID, extent_root); |
| 1084 | if (ret) |
| 1085 | goto fail_tree_root; |
| 1086 | extent_root->track_dirty = 1; |
| 1087 | |
| 1088 | ret = find_and_setup_root(tree_root, fs_info, |
| 1089 | BTRFS_DEV_TREE_OBJECTID, dev_root); |
| 1090 | dev_root->track_dirty = 1; |
| 1091 | |
| 1092 | if (ret) |
| 1093 | goto fail_extent_root; |
| 1094 | |
| 1095 | btrfs_read_block_groups(extent_root); |
| 1096 | |
| 1097 | fs_info->generation = btrfs_super_generation(disk_super) + 1; |
| 1098 | fs_info->data_alloc_profile = (u64)-1; |
| 1099 | fs_info->metadata_alloc_profile = (u64)-1; |
| 1100 | fs_info->system_alloc_profile = fs_info->metadata_alloc_profile; |
| 1101 | |
| 1102 | mutex_unlock(&fs_info->fs_mutex); |
| 1103 | return tree_root; |
| 1104 | |
| 1105 | fail_extent_root: |
| 1106 | free_extent_buffer(extent_root->node); |
| 1107 | fail_tree_root: |
| 1108 | mutex_unlock(&fs_info->fs_mutex); |
| 1109 | free_extent_buffer(tree_root->node); |
| 1110 | fail_sb_buffer: |
| 1111 | free_extent_buffer(fs_info->sb_buffer); |
| 1112 | extent_io_tree_empty_lru(&BTRFS_I(fs_info->btree_inode)->io_tree); |
| 1113 | fail_iput: |
| 1114 | iput(fs_info->btree_inode); |
| 1115 | fail: |
| 1116 | close_all_devices(fs_info); |
| 1117 | kfree(extent_root); |
| 1118 | kfree(tree_root); |
| 1119 | bdi_destroy(&fs_info->bdi); |
| 1120 | kfree(fs_info); |
| 1121 | return ERR_PTR(err); |
| 1122 | } |
| 1123 | |
| 1124 | int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root |
| 1125 | *root) |
| 1126 | { |
| 1127 | int ret; |
| 1128 | struct extent_buffer *super = root->fs_info->sb_buffer; |
| 1129 | struct inode *btree_inode = root->fs_info->btree_inode; |
| 1130 | struct super_block *sb = root->fs_info->sb; |
| 1131 | |
| 1132 | if (!btrfs_test_opt(root, NOBARRIER)) |
| 1133 | blkdev_issue_flush(sb->s_bdev, NULL); |
| 1134 | set_extent_buffer_dirty(&BTRFS_I(btree_inode)->io_tree, super); |
| 1135 | ret = sync_page_range_nolock(btree_inode, btree_inode->i_mapping, |
| 1136 | super->start, super->len); |
| 1137 | if (!btrfs_test_opt(root, NOBARRIER)) |
| 1138 | blkdev_issue_flush(sb->s_bdev, NULL); |
| 1139 | return ret; |
| 1140 | } |
| 1141 | |
| 1142 | int btrfs_free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root) |
| 1143 | { |
| 1144 | radix_tree_delete(&fs_info->fs_roots_radix, |
| 1145 | (unsigned long)root->root_key.objectid); |
| 1146 | if (root->in_sysfs) |
| 1147 | btrfs_sysfs_del_root(root); |
| 1148 | if (root->inode) |
| 1149 | iput(root->inode); |
| 1150 | if (root->node) |
| 1151 | free_extent_buffer(root->node); |
| 1152 | if (root->commit_root) |
| 1153 | free_extent_buffer(root->commit_root); |
| 1154 | if (root->name) |
| 1155 | kfree(root->name); |
| 1156 | kfree(root); |
| 1157 | return 0; |
| 1158 | } |
| 1159 | |
| 1160 | static int del_fs_roots(struct btrfs_fs_info *fs_info) |
| 1161 | { |
| 1162 | int ret; |
| 1163 | struct btrfs_root *gang[8]; |
| 1164 | int i; |
| 1165 | |
| 1166 | while(1) { |
| 1167 | ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix, |
| 1168 | (void **)gang, 0, |
| 1169 | ARRAY_SIZE(gang)); |
| 1170 | if (!ret) |
| 1171 | break; |
| 1172 | for (i = 0; i < ret; i++) |
| 1173 | btrfs_free_fs_root(fs_info, gang[i]); |
| 1174 | } |
| 1175 | return 0; |
| 1176 | } |
| 1177 | |
| 1178 | int close_ctree(struct btrfs_root *root) |
| 1179 | { |
| 1180 | int ret; |
| 1181 | struct btrfs_trans_handle *trans; |
| 1182 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 1183 | |
| 1184 | fs_info->closing = 1; |
| 1185 | btrfs_transaction_flush_work(root); |
| 1186 | mutex_lock(&fs_info->fs_mutex); |
| 1187 | btrfs_defrag_dirty_roots(root->fs_info); |
| 1188 | trans = btrfs_start_transaction(root, 1); |
| 1189 | ret = btrfs_commit_transaction(trans, root); |
| 1190 | /* run commit again to drop the original snapshot */ |
| 1191 | trans = btrfs_start_transaction(root, 1); |
| 1192 | btrfs_commit_transaction(trans, root); |
| 1193 | ret = btrfs_write_and_wait_transaction(NULL, root); |
| 1194 | BUG_ON(ret); |
| 1195 | write_ctree_super(NULL, root); |
| 1196 | mutex_unlock(&fs_info->fs_mutex); |
| 1197 | |
| 1198 | if (fs_info->delalloc_bytes) { |
| 1199 | printk("btrfs: at unmount delalloc count %Lu\n", |
| 1200 | fs_info->delalloc_bytes); |
| 1201 | } |
| 1202 | if (fs_info->extent_root->node) |
| 1203 | free_extent_buffer(fs_info->extent_root->node); |
| 1204 | |
| 1205 | if (fs_info->tree_root->node) |
| 1206 | free_extent_buffer(fs_info->tree_root->node); |
| 1207 | |
| 1208 | if (root->fs_info->chunk_root->node); |
| 1209 | free_extent_buffer(root->fs_info->chunk_root->node); |
| 1210 | |
| 1211 | if (root->fs_info->dev_root->node); |
| 1212 | free_extent_buffer(root->fs_info->dev_root->node); |
| 1213 | |
| 1214 | free_extent_buffer(fs_info->sb_buffer); |
| 1215 | |
| 1216 | btrfs_free_block_groups(root->fs_info); |
| 1217 | del_fs_roots(fs_info); |
| 1218 | |
| 1219 | filemap_write_and_wait(fs_info->btree_inode->i_mapping); |
| 1220 | |
| 1221 | extent_io_tree_empty_lru(&fs_info->free_space_cache); |
| 1222 | extent_io_tree_empty_lru(&fs_info->block_group_cache); |
| 1223 | extent_io_tree_empty_lru(&fs_info->pinned_extents); |
| 1224 | extent_io_tree_empty_lru(&fs_info->pending_del); |
| 1225 | extent_io_tree_empty_lru(&fs_info->extent_ins); |
| 1226 | extent_io_tree_empty_lru(&BTRFS_I(fs_info->btree_inode)->io_tree); |
| 1227 | |
| 1228 | truncate_inode_pages(fs_info->btree_inode->i_mapping, 0); |
| 1229 | flush_workqueue(end_io_workqueue); |
| 1230 | destroy_workqueue(end_io_workqueue); |
| 1231 | |
| 1232 | iput(fs_info->btree_inode); |
| 1233 | #if 0 |
| 1234 | while(!list_empty(&fs_info->hashers)) { |
| 1235 | struct btrfs_hasher *hasher; |
| 1236 | hasher = list_entry(fs_info->hashers.next, struct btrfs_hasher, |
| 1237 | hashers); |
| 1238 | list_del(&hasher->hashers); |
| 1239 | crypto_free_hash(&fs_info->hash_tfm); |
| 1240 | kfree(hasher); |
| 1241 | } |
| 1242 | #endif |
| 1243 | close_all_devices(fs_info); |
| 1244 | btrfs_mapping_tree_free(&fs_info->mapping_tree); |
| 1245 | bdi_destroy(&fs_info->bdi); |
| 1246 | |
| 1247 | kfree(fs_info->extent_root); |
| 1248 | kfree(fs_info->tree_root); |
| 1249 | kfree(fs_info->chunk_root); |
| 1250 | kfree(fs_info->dev_root); |
| 1251 | return 0; |
| 1252 | } |
| 1253 | |
| 1254 | int btrfs_buffer_uptodate(struct extent_buffer *buf) |
| 1255 | { |
| 1256 | struct inode *btree_inode = buf->first_page->mapping->host; |
| 1257 | return extent_buffer_uptodate(&BTRFS_I(btree_inode)->io_tree, buf); |
| 1258 | } |
| 1259 | |
| 1260 | int btrfs_set_buffer_uptodate(struct extent_buffer *buf) |
| 1261 | { |
| 1262 | struct inode *btree_inode = buf->first_page->mapping->host; |
| 1263 | return set_extent_buffer_uptodate(&BTRFS_I(btree_inode)->io_tree, |
| 1264 | buf); |
| 1265 | } |
| 1266 | |
| 1267 | void btrfs_mark_buffer_dirty(struct extent_buffer *buf) |
| 1268 | { |
| 1269 | struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root; |
| 1270 | u64 transid = btrfs_header_generation(buf); |
| 1271 | struct inode *btree_inode = root->fs_info->btree_inode; |
| 1272 | |
| 1273 | if (transid != root->fs_info->generation) { |
| 1274 | printk(KERN_CRIT "transid mismatch buffer %llu, found %Lu running %Lu\n", |
| 1275 | (unsigned long long)buf->start, |
| 1276 | transid, root->fs_info->generation); |
| 1277 | WARN_ON(1); |
| 1278 | } |
| 1279 | set_extent_buffer_dirty(&BTRFS_I(btree_inode)->io_tree, buf); |
| 1280 | } |
| 1281 | |
| 1282 | void btrfs_throttle(struct btrfs_root *root) |
| 1283 | { |
| 1284 | struct backing_dev_info *bdi; |
| 1285 | |
| 1286 | bdi = root->fs_info->sb->s_bdev->bd_inode->i_mapping->backing_dev_info; |
| 1287 | if (root->fs_info->throttles && bdi_write_congested(bdi)) { |
| 1288 | #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,18) |
| 1289 | congestion_wait(WRITE, HZ/20); |
| 1290 | #else |
| 1291 | blk_congestion_wait(WRITE, HZ/20); |
| 1292 | #endif |
| 1293 | } |
| 1294 | } |
| 1295 | |
| 1296 | void btrfs_btree_balance_dirty(struct btrfs_root *root, unsigned long nr) |
| 1297 | { |
| 1298 | balance_dirty_pages_ratelimited_nr( |
| 1299 | root->fs_info->btree_inode->i_mapping, 1); |
| 1300 | } |
| 1301 | |
| 1302 | void btrfs_set_buffer_defrag(struct extent_buffer *buf) |
| 1303 | { |
| 1304 | struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root; |
| 1305 | struct inode *btree_inode = root->fs_info->btree_inode; |
| 1306 | set_extent_bits(&BTRFS_I(btree_inode)->io_tree, buf->start, |
| 1307 | buf->start + buf->len - 1, EXTENT_DEFRAG, GFP_NOFS); |
| 1308 | } |
| 1309 | |
| 1310 | void btrfs_set_buffer_defrag_done(struct extent_buffer *buf) |
| 1311 | { |
| 1312 | struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root; |
| 1313 | struct inode *btree_inode = root->fs_info->btree_inode; |
| 1314 | set_extent_bits(&BTRFS_I(btree_inode)->io_tree, buf->start, |
| 1315 | buf->start + buf->len - 1, EXTENT_DEFRAG_DONE, |
| 1316 | GFP_NOFS); |
| 1317 | } |
| 1318 | |
| 1319 | int btrfs_buffer_defrag(struct extent_buffer *buf) |
| 1320 | { |
| 1321 | struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root; |
| 1322 | struct inode *btree_inode = root->fs_info->btree_inode; |
| 1323 | return test_range_bit(&BTRFS_I(btree_inode)->io_tree, |
| 1324 | buf->start, buf->start + buf->len - 1, EXTENT_DEFRAG, 0); |
| 1325 | } |
| 1326 | |
| 1327 | int btrfs_buffer_defrag_done(struct extent_buffer *buf) |
| 1328 | { |
| 1329 | struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root; |
| 1330 | struct inode *btree_inode = root->fs_info->btree_inode; |
| 1331 | return test_range_bit(&BTRFS_I(btree_inode)->io_tree, |
| 1332 | buf->start, buf->start + buf->len - 1, |
| 1333 | EXTENT_DEFRAG_DONE, 0); |
| 1334 | } |
| 1335 | |
| 1336 | int btrfs_clear_buffer_defrag_done(struct extent_buffer *buf) |
| 1337 | { |
| 1338 | struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root; |
| 1339 | struct inode *btree_inode = root->fs_info->btree_inode; |
| 1340 | return clear_extent_bits(&BTRFS_I(btree_inode)->io_tree, |
| 1341 | buf->start, buf->start + buf->len - 1, |
| 1342 | EXTENT_DEFRAG_DONE, GFP_NOFS); |
| 1343 | } |
| 1344 | |
| 1345 | int btrfs_clear_buffer_defrag(struct extent_buffer *buf) |
| 1346 | { |
| 1347 | struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root; |
| 1348 | struct inode *btree_inode = root->fs_info->btree_inode; |
| 1349 | return clear_extent_bits(&BTRFS_I(btree_inode)->io_tree, |
| 1350 | buf->start, buf->start + buf->len - 1, |
| 1351 | EXTENT_DEFRAG, GFP_NOFS); |
| 1352 | } |
| 1353 | |
| 1354 | int btrfs_read_buffer(struct extent_buffer *buf) |
| 1355 | { |
| 1356 | struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root; |
| 1357 | struct inode *btree_inode = root->fs_info->btree_inode; |
| 1358 | int ret; |
| 1359 | ret = read_extent_buffer_pages(&BTRFS_I(btree_inode)->io_tree, |
| 1360 | buf, 0, 1, btree_get_extent); |
| 1361 | if (ret == 0) { |
| 1362 | buf->flags |= EXTENT_UPTODATE; |
| 1363 | } |
| 1364 | return ret; |
| 1365 | } |
| 1366 | |
| 1367 | static struct extent_io_ops btree_extent_io_ops = { |
| 1368 | .writepage_io_hook = btree_writepage_io_hook, |
| 1369 | .readpage_end_io_hook = btree_readpage_end_io_hook, |
| 1370 | .submit_bio_hook = btree_submit_bio_hook, |
| 1371 | /* note we're sharing with inode.c for the merge bio hook */ |
| 1372 | .merge_bio_hook = btrfs_merge_bio_hook, |
| 1373 | }; |