@@ -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
2083
2091
/*
@@ -2086,6 +2094,7 @@ int btrfs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
2086
2094
*/
2087
2095
ret = btrfs_wait_ordered_range (inode , start , len );
2088
2096
if (ret ) {
2097
+ up_write (& BTRFS_I (inode )-> dio_sem );
2089
2098
inode_unlock (inode );
2090
2099
goto out ;
2091
2100
}
@@ -2109,6 +2118,7 @@ int btrfs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
2109
2118
* checked called fsync.
2110
2119
*/
2111
2120
ret = filemap_check_wb_err (inode -> i_mapping , file -> f_wb_err );
2121
+ up_write (& BTRFS_I (inode )-> dio_sem );
2112
2122
inode_unlock (inode );
2113
2123
goto out ;
2114
2124
}
@@ -2127,6 +2137,7 @@ int btrfs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
2127
2137
trans = btrfs_start_transaction (root , 0 );
2128
2138
if (IS_ERR (trans )) {
2129
2139
ret = PTR_ERR (trans );
2140
+ up_write (& BTRFS_I (inode )-> dio_sem );
2130
2141
inode_unlock (inode );
2131
2142
goto out ;
2132
2143
}
@@ -2148,6 +2159,7 @@ int btrfs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
2148
2159
* file again, but that will end up using the synchronization
2149
2160
* inside btrfs_sync_log to keep things safe.
2150
2161
*/
2162
+ up_write (& BTRFS_I (inode )-> dio_sem );
2151
2163
inode_unlock (inode );
2152
2164
2153
2165
/*
0 commit comments