0001
0002
0003
0004
0005
0006 #include "xfs.h"
0007 #include "xfs_fs.h"
0008 #include "xfs_shared.h"
0009 #include "xfs_format.h"
0010 #include "xfs_log_format.h"
0011 #include "xfs_trans_resv.h"
0012 #include "xfs_mount.h"
0013 #include "xfs_defer.h"
0014 #include "xfs_inode.h"
0015 #include "xfs_trans.h"
0016 #include "xfs_bmap.h"
0017 #include "xfs_bmap_util.h"
0018 #include "xfs_trace.h"
0019 #include "xfs_icache.h"
0020 #include "xfs_btree.h"
0021 #include "xfs_refcount_btree.h"
0022 #include "xfs_refcount.h"
0023 #include "xfs_bmap_btree.h"
0024 #include "xfs_trans_space.h"
0025 #include "xfs_bit.h"
0026 #include "xfs_alloc.h"
0027 #include "xfs_quota.h"
0028 #include "xfs_reflink.h"
0029 #include "xfs_iomap.h"
0030 #include "xfs_ag.h"
0031 #include "xfs_ag_resv.h"
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084
0085
0086
0087
0088
0089
0090
0091
0092
0093
0094
0095
0096
0097
0098
0099
0100
0101
0102
0103
0104
0105
0106
0107
0108
0109
0110
0111
0112
0113
0114
0115
0116
0117
0118
0119
0120
0121
0122
0123
0124
0125
0126
0127
0128 static int
0129 xfs_reflink_find_shared(
0130 struct xfs_perag *pag,
0131 struct xfs_trans *tp,
0132 xfs_agblock_t agbno,
0133 xfs_extlen_t aglen,
0134 xfs_agblock_t *fbno,
0135 xfs_extlen_t *flen,
0136 bool find_end_of_shared)
0137 {
0138 struct xfs_buf *agbp;
0139 struct xfs_btree_cur *cur;
0140 int error;
0141
0142 error = xfs_alloc_read_agf(pag, tp, 0, &agbp);
0143 if (error)
0144 return error;
0145
0146 cur = xfs_refcountbt_init_cursor(pag->pag_mount, tp, agbp, pag);
0147
0148 error = xfs_refcount_find_shared(cur, agbno, aglen, fbno, flen,
0149 find_end_of_shared);
0150
0151 xfs_btree_del_cursor(cur, error);
0152
0153 xfs_trans_brelse(tp, agbp);
0154 return error;
0155 }
0156
0157
0158
0159
0160
0161
0162
0163
0164
0165
0166
0167 int
0168 xfs_reflink_trim_around_shared(
0169 struct xfs_inode *ip,
0170 struct xfs_bmbt_irec *irec,
0171 bool *shared)
0172 {
0173 struct xfs_mount *mp = ip->i_mount;
0174 struct xfs_perag *pag;
0175 xfs_agblock_t agbno;
0176 xfs_extlen_t aglen;
0177 xfs_agblock_t fbno;
0178 xfs_extlen_t flen;
0179 int error = 0;
0180
0181
0182 if (!xfs_is_cow_inode(ip) || !xfs_bmap_is_written_extent(irec)) {
0183 *shared = false;
0184 return 0;
0185 }
0186
0187 trace_xfs_reflink_trim_around_shared(ip, irec);
0188
0189 pag = xfs_perag_get(mp, XFS_FSB_TO_AGNO(mp, irec->br_startblock));
0190 agbno = XFS_FSB_TO_AGBNO(mp, irec->br_startblock);
0191 aglen = irec->br_blockcount;
0192
0193 error = xfs_reflink_find_shared(pag, NULL, agbno, aglen, &fbno, &flen,
0194 true);
0195 xfs_perag_put(pag);
0196 if (error)
0197 return error;
0198
0199 *shared = false;
0200 if (fbno == NULLAGBLOCK) {
0201
0202 return 0;
0203 } else if (fbno == agbno) {
0204
0205
0206
0207
0208
0209
0210 irec->br_blockcount = flen;
0211 *shared = true;
0212 return 0;
0213 } else {
0214
0215
0216
0217
0218
0219
0220 irec->br_blockcount = fbno - agbno;
0221 return 0;
0222 }
0223 }
0224
0225 int
0226 xfs_bmap_trim_cow(
0227 struct xfs_inode *ip,
0228 struct xfs_bmbt_irec *imap,
0229 bool *shared)
0230 {
0231
0232 if (xfs_is_always_cow_inode(ip) &&
0233 !isnullstartblock(imap->br_startblock)) {
0234 *shared = true;
0235 return 0;
0236 }
0237
0238
0239 return xfs_reflink_trim_around_shared(ip, imap, shared);
0240 }
0241
0242 static int
0243 xfs_reflink_convert_cow_locked(
0244 struct xfs_inode *ip,
0245 xfs_fileoff_t offset_fsb,
0246 xfs_filblks_t count_fsb)
0247 {
0248 struct xfs_iext_cursor icur;
0249 struct xfs_bmbt_irec got;
0250 struct xfs_btree_cur *dummy_cur = NULL;
0251 int dummy_logflags;
0252 int error = 0;
0253
0254 if (!xfs_iext_lookup_extent(ip, ip->i_cowfp, offset_fsb, &icur, &got))
0255 return 0;
0256
0257 do {
0258 if (got.br_startoff >= offset_fsb + count_fsb)
0259 break;
0260 if (got.br_state == XFS_EXT_NORM)
0261 continue;
0262 if (WARN_ON_ONCE(isnullstartblock(got.br_startblock)))
0263 return -EIO;
0264
0265 xfs_trim_extent(&got, offset_fsb, count_fsb);
0266 if (!got.br_blockcount)
0267 continue;
0268
0269 got.br_state = XFS_EXT_NORM;
0270 error = xfs_bmap_add_extent_unwritten_real(NULL, ip,
0271 XFS_COW_FORK, &icur, &dummy_cur, &got,
0272 &dummy_logflags);
0273 if (error)
0274 return error;
0275 } while (xfs_iext_next_extent(ip->i_cowfp, &icur, &got));
0276
0277 return error;
0278 }
0279
0280
0281 int
0282 xfs_reflink_convert_cow(
0283 struct xfs_inode *ip,
0284 xfs_off_t offset,
0285 xfs_off_t count)
0286 {
0287 struct xfs_mount *mp = ip->i_mount;
0288 xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
0289 xfs_fileoff_t end_fsb = XFS_B_TO_FSB(mp, offset + count);
0290 xfs_filblks_t count_fsb = end_fsb - offset_fsb;
0291 int error;
0292
0293 ASSERT(count != 0);
0294
0295 xfs_ilock(ip, XFS_ILOCK_EXCL);
0296 error = xfs_reflink_convert_cow_locked(ip, offset_fsb, count_fsb);
0297 xfs_iunlock(ip, XFS_ILOCK_EXCL);
0298 return error;
0299 }
0300
0301
0302
0303
0304
0305
0306 static int
0307 xfs_find_trim_cow_extent(
0308 struct xfs_inode *ip,
0309 struct xfs_bmbt_irec *imap,
0310 struct xfs_bmbt_irec *cmap,
0311 bool *shared,
0312 bool *found)
0313 {
0314 xfs_fileoff_t offset_fsb = imap->br_startoff;
0315 xfs_filblks_t count_fsb = imap->br_blockcount;
0316 struct xfs_iext_cursor icur;
0317
0318 *found = false;
0319
0320
0321
0322
0323
0324 if (!xfs_iext_lookup_extent(ip, ip->i_cowfp, offset_fsb, &icur, cmap))
0325 cmap->br_startoff = offset_fsb + count_fsb;
0326 if (cmap->br_startoff > offset_fsb) {
0327 xfs_trim_extent(imap, imap->br_startoff,
0328 cmap->br_startoff - imap->br_startoff);
0329 return xfs_bmap_trim_cow(ip, imap, shared);
0330 }
0331
0332 *shared = true;
0333 if (isnullstartblock(cmap->br_startblock)) {
0334 xfs_trim_extent(imap, cmap->br_startoff, cmap->br_blockcount);
0335 return 0;
0336 }
0337
0338
0339 xfs_trim_extent(cmap, offset_fsb, count_fsb);
0340 *found = true;
0341 return 0;
0342 }
0343
0344 static int
0345 xfs_reflink_convert_unwritten(
0346 struct xfs_inode *ip,
0347 struct xfs_bmbt_irec *imap,
0348 struct xfs_bmbt_irec *cmap,
0349 bool convert_now)
0350 {
0351 xfs_fileoff_t offset_fsb = imap->br_startoff;
0352 xfs_filblks_t count_fsb = imap->br_blockcount;
0353 int error;
0354
0355
0356
0357
0358 xfs_trim_extent(cmap, offset_fsb, count_fsb);
0359
0360
0361
0362
0363
0364
0365 if (!convert_now || cmap->br_state == XFS_EXT_NORM)
0366 return 0;
0367
0368 trace_xfs_reflink_convert_cow(ip, cmap);
0369
0370 error = xfs_reflink_convert_cow_locked(ip, offset_fsb, count_fsb);
0371 if (!error)
0372 cmap->br_state = XFS_EXT_NORM;
0373
0374 return error;
0375 }
0376
0377 static int
0378 xfs_reflink_fill_cow_hole(
0379 struct xfs_inode *ip,
0380 struct xfs_bmbt_irec *imap,
0381 struct xfs_bmbt_irec *cmap,
0382 bool *shared,
0383 uint *lockmode,
0384 bool convert_now)
0385 {
0386 struct xfs_mount *mp = ip->i_mount;
0387 struct xfs_trans *tp;
0388 xfs_filblks_t resaligned;
0389 xfs_extlen_t resblks;
0390 int nimaps;
0391 int error;
0392 bool found;
0393
0394 resaligned = xfs_aligned_fsb_count(imap->br_startoff,
0395 imap->br_blockcount, xfs_get_cowextsz_hint(ip));
0396 resblks = XFS_DIOSTRAT_SPACE_RES(mp, resaligned);
0397
0398 xfs_iunlock(ip, *lockmode);
0399 *lockmode = 0;
0400
0401 error = xfs_trans_alloc_inode(ip, &M_RES(mp)->tr_write, resblks, 0,
0402 false, &tp);
0403 if (error)
0404 return error;
0405
0406 *lockmode = XFS_ILOCK_EXCL;
0407
0408 error = xfs_find_trim_cow_extent(ip, imap, cmap, shared, &found);
0409 if (error || !*shared)
0410 goto out_trans_cancel;
0411
0412 if (found) {
0413 xfs_trans_cancel(tp);
0414 goto convert;
0415 }
0416
0417 ASSERT(cmap->br_startoff > imap->br_startoff);
0418
0419
0420 nimaps = 1;
0421 error = xfs_bmapi_write(tp, ip, imap->br_startoff, imap->br_blockcount,
0422 XFS_BMAPI_COWFORK | XFS_BMAPI_PREALLOC, 0, cmap,
0423 &nimaps);
0424 if (error)
0425 goto out_trans_cancel;
0426
0427 xfs_inode_set_cowblocks_tag(ip);
0428 error = xfs_trans_commit(tp);
0429 if (error)
0430 return error;
0431
0432
0433
0434
0435
0436 if (nimaps == 0)
0437 return -ENOSPC;
0438
0439 convert:
0440 return xfs_reflink_convert_unwritten(ip, imap, cmap, convert_now);
0441
0442 out_trans_cancel:
0443 xfs_trans_cancel(tp);
0444 return error;
0445 }
0446
0447 static int
0448 xfs_reflink_fill_delalloc(
0449 struct xfs_inode *ip,
0450 struct xfs_bmbt_irec *imap,
0451 struct xfs_bmbt_irec *cmap,
0452 bool *shared,
0453 uint *lockmode,
0454 bool convert_now)
0455 {
0456 struct xfs_mount *mp = ip->i_mount;
0457 struct xfs_trans *tp;
0458 int nimaps;
0459 int error;
0460 bool found;
0461
0462 do {
0463 xfs_iunlock(ip, *lockmode);
0464 *lockmode = 0;
0465
0466 error = xfs_trans_alloc_inode(ip, &M_RES(mp)->tr_write, 0, 0,
0467 false, &tp);
0468 if (error)
0469 return error;
0470
0471 *lockmode = XFS_ILOCK_EXCL;
0472
0473 error = xfs_find_trim_cow_extent(ip, imap, cmap, shared,
0474 &found);
0475 if (error || !*shared)
0476 goto out_trans_cancel;
0477
0478 if (found) {
0479 xfs_trans_cancel(tp);
0480 break;
0481 }
0482
0483 ASSERT(isnullstartblock(cmap->br_startblock) ||
0484 cmap->br_startblock == DELAYSTARTBLOCK);
0485
0486
0487
0488
0489 nimaps = 1;
0490 error = xfs_bmapi_write(tp, ip, cmap->br_startoff,
0491 cmap->br_blockcount,
0492 XFS_BMAPI_COWFORK | XFS_BMAPI_PREALLOC, 0,
0493 cmap, &nimaps);
0494 if (error)
0495 goto out_trans_cancel;
0496
0497 xfs_inode_set_cowblocks_tag(ip);
0498 error = xfs_trans_commit(tp);
0499 if (error)
0500 return error;
0501
0502
0503
0504
0505
0506 if (nimaps == 0)
0507 return -ENOSPC;
0508 } while (cmap->br_startoff + cmap->br_blockcount <= imap->br_startoff);
0509
0510 return xfs_reflink_convert_unwritten(ip, imap, cmap, convert_now);
0511
0512 out_trans_cancel:
0513 xfs_trans_cancel(tp);
0514 return error;
0515 }
0516
0517
0518 int
0519 xfs_reflink_allocate_cow(
0520 struct xfs_inode *ip,
0521 struct xfs_bmbt_irec *imap,
0522 struct xfs_bmbt_irec *cmap,
0523 bool *shared,
0524 uint *lockmode,
0525 bool convert_now)
0526 {
0527 int error;
0528 bool found;
0529
0530 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
0531 if (!ip->i_cowfp) {
0532 ASSERT(!xfs_is_reflink_inode(ip));
0533 xfs_ifork_init_cow(ip);
0534 }
0535
0536 error = xfs_find_trim_cow_extent(ip, imap, cmap, shared, &found);
0537 if (error || !*shared)
0538 return error;
0539
0540
0541 if (found)
0542 return xfs_reflink_convert_unwritten(ip, imap, cmap,
0543 convert_now);
0544
0545
0546
0547
0548
0549 if (cmap->br_startoff > imap->br_startoff)
0550 return xfs_reflink_fill_cow_hole(ip, imap, cmap, shared,
0551 lockmode, convert_now);
0552
0553
0554
0555
0556
0557 if (isnullstartblock(cmap->br_startblock) ||
0558 cmap->br_startblock == DELAYSTARTBLOCK)
0559 return xfs_reflink_fill_delalloc(ip, imap, cmap, shared,
0560 lockmode, convert_now);
0561
0562
0563 ASSERT(0);
0564 return -EFSCORRUPTED;
0565 }
0566
0567
0568
0569
0570
0571
0572
0573
0574
0575
0576 int
0577 xfs_reflink_cancel_cow_blocks(
0578 struct xfs_inode *ip,
0579 struct xfs_trans **tpp,
0580 xfs_fileoff_t offset_fsb,
0581 xfs_fileoff_t end_fsb,
0582 bool cancel_real)
0583 {
0584 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, XFS_COW_FORK);
0585 struct xfs_bmbt_irec got, del;
0586 struct xfs_iext_cursor icur;
0587 int error = 0;
0588
0589 if (!xfs_inode_has_cow_data(ip))
0590 return 0;
0591 if (!xfs_iext_lookup_extent_before(ip, ifp, &end_fsb, &icur, &got))
0592 return 0;
0593
0594
0595 while (got.br_startoff + got.br_blockcount > offset_fsb) {
0596 del = got;
0597 xfs_trim_extent(&del, offset_fsb, end_fsb - offset_fsb);
0598
0599
0600 if (!del.br_blockcount) {
0601 xfs_iext_prev(ifp, &icur);
0602 goto next_extent;
0603 }
0604
0605 trace_xfs_reflink_cancel_cow(ip, &del);
0606
0607 if (isnullstartblock(del.br_startblock)) {
0608 error = xfs_bmap_del_extent_delay(ip, XFS_COW_FORK,
0609 &icur, &got, &del);
0610 if (error)
0611 break;
0612 } else if (del.br_state == XFS_EXT_UNWRITTEN || cancel_real) {
0613 ASSERT((*tpp)->t_firstblock == NULLFSBLOCK);
0614
0615
0616 xfs_refcount_free_cow_extent(*tpp, del.br_startblock,
0617 del.br_blockcount);
0618
0619 xfs_free_extent_later(*tpp, del.br_startblock,
0620 del.br_blockcount, NULL);
0621
0622
0623 error = xfs_defer_finish(tpp);
0624 if (error)
0625 break;
0626
0627
0628 xfs_bmap_del_extent_cow(ip, &icur, &got, &del);
0629
0630
0631 error = xfs_quota_unreserve_blkres(ip,
0632 del.br_blockcount);
0633 if (error)
0634 break;
0635 } else {
0636
0637 xfs_iext_prev(ifp, &icur);
0638 }
0639 next_extent:
0640 if (!xfs_iext_get_extent(ifp, &icur, &got))
0641 break;
0642 }
0643
0644
0645 if (!ifp->if_bytes)
0646 xfs_inode_clear_cowblocks_tag(ip);
0647 return error;
0648 }
0649
0650
0651
0652
0653
0654
0655
0656 int
0657 xfs_reflink_cancel_cow_range(
0658 struct xfs_inode *ip,
0659 xfs_off_t offset,
0660 xfs_off_t count,
0661 bool cancel_real)
0662 {
0663 struct xfs_trans *tp;
0664 xfs_fileoff_t offset_fsb;
0665 xfs_fileoff_t end_fsb;
0666 int error;
0667
0668 trace_xfs_reflink_cancel_cow_range(ip, offset, count);
0669 ASSERT(ip->i_cowfp);
0670
0671 offset_fsb = XFS_B_TO_FSBT(ip->i_mount, offset);
0672 if (count == NULLFILEOFF)
0673 end_fsb = NULLFILEOFF;
0674 else
0675 end_fsb = XFS_B_TO_FSB(ip->i_mount, offset + count);
0676
0677
0678 error = xfs_trans_alloc(ip->i_mount, &M_RES(ip->i_mount)->tr_write,
0679 0, 0, 0, &tp);
0680 if (error)
0681 goto out;
0682
0683 xfs_ilock(ip, XFS_ILOCK_EXCL);
0684 xfs_trans_ijoin(tp, ip, 0);
0685
0686
0687 error = xfs_reflink_cancel_cow_blocks(ip, &tp, offset_fsb, end_fsb,
0688 cancel_real);
0689 if (error)
0690 goto out_cancel;
0691
0692 error = xfs_trans_commit(tp);
0693
0694 xfs_iunlock(ip, XFS_ILOCK_EXCL);
0695 return error;
0696
0697 out_cancel:
0698 xfs_trans_cancel(tp);
0699 xfs_iunlock(ip, XFS_ILOCK_EXCL);
0700 out:
0701 trace_xfs_reflink_cancel_cow_range_error(ip, error, _RET_IP_);
0702 return error;
0703 }
0704
0705
0706
0707
0708
0709
0710
0711
0712
0713
0714
0715 STATIC int
0716 xfs_reflink_end_cow_extent(
0717 struct xfs_inode *ip,
0718 xfs_fileoff_t *offset_fsb,
0719 xfs_fileoff_t end_fsb)
0720 {
0721 struct xfs_iext_cursor icur;
0722 struct xfs_bmbt_irec got, del, data;
0723 struct xfs_mount *mp = ip->i_mount;
0724 struct xfs_trans *tp;
0725 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, XFS_COW_FORK);
0726 unsigned int resblks;
0727 int nmaps;
0728 int error;
0729
0730
0731 if (ifp->if_bytes == 0) {
0732 *offset_fsb = end_fsb;
0733 return 0;
0734 }
0735
0736 resblks = XFS_EXTENTADD_SPACE_RES(mp, XFS_DATA_FORK);
0737 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, 0,
0738 XFS_TRANS_RESERVE, &tp);
0739 if (error)
0740 return error;
0741
0742
0743
0744
0745
0746
0747 xfs_ilock(ip, XFS_ILOCK_EXCL);
0748 xfs_trans_ijoin(tp, ip, 0);
0749
0750 error = xfs_iext_count_may_overflow(ip, XFS_DATA_FORK,
0751 XFS_IEXT_REFLINK_END_COW_CNT);
0752 if (error == -EFBIG)
0753 error = xfs_iext_count_upgrade(tp, ip,
0754 XFS_IEXT_REFLINK_END_COW_CNT);
0755 if (error)
0756 goto out_cancel;
0757
0758
0759
0760
0761
0762
0763 if (!xfs_iext_lookup_extent(ip, ifp, *offset_fsb, &icur, &got) ||
0764 got.br_startoff >= end_fsb) {
0765 *offset_fsb = end_fsb;
0766 goto out_cancel;
0767 }
0768
0769
0770
0771
0772
0773
0774
0775
0776 while (!xfs_bmap_is_written_extent(&got)) {
0777 if (!xfs_iext_next_extent(ifp, &icur, &got) ||
0778 got.br_startoff >= end_fsb) {
0779 *offset_fsb = end_fsb;
0780 goto out_cancel;
0781 }
0782 }
0783 del = got;
0784
0785
0786 nmaps = 1;
0787 error = xfs_bmapi_read(ip, del.br_startoff, del.br_blockcount, &data,
0788 &nmaps, 0);
0789 if (error)
0790 goto out_cancel;
0791
0792
0793 data.br_blockcount = min(data.br_blockcount, del.br_blockcount);
0794 del.br_blockcount = data.br_blockcount;
0795
0796 trace_xfs_reflink_cow_remap_from(ip, &del);
0797 trace_xfs_reflink_cow_remap_to(ip, &data);
0798
0799 if (xfs_bmap_is_real_extent(&data)) {
0800
0801
0802
0803
0804 xfs_bmap_unmap_extent(tp, ip, &data);
0805 xfs_refcount_decrease_extent(tp, &data);
0806 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT,
0807 -data.br_blockcount);
0808 } else if (data.br_startblock == DELAYSTARTBLOCK) {
0809 int done;
0810
0811
0812
0813
0814
0815
0816
0817 error = xfs_bunmapi(NULL, ip, data.br_startoff,
0818 data.br_blockcount, 0, 1, &done);
0819 if (error)
0820 goto out_cancel;
0821 ASSERT(done);
0822 }
0823
0824
0825 xfs_refcount_free_cow_extent(tp, del.br_startblock, del.br_blockcount);
0826
0827
0828 xfs_bmap_map_extent(tp, ip, &del);
0829
0830
0831 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_DELBCOUNT,
0832 (long)del.br_blockcount);
0833
0834
0835 xfs_bmap_del_extent_cow(ip, &icur, &got, &del);
0836
0837 error = xfs_trans_commit(tp);
0838 xfs_iunlock(ip, XFS_ILOCK_EXCL);
0839 if (error)
0840 return error;
0841
0842
0843 *offset_fsb = del.br_startoff + del.br_blockcount;
0844 return 0;
0845
0846 out_cancel:
0847 xfs_trans_cancel(tp);
0848 xfs_iunlock(ip, XFS_ILOCK_EXCL);
0849 return error;
0850 }
0851
0852
0853
0854
0855 int
0856 xfs_reflink_end_cow(
0857 struct xfs_inode *ip,
0858 xfs_off_t offset,
0859 xfs_off_t count)
0860 {
0861 xfs_fileoff_t offset_fsb;
0862 xfs_fileoff_t end_fsb;
0863 int error = 0;
0864
0865 trace_xfs_reflink_end_cow(ip, offset, count);
0866
0867 offset_fsb = XFS_B_TO_FSBT(ip->i_mount, offset);
0868 end_fsb = XFS_B_TO_FSB(ip->i_mount, offset + count);
0869
0870
0871
0872
0873
0874
0875
0876
0877
0878
0879
0880
0881
0882
0883
0884
0885
0886
0887
0888
0889
0890
0891
0892
0893
0894
0895
0896
0897
0898
0899
0900
0901
0902 while (end_fsb > offset_fsb && !error)
0903 error = xfs_reflink_end_cow_extent(ip, &offset_fsb, end_fsb);
0904
0905 if (error)
0906 trace_xfs_reflink_end_cow_error(ip, error, _RET_IP_);
0907 return error;
0908 }
0909
0910
0911
0912
0913
0914
0915
0916 int
0917 xfs_reflink_recover_cow(
0918 struct xfs_mount *mp)
0919 {
0920 struct xfs_perag *pag;
0921 xfs_agnumber_t agno;
0922 int error = 0;
0923
0924 if (!xfs_has_reflink(mp))
0925 return 0;
0926
0927 for_each_perag(mp, agno, pag) {
0928 error = xfs_refcount_recover_cow_leftovers(mp, pag);
0929 if (error) {
0930 xfs_perag_put(pag);
0931 break;
0932 }
0933 }
0934
0935 return error;
0936 }
0937
0938
0939
0940
0941
0942
0943
0944
0945
0946
0947
0948
0949
0950
0951
0952
0953
0954
0955
0956
0957
0958
0959
0960
0961
0962
0963
0964
0965
0966
0967
0968
0969
0970
0971
0972
0973
0974
0975
0976
0977
0978
0979
0980
0981
0982
0983
0984
0985
0986
0987
0988
0989
0990
0991
0992
0993
0994
0995
0996
0997
0998
0999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017 STATIC int
1018 xfs_reflink_set_inode_flag(
1019 struct xfs_inode *src,
1020 struct xfs_inode *dest)
1021 {
1022 struct xfs_mount *mp = src->i_mount;
1023 int error;
1024 struct xfs_trans *tp;
1025
1026 if (xfs_is_reflink_inode(src) && xfs_is_reflink_inode(dest))
1027 return 0;
1028
1029 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp);
1030 if (error)
1031 goto out_error;
1032
1033
1034 if (src->i_ino == dest->i_ino)
1035 xfs_ilock(src, XFS_ILOCK_EXCL);
1036 else
1037 xfs_lock_two_inodes(src, XFS_ILOCK_EXCL, dest, XFS_ILOCK_EXCL);
1038
1039 if (!xfs_is_reflink_inode(src)) {
1040 trace_xfs_reflink_set_inode_flag(src);
1041 xfs_trans_ijoin(tp, src, XFS_ILOCK_EXCL);
1042 src->i_diflags2 |= XFS_DIFLAG2_REFLINK;
1043 xfs_trans_log_inode(tp, src, XFS_ILOG_CORE);
1044 xfs_ifork_init_cow(src);
1045 } else
1046 xfs_iunlock(src, XFS_ILOCK_EXCL);
1047
1048 if (src->i_ino == dest->i_ino)
1049 goto commit_flags;
1050
1051 if (!xfs_is_reflink_inode(dest)) {
1052 trace_xfs_reflink_set_inode_flag(dest);
1053 xfs_trans_ijoin(tp, dest, XFS_ILOCK_EXCL);
1054 dest->i_diflags2 |= XFS_DIFLAG2_REFLINK;
1055 xfs_trans_log_inode(tp, dest, XFS_ILOG_CORE);
1056 xfs_ifork_init_cow(dest);
1057 } else
1058 xfs_iunlock(dest, XFS_ILOCK_EXCL);
1059
1060 commit_flags:
1061 error = xfs_trans_commit(tp);
1062 if (error)
1063 goto out_error;
1064 return error;
1065
1066 out_error:
1067 trace_xfs_reflink_set_inode_flag_error(dest, error, _RET_IP_);
1068 return error;
1069 }
1070
1071
1072
1073
1074 int
1075 xfs_reflink_update_dest(
1076 struct xfs_inode *dest,
1077 xfs_off_t newlen,
1078 xfs_extlen_t cowextsize,
1079 unsigned int remap_flags)
1080 {
1081 struct xfs_mount *mp = dest->i_mount;
1082 struct xfs_trans *tp;
1083 int error;
1084
1085 if (newlen <= i_size_read(VFS_I(dest)) && cowextsize == 0)
1086 return 0;
1087
1088 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp);
1089 if (error)
1090 goto out_error;
1091
1092 xfs_ilock(dest, XFS_ILOCK_EXCL);
1093 xfs_trans_ijoin(tp, dest, XFS_ILOCK_EXCL);
1094
1095 if (newlen > i_size_read(VFS_I(dest))) {
1096 trace_xfs_reflink_update_inode_size(dest, newlen);
1097 i_size_write(VFS_I(dest), newlen);
1098 dest->i_disk_size = newlen;
1099 }
1100
1101 if (cowextsize) {
1102 dest->i_cowextsize = cowextsize;
1103 dest->i_diflags2 |= XFS_DIFLAG2_COWEXTSIZE;
1104 }
1105
1106 xfs_trans_log_inode(tp, dest, XFS_ILOG_CORE);
1107
1108 error = xfs_trans_commit(tp);
1109 if (error)
1110 goto out_error;
1111 return error;
1112
1113 out_error:
1114 trace_xfs_reflink_update_inode_size_error(dest, error, _RET_IP_);
1115 return error;
1116 }
1117
1118
1119
1120
1121
1122
1123
1124 static int
1125 xfs_reflink_ag_has_free_space(
1126 struct xfs_mount *mp,
1127 xfs_agnumber_t agno)
1128 {
1129 struct xfs_perag *pag;
1130 int error = 0;
1131
1132 if (!xfs_has_rmapbt(mp))
1133 return 0;
1134
1135 pag = xfs_perag_get(mp, agno);
1136 if (xfs_ag_resv_critical(pag, XFS_AG_RESV_RMAPBT) ||
1137 xfs_ag_resv_critical(pag, XFS_AG_RESV_METADATA))
1138 error = -ENOSPC;
1139 xfs_perag_put(pag);
1140 return error;
1141 }
1142
1143
1144
1145
1146
1147 STATIC int
1148 xfs_reflink_remap_extent(
1149 struct xfs_inode *ip,
1150 struct xfs_bmbt_irec *dmap,
1151 xfs_off_t new_isize)
1152 {
1153 struct xfs_bmbt_irec smap;
1154 struct xfs_mount *mp = ip->i_mount;
1155 struct xfs_trans *tp;
1156 xfs_off_t newlen;
1157 int64_t qdelta = 0;
1158 unsigned int resblks;
1159 bool quota_reserved = true;
1160 bool smap_real;
1161 bool dmap_written = xfs_bmap_is_written_extent(dmap);
1162 int iext_delta = 0;
1163 int nimaps;
1164 int error;
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187 resblks = XFS_EXTENTADD_SPACE_RES(mp, XFS_DATA_FORK);
1188 error = xfs_trans_alloc_inode(ip, &M_RES(mp)->tr_write,
1189 resblks + dmap->br_blockcount, 0, false, &tp);
1190 if (error == -EDQUOT || error == -ENOSPC) {
1191 quota_reserved = false;
1192 error = xfs_trans_alloc_inode(ip, &M_RES(mp)->tr_write,
1193 resblks, 0, false, &tp);
1194 }
1195 if (error)
1196 goto out;
1197
1198
1199
1200
1201
1202
1203 nimaps = 1;
1204 error = xfs_bmapi_read(ip, dmap->br_startoff, dmap->br_blockcount,
1205 &smap, &nimaps, 0);
1206 if (error)
1207 goto out_cancel;
1208 ASSERT(nimaps == 1 && smap.br_startoff == dmap->br_startoff);
1209 smap_real = xfs_bmap_is_real_extent(&smap);
1210
1211
1212
1213
1214
1215 dmap->br_blockcount = min(dmap->br_blockcount, smap.br_blockcount);
1216 ASSERT(dmap->br_blockcount == smap.br_blockcount);
1217
1218 trace_xfs_reflink_remap_extent_dest(ip, &smap);
1219
1220
1221
1222
1223
1224
1225 if (dmap->br_startblock == smap.br_startblock) {
1226 if (dmap->br_state != smap.br_state)
1227 error = -EFSCORRUPTED;
1228 goto out_cancel;
1229 }
1230
1231
1232 if (dmap->br_state == XFS_EXT_UNWRITTEN &&
1233 smap.br_state == XFS_EXT_UNWRITTEN)
1234 goto out_cancel;
1235
1236
1237 if (dmap_written) {
1238 error = xfs_reflink_ag_has_free_space(mp,
1239 XFS_FSB_TO_AGNO(mp, dmap->br_startblock));
1240 if (error)
1241 goto out_cancel;
1242 }
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266 if (!quota_reserved && !smap_real && dmap_written) {
1267 error = xfs_trans_reserve_quota_nblks(tp, ip,
1268 dmap->br_blockcount, 0, false);
1269 if (error)
1270 goto out_cancel;
1271 }
1272
1273 if (smap_real)
1274 ++iext_delta;
1275
1276 if (dmap_written)
1277 ++iext_delta;
1278
1279 error = xfs_iext_count_may_overflow(ip, XFS_DATA_FORK, iext_delta);
1280 if (error == -EFBIG)
1281 error = xfs_iext_count_upgrade(tp, ip, iext_delta);
1282 if (error)
1283 goto out_cancel;
1284
1285 if (smap_real) {
1286
1287
1288
1289
1290 xfs_bmap_unmap_extent(tp, ip, &smap);
1291 xfs_refcount_decrease_extent(tp, &smap);
1292 qdelta -= smap.br_blockcount;
1293 } else if (smap.br_startblock == DELAYSTARTBLOCK) {
1294 int done;
1295
1296
1297
1298
1299
1300
1301
1302 error = xfs_bunmapi(NULL, ip, smap.br_startoff,
1303 smap.br_blockcount, 0, 1, &done);
1304 if (error)
1305 goto out_cancel;
1306 ASSERT(done);
1307 }
1308
1309
1310
1311
1312
1313 if (dmap_written) {
1314 xfs_refcount_increase_extent(tp, dmap);
1315 xfs_bmap_map_extent(tp, ip, dmap);
1316 qdelta += dmap->br_blockcount;
1317 }
1318
1319 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, qdelta);
1320
1321
1322 newlen = XFS_FSB_TO_B(mp, dmap->br_startoff + dmap->br_blockcount);
1323 newlen = min_t(xfs_off_t, newlen, new_isize);
1324 if (newlen > i_size_read(VFS_I(ip))) {
1325 trace_xfs_reflink_update_inode_size(ip, newlen);
1326 i_size_write(VFS_I(ip), newlen);
1327 ip->i_disk_size = newlen;
1328 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1329 }
1330
1331
1332 error = xfs_trans_commit(tp);
1333 goto out_unlock;
1334
1335 out_cancel:
1336 xfs_trans_cancel(tp);
1337 out_unlock:
1338 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1339 out:
1340 if (error)
1341 trace_xfs_reflink_remap_extent_error(ip, error, _RET_IP_);
1342 return error;
1343 }
1344
1345
1346 int
1347 xfs_reflink_remap_blocks(
1348 struct xfs_inode *src,
1349 loff_t pos_in,
1350 struct xfs_inode *dest,
1351 loff_t pos_out,
1352 loff_t remap_len,
1353 loff_t *remapped)
1354 {
1355 struct xfs_bmbt_irec imap;
1356 struct xfs_mount *mp = src->i_mount;
1357 xfs_fileoff_t srcoff = XFS_B_TO_FSBT(mp, pos_in);
1358 xfs_fileoff_t destoff = XFS_B_TO_FSBT(mp, pos_out);
1359 xfs_filblks_t len;
1360 xfs_filblks_t remapped_len = 0;
1361 xfs_off_t new_isize = pos_out + remap_len;
1362 int nimaps;
1363 int error = 0;
1364
1365 len = min_t(xfs_filblks_t, XFS_B_TO_FSB(mp, remap_len),
1366 XFS_MAX_FILEOFF);
1367
1368 trace_xfs_reflink_remap_blocks(src, srcoff, len, dest, destoff);
1369
1370 while (len > 0) {
1371 unsigned int lock_mode;
1372
1373
1374 nimaps = 1;
1375 lock_mode = xfs_ilock_data_map_shared(src);
1376 error = xfs_bmapi_read(src, srcoff, len, &imap, &nimaps, 0);
1377 xfs_iunlock(src, lock_mode);
1378 if (error)
1379 break;
1380
1381
1382
1383
1384
1385
1386
1387 ASSERT(nimaps == 1 && imap.br_startoff == srcoff);
1388 if (imap.br_startblock == DELAYSTARTBLOCK) {
1389 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
1390 error = -EFSCORRUPTED;
1391 break;
1392 }
1393
1394 trace_xfs_reflink_remap_extent_src(src, &imap);
1395
1396
1397 imap.br_startoff = destoff;
1398 error = xfs_reflink_remap_extent(dest, &imap, new_isize);
1399 if (error)
1400 break;
1401
1402 if (fatal_signal_pending(current)) {
1403 error = -EINTR;
1404 break;
1405 }
1406
1407
1408 srcoff += imap.br_blockcount;
1409 destoff += imap.br_blockcount;
1410 len -= imap.br_blockcount;
1411 remapped_len += imap.br_blockcount;
1412 }
1413
1414 if (error)
1415 trace_xfs_reflink_remap_blocks_error(dest, error, _RET_IP_);
1416 *remapped = min_t(loff_t, remap_len,
1417 XFS_FSB_TO_B(src->i_mount, remapped_len));
1418 return error;
1419 }
1420
1421
1422
1423
1424
1425
1426 static int
1427 xfs_reflink_zero_posteof(
1428 struct xfs_inode *ip,
1429 loff_t pos)
1430 {
1431 loff_t isize = i_size_read(VFS_I(ip));
1432
1433 if (pos <= isize)
1434 return 0;
1435
1436 trace_xfs_zero_eof(ip, isize, pos - isize);
1437 return xfs_zero_range(ip, isize, pos - isize, NULL);
1438 }
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469 int
1470 xfs_reflink_remap_prep(
1471 struct file *file_in,
1472 loff_t pos_in,
1473 struct file *file_out,
1474 loff_t pos_out,
1475 loff_t *len,
1476 unsigned int remap_flags)
1477 {
1478 struct inode *inode_in = file_inode(file_in);
1479 struct xfs_inode *src = XFS_I(inode_in);
1480 struct inode *inode_out = file_inode(file_out);
1481 struct xfs_inode *dest = XFS_I(inode_out);
1482 int ret;
1483
1484
1485 ret = xfs_ilock2_io_mmap(src, dest);
1486 if (ret)
1487 return ret;
1488
1489
1490 ret = -EINVAL;
1491
1492 if (XFS_IS_REALTIME_INODE(src) || XFS_IS_REALTIME_INODE(dest))
1493 goto out_unlock;
1494
1495
1496 if (IS_DAX(inode_in) != IS_DAX(inode_out))
1497 goto out_unlock;
1498
1499 if (!IS_DAX(inode_in))
1500 ret = generic_remap_file_range_prep(file_in, pos_in, file_out,
1501 pos_out, len, remap_flags);
1502 else
1503 ret = dax_remap_file_range_prep(file_in, pos_in, file_out,
1504 pos_out, len, remap_flags, &xfs_read_iomap_ops);
1505 if (ret || *len == 0)
1506 goto out_unlock;
1507
1508
1509 ret = xfs_qm_dqattach(dest);
1510 if (ret)
1511 goto out_unlock;
1512
1513
1514
1515
1516
1517 ret = xfs_reflink_zero_posteof(dest, pos_out);
1518 if (ret)
1519 goto out_unlock;
1520
1521
1522 ret = xfs_reflink_set_inode_flag(src, dest);
1523 if (ret)
1524 goto out_unlock;
1525
1526
1527
1528
1529
1530
1531 if (pos_out > XFS_ISIZE(dest)) {
1532 loff_t flen = *len + (pos_out - XFS_ISIZE(dest));
1533 ret = xfs_flush_unmap_range(dest, XFS_ISIZE(dest), flen);
1534 } else {
1535 ret = xfs_flush_unmap_range(dest, pos_out, *len);
1536 }
1537 if (ret)
1538 goto out_unlock;
1539
1540 return 0;
1541 out_unlock:
1542 xfs_iunlock2_io_mmap(src, dest);
1543 return ret;
1544 }
1545
1546
1547 int
1548 xfs_reflink_inode_has_shared_extents(
1549 struct xfs_trans *tp,
1550 struct xfs_inode *ip,
1551 bool *has_shared)
1552 {
1553 struct xfs_bmbt_irec got;
1554 struct xfs_mount *mp = ip->i_mount;
1555 struct xfs_ifork *ifp;
1556 struct xfs_iext_cursor icur;
1557 bool found;
1558 int error;
1559
1560 ifp = xfs_ifork_ptr(ip, XFS_DATA_FORK);
1561 error = xfs_iread_extents(tp, ip, XFS_DATA_FORK);
1562 if (error)
1563 return error;
1564
1565 *has_shared = false;
1566 found = xfs_iext_lookup_extent(ip, ifp, 0, &icur, &got);
1567 while (found) {
1568 struct xfs_perag *pag;
1569 xfs_agblock_t agbno;
1570 xfs_extlen_t aglen;
1571 xfs_agblock_t rbno;
1572 xfs_extlen_t rlen;
1573
1574 if (isnullstartblock(got.br_startblock) ||
1575 got.br_state != XFS_EXT_NORM)
1576 goto next;
1577
1578 pag = xfs_perag_get(mp, XFS_FSB_TO_AGNO(mp, got.br_startblock));
1579 agbno = XFS_FSB_TO_AGBNO(mp, got.br_startblock);
1580 aglen = got.br_blockcount;
1581 error = xfs_reflink_find_shared(pag, tp, agbno, aglen,
1582 &rbno, &rlen, false);
1583 xfs_perag_put(pag);
1584 if (error)
1585 return error;
1586
1587
1588 if (rbno != NULLAGBLOCK) {
1589 *has_shared = true;
1590 return 0;
1591 }
1592 next:
1593 found = xfs_iext_next_extent(ifp, &icur, &got);
1594 }
1595
1596 return 0;
1597 }
1598
1599
1600
1601
1602
1603
1604
1605 int
1606 xfs_reflink_clear_inode_flag(
1607 struct xfs_inode *ip,
1608 struct xfs_trans **tpp)
1609 {
1610 bool needs_flag;
1611 int error = 0;
1612
1613 ASSERT(xfs_is_reflink_inode(ip));
1614
1615 error = xfs_reflink_inode_has_shared_extents(*tpp, ip, &needs_flag);
1616 if (error || needs_flag)
1617 return error;
1618
1619
1620
1621
1622
1623 error = xfs_reflink_cancel_cow_blocks(ip, tpp, 0, XFS_MAX_FILEOFF,
1624 true);
1625 if (error)
1626 return error;
1627
1628
1629 trace_xfs_reflink_unset_inode_flag(ip);
1630 ip->i_diflags2 &= ~XFS_DIFLAG2_REFLINK;
1631 xfs_inode_clear_cowblocks_tag(ip);
1632 xfs_trans_log_inode(*tpp, ip, XFS_ILOG_CORE);
1633
1634 return error;
1635 }
1636
1637
1638
1639
1640
1641 STATIC int
1642 xfs_reflink_try_clear_inode_flag(
1643 struct xfs_inode *ip)
1644 {
1645 struct xfs_mount *mp = ip->i_mount;
1646 struct xfs_trans *tp;
1647 int error = 0;
1648
1649
1650 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, 0, 0, 0, &tp);
1651 if (error)
1652 return error;
1653
1654 xfs_ilock(ip, XFS_ILOCK_EXCL);
1655 xfs_trans_ijoin(tp, ip, 0);
1656
1657 error = xfs_reflink_clear_inode_flag(ip, &tp);
1658 if (error)
1659 goto cancel;
1660
1661 error = xfs_trans_commit(tp);
1662 if (error)
1663 goto out;
1664
1665 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1666 return 0;
1667 cancel:
1668 xfs_trans_cancel(tp);
1669 out:
1670 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1671 return error;
1672 }
1673
1674
1675
1676
1677
1678 int
1679 xfs_reflink_unshare(
1680 struct xfs_inode *ip,
1681 xfs_off_t offset,
1682 xfs_off_t len)
1683 {
1684 struct inode *inode = VFS_I(ip);
1685 int error;
1686
1687 if (!xfs_is_reflink_inode(ip))
1688 return 0;
1689
1690 trace_xfs_reflink_unshare(ip, offset, len);
1691
1692 inode_dio_wait(inode);
1693
1694 error = iomap_file_unshare(inode, offset, len,
1695 &xfs_buffered_write_iomap_ops);
1696 if (error)
1697 goto out;
1698
1699 error = filemap_write_and_wait_range(inode->i_mapping, offset,
1700 offset + len - 1);
1701 if (error)
1702 goto out;
1703
1704
1705 error = xfs_reflink_try_clear_inode_flag(ip);
1706 if (error)
1707 goto out;
1708 return 0;
1709
1710 out:
1711 trace_xfs_reflink_unshare_error(ip, error, _RET_IP_);
1712 return error;
1713 }