aboutsummaryrefslogtreecommitdiffstats
path: root/fs/btrfs/inode.c
diff options
context:
space:
mode:
Diffstat (limited to 'fs/btrfs/inode.c')
-rw-r--r--fs/btrfs/inode.c387
1 files changed, 224 insertions, 163 deletions
diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
index 72f31ecb5c9..4a0107e1874 100644
--- a/fs/btrfs/inode.c
+++ b/fs/btrfs/inode.c
@@ -122,10 +122,10 @@ static noinline int insert_inline_extent(struct btrfs_trans_handle *trans,
122 size_t cur_size = size; 122 size_t cur_size = size;
123 size_t datasize; 123 size_t datasize;
124 unsigned long offset; 124 unsigned long offset;
125 int use_compress = 0; 125 int compress_type = BTRFS_COMPRESS_NONE;
126 126
127 if (compressed_size && compressed_pages) { 127 if (compressed_size && compressed_pages) {
128 use_compress = 1; 128 compress_type = root->fs_info->compress_type;
129 cur_size = compressed_size; 129 cur_size = compressed_size;
130 } 130 }
131 131
@@ -159,7 +159,7 @@ static noinline int insert_inline_extent(struct btrfs_trans_handle *trans,
159 btrfs_set_file_extent_ram_bytes(leaf, ei, size); 159 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
160 ptr = btrfs_file_extent_inline_start(ei); 160 ptr = btrfs_file_extent_inline_start(ei);
161 161
162 if (use_compress) { 162 if (compress_type != BTRFS_COMPRESS_NONE) {
163 struct page *cpage; 163 struct page *cpage;
164 int i = 0; 164 int i = 0;
165 while (compressed_size > 0) { 165 while (compressed_size > 0) {
@@ -176,7 +176,7 @@ static noinline int insert_inline_extent(struct btrfs_trans_handle *trans,
176 compressed_size -= cur_size; 176 compressed_size -= cur_size;
177 } 177 }
178 btrfs_set_file_extent_compression(leaf, ei, 178 btrfs_set_file_extent_compression(leaf, ei,
179 BTRFS_COMPRESS_ZLIB); 179 compress_type);
180 } else { 180 } else {
181 page = find_get_page(inode->i_mapping, 181 page = find_get_page(inode->i_mapping,
182 start >> PAGE_CACHE_SHIFT); 182 start >> PAGE_CACHE_SHIFT);
@@ -263,6 +263,7 @@ struct async_extent {
263 u64 compressed_size; 263 u64 compressed_size;
264 struct page **pages; 264 struct page **pages;
265 unsigned long nr_pages; 265 unsigned long nr_pages;
266 int compress_type;
266 struct list_head list; 267 struct list_head list;
267}; 268};
268 269
@@ -280,7 +281,8 @@ static noinline int add_async_extent(struct async_cow *cow,
280 u64 start, u64 ram_size, 281 u64 start, u64 ram_size,
281 u64 compressed_size, 282 u64 compressed_size,
282 struct page **pages, 283 struct page **pages,
283 unsigned long nr_pages) 284 unsigned long nr_pages,
285 int compress_type)
284{ 286{
285 struct async_extent *async_extent; 287 struct async_extent *async_extent;
286 288
@@ -290,6 +292,7 @@ static noinline int add_async_extent(struct async_cow *cow,
290 async_extent->compressed_size = compressed_size; 292 async_extent->compressed_size = compressed_size;
291 async_extent->pages = pages; 293 async_extent->pages = pages;
292 async_extent->nr_pages = nr_pages; 294 async_extent->nr_pages = nr_pages;
295 async_extent->compress_type = compress_type;
293 list_add_tail(&async_extent->list, &cow->extents); 296 list_add_tail(&async_extent->list, &cow->extents);
294 return 0; 297 return 0;
295} 298}
@@ -332,6 +335,7 @@ static noinline int compress_file_range(struct inode *inode,
332 unsigned long max_uncompressed = 128 * 1024; 335 unsigned long max_uncompressed = 128 * 1024;
333 int i; 336 int i;
334 int will_compress; 337 int will_compress;
338 int compress_type = root->fs_info->compress_type;
335 339
336 actual_end = min_t(u64, isize, end + 1); 340 actual_end = min_t(u64, isize, end + 1);
337again: 341again:
@@ -381,12 +385,16 @@ again:
381 WARN_ON(pages); 385 WARN_ON(pages);
382 pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS); 386 pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS);
383 387
384 ret = btrfs_zlib_compress_pages(inode->i_mapping, start, 388 if (BTRFS_I(inode)->force_compress)
385 total_compressed, pages, 389 compress_type = BTRFS_I(inode)->force_compress;
386 nr_pages, &nr_pages_ret, 390
387 &total_in, 391 ret = btrfs_compress_pages(compress_type,
388 &total_compressed, 392 inode->i_mapping, start,
389 max_compressed); 393 total_compressed, pages,
394 nr_pages, &nr_pages_ret,
395 &total_in,
396 &total_compressed,
397 max_compressed);
390 398
391 if (!ret) { 399 if (!ret) {
392 unsigned long offset = total_compressed & 400 unsigned long offset = total_compressed &
@@ -408,7 +416,7 @@ again:
408 } 416 }
409 if (start == 0) { 417 if (start == 0) {
410 trans = btrfs_join_transaction(root, 1); 418 trans = btrfs_join_transaction(root, 1);
411 BUG_ON(!trans); 419 BUG_ON(IS_ERR(trans));
412 btrfs_set_trans_block_group(trans, inode); 420 btrfs_set_trans_block_group(trans, inode);
413 trans->block_rsv = &root->fs_info->delalloc_block_rsv; 421 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
414 422
@@ -493,7 +501,8 @@ again:
493 * and will submit them to the elevator. 501 * and will submit them to the elevator.
494 */ 502 */
495 add_async_extent(async_cow, start, num_bytes, 503 add_async_extent(async_cow, start, num_bytes,
496 total_compressed, pages, nr_pages_ret); 504 total_compressed, pages, nr_pages_ret,
505 compress_type);
497 506
498 if (start + num_bytes < end) { 507 if (start + num_bytes < end) {
499 start += num_bytes; 508 start += num_bytes;
@@ -515,7 +524,8 @@ cleanup_and_bail_uncompressed:
515 __set_page_dirty_nobuffers(locked_page); 524 __set_page_dirty_nobuffers(locked_page);
516 /* unlocked later on in the async handlers */ 525 /* unlocked later on in the async handlers */
517 } 526 }
518 add_async_extent(async_cow, start, end - start + 1, 0, NULL, 0); 527 add_async_extent(async_cow, start, end - start + 1,
528 0, NULL, 0, BTRFS_COMPRESS_NONE);
519 *num_added += 1; 529 *num_added += 1;
520 } 530 }
521 531
@@ -602,6 +612,7 @@ retry:
602 GFP_NOFS); 612 GFP_NOFS);
603 613
604 trans = btrfs_join_transaction(root, 1); 614 trans = btrfs_join_transaction(root, 1);
615 BUG_ON(IS_ERR(trans));
605 ret = btrfs_reserve_extent(trans, root, 616 ret = btrfs_reserve_extent(trans, root,
606 async_extent->compressed_size, 617 async_extent->compressed_size,
607 async_extent->compressed_size, 618 async_extent->compressed_size,
@@ -633,6 +644,7 @@ retry:
633 async_extent->ram_size - 1, 0); 644 async_extent->ram_size - 1, 0);
634 645
635 em = alloc_extent_map(GFP_NOFS); 646 em = alloc_extent_map(GFP_NOFS);
647 BUG_ON(!em);
636 em->start = async_extent->start; 648 em->start = async_extent->start;
637 em->len = async_extent->ram_size; 649 em->len = async_extent->ram_size;
638 em->orig_start = em->start; 650 em->orig_start = em->start;
@@ -640,6 +652,7 @@ retry:
640 em->block_start = ins.objectid; 652 em->block_start = ins.objectid;
641 em->block_len = ins.offset; 653 em->block_len = ins.offset;
642 em->bdev = root->fs_info->fs_devices->latest_bdev; 654 em->bdev = root->fs_info->fs_devices->latest_bdev;
655 em->compress_type = async_extent->compress_type;
643 set_bit(EXTENT_FLAG_PINNED, &em->flags); 656 set_bit(EXTENT_FLAG_PINNED, &em->flags);
644 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); 657 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
645 658
@@ -656,11 +669,13 @@ retry:
656 async_extent->ram_size - 1, 0); 669 async_extent->ram_size - 1, 0);
657 } 670 }
658 671
659 ret = btrfs_add_ordered_extent(inode, async_extent->start, 672 ret = btrfs_add_ordered_extent_compress(inode,
660 ins.objectid, 673 async_extent->start,
661 async_extent->ram_size, 674 ins.objectid,
662 ins.offset, 675 async_extent->ram_size,
663 BTRFS_ORDERED_COMPRESSED); 676 ins.offset,
677 BTRFS_ORDERED_COMPRESSED,
678 async_extent->compress_type);
664 BUG_ON(ret); 679 BUG_ON(ret);
665 680
666 /* 681 /*
@@ -758,7 +773,7 @@ static noinline int cow_file_range(struct inode *inode,
758 773
759 BUG_ON(root == root->fs_info->tree_root); 774 BUG_ON(root == root->fs_info->tree_root);
760 trans = btrfs_join_transaction(root, 1); 775 trans = btrfs_join_transaction(root, 1);
761 BUG_ON(!trans); 776 BUG_ON(IS_ERR(trans));
762 btrfs_set_trans_block_group(trans, inode); 777 btrfs_set_trans_block_group(trans, inode);
763 trans->block_rsv = &root->fs_info->delalloc_block_rsv; 778 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
764 779
@@ -806,6 +821,7 @@ static noinline int cow_file_range(struct inode *inode,
806 BUG_ON(ret); 821 BUG_ON(ret);
807 822
808 em = alloc_extent_map(GFP_NOFS); 823 em = alloc_extent_map(GFP_NOFS);
824 BUG_ON(!em);
809 em->start = start; 825 em->start = start;
810 em->orig_start = em->start; 826 em->orig_start = em->start;
811 ram_size = ins.offset; 827 ram_size = ins.offset;
@@ -1036,7 +1052,7 @@ static noinline int run_delalloc_nocow(struct inode *inode,
1036 } else { 1052 } else {
1037 trans = btrfs_join_transaction(root, 1); 1053 trans = btrfs_join_transaction(root, 1);
1038 } 1054 }
1039 BUG_ON(!trans); 1055 BUG_ON(IS_ERR(trans));
1040 1056
1041 cow_start = (u64)-1; 1057 cow_start = (u64)-1;
1042 cur_offset = start; 1058 cur_offset = start;
@@ -1155,6 +1171,7 @@ out_check:
1155 struct extent_map_tree *em_tree; 1171 struct extent_map_tree *em_tree;
1156 em_tree = &BTRFS_I(inode)->extent_tree; 1172 em_tree = &BTRFS_I(inode)->extent_tree;
1157 em = alloc_extent_map(GFP_NOFS); 1173 em = alloc_extent_map(GFP_NOFS);
1174 BUG_ON(!em);
1158 em->start = cur_offset; 1175 em->start = cur_offset;
1159 em->orig_start = em->start; 1176 em->orig_start = em->start;
1160 em->len = num_bytes; 1177 em->len = num_bytes;
@@ -1544,6 +1561,7 @@ out:
1544out_page: 1561out_page:
1545 unlock_page(page); 1562 unlock_page(page);
1546 page_cache_release(page); 1563 page_cache_release(page);
1564 kfree(fixup);
1547} 1565}
1548 1566
1549/* 1567/*
@@ -1670,7 +1688,7 @@ static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
1670 struct btrfs_ordered_extent *ordered_extent = NULL; 1688 struct btrfs_ordered_extent *ordered_extent = NULL;
1671 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; 1689 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1672 struct extent_state *cached_state = NULL; 1690 struct extent_state *cached_state = NULL;
1673 int compressed = 0; 1691 int compress_type = 0;
1674 int ret; 1692 int ret;
1675 bool nolock = false; 1693 bool nolock = false;
1676 1694
@@ -1690,7 +1708,7 @@ static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
1690 trans = btrfs_join_transaction_nolock(root, 1); 1708 trans = btrfs_join_transaction_nolock(root, 1);
1691 else 1709 else
1692 trans = btrfs_join_transaction(root, 1); 1710 trans = btrfs_join_transaction(root, 1);
1693 BUG_ON(!trans); 1711 BUG_ON(IS_ERR(trans));
1694 btrfs_set_trans_block_group(trans, inode); 1712 btrfs_set_trans_block_group(trans, inode);
1695 trans->block_rsv = &root->fs_info->delalloc_block_rsv; 1713 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
1696 ret = btrfs_update_inode(trans, root, inode); 1714 ret = btrfs_update_inode(trans, root, inode);
@@ -1707,13 +1725,14 @@ static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
1707 trans = btrfs_join_transaction_nolock(root, 1); 1725 trans = btrfs_join_transaction_nolock(root, 1);
1708 else 1726 else
1709 trans = btrfs_join_transaction(root, 1); 1727 trans = btrfs_join_transaction(root, 1);
1728 BUG_ON(IS_ERR(trans));
1710 btrfs_set_trans_block_group(trans, inode); 1729 btrfs_set_trans_block_group(trans, inode);
1711 trans->block_rsv = &root->fs_info->delalloc_block_rsv; 1730 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
1712 1731
1713 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags)) 1732 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
1714 compressed = 1; 1733 compress_type = ordered_extent->compress_type;
1715 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) { 1734 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
1716 BUG_ON(compressed); 1735 BUG_ON(compress_type);
1717 ret = btrfs_mark_extent_written(trans, inode, 1736 ret = btrfs_mark_extent_written(trans, inode,
1718 ordered_extent->file_offset, 1737 ordered_extent->file_offset,
1719 ordered_extent->file_offset + 1738 ordered_extent->file_offset +
@@ -1727,7 +1746,7 @@ static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
1727 ordered_extent->disk_len, 1746 ordered_extent->disk_len,
1728 ordered_extent->len, 1747 ordered_extent->len,
1729 ordered_extent->len, 1748 ordered_extent->len,
1730 compressed, 0, 0, 1749 compress_type, 0, 0,
1731 BTRFS_FILE_EXTENT_REG); 1750 BTRFS_FILE_EXTENT_REG);
1732 unpin_extent_cache(&BTRFS_I(inode)->extent_tree, 1751 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
1733 ordered_extent->file_offset, 1752 ordered_extent->file_offset,
@@ -1829,6 +1848,8 @@ static int btrfs_io_failed_hook(struct bio *failed_bio,
1829 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { 1848 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
1830 logical = em->block_start; 1849 logical = em->block_start;
1831 failrec->bio_flags = EXTENT_BIO_COMPRESSED; 1850 failrec->bio_flags = EXTENT_BIO_COMPRESSED;
1851 extent_set_compress_type(&failrec->bio_flags,
1852 em->compress_type);
1832 } 1853 }
1833 failrec->logical = logical; 1854 failrec->logical = logical;
1834 free_extent_map(em); 1855 free_extent_map(em);
@@ -1892,7 +1913,7 @@ static int btrfs_clean_io_failures(struct inode *inode, u64 start)
1892 1913
1893 private = 0; 1914 private = 0;
1894 if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private, 1915 if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
1895 (u64)-1, 1, EXTENT_DIRTY)) { 1916 (u64)-1, 1, EXTENT_DIRTY, 0)) {
1896 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree, 1917 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
1897 start, &private_failure); 1918 start, &private_failure);
1898 if (ret == 0) { 1919 if (ret == 0) {
@@ -2339,6 +2360,7 @@ void btrfs_orphan_cleanup(struct btrfs_root *root)
2339 */ 2360 */
2340 if (is_bad_inode(inode)) { 2361 if (is_bad_inode(inode)) {
2341 trans = btrfs_start_transaction(root, 0); 2362 trans = btrfs_start_transaction(root, 0);
2363 BUG_ON(IS_ERR(trans));
2342 btrfs_orphan_del(trans, inode); 2364 btrfs_orphan_del(trans, inode);
2343 btrfs_end_transaction(trans, root); 2365 btrfs_end_transaction(trans, root);
2344 iput(inode); 2366 iput(inode);
@@ -2366,6 +2388,7 @@ void btrfs_orphan_cleanup(struct btrfs_root *root)
2366 2388
2367 if (root->orphan_block_rsv || root->orphan_item_inserted) { 2389 if (root->orphan_block_rsv || root->orphan_item_inserted) {
2368 trans = btrfs_join_transaction(root, 1); 2390 trans = btrfs_join_transaction(root, 1);
2391 BUG_ON(IS_ERR(trans));
2369 btrfs_end_transaction(trans, root); 2392 btrfs_end_transaction(trans, root);
2370 } 2393 }
2371 2394
@@ -2626,7 +2649,7 @@ int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2626 path = btrfs_alloc_path(); 2649 path = btrfs_alloc_path();
2627 if (!path) { 2650 if (!path) {
2628 ret = -ENOMEM; 2651 ret = -ENOMEM;
2629 goto err; 2652 goto out;
2630 } 2653 }
2631 2654
2632 path->leave_spinning = 1; 2655 path->leave_spinning = 1;
@@ -2699,9 +2722,10 @@ static int check_path_shared(struct btrfs_root *root,
2699 struct extent_buffer *eb; 2722 struct extent_buffer *eb;
2700 int level; 2723 int level;
2701 u64 refs = 1; 2724 u64 refs = 1;
2702 int uninitialized_var(ret);
2703 2725
2704 for (level = 0; level < BTRFS_MAX_LEVEL; level++) { 2726 for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
2727 int ret;
2728
2705 if (!path->nodes[level]) 2729 if (!path->nodes[level])
2706 break; 2730 break;
2707 eb = path->nodes[level]; 2731 eb = path->nodes[level];
@@ -2712,7 +2736,7 @@ static int check_path_shared(struct btrfs_root *root,
2712 if (refs > 1) 2736 if (refs > 1)
2713 return 1; 2737 return 1;
2714 } 2738 }
2715 return ret; /* XXX callers? */ 2739 return 0;
2716} 2740}
2717 2741
2718/* 2742/*
@@ -3671,8 +3695,12 @@ static int btrfs_setattr_size(struct inode *inode, struct iattr *attr)
3671static int btrfs_setattr(struct dentry *dentry, struct iattr *attr) 3695static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
3672{ 3696{
3673 struct inode *inode = dentry->d_inode; 3697 struct inode *inode = dentry->d_inode;
3698 struct btrfs_root *root = BTRFS_I(inode)->root;
3674 int err; 3699 int err;
3675 3700
3701 if (btrfs_root_readonly(root))
3702 return -EROFS;
3703
3676 err = inode_change_ok(inode, attr); 3704 err = inode_change_ok(inode, attr);
3677 if (err) 3705 if (err)
3678 return err; 3706 return err;
@@ -4084,8 +4112,6 @@ struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
4084 int index; 4112 int index;
4085 int ret; 4113 int ret;
4086 4114
4087 dentry->d_op = &btrfs_dentry_operations;
4088
4089 if (dentry->d_name.len > BTRFS_NAME_LEN) 4115 if (dentry->d_name.len > BTRFS_NAME_LEN)
4090 return ERR_PTR(-ENAMETOOLONG); 4116 return ERR_PTR(-ENAMETOOLONG);
4091 4117
@@ -4117,7 +4143,7 @@ struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
4117 } 4143 }
4118 srcu_read_unlock(&root->fs_info->subvol_srcu, index); 4144 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
4119 4145
4120 if (root != sub_root) { 4146 if (!IS_ERR(inode) && root != sub_root) {
4121 down_read(&root->fs_info->cleanup_work_sem); 4147 down_read(&root->fs_info->cleanup_work_sem);
4122 if (!(inode->i_sb->s_flags & MS_RDONLY)) 4148 if (!(inode->i_sb->s_flags & MS_RDONLY))
4123 btrfs_orphan_cleanup(sub_root); 4149 btrfs_orphan_cleanup(sub_root);
@@ -4127,7 +4153,7 @@ struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
4127 return inode; 4153 return inode;
4128} 4154}
4129 4155
4130static int btrfs_dentry_delete(struct dentry *dentry) 4156static int btrfs_dentry_delete(const struct dentry *dentry)
4131{ 4157{
4132 struct btrfs_root *root; 4158 struct btrfs_root *root;
4133 4159
@@ -4330,6 +4356,8 @@ int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
4330 trans = btrfs_join_transaction_nolock(root, 1); 4356 trans = btrfs_join_transaction_nolock(root, 1);
4331 else 4357 else
4332 trans = btrfs_join_transaction(root, 1); 4358 trans = btrfs_join_transaction(root, 1);
4359 if (IS_ERR(trans))
4360 return PTR_ERR(trans);
4333 btrfs_set_trans_block_group(trans, inode); 4361 btrfs_set_trans_block_group(trans, inode);
4334 if (nolock) 4362 if (nolock)
4335 ret = btrfs_end_transaction_nolock(trans, root); 4363 ret = btrfs_end_transaction_nolock(trans, root);
@@ -4355,6 +4383,7 @@ void btrfs_dirty_inode(struct inode *inode)
4355 return; 4383 return;
4356 4384
4357 trans = btrfs_join_transaction(root, 1); 4385 trans = btrfs_join_transaction(root, 1);
4386 BUG_ON(IS_ERR(trans));
4358 btrfs_set_trans_block_group(trans, inode); 4387 btrfs_set_trans_block_group(trans, inode);
4359 4388
4360 ret = btrfs_update_inode(trans, root, inode); 4389 ret = btrfs_update_inode(trans, root, inode);
@@ -4777,9 +4806,6 @@ static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
4777 int err; 4806 int err;
4778 int drop_inode = 0; 4807 int drop_inode = 0;
4779 4808
4780 if (inode->i_nlink == 0)
4781 return -ENOENT;
4782
4783 /* do not allow sys_link's with other subvols of the same device */ 4809 /* do not allow sys_link's with other subvols of the same device */
4784 if (root->objectid != BTRFS_I(inode)->root->objectid) 4810 if (root->objectid != BTRFS_I(inode)->root->objectid)
4785 return -EPERM; 4811 return -EPERM;
@@ -4792,10 +4818,11 @@ static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
4792 goto fail; 4818 goto fail;
4793 4819
4794 /* 4820 /*
4795 * 1 item for inode ref 4821 * 2 items for inode and inode ref
4796 * 2 items for dir items 4822 * 2 items for dir items
4823 * 1 item for parent inode
4797 */ 4824 */
4798 trans = btrfs_start_transaction(root, 3); 4825 trans = btrfs_start_transaction(root, 5);
4799 if (IS_ERR(trans)) { 4826 if (IS_ERR(trans)) {
4800 err = PTR_ERR(trans); 4827 err = PTR_ERR(trans);
4801 goto fail; 4828 goto fail;
@@ -4930,8 +4957,10 @@ static noinline int uncompress_inline(struct btrfs_path *path,
4930 size_t max_size; 4957 size_t max_size;
4931 unsigned long inline_size; 4958 unsigned long inline_size;
4932 unsigned long ptr; 4959 unsigned long ptr;
4960 int compress_type;
4933 4961
4934 WARN_ON(pg_offset != 0); 4962 WARN_ON(pg_offset != 0);
4963 compress_type = btrfs_file_extent_compression(leaf, item);
4935 max_size = btrfs_file_extent_ram_bytes(leaf, item); 4964 max_size = btrfs_file_extent_ram_bytes(leaf, item);
4936 inline_size = btrfs_file_extent_inline_item_len(leaf, 4965 inline_size = btrfs_file_extent_inline_item_len(leaf,
4937 btrfs_item_nr(leaf, path->slots[0])); 4966 btrfs_item_nr(leaf, path->slots[0]));
@@ -4941,8 +4970,8 @@ static noinline int uncompress_inline(struct btrfs_path *path,
4941 read_extent_buffer(leaf, tmp, ptr, inline_size); 4970 read_extent_buffer(leaf, tmp, ptr, inline_size);
4942 4971
4943 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size); 4972 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
4944 ret = btrfs_zlib_decompress(tmp, page, extent_offset, 4973 ret = btrfs_decompress(compress_type, tmp, page,
4945 inline_size, max_size); 4974 extent_offset, inline_size, max_size);
4946 if (ret) { 4975 if (ret) {
4947 char *kaddr = kmap_atomic(page, KM_USER0); 4976 char *kaddr = kmap_atomic(page, KM_USER0);
4948 unsigned long copy_size = min_t(u64, 4977 unsigned long copy_size = min_t(u64,
@@ -4984,7 +5013,7 @@ struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
4984 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; 5013 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
4985 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; 5014 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4986 struct btrfs_trans_handle *trans = NULL; 5015 struct btrfs_trans_handle *trans = NULL;
4987 int compressed; 5016 int compress_type;
4988 5017
4989again: 5018again:
4990 read_lock(&em_tree->lock); 5019 read_lock(&em_tree->lock);
@@ -5043,7 +5072,7 @@ again:
5043 5072
5044 found_type = btrfs_file_extent_type(leaf, item); 5073 found_type = btrfs_file_extent_type(leaf, item);
5045 extent_start = found_key.offset; 5074 extent_start = found_key.offset;
5046 compressed = btrfs_file_extent_compression(leaf, item); 5075 compress_type = btrfs_file_extent_compression(leaf, item);
5047 if (found_type == BTRFS_FILE_EXTENT_REG || 5076 if (found_type == BTRFS_FILE_EXTENT_REG ||
5048 found_type == BTRFS_FILE_EXTENT_PREALLOC) { 5077 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
5049 extent_end = extent_start + 5078 extent_end = extent_start +
@@ -5089,8 +5118,9 @@ again:
5089 em->block_start = EXTENT_MAP_HOLE; 5118 em->block_start = EXTENT_MAP_HOLE;
5090 goto insert; 5119 goto insert;
5091 } 5120 }
5092 if (compressed) { 5121 if (compress_type != BTRFS_COMPRESS_NONE) {
5093 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); 5122 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
5123 em->compress_type = compress_type;
5094 em->block_start = bytenr; 5124 em->block_start = bytenr;
5095 em->block_len = btrfs_file_extent_disk_num_bytes(leaf, 5125 em->block_len = btrfs_file_extent_disk_num_bytes(leaf,
5096 item); 5126 item);
@@ -5124,12 +5154,14 @@ again:
5124 em->len = (copy_size + root->sectorsize - 1) & 5154 em->len = (copy_size + root->sectorsize - 1) &
5125 ~((u64)root->sectorsize - 1); 5155 ~((u64)root->sectorsize - 1);
5126 em->orig_start = EXTENT_MAP_INLINE; 5156 em->orig_start = EXTENT_MAP_INLINE;
5127 if (compressed) 5157 if (compress_type) {
5128 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); 5158 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
5159 em->compress_type = compress_type;
5160 }
5129 ptr = btrfs_file_extent_inline_start(item) + extent_offset; 5161 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
5130 if (create == 0 && !PageUptodate(page)) { 5162 if (create == 0 && !PageUptodate(page)) {
5131 if (btrfs_file_extent_compression(leaf, item) == 5163 if (btrfs_file_extent_compression(leaf, item) !=
5132 BTRFS_COMPRESS_ZLIB) { 5164 BTRFS_COMPRESS_NONE) {
5133 ret = uncompress_inline(path, inode, page, 5165 ret = uncompress_inline(path, inode, page,
5134 pg_offset, 5166 pg_offset,
5135 extent_offset, item); 5167 extent_offset, item);
@@ -5154,6 +5186,8 @@ again:
5154 em = NULL; 5186 em = NULL;
5155 btrfs_release_path(root, path); 5187 btrfs_release_path(root, path);
5156 trans = btrfs_join_transaction(root, 1); 5188 trans = btrfs_join_transaction(root, 1);
5189 if (IS_ERR(trans))
5190 return ERR_CAST(trans);
5157 goto again; 5191 goto again;
5158 } 5192 }
5159 map = kmap(page); 5193 map = kmap(page);
@@ -5244,6 +5278,128 @@ out:
5244 return em; 5278 return em;
5245} 5279}
5246 5280
5281struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
5282 size_t pg_offset, u64 start, u64 len,
5283 int create)
5284{
5285 struct extent_map *em;
5286 struct extent_map *hole_em = NULL;
5287 u64 range_start = start;
5288 u64 end;
5289 u64 found;
5290 u64 found_end;
5291 int err = 0;
5292
5293 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
5294 if (IS_ERR(em))
5295 return em;
5296 if (em) {
5297 /*
5298 * if our em maps to a hole, there might
5299 * actually be delalloc bytes behind it
5300 */
5301 if (em->block_start != EXTENT_MAP_HOLE)
5302 return em;
5303 else
5304 hole_em = em;
5305 }
5306
5307 /* check to see if we've wrapped (len == -1 or similar) */
5308 end = start + len;
5309 if (end < start)
5310 end = (u64)-1;
5311 else
5312 end -= 1;
5313
5314 em = NULL;
5315
5316 /* ok, we didn't find anything, lets look for delalloc */
5317 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
5318 end, len, EXTENT_DELALLOC, 1);
5319 found_end = range_start + found;
5320 if (found_end < range_start)
5321 found_end = (u64)-1;
5322
5323 /*
5324 * we didn't find anything useful, return
5325 * the original results from get_extent()
5326 */
5327 if (range_start > end || found_end <= start) {
5328 em = hole_em;
5329 hole_em = NULL;
5330 goto out;
5331 }
5332
5333 /* adjust the range_start to make sure it doesn't
5334 * go backwards from the start they passed in
5335 */
5336 range_start = max(start,range_start);
5337 found = found_end - range_start;
5338
5339 if (found > 0) {
5340 u64 hole_start = start;
5341 u64 hole_len = len;
5342
5343 em = alloc_extent_map(GFP_NOFS);
5344 if (!em) {
5345 err = -ENOMEM;
5346 goto out;
5347 }
5348 /*
5349 * when btrfs_get_extent can't find anything it
5350 * returns one huge hole
5351 *
5352 * make sure what it found really fits our range, and
5353 * adjust to make sure it is based on the start from
5354 * the caller
5355 */
5356 if (hole_em) {
5357 u64 calc_end = extent_map_end(hole_em);
5358
5359 if (calc_end <= start || (hole_em->start > end)) {
5360 free_extent_map(hole_em);
5361 hole_em = NULL;
5362 } else {
5363 hole_start = max(hole_em->start, start);
5364 hole_len = calc_end - hole_start;
5365 }
5366 }
5367 em->bdev = NULL;
5368 if (hole_em && range_start > hole_start) {
5369 /* our hole starts before our delalloc, so we
5370 * have to return just the parts of the hole
5371 * that go until the delalloc starts
5372 */
5373 em->len = min(hole_len,
5374 range_start - hole_start);
5375 em->start = hole_start;
5376 em->orig_start = hole_start;
5377 /*
5378 * don't adjust block start at all,
5379 * it is fixed at EXTENT_MAP_HOLE
5380 */
5381 em->block_start = hole_em->block_start;
5382 em->block_len = hole_len;
5383 } else {
5384 em->start = range_start;
5385 em->len = found;
5386 em->orig_start = range_start;
5387 em->block_start = EXTENT_MAP_DELALLOC;
5388 em->block_len = found;
5389 }
5390 } else if (hole_em) {
5391 return hole_em;
5392 }
5393out:
5394
5395 free_extent_map(hole_em);
5396 if (err) {
5397 free_extent_map(em);
5398 return ERR_PTR(err);
5399 }
5400 return em;
5401}
5402
5247static struct extent_map *btrfs_new_extent_direct(struct inode *inode, 5403static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
5248 u64 start, u64 len) 5404 u64 start, u64 len)
5249{ 5405{
@@ -5258,8 +5414,8 @@ static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
5258 btrfs_drop_extent_cache(inode, start, start + len - 1, 0); 5414 btrfs_drop_extent_cache(inode, start, start + len - 1, 0);
5259 5415
5260 trans = btrfs_join_transaction(root, 0); 5416 trans = btrfs_join_transaction(root, 0);
5261 if (!trans) 5417 if (IS_ERR(trans))
5262 return ERR_PTR(-ENOMEM); 5418 return ERR_CAST(trans);
5263 5419
5264 trans->block_rsv = &root->fs_info->delalloc_block_rsv; 5420 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
5265 5421
@@ -5483,7 +5639,7 @@ static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
5483 * while we look for nocow cross refs 5639 * while we look for nocow cross refs
5484 */ 5640 */
5485 trans = btrfs_join_transaction(root, 0); 5641 trans = btrfs_join_transaction(root, 0);
5486 if (!trans) 5642 if (IS_ERR(trans))
5487 goto must_cow; 5643 goto must_cow;
5488 5644
5489 if (can_nocow_odirect(trans, inode, start, len) == 1) { 5645 if (can_nocow_odirect(trans, inode, start, len) == 1) {
@@ -5618,7 +5774,7 @@ again:
5618 BUG_ON(!ordered); 5774 BUG_ON(!ordered);
5619 5775
5620 trans = btrfs_join_transaction(root, 1); 5776 trans = btrfs_join_transaction(root, 1);
5621 if (!trans) { 5777 if (IS_ERR(trans)) {
5622 err = -ENOMEM; 5778 err = -ENOMEM;
5623 goto out; 5779 goto out;
5624 } 5780 }
@@ -5898,6 +6054,7 @@ static void btrfs_submit_direct(int rw, struct bio *bio, struct inode *inode,
5898 if (!skip_sum) { 6054 if (!skip_sum) {
5899 dip->csums = kmalloc(sizeof(u32) * bio->bi_vcnt, GFP_NOFS); 6055 dip->csums = kmalloc(sizeof(u32) * bio->bi_vcnt, GFP_NOFS);
5900 if (!dip->csums) { 6056 if (!dip->csums) {
6057 kfree(dip);
5901 ret = -ENOMEM; 6058 ret = -ENOMEM;
5902 goto free_ordered; 6059 goto free_ordered;
5903 } 6060 }
@@ -6066,7 +6223,7 @@ out:
6066static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, 6223static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
6067 __u64 start, __u64 len) 6224 __u64 start, __u64 len)
6068{ 6225{
6069 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent); 6226 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
6070} 6227}
6071 6228
6072int btrfs_readpage(struct file *file, struct page *page) 6229int btrfs_readpage(struct file *file, struct page *page)
@@ -6479,7 +6636,7 @@ struct inode *btrfs_alloc_inode(struct super_block *sb)
6479 ei->ordered_data_close = 0; 6636 ei->ordered_data_close = 0;
6480 ei->orphan_meta_reserved = 0; 6637 ei->orphan_meta_reserved = 0;
6481 ei->dummy_inode = 0; 6638 ei->dummy_inode = 0;
6482 ei->force_compress = 0; 6639 ei->force_compress = BTRFS_COMPRESS_NONE;
6483 6640
6484 inode = &ei->vfs_inode; 6641 inode = &ei->vfs_inode;
6485 extent_map_tree_init(&ei->extent_tree, GFP_NOFS); 6642 extent_map_tree_init(&ei->extent_tree, GFP_NOFS);
@@ -6495,6 +6652,13 @@ struct inode *btrfs_alloc_inode(struct super_block *sb)
6495 return inode; 6652 return inode;
6496} 6653}
6497 6654
6655static void btrfs_i_callback(struct rcu_head *head)
6656{
6657 struct inode *inode = container_of(head, struct inode, i_rcu);
6658 INIT_LIST_HEAD(&inode->i_dentry);
6659 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
6660}
6661
6498void btrfs_destroy_inode(struct inode *inode) 6662void btrfs_destroy_inode(struct inode *inode)
6499{ 6663{
6500 struct btrfs_ordered_extent *ordered; 6664 struct btrfs_ordered_extent *ordered;
@@ -6564,7 +6728,7 @@ void btrfs_destroy_inode(struct inode *inode)
6564 inode_tree_del(inode); 6728 inode_tree_del(inode);
6565 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0); 6729 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
6566free: 6730free:
6567 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); 6731 call_rcu(&inode->i_rcu, btrfs_i_callback);
6568} 6732}
6569 6733
6570int btrfs_drop_inode(struct inode *inode) 6734int btrfs_drop_inode(struct inode *inode)
@@ -7093,122 +7257,20 @@ int btrfs_prealloc_file_range_trans(struct inode *inode,
7093 min_size, actual_len, alloc_hint, trans); 7257 min_size, actual_len, alloc_hint, trans);
7094} 7258}
7095 7259
7096static long btrfs_fallocate(struct inode *inode, int mode,
7097 loff_t offset, loff_t len)
7098{
7099 struct extent_state *cached_state = NULL;
7100 u64 cur_offset;
7101 u64 last_byte;
7102 u64 alloc_start;
7103 u64 alloc_end;
7104 u64 alloc_hint = 0;
7105 u64 locked_end;
7106 u64 mask = BTRFS_I(inode)->root->sectorsize - 1;
7107 struct extent_map *em;
7108 int ret;
7109
7110 alloc_start = offset & ~mask;
7111 alloc_end = (offset + len + mask) & ~mask;
7112
7113 /*
7114 * wait for ordered IO before we have any locks. We'll loop again
7115 * below with the locks held.
7116 */
7117 btrfs_wait_ordered_range(inode, alloc_start, alloc_end - alloc_start);
7118
7119 mutex_lock(&inode->i_mutex);
7120 ret = inode_newsize_ok(inode, alloc_end);
7121 if (ret)
7122 goto out;
7123
7124 if (alloc_start > inode->i_size) {
7125 ret = btrfs_cont_expand(inode, alloc_start);
7126 if (ret)
7127 goto out;
7128 }
7129
7130 ret = btrfs_check_data_free_space(inode, alloc_end - alloc_start);
7131 if (ret)
7132 goto out;
7133
7134 locked_end = alloc_end - 1;
7135 while (1) {
7136 struct btrfs_ordered_extent *ordered;
7137
7138 /* the extent lock is ordered inside the running
7139 * transaction
7140 */
7141 lock_extent_bits(&BTRFS_I(inode)->io_tree, alloc_start,
7142 locked_end, 0, &cached_state, GFP_NOFS);
7143 ordered = btrfs_lookup_first_ordered_extent(inode,
7144 alloc_end - 1);
7145 if (ordered &&
7146 ordered->file_offset + ordered->len > alloc_start &&
7147 ordered->file_offset < alloc_end) {
7148 btrfs_put_ordered_extent(ordered);
7149 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
7150 alloc_start, locked_end,
7151 &cached_state, GFP_NOFS);
7152 /*
7153 * we can't wait on the range with the transaction
7154 * running or with the extent lock held
7155 */
7156 btrfs_wait_ordered_range(inode, alloc_start,
7157 alloc_end - alloc_start);
7158 } else {
7159 if (ordered)
7160 btrfs_put_ordered_extent(ordered);
7161 break;
7162 }
7163 }
7164
7165 cur_offset = alloc_start;
7166 while (1) {
7167 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
7168 alloc_end - cur_offset, 0);
7169 BUG_ON(IS_ERR(em) || !em);
7170 last_byte = min(extent_map_end(em), alloc_end);
7171 last_byte = (last_byte + mask) & ~mask;
7172 if (em->block_start == EXTENT_MAP_HOLE ||
7173 (cur_offset >= inode->i_size &&
7174 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
7175 ret = btrfs_prealloc_file_range(inode, mode, cur_offset,
7176 last_byte - cur_offset,
7177 1 << inode->i_blkbits,
7178 offset + len,
7179 &alloc_hint);
7180 if (ret < 0) {
7181 free_extent_map(em);
7182 break;
7183 }
7184 }
7185 free_extent_map(em);
7186
7187 cur_offset = last_byte;
7188 if (cur_offset >= alloc_end) {
7189 ret = 0;
7190 break;
7191 }
7192 }
7193 unlock_extent_cached(&BTRFS_I(inode)->io_tree, alloc_start, locked_end,
7194 &cached_state, GFP_NOFS);
7195
7196 btrfs_free_reserved_data_space(inode, alloc_end - alloc_start);
7197out:
7198 mutex_unlock(&inode->i_mutex);
7199 return ret;
7200}
7201
7202static int btrfs_set_page_dirty(struct page *page) 7260static int btrfs_set_page_dirty(struct page *page)
7203{ 7261{
7204 return __set_page_dirty_nobuffers(page); 7262 return __set_page_dirty_nobuffers(page);
7205} 7263}
7206 7264
7207static int btrfs_permission(struct inode *inode, int mask) 7265static int btrfs_permission(struct inode *inode, int mask, unsigned int flags)
7208{ 7266{
7267 struct btrfs_root *root = BTRFS_I(inode)->root;
7268
7269 if (btrfs_root_readonly(root) && (mask & MAY_WRITE))
7270 return -EROFS;
7209 if ((BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) && (mask & MAY_WRITE)) 7271 if ((BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) && (mask & MAY_WRITE))
7210 return -EACCES; 7272 return -EACCES;
7211 return generic_permission(inode, mask, btrfs_check_acl); 7273 return generic_permission(inode, mask, flags, btrfs_check_acl);
7212} 7274}
7213 7275
7214static const struct inode_operations btrfs_dir_inode_operations = { 7276static const struct inode_operations btrfs_dir_inode_operations = {
@@ -7301,7 +7363,6 @@ static const struct inode_operations btrfs_file_inode_operations = {
7301 .listxattr = btrfs_listxattr, 7363 .listxattr = btrfs_listxattr,
7302 .removexattr = btrfs_removexattr, 7364 .removexattr = btrfs_removexattr,
7303 .permission = btrfs_permission, 7365 .permission = btrfs_permission,
7304 .fallocate = btrfs_fallocate,
7305 .fiemap = btrfs_fiemap, 7366 .fiemap = btrfs_fiemap,
7306}; 7367};
7307static const struct inode_operations btrfs_special_inode_operations = { 7368static const struct inode_operations btrfs_special_inode_operations = {