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_da_format.h"
28 #include "xfs_da_btree.h"
30 #include "xfs_inode.h"
31 #include "xfs_btree.h"
32 #include "xfs_trans.h"
33 #include "xfs_inode_item.h"
34 #include "xfs_extfree_item.h"
35 #include "xfs_alloc.h"
37 #include "xfs_bmap_util.h"
38 #include "xfs_bmap_btree.h"
39 #include "xfs_rtalloc.h"
40 #include "xfs_error.h"
41 #include "xfs_quota.h"
42 #include "xfs_trans_space.h"
43 #include "xfs_buf_item.h"
44 #include "xfs_trace.h"
45 #include "xfs_symlink.h"
46 #include "xfs_attr_leaf.h"
47 #include "xfs_filestream.h"
50 kmem_zone_t *xfs_bmap_free_item_zone;
53 * Miscellaneous helper functions
57 * Compute and fill in the value of the maximum depth of a bmap btree
58 * in this filesystem. Done once, during mount.
61 xfs_bmap_compute_maxlevels(
62 xfs_mount_t *mp, /* file system mount structure */
63 int whichfork) /* data or attr fork */
65 int level; /* btree level */
66 uint maxblocks; /* max blocks at this level */
67 uint maxleafents; /* max leaf entries possible */
68 int maxrootrecs; /* max records in root block */
69 int minleafrecs; /* min records in leaf block */
70 int minnoderecs; /* min records in node block */
71 int sz; /* root block size */
74 * The maximum number of extents in a file, hence the maximum
75 * number of leaf entries, is controlled by the type of di_nextents
76 * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
77 * (a signed 16-bit number, xfs_aextnum_t).
79 * Note that we can no longer assume that if we are in ATTR1 that
80 * the fork offset of all the inodes will be
81 * (xfs_default_attroffset(ip) >> 3) because we could have mounted
82 * with ATTR2 and then mounted back with ATTR1, keeping the
83 * di_forkoff's fixed but probably at various positions. Therefore,
84 * for both ATTR1 and ATTR2 we have to assume the worst case scenario
85 * of a minimum size available.
87 if (whichfork == XFS_DATA_FORK) {
88 maxleafents = MAXEXTNUM;
89 sz = XFS_BMDR_SPACE_CALC(MINDBTPTRS);
91 maxleafents = MAXAEXTNUM;
92 sz = XFS_BMDR_SPACE_CALC(MINABTPTRS);
94 maxrootrecs = xfs_bmdr_maxrecs(sz, 0);
95 minleafrecs = mp->m_bmap_dmnr[0];
96 minnoderecs = mp->m_bmap_dmnr[1];
97 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
98 for (level = 1; maxblocks > 1; level++) {
99 if (maxblocks <= maxrootrecs)
102 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
104 mp->m_bm_maxlevels[whichfork] = level;
107 STATIC int /* error */
109 struct xfs_btree_cur *cur,
113 int *stat) /* success/failure */
115 cur->bc_rec.b.br_startoff = off;
116 cur->bc_rec.b.br_startblock = bno;
117 cur->bc_rec.b.br_blockcount = len;
118 return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
121 STATIC int /* error */
123 struct xfs_btree_cur *cur,
127 int *stat) /* success/failure */
129 cur->bc_rec.b.br_startoff = off;
130 cur->bc_rec.b.br_startblock = bno;
131 cur->bc_rec.b.br_blockcount = len;
132 return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
136 * Check if the inode needs to be converted to btree format.
138 static inline bool xfs_bmap_needs_btree(struct xfs_inode *ip, int whichfork)
140 return 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 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
151 XFS_IFORK_NEXTENTS(ip, whichfork) <=
152 XFS_IFORK_MAXEXT(ip, whichfork);
156 * Update the record referred to by cur to the value given
157 * by [off, bno, len, state].
158 * This either works (return 0) or gets an EFSCORRUPTED error.
162 struct xfs_btree_cur *cur,
168 union xfs_btree_rec rec;
170 xfs_bmbt_disk_set_allf(&rec.bmbt, off, bno, len, state);
171 return xfs_btree_update(cur, &rec);
175 * Compute the worst-case number of indirect blocks that will be used
176 * for ip's delayed extent of length "len".
179 xfs_bmap_worst_indlen(
180 xfs_inode_t *ip, /* incore inode pointer */
181 xfs_filblks_t len) /* delayed extent length */
183 int level; /* btree level number */
184 int maxrecs; /* maximum record count at this level */
185 xfs_mount_t *mp; /* mount structure */
186 xfs_filblks_t rval; /* return value */
189 maxrecs = mp->m_bmap_dmxr[0];
190 for (level = 0, rval = 0;
191 level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK);
194 do_div(len, maxrecs);
197 return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) -
200 maxrecs = mp->m_bmap_dmxr[1];
206 * Calculate the default attribute fork offset for newly created inodes.
209 xfs_default_attroffset(
210 struct xfs_inode *ip)
212 struct xfs_mount *mp = ip->i_mount;
215 if (mp->m_sb.sb_inodesize == 256) {
216 offset = XFS_LITINO(mp, ip->i_d.di_version) -
217 XFS_BMDR_SPACE_CALC(MINABTPTRS);
219 offset = XFS_BMDR_SPACE_CALC(6 * MINABTPTRS);
222 ASSERT(offset < XFS_LITINO(mp, ip->i_d.di_version));
227 * Helper routine to reset inode di_forkoff field when switching
228 * attribute fork from local to extent format - we reset it where
229 * possible to make space available for inline data fork extents.
232 xfs_bmap_forkoff_reset(
236 if (whichfork == XFS_ATTR_FORK &&
237 ip->i_d.di_format != XFS_DINODE_FMT_DEV &&
238 ip->i_d.di_format != XFS_DINODE_FMT_UUID &&
239 ip->i_d.di_format != XFS_DINODE_FMT_BTREE) {
240 uint dfl_forkoff = xfs_default_attroffset(ip) >> 3;
242 if (dfl_forkoff > ip->i_d.di_forkoff)
243 ip->i_d.di_forkoff = dfl_forkoff;
248 * Debug/sanity checking code
252 xfs_bmap_sanity_check(
253 struct xfs_mount *mp,
257 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
259 if (block->bb_magic != cpu_to_be32(XFS_BMAP_CRC_MAGIC) &&
260 block->bb_magic != cpu_to_be32(XFS_BMAP_MAGIC))
263 if (be16_to_cpu(block->bb_level) != level ||
264 be16_to_cpu(block->bb_numrecs) == 0 ||
265 be16_to_cpu(block->bb_numrecs) > mp->m_bmap_dmxr[level != 0])
272 STATIC struct xfs_buf *
274 struct xfs_btree_cur *cur,
277 struct xfs_log_item_desc *lidp;
283 for (i = 0; i < XFS_BTREE_MAXLEVELS; i++) {
284 if (!cur->bc_bufs[i])
286 if (XFS_BUF_ADDR(cur->bc_bufs[i]) == bno)
287 return cur->bc_bufs[i];
290 /* Chase down all the log items to see if the bp is there */
291 list_for_each_entry(lidp, &cur->bc_tp->t_items, lid_trans) {
292 struct xfs_buf_log_item *bip;
293 bip = (struct xfs_buf_log_item *)lidp->lid_item;
294 if (bip->bli_item.li_type == XFS_LI_BUF &&
295 XFS_BUF_ADDR(bip->bli_buf) == bno)
304 struct xfs_btree_block *block,
310 __be64 *pp, *thispa; /* pointer to block address */
311 xfs_bmbt_key_t *prevp, *keyp;
313 ASSERT(be16_to_cpu(block->bb_level) > 0);
316 for( i = 1; i <= xfs_btree_get_numrecs(block); i++) {
317 dmxr = mp->m_bmap_dmxr[0];
318 keyp = XFS_BMBT_KEY_ADDR(mp, block, i);
321 ASSERT(be64_to_cpu(prevp->br_startoff) <
322 be64_to_cpu(keyp->br_startoff));
327 * Compare the block numbers to see if there are dups.
330 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, i, sz);
332 pp = XFS_BMBT_PTR_ADDR(mp, block, i, dmxr);
334 for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) {
336 thispa = XFS_BMAP_BROOT_PTR_ADDR(mp, block, j, sz);
338 thispa = XFS_BMBT_PTR_ADDR(mp, block, j, dmxr);
339 if (*thispa == *pp) {
340 xfs_warn(mp, "%s: thispa(%d) == pp(%d) %Ld",
342 (unsigned long long)be64_to_cpu(*thispa));
343 panic("%s: ptrs are equal in node\n",
351 * Check that the extents for the inode ip are in the right order in all
356 xfs_bmap_check_leaf_extents(
357 xfs_btree_cur_t *cur, /* btree cursor or null */
358 xfs_inode_t *ip, /* incore inode pointer */
359 int whichfork) /* data or attr fork */
361 struct xfs_btree_block *block; /* current btree block */
362 xfs_fsblock_t bno; /* block # of "block" */
363 xfs_buf_t *bp; /* buffer for "block" */
364 int error; /* error return value */
365 xfs_extnum_t i=0, j; /* index into the extents list */
366 xfs_ifork_t *ifp; /* fork structure */
367 int level; /* btree level, for checking */
368 xfs_mount_t *mp; /* file system mount structure */
369 __be64 *pp; /* pointer to block address */
370 xfs_bmbt_rec_t *ep; /* pointer to current extent */
371 xfs_bmbt_rec_t last = {0, 0}; /* last extent in prev block */
372 xfs_bmbt_rec_t *nextp; /* pointer to next extent */
375 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) {
381 ifp = XFS_IFORK_PTR(ip, whichfork);
382 block = ifp->if_broot;
384 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
386 level = be16_to_cpu(block->bb_level);
388 xfs_check_block(block, mp, 1, ifp->if_broot_bytes);
389 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
390 bno = be64_to_cpu(*pp);
392 ASSERT(bno != NULLFSBLOCK);
393 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
394 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
397 * Go down the tree until leaf level is reached, following the first
398 * pointer (leftmost) at each level.
400 while (level-- > 0) {
401 /* See if buf is in cur first */
403 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
406 error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
412 block = XFS_BUF_TO_BLOCK(bp);
413 XFS_WANT_CORRUPTED_GOTO(
414 xfs_bmap_sanity_check(mp, bp, level),
420 * Check this block for basic sanity (increasing keys and
421 * no duplicate blocks).
424 xfs_check_block(block, mp, 0, 0);
425 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
426 bno = be64_to_cpu(*pp);
427 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0);
430 xfs_trans_brelse(NULL, bp);
435 * Here with bp and block set to the leftmost leaf node in the tree.
440 * Loop over all leaf nodes checking that all extents are in the right order.
443 xfs_fsblock_t nextbno;
444 xfs_extnum_t num_recs;
447 num_recs = xfs_btree_get_numrecs(block);
450 * Read-ahead the next leaf block, if any.
453 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
456 * Check all the extents to make sure they are OK.
457 * If we had a previous block, the last entry should
458 * conform with the first entry in this one.
461 ep = XFS_BMBT_REC_ADDR(mp, block, 1);
463 ASSERT(xfs_bmbt_disk_get_startoff(&last) +
464 xfs_bmbt_disk_get_blockcount(&last) <=
465 xfs_bmbt_disk_get_startoff(ep));
467 for (j = 1; j < num_recs; j++) {
468 nextp = XFS_BMBT_REC_ADDR(mp, block, j + 1);
469 ASSERT(xfs_bmbt_disk_get_startoff(ep) +
470 xfs_bmbt_disk_get_blockcount(ep) <=
471 xfs_bmbt_disk_get_startoff(nextp));
479 xfs_trans_brelse(NULL, bp);
483 * If we've reached the end, stop.
485 if (bno == NULLFSBLOCK)
489 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
492 error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
498 block = XFS_BUF_TO_BLOCK(bp);
502 xfs_trans_brelse(NULL, bp);
507 xfs_warn(mp, "%s: at error0", __func__);
509 xfs_trans_brelse(NULL, bp);
511 xfs_warn(mp, "%s: BAD after btree leaves for %d extents",
513 panic("%s: CORRUPTED BTREE OR SOMETHING", __func__);
518 * Add bmap trace insert entries for all the contents of the extent records.
521 xfs_bmap_trace_exlist(
522 xfs_inode_t *ip, /* incore inode pointer */
523 xfs_extnum_t cnt, /* count of entries in the list */
524 int whichfork, /* data or attr fork */
525 unsigned long caller_ip)
527 xfs_extnum_t idx; /* extent record index */
528 xfs_ifork_t *ifp; /* inode fork pointer */
531 if (whichfork == XFS_ATTR_FORK)
532 state |= BMAP_ATTRFORK;
534 ifp = XFS_IFORK_PTR(ip, whichfork);
535 ASSERT(cnt == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
536 for (idx = 0; idx < cnt; idx++)
537 trace_xfs_extlist(ip, idx, whichfork, caller_ip);
541 * Validate that the bmbt_irecs being returned from bmapi are valid
542 * given the caller's original parameters. Specifically check the
543 * ranges of the returned irecs to ensure that they only extend beyond
544 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
547 xfs_bmap_validate_ret(
551 xfs_bmbt_irec_t *mval,
555 int i; /* index to map values */
557 ASSERT(ret_nmap <= nmap);
559 for (i = 0; i < ret_nmap; i++) {
560 ASSERT(mval[i].br_blockcount > 0);
561 if (!(flags & XFS_BMAPI_ENTIRE)) {
562 ASSERT(mval[i].br_startoff >= bno);
563 ASSERT(mval[i].br_blockcount <= len);
564 ASSERT(mval[i].br_startoff + mval[i].br_blockcount <=
567 ASSERT(mval[i].br_startoff < bno + len);
568 ASSERT(mval[i].br_startoff + mval[i].br_blockcount >
572 mval[i - 1].br_startoff + mval[i - 1].br_blockcount ==
573 mval[i].br_startoff);
574 ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK &&
575 mval[i].br_startblock != HOLESTARTBLOCK);
576 ASSERT(mval[i].br_state == XFS_EXT_NORM ||
577 mval[i].br_state == XFS_EXT_UNWRITTEN);
582 #define xfs_bmap_check_leaf_extents(cur, ip, whichfork) do { } while (0)
583 #define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap)
587 * bmap free list manipulation functions
591 * Add the extent to the list of extents to be free at transaction end.
592 * The list is maintained sorted (by block number).
596 xfs_fsblock_t bno, /* fs block number of extent */
597 xfs_filblks_t len, /* length of extent */
598 xfs_bmap_free_t *flist, /* list of extents */
599 xfs_mount_t *mp) /* mount point structure */
601 xfs_bmap_free_item_t *cur; /* current (next) element */
602 xfs_bmap_free_item_t *new; /* new element */
603 xfs_bmap_free_item_t *prev; /* previous element */
608 ASSERT(bno != NULLFSBLOCK);
610 ASSERT(len <= MAXEXTLEN);
611 ASSERT(!isnullstartblock(bno));
612 agno = XFS_FSB_TO_AGNO(mp, bno);
613 agbno = XFS_FSB_TO_AGBNO(mp, bno);
614 ASSERT(agno < mp->m_sb.sb_agcount);
615 ASSERT(agbno < mp->m_sb.sb_agblocks);
616 ASSERT(len < mp->m_sb.sb_agblocks);
617 ASSERT(agbno + len <= mp->m_sb.sb_agblocks);
619 ASSERT(xfs_bmap_free_item_zone != NULL);
620 new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP);
621 new->xbfi_startblock = bno;
622 new->xbfi_blockcount = (xfs_extlen_t)len;
623 for (prev = NULL, cur = flist->xbf_first;
625 prev = cur, cur = cur->xbfi_next) {
626 if (cur->xbfi_startblock >= bno)
630 prev->xbfi_next = new;
632 flist->xbf_first = new;
633 new->xbfi_next = cur;
638 * Remove the entry "free" from the free item list. Prev points to the
639 * previous entry, unless "free" is the head of the list.
643 xfs_bmap_free_t *flist, /* free item list header */
644 xfs_bmap_free_item_t *prev, /* previous item on list, if any */
645 xfs_bmap_free_item_t *free) /* list item to be freed */
648 prev->xbfi_next = free->xbfi_next;
650 flist->xbf_first = free->xbfi_next;
652 kmem_zone_free(xfs_bmap_free_item_zone, free);
656 * Free up any items left in the list.
660 xfs_bmap_free_t *flist) /* list of bmap_free_items */
662 xfs_bmap_free_item_t *free; /* free list item */
663 xfs_bmap_free_item_t *next;
665 if (flist->xbf_count == 0)
667 ASSERT(flist->xbf_first != NULL);
668 for (free = flist->xbf_first; free; free = next) {
669 next = free->xbfi_next;
670 xfs_bmap_del_free(flist, NULL, free);
672 ASSERT(flist->xbf_count == 0);
676 * Inode fork format manipulation functions
680 * Transform a btree format file with only one leaf node, where the
681 * extents list will fit in the inode, into an extents format file.
682 * Since the file extents are already in-core, all we have to do is
683 * give up the space for the btree root and pitch the leaf block.
685 STATIC int /* error */
686 xfs_bmap_btree_to_extents(
687 xfs_trans_t *tp, /* transaction pointer */
688 xfs_inode_t *ip, /* incore inode pointer */
689 xfs_btree_cur_t *cur, /* btree cursor */
690 int *logflagsp, /* inode logging flags */
691 int whichfork) /* data or attr fork */
694 struct xfs_btree_block *cblock;/* child btree block */
695 xfs_fsblock_t cbno; /* child block number */
696 xfs_buf_t *cbp; /* child block's buffer */
697 int error; /* error return value */
698 xfs_ifork_t *ifp; /* inode fork data */
699 xfs_mount_t *mp; /* mount point structure */
700 __be64 *pp; /* ptr to block address */
701 struct xfs_btree_block *rblock;/* root btree block */
704 ifp = XFS_IFORK_PTR(ip, whichfork);
705 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
706 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
707 rblock = ifp->if_broot;
708 ASSERT(be16_to_cpu(rblock->bb_level) == 1);
709 ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1);
710 ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0) == 1);
711 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, ifp->if_broot_bytes);
712 cbno = be64_to_cpu(*pp);
715 if ((error = xfs_btree_check_lptr(cur, cbno, 1)))
718 error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp, XFS_BMAP_BTREE_REF,
722 cblock = XFS_BUF_TO_BLOCK(cbp);
723 if ((error = xfs_btree_check_block(cur, cblock, 0, cbp)))
725 xfs_bmap_add_free(cbno, 1, cur->bc_private.b.flist, mp);
726 ip->i_d.di_nblocks--;
727 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
728 xfs_trans_binval(tp, cbp);
729 if (cur->bc_bufs[0] == cbp)
730 cur->bc_bufs[0] = NULL;
731 xfs_iroot_realloc(ip, -1, whichfork);
732 ASSERT(ifp->if_broot == NULL);
733 ASSERT((ifp->if_flags & XFS_IFBROOT) == 0);
734 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
735 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
740 * Convert an extents-format file into a btree-format file.
741 * The new file will have a root block (in the inode) and a single child block.
743 STATIC int /* error */
744 xfs_bmap_extents_to_btree(
745 xfs_trans_t *tp, /* transaction pointer */
746 xfs_inode_t *ip, /* incore inode pointer */
747 xfs_fsblock_t *firstblock, /* first-block-allocated */
748 xfs_bmap_free_t *flist, /* blocks freed in xaction */
749 xfs_btree_cur_t **curp, /* cursor returned to caller */
750 int wasdel, /* converting a delayed alloc */
751 int *logflagsp, /* inode logging flags */
752 int whichfork) /* data or attr fork */
754 struct xfs_btree_block *ablock; /* allocated (child) bt block */
755 xfs_buf_t *abp; /* buffer for ablock */
756 xfs_alloc_arg_t args; /* allocation arguments */
757 xfs_bmbt_rec_t *arp; /* child record pointer */
758 struct xfs_btree_block *block; /* btree root block */
759 xfs_btree_cur_t *cur; /* bmap btree cursor */
760 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
761 int error; /* error return value */
762 xfs_extnum_t i, cnt; /* extent record index */
763 xfs_ifork_t *ifp; /* inode fork pointer */
764 xfs_bmbt_key_t *kp; /* root block key pointer */
765 xfs_mount_t *mp; /* mount structure */
766 xfs_extnum_t nextents; /* number of file extents */
767 xfs_bmbt_ptr_t *pp; /* root block address pointer */
770 ifp = XFS_IFORK_PTR(ip, whichfork);
771 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS);
774 * Make space in the inode incore.
776 xfs_iroot_realloc(ip, 1, whichfork);
777 ifp->if_flags |= XFS_IFBROOT;
782 block = ifp->if_broot;
783 if (xfs_sb_version_hascrc(&mp->m_sb))
784 xfs_btree_init_block_int(mp, block, XFS_BUF_DADDR_NULL,
785 XFS_BMAP_CRC_MAGIC, 1, 1, ip->i_ino,
786 XFS_BTREE_LONG_PTRS | XFS_BTREE_CRC_BLOCKS);
788 xfs_btree_init_block_int(mp, block, XFS_BUF_DADDR_NULL,
789 XFS_BMAP_MAGIC, 1, 1, ip->i_ino,
790 XFS_BTREE_LONG_PTRS);
793 * Need a cursor. Can't allocate until bb_level is filled in.
795 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
796 cur->bc_private.b.firstblock = *firstblock;
797 cur->bc_private.b.flist = flist;
798 cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
800 * Convert to a btree with two levels, one record in root.
802 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE);
803 memset(&args, 0, sizeof(args));
806 args.firstblock = *firstblock;
807 if (*firstblock == NULLFSBLOCK) {
808 args.type = XFS_ALLOCTYPE_START_BNO;
809 args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino);
810 } else if (flist->xbf_low) {
811 args.type = XFS_ALLOCTYPE_START_BNO;
812 args.fsbno = *firstblock;
814 args.type = XFS_ALLOCTYPE_NEAR_BNO;
815 args.fsbno = *firstblock;
817 args.minlen = args.maxlen = args.prod = 1;
818 args.wasdel = wasdel;
820 if ((error = xfs_alloc_vextent(&args))) {
821 xfs_iroot_realloc(ip, -1, whichfork);
822 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
826 * Allocation can't fail, the space was reserved.
828 ASSERT(args.fsbno != NULLFSBLOCK);
829 ASSERT(*firstblock == NULLFSBLOCK ||
830 args.agno == XFS_FSB_TO_AGNO(mp, *firstblock) ||
832 args.agno > XFS_FSB_TO_AGNO(mp, *firstblock)));
833 *firstblock = cur->bc_private.b.firstblock = args.fsbno;
834 cur->bc_private.b.allocated++;
835 ip->i_d.di_nblocks++;
836 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
837 abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0);
839 * Fill in the child block.
841 abp->b_ops = &xfs_bmbt_buf_ops;
842 ablock = XFS_BUF_TO_BLOCK(abp);
843 if (xfs_sb_version_hascrc(&mp->m_sb))
844 xfs_btree_init_block_int(mp, ablock, abp->b_bn,
845 XFS_BMAP_CRC_MAGIC, 0, 0, ip->i_ino,
846 XFS_BTREE_LONG_PTRS | XFS_BTREE_CRC_BLOCKS);
848 xfs_btree_init_block_int(mp, ablock, abp->b_bn,
849 XFS_BMAP_MAGIC, 0, 0, ip->i_ino,
850 XFS_BTREE_LONG_PTRS);
852 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
853 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
854 for (cnt = i = 0; i < nextents; i++) {
855 ep = xfs_iext_get_ext(ifp, i);
856 if (!isnullstartblock(xfs_bmbt_get_startblock(ep))) {
857 arp->l0 = cpu_to_be64(ep->l0);
858 arp->l1 = cpu_to_be64(ep->l1);
862 ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork));
863 xfs_btree_set_numrecs(ablock, cnt);
866 * Fill in the root key and pointer.
868 kp = XFS_BMBT_KEY_ADDR(mp, block, 1);
869 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
870 kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp));
871 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur,
872 be16_to_cpu(block->bb_level)));
873 *pp = cpu_to_be64(args.fsbno);
876 * Do all this logging at the end so that
877 * the root is at the right level.
879 xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS);
880 xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs));
881 ASSERT(*curp == NULL);
883 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork);
888 * Convert a local file to an extents file.
889 * This code is out of bounds for data forks of regular files,
890 * since the file data needs to get logged so things will stay consistent.
891 * (The bmap-level manipulations are ok, though).
894 xfs_bmap_local_to_extents_empty(
895 struct xfs_inode *ip,
898 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
900 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
901 ASSERT(ifp->if_bytes == 0);
902 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0);
904 xfs_bmap_forkoff_reset(ip, whichfork);
905 ifp->if_flags &= ~XFS_IFINLINE;
906 ifp->if_flags |= XFS_IFEXTENTS;
907 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
911 STATIC int /* error */
912 xfs_bmap_local_to_extents(
913 xfs_trans_t *tp, /* transaction pointer */
914 xfs_inode_t *ip, /* incore inode pointer */
915 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
916 xfs_extlen_t total, /* total blocks needed by transaction */
917 int *logflagsp, /* inode logging flags */
919 void (*init_fn)(struct xfs_trans *tp,
921 struct xfs_inode *ip,
922 struct xfs_ifork *ifp))
925 int flags; /* logging flags returned */
926 xfs_ifork_t *ifp; /* inode fork pointer */
927 xfs_alloc_arg_t args; /* allocation arguments */
928 xfs_buf_t *bp; /* buffer for extent block */
929 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
932 * We don't want to deal with the case of keeping inode data inline yet.
933 * So sending the data fork of a regular inode is invalid.
935 ASSERT(!(S_ISREG(ip->i_d.di_mode) && whichfork == XFS_DATA_FORK));
936 ifp = XFS_IFORK_PTR(ip, whichfork);
937 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
939 if (!ifp->if_bytes) {
940 xfs_bmap_local_to_extents_empty(ip, whichfork);
941 flags = XFS_ILOG_CORE;
947 ASSERT((ifp->if_flags & (XFS_IFINLINE|XFS_IFEXTENTS|XFS_IFEXTIREC)) ==
949 memset(&args, 0, sizeof(args));
951 args.mp = ip->i_mount;
952 args.firstblock = *firstblock;
954 * Allocate a block. We know we need only one, since the
955 * file currently fits in an inode.
957 if (*firstblock == NULLFSBLOCK) {
958 args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino);
959 args.type = XFS_ALLOCTYPE_START_BNO;
961 args.fsbno = *firstblock;
962 args.type = XFS_ALLOCTYPE_NEAR_BNO;
965 args.minlen = args.maxlen = args.prod = 1;
966 error = xfs_alloc_vextent(&args);
970 /* Can't fail, the space was reserved. */
971 ASSERT(args.fsbno != NULLFSBLOCK);
972 ASSERT(args.len == 1);
973 *firstblock = args.fsbno;
974 bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0);
976 /* initialise the block and copy the data */
977 init_fn(tp, bp, ip, ifp);
979 /* account for the change in fork size and log everything */
980 xfs_trans_log_buf(tp, bp, 0, ifp->if_bytes - 1);
981 xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
982 xfs_bmap_local_to_extents_empty(ip, whichfork);
983 flags |= XFS_ILOG_CORE;
985 xfs_iext_add(ifp, 0, 1);
986 ep = xfs_iext_get_ext(ifp, 0);
987 xfs_bmbt_set_allf(ep, 0, args.fsbno, 1, XFS_EXT_NORM);
988 trace_xfs_bmap_post_update(ip, 0,
989 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0,
991 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
992 ip->i_d.di_nblocks = 1;
993 xfs_trans_mod_dquot_byino(tp, ip,
994 XFS_TRANS_DQ_BCOUNT, 1L);
995 flags |= xfs_ilog_fext(whichfork);
1003 * Called from xfs_bmap_add_attrfork to handle btree format files.
1005 STATIC int /* error */
1006 xfs_bmap_add_attrfork_btree(
1007 xfs_trans_t *tp, /* transaction pointer */
1008 xfs_inode_t *ip, /* incore inode pointer */
1009 xfs_fsblock_t *firstblock, /* first block allocated */
1010 xfs_bmap_free_t *flist, /* blocks to free at commit */
1011 int *flags) /* inode logging flags */
1013 xfs_btree_cur_t *cur; /* btree cursor */
1014 int error; /* error return value */
1015 xfs_mount_t *mp; /* file system mount struct */
1016 int stat; /* newroot status */
1019 if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip))
1020 *flags |= XFS_ILOG_DBROOT;
1022 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
1023 cur->bc_private.b.flist = flist;
1024 cur->bc_private.b.firstblock = *firstblock;
1025 if ((error = xfs_bmbt_lookup_ge(cur, 0, 0, 0, &stat)))
1027 /* must be at least one entry */
1028 XFS_WANT_CORRUPTED_GOTO(stat == 1, error0);
1029 if ((error = xfs_btree_new_iroot(cur, flags, &stat)))
1032 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1035 *firstblock = cur->bc_private.b.firstblock;
1036 cur->bc_private.b.allocated = 0;
1037 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1041 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
1046 * Called from xfs_bmap_add_attrfork to handle extents format files.
1048 STATIC int /* error */
1049 xfs_bmap_add_attrfork_extents(
1050 xfs_trans_t *tp, /* transaction pointer */
1051 xfs_inode_t *ip, /* incore inode pointer */
1052 xfs_fsblock_t *firstblock, /* first block allocated */
1053 xfs_bmap_free_t *flist, /* blocks to free at commit */
1054 int *flags) /* inode logging flags */
1056 xfs_btree_cur_t *cur; /* bmap btree cursor */
1057 int error; /* error return value */
1059 if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip))
1062 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist, &cur, 0,
1063 flags, XFS_DATA_FORK);
1065 cur->bc_private.b.allocated = 0;
1066 xfs_btree_del_cursor(cur,
1067 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
1073 * Called from xfs_bmap_add_attrfork to handle local format files. Each
1074 * different data fork content type needs a different callout to do the
1075 * conversion. Some are basic and only require special block initialisation
1076 * callouts for the data formating, others (directories) are so specialised they
1077 * handle everything themselves.
1079 * XXX (dgc): investigate whether directory conversion can use the generic
1080 * formatting callout. It should be possible - it's just a very complex
1083 STATIC int /* error */
1084 xfs_bmap_add_attrfork_local(
1085 xfs_trans_t *tp, /* transaction pointer */
1086 xfs_inode_t *ip, /* incore inode pointer */
1087 xfs_fsblock_t *firstblock, /* first block allocated */
1088 xfs_bmap_free_t *flist, /* blocks to free at commit */
1089 int *flags) /* inode logging flags */
1091 xfs_da_args_t dargs; /* args for dir/attr code */
1093 if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip))
1096 if (S_ISDIR(ip->i_d.di_mode)) {
1097 memset(&dargs, 0, sizeof(dargs));
1098 dargs.geo = ip->i_mount->m_dir_geo;
1100 dargs.firstblock = firstblock;
1101 dargs.flist = flist;
1102 dargs.total = dargs.geo->fsbcount;
1103 dargs.whichfork = XFS_DATA_FORK;
1105 return xfs_dir2_sf_to_block(&dargs);
1108 if (S_ISLNK(ip->i_d.di_mode))
1109 return xfs_bmap_local_to_extents(tp, ip, firstblock, 1,
1110 flags, XFS_DATA_FORK,
1111 xfs_symlink_local_to_remote);
1113 /* should only be called for types that support local format data */
1115 return -EFSCORRUPTED;
1119 * Convert inode from non-attributed to attributed.
1120 * Must not be in a transaction, ip must not be locked.
1122 int /* error code */
1123 xfs_bmap_add_attrfork(
1124 xfs_inode_t *ip, /* incore inode pointer */
1125 int size, /* space new attribute needs */
1126 int rsvd) /* xact may use reserved blks */
1128 xfs_fsblock_t firstblock; /* 1st block/ag allocated */
1129 xfs_bmap_free_t flist; /* freed extent records */
1130 xfs_mount_t *mp; /* mount structure */
1131 xfs_trans_t *tp; /* transaction pointer */
1132 int blks; /* space reservation */
1133 int version = 1; /* superblock attr version */
1134 int committed; /* xaction was committed */
1135 int logflags; /* logging flags */
1136 int error; /* error return value */
1137 int cancel_flags = 0;
1139 ASSERT(XFS_IFORK_Q(ip) == 0);
1142 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
1143 tp = xfs_trans_alloc(mp, XFS_TRANS_ADDAFORK);
1144 blks = XFS_ADDAFORK_SPACE_RES(mp);
1146 tp->t_flags |= XFS_TRANS_RESERVE;
1147 error = xfs_trans_reserve(tp, &M_RES(mp)->tr_addafork, blks, 0);
1149 xfs_trans_cancel(tp, 0);
1152 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
1153 xfs_ilock(ip, XFS_ILOCK_EXCL);
1154 error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ?
1155 XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
1156 XFS_QMOPT_RES_REGBLKS);
1159 cancel_flags |= XFS_TRANS_ABORT;
1160 if (XFS_IFORK_Q(ip))
1162 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) {
1164 * For inodes coming from pre-6.2 filesystems.
1166 ASSERT(ip->i_d.di_aformat == 0);
1167 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
1169 ASSERT(ip->i_d.di_anextents == 0);
1171 xfs_trans_ijoin(tp, ip, 0);
1172 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1174 switch (ip->i_d.di_format) {
1175 case XFS_DINODE_FMT_DEV:
1176 ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
1178 case XFS_DINODE_FMT_UUID:
1179 ip->i_d.di_forkoff = roundup(sizeof(uuid_t), 8) >> 3;
1181 case XFS_DINODE_FMT_LOCAL:
1182 case XFS_DINODE_FMT_EXTENTS:
1183 case XFS_DINODE_FMT_BTREE:
1184 ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size);
1185 if (!ip->i_d.di_forkoff)
1186 ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3;
1187 else if (mp->m_flags & XFS_MOUNT_ATTR2)
1196 ASSERT(ip->i_afp == NULL);
1197 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP);
1198 ip->i_afp->if_flags = XFS_IFEXTENTS;
1200 xfs_bmap_init(&flist, &firstblock);
1201 switch (ip->i_d.di_format) {
1202 case XFS_DINODE_FMT_LOCAL:
1203 error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &flist,
1206 case XFS_DINODE_FMT_EXTENTS:
1207 error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock,
1210 case XFS_DINODE_FMT_BTREE:
1211 error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &flist,
1219 xfs_trans_log_inode(tp, ip, logflags);
1222 if (!xfs_sb_version_hasattr(&mp->m_sb) ||
1223 (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) {
1224 __int64_t sbfields = 0;
1226 spin_lock(&mp->m_sb_lock);
1227 if (!xfs_sb_version_hasattr(&mp->m_sb)) {
1228 xfs_sb_version_addattr(&mp->m_sb);
1229 sbfields |= XFS_SB_VERSIONNUM;
1231 if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) {
1232 xfs_sb_version_addattr2(&mp->m_sb);
1233 sbfields |= (XFS_SB_VERSIONNUM | XFS_SB_FEATURES2);
1236 spin_unlock(&mp->m_sb_lock);
1237 xfs_mod_sb(tp, sbfields);
1239 spin_unlock(&mp->m_sb_lock);
1242 error = xfs_bmap_finish(&tp, &flist, &committed);
1245 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1246 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1250 xfs_bmap_cancel(&flist);
1252 xfs_trans_cancel(tp, cancel_flags);
1253 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1258 * Internal and external extent tree search functions.
1262 * Read in the extents to if_extents.
1263 * All inode fields are set up by caller, we just traverse the btree
1264 * and copy the records in. If the file system cannot contain unwritten
1265 * extents, the records are checked for no "state" flags.
1268 xfs_bmap_read_extents(
1269 xfs_trans_t *tp, /* transaction pointer */
1270 xfs_inode_t *ip, /* incore inode */
1271 int whichfork) /* data or attr fork */
1273 struct xfs_btree_block *block; /* current btree block */
1274 xfs_fsblock_t bno; /* block # of "block" */
1275 xfs_buf_t *bp; /* buffer for "block" */
1276 int error; /* error return value */
1277 xfs_exntfmt_t exntf; /* XFS_EXTFMT_NOSTATE, if checking */
1278 xfs_extnum_t i, j; /* index into the extents list */
1279 xfs_ifork_t *ifp; /* fork structure */
1280 int level; /* btree level, for checking */
1281 xfs_mount_t *mp; /* file system mount structure */
1282 __be64 *pp; /* pointer to block address */
1284 xfs_extnum_t room; /* number of entries there's room for */
1288 ifp = XFS_IFORK_PTR(ip, whichfork);
1289 exntf = (whichfork != XFS_DATA_FORK) ? XFS_EXTFMT_NOSTATE :
1290 XFS_EXTFMT_INODE(ip);
1291 block = ifp->if_broot;
1293 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
1295 level = be16_to_cpu(block->bb_level);
1297 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
1298 bno = be64_to_cpu(*pp);
1299 ASSERT(bno != NULLFSBLOCK);
1300 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
1301 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
1303 * Go down the tree until leaf level is reached, following the first
1304 * pointer (leftmost) at each level.
1306 while (level-- > 0) {
1307 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
1308 XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
1311 block = XFS_BUF_TO_BLOCK(bp);
1312 XFS_WANT_CORRUPTED_GOTO(
1313 xfs_bmap_sanity_check(mp, bp, level),
1317 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
1318 bno = be64_to_cpu(*pp);
1319 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0);
1320 xfs_trans_brelse(tp, bp);
1323 * Here with bp and block set to the leftmost leaf node in the tree.
1325 room = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
1328 * Loop over all leaf nodes. Copy information to the extent records.
1331 xfs_bmbt_rec_t *frp;
1332 xfs_fsblock_t nextbno;
1333 xfs_extnum_t num_recs;
1336 num_recs = xfs_btree_get_numrecs(block);
1337 if (unlikely(i + num_recs > room)) {
1338 ASSERT(i + num_recs <= room);
1339 xfs_warn(ip->i_mount,
1340 "corrupt dinode %Lu, (btree extents).",
1341 (unsigned long long) ip->i_ino);
1342 XFS_CORRUPTION_ERROR("xfs_bmap_read_extents(1)",
1343 XFS_ERRLEVEL_LOW, ip->i_mount, block);
1346 XFS_WANT_CORRUPTED_GOTO(
1347 xfs_bmap_sanity_check(mp, bp, 0),
1350 * Read-ahead the next leaf block, if any.
1352 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
1353 if (nextbno != NULLFSBLOCK)
1354 xfs_btree_reada_bufl(mp, nextbno, 1,
1357 * Copy records into the extent records.
1359 frp = XFS_BMBT_REC_ADDR(mp, block, 1);
1361 for (j = 0; j < num_recs; j++, i++, frp++) {
1362 xfs_bmbt_rec_host_t *trp = xfs_iext_get_ext(ifp, i);
1363 trp->l0 = be64_to_cpu(frp->l0);
1364 trp->l1 = be64_to_cpu(frp->l1);
1366 if (exntf == XFS_EXTFMT_NOSTATE) {
1368 * Check all attribute bmap btree records and
1369 * any "older" data bmap btree records for a
1370 * set bit in the "extent flag" position.
1372 if (unlikely(xfs_check_nostate_extents(ifp,
1373 start, num_recs))) {
1374 XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
1380 xfs_trans_brelse(tp, bp);
1383 * If we've reached the end, stop.
1385 if (bno == NULLFSBLOCK)
1387 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
1388 XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
1391 block = XFS_BUF_TO_BLOCK(bp);
1393 ASSERT(i == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
1394 ASSERT(i == XFS_IFORK_NEXTENTS(ip, whichfork));
1395 XFS_BMAP_TRACE_EXLIST(ip, i, whichfork);
1398 xfs_trans_brelse(tp, bp);
1399 return -EFSCORRUPTED;
1404 * Search the extent records for the entry containing block bno.
1405 * If bno lies in a hole, point to the next entry. If bno lies
1406 * past eof, *eofp will be set, and *prevp will contain the last
1407 * entry (null if none). Else, *lastxp will be set to the index
1408 * of the found entry; *gotp will contain the entry.
1410 STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
1411 xfs_bmap_search_multi_extents(
1412 xfs_ifork_t *ifp, /* inode fork pointer */
1413 xfs_fileoff_t bno, /* block number searched for */
1414 int *eofp, /* out: end of file found */
1415 xfs_extnum_t *lastxp, /* out: last extent index */
1416 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
1417 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
1419 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
1420 xfs_extnum_t lastx; /* last extent index */
1423 * Initialize the extent entry structure to catch access to
1424 * uninitialized br_startblock field.
1426 gotp->br_startoff = 0xffa5a5a5a5a5a5a5LL;
1427 gotp->br_blockcount = 0xa55a5a5a5a5a5a5aLL;
1428 gotp->br_state = XFS_EXT_INVALID;
1429 gotp->br_startblock = 0xffffa5a5a5a5a5a5LL;
1430 prevp->br_startoff = NULLFILEOFF;
1432 ep = xfs_iext_bno_to_ext(ifp, bno, &lastx);
1434 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx - 1), prevp);
1436 if (lastx < (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) {
1437 xfs_bmbt_get_all(ep, gotp);
1451 * Search the extents list for the inode, for the extent containing bno.
1452 * If bno lies in a hole, point to the next entry. If bno lies past eof,
1453 * *eofp will be set, and *prevp will contain the last entry (null if none).
1454 * Else, *lastxp will be set to the index of the found
1455 * entry; *gotp will contain the entry.
1457 STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
1458 xfs_bmap_search_extents(
1459 xfs_inode_t *ip, /* incore inode pointer */
1460 xfs_fileoff_t bno, /* block number searched for */
1461 int fork, /* data or attr fork */
1462 int *eofp, /* out: end of file found */
1463 xfs_extnum_t *lastxp, /* out: last extent index */
1464 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
1465 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
1467 xfs_ifork_t *ifp; /* inode fork pointer */
1468 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
1470 XFS_STATS_INC(xs_look_exlist);
1471 ifp = XFS_IFORK_PTR(ip, fork);
1473 ep = xfs_bmap_search_multi_extents(ifp, bno, eofp, lastxp, gotp, prevp);
1475 if (unlikely(!(gotp->br_startblock) && (*lastxp != NULLEXTNUM) &&
1476 !(XFS_IS_REALTIME_INODE(ip) && fork == XFS_DATA_FORK))) {
1477 xfs_alert_tag(ip->i_mount, XFS_PTAG_FSBLOCK_ZERO,
1478 "Access to block zero in inode %llu "
1479 "start_block: %llx start_off: %llx "
1480 "blkcnt: %llx extent-state: %x lastx: %x",
1481 (unsigned long long)ip->i_ino,
1482 (unsigned long long)gotp->br_startblock,
1483 (unsigned long long)gotp->br_startoff,
1484 (unsigned long long)gotp->br_blockcount,
1485 gotp->br_state, *lastxp);
1486 *lastxp = NULLEXTNUM;
1494 * Returns the file-relative block number of the first unused block(s)
1495 * in the file with at least "len" logically contiguous blocks free.
1496 * This is the lowest-address hole if the file has holes, else the first block
1497 * past the end of file.
1498 * Return 0 if the file is currently local (in-inode).
1501 xfs_bmap_first_unused(
1502 xfs_trans_t *tp, /* transaction pointer */
1503 xfs_inode_t *ip, /* incore inode */
1504 xfs_extlen_t len, /* size of hole to find */
1505 xfs_fileoff_t *first_unused, /* unused block */
1506 int whichfork) /* data or attr fork */
1508 int error; /* error return value */
1509 int idx; /* extent record index */
1510 xfs_ifork_t *ifp; /* inode fork pointer */
1511 xfs_fileoff_t lastaddr; /* last block number seen */
1512 xfs_fileoff_t lowest; /* lowest useful block */
1513 xfs_fileoff_t max; /* starting useful block */
1514 xfs_fileoff_t off; /* offset for this block */
1515 xfs_extnum_t nextents; /* number of extent entries */
1517 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE ||
1518 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ||
1519 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
1520 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
1524 ifp = XFS_IFORK_PTR(ip, whichfork);
1525 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
1526 (error = xfs_iread_extents(tp, ip, whichfork)))
1528 lowest = *first_unused;
1529 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
1530 for (idx = 0, lastaddr = 0, max = lowest; idx < nextents; idx++) {
1531 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, idx);
1532 off = xfs_bmbt_get_startoff(ep);
1534 * See if the hole before this extent will work.
1536 if (off >= lowest + len && off - max >= len) {
1537 *first_unused = max;
1540 lastaddr = off + xfs_bmbt_get_blockcount(ep);
1541 max = XFS_FILEOFF_MAX(lastaddr, lowest);
1543 *first_unused = max;
1548 * Returns the file-relative block number of the last block - 1 before
1549 * last_block (input value) in the file.
1550 * This is not based on i_size, it is based on the extent records.
1551 * Returns 0 for local files, as they do not have extent records.
1554 xfs_bmap_last_before(
1555 xfs_trans_t *tp, /* transaction pointer */
1556 xfs_inode_t *ip, /* incore inode */
1557 xfs_fileoff_t *last_block, /* last block */
1558 int whichfork) /* data or attr fork */
1560 xfs_fileoff_t bno; /* input file offset */
1561 int eof; /* hit end of file */
1562 xfs_bmbt_rec_host_t *ep; /* pointer to last extent */
1563 int error; /* error return value */
1564 xfs_bmbt_irec_t got; /* current extent value */
1565 xfs_ifork_t *ifp; /* inode fork pointer */
1566 xfs_extnum_t lastx; /* last extent used */
1567 xfs_bmbt_irec_t prev; /* previous extent value */
1569 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
1570 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
1571 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
1573 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
1577 ifp = XFS_IFORK_PTR(ip, whichfork);
1578 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
1579 (error = xfs_iread_extents(tp, ip, whichfork)))
1581 bno = *last_block - 1;
1582 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
1584 if (eof || xfs_bmbt_get_startoff(ep) > bno) {
1585 if (prev.br_startoff == NULLFILEOFF)
1588 *last_block = prev.br_startoff + prev.br_blockcount;
1591 * Otherwise *last_block is already the right answer.
1597 xfs_bmap_last_extent(
1598 struct xfs_trans *tp,
1599 struct xfs_inode *ip,
1601 struct xfs_bmbt_irec *rec,
1604 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1608 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1609 error = xfs_iread_extents(tp, ip, whichfork);
1614 nextents = ifp->if_bytes / sizeof(xfs_bmbt_rec_t);
1615 if (nextents == 0) {
1620 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, nextents - 1), rec);
1626 * Check the last inode extent to determine whether this allocation will result
1627 * in blocks being allocated at the end of the file. When we allocate new data
1628 * blocks at the end of the file which do not start at the previous data block,
1629 * we will try to align the new blocks at stripe unit boundaries.
1631 * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be
1632 * at, or past the EOF.
1636 struct xfs_bmalloca *bma,
1639 struct xfs_bmbt_irec rec;
1644 error = xfs_bmap_last_extent(NULL, bma->ip, whichfork, &rec,
1655 * Check if we are allocation or past the last extent, or at least into
1656 * the last delayed allocated extent.
1658 bma->aeof = bma->offset >= rec.br_startoff + rec.br_blockcount ||
1659 (bma->offset >= rec.br_startoff &&
1660 isnullstartblock(rec.br_startblock));
1665 * Returns the file-relative block number of the first block past eof in
1666 * the file. This is not based on i_size, it is based on the extent records.
1667 * Returns 0 for local files, as they do not have extent records.
1670 xfs_bmap_last_offset(
1671 struct xfs_inode *ip,
1672 xfs_fileoff_t *last_block,
1675 struct xfs_bmbt_irec rec;
1681 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL)
1684 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
1685 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
1688 error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, &is_empty);
1689 if (error || is_empty)
1692 *last_block = rec.br_startoff + rec.br_blockcount;
1697 * Returns whether the selected fork of the inode has exactly one
1698 * block or not. For the data fork we check this matches di_size,
1699 * implying the file's range is 0..bsize-1.
1701 int /* 1=>1 block, 0=>otherwise */
1703 xfs_inode_t *ip, /* incore inode */
1704 int whichfork) /* data or attr fork */
1706 xfs_bmbt_rec_host_t *ep; /* ptr to fork's extent */
1707 xfs_ifork_t *ifp; /* inode fork pointer */
1708 int rval; /* return value */
1709 xfs_bmbt_irec_t s; /* internal version of extent */
1712 if (whichfork == XFS_DATA_FORK)
1713 return XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize;
1715 if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1)
1717 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
1719 ifp = XFS_IFORK_PTR(ip, whichfork);
1720 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
1721 ep = xfs_iext_get_ext(ifp, 0);
1722 xfs_bmbt_get_all(ep, &s);
1723 rval = s.br_startoff == 0 && s.br_blockcount == 1;
1724 if (rval && whichfork == XFS_DATA_FORK)
1725 ASSERT(XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize);
1730 * Extent tree manipulation functions used during allocation.
1734 * Convert a delayed allocation to a real allocation.
1736 STATIC int /* error */
1737 xfs_bmap_add_extent_delay_real(
1738 struct xfs_bmalloca *bma)
1740 struct xfs_bmbt_irec *new = &bma->got;
1741 int diff; /* temp value */
1742 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
1743 int error; /* error return value */
1744 int i; /* temp state */
1745 xfs_ifork_t *ifp; /* inode fork pointer */
1746 xfs_fileoff_t new_endoff; /* end offset of new entry */
1747 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
1748 /* left is 0, right is 1, prev is 2 */
1749 int rval=0; /* return value (logging flags) */
1750 int state = 0;/* state bits, accessed thru macros */
1751 xfs_filblks_t da_new; /* new count del alloc blocks used */
1752 xfs_filblks_t da_old; /* old count del alloc blocks used */
1753 xfs_filblks_t temp=0; /* value for da_new calculations */
1754 xfs_filblks_t temp2=0;/* value for da_new calculations */
1755 int tmp_rval; /* partial logging flags */
1757 ifp = XFS_IFORK_PTR(bma->ip, XFS_DATA_FORK);
1759 ASSERT(bma->idx >= 0);
1760 ASSERT(bma->idx <= ifp->if_bytes / sizeof(struct xfs_bmbt_rec));
1761 ASSERT(!isnullstartblock(new->br_startblock));
1763 (bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
1765 XFS_STATS_INC(xs_add_exlist);
1772 * Set up a bunch of variables to make the tests simpler.
1774 ep = xfs_iext_get_ext(ifp, bma->idx);
1775 xfs_bmbt_get_all(ep, &PREV);
1776 new_endoff = new->br_startoff + new->br_blockcount;
1777 ASSERT(PREV.br_startoff <= new->br_startoff);
1778 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
1780 da_old = startblockval(PREV.br_startblock);
1784 * Set flags determining what part of the previous delayed allocation
1785 * extent is being replaced by a real allocation.
1787 if (PREV.br_startoff == new->br_startoff)
1788 state |= BMAP_LEFT_FILLING;
1789 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
1790 state |= BMAP_RIGHT_FILLING;
1793 * Check and set flags if this segment has a left neighbor.
1794 * Don't set contiguous if the combined extent would be too large.
1797 state |= BMAP_LEFT_VALID;
1798 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1), &LEFT);
1800 if (isnullstartblock(LEFT.br_startblock))
1801 state |= BMAP_LEFT_DELAY;
1804 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1805 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1806 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
1807 LEFT.br_state == new->br_state &&
1808 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1809 state |= BMAP_LEFT_CONTIG;
1812 * Check and set flags if this segment has a right neighbor.
1813 * Don't set contiguous if the combined extent would be too large.
1814 * Also check for all-three-contiguous being too large.
1816 if (bma->idx < bma->ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1) {
1817 state |= BMAP_RIGHT_VALID;
1818 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx + 1), &RIGHT);
1820 if (isnullstartblock(RIGHT.br_startblock))
1821 state |= BMAP_RIGHT_DELAY;
1824 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1825 new_endoff == RIGHT.br_startoff &&
1826 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
1827 new->br_state == RIGHT.br_state &&
1828 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
1829 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1830 BMAP_RIGHT_FILLING)) !=
1831 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1832 BMAP_RIGHT_FILLING) ||
1833 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
1835 state |= BMAP_RIGHT_CONTIG;
1839 * Switch out based on the FILLING and CONTIG state bits.
1841 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1842 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
1843 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1844 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1846 * Filling in all of a previously delayed allocation extent.
1847 * The left and right neighbors are both contiguous with new.
1850 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
1851 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
1852 LEFT.br_blockcount + PREV.br_blockcount +
1853 RIGHT.br_blockcount);
1854 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
1856 xfs_iext_remove(bma->ip, bma->idx + 1, 2, state);
1857 bma->ip->i_d.di_nextents--;
1858 if (bma->cur == NULL)
1859 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1861 rval = XFS_ILOG_CORE;
1862 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
1863 RIGHT.br_startblock,
1864 RIGHT.br_blockcount, &i);
1867 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1868 error = xfs_btree_delete(bma->cur, &i);
1871 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1872 error = xfs_btree_decrement(bma->cur, 0, &i);
1875 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1876 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
1878 LEFT.br_blockcount +
1879 PREV.br_blockcount +
1880 RIGHT.br_blockcount, LEFT.br_state);
1886 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1888 * Filling in all of a previously delayed allocation extent.
1889 * The left neighbor is contiguous, the right is not.
1893 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
1894 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
1895 LEFT.br_blockcount + PREV.br_blockcount);
1896 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
1898 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
1899 if (bma->cur == NULL)
1900 rval = XFS_ILOG_DEXT;
1903 error = xfs_bmbt_lookup_eq(bma->cur, LEFT.br_startoff,
1904 LEFT.br_startblock, LEFT.br_blockcount,
1908 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1909 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
1911 LEFT.br_blockcount +
1912 PREV.br_blockcount, LEFT.br_state);
1918 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1920 * Filling in all of a previously delayed allocation extent.
1921 * The right neighbor is contiguous, the left is not.
1923 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
1924 xfs_bmbt_set_startblock(ep, new->br_startblock);
1925 xfs_bmbt_set_blockcount(ep,
1926 PREV.br_blockcount + RIGHT.br_blockcount);
1927 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
1929 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
1930 if (bma->cur == NULL)
1931 rval = XFS_ILOG_DEXT;
1934 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
1935 RIGHT.br_startblock,
1936 RIGHT.br_blockcount, &i);
1939 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1940 error = xfs_bmbt_update(bma->cur, PREV.br_startoff,
1942 PREV.br_blockcount +
1943 RIGHT.br_blockcount, PREV.br_state);
1949 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
1951 * Filling in all of a previously delayed allocation extent.
1952 * Neither the left nor right neighbors are contiguous with
1955 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
1956 xfs_bmbt_set_startblock(ep, new->br_startblock);
1957 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
1959 bma->ip->i_d.di_nextents++;
1960 if (bma->cur == NULL)
1961 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1963 rval = XFS_ILOG_CORE;
1964 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
1965 new->br_startblock, new->br_blockcount,
1969 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
1970 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
1971 error = xfs_btree_insert(bma->cur, &i);
1974 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1978 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
1980 * Filling in the first part of a previous delayed allocation.
1981 * The left neighbor is contiguous.
1983 trace_xfs_bmap_pre_update(bma->ip, bma->idx - 1, state, _THIS_IP_);
1984 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx - 1),
1985 LEFT.br_blockcount + new->br_blockcount);
1986 xfs_bmbt_set_startoff(ep,
1987 PREV.br_startoff + new->br_blockcount);
1988 trace_xfs_bmap_post_update(bma->ip, bma->idx - 1, state, _THIS_IP_);
1990 temp = PREV.br_blockcount - new->br_blockcount;
1991 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
1992 xfs_bmbt_set_blockcount(ep, temp);
1993 if (bma->cur == NULL)
1994 rval = XFS_ILOG_DEXT;
1997 error = xfs_bmbt_lookup_eq(bma->cur, LEFT.br_startoff,
1998 LEFT.br_startblock, LEFT.br_blockcount,
2002 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
2003 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
2005 LEFT.br_blockcount +
2011 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
2012 startblockval(PREV.br_startblock));
2013 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
2014 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
2019 case BMAP_LEFT_FILLING:
2021 * Filling in the first part of a previous delayed allocation.
2022 * The left neighbor is not contiguous.
2024 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
2025 xfs_bmbt_set_startoff(ep, new_endoff);
2026 temp = PREV.br_blockcount - new->br_blockcount;
2027 xfs_bmbt_set_blockcount(ep, temp);
2028 xfs_iext_insert(bma->ip, bma->idx, 1, new, state);
2029 bma->ip->i_d.di_nextents++;
2030 if (bma->cur == NULL)
2031 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2033 rval = XFS_ILOG_CORE;
2034 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
2035 new->br_startblock, new->br_blockcount,
2039 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
2040 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
2041 error = xfs_btree_insert(bma->cur, &i);
2044 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
2047 if (xfs_bmap_needs_btree(bma->ip, XFS_DATA_FORK)) {
2048 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
2049 bma->firstblock, bma->flist,
2050 &bma->cur, 1, &tmp_rval, XFS_DATA_FORK);
2055 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
2056 startblockval(PREV.br_startblock) -
2057 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
2058 ep = xfs_iext_get_ext(ifp, bma->idx + 1);
2059 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
2060 trace_xfs_bmap_post_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
2063 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
2065 * Filling in the last part of a previous delayed allocation.
2066 * The right neighbor is contiguous with the new allocation.
2068 temp = PREV.br_blockcount - new->br_blockcount;
2069 trace_xfs_bmap_pre_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
2070 xfs_bmbt_set_blockcount(ep, temp);
2071 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, bma->idx + 1),
2072 new->br_startoff, new->br_startblock,
2073 new->br_blockcount + RIGHT.br_blockcount,
2075 trace_xfs_bmap_post_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
2076 if (bma->cur == NULL)
2077 rval = XFS_ILOG_DEXT;
2080 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
2081 RIGHT.br_startblock,
2082 RIGHT.br_blockcount, &i);
2085 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
2086 error = xfs_bmbt_update(bma->cur, new->br_startoff,
2088 new->br_blockcount +
2089 RIGHT.br_blockcount,
2095 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
2096 startblockval(PREV.br_startblock));
2097 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
2098 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
2099 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
2104 case BMAP_RIGHT_FILLING:
2106 * Filling in the last part of a previous delayed allocation.
2107 * The right neighbor is not contiguous.
2109 temp = PREV.br_blockcount - new->br_blockcount;
2110 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
2111 xfs_bmbt_set_blockcount(ep, temp);
2112 xfs_iext_insert(bma->ip, bma->idx + 1, 1, new, state);
2113 bma->ip->i_d.di_nextents++;
2114 if (bma->cur == NULL)
2115 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2117 rval = XFS_ILOG_CORE;
2118 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
2119 new->br_startblock, new->br_blockcount,
2123 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
2124 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
2125 error = xfs_btree_insert(bma->cur, &i);
2128 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
2131 if (xfs_bmap_needs_btree(bma->ip, XFS_DATA_FORK)) {
2132 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
2133 bma->firstblock, bma->flist, &bma->cur, 1,
2134 &tmp_rval, XFS_DATA_FORK);
2139 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
2140 startblockval(PREV.br_startblock) -
2141 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
2142 ep = xfs_iext_get_ext(ifp, bma->idx);
2143 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
2144 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
2151 * Filling in the middle part of a previous delayed allocation.
2152 * Contiguity is impossible here.
2153 * This case is avoided almost all the time.
2155 * We start with a delayed allocation:
2157 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
2160 * and we are allocating:
2161 * +rrrrrrrrrrrrrrrrr+
2164 * and we set it up for insertion as:
2165 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
2167 * PREV @ idx LEFT RIGHT
2168 * inserted at idx + 1
2170 temp = new->br_startoff - PREV.br_startoff;
2171 temp2 = PREV.br_startoff + PREV.br_blockcount - new_endoff;
2172 trace_xfs_bmap_pre_update(bma->ip, bma->idx, 0, _THIS_IP_);
2173 xfs_bmbt_set_blockcount(ep, temp); /* truncate PREV */
2175 RIGHT.br_state = PREV.br_state;
2176 RIGHT.br_startblock = nullstartblock(
2177 (int)xfs_bmap_worst_indlen(bma->ip, temp2));
2178 RIGHT.br_startoff = new_endoff;
2179 RIGHT.br_blockcount = temp2;
2180 /* insert LEFT (r[0]) and RIGHT (r[1]) at the same time */
2181 xfs_iext_insert(bma->ip, bma->idx + 1, 2, &LEFT, state);
2182 bma->ip->i_d.di_nextents++;
2183 if (bma->cur == NULL)
2184 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2186 rval = XFS_ILOG_CORE;
2187 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
2188 new->br_startblock, new->br_blockcount,
2192 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
2193 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
2194 error = xfs_btree_insert(bma->cur, &i);
2197 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
2200 if (xfs_bmap_needs_btree(bma->ip, XFS_DATA_FORK)) {
2201 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
2202 bma->firstblock, bma->flist, &bma->cur,
2203 1, &tmp_rval, XFS_DATA_FORK);
2208 temp = xfs_bmap_worst_indlen(bma->ip, temp);
2209 temp2 = xfs_bmap_worst_indlen(bma->ip, temp2);
2210 diff = (int)(temp + temp2 - startblockval(PREV.br_startblock) -
2211 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
2213 error = xfs_icsb_modify_counters(bma->ip->i_mount,
2215 -((int64_t)diff), 0);
2221 ep = xfs_iext_get_ext(ifp, bma->idx);
2222 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
2223 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
2224 trace_xfs_bmap_pre_update(bma->ip, bma->idx + 2, state, _THIS_IP_);
2225 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, bma->idx + 2),
2226 nullstartblock((int)temp2));
2227 trace_xfs_bmap_post_update(bma->ip, bma->idx + 2, state, _THIS_IP_);
2230 da_new = temp + temp2;
2233 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2234 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2235 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
2236 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2237 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2238 case BMAP_LEFT_CONTIG:
2239 case BMAP_RIGHT_CONTIG:
2241 * These cases are all impossible.
2246 /* convert to a btree if necessary */
2247 if (xfs_bmap_needs_btree(bma->ip, XFS_DATA_FORK)) {
2248 int tmp_logflags; /* partial log flag return val */
2250 ASSERT(bma->cur == NULL);
2251 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
2252 bma->firstblock, bma->flist, &bma->cur,
2253 da_old > 0, &tmp_logflags, XFS_DATA_FORK);
2254 bma->logflags |= tmp_logflags;
2259 /* adjust for changes in reserved delayed indirect blocks */
2260 if (da_old || da_new) {
2263 temp += bma->cur->bc_private.b.allocated;
2264 ASSERT(temp <= da_old);
2266 xfs_icsb_modify_counters(bma->ip->i_mount,
2268 (int64_t)(da_old - temp), 0);
2271 /* clear out the allocated field, done with it now in any case. */
2273 bma->cur->bc_private.b.allocated = 0;
2275 xfs_bmap_check_leaf_extents(bma->cur, bma->ip, XFS_DATA_FORK);
2277 bma->logflags |= rval;
2285 * Convert an unwritten allocation to a real allocation or vice versa.
2287 STATIC int /* error */
2288 xfs_bmap_add_extent_unwritten_real(
2289 struct xfs_trans *tp,
2290 xfs_inode_t *ip, /* incore inode pointer */
2291 xfs_extnum_t *idx, /* extent number to update/insert */
2292 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
2293 xfs_bmbt_irec_t *new, /* new data to add to file extents */
2294 xfs_fsblock_t *first, /* pointer to firstblock variable */
2295 xfs_bmap_free_t *flist, /* list of extents to be freed */
2296 int *logflagsp) /* inode logging flags */
2298 xfs_btree_cur_t *cur; /* btree cursor */
2299 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
2300 int error; /* error return value */
2301 int i; /* temp state */
2302 xfs_ifork_t *ifp; /* inode fork pointer */
2303 xfs_fileoff_t new_endoff; /* end offset of new entry */
2304 xfs_exntst_t newext; /* new extent state */
2305 xfs_exntst_t oldext; /* old extent state */
2306 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
2307 /* left is 0, right is 1, prev is 2 */
2308 int rval=0; /* return value (logging flags) */
2309 int state = 0;/* state bits, accessed thru macros */
2314 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
2317 ASSERT(*idx <= ifp->if_bytes / sizeof(struct xfs_bmbt_rec));
2318 ASSERT(!isnullstartblock(new->br_startblock));
2320 XFS_STATS_INC(xs_add_exlist);
2327 * Set up a bunch of variables to make the tests simpler.
2330 ep = xfs_iext_get_ext(ifp, *idx);
2331 xfs_bmbt_get_all(ep, &PREV);
2332 newext = new->br_state;
2333 oldext = (newext == XFS_EXT_UNWRITTEN) ?
2334 XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
2335 ASSERT(PREV.br_state == oldext);
2336 new_endoff = new->br_startoff + new->br_blockcount;
2337 ASSERT(PREV.br_startoff <= new->br_startoff);
2338 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
2341 * Set flags determining what part of the previous oldext allocation
2342 * extent is being replaced by a newext allocation.
2344 if (PREV.br_startoff == new->br_startoff)
2345 state |= BMAP_LEFT_FILLING;
2346 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
2347 state |= BMAP_RIGHT_FILLING;
2350 * Check and set flags if this segment has a left neighbor.
2351 * Don't set contiguous if the combined extent would be too large.
2354 state |= BMAP_LEFT_VALID;
2355 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &LEFT);
2357 if (isnullstartblock(LEFT.br_startblock))
2358 state |= BMAP_LEFT_DELAY;
2361 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2362 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
2363 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
2364 LEFT.br_state == newext &&
2365 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2366 state |= BMAP_LEFT_CONTIG;
2369 * Check and set flags if this segment has a right neighbor.
2370 * Don't set contiguous if the combined extent would be too large.
2371 * Also check for all-three-contiguous being too large.
2373 if (*idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1) {
2374 state |= BMAP_RIGHT_VALID;
2375 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx + 1), &RIGHT);
2376 if (isnullstartblock(RIGHT.br_startblock))
2377 state |= BMAP_RIGHT_DELAY;
2380 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2381 new_endoff == RIGHT.br_startoff &&
2382 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
2383 newext == RIGHT.br_state &&
2384 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
2385 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2386 BMAP_RIGHT_FILLING)) !=
2387 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2388 BMAP_RIGHT_FILLING) ||
2389 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
2391 state |= BMAP_RIGHT_CONTIG;
2394 * Switch out based on the FILLING and CONTIG state bits.
2396 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2397 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
2398 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2399 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
2401 * Setting all of a previous oldext extent to newext.
2402 * The left and right neighbors are both contiguous with new.
2406 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2407 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
2408 LEFT.br_blockcount + PREV.br_blockcount +
2409 RIGHT.br_blockcount);
2410 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2412 xfs_iext_remove(ip, *idx + 1, 2, state);
2413 ip->i_d.di_nextents -= 2;
2415 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2417 rval = XFS_ILOG_CORE;
2418 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
2419 RIGHT.br_startblock,
2420 RIGHT.br_blockcount, &i)))
2422 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
2423 if ((error = xfs_btree_delete(cur, &i)))
2425 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
2426 if ((error = xfs_btree_decrement(cur, 0, &i)))
2428 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
2429 if ((error = xfs_btree_delete(cur, &i)))
2431 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
2432 if ((error = xfs_btree_decrement(cur, 0, &i)))
2434 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
2435 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
2437 LEFT.br_blockcount + PREV.br_blockcount +
2438 RIGHT.br_blockcount, LEFT.br_state)))
2443 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2445 * Setting all of a previous oldext extent to newext.
2446 * The left neighbor is contiguous, the right is not.
2450 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2451 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
2452 LEFT.br_blockcount + PREV.br_blockcount);
2453 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2455 xfs_iext_remove(ip, *idx + 1, 1, state);
2456 ip->i_d.di_nextents--;
2458 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2460 rval = XFS_ILOG_CORE;
2461 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2462 PREV.br_startblock, PREV.br_blockcount,
2465 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
2466 if ((error = xfs_btree_delete(cur, &i)))
2468 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
2469 if ((error = xfs_btree_decrement(cur, 0, &i)))
2471 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
2472 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
2474 LEFT.br_blockcount + PREV.br_blockcount,
2480 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
2482 * Setting all of a previous oldext extent to newext.
2483 * The right neighbor is contiguous, the left is not.
2485 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2486 xfs_bmbt_set_blockcount(ep,
2487 PREV.br_blockcount + RIGHT.br_blockcount);
2488 xfs_bmbt_set_state(ep, newext);
2489 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2490 xfs_iext_remove(ip, *idx + 1, 1, state);
2491 ip->i_d.di_nextents--;
2493 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2495 rval = XFS_ILOG_CORE;
2496 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
2497 RIGHT.br_startblock,
2498 RIGHT.br_blockcount, &i)))
2500 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
2501 if ((error = xfs_btree_delete(cur, &i)))
2503 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
2504 if ((error = xfs_btree_decrement(cur, 0, &i)))
2506 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
2507 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2509 new->br_blockcount + RIGHT.br_blockcount,
2515 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
2517 * Setting all of a previous oldext extent to newext.
2518 * Neither the left nor right neighbors are contiguous with
2521 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2522 xfs_bmbt_set_state(ep, newext);
2523 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2526 rval = XFS_ILOG_DEXT;
2529 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
2530 new->br_startblock, new->br_blockcount,
2533 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
2534 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2535 new->br_startblock, new->br_blockcount,
2541 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
2543 * Setting the first part of a previous oldext extent to newext.
2544 * The left neighbor is contiguous.
2546 trace_xfs_bmap_pre_update(ip, *idx - 1, state, _THIS_IP_);
2547 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx - 1),
2548 LEFT.br_blockcount + new->br_blockcount);
2549 xfs_bmbt_set_startoff(ep,
2550 PREV.br_startoff + new->br_blockcount);
2551 trace_xfs_bmap_post_update(ip, *idx - 1, state, _THIS_IP_);
2553 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2554 xfs_bmbt_set_startblock(ep,
2555 new->br_startblock + new->br_blockcount);
2556 xfs_bmbt_set_blockcount(ep,
2557 PREV.br_blockcount - new->br_blockcount);
2558 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2563 rval = XFS_ILOG_DEXT;
2566 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2567 PREV.br_startblock, PREV.br_blockcount,
2570 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
2571 if ((error = xfs_bmbt_update(cur,
2572 PREV.br_startoff + new->br_blockcount,
2573 PREV.br_startblock + new->br_blockcount,
2574 PREV.br_blockcount - new->br_blockcount,
2577 if ((error = xfs_btree_decrement(cur, 0, &i)))
2579 error = xfs_bmbt_update(cur, LEFT.br_startoff,
2581 LEFT.br_blockcount + new->br_blockcount,
2588 case BMAP_LEFT_FILLING:
2590 * Setting the first part of a previous oldext extent to newext.
2591 * The left neighbor is not contiguous.
2593 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2594 ASSERT(ep && xfs_bmbt_get_state(ep) == oldext);
2595 xfs_bmbt_set_startoff(ep, new_endoff);
2596 xfs_bmbt_set_blockcount(ep,
2597 PREV.br_blockcount - new->br_blockcount);
2598 xfs_bmbt_set_startblock(ep,
2599 new->br_startblock + new->br_blockcount);
2600 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2602 xfs_iext_insert(ip, *idx, 1, new, state);
2603 ip->i_d.di_nextents++;
2605 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2607 rval = XFS_ILOG_CORE;
2608 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2609 PREV.br_startblock, PREV.br_blockcount,
2612 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
2613 if ((error = xfs_bmbt_update(cur,
2614 PREV.br_startoff + new->br_blockcount,
2615 PREV.br_startblock + new->br_blockcount,
2616 PREV.br_blockcount - new->br_blockcount,
2619 cur->bc_rec.b = *new;
2620 if ((error = xfs_btree_insert(cur, &i)))
2622 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
2626 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
2628 * Setting the last part of a previous oldext extent to newext.
2629 * The right neighbor is contiguous with the new allocation.
2631 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2632 xfs_bmbt_set_blockcount(ep,
2633 PREV.br_blockcount - new->br_blockcount);
2634 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2638 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2639 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx),
2640 new->br_startoff, new->br_startblock,
2641 new->br_blockcount + RIGHT.br_blockcount, newext);
2642 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2645 rval = XFS_ILOG_DEXT;
2648 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2650 PREV.br_blockcount, &i)))
2652 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
2653 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
2655 PREV.br_blockcount - new->br_blockcount,
2658 if ((error = xfs_btree_increment(cur, 0, &i)))
2660 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2662 new->br_blockcount + RIGHT.br_blockcount,
2668 case BMAP_RIGHT_FILLING:
2670 * Setting the last part of a previous oldext extent to newext.
2671 * The right neighbor is not contiguous.
2673 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2674 xfs_bmbt_set_blockcount(ep,
2675 PREV.br_blockcount - new->br_blockcount);
2676 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2679 xfs_iext_insert(ip, *idx, 1, new, state);
2681 ip->i_d.di_nextents++;
2683 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2685 rval = XFS_ILOG_CORE;
2686 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2687 PREV.br_startblock, PREV.br_blockcount,
2690 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
2691 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
2693 PREV.br_blockcount - new->br_blockcount,
2696 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
2697 new->br_startblock, new->br_blockcount,
2700 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
2701 cur->bc_rec.b.br_state = XFS_EXT_NORM;
2702 if ((error = xfs_btree_insert(cur, &i)))
2704 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
2710 * Setting the middle part of a previous oldext extent to
2711 * newext. Contiguity is impossible here.
2712 * One extent becomes three extents.
2714 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2715 xfs_bmbt_set_blockcount(ep,
2716 new->br_startoff - PREV.br_startoff);
2717 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2720 r[1].br_startoff = new_endoff;
2721 r[1].br_blockcount =
2722 PREV.br_startoff + PREV.br_blockcount - new_endoff;
2723 r[1].br_startblock = new->br_startblock + new->br_blockcount;
2724 r[1].br_state = oldext;
2727 xfs_iext_insert(ip, *idx, 2, &r[0], state);
2729 ip->i_d.di_nextents += 2;
2731 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2733 rval = XFS_ILOG_CORE;
2734 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2735 PREV.br_startblock, PREV.br_blockcount,
2738 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
2739 /* new right extent - oldext */
2740 if ((error = xfs_bmbt_update(cur, r[1].br_startoff,
2741 r[1].br_startblock, r[1].br_blockcount,
2744 /* new left extent - oldext */
2745 cur->bc_rec.b = PREV;
2746 cur->bc_rec.b.br_blockcount =
2747 new->br_startoff - PREV.br_startoff;
2748 if ((error = xfs_btree_insert(cur, &i)))
2750 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
2752 * Reset the cursor to the position of the new extent
2753 * we are about to insert as we can't trust it after
2754 * the previous insert.
2756 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
2757 new->br_startblock, new->br_blockcount,
2760 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
2761 /* new middle extent - newext */
2762 cur->bc_rec.b.br_state = new->br_state;
2763 if ((error = xfs_btree_insert(cur, &i)))
2765 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
2769 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2770 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2771 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
2772 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2773 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2774 case BMAP_LEFT_CONTIG:
2775 case BMAP_RIGHT_CONTIG:
2777 * These cases are all impossible.
2782 /* convert to a btree if necessary */
2783 if (xfs_bmap_needs_btree(ip, XFS_DATA_FORK)) {
2784 int tmp_logflags; /* partial log flag return val */
2786 ASSERT(cur == NULL);
2787 error = xfs_bmap_extents_to_btree(tp, ip, first, flist, &cur,
2788 0, &tmp_logflags, XFS_DATA_FORK);
2789 *logflagsp |= tmp_logflags;
2794 /* clear out the allocated field, done with it now in any case. */
2796 cur->bc_private.b.allocated = 0;
2800 xfs_bmap_check_leaf_extents(*curp, ip, XFS_DATA_FORK);
2810 * Convert a hole to a delayed allocation.
2813 xfs_bmap_add_extent_hole_delay(
2814 xfs_inode_t *ip, /* incore inode pointer */
2815 xfs_extnum_t *idx, /* extent number to update/insert */
2816 xfs_bmbt_irec_t *new) /* new data to add to file extents */
2818 xfs_ifork_t *ifp; /* inode fork pointer */
2819 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2820 xfs_filblks_t newlen=0; /* new indirect size */
2821 xfs_filblks_t oldlen=0; /* old indirect size */
2822 xfs_bmbt_irec_t right; /* right neighbor extent entry */
2823 int state; /* state bits, accessed thru macros */
2824 xfs_filblks_t temp=0; /* temp for indirect calculations */
2826 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
2828 ASSERT(isnullstartblock(new->br_startblock));
2831 * Check and set flags if this segment has a left neighbor
2834 state |= BMAP_LEFT_VALID;
2835 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &left);
2837 if (isnullstartblock(left.br_startblock))
2838 state |= BMAP_LEFT_DELAY;
2842 * Check and set flags if the current (right) segment exists.
2843 * If it doesn't exist, we're converting the hole at end-of-file.
2845 if (*idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) {
2846 state |= BMAP_RIGHT_VALID;
2847 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx), &right);
2849 if (isnullstartblock(right.br_startblock))
2850 state |= BMAP_RIGHT_DELAY;
2854 * Set contiguity flags on the left and right neighbors.
2855 * Don't let extents get too large, even if the pieces are contiguous.
2857 if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) &&
2858 left.br_startoff + left.br_blockcount == new->br_startoff &&
2859 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2860 state |= BMAP_LEFT_CONTIG;
2862 if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) &&
2863 new->br_startoff + new->br_blockcount == right.br_startoff &&
2864 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2865 (!(state & BMAP_LEFT_CONTIG) ||
2866 (left.br_blockcount + new->br_blockcount +
2867 right.br_blockcount <= MAXEXTLEN)))
2868 state |= BMAP_RIGHT_CONTIG;
2871 * Switch out based on the contiguity flags.
2873 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2874 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2876 * New allocation is contiguous with delayed allocations
2877 * on the left and on the right.
2878 * Merge all three into a single extent record.
2881 temp = left.br_blockcount + new->br_blockcount +
2882 right.br_blockcount;
2884 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2885 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp);
2886 oldlen = startblockval(left.br_startblock) +
2887 startblockval(new->br_startblock) +
2888 startblockval(right.br_startblock);
2889 newlen = xfs_bmap_worst_indlen(ip, temp);
2890 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx),
2891 nullstartblock((int)newlen));
2892 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2894 xfs_iext_remove(ip, *idx + 1, 1, state);
2897 case BMAP_LEFT_CONTIG:
2899 * New allocation is contiguous with a delayed allocation
2901 * Merge the new allocation with the left neighbor.
2904 temp = left.br_blockcount + new->br_blockcount;
2906 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2907 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp);
2908 oldlen = startblockval(left.br_startblock) +
2909 startblockval(new->br_startblock);
2910 newlen = xfs_bmap_worst_indlen(ip, temp);
2911 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx),
2912 nullstartblock((int)newlen));
2913 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2916 case BMAP_RIGHT_CONTIG:
2918 * New allocation is contiguous with a delayed allocation
2920 * Merge the new allocation with the right neighbor.
2922 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2923 temp = new->br_blockcount + right.br_blockcount;
2924 oldlen = startblockval(new->br_startblock) +
2925 startblockval(right.br_startblock);
2926 newlen = xfs_bmap_worst_indlen(ip, temp);
2927 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx),
2929 nullstartblock((int)newlen), temp, right.br_state);
2930 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2935 * New allocation is not contiguous with another
2936 * delayed allocation.
2937 * Insert a new entry.
2939 oldlen = newlen = 0;
2940 xfs_iext_insert(ip, *idx, 1, new, state);
2943 if (oldlen != newlen) {
2944 ASSERT(oldlen > newlen);
2945 xfs_icsb_modify_counters(ip->i_mount, XFS_SBS_FDBLOCKS,
2946 (int64_t)(oldlen - newlen), 0);
2948 * Nothing to do for disk quota accounting here.
2954 * Convert a hole to a real allocation.
2956 STATIC int /* error */
2957 xfs_bmap_add_extent_hole_real(
2958 struct xfs_bmalloca *bma,
2961 struct xfs_bmbt_irec *new = &bma->got;
2962 int error; /* error return value */
2963 int i; /* temp state */
2964 xfs_ifork_t *ifp; /* inode fork pointer */
2965 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2966 xfs_bmbt_irec_t right; /* right neighbor extent entry */
2967 int rval=0; /* return value (logging flags) */
2968 int state; /* state bits, accessed thru macros */
2970 ifp = XFS_IFORK_PTR(bma->ip, whichfork);
2972 ASSERT(bma->idx >= 0);
2973 ASSERT(bma->idx <= ifp->if_bytes / sizeof(struct xfs_bmbt_rec));
2974 ASSERT(!isnullstartblock(new->br_startblock));
2976 !(bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
2978 XFS_STATS_INC(xs_add_exlist);
2981 if (whichfork == XFS_ATTR_FORK)
2982 state |= BMAP_ATTRFORK;
2985 * Check and set flags if this segment has a left neighbor.
2988 state |= BMAP_LEFT_VALID;
2989 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1), &left);
2990 if (isnullstartblock(left.br_startblock))
2991 state |= BMAP_LEFT_DELAY;
2995 * Check and set flags if this segment has a current value.
2996 * Not true if we're inserting into the "hole" at eof.
2998 if (bma->idx < ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) {
2999 state |= BMAP_RIGHT_VALID;
3000 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &right);
3001 if (isnullstartblock(right.br_startblock))
3002 state |= BMAP_RIGHT_DELAY;
3006 * We're inserting a real allocation between "left" and "right".
3007 * Set the contiguity flags. Don't let extents get too large.
3009 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
3010 left.br_startoff + left.br_blockcount == new->br_startoff &&
3011 left.br_startblock + left.br_blockcount == new->br_startblock &&
3012 left.br_state == new->br_state &&
3013 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
3014 state |= BMAP_LEFT_CONTIG;
3016 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
3017 new->br_startoff + new->br_blockcount == right.br_startoff &&
3018 new->br_startblock + new->br_blockcount == right.br_startblock &&
3019 new->br_state == right.br_state &&
3020 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
3021 (!(state & BMAP_LEFT_CONTIG) ||
3022 left.br_blockcount + new->br_blockcount +
3023 right.br_blockcount <= MAXEXTLEN))
3024 state |= BMAP_RIGHT_CONTIG;
3028 * Select which case we're in here, and implement it.
3030 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
3031 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
3033 * New allocation is contiguous with real allocations on the
3034 * left and on the right.
3035 * Merge all three into a single extent record.
3038 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
3039 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
3040 left.br_blockcount + new->br_blockcount +
3041 right.br_blockcount);
3042 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
3044 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
3046 XFS_IFORK_NEXT_SET(bma->ip, whichfork,
3047 XFS_IFORK_NEXTENTS(bma->ip, whichfork) - 1);
3048 if (bma->cur == NULL) {
3049 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
3051 rval = XFS_ILOG_CORE;
3052 error = xfs_bmbt_lookup_eq(bma->cur, right.br_startoff,
3053 right.br_startblock, right.br_blockcount,
3057 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
3058 error = xfs_btree_delete(bma->cur, &i);
3061 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
3062 error = xfs_btree_decrement(bma->cur, 0, &i);
3065 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
3066 error = xfs_bmbt_update(bma->cur, left.br_startoff,
3068 left.br_blockcount +
3069 new->br_blockcount +
3070 right.br_blockcount,
3077 case BMAP_LEFT_CONTIG:
3079 * New allocation is contiguous with a real allocation
3081 * Merge the new allocation with the left neighbor.
3084 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
3085 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
3086 left.br_blockcount + new->br_blockcount);
3087 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
3089 if (bma->cur == NULL) {
3090 rval = xfs_ilog_fext(whichfork);
3093 error = xfs_bmbt_lookup_eq(bma->cur, left.br_startoff,
3094 left.br_startblock, left.br_blockcount,
3098 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
3099 error = xfs_bmbt_update(bma->cur, left.br_startoff,
3101 left.br_blockcount +
3109 case BMAP_RIGHT_CONTIG:
3111 * New allocation is contiguous with a real allocation
3113 * Merge the new allocation with the right neighbor.
3115 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
3116 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, bma->idx),
3117 new->br_startoff, new->br_startblock,
3118 new->br_blockcount + right.br_blockcount,
3120 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
3122 if (bma->cur == NULL) {
3123 rval = xfs_ilog_fext(whichfork);
3126 error = xfs_bmbt_lookup_eq(bma->cur,
3128 right.br_startblock,
3129 right.br_blockcount, &i);
3132 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
3133 error = xfs_bmbt_update(bma->cur, new->br_startoff,
3135 new->br_blockcount +
3136 right.br_blockcount,
3145 * New allocation is not contiguous with another
3147 * Insert a new entry.
3149 xfs_iext_insert(bma->ip, bma->idx, 1, new, state);
3150 XFS_IFORK_NEXT_SET(bma->ip, whichfork,
3151 XFS_IFORK_NEXTENTS(bma->ip, whichfork) + 1);
3152 if (bma->cur == NULL) {
3153 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
3155 rval = XFS_ILOG_CORE;
3156 error = xfs_bmbt_lookup_eq(bma->cur,
3159 new->br_blockcount, &i);
3162 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
3163 bma->cur->bc_rec.b.br_state = new->br_state;
3164 error = xfs_btree_insert(bma->cur, &i);
3167 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
3172 /* convert to a btree if necessary */
3173 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
3174 int tmp_logflags; /* partial log flag return val */
3176 ASSERT(bma->cur == NULL);
3177 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
3178 bma->firstblock, bma->flist, &bma->cur,
3179 0, &tmp_logflags, whichfork);
3180 bma->logflags |= tmp_logflags;
3185 /* clear out the allocated field, done with it now in any case. */
3187 bma->cur->bc_private.b.allocated = 0;
3189 xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork);
3191 bma->logflags |= rval;
3196 * Functions used in the extent read, allocate and remove paths
3200 * Adjust the size of the new extent based on di_extsize and rt extsize.
3203 xfs_bmap_extsize_align(
3205 xfs_bmbt_irec_t *gotp, /* next extent pointer */
3206 xfs_bmbt_irec_t *prevp, /* previous extent pointer */
3207 xfs_extlen_t extsz, /* align to this extent size */
3208 int rt, /* is this a realtime inode? */
3209 int eof, /* is extent at end-of-file? */
3210 int delay, /* creating delalloc extent? */
3211 int convert, /* overwriting unwritten extent? */
3212 xfs_fileoff_t *offp, /* in/out: aligned offset */
3213 xfs_extlen_t *lenp) /* in/out: aligned length */
3215 xfs_fileoff_t orig_off; /* original offset */
3216 xfs_extlen_t orig_alen; /* original length */
3217 xfs_fileoff_t orig_end; /* original off+len */
3218 xfs_fileoff_t nexto; /* next file offset */
3219 xfs_fileoff_t prevo; /* previous file offset */
3220 xfs_fileoff_t align_off; /* temp for offset */
3221 xfs_extlen_t align_alen; /* temp for length */
3222 xfs_extlen_t temp; /* temp for calculations */
3227 orig_off = align_off = *offp;
3228 orig_alen = align_alen = *lenp;
3229 orig_end = orig_off + orig_alen;
3232 * If this request overlaps an existing extent, then don't
3233 * attempt to perform any additional alignment.
3235 if (!delay && !eof &&
3236 (orig_off >= gotp->br_startoff) &&
3237 (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
3242 * If the file offset is unaligned vs. the extent size
3243 * we need to align it. This will be possible unless
3244 * the file was previously written with a kernel that didn't
3245 * perform this alignment, or if a truncate shot us in the
3248 temp = do_mod(orig_off, extsz);
3254 * Same adjustment for the end of the requested area.
3256 if ((temp = (align_alen % extsz))) {
3257 align_alen += extsz - temp;
3260 * If the previous block overlaps with this proposed allocation
3261 * then move the start forward without adjusting the length.
3263 if (prevp->br_startoff != NULLFILEOFF) {
3264 if (prevp->br_startblock == HOLESTARTBLOCK)
3265 prevo = prevp->br_startoff;
3267 prevo = prevp->br_startoff + prevp->br_blockcount;
3270 if (align_off != orig_off && align_off < prevo)
3273 * If the next block overlaps with this proposed allocation
3274 * then move the start back without adjusting the length,
3275 * but not before offset 0.
3276 * This may of course make the start overlap previous block,
3277 * and if we hit the offset 0 limit then the next block
3278 * can still overlap too.
3280 if (!eof && gotp->br_startoff != NULLFILEOFF) {
3281 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
3282 (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
3283 nexto = gotp->br_startoff + gotp->br_blockcount;
3285 nexto = gotp->br_startoff;
3287 nexto = NULLFILEOFF;
3289 align_off + align_alen != orig_end &&
3290 align_off + align_alen > nexto)
3291 align_off = nexto > align_alen ? nexto - align_alen : 0;
3293 * If we're now overlapping the next or previous extent that
3294 * means we can't fit an extsz piece in this hole. Just move
3295 * the start forward to the first valid spot and set
3296 * the length so we hit the end.
3298 if (align_off != orig_off && align_off < prevo)
3300 if (align_off + align_alen != orig_end &&
3301 align_off + align_alen > nexto &&
3302 nexto != NULLFILEOFF) {
3303 ASSERT(nexto > prevo);
3304 align_alen = nexto - align_off;
3308 * If realtime, and the result isn't a multiple of the realtime
3309 * extent size we need to remove blocks until it is.
3311 if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
3313 * We're not covering the original request, or
3314 * we won't be able to once we fix the length.
3316 if (orig_off < align_off ||
3317 orig_end > align_off + align_alen ||
3318 align_alen - temp < orig_alen)
3321 * Try to fix it by moving the start up.
3323 if (align_off + temp <= orig_off) {
3328 * Try to fix it by moving the end in.
3330 else if (align_off + align_alen - temp >= orig_end)
3333 * Set the start to the minimum then trim the length.
3336 align_alen -= orig_off - align_off;
3337 align_off = orig_off;
3338 align_alen -= align_alen % mp->m_sb.sb_rextsize;
3341 * Result doesn't cover the request, fail it.
3343 if (orig_off < align_off || orig_end > align_off + align_alen)
3346 ASSERT(orig_off >= align_off);
3347 ASSERT(orig_end <= align_off + align_alen);
3351 if (!eof && gotp->br_startoff != NULLFILEOFF)
3352 ASSERT(align_off + align_alen <= gotp->br_startoff);
3353 if (prevp->br_startoff != NULLFILEOFF)
3354 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
3362 #define XFS_ALLOC_GAP_UNITS 4
3366 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
3368 xfs_fsblock_t adjust; /* adjustment to block numbers */
3369 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
3370 xfs_mount_t *mp; /* mount point structure */
3371 int nullfb; /* true if ap->firstblock isn't set */
3372 int rt; /* true if inode is realtime */
3374 #define ISVALID(x,y) \
3376 (x) < mp->m_sb.sb_rblocks : \
3377 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
3378 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
3379 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
3381 mp = ap->ip->i_mount;
3382 nullfb = *ap->firstblock == NULLFSBLOCK;
3383 rt = XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata;
3384 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
3386 * If allocating at eof, and there's a previous real block,
3387 * try to use its last block as our starting point.
3389 if (ap->eof && ap->prev.br_startoff != NULLFILEOFF &&
3390 !isnullstartblock(ap->prev.br_startblock) &&
3391 ISVALID(ap->prev.br_startblock + ap->prev.br_blockcount,
3392 ap->prev.br_startblock)) {
3393 ap->blkno = ap->prev.br_startblock + ap->prev.br_blockcount;
3395 * Adjust for the gap between prevp and us.
3397 adjust = ap->offset -
3398 (ap->prev.br_startoff + ap->prev.br_blockcount);
3400 ISVALID(ap->blkno + adjust, ap->prev.br_startblock))
3401 ap->blkno += adjust;
3404 * If not at eof, then compare the two neighbor blocks.
3405 * Figure out whether either one gives us a good starting point,
3406 * and pick the better one.
3408 else if (!ap->eof) {
3409 xfs_fsblock_t gotbno; /* right side block number */
3410 xfs_fsblock_t gotdiff=0; /* right side difference */
3411 xfs_fsblock_t prevbno; /* left side block number */
3412 xfs_fsblock_t prevdiff=0; /* left side difference */
3415 * If there's a previous (left) block, select a requested
3416 * start block based on it.
3418 if (ap->prev.br_startoff != NULLFILEOFF &&
3419 !isnullstartblock(ap->prev.br_startblock) &&
3420 (prevbno = ap->prev.br_startblock +
3421 ap->prev.br_blockcount) &&
3422 ISVALID(prevbno, ap->prev.br_startblock)) {
3424 * Calculate gap to end of previous block.
3426 adjust = prevdiff = ap->offset -
3427 (ap->prev.br_startoff +
3428 ap->prev.br_blockcount);
3430 * Figure the startblock based on the previous block's
3431 * end and the gap size.
3433 * If the gap is large relative to the piece we're
3434 * allocating, or using it gives us an invalid block
3435 * number, then just use the end of the previous block.
3437 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
3438 ISVALID(prevbno + prevdiff,
3439 ap->prev.br_startblock))
3444 * If the firstblock forbids it, can't use it,
3447 if (!rt && !nullfb &&
3448 XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
3449 prevbno = NULLFSBLOCK;
3452 * No previous block or can't follow it, just default.
3455 prevbno = NULLFSBLOCK;
3457 * If there's a following (right) block, select a requested
3458 * start block based on it.
3460 if (!isnullstartblock(ap->got.br_startblock)) {
3462 * Calculate gap to start of next block.
3464 adjust = gotdiff = ap->got.br_startoff - ap->offset;
3466 * Figure the startblock based on the next block's
3467 * start and the gap size.
3469 gotbno = ap->got.br_startblock;
3472 * If the gap is large relative to the piece we're
3473 * allocating, or using it gives us an invalid block
3474 * number, then just use the start of the next block
3475 * offset by our length.
3477 if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
3478 ISVALID(gotbno - gotdiff, gotbno))
3480 else if (ISVALID(gotbno - ap->length, gotbno)) {
3481 gotbno -= ap->length;
3482 gotdiff += adjust - ap->length;
3486 * If the firstblock forbids it, can't use it,
3489 if (!rt && !nullfb &&
3490 XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
3491 gotbno = NULLFSBLOCK;
3494 * No next block, just default.
3497 gotbno = NULLFSBLOCK;
3499 * If both valid, pick the better one, else the only good
3500 * one, else ap->blkno is already set (to 0 or the inode block).
3502 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
3503 ap->blkno = prevdiff <= gotdiff ? prevbno : gotbno;
3504 else if (prevbno != NULLFSBLOCK)
3505 ap->blkno = prevbno;
3506 else if (gotbno != NULLFSBLOCK)
3513 xfs_bmap_longest_free_extent(
3514 struct xfs_trans *tp,
3519 struct xfs_mount *mp = tp->t_mountp;
3520 struct xfs_perag *pag;
3521 xfs_extlen_t longest;
3524 pag = xfs_perag_get(mp, ag);
3525 if (!pag->pagf_init) {
3526 error = xfs_alloc_pagf_init(mp, tp, ag, XFS_ALLOC_FLAG_TRYLOCK);
3530 if (!pag->pagf_init) {
3536 longest = xfs_alloc_longest_free_extent(mp, pag);
3537 if (*blen < longest)
3546 xfs_bmap_select_minlen(
3547 struct xfs_bmalloca *ap,
3548 struct xfs_alloc_arg *args,
3552 if (notinit || *blen < ap->minlen) {
3554 * Since we did a BUF_TRYLOCK above, it is possible that
3555 * there is space for this request.
3557 args->minlen = ap->minlen;
3558 } else if (*blen < args->maxlen) {
3560 * If the best seen length is less than the request length,
3561 * use the best as the minimum.
3563 args->minlen = *blen;
3566 * Otherwise we've seen an extent as big as maxlen, use that
3569 args->minlen = args->maxlen;
3574 xfs_bmap_btalloc_nullfb(
3575 struct xfs_bmalloca *ap,
3576 struct xfs_alloc_arg *args,
3579 struct xfs_mount *mp = ap->ip->i_mount;
3580 xfs_agnumber_t ag, startag;
3584 args->type = XFS_ALLOCTYPE_START_BNO;
3585 args->total = ap->total;
3587 startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3588 if (startag == NULLAGNUMBER)
3591 while (*blen < args->maxlen) {
3592 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3597 if (++ag == mp->m_sb.sb_agcount)
3603 xfs_bmap_select_minlen(ap, args, blen, notinit);
3608 xfs_bmap_btalloc_filestreams(
3609 struct xfs_bmalloca *ap,
3610 struct xfs_alloc_arg *args,
3613 struct xfs_mount *mp = ap->ip->i_mount;
3618 args->type = XFS_ALLOCTYPE_NEAR_BNO;
3619 args->total = ap->total;
3621 ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3622 if (ag == NULLAGNUMBER)
3625 error = xfs_bmap_longest_free_extent(args->tp, ag, blen, ¬init);
3629 if (*blen < args->maxlen) {
3630 error = xfs_filestream_new_ag(ap, &ag);
3634 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3641 xfs_bmap_select_minlen(ap, args, blen, notinit);
3644 * Set the failure fallback case to look in the selected AG as stream
3647 ap->blkno = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0);
3653 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
3655 xfs_mount_t *mp; /* mount point structure */
3656 xfs_alloctype_t atype = 0; /* type for allocation routines */
3657 xfs_extlen_t align; /* minimum allocation alignment */
3658 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
3660 xfs_alloc_arg_t args;
3662 xfs_extlen_t nextminlen = 0;
3663 int nullfb; /* true if ap->firstblock isn't set */
3671 mp = ap->ip->i_mount;
3673 /* stripe alignment for allocation is determined by mount parameters */
3675 if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC))
3676 stripe_align = mp->m_swidth;
3677 else if (mp->m_dalign)
3678 stripe_align = mp->m_dalign;
3680 align = ap->userdata ? xfs_get_extsz_hint(ap->ip) : 0;
3681 if (unlikely(align)) {
3682 error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev,
3683 align, 0, ap->eof, 0, ap->conv,
3684 &ap->offset, &ap->length);
3690 nullfb = *ap->firstblock == NULLFSBLOCK;
3691 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
3693 if (ap->userdata && xfs_inode_is_filestream(ap->ip)) {
3694 ag = xfs_filestream_lookup_ag(ap->ip);
3695 ag = (ag != NULLAGNUMBER) ? ag : 0;
3696 ap->blkno = XFS_AGB_TO_FSB(mp, ag, 0);
3698 ap->blkno = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
3701 ap->blkno = *ap->firstblock;
3703 xfs_bmap_adjacent(ap);
3706 * If allowed, use ap->blkno; otherwise must use firstblock since
3707 * it's in the right allocation group.
3709 if (nullfb || XFS_FSB_TO_AGNO(mp, ap->blkno) == fb_agno)
3712 ap->blkno = *ap->firstblock;
3714 * Normal allocation, done through xfs_alloc_vextent.
3716 tryagain = isaligned = 0;
3717 memset(&args, 0, sizeof(args));
3720 args.fsbno = ap->blkno;
3722 /* Trim the allocation back to the maximum an AG can fit. */
3723 args.maxlen = MIN(ap->length, XFS_ALLOC_AG_MAX_USABLE(mp));
3724 args.firstblock = *ap->firstblock;
3728 * Search for an allocation group with a single extent large
3729 * enough for the request. If one isn't found, then adjust
3730 * the minimum allocation size to the largest space found.
3732 if (ap->userdata && xfs_inode_is_filestream(ap->ip))
3733 error = xfs_bmap_btalloc_filestreams(ap, &args, &blen);
3735 error = xfs_bmap_btalloc_nullfb(ap, &args, &blen);
3738 } else if (ap->flist->xbf_low) {
3739 if (xfs_inode_is_filestream(ap->ip))
3740 args.type = XFS_ALLOCTYPE_FIRST_AG;
3742 args.type = XFS_ALLOCTYPE_START_BNO;
3743 args.total = args.minlen = ap->minlen;
3745 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3746 args.total = ap->total;
3747 args.minlen = ap->minlen;
3749 /* apply extent size hints if obtained earlier */
3750 if (unlikely(align)) {
3752 if ((args.mod = (xfs_extlen_t)do_mod(ap->offset, args.prod)))
3753 args.mod = (xfs_extlen_t)(args.prod - args.mod);
3754 } else if (mp->m_sb.sb_blocksize >= PAGE_CACHE_SIZE) {
3758 args.prod = PAGE_CACHE_SIZE >> mp->m_sb.sb_blocklog;
3759 if ((args.mod = (xfs_extlen_t)(do_mod(ap->offset, args.prod))))
3760 args.mod = (xfs_extlen_t)(args.prod - args.mod);
3763 * If we are not low on available data blocks, and the
3764 * underlying logical volume manager is a stripe, and
3765 * the file offset is zero then try to allocate data
3766 * blocks on stripe unit boundary.
3767 * NOTE: ap->aeof is only set if the allocation length
3768 * is >= the stripe unit and the allocation offset is
3769 * at the end of file.
3771 if (!ap->flist->xbf_low && ap->aeof) {
3773 args.alignment = stripe_align;
3777 * Adjust for alignment
3779 if (blen > args.alignment && blen <= args.maxlen)
3780 args.minlen = blen - args.alignment;
3781 args.minalignslop = 0;
3784 * First try an exact bno allocation.
3785 * If it fails then do a near or start bno
3786 * allocation with alignment turned on.
3790 args.type = XFS_ALLOCTYPE_THIS_BNO;
3793 * Compute the minlen+alignment for the
3794 * next case. Set slop so that the value
3795 * of minlen+alignment+slop doesn't go up
3796 * between the calls.
3798 if (blen > stripe_align && blen <= args.maxlen)
3799 nextminlen = blen - stripe_align;
3801 nextminlen = args.minlen;
3802 if (nextminlen + stripe_align > args.minlen + 1)
3804 nextminlen + stripe_align -
3807 args.minalignslop = 0;
3811 args.minalignslop = 0;
3813 args.minleft = ap->minleft;
3814 args.wasdel = ap->wasdel;
3816 args.userdata = ap->userdata;
3817 if ((error = xfs_alloc_vextent(&args)))
3819 if (tryagain && args.fsbno == NULLFSBLOCK) {
3821 * Exact allocation failed. Now try with alignment
3825 args.fsbno = ap->blkno;
3826 args.alignment = stripe_align;
3827 args.minlen = nextminlen;
3828 args.minalignslop = 0;
3830 if ((error = xfs_alloc_vextent(&args)))
3833 if (isaligned && args.fsbno == NULLFSBLOCK) {
3835 * allocation failed, so turn off alignment and
3839 args.fsbno = ap->blkno;
3841 if ((error = xfs_alloc_vextent(&args)))
3844 if (args.fsbno == NULLFSBLOCK && nullfb &&
3845 args.minlen > ap->minlen) {
3846 args.minlen = ap->minlen;
3847 args.type = XFS_ALLOCTYPE_START_BNO;
3848 args.fsbno = ap->blkno;
3849 if ((error = xfs_alloc_vextent(&args)))
3852 if (args.fsbno == NULLFSBLOCK && nullfb) {
3854 args.type = XFS_ALLOCTYPE_FIRST_AG;
3855 args.total = ap->minlen;
3857 if ((error = xfs_alloc_vextent(&args)))
3859 ap->flist->xbf_low = 1;
3861 if (args.fsbno != NULLFSBLOCK) {
3863 * check the allocation happened at the same or higher AG than
3864 * the first block that was allocated.
3866 ASSERT(*ap->firstblock == NULLFSBLOCK ||
3867 XFS_FSB_TO_AGNO(mp, *ap->firstblock) ==
3868 XFS_FSB_TO_AGNO(mp, args.fsbno) ||
3869 (ap->flist->xbf_low &&
3870 XFS_FSB_TO_AGNO(mp, *ap->firstblock) <
3871 XFS_FSB_TO_AGNO(mp, args.fsbno)));
3873 ap->blkno = args.fsbno;
3874 if (*ap->firstblock == NULLFSBLOCK)
3875 *ap->firstblock = args.fsbno;
3876 ASSERT(nullfb || fb_agno == args.agno ||
3877 (ap->flist->xbf_low && fb_agno < args.agno));
3878 ap->length = args.len;
3879 ap->ip->i_d.di_nblocks += args.len;
3880 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
3882 ap->ip->i_delayed_blks -= args.len;
3884 * Adjust the disk quota also. This was reserved
3887 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
3888 ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT :
3889 XFS_TRANS_DQ_BCOUNT,
3892 ap->blkno = NULLFSBLOCK;
3899 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
3900 * It figures out where to ask the underlying allocator to put the new extent.
3904 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
3906 if (XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata)
3907 return xfs_bmap_rtalloc(ap);
3908 return xfs_bmap_btalloc(ap);
3912 * Trim the returned map to the required bounds
3916 struct xfs_bmbt_irec *mval,
3917 struct xfs_bmbt_irec *got,
3925 if ((flags & XFS_BMAPI_ENTIRE) ||
3926 got->br_startoff + got->br_blockcount <= obno) {
3928 if (isnullstartblock(got->br_startblock))
3929 mval->br_startblock = DELAYSTARTBLOCK;
3935 ASSERT((*bno >= obno) || (n == 0));
3937 mval->br_startoff = *bno;
3938 if (isnullstartblock(got->br_startblock))
3939 mval->br_startblock = DELAYSTARTBLOCK;
3941 mval->br_startblock = got->br_startblock +
3942 (*bno - got->br_startoff);
3944 * Return the minimum of what we got and what we asked for for
3945 * the length. We can use the len variable here because it is
3946 * modified below and we could have been there before coming
3947 * here if the first part of the allocation didn't overlap what
3950 mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno,
3951 got->br_blockcount - (*bno - got->br_startoff));
3952 mval->br_state = got->br_state;
3953 ASSERT(mval->br_blockcount <= len);
3958 * Update and validate the extent map to return
3961 xfs_bmapi_update_map(
3962 struct xfs_bmbt_irec **map,
3970 xfs_bmbt_irec_t *mval = *map;
3972 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
3973 ((mval->br_startoff + mval->br_blockcount) <= end));
3974 ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) ||
3975 (mval->br_startoff < obno));
3977 *bno = mval->br_startoff + mval->br_blockcount;
3979 if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) {
3980 /* update previous map with new information */
3981 ASSERT(mval->br_startblock == mval[-1].br_startblock);
3982 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
3983 ASSERT(mval->br_state == mval[-1].br_state);
3984 mval[-1].br_blockcount = mval->br_blockcount;
3985 mval[-1].br_state = mval->br_state;
3986 } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
3987 mval[-1].br_startblock != DELAYSTARTBLOCK &&
3988 mval[-1].br_startblock != HOLESTARTBLOCK &&
3989 mval->br_startblock == mval[-1].br_startblock +
3990 mval[-1].br_blockcount &&
3991 ((flags & XFS_BMAPI_IGSTATE) ||
3992 mval[-1].br_state == mval->br_state)) {
3993 ASSERT(mval->br_startoff ==
3994 mval[-1].br_startoff + mval[-1].br_blockcount);
3995 mval[-1].br_blockcount += mval->br_blockcount;
3996 } else if (*n > 0 &&
3997 mval->br_startblock == DELAYSTARTBLOCK &&
3998 mval[-1].br_startblock == DELAYSTARTBLOCK &&
3999 mval->br_startoff ==
4000 mval[-1].br_startoff + mval[-1].br_blockcount) {
4001 mval[-1].br_blockcount += mval->br_blockcount;
4002 mval[-1].br_state = mval->br_state;
4003 } else if (!((*n == 0) &&
4004 ((mval->br_startoff + mval->br_blockcount) <=
4013 * Map file blocks to filesystem blocks without allocation.
4017 struct xfs_inode *ip,
4020 struct xfs_bmbt_irec *mval,
4024 struct xfs_mount *mp = ip->i_mount;
4025 struct xfs_ifork *ifp;
4026 struct xfs_bmbt_irec got;
4027 struct xfs_bmbt_irec prev;
4034 int whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
4035 XFS_ATTR_FORK : XFS_DATA_FORK;
4038 ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK|XFS_BMAPI_ENTIRE|
4039 XFS_BMAPI_IGSTATE)));
4040 ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED|XFS_ILOCK_EXCL));
4042 if (unlikely(XFS_TEST_ERROR(
4043 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4044 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
4045 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
4046 XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW, mp);
4047 return -EFSCORRUPTED;
4050 if (XFS_FORCED_SHUTDOWN(mp))
4053 XFS_STATS_INC(xs_blk_mapr);
4055 ifp = XFS_IFORK_PTR(ip, whichfork);
4057 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4058 error = xfs_iread_extents(NULL, ip, whichfork);
4063 xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got, &prev);
4067 while (bno < end && n < *nmap) {
4068 /* Reading past eof, act as though there's a hole up to end. */
4070 got.br_startoff = end;
4071 if (got.br_startoff > bno) {
4072 /* Reading in a hole. */
4073 mval->br_startoff = bno;
4074 mval->br_startblock = HOLESTARTBLOCK;
4075 mval->br_blockcount =
4076 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
4077 mval->br_state = XFS_EXT_NORM;
4078 bno += mval->br_blockcount;
4079 len -= mval->br_blockcount;
4085 /* set up the extent map to return. */
4086 xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
4087 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4089 /* If we're done, stop now. */
4090 if (bno >= end || n >= *nmap)
4093 /* Else go on to the next record. */
4094 if (++lastx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t))
4095 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx), &got);
4104 xfs_bmapi_reserve_delalloc(
4105 struct xfs_inode *ip,
4108 struct xfs_bmbt_irec *got,
4109 struct xfs_bmbt_irec *prev,
4110 xfs_extnum_t *lastx,
4113 struct xfs_mount *mp = ip->i_mount;
4114 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
4116 xfs_extlen_t indlen;
4117 char rt = XFS_IS_REALTIME_INODE(ip);
4121 alen = XFS_FILBLKS_MIN(len, MAXEXTLEN);
4123 alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff);
4125 /* Figure out the extent size, adjust alen */
4126 extsz = xfs_get_extsz_hint(ip);
4129 * Make sure we don't exceed a single extent length when we
4130 * align the extent by reducing length we are going to
4131 * allocate by the maximum amount extent size aligment may
4134 alen = XFS_FILBLKS_MIN(len, MAXEXTLEN - (2 * extsz - 1));
4135 error = xfs_bmap_extsize_align(mp, got, prev, extsz, rt, eof,
4136 1, 0, &aoff, &alen);
4141 extsz = alen / mp->m_sb.sb_rextsize;
4144 * Make a transaction-less quota reservation for delayed allocation
4145 * blocks. This number gets adjusted later. We return if we haven't
4146 * allocated blocks already inside this loop.
4148 error = xfs_trans_reserve_quota_nblks(NULL, ip, (long)alen, 0,
4149 rt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
4154 * Split changing sb for alen and indlen since they could be coming
4155 * from different places.
4157 indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen);
4161 error = xfs_mod_incore_sb(mp, XFS_SBS_FREXTENTS,
4162 -((int64_t)extsz), 0);
4164 error = xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS,
4165 -((int64_t)alen), 0);
4169 goto out_unreserve_quota;
4171 error = xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS,
4172 -((int64_t)indlen), 0);
4174 goto out_unreserve_blocks;
4177 ip->i_delayed_blks += alen;
4179 got->br_startoff = aoff;
4180 got->br_startblock = nullstartblock(indlen);
4181 got->br_blockcount = alen;
4182 got->br_state = XFS_EXT_NORM;
4183 xfs_bmap_add_extent_hole_delay(ip, lastx, got);
4186 * Update our extent pointer, given that xfs_bmap_add_extent_hole_delay
4187 * might have merged it into one of the neighbouring ones.
4189 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *lastx), got);
4191 ASSERT(got->br_startoff <= aoff);
4192 ASSERT(got->br_startoff + got->br_blockcount >= aoff + alen);
4193 ASSERT(isnullstartblock(got->br_startblock));
4194 ASSERT(got->br_state == XFS_EXT_NORM);
4197 out_unreserve_blocks:
4199 xfs_mod_incore_sb(mp, XFS_SBS_FREXTENTS, extsz, 0);
4201 xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS, alen, 0);
4202 out_unreserve_quota:
4203 if (XFS_IS_QUOTA_ON(mp))
4204 xfs_trans_unreserve_quota_nblks(NULL, ip, (long)alen, 0, rt ?
4205 XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
4210 * Map file blocks to filesystem blocks, adding delayed allocations as needed.
4214 struct xfs_inode *ip, /* incore inode */
4215 xfs_fileoff_t bno, /* starting file offs. mapped */
4216 xfs_filblks_t len, /* length to map in file */
4217 struct xfs_bmbt_irec *mval, /* output: map values */
4218 int *nmap, /* i/o: mval size/count */
4219 int flags) /* XFS_BMAPI_... */
4221 struct xfs_mount *mp = ip->i_mount;
4222 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
4223 struct xfs_bmbt_irec got; /* current file extent record */
4224 struct xfs_bmbt_irec prev; /* previous file extent record */
4225 xfs_fileoff_t obno; /* old block number (offset) */
4226 xfs_fileoff_t end; /* end of mapped file region */
4227 xfs_extnum_t lastx; /* last useful extent number */
4228 int eof; /* we've hit the end of extents */
4229 int n = 0; /* current extent index */
4233 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP);
4234 ASSERT(!(flags & ~XFS_BMAPI_ENTIRE));
4235 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
4237 if (unlikely(XFS_TEST_ERROR(
4238 (XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_EXTENTS &&
4239 XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_BTREE),
4240 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
4241 XFS_ERROR_REPORT("xfs_bmapi_delay", XFS_ERRLEVEL_LOW, mp);
4242 return -EFSCORRUPTED;
4245 if (XFS_FORCED_SHUTDOWN(mp))
4248 XFS_STATS_INC(xs_blk_mapw);
4250 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4251 error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK);
4256 xfs_bmap_search_extents(ip, bno, XFS_DATA_FORK, &eof, &lastx, &got, &prev);
4260 while (bno < end && n < *nmap) {
4261 if (eof || got.br_startoff > bno) {
4262 error = xfs_bmapi_reserve_delalloc(ip, bno, len, &got,
4263 &prev, &lastx, eof);
4273 /* set up the extent map to return. */
4274 xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
4275 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4277 /* If we're done, stop now. */
4278 if (bno >= end || n >= *nmap)
4281 /* Else go on to the next record. */
4283 if (++lastx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t))
4284 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx), &got);
4296 struct xfs_bmalloca *bma)
4298 struct xfs_mount *mp = bma->ip->i_mount;
4299 int whichfork = (bma->flags & XFS_BMAPI_ATTRFORK) ?
4300 XFS_ATTR_FORK : XFS_DATA_FORK;
4301 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
4302 int tmp_logflags = 0;
4305 ASSERT(bma->length > 0);
4308 * For the wasdelay case, we could also just allocate the stuff asked
4309 * for in this bmap call but that wouldn't be as good.
4312 bma->length = (xfs_extlen_t)bma->got.br_blockcount;
4313 bma->offset = bma->got.br_startoff;
4314 if (bma->idx != NULLEXTNUM && bma->idx) {
4315 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1),
4319 bma->length = XFS_FILBLKS_MIN(bma->length, MAXEXTLEN);
4321 bma->length = XFS_FILBLKS_MIN(bma->length,
4322 bma->got.br_startoff - bma->offset);
4326 * Indicate if this is the first user data in the file, or just any
4329 if (!(bma->flags & XFS_BMAPI_METADATA)) {
4330 bma->userdata = (bma->offset == 0) ?
4331 XFS_ALLOC_INITIAL_USER_DATA : XFS_ALLOC_USERDATA;
4334 bma->minlen = (bma->flags & XFS_BMAPI_CONTIG) ? bma->length : 1;
4337 * Only want to do the alignment at the eof if it is userdata and
4338 * allocation length is larger than a stripe unit.
4340 if (mp->m_dalign && bma->length >= mp->m_dalign &&
4341 !(bma->flags & XFS_BMAPI_METADATA) && whichfork == XFS_DATA_FORK) {
4342 error = xfs_bmap_isaeof(bma, whichfork);
4347 error = xfs_bmap_alloc(bma);
4351 if (bma->flist->xbf_low)
4354 bma->cur->bc_private.b.firstblock = *bma->firstblock;
4355 if (bma->blkno == NULLFSBLOCK)
4357 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4358 bma->cur = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork);
4359 bma->cur->bc_private.b.firstblock = *bma->firstblock;
4360 bma->cur->bc_private.b.flist = bma->flist;
4363 * Bump the number of extents we've allocated
4369 bma->cur->bc_private.b.flags =
4370 bma->wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
4372 bma->got.br_startoff = bma->offset;
4373 bma->got.br_startblock = bma->blkno;
4374 bma->got.br_blockcount = bma->length;
4375 bma->got.br_state = XFS_EXT_NORM;
4378 * A wasdelay extent has been initialized, so shouldn't be flagged
4381 if (!bma->wasdel && (bma->flags & XFS_BMAPI_PREALLOC) &&
4382 xfs_sb_version_hasextflgbit(&mp->m_sb))
4383 bma->got.br_state = XFS_EXT_UNWRITTEN;
4386 error = xfs_bmap_add_extent_delay_real(bma);
4388 error = xfs_bmap_add_extent_hole_real(bma, whichfork);
4390 bma->logflags |= tmp_logflags;
4395 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4396 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4397 * the neighbouring ones.
4399 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &bma->got);
4401 ASSERT(bma->got.br_startoff <= bma->offset);
4402 ASSERT(bma->got.br_startoff + bma->got.br_blockcount >=
4403 bma->offset + bma->length);
4404 ASSERT(bma->got.br_state == XFS_EXT_NORM ||
4405 bma->got.br_state == XFS_EXT_UNWRITTEN);
4410 xfs_bmapi_convert_unwritten(
4411 struct xfs_bmalloca *bma,
4412 struct xfs_bmbt_irec *mval,
4416 int whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
4417 XFS_ATTR_FORK : XFS_DATA_FORK;
4418 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
4419 int tmp_logflags = 0;
4422 /* check if we need to do unwritten->real conversion */
4423 if (mval->br_state == XFS_EXT_UNWRITTEN &&
4424 (flags & XFS_BMAPI_PREALLOC))
4427 /* check if we need to do real->unwritten conversion */
4428 if (mval->br_state == XFS_EXT_NORM &&
4429 (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) !=
4430 (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT))
4434 * Modify (by adding) the state flag, if writing.
4436 ASSERT(mval->br_blockcount <= len);
4437 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4438 bma->cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp,
4439 bma->ip, whichfork);
4440 bma->cur->bc_private.b.firstblock = *bma->firstblock;
4441 bma->cur->bc_private.b.flist = bma->flist;
4443 mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN)
4444 ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
4446 error = xfs_bmap_add_extent_unwritten_real(bma->tp, bma->ip, &bma->idx,
4447 &bma->cur, mval, bma->firstblock, bma->flist,
4449 bma->logflags |= tmp_logflags;
4454 * Update our extent pointer, given that
4455 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4456 * of the neighbouring ones.
4458 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &bma->got);
4461 * We may have combined previously unwritten space with written space,
4462 * so generate another request.
4464 if (mval->br_blockcount < len)
4470 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4471 * extent state if necessary. Details behaviour is controlled by the flags
4472 * parameter. Only allocates blocks from a single allocation group, to avoid
4475 * The returned value in "firstblock" from the first call in a transaction
4476 * must be remembered and presented to subsequent calls in "firstblock".
4477 * An upper bound for the number of blocks to be allocated is supplied to
4478 * the first call in "total"; if no allocation group has that many free
4479 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4483 struct xfs_trans *tp, /* transaction pointer */
4484 struct xfs_inode *ip, /* incore inode */
4485 xfs_fileoff_t bno, /* starting file offs. mapped */
4486 xfs_filblks_t len, /* length to map in file */
4487 int flags, /* XFS_BMAPI_... */
4488 xfs_fsblock_t *firstblock, /* first allocated block
4489 controls a.g. for allocs */
4490 xfs_extlen_t total, /* total blocks needed */
4491 struct xfs_bmbt_irec *mval, /* output: map values */
4492 int *nmap, /* i/o: mval size/count */
4493 struct xfs_bmap_free *flist) /* i/o: list extents to free */
4495 struct xfs_mount *mp = ip->i_mount;
4496 struct xfs_ifork *ifp;
4497 struct xfs_bmalloca bma = { NULL }; /* args for xfs_bmap_alloc */
4498 xfs_fileoff_t end; /* end of mapped file region */
4499 int eof; /* after the end of extents */
4500 int error; /* error return */
4501 int n; /* current extent index */
4502 xfs_fileoff_t obno; /* old block number (offset) */
4503 int whichfork; /* data or attr fork */
4504 char inhole; /* current location is hole in file */
4505 char wasdelay; /* old extent was delayed */
4508 xfs_fileoff_t orig_bno; /* original block number value */
4509 int orig_flags; /* original flags arg value */
4510 xfs_filblks_t orig_len; /* original value of len arg */
4511 struct xfs_bmbt_irec *orig_mval; /* original value of mval */
4512 int orig_nmap; /* original value of *nmap */
4520 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
4521 XFS_ATTR_FORK : XFS_DATA_FORK;
4524 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP);
4525 ASSERT(!(flags & XFS_BMAPI_IGSTATE));
4528 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL);
4529 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
4531 if (unlikely(XFS_TEST_ERROR(
4532 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4533 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
4534 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
4535 XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW, mp);
4536 return -EFSCORRUPTED;
4539 if (XFS_FORCED_SHUTDOWN(mp))
4542 ifp = XFS_IFORK_PTR(ip, whichfork);
4544 XFS_STATS_INC(xs_blk_mapw);
4546 if (*firstblock == NULLFSBLOCK) {
4547 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE)
4548 bma.minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1;
4555 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4556 error = xfs_iread_extents(tp, ip, whichfork);
4561 xfs_bmap_search_extents(ip, bno, whichfork, &eof, &bma.idx, &bma.got,
4572 bma.firstblock = firstblock;
4574 while (bno < end && n < *nmap) {
4575 inhole = eof || bma.got.br_startoff > bno;
4576 wasdelay = !inhole && isnullstartblock(bma.got.br_startblock);
4579 * First, deal with the hole before the allocated space
4580 * that we found, if any.
4582 if (inhole || wasdelay) {
4584 bma.conv = !!(flags & XFS_BMAPI_CONVERT);
4585 bma.wasdel = wasdelay;
4590 * There's a 32/64 bit type mismatch between the
4591 * allocation length request (which can be 64 bits in
4592 * length) and the bma length request, which is
4593 * xfs_extlen_t and therefore 32 bits. Hence we have to
4594 * check for 32-bit overflows and handle them here.
4596 if (len > (xfs_filblks_t)MAXEXTLEN)
4597 bma.length = MAXEXTLEN;
4602 ASSERT(bma.length > 0);
4603 error = xfs_bmapi_allocate(&bma);
4606 if (bma.blkno == NULLFSBLOCK)
4610 /* Deal with the allocated space we found. */
4611 xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno,
4614 /* Execute unwritten extent conversion if necessary */
4615 error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags);
4616 if (error == -EAGAIN)
4621 /* update the extent map to return */
4622 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4625 * If we're done, stop now. Stop when we've allocated
4626 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4627 * the transaction may get too big.
4629 if (bno >= end || n >= *nmap || bma.nallocs >= *nmap)
4632 /* Else go on to the next record. */
4634 if (++bma.idx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t)) {
4635 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma.idx),
4643 * Transform from btree to extents, give it cur.
4645 if (xfs_bmap_wants_extents(ip, whichfork)) {
4646 int tmp_logflags = 0;
4649 error = xfs_bmap_btree_to_extents(tp, ip, bma.cur,
4650 &tmp_logflags, whichfork);
4651 bma.logflags |= tmp_logflags;
4656 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE ||
4657 XFS_IFORK_NEXTENTS(ip, whichfork) >
4658 XFS_IFORK_MAXEXT(ip, whichfork));
4662 * Log everything. Do this after conversion, there's no point in
4663 * logging the extent records if we've converted to btree format.
4665 if ((bma.logflags & xfs_ilog_fext(whichfork)) &&
4666 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
4667 bma.logflags &= ~xfs_ilog_fext(whichfork);
4668 else if ((bma.logflags & xfs_ilog_fbroot(whichfork)) &&
4669 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
4670 bma.logflags &= ~xfs_ilog_fbroot(whichfork);
4672 * Log whatever the flags say, even if error. Otherwise we might miss
4673 * detecting a case where the data is changed, there's an error,
4674 * and it's not logged so we don't shutdown when we should.
4677 xfs_trans_log_inode(tp, ip, bma.logflags);
4681 ASSERT(*firstblock == NULLFSBLOCK ||
4682 XFS_FSB_TO_AGNO(mp, *firstblock) ==
4684 bma.cur->bc_private.b.firstblock) ||
4686 XFS_FSB_TO_AGNO(mp, *firstblock) <
4688 bma.cur->bc_private.b.firstblock)));
4689 *firstblock = bma.cur->bc_private.b.firstblock;
4691 xfs_btree_del_cursor(bma.cur,
4692 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
4695 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
4701 * Called by xfs_bmapi to update file extent records and the btree
4702 * after removing space (or undoing a delayed allocation).
4704 STATIC int /* error */
4705 xfs_bmap_del_extent(
4706 xfs_inode_t *ip, /* incore inode pointer */
4707 xfs_trans_t *tp, /* current transaction pointer */
4708 xfs_extnum_t *idx, /* extent number to update/delete */
4709 xfs_bmap_free_t *flist, /* list of extents to be freed */
4710 xfs_btree_cur_t *cur, /* if null, not a btree */
4711 xfs_bmbt_irec_t *del, /* data to remove from extents */
4712 int *logflagsp, /* inode logging flags */
4713 int whichfork) /* data or attr fork */
4715 xfs_filblks_t da_new; /* new delay-alloc indirect blocks */
4716 xfs_filblks_t da_old; /* old delay-alloc indirect blocks */
4717 xfs_fsblock_t del_endblock=0; /* first block past del */
4718 xfs_fileoff_t del_endoff; /* first offset past del */
4719 int delay; /* current block is delayed allocated */
4720 int do_fx; /* free extent at end of routine */
4721 xfs_bmbt_rec_host_t *ep; /* current extent entry pointer */
4722 int error; /* error return value */
4723 int flags; /* inode logging flags */
4724 xfs_bmbt_irec_t got; /* current extent entry */
4725 xfs_fileoff_t got_endoff; /* first offset past got */
4726 int i; /* temp state */
4727 xfs_ifork_t *ifp; /* inode fork pointer */
4728 xfs_mount_t *mp; /* mount structure */
4729 xfs_filblks_t nblks; /* quota/sb block count */
4730 xfs_bmbt_irec_t new; /* new record to be inserted */
4732 uint qfield; /* quota field to update */
4733 xfs_filblks_t temp; /* for indirect length calculations */
4734 xfs_filblks_t temp2; /* for indirect length calculations */
4737 XFS_STATS_INC(xs_del_exlist);
4739 if (whichfork == XFS_ATTR_FORK)
4740 state |= BMAP_ATTRFORK;
4743 ifp = XFS_IFORK_PTR(ip, whichfork);
4744 ASSERT((*idx >= 0) && (*idx < ifp->if_bytes /
4745 (uint)sizeof(xfs_bmbt_rec_t)));
4746 ASSERT(del->br_blockcount > 0);
4747 ep = xfs_iext_get_ext(ifp, *idx);
4748 xfs_bmbt_get_all(ep, &got);
4749 ASSERT(got.br_startoff <= del->br_startoff);
4750 del_endoff = del->br_startoff + del->br_blockcount;
4751 got_endoff = got.br_startoff + got.br_blockcount;
4752 ASSERT(got_endoff >= del_endoff);
4753 delay = isnullstartblock(got.br_startblock);
4754 ASSERT(isnullstartblock(del->br_startblock) == delay);
4759 * If deleting a real allocation, must free up the disk space.
4762 flags = XFS_ILOG_CORE;
4764 * Realtime allocation. Free it and record di_nblocks update.
4766 if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) {
4770 ASSERT(do_mod(del->br_blockcount,
4771 mp->m_sb.sb_rextsize) == 0);
4772 ASSERT(do_mod(del->br_startblock,
4773 mp->m_sb.sb_rextsize) == 0);
4774 bno = del->br_startblock;
4775 len = del->br_blockcount;
4776 do_div(bno, mp->m_sb.sb_rextsize);
4777 do_div(len, mp->m_sb.sb_rextsize);
4778 error = xfs_rtfree_extent(tp, bno, (xfs_extlen_t)len);
4782 nblks = len * mp->m_sb.sb_rextsize;
4783 qfield = XFS_TRANS_DQ_RTBCOUNT;
4786 * Ordinary allocation.
4790 nblks = del->br_blockcount;
4791 qfield = XFS_TRANS_DQ_BCOUNT;
4794 * Set up del_endblock and cur for later.
4796 del_endblock = del->br_startblock + del->br_blockcount;
4798 if ((error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
4799 got.br_startblock, got.br_blockcount,
4802 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
4804 da_old = da_new = 0;
4806 da_old = startblockval(got.br_startblock);
4812 * Set flag value to use in switch statement.
4813 * Left-contig is 2, right-contig is 1.
4815 switch (((got.br_startoff == del->br_startoff) << 1) |
4816 (got_endoff == del_endoff)) {
4819 * Matches the whole extent. Delete the entry.
4821 xfs_iext_remove(ip, *idx, 1,
4822 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0);
4827 XFS_IFORK_NEXT_SET(ip, whichfork,
4828 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
4829 flags |= XFS_ILOG_CORE;
4831 flags |= xfs_ilog_fext(whichfork);
4834 if ((error = xfs_btree_delete(cur, &i)))
4836 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
4841 * Deleting the first part of the extent.
4843 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4844 xfs_bmbt_set_startoff(ep, del_endoff);
4845 temp = got.br_blockcount - del->br_blockcount;
4846 xfs_bmbt_set_blockcount(ep, temp);
4848 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
4850 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
4851 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
4855 xfs_bmbt_set_startblock(ep, del_endblock);
4856 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
4858 flags |= xfs_ilog_fext(whichfork);
4861 if ((error = xfs_bmbt_update(cur, del_endoff, del_endblock,
4862 got.br_blockcount - del->br_blockcount,
4869 * Deleting the last part of the extent.
4871 temp = got.br_blockcount - del->br_blockcount;
4872 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4873 xfs_bmbt_set_blockcount(ep, temp);
4875 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
4877 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
4878 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
4882 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
4884 flags |= xfs_ilog_fext(whichfork);
4887 if ((error = xfs_bmbt_update(cur, got.br_startoff,
4889 got.br_blockcount - del->br_blockcount,
4896 * Deleting the middle of the extent.
4898 temp = del->br_startoff - got.br_startoff;
4899 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4900 xfs_bmbt_set_blockcount(ep, temp);
4901 new.br_startoff = del_endoff;
4902 temp2 = got_endoff - del_endoff;
4903 new.br_blockcount = temp2;
4904 new.br_state = got.br_state;
4906 new.br_startblock = del_endblock;
4907 flags |= XFS_ILOG_CORE;
4909 if ((error = xfs_bmbt_update(cur,
4911 got.br_startblock, temp,
4914 if ((error = xfs_btree_increment(cur, 0, &i)))
4916 cur->bc_rec.b = new;
4917 error = xfs_btree_insert(cur, &i);
4918 if (error && error != -ENOSPC)
4921 * If get no-space back from btree insert,
4922 * it tried a split, and we have a zero
4923 * block reservation.
4924 * Fix up our state and return the error.
4926 if (error == -ENOSPC) {
4928 * Reset the cursor, don't trust
4929 * it after any insert operation.
4931 if ((error = xfs_bmbt_lookup_eq(cur,
4936 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
4938 * Update the btree record back
4939 * to the original value.
4941 if ((error = xfs_bmbt_update(cur,
4948 * Reset the extent record back
4949 * to the original value.
4951 xfs_bmbt_set_blockcount(ep,
4957 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
4959 flags |= xfs_ilog_fext(whichfork);
4960 XFS_IFORK_NEXT_SET(ip, whichfork,
4961 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
4963 ASSERT(whichfork == XFS_DATA_FORK);
4964 temp = xfs_bmap_worst_indlen(ip, temp);
4965 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
4966 temp2 = xfs_bmap_worst_indlen(ip, temp2);
4967 new.br_startblock = nullstartblock((int)temp2);
4968 da_new = temp + temp2;
4969 while (da_new > da_old) {
4973 xfs_bmbt_set_startblock(ep,
4974 nullstartblock((int)temp));
4976 if (da_new == da_old)
4982 nullstartblock((int)temp2);
4986 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
4987 xfs_iext_insert(ip, *idx + 1, 1, &new, state);
4992 * If we need to, add to list of extents to delete.
4995 xfs_bmap_add_free(del->br_startblock, del->br_blockcount, flist,
4998 * Adjust inode # blocks in the file.
5001 ip->i_d.di_nblocks -= nblks;
5003 * Adjust quota data.
5006 xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks);
5009 * Account for change in delayed indirect blocks.
5010 * Nothing to do for disk quota accounting here.
5012 ASSERT(da_old >= da_new);
5013 if (da_old > da_new) {
5014 xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS,
5015 (int64_t)(da_old - da_new), 0);
5023 * Unmap (remove) blocks from a file.
5024 * If nexts is nonzero then the number of extents to remove is limited to
5025 * that value. If not all extents in the block range can be removed then
5030 xfs_trans_t *tp, /* transaction pointer */
5031 struct xfs_inode *ip, /* incore inode */
5032 xfs_fileoff_t bno, /* starting offset to unmap */
5033 xfs_filblks_t len, /* length to unmap in file */
5034 int flags, /* misc flags */
5035 xfs_extnum_t nexts, /* number of extents max */
5036 xfs_fsblock_t *firstblock, /* first allocated block
5037 controls a.g. for allocs */
5038 xfs_bmap_free_t *flist, /* i/o: list extents to free */
5039 int *done) /* set if not done yet */
5041 xfs_btree_cur_t *cur; /* bmap btree cursor */
5042 xfs_bmbt_irec_t del; /* extent being deleted */
5043 int eof; /* is deleting at eof */
5044 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
5045 int error; /* error return value */
5046 xfs_extnum_t extno; /* extent number in list */
5047 xfs_bmbt_irec_t got; /* current extent record */
5048 xfs_ifork_t *ifp; /* inode fork pointer */
5049 int isrt; /* freeing in rt area */
5050 xfs_extnum_t lastx; /* last extent index used */
5051 int logflags; /* transaction logging flags */
5052 xfs_extlen_t mod; /* rt extent offset */
5053 xfs_mount_t *mp; /* mount structure */
5054 xfs_extnum_t nextents; /* number of file extents */
5055 xfs_bmbt_irec_t prev; /* previous extent record */
5056 xfs_fileoff_t start; /* first file offset deleted */
5057 int tmp_logflags; /* partial logging flags */
5058 int wasdel; /* was a delayed alloc extent */
5059 int whichfork; /* data or attribute fork */
5062 trace_xfs_bunmap(ip, bno, len, flags, _RET_IP_);
5064 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
5065 XFS_ATTR_FORK : XFS_DATA_FORK;
5066 ifp = XFS_IFORK_PTR(ip, whichfork);
5068 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5069 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
5070 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW,
5072 return -EFSCORRUPTED;
5075 if (XFS_FORCED_SHUTDOWN(mp))
5078 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
5082 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5083 (error = xfs_iread_extents(tp, ip, whichfork)))
5085 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5086 if (nextents == 0) {
5090 XFS_STATS_INC(xs_blk_unmap);
5091 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
5093 bno = start + len - 1;
5094 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
5098 * Check to see if the given block number is past the end of the
5099 * file, back up to the last block if so...
5102 ep = xfs_iext_get_ext(ifp, --lastx);
5103 xfs_bmbt_get_all(ep, &got);
5104 bno = got.br_startoff + got.br_blockcount - 1;
5107 if (ifp->if_flags & XFS_IFBROOT) {
5108 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
5109 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5110 cur->bc_private.b.firstblock = *firstblock;
5111 cur->bc_private.b.flist = flist;
5112 cur->bc_private.b.flags = 0;
5118 * Synchronize by locking the bitmap inode.
5120 xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL);
5121 xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL);
5125 while (bno != (xfs_fileoff_t)-1 && bno >= start && lastx >= 0 &&
5126 (nexts == 0 || extno < nexts)) {
5128 * Is the found extent after a hole in which bno lives?
5129 * Just back up to the previous extent, if so.
5131 if (got.br_startoff > bno) {
5134 ep = xfs_iext_get_ext(ifp, lastx);
5135 xfs_bmbt_get_all(ep, &got);
5138 * Is the last block of this extent before the range
5139 * we're supposed to delete? If so, we're done.
5141 bno = XFS_FILEOFF_MIN(bno,
5142 got.br_startoff + got.br_blockcount - 1);
5146 * Then deal with the (possibly delayed) allocated space
5151 wasdel = isnullstartblock(del.br_startblock);
5152 if (got.br_startoff < start) {
5153 del.br_startoff = start;
5154 del.br_blockcount -= start - got.br_startoff;
5156 del.br_startblock += start - got.br_startoff;
5158 if (del.br_startoff + del.br_blockcount > bno + 1)
5159 del.br_blockcount = bno + 1 - del.br_startoff;
5160 sum = del.br_startblock + del.br_blockcount;
5162 (mod = do_mod(sum, mp->m_sb.sb_rextsize))) {
5164 * Realtime extent not lined up at the end.
5165 * The extent could have been split into written
5166 * and unwritten pieces, or we could just be
5167 * unmapping part of it. But we can't really
5168 * get rid of part of a realtime extent.
5170 if (del.br_state == XFS_EXT_UNWRITTEN ||
5171 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
5173 * This piece is unwritten, or we're not
5174 * using unwritten extents. Skip over it.
5177 bno -= mod > del.br_blockcount ?
5178 del.br_blockcount : mod;
5179 if (bno < got.br_startoff) {
5181 xfs_bmbt_get_all(xfs_iext_get_ext(
5187 * It's written, turn it unwritten.
5188 * This is better than zeroing it.
5190 ASSERT(del.br_state == XFS_EXT_NORM);
5191 ASSERT(xfs_trans_get_block_res(tp) > 0);
5193 * If this spans a realtime extent boundary,
5194 * chop it back to the start of the one we end at.
5196 if (del.br_blockcount > mod) {
5197 del.br_startoff += del.br_blockcount - mod;
5198 del.br_startblock += del.br_blockcount - mod;
5199 del.br_blockcount = mod;
5201 del.br_state = XFS_EXT_UNWRITTEN;
5202 error = xfs_bmap_add_extent_unwritten_real(tp, ip,
5203 &lastx, &cur, &del, firstblock, flist,
5209 if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) {
5211 * Realtime extent is lined up at the end but not
5212 * at the front. We'll get rid of full extents if
5215 mod = mp->m_sb.sb_rextsize - mod;
5216 if (del.br_blockcount > mod) {
5217 del.br_blockcount -= mod;
5218 del.br_startoff += mod;
5219 del.br_startblock += mod;
5220 } else if ((del.br_startoff == start &&
5221 (del.br_state == XFS_EXT_UNWRITTEN ||
5222 xfs_trans_get_block_res(tp) == 0)) ||
5223 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
5225 * Can't make it unwritten. There isn't
5226 * a full extent here so just skip it.
5228 ASSERT(bno >= del.br_blockcount);
5229 bno -= del.br_blockcount;
5230 if (got.br_startoff > bno) {
5232 ep = xfs_iext_get_ext(ifp,
5234 xfs_bmbt_get_all(ep, &got);
5238 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
5240 * This one is already unwritten.
5241 * It must have a written left neighbor.
5242 * Unwrite the killed part of that one and
5246 xfs_bmbt_get_all(xfs_iext_get_ext(ifp,
5248 ASSERT(prev.br_state == XFS_EXT_NORM);
5249 ASSERT(!isnullstartblock(prev.br_startblock));
5250 ASSERT(del.br_startblock ==
5251 prev.br_startblock + prev.br_blockcount);
5252 if (prev.br_startoff < start) {
5253 mod = start - prev.br_startoff;
5254 prev.br_blockcount -= mod;
5255 prev.br_startblock += mod;
5256 prev.br_startoff = start;
5258 prev.br_state = XFS_EXT_UNWRITTEN;
5260 error = xfs_bmap_add_extent_unwritten_real(tp,
5261 ip, &lastx, &cur, &prev,
5262 firstblock, flist, &logflags);
5267 ASSERT(del.br_state == XFS_EXT_NORM);
5268 del.br_state = XFS_EXT_UNWRITTEN;
5269 error = xfs_bmap_add_extent_unwritten_real(tp,
5270 ip, &lastx, &cur, &del,
5271 firstblock, flist, &logflags);
5278 ASSERT(startblockval(del.br_startblock) > 0);
5279 /* Update realtime/data freespace, unreserve quota */
5281 xfs_filblks_t rtexts;
5283 rtexts = XFS_FSB_TO_B(mp, del.br_blockcount);
5284 do_div(rtexts, mp->m_sb.sb_rextsize);
5285 xfs_mod_incore_sb(mp, XFS_SBS_FREXTENTS,
5286 (int64_t)rtexts, 0);
5287 (void)xfs_trans_reserve_quota_nblks(NULL,
5288 ip, -((long)del.br_blockcount), 0,
5289 XFS_QMOPT_RES_RTBLKS);
5291 xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS,
5292 (int64_t)del.br_blockcount, 0);
5293 (void)xfs_trans_reserve_quota_nblks(NULL,
5294 ip, -((long)del.br_blockcount), 0,
5295 XFS_QMOPT_RES_REGBLKS);
5297 ip->i_delayed_blks -= del.br_blockcount;
5299 cur->bc_private.b.flags |=
5300 XFS_BTCUR_BPRV_WASDEL;
5302 cur->bc_private.b.flags &= ~XFS_BTCUR_BPRV_WASDEL;
5304 * If it's the case where the directory code is running
5305 * with no block reservation, and the deleted block is in
5306 * the middle of its extent, and the resulting insert
5307 * of an extent would cause transformation to btree format,
5308 * then reject it. The calling code will then swap
5309 * blocks around instead.
5310 * We have to do this now, rather than waiting for the
5311 * conversion to btree format, since the transaction
5314 if (!wasdel && xfs_trans_get_block_res(tp) == 0 &&
5315 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
5316 XFS_IFORK_NEXTENTS(ip, whichfork) >= /* Note the >= */
5317 XFS_IFORK_MAXEXT(ip, whichfork) &&
5318 del.br_startoff > got.br_startoff &&
5319 del.br_startoff + del.br_blockcount <
5320 got.br_startoff + got.br_blockcount) {
5324 error = xfs_bmap_del_extent(ip, tp, &lastx, flist, cur, &del,
5325 &tmp_logflags, whichfork);
5326 logflags |= tmp_logflags;
5329 bno = del.br_startoff - 1;
5332 * If not done go on to the next (previous) record.
5334 if (bno != (xfs_fileoff_t)-1 && bno >= start) {
5336 ep = xfs_iext_get_ext(ifp, lastx);
5337 if (xfs_bmbt_get_startoff(ep) > bno) {
5339 ep = xfs_iext_get_ext(ifp,
5342 xfs_bmbt_get_all(ep, &got);
5347 *done = bno == (xfs_fileoff_t)-1 || bno < start || lastx < 0;
5350 * Convert to a btree if necessary.
5352 if (xfs_bmap_needs_btree(ip, whichfork)) {
5353 ASSERT(cur == NULL);
5354 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist,
5355 &cur, 0, &tmp_logflags, whichfork);
5356 logflags |= tmp_logflags;
5361 * transform from btree to extents, give it cur
5363 else if (xfs_bmap_wants_extents(ip, whichfork)) {
5364 ASSERT(cur != NULL);
5365 error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags,
5367 logflags |= tmp_logflags;
5372 * transform from extents to local?
5377 * Log everything. Do this after conversion, there's no point in
5378 * logging the extent records if we've converted to btree format.
5380 if ((logflags & xfs_ilog_fext(whichfork)) &&
5381 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
5382 logflags &= ~xfs_ilog_fext(whichfork);
5383 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
5384 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
5385 logflags &= ~xfs_ilog_fbroot(whichfork);
5387 * Log inode even in the error case, if the transaction
5388 * is dirty we'll need to shut down the filesystem.
5391 xfs_trans_log_inode(tp, ip, logflags);
5394 *firstblock = cur->bc_private.b.firstblock;
5395 cur->bc_private.b.allocated = 0;
5397 xfs_btree_del_cursor(cur,
5398 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5404 * Determine whether an extent shift can be accomplished by a merge with the
5405 * extent that precedes the target hole of the shift.
5409 struct xfs_bmbt_irec *left, /* preceding extent */
5410 struct xfs_bmbt_irec *got, /* current extent to shift */
5411 xfs_fileoff_t shift) /* shift fsb */
5413 xfs_fileoff_t startoff;
5415 startoff = got->br_startoff - shift;
5418 * The extent, once shifted, must be adjacent in-file and on-disk with
5419 * the preceding extent.
5421 if ((left->br_startoff + left->br_blockcount != startoff) ||
5422 (left->br_startblock + left->br_blockcount != got->br_startblock) ||
5423 (left->br_state != got->br_state) ||
5424 (left->br_blockcount + got->br_blockcount > MAXEXTLEN))
5431 * A bmap extent shift adjusts the file offset of an extent to fill a preceding
5432 * hole in the file. If an extent shift would result in the extent being fully
5433 * adjacent to the extent that currently precedes the hole, we can merge with
5434 * the preceding extent rather than do the shift.
5436 * This function assumes the caller has verified a shift-by-merge is possible
5437 * with the provided extents via xfs_bmse_can_merge().
5441 struct xfs_inode *ip,
5443 xfs_fileoff_t shift, /* shift fsb */
5444 int current_ext, /* idx of gotp */
5445 struct xfs_bmbt_rec_host *gotp, /* extent to shift */
5446 struct xfs_bmbt_rec_host *leftp, /* preceding extent */
5447 struct xfs_btree_cur *cur,
5448 int *logflags) /* output */
5450 struct xfs_bmbt_irec got;
5451 struct xfs_bmbt_irec left;
5452 xfs_filblks_t blockcount;
5455 xfs_bmbt_get_all(gotp, &got);
5456 xfs_bmbt_get_all(leftp, &left);
5457 blockcount = left.br_blockcount + got.br_blockcount;
5459 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
5460 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
5461 ASSERT(xfs_bmse_can_merge(&left, &got, shift));
5464 * Merge the in-core extents. Note that the host record pointers and
5465 * current_ext index are invalid once the extent has been removed via
5466 * xfs_iext_remove().
5468 xfs_bmbt_set_blockcount(leftp, blockcount);
5469 xfs_iext_remove(ip, current_ext, 1, 0);
5472 * Update the on-disk extent count, the btree if necessary and log the
5475 XFS_IFORK_NEXT_SET(ip, whichfork,
5476 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
5477 *logflags |= XFS_ILOG_CORE;
5479 *logflags |= XFS_ILOG_DEXT;
5483 /* lookup and remove the extent to merge */
5484 error = xfs_bmbt_lookup_eq(cur, got.br_startoff, got.br_startblock,
5485 got.br_blockcount, &i);
5488 XFS_WANT_CORRUPTED_RETURN(i == 1);
5490 error = xfs_btree_delete(cur, &i);
5493 XFS_WANT_CORRUPTED_RETURN(i == 1);
5495 /* lookup and update size of the previous extent */
5496 error = xfs_bmbt_lookup_eq(cur, left.br_startoff, left.br_startblock,
5497 left.br_blockcount, &i);
5500 XFS_WANT_CORRUPTED_RETURN(i == 1);
5502 left.br_blockcount = blockcount;
5504 return xfs_bmbt_update(cur, left.br_startoff, left.br_startblock,
5505 left.br_blockcount, left.br_state);
5509 * Shift a single extent.
5513 struct xfs_inode *ip,
5515 xfs_fileoff_t offset_shift_fsb,
5517 struct xfs_bmbt_rec_host *gotp,
5518 struct xfs_btree_cur *cur,
5521 struct xfs_ifork *ifp;
5522 xfs_fileoff_t startoff;
5523 struct xfs_bmbt_rec_host *leftp;
5524 struct xfs_bmbt_irec got;
5525 struct xfs_bmbt_irec left;
5529 ifp = XFS_IFORK_PTR(ip, whichfork);
5531 xfs_bmbt_get_all(gotp, &got);
5532 startoff = got.br_startoff - offset_shift_fsb;
5534 /* delalloc extents should be prevented by caller */
5535 XFS_WANT_CORRUPTED_RETURN(!isnullstartblock(got.br_startblock));
5538 * Check for merge if we've got an extent to the left, otherwise make
5539 * sure there's enough room at the start of the file for the shift.
5542 /* grab the left extent and check for a large enough hole */
5543 leftp = xfs_iext_get_ext(ifp, *current_ext - 1);
5544 xfs_bmbt_get_all(leftp, &left);
5546 if (startoff < left.br_startoff + left.br_blockcount)
5549 /* check whether to merge the extent or shift it down */
5550 if (xfs_bmse_can_merge(&left, &got, offset_shift_fsb)) {
5551 return xfs_bmse_merge(ip, whichfork, offset_shift_fsb,
5552 *current_ext, gotp, leftp, cur,
5555 } else if (got.br_startoff < offset_shift_fsb)
5559 * Increment the extent index for the next iteration, update the start
5560 * offset of the in-core extent and update the btree if applicable.
5563 xfs_bmbt_set_startoff(gotp, startoff);
5564 *logflags |= XFS_ILOG_CORE;
5566 *logflags |= XFS_ILOG_DEXT;
5570 error = xfs_bmbt_lookup_eq(cur, got.br_startoff, got.br_startblock,
5571 got.br_blockcount, &i);
5574 XFS_WANT_CORRUPTED_RETURN(i == 1);
5576 got.br_startoff = startoff;
5577 return xfs_bmbt_update(cur, got.br_startoff, got.br_startblock,
5578 got.br_blockcount, got.br_state);
5582 * Shift extent records to the left to cover a hole.
5584 * The maximum number of extents to be shifted in a single operation is
5585 * @num_exts. @start_fsb specifies the file offset to start the shift and the
5586 * file offset where we've left off is returned in @next_fsb. @offset_shift_fsb
5587 * is the length by which each extent is shifted. If there is no hole to shift
5588 * the extents into, this will be considered invalid operation and we abort
5592 xfs_bmap_shift_extents(
5593 struct xfs_trans *tp,
5594 struct xfs_inode *ip,
5595 xfs_fileoff_t start_fsb,
5596 xfs_fileoff_t offset_shift_fsb,
5598 xfs_fileoff_t *next_fsb,
5599 xfs_fsblock_t *firstblock,
5600 struct xfs_bmap_free *flist,
5603 struct xfs_btree_cur *cur = NULL;
5604 struct xfs_bmbt_rec_host *gotp;
5605 struct xfs_bmbt_irec got;
5606 struct xfs_mount *mp = ip->i_mount;
5607 struct xfs_ifork *ifp;
5608 xfs_extnum_t nexts = 0;
5609 xfs_extnum_t current_ext;
5611 int whichfork = XFS_DATA_FORK;
5615 if (unlikely(XFS_TEST_ERROR(
5616 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5617 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
5618 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
5619 XFS_ERROR_REPORT("xfs_bmap_shift_extents",
5620 XFS_ERRLEVEL_LOW, mp);
5621 return -EFSCORRUPTED;
5624 if (XFS_FORCED_SHUTDOWN(mp))
5627 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
5628 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
5630 ifp = XFS_IFORK_PTR(ip, whichfork);
5631 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
5632 /* Read in all the extents */
5633 error = xfs_iread_extents(tp, ip, whichfork);
5638 if (ifp->if_flags & XFS_IFBROOT) {
5639 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5640 cur->bc_private.b.firstblock = *firstblock;
5641 cur->bc_private.b.flist = flist;
5642 cur->bc_private.b.flags = 0;
5646 * Look up the extent index for the fsb where we start shifting. We can
5647 * henceforth iterate with current_ext as extent list changes are locked
5650 * gotp can be null in 2 cases: 1) if there are no extents or 2)
5651 * start_fsb lies in a hole beyond which there are no extents. Either
5654 gotp = xfs_iext_bno_to_ext(ifp, start_fsb, ¤t_ext);
5661 * There may be delalloc extents in the data fork before the range we
5662 * are collapsing out, so we cannot use the count of real extents here.
5663 * Instead we have to calculate it from the incore fork.
5665 total_extents = ifp->if_bytes / sizeof(xfs_bmbt_rec_t);
5666 while (nexts++ < num_exts && current_ext < total_extents) {
5667 error = xfs_bmse_shift_one(ip, whichfork, offset_shift_fsb,
5668 ¤t_ext, gotp, cur, &logflags);
5672 /* update total extent count and grab the next record */
5673 total_extents = ifp->if_bytes / sizeof(xfs_bmbt_rec_t);
5674 if (current_ext >= total_extents)
5676 gotp = xfs_iext_get_ext(ifp, current_ext);
5679 /* Check if we are done */
5680 if (current_ext == total_extents) {
5682 } else if (next_fsb) {
5683 xfs_bmbt_get_all(gotp, &got);
5684 *next_fsb = got.br_startoff;
5689 xfs_btree_del_cursor(cur,
5690 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5693 xfs_trans_log_inode(tp, ip, logflags);