@@ -2078,6 +2078,14 @@ int btrfs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
2078
2078
goto out ;
2079
2079
2080
2080
inode_lock (inode );
2081
+
2082
+ /*
2083
+ * We take the dio_sem here because the tree log stuff can race with
2084
+ * lockless dio writes and get an extent map logged for an extent we
2085
+ * never waited on. We need it this high up for lockdep reasons.
2086
+ */
2087
+ down_write (& BTRFS_I (inode )-> dio_sem );
2088
+
2081
2089
atomic_inc (& root -> log_batch );
2082
2090
full_sync = test_bit (BTRFS_INODE_NEEDS_FULL_SYNC ,
2083
2091
& BTRFS_I (inode )-> runtime_flags );
@@ -2129,6 +2137,7 @@ int btrfs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
2129
2137
ret = start_ordered_ops (inode , start , end );
2130
2138
}
2131
2139
if (ret ) {
2140
+ up_write (& BTRFS_I (inode )-> dio_sem );
2132
2141
inode_unlock (inode );
2133
2142
goto out ;
2134
2143
}
@@ -2184,6 +2193,7 @@ int btrfs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
2184
2193
* checked called fsync.
2185
2194
*/
2186
2195
ret = filemap_check_wb_err (inode -> i_mapping , file -> f_wb_err );
2196
+ up_write (& BTRFS_I (inode )-> dio_sem );
2187
2197
inode_unlock (inode );
2188
2198
goto out ;
2189
2199
}
@@ -2208,6 +2218,7 @@ int btrfs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
2208
2218
trans = btrfs_start_transaction (root , 0 );
2209
2219
if (IS_ERR (trans )) {
2210
2220
ret = PTR_ERR (trans );
2221
+ up_write (& BTRFS_I (inode )-> dio_sem );
2211
2222
inode_unlock (inode );
2212
2223
goto out ;
2213
2224
}
@@ -2229,6 +2240,7 @@ int btrfs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
2229
2240
* file again, but that will end up using the synchronization
2230
2241
* inside btrfs_sync_log to keep things safe.
2231
2242
*/
2243
+ up_write (& BTRFS_I (inode )-> dio_sem );
2232
2244
inode_unlock (inode );
2233
2245
2234
2246
/*
0 commit comments