1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
8 #include "xfs_shared.h"
9 #include "xfs_format.h"
10 #include "xfs_log_format.h"
11 #include "xfs_trans_resv.h"
14 #include "xfs_mount.h"
15 #include "xfs_defer.h"
16 #include "xfs_da_format.h"
17 #include "xfs_da_btree.h"
19 #include "xfs_inode.h"
20 #include "xfs_btree.h"
21 #include "xfs_trans.h"
22 #include "xfs_inode_item.h"
23 #include "xfs_extfree_item.h"
24 #include "xfs_alloc.h"
26 #include "xfs_bmap_util.h"
27 #include "xfs_bmap_btree.h"
28 #include "xfs_rtalloc.h"
29 #include "xfs_errortag.h"
30 #include "xfs_error.h"
31 #include "xfs_quota.h"
32 #include "xfs_trans_space.h"
33 #include "xfs_buf_item.h"
34 #include "xfs_trace.h"
35 #include "xfs_symlink.h"
36 #include "xfs_attr_leaf.h"
37 #include "xfs_filestream.h"
39 #include "xfs_ag_resv.h"
40 #include "xfs_refcount.h"
41 #include "xfs_icache.h"
44 kmem_zone_t *xfs_bmap_free_item_zone;
47 * Miscellaneous helper functions
51 * Compute and fill in the value of the maximum depth of a bmap btree
52 * in this filesystem. Done once, during mount.
55 xfs_bmap_compute_maxlevels(
56 xfs_mount_t *mp, /* file system mount structure */
57 int whichfork) /* data or attr fork */
59 int level; /* btree level */
60 uint maxblocks; /* max blocks at this level */
61 uint maxleafents; /* max leaf entries possible */
62 int maxrootrecs; /* max records in root block */
63 int minleafrecs; /* min records in leaf block */
64 int minnoderecs; /* min records in node block */
65 int sz; /* root block size */
68 * The maximum number of extents in a file, hence the maximum
69 * number of leaf entries, is controlled by the type of di_nextents
70 * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
71 * (a signed 16-bit number, xfs_aextnum_t).
73 * Note that we can no longer assume that if we are in ATTR1 that
74 * the fork offset of all the inodes will be
75 * (xfs_default_attroffset(ip) >> 3) because we could have mounted
76 * with ATTR2 and then mounted back with ATTR1, keeping the
77 * di_forkoff's fixed but probably at various positions. Therefore,
78 * for both ATTR1 and ATTR2 we have to assume the worst case scenario
79 * of a minimum size available.
81 if (whichfork == XFS_DATA_FORK) {
82 maxleafents = MAXEXTNUM;
83 sz = XFS_BMDR_SPACE_CALC(MINDBTPTRS);
85 maxleafents = MAXAEXTNUM;
86 sz = XFS_BMDR_SPACE_CALC(MINABTPTRS);
88 maxrootrecs = xfs_bmdr_maxrecs(sz, 0);
89 minleafrecs = mp->m_bmap_dmnr[0];
90 minnoderecs = mp->m_bmap_dmnr[1];
91 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
92 for (level = 1; maxblocks > 1; level++) {
93 if (maxblocks <= maxrootrecs)
96 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
98 mp->m_bm_maxlevels[whichfork] = level;
101 STATIC int /* error */
103 struct xfs_btree_cur *cur,
104 struct xfs_bmbt_irec *irec,
105 int *stat) /* success/failure */
107 cur->bc_rec.b = *irec;
108 return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
111 STATIC int /* error */
112 xfs_bmbt_lookup_first(
113 struct xfs_btree_cur *cur,
114 int *stat) /* success/failure */
116 cur->bc_rec.b.br_startoff = 0;
117 cur->bc_rec.b.br_startblock = 0;
118 cur->bc_rec.b.br_blockcount = 0;
119 return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
123 * Check if the inode needs to be converted to btree format.
125 static inline bool xfs_bmap_needs_btree(struct xfs_inode *ip, int whichfork)
127 return whichfork != XFS_COW_FORK &&
128 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
129 XFS_IFORK_NEXTENTS(ip, whichfork) >
130 XFS_IFORK_MAXEXT(ip, whichfork);
134 * Check if the inode should be converted to extent format.
136 static inline bool xfs_bmap_wants_extents(struct xfs_inode *ip, int whichfork)
138 return whichfork != XFS_COW_FORK &&
139 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
140 XFS_IFORK_NEXTENTS(ip, whichfork) <=
141 XFS_IFORK_MAXEXT(ip, whichfork);
145 * Update the record referred to by cur to the value given by irec
146 * This either works (return 0) or gets an EFSCORRUPTED error.
150 struct xfs_btree_cur *cur,
151 struct xfs_bmbt_irec *irec)
153 union xfs_btree_rec rec;
155 xfs_bmbt_disk_set_all(&rec.bmbt, irec);
156 return xfs_btree_update(cur, &rec);
160 * Compute the worst-case number of indirect blocks that will be used
161 * for ip's delayed extent of length "len".
164 xfs_bmap_worst_indlen(
165 xfs_inode_t *ip, /* incore inode pointer */
166 xfs_filblks_t len) /* delayed extent length */
168 int level; /* btree level number */
169 int maxrecs; /* maximum record count at this level */
170 xfs_mount_t *mp; /* mount structure */
171 xfs_filblks_t rval; /* return value */
174 maxrecs = mp->m_bmap_dmxr[0];
175 for (level = 0, rval = 0;
176 level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK);
179 do_div(len, maxrecs);
182 return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) -
185 maxrecs = mp->m_bmap_dmxr[1];
191 * Calculate the default attribute fork offset for newly created inodes.
194 xfs_default_attroffset(
195 struct xfs_inode *ip)
197 struct xfs_mount *mp = ip->i_mount;
200 if (mp->m_sb.sb_inodesize == 256) {
201 offset = XFS_LITINO(mp, ip->i_d.di_version) -
202 XFS_BMDR_SPACE_CALC(MINABTPTRS);
204 offset = XFS_BMDR_SPACE_CALC(6 * MINABTPTRS);
207 ASSERT(offset < XFS_LITINO(mp, ip->i_d.di_version));
212 * Helper routine to reset inode di_forkoff field when switching
213 * attribute fork from local to extent format - we reset it where
214 * possible to make space available for inline data fork extents.
217 xfs_bmap_forkoff_reset(
221 if (whichfork == XFS_ATTR_FORK &&
222 ip->i_d.di_format != XFS_DINODE_FMT_DEV &&
223 ip->i_d.di_format != XFS_DINODE_FMT_BTREE) {
224 uint dfl_forkoff = xfs_default_attroffset(ip) >> 3;
226 if (dfl_forkoff > ip->i_d.di_forkoff)
227 ip->i_d.di_forkoff = dfl_forkoff;
232 STATIC struct xfs_buf *
234 struct xfs_btree_cur *cur,
237 struct xfs_log_item *lip;
243 for (i = 0; i < XFS_BTREE_MAXLEVELS; i++) {
244 if (!cur->bc_bufs[i])
246 if (XFS_BUF_ADDR(cur->bc_bufs[i]) == bno)
247 return cur->bc_bufs[i];
250 /* Chase down all the log items to see if the bp is there */
251 list_for_each_entry(lip, &cur->bc_tp->t_items, li_trans) {
252 struct xfs_buf_log_item *bip = (struct xfs_buf_log_item *)lip;
254 if (bip->bli_item.li_type == XFS_LI_BUF &&
255 XFS_BUF_ADDR(bip->bli_buf) == bno)
264 struct xfs_btree_block *block,
270 __be64 *pp, *thispa; /* pointer to block address */
271 xfs_bmbt_key_t *prevp, *keyp;
273 ASSERT(be16_to_cpu(block->bb_level) > 0);
276 for( i = 1; i <= xfs_btree_get_numrecs(block); i++) {
277 dmxr = mp->m_bmap_dmxr[0];
278 keyp = XFS_BMBT_KEY_ADDR(mp, block, i);
281 ASSERT(be64_to_cpu(prevp->br_startoff) <
282 be64_to_cpu(keyp->br_startoff));
287 * Compare the block numbers to see if there are dups.
290 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, i, sz);
292 pp = XFS_BMBT_PTR_ADDR(mp, block, i, dmxr);
294 for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) {
296 thispa = XFS_BMAP_BROOT_PTR_ADDR(mp, block, j, sz);
298 thispa = XFS_BMBT_PTR_ADDR(mp, block, j, dmxr);
299 if (*thispa == *pp) {
300 xfs_warn(mp, "%s: thispa(%d) == pp(%d) %Ld",
302 (unsigned long long)be64_to_cpu(*thispa));
303 xfs_err(mp, "%s: ptrs are equal in node\n",
305 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
312 * Check that the extents for the inode ip are in the right order in all
313 * btree leaves. THis becomes prohibitively expensive for large extent count
314 * files, so don't bother with inodes that have more than 10,000 extents in
315 * them. The btree record ordering checks will still be done, so for such large
316 * bmapbt constructs that is going to catch most corruptions.
319 xfs_bmap_check_leaf_extents(
320 xfs_btree_cur_t *cur, /* btree cursor or null */
321 xfs_inode_t *ip, /* incore inode pointer */
322 int whichfork) /* data or attr fork */
324 struct xfs_btree_block *block; /* current btree block */
325 xfs_fsblock_t bno; /* block # of "block" */
326 xfs_buf_t *bp; /* buffer for "block" */
327 int error; /* error return value */
328 xfs_extnum_t i=0, j; /* index into the extents list */
329 struct xfs_ifork *ifp; /* fork structure */
330 int level; /* btree level, for checking */
331 xfs_mount_t *mp; /* file system mount structure */
332 __be64 *pp; /* pointer to block address */
333 xfs_bmbt_rec_t *ep; /* pointer to current extent */
334 xfs_bmbt_rec_t last = {0, 0}; /* last extent in prev block */
335 xfs_bmbt_rec_t *nextp; /* pointer to next extent */
338 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) {
342 /* skip large extent count inodes */
343 if (ip->i_d.di_nextents > 10000)
348 ifp = XFS_IFORK_PTR(ip, whichfork);
349 block = ifp->if_broot;
351 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
353 level = be16_to_cpu(block->bb_level);
355 xfs_check_block(block, mp, 1, ifp->if_broot_bytes);
356 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
357 bno = be64_to_cpu(*pp);
359 ASSERT(bno != NULLFSBLOCK);
360 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
361 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
364 * Go down the tree until leaf level is reached, following the first
365 * pointer (leftmost) at each level.
367 while (level-- > 0) {
368 /* See if buf is in cur first */
370 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
373 error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
379 block = XFS_BUF_TO_BLOCK(bp);
384 * Check this block for basic sanity (increasing keys and
385 * no duplicate blocks).
388 xfs_check_block(block, mp, 0, 0);
389 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
390 bno = be64_to_cpu(*pp);
391 XFS_WANT_CORRUPTED_GOTO(mp,
392 xfs_verify_fsbno(mp, bno), error0);
395 xfs_trans_brelse(NULL, bp);
400 * Here with bp and block set to the leftmost leaf node in the tree.
405 * Loop over all leaf nodes checking that all extents are in the right order.
408 xfs_fsblock_t nextbno;
409 xfs_extnum_t num_recs;
412 num_recs = xfs_btree_get_numrecs(block);
415 * Read-ahead the next leaf block, if any.
418 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
421 * Check all the extents to make sure they are OK.
422 * If we had a previous block, the last entry should
423 * conform with the first entry in this one.
426 ep = XFS_BMBT_REC_ADDR(mp, block, 1);
428 ASSERT(xfs_bmbt_disk_get_startoff(&last) +
429 xfs_bmbt_disk_get_blockcount(&last) <=
430 xfs_bmbt_disk_get_startoff(ep));
432 for (j = 1; j < num_recs; j++) {
433 nextp = XFS_BMBT_REC_ADDR(mp, block, j + 1);
434 ASSERT(xfs_bmbt_disk_get_startoff(ep) +
435 xfs_bmbt_disk_get_blockcount(ep) <=
436 xfs_bmbt_disk_get_startoff(nextp));
444 xfs_trans_brelse(NULL, bp);
448 * If we've reached the end, stop.
450 if (bno == NULLFSBLOCK)
454 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
457 error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
463 block = XFS_BUF_TO_BLOCK(bp);
469 xfs_warn(mp, "%s: at error0", __func__);
471 xfs_trans_brelse(NULL, bp);
473 xfs_warn(mp, "%s: BAD after btree leaves for %d extents",
475 xfs_err(mp, "%s: CORRUPTED BTREE OR SOMETHING", __func__);
476 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
481 * Validate that the bmbt_irecs being returned from bmapi are valid
482 * given the caller's original parameters. Specifically check the
483 * ranges of the returned irecs to ensure that they only extend beyond
484 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
487 xfs_bmap_validate_ret(
491 xfs_bmbt_irec_t *mval,
495 int i; /* index to map values */
497 ASSERT(ret_nmap <= nmap);
499 for (i = 0; i < ret_nmap; i++) {
500 ASSERT(mval[i].br_blockcount > 0);
501 if (!(flags & XFS_BMAPI_ENTIRE)) {
502 ASSERT(mval[i].br_startoff >= bno);
503 ASSERT(mval[i].br_blockcount <= len);
504 ASSERT(mval[i].br_startoff + mval[i].br_blockcount <=
507 ASSERT(mval[i].br_startoff < bno + len);
508 ASSERT(mval[i].br_startoff + mval[i].br_blockcount >
512 mval[i - 1].br_startoff + mval[i - 1].br_blockcount ==
513 mval[i].br_startoff);
514 ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK &&
515 mval[i].br_startblock != HOLESTARTBLOCK);
516 ASSERT(mval[i].br_state == XFS_EXT_NORM ||
517 mval[i].br_state == XFS_EXT_UNWRITTEN);
522 #define xfs_bmap_check_leaf_extents(cur, ip, whichfork) do { } while (0)
523 #define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap) do { } while (0)
527 * bmap free list manipulation functions
531 * Add the extent to the list of extents to be free at transaction end.
532 * The list is maintained sorted (by block number).
536 struct xfs_trans *tp,
539 struct xfs_owner_info *oinfo,
542 struct xfs_extent_free_item *new; /* new element */
544 struct xfs_mount *mp = tp->t_mountp;
548 ASSERT(bno != NULLFSBLOCK);
550 ASSERT(len <= MAXEXTLEN);
551 ASSERT(!isnullstartblock(bno));
552 agno = XFS_FSB_TO_AGNO(mp, bno);
553 agbno = XFS_FSB_TO_AGBNO(mp, bno);
554 ASSERT(agno < mp->m_sb.sb_agcount);
555 ASSERT(agbno < mp->m_sb.sb_agblocks);
556 ASSERT(len < mp->m_sb.sb_agblocks);
557 ASSERT(agbno + len <= mp->m_sb.sb_agblocks);
559 ASSERT(xfs_bmap_free_item_zone != NULL);
561 new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP);
562 new->xefi_startblock = bno;
563 new->xefi_blockcount = (xfs_extlen_t)len;
565 new->xefi_oinfo = *oinfo;
567 xfs_rmap_skip_owner_update(&new->xefi_oinfo);
568 new->xefi_skip_discard = skip_discard;
569 trace_xfs_bmap_free_defer(tp->t_mountp,
570 XFS_FSB_TO_AGNO(tp->t_mountp, bno), 0,
571 XFS_FSB_TO_AGBNO(tp->t_mountp, bno), len);
572 xfs_defer_add(tp, XFS_DEFER_OPS_TYPE_FREE, &new->xefi_list);
576 * Inode fork format manipulation functions
580 * Transform a btree format file with only one leaf node, where the
581 * extents list will fit in the inode, into an extents format file.
582 * Since the file extents are already in-core, all we have to do is
583 * give up the space for the btree root and pitch the leaf block.
585 STATIC int /* error */
586 xfs_bmap_btree_to_extents(
587 xfs_trans_t *tp, /* transaction pointer */
588 xfs_inode_t *ip, /* incore inode pointer */
589 xfs_btree_cur_t *cur, /* btree cursor */
590 int *logflagsp, /* inode logging flags */
591 int whichfork) /* data or attr fork */
594 struct xfs_btree_block *cblock;/* child btree block */
595 xfs_fsblock_t cbno; /* child block number */
596 xfs_buf_t *cbp; /* child block's buffer */
597 int error; /* error return value */
598 struct xfs_ifork *ifp; /* inode fork data */
599 xfs_mount_t *mp; /* mount point structure */
600 __be64 *pp; /* ptr to block address */
601 struct xfs_btree_block *rblock;/* root btree block */
602 struct xfs_owner_info oinfo;
605 ifp = XFS_IFORK_PTR(ip, whichfork);
606 ASSERT(whichfork != XFS_COW_FORK);
607 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
608 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
609 rblock = ifp->if_broot;
610 ASSERT(be16_to_cpu(rblock->bb_level) == 1);
611 ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1);
612 ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0) == 1);
613 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, ifp->if_broot_bytes);
614 cbno = be64_to_cpu(*pp);
617 XFS_WANT_CORRUPTED_RETURN(cur->bc_mp,
618 xfs_btree_check_lptr(cur, cbno, 1));
620 error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp, XFS_BMAP_BTREE_REF,
624 cblock = XFS_BUF_TO_BLOCK(cbp);
625 if ((error = xfs_btree_check_block(cur, cblock, 0, cbp)))
627 xfs_rmap_ino_bmbt_owner(&oinfo, ip->i_ino, whichfork);
628 xfs_bmap_add_free(cur->bc_tp, cbno, 1, &oinfo);
629 ip->i_d.di_nblocks--;
630 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
631 xfs_trans_binval(tp, cbp);
632 if (cur->bc_bufs[0] == cbp)
633 cur->bc_bufs[0] = NULL;
634 xfs_iroot_realloc(ip, -1, whichfork);
635 ASSERT(ifp->if_broot == NULL);
636 ASSERT((ifp->if_flags & XFS_IFBROOT) == 0);
637 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
638 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
643 * Convert an extents-format file into a btree-format file.
644 * The new file will have a root block (in the inode) and a single child block.
646 STATIC int /* error */
647 xfs_bmap_extents_to_btree(
648 struct xfs_trans *tp, /* transaction pointer */
649 struct xfs_inode *ip, /* incore inode pointer */
650 struct xfs_btree_cur **curp, /* cursor returned to caller */
651 int wasdel, /* converting a delayed alloc */
652 int *logflagsp, /* inode logging flags */
653 int whichfork) /* data or attr fork */
655 struct xfs_btree_block *ablock; /* allocated (child) bt block */
656 struct xfs_buf *abp; /* buffer for ablock */
657 struct xfs_alloc_arg args; /* allocation arguments */
658 struct xfs_bmbt_rec *arp; /* child record pointer */
659 struct xfs_btree_block *block; /* btree root block */
660 struct xfs_btree_cur *cur; /* bmap btree cursor */
661 int error; /* error return value */
662 struct xfs_ifork *ifp; /* inode fork pointer */
663 struct xfs_bmbt_key *kp; /* root block key pointer */
664 struct xfs_mount *mp; /* mount structure */
665 xfs_bmbt_ptr_t *pp; /* root block address pointer */
666 struct xfs_iext_cursor icur;
667 struct xfs_bmbt_irec rec;
668 xfs_extnum_t cnt = 0;
671 ASSERT(whichfork != XFS_COW_FORK);
672 ifp = XFS_IFORK_PTR(ip, whichfork);
673 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS);
676 * Make space in the inode incore. This needs to be undone if we fail
677 * to expand the root.
679 xfs_iroot_realloc(ip, 1, whichfork);
680 ifp->if_flags |= XFS_IFBROOT;
685 block = ifp->if_broot;
686 xfs_btree_init_block_int(mp, block, XFS_BUF_DADDR_NULL,
687 XFS_BTNUM_BMAP, 1, 1, ip->i_ino,
688 XFS_BTREE_LONG_PTRS);
690 * Need a cursor. Can't allocate until bb_level is filled in.
692 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
693 cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
695 * Convert to a btree with two levels, one record in root.
697 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE);
698 memset(&args, 0, sizeof(args));
701 xfs_rmap_ino_bmbt_owner(&args.oinfo, ip->i_ino, whichfork);
702 if (tp->t_firstblock == NULLFSBLOCK) {
703 args.type = XFS_ALLOCTYPE_START_BNO;
704 args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino);
705 } else if (tp->t_flags & XFS_TRANS_LOWMODE) {
706 args.type = XFS_ALLOCTYPE_START_BNO;
707 args.fsbno = tp->t_firstblock;
709 args.type = XFS_ALLOCTYPE_NEAR_BNO;
710 args.fsbno = tp->t_firstblock;
712 args.minlen = args.maxlen = args.prod = 1;
713 args.wasdel = wasdel;
715 error = xfs_alloc_vextent(&args);
717 goto out_root_realloc;
719 if (WARN_ON_ONCE(args.fsbno == NULLFSBLOCK)) {
721 goto out_root_realloc;
725 * Allocation can't fail, the space was reserved.
727 ASSERT(tp->t_firstblock == NULLFSBLOCK ||
728 args.agno >= XFS_FSB_TO_AGNO(mp, tp->t_firstblock));
729 tp->t_firstblock = args.fsbno;
730 cur->bc_private.b.allocated++;
731 ip->i_d.di_nblocks++;
732 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
733 abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0);
735 error = -EFSCORRUPTED;
736 goto out_unreserve_dquot;
740 * Fill in the child block.
742 abp->b_ops = &xfs_bmbt_buf_ops;
743 ablock = XFS_BUF_TO_BLOCK(abp);
744 xfs_btree_init_block_int(mp, ablock, abp->b_bn,
745 XFS_BTNUM_BMAP, 0, 0, ip->i_ino,
746 XFS_BTREE_LONG_PTRS);
748 for_each_xfs_iext(ifp, &icur, &rec) {
749 if (isnullstartblock(rec.br_startblock))
751 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1 + cnt);
752 xfs_bmbt_disk_set_all(arp, &rec);
755 ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork));
756 xfs_btree_set_numrecs(ablock, cnt);
759 * Fill in the root key and pointer.
761 kp = XFS_BMBT_KEY_ADDR(mp, block, 1);
762 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
763 kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp));
764 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur,
765 be16_to_cpu(block->bb_level)));
766 *pp = cpu_to_be64(args.fsbno);
769 * Do all this logging at the end so that
770 * the root is at the right level.
772 xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS);
773 xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs));
774 ASSERT(*curp == NULL);
776 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork);
780 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
782 xfs_iroot_realloc(ip, -1, whichfork);
783 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
784 ASSERT(ifp->if_broot == NULL);
785 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
791 * Convert a local file to an extents file.
792 * This code is out of bounds for data forks of regular files,
793 * since the file data needs to get logged so things will stay consistent.
794 * (The bmap-level manipulations are ok, though).
797 xfs_bmap_local_to_extents_empty(
798 struct xfs_inode *ip,
801 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
803 ASSERT(whichfork != XFS_COW_FORK);
804 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
805 ASSERT(ifp->if_bytes == 0);
806 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0);
808 xfs_bmap_forkoff_reset(ip, whichfork);
809 ifp->if_flags &= ~XFS_IFINLINE;
810 ifp->if_flags |= XFS_IFEXTENTS;
811 ifp->if_u1.if_root = NULL;
813 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
817 STATIC int /* error */
818 xfs_bmap_local_to_extents(
819 xfs_trans_t *tp, /* transaction pointer */
820 xfs_inode_t *ip, /* incore inode pointer */
821 xfs_extlen_t total, /* total blocks needed by transaction */
822 int *logflagsp, /* inode logging flags */
824 void (*init_fn)(struct xfs_trans *tp,
826 struct xfs_inode *ip,
827 struct xfs_ifork *ifp))
830 int flags; /* logging flags returned */
831 struct xfs_ifork *ifp; /* inode fork pointer */
832 xfs_alloc_arg_t args; /* allocation arguments */
833 xfs_buf_t *bp; /* buffer for extent block */
834 struct xfs_bmbt_irec rec;
835 struct xfs_iext_cursor icur;
838 * We don't want to deal with the case of keeping inode data inline yet.
839 * So sending the data fork of a regular inode is invalid.
841 ASSERT(!(S_ISREG(VFS_I(ip)->i_mode) && whichfork == XFS_DATA_FORK));
842 ifp = XFS_IFORK_PTR(ip, whichfork);
843 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
845 if (!ifp->if_bytes) {
846 xfs_bmap_local_to_extents_empty(ip, whichfork);
847 flags = XFS_ILOG_CORE;
853 ASSERT((ifp->if_flags & (XFS_IFINLINE|XFS_IFEXTENTS)) == XFS_IFINLINE);
854 memset(&args, 0, sizeof(args));
856 args.mp = ip->i_mount;
857 xfs_rmap_ino_owner(&args.oinfo, ip->i_ino, whichfork, 0);
859 * Allocate a block. We know we need only one, since the
860 * file currently fits in an inode.
862 if (tp->t_firstblock == NULLFSBLOCK) {
863 args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino);
864 args.type = XFS_ALLOCTYPE_START_BNO;
866 args.fsbno = tp->t_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 tp->t_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 int *flags) /* inode logging flags */
926 xfs_btree_cur_t *cur; /* btree cursor */
927 int error; /* error return value */
928 xfs_mount_t *mp; /* file system mount struct */
929 int stat; /* newroot status */
932 if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip))
933 *flags |= XFS_ILOG_DBROOT;
935 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
936 error = xfs_bmbt_lookup_first(cur, &stat);
939 /* must be at least one entry */
940 XFS_WANT_CORRUPTED_GOTO(mp, stat == 1, error0);
941 if ((error = xfs_btree_new_iroot(cur, flags, &stat)))
944 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
947 cur->bc_private.b.allocated = 0;
948 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
952 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
957 * Called from xfs_bmap_add_attrfork to handle extents format files.
959 STATIC int /* error */
960 xfs_bmap_add_attrfork_extents(
961 struct xfs_trans *tp, /* transaction pointer */
962 struct xfs_inode *ip, /* incore inode pointer */
963 int *flags) /* inode logging flags */
965 xfs_btree_cur_t *cur; /* bmap btree cursor */
966 int error; /* error return value */
968 if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip))
971 error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0, flags,
974 cur->bc_private.b.allocated = 0;
975 xfs_btree_del_cursor(cur, error);
981 * Called from xfs_bmap_add_attrfork to handle local format files. Each
982 * different data fork content type needs a different callout to do the
983 * conversion. Some are basic and only require special block initialisation
984 * callouts for the data formating, others (directories) are so specialised they
985 * handle everything themselves.
987 * XXX (dgc): investigate whether directory conversion can use the generic
988 * formatting callout. It should be possible - it's just a very complex
991 STATIC int /* error */
992 xfs_bmap_add_attrfork_local(
993 struct xfs_trans *tp, /* transaction pointer */
994 struct xfs_inode *ip, /* incore inode pointer */
995 int *flags) /* inode logging flags */
997 struct xfs_da_args dargs; /* args for dir/attr code */
999 if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip))
1002 if (S_ISDIR(VFS_I(ip)->i_mode)) {
1003 memset(&dargs, 0, sizeof(dargs));
1004 dargs.geo = ip->i_mount->m_dir_geo;
1006 dargs.total = dargs.geo->fsbcount;
1007 dargs.whichfork = XFS_DATA_FORK;
1009 return xfs_dir2_sf_to_block(&dargs);
1012 if (S_ISLNK(VFS_I(ip)->i_mode))
1013 return xfs_bmap_local_to_extents(tp, ip, 1, flags,
1015 xfs_symlink_local_to_remote);
1017 /* should only be called for types that support local format data */
1019 return -EFSCORRUPTED;
1022 /* Set an inode attr fork off based on the format */
1024 xfs_bmap_set_attrforkoff(
1025 struct xfs_inode *ip,
1029 switch (ip->i_d.di_format) {
1030 case XFS_DINODE_FMT_DEV:
1031 ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
1033 case XFS_DINODE_FMT_LOCAL:
1034 case XFS_DINODE_FMT_EXTENTS:
1035 case XFS_DINODE_FMT_BTREE:
1036 ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size);
1037 if (!ip->i_d.di_forkoff)
1038 ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3;
1039 else if ((ip->i_mount->m_flags & XFS_MOUNT_ATTR2) && version)
1051 * Convert inode from non-attributed to attributed.
1052 * Must not be in a transaction, ip must not be locked.
1054 int /* error code */
1055 xfs_bmap_add_attrfork(
1056 xfs_inode_t *ip, /* incore inode pointer */
1057 int size, /* space new attribute needs */
1058 int rsvd) /* xact may use reserved blks */
1060 xfs_mount_t *mp; /* mount structure */
1061 xfs_trans_t *tp; /* transaction pointer */
1062 int blks; /* space reservation */
1063 int version = 1; /* superblock attr version */
1064 int logflags; /* logging flags */
1065 int error; /* error return value */
1067 ASSERT(XFS_IFORK_Q(ip) == 0);
1070 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
1072 blks = XFS_ADDAFORK_SPACE_RES(mp);
1074 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_addafork, blks, 0,
1075 rsvd ? XFS_TRANS_RESERVE : 0, &tp);
1079 xfs_ilock(ip, XFS_ILOCK_EXCL);
1080 error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ?
1081 XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
1082 XFS_QMOPT_RES_REGBLKS);
1085 if (XFS_IFORK_Q(ip))
1087 if (ip->i_d.di_anextents != 0) {
1088 error = -EFSCORRUPTED;
1091 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) {
1093 * For inodes coming from pre-6.2 filesystems.
1095 ASSERT(ip->i_d.di_aformat == 0);
1096 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
1099 xfs_trans_ijoin(tp, ip, 0);
1100 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1101 error = xfs_bmap_set_attrforkoff(ip, size, &version);
1104 ASSERT(ip->i_afp == NULL);
1105 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP);
1106 ip->i_afp->if_flags = XFS_IFEXTENTS;
1108 switch (ip->i_d.di_format) {
1109 case XFS_DINODE_FMT_LOCAL:
1110 error = xfs_bmap_add_attrfork_local(tp, ip, &logflags);
1112 case XFS_DINODE_FMT_EXTENTS:
1113 error = xfs_bmap_add_attrfork_extents(tp, ip, &logflags);
1115 case XFS_DINODE_FMT_BTREE:
1116 error = xfs_bmap_add_attrfork_btree(tp, ip, &logflags);
1123 xfs_trans_log_inode(tp, ip, logflags);
1126 if (!xfs_sb_version_hasattr(&mp->m_sb) ||
1127 (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) {
1128 bool log_sb = false;
1130 spin_lock(&mp->m_sb_lock);
1131 if (!xfs_sb_version_hasattr(&mp->m_sb)) {
1132 xfs_sb_version_addattr(&mp->m_sb);
1135 if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) {
1136 xfs_sb_version_addattr2(&mp->m_sb);
1139 spin_unlock(&mp->m_sb_lock);
1144 error = xfs_trans_commit(tp);
1145 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1149 xfs_trans_cancel(tp);
1150 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1155 * Internal and external extent tree search functions.
1159 * Read in extents from a btree-format inode.
1163 struct xfs_trans *tp,
1164 struct xfs_inode *ip,
1167 struct xfs_mount *mp = ip->i_mount;
1168 int state = xfs_bmap_fork_to_state(whichfork);
1169 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1170 xfs_extnum_t nextents = XFS_IFORK_NEXTENTS(ip, whichfork);
1171 struct xfs_btree_block *block = ifp->if_broot;
1172 struct xfs_iext_cursor icur;
1173 struct xfs_bmbt_irec new;
1181 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
1183 if (unlikely(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
1184 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp);
1185 return -EFSCORRUPTED;
1189 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
1191 level = be16_to_cpu(block->bb_level);
1193 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
1194 bno = be64_to_cpu(*pp);
1197 * Go down the tree until leaf level is reached, following the first
1198 * pointer (leftmost) at each level.
1200 while (level-- > 0) {
1201 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
1202 XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
1205 block = XFS_BUF_TO_BLOCK(bp);
1208 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
1209 bno = be64_to_cpu(*pp);
1210 XFS_WANT_CORRUPTED_GOTO(mp,
1211 xfs_verify_fsbno(mp, bno), out_brelse);
1212 xfs_trans_brelse(tp, bp);
1216 * Here with bp and block set to the leftmost leaf node in the tree.
1219 xfs_iext_first(ifp, &icur);
1222 * Loop over all leaf nodes. Copy information to the extent records.
1225 xfs_bmbt_rec_t *frp;
1226 xfs_fsblock_t nextbno;
1227 xfs_extnum_t num_recs;
1229 num_recs = xfs_btree_get_numrecs(block);
1230 if (unlikely(i + num_recs > nextents)) {
1231 xfs_warn(ip->i_mount,
1232 "corrupt dinode %Lu, (btree extents).",
1233 (unsigned long long) ip->i_ino);
1234 xfs_inode_verifier_error(ip, -EFSCORRUPTED,
1235 __func__, block, sizeof(*block),
1237 error = -EFSCORRUPTED;
1241 * Read-ahead the next leaf block, if any.
1243 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
1244 if (nextbno != NULLFSBLOCK)
1245 xfs_btree_reada_bufl(mp, nextbno, 1,
1248 * Copy records into the extent records.
1250 frp = XFS_BMBT_REC_ADDR(mp, block, 1);
1251 for (j = 0; j < num_recs; j++, frp++, i++) {
1254 xfs_bmbt_disk_get_all(frp, &new);
1255 fa = xfs_bmap_validate_extent(ip, whichfork, &new);
1257 error = -EFSCORRUPTED;
1258 xfs_inode_verifier_error(ip, error,
1259 "xfs_iread_extents(2)",
1260 frp, sizeof(*frp), fa);
1263 xfs_iext_insert(ip, &icur, &new, state);
1264 trace_xfs_read_extent(ip, &icur, state, _THIS_IP_);
1265 xfs_iext_next(ifp, &icur);
1267 xfs_trans_brelse(tp, bp);
1270 * If we've reached the end, stop.
1272 if (bno == NULLFSBLOCK)
1274 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
1275 XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
1278 block = XFS_BUF_TO_BLOCK(bp);
1281 if (i != XFS_IFORK_NEXTENTS(ip, whichfork)) {
1282 error = -EFSCORRUPTED;
1285 ASSERT(i == xfs_iext_count(ifp));
1287 ifp->if_flags |= XFS_IFEXTENTS;
1291 xfs_trans_brelse(tp, bp);
1293 xfs_iext_destroy(ifp);
1298 * Returns the relative block number of the first unused block(s) in the given
1299 * fork with at least "len" logically contiguous blocks free. This is the
1300 * lowest-address hole if the fork has holes, else the first block past the end
1301 * of fork. Return 0 if the fork is currently local (in-inode).
1304 xfs_bmap_first_unused(
1305 struct xfs_trans *tp, /* transaction pointer */
1306 struct xfs_inode *ip, /* incore inode */
1307 xfs_extlen_t len, /* size of hole to find */
1308 xfs_fileoff_t *first_unused, /* unused block */
1309 int whichfork) /* data or attr fork */
1311 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1312 struct xfs_bmbt_irec got;
1313 struct xfs_iext_cursor icur;
1314 xfs_fileoff_t lastaddr = 0;
1315 xfs_fileoff_t lowest, max;
1318 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE ||
1319 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ||
1320 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
1322 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
1327 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1328 error = xfs_iread_extents(tp, ip, whichfork);
1333 lowest = max = *first_unused;
1334 for_each_xfs_iext(ifp, &icur, &got) {
1336 * See if the hole before this extent will work.
1338 if (got.br_startoff >= lowest + len &&
1339 got.br_startoff - max >= len)
1341 lastaddr = got.br_startoff + got.br_blockcount;
1342 max = XFS_FILEOFF_MAX(lastaddr, lowest);
1345 *first_unused = max;
1350 * Returns the file-relative block number of the last block - 1 before
1351 * last_block (input value) in the file.
1352 * This is not based on i_size, it is based on the extent records.
1353 * Returns 0 for local files, as they do not have extent records.
1356 xfs_bmap_last_before(
1357 struct xfs_trans *tp, /* transaction pointer */
1358 struct xfs_inode *ip, /* incore inode */
1359 xfs_fileoff_t *last_block, /* last block */
1360 int whichfork) /* data or attr fork */
1362 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1363 struct xfs_bmbt_irec got;
1364 struct xfs_iext_cursor icur;
1367 switch (XFS_IFORK_FORMAT(ip, whichfork)) {
1368 case XFS_DINODE_FMT_LOCAL:
1371 case XFS_DINODE_FMT_BTREE:
1372 case XFS_DINODE_FMT_EXTENTS:
1378 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1379 error = xfs_iread_extents(tp, ip, whichfork);
1384 if (!xfs_iext_lookup_extent_before(ip, ifp, last_block, &icur, &got))
1390 xfs_bmap_last_extent(
1391 struct xfs_trans *tp,
1392 struct xfs_inode *ip,
1394 struct xfs_bmbt_irec *rec,
1397 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1398 struct xfs_iext_cursor icur;
1401 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1402 error = xfs_iread_extents(tp, ip, whichfork);
1407 xfs_iext_last(ifp, &icur);
1408 if (!xfs_iext_get_extent(ifp, &icur, rec))
1416 * Check the last inode extent to determine whether this allocation will result
1417 * in blocks being allocated at the end of the file. When we allocate new data
1418 * blocks at the end of the file which do not start at the previous data block,
1419 * we will try to align the new blocks at stripe unit boundaries.
1421 * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be
1422 * at, or past the EOF.
1426 struct xfs_bmalloca *bma,
1429 struct xfs_bmbt_irec rec;
1434 error = xfs_bmap_last_extent(NULL, bma->ip, whichfork, &rec,
1445 * Check if we are allocation or past the last extent, or at least into
1446 * the last delayed allocated extent.
1448 bma->aeof = bma->offset >= rec.br_startoff + rec.br_blockcount ||
1449 (bma->offset >= rec.br_startoff &&
1450 isnullstartblock(rec.br_startblock));
1455 * Returns the file-relative block number of the first block past eof in
1456 * the file. This is not based on i_size, it is based on the extent records.
1457 * Returns 0 for local files, as they do not have extent records.
1460 xfs_bmap_last_offset(
1461 struct xfs_inode *ip,
1462 xfs_fileoff_t *last_block,
1465 struct xfs_bmbt_irec rec;
1471 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL)
1474 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
1475 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
1478 error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, &is_empty);
1479 if (error || is_empty)
1482 *last_block = rec.br_startoff + rec.br_blockcount;
1487 * Returns whether the selected fork of the inode has exactly one
1488 * block or not. For the data fork we check this matches di_size,
1489 * implying the file's range is 0..bsize-1.
1491 int /* 1=>1 block, 0=>otherwise */
1493 xfs_inode_t *ip, /* incore inode */
1494 int whichfork) /* data or attr fork */
1496 struct xfs_ifork *ifp; /* inode fork pointer */
1497 int rval; /* return value */
1498 xfs_bmbt_irec_t s; /* internal version of extent */
1499 struct xfs_iext_cursor icur;
1502 if (whichfork == XFS_DATA_FORK)
1503 return XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize;
1505 if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1)
1507 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
1509 ifp = XFS_IFORK_PTR(ip, whichfork);
1510 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
1511 xfs_iext_first(ifp, &icur);
1512 xfs_iext_get_extent(ifp, &icur, &s);
1513 rval = s.br_startoff == 0 && s.br_blockcount == 1;
1514 if (rval && whichfork == XFS_DATA_FORK)
1515 ASSERT(XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize);
1520 * Extent tree manipulation functions used during allocation.
1524 * Convert a delayed allocation to a real allocation.
1526 STATIC int /* error */
1527 xfs_bmap_add_extent_delay_real(
1528 struct xfs_bmalloca *bma,
1531 struct xfs_bmbt_irec *new = &bma->got;
1532 int error; /* error return value */
1533 int i; /* temp state */
1534 struct xfs_ifork *ifp; /* inode fork pointer */
1535 xfs_fileoff_t new_endoff; /* end offset of new entry */
1536 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
1537 /* left is 0, right is 1, prev is 2 */
1538 int rval=0; /* return value (logging flags) */
1539 int state = xfs_bmap_fork_to_state(whichfork);
1540 xfs_filblks_t da_new; /* new count del alloc blocks used */
1541 xfs_filblks_t da_old; /* old count del alloc blocks used */
1542 xfs_filblks_t temp=0; /* value for da_new calculations */
1543 int tmp_rval; /* partial logging flags */
1544 struct xfs_mount *mp;
1545 xfs_extnum_t *nextents;
1546 struct xfs_bmbt_irec old;
1548 mp = bma->ip->i_mount;
1549 ifp = XFS_IFORK_PTR(bma->ip, whichfork);
1550 ASSERT(whichfork != XFS_ATTR_FORK);
1551 nextents = (whichfork == XFS_COW_FORK ? &bma->ip->i_cnextents :
1552 &bma->ip->i_d.di_nextents);
1554 ASSERT(!isnullstartblock(new->br_startblock));
1556 (bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
1558 XFS_STATS_INC(mp, xs_add_exlist);
1565 * Set up a bunch of variables to make the tests simpler.
1567 xfs_iext_get_extent(ifp, &bma->icur, &PREV);
1568 new_endoff = new->br_startoff + new->br_blockcount;
1569 ASSERT(isnullstartblock(PREV.br_startblock));
1570 ASSERT(PREV.br_startoff <= new->br_startoff);
1571 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
1573 da_old = startblockval(PREV.br_startblock);
1577 * Set flags determining what part of the previous delayed allocation
1578 * extent is being replaced by a real allocation.
1580 if (PREV.br_startoff == new->br_startoff)
1581 state |= BMAP_LEFT_FILLING;
1582 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
1583 state |= BMAP_RIGHT_FILLING;
1586 * Check and set flags if this segment has a left neighbor.
1587 * Don't set contiguous if the combined extent would be too large.
1589 if (xfs_iext_peek_prev_extent(ifp, &bma->icur, &LEFT)) {
1590 state |= BMAP_LEFT_VALID;
1591 if (isnullstartblock(LEFT.br_startblock))
1592 state |= BMAP_LEFT_DELAY;
1595 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1596 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1597 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
1598 LEFT.br_state == new->br_state &&
1599 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1600 state |= BMAP_LEFT_CONTIG;
1603 * Check and set flags if this segment has a right neighbor.
1604 * Don't set contiguous if the combined extent would be too large.
1605 * Also check for all-three-contiguous being too large.
1607 if (xfs_iext_peek_next_extent(ifp, &bma->icur, &RIGHT)) {
1608 state |= BMAP_RIGHT_VALID;
1609 if (isnullstartblock(RIGHT.br_startblock))
1610 state |= BMAP_RIGHT_DELAY;
1613 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1614 new_endoff == RIGHT.br_startoff &&
1615 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
1616 new->br_state == RIGHT.br_state &&
1617 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
1618 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1619 BMAP_RIGHT_FILLING)) !=
1620 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1621 BMAP_RIGHT_FILLING) ||
1622 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
1624 state |= BMAP_RIGHT_CONTIG;
1628 * Switch out based on the FILLING and CONTIG state bits.
1630 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1631 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
1632 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1633 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1635 * Filling in all of a previously delayed allocation extent.
1636 * The left and right neighbors are both contiguous with new.
1638 LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount;
1640 xfs_iext_remove(bma->ip, &bma->icur, state);
1641 xfs_iext_remove(bma->ip, &bma->icur, state);
1642 xfs_iext_prev(ifp, &bma->icur);
1643 xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT);
1646 if (bma->cur == NULL)
1647 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1649 rval = XFS_ILOG_CORE;
1650 error = xfs_bmbt_lookup_eq(bma->cur, &RIGHT, &i);
1653 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
1654 error = xfs_btree_delete(bma->cur, &i);
1657 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
1658 error = xfs_btree_decrement(bma->cur, 0, &i);
1661 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
1662 error = xfs_bmbt_update(bma->cur, &LEFT);
1668 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1670 * Filling in all of a previously delayed allocation extent.
1671 * The left neighbor is contiguous, the right is not.
1674 LEFT.br_blockcount += PREV.br_blockcount;
1676 xfs_iext_remove(bma->ip, &bma->icur, state);
1677 xfs_iext_prev(ifp, &bma->icur);
1678 xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT);
1680 if (bma->cur == NULL)
1681 rval = XFS_ILOG_DEXT;
1684 error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
1687 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
1688 error = xfs_bmbt_update(bma->cur, &LEFT);
1694 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1696 * Filling in all of a previously delayed allocation extent.
1697 * The right neighbor is contiguous, the left is not. Take care
1698 * with delay -> unwritten extent allocation here because the
1699 * delalloc record we are overwriting is always written.
1701 PREV.br_startblock = new->br_startblock;
1702 PREV.br_blockcount += RIGHT.br_blockcount;
1703 PREV.br_state = new->br_state;
1705 xfs_iext_next(ifp, &bma->icur);
1706 xfs_iext_remove(bma->ip, &bma->icur, state);
1707 xfs_iext_prev(ifp, &bma->icur);
1708 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
1710 if (bma->cur == NULL)
1711 rval = XFS_ILOG_DEXT;
1714 error = xfs_bmbt_lookup_eq(bma->cur, &RIGHT, &i);
1717 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
1718 error = xfs_bmbt_update(bma->cur, &PREV);
1724 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
1726 * Filling in all of a previously delayed allocation extent.
1727 * Neither the left nor right neighbors are contiguous with
1730 PREV.br_startblock = new->br_startblock;
1731 PREV.br_state = new->br_state;
1732 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
1735 if (bma->cur == NULL)
1736 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1738 rval = XFS_ILOG_CORE;
1739 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
1742 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
1743 error = xfs_btree_insert(bma->cur, &i);
1746 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
1750 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
1752 * Filling in the first part of a previous delayed allocation.
1753 * The left neighbor is contiguous.
1756 temp = PREV.br_blockcount - new->br_blockcount;
1757 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
1758 startblockval(PREV.br_startblock));
1760 LEFT.br_blockcount += new->br_blockcount;
1762 PREV.br_blockcount = temp;
1763 PREV.br_startoff += new->br_blockcount;
1764 PREV.br_startblock = nullstartblock(da_new);
1766 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
1767 xfs_iext_prev(ifp, &bma->icur);
1768 xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT);
1770 if (bma->cur == NULL)
1771 rval = XFS_ILOG_DEXT;
1774 error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
1777 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
1778 error = xfs_bmbt_update(bma->cur, &LEFT);
1784 case BMAP_LEFT_FILLING:
1786 * Filling in the first part of a previous delayed allocation.
1787 * The left neighbor is not contiguous.
1789 xfs_iext_update_extent(bma->ip, state, &bma->icur, new);
1791 if (bma->cur == NULL)
1792 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1794 rval = XFS_ILOG_CORE;
1795 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
1798 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
1799 error = xfs_btree_insert(bma->cur, &i);
1802 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
1805 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
1806 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
1807 &bma->cur, 1, &tmp_rval, whichfork);
1813 temp = PREV.br_blockcount - new->br_blockcount;
1814 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
1815 startblockval(PREV.br_startblock) -
1816 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
1818 PREV.br_startoff = new_endoff;
1819 PREV.br_blockcount = temp;
1820 PREV.br_startblock = nullstartblock(da_new);
1821 xfs_iext_next(ifp, &bma->icur);
1822 xfs_iext_insert(bma->ip, &bma->icur, &PREV, state);
1823 xfs_iext_prev(ifp, &bma->icur);
1826 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1828 * Filling in the last part of a previous delayed allocation.
1829 * The right neighbor is contiguous with the new allocation.
1832 RIGHT.br_startoff = new->br_startoff;
1833 RIGHT.br_startblock = new->br_startblock;
1834 RIGHT.br_blockcount += new->br_blockcount;
1836 if (bma->cur == NULL)
1837 rval = XFS_ILOG_DEXT;
1840 error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
1843 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
1844 error = xfs_bmbt_update(bma->cur, &RIGHT);
1849 temp = PREV.br_blockcount - new->br_blockcount;
1850 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
1851 startblockval(PREV.br_startblock));
1853 PREV.br_blockcount = temp;
1854 PREV.br_startblock = nullstartblock(da_new);
1856 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
1857 xfs_iext_next(ifp, &bma->icur);
1858 xfs_iext_update_extent(bma->ip, state, &bma->icur, &RIGHT);
1861 case BMAP_RIGHT_FILLING:
1863 * Filling in the last part of a previous delayed allocation.
1864 * The right neighbor is not contiguous.
1866 xfs_iext_update_extent(bma->ip, state, &bma->icur, new);
1868 if (bma->cur == NULL)
1869 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1871 rval = XFS_ILOG_CORE;
1872 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
1875 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
1876 error = xfs_btree_insert(bma->cur, &i);
1879 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
1882 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
1883 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
1884 &bma->cur, 1, &tmp_rval, whichfork);
1890 temp = PREV.br_blockcount - new->br_blockcount;
1891 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
1892 startblockval(PREV.br_startblock) -
1893 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
1895 PREV.br_startblock = nullstartblock(da_new);
1896 PREV.br_blockcount = temp;
1897 xfs_iext_insert(bma->ip, &bma->icur, &PREV, state);
1898 xfs_iext_next(ifp, &bma->icur);
1903 * Filling in the middle part of a previous delayed allocation.
1904 * Contiguity is impossible here.
1905 * This case is avoided almost all the time.
1907 * We start with a delayed allocation:
1909 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
1912 * and we are allocating:
1913 * +rrrrrrrrrrrrrrrrr+
1916 * and we set it up for insertion as:
1917 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
1919 * PREV @ idx LEFT RIGHT
1920 * inserted at idx + 1
1924 /* LEFT is the new middle */
1927 /* RIGHT is the new right */
1928 RIGHT.br_state = PREV.br_state;
1929 RIGHT.br_startoff = new_endoff;
1930 RIGHT.br_blockcount =
1931 PREV.br_startoff + PREV.br_blockcount - new_endoff;
1932 RIGHT.br_startblock =
1933 nullstartblock(xfs_bmap_worst_indlen(bma->ip,
1934 RIGHT.br_blockcount));
1937 PREV.br_blockcount = new->br_startoff - PREV.br_startoff;
1938 PREV.br_startblock =
1939 nullstartblock(xfs_bmap_worst_indlen(bma->ip,
1940 PREV.br_blockcount));
1941 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
1943 xfs_iext_next(ifp, &bma->icur);
1944 xfs_iext_insert(bma->ip, &bma->icur, &RIGHT, state);
1945 xfs_iext_insert(bma->ip, &bma->icur, &LEFT, state);
1948 if (bma->cur == NULL)
1949 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1951 rval = XFS_ILOG_CORE;
1952 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
1955 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
1956 error = xfs_btree_insert(bma->cur, &i);
1959 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
1962 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
1963 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
1964 &bma->cur, 1, &tmp_rval, whichfork);
1970 da_new = startblockval(PREV.br_startblock) +
1971 startblockval(RIGHT.br_startblock);
1974 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1975 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1976 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
1977 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1978 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1979 case BMAP_LEFT_CONTIG:
1980 case BMAP_RIGHT_CONTIG:
1982 * These cases are all impossible.
1987 /* add reverse mapping unless caller opted out */
1988 if (!(bma->flags & XFS_BMAPI_NORMAP)) {
1989 error = xfs_rmap_map_extent(bma->tp, bma->ip, whichfork, new);
1994 /* convert to a btree if necessary */
1995 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
1996 int tmp_logflags; /* partial log flag return val */
1998 ASSERT(bma->cur == NULL);
1999 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
2000 &bma->cur, da_old > 0, &tmp_logflags,
2002 bma->logflags |= tmp_logflags;
2008 da_new += bma->cur->bc_private.b.allocated;
2009 bma->cur->bc_private.b.allocated = 0;
2012 /* adjust for changes in reserved delayed indirect blocks */
2013 if (da_new != da_old) {
2014 ASSERT(state == 0 || da_new < da_old);
2015 error = xfs_mod_fdblocks(mp, (int64_t)(da_old - da_new),
2019 xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork);
2021 if (whichfork != XFS_COW_FORK)
2022 bma->logflags |= rval;
2030 * Convert an unwritten allocation to a real allocation or vice versa.
2032 STATIC int /* error */
2033 xfs_bmap_add_extent_unwritten_real(
2034 struct xfs_trans *tp,
2035 xfs_inode_t *ip, /* incore inode pointer */
2037 struct xfs_iext_cursor *icur,
2038 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
2039 xfs_bmbt_irec_t *new, /* new data to add to file extents */
2040 int *logflagsp) /* inode logging flags */
2042 xfs_btree_cur_t *cur; /* btree cursor */
2043 int error; /* error return value */
2044 int i; /* temp state */
2045 struct xfs_ifork *ifp; /* inode fork pointer */
2046 xfs_fileoff_t new_endoff; /* end offset of new entry */
2047 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
2048 /* left is 0, right is 1, prev is 2 */
2049 int rval=0; /* return value (logging flags) */
2050 int state = xfs_bmap_fork_to_state(whichfork);
2051 struct xfs_mount *mp = ip->i_mount;
2052 struct xfs_bmbt_irec old;
2057 ifp = XFS_IFORK_PTR(ip, whichfork);
2059 ASSERT(!isnullstartblock(new->br_startblock));
2061 XFS_STATS_INC(mp, xs_add_exlist);
2068 * Set up a bunch of variables to make the tests simpler.
2071 xfs_iext_get_extent(ifp, icur, &PREV);
2072 ASSERT(new->br_state != PREV.br_state);
2073 new_endoff = new->br_startoff + new->br_blockcount;
2074 ASSERT(PREV.br_startoff <= new->br_startoff);
2075 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
2078 * Set flags determining what part of the previous oldext allocation
2079 * extent is being replaced by a newext allocation.
2081 if (PREV.br_startoff == new->br_startoff)
2082 state |= BMAP_LEFT_FILLING;
2083 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
2084 state |= BMAP_RIGHT_FILLING;
2087 * Check and set flags if this segment has a left neighbor.
2088 * Don't set contiguous if the combined extent would be too large.
2090 if (xfs_iext_peek_prev_extent(ifp, icur, &LEFT)) {
2091 state |= BMAP_LEFT_VALID;
2092 if (isnullstartblock(LEFT.br_startblock))
2093 state |= BMAP_LEFT_DELAY;
2096 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2097 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
2098 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
2099 LEFT.br_state == new->br_state &&
2100 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2101 state |= BMAP_LEFT_CONTIG;
2104 * Check and set flags if this segment has a right neighbor.
2105 * Don't set contiguous if the combined extent would be too large.
2106 * Also check for all-three-contiguous being too large.
2108 if (xfs_iext_peek_next_extent(ifp, icur, &RIGHT)) {
2109 state |= BMAP_RIGHT_VALID;
2110 if (isnullstartblock(RIGHT.br_startblock))
2111 state |= BMAP_RIGHT_DELAY;
2114 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2115 new_endoff == RIGHT.br_startoff &&
2116 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
2117 new->br_state == RIGHT.br_state &&
2118 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
2119 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2120 BMAP_RIGHT_FILLING)) !=
2121 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2122 BMAP_RIGHT_FILLING) ||
2123 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
2125 state |= BMAP_RIGHT_CONTIG;
2128 * Switch out based on the FILLING and CONTIG state bits.
2130 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2131 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
2132 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2133 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
2135 * Setting all of a previous oldext extent to newext.
2136 * The left and right neighbors are both contiguous with new.
2138 LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount;
2140 xfs_iext_remove(ip, icur, state);
2141 xfs_iext_remove(ip, icur, state);
2142 xfs_iext_prev(ifp, icur);
2143 xfs_iext_update_extent(ip, state, icur, &LEFT);
2144 XFS_IFORK_NEXT_SET(ip, whichfork,
2145 XFS_IFORK_NEXTENTS(ip, whichfork) - 2);
2147 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2149 rval = XFS_ILOG_CORE;
2150 error = xfs_bmbt_lookup_eq(cur, &RIGHT, &i);
2153 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2154 if ((error = xfs_btree_delete(cur, &i)))
2156 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2157 if ((error = xfs_btree_decrement(cur, 0, &i)))
2159 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2160 if ((error = xfs_btree_delete(cur, &i)))
2162 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2163 if ((error = xfs_btree_decrement(cur, 0, &i)))
2165 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2166 error = xfs_bmbt_update(cur, &LEFT);
2172 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2174 * Setting all of a previous oldext extent to newext.
2175 * The left neighbor is contiguous, the right is not.
2177 LEFT.br_blockcount += PREV.br_blockcount;
2179 xfs_iext_remove(ip, icur, state);
2180 xfs_iext_prev(ifp, icur);
2181 xfs_iext_update_extent(ip, state, icur, &LEFT);
2182 XFS_IFORK_NEXT_SET(ip, whichfork,
2183 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
2185 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2187 rval = XFS_ILOG_CORE;
2188 error = xfs_bmbt_lookup_eq(cur, &PREV, &i);
2191 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2192 if ((error = xfs_btree_delete(cur, &i)))
2194 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2195 if ((error = xfs_btree_decrement(cur, 0, &i)))
2197 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2198 error = xfs_bmbt_update(cur, &LEFT);
2204 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
2206 * Setting all of a previous oldext extent to newext.
2207 * The right neighbor is contiguous, the left is not.
2209 PREV.br_blockcount += RIGHT.br_blockcount;
2210 PREV.br_state = new->br_state;
2212 xfs_iext_next(ifp, icur);
2213 xfs_iext_remove(ip, icur, state);
2214 xfs_iext_prev(ifp, icur);
2215 xfs_iext_update_extent(ip, state, icur, &PREV);
2217 XFS_IFORK_NEXT_SET(ip, whichfork,
2218 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
2220 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2222 rval = XFS_ILOG_CORE;
2223 error = xfs_bmbt_lookup_eq(cur, &RIGHT, &i);
2226 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2227 if ((error = xfs_btree_delete(cur, &i)))
2229 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2230 if ((error = xfs_btree_decrement(cur, 0, &i)))
2232 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2233 error = xfs_bmbt_update(cur, &PREV);
2239 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
2241 * Setting all of a previous oldext extent to newext.
2242 * Neither the left nor right neighbors are contiguous with
2245 PREV.br_state = new->br_state;
2246 xfs_iext_update_extent(ip, state, icur, &PREV);
2249 rval = XFS_ILOG_DEXT;
2252 error = xfs_bmbt_lookup_eq(cur, new, &i);
2255 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2256 error = xfs_bmbt_update(cur, &PREV);
2262 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
2264 * Setting the first part of a previous oldext extent to newext.
2265 * The left neighbor is contiguous.
2267 LEFT.br_blockcount += new->br_blockcount;
2270 PREV.br_startoff += new->br_blockcount;
2271 PREV.br_startblock += new->br_blockcount;
2272 PREV.br_blockcount -= new->br_blockcount;
2274 xfs_iext_update_extent(ip, state, icur, &PREV);
2275 xfs_iext_prev(ifp, icur);
2276 xfs_iext_update_extent(ip, state, icur, &LEFT);
2279 rval = XFS_ILOG_DEXT;
2282 error = xfs_bmbt_lookup_eq(cur, &old, &i);
2285 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2286 error = xfs_bmbt_update(cur, &PREV);
2289 error = xfs_btree_decrement(cur, 0, &i);
2292 error = xfs_bmbt_update(cur, &LEFT);
2298 case BMAP_LEFT_FILLING:
2300 * Setting the first part of a previous oldext extent to newext.
2301 * The left neighbor is not contiguous.
2304 PREV.br_startoff += new->br_blockcount;
2305 PREV.br_startblock += new->br_blockcount;
2306 PREV.br_blockcount -= new->br_blockcount;
2308 xfs_iext_update_extent(ip, state, icur, &PREV);
2309 xfs_iext_insert(ip, icur, new, state);
2310 XFS_IFORK_NEXT_SET(ip, whichfork,
2311 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
2313 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2315 rval = XFS_ILOG_CORE;
2316 error = xfs_bmbt_lookup_eq(cur, &old, &i);
2319 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2320 error = xfs_bmbt_update(cur, &PREV);
2323 cur->bc_rec.b = *new;
2324 if ((error = xfs_btree_insert(cur, &i)))
2326 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2330 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
2332 * Setting the last part of a previous oldext extent to newext.
2333 * The right neighbor is contiguous with the new allocation.
2336 PREV.br_blockcount -= new->br_blockcount;
2338 RIGHT.br_startoff = new->br_startoff;
2339 RIGHT.br_startblock = new->br_startblock;
2340 RIGHT.br_blockcount += new->br_blockcount;
2342 xfs_iext_update_extent(ip, state, icur, &PREV);
2343 xfs_iext_next(ifp, icur);
2344 xfs_iext_update_extent(ip, state, icur, &RIGHT);
2347 rval = XFS_ILOG_DEXT;
2350 error = xfs_bmbt_lookup_eq(cur, &old, &i);
2353 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2354 error = xfs_bmbt_update(cur, &PREV);
2357 error = xfs_btree_increment(cur, 0, &i);
2360 error = xfs_bmbt_update(cur, &RIGHT);
2366 case BMAP_RIGHT_FILLING:
2368 * Setting the last part of a previous oldext extent to newext.
2369 * The right neighbor is not contiguous.
2372 PREV.br_blockcount -= new->br_blockcount;
2374 xfs_iext_update_extent(ip, state, icur, &PREV);
2375 xfs_iext_next(ifp, icur);
2376 xfs_iext_insert(ip, icur, new, state);
2378 XFS_IFORK_NEXT_SET(ip, whichfork,
2379 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
2381 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2383 rval = XFS_ILOG_CORE;
2384 error = xfs_bmbt_lookup_eq(cur, &old, &i);
2387 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2388 error = xfs_bmbt_update(cur, &PREV);
2391 error = xfs_bmbt_lookup_eq(cur, new, &i);
2394 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
2395 if ((error = xfs_btree_insert(cur, &i)))
2397 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2403 * Setting the middle part of a previous oldext extent to
2404 * newext. Contiguity is impossible here.
2405 * One extent becomes three extents.
2408 PREV.br_blockcount = new->br_startoff - PREV.br_startoff;
2411 r[1].br_startoff = new_endoff;
2412 r[1].br_blockcount =
2413 old.br_startoff + old.br_blockcount - new_endoff;
2414 r[1].br_startblock = new->br_startblock + new->br_blockcount;
2415 r[1].br_state = PREV.br_state;
2417 xfs_iext_update_extent(ip, state, icur, &PREV);
2418 xfs_iext_next(ifp, icur);
2419 xfs_iext_insert(ip, icur, &r[1], state);
2420 xfs_iext_insert(ip, icur, &r[0], state);
2422 XFS_IFORK_NEXT_SET(ip, whichfork,
2423 XFS_IFORK_NEXTENTS(ip, whichfork) + 2);
2425 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2427 rval = XFS_ILOG_CORE;
2428 error = xfs_bmbt_lookup_eq(cur, &old, &i);
2431 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2432 /* new right extent - oldext */
2433 error = xfs_bmbt_update(cur, &r[1]);
2436 /* new left extent - oldext */
2437 cur->bc_rec.b = PREV;
2438 if ((error = xfs_btree_insert(cur, &i)))
2440 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2442 * Reset the cursor to the position of the new extent
2443 * we are about to insert as we can't trust it after
2444 * the previous insert.
2446 error = xfs_bmbt_lookup_eq(cur, new, &i);
2449 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
2450 /* new middle extent - newext */
2451 if ((error = xfs_btree_insert(cur, &i)))
2453 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2457 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2458 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2459 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
2460 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2461 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2462 case BMAP_LEFT_CONTIG:
2463 case BMAP_RIGHT_CONTIG:
2465 * These cases are all impossible.
2470 /* update reverse mappings */
2471 error = xfs_rmap_convert_extent(mp, tp, ip, whichfork, new);
2475 /* convert to a btree if necessary */
2476 if (xfs_bmap_needs_btree(ip, whichfork)) {
2477 int tmp_logflags; /* partial log flag return val */
2479 ASSERT(cur == NULL);
2480 error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0,
2481 &tmp_logflags, whichfork);
2482 *logflagsp |= tmp_logflags;
2487 /* clear out the allocated field, done with it now in any case. */
2489 cur->bc_private.b.allocated = 0;
2493 xfs_bmap_check_leaf_extents(*curp, ip, whichfork);
2503 * Convert a hole to a delayed allocation.
2506 xfs_bmap_add_extent_hole_delay(
2507 xfs_inode_t *ip, /* incore inode pointer */
2509 struct xfs_iext_cursor *icur,
2510 xfs_bmbt_irec_t *new) /* new data to add to file extents */
2512 struct xfs_ifork *ifp; /* inode fork pointer */
2513 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2514 xfs_filblks_t newlen=0; /* new indirect size */
2515 xfs_filblks_t oldlen=0; /* old indirect size */
2516 xfs_bmbt_irec_t right; /* right neighbor extent entry */
2517 int state = xfs_bmap_fork_to_state(whichfork);
2518 xfs_filblks_t temp; /* temp for indirect calculations */
2520 ifp = XFS_IFORK_PTR(ip, whichfork);
2521 ASSERT(isnullstartblock(new->br_startblock));
2524 * Check and set flags if this segment has a left neighbor
2526 if (xfs_iext_peek_prev_extent(ifp, icur, &left)) {
2527 state |= BMAP_LEFT_VALID;
2528 if (isnullstartblock(left.br_startblock))
2529 state |= BMAP_LEFT_DELAY;
2533 * Check and set flags if the current (right) segment exists.
2534 * If it doesn't exist, we're converting the hole at end-of-file.
2536 if (xfs_iext_get_extent(ifp, icur, &right)) {
2537 state |= BMAP_RIGHT_VALID;
2538 if (isnullstartblock(right.br_startblock))
2539 state |= BMAP_RIGHT_DELAY;
2543 * Set contiguity flags on the left and right neighbors.
2544 * Don't let extents get too large, even if the pieces are contiguous.
2546 if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) &&
2547 left.br_startoff + left.br_blockcount == new->br_startoff &&
2548 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2549 state |= BMAP_LEFT_CONTIG;
2551 if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) &&
2552 new->br_startoff + new->br_blockcount == right.br_startoff &&
2553 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2554 (!(state & BMAP_LEFT_CONTIG) ||
2555 (left.br_blockcount + new->br_blockcount +
2556 right.br_blockcount <= MAXEXTLEN)))
2557 state |= BMAP_RIGHT_CONTIG;
2560 * Switch out based on the contiguity flags.
2562 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2563 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2565 * New allocation is contiguous with delayed allocations
2566 * on the left and on the right.
2567 * Merge all three into a single extent record.
2569 temp = left.br_blockcount + new->br_blockcount +
2570 right.br_blockcount;
2572 oldlen = startblockval(left.br_startblock) +
2573 startblockval(new->br_startblock) +
2574 startblockval(right.br_startblock);
2575 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2577 left.br_startblock = nullstartblock(newlen);
2578 left.br_blockcount = temp;
2580 xfs_iext_remove(ip, icur, state);
2581 xfs_iext_prev(ifp, icur);
2582 xfs_iext_update_extent(ip, state, icur, &left);
2585 case BMAP_LEFT_CONTIG:
2587 * New allocation is contiguous with a delayed allocation
2589 * Merge the new allocation with the left neighbor.
2591 temp = left.br_blockcount + new->br_blockcount;
2593 oldlen = startblockval(left.br_startblock) +
2594 startblockval(new->br_startblock);
2595 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2597 left.br_blockcount = temp;
2598 left.br_startblock = nullstartblock(newlen);
2600 xfs_iext_prev(ifp, icur);
2601 xfs_iext_update_extent(ip, state, icur, &left);
2604 case BMAP_RIGHT_CONTIG:
2606 * New allocation is contiguous with a delayed allocation
2608 * Merge the new allocation with the right neighbor.
2610 temp = new->br_blockcount + right.br_blockcount;
2611 oldlen = startblockval(new->br_startblock) +
2612 startblockval(right.br_startblock);
2613 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2615 right.br_startoff = new->br_startoff;
2616 right.br_startblock = nullstartblock(newlen);
2617 right.br_blockcount = temp;
2618 xfs_iext_update_extent(ip, state, icur, &right);
2623 * New allocation is not contiguous with another
2624 * delayed allocation.
2625 * Insert a new entry.
2627 oldlen = newlen = 0;
2628 xfs_iext_insert(ip, icur, new, state);
2631 if (oldlen != newlen) {
2632 ASSERT(oldlen > newlen);
2633 xfs_mod_fdblocks(ip->i_mount, (int64_t)(oldlen - newlen),
2636 * Nothing to do for disk quota accounting here.
2642 * Convert a hole to a real allocation.
2644 STATIC int /* error */
2645 xfs_bmap_add_extent_hole_real(
2646 struct xfs_trans *tp,
2647 struct xfs_inode *ip,
2649 struct xfs_iext_cursor *icur,
2650 struct xfs_btree_cur **curp,
2651 struct xfs_bmbt_irec *new,
2655 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
2656 struct xfs_mount *mp = ip->i_mount;
2657 struct xfs_btree_cur *cur = *curp;
2658 int error; /* error return value */
2659 int i; /* temp state */
2660 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2661 xfs_bmbt_irec_t right; /* right neighbor extent entry */
2662 int rval=0; /* return value (logging flags) */
2663 int state = xfs_bmap_fork_to_state(whichfork);
2664 struct xfs_bmbt_irec old;
2666 ASSERT(!isnullstartblock(new->br_startblock));
2667 ASSERT(!cur || !(cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
2669 XFS_STATS_INC(mp, xs_add_exlist);
2672 * Check and set flags if this segment has a left neighbor.
2674 if (xfs_iext_peek_prev_extent(ifp, icur, &left)) {
2675 state |= BMAP_LEFT_VALID;
2676 if (isnullstartblock(left.br_startblock))
2677 state |= BMAP_LEFT_DELAY;
2681 * Check and set flags if this segment has a current value.
2682 * Not true if we're inserting into the "hole" at eof.
2684 if (xfs_iext_get_extent(ifp, icur, &right)) {
2685 state |= BMAP_RIGHT_VALID;
2686 if (isnullstartblock(right.br_startblock))
2687 state |= BMAP_RIGHT_DELAY;
2691 * We're inserting a real allocation between "left" and "right".
2692 * Set the contiguity flags. Don't let extents get too large.
2694 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2695 left.br_startoff + left.br_blockcount == new->br_startoff &&
2696 left.br_startblock + left.br_blockcount == new->br_startblock &&
2697 left.br_state == new->br_state &&
2698 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2699 state |= BMAP_LEFT_CONTIG;
2701 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2702 new->br_startoff + new->br_blockcount == right.br_startoff &&
2703 new->br_startblock + new->br_blockcount == right.br_startblock &&
2704 new->br_state == right.br_state &&
2705 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2706 (!(state & BMAP_LEFT_CONTIG) ||
2707 left.br_blockcount + new->br_blockcount +
2708 right.br_blockcount <= MAXEXTLEN))
2709 state |= BMAP_RIGHT_CONTIG;
2713 * Select which case we're in here, and implement it.
2715 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2716 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2718 * New allocation is contiguous with real allocations on the
2719 * left and on the right.
2720 * Merge all three into a single extent record.
2722 left.br_blockcount += new->br_blockcount + right.br_blockcount;
2724 xfs_iext_remove(ip, icur, state);
2725 xfs_iext_prev(ifp, icur);
2726 xfs_iext_update_extent(ip, state, icur, &left);
2728 XFS_IFORK_NEXT_SET(ip, whichfork,
2729 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
2731 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
2733 rval = XFS_ILOG_CORE;
2734 error = xfs_bmbt_lookup_eq(cur, &right, &i);
2737 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2738 error = xfs_btree_delete(cur, &i);
2741 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2742 error = xfs_btree_decrement(cur, 0, &i);
2745 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2746 error = xfs_bmbt_update(cur, &left);
2752 case BMAP_LEFT_CONTIG:
2754 * New allocation is contiguous with a real allocation
2756 * Merge the new allocation with the left neighbor.
2759 left.br_blockcount += new->br_blockcount;
2761 xfs_iext_prev(ifp, icur);
2762 xfs_iext_update_extent(ip, state, icur, &left);
2765 rval = xfs_ilog_fext(whichfork);
2768 error = xfs_bmbt_lookup_eq(cur, &old, &i);
2771 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2772 error = xfs_bmbt_update(cur, &left);
2778 case BMAP_RIGHT_CONTIG:
2780 * New allocation is contiguous with a real allocation
2782 * Merge the new allocation with the right neighbor.
2786 right.br_startoff = new->br_startoff;
2787 right.br_startblock = new->br_startblock;
2788 right.br_blockcount += new->br_blockcount;
2789 xfs_iext_update_extent(ip, state, icur, &right);
2792 rval = xfs_ilog_fext(whichfork);
2795 error = xfs_bmbt_lookup_eq(cur, &old, &i);
2798 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2799 error = xfs_bmbt_update(cur, &right);
2807 * New allocation is not contiguous with another
2809 * Insert a new entry.
2811 xfs_iext_insert(ip, icur, new, state);
2812 XFS_IFORK_NEXT_SET(ip, whichfork,
2813 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
2815 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
2817 rval = XFS_ILOG_CORE;
2818 error = xfs_bmbt_lookup_eq(cur, new, &i);
2821 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
2822 error = xfs_btree_insert(cur, &i);
2825 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2830 /* add reverse mapping unless caller opted out */
2831 if (!(flags & XFS_BMAPI_NORMAP)) {
2832 error = xfs_rmap_map_extent(tp, ip, whichfork, new);
2837 /* convert to a btree if necessary */
2838 if (xfs_bmap_needs_btree(ip, whichfork)) {
2839 int tmp_logflags; /* partial log flag return val */
2841 ASSERT(cur == NULL);
2842 error = xfs_bmap_extents_to_btree(tp, ip, curp, 0,
2843 &tmp_logflags, whichfork);
2844 *logflagsp |= tmp_logflags;
2850 /* clear out the allocated field, done with it now in any case. */
2852 cur->bc_private.b.allocated = 0;
2854 xfs_bmap_check_leaf_extents(cur, ip, whichfork);
2861 * Functions used in the extent read, allocate and remove paths
2865 * Adjust the size of the new extent based on di_extsize and rt extsize.
2868 xfs_bmap_extsize_align(
2870 xfs_bmbt_irec_t *gotp, /* next extent pointer */
2871 xfs_bmbt_irec_t *prevp, /* previous extent pointer */
2872 xfs_extlen_t extsz, /* align to this extent size */
2873 int rt, /* is this a realtime inode? */
2874 int eof, /* is extent at end-of-file? */
2875 int delay, /* creating delalloc extent? */
2876 int convert, /* overwriting unwritten extent? */
2877 xfs_fileoff_t *offp, /* in/out: aligned offset */
2878 xfs_extlen_t *lenp) /* in/out: aligned length */
2880 xfs_fileoff_t orig_off; /* original offset */
2881 xfs_extlen_t orig_alen; /* original length */
2882 xfs_fileoff_t orig_end; /* original off+len */
2883 xfs_fileoff_t nexto; /* next file offset */
2884 xfs_fileoff_t prevo; /* previous file offset */
2885 xfs_fileoff_t align_off; /* temp for offset */
2886 xfs_extlen_t align_alen; /* temp for length */
2887 xfs_extlen_t temp; /* temp for calculations */
2892 orig_off = align_off = *offp;
2893 orig_alen = align_alen = *lenp;
2894 orig_end = orig_off + orig_alen;
2897 * If this request overlaps an existing extent, then don't
2898 * attempt to perform any additional alignment.
2900 if (!delay && !eof &&
2901 (orig_off >= gotp->br_startoff) &&
2902 (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
2907 * If the file offset is unaligned vs. the extent size
2908 * we need to align it. This will be possible unless
2909 * the file was previously written with a kernel that didn't
2910 * perform this alignment, or if a truncate shot us in the
2913 div_u64_rem(orig_off, extsz, &temp);
2919 /* Same adjustment for the end of the requested area. */
2920 temp = (align_alen % extsz);
2922 align_alen += extsz - temp;
2925 * For large extent hint sizes, the aligned extent might be larger than
2926 * MAXEXTLEN. In that case, reduce the size by an extsz so that it pulls
2927 * the length back under MAXEXTLEN. The outer allocation loops handle
2928 * short allocation just fine, so it is safe to do this. We only want to
2929 * do it when we are forced to, though, because it means more allocation
2930 * operations are required.
2932 while (align_alen > MAXEXTLEN)
2933 align_alen -= extsz;
2934 ASSERT(align_alen <= MAXEXTLEN);
2937 * If the previous block overlaps with this proposed allocation
2938 * then move the start forward without adjusting the length.
2940 if (prevp->br_startoff != NULLFILEOFF) {
2941 if (prevp->br_startblock == HOLESTARTBLOCK)
2942 prevo = prevp->br_startoff;
2944 prevo = prevp->br_startoff + prevp->br_blockcount;
2947 if (align_off != orig_off && align_off < prevo)
2950 * If the next block overlaps with this proposed allocation
2951 * then move the start back without adjusting the length,
2952 * but not before offset 0.
2953 * This may of course make the start overlap previous block,
2954 * and if we hit the offset 0 limit then the next block
2955 * can still overlap too.
2957 if (!eof && gotp->br_startoff != NULLFILEOFF) {
2958 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
2959 (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
2960 nexto = gotp->br_startoff + gotp->br_blockcount;
2962 nexto = gotp->br_startoff;
2964 nexto = NULLFILEOFF;
2966 align_off + align_alen != orig_end &&
2967 align_off + align_alen > nexto)
2968 align_off = nexto > align_alen ? nexto - align_alen : 0;
2970 * If we're now overlapping the next or previous extent that
2971 * means we can't fit an extsz piece in this hole. Just move
2972 * the start forward to the first valid spot and set
2973 * the length so we hit the end.
2975 if (align_off != orig_off && align_off < prevo)
2977 if (align_off + align_alen != orig_end &&
2978 align_off + align_alen > nexto &&
2979 nexto != NULLFILEOFF) {
2980 ASSERT(nexto > prevo);
2981 align_alen = nexto - align_off;
2985 * If realtime, and the result isn't a multiple of the realtime
2986 * extent size we need to remove blocks until it is.