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.c300
1 files changed, 209 insertions, 91 deletions
diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
index 03708ef3deef..016c403bfe7e 100644
--- a/fs/btrfs/inode.c
+++ b/fs/btrfs/inode.c
@@ -778,8 +778,12 @@ retry:
778 ins.offset, 778 ins.offset,
779 BTRFS_ORDERED_COMPRESSED, 779 BTRFS_ORDERED_COMPRESSED,
780 async_extent->compress_type); 780 async_extent->compress_type);
781 if (ret) 781 if (ret) {
782 btrfs_drop_extent_cache(inode, async_extent->start,
783 async_extent->start +
784 async_extent->ram_size - 1, 0);
782 goto out_free_reserve; 785 goto out_free_reserve;
786 }
783 787
784 /* 788 /*
785 * clear dirty, set writeback and unlock the pages. 789 * clear dirty, set writeback and unlock the pages.
@@ -971,14 +975,14 @@ static noinline int cow_file_range(struct inode *inode,
971 ret = btrfs_add_ordered_extent(inode, start, ins.objectid, 975 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
972 ram_size, cur_alloc_size, 0); 976 ram_size, cur_alloc_size, 0);
973 if (ret) 977 if (ret)
974 goto out_reserve; 978 goto out_drop_extent_cache;
975 979
976 if (root->root_key.objectid == 980 if (root->root_key.objectid ==
977 BTRFS_DATA_RELOC_TREE_OBJECTID) { 981 BTRFS_DATA_RELOC_TREE_OBJECTID) {
978 ret = btrfs_reloc_clone_csums(inode, start, 982 ret = btrfs_reloc_clone_csums(inode, start,
979 cur_alloc_size); 983 cur_alloc_size);
980 if (ret) 984 if (ret)
981 goto out_reserve; 985 goto out_drop_extent_cache;
982 } 986 }
983 987
984 if (disk_num_bytes < cur_alloc_size) 988 if (disk_num_bytes < cur_alloc_size)
@@ -1006,6 +1010,8 @@ static noinline int cow_file_range(struct inode *inode,
1006out: 1010out:
1007 return ret; 1011 return ret;
1008 1012
1013out_drop_extent_cache:
1014 btrfs_drop_extent_cache(inode, start, start + ram_size - 1, 0);
1009out_reserve: 1015out_reserve:
1010 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1); 1016 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
1011out_unlock: 1017out_unlock:
@@ -1096,8 +1102,10 @@ static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1096 async_cow->end = cur_end; 1102 async_cow->end = cur_end;
1097 INIT_LIST_HEAD(&async_cow->extents); 1103 INIT_LIST_HEAD(&async_cow->extents);
1098 1104
1099 btrfs_init_work(&async_cow->work, async_cow_start, 1105 btrfs_init_work(&async_cow->work,
1100 async_cow_submit, async_cow_free); 1106 btrfs_delalloc_helper,
1107 async_cow_start, async_cow_submit,
1108 async_cow_free);
1101 1109
1102 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >> 1110 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
1103 PAGE_CACHE_SHIFT; 1111 PAGE_CACHE_SHIFT;
@@ -1881,7 +1889,8 @@ static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
1881 1889
1882 SetPageChecked(page); 1890 SetPageChecked(page);
1883 page_cache_get(page); 1891 page_cache_get(page);
1884 btrfs_init_work(&fixup->work, btrfs_writepage_fixup_worker, NULL, NULL); 1892 btrfs_init_work(&fixup->work, btrfs_fixup_helper,
1893 btrfs_writepage_fixup_worker, NULL, NULL);
1885 fixup->page = page; 1894 fixup->page = page;
1886 btrfs_queue_work(root->fs_info->fixup_workers, &fixup->work); 1895 btrfs_queue_work(root->fs_info->fixup_workers, &fixup->work);
1887 return -EBUSY; 1896 return -EBUSY;
@@ -2822,7 +2831,8 @@ static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
2822 struct inode *inode = page->mapping->host; 2831 struct inode *inode = page->mapping->host;
2823 struct btrfs_root *root = BTRFS_I(inode)->root; 2832 struct btrfs_root *root = BTRFS_I(inode)->root;
2824 struct btrfs_ordered_extent *ordered_extent = NULL; 2833 struct btrfs_ordered_extent *ordered_extent = NULL;
2825 struct btrfs_workqueue *workers; 2834 struct btrfs_workqueue *wq;
2835 btrfs_work_func_t func;
2826 2836
2827 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate); 2837 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
2828 2838
@@ -2831,13 +2841,17 @@ static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
2831 end - start + 1, uptodate)) 2841 end - start + 1, uptodate))
2832 return 0; 2842 return 0;
2833 2843
2834 btrfs_init_work(&ordered_extent->work, finish_ordered_fn, NULL, NULL); 2844 if (btrfs_is_free_space_inode(inode)) {
2845 wq = root->fs_info->endio_freespace_worker;
2846 func = btrfs_freespace_write_helper;
2847 } else {
2848 wq = root->fs_info->endio_write_workers;
2849 func = btrfs_endio_write_helper;
2850 }
2835 2851
2836 if (btrfs_is_free_space_inode(inode)) 2852 btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL,
2837 workers = root->fs_info->endio_freespace_worker; 2853 NULL);
2838 else 2854 btrfs_queue_work(wq, &ordered_extent->work);
2839 workers = root->fs_info->endio_write_workers;
2840 btrfs_queue_work(workers, &ordered_extent->work);
2841 2855
2842 return 0; 2856 return 0;
2843} 2857}
@@ -4234,7 +4248,8 @@ out:
4234 btrfs_abort_transaction(trans, root, ret); 4248 btrfs_abort_transaction(trans, root, ret);
4235 } 4249 }
4236error: 4250error:
4237 if (last_size != (u64)-1) 4251 if (last_size != (u64)-1 &&
4252 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
4238 btrfs_ordered_update_i_size(inode, last_size, NULL); 4253 btrfs_ordered_update_i_size(inode, last_size, NULL);
4239 btrfs_free_path(path); 4254 btrfs_free_path(path);
4240 return err; 4255 return err;
@@ -4674,6 +4689,11 @@ static void evict_inode_truncate_pages(struct inode *inode)
4674 clear_bit(EXTENT_FLAG_LOGGING, &em->flags); 4689 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
4675 remove_extent_mapping(map_tree, em); 4690 remove_extent_mapping(map_tree, em);
4676 free_extent_map(em); 4691 free_extent_map(em);
4692 if (need_resched()) {
4693 write_unlock(&map_tree->lock);
4694 cond_resched();
4695 write_lock(&map_tree->lock);
4696 }
4677 } 4697 }
4678 write_unlock(&map_tree->lock); 4698 write_unlock(&map_tree->lock);
4679 4699
@@ -4696,6 +4716,7 @@ static void evict_inode_truncate_pages(struct inode *inode)
4696 &cached_state, GFP_NOFS); 4716 &cached_state, GFP_NOFS);
4697 free_extent_state(state); 4717 free_extent_state(state);
4698 4718
4719 cond_resched();
4699 spin_lock(&io_tree->lock); 4720 spin_lock(&io_tree->lock);
4700 } 4721 }
4701 spin_unlock(&io_tree->lock); 4722 spin_unlock(&io_tree->lock);
@@ -5181,6 +5202,42 @@ struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
5181 iput(inode); 5202 iput(inode);
5182 inode = ERR_PTR(ret); 5203 inode = ERR_PTR(ret);
5183 } 5204 }
5205 /*
5206 * If orphan cleanup did remove any orphans, it means the tree
5207 * was modified and therefore the commit root is not the same as
5208 * the current root anymore. This is a problem, because send
5209 * uses the commit root and therefore can see inode items that
5210 * don't exist in the current root anymore, and for example make
5211 * calls to btrfs_iget, which will do tree lookups based on the
5212 * current root and not on the commit root. Those lookups will
5213 * fail, returning a -ESTALE error, and making send fail with
5214 * that error. So make sure a send does not see any orphans we
5215 * have just removed, and that it will see the same inodes
5216 * regardless of whether a transaction commit happened before
5217 * it started (meaning that the commit root will be the same as
5218 * the current root) or not.
5219 */
5220 if (sub_root->node != sub_root->commit_root) {
5221 u64 sub_flags = btrfs_root_flags(&sub_root->root_item);
5222
5223 if (sub_flags & BTRFS_ROOT_SUBVOL_RDONLY) {
5224 struct extent_buffer *eb;
5225
5226 /*
5227 * Assert we can't have races between dentry
5228 * lookup called through the snapshot creation
5229 * ioctl and the VFS.
5230 */
5231 ASSERT(mutex_is_locked(&dir->i_mutex));
5232
5233 down_write(&root->fs_info->commit_root_sem);
5234 eb = sub_root->commit_root;
5235 sub_root->commit_root =
5236 btrfs_root_node(sub_root);
5237 up_write(&root->fs_info->commit_root_sem);
5238 free_extent_buffer(eb);
5239 }
5240 }
5184 } 5241 }
5185 5242
5186 return inode; 5243 return inode;
@@ -5577,6 +5634,17 @@ int btrfs_set_inode_index(struct inode *dir, u64 *index)
5577 return ret; 5634 return ret;
5578} 5635}
5579 5636
5637static int btrfs_insert_inode_locked(struct inode *inode)
5638{
5639 struct btrfs_iget_args args;
5640 args.location = &BTRFS_I(inode)->location;
5641 args.root = BTRFS_I(inode)->root;
5642
5643 return insert_inode_locked4(inode,
5644 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
5645 btrfs_find_actor, &args);
5646}
5647
5580static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans, 5648static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
5581 struct btrfs_root *root, 5649 struct btrfs_root *root,
5582 struct inode *dir, 5650 struct inode *dir,
@@ -5606,6 +5674,13 @@ static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
5606 } 5674 }
5607 5675
5608 /* 5676 /*
5677 * O_TMPFILE, set link count to 0, so that after this point,
5678 * we fill in an inode item with the correct link count.
5679 */
5680 if (!name)
5681 set_nlink(inode, 0);
5682
5683 /*
5609 * we have to initialize this early, so we can reclaim the inode 5684 * we have to initialize this early, so we can reclaim the inode
5610 * number if we fail afterwards in this function. 5685 * number if we fail afterwards in this function.
5611 */ 5686 */
@@ -5662,10 +5737,19 @@ static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
5662 sizes[1] = name_len + sizeof(*ref); 5737 sizes[1] = name_len + sizeof(*ref);
5663 } 5738 }
5664 5739
5740 location = &BTRFS_I(inode)->location;
5741 location->objectid = objectid;
5742 location->offset = 0;
5743 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
5744
5745 ret = btrfs_insert_inode_locked(inode);
5746 if (ret < 0)
5747 goto fail;
5748
5665 path->leave_spinning = 1; 5749 path->leave_spinning = 1;
5666 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems); 5750 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
5667 if (ret != 0) 5751 if (ret != 0)
5668 goto fail; 5752 goto fail_unlock;
5669 5753
5670 inode_init_owner(inode, dir, mode); 5754 inode_init_owner(inode, dir, mode);
5671 inode_set_bytes(inode, 0); 5755 inode_set_bytes(inode, 0);
@@ -5688,11 +5772,6 @@ static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
5688 btrfs_mark_buffer_dirty(path->nodes[0]); 5772 btrfs_mark_buffer_dirty(path->nodes[0]);
5689 btrfs_free_path(path); 5773 btrfs_free_path(path);
5690 5774
5691 location = &BTRFS_I(inode)->location;
5692 location->objectid = objectid;
5693 location->offset = 0;
5694 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
5695
5696 btrfs_inherit_iflags(inode, dir); 5775 btrfs_inherit_iflags(inode, dir);
5697 5776
5698 if (S_ISREG(mode)) { 5777 if (S_ISREG(mode)) {
@@ -5703,7 +5782,6 @@ static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
5703 BTRFS_INODE_NODATASUM; 5782 BTRFS_INODE_NODATASUM;
5704 } 5783 }
5705 5784
5706 btrfs_insert_inode_hash(inode);
5707 inode_tree_add(inode); 5785 inode_tree_add(inode);
5708 5786
5709 trace_btrfs_inode_new(inode); 5787 trace_btrfs_inode_new(inode);
@@ -5718,6 +5796,9 @@ static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
5718 btrfs_ino(inode), root->root_key.objectid, ret); 5796 btrfs_ino(inode), root->root_key.objectid, ret);
5719 5797
5720 return inode; 5798 return inode;
5799
5800fail_unlock:
5801 unlock_new_inode(inode);
5721fail: 5802fail:
5722 if (dir && name) 5803 if (dir && name)
5723 BTRFS_I(dir)->index_cnt--; 5804 BTRFS_I(dir)->index_cnt--;
@@ -5852,28 +5933,28 @@ static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
5852 goto out_unlock; 5933 goto out_unlock;
5853 } 5934 }
5854 5935
5855 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
5856 if (err) {
5857 drop_inode = 1;
5858 goto out_unlock;
5859 }
5860
5861 /* 5936 /*
5862 * If the active LSM wants to access the inode during 5937 * If the active LSM wants to access the inode during
5863 * d_instantiate it needs these. Smack checks to see 5938 * d_instantiate it needs these. Smack checks to see
5864 * if the filesystem supports xattrs by looking at the 5939 * if the filesystem supports xattrs by looking at the
5865 * ops vector. 5940 * ops vector.
5866 */ 5941 */
5867
5868 inode->i_op = &btrfs_special_inode_operations; 5942 inode->i_op = &btrfs_special_inode_operations;
5869 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); 5943 init_special_inode(inode, inode->i_mode, rdev);
5944
5945 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
5870 if (err) 5946 if (err)
5871 drop_inode = 1; 5947 goto out_unlock_inode;
5872 else { 5948
5873 init_special_inode(inode, inode->i_mode, rdev); 5949 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
5950 if (err) {
5951 goto out_unlock_inode;
5952 } else {
5874 btrfs_update_inode(trans, root, inode); 5953 btrfs_update_inode(trans, root, inode);
5954 unlock_new_inode(inode);
5875 d_instantiate(dentry, inode); 5955 d_instantiate(dentry, inode);
5876 } 5956 }
5957
5877out_unlock: 5958out_unlock:
5878 btrfs_end_transaction(trans, root); 5959 btrfs_end_transaction(trans, root);
5879 btrfs_balance_delayed_items(root); 5960 btrfs_balance_delayed_items(root);
@@ -5883,6 +5964,12 @@ out_unlock:
5883 iput(inode); 5964 iput(inode);
5884 } 5965 }
5885 return err; 5966 return err;
5967
5968out_unlock_inode:
5969 drop_inode = 1;
5970 unlock_new_inode(inode);
5971 goto out_unlock;
5972
5886} 5973}
5887 5974
5888static int btrfs_create(struct inode *dir, struct dentry *dentry, 5975static int btrfs_create(struct inode *dir, struct dentry *dentry,
@@ -5917,15 +6004,6 @@ static int btrfs_create(struct inode *dir, struct dentry *dentry,
5917 goto out_unlock; 6004 goto out_unlock;
5918 } 6005 }
5919 drop_inode_on_err = 1; 6006 drop_inode_on_err = 1;
5920
5921 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
5922 if (err)
5923 goto out_unlock;
5924
5925 err = btrfs_update_inode(trans, root, inode);
5926 if (err)
5927 goto out_unlock;
5928
5929 /* 6007 /*
5930 * If the active LSM wants to access the inode during 6008 * If the active LSM wants to access the inode during
5931 * d_instantiate it needs these. Smack checks to see 6009 * d_instantiate it needs these. Smack checks to see
@@ -5934,14 +6012,23 @@ static int btrfs_create(struct inode *dir, struct dentry *dentry,
5934 */ 6012 */
5935 inode->i_fop = &btrfs_file_operations; 6013 inode->i_fop = &btrfs_file_operations;
5936 inode->i_op = &btrfs_file_inode_operations; 6014 inode->i_op = &btrfs_file_inode_operations;
6015 inode->i_mapping->a_ops = &btrfs_aops;
6016 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
6017
6018 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6019 if (err)
6020 goto out_unlock_inode;
6021
6022 err = btrfs_update_inode(trans, root, inode);
6023 if (err)
6024 goto out_unlock_inode;
5937 6025
5938 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); 6026 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
5939 if (err) 6027 if (err)
5940 goto out_unlock; 6028 goto out_unlock_inode;
5941 6029
5942 inode->i_mapping->a_ops = &btrfs_aops;
5943 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
5944 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; 6030 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
6031 unlock_new_inode(inode);
5945 d_instantiate(dentry, inode); 6032 d_instantiate(dentry, inode);
5946 6033
5947out_unlock: 6034out_unlock:
@@ -5953,6 +6040,11 @@ out_unlock:
5953 btrfs_balance_delayed_items(root); 6040 btrfs_balance_delayed_items(root);
5954 btrfs_btree_balance_dirty(root); 6041 btrfs_btree_balance_dirty(root);
5955 return err; 6042 return err;
6043
6044out_unlock_inode:
6045 unlock_new_inode(inode);
6046 goto out_unlock;
6047
5956} 6048}
5957 6049
5958static int btrfs_link(struct dentry *old_dentry, struct inode *dir, 6050static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
@@ -6060,25 +6152,30 @@ static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
6060 } 6152 }
6061 6153
6062 drop_on_err = 1; 6154 drop_on_err = 1;
6155 /* these must be set before we unlock the inode */
6156 inode->i_op = &btrfs_dir_inode_operations;
6157 inode->i_fop = &btrfs_dir_file_operations;
6063 6158
6064 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); 6159 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6065 if (err) 6160 if (err)
6066 goto out_fail; 6161 goto out_fail_inode;
6067
6068 inode->i_op = &btrfs_dir_inode_operations;
6069 inode->i_fop = &btrfs_dir_file_operations;
6070 6162
6071 btrfs_i_size_write(inode, 0); 6163 btrfs_i_size_write(inode, 0);
6072 err = btrfs_update_inode(trans, root, inode); 6164 err = btrfs_update_inode(trans, root, inode);
6073 if (err) 6165 if (err)
6074 goto out_fail; 6166 goto out_fail_inode;
6075 6167
6076 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name, 6168 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
6077 dentry->d_name.len, 0, index); 6169 dentry->d_name.len, 0, index);
6078 if (err) 6170 if (err)
6079 goto out_fail; 6171 goto out_fail_inode;
6080 6172
6081 d_instantiate(dentry, inode); 6173 d_instantiate(dentry, inode);
6174 /*
6175 * mkdir is special. We're unlocking after we call d_instantiate
6176 * to avoid a race with nfsd calling d_instantiate.
6177 */
6178 unlock_new_inode(inode);
6082 drop_on_err = 0; 6179 drop_on_err = 0;
6083 6180
6084out_fail: 6181out_fail:
@@ -6088,6 +6185,10 @@ out_fail:
6088 btrfs_balance_delayed_items(root); 6185 btrfs_balance_delayed_items(root);
6089 btrfs_btree_balance_dirty(root); 6186 btrfs_btree_balance_dirty(root);
6090 return err; 6187 return err;
6188
6189out_fail_inode:
6190 unlock_new_inode(inode);
6191 goto out_fail;
6091} 6192}
6092 6193
6093/* helper for btfs_get_extent. Given an existing extent in the tree, 6194/* helper for btfs_get_extent. Given an existing extent in the tree,
@@ -6097,14 +6198,14 @@ out_fail:
6097static int merge_extent_mapping(struct extent_map_tree *em_tree, 6198static int merge_extent_mapping(struct extent_map_tree *em_tree,
6098 struct extent_map *existing, 6199 struct extent_map *existing,
6099 struct extent_map *em, 6200 struct extent_map *em,
6100 u64 map_start, u64 map_len) 6201 u64 map_start)
6101{ 6202{
6102 u64 start_diff; 6203 u64 start_diff;
6103 6204
6104 BUG_ON(map_start < em->start || map_start >= extent_map_end(em)); 6205 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
6105 start_diff = map_start - em->start; 6206 start_diff = map_start - em->start;
6106 em->start = map_start; 6207 em->start = map_start;
6107 em->len = map_len; 6208 em->len = existing->start - em->start;
6108 if (em->block_start < EXTENT_MAP_LAST_BYTE && 6209 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
6109 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { 6210 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
6110 em->block_start += start_diff; 6211 em->block_start += start_diff;
@@ -6275,6 +6376,8 @@ next:
6275 goto not_found; 6376 goto not_found;
6276 if (start + len <= found_key.offset) 6377 if (start + len <= found_key.offset)
6277 goto not_found; 6378 goto not_found;
6379 if (start > found_key.offset)
6380 goto next;
6278 em->start = start; 6381 em->start = start;
6279 em->orig_start = start; 6382 em->orig_start = start;
6280 em->len = found_key.offset - start; 6383 em->len = found_key.offset - start;
@@ -6390,8 +6493,7 @@ insert:
6390 em->len); 6493 em->len);
6391 if (existing) { 6494 if (existing) {
6392 err = merge_extent_mapping(em_tree, existing, 6495 err = merge_extent_mapping(em_tree, existing,
6393 em, start, 6496 em, start);
6394 root->sectorsize);
6395 free_extent_map(existing); 6497 free_extent_map(existing);
6396 if (err) { 6498 if (err) {
6397 free_extent_map(em); 6499 free_extent_map(em);
@@ -7158,7 +7260,8 @@ again:
7158 if (!ret) 7260 if (!ret)
7159 goto out_test; 7261 goto out_test;
7160 7262
7161 btrfs_init_work(&ordered->work, finish_ordered_fn, NULL, NULL); 7263 btrfs_init_work(&ordered->work, btrfs_endio_write_helper,
7264 finish_ordered_fn, NULL, NULL);
7162 btrfs_queue_work(root->fs_info->endio_write_workers, 7265 btrfs_queue_work(root->fs_info->endio_write_workers,
7163 &ordered->work); 7266 &ordered->work);
7164out_test: 7267out_test:
@@ -7306,10 +7409,8 @@ static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
7306 map_length = orig_bio->bi_iter.bi_size; 7409 map_length = orig_bio->bi_iter.bi_size;
7307 ret = btrfs_map_block(root->fs_info, rw, start_sector << 9, 7410 ret = btrfs_map_block(root->fs_info, rw, start_sector << 9,
7308 &map_length, NULL, 0); 7411 &map_length, NULL, 0);
7309 if (ret) { 7412 if (ret)
7310 bio_put(orig_bio);
7311 return -EIO; 7413 return -EIO;
7312 }
7313 7414
7314 if (map_length >= orig_bio->bi_iter.bi_size) { 7415 if (map_length >= orig_bio->bi_iter.bi_size) {
7315 bio = orig_bio; 7416 bio = orig_bio;
@@ -7326,6 +7427,7 @@ static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
7326 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS); 7427 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
7327 if (!bio) 7428 if (!bio)
7328 return -ENOMEM; 7429 return -ENOMEM;
7430
7329 bio->bi_private = dip; 7431 bio->bi_private = dip;
7330 bio->bi_end_io = btrfs_end_dio_bio; 7432 bio->bi_end_io = btrfs_end_dio_bio;
7331 atomic_inc(&dip->pending_bios); 7433 atomic_inc(&dip->pending_bios);
@@ -7534,7 +7636,8 @@ static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
7534 count = iov_iter_count(iter); 7636 count = iov_iter_count(iter);
7535 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, 7637 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
7536 &BTRFS_I(inode)->runtime_flags)) 7638 &BTRFS_I(inode)->runtime_flags))
7537 filemap_fdatawrite_range(inode->i_mapping, offset, count); 7639 filemap_fdatawrite_range(inode->i_mapping, offset,
7640 offset + count - 1);
7538 7641
7539 if (rw & WRITE) { 7642 if (rw & WRITE) {
7540 /* 7643 /*
@@ -8041,6 +8144,7 @@ int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
8041 8144
8042 set_nlink(inode, 1); 8145 set_nlink(inode, 1);
8043 btrfs_i_size_write(inode, 0); 8146 btrfs_i_size_write(inode, 0);
8147 unlock_new_inode(inode);
8044 8148
8045 err = btrfs_subvol_inherit_props(trans, new_root, parent_root); 8149 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
8046 if (err) 8150 if (err)
@@ -8495,7 +8599,9 @@ struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
8495 work->inode = inode; 8599 work->inode = inode;
8496 work->wait = wait; 8600 work->wait = wait;
8497 work->delay_iput = delay_iput; 8601 work->delay_iput = delay_iput;
8498 btrfs_init_work(&work->work, btrfs_run_delalloc_work, NULL, NULL); 8602 WARN_ON_ONCE(!inode);
8603 btrfs_init_work(&work->work, btrfs_flush_delalloc_helper,
8604 btrfs_run_delalloc_work, NULL, NULL);
8499 8605
8500 return work; 8606 return work;
8501} 8607}
@@ -8699,12 +8805,6 @@ static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
8699 goto out_unlock; 8805 goto out_unlock;
8700 } 8806 }
8701 8807
8702 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
8703 if (err) {
8704 drop_inode = 1;
8705 goto out_unlock;
8706 }
8707
8708 /* 8808 /*
8709 * If the active LSM wants to access the inode during 8809 * If the active LSM wants to access the inode during
8710 * d_instantiate it needs these. Smack checks to see 8810 * d_instantiate it needs these. Smack checks to see
@@ -8713,23 +8813,22 @@ static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
8713 */ 8813 */
8714 inode->i_fop = &btrfs_file_operations; 8814 inode->i_fop = &btrfs_file_operations;
8715 inode->i_op = &btrfs_file_inode_operations; 8815 inode->i_op = &btrfs_file_inode_operations;
8816 inode->i_mapping->a_ops = &btrfs_aops;
8817 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
8818 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
8819
8820 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
8821 if (err)
8822 goto out_unlock_inode;
8716 8823
8717 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); 8824 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
8718 if (err) 8825 if (err)
8719 drop_inode = 1; 8826 goto out_unlock_inode;
8720 else {
8721 inode->i_mapping->a_ops = &btrfs_aops;
8722 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
8723 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
8724 }
8725 if (drop_inode)
8726 goto out_unlock;
8727 8827
8728 path = btrfs_alloc_path(); 8828 path = btrfs_alloc_path();
8729 if (!path) { 8829 if (!path) {
8730 err = -ENOMEM; 8830 err = -ENOMEM;
8731 drop_inode = 1; 8831 goto out_unlock_inode;
8732 goto out_unlock;
8733 } 8832 }
8734 key.objectid = btrfs_ino(inode); 8833 key.objectid = btrfs_ino(inode);
8735 key.offset = 0; 8834 key.offset = 0;
@@ -8738,9 +8837,8 @@ static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
8738 err = btrfs_insert_empty_item(trans, root, path, &key, 8837 err = btrfs_insert_empty_item(trans, root, path, &key,
8739 datasize); 8838 datasize);
8740 if (err) { 8839 if (err) {
8741 drop_inode = 1;
8742 btrfs_free_path(path); 8840 btrfs_free_path(path);
8743 goto out_unlock; 8841 goto out_unlock_inode;
8744 } 8842 }
8745 leaf = path->nodes[0]; 8843 leaf = path->nodes[0];
8746 ei = btrfs_item_ptr(leaf, path->slots[0], 8844 ei = btrfs_item_ptr(leaf, path->slots[0],
@@ -8764,12 +8862,15 @@ static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
8764 inode_set_bytes(inode, name_len); 8862 inode_set_bytes(inode, name_len);
8765 btrfs_i_size_write(inode, name_len); 8863 btrfs_i_size_write(inode, name_len);
8766 err = btrfs_update_inode(trans, root, inode); 8864 err = btrfs_update_inode(trans, root, inode);
8767 if (err) 8865 if (err) {
8768 drop_inode = 1; 8866 drop_inode = 1;
8867 goto out_unlock_inode;
8868 }
8869
8870 unlock_new_inode(inode);
8871 d_instantiate(dentry, inode);
8769 8872
8770out_unlock: 8873out_unlock:
8771 if (!err)
8772 d_instantiate(dentry, inode);
8773 btrfs_end_transaction(trans, root); 8874 btrfs_end_transaction(trans, root);
8774 if (drop_inode) { 8875 if (drop_inode) {
8775 inode_dec_link_count(inode); 8876 inode_dec_link_count(inode);
@@ -8777,6 +8878,11 @@ out_unlock:
8777 } 8878 }
8778 btrfs_btree_balance_dirty(root); 8879 btrfs_btree_balance_dirty(root);
8779 return err; 8880 return err;
8881
8882out_unlock_inode:
8883 drop_inode = 1;
8884 unlock_new_inode(inode);
8885 goto out_unlock;
8780} 8886}
8781 8887
8782static int __btrfs_prealloc_file_range(struct inode *inode, int mode, 8888static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
@@ -8960,14 +9066,6 @@ static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
8960 goto out; 9066 goto out;
8961 } 9067 }
8962 9068
8963 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
8964 if (ret)
8965 goto out;
8966
8967 ret = btrfs_update_inode(trans, root, inode);
8968 if (ret)
8969 goto out;
8970
8971 inode->i_fop = &btrfs_file_operations; 9069 inode->i_fop = &btrfs_file_operations;
8972 inode->i_op = &btrfs_file_inode_operations; 9070 inode->i_op = &btrfs_file_inode_operations;
8973 9071
@@ -8975,10 +9073,26 @@ static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
8975 inode->i_mapping->backing_dev_info = &root->fs_info->bdi; 9073 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
8976 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; 9074 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
8977 9075
9076 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
9077 if (ret)
9078 goto out_inode;
9079
9080 ret = btrfs_update_inode(trans, root, inode);
9081 if (ret)
9082 goto out_inode;
8978 ret = btrfs_orphan_add(trans, inode); 9083 ret = btrfs_orphan_add(trans, inode);
8979 if (ret) 9084 if (ret)
8980 goto out; 9085 goto out_inode;
8981 9086
9087 /*
9088 * We set number of links to 0 in btrfs_new_inode(), and here we set
9089 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
9090 * through:
9091 *
9092 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
9093 */
9094 set_nlink(inode, 1);
9095 unlock_new_inode(inode);
8982 d_tmpfile(dentry, inode); 9096 d_tmpfile(dentry, inode);
8983 mark_inode_dirty(inode); 9097 mark_inode_dirty(inode);
8984 9098
@@ -8988,8 +9102,12 @@ out:
8988 iput(inode); 9102 iput(inode);
8989 btrfs_balance_delayed_items(root); 9103 btrfs_balance_delayed_items(root);
8990 btrfs_btree_balance_dirty(root); 9104 btrfs_btree_balance_dirty(root);
8991
8992 return ret; 9105 return ret;
9106
9107out_inode:
9108 unlock_new_inode(inode);
9109 goto out;
9110
8993} 9111}
8994 9112
8995static const struct inode_operations btrfs_dir_inode_operations = { 9113static const struct inode_operations btrfs_dir_inode_operations = {