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 if (xfs_sb_version_hascrc(&mp->m_sb))
744 xfs_btree_init_block_int(mp, block, XFS_BUF_DADDR_NULL,
745 XFS_BMAP_CRC_MAGIC, 1, 1, ip->i_ino,
746 XFS_BTREE_LONG_PTRS | XFS_BTREE_CRC_BLOCKS);
748 xfs_btree_init_block_int(mp, block, XFS_BUF_DADDR_NULL,
749 XFS_BMAP_MAGIC, 1, 1, ip->i_ino,
750 XFS_BTREE_LONG_PTRS);
753 * Need a cursor. Can't allocate until bb_level is filled in.
755 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
756 cur->bc_private.b.firstblock = *firstblock;
757 cur->bc_private.b.dfops = dfops;
758 cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
760 * Convert to a btree with two levels, one record in root.
762 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE);
763 memset(&args, 0, sizeof(args));
766 xfs_rmap_ino_bmbt_owner(&args.oinfo, ip->i_ino, whichfork);
767 args.firstblock = *firstblock;
768 if (*firstblock == NULLFSBLOCK) {
769 args.type = XFS_ALLOCTYPE_START_BNO;
770 args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino);
771 } else if (dfops->dop_low) {
773 args.type = XFS_ALLOCTYPE_START_BNO;
774 args.fsbno = *firstblock;
776 args.type = XFS_ALLOCTYPE_NEAR_BNO;
777 args.fsbno = *firstblock;
779 args.minlen = args.maxlen = args.prod = 1;
780 args.wasdel = wasdel;
782 if ((error = xfs_alloc_vextent(&args))) {
783 xfs_iroot_realloc(ip, -1, whichfork);
784 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
789 * During a CoW operation, the allocation and bmbt updates occur in
790 * different transactions. The mapping code tries to put new bmbt
791 * blocks near extents being mapped, but the only way to guarantee this
792 * is if the alloc and the mapping happen in a single transaction that
793 * has a block reservation. That isn't the case here, so if we run out
794 * of space we'll try again with another AG.
796 if (xfs_sb_version_hasreflink(&cur->bc_mp->m_sb) &&
797 args.fsbno == NULLFSBLOCK &&
798 args.type == XFS_ALLOCTYPE_NEAR_BNO) {
799 dfops->dop_low = true;
803 * Allocation can't fail, the space was reserved.
805 ASSERT(args.fsbno != NULLFSBLOCK);
806 ASSERT(*firstblock == NULLFSBLOCK ||
807 args.agno == XFS_FSB_TO_AGNO(mp, *firstblock) ||
809 args.agno > XFS_FSB_TO_AGNO(mp, *firstblock)));
810 *firstblock = cur->bc_private.b.firstblock = args.fsbno;
811 cur->bc_private.b.allocated++;
812 ip->i_d.di_nblocks++;
813 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
814 abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0);
816 * Fill in the child block.
818 abp->b_ops = &xfs_bmbt_buf_ops;
819 ablock = XFS_BUF_TO_BLOCK(abp);
820 if (xfs_sb_version_hascrc(&mp->m_sb))
821 xfs_btree_init_block_int(mp, ablock, abp->b_bn,
822 XFS_BMAP_CRC_MAGIC, 0, 0, ip->i_ino,
823 XFS_BTREE_LONG_PTRS | XFS_BTREE_CRC_BLOCKS);
825 xfs_btree_init_block_int(mp, ablock, abp->b_bn,
826 XFS_BMAP_MAGIC, 0, 0, ip->i_ino,
827 XFS_BTREE_LONG_PTRS);
829 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
830 nextents = xfs_iext_count(ifp);
831 for (cnt = i = 0; i < nextents; i++) {
832 ep = xfs_iext_get_ext(ifp, i);
833 if (!isnullstartblock(xfs_bmbt_get_startblock(ep))) {
834 arp->l0 = cpu_to_be64(ep->l0);
835 arp->l1 = cpu_to_be64(ep->l1);
839 ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork));
840 xfs_btree_set_numrecs(ablock, cnt);
843 * Fill in the root key and pointer.
845 kp = XFS_BMBT_KEY_ADDR(mp, block, 1);
846 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
847 kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp));
848 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur,
849 be16_to_cpu(block->bb_level)));
850 *pp = cpu_to_be64(args.fsbno);
853 * Do all this logging at the end so that
854 * the root is at the right level.
856 xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS);
857 xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs));
858 ASSERT(*curp == NULL);
860 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork);
865 * Convert a local file to an extents file.
866 * This code is out of bounds for data forks of regular files,
867 * since the file data needs to get logged so things will stay consistent.
868 * (The bmap-level manipulations are ok, though).
871 xfs_bmap_local_to_extents_empty(
872 struct xfs_inode *ip,
875 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
877 ASSERT(whichfork != XFS_COW_FORK);
878 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
879 ASSERT(ifp->if_bytes == 0);
880 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0);
882 xfs_bmap_forkoff_reset(ip, whichfork);
883 ifp->if_flags &= ~XFS_IFINLINE;
884 ifp->if_flags |= XFS_IFEXTENTS;
885 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
889 STATIC int /* error */
890 xfs_bmap_local_to_extents(
891 xfs_trans_t *tp, /* transaction pointer */
892 xfs_inode_t *ip, /* incore inode pointer */
893 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
894 xfs_extlen_t total, /* total blocks needed by transaction */
895 int *logflagsp, /* inode logging flags */
897 void (*init_fn)(struct xfs_trans *tp,
899 struct xfs_inode *ip,
900 struct xfs_ifork *ifp))
903 int flags; /* logging flags returned */
904 xfs_ifork_t *ifp; /* inode fork pointer */
905 xfs_alloc_arg_t args; /* allocation arguments */
906 xfs_buf_t *bp; /* buffer for extent block */
907 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
910 * We don't want to deal with the case of keeping inode data inline yet.
911 * So sending the data fork of a regular inode is invalid.
913 ASSERT(!(S_ISREG(VFS_I(ip)->i_mode) && whichfork == XFS_DATA_FORK));
914 ifp = XFS_IFORK_PTR(ip, whichfork);
915 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
917 if (!ifp->if_bytes) {
918 xfs_bmap_local_to_extents_empty(ip, whichfork);
919 flags = XFS_ILOG_CORE;
925 ASSERT((ifp->if_flags & (XFS_IFINLINE|XFS_IFEXTENTS|XFS_IFEXTIREC)) ==
927 memset(&args, 0, sizeof(args));
929 args.mp = ip->i_mount;
930 xfs_rmap_ino_owner(&args.oinfo, ip->i_ino, whichfork, 0);
931 args.firstblock = *firstblock;
933 * Allocate a block. We know we need only one, since the
934 * file currently fits in an inode.
936 if (*firstblock == NULLFSBLOCK) {
938 args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino);
939 args.type = XFS_ALLOCTYPE_START_BNO;
941 args.fsbno = *firstblock;
942 args.type = XFS_ALLOCTYPE_NEAR_BNO;
945 args.minlen = args.maxlen = args.prod = 1;
946 error = xfs_alloc_vextent(&args);
951 * During a CoW operation, the allocation and bmbt updates occur in
952 * different transactions. The mapping code tries to put new bmbt
953 * blocks near extents being mapped, but the only way to guarantee this
954 * is if the alloc and the mapping happen in a single transaction that
955 * has a block reservation. That isn't the case here, so if we run out
956 * of space we'll try again with another AG.
958 if (xfs_sb_version_hasreflink(&ip->i_mount->m_sb) &&
959 args.fsbno == NULLFSBLOCK &&
960 args.type == XFS_ALLOCTYPE_NEAR_BNO) {
963 /* Can't fail, the space was reserved. */
964 ASSERT(args.fsbno != NULLFSBLOCK);
965 ASSERT(args.len == 1);
966 *firstblock = args.fsbno;
967 bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0);
970 * Initialize the block, copy the data and log the remote buffer.
972 * The callout is responsible for logging because the remote format
973 * might differ from the local format and thus we don't know how much to
974 * log here. Note that init_fn must also set the buffer log item type
977 init_fn(tp, bp, ip, ifp);
979 /* account for the change in fork size */
980 xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
981 xfs_bmap_local_to_extents_empty(ip, whichfork);
982 flags |= XFS_ILOG_CORE;
984 xfs_iext_add(ifp, 0, 1);
985 ep = xfs_iext_get_ext(ifp, 0);
986 xfs_bmbt_set_allf(ep, 0, args.fsbno, 1, XFS_EXT_NORM);
987 trace_xfs_bmap_post_update(ip, 0,
988 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0,
990 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
991 ip->i_d.di_nblocks = 1;
992 xfs_trans_mod_dquot_byino(tp, ip,
993 XFS_TRANS_DQ_BCOUNT, 1L);
994 flags |= xfs_ilog_fext(whichfork);
1002 * Called from xfs_bmap_add_attrfork to handle btree format files.
1004 STATIC int /* error */
1005 xfs_bmap_add_attrfork_btree(
1006 xfs_trans_t *tp, /* transaction pointer */
1007 xfs_inode_t *ip, /* incore inode pointer */
1008 xfs_fsblock_t *firstblock, /* first block allocated */
1009 struct xfs_defer_ops *dfops, /* blocks to free at commit */
1010 int *flags) /* inode logging flags */
1012 xfs_btree_cur_t *cur; /* btree cursor */
1013 int error; /* error return value */
1014 xfs_mount_t *mp; /* file system mount struct */
1015 int stat; /* newroot status */
1018 if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip))
1019 *flags |= XFS_ILOG_DBROOT;
1021 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
1022 cur->bc_private.b.dfops = dfops;
1023 cur->bc_private.b.firstblock = *firstblock;
1024 if ((error = xfs_bmbt_lookup_ge(cur, 0, 0, 0, &stat)))
1026 /* must be at least one entry */
1027 XFS_WANT_CORRUPTED_GOTO(mp, stat == 1, error0);
1028 if ((error = xfs_btree_new_iroot(cur, flags, &stat)))
1031 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1034 *firstblock = cur->bc_private.b.firstblock;
1035 cur->bc_private.b.allocated = 0;
1036 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1040 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
1045 * Called from xfs_bmap_add_attrfork to handle extents format files.
1047 STATIC int /* error */
1048 xfs_bmap_add_attrfork_extents(
1049 xfs_trans_t *tp, /* transaction pointer */
1050 xfs_inode_t *ip, /* incore inode pointer */
1051 xfs_fsblock_t *firstblock, /* first block allocated */
1052 struct xfs_defer_ops *dfops, /* blocks to free at commit */
1053 int *flags) /* inode logging flags */
1055 xfs_btree_cur_t *cur; /* bmap btree cursor */
1056 int error; /* error return value */
1058 if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip))
1061 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, dfops, &cur, 0,
1062 flags, XFS_DATA_FORK);
1064 cur->bc_private.b.allocated = 0;
1065 xfs_btree_del_cursor(cur,
1066 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
1072 * Called from xfs_bmap_add_attrfork to handle local format files. Each
1073 * different data fork content type needs a different callout to do the
1074 * conversion. Some are basic and only require special block initialisation
1075 * callouts for the data formating, others (directories) are so specialised they
1076 * handle everything themselves.
1078 * XXX (dgc): investigate whether directory conversion can use the generic
1079 * formatting callout. It should be possible - it's just a very complex
1082 STATIC int /* error */
1083 xfs_bmap_add_attrfork_local(
1084 xfs_trans_t *tp, /* transaction pointer */
1085 xfs_inode_t *ip, /* incore inode pointer */
1086 xfs_fsblock_t *firstblock, /* first block allocated */
1087 struct xfs_defer_ops *dfops, /* blocks to free at commit */
1088 int *flags) /* inode logging flags */
1090 xfs_da_args_t dargs; /* args for dir/attr code */
1092 if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip))
1095 if (S_ISDIR(VFS_I(ip)->i_mode)) {
1096 memset(&dargs, 0, sizeof(dargs));
1097 dargs.geo = ip->i_mount->m_dir_geo;
1099 dargs.firstblock = firstblock;
1100 dargs.dfops = dfops;
1101 dargs.total = dargs.geo->fsbcount;
1102 dargs.whichfork = XFS_DATA_FORK;
1104 return xfs_dir2_sf_to_block(&dargs);
1107 if (S_ISLNK(VFS_I(ip)->i_mode))
1108 return xfs_bmap_local_to_extents(tp, ip, firstblock, 1,
1109 flags, XFS_DATA_FORK,
1110 xfs_symlink_local_to_remote);
1112 /* should only be called for types that support local format data */
1114 return -EFSCORRUPTED;
1118 * Convert inode from non-attributed to attributed.
1119 * Must not be in a transaction, ip must not be locked.
1121 int /* error code */
1122 xfs_bmap_add_attrfork(
1123 xfs_inode_t *ip, /* incore inode pointer */
1124 int size, /* space new attribute needs */
1125 int rsvd) /* xact may use reserved blks */
1127 xfs_fsblock_t firstblock; /* 1st block/ag allocated */
1128 struct xfs_defer_ops dfops; /* freed extent records */
1129 xfs_mount_t *mp; /* mount structure */
1130 xfs_trans_t *tp; /* transaction pointer */
1131 int blks; /* space reservation */
1132 int version = 1; /* superblock attr version */
1133 int logflags; /* logging flags */
1134 int error; /* error return value */
1136 ASSERT(XFS_IFORK_Q(ip) == 0);
1139 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
1141 blks = XFS_ADDAFORK_SPACE_RES(mp);
1143 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_addafork, blks, 0,
1144 rsvd ? XFS_TRANS_RESERVE : 0, &tp);
1148 xfs_ilock(ip, XFS_ILOCK_EXCL);
1149 error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ?
1150 XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
1151 XFS_QMOPT_RES_REGBLKS);
1154 if (XFS_IFORK_Q(ip))
1156 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) {
1158 * For inodes coming from pre-6.2 filesystems.
1160 ASSERT(ip->i_d.di_aformat == 0);
1161 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
1163 ASSERT(ip->i_d.di_anextents == 0);
1165 xfs_trans_ijoin(tp, ip, 0);
1166 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1168 switch (ip->i_d.di_format) {
1169 case XFS_DINODE_FMT_DEV:
1170 ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
1172 case XFS_DINODE_FMT_UUID:
1173 ip->i_d.di_forkoff = roundup(sizeof(uuid_t), 8) >> 3;
1175 case XFS_DINODE_FMT_LOCAL:
1176 case XFS_DINODE_FMT_EXTENTS:
1177 case XFS_DINODE_FMT_BTREE:
1178 ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size);
1179 if (!ip->i_d.di_forkoff)
1180 ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3;
1181 else if (mp->m_flags & XFS_MOUNT_ATTR2)
1190 ASSERT(ip->i_afp == NULL);
1191 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP);
1192 ip->i_afp->if_flags = XFS_IFEXTENTS;
1194 xfs_defer_init(&dfops, &firstblock);
1195 switch (ip->i_d.di_format) {
1196 case XFS_DINODE_FMT_LOCAL:
1197 error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &dfops,
1200 case XFS_DINODE_FMT_EXTENTS:
1201 error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock,
1204 case XFS_DINODE_FMT_BTREE:
1205 error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &dfops,
1213 xfs_trans_log_inode(tp, ip, logflags);
1216 if (!xfs_sb_version_hasattr(&mp->m_sb) ||
1217 (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) {
1218 bool log_sb = false;
1220 spin_lock(&mp->m_sb_lock);
1221 if (!xfs_sb_version_hasattr(&mp->m_sb)) {
1222 xfs_sb_version_addattr(&mp->m_sb);
1225 if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) {
1226 xfs_sb_version_addattr2(&mp->m_sb);
1229 spin_unlock(&mp->m_sb_lock);
1234 error = xfs_defer_finish(&tp, &dfops, NULL);
1237 error = xfs_trans_commit(tp);
1238 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1242 xfs_defer_cancel(&dfops);
1244 xfs_trans_cancel(tp);
1245 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1250 * Internal and external extent tree search functions.
1254 * Read in the extents to if_extents.
1255 * All inode fields are set up by caller, we just traverse the btree
1256 * and copy the records in. If the file system cannot contain unwritten
1257 * extents, the records are checked for no "state" flags.
1260 xfs_bmap_read_extents(
1261 xfs_trans_t *tp, /* transaction pointer */
1262 xfs_inode_t *ip, /* incore inode */
1263 int whichfork) /* data or attr fork */
1265 struct xfs_btree_block *block; /* current btree block */
1266 xfs_fsblock_t bno; /* block # of "block" */
1267 xfs_buf_t *bp; /* buffer for "block" */
1268 int error; /* error return value */
1269 xfs_exntfmt_t exntf; /* XFS_EXTFMT_NOSTATE, if checking */
1270 xfs_extnum_t i, j; /* index into the extents list */
1271 xfs_ifork_t *ifp; /* fork structure */
1272 int level; /* btree level, for checking */
1273 xfs_mount_t *mp; /* file system mount structure */
1274 __be64 *pp; /* pointer to block address */
1276 xfs_extnum_t room; /* number of entries there's room for */
1280 ifp = XFS_IFORK_PTR(ip, whichfork);
1281 exntf = (whichfork != XFS_DATA_FORK) ? XFS_EXTFMT_NOSTATE :
1282 XFS_EXTFMT_INODE(ip);
1283 block = ifp->if_broot;
1285 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
1287 level = be16_to_cpu(block->bb_level);
1289 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
1290 bno = be64_to_cpu(*pp);
1291 ASSERT(bno != NULLFSBLOCK);
1292 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
1293 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
1295 * Go down the tree until leaf level is reached, following the first
1296 * pointer (leftmost) at each level.
1298 while (level-- > 0) {
1299 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
1300 XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
1303 block = XFS_BUF_TO_BLOCK(bp);
1306 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
1307 bno = be64_to_cpu(*pp);
1308 XFS_WANT_CORRUPTED_GOTO(mp,
1309 XFS_FSB_SANITY_CHECK(mp, bno), error0);
1310 xfs_trans_brelse(tp, bp);
1313 * Here with bp and block set to the leftmost leaf node in the tree.
1315 room = xfs_iext_count(ifp);
1318 * Loop over all leaf nodes. Copy information to the extent records.
1321 xfs_bmbt_rec_t *frp;
1322 xfs_fsblock_t nextbno;
1323 xfs_extnum_t num_recs;
1326 num_recs = xfs_btree_get_numrecs(block);
1327 if (unlikely(i + num_recs > room)) {
1328 ASSERT(i + num_recs <= room);
1329 xfs_warn(ip->i_mount,
1330 "corrupt dinode %Lu, (btree extents).",
1331 (unsigned long long) ip->i_ino);
1332 XFS_CORRUPTION_ERROR("xfs_bmap_read_extents(1)",
1333 XFS_ERRLEVEL_LOW, ip->i_mount, block);
1337 * Read-ahead the next leaf block, if any.
1339 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
1340 if (nextbno != NULLFSBLOCK)
1341 xfs_btree_reada_bufl(mp, nextbno, 1,
1344 * Copy records into the extent records.
1346 frp = XFS_BMBT_REC_ADDR(mp, block, 1);
1348 for (j = 0; j < num_recs; j++, i++, frp++) {
1349 xfs_bmbt_rec_host_t *trp = xfs_iext_get_ext(ifp, i);
1350 trp->l0 = be64_to_cpu(frp->l0);
1351 trp->l1 = be64_to_cpu(frp->l1);
1353 if (exntf == XFS_EXTFMT_NOSTATE) {
1355 * Check all attribute bmap btree records and
1356 * any "older" data bmap btree records for a
1357 * set bit in the "extent flag" position.
1359 if (unlikely(xfs_check_nostate_extents(ifp,
1360 start, num_recs))) {
1361 XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
1367 xfs_trans_brelse(tp, bp);
1370 * If we've reached the end, stop.
1372 if (bno == NULLFSBLOCK)
1374 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
1375 XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
1378 block = XFS_BUF_TO_BLOCK(bp);
1380 ASSERT(i == xfs_iext_count(ifp));
1381 ASSERT(i == XFS_IFORK_NEXTENTS(ip, whichfork));
1382 XFS_BMAP_TRACE_EXLIST(ip, i, whichfork);
1385 xfs_trans_brelse(tp, bp);
1386 return -EFSCORRUPTED;
1390 * Returns the file-relative block number of the first unused block(s)
1391 * in the file with at least "len" logically contiguous blocks free.
1392 * This is the lowest-address hole if the file has holes, else the first block
1393 * past the end of file.
1394 * Return 0 if the file is currently local (in-inode).
1397 xfs_bmap_first_unused(
1398 xfs_trans_t *tp, /* transaction pointer */
1399 xfs_inode_t *ip, /* incore inode */
1400 xfs_extlen_t len, /* size of hole to find */
1401 xfs_fileoff_t *first_unused, /* unused block */
1402 int whichfork) /* data or attr fork */
1404 int error; /* error return value */
1405 int idx; /* extent record index */
1406 xfs_ifork_t *ifp; /* inode fork pointer */
1407 xfs_fileoff_t lastaddr; /* last block number seen */
1408 xfs_fileoff_t lowest; /* lowest useful block */
1409 xfs_fileoff_t max; /* starting useful block */
1410 xfs_fileoff_t off; /* offset for this block */
1411 xfs_extnum_t nextents; /* number of extent entries */
1413 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE ||
1414 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ||
1415 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
1416 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
1420 ifp = XFS_IFORK_PTR(ip, whichfork);
1421 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
1422 (error = xfs_iread_extents(tp, ip, whichfork)))
1424 lowest = *first_unused;
1425 nextents = xfs_iext_count(ifp);
1426 for (idx = 0, lastaddr = 0, max = lowest; idx < nextents; idx++) {
1427 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, idx);
1428 off = xfs_bmbt_get_startoff(ep);
1430 * See if the hole before this extent will work.
1432 if (off >= lowest + len && off - max >= len) {
1433 *first_unused = max;
1436 lastaddr = off + xfs_bmbt_get_blockcount(ep);
1437 max = XFS_FILEOFF_MAX(lastaddr, lowest);
1439 *first_unused = max;
1444 * Returns the file-relative block number of the last block - 1 before
1445 * last_block (input value) in the file.
1446 * This is not based on i_size, it is based on the extent records.
1447 * Returns 0 for local files, as they do not have extent records.
1450 xfs_bmap_last_before(
1451 struct xfs_trans *tp, /* transaction pointer */
1452 struct xfs_inode *ip, /* incore inode */
1453 xfs_fileoff_t *last_block, /* last block */
1454 int whichfork) /* data or attr fork */
1456 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1457 struct xfs_bmbt_irec got;
1461 switch (XFS_IFORK_FORMAT(ip, whichfork)) {
1462 case XFS_DINODE_FMT_LOCAL:
1465 case XFS_DINODE_FMT_BTREE:
1466 case XFS_DINODE_FMT_EXTENTS:
1472 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1473 error = xfs_iread_extents(tp, ip, whichfork);
1478 if (xfs_iext_lookup_extent(ip, ifp, *last_block - 1, &idx, &got)) {
1479 if (got.br_startoff <= *last_block - 1)
1483 if (xfs_iext_get_extent(ifp, idx - 1, &got)) {
1484 *last_block = got.br_startoff + got.br_blockcount;
1493 xfs_bmap_last_extent(
1494 struct xfs_trans *tp,
1495 struct xfs_inode *ip,
1497 struct xfs_bmbt_irec *rec,
1500 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1504 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1505 error = xfs_iread_extents(tp, ip, whichfork);
1510 nextents = xfs_iext_count(ifp);
1511 if (nextents == 0) {
1516 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, nextents - 1), rec);
1522 * Check the last inode extent to determine whether this allocation will result
1523 * in blocks being allocated at the end of the file. When we allocate new data
1524 * blocks at the end of the file which do not start at the previous data block,
1525 * we will try to align the new blocks at stripe unit boundaries.
1527 * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be
1528 * at, or past the EOF.
1532 struct xfs_bmalloca *bma,
1535 struct xfs_bmbt_irec rec;
1540 error = xfs_bmap_last_extent(NULL, bma->ip, whichfork, &rec,
1551 * Check if we are allocation or past the last extent, or at least into
1552 * the last delayed allocated extent.
1554 bma->aeof = bma->offset >= rec.br_startoff + rec.br_blockcount ||
1555 (bma->offset >= rec.br_startoff &&
1556 isnullstartblock(rec.br_startblock));
1561 * Returns the file-relative block number of the first block past eof in
1562 * the file. This is not based on i_size, it is based on the extent records.
1563 * Returns 0 for local files, as they do not have extent records.
1566 xfs_bmap_last_offset(
1567 struct xfs_inode *ip,
1568 xfs_fileoff_t *last_block,
1571 struct xfs_bmbt_irec rec;
1577 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL)
1580 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
1581 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
1584 error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, &is_empty);
1585 if (error || is_empty)
1588 *last_block = rec.br_startoff + rec.br_blockcount;
1593 * Returns whether the selected fork of the inode has exactly one
1594 * block or not. For the data fork we check this matches di_size,
1595 * implying the file's range is 0..bsize-1.
1597 int /* 1=>1 block, 0=>otherwise */
1599 xfs_inode_t *ip, /* incore inode */
1600 int whichfork) /* data or attr fork */
1602 xfs_bmbt_rec_host_t *ep; /* ptr to fork's extent */
1603 xfs_ifork_t *ifp; /* inode fork pointer */
1604 int rval; /* return value */
1605 xfs_bmbt_irec_t s; /* internal version of extent */
1608 if (whichfork == XFS_DATA_FORK)
1609 return XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize;
1611 if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1)
1613 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
1615 ifp = XFS_IFORK_PTR(ip, whichfork);
1616 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
1617 ep = xfs_iext_get_ext(ifp, 0);
1618 xfs_bmbt_get_all(ep, &s);
1619 rval = s.br_startoff == 0 && s.br_blockcount == 1;
1620 if (rval && whichfork == XFS_DATA_FORK)
1621 ASSERT(XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize);
1626 * Extent tree manipulation functions used during allocation.
1630 * Convert a delayed allocation to a real allocation.
1632 STATIC int /* error */
1633 xfs_bmap_add_extent_delay_real(
1634 struct xfs_bmalloca *bma,
1637 struct xfs_bmbt_irec *new = &bma->got;
1638 int diff; /* temp value */
1639 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
1640 int error; /* error return value */
1641 int i; /* temp state */
1642 xfs_ifork_t *ifp; /* inode fork pointer */
1643 xfs_fileoff_t new_endoff; /* end offset of new entry */
1644 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
1645 /* left is 0, right is 1, prev is 2 */
1646 int rval=0; /* return value (logging flags) */
1647 int state = 0;/* state bits, accessed thru macros */
1648 xfs_filblks_t da_new; /* new count del alloc blocks used */
1649 xfs_filblks_t da_old; /* old count del alloc blocks used */
1650 xfs_filblks_t temp=0; /* value for da_new calculations */
1651 xfs_filblks_t temp2=0;/* value for da_new calculations */
1652 int tmp_rval; /* partial logging flags */
1653 struct xfs_mount *mp;
1654 xfs_extnum_t *nextents;
1656 mp = bma->ip->i_mount;
1657 ifp = XFS_IFORK_PTR(bma->ip, whichfork);
1658 ASSERT(whichfork != XFS_ATTR_FORK);
1659 nextents = (whichfork == XFS_COW_FORK ? &bma->ip->i_cnextents :
1660 &bma->ip->i_d.di_nextents);
1662 ASSERT(bma->idx >= 0);
1663 ASSERT(bma->idx <= xfs_iext_count(ifp));
1664 ASSERT(!isnullstartblock(new->br_startblock));
1666 (bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
1668 XFS_STATS_INC(mp, xs_add_exlist);
1674 if (whichfork == XFS_COW_FORK)
1675 state |= BMAP_COWFORK;
1678 * Set up a bunch of variables to make the tests simpler.
1680 ep = xfs_iext_get_ext(ifp, bma->idx);
1681 xfs_bmbt_get_all(ep, &PREV);
1682 new_endoff = new->br_startoff + new->br_blockcount;
1683 ASSERT(PREV.br_startoff <= new->br_startoff);
1684 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
1686 da_old = startblockval(PREV.br_startblock);
1690 * Set flags determining what part of the previous delayed allocation
1691 * extent is being replaced by a real allocation.
1693 if (PREV.br_startoff == new->br_startoff)
1694 state |= BMAP_LEFT_FILLING;
1695 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
1696 state |= BMAP_RIGHT_FILLING;
1699 * Check and set flags if this segment has a left neighbor.
1700 * Don't set contiguous if the combined extent would be too large.
1703 state |= BMAP_LEFT_VALID;
1704 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1), &LEFT);
1706 if (isnullstartblock(LEFT.br_startblock))
1707 state |= BMAP_LEFT_DELAY;
1710 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1711 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1712 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
1713 LEFT.br_state == new->br_state &&
1714 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1715 state |= BMAP_LEFT_CONTIG;
1718 * Check and set flags if this segment has a right neighbor.
1719 * Don't set contiguous if the combined extent would be too large.
1720 * Also check for all-three-contiguous being too large.
1722 if (bma->idx < xfs_iext_count(ifp) - 1) {
1723 state |= BMAP_RIGHT_VALID;
1724 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx + 1), &RIGHT);
1726 if (isnullstartblock(RIGHT.br_startblock))
1727 state |= BMAP_RIGHT_DELAY;
1730 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1731 new_endoff == RIGHT.br_startoff &&
1732 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
1733 new->br_state == RIGHT.br_state &&
1734 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
1735 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1736 BMAP_RIGHT_FILLING)) !=
1737 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1738 BMAP_RIGHT_FILLING) ||
1739 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
1741 state |= BMAP_RIGHT_CONTIG;
1745 * Switch out based on the FILLING and CONTIG state bits.
1747 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1748 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
1749 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1750 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1752 * Filling in all of a previously delayed allocation extent.
1753 * The left and right neighbors are both contiguous with new.
1756 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
1757 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
1758 LEFT.br_blockcount + PREV.br_blockcount +
1759 RIGHT.br_blockcount);
1760 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
1762 xfs_iext_remove(bma->ip, bma->idx + 1, 2, state);
1764 if (bma->cur == NULL)
1765 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1767 rval = XFS_ILOG_CORE;
1768 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
1769 RIGHT.br_startblock,
1770 RIGHT.br_blockcount, &i);
1773 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
1774 error = xfs_btree_delete(bma->cur, &i);
1777 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
1778 error = xfs_btree_decrement(bma->cur, 0, &i);
1781 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
1782 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
1784 LEFT.br_blockcount +
1785 PREV.br_blockcount +
1786 RIGHT.br_blockcount, LEFT.br_state);
1792 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1794 * Filling in all of a previously delayed allocation extent.
1795 * The left neighbor is contiguous, the right is not.
1799 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
1800 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
1801 LEFT.br_blockcount + PREV.br_blockcount);
1802 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
1804 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
1805 if (bma->cur == NULL)
1806 rval = XFS_ILOG_DEXT;
1809 error = xfs_bmbt_lookup_eq(bma->cur, LEFT.br_startoff,
1810 LEFT.br_startblock, LEFT.br_blockcount,
1814 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
1815 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
1817 LEFT.br_blockcount +
1818 PREV.br_blockcount, LEFT.br_state);
1824 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1826 * Filling in all of a previously delayed allocation extent.
1827 * The right neighbor is contiguous, the left is not.
1829 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
1830 xfs_bmbt_set_startblock(ep, new->br_startblock);
1831 xfs_bmbt_set_blockcount(ep,
1832 PREV.br_blockcount + RIGHT.br_blockcount);
1833 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
1835 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
1836 if (bma->cur == NULL)
1837 rval = XFS_ILOG_DEXT;
1840 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
1841 RIGHT.br_startblock,
1842 RIGHT.br_blockcount, &i);
1845 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
1846 error = xfs_bmbt_update(bma->cur, PREV.br_startoff,
1848 PREV.br_blockcount +
1849 RIGHT.br_blockcount, PREV.br_state);
1855 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
1857 * Filling in all of a previously delayed allocation extent.
1858 * Neither the left nor right neighbors are contiguous with
1861 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
1862 xfs_bmbt_set_startblock(ep, new->br_startblock);
1863 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
1866 if (bma->cur == NULL)
1867 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1869 rval = XFS_ILOG_CORE;
1870 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
1871 new->br_startblock, new->br_blockcount,
1875 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
1876 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
1877 error = xfs_btree_insert(bma->cur, &i);
1880 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
1884 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
1886 * Filling in the first part of a previous delayed allocation.
1887 * The left neighbor is contiguous.
1889 trace_xfs_bmap_pre_update(bma->ip, bma->idx - 1, state, _THIS_IP_);
1890 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx - 1),
1891 LEFT.br_blockcount + new->br_blockcount);
1892 xfs_bmbt_set_startoff(ep,
1893 PREV.br_startoff + new->br_blockcount);
1894 trace_xfs_bmap_post_update(bma->ip, bma->idx - 1, state, _THIS_IP_);
1896 temp = PREV.br_blockcount - new->br_blockcount;
1897 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
1898 xfs_bmbt_set_blockcount(ep, temp);
1899 if (bma->cur == NULL)
1900 rval = XFS_ILOG_DEXT;
1903 error = xfs_bmbt_lookup_eq(bma->cur, LEFT.br_startoff,
1904 LEFT.br_startblock, LEFT.br_blockcount,
1908 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
1909 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
1911 LEFT.br_blockcount +
1917 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
1918 startblockval(PREV.br_startblock));
1919 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
1920 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
1925 case BMAP_LEFT_FILLING:
1927 * Filling in the first part of a previous delayed allocation.
1928 * The left neighbor is not contiguous.
1930 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
1931 xfs_bmbt_set_startoff(ep, new_endoff);
1932 temp = PREV.br_blockcount - new->br_blockcount;
1933 xfs_bmbt_set_blockcount(ep, temp);
1934 xfs_iext_insert(bma->ip, bma->idx, 1, new, state);
1936 if (bma->cur == NULL)
1937 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1939 rval = XFS_ILOG_CORE;
1940 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
1941 new->br_startblock, new->br_blockcount,
1945 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
1946 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
1947 error = xfs_btree_insert(bma->cur, &i);
1950 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
1953 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
1954 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
1955 bma->firstblock, bma->dfops,
1956 &bma->cur, 1, &tmp_rval, whichfork);
1961 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
1962 startblockval(PREV.br_startblock) -
1963 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
1964 ep = xfs_iext_get_ext(ifp, bma->idx + 1);
1965 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
1966 trace_xfs_bmap_post_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
1969 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1971 * Filling in the last part of a previous delayed allocation.
1972 * The right neighbor is contiguous with the new allocation.
1974 temp = PREV.br_blockcount - new->br_blockcount;
1975 trace_xfs_bmap_pre_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
1976 xfs_bmbt_set_blockcount(ep, temp);
1977 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, bma->idx + 1),
1978 new->br_startoff, new->br_startblock,
1979 new->br_blockcount + RIGHT.br_blockcount,
1981 trace_xfs_bmap_post_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
1982 if (bma->cur == NULL)
1983 rval = XFS_ILOG_DEXT;
1986 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
1987 RIGHT.br_startblock,
1988 RIGHT.br_blockcount, &i);
1991 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
1992 error = xfs_bmbt_update(bma->cur, new->br_startoff,
1994 new->br_blockcount +
1995 RIGHT.br_blockcount,
2001 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
2002 startblockval(PREV.br_startblock));
2003 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
2004 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
2005 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
2010 case BMAP_RIGHT_FILLING:
2012 * Filling in the last part of a previous delayed allocation.
2013 * The right neighbor is not contiguous.
2015 temp = PREV.br_blockcount - new->br_blockcount;
2016 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
2017 xfs_bmbt_set_blockcount(ep, temp);
2018 xfs_iext_insert(bma->ip, bma->idx + 1, 1, new, state);
2020 if (bma->cur == NULL)
2021 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2023 rval = XFS_ILOG_CORE;
2024 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
2025 new->br_startblock, new->br_blockcount,
2029 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
2030 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
2031 error = xfs_btree_insert(bma->cur, &i);
2034 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2037 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
2038 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
2039 bma->firstblock, bma->dfops, &bma->cur, 1,
2040 &tmp_rval, whichfork);
2045 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
2046 startblockval(PREV.br_startblock) -
2047 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
2048 ep = xfs_iext_get_ext(ifp, bma->idx);
2049 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
2050 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
2057 * Filling in the middle part of a previous delayed allocation.
2058 * Contiguity is impossible here.
2059 * This case is avoided almost all the time.
2061 * We start with a delayed allocation:
2063 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
2066 * and we are allocating:
2067 * +rrrrrrrrrrrrrrrrr+
2070 * and we set it up for insertion as:
2071 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
2073 * PREV @ idx LEFT RIGHT
2074 * inserted at idx + 1
2076 temp = new->br_startoff - PREV.br_startoff;
2077 temp2 = PREV.br_startoff + PREV.br_blockcount - new_endoff;
2078 trace_xfs_bmap_pre_update(bma->ip, bma->idx, 0, _THIS_IP_);
2079 xfs_bmbt_set_blockcount(ep, temp); /* truncate PREV */
2081 RIGHT.br_state = PREV.br_state;
2082 RIGHT.br_startblock = nullstartblock(
2083 (int)xfs_bmap_worst_indlen(bma->ip, temp2));
2084 RIGHT.br_startoff = new_endoff;
2085 RIGHT.br_blockcount = temp2;
2086 /* insert LEFT (r[0]) and RIGHT (r[1]) at the same time */
2087 xfs_iext_insert(bma->ip, bma->idx + 1, 2, &LEFT, state);
2089 if (bma->cur == NULL)
2090 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2092 rval = XFS_ILOG_CORE;
2093 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
2094 new->br_startblock, new->br_blockcount,
2098 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
2099 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
2100 error = xfs_btree_insert(bma->cur, &i);
2103 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2106 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
2107 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
2108 bma->firstblock, bma->dfops, &bma->cur,
2109 1, &tmp_rval, whichfork);
2114 temp = xfs_bmap_worst_indlen(bma->ip, temp);
2115 temp2 = xfs_bmap_worst_indlen(bma->ip, temp2);
2116 diff = (int)(temp + temp2 - startblockval(PREV.br_startblock) -
2117 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
2119 error = xfs_mod_fdblocks(bma->ip->i_mount,
2120 -((int64_t)diff), false);
2126 ep = xfs_iext_get_ext(ifp, bma->idx);
2127 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
2128 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
2129 trace_xfs_bmap_pre_update(bma->ip, bma->idx + 2, state, _THIS_IP_);
2130 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, bma->idx + 2),
2131 nullstartblock((int)temp2));
2132 trace_xfs_bmap_post_update(bma->ip, bma->idx + 2, state, _THIS_IP_);
2135 da_new = temp + temp2;
2138 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2139 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2140 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
2141 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2142 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2143 case BMAP_LEFT_CONTIG:
2144 case BMAP_RIGHT_CONTIG:
2146 * These cases are all impossible.
2151 /* add reverse mapping */
2152 error = xfs_rmap_map_extent(mp, bma->dfops, bma->ip, whichfork, new);
2156 /* convert to a btree if necessary */
2157 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
2158 int tmp_logflags; /* partial log flag return val */
2160 ASSERT(bma->cur == NULL);
2161 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
2162 bma->firstblock, bma->dfops, &bma->cur,
2163 da_old > 0, &tmp_logflags, whichfork);
2164 bma->logflags |= tmp_logflags;
2169 /* adjust for changes in reserved delayed indirect blocks */
2170 if (da_old || da_new) {
2173 temp += bma->cur->bc_private.b.allocated;
2174 ASSERT(temp <= da_old);
2176 xfs_mod_fdblocks(bma->ip->i_mount,
2177 (int64_t)(da_old - temp), false);
2180 /* clear out the allocated field, done with it now in any case. */
2182 bma->cur->bc_private.b.allocated = 0;
2184 xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork);
2186 if (whichfork != XFS_COW_FORK)
2187 bma->logflags |= rval;
2195 * Convert an unwritten allocation to a real allocation or vice versa.
2197 STATIC int /* error */
2198 xfs_bmap_add_extent_unwritten_real(
2199 struct xfs_trans *tp,
2200 xfs_inode_t *ip, /* incore inode pointer */
2201 xfs_extnum_t *idx, /* extent number to update/insert */
2202 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
2203 xfs_bmbt_irec_t *new, /* new data to add to file extents */
2204 xfs_fsblock_t *first, /* pointer to firstblock variable */
2205 struct xfs_defer_ops *dfops, /* list of extents to be freed */
2206 int *logflagsp) /* inode logging flags */
2208 xfs_btree_cur_t *cur; /* btree cursor */
2209 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
2210 int error; /* error return value */
2211 int i; /* temp state */
2212 xfs_ifork_t *ifp; /* inode fork pointer */
2213 xfs_fileoff_t new_endoff; /* end offset of new entry */
2214 xfs_exntst_t newext; /* new extent state */
2215 xfs_exntst_t oldext; /* old extent state */
2216 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
2217 /* left is 0, right is 1, prev is 2 */
2218 int rval=0; /* return value (logging flags) */
2219 int state = 0;/* state bits, accessed thru macros */
2220 struct xfs_mount *mp = tp->t_mountp;
2225 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
2228 ASSERT(*idx <= xfs_iext_count(ifp));
2229 ASSERT(!isnullstartblock(new->br_startblock));
2231 XFS_STATS_INC(mp, xs_add_exlist);
2238 * Set up a bunch of variables to make the tests simpler.
2241 ep = xfs_iext_get_ext(ifp, *idx);
2242 xfs_bmbt_get_all(ep, &PREV);
2243 newext = new->br_state;
2244 oldext = (newext == XFS_EXT_UNWRITTEN) ?
2245 XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
2246 ASSERT(PREV.br_state == oldext);
2247 new_endoff = new->br_startoff + new->br_blockcount;
2248 ASSERT(PREV.br_startoff <= new->br_startoff);
2249 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
2252 * Set flags determining what part of the previous oldext allocation
2253 * extent is being replaced by a newext allocation.
2255 if (PREV.br_startoff == new->br_startoff)
2256 state |= BMAP_LEFT_FILLING;
2257 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
2258 state |= BMAP_RIGHT_FILLING;
2261 * Check and set flags if this segment has a left neighbor.
2262 * Don't set contiguous if the combined extent would be too large.
2265 state |= BMAP_LEFT_VALID;
2266 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &LEFT);
2268 if (isnullstartblock(LEFT.br_startblock))
2269 state |= BMAP_LEFT_DELAY;
2272 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2273 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
2274 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
2275 LEFT.br_state == newext &&
2276 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2277 state |= BMAP_LEFT_CONTIG;
2280 * Check and set flags if this segment has a right neighbor.
2281 * Don't set contiguous if the combined extent would be too large.
2282 * Also check for all-three-contiguous being too large.
2284 if (*idx < xfs_iext_count(&ip->i_df) - 1) {
2285 state |= BMAP_RIGHT_VALID;
2286 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx + 1), &RIGHT);
2287 if (isnullstartblock(RIGHT.br_startblock))
2288 state |= BMAP_RIGHT_DELAY;
2291 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2292 new_endoff == RIGHT.br_startoff &&
2293 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
2294 newext == RIGHT.br_state &&
2295 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
2296 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2297 BMAP_RIGHT_FILLING)) !=
2298 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2299 BMAP_RIGHT_FILLING) ||
2300 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
2302 state |= BMAP_RIGHT_CONTIG;
2305 * Switch out based on the FILLING and CONTIG state bits.
2307 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2308 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
2309 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2310 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
2312 * Setting all of a previous oldext extent to newext.
2313 * The left and right neighbors are both contiguous with new.
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 RIGHT.br_blockcount);
2321 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2323 xfs_iext_remove(ip, *idx + 1, 2, state);
2324 ip->i_d.di_nextents -= 2;
2326 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2328 rval = XFS_ILOG_CORE;
2329 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
2330 RIGHT.br_startblock,
2331 RIGHT.br_blockcount, &i)))
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_btree_delete(cur, &i)))
2342 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2343 if ((error = xfs_btree_decrement(cur, 0, &i)))
2345 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2346 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
2348 LEFT.br_blockcount + PREV.br_blockcount +
2349 RIGHT.br_blockcount, LEFT.br_state)))
2354 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2356 * Setting all of a previous oldext extent to newext.
2357 * The left neighbor is contiguous, the right is not.
2361 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2362 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
2363 LEFT.br_blockcount + PREV.br_blockcount);
2364 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2366 xfs_iext_remove(ip, *idx + 1, 1, state);
2367 ip->i_d.di_nextents--;
2369 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2371 rval = XFS_ILOG_CORE;
2372 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2373 PREV.br_startblock, PREV.br_blockcount,
2376 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2377 if ((error = xfs_btree_delete(cur, &i)))
2379 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2380 if ((error = xfs_btree_decrement(cur, 0, &i)))
2382 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2383 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
2385 LEFT.br_blockcount + PREV.br_blockcount,
2391 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
2393 * Setting all of a previous oldext extent to newext.
2394 * The right neighbor is contiguous, the left is not.
2396 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2397 xfs_bmbt_set_blockcount(ep,
2398 PREV.br_blockcount + RIGHT.br_blockcount);
2399 xfs_bmbt_set_state(ep, newext);
2400 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2401 xfs_iext_remove(ip, *idx + 1, 1, state);
2402 ip->i_d.di_nextents--;
2404 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2406 rval = XFS_ILOG_CORE;
2407 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
2408 RIGHT.br_startblock,
2409 RIGHT.br_blockcount, &i)))
2411 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2412 if ((error = xfs_btree_delete(cur, &i)))
2414 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2415 if ((error = xfs_btree_decrement(cur, 0, &i)))
2417 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2418 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2420 new->br_blockcount + RIGHT.br_blockcount,
2426 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
2428 * Setting all of a previous oldext extent to newext.
2429 * Neither the left nor right neighbors are contiguous with
2432 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2433 xfs_bmbt_set_state(ep, newext);
2434 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2437 rval = XFS_ILOG_DEXT;
2440 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
2441 new->br_startblock, new->br_blockcount,
2444 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2445 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2446 new->br_startblock, new->br_blockcount,
2452 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
2454 * Setting the first part of a previous oldext extent to newext.
2455 * The left neighbor is contiguous.
2457 trace_xfs_bmap_pre_update(ip, *idx - 1, state, _THIS_IP_);
2458 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx - 1),
2459 LEFT.br_blockcount + new->br_blockcount);
2460 xfs_bmbt_set_startoff(ep,
2461 PREV.br_startoff + new->br_blockcount);
2462 trace_xfs_bmap_post_update(ip, *idx - 1, state, _THIS_IP_);
2464 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2465 xfs_bmbt_set_startblock(ep,
2466 new->br_startblock + new->br_blockcount);
2467 xfs_bmbt_set_blockcount(ep,
2468 PREV.br_blockcount - new->br_blockcount);
2469 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2474 rval = XFS_ILOG_DEXT;
2477 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2478 PREV.br_startblock, PREV.br_blockcount,
2481 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2482 if ((error = xfs_bmbt_update(cur,
2483 PREV.br_startoff + new->br_blockcount,
2484 PREV.br_startblock + new->br_blockcount,
2485 PREV.br_blockcount - new->br_blockcount,
2488 if ((error = xfs_btree_decrement(cur, 0, &i)))
2490 error = xfs_bmbt_update(cur, LEFT.br_startoff,
2492 LEFT.br_blockcount + new->br_blockcount,
2499 case BMAP_LEFT_FILLING:
2501 * Setting the first part of a previous oldext extent to newext.
2502 * The left neighbor is not contiguous.
2504 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2505 ASSERT(ep && xfs_bmbt_get_state(ep) == oldext);
2506 xfs_bmbt_set_startoff(ep, new_endoff);
2507 xfs_bmbt_set_blockcount(ep,
2508 PREV.br_blockcount - new->br_blockcount);
2509 xfs_bmbt_set_startblock(ep,
2510 new->br_startblock + new->br_blockcount);
2511 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2513 xfs_iext_insert(ip, *idx, 1, new, state);
2514 ip->i_d.di_nextents++;
2516 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2518 rval = XFS_ILOG_CORE;
2519 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2520 PREV.br_startblock, PREV.br_blockcount,
2523 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2524 if ((error = xfs_bmbt_update(cur,
2525 PREV.br_startoff + new->br_blockcount,
2526 PREV.br_startblock + new->br_blockcount,
2527 PREV.br_blockcount - new->br_blockcount,
2530 cur->bc_rec.b = *new;
2531 if ((error = xfs_btree_insert(cur, &i)))
2533 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2537 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
2539 * Setting the last part of a previous oldext extent to newext.
2540 * The right neighbor is contiguous with the new allocation.
2542 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2543 xfs_bmbt_set_blockcount(ep,
2544 PREV.br_blockcount - new->br_blockcount);
2545 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2549 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2550 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx),
2551 new->br_startoff, new->br_startblock,
2552 new->br_blockcount + RIGHT.br_blockcount, newext);
2553 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2556 rval = XFS_ILOG_DEXT;
2559 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2561 PREV.br_blockcount, &i)))
2563 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2564 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
2566 PREV.br_blockcount - new->br_blockcount,
2569 if ((error = xfs_btree_increment(cur, 0, &i)))
2571 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2573 new->br_blockcount + RIGHT.br_blockcount,
2579 case BMAP_RIGHT_FILLING:
2581 * Setting the last part of a previous oldext extent to newext.
2582 * The right neighbor is not contiguous.
2584 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2585 xfs_bmbt_set_blockcount(ep,
2586 PREV.br_blockcount - new->br_blockcount);
2587 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2590 xfs_iext_insert(ip, *idx, 1, new, state);
2592 ip->i_d.di_nextents++;
2594 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2596 rval = XFS_ILOG_CORE;
2597 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2598 PREV.br_startblock, PREV.br_blockcount,
2601 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2602 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
2604 PREV.br_blockcount - new->br_blockcount,
2607 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
2608 new->br_startblock, new->br_blockcount,
2611 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
2612 cur->bc_rec.b.br_state = XFS_EXT_NORM;
2613 if ((error = xfs_btree_insert(cur, &i)))
2615 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2621 * Setting the middle part of a previous oldext extent to
2622 * newext. Contiguity is impossible here.
2623 * One extent becomes three extents.
2625 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2626 xfs_bmbt_set_blockcount(ep,
2627 new->br_startoff - PREV.br_startoff);
2628 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2631 r[1].br_startoff = new_endoff;
2632 r[1].br_blockcount =
2633 PREV.br_startoff + PREV.br_blockcount - new_endoff;
2634 r[1].br_startblock = new->br_startblock + new->br_blockcount;
2635 r[1].br_state = oldext;
2638 xfs_iext_insert(ip, *idx, 2, &r[0], state);
2640 ip->i_d.di_nextents += 2;
2642 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2644 rval = XFS_ILOG_CORE;
2645 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2646 PREV.br_startblock, PREV.br_blockcount,
2649 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2650 /* new right extent - oldext */
2651 if ((error = xfs_bmbt_update(cur, r[1].br_startoff,
2652 r[1].br_startblock, r[1].br_blockcount,
2655 /* new left extent - oldext */
2656 cur->bc_rec.b = PREV;
2657 cur->bc_rec.b.br_blockcount =
2658 new->br_startoff - PREV.br_startoff;
2659 if ((error = xfs_btree_insert(cur, &i)))
2661 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2663 * Reset the cursor to the position of the new extent
2664 * we are about to insert as we can't trust it after
2665 * the previous insert.
2667 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
2668 new->br_startblock, new->br_blockcount,
2671 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
2672 /* new middle extent - newext */
2673 cur->bc_rec.b.br_state = new->br_state;
2674 if ((error = xfs_btree_insert(cur, &i)))
2676 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2680 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2681 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2682 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
2683 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2684 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2685 case BMAP_LEFT_CONTIG:
2686 case BMAP_RIGHT_CONTIG:
2688 * These cases are all impossible.
2693 /* update reverse mappings */
2694 error = xfs_rmap_convert_extent(mp, dfops, ip, XFS_DATA_FORK, new);
2698 /* convert to a btree if necessary */
2699 if (xfs_bmap_needs_btree(ip, XFS_DATA_FORK)) {
2700 int tmp_logflags; /* partial log flag return val */
2702 ASSERT(cur == NULL);
2703 error = xfs_bmap_extents_to_btree(tp, ip, first, dfops, &cur,
2704 0, &tmp_logflags, XFS_DATA_FORK);
2705 *logflagsp |= tmp_logflags;
2710 /* clear out the allocated field, done with it now in any case. */
2712 cur->bc_private.b.allocated = 0;
2716 xfs_bmap_check_leaf_extents(*curp, ip, XFS_DATA_FORK);
2726 * Convert a hole to a delayed allocation.
2729 xfs_bmap_add_extent_hole_delay(
2730 xfs_inode_t *ip, /* incore inode pointer */
2732 xfs_extnum_t *idx, /* extent number to update/insert */
2733 xfs_bmbt_irec_t *new) /* new data to add to file extents */
2735 xfs_ifork_t *ifp; /* inode fork pointer */
2736 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2737 xfs_filblks_t newlen=0; /* new indirect size */
2738 xfs_filblks_t oldlen=0; /* old indirect size */
2739 xfs_bmbt_irec_t right; /* right neighbor extent entry */
2740 int state; /* state bits, accessed thru macros */
2741 xfs_filblks_t temp=0; /* temp for indirect calculations */
2743 ifp = XFS_IFORK_PTR(ip, whichfork);
2745 if (whichfork == XFS_COW_FORK)
2746 state |= BMAP_COWFORK;
2747 ASSERT(isnullstartblock(new->br_startblock));
2750 * Check and set flags if this segment has a left neighbor
2753 state |= BMAP_LEFT_VALID;
2754 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &left);
2756 if (isnullstartblock(left.br_startblock))
2757 state |= BMAP_LEFT_DELAY;
2761 * Check and set flags if the current (right) segment exists.
2762 * If it doesn't exist, we're converting the hole at end-of-file.
2764 if (*idx < xfs_iext_count(ifp)) {
2765 state |= BMAP_RIGHT_VALID;
2766 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx), &right);
2768 if (isnullstartblock(right.br_startblock))
2769 state |= BMAP_RIGHT_DELAY;
2773 * Set contiguity flags on the left and right neighbors.
2774 * Don't let extents get too large, even if the pieces are contiguous.
2776 if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) &&
2777 left.br_startoff + left.br_blockcount == new->br_startoff &&
2778 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2779 state |= BMAP_LEFT_CONTIG;
2781 if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) &&
2782 new->br_startoff + new->br_blockcount == right.br_startoff &&
2783 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2784 (!(state & BMAP_LEFT_CONTIG) ||
2785 (left.br_blockcount + new->br_blockcount +
2786 right.br_blockcount <= MAXEXTLEN)))
2787 state |= BMAP_RIGHT_CONTIG;
2790 * Switch out based on the contiguity flags.
2792 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2793 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2795 * New allocation is contiguous with delayed allocations
2796 * on the left and on the right.
2797 * Merge all three into a single extent record.
2800 temp = left.br_blockcount + new->br_blockcount +
2801 right.br_blockcount;
2803 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2804 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp);
2805 oldlen = startblockval(left.br_startblock) +
2806 startblockval(new->br_startblock) +
2807 startblockval(right.br_startblock);
2808 newlen = xfs_bmap_worst_indlen(ip, temp);
2809 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx),
2810 nullstartblock((int)newlen));
2811 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2813 xfs_iext_remove(ip, *idx + 1, 1, state);
2816 case BMAP_LEFT_CONTIG:
2818 * New allocation is contiguous with a delayed allocation
2820 * Merge the new allocation with the left neighbor.
2823 temp = left.br_blockcount + new->br_blockcount;
2825 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2826 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp);
2827 oldlen = startblockval(left.br_startblock) +
2828 startblockval(new->br_startblock);
2829 newlen = xfs_bmap_worst_indlen(ip, temp);
2830 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx),
2831 nullstartblock((int)newlen));
2832 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2835 case BMAP_RIGHT_CONTIG:
2837 * New allocation is contiguous with a delayed allocation
2839 * Merge the new allocation with the right neighbor.
2841 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2842 temp = new->br_blockcount + right.br_blockcount;
2843 oldlen = startblockval(new->br_startblock) +
2844 startblockval(right.br_startblock);
2845 newlen = xfs_bmap_worst_indlen(ip, temp);
2846 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx),
2848 nullstartblock((int)newlen), temp, right.br_state);
2849 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2854 * New allocation is not contiguous with another
2855 * delayed allocation.
2856 * Insert a new entry.
2858 oldlen = newlen = 0;
2859 xfs_iext_insert(ip, *idx, 1, new, state);
2862 if (oldlen != newlen) {
2863 ASSERT(oldlen > newlen);
2864 xfs_mod_fdblocks(ip->i_mount, (int64_t)(oldlen - newlen),
2867 * Nothing to do for disk quota accounting here.
2873 * Convert a hole to a real allocation.
2875 STATIC int /* error */
2876 xfs_bmap_add_extent_hole_real(
2877 struct xfs_bmalloca *bma,
2880 struct xfs_bmbt_irec *new = &bma->got;
2881 int error; /* error return value */
2882 int i; /* temp state */
2883 xfs_ifork_t *ifp; /* inode fork pointer */
2884 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2885 xfs_bmbt_irec_t right; /* right neighbor extent entry */
2886 int rval=0; /* return value (logging flags) */
2887 int state; /* state bits, accessed thru macros */
2888 struct xfs_mount *mp;
2890 mp = bma->ip->i_mount;
2891 ifp = XFS_IFORK_PTR(bma->ip, whichfork);
2893 ASSERT(bma->idx >= 0);
2894 ASSERT(bma->idx <= xfs_iext_count(ifp));
2895 ASSERT(!isnullstartblock(new->br_startblock));
2897 !(bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
2898 ASSERT(whichfork != XFS_COW_FORK);
2900 XFS_STATS_INC(mp, xs_add_exlist);
2903 if (whichfork == XFS_ATTR_FORK)
2904 state |= BMAP_ATTRFORK;
2907 * Check and set flags if this segment has a left neighbor.
2910 state |= BMAP_LEFT_VALID;
2911 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1), &left);
2912 if (isnullstartblock(left.br_startblock))
2913 state |= BMAP_LEFT_DELAY;
2917 * Check and set flags if this segment has a current value.
2918 * Not true if we're inserting into the "hole" at eof.
2920 if (bma->idx < xfs_iext_count(ifp)) {
2921 state |= BMAP_RIGHT_VALID;
2922 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &right);
2923 if (isnullstartblock(right.br_startblock))
2924 state |= BMAP_RIGHT_DELAY;
2928 * We're inserting a real allocation between "left" and "right".
2929 * Set the contiguity flags. Don't let extents get too large.
2931 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2932 left.br_startoff + left.br_blockcount == new->br_startoff &&
2933 left.br_startblock + left.br_blockcount == new->br_startblock &&
2934 left.br_state == new->br_state &&
2935 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2936 state |= BMAP_LEFT_CONTIG;
2938 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2939 new->br_startoff + new->br_blockcount == right.br_startoff &&
2940 new->br_startblock + new->br_blockcount == right.br_startblock &&
2941 new->br_state == right.br_state &&
2942 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2943 (!(state & BMAP_LEFT_CONTIG) ||
2944 left.br_blockcount + new->br_blockcount +
2945 right.br_blockcount <= MAXEXTLEN))
2946 state |= BMAP_RIGHT_CONTIG;
2950 * Select which case we're in here, and implement it.
2952 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2953 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2955 * New allocation is contiguous with real allocations on the
2956 * left and on the right.
2957 * Merge all three into a single extent record.
2960 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
2961 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
2962 left.br_blockcount + new->br_blockcount +
2963 right.br_blockcount);
2964 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
2966 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
2968 XFS_IFORK_NEXT_SET(bma->ip, whichfork,
2969 XFS_IFORK_NEXTENTS(bma->ip, whichfork) - 1);
2970 if (bma->cur == NULL) {
2971 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
2973 rval = XFS_ILOG_CORE;
2974 error = xfs_bmbt_lookup_eq(bma->cur, right.br_startoff,
2975 right.br_startblock, right.br_blockcount,
2979 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2980 error = xfs_btree_delete(bma->cur, &i);
2983 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2984 error = xfs_btree_decrement(bma->cur, 0, &i);
2987 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2988 error = xfs_bmbt_update(bma->cur, left.br_startoff,
2990 left.br_blockcount +
2991 new->br_blockcount +
2992 right.br_blockcount,
2999 case BMAP_LEFT_CONTIG:
3001 * New allocation is contiguous with a real allocation
3003 * Merge the new allocation with the left neighbor.
3006 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
3007 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
3008 left.br_blockcount + new->br_blockcount);
3009 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
3011 if (bma->cur == NULL) {
3012 rval = xfs_ilog_fext(whichfork);
3015 error = xfs_bmbt_lookup_eq(bma->cur, left.br_startoff,
3016 left.br_startblock, left.br_blockcount,
3020 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
3021 error = xfs_bmbt_update(bma->cur, left.br_startoff,
3023 left.br_blockcount +
3031 case BMAP_RIGHT_CONTIG:
3033 * New allocation is contiguous with a real allocation
3035 * Merge the new allocation with the right neighbor.
3037 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
3038 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, bma->idx),
3039 new->br_startoff, new->br_startblock,
3040 new->br_blockcount + right.br_blockcount,
3042 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
3044 if (bma->cur == NULL) {
3045 rval = xfs_ilog_fext(whichfork);
3048 error = xfs_bmbt_lookup_eq(bma->cur,
3050 right.br_startblock,
3051 right.br_blockcount, &i);
3054 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
3055 error = xfs_bmbt_update(bma->cur, new->br_startoff,
3057 new->br_blockcount +
3058 right.br_blockcount,
3067 * New allocation is not contiguous with another
3069 * Insert a new entry.
3071 xfs_iext_insert(bma->ip, bma->idx, 1, new, state);
3072 XFS_IFORK_NEXT_SET(bma->ip, whichfork,
3073 XFS_IFORK_NEXTENTS(bma->ip, whichfork) + 1);
3074 if (bma->cur == NULL) {
3075 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
3077 rval = XFS_ILOG_CORE;
3078 error = xfs_bmbt_lookup_eq(bma->cur,
3081 new->br_blockcount, &i);
3084 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
3085 bma->cur->bc_rec.b.br_state = new->br_state;
3086 error = xfs_btree_insert(bma->cur, &i);
3089 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
3094 /* add reverse mapping */
3095 error = xfs_rmap_map_extent(mp, bma->dfops, bma->ip, whichfork, new);
3099 /* convert to a btree if necessary */
3100 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
3101 int tmp_logflags; /* partial log flag return val */
3103 ASSERT(bma->cur == NULL);
3104 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
3105 bma->firstblock, bma->dfops, &bma->cur,
3106 0, &tmp_logflags, whichfork);
3107 bma->logflags |= tmp_logflags;
3112 /* clear out the allocated field, done with it now in any case. */
3114 bma->cur->bc_private.b.allocated = 0;
3116 xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork);
3118 bma->logflags |= rval;
3123 * Functions used in the extent read, allocate and remove paths
3127 * Adjust the size of the new extent based on di_extsize and rt extsize.
3130 xfs_bmap_extsize_align(
3132 xfs_bmbt_irec_t *gotp, /* next extent pointer */
3133 xfs_bmbt_irec_t *prevp, /* previous extent pointer */
3134 xfs_extlen_t extsz, /* align to this extent size */
3135 int rt, /* is this a realtime inode? */
3136 int eof, /* is extent at end-of-file? */
3137 int delay, /* creating delalloc extent? */
3138 int convert, /* overwriting unwritten extent? */
3139 xfs_fileoff_t *offp, /* in/out: aligned offset */
3140 xfs_extlen_t *lenp) /* in/out: aligned length */
3142 xfs_fileoff_t orig_off; /* original offset */
3143 xfs_extlen_t orig_alen; /* original length */
3144 xfs_fileoff_t orig_end; /* original off+len */
3145 xfs_fileoff_t nexto; /* next file offset */
3146 xfs_fileoff_t prevo; /* previous file offset */
3147 xfs_fileoff_t align_off; /* temp for offset */
3148 xfs_extlen_t align_alen; /* temp for length */
3149 xfs_extlen_t temp; /* temp for calculations */
3154 orig_off = align_off = *offp;
3155 orig_alen = align_alen = *lenp;
3156 orig_end = orig_off + orig_alen;
3159 * If this request overlaps an existing extent, then don't
3160 * attempt to perform any additional alignment.
3162 if (!delay && !eof &&
3163 (orig_off >= gotp->br_startoff) &&
3164 (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
3169 * If the file offset is unaligned vs. the extent size
3170 * we need to align it. This will be possible unless
3171 * the file was previously written with a kernel that didn't
3172 * perform this alignment, or if a truncate shot us in the
3175 temp = do_mod(orig_off, extsz);
3181 /* Same adjustment for the end of the requested area. */
3182 temp = (align_alen % extsz);
3184 align_alen += extsz - temp;
3187 * For large extent hint sizes, the aligned extent might be larger than
3188 * MAXEXTLEN. In that case, reduce the size by an extsz so that it pulls
3189 * the length back under MAXEXTLEN. The outer allocation loops handle
3190 * short allocation just fine, so it is safe to do this. We only want to
3191 * do it when we are forced to, though, because it means more allocation
3192 * operations are required.
3194 while (align_alen > MAXEXTLEN)
3195 align_alen -= extsz;
3196 ASSERT(align_alen <= MAXEXTLEN);
3199 * If the previous block overlaps with this proposed allocation
3200 * then move the start forward without adjusting the length.
3202 if (prevp->br_startoff != NULLFILEOFF) {
3203 if (prevp->br_startblock == HOLESTARTBLOCK)
3204 prevo = prevp->br_startoff;
3206 prevo = prevp->br_startoff + prevp->br_blockcount;
3209 if (align_off != orig_off && align_off < prevo)
3212 * If the next block overlaps with this proposed allocation
3213 * then move the start back without adjusting the length,
3214 * but not before offset 0.
3215 * This may of course make the start overlap previous block,
3216 * and if we hit the offset 0 limit then the next block
3217 * can still overlap too.
3219 if (!eof && gotp->br_startoff != NULLFILEOFF) {
3220 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
3221 (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
3222 nexto = gotp->br_startoff + gotp->br_blockcount;
3224 nexto = gotp->br_startoff;
3226 nexto = NULLFILEOFF;
3228 align_off + align_alen != orig_end &&
3229 align_off + align_alen > nexto)
3230 align_off = nexto > align_alen ? nexto - align_alen : 0;
3232 * If we're now overlapping the next or previous extent that
3233 * means we can't fit an extsz piece in this hole. Just move
3234 * the start forward to the first valid spot and set
3235 * the length so we hit the end.
3237 if (align_off != orig_off && align_off < prevo)
3239 if (align_off + align_alen != orig_end &&
3240 align_off + align_alen > nexto &&
3241 nexto != NULLFILEOFF) {
3242 ASSERT(nexto > prevo);
3243 align_alen = nexto - align_off;
3247 * If realtime, and the result isn't a multiple of the realtime
3248 * extent size we need to remove blocks until it is.
3250 if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
3252 * We're not covering the original request, or
3253 * we won't be able to once we fix the length.
3255 if (orig_off < align_off ||
3256 orig_end > align_off + align_alen ||
3257 align_alen - temp < orig_alen)
3260 * Try to fix it by moving the start up.
3262 if (align_off + temp <= orig_off) {
3267 * Try to fix it by moving the end in.
3269 else if (align_off + align_alen - temp >= orig_end)
3272 * Set the start to the minimum then trim the length.
3275 align_alen -= orig_off - align_off;
3276 align_off = orig_off;
3277 align_alen -= align_alen % mp->m_sb.sb_rextsize;
3280 * Result doesn't cover the request, fail it.
3282 if (orig_off < align_off || orig_end > align_off + align_alen)
3285 ASSERT(orig_off >= align_off);
3286 /* see MAXEXTLEN handling above */
3287 ASSERT(orig_end <= align_off + align_alen ||
3288 align_alen + extsz > MAXEXTLEN);
3292 if (!eof && gotp->br_startoff != NULLFILEOFF)
3293 ASSERT(align_off + align_alen <= gotp->br_startoff);
3294 if (prevp->br_startoff != NULLFILEOFF)
3295 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
3303 #define XFS_ALLOC_GAP_UNITS 4
3307 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
3309 xfs_fsblock_t adjust; /* adjustment to block numbers */
3310 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
3311 xfs_mount_t *mp; /* mount point structure */
3312 int nullfb; /* true if ap->firstblock isn't set */
3313 int rt; /* true if inode is realtime */
3315 #define ISVALID(x,y) \
3317 (x) < mp->m_sb.sb_rblocks : \
3318 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
3319 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
3320 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
3322 mp = ap->ip->i_mount;
3323 nullfb = *ap->firstblock == NULLFSBLOCK;
3324 rt = XFS_IS_REALTIME_INODE(ap->ip) &&
3325 xfs_alloc_is_userdata(ap->datatype);
3326 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
3328 * If allocating at eof, and there's a previous real block,
3329 * try to use its last block as our starting point.
3331 if (ap->eof && ap->prev.br_startoff != NULLFILEOFF &&
3332 !isnullstartblock(ap->prev.br_startblock) &&
3333 ISVALID(ap->prev.br_startblock + ap->prev.br_blockcount,
3334 ap->prev.br_startblock)) {
3335 ap->blkno = ap->prev.br_startblock + ap->prev.br_blockcount;
3337 * Adjust for the gap between prevp and us.
3339 adjust = ap->offset -
3340 (ap->prev.br_startoff + ap->prev.br_blockcount);
3342 ISVALID(ap->blkno + adjust, ap->prev.br_startblock))
3343 ap->blkno += adjust;
3346 * If not at eof, then compare the two neighbor blocks.
3347 * Figure out whether either one gives us a good starting point,
3348 * and pick the better one.
3350 else if (!ap->eof) {
3351 xfs_fsblock_t gotbno; /* right side block number */
3352 xfs_fsblock_t gotdiff=0; /* right side difference */
3353 xfs_fsblock_t prevbno; /* left side block number */
3354 xfs_fsblock_t prevdiff=0; /* left side difference */
3357 * If there's a previous (left) block, select a requested
3358 * start block based on it.
3360 if (ap->prev.br_startoff != NULLFILEOFF &&
3361 !isnullstartblock(ap->prev.br_startblock) &&
3362 (prevbno = ap->prev.br_startblock +
3363 ap->prev.br_blockcount) &&
3364 ISVALID(prevbno, ap->prev.br_startblock)) {
3366 * Calculate gap to end of previous block.
3368 adjust = prevdiff = ap->offset -
3369 (ap->prev.br_startoff +
3370 ap->prev.br_blockcount);
3372 * Figure the startblock based on the previous block's
3373 * end and the gap size.
3375 * If the gap is large relative to the piece we're
3376 * allocating, or using it gives us an invalid block
3377 * number, then just use the end of the previous block.
3379 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
3380 ISVALID(prevbno + prevdiff,
3381 ap->prev.br_startblock))
3386 * If the firstblock forbids it, can't use it,
3389 if (!rt && !nullfb &&
3390 XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
3391 prevbno = NULLFSBLOCK;
3394 * No previous block or can't follow it, just default.
3397 prevbno = NULLFSBLOCK;
3399 * If there's a following (right) block, select a requested
3400 * start block based on it.
3402 if (!isnullstartblock(ap->got.br_startblock)) {
3404 * Calculate gap to start of next block.
3406 adjust = gotdiff = ap->got.br_startoff - ap->offset;
3408 * Figure the startblock based on the next block's
3409 * start and the gap size.
3411 gotbno = ap->got.br_startblock;
3414 * If the gap is large relative to the piece we're
3415 * allocating, or using it gives us an invalid block
3416 * number, then just use the start of the next block
3417 * offset by our length.
3419 if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
3420 ISVALID(gotbno - gotdiff, gotbno))
3422 else if (ISVALID(gotbno - ap->length, gotbno)) {
3423 gotbno -= ap->length;
3424 gotdiff += adjust - ap->length;
3428 * If the firstblock forbids it, can't use it,
3431 if (!rt && !nullfb &&
3432 XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
3433 gotbno = NULLFSBLOCK;
3436 * No next block, just default.
3439 gotbno = NULLFSBLOCK;
3441 * If both valid, pick the better one, else the only good
3442 * one, else ap->blkno is already set (to 0 or the inode block).
3444 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
3445 ap->blkno = prevdiff <= gotdiff ? prevbno : gotbno;
3446 else if (prevbno != NULLFSBLOCK)
3447 ap->blkno = prevbno;
3448 else if (gotbno != NULLFSBLOCK)
3455 xfs_bmap_longest_free_extent(
3456 struct xfs_trans *tp,
3461 struct xfs_mount *mp = tp->t_mountp;
3462 struct xfs_perag *pag;
3463 xfs_extlen_t longest;
3466 pag = xfs_perag_get(mp, ag);
3467 if (!pag->pagf_init) {
3468 error = xfs_alloc_pagf_init(mp, tp, ag, XFS_ALLOC_FLAG_TRYLOCK);
3472 if (!pag->pagf_init) {
3478 longest = xfs_alloc_longest_free_extent(mp, pag,
3479 xfs_alloc_min_freelist(mp, pag),
3480 xfs_ag_resv_needed(pag, XFS_AG_RESV_NONE));
3481 if (*blen < longest)
3490 xfs_bmap_select_minlen(
3491 struct xfs_bmalloca *ap,
3492 struct xfs_alloc_arg *args,
3496 if (notinit || *blen < ap->minlen) {
3498 * Since we did a BUF_TRYLOCK above, it is possible that
3499 * there is space for this request.
3501 args->minlen = ap->minlen;
3502 } else if (*blen < args->maxlen) {
3504 * If the best seen length is less than the request length,
3505 * use the best as the minimum.
3507 args->minlen = *blen;
3510 * Otherwise we've seen an extent as big as maxlen, use that
3513 args->minlen = args->maxlen;
3518 xfs_bmap_btalloc_nullfb(
3519 struct xfs_bmalloca *ap,
3520 struct xfs_alloc_arg *args,
3523 struct xfs_mount *mp = ap->ip->i_mount;
3524 xfs_agnumber_t ag, startag;
3528 args->type = XFS_ALLOCTYPE_START_BNO;
3529 args->total = ap->total;
3531 startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3532 if (startag == NULLAGNUMBER)
3535 while (*blen < args->maxlen) {
3536 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3541 if (++ag == mp->m_sb.sb_agcount)
3547 xfs_bmap_select_minlen(ap, args, blen, notinit);
3552 xfs_bmap_btalloc_filestreams(
3553 struct xfs_bmalloca *ap,
3554 struct xfs_alloc_arg *args,
3557 struct xfs_mount *mp = ap->ip->i_mount;
3562 args->type = XFS_ALLOCTYPE_NEAR_BNO;
3563 args->total = ap->total;
3565 ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3566 if (ag == NULLAGNUMBER)
3569 error = xfs_bmap_longest_free_extent(args->tp, ag, blen, ¬init);
3573 if (*blen < args->maxlen) {
3574 error = xfs_filestream_new_ag(ap, &ag);
3578 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3585 xfs_bmap_select_minlen(ap, args, blen, notinit);
3588 * Set the failure fallback case to look in the selected AG as stream
3591 ap->blkno = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0);
3597 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
3599 xfs_mount_t *mp; /* mount point structure */
3600 xfs_alloctype_t atype = 0; /* type for allocation routines */
3601 xfs_extlen_t align = 0; /* minimum allocation alignment */
3602 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
3604 xfs_alloc_arg_t args;
3606 xfs_extlen_t nextminlen = 0;
3607 int nullfb; /* true if ap->firstblock isn't set */
3615 mp = ap->ip->i_mount;
3617 /* stripe alignment for allocation is determined by mount parameters */
3619 if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC))
3620 stripe_align = mp->m_swidth;
3621 else if (mp->m_dalign)
3622 stripe_align = mp->m_dalign;
3624 if (ap->flags & XFS_BMAPI_COWFORK)
3625 align = xfs_get_cowextsz_hint(ap->ip);
3626 else if (xfs_alloc_is_userdata(ap->datatype))
3627 align = xfs_get_extsz_hint(ap->ip);
3628 if (unlikely(align)) {
3629 error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev,
3630 align, 0, ap->eof, 0, ap->conv,
3631 &ap->offset, &ap->length);
3637 nullfb = *ap->firstblock == NULLFSBLOCK;
3638 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
3640 if (xfs_alloc_is_userdata(ap->datatype) &&
3641 xfs_inode_is_filestream(ap->ip)) {
3642 ag = xfs_filestream_lookup_ag(ap->ip);
3643 ag = (ag != NULLAGNUMBER) ? ag : 0;
3644 ap->blkno = XFS_AGB_TO_FSB(mp, ag, 0);
3646 ap->blkno = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
3649 ap->blkno = *ap->firstblock;
3651 xfs_bmap_adjacent(ap);
3654 * If allowed, use ap->blkno; otherwise must use firstblock since
3655 * it's in the right allocation group.
3657 if (nullfb || XFS_FSB_TO_AGNO(mp, ap->blkno) == fb_agno)
3660 ap->blkno = *ap->firstblock;
3662 * Normal allocation, done through xfs_alloc_vextent.
3664 tryagain = isaligned = 0;
3665 memset(&args, 0, sizeof(args));
3668 args.fsbno = ap->blkno;
3669 xfs_rmap_skip_owner_update(&args.oinfo);
3671 /* Trim the allocation back to the maximum an AG can fit. */
3672 args.maxlen = MIN(ap->length, mp->m_ag_max_usable);
3673 args.firstblock = *ap->firstblock;
3677 * Search for an allocation group with a single extent large
3678 * enough for the request. If one isn't found, then adjust
3679 * the minimum allocation size to the largest space found.
3681 if (xfs_alloc_is_userdata(ap->datatype) &&
3682 xfs_inode_is_filestream(ap->ip))
3683 error = xfs_bmap_btalloc_filestreams(ap, &args, &blen);
3685 error = xfs_bmap_btalloc_nullfb(ap, &args, &blen);
3688 } else if (ap->dfops->dop_low) {
3689 if (xfs_inode_is_filestream(ap->ip))
3690 args.type = XFS_ALLOCTYPE_FIRST_AG;
3692 args.type = XFS_ALLOCTYPE_START_BNO;
3693 args.total = args.minlen = ap->minlen;
3695 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3696 args.total = ap->total;
3697 args.minlen = ap->minlen;
3699 /* apply extent size hints if obtained earlier */
3700 if (unlikely(align)) {
3702 if ((args.mod = (xfs_extlen_t)do_mod(ap->offset, args.prod)))
3703 args.mod = (xfs_extlen_t)(args.prod - args.mod);
3704 } else if (mp->m_sb.sb_blocksize >= PAGE_SIZE) {
3708 args.prod = PAGE_SIZE >> mp->m_sb.sb_blocklog;
3709 if ((args.mod = (xfs_extlen_t)(do_mod(ap->offset, args.prod))))
3710 args.mod = (xfs_extlen_t)(args.prod - args.mod);
3713 * If we are not low on available data blocks, and the
3714 * underlying logical volume manager is a stripe, and
3715 * the file offset is zero then try to allocate data
3716 * blocks on stripe unit boundary.
3717 * NOTE: ap->aeof is only set if the allocation length
3718 * is >= the stripe unit and the allocation offset is
3719 * at the end of file.
3721 if (!ap->dfops->dop_low && ap->aeof) {
3723 args.alignment = stripe_align;
3727 * Adjust for alignment
3729 if (blen > args.alignment && blen <= args.maxlen)
3730 args.minlen = blen - args.alignment;
3731 args.minalignslop = 0;
3734 * First try an exact bno allocation.
3735 * If it fails then do a near or start bno
3736 * allocation with alignment turned on.
3740 args.type = XFS_ALLOCTYPE_THIS_BNO;
3743 * Compute the minlen+alignment for the
3744 * next case. Set slop so that the value
3745 * of minlen+alignment+slop doesn't go up
3746 * between the calls.
3748 if (blen > stripe_align && blen <= args.maxlen)
3749 nextminlen = blen - stripe_align;
3751 nextminlen = args.minlen;
3752 if (nextminlen + stripe_align > args.minlen + 1)
3754 nextminlen + stripe_align -
3757 args.minalignslop = 0;
3761 args.minalignslop = 0;
3763 args.minleft = ap->minleft;
3764 args.wasdel = ap->wasdel;
3765 args.resv = XFS_AG_RESV_NONE;
3766 args.datatype = ap->datatype;
3767 if (ap->datatype & XFS_ALLOC_USERDATA_ZERO)
3770 error = xfs_alloc_vextent(&args);
3774 if (tryagain && args.fsbno == NULLFSBLOCK) {
3776 * Exact allocation failed. Now try with alignment
3780 args.fsbno = ap->blkno;
3781 args.alignment = stripe_align;
3782 args.minlen = nextminlen;
3783 args.minalignslop = 0;
3785 if ((error = xfs_alloc_vextent(&args)))
3788 if (isaligned && args.fsbno == NULLFSBLOCK) {
3790 * allocation failed, so turn off alignment and
3794 args.fsbno = ap->blkno;
3796 if ((error = xfs_alloc_vextent(&args)))
3799 if (args.fsbno == NULLFSBLOCK && nullfb &&
3800 args.minlen > ap->minlen) {
3801 args.minlen = ap->minlen;
3802 args.type = XFS_ALLOCTYPE_START_BNO;
3803 args.fsbno = ap->blkno;
3804 if ((error = xfs_alloc_vextent(&args)))
3807 if (args.fsbno == NULLFSBLOCK && nullfb) {
3809 args.type = XFS_ALLOCTYPE_FIRST_AG;
3810 args.total = ap->minlen;
3812 if ((error = xfs_alloc_vextent(&args)))
3814 ap->dfops->dop_low = true;
3816 if (args.fsbno != NULLFSBLOCK) {
3818 * check the allocation happened at the same or higher AG than
3819 * the first block that was allocated.
3821 ASSERT(*ap->firstblock == NULLFSBLOCK ||
3822 XFS_FSB_TO_AGNO(mp, *ap->firstblock) ==
3823 XFS_FSB_TO_AGNO(mp, args.fsbno) ||
3824 (ap->dfops->dop_low &&
3825 XFS_FSB_TO_AGNO(mp, *ap->firstblock) <
3826 XFS_FSB_TO_AGNO(mp, args.fsbno)));
3828 ap->blkno = args.fsbno;
3829 if (*ap->firstblock == NULLFSBLOCK)
3830 *ap->firstblock = args.fsbno;
3831 ASSERT(nullfb || fb_agno == args.agno ||
3832 (ap->dfops->dop_low && fb_agno < args.agno));
3833 ap->length = args.len;
3834 if (!(ap->flags & XFS_BMAPI_COWFORK))
3835 ap->ip->i_d.di_nblocks += args.len;
3836 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
3838 ap->ip->i_delayed_blks -= args.len;
3840 * Adjust the disk quota also. This was reserved
3843 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
3844 ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT :
3845 XFS_TRANS_DQ_BCOUNT,
3848 ap->blkno = NULLFSBLOCK;
3855 * For a remap operation, just "allocate" an extent at the address that the
3856 * caller passed in, and ensure that the AGFL is the right size. The caller
3857 * will then map the "allocated" extent into the file somewhere.
3860 xfs_bmap_remap_alloc(
3861 struct xfs_bmalloca *ap)
3863 struct xfs_trans *tp = ap->tp;
3864 struct xfs_mount *mp = tp->t_mountp;
3866 struct xfs_alloc_arg args;
3870 * validate that the block number is legal - the enables us to detect
3871 * and handle a silent filesystem corruption rather than crashing.
3873 memset(&args, 0, sizeof(struct xfs_alloc_arg));
3875 args.mp = ap->tp->t_mountp;
3876 bno = *ap->firstblock;
3877 args.agno = XFS_FSB_TO_AGNO(mp, bno);
3878 args.agbno = XFS_FSB_TO_AGBNO(mp, bno);
3879 if (args.agno >= mp->m_sb.sb_agcount ||
3880 args.agbno >= mp->m_sb.sb_agblocks)
3881 return -EFSCORRUPTED;
3883 /* "Allocate" the extent from the range we passed in. */
3884 trace_xfs_bmap_remap_alloc(ap->ip, *ap->firstblock, ap->length);
3886 ap->ip->i_d.di_nblocks += ap->length;
3887 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
3889 /* Fix the freelist, like a real allocator does. */
3890 args.datatype = ap->datatype;
3891 args.pag = xfs_perag_get(args.mp, args.agno);
3895 * The freelist fixing code will decline the allocation if
3896 * the size and shape of the free space doesn't allow for
3897 * allocating the extent and updating all the metadata that
3898 * happens during an allocation. We're remapping, not
3899 * allocating, so skip that check by pretending to be freeing.
3901 error = xfs_alloc_fix_freelist(&args, XFS_ALLOC_FLAG_FREEING);
3902 xfs_perag_put(args.pag);
3904 trace_xfs_bmap_remap_alloc_error(ap->ip, error, _RET_IP_);
3909 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
3910 * It figures out where to ask the underlying allocator to put the new extent.
3914 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
3916 if (ap->flags & XFS_BMAPI_REMAP)
3917 return xfs_bmap_remap_alloc(ap);
3918 if (XFS_IS_REALTIME_INODE(ap->ip) &&
3919 xfs_alloc_is_userdata(ap->datatype))
3920 return xfs_bmap_rtalloc(ap);
3921 return xfs_bmap_btalloc(ap);
3924 /* Trim extent to fit a logical block range. */
3927 struct xfs_bmbt_irec *irec,
3931 xfs_fileoff_t distance;
3932 xfs_fileoff_t end = bno + len;
3934 if (irec->br_startoff + irec->br_blockcount <= bno ||
3935 irec->br_startoff >= end) {
3936 irec->br_blockcount = 0;
3940 if (irec->br_startoff < bno) {
3941 distance = bno - irec->br_startoff;
3942 if (isnullstartblock(irec->br_startblock))
3943 irec->br_startblock = DELAYSTARTBLOCK;
3944 if (irec->br_startblock != DELAYSTARTBLOCK &&
3945 irec->br_startblock != HOLESTARTBLOCK)
3946 irec->br_startblock += distance;
3947 irec->br_startoff += distance;
3948 irec->br_blockcount -= distance;
3951 if (end < irec->br_startoff + irec->br_blockcount) {
3952 distance = irec->br_startoff + irec->br_blockcount - end;
3953 irec->br_blockcount -= distance;
3958 * Trim the returned map to the required bounds
3962 struct xfs_bmbt_irec *mval,
3963 struct xfs_bmbt_irec *got,
3971 if ((flags & XFS_BMAPI_ENTIRE) ||
3972 got->br_startoff + got->br_blockcount <= obno) {
3974 if (isnullstartblock(got->br_startblock))
3975 mval->br_startblock = DELAYSTARTBLOCK;
3981 ASSERT((*bno >= obno) || (n == 0));
3983 mval->br_startoff = *bno;
3984 if (isnullstartblock(got->br_startblock))
3985 mval->br_startblock = DELAYSTARTBLOCK;
3987 mval->br_startblock = got->br_startblock +
3988 (*bno - got->br_startoff);
3990 * Return the minimum of what we got and what we asked for for
3991 * the length. We can use the len variable here because it is
3992 * modified below and we could have been there before coming
3993 * here if the first part of the allocation didn't overlap what
3996 mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno,
3997 got->br_blockcount - (*bno - got->br_startoff));
3998 mval->br_state = got->br_state;
3999 ASSERT(mval->br_blockcount <= len);
4004 * Update and validate the extent map to return
4007 xfs_bmapi_update_map(
4008 struct xfs_bmbt_irec **map,
4016 xfs_bmbt_irec_t *mval = *map;
4018 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
4019 ((mval->br_startoff + mval->br_blockcount) <= end));
4020 ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) ||
4021 (mval->br_startoff < obno));
4023 *bno = mval->br_startoff + mval->br_blockcount;
4025 if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) {
4026 /* update previous map with new information */
4027 ASSERT(mval->br_startblock == mval[-1].br_startblock);
4028 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
4029 ASSERT(mval->br_state == mval[-1].br_state);
4030 mval[-1].br_blockcount = mval->br_blockcount;
4031 mval[-1].br_state = mval->br_state;
4032 } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
4033 mval[-1].br_startblock != DELAYSTARTBLOCK &&
4034 mval[-1].br_startblock != HOLESTARTBLOCK &&
4035 mval->br_startblock == mval[-1].br_startblock +
4036 mval[-1].br_blockcount &&
4037 ((flags & XFS_BMAPI_IGSTATE) ||
4038 mval[-1].br_state == mval->br_state)) {
4039 ASSERT(mval->br_startoff ==
4040 mval[-1].br_startoff + mval[-1].br_blockcount);
4041 mval[-1].br_blockcount += mval->br_blockcount;
4042 } else if (*n > 0 &&
4043 mval->br_startblock == DELAYSTARTBLOCK &&
4044 mval[-1].br_startblock == DELAYSTARTBLOCK &&
4045 mval->br_startoff ==
4046 mval[-1].br_startoff + mval[-1].br_blockcount) {
4047 mval[-1].br_blockcount += mval->br_blockcount;
4048 mval[-1].br_state = mval->br_state;
4049 } else if (!((*n == 0) &&
4050 ((mval->br_startoff + mval->br_blockcount) <=
4059 * Map file blocks to filesystem blocks without allocation.
4063 struct xfs_inode *ip,
4066 struct xfs_bmbt_irec *mval,
4070 struct xfs_mount *mp = ip->i_mount;
4071 struct xfs_ifork *ifp;
4072 struct xfs_bmbt_irec got;
4079 int whichfork = xfs_bmapi_whichfork(flags);
4082 ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK|XFS_BMAPI_ENTIRE|
4083 XFS_BMAPI_IGSTATE|XFS_BMAPI_COWFORK)));
4084 ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED|XFS_ILOCK_EXCL));
4086 if (unlikely(XFS_TEST_ERROR(
4087 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4088 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
4089 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
4090 XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW, mp);
4091 return -EFSCORRUPTED;
4094 if (XFS_FORCED_SHUTDOWN(mp))
4097 XFS_STATS_INC(mp, xs_blk_mapr);
4099 ifp = XFS_IFORK_PTR(ip, whichfork);
4101 /* No CoW fork? Return a hole. */
4102 if (whichfork == XFS_COW_FORK && !ifp) {
4103 mval->br_startoff = bno;
4104 mval->br_startblock = HOLESTARTBLOCK;
4105 mval->br_blockcount = len;
4106 mval->br_state = XFS_EXT_NORM;
4111 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4112 error = xfs_iread_extents(NULL, ip, whichfork);
4117 if (!xfs_iext_lookup_extent(ip, ifp, bno, &idx, &got))
4122 while (bno < end && n < *nmap) {
4123 /* Reading past eof, act as though there's a hole up to end. */
4125 got.br_startoff = end;
4126 if (got.br_startoff > bno) {
4127 /* Reading in a hole. */
4128 mval->br_startoff = bno;
4129 mval->br_startblock = HOLESTARTBLOCK;
4130 mval->br_blockcount =
4131 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
4132 mval->br_state = XFS_EXT_NORM;
4133 bno += mval->br_blockcount;
4134 len -= mval->br_blockcount;
4140 /* set up the extent map to return. */
4141 xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
4142 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4144 /* If we're done, stop now. */
4145 if (bno >= end || n >= *nmap)
4148 /* Else go on to the next record. */
4149 if (!xfs_iext_get_extent(ifp, ++idx, &got))
4157 xfs_bmapi_reserve_delalloc(
4158 struct xfs_inode *ip,
4162 xfs_filblks_t prealloc,
4163 struct xfs_bmbt_irec *got,
4164 xfs_extnum_t *lastx,
4167 struct xfs_mount *mp = ip->i_mount;
4168 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
4170 xfs_extlen_t indlen;
4171 char rt = XFS_IS_REALTIME_INODE(ip);
4174 xfs_fileoff_t aoff = off;
4177 * Cap the alloc length. Keep track of prealloc so we know whether to
4178 * tag the inode before we return.
4180 alen = XFS_FILBLKS_MIN(len + prealloc, MAXEXTLEN);
4182 alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff);
4183 if (prealloc && alen >= len)
4184 prealloc = alen - len;
4186 /* Figure out the extent size, adjust alen */
4187 if (whichfork == XFS_COW_FORK)
4188 extsz = xfs_get_cowextsz_hint(ip);
4190 extsz = xfs_get_extsz_hint(ip);
4192 struct xfs_bmbt_irec prev;
4194 if (!xfs_iext_get_extent(ifp, *lastx - 1, &prev))
4195 prev.br_startoff = NULLFILEOFF;
4197 error = xfs_bmap_extsize_align(mp, got, &prev, extsz, rt, eof,
4198 1, 0, &aoff, &alen);
4203 extsz = alen / mp->m_sb.sb_rextsize;
4206 * Make a transaction-less quota reservation for delayed allocation
4207 * blocks. This number gets adjusted later. We return if we haven't
4208 * allocated blocks already inside this loop.
4210 error = xfs_trans_reserve_quota_nblks(NULL, ip, (long)alen, 0,
4211 rt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
4216 * Split changing sb for alen and indlen since they could be coming
4217 * from different places.
4219 indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen);
4223 error = xfs_mod_frextents(mp, -((int64_t)extsz));
4225 error = xfs_mod_fdblocks(mp, -((int64_t)alen), false);
4229 goto out_unreserve_quota;
4231 error = xfs_mod_fdblocks(mp, -((int64_t)indlen), false);
4233 goto out_unreserve_blocks;
4236 ip->i_delayed_blks += alen;
4238 got->br_startoff = aoff;
4239 got->br_startblock = nullstartblock(indlen);
4240 got->br_blockcount = alen;
4241 got->br_state = XFS_EXT_NORM;
4242 xfs_bmap_add_extent_hole_delay(ip, whichfork, lastx, got);
4245 * Update our extent pointer, given that xfs_bmap_add_extent_hole_delay
4246 * might have merged it into one of the neighbouring ones.
4248 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *lastx), got);
4251 * Tag the inode if blocks were preallocated. Note that COW fork
4252 * preallocation can occur at the start or end of the extent, even when
4253 * prealloc == 0, so we must also check the aligned offset and length.
4255 if (whichfork == XFS_DATA_FORK && prealloc)
4256 xfs_inode_set_eofblocks_tag(ip);
4257 if (whichfork == XFS_COW_FORK && (prealloc || aoff < off || alen > len))
4258 xfs_inode_set_cowblocks_tag(ip);
4260 ASSERT(got->br_startoff <= aoff);
4261 ASSERT(got->br_startoff + got->br_blockcount >= aoff + alen);
4262 ASSERT(isnullstartblock(got->br_startblock));
4263 ASSERT(got->br_state == XFS_EXT_NORM);
4266 out_unreserve_blocks:
4268 xfs_mod_frextents(mp, extsz);
4270 xfs_mod_fdblocks(mp, alen, false);
4271 out_unreserve_quota:
4272 if (XFS_IS_QUOTA_ON(mp))
4273 xfs_trans_unreserve_quota_nblks(NULL, ip, (long)alen, 0, rt ?
4274 XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
4280 struct xfs_bmalloca *bma)
4282 struct xfs_mount *mp = bma->ip->i_mount;
4283 int whichfork = xfs_bmapi_whichfork(bma->flags);
4284 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
4285 int tmp_logflags = 0;
4288 ASSERT(bma->length > 0);
4291 * For the wasdelay case, we could also just allocate the stuff asked
4292 * for in this bmap call but that wouldn't be as good.
4295 bma->length = (xfs_extlen_t)bma->got.br_blockcount;
4296 bma->offset = bma->got.br_startoff;
4298 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1),
4302 bma->length = XFS_FILBLKS_MIN(bma->length, MAXEXTLEN);
4304 bma->length = XFS_FILBLKS_MIN(bma->length,
4305 bma->got.br_startoff - bma->offset);
4309 * Set the data type being allocated. For the data fork, the first data
4310 * in the file is treated differently to all other allocations. For the
4311 * attribute fork, we only need to ensure the allocated range is not on
4314 if (!(bma->flags & XFS_BMAPI_METADATA)) {
4315 bma->datatype = XFS_ALLOC_NOBUSY;
4316 if (whichfork == XFS_DATA_FORK) {
4317 if (bma->offset == 0)
4318 bma->datatype |= XFS_ALLOC_INITIAL_USER_DATA;
4320 bma->datatype |= XFS_ALLOC_USERDATA;
4322 if (bma->flags & XFS_BMAPI_ZERO)
4323 bma->datatype |= XFS_ALLOC_USERDATA_ZERO;
4326 bma->minlen = (bma->flags & XFS_BMAPI_CONTIG) ? bma->length : 1;
4329 * Only want to do the alignment at the eof if it is userdata and
4330 * allocation length is larger than a stripe unit.
4332 if (mp->m_dalign && bma->length >= mp->m_dalign &&
4333 !(bma->flags & XFS_BMAPI_METADATA) && whichfork == XFS_DATA_FORK) {
4334 error = xfs_bmap_isaeof(bma, whichfork);
4339 error = xfs_bmap_alloc(bma);
4343 if (bma->dfops->dop_low)
4346 bma->cur->bc_private.b.firstblock = *bma->firstblock;
4347 if (bma->blkno == NULLFSBLOCK)
4349 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4350 bma->cur = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork);
4351 bma->cur->bc_private.b.firstblock = *bma->firstblock;
4352 bma->cur->bc_private.b.dfops = bma->dfops;
4355 * Bump the number of extents we've allocated
4361 bma->cur->bc_private.b.flags =
4362 bma->wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
4364 bma->got.br_startoff = bma->offset;
4365 bma->got.br_startblock = bma->blkno;
4366 bma->got.br_blockcount = bma->length;
4367 bma->got.br_state = XFS_EXT_NORM;
4370 * A wasdelay extent has been initialized, so shouldn't be flagged
4373 if (!bma->wasdel && (bma->flags & XFS_BMAPI_PREALLOC) &&
4374 xfs_sb_version_hasextflgbit(&mp->m_sb))
4375 bma->got.br_state = XFS_EXT_UNWRITTEN;
4378 error = xfs_bmap_add_extent_delay_real(bma, whichfork);
4380 error = xfs_bmap_add_extent_hole_real(bma, whichfork);
4382 bma->logflags |= tmp_logflags;
4387 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4388 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4389 * the neighbouring ones.
4391 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &bma->got);
4393 ASSERT(bma->got.br_startoff <= bma->offset);
4394 ASSERT(bma->got.br_startoff + bma->got.br_blockcount >=
4395 bma->offset + bma->length);
4396 ASSERT(bma->got.br_state == XFS_EXT_NORM ||
4397 bma->got.br_state == XFS_EXT_UNWRITTEN);
4402 xfs_bmapi_convert_unwritten(
4403 struct xfs_bmalloca *bma,
4404 struct xfs_bmbt_irec *mval,
4408 int whichfork = xfs_bmapi_whichfork(flags);
4409 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
4410 int tmp_logflags = 0;
4413 /* check if we need to do unwritten->real conversion */
4414 if (mval->br_state == XFS_EXT_UNWRITTEN &&
4415 (flags & XFS_BMAPI_PREALLOC))
4418 /* check if we need to do real->unwritten conversion */
4419 if (mval->br_state == XFS_EXT_NORM &&
4420 (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) !=
4421 (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT))
4424 ASSERT(whichfork != XFS_COW_FORK);
4427 * Modify (by adding) the state flag, if writing.
4429 ASSERT(mval->br_blockcount <= len);
4430 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4431 bma->cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp,
4432 bma->ip, whichfork);
4433 bma->cur->bc_private.b.firstblock = *bma->firstblock;
4434 bma->cur->bc_private.b.dfops = bma->dfops;
4436 mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN)
4437 ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
4440 * Before insertion into the bmbt, zero the range being converted
4443 if (flags & XFS_BMAPI_ZERO) {
4444 error = xfs_zero_extent(bma->ip, mval->br_startblock,
4445 mval->br_blockcount);
4450 error = xfs_bmap_add_extent_unwritten_real(bma->tp, bma->ip, &bma->idx,
4451 &bma->cur, mval, bma->firstblock, bma->dfops,
4454 * Log the inode core unconditionally in the unwritten extent conversion
4455 * path because the conversion might not have done so (e.g., if the
4456 * extent count hasn't changed). We need to make sure the inode is dirty
4457 * in the transaction for the sake of fsync(), even if nothing has
4458 * changed, because fsync() will not force the log for this transaction
4459 * unless it sees the inode pinned.
4461 bma->logflags |= tmp_logflags | XFS_ILOG_CORE;
4466 * Update our extent pointer, given that
4467 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4468 * of the neighbouring ones.
4470 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &bma->got);
4473 * We may have combined previously unwritten space with written space,
4474 * so generate another request.
4476 if (mval->br_blockcount < len)
4482 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4483 * extent state if necessary. Details behaviour is controlled by the flags
4484 * parameter. Only allocates blocks from a single allocation group, to avoid
4487 * The returned value in "firstblock" from the first call in a transaction
4488 * must be remembered and presented to subsequent calls in "firstblock".
4489 * An upper bound for the number of blocks to be allocated is supplied to
4490 * the first call in "total"; if no allocation group has that many free
4491 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4495 struct xfs_trans *tp, /* transaction pointer */
4496 struct xfs_inode *ip, /* incore inode */
4497 xfs_fileoff_t bno, /* starting file offs. mapped */
4498 xfs_filblks_t len, /* length to map in file */
4499 int flags, /* XFS_BMAPI_... */
4500 xfs_fsblock_t *firstblock, /* first allocated block
4501 controls a.g. for allocs */
4502 xfs_extlen_t total, /* total blocks needed */
4503 struct xfs_bmbt_irec *mval, /* output: map values */
4504 int *nmap, /* i/o: mval size/count */
4505 struct xfs_defer_ops *dfops) /* i/o: list extents to free */
4507 struct xfs_mount *mp = ip->i_mount;
4508 struct xfs_ifork *ifp;
4509 struct xfs_bmalloca bma = { NULL }; /* args for xfs_bmap_alloc */
4510 xfs_fileoff_t end; /* end of mapped file region */
4511 bool eof = false; /* after the end of extents */
4512 int error; /* error return */
4513 int n; /* current extent index */
4514 xfs_fileoff_t obno; /* old block number (offset) */
4515 int whichfork; /* data or attr fork */
4516 char inhole; /* current location is hole in file */
4517 char wasdelay; /* old extent was delayed */
4520 xfs_fileoff_t orig_bno; /* original block number value */
4521 int orig_flags; /* original flags arg value */
4522 xfs_filblks_t orig_len; /* original value of len arg */
4523 struct xfs_bmbt_irec *orig_mval; /* original value of mval */
4524 int orig_nmap; /* original value of *nmap */
4532 whichfork = xfs_bmapi_whichfork(flags);
4535 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP);
4536 ASSERT(!(flags & XFS_BMAPI_IGSTATE));
4539 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL);
4540 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
4541 ASSERT(!(flags & XFS_BMAPI_REMAP) || whichfork == XFS_DATA_FORK);
4542 ASSERT(!(flags & XFS_BMAPI_PREALLOC) || !(flags & XFS_BMAPI_REMAP));
4543 ASSERT(!(flags & XFS_BMAPI_CONVERT) || !(flags & XFS_BMAPI_REMAP));
4544 ASSERT(!(flags & XFS_BMAPI_PREALLOC) || whichfork != XFS_COW_FORK);
4545 ASSERT(!(flags & XFS_BMAPI_CONVERT) || whichfork != XFS_COW_FORK);
4547 /* zeroing is for currently only for data extents, not metadata */
4548 ASSERT((flags & (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO)) !=
4549 (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO));
4551 * we can allocate unwritten extents or pre-zero allocated blocks,
4552 * but it makes no sense to do both at once. This would result in
4553 * zeroing the unwritten extent twice, but it still being an
4554 * unwritten extent....
4556 ASSERT((flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)) !=
4557 (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO));
4559 if (unlikely(XFS_TEST_ERROR(
4560 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4561 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
4562 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
4563 XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW, mp);
4564 return -EFSCORRUPTED;
4567 if (XFS_FORCED_SHUTDOWN(mp))
4570 ifp = XFS_IFORK_PTR(ip, whichfork);
4572 XFS_STATS_INC(mp, xs_blk_mapw);
4574 if (*firstblock == NULLFSBLOCK) {
4575 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE)
4576 bma.minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1;
4583 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4584 error = xfs_iread_extents(tp, ip, whichfork);
4593 if (!xfs_iext_lookup_extent(ip, ifp, bno, &bma.idx, &bma.got))
4595 if (!xfs_iext_get_extent(ifp, bma.idx - 1, &bma.prev))
4596 bma.prev.br_startoff = NULLFILEOFF;
4602 bma.firstblock = firstblock;
4604 while (bno < end && n < *nmap) {
4605 inhole = eof || bma.got.br_startoff > bno;
4606 wasdelay = !inhole && isnullstartblock(bma.got.br_startblock);
4609 * Make sure we only reflink into a hole.
4611 if (flags & XFS_BMAPI_REMAP)
4613 if (flags & XFS_BMAPI_COWFORK)
4617 * First, deal with the hole before the allocated space
4618 * that we found, if any.
4620 if (inhole || wasdelay) {
4622 bma.conv = !!(flags & XFS_BMAPI_CONVERT);
4623 bma.wasdel = wasdelay;
4628 * There's a 32/64 bit type mismatch between the
4629 * allocation length request (which can be 64 bits in
4630 * length) and the bma length request, which is
4631 * xfs_extlen_t and therefore 32 bits. Hence we have to
4632 * check for 32-bit overflows and handle them here.
4634 if (len > (xfs_filblks_t)MAXEXTLEN)
4635 bma.length = MAXEXTLEN;
4640 ASSERT(bma.length > 0);
4641 error = xfs_bmapi_allocate(&bma);
4644 if (bma.blkno == NULLFSBLOCK)
4648 * If this is a CoW allocation, record the data in
4649 * the refcount btree for orphan recovery.
4651 if (whichfork == XFS_COW_FORK) {
4652 error = xfs_refcount_alloc_cow_extent(mp, dfops,
4653 bma.blkno, bma.length);
4659 /* Deal with the allocated space we found. */
4660 xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno,
4663 /* Execute unwritten extent conversion if necessary */
4664 error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags);
4665 if (error == -EAGAIN)
4670 /* update the extent map to return */
4671 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4674 * If we're done, stop now. Stop when we've allocated
4675 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4676 * the transaction may get too big.
4678 if (bno >= end || n >= *nmap || bma.nallocs >= *nmap)
4681 /* Else go on to the next record. */
4683 if (!xfs_iext_get_extent(ifp, ++bma.idx, &bma.got))
4689 * Transform from btree to extents, give it cur.
4691 if (xfs_bmap_wants_extents(ip, whichfork)) {
4692 int tmp_logflags = 0;
4695 error = xfs_bmap_btree_to_extents(tp, ip, bma.cur,
4696 &tmp_logflags, whichfork);
4697 bma.logflags |= tmp_logflags;
4702 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE ||
4703 XFS_IFORK_NEXTENTS(ip, whichfork) >
4704 XFS_IFORK_MAXEXT(ip, whichfork));
4708 * Log everything. Do this after conversion, there's no point in
4709 * logging the extent records if we've converted to btree format.
4711 if ((bma.logflags & xfs_ilog_fext(whichfork)) &&
4712 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
4713 bma.logflags &= ~xfs_ilog_fext(whichfork);
4714 else if ((bma.logflags & xfs_ilog_fbroot(whichfork)) &&
4715 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
4716 bma.logflags &= ~xfs_ilog_fbroot(whichfork);
4718 * Log whatever the flags say, even if error. Otherwise we might miss
4719 * detecting a case where the data is changed, there's an error,
4720 * and it's not logged so we don't shutdown when we should.
4723 xfs_trans_log_inode(tp, ip, bma.logflags);
4727 ASSERT(*firstblock == NULLFSBLOCK ||
4728 XFS_FSB_TO_AGNO(mp, *firstblock) ==
4730 bma.cur->bc_private.b.firstblock) ||
4732 XFS_FSB_TO_AGNO(mp, *firstblock) <
4734 bma.cur->bc_private.b.firstblock)));
4735 *firstblock = bma.cur->bc_private.b.firstblock;
4737 xfs_btree_del_cursor(bma.cur,
4738 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
4741 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
4747 * When a delalloc extent is split (e.g., due to a hole punch), the original
4748 * indlen reservation must be shared across the two new extents that are left
4751 * Given the original reservation and the worst case indlen for the two new
4752 * extents (as calculated by xfs_bmap_worst_indlen()), split the original
4753 * reservation fairly across the two new extents. If necessary, steal available
4754 * blocks from a deleted extent to make up a reservation deficiency (e.g., if
4755 * ores == 1). The number of stolen blocks is returned. The availability and
4756 * subsequent accounting of stolen blocks is the responsibility of the caller.
4758 static xfs_filblks_t
4759 xfs_bmap_split_indlen(
4760 xfs_filblks_t ores, /* original res. */
4761 xfs_filblks_t *indlen1, /* ext1 worst indlen */
4762 xfs_filblks_t *indlen2, /* ext2 worst indlen */
4763 xfs_filblks_t avail) /* stealable blocks */
4765 xfs_filblks_t len1 = *indlen1;
4766 xfs_filblks_t len2 = *indlen2;
4767 xfs_filblks_t nres = len1 + len2; /* new total res. */
4768 xfs_filblks_t stolen = 0;
4771 * Steal as many blocks as we can to try and satisfy the worst case
4772 * indlen for both new extents.
4774 while (nres > ores && avail) {
4781 * The only blocks available are those reserved for the original
4782 * extent and what we can steal from the extent being removed.
4783 * If this still isn't enough to satisfy the combined
4784 * requirements for the two new extents, skim blocks off of each
4785 * of the new reservations until they match what is available.
4787 while (nres > ores) {
4807 xfs_bmap_del_extent_delay(
4808 struct xfs_inode *ip,
4811 struct xfs_bmbt_irec *got,
4812 struct xfs_bmbt_irec *del)
4814 struct xfs_mount *mp = ip->i_mount;
4815 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
4816 struct xfs_bmbt_irec new;
4817 int64_t da_old, da_new, da_diff = 0;
4818 xfs_fileoff_t del_endoff, got_endoff;
4819 xfs_filblks_t got_indlen, new_indlen, stolen;
4820 int error = 0, state = 0;
4823 XFS_STATS_INC(mp, xs_del_exlist);
4825 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
4826 del_endoff = del->br_startoff + del->br_blockcount;
4827 got_endoff = got->br_startoff + got->br_blockcount;
4828 da_old = startblockval(got->br_startblock);
4832 ASSERT(*idx <= xfs_iext_count(ifp));
4833 ASSERT(del->br_blockcount > 0);
4834 ASSERT(got->br_startoff <= del->br_startoff);
4835 ASSERT(got_endoff >= del_endoff);
4838 int64_t rtexts = XFS_FSB_TO_B(mp, del->br_blockcount);
4840 do_div(rtexts, mp->m_sb.sb_rextsize);
4841 xfs_mod_frextents(mp, rtexts);
4845 * Update the inode delalloc counter now and wait to update the
4846 * sb counters as we might have to borrow some blocks for the
4847 * indirect block accounting.
4849 xfs_trans_reserve_quota_nblks(NULL, ip, -((long)del->br_blockcount), 0,
4850 isrt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
4851 ip->i_delayed_blks -= del->br_blockcount;
4853 if (whichfork == XFS_COW_FORK)
4854 state |= BMAP_COWFORK;
4856 if (got->br_startoff == del->br_startoff)
4857 state |= BMAP_LEFT_CONTIG;
4858 if (got_endoff == del_endoff)
4859 state |= BMAP_RIGHT_CONTIG;
4861 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
4862 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
4864 * Matches the whole extent. Delete the entry.
4866 xfs_iext_remove(ip, *idx, 1, state);
4869 case BMAP_LEFT_CONTIG:
4871 * Deleting the first part of the extent.
4873 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4874 got->br_startoff = del_endoff;
4875 got->br_blockcount -= del->br_blockcount;
4876 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
4877 got->br_blockcount), da_old);
4878 got->br_startblock = nullstartblock((int)da_new);
4879 xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got);
4880 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
4882 case BMAP_RIGHT_CONTIG:
4884 * Deleting the last part of the extent.
4886 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4887 got->br_blockcount = got->br_blockcount - del->br_blockcount;
4888 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
4889 got->br_blockcount), da_old);
4890 got->br_startblock = nullstartblock((int)da_new);
4891 xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got);
4892 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
4896 * Deleting the middle of the extent.
4898 * Distribute the original indlen reservation across the two new
4899 * extents. Steal blocks from the deleted extent if necessary.
4900 * Stealing blocks simply fudges the fdblocks accounting below.
4901 * Warn if either of the new indlen reservations is zero as this
4902 * can lead to delalloc problems.
4904 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4906 got->br_blockcount = del->br_startoff - got->br_startoff;
4907 got_indlen = xfs_bmap_worst_indlen(ip, got->br_blockcount);
4909 new.br_blockcount = got_endoff - del_endoff;
4910 new_indlen = xfs_bmap_worst_indlen(ip, new.br_blockcount);
4912 WARN_ON_ONCE(!got_indlen || !new_indlen);
4913 stolen = xfs_bmap_split_indlen(da_old, &got_indlen, &new_indlen,
4914 del->br_blockcount);
4916 got->br_startblock = nullstartblock((int)got_indlen);
4917 xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got);
4918 trace_xfs_bmap_post_update(ip, *idx, 0, _THIS_IP_);
4920 new.br_startoff = del_endoff;
4921 new.br_state = got->br_state;
4922 new.br_startblock = nullstartblock((int)new_indlen);
4925 xfs_iext_insert(ip, *idx, 1, &new, state);
4927 da_new = got_indlen + new_indlen - stolen;
4928 del->br_blockcount -= stolen;
4932 ASSERT(da_old >= da_new);
4933 da_diff = da_old - da_new;
4935 da_diff += del->br_blockcount;
4937 xfs_mod_fdblocks(mp, da_diff, false);
4942 xfs_bmap_del_extent_cow(
4943 struct xfs_inode *ip,
4945 struct xfs_bmbt_irec *got,
4946 struct xfs_bmbt_irec *del)
4948 struct xfs_mount *mp = ip->i_mount;
4949 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
4950 struct xfs_bmbt_irec new;
4951 xfs_fileoff_t del_endoff, got_endoff;
4952 int state = BMAP_COWFORK;
4954 XFS_STATS_INC(mp, xs_del_exlist);
4956 del_endoff = del->br_startoff + del->br_blockcount;
4957 got_endoff = got->br_startoff + got->br_blockcount;
4960 ASSERT(*idx <= xfs_iext_count(ifp));
4961 ASSERT(del->br_blockcount > 0);
4962 ASSERT(got->br_startoff <= del->br_startoff);
4963 ASSERT(got_endoff >= del_endoff);
4964 ASSERT(!isnullstartblock(got->br_startblock));
4966 if (got->br_startoff == del->br_startoff)
4967 state |= BMAP_LEFT_CONTIG;
4968 if (got_endoff == del_endoff)
4969 state |= BMAP_RIGHT_CONTIG;
4971 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
4972 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
4974 * Matches the whole extent. Delete the entry.
4976 xfs_iext_remove(ip, *idx, 1, state);
4979 case BMAP_LEFT_CONTIG:
4981 * Deleting the first part of the extent.
4983 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4984 got->br_startoff = del_endoff;
4985 got->br_blockcount -= del->br_blockcount;
4986 got->br_startblock = del->br_startblock + del->br_blockcount;
4987 xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got);
4988 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
4990 case BMAP_RIGHT_CONTIG:
4992 * Deleting the last part of the extent.
4994 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4995 got->br_blockcount -= del->br_blockcount;
4996 xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got);
4997 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5001 * Deleting the middle of the extent.
5003 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5004 got->br_blockcount = del->br_startoff - got->br_startoff;
5005 xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got);
5006 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5008 new.br_startoff = del_endoff;
5009 new.br_blockcount = got_endoff - del_endoff;
5010 new.br_state = got->br_state;
5011 new.br_startblock = del->br_startblock + del->br_blockcount;
5014 xfs_iext_insert(ip, *idx, 1, &new, state);
5020 * Called by xfs_bmapi to update file extent records and the btree
5021 * after removing space (or undoing a delayed allocation).
5023 STATIC int /* error */
5024 xfs_bmap_del_extent(
5025 xfs_inode_t *ip, /* incore inode pointer */
5026 xfs_trans_t *tp, /* current transaction pointer */
5027 xfs_extnum_t *idx, /* extent number to update/delete */
5028 struct xfs_defer_ops *dfops, /* list of extents to be freed */
5029 xfs_btree_cur_t *cur, /* if null, not a btree */
5030 xfs_bmbt_irec_t *del, /* data to remove from extents */
5031 int *logflagsp, /* inode logging flags */
5032 int whichfork, /* data or attr fork */
5033 int bflags) /* bmapi flags */
5035 xfs_filblks_t da_new; /* new delay-alloc indirect blocks */
5036 xfs_filblks_t da_old; /* old delay-alloc indirect blocks */
5037 xfs_fsblock_t del_endblock=0; /* first block past del */
5038 xfs_fileoff_t del_endoff; /* first offset past del */
5039 int delay; /* current block is delayed allocated */
5040 int do_fx; /* free extent at end of routine */
5041 xfs_bmbt_rec_host_t *ep; /* current extent entry pointer */
5042 int error; /* error return value */
5043 int flags; /* inode logging flags */
5044 xfs_bmbt_irec_t got; /* current extent entry */
5045 xfs_fileoff_t got_endoff; /* first offset past got */
5046 int i; /* temp state */
5047 xfs_ifork_t *ifp; /* inode fork pointer */
5048 xfs_mount_t *mp; /* mount structure */
5049 xfs_filblks_t nblks; /* quota/sb block count */
5050 xfs_bmbt_irec_t new; /* new record to be inserted */
5052 uint qfield; /* quota field to update */
5053 xfs_filblks_t temp; /* for indirect length calculations */
5054 xfs_filblks_t temp2; /* for indirect length calculations */
5058 XFS_STATS_INC(mp, xs_del_exlist);
5060 if (whichfork == XFS_ATTR_FORK)
5061 state |= BMAP_ATTRFORK;
5062 else if (whichfork == XFS_COW_FORK)
5063 state |= BMAP_COWFORK;
5065 ifp = XFS_IFORK_PTR(ip, whichfork);
5066 ASSERT((*idx >= 0) && (*idx < xfs_iext_count(ifp)));
5067 ASSERT(del->br_blockcount > 0);
5068 ep = xfs_iext_get_ext(ifp, *idx);
5069 xfs_bmbt_get_all(ep, &got);
5070 ASSERT(got.br_startoff <= del->br_startoff);
5071 del_endoff = del->br_startoff + del->br_blockcount;
5072 got_endoff = got.br_startoff + got.br_blockcount;
5073 ASSERT(got_endoff >= del_endoff);
5074 delay = isnullstartblock(got.br_startblock);
5075 ASSERT(isnullstartblock(del->br_startblock) == delay);
5080 * If deleting a real allocation, must free up the disk space.
5083 flags = XFS_ILOG_CORE;
5085 * Realtime allocation. Free it and record di_nblocks update.
5087 if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) {
5091 ASSERT(do_mod(del->br_blockcount,
5092 mp->m_sb.sb_rextsize) == 0);
5093 ASSERT(do_mod(del->br_startblock,
5094 mp->m_sb.sb_rextsize) == 0);
5095 bno = del->br_startblock;
5096 len = del->br_blockcount;
5097 do_div(bno, mp->m_sb.sb_rextsize);
5098 do_div(len, mp->m_sb.sb_rextsize);
5099 error = xfs_rtfree_extent(tp, bno, (xfs_extlen_t)len);
5103 nblks = len * mp->m_sb.sb_rextsize;
5104 qfield = XFS_TRANS_DQ_RTBCOUNT;
5107 * Ordinary allocation.
5111 nblks = del->br_blockcount;
5112 qfield = XFS_TRANS_DQ_BCOUNT;
5115 * Set up del_endblock and cur for later.
5117 del_endblock = del->br_startblock + del->br_blockcount;
5119 if ((error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
5120 got.br_startblock, got.br_blockcount,
5123 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
5125 da_old = da_new = 0;
5127 da_old = startblockval(got.br_startblock);
5134 * Set flag value to use in switch statement.
5135 * Left-contig is 2, right-contig is 1.
5137 switch (((got.br_startoff == del->br_startoff) << 1) |
5138 (got_endoff == del_endoff)) {
5141 * Matches the whole extent. Delete the entry.
5143 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5144 xfs_iext_remove(ip, *idx, 1,
5145 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0);
5150 XFS_IFORK_NEXT_SET(ip, whichfork,
5151 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
5152 flags |= XFS_ILOG_CORE;
5154 flags |= xfs_ilog_fext(whichfork);
5157 if ((error = xfs_btree_delete(cur, &i)))
5159 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
5164 * Deleting the first part of the extent.
5166 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5167 xfs_bmbt_set_startoff(ep, del_endoff);
5168 temp = got.br_blockcount - del->br_blockcount;
5169 xfs_bmbt_set_blockcount(ep, temp);
5171 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
5173 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
5174 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5178 xfs_bmbt_set_startblock(ep, del_endblock);
5179 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5181 flags |= xfs_ilog_fext(whichfork);
5184 if ((error = xfs_bmbt_update(cur, del_endoff, del_endblock,
5185 got.br_blockcount - del->br_blockcount,
5192 * Deleting the last part of the extent.
5194 temp = got.br_blockcount - del->br_blockcount;
5195 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5196 xfs_bmbt_set_blockcount(ep, temp);
5198 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
5200 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
5201 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5205 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5207 flags |= xfs_ilog_fext(whichfork);
5210 if ((error = xfs_bmbt_update(cur, got.br_startoff,
5212 got.br_blockcount - del->br_blockcount,
5219 * Deleting the middle of the extent.
5221 temp = del->br_startoff - got.br_startoff;
5222 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5223 xfs_bmbt_set_blockcount(ep, temp);
5224 new.br_startoff = del_endoff;
5225 temp2 = got_endoff - del_endoff;
5226 new.br_blockcount = temp2;
5227 new.br_state = got.br_state;
5229 new.br_startblock = del_endblock;
5230 flags |= XFS_ILOG_CORE;
5232 if ((error = xfs_bmbt_update(cur,
5234 got.br_startblock, temp,
5237 if ((error = xfs_btree_increment(cur, 0, &i)))
5239 cur->bc_rec.b = new;
5240 error = xfs_btree_insert(cur, &i);
5241 if (error && error != -ENOSPC)
5244 * If get no-space back from btree insert,
5245 * it tried a split, and we have a zero
5246 * block reservation.
5247 * Fix up our state and return the error.
5249 if (error == -ENOSPC) {
5251 * Reset the cursor, don't trust
5252 * it after any insert operation.
5254 if ((error = xfs_bmbt_lookup_eq(cur,
5259 XFS_WANT_CORRUPTED_GOTO(mp,
5262 * Update the btree record back
5263 * to the original value.
5265 if ((error = xfs_bmbt_update(cur,
5272 * Reset the extent record back
5273 * to the original value.
5275 xfs_bmbt_set_blockcount(ep,
5281 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
5283 flags |= xfs_ilog_fext(whichfork);
5284 XFS_IFORK_NEXT_SET(ip, whichfork,
5285 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
5287 xfs_filblks_t stolen;
5288 ASSERT(whichfork == XFS_DATA_FORK);
5291 * Distribute the original indlen reservation across the
5292 * two new extents. Steal blocks from the deleted extent
5293 * if necessary. Stealing blocks simply fudges the
5294 * fdblocks accounting in xfs_bunmapi().
5296 temp = xfs_bmap_worst_indlen(ip, got.br_blockcount);
5297 temp2 = xfs_bmap_worst_indlen(ip, new.br_blockcount);
5298 stolen = xfs_bmap_split_indlen(da_old, &temp, &temp2,
5299 del->br_blockcount);
5300 da_new = temp + temp2 - stolen;
5301 del->br_blockcount -= stolen;
5304 * Set the reservation for each extent. Warn if either
5305 * is zero as this can lead to delalloc problems.
5307 WARN_ON_ONCE(!temp || !temp2);
5308 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
5309 new.br_startblock = nullstartblock((int)temp2);
5311 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5312 xfs_iext_insert(ip, *idx + 1, 1, &new, state);
5317 /* remove reverse mapping */
5319 error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, del);
5325 * If we need to, add to list of extents to delete.
5327 if (do_fx && !(bflags & XFS_BMAPI_REMAP)) {
5328 if (xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK) {
5329 error = xfs_refcount_decrease_extent(mp, dfops, del);
5333 xfs_bmap_add_free(mp, dfops, del->br_startblock,
5334 del->br_blockcount, NULL);
5338 * Adjust inode # blocks in the file.
5341 ip->i_d.di_nblocks -= nblks;
5343 * Adjust quota data.
5345 if (qfield && !(bflags & XFS_BMAPI_REMAP))
5346 xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks);
5349 * Account for change in delayed indirect blocks.
5350 * Nothing to do for disk quota accounting here.
5352 ASSERT(da_old >= da_new);
5353 if (da_old > da_new)
5354 xfs_mod_fdblocks(mp, (int64_t)(da_old - da_new), false);
5361 * Unmap (remove) blocks from a file.
5362 * If nexts is nonzero then the number of extents to remove is limited to
5363 * that value. If not all extents in the block range can be removed then
5368 xfs_trans_t *tp, /* transaction pointer */
5369 struct xfs_inode *ip, /* incore inode */
5370 xfs_fileoff_t bno, /* starting offset to unmap */
5371 xfs_filblks_t *rlen, /* i/o: amount remaining */
5372 int flags, /* misc flags */
5373 xfs_extnum_t nexts, /* number of extents max */
5374 xfs_fsblock_t *firstblock, /* first allocated block
5375 controls a.g. for allocs */
5376 struct xfs_defer_ops *dfops) /* i/o: deferred updates */
5378 xfs_btree_cur_t *cur; /* bmap btree cursor */
5379 xfs_bmbt_irec_t del; /* extent being deleted */
5380 int error; /* error return value */
5381 xfs_extnum_t extno; /* extent number in list */
5382 xfs_bmbt_irec_t got; /* current extent record */
5383 xfs_ifork_t *ifp; /* inode fork pointer */
5384 int isrt; /* freeing in rt area */
5385 xfs_extnum_t lastx; /* last extent index used */
5386 int logflags; /* transaction logging flags */
5387 xfs_extlen_t mod; /* rt extent offset */
5388 xfs_mount_t *mp; /* mount structure */
5389 xfs_fileoff_t start; /* first file offset deleted */
5390 int tmp_logflags; /* partial logging flags */
5391 int wasdel; /* was a delayed alloc extent */
5392 int whichfork; /* data or attribute fork */
5394 xfs_filblks_t len = *rlen; /* length to unmap in file */
5396 trace_xfs_bunmap(ip, bno, len, flags, _RET_IP_);
5398 whichfork = xfs_bmapi_whichfork(flags);
5399 ASSERT(whichfork != XFS_COW_FORK);
5400 ifp = XFS_IFORK_PTR(ip, whichfork);
5402 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5403 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
5404 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW,
5406 return -EFSCORRUPTED;
5409 if (XFS_FORCED_SHUTDOWN(mp))
5412 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
5416 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5417 (error = xfs_iread_extents(tp, ip, whichfork)))
5419 if (xfs_iext_count(ifp) == 0) {
5423 XFS_STATS_INC(mp, xs_blk_unmap);
5424 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
5426 bno = start + len - 1;
5429 * Check to see if the given block number is past the end of the
5430 * file, back up to the last block if so...
5432 if (!xfs_iext_lookup_extent(ip, ifp, bno, &lastx, &got)) {
5434 xfs_iext_get_extent(ifp, --lastx, &got);
5435 bno = got.br_startoff + got.br_blockcount - 1;
5439 if (ifp->if_flags & XFS_IFBROOT) {
5440 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
5441 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5442 cur->bc_private.b.firstblock = *firstblock;
5443 cur->bc_private.b.dfops = dfops;
5444 cur->bc_private.b.flags = 0;
5450 * Synchronize by locking the bitmap inode.
5452 xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL|XFS_ILOCK_RTBITMAP);
5453 xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL);
5454 xfs_ilock(mp->m_rsumip, XFS_ILOCK_EXCL|XFS_ILOCK_RTSUM);
5455 xfs_trans_ijoin(tp, mp->m_rsumip, XFS_ILOCK_EXCL);
5459 while (bno != (xfs_fileoff_t)-1 && bno >= start && lastx >= 0 &&
5460 (nexts == 0 || extno < nexts)) {
5462 * Is the found extent after a hole in which bno lives?
5463 * Just back up to the previous extent, if so.
5465 if (got.br_startoff > bno) {
5468 xfs_iext_get_extent(ifp, lastx, &got);
5471 * Is the last block of this extent before the range
5472 * we're supposed to delete? If so, we're done.
5474 bno = XFS_FILEOFF_MIN(bno,
5475 got.br_startoff + got.br_blockcount - 1);
5479 * Then deal with the (possibly delayed) allocated space
5483 wasdel = isnullstartblock(del.br_startblock);
5484 if (got.br_startoff < start) {
5485 del.br_startoff = start;
5486 del.br_blockcount -= start - got.br_startoff;
5488 del.br_startblock += start - got.br_startoff;
5490 if (del.br_startoff + del.br_blockcount > bno + 1)
5491 del.br_blockcount = bno + 1 - del.br_startoff;
5492 sum = del.br_startblock + del.br_blockcount;
5494 (mod = do_mod(sum, mp->m_sb.sb_rextsize))) {
5496 * Realtime extent not lined up at the end.
5497 * The extent could have been split into written
5498 * and unwritten pieces, or we could just be
5499 * unmapping part of it. But we can't really
5500 * get rid of part of a realtime extent.
5502 if (del.br_state == XFS_EXT_UNWRITTEN ||
5503 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
5505 * This piece is unwritten, or we're not
5506 * using unwritten extents. Skip over it.
5509 bno -= mod > del.br_blockcount ?
5510 del.br_blockcount : mod;
5511 if (bno < got.br_startoff) {
5513 xfs_bmbt_get_all(xfs_iext_get_ext(
5519 * It's written, turn it unwritten.
5520 * This is better than zeroing it.
5522 ASSERT(del.br_state == XFS_EXT_NORM);
5523 ASSERT(tp->t_blk_res > 0);
5525 * If this spans a realtime extent boundary,
5526 * chop it back to the start of the one we end at.
5528 if (del.br_blockcount > mod) {
5529 del.br_startoff += del.br_blockcount - mod;
5530 del.br_startblock += del.br_blockcount - mod;
5531 del.br_blockcount = mod;
5533 del.br_state = XFS_EXT_UNWRITTEN;
5534 error = xfs_bmap_add_extent_unwritten_real(tp, ip,
5535 &lastx, &cur, &del, firstblock, dfops,
5541 if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) {
5543 * Realtime extent is lined up at the end but not
5544 * at the front. We'll get rid of full extents if
5547 mod = mp->m_sb.sb_rextsize - mod;
5548 if (del.br_blockcount > mod) {
5549 del.br_blockcount -= mod;
5550 del.br_startoff += mod;
5551 del.br_startblock += mod;
5552 } else if ((del.br_startoff == start &&
5553 (del.br_state == XFS_EXT_UNWRITTEN ||
5554 tp->t_blk_res == 0)) ||
5555 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
5557 * Can't make it unwritten. There isn't
5558 * a full extent here so just skip it.
5560 ASSERT(bno >= del.br_blockcount);
5561 bno -= del.br_blockcount;
5562 if (got.br_startoff > bno && --lastx >= 0)
5563 xfs_iext_get_extent(ifp, lastx, &got);
5565 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
5566 struct xfs_bmbt_irec prev;
5569 * This one is already unwritten.
5570 * It must have a written left neighbor.
5571 * Unwrite the killed part of that one and
5575 xfs_iext_get_extent(ifp, lastx - 1, &prev);
5576 ASSERT(prev.br_state == XFS_EXT_NORM);
5577 ASSERT(!isnullstartblock(prev.br_startblock));
5578 ASSERT(del.br_startblock ==
5579 prev.br_startblock + prev.br_blockcount);
5580 if (prev.br_startoff < start) {
5581 mod = start - prev.br_startoff;
5582 prev.br_blockcount -= mod;
5583 prev.br_startblock += mod;
5584 prev.br_startoff = start;
5586 prev.br_state = XFS_EXT_UNWRITTEN;
5588 error = xfs_bmap_add_extent_unwritten_real(tp,
5589 ip, &lastx, &cur, &prev,
5590 firstblock, dfops, &logflags);
5595 ASSERT(del.br_state == XFS_EXT_NORM);
5596 del.br_state = XFS_EXT_UNWRITTEN;
5597 error = xfs_bmap_add_extent_unwritten_real(tp,
5598 ip, &lastx, &cur, &del,
5599 firstblock, dfops, &logflags);
5607 * If it's the case where the directory code is running
5608 * with no block reservation, and the deleted block is in
5609 * the middle of its extent, and the resulting insert
5610 * of an extent would cause transformation to btree format,
5611 * then reject it. The calling code will then swap
5612 * blocks around instead.
5613 * We have to do this now, rather than waiting for the
5614 * conversion to btree format, since the transaction
5617 if (!wasdel && tp->t_blk_res == 0 &&
5618 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
5619 XFS_IFORK_NEXTENTS(ip, whichfork) >= /* Note the >= */
5620 XFS_IFORK_MAXEXT(ip, whichfork) &&
5621 del.br_startoff > got.br_startoff &&
5622 del.br_startoff + del.br_blockcount <
5623 got.br_startoff + got.br_blockcount) {
5629 * Unreserve quota and update realtime free space, if
5630 * appropriate. If delayed allocation, update the inode delalloc
5631 * counter now and wait to update the sb counters as
5632 * xfs_bmap_del_extent() might need to borrow some blocks.
5635 ASSERT(startblockval(del.br_startblock) > 0);
5637 xfs_filblks_t rtexts;
5639 rtexts = XFS_FSB_TO_B(mp, del.br_blockcount);
5640 do_div(rtexts, mp->m_sb.sb_rextsize);
5641 xfs_mod_frextents(mp, (int64_t)rtexts);
5642 (void)xfs_trans_reserve_quota_nblks(NULL,
5643 ip, -((long)del.br_blockcount), 0,
5644 XFS_QMOPT_RES_RTBLKS);
5646 (void)xfs_trans_reserve_quota_nblks(NULL,
5647 ip, -((long)del.br_blockcount), 0,
5648 XFS_QMOPT_RES_REGBLKS);
5650 ip->i_delayed_blks -= del.br_blockcount;
5652 cur->bc_private.b.flags |=
5653 XFS_BTCUR_BPRV_WASDEL;
5655 cur->bc_private.b.flags &= ~XFS_BTCUR_BPRV_WASDEL;
5657 error = xfs_bmap_del_extent(ip, tp, &lastx, dfops, cur, &del,
5658 &tmp_logflags, whichfork, flags);
5659 logflags |= tmp_logflags;
5663 if (!isrt && wasdel)
5664 xfs_mod_fdblocks(mp, (int64_t)del.br_blockcount, false);
5666 bno = del.br_startoff - 1;
5669 * If not done go on to the next (previous) record.
5671 if (bno != (xfs_fileoff_t)-1 && bno >= start) {
5673 xfs_iext_get_extent(ifp, lastx, &got);
5674 if (got.br_startoff > bno && --lastx >= 0)
5675 xfs_iext_get_extent(ifp, lastx, &got);
5680 if (bno == (xfs_fileoff_t)-1 || bno < start || lastx < 0)
5683 *rlen = bno - start + 1;
5686 * Convert to a btree if necessary.
5688 if (xfs_bmap_needs_btree(ip, whichfork)) {
5689 ASSERT(cur == NULL);
5690 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, dfops,
5691 &cur, 0, &tmp_logflags, whichfork);
5692 logflags |= tmp_logflags;
5697 * transform from btree to extents, give it cur
5699 else if (xfs_bmap_wants_extents(ip, whichfork)) {
5700 ASSERT(cur != NULL);
5701 error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags,
5703 logflags |= tmp_logflags;
5708 * transform from extents to local?
5713 * Log everything. Do this after conversion, there's no point in
5714 * logging the extent records if we've converted to btree format.
5716 if ((logflags & xfs_ilog_fext(whichfork)) &&
5717 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
5718 logflags &= ~xfs_ilog_fext(whichfork);
5719 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
5720 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
5721 logflags &= ~xfs_ilog_fbroot(whichfork);
5723 * Log inode even in the error case, if the transaction
5724 * is dirty we'll need to shut down the filesystem.
5727 xfs_trans_log_inode(tp, ip, logflags);
5730 *firstblock = cur->bc_private.b.firstblock;
5731 cur->bc_private.b.allocated = 0;
5733 xfs_btree_del_cursor(cur,
5734 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5739 /* Unmap a range of a file. */
5743 struct xfs_inode *ip,
5748 xfs_fsblock_t *firstblock,
5749 struct xfs_defer_ops *dfops,
5754 error = __xfs_bunmapi(tp, ip, bno, &len, flags, nexts, firstblock,
5761 * Determine whether an extent shift can be accomplished by a merge with the
5762 * extent that precedes the target hole of the shift.
5766 struct xfs_bmbt_irec *left, /* preceding extent */
5767 struct xfs_bmbt_irec *got, /* current extent to shift */
5768 xfs_fileoff_t shift) /* shift fsb */
5770 xfs_fileoff_t startoff;
5772 startoff = got->br_startoff - shift;
5775 * The extent, once shifted, must be adjacent in-file and on-disk with
5776 * the preceding extent.
5778 if ((left->br_startoff + left->br_blockcount != startoff) ||
5779 (left->br_startblock + left->br_blockcount != got->br_startblock) ||
5780 (left->br_state != got->br_state) ||
5781 (left->br_blockcount + got->br_blockcount > MAXEXTLEN))
5788 * A bmap extent shift adjusts the file offset of an extent to fill a preceding
5789 * hole in the file. If an extent shift would result in the extent being fully
5790 * adjacent to the extent that currently precedes the hole, we can merge with
5791 * the preceding extent rather than do the shift.
5793 * This function assumes the caller has verified a shift-by-merge is possible
5794 * with the provided extents via xfs_bmse_can_merge().
5798 struct xfs_inode *ip,
5800 xfs_fileoff_t shift, /* shift fsb */
5801 int current_ext, /* idx of gotp */
5802 struct xfs_bmbt_rec_host *gotp, /* extent to shift */
5803 struct xfs_bmbt_rec_host *leftp, /* preceding extent */
5804 struct xfs_btree_cur *cur,
5805 int *logflags) /* output */
5807 struct xfs_bmbt_irec got;
5808 struct xfs_bmbt_irec left;
5809 xfs_filblks_t blockcount;
5811 struct xfs_mount *mp = ip->i_mount;
5813 xfs_bmbt_get_all(gotp, &got);
5814 xfs_bmbt_get_all(leftp, &left);
5815 blockcount = left.br_blockcount + got.br_blockcount;
5817 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
5818 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
5819 ASSERT(xfs_bmse_can_merge(&left, &got, shift));
5822 * Merge the in-core extents. Note that the host record pointers and
5823 * current_ext index are invalid once the extent has been removed via
5824 * xfs_iext_remove().
5826 xfs_bmbt_set_blockcount(leftp, blockcount);
5827 xfs_iext_remove(ip, current_ext, 1, 0);
5830 * Update the on-disk extent count, the btree if necessary and log the
5833 XFS_IFORK_NEXT_SET(ip, whichfork,
5834 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
5835 *logflags |= XFS_ILOG_CORE;
5837 *logflags |= XFS_ILOG_DEXT;
5841 /* lookup and remove the extent to merge */
5842 error = xfs_bmbt_lookup_eq(cur, got.br_startoff, got.br_startblock,
5843 got.br_blockcount, &i);
5846 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
5848 error = xfs_btree_delete(cur, &i);
5851 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
5853 /* lookup and update size of the previous extent */
5854 error = xfs_bmbt_lookup_eq(cur, left.br_startoff, left.br_startblock,
5855 left.br_blockcount, &i);
5858 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
5860 left.br_blockcount = blockcount;
5862 return xfs_bmbt_update(cur, left.br_startoff, left.br_startblock,
5863 left.br_blockcount, left.br_state);
5867 * Shift a single extent.
5871 struct xfs_inode *ip,
5873 xfs_fileoff_t offset_shift_fsb,
5875 struct xfs_bmbt_rec_host *gotp,
5876 struct xfs_btree_cur *cur,
5878 enum shift_direction direction,
5879 struct xfs_defer_ops *dfops)
5881 struct xfs_ifork *ifp;
5882 struct xfs_mount *mp;
5883 xfs_fileoff_t startoff;
5884 struct xfs_bmbt_rec_host *adj_irecp;
5885 struct xfs_bmbt_irec got;
5886 struct xfs_bmbt_irec adj_irec;
5892 ifp = XFS_IFORK_PTR(ip, whichfork);
5893 total_extents = xfs_iext_count(ifp);
5895 xfs_bmbt_get_all(gotp, &got);
5897 /* delalloc extents should be prevented by caller */
5898 XFS_WANT_CORRUPTED_RETURN(mp, !isnullstartblock(got.br_startblock));
5900 if (direction == SHIFT_LEFT) {
5901 startoff = got.br_startoff - offset_shift_fsb;
5904 * Check for merge if we've got an extent to the left,
5905 * otherwise make sure there's enough room at the start
5906 * of the file for the shift.
5908 if (!*current_ext) {
5909 if (got.br_startoff < offset_shift_fsb)
5911 goto update_current_ext;
5914 * grab the left extent and check for a large
5917 adj_irecp = xfs_iext_get_ext(ifp, *current_ext - 1);
5918 xfs_bmbt_get_all(adj_irecp, &adj_irec);
5921 adj_irec.br_startoff + adj_irec.br_blockcount)
5924 /* check whether to merge the extent or shift it down */
5925 if (xfs_bmse_can_merge(&adj_irec, &got,
5926 offset_shift_fsb)) {
5927 error = xfs_bmse_merge(ip, whichfork, offset_shift_fsb,
5928 *current_ext, gotp, adj_irecp,
5936 startoff = got.br_startoff + offset_shift_fsb;
5937 /* nothing to move if this is the last extent */
5938 if (*current_ext >= (total_extents - 1))
5939 goto update_current_ext;
5941 * If this is not the last extent in the file, make sure there
5942 * is enough room between current extent and next extent for
5943 * accommodating the shift.
5945 adj_irecp = xfs_iext_get_ext(ifp, *current_ext + 1);
5946 xfs_bmbt_get_all(adj_irecp, &adj_irec);
5947 if (startoff + got.br_blockcount > adj_irec.br_startoff)
5950 * Unlike a left shift (which involves a hole punch),
5951 * a right shift does not modify extent neighbors
5952 * in any way. We should never find mergeable extents
5953 * in this scenario. Check anyways and warn if we
5954 * encounter two extents that could be one.
5956 if (xfs_bmse_can_merge(&got, &adj_irec, offset_shift_fsb))
5960 * Increment the extent index for the next iteration, update the start
5961 * offset of the in-core extent and update the btree if applicable.
5964 if (direction == SHIFT_LEFT)
5968 xfs_bmbt_set_startoff(gotp, startoff);
5969 *logflags |= XFS_ILOG_CORE;
5972 *logflags |= XFS_ILOG_DEXT;
5976 error = xfs_bmbt_lookup_eq(cur, got.br_startoff, got.br_startblock,
5977 got.br_blockcount, &i);
5980 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
5982 got.br_startoff = startoff;
5983 error = xfs_bmbt_update(cur, got.br_startoff, got.br_startblock,
5984 got.br_blockcount, got.br_state);
5989 /* update reverse mapping */
5990 error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, &adj_irec);
5993 adj_irec.br_startoff = startoff;
5994 return xfs_rmap_map_extent(mp, dfops, ip, whichfork, &adj_irec);
5998 * Shift extent records to the left/right to cover/create a hole.
6000 * The maximum number of extents to be shifted in a single operation is
6001 * @num_exts. @stop_fsb specifies the file offset at which to stop shift and the
6002 * file offset where we've left off is returned in @next_fsb. @offset_shift_fsb
6003 * is the length by which each extent is shifted. If there is no hole to shift
6004 * the extents into, this will be considered invalid operation and we abort
6008 xfs_bmap_shift_extents(
6009 struct xfs_trans *tp,
6010 struct xfs_inode *ip,
6011 xfs_fileoff_t *next_fsb,
6012 xfs_fileoff_t offset_shift_fsb,
6014 xfs_fileoff_t stop_fsb,
6015 xfs_fsblock_t *firstblock,
6016 struct xfs_defer_ops *dfops,
6017 enum shift_direction direction,
6020 struct xfs_btree_cur *cur = NULL;
6021 struct xfs_bmbt_rec_host *gotp;
6022 struct xfs_bmbt_irec got;
6023 struct xfs_mount *mp = ip->i_mount;
6024 struct xfs_ifork *ifp;
6025 xfs_extnum_t nexts = 0;
6026 xfs_extnum_t current_ext;
6027 xfs_extnum_t total_extents;
6028 xfs_extnum_t stop_extent;
6030 int whichfork = XFS_DATA_FORK;
6033 if (unlikely(XFS_TEST_ERROR(
6034 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
6035 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
6036 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
6037 XFS_ERROR_REPORT("xfs_bmap_shift_extents",
6038 XFS_ERRLEVEL_LOW, mp);
6039 return -EFSCORRUPTED;
6042 if (XFS_FORCED_SHUTDOWN(mp))
6045 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
6046 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
6047 ASSERT(direction == SHIFT_LEFT || direction == SHIFT_RIGHT);
6048 ASSERT(*next_fsb != NULLFSBLOCK || direction == SHIFT_RIGHT);
6050 ifp = XFS_IFORK_PTR(ip, whichfork);
6051 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
6052 /* Read in all the extents */
6053 error = xfs_iread_extents(tp, ip, whichfork);
6058 if (ifp->if_flags & XFS_IFBROOT) {
6059 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
6060 cur->bc_private.b.firstblock = *firstblock;
6061 cur->bc_private.b.dfops = dfops;
6062 cur->bc_private.b.flags = 0;
6066 * There may be delalloc extents in the data fork before the range we
6067 * are collapsing out, so we cannot use the count of real extents here.
6068 * Instead we have to calculate it from the incore fork.
6070 total_extents = xfs_iext_count(ifp);
6071 if (total_extents == 0) {
6077 * In case of first right shift, we need to initialize next_fsb
6079 if (*next_fsb == NULLFSBLOCK) {
6080 gotp = xfs_iext_get_ext(ifp, total_extents - 1);
6081 xfs_bmbt_get_all(gotp, &got);
6082 *next_fsb = got.br_startoff;
6083 if (stop_fsb > *next_fsb) {
6089 /* Lookup the extent index at which we have to stop */
6090 if (direction == SHIFT_RIGHT) {
6091 gotp = xfs_iext_bno_to_ext(ifp, stop_fsb, &stop_extent);
6092 /* Make stop_extent exclusive of shift range */
6095 stop_extent = total_extents;
6098 * Look up the extent index for the fsb where we start shifting. We can
6099 * henceforth iterate with current_ext as extent list changes are locked
6102 * gotp can be null in 2 cases: 1) if there are no extents or 2)
6103 * *next_fsb lies in a hole beyond which there are no extents. Either
6106 gotp = xfs_iext_bno_to_ext(ifp, *next_fsb, ¤t_ext);
6112 /* some sanity checking before we finally start shifting extents */
6113 if ((direction == SHIFT_LEFT && current_ext >= stop_extent) ||
6114 (direction == SHIFT_RIGHT && current_ext <= stop_extent)) {
6119 while (nexts++ < num_exts) {
6120 error = xfs_bmse_shift_one(ip, whichfork, offset_shift_fsb,
6121 ¤t_ext, gotp, cur, &logflags,
6126 * If there was an extent merge during the shift, the extent
6127 * count can change. Update the total and grade the next record.
6129 if (direction == SHIFT_LEFT) {
6130 total_extents = xfs_iext_count(ifp);
6131 stop_extent = total_extents;
6134 if (current_ext == stop_extent) {
6136 *next_fsb = NULLFSBLOCK;
6139 gotp = xfs_iext_get_ext(ifp, current_ext);
6143 xfs_bmbt_get_all(gotp, &got);
6144 *next_fsb = got.br_startoff;
6149 xfs_btree_del_cursor(cur,
6150 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
6153 xfs_trans_log_inode(tp, ip, logflags);
6159 * Splits an extent into two extents at split_fsb block such that it is
6160 * the first block of the current_ext. @current_ext is a target extent
6161 * to be split. @split_fsb is a block where the extents is split.
6162 * If split_fsb lies in a hole or the first block of extents, just return 0.
6165 xfs_bmap_split_extent_at(
6166 struct xfs_trans *tp,
6167 struct xfs_inode *ip,
6168 xfs_fileoff_t split_fsb,
6169 xfs_fsblock_t *firstfsb,
6170 struct xfs_defer_ops *dfops)
6172 int whichfork = XFS_DATA_FORK;
6173 struct xfs_btree_cur *cur = NULL;
6174 struct xfs_bmbt_rec_host *gotp;
6175 struct xfs_bmbt_irec got;
6176 struct xfs_bmbt_irec new; /* split extent */
6177 struct xfs_mount *mp = ip->i_mount;
6178 struct xfs_ifork *ifp;
6179 xfs_fsblock_t gotblkcnt; /* new block count for got */
6180 xfs_extnum_t current_ext;
6185 if (unlikely(XFS_TEST_ERROR(
6186 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
6187 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
6188 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
6189 XFS_ERROR_REPORT("xfs_bmap_split_extent_at",
6190 XFS_ERRLEVEL_LOW, mp);
6191 return -EFSCORRUPTED;
6194 if (XFS_FORCED_SHUTDOWN(mp))
6197 ifp = XFS_IFORK_PTR(ip, whichfork);
6198 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
6199 /* Read in all the extents */
6200 error = xfs_iread_extents(tp, ip, whichfork);
6206 * gotp can be null in 2 cases: 1) if there are no extents
6207 * or 2) split_fsb lies in a hole beyond which there are
6208 * no extents. Either way, we are done.
6210 gotp = xfs_iext_bno_to_ext(ifp, split_fsb, ¤t_ext);
6214 xfs_bmbt_get_all(gotp, &got);
6217 * Check split_fsb lies in a hole or the start boundary offset
6220 if (got.br_startoff >= split_fsb)
6223 gotblkcnt = split_fsb - got.br_startoff;
6224 new.br_startoff = split_fsb;
6225 new.br_startblock = got.br_startblock + gotblkcnt;
6226 new.br_blockcount = got.br_blockcount - gotblkcnt;
6227 new.br_state = got.br_state;
6229 if (ifp->if_flags & XFS_IFBROOT) {
6230 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
6231 cur->bc_private.b.firstblock = *firstfsb;
6232 cur->bc_private.b.dfops = dfops;
6233 cur->bc_private.b.flags = 0;
6234 error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
6240 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor);
6243 xfs_bmbt_set_blockcount(gotp, gotblkcnt);
6244 got.br_blockcount = gotblkcnt;
6246 logflags = XFS_ILOG_CORE;
6248 error = xfs_bmbt_update(cur, got.br_startoff,
6255 logflags |= XFS_ILOG_DEXT;
6257 /* Add new extent */
6259 xfs_iext_insert(ip, current_ext, 1, &new, 0);
6260 XFS_IFORK_NEXT_SET(ip, whichfork,
6261 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
6264 error = xfs_bmbt_lookup_eq(cur, new.br_startoff,
6265 new.br_startblock, new.br_blockcount,
6269 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, del_cursor);
6270 cur->bc_rec.b.br_state = new.br_state;
6272 error = xfs_btree_insert(cur, &i);
6275 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor);
6279 * Convert to a btree if necessary.
6281 if (xfs_bmap_needs_btree(ip, whichfork)) {
6282 int tmp_logflags; /* partial log flag return val */
6284 ASSERT(cur == NULL);
6285 error = xfs_bmap_extents_to_btree(tp, ip, firstfsb, dfops,
6286 &cur, 0, &tmp_logflags, whichfork);
6287 logflags |= tmp_logflags;
6292 cur->bc_private.b.allocated = 0;
6293 xfs_btree_del_cursor(cur,
6294 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
6298 xfs_trans_log_inode(tp, ip, logflags);
6303 xfs_bmap_split_extent(
6304 struct xfs_inode *ip,
6305 xfs_fileoff_t split_fsb)
6307 struct xfs_mount *mp = ip->i_mount;
6308 struct xfs_trans *tp;
6309 struct xfs_defer_ops dfops;
6310 xfs_fsblock_t firstfsb;
6313 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write,
6314 XFS_DIOSTRAT_SPACE_RES(mp, 0), 0, 0, &tp);
6318 xfs_ilock(ip, XFS_ILOCK_EXCL);
6319 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
6321 xfs_defer_init(&dfops, &firstfsb);
6323 error = xfs_bmap_split_extent_at(tp, ip, split_fsb,
6328 error = xfs_defer_finish(&tp, &dfops, NULL);
6332 return xfs_trans_commit(tp);
6335 xfs_defer_cancel(&dfops);
6336 xfs_trans_cancel(tp);
6340 /* Deferred mapping is only for real extents in the data fork. */
6342 xfs_bmap_is_update_needed(
6343 struct xfs_bmbt_irec *bmap)
6345 return bmap->br_startblock != HOLESTARTBLOCK &&
6346 bmap->br_startblock != DELAYSTARTBLOCK;
6349 /* Record a bmap intent. */
6352 struct xfs_mount *mp,
6353 struct xfs_defer_ops *dfops,
6354 enum xfs_bmap_intent_type type,
6355 struct xfs_inode *ip,
6357 struct xfs_bmbt_irec *bmap)
6360 struct xfs_bmap_intent *bi;
6362 trace_xfs_bmap_defer(mp,
6363 XFS_FSB_TO_AGNO(mp, bmap->br_startblock),
6365 XFS_FSB_TO_AGBNO(mp, bmap->br_startblock),
6366 ip->i_ino, whichfork,
6368 bmap->br_blockcount,
6371 bi = kmem_alloc(sizeof(struct xfs_bmap_intent), KM_SLEEP | KM_NOFS);
6372 INIT_LIST_HEAD(&bi->bi_list);
6375 bi->bi_whichfork = whichfork;
6376 bi->bi_bmap = *bmap;
6378 error = xfs_defer_join(dfops, bi->bi_owner);
6384 xfs_defer_add(dfops, XFS_DEFER_OPS_TYPE_BMAP, &bi->bi_list);
6388 /* Map an extent into a file. */
6390 xfs_bmap_map_extent(
6391 struct xfs_mount *mp,
6392 struct xfs_defer_ops *dfops,
6393 struct xfs_inode *ip,
6394 struct xfs_bmbt_irec *PREV)
6396 if (!xfs_bmap_is_update_needed(PREV))
6399 return __xfs_bmap_add(mp, dfops, XFS_BMAP_MAP, ip,
6400 XFS_DATA_FORK, PREV);
6403 /* Unmap an extent out of a file. */
6405 xfs_bmap_unmap_extent(
6406 struct xfs_mount *mp,
6407 struct xfs_defer_ops *dfops,
6408 struct xfs_inode *ip,
6409 struct xfs_bmbt_irec *PREV)
6411 if (!xfs_bmap_is_update_needed(PREV))
6414 return __xfs_bmap_add(mp, dfops, XFS_BMAP_UNMAP, ip,
6415 XFS_DATA_FORK, PREV);
6419 * Process one of the deferred bmap operations. We pass back the
6420 * btree cursor to maintain our lock on the bmapbt between calls.
6423 xfs_bmap_finish_one(
6424 struct xfs_trans *tp,
6425 struct xfs_defer_ops *dfops,
6426 struct xfs_inode *ip,
6427 enum xfs_bmap_intent_type type,
6429 xfs_fileoff_t startoff,
6430 xfs_fsblock_t startblock,
6431 xfs_filblks_t blockcount,
6434 struct xfs_bmbt_irec bmap;
6436 xfs_fsblock_t firstfsb;
6437 int flags = XFS_BMAPI_REMAP;
6441 bmap.br_startblock = startblock;
6442 bmap.br_startoff = startoff;
6443 bmap.br_blockcount = blockcount;
6444 bmap.br_state = state;
6446 trace_xfs_bmap_deferred(tp->t_mountp,
6447 XFS_FSB_TO_AGNO(tp->t_mountp, startblock), type,
6448 XFS_FSB_TO_AGBNO(tp->t_mountp, startblock),
6449 ip->i_ino, whichfork, startoff, blockcount, state);
6451 if (whichfork != XFS_DATA_FORK && whichfork != XFS_ATTR_FORK)
6452 return -EFSCORRUPTED;
6453 if (whichfork == XFS_ATTR_FORK)
6454 flags |= XFS_BMAPI_ATTRFORK;
6456 if (XFS_TEST_ERROR(false, tp->t_mountp,
6457 XFS_ERRTAG_BMAP_FINISH_ONE,
6458 XFS_RANDOM_BMAP_FINISH_ONE))
6463 firstfsb = bmap.br_startblock;
6464 error = xfs_bmapi_write(tp, ip, bmap.br_startoff,
6465 bmap.br_blockcount, flags, &firstfsb,
6466 bmap.br_blockcount, &bmap, &nimaps,
6469 case XFS_BMAP_UNMAP:
6470 error = xfs_bunmapi(tp, ip, bmap.br_startoff,
6471 bmap.br_blockcount, flags, 1, &firstfsb,
6477 error = -EFSCORRUPTED;