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_bit.h"
0013 #include "xfs_sb.h"
0014 #include "xfs_mount.h"
0015 #include "xfs_defer.h"
0016 #include "xfs_dir2.h"
0017 #include "xfs_inode.h"
0018 #include "xfs_btree.h"
0019 #include "xfs_trans.h"
0020 #include "xfs_alloc.h"
0021 #include "xfs_bmap.h"
0022 #include "xfs_bmap_util.h"
0023 #include "xfs_bmap_btree.h"
0024 #include "xfs_rtalloc.h"
0025 #include "xfs_errortag.h"
0026 #include "xfs_error.h"
0027 #include "xfs_quota.h"
0028 #include "xfs_trans_space.h"
0029 #include "xfs_buf_item.h"
0030 #include "xfs_trace.h"
0031 #include "xfs_attr_leaf.h"
0032 #include "xfs_filestream.h"
0033 #include "xfs_rmap.h"
0034 #include "xfs_ag.h"
0035 #include "xfs_ag_resv.h"
0036 #include "xfs_refcount.h"
0037 #include "xfs_icache.h"
0038 #include "xfs_iomap.h"
0039
0040 struct kmem_cache *xfs_bmap_intent_cache;
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050 void
0051 xfs_bmap_compute_maxlevels(
0052 xfs_mount_t *mp,
0053 int whichfork)
0054 {
0055 uint64_t maxblocks;
0056 xfs_extnum_t maxleafents;
0057 int level;
0058 int maxrootrecs;
0059 int minleafrecs;
0060 int minnoderecs;
0061 int sz;
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075 maxleafents = xfs_iext_max_nextents(xfs_has_large_extent_counts(mp),
0076 whichfork);
0077 if (whichfork == XFS_DATA_FORK)
0078 sz = XFS_BMDR_SPACE_CALC(MINDBTPTRS);
0079 else
0080 sz = XFS_BMDR_SPACE_CALC(MINABTPTRS);
0081
0082 maxrootrecs = xfs_bmdr_maxrecs(sz, 0);
0083 minleafrecs = mp->m_bmap_dmnr[0];
0084 minnoderecs = mp->m_bmap_dmnr[1];
0085 maxblocks = howmany_64(maxleafents, minleafrecs);
0086 for (level = 1; maxblocks > 1; level++) {
0087 if (maxblocks <= maxrootrecs)
0088 maxblocks = 1;
0089 else
0090 maxblocks = howmany_64(maxblocks, minnoderecs);
0091 }
0092 mp->m_bm_maxlevels[whichfork] = level;
0093 ASSERT(mp->m_bm_maxlevels[whichfork] <= xfs_bmbt_maxlevels_ondisk());
0094 }
0095
0096 unsigned int
0097 xfs_bmap_compute_attr_offset(
0098 struct xfs_mount *mp)
0099 {
0100 if (mp->m_sb.sb_inodesize == 256)
0101 return XFS_LITINO(mp) - XFS_BMDR_SPACE_CALC(MINABTPTRS);
0102 return XFS_BMDR_SPACE_CALC(6 * MINABTPTRS);
0103 }
0104
0105 STATIC int
0106 xfs_bmbt_lookup_eq(
0107 struct xfs_btree_cur *cur,
0108 struct xfs_bmbt_irec *irec,
0109 int *stat)
0110 {
0111 cur->bc_rec.b = *irec;
0112 return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
0113 }
0114
0115 STATIC int
0116 xfs_bmbt_lookup_first(
0117 struct xfs_btree_cur *cur,
0118 int *stat)
0119 {
0120 cur->bc_rec.b.br_startoff = 0;
0121 cur->bc_rec.b.br_startblock = 0;
0122 cur->bc_rec.b.br_blockcount = 0;
0123 return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
0124 }
0125
0126
0127
0128
0129 static inline bool xfs_bmap_needs_btree(struct xfs_inode *ip, int whichfork)
0130 {
0131 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
0132
0133 return whichfork != XFS_COW_FORK &&
0134 ifp->if_format == XFS_DINODE_FMT_EXTENTS &&
0135 ifp->if_nextents > XFS_IFORK_MAXEXT(ip, whichfork);
0136 }
0137
0138
0139
0140
0141 static inline bool xfs_bmap_wants_extents(struct xfs_inode *ip, int whichfork)
0142 {
0143 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
0144
0145 return whichfork != XFS_COW_FORK &&
0146 ifp->if_format == XFS_DINODE_FMT_BTREE &&
0147 ifp->if_nextents <= XFS_IFORK_MAXEXT(ip, whichfork);
0148 }
0149
0150
0151
0152
0153
0154 STATIC int
0155 xfs_bmbt_update(
0156 struct xfs_btree_cur *cur,
0157 struct xfs_bmbt_irec *irec)
0158 {
0159 union xfs_btree_rec rec;
0160
0161 xfs_bmbt_disk_set_all(&rec.bmbt, irec);
0162 return xfs_btree_update(cur, &rec);
0163 }
0164
0165
0166
0167
0168
0169 STATIC xfs_filblks_t
0170 xfs_bmap_worst_indlen(
0171 xfs_inode_t *ip,
0172 xfs_filblks_t len)
0173 {
0174 int level;
0175 int maxrecs;
0176 xfs_mount_t *mp;
0177 xfs_filblks_t rval;
0178
0179 mp = ip->i_mount;
0180 maxrecs = mp->m_bmap_dmxr[0];
0181 for (level = 0, rval = 0;
0182 level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK);
0183 level++) {
0184 len += maxrecs - 1;
0185 do_div(len, maxrecs);
0186 rval += len;
0187 if (len == 1)
0188 return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) -
0189 level - 1;
0190 if (level == 0)
0191 maxrecs = mp->m_bmap_dmxr[1];
0192 }
0193 return rval;
0194 }
0195
0196
0197
0198
0199 uint
0200 xfs_default_attroffset(
0201 struct xfs_inode *ip)
0202 {
0203 if (ip->i_df.if_format == XFS_DINODE_FMT_DEV)
0204 return roundup(sizeof(xfs_dev_t), 8);
0205 return M_IGEO(ip->i_mount)->attr_fork_offset;
0206 }
0207
0208
0209
0210
0211
0212
0213 STATIC void
0214 xfs_bmap_forkoff_reset(
0215 xfs_inode_t *ip,
0216 int whichfork)
0217 {
0218 if (whichfork == XFS_ATTR_FORK &&
0219 ip->i_df.if_format != XFS_DINODE_FMT_DEV &&
0220 ip->i_df.if_format != XFS_DINODE_FMT_BTREE) {
0221 uint dfl_forkoff = xfs_default_attroffset(ip) >> 3;
0222
0223 if (dfl_forkoff > ip->i_forkoff)
0224 ip->i_forkoff = dfl_forkoff;
0225 }
0226 }
0227
0228 #ifdef DEBUG
0229 STATIC struct xfs_buf *
0230 xfs_bmap_get_bp(
0231 struct xfs_btree_cur *cur,
0232 xfs_fsblock_t bno)
0233 {
0234 struct xfs_log_item *lip;
0235 int i;
0236
0237 if (!cur)
0238 return NULL;
0239
0240 for (i = 0; i < cur->bc_maxlevels; i++) {
0241 if (!cur->bc_levels[i].bp)
0242 break;
0243 if (xfs_buf_daddr(cur->bc_levels[i].bp) == bno)
0244 return cur->bc_levels[i].bp;
0245 }
0246
0247
0248 list_for_each_entry(lip, &cur->bc_tp->t_items, li_trans) {
0249 struct xfs_buf_log_item *bip = (struct xfs_buf_log_item *)lip;
0250
0251 if (bip->bli_item.li_type == XFS_LI_BUF &&
0252 xfs_buf_daddr(bip->bli_buf) == bno)
0253 return bip->bli_buf;
0254 }
0255
0256 return NULL;
0257 }
0258
0259 STATIC void
0260 xfs_check_block(
0261 struct xfs_btree_block *block,
0262 xfs_mount_t *mp,
0263 int root,
0264 short sz)
0265 {
0266 int i, j, dmxr;
0267 __be64 *pp, *thispa;
0268 xfs_bmbt_key_t *prevp, *keyp;
0269
0270 ASSERT(be16_to_cpu(block->bb_level) > 0);
0271
0272 prevp = NULL;
0273 for( i = 1; i <= xfs_btree_get_numrecs(block); i++) {
0274 dmxr = mp->m_bmap_dmxr[0];
0275 keyp = XFS_BMBT_KEY_ADDR(mp, block, i);
0276
0277 if (prevp) {
0278 ASSERT(be64_to_cpu(prevp->br_startoff) <
0279 be64_to_cpu(keyp->br_startoff));
0280 }
0281 prevp = keyp;
0282
0283
0284
0285
0286 if (root)
0287 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, i, sz);
0288 else
0289 pp = XFS_BMBT_PTR_ADDR(mp, block, i, dmxr);
0290
0291 for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) {
0292 if (root)
0293 thispa = XFS_BMAP_BROOT_PTR_ADDR(mp, block, j, sz);
0294 else
0295 thispa = XFS_BMBT_PTR_ADDR(mp, block, j, dmxr);
0296 if (*thispa == *pp) {
0297 xfs_warn(mp, "%s: thispa(%d) == pp(%d) %Ld",
0298 __func__, j, i,
0299 (unsigned long long)be64_to_cpu(*thispa));
0300 xfs_err(mp, "%s: ptrs are equal in node\n",
0301 __func__);
0302 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
0303 }
0304 }
0305 }
0306 }
0307
0308
0309
0310
0311
0312
0313
0314
0315 STATIC void
0316 xfs_bmap_check_leaf_extents(
0317 struct xfs_btree_cur *cur,
0318 xfs_inode_t *ip,
0319 int whichfork)
0320 {
0321 struct xfs_mount *mp = ip->i_mount;
0322 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
0323 struct xfs_btree_block *block;
0324 xfs_fsblock_t bno;
0325 struct xfs_buf *bp;
0326 int error;
0327 xfs_extnum_t i=0, j;
0328 int level;
0329 __be64 *pp;
0330 xfs_bmbt_rec_t *ep;
0331 xfs_bmbt_rec_t last = {0, 0};
0332 xfs_bmbt_rec_t *nextp;
0333 int bp_release = 0;
0334
0335 if (ifp->if_format != XFS_DINODE_FMT_BTREE)
0336 return;
0337
0338
0339 if (ip->i_df.if_nextents > 10000)
0340 return;
0341
0342 bno = NULLFSBLOCK;
0343 block = ifp->if_broot;
0344
0345
0346
0347 level = be16_to_cpu(block->bb_level);
0348 ASSERT(level > 0);
0349 xfs_check_block(block, mp, 1, ifp->if_broot_bytes);
0350 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
0351 bno = be64_to_cpu(*pp);
0352
0353 ASSERT(bno != NULLFSBLOCK);
0354 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
0355 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
0356
0357
0358
0359
0360
0361 while (level-- > 0) {
0362
0363 bp_release = 0;
0364 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
0365 if (!bp) {
0366 bp_release = 1;
0367 error = xfs_btree_read_bufl(mp, NULL, bno, &bp,
0368 XFS_BMAP_BTREE_REF,
0369 &xfs_bmbt_buf_ops);
0370 if (error)
0371 goto error_norelse;
0372 }
0373 block = XFS_BUF_TO_BLOCK(bp);
0374 if (level == 0)
0375 break;
0376
0377
0378
0379
0380
0381
0382 xfs_check_block(block, mp, 0, 0);
0383 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
0384 bno = be64_to_cpu(*pp);
0385 if (XFS_IS_CORRUPT(mp, !xfs_verify_fsbno(mp, bno))) {
0386 error = -EFSCORRUPTED;
0387 goto error0;
0388 }
0389 if (bp_release) {
0390 bp_release = 0;
0391 xfs_trans_brelse(NULL, bp);
0392 }
0393 }
0394
0395
0396
0397
0398 i = 0;
0399
0400
0401
0402
0403 for (;;) {
0404 xfs_fsblock_t nextbno;
0405 xfs_extnum_t num_recs;
0406
0407
0408 num_recs = xfs_btree_get_numrecs(block);
0409
0410
0411
0412
0413
0414 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
0415
0416
0417
0418
0419
0420
0421
0422 ep = XFS_BMBT_REC_ADDR(mp, block, 1);
0423 if (i) {
0424 ASSERT(xfs_bmbt_disk_get_startoff(&last) +
0425 xfs_bmbt_disk_get_blockcount(&last) <=
0426 xfs_bmbt_disk_get_startoff(ep));
0427 }
0428 for (j = 1; j < num_recs; j++) {
0429 nextp = XFS_BMBT_REC_ADDR(mp, block, j + 1);
0430 ASSERT(xfs_bmbt_disk_get_startoff(ep) +
0431 xfs_bmbt_disk_get_blockcount(ep) <=
0432 xfs_bmbt_disk_get_startoff(nextp));
0433 ep = nextp;
0434 }
0435
0436 last = *ep;
0437 i += num_recs;
0438 if (bp_release) {
0439 bp_release = 0;
0440 xfs_trans_brelse(NULL, bp);
0441 }
0442 bno = nextbno;
0443
0444
0445
0446 if (bno == NULLFSBLOCK)
0447 break;
0448
0449 bp_release = 0;
0450 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
0451 if (!bp) {
0452 bp_release = 1;
0453 error = xfs_btree_read_bufl(mp, NULL, bno, &bp,
0454 XFS_BMAP_BTREE_REF,
0455 &xfs_bmbt_buf_ops);
0456 if (error)
0457 goto error_norelse;
0458 }
0459 block = XFS_BUF_TO_BLOCK(bp);
0460 }
0461
0462 return;
0463
0464 error0:
0465 xfs_warn(mp, "%s: at error0", __func__);
0466 if (bp_release)
0467 xfs_trans_brelse(NULL, bp);
0468 error_norelse:
0469 xfs_warn(mp, "%s: BAD after btree leaves for %llu extents",
0470 __func__, i);
0471 xfs_err(mp, "%s: CORRUPTED BTREE OR SOMETHING", __func__);
0472 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
0473 return;
0474 }
0475
0476
0477
0478
0479
0480
0481
0482 STATIC void
0483 xfs_bmap_validate_ret(
0484 xfs_fileoff_t bno,
0485 xfs_filblks_t len,
0486 uint32_t flags,
0487 xfs_bmbt_irec_t *mval,
0488 int nmap,
0489 int ret_nmap)
0490 {
0491 int i;
0492
0493 ASSERT(ret_nmap <= nmap);
0494
0495 for (i = 0; i < ret_nmap; i++) {
0496 ASSERT(mval[i].br_blockcount > 0);
0497 if (!(flags & XFS_BMAPI_ENTIRE)) {
0498 ASSERT(mval[i].br_startoff >= bno);
0499 ASSERT(mval[i].br_blockcount <= len);
0500 ASSERT(mval[i].br_startoff + mval[i].br_blockcount <=
0501 bno + len);
0502 } else {
0503 ASSERT(mval[i].br_startoff < bno + len);
0504 ASSERT(mval[i].br_startoff + mval[i].br_blockcount >
0505 bno);
0506 }
0507 ASSERT(i == 0 ||
0508 mval[i - 1].br_startoff + mval[i - 1].br_blockcount ==
0509 mval[i].br_startoff);
0510 ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK &&
0511 mval[i].br_startblock != HOLESTARTBLOCK);
0512 ASSERT(mval[i].br_state == XFS_EXT_NORM ||
0513 mval[i].br_state == XFS_EXT_UNWRITTEN);
0514 }
0515 }
0516
0517 #else
0518 #define xfs_bmap_check_leaf_extents(cur, ip, whichfork) do { } while (0)
0519 #define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap) do { } while (0)
0520 #endif
0521
0522
0523
0524
0525
0526
0527
0528
0529
0530
0531
0532
0533 STATIC int
0534 xfs_bmap_btree_to_extents(
0535 struct xfs_trans *tp,
0536 struct xfs_inode *ip,
0537 struct xfs_btree_cur *cur,
0538 int *logflagsp,
0539 int whichfork)
0540 {
0541 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
0542 struct xfs_mount *mp = ip->i_mount;
0543 struct xfs_btree_block *rblock = ifp->if_broot;
0544 struct xfs_btree_block *cblock;
0545 xfs_fsblock_t cbno;
0546 struct xfs_buf *cbp;
0547 int error;
0548 __be64 *pp;
0549 struct xfs_owner_info oinfo;
0550
0551
0552 if (!xfs_bmap_wants_extents(ip, whichfork))
0553 return 0;
0554
0555 ASSERT(cur);
0556 ASSERT(whichfork != XFS_COW_FORK);
0557 ASSERT(ifp->if_format == XFS_DINODE_FMT_BTREE);
0558 ASSERT(be16_to_cpu(rblock->bb_level) == 1);
0559 ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1);
0560 ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0) == 1);
0561
0562 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, ifp->if_broot_bytes);
0563 cbno = be64_to_cpu(*pp);
0564 #ifdef DEBUG
0565 if (XFS_IS_CORRUPT(cur->bc_mp, !xfs_btree_check_lptr(cur, cbno, 1)))
0566 return -EFSCORRUPTED;
0567 #endif
0568 error = xfs_btree_read_bufl(mp, tp, cbno, &cbp, XFS_BMAP_BTREE_REF,
0569 &xfs_bmbt_buf_ops);
0570 if (error)
0571 return error;
0572 cblock = XFS_BUF_TO_BLOCK(cbp);
0573 if ((error = xfs_btree_check_block(cur, cblock, 0, cbp)))
0574 return error;
0575 xfs_rmap_ino_bmbt_owner(&oinfo, ip->i_ino, whichfork);
0576 xfs_free_extent_later(cur->bc_tp, cbno, 1, &oinfo);
0577 ip->i_nblocks--;
0578 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
0579 xfs_trans_binval(tp, cbp);
0580 if (cur->bc_levels[0].bp == cbp)
0581 cur->bc_levels[0].bp = NULL;
0582 xfs_iroot_realloc(ip, -1, whichfork);
0583 ASSERT(ifp->if_broot == NULL);
0584 ifp->if_format = XFS_DINODE_FMT_EXTENTS;
0585 *logflagsp |= XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
0586 return 0;
0587 }
0588
0589
0590
0591
0592
0593 STATIC int
0594 xfs_bmap_extents_to_btree(
0595 struct xfs_trans *tp,
0596 struct xfs_inode *ip,
0597 struct xfs_btree_cur **curp,
0598 int wasdel,
0599 int *logflagsp,
0600 int whichfork)
0601 {
0602 struct xfs_btree_block *ablock;
0603 struct xfs_buf *abp;
0604 struct xfs_alloc_arg args;
0605 struct xfs_bmbt_rec *arp;
0606 struct xfs_btree_block *block;
0607 struct xfs_btree_cur *cur;
0608 int error;
0609 struct xfs_ifork *ifp;
0610 struct xfs_bmbt_key *kp;
0611 struct xfs_mount *mp;
0612 xfs_bmbt_ptr_t *pp;
0613 struct xfs_iext_cursor icur;
0614 struct xfs_bmbt_irec rec;
0615 xfs_extnum_t cnt = 0;
0616
0617 mp = ip->i_mount;
0618 ASSERT(whichfork != XFS_COW_FORK);
0619 ifp = xfs_ifork_ptr(ip, whichfork);
0620 ASSERT(ifp->if_format == XFS_DINODE_FMT_EXTENTS);
0621
0622
0623
0624
0625
0626 xfs_iroot_realloc(ip, 1, whichfork);
0627
0628
0629
0630
0631 block = ifp->if_broot;
0632 xfs_btree_init_block_int(mp, block, XFS_BUF_DADDR_NULL,
0633 XFS_BTNUM_BMAP, 1, 1, ip->i_ino,
0634 XFS_BTREE_LONG_PTRS);
0635
0636
0637
0638 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
0639 cur->bc_ino.flags = wasdel ? XFS_BTCUR_BMBT_WASDEL : 0;
0640
0641
0642
0643 ifp->if_format = XFS_DINODE_FMT_BTREE;
0644 memset(&args, 0, sizeof(args));
0645 args.tp = tp;
0646 args.mp = mp;
0647 xfs_rmap_ino_bmbt_owner(&args.oinfo, ip->i_ino, whichfork);
0648 if (tp->t_firstblock == NULLFSBLOCK) {
0649 args.type = XFS_ALLOCTYPE_START_BNO;
0650 args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino);
0651 } else if (tp->t_flags & XFS_TRANS_LOWMODE) {
0652 args.type = XFS_ALLOCTYPE_START_BNO;
0653 args.fsbno = tp->t_firstblock;
0654 } else {
0655 args.type = XFS_ALLOCTYPE_NEAR_BNO;
0656 args.fsbno = tp->t_firstblock;
0657 }
0658 args.minlen = args.maxlen = args.prod = 1;
0659 args.wasdel = wasdel;
0660 *logflagsp = 0;
0661 error = xfs_alloc_vextent(&args);
0662 if (error)
0663 goto out_root_realloc;
0664
0665 if (WARN_ON_ONCE(args.fsbno == NULLFSBLOCK)) {
0666 error = -ENOSPC;
0667 goto out_root_realloc;
0668 }
0669
0670
0671
0672
0673 ASSERT(tp->t_firstblock == NULLFSBLOCK ||
0674 args.agno >= XFS_FSB_TO_AGNO(mp, tp->t_firstblock));
0675 tp->t_firstblock = args.fsbno;
0676 cur->bc_ino.allocated++;
0677 ip->i_nblocks++;
0678 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
0679 error = xfs_trans_get_buf(tp, mp->m_ddev_targp,
0680 XFS_FSB_TO_DADDR(mp, args.fsbno),
0681 mp->m_bsize, 0, &abp);
0682 if (error)
0683 goto out_unreserve_dquot;
0684
0685
0686
0687
0688 abp->b_ops = &xfs_bmbt_buf_ops;
0689 ablock = XFS_BUF_TO_BLOCK(abp);
0690 xfs_btree_init_block_int(mp, ablock, xfs_buf_daddr(abp),
0691 XFS_BTNUM_BMAP, 0, 0, ip->i_ino,
0692 XFS_BTREE_LONG_PTRS);
0693
0694 for_each_xfs_iext(ifp, &icur, &rec) {
0695 if (isnullstartblock(rec.br_startblock))
0696 continue;
0697 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1 + cnt);
0698 xfs_bmbt_disk_set_all(arp, &rec);
0699 cnt++;
0700 }
0701 ASSERT(cnt == ifp->if_nextents);
0702 xfs_btree_set_numrecs(ablock, cnt);
0703
0704
0705
0706
0707 kp = XFS_BMBT_KEY_ADDR(mp, block, 1);
0708 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
0709 kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp));
0710 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur,
0711 be16_to_cpu(block->bb_level)));
0712 *pp = cpu_to_be64(args.fsbno);
0713
0714
0715
0716
0717
0718 xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS);
0719 xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs));
0720 ASSERT(*curp == NULL);
0721 *curp = cur;
0722 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork);
0723 return 0;
0724
0725 out_unreserve_dquot:
0726 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
0727 out_root_realloc:
0728 xfs_iroot_realloc(ip, -1, whichfork);
0729 ifp->if_format = XFS_DINODE_FMT_EXTENTS;
0730 ASSERT(ifp->if_broot == NULL);
0731 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
0732
0733 return error;
0734 }
0735
0736
0737
0738
0739
0740
0741
0742 void
0743 xfs_bmap_local_to_extents_empty(
0744 struct xfs_trans *tp,
0745 struct xfs_inode *ip,
0746 int whichfork)
0747 {
0748 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
0749
0750 ASSERT(whichfork != XFS_COW_FORK);
0751 ASSERT(ifp->if_format == XFS_DINODE_FMT_LOCAL);
0752 ASSERT(ifp->if_bytes == 0);
0753 ASSERT(ifp->if_nextents == 0);
0754
0755 xfs_bmap_forkoff_reset(ip, whichfork);
0756 ifp->if_u1.if_root = NULL;
0757 ifp->if_height = 0;
0758 ifp->if_format = XFS_DINODE_FMT_EXTENTS;
0759 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
0760 }
0761
0762
0763 STATIC int
0764 xfs_bmap_local_to_extents(
0765 xfs_trans_t *tp,
0766 xfs_inode_t *ip,
0767 xfs_extlen_t total,
0768 int *logflagsp,
0769 int whichfork,
0770 void (*init_fn)(struct xfs_trans *tp,
0771 struct xfs_buf *bp,
0772 struct xfs_inode *ip,
0773 struct xfs_ifork *ifp))
0774 {
0775 int error = 0;
0776 int flags;
0777 struct xfs_ifork *ifp;
0778 xfs_alloc_arg_t args;
0779 struct xfs_buf *bp;
0780 struct xfs_bmbt_irec rec;
0781 struct xfs_iext_cursor icur;
0782
0783
0784
0785
0786
0787 ASSERT(!(S_ISREG(VFS_I(ip)->i_mode) && whichfork == XFS_DATA_FORK));
0788 ifp = xfs_ifork_ptr(ip, whichfork);
0789 ASSERT(ifp->if_format == XFS_DINODE_FMT_LOCAL);
0790
0791 if (!ifp->if_bytes) {
0792 xfs_bmap_local_to_extents_empty(tp, ip, whichfork);
0793 flags = XFS_ILOG_CORE;
0794 goto done;
0795 }
0796
0797 flags = 0;
0798 error = 0;
0799 memset(&args, 0, sizeof(args));
0800 args.tp = tp;
0801 args.mp = ip->i_mount;
0802 xfs_rmap_ino_owner(&args.oinfo, ip->i_ino, whichfork, 0);
0803
0804
0805
0806
0807 if (tp->t_firstblock == NULLFSBLOCK) {
0808 args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino);
0809 args.type = XFS_ALLOCTYPE_START_BNO;
0810 } else {
0811 args.fsbno = tp->t_firstblock;
0812 args.type = XFS_ALLOCTYPE_NEAR_BNO;
0813 }
0814 args.total = total;
0815 args.minlen = args.maxlen = args.prod = 1;
0816 error = xfs_alloc_vextent(&args);
0817 if (error)
0818 goto done;
0819
0820
0821 ASSERT(args.fsbno != NULLFSBLOCK);
0822 ASSERT(args.len == 1);
0823 tp->t_firstblock = args.fsbno;
0824 error = xfs_trans_get_buf(tp, args.mp->m_ddev_targp,
0825 XFS_FSB_TO_DADDR(args.mp, args.fsbno),
0826 args.mp->m_bsize, 0, &bp);
0827 if (error)
0828 goto done;
0829
0830
0831
0832
0833
0834
0835
0836
0837
0838 init_fn(tp, bp, ip, ifp);
0839
0840
0841 xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
0842 xfs_bmap_local_to_extents_empty(tp, ip, whichfork);
0843 flags |= XFS_ILOG_CORE;
0844
0845 ifp->if_u1.if_root = NULL;
0846 ifp->if_height = 0;
0847
0848 rec.br_startoff = 0;
0849 rec.br_startblock = args.fsbno;
0850 rec.br_blockcount = 1;
0851 rec.br_state = XFS_EXT_NORM;
0852 xfs_iext_first(ifp, &icur);
0853 xfs_iext_insert(ip, &icur, &rec, 0);
0854
0855 ifp->if_nextents = 1;
0856 ip->i_nblocks = 1;
0857 xfs_trans_mod_dquot_byino(tp, ip,
0858 XFS_TRANS_DQ_BCOUNT, 1L);
0859 flags |= xfs_ilog_fext(whichfork);
0860
0861 done:
0862 *logflagsp = flags;
0863 return error;
0864 }
0865
0866
0867
0868
0869 STATIC int
0870 xfs_bmap_add_attrfork_btree(
0871 xfs_trans_t *tp,
0872 xfs_inode_t *ip,
0873 int *flags)
0874 {
0875 struct xfs_btree_block *block = ip->i_df.if_broot;
0876 struct xfs_btree_cur *cur;
0877 int error;
0878 xfs_mount_t *mp;
0879 int stat;
0880
0881 mp = ip->i_mount;
0882
0883 if (XFS_BMAP_BMDR_SPACE(block) <= xfs_inode_data_fork_size(ip))
0884 *flags |= XFS_ILOG_DBROOT;
0885 else {
0886 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
0887 error = xfs_bmbt_lookup_first(cur, &stat);
0888 if (error)
0889 goto error0;
0890
0891 if (XFS_IS_CORRUPT(mp, stat != 1)) {
0892 error = -EFSCORRUPTED;
0893 goto error0;
0894 }
0895 if ((error = xfs_btree_new_iroot(cur, flags, &stat)))
0896 goto error0;
0897 if (stat == 0) {
0898 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
0899 return -ENOSPC;
0900 }
0901 cur->bc_ino.allocated = 0;
0902 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
0903 }
0904 return 0;
0905 error0:
0906 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
0907 return error;
0908 }
0909
0910
0911
0912
0913 STATIC int
0914 xfs_bmap_add_attrfork_extents(
0915 struct xfs_trans *tp,
0916 struct xfs_inode *ip,
0917 int *flags)
0918 {
0919 struct xfs_btree_cur *cur;
0920 int error;
0921
0922 if (ip->i_df.if_nextents * sizeof(struct xfs_bmbt_rec) <=
0923 xfs_inode_data_fork_size(ip))
0924 return 0;
0925 cur = NULL;
0926 error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0, flags,
0927 XFS_DATA_FORK);
0928 if (cur) {
0929 cur->bc_ino.allocated = 0;
0930 xfs_btree_del_cursor(cur, error);
0931 }
0932 return error;
0933 }
0934
0935
0936
0937
0938
0939
0940
0941
0942
0943
0944
0945
0946 STATIC int
0947 xfs_bmap_add_attrfork_local(
0948 struct xfs_trans *tp,
0949 struct xfs_inode *ip,
0950 int *flags)
0951 {
0952 struct xfs_da_args dargs;
0953
0954 if (ip->i_df.if_bytes <= xfs_inode_data_fork_size(ip))
0955 return 0;
0956
0957 if (S_ISDIR(VFS_I(ip)->i_mode)) {
0958 memset(&dargs, 0, sizeof(dargs));
0959 dargs.geo = ip->i_mount->m_dir_geo;
0960 dargs.dp = ip;
0961 dargs.total = dargs.geo->fsbcount;
0962 dargs.whichfork = XFS_DATA_FORK;
0963 dargs.trans = tp;
0964 return xfs_dir2_sf_to_block(&dargs);
0965 }
0966
0967 if (S_ISLNK(VFS_I(ip)->i_mode))
0968 return xfs_bmap_local_to_extents(tp, ip, 1, flags,
0969 XFS_DATA_FORK,
0970 xfs_symlink_local_to_remote);
0971
0972
0973 ASSERT(0);
0974 return -EFSCORRUPTED;
0975 }
0976
0977
0978
0979
0980 static int
0981 xfs_bmap_set_attrforkoff(
0982 struct xfs_inode *ip,
0983 int size,
0984 int *version)
0985 {
0986 int default_size = xfs_default_attroffset(ip) >> 3;
0987
0988 switch (ip->i_df.if_format) {
0989 case XFS_DINODE_FMT_DEV:
0990 ip->i_forkoff = default_size;
0991 break;
0992 case XFS_DINODE_FMT_LOCAL:
0993 case XFS_DINODE_FMT_EXTENTS:
0994 case XFS_DINODE_FMT_BTREE:
0995 ip->i_forkoff = xfs_attr_shortform_bytesfit(ip, size);
0996 if (!ip->i_forkoff)
0997 ip->i_forkoff = default_size;
0998 else if (xfs_has_attr2(ip->i_mount) && version)
0999 *version = 2;
1000 break;
1001 default:
1002 ASSERT(0);
1003 return -EINVAL;
1004 }
1005
1006 return 0;
1007 }
1008
1009
1010
1011
1012
1013 int
1014 xfs_bmap_add_attrfork(
1015 xfs_inode_t *ip,
1016 int size,
1017 int rsvd)
1018 {
1019 xfs_mount_t *mp;
1020 xfs_trans_t *tp;
1021 int blks;
1022 int version = 1;
1023 int logflags;
1024 int error;
1025
1026 ASSERT(xfs_inode_has_attr_fork(ip) == 0);
1027
1028 mp = ip->i_mount;
1029 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
1030
1031 blks = XFS_ADDAFORK_SPACE_RES(mp);
1032
1033 error = xfs_trans_alloc_inode(ip, &M_RES(mp)->tr_addafork, blks, 0,
1034 rsvd, &tp);
1035 if (error)
1036 return error;
1037 if (xfs_inode_has_attr_fork(ip))
1038 goto trans_cancel;
1039
1040 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1041 error = xfs_bmap_set_attrforkoff(ip, size, &version);
1042 if (error)
1043 goto trans_cancel;
1044
1045 xfs_ifork_init_attr(ip, XFS_DINODE_FMT_EXTENTS, 0);
1046 logflags = 0;
1047 switch (ip->i_df.if_format) {
1048 case XFS_DINODE_FMT_LOCAL:
1049 error = xfs_bmap_add_attrfork_local(tp, ip, &logflags);
1050 break;
1051 case XFS_DINODE_FMT_EXTENTS:
1052 error = xfs_bmap_add_attrfork_extents(tp, ip, &logflags);
1053 break;
1054 case XFS_DINODE_FMT_BTREE:
1055 error = xfs_bmap_add_attrfork_btree(tp, ip, &logflags);
1056 break;
1057 default:
1058 error = 0;
1059 break;
1060 }
1061 if (logflags)
1062 xfs_trans_log_inode(tp, ip, logflags);
1063 if (error)
1064 goto trans_cancel;
1065 if (!xfs_has_attr(mp) ||
1066 (!xfs_has_attr2(mp) && version == 2)) {
1067 bool log_sb = false;
1068
1069 spin_lock(&mp->m_sb_lock);
1070 if (!xfs_has_attr(mp)) {
1071 xfs_add_attr(mp);
1072 log_sb = true;
1073 }
1074 if (!xfs_has_attr2(mp) && version == 2) {
1075 xfs_add_attr2(mp);
1076 log_sb = true;
1077 }
1078 spin_unlock(&mp->m_sb_lock);
1079 if (log_sb)
1080 xfs_log_sb(tp);
1081 }
1082
1083 error = xfs_trans_commit(tp);
1084 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1085 return error;
1086
1087 trans_cancel:
1088 xfs_trans_cancel(tp);
1089 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1090 return error;
1091 }
1092
1093
1094
1095
1096
1097 struct xfs_iread_state {
1098 struct xfs_iext_cursor icur;
1099 xfs_extnum_t loaded;
1100 };
1101
1102
1103 static int
1104 xfs_iread_bmbt_block(
1105 struct xfs_btree_cur *cur,
1106 int level,
1107 void *priv)
1108 {
1109 struct xfs_iread_state *ir = priv;
1110 struct xfs_mount *mp = cur->bc_mp;
1111 struct xfs_inode *ip = cur->bc_ino.ip;
1112 struct xfs_btree_block *block;
1113 struct xfs_buf *bp;
1114 struct xfs_bmbt_rec *frp;
1115 xfs_extnum_t num_recs;
1116 xfs_extnum_t j;
1117 int whichfork = cur->bc_ino.whichfork;
1118 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
1119
1120 block = xfs_btree_get_block(cur, level, &bp);
1121
1122
1123 num_recs = xfs_btree_get_numrecs(block);
1124 if (unlikely(ir->loaded + num_recs > ifp->if_nextents)) {
1125 xfs_warn(ip->i_mount, "corrupt dinode %llu, (btree extents).",
1126 (unsigned long long)ip->i_ino);
1127 xfs_inode_verifier_error(ip, -EFSCORRUPTED, __func__, block,
1128 sizeof(*block), __this_address);
1129 return -EFSCORRUPTED;
1130 }
1131
1132
1133 frp = XFS_BMBT_REC_ADDR(mp, block, 1);
1134 for (j = 0; j < num_recs; j++, frp++, ir->loaded++) {
1135 struct xfs_bmbt_irec new;
1136 xfs_failaddr_t fa;
1137
1138 xfs_bmbt_disk_get_all(frp, &new);
1139 fa = xfs_bmap_validate_extent(ip, whichfork, &new);
1140 if (fa) {
1141 xfs_inode_verifier_error(ip, -EFSCORRUPTED,
1142 "xfs_iread_extents(2)", frp,
1143 sizeof(*frp), fa);
1144 return -EFSCORRUPTED;
1145 }
1146 xfs_iext_insert(ip, &ir->icur, &new,
1147 xfs_bmap_fork_to_state(whichfork));
1148 trace_xfs_read_extent(ip, &ir->icur,
1149 xfs_bmap_fork_to_state(whichfork), _THIS_IP_);
1150 xfs_iext_next(ifp, &ir->icur);
1151 }
1152
1153 return 0;
1154 }
1155
1156
1157
1158
1159 int
1160 xfs_iread_extents(
1161 struct xfs_trans *tp,
1162 struct xfs_inode *ip,
1163 int whichfork)
1164 {
1165 struct xfs_iread_state ir;
1166 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
1167 struct xfs_mount *mp = ip->i_mount;
1168 struct xfs_btree_cur *cur;
1169 int error;
1170
1171 if (!xfs_need_iread_extents(ifp))
1172 return 0;
1173
1174 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
1175
1176 ir.loaded = 0;
1177 xfs_iext_first(ifp, &ir.icur);
1178 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
1179 error = xfs_btree_visit_blocks(cur, xfs_iread_bmbt_block,
1180 XFS_BTREE_VISIT_RECORDS, &ir);
1181 xfs_btree_del_cursor(cur, error);
1182 if (error)
1183 goto out;
1184
1185 if (XFS_IS_CORRUPT(mp, ir.loaded != ifp->if_nextents)) {
1186 error = -EFSCORRUPTED;
1187 goto out;
1188 }
1189 ASSERT(ir.loaded == xfs_iext_count(ifp));
1190 return 0;
1191 out:
1192 xfs_iext_destroy(ifp);
1193 return error;
1194 }
1195
1196
1197
1198
1199
1200
1201
1202 int
1203 xfs_bmap_first_unused(
1204 struct xfs_trans *tp,
1205 struct xfs_inode *ip,
1206 xfs_extlen_t len,
1207 xfs_fileoff_t *first_unused,
1208 int whichfork)
1209 {
1210 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
1211 struct xfs_bmbt_irec got;
1212 struct xfs_iext_cursor icur;
1213 xfs_fileoff_t lastaddr = 0;
1214 xfs_fileoff_t lowest, max;
1215 int error;
1216
1217 if (ifp->if_format == XFS_DINODE_FMT_LOCAL) {
1218 *first_unused = 0;
1219 return 0;
1220 }
1221
1222 ASSERT(xfs_ifork_has_extents(ifp));
1223
1224 error = xfs_iread_extents(tp, ip, whichfork);
1225 if (error)
1226 return error;
1227
1228 lowest = max = *first_unused;
1229 for_each_xfs_iext(ifp, &icur, &got) {
1230
1231
1232
1233 if (got.br_startoff >= lowest + len &&
1234 got.br_startoff - max >= len)
1235 break;
1236 lastaddr = got.br_startoff + got.br_blockcount;
1237 max = XFS_FILEOFF_MAX(lastaddr, lowest);
1238 }
1239
1240 *first_unused = max;
1241 return 0;
1242 }
1243
1244
1245
1246
1247
1248
1249
1250 int
1251 xfs_bmap_last_before(
1252 struct xfs_trans *tp,
1253 struct xfs_inode *ip,
1254 xfs_fileoff_t *last_block,
1255 int whichfork)
1256 {
1257 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
1258 struct xfs_bmbt_irec got;
1259 struct xfs_iext_cursor icur;
1260 int error;
1261
1262 switch (ifp->if_format) {
1263 case XFS_DINODE_FMT_LOCAL:
1264 *last_block = 0;
1265 return 0;
1266 case XFS_DINODE_FMT_BTREE:
1267 case XFS_DINODE_FMT_EXTENTS:
1268 break;
1269 default:
1270 ASSERT(0);
1271 return -EFSCORRUPTED;
1272 }
1273
1274 error = xfs_iread_extents(tp, ip, whichfork);
1275 if (error)
1276 return error;
1277
1278 if (!xfs_iext_lookup_extent_before(ip, ifp, last_block, &icur, &got))
1279 *last_block = 0;
1280 return 0;
1281 }
1282
1283 int
1284 xfs_bmap_last_extent(
1285 struct xfs_trans *tp,
1286 struct xfs_inode *ip,
1287 int whichfork,
1288 struct xfs_bmbt_irec *rec,
1289 int *is_empty)
1290 {
1291 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
1292 struct xfs_iext_cursor icur;
1293 int error;
1294
1295 error = xfs_iread_extents(tp, ip, whichfork);
1296 if (error)
1297 return error;
1298
1299 xfs_iext_last(ifp, &icur);
1300 if (!xfs_iext_get_extent(ifp, &icur, rec))
1301 *is_empty = 1;
1302 else
1303 *is_empty = 0;
1304 return 0;
1305 }
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316 STATIC int
1317 xfs_bmap_isaeof(
1318 struct xfs_bmalloca *bma,
1319 int whichfork)
1320 {
1321 struct xfs_bmbt_irec rec;
1322 int is_empty;
1323 int error;
1324
1325 bma->aeof = false;
1326 error = xfs_bmap_last_extent(NULL, bma->ip, whichfork, &rec,
1327 &is_empty);
1328 if (error)
1329 return error;
1330
1331 if (is_empty) {
1332 bma->aeof = true;
1333 return 0;
1334 }
1335
1336
1337
1338
1339
1340 bma->aeof = bma->offset >= rec.br_startoff + rec.br_blockcount ||
1341 (bma->offset >= rec.br_startoff &&
1342 isnullstartblock(rec.br_startblock));
1343 return 0;
1344 }
1345
1346
1347
1348
1349
1350
1351 int
1352 xfs_bmap_last_offset(
1353 struct xfs_inode *ip,
1354 xfs_fileoff_t *last_block,
1355 int whichfork)
1356 {
1357 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
1358 struct xfs_bmbt_irec rec;
1359 int is_empty;
1360 int error;
1361
1362 *last_block = 0;
1363
1364 if (ifp->if_format == XFS_DINODE_FMT_LOCAL)
1365 return 0;
1366
1367 if (XFS_IS_CORRUPT(ip->i_mount, !xfs_ifork_has_extents(ifp)))
1368 return -EFSCORRUPTED;
1369
1370 error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, &is_empty);
1371 if (error || is_empty)
1372 return error;
1373
1374 *last_block = rec.br_startoff + rec.br_blockcount;
1375 return 0;
1376 }
1377
1378
1379
1380
1381
1382
1383
1384
1385 STATIC int
1386 xfs_bmap_add_extent_delay_real(
1387 struct xfs_bmalloca *bma,
1388 int whichfork)
1389 {
1390 struct xfs_mount *mp = bma->ip->i_mount;
1391 struct xfs_ifork *ifp = xfs_ifork_ptr(bma->ip, whichfork);
1392 struct xfs_bmbt_irec *new = &bma->got;
1393 int error;
1394 int i;
1395 xfs_fileoff_t new_endoff;
1396 xfs_bmbt_irec_t r[3];
1397
1398 int rval=0;
1399 uint32_t state = xfs_bmap_fork_to_state(whichfork);
1400 xfs_filblks_t da_new;
1401 xfs_filblks_t da_old;
1402 xfs_filblks_t temp=0;
1403 int tmp_rval;
1404 struct xfs_bmbt_irec old;
1405
1406 ASSERT(whichfork != XFS_ATTR_FORK);
1407 ASSERT(!isnullstartblock(new->br_startblock));
1408 ASSERT(!bma->cur ||
1409 (bma->cur->bc_ino.flags & XFS_BTCUR_BMBT_WASDEL));
1410
1411 XFS_STATS_INC(mp, xs_add_exlist);
1412
1413 #define LEFT r[0]
1414 #define RIGHT r[1]
1415 #define PREV r[2]
1416
1417
1418
1419
1420 xfs_iext_get_extent(ifp, &bma->icur, &PREV);
1421 new_endoff = new->br_startoff + new->br_blockcount;
1422 ASSERT(isnullstartblock(PREV.br_startblock));
1423 ASSERT(PREV.br_startoff <= new->br_startoff);
1424 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
1425
1426 da_old = startblockval(PREV.br_startblock);
1427 da_new = 0;
1428
1429
1430
1431
1432
1433 if (PREV.br_startoff == new->br_startoff)
1434 state |= BMAP_LEFT_FILLING;
1435 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
1436 state |= BMAP_RIGHT_FILLING;
1437
1438
1439
1440
1441
1442 if (xfs_iext_peek_prev_extent(ifp, &bma->icur, &LEFT)) {
1443 state |= BMAP_LEFT_VALID;
1444 if (isnullstartblock(LEFT.br_startblock))
1445 state |= BMAP_LEFT_DELAY;
1446 }
1447
1448 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1449 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1450 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
1451 LEFT.br_state == new->br_state &&
1452 LEFT.br_blockcount + new->br_blockcount <= XFS_MAX_BMBT_EXTLEN)
1453 state |= BMAP_LEFT_CONTIG;
1454
1455
1456
1457
1458
1459
1460 if (xfs_iext_peek_next_extent(ifp, &bma->icur, &RIGHT)) {
1461 state |= BMAP_RIGHT_VALID;
1462 if (isnullstartblock(RIGHT.br_startblock))
1463 state |= BMAP_RIGHT_DELAY;
1464 }
1465
1466 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1467 new_endoff == RIGHT.br_startoff &&
1468 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
1469 new->br_state == RIGHT.br_state &&
1470 new->br_blockcount + RIGHT.br_blockcount <= XFS_MAX_BMBT_EXTLEN &&
1471 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1472 BMAP_RIGHT_FILLING)) !=
1473 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1474 BMAP_RIGHT_FILLING) ||
1475 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
1476 <= XFS_MAX_BMBT_EXTLEN))
1477 state |= BMAP_RIGHT_CONTIG;
1478
1479 error = 0;
1480
1481
1482
1483 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1484 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
1485 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1486 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1487
1488
1489
1490
1491 LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount;
1492
1493 xfs_iext_remove(bma->ip, &bma->icur, state);
1494 xfs_iext_remove(bma->ip, &bma->icur, state);
1495 xfs_iext_prev(ifp, &bma->icur);
1496 xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT);
1497 ifp->if_nextents--;
1498
1499 if (bma->cur == NULL)
1500 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1501 else {
1502 rval = XFS_ILOG_CORE;
1503 error = xfs_bmbt_lookup_eq(bma->cur, &RIGHT, &i);
1504 if (error)
1505 goto done;
1506 if (XFS_IS_CORRUPT(mp, i != 1)) {
1507 error = -EFSCORRUPTED;
1508 goto done;
1509 }
1510 error = xfs_btree_delete(bma->cur, &i);
1511 if (error)
1512 goto done;
1513 if (XFS_IS_CORRUPT(mp, i != 1)) {
1514 error = -EFSCORRUPTED;
1515 goto done;
1516 }
1517 error = xfs_btree_decrement(bma->cur, 0, &i);
1518 if (error)
1519 goto done;
1520 if (XFS_IS_CORRUPT(mp, i != 1)) {
1521 error = -EFSCORRUPTED;
1522 goto done;
1523 }
1524 error = xfs_bmbt_update(bma->cur, &LEFT);
1525 if (error)
1526 goto done;
1527 }
1528 break;
1529
1530 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1531
1532
1533
1534
1535 old = LEFT;
1536 LEFT.br_blockcount += PREV.br_blockcount;
1537
1538 xfs_iext_remove(bma->ip, &bma->icur, state);
1539 xfs_iext_prev(ifp, &bma->icur);
1540 xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT);
1541
1542 if (bma->cur == NULL)
1543 rval = XFS_ILOG_DEXT;
1544 else {
1545 rval = 0;
1546 error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
1547 if (error)
1548 goto done;
1549 if (XFS_IS_CORRUPT(mp, i != 1)) {
1550 error = -EFSCORRUPTED;
1551 goto done;
1552 }
1553 error = xfs_bmbt_update(bma->cur, &LEFT);
1554 if (error)
1555 goto done;
1556 }
1557 break;
1558
1559 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1560
1561
1562
1563
1564
1565
1566 PREV.br_startblock = new->br_startblock;
1567 PREV.br_blockcount += RIGHT.br_blockcount;
1568 PREV.br_state = new->br_state;
1569
1570 xfs_iext_next(ifp, &bma->icur);
1571 xfs_iext_remove(bma->ip, &bma->icur, state);
1572 xfs_iext_prev(ifp, &bma->icur);
1573 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
1574
1575 if (bma->cur == NULL)
1576 rval = XFS_ILOG_DEXT;
1577 else {
1578 rval = 0;
1579 error = xfs_bmbt_lookup_eq(bma->cur, &RIGHT, &i);
1580 if (error)
1581 goto done;
1582 if (XFS_IS_CORRUPT(mp, i != 1)) {
1583 error = -EFSCORRUPTED;
1584 goto done;
1585 }
1586 error = xfs_bmbt_update(bma->cur, &PREV);
1587 if (error)
1588 goto done;
1589 }
1590 break;
1591
1592 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
1593
1594
1595
1596
1597
1598 PREV.br_startblock = new->br_startblock;
1599 PREV.br_state = new->br_state;
1600 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
1601 ifp->if_nextents++;
1602
1603 if (bma->cur == NULL)
1604 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1605 else {
1606 rval = XFS_ILOG_CORE;
1607 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
1608 if (error)
1609 goto done;
1610 if (XFS_IS_CORRUPT(mp, i != 0)) {
1611 error = -EFSCORRUPTED;
1612 goto done;
1613 }
1614 error = xfs_btree_insert(bma->cur, &i);
1615 if (error)
1616 goto done;
1617 if (XFS_IS_CORRUPT(mp, i != 1)) {
1618 error = -EFSCORRUPTED;
1619 goto done;
1620 }
1621 }
1622 break;
1623
1624 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
1625
1626
1627
1628
1629 old = LEFT;
1630 temp = PREV.br_blockcount - new->br_blockcount;
1631 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
1632 startblockval(PREV.br_startblock));
1633
1634 LEFT.br_blockcount += new->br_blockcount;
1635
1636 PREV.br_blockcount = temp;
1637 PREV.br_startoff += new->br_blockcount;
1638 PREV.br_startblock = nullstartblock(da_new);
1639
1640 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
1641 xfs_iext_prev(ifp, &bma->icur);
1642 xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT);
1643
1644 if (bma->cur == NULL)
1645 rval = XFS_ILOG_DEXT;
1646 else {
1647 rval = 0;
1648 error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
1649 if (error)
1650 goto done;
1651 if (XFS_IS_CORRUPT(mp, i != 1)) {
1652 error = -EFSCORRUPTED;
1653 goto done;
1654 }
1655 error = xfs_bmbt_update(bma->cur, &LEFT);
1656 if (error)
1657 goto done;
1658 }
1659 break;
1660
1661 case BMAP_LEFT_FILLING:
1662
1663
1664
1665
1666 xfs_iext_update_extent(bma->ip, state, &bma->icur, new);
1667 ifp->if_nextents++;
1668
1669 if (bma->cur == NULL)
1670 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1671 else {
1672 rval = XFS_ILOG_CORE;
1673 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
1674 if (error)
1675 goto done;
1676 if (XFS_IS_CORRUPT(mp, i != 0)) {
1677 error = -EFSCORRUPTED;
1678 goto done;
1679 }
1680 error = xfs_btree_insert(bma->cur, &i);
1681 if (error)
1682 goto done;
1683 if (XFS_IS_CORRUPT(mp, i != 1)) {
1684 error = -EFSCORRUPTED;
1685 goto done;
1686 }
1687 }
1688
1689 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
1690 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
1691 &bma->cur, 1, &tmp_rval, whichfork);
1692 rval |= tmp_rval;
1693 if (error)
1694 goto done;
1695 }
1696
1697 temp = PREV.br_blockcount - new->br_blockcount;
1698 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
1699 startblockval(PREV.br_startblock) -
1700 (bma->cur ? bma->cur->bc_ino.allocated : 0));
1701
1702 PREV.br_startoff = new_endoff;
1703 PREV.br_blockcount = temp;
1704 PREV.br_startblock = nullstartblock(da_new);
1705 xfs_iext_next(ifp, &bma->icur);
1706 xfs_iext_insert(bma->ip, &bma->icur, &PREV, state);
1707 xfs_iext_prev(ifp, &bma->icur);
1708 break;
1709
1710 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1711
1712
1713
1714
1715 old = RIGHT;
1716 RIGHT.br_startoff = new->br_startoff;
1717 RIGHT.br_startblock = new->br_startblock;
1718 RIGHT.br_blockcount += new->br_blockcount;
1719
1720 if (bma->cur == NULL)
1721 rval = XFS_ILOG_DEXT;
1722 else {
1723 rval = 0;
1724 error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
1725 if (error)
1726 goto done;
1727 if (XFS_IS_CORRUPT(mp, i != 1)) {
1728 error = -EFSCORRUPTED;
1729 goto done;
1730 }
1731 error = xfs_bmbt_update(bma->cur, &RIGHT);
1732 if (error)
1733 goto done;
1734 }
1735
1736 temp = PREV.br_blockcount - new->br_blockcount;
1737 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
1738 startblockval(PREV.br_startblock));
1739
1740 PREV.br_blockcount = temp;
1741 PREV.br_startblock = nullstartblock(da_new);
1742
1743 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
1744 xfs_iext_next(ifp, &bma->icur);
1745 xfs_iext_update_extent(bma->ip, state, &bma->icur, &RIGHT);
1746 break;
1747
1748 case BMAP_RIGHT_FILLING:
1749
1750
1751
1752
1753 xfs_iext_update_extent(bma->ip, state, &bma->icur, new);
1754 ifp->if_nextents++;
1755
1756 if (bma->cur == NULL)
1757 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1758 else {
1759 rval = XFS_ILOG_CORE;
1760 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
1761 if (error)
1762 goto done;
1763 if (XFS_IS_CORRUPT(mp, i != 0)) {
1764 error = -EFSCORRUPTED;
1765 goto done;
1766 }
1767 error = xfs_btree_insert(bma->cur, &i);
1768 if (error)
1769 goto done;
1770 if (XFS_IS_CORRUPT(mp, i != 1)) {
1771 error = -EFSCORRUPTED;
1772 goto done;
1773 }
1774 }
1775
1776 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
1777 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
1778 &bma->cur, 1, &tmp_rval, whichfork);
1779 rval |= tmp_rval;
1780 if (error)
1781 goto done;
1782 }
1783
1784 temp = PREV.br_blockcount - new->br_blockcount;
1785 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
1786 startblockval(PREV.br_startblock) -
1787 (bma->cur ? bma->cur->bc_ino.allocated : 0));
1788
1789 PREV.br_startblock = nullstartblock(da_new);
1790 PREV.br_blockcount = temp;
1791 xfs_iext_insert(bma->ip, &bma->icur, &PREV, state);
1792 xfs_iext_next(ifp, &bma->icur);
1793 break;
1794
1795 case 0:
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816 old = PREV;
1817
1818
1819 LEFT = *new;
1820
1821
1822 RIGHT.br_state = PREV.br_state;
1823 RIGHT.br_startoff = new_endoff;
1824 RIGHT.br_blockcount =
1825 PREV.br_startoff + PREV.br_blockcount - new_endoff;
1826 RIGHT.br_startblock =
1827 nullstartblock(xfs_bmap_worst_indlen(bma->ip,
1828 RIGHT.br_blockcount));
1829
1830
1831 PREV.br_blockcount = new->br_startoff - PREV.br_startoff;
1832 PREV.br_startblock =
1833 nullstartblock(xfs_bmap_worst_indlen(bma->ip,
1834 PREV.br_blockcount));
1835 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
1836
1837 xfs_iext_next(ifp, &bma->icur);
1838 xfs_iext_insert(bma->ip, &bma->icur, &RIGHT, state);
1839 xfs_iext_insert(bma->ip, &bma->icur, &LEFT, state);
1840 ifp->if_nextents++;
1841
1842 if (bma->cur == NULL)
1843 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1844 else {
1845 rval = XFS_ILOG_CORE;
1846 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
1847 if (error)
1848 goto done;
1849 if (XFS_IS_CORRUPT(mp, i != 0)) {
1850 error = -EFSCORRUPTED;
1851 goto done;
1852 }
1853 error = xfs_btree_insert(bma->cur, &i);
1854 if (error)
1855 goto done;
1856 if (XFS_IS_CORRUPT(mp, i != 1)) {
1857 error = -EFSCORRUPTED;
1858 goto done;
1859 }
1860 }
1861
1862 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
1863 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
1864 &bma->cur, 1, &tmp_rval, whichfork);
1865 rval |= tmp_rval;
1866 if (error)
1867 goto done;
1868 }
1869
1870 da_new = startblockval(PREV.br_startblock) +
1871 startblockval(RIGHT.br_startblock);
1872 break;
1873
1874 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1875 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1876 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
1877 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1878 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1879 case BMAP_LEFT_CONTIG:
1880 case BMAP_RIGHT_CONTIG:
1881
1882
1883
1884 ASSERT(0);
1885 }
1886
1887
1888 if (!(bma->flags & XFS_BMAPI_NORMAP))
1889 xfs_rmap_map_extent(bma->tp, bma->ip, whichfork, new);
1890
1891
1892 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
1893 int tmp_logflags;
1894
1895 ASSERT(bma->cur == NULL);
1896 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
1897 &bma->cur, da_old > 0, &tmp_logflags,
1898 whichfork);
1899 bma->logflags |= tmp_logflags;
1900 if (error)
1901 goto done;
1902 }
1903
1904 if (da_new != da_old)
1905 xfs_mod_delalloc(mp, (int64_t)da_new - da_old);
1906
1907 if (bma->cur) {
1908 da_new += bma->cur->bc_ino.allocated;
1909 bma->cur->bc_ino.allocated = 0;
1910 }
1911
1912
1913 if (da_new != da_old) {
1914 ASSERT(state == 0 || da_new < da_old);
1915 error = xfs_mod_fdblocks(mp, (int64_t)(da_old - da_new),
1916 false);
1917 }
1918
1919 xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork);
1920 done:
1921 if (whichfork != XFS_COW_FORK)
1922 bma->logflags |= rval;
1923 return error;
1924 #undef LEFT
1925 #undef RIGHT
1926 #undef PREV
1927 }
1928
1929
1930
1931
1932 int
1933 xfs_bmap_add_extent_unwritten_real(
1934 struct xfs_trans *tp,
1935 xfs_inode_t *ip,
1936 int whichfork,
1937 struct xfs_iext_cursor *icur,
1938 struct xfs_btree_cur **curp,
1939 xfs_bmbt_irec_t *new,
1940 int *logflagsp)
1941 {
1942 struct xfs_btree_cur *cur;
1943 int error;
1944 int i;
1945 struct xfs_ifork *ifp;
1946 xfs_fileoff_t new_endoff;
1947 xfs_bmbt_irec_t r[3];
1948
1949 int rval=0;
1950 uint32_t state = xfs_bmap_fork_to_state(whichfork);
1951 struct xfs_mount *mp = ip->i_mount;
1952 struct xfs_bmbt_irec old;
1953
1954 *logflagsp = 0;
1955
1956 cur = *curp;
1957 ifp = xfs_ifork_ptr(ip, whichfork);
1958
1959 ASSERT(!isnullstartblock(new->br_startblock));
1960
1961 XFS_STATS_INC(mp, xs_add_exlist);
1962
1963 #define LEFT r[0]
1964 #define RIGHT r[1]
1965 #define PREV r[2]
1966
1967
1968
1969
1970 error = 0;
1971 xfs_iext_get_extent(ifp, icur, &PREV);
1972 ASSERT(new->br_state != PREV.br_state);
1973 new_endoff = new->br_startoff + new->br_blockcount;
1974 ASSERT(PREV.br_startoff <= new->br_startoff);
1975 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
1976
1977
1978
1979
1980
1981 if (PREV.br_startoff == new->br_startoff)
1982 state |= BMAP_LEFT_FILLING;
1983 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
1984 state |= BMAP_RIGHT_FILLING;
1985
1986
1987
1988
1989
1990 if (xfs_iext_peek_prev_extent(ifp, icur, &LEFT)) {
1991 state |= BMAP_LEFT_VALID;
1992 if (isnullstartblock(LEFT.br_startblock))
1993 state |= BMAP_LEFT_DELAY;
1994 }
1995
1996 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1997 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1998 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
1999 LEFT.br_state == new->br_state &&
2000 LEFT.br_blockcount + new->br_blockcount <= XFS_MAX_BMBT_EXTLEN)
2001 state |= BMAP_LEFT_CONTIG;
2002
2003
2004
2005
2006
2007
2008 if (xfs_iext_peek_next_extent(ifp, icur, &RIGHT)) {
2009 state |= BMAP_RIGHT_VALID;
2010 if (isnullstartblock(RIGHT.br_startblock))
2011 state |= BMAP_RIGHT_DELAY;
2012 }
2013
2014 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2015 new_endoff == RIGHT.br_startoff &&
2016 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
2017 new->br_state == RIGHT.br_state &&
2018 new->br_blockcount + RIGHT.br_blockcount <= XFS_MAX_BMBT_EXTLEN &&
2019 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2020 BMAP_RIGHT_FILLING)) !=
2021 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2022 BMAP_RIGHT_FILLING) ||
2023 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
2024 <= XFS_MAX_BMBT_EXTLEN))
2025 state |= BMAP_RIGHT_CONTIG;
2026
2027
2028
2029
2030 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2031 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
2032 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2033 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
2034
2035
2036
2037
2038 LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount;
2039
2040 xfs_iext_remove(ip, icur, state);
2041 xfs_iext_remove(ip, icur, state);
2042 xfs_iext_prev(ifp, icur);
2043 xfs_iext_update_extent(ip, state, icur, &LEFT);
2044 ifp->if_nextents -= 2;
2045 if (cur == NULL)
2046 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2047 else {
2048 rval = XFS_ILOG_CORE;
2049 error = xfs_bmbt_lookup_eq(cur, &RIGHT, &i);
2050 if (error)
2051 goto done;
2052 if (XFS_IS_CORRUPT(mp, i != 1)) {
2053 error = -EFSCORRUPTED;
2054 goto done;
2055 }
2056 if ((error = xfs_btree_delete(cur, &i)))
2057 goto done;
2058 if (XFS_IS_CORRUPT(mp, i != 1)) {
2059 error = -EFSCORRUPTED;
2060 goto done;
2061 }
2062 if ((error = xfs_btree_decrement(cur, 0, &i)))
2063 goto done;
2064 if (XFS_IS_CORRUPT(mp, i != 1)) {
2065 error = -EFSCORRUPTED;
2066 goto done;
2067 }
2068 if ((error = xfs_btree_delete(cur, &i)))
2069 goto done;
2070 if (XFS_IS_CORRUPT(mp, i != 1)) {
2071 error = -EFSCORRUPTED;
2072 goto done;
2073 }
2074 if ((error = xfs_btree_decrement(cur, 0, &i)))
2075 goto done;
2076 if (XFS_IS_CORRUPT(mp, i != 1)) {
2077 error = -EFSCORRUPTED;
2078 goto done;
2079 }
2080 error = xfs_bmbt_update(cur, &LEFT);
2081 if (error)
2082 goto done;
2083 }
2084 break;
2085
2086 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2087
2088
2089
2090
2091 LEFT.br_blockcount += PREV.br_blockcount;
2092
2093 xfs_iext_remove(ip, icur, state);
2094 xfs_iext_prev(ifp, icur);
2095 xfs_iext_update_extent(ip, state, icur, &LEFT);
2096 ifp->if_nextents--;
2097 if (cur == NULL)
2098 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2099 else {
2100 rval = XFS_ILOG_CORE;
2101 error = xfs_bmbt_lookup_eq(cur, &PREV, &i);
2102 if (error)
2103 goto done;
2104 if (XFS_IS_CORRUPT(mp, i != 1)) {
2105 error = -EFSCORRUPTED;
2106 goto done;
2107 }
2108 if ((error = xfs_btree_delete(cur, &i)))
2109 goto done;
2110 if (XFS_IS_CORRUPT(mp, i != 1)) {
2111 error = -EFSCORRUPTED;
2112 goto done;
2113 }
2114 if ((error = xfs_btree_decrement(cur, 0, &i)))
2115 goto done;
2116 if (XFS_IS_CORRUPT(mp, i != 1)) {
2117 error = -EFSCORRUPTED;
2118 goto done;
2119 }
2120 error = xfs_bmbt_update(cur, &LEFT);
2121 if (error)
2122 goto done;
2123 }
2124 break;
2125
2126 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
2127
2128
2129
2130
2131 PREV.br_blockcount += RIGHT.br_blockcount;
2132 PREV.br_state = new->br_state;
2133
2134 xfs_iext_next(ifp, icur);
2135 xfs_iext_remove(ip, icur, state);
2136 xfs_iext_prev(ifp, icur);
2137 xfs_iext_update_extent(ip, state, icur, &PREV);
2138 ifp->if_nextents--;
2139
2140 if (cur == NULL)
2141 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2142 else {
2143 rval = XFS_ILOG_CORE;
2144 error = xfs_bmbt_lookup_eq(cur, &RIGHT, &i);
2145 if (error)
2146 goto done;
2147 if (XFS_IS_CORRUPT(mp, i != 1)) {
2148 error = -EFSCORRUPTED;
2149 goto done;
2150 }
2151 if ((error = xfs_btree_delete(cur, &i)))
2152 goto done;
2153 if (XFS_IS_CORRUPT(mp, i != 1)) {
2154 error = -EFSCORRUPTED;
2155 goto done;
2156 }
2157 if ((error = xfs_btree_decrement(cur, 0, &i)))
2158 goto done;
2159 if (XFS_IS_CORRUPT(mp, i != 1)) {
2160 error = -EFSCORRUPTED;
2161 goto done;
2162 }
2163 error = xfs_bmbt_update(cur, &PREV);
2164 if (error)
2165 goto done;
2166 }
2167 break;
2168
2169 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
2170
2171
2172
2173
2174
2175 PREV.br_state = new->br_state;
2176 xfs_iext_update_extent(ip, state, icur, &PREV);
2177
2178 if (cur == NULL)
2179 rval = XFS_ILOG_DEXT;
2180 else {
2181 rval = 0;
2182 error = xfs_bmbt_lookup_eq(cur, new, &i);
2183 if (error)
2184 goto done;
2185 if (XFS_IS_CORRUPT(mp, i != 1)) {
2186 error = -EFSCORRUPTED;
2187 goto done;
2188 }
2189 error = xfs_bmbt_update(cur, &PREV);
2190 if (error)
2191 goto done;
2192 }
2193 break;
2194
2195 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
2196
2197
2198
2199
2200 LEFT.br_blockcount += new->br_blockcount;
2201
2202 old = PREV;
2203 PREV.br_startoff += new->br_blockcount;
2204 PREV.br_startblock += new->br_blockcount;
2205 PREV.br_blockcount -= new->br_blockcount;
2206
2207 xfs_iext_update_extent(ip, state, icur, &PREV);
2208 xfs_iext_prev(ifp, icur);
2209 xfs_iext_update_extent(ip, state, icur, &LEFT);
2210
2211 if (cur == NULL)
2212 rval = XFS_ILOG_DEXT;
2213 else {
2214 rval = 0;
2215 error = xfs_bmbt_lookup_eq(cur, &old, &i);
2216 if (error)
2217 goto done;
2218 if (XFS_IS_CORRUPT(mp, i != 1)) {
2219 error = -EFSCORRUPTED;
2220 goto done;
2221 }
2222 error = xfs_bmbt_update(cur, &PREV);
2223 if (error)
2224 goto done;
2225 error = xfs_btree_decrement(cur, 0, &i);
2226 if (error)
2227 goto done;
2228 error = xfs_bmbt_update(cur, &LEFT);
2229 if (error)
2230 goto done;
2231 }
2232 break;
2233
2234 case BMAP_LEFT_FILLING:
2235
2236
2237
2238
2239 old = PREV;
2240 PREV.br_startoff += new->br_blockcount;
2241 PREV.br_startblock += new->br_blockcount;
2242 PREV.br_blockcount -= new->br_blockcount;
2243
2244 xfs_iext_update_extent(ip, state, icur, &PREV);
2245 xfs_iext_insert(ip, icur, new, state);
2246 ifp->if_nextents++;
2247
2248 if (cur == NULL)
2249 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2250 else {
2251 rval = XFS_ILOG_CORE;
2252 error = xfs_bmbt_lookup_eq(cur, &old, &i);
2253 if (error)
2254 goto done;
2255 if (XFS_IS_CORRUPT(mp, i != 1)) {
2256 error = -EFSCORRUPTED;
2257 goto done;
2258 }
2259 error = xfs_bmbt_update(cur, &PREV);
2260 if (error)
2261 goto done;
2262 cur->bc_rec.b = *new;
2263 if ((error = xfs_btree_insert(cur, &i)))
2264 goto done;
2265 if (XFS_IS_CORRUPT(mp, i != 1)) {
2266 error = -EFSCORRUPTED;
2267 goto done;
2268 }
2269 }
2270 break;
2271
2272 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
2273
2274
2275
2276
2277 old = PREV;
2278 PREV.br_blockcount -= new->br_blockcount;
2279
2280 RIGHT.br_startoff = new->br_startoff;
2281 RIGHT.br_startblock = new->br_startblock;
2282 RIGHT.br_blockcount += new->br_blockcount;
2283
2284 xfs_iext_update_extent(ip, state, icur, &PREV);
2285 xfs_iext_next(ifp, icur);
2286 xfs_iext_update_extent(ip, state, icur, &RIGHT);
2287
2288 if (cur == NULL)
2289 rval = XFS_ILOG_DEXT;
2290 else {
2291 rval = 0;
2292 error = xfs_bmbt_lookup_eq(cur, &old, &i);
2293 if (error)
2294 goto done;
2295 if (XFS_IS_CORRUPT(mp, i != 1)) {
2296 error = -EFSCORRUPTED;
2297 goto done;
2298 }
2299 error = xfs_bmbt_update(cur, &PREV);
2300 if (error)
2301 goto done;
2302 error = xfs_btree_increment(cur, 0, &i);
2303 if (error)
2304 goto done;
2305 error = xfs_bmbt_update(cur, &RIGHT);
2306 if (error)
2307 goto done;
2308 }
2309 break;
2310
2311 case BMAP_RIGHT_FILLING:
2312
2313
2314
2315
2316 old = PREV;
2317 PREV.br_blockcount -= new->br_blockcount;
2318
2319 xfs_iext_update_extent(ip, state, icur, &PREV);
2320 xfs_iext_next(ifp, icur);
2321 xfs_iext_insert(ip, icur, new, state);
2322 ifp->if_nextents++;
2323
2324 if (cur == NULL)
2325 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2326 else {
2327 rval = XFS_ILOG_CORE;
2328 error = xfs_bmbt_lookup_eq(cur, &old, &i);
2329 if (error)
2330 goto done;
2331 if (XFS_IS_CORRUPT(mp, i != 1)) {
2332 error = -EFSCORRUPTED;
2333 goto done;
2334 }
2335 error = xfs_bmbt_update(cur, &PREV);
2336 if (error)
2337 goto done;
2338 error = xfs_bmbt_lookup_eq(cur, new, &i);
2339 if (error)
2340 goto done;
2341 if (XFS_IS_CORRUPT(mp, i != 0)) {
2342 error = -EFSCORRUPTED;
2343 goto done;
2344 }
2345 if ((error = xfs_btree_insert(cur, &i)))
2346 goto done;
2347 if (XFS_IS_CORRUPT(mp, i != 1)) {
2348 error = -EFSCORRUPTED;
2349 goto done;
2350 }
2351 }
2352 break;
2353
2354 case 0:
2355
2356
2357
2358
2359
2360 old = PREV;
2361 PREV.br_blockcount = new->br_startoff - PREV.br_startoff;
2362
2363 r[0] = *new;
2364 r[1].br_startoff = new_endoff;
2365 r[1].br_blockcount =
2366 old.br_startoff + old.br_blockcount - new_endoff;
2367 r[1].br_startblock = new->br_startblock + new->br_blockcount;
2368 r[1].br_state = PREV.br_state;
2369
2370 xfs_iext_update_extent(ip, state, icur, &PREV);
2371 xfs_iext_next(ifp, icur);
2372 xfs_iext_insert(ip, icur, &r[1], state);
2373 xfs_iext_insert(ip, icur, &r[0], state);
2374 ifp->if_nextents += 2;
2375
2376 if (cur == NULL)
2377 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2378 else {
2379 rval = XFS_ILOG_CORE;
2380 error = xfs_bmbt_lookup_eq(cur, &old, &i);
2381 if (error)
2382 goto done;
2383 if (XFS_IS_CORRUPT(mp, i != 1)) {
2384 error = -EFSCORRUPTED;
2385 goto done;
2386 }
2387
2388 error = xfs_bmbt_update(cur, &r[1]);
2389 if (error)
2390 goto done;
2391
2392 cur->bc_rec.b = PREV;
2393 if ((error = xfs_btree_insert(cur, &i)))
2394 goto done;
2395 if (XFS_IS_CORRUPT(mp, i != 1)) {
2396 error = -EFSCORRUPTED;
2397 goto done;
2398 }
2399
2400
2401
2402
2403
2404 error = xfs_bmbt_lookup_eq(cur, new, &i);
2405 if (error)
2406 goto done;
2407 if (XFS_IS_CORRUPT(mp, i != 0)) {
2408 error = -EFSCORRUPTED;
2409 goto done;
2410 }
2411
2412 if ((error = xfs_btree_insert(cur, &i)))
2413 goto done;
2414 if (XFS_IS_CORRUPT(mp, i != 1)) {
2415 error = -EFSCORRUPTED;
2416 goto done;
2417 }
2418 }
2419 break;
2420
2421 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2422 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2423 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
2424 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2425 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2426 case BMAP_LEFT_CONTIG:
2427 case BMAP_RIGHT_CONTIG:
2428
2429
2430
2431 ASSERT(0);
2432 }
2433
2434
2435 xfs_rmap_convert_extent(mp, tp, ip, whichfork, new);
2436
2437
2438 if (xfs_bmap_needs_btree(ip, whichfork)) {
2439 int tmp_logflags;
2440
2441 ASSERT(cur == NULL);
2442 error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0,
2443 &tmp_logflags, whichfork);
2444 *logflagsp |= tmp_logflags;
2445 if (error)
2446 goto done;
2447 }
2448
2449
2450 if (cur) {
2451 cur->bc_ino.allocated = 0;
2452 *curp = cur;
2453 }
2454
2455 xfs_bmap_check_leaf_extents(*curp, ip, whichfork);
2456 done:
2457 *logflagsp |= rval;
2458 return error;
2459 #undef LEFT
2460 #undef RIGHT
2461 #undef PREV
2462 }
2463
2464
2465
2466
2467 STATIC void
2468 xfs_bmap_add_extent_hole_delay(
2469 xfs_inode_t *ip,
2470 int whichfork,
2471 struct xfs_iext_cursor *icur,
2472 xfs_bmbt_irec_t *new)
2473 {
2474 struct xfs_ifork *ifp;
2475 xfs_bmbt_irec_t left;
2476 xfs_filblks_t newlen=0;
2477 xfs_filblks_t oldlen=0;
2478 xfs_bmbt_irec_t right;
2479 uint32_t state = xfs_bmap_fork_to_state(whichfork);
2480 xfs_filblks_t temp;
2481
2482 ifp = xfs_ifork_ptr(ip, whichfork);
2483 ASSERT(isnullstartblock(new->br_startblock));
2484
2485
2486
2487
2488 if (xfs_iext_peek_prev_extent(ifp, icur, &left)) {
2489 state |= BMAP_LEFT_VALID;
2490 if (isnullstartblock(left.br_startblock))
2491 state |= BMAP_LEFT_DELAY;
2492 }
2493
2494
2495
2496
2497
2498 if (xfs_iext_get_extent(ifp, icur, &right)) {
2499 state |= BMAP_RIGHT_VALID;
2500 if (isnullstartblock(right.br_startblock))
2501 state |= BMAP_RIGHT_DELAY;
2502 }
2503
2504
2505
2506
2507
2508 if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) &&
2509 left.br_startoff + left.br_blockcount == new->br_startoff &&
2510 left.br_blockcount + new->br_blockcount <= XFS_MAX_BMBT_EXTLEN)
2511 state |= BMAP_LEFT_CONTIG;
2512
2513 if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) &&
2514 new->br_startoff + new->br_blockcount == right.br_startoff &&
2515 new->br_blockcount + right.br_blockcount <= XFS_MAX_BMBT_EXTLEN &&
2516 (!(state & BMAP_LEFT_CONTIG) ||
2517 (left.br_blockcount + new->br_blockcount +
2518 right.br_blockcount <= XFS_MAX_BMBT_EXTLEN)))
2519 state |= BMAP_RIGHT_CONTIG;
2520
2521
2522
2523
2524 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2525 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2526
2527
2528
2529
2530
2531 temp = left.br_blockcount + new->br_blockcount +
2532 right.br_blockcount;
2533
2534 oldlen = startblockval(left.br_startblock) +
2535 startblockval(new->br_startblock) +
2536 startblockval(right.br_startblock);
2537 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2538 oldlen);
2539 left.br_startblock = nullstartblock(newlen);
2540 left.br_blockcount = temp;
2541
2542 xfs_iext_remove(ip, icur, state);
2543 xfs_iext_prev(ifp, icur);
2544 xfs_iext_update_extent(ip, state, icur, &left);
2545 break;
2546
2547 case BMAP_LEFT_CONTIG:
2548
2549
2550
2551
2552
2553 temp = left.br_blockcount + new->br_blockcount;
2554
2555 oldlen = startblockval(left.br_startblock) +
2556 startblockval(new->br_startblock);
2557 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2558 oldlen);
2559 left.br_blockcount = temp;
2560 left.br_startblock = nullstartblock(newlen);
2561
2562 xfs_iext_prev(ifp, icur);
2563 xfs_iext_update_extent(ip, state, icur, &left);
2564 break;
2565
2566 case BMAP_RIGHT_CONTIG:
2567
2568
2569
2570
2571
2572 temp = new->br_blockcount + right.br_blockcount;
2573 oldlen = startblockval(new->br_startblock) +
2574 startblockval(right.br_startblock);
2575 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2576 oldlen);
2577 right.br_startoff = new->br_startoff;
2578 right.br_startblock = nullstartblock(newlen);
2579 right.br_blockcount = temp;
2580 xfs_iext_update_extent(ip, state, icur, &right);
2581 break;
2582
2583 case 0:
2584
2585
2586
2587
2588
2589 oldlen = newlen = 0;
2590 xfs_iext_insert(ip, icur, new, state);
2591 break;
2592 }
2593 if (oldlen != newlen) {
2594 ASSERT(oldlen > newlen);
2595 xfs_mod_fdblocks(ip->i_mount, (int64_t)(oldlen - newlen),
2596 false);
2597
2598
2599
2600 xfs_mod_delalloc(ip->i_mount, (int64_t)newlen - oldlen);
2601 }
2602 }
2603
2604
2605
2606
2607 STATIC int
2608 xfs_bmap_add_extent_hole_real(
2609 struct xfs_trans *tp,
2610 struct xfs_inode *ip,
2611 int whichfork,
2612 struct xfs_iext_cursor *icur,
2613 struct xfs_btree_cur **curp,
2614 struct xfs_bmbt_irec *new,
2615 int *logflagsp,
2616 uint32_t flags)
2617 {
2618 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
2619 struct xfs_mount *mp = ip->i_mount;
2620 struct xfs_btree_cur *cur = *curp;
2621 int error;
2622 int i;
2623 xfs_bmbt_irec_t left;
2624 xfs_bmbt_irec_t right;
2625 int rval=0;
2626 uint32_t state = xfs_bmap_fork_to_state(whichfork);
2627 struct xfs_bmbt_irec old;
2628
2629 ASSERT(!isnullstartblock(new->br_startblock));
2630 ASSERT(!cur || !(cur->bc_ino.flags & XFS_BTCUR_BMBT_WASDEL));
2631
2632 XFS_STATS_INC(mp, xs_add_exlist);
2633
2634
2635
2636
2637 if (xfs_iext_peek_prev_extent(ifp, icur, &left)) {
2638 state |= BMAP_LEFT_VALID;
2639 if (isnullstartblock(left.br_startblock))
2640 state |= BMAP_LEFT_DELAY;
2641 }
2642
2643
2644
2645
2646
2647 if (xfs_iext_get_extent(ifp, icur, &right)) {
2648 state |= BMAP_RIGHT_VALID;
2649 if (isnullstartblock(right.br_startblock))
2650 state |= BMAP_RIGHT_DELAY;
2651 }
2652
2653
2654
2655
2656
2657 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2658 left.br_startoff + left.br_blockcount == new->br_startoff &&
2659 left.br_startblock + left.br_blockcount == new->br_startblock &&
2660 left.br_state == new->br_state &&
2661 left.br_blockcount + new->br_blockcount <= XFS_MAX_BMBT_EXTLEN)
2662 state |= BMAP_LEFT_CONTIG;
2663
2664 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2665 new->br_startoff + new->br_blockcount == right.br_startoff &&
2666 new->br_startblock + new->br_blockcount == right.br_startblock &&
2667 new->br_state == right.br_state &&
2668 new->br_blockcount + right.br_blockcount <= XFS_MAX_BMBT_EXTLEN &&
2669 (!(state & BMAP_LEFT_CONTIG) ||
2670 left.br_blockcount + new->br_blockcount +
2671 right.br_blockcount <= XFS_MAX_BMBT_EXTLEN))
2672 state |= BMAP_RIGHT_CONTIG;
2673
2674 error = 0;
2675
2676
2677
2678 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2679 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2680
2681
2682
2683
2684
2685 left.br_blockcount += new->br_blockcount + right.br_blockcount;
2686
2687 xfs_iext_remove(ip, icur, state);
2688 xfs_iext_prev(ifp, icur);
2689 xfs_iext_update_extent(ip, state, icur, &left);
2690 ifp->if_nextents--;
2691
2692 if (cur == NULL) {
2693 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
2694 } else {
2695 rval = XFS_ILOG_CORE;
2696 error = xfs_bmbt_lookup_eq(cur, &right, &i);
2697 if (error)
2698 goto done;
2699 if (XFS_IS_CORRUPT(mp, i != 1)) {
2700 error = -EFSCORRUPTED;
2701 goto done;
2702 }
2703 error = xfs_btree_delete(cur, &i);
2704 if (error)
2705 goto done;
2706 if (XFS_IS_CORRUPT(mp, i != 1)) {
2707 error = -EFSCORRUPTED;
2708 goto done;
2709 }
2710 error = xfs_btree_decrement(cur, 0, &i);
2711 if (error)
2712 goto done;
2713 if (XFS_IS_CORRUPT(mp, i != 1)) {
2714 error = -EFSCORRUPTED;
2715 goto done;
2716 }
2717 error = xfs_bmbt_update(cur, &left);
2718 if (error)
2719 goto done;
2720 }
2721 break;
2722
2723 case BMAP_LEFT_CONTIG:
2724
2725
2726
2727
2728
2729 old = left;
2730 left.br_blockcount += new->br_blockcount;
2731
2732 xfs_iext_prev(ifp, icur);
2733 xfs_iext_update_extent(ip, state, icur, &left);
2734
2735 if (cur == NULL) {
2736 rval = xfs_ilog_fext(whichfork);
2737 } else {
2738 rval = 0;
2739 error = xfs_bmbt_lookup_eq(cur, &old, &i);
2740 if (error)
2741 goto done;
2742 if (XFS_IS_CORRUPT(mp, i != 1)) {
2743 error = -EFSCORRUPTED;
2744 goto done;
2745 }
2746 error = xfs_bmbt_update(cur, &left);
2747 if (error)
2748 goto done;
2749 }
2750 break;
2751
2752 case BMAP_RIGHT_CONTIG:
2753
2754
2755
2756
2757
2758 old = right;
2759
2760 right.br_startoff = new->br_startoff;
2761 right.br_startblock = new->br_startblock;
2762 right.br_blockcount += new->br_blockcount;
2763 xfs_iext_update_extent(ip, state, icur, &right);
2764
2765 if (cur == NULL) {
2766 rval = xfs_ilog_fext(whichfork);
2767 } else {
2768 rval = 0;
2769 error = xfs_bmbt_lookup_eq(cur, &old, &i);
2770 if (error)
2771 goto done;
2772 if (XFS_IS_CORRUPT(mp, i != 1)) {
2773 error = -EFSCORRUPTED;
2774 goto done;
2775 }
2776 error = xfs_bmbt_update(cur, &right);
2777 if (error)
2778 goto done;
2779 }
2780 break;
2781
2782 case 0:
2783
2784
2785
2786
2787
2788 xfs_iext_insert(ip, icur, new, state);
2789 ifp->if_nextents++;
2790
2791 if (cur == NULL) {
2792 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
2793 } else {
2794 rval = XFS_ILOG_CORE;
2795 error = xfs_bmbt_lookup_eq(cur, new, &i);
2796 if (error)
2797 goto done;
2798 if (XFS_IS_CORRUPT(mp, i != 0)) {
2799 error = -EFSCORRUPTED;
2800 goto done;
2801 }
2802 error = xfs_btree_insert(cur, &i);
2803 if (error)
2804 goto done;
2805 if (XFS_IS_CORRUPT(mp, i != 1)) {
2806 error = -EFSCORRUPTED;
2807 goto done;
2808 }
2809 }
2810 break;
2811 }
2812
2813
2814 if (!(flags & XFS_BMAPI_NORMAP))
2815 xfs_rmap_map_extent(tp, ip, whichfork, new);
2816
2817
2818 if (xfs_bmap_needs_btree(ip, whichfork)) {
2819 int tmp_logflags;
2820
2821 ASSERT(cur == NULL);
2822 error = xfs_bmap_extents_to_btree(tp, ip, curp, 0,
2823 &tmp_logflags, whichfork);
2824 *logflagsp |= tmp_logflags;
2825 cur = *curp;
2826 if (error)
2827 goto done;
2828 }
2829
2830
2831 if (cur)
2832 cur->bc_ino.allocated = 0;
2833
2834 xfs_bmap_check_leaf_extents(cur, ip, whichfork);
2835 done:
2836 *logflagsp |= rval;
2837 return error;
2838 }
2839
2840
2841
2842
2843
2844
2845
2846
2847 int
2848 xfs_bmap_extsize_align(
2849 xfs_mount_t *mp,
2850 xfs_bmbt_irec_t *gotp,
2851 xfs_bmbt_irec_t *prevp,
2852 xfs_extlen_t extsz,
2853 int rt,
2854 int eof,
2855 int delay,
2856 int convert,
2857 xfs_fileoff_t *offp,
2858 xfs_extlen_t *lenp)
2859 {
2860 xfs_fileoff_t orig_off;
2861 xfs_extlen_t orig_alen;
2862 xfs_fileoff_t orig_end;
2863 xfs_fileoff_t nexto;
2864 xfs_fileoff_t prevo;
2865 xfs_fileoff_t align_off;
2866 xfs_extlen_t align_alen;
2867 xfs_extlen_t temp;
2868
2869 if (convert)
2870 return 0;
2871
2872 orig_off = align_off = *offp;
2873 orig_alen = align_alen = *lenp;
2874 orig_end = orig_off + orig_alen;
2875
2876
2877
2878
2879
2880 if (!delay && !eof &&
2881 (orig_off >= gotp->br_startoff) &&
2882 (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
2883 return 0;
2884 }
2885
2886
2887
2888
2889
2890
2891
2892
2893 div_u64_rem(orig_off, extsz, &temp);
2894 if (temp) {
2895 align_alen += temp;
2896 align_off -= temp;
2897 }
2898
2899
2900 temp = (align_alen % extsz);
2901 if (temp)
2902 align_alen += extsz - temp;
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912 while (align_alen > XFS_MAX_BMBT_EXTLEN)
2913 align_alen -= extsz;
2914 ASSERT(align_alen <= XFS_MAX_BMBT_EXTLEN);
2915
2916
2917
2918
2919
2920 if (prevp->br_startoff != NULLFILEOFF) {
2921 if (prevp->br_startblock == HOLESTARTBLOCK)
2922 prevo = prevp->br_startoff;
2923 else
2924 prevo = prevp->br_startoff + prevp->br_blockcount;
2925 } else
2926 prevo = 0;
2927 if (align_off != orig_off && align_off < prevo)
2928 align_off = prevo;
2929
2930
2931
2932
2933
2934
2935
2936
2937 if (!eof && gotp->br_startoff != NULLFILEOFF) {
2938 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
2939 (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
2940 nexto = gotp->br_startoff + gotp->br_blockcount;
2941 else
2942 nexto = gotp->br_startoff;
2943 } else
2944 nexto = NULLFILEOFF;
2945 if (!eof &&
2946 align_off + align_alen != orig_end &&
2947 align_off + align_alen > nexto)
2948 align_off = nexto > align_alen ? nexto - align_alen : 0;
2949
2950
2951
2952
2953
2954
2955 if (align_off != orig_off && align_off < prevo)
2956 align_off = prevo;
2957 if (align_off + align_alen != orig_end &&
2958 align_off + align_alen > nexto &&
2959 nexto != NULLFILEOFF) {
2960 ASSERT(nexto > prevo);
2961 align_alen = nexto - align_off;
2962 }
2963
2964
2965
2966
2967
2968 if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
2969
2970
2971
2972
2973 if (orig_off < align_off ||
2974 orig_end > align_off + align_alen ||
2975 align_alen - temp < orig_alen)
2976 return -EINVAL;
2977
2978
2979
2980 if (align_off + temp <= orig_off) {
2981 align_alen -= temp;
2982 align_off += temp;
2983 }
2984
2985
2986
2987 else if (align_off + align_alen - temp >= orig_end)
2988 align_alen -= temp;
2989
2990
2991
2992 else {
2993 align_alen -= orig_off - align_off;
2994 align_off = orig_off;
2995 align_alen -= align_alen % mp->m_sb.sb_rextsize;
2996 }
2997
2998
2999
3000 if (orig_off < align_off || orig_end > align_off + align_alen)
3001 return -EINVAL;
3002 } else {
3003 ASSERT(orig_off >= align_off);
3004
3005 ASSERT(orig_end <= align_off + align_alen ||
3006 align_alen + extsz > XFS_MAX_BMBT_EXTLEN);
3007 }
3008
3009 #ifdef DEBUG
3010 if (!eof && gotp->br_startoff != NULLFILEOFF)
3011 ASSERT(align_off + align_alen <= gotp->br_startoff);
3012 if (prevp->br_startoff != NULLFILEOFF)
3013 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
3014 #endif
3015
3016 *lenp = align_alen;
3017 *offp = align_off;
3018 return 0;
3019 }
3020
3021 #define XFS_ALLOC_GAP_UNITS 4
3022
3023 void
3024 xfs_bmap_adjacent(
3025 struct xfs_bmalloca *ap)
3026 {
3027 xfs_fsblock_t adjust;
3028 xfs_agnumber_t fb_agno;
3029 xfs_mount_t *mp;
3030 int nullfb;
3031 int rt;
3032
3033 #define ISVALID(x,y) \
3034 (rt ? \
3035 (x) < mp->m_sb.sb_rblocks : \
3036 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
3037 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
3038 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
3039
3040 mp = ap->ip->i_mount;
3041 nullfb = ap->tp->t_firstblock == NULLFSBLOCK;
3042 rt = XFS_IS_REALTIME_INODE(ap->ip) &&
3043 (ap->datatype & XFS_ALLOC_USERDATA);
3044 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp,
3045 ap->tp->t_firstblock);
3046
3047
3048
3049
3050 if (ap->eof && ap->prev.br_startoff != NULLFILEOFF &&
3051 !isnullstartblock(ap->prev.br_startblock) &&
3052 ISVALID(ap->prev.br_startblock + ap->prev.br_blockcount,
3053 ap->prev.br_startblock)) {
3054 ap->blkno = ap->prev.br_startblock + ap->prev.br_blockcount;
3055
3056
3057
3058 adjust = ap->offset -
3059 (ap->prev.br_startoff + ap->prev.br_blockcount);
3060 if (adjust &&
3061 ISVALID(ap->blkno + adjust, ap->prev.br_startblock))
3062 ap->blkno += adjust;
3063 }
3064
3065
3066
3067
3068
3069 else if (!ap->eof) {
3070 xfs_fsblock_t gotbno;
3071 xfs_fsblock_t gotdiff=0;
3072 xfs_fsblock_t prevbno;
3073 xfs_fsblock_t prevdiff=0;
3074
3075
3076
3077
3078
3079 if (ap->prev.br_startoff != NULLFILEOFF &&
3080 !isnullstartblock(ap->prev.br_startblock) &&
3081 (prevbno = ap->prev.br_startblock +
3082 ap->prev.br_blockcount) &&
3083 ISVALID(prevbno, ap->prev.br_startblock)) {
3084
3085
3086
3087 adjust = prevdiff = ap->offset -
3088 (ap->prev.br_startoff +
3089 ap->prev.br_blockcount);
3090
3091
3092
3093
3094
3095
3096
3097
3098 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
3099 ISVALID(prevbno + prevdiff,
3100 ap->prev.br_startblock))
3101 prevbno += adjust;
3102 else
3103 prevdiff += adjust;
3104
3105
3106
3107
3108 if (!rt && !nullfb &&
3109 XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
3110 prevbno = NULLFSBLOCK;
3111 }
3112
3113
3114
3115 else
3116 prevbno = NULLFSBLOCK;
3117
3118
3119
3120
3121 if (!isnullstartblock(ap->got.br_startblock)) {
3122
3123
3124
3125 adjust = gotdiff = ap->got.br_startoff - ap->offset;
3126
3127
3128
3129
3130 gotbno = ap->got.br_startblock;
3131
3132
3133
3134
3135
3136
3137
3138 if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
3139 ISVALID(gotbno - gotdiff, gotbno))
3140 gotbno -= adjust;
3141 else if (ISVALID(gotbno - ap->length, gotbno)) {
3142 gotbno -= ap->length;
3143 gotdiff += adjust - ap->length;
3144 } else
3145 gotdiff += adjust;
3146
3147
3148
3149
3150 if (!rt && !nullfb &&
3151 XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
3152 gotbno = NULLFSBLOCK;
3153 }
3154
3155
3156
3157 else
3158 gotbno = NULLFSBLOCK;
3159
3160
3161
3162
3163 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
3164 ap->blkno = prevdiff <= gotdiff ? prevbno : gotbno;
3165 else if (prevbno != NULLFSBLOCK)
3166 ap->blkno = prevbno;
3167 else if (gotbno != NULLFSBLOCK)
3168 ap->blkno = gotbno;
3169 }
3170 #undef ISVALID
3171 }
3172
3173 static int
3174 xfs_bmap_longest_free_extent(
3175 struct xfs_trans *tp,
3176 xfs_agnumber_t ag,
3177 xfs_extlen_t *blen,
3178 int *notinit)
3179 {
3180 struct xfs_mount *mp = tp->t_mountp;
3181 struct xfs_perag *pag;
3182 xfs_extlen_t longest;
3183 int error = 0;
3184
3185 pag = xfs_perag_get(mp, ag);
3186 if (!pag->pagf_init) {
3187 error = xfs_alloc_read_agf(pag, tp, XFS_ALLOC_FLAG_TRYLOCK,
3188 NULL);
3189 if (error) {
3190
3191 if (error == -EAGAIN) {
3192 *notinit = 1;
3193 error = 0;
3194 }
3195 goto out;
3196 }
3197 }
3198
3199 longest = xfs_alloc_longest_free_extent(pag,
3200 xfs_alloc_min_freelist(mp, pag),
3201 xfs_ag_resv_needed(pag, XFS_AG_RESV_NONE));
3202 if (*blen < longest)
3203 *blen = longest;
3204
3205 out:
3206 xfs_perag_put(pag);
3207 return error;
3208 }
3209
3210 static void
3211 xfs_bmap_select_minlen(
3212 struct xfs_bmalloca *ap,
3213 struct xfs_alloc_arg *args,
3214 xfs_extlen_t *blen,
3215 int notinit)
3216 {
3217 if (notinit || *blen < ap->minlen) {
3218
3219
3220
3221
3222 args->minlen = ap->minlen;
3223 } else if (*blen < args->maxlen) {
3224
3225
3226
3227
3228 args->minlen = *blen;
3229 } else {
3230
3231
3232
3233
3234 args->minlen = args->maxlen;
3235 }
3236 }
3237
3238 STATIC int
3239 xfs_bmap_btalloc_nullfb(
3240 struct xfs_bmalloca *ap,
3241 struct xfs_alloc_arg *args,
3242 xfs_extlen_t *blen)
3243 {
3244 struct xfs_mount *mp = ap->ip->i_mount;
3245 xfs_agnumber_t ag, startag;
3246 int notinit = 0;
3247 int error;
3248
3249 args->type = XFS_ALLOCTYPE_START_BNO;
3250 args->total = ap->total;
3251
3252 startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3253 if (startag == NULLAGNUMBER)
3254 startag = ag = 0;
3255
3256 while (*blen < args->maxlen) {
3257 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3258 ¬init);
3259 if (error)
3260 return error;
3261
3262 if (++ag == mp->m_sb.sb_agcount)
3263 ag = 0;
3264 if (ag == startag)
3265 break;
3266 }
3267
3268 xfs_bmap_select_minlen(ap, args, blen, notinit);
3269 return 0;
3270 }
3271
3272 STATIC int
3273 xfs_bmap_btalloc_filestreams(
3274 struct xfs_bmalloca *ap,
3275 struct xfs_alloc_arg *args,
3276 xfs_extlen_t *blen)
3277 {
3278 struct xfs_mount *mp = ap->ip->i_mount;
3279 xfs_agnumber_t ag;
3280 int notinit = 0;
3281 int error;
3282
3283 args->type = XFS_ALLOCTYPE_NEAR_BNO;
3284 args->total = ap->total;
3285
3286 ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3287 if (ag == NULLAGNUMBER)
3288 ag = 0;
3289
3290 error = xfs_bmap_longest_free_extent(args->tp, ag, blen, ¬init);
3291 if (error)
3292 return error;
3293
3294 if (*blen < args->maxlen) {
3295 error = xfs_filestream_new_ag(ap, &ag);
3296 if (error)
3297 return error;
3298
3299 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3300 ¬init);
3301 if (error)
3302 return error;
3303
3304 }
3305
3306 xfs_bmap_select_minlen(ap, args, blen, notinit);
3307
3308
3309
3310
3311
3312 ap->blkno = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0);
3313 return 0;
3314 }
3315
3316
3317 static void
3318 xfs_bmap_btalloc_accounting(
3319 struct xfs_bmalloca *ap,
3320 struct xfs_alloc_arg *args)
3321 {
3322 if (ap->flags & XFS_BMAPI_COWFORK) {
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332 if (ap->wasdel) {
3333 xfs_mod_delalloc(ap->ip->i_mount, -(int64_t)args->len);
3334 return;
3335 }
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345 ap->ip->i_delayed_blks += args->len;
3346 xfs_trans_mod_dquot_byino(ap->tp, ap->ip, XFS_TRANS_DQ_RES_BLKS,
3347 -(long)args->len);
3348 return;
3349 }
3350
3351
3352 ap->ip->i_nblocks += args->len;
3353 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
3354 if (ap->wasdel) {
3355 ap->ip->i_delayed_blks -= args->len;
3356 xfs_mod_delalloc(ap->ip->i_mount, -(int64_t)args->len);
3357 }
3358 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
3359 ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT : XFS_TRANS_DQ_BCOUNT,
3360 args->len);
3361 }
3362
3363 static int
3364 xfs_bmap_compute_alignments(
3365 struct xfs_bmalloca *ap,
3366 struct xfs_alloc_arg *args)
3367 {
3368 struct xfs_mount *mp = args->mp;
3369 xfs_extlen_t align = 0;
3370 int stripe_align = 0;
3371
3372
3373 if (mp->m_swidth && xfs_has_swalloc(mp))
3374 stripe_align = mp->m_swidth;
3375 else if (mp->m_dalign)
3376 stripe_align = mp->m_dalign;
3377
3378 if (ap->flags & XFS_BMAPI_COWFORK)
3379 align = xfs_get_cowextsz_hint(ap->ip);
3380 else if (ap->datatype & XFS_ALLOC_USERDATA)
3381 align = xfs_get_extsz_hint(ap->ip);
3382 if (align) {
3383 if (xfs_bmap_extsize_align(mp, &ap->got, &ap->prev, align, 0,
3384 ap->eof, 0, ap->conv, &ap->offset,
3385 &ap->length))
3386 ASSERT(0);
3387 ASSERT(ap->length);
3388 }
3389
3390
3391 if (align) {
3392 args->prod = align;
3393 div_u64_rem(ap->offset, args->prod, &args->mod);
3394 if (args->mod)
3395 args->mod = args->prod - args->mod;
3396 } else if (mp->m_sb.sb_blocksize >= PAGE_SIZE) {
3397 args->prod = 1;
3398 args->mod = 0;
3399 } else {
3400 args->prod = PAGE_SIZE >> mp->m_sb.sb_blocklog;
3401 div_u64_rem(ap->offset, args->prod, &args->mod);
3402 if (args->mod)
3403 args->mod = args->prod - args->mod;
3404 }
3405
3406 return stripe_align;
3407 }
3408
3409 static void
3410 xfs_bmap_process_allocated_extent(
3411 struct xfs_bmalloca *ap,
3412 struct xfs_alloc_arg *args,
3413 xfs_fileoff_t orig_offset,
3414 xfs_extlen_t orig_length)
3415 {
3416 int nullfb;
3417
3418 nullfb = ap->tp->t_firstblock == NULLFSBLOCK;
3419
3420
3421
3422
3423
3424 ASSERT(nullfb ||
3425 XFS_FSB_TO_AGNO(args->mp, ap->tp->t_firstblock) <=
3426 XFS_FSB_TO_AGNO(args->mp, args->fsbno));
3427
3428 ap->blkno = args->fsbno;
3429 if (nullfb)
3430 ap->tp->t_firstblock = args->fsbno;
3431 ap->length = args->len;
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444 if (ap->length <= orig_length)
3445 ap->offset = orig_offset;
3446 else if (ap->offset + ap->length < orig_offset + orig_length)
3447 ap->offset = orig_offset + orig_length - ap->length;
3448 xfs_bmap_btalloc_accounting(ap, args);
3449 }
3450
3451 #ifdef DEBUG
3452 static int
3453 xfs_bmap_exact_minlen_extent_alloc(
3454 struct xfs_bmalloca *ap)
3455 {
3456 struct xfs_mount *mp = ap->ip->i_mount;
3457 struct xfs_alloc_arg args = { .tp = ap->tp, .mp = mp };
3458 xfs_fileoff_t orig_offset;
3459 xfs_extlen_t orig_length;
3460 int error;
3461
3462 ASSERT(ap->length);
3463
3464 if (ap->minlen != 1) {
3465 ap->blkno = NULLFSBLOCK;
3466 ap->length = 0;
3467 return 0;
3468 }
3469
3470 orig_offset = ap->offset;
3471 orig_length = ap->length;
3472
3473 args.alloc_minlen_only = 1;
3474
3475 xfs_bmap_compute_alignments(ap, &args);
3476
3477 if (ap->tp->t_firstblock == NULLFSBLOCK) {
3478
3479
3480
3481
3482
3483
3484
3485
3486 ap->blkno = XFS_AGB_TO_FSB(mp, 0, 0);
3487 } else {
3488 ap->blkno = ap->tp->t_firstblock;
3489 }
3490
3491 args.fsbno = ap->blkno;
3492 args.oinfo = XFS_RMAP_OINFO_SKIP_UPDATE;
3493 args.type = XFS_ALLOCTYPE_FIRST_AG;
3494 args.minlen = args.maxlen = ap->minlen;
3495 args.total = ap->total;
3496
3497 args.alignment = 1;
3498 args.minalignslop = 0;
3499
3500 args.minleft = ap->minleft;
3501 args.wasdel = ap->wasdel;
3502 args.resv = XFS_AG_RESV_NONE;
3503 args.datatype = ap->datatype;
3504
3505 error = xfs_alloc_vextent(&args);
3506 if (error)
3507 return error;
3508
3509 if (args.fsbno != NULLFSBLOCK) {
3510 xfs_bmap_process_allocated_extent(ap, &args, orig_offset,
3511 orig_length);
3512 } else {
3513 ap->blkno = NULLFSBLOCK;
3514 ap->length = 0;
3515 }
3516
3517 return 0;
3518 }
3519 #else
3520
3521 #define xfs_bmap_exact_minlen_extent_alloc(bma) (-EFSCORRUPTED)
3522
3523 #endif
3524
3525 STATIC int
3526 xfs_bmap_btalloc(
3527 struct xfs_bmalloca *ap)
3528 {
3529 struct xfs_mount *mp = ap->ip->i_mount;
3530 struct xfs_alloc_arg args = { .tp = ap->tp, .mp = mp };
3531 xfs_alloctype_t atype = 0;
3532 xfs_agnumber_t fb_agno;
3533 xfs_agnumber_t ag;
3534 xfs_fileoff_t orig_offset;
3535 xfs_extlen_t orig_length;
3536 xfs_extlen_t blen;
3537 xfs_extlen_t nextminlen = 0;
3538 int nullfb;
3539 int isaligned;
3540 int tryagain;
3541 int error;
3542 int stripe_align;
3543
3544 ASSERT(ap->length);
3545 orig_offset = ap->offset;
3546 orig_length = ap->length;
3547
3548 stripe_align = xfs_bmap_compute_alignments(ap, &args);
3549
3550 nullfb = ap->tp->t_firstblock == NULLFSBLOCK;
3551 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp,
3552 ap->tp->t_firstblock);
3553 if (nullfb) {
3554 if ((ap->datatype & XFS_ALLOC_USERDATA) &&
3555 xfs_inode_is_filestream(ap->ip)) {
3556 ag = xfs_filestream_lookup_ag(ap->ip);
3557 ag = (ag != NULLAGNUMBER) ? ag : 0;
3558 ap->blkno = XFS_AGB_TO_FSB(mp, ag, 0);
3559 } else {
3560 ap->blkno = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
3561 }
3562 } else
3563 ap->blkno = ap->tp->t_firstblock;
3564
3565 xfs_bmap_adjacent(ap);
3566
3567
3568
3569
3570
3571 if (nullfb || XFS_FSB_TO_AGNO(mp, ap->blkno) == fb_agno)
3572 ;
3573 else
3574 ap->blkno = ap->tp->t_firstblock;
3575
3576
3577
3578 tryagain = isaligned = 0;
3579 args.fsbno = ap->blkno;
3580 args.oinfo = XFS_RMAP_OINFO_SKIP_UPDATE;
3581
3582
3583 args.maxlen = min(ap->length, mp->m_ag_max_usable);
3584 blen = 0;
3585 if (nullfb) {
3586
3587
3588
3589
3590
3591 if ((ap->datatype & XFS_ALLOC_USERDATA) &&
3592 xfs_inode_is_filestream(ap->ip))
3593 error = xfs_bmap_btalloc_filestreams(ap, &args, &blen);
3594 else
3595 error = xfs_bmap_btalloc_nullfb(ap, &args, &blen);
3596 if (error)
3597 return error;
3598 } else if (ap->tp->t_flags & XFS_TRANS_LOWMODE) {
3599 if (xfs_inode_is_filestream(ap->ip))
3600 args.type = XFS_ALLOCTYPE_FIRST_AG;
3601 else
3602 args.type = XFS_ALLOCTYPE_START_BNO;
3603 args.total = args.minlen = ap->minlen;
3604 } else {
3605 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3606 args.total = ap->total;
3607 args.minlen = ap->minlen;
3608 }
3609
3610
3611
3612
3613
3614
3615
3616
3617 if (!(ap->tp->t_flags & XFS_TRANS_LOWMODE) && ap->aeof) {
3618 if (!ap->offset) {
3619 args.alignment = stripe_align;
3620 atype = args.type;
3621 isaligned = 1;
3622
3623
3624
3625
3626 if (blen > args.alignment &&
3627 blen <= args.maxlen + args.alignment)
3628 args.minlen = blen - args.alignment;
3629 args.minalignslop = 0;
3630 } else {
3631
3632
3633
3634
3635
3636 atype = args.type;
3637 tryagain = 1;
3638 args.type = XFS_ALLOCTYPE_THIS_BNO;
3639 args.alignment = 1;
3640
3641
3642
3643
3644
3645
3646 if (blen > stripe_align && blen <= args.maxlen)
3647 nextminlen = blen - stripe_align;
3648 else
3649 nextminlen = args.minlen;
3650 if (nextminlen + stripe_align > args.minlen + 1)
3651 args.minalignslop =
3652 nextminlen + stripe_align -
3653 args.minlen - 1;
3654 else
3655 args.minalignslop = 0;
3656 }
3657 } else {
3658 args.alignment = 1;
3659 args.minalignslop = 0;
3660 }
3661 args.minleft = ap->minleft;
3662 args.wasdel = ap->wasdel;
3663 args.resv = XFS_AG_RESV_NONE;
3664 args.datatype = ap->datatype;
3665
3666 error = xfs_alloc_vextent(&args);
3667 if (error)
3668 return error;
3669
3670 if (tryagain && args.fsbno == NULLFSBLOCK) {
3671
3672
3673
3674
3675 args.type = atype;
3676 args.fsbno = ap->blkno;
3677 args.alignment = stripe_align;
3678 args.minlen = nextminlen;
3679 args.minalignslop = 0;
3680 isaligned = 1;
3681 if ((error = xfs_alloc_vextent(&args)))
3682 return error;
3683 }
3684 if (isaligned && args.fsbno == NULLFSBLOCK) {
3685
3686
3687
3688
3689 args.type = atype;
3690 args.fsbno = ap->blkno;
3691 args.alignment = 0;
3692 if ((error = xfs_alloc_vextent(&args)))
3693 return error;
3694 }
3695 if (args.fsbno == NULLFSBLOCK && nullfb &&
3696 args.minlen > ap->minlen) {
3697 args.minlen = ap->minlen;
3698 args.type = XFS_ALLOCTYPE_START_BNO;
3699 args.fsbno = ap->blkno;
3700 if ((error = xfs_alloc_vextent(&args)))
3701 return error;
3702 }
3703 if (args.fsbno == NULLFSBLOCK && nullfb) {
3704 args.fsbno = 0;
3705 args.type = XFS_ALLOCTYPE_FIRST_AG;
3706 args.total = ap->minlen;
3707 if ((error = xfs_alloc_vextent(&args)))
3708 return error;
3709 ap->tp->t_flags |= XFS_TRANS_LOWMODE;
3710 }
3711
3712 if (args.fsbno != NULLFSBLOCK) {
3713 xfs_bmap_process_allocated_extent(ap, &args, orig_offset,
3714 orig_length);
3715 } else {
3716 ap->blkno = NULLFSBLOCK;
3717 ap->length = 0;
3718 }
3719 return 0;
3720 }
3721
3722
3723 void
3724 xfs_trim_extent(
3725 struct xfs_bmbt_irec *irec,
3726 xfs_fileoff_t bno,
3727 xfs_filblks_t len)
3728 {
3729 xfs_fileoff_t distance;
3730 xfs_fileoff_t end = bno + len;
3731
3732 if (irec->br_startoff + irec->br_blockcount <= bno ||
3733 irec->br_startoff >= end) {
3734 irec->br_blockcount = 0;
3735 return;
3736 }
3737
3738 if (irec->br_startoff < bno) {
3739 distance = bno - irec->br_startoff;
3740 if (isnullstartblock(irec->br_startblock))
3741 irec->br_startblock = DELAYSTARTBLOCK;
3742 if (irec->br_startblock != DELAYSTARTBLOCK &&
3743 irec->br_startblock != HOLESTARTBLOCK)
3744 irec->br_startblock += distance;
3745 irec->br_startoff += distance;
3746 irec->br_blockcount -= distance;
3747 }
3748
3749 if (end < irec->br_startoff + irec->br_blockcount) {
3750 distance = irec->br_startoff + irec->br_blockcount - end;
3751 irec->br_blockcount -= distance;
3752 }
3753 }
3754
3755
3756
3757
3758 STATIC void
3759 xfs_bmapi_trim_map(
3760 struct xfs_bmbt_irec *mval,
3761 struct xfs_bmbt_irec *got,
3762 xfs_fileoff_t *bno,
3763 xfs_filblks_t len,
3764 xfs_fileoff_t obno,
3765 xfs_fileoff_t end,
3766 int n,
3767 uint32_t flags)
3768 {
3769 if ((flags & XFS_BMAPI_ENTIRE) ||
3770 got->br_startoff + got->br_blockcount <= obno) {
3771 *mval = *got;
3772 if (isnullstartblock(got->br_startblock))
3773 mval->br_startblock = DELAYSTARTBLOCK;
3774 return;
3775 }
3776
3777 if (obno > *bno)
3778 *bno = obno;
3779 ASSERT((*bno >= obno) || (n == 0));
3780 ASSERT(*bno < end);
3781 mval->br_startoff = *bno;
3782 if (isnullstartblock(got->br_startblock))
3783 mval->br_startblock = DELAYSTARTBLOCK;
3784 else
3785 mval->br_startblock = got->br_startblock +
3786 (*bno - got->br_startoff);
3787
3788
3789
3790
3791
3792
3793
3794 mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno,
3795 got->br_blockcount - (*bno - got->br_startoff));
3796 mval->br_state = got->br_state;
3797 ASSERT(mval->br_blockcount <= len);
3798 return;
3799 }
3800
3801
3802
3803
3804 STATIC void
3805 xfs_bmapi_update_map(
3806 struct xfs_bmbt_irec **map,
3807 xfs_fileoff_t *bno,
3808 xfs_filblks_t *len,
3809 xfs_fileoff_t obno,
3810 xfs_fileoff_t end,
3811 int *n,
3812 uint32_t flags)
3813 {
3814 xfs_bmbt_irec_t *mval = *map;
3815
3816 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
3817 ((mval->br_startoff + mval->br_blockcount) <= end));
3818 ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) ||
3819 (mval->br_startoff < obno));
3820
3821 *bno = mval->br_startoff + mval->br_blockcount;
3822 *len = end - *bno;
3823 if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) {
3824
3825 ASSERT(mval->br_startblock == mval[-1].br_startblock);
3826 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
3827 ASSERT(mval->br_state == mval[-1].br_state);
3828 mval[-1].br_blockcount = mval->br_blockcount;
3829 mval[-1].br_state = mval->br_state;
3830 } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
3831 mval[-1].br_startblock != DELAYSTARTBLOCK &&
3832 mval[-1].br_startblock != HOLESTARTBLOCK &&
3833 mval->br_startblock == mval[-1].br_startblock +
3834 mval[-1].br_blockcount &&
3835 mval[-1].br_state == mval->br_state) {
3836 ASSERT(mval->br_startoff ==
3837 mval[-1].br_startoff + mval[-1].br_blockcount);
3838 mval[-1].br_blockcount += mval->br_blockcount;
3839 } else if (*n > 0 &&
3840 mval->br_startblock == DELAYSTARTBLOCK &&
3841 mval[-1].br_startblock == DELAYSTARTBLOCK &&
3842 mval->br_startoff ==
3843 mval[-1].br_startoff + mval[-1].br_blockcount) {
3844 mval[-1].br_blockcount += mval->br_blockcount;
3845 mval[-1].br_state = mval->br_state;
3846 } else if (!((*n == 0) &&
3847 ((mval->br_startoff + mval->br_blockcount) <=
3848 obno))) {
3849 mval++;
3850 (*n)++;
3851 }
3852 *map = mval;
3853 }
3854
3855
3856
3857
3858 int
3859 xfs_bmapi_read(
3860 struct xfs_inode *ip,
3861 xfs_fileoff_t bno,
3862 xfs_filblks_t len,
3863 struct xfs_bmbt_irec *mval,
3864 int *nmap,
3865 uint32_t flags)
3866 {
3867 struct xfs_mount *mp = ip->i_mount;
3868 int whichfork = xfs_bmapi_whichfork(flags);
3869 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
3870 struct xfs_bmbt_irec got;
3871 xfs_fileoff_t obno;
3872 xfs_fileoff_t end;
3873 struct xfs_iext_cursor icur;
3874 int error;
3875 bool eof = false;
3876 int n = 0;
3877
3878 ASSERT(*nmap >= 1);
3879 ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK | XFS_BMAPI_ENTIRE)));
3880 ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED|XFS_ILOCK_EXCL));
3881
3882 if (WARN_ON_ONCE(!ifp))
3883 return -EFSCORRUPTED;
3884
3885 if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
3886 XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT))
3887 return -EFSCORRUPTED;
3888
3889 if (xfs_is_shutdown(mp))
3890 return -EIO;
3891
3892 XFS_STATS_INC(mp, xs_blk_mapr);
3893
3894 error = xfs_iread_extents(NULL, ip, whichfork);
3895 if (error)
3896 return error;
3897
3898 if (!xfs_iext_lookup_extent(ip, ifp, bno, &icur, &got))
3899 eof = true;
3900 end = bno + len;
3901 obno = bno;
3902
3903 while (bno < end && n < *nmap) {
3904
3905 if (eof)
3906 got.br_startoff = end;
3907 if (got.br_startoff > bno) {
3908
3909 mval->br_startoff = bno;
3910 mval->br_startblock = HOLESTARTBLOCK;
3911 mval->br_blockcount =
3912 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
3913 mval->br_state = XFS_EXT_NORM;
3914 bno += mval->br_blockcount;
3915 len -= mval->br_blockcount;
3916 mval++;
3917 n++;
3918 continue;
3919 }
3920
3921
3922 xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
3923 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
3924
3925
3926 if (bno >= end || n >= *nmap)
3927 break;
3928
3929
3930 if (!xfs_iext_next_extent(ifp, &icur, &got))
3931 eof = true;
3932 }
3933 *nmap = n;
3934 return 0;
3935 }
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950 int
3951 xfs_bmapi_reserve_delalloc(
3952 struct xfs_inode *ip,
3953 int whichfork,
3954 xfs_fileoff_t off,
3955 xfs_filblks_t len,
3956 xfs_filblks_t prealloc,
3957 struct xfs_bmbt_irec *got,
3958 struct xfs_iext_cursor *icur,
3959 int eof)
3960 {
3961 struct xfs_mount *mp = ip->i_mount;
3962 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
3963 xfs_extlen_t alen;
3964 xfs_extlen_t indlen;
3965 int error;
3966 xfs_fileoff_t aoff = off;
3967
3968
3969
3970
3971
3972 alen = XFS_FILBLKS_MIN(len + prealloc, XFS_MAX_BMBT_EXTLEN);
3973 if (!eof)
3974 alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff);
3975 if (prealloc && alen >= len)
3976 prealloc = alen - len;
3977
3978
3979 if (whichfork == XFS_COW_FORK) {
3980 struct xfs_bmbt_irec prev;
3981 xfs_extlen_t extsz = xfs_get_cowextsz_hint(ip);
3982
3983 if (!xfs_iext_peek_prev_extent(ifp, icur, &prev))
3984 prev.br_startoff = NULLFILEOFF;
3985
3986 error = xfs_bmap_extsize_align(mp, got, &prev, extsz, 0, eof,
3987 1, 0, &aoff, &alen);
3988 ASSERT(!error);
3989 }
3990
3991
3992
3993
3994
3995
3996 error = xfs_quota_reserve_blkres(ip, alen);
3997 if (error)
3998 return error;
3999
4000
4001
4002
4003
4004 indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen);
4005 ASSERT(indlen > 0);
4006
4007 error = xfs_mod_fdblocks(mp, -((int64_t)alen), false);
4008 if (error)
4009 goto out_unreserve_quota;
4010
4011 error = xfs_mod_fdblocks(mp, -((int64_t)indlen), false);
4012 if (error)
4013 goto out_unreserve_blocks;
4014
4015
4016 ip->i_delayed_blks += alen;
4017 xfs_mod_delalloc(ip->i_mount, alen + indlen);
4018
4019 got->br_startoff = aoff;
4020 got->br_startblock = nullstartblock(indlen);
4021 got->br_blockcount = alen;
4022 got->br_state = XFS_EXT_NORM;
4023
4024 xfs_bmap_add_extent_hole_delay(ip, whichfork, icur, got);
4025
4026
4027
4028
4029
4030
4031 if (whichfork == XFS_DATA_FORK && prealloc)
4032 xfs_inode_set_eofblocks_tag(ip);
4033 if (whichfork == XFS_COW_FORK && (prealloc || aoff < off || alen > len))
4034 xfs_inode_set_cowblocks_tag(ip);
4035
4036 return 0;
4037
4038 out_unreserve_blocks:
4039 xfs_mod_fdblocks(mp, alen, false);
4040 out_unreserve_quota:
4041 if (XFS_IS_QUOTA_ON(mp))
4042 xfs_quota_unreserve_blkres(ip, alen);
4043 return error;
4044 }
4045
4046 static int
4047 xfs_bmap_alloc_userdata(
4048 struct xfs_bmalloca *bma)
4049 {
4050 struct xfs_mount *mp = bma->ip->i_mount;
4051 int whichfork = xfs_bmapi_whichfork(bma->flags);
4052 int error;
4053
4054
4055
4056
4057
4058
4059
4060 bma->datatype = XFS_ALLOC_NOBUSY;
4061 if (whichfork == XFS_DATA_FORK) {
4062 bma->datatype |= XFS_ALLOC_USERDATA;
4063 if (bma->offset == 0)
4064 bma->datatype |= XFS_ALLOC_INITIAL_USER_DATA;
4065
4066 if (mp->m_dalign && bma->length >= mp->m_dalign) {
4067 error = xfs_bmap_isaeof(bma, whichfork);
4068 if (error)
4069 return error;
4070 }
4071
4072 if (XFS_IS_REALTIME_INODE(bma->ip))
4073 return xfs_bmap_rtalloc(bma);
4074 }
4075
4076 if (unlikely(XFS_TEST_ERROR(false, mp,
4077 XFS_ERRTAG_BMAP_ALLOC_MINLEN_EXTENT)))
4078 return xfs_bmap_exact_minlen_extent_alloc(bma);
4079
4080 return xfs_bmap_btalloc(bma);
4081 }
4082
4083 static int
4084 xfs_bmapi_allocate(
4085 struct xfs_bmalloca *bma)
4086 {
4087 struct xfs_mount *mp = bma->ip->i_mount;
4088 int whichfork = xfs_bmapi_whichfork(bma->flags);
4089 struct xfs_ifork *ifp = xfs_ifork_ptr(bma->ip, whichfork);
4090 int tmp_logflags = 0;
4091 int error;
4092
4093 ASSERT(bma->length > 0);
4094
4095
4096
4097
4098
4099 if (bma->wasdel) {
4100 bma->length = (xfs_extlen_t)bma->got.br_blockcount;
4101 bma->offset = bma->got.br_startoff;
4102 if (!xfs_iext_peek_prev_extent(ifp, &bma->icur, &bma->prev))
4103 bma->prev.br_startoff = NULLFILEOFF;
4104 } else {
4105 bma->length = XFS_FILBLKS_MIN(bma->length, XFS_MAX_BMBT_EXTLEN);
4106 if (!bma->eof)
4107 bma->length = XFS_FILBLKS_MIN(bma->length,
4108 bma->got.br_startoff - bma->offset);
4109 }
4110
4111 if (bma->flags & XFS_BMAPI_CONTIG)
4112 bma->minlen = bma->length;
4113 else
4114 bma->minlen = 1;
4115
4116 if (bma->flags & XFS_BMAPI_METADATA) {
4117 if (unlikely(XFS_TEST_ERROR(false, mp,
4118 XFS_ERRTAG_BMAP_ALLOC_MINLEN_EXTENT)))
4119 error = xfs_bmap_exact_minlen_extent_alloc(bma);
4120 else
4121 error = xfs_bmap_btalloc(bma);
4122 } else {
4123 error = xfs_bmap_alloc_userdata(bma);
4124 }
4125 if (error || bma->blkno == NULLFSBLOCK)
4126 return error;
4127
4128 if (bma->flags & XFS_BMAPI_ZERO) {
4129 error = xfs_zero_extent(bma->ip, bma->blkno, bma->length);
4130 if (error)
4131 return error;
4132 }
4133
4134 if (ifp->if_format == XFS_DINODE_FMT_BTREE && !bma->cur)
4135 bma->cur = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork);
4136
4137
4138
4139
4140 bma->nallocs++;
4141
4142 if (bma->cur)
4143 bma->cur->bc_ino.flags =
4144 bma->wasdel ? XFS_BTCUR_BMBT_WASDEL : 0;
4145
4146 bma->got.br_startoff = bma->offset;
4147 bma->got.br_startblock = bma->blkno;
4148 bma->got.br_blockcount = bma->length;
4149 bma->got.br_state = XFS_EXT_NORM;
4150
4151 if (bma->flags & XFS_BMAPI_PREALLOC)
4152 bma->got.br_state = XFS_EXT_UNWRITTEN;
4153
4154 if (bma->wasdel)
4155 error = xfs_bmap_add_extent_delay_real(bma, whichfork);
4156 else
4157 error = xfs_bmap_add_extent_hole_real(bma->tp, bma->ip,
4158 whichfork, &bma->icur, &bma->cur, &bma->got,
4159 &bma->logflags, bma->flags);
4160
4161 bma->logflags |= tmp_logflags;
4162 if (error)
4163 return error;
4164
4165
4166
4167
4168
4169
4170 xfs_iext_get_extent(ifp, &bma->icur, &bma->got);
4171
4172 ASSERT(bma->got.br_startoff <= bma->offset);
4173 ASSERT(bma->got.br_startoff + bma->got.br_blockcount >=
4174 bma->offset + bma->length);
4175 ASSERT(bma->got.br_state == XFS_EXT_NORM ||
4176 bma->got.br_state == XFS_EXT_UNWRITTEN);
4177 return 0;
4178 }
4179
4180 STATIC int
4181 xfs_bmapi_convert_unwritten(
4182 struct xfs_bmalloca *bma,
4183 struct xfs_bmbt_irec *mval,
4184 xfs_filblks_t len,
4185 uint32_t flags)
4186 {
4187 int whichfork = xfs_bmapi_whichfork(flags);
4188 struct xfs_ifork *ifp = xfs_ifork_ptr(bma->ip, whichfork);
4189 int tmp_logflags = 0;
4190 int error;
4191
4192
4193 if (mval->br_state == XFS_EXT_UNWRITTEN &&
4194 (flags & XFS_BMAPI_PREALLOC))
4195 return 0;
4196
4197
4198 if (mval->br_state == XFS_EXT_NORM &&
4199 (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) !=
4200 (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT))
4201 return 0;
4202
4203
4204
4205
4206 ASSERT(mval->br_blockcount <= len);
4207 if (ifp->if_format == XFS_DINODE_FMT_BTREE && !bma->cur) {
4208 bma->cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp,
4209 bma->ip, whichfork);
4210 }
4211 mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN)
4212 ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
4213
4214
4215
4216
4217
4218 if (flags & XFS_BMAPI_ZERO) {
4219 error = xfs_zero_extent(bma->ip, mval->br_startblock,
4220 mval->br_blockcount);
4221 if (error)
4222 return error;
4223 }
4224
4225 error = xfs_bmap_add_extent_unwritten_real(bma->tp, bma->ip, whichfork,
4226 &bma->icur, &bma->cur, mval, &tmp_logflags);
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238 if (whichfork != XFS_COW_FORK)
4239 bma->logflags |= tmp_logflags | XFS_ILOG_CORE;
4240 if (error)
4241 return error;
4242
4243
4244
4245
4246
4247
4248 xfs_iext_get_extent(ifp, &bma->icur, &bma->got);
4249
4250
4251
4252
4253
4254 if (mval->br_blockcount < len)
4255 return -EAGAIN;
4256 return 0;
4257 }
4258
4259 static inline xfs_extlen_t
4260 xfs_bmapi_minleft(
4261 struct xfs_trans *tp,
4262 struct xfs_inode *ip,
4263 int fork)
4264 {
4265 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, fork);
4266
4267 if (tp && tp->t_firstblock != NULLFSBLOCK)
4268 return 0;
4269 if (ifp->if_format != XFS_DINODE_FMT_BTREE)
4270 return 1;
4271 return be16_to_cpu(ifp->if_broot->bb_level) + 1;
4272 }
4273
4274
4275
4276
4277
4278
4279
4280 static void
4281 xfs_bmapi_finish(
4282 struct xfs_bmalloca *bma,
4283 int whichfork,
4284 int error)
4285 {
4286 struct xfs_ifork *ifp = xfs_ifork_ptr(bma->ip, whichfork);
4287
4288 if ((bma->logflags & xfs_ilog_fext(whichfork)) &&
4289 ifp->if_format != XFS_DINODE_FMT_EXTENTS)
4290 bma->logflags &= ~xfs_ilog_fext(whichfork);
4291 else if ((bma->logflags & xfs_ilog_fbroot(whichfork)) &&
4292 ifp->if_format != XFS_DINODE_FMT_BTREE)
4293 bma->logflags &= ~xfs_ilog_fbroot(whichfork);
4294
4295 if (bma->logflags)
4296 xfs_trans_log_inode(bma->tp, bma->ip, bma->logflags);
4297 if (bma->cur)
4298 xfs_btree_del_cursor(bma->cur, error);
4299 }
4300
4301
4302
4303
4304
4305
4306
4307 int
4308 xfs_bmapi_write(
4309 struct xfs_trans *tp,
4310 struct xfs_inode *ip,
4311 xfs_fileoff_t bno,
4312 xfs_filblks_t len,
4313 uint32_t flags,
4314 xfs_extlen_t total,
4315 struct xfs_bmbt_irec *mval,
4316 int *nmap)
4317 {
4318 struct xfs_bmalloca bma = {
4319 .tp = tp,
4320 .ip = ip,
4321 .total = total,
4322 };
4323 struct xfs_mount *mp = ip->i_mount;
4324 int whichfork = xfs_bmapi_whichfork(flags);
4325 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
4326 xfs_fileoff_t end;
4327 bool eof = false;
4328 int error;
4329 int n;
4330 xfs_fileoff_t obno;
4331
4332 #ifdef DEBUG
4333 xfs_fileoff_t orig_bno;
4334 int orig_flags;
4335 xfs_filblks_t orig_len;
4336 struct xfs_bmbt_irec *orig_mval;
4337 int orig_nmap;
4338
4339 orig_bno = bno;
4340 orig_len = len;
4341 orig_flags = flags;
4342 orig_mval = mval;
4343 orig_nmap = *nmap;
4344 #endif
4345
4346 ASSERT(*nmap >= 1);
4347 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP);
4348 ASSERT(tp != NULL);
4349 ASSERT(len > 0);
4350 ASSERT(ifp->if_format != XFS_DINODE_FMT_LOCAL);
4351 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
4352 ASSERT(!(flags & XFS_BMAPI_REMAP));
4353
4354
4355 ASSERT((flags & (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO)) !=
4356 (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO));
4357
4358
4359
4360
4361
4362
4363 ASSERT((flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)) !=
4364 (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO));
4365
4366 if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
4367 XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
4368 return -EFSCORRUPTED;
4369 }
4370
4371 if (xfs_is_shutdown(mp))
4372 return -EIO;
4373
4374 XFS_STATS_INC(mp, xs_blk_mapw);
4375
4376 error = xfs_iread_extents(tp, ip, whichfork);
4377 if (error)
4378 goto error0;
4379
4380 if (!xfs_iext_lookup_extent(ip, ifp, bno, &bma.icur, &bma.got))
4381 eof = true;
4382 if (!xfs_iext_peek_prev_extent(ifp, &bma.icur, &bma.prev))
4383 bma.prev.br_startoff = NULLFILEOFF;
4384 bma.minleft = xfs_bmapi_minleft(tp, ip, whichfork);
4385
4386 n = 0;
4387 end = bno + len;
4388 obno = bno;
4389 while (bno < end && n < *nmap) {
4390 bool need_alloc = false, wasdelay = false;
4391
4392
4393 if (eof || bma.got.br_startoff > bno) {
4394
4395
4396
4397
4398
4399 ASSERT(!((flags & XFS_BMAPI_CONVERT) &&
4400 (flags & XFS_BMAPI_COWFORK)));
4401
4402 need_alloc = true;
4403 } else if (isnullstartblock(bma.got.br_startblock)) {
4404 wasdelay = true;
4405 }
4406
4407
4408
4409
4410
4411 if (need_alloc || wasdelay) {
4412 bma.eof = eof;
4413 bma.conv = !!(flags & XFS_BMAPI_CONVERT);
4414 bma.wasdel = wasdelay;
4415 bma.offset = bno;
4416 bma.flags = flags;
4417
4418
4419
4420
4421
4422
4423
4424
4425 if (len > (xfs_filblks_t)XFS_MAX_BMBT_EXTLEN)
4426 bma.length = XFS_MAX_BMBT_EXTLEN;
4427 else
4428 bma.length = len;
4429
4430 ASSERT(len > 0);
4431 ASSERT(bma.length > 0);
4432 error = xfs_bmapi_allocate(&bma);
4433 if (error)
4434 goto error0;
4435 if (bma.blkno == NULLFSBLOCK)
4436 break;
4437
4438
4439
4440
4441
4442 if (whichfork == XFS_COW_FORK)
4443 xfs_refcount_alloc_cow_extent(tp, bma.blkno,
4444 bma.length);
4445 }
4446
4447
4448 xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno,
4449 end, n, flags);
4450
4451
4452 error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags);
4453 if (error == -EAGAIN)
4454 continue;
4455 if (error)
4456 goto error0;
4457
4458
4459 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4460
4461
4462
4463
4464
4465
4466 if (bno >= end || n >= *nmap || bma.nallocs >= *nmap)
4467 break;
4468
4469
4470 bma.prev = bma.got;
4471 if (!xfs_iext_next_extent(ifp, &bma.icur, &bma.got))
4472 eof = true;
4473 }
4474 *nmap = n;
4475
4476 error = xfs_bmap_btree_to_extents(tp, ip, bma.cur, &bma.logflags,
4477 whichfork);
4478 if (error)
4479 goto error0;
4480
4481 ASSERT(ifp->if_format != XFS_DINODE_FMT_BTREE ||
4482 ifp->if_nextents > XFS_IFORK_MAXEXT(ip, whichfork));
4483 xfs_bmapi_finish(&bma, whichfork, 0);
4484 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
4485 orig_nmap, *nmap);
4486 return 0;
4487 error0:
4488 xfs_bmapi_finish(&bma, whichfork, error);
4489 return error;
4490 }
4491
4492
4493
4494
4495
4496
4497
4498 int
4499 xfs_bmapi_convert_delalloc(
4500 struct xfs_inode *ip,
4501 int whichfork,
4502 xfs_off_t offset,
4503 struct iomap *iomap,
4504 unsigned int *seq)
4505 {
4506 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
4507 struct xfs_mount *mp = ip->i_mount;
4508 xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
4509 struct xfs_bmalloca bma = { NULL };
4510 uint16_t flags = 0;
4511 struct xfs_trans *tp;
4512 int error;
4513
4514 if (whichfork == XFS_COW_FORK)
4515 flags |= IOMAP_F_SHARED;
4516
4517
4518
4519
4520
4521 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, 0, 0,
4522 XFS_TRANS_RESERVE, &tp);
4523 if (error)
4524 return error;
4525
4526 xfs_ilock(ip, XFS_ILOCK_EXCL);
4527 xfs_trans_ijoin(tp, ip, 0);
4528
4529 error = xfs_iext_count_may_overflow(ip, whichfork,
4530 XFS_IEXT_ADD_NOSPLIT_CNT);
4531 if (error == -EFBIG)
4532 error = xfs_iext_count_upgrade(tp, ip,
4533 XFS_IEXT_ADD_NOSPLIT_CNT);
4534 if (error)
4535 goto out_trans_cancel;
4536
4537 if (!xfs_iext_lookup_extent(ip, ifp, offset_fsb, &bma.icur, &bma.got) ||
4538 bma.got.br_startoff > offset_fsb) {
4539
4540
4541
4542
4543
4544 WARN_ON_ONCE(whichfork != XFS_COW_FORK);
4545 error = -EAGAIN;
4546 goto out_trans_cancel;
4547 }
4548
4549
4550
4551
4552
4553 if (!isnullstartblock(bma.got.br_startblock)) {
4554 xfs_bmbt_to_iomap(ip, iomap, &bma.got, 0, flags);
4555 *seq = READ_ONCE(ifp->if_seq);
4556 goto out_trans_cancel;
4557 }
4558
4559 bma.tp = tp;
4560 bma.ip = ip;
4561 bma.wasdel = true;
4562 bma.offset = bma.got.br_startoff;
4563 bma.length = max_t(xfs_filblks_t, bma.got.br_blockcount,
4564 XFS_MAX_BMBT_EXTLEN);
4565 bma.minleft = xfs_bmapi_minleft(tp, ip, whichfork);
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580 bma.flags = XFS_BMAPI_PREALLOC;
4581 if (whichfork == XFS_COW_FORK)
4582 bma.flags |= XFS_BMAPI_COWFORK;
4583
4584 if (!xfs_iext_peek_prev_extent(ifp, &bma.icur, &bma.prev))
4585 bma.prev.br_startoff = NULLFILEOFF;
4586
4587 error = xfs_bmapi_allocate(&bma);
4588 if (error)
4589 goto out_finish;
4590
4591 error = -ENOSPC;
4592 if (WARN_ON_ONCE(bma.blkno == NULLFSBLOCK))
4593 goto out_finish;
4594 error = -EFSCORRUPTED;
4595 if (WARN_ON_ONCE(!xfs_valid_startblock(ip, bma.got.br_startblock)))
4596 goto out_finish;
4597
4598 XFS_STATS_ADD(mp, xs_xstrat_bytes, XFS_FSB_TO_B(mp, bma.length));
4599 XFS_STATS_INC(mp, xs_xstrat_quick);
4600
4601 ASSERT(!isnullstartblock(bma.got.br_startblock));
4602 xfs_bmbt_to_iomap(ip, iomap, &bma.got, 0, flags);
4603 *seq = READ_ONCE(ifp->if_seq);
4604
4605 if (whichfork == XFS_COW_FORK)
4606 xfs_refcount_alloc_cow_extent(tp, bma.blkno, bma.length);
4607
4608 error = xfs_bmap_btree_to_extents(tp, ip, bma.cur, &bma.logflags,
4609 whichfork);
4610 if (error)
4611 goto out_finish;
4612
4613 xfs_bmapi_finish(&bma, whichfork, 0);
4614 error = xfs_trans_commit(tp);
4615 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4616 return error;
4617
4618 out_finish:
4619 xfs_bmapi_finish(&bma, whichfork, error);
4620 out_trans_cancel:
4621 xfs_trans_cancel(tp);
4622 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4623 return error;
4624 }
4625
4626 int
4627 xfs_bmapi_remap(
4628 struct xfs_trans *tp,
4629 struct xfs_inode *ip,
4630 xfs_fileoff_t bno,
4631 xfs_filblks_t len,
4632 xfs_fsblock_t startblock,
4633 uint32_t flags)
4634 {
4635 struct xfs_mount *mp = ip->i_mount;
4636 struct xfs_ifork *ifp;
4637 struct xfs_btree_cur *cur = NULL;
4638 struct xfs_bmbt_irec got;
4639 struct xfs_iext_cursor icur;
4640 int whichfork = xfs_bmapi_whichfork(flags);
4641 int logflags = 0, error;
4642
4643 ifp = xfs_ifork_ptr(ip, whichfork);
4644 ASSERT(len > 0);
4645 ASSERT(len <= (xfs_filblks_t)XFS_MAX_BMBT_EXTLEN);
4646 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
4647 ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK | XFS_BMAPI_PREALLOC |
4648 XFS_BMAPI_NORMAP)));
4649 ASSERT((flags & (XFS_BMAPI_ATTRFORK | XFS_BMAPI_PREALLOC)) !=
4650 (XFS_BMAPI_ATTRFORK | XFS_BMAPI_PREALLOC));
4651
4652 if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
4653 XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
4654 return -EFSCORRUPTED;
4655 }
4656
4657 if (xfs_is_shutdown(mp))
4658 return -EIO;
4659
4660 error = xfs_iread_extents(tp, ip, whichfork);
4661 if (error)
4662 return error;
4663
4664 if (xfs_iext_lookup_extent(ip, ifp, bno, &icur, &got)) {
4665
4666 ASSERT(got.br_startoff > bno);
4667 ASSERT(got.br_startoff - bno >= len);
4668 }
4669
4670 ip->i_nblocks += len;
4671 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
4672
4673 if (ifp->if_format == XFS_DINODE_FMT_BTREE) {
4674 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
4675 cur->bc_ino.flags = 0;
4676 }
4677
4678 got.br_startoff = bno;
4679 got.br_startblock = startblock;
4680 got.br_blockcount = len;
4681 if (flags & XFS_BMAPI_PREALLOC)
4682 got.br_state = XFS_EXT_UNWRITTEN;
4683 else
4684 got.br_state = XFS_EXT_NORM;
4685
4686 error = xfs_bmap_add_extent_hole_real(tp, ip, whichfork, &icur,
4687 &cur, &got, &logflags, flags);
4688 if (error)
4689 goto error0;
4690
4691 error = xfs_bmap_btree_to_extents(tp, ip, cur, &logflags, whichfork);
4692
4693 error0:
4694 if (ip->i_df.if_format != XFS_DINODE_FMT_EXTENTS)
4695 logflags &= ~XFS_ILOG_DEXT;
4696 else if (ip->i_df.if_format != XFS_DINODE_FMT_BTREE)
4697 logflags &= ~XFS_ILOG_DBROOT;
4698
4699 if (logflags)
4700 xfs_trans_log_inode(tp, ip, logflags);
4701 if (cur)
4702 xfs_btree_del_cursor(cur, error);
4703 return error;
4704 }
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718 static xfs_filblks_t
4719 xfs_bmap_split_indlen(
4720 xfs_filblks_t ores,
4721 xfs_filblks_t *indlen1,
4722 xfs_filblks_t *indlen2,
4723 xfs_filblks_t avail)
4724 {
4725 xfs_filblks_t len1 = *indlen1;
4726 xfs_filblks_t len2 = *indlen2;
4727 xfs_filblks_t nres = len1 + len2;
4728 xfs_filblks_t stolen = 0;
4729 xfs_filblks_t resfactor;
4730
4731
4732
4733
4734
4735 if (ores < nres && avail)
4736 stolen = XFS_FILBLKS_MIN(nres - ores, avail);
4737 ores += stolen;
4738
4739
4740 if (ores >= nres)
4741 return stolen;
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751 resfactor = (ores * 100);
4752 do_div(resfactor, nres);
4753 len1 *= resfactor;
4754 do_div(len1, 100);
4755 len2 *= resfactor;
4756 do_div(len2, 100);
4757 ASSERT(len1 + len2 <= ores);
4758 ASSERT(len1 < *indlen1 && len2 < *indlen2);
4759
4760
4761
4762
4763
4764
4765
4766 ores -= (len1 + len2);
4767 ASSERT((*indlen1 - len1) + (*indlen2 - len2) >= ores);
4768 if (ores && !len2 && *indlen2) {
4769 len2++;
4770 ores--;
4771 }
4772 while (ores) {
4773 if (len1 < *indlen1) {
4774 len1++;
4775 ores--;
4776 }
4777 if (!ores)
4778 break;
4779 if (len2 < *indlen2) {
4780 len2++;
4781 ores--;
4782 }
4783 }
4784
4785 *indlen1 = len1;
4786 *indlen2 = len2;
4787
4788 return stolen;
4789 }
4790
4791 int
4792 xfs_bmap_del_extent_delay(
4793 struct xfs_inode *ip,
4794 int whichfork,
4795 struct xfs_iext_cursor *icur,
4796 struct xfs_bmbt_irec *got,
4797 struct xfs_bmbt_irec *del)
4798 {
4799 struct xfs_mount *mp = ip->i_mount;
4800 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
4801 struct xfs_bmbt_irec new;
4802 int64_t da_old, da_new, da_diff = 0;
4803 xfs_fileoff_t del_endoff, got_endoff;
4804 xfs_filblks_t got_indlen, new_indlen, stolen;
4805 uint32_t state = xfs_bmap_fork_to_state(whichfork);
4806 int error = 0;
4807 bool isrt;
4808
4809 XFS_STATS_INC(mp, xs_del_exlist);
4810
4811 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
4812 del_endoff = del->br_startoff + del->br_blockcount;
4813 got_endoff = got->br_startoff + got->br_blockcount;
4814 da_old = startblockval(got->br_startblock);
4815 da_new = 0;
4816
4817 ASSERT(del->br_blockcount > 0);
4818 ASSERT(got->br_startoff <= del->br_startoff);
4819 ASSERT(got_endoff >= del_endoff);
4820
4821 if (isrt) {
4822 uint64_t rtexts = XFS_FSB_TO_B(mp, del->br_blockcount);
4823
4824 do_div(rtexts, mp->m_sb.sb_rextsize);
4825 xfs_mod_frextents(mp, rtexts);
4826 }
4827
4828
4829
4830
4831
4832
4833 ASSERT(!isrt);
4834 error = xfs_quota_unreserve_blkres(ip, del->br_blockcount);
4835 if (error)
4836 return error;
4837 ip->i_delayed_blks -= del->br_blockcount;
4838
4839 if (got->br_startoff == del->br_startoff)
4840 state |= BMAP_LEFT_FILLING;
4841 if (got_endoff == del_endoff)
4842 state |= BMAP_RIGHT_FILLING;
4843
4844 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
4845 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
4846
4847
4848
4849 xfs_iext_remove(ip, icur, state);
4850 xfs_iext_prev(ifp, icur);
4851 break;
4852 case BMAP_LEFT_FILLING:
4853
4854
4855
4856 got->br_startoff = del_endoff;
4857 got->br_blockcount -= del->br_blockcount;
4858 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
4859 got->br_blockcount), da_old);
4860 got->br_startblock = nullstartblock((int)da_new);
4861 xfs_iext_update_extent(ip, state, icur, got);
4862 break;
4863 case BMAP_RIGHT_FILLING:
4864
4865
4866
4867 got->br_blockcount = got->br_blockcount - del->br_blockcount;
4868 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
4869 got->br_blockcount), da_old);
4870 got->br_startblock = nullstartblock((int)da_new);
4871 xfs_iext_update_extent(ip, state, icur, got);
4872 break;
4873 case 0:
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883 got->br_blockcount = del->br_startoff - got->br_startoff;
4884 got_indlen = xfs_bmap_worst_indlen(ip, got->br_blockcount);
4885
4886 new.br_blockcount = got_endoff - del_endoff;
4887 new_indlen = xfs_bmap_worst_indlen(ip, new.br_blockcount);
4888
4889 WARN_ON_ONCE(!got_indlen || !new_indlen);
4890 stolen = xfs_bmap_split_indlen(da_old, &got_indlen, &new_indlen,
4891 del->br_blockcount);
4892
4893 got->br_startblock = nullstartblock((int)got_indlen);
4894
4895 new.br_startoff = del_endoff;
4896 new.br_state = got->br_state;
4897 new.br_startblock = nullstartblock((int)new_indlen);
4898
4899 xfs_iext_update_extent(ip, state, icur, got);
4900 xfs_iext_next(ifp, icur);
4901 xfs_iext_insert(ip, icur, &new, state);
4902
4903 da_new = got_indlen + new_indlen - stolen;
4904 del->br_blockcount -= stolen;
4905 break;
4906 }
4907
4908 ASSERT(da_old >= da_new);
4909 da_diff = da_old - da_new;
4910 if (!isrt)
4911 da_diff += del->br_blockcount;
4912 if (da_diff) {
4913 xfs_mod_fdblocks(mp, da_diff, false);
4914 xfs_mod_delalloc(mp, -da_diff);
4915 }
4916 return error;
4917 }
4918
4919 void
4920 xfs_bmap_del_extent_cow(
4921 struct xfs_inode *ip,
4922 struct xfs_iext_cursor *icur,
4923 struct xfs_bmbt_irec *got,
4924 struct xfs_bmbt_irec *del)
4925 {
4926 struct xfs_mount *mp = ip->i_mount;
4927 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, XFS_COW_FORK);
4928 struct xfs_bmbt_irec new;
4929 xfs_fileoff_t del_endoff, got_endoff;
4930 uint32_t state = BMAP_COWFORK;
4931
4932 XFS_STATS_INC(mp, xs_del_exlist);
4933
4934 del_endoff = del->br_startoff + del->br_blockcount;
4935 got_endoff = got->br_startoff + got->br_blockcount;
4936
4937 ASSERT(del->br_blockcount > 0);
4938 ASSERT(got->br_startoff <= del->br_startoff);
4939 ASSERT(got_endoff >= del_endoff);
4940 ASSERT(!isnullstartblock(got->br_startblock));
4941
4942 if (got->br_startoff == del->br_startoff)
4943 state |= BMAP_LEFT_FILLING;
4944 if (got_endoff == del_endoff)
4945 state |= BMAP_RIGHT_FILLING;
4946
4947 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
4948 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
4949
4950
4951
4952 xfs_iext_remove(ip, icur, state);
4953 xfs_iext_prev(ifp, icur);
4954 break;
4955 case BMAP_LEFT_FILLING:
4956
4957
4958
4959 got->br_startoff = del_endoff;
4960 got->br_blockcount -= del->br_blockcount;
4961 got->br_startblock = del->br_startblock + del->br_blockcount;
4962 xfs_iext_update_extent(ip, state, icur, got);
4963 break;
4964 case BMAP_RIGHT_FILLING:
4965
4966
4967
4968 got->br_blockcount -= del->br_blockcount;
4969 xfs_iext_update_extent(ip, state, icur, got);
4970 break;
4971 case 0:
4972
4973
4974
4975 got->br_blockcount = del->br_startoff - got->br_startoff;
4976
4977 new.br_startoff = del_endoff;
4978 new.br_blockcount = got_endoff - del_endoff;
4979 new.br_state = got->br_state;
4980 new.br_startblock = del->br_startblock + del->br_blockcount;
4981
4982 xfs_iext_update_extent(ip, state, icur, got);
4983 xfs_iext_next(ifp, icur);
4984 xfs_iext_insert(ip, icur, &new, state);
4985 break;
4986 }
4987 ip->i_delayed_blks -= del->br_blockcount;
4988 }
4989
4990
4991
4992
4993
4994 STATIC int
4995 xfs_bmap_del_extent_real(
4996 xfs_inode_t *ip,
4997 xfs_trans_t *tp,
4998 struct xfs_iext_cursor *icur,
4999 struct xfs_btree_cur *cur,
5000 xfs_bmbt_irec_t *del,
5001 int *logflagsp,
5002 int whichfork,
5003 uint32_t bflags)
5004 {
5005 xfs_fsblock_t del_endblock=0;
5006 xfs_fileoff_t del_endoff;
5007 int do_fx;
5008 int error;
5009 int flags = 0;
5010 struct xfs_bmbt_irec got;
5011 xfs_fileoff_t got_endoff;
5012 int i;
5013 struct xfs_ifork *ifp;
5014 xfs_mount_t *mp;
5015 xfs_filblks_t nblks;
5016 xfs_bmbt_irec_t new;
5017
5018 uint qfield;
5019 uint32_t state = xfs_bmap_fork_to_state(whichfork);
5020 struct xfs_bmbt_irec old;
5021
5022 mp = ip->i_mount;
5023 XFS_STATS_INC(mp, xs_del_exlist);
5024
5025 ifp = xfs_ifork_ptr(ip, whichfork);
5026 ASSERT(del->br_blockcount > 0);
5027 xfs_iext_get_extent(ifp, icur, &got);
5028 ASSERT(got.br_startoff <= del->br_startoff);
5029 del_endoff = del->br_startoff + del->br_blockcount;
5030 got_endoff = got.br_startoff + got.br_blockcount;
5031 ASSERT(got_endoff >= del_endoff);
5032 ASSERT(!isnullstartblock(got.br_startblock));
5033 qfield = 0;
5034 error = 0;
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044 if (tp->t_blk_res == 0 &&
5045 ifp->if_format == XFS_DINODE_FMT_EXTENTS &&
5046 ifp->if_nextents >= XFS_IFORK_MAXEXT(ip, whichfork) &&
5047 del->br_startoff > got.br_startoff && del_endoff < got_endoff)
5048 return -ENOSPC;
5049
5050 flags = XFS_ILOG_CORE;
5051 if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) {
5052 xfs_filblks_t len;
5053 xfs_extlen_t mod;
5054
5055 len = div_u64_rem(del->br_blockcount, mp->m_sb.sb_rextsize,
5056 &mod);
5057 ASSERT(mod == 0);
5058
5059 if (!(bflags & XFS_BMAPI_REMAP)) {
5060 xfs_fsblock_t bno;
5061
5062 bno = div_u64_rem(del->br_startblock,
5063 mp->m_sb.sb_rextsize, &mod);
5064 ASSERT(mod == 0);
5065
5066 error = xfs_rtfree_extent(tp, bno, (xfs_extlen_t)len);
5067 if (error)
5068 goto done;
5069 }
5070
5071 do_fx = 0;
5072 nblks = len * mp->m_sb.sb_rextsize;
5073 qfield = XFS_TRANS_DQ_RTBCOUNT;
5074 } else {
5075 do_fx = 1;
5076 nblks = del->br_blockcount;
5077 qfield = XFS_TRANS_DQ_BCOUNT;
5078 }
5079
5080 del_endblock = del->br_startblock + del->br_blockcount;
5081 if (cur) {
5082 error = xfs_bmbt_lookup_eq(cur, &got, &i);
5083 if (error)
5084 goto done;
5085 if (XFS_IS_CORRUPT(mp, i != 1)) {
5086 error = -EFSCORRUPTED;
5087 goto done;
5088 }
5089 }
5090
5091 if (got.br_startoff == del->br_startoff)
5092 state |= BMAP_LEFT_FILLING;
5093 if (got_endoff == del_endoff)
5094 state |= BMAP_RIGHT_FILLING;
5095
5096 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
5097 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
5098
5099
5100
5101 xfs_iext_remove(ip, icur, state);
5102 xfs_iext_prev(ifp, icur);
5103 ifp->if_nextents--;
5104
5105 flags |= XFS_ILOG_CORE;
5106 if (!cur) {
5107 flags |= xfs_ilog_fext(whichfork);
5108 break;
5109 }
5110 if ((error = xfs_btree_delete(cur, &i)))
5111 goto done;
5112 if (XFS_IS_CORRUPT(mp, i != 1)) {
5113 error = -EFSCORRUPTED;
5114 goto done;
5115 }
5116 break;
5117 case BMAP_LEFT_FILLING:
5118
5119
5120
5121 got.br_startoff = del_endoff;
5122 got.br_startblock = del_endblock;
5123 got.br_blockcount -= del->br_blockcount;
5124 xfs_iext_update_extent(ip, state, icur, &got);
5125 if (!cur) {
5126 flags |= xfs_ilog_fext(whichfork);
5127 break;
5128 }
5129 error = xfs_bmbt_update(cur, &got);
5130 if (error)
5131 goto done;
5132 break;
5133 case BMAP_RIGHT_FILLING:
5134
5135
5136
5137 got.br_blockcount -= del->br_blockcount;
5138 xfs_iext_update_extent(ip, state, icur, &got);
5139 if (!cur) {
5140 flags |= xfs_ilog_fext(whichfork);
5141 break;
5142 }
5143 error = xfs_bmbt_update(cur, &got);
5144 if (error)
5145 goto done;
5146 break;
5147 case 0:
5148
5149
5150
5151
5152 old = got;
5153
5154 got.br_blockcount = del->br_startoff - got.br_startoff;
5155 xfs_iext_update_extent(ip, state, icur, &got);
5156
5157 new.br_startoff = del_endoff;
5158 new.br_blockcount = got_endoff - del_endoff;
5159 new.br_state = got.br_state;
5160 new.br_startblock = del_endblock;
5161
5162 flags |= XFS_ILOG_CORE;
5163 if (cur) {
5164 error = xfs_bmbt_update(cur, &got);
5165 if (error)
5166 goto done;
5167 error = xfs_btree_increment(cur, 0, &i);
5168 if (error)
5169 goto done;
5170 cur->bc_rec.b = new;
5171 error = xfs_btree_insert(cur, &i);
5172 if (error && error != -ENOSPC)
5173 goto done;
5174
5175
5176
5177
5178
5179 if (error == -ENOSPC) {
5180
5181
5182
5183
5184 error = xfs_bmbt_lookup_eq(cur, &got, &i);
5185 if (error)
5186 goto done;
5187 if (XFS_IS_CORRUPT(mp, i != 1)) {
5188 error = -EFSCORRUPTED;
5189 goto done;
5190 }
5191
5192
5193
5194
5195 error = xfs_bmbt_update(cur, &old);
5196 if (error)
5197 goto done;
5198
5199
5200
5201
5202 xfs_iext_update_extent(ip, state, icur, &old);
5203 flags = 0;
5204 error = -ENOSPC;
5205 goto done;
5206 }
5207 if (XFS_IS_CORRUPT(mp, i != 1)) {
5208 error = -EFSCORRUPTED;
5209 goto done;
5210 }
5211 } else
5212 flags |= xfs_ilog_fext(whichfork);
5213
5214 ifp->if_nextents++;
5215 xfs_iext_next(ifp, icur);
5216 xfs_iext_insert(ip, icur, &new, state);
5217 break;
5218 }
5219
5220
5221 xfs_rmap_unmap_extent(tp, ip, whichfork, del);
5222
5223
5224
5225
5226 if (do_fx && !(bflags & XFS_BMAPI_REMAP)) {
5227 if (xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK) {
5228 xfs_refcount_decrease_extent(tp, del);
5229 } else {
5230 __xfs_free_extent_later(tp, del->br_startblock,
5231 del->br_blockcount, NULL,
5232 (bflags & XFS_BMAPI_NODISCARD) ||
5233 del->br_state == XFS_EXT_UNWRITTEN);
5234 }
5235 }
5236
5237
5238
5239
5240 if (nblks)
5241 ip->i_nblocks -= nblks;
5242
5243
5244
5245 if (qfield && !(bflags & XFS_BMAPI_REMAP))
5246 xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks);
5247
5248 done:
5249 *logflagsp = flags;
5250 return error;
5251 }
5252
5253
5254
5255
5256
5257
5258
5259 int
5260 __xfs_bunmapi(
5261 struct xfs_trans *tp,
5262 struct xfs_inode *ip,
5263 xfs_fileoff_t start,
5264 xfs_filblks_t *rlen,
5265 uint32_t flags,
5266 xfs_extnum_t nexts)
5267 {
5268 struct xfs_btree_cur *cur;
5269 struct xfs_bmbt_irec del;
5270 int error;
5271 xfs_extnum_t extno;
5272 struct xfs_bmbt_irec got;
5273 struct xfs_ifork *ifp;
5274 int isrt;
5275 int logflags;
5276 xfs_extlen_t mod;
5277 struct xfs_mount *mp = ip->i_mount;
5278 int tmp_logflags;
5279 int wasdel;
5280 int whichfork;
5281 xfs_fsblock_t sum;
5282 xfs_filblks_t len = *rlen;
5283 xfs_fileoff_t end;
5284 struct xfs_iext_cursor icur;
5285 bool done = false;
5286
5287 trace_xfs_bunmap(ip, start, len, flags, _RET_IP_);
5288
5289 whichfork = xfs_bmapi_whichfork(flags);
5290 ASSERT(whichfork != XFS_COW_FORK);
5291 ifp = xfs_ifork_ptr(ip, whichfork);
5292 if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)))
5293 return -EFSCORRUPTED;
5294 if (xfs_is_shutdown(mp))
5295 return -EIO;
5296
5297 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
5298 ASSERT(len > 0);
5299 ASSERT(nexts >= 0);
5300
5301 error = xfs_iread_extents(tp, ip, whichfork);
5302 if (error)
5303 return error;
5304
5305 if (xfs_iext_count(ifp) == 0) {
5306 *rlen = 0;
5307 return 0;
5308 }
5309 XFS_STATS_INC(mp, xs_blk_unmap);
5310 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
5311 end = start + len;
5312
5313 if (!xfs_iext_lookup_extent_before(ip, ifp, &end, &icur, &got)) {
5314 *rlen = 0;
5315 return 0;
5316 }
5317 end--;
5318
5319 logflags = 0;
5320 if (ifp->if_format == XFS_DINODE_FMT_BTREE) {
5321 ASSERT(ifp->if_format == XFS_DINODE_FMT_BTREE);
5322 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5323 cur->bc_ino.flags = 0;
5324 } else
5325 cur = NULL;
5326
5327 if (isrt) {
5328
5329
5330
5331 xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL|XFS_ILOCK_RTBITMAP);
5332 xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL);
5333 xfs_ilock(mp->m_rsumip, XFS_ILOCK_EXCL|XFS_ILOCK_RTSUM);
5334 xfs_trans_ijoin(tp, mp->m_rsumip, XFS_ILOCK_EXCL);
5335 }
5336
5337 extno = 0;
5338 while (end != (xfs_fileoff_t)-1 && end >= start &&
5339 (nexts == 0 || extno < nexts)) {
5340
5341
5342
5343
5344 if (got.br_startoff > end &&
5345 !xfs_iext_prev_extent(ifp, &icur, &got)) {
5346 done = true;
5347 break;
5348 }
5349
5350
5351
5352
5353 end = XFS_FILEOFF_MIN(end,
5354 got.br_startoff + got.br_blockcount - 1);
5355 if (end < start)
5356 break;
5357
5358
5359
5360
5361 del = got;
5362 wasdel = isnullstartblock(del.br_startblock);
5363
5364 if (got.br_startoff < start) {
5365 del.br_startoff = start;
5366 del.br_blockcount -= start - got.br_startoff;
5367 if (!wasdel)
5368 del.br_startblock += start - got.br_startoff;
5369 }
5370 if (del.br_startoff + del.br_blockcount > end + 1)
5371 del.br_blockcount = end + 1 - del.br_startoff;
5372
5373 if (!isrt)
5374 goto delete;
5375
5376 sum = del.br_startblock + del.br_blockcount;
5377 div_u64_rem(sum, mp->m_sb.sb_rextsize, &mod);
5378 if (mod) {
5379
5380
5381
5382
5383
5384
5385
5386 if (del.br_state == XFS_EXT_UNWRITTEN) {
5387
5388
5389
5390
5391 ASSERT(end >= mod);
5392 end -= mod > del.br_blockcount ?
5393 del.br_blockcount : mod;
5394 if (end < got.br_startoff &&
5395 !xfs_iext_prev_extent(ifp, &icur, &got)) {
5396 done = true;
5397 break;
5398 }
5399 continue;
5400 }
5401
5402
5403
5404
5405 ASSERT(del.br_state == XFS_EXT_NORM);
5406 ASSERT(tp->t_blk_res > 0);
5407
5408
5409
5410
5411 if (del.br_blockcount > mod) {
5412 del.br_startoff += del.br_blockcount - mod;
5413 del.br_startblock += del.br_blockcount - mod;
5414 del.br_blockcount = mod;
5415 }
5416 del.br_state = XFS_EXT_UNWRITTEN;
5417 error = xfs_bmap_add_extent_unwritten_real(tp, ip,
5418 whichfork, &icur, &cur, &del,
5419 &logflags);
5420 if (error)
5421 goto error0;
5422 goto nodelete;
5423 }
5424 div_u64_rem(del.br_startblock, mp->m_sb.sb_rextsize, &mod);
5425 if (mod) {
5426 xfs_extlen_t off = mp->m_sb.sb_rextsize - mod;
5427
5428
5429
5430
5431
5432
5433 if (del.br_blockcount > off) {
5434 del.br_blockcount -= off;
5435 del.br_startoff += off;
5436 del.br_startblock += off;
5437 } else if (del.br_startoff == start &&
5438 (del.br_state == XFS_EXT_UNWRITTEN ||
5439 tp->t_blk_res == 0)) {
5440
5441
5442
5443
5444 ASSERT(end >= del.br_blockcount);
5445 end -= del.br_blockcount;
5446 if (got.br_startoff > end &&
5447 !xfs_iext_prev_extent(ifp, &icur, &got)) {
5448 done = true;
5449 break;
5450 }
5451 continue;
5452 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
5453 struct xfs_bmbt_irec prev;
5454 xfs_fileoff_t unwrite_start;
5455
5456
5457
5458
5459
5460
5461
5462 if (!xfs_iext_prev_extent(ifp, &icur, &prev))
5463 ASSERT(0);
5464 ASSERT(prev.br_state == XFS_EXT_NORM);
5465 ASSERT(!isnullstartblock(prev.br_startblock));
5466 ASSERT(del.br_startblock ==
5467 prev.br_startblock + prev.br_blockcount);
5468 unwrite_start = max3(start,
5469 del.br_startoff - mod,
5470 prev.br_startoff);
5471 mod = unwrite_start - prev.br_startoff;
5472 prev.br_startoff = unwrite_start;
5473 prev.br_startblock += mod;
5474 prev.br_blockcount -= mod;
5475 prev.br_state = XFS_EXT_UNWRITTEN;
5476 error = xfs_bmap_add_extent_unwritten_real(tp,
5477 ip, whichfork, &icur, &cur,
5478 &prev, &logflags);
5479 if (error)
5480 goto error0;
5481 goto nodelete;
5482 } else {
5483 ASSERT(del.br_state == XFS_EXT_NORM);
5484 del.br_state = XFS_EXT_UNWRITTEN;
5485 error = xfs_bmap_add_extent_unwritten_real(tp,
5486 ip, whichfork, &icur, &cur,
5487 &del, &logflags);
5488 if (error)
5489 goto error0;
5490 goto nodelete;
5491 }
5492 }
5493
5494 delete:
5495 if (wasdel) {
5496 error = xfs_bmap_del_extent_delay(ip, whichfork, &icur,
5497 &got, &del);
5498 } else {
5499 error = xfs_bmap_del_extent_real(ip, tp, &icur, cur,
5500 &del, &tmp_logflags, whichfork,
5501 flags);
5502 logflags |= tmp_logflags;
5503 }
5504
5505 if (error)
5506 goto error0;
5507
5508 end = del.br_startoff - 1;
5509 nodelete:
5510
5511
5512
5513 if (end != (xfs_fileoff_t)-1 && end >= start) {
5514 if (!xfs_iext_get_extent(ifp, &icur, &got) ||
5515 (got.br_startoff > end &&
5516 !xfs_iext_prev_extent(ifp, &icur, &got))) {
5517 done = true;
5518 break;
5519 }
5520 extno++;
5521 }
5522 }
5523 if (done || end == (xfs_fileoff_t)-1 || end < start)
5524 *rlen = 0;
5525 else
5526 *rlen = end - start + 1;
5527
5528
5529
5530
5531 if (xfs_bmap_needs_btree(ip, whichfork)) {
5532 ASSERT(cur == NULL);
5533 error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0,
5534 &tmp_logflags, whichfork);
5535 logflags |= tmp_logflags;
5536 } else {
5537 error = xfs_bmap_btree_to_extents(tp, ip, cur, &logflags,
5538 whichfork);
5539 }
5540
5541 error0:
5542
5543
5544
5545
5546 if ((logflags & xfs_ilog_fext(whichfork)) &&
5547 ifp->if_format != XFS_DINODE_FMT_EXTENTS)
5548 logflags &= ~xfs_ilog_fext(whichfork);
5549 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
5550 ifp->if_format != XFS_DINODE_FMT_BTREE)
5551 logflags &= ~xfs_ilog_fbroot(whichfork);
5552
5553
5554
5555
5556 if (logflags)
5557 xfs_trans_log_inode(tp, ip, logflags);
5558 if (cur) {
5559 if (!error)
5560 cur->bc_ino.allocated = 0;
5561 xfs_btree_del_cursor(cur, error);
5562 }
5563 return error;
5564 }
5565
5566
5567 int
5568 xfs_bunmapi(
5569 xfs_trans_t *tp,
5570 struct xfs_inode *ip,
5571 xfs_fileoff_t bno,
5572 xfs_filblks_t len,
5573 uint32_t flags,
5574 xfs_extnum_t nexts,
5575 int *done)
5576 {
5577 int error;
5578
5579 error = __xfs_bunmapi(tp, ip, bno, &len, flags, nexts);
5580 *done = (len == 0);
5581 return error;
5582 }
5583
5584
5585
5586
5587
5588 STATIC bool
5589 xfs_bmse_can_merge(
5590 struct xfs_bmbt_irec *left,
5591 struct xfs_bmbt_irec *got,
5592 xfs_fileoff_t shift)
5593 {
5594 xfs_fileoff_t startoff;
5595
5596 startoff = got->br_startoff - shift;
5597
5598
5599
5600
5601
5602 if ((left->br_startoff + left->br_blockcount != startoff) ||
5603 (left->br_startblock + left->br_blockcount != got->br_startblock) ||
5604 (left->br_state != got->br_state) ||
5605 (left->br_blockcount + got->br_blockcount > XFS_MAX_BMBT_EXTLEN))
5606 return false;
5607
5608 return true;
5609 }
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620 STATIC int
5621 xfs_bmse_merge(
5622 struct xfs_trans *tp,
5623 struct xfs_inode *ip,
5624 int whichfork,
5625 xfs_fileoff_t shift,
5626 struct xfs_iext_cursor *icur,
5627 struct xfs_bmbt_irec *got,
5628 struct xfs_bmbt_irec *left,
5629 struct xfs_btree_cur *cur,
5630 int *logflags)
5631 {
5632 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
5633 struct xfs_bmbt_irec new;
5634 xfs_filblks_t blockcount;
5635 int error, i;
5636 struct xfs_mount *mp = ip->i_mount;
5637
5638 blockcount = left->br_blockcount + got->br_blockcount;
5639
5640 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
5641 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
5642 ASSERT(xfs_bmse_can_merge(left, got, shift));
5643
5644 new = *left;
5645 new.br_blockcount = blockcount;
5646
5647
5648
5649
5650
5651 ifp->if_nextents--;
5652 *logflags |= XFS_ILOG_CORE;
5653 if (!cur) {
5654 *logflags |= XFS_ILOG_DEXT;
5655 goto done;
5656 }
5657
5658
5659 error = xfs_bmbt_lookup_eq(cur, got, &i);
5660 if (error)
5661 return error;
5662 if (XFS_IS_CORRUPT(mp, i != 1))
5663 return -EFSCORRUPTED;
5664
5665 error = xfs_btree_delete(cur, &i);
5666 if (error)
5667 return error;
5668 if (XFS_IS_CORRUPT(mp, i != 1))
5669 return -EFSCORRUPTED;
5670
5671
5672 error = xfs_bmbt_lookup_eq(cur, left, &i);
5673 if (error)
5674 return error;
5675 if (XFS_IS_CORRUPT(mp, i != 1))
5676 return -EFSCORRUPTED;
5677
5678 error = xfs_bmbt_update(cur, &new);
5679 if (error)
5680 return error;
5681
5682
5683 error = xfs_bmap_btree_to_extents(tp, ip, cur, logflags, whichfork);
5684 if (error)
5685 return error;
5686
5687 done:
5688 xfs_iext_remove(ip, icur, 0);
5689 xfs_iext_prev(ifp, icur);
5690 xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), icur,
5691 &new);
5692
5693
5694 xfs_rmap_unmap_extent(tp, ip, whichfork, got);
5695 memcpy(&new, got, sizeof(new));
5696 new.br_startoff = left->br_startoff + left->br_blockcount;
5697 xfs_rmap_map_extent(tp, ip, whichfork, &new);
5698 return 0;
5699 }
5700
5701 static int
5702 xfs_bmap_shift_update_extent(
5703 struct xfs_trans *tp,
5704 struct xfs_inode *ip,
5705 int whichfork,
5706 struct xfs_iext_cursor *icur,
5707 struct xfs_bmbt_irec *got,
5708 struct xfs_btree_cur *cur,
5709 int *logflags,
5710 xfs_fileoff_t startoff)
5711 {
5712 struct xfs_mount *mp = ip->i_mount;
5713 struct xfs_bmbt_irec prev = *got;
5714 int error, i;
5715
5716 *logflags |= XFS_ILOG_CORE;
5717
5718 got->br_startoff = startoff;
5719
5720 if (cur) {
5721 error = xfs_bmbt_lookup_eq(cur, &prev, &i);
5722 if (error)
5723 return error;
5724 if (XFS_IS_CORRUPT(mp, i != 1))
5725 return -EFSCORRUPTED;
5726
5727 error = xfs_bmbt_update(cur, got);
5728 if (error)
5729 return error;
5730 } else {
5731 *logflags |= XFS_ILOG_DEXT;
5732 }
5733
5734 xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), icur,
5735 got);
5736
5737
5738 xfs_rmap_unmap_extent(tp, ip, whichfork, &prev);
5739 xfs_rmap_map_extent(tp, ip, whichfork, got);
5740 return 0;
5741 }
5742
5743 int
5744 xfs_bmap_collapse_extents(
5745 struct xfs_trans *tp,
5746 struct xfs_inode *ip,
5747 xfs_fileoff_t *next_fsb,
5748 xfs_fileoff_t offset_shift_fsb,
5749 bool *done)
5750 {
5751 int whichfork = XFS_DATA_FORK;
5752 struct xfs_mount *mp = ip->i_mount;
5753 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
5754 struct xfs_btree_cur *cur = NULL;
5755 struct xfs_bmbt_irec got, prev;
5756 struct xfs_iext_cursor icur;
5757 xfs_fileoff_t new_startoff;
5758 int error = 0;
5759 int logflags = 0;
5760
5761 if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
5762 XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
5763 return -EFSCORRUPTED;
5764 }
5765
5766 if (xfs_is_shutdown(mp))
5767 return -EIO;
5768
5769 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL));
5770
5771 error = xfs_iread_extents(tp, ip, whichfork);
5772 if (error)
5773 return error;
5774
5775 if (ifp->if_format == XFS_DINODE_FMT_BTREE) {
5776 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5777 cur->bc_ino.flags = 0;
5778 }
5779
5780 if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &icur, &got)) {
5781 *done = true;
5782 goto del_cursor;
5783 }
5784 if (XFS_IS_CORRUPT(mp, isnullstartblock(got.br_startblock))) {
5785 error = -EFSCORRUPTED;
5786 goto del_cursor;
5787 }
5788
5789 new_startoff = got.br_startoff - offset_shift_fsb;
5790 if (xfs_iext_peek_prev_extent(ifp, &icur, &prev)) {
5791 if (new_startoff < prev.br_startoff + prev.br_blockcount) {
5792 error = -EINVAL;
5793 goto del_cursor;
5794 }
5795
5796 if (xfs_bmse_can_merge(&prev, &got, offset_shift_fsb)) {
5797 error = xfs_bmse_merge(tp, ip, whichfork,
5798 offset_shift_fsb, &icur, &got, &prev,
5799 cur, &logflags);
5800 if (error)
5801 goto del_cursor;
5802 goto done;
5803 }
5804 } else {
5805 if (got.br_startoff < offset_shift_fsb) {
5806 error = -EINVAL;
5807 goto del_cursor;
5808 }
5809 }
5810
5811 error = xfs_bmap_shift_update_extent(tp, ip, whichfork, &icur, &got,
5812 cur, &logflags, new_startoff);
5813 if (error)
5814 goto del_cursor;
5815
5816 done:
5817 if (!xfs_iext_next_extent(ifp, &icur, &got)) {
5818 *done = true;
5819 goto del_cursor;
5820 }
5821
5822 *next_fsb = got.br_startoff;
5823 del_cursor:
5824 if (cur)
5825 xfs_btree_del_cursor(cur, error);
5826 if (logflags)
5827 xfs_trans_log_inode(tp, ip, logflags);
5828 return error;
5829 }
5830
5831
5832 int
5833 xfs_bmap_can_insert_extents(
5834 struct xfs_inode *ip,
5835 xfs_fileoff_t off,
5836 xfs_fileoff_t shift)
5837 {
5838 struct xfs_bmbt_irec got;
5839 int is_empty;
5840 int error = 0;
5841
5842 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
5843
5844 if (xfs_is_shutdown(ip->i_mount))
5845 return -EIO;
5846
5847 xfs_ilock(ip, XFS_ILOCK_EXCL);
5848 error = xfs_bmap_last_extent(NULL, ip, XFS_DATA_FORK, &got, &is_empty);
5849 if (!error && !is_empty && got.br_startoff >= off &&
5850 ((got.br_startoff + shift) & BMBT_STARTOFF_MASK) < got.br_startoff)
5851 error = -EINVAL;
5852 xfs_iunlock(ip, XFS_ILOCK_EXCL);
5853
5854 return error;
5855 }
5856
5857 int
5858 xfs_bmap_insert_extents(
5859 struct xfs_trans *tp,
5860 struct xfs_inode *ip,
5861 xfs_fileoff_t *next_fsb,
5862 xfs_fileoff_t offset_shift_fsb,
5863 bool *done,
5864 xfs_fileoff_t stop_fsb)
5865 {
5866 int whichfork = XFS_DATA_FORK;
5867 struct xfs_mount *mp = ip->i_mount;
5868 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
5869 struct xfs_btree_cur *cur = NULL;
5870 struct xfs_bmbt_irec got, next;
5871 struct xfs_iext_cursor icur;
5872 xfs_fileoff_t new_startoff;
5873 int error = 0;
5874 int logflags = 0;
5875
5876 if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
5877 XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
5878 return -EFSCORRUPTED;
5879 }
5880
5881 if (xfs_is_shutdown(mp))
5882 return -EIO;
5883
5884 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL));
5885
5886 error = xfs_iread_extents(tp, ip, whichfork);
5887 if (error)
5888 return error;
5889
5890 if (ifp->if_format == XFS_DINODE_FMT_BTREE) {
5891 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5892 cur->bc_ino.flags = 0;
5893 }
5894
5895 if (*next_fsb == NULLFSBLOCK) {
5896 xfs_iext_last(ifp, &icur);
5897 if (!xfs_iext_get_extent(ifp, &icur, &got) ||
5898 stop_fsb > got.br_startoff) {
5899 *done = true;
5900 goto del_cursor;
5901 }
5902 } else {
5903 if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &icur, &got)) {
5904 *done = true;
5905 goto del_cursor;
5906 }
5907 }
5908 if (XFS_IS_CORRUPT(mp, isnullstartblock(got.br_startblock))) {
5909 error = -EFSCORRUPTED;
5910 goto del_cursor;
5911 }
5912
5913 if (XFS_IS_CORRUPT(mp, stop_fsb > got.br_startoff)) {
5914 error = -EFSCORRUPTED;
5915 goto del_cursor;
5916 }
5917
5918 new_startoff = got.br_startoff + offset_shift_fsb;
5919 if (xfs_iext_peek_next_extent(ifp, &icur, &next)) {
5920 if (new_startoff + got.br_blockcount > next.br_startoff) {
5921 error = -EINVAL;
5922 goto del_cursor;
5923 }
5924
5925
5926
5927
5928
5929
5930
5931 if (xfs_bmse_can_merge(&got, &next, offset_shift_fsb))
5932 WARN_ON_ONCE(1);
5933 }
5934
5935 error = xfs_bmap_shift_update_extent(tp, ip, whichfork, &icur, &got,
5936 cur, &logflags, new_startoff);
5937 if (error)
5938 goto del_cursor;
5939
5940 if (!xfs_iext_prev_extent(ifp, &icur, &got) ||
5941 stop_fsb >= got.br_startoff + got.br_blockcount) {
5942 *done = true;
5943 goto del_cursor;
5944 }
5945
5946 *next_fsb = got.br_startoff;
5947 del_cursor:
5948 if (cur)
5949 xfs_btree_del_cursor(cur, error);
5950 if (logflags)
5951 xfs_trans_log_inode(tp, ip, logflags);
5952 return error;
5953 }
5954
5955
5956
5957
5958
5959
5960
5961 int
5962 xfs_bmap_split_extent(
5963 struct xfs_trans *tp,
5964 struct xfs_inode *ip,
5965 xfs_fileoff_t split_fsb)
5966 {
5967 int whichfork = XFS_DATA_FORK;
5968 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
5969 struct xfs_btree_cur *cur = NULL;
5970 struct xfs_bmbt_irec got;
5971 struct xfs_bmbt_irec new;
5972 struct xfs_mount *mp = ip->i_mount;
5973 xfs_fsblock_t gotblkcnt;
5974 struct xfs_iext_cursor icur;
5975 int error = 0;
5976 int logflags = 0;
5977 int i = 0;
5978
5979 if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
5980 XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
5981 return -EFSCORRUPTED;
5982 }
5983
5984 if (xfs_is_shutdown(mp))
5985 return -EIO;
5986
5987
5988 error = xfs_iread_extents(tp, ip, whichfork);
5989 if (error)
5990 return error;
5991
5992
5993
5994
5995 if (!xfs_iext_lookup_extent(ip, ifp, split_fsb, &icur, &got) ||
5996 got.br_startoff >= split_fsb)
5997 return 0;
5998
5999 gotblkcnt = split_fsb - got.br_startoff;
6000 new.br_startoff = split_fsb;
6001 new.br_startblock = got.br_startblock + gotblkcnt;
6002 new.br_blockcount = got.br_blockcount - gotblkcnt;
6003 new.br_state = got.br_state;
6004
6005 if (ifp->if_format == XFS_DINODE_FMT_BTREE) {
6006 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
6007 cur->bc_ino.flags = 0;
6008 error = xfs_bmbt_lookup_eq(cur, &got, &i);
6009 if (error)
6010 goto del_cursor;
6011 if (XFS_IS_CORRUPT(mp, i != 1)) {
6012 error = -EFSCORRUPTED;
6013 goto del_cursor;
6014 }
6015 }
6016
6017 got.br_blockcount = gotblkcnt;
6018 xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), &icur,
6019 &got);
6020
6021 logflags = XFS_ILOG_CORE;
6022 if (cur) {
6023 error = xfs_bmbt_update(cur, &got);
6024 if (error)
6025 goto del_cursor;
6026 } else
6027 logflags |= XFS_ILOG_DEXT;
6028
6029
6030 xfs_iext_next(ifp, &icur);
6031 xfs_iext_insert(ip, &icur, &new, 0);
6032 ifp->if_nextents++;
6033
6034 if (cur) {
6035 error = xfs_bmbt_lookup_eq(cur, &new, &i);
6036 if (error)
6037 goto del_cursor;
6038 if (XFS_IS_CORRUPT(mp, i != 0)) {
6039 error = -EFSCORRUPTED;
6040 goto del_cursor;
6041 }
6042 error = xfs_btree_insert(cur, &i);
6043 if (error)
6044 goto del_cursor;
6045 if (XFS_IS_CORRUPT(mp, i != 1)) {
6046 error = -EFSCORRUPTED;
6047 goto del_cursor;
6048 }
6049 }
6050
6051
6052
6053
6054 if (xfs_bmap_needs_btree(ip, whichfork)) {
6055 int tmp_logflags;
6056
6057 ASSERT(cur == NULL);
6058 error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0,
6059 &tmp_logflags, whichfork);
6060 logflags |= tmp_logflags;
6061 }
6062
6063 del_cursor:
6064 if (cur) {
6065 cur->bc_ino.allocated = 0;
6066 xfs_btree_del_cursor(cur, error);
6067 }
6068
6069 if (logflags)
6070 xfs_trans_log_inode(tp, ip, logflags);
6071 return error;
6072 }
6073
6074
6075 static bool
6076 xfs_bmap_is_update_needed(
6077 struct xfs_bmbt_irec *bmap)
6078 {
6079 return bmap->br_startblock != HOLESTARTBLOCK &&
6080 bmap->br_startblock != DELAYSTARTBLOCK;
6081 }
6082
6083
6084 static int
6085 __xfs_bmap_add(
6086 struct xfs_trans *tp,
6087 enum xfs_bmap_intent_type type,
6088 struct xfs_inode *ip,
6089 int whichfork,
6090 struct xfs_bmbt_irec *bmap)
6091 {
6092 struct xfs_bmap_intent *bi;
6093
6094 trace_xfs_bmap_defer(tp->t_mountp,
6095 XFS_FSB_TO_AGNO(tp->t_mountp, bmap->br_startblock),
6096 type,
6097 XFS_FSB_TO_AGBNO(tp->t_mountp, bmap->br_startblock),
6098 ip->i_ino, whichfork,
6099 bmap->br_startoff,
6100 bmap->br_blockcount,
6101 bmap->br_state);
6102
6103 bi = kmem_cache_alloc(xfs_bmap_intent_cache, GFP_NOFS | __GFP_NOFAIL);
6104 INIT_LIST_HEAD(&bi->bi_list);
6105 bi->bi_type = type;
6106 bi->bi_owner = ip;
6107 bi->bi_whichfork = whichfork;
6108 bi->bi_bmap = *bmap;
6109
6110 xfs_defer_add(tp, XFS_DEFER_OPS_TYPE_BMAP, &bi->bi_list);
6111 return 0;
6112 }
6113
6114
6115 void
6116 xfs_bmap_map_extent(
6117 struct xfs_trans *tp,
6118 struct xfs_inode *ip,
6119 struct xfs_bmbt_irec *PREV)
6120 {
6121 if (!xfs_bmap_is_update_needed(PREV))
6122 return;
6123
6124 __xfs_bmap_add(tp, XFS_BMAP_MAP, ip, XFS_DATA_FORK, PREV);
6125 }
6126
6127
6128 void
6129 xfs_bmap_unmap_extent(
6130 struct xfs_trans *tp,
6131 struct xfs_inode *ip,
6132 struct xfs_bmbt_irec *PREV)
6133 {
6134 if (!xfs_bmap_is_update_needed(PREV))
6135 return;
6136
6137 __xfs_bmap_add(tp, XFS_BMAP_UNMAP, ip, XFS_DATA_FORK, PREV);
6138 }
6139
6140
6141
6142
6143
6144 int
6145 xfs_bmap_finish_one(
6146 struct xfs_trans *tp,
6147 struct xfs_inode *ip,
6148 enum xfs_bmap_intent_type type,
6149 int whichfork,
6150 xfs_fileoff_t startoff,
6151 xfs_fsblock_t startblock,
6152 xfs_filblks_t *blockcount,
6153 xfs_exntst_t state)
6154 {
6155 int error = 0;
6156
6157 ASSERT(tp->t_firstblock == NULLFSBLOCK);
6158
6159 trace_xfs_bmap_deferred(tp->t_mountp,
6160 XFS_FSB_TO_AGNO(tp->t_mountp, startblock), type,
6161 XFS_FSB_TO_AGBNO(tp->t_mountp, startblock),
6162 ip->i_ino, whichfork, startoff, *blockcount, state);
6163
6164 if (WARN_ON_ONCE(whichfork != XFS_DATA_FORK))
6165 return -EFSCORRUPTED;
6166
6167 if (XFS_TEST_ERROR(false, tp->t_mountp,
6168 XFS_ERRTAG_BMAP_FINISH_ONE))
6169 return -EIO;
6170
6171 switch (type) {
6172 case XFS_BMAP_MAP:
6173 error = xfs_bmapi_remap(tp, ip, startoff, *blockcount,
6174 startblock, 0);
6175 *blockcount = 0;
6176 break;
6177 case XFS_BMAP_UNMAP:
6178 error = __xfs_bunmapi(tp, ip, startoff, blockcount,
6179 XFS_BMAPI_REMAP, 1);
6180 break;
6181 default:
6182 ASSERT(0);
6183 error = -EFSCORRUPTED;
6184 }
6185
6186 return error;
6187 }
6188
6189
6190 xfs_failaddr_t
6191 xfs_bmap_validate_extent(
6192 struct xfs_inode *ip,
6193 int whichfork,
6194 struct xfs_bmbt_irec *irec)
6195 {
6196 struct xfs_mount *mp = ip->i_mount;
6197
6198 if (!xfs_verify_fileext(mp, irec->br_startoff, irec->br_blockcount))
6199 return __this_address;
6200
6201 if (XFS_IS_REALTIME_INODE(ip) && whichfork == XFS_DATA_FORK) {
6202 if (!xfs_verify_rtext(mp, irec->br_startblock,
6203 irec->br_blockcount))
6204 return __this_address;
6205 } else {
6206 if (!xfs_verify_fsbext(mp, irec->br_startblock,
6207 irec->br_blockcount))
6208 return __this_address;
6209 }
6210 if (irec->br_state != XFS_EXT_NORM && whichfork != XFS_DATA_FORK)
6211 return __this_address;
6212 return NULL;
6213 }
6214
6215 int __init
6216 xfs_bmap_intent_init_cache(void)
6217 {
6218 xfs_bmap_intent_cache = kmem_cache_create("xfs_bmap_intent",
6219 sizeof(struct xfs_bmap_intent),
6220 0, 0, NULL);
6221
6222 return xfs_bmap_intent_cache != NULL ? 0 : -ENOMEM;
6223 }
6224
6225 void
6226 xfs_bmap_intent_destroy_cache(void)
6227 {
6228 kmem_cache_destroy(xfs_bmap_intent_cache);
6229 xfs_bmap_intent_cache = NULL;
6230 }