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.c137
1 files changed, 102 insertions, 35 deletions
diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
index 6f4e41dca970..492ee0ee8c64 100644
--- a/fs/btrfs/inode.c
+++ b/fs/btrfs/inode.c
@@ -89,7 +89,7 @@ static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
89 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK, 89 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
90}; 90};
91 91
92static int btrfs_setsize(struct inode *inode, loff_t newsize); 92static int btrfs_setsize(struct inode *inode, struct iattr *attr);
93static int btrfs_truncate(struct inode *inode); 93static int btrfs_truncate(struct inode *inode);
94static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent); 94static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
95static noinline int cow_file_range(struct inode *inode, 95static noinline int cow_file_range(struct inode *inode,
@@ -2479,6 +2479,18 @@ int btrfs_orphan_cleanup(struct btrfs_root *root)
2479 continue; 2479 continue;
2480 } 2480 }
2481 nr_truncate++; 2481 nr_truncate++;
2482
2483 /* 1 for the orphan item deletion. */
2484 trans = btrfs_start_transaction(root, 1);
2485 if (IS_ERR(trans)) {
2486 ret = PTR_ERR(trans);
2487 goto out;
2488 }
2489 ret = btrfs_orphan_add(trans, inode);
2490 btrfs_end_transaction(trans, root);
2491 if (ret)
2492 goto out;
2493
2482 ret = btrfs_truncate(inode); 2494 ret = btrfs_truncate(inode);
2483 } else { 2495 } else {
2484 nr_unlink++; 2496 nr_unlink++;
@@ -3666,6 +3678,7 @@ int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
3666 block_end - cur_offset, 0); 3678 block_end - cur_offset, 0);
3667 if (IS_ERR(em)) { 3679 if (IS_ERR(em)) {
3668 err = PTR_ERR(em); 3680 err = PTR_ERR(em);
3681 em = NULL;
3669 break; 3682 break;
3670 } 3683 }
3671 last_byte = min(extent_map_end(em), block_end); 3684 last_byte = min(extent_map_end(em), block_end);
@@ -3749,16 +3762,27 @@ next:
3749 return err; 3762 return err;
3750} 3763}
3751 3764
3752static int btrfs_setsize(struct inode *inode, loff_t newsize) 3765static int btrfs_setsize(struct inode *inode, struct iattr *attr)
3753{ 3766{
3754 struct btrfs_root *root = BTRFS_I(inode)->root; 3767 struct btrfs_root *root = BTRFS_I(inode)->root;
3755 struct btrfs_trans_handle *trans; 3768 struct btrfs_trans_handle *trans;
3756 loff_t oldsize = i_size_read(inode); 3769 loff_t oldsize = i_size_read(inode);
3770 loff_t newsize = attr->ia_size;
3771 int mask = attr->ia_valid;
3757 int ret; 3772 int ret;
3758 3773
3759 if (newsize == oldsize) 3774 if (newsize == oldsize)
3760 return 0; 3775 return 0;
3761 3776
3777 /*
3778 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
3779 * special case where we need to update the times despite not having
3780 * these flags set. For all other operations the VFS set these flags
3781 * explicitly if it wants a timestamp update.
3782 */
3783 if (newsize != oldsize && (!(mask & (ATTR_CTIME | ATTR_MTIME))))
3784 inode->i_ctime = inode->i_mtime = current_fs_time(inode->i_sb);
3785
3762 if (newsize > oldsize) { 3786 if (newsize > oldsize) {
3763 truncate_pagecache(inode, oldsize, newsize); 3787 truncate_pagecache(inode, oldsize, newsize);
3764 ret = btrfs_cont_expand(inode, oldsize, newsize); 3788 ret = btrfs_cont_expand(inode, oldsize, newsize);
@@ -3784,9 +3808,34 @@ static int btrfs_setsize(struct inode *inode, loff_t newsize)
3784 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE, 3808 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
3785 &BTRFS_I(inode)->runtime_flags); 3809 &BTRFS_I(inode)->runtime_flags);
3786 3810
3811 /*
3812 * 1 for the orphan item we're going to add
3813 * 1 for the orphan item deletion.
3814 */
3815 trans = btrfs_start_transaction(root, 2);
3816 if (IS_ERR(trans))
3817 return PTR_ERR(trans);
3818
3819 /*
3820 * We need to do this in case we fail at _any_ point during the
3821 * actual truncate. Once we do the truncate_setsize we could
3822 * invalidate pages which forces any outstanding ordered io to
3823 * be instantly completed which will give us extents that need
3824 * to be truncated. If we fail to get an orphan inode down we
3825 * could have left over extents that were never meant to live,
3826 * so we need to garuntee from this point on that everything
3827 * will be consistent.
3828 */
3829 ret = btrfs_orphan_add(trans, inode);
3830 btrfs_end_transaction(trans, root);
3831 if (ret)
3832 return ret;
3833
3787 /* we don't support swapfiles, so vmtruncate shouldn't fail */ 3834 /* we don't support swapfiles, so vmtruncate shouldn't fail */
3788 truncate_setsize(inode, newsize); 3835 truncate_setsize(inode, newsize);
3789 ret = btrfs_truncate(inode); 3836 ret = btrfs_truncate(inode);
3837 if (ret && inode->i_nlink)
3838 btrfs_orphan_del(NULL, inode);
3790 } 3839 }
3791 3840
3792 return ret; 3841 return ret;
@@ -3806,7 +3855,7 @@ static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
3806 return err; 3855 return err;
3807 3856
3808 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) { 3857 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
3809 err = btrfs_setsize(inode, attr->ia_size); 3858 err = btrfs_setsize(inode, attr);
3810 if (err) 3859 if (err)
3811 return err; 3860 return err;
3812 } 3861 }
@@ -5587,10 +5636,13 @@ struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *pag
5587 return em; 5636 return em;
5588 if (em) { 5637 if (em) {
5589 /* 5638 /*
5590 * if our em maps to a hole, there might 5639 * if our em maps to
5591 * actually be delalloc bytes behind it 5640 * - a hole or
5641 * - a pre-alloc extent,
5642 * there might actually be delalloc bytes behind it.
5592 */ 5643 */
5593 if (em->block_start != EXTENT_MAP_HOLE) 5644 if (em->block_start != EXTENT_MAP_HOLE &&
5645 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
5594 return em; 5646 return em;
5595 else 5647 else
5596 hole_em = em; 5648 hole_em = em;
@@ -5672,6 +5724,8 @@ struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *pag
5672 */ 5724 */
5673 em->block_start = hole_em->block_start; 5725 em->block_start = hole_em->block_start;
5674 em->block_len = hole_len; 5726 em->block_len = hole_len;
5727 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
5728 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
5675 } else { 5729 } else {
5676 em->start = range_start; 5730 em->start = range_start;
5677 em->len = found; 5731 em->len = found;
@@ -6937,11 +6991,9 @@ static int btrfs_truncate(struct inode *inode)
6937 6991
6938 /* 6992 /*
6939 * 1 for the truncate slack space 6993 * 1 for the truncate slack space
6940 * 1 for the orphan item we're going to add
6941 * 1 for the orphan item deletion
6942 * 1 for updating the inode. 6994 * 1 for updating the inode.
6943 */ 6995 */
6944 trans = btrfs_start_transaction(root, 4); 6996 trans = btrfs_start_transaction(root, 2);
6945 if (IS_ERR(trans)) { 6997 if (IS_ERR(trans)) {
6946 err = PTR_ERR(trans); 6998 err = PTR_ERR(trans);
6947 goto out; 6999 goto out;
@@ -6952,12 +7004,6 @@ static int btrfs_truncate(struct inode *inode)
6952 min_size); 7004 min_size);
6953 BUG_ON(ret); 7005 BUG_ON(ret);
6954 7006
6955 ret = btrfs_orphan_add(trans, inode);
6956 if (ret) {
6957 btrfs_end_transaction(trans, root);
6958 goto out;
6959 }
6960
6961 /* 7007 /*
6962 * setattr is responsible for setting the ordered_data_close flag, 7008 * setattr is responsible for setting the ordered_data_close flag,
6963 * but that is only tested during the last file release. That 7009 * but that is only tested during the last file release. That
@@ -7026,12 +7072,6 @@ static int btrfs_truncate(struct inode *inode)
7026 ret = btrfs_orphan_del(trans, inode); 7072 ret = btrfs_orphan_del(trans, inode);
7027 if (ret) 7073 if (ret)
7028 err = ret; 7074 err = ret;
7029 } else if (ret && inode->i_nlink > 0) {
7030 /*
7031 * Failed to do the truncate, remove us from the in memory
7032 * orphan list.
7033 */
7034 ret = btrfs_orphan_del(NULL, inode);
7035 } 7075 }
7036 7076
7037 if (trans) { 7077 if (trans) {
@@ -7553,41 +7593,61 @@ void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
7553 */ 7593 */
7554int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput) 7594int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
7555{ 7595{
7556 struct list_head *head = &root->fs_info->delalloc_inodes;
7557 struct btrfs_inode *binode; 7596 struct btrfs_inode *binode;
7558 struct inode *inode; 7597 struct inode *inode;
7559 struct btrfs_delalloc_work *work, *next; 7598 struct btrfs_delalloc_work *work, *next;
7560 struct list_head works; 7599 struct list_head works;
7600 struct list_head splice;
7561 int ret = 0; 7601 int ret = 0;
7562 7602
7563 if (root->fs_info->sb->s_flags & MS_RDONLY) 7603 if (root->fs_info->sb->s_flags & MS_RDONLY)
7564 return -EROFS; 7604 return -EROFS;
7565 7605
7566 INIT_LIST_HEAD(&works); 7606 INIT_LIST_HEAD(&works);
7567 7607 INIT_LIST_HEAD(&splice);
7608again:
7568 spin_lock(&root->fs_info->delalloc_lock); 7609 spin_lock(&root->fs_info->delalloc_lock);
7569 while (!list_empty(head)) { 7610 list_splice_init(&root->fs_info->delalloc_inodes, &splice);
7570 binode = list_entry(head->next, struct btrfs_inode, 7611 while (!list_empty(&splice)) {
7612 binode = list_entry(splice.next, struct btrfs_inode,
7571 delalloc_inodes); 7613 delalloc_inodes);
7614
7615 list_del_init(&binode->delalloc_inodes);
7616
7572 inode = igrab(&binode->vfs_inode); 7617 inode = igrab(&binode->vfs_inode);
7573 if (!inode) 7618 if (!inode)
7574 list_del_init(&binode->delalloc_inodes); 7619 continue;
7620
7621 list_add_tail(&binode->delalloc_inodes,
7622 &root->fs_info->delalloc_inodes);
7575 spin_unlock(&root->fs_info->delalloc_lock); 7623 spin_unlock(&root->fs_info->delalloc_lock);
7576 if (inode) { 7624
7577 work = btrfs_alloc_delalloc_work(inode, 0, delay_iput); 7625 work = btrfs_alloc_delalloc_work(inode, 0, delay_iput);
7578 if (!work) { 7626 if (unlikely(!work)) {
7579 ret = -ENOMEM; 7627 ret = -ENOMEM;
7580 goto out; 7628 goto out;
7581 }
7582 list_add_tail(&work->list, &works);
7583 btrfs_queue_worker(&root->fs_info->flush_workers,
7584 &work->work);
7585 } 7629 }
7630 list_add_tail(&work->list, &works);
7631 btrfs_queue_worker(&root->fs_info->flush_workers,
7632 &work->work);
7633
7586 cond_resched(); 7634 cond_resched();
7587 spin_lock(&root->fs_info->delalloc_lock); 7635 spin_lock(&root->fs_info->delalloc_lock);
7588 } 7636 }
7589 spin_unlock(&root->fs_info->delalloc_lock); 7637 spin_unlock(&root->fs_info->delalloc_lock);
7590 7638
7639 list_for_each_entry_safe(work, next, &works, list) {
7640 list_del_init(&work->list);
7641 btrfs_wait_and_free_delalloc_work(work);
7642 }
7643
7644 spin_lock(&root->fs_info->delalloc_lock);
7645 if (!list_empty(&root->fs_info->delalloc_inodes)) {
7646 spin_unlock(&root->fs_info->delalloc_lock);
7647 goto again;
7648 }
7649 spin_unlock(&root->fs_info->delalloc_lock);
7650
7591 /* the filemap_flush will queue IO into the worker threads, but 7651 /* the filemap_flush will queue IO into the worker threads, but
7592 * we have to make sure the IO is actually started and that 7652 * we have to make sure the IO is actually started and that
7593 * ordered extents get created before we return 7653 * ordered extents get created before we return
@@ -7600,11 +7660,18 @@ int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
7600 atomic_read(&root->fs_info->async_delalloc_pages) == 0)); 7660 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
7601 } 7661 }
7602 atomic_dec(&root->fs_info->async_submit_draining); 7662 atomic_dec(&root->fs_info->async_submit_draining);
7663 return 0;
7603out: 7664out:
7604 list_for_each_entry_safe(work, next, &works, list) { 7665 list_for_each_entry_safe(work, next, &works, list) {
7605 list_del_init(&work->list); 7666 list_del_init(&work->list);
7606 btrfs_wait_and_free_delalloc_work(work); 7667 btrfs_wait_and_free_delalloc_work(work);
7607 } 7668 }
7669
7670 if (!list_empty_careful(&splice)) {
7671 spin_lock(&root->fs_info->delalloc_lock);
7672 list_splice_tail(&splice, &root->fs_info->delalloc_inodes);
7673 spin_unlock(&root->fs_info->delalloc_lock);
7674 }
7608 return ret; 7675 return ret;
7609} 7676}
7610 7677