aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2014-09-12 14:53:30 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2014-09-12 14:53:30 -0400
commit7ed641be750d365df4329764a504e22fbd30cd80 (patch)
tree6d8976f38f6f8a9fe74afa362bf940fc58e57764
parent9925cc1396339da25d5ef477be1f8c41b2391918 (diff)
parentb0d5d10f41a0f1cd839408dd94427f2db3553bca (diff)
Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mason/linux-btrfs
Pull btrfs fixes from Chris Mason: "Filipe is doing a careful pass through fsync problems, and these are the fixes so far. I'll have one more for rc6 that we're still testing. My big commit is fixing up some inode hash races that Al Viro found (thanks Al)" * 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mason/linux-btrfs: Btrfs: use insert_inode_locked4 for inode creation Btrfs: fix fsync data loss after a ranged fsync Btrfs: kfree()ing ERR_PTRs Btrfs: fix crash while doing a ranged fsync Btrfs: fix corruption after write/fsync failure + fsync + log recovery Btrfs: fix autodefrag with compression
-rw-r--r--fs/btrfs/file.c2
-rw-r--r--fs/btrfs/inode.c191
-rw-r--r--fs/btrfs/ioctl.c32
-rw-r--r--fs/btrfs/tree-log.c77
-rw-r--r--fs/btrfs/tree-log.h2
5 files changed, 203 insertions, 101 deletions
diff --git a/fs/btrfs/file.c b/fs/btrfs/file.c
index 36861b7a6757..ff1cc0399b9a 100644
--- a/fs/btrfs/file.c
+++ b/fs/btrfs/file.c
@@ -1966,7 +1966,7 @@ int btrfs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
1966 1966
1967 btrfs_init_log_ctx(&ctx); 1967 btrfs_init_log_ctx(&ctx);
1968 1968
1969 ret = btrfs_log_dentry_safe(trans, root, dentry, &ctx); 1969 ret = btrfs_log_dentry_safe(trans, root, dentry, start, end, &ctx);
1970 if (ret < 0) { 1970 if (ret < 0) {
1971 /* Fallthrough and commit/free transaction. */ 1971 /* Fallthrough and commit/free transaction. */
1972 ret = 1; 1972 ret = 1;
diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
index 9c194bd74d6e..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:
@@ -4242,7 +4248,8 @@ out:
4242 btrfs_abort_transaction(trans, root, ret); 4248 btrfs_abort_transaction(trans, root, ret);
4243 } 4249 }
4244error: 4250error:
4245 if (last_size != (u64)-1) 4251 if (last_size != (u64)-1 &&
4252 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
4246 btrfs_ordered_update_i_size(inode, last_size, NULL); 4253 btrfs_ordered_update_i_size(inode, last_size, NULL);
4247 btrfs_free_path(path); 4254 btrfs_free_path(path);
4248 return err; 4255 return err;
@@ -5627,6 +5634,17 @@ int btrfs_set_inode_index(struct inode *dir, u64 *index)
5627 return ret; 5634 return ret;
5628} 5635}
5629 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
5630static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans, 5648static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
5631 struct btrfs_root *root, 5649 struct btrfs_root *root,
5632 struct inode *dir, 5650 struct inode *dir,
@@ -5719,10 +5737,19 @@ static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
5719 sizes[1] = name_len + sizeof(*ref); 5737 sizes[1] = name_len + sizeof(*ref);
5720 } 5738 }
5721 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
5722 path->leave_spinning = 1; 5749 path->leave_spinning = 1;
5723 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems); 5750 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
5724 if (ret != 0) 5751 if (ret != 0)
5725 goto fail; 5752 goto fail_unlock;
5726 5753
5727 inode_init_owner(inode, dir, mode); 5754 inode_init_owner(inode, dir, mode);
5728 inode_set_bytes(inode, 0); 5755 inode_set_bytes(inode, 0);
@@ -5745,11 +5772,6 @@ static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
5745 btrfs_mark_buffer_dirty(path->nodes[0]); 5772 btrfs_mark_buffer_dirty(path->nodes[0]);
5746 btrfs_free_path(path); 5773 btrfs_free_path(path);
5747 5774
5748 location = &BTRFS_I(inode)->location;
5749 location->objectid = objectid;
5750 location->offset = 0;
5751 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
5752
5753 btrfs_inherit_iflags(inode, dir); 5775 btrfs_inherit_iflags(inode, dir);
5754 5776
5755 if (S_ISREG(mode)) { 5777 if (S_ISREG(mode)) {
@@ -5760,7 +5782,6 @@ static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
5760 BTRFS_INODE_NODATASUM; 5782 BTRFS_INODE_NODATASUM;
5761 } 5783 }
5762 5784
5763 btrfs_insert_inode_hash(inode);
5764 inode_tree_add(inode); 5785 inode_tree_add(inode);
5765 5786
5766 trace_btrfs_inode_new(inode); 5787 trace_btrfs_inode_new(inode);
@@ -5775,6 +5796,9 @@ static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
5775 btrfs_ino(inode), root->root_key.objectid, ret); 5796 btrfs_ino(inode), root->root_key.objectid, ret);
5776 5797
5777 return inode; 5798 return inode;
5799
5800fail_unlock:
5801 unlock_new_inode(inode);
5778fail: 5802fail:
5779 if (dir && name) 5803 if (dir && name)
5780 BTRFS_I(dir)->index_cnt--; 5804 BTRFS_I(dir)->index_cnt--;
@@ -5909,28 +5933,28 @@ static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
5909 goto out_unlock; 5933 goto out_unlock;
5910 } 5934 }
5911 5935
5912 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
5913 if (err) {
5914 drop_inode = 1;
5915 goto out_unlock;
5916 }
5917
5918 /* 5936 /*
5919 * If the active LSM wants to access the inode during 5937 * If the active LSM wants to access the inode during
5920 * d_instantiate it needs these. Smack checks to see 5938 * d_instantiate it needs these. Smack checks to see
5921 * if the filesystem supports xattrs by looking at the 5939 * if the filesystem supports xattrs by looking at the
5922 * ops vector. 5940 * ops vector.
5923 */ 5941 */
5924
5925 inode->i_op = &btrfs_special_inode_operations; 5942 inode->i_op = &btrfs_special_inode_operations;
5926 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);
5927 if (err) 5946 if (err)
5928 drop_inode = 1; 5947 goto out_unlock_inode;
5929 else { 5948
5930 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 {
5931 btrfs_update_inode(trans, root, inode); 5953 btrfs_update_inode(trans, root, inode);
5954 unlock_new_inode(inode);
5932 d_instantiate(dentry, inode); 5955 d_instantiate(dentry, inode);
5933 } 5956 }
5957
5934out_unlock: 5958out_unlock:
5935 btrfs_end_transaction(trans, root); 5959 btrfs_end_transaction(trans, root);
5936 btrfs_balance_delayed_items(root); 5960 btrfs_balance_delayed_items(root);
@@ -5940,6 +5964,12 @@ out_unlock:
5940 iput(inode); 5964 iput(inode);
5941 } 5965 }
5942 return err; 5966 return err;
5967
5968out_unlock_inode:
5969 drop_inode = 1;
5970 unlock_new_inode(inode);
5971 goto out_unlock;
5972
5943} 5973}
5944 5974
5945static int btrfs_create(struct inode *dir, struct dentry *dentry, 5975static int btrfs_create(struct inode *dir, struct dentry *dentry,
@@ -5974,15 +6004,6 @@ static int btrfs_create(struct inode *dir, struct dentry *dentry,
5974 goto out_unlock; 6004 goto out_unlock;
5975 } 6005 }
5976 drop_inode_on_err = 1; 6006 drop_inode_on_err = 1;
5977
5978 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
5979 if (err)
5980 goto out_unlock;
5981
5982 err = btrfs_update_inode(trans, root, inode);
5983 if (err)
5984 goto out_unlock;
5985
5986 /* 6007 /*
5987 * If the active LSM wants to access the inode during 6008 * If the active LSM wants to access the inode during
5988 * d_instantiate it needs these. Smack checks to see 6009 * d_instantiate it needs these. Smack checks to see
@@ -5991,14 +6012,23 @@ static int btrfs_create(struct inode *dir, struct dentry *dentry,
5991 */ 6012 */
5992 inode->i_fop = &btrfs_file_operations; 6013 inode->i_fop = &btrfs_file_operations;
5993 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;
5994 6025
5995 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); 6026 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
5996 if (err) 6027 if (err)
5997 goto out_unlock; 6028 goto out_unlock_inode;
5998 6029
5999 inode->i_mapping->a_ops = &btrfs_aops;
6000 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
6001 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);
6002 d_instantiate(dentry, inode); 6032 d_instantiate(dentry, inode);
6003 6033
6004out_unlock: 6034out_unlock:
@@ -6010,6 +6040,11 @@ out_unlock:
6010 btrfs_balance_delayed_items(root); 6040 btrfs_balance_delayed_items(root);
6011 btrfs_btree_balance_dirty(root); 6041 btrfs_btree_balance_dirty(root);
6012 return err; 6042 return err;
6043
6044out_unlock_inode:
6045 unlock_new_inode(inode);
6046 goto out_unlock;
6047
6013} 6048}
6014 6049
6015static int btrfs_link(struct dentry *old_dentry, struct inode *dir, 6050static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
@@ -6117,25 +6152,30 @@ static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
6117 } 6152 }
6118 6153
6119 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;
6120 6158
6121 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); 6159 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6122 if (err) 6160 if (err)
6123 goto out_fail; 6161 goto out_fail_inode;
6124
6125 inode->i_op = &btrfs_dir_inode_operations;
6126 inode->i_fop = &btrfs_dir_file_operations;
6127 6162
6128 btrfs_i_size_write(inode, 0); 6163 btrfs_i_size_write(inode, 0);
6129 err = btrfs_update_inode(trans, root, inode); 6164 err = btrfs_update_inode(trans, root, inode);
6130 if (err) 6165 if (err)
6131 goto out_fail; 6166 goto out_fail_inode;
6132 6167
6133 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name, 6168 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
6134 dentry->d_name.len, 0, index); 6169 dentry->d_name.len, 0, index);
6135 if (err) 6170 if (err)
6136 goto out_fail; 6171 goto out_fail_inode;
6137 6172
6138 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);
6139 drop_on_err = 0; 6179 drop_on_err = 0;
6140 6180
6141out_fail: 6181out_fail:
@@ -6145,6 +6185,10 @@ out_fail:
6145 btrfs_balance_delayed_items(root); 6185 btrfs_balance_delayed_items(root);
6146 btrfs_btree_balance_dirty(root); 6186 btrfs_btree_balance_dirty(root);
6147 return err; 6187 return err;
6188
6189out_fail_inode:
6190 unlock_new_inode(inode);
6191 goto out_fail;
6148} 6192}
6149 6193
6150/* 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,
@@ -8100,6 +8144,7 @@ int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
8100 8144
8101 set_nlink(inode, 1); 8145 set_nlink(inode, 1);
8102 btrfs_i_size_write(inode, 0); 8146 btrfs_i_size_write(inode, 0);
8147 unlock_new_inode(inode);
8103 8148
8104 err = btrfs_subvol_inherit_props(trans, new_root, parent_root); 8149 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
8105 if (err) 8150 if (err)
@@ -8760,12 +8805,6 @@ static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
8760 goto out_unlock; 8805 goto out_unlock;
8761 } 8806 }
8762 8807
8763 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
8764 if (err) {
8765 drop_inode = 1;
8766 goto out_unlock;
8767 }
8768
8769 /* 8808 /*
8770 * If the active LSM wants to access the inode during 8809 * If the active LSM wants to access the inode during
8771 * d_instantiate it needs these. Smack checks to see 8810 * d_instantiate it needs these. Smack checks to see
@@ -8774,23 +8813,22 @@ static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
8774 */ 8813 */
8775 inode->i_fop = &btrfs_file_operations; 8814 inode->i_fop = &btrfs_file_operations;
8776 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;
8777 8823
8778 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); 8824 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
8779 if (err) 8825 if (err)
8780 drop_inode = 1; 8826 goto out_unlock_inode;
8781 else {
8782 inode->i_mapping->a_ops = &btrfs_aops;
8783 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
8784 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
8785 }
8786 if (drop_inode)
8787 goto out_unlock;
8788 8827
8789 path = btrfs_alloc_path(); 8828 path = btrfs_alloc_path();
8790 if (!path) { 8829 if (!path) {
8791 err = -ENOMEM; 8830 err = -ENOMEM;
8792 drop_inode = 1; 8831 goto out_unlock_inode;
8793 goto out_unlock;
8794 } 8832 }
8795 key.objectid = btrfs_ino(inode); 8833 key.objectid = btrfs_ino(inode);
8796 key.offset = 0; 8834 key.offset = 0;
@@ -8799,9 +8837,8 @@ static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
8799 err = btrfs_insert_empty_item(trans, root, path, &key, 8837 err = btrfs_insert_empty_item(trans, root, path, &key,
8800 datasize); 8838 datasize);
8801 if (err) { 8839 if (err) {
8802 drop_inode = 1;
8803 btrfs_free_path(path); 8840 btrfs_free_path(path);
8804 goto out_unlock; 8841 goto out_unlock_inode;
8805 } 8842 }
8806 leaf = path->nodes[0]; 8843 leaf = path->nodes[0];
8807 ei = btrfs_item_ptr(leaf, path->slots[0], 8844 ei = btrfs_item_ptr(leaf, path->slots[0],
@@ -8825,12 +8862,15 @@ static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
8825 inode_set_bytes(inode, name_len); 8862 inode_set_bytes(inode, name_len);
8826 btrfs_i_size_write(inode, name_len); 8863 btrfs_i_size_write(inode, name_len);
8827 err = btrfs_update_inode(trans, root, inode); 8864 err = btrfs_update_inode(trans, root, inode);
8828 if (err) 8865 if (err) {
8829 drop_inode = 1; 8866 drop_inode = 1;
8867 goto out_unlock_inode;
8868 }
8869
8870 unlock_new_inode(inode);
8871 d_instantiate(dentry, inode);
8830 8872
8831out_unlock: 8873out_unlock:
8832 if (!err)
8833 d_instantiate(dentry, inode);
8834 btrfs_end_transaction(trans, root); 8874 btrfs_end_transaction(trans, root);
8835 if (drop_inode) { 8875 if (drop_inode) {
8836 inode_dec_link_count(inode); 8876 inode_dec_link_count(inode);
@@ -8838,6 +8878,11 @@ out_unlock:
8838 } 8878 }
8839 btrfs_btree_balance_dirty(root); 8879 btrfs_btree_balance_dirty(root);
8840 return err; 8880 return err;
8881
8882out_unlock_inode:
8883 drop_inode = 1;
8884 unlock_new_inode(inode);
8885 goto out_unlock;
8841} 8886}
8842 8887
8843static int __btrfs_prealloc_file_range(struct inode *inode, int mode, 8888static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
@@ -9021,14 +9066,6 @@ static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
9021 goto out; 9066 goto out;
9022 } 9067 }
9023 9068
9024 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
9025 if (ret)
9026 goto out;
9027
9028 ret = btrfs_update_inode(trans, root, inode);
9029 if (ret)
9030 goto out;
9031
9032 inode->i_fop = &btrfs_file_operations; 9069 inode->i_fop = &btrfs_file_operations;
9033 inode->i_op = &btrfs_file_inode_operations; 9070 inode->i_op = &btrfs_file_inode_operations;
9034 9071
@@ -9036,9 +9073,16 @@ static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
9036 inode->i_mapping->backing_dev_info = &root->fs_info->bdi; 9073 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
9037 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; 9074 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
9038 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;
9039 ret = btrfs_orphan_add(trans, inode); 9083 ret = btrfs_orphan_add(trans, inode);
9040 if (ret) 9084 if (ret)
9041 goto out; 9085 goto out_inode;
9042 9086
9043 /* 9087 /*
9044 * We set number of links to 0 in btrfs_new_inode(), and here we set 9088 * We set number of links to 0 in btrfs_new_inode(), and here we set
@@ -9048,6 +9092,7 @@ static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
9048 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink() 9092 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
9049 */ 9093 */
9050 set_nlink(inode, 1); 9094 set_nlink(inode, 1);
9095 unlock_new_inode(inode);
9051 d_tmpfile(dentry, inode); 9096 d_tmpfile(dentry, inode);
9052 mark_inode_dirty(inode); 9097 mark_inode_dirty(inode);
9053 9098
@@ -9057,8 +9102,12 @@ out:
9057 iput(inode); 9102 iput(inode);
9058 btrfs_balance_delayed_items(root); 9103 btrfs_balance_delayed_items(root);
9059 btrfs_btree_balance_dirty(root); 9104 btrfs_btree_balance_dirty(root);
9060
9061 return ret; 9105 return ret;
9106
9107out_inode:
9108 unlock_new_inode(inode);
9109 goto out;
9110
9062} 9111}
9063 9112
9064static const struct inode_operations btrfs_dir_inode_operations = { 9113static const struct inode_operations btrfs_dir_inode_operations = {
diff --git a/fs/btrfs/ioctl.c b/fs/btrfs/ioctl.c
index fce6fd0e3f50..8a8e29878c34 100644
--- a/fs/btrfs/ioctl.c
+++ b/fs/btrfs/ioctl.c
@@ -1019,8 +1019,10 @@ static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em)
1019 return false; 1019 return false;
1020 1020
1021 next = defrag_lookup_extent(inode, em->start + em->len); 1021 next = defrag_lookup_extent(inode, em->start + em->len);
1022 if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE || 1022 if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE)
1023 (em->block_start + em->block_len == next->block_start)) 1023 ret = false;
1024 else if ((em->block_start + em->block_len == next->block_start) &&
1025 (em->block_len > 128 * 1024 && next->block_len > 128 * 1024))
1024 ret = false; 1026 ret = false;
1025 1027
1026 free_extent_map(next); 1028 free_extent_map(next);
@@ -1055,7 +1057,6 @@ static int should_defrag_range(struct inode *inode, u64 start, int thresh,
1055 } 1057 }
1056 1058
1057 next_mergeable = defrag_check_next_extent(inode, em); 1059 next_mergeable = defrag_check_next_extent(inode, em);
1058
1059 /* 1060 /*
1060 * we hit a real extent, if it is big or the next extent is not a 1061 * we hit a real extent, if it is big or the next extent is not a
1061 * real extent, don't bother defragging it 1062 * real extent, don't bother defragging it
@@ -1702,7 +1703,7 @@ static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1702 ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY | 1703 ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY |
1703 BTRFS_SUBVOL_QGROUP_INHERIT)) { 1704 BTRFS_SUBVOL_QGROUP_INHERIT)) {
1704 ret = -EOPNOTSUPP; 1705 ret = -EOPNOTSUPP;
1705 goto out; 1706 goto free_args;
1706 } 1707 }
1707 1708
1708 if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC) 1709 if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
@@ -1712,27 +1713,31 @@ static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1712 if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) { 1713 if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) {
1713 if (vol_args->size > PAGE_CACHE_SIZE) { 1714 if (vol_args->size > PAGE_CACHE_SIZE) {
1714 ret = -EINVAL; 1715 ret = -EINVAL;
1715 goto out; 1716 goto free_args;
1716 } 1717 }
1717 inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size); 1718 inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size);
1718 if (IS_ERR(inherit)) { 1719 if (IS_ERR(inherit)) {
1719 ret = PTR_ERR(inherit); 1720 ret = PTR_ERR(inherit);
1720 goto out; 1721 goto free_args;
1721 } 1722 }
1722 } 1723 }
1723 1724
1724 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name, 1725 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
1725 vol_args->fd, subvol, ptr, 1726 vol_args->fd, subvol, ptr,
1726 readonly, inherit); 1727 readonly, inherit);
1728 if (ret)
1729 goto free_inherit;
1727 1730
1728 if (ret == 0 && ptr && 1731 if (ptr && copy_to_user(arg +
1729 copy_to_user(arg + 1732 offsetof(struct btrfs_ioctl_vol_args_v2,
1730 offsetof(struct btrfs_ioctl_vol_args_v2, 1733 transid),
1731 transid), ptr, sizeof(*ptr))) 1734 ptr, sizeof(*ptr)))
1732 ret = -EFAULT; 1735 ret = -EFAULT;
1733out: 1736
1734 kfree(vol_args); 1737free_inherit:
1735 kfree(inherit); 1738 kfree(inherit);
1739free_args:
1740 kfree(vol_args);
1736 return ret; 1741 return ret;
1737} 1742}
1738 1743
@@ -2652,7 +2657,7 @@ static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg)
2652 vol_args = memdup_user(arg, sizeof(*vol_args)); 2657 vol_args = memdup_user(arg, sizeof(*vol_args));
2653 if (IS_ERR(vol_args)) { 2658 if (IS_ERR(vol_args)) {
2654 ret = PTR_ERR(vol_args); 2659 ret = PTR_ERR(vol_args);
2655 goto out; 2660 goto err_drop;
2656 } 2661 }
2657 2662
2658 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; 2663 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
@@ -2670,6 +2675,7 @@ static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg)
2670 2675
2671out: 2676out:
2672 kfree(vol_args); 2677 kfree(vol_args);
2678err_drop:
2673 mnt_drop_write_file(file); 2679 mnt_drop_write_file(file);
2674 return ret; 2680 return ret;
2675} 2681}
diff --git a/fs/btrfs/tree-log.c b/fs/btrfs/tree-log.c
index 7e0e6e3029dd..d296efe2d3e7 100644
--- a/fs/btrfs/tree-log.c
+++ b/fs/btrfs/tree-log.c
@@ -94,8 +94,10 @@
94#define LOG_WALK_REPLAY_ALL 3 94#define LOG_WALK_REPLAY_ALL 3
95 95
96static int btrfs_log_inode(struct btrfs_trans_handle *trans, 96static int btrfs_log_inode(struct btrfs_trans_handle *trans,
97 struct btrfs_root *root, struct inode *inode, 97 struct btrfs_root *root, struct inode *inode,
98 int inode_only); 98 int inode_only,
99 const loff_t start,
100 const loff_t end);
99static int link_to_fixup_dir(struct btrfs_trans_handle *trans, 101static int link_to_fixup_dir(struct btrfs_trans_handle *trans,
100 struct btrfs_root *root, 102 struct btrfs_root *root,
101 struct btrfs_path *path, u64 objectid); 103 struct btrfs_path *path, u64 objectid);
@@ -3858,8 +3860,10 @@ process:
3858 * This handles both files and directories. 3860 * This handles both files and directories.
3859 */ 3861 */
3860static int btrfs_log_inode(struct btrfs_trans_handle *trans, 3862static int btrfs_log_inode(struct btrfs_trans_handle *trans,
3861 struct btrfs_root *root, struct inode *inode, 3863 struct btrfs_root *root, struct inode *inode,
3862 int inode_only) 3864 int inode_only,
3865 const loff_t start,
3866 const loff_t end)
3863{ 3867{
3864 struct btrfs_path *path; 3868 struct btrfs_path *path;
3865 struct btrfs_path *dst_path; 3869 struct btrfs_path *dst_path;
@@ -3876,6 +3880,7 @@ static int btrfs_log_inode(struct btrfs_trans_handle *trans,
3876 int ins_nr; 3880 int ins_nr;
3877 bool fast_search = false; 3881 bool fast_search = false;
3878 u64 ino = btrfs_ino(inode); 3882 u64 ino = btrfs_ino(inode);
3883 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
3879 3884
3880 path = btrfs_alloc_path(); 3885 path = btrfs_alloc_path();
3881 if (!path) 3886 if (!path)
@@ -4049,13 +4054,35 @@ log_extents:
4049 goto out_unlock; 4054 goto out_unlock;
4050 } 4055 }
4051 } else if (inode_only == LOG_INODE_ALL) { 4056 } else if (inode_only == LOG_INODE_ALL) {
4052 struct extent_map_tree *tree = &BTRFS_I(inode)->extent_tree;
4053 struct extent_map *em, *n; 4057 struct extent_map *em, *n;
4054 4058
4055 write_lock(&tree->lock); 4059 write_lock(&em_tree->lock);
4056 list_for_each_entry_safe(em, n, &tree->modified_extents, list) 4060 /*
4057 list_del_init(&em->list); 4061 * We can't just remove every em if we're called for a ranged
4058 write_unlock(&tree->lock); 4062 * fsync - that is, one that doesn't cover the whole possible
4063 * file range (0 to LLONG_MAX). This is because we can have
4064 * em's that fall outside the range we're logging and therefore
4065 * their ordered operations haven't completed yet
4066 * (btrfs_finish_ordered_io() not invoked yet). This means we
4067 * didn't get their respective file extent item in the fs/subvol
4068 * tree yet, and need to let the next fast fsync (one which
4069 * consults the list of modified extent maps) find the em so
4070 * that it logs a matching file extent item and waits for the
4071 * respective ordered operation to complete (if it's still
4072 * running).
4073 *
4074 * Removing every em outside the range we're logging would make
4075 * the next fast fsync not log their matching file extent items,
4076 * therefore making us lose data after a log replay.
4077 */
4078 list_for_each_entry_safe(em, n, &em_tree->modified_extents,
4079 list) {
4080 const u64 mod_end = em->mod_start + em->mod_len - 1;
4081
4082 if (em->mod_start >= start && mod_end <= end)
4083 list_del_init(&em->list);
4084 }
4085 write_unlock(&em_tree->lock);
4059 } 4086 }
4060 4087
4061 if (inode_only == LOG_INODE_ALL && S_ISDIR(inode->i_mode)) { 4088 if (inode_only == LOG_INODE_ALL && S_ISDIR(inode->i_mode)) {
@@ -4065,8 +4092,19 @@ log_extents:
4065 goto out_unlock; 4092 goto out_unlock;
4066 } 4093 }
4067 } 4094 }
4068 BTRFS_I(inode)->logged_trans = trans->transid; 4095
4069 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->last_sub_trans; 4096 write_lock(&em_tree->lock);
4097 /*
4098 * If we're doing a ranged fsync and there are still modified extents
4099 * in the list, we must run on the next fsync call as it might cover
4100 * those extents (a full fsync or an fsync for other range).
4101 */
4102 if (list_empty(&em_tree->modified_extents)) {
4103 BTRFS_I(inode)->logged_trans = trans->transid;
4104 BTRFS_I(inode)->last_log_commit =
4105 BTRFS_I(inode)->last_sub_trans;
4106 }
4107 write_unlock(&em_tree->lock);
4070out_unlock: 4108out_unlock:
4071 if (unlikely(err)) 4109 if (unlikely(err))
4072 btrfs_put_logged_extents(&logged_list); 4110 btrfs_put_logged_extents(&logged_list);
@@ -4161,7 +4199,10 @@ out:
4161 */ 4199 */
4162static int btrfs_log_inode_parent(struct btrfs_trans_handle *trans, 4200static int btrfs_log_inode_parent(struct btrfs_trans_handle *trans,
4163 struct btrfs_root *root, struct inode *inode, 4201 struct btrfs_root *root, struct inode *inode,
4164 struct dentry *parent, int exists_only, 4202 struct dentry *parent,
4203 const loff_t start,
4204 const loff_t end,
4205 int exists_only,
4165 struct btrfs_log_ctx *ctx) 4206 struct btrfs_log_ctx *ctx)
4166{ 4207{
4167 int inode_only = exists_only ? LOG_INODE_EXISTS : LOG_INODE_ALL; 4208 int inode_only = exists_only ? LOG_INODE_EXISTS : LOG_INODE_ALL;
@@ -4207,7 +4248,7 @@ static int btrfs_log_inode_parent(struct btrfs_trans_handle *trans,
4207 if (ret) 4248 if (ret)
4208 goto end_no_trans; 4249 goto end_no_trans;
4209 4250
4210 ret = btrfs_log_inode(trans, root, inode, inode_only); 4251 ret = btrfs_log_inode(trans, root, inode, inode_only, start, end);
4211 if (ret) 4252 if (ret)
4212 goto end_trans; 4253 goto end_trans;
4213 4254
@@ -4235,7 +4276,8 @@ static int btrfs_log_inode_parent(struct btrfs_trans_handle *trans,
4235 4276
4236 if (BTRFS_I(inode)->generation > 4277 if (BTRFS_I(inode)->generation >
4237 root->fs_info->last_trans_committed) { 4278 root->fs_info->last_trans_committed) {
4238 ret = btrfs_log_inode(trans, root, inode, inode_only); 4279 ret = btrfs_log_inode(trans, root, inode, inode_only,
4280 0, LLONG_MAX);
4239 if (ret) 4281 if (ret)
4240 goto end_trans; 4282 goto end_trans;
4241 } 4283 }
@@ -4269,13 +4311,15 @@ end_no_trans:
4269 */ 4311 */
4270int btrfs_log_dentry_safe(struct btrfs_trans_handle *trans, 4312int btrfs_log_dentry_safe(struct btrfs_trans_handle *trans,
4271 struct btrfs_root *root, struct dentry *dentry, 4313 struct btrfs_root *root, struct dentry *dentry,
4314 const loff_t start,
4315 const loff_t end,
4272 struct btrfs_log_ctx *ctx) 4316 struct btrfs_log_ctx *ctx)
4273{ 4317{
4274 struct dentry *parent = dget_parent(dentry); 4318 struct dentry *parent = dget_parent(dentry);
4275 int ret; 4319 int ret;
4276 4320
4277 ret = btrfs_log_inode_parent(trans, root, dentry->d_inode, parent, 4321 ret = btrfs_log_inode_parent(trans, root, dentry->d_inode, parent,
4278 0, ctx); 4322 start, end, 0, ctx);
4279 dput(parent); 4323 dput(parent);
4280 4324
4281 return ret; 4325 return ret;
@@ -4512,6 +4556,7 @@ int btrfs_log_new_name(struct btrfs_trans_handle *trans,
4512 root->fs_info->last_trans_committed)) 4556 root->fs_info->last_trans_committed))
4513 return 0; 4557 return 0;
4514 4558
4515 return btrfs_log_inode_parent(trans, root, inode, parent, 1, NULL); 4559 return btrfs_log_inode_parent(trans, root, inode, parent, 0,
4560 LLONG_MAX, 1, NULL);
4516} 4561}
4517 4562
diff --git a/fs/btrfs/tree-log.h b/fs/btrfs/tree-log.h
index 7f5b41bd5373..e2e798ae7cd7 100644
--- a/fs/btrfs/tree-log.h
+++ b/fs/btrfs/tree-log.h
@@ -59,6 +59,8 @@ int btrfs_free_log_root_tree(struct btrfs_trans_handle *trans,
59int btrfs_recover_log_trees(struct btrfs_root *tree_root); 59int btrfs_recover_log_trees(struct btrfs_root *tree_root);
60int btrfs_log_dentry_safe(struct btrfs_trans_handle *trans, 60int btrfs_log_dentry_safe(struct btrfs_trans_handle *trans,
61 struct btrfs_root *root, struct dentry *dentry, 61 struct btrfs_root *root, struct dentry *dentry,
62 const loff_t start,
63 const loff_t end,
62 struct btrfs_log_ctx *ctx); 64 struct btrfs_log_ctx *ctx);
63int btrfs_del_dir_entries_in_log(struct btrfs_trans_handle *trans, 65int btrfs_del_dir_entries_in_log(struct btrfs_trans_handle *trans,
64 struct btrfs_root *root, 66 struct btrfs_root *root,