@@ -2078,6 +2078,14 @@ int btrfs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
20782078 goto out ;
20792079
20802080 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+
20812089 atomic_inc (& root -> log_batch );
20822090
20832091 /*
@@ -2086,6 +2094,7 @@ int btrfs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
20862094 */
20872095 ret = btrfs_wait_ordered_range (inode , start , len );
20882096 if (ret ) {
2097+ up_write (& BTRFS_I (inode )-> dio_sem );
20892098 inode_unlock (inode );
20902099 goto out ;
20912100 }
@@ -2109,6 +2118,7 @@ int btrfs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
21092118 * checked called fsync.
21102119 */
21112120 ret = filemap_check_wb_err (inode -> i_mapping , file -> f_wb_err );
2121+ up_write (& BTRFS_I (inode )-> dio_sem );
21122122 inode_unlock (inode );
21132123 goto out ;
21142124 }
@@ -2127,6 +2137,7 @@ int btrfs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
21272137 trans = btrfs_start_transaction (root , 0 );
21282138 if (IS_ERR (trans )) {
21292139 ret = PTR_ERR (trans );
2140+ up_write (& BTRFS_I (inode )-> dio_sem );
21302141 inode_unlock (inode );
21312142 goto out ;
21322143 }
@@ -2148,6 +2159,7 @@ int btrfs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
21482159 * file again, but that will end up using the synchronization
21492160 * inside btrfs_sync_log to keep things safe.
21502161 */
2162+ up_write (& BTRFS_I (inode )-> dio_sem );
21512163 inode_unlock (inode );
21522164
21532165 /*
0 commit comments