2 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 #include "xfs_shared.h"
21 #include "xfs_format.h"
22 #include "xfs_log_format.h"
23 #include "xfs_trans_resv.h"
26 #include "xfs_mount.h"
27 #include "xfs_defer.h"
28 #include "xfs_da_format.h"
29 #include "xfs_da_btree.h"
31 #include "xfs_inode.h"
32 #include "xfs_btree.h"
33 #include "xfs_trans.h"
34 #include "xfs_inode_item.h"
35 #include "xfs_extfree_item.h"
36 #include "xfs_alloc.h"
38 #include "xfs_bmap_util.h"
39 #include "xfs_bmap_btree.h"
40 #include "xfs_rtalloc.h"
41 #include "xfs_error.h"
42 #include "xfs_quota.h"
43 #include "xfs_trans_space.h"
44 #include "xfs_buf_item.h"
45 #include "xfs_trace.h"
46 #include "xfs_symlink.h"
47 #include "xfs_attr_leaf.h"
48 #include "xfs_filestream.h"
50 #include "xfs_ag_resv.h"
51 #include "xfs_refcount.h"
52 #include "xfs_rmap_btree.h"
53 #include "xfs_icache.h"
56 kmem_zone_t *xfs_bmap_free_item_zone;
59 * Miscellaneous helper functions
63 * Compute and fill in the value of the maximum depth of a bmap btree
64 * in this filesystem. Done once, during mount.
67 xfs_bmap_compute_maxlevels(
68 xfs_mount_t *mp, /* file system mount structure */
69 int whichfork) /* data or attr fork */
71 int level; /* btree level */
72 uint maxblocks; /* max blocks at this level */
73 uint maxleafents; /* max leaf entries possible */
74 int maxrootrecs; /* max records in root block */
75 int minleafrecs; /* min records in leaf block */
76 int minnoderecs; /* min records in node block */
77 int sz; /* root block size */
80 * The maximum number of extents in a file, hence the maximum
81 * number of leaf entries, is controlled by the type of di_nextents
82 * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
83 * (a signed 16-bit number, xfs_aextnum_t).
85 * Note that we can no longer assume that if we are in ATTR1 that
86 * the fork offset of all the inodes will be
87 * (xfs_default_attroffset(ip) >> 3) because we could have mounted
88 * with ATTR2 and then mounted back with ATTR1, keeping the
89 * di_forkoff's fixed but probably at various positions. Therefore,
90 * for both ATTR1 and ATTR2 we have to assume the worst case scenario
91 * of a minimum size available.
93 if (whichfork == XFS_DATA_FORK) {
94 maxleafents = MAXEXTNUM;
95 sz = XFS_BMDR_SPACE_CALC(MINDBTPTRS);
97 maxleafents = MAXAEXTNUM;
98 sz = XFS_BMDR_SPACE_CALC(MINABTPTRS);
100 maxrootrecs = xfs_bmdr_maxrecs(sz, 0);
101 minleafrecs = mp->m_bmap_dmnr[0];
102 minnoderecs = mp->m_bmap_dmnr[1];
103 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
104 for (level = 1; maxblocks > 1; level++) {
105 if (maxblocks <= maxrootrecs)
108 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
110 mp->m_bm_maxlevels[whichfork] = level;
113 STATIC int /* error */
115 struct xfs_btree_cur *cur,
119 int *stat) /* success/failure */
121 cur->bc_rec.b.br_startoff = off;
122 cur->bc_rec.b.br_startblock = bno;
123 cur->bc_rec.b.br_blockcount = len;
124 return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
127 STATIC int /* error */
129 struct xfs_btree_cur *cur,
133 int *stat) /* success/failure */
135 cur->bc_rec.b.br_startoff = off;
136 cur->bc_rec.b.br_startblock = bno;
137 cur->bc_rec.b.br_blockcount = len;
138 return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
142 * Check if the inode needs to be converted to btree format.
144 static inline bool xfs_bmap_needs_btree(struct xfs_inode *ip, int whichfork)
146 return whichfork != XFS_COW_FORK &&
147 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
148 XFS_IFORK_NEXTENTS(ip, whichfork) >
149 XFS_IFORK_MAXEXT(ip, whichfork);
153 * Check if the inode should be converted to extent format.
155 static inline bool xfs_bmap_wants_extents(struct xfs_inode *ip, int whichfork)
157 return whichfork != XFS_COW_FORK &&
158 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
159 XFS_IFORK_NEXTENTS(ip, whichfork) <=
160 XFS_IFORK_MAXEXT(ip, whichfork);
164 * Update the record referred to by cur to the value given
165 * by [off, bno, len, state].
166 * This either works (return 0) or gets an EFSCORRUPTED error.
170 struct xfs_btree_cur *cur,
176 union xfs_btree_rec rec;
178 xfs_bmbt_disk_set_allf(&rec.bmbt, off, bno, len, state);
179 return xfs_btree_update(cur, &rec);
183 * Compute the worst-case number of indirect blocks that will be used
184 * for ip's delayed extent of length "len".
187 xfs_bmap_worst_indlen(
188 xfs_inode_t *ip, /* incore inode pointer */
189 xfs_filblks_t len) /* delayed extent length */
191 int level; /* btree level number */
192 int maxrecs; /* maximum record count at this level */
193 xfs_mount_t *mp; /* mount structure */
194 xfs_filblks_t rval; /* return value */
195 xfs_filblks_t orig_len;
199 /* Calculate the worst-case size of the bmbt. */
201 maxrecs = mp->m_bmap_dmxr[0];
202 for (level = 0, rval = 0;
203 level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK);
206 do_div(len, maxrecs);
209 rval += XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) -
214 maxrecs = mp->m_bmap_dmxr[1];
217 /* Calculate the worst-case size of the rmapbt. */
218 if (xfs_sb_version_hasrmapbt(&mp->m_sb))
219 rval += 1 + xfs_rmapbt_calc_size(mp, orig_len) +
220 mp->m_rmap_maxlevels;
226 * Calculate the default attribute fork offset for newly created inodes.
229 xfs_default_attroffset(
230 struct xfs_inode *ip)
232 struct xfs_mount *mp = ip->i_mount;
235 if (mp->m_sb.sb_inodesize == 256) {
236 offset = XFS_LITINO(mp, ip->i_d.di_version) -
237 XFS_BMDR_SPACE_CALC(MINABTPTRS);
239 offset = XFS_BMDR_SPACE_CALC(6 * MINABTPTRS);
242 ASSERT(offset < XFS_LITINO(mp, ip->i_d.di_version));
247 * Helper routine to reset inode di_forkoff field when switching
248 * attribute fork from local to extent format - we reset it where
249 * possible to make space available for inline data fork extents.
252 xfs_bmap_forkoff_reset(
256 if (whichfork == XFS_ATTR_FORK &&
257 ip->i_d.di_format != XFS_DINODE_FMT_DEV &&
258 ip->i_d.di_format != XFS_DINODE_FMT_UUID &&
259 ip->i_d.di_format != XFS_DINODE_FMT_BTREE) {
260 uint dfl_forkoff = xfs_default_attroffset(ip) >> 3;
262 if (dfl_forkoff > ip->i_d.di_forkoff)
263 ip->i_d.di_forkoff = dfl_forkoff;
268 STATIC struct xfs_buf *
270 struct xfs_btree_cur *cur,
273 struct xfs_log_item_desc *lidp;
279 for (i = 0; i < XFS_BTREE_MAXLEVELS; i++) {
280 if (!cur->bc_bufs[i])
282 if (XFS_BUF_ADDR(cur->bc_bufs[i]) == bno)
283 return cur->bc_bufs[i];
286 /* Chase down all the log items to see if the bp is there */
287 list_for_each_entry(lidp, &cur->bc_tp->t_items, lid_trans) {
288 struct xfs_buf_log_item *bip;
289 bip = (struct xfs_buf_log_item *)lidp->lid_item;
290 if (bip->bli_item.li_type == XFS_LI_BUF &&
291 XFS_BUF_ADDR(bip->bli_buf) == bno)
300 struct xfs_btree_block *block,
306 __be64 *pp, *thispa; /* pointer to block address */
307 xfs_bmbt_key_t *prevp, *keyp;
309 ASSERT(be16_to_cpu(block->bb_level) > 0);
312 for( i = 1; i <= xfs_btree_get_numrecs(block); i++) {
313 dmxr = mp->m_bmap_dmxr[0];
314 keyp = XFS_BMBT_KEY_ADDR(mp, block, i);
317 ASSERT(be64_to_cpu(prevp->br_startoff) <
318 be64_to_cpu(keyp->br_startoff));
323 * Compare the block numbers to see if there are dups.
326 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, i, sz);
328 pp = XFS_BMBT_PTR_ADDR(mp, block, i, dmxr);
330 for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) {
332 thispa = XFS_BMAP_BROOT_PTR_ADDR(mp, block, j, sz);
334 thispa = XFS_BMBT_PTR_ADDR(mp, block, j, dmxr);
335 if (*thispa == *pp) {
336 xfs_warn(mp, "%s: thispa(%d) == pp(%d) %Ld",
338 (unsigned long long)be64_to_cpu(*thispa));
339 panic("%s: ptrs are equal in node\n",
347 * Check that the extents for the inode ip are in the right order in all
348 * btree leaves. THis becomes prohibitively expensive for large extent count
349 * files, so don't bother with inodes that have more than 10,000 extents in
350 * them. The btree record ordering checks will still be done, so for such large
351 * bmapbt constructs that is going to catch most corruptions.
354 xfs_bmap_check_leaf_extents(
355 xfs_btree_cur_t *cur, /* btree cursor or null */
356 xfs_inode_t *ip, /* incore inode pointer */
357 int whichfork) /* data or attr fork */
359 struct xfs_btree_block *block; /* current btree block */
360 xfs_fsblock_t bno; /* block # of "block" */
361 xfs_buf_t *bp; /* buffer for "block" */
362 int error; /* error return value */
363 xfs_extnum_t i=0, j; /* index into the extents list */
364 xfs_ifork_t *ifp; /* fork structure */
365 int level; /* btree level, for checking */
366 xfs_mount_t *mp; /* file system mount structure */
367 __be64 *pp; /* pointer to block address */
368 xfs_bmbt_rec_t *ep; /* pointer to current extent */
369 xfs_bmbt_rec_t last = {0, 0}; /* last extent in prev block */
370 xfs_bmbt_rec_t *nextp; /* pointer to next extent */
373 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) {
377 /* skip large extent count inodes */
378 if (ip->i_d.di_nextents > 10000)
383 ifp = XFS_IFORK_PTR(ip, whichfork);
384 block = ifp->if_broot;
386 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
388 level = be16_to_cpu(block->bb_level);
390 xfs_check_block(block, mp, 1, ifp->if_broot_bytes);
391 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
392 bno = be64_to_cpu(*pp);
394 ASSERT(bno != NULLFSBLOCK);
395 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
396 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
399 * Go down the tree until leaf level is reached, following the first
400 * pointer (leftmost) at each level.
402 while (level-- > 0) {
403 /* See if buf is in cur first */
405 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
408 error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
414 block = XFS_BUF_TO_BLOCK(bp);
419 * Check this block for basic sanity (increasing keys and
420 * no duplicate blocks).
423 xfs_check_block(block, mp, 0, 0);
424 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
425 bno = be64_to_cpu(*pp);
426 XFS_WANT_CORRUPTED_GOTO(mp,
427 XFS_FSB_SANITY_CHECK(mp, bno), error0);
430 xfs_trans_brelse(NULL, bp);
435 * Here with bp and block set to the leftmost leaf node in the tree.
440 * Loop over all leaf nodes checking that all extents are in the right order.
443 xfs_fsblock_t nextbno;
444 xfs_extnum_t num_recs;
447 num_recs = xfs_btree_get_numrecs(block);
450 * Read-ahead the next leaf block, if any.
453 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
456 * Check all the extents to make sure they are OK.
457 * If we had a previous block, the last entry should
458 * conform with the first entry in this one.
461 ep = XFS_BMBT_REC_ADDR(mp, block, 1);
463 ASSERT(xfs_bmbt_disk_get_startoff(&last) +
464 xfs_bmbt_disk_get_blockcount(&last) <=
465 xfs_bmbt_disk_get_startoff(ep));
467 for (j = 1; j < num_recs; j++) {
468 nextp = XFS_BMBT_REC_ADDR(mp, block, j + 1);
469 ASSERT(xfs_bmbt_disk_get_startoff(ep) +
470 xfs_bmbt_disk_get_blockcount(ep) <=
471 xfs_bmbt_disk_get_startoff(nextp));
479 xfs_trans_brelse(NULL, bp);
483 * If we've reached the end, stop.
485 if (bno == NULLFSBLOCK)
489 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
492 error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
498 block = XFS_BUF_TO_BLOCK(bp);
504 xfs_warn(mp, "%s: at error0", __func__);
506 xfs_trans_brelse(NULL, bp);
508 xfs_warn(mp, "%s: BAD after btree leaves for %d extents",
510 panic("%s: CORRUPTED BTREE OR SOMETHING", __func__);
515 * Add bmap trace insert entries for all the contents of the extent records.
518 xfs_bmap_trace_exlist(
519 xfs_inode_t *ip, /* incore inode pointer */
520 xfs_extnum_t cnt, /* count of entries in the list */
521 int whichfork, /* data or attr or cow fork */
522 unsigned long caller_ip)
524 xfs_extnum_t idx; /* extent record index */
525 xfs_ifork_t *ifp; /* inode fork pointer */
528 if (whichfork == XFS_ATTR_FORK)
529 state |= BMAP_ATTRFORK;
530 else if (whichfork == XFS_COW_FORK)
531 state |= BMAP_COWFORK;
533 ifp = XFS_IFORK_PTR(ip, whichfork);
534 ASSERT(cnt == xfs_iext_count(ifp));
535 for (idx = 0; idx < cnt; idx++)
536 trace_xfs_extlist(ip, idx, state, caller_ip);
540 * Validate that the bmbt_irecs being returned from bmapi are valid
541 * given the caller's original parameters. Specifically check the
542 * ranges of the returned irecs to ensure that they only extend beyond
543 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
546 xfs_bmap_validate_ret(
550 xfs_bmbt_irec_t *mval,
554 int i; /* index to map values */
556 ASSERT(ret_nmap <= nmap);
558 for (i = 0; i < ret_nmap; i++) {
559 ASSERT(mval[i].br_blockcount > 0);
560 if (!(flags & XFS_BMAPI_ENTIRE)) {
561 ASSERT(mval[i].br_startoff >= bno);
562 ASSERT(mval[i].br_blockcount <= len);
563 ASSERT(mval[i].br_startoff + mval[i].br_blockcount <=
566 ASSERT(mval[i].br_startoff < bno + len);
567 ASSERT(mval[i].br_startoff + mval[i].br_blockcount >
571 mval[i - 1].br_startoff + mval[i - 1].br_blockcount ==
572 mval[i].br_startoff);
573 ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK &&
574 mval[i].br_startblock != HOLESTARTBLOCK);
575 ASSERT(mval[i].br_state == XFS_EXT_NORM ||
576 mval[i].br_state == XFS_EXT_UNWRITTEN);
581 #define xfs_bmap_check_leaf_extents(cur, ip, whichfork) do { } while (0)
582 #define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap)
586 * bmap free list manipulation functions
590 * Add the extent to the list of extents to be free at transaction end.
591 * The list is maintained sorted (by block number).
595 struct xfs_mount *mp,
596 struct xfs_defer_ops *dfops,
599 struct xfs_owner_info *oinfo)
601 struct xfs_extent_free_item *new; /* new element */
606 ASSERT(bno != NULLFSBLOCK);
608 ASSERT(len <= MAXEXTLEN);
609 ASSERT(!isnullstartblock(bno));
610 agno = XFS_FSB_TO_AGNO(mp, bno);
611 agbno = XFS_FSB_TO_AGBNO(mp, bno);
612 ASSERT(agno < mp->m_sb.sb_agcount);
613 ASSERT(agbno < mp->m_sb.sb_agblocks);
614 ASSERT(len < mp->m_sb.sb_agblocks);
615 ASSERT(agbno + len <= mp->m_sb.sb_agblocks);
617 ASSERT(xfs_bmap_free_item_zone != NULL);
619 new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP);
620 new->xefi_startblock = bno;
621 new->xefi_blockcount = (xfs_extlen_t)len;
623 new->xefi_oinfo = *oinfo;
625 xfs_rmap_skip_owner_update(&new->xefi_oinfo);
626 trace_xfs_bmap_free_defer(mp, XFS_FSB_TO_AGNO(mp, bno), 0,
627 XFS_FSB_TO_AGBNO(mp, bno), len);
628 xfs_defer_add(dfops, XFS_DEFER_OPS_TYPE_FREE, &new->xefi_list);
632 * Inode fork format manipulation functions
636 * Transform a btree format file with only one leaf node, where the
637 * extents list will fit in the inode, into an extents format file.
638 * Since the file extents are already in-core, all we have to do is
639 * give up the space for the btree root and pitch the leaf block.
641 STATIC int /* error */
642 xfs_bmap_btree_to_extents(
643 xfs_trans_t *tp, /* transaction pointer */
644 xfs_inode_t *ip, /* incore inode pointer */
645 xfs_btree_cur_t *cur, /* btree cursor */
646 int *logflagsp, /* inode logging flags */
647 int whichfork) /* data or attr fork */
650 struct xfs_btree_block *cblock;/* child btree block */
651 xfs_fsblock_t cbno; /* child block number */
652 xfs_buf_t *cbp; /* child block's buffer */
653 int error; /* error return value */
654 xfs_ifork_t *ifp; /* inode fork data */
655 xfs_mount_t *mp; /* mount point structure */
656 __be64 *pp; /* ptr to block address */
657 struct xfs_btree_block *rblock;/* root btree block */
658 struct xfs_owner_info oinfo;
661 ifp = XFS_IFORK_PTR(ip, whichfork);
662 ASSERT(whichfork != XFS_COW_FORK);
663 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
664 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
665 rblock = ifp->if_broot;
666 ASSERT(be16_to_cpu(rblock->bb_level) == 1);
667 ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1);
668 ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0) == 1);
669 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, ifp->if_broot_bytes);
670 cbno = be64_to_cpu(*pp);
673 if ((error = xfs_btree_check_lptr(cur, cbno, 1)))
676 error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp, XFS_BMAP_BTREE_REF,
680 cblock = XFS_BUF_TO_BLOCK(cbp);
681 if ((error = xfs_btree_check_block(cur, cblock, 0, cbp)))
683 xfs_rmap_ino_bmbt_owner(&oinfo, ip->i_ino, whichfork);
684 xfs_bmap_add_free(mp, cur->bc_private.b.dfops, cbno, 1, &oinfo);
685 ip->i_d.di_nblocks--;
686 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
687 xfs_trans_binval(tp, cbp);
688 if (cur->bc_bufs[0] == cbp)
689 cur->bc_bufs[0] = NULL;
690 xfs_iroot_realloc(ip, -1, whichfork);
691 ASSERT(ifp->if_broot == NULL);
692 ASSERT((ifp->if_flags & XFS_IFBROOT) == 0);
693 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
694 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
699 * Convert an extents-format file into a btree-format file.
700 * The new file will have a root block (in the inode) and a single child block.
702 STATIC int /* error */
703 xfs_bmap_extents_to_btree(
704 xfs_trans_t *tp, /* transaction pointer */
705 xfs_inode_t *ip, /* incore inode pointer */
706 xfs_fsblock_t *firstblock, /* first-block-allocated */
707 struct xfs_defer_ops *dfops, /* blocks freed in xaction */
708 xfs_btree_cur_t **curp, /* cursor returned to caller */
709 int wasdel, /* converting a delayed alloc */
710 int *logflagsp, /* inode logging flags */
711 int whichfork) /* data or attr fork */
713 struct xfs_btree_block *ablock; /* allocated (child) bt block */
714 xfs_buf_t *abp; /* buffer for ablock */
715 xfs_alloc_arg_t args; /* allocation arguments */
716 xfs_bmbt_rec_t *arp; /* child record pointer */
717 struct xfs_btree_block *block; /* btree root block */
718 xfs_btree_cur_t *cur; /* bmap btree cursor */
719 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
720 int error; /* error return value */
721 xfs_extnum_t i, cnt; /* extent record index */
722 xfs_ifork_t *ifp; /* inode fork pointer */
723 xfs_bmbt_key_t *kp; /* root block key pointer */
724 xfs_mount_t *mp; /* mount structure */
725 xfs_extnum_t nextents; /* number of file extents */
726 xfs_bmbt_ptr_t *pp; /* root block address pointer */
729 ASSERT(whichfork != XFS_COW_FORK);
730 ifp = XFS_IFORK_PTR(ip, whichfork);
731 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS);
734 * Make space in the inode incore.
736 xfs_iroot_realloc(ip, 1, whichfork);
737 ifp->if_flags |= XFS_IFBROOT;
742 block = ifp->if_broot;
743 xfs_btree_init_block_int(mp, block, XFS_BUF_DADDR_NULL,
744 XFS_BTNUM_BMAP, 1, 1, ip->i_ino,
745 XFS_BTREE_LONG_PTRS);
747 * Need a cursor. Can't allocate until bb_level is filled in.
749 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
750 cur->bc_private.b.firstblock = *firstblock;
751 cur->bc_private.b.dfops = dfops;
752 cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
754 * Convert to a btree with two levels, one record in root.
756 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE);
757 memset(&args, 0, sizeof(args));
760 xfs_rmap_ino_bmbt_owner(&args.oinfo, ip->i_ino, whichfork);
761 args.firstblock = *firstblock;
762 if (*firstblock == NULLFSBLOCK) {
763 args.type = XFS_ALLOCTYPE_START_BNO;
764 args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino);
765 } else if (dfops->dop_low) {
766 args.type = XFS_ALLOCTYPE_START_BNO;
767 args.fsbno = *firstblock;
769 args.type = XFS_ALLOCTYPE_NEAR_BNO;
770 args.fsbno = *firstblock;
772 args.minlen = args.maxlen = args.prod = 1;
773 args.wasdel = wasdel;
775 if ((error = xfs_alloc_vextent(&args))) {
776 xfs_iroot_realloc(ip, -1, whichfork);
777 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
781 if (WARN_ON_ONCE(args.fsbno == NULLFSBLOCK)) {
782 xfs_iroot_realloc(ip, -1, whichfork);
783 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
787 * Allocation can't fail, the space was reserved.
789 ASSERT(*firstblock == NULLFSBLOCK ||
790 args.agno >= XFS_FSB_TO_AGNO(mp, *firstblock));
791 *firstblock = cur->bc_private.b.firstblock = args.fsbno;
792 cur->bc_private.b.allocated++;
793 ip->i_d.di_nblocks++;
794 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
795 abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0);
797 * Fill in the child block.
799 abp->b_ops = &xfs_bmbt_buf_ops;
800 ablock = XFS_BUF_TO_BLOCK(abp);
801 xfs_btree_init_block_int(mp, ablock, abp->b_bn,
802 XFS_BTNUM_BMAP, 0, 0, ip->i_ino,
803 XFS_BTREE_LONG_PTRS);
805 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
806 nextents = xfs_iext_count(ifp);
807 for (cnt = i = 0; i < nextents; i++) {
808 ep = xfs_iext_get_ext(ifp, i);
809 if (!isnullstartblock(xfs_bmbt_get_startblock(ep))) {
810 arp->l0 = cpu_to_be64(ep->l0);
811 arp->l1 = cpu_to_be64(ep->l1);
815 ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork));
816 xfs_btree_set_numrecs(ablock, cnt);
819 * Fill in the root key and pointer.
821 kp = XFS_BMBT_KEY_ADDR(mp, block, 1);
822 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
823 kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp));
824 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur,
825 be16_to_cpu(block->bb_level)));
826 *pp = cpu_to_be64(args.fsbno);
829 * Do all this logging at the end so that
830 * the root is at the right level.
832 xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS);
833 xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs));
834 ASSERT(*curp == NULL);
836 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork);
841 * Convert a local file to an extents file.
842 * This code is out of bounds for data forks of regular files,
843 * since the file data needs to get logged so things will stay consistent.
844 * (The bmap-level manipulations are ok, though).
847 xfs_bmap_local_to_extents_empty(
848 struct xfs_inode *ip,
851 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
853 ASSERT(whichfork != XFS_COW_FORK);
854 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
855 ASSERT(ifp->if_bytes == 0);
856 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0);
858 xfs_bmap_forkoff_reset(ip, whichfork);
859 ifp->if_flags &= ~XFS_IFINLINE;
860 ifp->if_flags |= XFS_IFEXTENTS;
861 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
865 STATIC int /* error */
866 xfs_bmap_local_to_extents(
867 xfs_trans_t *tp, /* transaction pointer */
868 xfs_inode_t *ip, /* incore inode pointer */
869 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
870 xfs_extlen_t total, /* total blocks needed by transaction */
871 int *logflagsp, /* inode logging flags */
873 void (*init_fn)(struct xfs_trans *tp,
875 struct xfs_inode *ip,
876 struct xfs_ifork *ifp))
879 int flags; /* logging flags returned */
880 xfs_ifork_t *ifp; /* inode fork pointer */
881 xfs_alloc_arg_t args; /* allocation arguments */
882 xfs_buf_t *bp; /* buffer for extent block */
883 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
886 * We don't want to deal with the case of keeping inode data inline yet.
887 * So sending the data fork of a regular inode is invalid.
889 ASSERT(!(S_ISREG(VFS_I(ip)->i_mode) && whichfork == XFS_DATA_FORK));
890 ifp = XFS_IFORK_PTR(ip, whichfork);
891 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
893 if (!ifp->if_bytes) {
894 xfs_bmap_local_to_extents_empty(ip, whichfork);
895 flags = XFS_ILOG_CORE;
901 ASSERT((ifp->if_flags & (XFS_IFINLINE|XFS_IFEXTENTS|XFS_IFEXTIREC)) ==
903 memset(&args, 0, sizeof(args));
905 args.mp = ip->i_mount;
906 xfs_rmap_ino_owner(&args.oinfo, ip->i_ino, whichfork, 0);
907 args.firstblock = *firstblock;
909 * Allocate a block. We know we need only one, since the
910 * file currently fits in an inode.
912 if (*firstblock == NULLFSBLOCK) {
913 args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino);
914 args.type = XFS_ALLOCTYPE_START_BNO;
916 args.fsbno = *firstblock;
917 args.type = XFS_ALLOCTYPE_NEAR_BNO;
920 args.minlen = args.maxlen = args.prod = 1;
921 error = xfs_alloc_vextent(&args);
925 /* Can't fail, the space was reserved. */
926 ASSERT(args.fsbno != NULLFSBLOCK);
927 ASSERT(args.len == 1);
928 *firstblock = args.fsbno;
929 bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0);
932 * Initialize the block, copy the data and log the remote buffer.
934 * The callout is responsible for logging because the remote format
935 * might differ from the local format and thus we don't know how much to
936 * log here. Note that init_fn must also set the buffer log item type
939 init_fn(tp, bp, ip, ifp);
941 /* account for the change in fork size */
942 xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
943 xfs_bmap_local_to_extents_empty(ip, whichfork);
944 flags |= XFS_ILOG_CORE;
946 xfs_iext_add(ifp, 0, 1);
947 ep = xfs_iext_get_ext(ifp, 0);
948 xfs_bmbt_set_allf(ep, 0, args.fsbno, 1, XFS_EXT_NORM);
949 trace_xfs_bmap_post_update(ip, 0,
950 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0,
952 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
953 ip->i_d.di_nblocks = 1;
954 xfs_trans_mod_dquot_byino(tp, ip,
955 XFS_TRANS_DQ_BCOUNT, 1L);
956 flags |= xfs_ilog_fext(whichfork);
964 * Called from xfs_bmap_add_attrfork to handle btree format files.
966 STATIC int /* error */
967 xfs_bmap_add_attrfork_btree(
968 xfs_trans_t *tp, /* transaction pointer */
969 xfs_inode_t *ip, /* incore inode pointer */
970 xfs_fsblock_t *firstblock, /* first block allocated */
971 struct xfs_defer_ops *dfops, /* blocks to free at commit */
972 int *flags) /* inode logging flags */
974 xfs_btree_cur_t *cur; /* btree cursor */
975 int error; /* error return value */
976 xfs_mount_t *mp; /* file system mount struct */
977 int stat; /* newroot status */
980 if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip))
981 *flags |= XFS_ILOG_DBROOT;
983 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
984 cur->bc_private.b.dfops = dfops;
985 cur->bc_private.b.firstblock = *firstblock;
986 if ((error = xfs_bmbt_lookup_ge(cur, 0, 0, 0, &stat)))
988 /* must be at least one entry */
989 XFS_WANT_CORRUPTED_GOTO(mp, stat == 1, error0);
990 if ((error = xfs_btree_new_iroot(cur, flags, &stat)))
993 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
996 *firstblock = cur->bc_private.b.firstblock;
997 cur->bc_private.b.allocated = 0;
998 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1002 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
1007 * Called from xfs_bmap_add_attrfork to handle extents format files.
1009 STATIC int /* error */
1010 xfs_bmap_add_attrfork_extents(
1011 xfs_trans_t *tp, /* transaction pointer */
1012 xfs_inode_t *ip, /* incore inode pointer */
1013 xfs_fsblock_t *firstblock, /* first block allocated */
1014 struct xfs_defer_ops *dfops, /* blocks to free at commit */
1015 int *flags) /* inode logging flags */
1017 xfs_btree_cur_t *cur; /* bmap btree cursor */
1018 int error; /* error return value */
1020 if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip))
1023 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, dfops, &cur, 0,
1024 flags, XFS_DATA_FORK);
1026 cur->bc_private.b.allocated = 0;
1027 xfs_btree_del_cursor(cur,
1028 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
1034 * Called from xfs_bmap_add_attrfork to handle local format files. Each
1035 * different data fork content type needs a different callout to do the
1036 * conversion. Some are basic and only require special block initialisation
1037 * callouts for the data formating, others (directories) are so specialised they
1038 * handle everything themselves.
1040 * XXX (dgc): investigate whether directory conversion can use the generic
1041 * formatting callout. It should be possible - it's just a very complex
1044 STATIC int /* error */
1045 xfs_bmap_add_attrfork_local(
1046 xfs_trans_t *tp, /* transaction pointer */
1047 xfs_inode_t *ip, /* incore inode pointer */
1048 xfs_fsblock_t *firstblock, /* first block allocated */
1049 struct xfs_defer_ops *dfops, /* blocks to free at commit */
1050 int *flags) /* inode logging flags */
1052 xfs_da_args_t dargs; /* args for dir/attr code */
1054 if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip))
1057 if (S_ISDIR(VFS_I(ip)->i_mode)) {
1058 memset(&dargs, 0, sizeof(dargs));
1059 dargs.geo = ip->i_mount->m_dir_geo;
1061 dargs.firstblock = firstblock;
1062 dargs.dfops = dfops;
1063 dargs.total = dargs.geo->fsbcount;
1064 dargs.whichfork = XFS_DATA_FORK;
1066 return xfs_dir2_sf_to_block(&dargs);
1069 if (S_ISLNK(VFS_I(ip)->i_mode))
1070 return xfs_bmap_local_to_extents(tp, ip, firstblock, 1,
1071 flags, XFS_DATA_FORK,
1072 xfs_symlink_local_to_remote);
1074 /* should only be called for types that support local format data */
1076 return -EFSCORRUPTED;
1080 * Convert inode from non-attributed to attributed.
1081 * Must not be in a transaction, ip must not be locked.
1083 int /* error code */
1084 xfs_bmap_add_attrfork(
1085 xfs_inode_t *ip, /* incore inode pointer */
1086 int size, /* space new attribute needs */
1087 int rsvd) /* xact may use reserved blks */
1089 xfs_fsblock_t firstblock; /* 1st block/ag allocated */
1090 struct xfs_defer_ops dfops; /* freed extent records */
1091 xfs_mount_t *mp; /* mount structure */
1092 xfs_trans_t *tp; /* transaction pointer */
1093 int blks; /* space reservation */
1094 int version = 1; /* superblock attr version */
1095 int logflags; /* logging flags */
1096 int error; /* error return value */
1098 ASSERT(XFS_IFORK_Q(ip) == 0);
1101 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
1103 blks = XFS_ADDAFORK_SPACE_RES(mp);
1105 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_addafork, blks, 0,
1106 rsvd ? XFS_TRANS_RESERVE : 0, &tp);
1110 xfs_ilock(ip, XFS_ILOCK_EXCL);
1111 error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ?
1112 XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
1113 XFS_QMOPT_RES_REGBLKS);
1116 if (XFS_IFORK_Q(ip))
1118 if (ip->i_d.di_anextents != 0) {
1119 error = -EFSCORRUPTED;
1122 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) {
1124 * For inodes coming from pre-6.2 filesystems.
1126 ASSERT(ip->i_d.di_aformat == 0);
1127 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
1130 xfs_trans_ijoin(tp, ip, 0);
1131 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1133 switch (ip->i_d.di_format) {
1134 case XFS_DINODE_FMT_DEV:
1135 ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
1137 case XFS_DINODE_FMT_UUID:
1138 ip->i_d.di_forkoff = roundup(sizeof(uuid_t), 8) >> 3;
1140 case XFS_DINODE_FMT_LOCAL:
1141 case XFS_DINODE_FMT_EXTENTS:
1142 case XFS_DINODE_FMT_BTREE:
1143 ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size);
1144 if (!ip->i_d.di_forkoff)
1145 ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3;
1146 else if (mp->m_flags & XFS_MOUNT_ATTR2)
1155 ASSERT(ip->i_afp == NULL);
1156 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP);
1157 ip->i_afp->if_flags = XFS_IFEXTENTS;
1159 xfs_defer_init(&dfops, &firstblock);
1160 switch (ip->i_d.di_format) {
1161 case XFS_DINODE_FMT_LOCAL:
1162 error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &dfops,
1165 case XFS_DINODE_FMT_EXTENTS:
1166 error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock,
1169 case XFS_DINODE_FMT_BTREE:
1170 error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &dfops,
1178 xfs_trans_log_inode(tp, ip, logflags);
1181 if (!xfs_sb_version_hasattr(&mp->m_sb) ||
1182 (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) {
1183 bool log_sb = false;
1185 spin_lock(&mp->m_sb_lock);
1186 if (!xfs_sb_version_hasattr(&mp->m_sb)) {
1187 xfs_sb_version_addattr(&mp->m_sb);
1190 if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) {
1191 xfs_sb_version_addattr2(&mp->m_sb);
1194 spin_unlock(&mp->m_sb_lock);
1199 error = xfs_defer_finish(&tp, &dfops, NULL);
1202 error = xfs_trans_commit(tp);
1203 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1207 xfs_defer_cancel(&dfops);
1209 xfs_trans_cancel(tp);
1210 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1215 * Internal and external extent tree search functions.
1219 * Read in the extents to if_extents.
1220 * All inode fields are set up by caller, we just traverse the btree
1221 * and copy the records in. If the file system cannot contain unwritten
1222 * extents, the records are checked for no "state" flags.
1225 xfs_bmap_read_extents(
1226 xfs_trans_t *tp, /* transaction pointer */
1227 xfs_inode_t *ip, /* incore inode */
1228 int whichfork) /* data or attr fork */
1230 struct xfs_btree_block *block; /* current btree block */
1231 xfs_fsblock_t bno; /* block # of "block" */
1232 xfs_buf_t *bp; /* buffer for "block" */
1233 int error; /* error return value */
1234 xfs_extnum_t i, j; /* index into the extents list */
1235 xfs_ifork_t *ifp; /* fork structure */
1236 int level; /* btree level, for checking */
1237 xfs_mount_t *mp; /* file system mount structure */
1238 __be64 *pp; /* pointer to block address */
1240 xfs_extnum_t room; /* number of entries there's room for */
1243 ifp = XFS_IFORK_PTR(ip, whichfork);
1244 block = ifp->if_broot;
1246 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
1248 level = be16_to_cpu(block->bb_level);
1250 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
1251 bno = be64_to_cpu(*pp);
1254 * Go down the tree until leaf level is reached, following the first
1255 * pointer (leftmost) at each level.
1257 while (level-- > 0) {
1258 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
1259 XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
1262 block = XFS_BUF_TO_BLOCK(bp);
1265 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
1266 bno = be64_to_cpu(*pp);
1267 XFS_WANT_CORRUPTED_GOTO(mp,
1268 XFS_FSB_SANITY_CHECK(mp, bno), error0);
1269 xfs_trans_brelse(tp, bp);
1272 * Here with bp and block set to the leftmost leaf node in the tree.
1274 room = xfs_iext_count(ifp);
1277 * Loop over all leaf nodes. Copy information to the extent records.
1280 xfs_bmbt_rec_t *frp;
1281 xfs_fsblock_t nextbno;
1282 xfs_extnum_t num_recs;
1285 num_recs = xfs_btree_get_numrecs(block);
1286 if (unlikely(i + num_recs > room)) {
1287 ASSERT(i + num_recs <= room);
1288 xfs_warn(ip->i_mount,
1289 "corrupt dinode %Lu, (btree extents).",
1290 (unsigned long long) ip->i_ino);
1291 XFS_CORRUPTION_ERROR("xfs_bmap_read_extents(1)",
1292 XFS_ERRLEVEL_LOW, ip->i_mount, block);
1296 * Read-ahead the next leaf block, if any.
1298 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
1299 if (nextbno != NULLFSBLOCK)
1300 xfs_btree_reada_bufl(mp, nextbno, 1,
1303 * Copy records into the extent records.
1305 frp = XFS_BMBT_REC_ADDR(mp, block, 1);
1307 for (j = 0; j < num_recs; j++, i++, frp++) {
1308 xfs_bmbt_rec_host_t *trp = xfs_iext_get_ext(ifp, i);
1309 trp->l0 = be64_to_cpu(frp->l0);
1310 trp->l1 = be64_to_cpu(frp->l1);
1311 if (!xfs_bmbt_validate_extent(mp, whichfork, trp)) {
1312 XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
1313 XFS_ERRLEVEL_LOW, mp);
1317 xfs_trans_brelse(tp, bp);
1320 * If we've reached the end, stop.
1322 if (bno == NULLFSBLOCK)
1324 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
1325 XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
1328 block = XFS_BUF_TO_BLOCK(bp);
1330 if (i != XFS_IFORK_NEXTENTS(ip, whichfork))
1331 return -EFSCORRUPTED;
1332 ASSERT(i == xfs_iext_count(ifp));
1333 XFS_BMAP_TRACE_EXLIST(ip, i, whichfork);
1336 xfs_trans_brelse(tp, bp);
1337 return -EFSCORRUPTED;
1341 * Returns the file-relative block number of the first unused block(s)
1342 * in the file with at least "len" logically contiguous blocks free.
1343 * This is the lowest-address hole if the file has holes, else the first block
1344 * past the end of file.
1345 * Return 0 if the file is currently local (in-inode).
1348 xfs_bmap_first_unused(
1349 xfs_trans_t *tp, /* transaction pointer */
1350 xfs_inode_t *ip, /* incore inode */
1351 xfs_extlen_t len, /* size of hole to find */
1352 xfs_fileoff_t *first_unused, /* unused block */
1353 int whichfork) /* data or attr fork */
1355 int error; /* error return value */
1356 int idx; /* extent record index */
1357 xfs_ifork_t *ifp; /* inode fork pointer */
1358 xfs_fileoff_t lastaddr; /* last block number seen */
1359 xfs_fileoff_t lowest; /* lowest useful block */
1360 xfs_fileoff_t max; /* starting useful block */
1361 xfs_fileoff_t off; /* offset for this block */
1362 xfs_extnum_t nextents; /* number of extent entries */
1364 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE ||
1365 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ||
1366 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
1367 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
1371 ifp = XFS_IFORK_PTR(ip, whichfork);
1372 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
1373 (error = xfs_iread_extents(tp, ip, whichfork)))
1375 lowest = *first_unused;
1376 nextents = xfs_iext_count(ifp);
1377 for (idx = 0, lastaddr = 0, max = lowest; idx < nextents; idx++) {
1378 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, idx);
1379 off = xfs_bmbt_get_startoff(ep);
1381 * See if the hole before this extent will work.
1383 if (off >= lowest + len && off - max >= len) {
1384 *first_unused = max;
1387 lastaddr = off + xfs_bmbt_get_blockcount(ep);
1388 max = XFS_FILEOFF_MAX(lastaddr, lowest);
1390 *first_unused = max;
1395 * Returns the file-relative block number of the last block - 1 before
1396 * last_block (input value) in the file.
1397 * This is not based on i_size, it is based on the extent records.
1398 * Returns 0 for local files, as they do not have extent records.
1401 xfs_bmap_last_before(
1402 struct xfs_trans *tp, /* transaction pointer */
1403 struct xfs_inode *ip, /* incore inode */
1404 xfs_fileoff_t *last_block, /* last block */
1405 int whichfork) /* data or attr fork */
1407 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1408 struct xfs_bmbt_irec got;
1412 switch (XFS_IFORK_FORMAT(ip, whichfork)) {
1413 case XFS_DINODE_FMT_LOCAL:
1416 case XFS_DINODE_FMT_BTREE:
1417 case XFS_DINODE_FMT_EXTENTS:
1423 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1424 error = xfs_iread_extents(tp, ip, whichfork);
1429 if (xfs_iext_lookup_extent(ip, ifp, *last_block - 1, &idx, &got)) {
1430 if (got.br_startoff <= *last_block - 1)
1434 if (xfs_iext_get_extent(ifp, idx - 1, &got)) {
1435 *last_block = got.br_startoff + got.br_blockcount;
1444 xfs_bmap_last_extent(
1445 struct xfs_trans *tp,
1446 struct xfs_inode *ip,
1448 struct xfs_bmbt_irec *rec,
1451 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1455 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1456 error = xfs_iread_extents(tp, ip, whichfork);
1461 nextents = xfs_iext_count(ifp);
1462 if (nextents == 0) {
1467 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, nextents - 1), rec);
1473 * Check the last inode extent to determine whether this allocation will result
1474 * in blocks being allocated at the end of the file. When we allocate new data
1475 * blocks at the end of the file which do not start at the previous data block,
1476 * we will try to align the new blocks at stripe unit boundaries.
1478 * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be
1479 * at, or past the EOF.
1483 struct xfs_bmalloca *bma,
1486 struct xfs_bmbt_irec rec;
1491 error = xfs_bmap_last_extent(NULL, bma->ip, whichfork, &rec,
1502 * Check if we are allocation or past the last extent, or at least into
1503 * the last delayed allocated extent.
1505 bma->aeof = bma->offset >= rec.br_startoff + rec.br_blockcount ||
1506 (bma->offset >= rec.br_startoff &&
1507 isnullstartblock(rec.br_startblock));
1512 * Returns the file-relative block number of the first block past eof in
1513 * the file. This is not based on i_size, it is based on the extent records.
1514 * Returns 0 for local files, as they do not have extent records.
1517 xfs_bmap_last_offset(
1518 struct xfs_inode *ip,
1519 xfs_fileoff_t *last_block,
1522 struct xfs_bmbt_irec rec;
1528 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL)
1531 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
1532 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
1535 error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, &is_empty);
1536 if (error || is_empty)
1539 *last_block = rec.br_startoff + rec.br_blockcount;
1544 * Returns whether the selected fork of the inode has exactly one
1545 * block or not. For the data fork we check this matches di_size,
1546 * implying the file's range is 0..bsize-1.
1548 int /* 1=>1 block, 0=>otherwise */
1550 xfs_inode_t *ip, /* incore inode */
1551 int whichfork) /* data or attr fork */
1553 xfs_bmbt_rec_host_t *ep; /* ptr to fork's extent */
1554 xfs_ifork_t *ifp; /* inode fork pointer */
1555 int rval; /* return value */
1556 xfs_bmbt_irec_t s; /* internal version of extent */
1559 if (whichfork == XFS_DATA_FORK)
1560 return XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize;
1562 if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1)
1564 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
1566 ifp = XFS_IFORK_PTR(ip, whichfork);
1567 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
1568 ep = xfs_iext_get_ext(ifp, 0);
1569 xfs_bmbt_get_all(ep, &s);
1570 rval = s.br_startoff == 0 && s.br_blockcount == 1;
1571 if (rval && whichfork == XFS_DATA_FORK)
1572 ASSERT(XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize);
1577 * Extent tree manipulation functions used during allocation.
1581 * Convert a delayed allocation to a real allocation.
1583 STATIC int /* error */
1584 xfs_bmap_add_extent_delay_real(
1585 struct xfs_bmalloca *bma,
1588 struct xfs_bmbt_irec *new = &bma->got;
1589 int diff; /* temp value */
1590 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
1591 int error; /* error return value */
1592 int i; /* temp state */
1593 xfs_ifork_t *ifp; /* inode fork pointer */
1594 xfs_fileoff_t new_endoff; /* end offset of new entry */
1595 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
1596 /* left is 0, right is 1, prev is 2 */
1597 int rval=0; /* return value (logging flags) */
1598 int state = 0;/* state bits, accessed thru macros */
1599 xfs_filblks_t da_new; /* new count del alloc blocks used */
1600 xfs_filblks_t da_old; /* old count del alloc blocks used */
1601 xfs_filblks_t temp=0; /* value for da_new calculations */
1602 xfs_filblks_t temp2=0;/* value for da_new calculations */
1603 int tmp_rval; /* partial logging flags */
1604 struct xfs_mount *mp;
1605 xfs_extnum_t *nextents;
1607 mp = bma->ip->i_mount;
1608 ifp = XFS_IFORK_PTR(bma->ip, whichfork);
1609 ASSERT(whichfork != XFS_ATTR_FORK);
1610 nextents = (whichfork == XFS_COW_FORK ? &bma->ip->i_cnextents :
1611 &bma->ip->i_d.di_nextents);
1613 ASSERT(bma->idx >= 0);
1614 ASSERT(bma->idx <= xfs_iext_count(ifp));
1615 ASSERT(!isnullstartblock(new->br_startblock));
1617 (bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
1619 XFS_STATS_INC(mp, xs_add_exlist);
1625 if (whichfork == XFS_COW_FORK)
1626 state |= BMAP_COWFORK;
1629 * Set up a bunch of variables to make the tests simpler.
1631 ep = xfs_iext_get_ext(ifp, bma->idx);
1632 xfs_bmbt_get_all(ep, &PREV);
1633 new_endoff = new->br_startoff + new->br_blockcount;
1634 ASSERT(PREV.br_startoff <= new->br_startoff);
1635 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
1637 da_old = startblockval(PREV.br_startblock);
1641 * Set flags determining what part of the previous delayed allocation
1642 * extent is being replaced by a real allocation.
1644 if (PREV.br_startoff == new->br_startoff)
1645 state |= BMAP_LEFT_FILLING;
1646 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
1647 state |= BMAP_RIGHT_FILLING;
1650 * Check and set flags if this segment has a left neighbor.
1651 * Don't set contiguous if the combined extent would be too large.
1654 state |= BMAP_LEFT_VALID;
1655 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1), &LEFT);
1657 if (isnullstartblock(LEFT.br_startblock))
1658 state |= BMAP_LEFT_DELAY;
1661 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1662 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1663 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
1664 LEFT.br_state == new->br_state &&
1665 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1666 state |= BMAP_LEFT_CONTIG;
1669 * Check and set flags if this segment has a right neighbor.
1670 * Don't set contiguous if the combined extent would be too large.
1671 * Also check for all-three-contiguous being too large.
1673 if (bma->idx < xfs_iext_count(ifp) - 1) {
1674 state |= BMAP_RIGHT_VALID;
1675 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx + 1), &RIGHT);
1677 if (isnullstartblock(RIGHT.br_startblock))
1678 state |= BMAP_RIGHT_DELAY;
1681 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1682 new_endoff == RIGHT.br_startoff &&
1683 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
1684 new->br_state == RIGHT.br_state &&
1685 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
1686 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1687 BMAP_RIGHT_FILLING)) !=
1688 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1689 BMAP_RIGHT_FILLING) ||
1690 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
1692 state |= BMAP_RIGHT_CONTIG;
1696 * Switch out based on the FILLING and CONTIG state bits.
1698 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1699 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
1700 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1701 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1703 * Filling in all of a previously delayed allocation extent.
1704 * The left and right neighbors are both contiguous with new.
1707 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
1708 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
1709 LEFT.br_blockcount + PREV.br_blockcount +
1710 RIGHT.br_blockcount);
1711 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
1713 xfs_iext_remove(bma->ip, bma->idx + 1, 2, state);
1715 if (bma->cur == NULL)
1716 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1718 rval = XFS_ILOG_CORE;
1719 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
1720 RIGHT.br_startblock,
1721 RIGHT.br_blockcount, &i);
1724 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
1725 error = xfs_btree_delete(bma->cur, &i);
1728 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
1729 error = xfs_btree_decrement(bma->cur, 0, &i);
1732 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
1733 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
1735 LEFT.br_blockcount +
1736 PREV.br_blockcount +
1737 RIGHT.br_blockcount, LEFT.br_state);
1743 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1745 * Filling in all of a previously delayed allocation extent.
1746 * The left neighbor is contiguous, the right is not.
1750 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
1751 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
1752 LEFT.br_blockcount + PREV.br_blockcount);
1753 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
1755 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
1756 if (bma->cur == NULL)
1757 rval = XFS_ILOG_DEXT;
1760 error = xfs_bmbt_lookup_eq(bma->cur, LEFT.br_startoff,
1761 LEFT.br_startblock, LEFT.br_blockcount,
1765 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
1766 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
1768 LEFT.br_blockcount +
1769 PREV.br_blockcount, LEFT.br_state);
1775 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1777 * Filling in all of a previously delayed allocation extent.
1778 * The right neighbor is contiguous, the left is not.
1780 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
1781 xfs_bmbt_set_startblock(ep, new->br_startblock);
1782 xfs_bmbt_set_blockcount(ep,
1783 PREV.br_blockcount + RIGHT.br_blockcount);
1784 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
1786 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
1787 if (bma->cur == NULL)
1788 rval = XFS_ILOG_DEXT;
1791 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
1792 RIGHT.br_startblock,
1793 RIGHT.br_blockcount, &i);
1796 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
1797 error = xfs_bmbt_update(bma->cur, PREV.br_startoff,
1799 PREV.br_blockcount +
1800 RIGHT.br_blockcount, PREV.br_state);
1806 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
1808 * Filling in all of a previously delayed allocation extent.
1809 * Neither the left nor right neighbors are contiguous with
1812 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
1813 xfs_bmbt_set_startblock(ep, new->br_startblock);
1814 xfs_bmbt_set_state(ep, new->br_state);
1815 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
1818 if (bma->cur == NULL)
1819 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1821 rval = XFS_ILOG_CORE;
1822 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
1823 new->br_startblock, new->br_blockcount,
1827 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
1828 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
1829 error = xfs_btree_insert(bma->cur, &i);
1832 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
1836 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
1838 * Filling in the first part of a previous delayed allocation.
1839 * The left neighbor is contiguous.
1841 trace_xfs_bmap_pre_update(bma->ip, bma->idx - 1, state, _THIS_IP_);
1842 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx - 1),
1843 LEFT.br_blockcount + new->br_blockcount);
1844 xfs_bmbt_set_startoff(ep,
1845 PREV.br_startoff + new->br_blockcount);
1846 trace_xfs_bmap_post_update(bma->ip, bma->idx - 1, state, _THIS_IP_);
1848 temp = PREV.br_blockcount - new->br_blockcount;
1849 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
1850 xfs_bmbt_set_blockcount(ep, temp);
1851 if (bma->cur == NULL)
1852 rval = XFS_ILOG_DEXT;
1855 error = xfs_bmbt_lookup_eq(bma->cur, LEFT.br_startoff,
1856 LEFT.br_startblock, LEFT.br_blockcount,
1860 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
1861 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
1863 LEFT.br_blockcount +
1869 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
1870 startblockval(PREV.br_startblock));
1871 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
1872 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
1877 case BMAP_LEFT_FILLING:
1879 * Filling in the first part of a previous delayed allocation.
1880 * The left neighbor is not contiguous.
1882 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
1883 xfs_bmbt_set_startoff(ep, new_endoff);
1884 temp = PREV.br_blockcount - new->br_blockcount;
1885 xfs_bmbt_set_blockcount(ep, temp);
1886 xfs_iext_insert(bma->ip, bma->idx, 1, new, state);
1888 if (bma->cur == NULL)
1889 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1891 rval = XFS_ILOG_CORE;
1892 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
1893 new->br_startblock, new->br_blockcount,
1897 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
1898 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
1899 error = xfs_btree_insert(bma->cur, &i);
1902 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
1905 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
1906 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
1907 bma->firstblock, bma->dfops,
1908 &bma->cur, 1, &tmp_rval, whichfork);
1913 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
1914 startblockval(PREV.br_startblock) -
1915 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
1916 ep = xfs_iext_get_ext(ifp, bma->idx + 1);
1917 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
1918 trace_xfs_bmap_post_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
1921 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1923 * Filling in the last part of a previous delayed allocation.
1924 * The right neighbor is contiguous with the new allocation.
1926 temp = PREV.br_blockcount - new->br_blockcount;
1927 trace_xfs_bmap_pre_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
1928 xfs_bmbt_set_blockcount(ep, temp);
1929 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, bma->idx + 1),
1930 new->br_startoff, new->br_startblock,
1931 new->br_blockcount + RIGHT.br_blockcount,
1933 trace_xfs_bmap_post_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
1934 if (bma->cur == NULL)
1935 rval = XFS_ILOG_DEXT;
1938 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
1939 RIGHT.br_startblock,
1940 RIGHT.br_blockcount, &i);
1943 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
1944 error = xfs_bmbt_update(bma->cur, new->br_startoff,
1946 new->br_blockcount +
1947 RIGHT.br_blockcount,
1953 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
1954 startblockval(PREV.br_startblock));
1955 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
1956 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
1957 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
1962 case BMAP_RIGHT_FILLING:
1964 * Filling in the last part of a previous delayed allocation.
1965 * The right neighbor is not contiguous.
1967 temp = PREV.br_blockcount - new->br_blockcount;
1968 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
1969 xfs_bmbt_set_blockcount(ep, temp);
1970 xfs_iext_insert(bma->ip, bma->idx + 1, 1, new, state);
1972 if (bma->cur == NULL)
1973 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1975 rval = XFS_ILOG_CORE;
1976 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
1977 new->br_startblock, new->br_blockcount,
1981 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
1982 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
1983 error = xfs_btree_insert(bma->cur, &i);
1986 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
1989 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
1990 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
1991 bma->firstblock, bma->dfops, &bma->cur, 1,
1992 &tmp_rval, whichfork);
1997 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
1998 startblockval(PREV.br_startblock) -
1999 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
2000 ep = xfs_iext_get_ext(ifp, bma->idx);
2001 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
2002 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
2009 * Filling in the middle part of a previous delayed allocation.
2010 * Contiguity is impossible here.
2011 * This case is avoided almost all the time.
2013 * We start with a delayed allocation:
2015 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
2018 * and we are allocating:
2019 * +rrrrrrrrrrrrrrrrr+
2022 * and we set it up for insertion as:
2023 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
2025 * PREV @ idx LEFT RIGHT
2026 * inserted at idx + 1
2028 temp = new->br_startoff - PREV.br_startoff;
2029 temp2 = PREV.br_startoff + PREV.br_blockcount - new_endoff;
2030 trace_xfs_bmap_pre_update(bma->ip, bma->idx, 0, _THIS_IP_);
2031 xfs_bmbt_set_blockcount(ep, temp); /* truncate PREV */
2033 RIGHT.br_state = PREV.br_state;
2034 RIGHT.br_startblock = nullstartblock(
2035 (int)xfs_bmap_worst_indlen(bma->ip, temp2));
2036 RIGHT.br_startoff = new_endoff;
2037 RIGHT.br_blockcount = temp2;
2038 /* insert LEFT (r[0]) and RIGHT (r[1]) at the same time */
2039 xfs_iext_insert(bma->ip, bma->idx + 1, 2, &LEFT, state);
2041 if (bma->cur == NULL)
2042 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2044 rval = XFS_ILOG_CORE;
2045 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
2046 new->br_startblock, new->br_blockcount,
2050 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
2051 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
2052 error = xfs_btree_insert(bma->cur, &i);
2055 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2058 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
2059 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
2060 bma->firstblock, bma->dfops, &bma->cur,
2061 1, &tmp_rval, whichfork);
2066 temp = xfs_bmap_worst_indlen(bma->ip, temp);
2067 temp2 = xfs_bmap_worst_indlen(bma->ip, temp2);
2068 diff = (int)(temp + temp2 - startblockval(PREV.br_startblock) -
2069 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
2071 error = xfs_mod_fdblocks(bma->ip->i_mount,
2072 -((int64_t)diff), false);
2078 ep = xfs_iext_get_ext(ifp, bma->idx);
2079 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
2080 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
2081 trace_xfs_bmap_pre_update(bma->ip, bma->idx + 2, state, _THIS_IP_);
2082 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, bma->idx + 2),
2083 nullstartblock((int)temp2));
2084 trace_xfs_bmap_post_update(bma->ip, bma->idx + 2, state, _THIS_IP_);
2087 da_new = temp + temp2;
2090 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2091 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2092 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
2093 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2094 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2095 case BMAP_LEFT_CONTIG:
2096 case BMAP_RIGHT_CONTIG:
2098 * These cases are all impossible.
2103 /* add reverse mapping */
2104 error = xfs_rmap_map_extent(mp, bma->dfops, bma->ip, whichfork, new);
2108 /* convert to a btree if necessary */
2109 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
2110 int tmp_logflags; /* partial log flag return val */
2112 ASSERT(bma->cur == NULL);
2113 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
2114 bma->firstblock, bma->dfops, &bma->cur,
2115 da_old > 0, &tmp_logflags, whichfork);
2116 bma->logflags |= tmp_logflags;
2121 /* adjust for changes in reserved delayed indirect blocks */
2122 if (da_old || da_new) {
2125 temp += bma->cur->bc_private.b.allocated;
2126 ASSERT(temp <= da_old);
2128 xfs_mod_fdblocks(bma->ip->i_mount,
2129 (int64_t)(da_old - temp), false);
2132 /* clear out the allocated field, done with it now in any case. */
2134 bma->cur->bc_private.b.allocated = 0;
2136 xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork);
2138 if (whichfork != XFS_COW_FORK)
2139 bma->logflags |= rval;
2147 * Convert an unwritten allocation to a real allocation or vice versa.
2149 STATIC int /* error */
2150 xfs_bmap_add_extent_unwritten_real(
2151 struct xfs_trans *tp,
2152 xfs_inode_t *ip, /* incore inode pointer */
2154 xfs_extnum_t *idx, /* extent number to update/insert */
2155 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
2156 xfs_bmbt_irec_t *new, /* new data to add to file extents */
2157 xfs_fsblock_t *first, /* pointer to firstblock variable */
2158 struct xfs_defer_ops *dfops, /* list of extents to be freed */
2159 int *logflagsp) /* inode logging flags */
2161 xfs_btree_cur_t *cur; /* btree cursor */
2162 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
2163 int error; /* error return value */
2164 int i; /* temp state */
2165 xfs_ifork_t *ifp; /* inode fork pointer */
2166 xfs_fileoff_t new_endoff; /* end offset of new entry */
2167 xfs_exntst_t newext; /* new extent state */
2168 xfs_exntst_t oldext; /* old extent state */
2169 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
2170 /* left is 0, right is 1, prev is 2 */
2171 int rval=0; /* return value (logging flags) */
2172 int state = 0;/* state bits, accessed thru macros */
2173 struct xfs_mount *mp = ip->i_mount;
2178 ifp = XFS_IFORK_PTR(ip, whichfork);
2179 if (whichfork == XFS_COW_FORK)
2180 state |= BMAP_COWFORK;
2183 ASSERT(*idx <= xfs_iext_count(ifp));
2184 ASSERT(!isnullstartblock(new->br_startblock));
2186 XFS_STATS_INC(mp, xs_add_exlist);
2193 * Set up a bunch of variables to make the tests simpler.
2196 ep = xfs_iext_get_ext(ifp, *idx);
2197 xfs_bmbt_get_all(ep, &PREV);
2198 newext = new->br_state;
2199 oldext = (newext == XFS_EXT_UNWRITTEN) ?
2200 XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
2201 ASSERT(PREV.br_state == oldext);
2202 new_endoff = new->br_startoff + new->br_blockcount;
2203 ASSERT(PREV.br_startoff <= new->br_startoff);
2204 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
2207 * Set flags determining what part of the previous oldext allocation
2208 * extent is being replaced by a newext allocation.
2210 if (PREV.br_startoff == new->br_startoff)
2211 state |= BMAP_LEFT_FILLING;
2212 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
2213 state |= BMAP_RIGHT_FILLING;
2216 * Check and set flags if this segment has a left neighbor.
2217 * Don't set contiguous if the combined extent would be too large.
2220 state |= BMAP_LEFT_VALID;
2221 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &LEFT);
2223 if (isnullstartblock(LEFT.br_startblock))
2224 state |= BMAP_LEFT_DELAY;
2227 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2228 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
2229 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
2230 LEFT.br_state == newext &&
2231 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2232 state |= BMAP_LEFT_CONTIG;
2235 * Check and set flags if this segment has a right neighbor.
2236 * Don't set contiguous if the combined extent would be too large.
2237 * Also check for all-three-contiguous being too large.
2239 if (*idx < xfs_iext_count(ifp) - 1) {
2240 state |= BMAP_RIGHT_VALID;
2241 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx + 1), &RIGHT);
2242 if (isnullstartblock(RIGHT.br_startblock))
2243 state |= BMAP_RIGHT_DELAY;
2246 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2247 new_endoff == RIGHT.br_startoff &&
2248 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
2249 newext == RIGHT.br_state &&
2250 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
2251 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2252 BMAP_RIGHT_FILLING)) !=
2253 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2254 BMAP_RIGHT_FILLING) ||
2255 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
2257 state |= BMAP_RIGHT_CONTIG;
2260 * Switch out based on the FILLING and CONTIG state bits.
2262 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2263 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
2264 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2265 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
2267 * Setting all of a previous oldext extent to newext.
2268 * The left and right neighbors are both contiguous with new.
2272 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2273 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
2274 LEFT.br_blockcount + PREV.br_blockcount +
2275 RIGHT.br_blockcount);
2276 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2278 xfs_iext_remove(ip, *idx + 1, 2, state);
2279 XFS_IFORK_NEXT_SET(ip, whichfork,
2280 XFS_IFORK_NEXTENTS(ip, whichfork) - 2);
2282 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2284 rval = XFS_ILOG_CORE;
2285 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
2286 RIGHT.br_startblock,
2287 RIGHT.br_blockcount, &i)))
2289 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2290 if ((error = xfs_btree_delete(cur, &i)))
2292 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2293 if ((error = xfs_btree_decrement(cur, 0, &i)))
2295 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2296 if ((error = xfs_btree_delete(cur, &i)))
2298 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2299 if ((error = xfs_btree_decrement(cur, 0, &i)))
2301 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2302 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
2304 LEFT.br_blockcount + PREV.br_blockcount +
2305 RIGHT.br_blockcount, LEFT.br_state)))
2310 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2312 * Setting all of a previous oldext extent to newext.
2313 * The left neighbor is contiguous, the right is not.
2317 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2318 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
2319 LEFT.br_blockcount + PREV.br_blockcount);
2320 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2322 xfs_iext_remove(ip, *idx + 1, 1, state);
2323 XFS_IFORK_NEXT_SET(ip, whichfork,
2324 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
2326 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2328 rval = XFS_ILOG_CORE;
2329 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2330 PREV.br_startblock, PREV.br_blockcount,
2333 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2334 if ((error = xfs_btree_delete(cur, &i)))
2336 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2337 if ((error = xfs_btree_decrement(cur, 0, &i)))
2339 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2340 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
2342 LEFT.br_blockcount + PREV.br_blockcount,
2348 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
2350 * Setting all of a previous oldext extent to newext.
2351 * The right neighbor is contiguous, the left is not.
2353 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2354 xfs_bmbt_set_blockcount(ep,
2355 PREV.br_blockcount + RIGHT.br_blockcount);
2356 xfs_bmbt_set_state(ep, newext);
2357 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2358 xfs_iext_remove(ip, *idx + 1, 1, state);
2359 XFS_IFORK_NEXT_SET(ip, whichfork,
2360 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
2362 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2364 rval = XFS_ILOG_CORE;
2365 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
2366 RIGHT.br_startblock,
2367 RIGHT.br_blockcount, &i)))
2369 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2370 if ((error = xfs_btree_delete(cur, &i)))
2372 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2373 if ((error = xfs_btree_decrement(cur, 0, &i)))
2375 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2376 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2378 new->br_blockcount + RIGHT.br_blockcount,
2384 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
2386 * Setting all of a previous oldext extent to newext.
2387 * Neither the left nor right neighbors are contiguous with
2390 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2391 xfs_bmbt_set_state(ep, newext);
2392 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2395 rval = XFS_ILOG_DEXT;
2398 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
2399 new->br_startblock, new->br_blockcount,
2402 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2403 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2404 new->br_startblock, new->br_blockcount,
2410 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
2412 * Setting the first part of a previous oldext extent to newext.
2413 * The left neighbor is contiguous.
2415 trace_xfs_bmap_pre_update(ip, *idx - 1, state, _THIS_IP_);
2416 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx - 1),
2417 LEFT.br_blockcount + new->br_blockcount);
2418 xfs_bmbt_set_startoff(ep,
2419 PREV.br_startoff + new->br_blockcount);
2420 trace_xfs_bmap_post_update(ip, *idx - 1, state, _THIS_IP_);
2422 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2423 xfs_bmbt_set_startblock(ep,
2424 new->br_startblock + new->br_blockcount);
2425 xfs_bmbt_set_blockcount(ep,
2426 PREV.br_blockcount - new->br_blockcount);
2427 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2432 rval = XFS_ILOG_DEXT;
2435 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2436 PREV.br_startblock, PREV.br_blockcount,
2439 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2440 if ((error = xfs_bmbt_update(cur,
2441 PREV.br_startoff + new->br_blockcount,
2442 PREV.br_startblock + new->br_blockcount,
2443 PREV.br_blockcount - new->br_blockcount,
2446 if ((error = xfs_btree_decrement(cur, 0, &i)))
2448 error = xfs_bmbt_update(cur, LEFT.br_startoff,
2450 LEFT.br_blockcount + new->br_blockcount,
2457 case BMAP_LEFT_FILLING:
2459 * Setting the first part of a previous oldext extent to newext.
2460 * The left neighbor is not contiguous.
2462 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2463 ASSERT(ep && xfs_bmbt_get_state(ep) == oldext);
2464 xfs_bmbt_set_startoff(ep, new_endoff);
2465 xfs_bmbt_set_blockcount(ep,
2466 PREV.br_blockcount - new->br_blockcount);
2467 xfs_bmbt_set_startblock(ep,
2468 new->br_startblock + new->br_blockcount);
2469 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2471 xfs_iext_insert(ip, *idx, 1, new, state);
2472 XFS_IFORK_NEXT_SET(ip, whichfork,
2473 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
2475 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2477 rval = XFS_ILOG_CORE;
2478 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2479 PREV.br_startblock, PREV.br_blockcount,
2482 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2483 if ((error = xfs_bmbt_update(cur,
2484 PREV.br_startoff + new->br_blockcount,
2485 PREV.br_startblock + new->br_blockcount,
2486 PREV.br_blockcount - new->br_blockcount,
2489 cur->bc_rec.b = *new;
2490 if ((error = xfs_btree_insert(cur, &i)))
2492 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2496 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
2498 * Setting the last part of a previous oldext extent to newext.
2499 * The right neighbor is contiguous with the new allocation.
2501 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2502 xfs_bmbt_set_blockcount(ep,
2503 PREV.br_blockcount - new->br_blockcount);
2504 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2508 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2509 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx),
2510 new->br_startoff, new->br_startblock,
2511 new->br_blockcount + RIGHT.br_blockcount, newext);
2512 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2515 rval = XFS_ILOG_DEXT;
2518 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2520 PREV.br_blockcount, &i)))
2522 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2523 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
2525 PREV.br_blockcount - new->br_blockcount,
2528 if ((error = xfs_btree_increment(cur, 0, &i)))
2530 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2532 new->br_blockcount + RIGHT.br_blockcount,
2538 case BMAP_RIGHT_FILLING:
2540 * Setting the last part of a previous oldext extent to newext.
2541 * The right neighbor is not contiguous.
2543 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2544 xfs_bmbt_set_blockcount(ep,
2545 PREV.br_blockcount - new->br_blockcount);
2546 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2549 xfs_iext_insert(ip, *idx, 1, new, state);
2551 XFS_IFORK_NEXT_SET(ip, whichfork,
2552 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
2554 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2556 rval = XFS_ILOG_CORE;
2557 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2558 PREV.br_startblock, PREV.br_blockcount,
2561 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2562 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
2564 PREV.br_blockcount - new->br_blockcount,
2567 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
2568 new->br_startblock, new->br_blockcount,
2571 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
2572 cur->bc_rec.b.br_state = XFS_EXT_NORM;
2573 if ((error = xfs_btree_insert(cur, &i)))
2575 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2581 * Setting the middle part of a previous oldext extent to
2582 * newext. Contiguity is impossible here.
2583 * One extent becomes three extents.
2585 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2586 xfs_bmbt_set_blockcount(ep,
2587 new->br_startoff - PREV.br_startoff);
2588 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2591 r[1].br_startoff = new_endoff;
2592 r[1].br_blockcount =
2593 PREV.br_startoff + PREV.br_blockcount - new_endoff;
2594 r[1].br_startblock = new->br_startblock + new->br_blockcount;
2595 r[1].br_state = oldext;
2598 xfs_iext_insert(ip, *idx, 2, &r[0], state);
2600 XFS_IFORK_NEXT_SET(ip, whichfork,
2601 XFS_IFORK_NEXTENTS(ip, whichfork) + 2);
2603 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2605 rval = XFS_ILOG_CORE;
2606 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2607 PREV.br_startblock, PREV.br_blockcount,
2610 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2611 /* new right extent - oldext */
2612 if ((error = xfs_bmbt_update(cur, r[1].br_startoff,
2613 r[1].br_startblock, r[1].br_blockcount,
2616 /* new left extent - oldext */
2617 cur->bc_rec.b = PREV;
2618 cur->bc_rec.b.br_blockcount =
2619 new->br_startoff - PREV.br_startoff;
2620 if ((error = xfs_btree_insert(cur, &i)))
2622 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2624 * Reset the cursor to the position of the new extent
2625 * we are about to insert as we can't trust it after
2626 * the previous insert.
2628 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
2629 new->br_startblock, new->br_blockcount,
2632 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
2633 /* new middle extent - newext */
2634 cur->bc_rec.b.br_state = new->br_state;
2635 if ((error = xfs_btree_insert(cur, &i)))
2637 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2641 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2642 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2643 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
2644 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2645 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2646 case BMAP_LEFT_CONTIG:
2647 case BMAP_RIGHT_CONTIG:
2649 * These cases are all impossible.
2654 /* update reverse mappings */
2655 error = xfs_rmap_convert_extent(mp, dfops, ip, whichfork, new);
2659 /* convert to a btree if necessary */
2660 if (xfs_bmap_needs_btree(ip, whichfork)) {
2661 int tmp_logflags; /* partial log flag return val */
2663 ASSERT(cur == NULL);
2664 error = xfs_bmap_extents_to_btree(tp, ip, first, dfops, &cur,
2665 0, &tmp_logflags, whichfork);
2666 *logflagsp |= tmp_logflags;
2671 /* clear out the allocated field, done with it now in any case. */
2673 cur->bc_private.b.allocated = 0;
2677 xfs_bmap_check_leaf_extents(*curp, ip, whichfork);
2687 * Convert a hole to a delayed allocation.
2690 xfs_bmap_add_extent_hole_delay(
2691 xfs_inode_t *ip, /* incore inode pointer */
2693 xfs_extnum_t *idx, /* extent number to update/insert */
2694 xfs_bmbt_irec_t *new) /* new data to add to file extents */
2696 xfs_ifork_t *ifp; /* inode fork pointer */
2697 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2698 xfs_filblks_t newlen=0; /* new indirect size */
2699 xfs_filblks_t oldlen=0; /* old indirect size */
2700 xfs_bmbt_irec_t right; /* right neighbor extent entry */
2701 int state; /* state bits, accessed thru macros */
2702 xfs_filblks_t temp=0; /* temp for indirect calculations */
2704 ifp = XFS_IFORK_PTR(ip, whichfork);
2706 if (whichfork == XFS_COW_FORK)
2707 state |= BMAP_COWFORK;
2708 ASSERT(isnullstartblock(new->br_startblock));
2711 * Check and set flags if this segment has a left neighbor
2714 state |= BMAP_LEFT_VALID;
2715 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &left);
2717 if (isnullstartblock(left.br_startblock))
2718 state |= BMAP_LEFT_DELAY;
2722 * Check and set flags if the current (right) segment exists.
2723 * If it doesn't exist, we're converting the hole at end-of-file.
2725 if (*idx < xfs_iext_count(ifp)) {
2726 state |= BMAP_RIGHT_VALID;
2727 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx), &right);
2729 if (isnullstartblock(right.br_startblock))
2730 state |= BMAP_RIGHT_DELAY;
2734 * Set contiguity flags on the left and right neighbors.
2735 * Don't let extents get too large, even if the pieces are contiguous.
2737 if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) &&
2738 left.br_startoff + left.br_blockcount == new->br_startoff &&
2739 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2740 state |= BMAP_LEFT_CONTIG;
2742 if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) &&
2743 new->br_startoff + new->br_blockcount == right.br_startoff &&
2744 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2745 (!(state & BMAP_LEFT_CONTIG) ||
2746 (left.br_blockcount + new->br_blockcount +
2747 right.br_blockcount <= MAXEXTLEN)))
2748 state |= BMAP_RIGHT_CONTIG;
2751 * Switch out based on the contiguity flags.
2753 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2754 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2756 * New allocation is contiguous with delayed allocations
2757 * on the left and on the right.
2758 * Merge all three into a single extent record.
2761 temp = left.br_blockcount + new->br_blockcount +
2762 right.br_blockcount;
2764 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2765 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp);
2766 oldlen = startblockval(left.br_startblock) +
2767 startblockval(new->br_startblock) +
2768 startblockval(right.br_startblock);
2769 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2771 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx),
2772 nullstartblock((int)newlen));
2773 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2775 xfs_iext_remove(ip, *idx + 1, 1, state);
2778 case BMAP_LEFT_CONTIG:
2780 * New allocation is contiguous with a delayed allocation
2782 * Merge the new allocation with the left neighbor.
2785 temp = left.br_blockcount + new->br_blockcount;
2787 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2788 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp);
2789 oldlen = startblockval(left.br_startblock) +
2790 startblockval(new->br_startblock);
2791 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2793 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx),
2794 nullstartblock((int)newlen));
2795 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2798 case BMAP_RIGHT_CONTIG:
2800 * New allocation is contiguous with a delayed allocation
2802 * Merge the new allocation with the right neighbor.
2804 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2805 temp = new->br_blockcount + right.br_blockcount;
2806 oldlen = startblockval(new->br_startblock) +
2807 startblockval(right.br_startblock);
2808 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2810 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx),
2812 nullstartblock((int)newlen), temp, right.br_state);
2813 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2818 * New allocation is not contiguous with another
2819 * delayed allocation.
2820 * Insert a new entry.
2822 oldlen = newlen = 0;
2823 xfs_iext_insert(ip, *idx, 1, new, state);
2826 if (oldlen != newlen) {
2827 ASSERT(oldlen > newlen);
2828 xfs_mod_fdblocks(ip->i_mount, (int64_t)(oldlen - newlen),
2831 * Nothing to do for disk quota accounting here.
2837 * Convert a hole to a real allocation.
2839 STATIC int /* error */
2840 xfs_bmap_add_extent_hole_real(
2841 struct xfs_trans *tp,
2842 struct xfs_inode *ip,
2845 struct xfs_btree_cur **curp,
2846 struct xfs_bmbt_irec *new,
2847 xfs_fsblock_t *first,
2848 struct xfs_defer_ops *dfops,
2851 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
2852 struct xfs_mount *mp = ip->i_mount;
2853 struct xfs_btree_cur *cur = *curp;
2854 int error; /* error return value */
2855 int i; /* temp state */
2856 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2857 xfs_bmbt_irec_t right; /* right neighbor extent entry */
2858 int rval=0; /* return value (logging flags) */
2859 int state; /* state bits, accessed thru macros */
2862 ASSERT(*idx <= xfs_iext_count(ifp));
2863 ASSERT(!isnullstartblock(new->br_startblock));
2864 ASSERT(!cur || !(cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
2866 XFS_STATS_INC(mp, xs_add_exlist);
2869 if (whichfork == XFS_ATTR_FORK)
2870 state |= BMAP_ATTRFORK;
2871 if (whichfork == XFS_COW_FORK)
2872 state |= BMAP_COWFORK;
2875 * Check and set flags if this segment has a left neighbor.
2878 state |= BMAP_LEFT_VALID;
2879 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &left);
2880 if (isnullstartblock(left.br_startblock))
2881 state |= BMAP_LEFT_DELAY;
2885 * Check and set flags if this segment has a current value.
2886 * Not true if we're inserting into the "hole" at eof.
2888 if (*idx < xfs_iext_count(ifp)) {
2889 state |= BMAP_RIGHT_VALID;
2890 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx), &right);
2891 if (isnullstartblock(right.br_startblock))
2892 state |= BMAP_RIGHT_DELAY;
2896 * We're inserting a real allocation between "left" and "right".
2897 * Set the contiguity flags. Don't let extents get too large.
2899 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2900 left.br_startoff + left.br_blockcount == new->br_startoff &&
2901 left.br_startblock + left.br_blockcount == new->br_startblock &&
2902 left.br_state == new->br_state &&
2903 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2904 state |= BMAP_LEFT_CONTIG;
2906 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2907 new->br_startoff + new->br_blockcount == right.br_startoff &&
2908 new->br_startblock + new->br_blockcount == right.br_startblock &&
2909 new->br_state == right.br_state &&
2910 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2911 (!(state & BMAP_LEFT_CONTIG) ||
2912 left.br_blockcount + new->br_blockcount +
2913 right.br_blockcount <= MAXEXTLEN))
2914 state |= BMAP_RIGHT_CONTIG;
2918 * Select which case we're in here, and implement it.
2920 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2921 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2923 * New allocation is contiguous with real allocations on the
2924 * left and on the right.
2925 * Merge all three into a single extent record.
2928 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2929 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
2930 left.br_blockcount + new->br_blockcount +
2931 right.br_blockcount);
2932 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2934 xfs_iext_remove(ip, *idx + 1, 1, state);
2936 XFS_IFORK_NEXT_SET(ip, whichfork,
2937 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
2939 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
2941 rval = XFS_ILOG_CORE;
2942 error = xfs_bmbt_lookup_eq(cur, right.br_startoff,
2943 right.br_startblock, right.br_blockcount,
2947 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2948 error = xfs_btree_delete(cur, &i);
2951 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2952 error = xfs_btree_decrement(cur, 0, &i);
2955 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2956 error = xfs_bmbt_update(cur, left.br_startoff,
2958 left.br_blockcount +
2959 new->br_blockcount +
2960 right.br_blockcount,
2967 case BMAP_LEFT_CONTIG:
2969 * New allocation is contiguous with a real allocation
2971 * Merge the new allocation with the left neighbor.
2974 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2975 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
2976 left.br_blockcount + new->br_blockcount);
2977 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2980 rval = xfs_ilog_fext(whichfork);
2983 error = xfs_bmbt_lookup_eq(cur, left.br_startoff,
2984 left.br_startblock, left.br_blockcount,
2988 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2989 error = xfs_bmbt_update(cur, left.br_startoff,
2991 left.br_blockcount +
2999 case BMAP_RIGHT_CONTIG:
3001 * New allocation is contiguous with a real allocation
3003 * Merge the new allocation with the right neighbor.
3005 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
3006 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx),
3007 new->br_startoff, new->br_startblock,
3008 new->br_blockcount + right.br_blockcount,
3010 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
3013 rval = xfs_ilog_fext(whichfork);
3016 error = xfs_bmbt_lookup_eq(cur,
3018 right.br_startblock,
3019 right.br_blockcount, &i);
3022 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
3023 error = xfs_bmbt_update(cur, new->br_startoff,
3025 new->br_blockcount +
3026 right.br_blockcount,
3035 * New allocation is not contiguous with another
3037 * Insert a new entry.
3039 xfs_iext_insert(ip, *idx, 1, new, state);
3040 XFS_IFORK_NEXT_SET(ip, whichfork,
3041 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
3043 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
3045 rval = XFS_ILOG_CORE;
3046 error = xfs_bmbt_lookup_eq(cur,
3049 new->br_blockcount, &i);
3052 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
3053 cur->bc_rec.b.br_state = new->br_state;
3054 error = xfs_btree_insert(cur, &i);
3057 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
3062 /* add reverse mapping */
3063 error = xfs_rmap_map_extent(mp, dfops, ip, whichfork, new);
3067 /* convert to a btree if necessary */
3068 if (xfs_bmap_needs_btree(ip, whichfork)) {
3069 int tmp_logflags; /* partial log flag return val */
3071 ASSERT(cur == NULL);
3072 error = xfs_bmap_extents_to_btree(tp, ip, first, dfops, curp,
3073 0, &tmp_logflags, whichfork);
3074 *logflagsp |= tmp_logflags;
3080 /* clear out the allocated field, done with it now in any case. */
3082 cur->bc_private.b.allocated = 0;
3084 xfs_bmap_check_leaf_extents(cur, ip, whichfork);
3091 * Functions used in the extent read, allocate and remove paths
3095 * Adjust the size of the new extent based on di_extsize and rt extsize.
3098 xfs_bmap_extsize_align(
3100 xfs_bmbt_irec_t *gotp, /* next extent pointer */
3101 xfs_bmbt_irec_t *prevp, /* previous extent pointer */
3102 xfs_extlen_t extsz, /* align to this extent size */
3103 int rt, /* is this a realtime inode? */
3104 int eof, /* is extent at end-of-file? */
3105 int delay, /* creating delalloc extent? */
3106 int convert, /* overwriting unwritten extent? */
3107 xfs_fileoff_t *offp, /* in/out: aligned offset */
3108 xfs_extlen_t *lenp) /* in/out: aligned length */
3110 xfs_fileoff_t orig_off; /* original offset */
3111 xfs_extlen_t orig_alen; /* original length */
3112 xfs_fileoff_t orig_end; /* original off+len */
3113 xfs_fileoff_t nexto; /* next file offset */
3114 xfs_fileoff_t prevo; /* previous file offset */
3115 xfs_fileoff_t align_off; /* temp for offset */
3116 xfs_extlen_t align_alen; /* temp for length */
3117 xfs_extlen_t temp; /* temp for calculations */
3122 orig_off = align_off = *offp;
3123 orig_alen = align_alen = *lenp;
3124 orig_end = orig_off + orig_alen;
3127 * If this request overlaps an existing extent, then don't
3128 * attempt to perform any additional alignment.
3130 if (!delay && !eof &&
3131 (orig_off >= gotp->br_startoff) &&
3132 (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
3137 * If the file offset is unaligned vs. the extent size
3138 * we need to align it. This will be possible unless
3139 * the file was previously written with a kernel that didn't
3140 * perform this alignment, or if a truncate shot us in the
3143 temp = do_mod(orig_off, extsz);
3149 /* Same adjustment for the end of the requested area. */
3150 temp = (align_alen % extsz);
3152 align_alen += extsz - temp;
3155 * For large extent hint sizes, the aligned extent might be larger than
3156 * MAXEXTLEN. In that case, reduce the size by an extsz so that it pulls
3157 * the length back under MAXEXTLEN. The outer allocation loops handle
3158 * short allocation just fine, so it is safe to do this. We only want to
3159 * do it when we are forced to, though, because it means more allocation
3160 * operations are required.
3162 while (align_alen > MAXEXTLEN)
3163 align_alen -= extsz;
3164 ASSERT(align_alen <= MAXEXTLEN);
3167 * If the previous block overlaps with this proposed allocation
3168 * then move the start forward without adjusting the length.
3170 if (prevp->br_startoff != NULLFILEOFF) {
3171 if (prevp->br_startblock == HOLESTARTBLOCK)
3172 prevo = prevp->br_startoff;
3174 prevo = prevp->br_startoff + prevp->br_blockcount;
3177 if (align_off != orig_off && align_off < prevo)
3180 * If the next block overlaps with this proposed allocation
3181 * then move the start back without adjusting the length,
3182 * but not before offset 0.
3183 * This may of course make the start overlap previous block,
3184 * and if we hit the offset 0 limit then the next block
3185 * can still overlap too.
3187 if (!eof && gotp->br_startoff != NULLFILEOFF) {
3188 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
3189 (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
3190 nexto = gotp->br_startoff + gotp->br_blockcount;
3192 nexto = gotp->br_startoff;
3194 nexto = NULLFILEOFF;
3196 align_off + align_alen != orig_end &&
3197 align_off + align_alen > nexto)
3198 align_off = nexto > align_alen ? nexto - align_alen : 0;
3200 * If we're now overlapping the next or previous extent that
3201 * means we can't fit an extsz piece in this hole. Just move
3202 * the start forward to the first valid spot and set
3203 * the length so we hit the end.
3205 if (align_off != orig_off && align_off < prevo)
3207 if (align_off + align_alen != orig_end &&
3208 align_off + align_alen > nexto &&
3209 nexto != NULLFILEOFF) {
3210 ASSERT(nexto > prevo);
3211 align_alen = nexto - align_off;
3215 * If realtime, and the result isn't a multiple of the realtime
3216 * extent size we need to remove blocks until it is.
3218 if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
3220 * We're not covering the original request, or
3221 * we won't be able to once we fix the length.
3223 if (orig_off < align_off ||
3224 orig_end > align_off + align_alen ||
3225 align_alen - temp < orig_alen)
3228 * Try to fix it by moving the start up.
3230 if (align_off + temp <= orig_off) {
3235 * Try to fix it by moving the end in.
3237 else if (align_off + align_alen - temp >= orig_end)
3240 * Set the start to the minimum then trim the length.
3243 align_alen -= orig_off - align_off;
3244 align_off = orig_off;
3245 align_alen -= align_alen % mp->m_sb.sb_rextsize;
3248 * Result doesn't cover the request, fail it.
3250 if (orig_off < align_off || orig_end > align_off + align_alen)
3253 ASSERT(orig_off >= align_off);
3254 /* see MAXEXTLEN handling above */
3255 ASSERT(orig_end <= align_off + align_alen ||
3256 align_alen + extsz > MAXEXTLEN);
3260 if (!eof && gotp->br_startoff != NULLFILEOFF)
3261 ASSERT(align_off + align_alen <= gotp->br_startoff);
3262 if (prevp->br_startoff != NULLFILEOFF)
3263 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
3271 #define XFS_ALLOC_GAP_UNITS 4
3275 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
3277 xfs_fsblock_t adjust; /* adjustment to block numbers */
3278 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
3279 xfs_mount_t *mp; /* mount point structure */
3280 int nullfb; /* true if ap->firstblock isn't set */
3281 int rt; /* true if inode is realtime */
3283 #define ISVALID(x,y) \
3285 (x) < mp->m_sb.sb_rblocks : \
3286 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
3287 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
3288 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
3290 mp = ap->ip->i_mount;
3291 nullfb = *ap->firstblock == NULLFSBLOCK;
3292 rt = XFS_IS_REALTIME_INODE(ap->ip) &&
3293 xfs_alloc_is_userdata(ap->datatype);
3294 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
3296 * If allocating at eof, and there's a previous real block,
3297 * try to use its last block as our starting point.
3299 if (ap->eof && ap->prev.br_startoff != NULLFILEOFF &&
3300 !isnullstartblock(ap->prev.br_startblock) &&
3301 ISVALID(ap->prev.br_startblock + ap->prev.br_blockcount,
3302 ap->prev.br_startblock)) {
3303 ap->blkno = ap->prev.br_startblock + ap->prev.br_blockcount;
3305 * Adjust for the gap between prevp and us.
3307 adjust = ap->offset -
3308 (ap->prev.br_startoff + ap->prev.br_blockcount);
3310 ISVALID(ap->blkno + adjust, ap->prev.br_startblock))
3311 ap->blkno += adjust;
3314 * If not at eof, then compare the two neighbor blocks.
3315 * Figure out whether either one gives us a good starting point,
3316 * and pick the better one.
3318 else if (!ap->eof) {
3319 xfs_fsblock_t gotbno; /* right side block number */
3320 xfs_fsblock_t gotdiff=0; /* right side difference */
3321 xfs_fsblock_t prevbno; /* left side block number */
3322 xfs_fsblock_t prevdiff=0; /* left side difference */
3325 * If there's a previous (left) block, select a requested
3326 * start block based on it.
3328 if (ap->prev.br_startoff != NULLFILEOFF &&
3329 !isnullstartblock(ap->prev.br_startblock) &&
3330 (prevbno = ap->prev.br_startblock +
3331 ap->prev.br_blockcount) &&
3332 ISVALID(prevbno, ap->prev.br_startblock)) {
3334 * Calculate gap to end of previous block.
3336 adjust = prevdiff = ap->offset -
3337 (ap->prev.br_startoff +
3338 ap->prev.br_blockcount);
3340 * Figure the startblock based on the previous block's
3341 * end and the gap size.
3343 * If the gap is large relative to the piece we're
3344 * allocating, or using it gives us an invalid block
3345 * number, then just use the end of the previous block.
3347 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
3348 ISVALID(prevbno + prevdiff,
3349 ap->prev.br_startblock))
3354 * If the firstblock forbids it, can't use it,
3357 if (!rt && !nullfb &&
3358 XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
3359 prevbno = NULLFSBLOCK;
3362 * No previous block or can't follow it, just default.
3365 prevbno = NULLFSBLOCK;
3367 * If there's a following (right) block, select a requested
3368 * start block based on it.
3370 if (!isnullstartblock(ap->got.br_startblock)) {
3372 * Calculate gap to start of next block.
3374 adjust = gotdiff = ap->got.br_startoff - ap->offset;
3376 * Figure the startblock based on the next block's
3377 * start and the gap size.
3379 gotbno = ap->got.br_startblock;
3382 * If the gap is large relative to the piece we're
3383 * allocating, or using it gives us an invalid block
3384 * number, then just use the start of the next block
3385 * offset by our length.
3387 if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
3388 ISVALID(gotbno - gotdiff, gotbno))
3390 else if (ISVALID(gotbno - ap->length, gotbno)) {
3391 gotbno -= ap->length;
3392 gotdiff += adjust - ap->length;
3396 * If the firstblock forbids it, can't use it,
3399 if (!rt && !nullfb &&
3400 XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
3401 gotbno = NULLFSBLOCK;
3404 * No next block, just default.
3407 gotbno = NULLFSBLOCK;
3409 * If both valid, pick the better one, else the only good
3410 * one, else ap->blkno is already set (to 0 or the inode block).
3412 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
3413 ap->blkno = prevdiff <= gotdiff ? prevbno : gotbno;
3414 else if (prevbno != NULLFSBLOCK)
3415 ap->blkno = prevbno;
3416 else if (gotbno != NULLFSBLOCK)
3423 xfs_bmap_longest_free_extent(
3424 struct xfs_trans *tp,
3429 struct xfs_mount *mp = tp->t_mountp;
3430 struct xfs_perag *pag;
3431 xfs_extlen_t longest;
3434 pag = xfs_perag_get(mp, ag);
3435 if (!pag->pagf_init) {
3436 error = xfs_alloc_pagf_init(mp, tp, ag, XFS_ALLOC_FLAG_TRYLOCK);
3440 if (!pag->pagf_init) {
3446 longest = xfs_alloc_longest_free_extent(mp, pag,
3447 xfs_alloc_min_freelist(mp, pag),
3448 xfs_ag_resv_needed(pag, XFS_AG_RESV_NONE));
3449 if (*blen < longest)
3458 xfs_bmap_select_minlen(
3459 struct xfs_bmalloca *ap,
3460 struct xfs_alloc_arg *args,
3464 if (notinit || *blen < ap->minlen) {
3466 * Since we did a BUF_TRYLOCK above, it is possible that
3467 * there is space for this request.
3469 args->minlen = ap->minlen;
3470 } else if (*blen < args->maxlen) {
3472 * If the best seen length is less than the request length,
3473 * use the best as the minimum.
3475 args->minlen = *blen;
3478 * Otherwise we've seen an extent as big as maxlen, use that
3481 args->minlen = args->maxlen;
3486 xfs_bmap_btalloc_nullfb(
3487 struct xfs_bmalloca *ap,
3488 struct xfs_alloc_arg *args,
3491 struct xfs_mount *mp = ap->ip->i_mount;
3492 xfs_agnumber_t ag, startag;
3496 args->type = XFS_ALLOCTYPE_START_BNO;
3497 args->total = ap->total;
3499 startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3500 if (startag == NULLAGNUMBER)
3503 while (*blen < args->maxlen) {
3504 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3509 if (++ag == mp->m_sb.sb_agcount)
3515 xfs_bmap_select_minlen(ap, args, blen, notinit);
3520 xfs_bmap_btalloc_filestreams(
3521 struct xfs_bmalloca *ap,
3522 struct xfs_alloc_arg *args,
3525 struct xfs_mount *mp = ap->ip->i_mount;
3530 args->type = XFS_ALLOCTYPE_NEAR_BNO;
3531 args->total = ap->total;
3533 ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3534 if (ag == NULLAGNUMBER)
3537 error = xfs_bmap_longest_free_extent(args->tp, ag, blen, ¬init);
3541 if (*blen < args->maxlen) {
3542 error = xfs_filestream_new_ag(ap, &ag);
3546 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3553 xfs_bmap_select_minlen(ap, args, blen, notinit);
3556 * Set the failure fallback case to look in the selected AG as stream
3559 ap->blkno = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0);
3565 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
3567 xfs_mount_t *mp; /* mount point structure */
3568 xfs_alloctype_t atype = 0; /* type for allocation routines */
3569 xfs_extlen_t align = 0; /* minimum allocation alignment */
3570 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
3572 xfs_alloc_arg_t args;
3574 xfs_extlen_t nextminlen = 0;
3575 int nullfb; /* true if ap->firstblock isn't set */
3583 mp = ap->ip->i_mount;
3585 /* stripe alignment for allocation is determined by mount parameters */
3587 if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC))
3588 stripe_align = mp->m_swidth;
3589 else if (mp->m_dalign)
3590 stripe_align = mp->m_dalign;
3592 if (ap->flags & XFS_BMAPI_COWFORK)
3593 align = xfs_get_cowextsz_hint(ap->ip);
3594 else if (xfs_alloc_is_userdata(ap->datatype))
3595 align = xfs_get_extsz_hint(ap->ip);
3597 error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev,
3598 align, 0, ap->eof, 0, ap->conv,
3599 &ap->offset, &ap->length);
3605 nullfb = *ap->firstblock == NULLFSBLOCK;
3606 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
3608 if (xfs_alloc_is_userdata(ap->datatype) &&
3609 xfs_inode_is_filestream(ap->ip)) {
3610 ag = xfs_filestream_lookup_ag(ap->ip);
3611 ag = (ag != NULLAGNUMBER) ? ag : 0;
3612 ap->blkno = XFS_AGB_TO_FSB(mp, ag, 0);
3614 ap->blkno = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
3617 ap->blkno = *ap->firstblock;
3619 xfs_bmap_adjacent(ap);
3622 * If allowed, use ap->blkno; otherwise must use firstblock since
3623 * it's in the right allocation group.
3625 if (nullfb || XFS_FSB_TO_AGNO(mp, ap->blkno) == fb_agno)
3628 ap->blkno = *ap->firstblock;
3630 * Normal allocation, done through xfs_alloc_vextent.
3632 tryagain = isaligned = 0;
3633 memset(&args, 0, sizeof(args));
3636 args.fsbno = ap->blkno;
3637 xfs_rmap_skip_owner_update(&args.oinfo);
3639 /* Trim the allocation back to the maximum an AG can fit. */
3640 args.maxlen = MIN(ap->length, mp->m_ag_max_usable);
3641 args.firstblock = *ap->firstblock;
3645 * Search for an allocation group with a single extent large
3646 * enough for the request. If one isn't found, then adjust
3647 * the minimum allocation size to the largest space found.
3649 if (xfs_alloc_is_userdata(ap->datatype) &&
3650 xfs_inode_is_filestream(ap->ip))
3651 error = xfs_bmap_btalloc_filestreams(ap, &args, &blen);
3653 error = xfs_bmap_btalloc_nullfb(ap, &args, &blen);
3656 } else if (ap->dfops->dop_low) {
3657 if (xfs_inode_is_filestream(ap->ip))
3658 args.type = XFS_ALLOCTYPE_FIRST_AG;
3660 args.type = XFS_ALLOCTYPE_START_BNO;
3661 args.total = args.minlen = ap->minlen;
3663 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3664 args.total = ap->total;
3665 args.minlen = ap->minlen;
3667 /* apply extent size hints if obtained earlier */
3670 if ((args.mod = (xfs_extlen_t)do_mod(ap->offset, args.prod)))
3671 args.mod = (xfs_extlen_t)(args.prod - args.mod);
3672 } else if (mp->m_sb.sb_blocksize >= PAGE_SIZE) {
3676 args.prod = PAGE_SIZE >> mp->m_sb.sb_blocklog;
3677 if ((args.mod = (xfs_extlen_t)(do_mod(ap->offset, args.prod))))
3678 args.mod = (xfs_extlen_t)(args.prod - args.mod);
3681 * If we are not low on available data blocks, and the
3682 * underlying logical volume manager is a stripe, and
3683 * the file offset is zero then try to allocate data
3684 * blocks on stripe unit boundary.
3685 * NOTE: ap->aeof is only set if the allocation length
3686 * is >= the stripe unit and the allocation offset is
3687 * at the end of file.
3689 if (!ap->dfops->dop_low && ap->aeof) {
3691 args.alignment = stripe_align;
3695 * Adjust for alignment
3697 if (blen > args.alignment && blen <= args.maxlen)
3698 args.minlen = blen - args.alignment;
3699 args.minalignslop = 0;
3702 * First try an exact bno allocation.
3703 * If it fails then do a near or start bno
3704 * allocation with alignment turned on.
3708 args.type = XFS_ALLOCTYPE_THIS_BNO;
3711 * Compute the minlen+alignment for the
3712 * next case. Set slop so that the value
3713 * of minlen+alignment+slop doesn't go up
3714 * between the calls.
3716 if (blen > stripe_align && blen <= args.maxlen)
3717 nextminlen = blen - stripe_align;
3719 nextminlen = args.minlen;
3720 if (nextminlen + stripe_align > args.minlen + 1)
3722 nextminlen + stripe_align -
3725 args.minalignslop = 0;
3729 args.minalignslop = 0;
3731 args.minleft = ap->minleft;
3732 args.wasdel = ap->wasdel;
3733 args.resv = XFS_AG_RESV_NONE;
3734 args.datatype = ap->datatype;
3735 if (ap->datatype & XFS_ALLOC_USERDATA_ZERO)
3738 error = xfs_alloc_vextent(&args);
3742 if (tryagain && args.fsbno == NULLFSBLOCK) {
3744 * Exact allocation failed. Now try with alignment
3748 args.fsbno = ap->blkno;
3749 args.alignment = stripe_align;
3750 args.minlen = nextminlen;
3751 args.minalignslop = 0;
3753 if ((error = xfs_alloc_vextent(&args)))
3756 if (isaligned && args.fsbno == NULLFSBLOCK) {
3758 * allocation failed, so turn off alignment and
3762 args.fsbno = ap->blkno;
3764 if ((error = xfs_alloc_vextent(&args)))
3767 if (args.fsbno == NULLFSBLOCK && nullfb &&
3768 args.minlen > ap->minlen) {
3769 args.minlen = ap->minlen;
3770 args.type = XFS_ALLOCTYPE_START_BNO;
3771 args.fsbno = ap->blkno;
3772 if ((error = xfs_alloc_vextent(&args)))
3775 if (args.fsbno == NULLFSBLOCK && nullfb) {
3777 args.type = XFS_ALLOCTYPE_FIRST_AG;
3778 args.total = ap->minlen;
3779 if ((error = xfs_alloc_vextent(&args)))
3781 ap->dfops->dop_low = true;
3783 if (args.fsbno != NULLFSBLOCK) {
3785 * check the allocation happened at the same or higher AG than
3786 * the first block that was allocated.
3788 ASSERT(*ap->firstblock == NULLFSBLOCK ||
3789 XFS_FSB_TO_AGNO(mp, *ap->firstblock) <=
3790 XFS_FSB_TO_AGNO(mp, args.fsbno));
3792 ap->blkno = args.fsbno;
3793 if (*ap->firstblock == NULLFSBLOCK)
3794 *ap->firstblock = args.fsbno;
3795 ASSERT(nullfb || fb_agno <= args.agno);
3796 ap->length = args.len;
3797 if (!(ap->flags & XFS_BMAPI_COWFORK))
3798 ap->ip->i_d.di_nblocks += args.len;
3799 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
3801 ap->ip->i_delayed_blks -= args.len;
3803 * Adjust the disk quota also. This was reserved
3806 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
3807 ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT :
3808 XFS_TRANS_DQ_BCOUNT,
3811 ap->blkno = NULLFSBLOCK;
3818 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
3819 * It figures out where to ask the underlying allocator to put the new extent.
3823 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
3825 if (XFS_IS_REALTIME_INODE(ap->ip) &&
3826 xfs_alloc_is_userdata(ap->datatype))
3827 return xfs_bmap_rtalloc(ap);
3828 return xfs_bmap_btalloc(ap);
3831 /* Trim extent to fit a logical block range. */
3834 struct xfs_bmbt_irec *irec,
3838 xfs_fileoff_t distance;
3839 xfs_fileoff_t end = bno + len;
3841 if (irec->br_startoff + irec->br_blockcount <= bno ||
3842 irec->br_startoff >= end) {
3843 irec->br_blockcount = 0;
3847 if (irec->br_startoff < bno) {
3848 distance = bno - irec->br_startoff;
3849 if (isnullstartblock(irec->br_startblock))
3850 irec->br_startblock = DELAYSTARTBLOCK;
3851 if (irec->br_startblock != DELAYSTARTBLOCK &&
3852 irec->br_startblock != HOLESTARTBLOCK)
3853 irec->br_startblock += distance;
3854 irec->br_startoff += distance;
3855 irec->br_blockcount -= distance;
3858 if (end < irec->br_startoff + irec->br_blockcount) {
3859 distance = irec->br_startoff + irec->br_blockcount - end;
3860 irec->br_blockcount -= distance;
3865 * Trim the returned map to the required bounds
3869 struct xfs_bmbt_irec *mval,
3870 struct xfs_bmbt_irec *got,
3878 if ((flags & XFS_BMAPI_ENTIRE) ||
3879 got->br_startoff + got->br_blockcount <= obno) {
3881 if (isnullstartblock(got->br_startblock))
3882 mval->br_startblock = DELAYSTARTBLOCK;
3888 ASSERT((*bno >= obno) || (n == 0));
3890 mval->br_startoff = *bno;
3891 if (isnullstartblock(got->br_startblock))
3892 mval->br_startblock = DELAYSTARTBLOCK;
3894 mval->br_startblock = got->br_startblock +
3895 (*bno - got->br_startoff);
3897 * Return the minimum of what we got and what we asked for for
3898 * the length. We can use the len variable here because it is
3899 * modified below and we could have been there before coming
3900 * here if the first part of the allocation didn't overlap what
3903 mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno,
3904 got->br_blockcount - (*bno - got->br_startoff));
3905 mval->br_state = got->br_state;
3906 ASSERT(mval->br_blockcount <= len);
3911 * Update and validate the extent map to return
3914 xfs_bmapi_update_map(
3915 struct xfs_bmbt_irec **map,
3923 xfs_bmbt_irec_t *mval = *map;
3925 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
3926 ((mval->br_startoff + mval->br_blockcount) <= end));
3927 ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) ||
3928 (mval->br_startoff < obno));
3930 *bno = mval->br_startoff + mval->br_blockcount;
3932 if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) {
3933 /* update previous map with new information */
3934 ASSERT(mval->br_startblock == mval[-1].br_startblock);
3935 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
3936 ASSERT(mval->br_state == mval[-1].br_state);
3937 mval[-1].br_blockcount = mval->br_blockcount;
3938 mval[-1].br_state = mval->br_state;
3939 } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
3940 mval[-1].br_startblock != DELAYSTARTBLOCK &&
3941 mval[-1].br_startblock != HOLESTARTBLOCK &&
3942 mval->br_startblock == mval[-1].br_startblock +
3943 mval[-1].br_blockcount &&
3944 ((flags & XFS_BMAPI_IGSTATE) ||
3945 mval[-1].br_state == mval->br_state)) {
3946 ASSERT(mval->br_startoff ==
3947 mval[-1].br_startoff + mval[-1].br_blockcount);
3948 mval[-1].br_blockcount += mval->br_blockcount;
3949 } else if (*n > 0 &&
3950 mval->br_startblock == DELAYSTARTBLOCK &&
3951 mval[-1].br_startblock == DELAYSTARTBLOCK &&
3952 mval->br_startoff ==
3953 mval[-1].br_startoff + mval[-1].br_blockcount) {
3954 mval[-1].br_blockcount += mval->br_blockcount;
3955 mval[-1].br_state = mval->br_state;
3956 } else if (!((*n == 0) &&
3957 ((mval->br_startoff + mval->br_blockcount) <=
3966 * Map file blocks to filesystem blocks without allocation.
3970 struct xfs_inode *ip,
3973 struct xfs_bmbt_irec *mval,
3977 struct xfs_mount *mp = ip->i_mount;
3978 struct xfs_ifork *ifp;
3979 struct xfs_bmbt_irec got;
3986 int whichfork = xfs_bmapi_whichfork(flags);
3989 ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK|XFS_BMAPI_ENTIRE|
3990 XFS_BMAPI_IGSTATE|XFS_BMAPI_COWFORK)));
3991 ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED|XFS_ILOCK_EXCL));
3993 if (unlikely(XFS_TEST_ERROR(
3994 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
3995 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
3996 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
3997 XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW, mp);
3998 return -EFSCORRUPTED;
4001 if (XFS_FORCED_SHUTDOWN(mp))
4004 XFS_STATS_INC(mp, xs_blk_mapr);
4006 ifp = XFS_IFORK_PTR(ip, whichfork);
4008 /* No CoW fork? Return a hole. */
4009 if (whichfork == XFS_COW_FORK && !ifp) {
4010 mval->br_startoff = bno;
4011 mval->br_startblock = HOLESTARTBLOCK;
4012 mval->br_blockcount = len;
4013 mval->br_state = XFS_EXT_NORM;
4018 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4019 error = xfs_iread_extents(NULL, ip, whichfork);
4024 if (!xfs_iext_lookup_extent(ip, ifp, bno, &idx, &got))
4029 while (bno < end && n < *nmap) {
4030 /* Reading past eof, act as though there's a hole up to end. */
4032 got.br_startoff = end;
4033 if (got.br_startoff > bno) {
4034 /* Reading in a hole. */
4035 mval->br_startoff = bno;
4036 mval->br_startblock = HOLESTARTBLOCK;
4037 mval->br_blockcount =
4038 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
4039 mval->br_state = XFS_EXT_NORM;
4040 bno += mval->br_blockcount;
4041 len -= mval->br_blockcount;
4047 /* set up the extent map to return. */
4048 xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
4049 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4051 /* If we're done, stop now. */
4052 if (bno >= end || n >= *nmap)
4055 /* Else go on to the next record. */
4056 if (!xfs_iext_get_extent(ifp, ++idx, &got))
4064 * Add a delayed allocation extent to an inode. Blocks are reserved from the
4065 * global pool and the extent inserted into the inode in-core extent tree.
4067 * On entry, got refers to the first extent beyond the offset of the extent to
4068 * allocate or eof is specified if no such extent exists. On return, got refers
4069 * to the extent record that was inserted to the inode fork.
4071 * Note that the allocated extent may have been merged with contiguous extents
4072 * during insertion into the inode fork. Thus, got does not reflect the current
4073 * state of the inode fork on return. If necessary, the caller can use lastx to
4074 * look up the updated record in the inode fork.
4077 xfs_bmapi_reserve_delalloc(
4078 struct xfs_inode *ip,
4082 xfs_filblks_t prealloc,
4083 struct xfs_bmbt_irec *got,
4084 xfs_extnum_t *lastx,
4087 struct xfs_mount *mp = ip->i_mount;
4088 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
4090 xfs_extlen_t indlen;
4091 char rt = XFS_IS_REALTIME_INODE(ip);
4094 xfs_fileoff_t aoff = off;
4097 * Cap the alloc length. Keep track of prealloc so we know whether to
4098 * tag the inode before we return.
4100 alen = XFS_FILBLKS_MIN(len + prealloc, MAXEXTLEN);
4102 alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff);
4103 if (prealloc && alen >= len)
4104 prealloc = alen - len;
4106 /* Figure out the extent size, adjust alen */
4107 if (whichfork == XFS_COW_FORK)
4108 extsz = xfs_get_cowextsz_hint(ip);
4110 extsz = xfs_get_extsz_hint(ip);
4112 struct xfs_bmbt_irec prev;
4114 if (!xfs_iext_get_extent(ifp, *lastx - 1, &prev))
4115 prev.br_startoff = NULLFILEOFF;
4117 error = xfs_bmap_extsize_align(mp, got, &prev, extsz, rt, eof,
4118 1, 0, &aoff, &alen);
4123 extsz = alen / mp->m_sb.sb_rextsize;
4126 * Make a transaction-less quota reservation for delayed allocation
4127 * blocks. This number gets adjusted later. We return if we haven't
4128 * allocated blocks already inside this loop.
4130 error = xfs_trans_reserve_quota_nblks(NULL, ip, (long)alen, 0,
4131 rt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
4136 * Split changing sb for alen and indlen since they could be coming
4137 * from different places.
4139 indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen);
4143 error = xfs_mod_frextents(mp, -((int64_t)extsz));
4145 error = xfs_mod_fdblocks(mp, -((int64_t)alen), false);
4149 goto out_unreserve_quota;
4151 error = xfs_mod_fdblocks(mp, -((int64_t)indlen), false);
4153 goto out_unreserve_blocks;
4156 ip->i_delayed_blks += alen;
4158 got->br_startoff = aoff;
4159 got->br_startblock = nullstartblock(indlen);
4160 got->br_blockcount = alen;
4161 got->br_state = XFS_EXT_NORM;
4163 xfs_bmap_add_extent_hole_delay(ip, whichfork, lastx, got);
4166 * Tag the inode if blocks were preallocated. Note that COW fork
4167 * preallocation can occur at the start or end of the extent, even when
4168 * prealloc == 0, so we must also check the aligned offset and length.
4170 if (whichfork == XFS_DATA_FORK && prealloc)
4171 xfs_inode_set_eofblocks_tag(ip);
4172 if (whichfork == XFS_COW_FORK && (prealloc || aoff < off || alen > len))
4173 xfs_inode_set_cowblocks_tag(ip);
4177 out_unreserve_blocks:
4179 xfs_mod_frextents(mp, extsz);
4181 xfs_mod_fdblocks(mp, alen, false);
4182 out_unreserve_quota:
4183 if (XFS_IS_QUOTA_ON(mp))
4184 xfs_trans_unreserve_quota_nblks(NULL, ip, (long)alen, 0, rt ?
4185 XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
4191 struct xfs_bmalloca *bma)
4193 struct xfs_mount *mp = bma->ip->i_mount;
4194 int whichfork = xfs_bmapi_whichfork(bma->flags);
4195 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
4196 int tmp_logflags = 0;
4199 ASSERT(bma->length > 0);
4202 * For the wasdelay case, we could also just allocate the stuff asked
4203 * for in this bmap call but that wouldn't be as good.
4206 bma->length = (xfs_extlen_t)bma->got.br_blockcount;
4207 bma->offset = bma->got.br_startoff;
4209 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1),
4213 bma->length = XFS_FILBLKS_MIN(bma->length, MAXEXTLEN);
4215 bma->length = XFS_FILBLKS_MIN(bma->length,
4216 bma->got.br_startoff - bma->offset);
4220 * Set the data type being allocated. For the data fork, the first data
4221 * in the file is treated differently to all other allocations. For the
4222 * attribute fork, we only need to ensure the allocated range is not on
4225 if (!(bma->flags & XFS_BMAPI_METADATA)) {
4226 bma->datatype = XFS_ALLOC_NOBUSY;
4227 if (whichfork == XFS_DATA_FORK) {
4228 if (bma->offset == 0)
4229 bma->datatype |= XFS_ALLOC_INITIAL_USER_DATA;
4231 bma->datatype |= XFS_ALLOC_USERDATA;
4233 if (bma->flags & XFS_BMAPI_ZERO)
4234 bma->datatype |= XFS_ALLOC_USERDATA_ZERO;
4237 bma->minlen = (bma->flags & XFS_BMAPI_CONTIG) ? bma->length : 1;
4240 * Only want to do the alignment at the eof if it is userdata and
4241 * allocation length is larger than a stripe unit.
4243 if (mp->m_dalign && bma->length >= mp->m_dalign &&
4244 !(bma->flags & XFS_BMAPI_METADATA) && whichfork == XFS_DATA_FORK) {
4245 error = xfs_bmap_isaeof(bma, whichfork);
4250 error = xfs_bmap_alloc(bma);
4255 bma->cur->bc_private.b.firstblock = *bma->firstblock;
4256 if (bma->blkno == NULLFSBLOCK)
4258 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4259 bma->cur = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork);
4260 bma->cur->bc_private.b.firstblock = *bma->firstblock;
4261 bma->cur->bc_private.b.dfops = bma->dfops;
4264 * Bump the number of extents we've allocated
4270 bma->cur->bc_private.b.flags =
4271 bma->wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
4273 bma->got.br_startoff = bma->offset;
4274 bma->got.br_startblock = bma->blkno;
4275 bma->got.br_blockcount = bma->length;
4276 bma->got.br_state = XFS_EXT_NORM;
4279 * In the data fork, a wasdelay extent has been initialized, so
4280 * shouldn't be flagged as unwritten.
4282 * For the cow fork, however, we convert delalloc reservations
4283 * (extents allocated for speculative preallocation) to
4284 * allocated unwritten extents, and only convert the unwritten
4285 * extents to real extents when we're about to write the data.
4287 if ((!bma->wasdel || (bma->flags & XFS_BMAPI_COWFORK)) &&
4288 (bma->flags & XFS_BMAPI_PREALLOC) &&
4289 xfs_sb_version_hasextflgbit(&mp->m_sb))
4290 bma->got.br_state = XFS_EXT_UNWRITTEN;
4293 error = xfs_bmap_add_extent_delay_real(bma, whichfork);
4295 error = xfs_bmap_add_extent_hole_real(bma->tp, bma->ip,
4296 whichfork, &bma->idx, &bma->cur, &bma->got,
4297 bma->firstblock, bma->dfops, &bma->logflags);
4299 bma->logflags |= tmp_logflags;
4304 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4305 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4306 * the neighbouring ones.
4308 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &bma->got);
4310 ASSERT(bma->got.br_startoff <= bma->offset);
4311 ASSERT(bma->got.br_startoff + bma->got.br_blockcount >=
4312 bma->offset + bma->length);
4313 ASSERT(bma->got.br_state == XFS_EXT_NORM ||
4314 bma->got.br_state == XFS_EXT_UNWRITTEN);
4319 xfs_bmapi_convert_unwritten(
4320 struct xfs_bmalloca *bma,
4321 struct xfs_bmbt_irec *mval,
4325 int whichfork = xfs_bmapi_whichfork(flags);
4326 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
4327 int tmp_logflags = 0;
4330 /* check if we need to do unwritten->real conversion */
4331 if (mval->br_state == XFS_EXT_UNWRITTEN &&
4332 (flags & XFS_BMAPI_PREALLOC))
4335 /* check if we need to do real->unwritten conversion */
4336 if (mval->br_state == XFS_EXT_NORM &&
4337 (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) !=
4338 (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT))
4342 * Modify (by adding) the state flag, if writing.
4344 ASSERT(mval->br_blockcount <= len);
4345 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4346 bma->cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp,
4347 bma->ip, whichfork);
4348 bma->cur->bc_private.b.firstblock = *bma->firstblock;
4349 bma->cur->bc_private.b.dfops = bma->dfops;
4351 mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN)
4352 ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
4355 * Before insertion into the bmbt, zero the range being converted
4358 if (flags & XFS_BMAPI_ZERO) {
4359 error = xfs_zero_extent(bma->ip, mval->br_startblock,
4360 mval->br_blockcount);
4365 error = xfs_bmap_add_extent_unwritten_real(bma->tp, bma->ip, whichfork,
4366 &bma->idx, &bma->cur, mval, bma->firstblock, bma->dfops,
4369 * Log the inode core unconditionally in the unwritten extent conversion
4370 * path because the conversion might not have done so (e.g., if the
4371 * extent count hasn't changed). We need to make sure the inode is dirty
4372 * in the transaction for the sake of fsync(), even if nothing has
4373 * changed, because fsync() will not force the log for this transaction
4374 * unless it sees the inode pinned.
4376 * Note: If we're only converting cow fork extents, there aren't
4377 * any on-disk updates to make, so we don't need to log anything.
4379 if (whichfork != XFS_COW_FORK)
4380 bma->logflags |= tmp_logflags | XFS_ILOG_CORE;
4385 * Update our extent pointer, given that
4386 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4387 * of the neighbouring ones.
4389 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &bma->got);
4392 * We may have combined previously unwritten space with written space,
4393 * so generate another request.
4395 if (mval->br_blockcount < len)
4401 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4402 * extent state if necessary. Details behaviour is controlled by the flags
4403 * parameter. Only allocates blocks from a single allocation group, to avoid
4406 * The returned value in "firstblock" from the first call in a transaction
4407 * must be remembered and presented to subsequent calls in "firstblock".
4408 * An upper bound for the number of blocks to be allocated is supplied to
4409 * the first call in "total"; if no allocation group has that many free
4410 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4414 struct xfs_trans *tp, /* transaction pointer */
4415 struct xfs_inode *ip, /* incore inode */
4416 xfs_fileoff_t bno, /* starting file offs. mapped */
4417 xfs_filblks_t len, /* length to map in file */
4418 int flags, /* XFS_BMAPI_... */
4419 xfs_fsblock_t *firstblock, /* first allocated block
4420 controls a.g. for allocs */
4421 xfs_extlen_t total, /* total blocks needed */
4422 struct xfs_bmbt_irec *mval, /* output: map values */
4423 int *nmap, /* i/o: mval size/count */
4424 struct xfs_defer_ops *dfops) /* i/o: list extents to free */
4426 struct xfs_mount *mp = ip->i_mount;
4427 struct xfs_ifork *ifp;
4428 struct xfs_bmalloca bma = { NULL }; /* args for xfs_bmap_alloc */
4429 xfs_fileoff_t end; /* end of mapped file region */
4430 bool eof = false; /* after the end of extents */
4431 int error; /* error return */
4432 int n; /* current extent index */
4433 xfs_fileoff_t obno; /* old block number (offset) */
4434 int whichfork; /* data or attr fork */
4437 xfs_fileoff_t orig_bno; /* original block number value */
4438 int orig_flags; /* original flags arg value */
4439 xfs_filblks_t orig_len; /* original value of len arg */
4440 struct xfs_bmbt_irec *orig_mval; /* original value of mval */
4441 int orig_nmap; /* original value of *nmap */
4449 whichfork = xfs_bmapi_whichfork(flags);
4452 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP);
4453 ASSERT(!(flags & XFS_BMAPI_IGSTATE));
4454 ASSERT(tp != NULL ||
4455 (flags & (XFS_BMAPI_CONVERT | XFS_BMAPI_COWFORK)) ==
4456 (XFS_BMAPI_CONVERT | XFS_BMAPI_COWFORK));
4458 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL);
4459 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
4460 ASSERT(!(flags & XFS_BMAPI_REMAP));
4462 /* zeroing is for currently only for data extents, not metadata */
4463 ASSERT((flags & (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO)) !=
4464 (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO));
4466 * we can allocate unwritten extents or pre-zero allocated blocks,
4467 * but it makes no sense to do both at once. This would result in
4468 * zeroing the unwritten extent twice, but it still being an
4469 * unwritten extent....
4471 ASSERT((flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)) !=
4472 (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO));
4474 if (unlikely(XFS_TEST_ERROR(
4475 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4476 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
4477 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
4478 XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW, mp);
4479 return -EFSCORRUPTED;
4482 if (XFS_FORCED_SHUTDOWN(mp))
4485 ifp = XFS_IFORK_PTR(ip, whichfork);
4487 XFS_STATS_INC(mp, xs_blk_mapw);
4489 if (*firstblock == NULLFSBLOCK) {
4490 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE)
4491 bma.minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1;
4498 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4499 error = xfs_iread_extents(tp, ip, whichfork);
4508 if (!xfs_iext_lookup_extent(ip, ifp, bno, &bma.idx, &bma.got))
4510 if (!xfs_iext_get_extent(ifp, bma.idx - 1, &bma.prev))
4511 bma.prev.br_startoff = NULLFILEOFF;
4517 bma.firstblock = firstblock;
4519 while (bno < end && n < *nmap) {
4520 bool need_alloc = false, wasdelay = false;
4522 /* in hole or beyoned EOF? */
4523 if (eof || bma.got.br_startoff > bno) {
4524 if (flags & XFS_BMAPI_DELALLOC) {
4526 * For the COW fork we can reasonably get a
4527 * request for converting an extent that races
4528 * with other threads already having converted
4529 * part of it, as there converting COW to
4530 * regular blocks is not protected using the
4533 ASSERT(flags & XFS_BMAPI_COWFORK);
4534 if (!(flags & XFS_BMAPI_COWFORK)) {
4539 if (eof || bno >= end)
4544 } else if (isnullstartblock(bma.got.br_startblock)) {
4549 * First, deal with the hole before the allocated space
4550 * that we found, if any.
4552 if (need_alloc || wasdelay) {
4554 bma.conv = !!(flags & XFS_BMAPI_CONVERT);
4555 bma.wasdel = wasdelay;
4560 * There's a 32/64 bit type mismatch between the
4561 * allocation length request (which can be 64 bits in
4562 * length) and the bma length request, which is
4563 * xfs_extlen_t and therefore 32 bits. Hence we have to
4564 * check for 32-bit overflows and handle them here.
4566 if (len > (xfs_filblks_t)MAXEXTLEN)
4567 bma.length = MAXEXTLEN;
4572 ASSERT(bma.length > 0);
4573 error = xfs_bmapi_allocate(&bma);
4576 if (bma.blkno == NULLFSBLOCK)
4580 * If this is a CoW allocation, record the data in
4581 * the refcount btree for orphan recovery.
4583 if (whichfork == XFS_COW_FORK) {
4584 error = xfs_refcount_alloc_cow_extent(mp, dfops,
4585 bma.blkno, bma.length);
4591 /* Deal with the allocated space we found. */
4592 xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno,
4595 /* Execute unwritten extent conversion if necessary */
4596 error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags);
4597 if (error == -EAGAIN)
4602 /* update the extent map to return */
4603 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4606 * If we're done, stop now. Stop when we've allocated
4607 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4608 * the transaction may get too big.
4610 if (bno >= end || n >= *nmap || bma.nallocs >= *nmap)
4613 /* Else go on to the next record. */
4615 if (!xfs_iext_get_extent(ifp, ++bma.idx, &bma.got))
4621 * Transform from btree to extents, give it cur.
4623 if (xfs_bmap_wants_extents(ip, whichfork)) {
4624 int tmp_logflags = 0;
4627 error = xfs_bmap_btree_to_extents(tp, ip, bma.cur,
4628 &tmp_logflags, whichfork);
4629 bma.logflags |= tmp_logflags;
4634 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE ||
4635 XFS_IFORK_NEXTENTS(ip, whichfork) >
4636 XFS_IFORK_MAXEXT(ip, whichfork));
4640 * Log everything. Do this after conversion, there's no point in
4641 * logging the extent records if we've converted to btree format.
4643 if ((bma.logflags & xfs_ilog_fext(whichfork)) &&
4644 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
4645 bma.logflags &= ~xfs_ilog_fext(whichfork);
4646 else if ((bma.logflags & xfs_ilog_fbroot(whichfork)) &&
4647 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
4648 bma.logflags &= ~xfs_ilog_fbroot(whichfork);
4650 * Log whatever the flags say, even if error. Otherwise we might miss
4651 * detecting a case where the data is changed, there's an error,
4652 * and it's not logged so we don't shutdown when we should.
4655 xfs_trans_log_inode(tp, ip, bma.logflags);
4659 ASSERT(*firstblock == NULLFSBLOCK ||
4660 XFS_FSB_TO_AGNO(mp, *firstblock) <=
4662 bma.cur->bc_private.b.firstblock));
4663 *firstblock = bma.cur->bc_private.b.firstblock;
4665 xfs_btree_del_cursor(bma.cur,
4666 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
4669 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
4676 struct xfs_trans *tp,
4677 struct xfs_inode *ip,
4680 xfs_fsblock_t startblock,
4681 struct xfs_defer_ops *dfops)
4683 struct xfs_mount *mp = ip->i_mount;
4684 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
4685 struct xfs_btree_cur *cur = NULL;
4686 xfs_fsblock_t firstblock = NULLFSBLOCK;
4687 struct xfs_bmbt_irec got;
4689 int logflags = 0, error;
4692 ASSERT(len <= (xfs_filblks_t)MAXEXTLEN);
4693 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
4695 if (unlikely(XFS_TEST_ERROR(
4696 (XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_EXTENTS &&
4697 XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_BTREE),
4698 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
4699 XFS_ERROR_REPORT("xfs_bmapi_remap", XFS_ERRLEVEL_LOW, mp);
4700 return -EFSCORRUPTED;
4703 if (XFS_FORCED_SHUTDOWN(mp))
4706 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4707 error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK);
4712 if (xfs_iext_lookup_extent(ip, ifp, bno, &idx, &got)) {
4713 /* make sure we only reflink into a hole. */
4714 ASSERT(got.br_startoff > bno);
4715 ASSERT(got.br_startoff - bno >= len);
4718 ip->i_d.di_nblocks += len;
4719 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
4721 if (ifp->if_flags & XFS_IFBROOT) {
4722 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
4723 cur->bc_private.b.firstblock = firstblock;
4724 cur->bc_private.b.dfops = dfops;
4725 cur->bc_private.b.flags = 0;
4728 got.br_startoff = bno;
4729 got.br_startblock = startblock;
4730 got.br_blockcount = len;
4731 got.br_state = XFS_EXT_NORM;
4733 error = xfs_bmap_add_extent_hole_real(tp, ip, XFS_DATA_FORK, &idx, &cur,
4734 &got, &firstblock, dfops, &logflags);
4738 if (xfs_bmap_wants_extents(ip, XFS_DATA_FORK)) {
4739 int tmp_logflags = 0;
4741 error = xfs_bmap_btree_to_extents(tp, ip, cur,
4742 &tmp_logflags, XFS_DATA_FORK);
4743 logflags |= tmp_logflags;
4747 if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS)
4748 logflags &= ~XFS_ILOG_DEXT;
4749 else if (ip->i_d.di_format != XFS_DINODE_FMT_BTREE)
4750 logflags &= ~XFS_ILOG_DBROOT;
4753 xfs_trans_log_inode(tp, ip, logflags);
4755 xfs_btree_del_cursor(cur,
4756 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
4762 * When a delalloc extent is split (e.g., due to a hole punch), the original
4763 * indlen reservation must be shared across the two new extents that are left
4766 * Given the original reservation and the worst case indlen for the two new
4767 * extents (as calculated by xfs_bmap_worst_indlen()), split the original
4768 * reservation fairly across the two new extents. If necessary, steal available
4769 * blocks from a deleted extent to make up a reservation deficiency (e.g., if
4770 * ores == 1). The number of stolen blocks is returned. The availability and
4771 * subsequent accounting of stolen blocks is the responsibility of the caller.
4773 static xfs_filblks_t
4774 xfs_bmap_split_indlen(
4775 xfs_filblks_t ores, /* original res. */
4776 xfs_filblks_t *indlen1, /* ext1 worst indlen */
4777 xfs_filblks_t *indlen2, /* ext2 worst indlen */
4778 xfs_filblks_t avail) /* stealable blocks */
4780 xfs_filblks_t len1 = *indlen1;
4781 xfs_filblks_t len2 = *indlen2;
4782 xfs_filblks_t nres = len1 + len2; /* new total res. */
4783 xfs_filblks_t stolen = 0;
4784 xfs_filblks_t resfactor;
4787 * Steal as many blocks as we can to try and satisfy the worst case
4788 * indlen for both new extents.
4790 if (ores < nres && avail)
4791 stolen = XFS_FILBLKS_MIN(nres - ores, avail);
4794 /* nothing else to do if we've satisfied the new reservation */
4799 * We can't meet the total required reservation for the two extents.
4800 * Calculate the percent of the overall shortage between both extents
4801 * and apply this percentage to each of the requested indlen values.
4802 * This distributes the shortage fairly and reduces the chances that one
4803 * of the two extents is left with nothing when extents are repeatedly
4806 resfactor = (ores * 100);
4807 do_div(resfactor, nres);
4812 ASSERT(len1 + len2 <= ores);
4813 ASSERT(len1 < *indlen1 && len2 < *indlen2);
4816 * Hand out the remainder to each extent. If one of the two reservations
4817 * is zero, we want to make sure that one gets a block first. The loop
4818 * below starts with len1, so hand len2 a block right off the bat if it
4821 ores -= (len1 + len2);
4822 ASSERT((*indlen1 - len1) + (*indlen2 - len2) >= ores);
4823 if (ores && !len2 && *indlen2) {
4828 if (len1 < *indlen1) {
4834 if (len2 < *indlen2) {
4847 xfs_bmap_del_extent_delay(
4848 struct xfs_inode *ip,
4851 struct xfs_bmbt_irec *got,
4852 struct xfs_bmbt_irec *del)
4854 struct xfs_mount *mp = ip->i_mount;
4855 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
4856 struct xfs_bmbt_irec new;
4857 int64_t da_old, da_new, da_diff = 0;
4858 xfs_fileoff_t del_endoff, got_endoff;
4859 xfs_filblks_t got_indlen, new_indlen, stolen;
4860 int error = 0, state = 0;
4863 XFS_STATS_INC(mp, xs_del_exlist);
4865 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
4866 del_endoff = del->br_startoff + del->br_blockcount;
4867 got_endoff = got->br_startoff + got->br_blockcount;
4868 da_old = startblockval(got->br_startblock);
4872 ASSERT(*idx <= xfs_iext_count(ifp));
4873 ASSERT(del->br_blockcount > 0);
4874 ASSERT(got->br_startoff <= del->br_startoff);
4875 ASSERT(got_endoff >= del_endoff);
4878 uint64_t rtexts = XFS_FSB_TO_B(mp, del->br_blockcount);
4880 do_div(rtexts, mp->m_sb.sb_rextsize);
4881 xfs_mod_frextents(mp, rtexts);
4885 * Update the inode delalloc counter now and wait to update the
4886 * sb counters as we might have to borrow some blocks for the
4887 * indirect block accounting.
4889 error = xfs_trans_reserve_quota_nblks(NULL, ip,
4890 -((long)del->br_blockcount), 0,
4891 isrt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
4894 ip->i_delayed_blks -= del->br_blockcount;
4896 if (whichfork == XFS_COW_FORK)
4897 state |= BMAP_COWFORK;
4899 if (got->br_startoff == del->br_startoff)
4900 state |= BMAP_LEFT_CONTIG;
4901 if (got_endoff == del_endoff)
4902 state |= BMAP_RIGHT_CONTIG;
4904 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
4905 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
4907 * Matches the whole extent. Delete the entry.
4909 xfs_iext_remove(ip, *idx, 1, state);
4912 case BMAP_LEFT_CONTIG:
4914 * Deleting the first part of the extent.
4916 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4917 got->br_startoff = del_endoff;
4918 got->br_blockcount -= del->br_blockcount;
4919 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
4920 got->br_blockcount), da_old);
4921 got->br_startblock = nullstartblock((int)da_new);
4922 xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got);
4923 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
4925 case BMAP_RIGHT_CONTIG:
4927 * Deleting the last part of the extent.
4929 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4930 got->br_blockcount = got->br_blockcount - del->br_blockcount;
4931 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
4932 got->br_blockcount), da_old);
4933 got->br_startblock = nullstartblock((int)da_new);
4934 xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got);
4935 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
4939 * Deleting the middle of the extent.
4941 * Distribute the original indlen reservation across the two new
4942 * extents. Steal blocks from the deleted extent if necessary.
4943 * Stealing blocks simply fudges the fdblocks accounting below.
4944 * Warn if either of the new indlen reservations is zero as this
4945 * can lead to delalloc problems.
4947 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4949 got->br_blockcount = del->br_startoff - got->br_startoff;
4950 got_indlen = xfs_bmap_worst_indlen(ip, got->br_blockcount);
4952 new.br_blockcount = got_endoff - del_endoff;
4953 new_indlen = xfs_bmap_worst_indlen(ip, new.br_blockcount);
4955 WARN_ON_ONCE(!got_indlen || !new_indlen);
4956 stolen = xfs_bmap_split_indlen(da_old, &got_indlen, &new_indlen,
4957 del->br_blockcount);
4959 got->br_startblock = nullstartblock((int)got_indlen);
4960 xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got);
4961 trace_xfs_bmap_post_update(ip, *idx, 0, _THIS_IP_);
4963 new.br_startoff = del_endoff;
4964 new.br_state = got->br_state;
4965 new.br_startblock = nullstartblock((int)new_indlen);
4968 xfs_iext_insert(ip, *idx, 1, &new, state);
4970 da_new = got_indlen + new_indlen - stolen;
4971 del->br_blockcount -= stolen;
4975 ASSERT(da_old >= da_new);
4976 da_diff = da_old - da_new;
4978 da_diff += del->br_blockcount;
4980 xfs_mod_fdblocks(mp, da_diff, false);
4985 xfs_bmap_del_extent_cow(
4986 struct xfs_inode *ip,
4988 struct xfs_bmbt_irec *got,
4989 struct xfs_bmbt_irec *del)
4991 struct xfs_mount *mp = ip->i_mount;
4992 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
4993 struct xfs_bmbt_irec new;
4994 xfs_fileoff_t del_endoff, got_endoff;
4995 int state = BMAP_COWFORK;
4997 XFS_STATS_INC(mp, xs_del_exlist);
4999 del_endoff = del->br_startoff + del->br_blockcount;
5000 got_endoff = got->br_startoff + got->br_blockcount;
5003 ASSERT(*idx <= xfs_iext_count(ifp));
5004 ASSERT(del->br_blockcount > 0);
5005 ASSERT(got->br_startoff <= del->br_startoff);
5006 ASSERT(got_endoff >= del_endoff);
5007 ASSERT(!isnullstartblock(got->br_startblock));
5009 if (got->br_startoff == del->br_startoff)
5010 state |= BMAP_LEFT_CONTIG;
5011 if (got_endoff == del_endoff)
5012 state |= BMAP_RIGHT_CONTIG;
5014 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
5015 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
5017 * Matches the whole extent. Delete the entry.
5019 xfs_iext_remove(ip, *idx, 1, state);
5022 case BMAP_LEFT_CONTIG:
5024 * Deleting the first part of the extent.
5026 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5027 got->br_startoff = del_endoff;
5028 got->br_blockcount -= del->br_blockcount;
5029 got->br_startblock = del->br_startblock + del->br_blockcount;
5030 xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got);
5031 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5033 case BMAP_RIGHT_CONTIG:
5035 * Deleting the last part of the extent.
5037 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5038 got->br_blockcount -= del->br_blockcount;
5039 xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got);
5040 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5044 * Deleting the middle of the extent.
5046 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5047 got->br_blockcount = del->br_startoff - got->br_startoff;
5048 xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got);
5049 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5051 new.br_startoff = del_endoff;
5052 new.br_blockcount = got_endoff - del_endoff;
5053 new.br_state = got->br_state;
5054 new.br_startblock = del->br_startblock + del->br_blockcount;
5057 xfs_iext_insert(ip, *idx, 1, &new, state);
5063 * Called by xfs_bmapi to update file extent records and the btree
5064 * after removing space (or undoing a delayed allocation).
5066 STATIC int /* error */
5067 xfs_bmap_del_extent(
5068 xfs_inode_t *ip, /* incore inode pointer */
5069 xfs_trans_t *tp, /* current transaction pointer */
5070 xfs_extnum_t *idx, /* extent number to update/delete */
5071 struct xfs_defer_ops *dfops, /* list of extents to be freed */
5072 xfs_btree_cur_t *cur, /* if null, not a btree */
5073 xfs_bmbt_irec_t *del, /* data to remove from extents */
5074 int *logflagsp, /* inode logging flags */
5075 int whichfork, /* data or attr fork */
5076 int bflags) /* bmapi flags */
5078 xfs_filblks_t da_new; /* new delay-alloc indirect blocks */
5079 xfs_filblks_t da_old; /* old delay-alloc indirect blocks */
5080 xfs_fsblock_t del_endblock=0; /* first block past del */
5081 xfs_fileoff_t del_endoff; /* first offset past del */
5082 int delay; /* current block is delayed allocated */
5083 int do_fx; /* free extent at end of routine */
5084 xfs_bmbt_rec_host_t *ep; /* current extent entry pointer */
5085 int error; /* error return value */
5086 int flags; /* inode logging flags */
5087 xfs_bmbt_irec_t got; /* current extent entry */
5088 xfs_fileoff_t got_endoff; /* first offset past got */
5089 int i; /* temp state */
5090 xfs_ifork_t *ifp; /* inode fork pointer */
5091 xfs_mount_t *mp; /* mount structure */
5092 xfs_filblks_t nblks; /* quota/sb block count */
5093 xfs_bmbt_irec_t new; /* new record to be inserted */
5095 uint qfield; /* quota field to update */
5096 xfs_filblks_t temp; /* for indirect length calculations */
5097 xfs_filblks_t temp2; /* for indirect length calculations */
5101 XFS_STATS_INC(mp, xs_del_exlist);
5103 if (whichfork == XFS_ATTR_FORK)
5104 state |= BMAP_ATTRFORK;
5105 else if (whichfork == XFS_COW_FORK)
5106 state |= BMAP_COWFORK;
5108 ifp = XFS_IFORK_PTR(ip, whichfork);
5109 ASSERT((*idx >= 0) && (*idx < xfs_iext_count(ifp)));
5110 ASSERT(del->br_blockcount > 0);
5111 ep = xfs_iext_get_ext(ifp, *idx);
5112 xfs_bmbt_get_all(ep, &got);
5113 ASSERT(got.br_startoff <= del->br_startoff);
5114 del_endoff = del->br_startoff + del->br_blockcount;
5115 got_endoff = got.br_startoff + got.br_blockcount;
5116 ASSERT(got_endoff >= del_endoff);
5117 delay = isnullstartblock(got.br_startblock);
5118 ASSERT(isnullstartblock(del->br_startblock) == delay);
5123 * If deleting a real allocation, must free up the disk space.
5126 flags = XFS_ILOG_CORE;
5128 * Realtime allocation. Free it and record di_nblocks update.
5130 if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) {
5134 ASSERT(do_mod(del->br_blockcount,
5135 mp->m_sb.sb_rextsize) == 0);
5136 ASSERT(do_mod(del->br_startblock,
5137 mp->m_sb.sb_rextsize) == 0);
5138 bno = del->br_startblock;
5139 len = del->br_blockcount;
5140 do_div(bno, mp->m_sb.sb_rextsize);
5141 do_div(len, mp->m_sb.sb_rextsize);
5142 error = xfs_rtfree_extent(tp, bno, (xfs_extlen_t)len);
5146 nblks = len * mp->m_sb.sb_rextsize;
5147 qfield = XFS_TRANS_DQ_RTBCOUNT;
5150 * Ordinary allocation.
5154 nblks = del->br_blockcount;
5155 qfield = XFS_TRANS_DQ_BCOUNT;
5158 * Set up del_endblock and cur for later.
5160 del_endblock = del->br_startblock + del->br_blockcount;
5162 if ((error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
5163 got.br_startblock, got.br_blockcount,
5166 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
5168 da_old = da_new = 0;
5170 da_old = startblockval(got.br_startblock);
5177 * Set flag value to use in switch statement.
5178 * Left-contig is 2, right-contig is 1.
5180 switch (((got.br_startoff == del->br_startoff) << 1) |
5181 (got_endoff == del_endoff)) {
5184 * Matches the whole extent. Delete the entry.
5186 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5187 xfs_iext_remove(ip, *idx, 1,
5188 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0);
5193 XFS_IFORK_NEXT_SET(ip, whichfork,
5194 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
5195 flags |= XFS_ILOG_CORE;
5197 flags |= xfs_ilog_fext(whichfork);
5200 if ((error = xfs_btree_delete(cur, &i)))
5202 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
5207 * Deleting the first part of the extent.
5209 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5210 xfs_bmbt_set_startoff(ep, del_endoff);
5211 temp = got.br_blockcount - del->br_blockcount;
5212 xfs_bmbt_set_blockcount(ep, temp);
5214 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
5216 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
5217 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5221 xfs_bmbt_set_startblock(ep, del_endblock);
5222 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5224 flags |= xfs_ilog_fext(whichfork);
5227 if ((error = xfs_bmbt_update(cur, del_endoff, del_endblock,
5228 got.br_blockcount - del->br_blockcount,
5235 * Deleting the last part of the extent.
5237 temp = got.br_blockcount - del->br_blockcount;
5238 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5239 xfs_bmbt_set_blockcount(ep, temp);
5241 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
5243 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
5244 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5248 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5250 flags |= xfs_ilog_fext(whichfork);
5253 if ((error = xfs_bmbt_update(cur, got.br_startoff,
5255 got.br_blockcount - del->br_blockcount,
5262 * Deleting the middle of the extent.
5264 temp = del->br_startoff - got.br_startoff;
5265 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5266 xfs_bmbt_set_blockcount(ep, temp);
5267 new.br_startoff = del_endoff;
5268 temp2 = got_endoff - del_endoff;
5269 new.br_blockcount = temp2;
5270 new.br_state = got.br_state;
5272 new.br_startblock = del_endblock;
5273 flags |= XFS_ILOG_CORE;
5275 if ((error = xfs_bmbt_update(cur,
5277 got.br_startblock, temp,
5280 if ((error = xfs_btree_increment(cur, 0, &i)))
5282 cur->bc_rec.b = new;
5283 error = xfs_btree_insert(cur, &i);
5284 if (error && error != -ENOSPC)
5287 * If get no-space back from btree insert,
5288 * it tried a split, and we have a zero
5289 * block reservation.
5290 * Fix up our state and return the error.
5292 if (error == -ENOSPC) {
5294 * Reset the cursor, don't trust
5295 * it after any insert operation.
5297 if ((error = xfs_bmbt_lookup_eq(cur,
5302 XFS_WANT_CORRUPTED_GOTO(mp,
5305 * Update the btree record back
5306 * to the original value.
5308 if ((error = xfs_bmbt_update(cur,
5315 * Reset the extent record back
5316 * to the original value.
5318 xfs_bmbt_set_blockcount(ep,
5324 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
5326 flags |= xfs_ilog_fext(whichfork);
5327 XFS_IFORK_NEXT_SET(ip, whichfork,
5328 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
5330 xfs_filblks_t stolen;
5331 ASSERT(whichfork == XFS_DATA_FORK);
5334 * Distribute the original indlen reservation across the
5335 * two new extents. Steal blocks from the deleted extent
5336 * if necessary. Stealing blocks simply fudges the
5337 * fdblocks accounting in xfs_bunmapi().
5339 temp = xfs_bmap_worst_indlen(ip, got.br_blockcount);
5340 temp2 = xfs_bmap_worst_indlen(ip, new.br_blockcount);
5341 stolen = xfs_bmap_split_indlen(da_old, &temp, &temp2,
5342 del->br_blockcount);
5343 da_new = temp + temp2 - stolen;
5344 del->br_blockcount -= stolen;
5347 * Set the reservation for each extent. Warn if either
5348 * is zero as this can lead to delalloc problems.
5350 WARN_ON_ONCE(!temp || !temp2);
5351 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
5352 new.br_startblock = nullstartblock((int)temp2);
5354 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5355 xfs_iext_insert(ip, *idx + 1, 1, &new, state);
5360 /* remove reverse mapping */
5362 error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, del);
5368 * If we need to, add to list of extents to delete.
5370 if (do_fx && !(bflags & XFS_BMAPI_REMAP)) {
5371 if (xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK) {
5372 error = xfs_refcount_decrease_extent(mp, dfops, del);
5376 xfs_bmap_add_free(mp, dfops, del->br_startblock,
5377 del->br_blockcount, NULL);
5381 * Adjust inode # blocks in the file.
5384 ip->i_d.di_nblocks -= nblks;
5386 * Adjust quota data.
5388 if (qfield && !(bflags & XFS_BMAPI_REMAP))
5389 xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks);
5392 * Account for change in delayed indirect blocks.
5393 * Nothing to do for disk quota accounting here.
5395 ASSERT(da_old >= da_new);
5396 if (da_old > da_new)
5397 xfs_mod_fdblocks(mp, (int64_t)(da_old - da_new), false);
5404 * Unmap (remove) blocks from a file.
5405 * If nexts is nonzero then the number of extents to remove is limited to
5406 * that value. If not all extents in the block range can be removed then
5411 xfs_trans_t *tp, /* transaction pointer */
5412 struct xfs_inode *ip, /* incore inode */
5413 xfs_fileoff_t bno, /* starting offset to unmap */
5414 xfs_filblks_t *rlen, /* i/o: amount remaining */
5415 int flags, /* misc flags */
5416 xfs_extnum_t nexts, /* number of extents max */
5417 xfs_fsblock_t *firstblock, /* first allocated block
5418 controls a.g. for allocs */
5419 struct xfs_defer_ops *dfops) /* i/o: deferred updates */
5421 xfs_btree_cur_t *cur; /* bmap btree cursor */
5422 xfs_bmbt_irec_t del; /* extent being deleted */
5423 int error; /* error return value */
5424 xfs_extnum_t extno; /* extent number in list */
5425 xfs_bmbt_irec_t got; /* current extent record */
5426 xfs_ifork_t *ifp; /* inode fork pointer */
5427 int isrt; /* freeing in rt area */
5428 xfs_extnum_t lastx; /* last extent index used */
5429 int logflags; /* transaction logging flags */
5430 xfs_extlen_t mod; /* rt extent offset */
5431 xfs_mount_t *mp; /* mount structure */
5432 xfs_fileoff_t start; /* first file offset deleted */
5433 int tmp_logflags; /* partial logging flags */
5434 int wasdel; /* was a delayed alloc extent */
5435 int whichfork; /* data or attribute fork */
5437 xfs_filblks_t len = *rlen; /* length to unmap in file */
5439 trace_xfs_bunmap(ip, bno, len, flags, _RET_IP_);
5441 whichfork = xfs_bmapi_whichfork(flags);
5442 ASSERT(whichfork != XFS_COW_FORK);
5443 ifp = XFS_IFORK_PTR(ip, whichfork);
5445 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5446 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
5447 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW,
5449 return -EFSCORRUPTED;
5452 if (XFS_FORCED_SHUTDOWN(mp))
5455 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
5459 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5460 (error = xfs_iread_extents(tp, ip, whichfork)))
5462 if (xfs_iext_count(ifp) == 0) {
5466 XFS_STATS_INC(mp, xs_blk_unmap);
5467 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
5469 bno = start + len - 1;
5472 * Check to see if the given block number is past the end of the
5473 * file, back up to the last block if so...
5475 if (!xfs_iext_lookup_extent(ip, ifp, bno, &lastx, &got)) {
5477 xfs_iext_get_extent(ifp, --lastx, &got);
5478 bno = got.br_startoff + got.br_blockcount - 1;
5482 if (ifp->if_flags & XFS_IFBROOT) {
5483 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
5484 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5485 cur->bc_private.b.firstblock = *firstblock;
5486 cur->bc_private.b.dfops = dfops;
5487 cur->bc_private.b.flags = 0;
5493 * Synchronize by locking the bitmap inode.
5495 xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL|XFS_ILOCK_RTBITMAP);
5496 xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL);
5497 xfs_ilock(mp->m_rsumip, XFS_ILOCK_EXCL|XFS_ILOCK_RTSUM);
5498 xfs_trans_ijoin(tp, mp->m_rsumip, XFS_ILOCK_EXCL);
5502 while (bno != (xfs_fileoff_t)-1 && bno >= start && lastx >= 0 &&
5503 (nexts == 0 || extno < nexts)) {
5505 * Is the found extent after a hole in which bno lives?
5506 * Just back up to the previous extent, if so.
5508 if (got.br_startoff > bno) {
5511 xfs_iext_get_extent(ifp, lastx, &got);
5514 * Is the last block of this extent before the range
5515 * we're supposed to delete? If so, we're done.
5517 bno = XFS_FILEOFF_MIN(bno,
5518 got.br_startoff + got.br_blockcount - 1);
5522 * Then deal with the (possibly delayed) allocated space
5526 wasdel = isnullstartblock(del.br_startblock);
5527 if (got.br_startoff < start) {
5528 del.br_startoff = start;
5529 del.br_blockcount -= start - got.br_startoff;
5531 del.br_startblock += start - got.br_startoff;
5533 if (del.br_startoff + del.br_blockcount > bno + 1)
5534 del.br_blockcount = bno + 1 - del.br_startoff;
5535 sum = del.br_startblock + del.br_blockcount;
5537 (mod = do_mod(sum, mp->m_sb.sb_rextsize))) {
5539 * Realtime extent not lined up at the end.
5540 * The extent could have been split into written
5541 * and unwritten pieces, or we could just be
5542 * unmapping part of it. But we can't really
5543 * get rid of part of a realtime extent.
5545 if (del.br_state == XFS_EXT_UNWRITTEN ||
5546 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
5548 * This piece is unwritten, or we're not
5549 * using unwritten extents. Skip over it.
5552 bno -= mod > del.br_blockcount ?
5553 del.br_blockcount : mod;
5554 if (bno < got.br_startoff) {
5556 xfs_bmbt_get_all(xfs_iext_get_ext(
5562 * It's written, turn it unwritten.
5563 * This is better than zeroing it.
5565 ASSERT(del.br_state == XFS_EXT_NORM);
5566 ASSERT(tp->t_blk_res > 0);
5568 * If this spans a realtime extent boundary,
5569 * chop it back to the start of the one we end at.
5571 if (del.br_blockcount > mod) {
5572 del.br_startoff += del.br_blockcount - mod;
5573 del.br_startblock += del.br_blockcount - mod;
5574 del.br_blockcount = mod;
5576 del.br_state = XFS_EXT_UNWRITTEN;
5577 error = xfs_bmap_add_extent_unwritten_real(tp, ip,
5578 whichfork, &lastx, &cur, &del,
5579 firstblock, dfops, &logflags);
5584 if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) {
5586 * Realtime extent is lined up at the end but not
5587 * at the front. We'll get rid of full extents if
5590 mod = mp->m_sb.sb_rextsize - mod;
5591 if (del.br_blockcount > mod) {
5592 del.br_blockcount -= mod;
5593 del.br_startoff += mod;
5594 del.br_startblock += mod;
5595 } else if ((del.br_startoff == start &&
5596 (del.br_state == XFS_EXT_UNWRITTEN ||
5597 tp->t_blk_res == 0)) ||
5598 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
5600 * Can't make it unwritten. There isn't
5601 * a full extent here so just skip it.
5603 ASSERT(bno >= del.br_blockcount);
5604 bno -= del.br_blockcount;
5605 if (got.br_startoff > bno && --lastx >= 0)
5606 xfs_iext_get_extent(ifp, lastx, &got);
5608 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
5609 struct xfs_bmbt_irec prev;
5612 * This one is already unwritten.
5613 * It must have a written left neighbor.
5614 * Unwrite the killed part of that one and
5618 xfs_iext_get_extent(ifp, lastx - 1, &prev);
5619 ASSERT(prev.br_state == XFS_EXT_NORM);
5620 ASSERT(!isnullstartblock(prev.br_startblock));
5621 ASSERT(del.br_startblock ==
5622 prev.br_startblock + prev.br_blockcount);
5623 if (prev.br_startoff < start) {
5624 mod = start - prev.br_startoff;
5625 prev.br_blockcount -= mod;
5626 prev.br_startblock += mod;
5627 prev.br_startoff = start;
5629 prev.br_state = XFS_EXT_UNWRITTEN;
5631 error = xfs_bmap_add_extent_unwritten_real(tp,
5632 ip, whichfork, &lastx, &cur,
5633 &prev, firstblock, dfops,
5639 ASSERT(del.br_state == XFS_EXT_NORM);
5640 del.br_state = XFS_EXT_UNWRITTEN;
5641 error = xfs_bmap_add_extent_unwritten_real(tp,
5642 ip, whichfork, &lastx, &cur,
5643 &del, firstblock, dfops,
5652 * If it's the case where the directory code is running
5653 * with no block reservation, and the deleted block is in
5654 * the middle of its extent, and the resulting insert
5655 * of an extent would cause transformation to btree format,
5656 * then reject it. The calling code will then swap
5657 * blocks around instead.
5658 * We have to do this now, rather than waiting for the
5659 * conversion to btree format, since the transaction
5662 if (!wasdel && tp->t_blk_res == 0 &&
5663 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
5664 XFS_IFORK_NEXTENTS(ip, whichfork) >= /* Note the >= */
5665 XFS_IFORK_MAXEXT(ip, whichfork) &&
5666 del.br_startoff > got.br_startoff &&
5667 del.br_startoff + del.br_blockcount <
5668 got.br_startoff + got.br_blockcount) {
5674 * Unreserve quota and update realtime free space, if
5675 * appropriate. If delayed allocation, update the inode delalloc
5676 * counter now and wait to update the sb counters as
5677 * xfs_bmap_del_extent() might need to borrow some blocks.
5680 ASSERT(startblockval(del.br_startblock) > 0);
5682 xfs_filblks_t rtexts;
5684 rtexts = XFS_FSB_TO_B(mp, del.br_blockcount);
5685 do_div(rtexts, mp->m_sb.sb_rextsize);
5686 xfs_mod_frextents(mp, (int64_t)rtexts);
5687 (void)xfs_trans_reserve_quota_nblks(NULL,
5688 ip, -((long)del.br_blockcount), 0,
5689 XFS_QMOPT_RES_RTBLKS);
5691 (void)xfs_trans_reserve_quota_nblks(NULL,
5692 ip, -((long)del.br_blockcount), 0,
5693 XFS_QMOPT_RES_REGBLKS);
5695 ip->i_delayed_blks -= del.br_blockcount;
5697 cur->bc_private.b.flags |=
5698 XFS_BTCUR_BPRV_WASDEL;
5700 cur->bc_private.b.flags &= ~XFS_BTCUR_BPRV_WASDEL;
5702 error = xfs_bmap_del_extent(ip, tp, &lastx, dfops, cur, &del,
5703 &tmp_logflags, whichfork, flags);
5704 logflags |= tmp_logflags;
5708 if (!isrt && wasdel)
5709 xfs_mod_fdblocks(mp, (int64_t)del.br_blockcount, false);
5711 bno = del.br_startoff - 1;
5714 * If not done go on to the next (previous) record.
5716 if (bno != (xfs_fileoff_t)-1 && bno >= start) {
5718 xfs_iext_get_extent(ifp, lastx, &got);
5719 if (got.br_startoff > bno && --lastx >= 0)
5720 xfs_iext_get_extent(ifp, lastx, &got);
5725 if (bno == (xfs_fileoff_t)-1 || bno < start || lastx < 0)
5728 *rlen = bno - start + 1;
5731 * Convert to a btree if necessary.
5733 if (xfs_bmap_needs_btree(ip, whichfork)) {
5734 ASSERT(cur == NULL);
5735 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, dfops,
5736 &cur, 0, &tmp_logflags, whichfork);
5737 logflags |= tmp_logflags;
5742 * transform from btree to extents, give it cur
5744 else if (xfs_bmap_wants_extents(ip, whichfork)) {
5745 ASSERT(cur != NULL);
5746 error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags,
5748 logflags |= tmp_logflags;
5753 * transform from extents to local?
5758 * Log everything. Do this after conversion, there's no point in
5759 * logging the extent records if we've converted to btree format.
5761 if ((logflags & xfs_ilog_fext(whichfork)) &&
5762 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
5763 logflags &= ~xfs_ilog_fext(whichfork);
5764 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
5765 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
5766 logflags &= ~xfs_ilog_fbroot(whichfork);
5768 * Log inode even in the error case, if the transaction
5769 * is dirty we'll need to shut down the filesystem.
5772 xfs_trans_log_inode(tp, ip, logflags);
5775 *firstblock = cur->bc_private.b.firstblock;
5776 cur->bc_private.b.allocated = 0;
5778 xfs_btree_del_cursor(cur,
5779 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5784 /* Unmap a range of a file. */
5788 struct xfs_inode *ip,
5793 xfs_fsblock_t *firstblock,
5794 struct xfs_defer_ops *dfops,
5799 error = __xfs_bunmapi(tp, ip, bno, &len, flags, nexts, firstblock,
5806 * Determine whether an extent shift can be accomplished by a merge with the
5807 * extent that precedes the target hole of the shift.
5811 struct xfs_bmbt_irec *left, /* preceding extent */
5812 struct xfs_bmbt_irec *got, /* current extent to shift */
5813 xfs_fileoff_t shift) /* shift fsb */
5815 xfs_fileoff_t startoff;
5817 startoff = got->br_startoff - shift;
5820 * The extent, once shifted, must be adjacent in-file and on-disk with
5821 * the preceding extent.
5823 if ((left->br_startoff + left->br_blockcount != startoff) ||
5824 (left->br_startblock + left->br_blockcount != got->br_startblock) ||
5825 (left->br_state != got->br_state) ||
5826 (left->br_blockcount + got->br_blockcount > MAXEXTLEN))
5833 * A bmap extent shift adjusts the file offset of an extent to fill a preceding
5834 * hole in the file. If an extent shift would result in the extent being fully
5835 * adjacent to the extent that currently precedes the hole, we can merge with
5836 * the preceding extent rather than do the shift.
5838 * This function assumes the caller has verified a shift-by-merge is possible
5839 * with the provided extents via xfs_bmse_can_merge().
5843 struct xfs_inode *ip,
5845 xfs_fileoff_t shift, /* shift fsb */
5846 int current_ext, /* idx of gotp */
5847 struct xfs_bmbt_rec_host *gotp, /* extent to shift */
5848 struct xfs_bmbt_rec_host *leftp, /* preceding extent */
5849 struct xfs_btree_cur *cur,
5850 int *logflags) /* output */
5852 struct xfs_bmbt_irec got;
5853 struct xfs_bmbt_irec left;
5854 xfs_filblks_t blockcount;
5856 struct xfs_mount *mp = ip->i_mount;
5858 xfs_bmbt_get_all(gotp, &got);
5859 xfs_bmbt_get_all(leftp, &left);
5860 blockcount = left.br_blockcount + got.br_blockcount;
5862 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
5863 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
5864 ASSERT(xfs_bmse_can_merge(&left, &got, shift));
5867 * Merge the in-core extents. Note that the host record pointers and
5868 * current_ext index are invalid once the extent has been removed via
5869 * xfs_iext_remove().
5871 xfs_bmbt_set_blockcount(leftp, blockcount);
5872 xfs_iext_remove(ip, current_ext, 1, 0);
5875 * Update the on-disk extent count, the btree if necessary and log the
5878 XFS_IFORK_NEXT_SET(ip, whichfork,
5879 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
5880 *logflags |= XFS_ILOG_CORE;
5882 *logflags |= XFS_ILOG_DEXT;
5886 /* lookup and remove the extent to merge */
5887 error = xfs_bmbt_lookup_eq(cur, got.br_startoff, got.br_startblock,
5888 got.br_blockcount, &i);
5891 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
5893 error = xfs_btree_delete(cur, &i);
5896 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
5898 /* lookup and update size of the previous extent */
5899 error = xfs_bmbt_lookup_eq(cur, left.br_startoff, left.br_startblock,
5900 left.br_blockcount, &i);
5903 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
5905 left.br_blockcount = blockcount;
5907 return xfs_bmbt_update(cur, left.br_startoff, left.br_startblock,
5908 left.br_blockcount, left.br_state);
5912 * Shift a single extent.
5916 struct xfs_inode *ip,
5918 xfs_fileoff_t offset_shift_fsb,
5920 struct xfs_bmbt_rec_host *gotp,
5921 struct xfs_btree_cur *cur,
5923 enum shift_direction direction,
5924 struct xfs_defer_ops *dfops)
5926 struct xfs_ifork *ifp;
5927 struct xfs_mount *mp;
5928 xfs_fileoff_t startoff;
5929 struct xfs_bmbt_rec_host *adj_irecp;
5930 struct xfs_bmbt_irec got;
5931 struct xfs_bmbt_irec adj_irec;
5937 ifp = XFS_IFORK_PTR(ip, whichfork);
5938 total_extents = xfs_iext_count(ifp);
5940 xfs_bmbt_get_all(gotp, &got);
5942 /* delalloc extents should be prevented by caller */
5943 XFS_WANT_CORRUPTED_RETURN(mp, !isnullstartblock(got.br_startblock));
5945 if (direction == SHIFT_LEFT) {
5946 startoff = got.br_startoff - offset_shift_fsb;
5949 * Check for merge if we've got an extent to the left,
5950 * otherwise make sure there's enough room at the start
5951 * of the file for the shift.
5953 if (!*current_ext) {
5954 if (got.br_startoff < offset_shift_fsb)
5956 goto update_current_ext;
5959 * grab the left extent and check for a large
5962 adj_irecp = xfs_iext_get_ext(ifp, *current_ext - 1);
5963 xfs_bmbt_get_all(adj_irecp, &adj_irec);
5966 adj_irec.br_startoff + adj_irec.br_blockcount)
5969 /* check whether to merge the extent or shift it down */
5970 if (xfs_bmse_can_merge(&adj_irec, &got,
5971 offset_shift_fsb)) {
5972 error = xfs_bmse_merge(ip, whichfork, offset_shift_fsb,
5973 *current_ext, gotp, adj_irecp,
5981 startoff = got.br_startoff + offset_shift_fsb;
5982 /* nothing to move if this is the last extent */
5983 if (*current_ext >= (total_extents - 1))
5984 goto update_current_ext;
5986 * If this is not the last extent in the file, make sure there
5987 * is enough room between current extent and next extent for
5988 * accommodating the shift.
5990 adj_irecp = xfs_iext_get_ext(ifp, *current_ext + 1);
5991 xfs_bmbt_get_all(adj_irecp, &adj_irec);
5992 if (startoff + got.br_blockcount > adj_irec.br_startoff)
5995 * Unlike a left shift (which involves a hole punch),
5996 * a right shift does not modify extent neighbors
5997 * in any way. We should never find mergeable extents
5998 * in this scenario. Check anyways and warn if we
5999 * encounter two extents that could be one.
6001 if (xfs_bmse_can_merge(&got, &adj_irec, offset_shift_fsb))
6005 * Increment the extent index for the next iteration, update the start
6006 * offset of the in-core extent and update the btree if applicable.
6009 if (direction == SHIFT_LEFT)
6013 xfs_bmbt_set_startoff(gotp, startoff);
6014 *logflags |= XFS_ILOG_CORE;
6017 *logflags |= XFS_ILOG_DEXT;
6021 error = xfs_bmbt_lookup_eq(cur, got.br_startoff, got.br_startblock,
6022 got.br_blockcount, &i);
6025 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
6027 got.br_startoff = startoff;
6028 error = xfs_bmbt_update(cur, got.br_startoff, got.br_startblock,
6029 got.br_blockcount, got.br_state);
6034 /* update reverse mapping */
6035 error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, &adj_irec);
6038 adj_irec.br_startoff = startoff;
6039 return xfs_rmap_map_extent(mp, dfops, ip, whichfork, &adj_irec);
6043 * Shift extent records to the left/right to cover/create a hole.
6045 * The maximum number of extents to be shifted in a single operation is
6046 * @num_exts. @stop_fsb specifies the file offset at which to stop shift and the
6047 * file offset where we've left off is returned in @next_fsb. @offset_shift_fsb
6048 * is the length by which each extent is shifted. If there is no hole to shift
6049 * the extents into, this will be considered invalid operation and we abort
6053 xfs_bmap_shift_extents(
6054 struct xfs_trans *tp,
6055 struct xfs_inode *ip,
6056 xfs_fileoff_t *next_fsb,
6057 xfs_fileoff_t offset_shift_fsb,
6059 xfs_fileoff_t stop_fsb,
6060 xfs_fsblock_t *firstblock,
6061 struct xfs_defer_ops *dfops,
6062 enum shift_direction direction,
6065 struct xfs_btree_cur *cur = NULL;
6066 struct xfs_bmbt_rec_host *gotp;
6067 struct xfs_bmbt_irec got;
6068 struct xfs_mount *mp = ip->i_mount;
6069 struct xfs_ifork *ifp;
6070 xfs_extnum_t nexts = 0;
6071 xfs_extnum_t current_ext;
6072 xfs_extnum_t total_extents;
6073 xfs_extnum_t stop_extent;
6075 int whichfork = XFS_DATA_FORK;
6078 if (unlikely(XFS_TEST_ERROR(
6079 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
6080 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
6081 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
6082 XFS_ERROR_REPORT("xfs_bmap_shift_extents",
6083 XFS_ERRLEVEL_LOW, mp);
6084 return -EFSCORRUPTED;
6087 if (XFS_FORCED_SHUTDOWN(mp))
6090 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
6091 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
6092 ASSERT(direction == SHIFT_LEFT || direction == SHIFT_RIGHT);
6093 ASSERT(*next_fsb != NULLFSBLOCK || direction == SHIFT_RIGHT);
6095 ifp = XFS_IFORK_PTR(ip, whichfork);
6096 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
6097 /* Read in all the extents */
6098 error = xfs_iread_extents(tp, ip, whichfork);
6103 if (ifp->if_flags & XFS_IFBROOT) {
6104 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
6105 cur->bc_private.b.firstblock = *firstblock;
6106 cur->bc_private.b.dfops = dfops;
6107 cur->bc_private.b.flags = 0;
6111 * There may be delalloc extents in the data fork before the range we
6112 * are collapsing out, so we cannot use the count of real extents here.
6113 * Instead we have to calculate it from the incore fork.
6115 total_extents = xfs_iext_count(ifp);
6116 if (total_extents == 0) {
6122 * In case of first right shift, we need to initialize next_fsb
6124 if (*next_fsb == NULLFSBLOCK) {
6125 gotp = xfs_iext_get_ext(ifp, total_extents - 1);
6126 xfs_bmbt_get_all(gotp, &got);
6127 *next_fsb = got.br_startoff;
6128 if (stop_fsb > *next_fsb) {
6134 /* Lookup the extent index at which we have to stop */
6135 if (direction == SHIFT_RIGHT) {
6136 gotp = xfs_iext_bno_to_ext(ifp, stop_fsb, &stop_extent);
6137 /* Make stop_extent exclusive of shift range */
6140 stop_extent = total_extents;
6143 * Look up the extent index for the fsb where we start shifting. We can
6144 * henceforth iterate with current_ext as extent list changes are locked
6147 * gotp can be null in 2 cases: 1) if there are no extents or 2)
6148 * *next_fsb lies in a hole beyond which there are no extents. Either
6151 gotp = xfs_iext_bno_to_ext(ifp, *next_fsb, ¤t_ext);
6157 /* some sanity checking before we finally start shifting extents */
6158 if ((direction == SHIFT_LEFT && current_ext >= stop_extent) ||
6159 (direction == SHIFT_RIGHT && current_ext <= stop_extent)) {
6164 while (nexts++ < num_exts) {
6165 error = xfs_bmse_shift_one(ip, whichfork, offset_shift_fsb,
6166 ¤t_ext, gotp, cur, &logflags,
6171 * If there was an extent merge during the shift, the extent
6172 * count can change. Update the total and grade the next record.
6174 if (direction == SHIFT_LEFT) {
6175 total_extents = xfs_iext_count(ifp);
6176 stop_extent = total_extents;
6179 if (current_ext == stop_extent) {
6181 *next_fsb = NULLFSBLOCK;
6184 gotp = xfs_iext_get_ext(ifp, current_ext);
6188 xfs_bmbt_get_all(gotp, &got);
6189 *next_fsb = got.br_startoff;
6194 xfs_btree_del_cursor(cur,
6195 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
6198 xfs_trans_log_inode(tp, ip, logflags);
6204 * Splits an extent into two extents at split_fsb block such that it is
6205 * the first block of the current_ext. @current_ext is a target extent
6206 * to be split. @split_fsb is a block where the extents is split.
6207 * If split_fsb lies in a hole or the first block of extents, just return 0.
6210 xfs_bmap_split_extent_at(
6211 struct xfs_trans *tp,
6212 struct xfs_inode *ip,
6213 xfs_fileoff_t split_fsb,
6214 xfs_fsblock_t *firstfsb,
6215 struct xfs_defer_ops *dfops)
6217 int whichfork = XFS_DATA_FORK;
6218 struct xfs_btree_cur *cur = NULL;
6219 struct xfs_bmbt_rec_host *gotp;
6220 struct xfs_bmbt_irec got;
6221 struct xfs_bmbt_irec new; /* split extent */
6222 struct xfs_mount *mp = ip->i_mount;
6223 struct xfs_ifork *ifp;
6224 xfs_fsblock_t gotblkcnt; /* new block count for got */
6225 xfs_extnum_t current_ext;
6230 if (unlikely(XFS_TEST_ERROR(
6231 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
6232 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
6233 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
6234 XFS_ERROR_REPORT("xfs_bmap_split_extent_at",
6235 XFS_ERRLEVEL_LOW, mp);
6236 return -EFSCORRUPTED;
6239 if (XFS_FORCED_SHUTDOWN(mp))
6242 ifp = XFS_IFORK_PTR(ip, whichfork);
6243 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
6244 /* Read in all the extents */
6245 error = xfs_iread_extents(tp, ip, whichfork);
6251 * gotp can be null in 2 cases: 1) if there are no extents
6252 * or 2) split_fsb lies in a hole beyond which there are
6253 * no extents. Either way, we are done.
6255 gotp = xfs_iext_bno_to_ext(ifp, split_fsb, ¤t_ext);
6259 xfs_bmbt_get_all(gotp, &got);
6262 * Check split_fsb lies in a hole or the start boundary offset
6265 if (got.br_startoff >= split_fsb)
6268 gotblkcnt = split_fsb - got.br_startoff;
6269 new.br_startoff = split_fsb;
6270 new.br_startblock = got.br_startblock + gotblkcnt;
6271 new.br_blockcount = got.br_blockcount - gotblkcnt;
6272 new.br_state = got.br_state;
6274 if (ifp->if_flags & XFS_IFBROOT) {
6275 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
6276 cur->bc_private.b.firstblock = *firstfsb;
6277 cur->bc_private.b.dfops = dfops;
6278 cur->bc_private.b.flags = 0;
6279 error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
6285 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor);
6288 xfs_bmbt_set_blockcount(gotp, gotblkcnt);
6289 got.br_blockcount = gotblkcnt;
6291 logflags = XFS_ILOG_CORE;
6293 error = xfs_bmbt_update(cur, got.br_startoff,
6300 logflags |= XFS_ILOG_DEXT;
6302 /* Add new extent */
6304 xfs_iext_insert(ip, current_ext, 1, &new, 0);
6305 XFS_IFORK_NEXT_SET(ip, whichfork,
6306 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
6309 error = xfs_bmbt_lookup_eq(cur, new.br_startoff,
6310 new.br_startblock, new.br_blockcount,
6314 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, del_cursor);
6315 cur->bc_rec.b.br_state = new.br_state;
6317 error = xfs_btree_insert(cur, &i);
6320 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor);
6324 * Convert to a btree if necessary.
6326 if (xfs_bmap_needs_btree(ip, whichfork)) {
6327 int tmp_logflags; /* partial log flag return val */
6329 ASSERT(cur == NULL);
6330 error = xfs_bmap_extents_to_btree(tp, ip, firstfsb, dfops,
6331 &cur, 0, &tmp_logflags, whichfork);
6332 logflags |= tmp_logflags;
6337 cur->bc_private.b.allocated = 0;
6338 xfs_btree_del_cursor(cur,
6339 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
6343 xfs_trans_log_inode(tp, ip, logflags);
6348 xfs_bmap_split_extent(
6349 struct xfs_inode *ip,
6350 xfs_fileoff_t split_fsb)
6352 struct xfs_mount *mp = ip->i_mount;
6353 struct xfs_trans *tp;
6354 struct xfs_defer_ops dfops;
6355 xfs_fsblock_t firstfsb;
6358 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write,
6359 XFS_DIOSTRAT_SPACE_RES(mp, 0), 0, 0, &tp);
6363 xfs_ilock(ip, XFS_ILOCK_EXCL);
6364 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
6366 xfs_defer_init(&dfops, &firstfsb);
6368 error = xfs_bmap_split_extent_at(tp, ip, split_fsb,
6373 error = xfs_defer_finish(&tp, &dfops, NULL);
6377 return xfs_trans_commit(tp);
6380 xfs_defer_cancel(&dfops);
6381 xfs_trans_cancel(tp);
6385 /* Deferred mapping is only for real extents in the data fork. */
6387 xfs_bmap_is_update_needed(
6388 struct xfs_bmbt_irec *bmap)
6390 return bmap->br_startblock != HOLESTARTBLOCK &&
6391 bmap->br_startblock != DELAYSTARTBLOCK;
6394 /* Record a bmap intent. */
6397 struct xfs_mount *mp,
6398 struct xfs_defer_ops *dfops,
6399 enum xfs_bmap_intent_type type,
6400 struct xfs_inode *ip,
6402 struct xfs_bmbt_irec *bmap)
6405 struct xfs_bmap_intent *bi;
6407 trace_xfs_bmap_defer(mp,
6408 XFS_FSB_TO_AGNO(mp, bmap->br_startblock),
6410 XFS_FSB_TO_AGBNO(mp, bmap->br_startblock),
6411 ip->i_ino, whichfork,
6413 bmap->br_blockcount,
6416 bi = kmem_alloc(sizeof(struct xfs_bmap_intent), KM_SLEEP | KM_NOFS);
6417 INIT_LIST_HEAD(&bi->bi_list);
6420 bi->bi_whichfork = whichfork;
6421 bi->bi_bmap = *bmap;
6423 error = xfs_defer_join(dfops, bi->bi_owner);
6429 xfs_defer_add(dfops, XFS_DEFER_OPS_TYPE_BMAP, &bi->bi_list);
6433 /* Map an extent into a file. */
6435 xfs_bmap_map_extent(
6436 struct xfs_mount *mp,
6437 struct xfs_defer_ops *dfops,
6438 struct xfs_inode *ip,
6439 struct xfs_bmbt_irec *PREV)
6441 if (!xfs_bmap_is_update_needed(PREV))
6444 return __xfs_bmap_add(mp, dfops, XFS_BMAP_MAP, ip,
6445 XFS_DATA_FORK, PREV);
6448 /* Unmap an extent out of a file. */
6450 xfs_bmap_unmap_extent(
6451 struct xfs_mount *mp,
6452 struct xfs_defer_ops *dfops,
6453 struct xfs_inode *ip,
6454 struct xfs_bmbt_irec *PREV)
6456 if (!xfs_bmap_is_update_needed(PREV))
6459 return __xfs_bmap_add(mp, dfops, XFS_BMAP_UNMAP, ip,
6460 XFS_DATA_FORK, PREV);
6464 * Process one of the deferred bmap operations. We pass back the
6465 * btree cursor to maintain our lock on the bmapbt between calls.
6468 xfs_bmap_finish_one(
6469 struct xfs_trans *tp,
6470 struct xfs_defer_ops *dfops,
6471 struct xfs_inode *ip,
6472 enum xfs_bmap_intent_type type,
6474 xfs_fileoff_t startoff,
6475 xfs_fsblock_t startblock,
6476 xfs_filblks_t blockcount,
6479 int error = 0, done;
6481 trace_xfs_bmap_deferred(tp->t_mountp,
6482 XFS_FSB_TO_AGNO(tp->t_mountp, startblock), type,
6483 XFS_FSB_TO_AGBNO(tp->t_mountp, startblock),
6484 ip->i_ino, whichfork, startoff, blockcount, state);
6486 if (WARN_ON_ONCE(whichfork != XFS_DATA_FORK))
6487 return -EFSCORRUPTED;
6489 if (XFS_TEST_ERROR(false, tp->t_mountp,
6490 XFS_ERRTAG_BMAP_FINISH_ONE,
6491 XFS_RANDOM_BMAP_FINISH_ONE))
6496 error = xfs_bmapi_remap(tp, ip, startoff, blockcount,
6499 case XFS_BMAP_UNMAP:
6500 error = xfs_bunmapi(tp, ip, startoff, blockcount,
6501 XFS_BMAPI_REMAP, 1, &startblock, dfops, &done);
6506 error = -EFSCORRUPTED;