1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
8 #include "xfs_shared.h"
9 #include "xfs_format.h"
10 #include "xfs_log_format.h"
11 #include "xfs_trans_resv.h"
14 #include "xfs_mount.h"
15 #include "xfs_defer.h"
16 #include "xfs_da_format.h"
17 #include "xfs_da_btree.h"
19 #include "xfs_inode.h"
20 #include "xfs_btree.h"
21 #include "xfs_trans.h"
22 #include "xfs_inode_item.h"
23 #include "xfs_extfree_item.h"
24 #include "xfs_alloc.h"
26 #include "xfs_bmap_util.h"
27 #include "xfs_bmap_btree.h"
28 #include "xfs_rtalloc.h"
29 #include "xfs_errortag.h"
30 #include "xfs_error.h"
31 #include "xfs_quota.h"
32 #include "xfs_trans_space.h"
33 #include "xfs_buf_item.h"
34 #include "xfs_trace.h"
35 #include "xfs_symlink.h"
36 #include "xfs_attr_leaf.h"
37 #include "xfs_filestream.h"
39 #include "xfs_ag_resv.h"
40 #include "xfs_refcount.h"
41 #include "xfs_icache.h"
44 kmem_zone_t *xfs_bmap_free_item_zone;
47 * Miscellaneous helper functions
51 * Compute and fill in the value of the maximum depth of a bmap btree
52 * in this filesystem. Done once, during mount.
55 xfs_bmap_compute_maxlevels(
56 xfs_mount_t *mp, /* file system mount structure */
57 int whichfork) /* data or attr fork */
59 int level; /* btree level */
60 uint maxblocks; /* max blocks at this level */
61 uint maxleafents; /* max leaf entries possible */
62 int maxrootrecs; /* max records in root block */
63 int minleafrecs; /* min records in leaf block */
64 int minnoderecs; /* min records in node block */
65 int sz; /* root block size */
68 * The maximum number of extents in a file, hence the maximum
69 * number of leaf entries, is controlled by the type of di_nextents
70 * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
71 * (a signed 16-bit number, xfs_aextnum_t).
73 * Note that we can no longer assume that if we are in ATTR1 that
74 * the fork offset of all the inodes will be
75 * (xfs_default_attroffset(ip) >> 3) because we could have mounted
76 * with ATTR2 and then mounted back with ATTR1, keeping the
77 * di_forkoff's fixed but probably at various positions. Therefore,
78 * for both ATTR1 and ATTR2 we have to assume the worst case scenario
79 * of a minimum size available.
81 if (whichfork == XFS_DATA_FORK) {
82 maxleafents = MAXEXTNUM;
83 sz = XFS_BMDR_SPACE_CALC(MINDBTPTRS);
85 maxleafents = MAXAEXTNUM;
86 sz = XFS_BMDR_SPACE_CALC(MINABTPTRS);
88 maxrootrecs = xfs_bmdr_maxrecs(sz, 0);
89 minleafrecs = mp->m_bmap_dmnr[0];
90 minnoderecs = mp->m_bmap_dmnr[1];
91 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
92 for (level = 1; maxblocks > 1; level++) {
93 if (maxblocks <= maxrootrecs)
96 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
98 mp->m_bm_maxlevels[whichfork] = level;
101 STATIC int /* error */
103 struct xfs_btree_cur *cur,
104 struct xfs_bmbt_irec *irec,
105 int *stat) /* success/failure */
107 cur->bc_rec.b = *irec;
108 return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
111 STATIC int /* error */
112 xfs_bmbt_lookup_first(
113 struct xfs_btree_cur *cur,
114 int *stat) /* success/failure */
116 cur->bc_rec.b.br_startoff = 0;
117 cur->bc_rec.b.br_startblock = 0;
118 cur->bc_rec.b.br_blockcount = 0;
119 return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
123 * Check if the inode needs to be converted to btree format.
125 static inline bool xfs_bmap_needs_btree(struct xfs_inode *ip, int whichfork)
127 return whichfork != XFS_COW_FORK &&
128 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
129 XFS_IFORK_NEXTENTS(ip, whichfork) >
130 XFS_IFORK_MAXEXT(ip, whichfork);
134 * Check if the inode should be converted to extent format.
136 static inline bool xfs_bmap_wants_extents(struct xfs_inode *ip, int whichfork)
138 return whichfork != XFS_COW_FORK &&
139 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
140 XFS_IFORK_NEXTENTS(ip, whichfork) <=
141 XFS_IFORK_MAXEXT(ip, whichfork);
145 * Update the record referred to by cur to the value given by irec
146 * This either works (return 0) or gets an EFSCORRUPTED error.
150 struct xfs_btree_cur *cur,
151 struct xfs_bmbt_irec *irec)
153 union xfs_btree_rec rec;
155 xfs_bmbt_disk_set_all(&rec.bmbt, irec);
156 return xfs_btree_update(cur, &rec);
160 * Compute the worst-case number of indirect blocks that will be used
161 * for ip's delayed extent of length "len".
164 xfs_bmap_worst_indlen(
165 xfs_inode_t *ip, /* incore inode pointer */
166 xfs_filblks_t len) /* delayed extent length */
168 int level; /* btree level number */
169 int maxrecs; /* maximum record count at this level */
170 xfs_mount_t *mp; /* mount structure */
171 xfs_filblks_t rval; /* return value */
174 maxrecs = mp->m_bmap_dmxr[0];
175 for (level = 0, rval = 0;
176 level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK);
179 do_div(len, maxrecs);
182 return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) -
185 maxrecs = mp->m_bmap_dmxr[1];
191 * Calculate the default attribute fork offset for newly created inodes.
194 xfs_default_attroffset(
195 struct xfs_inode *ip)
197 struct xfs_mount *mp = ip->i_mount;
200 if (mp->m_sb.sb_inodesize == 256) {
201 offset = XFS_LITINO(mp, ip->i_d.di_version) -
202 XFS_BMDR_SPACE_CALC(MINABTPTRS);
204 offset = XFS_BMDR_SPACE_CALC(6 * MINABTPTRS);
207 ASSERT(offset < XFS_LITINO(mp, ip->i_d.di_version));
212 * Helper routine to reset inode di_forkoff field when switching
213 * attribute fork from local to extent format - we reset it where
214 * possible to make space available for inline data fork extents.
217 xfs_bmap_forkoff_reset(
221 if (whichfork == XFS_ATTR_FORK &&
222 ip->i_d.di_format != XFS_DINODE_FMT_DEV &&
223 ip->i_d.di_format != XFS_DINODE_FMT_BTREE) {
224 uint dfl_forkoff = xfs_default_attroffset(ip) >> 3;
226 if (dfl_forkoff > ip->i_d.di_forkoff)
227 ip->i_d.di_forkoff = dfl_forkoff;
232 STATIC struct xfs_buf *
234 struct xfs_btree_cur *cur,
237 struct xfs_log_item *lip;
243 for (i = 0; i < XFS_BTREE_MAXLEVELS; i++) {
244 if (!cur->bc_bufs[i])
246 if (XFS_BUF_ADDR(cur->bc_bufs[i]) == bno)
247 return cur->bc_bufs[i];
250 /* Chase down all the log items to see if the bp is there */
251 list_for_each_entry(lip, &cur->bc_tp->t_items, li_trans) {
252 struct xfs_buf_log_item *bip = (struct xfs_buf_log_item *)lip;
254 if (bip->bli_item.li_type == XFS_LI_BUF &&
255 XFS_BUF_ADDR(bip->bli_buf) == bno)
264 struct xfs_btree_block *block,
270 __be64 *pp, *thispa; /* pointer to block address */
271 xfs_bmbt_key_t *prevp, *keyp;
273 ASSERT(be16_to_cpu(block->bb_level) > 0);
276 for( i = 1; i <= xfs_btree_get_numrecs(block); i++) {
277 dmxr = mp->m_bmap_dmxr[0];
278 keyp = XFS_BMBT_KEY_ADDR(mp, block, i);
281 ASSERT(be64_to_cpu(prevp->br_startoff) <
282 be64_to_cpu(keyp->br_startoff));
287 * Compare the block numbers to see if there are dups.
290 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, i, sz);
292 pp = XFS_BMBT_PTR_ADDR(mp, block, i, dmxr);
294 for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) {
296 thispa = XFS_BMAP_BROOT_PTR_ADDR(mp, block, j, sz);
298 thispa = XFS_BMBT_PTR_ADDR(mp, block, j, dmxr);
299 if (*thispa == *pp) {
300 xfs_warn(mp, "%s: thispa(%d) == pp(%d) %Ld",
302 (unsigned long long)be64_to_cpu(*thispa));
303 xfs_err(mp, "%s: ptrs are equal in node\n",
305 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
312 * Check that the extents for the inode ip are in the right order in all
313 * btree leaves. THis becomes prohibitively expensive for large extent count
314 * files, so don't bother with inodes that have more than 10,000 extents in
315 * them. The btree record ordering checks will still be done, so for such large
316 * bmapbt constructs that is going to catch most corruptions.
319 xfs_bmap_check_leaf_extents(
320 xfs_btree_cur_t *cur, /* btree cursor or null */
321 xfs_inode_t *ip, /* incore inode pointer */
322 int whichfork) /* data or attr fork */
324 struct xfs_btree_block *block; /* current btree block */
325 xfs_fsblock_t bno; /* block # of "block" */
326 xfs_buf_t *bp; /* buffer for "block" */
327 int error; /* error return value */
328 xfs_extnum_t i=0, j; /* index into the extents list */
329 struct xfs_ifork *ifp; /* fork structure */
330 int level; /* btree level, for checking */
331 xfs_mount_t *mp; /* file system mount structure */
332 __be64 *pp; /* pointer to block address */
333 xfs_bmbt_rec_t *ep; /* pointer to current extent */
334 xfs_bmbt_rec_t last = {0, 0}; /* last extent in prev block */
335 xfs_bmbt_rec_t *nextp; /* pointer to next extent */
338 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) {
342 /* skip large extent count inodes */
343 if (ip->i_d.di_nextents > 10000)
348 ifp = XFS_IFORK_PTR(ip, whichfork);
349 block = ifp->if_broot;
351 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
353 level = be16_to_cpu(block->bb_level);
355 xfs_check_block(block, mp, 1, ifp->if_broot_bytes);
356 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
357 bno = be64_to_cpu(*pp);
359 ASSERT(bno != NULLFSBLOCK);
360 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
361 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
364 * Go down the tree until leaf level is reached, following the first
365 * pointer (leftmost) at each level.
367 while (level-- > 0) {
368 /* See if buf is in cur first */
370 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
373 error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
379 block = XFS_BUF_TO_BLOCK(bp);
384 * Check this block for basic sanity (increasing keys and
385 * no duplicate blocks).
388 xfs_check_block(block, mp, 0, 0);
389 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
390 bno = be64_to_cpu(*pp);
391 XFS_WANT_CORRUPTED_GOTO(mp,
392 xfs_verify_fsbno(mp, bno), error0);
395 xfs_trans_brelse(NULL, bp);
400 * Here with bp and block set to the leftmost leaf node in the tree.
405 * Loop over all leaf nodes checking that all extents are in the right order.
408 xfs_fsblock_t nextbno;
409 xfs_extnum_t num_recs;
412 num_recs = xfs_btree_get_numrecs(block);
415 * Read-ahead the next leaf block, if any.
418 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
421 * Check all the extents to make sure they are OK.
422 * If we had a previous block, the last entry should
423 * conform with the first entry in this one.
426 ep = XFS_BMBT_REC_ADDR(mp, block, 1);
428 ASSERT(xfs_bmbt_disk_get_startoff(&last) +
429 xfs_bmbt_disk_get_blockcount(&last) <=
430 xfs_bmbt_disk_get_startoff(ep));
432 for (j = 1; j < num_recs; j++) {
433 nextp = XFS_BMBT_REC_ADDR(mp, block, j + 1);
434 ASSERT(xfs_bmbt_disk_get_startoff(ep) +
435 xfs_bmbt_disk_get_blockcount(ep) <=
436 xfs_bmbt_disk_get_startoff(nextp));
444 xfs_trans_brelse(NULL, bp);
448 * If we've reached the end, stop.
450 if (bno == NULLFSBLOCK)
454 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
457 error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
463 block = XFS_BUF_TO_BLOCK(bp);
469 xfs_warn(mp, "%s: at error0", __func__);
471 xfs_trans_brelse(NULL, bp);
473 xfs_warn(mp, "%s: BAD after btree leaves for %d extents",
475 xfs_err(mp, "%s: CORRUPTED BTREE OR SOMETHING", __func__);
476 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
481 * Validate that the bmbt_irecs being returned from bmapi are valid
482 * given the caller's original parameters. Specifically check the
483 * ranges of the returned irecs to ensure that they only extend beyond
484 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
487 xfs_bmap_validate_ret(
491 xfs_bmbt_irec_t *mval,
495 int i; /* index to map values */
497 ASSERT(ret_nmap <= nmap);
499 for (i = 0; i < ret_nmap; i++) {
500 ASSERT(mval[i].br_blockcount > 0);
501 if (!(flags & XFS_BMAPI_ENTIRE)) {
502 ASSERT(mval[i].br_startoff >= bno);
503 ASSERT(mval[i].br_blockcount <= len);
504 ASSERT(mval[i].br_startoff + mval[i].br_blockcount <=
507 ASSERT(mval[i].br_startoff < bno + len);
508 ASSERT(mval[i].br_startoff + mval[i].br_blockcount >
512 mval[i - 1].br_startoff + mval[i - 1].br_blockcount ==
513 mval[i].br_startoff);
514 ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK &&
515 mval[i].br_startblock != HOLESTARTBLOCK);
516 ASSERT(mval[i].br_state == XFS_EXT_NORM ||
517 mval[i].br_state == XFS_EXT_UNWRITTEN);
522 #define xfs_bmap_check_leaf_extents(cur, ip, whichfork) do { } while (0)
523 #define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap) do { } while (0)
527 * bmap free list manipulation functions
531 * Add the extent to the list of extents to be free at transaction end.
532 * The list is maintained sorted (by block number).
536 struct xfs_trans *tp,
539 const struct xfs_owner_info *oinfo,
542 struct xfs_extent_free_item *new; /* new element */
544 struct xfs_mount *mp = tp->t_mountp;
548 ASSERT(bno != NULLFSBLOCK);
550 ASSERT(len <= MAXEXTLEN);
551 ASSERT(!isnullstartblock(bno));
552 agno = XFS_FSB_TO_AGNO(mp, bno);
553 agbno = XFS_FSB_TO_AGBNO(mp, bno);
554 ASSERT(agno < mp->m_sb.sb_agcount);
555 ASSERT(agbno < mp->m_sb.sb_agblocks);
556 ASSERT(len < mp->m_sb.sb_agblocks);
557 ASSERT(agbno + len <= mp->m_sb.sb_agblocks);
559 ASSERT(xfs_bmap_free_item_zone != NULL);
561 new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP);
562 new->xefi_startblock = bno;
563 new->xefi_blockcount = (xfs_extlen_t)len;
565 new->xefi_oinfo = *oinfo;
567 new->xefi_oinfo = XFS_RMAP_OINFO_SKIP_UPDATE;
568 new->xefi_skip_discard = skip_discard;
569 trace_xfs_bmap_free_defer(tp->t_mountp,
570 XFS_FSB_TO_AGNO(tp->t_mountp, bno), 0,
571 XFS_FSB_TO_AGBNO(tp->t_mountp, bno), len);
572 xfs_defer_add(tp, XFS_DEFER_OPS_TYPE_FREE, &new->xefi_list);
576 * Inode fork format manipulation functions
580 * Convert the inode format to extent format if it currently is in btree format,
581 * but the extent list is small enough that it fits into the extent format.
583 * Since the extents are already in-core, all we have to do is give up the space
584 * for the btree root and pitch the leaf block.
586 STATIC int /* error */
587 xfs_bmap_btree_to_extents(
588 struct xfs_trans *tp, /* transaction pointer */
589 struct xfs_inode *ip, /* incore inode pointer */
590 struct xfs_btree_cur *cur, /* btree cursor */
591 int *logflagsp, /* inode logging flags */
592 int whichfork) /* data or attr fork */
594 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
595 struct xfs_mount *mp = ip->i_mount;
596 struct xfs_btree_block *rblock = ifp->if_broot;
597 struct xfs_btree_block *cblock;/* child btree block */
598 xfs_fsblock_t cbno; /* child block number */
599 xfs_buf_t *cbp; /* child block's buffer */
600 int error; /* error return value */
601 __be64 *pp; /* ptr to block address */
602 struct xfs_owner_info oinfo;
604 /* check if we actually need the extent format first: */
605 if (!xfs_bmap_wants_extents(ip, whichfork))
609 ASSERT(whichfork != XFS_COW_FORK);
610 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
611 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
612 ASSERT(be16_to_cpu(rblock->bb_level) == 1);
613 ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1);
614 ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0) == 1);
616 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, ifp->if_broot_bytes);
617 cbno = be64_to_cpu(*pp);
619 XFS_WANT_CORRUPTED_RETURN(cur->bc_mp,
620 xfs_btree_check_lptr(cur, cbno, 1));
622 error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp, XFS_BMAP_BTREE_REF,
626 cblock = XFS_BUF_TO_BLOCK(cbp);
627 if ((error = xfs_btree_check_block(cur, cblock, 0, cbp)))
629 xfs_rmap_ino_bmbt_owner(&oinfo, ip->i_ino, whichfork);
630 xfs_bmap_add_free(cur->bc_tp, cbno, 1, &oinfo);
631 ip->i_d.di_nblocks--;
632 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
633 xfs_trans_binval(tp, cbp);
634 if (cur->bc_bufs[0] == cbp)
635 cur->bc_bufs[0] = NULL;
636 xfs_iroot_realloc(ip, -1, whichfork);
637 ASSERT(ifp->if_broot == NULL);
638 ASSERT((ifp->if_flags & XFS_IFBROOT) == 0);
639 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
640 *logflagsp |= XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
645 * Convert an extents-format file into a btree-format file.
646 * The new file will have a root block (in the inode) and a single child block.
648 STATIC int /* error */
649 xfs_bmap_extents_to_btree(
650 struct xfs_trans *tp, /* transaction pointer */
651 struct xfs_inode *ip, /* incore inode pointer */
652 struct xfs_btree_cur **curp, /* cursor returned to caller */
653 int wasdel, /* converting a delayed alloc */
654 int *logflagsp, /* inode logging flags */
655 int whichfork) /* data or attr fork */
657 struct xfs_btree_block *ablock; /* allocated (child) bt block */
658 struct xfs_buf *abp; /* buffer for ablock */
659 struct xfs_alloc_arg args; /* allocation arguments */
660 struct xfs_bmbt_rec *arp; /* child record pointer */
661 struct xfs_btree_block *block; /* btree root block */
662 struct xfs_btree_cur *cur; /* bmap btree cursor */
663 int error; /* error return value */
664 struct xfs_ifork *ifp; /* inode fork pointer */
665 struct xfs_bmbt_key *kp; /* root block key pointer */
666 struct xfs_mount *mp; /* mount structure */
667 xfs_bmbt_ptr_t *pp; /* root block address pointer */
668 struct xfs_iext_cursor icur;
669 struct xfs_bmbt_irec rec;
670 xfs_extnum_t cnt = 0;
673 ASSERT(whichfork != XFS_COW_FORK);
674 ifp = XFS_IFORK_PTR(ip, whichfork);
675 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS);
678 * Make space in the inode incore. This needs to be undone if we fail
679 * to expand the root.
681 xfs_iroot_realloc(ip, 1, whichfork);
682 ifp->if_flags |= XFS_IFBROOT;
687 block = ifp->if_broot;
688 xfs_btree_init_block_int(mp, block, XFS_BUF_DADDR_NULL,
689 XFS_BTNUM_BMAP, 1, 1, ip->i_ino,
690 XFS_BTREE_LONG_PTRS);
692 * Need a cursor. Can't allocate until bb_level is filled in.
694 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
695 cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
697 * Convert to a btree with two levels, one record in root.
699 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE);
700 memset(&args, 0, sizeof(args));
703 xfs_rmap_ino_bmbt_owner(&args.oinfo, ip->i_ino, whichfork);
704 if (tp->t_firstblock == NULLFSBLOCK) {
705 args.type = XFS_ALLOCTYPE_START_BNO;
706 args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino);
707 } else if (tp->t_flags & XFS_TRANS_LOWMODE) {
708 args.type = XFS_ALLOCTYPE_START_BNO;
709 args.fsbno = tp->t_firstblock;
711 args.type = XFS_ALLOCTYPE_NEAR_BNO;
712 args.fsbno = tp->t_firstblock;
714 args.minlen = args.maxlen = args.prod = 1;
715 args.wasdel = wasdel;
717 error = xfs_alloc_vextent(&args);
719 goto out_root_realloc;
721 if (WARN_ON_ONCE(args.fsbno == NULLFSBLOCK)) {
723 goto out_root_realloc;
727 * Allocation can't fail, the space was reserved.
729 ASSERT(tp->t_firstblock == NULLFSBLOCK ||
730 args.agno >= XFS_FSB_TO_AGNO(mp, tp->t_firstblock));
731 tp->t_firstblock = args.fsbno;
732 cur->bc_private.b.allocated++;
733 ip->i_d.di_nblocks++;
734 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
735 abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0);
737 error = -EFSCORRUPTED;
738 goto out_unreserve_dquot;
742 * Fill in the child block.
744 abp->b_ops = &xfs_bmbt_buf_ops;
745 ablock = XFS_BUF_TO_BLOCK(abp);
746 xfs_btree_init_block_int(mp, ablock, abp->b_bn,
747 XFS_BTNUM_BMAP, 0, 0, ip->i_ino,
748 XFS_BTREE_LONG_PTRS);
750 for_each_xfs_iext(ifp, &icur, &rec) {
751 if (isnullstartblock(rec.br_startblock))
753 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1 + cnt);
754 xfs_bmbt_disk_set_all(arp, &rec);
757 ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork));
758 xfs_btree_set_numrecs(ablock, cnt);
761 * Fill in the root key and pointer.
763 kp = XFS_BMBT_KEY_ADDR(mp, block, 1);
764 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
765 kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp));
766 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur,
767 be16_to_cpu(block->bb_level)));
768 *pp = cpu_to_be64(args.fsbno);
771 * Do all this logging at the end so that
772 * the root is at the right level.
774 xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS);
775 xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs));
776 ASSERT(*curp == NULL);
778 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork);
782 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
784 xfs_iroot_realloc(ip, -1, whichfork);
785 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
786 ASSERT(ifp->if_broot == NULL);
787 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
793 * Convert a local file to an extents file.
794 * This code is out of bounds for data forks of regular files,
795 * since the file data needs to get logged so things will stay consistent.
796 * (The bmap-level manipulations are ok, though).
799 xfs_bmap_local_to_extents_empty(
800 struct xfs_inode *ip,
803 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
805 ASSERT(whichfork != XFS_COW_FORK);
806 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
807 ASSERT(ifp->if_bytes == 0);
808 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0);
810 xfs_bmap_forkoff_reset(ip, whichfork);
811 ifp->if_flags &= ~XFS_IFINLINE;
812 ifp->if_flags |= XFS_IFEXTENTS;
813 ifp->if_u1.if_root = NULL;
815 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
819 STATIC int /* error */
820 xfs_bmap_local_to_extents(
821 xfs_trans_t *tp, /* transaction pointer */
822 xfs_inode_t *ip, /* incore inode pointer */
823 xfs_extlen_t total, /* total blocks needed by transaction */
824 int *logflagsp, /* inode logging flags */
826 void (*init_fn)(struct xfs_trans *tp,
828 struct xfs_inode *ip,
829 struct xfs_ifork *ifp))
832 int flags; /* logging flags returned */
833 struct xfs_ifork *ifp; /* inode fork pointer */
834 xfs_alloc_arg_t args; /* allocation arguments */
835 xfs_buf_t *bp; /* buffer for extent block */
836 struct xfs_bmbt_irec rec;
837 struct xfs_iext_cursor icur;
840 * We don't want to deal with the case of keeping inode data inline yet.
841 * So sending the data fork of a regular inode is invalid.
843 ASSERT(!(S_ISREG(VFS_I(ip)->i_mode) && whichfork == XFS_DATA_FORK));
844 ifp = XFS_IFORK_PTR(ip, whichfork);
845 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
847 if (!ifp->if_bytes) {
848 xfs_bmap_local_to_extents_empty(ip, whichfork);
849 flags = XFS_ILOG_CORE;
855 ASSERT((ifp->if_flags & (XFS_IFINLINE|XFS_IFEXTENTS)) == XFS_IFINLINE);
856 memset(&args, 0, sizeof(args));
858 args.mp = ip->i_mount;
859 xfs_rmap_ino_owner(&args.oinfo, ip->i_ino, whichfork, 0);
861 * Allocate a block. We know we need only one, since the
862 * file currently fits in an inode.
864 if (tp->t_firstblock == NULLFSBLOCK) {
865 args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino);
866 args.type = XFS_ALLOCTYPE_START_BNO;
868 args.fsbno = tp->t_firstblock;
869 args.type = XFS_ALLOCTYPE_NEAR_BNO;
872 args.minlen = args.maxlen = args.prod = 1;
873 error = xfs_alloc_vextent(&args);
877 /* Can't fail, the space was reserved. */
878 ASSERT(args.fsbno != NULLFSBLOCK);
879 ASSERT(args.len == 1);
880 tp->t_firstblock = args.fsbno;
881 bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0);
884 * Initialize the block, copy the data and log the remote buffer.
886 * The callout is responsible for logging because the remote format
887 * might differ from the local format and thus we don't know how much to
888 * log here. Note that init_fn must also set the buffer log item type
891 init_fn(tp, bp, ip, ifp);
893 /* account for the change in fork size */
894 xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
895 xfs_bmap_local_to_extents_empty(ip, whichfork);
896 flags |= XFS_ILOG_CORE;
898 ifp->if_u1.if_root = NULL;
902 rec.br_startblock = args.fsbno;
903 rec.br_blockcount = 1;
904 rec.br_state = XFS_EXT_NORM;
905 xfs_iext_first(ifp, &icur);
906 xfs_iext_insert(ip, &icur, &rec, 0);
908 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
909 ip->i_d.di_nblocks = 1;
910 xfs_trans_mod_dquot_byino(tp, ip,
911 XFS_TRANS_DQ_BCOUNT, 1L);
912 flags |= xfs_ilog_fext(whichfork);
920 * Called from xfs_bmap_add_attrfork to handle btree format files.
922 STATIC int /* error */
923 xfs_bmap_add_attrfork_btree(
924 xfs_trans_t *tp, /* transaction pointer */
925 xfs_inode_t *ip, /* incore inode pointer */
926 int *flags) /* inode logging flags */
928 xfs_btree_cur_t *cur; /* btree cursor */
929 int error; /* error return value */
930 xfs_mount_t *mp; /* file system mount struct */
931 int stat; /* newroot status */
934 if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip))
935 *flags |= XFS_ILOG_DBROOT;
937 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
938 error = xfs_bmbt_lookup_first(cur, &stat);
941 /* must be at least one entry */
942 XFS_WANT_CORRUPTED_GOTO(mp, stat == 1, error0);
943 if ((error = xfs_btree_new_iroot(cur, flags, &stat)))
946 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
949 cur->bc_private.b.allocated = 0;
950 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
954 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
959 * Called from xfs_bmap_add_attrfork to handle extents format files.
961 STATIC int /* error */
962 xfs_bmap_add_attrfork_extents(
963 struct xfs_trans *tp, /* transaction pointer */
964 struct xfs_inode *ip, /* incore inode pointer */
965 int *flags) /* inode logging flags */
967 xfs_btree_cur_t *cur; /* bmap btree cursor */
968 int error; /* error return value */
970 if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip))
973 error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0, flags,
976 cur->bc_private.b.allocated = 0;
977 xfs_btree_del_cursor(cur, error);
983 * Called from xfs_bmap_add_attrfork to handle local format files. Each
984 * different data fork content type needs a different callout to do the
985 * conversion. Some are basic and only require special block initialisation
986 * callouts for the data formating, others (directories) are so specialised they
987 * handle everything themselves.
989 * XXX (dgc): investigate whether directory conversion can use the generic
990 * formatting callout. It should be possible - it's just a very complex
993 STATIC int /* error */
994 xfs_bmap_add_attrfork_local(
995 struct xfs_trans *tp, /* transaction pointer */
996 struct xfs_inode *ip, /* incore inode pointer */
997 int *flags) /* inode logging flags */
999 struct xfs_da_args dargs; /* args for dir/attr code */
1001 if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip))
1004 if (S_ISDIR(VFS_I(ip)->i_mode)) {
1005 memset(&dargs, 0, sizeof(dargs));
1006 dargs.geo = ip->i_mount->m_dir_geo;
1008 dargs.total = dargs.geo->fsbcount;
1009 dargs.whichfork = XFS_DATA_FORK;
1011 return xfs_dir2_sf_to_block(&dargs);
1014 if (S_ISLNK(VFS_I(ip)->i_mode))
1015 return xfs_bmap_local_to_extents(tp, ip, 1, flags,
1017 xfs_symlink_local_to_remote);
1019 /* should only be called for types that support local format data */
1021 return -EFSCORRUPTED;
1024 /* Set an inode attr fork off based on the format */
1026 xfs_bmap_set_attrforkoff(
1027 struct xfs_inode *ip,
1031 switch (ip->i_d.di_format) {
1032 case XFS_DINODE_FMT_DEV:
1033 ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
1035 case XFS_DINODE_FMT_LOCAL:
1036 case XFS_DINODE_FMT_EXTENTS:
1037 case XFS_DINODE_FMT_BTREE:
1038 ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size);
1039 if (!ip->i_d.di_forkoff)
1040 ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3;
1041 else if ((ip->i_mount->m_flags & XFS_MOUNT_ATTR2) && version)
1053 * Convert inode from non-attributed to attributed.
1054 * Must not be in a transaction, ip must not be locked.
1056 int /* error code */
1057 xfs_bmap_add_attrfork(
1058 xfs_inode_t *ip, /* incore inode pointer */
1059 int size, /* space new attribute needs */
1060 int rsvd) /* xact may use reserved blks */
1062 xfs_mount_t *mp; /* mount structure */
1063 xfs_trans_t *tp; /* transaction pointer */
1064 int blks; /* space reservation */
1065 int version = 1; /* superblock attr version */
1066 int logflags; /* logging flags */
1067 int error; /* error return value */
1069 ASSERT(XFS_IFORK_Q(ip) == 0);
1072 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
1074 blks = XFS_ADDAFORK_SPACE_RES(mp);
1076 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_addafork, blks, 0,
1077 rsvd ? XFS_TRANS_RESERVE : 0, &tp);
1081 xfs_ilock(ip, XFS_ILOCK_EXCL);
1082 error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ?
1083 XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
1084 XFS_QMOPT_RES_REGBLKS);
1087 if (XFS_IFORK_Q(ip))
1089 if (ip->i_d.di_anextents != 0) {
1090 error = -EFSCORRUPTED;
1093 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) {
1095 * For inodes coming from pre-6.2 filesystems.
1097 ASSERT(ip->i_d.di_aformat == 0);
1098 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
1101 xfs_trans_ijoin(tp, ip, 0);
1102 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1103 error = xfs_bmap_set_attrforkoff(ip, size, &version);
1106 ASSERT(ip->i_afp == NULL);
1107 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP);
1108 ip->i_afp->if_flags = XFS_IFEXTENTS;
1110 switch (ip->i_d.di_format) {
1111 case XFS_DINODE_FMT_LOCAL:
1112 error = xfs_bmap_add_attrfork_local(tp, ip, &logflags);
1114 case XFS_DINODE_FMT_EXTENTS:
1115 error = xfs_bmap_add_attrfork_extents(tp, ip, &logflags);
1117 case XFS_DINODE_FMT_BTREE:
1118 error = xfs_bmap_add_attrfork_btree(tp, ip, &logflags);
1125 xfs_trans_log_inode(tp, ip, logflags);
1128 if (!xfs_sb_version_hasattr(&mp->m_sb) ||
1129 (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) {
1130 bool log_sb = false;
1132 spin_lock(&mp->m_sb_lock);
1133 if (!xfs_sb_version_hasattr(&mp->m_sb)) {
1134 xfs_sb_version_addattr(&mp->m_sb);
1137 if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) {
1138 xfs_sb_version_addattr2(&mp->m_sb);
1141 spin_unlock(&mp->m_sb_lock);
1146 error = xfs_trans_commit(tp);
1147 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1151 xfs_trans_cancel(tp);
1152 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1157 * Internal and external extent tree search functions.
1161 * Read in extents from a btree-format inode.
1165 struct xfs_trans *tp,
1166 struct xfs_inode *ip,
1169 struct xfs_mount *mp = ip->i_mount;
1170 int state = xfs_bmap_fork_to_state(whichfork);
1171 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1172 xfs_extnum_t nextents = XFS_IFORK_NEXTENTS(ip, whichfork);
1173 struct xfs_btree_block *block = ifp->if_broot;
1174 struct xfs_iext_cursor icur;
1175 struct xfs_bmbt_irec new;
1183 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
1185 if (unlikely(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
1186 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp);
1187 return -EFSCORRUPTED;
1191 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
1193 level = be16_to_cpu(block->bb_level);
1194 if (unlikely(level == 0)) {
1195 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp);
1196 return -EFSCORRUPTED;
1198 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
1199 bno = be64_to_cpu(*pp);
1202 * Go down the tree until leaf level is reached, following the first
1203 * pointer (leftmost) at each level.
1205 while (level-- > 0) {
1206 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
1207 XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
1210 block = XFS_BUF_TO_BLOCK(bp);
1213 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
1214 bno = be64_to_cpu(*pp);
1215 XFS_WANT_CORRUPTED_GOTO(mp,
1216 xfs_verify_fsbno(mp, bno), out_brelse);
1217 xfs_trans_brelse(tp, bp);
1221 * Here with bp and block set to the leftmost leaf node in the tree.
1224 xfs_iext_first(ifp, &icur);
1227 * Loop over all leaf nodes. Copy information to the extent records.
1230 xfs_bmbt_rec_t *frp;
1231 xfs_fsblock_t nextbno;
1232 xfs_extnum_t num_recs;
1234 num_recs = xfs_btree_get_numrecs(block);
1235 if (unlikely(i + num_recs > nextents)) {
1236 xfs_warn(ip->i_mount,
1237 "corrupt dinode %Lu, (btree extents).",
1238 (unsigned long long) ip->i_ino);
1239 xfs_inode_verifier_error(ip, -EFSCORRUPTED,
1240 __func__, block, sizeof(*block),
1242 error = -EFSCORRUPTED;
1246 * Read-ahead the next leaf block, if any.
1248 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
1249 if (nextbno != NULLFSBLOCK)
1250 xfs_btree_reada_bufl(mp, nextbno, 1,
1253 * Copy records into the extent records.
1255 frp = XFS_BMBT_REC_ADDR(mp, block, 1);
1256 for (j = 0; j < num_recs; j++, frp++, i++) {
1259 xfs_bmbt_disk_get_all(frp, &new);
1260 fa = xfs_bmap_validate_extent(ip, whichfork, &new);
1262 error = -EFSCORRUPTED;
1263 xfs_inode_verifier_error(ip, error,
1264 "xfs_iread_extents(2)",
1265 frp, sizeof(*frp), fa);
1268 xfs_iext_insert(ip, &icur, &new, state);
1269 trace_xfs_read_extent(ip, &icur, state, _THIS_IP_);
1270 xfs_iext_next(ifp, &icur);
1272 xfs_trans_brelse(tp, bp);
1275 * If we've reached the end, stop.
1277 if (bno == NULLFSBLOCK)
1279 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
1280 XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
1283 block = XFS_BUF_TO_BLOCK(bp);
1286 if (i != XFS_IFORK_NEXTENTS(ip, whichfork)) {
1287 error = -EFSCORRUPTED;
1290 ASSERT(i == xfs_iext_count(ifp));
1292 ifp->if_flags |= XFS_IFEXTENTS;
1296 xfs_trans_brelse(tp, bp);
1298 xfs_iext_destroy(ifp);
1303 * Returns the relative block number of the first unused block(s) in the given
1304 * fork with at least "len" logically contiguous blocks free. This is the
1305 * lowest-address hole if the fork has holes, else the first block past the end
1306 * of fork. Return 0 if the fork is currently local (in-inode).
1309 xfs_bmap_first_unused(
1310 struct xfs_trans *tp, /* transaction pointer */
1311 struct xfs_inode *ip, /* incore inode */
1312 xfs_extlen_t len, /* size of hole to find */
1313 xfs_fileoff_t *first_unused, /* unused block */
1314 int whichfork) /* data or attr fork */
1316 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1317 struct xfs_bmbt_irec got;
1318 struct xfs_iext_cursor icur;
1319 xfs_fileoff_t lastaddr = 0;
1320 xfs_fileoff_t lowest, max;
1323 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE ||
1324 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ||
1325 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
1327 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
1332 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1333 error = xfs_iread_extents(tp, ip, whichfork);
1338 lowest = max = *first_unused;
1339 for_each_xfs_iext(ifp, &icur, &got) {
1341 * See if the hole before this extent will work.
1343 if (got.br_startoff >= lowest + len &&
1344 got.br_startoff - max >= len)
1346 lastaddr = got.br_startoff + got.br_blockcount;
1347 max = XFS_FILEOFF_MAX(lastaddr, lowest);
1350 *first_unused = max;
1355 * Returns the file-relative block number of the last block - 1 before
1356 * last_block (input value) in the file.
1357 * This is not based on i_size, it is based on the extent records.
1358 * Returns 0 for local files, as they do not have extent records.
1361 xfs_bmap_last_before(
1362 struct xfs_trans *tp, /* transaction pointer */
1363 struct xfs_inode *ip, /* incore inode */
1364 xfs_fileoff_t *last_block, /* last block */
1365 int whichfork) /* data or attr fork */
1367 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1368 struct xfs_bmbt_irec got;
1369 struct xfs_iext_cursor icur;
1372 switch (XFS_IFORK_FORMAT(ip, whichfork)) {
1373 case XFS_DINODE_FMT_LOCAL:
1376 case XFS_DINODE_FMT_BTREE:
1377 case XFS_DINODE_FMT_EXTENTS:
1383 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1384 error = xfs_iread_extents(tp, ip, whichfork);
1389 if (!xfs_iext_lookup_extent_before(ip, ifp, last_block, &icur, &got))
1395 xfs_bmap_last_extent(
1396 struct xfs_trans *tp,
1397 struct xfs_inode *ip,
1399 struct xfs_bmbt_irec *rec,
1402 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1403 struct xfs_iext_cursor icur;
1406 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1407 error = xfs_iread_extents(tp, ip, whichfork);
1412 xfs_iext_last(ifp, &icur);
1413 if (!xfs_iext_get_extent(ifp, &icur, rec))
1421 * Check the last inode extent to determine whether this allocation will result
1422 * in blocks being allocated at the end of the file. When we allocate new data
1423 * blocks at the end of the file which do not start at the previous data block,
1424 * we will try to align the new blocks at stripe unit boundaries.
1426 * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be
1427 * at, or past the EOF.
1431 struct xfs_bmalloca *bma,
1434 struct xfs_bmbt_irec rec;
1439 error = xfs_bmap_last_extent(NULL, bma->ip, whichfork, &rec,
1450 * Check if we are allocation or past the last extent, or at least into
1451 * the last delayed allocated extent.
1453 bma->aeof = bma->offset >= rec.br_startoff + rec.br_blockcount ||
1454 (bma->offset >= rec.br_startoff &&
1455 isnullstartblock(rec.br_startblock));
1460 * Returns the file-relative block number of the first block past eof in
1461 * the file. This is not based on i_size, it is based on the extent records.
1462 * Returns 0 for local files, as they do not have extent records.
1465 xfs_bmap_last_offset(
1466 struct xfs_inode *ip,
1467 xfs_fileoff_t *last_block,
1470 struct xfs_bmbt_irec rec;
1476 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL)
1479 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
1480 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
1483 error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, &is_empty);
1484 if (error || is_empty)
1487 *last_block = rec.br_startoff + rec.br_blockcount;
1492 * Returns whether the selected fork of the inode has exactly one
1493 * block or not. For the data fork we check this matches di_size,
1494 * implying the file's range is 0..bsize-1.
1496 int /* 1=>1 block, 0=>otherwise */
1498 xfs_inode_t *ip, /* incore inode */
1499 int whichfork) /* data or attr fork */
1501 struct xfs_ifork *ifp; /* inode fork pointer */
1502 int rval; /* return value */
1503 xfs_bmbt_irec_t s; /* internal version of extent */
1504 struct xfs_iext_cursor icur;
1507 if (whichfork == XFS_DATA_FORK)
1508 return XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize;
1510 if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1)
1512 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
1514 ifp = XFS_IFORK_PTR(ip, whichfork);
1515 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
1516 xfs_iext_first(ifp, &icur);
1517 xfs_iext_get_extent(ifp, &icur, &s);
1518 rval = s.br_startoff == 0 && s.br_blockcount == 1;
1519 if (rval && whichfork == XFS_DATA_FORK)
1520 ASSERT(XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize);
1525 * Extent tree manipulation functions used during allocation.
1529 * Convert a delayed allocation to a real allocation.
1531 STATIC int /* error */
1532 xfs_bmap_add_extent_delay_real(
1533 struct xfs_bmalloca *bma,
1536 struct xfs_bmbt_irec *new = &bma->got;
1537 int error; /* error return value */
1538 int i; /* temp state */
1539 struct xfs_ifork *ifp; /* inode fork pointer */
1540 xfs_fileoff_t new_endoff; /* end offset of new entry */
1541 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
1542 /* left is 0, right is 1, prev is 2 */
1543 int rval=0; /* return value (logging flags) */
1544 int state = xfs_bmap_fork_to_state(whichfork);
1545 xfs_filblks_t da_new; /* new count del alloc blocks used */
1546 xfs_filblks_t da_old; /* old count del alloc blocks used */
1547 xfs_filblks_t temp=0; /* value for da_new calculations */
1548 int tmp_rval; /* partial logging flags */
1549 struct xfs_mount *mp;
1550 xfs_extnum_t *nextents;
1551 struct xfs_bmbt_irec old;
1553 mp = bma->ip->i_mount;
1554 ifp = XFS_IFORK_PTR(bma->ip, whichfork);
1555 ASSERT(whichfork != XFS_ATTR_FORK);
1556 nextents = (whichfork == XFS_COW_FORK ? &bma->ip->i_cnextents :
1557 &bma->ip->i_d.di_nextents);
1559 ASSERT(!isnullstartblock(new->br_startblock));
1561 (bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
1563 XFS_STATS_INC(mp, xs_add_exlist);
1570 * Set up a bunch of variables to make the tests simpler.
1572 xfs_iext_get_extent(ifp, &bma->icur, &PREV);
1573 new_endoff = new->br_startoff + new->br_blockcount;
1574 ASSERT(isnullstartblock(PREV.br_startblock));
1575 ASSERT(PREV.br_startoff <= new->br_startoff);
1576 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
1578 da_old = startblockval(PREV.br_startblock);
1582 * Set flags determining what part of the previous delayed allocation
1583 * extent is being replaced by a real allocation.
1585 if (PREV.br_startoff == new->br_startoff)
1586 state |= BMAP_LEFT_FILLING;
1587 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
1588 state |= BMAP_RIGHT_FILLING;
1591 * Check and set flags if this segment has a left neighbor.
1592 * Don't set contiguous if the combined extent would be too large.
1594 if (xfs_iext_peek_prev_extent(ifp, &bma->icur, &LEFT)) {
1595 state |= BMAP_LEFT_VALID;
1596 if (isnullstartblock(LEFT.br_startblock))
1597 state |= BMAP_LEFT_DELAY;
1600 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1601 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1602 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
1603 LEFT.br_state == new->br_state &&
1604 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1605 state |= BMAP_LEFT_CONTIG;
1608 * Check and set flags if this segment has a right neighbor.
1609 * Don't set contiguous if the combined extent would be too large.
1610 * Also check for all-three-contiguous being too large.
1612 if (xfs_iext_peek_next_extent(ifp, &bma->icur, &RIGHT)) {
1613 state |= BMAP_RIGHT_VALID;
1614 if (isnullstartblock(RIGHT.br_startblock))
1615 state |= BMAP_RIGHT_DELAY;
1618 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1619 new_endoff == RIGHT.br_startoff &&
1620 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
1621 new->br_state == RIGHT.br_state &&
1622 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
1623 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1624 BMAP_RIGHT_FILLING)) !=
1625 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1626 BMAP_RIGHT_FILLING) ||
1627 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
1629 state |= BMAP_RIGHT_CONTIG;
1633 * Switch out based on the FILLING and CONTIG state bits.
1635 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1636 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
1637 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1638 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1640 * Filling in all of a previously delayed allocation extent.
1641 * The left and right neighbors are both contiguous with new.
1643 LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount;
1645 xfs_iext_remove(bma->ip, &bma->icur, state);
1646 xfs_iext_remove(bma->ip, &bma->icur, state);
1647 xfs_iext_prev(ifp, &bma->icur);
1648 xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT);
1651 if (bma->cur == NULL)
1652 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1654 rval = XFS_ILOG_CORE;
1655 error = xfs_bmbt_lookup_eq(bma->cur, &RIGHT, &i);
1658 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
1659 error = xfs_btree_delete(bma->cur, &i);
1662 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
1663 error = xfs_btree_decrement(bma->cur, 0, &i);
1666 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
1667 error = xfs_bmbt_update(bma->cur, &LEFT);
1673 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1675 * Filling in all of a previously delayed allocation extent.
1676 * The left neighbor is contiguous, the right is not.
1679 LEFT.br_blockcount += PREV.br_blockcount;
1681 xfs_iext_remove(bma->ip, &bma->icur, state);
1682 xfs_iext_prev(ifp, &bma->icur);
1683 xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT);
1685 if (bma->cur == NULL)
1686 rval = XFS_ILOG_DEXT;
1689 error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
1692 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
1693 error = xfs_bmbt_update(bma->cur, &LEFT);
1699 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1701 * Filling in all of a previously delayed allocation extent.
1702 * The right neighbor is contiguous, the left is not. Take care
1703 * with delay -> unwritten extent allocation here because the
1704 * delalloc record we are overwriting is always written.
1706 PREV.br_startblock = new->br_startblock;
1707 PREV.br_blockcount += RIGHT.br_blockcount;
1708 PREV.br_state = new->br_state;
1710 xfs_iext_next(ifp, &bma->icur);
1711 xfs_iext_remove(bma->ip, &bma->icur, state);
1712 xfs_iext_prev(ifp, &bma->icur);
1713 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
1715 if (bma->cur == NULL)
1716 rval = XFS_ILOG_DEXT;
1719 error = xfs_bmbt_lookup_eq(bma->cur, &RIGHT, &i);
1722 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
1723 error = xfs_bmbt_update(bma->cur, &PREV);
1729 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
1731 * Filling in all of a previously delayed allocation extent.
1732 * Neither the left nor right neighbors are contiguous with
1735 PREV.br_startblock = new->br_startblock;
1736 PREV.br_state = new->br_state;
1737 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
1740 if (bma->cur == NULL)
1741 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1743 rval = XFS_ILOG_CORE;
1744 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
1747 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
1748 error = xfs_btree_insert(bma->cur, &i);
1751 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
1755 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
1757 * Filling in the first part of a previous delayed allocation.
1758 * The left neighbor is contiguous.
1761 temp = PREV.br_blockcount - new->br_blockcount;
1762 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
1763 startblockval(PREV.br_startblock));
1765 LEFT.br_blockcount += new->br_blockcount;
1767 PREV.br_blockcount = temp;
1768 PREV.br_startoff += new->br_blockcount;
1769 PREV.br_startblock = nullstartblock(da_new);
1771 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
1772 xfs_iext_prev(ifp, &bma->icur);
1773 xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT);
1775 if (bma->cur == NULL)
1776 rval = XFS_ILOG_DEXT;
1779 error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
1782 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
1783 error = xfs_bmbt_update(bma->cur, &LEFT);
1789 case BMAP_LEFT_FILLING:
1791 * Filling in the first part of a previous delayed allocation.
1792 * The left neighbor is not contiguous.
1794 xfs_iext_update_extent(bma->ip, state, &bma->icur, new);
1796 if (bma->cur == NULL)
1797 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1799 rval = XFS_ILOG_CORE;
1800 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
1803 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
1804 error = xfs_btree_insert(bma->cur, &i);
1807 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
1810 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
1811 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
1812 &bma->cur, 1, &tmp_rval, whichfork);
1818 temp = PREV.br_blockcount - new->br_blockcount;
1819 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
1820 startblockval(PREV.br_startblock) -
1821 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
1823 PREV.br_startoff = new_endoff;
1824 PREV.br_blockcount = temp;
1825 PREV.br_startblock = nullstartblock(da_new);
1826 xfs_iext_next(ifp, &bma->icur);
1827 xfs_iext_insert(bma->ip, &bma->icur, &PREV, state);
1828 xfs_iext_prev(ifp, &bma->icur);
1831 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1833 * Filling in the last part of a previous delayed allocation.
1834 * The right neighbor is contiguous with the new allocation.
1837 RIGHT.br_startoff = new->br_startoff;
1838 RIGHT.br_startblock = new->br_startblock;
1839 RIGHT.br_blockcount += new->br_blockcount;
1841 if (bma->cur == NULL)
1842 rval = XFS_ILOG_DEXT;
1845 error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
1848 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
1849 error = xfs_bmbt_update(bma->cur, &RIGHT);
1854 temp = PREV.br_blockcount - new->br_blockcount;
1855 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
1856 startblockval(PREV.br_startblock));
1858 PREV.br_blockcount = temp;
1859 PREV.br_startblock = nullstartblock(da_new);
1861 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
1862 xfs_iext_next(ifp, &bma->icur);
1863 xfs_iext_update_extent(bma->ip, state, &bma->icur, &RIGHT);
1866 case BMAP_RIGHT_FILLING:
1868 * Filling in the last part of a previous delayed allocation.
1869 * The right neighbor is not contiguous.
1871 xfs_iext_update_extent(bma->ip, state, &bma->icur, new);
1873 if (bma->cur == NULL)
1874 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1876 rval = XFS_ILOG_CORE;
1877 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
1880 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
1881 error = xfs_btree_insert(bma->cur, &i);
1884 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
1887 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
1888 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
1889 &bma->cur, 1, &tmp_rval, whichfork);
1895 temp = PREV.br_blockcount - new->br_blockcount;
1896 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
1897 startblockval(PREV.br_startblock) -
1898 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
1900 PREV.br_startblock = nullstartblock(da_new);
1901 PREV.br_blockcount = temp;
1902 xfs_iext_insert(bma->ip, &bma->icur, &PREV, state);
1903 xfs_iext_next(ifp, &bma->icur);
1908 * Filling in the middle part of a previous delayed allocation.
1909 * Contiguity is impossible here.
1910 * This case is avoided almost all the time.
1912 * We start with a delayed allocation:
1914 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
1917 * and we are allocating:
1918 * +rrrrrrrrrrrrrrrrr+
1921 * and we set it up for insertion as:
1922 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
1924 * PREV @ idx LEFT RIGHT
1925 * inserted at idx + 1
1929 /* LEFT is the new middle */
1932 /* RIGHT is the new right */
1933 RIGHT.br_state = PREV.br_state;
1934 RIGHT.br_startoff = new_endoff;
1935 RIGHT.br_blockcount =
1936 PREV.br_startoff + PREV.br_blockcount - new_endoff;
1937 RIGHT.br_startblock =
1938 nullstartblock(xfs_bmap_worst_indlen(bma->ip,
1939 RIGHT.br_blockcount));
1942 PREV.br_blockcount = new->br_startoff - PREV.br_startoff;
1943 PREV.br_startblock =
1944 nullstartblock(xfs_bmap_worst_indlen(bma->ip,
1945 PREV.br_blockcount));
1946 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
1948 xfs_iext_next(ifp, &bma->icur);
1949 xfs_iext_insert(bma->ip, &bma->icur, &RIGHT, state);
1950 xfs_iext_insert(bma->ip, &bma->icur, &LEFT, state);
1953 if (bma->cur == NULL)
1954 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1956 rval = XFS_ILOG_CORE;
1957 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
1960 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
1961 error = xfs_btree_insert(bma->cur, &i);
1964 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
1967 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
1968 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
1969 &bma->cur, 1, &tmp_rval, whichfork);
1975 da_new = startblockval(PREV.br_startblock) +
1976 startblockval(RIGHT.br_startblock);
1979 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1980 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1981 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
1982 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1983 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1984 case BMAP_LEFT_CONTIG:
1985 case BMAP_RIGHT_CONTIG:
1987 * These cases are all impossible.
1992 /* add reverse mapping unless caller opted out */
1993 if (!(bma->flags & XFS_BMAPI_NORMAP)) {
1994 error = xfs_rmap_map_extent(bma->tp, bma->ip, whichfork, new);
1999 /* convert to a btree if necessary */
2000 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
2001 int tmp_logflags; /* partial log flag return val */
2003 ASSERT(bma->cur == NULL);
2004 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
2005 &bma->cur, da_old > 0, &tmp_logflags,
2007 bma->logflags |= tmp_logflags;
2012 if (da_new != da_old)
2013 xfs_mod_delalloc(mp, (int64_t)da_new - da_old);
2016 da_new += bma->cur->bc_private.b.allocated;
2017 bma->cur->bc_private.b.allocated = 0;
2020 /* adjust for changes in reserved delayed indirect blocks */
2021 if (da_new != da_old) {
2022 ASSERT(state == 0 || da_new < da_old);
2023 error = xfs_mod_fdblocks(mp, (int64_t)(da_old - da_new),
2027 xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork);
2029 if (whichfork != XFS_COW_FORK)
2030 bma->logflags |= rval;
2038 * Convert an unwritten allocation to a real allocation or vice versa.
2041 xfs_bmap_add_extent_unwritten_real(
2042 struct xfs_trans *tp,
2043 xfs_inode_t *ip, /* incore inode pointer */
2045 struct xfs_iext_cursor *icur,
2046 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
2047 xfs_bmbt_irec_t *new, /* new data to add to file extents */
2048 int *logflagsp) /* inode logging flags */
2050 xfs_btree_cur_t *cur; /* btree cursor */
2051 int error; /* error return value */
2052 int i; /* temp state */
2053 struct xfs_ifork *ifp; /* inode fork pointer */
2054 xfs_fileoff_t new_endoff; /* end offset of new entry */
2055 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
2056 /* left is 0, right is 1, prev is 2 */
2057 int rval=0; /* return value (logging flags) */
2058 int state = xfs_bmap_fork_to_state(whichfork);
2059 struct xfs_mount *mp = ip->i_mount;
2060 struct xfs_bmbt_irec old;
2065 ifp = XFS_IFORK_PTR(ip, whichfork);
2067 ASSERT(!isnullstartblock(new->br_startblock));
2069 XFS_STATS_INC(mp, xs_add_exlist);
2076 * Set up a bunch of variables to make the tests simpler.
2079 xfs_iext_get_extent(ifp, icur, &PREV);
2080 ASSERT(new->br_state != PREV.br_state);
2081 new_endoff = new->br_startoff + new->br_blockcount;
2082 ASSERT(PREV.br_startoff <= new->br_startoff);
2083 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
2086 * Set flags determining what part of the previous oldext allocation
2087 * extent is being replaced by a newext allocation.
2089 if (PREV.br_startoff == new->br_startoff)
2090 state |= BMAP_LEFT_FILLING;
2091 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
2092 state |= BMAP_RIGHT_FILLING;
2095 * Check and set flags if this segment has a left neighbor.
2096 * Don't set contiguous if the combined extent would be too large.
2098 if (xfs_iext_peek_prev_extent(ifp, icur, &LEFT)) {
2099 state |= BMAP_LEFT_VALID;
2100 if (isnullstartblock(LEFT.br_startblock))
2101 state |= BMAP_LEFT_DELAY;
2104 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2105 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
2106 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
2107 LEFT.br_state == new->br_state &&
2108 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2109 state |= BMAP_LEFT_CONTIG;
2112 * Check and set flags if this segment has a right neighbor.
2113 * Don't set contiguous if the combined extent would be too large.
2114 * Also check for all-three-contiguous being too large.
2116 if (xfs_iext_peek_next_extent(ifp, icur, &RIGHT)) {
2117 state |= BMAP_RIGHT_VALID;
2118 if (isnullstartblock(RIGHT.br_startblock))
2119 state |= BMAP_RIGHT_DELAY;
2122 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2123 new_endoff == RIGHT.br_startoff &&
2124 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
2125 new->br_state == RIGHT.br_state &&
2126 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
2127 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2128 BMAP_RIGHT_FILLING)) !=
2129 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2130 BMAP_RIGHT_FILLING) ||
2131 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
2133 state |= BMAP_RIGHT_CONTIG;
2136 * Switch out based on the FILLING and CONTIG state bits.
2138 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2139 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
2140 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2141 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
2143 * Setting all of a previous oldext extent to newext.
2144 * The left and right neighbors are both contiguous with new.
2146 LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount;
2148 xfs_iext_remove(ip, icur, state);
2149 xfs_iext_remove(ip, icur, state);
2150 xfs_iext_prev(ifp, icur);
2151 xfs_iext_update_extent(ip, state, icur, &LEFT);
2152 XFS_IFORK_NEXT_SET(ip, whichfork,
2153 XFS_IFORK_NEXTENTS(ip, whichfork) - 2);
2155 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2157 rval = XFS_ILOG_CORE;
2158 error = xfs_bmbt_lookup_eq(cur, &RIGHT, &i);
2161 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2162 if ((error = xfs_btree_delete(cur, &i)))
2164 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2165 if ((error = xfs_btree_decrement(cur, 0, &i)))
2167 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2168 if ((error = xfs_btree_delete(cur, &i)))
2170 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2171 if ((error = xfs_btree_decrement(cur, 0, &i)))
2173 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2174 error = xfs_bmbt_update(cur, &LEFT);
2180 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2182 * Setting all of a previous oldext extent to newext.
2183 * The left neighbor is contiguous, the right is not.
2185 LEFT.br_blockcount += PREV.br_blockcount;
2187 xfs_iext_remove(ip, icur, state);
2188 xfs_iext_prev(ifp, icur);
2189 xfs_iext_update_extent(ip, state, icur, &LEFT);
2190 XFS_IFORK_NEXT_SET(ip, whichfork,
2191 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
2193 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2195 rval = XFS_ILOG_CORE;
2196 error = xfs_bmbt_lookup_eq(cur, &PREV, &i);
2199 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2200 if ((error = xfs_btree_delete(cur, &i)))
2202 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2203 if ((error = xfs_btree_decrement(cur, 0, &i)))
2205 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2206 error = xfs_bmbt_update(cur, &LEFT);
2212 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
2214 * Setting all of a previous oldext extent to newext.
2215 * The right neighbor is contiguous, the left is not.
2217 PREV.br_blockcount += RIGHT.br_blockcount;
2218 PREV.br_state = new->br_state;
2220 xfs_iext_next(ifp, icur);
2221 xfs_iext_remove(ip, icur, state);
2222 xfs_iext_prev(ifp, icur);
2223 xfs_iext_update_extent(ip, state, icur, &PREV);
2225 XFS_IFORK_NEXT_SET(ip, whichfork,
2226 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
2228 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2230 rval = XFS_ILOG_CORE;
2231 error = xfs_bmbt_lookup_eq(cur, &RIGHT, &i);
2234 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2235 if ((error = xfs_btree_delete(cur, &i)))
2237 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2238 if ((error = xfs_btree_decrement(cur, 0, &i)))
2240 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2241 error = xfs_bmbt_update(cur, &PREV);
2247 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
2249 * Setting all of a previous oldext extent to newext.
2250 * Neither the left nor right neighbors are contiguous with
2253 PREV.br_state = new->br_state;
2254 xfs_iext_update_extent(ip, state, icur, &PREV);
2257 rval = XFS_ILOG_DEXT;
2260 error = xfs_bmbt_lookup_eq(cur, new, &i);
2263 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2264 error = xfs_bmbt_update(cur, &PREV);
2270 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
2272 * Setting the first part of a previous oldext extent to newext.
2273 * The left neighbor is contiguous.
2275 LEFT.br_blockcount += new->br_blockcount;
2278 PREV.br_startoff += new->br_blockcount;
2279 PREV.br_startblock += new->br_blockcount;
2280 PREV.br_blockcount -= new->br_blockcount;
2282 xfs_iext_update_extent(ip, state, icur, &PREV);
2283 xfs_iext_prev(ifp, icur);
2284 xfs_iext_update_extent(ip, state, icur, &LEFT);
2287 rval = XFS_ILOG_DEXT;
2290 error = xfs_bmbt_lookup_eq(cur, &old, &i);
2293 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2294 error = xfs_bmbt_update(cur, &PREV);
2297 error = xfs_btree_decrement(cur, 0, &i);
2300 error = xfs_bmbt_update(cur, &LEFT);
2306 case BMAP_LEFT_FILLING:
2308 * Setting the first part of a previous oldext extent to newext.
2309 * The left neighbor is not contiguous.
2312 PREV.br_startoff += new->br_blockcount;
2313 PREV.br_startblock += new->br_blockcount;
2314 PREV.br_blockcount -= new->br_blockcount;
2316 xfs_iext_update_extent(ip, state, icur, &PREV);
2317 xfs_iext_insert(ip, icur, new, state);
2318 XFS_IFORK_NEXT_SET(ip, whichfork,
2319 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
2321 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2323 rval = XFS_ILOG_CORE;
2324 error = xfs_bmbt_lookup_eq(cur, &old, &i);
2327 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2328 error = xfs_bmbt_update(cur, &PREV);
2331 cur->bc_rec.b = *new;
2332 if ((error = xfs_btree_insert(cur, &i)))
2334 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2338 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
2340 * Setting the last part of a previous oldext extent to newext.
2341 * The right neighbor is contiguous with the new allocation.
2344 PREV.br_blockcount -= new->br_blockcount;
2346 RIGHT.br_startoff = new->br_startoff;
2347 RIGHT.br_startblock = new->br_startblock;
2348 RIGHT.br_blockcount += new->br_blockcount;
2350 xfs_iext_update_extent(ip, state, icur, &PREV);
2351 xfs_iext_next(ifp, icur);
2352 xfs_iext_update_extent(ip, state, icur, &RIGHT);
2355 rval = XFS_ILOG_DEXT;
2358 error = xfs_bmbt_lookup_eq(cur, &old, &i);
2361 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2362 error = xfs_bmbt_update(cur, &PREV);
2365 error = xfs_btree_increment(cur, 0, &i);
2368 error = xfs_bmbt_update(cur, &RIGHT);
2374 case BMAP_RIGHT_FILLING:
2376 * Setting the last part of a previous oldext extent to newext.
2377 * The right neighbor is not contiguous.
2380 PREV.br_blockcount -= new->br_blockcount;
2382 xfs_iext_update_extent(ip, state, icur, &PREV);
2383 xfs_iext_next(ifp, icur);
2384 xfs_iext_insert(ip, icur, new, state);
2386 XFS_IFORK_NEXT_SET(ip, whichfork,
2387 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
2389 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2391 rval = XFS_ILOG_CORE;
2392 error = xfs_bmbt_lookup_eq(cur, &old, &i);
2395 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2396 error = xfs_bmbt_update(cur, &PREV);
2399 error = xfs_bmbt_lookup_eq(cur, new, &i);
2402 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
2403 if ((error = xfs_btree_insert(cur, &i)))
2405 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2411 * Setting the middle part of a previous oldext extent to
2412 * newext. Contiguity is impossible here.
2413 * One extent becomes three extents.
2416 PREV.br_blockcount = new->br_startoff - PREV.br_startoff;
2419 r[1].br_startoff = new_endoff;
2420 r[1].br_blockcount =
2421 old.br_startoff + old.br_blockcount - new_endoff;
2422 r[1].br_startblock = new->br_startblock + new->br_blockcount;
2423 r[1].br_state = PREV.br_state;
2425 xfs_iext_update_extent(ip, state, icur, &PREV);
2426 xfs_iext_next(ifp, icur);
2427 xfs_iext_insert(ip, icur, &r[1], state);
2428 xfs_iext_insert(ip, icur, &r[0], state);
2430 XFS_IFORK_NEXT_SET(ip, whichfork,
2431 XFS_IFORK_NEXTENTS(ip, whichfork) + 2);
2433 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2435 rval = XFS_ILOG_CORE;
2436 error = xfs_bmbt_lookup_eq(cur, &old, &i);
2439 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2440 /* new right extent - oldext */
2441 error = xfs_bmbt_update(cur, &r[1]);
2444 /* new left extent - oldext */
2445 cur->bc_rec.b = PREV;
2446 if ((error = xfs_btree_insert(cur, &i)))
2448 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2450 * Reset the cursor to the position of the new extent
2451 * we are about to insert as we can't trust it after
2452 * the previous insert.
2454 error = xfs_bmbt_lookup_eq(cur, new, &i);
2457 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
2458 /* new middle extent - newext */
2459 if ((error = xfs_btree_insert(cur, &i)))
2461 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2465 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2466 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2467 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
2468 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2469 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2470 case BMAP_LEFT_CONTIG:
2471 case BMAP_RIGHT_CONTIG:
2473 * These cases are all impossible.
2478 /* update reverse mappings */
2479 error = xfs_rmap_convert_extent(mp, tp, ip, whichfork, new);
2483 /* convert to a btree if necessary */
2484 if (xfs_bmap_needs_btree(ip, whichfork)) {
2485 int tmp_logflags; /* partial log flag return val */
2487 ASSERT(cur == NULL);
2488 error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0,
2489 &tmp_logflags, whichfork);
2490 *logflagsp |= tmp_logflags;
2495 /* clear out the allocated field, done with it now in any case. */
2497 cur->bc_private.b.allocated = 0;
2501 xfs_bmap_check_leaf_extents(*curp, ip, whichfork);
2511 * Convert a hole to a delayed allocation.
2514 xfs_bmap_add_extent_hole_delay(
2515 xfs_inode_t *ip, /* incore inode pointer */
2517 struct xfs_iext_cursor *icur,
2518 xfs_bmbt_irec_t *new) /* new data to add to file extents */
2520 struct xfs_ifork *ifp; /* inode fork pointer */
2521 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2522 xfs_filblks_t newlen=0; /* new indirect size */
2523 xfs_filblks_t oldlen=0; /* old indirect size */
2524 xfs_bmbt_irec_t right; /* right neighbor extent entry */
2525 int state = xfs_bmap_fork_to_state(whichfork);
2526 xfs_filblks_t temp; /* temp for indirect calculations */
2528 ifp = XFS_IFORK_PTR(ip, whichfork);
2529 ASSERT(isnullstartblock(new->br_startblock));
2532 * Check and set flags if this segment has a left neighbor
2534 if (xfs_iext_peek_prev_extent(ifp, icur, &left)) {
2535 state |= BMAP_LEFT_VALID;
2536 if (isnullstartblock(left.br_startblock))
2537 state |= BMAP_LEFT_DELAY;
2541 * Check and set flags if the current (right) segment exists.
2542 * If it doesn't exist, we're converting the hole at end-of-file.
2544 if (xfs_iext_get_extent(ifp, icur, &right)) {
2545 state |= BMAP_RIGHT_VALID;
2546 if (isnullstartblock(right.br_startblock))
2547 state |= BMAP_RIGHT_DELAY;
2551 * Set contiguity flags on the left and right neighbors.
2552 * Don't let extents get too large, even if the pieces are contiguous.
2554 if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) &&
2555 left.br_startoff + left.br_blockcount == new->br_startoff &&
2556 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2557 state |= BMAP_LEFT_CONTIG;
2559 if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) &&
2560 new->br_startoff + new->br_blockcount == right.br_startoff &&
2561 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2562 (!(state & BMAP_LEFT_CONTIG) ||
2563 (left.br_blockcount + new->br_blockcount +
2564 right.br_blockcount <= MAXEXTLEN)))
2565 state |= BMAP_RIGHT_CONTIG;
2568 * Switch out based on the contiguity flags.
2570 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2571 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2573 * New allocation is contiguous with delayed allocations
2574 * on the left and on the right.
2575 * Merge all three into a single extent record.
2577 temp = left.br_blockcount + new->br_blockcount +
2578 right.br_blockcount;
2580 oldlen = startblockval(left.br_startblock) +
2581 startblockval(new->br_startblock) +
2582 startblockval(right.br_startblock);
2583 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2585 left.br_startblock = nullstartblock(newlen);
2586 left.br_blockcount = temp;
2588 xfs_iext_remove(ip, icur, state);
2589 xfs_iext_prev(ifp, icur);
2590 xfs_iext_update_extent(ip, state, icur, &left);
2593 case BMAP_LEFT_CONTIG:
2595 * New allocation is contiguous with a delayed allocation
2597 * Merge the new allocation with the left neighbor.
2599 temp = left.br_blockcount + new->br_blockcount;
2601 oldlen = startblockval(left.br_startblock) +
2602 startblockval(new->br_startblock);
2603 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2605 left.br_blockcount = temp;
2606 left.br_startblock = nullstartblock(newlen);
2608 xfs_iext_prev(ifp, icur);
2609 xfs_iext_update_extent(ip, state, icur, &left);
2612 case BMAP_RIGHT_CONTIG:
2614 * New allocation is contiguous with a delayed allocation
2616 * Merge the new allocation with the right neighbor.
2618 temp = new->br_blockcount + right.br_blockcount;
2619 oldlen = startblockval(new->br_startblock) +
2620 startblockval(right.br_startblock);
2621 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2623 right.br_startoff = new->br_startoff;
2624 right.br_startblock = nullstartblock(newlen);
2625 right.br_blockcount = temp;
2626 xfs_iext_update_extent(ip, state, icur, &right);
2631 * New allocation is not contiguous with another
2632 * delayed allocation.
2633 * Insert a new entry.
2635 oldlen = newlen = 0;
2636 xfs_iext_insert(ip, icur, new, state);
2639 if (oldlen != newlen) {
2640 ASSERT(oldlen > newlen);
2641 xfs_mod_fdblocks(ip->i_mount, (int64_t)(oldlen - newlen),
2644 * Nothing to do for disk quota accounting here.
2646 xfs_mod_delalloc(ip->i_mount, (int64_t)newlen - oldlen);
2651 * Convert a hole to a real allocation.
2653 STATIC int /* error */
2654 xfs_bmap_add_extent_hole_real(
2655 struct xfs_trans *tp,
2656 struct xfs_inode *ip,
2658 struct xfs_iext_cursor *icur,
2659 struct xfs_btree_cur **curp,
2660 struct xfs_bmbt_irec *new,
2664 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
2665 struct xfs_mount *mp = ip->i_mount;
2666 struct xfs_btree_cur *cur = *curp;
2667 int error; /* error return value */
2668 int i; /* temp state */
2669 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2670 xfs_bmbt_irec_t right; /* right neighbor extent entry */
2671 int rval=0; /* return value (logging flags) */
2672 int state = xfs_bmap_fork_to_state(whichfork);
2673 struct xfs_bmbt_irec old;
2675 ASSERT(!isnullstartblock(new->br_startblock));
2676 ASSERT(!cur || !(cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
2678 XFS_STATS_INC(mp, xs_add_exlist);
2681 * Check and set flags if this segment has a left neighbor.
2683 if (xfs_iext_peek_prev_extent(ifp, icur, &left)) {
2684 state |= BMAP_LEFT_VALID;
2685 if (isnullstartblock(left.br_startblock))
2686 state |= BMAP_LEFT_DELAY;
2690 * Check and set flags if this segment has a current value.
2691 * Not true if we're inserting into the "hole" at eof.
2693 if (xfs_iext_get_extent(ifp, icur, &right)) {
2694 state |= BMAP_RIGHT_VALID;
2695 if (isnullstartblock(right.br_startblock))
2696 state |= BMAP_RIGHT_DELAY;
2700 * We're inserting a real allocation between "left" and "right".
2701 * Set the contiguity flags. Don't let extents get too large.
2703 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2704 left.br_startoff + left.br_blockcount == new->br_startoff &&
2705 left.br_startblock + left.br_blockcount == new->br_startblock &&
2706 left.br_state == new->br_state &&
2707 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2708 state |= BMAP_LEFT_CONTIG;
2710 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2711 new->br_startoff + new->br_blockcount == right.br_startoff &&
2712 new->br_startblock + new->br_blockcount == right.br_startblock &&
2713 new->br_state == right.br_state &&
2714 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2715 (!(state & BMAP_LEFT_CONTIG) ||
2716 left.br_blockcount + new->br_blockcount +
2717 right.br_blockcount <= MAXEXTLEN))
2718 state |= BMAP_RIGHT_CONTIG;
2722 * Select which case we're in here, and implement it.
2724 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2725 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2727 * New allocation is contiguous with real allocations on the
2728 * left and on the right.
2729 * Merge all three into a single extent record.
2731 left.br_blockcount += new->br_blockcount + right.br_blockcount;
2733 xfs_iext_remove(ip, icur, state);
2734 xfs_iext_prev(ifp, icur);
2735 xfs_iext_update_extent(ip, state, icur, &left);
2737 XFS_IFORK_NEXT_SET(ip, whichfork,
2738 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
2740 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
2742 rval = XFS_ILOG_CORE;
2743 error = xfs_bmbt_lookup_eq(cur, &right, &i);
2746 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2747 error = xfs_btree_delete(cur, &i);
2750 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2751 error = xfs_btree_decrement(cur, 0, &i);
2754 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2755 error = xfs_bmbt_update(cur, &left);
2761 case BMAP_LEFT_CONTIG:
2763 * New allocation is contiguous with a real allocation
2765 * Merge the new allocation with the left neighbor.
2768 left.br_blockcount += new->br_blockcount;
2770 xfs_iext_prev(ifp, icur);
2771 xfs_iext_update_extent(ip, state, icur, &left);
2774 rval = xfs_ilog_fext(whichfork);
2777 error = xfs_bmbt_lookup_eq(cur, &old, &i);
2780 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2781 error = xfs_bmbt_update(cur, &left);
2787 case BMAP_RIGHT_CONTIG:
2789 * New allocation is contiguous with a real allocation
2791 * Merge the new allocation with the right neighbor.
2795 right.br_startoff = new->br_startoff;
2796 right.br_startblock = new->br_startblock;
2797 right.br_blockcount += new->br_blockcount;
2798 xfs_iext_update_extent(ip, state, icur, &right);
2801 rval = xfs_ilog_fext(whichfork);
2804 error = xfs_bmbt_lookup_eq(cur, &old, &i);
2807 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2808 error = xfs_bmbt_update(cur, &right);
2816 * New allocation is not contiguous with another
2818 * Insert a new entry.
2820 xfs_iext_insert(ip, icur, new, state);
2821 XFS_IFORK_NEXT_SET(ip, whichfork,
2822 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
2824 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
2826 rval = XFS_ILOG_CORE;
2827 error = xfs_bmbt_lookup_eq(cur, new, &i);
2830 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
2831 error = xfs_btree_insert(cur, &i);
2834 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2839 /* add reverse mapping unless caller opted out */
2840 if (!(flags & XFS_BMAPI_NORMAP)) {
2841 error = xfs_rmap_map_extent(tp, ip, whichfork, new);
2846 /* convert to a btree if necessary */
2847 if (xfs_bmap_needs_btree(ip, whichfork)) {
2848 int tmp_logflags; /* partial log flag return val */
2850 ASSERT(cur == NULL);
2851 error = xfs_bmap_extents_to_btree(tp, ip, curp, 0,
2852 &tmp_logflags, whichfork);
2853 *logflagsp |= tmp_logflags;
2859 /* clear out the allocated field, done with it now in any case. */
2861 cur->bc_private.b.allocated = 0;
2863 xfs_bmap_check_leaf_extents(cur, ip, whichfork);
2870 * Functions used in the extent read, allocate and remove paths
2874 * Adjust the size of the new extent based on di_extsize and rt extsize.
2877 xfs_bmap_extsize_align(
2879 xfs_bmbt_irec_t *gotp, /* next extent pointer */
2880 xfs_bmbt_irec_t *prevp, /* previous extent pointer */
2881 xfs_extlen_t extsz, /* align to this extent size */
2882 int rt, /* is this a realtime inode? */
2883 int eof, /* is extent at end-of-file? */
2884 int delay, /* creating delalloc extent? */
2885 int convert, /* overwriting unwritten extent? */
2886 xfs_fileoff_t *offp, /* in/out: aligned offset */
2887 xfs_extlen_t *lenp) /* in/out: aligned length */
2889 xfs_fileoff_t orig_off; /* original offset */
2890 xfs_extlen_t orig_alen; /* original length */
2891 xfs_fileoff_t orig_end; /* original off+len */
2892 xfs_fileoff_t nexto; /* next file offset */
2893 xfs_fileoff_t prevo; /* previous file offset */
2894 xfs_fileoff_t align_off; /* temp for offset */
2895 xfs_extlen_t align_alen; /* temp for length */
2896 xfs_extlen_t temp; /* temp for calculations */
2901 orig_off = align_off = *offp;
2902 orig_alen = align_alen = *lenp;
2903 orig_end = orig_off + orig_alen;
2906 * If this request overlaps an existing extent, then don't
2907 * attempt to perform any additional alignment.
2909 if (!delay && !eof &&
2910 (orig_off >= gotp->br_startoff) &&
2911 (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
2916 * If the file offset is unaligned vs. the extent size
2917 * we need to align it. This will be possible unless
2918 * the file was previously written with a kernel that didn't
2919 * perform this alignment, or if a truncate shot us in the
2922 div_u64_rem(orig_off, extsz, &temp);
2928 /* Same adjustment for the end of the requested area. */
2929 temp = (align_alen % extsz);
2931 align_alen += extsz - temp;
2934 * For large extent hint sizes, the aligned extent might be larger than
2935 * MAXEXTLEN. In that case, reduce the size by an extsz so that it pulls
2936 * the length back under MAXEXTLEN. The outer allocation loops handle
2937 * short allocation just fine, so it is safe to do this. We only want to
2938 * do it when we are forced to, though, because it means more allocation
2939 * operations are required.
2941 while (align_alen > MAXEXTLEN)
2942 align_alen -= extsz;
2943 ASSERT(align_alen <= MAXEXTLEN);
2946 * If the previous block overlaps with this proposed allocation
2947 * then move the start forward without adjusting the length.
2949 if (prevp->br_startoff != NULLFILEOFF) {
2950 if (prevp->br_startblock == HOLESTARTBLOCK)
2951 prevo = prevp->br_startoff;
2953 prevo = prevp->br_startoff + prevp->br_blockcount;
2956 if (align_off != orig_off && align_off < prevo)
2959 * If the next block overlaps with this proposed allocation
2960 * then move the start back without adjusting the length,
2961 * but not before offset 0.
2962 * This may of course make the start overlap previous block,
2963 * and if we hit the offset 0 limit then the next block
2964 * can still overlap too.
2966 if (!eof && gotp->br_startoff != NULLFILEOFF) {
2967 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
2968 (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
2969 nexto = gotp->br_startoff + gotp->br_blockcount;
2971 nexto = gotp->br_startoff;
2973 nexto = NULLFILEOFF;
2975 align_off + align_alen != orig_end &&
2976 align_off + align_alen > nexto)
2977 align_off = nexto > align_alen ? nexto - align_alen : 0;
2979 * If we're now overlapping the next or previous extent that
2980 * means we can't fit an extsz piece in this hole. Just move
2981 * the start forward to the first valid spot and set
2982 * the length so we hit the end.
2984 if (align_off != orig_off && align_off < prevo)
2986 if (align_off + align_alen != orig_end &&
2987 align_off + align_alen > nexto &&
2988 nexto != NULLFILEOFF) {
2989 ASSERT(nexto > prevo);
2990 align_alen = nexto - align_off;
2994 * If realtime, and the result isn't a multiple of the realtime
2995 * extent size we need to remove blocks until it is.
2997 if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
2999 * We're not covering the original request, or
3000 * we won't be able to once we fix the length.
3002 if (orig_off < align_off ||
3003 orig_end > align_off + align_alen ||
3004 align_alen - temp < orig_alen)
3007 * Try to fix it by moving the start up.
3009 if (align_off + temp <= orig_off) {
3014 * Try to fix it by moving the end in.
3016 else if (align_off + align_alen - temp >= orig_end)
3019 * Set the start to the minimum then trim the length.
3022 align_alen -= orig_off - align_off;
3023 align_off = orig_off;
3024 align_alen -= align_alen % mp->m_sb.sb_rextsize;
3027 * Result doesn't cover the request, fail it.
3029 if (orig_off < align_off || orig_end > align_off + align_alen)
3032 ASSERT(orig_off >= align_off);
3033 /* see MAXEXTLEN handling above */
3034 ASSERT(orig_end <= align_off + align_alen ||
3035 align_alen + extsz > MAXEXTLEN);
3039 if (!eof && gotp->br_startoff != NULLFILEOFF)
3040 ASSERT(align_off + align_alen <= gotp->br_startoff);
3041 if (prevp->br_startoff != NULLFILEOFF)
3042 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
3050 #define XFS_ALLOC_GAP_UNITS 4
3054 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
3056 xfs_fsblock_t adjust; /* adjustment to block numbers */
3057 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
3058 xfs_mount_t *mp; /* mount point structure */
3059 int nullfb; /* true if ap->firstblock isn't set */
3060 int rt; /* true if inode is realtime */
3062 #define ISVALID(x,y) \
3064 (x) < mp->m_sb.sb_rblocks : \
3065 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
3066 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
3067 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
3069 mp = ap->ip->i_mount;
3070 nullfb = ap->tp->t_firstblock == NULLFSBLOCK;
3071 rt = XFS_IS_REALTIME_INODE(ap->ip) &&
3072 xfs_alloc_is_userdata(ap->datatype);
3073 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp,
3074 ap->tp->t_firstblock);
3076 * If allocating at eof, and there's a previous real block,
3077 * try to use its last block as our starting point.
3079 if (ap->eof && ap->prev.br_startoff != NULLFILEOFF &&
3080 !isnullstartblock(ap->prev.br_startblock) &&
3081 ISVALID(ap->prev.br_startblock + ap->prev.br_blockcount,
3082 ap->prev.br_startblock)) {
3083 ap->blkno = ap->prev.br_startblock + ap->prev.br_blockcount;
3085 * Adjust for the gap between prevp and us.
3087 adjust = ap->offset -
3088 (ap->prev.br_startoff + ap->prev.br_blockcount);
3090 ISVALID(ap->blkno + adjust, ap->prev.br_startblock))
3091 ap->blkno += adjust;
3094 * If not at eof, then compare the two neighbor blocks.
3095 * Figure out whether either one gives us a good starting point,
3096 * and pick the better one.
3098 else if (!ap->eof) {
3099 xfs_fsblock_t gotbno; /* right side block number */
3100 xfs_fsblock_t gotdiff=0; /* right side difference */
3101 xfs_fsblock_t prevbno; /* left side block number */
3102 xfs_fsblock_t prevdiff=0; /* left side difference */
3105 * If there's a previous (left) block, select a requested
3106 * start block based on it.
3108 if (ap->prev.br_startoff != NULLFILEOFF &&
3109 !isnullstartblock(ap->prev.br_startblock) &&
3110 (prevbno = ap->prev.br_startblock +
3111 ap->prev.br_blockcount) &&
3112 ISVALID(prevbno, ap->prev.br_startblock)) {
3114 * Calculate gap to end of previous block.
3116 adjust = prevdiff = ap->offset -
3117 (ap->prev.br_startoff +
3118 ap->prev.br_blockcount);
3120 * Figure the startblock based on the previous block's
3121 * end and the gap size.
3123 * If the gap is large relative to the piece we're
3124 * allocating, or using it gives us an invalid block
3125 * number, then just use the end of the previous block.
3127 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
3128 ISVALID(prevbno + prevdiff,
3129 ap->prev.br_startblock))
3134 * If the firstblock forbids it, can't use it,
3137 if (!rt && !nullfb &&
3138 XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
3139 prevbno = NULLFSBLOCK;
3142 * No previous block or can't follow it, just default.
3145 prevbno = NULLFSBLOCK;
3147 * If there's a following (right) block, select a requested
3148 * start block based on it.
3150 if (!isnullstartblock(ap->got.br_startblock)) {
3152 * Calculate gap to start of next block.
3154 adjust = gotdiff = ap->got.br_startoff - ap->offset;
3156 * Figure the startblock based on the next block's
3157 * start and the gap size.
3159 gotbno = ap->got.br_startblock;
3162 * If the gap is large relative to the piece we're
3163 * allocating, or using it gives us an invalid block
3164 * number, then just use the start of the next block
3165 * offset by our length.
3167 if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
3168 ISVALID(gotbno - gotdiff, gotbno))
3170 else if (ISVALID(gotbno - ap->length, gotbno)) {
3171 gotbno -= ap->length;
3172 gotdiff += adjust - ap->length;
3176 * If the firstblock forbids it, can't use it,
3179 if (!rt && !nullfb &&
3180 XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
3181 gotbno = NULLFSBLOCK;
3184 * No next block, just default.
3187 gotbno = NULLFSBLOCK;
3189 * If both valid, pick the better one, else the only good
3190 * one, else ap->blkno is already set (to 0 or the inode block).
3192 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
3193 ap->blkno = prevdiff <= gotdiff ? prevbno : gotbno;
3194 else if (prevbno != NULLFSBLOCK)
3195 ap->blkno = prevbno;
3196 else if (gotbno != NULLFSBLOCK)
3203 xfs_bmap_longest_free_extent(
3204 struct xfs_trans *tp,
3209 struct xfs_mount *mp = tp->t_mountp;
3210 struct xfs_perag *pag;
3211 xfs_extlen_t longest;
3214 pag = xfs_perag_get(mp, ag);
3215 if (!pag->pagf_init) {
3216 error = xfs_alloc_pagf_init(mp, tp, ag, XFS_ALLOC_FLAG_TRYLOCK);
3220 if (!pag->pagf_init) {
3226 longest = xfs_alloc_longest_free_extent(pag,
3227 xfs_alloc_min_freelist(mp, pag),
3228 xfs_ag_resv_needed(pag, XFS_AG_RESV_NONE));
3229 if (*blen < longest)
3238 xfs_bmap_select_minlen(
3239 struct xfs_bmalloca *ap,
3240 struct xfs_alloc_arg *args,
3244 if (notinit || *blen < ap->minlen) {
3246 * Since we did a BUF_TRYLOCK above, it is possible that
3247 * there is space for this request.
3249 args->minlen = ap->minlen;
3250 } else if (*blen < args->maxlen) {
3252 * If the best seen length is less than the request length,
3253 * use the best as the minimum.
3255 args->minlen = *blen;
3258 * Otherwise we've seen an extent as big as maxlen, use that
3261 args->minlen = args->maxlen;
3266 xfs_bmap_btalloc_nullfb(
3267 struct xfs_bmalloca *ap,
3268 struct xfs_alloc_arg *args,
3271 struct xfs_mount *mp = ap->ip->i_mount;
3272 xfs_agnumber_t ag, startag;
3276 args->type = XFS_ALLOCTYPE_START_BNO;
3277 args->total = ap->total;
3279 startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3280 if (startag == NULLAGNUMBER)
3283 while (*blen < args->maxlen) {
3284 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3289 if (++ag == mp->m_sb.sb_agcount)
3295 xfs_bmap_select_minlen(ap, args, blen, notinit);
3300 xfs_bmap_btalloc_filestreams(
3301 struct xfs_bmalloca *ap,
3302 struct xfs_alloc_arg *args,
3305 struct xfs_mount *mp = ap->ip->i_mount;
3310 args->type = XFS_ALLOCTYPE_NEAR_BNO;
3311 args->total = ap->total;
3313 ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3314 if (ag == NULLAGNUMBER)
3317 error = xfs_bmap_longest_free_extent(args->tp, ag, blen, ¬init);
3321 if (*blen < args->maxlen) {
3322 error = xfs_filestream_new_ag(ap, &ag);
3326 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3333 xfs_bmap_select_minlen(ap, args, blen, notinit);
3336 * Set the failure fallback case to look in the selected AG as stream
3339 ap->blkno = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0);
3343 /* Update all inode and quota accounting for the allocation we just did. */
3345 xfs_bmap_btalloc_accounting(
3346 struct xfs_bmalloca *ap,
3347 struct xfs_alloc_arg *args)
3349 if (ap->flags & XFS_BMAPI_COWFORK) {
3351 * COW fork blocks are in-core only and thus are treated as
3352 * in-core quota reservation (like delalloc blocks) even when
3353 * converted to real blocks. The quota reservation is not
3354 * accounted to disk until blocks are remapped to the data
3355 * fork. So if these blocks were previously delalloc, we
3356 * already have quota reservation and there's nothing to do
3360 xfs_mod_delalloc(ap->ip->i_mount, -(int64_t)args->len);
3365 * Otherwise, we've allocated blocks in a hole. The transaction
3366 * has acquired in-core quota reservation for this extent.
3367 * Rather than account these as real blocks, however, we reduce
3368 * the transaction quota reservation based on the allocation.
3369 * This essentially transfers the transaction quota reservation
3370 * to that of a delalloc extent.
3372 ap->ip->i_delayed_blks += args->len;
3373 xfs_trans_mod_dquot_byino(ap->tp, ap->ip, XFS_TRANS_DQ_RES_BLKS,
3378 /* data/attr fork only */
3379 ap->ip->i_d.di_nblocks += args->len;
3380 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
3382 ap->ip->i_delayed_blks -= args->len;
3383 xfs_mod_delalloc(ap->ip->i_mount, -(int64_t)args->len);
3385 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
3386 ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT : XFS_TRANS_DQ_BCOUNT,
3392 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
3394 xfs_mount_t *mp; /* mount point structure */
3395 xfs_alloctype_t atype = 0; /* type for allocation routines */
3396 xfs_extlen_t align = 0; /* minimum allocation alignment */
3397 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
3399 xfs_alloc_arg_t args;
3400 xfs_fileoff_t orig_offset;
3401 xfs_extlen_t orig_length;
3403 xfs_extlen_t nextminlen = 0;
3404 int nullfb; /* true if ap->firstblock isn't set */
3411 orig_offset = ap->offset;
3412 orig_length = ap->length;
3414 mp = ap->ip->i_mount;
3416 /* stripe alignment for allocation is determined by mount parameters */
3418 if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC))
3419 stripe_align = mp->m_swidth;
3420 else if (mp->m_dalign)
3421 stripe_align = mp->m_dalign;
3423 if (ap->flags & XFS_BMAPI_COWFORK)
3424 align = xfs_get_cowextsz_hint(ap->ip);
3425 else if (xfs_alloc_is_userdata(ap->datatype))
3426 align = xfs_get_extsz_hint(ap->ip);
3428 error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev,
3429 align, 0, ap->eof, 0, ap->conv,
3430 &ap->offset, &ap->length);
3436 nullfb = ap->tp->t_firstblock == NULLFSBLOCK;
3437 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp,
3438 ap->tp->t_firstblock);
3440 if (xfs_alloc_is_userdata(ap->datatype) &&
3441 xfs_inode_is_filestream(ap->ip)) {
3442 ag = xfs_filestream_lookup_ag(ap->ip);
3443 ag = (ag != NULLAGNUMBER) ? ag : 0;
3444 ap->blkno = XFS_AGB_TO_FSB(mp, ag, 0);
3446 ap->blkno = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
3449 ap->blkno = ap->tp->t_firstblock;
3451 xfs_bmap_adjacent(ap);
3454 * If allowed, use ap->blkno; otherwise must use firstblock since
3455 * it's in the right allocation group.
3457 if (nullfb || XFS_FSB_TO_AGNO(mp, ap->blkno) == fb_agno)
3460 ap->blkno = ap->tp->t_firstblock;
3462 * Normal allocation, done through xfs_alloc_vextent.
3464 tryagain = isaligned = 0;
3465 memset(&args, 0, sizeof(args));
3468 args.fsbno = ap->blkno;
3469 args.oinfo = XFS_RMAP_OINFO_SKIP_UPDATE;
3471 /* Trim the allocation back to the maximum an AG can fit. */
3472 args.maxlen = min(ap->length, mp->m_ag_max_usable);
3476 * Search for an allocation group with a single extent large
3477 * enough for the request. If one isn't found, then adjust
3478 * the minimum allocation size to the largest space found.
3480 if (xfs_alloc_is_userdata(ap->datatype) &&
3481 xfs_inode_is_filestream(ap->ip))
3482 error = xfs_bmap_btalloc_filestreams(ap, &args, &blen);
3484 error = xfs_bmap_btalloc_nullfb(ap, &args, &blen);
3487 } else if (ap->tp->t_flags & XFS_TRANS_LOWMODE) {
3488 if (xfs_inode_is_filestream(ap->ip))
3489 args.type = XFS_ALLOCTYPE_FIRST_AG;
3491 args.type = XFS_ALLOCTYPE_START_BNO;
3492 args.total = args.minlen = ap->minlen;
3494 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3495 args.total = ap->total;
3496 args.minlen = ap->minlen;
3498 /* apply extent size hints if obtained earlier */
3501 div_u64_rem(ap->offset, args.prod, &args.mod);
3503 args.mod = args.prod - args.mod;
3504 } else if (mp->m_sb.sb_blocksize >= PAGE_SIZE) {
3508 args.prod = PAGE_SIZE >> mp->m_sb.sb_blocklog;
3509 div_u64_rem(ap->offset, args.prod, &args.mod);
3511 args.mod = args.prod - args.mod;
3514 * If we are not low on available data blocks, and the
3515 * underlying logical volume manager is a stripe, and
3516 * the file offset is zero then try to allocate data
3517 * blocks on stripe unit boundary.
3518 * NOTE: ap->aeof is only set if the allocation length
3519 * is >= the stripe unit and the allocation offset is
3520 * at the end of file.
3522 if (!(ap->tp->t_flags & XFS_TRANS_LOWMODE) && ap->aeof) {
3524 args.alignment = stripe_align;
3528 * Adjust for alignment
3530 if (blen > args.alignment && blen <= args.maxlen)
3531 args.minlen = blen - args.alignment;
3532 args.minalignslop = 0;
3535 * First try an exact bno allocation.
3536 * If it fails then do a near or start bno
3537 * allocation with alignment turned on.
3541 args.type = XFS_ALLOCTYPE_THIS_BNO;
3544 * Compute the minlen+alignment for the
3545 * next case. Set slop so that the value
3546 * of minlen+alignment+slop doesn't go up
3547 * between the calls.
3549 if (blen > stripe_align && blen <= args.maxlen)
3550 nextminlen = blen - stripe_align;
3552 nextminlen = args.minlen;
3553 if (nextminlen + stripe_align > args.minlen + 1)
3555 nextminlen + stripe_align -
3558 args.minalignslop = 0;
3562 args.minalignslop = 0;
3564 args.minleft = ap->minleft;
3565 args.wasdel = ap->wasdel;
3566 args.resv = XFS_AG_RESV_NONE;
3567 args.datatype = ap->datatype;
3568 if (ap->datatype & XFS_ALLOC_USERDATA_ZERO)
3571 error = xfs_alloc_vextent(&args);
3575 if (tryagain && args.fsbno == NULLFSBLOCK) {
3577 * Exact allocation failed. Now try with alignment
3581 args.fsbno = ap->blkno;
3582 args.alignment = stripe_align;
3583 args.minlen = nextminlen;
3584 args.minalignslop = 0;
3586 if ((error = xfs_alloc_vextent(&args)))
3589 if (isaligned && args.fsbno == NULLFSBLOCK) {
3591 * allocation failed, so turn off alignment and
3595 args.fsbno = ap->blkno;
3597 if ((error = xfs_alloc_vextent(&args)))
3600 if (args.fsbno == NULLFSBLOCK && nullfb &&
3601 args.minlen > ap->minlen) {
3602 args.minlen = ap->minlen;
3603 args.type = XFS_ALLOCTYPE_START_BNO;
3604 args.fsbno = ap->blkno;
3605 if ((error = xfs_alloc_vextent(&args)))
3608 if (args.fsbno == NULLFSBLOCK && nullfb) {
3610 args.type = XFS_ALLOCTYPE_FIRST_AG;
3611 args.total = ap->minlen;
3612 if ((error = xfs_alloc_vextent(&args)))
3614 ap->tp->t_flags |= XFS_TRANS_LOWMODE;
3616 if (args.fsbno != NULLFSBLOCK) {
3618 * check the allocation happened at the same or higher AG than
3619 * the first block that was allocated.
3621 ASSERT(ap->tp->t_firstblock == NULLFSBLOCK ||
3622 XFS_FSB_TO_AGNO(mp, ap->tp->t_firstblock) <=
3623 XFS_FSB_TO_AGNO(mp, args.fsbno));
3625 ap->blkno = args.fsbno;
3626 if (ap->tp->t_firstblock == NULLFSBLOCK)
3627 ap->tp->t_firstblock = args.fsbno;
3628 ASSERT(nullfb || fb_agno <= args.agno);
3629 ap->length = args.len;
3631 * If the extent size hint is active, we tried to round the
3632 * caller's allocation request offset down to extsz and the
3633 * length up to another extsz boundary. If we found a free
3634 * extent we mapped it in starting at this new offset. If the
3635 * newly mapped space isn't long enough to cover any of the
3636 * range of offsets that was originally requested, move the
3637 * mapping up so that we can fill as much of the caller's
3638 * original request as possible. Free space is apparently
3639 * very fragmented so we're unlikely to be able to satisfy the
3642 if (ap->length <= orig_length)
3643 ap->offset = orig_offset;
3644 else if (ap->offset + ap->length < orig_offset + orig_length)
3645 ap->offset = orig_offset + orig_length - ap->length;
3646 xfs_bmap_btalloc_accounting(ap, &args);
3648 ap->blkno = NULLFSBLOCK;
3655 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
3656 * It figures out where to ask the underlying allocator to put the new extent.
3660 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
3662 if (XFS_IS_REALTIME_INODE(ap->ip) &&
3663 xfs_alloc_is_userdata(ap->datatype))
3664 return xfs_bmap_rtalloc(ap);
3665 return xfs_bmap_btalloc(ap);
3668 /* Trim extent to fit a logical block range. */
3671 struct xfs_bmbt_irec *irec,
3675 xfs_fileoff_t distance;
3676 xfs_fileoff_t end = bno + len;
3678 if (irec->br_startoff + irec->br_blockcount <= bno ||
3679 irec->br_startoff >= end) {
3680 irec->br_blockcount = 0;
3684 if (irec->br_startoff < bno) {
3685 distance = bno - irec->br_startoff;
3686 if (isnullstartblock(irec->br_startblock))
3687 irec->br_startblock = DELAYSTARTBLOCK;
3688 if (irec->br_startblock != DELAYSTARTBLOCK &&
3689 irec->br_startblock != HOLESTARTBLOCK)
3690 irec->br_startblock += distance;
3691 irec->br_startoff += distance;
3692 irec->br_blockcount -= distance;
3695 if (end < irec->br_startoff + irec->br_blockcount) {
3696 distance = irec->br_startoff + irec->br_blockcount - end;
3697 irec->br_blockcount -= distance;
3702 * Trim the returned map to the required bounds
3706 struct xfs_bmbt_irec *mval,
3707 struct xfs_bmbt_irec *got,
3715 if ((flags & XFS_BMAPI_ENTIRE) ||
3716 got->br_startoff + got->br_blockcount <= obno) {
3718 if (isnullstartblock(got->br_startblock))
3719 mval->br_startblock = DELAYSTARTBLOCK;
3725 ASSERT((*bno >= obno) || (n == 0));
3727 mval->br_startoff = *bno;
3728 if (isnullstartblock(got->br_startblock))
3729 mval->br_startblock = DELAYSTARTBLOCK;
3731 mval->br_startblock = got->br_startblock +
3732 (*bno - got->br_startoff);
3734 * Return the minimum of what we got and what we asked for for
3735 * the length. We can use the len variable here because it is
3736 * modified below and we could have been there before coming
3737 * here if the first part of the allocation didn't overlap what
3740 mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno,
3741 got->br_blockcount - (*bno - got->br_startoff));
3742 mval->br_state = got->br_state;
3743 ASSERT(mval->br_blockcount <= len);
3748 * Update and validate the extent map to return
3751 xfs_bmapi_update_map(
3752 struct xfs_bmbt_irec **map,
3760 xfs_bmbt_irec_t *mval = *map;
3762 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
3763 ((mval->br_startoff + mval->br_blockcount) <= end));
3764 ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) ||
3765 (mval->br_startoff < obno));
3767 *bno = mval->br_startoff + mval->br_blockcount;
3769 if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) {
3770 /* update previous map with new information */
3771 ASSERT(mval->br_startblock == mval[-1].br_startblock);
3772 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
3773 ASSERT(mval->br_state == mval[-1].br_state);
3774 mval[-1].br_blockcount = mval->br_blockcount;
3775 mval[-1].br_state = mval->br_state;
3776 } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
3777 mval[-1].br_startblock != DELAYSTARTBLOCK &&
3778 mval[-1].br_startblock != HOLESTARTBLOCK &&
3779 mval->br_startblock == mval[-1].br_startblock +
3780 mval[-1].br_blockcount &&
3781 mval[-1].br_state == mval->br_state) {
3782 ASSERT(mval->br_startoff ==
3783 mval[-1].br_startoff + mval[-1].br_blockcount);
3784 mval[-1].br_blockcount += mval->br_blockcount;
3785 } else if (*n > 0 &&
3786 mval->br_startblock == DELAYSTARTBLOCK &&
3787 mval[-1].br_startblock == DELAYSTARTBLOCK &&
3788 mval->br_startoff ==
3789 mval[-1].br_startoff + mval[-1].br_blockcount) {
3790 mval[-1].br_blockcount += mval->br_blockcount;
3791 mval[-1].br_state = mval->br_state;
3792 } else if (!((*n == 0) &&
3793 ((mval->br_startoff + mval->br_blockcount) <=
3802 * Map file blocks to filesystem blocks without allocation.
3806 struct xfs_inode *ip,
3809 struct xfs_bmbt_irec *mval,
3813 struct xfs_mount *mp = ip->i_mount;
3814 struct xfs_ifork *ifp;
3815 struct xfs_bmbt_irec got;
3818 struct xfs_iext_cursor icur;
3822 int whichfork = xfs_bmapi_whichfork(flags);
3825 ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK|XFS_BMAPI_ENTIRE|
3826 XFS_BMAPI_COWFORK)));
3827 ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED|XFS_ILOCK_EXCL));
3829 if (unlikely(XFS_TEST_ERROR(
3830 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
3831 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
3832 mp, XFS_ERRTAG_BMAPIFORMAT))) {
3833 XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW, mp);
3834 return -EFSCORRUPTED;
3837 if (XFS_FORCED_SHUTDOWN(mp))
3840 XFS_STATS_INC(mp, xs_blk_mapr);
3842 ifp = XFS_IFORK_PTR(ip, whichfork);
3844 /* No CoW fork? Return a hole. */
3845 if (whichfork == XFS_COW_FORK && !ifp) {
3846 mval->br_startoff = bno;
3847 mval->br_startblock = HOLESTARTBLOCK;
3848 mval->br_blockcount = len;
3849 mval->br_state = XFS_EXT_NORM;
3854 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
3855 error = xfs_iread_extents(NULL, ip, whichfork);
3860 if (!xfs_iext_lookup_extent(ip, ifp, bno, &icur, &got))
3865 while (bno < end && n < *nmap) {
3866 /* Reading past eof, act as though there's a hole up to end. */
3868 got.br_startoff = end;
3869 if (got.br_startoff > bno) {
3870 /* Reading in a hole. */
3871 mval->br_startoff = bno;
3872 mval->br_startblock = HOLESTARTBLOCK;
3873 mval->br_blockcount =
3874 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
3875 mval->br_state = XFS_EXT_NORM;
3876 bno += mval->br_blockcount;
3877 len -= mval->br_blockcount;
3883 /* set up the extent map to return. */
3884 xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
3885 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
3887 /* If we're done, stop now. */
3888 if (bno >= end || n >= *nmap)
3891 /* Else go on to the next record. */
3892 if (!xfs_iext_next_extent(ifp, &icur, &got))
3900 * Add a delayed allocation extent to an inode. Blocks are reserved from the
3901 * global pool and the extent inserted into the inode in-core extent tree.
3903 * On entry, got refers to the first extent beyond the offset of the extent to
3904 * allocate or eof is specified if no such extent exists. On return, got refers
3905 * to the extent record that was inserted to the inode fork.
3907 * Note that the allocated extent may have been merged with contiguous extents
3908 * during insertion into the inode fork. Thus, got does not reflect the current
3909 * state of the inode fork on return. If necessary, the caller can use lastx to
3910 * look up the updated record in the inode fork.
3913 xfs_bmapi_reserve_delalloc(
3914 struct xfs_inode *ip,
3918 xfs_filblks_t prealloc,
3919 struct xfs_bmbt_irec *got,
3920 struct xfs_iext_cursor *icur,
3923 struct xfs_mount *mp = ip->i_mount;
3924 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
3926 xfs_extlen_t indlen;
3928 xfs_fileoff_t aoff = off;
3931 * Cap the alloc length. Keep track of prealloc so we know whether to
3932 * tag the inode before we return.
3934 alen = XFS_FILBLKS_MIN(len + prealloc, MAXEXTLEN);
3936 alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff);
3937 if (prealloc && alen >= len)
3938 prealloc = alen - len;
3940 /* Figure out the extent size, adjust alen */
3941 if (whichfork == XFS_COW_FORK) {
3942 struct xfs_bmbt_irec prev;
3943 xfs_extlen_t extsz = xfs_get_cowextsz_hint(ip);
3945 if (!xfs_iext_peek_prev_extent(ifp, icur, &prev))
3946 prev.br_startoff = NULLFILEOFF;
3948 error = xfs_bmap_extsize_align(mp, got, &prev, extsz, 0, eof,
3949 1, 0, &aoff, &alen);
3954 * Make a transaction-less quota reservation for delayed allocation
3955 * blocks. This number gets adjusted later. We return if we haven't
3956 * allocated blocks already inside this loop.
3958 error = xfs_trans_reserve_quota_nblks(NULL, ip, (long)alen, 0,
3959 XFS_QMOPT_RES_REGBLKS);
3964 * Split changing sb for alen and indlen since they could be coming
3965 * from different places.
3967 indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen);
3970 error = xfs_mod_fdblocks(mp, -((int64_t)alen), false);
3972 goto out_unreserve_quota;
3974 error = xfs_mod_fdblocks(mp, -((int64_t)indlen), false);
3976 goto out_unreserve_blocks;
3979 ip->i_delayed_blks += alen;
3980 xfs_mod_delalloc(ip->i_mount, alen + indlen);
3982 got->br_startoff = aoff;
3983 got->br_startblock = nullstartblock(indlen);
3984 got->br_blockcount = alen;
3985 got->br_state = XFS_EXT_NORM;
3987 xfs_bmap_add_extent_hole_delay(ip, whichfork, icur, got);
3990 * Tag the inode if blocks were preallocated. Note that COW fork
3991 * preallocation can occur at the start or end of the extent, even when
3992 * prealloc == 0, so we must also check the aligned offset and length.
3994 if (whichfork == XFS_DATA_FORK && prealloc)
3995 xfs_inode_set_eofblocks_tag(ip);
3996 if (whichfork == XFS_COW_FORK && (prealloc || aoff < off || alen > len))
3997 xfs_inode_set_cowblocks_tag(ip);
4001 out_unreserve_blocks:
4002 xfs_mod_fdblocks(mp, alen, false);
4003 out_unreserve_quota:
4004 if (XFS_IS_QUOTA_ON(mp))
4005 xfs_trans_unreserve_quota_nblks(NULL, ip, (long)alen, 0,
4006 XFS_QMOPT_RES_REGBLKS);
4012 struct xfs_bmalloca *bma)
4014 struct xfs_mount *mp = bma->ip->i_mount;
4015 int whichfork = xfs_bmapi_whichfork(bma->flags);
4016 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
4017 int tmp_logflags = 0;
4020 ASSERT(bma->length > 0);
4023 * For the wasdelay case, we could also just allocate the stuff asked
4024 * for in this bmap call but that wouldn't be as good.
4027 bma->length = (xfs_extlen_t)bma->got.br_blockcount;
4028 bma->offset = bma->got.br_startoff;
4029 xfs_iext_peek_prev_extent(ifp, &bma->icur, &bma->prev);
4031 bma->length = XFS_FILBLKS_MIN(bma->length, MAXEXTLEN);
4033 bma->length = XFS_FILBLKS_MIN(bma->length,
4034 bma->got.br_startoff - bma->offset);
4038 * Set the data type being allocated. For the data fork, the first data
4039 * in the file is treated differently to all other allocations. For the
4040 * attribute fork, we only need to ensure the allocated range is not on
4043 if (!(bma->flags & XFS_BMAPI_METADATA)) {
4044 bma->datatype = XFS_ALLOC_NOBUSY;
4045 if (whichfork == XFS_DATA_FORK) {
4046 if (bma->offset == 0)
4047 bma->datatype |= XFS_ALLOC_INITIAL_USER_DATA;
4049 bma->datatype |= XFS_ALLOC_USERDATA;
4051 if (bma->flags & XFS_BMAPI_ZERO)
4052 bma->datatype |= XFS_ALLOC_USERDATA_ZERO;
4055 bma->minlen = (bma->flags & XFS_BMAPI_CONTIG) ? bma->length : 1;
4058 * Only want to do the alignment at the eof if it is userdata and
4059 * allocation length is larger than a stripe unit.
4061 if (mp->m_dalign && bma->length >= mp->m_dalign &&
4062 !(bma->flags & XFS_BMAPI_METADATA) && whichfork == XFS_DATA_FORK) {
4063 error = xfs_bmap_isaeof(bma, whichfork);
4068 error = xfs_bmap_alloc(bma);
4072 if (bma->blkno == NULLFSBLOCK)
4074 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur)
4075 bma->cur = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork);
4077 * Bump the number of extents we've allocated
4083 bma->cur->bc_private.b.flags =
4084 bma->wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
4086 bma->got.br_startoff = bma->offset;
4087 bma->got.br_startblock = bma->blkno;
4088 bma->got.br_blockcount = bma->length;
4089 bma->got.br_state = XFS_EXT_NORM;
4092 * In the data fork, a wasdelay extent has been initialized, so
4093 * shouldn't be flagged as unwritten.
4095 * For the cow fork, however, we convert delalloc reservations
4096 * (extents allocated for speculative preallocation) to
4097 * allocated unwritten extents, and only convert the unwritten
4098 * extents to real extents when we're about to write the data.
4100 if ((!bma->wasdel || (bma->flags & XFS_BMAPI_COWFORK)) &&
4101 (bma->flags & XFS_BMAPI_PREALLOC))
4102 bma->got.br_state = XFS_EXT_UNWRITTEN;
4105 error = xfs_bmap_add_extent_delay_real(bma, whichfork);
4107 error = xfs_bmap_add_extent_hole_real(bma->tp, bma->ip,
4108 whichfork, &bma->icur, &bma->cur, &bma->got,
4109 &bma->logflags, bma->flags);
4111 bma->logflags |= tmp_logflags;
4116 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4117 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4118 * the neighbouring ones.
4120 xfs_iext_get_extent(ifp, &bma->icur, &bma->got);
4122 ASSERT(bma->got.br_startoff <= bma->offset);
4123 ASSERT(bma->got.br_startoff + bma->got.br_blockcount >=
4124 bma->offset + bma->length);
4125 ASSERT(bma->got.br_state == XFS_EXT_NORM ||
4126 bma->got.br_state == XFS_EXT_UNWRITTEN);
4131 xfs_bmapi_convert_unwritten(
4132 struct xfs_bmalloca *bma,
4133 struct xfs_bmbt_irec *mval,
4137 int whichfork = xfs_bmapi_whichfork(flags);
4138 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
4139 int tmp_logflags = 0;
4142 /* check if we need to do unwritten->real conversion */
4143 if (mval->br_state == XFS_EXT_UNWRITTEN &&
4144 (flags & XFS_BMAPI_PREALLOC))
4147 /* check if we need to do real->unwritten conversion */
4148 if (mval->br_state == XFS_EXT_NORM &&
4149 (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) !=
4150 (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT))
4154 * Modify (by adding) the state flag, if writing.
4156 ASSERT(mval->br_blockcount <= len);
4157 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4158 bma->cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp,
4159 bma->ip, whichfork);
4161 mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN)
4162 ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
4165 * Before insertion into the bmbt, zero the range being converted
4168 if (flags & XFS_BMAPI_ZERO) {
4169 error = xfs_zero_extent(bma->ip, mval->br_startblock,
4170 mval->br_blockcount);
4175 error = xfs_bmap_add_extent_unwritten_real(bma->tp, bma->ip, whichfork,
4176 &bma->icur, &bma->cur, mval, &tmp_logflags);
4178 * Log the inode core unconditionally in the unwritten extent conversion
4179 * path because the conversion might not have done so (e.g., if the
4180 * extent count hasn't changed). We need to make sure the inode is dirty
4181 * in the transaction for the sake of fsync(), even if nothing has
4182 * changed, because fsync() will not force the log for this transaction
4183 * unless it sees the inode pinned.
4185 * Note: If we're only converting cow fork extents, there aren't
4186 * any on-disk updates to make, so we don't need to log anything.
4188 if (whichfork != XFS_COW_FORK)
4189 bma->logflags |= tmp_logflags | XFS_ILOG_CORE;
4194 * Update our extent pointer, given that
4195 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4196 * of the neighbouring ones.
4198 xfs_iext_get_extent(ifp, &bma->icur, &bma->got);
4201 * We may have combined previously unwritten space with written space,
4202 * so generate another request.
4204 if (mval->br_blockcount < len)
4209 static inline xfs_extlen_t
4211 struct xfs_trans *tp,
4212 struct xfs_inode *ip,
4215 if (tp && tp->t_firstblock != NULLFSBLOCK)
4217 if (XFS_IFORK_FORMAT(ip, fork) != XFS_DINODE_FMT_BTREE)
4219 return be16_to_cpu(XFS_IFORK_PTR(ip, fork)->if_broot->bb_level) + 1;
4223 * Log whatever the flags say, even if error. Otherwise we might miss detecting
4224 * a case where the data is changed, there's an error, and it's not logged so we
4225 * don't shutdown when we should. Don't bother logging extents/btree changes if
4226 * we converted to the other format.
4230 struct xfs_bmalloca *bma,
4234 if ((bma->logflags & xfs_ilog_fext(whichfork)) &&
4235 XFS_IFORK_FORMAT(bma->ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
4236 bma->logflags &= ~xfs_ilog_fext(whichfork);
4237 else if ((bma->logflags & xfs_ilog_fbroot(whichfork)) &&
4238 XFS_IFORK_FORMAT(bma->ip, whichfork) != XFS_DINODE_FMT_BTREE)
4239 bma->logflags &= ~xfs_ilog_fbroot(whichfork);
4242 xfs_trans_log_inode(bma->tp, bma->ip, bma->logflags);
4244 xfs_btree_del_cursor(bma->cur, error);
4248 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4249 * extent state if necessary. Details behaviour is controlled by the flags
4250 * parameter. Only allocates blocks from a single allocation group, to avoid
4255 struct xfs_trans *tp, /* transaction pointer */
4256 struct xfs_inode *ip, /* incore inode */
4257 xfs_fileoff_t bno, /* starting file offs. mapped */
4258 xfs_filblks_t len, /* length to map in file */
4259 int flags, /* XFS_BMAPI_... */
4260 xfs_extlen_t total, /* total blocks needed */
4261 struct xfs_bmbt_irec *mval, /* output: map values */
4262 int *nmap) /* i/o: mval size/count */
4264 struct xfs_bmalloca bma = {
4269 struct xfs_mount *mp = ip->i_mount;
4270 struct xfs_ifork *ifp;
4271 xfs_fileoff_t end; /* end of mapped file region */
4272 bool eof = false; /* after the end of extents */
4273 int error; /* error return */
4274 int n; /* current extent index */
4275 xfs_fileoff_t obno; /* old block number (offset) */
4276 int whichfork; /* data or attr fork */
4279 xfs_fileoff_t orig_bno; /* original block number value */
4280 int orig_flags; /* original flags arg value */
4281 xfs_filblks_t orig_len; /* original value of len arg */
4282 struct xfs_bmbt_irec *orig_mval; /* original value of mval */
4283 int orig_nmap; /* original value of *nmap */
4291 whichfork = xfs_bmapi_whichfork(flags);
4294 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP);
4297 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL);
4298 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
4299 ASSERT(!(flags & XFS_BMAPI_REMAP));
4301 /* zeroing is for currently only for data extents, not metadata */
4302 ASSERT((flags & (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO)) !=
4303 (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO));
4305 * we can allocate unwritten extents or pre-zero allocated blocks,
4306 * but it makes no sense to do both at once. This would result in
4307 * zeroing the unwritten extent twice, but it still being an
4308 * unwritten extent....
4310 ASSERT((flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)) !=
4311 (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO));
4313 if (unlikely(XFS_TEST_ERROR(
4314 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4315 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
4316 mp, XFS_ERRTAG_BMAPIFORMAT))) {
4317 XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW, mp);
4318 return -EFSCORRUPTED;
4321 if (XFS_FORCED_SHUTDOWN(mp))
4324 ifp = XFS_IFORK_PTR(ip, whichfork);
4326 XFS_STATS_INC(mp, xs_blk_mapw);
4328 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4329 error = xfs_iread_extents(tp, ip, whichfork);
4334 if (!xfs_iext_lookup_extent(ip, ifp, bno, &bma.icur, &bma.got))
4336 if (!xfs_iext_peek_prev_extent(ifp, &bma.icur, &bma.prev))
4337 bma.prev.br_startoff = NULLFILEOFF;
4338 bma.minleft = xfs_bmapi_minleft(tp, ip, whichfork);
4343 while (bno < end && n < *nmap) {
4344 bool need_alloc = false, wasdelay = false;
4346 /* in hole or beyond EOF? */
4347 if (eof || bma.got.br_startoff > bno) {
4349 * CoW fork conversions should /never/ hit EOF or
4350 * holes. There should always be something for us
4353 ASSERT(!((flags & XFS_BMAPI_CONVERT) &&
4354 (flags & XFS_BMAPI_COWFORK)));
4357 } else if (isnullstartblock(bma.got.br_startblock)) {
4362 * First, deal with the hole before the allocated space
4363 * that we found, if any.
4365 if (need_alloc || wasdelay) {
4367 bma.conv = !!(flags & XFS_BMAPI_CONVERT);
4368 bma.wasdel = wasdelay;
4373 * There's a 32/64 bit type mismatch between the
4374 * allocation length request (which can be 64 bits in
4375 * length) and the bma length request, which is
4376 * xfs_extlen_t and therefore 32 bits. Hence we have to
4377 * check for 32-bit overflows and handle them here.
4379 if (len > (xfs_filblks_t)MAXEXTLEN)
4380 bma.length = MAXEXTLEN;
4385 ASSERT(bma.length > 0);
4386 error = xfs_bmapi_allocate(&bma);
4389 if (bma.blkno == NULLFSBLOCK)
4393 * If this is a CoW allocation, record the data in
4394 * the refcount btree for orphan recovery.
4396 if (whichfork == XFS_COW_FORK) {
4397 error = xfs_refcount_alloc_cow_extent(tp,
4398 bma.blkno, bma.length);
4404 /* Deal with the allocated space we found. */
4405 xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno,
4408 /* Execute unwritten extent conversion if necessary */
4409 error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags);
4410 if (error == -EAGAIN)
4415 /* update the extent map to return */
4416 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4419 * If we're done, stop now. Stop when we've allocated
4420 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4421 * the transaction may get too big.
4423 if (bno >= end || n >= *nmap || bma.nallocs >= *nmap)
4426 /* Else go on to the next record. */
4428 if (!xfs_iext_next_extent(ifp, &bma.icur, &bma.got))
4433 error = xfs_bmap_btree_to_extents(tp, ip, bma.cur, &bma.logflags,
4438 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE ||
4439 XFS_IFORK_NEXTENTS(ip, whichfork) >
4440 XFS_IFORK_MAXEXT(ip, whichfork));
4441 xfs_bmapi_finish(&bma, whichfork, 0);
4442 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
4446 xfs_bmapi_finish(&bma, whichfork, error);
4451 * Convert an existing delalloc extent to real blocks based on file offset. This
4452 * attempts to allocate the entire delalloc extent and may require multiple
4453 * invocations to allocate the target offset if a large enough physical extent
4457 xfs_bmapi_convert_delalloc(
4458 struct xfs_inode *ip,
4460 xfs_fileoff_t offset_fsb,
4461 struct xfs_bmbt_irec *imap,
4464 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
4465 struct xfs_mount *mp = ip->i_mount;
4466 struct xfs_bmalloca bma = { NULL };
4467 struct xfs_trans *tp;
4471 * Space for the extent and indirect blocks was reserved when the
4472 * delalloc extent was created so there's no need to do so here.
4474 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, 0, 0,
4475 XFS_TRANS_RESERVE, &tp);
4479 xfs_ilock(ip, XFS_ILOCK_EXCL);
4480 xfs_trans_ijoin(tp, ip, 0);
4482 if (!xfs_iext_lookup_extent(ip, ifp, offset_fsb, &bma.icur, &bma.got) ||
4483 bma.got.br_startoff > offset_fsb) {
4485 * No extent found in the range we are trying to convert. This
4486 * should only happen for the COW fork, where another thread
4487 * might have moved the extent to the data fork in the meantime.
4489 WARN_ON_ONCE(whichfork != XFS_COW_FORK);
4491 goto out_trans_cancel;
4495 * If we find a real extent here we raced with another thread converting
4496 * the extent. Just return the real extent at this offset.
4498 if (!isnullstartblock(bma.got.br_startblock)) {
4500 *seq = READ_ONCE(ifp->if_seq);
4501 goto out_trans_cancel;
4507 bma.offset = bma.got.br_startoff;
4508 bma.length = max_t(xfs_filblks_t, bma.got.br_blockcount, MAXEXTLEN);
4509 bma.total = XFS_EXTENTADD_SPACE_RES(ip->i_mount, XFS_DATA_FORK);
4510 bma.minleft = xfs_bmapi_minleft(tp, ip, whichfork);
4511 if (whichfork == XFS_COW_FORK)
4512 bma.flags = XFS_BMAPI_COWFORK | XFS_BMAPI_PREALLOC;
4514 if (!xfs_iext_peek_prev_extent(ifp, &bma.icur, &bma.prev))
4515 bma.prev.br_startoff = NULLFILEOFF;
4517 error = xfs_bmapi_allocate(&bma);
4522 if (WARN_ON_ONCE(bma.blkno == NULLFSBLOCK))
4524 error = -EFSCORRUPTED;
4525 if (WARN_ON_ONCE(!bma.got.br_startblock && !XFS_IS_REALTIME_INODE(ip)))
4528 XFS_STATS_ADD(mp, xs_xstrat_bytes, XFS_FSB_TO_B(mp, bma.length));
4529 XFS_STATS_INC(mp, xs_xstrat_quick);
4531 ASSERT(!isnullstartblock(bma.got.br_startblock));
4533 *seq = READ_ONCE(ifp->if_seq);
4535 if (whichfork == XFS_COW_FORK) {
4536 error = xfs_refcount_alloc_cow_extent(tp, bma.blkno,
4542 error = xfs_bmap_btree_to_extents(tp, ip, bma.cur, &bma.logflags,
4547 xfs_bmapi_finish(&bma, whichfork, 0);
4548 error = xfs_trans_commit(tp);
4549 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4553 xfs_bmapi_finish(&bma, whichfork, error);
4555 xfs_trans_cancel(tp);
4556 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4562 struct xfs_trans *tp,
4563 struct xfs_inode *ip,
4566 xfs_fsblock_t startblock,
4569 struct xfs_mount *mp = ip->i_mount;
4570 struct xfs_ifork *ifp;
4571 struct xfs_btree_cur *cur = NULL;
4572 struct xfs_bmbt_irec got;
4573 struct xfs_iext_cursor icur;
4574 int whichfork = xfs_bmapi_whichfork(flags);
4575 int logflags = 0, error;
4577 ifp = XFS_IFORK_PTR(ip, whichfork);
4579 ASSERT(len <= (xfs_filblks_t)MAXEXTLEN);
4580 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
4581 ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK | XFS_BMAPI_PREALLOC |
4582 XFS_BMAPI_NORMAP)));
4583 ASSERT((flags & (XFS_BMAPI_ATTRFORK | XFS_BMAPI_PREALLOC)) !=
4584 (XFS_BMAPI_ATTRFORK | XFS_BMAPI_PREALLOC));
4586 if (unlikely(XFS_TEST_ERROR(
4587 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4588 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
4589 mp, XFS_ERRTAG_BMAPIFORMAT))) {
4590 XFS_ERROR_REPORT("xfs_bmapi_remap", XFS_ERRLEVEL_LOW, mp);
4591 return -EFSCORRUPTED;
4594 if (XFS_FORCED_SHUTDOWN(mp))
4597 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4598 error = xfs_iread_extents(tp, ip, whichfork);
4603 if (xfs_iext_lookup_extent(ip, ifp, bno, &icur, &got)) {
4604 /* make sure we only reflink into a hole. */
4605 ASSERT(got.br_startoff > bno);
4606 ASSERT(got.br_startoff - bno >= len);
4609 ip->i_d.di_nblocks += len;
4610 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
4612 if (ifp->if_flags & XFS_IFBROOT) {
4613 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
4614 cur->bc_private.b.flags = 0;
4617 got.br_startoff = bno;
4618 got.br_startblock = startblock;
4619 got.br_blockcount = len;
4620 if (flags & XFS_BMAPI_PREALLOC)
4621 got.br_state = XFS_EXT_UNWRITTEN;
4623 got.br_state = XFS_EXT_NORM;
4625 error = xfs_bmap_add_extent_hole_real(tp, ip, whichfork, &icur,
4626 &cur, &got, &logflags, flags);
4630 error = xfs_bmap_btree_to_extents(tp, ip, cur, &logflags, whichfork);
4633 if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS)
4634 logflags &= ~XFS_ILOG_DEXT;
4635 else if (ip->i_d.di_format != XFS_DINODE_FMT_BTREE)
4636 logflags &= ~XFS_ILOG_DBROOT;
4639 xfs_trans_log_inode(tp, ip, logflags);
4641 xfs_btree_del_cursor(cur, error);
4646 * When a delalloc extent is split (e.g., due to a hole punch), the original
4647 * indlen reservation must be shared across the two new extents that are left
4650 * Given the original reservation and the worst case indlen for the two new
4651 * extents (as calculated by xfs_bmap_worst_indlen()), split the original
4652 * reservation fairly across the two new extents. If necessary, steal available
4653 * blocks from a deleted extent to make up a reservation deficiency (e.g., if
4654 * ores == 1). The number of stolen blocks is returned. The availability and
4655 * subsequent accounting of stolen blocks is the responsibility of the caller.
4657 static xfs_filblks_t
4658 xfs_bmap_split_indlen(
4659 xfs_filblks_t ores, /* original res. */
4660 xfs_filblks_t *indlen1, /* ext1 worst indlen */
4661 xfs_filblks_t *indlen2, /* ext2 worst indlen */
4662 xfs_filblks_t avail) /* stealable blocks */
4664 xfs_filblks_t len1 = *indlen1;
4665 xfs_filblks_t len2 = *indlen2;
4666 xfs_filblks_t nres = len1 + len2; /* new total res. */
4667 xfs_filblks_t stolen = 0;
4668 xfs_filblks_t resfactor;
4671 * Steal as many blocks as we can to try and satisfy the worst case
4672 * indlen for both new extents.
4674 if (ores < nres && avail)
4675 stolen = XFS_FILBLKS_MIN(nres - ores, avail);
4678 /* nothing else to do if we've satisfied the new reservation */
4683 * We can't meet the total required reservation for the two extents.
4684 * Calculate the percent of the overall shortage between both extents
4685 * and apply this percentage to each of the requested indlen values.
4686 * This distributes the shortage fairly and reduces the chances that one
4687 * of the two extents is left with nothing when extents are repeatedly
4690 resfactor = (ores * 100);
4691 do_div(resfactor, nres);
4696 ASSERT(len1 + len2 <= ores);
4697 ASSERT(len1 < *indlen1 && len2 < *indlen2);
4700 * Hand out the remainder to each extent. If one of the two reservations
4701 * is zero, we want to make sure that one gets a block first. The loop
4702 * below starts with len1, so hand len2 a block right off the bat if it
4705 ores -= (len1 + len2);
4706 ASSERT((*indlen1 - len1) + (*indlen2 - len2) >= ores);
4707 if (ores && !len2 && *indlen2) {
4712 if (len1 < *indlen1) {
4718 if (len2 < *indlen2) {
4731 xfs_bmap_del_extent_delay(
4732 struct xfs_inode *ip,
4734 struct xfs_iext_cursor *icur,
4735 struct xfs_bmbt_irec *got,
4736 struct xfs_bmbt_irec *del)
4738 struct xfs_mount *mp = ip->i_mount;
4739 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
4740 struct xfs_bmbt_irec new;
4741 int64_t da_old, da_new, da_diff = 0;
4742 xfs_fileoff_t del_endoff, got_endoff;
4743 xfs_filblks_t got_indlen, new_indlen, stolen;
4744 int state = xfs_bmap_fork_to_state(whichfork);
4748 XFS_STATS_INC(mp, xs_del_exlist);
4750 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
4751 del_endoff = del->br_startoff + del->br_blockcount;
4752 got_endoff = got->br_startoff + got->br_blockcount;
4753 da_old = startblockval(got->br_startblock);
4756 ASSERT(del->br_blockcount > 0);
4757 ASSERT(got->br_startoff <= del->br_startoff);
4758 ASSERT(got_endoff >= del_endoff);
4761 uint64_t rtexts = XFS_FSB_TO_B(mp, del->br_blockcount);
4763 do_div(rtexts, mp->m_sb.sb_rextsize);
4764 xfs_mod_frextents(mp, rtexts);
4768 * Update the inode delalloc counter now and wait to update the
4769 * sb counters as we might have to borrow some blocks for the
4770 * indirect block accounting.
4772 error = xfs_trans_reserve_quota_nblks(NULL, ip,
4773 -((long)del->br_blockcount), 0,
4774 isrt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
4777 ip->i_delayed_blks -= del->br_blockcount;
4779 if (got->br_startoff == del->br_startoff)
4780 state |= BMAP_LEFT_FILLING;
4781 if (got_endoff == del_endoff)
4782 state |= BMAP_RIGHT_FILLING;
4784 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
4785 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
4787 * Matches the whole extent. Delete the entry.
4789 xfs_iext_remove(ip, icur, state);
4790 xfs_iext_prev(ifp, icur);
4792 case BMAP_LEFT_FILLING:
4794 * Deleting the first part of the extent.
4796 got->br_startoff = del_endoff;
4797 got->br_blockcount -= del->br_blockcount;
4798 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
4799 got->br_blockcount), da_old);
4800 got->br_startblock = nullstartblock((int)da_new);
4801 xfs_iext_update_extent(ip, state, icur, got);
4803 case BMAP_RIGHT_FILLING:
4805 * Deleting the last part of the extent.
4807 got->br_blockcount = got->br_blockcount - del->br_blockcount;
4808 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
4809 got->br_blockcount), da_old);
4810 got->br_startblock = nullstartblock((int)da_new);
4811 xfs_iext_update_extent(ip, state, icur, got);
4815 * Deleting the middle of the extent.
4817 * Distribute the original indlen reservation across the two new
4818 * extents. Steal blocks from the deleted extent if necessary.
4819 * Stealing blocks simply fudges the fdblocks accounting below.
4820 * Warn if either of the new indlen reservations is zero as this
4821 * can lead to delalloc problems.
4823 got->br_blockcount = del->br_startoff - got->br_startoff;
4824 got_indlen = xfs_bmap_worst_indlen(ip, got->br_blockcount);
4826 new.br_blockcount = got_endoff - del_endoff;
4827 new_indlen = xfs_bmap_worst_indlen(ip, new.br_blockcount);
4829 WARN_ON_ONCE(!got_indlen || !new_indlen);
4830 stolen = xfs_bmap_split_indlen(da_old, &got_indlen, &new_indlen,
4831 del->br_blockcount);
4833 got->br_startblock = nullstartblock((int)got_indlen);
4835 new.br_startoff = del_endoff;
4836 new.br_state = got->br_state;
4837 new.br_startblock = nullstartblock((int)new_indlen);
4839 xfs_iext_update_extent(ip, state, icur, got);
4840 xfs_iext_next(ifp, icur);
4841 xfs_iext_insert(ip, icur, &new, state);
4843 da_new = got_indlen + new_indlen - stolen;
4844 del->br_blockcount -= stolen;
4848 ASSERT(da_old >= da_new);
4849 da_diff = da_old - da_new;
4851 da_diff += del->br_blockcount;
4853 xfs_mod_fdblocks(mp, da_diff, false);
4854 xfs_mod_delalloc(mp, -da_diff);
4860 xfs_bmap_del_extent_cow(
4861 struct xfs_inode *ip,
4862 struct xfs_iext_cursor *icur,
4863 struct xfs_bmbt_irec *got,
4864 struct xfs_bmbt_irec *del)
4866 struct xfs_mount *mp = ip->i_mount;
4867 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
4868 struct xfs_bmbt_irec new;
4869 xfs_fileoff_t del_endoff, got_endoff;
4870 int state = BMAP_COWFORK;
4872 XFS_STATS_INC(mp, xs_del_exlist);
4874 del_endoff = del->br_startoff + del->br_blockcount;
4875 got_endoff = got->br_startoff + got->br_blockcount;
4877 ASSERT(del->br_blockcount > 0);
4878 ASSERT(got->br_startoff <= del->br_startoff);
4879 ASSERT(got_endoff >= del_endoff);
4880 ASSERT(!isnullstartblock(got->br_startblock));
4882 if (got->br_startoff == del->br_startoff)
4883 state |= BMAP_LEFT_FILLING;
4884 if (got_endoff == del_endoff)
4885 state |= BMAP_RIGHT_FILLING;
4887 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
4888 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
4890 * Matches the whole extent. Delete the entry.
4892 xfs_iext_remove(ip, icur, state);
4893 xfs_iext_prev(ifp, icur);
4895 case BMAP_LEFT_FILLING:
4897 * Deleting the first part of the extent.
4899 got->br_startoff = del_endoff;
4900 got->br_blockcount -= del->br_blockcount;
4901 got->br_startblock = del->br_startblock + del->br_blockcount;
4902 xfs_iext_update_extent(ip, state, icur, got);
4904 case BMAP_RIGHT_FILLING:
4906 * Deleting the last part of the extent.
4908 got->br_blockcount -= del->br_blockcount;
4909 xfs_iext_update_extent(ip, state, icur, got);
4913 * Deleting the middle of the extent.
4915 got->br_blockcount = del->br_startoff - got->br_startoff;
4917 new.br_startoff = del_endoff;
4918 new.br_blockcount = got_endoff - del_endoff;
4919 new.br_state = got->br_state;
4920 new.br_startblock = del->br_startblock + del->br_blockcount;
4922 xfs_iext_update_extent(ip, state, icur, got);
4923 xfs_iext_next(ifp, icur);
4924 xfs_iext_insert(ip, icur, &new, state);
4927 ip->i_delayed_blks -= del->br_blockcount;
4931 * Called by xfs_bmapi to update file extent records and the btree
4932 * after removing space.
4934 STATIC int /* error */
4935 xfs_bmap_del_extent_real(
4936 xfs_inode_t *ip, /* incore inode pointer */
4937 xfs_trans_t *tp, /* current transaction pointer */
4938 struct xfs_iext_cursor *icur,
4939 xfs_btree_cur_t *cur, /* if null, not a btree */
4940 xfs_bmbt_irec_t *del, /* data to remove from extents */
4941 int *logflagsp, /* inode logging flags */
4942 int whichfork, /* data or attr fork */
4943 int bflags) /* bmapi flags */
4945 xfs_fsblock_t del_endblock=0; /* first block past del */
4946 xfs_fileoff_t del_endoff; /* first offset past del */
4947 int do_fx; /* free extent at end of routine */
4948 int error; /* error return value */
4949 int flags = 0;/* inode logging flags */
4950 struct xfs_bmbt_irec got; /* current extent entry */
4951 xfs_fileoff_t got_endoff; /* first offset past got */
4952 int i; /* temp state */
4953 struct xfs_ifork *ifp; /* inode fork pointer */
4954 xfs_mount_t *mp; /* mount structure */
4955 xfs_filblks_t nblks; /* quota/sb block count */
4956 xfs_bmbt_irec_t new; /* new record to be inserted */
4958 uint qfield; /* quota field to update */
4959 int state = xfs_bmap_fork_to_state(whichfork);
4960 struct xfs_bmbt_irec old;
4963 XFS_STATS_INC(mp, xs_del_exlist);
4965 ifp = XFS_IFORK_PTR(ip, whichfork);
4966 ASSERT(del->br_blockcount > 0);
4967 xfs_iext_get_extent(ifp, icur, &got);
4968 ASSERT(got.br_startoff <= del->br_startoff);
4969 del_endoff = del->br_startoff + del->br_blockcount;
4970 got_endoff = got.br_startoff + got.br_blockcount;
4971 ASSERT(got_endoff >= del_endoff);
4972 ASSERT(!isnullstartblock(got.br_startblock));
4977 * If it's the case where the directory code is running with no block
4978 * reservation, and the deleted block is in the middle of its extent,
4979 * and the resulting insert of an extent would cause transformation to
4980 * btree format, then reject it. The calling code will then swap blocks
4981 * around instead. We have to do this now, rather than waiting for the
4982 * conversion to btree format, since the transaction will be dirty then.
4984 if (tp->t_blk_res == 0 &&
4985 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
4986 XFS_IFORK_NEXTENTS(ip, whichfork) >=
4987 XFS_IFORK_MAXEXT(ip, whichfork) &&
4988 del->br_startoff > got.br_startoff && del_endoff < got_endoff)
4991 flags = XFS_ILOG_CORE;
4992 if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) {
4997 bno = div_u64_rem(del->br_startblock, mp->m_sb.sb_rextsize,
5000 len = div_u64_rem(del->br_blockcount, mp->m_sb.sb_rextsize,
5004 error = xfs_rtfree_extent(tp, bno, (xfs_extlen_t)len);
5008 nblks = len * mp->m_sb.sb_rextsize;
5009 qfield = XFS_TRANS_DQ_RTBCOUNT;
5012 nblks = del->br_blockcount;
5013 qfield = XFS_TRANS_DQ_BCOUNT;
5016 del_endblock = del->br_startblock + del->br_blockcount;
5018 error = xfs_bmbt_lookup_eq(cur, &got, &i);
5021 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
5024 if (got.br_startoff == del->br_startoff)
5025 state |= BMAP_LEFT_FILLING;
5026 if (got_endoff == del_endoff)
5027 state |= BMAP_RIGHT_FILLING;
5029 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
5030 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
5032 * Matches the whole extent. Delete the entry.
5034 xfs_iext_remove(ip, icur, state);
5035 xfs_iext_prev(ifp, icur);
5036 XFS_IFORK_NEXT_SET(ip, whichfork,
5037 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
5038 flags |= XFS_ILOG_CORE;
5040 flags |= xfs_ilog_fext(whichfork);
5043 if ((error = xfs_btree_delete(cur, &i)))
5045 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
5047 case BMAP_LEFT_FILLING:
5049 * Deleting the first part of the extent.
5051 got.br_startoff = del_endoff;
5052 got.br_startblock = del_endblock;
5053 got.br_blockcount -= del->br_blockcount;
5054 xfs_iext_update_extent(ip, state, icur, &got);
5056 flags |= xfs_ilog_fext(whichfork);
5059 error = xfs_bmbt_update(cur, &got);
5063 case BMAP_RIGHT_FILLING:
5065 * Deleting the last part of the extent.
5067 got.br_blockcount -= del->br_blockcount;
5068 xfs_iext_update_extent(ip, state, icur, &got);
5070 flags |= xfs_ilog_fext(whichfork);
5073 error = xfs_bmbt_update(cur, &got);
5079 * Deleting the middle of the extent.
5083 got.br_blockcount = del->br_startoff - got.br_startoff;
5084 xfs_iext_update_extent(ip, state, icur, &got);
5086 new.br_startoff = del_endoff;
5087 new.br_blockcount = got_endoff - del_endoff;
5088 new.br_state = got.br_state;
5089 new.br_startblock = del_endblock;
5091 flags |= XFS_ILOG_CORE;
5093 error = xfs_bmbt_update(cur, &got);
5096 error = xfs_btree_increment(cur, 0, &i);
5099 cur->bc_rec.b = new;
5100 error = xfs_btree_insert(cur, &i);
5101 if (error && error != -ENOSPC)
5104 * If get no-space back from btree insert, it tried a
5105 * split, and we have a zero block reservation. Fix up
5106 * our state and return the error.
5108 if (error == -ENOSPC) {
5110 * Reset the cursor, don't trust it after any
5113 error = xfs_bmbt_lookup_eq(cur, &got, &i);
5116 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
5118 * Update the btree record back
5119 * to the original value.
5121 error = xfs_bmbt_update(cur, &old);
5125 * Reset the extent record back
5126 * to the original value.
5128 xfs_iext_update_extent(ip, state, icur, &old);
5133 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
5135 flags |= xfs_ilog_fext(whichfork);
5136 XFS_IFORK_NEXT_SET(ip, whichfork,
5137 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
5138 xfs_iext_next(ifp, icur);
5139 xfs_iext_insert(ip, icur, &new, state);
5143 /* remove reverse mapping */
5144 error = xfs_rmap_unmap_extent(tp, ip, whichfork, del);
5149 * If we need to, add to list of extents to delete.
5151 if (do_fx && !(bflags & XFS_BMAPI_REMAP)) {
5152 if (xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK) {
5153 error = xfs_refcount_decrease_extent(tp, del);
5157 __xfs_bmap_add_free(tp, del->br_startblock,
5158 del->br_blockcount, NULL,
5159 (bflags & XFS_BMAPI_NODISCARD) ||
5160 del->br_state == XFS_EXT_UNWRITTEN);
5165 * Adjust inode # blocks in the file.
5168 ip->i_d.di_nblocks -= nblks;
5170 * Adjust quota data.
5172 if (qfield && !(bflags & XFS_BMAPI_REMAP))
5173 xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks);
5181 * Unmap (remove) blocks from a file.
5182 * If nexts is nonzero then the number of extents to remove is limited to
5183 * that value. If not all extents in the block range can be removed then
5188 struct xfs_trans *tp, /* transaction pointer */
5189 struct xfs_inode *ip, /* incore inode */
5190 xfs_fileoff_t start, /* first file offset deleted */
5191 xfs_filblks_t *rlen, /* i/o: amount remaining */
5192 int flags, /* misc flags */
5193 xfs_extnum_t nexts) /* number of extents max */
5195 struct xfs_btree_cur *cur; /* bmap btree cursor */
5196 struct xfs_bmbt_irec del; /* extent being deleted */
5197 int error; /* error return value */
5198 xfs_extnum_t extno; /* extent number in list */
5199 struct xfs_bmbt_irec got; /* current extent record */
5200 struct xfs_ifork *ifp; /* inode fork pointer */
5201 int isrt; /* freeing in rt area */
5202 int logflags; /* transaction logging flags */
5203 xfs_extlen_t mod; /* rt extent offset */
5204 struct xfs_mount *mp; /* mount structure */
5205 int tmp_logflags; /* partial logging flags */
5206 int wasdel; /* was a delayed alloc extent */
5207 int whichfork; /* data or attribute fork */
5209 xfs_filblks_t len = *rlen; /* length to unmap in file */
5210 xfs_fileoff_t max_len;
5211 xfs_agnumber_t prev_agno = NULLAGNUMBER, agno;
5213 struct xfs_iext_cursor icur;
5216 trace_xfs_bunmap(ip, start, len, flags, _RET_IP_);
5218 whichfork = xfs_bmapi_whichfork(flags);
5219 ASSERT(whichfork != XFS_COW_FORK);
5220 ifp = XFS_IFORK_PTR(ip, whichfork);
5222 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5223 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
5224 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW,
5226 return -EFSCORRUPTED;
5229 if (XFS_FORCED_SHUTDOWN(mp))
5232 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
5237 * Guesstimate how many blocks we can unmap without running the risk of
5238 * blowing out the transaction with a mix of EFIs and reflink
5241 if (tp && xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK)
5242 max_len = min(len, xfs_refcount_max_unmap(tp->t_log_res));
5246 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5247 (error = xfs_iread_extents(tp, ip, whichfork)))
5249 if (xfs_iext_count(ifp) == 0) {
5253 XFS_STATS_INC(mp, xs_blk_unmap);
5254 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
5257 if (!xfs_iext_lookup_extent_before(ip, ifp, &end, &icur, &got)) {
5264 if (ifp->if_flags & XFS_IFBROOT) {
5265 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
5266 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5267 cur->bc_private.b.flags = 0;
5273 * Synchronize by locking the bitmap inode.
5275 xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL|XFS_ILOCK_RTBITMAP);
5276 xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL);
5277 xfs_ilock(mp->m_rsumip, XFS_ILOCK_EXCL|XFS_ILOCK_RTSUM);
5278 xfs_trans_ijoin(tp, mp->m_rsumip, XFS_ILOCK_EXCL);
5282 while (end != (xfs_fileoff_t)-1 && end >= start &&
5283 (nexts == 0 || extno < nexts) && max_len > 0) {
5285 * Is the found extent after a hole in which end lives?
5286 * Just back up to the previous extent, if so.
5288 if (got.br_startoff > end &&
5289 !xfs_iext_prev_extent(ifp, &icur, &got)) {
5294 * Is the last block of this extent before the range
5295 * we're supposed to delete? If so, we're done.
5297 end = XFS_FILEOFF_MIN(end,
5298 got.br_startoff + got.br_blockcount - 1);
5302 * Then deal with the (possibly delayed) allocated space
5306 wasdel = isnullstartblock(del.br_startblock);
5309 * Make sure we don't touch multiple AGF headers out of order
5310 * in a single transaction, as that could cause AB-BA deadlocks.
5313 agno = XFS_FSB_TO_AGNO(mp, del.br_startblock);
5314 if (prev_agno != NULLAGNUMBER && prev_agno > agno)
5318 if (got.br_startoff < start) {
5319 del.br_startoff = start;
5320 del.br_blockcount -= start - got.br_startoff;
5322 del.br_startblock += start - got.br_startoff;
5324 if (del.br_startoff + del.br_blockcount > end + 1)
5325 del.br_blockcount = end + 1 - del.br_startoff;
5327 /* How much can we safely unmap? */
5328 if (max_len < del.br_blockcount) {
5329 del.br_startoff += del.br_blockcount - max_len;
5331 del.br_startblock += del.br_blockcount - max_len;
5332 del.br_blockcount = max_len;
5338 sum = del.br_startblock + del.br_blockcount;
5339 div_u64_rem(sum, mp->m_sb.sb_rextsize, &mod);
5342 * Realtime extent not lined up at the end.
5343 * The extent could have been split into written
5344 * and unwritten pieces, or we could just be
5345 * unmapping part of it. But we can't really
5346 * get rid of part of a realtime extent.
5348 if (del.br_state == XFS_EXT_UNWRITTEN) {
5350 * This piece is unwritten, or we're not
5351 * using unwritten extents. Skip over it.
5354 end -= mod > del.br_blockcount ?
5355 del.br_blockcount : mod;
5356 if (end < got.br_startoff &&
5357 !xfs_iext_prev_extent(ifp, &icur, &got)) {
5364 * It's written, turn it unwritten.
5365 * This is better than zeroing it.
5367 ASSERT(del.br_state == XFS_EXT_NORM);
5368 ASSERT(tp->t_blk_res > 0);
5370 * If this spans a realtime extent boundary,
5371 * chop it back to the start of the one we end at.
5373 if (del.br_blockcount > mod) {
5374 del.br_startoff += del.br_blockcount - mod;
5375 del.br_startblock += del.br_blockcount - mod;
5376 del.br_blockcount = mod;
5378 del.br_state = XFS_EXT_UNWRITTEN;
5379 error = xfs_bmap_add_extent_unwritten_real(tp, ip,
5380 whichfork, &icur, &cur, &del,
5386 div_u64_rem(del.br_startblock, mp->m_sb.sb_rextsize, &mod);
5389 * Realtime extent is lined up at the end but not
5390 * at the front. We'll get rid of full extents if
5393 mod = mp->m_sb.sb_rextsize - mod;
5394 if (del.br_blockcount > mod) {
5395 del.br_blockcount -= mod;
5396 del.br_startoff += mod;
5397 del.br_startblock += mod;
5398 } else if (del.br_startoff == start &&
5399 (del.br_state == XFS_EXT_UNWRITTEN ||
5400 tp->t_blk_res == 0)) {
5402 * Can't make it unwritten. There isn't
5403 * a full extent here so just skip it.
5405 ASSERT(end >= del.br_blockcount);
5406 end -= del.br_blockcount;
5407 if (got.br_startoff > end &&
5408 !xfs_iext_prev_extent(ifp, &icur, &got)) {
5413 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
5414 struct xfs_bmbt_irec prev;
5417 * This one is already unwritten.
5418 * It must have a written left neighbor.
5419 * Unwrite the killed part of that one and
5422 if (!xfs_iext_prev_extent(ifp, &icur, &prev))
5424 ASSERT(prev.br_state == XFS_EXT_NORM);
5425 ASSERT(!isnullstartblock(prev.br_startblock));
5426 ASSERT(del.br_startblock ==
5427 prev.br_startblock + prev.br_blockcount);
5428 if (prev.br_startoff < start) {
5429 mod = start - prev.br_startoff;
5430 prev.br_blockcount -= mod;
5431 prev.br_startblock += mod;
5432 prev.br_startoff = start;
5434 prev.br_state = XFS_EXT_UNWRITTEN;
5435 error = xfs_bmap_add_extent_unwritten_real(tp,
5436 ip, whichfork, &icur, &cur,
5442 ASSERT(del.br_state == XFS_EXT_NORM);
5443 del.br_state = XFS_EXT_UNWRITTEN;
5444 error = xfs_bmap_add_extent_unwritten_real(tp,
5445 ip, whichfork, &icur, &cur,
5455 error = xfs_bmap_del_extent_delay(ip, whichfork, &icur,
5458 error = xfs_bmap_del_extent_real(ip, tp, &icur, cur,
5459 &del, &tmp_logflags, whichfork,
5461 logflags |= tmp_logflags;
5467 max_len -= del.br_blockcount;
5468 end = del.br_startoff - 1;
5471 * If not done go on to the next (previous) record.
5473 if (end != (xfs_fileoff_t)-1 && end >= start) {
5474 if (!xfs_iext_get_extent(ifp, &icur, &got) ||
5475 (got.br_startoff > end &&
5476 !xfs_iext_prev_extent(ifp, &icur, &got))) {
5483 if (done || end == (xfs_fileoff_t)-1 || end < start)
5486 *rlen = end - start + 1;
5489 * Convert to a btree if necessary.
5491 if (xfs_bmap_needs_btree(ip, whichfork)) {
5492 ASSERT(cur == NULL);
5493 error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0,
5494 &tmp_logflags, whichfork);
5495 logflags |= tmp_logflags;
5497 error = xfs_bmap_btree_to_extents(tp, ip, cur, &logflags,
5503 * Log everything. Do this after conversion, there's no point in
5504 * logging the extent records if we've converted to btree format.
5506 if ((logflags & xfs_ilog_fext(whichfork)) &&
5507 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
5508 logflags &= ~xfs_ilog_fext(whichfork);
5509 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
5510 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
5511 logflags &= ~xfs_ilog_fbroot(whichfork);
5513 * Log inode even in the error case, if the transaction
5514 * is dirty we'll need to shut down the filesystem.
5517 xfs_trans_log_inode(tp, ip, logflags);
5520 cur->bc_private.b.allocated = 0;
5521 xfs_btree_del_cursor(cur, error);
5526 /* Unmap a range of a file. */
5530 struct xfs_inode *ip,
5539 error = __xfs_bunmapi(tp, ip, bno, &len, flags, nexts);
5545 * Determine whether an extent shift can be accomplished by a merge with the
5546 * extent that precedes the target hole of the shift.
5550 struct xfs_bmbt_irec *left, /* preceding extent */
5551 struct xfs_bmbt_irec *got, /* current extent to shift */
5552 xfs_fileoff_t shift) /* shift fsb */
5554 xfs_fileoff_t startoff;
5556 startoff = got->br_startoff - shift;
5559 * The extent, once shifted, must be adjacent in-file and on-disk with
5560 * the preceding extent.
5562 if ((left->br_startoff + left->br_blockcount != startoff) ||
5563 (left->br_startblock + left->br_blockcount != got->br_startblock) ||
5564 (left->br_state != got->br_state) ||
5565 (left->br_blockcount + got->br_blockcount > MAXEXTLEN))
5572 * A bmap extent shift adjusts the file offset of an extent to fill a preceding
5573 * hole in the file. If an extent shift would result in the extent being fully
5574 * adjacent to the extent that currently precedes the hole, we can merge with
5575 * the preceding extent rather than do the shift.
5577 * This function assumes the caller has verified a shift-by-merge is possible
5578 * with the provided extents via xfs_bmse_can_merge().
5582 struct xfs_trans *tp,
5583 struct xfs_inode *ip,
5585 xfs_fileoff_t shift, /* shift fsb */
5586 struct xfs_iext_cursor *icur,
5587 struct xfs_bmbt_irec *got, /* extent to shift */
5588 struct xfs_bmbt_irec *left, /* preceding extent */
5589 struct xfs_btree_cur *cur,
5590 int *logflags) /* output */
5592 struct xfs_bmbt_irec new;
5593 xfs_filblks_t blockcount;
5595 struct xfs_mount *mp = ip->i_mount;
5597 blockcount = left->br_blockcount + got->br_blockcount;
5599 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
5600 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
5601 ASSERT(xfs_bmse_can_merge(left, got, shift));
5604 new.br_blockcount = blockcount;
5607 * Update the on-disk extent count, the btree if necessary and log the
5610 XFS_IFORK_NEXT_SET(ip, whichfork,
5611 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
5612 *logflags |= XFS_ILOG_CORE;
5614 *logflags |= XFS_ILOG_DEXT;
5618 /* lookup and remove the extent to merge */
5619 error = xfs_bmbt_lookup_eq(cur, got, &i);
5622 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
5624 error = xfs_btree_delete(cur, &i);
5627 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
5629 /* lookup and update size of the previous extent */
5630 error = xfs_bmbt_lookup_eq(cur, left, &i);
5633 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
5635 error = xfs_bmbt_update(cur, &new);
5640 xfs_iext_remove(ip, icur, 0);
5641 xfs_iext_prev(XFS_IFORK_PTR(ip, whichfork), icur);
5642 xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), icur,
5645 /* update reverse mapping. rmap functions merge the rmaps for us */
5646 error = xfs_rmap_unmap_extent(tp, ip, whichfork, got);
5649 memcpy(&new, got, sizeof(new));
5650 new.br_startoff = left->br_startoff + left->br_blockcount;
5651 return xfs_rmap_map_extent(tp, ip, whichfork, &new);
5655 xfs_bmap_shift_update_extent(
5656 struct xfs_trans *tp,
5657 struct xfs_inode *ip,
5659 struct xfs_iext_cursor *icur,
5660 struct xfs_bmbt_irec *got,
5661 struct xfs_btree_cur *cur,
5663 xfs_fileoff_t startoff)
5665 struct xfs_mount *mp = ip->i_mount;
5666 struct xfs_bmbt_irec prev = *got;
5669 *logflags |= XFS_ILOG_CORE;
5671 got->br_startoff = startoff;
5674 error = xfs_bmbt_lookup_eq(cur, &prev, &i);
5677 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
5679 error = xfs_bmbt_update(cur, got);
5683 *logflags |= XFS_ILOG_DEXT;
5686 xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), icur,
5689 /* update reverse mapping */
5690 error = xfs_rmap_unmap_extent(tp, ip, whichfork, &prev);
5693 return xfs_rmap_map_extent(tp, ip, whichfork, got);
5697 xfs_bmap_collapse_extents(
5698 struct xfs_trans *tp,
5699 struct xfs_inode *ip,
5700 xfs_fileoff_t *next_fsb,
5701 xfs_fileoff_t offset_shift_fsb,
5704 int whichfork = XFS_DATA_FORK;
5705 struct xfs_mount *mp = ip->i_mount;
5706 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
5707 struct xfs_btree_cur *cur = NULL;
5708 struct xfs_bmbt_irec got, prev;
5709 struct xfs_iext_cursor icur;
5710 xfs_fileoff_t new_startoff;
5714 if (unlikely(XFS_TEST_ERROR(
5715 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5716 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
5717 mp, XFS_ERRTAG_BMAPIFORMAT))) {
5718 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp);
5719 return -EFSCORRUPTED;
5722 if (XFS_FORCED_SHUTDOWN(mp))
5725 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL));
5727 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
5728 error = xfs_iread_extents(tp, ip, whichfork);
5733 if (ifp->if_flags & XFS_IFBROOT) {
5734 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5735 cur->bc_private.b.flags = 0;
5738 if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &icur, &got)) {
5742 XFS_WANT_CORRUPTED_GOTO(mp, !isnullstartblock(got.br_startblock),
5745 new_startoff = got.br_startoff - offset_shift_fsb;
5746 if (xfs_iext_peek_prev_extent(ifp, &icur, &prev)) {
5747 if (new_startoff < prev.br_startoff + prev.br_blockcount) {
5752 if (xfs_bmse_can_merge(&prev, &got, offset_shift_fsb)) {
5753 error = xfs_bmse_merge(tp, ip, whichfork,
5754 offset_shift_fsb, &icur, &got, &prev,
5761 if (got.br_startoff < offset_shift_fsb) {
5767 error = xfs_bmap_shift_update_extent(tp, ip, whichfork, &icur, &got,
5768 cur, &logflags, new_startoff);
5773 if (!xfs_iext_next_extent(ifp, &icur, &got)) {
5778 *next_fsb = got.br_startoff;
5781 xfs_btree_del_cursor(cur, error);
5783 xfs_trans_log_inode(tp, ip, logflags);
5787 /* Make sure we won't be right-shifting an extent past the maximum bound. */
5789 xfs_bmap_can_insert_extents(
5790 struct xfs_inode *ip,
5792 xfs_fileoff_t shift)
5794 struct xfs_bmbt_irec got;
5798 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
5800 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
5803 xfs_ilock(ip, XFS_ILOCK_EXCL);
5804 error = xfs_bmap_last_extent(NULL, ip, XFS_DATA_FORK, &got, &is_empty);
5805 if (!error && !is_empty && got.br_startoff >= off &&
5806 ((got.br_startoff + shift) & BMBT_STARTOFF_MASK) < got.br_startoff)
5808 xfs_iunlock(ip, XFS_ILOCK_EXCL);
5814 xfs_bmap_insert_extents(
5815 struct xfs_trans *tp,
5816 struct xfs_inode *ip,
5817 xfs_fileoff_t *next_fsb,
5818 xfs_fileoff_t offset_shift_fsb,
5820 xfs_fileoff_t stop_fsb)
5822 int whichfork = XFS_DATA_FORK;
5823 struct xfs_mount *mp = ip->i_mount;
5824 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
5825 struct xfs_btree_cur *cur = NULL;
5826 struct xfs_bmbt_irec got, next;
5827 struct xfs_iext_cursor icur;
5828 xfs_fileoff_t new_startoff;
5832 if (unlikely(XFS_TEST_ERROR(
5833 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5834 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
5835 mp, XFS_ERRTAG_BMAPIFORMAT))) {
5836 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp);
5837 return -EFSCORRUPTED;
5840 if (XFS_FORCED_SHUTDOWN(mp))
5843 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL));
5845 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
5846 error = xfs_iread_extents(tp, ip, whichfork);
5851 if (ifp->if_flags & XFS_IFBROOT) {
5852 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5853 cur->bc_private.b.flags = 0;
5856 if (*next_fsb == NULLFSBLOCK) {
5857 xfs_iext_last(ifp, &icur);
5858 if (!xfs_iext_get_extent(ifp, &icur, &got) ||
5859 stop_fsb > got.br_startoff) {
5864 if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &icur, &got)) {
5869 XFS_WANT_CORRUPTED_GOTO(mp, !isnullstartblock(got.br_startblock),
5872 if (stop_fsb >= got.br_startoff + got.br_blockcount) {
5877 new_startoff = got.br_startoff + offset_shift_fsb;
5878 if (xfs_iext_peek_next_extent(ifp, &icur, &next)) {
5879 if (new_startoff + got.br_blockcount > next.br_startoff) {
5885 * Unlike a left shift (which involves a hole punch), a right
5886 * shift does not modify extent neighbors in any way. We should
5887 * never find mergeable extents in this scenario. Check anyways
5888 * and warn if we encounter two extents that could be one.
5890 if (xfs_bmse_can_merge(&got, &next, offset_shift_fsb))
5894 error = xfs_bmap_shift_update_extent(tp, ip, whichfork, &icur, &got,
5895 cur, &logflags, new_startoff);
5899 if (!xfs_iext_prev_extent(ifp, &icur, &got) ||
5900 stop_fsb >= got.br_startoff + got.br_blockcount) {
5905 *next_fsb = got.br_startoff;
5908 xfs_btree_del_cursor(cur, error);
5910 xfs_trans_log_inode(tp, ip, logflags);
5915 * Splits an extent into two extents at split_fsb block such that it is the
5916 * first block of the current_ext. @ext is a target extent to be split.
5917 * @split_fsb is a block where the extents is split. If split_fsb lies in a
5918 * hole or the first block of extents, just return 0.
5921 xfs_bmap_split_extent_at(
5922 struct xfs_trans *tp,
5923 struct xfs_inode *ip,
5924 xfs_fileoff_t split_fsb)
5926 int whichfork = XFS_DATA_FORK;
5927 struct xfs_btree_cur *cur = NULL;
5928 struct xfs_bmbt_irec got;
5929 struct xfs_bmbt_irec new; /* split extent */
5930 struct xfs_mount *mp = ip->i_mount;
5931 struct xfs_ifork *ifp;
5932 xfs_fsblock_t gotblkcnt; /* new block count for got */
5933 struct xfs_iext_cursor icur;
5938 if (unlikely(XFS_TEST_ERROR(
5939 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5940 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
5941 mp, XFS_ERRTAG_BMAPIFORMAT))) {
5942 XFS_ERROR_REPORT("xfs_bmap_split_extent_at",
5943 XFS_ERRLEVEL_LOW, mp);
5944 return -EFSCORRUPTED;
5947 if (XFS_FORCED_SHUTDOWN(mp))
5950 ifp = XFS_IFORK_PTR(ip, whichfork);
5951 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
5952 /* Read in all the extents */
5953 error = xfs_iread_extents(tp, ip, whichfork);
5959 * If there are not extents, or split_fsb lies in a hole we are done.
5961 if (!xfs_iext_lookup_extent(ip, ifp, split_fsb, &icur, &got) ||
5962 got.br_startoff >= split_fsb)
5965 gotblkcnt = split_fsb - got.br_startoff;
5966 new.br_startoff = split_fsb;
5967 new.br_startblock = got.br_startblock + gotblkcnt;
5968 new.br_blockcount = got.br_blockcount - gotblkcnt;
5969 new.br_state = got.br_state;
5971 if (ifp->if_flags & XFS_IFBROOT) {
5972 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5973 cur->bc_private.b.flags = 0;
5974 error = xfs_bmbt_lookup_eq(cur, &got, &i);
5977 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor);
5980 got.br_blockcount = gotblkcnt;
5981 xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), &icur,
5984 logflags = XFS_ILOG_CORE;
5986 error = xfs_bmbt_update(cur, &got);
5990 logflags |= XFS_ILOG_DEXT;
5992 /* Add new extent */
5993 xfs_iext_next(ifp, &icur);
5994 xfs_iext_insert(ip, &icur, &new, 0);
5995 XFS_IFORK_NEXT_SET(ip, whichfork,
5996 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
5999 error = xfs_bmbt_lookup_eq(cur, &new, &i);
6002 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, del_cursor);
6003 error = xfs_btree_insert(cur, &i);
6006 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor);
6010 * Convert to a btree if necessary.
6012 if (xfs_bmap_needs_btree(ip, whichfork)) {
6013 int tmp_logflags; /* partial log flag return val */
6015 ASSERT(cur == NULL);
6016 error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0,
6017 &tmp_logflags, whichfork);
6018 logflags |= tmp_logflags;
6023 cur->bc_private.b.allocated = 0;
6024 xfs_btree_del_cursor(cur, error);
6028 xfs_trans_log_inode(tp, ip, logflags);
6033 xfs_bmap_split_extent(
6034 struct xfs_inode *ip,
6035 xfs_fileoff_t split_fsb)
6037 struct xfs_mount *mp = ip->i_mount;
6038 struct xfs_trans *tp;
6041 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write,
6042 XFS_DIOSTRAT_SPACE_RES(mp, 0), 0, 0, &tp);
6046 xfs_ilock(ip, XFS_ILOCK_EXCL);
6047 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
6049 error = xfs_bmap_split_extent_at(tp, ip, split_fsb);
6053 return xfs_trans_commit(tp);
6056 xfs_trans_cancel(tp);
6060 /* Deferred mapping is only for real extents in the data fork. */
6062 xfs_bmap_is_update_needed(
6063 struct xfs_bmbt_irec *bmap)
6065 return bmap->br_startblock != HOLESTARTBLOCK &&
6066 bmap->br_startblock != DELAYSTARTBLOCK;
6069 /* Record a bmap intent. */
6072 struct xfs_trans *tp,
6073 enum xfs_bmap_intent_type type,
6074 struct xfs_inode *ip,
6076 struct xfs_bmbt_irec *bmap)
6078 struct xfs_bmap_intent *bi;
6080 trace_xfs_bmap_defer(tp->t_mountp,
6081 XFS_FSB_TO_AGNO(tp->t_mountp, bmap->br_startblock),
6083 XFS_FSB_TO_AGBNO(tp->t_mountp, bmap->br_startblock),
6084 ip->i_ino, whichfork,
6086 bmap->br_blockcount,
6089 bi = kmem_alloc(sizeof(struct xfs_bmap_intent), KM_SLEEP | KM_NOFS);
6090 INIT_LIST_HEAD(&bi->bi_list);
6093 bi->bi_whichfork = whichfork;
6094 bi->bi_bmap = *bmap;
6096 xfs_defer_add(tp, XFS_DEFER_OPS_TYPE_BMAP, &bi->bi_list);
6100 /* Map an extent into a file. */
6102 xfs_bmap_map_extent(
6103 struct xfs_trans *tp,
6104 struct xfs_inode *ip,
6105 struct xfs_bmbt_irec *PREV)
6107 if (!xfs_bmap_is_update_needed(PREV))
6110 return __xfs_bmap_add(tp, XFS_BMAP_MAP, ip, XFS_DATA_FORK, PREV);
6113 /* Unmap an extent out of a file. */
6115 xfs_bmap_unmap_extent(
6116 struct xfs_trans *tp,
6117 struct xfs_inode *ip,
6118 struct xfs_bmbt_irec *PREV)
6120 if (!xfs_bmap_is_update_needed(PREV))
6123 return __xfs_bmap_add(tp, XFS_BMAP_UNMAP, ip, XFS_DATA_FORK, PREV);
6127 * Process one of the deferred bmap operations. We pass back the
6128 * btree cursor to maintain our lock on the bmapbt between calls.
6131 xfs_bmap_finish_one(
6132 struct xfs_trans *tp,
6133 struct xfs_inode *ip,
6134 enum xfs_bmap_intent_type type,
6136 xfs_fileoff_t startoff,
6137 xfs_fsblock_t startblock,
6138 xfs_filblks_t *blockcount,
6143 ASSERT(tp->t_firstblock == NULLFSBLOCK);
6145 trace_xfs_bmap_deferred(tp->t_mountp,
6146 XFS_FSB_TO_AGNO(tp->t_mountp, startblock), type,
6147 XFS_FSB_TO_AGBNO(tp->t_mountp, startblock),
6148 ip->i_ino, whichfork, startoff, *blockcount, state);
6150 if (WARN_ON_ONCE(whichfork != XFS_DATA_FORK))
6151 return -EFSCORRUPTED;
6153 if (XFS_TEST_ERROR(false, tp->t_mountp,
6154 XFS_ERRTAG_BMAP_FINISH_ONE))
6159 error = xfs_bmapi_remap(tp, ip, startoff, *blockcount,
6163 case XFS_BMAP_UNMAP:
6164 error = __xfs_bunmapi(tp, ip, startoff, blockcount,
6165 XFS_BMAPI_REMAP, 1);
6169 error = -EFSCORRUPTED;
6175 /* Check that an inode's extent does not have invalid flags or bad ranges. */
6177 xfs_bmap_validate_extent(
6178 struct xfs_inode *ip,
6180 struct xfs_bmbt_irec *irec)
6182 struct xfs_mount *mp = ip->i_mount;
6183 xfs_fsblock_t endfsb;
6186 isrt = XFS_IS_REALTIME_INODE(ip);
6187 endfsb = irec->br_startblock + irec->br_blockcount - 1;
6189 if (!xfs_verify_rtbno(mp, irec->br_startblock))
6190 return __this_address;
6191 if (!xfs_verify_rtbno(mp, endfsb))
6192 return __this_address;
6194 if (!xfs_verify_fsbno(mp, irec->br_startblock))
6195 return __this_address;
6196 if (!xfs_verify_fsbno(mp, endfsb))
6197 return __this_address;
6198 if (XFS_FSB_TO_AGNO(mp, irec->br_startblock) !=
6199 XFS_FSB_TO_AGNO(mp, endfsb))
6200 return __this_address;
6202 if (irec->br_state != XFS_EXT_NORM && whichfork != XFS_DATA_FORK)
6203 return __this_address;