Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

Merge tag 'xfs-6.12-fixes-4' of git://git.kernel.org/pub/scm/fs/xfs/xfs-linux

Pull xfs fixes from Carlos Maiolino:

- Fix integer overflow in xrep_bmap

- Fix stale dealloc punching for COW IO

* tag 'xfs-6.12-fixes-4' of git://git.kernel.org/pub/scm/fs/xfs/xfs-linux:
xfs: punch delalloc extents from the COW fork for COW writes
xfs: set IOMAP_F_SHARED for all COW fork allocations
xfs: share more code in xfs_buffered_write_iomap_begin
xfs: support the COW fork in xfs_bmap_punch_delalloc_range
xfs: IOMAP_ZERO and IOMAP_UNSHARE already hold invalidate_lock
xfs: take XFS_MMAPLOCK_EXCL xfs_file_write_zero_eof
xfs: factor out a xfs_file_write_zero_eof helper
iomap: move locking out of iomap_write_delalloc_release
iomap: remove iomap_file_buffered_write_punch_delalloc
iomap: factor out a iomap_last_written_block helper
xfs: fix integer overflow in xrep_bmap

+199 -165
+1 -1
Documentation/filesystems/iomap/operations.rst
··· 208 208 such `reservations 209 209 <https://lore.kernel.org/linux-xfs/20220817093627.GZ3600936@dread.disaster.area/>`_ 210 210 because writeback will not consume the reservation. 211 - The ``iomap_file_buffered_write_punch_delalloc`` can be called from a 211 + The ``iomap_write_delalloc_release`` can be called from a 212 212 ``->iomap_end`` function to find all the clean areas of the folios 213 213 caching a fresh (``IOMAP_F_NEW``) delalloc mapping. 214 214 It takes the ``invalidate_lock``.
+36 -75
fs/iomap/buffered-io.c
··· 1145 1145 } 1146 1146 1147 1147 /* 1148 + * When a short write occurs, the filesystem might need to use ->iomap_end 1149 + * to remove space reservations created in ->iomap_begin. 1150 + * 1151 + * For filesystems that use delayed allocation, there can be dirty pages over 1152 + * the delalloc extent outside the range of a short write but still within the 1153 + * delalloc extent allocated for this iomap if the write raced with page 1154 + * faults. 1155 + * 1148 1156 * Punch out all the delalloc blocks in the range given except for those that 1149 1157 * have dirty data still pending in the page cache - those are going to be 1150 1158 * written and so must still retain the delalloc backing for writeback. 1159 + * 1160 + * The punch() callback *must* only punch delalloc extents in the range passed 1161 + * to it. It must skip over all other types of extents in the range and leave 1162 + * them completely unchanged. It must do this punch atomically with respect to 1163 + * other extent modifications. 1164 + * 1165 + * The punch() callback may be called with a folio locked to prevent writeback 1166 + * extent allocation racing at the edge of the range we are currently punching. 1167 + * The locked folio may or may not cover the range being punched, so it is not 1168 + * safe for the punch() callback to lock folios itself. 1169 + * 1170 + * Lock order is: 1171 + * 1172 + * inode->i_rwsem (shared or exclusive) 1173 + * inode->i_mapping->invalidate_lock (exclusive) 1174 + * folio_lock() 1175 + * ->punch 1176 + * internal filesystem allocation lock 1151 1177 * 1152 1178 * As we are scanning the page cache for data, we don't need to reimplement the 1153 1179 * wheel - mapping_seek_hole_data() does exactly what we need to identify the ··· 1203 1177 * require sprinkling this code with magic "+ 1" and "- 1" arithmetic and expose 1204 1178 * the code to subtle off-by-one bugs.... 1205 1179 */ 1206 - static void iomap_write_delalloc_release(struct inode *inode, loff_t start_byte, 1180 + void iomap_write_delalloc_release(struct inode *inode, loff_t start_byte, 1207 1181 loff_t end_byte, unsigned flags, struct iomap *iomap, 1208 1182 iomap_punch_t punch) 1209 1183 { ··· 1211 1185 loff_t scan_end_byte = min(i_size_read(inode), end_byte); 1212 1186 1213 1187 /* 1214 - * Lock the mapping to avoid races with page faults re-instantiating 1215 - * folios and dirtying them via ->page_mkwrite whilst we walk the 1216 - * cache and perform delalloc extent removal. Failing to do this can 1217 - * leave dirty pages with no space reservation in the cache. 1188 + * The caller must hold invalidate_lock to avoid races with page faults 1189 + * re-instantiating folios and dirtying them via ->page_mkwrite whilst 1190 + * we walk the cache and perform delalloc extent removal. Failing to do 1191 + * this can leave dirty pages with no space reservation in the cache. 1218 1192 */ 1219 - filemap_invalidate_lock(inode->i_mapping); 1193 + lockdep_assert_held_write(&inode->i_mapping->invalidate_lock); 1194 + 1220 1195 while (start_byte < scan_end_byte) { 1221 1196 loff_t data_end; 1222 1197 ··· 1234 1207 if (start_byte == -ENXIO || start_byte == scan_end_byte) 1235 1208 break; 1236 1209 if (WARN_ON_ONCE(start_byte < 0)) 1237 - goto out_unlock; 1210 + return; 1238 1211 WARN_ON_ONCE(start_byte < punch_start_byte); 1239 1212 WARN_ON_ONCE(start_byte > scan_end_byte); 1240 1213 ··· 1245 1218 data_end = mapping_seek_hole_data(inode->i_mapping, start_byte, 1246 1219 scan_end_byte, SEEK_HOLE); 1247 1220 if (WARN_ON_ONCE(data_end < 0)) 1248 - goto out_unlock; 1221 + return; 1249 1222 1250 1223 /* 1251 1224 * If we race with post-direct I/O invalidation of the page cache, ··· 1267 1240 if (punch_start_byte < end_byte) 1268 1241 punch(inode, punch_start_byte, end_byte - punch_start_byte, 1269 1242 iomap); 1270 - out_unlock: 1271 - filemap_invalidate_unlock(inode->i_mapping); 1272 1243 } 1273 - 1274 - /* 1275 - * When a short write occurs, the filesystem may need to remove reserved space 1276 - * that was allocated in ->iomap_begin from it's ->iomap_end method. For 1277 - * filesystems that use delayed allocation, we need to punch out delalloc 1278 - * extents from the range that are not dirty in the page cache. As the write can 1279 - * race with page faults, there can be dirty pages over the delalloc extent 1280 - * outside the range of a short write but still within the delalloc extent 1281 - * allocated for this iomap. 1282 - * 1283 - * This function uses [start_byte, end_byte) intervals (i.e. open ended) to 1284 - * simplify range iterations. 1285 - * 1286 - * The punch() callback *must* only punch delalloc extents in the range passed 1287 - * to it. It must skip over all other types of extents in the range and leave 1288 - * them completely unchanged. It must do this punch atomically with respect to 1289 - * other extent modifications. 1290 - * 1291 - * The punch() callback may be called with a folio locked to prevent writeback 1292 - * extent allocation racing at the edge of the range we are currently punching. 1293 - * The locked folio may or may not cover the range being punched, so it is not 1294 - * safe for the punch() callback to lock folios itself. 1295 - * 1296 - * Lock order is: 1297 - * 1298 - * inode->i_rwsem (shared or exclusive) 1299 - * inode->i_mapping->invalidate_lock (exclusive) 1300 - * folio_lock() 1301 - * ->punch 1302 - * internal filesystem allocation lock 1303 - */ 1304 - void iomap_file_buffered_write_punch_delalloc(struct inode *inode, 1305 - loff_t pos, loff_t length, ssize_t written, unsigned flags, 1306 - struct iomap *iomap, iomap_punch_t punch) 1307 - { 1308 - loff_t start_byte; 1309 - loff_t end_byte; 1310 - unsigned int blocksize = i_blocksize(inode); 1311 - 1312 - if (iomap->type != IOMAP_DELALLOC) 1313 - return; 1314 - 1315 - /* If we didn't reserve the blocks, we're not allowed to punch them. */ 1316 - if (!(iomap->flags & IOMAP_F_NEW)) 1317 - return; 1318 - 1319 - /* 1320 - * start_byte refers to the first unused block after a short write. If 1321 - * nothing was written, round offset down to point at the first block in 1322 - * the range. 1323 - */ 1324 - if (unlikely(!written)) 1325 - start_byte = round_down(pos, blocksize); 1326 - else 1327 - start_byte = round_up(pos + written, blocksize); 1328 - end_byte = round_up(pos + length, blocksize); 1329 - 1330 - /* Nothing to do if we've written the entire delalloc extent */ 1331 - if (start_byte >= end_byte) 1332 - return; 1333 - 1334 - iomap_write_delalloc_release(inode, start_byte, end_byte, flags, iomap, 1335 - punch); 1336 - } 1337 - EXPORT_SYMBOL_GPL(iomap_file_buffered_write_punch_delalloc); 1244 + EXPORT_SYMBOL_GPL(iomap_write_delalloc_release); 1338 1245 1339 1246 static loff_t iomap_unshare_iter(struct iomap_iter *iter) 1340 1247 {
+1 -1
fs/xfs/scrub/bmap_repair.c
··· 801 801 { 802 802 struct xrep_bmap *rb; 803 803 char *descr; 804 - unsigned int max_bmbt_recs; 804 + xfs_extnum_t max_bmbt_recs; 805 805 bool large_extcount; 806 806 int error = 0; 807 807
+2 -2
fs/xfs/xfs_aops.c
··· 116 116 if (unlikely(error)) { 117 117 if (ioend->io_flags & IOMAP_F_SHARED) { 118 118 xfs_reflink_cancel_cow_range(ip, offset, size, true); 119 - xfs_bmap_punch_delalloc_range(ip, offset, 119 + xfs_bmap_punch_delalloc_range(ip, XFS_DATA_FORK, offset, 120 120 offset + size); 121 121 } 122 122 goto done; ··· 456 456 * byte of the next folio. Hence the end offset is only dependent on the 457 457 * folio itself and not the start offset that is passed in. 458 458 */ 459 - xfs_bmap_punch_delalloc_range(ip, pos, 459 + xfs_bmap_punch_delalloc_range(ip, XFS_DATA_FORK, pos, 460 460 folio_pos(folio) + folio_size(folio)); 461 461 } 462 462
+7 -3
fs/xfs/xfs_bmap_util.c
··· 442 442 void 443 443 xfs_bmap_punch_delalloc_range( 444 444 struct xfs_inode *ip, 445 + int whichfork, 445 446 xfs_off_t start_byte, 446 447 xfs_off_t end_byte) 447 448 { 448 449 struct xfs_mount *mp = ip->i_mount; 449 - struct xfs_ifork *ifp = &ip->i_df; 450 + struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork); 450 451 xfs_fileoff_t start_fsb = XFS_B_TO_FSBT(mp, start_byte); 451 452 xfs_fileoff_t end_fsb = XFS_B_TO_FSB(mp, end_byte); 452 453 struct xfs_bmbt_irec got, del; ··· 475 474 continue; 476 475 } 477 476 478 - xfs_bmap_del_extent_delay(ip, XFS_DATA_FORK, &icur, &got, &del); 477 + xfs_bmap_del_extent_delay(ip, whichfork, &icur, &got, &del); 479 478 if (!xfs_iext_get_extent(ifp, &icur, &got)) 480 479 break; 481 480 } 481 + 482 + if (whichfork == XFS_COW_FORK && !ifp->if_bytes) 483 + xfs_inode_clear_cowblocks_tag(ip); 482 484 483 485 out_unlock: 484 486 xfs_iunlock(ip, XFS_ILOCK_EXCL); ··· 584 580 */ 585 581 if (ip->i_diflags & (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)) { 586 582 if (ip->i_delayed_blks) { 587 - xfs_bmap_punch_delalloc_range(ip, 583 + xfs_bmap_punch_delalloc_range(ip, XFS_DATA_FORK, 588 584 round_up(XFS_ISIZE(ip), mp->m_sb.sb_blocksize), 589 585 LLONG_MAX); 590 586 }
+1 -1
fs/xfs/xfs_bmap_util.h
··· 30 30 } 31 31 #endif /* CONFIG_XFS_RT */ 32 32 33 - void xfs_bmap_punch_delalloc_range(struct xfs_inode *ip, 33 + void xfs_bmap_punch_delalloc_range(struct xfs_inode *ip, int whichfork, 34 34 xfs_off_t start_byte, xfs_off_t end_byte); 35 35 36 36 struct kgetbmap {
+88 -58
fs/xfs/xfs_file.c
··· 348 348 } 349 349 350 350 /* 351 + * Take care of zeroing post-EOF blocks when they might exist. 352 + * 353 + * Returns 0 if successfully, a negative error for a failure, or 1 if this 354 + * function dropped the iolock and reacquired it exclusively and the caller 355 + * needs to restart the write sanity checks. 356 + */ 357 + static ssize_t 358 + xfs_file_write_zero_eof( 359 + struct kiocb *iocb, 360 + struct iov_iter *from, 361 + unsigned int *iolock, 362 + size_t count, 363 + bool *drained_dio) 364 + { 365 + struct xfs_inode *ip = XFS_I(iocb->ki_filp->f_mapping->host); 366 + loff_t isize; 367 + int error; 368 + 369 + /* 370 + * We need to serialise against EOF updates that occur in IO completions 371 + * here. We want to make sure that nobody is changing the size while 372 + * we do this check until we have placed an IO barrier (i.e. hold 373 + * XFS_IOLOCK_EXCL) that prevents new IO from being dispatched. The 374 + * spinlock effectively forms a memory barrier once we have 375 + * XFS_IOLOCK_EXCL so we are guaranteed to see the latest EOF value and 376 + * hence be able to correctly determine if we need to run zeroing. 377 + */ 378 + spin_lock(&ip->i_flags_lock); 379 + isize = i_size_read(VFS_I(ip)); 380 + if (iocb->ki_pos <= isize) { 381 + spin_unlock(&ip->i_flags_lock); 382 + return 0; 383 + } 384 + spin_unlock(&ip->i_flags_lock); 385 + 386 + if (iocb->ki_flags & IOCB_NOWAIT) 387 + return -EAGAIN; 388 + 389 + if (!*drained_dio) { 390 + /* 391 + * If zeroing is needed and we are currently holding the iolock 392 + * shared, we need to update it to exclusive which implies 393 + * having to redo all checks before. 394 + */ 395 + if (*iolock == XFS_IOLOCK_SHARED) { 396 + xfs_iunlock(ip, *iolock); 397 + *iolock = XFS_IOLOCK_EXCL; 398 + xfs_ilock(ip, *iolock); 399 + iov_iter_reexpand(from, count); 400 + } 401 + 402 + /* 403 + * We now have an IO submission barrier in place, but AIO can do 404 + * EOF updates during IO completion and hence we now need to 405 + * wait for all of them to drain. Non-AIO DIO will have drained 406 + * before we are given the XFS_IOLOCK_EXCL, and so for most 407 + * cases this wait is a no-op. 408 + */ 409 + inode_dio_wait(VFS_I(ip)); 410 + *drained_dio = true; 411 + return 1; 412 + } 413 + 414 + trace_xfs_zero_eof(ip, isize, iocb->ki_pos - isize); 415 + 416 + xfs_ilock(ip, XFS_MMAPLOCK_EXCL); 417 + error = xfs_zero_range(ip, isize, iocb->ki_pos - isize, NULL); 418 + xfs_iunlock(ip, XFS_MMAPLOCK_EXCL); 419 + 420 + return error; 421 + } 422 + 423 + /* 351 424 * Common pre-write limit and setup checks. 352 425 * 353 - * Called with the iolocked held either shared and exclusive according to 426 + * Called with the iolock held either shared and exclusive according to 354 427 * @iolock, and returns with it held. Might upgrade the iolock to exclusive 355 428 * if called for a direct write beyond i_size. 356 429 */ ··· 433 360 struct iov_iter *from, 434 361 unsigned int *iolock) 435 362 { 436 - struct file *file = iocb->ki_filp; 437 - struct inode *inode = file->f_mapping->host; 438 - struct xfs_inode *ip = XFS_I(inode); 439 - ssize_t error = 0; 363 + struct inode *inode = iocb->ki_filp->f_mapping->host; 440 364 size_t count = iov_iter_count(from); 441 365 bool drained_dio = false; 442 - loff_t isize; 366 + ssize_t error; 443 367 444 368 restart: 445 369 error = generic_write_checks(iocb, from); ··· 459 389 * exclusively. 460 390 */ 461 391 if (*iolock == XFS_IOLOCK_SHARED && !IS_NOSEC(inode)) { 462 - xfs_iunlock(ip, *iolock); 392 + xfs_iunlock(XFS_I(inode), *iolock); 463 393 *iolock = XFS_IOLOCK_EXCL; 464 394 error = xfs_ilock_iocb(iocb, *iolock); 465 395 if (error) { ··· 470 400 } 471 401 472 402 /* 473 - * If the offset is beyond the size of the file, we need to zero any 403 + * If the offset is beyond the size of the file, we need to zero all 474 404 * blocks that fall between the existing EOF and the start of this 475 - * write. If zeroing is needed and we are currently holding the iolock 476 - * shared, we need to update it to exclusive which implies having to 477 - * redo all checks before. 405 + * write. 478 406 * 479 - * We need to serialise against EOF updates that occur in IO completions 480 - * here. We want to make sure that nobody is changing the size while we 481 - * do this check until we have placed an IO barrier (i.e. hold the 482 - * XFS_IOLOCK_EXCL) that prevents new IO from being dispatched. The 483 - * spinlock effectively forms a memory barrier once we have the 484 - * XFS_IOLOCK_EXCL so we are guaranteed to see the latest EOF value and 485 - * hence be able to correctly determine if we need to run zeroing. 486 - * 487 - * We can do an unlocked check here safely as IO completion can only 488 - * extend EOF. Truncate is locked out at this point, so the EOF can 489 - * not move backwards, only forwards. Hence we only need to take the 490 - * slow path and spin locks when we are at or beyond the current EOF. 407 + * We can do an unlocked check for i_size here safely as I/O completion 408 + * can only extend EOF. Truncate is locked out at this point, so the 409 + * EOF can not move backwards, only forwards. Hence we only need to take 410 + * the slow path when we are at or beyond the current EOF. 491 411 */ 492 - if (iocb->ki_pos <= i_size_read(inode)) 493 - goto out; 494 - 495 - spin_lock(&ip->i_flags_lock); 496 - isize = i_size_read(inode); 497 - if (iocb->ki_pos > isize) { 498 - spin_unlock(&ip->i_flags_lock); 499 - 500 - if (iocb->ki_flags & IOCB_NOWAIT) 501 - return -EAGAIN; 502 - 503 - if (!drained_dio) { 504 - if (*iolock == XFS_IOLOCK_SHARED) { 505 - xfs_iunlock(ip, *iolock); 506 - *iolock = XFS_IOLOCK_EXCL; 507 - xfs_ilock(ip, *iolock); 508 - iov_iter_reexpand(from, count); 509 - } 510 - /* 511 - * We now have an IO submission barrier in place, but 512 - * AIO can do EOF updates during IO completion and hence 513 - * we now need to wait for all of them to drain. Non-AIO 514 - * DIO will have drained before we are given the 515 - * XFS_IOLOCK_EXCL, and so for most cases this wait is a 516 - * no-op. 517 - */ 518 - inode_dio_wait(inode); 519 - drained_dio = true; 412 + if (iocb->ki_pos > i_size_read(inode)) { 413 + error = xfs_file_write_zero_eof(iocb, from, iolock, count, 414 + &drained_dio); 415 + if (error == 1) 520 416 goto restart; 521 - } 522 - 523 - trace_xfs_zero_eof(ip, isize, iocb->ki_pos - isize); 524 - error = xfs_zero_range(ip, isize, iocb->ki_pos - isize, NULL); 525 417 if (error) 526 418 return error; 527 - } else 528 - spin_unlock(&ip->i_flags_lock); 419 + } 529 420 530 - out: 531 421 return kiocb_modified(iocb); 532 422 } 533 423
+46 -21
fs/xfs/xfs_iomap.c
··· 975 975 int allocfork = XFS_DATA_FORK; 976 976 int error = 0; 977 977 unsigned int lockmode = XFS_ILOCK_EXCL; 978 + unsigned int iomap_flags = 0; 978 979 u64 seq; 979 980 980 981 if (xfs_is_shutdown(mp)) ··· 1146 1145 } 1147 1146 } 1148 1147 1148 + /* 1149 + * Flag newly allocated delalloc blocks with IOMAP_F_NEW so we punch 1150 + * them out if the write happens to fail. 1151 + */ 1152 + iomap_flags |= IOMAP_F_NEW; 1149 1153 if (allocfork == XFS_COW_FORK) { 1150 1154 error = xfs_bmapi_reserve_delalloc(ip, allocfork, offset_fsb, 1151 1155 end_fsb - offset_fsb, prealloc_blocks, &cmap, ··· 1168 1162 if (error) 1169 1163 goto out_unlock; 1170 1164 1171 - /* 1172 - * Flag newly allocated delalloc blocks with IOMAP_F_NEW so we punch 1173 - * them out if the write happens to fail. 1174 - */ 1175 - seq = xfs_iomap_inode_sequence(ip, IOMAP_F_NEW); 1176 - xfs_iunlock(ip, lockmode); 1177 1165 trace_xfs_iomap_alloc(ip, offset, count, allocfork, &imap); 1178 - return xfs_bmbt_to_iomap(ip, iomap, &imap, flags, IOMAP_F_NEW, seq); 1179 - 1180 1166 found_imap: 1181 - seq = xfs_iomap_inode_sequence(ip, 0); 1167 + seq = xfs_iomap_inode_sequence(ip, iomap_flags); 1182 1168 xfs_iunlock(ip, lockmode); 1183 - return xfs_bmbt_to_iomap(ip, iomap, &imap, flags, 0, seq); 1169 + return xfs_bmbt_to_iomap(ip, iomap, &imap, flags, iomap_flags, seq); 1184 1170 1185 1171 convert_delay: 1186 1172 xfs_iunlock(ip, lockmode); ··· 1186 1188 return 0; 1187 1189 1188 1190 found_cow: 1189 - seq = xfs_iomap_inode_sequence(ip, 0); 1190 1191 if (imap.br_startoff <= offset_fsb) { 1191 - error = xfs_bmbt_to_iomap(ip, srcmap, &imap, flags, 0, seq); 1192 + error = xfs_bmbt_to_iomap(ip, srcmap, &imap, flags, 0, 1193 + xfs_iomap_inode_sequence(ip, 0)); 1192 1194 if (error) 1193 1195 goto out_unlock; 1194 - seq = xfs_iomap_inode_sequence(ip, IOMAP_F_SHARED); 1195 - xfs_iunlock(ip, lockmode); 1196 - return xfs_bmbt_to_iomap(ip, iomap, &cmap, flags, 1197 - IOMAP_F_SHARED, seq); 1196 + } else { 1197 + xfs_trim_extent(&cmap, offset_fsb, 1198 + imap.br_startoff - offset_fsb); 1198 1199 } 1199 1200 1200 - xfs_trim_extent(&cmap, offset_fsb, imap.br_startoff - offset_fsb); 1201 + iomap_flags |= IOMAP_F_SHARED; 1202 + seq = xfs_iomap_inode_sequence(ip, iomap_flags); 1201 1203 xfs_iunlock(ip, lockmode); 1202 - return xfs_bmbt_to_iomap(ip, iomap, &cmap, flags, 0, seq); 1204 + return xfs_bmbt_to_iomap(ip, iomap, &cmap, flags, iomap_flags, seq); 1203 1205 1204 1206 out_unlock: 1205 1207 xfs_iunlock(ip, lockmode); ··· 1213 1215 loff_t length, 1214 1216 struct iomap *iomap) 1215 1217 { 1216 - xfs_bmap_punch_delalloc_range(XFS_I(inode), offset, offset + length); 1218 + xfs_bmap_punch_delalloc_range(XFS_I(inode), 1219 + (iomap->flags & IOMAP_F_SHARED) ? 1220 + XFS_COW_FORK : XFS_DATA_FORK, 1221 + offset, offset + length); 1217 1222 } 1218 1223 1219 1224 static int ··· 1228 1227 unsigned flags, 1229 1228 struct iomap *iomap) 1230 1229 { 1231 - iomap_file_buffered_write_punch_delalloc(inode, offset, length, written, 1232 - flags, iomap, &xfs_buffered_write_delalloc_punch); 1230 + loff_t start_byte, end_byte; 1231 + 1232 + /* If we didn't reserve the blocks, we're not allowed to punch them. */ 1233 + if (iomap->type != IOMAP_DELALLOC || !(iomap->flags & IOMAP_F_NEW)) 1234 + return 0; 1235 + 1236 + /* Nothing to do if we've written the entire delalloc extent */ 1237 + start_byte = iomap_last_written_block(inode, offset, written); 1238 + end_byte = round_up(offset + length, i_blocksize(inode)); 1239 + if (start_byte >= end_byte) 1240 + return 0; 1241 + 1242 + /* For zeroing operations the callers already hold invalidate_lock. */ 1243 + if (flags & (IOMAP_UNSHARE | IOMAP_ZERO)) { 1244 + rwsem_assert_held_write(&inode->i_mapping->invalidate_lock); 1245 + iomap_write_delalloc_release(inode, start_byte, end_byte, flags, 1246 + iomap, xfs_buffered_write_delalloc_punch); 1247 + } else { 1248 + filemap_invalidate_lock(inode->i_mapping); 1249 + iomap_write_delalloc_release(inode, start_byte, end_byte, flags, 1250 + iomap, xfs_buffered_write_delalloc_punch); 1251 + filemap_invalidate_unlock(inode->i_mapping); 1252 + } 1253 + 1233 1254 return 0; 1234 1255 } 1235 1256 ··· 1457 1434 bool *did_zero) 1458 1435 { 1459 1436 struct inode *inode = VFS_I(ip); 1437 + 1438 + xfs_assert_ilocked(ip, XFS_IOLOCK_EXCL | XFS_MMAPLOCK_EXCL); 1460 1439 1461 1440 if (IS_DAX(inode)) 1462 1441 return dax_zero_range(inode, pos, len, did_zero,
+17 -3
include/linux/iomap.h
··· 256 256 return &i->iomap; 257 257 } 258 258 259 + /* 260 + * Return the file offset for the first unchanged block after a short write. 261 + * 262 + * If nothing was written, round @pos down to point at the first block in 263 + * the range, else round up to include the partially written block. 264 + */ 265 + static inline loff_t iomap_last_written_block(struct inode *inode, loff_t pos, 266 + ssize_t written) 267 + { 268 + if (unlikely(!written)) 269 + return round_down(pos, i_blocksize(inode)); 270 + return round_up(pos + written, i_blocksize(inode)); 271 + } 272 + 259 273 ssize_t iomap_file_buffered_write(struct kiocb *iocb, struct iov_iter *from, 260 274 const struct iomap_ops *ops, void *private); 261 275 int iomap_read_folio(struct folio *folio, const struct iomap_ops *ops); ··· 290 276 291 277 typedef void (*iomap_punch_t)(struct inode *inode, loff_t offset, loff_t length, 292 278 struct iomap *iomap); 293 - void iomap_file_buffered_write_punch_delalloc(struct inode *inode, loff_t pos, 294 - loff_t length, ssize_t written, unsigned flag, 295 - struct iomap *iomap, iomap_punch_t punch); 279 + void iomap_write_delalloc_release(struct inode *inode, loff_t start_byte, 280 + loff_t end_byte, unsigned flags, struct iomap *iomap, 281 + iomap_punch_t punch); 296 282 297 283 int iomap_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, 298 284 u64 start, u64 len, const struct iomap_ops *ops);