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.c236
1 files changed, 82 insertions, 154 deletions
diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
index a3798a3aa0d2..bcc461a9695f 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,
@@ -640,6 +651,7 @@ retry:
640 em->block_start = ins.objectid; 651 em->block_start = ins.objectid;
641 em->block_len = ins.offset; 652 em->block_len = ins.offset;
642 em->bdev = root->fs_info->fs_devices->latest_bdev; 653 em->bdev = root->fs_info->fs_devices->latest_bdev;
654 em->compress_type = async_extent->compress_type;
643 set_bit(EXTENT_FLAG_PINNED, &em->flags); 655 set_bit(EXTENT_FLAG_PINNED, &em->flags);
644 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); 656 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
645 657
@@ -656,11 +668,13 @@ retry:
656 async_extent->ram_size - 1, 0); 668 async_extent->ram_size - 1, 0);
657 } 669 }
658 670
659 ret = btrfs_add_ordered_extent(inode, async_extent->start, 671 ret = btrfs_add_ordered_extent_compress(inode,
660 ins.objectid, 672 async_extent->start,
661 async_extent->ram_size, 673 ins.objectid,
662 ins.offset, 674 async_extent->ram_size,
663 BTRFS_ORDERED_COMPRESSED); 675 ins.offset,
676 BTRFS_ORDERED_COMPRESSED,
677 async_extent->compress_type);
664 BUG_ON(ret); 678 BUG_ON(ret);
665 679
666 /* 680 /*
@@ -758,7 +772,7 @@ static noinline int cow_file_range(struct inode *inode,
758 772
759 BUG_ON(root == root->fs_info->tree_root); 773 BUG_ON(root == root->fs_info->tree_root);
760 trans = btrfs_join_transaction(root, 1); 774 trans = btrfs_join_transaction(root, 1);
761 BUG_ON(!trans); 775 BUG_ON(IS_ERR(trans));
762 btrfs_set_trans_block_group(trans, inode); 776 btrfs_set_trans_block_group(trans, inode);
763 trans->block_rsv = &root->fs_info->delalloc_block_rsv; 777 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
764 778
@@ -1036,7 +1050,7 @@ static noinline int run_delalloc_nocow(struct inode *inode,
1036 } else { 1050 } else {
1037 trans = btrfs_join_transaction(root, 1); 1051 trans = btrfs_join_transaction(root, 1);
1038 } 1052 }
1039 BUG_ON(!trans); 1053 BUG_ON(IS_ERR(trans));
1040 1054
1041 cow_start = (u64)-1; 1055 cow_start = (u64)-1;
1042 cur_offset = start; 1056 cur_offset = start;
@@ -1544,6 +1558,7 @@ out:
1544out_page: 1558out_page:
1545 unlock_page(page); 1559 unlock_page(page);
1546 page_cache_release(page); 1560 page_cache_release(page);
1561 kfree(fixup);
1547} 1562}
1548 1563
1549/* 1564/*
@@ -1670,7 +1685,7 @@ static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
1670 struct btrfs_ordered_extent *ordered_extent = NULL; 1685 struct btrfs_ordered_extent *ordered_extent = NULL;
1671 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; 1686 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1672 struct extent_state *cached_state = NULL; 1687 struct extent_state *cached_state = NULL;
1673 int compressed = 0; 1688 int compress_type = 0;
1674 int ret; 1689 int ret;
1675 bool nolock = false; 1690 bool nolock = false;
1676 1691
@@ -1690,7 +1705,7 @@ static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
1690 trans = btrfs_join_transaction_nolock(root, 1); 1705 trans = btrfs_join_transaction_nolock(root, 1);
1691 else 1706 else
1692 trans = btrfs_join_transaction(root, 1); 1707 trans = btrfs_join_transaction(root, 1);
1693 BUG_ON(!trans); 1708 BUG_ON(IS_ERR(trans));
1694 btrfs_set_trans_block_group(trans, inode); 1709 btrfs_set_trans_block_group(trans, inode);
1695 trans->block_rsv = &root->fs_info->delalloc_block_rsv; 1710 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
1696 ret = btrfs_update_inode(trans, root, inode); 1711 ret = btrfs_update_inode(trans, root, inode);
@@ -1707,13 +1722,14 @@ static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
1707 trans = btrfs_join_transaction_nolock(root, 1); 1722 trans = btrfs_join_transaction_nolock(root, 1);
1708 else 1723 else
1709 trans = btrfs_join_transaction(root, 1); 1724 trans = btrfs_join_transaction(root, 1);
1725 BUG_ON(IS_ERR(trans));
1710 btrfs_set_trans_block_group(trans, inode); 1726 btrfs_set_trans_block_group(trans, inode);
1711 trans->block_rsv = &root->fs_info->delalloc_block_rsv; 1727 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
1712 1728
1713 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags)) 1729 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
1714 compressed = 1; 1730 compress_type = ordered_extent->compress_type;
1715 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) { 1731 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
1716 BUG_ON(compressed); 1732 BUG_ON(compress_type);
1717 ret = btrfs_mark_extent_written(trans, inode, 1733 ret = btrfs_mark_extent_written(trans, inode,
1718 ordered_extent->file_offset, 1734 ordered_extent->file_offset,
1719 ordered_extent->file_offset + 1735 ordered_extent->file_offset +
@@ -1727,7 +1743,7 @@ static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
1727 ordered_extent->disk_len, 1743 ordered_extent->disk_len,
1728 ordered_extent->len, 1744 ordered_extent->len,
1729 ordered_extent->len, 1745 ordered_extent->len,
1730 compressed, 0, 0, 1746 compress_type, 0, 0,
1731 BTRFS_FILE_EXTENT_REG); 1747 BTRFS_FILE_EXTENT_REG);
1732 unpin_extent_cache(&BTRFS_I(inode)->extent_tree, 1748 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
1733 ordered_extent->file_offset, 1749 ordered_extent->file_offset,
@@ -1829,6 +1845,8 @@ static int btrfs_io_failed_hook(struct bio *failed_bio,
1829 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { 1845 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
1830 logical = em->block_start; 1846 logical = em->block_start;
1831 failrec->bio_flags = EXTENT_BIO_COMPRESSED; 1847 failrec->bio_flags = EXTENT_BIO_COMPRESSED;
1848 extent_set_compress_type(&failrec->bio_flags,
1849 em->compress_type);
1832 } 1850 }
1833 failrec->logical = logical; 1851 failrec->logical = logical;
1834 free_extent_map(em); 1852 free_extent_map(em);
@@ -2339,6 +2357,7 @@ void btrfs_orphan_cleanup(struct btrfs_root *root)
2339 */ 2357 */
2340 if (is_bad_inode(inode)) { 2358 if (is_bad_inode(inode)) {
2341 trans = btrfs_start_transaction(root, 0); 2359 trans = btrfs_start_transaction(root, 0);
2360 BUG_ON(IS_ERR(trans));
2342 btrfs_orphan_del(trans, inode); 2361 btrfs_orphan_del(trans, inode);
2343 btrfs_end_transaction(trans, root); 2362 btrfs_end_transaction(trans, root);
2344 iput(inode); 2363 iput(inode);
@@ -2366,6 +2385,7 @@ void btrfs_orphan_cleanup(struct btrfs_root *root)
2366 2385
2367 if (root->orphan_block_rsv || root->orphan_item_inserted) { 2386 if (root->orphan_block_rsv || root->orphan_item_inserted) {
2368 trans = btrfs_join_transaction(root, 1); 2387 trans = btrfs_join_transaction(root, 1);
2388 BUG_ON(IS_ERR(trans));
2369 btrfs_end_transaction(trans, root); 2389 btrfs_end_transaction(trans, root);
2370 } 2390 }
2371 2391
@@ -2626,7 +2646,7 @@ int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2626 path = btrfs_alloc_path(); 2646 path = btrfs_alloc_path();
2627 if (!path) { 2647 if (!path) {
2628 ret = -ENOMEM; 2648 ret = -ENOMEM;
2629 goto err; 2649 goto out;
2630 } 2650 }
2631 2651
2632 path->leave_spinning = 1; 2652 path->leave_spinning = 1;
@@ -2699,9 +2719,10 @@ static int check_path_shared(struct btrfs_root *root,
2699 struct extent_buffer *eb; 2719 struct extent_buffer *eb;
2700 int level; 2720 int level;
2701 u64 refs = 1; 2721 u64 refs = 1;
2702 int uninitialized_var(ret);
2703 2722
2704 for (level = 0; level < BTRFS_MAX_LEVEL; level++) { 2723 for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
2724 int ret;
2725
2705 if (!path->nodes[level]) 2726 if (!path->nodes[level])
2706 break; 2727 break;
2707 eb = path->nodes[level]; 2728 eb = path->nodes[level];
@@ -2712,7 +2733,7 @@ static int check_path_shared(struct btrfs_root *root,
2712 if (refs > 1) 2733 if (refs > 1)
2713 return 1; 2734 return 1;
2714 } 2735 }
2715 return ret; /* XXX callers? */ 2736 return 0;
2716} 2737}
2717 2738
2718/* 2739/*
@@ -3671,8 +3692,12 @@ static int btrfs_setattr_size(struct inode *inode, struct iattr *attr)
3671static int btrfs_setattr(struct dentry *dentry, struct iattr *attr) 3692static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
3672{ 3693{
3673 struct inode *inode = dentry->d_inode; 3694 struct inode *inode = dentry->d_inode;
3695 struct btrfs_root *root = BTRFS_I(inode)->root;
3674 int err; 3696 int err;
3675 3697
3698 if (btrfs_root_readonly(root))
3699 return -EROFS;
3700
3676 err = inode_change_ok(inode, attr); 3701 err = inode_change_ok(inode, attr);
3677 if (err) 3702 if (err)
3678 return err; 3703 return err;
@@ -4115,7 +4140,7 @@ struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
4115 } 4140 }
4116 srcu_read_unlock(&root->fs_info->subvol_srcu, index); 4141 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
4117 4142
4118 if (root != sub_root) { 4143 if (!IS_ERR(inode) && root != sub_root) {
4119 down_read(&root->fs_info->cleanup_work_sem); 4144 down_read(&root->fs_info->cleanup_work_sem);
4120 if (!(inode->i_sb->s_flags & MS_RDONLY)) 4145 if (!(inode->i_sb->s_flags & MS_RDONLY))
4121 btrfs_orphan_cleanup(sub_root); 4146 btrfs_orphan_cleanup(sub_root);
@@ -4328,6 +4353,8 @@ int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
4328 trans = btrfs_join_transaction_nolock(root, 1); 4353 trans = btrfs_join_transaction_nolock(root, 1);
4329 else 4354 else
4330 trans = btrfs_join_transaction(root, 1); 4355 trans = btrfs_join_transaction(root, 1);
4356 if (IS_ERR(trans))
4357 return PTR_ERR(trans);
4331 btrfs_set_trans_block_group(trans, inode); 4358 btrfs_set_trans_block_group(trans, inode);
4332 if (nolock) 4359 if (nolock)
4333 ret = btrfs_end_transaction_nolock(trans, root); 4360 ret = btrfs_end_transaction_nolock(trans, root);
@@ -4353,6 +4380,7 @@ void btrfs_dirty_inode(struct inode *inode)
4353 return; 4380 return;
4354 4381
4355 trans = btrfs_join_transaction(root, 1); 4382 trans = btrfs_join_transaction(root, 1);
4383 BUG_ON(IS_ERR(trans));
4356 btrfs_set_trans_block_group(trans, inode); 4384 btrfs_set_trans_block_group(trans, inode);
4357 4385
4358 ret = btrfs_update_inode(trans, root, inode); 4386 ret = btrfs_update_inode(trans, root, inode);
@@ -4928,8 +4956,10 @@ static noinline int uncompress_inline(struct btrfs_path *path,
4928 size_t max_size; 4956 size_t max_size;
4929 unsigned long inline_size; 4957 unsigned long inline_size;
4930 unsigned long ptr; 4958 unsigned long ptr;
4959 int compress_type;
4931 4960
4932 WARN_ON(pg_offset != 0); 4961 WARN_ON(pg_offset != 0);
4962 compress_type = btrfs_file_extent_compression(leaf, item);
4933 max_size = btrfs_file_extent_ram_bytes(leaf, item); 4963 max_size = btrfs_file_extent_ram_bytes(leaf, item);
4934 inline_size = btrfs_file_extent_inline_item_len(leaf, 4964 inline_size = btrfs_file_extent_inline_item_len(leaf,
4935 btrfs_item_nr(leaf, path->slots[0])); 4965 btrfs_item_nr(leaf, path->slots[0]));
@@ -4939,8 +4969,8 @@ static noinline int uncompress_inline(struct btrfs_path *path,
4939 read_extent_buffer(leaf, tmp, ptr, inline_size); 4969 read_extent_buffer(leaf, tmp, ptr, inline_size);
4940 4970
4941 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size); 4971 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
4942 ret = btrfs_zlib_decompress(tmp, page, extent_offset, 4972 ret = btrfs_decompress(compress_type, tmp, page,
4943 inline_size, max_size); 4973 extent_offset, inline_size, max_size);
4944 if (ret) { 4974 if (ret) {
4945 char *kaddr = kmap_atomic(page, KM_USER0); 4975 char *kaddr = kmap_atomic(page, KM_USER0);
4946 unsigned long copy_size = min_t(u64, 4976 unsigned long copy_size = min_t(u64,
@@ -4982,7 +5012,7 @@ struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
4982 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; 5012 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
4983 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; 5013 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4984 struct btrfs_trans_handle *trans = NULL; 5014 struct btrfs_trans_handle *trans = NULL;
4985 int compressed; 5015 int compress_type;
4986 5016
4987again: 5017again:
4988 read_lock(&em_tree->lock); 5018 read_lock(&em_tree->lock);
@@ -5041,7 +5071,7 @@ again:
5041 5071
5042 found_type = btrfs_file_extent_type(leaf, item); 5072 found_type = btrfs_file_extent_type(leaf, item);
5043 extent_start = found_key.offset; 5073 extent_start = found_key.offset;
5044 compressed = btrfs_file_extent_compression(leaf, item); 5074 compress_type = btrfs_file_extent_compression(leaf, item);
5045 if (found_type == BTRFS_FILE_EXTENT_REG || 5075 if (found_type == BTRFS_FILE_EXTENT_REG ||
5046 found_type == BTRFS_FILE_EXTENT_PREALLOC) { 5076 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
5047 extent_end = extent_start + 5077 extent_end = extent_start +
@@ -5087,8 +5117,9 @@ again:
5087 em->block_start = EXTENT_MAP_HOLE; 5117 em->block_start = EXTENT_MAP_HOLE;
5088 goto insert; 5118 goto insert;
5089 } 5119 }
5090 if (compressed) { 5120 if (compress_type != BTRFS_COMPRESS_NONE) {
5091 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); 5121 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
5122 em->compress_type = compress_type;
5092 em->block_start = bytenr; 5123 em->block_start = bytenr;
5093 em->block_len = btrfs_file_extent_disk_num_bytes(leaf, 5124 em->block_len = btrfs_file_extent_disk_num_bytes(leaf,
5094 item); 5125 item);
@@ -5122,12 +5153,14 @@ again:
5122 em->len = (copy_size + root->sectorsize - 1) & 5153 em->len = (copy_size + root->sectorsize - 1) &
5123 ~((u64)root->sectorsize - 1); 5154 ~((u64)root->sectorsize - 1);
5124 em->orig_start = EXTENT_MAP_INLINE; 5155 em->orig_start = EXTENT_MAP_INLINE;
5125 if (compressed) 5156 if (compress_type) {
5126 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); 5157 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
5158 em->compress_type = compress_type;
5159 }
5127 ptr = btrfs_file_extent_inline_start(item) + extent_offset; 5160 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
5128 if (create == 0 && !PageUptodate(page)) { 5161 if (create == 0 && !PageUptodate(page)) {
5129 if (btrfs_file_extent_compression(leaf, item) == 5162 if (btrfs_file_extent_compression(leaf, item) !=
5130 BTRFS_COMPRESS_ZLIB) { 5163 BTRFS_COMPRESS_NONE) {
5131 ret = uncompress_inline(path, inode, page, 5164 ret = uncompress_inline(path, inode, page,
5132 pg_offset, 5165 pg_offset,
5133 extent_offset, item); 5166 extent_offset, item);
@@ -5152,6 +5185,8 @@ again:
5152 em = NULL; 5185 em = NULL;
5153 btrfs_release_path(root, path); 5186 btrfs_release_path(root, path);
5154 trans = btrfs_join_transaction(root, 1); 5187 trans = btrfs_join_transaction(root, 1);
5188 if (IS_ERR(trans))
5189 return ERR_CAST(trans);
5155 goto again; 5190 goto again;
5156 } 5191 }
5157 map = kmap(page); 5192 map = kmap(page);
@@ -5256,8 +5291,8 @@ static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
5256 btrfs_drop_extent_cache(inode, start, start + len - 1, 0); 5291 btrfs_drop_extent_cache(inode, start, start + len - 1, 0);
5257 5292
5258 trans = btrfs_join_transaction(root, 0); 5293 trans = btrfs_join_transaction(root, 0);
5259 if (!trans) 5294 if (IS_ERR(trans))
5260 return ERR_PTR(-ENOMEM); 5295 return ERR_CAST(trans);
5261 5296
5262 trans->block_rsv = &root->fs_info->delalloc_block_rsv; 5297 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
5263 5298
@@ -5481,7 +5516,7 @@ static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
5481 * while we look for nocow cross refs 5516 * while we look for nocow cross refs
5482 */ 5517 */
5483 trans = btrfs_join_transaction(root, 0); 5518 trans = btrfs_join_transaction(root, 0);
5484 if (!trans) 5519 if (IS_ERR(trans))
5485 goto must_cow; 5520 goto must_cow;
5486 5521
5487 if (can_nocow_odirect(trans, inode, start, len) == 1) { 5522 if (can_nocow_odirect(trans, inode, start, len) == 1) {
@@ -5616,7 +5651,7 @@ again:
5616 BUG_ON(!ordered); 5651 BUG_ON(!ordered);
5617 5652
5618 trans = btrfs_join_transaction(root, 1); 5653 trans = btrfs_join_transaction(root, 1);
5619 if (!trans) { 5654 if (IS_ERR(trans)) {
5620 err = -ENOMEM; 5655 err = -ENOMEM;
5621 goto out; 5656 goto out;
5622 } 5657 }
@@ -6477,7 +6512,7 @@ struct inode *btrfs_alloc_inode(struct super_block *sb)
6477 ei->ordered_data_close = 0; 6512 ei->ordered_data_close = 0;
6478 ei->orphan_meta_reserved = 0; 6513 ei->orphan_meta_reserved = 0;
6479 ei->dummy_inode = 0; 6514 ei->dummy_inode = 0;
6480 ei->force_compress = 0; 6515 ei->force_compress = BTRFS_COMPRESS_NONE;
6481 6516
6482 inode = &ei->vfs_inode; 6517 inode = &ei->vfs_inode;
6483 extent_map_tree_init(&ei->extent_tree, GFP_NOFS); 6518 extent_map_tree_init(&ei->extent_tree, GFP_NOFS);
@@ -7098,116 +7133,6 @@ int btrfs_prealloc_file_range_trans(struct inode *inode,
7098 min_size, actual_len, alloc_hint, trans); 7133 min_size, actual_len, alloc_hint, trans);
7099} 7134}
7100 7135
7101static long btrfs_fallocate(struct inode *inode, int mode,
7102 loff_t offset, loff_t len)
7103{
7104 struct extent_state *cached_state = NULL;
7105 u64 cur_offset;
7106 u64 last_byte;
7107 u64 alloc_start;
7108 u64 alloc_end;
7109 u64 alloc_hint = 0;
7110 u64 locked_end;
7111 u64 mask = BTRFS_I(inode)->root->sectorsize - 1;
7112 struct extent_map *em;
7113 int ret;
7114
7115 alloc_start = offset & ~mask;
7116 alloc_end = (offset + len + mask) & ~mask;
7117
7118 /* We only support the FALLOC_FL_KEEP_SIZE mode */
7119 if (mode && (mode != FALLOC_FL_KEEP_SIZE))
7120 return -EOPNOTSUPP;
7121
7122 /*
7123 * wait for ordered IO before we have any locks. We'll loop again
7124 * below with the locks held.
7125 */
7126 btrfs_wait_ordered_range(inode, alloc_start, alloc_end - alloc_start);
7127
7128 mutex_lock(&inode->i_mutex);
7129 ret = inode_newsize_ok(inode, alloc_end);
7130 if (ret)
7131 goto out;
7132
7133 if (alloc_start > inode->i_size) {
7134 ret = btrfs_cont_expand(inode, alloc_start);
7135 if (ret)
7136 goto out;
7137 }
7138
7139 ret = btrfs_check_data_free_space(inode, alloc_end - alloc_start);
7140 if (ret)
7141 goto out;
7142
7143 locked_end = alloc_end - 1;
7144 while (1) {
7145 struct btrfs_ordered_extent *ordered;
7146
7147 /* the extent lock is ordered inside the running
7148 * transaction
7149 */
7150 lock_extent_bits(&BTRFS_I(inode)->io_tree, alloc_start,
7151 locked_end, 0, &cached_state, GFP_NOFS);
7152 ordered = btrfs_lookup_first_ordered_extent(inode,
7153 alloc_end - 1);
7154 if (ordered &&
7155 ordered->file_offset + ordered->len > alloc_start &&
7156 ordered->file_offset < alloc_end) {
7157 btrfs_put_ordered_extent(ordered);
7158 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
7159 alloc_start, locked_end,
7160 &cached_state, GFP_NOFS);
7161 /*
7162 * we can't wait on the range with the transaction
7163 * running or with the extent lock held
7164 */
7165 btrfs_wait_ordered_range(inode, alloc_start,
7166 alloc_end - alloc_start);
7167 } else {
7168 if (ordered)
7169 btrfs_put_ordered_extent(ordered);
7170 break;
7171 }
7172 }
7173
7174 cur_offset = alloc_start;
7175 while (1) {
7176 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
7177 alloc_end - cur_offset, 0);
7178 BUG_ON(IS_ERR(em) || !em);
7179 last_byte = min(extent_map_end(em), alloc_end);
7180 last_byte = (last_byte + mask) & ~mask;
7181 if (em->block_start == EXTENT_MAP_HOLE ||
7182 (cur_offset >= inode->i_size &&
7183 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
7184 ret = btrfs_prealloc_file_range(inode, mode, cur_offset,
7185 last_byte - cur_offset,
7186 1 << inode->i_blkbits,
7187 offset + len,
7188 &alloc_hint);
7189 if (ret < 0) {
7190 free_extent_map(em);
7191 break;
7192 }
7193 }
7194 free_extent_map(em);
7195
7196 cur_offset = last_byte;
7197 if (cur_offset >= alloc_end) {
7198 ret = 0;
7199 break;
7200 }
7201 }
7202 unlock_extent_cached(&BTRFS_I(inode)->io_tree, alloc_start, locked_end,
7203 &cached_state, GFP_NOFS);
7204
7205 btrfs_free_reserved_data_space(inode, alloc_end - alloc_start);
7206out:
7207 mutex_unlock(&inode->i_mutex);
7208 return ret;
7209}
7210
7211static int btrfs_set_page_dirty(struct page *page) 7136static int btrfs_set_page_dirty(struct page *page)
7212{ 7137{
7213 return __set_page_dirty_nobuffers(page); 7138 return __set_page_dirty_nobuffers(page);
@@ -7215,6 +7140,10 @@ static int btrfs_set_page_dirty(struct page *page)
7215 7140
7216static int btrfs_permission(struct inode *inode, int mask, unsigned int flags) 7141static int btrfs_permission(struct inode *inode, int mask, unsigned int flags)
7217{ 7142{
7143 struct btrfs_root *root = BTRFS_I(inode)->root;
7144
7145 if (btrfs_root_readonly(root) && (mask & MAY_WRITE))
7146 return -EROFS;
7218 if ((BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) && (mask & MAY_WRITE)) 7147 if ((BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) && (mask & MAY_WRITE))
7219 return -EACCES; 7148 return -EACCES;
7220 return generic_permission(inode, mask, flags, btrfs_check_acl); 7149 return generic_permission(inode, mask, flags, btrfs_check_acl);
@@ -7310,7 +7239,6 @@ static const struct inode_operations btrfs_file_inode_operations = {
7310 .listxattr = btrfs_listxattr, 7239 .listxattr = btrfs_listxattr,
7311 .removexattr = btrfs_removexattr, 7240 .removexattr = btrfs_removexattr,
7312 .permission = btrfs_permission, 7241 .permission = btrfs_permission,
7313 .fallocate = btrfs_fallocate,
7314 .fiemap = btrfs_fiemap, 7242 .fiemap = btrfs_fiemap,
7315}; 7243};
7316static const struct inode_operations btrfs_special_inode_operations = { 7244static const struct inode_operations btrfs_special_inode_operations = {