0001
0002
0003
0004
0005
0006
0007 #include "xfs.h"
0008 #include "xfs_fs.h"
0009 #include "xfs_shared.h"
0010 #include "xfs_format.h"
0011 #include "xfs_log_format.h"
0012 #include "xfs_trans_resv.h"
0013 #include "xfs_mount.h"
0014 #include "xfs_inode.h"
0015 #include "xfs_btree.h"
0016 #include "xfs_bmap_btree.h"
0017 #include "xfs_bmap.h"
0018 #include "xfs_bmap_util.h"
0019 #include "xfs_errortag.h"
0020 #include "xfs_error.h"
0021 #include "xfs_trans.h"
0022 #include "xfs_trans_space.h"
0023 #include "xfs_inode_item.h"
0024 #include "xfs_iomap.h"
0025 #include "xfs_trace.h"
0026 #include "xfs_quota.h"
0027 #include "xfs_dquot_item.h"
0028 #include "xfs_dquot.h"
0029 #include "xfs_reflink.h"
0030
0031 #define XFS_ALLOC_ALIGN(mp, off) \
0032 (((off) >> mp->m_allocsize_log) << mp->m_allocsize_log)
0033
0034 static int
0035 xfs_alert_fsblock_zero(
0036 xfs_inode_t *ip,
0037 xfs_bmbt_irec_t *imap)
0038 {
0039 xfs_alert_tag(ip->i_mount, XFS_PTAG_FSBLOCK_ZERO,
0040 "Access to block zero in inode %llu "
0041 "start_block: %llx start_off: %llx "
0042 "blkcnt: %llx extent-state: %x",
0043 (unsigned long long)ip->i_ino,
0044 (unsigned long long)imap->br_startblock,
0045 (unsigned long long)imap->br_startoff,
0046 (unsigned long long)imap->br_blockcount,
0047 imap->br_state);
0048 return -EFSCORRUPTED;
0049 }
0050
0051 int
0052 xfs_bmbt_to_iomap(
0053 struct xfs_inode *ip,
0054 struct iomap *iomap,
0055 struct xfs_bmbt_irec *imap,
0056 unsigned int mapping_flags,
0057 u16 iomap_flags)
0058 {
0059 struct xfs_mount *mp = ip->i_mount;
0060 struct xfs_buftarg *target = xfs_inode_buftarg(ip);
0061
0062 if (unlikely(!xfs_valid_startblock(ip, imap->br_startblock)))
0063 return xfs_alert_fsblock_zero(ip, imap);
0064
0065 if (imap->br_startblock == HOLESTARTBLOCK) {
0066 iomap->addr = IOMAP_NULL_ADDR;
0067 iomap->type = IOMAP_HOLE;
0068 } else if (imap->br_startblock == DELAYSTARTBLOCK ||
0069 isnullstartblock(imap->br_startblock)) {
0070 iomap->addr = IOMAP_NULL_ADDR;
0071 iomap->type = IOMAP_DELALLOC;
0072 } else {
0073 iomap->addr = BBTOB(xfs_fsb_to_db(ip, imap->br_startblock));
0074 if (mapping_flags & IOMAP_DAX)
0075 iomap->addr += target->bt_dax_part_off;
0076
0077 if (imap->br_state == XFS_EXT_UNWRITTEN)
0078 iomap->type = IOMAP_UNWRITTEN;
0079 else
0080 iomap->type = IOMAP_MAPPED;
0081
0082 }
0083 iomap->offset = XFS_FSB_TO_B(mp, imap->br_startoff);
0084 iomap->length = XFS_FSB_TO_B(mp, imap->br_blockcount);
0085 if (mapping_flags & IOMAP_DAX)
0086 iomap->dax_dev = target->bt_daxdev;
0087 else
0088 iomap->bdev = target->bt_bdev;
0089 iomap->flags = iomap_flags;
0090
0091 if (xfs_ipincount(ip) &&
0092 (ip->i_itemp->ili_fsync_fields & ~XFS_ILOG_TIMESTAMP))
0093 iomap->flags |= IOMAP_F_DIRTY;
0094 return 0;
0095 }
0096
0097 static void
0098 xfs_hole_to_iomap(
0099 struct xfs_inode *ip,
0100 struct iomap *iomap,
0101 xfs_fileoff_t offset_fsb,
0102 xfs_fileoff_t end_fsb)
0103 {
0104 struct xfs_buftarg *target = xfs_inode_buftarg(ip);
0105
0106 iomap->addr = IOMAP_NULL_ADDR;
0107 iomap->type = IOMAP_HOLE;
0108 iomap->offset = XFS_FSB_TO_B(ip->i_mount, offset_fsb);
0109 iomap->length = XFS_FSB_TO_B(ip->i_mount, end_fsb - offset_fsb);
0110 iomap->bdev = target->bt_bdev;
0111 iomap->dax_dev = target->bt_daxdev;
0112 }
0113
0114 static inline xfs_fileoff_t
0115 xfs_iomap_end_fsb(
0116 struct xfs_mount *mp,
0117 loff_t offset,
0118 loff_t count)
0119 {
0120 ASSERT(offset <= mp->m_super->s_maxbytes);
0121 return min(XFS_B_TO_FSB(mp, offset + count),
0122 XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes));
0123 }
0124
0125 static xfs_extlen_t
0126 xfs_eof_alignment(
0127 struct xfs_inode *ip)
0128 {
0129 struct xfs_mount *mp = ip->i_mount;
0130 xfs_extlen_t align = 0;
0131
0132 if (!XFS_IS_REALTIME_INODE(ip)) {
0133
0134
0135
0136
0137
0138
0139
0140
0141 if (mp->m_swidth && xfs_has_swalloc(mp))
0142 align = mp->m_swidth;
0143 else if (mp->m_dalign)
0144 align = mp->m_dalign;
0145
0146 if (align && XFS_ISIZE(ip) < XFS_FSB_TO_B(mp, align))
0147 align = 0;
0148 }
0149
0150 return align;
0151 }
0152
0153
0154
0155
0156
0157 xfs_fileoff_t
0158 xfs_iomap_eof_align_last_fsb(
0159 struct xfs_inode *ip,
0160 xfs_fileoff_t end_fsb)
0161 {
0162 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, XFS_DATA_FORK);
0163 xfs_extlen_t extsz = xfs_get_extsz_hint(ip);
0164 xfs_extlen_t align = xfs_eof_alignment(ip);
0165 struct xfs_bmbt_irec irec;
0166 struct xfs_iext_cursor icur;
0167
0168 ASSERT(!xfs_need_iread_extents(ifp));
0169
0170
0171
0172
0173 if (extsz) {
0174 if (align)
0175 align = roundup_64(align, extsz);
0176 else
0177 align = extsz;
0178 }
0179
0180 if (align) {
0181 xfs_fileoff_t aligned_end_fsb = roundup_64(end_fsb, align);
0182
0183 xfs_iext_last(ifp, &icur);
0184 if (!xfs_iext_get_extent(ifp, &icur, &irec) ||
0185 aligned_end_fsb >= irec.br_startoff + irec.br_blockcount)
0186 return aligned_end_fsb;
0187 }
0188
0189 return end_fsb;
0190 }
0191
0192 int
0193 xfs_iomap_write_direct(
0194 struct xfs_inode *ip,
0195 xfs_fileoff_t offset_fsb,
0196 xfs_fileoff_t count_fsb,
0197 unsigned int flags,
0198 struct xfs_bmbt_irec *imap)
0199 {
0200 struct xfs_mount *mp = ip->i_mount;
0201 struct xfs_trans *tp;
0202 xfs_filblks_t resaligned;
0203 int nimaps;
0204 unsigned int dblocks, rblocks;
0205 bool force = false;
0206 int error;
0207 int bmapi_flags = XFS_BMAPI_PREALLOC;
0208 int nr_exts = XFS_IEXT_ADD_NOSPLIT_CNT;
0209
0210 ASSERT(count_fsb > 0);
0211
0212 resaligned = xfs_aligned_fsb_count(offset_fsb, count_fsb,
0213 xfs_get_extsz_hint(ip));
0214 if (unlikely(XFS_IS_REALTIME_INODE(ip))) {
0215 dblocks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
0216 rblocks = resaligned;
0217 } else {
0218 dblocks = XFS_DIOSTRAT_SPACE_RES(mp, resaligned);
0219 rblocks = 0;
0220 }
0221
0222 error = xfs_qm_dqattach(ip);
0223 if (error)
0224 return error;
0225
0226
0227
0228
0229
0230
0231
0232
0233
0234
0235
0236
0237
0238
0239 if (flags & IOMAP_DAX) {
0240 bmapi_flags = XFS_BMAPI_CONVERT | XFS_BMAPI_ZERO;
0241 if (imap->br_state == XFS_EXT_UNWRITTEN) {
0242 force = true;
0243 nr_exts = XFS_IEXT_WRITE_UNWRITTEN_CNT;
0244 dblocks = XFS_DIOSTRAT_SPACE_RES(mp, 0) << 1;
0245 }
0246 }
0247
0248 error = xfs_trans_alloc_inode(ip, &M_RES(mp)->tr_write, dblocks,
0249 rblocks, force, &tp);
0250 if (error)
0251 return error;
0252
0253 error = xfs_iext_count_may_overflow(ip, XFS_DATA_FORK, nr_exts);
0254 if (error == -EFBIG)
0255 error = xfs_iext_count_upgrade(tp, ip, nr_exts);
0256 if (error)
0257 goto out_trans_cancel;
0258
0259
0260
0261
0262
0263 nimaps = 1;
0264 error = xfs_bmapi_write(tp, ip, offset_fsb, count_fsb, bmapi_flags, 0,
0265 imap, &nimaps);
0266 if (error)
0267 goto out_trans_cancel;
0268
0269
0270
0271
0272 error = xfs_trans_commit(tp);
0273 if (error)
0274 goto out_unlock;
0275
0276
0277
0278
0279 if (nimaps == 0) {
0280 error = -ENOSPC;
0281 goto out_unlock;
0282 }
0283
0284 if (unlikely(!xfs_valid_startblock(ip, imap->br_startblock)))
0285 error = xfs_alert_fsblock_zero(ip, imap);
0286
0287 out_unlock:
0288 xfs_iunlock(ip, XFS_ILOCK_EXCL);
0289 return error;
0290
0291 out_trans_cancel:
0292 xfs_trans_cancel(tp);
0293 goto out_unlock;
0294 }
0295
0296 STATIC bool
0297 xfs_quota_need_throttle(
0298 struct xfs_inode *ip,
0299 xfs_dqtype_t type,
0300 xfs_fsblock_t alloc_blocks)
0301 {
0302 struct xfs_dquot *dq = xfs_inode_dquot(ip, type);
0303
0304 if (!dq || !xfs_this_quota_on(ip->i_mount, type))
0305 return false;
0306
0307
0308 if (!dq->q_prealloc_hi_wmark)
0309 return false;
0310
0311
0312 if (dq->q_blk.reserved + alloc_blocks < dq->q_prealloc_lo_wmark)
0313 return false;
0314
0315 return true;
0316 }
0317
0318 STATIC void
0319 xfs_quota_calc_throttle(
0320 struct xfs_inode *ip,
0321 xfs_dqtype_t type,
0322 xfs_fsblock_t *qblocks,
0323 int *qshift,
0324 int64_t *qfreesp)
0325 {
0326 struct xfs_dquot *dq = xfs_inode_dquot(ip, type);
0327 int64_t freesp;
0328 int shift = 0;
0329
0330
0331 if (!dq || dq->q_blk.reserved >= dq->q_prealloc_hi_wmark) {
0332 *qblocks = 0;
0333 *qfreesp = 0;
0334 return;
0335 }
0336
0337 freesp = dq->q_prealloc_hi_wmark - dq->q_blk.reserved;
0338 if (freesp < dq->q_low_space[XFS_QLOWSP_5_PCNT]) {
0339 shift = 2;
0340 if (freesp < dq->q_low_space[XFS_QLOWSP_3_PCNT])
0341 shift += 2;
0342 if (freesp < dq->q_low_space[XFS_QLOWSP_1_PCNT])
0343 shift += 2;
0344 }
0345
0346 if (freesp < *qfreesp)
0347 *qfreesp = freesp;
0348
0349
0350 if ((freesp >> shift) < (*qblocks >> *qshift)) {
0351 *qblocks = freesp;
0352 *qshift = shift;
0353 }
0354 }
0355
0356
0357
0358
0359
0360
0361
0362 STATIC xfs_fsblock_t
0363 xfs_iomap_prealloc_size(
0364 struct xfs_inode *ip,
0365 int whichfork,
0366 loff_t offset,
0367 loff_t count,
0368 struct xfs_iext_cursor *icur)
0369 {
0370 struct xfs_iext_cursor ncur = *icur;
0371 struct xfs_bmbt_irec prev, got;
0372 struct xfs_mount *mp = ip->i_mount;
0373 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
0374 xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
0375 int64_t freesp;
0376 xfs_fsblock_t qblocks;
0377 xfs_fsblock_t alloc_blocks = 0;
0378 xfs_extlen_t plen;
0379 int shift = 0;
0380 int qshift = 0;
0381
0382
0383
0384
0385
0386
0387
0388 if (XFS_ISIZE(ip) < XFS_FSB_TO_B(mp, mp->m_allocsize_blocks))
0389 return 0;
0390
0391
0392
0393
0394
0395 if (XFS_ISIZE(ip) < XFS_FSB_TO_B(mp, mp->m_dalign) ||
0396 !xfs_iext_prev_extent(ifp, &ncur, &prev) ||
0397 prev.br_startoff + prev.br_blockcount < offset_fsb)
0398 return mp->m_allocsize_blocks;
0399
0400
0401
0402
0403
0404
0405 plen = prev.br_blockcount;
0406 while (xfs_iext_prev_extent(ifp, &ncur, &got)) {
0407 if (plen > XFS_MAX_BMBT_EXTLEN / 2 ||
0408 isnullstartblock(got.br_startblock) ||
0409 got.br_startoff + got.br_blockcount != prev.br_startoff ||
0410 got.br_startblock + got.br_blockcount != prev.br_startblock)
0411 break;
0412 plen += got.br_blockcount;
0413 prev = got;
0414 }
0415
0416
0417
0418
0419
0420
0421
0422
0423 alloc_blocks = plen * 2;
0424 if (alloc_blocks > XFS_MAX_BMBT_EXTLEN)
0425 alloc_blocks = XFS_B_TO_FSB(mp, offset);
0426 qblocks = alloc_blocks;
0427
0428
0429
0430
0431
0432
0433
0434
0435 alloc_blocks = XFS_FILEOFF_MIN(roundup_pow_of_two(XFS_MAX_BMBT_EXTLEN),
0436 alloc_blocks);
0437
0438 freesp = percpu_counter_read_positive(&mp->m_fdblocks);
0439 if (freesp < mp->m_low_space[XFS_LOWSP_5_PCNT]) {
0440 shift = 2;
0441 if (freesp < mp->m_low_space[XFS_LOWSP_4_PCNT])
0442 shift++;
0443 if (freesp < mp->m_low_space[XFS_LOWSP_3_PCNT])
0444 shift++;
0445 if (freesp < mp->m_low_space[XFS_LOWSP_2_PCNT])
0446 shift++;
0447 if (freesp < mp->m_low_space[XFS_LOWSP_1_PCNT])
0448 shift++;
0449 }
0450
0451
0452
0453
0454
0455 if (xfs_quota_need_throttle(ip, XFS_DQTYPE_USER, alloc_blocks))
0456 xfs_quota_calc_throttle(ip, XFS_DQTYPE_USER, &qblocks, &qshift,
0457 &freesp);
0458 if (xfs_quota_need_throttle(ip, XFS_DQTYPE_GROUP, alloc_blocks))
0459 xfs_quota_calc_throttle(ip, XFS_DQTYPE_GROUP, &qblocks, &qshift,
0460 &freesp);
0461 if (xfs_quota_need_throttle(ip, XFS_DQTYPE_PROJ, alloc_blocks))
0462 xfs_quota_calc_throttle(ip, XFS_DQTYPE_PROJ, &qblocks, &qshift,
0463 &freesp);
0464
0465
0466
0467
0468
0469
0470
0471
0472 alloc_blocks = min(alloc_blocks, qblocks);
0473 shift = max(shift, qshift);
0474
0475 if (shift)
0476 alloc_blocks >>= shift;
0477
0478
0479
0480
0481 if (alloc_blocks)
0482 alloc_blocks = rounddown_pow_of_two(alloc_blocks);
0483 if (alloc_blocks > XFS_MAX_BMBT_EXTLEN)
0484 alloc_blocks = XFS_MAX_BMBT_EXTLEN;
0485
0486
0487
0488
0489
0490
0491
0492 while (alloc_blocks && alloc_blocks >= freesp)
0493 alloc_blocks >>= 4;
0494 if (alloc_blocks < mp->m_allocsize_blocks)
0495 alloc_blocks = mp->m_allocsize_blocks;
0496 trace_xfs_iomap_prealloc_size(ip, alloc_blocks, shift,
0497 mp->m_allocsize_blocks);
0498 return alloc_blocks;
0499 }
0500
0501 int
0502 xfs_iomap_write_unwritten(
0503 xfs_inode_t *ip,
0504 xfs_off_t offset,
0505 xfs_off_t count,
0506 bool update_isize)
0507 {
0508 xfs_mount_t *mp = ip->i_mount;
0509 xfs_fileoff_t offset_fsb;
0510 xfs_filblks_t count_fsb;
0511 xfs_filblks_t numblks_fsb;
0512 int nimaps;
0513 xfs_trans_t *tp;
0514 xfs_bmbt_irec_t imap;
0515 struct inode *inode = VFS_I(ip);
0516 xfs_fsize_t i_size;
0517 uint resblks;
0518 int error;
0519
0520 trace_xfs_unwritten_convert(ip, offset, count);
0521
0522 offset_fsb = XFS_B_TO_FSBT(mp, offset);
0523 count_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + count);
0524 count_fsb = (xfs_filblks_t)(count_fsb - offset_fsb);
0525
0526
0527
0528
0529
0530
0531
0532
0533
0534
0535
0536 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0) << 1;
0537
0538
0539 error = xfs_qm_dqattach(ip);
0540 if (error)
0541 return error;
0542
0543 do {
0544
0545
0546
0547
0548
0549
0550
0551
0552
0553 error = xfs_trans_alloc_inode(ip, &M_RES(mp)->tr_write, resblks,
0554 0, true, &tp);
0555 if (error)
0556 return error;
0557
0558 error = xfs_iext_count_may_overflow(ip, XFS_DATA_FORK,
0559 XFS_IEXT_WRITE_UNWRITTEN_CNT);
0560 if (error == -EFBIG)
0561 error = xfs_iext_count_upgrade(tp, ip,
0562 XFS_IEXT_WRITE_UNWRITTEN_CNT);
0563 if (error)
0564 goto error_on_bmapi_transaction;
0565
0566
0567
0568
0569 nimaps = 1;
0570 error = xfs_bmapi_write(tp, ip, offset_fsb, count_fsb,
0571 XFS_BMAPI_CONVERT, resblks, &imap,
0572 &nimaps);
0573 if (error)
0574 goto error_on_bmapi_transaction;
0575
0576
0577
0578
0579
0580
0581 i_size = XFS_FSB_TO_B(mp, offset_fsb + count_fsb);
0582 if (i_size > offset + count)
0583 i_size = offset + count;
0584 if (update_isize && i_size > i_size_read(inode))
0585 i_size_write(inode, i_size);
0586 i_size = xfs_new_eof(ip, i_size);
0587 if (i_size) {
0588 ip->i_disk_size = i_size;
0589 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
0590 }
0591
0592 error = xfs_trans_commit(tp);
0593 xfs_iunlock(ip, XFS_ILOCK_EXCL);
0594 if (error)
0595 return error;
0596
0597 if (unlikely(!xfs_valid_startblock(ip, imap.br_startblock)))
0598 return xfs_alert_fsblock_zero(ip, &imap);
0599
0600 if ((numblks_fsb = imap.br_blockcount) == 0) {
0601
0602
0603
0604
0605 ASSERT(imap.br_blockcount);
0606 break;
0607 }
0608 offset_fsb += numblks_fsb;
0609 count_fsb -= numblks_fsb;
0610 } while (count_fsb > 0);
0611
0612 return 0;
0613
0614 error_on_bmapi_transaction:
0615 xfs_trans_cancel(tp);
0616 xfs_iunlock(ip, XFS_ILOCK_EXCL);
0617 return error;
0618 }
0619
0620 static inline bool
0621 imap_needs_alloc(
0622 struct inode *inode,
0623 unsigned flags,
0624 struct xfs_bmbt_irec *imap,
0625 int nimaps)
0626 {
0627
0628 if (flags & IOMAP_ZERO)
0629 return false;
0630 if (!nimaps ||
0631 imap->br_startblock == HOLESTARTBLOCK ||
0632 imap->br_startblock == DELAYSTARTBLOCK)
0633 return true;
0634
0635 if ((flags & IOMAP_DAX) && imap->br_state == XFS_EXT_UNWRITTEN)
0636 return true;
0637 return false;
0638 }
0639
0640 static inline bool
0641 imap_needs_cow(
0642 struct xfs_inode *ip,
0643 unsigned int flags,
0644 struct xfs_bmbt_irec *imap,
0645 int nimaps)
0646 {
0647 if (!xfs_is_cow_inode(ip))
0648 return false;
0649
0650
0651 if (flags & IOMAP_ZERO) {
0652 if (!nimaps ||
0653 imap->br_startblock == HOLESTARTBLOCK ||
0654 imap->br_state == XFS_EXT_UNWRITTEN)
0655 return false;
0656 }
0657
0658 return true;
0659 }
0660
0661 static int
0662 xfs_ilock_for_iomap(
0663 struct xfs_inode *ip,
0664 unsigned flags,
0665 unsigned *lockmode)
0666 {
0667 unsigned int mode = *lockmode;
0668 bool is_write = flags & (IOMAP_WRITE | IOMAP_ZERO);
0669
0670
0671
0672
0673
0674 if (xfs_is_cow_inode(ip) && is_write)
0675 mode = XFS_ILOCK_EXCL;
0676
0677
0678
0679
0680
0681
0682 if (xfs_need_iread_extents(&ip->i_df)) {
0683 if (flags & IOMAP_NOWAIT)
0684 return -EAGAIN;
0685 mode = XFS_ILOCK_EXCL;
0686 }
0687
0688 relock:
0689 if (flags & IOMAP_NOWAIT) {
0690 if (!xfs_ilock_nowait(ip, mode))
0691 return -EAGAIN;
0692 } else {
0693 xfs_ilock(ip, mode);
0694 }
0695
0696
0697
0698
0699
0700
0701 if (mode == XFS_ILOCK_SHARED && is_write && xfs_is_cow_inode(ip)) {
0702 xfs_iunlock(ip, mode);
0703 mode = XFS_ILOCK_EXCL;
0704 goto relock;
0705 }
0706
0707 *lockmode = mode;
0708 return 0;
0709 }
0710
0711
0712
0713
0714
0715 static bool
0716 imap_spans_range(
0717 struct xfs_bmbt_irec *imap,
0718 xfs_fileoff_t offset_fsb,
0719 xfs_fileoff_t end_fsb)
0720 {
0721 if (imap->br_startoff > offset_fsb)
0722 return false;
0723 if (imap->br_startoff + imap->br_blockcount < end_fsb)
0724 return false;
0725 return true;
0726 }
0727
0728 static int
0729 xfs_direct_write_iomap_begin(
0730 struct inode *inode,
0731 loff_t offset,
0732 loff_t length,
0733 unsigned flags,
0734 struct iomap *iomap,
0735 struct iomap *srcmap)
0736 {
0737 struct xfs_inode *ip = XFS_I(inode);
0738 struct xfs_mount *mp = ip->i_mount;
0739 struct xfs_bmbt_irec imap, cmap;
0740 xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
0741 xfs_fileoff_t end_fsb = xfs_iomap_end_fsb(mp, offset, length);
0742 int nimaps = 1, error = 0;
0743 bool shared = false;
0744 u16 iomap_flags = 0;
0745 unsigned int lockmode = XFS_ILOCK_SHARED;
0746
0747 ASSERT(flags & (IOMAP_WRITE | IOMAP_ZERO));
0748
0749 if (xfs_is_shutdown(mp))
0750 return -EIO;
0751
0752
0753
0754
0755
0756
0757 if (offset + length > i_size_read(inode))
0758 iomap_flags |= IOMAP_F_DIRTY;
0759
0760 error = xfs_ilock_for_iomap(ip, flags, &lockmode);
0761 if (error)
0762 return error;
0763
0764 error = xfs_bmapi_read(ip, offset_fsb, end_fsb - offset_fsb, &imap,
0765 &nimaps, 0);
0766 if (error)
0767 goto out_unlock;
0768
0769 if (imap_needs_cow(ip, flags, &imap, nimaps)) {
0770 error = -EAGAIN;
0771 if (flags & IOMAP_NOWAIT)
0772 goto out_unlock;
0773
0774
0775 error = xfs_reflink_allocate_cow(ip, &imap, &cmap, &shared,
0776 &lockmode,
0777 (flags & IOMAP_DIRECT) || IS_DAX(inode));
0778 if (error)
0779 goto out_unlock;
0780 if (shared)
0781 goto out_found_cow;
0782 end_fsb = imap.br_startoff + imap.br_blockcount;
0783 length = XFS_FSB_TO_B(mp, end_fsb) - offset;
0784 }
0785
0786 if (imap_needs_alloc(inode, flags, &imap, nimaps))
0787 goto allocate_blocks;
0788
0789
0790
0791
0792
0793
0794
0795 if (flags & (IOMAP_NOWAIT | IOMAP_OVERWRITE_ONLY)) {
0796 error = -EAGAIN;
0797 if (!imap_spans_range(&imap, offset_fsb, end_fsb))
0798 goto out_unlock;
0799 }
0800
0801
0802
0803
0804
0805
0806
0807 if (flags & IOMAP_OVERWRITE_ONLY) {
0808 error = -EAGAIN;
0809 if (imap.br_state != XFS_EXT_NORM &&
0810 ((offset | length) & mp->m_blockmask))
0811 goto out_unlock;
0812 }
0813
0814 xfs_iunlock(ip, lockmode);
0815 trace_xfs_iomap_found(ip, offset, length, XFS_DATA_FORK, &imap);
0816 return xfs_bmbt_to_iomap(ip, iomap, &imap, flags, iomap_flags);
0817
0818 allocate_blocks:
0819 error = -EAGAIN;
0820 if (flags & (IOMAP_NOWAIT | IOMAP_OVERWRITE_ONLY))
0821 goto out_unlock;
0822
0823
0824
0825
0826
0827
0828
0829
0830
0831
0832 length = min_t(loff_t, length, 1024 * PAGE_SIZE);
0833 end_fsb = xfs_iomap_end_fsb(mp, offset, length);
0834
0835 if (offset + length > XFS_ISIZE(ip))
0836 end_fsb = xfs_iomap_eof_align_last_fsb(ip, end_fsb);
0837 else if (nimaps && imap.br_startblock == HOLESTARTBLOCK)
0838 end_fsb = min(end_fsb, imap.br_startoff + imap.br_blockcount);
0839 xfs_iunlock(ip, lockmode);
0840
0841 error = xfs_iomap_write_direct(ip, offset_fsb, end_fsb - offset_fsb,
0842 flags, &imap);
0843 if (error)
0844 return error;
0845
0846 trace_xfs_iomap_alloc(ip, offset, length, XFS_DATA_FORK, &imap);
0847 return xfs_bmbt_to_iomap(ip, iomap, &imap, flags,
0848 iomap_flags | IOMAP_F_NEW);
0849
0850 out_found_cow:
0851 xfs_iunlock(ip, lockmode);
0852 length = XFS_FSB_TO_B(mp, cmap.br_startoff + cmap.br_blockcount);
0853 trace_xfs_iomap_found(ip, offset, length - offset, XFS_COW_FORK, &cmap);
0854 if (imap.br_startblock != HOLESTARTBLOCK) {
0855 error = xfs_bmbt_to_iomap(ip, srcmap, &imap, flags, 0);
0856 if (error)
0857 return error;
0858 }
0859 return xfs_bmbt_to_iomap(ip, iomap, &cmap, flags, IOMAP_F_SHARED);
0860
0861 out_unlock:
0862 if (lockmode)
0863 xfs_iunlock(ip, lockmode);
0864 return error;
0865 }
0866
0867 const struct iomap_ops xfs_direct_write_iomap_ops = {
0868 .iomap_begin = xfs_direct_write_iomap_begin,
0869 };
0870
0871 static int
0872 xfs_dax_write_iomap_end(
0873 struct inode *inode,
0874 loff_t pos,
0875 loff_t length,
0876 ssize_t written,
0877 unsigned flags,
0878 struct iomap *iomap)
0879 {
0880 struct xfs_inode *ip = XFS_I(inode);
0881
0882 if (!xfs_is_cow_inode(ip))
0883 return 0;
0884
0885 if (!written) {
0886 xfs_reflink_cancel_cow_range(ip, pos, length, true);
0887 return 0;
0888 }
0889
0890 return xfs_reflink_end_cow(ip, pos, written);
0891 }
0892
0893 const struct iomap_ops xfs_dax_write_iomap_ops = {
0894 .iomap_begin = xfs_direct_write_iomap_begin,
0895 .iomap_end = xfs_dax_write_iomap_end,
0896 };
0897
0898 static int
0899 xfs_buffered_write_iomap_begin(
0900 struct inode *inode,
0901 loff_t offset,
0902 loff_t count,
0903 unsigned flags,
0904 struct iomap *iomap,
0905 struct iomap *srcmap)
0906 {
0907 struct xfs_inode *ip = XFS_I(inode);
0908 struct xfs_mount *mp = ip->i_mount;
0909 xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
0910 xfs_fileoff_t end_fsb = xfs_iomap_end_fsb(mp, offset, count);
0911 struct xfs_bmbt_irec imap, cmap;
0912 struct xfs_iext_cursor icur, ccur;
0913 xfs_fsblock_t prealloc_blocks = 0;
0914 bool eof = false, cow_eof = false, shared = false;
0915 int allocfork = XFS_DATA_FORK;
0916 int error = 0;
0917 unsigned int lockmode = XFS_ILOCK_EXCL;
0918
0919 if (xfs_is_shutdown(mp))
0920 return -EIO;
0921
0922
0923 if (xfs_get_extsz_hint(ip))
0924 return xfs_direct_write_iomap_begin(inode, offset, count,
0925 flags, iomap, srcmap);
0926
0927 ASSERT(!XFS_IS_REALTIME_INODE(ip));
0928
0929 error = xfs_ilock_for_iomap(ip, flags, &lockmode);
0930 if (error)
0931 return error;
0932
0933 if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(&ip->i_df)) ||
0934 XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
0935 error = -EFSCORRUPTED;
0936 goto out_unlock;
0937 }
0938
0939 XFS_STATS_INC(mp, xs_blk_mapw);
0940
0941 error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK);
0942 if (error)
0943 goto out_unlock;
0944
0945
0946
0947
0948
0949
0950
0951 eof = !xfs_iext_lookup_extent(ip, &ip->i_df, offset_fsb, &icur, &imap);
0952 if (eof)
0953 imap.br_startoff = end_fsb;
0954
0955
0956 if ((flags & IOMAP_ZERO) && imap.br_startoff > offset_fsb) {
0957 xfs_hole_to_iomap(ip, iomap, offset_fsb, imap.br_startoff);
0958 goto out_unlock;
0959 }
0960
0961
0962
0963
0964
0965
0966
0967
0968
0969 if (xfs_is_cow_inode(ip)) {
0970 if (!ip->i_cowfp) {
0971 ASSERT(!xfs_is_reflink_inode(ip));
0972 xfs_ifork_init_cow(ip);
0973 }
0974 cow_eof = !xfs_iext_lookup_extent(ip, ip->i_cowfp, offset_fsb,
0975 &ccur, &cmap);
0976 if (!cow_eof && cmap.br_startoff <= offset_fsb) {
0977 trace_xfs_reflink_cow_found(ip, &cmap);
0978 goto found_cow;
0979 }
0980 }
0981
0982 if (imap.br_startoff <= offset_fsb) {
0983
0984
0985
0986
0987
0988 if (!xfs_is_cow_inode(ip) ||
0989 ((flags & IOMAP_ZERO) && imap.br_state != XFS_EXT_NORM)) {
0990 trace_xfs_iomap_found(ip, offset, count, XFS_DATA_FORK,
0991 &imap);
0992 goto found_imap;
0993 }
0994
0995 xfs_trim_extent(&imap, offset_fsb, end_fsb - offset_fsb);
0996
0997
0998 error = xfs_bmap_trim_cow(ip, &imap, &shared);
0999 if (error)
1000 goto out_unlock;
1001
1002
1003 if (!shared) {
1004 trace_xfs_iomap_found(ip, offset, count, XFS_DATA_FORK,
1005 &imap);
1006 goto found_imap;
1007 }
1008
1009
1010
1011
1012
1013 allocfork = XFS_COW_FORK;
1014 end_fsb = imap.br_startoff + imap.br_blockcount;
1015 } else {
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025 count = min_t(loff_t, count, 1024 * PAGE_SIZE);
1026 end_fsb = xfs_iomap_end_fsb(mp, offset, count);
1027
1028 if (xfs_is_always_cow_inode(ip))
1029 allocfork = XFS_COW_FORK;
1030 }
1031
1032 error = xfs_qm_dqattach_locked(ip, false);
1033 if (error)
1034 goto out_unlock;
1035
1036 if (eof && offset + count > XFS_ISIZE(ip)) {
1037
1038
1039
1040
1041 if (xfs_has_allocsize(mp))
1042 prealloc_blocks = mp->m_allocsize_blocks;
1043 else
1044 prealloc_blocks = xfs_iomap_prealloc_size(ip, allocfork,
1045 offset, count, &icur);
1046 if (prealloc_blocks) {
1047 xfs_extlen_t align;
1048 xfs_off_t end_offset;
1049 xfs_fileoff_t p_end_fsb;
1050
1051 end_offset = XFS_ALLOC_ALIGN(mp, offset + count - 1);
1052 p_end_fsb = XFS_B_TO_FSBT(mp, end_offset) +
1053 prealloc_blocks;
1054
1055 align = xfs_eof_alignment(ip);
1056 if (align)
1057 p_end_fsb = roundup_64(p_end_fsb, align);
1058
1059 p_end_fsb = min(p_end_fsb,
1060 XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes));
1061 ASSERT(p_end_fsb > offset_fsb);
1062 prealloc_blocks = p_end_fsb - end_fsb;
1063 }
1064 }
1065
1066 retry:
1067 error = xfs_bmapi_reserve_delalloc(ip, allocfork, offset_fsb,
1068 end_fsb - offset_fsb, prealloc_blocks,
1069 allocfork == XFS_DATA_FORK ? &imap : &cmap,
1070 allocfork == XFS_DATA_FORK ? &icur : &ccur,
1071 allocfork == XFS_DATA_FORK ? eof : cow_eof);
1072 switch (error) {
1073 case 0:
1074 break;
1075 case -ENOSPC:
1076 case -EDQUOT:
1077
1078 trace_xfs_delalloc_enospc(ip, offset, count);
1079 if (prealloc_blocks) {
1080 prealloc_blocks = 0;
1081 goto retry;
1082 }
1083 fallthrough;
1084 default:
1085 goto out_unlock;
1086 }
1087
1088 if (allocfork == XFS_COW_FORK) {
1089 trace_xfs_iomap_alloc(ip, offset, count, allocfork, &cmap);
1090 goto found_cow;
1091 }
1092
1093
1094
1095
1096
1097 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1098 trace_xfs_iomap_alloc(ip, offset, count, allocfork, &imap);
1099 return xfs_bmbt_to_iomap(ip, iomap, &imap, flags, IOMAP_F_NEW);
1100
1101 found_imap:
1102 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1103 return xfs_bmbt_to_iomap(ip, iomap, &imap, flags, 0);
1104
1105 found_cow:
1106 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1107 if (imap.br_startoff <= offset_fsb) {
1108 error = xfs_bmbt_to_iomap(ip, srcmap, &imap, flags, 0);
1109 if (error)
1110 return error;
1111 return xfs_bmbt_to_iomap(ip, iomap, &cmap, flags,
1112 IOMAP_F_SHARED);
1113 }
1114
1115 xfs_trim_extent(&cmap, offset_fsb, imap.br_startoff - offset_fsb);
1116 return xfs_bmbt_to_iomap(ip, iomap, &cmap, flags, 0);
1117
1118 out_unlock:
1119 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1120 return error;
1121 }
1122
1123 static int
1124 xfs_buffered_write_iomap_end(
1125 struct inode *inode,
1126 loff_t offset,
1127 loff_t length,
1128 ssize_t written,
1129 unsigned flags,
1130 struct iomap *iomap)
1131 {
1132 struct xfs_inode *ip = XFS_I(inode);
1133 struct xfs_mount *mp = ip->i_mount;
1134 xfs_fileoff_t start_fsb;
1135 xfs_fileoff_t end_fsb;
1136 int error = 0;
1137
1138 if (iomap->type != IOMAP_DELALLOC)
1139 return 0;
1140
1141
1142
1143
1144
1145 if (XFS_TEST_ERROR(false, mp, XFS_ERRTAG_DROP_WRITES)) {
1146 iomap->flags |= IOMAP_F_NEW;
1147 written = 0;
1148 }
1149
1150
1151
1152
1153
1154
1155 if (unlikely(!written))
1156 start_fsb = XFS_B_TO_FSBT(mp, offset);
1157 else
1158 start_fsb = XFS_B_TO_FSB(mp, offset + written);
1159 end_fsb = XFS_B_TO_FSB(mp, offset + length);
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169 if ((iomap->flags & IOMAP_F_NEW) && start_fsb < end_fsb) {
1170 truncate_pagecache_range(VFS_I(ip), XFS_FSB_TO_B(mp, start_fsb),
1171 XFS_FSB_TO_B(mp, end_fsb) - 1);
1172
1173 error = xfs_bmap_punch_delalloc_range(ip, start_fsb,
1174 end_fsb - start_fsb);
1175 if (error && !xfs_is_shutdown(mp)) {
1176 xfs_alert(mp, "%s: unable to clean up ino %lld",
1177 __func__, ip->i_ino);
1178 return error;
1179 }
1180 }
1181
1182 return 0;
1183 }
1184
1185 const struct iomap_ops xfs_buffered_write_iomap_ops = {
1186 .iomap_begin = xfs_buffered_write_iomap_begin,
1187 .iomap_end = xfs_buffered_write_iomap_end,
1188 };
1189
1190 static int
1191 xfs_read_iomap_begin(
1192 struct inode *inode,
1193 loff_t offset,
1194 loff_t length,
1195 unsigned flags,
1196 struct iomap *iomap,
1197 struct iomap *srcmap)
1198 {
1199 struct xfs_inode *ip = XFS_I(inode);
1200 struct xfs_mount *mp = ip->i_mount;
1201 struct xfs_bmbt_irec imap;
1202 xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
1203 xfs_fileoff_t end_fsb = xfs_iomap_end_fsb(mp, offset, length);
1204 int nimaps = 1, error = 0;
1205 bool shared = false;
1206 unsigned int lockmode = XFS_ILOCK_SHARED;
1207
1208 ASSERT(!(flags & (IOMAP_WRITE | IOMAP_ZERO)));
1209
1210 if (xfs_is_shutdown(mp))
1211 return -EIO;
1212
1213 error = xfs_ilock_for_iomap(ip, flags, &lockmode);
1214 if (error)
1215 return error;
1216 error = xfs_bmapi_read(ip, offset_fsb, end_fsb - offset_fsb, &imap,
1217 &nimaps, 0);
1218 if (!error && (flags & IOMAP_REPORT))
1219 error = xfs_reflink_trim_around_shared(ip, &imap, &shared);
1220 xfs_iunlock(ip, lockmode);
1221
1222 if (error)
1223 return error;
1224 trace_xfs_iomap_found(ip, offset, length, XFS_DATA_FORK, &imap);
1225 return xfs_bmbt_to_iomap(ip, iomap, &imap, flags,
1226 shared ? IOMAP_F_SHARED : 0);
1227 }
1228
1229 const struct iomap_ops xfs_read_iomap_ops = {
1230 .iomap_begin = xfs_read_iomap_begin,
1231 };
1232
1233 static int
1234 xfs_seek_iomap_begin(
1235 struct inode *inode,
1236 loff_t offset,
1237 loff_t length,
1238 unsigned flags,
1239 struct iomap *iomap,
1240 struct iomap *srcmap)
1241 {
1242 struct xfs_inode *ip = XFS_I(inode);
1243 struct xfs_mount *mp = ip->i_mount;
1244 xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
1245 xfs_fileoff_t end_fsb = XFS_B_TO_FSB(mp, offset + length);
1246 xfs_fileoff_t cow_fsb = NULLFILEOFF, data_fsb = NULLFILEOFF;
1247 struct xfs_iext_cursor icur;
1248 struct xfs_bmbt_irec imap, cmap;
1249 int error = 0;
1250 unsigned lockmode;
1251
1252 if (xfs_is_shutdown(mp))
1253 return -EIO;
1254
1255 lockmode = xfs_ilock_data_map_shared(ip);
1256 error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK);
1257 if (error)
1258 goto out_unlock;
1259
1260 if (xfs_iext_lookup_extent(ip, &ip->i_df, offset_fsb, &icur, &imap)) {
1261
1262
1263
1264 if (imap.br_startoff <= offset_fsb)
1265 goto done;
1266 data_fsb = imap.br_startoff;
1267 } else {
1268
1269
1270
1271 data_fsb = xfs_iomap_end_fsb(mp, offset, length);
1272 }
1273
1274
1275
1276
1277
1278 if (xfs_inode_has_cow_data(ip) &&
1279 xfs_iext_lookup_extent(ip, ip->i_cowfp, offset_fsb, &icur, &cmap))
1280 cow_fsb = cmap.br_startoff;
1281 if (cow_fsb != NULLFILEOFF && cow_fsb <= offset_fsb) {
1282 if (data_fsb < cow_fsb + cmap.br_blockcount)
1283 end_fsb = min(end_fsb, data_fsb);
1284 xfs_trim_extent(&cmap, offset_fsb, end_fsb);
1285 error = xfs_bmbt_to_iomap(ip, iomap, &cmap, flags,
1286 IOMAP_F_SHARED);
1287
1288
1289
1290
1291
1292 iomap->type = IOMAP_UNWRITTEN;
1293 goto out_unlock;
1294 }
1295
1296
1297
1298
1299 if (cow_fsb != NULLFILEOFF && cow_fsb < data_fsb)
1300 imap.br_blockcount = cow_fsb - offset_fsb;
1301 else
1302 imap.br_blockcount = data_fsb - offset_fsb;
1303 imap.br_startoff = offset_fsb;
1304 imap.br_startblock = HOLESTARTBLOCK;
1305 imap.br_state = XFS_EXT_NORM;
1306 done:
1307 xfs_trim_extent(&imap, offset_fsb, end_fsb);
1308 error = xfs_bmbt_to_iomap(ip, iomap, &imap, flags, 0);
1309 out_unlock:
1310 xfs_iunlock(ip, lockmode);
1311 return error;
1312 }
1313
1314 const struct iomap_ops xfs_seek_iomap_ops = {
1315 .iomap_begin = xfs_seek_iomap_begin,
1316 };
1317
1318 static int
1319 xfs_xattr_iomap_begin(
1320 struct inode *inode,
1321 loff_t offset,
1322 loff_t length,
1323 unsigned flags,
1324 struct iomap *iomap,
1325 struct iomap *srcmap)
1326 {
1327 struct xfs_inode *ip = XFS_I(inode);
1328 struct xfs_mount *mp = ip->i_mount;
1329 xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
1330 xfs_fileoff_t end_fsb = XFS_B_TO_FSB(mp, offset + length);
1331 struct xfs_bmbt_irec imap;
1332 int nimaps = 1, error = 0;
1333 unsigned lockmode;
1334
1335 if (xfs_is_shutdown(mp))
1336 return -EIO;
1337
1338 lockmode = xfs_ilock_attr_map_shared(ip);
1339
1340
1341 if (!xfs_inode_has_attr_fork(ip) || !ip->i_af.if_nextents) {
1342 error = -ENOENT;
1343 goto out_unlock;
1344 }
1345
1346 ASSERT(ip->i_af.if_format != XFS_DINODE_FMT_LOCAL);
1347 error = xfs_bmapi_read(ip, offset_fsb, end_fsb - offset_fsb, &imap,
1348 &nimaps, XFS_BMAPI_ATTRFORK);
1349 out_unlock:
1350 xfs_iunlock(ip, lockmode);
1351
1352 if (error)
1353 return error;
1354 ASSERT(nimaps);
1355 return xfs_bmbt_to_iomap(ip, iomap, &imap, flags, 0);
1356 }
1357
1358 const struct iomap_ops xfs_xattr_iomap_ops = {
1359 .iomap_begin = xfs_xattr_iomap_begin,
1360 };
1361
1362 int
1363 xfs_zero_range(
1364 struct xfs_inode *ip,
1365 loff_t pos,
1366 loff_t len,
1367 bool *did_zero)
1368 {
1369 struct inode *inode = VFS_I(ip);
1370
1371 if (IS_DAX(inode))
1372 return dax_zero_range(inode, pos, len, did_zero,
1373 &xfs_direct_write_iomap_ops);
1374 return iomap_zero_range(inode, pos, len, did_zero,
1375 &xfs_buffered_write_iomap_ops);
1376 }
1377
1378 int
1379 xfs_truncate_page(
1380 struct xfs_inode *ip,
1381 loff_t pos,
1382 bool *did_zero)
1383 {
1384 struct inode *inode = VFS_I(ip);
1385
1386 if (IS_DAX(inode))
1387 return dax_truncate_page(inode, pos, did_zero,
1388 &xfs_direct_write_iomap_ops);
1389 return iomap_truncate_page(inode, pos, did_zero,
1390 &xfs_buffered_write_iomap_ops);
1391 }