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_errortag.h"
42 #include "xfs_error.h"
43 #include "xfs_quota.h"
44 #include "xfs_trans_space.h"
45 #include "xfs_buf_item.h"
46 #include "xfs_trace.h"
47 #include "xfs_symlink.h"
48 #include "xfs_attr_leaf.h"
49 #include "xfs_filestream.h"
51 #include "xfs_ag_resv.h"
52 #include "xfs_refcount.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,
116 struct xfs_bmbt_irec *irec,
117 int *stat) /* success/failure */
119 cur->bc_rec.b = *irec;
120 return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
123 STATIC int /* error */
124 xfs_bmbt_lookup_first(
125 struct xfs_btree_cur *cur,
126 int *stat) /* success/failure */
128 cur->bc_rec.b.br_startoff = 0;
129 cur->bc_rec.b.br_startblock = 0;
130 cur->bc_rec.b.br_blockcount = 0;
131 return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
135 * Check if the inode needs to be converted to btree format.
137 static inline bool xfs_bmap_needs_btree(struct xfs_inode *ip, int whichfork)
139 return whichfork != XFS_COW_FORK &&
140 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
141 XFS_IFORK_NEXTENTS(ip, whichfork) >
142 XFS_IFORK_MAXEXT(ip, whichfork);
146 * Check if the inode should be converted to extent format.
148 static inline bool xfs_bmap_wants_extents(struct xfs_inode *ip, int whichfork)
150 return whichfork != XFS_COW_FORK &&
151 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
152 XFS_IFORK_NEXTENTS(ip, whichfork) <=
153 XFS_IFORK_MAXEXT(ip, whichfork);
157 * Update the record referred to by cur to the value given by irec
158 * This either works (return 0) or gets an EFSCORRUPTED error.
162 struct xfs_btree_cur *cur,
163 struct xfs_bmbt_irec *irec)
165 union xfs_btree_rec rec;
167 xfs_bmbt_disk_set_all(&rec.bmbt, irec);
168 return xfs_btree_update(cur, &rec);
172 * Compute the worst-case number of indirect blocks that will be used
173 * for ip's delayed extent of length "len".
176 xfs_bmap_worst_indlen(
177 xfs_inode_t *ip, /* incore inode pointer */
178 xfs_filblks_t len) /* delayed extent length */
180 int level; /* btree level number */
181 int maxrecs; /* maximum record count at this level */
182 xfs_mount_t *mp; /* mount structure */
183 xfs_filblks_t rval; /* return value */
186 maxrecs = mp->m_bmap_dmxr[0];
187 for (level = 0, rval = 0;
188 level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK);
191 do_div(len, maxrecs);
194 return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) -
197 maxrecs = mp->m_bmap_dmxr[1];
203 * Calculate the default attribute fork offset for newly created inodes.
206 xfs_default_attroffset(
207 struct xfs_inode *ip)
209 struct xfs_mount *mp = ip->i_mount;
212 if (mp->m_sb.sb_inodesize == 256) {
213 offset = XFS_LITINO(mp, ip->i_d.di_version) -
214 XFS_BMDR_SPACE_CALC(MINABTPTRS);
216 offset = XFS_BMDR_SPACE_CALC(6 * MINABTPTRS);
219 ASSERT(offset < XFS_LITINO(mp, ip->i_d.di_version));
224 * Helper routine to reset inode di_forkoff field when switching
225 * attribute fork from local to extent format - we reset it where
226 * possible to make space available for inline data fork extents.
229 xfs_bmap_forkoff_reset(
233 if (whichfork == XFS_ATTR_FORK &&
234 ip->i_d.di_format != XFS_DINODE_FMT_DEV &&
235 ip->i_d.di_format != XFS_DINODE_FMT_BTREE) {
236 uint dfl_forkoff = xfs_default_attroffset(ip) >> 3;
238 if (dfl_forkoff > ip->i_d.di_forkoff)
239 ip->i_d.di_forkoff = dfl_forkoff;
244 STATIC struct xfs_buf *
246 struct xfs_btree_cur *cur,
249 struct xfs_log_item_desc *lidp;
255 for (i = 0; i < XFS_BTREE_MAXLEVELS; i++) {
256 if (!cur->bc_bufs[i])
258 if (XFS_BUF_ADDR(cur->bc_bufs[i]) == bno)
259 return cur->bc_bufs[i];
262 /* Chase down all the log items to see if the bp is there */
263 list_for_each_entry(lidp, &cur->bc_tp->t_items, lid_trans) {
264 struct xfs_buf_log_item *bip;
265 bip = (struct xfs_buf_log_item *)lidp->lid_item;
266 if (bip->bli_item.li_type == XFS_LI_BUF &&
267 XFS_BUF_ADDR(bip->bli_buf) == bno)
276 struct xfs_btree_block *block,
282 __be64 *pp, *thispa; /* pointer to block address */
283 xfs_bmbt_key_t *prevp, *keyp;
285 ASSERT(be16_to_cpu(block->bb_level) > 0);
288 for( i = 1; i <= xfs_btree_get_numrecs(block); i++) {
289 dmxr = mp->m_bmap_dmxr[0];
290 keyp = XFS_BMBT_KEY_ADDR(mp, block, i);
293 ASSERT(be64_to_cpu(prevp->br_startoff) <
294 be64_to_cpu(keyp->br_startoff));
299 * Compare the block numbers to see if there are dups.
302 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, i, sz);
304 pp = XFS_BMBT_PTR_ADDR(mp, block, i, dmxr);
306 for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) {
308 thispa = XFS_BMAP_BROOT_PTR_ADDR(mp, block, j, sz);
310 thispa = XFS_BMBT_PTR_ADDR(mp, block, j, dmxr);
311 if (*thispa == *pp) {
312 xfs_warn(mp, "%s: thispa(%d) == pp(%d) %Ld",
314 (unsigned long long)be64_to_cpu(*thispa));
315 panic("%s: ptrs are equal in node\n",
323 * Check that the extents for the inode ip are in the right order in all
324 * btree leaves. THis becomes prohibitively expensive for large extent count
325 * files, so don't bother with inodes that have more than 10,000 extents in
326 * them. The btree record ordering checks will still be done, so for such large
327 * bmapbt constructs that is going to catch most corruptions.
330 xfs_bmap_check_leaf_extents(
331 xfs_btree_cur_t *cur, /* btree cursor or null */
332 xfs_inode_t *ip, /* incore inode pointer */
333 int whichfork) /* data or attr fork */
335 struct xfs_btree_block *block; /* current btree block */
336 xfs_fsblock_t bno; /* block # of "block" */
337 xfs_buf_t *bp; /* buffer for "block" */
338 int error; /* error return value */
339 xfs_extnum_t i=0, j; /* index into the extents list */
340 xfs_ifork_t *ifp; /* fork structure */
341 int level; /* btree level, for checking */
342 xfs_mount_t *mp; /* file system mount structure */
343 __be64 *pp; /* pointer to block address */
344 xfs_bmbt_rec_t *ep; /* pointer to current extent */
345 xfs_bmbt_rec_t last = {0, 0}; /* last extent in prev block */
346 xfs_bmbt_rec_t *nextp; /* pointer to next extent */
349 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) {
353 /* skip large extent count inodes */
354 if (ip->i_d.di_nextents > 10000)
359 ifp = XFS_IFORK_PTR(ip, whichfork);
360 block = ifp->if_broot;
362 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
364 level = be16_to_cpu(block->bb_level);
366 xfs_check_block(block, mp, 1, ifp->if_broot_bytes);
367 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
368 bno = be64_to_cpu(*pp);
370 ASSERT(bno != NULLFSBLOCK);
371 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
372 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
375 * Go down the tree until leaf level is reached, following the first
376 * pointer (leftmost) at each level.
378 while (level-- > 0) {
379 /* See if buf is in cur first */
381 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
384 error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
390 block = XFS_BUF_TO_BLOCK(bp);
395 * Check this block for basic sanity (increasing keys and
396 * no duplicate blocks).
399 xfs_check_block(block, mp, 0, 0);
400 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
401 bno = be64_to_cpu(*pp);
402 XFS_WANT_CORRUPTED_GOTO(mp,
403 xfs_verify_fsbno(mp, bno), error0);
406 xfs_trans_brelse(NULL, bp);
411 * Here with bp and block set to the leftmost leaf node in the tree.
416 * Loop over all leaf nodes checking that all extents are in the right order.
419 xfs_fsblock_t nextbno;
420 xfs_extnum_t num_recs;
423 num_recs = xfs_btree_get_numrecs(block);
426 * Read-ahead the next leaf block, if any.
429 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
432 * Check all the extents to make sure they are OK.
433 * If we had a previous block, the last entry should
434 * conform with the first entry in this one.
437 ep = XFS_BMBT_REC_ADDR(mp, block, 1);
439 ASSERT(xfs_bmbt_disk_get_startoff(&last) +
440 xfs_bmbt_disk_get_blockcount(&last) <=
441 xfs_bmbt_disk_get_startoff(ep));
443 for (j = 1; j < num_recs; j++) {
444 nextp = XFS_BMBT_REC_ADDR(mp, block, j + 1);
445 ASSERT(xfs_bmbt_disk_get_startoff(ep) +
446 xfs_bmbt_disk_get_blockcount(ep) <=
447 xfs_bmbt_disk_get_startoff(nextp));
455 xfs_trans_brelse(NULL, bp);
459 * If we've reached the end, stop.
461 if (bno == NULLFSBLOCK)
465 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
468 error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
474 block = XFS_BUF_TO_BLOCK(bp);
480 xfs_warn(mp, "%s: at error0", __func__);
482 xfs_trans_brelse(NULL, bp);
484 xfs_warn(mp, "%s: BAD after btree leaves for %d extents",
486 panic("%s: CORRUPTED BTREE OR SOMETHING", __func__);
491 * Validate that the bmbt_irecs being returned from bmapi are valid
492 * given the caller's original parameters. Specifically check the
493 * ranges of the returned irecs to ensure that they only extend beyond
494 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
497 xfs_bmap_validate_ret(
501 xfs_bmbt_irec_t *mval,
505 int i; /* index to map values */
507 ASSERT(ret_nmap <= nmap);
509 for (i = 0; i < ret_nmap; i++) {
510 ASSERT(mval[i].br_blockcount > 0);
511 if (!(flags & XFS_BMAPI_ENTIRE)) {
512 ASSERT(mval[i].br_startoff >= bno);
513 ASSERT(mval[i].br_blockcount <= len);
514 ASSERT(mval[i].br_startoff + mval[i].br_blockcount <=
517 ASSERT(mval[i].br_startoff < bno + len);
518 ASSERT(mval[i].br_startoff + mval[i].br_blockcount >
522 mval[i - 1].br_startoff + mval[i - 1].br_blockcount ==
523 mval[i].br_startoff);
524 ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK &&
525 mval[i].br_startblock != HOLESTARTBLOCK);
526 ASSERT(mval[i].br_state == XFS_EXT_NORM ||
527 mval[i].br_state == XFS_EXT_UNWRITTEN);
532 #define xfs_bmap_check_leaf_extents(cur, ip, whichfork) do { } while (0)
533 #define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap) do { } while (0)
537 * bmap free list manipulation functions
541 * Add the extent to the list of extents to be free at transaction end.
542 * The list is maintained sorted (by block number).
546 struct xfs_mount *mp,
547 struct xfs_defer_ops *dfops,
550 struct xfs_owner_info *oinfo)
552 struct xfs_extent_free_item *new; /* new element */
557 ASSERT(bno != NULLFSBLOCK);
559 ASSERT(len <= MAXEXTLEN);
560 ASSERT(!isnullstartblock(bno));
561 agno = XFS_FSB_TO_AGNO(mp, bno);
562 agbno = XFS_FSB_TO_AGBNO(mp, bno);
563 ASSERT(agno < mp->m_sb.sb_agcount);
564 ASSERT(agbno < mp->m_sb.sb_agblocks);
565 ASSERT(len < mp->m_sb.sb_agblocks);
566 ASSERT(agbno + len <= mp->m_sb.sb_agblocks);
568 ASSERT(xfs_bmap_free_item_zone != NULL);
570 new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP);
571 new->xefi_startblock = bno;
572 new->xefi_blockcount = (xfs_extlen_t)len;
574 new->xefi_oinfo = *oinfo;
576 xfs_rmap_skip_owner_update(&new->xefi_oinfo);
577 trace_xfs_bmap_free_defer(mp, XFS_FSB_TO_AGNO(mp, bno), 0,
578 XFS_FSB_TO_AGBNO(mp, bno), len);
579 xfs_defer_add(dfops, XFS_DEFER_OPS_TYPE_FREE, &new->xefi_list);
583 * Inode fork format manipulation functions
587 * Transform a btree format file with only one leaf node, where the
588 * extents list will fit in the inode, into an extents format file.
589 * Since the file extents are already in-core, all we have to do is
590 * give up the space for the btree root and pitch the leaf block.
592 STATIC int /* error */
593 xfs_bmap_btree_to_extents(
594 xfs_trans_t *tp, /* transaction pointer */
595 xfs_inode_t *ip, /* incore inode pointer */
596 xfs_btree_cur_t *cur, /* btree cursor */
597 int *logflagsp, /* inode logging flags */
598 int whichfork) /* data or attr fork */
601 struct xfs_btree_block *cblock;/* child btree block */
602 xfs_fsblock_t cbno; /* child block number */
603 xfs_buf_t *cbp; /* child block's buffer */
604 int error; /* error return value */
605 xfs_ifork_t *ifp; /* inode fork data */
606 xfs_mount_t *mp; /* mount point structure */
607 __be64 *pp; /* ptr to block address */
608 struct xfs_btree_block *rblock;/* root btree block */
609 struct xfs_owner_info oinfo;
612 ifp = XFS_IFORK_PTR(ip, whichfork);
613 ASSERT(whichfork != XFS_COW_FORK);
614 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
615 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
616 rblock = ifp->if_broot;
617 ASSERT(be16_to_cpu(rblock->bb_level) == 1);
618 ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1);
619 ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0) == 1);
620 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, ifp->if_broot_bytes);
621 cbno = be64_to_cpu(*pp);
624 XFS_WANT_CORRUPTED_RETURN(cur->bc_mp,
625 xfs_btree_check_lptr(cur, cbno, 1));
627 error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp, XFS_BMAP_BTREE_REF,
631 cblock = XFS_BUF_TO_BLOCK(cbp);
632 if ((error = xfs_btree_check_block(cur, cblock, 0, cbp)))
634 xfs_rmap_ino_bmbt_owner(&oinfo, ip->i_ino, whichfork);
635 xfs_bmap_add_free(mp, cur->bc_private.b.dfops, cbno, 1, &oinfo);
636 ip->i_d.di_nblocks--;
637 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
638 xfs_trans_binval(tp, cbp);
639 if (cur->bc_bufs[0] == cbp)
640 cur->bc_bufs[0] = NULL;
641 xfs_iroot_realloc(ip, -1, whichfork);
642 ASSERT(ifp->if_broot == NULL);
643 ASSERT((ifp->if_flags & XFS_IFBROOT) == 0);
644 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
645 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
650 * Convert an extents-format file into a btree-format file.
651 * The new file will have a root block (in the inode) and a single child block.
653 STATIC int /* error */
654 xfs_bmap_extents_to_btree(
655 xfs_trans_t *tp, /* transaction pointer */
656 xfs_inode_t *ip, /* incore inode pointer */
657 xfs_fsblock_t *firstblock, /* first-block-allocated */
658 struct xfs_defer_ops *dfops, /* blocks freed in xaction */
659 xfs_btree_cur_t **curp, /* cursor returned to caller */
660 int wasdel, /* converting a delayed alloc */
661 int *logflagsp, /* inode logging flags */
662 int whichfork) /* data or attr fork */
664 struct xfs_btree_block *ablock; /* allocated (child) bt block */
665 xfs_buf_t *abp; /* buffer for ablock */
666 xfs_alloc_arg_t args; /* allocation arguments */
667 xfs_bmbt_rec_t *arp; /* child record pointer */
668 struct xfs_btree_block *block; /* btree root block */
669 xfs_btree_cur_t *cur; /* bmap btree cursor */
670 int error; /* error return value */
671 xfs_ifork_t *ifp; /* inode fork pointer */
672 xfs_bmbt_key_t *kp; /* root block key pointer */
673 xfs_mount_t *mp; /* mount structure */
674 xfs_bmbt_ptr_t *pp; /* root block address pointer */
675 struct xfs_iext_cursor icur;
676 struct xfs_bmbt_irec rec;
677 xfs_extnum_t cnt = 0;
680 ASSERT(whichfork != XFS_COW_FORK);
681 ifp = XFS_IFORK_PTR(ip, whichfork);
682 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS);
685 * Make space in the inode incore.
687 xfs_iroot_realloc(ip, 1, whichfork);
688 ifp->if_flags |= XFS_IFBROOT;
693 block = ifp->if_broot;
694 xfs_btree_init_block_int(mp, block, XFS_BUF_DADDR_NULL,
695 XFS_BTNUM_BMAP, 1, 1, ip->i_ino,
696 XFS_BTREE_LONG_PTRS);
698 * Need a cursor. Can't allocate until bb_level is filled in.
700 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
701 cur->bc_private.b.firstblock = *firstblock;
702 cur->bc_private.b.dfops = dfops;
703 cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
705 * Convert to a btree with two levels, one record in root.
707 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE);
708 memset(&args, 0, sizeof(args));
711 xfs_rmap_ino_bmbt_owner(&args.oinfo, ip->i_ino, whichfork);
712 args.firstblock = *firstblock;
713 if (*firstblock == NULLFSBLOCK) {
714 args.type = XFS_ALLOCTYPE_START_BNO;
715 args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino);
716 } else if (dfops->dop_low) {
717 args.type = XFS_ALLOCTYPE_START_BNO;
718 args.fsbno = *firstblock;
720 args.type = XFS_ALLOCTYPE_NEAR_BNO;
721 args.fsbno = *firstblock;
723 args.minlen = args.maxlen = args.prod = 1;
724 args.wasdel = wasdel;
726 if ((error = xfs_alloc_vextent(&args))) {
727 xfs_iroot_realloc(ip, -1, whichfork);
728 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
732 if (WARN_ON_ONCE(args.fsbno == NULLFSBLOCK)) {
733 xfs_iroot_realloc(ip, -1, whichfork);
734 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
738 * Allocation can't fail, the space was reserved.
740 ASSERT(*firstblock == NULLFSBLOCK ||
741 args.agno >= XFS_FSB_TO_AGNO(mp, *firstblock));
742 *firstblock = cur->bc_private.b.firstblock = args.fsbno;
743 cur->bc_private.b.allocated++;
744 ip->i_d.di_nblocks++;
745 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
746 abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0);
748 * Fill in the child block.
750 abp->b_ops = &xfs_bmbt_buf_ops;
751 ablock = XFS_BUF_TO_BLOCK(abp);
752 xfs_btree_init_block_int(mp, ablock, abp->b_bn,
753 XFS_BTNUM_BMAP, 0, 0, ip->i_ino,
754 XFS_BTREE_LONG_PTRS);
756 for_each_xfs_iext(ifp, &icur, &rec) {
757 if (isnullstartblock(rec.br_startblock))
759 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1 + cnt);
760 xfs_bmbt_disk_set_all(arp, &rec);
763 ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork));
764 xfs_btree_set_numrecs(ablock, cnt);
767 * Fill in the root key and pointer.
769 kp = XFS_BMBT_KEY_ADDR(mp, block, 1);
770 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
771 kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp));
772 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur,
773 be16_to_cpu(block->bb_level)));
774 *pp = cpu_to_be64(args.fsbno);
777 * Do all this logging at the end so that
778 * the root is at the right level.
780 xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS);
781 xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs));
782 ASSERT(*curp == NULL);
784 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork);
789 * Convert a local file to an extents file.
790 * This code is out of bounds for data forks of regular files,
791 * since the file data needs to get logged so things will stay consistent.
792 * (The bmap-level manipulations are ok, though).
795 xfs_bmap_local_to_extents_empty(
796 struct xfs_inode *ip,
799 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
801 ASSERT(whichfork != XFS_COW_FORK);
802 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
803 ASSERT(ifp->if_bytes == 0);
804 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0);
806 xfs_bmap_forkoff_reset(ip, whichfork);
807 ifp->if_flags &= ~XFS_IFINLINE;
808 ifp->if_flags |= XFS_IFEXTENTS;
809 ifp->if_u1.if_root = NULL;
811 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
815 STATIC int /* error */
816 xfs_bmap_local_to_extents(
817 xfs_trans_t *tp, /* transaction pointer */
818 xfs_inode_t *ip, /* incore inode pointer */
819 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
820 xfs_extlen_t total, /* total blocks needed by transaction */
821 int *logflagsp, /* inode logging flags */
823 void (*init_fn)(struct xfs_trans *tp,
825 struct xfs_inode *ip,
826 struct xfs_ifork *ifp))
829 int flags; /* logging flags returned */
830 xfs_ifork_t *ifp; /* inode fork pointer */
831 xfs_alloc_arg_t args; /* allocation arguments */
832 xfs_buf_t *bp; /* buffer for extent block */
833 struct xfs_bmbt_irec rec;
834 struct xfs_iext_cursor icur;
837 * We don't want to deal with the case of keeping inode data inline yet.
838 * So sending the data fork of a regular inode is invalid.
840 ASSERT(!(S_ISREG(VFS_I(ip)->i_mode) && whichfork == XFS_DATA_FORK));
841 ifp = XFS_IFORK_PTR(ip, whichfork);
842 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
844 if (!ifp->if_bytes) {
845 xfs_bmap_local_to_extents_empty(ip, whichfork);
846 flags = XFS_ILOG_CORE;
852 ASSERT((ifp->if_flags & (XFS_IFINLINE|XFS_IFEXTENTS)) == XFS_IFINLINE);
853 memset(&args, 0, sizeof(args));
855 args.mp = ip->i_mount;
856 xfs_rmap_ino_owner(&args.oinfo, ip->i_ino, whichfork, 0);
857 args.firstblock = *firstblock;
859 * Allocate a block. We know we need only one, since the
860 * file currently fits in an inode.
862 if (*firstblock == NULLFSBLOCK) {
863 args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino);
864 args.type = XFS_ALLOCTYPE_START_BNO;
866 args.fsbno = *firstblock;
867 args.type = XFS_ALLOCTYPE_NEAR_BNO;
870 args.minlen = args.maxlen = args.prod = 1;
871 error = xfs_alloc_vextent(&args);
875 /* Can't fail, the space was reserved. */
876 ASSERT(args.fsbno != NULLFSBLOCK);
877 ASSERT(args.len == 1);
878 *firstblock = args.fsbno;
879 bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0);
882 * Initialize the block, copy the data and log the remote buffer.
884 * The callout is responsible for logging because the remote format
885 * might differ from the local format and thus we don't know how much to
886 * log here. Note that init_fn must also set the buffer log item type
889 init_fn(tp, bp, ip, ifp);
891 /* account for the change in fork size */
892 xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
893 xfs_bmap_local_to_extents_empty(ip, whichfork);
894 flags |= XFS_ILOG_CORE;
896 ifp->if_u1.if_root = NULL;
900 rec.br_startblock = args.fsbno;
901 rec.br_blockcount = 1;
902 rec.br_state = XFS_EXT_NORM;
903 xfs_iext_first(ifp, &icur);
904 xfs_iext_insert(ip, &icur, &rec, 0);
906 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
907 ip->i_d.di_nblocks = 1;
908 xfs_trans_mod_dquot_byino(tp, ip,
909 XFS_TRANS_DQ_BCOUNT, 1L);
910 flags |= xfs_ilog_fext(whichfork);
918 * Called from xfs_bmap_add_attrfork to handle btree format files.
920 STATIC int /* error */
921 xfs_bmap_add_attrfork_btree(
922 xfs_trans_t *tp, /* transaction pointer */
923 xfs_inode_t *ip, /* incore inode pointer */
924 xfs_fsblock_t *firstblock, /* first block allocated */
925 struct xfs_defer_ops *dfops, /* blocks to free at commit */
926 int *flags) /* inode logging flags */
928 xfs_btree_cur_t *cur; /* btree cursor */
929 int error; /* error return value */
930 xfs_mount_t *mp; /* file system mount struct */
931 int stat; /* newroot status */
934 if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip))
935 *flags |= XFS_ILOG_DBROOT;
937 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
938 cur->bc_private.b.dfops = dfops;
939 cur->bc_private.b.firstblock = *firstblock;
940 error = xfs_bmbt_lookup_first(cur, &stat);
943 /* must be at least one entry */
944 XFS_WANT_CORRUPTED_GOTO(mp, stat == 1, error0);
945 if ((error = xfs_btree_new_iroot(cur, flags, &stat)))
948 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
951 *firstblock = cur->bc_private.b.firstblock;
952 cur->bc_private.b.allocated = 0;
953 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
957 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
962 * Called from xfs_bmap_add_attrfork to handle extents format files.
964 STATIC int /* error */
965 xfs_bmap_add_attrfork_extents(
966 xfs_trans_t *tp, /* transaction pointer */
967 xfs_inode_t *ip, /* incore inode pointer */
968 xfs_fsblock_t *firstblock, /* first block allocated */
969 struct xfs_defer_ops *dfops, /* blocks to free at commit */
970 int *flags) /* inode logging flags */
972 xfs_btree_cur_t *cur; /* bmap btree cursor */
973 int error; /* error return value */
975 if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip))
978 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, dfops, &cur, 0,
979 flags, XFS_DATA_FORK);
981 cur->bc_private.b.allocated = 0;
982 xfs_btree_del_cursor(cur,
983 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
989 * Called from xfs_bmap_add_attrfork to handle local format files. Each
990 * different data fork content type needs a different callout to do the
991 * conversion. Some are basic and only require special block initialisation
992 * callouts for the data formating, others (directories) are so specialised they
993 * handle everything themselves.
995 * XXX (dgc): investigate whether directory conversion can use the generic
996 * formatting callout. It should be possible - it's just a very complex
999 STATIC int /* error */
1000 xfs_bmap_add_attrfork_local(
1001 xfs_trans_t *tp, /* transaction pointer */
1002 xfs_inode_t *ip, /* incore inode pointer */
1003 xfs_fsblock_t *firstblock, /* first block allocated */
1004 struct xfs_defer_ops *dfops, /* blocks to free at commit */
1005 int *flags) /* inode logging flags */
1007 xfs_da_args_t dargs; /* args for dir/attr code */
1009 if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip))
1012 if (S_ISDIR(VFS_I(ip)->i_mode)) {
1013 memset(&dargs, 0, sizeof(dargs));
1014 dargs.geo = ip->i_mount->m_dir_geo;
1016 dargs.firstblock = firstblock;
1017 dargs.dfops = dfops;
1018 dargs.total = dargs.geo->fsbcount;
1019 dargs.whichfork = XFS_DATA_FORK;
1021 return xfs_dir2_sf_to_block(&dargs);
1024 if (S_ISLNK(VFS_I(ip)->i_mode))
1025 return xfs_bmap_local_to_extents(tp, ip, firstblock, 1,
1026 flags, XFS_DATA_FORK,
1027 xfs_symlink_local_to_remote);
1029 /* should only be called for types that support local format data */
1031 return -EFSCORRUPTED;
1035 * Convert inode from non-attributed to attributed.
1036 * Must not be in a transaction, ip must not be locked.
1038 int /* error code */
1039 xfs_bmap_add_attrfork(
1040 xfs_inode_t *ip, /* incore inode pointer */
1041 int size, /* space new attribute needs */
1042 int rsvd) /* xact may use reserved blks */
1044 xfs_fsblock_t firstblock; /* 1st block/ag allocated */
1045 struct xfs_defer_ops dfops; /* freed extent records */
1046 xfs_mount_t *mp; /* mount structure */
1047 xfs_trans_t *tp; /* transaction pointer */
1048 int blks; /* space reservation */
1049 int version = 1; /* superblock attr version */
1050 int logflags; /* logging flags */
1051 int error; /* error return value */
1053 ASSERT(XFS_IFORK_Q(ip) == 0);
1056 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
1058 blks = XFS_ADDAFORK_SPACE_RES(mp);
1060 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_addafork, blks, 0,
1061 rsvd ? XFS_TRANS_RESERVE : 0, &tp);
1065 xfs_ilock(ip, XFS_ILOCK_EXCL);
1066 error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ?
1067 XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
1068 XFS_QMOPT_RES_REGBLKS);
1071 if (XFS_IFORK_Q(ip))
1073 if (ip->i_d.di_anextents != 0) {
1074 error = -EFSCORRUPTED;
1077 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) {
1079 * For inodes coming from pre-6.2 filesystems.
1081 ASSERT(ip->i_d.di_aformat == 0);
1082 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
1085 xfs_trans_ijoin(tp, ip, 0);
1086 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1088 switch (ip->i_d.di_format) {
1089 case XFS_DINODE_FMT_DEV:
1090 ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
1092 case XFS_DINODE_FMT_LOCAL:
1093 case XFS_DINODE_FMT_EXTENTS:
1094 case XFS_DINODE_FMT_BTREE:
1095 ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size);
1096 if (!ip->i_d.di_forkoff)
1097 ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3;
1098 else if (mp->m_flags & XFS_MOUNT_ATTR2)
1107 ASSERT(ip->i_afp == NULL);
1108 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP);
1109 ip->i_afp->if_flags = XFS_IFEXTENTS;
1111 xfs_defer_init(&dfops, &firstblock);
1112 switch (ip->i_d.di_format) {
1113 case XFS_DINODE_FMT_LOCAL:
1114 error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &dfops,
1117 case XFS_DINODE_FMT_EXTENTS:
1118 error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock,
1121 case XFS_DINODE_FMT_BTREE:
1122 error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &dfops,
1130 xfs_trans_log_inode(tp, ip, logflags);
1133 if (!xfs_sb_version_hasattr(&mp->m_sb) ||
1134 (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) {
1135 bool log_sb = false;
1137 spin_lock(&mp->m_sb_lock);
1138 if (!xfs_sb_version_hasattr(&mp->m_sb)) {
1139 xfs_sb_version_addattr(&mp->m_sb);
1142 if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) {
1143 xfs_sb_version_addattr2(&mp->m_sb);
1146 spin_unlock(&mp->m_sb_lock);
1151 error = xfs_defer_finish(&tp, &dfops);
1154 error = xfs_trans_commit(tp);
1155 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1159 xfs_defer_cancel(&dfops);
1161 xfs_trans_cancel(tp);
1162 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1167 * Internal and external extent tree search functions.
1171 * Read in extents from a btree-format inode.
1175 struct xfs_trans *tp,
1176 struct xfs_inode *ip,
1179 struct xfs_mount *mp = ip->i_mount;
1180 int state = xfs_bmap_fork_to_state(whichfork);
1181 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1182 xfs_extnum_t nextents = XFS_IFORK_NEXTENTS(ip, whichfork);
1183 struct xfs_btree_block *block = ifp->if_broot;
1184 struct xfs_iext_cursor icur;
1185 struct xfs_bmbt_irec new;
1193 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
1195 if (unlikely(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
1196 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp);
1197 return -EFSCORRUPTED;
1201 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
1203 level = be16_to_cpu(block->bb_level);
1205 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
1206 bno = be64_to_cpu(*pp);
1209 * Go down the tree until leaf level is reached, following the first
1210 * pointer (leftmost) at each level.
1212 while (level-- > 0) {
1213 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
1214 XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
1217 block = XFS_BUF_TO_BLOCK(bp);
1220 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
1221 bno = be64_to_cpu(*pp);
1222 XFS_WANT_CORRUPTED_GOTO(mp,
1223 xfs_verify_fsbno(mp, bno), out_brelse);
1224 xfs_trans_brelse(tp, bp);
1228 * Here with bp and block set to the leftmost leaf node in the tree.
1231 xfs_iext_first(ifp, &icur);
1234 * Loop over all leaf nodes. Copy information to the extent records.
1237 xfs_bmbt_rec_t *frp;
1238 xfs_fsblock_t nextbno;
1239 xfs_extnum_t num_recs;
1241 num_recs = xfs_btree_get_numrecs(block);
1242 if (unlikely(i + num_recs > nextents)) {
1243 ASSERT(i + num_recs <= nextents);
1244 xfs_warn(ip->i_mount,
1245 "corrupt dinode %Lu, (btree extents).",
1246 (unsigned long long) ip->i_ino);
1247 XFS_CORRUPTION_ERROR(__func__,
1248 XFS_ERRLEVEL_LOW, ip->i_mount, block);
1249 error = -EFSCORRUPTED;
1253 * Read-ahead the next leaf block, if any.
1255 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
1256 if (nextbno != NULLFSBLOCK)
1257 xfs_btree_reada_bufl(mp, nextbno, 1,
1260 * Copy records into the extent records.
1262 frp = XFS_BMBT_REC_ADDR(mp, block, 1);
1263 for (j = 0; j < num_recs; j++, frp++, i++) {
1264 xfs_bmbt_disk_get_all(frp, &new);
1265 if (!xfs_bmbt_validate_extent(mp, whichfork, &new)) {
1266 XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
1267 XFS_ERRLEVEL_LOW, mp);
1268 error = -EFSCORRUPTED;
1271 xfs_iext_insert(ip, &icur, &new, state);
1272 trace_xfs_read_extent(ip, &icur, state, _THIS_IP_);
1273 xfs_iext_next(ifp, &icur);
1275 xfs_trans_brelse(tp, bp);
1278 * If we've reached the end, stop.
1280 if (bno == NULLFSBLOCK)
1282 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
1283 XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
1286 block = XFS_BUF_TO_BLOCK(bp);
1289 if (i != XFS_IFORK_NEXTENTS(ip, whichfork)) {
1290 error = -EFSCORRUPTED;
1293 ASSERT(i == xfs_iext_count(ifp));
1295 ifp->if_flags |= XFS_IFEXTENTS;
1299 xfs_trans_brelse(tp, bp);
1301 xfs_iext_destroy(ifp);
1306 * Returns the relative block number of the first unused block(s) in the given
1307 * fork with at least "len" logically contiguous blocks free. This is the
1308 * lowest-address hole if the fork has holes, else the first block past the end
1309 * of fork. Return 0 if the fork is currently local (in-inode).
1312 xfs_bmap_first_unused(
1313 struct xfs_trans *tp, /* transaction pointer */
1314 struct xfs_inode *ip, /* incore inode */
1315 xfs_extlen_t len, /* size of hole to find */
1316 xfs_fileoff_t *first_unused, /* unused block */
1317 int whichfork) /* data or attr fork */
1319 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1320 struct xfs_bmbt_irec got;
1321 struct xfs_iext_cursor icur;
1322 xfs_fileoff_t lastaddr = 0;
1323 xfs_fileoff_t lowest, max;
1326 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE ||
1327 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ||
1328 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
1330 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
1335 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1336 error = xfs_iread_extents(tp, ip, whichfork);
1341 lowest = max = *first_unused;
1342 for_each_xfs_iext(ifp, &icur, &got) {
1344 * See if the hole before this extent will work.
1346 if (got.br_startoff >= lowest + len &&
1347 got.br_startoff - max >= len)
1349 lastaddr = got.br_startoff + got.br_blockcount;
1350 max = XFS_FILEOFF_MAX(lastaddr, lowest);
1353 *first_unused = max;
1358 * Returns the file-relative block number of the last block - 1 before
1359 * last_block (input value) in the file.
1360 * This is not based on i_size, it is based on the extent records.
1361 * Returns 0 for local files, as they do not have extent records.
1364 xfs_bmap_last_before(
1365 struct xfs_trans *tp, /* transaction pointer */
1366 struct xfs_inode *ip, /* incore inode */
1367 xfs_fileoff_t *last_block, /* last block */
1368 int whichfork) /* data or attr fork */
1370 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1371 struct xfs_bmbt_irec got;
1372 struct xfs_iext_cursor icur;
1375 switch (XFS_IFORK_FORMAT(ip, whichfork)) {
1376 case XFS_DINODE_FMT_LOCAL:
1379 case XFS_DINODE_FMT_BTREE:
1380 case XFS_DINODE_FMT_EXTENTS:
1386 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1387 error = xfs_iread_extents(tp, ip, whichfork);
1392 if (!xfs_iext_lookup_extent_before(ip, ifp, last_block, &icur, &got))
1398 xfs_bmap_last_extent(
1399 struct xfs_trans *tp,
1400 struct xfs_inode *ip,
1402 struct xfs_bmbt_irec *rec,
1405 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1406 struct xfs_iext_cursor icur;
1409 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1410 error = xfs_iread_extents(tp, ip, whichfork);
1415 xfs_iext_last(ifp, &icur);
1416 if (!xfs_iext_get_extent(ifp, &icur, rec))
1424 * Check the last inode extent to determine whether this allocation will result
1425 * in blocks being allocated at the end of the file. When we allocate new data
1426 * blocks at the end of the file which do not start at the previous data block,
1427 * we will try to align the new blocks at stripe unit boundaries.
1429 * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be
1430 * at, or past the EOF.
1434 struct xfs_bmalloca *bma,
1437 struct xfs_bmbt_irec rec;
1442 error = xfs_bmap_last_extent(NULL, bma->ip, whichfork, &rec,
1453 * Check if we are allocation or past the last extent, or at least into
1454 * the last delayed allocated extent.
1456 bma->aeof = bma->offset >= rec.br_startoff + rec.br_blockcount ||
1457 (bma->offset >= rec.br_startoff &&
1458 isnullstartblock(rec.br_startblock));
1463 * Returns the file-relative block number of the first block past eof in
1464 * the file. This is not based on i_size, it is based on the extent records.
1465 * Returns 0 for local files, as they do not have extent records.
1468 xfs_bmap_last_offset(
1469 struct xfs_inode *ip,
1470 xfs_fileoff_t *last_block,
1473 struct xfs_bmbt_irec rec;
1479 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL)
1482 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
1483 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
1486 error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, &is_empty);
1487 if (error || is_empty)
1490 *last_block = rec.br_startoff + rec.br_blockcount;
1495 * Returns whether the selected fork of the inode has exactly one
1496 * block or not. For the data fork we check this matches di_size,
1497 * implying the file's range is 0..bsize-1.
1499 int /* 1=>1 block, 0=>otherwise */
1501 xfs_inode_t *ip, /* incore inode */
1502 int whichfork) /* data or attr fork */
1504 xfs_ifork_t *ifp; /* inode fork pointer */
1505 int rval; /* return value */
1506 xfs_bmbt_irec_t s; /* internal version of extent */
1507 struct xfs_iext_cursor icur;
1510 if (whichfork == XFS_DATA_FORK)
1511 return XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize;
1513 if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1)
1515 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
1517 ifp = XFS_IFORK_PTR(ip, whichfork);
1518 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
1519 xfs_iext_first(ifp, &icur);
1520 xfs_iext_get_extent(ifp, &icur, &s);
1521 rval = s.br_startoff == 0 && s.br_blockcount == 1;
1522 if (rval && whichfork == XFS_DATA_FORK)
1523 ASSERT(XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize);
1528 * Extent tree manipulation functions used during allocation.
1532 * Convert a delayed allocation to a real allocation.
1534 STATIC int /* error */
1535 xfs_bmap_add_extent_delay_real(
1536 struct xfs_bmalloca *bma,
1539 struct xfs_bmbt_irec *new = &bma->got;
1540 int error; /* error return value */
1541 int i; /* temp state */
1542 xfs_ifork_t *ifp; /* inode fork pointer */
1543 xfs_fileoff_t new_endoff; /* end offset of new entry */
1544 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
1545 /* left is 0, right is 1, prev is 2 */
1546 int rval=0; /* return value (logging flags) */
1547 int state = xfs_bmap_fork_to_state(whichfork);
1548 xfs_filblks_t da_new; /* new count del alloc blocks used */
1549 xfs_filblks_t da_old; /* old count del alloc blocks used */
1550 xfs_filblks_t temp=0; /* value for da_new calculations */
1551 int tmp_rval; /* partial logging flags */
1552 struct xfs_mount *mp;
1553 xfs_extnum_t *nextents;
1554 struct xfs_bmbt_irec old;
1556 mp = bma->ip->i_mount;
1557 ifp = XFS_IFORK_PTR(bma->ip, whichfork);
1558 ASSERT(whichfork != XFS_ATTR_FORK);
1559 nextents = (whichfork == XFS_COW_FORK ? &bma->ip->i_cnextents :
1560 &bma->ip->i_d.di_nextents);
1562 ASSERT(!isnullstartblock(new->br_startblock));
1564 (bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
1566 XFS_STATS_INC(mp, xs_add_exlist);
1573 * Set up a bunch of variables to make the tests simpler.
1575 xfs_iext_get_extent(ifp, &bma->icur, &PREV);
1576 new_endoff = new->br_startoff + new->br_blockcount;
1577 ASSERT(isnullstartblock(PREV.br_startblock));
1578 ASSERT(PREV.br_startoff <= new->br_startoff);
1579 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
1581 da_old = startblockval(PREV.br_startblock);
1585 * Set flags determining what part of the previous delayed allocation
1586 * extent is being replaced by a real allocation.
1588 if (PREV.br_startoff == new->br_startoff)
1589 state |= BMAP_LEFT_FILLING;
1590 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
1591 state |= BMAP_RIGHT_FILLING;
1594 * Check and set flags if this segment has a left neighbor.
1595 * Don't set contiguous if the combined extent would be too large.
1597 if (xfs_iext_peek_prev_extent(ifp, &bma->icur, &LEFT)) {
1598 state |= BMAP_LEFT_VALID;
1599 if (isnullstartblock(LEFT.br_startblock))
1600 state |= BMAP_LEFT_DELAY;
1603 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1604 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1605 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
1606 LEFT.br_state == new->br_state &&
1607 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1608 state |= BMAP_LEFT_CONTIG;
1611 * Check and set flags if this segment has a right neighbor.
1612 * Don't set contiguous if the combined extent would be too large.
1613 * Also check for all-three-contiguous being too large.
1615 if (xfs_iext_peek_next_extent(ifp, &bma->icur, &RIGHT)) {
1616 state |= BMAP_RIGHT_VALID;
1617 if (isnullstartblock(RIGHT.br_startblock))
1618 state |= BMAP_RIGHT_DELAY;
1621 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1622 new_endoff == RIGHT.br_startoff &&
1623 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
1624 new->br_state == RIGHT.br_state &&
1625 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
1626 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1627 BMAP_RIGHT_FILLING)) !=
1628 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1629 BMAP_RIGHT_FILLING) ||
1630 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
1632 state |= BMAP_RIGHT_CONTIG;
1636 * Switch out based on the FILLING and CONTIG state bits.
1638 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1639 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
1640 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1641 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1643 * Filling in all of a previously delayed allocation extent.
1644 * The left and right neighbors are both contiguous with new.
1646 LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount;
1648 xfs_iext_remove(bma->ip, &bma->icur, state);
1649 xfs_iext_remove(bma->ip, &bma->icur, state);
1650 xfs_iext_prev(ifp, &bma->icur);
1651 xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT);
1654 if (bma->cur == NULL)
1655 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1657 rval = XFS_ILOG_CORE;
1658 error = xfs_bmbt_lookup_eq(bma->cur, &RIGHT, &i);
1661 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
1662 error = xfs_btree_delete(bma->cur, &i);
1665 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
1666 error = xfs_btree_decrement(bma->cur, 0, &i);
1669 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
1670 error = xfs_bmbt_update(bma->cur, &LEFT);
1676 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1678 * Filling in all of a previously delayed allocation extent.
1679 * The left neighbor is contiguous, the right is not.
1682 LEFT.br_blockcount += PREV.br_blockcount;
1684 xfs_iext_remove(bma->ip, &bma->icur, state);
1685 xfs_iext_prev(ifp, &bma->icur);
1686 xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT);
1688 if (bma->cur == NULL)
1689 rval = XFS_ILOG_DEXT;
1692 error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
1695 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
1696 error = xfs_bmbt_update(bma->cur, &LEFT);
1702 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1704 * Filling in all of a previously delayed allocation extent.
1705 * The right neighbor is contiguous, the left is not.
1707 PREV.br_startblock = new->br_startblock;
1708 PREV.br_blockcount += RIGHT.br_blockcount;
1710 xfs_iext_next(ifp, &bma->icur);
1711 xfs_iext_remove(bma->ip, &bma->icur, state);
1712 xfs_iext_prev(ifp, &bma->icur);
1713 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
1715 if (bma->cur == NULL)
1716 rval = XFS_ILOG_DEXT;
1719 error = xfs_bmbt_lookup_eq(bma->cur, &RIGHT, &i);
1722 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
1723 error = xfs_bmbt_update(bma->cur, &PREV);
1729 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
1731 * Filling in all of a previously delayed allocation extent.
1732 * Neither the left nor right neighbors are contiguous with
1735 PREV.br_startblock = new->br_startblock;
1736 PREV.br_state = new->br_state;
1737 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
1740 if (bma->cur == NULL)
1741 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1743 rval = XFS_ILOG_CORE;
1744 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
1747 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
1748 error = xfs_btree_insert(bma->cur, &i);
1751 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
1755 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
1757 * Filling in the first part of a previous delayed allocation.
1758 * The left neighbor is contiguous.
1761 temp = PREV.br_blockcount - new->br_blockcount;
1762 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
1763 startblockval(PREV.br_startblock));
1765 LEFT.br_blockcount += new->br_blockcount;
1767 PREV.br_blockcount = temp;
1768 PREV.br_startoff += new->br_blockcount;
1769 PREV.br_startblock = nullstartblock(da_new);
1771 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
1772 xfs_iext_prev(ifp, &bma->icur);
1773 xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT);
1775 if (bma->cur == NULL)
1776 rval = XFS_ILOG_DEXT;
1779 error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
1782 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
1783 error = xfs_bmbt_update(bma->cur, &LEFT);
1789 case BMAP_LEFT_FILLING:
1791 * Filling in the first part of a previous delayed allocation.
1792 * The left neighbor is not contiguous.
1794 xfs_iext_update_extent(bma->ip, state, &bma->icur, new);
1796 if (bma->cur == NULL)
1797 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1799 rval = XFS_ILOG_CORE;
1800 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
1803 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
1804 error = xfs_btree_insert(bma->cur, &i);
1807 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
1810 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
1811 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
1812 bma->firstblock, bma->dfops,
1813 &bma->cur, 1, &tmp_rval, whichfork);
1819 temp = PREV.br_blockcount - new->br_blockcount;
1820 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
1821 startblockval(PREV.br_startblock) -
1822 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
1824 PREV.br_startoff = new_endoff;
1825 PREV.br_blockcount = temp;
1826 PREV.br_startblock = nullstartblock(da_new);
1827 xfs_iext_next(ifp, &bma->icur);
1828 xfs_iext_insert(bma->ip, &bma->icur, &PREV, state);
1829 xfs_iext_prev(ifp, &bma->icur);
1832 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1834 * Filling in the last part of a previous delayed allocation.
1835 * The right neighbor is contiguous with the new allocation.
1838 RIGHT.br_startoff = new->br_startoff;
1839 RIGHT.br_startblock = new->br_startblock;
1840 RIGHT.br_blockcount += new->br_blockcount;
1842 if (bma->cur == NULL)
1843 rval = XFS_ILOG_DEXT;
1846 error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
1849 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
1850 error = xfs_bmbt_update(bma->cur, &RIGHT);
1855 temp = PREV.br_blockcount - new->br_blockcount;
1856 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
1857 startblockval(PREV.br_startblock));
1859 PREV.br_blockcount = temp;
1860 PREV.br_startblock = nullstartblock(da_new);
1862 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
1863 xfs_iext_next(ifp, &bma->icur);
1864 xfs_iext_update_extent(bma->ip, state, &bma->icur, &RIGHT);
1867 case BMAP_RIGHT_FILLING:
1869 * Filling in the last part of a previous delayed allocation.
1870 * The right neighbor is not contiguous.
1872 xfs_iext_update_extent(bma->ip, state, &bma->icur, new);
1874 if (bma->cur == NULL)
1875 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1877 rval = XFS_ILOG_CORE;
1878 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
1881 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
1882 error = xfs_btree_insert(bma->cur, &i);
1885 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
1888 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
1889 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
1890 bma->firstblock, bma->dfops, &bma->cur, 1,
1891 &tmp_rval, whichfork);
1897 temp = PREV.br_blockcount - new->br_blockcount;
1898 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
1899 startblockval(PREV.br_startblock) -
1900 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
1902 PREV.br_startblock = nullstartblock(da_new);
1903 PREV.br_blockcount = temp;
1904 xfs_iext_insert(bma->ip, &bma->icur, &PREV, state);
1905 xfs_iext_next(ifp, &bma->icur);
1910 * Filling in the middle part of a previous delayed allocation.
1911 * Contiguity is impossible here.
1912 * This case is avoided almost all the time.
1914 * We start with a delayed allocation:
1916 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
1919 * and we are allocating:
1920 * +rrrrrrrrrrrrrrrrr+
1923 * and we set it up for insertion as:
1924 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
1926 * PREV @ idx LEFT RIGHT
1927 * inserted at idx + 1
1931 /* LEFT is the new middle */
1934 /* RIGHT is the new right */
1935 RIGHT.br_state = PREV.br_state;
1936 RIGHT.br_startoff = new_endoff;
1937 RIGHT.br_blockcount =
1938 PREV.br_startoff + PREV.br_blockcount - new_endoff;
1939 RIGHT.br_startblock =
1940 nullstartblock(xfs_bmap_worst_indlen(bma->ip,
1941 RIGHT.br_blockcount));
1944 PREV.br_blockcount = new->br_startoff - PREV.br_startoff;
1945 PREV.br_startblock =
1946 nullstartblock(xfs_bmap_worst_indlen(bma->ip,
1947 PREV.br_blockcount));
1948 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
1950 xfs_iext_next(ifp, &bma->icur);
1951 xfs_iext_insert(bma->ip, &bma->icur, &RIGHT, state);
1952 xfs_iext_insert(bma->ip, &bma->icur, &LEFT, state);
1955 if (bma->cur == NULL)
1956 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1958 rval = XFS_ILOG_CORE;
1959 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
1962 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
1963 error = xfs_btree_insert(bma->cur, &i);
1966 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
1969 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
1970 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
1971 bma->firstblock, bma->dfops, &bma->cur,
1972 1, &tmp_rval, whichfork);
1978 da_new = startblockval(PREV.br_startblock) +
1979 startblockval(RIGHT.br_startblock);
1982 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1983 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1984 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
1985 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1986 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1987 case BMAP_LEFT_CONTIG:
1988 case BMAP_RIGHT_CONTIG:
1990 * These cases are all impossible.
1995 /* add reverse mapping */
1996 error = xfs_rmap_map_extent(mp, bma->dfops, bma->ip, whichfork, new);
2000 /* convert to a btree if necessary */
2001 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
2002 int tmp_logflags; /* partial log flag return val */
2004 ASSERT(bma->cur == NULL);
2005 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
2006 bma->firstblock, bma->dfops, &bma->cur,
2007 da_old > 0, &tmp_logflags, whichfork);
2008 bma->logflags |= tmp_logflags;
2014 da_new += bma->cur->bc_private.b.allocated;
2015 bma->cur->bc_private.b.allocated = 0;
2018 /* adjust for changes in reserved delayed indirect blocks */
2019 if (da_new != da_old) {
2020 ASSERT(state == 0 || da_new < da_old);
2021 error = xfs_mod_fdblocks(mp, (int64_t)(da_old - da_new),
2025 xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork);
2027 if (whichfork != XFS_COW_FORK)
2028 bma->logflags |= rval;
2036 * Convert an unwritten allocation to a real allocation or vice versa.
2038 STATIC int /* error */
2039 xfs_bmap_add_extent_unwritten_real(
2040 struct xfs_trans *tp,
2041 xfs_inode_t *ip, /* incore inode pointer */
2043 struct xfs_iext_cursor *icur,
2044 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
2045 xfs_bmbt_irec_t *new, /* new data to add to file extents */
2046 xfs_fsblock_t *first, /* pointer to firstblock variable */
2047 struct xfs_defer_ops *dfops, /* list of extents to be freed */
2048 int *logflagsp) /* inode logging flags */
2050 xfs_btree_cur_t *cur; /* btree cursor */
2051 int error; /* error return value */
2052 int i; /* temp state */
2053 xfs_ifork_t *ifp; /* inode fork pointer */
2054 xfs_fileoff_t new_endoff; /* end offset of new entry */
2055 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
2056 /* left is 0, right is 1, prev is 2 */
2057 int rval=0; /* return value (logging flags) */
2058 int state = xfs_bmap_fork_to_state(whichfork);
2059 struct xfs_mount *mp = ip->i_mount;
2060 struct xfs_bmbt_irec old;
2065 ifp = XFS_IFORK_PTR(ip, whichfork);
2067 ASSERT(!isnullstartblock(new->br_startblock));
2069 XFS_STATS_INC(mp, xs_add_exlist);
2076 * Set up a bunch of variables to make the tests simpler.
2079 xfs_iext_get_extent(ifp, icur, &PREV);
2080 ASSERT(new->br_state != PREV.br_state);
2081 new_endoff = new->br_startoff + new->br_blockcount;
2082 ASSERT(PREV.br_startoff <= new->br_startoff);
2083 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
2086 * Set flags determining what part of the previous oldext allocation
2087 * extent is being replaced by a newext allocation.
2089 if (PREV.br_startoff == new->br_startoff)
2090 state |= BMAP_LEFT_FILLING;
2091 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
2092 state |= BMAP_RIGHT_FILLING;
2095 * Check and set flags if this segment has a left neighbor.
2096 * Don't set contiguous if the combined extent would be too large.
2098 if (xfs_iext_peek_prev_extent(ifp, icur, &LEFT)) {
2099 state |= BMAP_LEFT_VALID;
2100 if (isnullstartblock(LEFT.br_startblock))
2101 state |= BMAP_LEFT_DELAY;
2104 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2105 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
2106 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
2107 LEFT.br_state == new->br_state &&
2108 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2109 state |= BMAP_LEFT_CONTIG;
2112 * Check and set flags if this segment has a right neighbor.
2113 * Don't set contiguous if the combined extent would be too large.
2114 * Also check for all-three-contiguous being too large.
2116 if (xfs_iext_peek_next_extent(ifp, icur, &RIGHT)) {
2117 state |= BMAP_RIGHT_VALID;
2118 if (isnullstartblock(RIGHT.br_startblock))
2119 state |= BMAP_RIGHT_DELAY;
2122 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2123 new_endoff == RIGHT.br_startoff &&
2124 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
2125 new->br_state == RIGHT.br_state &&
2126 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
2127 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2128 BMAP_RIGHT_FILLING)) !=
2129 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2130 BMAP_RIGHT_FILLING) ||
2131 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
2133 state |= BMAP_RIGHT_CONTIG;
2136 * Switch out based on the FILLING and CONTIG state bits.
2138 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2139 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
2140 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2141 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
2143 * Setting all of a previous oldext extent to newext.
2144 * The left and right neighbors are both contiguous with new.
2146 LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount;
2148 xfs_iext_remove(ip, icur, state);
2149 xfs_iext_remove(ip, icur, state);
2150 xfs_iext_prev(ifp, icur);
2151 xfs_iext_update_extent(ip, state, icur, &LEFT);
2152 XFS_IFORK_NEXT_SET(ip, whichfork,
2153 XFS_IFORK_NEXTENTS(ip, whichfork) - 2);
2155 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2157 rval = XFS_ILOG_CORE;
2158 error = xfs_bmbt_lookup_eq(cur, &RIGHT, &i);
2161 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2162 if ((error = xfs_btree_delete(cur, &i)))
2164 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2165 if ((error = xfs_btree_decrement(cur, 0, &i)))
2167 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2168 if ((error = xfs_btree_delete(cur, &i)))
2170 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2171 if ((error = xfs_btree_decrement(cur, 0, &i)))
2173 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2174 error = xfs_bmbt_update(cur, &LEFT);
2180 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2182 * Setting all of a previous oldext extent to newext.
2183 * The left neighbor is contiguous, the right is not.
2185 LEFT.br_blockcount += PREV.br_blockcount;
2187 xfs_iext_remove(ip, icur, state);
2188 xfs_iext_prev(ifp, icur);
2189 xfs_iext_update_extent(ip, state, icur, &LEFT);
2190 XFS_IFORK_NEXT_SET(ip, whichfork,
2191 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
2193 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2195 rval = XFS_ILOG_CORE;
2196 error = xfs_bmbt_lookup_eq(cur, &PREV, &i);
2199 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2200 if ((error = xfs_btree_delete(cur, &i)))
2202 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2203 if ((error = xfs_btree_decrement(cur, 0, &i)))
2205 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2206 error = xfs_bmbt_update(cur, &LEFT);
2212 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
2214 * Setting all of a previous oldext extent to newext.
2215 * The right neighbor is contiguous, the left is not.
2217 PREV.br_blockcount += RIGHT.br_blockcount;
2218 PREV.br_state = new->br_state;
2220 xfs_iext_next(ifp, icur);
2221 xfs_iext_remove(ip, icur, state);
2222 xfs_iext_prev(ifp, icur);
2223 xfs_iext_update_extent(ip, state, icur, &PREV);
2225 XFS_IFORK_NEXT_SET(ip, whichfork,
2226 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
2228 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2230 rval = XFS_ILOG_CORE;
2231 error = xfs_bmbt_lookup_eq(cur, &RIGHT, &i);
2234 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2235 if ((error = xfs_btree_delete(cur, &i)))
2237 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2238 if ((error = xfs_btree_decrement(cur, 0, &i)))
2240 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2241 error = xfs_bmbt_update(cur, &PREV);
2247 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
2249 * Setting all of a previous oldext extent to newext.
2250 * Neither the left nor right neighbors are contiguous with
2253 PREV.br_state = new->br_state;
2254 xfs_iext_update_extent(ip, state, icur, &PREV);
2257 rval = XFS_ILOG_DEXT;
2260 error = xfs_bmbt_lookup_eq(cur, new, &i);
2263 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2264 error = xfs_bmbt_update(cur, &PREV);
2270 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
2272 * Setting the first part of a previous oldext extent to newext.
2273 * The left neighbor is contiguous.
2275 LEFT.br_blockcount += new->br_blockcount;
2278 PREV.br_startoff += new->br_blockcount;
2279 PREV.br_startblock += new->br_blockcount;
2280 PREV.br_blockcount -= new->br_blockcount;
2282 xfs_iext_update_extent(ip, state, icur, &PREV);
2283 xfs_iext_prev(ifp, icur);
2284 xfs_iext_update_extent(ip, state, icur, &LEFT);
2287 rval = XFS_ILOG_DEXT;
2290 error = xfs_bmbt_lookup_eq(cur, &old, &i);
2293 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2294 error = xfs_bmbt_update(cur, &PREV);
2297 error = xfs_btree_decrement(cur, 0, &i);
2300 error = xfs_bmbt_update(cur, &LEFT);
2306 case BMAP_LEFT_FILLING:
2308 * Setting the first part of a previous oldext extent to newext.
2309 * The left neighbor is not contiguous.
2312 PREV.br_startoff += new->br_blockcount;
2313 PREV.br_startblock += new->br_blockcount;
2314 PREV.br_blockcount -= new->br_blockcount;
2316 xfs_iext_update_extent(ip, state, icur, &PREV);
2317 xfs_iext_insert(ip, icur, new, state);
2318 XFS_IFORK_NEXT_SET(ip, whichfork,
2319 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
2321 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2323 rval = XFS_ILOG_CORE;
2324 error = xfs_bmbt_lookup_eq(cur, &old, &i);
2327 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2328 error = xfs_bmbt_update(cur, &PREV);
2331 cur->bc_rec.b = *new;
2332 if ((error = xfs_btree_insert(cur, &i)))
2334 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2338 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
2340 * Setting the last part of a previous oldext extent to newext.
2341 * The right neighbor is contiguous with the new allocation.
2344 PREV.br_blockcount -= new->br_blockcount;
2346 RIGHT.br_startoff = new->br_startoff;
2347 RIGHT.br_startblock = new->br_startblock;
2348 RIGHT.br_blockcount += new->br_blockcount;
2350 xfs_iext_update_extent(ip, state, icur, &PREV);
2351 xfs_iext_next(ifp, icur);
2352 xfs_iext_update_extent(ip, state, icur, &RIGHT);
2355 rval = XFS_ILOG_DEXT;
2358 error = xfs_bmbt_lookup_eq(cur, &old, &i);
2361 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2362 error = xfs_bmbt_update(cur, &PREV);
2365 error = xfs_btree_increment(cur, 0, &i);
2368 error = xfs_bmbt_update(cur, &RIGHT);
2374 case BMAP_RIGHT_FILLING:
2376 * Setting the last part of a previous oldext extent to newext.
2377 * The right neighbor is not contiguous.
2380 PREV.br_blockcount -= new->br_blockcount;
2382 xfs_iext_update_extent(ip, state, icur, &PREV);
2383 xfs_iext_next(ifp, icur);
2384 xfs_iext_insert(ip, icur, new, state);
2386 XFS_IFORK_NEXT_SET(ip, whichfork,
2387 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
2389 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2391 rval = XFS_ILOG_CORE;
2392 error = xfs_bmbt_lookup_eq(cur, &old, &i);
2395 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2396 error = xfs_bmbt_update(cur, &PREV);
2399 error = xfs_bmbt_lookup_eq(cur, new, &i);
2402 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
2403 if ((error = xfs_btree_insert(cur, &i)))
2405 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2411 * Setting the middle part of a previous oldext extent to
2412 * newext. Contiguity is impossible here.
2413 * One extent becomes three extents.
2416 PREV.br_blockcount = new->br_startoff - PREV.br_startoff;
2419 r[1].br_startoff = new_endoff;
2420 r[1].br_blockcount =
2421 old.br_startoff + old.br_blockcount - new_endoff;
2422 r[1].br_startblock = new->br_startblock + new->br_blockcount;
2423 r[1].br_state = PREV.br_state;
2425 xfs_iext_update_extent(ip, state, icur, &PREV);
2426 xfs_iext_next(ifp, icur);
2427 xfs_iext_insert(ip, icur, &r[1], state);
2428 xfs_iext_insert(ip, icur, &r[0], state);
2430 XFS_IFORK_NEXT_SET(ip, whichfork,
2431 XFS_IFORK_NEXTENTS(ip, whichfork) + 2);
2433 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2435 rval = XFS_ILOG_CORE;
2436 error = xfs_bmbt_lookup_eq(cur, &old, &i);
2439 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2440 /* new right extent - oldext */
2441 error = xfs_bmbt_update(cur, &r[1]);
2444 /* new left extent - oldext */
2445 cur->bc_rec.b = PREV;
2446 if ((error = xfs_btree_insert(cur, &i)))
2448 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2450 * Reset the cursor to the position of the new extent
2451 * we are about to insert as we can't trust it after
2452 * the previous insert.
2454 error = xfs_bmbt_lookup_eq(cur, new, &i);
2457 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
2458 /* new middle extent - newext */
2459 if ((error = xfs_btree_insert(cur, &i)))
2461 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2465 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2466 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2467 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
2468 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2469 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2470 case BMAP_LEFT_CONTIG:
2471 case BMAP_RIGHT_CONTIG:
2473 * These cases are all impossible.
2478 /* update reverse mappings */
2479 error = xfs_rmap_convert_extent(mp, dfops, ip, whichfork, new);
2483 /* convert to a btree if necessary */
2484 if (xfs_bmap_needs_btree(ip, whichfork)) {
2485 int tmp_logflags; /* partial log flag return val */
2487 ASSERT(cur == NULL);
2488 error = xfs_bmap_extents_to_btree(tp, ip, first, dfops, &cur,
2489 0, &tmp_logflags, whichfork);
2490 *logflagsp |= tmp_logflags;
2495 /* clear out the allocated field, done with it now in any case. */
2497 cur->bc_private.b.allocated = 0;
2501 xfs_bmap_check_leaf_extents(*curp, ip, whichfork);
2511 * Convert a hole to a delayed allocation.
2514 xfs_bmap_add_extent_hole_delay(
2515 xfs_inode_t *ip, /* incore inode pointer */
2517 struct xfs_iext_cursor *icur,
2518 xfs_bmbt_irec_t *new) /* new data to add to file extents */
2520 xfs_ifork_t *ifp; /* inode fork pointer */
2521 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2522 xfs_filblks_t newlen=0; /* new indirect size */
2523 xfs_filblks_t oldlen=0; /* old indirect size */
2524 xfs_bmbt_irec_t right; /* right neighbor extent entry */
2525 int state = xfs_bmap_fork_to_state(whichfork);
2526 xfs_filblks_t temp; /* temp for indirect calculations */
2528 ifp = XFS_IFORK_PTR(ip, whichfork);
2529 ASSERT(isnullstartblock(new->br_startblock));
2532 * Check and set flags if this segment has a left neighbor
2534 if (xfs_iext_peek_prev_extent(ifp, icur, &left)) {
2535 state |= BMAP_LEFT_VALID;
2536 if (isnullstartblock(left.br_startblock))
2537 state |= BMAP_LEFT_DELAY;
2541 * Check and set flags if the current (right) segment exists.
2542 * If it doesn't exist, we're converting the hole at end-of-file.
2544 if (xfs_iext_get_extent(ifp, icur, &right)) {
2545 state |= BMAP_RIGHT_VALID;
2546 if (isnullstartblock(right.br_startblock))
2547 state |= BMAP_RIGHT_DELAY;
2551 * Set contiguity flags on the left and right neighbors.
2552 * Don't let extents get too large, even if the pieces are contiguous.
2554 if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) &&
2555 left.br_startoff + left.br_blockcount == new->br_startoff &&
2556 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2557 state |= BMAP_LEFT_CONTIG;
2559 if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) &&
2560 new->br_startoff + new->br_blockcount == right.br_startoff &&
2561 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2562 (!(state & BMAP_LEFT_CONTIG) ||
2563 (left.br_blockcount + new->br_blockcount +
2564 right.br_blockcount <= MAXEXTLEN)))
2565 state |= BMAP_RIGHT_CONTIG;
2568 * Switch out based on the contiguity flags.
2570 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2571 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2573 * New allocation is contiguous with delayed allocations
2574 * on the left and on the right.
2575 * Merge all three into a single extent record.
2577 temp = left.br_blockcount + new->br_blockcount +
2578 right.br_blockcount;
2580 oldlen = startblockval(left.br_startblock) +
2581 startblockval(new->br_startblock) +
2582 startblockval(right.br_startblock);
2583 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2585 left.br_startblock = nullstartblock(newlen);
2586 left.br_blockcount = temp;
2588 xfs_iext_remove(ip, icur, state);
2589 xfs_iext_prev(ifp, icur);
2590 xfs_iext_update_extent(ip, state, icur, &left);
2593 case BMAP_LEFT_CONTIG:
2595 * New allocation is contiguous with a delayed allocation
2597 * Merge the new allocation with the left neighbor.
2599 temp = left.br_blockcount + new->br_blockcount;
2601 oldlen = startblockval(left.br_startblock) +
2602 startblockval(new->br_startblock);
2603 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2605 left.br_blockcount = temp;
2606 left.br_startblock = nullstartblock(newlen);
2608 xfs_iext_prev(ifp, icur);
2609 xfs_iext_update_extent(ip, state, icur, &left);
2612 case BMAP_RIGHT_CONTIG:
2614 * New allocation is contiguous with a delayed allocation
2616 * Merge the new allocation with the right neighbor.
2618 temp = new->br_blockcount + right.br_blockcount;
2619 oldlen = startblockval(new->br_startblock) +
2620 startblockval(right.br_startblock);
2621 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2623 right.br_startoff = new->br_startoff;
2624 right.br_startblock = nullstartblock(newlen);
2625 right.br_blockcount = temp;
2626 xfs_iext_update_extent(ip, state, icur, &right);
2631 * New allocation is not contiguous with another
2632 * delayed allocation.
2633 * Insert a new entry.
2635 oldlen = newlen = 0;
2636 xfs_iext_insert(ip, icur, new, state);
2639 if (oldlen != newlen) {
2640 ASSERT(oldlen > newlen);
2641 xfs_mod_fdblocks(ip->i_mount, (int64_t)(oldlen - newlen),
2644 * Nothing to do for disk quota accounting here.
2650 * Convert a hole to a real allocation.
2652 STATIC int /* error */
2653 xfs_bmap_add_extent_hole_real(
2654 struct xfs_trans *tp,
2655 struct xfs_inode *ip,
2657 struct xfs_iext_cursor *icur,
2658 struct xfs_btree_cur **curp,
2659 struct xfs_bmbt_irec *new,
2660 xfs_fsblock_t *first,
2661 struct xfs_defer_ops *dfops,
2664 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
2665 struct xfs_mount *mp = ip->i_mount;
2666 struct xfs_btree_cur *cur = *curp;
2667 int error; /* error return value */
2668 int i; /* temp state */
2669 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2670 xfs_bmbt_irec_t right; /* right neighbor extent entry */
2671 int rval=0; /* return value (logging flags) */
2672 int state = xfs_bmap_fork_to_state(whichfork);
2673 struct xfs_bmbt_irec old;
2675 ASSERT(!isnullstartblock(new->br_startblock));
2676 ASSERT(!cur || !(cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
2678 XFS_STATS_INC(mp, xs_add_exlist);
2681 * Check and set flags if this segment has a left neighbor.
2683 if (xfs_iext_peek_prev_extent(ifp, icur, &left)) {
2684 state |= BMAP_LEFT_VALID;
2685 if (isnullstartblock(left.br_startblock))
2686 state |= BMAP_LEFT_DELAY;
2690 * Check and set flags if this segment has a current value.
2691 * Not true if we're inserting into the "hole" at eof.
2693 if (xfs_iext_get_extent(ifp, icur, &right)) {
2694 state |= BMAP_RIGHT_VALID;
2695 if (isnullstartblock(right.br_startblock))
2696 state |= BMAP_RIGHT_DELAY;
2700 * We're inserting a real allocation between "left" and "right".
2701 * Set the contiguity flags. Don't let extents get too large.
2703 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2704 left.br_startoff + left.br_blockcount == new->br_startoff &&
2705 left.br_startblock + left.br_blockcount == new->br_startblock &&
2706 left.br_state == new->br_state &&
2707 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2708 state |= BMAP_LEFT_CONTIG;
2710 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2711 new->br_startoff + new->br_blockcount == right.br_startoff &&
2712 new->br_startblock + new->br_blockcount == right.br_startblock &&
2713 new->br_state == right.br_state &&
2714 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2715 (!(state & BMAP_LEFT_CONTIG) ||
2716 left.br_blockcount + new->br_blockcount +
2717 right.br_blockcount <= MAXEXTLEN))
2718 state |= BMAP_RIGHT_CONTIG;
2722 * Select which case we're in here, and implement it.
2724 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2725 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2727 * New allocation is contiguous with real allocations on the
2728 * left and on the right.
2729 * Merge all three into a single extent record.
2731 left.br_blockcount += new->br_blockcount + right.br_blockcount;
2733 xfs_iext_remove(ip, icur, state);
2734 xfs_iext_prev(ifp, icur);
2735 xfs_iext_update_extent(ip, state, icur, &left);
2737 XFS_IFORK_NEXT_SET(ip, whichfork,
2738 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
2740 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
2742 rval = XFS_ILOG_CORE;
2743 error = xfs_bmbt_lookup_eq(cur, &right, &i);
2746 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2747 error = xfs_btree_delete(cur, &i);
2750 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2751 error = xfs_btree_decrement(cur, 0, &i);
2754 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2755 error = xfs_bmbt_update(cur, &left);
2761 case BMAP_LEFT_CONTIG:
2763 * New allocation is contiguous with a real allocation
2765 * Merge the new allocation with the left neighbor.
2768 left.br_blockcount += new->br_blockcount;
2770 xfs_iext_prev(ifp, icur);
2771 xfs_iext_update_extent(ip, state, icur, &left);
2774 rval = xfs_ilog_fext(whichfork);
2777 error = xfs_bmbt_lookup_eq(cur, &old, &i);
2780 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2781 error = xfs_bmbt_update(cur, &left);
2787 case BMAP_RIGHT_CONTIG:
2789 * New allocation is contiguous with a real allocation
2791 * Merge the new allocation with the right neighbor.
2795 right.br_startoff = new->br_startoff;
2796 right.br_startblock = new->br_startblock;
2797 right.br_blockcount += new->br_blockcount;
2798 xfs_iext_update_extent(ip, state, icur, &right);
2801 rval = xfs_ilog_fext(whichfork);
2804 error = xfs_bmbt_lookup_eq(cur, &old, &i);
2807 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2808 error = xfs_bmbt_update(cur, &right);
2816 * New allocation is not contiguous with another
2818 * Insert a new entry.
2820 xfs_iext_insert(ip, icur, new, state);
2821 XFS_IFORK_NEXT_SET(ip, whichfork,
2822 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
2824 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
2826 rval = XFS_ILOG_CORE;
2827 error = xfs_bmbt_lookup_eq(cur, new, &i);
2830 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
2831 error = xfs_btree_insert(cur, &i);
2834 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2839 /* add reverse mapping */
2840 error = xfs_rmap_map_extent(mp, dfops, ip, whichfork, new);
2844 /* convert to a btree if necessary */
2845 if (xfs_bmap_needs_btree(ip, whichfork)) {
2846 int tmp_logflags; /* partial log flag return val */
2848 ASSERT(cur == NULL);
2849 error = xfs_bmap_extents_to_btree(tp, ip, first, dfops, curp,
2850 0, &tmp_logflags, whichfork);
2851 *logflagsp |= tmp_logflags;
2857 /* clear out the allocated field, done with it now in any case. */
2859 cur->bc_private.b.allocated = 0;
2861 xfs_bmap_check_leaf_extents(cur, ip, whichfork);
2868 * Functions used in the extent read, allocate and remove paths
2872 * Adjust the size of the new extent based on di_extsize and rt extsize.
2875 xfs_bmap_extsize_align(
2877 xfs_bmbt_irec_t *gotp, /* next extent pointer */
2878 xfs_bmbt_irec_t *prevp, /* previous extent pointer */
2879 xfs_extlen_t extsz, /* align to this extent size */
2880 int rt, /* is this a realtime inode? */
2881 int eof, /* is extent at end-of-file? */
2882 int delay, /* creating delalloc extent? */
2883 int convert, /* overwriting unwritten extent? */
2884 xfs_fileoff_t *offp, /* in/out: aligned offset */
2885 xfs_extlen_t *lenp) /* in/out: aligned length */
2887 xfs_fileoff_t orig_off; /* original offset */
2888 xfs_extlen_t orig_alen; /* original length */
2889 xfs_fileoff_t orig_end; /* original off+len */
2890 xfs_fileoff_t nexto; /* next file offset */
2891 xfs_fileoff_t prevo; /* previous file offset */
2892 xfs_fileoff_t align_off; /* temp for offset */
2893 xfs_extlen_t align_alen; /* temp for length */
2894 xfs_extlen_t temp; /* temp for calculations */
2899 orig_off = align_off = *offp;
2900 orig_alen = align_alen = *lenp;
2901 orig_end = orig_off + orig_alen;
2904 * If this request overlaps an existing extent, then don't
2905 * attempt to perform any additional alignment.
2907 if (!delay && !eof &&
2908 (orig_off >= gotp->br_startoff) &&
2909 (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
2914 * If the file offset is unaligned vs. the extent size
2915 * we need to align it. This will be possible unless
2916 * the file was previously written with a kernel that didn't
2917 * perform this alignment, or if a truncate shot us in the
2920 temp = do_mod(orig_off, extsz);
2926 /* Same adjustment for the end of the requested area. */
2927 temp = (align_alen % extsz);
2929 align_alen += extsz - temp;
2932 * For large extent hint sizes, the aligned extent might be larger than
2933 * MAXEXTLEN. In that case, reduce the size by an extsz so that it pulls
2934 * the length back under MAXEXTLEN. The outer allocation loops handle
2935 * short allocation just fine, so it is safe to do this. We only want to
2936 * do it when we are forced to, though, because it means more allocation
2937 * operations are required.
2939 while (align_alen > MAXEXTLEN)
2940 align_alen -= extsz;
2941 ASSERT(align_alen <= MAXEXTLEN);
2944 * If the previous block overlaps with this proposed allocation
2945 * then move the start forward without adjusting the length.
2947 if (prevp->br_startoff != NULLFILEOFF) {
2948 if (prevp->br_startblock == HOLESTARTBLOCK)
2949 prevo = prevp->br_startoff;
2951 prevo = prevp->br_startoff + prevp->br_blockcount;
2954 if (align_off != orig_off && align_off < prevo)
2957 * If the next block overlaps with this proposed allocation
2958 * then move the start back without adjusting the length,
2959 * but not before offset 0.
2960 * This may of course make the start overlap previous block,
2961 * and if we hit the offset 0 limit then the next block
2962 * can still overlap too.
2964 if (!eof && gotp->br_startoff != NULLFILEOFF) {
2965 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
2966 (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
2967 nexto = gotp->br_startoff + gotp->br_blockcount;
2969 nexto = gotp->br_startoff;
2971 nexto = NULLFILEOFF;
2973 align_off + align_alen != orig_end &&
2974 align_off + align_alen > nexto)
2975 align_off = nexto > align_alen ? nexto - align_alen : 0;
2977 * If we're now overlapping the next or previous extent that
2978 * means we can't fit an extsz piece in this hole. Just move
2979 * the start forward to the first valid spot and set
2980 * the length so we hit the end.
2982 if (align_off != orig_off && align_off < prevo)
2984 if (align_off + align_alen != orig_end &&
2985 align_off + align_alen > nexto &&
2986 nexto != NULLFILEOFF) {
2987 ASSERT(nexto > prevo);
2988 align_alen = nexto - align_off;
2992 * If realtime, and the result isn't a multiple of the realtime
2993 * extent size we need to remove blocks until it is.
2995 if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
2997 * We're not covering the original request, or
2998 * we won't be able to once we fix the length.
3000 if (orig_off < align_off ||
3001 orig_end > align_off + align_alen ||
3002 align_alen - temp < orig_alen)
3005 * Try to fix it by moving the start up.
3007 if (align_off + temp <= orig_off) {
3012 * Try to fix it by moving the end in.
3014 else if (align_off + align_alen - temp >= orig_end)
3017 * Set the start to the minimum then trim the length.
3020 align_alen -= orig_off - align_off;
3021 align_off = orig_off;
3022 align_alen -= align_alen % mp->m_sb.sb_rextsize;
3025 * Result doesn't cover the request, fail it.
3027 if (orig_off < align_off || orig_end > align_off + align_alen)
3030 ASSERT(orig_off >= align_off);
3031 /* see MAXEXTLEN handling above */
3032 ASSERT(orig_end <= align_off + align_alen ||
3033 align_alen + extsz > MAXEXTLEN);
3037 if (!eof && gotp->br_startoff != NULLFILEOFF)
3038 ASSERT(align_off + align_alen <= gotp->br_startoff);
3039 if (prevp->br_startoff != NULLFILEOFF)
3040 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
3048 #define XFS_ALLOC_GAP_UNITS 4
3052 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
3054 xfs_fsblock_t adjust; /* adjustment to block numbers */
3055 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
3056 xfs_mount_t *mp; /* mount point structure */
3057 int nullfb; /* true if ap->firstblock isn't set */
3058 int rt; /* true if inode is realtime */
3060 #define ISVALID(x,y) \
3062 (x) < mp->m_sb.sb_rblocks : \
3063 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
3064 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
3065 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
3067 mp = ap->ip->i_mount;
3068 nullfb = *ap->firstblock == NULLFSBLOCK;
3069 rt = XFS_IS_REALTIME_INODE(ap->ip) &&
3070 xfs_alloc_is_userdata(ap->datatype);
3071 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
3073 * If allocating at eof, and there's a previous real block,
3074 * try to use its last block as our starting point.
3076 if (ap->eof && ap->prev.br_startoff != NULLFILEOFF &&
3077 !isnullstartblock(ap->prev.br_startblock) &&
3078 ISVALID(ap->prev.br_startblock + ap->prev.br_blockcount,
3079 ap->prev.br_startblock)) {
3080 ap->blkno = ap->prev.br_startblock + ap->prev.br_blockcount;
3082 * Adjust for the gap between prevp and us.
3084 adjust = ap->offset -
3085 (ap->prev.br_startoff + ap->prev.br_blockcount);
3087 ISVALID(ap->blkno + adjust, ap->prev.br_startblock))
3088 ap->blkno += adjust;
3091 * If not at eof, then compare the two neighbor blocks.
3092 * Figure out whether either one gives us a good starting point,
3093 * and pick the better one.
3095 else if (!ap->eof) {
3096 xfs_fsblock_t gotbno; /* right side block number */
3097 xfs_fsblock_t gotdiff=0; /* right side difference */
3098 xfs_fsblock_t prevbno; /* left side block number */
3099 xfs_fsblock_t prevdiff=0; /* left side difference */
3102 * If there's a previous (left) block, select a requested
3103 * start block based on it.
3105 if (ap->prev.br_startoff != NULLFILEOFF &&
3106 !isnullstartblock(ap->prev.br_startblock) &&
3107 (prevbno = ap->prev.br_startblock +
3108 ap->prev.br_blockcount) &&
3109 ISVALID(prevbno, ap->prev.br_startblock)) {
3111 * Calculate gap to end of previous block.
3113 adjust = prevdiff = ap->offset -
3114 (ap->prev.br_startoff +
3115 ap->prev.br_blockcount);
3117 * Figure the startblock based on the previous block's
3118 * end and the gap size.
3120 * If the gap is large relative to the piece we're
3121 * allocating, or using it gives us an invalid block
3122 * number, then just use the end of the previous block.
3124 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
3125 ISVALID(prevbno + prevdiff,
3126 ap->prev.br_startblock))
3131 * If the firstblock forbids it, can't use it,
3134 if (!rt && !nullfb &&
3135 XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
3136 prevbno = NULLFSBLOCK;
3139 * No previous block or can't follow it, just default.
3142 prevbno = NULLFSBLOCK;
3144 * If there's a following (right) block, select a requested
3145 * start block based on it.
3147 if (!isnullstartblock(ap->got.br_startblock)) {
3149 * Calculate gap to start of next block.
3151 adjust = gotdiff = ap->got.br_startoff - ap->offset;
3153 * Figure the startblock based on the next block's
3154 * start and the gap size.
3156 gotbno = ap->got.br_startblock;
3159 * If the gap is large relative to the piece we're
3160 * allocating, or using it gives us an invalid block
3161 * number, then just use the start of the next block
3162 * offset by our length.
3164 if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
3165 ISVALID(gotbno - gotdiff, gotbno))
3167 else if (ISVALID(gotbno - ap->length, gotbno)) {
3168 gotbno -= ap->length;
3169 gotdiff += adjust - ap->length;
3173 * If the firstblock forbids it, can't use it,
3176 if (!rt && !nullfb &&
3177 XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
3178 gotbno = NULLFSBLOCK;
3181 * No next block, just default.
3184 gotbno = NULLFSBLOCK;
3186 * If both valid, pick the better one, else the only good
3187 * one, else ap->blkno is already set (to 0 or the inode block).
3189 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
3190 ap->blkno = prevdiff <= gotdiff ? prevbno : gotbno;
3191 else if (prevbno != NULLFSBLOCK)
3192 ap->blkno = prevbno;
3193 else if (gotbno != NULLFSBLOCK)
3200 xfs_bmap_longest_free_extent(
3201 struct xfs_trans *tp,
3206 struct xfs_mount *mp = tp->t_mountp;
3207 struct xfs_perag *pag;
3208 xfs_extlen_t longest;
3211 pag = xfs_perag_get(mp, ag);
3212 if (!pag->pagf_init) {
3213 error = xfs_alloc_pagf_init(mp, tp, ag, XFS_ALLOC_FLAG_TRYLOCK);
3217 if (!pag->pagf_init) {
3223 longest = xfs_alloc_longest_free_extent(mp, pag,
3224 xfs_alloc_min_freelist(mp, pag),
3225 xfs_ag_resv_needed(pag, XFS_AG_RESV_NONE));
3226 if (*blen < longest)
3235 xfs_bmap_select_minlen(
3236 struct xfs_bmalloca *ap,
3237 struct xfs_alloc_arg *args,
3241 if (notinit || *blen < ap->minlen) {
3243 * Since we did a BUF_TRYLOCK above, it is possible that
3244 * there is space for this request.
3246 args->minlen = ap->minlen;
3247 } else if (*blen < args->maxlen) {
3249 * If the best seen length is less than the request length,
3250 * use the best as the minimum.
3252 args->minlen = *blen;
3255 * Otherwise we've seen an extent as big as maxlen, use that
3258 args->minlen = args->maxlen;
3263 xfs_bmap_btalloc_nullfb(
3264 struct xfs_bmalloca *ap,
3265 struct xfs_alloc_arg *args,
3268 struct xfs_mount *mp = ap->ip->i_mount;
3269 xfs_agnumber_t ag, startag;
3273 args->type = XFS_ALLOCTYPE_START_BNO;
3274 args->total = ap->total;
3276 startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3277 if (startag == NULLAGNUMBER)
3280 while (*blen < args->maxlen) {
3281 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3286 if (++ag == mp->m_sb.sb_agcount)
3292 xfs_bmap_select_minlen(ap, args, blen, notinit);
3297 xfs_bmap_btalloc_filestreams(
3298 struct xfs_bmalloca *ap,
3299 struct xfs_alloc_arg *args,
3302 struct xfs_mount *mp = ap->ip->i_mount;
3307 args->type = XFS_ALLOCTYPE_NEAR_BNO;
3308 args->total = ap->total;
3310 ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3311 if (ag == NULLAGNUMBER)
3314 error = xfs_bmap_longest_free_extent(args->tp, ag, blen, ¬init);
3318 if (*blen < args->maxlen) {
3319 error = xfs_filestream_new_ag(ap, &ag);
3323 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3330 xfs_bmap_select_minlen(ap, args, blen, notinit);
3333 * Set the failure fallback case to look in the selected AG as stream
3336 ap->blkno = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0);
3340 /* Update all inode and quota accounting for the allocation we just did. */
3342 xfs_bmap_btalloc_accounting(
3343 struct xfs_bmalloca *ap,
3344 struct xfs_alloc_arg *args)
3346 if (!(ap->flags & XFS_BMAPI_COWFORK))
3347 ap->ip->i_d.di_nblocks += args->len;
3348 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
3350 ap->ip->i_delayed_blks -= args->len;
3351 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
3352 ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT : XFS_TRANS_DQ_BCOUNT,
3358 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
3360 xfs_mount_t *mp; /* mount point structure */
3361 xfs_alloctype_t atype = 0; /* type for allocation routines */
3362 xfs_extlen_t align = 0; /* minimum allocation alignment */
3363 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
3365 xfs_alloc_arg_t args;
3367 xfs_extlen_t nextminlen = 0;
3368 int nullfb; /* true if ap->firstblock isn't set */
3376 mp = ap->ip->i_mount;
3378 /* stripe alignment for allocation is determined by mount parameters */
3380 if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC))
3381 stripe_align = mp->m_swidth;
3382 else if (mp->m_dalign)
3383 stripe_align = mp->m_dalign;
3385 if (ap->flags & XFS_BMAPI_COWFORK)
3386 align = xfs_get_cowextsz_hint(ap->ip);
3387 else if (xfs_alloc_is_userdata(ap->datatype))
3388 align = xfs_get_extsz_hint(ap->ip);
3390 error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev,
3391 align, 0, ap->eof, 0, ap->conv,
3392 &ap->offset, &ap->length);
3398 nullfb = *ap->firstblock == NULLFSBLOCK;
3399 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
3401 if (xfs_alloc_is_userdata(ap->datatype) &&
3402 xfs_inode_is_filestream(ap->ip)) {
3403 ag = xfs_filestream_lookup_ag(ap->ip);
3404 ag = (ag != NULLAGNUMBER) ? ag : 0;
3405 ap->blkno = XFS_AGB_TO_FSB(mp, ag, 0);
3407 ap->blkno = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
3410 ap->blkno = *ap->firstblock;
3412 xfs_bmap_adjacent(ap);
3415 * If allowed, use ap->blkno; otherwise must use firstblock since
3416 * it's in the right allocation group.
3418 if (nullfb || XFS_FSB_TO_AGNO(mp, ap->blkno) == fb_agno)
3421 ap->blkno = *ap->firstblock;
3423 * Normal allocation, done through xfs_alloc_vextent.
3425 tryagain = isaligned = 0;
3426 memset(&args, 0, sizeof(args));
3429 args.fsbno = ap->blkno;
3430 xfs_rmap_skip_owner_update(&args.oinfo);
3432 /* Trim the allocation back to the maximum an AG can fit. */
3433 args.maxlen = MIN(ap->length, mp->m_ag_max_usable);
3434 args.firstblock = *ap->firstblock;
3438 * Search for an allocation group with a single extent large
3439 * enough for the request. If one isn't found, then adjust
3440 * the minimum allocation size to the largest space found.
3442 if (xfs_alloc_is_userdata(ap->datatype) &&
3443 xfs_inode_is_filestream(ap->ip))
3444 error = xfs_bmap_btalloc_filestreams(ap, &args, &blen);
3446 error = xfs_bmap_btalloc_nullfb(ap, &args, &blen);
3449 } else if (ap->dfops->dop_low) {
3450 if (xfs_inode_is_filestream(ap->ip))
3451 args.type = XFS_ALLOCTYPE_FIRST_AG;
3453 args.type = XFS_ALLOCTYPE_START_BNO;
3454 args.total = args.minlen = ap->minlen;
3456 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3457 args.total = ap->total;
3458 args.minlen = ap->minlen;
3460 /* apply extent size hints if obtained earlier */
3463 if ((args.mod = (xfs_extlen_t)do_mod(ap->offset, args.prod)))
3464 args.mod = (xfs_extlen_t)(args.prod - args.mod);
3465 } else if (mp->m_sb.sb_blocksize >= PAGE_SIZE) {
3469 args.prod = PAGE_SIZE >> mp->m_sb.sb_blocklog;
3470 if ((args.mod = (xfs_extlen_t)(do_mod(ap->offset, args.prod))))
3471 args.mod = (xfs_extlen_t)(args.prod - args.mod);
3474 * If we are not low on available data blocks, and the
3475 * underlying logical volume manager is a stripe, and
3476 * the file offset is zero then try to allocate data
3477 * blocks on stripe unit boundary.
3478 * NOTE: ap->aeof is only set if the allocation length
3479 * is >= the stripe unit and the allocation offset is
3480 * at the end of file.
3482 if (!ap->dfops->dop_low && ap->aeof) {
3484 args.alignment = stripe_align;
3488 * Adjust for alignment
3490 if (blen > args.alignment && blen <= args.maxlen)
3491 args.minlen = blen - args.alignment;
3492 args.minalignslop = 0;
3495 * First try an exact bno allocation.
3496 * If it fails then do a near or start bno
3497 * allocation with alignment turned on.
3501 args.type = XFS_ALLOCTYPE_THIS_BNO;
3504 * Compute the minlen+alignment for the
3505 * next case. Set slop so that the value
3506 * of minlen+alignment+slop doesn't go up
3507 * between the calls.
3509 if (blen > stripe_align && blen <= args.maxlen)
3510 nextminlen = blen - stripe_align;
3512 nextminlen = args.minlen;
3513 if (nextminlen + stripe_align > args.minlen + 1)
3515 nextminlen + stripe_align -
3518 args.minalignslop = 0;
3522 args.minalignslop = 0;
3524 args.minleft = ap->minleft;
3525 args.wasdel = ap->wasdel;
3526 args.resv = XFS_AG_RESV_NONE;
3527 args.datatype = ap->datatype;
3528 if (ap->datatype & XFS_ALLOC_USERDATA_ZERO)
3531 error = xfs_alloc_vextent(&args);
3535 if (tryagain && args.fsbno == NULLFSBLOCK) {
3537 * Exact allocation failed. Now try with alignment
3541 args.fsbno = ap->blkno;
3542 args.alignment = stripe_align;
3543 args.minlen = nextminlen;
3544 args.minalignslop = 0;
3546 if ((error = xfs_alloc_vextent(&args)))
3549 if (isaligned && args.fsbno == NULLFSBLOCK) {
3551 * allocation failed, so turn off alignment and
3555 args.fsbno = ap->blkno;
3557 if ((error = xfs_alloc_vextent(&args)))
3560 if (args.fsbno == NULLFSBLOCK && nullfb &&
3561 args.minlen > ap->minlen) {
3562 args.minlen = ap->minlen;
3563 args.type = XFS_ALLOCTYPE_START_BNO;
3564 args.fsbno = ap->blkno;
3565 if ((error = xfs_alloc_vextent(&args)))
3568 if (args.fsbno == NULLFSBLOCK && nullfb) {
3570 args.type = XFS_ALLOCTYPE_FIRST_AG;
3571 args.total = ap->minlen;
3572 if ((error = xfs_alloc_vextent(&args)))
3574 ap->dfops->dop_low = true;
3576 if (args.fsbno != NULLFSBLOCK) {
3578 * check the allocation happened at the same or higher AG than
3579 * the first block that was allocated.
3581 ASSERT(*ap->firstblock == NULLFSBLOCK ||
3582 XFS_FSB_TO_AGNO(mp, *ap->firstblock) <=
3583 XFS_FSB_TO_AGNO(mp, args.fsbno));
3585 ap->blkno = args.fsbno;
3586 if (*ap->firstblock == NULLFSBLOCK)
3587 *ap->firstblock = args.fsbno;
3588 ASSERT(nullfb || fb_agno <= args.agno);
3589 ap->length = args.len;
3590 xfs_bmap_btalloc_accounting(ap, &args);
3592 ap->blkno = NULLFSBLOCK;
3599 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
3600 * It figures out where to ask the underlying allocator to put the new extent.
3604 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
3606 if (XFS_IS_REALTIME_INODE(ap->ip) &&
3607 xfs_alloc_is_userdata(ap->datatype))
3608 return xfs_bmap_rtalloc(ap);
3609 return xfs_bmap_btalloc(ap);
3612 /* Trim extent to fit a logical block range. */
3615 struct xfs_bmbt_irec *irec,
3619 xfs_fileoff_t distance;
3620 xfs_fileoff_t end = bno + len;
3622 if (irec->br_startoff + irec->br_blockcount <= bno ||
3623 irec->br_startoff >= end) {
3624 irec->br_blockcount = 0;
3628 if (irec->br_startoff < bno) {
3629 distance = bno - irec->br_startoff;
3630 if (isnullstartblock(irec->br_startblock))
3631 irec->br_startblock = DELAYSTARTBLOCK;
3632 if (irec->br_startblock != DELAYSTARTBLOCK &&
3633 irec->br_startblock != HOLESTARTBLOCK)
3634 irec->br_startblock += distance;
3635 irec->br_startoff += distance;
3636 irec->br_blockcount -= distance;
3639 if (end < irec->br_startoff + irec->br_blockcount) {
3640 distance = irec->br_startoff + irec->br_blockcount - end;
3641 irec->br_blockcount -= distance;
3645 /* trim extent to within eof */
3647 xfs_trim_extent_eof(
3648 struct xfs_bmbt_irec *irec,
3649 struct xfs_inode *ip)
3652 xfs_trim_extent(irec, 0, XFS_B_TO_FSB(ip->i_mount,
3653 i_size_read(VFS_I(ip))));
3657 * Trim the returned map to the required bounds
3661 struct xfs_bmbt_irec *mval,
3662 struct xfs_bmbt_irec *got,
3670 if ((flags & XFS_BMAPI_ENTIRE) ||
3671 got->br_startoff + got->br_blockcount <= obno) {
3673 if (isnullstartblock(got->br_startblock))
3674 mval->br_startblock = DELAYSTARTBLOCK;
3680 ASSERT((*bno >= obno) || (n == 0));
3682 mval->br_startoff = *bno;
3683 if (isnullstartblock(got->br_startblock))
3684 mval->br_startblock = DELAYSTARTBLOCK;
3686 mval->br_startblock = got->br_startblock +
3687 (*bno - got->br_startoff);
3689 * Return the minimum of what we got and what we asked for for
3690 * the length. We can use the len variable here because it is
3691 * modified below and we could have been there before coming
3692 * here if the first part of the allocation didn't overlap what
3695 mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno,
3696 got->br_blockcount - (*bno - got->br_startoff));
3697 mval->br_state = got->br_state;
3698 ASSERT(mval->br_blockcount <= len);
3703 * Update and validate the extent map to return
3706 xfs_bmapi_update_map(
3707 struct xfs_bmbt_irec **map,
3715 xfs_bmbt_irec_t *mval = *map;
3717 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
3718 ((mval->br_startoff + mval->br_blockcount) <= end));
3719 ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) ||
3720 (mval->br_startoff < obno));
3722 *bno = mval->br_startoff + mval->br_blockcount;
3724 if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) {
3725 /* update previous map with new information */
3726 ASSERT(mval->br_startblock == mval[-1].br_startblock);
3727 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
3728 ASSERT(mval->br_state == mval[-1].br_state);
3729 mval[-1].br_blockcount = mval->br_blockcount;
3730 mval[-1].br_state = mval->br_state;
3731 } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
3732 mval[-1].br_startblock != DELAYSTARTBLOCK &&
3733 mval[-1].br_startblock != HOLESTARTBLOCK &&
3734 mval->br_startblock == mval[-1].br_startblock +
3735 mval[-1].br_blockcount &&
3736 ((flags & XFS_BMAPI_IGSTATE) ||
3737 mval[-1].br_state == mval->br_state)) {
3738 ASSERT(mval->br_startoff ==
3739 mval[-1].br_startoff + mval[-1].br_blockcount);
3740 mval[-1].br_blockcount += mval->br_blockcount;
3741 } else if (*n > 0 &&
3742 mval->br_startblock == DELAYSTARTBLOCK &&
3743 mval[-1].br_startblock == DELAYSTARTBLOCK &&
3744 mval->br_startoff ==
3745 mval[-1].br_startoff + mval[-1].br_blockcount) {
3746 mval[-1].br_blockcount += mval->br_blockcount;
3747 mval[-1].br_state = mval->br_state;
3748 } else if (!((*n == 0) &&
3749 ((mval->br_startoff + mval->br_blockcount) <=
3758 * Map file blocks to filesystem blocks without allocation.
3762 struct xfs_inode *ip,
3765 struct xfs_bmbt_irec *mval,
3769 struct xfs_mount *mp = ip->i_mount;
3770 struct xfs_ifork *ifp;
3771 struct xfs_bmbt_irec got;
3774 struct xfs_iext_cursor icur;
3778 int whichfork = xfs_bmapi_whichfork(flags);
3781 ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK|XFS_BMAPI_ENTIRE|
3782 XFS_BMAPI_IGSTATE|XFS_BMAPI_COWFORK)));
3783 ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED|XFS_ILOCK_EXCL));
3785 if (unlikely(XFS_TEST_ERROR(
3786 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
3787 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
3788 mp, XFS_ERRTAG_BMAPIFORMAT))) {
3789 XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW, mp);
3790 return -EFSCORRUPTED;
3793 if (XFS_FORCED_SHUTDOWN(mp))
3796 XFS_STATS_INC(mp, xs_blk_mapr);
3798 ifp = XFS_IFORK_PTR(ip, whichfork);
3800 /* No CoW fork? Return a hole. */
3801 if (whichfork == XFS_COW_FORK && !ifp) {
3802 mval->br_startoff = bno;
3803 mval->br_startblock = HOLESTARTBLOCK;
3804 mval->br_blockcount = len;
3805 mval->br_state = XFS_EXT_NORM;
3810 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
3811 error = xfs_iread_extents(NULL, ip, whichfork);
3816 if (!xfs_iext_lookup_extent(ip, ifp, bno, &icur, &got))
3821 while (bno < end && n < *nmap) {
3822 /* Reading past eof, act as though there's a hole up to end. */
3824 got.br_startoff = end;
3825 if (got.br_startoff > bno) {
3826 /* Reading in a hole. */
3827 mval->br_startoff = bno;
3828 mval->br_startblock = HOLESTARTBLOCK;
3829 mval->br_blockcount =
3830 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
3831 mval->br_state = XFS_EXT_NORM;
3832 bno += mval->br_blockcount;
3833 len -= mval->br_blockcount;
3839 /* set up the extent map to return. */
3840 xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
3841 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
3843 /* If we're done, stop now. */
3844 if (bno >= end || n >= *nmap)
3847 /* Else go on to the next record. */
3848 if (!xfs_iext_next_extent(ifp, &icur, &got))
3856 * Add a delayed allocation extent to an inode. Blocks are reserved from the
3857 * global pool and the extent inserted into the inode in-core extent tree.
3859 * On entry, got refers to the first extent beyond the offset of the extent to
3860 * allocate or eof is specified if no such extent exists. On return, got refers
3861 * to the extent record that was inserted to the inode fork.
3863 * Note that the allocated extent may have been merged with contiguous extents
3864 * during insertion into the inode fork. Thus, got does not reflect the current
3865 * state of the inode fork on return. If necessary, the caller can use lastx to
3866 * look up the updated record in the inode fork.
3869 xfs_bmapi_reserve_delalloc(
3870 struct xfs_inode *ip,
3874 xfs_filblks_t prealloc,
3875 struct xfs_bmbt_irec *got,
3876 struct xfs_iext_cursor *icur,
3879 struct xfs_mount *mp = ip->i_mount;
3880 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
3882 xfs_extlen_t indlen;
3884 xfs_fileoff_t aoff = off;
3887 * Cap the alloc length. Keep track of prealloc so we know whether to
3888 * tag the inode before we return.
3890 alen = XFS_FILBLKS_MIN(len + prealloc, MAXEXTLEN);
3892 alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff);
3893 if (prealloc && alen >= len)
3894 prealloc = alen - len;
3896 /* Figure out the extent size, adjust alen */
3897 if (whichfork == XFS_COW_FORK) {
3898 struct xfs_bmbt_irec prev;
3899 xfs_extlen_t extsz = xfs_get_cowextsz_hint(ip);
3901 if (!xfs_iext_peek_prev_extent(ifp, icur, &prev))
3902 prev.br_startoff = NULLFILEOFF;
3904 error = xfs_bmap_extsize_align(mp, got, &prev, extsz, 0, eof,
3905 1, 0, &aoff, &alen);
3910 * Make a transaction-less quota reservation for delayed allocation
3911 * blocks. This number gets adjusted later. We return if we haven't
3912 * allocated blocks already inside this loop.
3914 error = xfs_trans_reserve_quota_nblks(NULL, ip, (long)alen, 0,
3915 XFS_QMOPT_RES_REGBLKS);
3920 * Split changing sb for alen and indlen since they could be coming
3921 * from different places.
3923 indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen);
3926 error = xfs_mod_fdblocks(mp, -((int64_t)alen), false);
3928 goto out_unreserve_quota;
3930 error = xfs_mod_fdblocks(mp, -((int64_t)indlen), false);
3932 goto out_unreserve_blocks;
3935 ip->i_delayed_blks += alen;
3937 got->br_startoff = aoff;
3938 got->br_startblock = nullstartblock(indlen);
3939 got->br_blockcount = alen;
3940 got->br_state = XFS_EXT_NORM;
3942 xfs_bmap_add_extent_hole_delay(ip, whichfork, icur, got);
3945 * Tag the inode if blocks were preallocated. Note that COW fork
3946 * preallocation can occur at the start or end of the extent, even when
3947 * prealloc == 0, so we must also check the aligned offset and length.
3949 if (whichfork == XFS_DATA_FORK && prealloc)
3950 xfs_inode_set_eofblocks_tag(ip);
3951 if (whichfork == XFS_COW_FORK && (prealloc || aoff < off || alen > len))
3952 xfs_inode_set_cowblocks_tag(ip);
3956 out_unreserve_blocks:
3957 xfs_mod_fdblocks(mp, alen, false);
3958 out_unreserve_quota:
3959 if (XFS_IS_QUOTA_ON(mp))
3960 xfs_trans_unreserve_quota_nblks(NULL, ip, (long)alen, 0,
3961 XFS_QMOPT_RES_REGBLKS);
3967 struct xfs_bmalloca *bma)
3969 struct xfs_mount *mp = bma->ip->i_mount;
3970 int whichfork = xfs_bmapi_whichfork(bma->flags);
3971 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
3972 int tmp_logflags = 0;
3975 ASSERT(bma->length > 0);
3978 * For the wasdelay case, we could also just allocate the stuff asked
3979 * for in this bmap call but that wouldn't be as good.
3982 bma->length = (xfs_extlen_t)bma->got.br_blockcount;
3983 bma->offset = bma->got.br_startoff;
3984 xfs_iext_peek_prev_extent(ifp, &bma->icur, &bma->prev);
3986 bma->length = XFS_FILBLKS_MIN(bma->length, MAXEXTLEN);
3988 bma->length = XFS_FILBLKS_MIN(bma->length,
3989 bma->got.br_startoff - bma->offset);
3993 * Set the data type being allocated. For the data fork, the first data
3994 * in the file is treated differently to all other allocations. For the
3995 * attribute fork, we only need to ensure the allocated range is not on
3998 if (!(bma->flags & XFS_BMAPI_METADATA)) {
3999 bma->datatype = XFS_ALLOC_NOBUSY;
4000 if (whichfork == XFS_DATA_FORK) {
4001 if (bma->offset == 0)
4002 bma->datatype |= XFS_ALLOC_INITIAL_USER_DATA;
4004 bma->datatype |= XFS_ALLOC_USERDATA;
4006 if (bma->flags & XFS_BMAPI_ZERO)
4007 bma->datatype |= XFS_ALLOC_USERDATA_ZERO;
4010 bma->minlen = (bma->flags & XFS_BMAPI_CONTIG) ? bma->length : 1;
4013 * Only want to do the alignment at the eof if it is userdata and
4014 * allocation length is larger than a stripe unit.
4016 if (mp->m_dalign && bma->length >= mp->m_dalign &&
4017 !(bma->flags & XFS_BMAPI_METADATA) && whichfork == XFS_DATA_FORK) {
4018 error = xfs_bmap_isaeof(bma, whichfork);
4023 error = xfs_bmap_alloc(bma);
4028 bma->cur->bc_private.b.firstblock = *bma->firstblock;
4029 if (bma->blkno == NULLFSBLOCK)
4031 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4032 bma->cur = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork);
4033 bma->cur->bc_private.b.firstblock = *bma->firstblock;
4034 bma->cur->bc_private.b.dfops = bma->dfops;
4037 * Bump the number of extents we've allocated
4043 bma->cur->bc_private.b.flags =
4044 bma->wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
4046 bma->got.br_startoff = bma->offset;
4047 bma->got.br_startblock = bma->blkno;
4048 bma->got.br_blockcount = bma->length;
4049 bma->got.br_state = XFS_EXT_NORM;
4052 * In the data fork, a wasdelay extent has been initialized, so
4053 * shouldn't be flagged as unwritten.
4055 * For the cow fork, however, we convert delalloc reservations
4056 * (extents allocated for speculative preallocation) to
4057 * allocated unwritten extents, and only convert the unwritten
4058 * extents to real extents when we're about to write the data.
4060 if ((!bma->wasdel || (bma->flags & XFS_BMAPI_COWFORK)) &&
4061 (bma->flags & XFS_BMAPI_PREALLOC) &&
4062 xfs_sb_version_hasextflgbit(&mp->m_sb))
4063 bma->got.br_state = XFS_EXT_UNWRITTEN;
4066 error = xfs_bmap_add_extent_delay_real(bma, whichfork);
4068 error = xfs_bmap_add_extent_hole_real(bma->tp, bma->ip,
4069 whichfork, &bma->icur, &bma->cur, &bma->got,
4070 bma->firstblock, bma->dfops, &bma->logflags);
4072 bma->logflags |= tmp_logflags;
4077 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4078 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4079 * the neighbouring ones.
4081 xfs_iext_get_extent(ifp, &bma->icur, &bma->got);
4083 ASSERT(bma->got.br_startoff <= bma->offset);
4084 ASSERT(bma->got.br_startoff + bma->got.br_blockcount >=
4085 bma->offset + bma->length);
4086 ASSERT(bma->got.br_state == XFS_EXT_NORM ||
4087 bma->got.br_state == XFS_EXT_UNWRITTEN);
4092 xfs_bmapi_convert_unwritten(
4093 struct xfs_bmalloca *bma,
4094 struct xfs_bmbt_irec *mval,
4098 int whichfork = xfs_bmapi_whichfork(flags);
4099 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
4100 int tmp_logflags = 0;
4103 /* check if we need to do unwritten->real conversion */
4104 if (mval->br_state == XFS_EXT_UNWRITTEN &&
4105 (flags & XFS_BMAPI_PREALLOC))
4108 /* check if we need to do real->unwritten conversion */
4109 if (mval->br_state == XFS_EXT_NORM &&
4110 (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) !=
4111 (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT))
4115 * Modify (by adding) the state flag, if writing.
4117 ASSERT(mval->br_blockcount <= len);
4118 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4119 bma->cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp,
4120 bma->ip, whichfork);
4121 bma->cur->bc_private.b.firstblock = *bma->firstblock;
4122 bma->cur->bc_private.b.dfops = bma->dfops;
4124 mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN)
4125 ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
4128 * Before insertion into the bmbt, zero the range being converted
4131 if (flags & XFS_BMAPI_ZERO) {
4132 error = xfs_zero_extent(bma->ip, mval->br_startblock,
4133 mval->br_blockcount);
4138 error = xfs_bmap_add_extent_unwritten_real(bma->tp, bma->ip, whichfork,
4139 &bma->icur, &bma->cur, mval, bma->firstblock,
4140 bma->dfops, &tmp_logflags);
4142 * Log the inode core unconditionally in the unwritten extent conversion
4143 * path because the conversion might not have done so (e.g., if the
4144 * extent count hasn't changed). We need to make sure the inode is dirty
4145 * in the transaction for the sake of fsync(), even if nothing has
4146 * changed, because fsync() will not force the log for this transaction
4147 * unless it sees the inode pinned.
4149 * Note: If we're only converting cow fork extents, there aren't
4150 * any on-disk updates to make, so we don't need to log anything.
4152 if (whichfork != XFS_COW_FORK)
4153 bma->logflags |= tmp_logflags | XFS_ILOG_CORE;
4158 * Update our extent pointer, given that
4159 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4160 * of the neighbouring ones.
4162 xfs_iext_get_extent(ifp, &bma->icur, &bma->got);
4165 * We may have combined previously unwritten space with written space,
4166 * so generate another request.
4168 if (mval->br_blockcount < len)
4174 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4175 * extent state if necessary. Details behaviour is controlled by the flags
4176 * parameter. Only allocates blocks from a single allocation group, to avoid
4179 * The returned value in "firstblock" from the first call in a transaction
4180 * must be remembered and presented to subsequent calls in "firstblock".
4181 * An upper bound for the number of blocks to be allocated is supplied to
4182 * the first call in "total"; if no allocation group has that many free
4183 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4187 struct xfs_trans *tp, /* transaction pointer */
4188 struct xfs_inode *ip, /* incore inode */
4189 xfs_fileoff_t bno, /* starting file offs. mapped */
4190 xfs_filblks_t len, /* length to map in file */
4191 int flags, /* XFS_BMAPI_... */
4192 xfs_fsblock_t *firstblock, /* first allocated block
4193 controls a.g. for allocs */
4194 xfs_extlen_t total, /* total blocks needed */
4195 struct xfs_bmbt_irec *mval, /* output: map values */
4196 int *nmap, /* i/o: mval size/count */
4197 struct xfs_defer_ops *dfops) /* i/o: list extents to free */
4199 struct xfs_mount *mp = ip->i_mount;
4200 struct xfs_ifork *ifp;
4201 struct xfs_bmalloca bma = { NULL }; /* args for xfs_bmap_alloc */
4202 xfs_fileoff_t end; /* end of mapped file region */
4203 bool eof = false; /* after the end of extents */
4204 int error; /* error return */
4205 int n; /* current extent index */
4206 xfs_fileoff_t obno; /* old block number (offset) */
4207 int whichfork; /* data or attr fork */
4210 xfs_fileoff_t orig_bno; /* original block number value */
4211 int orig_flags; /* original flags arg value */
4212 xfs_filblks_t orig_len; /* original value of len arg */
4213 struct xfs_bmbt_irec *orig_mval; /* original value of mval */
4214 int orig_nmap; /* original value of *nmap */
4222 whichfork = xfs_bmapi_whichfork(flags);
4225 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP);
4226 ASSERT(!(flags & XFS_BMAPI_IGSTATE));
4227 ASSERT(tp != NULL ||
4228 (flags & (XFS_BMAPI_CONVERT | XFS_BMAPI_COWFORK)) ==
4229 (XFS_BMAPI_CONVERT | XFS_BMAPI_COWFORK));
4231 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL);
4232 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
4233 ASSERT(!(flags & XFS_BMAPI_REMAP));
4235 /* zeroing is for currently only for data extents, not metadata */
4236 ASSERT((flags & (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO)) !=
4237 (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO));
4239 * we can allocate unwritten extents or pre-zero allocated blocks,
4240 * but it makes no sense to do both at once. This would result in
4241 * zeroing the unwritten extent twice, but it still being an
4242 * unwritten extent....
4244 ASSERT((flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)) !=
4245 (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO));
4247 if (unlikely(XFS_TEST_ERROR(
4248 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4249 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
4250 mp, XFS_ERRTAG_BMAPIFORMAT))) {
4251 XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW, mp);
4252 return -EFSCORRUPTED;
4255 if (XFS_FORCED_SHUTDOWN(mp))
4258 ifp = XFS_IFORK_PTR(ip, whichfork);
4260 XFS_STATS_INC(mp, xs_blk_mapw);
4262 if (*firstblock == NULLFSBLOCK) {
4263 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE)
4264 bma.minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1;
4271 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4272 error = xfs_iread_extents(tp, ip, whichfork);
4281 if (!xfs_iext_lookup_extent(ip, ifp, bno, &bma.icur, &bma.got))
4283 if (!xfs_iext_peek_prev_extent(ifp, &bma.icur, &bma.prev))
4284 bma.prev.br_startoff = NULLFILEOFF;
4290 bma.firstblock = firstblock;
4292 while (bno < end && n < *nmap) {
4293 bool need_alloc = false, wasdelay = false;
4295 /* in hole or beyond EOF? */
4296 if (eof || bma.got.br_startoff > bno) {
4298 * CoW fork conversions should /never/ hit EOF or
4299 * holes. There should always be something for us
4302 ASSERT(!((flags & XFS_BMAPI_CONVERT) &&
4303 (flags & XFS_BMAPI_COWFORK)));
4305 if (flags & XFS_BMAPI_DELALLOC) {
4307 * For the COW fork we can reasonably get a
4308 * request for converting an extent that races
4309 * with other threads already having converted
4310 * part of it, as there converting COW to
4311 * regular blocks is not protected using the
4314 ASSERT(flags & XFS_BMAPI_COWFORK);
4315 if (!(flags & XFS_BMAPI_COWFORK)) {
4320 if (eof || bno >= end)
4325 } else if (isnullstartblock(bma.got.br_startblock)) {
4330 * First, deal with the hole before the allocated space
4331 * that we found, if any.
4333 if ((need_alloc || wasdelay) &&
4334 !(flags & XFS_BMAPI_CONVERT_ONLY)) {
4336 bma.conv = !!(flags & XFS_BMAPI_CONVERT);
4337 bma.wasdel = wasdelay;
4342 * There's a 32/64 bit type mismatch between the
4343 * allocation length request (which can be 64 bits in
4344 * length) and the bma length request, which is
4345 * xfs_extlen_t and therefore 32 bits. Hence we have to
4346 * check for 32-bit overflows and handle them here.
4348 if (len > (xfs_filblks_t)MAXEXTLEN)
4349 bma.length = MAXEXTLEN;
4354 ASSERT(bma.length > 0);
4355 error = xfs_bmapi_allocate(&bma);
4358 if (bma.blkno == NULLFSBLOCK)
4362 * If this is a CoW allocation, record the data in
4363 * the refcount btree for orphan recovery.
4365 if (whichfork == XFS_COW_FORK) {
4366 error = xfs_refcount_alloc_cow_extent(mp, dfops,
4367 bma.blkno, bma.length);
4373 /* Deal with the allocated space we found. */
4374 xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno,
4377 /* Execute unwritten extent conversion if necessary */
4378 error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags);
4379 if (error == -EAGAIN)
4384 /* update the extent map to return */
4385 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4388 * If we're done, stop now. Stop when we've allocated
4389 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4390 * the transaction may get too big.
4392 if (bno >= end || n >= *nmap || bma.nallocs >= *nmap)
4395 /* Else go on to the next record. */
4397 if (!xfs_iext_next_extent(ifp, &bma.icur, &bma.got))
4403 * Transform from btree to extents, give it cur.
4405 if (xfs_bmap_wants_extents(ip, whichfork)) {
4406 int tmp_logflags = 0;
4409 error = xfs_bmap_btree_to_extents(tp, ip, bma.cur,
4410 &tmp_logflags, whichfork);
4411 bma.logflags |= tmp_logflags;
4416 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE ||
4417 XFS_IFORK_NEXTENTS(ip, whichfork) >
4418 XFS_IFORK_MAXEXT(ip, whichfork));
4422 * Log everything. Do this after conversion, there's no point in
4423 * logging the extent records if we've converted to btree format.
4425 if ((bma.logflags & xfs_ilog_fext(whichfork)) &&
4426 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
4427 bma.logflags &= ~xfs_ilog_fext(whichfork);
4428 else if ((bma.logflags & xfs_ilog_fbroot(whichfork)) &&
4429 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
4430 bma.logflags &= ~xfs_ilog_fbroot(whichfork);
4432 * Log whatever the flags say, even if error. Otherwise we might miss
4433 * detecting a case where the data is changed, there's an error,
4434 * and it's not logged so we don't shutdown when we should.
4437 xfs_trans_log_inode(tp, ip, bma.logflags);
4441 ASSERT(*firstblock == NULLFSBLOCK ||
4442 XFS_FSB_TO_AGNO(mp, *firstblock) <=
4444 bma.cur->bc_private.b.firstblock));
4445 *firstblock = bma.cur->bc_private.b.firstblock;
4447 xfs_btree_del_cursor(bma.cur,
4448 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
4451 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
4458 struct xfs_trans *tp,
4459 struct xfs_inode *ip,
4462 xfs_fsblock_t startblock,
4463 struct xfs_defer_ops *dfops)
4465 struct xfs_mount *mp = ip->i_mount;
4466 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
4467 struct xfs_btree_cur *cur = NULL;
4468 xfs_fsblock_t firstblock = NULLFSBLOCK;
4469 struct xfs_bmbt_irec got;
4470 struct xfs_iext_cursor icur;
4471 int logflags = 0, error;
4474 ASSERT(len <= (xfs_filblks_t)MAXEXTLEN);
4475 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
4477 if (unlikely(XFS_TEST_ERROR(
4478 (XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_EXTENTS &&
4479 XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_BTREE),
4480 mp, XFS_ERRTAG_BMAPIFORMAT))) {
4481 XFS_ERROR_REPORT("xfs_bmapi_remap", XFS_ERRLEVEL_LOW, mp);
4482 return -EFSCORRUPTED;
4485 if (XFS_FORCED_SHUTDOWN(mp))
4488 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4489 error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK);
4494 if (xfs_iext_lookup_extent(ip, ifp, bno, &icur, &got)) {
4495 /* make sure we only reflink into a hole. */
4496 ASSERT(got.br_startoff > bno);
4497 ASSERT(got.br_startoff - bno >= len);
4500 ip->i_d.di_nblocks += len;
4501 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
4503 if (ifp->if_flags & XFS_IFBROOT) {
4504 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
4505 cur->bc_private.b.firstblock = firstblock;
4506 cur->bc_private.b.dfops = dfops;
4507 cur->bc_private.b.flags = 0;
4510 got.br_startoff = bno;
4511 got.br_startblock = startblock;
4512 got.br_blockcount = len;
4513 got.br_state = XFS_EXT_NORM;
4515 error = xfs_bmap_add_extent_hole_real(tp, ip, XFS_DATA_FORK, &icur,
4516 &cur, &got, &firstblock, dfops, &logflags);
4520 if (xfs_bmap_wants_extents(ip, XFS_DATA_FORK)) {
4521 int tmp_logflags = 0;
4523 error = xfs_bmap_btree_to_extents(tp, ip, cur,
4524 &tmp_logflags, XFS_DATA_FORK);
4525 logflags |= tmp_logflags;
4529 if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS)
4530 logflags &= ~XFS_ILOG_DEXT;
4531 else if (ip->i_d.di_format != XFS_DINODE_FMT_BTREE)
4532 logflags &= ~XFS_ILOG_DBROOT;
4535 xfs_trans_log_inode(tp, ip, logflags);
4537 xfs_btree_del_cursor(cur,
4538 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
4544 * When a delalloc extent is split (e.g., due to a hole punch), the original
4545 * indlen reservation must be shared across the two new extents that are left
4548 * Given the original reservation and the worst case indlen for the two new
4549 * extents (as calculated by xfs_bmap_worst_indlen()), split the original
4550 * reservation fairly across the two new extents. If necessary, steal available
4551 * blocks from a deleted extent to make up a reservation deficiency (e.g., if
4552 * ores == 1). The number of stolen blocks is returned. The availability and
4553 * subsequent accounting of stolen blocks is the responsibility of the caller.
4555 static xfs_filblks_t
4556 xfs_bmap_split_indlen(
4557 xfs_filblks_t ores, /* original res. */
4558 xfs_filblks_t *indlen1, /* ext1 worst indlen */
4559 xfs_filblks_t *indlen2, /* ext2 worst indlen */
4560 xfs_filblks_t avail) /* stealable blocks */
4562 xfs_filblks_t len1 = *indlen1;
4563 xfs_filblks_t len2 = *indlen2;
4564 xfs_filblks_t nres = len1 + len2; /* new total res. */
4565 xfs_filblks_t stolen = 0;
4566 xfs_filblks_t resfactor;
4569 * Steal as many blocks as we can to try and satisfy the worst case
4570 * indlen for both new extents.
4572 if (ores < nres && avail)
4573 stolen = XFS_FILBLKS_MIN(nres - ores, avail);
4576 /* nothing else to do if we've satisfied the new reservation */
4581 * We can't meet the total required reservation for the two extents.
4582 * Calculate the percent of the overall shortage between both extents
4583 * and apply this percentage to each of the requested indlen values.
4584 * This distributes the shortage fairly and reduces the chances that one
4585 * of the two extents is left with nothing when extents are repeatedly
4588 resfactor = (ores * 100);
4589 do_div(resfactor, nres);
4594 ASSERT(len1 + len2 <= ores);
4595 ASSERT(len1 < *indlen1 && len2 < *indlen2);
4598 * Hand out the remainder to each extent. If one of the two reservations
4599 * is zero, we want to make sure that one gets a block first. The loop
4600 * below starts with len1, so hand len2 a block right off the bat if it
4603 ores -= (len1 + len2);
4604 ASSERT((*indlen1 - len1) + (*indlen2 - len2) >= ores);
4605 if (ores && !len2 && *indlen2) {
4610 if (len1 < *indlen1) {
4616 if (len2 < *indlen2) {
4629 xfs_bmap_del_extent_delay(
4630 struct xfs_inode *ip,
4632 struct xfs_iext_cursor *icur,
4633 struct xfs_bmbt_irec *got,
4634 struct xfs_bmbt_irec *del)
4636 struct xfs_mount *mp = ip->i_mount;
4637 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
4638 struct xfs_bmbt_irec new;
4639 int64_t da_old, da_new, da_diff = 0;
4640 xfs_fileoff_t del_endoff, got_endoff;
4641 xfs_filblks_t got_indlen, new_indlen, stolen;
4642 int state = xfs_bmap_fork_to_state(whichfork);
4646 XFS_STATS_INC(mp, xs_del_exlist);
4648 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
4649 del_endoff = del->br_startoff + del->br_blockcount;
4650 got_endoff = got->br_startoff + got->br_blockcount;
4651 da_old = startblockval(got->br_startblock);
4654 ASSERT(del->br_blockcount > 0);
4655 ASSERT(got->br_startoff <= del->br_startoff);
4656 ASSERT(got_endoff >= del_endoff);
4659 uint64_t rtexts = XFS_FSB_TO_B(mp, del->br_blockcount);
4661 do_div(rtexts, mp->m_sb.sb_rextsize);
4662 xfs_mod_frextents(mp, rtexts);
4666 * Update the inode delalloc counter now and wait to update the
4667 * sb counters as we might have to borrow some blocks for the
4668 * indirect block accounting.
4670 error = xfs_trans_reserve_quota_nblks(NULL, ip,
4671 -((long)del->br_blockcount), 0,
4672 isrt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
4675 ip->i_delayed_blks -= del->br_blockcount;
4677 if (got->br_startoff == del->br_startoff)
4678 state |= BMAP_LEFT_FILLING;
4679 if (got_endoff == del_endoff)
4680 state |= BMAP_RIGHT_FILLING;
4682 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
4683 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
4685 * Matches the whole extent. Delete the entry.
4687 xfs_iext_remove(ip, icur, state);
4688 xfs_iext_prev(ifp, icur);
4690 case BMAP_LEFT_FILLING:
4692 * Deleting the first part of the extent.
4694 got->br_startoff = del_endoff;
4695 got->br_blockcount -= del->br_blockcount;
4696 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
4697 got->br_blockcount), da_old);
4698 got->br_startblock = nullstartblock((int)da_new);
4699 xfs_iext_update_extent(ip, state, icur, got);
4701 case BMAP_RIGHT_FILLING:
4703 * Deleting the last part of the extent.
4705 got->br_blockcount = got->br_blockcount - del->br_blockcount;
4706 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
4707 got->br_blockcount), da_old);
4708 got->br_startblock = nullstartblock((int)da_new);
4709 xfs_iext_update_extent(ip, state, icur, got);
4713 * Deleting the middle of the extent.
4715 * Distribute the original indlen reservation across the two new
4716 * extents. Steal blocks from the deleted extent if necessary.
4717 * Stealing blocks simply fudges the fdblocks accounting below.
4718 * Warn if either of the new indlen reservations is zero as this
4719 * can lead to delalloc problems.
4721 got->br_blockcount = del->br_startoff - got->br_startoff;
4722 got_indlen = xfs_bmap_worst_indlen(ip, got->br_blockcount);
4724 new.br_blockcount = got_endoff - del_endoff;
4725 new_indlen = xfs_bmap_worst_indlen(ip, new.br_blockcount);
4727 WARN_ON_ONCE(!got_indlen || !new_indlen);
4728 stolen = xfs_bmap_split_indlen(da_old, &got_indlen, &new_indlen,
4729 del->br_blockcount);
4731 got->br_startblock = nullstartblock((int)got_indlen);
4733 new.br_startoff = del_endoff;
4734 new.br_state = got->br_state;
4735 new.br_startblock = nullstartblock((int)new_indlen);
4737 xfs_iext_update_extent(ip, state, icur, got);
4738 xfs_iext_next(ifp, icur);
4739 xfs_iext_insert(ip, icur, &new, state);
4741 da_new = got_indlen + new_indlen - stolen;
4742 del->br_blockcount -= stolen;
4746 ASSERT(da_old >= da_new);
4747 da_diff = da_old - da_new;
4749 da_diff += del->br_blockcount;
4751 xfs_mod_fdblocks(mp, da_diff, false);
4756 xfs_bmap_del_extent_cow(
4757 struct xfs_inode *ip,
4758 struct xfs_iext_cursor *icur,
4759 struct xfs_bmbt_irec *got,
4760 struct xfs_bmbt_irec *del)
4762 struct xfs_mount *mp = ip->i_mount;
4763 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
4764 struct xfs_bmbt_irec new;
4765 xfs_fileoff_t del_endoff, got_endoff;
4766 int state = BMAP_COWFORK;
4768 XFS_STATS_INC(mp, xs_del_exlist);
4770 del_endoff = del->br_startoff + del->br_blockcount;
4771 got_endoff = got->br_startoff + got->br_blockcount;
4773 ASSERT(del->br_blockcount > 0);
4774 ASSERT(got->br_startoff <= del->br_startoff);
4775 ASSERT(got_endoff >= del_endoff);
4776 ASSERT(!isnullstartblock(got->br_startblock));
4778 if (got->br_startoff == del->br_startoff)
4779 state |= BMAP_LEFT_FILLING;
4780 if (got_endoff == del_endoff)
4781 state |= BMAP_RIGHT_FILLING;
4783 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
4784 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
4786 * Matches the whole extent. Delete the entry.
4788 xfs_iext_remove(ip, icur, state);
4789 xfs_iext_prev(ifp, icur);
4791 case BMAP_LEFT_FILLING:
4793 * Deleting the first part of the extent.
4795 got->br_startoff = del_endoff;
4796 got->br_blockcount -= del->br_blockcount;
4797 got->br_startblock = del->br_startblock + del->br_blockcount;
4798 xfs_iext_update_extent(ip, state, icur, got);
4800 case BMAP_RIGHT_FILLING:
4802 * Deleting the last part of the extent.
4804 got->br_blockcount -= del->br_blockcount;
4805 xfs_iext_update_extent(ip, state, icur, got);
4809 * Deleting the middle of the extent.
4811 got->br_blockcount = del->br_startoff - got->br_startoff;
4813 new.br_startoff = del_endoff;
4814 new.br_blockcount = got_endoff - del_endoff;
4815 new.br_state = got->br_state;
4816 new.br_startblock = del->br_startblock + del->br_blockcount;
4818 xfs_iext_update_extent(ip, state, icur, got);
4819 xfs_iext_next(ifp, icur);
4820 xfs_iext_insert(ip, icur, &new, state);
4826 * Called by xfs_bmapi to update file extent records and the btree
4827 * after removing space.
4829 STATIC int /* error */
4830 xfs_bmap_del_extent_real(
4831 xfs_inode_t *ip, /* incore inode pointer */
4832 xfs_trans_t *tp, /* current transaction pointer */
4833 struct xfs_iext_cursor *icur,
4834 struct xfs_defer_ops *dfops, /* list of extents to be freed */
4835 xfs_btree_cur_t *cur, /* if null, not a btree */
4836 xfs_bmbt_irec_t *del, /* data to remove from extents */
4837 int *logflagsp, /* inode logging flags */
4838 int whichfork, /* data or attr fork */
4839 int bflags) /* bmapi flags */
4841 xfs_fsblock_t del_endblock=0; /* first block past del */
4842 xfs_fileoff_t del_endoff; /* first offset past del */
4843 int do_fx; /* free extent at end of routine */
4844 int error; /* error return value */
4845 int flags = 0;/* inode logging flags */
4846 struct xfs_bmbt_irec got; /* current extent entry */
4847 xfs_fileoff_t got_endoff; /* first offset past got */
4848 int i; /* temp state */
4849 xfs_ifork_t *ifp; /* inode fork pointer */
4850 xfs_mount_t *mp; /* mount structure */
4851 xfs_filblks_t nblks; /* quota/sb block count */
4852 xfs_bmbt_irec_t new; /* new record to be inserted */
4854 uint qfield; /* quota field to update */
4855 int state = xfs_bmap_fork_to_state(whichfork);
4856 struct xfs_bmbt_irec old;
4859 XFS_STATS_INC(mp, xs_del_exlist);
4861 ifp = XFS_IFORK_PTR(ip, whichfork);
4862 ASSERT(del->br_blockcount > 0);
4863 xfs_iext_get_extent(ifp, icur, &got);
4864 ASSERT(got.br_startoff <= del->br_startoff);
4865 del_endoff = del->br_startoff + del->br_blockcount;
4866 got_endoff = got.br_startoff + got.br_blockcount;
4867 ASSERT(got_endoff >= del_endoff);
4868 ASSERT(!isnullstartblock(got.br_startblock));
4873 * If it's the case where the directory code is running with no block
4874 * reservation, and the deleted block is in the middle of its extent,
4875 * and the resulting insert of an extent would cause transformation to
4876 * btree format, then reject it. The calling code will then swap blocks
4877 * around instead. We have to do this now, rather than waiting for the
4878 * conversion to btree format, since the transaction will be dirty then.
4880 if (tp->t_blk_res == 0 &&
4881 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
4882 XFS_IFORK_NEXTENTS(ip, whichfork) >=
4883 XFS_IFORK_MAXEXT(ip, whichfork) &&
4884 del->br_startoff > got.br_startoff && del_endoff < got_endoff)
4887 flags = XFS_ILOG_CORE;
4888 if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) {
4892 ASSERT(do_mod(del->br_blockcount, mp->m_sb.sb_rextsize) == 0);
4893 ASSERT(do_mod(del->br_startblock, mp->m_sb.sb_rextsize) == 0);
4894 bno = del->br_startblock;
4895 len = del->br_blockcount;
4896 do_div(bno, mp->m_sb.sb_rextsize);
4897 do_div(len, mp->m_sb.sb_rextsize);
4898 error = xfs_rtfree_extent(tp, bno, (xfs_extlen_t)len);
4902 nblks = len * mp->m_sb.sb_rextsize;
4903 qfield = XFS_TRANS_DQ_RTBCOUNT;
4906 nblks = del->br_blockcount;
4907 qfield = XFS_TRANS_DQ_BCOUNT;
4910 del_endblock = del->br_startblock + del->br_blockcount;
4912 error = xfs_bmbt_lookup_eq(cur, &got, &i);
4915 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
4918 if (got.br_startoff == del->br_startoff)
4919 state |= BMAP_LEFT_FILLING;
4920 if (got_endoff == del_endoff)
4921 state |= BMAP_RIGHT_FILLING;
4923 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
4924 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
4926 * Matches the whole extent. Delete the entry.
4928 xfs_iext_remove(ip, icur, state);
4929 xfs_iext_prev(ifp, icur);
4930 XFS_IFORK_NEXT_SET(ip, whichfork,
4931 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
4932 flags |= XFS_ILOG_CORE;
4934 flags |= xfs_ilog_fext(whichfork);
4937 if ((error = xfs_btree_delete(cur, &i)))
4939 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
4941 case BMAP_LEFT_FILLING:
4943 * Deleting the first part of the extent.
4945 got.br_startoff = del_endoff;
4946 got.br_startblock = del_endblock;
4947 got.br_blockcount -= del->br_blockcount;
4948 xfs_iext_update_extent(ip, state, icur, &got);
4950 flags |= xfs_ilog_fext(whichfork);
4953 error = xfs_bmbt_update(cur, &got);
4957 case BMAP_RIGHT_FILLING:
4959 * Deleting the last part of the extent.
4961 got.br_blockcount -= del->br_blockcount;
4962 xfs_iext_update_extent(ip, state, icur, &got);
4964 flags |= xfs_ilog_fext(whichfork);
4967 error = xfs_bmbt_update(cur, &got);
4973 * Deleting the middle of the extent.
4977 got.br_blockcount = del->br_startoff - got.br_startoff;
4978 xfs_iext_update_extent(ip, state, icur, &got);
4980 new.br_startoff = del_endoff;
4981 new.br_blockcount = got_endoff - del_endoff;
4982 new.br_state = got.br_state;
4983 new.br_startblock = del_endblock;
4985 flags |= XFS_ILOG_CORE;
4987 error = xfs_bmbt_update(cur, &got);
4990 error = xfs_btree_increment(cur, 0, &i);
4993 cur->bc_rec.b = new;
4994 error = xfs_btree_insert(cur, &i);
4995 if (error && error != -ENOSPC)
4998 * If get no-space back from btree insert, it tried a
4999 * split, and we have a zero block reservation. Fix up
5000 * our state and return the error.
5002 if (error == -ENOSPC) {
5004 * Reset the cursor, don't trust it after any
5007 error = xfs_bmbt_lookup_eq(cur, &got, &i);
5010 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
5012 * Update the btree record back
5013 * to the original value.
5015 error = xfs_bmbt_update(cur, &old);
5019 * Reset the extent record back
5020 * to the original value.
5022 xfs_iext_update_extent(ip, state, icur, &old);
5027 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
5029 flags |= xfs_ilog_fext(whichfork);
5030 XFS_IFORK_NEXT_SET(ip, whichfork,
5031 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
5032 xfs_iext_next(ifp, icur);
5033 xfs_iext_insert(ip, icur, &new, state);
5037 /* remove reverse mapping */
5038 error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, del);
5043 * If we need to, add to list of extents to delete.
5045 if (do_fx && !(bflags & XFS_BMAPI_REMAP)) {
5046 if (xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK) {
5047 error = xfs_refcount_decrease_extent(mp, dfops, del);
5051 xfs_bmap_add_free(mp, dfops, del->br_startblock,
5052 del->br_blockcount, NULL);
5056 * Adjust inode # blocks in the file.
5059 ip->i_d.di_nblocks -= nblks;
5061 * Adjust quota data.
5063 if (qfield && !(bflags & XFS_BMAPI_REMAP))
5064 xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks);
5072 * Unmap (remove) blocks from a file.
5073 * If nexts is nonzero then the number of extents to remove is limited to
5074 * that value. If not all extents in the block range can be removed then
5079 xfs_trans_t *tp, /* transaction pointer */
5080 struct xfs_inode *ip, /* incore inode */
5081 xfs_fileoff_t start, /* first file offset deleted */
5082 xfs_filblks_t *rlen, /* i/o: amount remaining */
5083 int flags, /* misc flags */
5084 xfs_extnum_t nexts, /* number of extents max */
5085 xfs_fsblock_t *firstblock, /* first allocated block
5086 controls a.g. for allocs */
5087 struct xfs_defer_ops *dfops) /* i/o: deferred updates */
5089 xfs_btree_cur_t *cur; /* bmap btree cursor */
5090 xfs_bmbt_irec_t del; /* extent being deleted */
5091 int error; /* error return value */
5092 xfs_extnum_t extno; /* extent number in list */
5093 xfs_bmbt_irec_t got; /* current extent record */
5094 xfs_ifork_t *ifp; /* inode fork pointer */
5095 int isrt; /* freeing in rt area */
5096 int logflags; /* transaction logging flags */
5097 xfs_extlen_t mod; /* rt extent offset */
5098 xfs_mount_t *mp; /* mount structure */
5099 int tmp_logflags; /* partial logging flags */
5100 int wasdel; /* was a delayed alloc extent */
5101 int whichfork; /* data or attribute fork */
5103 xfs_filblks_t len = *rlen; /* length to unmap in file */
5104 xfs_fileoff_t max_len;
5105 xfs_agnumber_t prev_agno = NULLAGNUMBER, agno;
5107 struct xfs_iext_cursor icur;
5110 trace_xfs_bunmap(ip, start, len, flags, _RET_IP_);
5112 whichfork = xfs_bmapi_whichfork(flags);
5113 ASSERT(whichfork != XFS_COW_FORK);
5114 ifp = XFS_IFORK_PTR(ip, whichfork);
5116 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5117 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
5118 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW,
5120 return -EFSCORRUPTED;
5123 if (XFS_FORCED_SHUTDOWN(mp))
5126 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
5131 * Guesstimate how many blocks we can unmap without running the risk of
5132 * blowing out the transaction with a mix of EFIs and reflink
5135 if (tp && xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK)
5136 max_len = min(len, xfs_refcount_max_unmap(tp->t_log_res));
5140 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5141 (error = xfs_iread_extents(tp, ip, whichfork)))
5143 if (xfs_iext_count(ifp) == 0) {
5147 XFS_STATS_INC(mp, xs_blk_unmap);
5148 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
5151 if (!xfs_iext_lookup_extent_before(ip, ifp, &end, &icur, &got)) {
5158 if (ifp->if_flags & XFS_IFBROOT) {
5159 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
5160 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5161 cur->bc_private.b.firstblock = *firstblock;
5162 cur->bc_private.b.dfops = dfops;
5163 cur->bc_private.b.flags = 0;
5169 * Synchronize by locking the bitmap inode.
5171 xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL|XFS_ILOCK_RTBITMAP);
5172 xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL);
5173 xfs_ilock(mp->m_rsumip, XFS_ILOCK_EXCL|XFS_ILOCK_RTSUM);
5174 xfs_trans_ijoin(tp, mp->m_rsumip, XFS_ILOCK_EXCL);
5178 while (end != (xfs_fileoff_t)-1 && end >= start &&
5179 (nexts == 0 || extno < nexts) && max_len > 0) {
5181 * Is the found extent after a hole in which end lives?
5182 * Just back up to the previous extent, if so.
5184 if (got.br_startoff > end &&
5185 !xfs_iext_prev_extent(ifp, &icur, &got)) {
5190 * Is the last block of this extent before the range
5191 * we're supposed to delete? If so, we're done.
5193 end = XFS_FILEOFF_MIN(end,
5194 got.br_startoff + got.br_blockcount - 1);
5198 * Then deal with the (possibly delayed) allocated space
5202 wasdel = isnullstartblock(del.br_startblock);
5205 * Make sure we don't touch multiple AGF headers out of order
5206 * in a single transaction, as that could cause AB-BA deadlocks.
5209 agno = XFS_FSB_TO_AGNO(mp, del.br_startblock);
5210 if (prev_agno != NULLAGNUMBER && prev_agno > agno)
5214 if (got.br_startoff < start) {
5215 del.br_startoff = start;
5216 del.br_blockcount -= start - got.br_startoff;
5218 del.br_startblock += start - got.br_startoff;
5220 if (del.br_startoff + del.br_blockcount > end + 1)
5221 del.br_blockcount = end + 1 - del.br_startoff;
5223 /* How much can we safely unmap? */
5224 if (max_len < del.br_blockcount) {
5225 del.br_startoff += del.br_blockcount - max_len;
5227 del.br_startblock += del.br_blockcount - max_len;
5228 del.br_blockcount = max_len;
5231 sum = del.br_startblock + del.br_blockcount;
5233 (mod = do_mod(sum, mp->m_sb.sb_rextsize))) {
5235 * Realtime extent not lined up at the end.
5236 * The extent could have been split into written
5237 * and unwritten pieces, or we could just be
5238 * unmapping part of it. But we can't really
5239 * get rid of part of a realtime extent.
5241 if (del.br_state == XFS_EXT_UNWRITTEN ||
5242 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
5244 * This piece is unwritten, or we're not
5245 * using unwritten extents. Skip over it.
5248 end -= mod > del.br_blockcount ?
5249 del.br_blockcount : mod;
5250 if (end < got.br_startoff &&
5251 !xfs_iext_prev_extent(ifp, &icur, &got)) {
5258 * It's written, turn it unwritten.
5259 * This is better than zeroing it.
5261 ASSERT(del.br_state == XFS_EXT_NORM);
5262 ASSERT(tp->t_blk_res > 0);
5264 * If this spans a realtime extent boundary,
5265 * chop it back to the start of the one we end at.
5267 if (del.br_blockcount > mod) {
5268 del.br_startoff += del.br_blockcount - mod;
5269 del.br_startblock += del.br_blockcount - mod;
5270 del.br_blockcount = mod;
5272 del.br_state = XFS_EXT_UNWRITTEN;
5273 error = xfs_bmap_add_extent_unwritten_real(tp, ip,
5274 whichfork, &icur, &cur, &del,
5275 firstblock, dfops, &logflags);
5280 if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) {
5282 * Realtime extent is lined up at the end but not
5283 * at the front. We'll get rid of full extents if
5286 mod = mp->m_sb.sb_rextsize - mod;
5287 if (del.br_blockcount > mod) {
5288 del.br_blockcount -= mod;
5289 del.br_startoff += mod;
5290 del.br_startblock += mod;
5291 } else if ((del.br_startoff == start &&
5292 (del.br_state == XFS_EXT_UNWRITTEN ||
5293 tp->t_blk_res == 0)) ||
5294 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
5296 * Can't make it unwritten. There isn't
5297 * a full extent here so just skip it.
5299 ASSERT(end >= del.br_blockcount);
5300 end -= del.br_blockcount;
5301 if (got.br_startoff > end &&
5302 !xfs_iext_prev_extent(ifp, &icur, &got)) {
5307 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
5308 struct xfs_bmbt_irec prev;
5311 * This one is already unwritten.
5312 * It must have a written left neighbor.
5313 * Unwrite the killed part of that one and
5316 if (!xfs_iext_prev_extent(ifp, &icur, &prev))
5318 ASSERT(prev.br_state == XFS_EXT_NORM);
5319 ASSERT(!isnullstartblock(prev.br_startblock));
5320 ASSERT(del.br_startblock ==
5321 prev.br_startblock + prev.br_blockcount);
5322 if (prev.br_startoff < start) {
5323 mod = start - prev.br_startoff;
5324 prev.br_blockcount -= mod;
5325 prev.br_startblock += mod;
5326 prev.br_startoff = start;
5328 prev.br_state = XFS_EXT_UNWRITTEN;
5329 error = xfs_bmap_add_extent_unwritten_real(tp,
5330 ip, whichfork, &icur, &cur,
5331 &prev, firstblock, dfops,
5337 ASSERT(del.br_state == XFS_EXT_NORM);
5338 del.br_state = XFS_EXT_UNWRITTEN;
5339 error = xfs_bmap_add_extent_unwritten_real(tp,
5340 ip, whichfork, &icur, &cur,
5341 &del, firstblock, dfops,
5350 error = xfs_bmap_del_extent_delay(ip, whichfork, &icur,
5353 error = xfs_bmap_del_extent_real(ip, tp, &icur, dfops,
5354 cur, &del, &tmp_logflags, whichfork,
5356 logflags |= tmp_logflags;
5362 max_len -= del.br_blockcount;
5363 end = del.br_startoff - 1;
5366 * If not done go on to the next (previous) record.
5368 if (end != (xfs_fileoff_t)-1 && end >= start) {
5369 if (!xfs_iext_get_extent(ifp, &icur, &got) ||
5370 (got.br_startoff > end &&
5371 !xfs_iext_prev_extent(ifp, &icur, &got))) {
5378 if (done || end == (xfs_fileoff_t)-1 || end < start)
5381 *rlen = end - start + 1;
5384 * Convert to a btree if necessary.
5386 if (xfs_bmap_needs_btree(ip, whichfork)) {
5387 ASSERT(cur == NULL);
5388 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, dfops,
5389 &cur, 0, &tmp_logflags, whichfork);
5390 logflags |= tmp_logflags;
5395 * transform from btree to extents, give it cur
5397 else if (xfs_bmap_wants_extents(ip, whichfork)) {
5398 ASSERT(cur != NULL);
5399 error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags,
5401 logflags |= tmp_logflags;
5406 * transform from extents to local?
5411 * Log everything. Do this after conversion, there's no point in
5412 * logging the extent records if we've converted to btree format.
5414 if ((logflags & xfs_ilog_fext(whichfork)) &&
5415 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
5416 logflags &= ~xfs_ilog_fext(whichfork);
5417 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
5418 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
5419 logflags &= ~xfs_ilog_fbroot(whichfork);
5421 * Log inode even in the error case, if the transaction
5422 * is dirty we'll need to shut down the filesystem.
5425 xfs_trans_log_inode(tp, ip, logflags);
5428 *firstblock = cur->bc_private.b.firstblock;
5429 cur->bc_private.b.allocated = 0;
5431 xfs_btree_del_cursor(cur,
5432 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5437 /* Unmap a range of a file. */
5441 struct xfs_inode *ip,
5446 xfs_fsblock_t *firstblock,
5447 struct xfs_defer_ops *dfops,
5452 error = __xfs_bunmapi(tp, ip, bno, &len, flags, nexts, firstblock,
5459 * Determine whether an extent shift can be accomplished by a merge with the
5460 * extent that precedes the target hole of the shift.
5464 struct xfs_bmbt_irec *left, /* preceding extent */
5465 struct xfs_bmbt_irec *got, /* current extent to shift */
5466 xfs_fileoff_t shift) /* shift fsb */
5468 xfs_fileoff_t startoff;
5470 startoff = got->br_startoff - shift;
5473 * The extent, once shifted, must be adjacent in-file and on-disk with
5474 * the preceding extent.
5476 if ((left->br_startoff + left->br_blockcount != startoff) ||
5477 (left->br_startblock + left->br_blockcount != got->br_startblock) ||
5478 (left->br_state != got->br_state) ||
5479 (left->br_blockcount + got->br_blockcount > MAXEXTLEN))
5486 * A bmap extent shift adjusts the file offset of an extent to fill a preceding
5487 * hole in the file. If an extent shift would result in the extent being fully
5488 * adjacent to the extent that currently precedes the hole, we can merge with
5489 * the preceding extent rather than do the shift.
5491 * This function assumes the caller has verified a shift-by-merge is possible
5492 * with the provided extents via xfs_bmse_can_merge().
5496 struct xfs_inode *ip,
5498 xfs_fileoff_t shift, /* shift fsb */
5499 struct xfs_iext_cursor *icur,
5500 struct xfs_bmbt_irec *got, /* extent to shift */
5501 struct xfs_bmbt_irec *left, /* preceding extent */
5502 struct xfs_btree_cur *cur,
5503 int *logflags, /* output */
5504 struct xfs_defer_ops *dfops)
5506 struct xfs_bmbt_irec new;
5507 xfs_filblks_t blockcount;
5509 struct xfs_mount *mp = ip->i_mount;
5511 blockcount = left->br_blockcount + got->br_blockcount;
5513 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
5514 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
5515 ASSERT(xfs_bmse_can_merge(left, got, shift));
5518 new.br_blockcount = blockcount;
5521 * Update the on-disk extent count, the btree if necessary and log the
5524 XFS_IFORK_NEXT_SET(ip, whichfork,
5525 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
5526 *logflags |= XFS_ILOG_CORE;
5528 *logflags |= XFS_ILOG_DEXT;
5532 /* lookup and remove the extent to merge */
5533 error = xfs_bmbt_lookup_eq(cur, got, &i);
5536 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
5538 error = xfs_btree_delete(cur, &i);
5541 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
5543 /* lookup and update size of the previous extent */
5544 error = xfs_bmbt_lookup_eq(cur, left, &i);
5547 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
5549 error = xfs_bmbt_update(cur, &new);
5554 xfs_iext_remove(ip, icur, 0);
5555 xfs_iext_prev(XFS_IFORK_PTR(ip, whichfork), icur);
5556 xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), icur,
5559 /* update reverse mapping. rmap functions merge the rmaps for us */
5560 error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, got);
5563 memcpy(&new, got, sizeof(new));
5564 new.br_startoff = left->br_startoff + left->br_blockcount;
5565 return xfs_rmap_map_extent(mp, dfops, ip, whichfork, &new);
5569 xfs_bmap_shift_update_extent(
5570 struct xfs_inode *ip,
5572 struct xfs_iext_cursor *icur,
5573 struct xfs_bmbt_irec *got,
5574 struct xfs_btree_cur *cur,
5576 struct xfs_defer_ops *dfops,
5577 xfs_fileoff_t startoff)
5579 struct xfs_mount *mp = ip->i_mount;
5580 struct xfs_bmbt_irec prev = *got;
5583 *logflags |= XFS_ILOG_CORE;
5585 got->br_startoff = startoff;
5588 error = xfs_bmbt_lookup_eq(cur, &prev, &i);
5591 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
5593 error = xfs_bmbt_update(cur, got);
5597 *logflags |= XFS_ILOG_DEXT;
5600 xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), icur,
5603 /* update reverse mapping */
5604 error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, &prev);
5607 return xfs_rmap_map_extent(mp, dfops, ip, whichfork, got);
5611 xfs_bmap_collapse_extents(
5612 struct xfs_trans *tp,
5613 struct xfs_inode *ip,
5614 xfs_fileoff_t *next_fsb,
5615 xfs_fileoff_t offset_shift_fsb,
5617 xfs_fileoff_t stop_fsb,
5618 xfs_fsblock_t *firstblock,
5619 struct xfs_defer_ops *dfops)
5621 int whichfork = XFS_DATA_FORK;
5622 struct xfs_mount *mp = ip->i_mount;
5623 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
5624 struct xfs_btree_cur *cur = NULL;
5625 struct xfs_bmbt_irec got, prev;
5626 struct xfs_iext_cursor icur;
5627 xfs_fileoff_t new_startoff;
5631 if (unlikely(XFS_TEST_ERROR(
5632 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5633 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
5634 mp, XFS_ERRTAG_BMAPIFORMAT))) {
5635 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp);
5636 return -EFSCORRUPTED;
5639 if (XFS_FORCED_SHUTDOWN(mp))
5642 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL));
5644 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
5645 error = xfs_iread_extents(tp, ip, whichfork);
5650 if (ifp->if_flags & XFS_IFBROOT) {
5651 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5652 cur->bc_private.b.firstblock = *firstblock;
5653 cur->bc_private.b.dfops = dfops;
5654 cur->bc_private.b.flags = 0;
5657 if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &icur, &got)) {
5661 XFS_WANT_CORRUPTED_GOTO(mp, !isnullstartblock(got.br_startblock),
5664 new_startoff = got.br_startoff - offset_shift_fsb;
5665 if (xfs_iext_peek_prev_extent(ifp, &icur, &prev)) {
5666 if (new_startoff < prev.br_startoff + prev.br_blockcount) {
5671 if (xfs_bmse_can_merge(&prev, &got, offset_shift_fsb)) {
5672 error = xfs_bmse_merge(ip, whichfork, offset_shift_fsb,
5673 &icur, &got, &prev, cur, &logflags,
5680 if (got.br_startoff < offset_shift_fsb) {
5686 error = xfs_bmap_shift_update_extent(ip, whichfork, &icur, &got, cur,
5687 &logflags, dfops, new_startoff);
5692 if (!xfs_iext_next_extent(ifp, &icur, &got)) {
5697 *next_fsb = got.br_startoff;
5700 xfs_btree_del_cursor(cur,
5701 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5703 xfs_trans_log_inode(tp, ip, logflags);
5708 xfs_bmap_insert_extents(
5709 struct xfs_trans *tp,
5710 struct xfs_inode *ip,
5711 xfs_fileoff_t *next_fsb,
5712 xfs_fileoff_t offset_shift_fsb,
5714 xfs_fileoff_t stop_fsb,
5715 xfs_fsblock_t *firstblock,
5716 struct xfs_defer_ops *dfops)
5718 int whichfork = XFS_DATA_FORK;
5719 struct xfs_mount *mp = ip->i_mount;
5720 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
5721 struct xfs_btree_cur *cur = NULL;
5722 struct xfs_bmbt_irec got, next;
5723 struct xfs_iext_cursor icur;
5724 xfs_fileoff_t new_startoff;
5728 if (unlikely(XFS_TEST_ERROR(
5729 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5730 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
5731 mp, XFS_ERRTAG_BMAPIFORMAT))) {
5732 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp);
5733 return -EFSCORRUPTED;
5736 if (XFS_FORCED_SHUTDOWN(mp))
5739 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL));
5741 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
5742 error = xfs_iread_extents(tp, ip, whichfork);
5747 if (ifp->if_flags & XFS_IFBROOT) {
5748 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5749 cur->bc_private.b.firstblock = *firstblock;
5750 cur->bc_private.b.dfops = dfops;
5751 cur->bc_private.b.flags = 0;
5754 if (*next_fsb == NULLFSBLOCK) {
5755 xfs_iext_last(ifp, &icur);
5756 if (!xfs_iext_get_extent(ifp, &icur, &got) ||
5757 stop_fsb > got.br_startoff) {
5762 if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &icur, &got)) {
5767 XFS_WANT_CORRUPTED_GOTO(mp, !isnullstartblock(got.br_startblock),
5770 if (stop_fsb >= got.br_startoff + got.br_blockcount) {
5775 new_startoff = got.br_startoff + offset_shift_fsb;
5776 if (xfs_iext_peek_next_extent(ifp, &icur, &next)) {
5777 if (new_startoff + got.br_blockcount > next.br_startoff) {
5783 * Unlike a left shift (which involves a hole punch), a right
5784 * shift does not modify extent neighbors in any way. We should
5785 * never find mergeable extents in this scenario. Check anyways
5786 * and warn if we encounter two extents that could be one.
5788 if (xfs_bmse_can_merge(&got, &next, offset_shift_fsb))
5792 error = xfs_bmap_shift_update_extent(ip, whichfork, &icur, &got, cur,
5793 &logflags, dfops, new_startoff);
5797 if (!xfs_iext_prev_extent(ifp, &icur, &got) ||
5798 stop_fsb >= got.br_startoff + got.br_blockcount) {
5803 *next_fsb = got.br_startoff;
5806 xfs_btree_del_cursor(cur,
5807 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5809 xfs_trans_log_inode(tp, ip, logflags);
5814 * Splits an extent into two extents at split_fsb block such that it is the
5815 * first block of the current_ext. @ext is a target extent to be split.
5816 * @split_fsb is a block where the extents is split. If split_fsb lies in a
5817 * hole or the first block of extents, just return 0.
5820 xfs_bmap_split_extent_at(
5821 struct xfs_trans *tp,
5822 struct xfs_inode *ip,
5823 xfs_fileoff_t split_fsb,
5824 xfs_fsblock_t *firstfsb,
5825 struct xfs_defer_ops *dfops)
5827 int whichfork = XFS_DATA_FORK;
5828 struct xfs_btree_cur *cur = NULL;
5829 struct xfs_bmbt_irec got;
5830 struct xfs_bmbt_irec new; /* split extent */
5831 struct xfs_mount *mp = ip->i_mount;
5832 struct xfs_ifork *ifp;
5833 xfs_fsblock_t gotblkcnt; /* new block count for got */
5834 struct xfs_iext_cursor icur;
5839 if (unlikely(XFS_TEST_ERROR(
5840 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5841 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
5842 mp, XFS_ERRTAG_BMAPIFORMAT))) {
5843 XFS_ERROR_REPORT("xfs_bmap_split_extent_at",
5844 XFS_ERRLEVEL_LOW, mp);
5845 return -EFSCORRUPTED;
5848 if (XFS_FORCED_SHUTDOWN(mp))
5851 ifp = XFS_IFORK_PTR(ip, whichfork);
5852 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
5853 /* Read in all the extents */
5854 error = xfs_iread_extents(tp, ip, whichfork);
5860 * If there are not extents, or split_fsb lies in a hole we are done.
5862 if (!xfs_iext_lookup_extent(ip, ifp, split_fsb, &icur, &got) ||
5863 got.br_startoff >= split_fsb)
5866 gotblkcnt = split_fsb - got.br_startoff;
5867 new.br_startoff = split_fsb;
5868 new.br_startblock = got.br_startblock + gotblkcnt;
5869 new.br_blockcount = got.br_blockcount - gotblkcnt;
5870 new.br_state = got.br_state;
5872 if (ifp->if_flags & XFS_IFBROOT) {
5873 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5874 cur->bc_private.b.firstblock = *firstfsb;
5875 cur->bc_private.b.dfops = dfops;
5876 cur->bc_private.b.flags = 0;
5877 error = xfs_bmbt_lookup_eq(cur, &got, &i);
5880 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor);
5883 got.br_blockcount = gotblkcnt;
5884 xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), &icur,
5887 logflags = XFS_ILOG_CORE;
5889 error = xfs_bmbt_update(cur, &got);
5893 logflags |= XFS_ILOG_DEXT;
5895 /* Add new extent */
5896 xfs_iext_next(ifp, &icur);
5897 xfs_iext_insert(ip, &icur, &new, 0);
5898 XFS_IFORK_NEXT_SET(ip, whichfork,
5899 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
5902 error = xfs_bmbt_lookup_eq(cur, &new, &i);
5905 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, del_cursor);
5906 error = xfs_btree_insert(cur, &i);
5909 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor);
5913 * Convert to a btree if necessary.
5915 if (xfs_bmap_needs_btree(ip, whichfork)) {
5916 int tmp_logflags; /* partial log flag return val */
5918 ASSERT(cur == NULL);
5919 error = xfs_bmap_extents_to_btree(tp, ip, firstfsb, dfops,
5920 &cur, 0, &tmp_logflags, whichfork);
5921 logflags |= tmp_logflags;
5926 cur->bc_private.b.allocated = 0;
5927 xfs_btree_del_cursor(cur,
5928 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5932 xfs_trans_log_inode(tp, ip, logflags);
5937 xfs_bmap_split_extent(
5938 struct xfs_inode *ip,
5939 xfs_fileoff_t split_fsb)
5941 struct xfs_mount *mp = ip->i_mount;
5942 struct xfs_trans *tp;
5943 struct xfs_defer_ops dfops;
5944 xfs_fsblock_t firstfsb;
5947 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write,
5948 XFS_DIOSTRAT_SPACE_RES(mp, 0), 0, 0, &tp);
5952 xfs_ilock(ip, XFS_ILOCK_EXCL);
5953 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
5955 xfs_defer_init(&dfops, &firstfsb);
5957 error = xfs_bmap_split_extent_at(tp, ip, split_fsb,
5962 error = xfs_defer_finish(&tp, &dfops);
5966 return xfs_trans_commit(tp);
5969 xfs_defer_cancel(&dfops);
5970 xfs_trans_cancel(tp);
5974 /* Deferred mapping is only for real extents in the data fork. */
5976 xfs_bmap_is_update_needed(
5977 struct xfs_bmbt_irec *bmap)
5979 return bmap->br_startblock != HOLESTARTBLOCK &&
5980 bmap->br_startblock != DELAYSTARTBLOCK;
5983 /* Record a bmap intent. */
5986 struct xfs_mount *mp,
5987 struct xfs_defer_ops *dfops,
5988 enum xfs_bmap_intent_type type,
5989 struct xfs_inode *ip,
5991 struct xfs_bmbt_irec *bmap)
5994 struct xfs_bmap_intent *bi;
5996 trace_xfs_bmap_defer(mp,
5997 XFS_FSB_TO_AGNO(mp, bmap->br_startblock),
5999 XFS_FSB_TO_AGBNO(mp, bmap->br_startblock),
6000 ip->i_ino, whichfork,
6002 bmap->br_blockcount,
6005 bi = kmem_alloc(sizeof(struct xfs_bmap_intent), KM_SLEEP | KM_NOFS);
6006 INIT_LIST_HEAD(&bi->bi_list);
6009 bi->bi_whichfork = whichfork;
6010 bi->bi_bmap = *bmap;
6012 error = xfs_defer_ijoin(dfops, bi->bi_owner);
6018 xfs_defer_add(dfops, XFS_DEFER_OPS_TYPE_BMAP, &bi->bi_list);
6022 /* Map an extent into a file. */
6024 xfs_bmap_map_extent(
6025 struct xfs_mount *mp,
6026 struct xfs_defer_ops *dfops,
6027 struct xfs_inode *ip,
6028 struct xfs_bmbt_irec *PREV)
6030 if (!xfs_bmap_is_update_needed(PREV))
6033 return __xfs_bmap_add(mp, dfops, XFS_BMAP_MAP, ip,
6034 XFS_DATA_FORK, PREV);
6037 /* Unmap an extent out of a file. */
6039 xfs_bmap_unmap_extent(
6040 struct xfs_mount *mp,
6041 struct xfs_defer_ops *dfops,
6042 struct xfs_inode *ip,
6043 struct xfs_bmbt_irec *PREV)
6045 if (!xfs_bmap_is_update_needed(PREV))
6048 return __xfs_bmap_add(mp, dfops, XFS_BMAP_UNMAP, ip,
6049 XFS_DATA_FORK, PREV);
6053 * Process one of the deferred bmap operations. We pass back the
6054 * btree cursor to maintain our lock on the bmapbt between calls.
6057 xfs_bmap_finish_one(
6058 struct xfs_trans *tp,
6059 struct xfs_defer_ops *dfops,
6060 struct xfs_inode *ip,
6061 enum xfs_bmap_intent_type type,
6063 xfs_fileoff_t startoff,
6064 xfs_fsblock_t startblock,
6065 xfs_filblks_t *blockcount,
6068 xfs_fsblock_t firstfsb;
6072 * firstfsb is tied to the transaction lifetime and is used to
6073 * ensure correct AG locking order and schedule work item
6074 * continuations. XFS_BUI_MAX_FAST_EXTENTS (== 1) restricts us
6075 * to only making one bmap call per transaction, so it should
6076 * be safe to have it as a local variable here.
6078 firstfsb = NULLFSBLOCK;
6080 trace_xfs_bmap_deferred(tp->t_mountp,
6081 XFS_FSB_TO_AGNO(tp->t_mountp, startblock), type,
6082 XFS_FSB_TO_AGBNO(tp->t_mountp, startblock),
6083 ip->i_ino, whichfork, startoff, *blockcount, state);
6085 if (WARN_ON_ONCE(whichfork != XFS_DATA_FORK))
6086 return -EFSCORRUPTED;
6088 if (XFS_TEST_ERROR(false, tp->t_mountp,
6089 XFS_ERRTAG_BMAP_FINISH_ONE))
6094 error = xfs_bmapi_remap(tp, ip, startoff, *blockcount,
6098 case XFS_BMAP_UNMAP:
6099 error = __xfs_bunmapi(tp, ip, startoff, blockcount,
6100 XFS_BMAPI_REMAP, 1, &firstfsb, dfops);
6104 error = -EFSCORRUPTED;