1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (c) 2000-2006 Silicon Graphics, Inc. 4 * Copyright (c) 2016-2018 Christoph Hellwig. 5 * All Rights Reserved. 6 */ 7 #include "xfs_platform.h" 8 #include "xfs_fs.h" 9 #include "xfs_shared.h" 10 #include "xfs_format.h" 11 #include "xfs_log_format.h" 12 #include "xfs_trans_resv.h" 13 #include "xfs_mount.h" 14 #include "xfs_inode.h" 15 #include "xfs_btree.h" 16 #include "xfs_bmap_btree.h" 17 #include "xfs_bmap.h" 18 #include "xfs_bmap_util.h" 19 #include "xfs_errortag.h" 20 #include "xfs_error.h" 21 #include "xfs_trans.h" 22 #include "xfs_trans_space.h" 23 #include "xfs_inode_item.h" 24 #include "xfs_iomap.h" 25 #include "xfs_trace.h" 26 #include "xfs_quota.h" 27 #include "xfs_rtgroup.h" 28 #include "xfs_dquot_item.h" 29 #include "xfs_dquot.h" 30 #include "xfs_reflink.h" 31 #include "xfs_health.h" 32 #include "xfs_rtbitmap.h" 33 #include "xfs_icache.h" 34 #include "xfs_zone_alloc.h" 35 36 #define XFS_ALLOC_ALIGN(mp, off) \ 37 (((off) >> mp->m_allocsize_log) << mp->m_allocsize_log) 38 39 static int 40 xfs_alert_fsblock_zero( 41 xfs_inode_t *ip, 42 xfs_bmbt_irec_t *imap) 43 { 44 xfs_alert_tag(ip->i_mount, XFS_PTAG_FSBLOCK_ZERO, 45 "Access to block zero in inode %llu " 46 "start_block: %llx start_off: %llx " 47 "blkcnt: %llx extent-state: %x", 48 (unsigned long long)I_INO(ip), 49 (unsigned long long)imap->br_startblock, 50 (unsigned long long)imap->br_startoff, 51 (unsigned long long)imap->br_blockcount, 52 imap->br_state); 53 xfs_bmap_mark_sick(ip, XFS_DATA_FORK); 54 return -EFSCORRUPTED; 55 } 56 57 u64 58 xfs_iomap_inode_sequence( 59 struct xfs_inode *ip, 60 u16 iomap_flags) 61 { 62 u64 cookie = 0; 63 64 if (iomap_flags & IOMAP_F_XATTR) 65 return READ_ONCE(ip->i_af.if_seq); 66 if ((iomap_flags & IOMAP_F_SHARED) && ip->i_cowfp) 67 cookie = (u64)READ_ONCE(ip->i_cowfp->if_seq) << 32; 68 return cookie | READ_ONCE(ip->i_df.if_seq); 69 } 70 71 /* 72 * Check that the iomap passed to us is still valid for the given offset and 73 * length. 74 */ 75 static bool 76 xfs_iomap_valid( 77 struct inode *inode, 78 const struct iomap *iomap) 79 { 80 struct xfs_inode *ip = XFS_I(inode); 81 82 if (iomap->type == IOMAP_HOLE) 83 return true; 84 85 if (iomap->validity_cookie != 86 xfs_iomap_inode_sequence(ip, iomap->flags)) { 87 trace_xfs_iomap_invalid(ip, iomap); 88 return false; 89 } 90 91 XFS_ERRORTAG_DELAY(ip->i_mount, XFS_ERRTAG_WRITE_DELAY_MS); 92 return true; 93 } 94 95 const struct iomap_write_ops xfs_iomap_write_ops = { 96 .iomap_valid = xfs_iomap_valid, 97 }; 98 99 int 100 xfs_bmbt_to_iomap( 101 struct xfs_inode *ip, 102 struct iomap *iomap, 103 struct xfs_bmbt_irec *imap, 104 unsigned int mapping_flags, 105 u16 iomap_flags, 106 u64 sequence_cookie) 107 { 108 struct xfs_mount *mp = ip->i_mount; 109 struct xfs_buftarg *target = xfs_inode_buftarg(ip); 110 111 if (unlikely(!xfs_valid_startblock(ip, imap->br_startblock))) { 112 xfs_bmap_mark_sick(ip, XFS_DATA_FORK); 113 return xfs_alert_fsblock_zero(ip, imap); 114 } 115 116 iomap->flags = iomap_flags; 117 if (imap->br_startblock == HOLESTARTBLOCK) { 118 iomap->addr = IOMAP_NULL_ADDR; 119 iomap->type = IOMAP_HOLE; 120 } else if (imap->br_startblock == DELAYSTARTBLOCK || 121 isnullstartblock(imap->br_startblock)) { 122 iomap->addr = IOMAP_NULL_ADDR; 123 iomap->type = IOMAP_DELALLOC; 124 } else { 125 xfs_daddr_t daddr = xfs_fsb_to_db(ip, imap->br_startblock); 126 127 iomap->addr = BBTOB(daddr); 128 if (mapping_flags & IOMAP_DAX) 129 iomap->addr += target->bt_dax_part_off; 130 131 if (imap->br_state == XFS_EXT_UNWRITTEN) 132 iomap->type = IOMAP_UNWRITTEN; 133 else 134 iomap->type = IOMAP_MAPPED; 135 136 /* 137 * Mark iomaps starting at the first sector of a RTG as merge 138 * boundary so that each I/O completions is contained to a 139 * single RTG. 140 */ 141 if (XFS_IS_REALTIME_INODE(ip) && xfs_has_rtgroups(mp) && 142 xfs_rtbno_is_group_start(mp, imap->br_startblock)) 143 iomap->flags |= IOMAP_F_BOUNDARY; 144 } 145 iomap->offset = XFS_FSB_TO_B(mp, imap->br_startoff); 146 iomap->length = XFS_FSB_TO_B(mp, imap->br_blockcount); 147 if (mapping_flags & IOMAP_DAX) { 148 iomap->dax_dev = target->bt_daxdev; 149 } else { 150 iomap->bdev = target->bt_bdev; 151 if (bdev_has_integrity_csum(iomap->bdev)) 152 iomap->flags |= IOMAP_F_INTEGRITY; 153 } 154 155 /* 156 * If the inode is dirty for datasync purposes, let iomap know so it 157 * doesn't elide the IO completion journal flushes on O_DSYNC IO. 158 */ 159 if (ip->i_itemp) { 160 struct xfs_inode_log_item *iip = ip->i_itemp; 161 162 spin_lock(&iip->ili_lock); 163 if (iip->ili_datasync_seq) 164 iomap->flags |= IOMAP_F_DIRTY; 165 spin_unlock(&iip->ili_lock); 166 } 167 168 iomap->validity_cookie = sequence_cookie; 169 return 0; 170 } 171 172 static void 173 xfs_hole_to_iomap( 174 struct xfs_inode *ip, 175 struct iomap *iomap, 176 xfs_fileoff_t offset_fsb, 177 xfs_fileoff_t end_fsb) 178 { 179 struct xfs_buftarg *target = xfs_inode_buftarg(ip); 180 181 iomap->addr = IOMAP_NULL_ADDR; 182 iomap->type = IOMAP_HOLE; 183 iomap->offset = XFS_FSB_TO_B(ip->i_mount, offset_fsb); 184 iomap->length = XFS_FSB_TO_B(ip->i_mount, end_fsb - offset_fsb); 185 iomap->bdev = target->bt_bdev; 186 iomap->dax_dev = target->bt_daxdev; 187 } 188 189 static inline xfs_fileoff_t 190 xfs_iomap_end_fsb( 191 struct xfs_mount *mp, 192 loff_t offset, 193 loff_t count) 194 { 195 ASSERT(offset <= mp->m_super->s_maxbytes); 196 return min(XFS_B_TO_FSB(mp, offset + count), 197 XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes)); 198 } 199 200 static xfs_extlen_t 201 xfs_eof_alignment( 202 struct xfs_inode *ip) 203 { 204 struct xfs_mount *mp = ip->i_mount; 205 xfs_extlen_t align = 0; 206 207 if (!XFS_IS_REALTIME_INODE(ip)) { 208 /* 209 * Round up the allocation request to a stripe unit 210 * (m_dalign) boundary if the file size is >= stripe unit 211 * size, and we are allocating past the allocation eof. 212 * 213 * If mounted with the "-o swalloc" option the alignment is 214 * increased from the strip unit size to the stripe width. 215 */ 216 if (mp->m_swidth && xfs_has_swalloc(mp)) 217 align = mp->m_swidth; 218 else if (mp->m_dalign) 219 align = mp->m_dalign; 220 221 if (align && XFS_ISIZE(ip) < XFS_FSB_TO_B(mp, align)) 222 align = 0; 223 } 224 225 return align; 226 } 227 228 /* 229 * Check if last_fsb is outside the last extent, and if so grow it to the next 230 * stripe unit boundary. 231 */ 232 xfs_fileoff_t 233 xfs_iomap_eof_align_last_fsb( 234 struct xfs_inode *ip, 235 xfs_fileoff_t end_fsb) 236 { 237 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, XFS_DATA_FORK); 238 xfs_extlen_t extsz = xfs_get_extsz_hint(ip); 239 xfs_extlen_t align = xfs_eof_alignment(ip); 240 struct xfs_bmbt_irec irec; 241 struct xfs_iext_cursor icur; 242 243 ASSERT(!xfs_need_iread_extents(ifp)); 244 245 /* 246 * Always round up the allocation request to the extent hint boundary. 247 */ 248 if (extsz) { 249 if (align) 250 align = roundup_64(align, extsz); 251 else 252 align = extsz; 253 } 254 255 if (align) { 256 xfs_fileoff_t aligned_end_fsb = roundup_64(end_fsb, align); 257 258 xfs_iext_last(ifp, &icur); 259 if (!xfs_iext_get_extent(ifp, &icur, &irec) || 260 aligned_end_fsb >= irec.br_startoff + irec.br_blockcount) 261 return aligned_end_fsb; 262 } 263 264 return end_fsb; 265 } 266 267 int 268 xfs_iomap_write_direct( 269 struct xfs_inode *ip, 270 xfs_fileoff_t offset_fsb, 271 xfs_fileoff_t count_fsb, 272 unsigned int flags, 273 struct xfs_bmbt_irec *imap, 274 u64 *seq) 275 { 276 struct xfs_mount *mp = ip->i_mount; 277 struct xfs_trans *tp; 278 xfs_filblks_t resaligned; 279 int nimaps; 280 unsigned int dblocks, rblocks; 281 bool force = false; 282 int error; 283 int bmapi_flags = XFS_BMAPI_PREALLOC; 284 int nr_exts = XFS_IEXT_ADD_NOSPLIT_CNT; 285 286 ASSERT(count_fsb > 0); 287 288 resaligned = xfs_aligned_fsb_count(offset_fsb, count_fsb, 289 xfs_get_extsz_hint(ip)); 290 if (unlikely(XFS_IS_REALTIME_INODE(ip))) { 291 dblocks = XFS_DIOSTRAT_SPACE_RES(mp, 0); 292 rblocks = resaligned; 293 } else { 294 dblocks = XFS_DIOSTRAT_SPACE_RES(mp, resaligned); 295 rblocks = 0; 296 } 297 298 error = xfs_qm_dqattach(ip); 299 if (error) 300 return error; 301 302 /* 303 * For DAX, we do not allocate unwritten extents, but instead we zero 304 * the block before we commit the transaction. Ideally we'd like to do 305 * this outside the transaction context, but if we commit and then crash 306 * we may not have zeroed the blocks and this will be exposed on 307 * recovery of the allocation. Hence we must zero before commit. 308 * 309 * Further, if we are mapping unwritten extents here, we need to zero 310 * and convert them to written so that we don't need an unwritten extent 311 * callback for DAX. This also means that we need to be able to dip into 312 * the reserve block pool for bmbt block allocation if there is no space 313 * left but we need to do unwritten extent conversion. 314 */ 315 if (flags & IOMAP_DAX) { 316 bmapi_flags = XFS_BMAPI_CONVERT | XFS_BMAPI_ZERO; 317 if (imap->br_state == XFS_EXT_UNWRITTEN) { 318 force = true; 319 nr_exts = XFS_IEXT_WRITE_UNWRITTEN_CNT; 320 dblocks = XFS_DIOSTRAT_SPACE_RES(mp, 0) << 1; 321 } 322 } 323 324 error = xfs_trans_alloc_inode(ip, &M_RES(mp)->tr_write, dblocks, 325 rblocks, force, &tp); 326 if (error) 327 return error; 328 329 error = xfs_iext_count_extend(tp, ip, XFS_DATA_FORK, nr_exts); 330 if (error) 331 goto out_trans_cancel; 332 333 /* 334 * From this point onwards we overwrite the imap pointer that the 335 * caller gave to us. 336 */ 337 nimaps = 1; 338 error = xfs_bmapi_write(tp, ip, offset_fsb, count_fsb, bmapi_flags, 0, 339 imap, &nimaps); 340 if (error) 341 goto out_trans_cancel; 342 343 /* 344 * Complete the transaction 345 */ 346 error = xfs_trans_commit(tp); 347 if (error) 348 goto out_unlock; 349 350 if (unlikely(!xfs_valid_startblock(ip, imap->br_startblock))) { 351 xfs_bmap_mark_sick(ip, XFS_DATA_FORK); 352 error = xfs_alert_fsblock_zero(ip, imap); 353 } 354 355 out_unlock: 356 *seq = xfs_iomap_inode_sequence(ip, 0); 357 xfs_iunlock(ip, XFS_ILOCK_EXCL); 358 return error; 359 360 out_trans_cancel: 361 xfs_trans_cancel_error(tp, error); 362 goto out_unlock; 363 } 364 365 STATIC bool 366 xfs_quota_need_throttle( 367 struct xfs_inode *ip, 368 xfs_dqtype_t type, 369 xfs_fsblock_t alloc_blocks) 370 { 371 struct xfs_dquot *dq = xfs_inode_dquot(ip, type); 372 struct xfs_dquot_res *res; 373 struct xfs_dquot_pre *pre; 374 375 if (!dq || !xfs_this_quota_on(ip->i_mount, type)) 376 return false; 377 378 if (XFS_IS_REALTIME_INODE(ip)) { 379 res = &dq->q_rtb; 380 pre = &dq->q_rtb_prealloc; 381 } else { 382 res = &dq->q_blk; 383 pre = &dq->q_blk_prealloc; 384 } 385 386 /* no hi watermark, no throttle */ 387 if (!pre->q_prealloc_hi_wmark) 388 return false; 389 390 /* under the lo watermark, no throttle */ 391 if (res->reserved + alloc_blocks < pre->q_prealloc_lo_wmark) 392 return false; 393 394 return true; 395 } 396 397 STATIC void 398 xfs_quota_calc_throttle( 399 struct xfs_inode *ip, 400 xfs_dqtype_t type, 401 xfs_fsblock_t *qblocks, 402 int *qshift, 403 int64_t *qfreesp) 404 { 405 struct xfs_dquot *dq = xfs_inode_dquot(ip, type); 406 struct xfs_dquot_res *res; 407 struct xfs_dquot_pre *pre; 408 int64_t freesp; 409 int shift = 0; 410 411 if (!dq) { 412 res = NULL; 413 pre = NULL; 414 } else if (XFS_IS_REALTIME_INODE(ip)) { 415 res = &dq->q_rtb; 416 pre = &dq->q_rtb_prealloc; 417 } else { 418 res = &dq->q_blk; 419 pre = &dq->q_blk_prealloc; 420 } 421 422 /* no dq, or over hi wmark, squash the prealloc completely */ 423 if (!res || res->reserved >= pre->q_prealloc_hi_wmark) { 424 *qblocks = 0; 425 *qfreesp = 0; 426 return; 427 } 428 429 freesp = pre->q_prealloc_hi_wmark - res->reserved; 430 if (freesp < pre->q_low_space[XFS_QLOWSP_5_PCNT]) { 431 shift = 2; 432 if (freesp < pre->q_low_space[XFS_QLOWSP_3_PCNT]) 433 shift += 2; 434 if (freesp < pre->q_low_space[XFS_QLOWSP_1_PCNT]) 435 shift += 2; 436 } 437 438 if (freesp < *qfreesp) 439 *qfreesp = freesp; 440 441 /* only overwrite the throttle values if we are more aggressive */ 442 if ((freesp >> shift) < (*qblocks >> *qshift)) { 443 *qblocks = freesp; 444 *qshift = shift; 445 } 446 } 447 448 static int64_t 449 xfs_iomap_freesp( 450 struct xfs_mount *mp, 451 unsigned int idx, 452 uint64_t low_space[XFS_LOWSP_MAX], 453 int *shift) 454 { 455 int64_t freesp; 456 457 freesp = xfs_estimate_freecounter(mp, idx); 458 if (freesp < low_space[XFS_LOWSP_5_PCNT]) { 459 *shift = 2; 460 if (freesp < low_space[XFS_LOWSP_4_PCNT]) 461 (*shift)++; 462 if (freesp < low_space[XFS_LOWSP_3_PCNT]) 463 (*shift)++; 464 if (freesp < low_space[XFS_LOWSP_2_PCNT]) 465 (*shift)++; 466 if (freesp < low_space[XFS_LOWSP_1_PCNT]) 467 (*shift)++; 468 } 469 return freesp; 470 } 471 472 /* 473 * If we don't have a user specified preallocation size, dynamically increase 474 * the preallocation size as the size of the file grows. Cap the maximum size 475 * at a single extent or less if the filesystem is near full. The closer the 476 * filesystem is to being full, the smaller the maximum preallocation. 477 */ 478 STATIC xfs_fsblock_t 479 xfs_iomap_prealloc_size( 480 struct xfs_inode *ip, 481 int whichfork, 482 loff_t offset, 483 loff_t count, 484 struct xfs_iext_cursor *icur) 485 { 486 struct xfs_iext_cursor ncur = *icur; 487 struct xfs_bmbt_irec prev, got; 488 struct xfs_mount *mp = ip->i_mount; 489 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork); 490 xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset); 491 int64_t freesp; 492 xfs_fsblock_t qblocks; 493 xfs_fsblock_t alloc_blocks = 0; 494 xfs_extlen_t plen; 495 int shift = 0; 496 int qshift = 0; 497 498 /* 499 * As an exception we don't do any preallocation at all if the file is 500 * smaller than the minimum preallocation and we are using the default 501 * dynamic preallocation scheme, as it is likely this is the only write 502 * to the file that is going to be done. 503 */ 504 if (XFS_ISIZE(ip) < XFS_FSB_TO_B(mp, mp->m_allocsize_blocks)) 505 return 0; 506 507 /* 508 * Use the minimum preallocation size for small files or if we are 509 * writing right after a hole. 510 */ 511 if (XFS_ISIZE(ip) < XFS_FSB_TO_B(mp, mp->m_dalign) || 512 !xfs_iext_prev_extent(ifp, &ncur, &prev) || 513 prev.br_startoff + prev.br_blockcount < offset_fsb) 514 return mp->m_allocsize_blocks; 515 516 /* 517 * Take the size of the preceding data extents as the basis for the 518 * preallocation size. Note that we don't care if the previous extents 519 * are written or not. 520 */ 521 plen = prev.br_blockcount; 522 while (xfs_iext_prev_extent(ifp, &ncur, &got)) { 523 if (plen > XFS_MAX_BMBT_EXTLEN / 2 || 524 isnullstartblock(got.br_startblock) || 525 got.br_startoff + got.br_blockcount != prev.br_startoff || 526 got.br_startblock + got.br_blockcount != prev.br_startblock) 527 break; 528 plen += got.br_blockcount; 529 prev = got; 530 } 531 532 /* 533 * If the size of the extents is greater than half the maximum extent 534 * length, then use the current offset as the basis. This ensures that 535 * for large files the preallocation size always extends to 536 * XFS_BMBT_MAX_EXTLEN rather than falling short due to things like stripe 537 * unit/width alignment of real extents. 538 */ 539 alloc_blocks = plen * 2; 540 if (alloc_blocks > XFS_MAX_BMBT_EXTLEN) 541 alloc_blocks = XFS_B_TO_FSB(mp, offset); 542 qblocks = alloc_blocks; 543 544 /* 545 * XFS_BMBT_MAX_EXTLEN is not a power of two value but we round the prealloc 546 * down to the nearest power of two value after throttling. To prevent 547 * the round down from unconditionally reducing the maximum supported 548 * prealloc size, we round up first, apply appropriate throttling, round 549 * down and cap the value to XFS_BMBT_MAX_EXTLEN. 550 */ 551 alloc_blocks = XFS_FILEOFF_MIN(roundup_pow_of_two(XFS_MAX_BMBT_EXTLEN), 552 alloc_blocks); 553 554 if (unlikely(XFS_IS_REALTIME_INODE(ip))) 555 freesp = xfs_rtbxlen_to_blen(mp, 556 xfs_iomap_freesp(mp, XC_FREE_RTEXTENTS, 557 mp->m_low_rtexts, &shift)); 558 else 559 freesp = xfs_iomap_freesp(mp, XC_FREE_BLOCKS, mp->m_low_space, 560 &shift); 561 562 /* 563 * Check each quota to cap the prealloc size, provide a shift value to 564 * throttle with and adjust amount of available space. 565 */ 566 if (xfs_quota_need_throttle(ip, XFS_DQTYPE_USER, alloc_blocks)) 567 xfs_quota_calc_throttle(ip, XFS_DQTYPE_USER, &qblocks, &qshift, 568 &freesp); 569 if (xfs_quota_need_throttle(ip, XFS_DQTYPE_GROUP, alloc_blocks)) 570 xfs_quota_calc_throttle(ip, XFS_DQTYPE_GROUP, &qblocks, &qshift, 571 &freesp); 572 if (xfs_quota_need_throttle(ip, XFS_DQTYPE_PROJ, alloc_blocks)) 573 xfs_quota_calc_throttle(ip, XFS_DQTYPE_PROJ, &qblocks, &qshift, 574 &freesp); 575 576 /* 577 * The final prealloc size is set to the minimum of free space available 578 * in each of the quotas and the overall filesystem. 579 * 580 * The shift throttle value is set to the maximum value as determined by 581 * the global low free space values and per-quota low free space values. 582 */ 583 alloc_blocks = min(alloc_blocks, qblocks); 584 shift = max(shift, qshift); 585 586 if (shift) 587 alloc_blocks >>= shift; 588 /* 589 * rounddown_pow_of_two() returns an undefined result if we pass in 590 * alloc_blocks = 0. 591 */ 592 if (alloc_blocks) 593 alloc_blocks = rounddown_pow_of_two(alloc_blocks); 594 if (alloc_blocks > XFS_MAX_BMBT_EXTLEN) 595 alloc_blocks = XFS_MAX_BMBT_EXTLEN; 596 597 /* 598 * If we are still trying to allocate more space than is 599 * available, squash the prealloc hard. This can happen if we 600 * have a large file on a small filesystem and the above 601 * lowspace thresholds are smaller than XFS_BMBT_MAX_EXTLEN. 602 */ 603 while (alloc_blocks && alloc_blocks >= freesp) 604 alloc_blocks >>= 4; 605 if (alloc_blocks < mp->m_allocsize_blocks) 606 alloc_blocks = mp->m_allocsize_blocks; 607 trace_xfs_iomap_prealloc_size(ip, alloc_blocks, shift, 608 mp->m_allocsize_blocks); 609 return alloc_blocks; 610 } 611 612 int 613 xfs_iomap_write_unwritten( 614 xfs_inode_t *ip, 615 xfs_off_t offset, 616 xfs_off_t count, 617 bool update_isize) 618 { 619 xfs_mount_t *mp = ip->i_mount; 620 xfs_fileoff_t offset_fsb; 621 xfs_filblks_t count_fsb; 622 xfs_filblks_t numblks_fsb; 623 int nimaps; 624 xfs_trans_t *tp; 625 xfs_bmbt_irec_t imap; 626 struct inode *inode = VFS_I(ip); 627 xfs_fsize_t i_size; 628 uint resblks; 629 int error; 630 631 trace_xfs_unwritten_convert(ip, offset, count); 632 633 offset_fsb = XFS_B_TO_FSBT(mp, offset); 634 count_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + count); 635 count_fsb = (xfs_filblks_t)(count_fsb - offset_fsb); 636 637 /* 638 * Reserve enough blocks in this transaction for two complete extent 639 * btree splits. We may be converting the middle part of an unwritten 640 * extent and in this case we will insert two new extents in the btree 641 * each of which could cause a full split. 642 * 643 * This reservation amount will be used in the first call to 644 * xfs_bmbt_split() to select an AG with enough space to satisfy the 645 * rest of the operation. 646 */ 647 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0) << 1; 648 649 /* Attach dquots so that bmbt splits are accounted correctly. */ 650 error = xfs_qm_dqattach(ip); 651 if (error) 652 return error; 653 654 do { 655 /* 656 * Set up a transaction to convert the range of extents 657 * from unwritten to real. Do allocations in a loop until 658 * we have covered the range passed in. 659 * 660 * Note that we can't risk to recursing back into the filesystem 661 * here as we might be asked to write out the same inode that we 662 * complete here and might deadlock on the iolock. 663 */ 664 error = xfs_trans_alloc_inode(ip, &M_RES(mp)->tr_write, resblks, 665 0, true, &tp); 666 if (error) 667 return error; 668 669 error = xfs_iext_count_extend(tp, ip, XFS_DATA_FORK, 670 XFS_IEXT_WRITE_UNWRITTEN_CNT); 671 if (error) 672 goto error_on_bmapi_transaction; 673 674 /* 675 * Modify the unwritten extent state of the buffer. 676 */ 677 nimaps = 1; 678 error = xfs_bmapi_write(tp, ip, offset_fsb, count_fsb, 679 XFS_BMAPI_CONVERT, resblks, &imap, 680 &nimaps); 681 if (error) 682 goto error_on_bmapi_transaction; 683 684 /* 685 * Log the updated inode size as we go. We have to be careful 686 * to only log it up to the actual write offset if it is 687 * halfway into a block. 688 */ 689 i_size = XFS_FSB_TO_B(mp, offset_fsb + count_fsb); 690 if (i_size > offset + count) 691 i_size = offset + count; 692 if (update_isize && i_size > i_size_read(inode)) 693 i_size_write(inode, i_size); 694 i_size = xfs_new_eof(ip, i_size); 695 if (i_size) { 696 ip->i_disk_size = i_size; 697 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); 698 } 699 700 error = xfs_trans_commit(tp); 701 xfs_iunlock(ip, XFS_ILOCK_EXCL); 702 if (error) 703 return error; 704 705 if (unlikely(!xfs_valid_startblock(ip, imap.br_startblock))) { 706 xfs_bmap_mark_sick(ip, XFS_DATA_FORK); 707 return xfs_alert_fsblock_zero(ip, &imap); 708 } 709 710 if ((numblks_fsb = imap.br_blockcount) == 0) { 711 /* 712 * The numblks_fsb value should always get 713 * smaller, otherwise the loop is stuck. 714 */ 715 ASSERT(imap.br_blockcount); 716 break; 717 } 718 offset_fsb += numblks_fsb; 719 count_fsb -= numblks_fsb; 720 } while (count_fsb > 0); 721 722 return 0; 723 724 error_on_bmapi_transaction: 725 xfs_trans_cancel(tp); 726 xfs_iunlock(ip, XFS_ILOCK_EXCL); 727 return error; 728 } 729 730 static inline bool 731 imap_needs_alloc( 732 struct inode *inode, 733 unsigned flags, 734 struct xfs_bmbt_irec *imap, 735 int nimaps) 736 { 737 /* don't allocate blocks when just zeroing */ 738 if (flags & IOMAP_ZERO) 739 return false; 740 if (!nimaps || 741 imap->br_startblock == HOLESTARTBLOCK || 742 imap->br_startblock == DELAYSTARTBLOCK) 743 return true; 744 /* we convert unwritten extents before copying the data for DAX */ 745 if ((flags & IOMAP_DAX) && imap->br_state == XFS_EXT_UNWRITTEN) 746 return true; 747 return false; 748 } 749 750 static inline bool 751 imap_needs_cow( 752 struct xfs_inode *ip, 753 unsigned int flags, 754 struct xfs_bmbt_irec *imap, 755 int nimaps) 756 { 757 if (!xfs_is_cow_inode(ip)) 758 return false; 759 760 /* when zeroing we don't have to COW holes or unwritten extents */ 761 if (flags & (IOMAP_UNSHARE | IOMAP_ZERO)) { 762 if (!nimaps || 763 imap->br_startblock == HOLESTARTBLOCK || 764 imap->br_state == XFS_EXT_UNWRITTEN) 765 return false; 766 } 767 768 return true; 769 } 770 771 /* 772 * Extents not yet cached requires exclusive access, don't block for 773 * IOMAP_NOWAIT. 774 * 775 * This is basically an opencoded xfs_ilock_data_map_shared() call, but with 776 * support for IOMAP_NOWAIT. 777 */ 778 static int 779 xfs_ilock_for_iomap( 780 struct xfs_inode *ip, 781 unsigned flags, 782 unsigned *lockmode) 783 { 784 if (flags & IOMAP_NOWAIT) { 785 if (xfs_need_iread_extents(&ip->i_df)) 786 return -EAGAIN; 787 if (!xfs_ilock_nowait(ip, *lockmode)) 788 return -EAGAIN; 789 } else { 790 if (xfs_need_iread_extents(&ip->i_df)) 791 *lockmode = XFS_ILOCK_EXCL; 792 xfs_ilock(ip, *lockmode); 793 } 794 795 return 0; 796 } 797 798 /* 799 * Check that the imap we are going to return to the caller spans the entire 800 * range that the caller requested for the IO. 801 */ 802 static bool 803 imap_spans_range( 804 struct xfs_bmbt_irec *imap, 805 xfs_fileoff_t offset_fsb, 806 xfs_fileoff_t end_fsb) 807 { 808 if (imap->br_startoff > offset_fsb) 809 return false; 810 if (imap->br_startoff + imap->br_blockcount < end_fsb) 811 return false; 812 return true; 813 } 814 815 static bool 816 xfs_bmap_hw_atomic_write_possible( 817 struct xfs_inode *ip, 818 struct xfs_bmbt_irec *imap, 819 xfs_fileoff_t offset_fsb, 820 xfs_fileoff_t end_fsb) 821 { 822 struct xfs_mount *mp = ip->i_mount; 823 xfs_fsize_t len = XFS_FSB_TO_B(mp, end_fsb - offset_fsb); 824 825 /* 826 * atomic writes are required to be naturally aligned for disk blocks, 827 * which ensures that we adhere to block layer rules that we won't 828 * straddle any boundary or violate write alignment requirement. 829 */ 830 if (!IS_ALIGNED(imap->br_startblock, imap->br_blockcount)) 831 return false; 832 833 /* 834 * Spanning multiple extents would mean that multiple BIOs would be 835 * issued, and so would lose atomicity required for REQ_ATOMIC-based 836 * atomics. 837 */ 838 if (!imap_spans_range(imap, offset_fsb, end_fsb)) 839 return false; 840 841 /* 842 * The ->iomap_begin caller should ensure this, but check anyway. 843 */ 844 return len <= xfs_inode_buftarg(ip)->bt_awu_max; 845 } 846 847 static int 848 xfs_direct_write_iomap_begin( 849 struct inode *inode, 850 loff_t offset, 851 loff_t length, 852 unsigned flags, 853 struct iomap *iomap, 854 struct iomap *srcmap) 855 { 856 struct xfs_inode *ip = XFS_I(inode); 857 struct xfs_mount *mp = ip->i_mount; 858 struct xfs_bmbt_irec imap, cmap; 859 xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset); 860 xfs_fileoff_t end_fsb = xfs_iomap_end_fsb(mp, offset, length); 861 xfs_fileoff_t orig_end_fsb = end_fsb; 862 int nimaps = 1, error = 0; 863 bool shared = false; 864 u16 iomap_flags = 0; 865 bool needs_alloc; 866 unsigned int lockmode; 867 u64 seq; 868 869 ASSERT(flags & (IOMAP_WRITE | IOMAP_ZERO)); 870 871 if (xfs_is_shutdown(mp)) 872 return -EIO; 873 874 /* 875 * Writes that span EOF might trigger an IO size update on completion, 876 * so consider them to be dirty for the purposes of O_DSYNC even if 877 * there is no other metadata changes pending or have been made here. 878 */ 879 if (offset + length > i_size_read(inode)) 880 iomap_flags |= IOMAP_F_DIRTY; 881 882 /* HW-offload atomics are always used in this path */ 883 if (flags & IOMAP_ATOMIC) 884 iomap_flags |= IOMAP_F_ATOMIC_BIO; 885 886 /* 887 * COW writes may allocate delalloc space or convert unwritten COW 888 * extents, so we need to make sure to take the lock exclusively here. 889 */ 890 if (xfs_is_cow_inode(ip)) 891 lockmode = XFS_ILOCK_EXCL; 892 else 893 lockmode = XFS_ILOCK_SHARED; 894 895 relock: 896 error = xfs_ilock_for_iomap(ip, flags, &lockmode); 897 if (error) 898 return error; 899 900 /* 901 * The reflink iflag could have changed since the earlier unlocked 902 * check, check if it again and relock if needed. 903 */ 904 if (xfs_is_cow_inode(ip) && lockmode == XFS_ILOCK_SHARED) { 905 xfs_iunlock(ip, lockmode); 906 lockmode = XFS_ILOCK_EXCL; 907 goto relock; 908 } 909 910 error = xfs_bmapi_read(ip, offset_fsb, end_fsb - offset_fsb, &imap, 911 &nimaps, 0); 912 if (error) 913 goto out_unlock; 914 915 if (imap_needs_cow(ip, flags, &imap, nimaps)) { 916 error = -EAGAIN; 917 if (flags & IOMAP_NOWAIT) 918 goto out_unlock; 919 920 /* may drop and re-acquire the ilock */ 921 error = xfs_reflink_allocate_cow(ip, &imap, &cmap, &shared, 922 &lockmode, 923 (flags & IOMAP_DIRECT) || IS_DAX(inode)); 924 if (error) 925 goto out_unlock; 926 if (shared) { 927 if ((flags & IOMAP_ATOMIC) && 928 !xfs_bmap_hw_atomic_write_possible(ip, &cmap, 929 offset_fsb, end_fsb)) { 930 error = -ENOPROTOOPT; 931 goto out_unlock; 932 } 933 goto out_found_cow; 934 } 935 end_fsb = imap.br_startoff + imap.br_blockcount; 936 length = XFS_FSB_TO_B(mp, end_fsb) - offset; 937 } 938 939 needs_alloc = imap_needs_alloc(inode, flags, &imap, nimaps); 940 941 if (flags & IOMAP_ATOMIC) { 942 error = -ENOPROTOOPT; 943 /* 944 * If we allocate less than what is required for the write 945 * then we may end up with multiple extents, which means that 946 * REQ_ATOMIC-based cannot be used, so avoid this possibility. 947 */ 948 if (needs_alloc && orig_end_fsb - offset_fsb > 1) 949 goto out_unlock; 950 951 if (!xfs_bmap_hw_atomic_write_possible(ip, &imap, offset_fsb, 952 orig_end_fsb)) 953 goto out_unlock; 954 } 955 956 if (needs_alloc) 957 goto allocate_blocks; 958 959 /* 960 * NOWAIT and OVERWRITE I/O needs to span the entire requested I/O with 961 * a single map so that we avoid partial IO failures due to the rest of 962 * the I/O range not covered by this map triggering an EAGAIN condition 963 * when it is subsequently mapped and aborting the I/O. 964 */ 965 if (flags & (IOMAP_NOWAIT | IOMAP_OVERWRITE_ONLY)) { 966 error = -EAGAIN; 967 if (!imap_spans_range(&imap, offset_fsb, end_fsb)) 968 goto out_unlock; 969 } 970 971 /* 972 * For overwrite only I/O, we cannot convert unwritten extents without 973 * requiring sub-block zeroing. This can only be done under an 974 * exclusive IOLOCK, hence return -EAGAIN if this is not a written 975 * extent to tell the caller to try again. 976 */ 977 if (flags & IOMAP_OVERWRITE_ONLY) { 978 error = -EAGAIN; 979 if (imap.br_state != XFS_EXT_NORM && 980 ((offset | length) & mp->m_blockmask)) 981 goto out_unlock; 982 } 983 984 seq = xfs_iomap_inode_sequence(ip, iomap_flags); 985 xfs_iunlock(ip, lockmode); 986 trace_xfs_iomap_found(ip, offset, length, XFS_DATA_FORK, &imap); 987 return xfs_bmbt_to_iomap(ip, iomap, &imap, flags, iomap_flags, seq); 988 989 allocate_blocks: 990 error = -EAGAIN; 991 if (flags & (IOMAP_NOWAIT | IOMAP_OVERWRITE_ONLY)) 992 goto out_unlock; 993 994 /* 995 * We cap the maximum length we map to a sane size to keep the chunks 996 * of work done where somewhat symmetric with the work writeback does. 997 * This is a completely arbitrary number pulled out of thin air as a 998 * best guess for initial testing. 999 * 1000 * Note that the values needs to be less than 32-bits wide until the 1001 * lower level functions are updated. 1002 */ 1003 length = min_t(loff_t, length, 1024 * PAGE_SIZE); 1004 end_fsb = xfs_iomap_end_fsb(mp, offset, length); 1005 1006 if (offset + length > XFS_ISIZE(ip)) 1007 end_fsb = xfs_iomap_eof_align_last_fsb(ip, end_fsb); 1008 else if (nimaps && imap.br_startblock == HOLESTARTBLOCK) 1009 end_fsb = min(end_fsb, imap.br_startoff + imap.br_blockcount); 1010 xfs_iunlock(ip, lockmode); 1011 1012 error = xfs_iomap_write_direct(ip, offset_fsb, end_fsb - offset_fsb, 1013 flags, &imap, &seq); 1014 if (error) 1015 return error; 1016 1017 trace_xfs_iomap_alloc(ip, offset, length, XFS_DATA_FORK, &imap); 1018 return xfs_bmbt_to_iomap(ip, iomap, &imap, flags, 1019 iomap_flags | IOMAP_F_NEW, seq); 1020 1021 out_found_cow: 1022 length = XFS_FSB_TO_B(mp, cmap.br_startoff + cmap.br_blockcount); 1023 trace_xfs_iomap_found(ip, offset, length - offset, XFS_COW_FORK, &cmap); 1024 if (imap.br_startblock != HOLESTARTBLOCK) { 1025 seq = xfs_iomap_inode_sequence(ip, 0); 1026 error = xfs_bmbt_to_iomap(ip, srcmap, &imap, flags, 0, seq); 1027 if (error) 1028 goto out_unlock; 1029 } 1030 seq = xfs_iomap_inode_sequence(ip, IOMAP_F_SHARED); 1031 xfs_iunlock(ip, lockmode); 1032 return xfs_bmbt_to_iomap(ip, iomap, &cmap, flags, IOMAP_F_SHARED, seq); 1033 1034 out_unlock: 1035 if (lockmode) 1036 xfs_iunlock(ip, lockmode); 1037 return error; 1038 } 1039 1040 static DEFINE_IOMAP_ITER_NEXT(xfs_direct_write_iomap_next, 1041 xfs_direct_write_iomap_begin); 1042 1043 const struct iomap_ops xfs_direct_write_iomap_ops = { 1044 .iomap_next = xfs_direct_write_iomap_next, 1045 }; 1046 1047 #ifdef CONFIG_XFS_RT 1048 /* 1049 * This is really simple. The space has already been reserved before taking the 1050 * IOLOCK, the actual block allocation is done just before submitting the bio 1051 * and only recorded in the extent map on I/O completion. 1052 */ 1053 static int 1054 xfs_zoned_direct_write_iomap_begin( 1055 struct inode *inode, 1056 loff_t offset, 1057 loff_t length, 1058 unsigned flags, 1059 struct iomap *iomap, 1060 struct iomap *srcmap) 1061 { 1062 struct xfs_inode *ip = XFS_I(inode); 1063 int error; 1064 1065 ASSERT(!(flags & IOMAP_OVERWRITE_ONLY)); 1066 1067 /* 1068 * Needs to be pushed down into the allocator so that only writes into 1069 * a single zone can be supported. 1070 */ 1071 if (flags & IOMAP_NOWAIT) 1072 return -EAGAIN; 1073 1074 /* 1075 * Ensure the extent list is in memory in so that we don't have to do 1076 * read it from the I/O completion handler. 1077 */ 1078 if (xfs_need_iread_extents(&ip->i_df)) { 1079 xfs_ilock(ip, XFS_ILOCK_EXCL); 1080 error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK); 1081 xfs_iunlock(ip, XFS_ILOCK_EXCL); 1082 if (error) 1083 return error; 1084 } 1085 1086 xfs_iomap_set_anon_write(ip, iomap, offset, length); 1087 return 0; 1088 } 1089 1090 static DEFINE_IOMAP_ITER_NEXT(xfs_zoned_direct_write_iomap_next, 1091 xfs_zoned_direct_write_iomap_begin); 1092 1093 const struct iomap_ops xfs_zoned_direct_write_iomap_ops = { 1094 .iomap_next = xfs_zoned_direct_write_iomap_next, 1095 }; 1096 #endif /* CONFIG_XFS_RT */ 1097 1098 #ifdef DEBUG 1099 static void 1100 xfs_check_atomic_cow_conversion( 1101 struct xfs_inode *ip, 1102 xfs_fileoff_t offset_fsb, 1103 xfs_filblks_t count_fsb, 1104 const struct xfs_bmbt_irec *cmap) 1105 { 1106 struct xfs_iext_cursor icur; 1107 struct xfs_bmbt_irec cmap2 = { }; 1108 1109 if (xfs_iext_lookup_extent(ip, ip->i_cowfp, offset_fsb, &icur, &cmap2)) 1110 xfs_trim_extent(&cmap2, offset_fsb, count_fsb); 1111 1112 ASSERT(cmap2.br_startoff == cmap->br_startoff); 1113 ASSERT(cmap2.br_blockcount == cmap->br_blockcount); 1114 ASSERT(cmap2.br_startblock == cmap->br_startblock); 1115 ASSERT(cmap2.br_state == cmap->br_state); 1116 } 1117 #else 1118 # define xfs_check_atomic_cow_conversion(...) ((void)0) 1119 #endif 1120 1121 static int 1122 xfs_atomic_write_cow_iomap_begin( 1123 struct inode *inode, 1124 loff_t offset, 1125 loff_t length, 1126 unsigned flags, 1127 struct iomap *iomap, 1128 struct iomap *srcmap) 1129 { 1130 struct xfs_inode *ip = XFS_I(inode); 1131 struct xfs_mount *mp = ip->i_mount; 1132 const xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset); 1133 const xfs_fileoff_t end_fsb = XFS_B_TO_FSB(mp, offset + length); 1134 const xfs_filblks_t count_fsb = end_fsb - offset_fsb; 1135 xfs_filblks_t hole_count_fsb; 1136 int nmaps = 1; 1137 xfs_filblks_t resaligned; 1138 struct xfs_bmbt_irec cmap; 1139 struct xfs_iext_cursor icur; 1140 struct xfs_trans *tp; 1141 unsigned int dblocks = 0, rblocks = 0; 1142 int error; 1143 u64 seq; 1144 1145 ASSERT(flags & IOMAP_WRITE); 1146 ASSERT(flags & IOMAP_DIRECT); 1147 1148 if (xfs_is_shutdown(mp)) 1149 return -EIO; 1150 1151 if (!xfs_can_sw_atomic_write(mp)) { 1152 ASSERT(xfs_can_sw_atomic_write(mp)); 1153 return -EINVAL; 1154 } 1155 1156 /* blocks are always allocated in this path */ 1157 if (flags & IOMAP_NOWAIT) 1158 return -EAGAIN; 1159 1160 trace_xfs_iomap_atomic_write_cow(ip, offset, length); 1161 retry: 1162 xfs_ilock(ip, XFS_ILOCK_EXCL); 1163 1164 if (!ip->i_cowfp) { 1165 ASSERT(!xfs_is_reflink_inode(ip)); 1166 xfs_ifork_init_cow(ip); 1167 } 1168 1169 if (!xfs_iext_lookup_extent(ip, ip->i_cowfp, offset_fsb, &icur, &cmap)) 1170 cmap.br_startoff = end_fsb; 1171 if (cmap.br_startoff <= offset_fsb) { 1172 if (isnullstartblock(cmap.br_startblock)) 1173 goto convert_delay; 1174 1175 /* 1176 * cmap could extend outside the write range due to previous 1177 * speculative preallocations. We must trim cmap to the write 1178 * range because the cow fork treats written mappings to mean 1179 * "write in progress". 1180 */ 1181 xfs_trim_extent(&cmap, offset_fsb, count_fsb); 1182 goto found; 1183 } 1184 1185 hole_count_fsb = cmap.br_startoff - offset_fsb; 1186 1187 resaligned = xfs_aligned_fsb_count(offset_fsb, hole_count_fsb, 1188 xfs_get_cowextsz_hint(ip)); 1189 xfs_iunlock(ip, XFS_ILOCK_EXCL); 1190 1191 if (XFS_IS_REALTIME_INODE(ip)) { 1192 dblocks = XFS_DIOSTRAT_SPACE_RES(mp, 0); 1193 rblocks = resaligned; 1194 } else { 1195 dblocks = XFS_DIOSTRAT_SPACE_RES(mp, resaligned); 1196 rblocks = 0; 1197 } 1198 1199 error = xfs_trans_alloc_inode(ip, &M_RES(mp)->tr_write, dblocks, 1200 rblocks, false, &tp); 1201 if (error) 1202 return error; 1203 1204 /* extent layout could have changed since the unlock, so check again */ 1205 if (!xfs_iext_lookup_extent(ip, ip->i_cowfp, offset_fsb, &icur, &cmap)) 1206 cmap.br_startoff = end_fsb; 1207 if (cmap.br_startoff <= offset_fsb) { 1208 xfs_trans_cancel(tp); 1209 if (isnullstartblock(cmap.br_startblock)) 1210 goto convert_delay; 1211 xfs_trim_extent(&cmap, offset_fsb, count_fsb); 1212 goto found; 1213 } 1214 1215 /* 1216 * Allocate the entire reservation as unwritten blocks. 1217 * 1218 * Use XFS_BMAPI_EXTSZALIGN to hint at aligning new extents according to 1219 * extszhint, such that there will be a greater chance that future 1220 * atomic writes to that same range will be aligned (and don't require 1221 * this COW-based method). 1222 */ 1223 error = xfs_bmapi_write(tp, ip, offset_fsb, hole_count_fsb, 1224 XFS_BMAPI_COWFORK | XFS_BMAPI_PREALLOC | 1225 XFS_BMAPI_EXTSZALIGN, 0, &cmap, &nmaps); 1226 if (error) { 1227 xfs_trans_cancel(tp); 1228 goto out_unlock; 1229 } 1230 1231 xfs_inode_set_cowblocks_tag(ip); 1232 error = xfs_trans_commit(tp); 1233 if (error) 1234 goto out_unlock; 1235 1236 /* 1237 * cmap could map more blocks than the range we passed into bmapi_write 1238 * because of EXTSZALIGN or adjacent pre-existing unwritten mappings 1239 * that were merged. Trim cmap to the original write range so that we 1240 * don't convert more than we were asked to do for this write. 1241 */ 1242 xfs_trim_extent(&cmap, offset_fsb, count_fsb); 1243 1244 found: 1245 if (cmap.br_state != XFS_EXT_NORM) { 1246 error = xfs_reflink_convert_cow_locked(ip, cmap.br_startoff, 1247 cmap.br_blockcount); 1248 if (error) 1249 goto out_unlock; 1250 cmap.br_state = XFS_EXT_NORM; 1251 xfs_check_atomic_cow_conversion(ip, offset_fsb, count_fsb, 1252 &cmap); 1253 } 1254 1255 trace_xfs_iomap_found(ip, offset, length, XFS_COW_FORK, &cmap); 1256 seq = xfs_iomap_inode_sequence(ip, IOMAP_F_SHARED); 1257 xfs_iunlock(ip, XFS_ILOCK_EXCL); 1258 return xfs_bmbt_to_iomap(ip, iomap, &cmap, flags, IOMAP_F_SHARED, seq); 1259 1260 convert_delay: 1261 xfs_iunlock(ip, XFS_ILOCK_EXCL); 1262 error = xfs_bmapi_convert_delalloc(ip, XFS_COW_FORK, offset, iomap, 1263 NULL); 1264 if (error) 1265 return error; 1266 1267 /* 1268 * Try the lookup again, because the delalloc conversion might have 1269 * turned the COW mapping into unwritten, but we need it to be in 1270 * written state. 1271 */ 1272 goto retry; 1273 out_unlock: 1274 xfs_iunlock(ip, XFS_ILOCK_EXCL); 1275 return error; 1276 } 1277 1278 static DEFINE_IOMAP_ITER_NEXT(xfs_atomic_write_cow_iomap_next, 1279 xfs_atomic_write_cow_iomap_begin); 1280 1281 const struct iomap_ops xfs_atomic_write_cow_iomap_ops = { 1282 .iomap_next = xfs_atomic_write_cow_iomap_next, 1283 }; 1284 1285 static int 1286 xfs_dax_write_iomap_end( 1287 struct inode *inode, 1288 loff_t pos, 1289 loff_t length, 1290 ssize_t written, 1291 unsigned flags, 1292 struct iomap *iomap) 1293 { 1294 struct xfs_inode *ip = XFS_I(inode); 1295 1296 if (!xfs_is_cow_inode(ip)) 1297 return 0; 1298 1299 if (!written) 1300 return xfs_reflink_cancel_cow_range(ip, pos, length, true); 1301 1302 return xfs_reflink_end_cow(ip, pos, written); 1303 } 1304 1305 static DEFINE_IOMAP_ITER_NEXT_END(xfs_dax_write_iomap_next, 1306 xfs_direct_write_iomap_begin, xfs_dax_write_iomap_end); 1307 1308 const struct iomap_ops xfs_dax_write_iomap_ops = { 1309 .iomap_next = xfs_dax_write_iomap_next, 1310 }; 1311 1312 /* 1313 * Convert a hole to a delayed allocation. 1314 */ 1315 static void 1316 xfs_bmap_add_extent_hole_delay( 1317 struct xfs_inode *ip, /* incore inode pointer */ 1318 int whichfork, 1319 struct xfs_iext_cursor *icur, 1320 struct xfs_bmbt_irec *new) /* new data to add to file extents */ 1321 { 1322 struct xfs_ifork *ifp; /* inode fork pointer */ 1323 xfs_bmbt_irec_t left; /* left neighbor extent entry */ 1324 xfs_filblks_t newlen=0; /* new indirect size */ 1325 xfs_filblks_t oldlen=0; /* old indirect size */ 1326 xfs_bmbt_irec_t right; /* right neighbor extent entry */ 1327 uint32_t state = xfs_bmap_fork_to_state(whichfork); 1328 xfs_filblks_t temp; /* temp for indirect calculations */ 1329 1330 ifp = xfs_ifork_ptr(ip, whichfork); 1331 ASSERT(isnullstartblock(new->br_startblock)); 1332 1333 /* 1334 * Check and set flags if this segment has a left neighbor 1335 */ 1336 if (xfs_iext_peek_prev_extent(ifp, icur, &left)) { 1337 state |= BMAP_LEFT_VALID; 1338 if (isnullstartblock(left.br_startblock)) 1339 state |= BMAP_LEFT_DELAY; 1340 } 1341 1342 /* 1343 * Check and set flags if the current (right) segment exists. 1344 * If it doesn't exist, we're converting the hole at end-of-file. 1345 */ 1346 if (xfs_iext_get_extent(ifp, icur, &right)) { 1347 state |= BMAP_RIGHT_VALID; 1348 if (isnullstartblock(right.br_startblock)) 1349 state |= BMAP_RIGHT_DELAY; 1350 } 1351 1352 /* 1353 * Set contiguity flags on the left and right neighbors. 1354 * Don't let extents get too large, even if the pieces are contiguous. 1355 */ 1356 if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) && 1357 left.br_startoff + left.br_blockcount == new->br_startoff && 1358 left.br_blockcount + new->br_blockcount <= XFS_MAX_BMBT_EXTLEN) 1359 state |= BMAP_LEFT_CONTIG; 1360 1361 if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) && 1362 new->br_startoff + new->br_blockcount == right.br_startoff && 1363 new->br_blockcount + right.br_blockcount <= XFS_MAX_BMBT_EXTLEN && 1364 (!(state & BMAP_LEFT_CONTIG) || 1365 (left.br_blockcount + new->br_blockcount + 1366 right.br_blockcount <= XFS_MAX_BMBT_EXTLEN))) 1367 state |= BMAP_RIGHT_CONTIG; 1368 1369 /* 1370 * Switch out based on the contiguity flags. 1371 */ 1372 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) { 1373 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: 1374 /* 1375 * New allocation is contiguous with delayed allocations 1376 * on the left and on the right. 1377 * Merge all three into a single extent record. 1378 */ 1379 temp = left.br_blockcount + new->br_blockcount + 1380 right.br_blockcount; 1381 1382 oldlen = startblockval(left.br_startblock) + 1383 startblockval(new->br_startblock) + 1384 startblockval(right.br_startblock); 1385 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), 1386 oldlen); 1387 left.br_startblock = nullstartblock(newlen); 1388 left.br_blockcount = temp; 1389 1390 xfs_iext_remove(ip, icur, state); 1391 xfs_iext_prev(ifp, icur); 1392 xfs_iext_update_extent(ip, state, icur, &left); 1393 break; 1394 1395 case BMAP_LEFT_CONTIG: 1396 /* 1397 * New allocation is contiguous with a delayed allocation 1398 * on the left. 1399 * Merge the new allocation with the left neighbor. 1400 */ 1401 temp = left.br_blockcount + new->br_blockcount; 1402 1403 oldlen = startblockval(left.br_startblock) + 1404 startblockval(new->br_startblock); 1405 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), 1406 oldlen); 1407 left.br_blockcount = temp; 1408 left.br_startblock = nullstartblock(newlen); 1409 1410 xfs_iext_prev(ifp, icur); 1411 xfs_iext_update_extent(ip, state, icur, &left); 1412 break; 1413 1414 case BMAP_RIGHT_CONTIG: 1415 /* 1416 * New allocation is contiguous with a delayed allocation 1417 * on the right. 1418 * Merge the new allocation with the right neighbor. 1419 */ 1420 temp = new->br_blockcount + right.br_blockcount; 1421 oldlen = startblockval(new->br_startblock) + 1422 startblockval(right.br_startblock); 1423 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), 1424 oldlen); 1425 right.br_startoff = new->br_startoff; 1426 right.br_startblock = nullstartblock(newlen); 1427 right.br_blockcount = temp; 1428 xfs_iext_update_extent(ip, state, icur, &right); 1429 break; 1430 1431 case 0: 1432 /* 1433 * New allocation is not contiguous with another 1434 * delayed allocation. 1435 * Insert a new entry. 1436 */ 1437 oldlen = newlen = 0; 1438 xfs_iext_insert(ip, icur, new, state); 1439 break; 1440 } 1441 if (oldlen != newlen) { 1442 ASSERT(oldlen > newlen); 1443 xfs_add_fdblocks(ip->i_mount, oldlen - newlen); 1444 1445 /* 1446 * Nothing to do for disk quota accounting here. 1447 */ 1448 xfs_mod_delalloc(ip, 0, (int64_t)newlen - oldlen); 1449 } 1450 } 1451 1452 /* 1453 * Add a delayed allocation extent to an inode. Blocks are reserved from the 1454 * global pool and the extent inserted into the inode in-core extent tree. 1455 * 1456 * On entry, got refers to the first extent beyond the offset of the extent to 1457 * allocate or eof is specified if no such extent exists. On return, got refers 1458 * to the extent record that was inserted to the inode fork. 1459 * 1460 * Note that the allocated extent may have been merged with contiguous extents 1461 * during insertion into the inode fork. Thus, got does not reflect the current 1462 * state of the inode fork on return. If necessary, the caller can use lastx to 1463 * look up the updated record in the inode fork. 1464 */ 1465 static int 1466 xfs_bmapi_reserve_delalloc( 1467 struct xfs_inode *ip, 1468 int whichfork, 1469 xfs_fileoff_t off, 1470 xfs_filblks_t len, 1471 xfs_filblks_t prealloc, 1472 struct xfs_bmbt_irec *got, 1473 struct xfs_iext_cursor *icur, 1474 int eof) 1475 { 1476 struct xfs_mount *mp = ip->i_mount; 1477 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork); 1478 xfs_extlen_t alen; 1479 xfs_extlen_t indlen; 1480 uint64_t fdblocks; 1481 int error; 1482 xfs_fileoff_t aoff; 1483 bool use_cowextszhint = 1484 whichfork == XFS_COW_FORK && !prealloc; 1485 1486 retry: 1487 /* 1488 * Cap the alloc length. Keep track of prealloc so we know whether to 1489 * tag the inode before we return. 1490 */ 1491 aoff = off; 1492 alen = XFS_FILBLKS_MIN(len + prealloc, XFS_MAX_BMBT_EXTLEN); 1493 if (!eof) 1494 alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff); 1495 if (prealloc && alen >= len) 1496 prealloc = alen - len; 1497 1498 /* 1499 * If we're targetting the COW fork but aren't creating a speculative 1500 * posteof preallocation, try to expand the reservation to align with 1501 * the COW extent size hint if there's sufficient free space. 1502 * 1503 * Unlike the data fork, the CoW cancellation functions will free all 1504 * the reservations at inactivation, so we don't require that every 1505 * delalloc reservation have a dirty pagecache. 1506 */ 1507 if (use_cowextszhint) { 1508 struct xfs_bmbt_irec prev; 1509 xfs_extlen_t extsz = xfs_get_cowextsz_hint(ip); 1510 1511 if (!xfs_iext_peek_prev_extent(ifp, icur, &prev)) 1512 prev.br_startoff = NULLFILEOFF; 1513 1514 error = xfs_bmap_extsize_align(mp, got, &prev, extsz, 0, eof, 1515 1, 0, &aoff, &alen); 1516 ASSERT(!error); 1517 } 1518 1519 /* 1520 * Make a transaction-less quota reservation for delayed allocation 1521 * blocks. This number gets adjusted later. We return if we haven't 1522 * allocated blocks already inside this loop. 1523 */ 1524 error = xfs_quota_reserve_blkres(ip, alen); 1525 if (error) 1526 goto out; 1527 1528 /* 1529 * Split changing sb for alen and indlen since they could be coming 1530 * from different places. 1531 */ 1532 indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen); 1533 ASSERT(indlen > 0); 1534 1535 fdblocks = indlen; 1536 if (XFS_IS_REALTIME_INODE(ip)) { 1537 ASSERT(!xfs_is_zoned_inode(ip)); 1538 error = xfs_dec_frextents(mp, xfs_blen_to_rtbxlen(mp, alen)); 1539 if (error) 1540 goto out_unreserve_quota; 1541 } else { 1542 fdblocks += alen; 1543 } 1544 1545 error = xfs_dec_fdblocks(mp, fdblocks, false); 1546 if (error) 1547 goto out_unreserve_frextents; 1548 1549 ip->i_delayed_blks += alen; 1550 xfs_mod_delalloc(ip, alen, indlen); 1551 1552 got->br_startoff = aoff; 1553 got->br_startblock = nullstartblock(indlen); 1554 got->br_blockcount = alen; 1555 got->br_state = XFS_EXT_NORM; 1556 1557 xfs_bmap_add_extent_hole_delay(ip, whichfork, icur, got); 1558 1559 /* 1560 * Tag the inode if blocks were preallocated. Note that COW fork 1561 * preallocation can occur at the start or end of the extent, even when 1562 * prealloc == 0, so we must also check the aligned offset and length. 1563 */ 1564 if (whichfork == XFS_DATA_FORK && prealloc) 1565 xfs_inode_set_eofblocks_tag(ip); 1566 if (whichfork == XFS_COW_FORK && (prealloc || aoff < off || alen > len)) 1567 xfs_inode_set_cowblocks_tag(ip); 1568 1569 return 0; 1570 1571 out_unreserve_frextents: 1572 if (XFS_IS_REALTIME_INODE(ip)) 1573 xfs_add_frextents(mp, xfs_blen_to_rtbxlen(mp, alen)); 1574 out_unreserve_quota: 1575 if (XFS_IS_QUOTA_ON(mp)) 1576 xfs_quota_unreserve_blkres(ip, alen); 1577 out: 1578 if (error == -ENOSPC || error == -EDQUOT) { 1579 trace_xfs_delalloc_enospc(ip, off, len); 1580 1581 if (prealloc || use_cowextszhint) { 1582 /* retry without any preallocation */ 1583 use_cowextszhint = false; 1584 prealloc = 0; 1585 goto retry; 1586 } 1587 } 1588 return error; 1589 } 1590 1591 static int 1592 xfs_zoned_fill_srcmap( 1593 struct xfs_inode *ip, 1594 xfs_fileoff_t offset_fsb, 1595 xfs_fileoff_t *end_fsb, 1596 unsigned flags, 1597 struct iomap *srcmap) 1598 { 1599 struct xfs_bmbt_irec smap; 1600 struct xfs_iext_cursor scur; 1601 1602 if (!xfs_iext_lookup_extent(ip, &ip->i_df, offset_fsb, &scur, &smap)) 1603 smap.br_startoff = *end_fsb; /* fake hole until EOF */ 1604 if (smap.br_startoff > offset_fsb) { 1605 /* 1606 * There is a hole at @offset_fsb. Only map until the end of 1607 * the of it. 1608 */ 1609 *end_fsb = min(*end_fsb, smap.br_startoff); 1610 return 0; 1611 } 1612 1613 /* 1614 * There is a data fork mapping, only map until the end of it. 1615 */ 1616 xfs_trim_extent(&smap, offset_fsb, *end_fsb - offset_fsb); 1617 *end_fsb = min(*end_fsb, smap.br_startoff + smap.br_blockcount); 1618 return xfs_bmbt_to_iomap(ip, srcmap, &smap, flags, 0, 1619 xfs_iomap_inode_sequence(ip, 0)); 1620 } 1621 1622 static int 1623 xfs_zoned_buffered_write_iomap_begin( 1624 struct inode *inode, 1625 loff_t offset, 1626 loff_t count, 1627 unsigned flags, 1628 struct iomap *iomap, 1629 struct iomap *srcmap) 1630 { 1631 struct iomap_iter *iter = 1632 container_of(iomap, struct iomap_iter, iomap); 1633 struct address_space *mapping = inode->i_mapping; 1634 struct xfs_zone_alloc_ctx *ac = iter->private; 1635 struct xfs_inode *ip = XFS_I(inode); 1636 struct xfs_mount *mp = ip->i_mount; 1637 xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset); 1638 xfs_fileoff_t end_fsb = xfs_iomap_end_fsb(mp, offset, count); 1639 u16 iomap_flags = IOMAP_F_SHARED; 1640 unsigned int lockmode = XFS_ILOCK_EXCL; 1641 xfs_filblks_t count_fsb; 1642 xfs_extlen_t indlen; 1643 struct xfs_bmbt_irec got; 1644 struct xfs_iext_cursor icur; 1645 int error = 0; 1646 1647 ASSERT(!xfs_get_extsz_hint(ip)); 1648 ASSERT(!(flags & IOMAP_UNSHARE)); 1649 ASSERT(ac); 1650 1651 if (xfs_is_shutdown(mp)) 1652 return -EIO; 1653 1654 error = xfs_qm_dqattach(ip); 1655 if (error) 1656 return error; 1657 1658 restart: 1659 error = xfs_ilock_for_iomap(ip, flags, &lockmode); 1660 if (error) 1661 return error; 1662 1663 if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(&ip->i_df)) || 1664 XFS_TEST_ERROR(mp, XFS_ERRTAG_BMAPIFORMAT)) { 1665 xfs_bmap_mark_sick(ip, XFS_DATA_FORK); 1666 error = -EFSCORRUPTED; 1667 goto out_unlock; 1668 } 1669 1670 XFS_STATS_INC(mp, xs_blk_mapw); 1671 1672 error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK); 1673 if (error) 1674 goto out_unlock; 1675 1676 /* 1677 * For zeroing operations check if there is any data to zero first. 1678 * 1679 * For regular writes we always need to allocate new blocks, but need to 1680 * provide the source mapping when the range is unaligned to support 1681 * read-modify-write of the whole block in the page cache. 1682 * 1683 * In either case we need to limit the reported range to the boundaries 1684 * of the source map in the data fork. 1685 */ 1686 if (!IS_ALIGNED(offset, mp->m_sb.sb_blocksize) || 1687 !IS_ALIGNED(offset + count, mp->m_sb.sb_blocksize) || 1688 (flags & IOMAP_ZERO)) { 1689 error = xfs_zoned_fill_srcmap(ip, offset_fsb, &end_fsb, flags, 1690 srcmap); 1691 if (error) 1692 goto out_unlock; 1693 } 1694 1695 if (!ip->i_cowfp) 1696 xfs_ifork_init_cow(ip); 1697 1698 if (!xfs_iext_lookup_extent(ip, ip->i_cowfp, offset_fsb, &icur, &got)) 1699 got.br_startoff = end_fsb; 1700 if (got.br_startoff <= offset_fsb) { 1701 trace_xfs_reflink_cow_found(ip, &got); 1702 goto done; 1703 } 1704 1705 /* 1706 * Cap the maximum length to keep the chunks of work done here somewhat 1707 * symmetric with the work writeback does. 1708 */ 1709 end_fsb = min(end_fsb, got.br_startoff); 1710 count_fsb = min3(end_fsb - offset_fsb, XFS_MAX_BMBT_EXTLEN, 1711 XFS_B_TO_FSB(mp, 1024 * PAGE_SIZE)); 1712 1713 /* 1714 * When zeroing, don't allocate blocks for holes as they are already 1715 * zeroes, but we need to ensure that no extents exist in both the data 1716 * and COW fork to ensure this really is a hole. 1717 * 1718 * A window exists where we might observe a hole in both forks with 1719 * valid data in cache. Writeback removes the COW fork blocks on 1720 * submission but doesn't remap into the data fork until completion. If 1721 * the data fork was previously a hole, we'll fail to zero. Until we 1722 * find a way to avoid this transient state, check for dirty pagecache 1723 * and flush to wait on blocks to land in the data fork. 1724 */ 1725 if ((flags & IOMAP_ZERO) && srcmap->type == IOMAP_HOLE) { 1726 if (filemap_range_needs_writeback(mapping, offset, 1727 offset + count - 1)) { 1728 xfs_iunlock(ip, lockmode); 1729 error = filemap_write_and_wait_range(mapping, offset, 1730 offset + count - 1); 1731 if (error) 1732 return error; 1733 goto restart; 1734 } 1735 1736 xfs_hole_to_iomap(ip, iomap, offset_fsb, end_fsb); 1737 goto out_unlock; 1738 } 1739 1740 /* 1741 * The block reservation is supposed to cover all blocks that the 1742 * operation could possible write, but there is a nasty corner case 1743 * where blocks could be stolen from underneath us: 1744 * 1745 * 1) while this thread iterates over a larger buffered write, 1746 * 2) another thread is causing a write fault that calls into 1747 * ->page_mkwrite in range this thread writes to, using up the 1748 * delalloc reservation created by a previous call to this function. 1749 * 3) another thread does direct I/O on the range that the write fault 1750 * happened on, which causes writeback of the dirty data. 1751 * 4) this then set the stale flag, which cuts the current iomap 1752 * iteration short, causing the new call to ->iomap_begin that gets 1753 * us here again, but now without a sufficient reservation. 1754 * 1755 * This is a very unusual I/O pattern, and nothing but generic/095 is 1756 * known to hit it. There's not really much we can do here, so turn this 1757 * into a short write. 1758 */ 1759 if (count_fsb > ac->reserved_blocks) { 1760 xfs_warn_ratelimited(mp, 1761 "Short write on ino 0x%llx comm %.20s due to three-way race with write fault and direct I/O", 1762 I_INO(ip), current->comm); 1763 count_fsb = ac->reserved_blocks; 1764 if (!count_fsb) { 1765 error = -EIO; 1766 goto out_unlock; 1767 } 1768 } 1769 1770 error = xfs_quota_reserve_blkres(ip, count_fsb); 1771 if (error) 1772 goto out_unlock; 1773 1774 indlen = xfs_bmap_worst_indlen(ip, count_fsb); 1775 error = xfs_dec_fdblocks(mp, indlen, false); 1776 if (error) 1777 goto out_unlock; 1778 ip->i_delayed_blks += count_fsb; 1779 xfs_mod_delalloc(ip, count_fsb, indlen); 1780 1781 got.br_startoff = offset_fsb; 1782 got.br_startblock = nullstartblock(indlen); 1783 got.br_blockcount = count_fsb; 1784 got.br_state = XFS_EXT_NORM; 1785 xfs_bmap_add_extent_hole_delay(ip, XFS_COW_FORK, &icur, &got); 1786 ac->reserved_blocks -= count_fsb; 1787 iomap_flags |= IOMAP_F_NEW; 1788 1789 trace_xfs_iomap_alloc(ip, offset, XFS_FSB_TO_B(mp, count_fsb), 1790 XFS_COW_FORK, &got); 1791 done: 1792 error = xfs_bmbt_to_iomap(ip, iomap, &got, flags, iomap_flags, 1793 xfs_iomap_inode_sequence(ip, IOMAP_F_SHARED)); 1794 out_unlock: 1795 xfs_iunlock(ip, lockmode); 1796 return error; 1797 } 1798 1799 static int 1800 xfs_buffered_write_iomap_begin( 1801 struct inode *inode, 1802 loff_t offset, 1803 loff_t count, 1804 unsigned flags, 1805 struct iomap *iomap, 1806 struct iomap *srcmap) 1807 { 1808 struct iomap_iter *iter = container_of(iomap, struct iomap_iter, 1809 iomap); 1810 struct xfs_inode *ip = XFS_I(inode); 1811 struct xfs_mount *mp = ip->i_mount; 1812 xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset); 1813 xfs_fileoff_t end_fsb = xfs_iomap_end_fsb(mp, offset, count); 1814 xfs_fileoff_t cow_fsb = NULLFILEOFF; 1815 xfs_fileoff_t eof_fsb = XFS_B_TO_FSB(mp, XFS_ISIZE(ip)); 1816 struct xfs_bmbt_irec imap, cmap; 1817 struct xfs_iext_cursor icur, ccur; 1818 xfs_fsblock_t prealloc_blocks = 0; 1819 bool eof = false, cow_eof = false, shared = false; 1820 int allocfork = XFS_DATA_FORK; 1821 int error = 0; 1822 unsigned int lockmode = XFS_ILOCK_EXCL; 1823 unsigned int iomap_flags = 0; 1824 u64 seq; 1825 1826 if (xfs_is_shutdown(mp)) 1827 return -EIO; 1828 1829 if (xfs_is_zoned_inode(ip)) 1830 return xfs_zoned_buffered_write_iomap_begin(inode, offset, 1831 count, flags, iomap, srcmap); 1832 1833 /* we can't use delayed allocations when using extent size hints */ 1834 if (xfs_get_extsz_hint(ip)) 1835 return xfs_direct_write_iomap_begin(inode, offset, count, 1836 flags, iomap, srcmap); 1837 1838 error = xfs_qm_dqattach(ip); 1839 if (error) 1840 return error; 1841 1842 error = xfs_ilock_for_iomap(ip, flags, &lockmode); 1843 if (error) 1844 return error; 1845 1846 if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(&ip->i_df)) || 1847 XFS_TEST_ERROR(mp, XFS_ERRTAG_BMAPIFORMAT)) { 1848 xfs_bmap_mark_sick(ip, XFS_DATA_FORK); 1849 error = -EFSCORRUPTED; 1850 goto out_unlock; 1851 } 1852 1853 XFS_STATS_INC(mp, xs_blk_mapw); 1854 1855 error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK); 1856 if (error) 1857 goto out_unlock; 1858 1859 /* 1860 * Search the data fork first to look up our source mapping. We always 1861 * need the data fork map, as we have to return it to the iomap code so 1862 * that the higher level write code can read data in to perform 1863 * read-modify-write cycles for unaligned writes. 1864 * 1865 * Then search the COW fork extent list even if we did not find a data 1866 * fork extent. This serves two purposes: first this implements the 1867 * speculative preallocation using cowextsize, so that we also unshare 1868 * block adjacent to shared blocks instead of just the shared blocks 1869 * themselves. Second the lookup in the extent list is generally faster 1870 * than going out to the shared extent tree. 1871 */ 1872 eof = !xfs_iext_lookup_extent(ip, &ip->i_df, offset_fsb, &icur, &imap); 1873 if (eof) 1874 imap.br_startoff = end_fsb; /* fake hole until the end */ 1875 if (xfs_is_cow_inode(ip)) { 1876 if (!ip->i_cowfp) { 1877 ASSERT(!xfs_is_reflink_inode(ip)); 1878 xfs_ifork_init_cow(ip); 1879 } 1880 cow_eof = !xfs_iext_lookup_extent(ip, ip->i_cowfp, offset_fsb, 1881 &ccur, &cmap); 1882 if (!cow_eof) 1883 cow_fsb = cmap.br_startoff; 1884 } 1885 1886 /* We never need to allocate blocks for unsharing a hole. */ 1887 if ((flags & IOMAP_UNSHARE) && imap.br_startoff > offset_fsb) { 1888 xfs_hole_to_iomap(ip, iomap, offset_fsb, imap.br_startoff); 1889 goto out_unlock; 1890 } 1891 1892 /* 1893 * We may need to zero over a hole in the data fork if it's fronted by 1894 * COW blocks and dirty pagecache. Scan such file ranges for dirty 1895 * cache and fill the iomap batch with folios that need zeroing. 1896 */ 1897 if ((flags & IOMAP_ZERO) && imap.br_startoff > offset_fsb) { 1898 loff_t start, end; 1899 unsigned int fbatch_count; 1900 1901 imap.br_blockcount = imap.br_startoff - offset_fsb; 1902 imap.br_startoff = offset_fsb; 1903 imap.br_startblock = HOLESTARTBLOCK; 1904 imap.br_state = XFS_EXT_NORM; 1905 1906 if (cow_fsb == NULLFILEOFF) 1907 goto found_imap; 1908 if (cow_fsb > offset_fsb) { 1909 xfs_trim_extent(&imap, offset_fsb, 1910 cow_fsb - offset_fsb); 1911 goto found_imap; 1912 } 1913 1914 /* no zeroing beyond eof, so split at the boundary */ 1915 if (offset_fsb >= eof_fsb) 1916 goto found_imap; 1917 if (offset_fsb < eof_fsb && end_fsb > eof_fsb) 1918 xfs_trim_extent(&imap, offset_fsb, 1919 eof_fsb - offset_fsb); 1920 1921 /* COW fork blocks overlap the hole */ 1922 xfs_trim_extent(&imap, offset_fsb, 1923 cmap.br_startoff + cmap.br_blockcount - offset_fsb); 1924 start = XFS_FSB_TO_B(mp, imap.br_startoff); 1925 end = XFS_FSB_TO_B(mp, imap.br_startoff + imap.br_blockcount); 1926 fbatch_count = iomap_fill_dirty_folios(iter, &start, end, 1927 &iomap_flags); 1928 xfs_trim_extent(&imap, offset_fsb, 1929 XFS_B_TO_FSB(mp, start) - offset_fsb); 1930 1931 /* 1932 * Report the COW mapping if we have folios to zero. Otherwise 1933 * ignore the COW blocks as preallocation and report a hole. 1934 */ 1935 if (fbatch_count) { 1936 xfs_trim_extent(&cmap, imap.br_startoff, 1937 imap.br_blockcount); 1938 imap.br_startoff = end_fsb; /* fake hole */ 1939 goto found_cow; 1940 } 1941 goto found_imap; 1942 } 1943 1944 /* 1945 * For zeroing, trim extents that extend beyond the EOF block. If a 1946 * delalloc extent starts beyond the EOF block, convert it to an 1947 * unwritten extent. 1948 */ 1949 if (flags & IOMAP_ZERO) { 1950 if (isnullstartblock(imap.br_startblock) && 1951 offset_fsb >= eof_fsb) 1952 goto convert_delay; 1953 if (offset_fsb < eof_fsb && end_fsb > eof_fsb) 1954 end_fsb = eof_fsb; 1955 1956 /* 1957 * Look up dirty folios for unwritten mappings within EOF. 1958 * Providing this bypasses the flush iomap uses to trigger 1959 * extent conversion when unwritten mappings have dirty 1960 * pagecache in need of zeroing. 1961 * 1962 * Trim the mapping to the end pos of the lookup, which in turn 1963 * was trimmed to the end of the batch if it became full before 1964 * the end of the mapping. 1965 */ 1966 if (imap.br_state == XFS_EXT_UNWRITTEN && 1967 offset_fsb < eof_fsb) { 1968 loff_t foffset = offset, fend; 1969 1970 fend = offset + 1971 min(count, XFS_FSB_TO_B(mp, imap.br_blockcount)); 1972 iomap_fill_dirty_folios(iter, &foffset, fend, 1973 &iomap_flags); 1974 end_fsb = min_t(xfs_fileoff_t, end_fsb, 1975 XFS_B_TO_FSB(mp, foffset)); 1976 } 1977 1978 xfs_trim_extent(&imap, offset_fsb, end_fsb - offset_fsb); 1979 } 1980 1981 /* 1982 * Now that we've handled any operation specific special cases, at this 1983 * point we can report a COW mapping if found. 1984 */ 1985 if (xfs_is_cow_inode(ip) && 1986 !cow_eof && cmap.br_startoff <= offset_fsb) { 1987 trace_xfs_reflink_cow_found(ip, &cmap); 1988 goto found_cow; 1989 } 1990 1991 if (imap.br_startoff <= offset_fsb) { 1992 /* 1993 * For reflink files we may need a delalloc reservation when 1994 * overwriting shared extents. This includes zeroing of 1995 * existing extents that contain data. 1996 */ 1997 if (!xfs_is_cow_inode(ip) || 1998 ((flags & IOMAP_ZERO) && imap.br_state != XFS_EXT_NORM)) { 1999 trace_xfs_iomap_found(ip, offset, count, XFS_DATA_FORK, 2000 &imap); 2001 goto found_imap; 2002 } 2003 2004 xfs_trim_extent(&imap, offset_fsb, end_fsb - offset_fsb); 2005 2006 /* Trim the mapping to the nearest shared extent boundary. */ 2007 error = xfs_bmap_trim_cow(ip, &imap, &shared); 2008 if (error) 2009 goto out_unlock; 2010 2011 /* Not shared? Just report the (potentially capped) extent. */ 2012 if (!shared) { 2013 trace_xfs_iomap_found(ip, offset, count, XFS_DATA_FORK, 2014 &imap); 2015 goto found_imap; 2016 } 2017 2018 /* 2019 * Fork all the shared blocks from our write offset until the 2020 * end of the extent. 2021 */ 2022 allocfork = XFS_COW_FORK; 2023 end_fsb = imap.br_startoff + imap.br_blockcount; 2024 } else { 2025 /* 2026 * We cap the maximum length we map here to MAX_WRITEBACK_PAGES 2027 * pages to keep the chunks of work done where somewhat 2028 * symmetric with the work writeback does. This is a completely 2029 * arbitrary number pulled out of thin air. 2030 * 2031 * Note that the values needs to be less than 32-bits wide until 2032 * the lower level functions are updated. 2033 */ 2034 count = min_t(loff_t, count, 1024 * PAGE_SIZE); 2035 end_fsb = xfs_iomap_end_fsb(mp, offset, count); 2036 2037 if (xfs_is_always_cow_inode(ip)) 2038 allocfork = XFS_COW_FORK; 2039 } 2040 2041 if (eof && offset + count > XFS_ISIZE(ip)) { 2042 /* 2043 * Determine the initial size of the preallocation. 2044 * We clean up any extra preallocation when the file is closed. 2045 */ 2046 if (xfs_has_allocsize(mp)) 2047 prealloc_blocks = mp->m_allocsize_blocks; 2048 else if (allocfork == XFS_DATA_FORK) 2049 prealloc_blocks = xfs_iomap_prealloc_size(ip, allocfork, 2050 offset, count, &icur); 2051 else 2052 prealloc_blocks = xfs_iomap_prealloc_size(ip, allocfork, 2053 offset, count, &ccur); 2054 if (prealloc_blocks) { 2055 xfs_extlen_t align; 2056 xfs_off_t end_offset; 2057 xfs_fileoff_t p_end_fsb; 2058 2059 end_offset = XFS_ALLOC_ALIGN(mp, offset + count - 1); 2060 p_end_fsb = XFS_B_TO_FSBT(mp, end_offset) + 2061 prealloc_blocks; 2062 2063 align = xfs_eof_alignment(ip); 2064 if (align) 2065 p_end_fsb = roundup_64(p_end_fsb, align); 2066 2067 p_end_fsb = min(p_end_fsb, 2068 XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes)); 2069 ASSERT(p_end_fsb > offset_fsb); 2070 prealloc_blocks = p_end_fsb - end_fsb; 2071 } 2072 } 2073 2074 /* 2075 * Flag newly allocated delalloc blocks with IOMAP_F_NEW so we punch 2076 * them out if the write happens to fail. 2077 */ 2078 iomap_flags |= IOMAP_F_NEW; 2079 if (allocfork == XFS_COW_FORK) { 2080 error = xfs_bmapi_reserve_delalloc(ip, allocfork, offset_fsb, 2081 end_fsb - offset_fsb, prealloc_blocks, &cmap, 2082 &ccur, cow_eof); 2083 if (error) 2084 goto out_unlock; 2085 2086 trace_xfs_iomap_alloc(ip, offset, count, allocfork, &cmap); 2087 goto found_cow; 2088 } 2089 2090 error = xfs_bmapi_reserve_delalloc(ip, allocfork, offset_fsb, 2091 end_fsb - offset_fsb, prealloc_blocks, &imap, &icur, 2092 eof); 2093 if (error) 2094 goto out_unlock; 2095 2096 trace_xfs_iomap_alloc(ip, offset, count, allocfork, &imap); 2097 found_imap: 2098 seq = xfs_iomap_inode_sequence(ip, iomap_flags); 2099 xfs_iunlock(ip, lockmode); 2100 return xfs_bmbt_to_iomap(ip, iomap, &imap, flags, iomap_flags, seq); 2101 2102 convert_delay: 2103 xfs_iunlock(ip, lockmode); 2104 truncate_pagecache(inode, offset); 2105 error = xfs_bmapi_convert_delalloc(ip, XFS_DATA_FORK, offset, 2106 iomap, NULL); 2107 if (error) 2108 return error; 2109 2110 trace_xfs_iomap_alloc(ip, offset, count, XFS_DATA_FORK, &imap); 2111 return 0; 2112 2113 found_cow: 2114 if (imap.br_startoff <= offset_fsb) { 2115 error = xfs_bmbt_to_iomap(ip, srcmap, &imap, flags, 0, 2116 xfs_iomap_inode_sequence(ip, 0)); 2117 if (error) 2118 goto out_unlock; 2119 } else { 2120 xfs_trim_extent(&cmap, offset_fsb, 2121 imap.br_startoff - offset_fsb); 2122 } 2123 2124 iomap_flags |= IOMAP_F_SHARED; 2125 seq = xfs_iomap_inode_sequence(ip, iomap_flags); 2126 xfs_iunlock(ip, lockmode); 2127 return xfs_bmbt_to_iomap(ip, iomap, &cmap, flags, iomap_flags, seq); 2128 2129 out_unlock: 2130 xfs_iunlock(ip, lockmode); 2131 return error; 2132 } 2133 2134 static void 2135 xfs_buffered_write_delalloc_punch( 2136 struct inode *inode, 2137 loff_t offset, 2138 loff_t length, 2139 struct iomap *iomap) 2140 { 2141 struct iomap_iter *iter = 2142 container_of(iomap, struct iomap_iter, iomap); 2143 2144 xfs_bmap_punch_delalloc_range(XFS_I(inode), 2145 (iomap->flags & IOMAP_F_SHARED) ? 2146 XFS_COW_FORK : XFS_DATA_FORK, 2147 offset, offset + length, iter->private); 2148 } 2149 2150 static int 2151 xfs_buffered_write_iomap_end( 2152 struct inode *inode, 2153 loff_t offset, 2154 loff_t length, 2155 ssize_t written, 2156 unsigned flags, 2157 struct iomap *iomap) 2158 { 2159 loff_t start_byte, end_byte; 2160 2161 /* If we didn't reserve the blocks, we're not allowed to punch them. */ 2162 if (iomap->type != IOMAP_DELALLOC || !(iomap->flags & IOMAP_F_NEW)) 2163 return 0; 2164 2165 /* 2166 * iomap_page_mkwrite() will never fail in a way that requires delalloc 2167 * extents that it allocated to be revoked. Hence never try to release 2168 * them here. 2169 */ 2170 if (flags & IOMAP_FAULT) 2171 return 0; 2172 2173 /* Nothing to do if we've written the entire delalloc extent */ 2174 start_byte = iomap_last_written_block(inode, offset, written); 2175 end_byte = round_up(offset + length, i_blocksize(inode)); 2176 if (start_byte >= end_byte) 2177 return 0; 2178 2179 /* For zeroing operations the callers already hold invalidate_lock. */ 2180 if (flags & (IOMAP_UNSHARE | IOMAP_ZERO)) { 2181 rwsem_assert_held_write(&inode->i_mapping->invalidate_lock); 2182 iomap_write_delalloc_release(inode, start_byte, end_byte, flags, 2183 iomap, xfs_buffered_write_delalloc_punch); 2184 } else { 2185 filemap_invalidate_lock(inode->i_mapping); 2186 iomap_write_delalloc_release(inode, start_byte, end_byte, flags, 2187 iomap, xfs_buffered_write_delalloc_punch); 2188 filemap_invalidate_unlock(inode->i_mapping); 2189 } 2190 2191 return 0; 2192 } 2193 2194 static DEFINE_IOMAP_ITER_NEXT_END(xfs_buffered_write_iomap_next, 2195 xfs_buffered_write_iomap_begin, xfs_buffered_write_iomap_end); 2196 2197 const struct iomap_ops xfs_buffered_write_iomap_ops = { 2198 .iomap_next = xfs_buffered_write_iomap_next, 2199 }; 2200 2201 int 2202 xfs_read_iomap_begin( 2203 struct inode *inode, 2204 loff_t offset, 2205 loff_t length, 2206 unsigned flags, 2207 struct iomap *iomap, 2208 struct iomap *srcmap) 2209 { 2210 struct xfs_inode *ip = XFS_I(inode); 2211 struct xfs_mount *mp = ip->i_mount; 2212 struct xfs_bmbt_irec imap; 2213 xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset); 2214 xfs_fileoff_t end_fsb = xfs_iomap_end_fsb(mp, offset, length); 2215 int nimaps = 1, error = 0; 2216 bool shared = false; 2217 unsigned int lockmode = XFS_ILOCK_SHARED; 2218 u64 seq; 2219 2220 ASSERT(!(flags & (IOMAP_WRITE | IOMAP_ZERO))); 2221 2222 if (xfs_is_shutdown(mp)) 2223 return -EIO; 2224 2225 error = xfs_ilock_for_iomap(ip, flags, &lockmode); 2226 if (error) 2227 return error; 2228 error = xfs_bmapi_read(ip, offset_fsb, end_fsb - offset_fsb, &imap, 2229 &nimaps, 0); 2230 if (!error && ((flags & IOMAP_REPORT) || IS_DAX(inode))) 2231 error = xfs_reflink_trim_around_shared(ip, &imap, &shared); 2232 seq = xfs_iomap_inode_sequence(ip, shared ? IOMAP_F_SHARED : 0); 2233 xfs_iunlock(ip, lockmode); 2234 2235 if (error) 2236 return error; 2237 trace_xfs_iomap_found(ip, offset, length, XFS_DATA_FORK, &imap); 2238 return xfs_bmbt_to_iomap(ip, iomap, &imap, flags, 2239 shared ? IOMAP_F_SHARED : 0, seq); 2240 } 2241 2242 static DEFINE_IOMAP_ITER_NEXT(xfs_read_iomap_next, xfs_read_iomap_begin); 2243 2244 const struct iomap_ops xfs_read_iomap_ops = { 2245 .iomap_next = xfs_read_iomap_next, 2246 }; 2247 2248 static int 2249 xfs_seek_iomap_begin( 2250 struct inode *inode, 2251 loff_t offset, 2252 loff_t length, 2253 unsigned flags, 2254 struct iomap *iomap, 2255 struct iomap *srcmap) 2256 { 2257 struct xfs_inode *ip = XFS_I(inode); 2258 struct xfs_mount *mp = ip->i_mount; 2259 xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset); 2260 xfs_fileoff_t end_fsb = XFS_B_TO_FSB(mp, offset + length); 2261 xfs_fileoff_t cow_fsb = NULLFILEOFF, data_fsb = NULLFILEOFF; 2262 struct xfs_iext_cursor icur; 2263 struct xfs_bmbt_irec imap, cmap; 2264 int error = 0; 2265 unsigned lockmode; 2266 u64 seq; 2267 2268 if (xfs_is_shutdown(mp)) 2269 return -EIO; 2270 2271 lockmode = xfs_ilock_data_map_shared(ip); 2272 error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK); 2273 if (error) 2274 goto out_unlock; 2275 2276 if (xfs_iext_lookup_extent(ip, &ip->i_df, offset_fsb, &icur, &imap)) { 2277 /* 2278 * If we found a data extent we are done. 2279 */ 2280 if (imap.br_startoff <= offset_fsb) 2281 goto done; 2282 data_fsb = imap.br_startoff; 2283 } else { 2284 /* 2285 * Fake a hole until the end of the file. 2286 */ 2287 data_fsb = xfs_iomap_end_fsb(mp, offset, length); 2288 } 2289 2290 /* 2291 * If a COW fork extent covers the hole, report it - capped to the next 2292 * data fork extent: 2293 */ 2294 if (xfs_inode_has_cow_data(ip) && 2295 xfs_iext_lookup_extent(ip, ip->i_cowfp, offset_fsb, &icur, &cmap)) 2296 cow_fsb = cmap.br_startoff; 2297 if (cow_fsb != NULLFILEOFF && cow_fsb <= offset_fsb) { 2298 if (data_fsb < cow_fsb + cmap.br_blockcount) 2299 end_fsb = min(end_fsb, data_fsb); 2300 xfs_trim_extent(&cmap, offset_fsb, end_fsb - offset_fsb); 2301 seq = xfs_iomap_inode_sequence(ip, IOMAP_F_SHARED); 2302 error = xfs_bmbt_to_iomap(ip, iomap, &cmap, flags, 2303 IOMAP_F_SHARED, seq); 2304 /* 2305 * This is a COW extent, so we must probe the page cache 2306 * because there could be dirty page cache being backed 2307 * by this extent. 2308 */ 2309 iomap->type = IOMAP_UNWRITTEN; 2310 goto out_unlock; 2311 } 2312 2313 /* 2314 * Else report a hole, capped to the next found data or COW extent. 2315 */ 2316 if (cow_fsb != NULLFILEOFF && cow_fsb < data_fsb) 2317 imap.br_blockcount = cow_fsb - offset_fsb; 2318 else 2319 imap.br_blockcount = data_fsb - offset_fsb; 2320 imap.br_startoff = offset_fsb; 2321 imap.br_startblock = HOLESTARTBLOCK; 2322 imap.br_state = XFS_EXT_NORM; 2323 done: 2324 seq = xfs_iomap_inode_sequence(ip, 0); 2325 xfs_trim_extent(&imap, offset_fsb, end_fsb - offset_fsb); 2326 error = xfs_bmbt_to_iomap(ip, iomap, &imap, flags, 0, seq); 2327 out_unlock: 2328 xfs_iunlock(ip, lockmode); 2329 return error; 2330 } 2331 2332 static DEFINE_IOMAP_ITER_NEXT(xfs_seek_iomap_next, xfs_seek_iomap_begin); 2333 2334 const struct iomap_ops xfs_seek_iomap_ops = { 2335 .iomap_next = xfs_seek_iomap_next, 2336 }; 2337 2338 static int 2339 xfs_xattr_iomap_begin( 2340 struct inode *inode, 2341 loff_t offset, 2342 loff_t length, 2343 unsigned flags, 2344 struct iomap *iomap, 2345 struct iomap *srcmap) 2346 { 2347 struct xfs_inode *ip = XFS_I(inode); 2348 struct xfs_mount *mp = ip->i_mount; 2349 xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset); 2350 xfs_fileoff_t end_fsb = XFS_B_TO_FSB(mp, offset + length); 2351 struct xfs_bmbt_irec imap; 2352 int nimaps = 1, error = 0; 2353 unsigned lockmode; 2354 int seq; 2355 2356 if (xfs_is_shutdown(mp)) 2357 return -EIO; 2358 2359 lockmode = xfs_ilock_attr_map_shared(ip); 2360 2361 /* if there are no attribute fork or extents, return ENOENT */ 2362 if (!xfs_inode_has_attr_fork(ip) || !ip->i_af.if_nextents) { 2363 error = -ENOENT; 2364 goto out_unlock; 2365 } 2366 2367 ASSERT(ip->i_af.if_format != XFS_DINODE_FMT_LOCAL); 2368 error = xfs_bmapi_read(ip, offset_fsb, end_fsb - offset_fsb, &imap, 2369 &nimaps, XFS_BMAPI_ATTRFORK); 2370 out_unlock: 2371 2372 seq = xfs_iomap_inode_sequence(ip, IOMAP_F_XATTR); 2373 xfs_iunlock(ip, lockmode); 2374 2375 if (error) 2376 return error; 2377 ASSERT(nimaps); 2378 return xfs_bmbt_to_iomap(ip, iomap, &imap, flags, IOMAP_F_XATTR, seq); 2379 } 2380 2381 static DEFINE_IOMAP_ITER_NEXT(xfs_xattr_iomap_next, xfs_xattr_iomap_begin); 2382 2383 const struct iomap_ops xfs_xattr_iomap_ops = { 2384 .iomap_next = xfs_xattr_iomap_next, 2385 }; 2386 2387 int 2388 xfs_zero_range( 2389 struct xfs_inode *ip, 2390 loff_t pos, 2391 loff_t len, 2392 struct xfs_zone_alloc_ctx *ac, 2393 bool *did_zero) 2394 { 2395 struct inode *inode = VFS_I(ip); 2396 2397 xfs_assert_ilocked(ip, XFS_IOLOCK_EXCL | XFS_MMAPLOCK_EXCL); 2398 2399 if (IS_DAX(inode)) 2400 return dax_zero_range(inode, pos, len, did_zero, 2401 &xfs_dax_write_iomap_ops); 2402 return iomap_zero_range(inode, pos, len, did_zero, 2403 &xfs_buffered_write_iomap_ops, &xfs_iomap_write_ops, 2404 ac); 2405 } 2406 2407 int 2408 xfs_truncate_page( 2409 struct xfs_inode *ip, 2410 loff_t pos, 2411 struct xfs_zone_alloc_ctx *ac, 2412 bool *did_zero) 2413 { 2414 struct inode *inode = VFS_I(ip); 2415 2416 if (IS_DAX(inode)) 2417 return dax_truncate_page(inode, pos, did_zero, 2418 &xfs_dax_write_iomap_ops); 2419 return iomap_truncate_page(inode, pos, did_zero, 2420 &xfs_buffered_write_iomap_ops, &xfs_iomap_write_ops, 2421 ac); 2422 } 2423