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 STATIC struct xfs_buf *
250 struct xfs_btree_cur *cur,
253 struct xfs_log_item_desc *lidp;
259 for (i = 0; i < XFS_BTREE_MAXLEVELS; i++) {
260 if (!cur->bc_bufs[i])
262 if (XFS_BUF_ADDR(cur->bc_bufs[i]) == bno)
263 return cur->bc_bufs[i];
266 /* Chase down all the log items to see if the bp is there */
267 list_for_each_entry(lidp, &cur->bc_tp->t_items, lid_trans) {
268 struct xfs_buf_log_item *bip;
269 bip = (struct xfs_buf_log_item *)lidp->lid_item;
270 if (bip->bli_item.li_type == XFS_LI_BUF &&
271 XFS_BUF_ADDR(bip->bli_buf) == bno)
280 struct xfs_btree_block *block,
286 __be64 *pp, *thispa; /* pointer to block address */
287 xfs_bmbt_key_t *prevp, *keyp;
289 ASSERT(be16_to_cpu(block->bb_level) > 0);
292 for( i = 1; i <= xfs_btree_get_numrecs(block); i++) {
293 dmxr = mp->m_bmap_dmxr[0];
294 keyp = XFS_BMBT_KEY_ADDR(mp, block, i);
297 ASSERT(be64_to_cpu(prevp->br_startoff) <
298 be64_to_cpu(keyp->br_startoff));
303 * Compare the block numbers to see if there are dups.
306 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, i, sz);
308 pp = XFS_BMBT_PTR_ADDR(mp, block, i, dmxr);
310 for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) {
312 thispa = XFS_BMAP_BROOT_PTR_ADDR(mp, block, j, sz);
314 thispa = XFS_BMBT_PTR_ADDR(mp, block, j, dmxr);
315 if (*thispa == *pp) {
316 xfs_warn(mp, "%s: thispa(%d) == pp(%d) %Ld",
318 (unsigned long long)be64_to_cpu(*thispa));
319 panic("%s: ptrs are equal in node\n",
327 * Check that the extents for the inode ip are in the right order in all
332 xfs_bmap_check_leaf_extents(
333 xfs_btree_cur_t *cur, /* btree cursor or null */
334 xfs_inode_t *ip, /* incore inode pointer */
335 int whichfork) /* data or attr fork */
337 struct xfs_btree_block *block; /* current btree block */
338 xfs_fsblock_t bno; /* block # of "block" */
339 xfs_buf_t *bp; /* buffer for "block" */
340 int error; /* error return value */
341 xfs_extnum_t i=0, j; /* index into the extents list */
342 xfs_ifork_t *ifp; /* fork structure */
343 int level; /* btree level, for checking */
344 xfs_mount_t *mp; /* file system mount structure */
345 __be64 *pp; /* pointer to block address */
346 xfs_bmbt_rec_t *ep; /* pointer to current extent */
347 xfs_bmbt_rec_t last = {0, 0}; /* last extent in prev block */
348 xfs_bmbt_rec_t *nextp; /* pointer to next extent */
351 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) {
357 ifp = XFS_IFORK_PTR(ip, whichfork);
358 block = ifp->if_broot;
360 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
362 level = be16_to_cpu(block->bb_level);
364 xfs_check_block(block, mp, 1, ifp->if_broot_bytes);
365 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
366 bno = be64_to_cpu(*pp);
368 ASSERT(bno != NULLFSBLOCK);
369 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
370 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
373 * Go down the tree until leaf level is reached, following the first
374 * pointer (leftmost) at each level.
376 while (level-- > 0) {
377 /* See if buf is in cur first */
379 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
382 error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
388 block = XFS_BUF_TO_BLOCK(bp);
393 * Check this block for basic sanity (increasing keys and
394 * no duplicate blocks).
397 xfs_check_block(block, mp, 0, 0);
398 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
399 bno = be64_to_cpu(*pp);
400 XFS_WANT_CORRUPTED_GOTO(mp,
401 XFS_FSB_SANITY_CHECK(mp, bno), error0);
404 xfs_trans_brelse(NULL, bp);
409 * Here with bp and block set to the leftmost leaf node in the tree.
414 * Loop over all leaf nodes checking that all extents are in the right order.
417 xfs_fsblock_t nextbno;
418 xfs_extnum_t num_recs;
421 num_recs = xfs_btree_get_numrecs(block);
424 * Read-ahead the next leaf block, if any.
427 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
430 * Check all the extents to make sure they are OK.
431 * If we had a previous block, the last entry should
432 * conform with the first entry in this one.
435 ep = XFS_BMBT_REC_ADDR(mp, block, 1);
437 ASSERT(xfs_bmbt_disk_get_startoff(&last) +
438 xfs_bmbt_disk_get_blockcount(&last) <=
439 xfs_bmbt_disk_get_startoff(ep));
441 for (j = 1; j < num_recs; j++) {
442 nextp = XFS_BMBT_REC_ADDR(mp, block, j + 1);
443 ASSERT(xfs_bmbt_disk_get_startoff(ep) +
444 xfs_bmbt_disk_get_blockcount(ep) <=
445 xfs_bmbt_disk_get_startoff(nextp));
453 xfs_trans_brelse(NULL, bp);
457 * If we've reached the end, stop.
459 if (bno == NULLFSBLOCK)
463 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
466 error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
472 block = XFS_BUF_TO_BLOCK(bp);
476 xfs_trans_brelse(NULL, bp);
481 xfs_warn(mp, "%s: at error0", __func__);
483 xfs_trans_brelse(NULL, bp);
485 xfs_warn(mp, "%s: BAD after btree leaves for %d extents",
487 panic("%s: CORRUPTED BTREE OR SOMETHING", __func__);
492 * Add bmap trace insert entries for all the contents of the extent records.
495 xfs_bmap_trace_exlist(
496 xfs_inode_t *ip, /* incore inode pointer */
497 xfs_extnum_t cnt, /* count of entries in the list */
498 int whichfork, /* data or attr fork */
499 unsigned long caller_ip)
501 xfs_extnum_t idx; /* extent record index */
502 xfs_ifork_t *ifp; /* inode fork pointer */
505 if (whichfork == XFS_ATTR_FORK)
506 state |= BMAP_ATTRFORK;
508 ifp = XFS_IFORK_PTR(ip, whichfork);
509 ASSERT(cnt == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
510 for (idx = 0; idx < cnt; idx++)
511 trace_xfs_extlist(ip, idx, whichfork, caller_ip);
515 * Validate that the bmbt_irecs being returned from bmapi are valid
516 * given the caller's original parameters. Specifically check the
517 * ranges of the returned irecs to ensure that they only extend beyond
518 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
521 xfs_bmap_validate_ret(
525 xfs_bmbt_irec_t *mval,
529 int i; /* index to map values */
531 ASSERT(ret_nmap <= nmap);
533 for (i = 0; i < ret_nmap; i++) {
534 ASSERT(mval[i].br_blockcount > 0);
535 if (!(flags & XFS_BMAPI_ENTIRE)) {
536 ASSERT(mval[i].br_startoff >= bno);
537 ASSERT(mval[i].br_blockcount <= len);
538 ASSERT(mval[i].br_startoff + mval[i].br_blockcount <=
541 ASSERT(mval[i].br_startoff < bno + len);
542 ASSERT(mval[i].br_startoff + mval[i].br_blockcount >
546 mval[i - 1].br_startoff + mval[i - 1].br_blockcount ==
547 mval[i].br_startoff);
548 ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK &&
549 mval[i].br_startblock != HOLESTARTBLOCK);
550 ASSERT(mval[i].br_state == XFS_EXT_NORM ||
551 mval[i].br_state == XFS_EXT_UNWRITTEN);
556 #define xfs_bmap_check_leaf_extents(cur, ip, whichfork) do { } while (0)
557 #define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap)
561 * bmap free list manipulation functions
565 * Add the extent to the list of extents to be free at transaction end.
566 * The list is maintained sorted (by block number).
570 xfs_fsblock_t bno, /* fs block number of extent */
571 xfs_filblks_t len, /* length of extent */
572 xfs_bmap_free_t *flist, /* list of extents */
573 xfs_mount_t *mp) /* mount point structure */
575 xfs_bmap_free_item_t *cur; /* current (next) element */
576 xfs_bmap_free_item_t *new; /* new element */
577 xfs_bmap_free_item_t *prev; /* previous element */
582 ASSERT(bno != NULLFSBLOCK);
584 ASSERT(len <= MAXEXTLEN);
585 ASSERT(!isnullstartblock(bno));
586 agno = XFS_FSB_TO_AGNO(mp, bno);
587 agbno = XFS_FSB_TO_AGBNO(mp, bno);
588 ASSERT(agno < mp->m_sb.sb_agcount);
589 ASSERT(agbno < mp->m_sb.sb_agblocks);
590 ASSERT(len < mp->m_sb.sb_agblocks);
591 ASSERT(agbno + len <= mp->m_sb.sb_agblocks);
593 ASSERT(xfs_bmap_free_item_zone != NULL);
594 new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP);
595 new->xbfi_startblock = bno;
596 new->xbfi_blockcount = (xfs_extlen_t)len;
597 for (prev = NULL, cur = flist->xbf_first;
599 prev = cur, cur = cur->xbfi_next) {
600 if (cur->xbfi_startblock >= bno)
604 prev->xbfi_next = new;
606 flist->xbf_first = new;
607 new->xbfi_next = cur;
612 * Remove the entry "free" from the free item list. Prev points to the
613 * previous entry, unless "free" is the head of the list.
617 xfs_bmap_free_t *flist, /* free item list header */
618 xfs_bmap_free_item_t *prev, /* previous item on list, if any */
619 xfs_bmap_free_item_t *free) /* list item to be freed */
622 prev->xbfi_next = free->xbfi_next;
624 flist->xbf_first = free->xbfi_next;
626 kmem_zone_free(xfs_bmap_free_item_zone, free);
630 * Free up any items left in the list.
634 xfs_bmap_free_t *flist) /* list of bmap_free_items */
636 xfs_bmap_free_item_t *free; /* free list item */
637 xfs_bmap_free_item_t *next;
639 if (flist->xbf_count == 0)
641 ASSERT(flist->xbf_first != NULL);
642 for (free = flist->xbf_first; free; free = next) {
643 next = free->xbfi_next;
644 xfs_bmap_del_free(flist, NULL, free);
646 ASSERT(flist->xbf_count == 0);
650 * Inode fork format manipulation functions
654 * Transform a btree format file with only one leaf node, where the
655 * extents list will fit in the inode, into an extents format file.
656 * Since the file extents are already in-core, all we have to do is
657 * give up the space for the btree root and pitch the leaf block.
659 STATIC int /* error */
660 xfs_bmap_btree_to_extents(
661 xfs_trans_t *tp, /* transaction pointer */
662 xfs_inode_t *ip, /* incore inode pointer */
663 xfs_btree_cur_t *cur, /* btree cursor */
664 int *logflagsp, /* inode logging flags */
665 int whichfork) /* data or attr fork */
668 struct xfs_btree_block *cblock;/* child btree block */
669 xfs_fsblock_t cbno; /* child block number */
670 xfs_buf_t *cbp; /* child block's buffer */
671 int error; /* error return value */
672 xfs_ifork_t *ifp; /* inode fork data */
673 xfs_mount_t *mp; /* mount point structure */
674 __be64 *pp; /* ptr to block address */
675 struct xfs_btree_block *rblock;/* root btree block */
678 ifp = XFS_IFORK_PTR(ip, whichfork);
679 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
680 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
681 rblock = ifp->if_broot;
682 ASSERT(be16_to_cpu(rblock->bb_level) == 1);
683 ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1);
684 ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0) == 1);
685 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, ifp->if_broot_bytes);
686 cbno = be64_to_cpu(*pp);
689 if ((error = xfs_btree_check_lptr(cur, cbno, 1)))
692 error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp, XFS_BMAP_BTREE_REF,
696 cblock = XFS_BUF_TO_BLOCK(cbp);
697 if ((error = xfs_btree_check_block(cur, cblock, 0, cbp)))
699 xfs_bmap_add_free(cbno, 1, cur->bc_private.b.flist, mp);
700 ip->i_d.di_nblocks--;
701 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
702 xfs_trans_binval(tp, cbp);
703 if (cur->bc_bufs[0] == cbp)
704 cur->bc_bufs[0] = NULL;
705 xfs_iroot_realloc(ip, -1, whichfork);
706 ASSERT(ifp->if_broot == NULL);
707 ASSERT((ifp->if_flags & XFS_IFBROOT) == 0);
708 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
709 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
714 * Convert an extents-format file into a btree-format file.
715 * The new file will have a root block (in the inode) and a single child block.
717 STATIC int /* error */
718 xfs_bmap_extents_to_btree(
719 xfs_trans_t *tp, /* transaction pointer */
720 xfs_inode_t *ip, /* incore inode pointer */
721 xfs_fsblock_t *firstblock, /* first-block-allocated */
722 xfs_bmap_free_t *flist, /* blocks freed in xaction */
723 xfs_btree_cur_t **curp, /* cursor returned to caller */
724 int wasdel, /* converting a delayed alloc */
725 int *logflagsp, /* inode logging flags */
726 int whichfork) /* data or attr fork */
728 struct xfs_btree_block *ablock; /* allocated (child) bt block */
729 xfs_buf_t *abp; /* buffer for ablock */
730 xfs_alloc_arg_t args; /* allocation arguments */
731 xfs_bmbt_rec_t *arp; /* child record pointer */
732 struct xfs_btree_block *block; /* btree root block */
733 xfs_btree_cur_t *cur; /* bmap btree cursor */
734 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
735 int error; /* error return value */
736 xfs_extnum_t i, cnt; /* extent record index */
737 xfs_ifork_t *ifp; /* inode fork pointer */
738 xfs_bmbt_key_t *kp; /* root block key pointer */
739 xfs_mount_t *mp; /* mount structure */
740 xfs_extnum_t nextents; /* number of file extents */
741 xfs_bmbt_ptr_t *pp; /* root block address pointer */
744 ifp = XFS_IFORK_PTR(ip, whichfork);
745 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS);
748 * Make space in the inode incore.
750 xfs_iroot_realloc(ip, 1, whichfork);
751 ifp->if_flags |= XFS_IFBROOT;
756 block = ifp->if_broot;
757 if (xfs_sb_version_hascrc(&mp->m_sb))
758 xfs_btree_init_block_int(mp, block, XFS_BUF_DADDR_NULL,
759 XFS_BMAP_CRC_MAGIC, 1, 1, ip->i_ino,
760 XFS_BTREE_LONG_PTRS | XFS_BTREE_CRC_BLOCKS);
762 xfs_btree_init_block_int(mp, block, XFS_BUF_DADDR_NULL,
763 XFS_BMAP_MAGIC, 1, 1, ip->i_ino,
764 XFS_BTREE_LONG_PTRS);
767 * Need a cursor. Can't allocate until bb_level is filled in.
769 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
770 cur->bc_private.b.firstblock = *firstblock;
771 cur->bc_private.b.flist = flist;
772 cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
774 * Convert to a btree with two levels, one record in root.
776 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE);
777 memset(&args, 0, sizeof(args));
780 args.firstblock = *firstblock;
781 if (*firstblock == NULLFSBLOCK) {
782 args.type = XFS_ALLOCTYPE_START_BNO;
783 args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino);
784 } else if (flist->xbf_low) {
785 args.type = XFS_ALLOCTYPE_START_BNO;
786 args.fsbno = *firstblock;
788 args.type = XFS_ALLOCTYPE_NEAR_BNO;
789 args.fsbno = *firstblock;
791 args.minlen = args.maxlen = args.prod = 1;
792 args.wasdel = wasdel;
794 if ((error = xfs_alloc_vextent(&args))) {
795 xfs_iroot_realloc(ip, -1, whichfork);
796 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
800 * Allocation can't fail, the space was reserved.
802 ASSERT(args.fsbno != NULLFSBLOCK);
803 ASSERT(*firstblock == NULLFSBLOCK ||
804 args.agno == XFS_FSB_TO_AGNO(mp, *firstblock) ||
806 args.agno > XFS_FSB_TO_AGNO(mp, *firstblock)));
807 *firstblock = cur->bc_private.b.firstblock = args.fsbno;
808 cur->bc_private.b.allocated++;
809 ip->i_d.di_nblocks++;
810 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
811 abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0);
813 * Fill in the child block.
815 abp->b_ops = &xfs_bmbt_buf_ops;
816 ablock = XFS_BUF_TO_BLOCK(abp);
817 if (xfs_sb_version_hascrc(&mp->m_sb))
818 xfs_btree_init_block_int(mp, ablock, abp->b_bn,
819 XFS_BMAP_CRC_MAGIC, 0, 0, ip->i_ino,
820 XFS_BTREE_LONG_PTRS | XFS_BTREE_CRC_BLOCKS);
822 xfs_btree_init_block_int(mp, ablock, abp->b_bn,
823 XFS_BMAP_MAGIC, 0, 0, ip->i_ino,
824 XFS_BTREE_LONG_PTRS);
826 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
827 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
828 for (cnt = i = 0; i < nextents; i++) {
829 ep = xfs_iext_get_ext(ifp, i);
830 if (!isnullstartblock(xfs_bmbt_get_startblock(ep))) {
831 arp->l0 = cpu_to_be64(ep->l0);
832 arp->l1 = cpu_to_be64(ep->l1);
836 ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork));
837 xfs_btree_set_numrecs(ablock, cnt);
840 * Fill in the root key and pointer.
842 kp = XFS_BMBT_KEY_ADDR(mp, block, 1);
843 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
844 kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp));
845 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur,
846 be16_to_cpu(block->bb_level)));
847 *pp = cpu_to_be64(args.fsbno);
850 * Do all this logging at the end so that
851 * the root is at the right level.
853 xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS);
854 xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs));
855 ASSERT(*curp == NULL);
857 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork);
862 * Convert a local file to an extents file.
863 * This code is out of bounds for data forks of regular files,
864 * since the file data needs to get logged so things will stay consistent.
865 * (The bmap-level manipulations are ok, though).
868 xfs_bmap_local_to_extents_empty(
869 struct xfs_inode *ip,
872 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
874 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
875 ASSERT(ifp->if_bytes == 0);
876 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0);
878 xfs_bmap_forkoff_reset(ip, whichfork);
879 ifp->if_flags &= ~XFS_IFINLINE;
880 ifp->if_flags |= XFS_IFEXTENTS;
881 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
885 STATIC int /* error */
886 xfs_bmap_local_to_extents(
887 xfs_trans_t *tp, /* transaction pointer */
888 xfs_inode_t *ip, /* incore inode pointer */
889 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
890 xfs_extlen_t total, /* total blocks needed by transaction */
891 int *logflagsp, /* inode logging flags */
893 void (*init_fn)(struct xfs_trans *tp,
895 struct xfs_inode *ip,
896 struct xfs_ifork *ifp))
899 int flags; /* logging flags returned */
900 xfs_ifork_t *ifp; /* inode fork pointer */
901 xfs_alloc_arg_t args; /* allocation arguments */
902 xfs_buf_t *bp; /* buffer for extent block */
903 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
906 * We don't want to deal with the case of keeping inode data inline yet.
907 * So sending the data fork of a regular inode is invalid.
909 ASSERT(!(S_ISREG(ip->i_d.di_mode) && whichfork == XFS_DATA_FORK));
910 ifp = XFS_IFORK_PTR(ip, whichfork);
911 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
913 if (!ifp->if_bytes) {
914 xfs_bmap_local_to_extents_empty(ip, whichfork);
915 flags = XFS_ILOG_CORE;
921 ASSERT((ifp->if_flags & (XFS_IFINLINE|XFS_IFEXTENTS|XFS_IFEXTIREC)) ==
923 memset(&args, 0, sizeof(args));
925 args.mp = ip->i_mount;
926 args.firstblock = *firstblock;
928 * Allocate a block. We know we need only one, since the
929 * file currently fits in an inode.
931 if (*firstblock == NULLFSBLOCK) {
932 args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino);
933 args.type = XFS_ALLOCTYPE_START_BNO;
935 args.fsbno = *firstblock;
936 args.type = XFS_ALLOCTYPE_NEAR_BNO;
939 args.minlen = args.maxlen = args.prod = 1;
940 error = xfs_alloc_vextent(&args);
944 /* Can't fail, the space was reserved. */
945 ASSERT(args.fsbno != NULLFSBLOCK);
946 ASSERT(args.len == 1);
947 *firstblock = args.fsbno;
948 bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0);
951 * Initialize the block, copy the data and log the remote buffer.
953 * The callout is responsible for logging because the remote format
954 * might differ from the local format and thus we don't know how much to
955 * log here. Note that init_fn must also set the buffer log item type
958 init_fn(tp, bp, ip, ifp);
960 /* account for the change in fork size */
961 xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
962 xfs_bmap_local_to_extents_empty(ip, whichfork);
963 flags |= XFS_ILOG_CORE;
965 xfs_iext_add(ifp, 0, 1);
966 ep = xfs_iext_get_ext(ifp, 0);
967 xfs_bmbt_set_allf(ep, 0, args.fsbno, 1, XFS_EXT_NORM);
968 trace_xfs_bmap_post_update(ip, 0,
969 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0,
971 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
972 ip->i_d.di_nblocks = 1;
973 xfs_trans_mod_dquot_byino(tp, ip,
974 XFS_TRANS_DQ_BCOUNT, 1L);
975 flags |= xfs_ilog_fext(whichfork);
983 * Called from xfs_bmap_add_attrfork to handle btree format files.
985 STATIC int /* error */
986 xfs_bmap_add_attrfork_btree(
987 xfs_trans_t *tp, /* transaction pointer */
988 xfs_inode_t *ip, /* incore inode pointer */
989 xfs_fsblock_t *firstblock, /* first block allocated */
990 xfs_bmap_free_t *flist, /* blocks to free at commit */
991 int *flags) /* inode logging flags */
993 xfs_btree_cur_t *cur; /* btree cursor */
994 int error; /* error return value */
995 xfs_mount_t *mp; /* file system mount struct */
996 int stat; /* newroot status */
999 if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip))
1000 *flags |= XFS_ILOG_DBROOT;
1002 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
1003 cur->bc_private.b.flist = flist;
1004 cur->bc_private.b.firstblock = *firstblock;
1005 if ((error = xfs_bmbt_lookup_ge(cur, 0, 0, 0, &stat)))
1007 /* must be at least one entry */
1008 XFS_WANT_CORRUPTED_GOTO(mp, stat == 1, error0);
1009 if ((error = xfs_btree_new_iroot(cur, flags, &stat)))
1012 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1015 *firstblock = cur->bc_private.b.firstblock;
1016 cur->bc_private.b.allocated = 0;
1017 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1021 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
1026 * Called from xfs_bmap_add_attrfork to handle extents format files.
1028 STATIC int /* error */
1029 xfs_bmap_add_attrfork_extents(
1030 xfs_trans_t *tp, /* transaction pointer */
1031 xfs_inode_t *ip, /* incore inode pointer */
1032 xfs_fsblock_t *firstblock, /* first block allocated */
1033 xfs_bmap_free_t *flist, /* blocks to free at commit */
1034 int *flags) /* inode logging flags */
1036 xfs_btree_cur_t *cur; /* bmap btree cursor */
1037 int error; /* error return value */
1039 if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip))
1042 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist, &cur, 0,
1043 flags, XFS_DATA_FORK);
1045 cur->bc_private.b.allocated = 0;
1046 xfs_btree_del_cursor(cur,
1047 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
1053 * Called from xfs_bmap_add_attrfork to handle local format files. Each
1054 * different data fork content type needs a different callout to do the
1055 * conversion. Some are basic and only require special block initialisation
1056 * callouts for the data formating, others (directories) are so specialised they
1057 * handle everything themselves.
1059 * XXX (dgc): investigate whether directory conversion can use the generic
1060 * formatting callout. It should be possible - it's just a very complex
1063 STATIC int /* error */
1064 xfs_bmap_add_attrfork_local(
1065 xfs_trans_t *tp, /* transaction pointer */
1066 xfs_inode_t *ip, /* incore inode pointer */
1067 xfs_fsblock_t *firstblock, /* first block allocated */
1068 xfs_bmap_free_t *flist, /* blocks to free at commit */
1069 int *flags) /* inode logging flags */
1071 xfs_da_args_t dargs; /* args for dir/attr code */
1073 if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip))
1076 if (S_ISDIR(ip->i_d.di_mode)) {
1077 memset(&dargs, 0, sizeof(dargs));
1078 dargs.geo = ip->i_mount->m_dir_geo;
1080 dargs.firstblock = firstblock;
1081 dargs.flist = flist;
1082 dargs.total = dargs.geo->fsbcount;
1083 dargs.whichfork = XFS_DATA_FORK;
1085 return xfs_dir2_sf_to_block(&dargs);
1088 if (S_ISLNK(ip->i_d.di_mode))
1089 return xfs_bmap_local_to_extents(tp, ip, firstblock, 1,
1090 flags, XFS_DATA_FORK,
1091 xfs_symlink_local_to_remote);
1093 /* should only be called for types that support local format data */
1095 return -EFSCORRUPTED;
1099 * Convert inode from non-attributed to attributed.
1100 * Must not be in a transaction, ip must not be locked.
1102 int /* error code */
1103 xfs_bmap_add_attrfork(
1104 xfs_inode_t *ip, /* incore inode pointer */
1105 int size, /* space new attribute needs */
1106 int rsvd) /* xact may use reserved blks */
1108 xfs_fsblock_t firstblock; /* 1st block/ag allocated */
1109 xfs_bmap_free_t flist; /* freed extent records */
1110 xfs_mount_t *mp; /* mount structure */
1111 xfs_trans_t *tp; /* transaction pointer */
1112 int blks; /* space reservation */
1113 int version = 1; /* superblock attr version */
1114 int committed; /* xaction was committed */
1115 int logflags; /* logging flags */
1116 int error; /* error return value */
1118 ASSERT(XFS_IFORK_Q(ip) == 0);
1121 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
1122 tp = xfs_trans_alloc(mp, XFS_TRANS_ADDAFORK);
1123 blks = XFS_ADDAFORK_SPACE_RES(mp);
1125 tp->t_flags |= XFS_TRANS_RESERVE;
1126 error = xfs_trans_reserve(tp, &M_RES(mp)->tr_addafork, blks, 0);
1128 xfs_trans_cancel(tp);
1131 xfs_ilock(ip, XFS_ILOCK_EXCL);
1132 error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ?
1133 XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
1134 XFS_QMOPT_RES_REGBLKS);
1137 if (XFS_IFORK_Q(ip))
1139 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) {
1141 * For inodes coming from pre-6.2 filesystems.
1143 ASSERT(ip->i_d.di_aformat == 0);
1144 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
1146 ASSERT(ip->i_d.di_anextents == 0);
1148 xfs_trans_ijoin(tp, ip, 0);
1149 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1151 switch (ip->i_d.di_format) {
1152 case XFS_DINODE_FMT_DEV:
1153 ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
1155 case XFS_DINODE_FMT_UUID:
1156 ip->i_d.di_forkoff = roundup(sizeof(uuid_t), 8) >> 3;
1158 case XFS_DINODE_FMT_LOCAL:
1159 case XFS_DINODE_FMT_EXTENTS:
1160 case XFS_DINODE_FMT_BTREE:
1161 ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size);
1162 if (!ip->i_d.di_forkoff)
1163 ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3;
1164 else if (mp->m_flags & XFS_MOUNT_ATTR2)
1173 ASSERT(ip->i_afp == NULL);
1174 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP);
1175 ip->i_afp->if_flags = XFS_IFEXTENTS;
1177 xfs_bmap_init(&flist, &firstblock);
1178 switch (ip->i_d.di_format) {
1179 case XFS_DINODE_FMT_LOCAL:
1180 error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &flist,
1183 case XFS_DINODE_FMT_EXTENTS:
1184 error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock,
1187 case XFS_DINODE_FMT_BTREE:
1188 error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &flist,
1196 xfs_trans_log_inode(tp, ip, logflags);
1199 if (!xfs_sb_version_hasattr(&mp->m_sb) ||
1200 (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) {
1201 bool log_sb = false;
1203 spin_lock(&mp->m_sb_lock);
1204 if (!xfs_sb_version_hasattr(&mp->m_sb)) {
1205 xfs_sb_version_addattr(&mp->m_sb);
1208 if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) {
1209 xfs_sb_version_addattr2(&mp->m_sb);
1212 spin_unlock(&mp->m_sb_lock);
1217 error = xfs_bmap_finish(&tp, &flist, &committed);
1220 error = xfs_trans_commit(tp);
1221 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1225 xfs_bmap_cancel(&flist);
1227 xfs_trans_cancel(tp);
1228 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1233 * Internal and external extent tree search functions.
1237 * Read in the extents to if_extents.
1238 * All inode fields are set up by caller, we just traverse the btree
1239 * and copy the records in. If the file system cannot contain unwritten
1240 * extents, the records are checked for no "state" flags.
1243 xfs_bmap_read_extents(
1244 xfs_trans_t *tp, /* transaction pointer */
1245 xfs_inode_t *ip, /* incore inode */
1246 int whichfork) /* data or attr fork */
1248 struct xfs_btree_block *block; /* current btree block */
1249 xfs_fsblock_t bno; /* block # of "block" */
1250 xfs_buf_t *bp; /* buffer for "block" */
1251 int error; /* error return value */
1252 xfs_exntfmt_t exntf; /* XFS_EXTFMT_NOSTATE, if checking */
1253 xfs_extnum_t i, j; /* index into the extents list */
1254 xfs_ifork_t *ifp; /* fork structure */
1255 int level; /* btree level, for checking */
1256 xfs_mount_t *mp; /* file system mount structure */
1257 __be64 *pp; /* pointer to block address */
1259 xfs_extnum_t room; /* number of entries there's room for */
1263 ifp = XFS_IFORK_PTR(ip, whichfork);
1264 exntf = (whichfork != XFS_DATA_FORK) ? XFS_EXTFMT_NOSTATE :
1265 XFS_EXTFMT_INODE(ip);
1266 block = ifp->if_broot;
1268 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
1270 level = be16_to_cpu(block->bb_level);
1272 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
1273 bno = be64_to_cpu(*pp);
1274 ASSERT(bno != NULLFSBLOCK);
1275 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
1276 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
1278 * Go down the tree until leaf level is reached, following the first
1279 * pointer (leftmost) at each level.
1281 while (level-- > 0) {
1282 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
1283 XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
1286 block = XFS_BUF_TO_BLOCK(bp);
1289 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
1290 bno = be64_to_cpu(*pp);
1291 XFS_WANT_CORRUPTED_GOTO(mp,
1292 XFS_FSB_SANITY_CHECK(mp, bno), error0);
1293 xfs_trans_brelse(tp, bp);
1296 * Here with bp and block set to the leftmost leaf node in the tree.
1298 room = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
1301 * Loop over all leaf nodes. Copy information to the extent records.
1304 xfs_bmbt_rec_t *frp;
1305 xfs_fsblock_t nextbno;
1306 xfs_extnum_t num_recs;
1309 num_recs = xfs_btree_get_numrecs(block);
1310 if (unlikely(i + num_recs > room)) {
1311 ASSERT(i + num_recs <= room);
1312 xfs_warn(ip->i_mount,
1313 "corrupt dinode %Lu, (btree extents).",
1314 (unsigned long long) ip->i_ino);
1315 XFS_CORRUPTION_ERROR("xfs_bmap_read_extents(1)",
1316 XFS_ERRLEVEL_LOW, ip->i_mount, block);
1320 * Read-ahead the next leaf block, if any.
1322 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
1323 if (nextbno != NULLFSBLOCK)
1324 xfs_btree_reada_bufl(mp, nextbno, 1,
1327 * Copy records into the extent records.
1329 frp = XFS_BMBT_REC_ADDR(mp, block, 1);
1331 for (j = 0; j < num_recs; j++, i++, frp++) {
1332 xfs_bmbt_rec_host_t *trp = xfs_iext_get_ext(ifp, i);
1333 trp->l0 = be64_to_cpu(frp->l0);
1334 trp->l1 = be64_to_cpu(frp->l1);
1336 if (exntf == XFS_EXTFMT_NOSTATE) {
1338 * Check all attribute bmap btree records and
1339 * any "older" data bmap btree records for a
1340 * set bit in the "extent flag" position.
1342 if (unlikely(xfs_check_nostate_extents(ifp,
1343 start, num_recs))) {
1344 XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
1350 xfs_trans_brelse(tp, bp);
1353 * If we've reached the end, stop.
1355 if (bno == NULLFSBLOCK)
1357 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
1358 XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
1361 block = XFS_BUF_TO_BLOCK(bp);
1363 ASSERT(i == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
1364 ASSERT(i == XFS_IFORK_NEXTENTS(ip, whichfork));
1365 XFS_BMAP_TRACE_EXLIST(ip, i, whichfork);
1368 xfs_trans_brelse(tp, bp);
1369 return -EFSCORRUPTED;
1374 * Search the extent records for the entry containing block bno.
1375 * If bno lies in a hole, point to the next entry. If bno lies
1376 * past eof, *eofp will be set, and *prevp will contain the last
1377 * entry (null if none). Else, *lastxp will be set to the index
1378 * of the found entry; *gotp will contain the entry.
1380 STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
1381 xfs_bmap_search_multi_extents(
1382 xfs_ifork_t *ifp, /* inode fork pointer */
1383 xfs_fileoff_t bno, /* block number searched for */
1384 int *eofp, /* out: end of file found */
1385 xfs_extnum_t *lastxp, /* out: last extent index */
1386 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
1387 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
1389 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
1390 xfs_extnum_t lastx; /* last extent index */
1393 * Initialize the extent entry structure to catch access to
1394 * uninitialized br_startblock field.
1396 gotp->br_startoff = 0xffa5a5a5a5a5a5a5LL;
1397 gotp->br_blockcount = 0xa55a5a5a5a5a5a5aLL;
1398 gotp->br_state = XFS_EXT_INVALID;
1399 gotp->br_startblock = 0xffffa5a5a5a5a5a5LL;
1400 prevp->br_startoff = NULLFILEOFF;
1402 ep = xfs_iext_bno_to_ext(ifp, bno, &lastx);
1404 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx - 1), prevp);
1406 if (lastx < (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) {
1407 xfs_bmbt_get_all(ep, gotp);
1421 * Search the extents list for the inode, for the extent containing bno.
1422 * If bno lies in a hole, point to the next entry. If bno lies past eof,
1423 * *eofp will be set, and *prevp will contain the last entry (null if none).
1424 * Else, *lastxp will be set to the index of the found
1425 * entry; *gotp will contain the entry.
1427 STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
1428 xfs_bmap_search_extents(
1429 xfs_inode_t *ip, /* incore inode pointer */
1430 xfs_fileoff_t bno, /* block number searched for */
1431 int fork, /* data or attr fork */
1432 int *eofp, /* out: end of file found */
1433 xfs_extnum_t *lastxp, /* out: last extent index */
1434 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
1435 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
1437 xfs_ifork_t *ifp; /* inode fork pointer */
1438 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
1440 XFS_STATS_INC(ip->i_mount, xs_look_exlist);
1441 ifp = XFS_IFORK_PTR(ip, fork);
1443 ep = xfs_bmap_search_multi_extents(ifp, bno, eofp, lastxp, gotp, prevp);
1445 if (unlikely(!(gotp->br_startblock) && (*lastxp != NULLEXTNUM) &&
1446 !(XFS_IS_REALTIME_INODE(ip) && fork == XFS_DATA_FORK))) {
1447 xfs_alert_tag(ip->i_mount, XFS_PTAG_FSBLOCK_ZERO,
1448 "Access to block zero in inode %llu "
1449 "start_block: %llx start_off: %llx "
1450 "blkcnt: %llx extent-state: %x lastx: %x",
1451 (unsigned long long)ip->i_ino,
1452 (unsigned long long)gotp->br_startblock,
1453 (unsigned long long)gotp->br_startoff,
1454 (unsigned long long)gotp->br_blockcount,
1455 gotp->br_state, *lastxp);
1456 *lastxp = NULLEXTNUM;
1464 * Returns the file-relative block number of the first unused block(s)
1465 * in the file with at least "len" logically contiguous blocks free.
1466 * This is the lowest-address hole if the file has holes, else the first block
1467 * past the end of file.
1468 * Return 0 if the file is currently local (in-inode).
1471 xfs_bmap_first_unused(
1472 xfs_trans_t *tp, /* transaction pointer */
1473 xfs_inode_t *ip, /* incore inode */
1474 xfs_extlen_t len, /* size of hole to find */
1475 xfs_fileoff_t *first_unused, /* unused block */
1476 int whichfork) /* data or attr fork */
1478 int error; /* error return value */
1479 int idx; /* extent record index */
1480 xfs_ifork_t *ifp; /* inode fork pointer */
1481 xfs_fileoff_t lastaddr; /* last block number seen */
1482 xfs_fileoff_t lowest; /* lowest useful block */
1483 xfs_fileoff_t max; /* starting useful block */
1484 xfs_fileoff_t off; /* offset for this block */
1485 xfs_extnum_t nextents; /* number of extent entries */
1487 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE ||
1488 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ||
1489 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
1490 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
1494 ifp = XFS_IFORK_PTR(ip, whichfork);
1495 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
1496 (error = xfs_iread_extents(tp, ip, whichfork)))
1498 lowest = *first_unused;
1499 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
1500 for (idx = 0, lastaddr = 0, max = lowest; idx < nextents; idx++) {
1501 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, idx);
1502 off = xfs_bmbt_get_startoff(ep);
1504 * See if the hole before this extent will work.
1506 if (off >= lowest + len && off - max >= len) {
1507 *first_unused = max;
1510 lastaddr = off + xfs_bmbt_get_blockcount(ep);
1511 max = XFS_FILEOFF_MAX(lastaddr, lowest);
1513 *first_unused = max;
1518 * Returns the file-relative block number of the last block - 1 before
1519 * last_block (input value) in the file.
1520 * This is not based on i_size, it is based on the extent records.
1521 * Returns 0 for local files, as they do not have extent records.
1524 xfs_bmap_last_before(
1525 xfs_trans_t *tp, /* transaction pointer */
1526 xfs_inode_t *ip, /* incore inode */
1527 xfs_fileoff_t *last_block, /* last block */
1528 int whichfork) /* data or attr fork */
1530 xfs_fileoff_t bno; /* input file offset */
1531 int eof; /* hit end of file */
1532 xfs_bmbt_rec_host_t *ep; /* pointer to last extent */
1533 int error; /* error return value */
1534 xfs_bmbt_irec_t got; /* current extent value */
1535 xfs_ifork_t *ifp; /* inode fork pointer */
1536 xfs_extnum_t lastx; /* last extent used */
1537 xfs_bmbt_irec_t prev; /* previous extent value */
1539 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
1540 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
1541 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
1543 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
1547 ifp = XFS_IFORK_PTR(ip, whichfork);
1548 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
1549 (error = xfs_iread_extents(tp, ip, whichfork)))
1551 bno = *last_block - 1;
1552 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
1554 if (eof || xfs_bmbt_get_startoff(ep) > bno) {
1555 if (prev.br_startoff == NULLFILEOFF)
1558 *last_block = prev.br_startoff + prev.br_blockcount;
1561 * Otherwise *last_block is already the right answer.
1567 xfs_bmap_last_extent(
1568 struct xfs_trans *tp,
1569 struct xfs_inode *ip,
1571 struct xfs_bmbt_irec *rec,
1574 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1578 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1579 error = xfs_iread_extents(tp, ip, whichfork);
1584 nextents = ifp->if_bytes / sizeof(xfs_bmbt_rec_t);
1585 if (nextents == 0) {
1590 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, nextents - 1), rec);
1596 * Check the last inode extent to determine whether this allocation will result
1597 * in blocks being allocated at the end of the file. When we allocate new data
1598 * blocks at the end of the file which do not start at the previous data block,
1599 * we will try to align the new blocks at stripe unit boundaries.
1601 * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be
1602 * at, or past the EOF.
1606 struct xfs_bmalloca *bma,
1609 struct xfs_bmbt_irec rec;
1614 error = xfs_bmap_last_extent(NULL, bma->ip, whichfork, &rec,
1625 * Check if we are allocation or past the last extent, or at least into
1626 * the last delayed allocated extent.
1628 bma->aeof = bma->offset >= rec.br_startoff + rec.br_blockcount ||
1629 (bma->offset >= rec.br_startoff &&
1630 isnullstartblock(rec.br_startblock));
1635 * Returns the file-relative block number of the first block past eof in
1636 * the file. This is not based on i_size, it is based on the extent records.
1637 * Returns 0 for local files, as they do not have extent records.
1640 xfs_bmap_last_offset(
1641 struct xfs_inode *ip,
1642 xfs_fileoff_t *last_block,
1645 struct xfs_bmbt_irec rec;
1651 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL)
1654 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
1655 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
1658 error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, &is_empty);
1659 if (error || is_empty)
1662 *last_block = rec.br_startoff + rec.br_blockcount;
1667 * Returns whether the selected fork of the inode has exactly one
1668 * block or not. For the data fork we check this matches di_size,
1669 * implying the file's range is 0..bsize-1.
1671 int /* 1=>1 block, 0=>otherwise */
1673 xfs_inode_t *ip, /* incore inode */
1674 int whichfork) /* data or attr fork */
1676 xfs_bmbt_rec_host_t *ep; /* ptr to fork's extent */
1677 xfs_ifork_t *ifp; /* inode fork pointer */
1678 int rval; /* return value */
1679 xfs_bmbt_irec_t s; /* internal version of extent */
1682 if (whichfork == XFS_DATA_FORK)
1683 return XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize;
1685 if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1)
1687 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
1689 ifp = XFS_IFORK_PTR(ip, whichfork);
1690 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
1691 ep = xfs_iext_get_ext(ifp, 0);
1692 xfs_bmbt_get_all(ep, &s);
1693 rval = s.br_startoff == 0 && s.br_blockcount == 1;
1694 if (rval && whichfork == XFS_DATA_FORK)
1695 ASSERT(XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize);
1700 * Extent tree manipulation functions used during allocation.
1704 * Convert a delayed allocation to a real allocation.
1706 STATIC int /* error */
1707 xfs_bmap_add_extent_delay_real(
1708 struct xfs_bmalloca *bma)
1710 struct xfs_bmbt_irec *new = &bma->got;
1711 int diff; /* temp value */
1712 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
1713 int error; /* error return value */
1714 int i; /* temp state */
1715 xfs_ifork_t *ifp; /* inode fork pointer */
1716 xfs_fileoff_t new_endoff; /* end offset of new entry */
1717 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
1718 /* left is 0, right is 1, prev is 2 */
1719 int rval=0; /* return value (logging flags) */
1720 int state = 0;/* state bits, accessed thru macros */
1721 xfs_filblks_t da_new; /* new count del alloc blocks used */
1722 xfs_filblks_t da_old; /* old count del alloc blocks used */
1723 xfs_filblks_t temp=0; /* value for da_new calculations */
1724 xfs_filblks_t temp2=0;/* value for da_new calculations */
1725 int tmp_rval; /* partial logging flags */
1726 struct xfs_mount *mp;
1728 mp = bma->tp ? bma->tp->t_mountp : NULL;
1729 ifp = XFS_IFORK_PTR(bma->ip, XFS_DATA_FORK);
1731 ASSERT(bma->idx >= 0);
1732 ASSERT(bma->idx <= ifp->if_bytes / sizeof(struct xfs_bmbt_rec));
1733 ASSERT(!isnullstartblock(new->br_startblock));
1735 (bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
1737 XFS_STATS_INC(mp, xs_add_exlist);
1744 * Set up a bunch of variables to make the tests simpler.
1746 ep = xfs_iext_get_ext(ifp, bma->idx);
1747 xfs_bmbt_get_all(ep, &PREV);
1748 new_endoff = new->br_startoff + new->br_blockcount;
1749 ASSERT(PREV.br_startoff <= new->br_startoff);
1750 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
1752 da_old = startblockval(PREV.br_startblock);
1756 * Set flags determining what part of the previous delayed allocation
1757 * extent is being replaced by a real allocation.
1759 if (PREV.br_startoff == new->br_startoff)
1760 state |= BMAP_LEFT_FILLING;
1761 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
1762 state |= BMAP_RIGHT_FILLING;
1765 * Check and set flags if this segment has a left neighbor.
1766 * Don't set contiguous if the combined extent would be too large.
1769 state |= BMAP_LEFT_VALID;
1770 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1), &LEFT);
1772 if (isnullstartblock(LEFT.br_startblock))
1773 state |= BMAP_LEFT_DELAY;
1776 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1777 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1778 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
1779 LEFT.br_state == new->br_state &&
1780 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1781 state |= BMAP_LEFT_CONTIG;
1784 * Check and set flags if this segment has a right neighbor.
1785 * Don't set contiguous if the combined extent would be too large.
1786 * Also check for all-three-contiguous being too large.
1788 if (bma->idx < bma->ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1) {
1789 state |= BMAP_RIGHT_VALID;
1790 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx + 1), &RIGHT);
1792 if (isnullstartblock(RIGHT.br_startblock))
1793 state |= BMAP_RIGHT_DELAY;
1796 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1797 new_endoff == RIGHT.br_startoff &&
1798 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
1799 new->br_state == RIGHT.br_state &&
1800 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
1801 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1802 BMAP_RIGHT_FILLING)) !=
1803 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1804 BMAP_RIGHT_FILLING) ||
1805 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
1807 state |= BMAP_RIGHT_CONTIG;
1811 * Switch out based on the FILLING and CONTIG state bits.
1813 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1814 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
1815 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1816 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1818 * Filling in all of a previously delayed allocation extent.
1819 * The left and right neighbors are both contiguous with new.
1822 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
1823 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
1824 LEFT.br_blockcount + PREV.br_blockcount +
1825 RIGHT.br_blockcount);
1826 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
1828 xfs_iext_remove(bma->ip, bma->idx + 1, 2, state);
1829 bma->ip->i_d.di_nextents--;
1830 if (bma->cur == NULL)
1831 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1833 rval = XFS_ILOG_CORE;
1834 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
1835 RIGHT.br_startblock,
1836 RIGHT.br_blockcount, &i);
1839 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
1840 error = xfs_btree_delete(bma->cur, &i);
1843 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
1844 error = xfs_btree_decrement(bma->cur, 0, &i);
1847 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
1848 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
1850 LEFT.br_blockcount +
1851 PREV.br_blockcount +
1852 RIGHT.br_blockcount, LEFT.br_state);
1858 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1860 * Filling in all of a previously delayed allocation extent.
1861 * The left neighbor is contiguous, the right is not.
1865 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
1866 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
1867 LEFT.br_blockcount + PREV.br_blockcount);
1868 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
1870 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
1871 if (bma->cur == NULL)
1872 rval = XFS_ILOG_DEXT;
1875 error = xfs_bmbt_lookup_eq(bma->cur, LEFT.br_startoff,
1876 LEFT.br_startblock, LEFT.br_blockcount,
1880 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
1881 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
1883 LEFT.br_blockcount +
1884 PREV.br_blockcount, LEFT.br_state);
1890 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1892 * Filling in all of a previously delayed allocation extent.
1893 * The right neighbor is contiguous, the left is not.
1895 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
1896 xfs_bmbt_set_startblock(ep, new->br_startblock);
1897 xfs_bmbt_set_blockcount(ep,
1898 PREV.br_blockcount + RIGHT.br_blockcount);
1899 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
1901 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
1902 if (bma->cur == NULL)
1903 rval = XFS_ILOG_DEXT;
1906 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
1907 RIGHT.br_startblock,
1908 RIGHT.br_blockcount, &i);
1911 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
1912 error = xfs_bmbt_update(bma->cur, PREV.br_startoff,
1914 PREV.br_blockcount +
1915 RIGHT.br_blockcount, PREV.br_state);
1921 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
1923 * Filling in all of a previously delayed allocation extent.
1924 * Neither the left nor right neighbors are contiguous with
1927 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
1928 xfs_bmbt_set_startblock(ep, new->br_startblock);
1929 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
1931 bma->ip->i_d.di_nextents++;
1932 if (bma->cur == NULL)
1933 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1935 rval = XFS_ILOG_CORE;
1936 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
1937 new->br_startblock, new->br_blockcount,
1941 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
1942 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
1943 error = xfs_btree_insert(bma->cur, &i);
1946 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
1950 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
1952 * Filling in the first part of a previous delayed allocation.
1953 * The left neighbor is contiguous.
1955 trace_xfs_bmap_pre_update(bma->ip, bma->idx - 1, state, _THIS_IP_);
1956 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx - 1),
1957 LEFT.br_blockcount + new->br_blockcount);
1958 xfs_bmbt_set_startoff(ep,
1959 PREV.br_startoff + new->br_blockcount);
1960 trace_xfs_bmap_post_update(bma->ip, bma->idx - 1, state, _THIS_IP_);
1962 temp = PREV.br_blockcount - new->br_blockcount;
1963 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
1964 xfs_bmbt_set_blockcount(ep, temp);
1965 if (bma->cur == NULL)
1966 rval = XFS_ILOG_DEXT;
1969 error = xfs_bmbt_lookup_eq(bma->cur, LEFT.br_startoff,
1970 LEFT.br_startblock, LEFT.br_blockcount,
1974 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
1975 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
1977 LEFT.br_blockcount +
1983 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
1984 startblockval(PREV.br_startblock));
1985 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
1986 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
1991 case BMAP_LEFT_FILLING:
1993 * Filling in the first part of a previous delayed allocation.
1994 * The left neighbor is not contiguous.
1996 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
1997 xfs_bmbt_set_startoff(ep, new_endoff);
1998 temp = PREV.br_blockcount - new->br_blockcount;
1999 xfs_bmbt_set_blockcount(ep, temp);
2000 xfs_iext_insert(bma->ip, bma->idx, 1, new, state);
2001 bma->ip->i_d.di_nextents++;
2002 if (bma->cur == NULL)
2003 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2005 rval = XFS_ILOG_CORE;
2006 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
2007 new->br_startblock, new->br_blockcount,
2011 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
2012 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
2013 error = xfs_btree_insert(bma->cur, &i);
2016 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2019 if (xfs_bmap_needs_btree(bma->ip, XFS_DATA_FORK)) {
2020 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
2021 bma->firstblock, bma->flist,
2022 &bma->cur, 1, &tmp_rval, XFS_DATA_FORK);
2027 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
2028 startblockval(PREV.br_startblock) -
2029 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
2030 ep = xfs_iext_get_ext(ifp, bma->idx + 1);
2031 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
2032 trace_xfs_bmap_post_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
2035 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
2037 * Filling in the last part of a previous delayed allocation.
2038 * The right neighbor is contiguous with the new allocation.
2040 temp = PREV.br_blockcount - new->br_blockcount;
2041 trace_xfs_bmap_pre_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
2042 xfs_bmbt_set_blockcount(ep, temp);
2043 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, bma->idx + 1),
2044 new->br_startoff, new->br_startblock,
2045 new->br_blockcount + RIGHT.br_blockcount,
2047 trace_xfs_bmap_post_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
2048 if (bma->cur == NULL)
2049 rval = XFS_ILOG_DEXT;
2052 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
2053 RIGHT.br_startblock,
2054 RIGHT.br_blockcount, &i);
2057 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2058 error = xfs_bmbt_update(bma->cur, new->br_startoff,
2060 new->br_blockcount +
2061 RIGHT.br_blockcount,
2067 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
2068 startblockval(PREV.br_startblock));
2069 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
2070 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
2071 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
2076 case BMAP_RIGHT_FILLING:
2078 * Filling in the last part of a previous delayed allocation.
2079 * The right neighbor is not contiguous.
2081 temp = PREV.br_blockcount - new->br_blockcount;
2082 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
2083 xfs_bmbt_set_blockcount(ep, temp);
2084 xfs_iext_insert(bma->ip, bma->idx + 1, 1, new, state);
2085 bma->ip->i_d.di_nextents++;
2086 if (bma->cur == NULL)
2087 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2089 rval = XFS_ILOG_CORE;
2090 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
2091 new->br_startblock, new->br_blockcount,
2095 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
2096 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
2097 error = xfs_btree_insert(bma->cur, &i);
2100 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2103 if (xfs_bmap_needs_btree(bma->ip, XFS_DATA_FORK)) {
2104 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
2105 bma->firstblock, bma->flist, &bma->cur, 1,
2106 &tmp_rval, XFS_DATA_FORK);
2111 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
2112 startblockval(PREV.br_startblock) -
2113 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
2114 ep = xfs_iext_get_ext(ifp, bma->idx);
2115 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
2116 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
2123 * Filling in the middle part of a previous delayed allocation.
2124 * Contiguity is impossible here.
2125 * This case is avoided almost all the time.
2127 * We start with a delayed allocation:
2129 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
2132 * and we are allocating:
2133 * +rrrrrrrrrrrrrrrrr+
2136 * and we set it up for insertion as:
2137 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
2139 * PREV @ idx LEFT RIGHT
2140 * inserted at idx + 1
2142 temp = new->br_startoff - PREV.br_startoff;
2143 temp2 = PREV.br_startoff + PREV.br_blockcount - new_endoff;
2144 trace_xfs_bmap_pre_update(bma->ip, bma->idx, 0, _THIS_IP_);
2145 xfs_bmbt_set_blockcount(ep, temp); /* truncate PREV */
2147 RIGHT.br_state = PREV.br_state;
2148 RIGHT.br_startblock = nullstartblock(
2149 (int)xfs_bmap_worst_indlen(bma->ip, temp2));
2150 RIGHT.br_startoff = new_endoff;
2151 RIGHT.br_blockcount = temp2;
2152 /* insert LEFT (r[0]) and RIGHT (r[1]) at the same time */
2153 xfs_iext_insert(bma->ip, bma->idx + 1, 2, &LEFT, state);
2154 bma->ip->i_d.di_nextents++;
2155 if (bma->cur == NULL)
2156 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2158 rval = XFS_ILOG_CORE;
2159 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
2160 new->br_startblock, new->br_blockcount,
2164 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
2165 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
2166 error = xfs_btree_insert(bma->cur, &i);
2169 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2172 if (xfs_bmap_needs_btree(bma->ip, XFS_DATA_FORK)) {
2173 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
2174 bma->firstblock, bma->flist, &bma->cur,
2175 1, &tmp_rval, XFS_DATA_FORK);
2180 temp = xfs_bmap_worst_indlen(bma->ip, temp);
2181 temp2 = xfs_bmap_worst_indlen(bma->ip, temp2);
2182 diff = (int)(temp + temp2 - startblockval(PREV.br_startblock) -
2183 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
2185 error = xfs_mod_fdblocks(bma->ip->i_mount,
2186 -((int64_t)diff), false);
2192 ep = xfs_iext_get_ext(ifp, bma->idx);
2193 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
2194 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
2195 trace_xfs_bmap_pre_update(bma->ip, bma->idx + 2, state, _THIS_IP_);
2196 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, bma->idx + 2),
2197 nullstartblock((int)temp2));
2198 trace_xfs_bmap_post_update(bma->ip, bma->idx + 2, state, _THIS_IP_);
2201 da_new = temp + temp2;
2204 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2205 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2206 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
2207 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2208 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2209 case BMAP_LEFT_CONTIG:
2210 case BMAP_RIGHT_CONTIG:
2212 * These cases are all impossible.
2217 /* convert to a btree if necessary */
2218 if (xfs_bmap_needs_btree(bma->ip, XFS_DATA_FORK)) {
2219 int tmp_logflags; /* partial log flag return val */
2221 ASSERT(bma->cur == NULL);
2222 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
2223 bma->firstblock, bma->flist, &bma->cur,
2224 da_old > 0, &tmp_logflags, XFS_DATA_FORK);
2225 bma->logflags |= tmp_logflags;
2230 /* adjust for changes in reserved delayed indirect blocks */
2231 if (da_old || da_new) {
2234 temp += bma->cur->bc_private.b.allocated;
2235 ASSERT(temp <= da_old);
2237 xfs_mod_fdblocks(bma->ip->i_mount,
2238 (int64_t)(da_old - temp), false);
2241 /* clear out the allocated field, done with it now in any case. */
2243 bma->cur->bc_private.b.allocated = 0;
2245 xfs_bmap_check_leaf_extents(bma->cur, bma->ip, XFS_DATA_FORK);
2247 bma->logflags |= rval;
2255 * Convert an unwritten allocation to a real allocation or vice versa.
2257 STATIC int /* error */
2258 xfs_bmap_add_extent_unwritten_real(
2259 struct xfs_trans *tp,
2260 xfs_inode_t *ip, /* incore inode pointer */
2261 xfs_extnum_t *idx, /* extent number to update/insert */
2262 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
2263 xfs_bmbt_irec_t *new, /* new data to add to file extents */
2264 xfs_fsblock_t *first, /* pointer to firstblock variable */
2265 xfs_bmap_free_t *flist, /* list of extents to be freed */
2266 int *logflagsp) /* inode logging flags */
2268 xfs_btree_cur_t *cur; /* btree cursor */
2269 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
2270 int error; /* error return value */
2271 int i; /* temp state */
2272 xfs_ifork_t *ifp; /* inode fork pointer */
2273 xfs_fileoff_t new_endoff; /* end offset of new entry */
2274 xfs_exntst_t newext; /* new extent state */
2275 xfs_exntst_t oldext; /* old extent state */
2276 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
2277 /* left is 0, right is 1, prev is 2 */
2278 int rval=0; /* return value (logging flags) */
2279 int state = 0;/* state bits, accessed thru macros */
2280 struct xfs_mount *mp = tp->t_mountp;
2285 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
2288 ASSERT(*idx <= ifp->if_bytes / sizeof(struct xfs_bmbt_rec));
2289 ASSERT(!isnullstartblock(new->br_startblock));
2291 XFS_STATS_INC(mp, xs_add_exlist);
2298 * Set up a bunch of variables to make the tests simpler.
2301 ep = xfs_iext_get_ext(ifp, *idx);
2302 xfs_bmbt_get_all(ep, &PREV);
2303 newext = new->br_state;
2304 oldext = (newext == XFS_EXT_UNWRITTEN) ?
2305 XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
2306 ASSERT(PREV.br_state == oldext);
2307 new_endoff = new->br_startoff + new->br_blockcount;
2308 ASSERT(PREV.br_startoff <= new->br_startoff);
2309 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
2312 * Set flags determining what part of the previous oldext allocation
2313 * extent is being replaced by a newext allocation.
2315 if (PREV.br_startoff == new->br_startoff)
2316 state |= BMAP_LEFT_FILLING;
2317 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
2318 state |= BMAP_RIGHT_FILLING;
2321 * Check and set flags if this segment has a left neighbor.
2322 * Don't set contiguous if the combined extent would be too large.
2325 state |= BMAP_LEFT_VALID;
2326 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &LEFT);
2328 if (isnullstartblock(LEFT.br_startblock))
2329 state |= BMAP_LEFT_DELAY;
2332 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2333 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
2334 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
2335 LEFT.br_state == newext &&
2336 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2337 state |= BMAP_LEFT_CONTIG;
2340 * Check and set flags if this segment has a right neighbor.
2341 * Don't set contiguous if the combined extent would be too large.
2342 * Also check for all-three-contiguous being too large.
2344 if (*idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1) {
2345 state |= BMAP_RIGHT_VALID;
2346 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx + 1), &RIGHT);
2347 if (isnullstartblock(RIGHT.br_startblock))
2348 state |= BMAP_RIGHT_DELAY;
2351 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2352 new_endoff == RIGHT.br_startoff &&
2353 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
2354 newext == RIGHT.br_state &&
2355 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
2356 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2357 BMAP_RIGHT_FILLING)) !=
2358 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2359 BMAP_RIGHT_FILLING) ||
2360 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
2362 state |= BMAP_RIGHT_CONTIG;
2365 * Switch out based on the FILLING and CONTIG state bits.
2367 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2368 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
2369 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2370 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
2372 * Setting all of a previous oldext extent to newext.
2373 * The left and right neighbors are both contiguous with new.
2377 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2378 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
2379 LEFT.br_blockcount + PREV.br_blockcount +
2380 RIGHT.br_blockcount);
2381 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2383 xfs_iext_remove(ip, *idx + 1, 2, state);
2384 ip->i_d.di_nextents -= 2;
2386 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2388 rval = XFS_ILOG_CORE;
2389 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
2390 RIGHT.br_startblock,
2391 RIGHT.br_blockcount, &i)))
2393 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2394 if ((error = xfs_btree_delete(cur, &i)))
2396 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2397 if ((error = xfs_btree_decrement(cur, 0, &i)))
2399 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2400 if ((error = xfs_btree_delete(cur, &i)))
2402 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2403 if ((error = xfs_btree_decrement(cur, 0, &i)))
2405 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2406 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
2408 LEFT.br_blockcount + PREV.br_blockcount +
2409 RIGHT.br_blockcount, LEFT.br_state)))
2414 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2416 * Setting all of a previous oldext extent to newext.
2417 * The left neighbor is contiguous, the right is not.
2421 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2422 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
2423 LEFT.br_blockcount + PREV.br_blockcount);
2424 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2426 xfs_iext_remove(ip, *idx + 1, 1, state);
2427 ip->i_d.di_nextents--;
2429 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2431 rval = XFS_ILOG_CORE;
2432 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2433 PREV.br_startblock, PREV.br_blockcount,
2436 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2437 if ((error = xfs_btree_delete(cur, &i)))
2439 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2440 if ((error = xfs_btree_decrement(cur, 0, &i)))
2442 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2443 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
2445 LEFT.br_blockcount + PREV.br_blockcount,
2451 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
2453 * Setting all of a previous oldext extent to newext.
2454 * The right neighbor is contiguous, the left is not.
2456 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2457 xfs_bmbt_set_blockcount(ep,
2458 PREV.br_blockcount + RIGHT.br_blockcount);
2459 xfs_bmbt_set_state(ep, newext);
2460 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2461 xfs_iext_remove(ip, *idx + 1, 1, state);
2462 ip->i_d.di_nextents--;
2464 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2466 rval = XFS_ILOG_CORE;
2467 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
2468 RIGHT.br_startblock,
2469 RIGHT.br_blockcount, &i)))
2471 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2472 if ((error = xfs_btree_delete(cur, &i)))
2474 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2475 if ((error = xfs_btree_decrement(cur, 0, &i)))
2477 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2478 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2480 new->br_blockcount + RIGHT.br_blockcount,
2486 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
2488 * Setting all of a previous oldext extent to newext.
2489 * Neither the left nor right neighbors are contiguous with
2492 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2493 xfs_bmbt_set_state(ep, newext);
2494 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2497 rval = XFS_ILOG_DEXT;
2500 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
2501 new->br_startblock, new->br_blockcount,
2504 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2505 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2506 new->br_startblock, new->br_blockcount,
2512 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
2514 * Setting the first part of a previous oldext extent to newext.
2515 * The left neighbor is contiguous.
2517 trace_xfs_bmap_pre_update(ip, *idx - 1, state, _THIS_IP_);
2518 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx - 1),
2519 LEFT.br_blockcount + new->br_blockcount);
2520 xfs_bmbt_set_startoff(ep,
2521 PREV.br_startoff + new->br_blockcount);
2522 trace_xfs_bmap_post_update(ip, *idx - 1, state, _THIS_IP_);
2524 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2525 xfs_bmbt_set_startblock(ep,
2526 new->br_startblock + new->br_blockcount);
2527 xfs_bmbt_set_blockcount(ep,
2528 PREV.br_blockcount - new->br_blockcount);
2529 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2534 rval = XFS_ILOG_DEXT;
2537 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2538 PREV.br_startblock, PREV.br_blockcount,
2541 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2542 if ((error = xfs_bmbt_update(cur,
2543 PREV.br_startoff + new->br_blockcount,
2544 PREV.br_startblock + new->br_blockcount,
2545 PREV.br_blockcount - new->br_blockcount,
2548 if ((error = xfs_btree_decrement(cur, 0, &i)))
2550 error = xfs_bmbt_update(cur, LEFT.br_startoff,
2552 LEFT.br_blockcount + new->br_blockcount,
2559 case BMAP_LEFT_FILLING:
2561 * Setting the first part of a previous oldext extent to newext.
2562 * The left neighbor is not contiguous.
2564 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2565 ASSERT(ep && xfs_bmbt_get_state(ep) == oldext);
2566 xfs_bmbt_set_startoff(ep, new_endoff);
2567 xfs_bmbt_set_blockcount(ep,
2568 PREV.br_blockcount - new->br_blockcount);
2569 xfs_bmbt_set_startblock(ep,
2570 new->br_startblock + new->br_blockcount);
2571 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2573 xfs_iext_insert(ip, *idx, 1, new, state);
2574 ip->i_d.di_nextents++;
2576 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2578 rval = XFS_ILOG_CORE;
2579 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2580 PREV.br_startblock, PREV.br_blockcount,
2583 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2584 if ((error = xfs_bmbt_update(cur,
2585 PREV.br_startoff + new->br_blockcount,
2586 PREV.br_startblock + new->br_blockcount,
2587 PREV.br_blockcount - new->br_blockcount,
2590 cur->bc_rec.b = *new;
2591 if ((error = xfs_btree_insert(cur, &i)))
2593 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2597 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
2599 * Setting the last part of a previous oldext extent to newext.
2600 * The right neighbor is contiguous with the new allocation.
2602 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2603 xfs_bmbt_set_blockcount(ep,
2604 PREV.br_blockcount - new->br_blockcount);
2605 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2609 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2610 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx),
2611 new->br_startoff, new->br_startblock,
2612 new->br_blockcount + RIGHT.br_blockcount, newext);
2613 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2616 rval = XFS_ILOG_DEXT;
2619 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2621 PREV.br_blockcount, &i)))
2623 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2624 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
2626 PREV.br_blockcount - new->br_blockcount,
2629 if ((error = xfs_btree_increment(cur, 0, &i)))
2631 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2633 new->br_blockcount + RIGHT.br_blockcount,
2639 case BMAP_RIGHT_FILLING:
2641 * Setting the last part of a previous oldext extent to newext.
2642 * The right neighbor is not contiguous.
2644 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2645 xfs_bmbt_set_blockcount(ep,
2646 PREV.br_blockcount - new->br_blockcount);
2647 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2650 xfs_iext_insert(ip, *idx, 1, new, state);
2652 ip->i_d.di_nextents++;
2654 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2656 rval = XFS_ILOG_CORE;
2657 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2658 PREV.br_startblock, PREV.br_blockcount,
2661 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2662 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
2664 PREV.br_blockcount - new->br_blockcount,
2667 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
2668 new->br_startblock, new->br_blockcount,
2671 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
2672 cur->bc_rec.b.br_state = XFS_EXT_NORM;
2673 if ((error = xfs_btree_insert(cur, &i)))
2675 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2681 * Setting the middle part of a previous oldext extent to
2682 * newext. Contiguity is impossible here.
2683 * One extent becomes three extents.
2685 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2686 xfs_bmbt_set_blockcount(ep,
2687 new->br_startoff - PREV.br_startoff);
2688 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2691 r[1].br_startoff = new_endoff;
2692 r[1].br_blockcount =
2693 PREV.br_startoff + PREV.br_blockcount - new_endoff;
2694 r[1].br_startblock = new->br_startblock + new->br_blockcount;
2695 r[1].br_state = oldext;
2698 xfs_iext_insert(ip, *idx, 2, &r[0], state);
2700 ip->i_d.di_nextents += 2;
2702 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2704 rval = XFS_ILOG_CORE;
2705 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2706 PREV.br_startblock, PREV.br_blockcount,
2709 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2710 /* new right extent - oldext */
2711 if ((error = xfs_bmbt_update(cur, r[1].br_startoff,
2712 r[1].br_startblock, r[1].br_blockcount,
2715 /* new left extent - oldext */
2716 cur->bc_rec.b = PREV;
2717 cur->bc_rec.b.br_blockcount =
2718 new->br_startoff - PREV.br_startoff;
2719 if ((error = xfs_btree_insert(cur, &i)))
2721 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2723 * Reset the cursor to the position of the new extent
2724 * we are about to insert as we can't trust it after
2725 * the previous insert.
2727 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
2728 new->br_startblock, new->br_blockcount,
2731 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
2732 /* new middle extent - newext */
2733 cur->bc_rec.b.br_state = new->br_state;
2734 if ((error = xfs_btree_insert(cur, &i)))
2736 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2740 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2741 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2742 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
2743 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2744 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2745 case BMAP_LEFT_CONTIG:
2746 case BMAP_RIGHT_CONTIG:
2748 * These cases are all impossible.
2753 /* convert to a btree if necessary */
2754 if (xfs_bmap_needs_btree(ip, XFS_DATA_FORK)) {
2755 int tmp_logflags; /* partial log flag return val */
2757 ASSERT(cur == NULL);
2758 error = xfs_bmap_extents_to_btree(tp, ip, first, flist, &cur,
2759 0, &tmp_logflags, XFS_DATA_FORK);
2760 *logflagsp |= tmp_logflags;
2765 /* clear out the allocated field, done with it now in any case. */
2767 cur->bc_private.b.allocated = 0;
2771 xfs_bmap_check_leaf_extents(*curp, ip, XFS_DATA_FORK);
2781 * Convert a hole to a delayed allocation.
2784 xfs_bmap_add_extent_hole_delay(
2785 xfs_inode_t *ip, /* incore inode pointer */
2786 xfs_extnum_t *idx, /* extent number to update/insert */
2787 xfs_bmbt_irec_t *new) /* new data to add to file extents */
2789 xfs_ifork_t *ifp; /* inode fork pointer */
2790 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2791 xfs_filblks_t newlen=0; /* new indirect size */
2792 xfs_filblks_t oldlen=0; /* old indirect size */
2793 xfs_bmbt_irec_t right; /* right neighbor extent entry */
2794 int state; /* state bits, accessed thru macros */
2795 xfs_filblks_t temp=0; /* temp for indirect calculations */
2797 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
2799 ASSERT(isnullstartblock(new->br_startblock));
2802 * Check and set flags if this segment has a left neighbor
2805 state |= BMAP_LEFT_VALID;
2806 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &left);
2808 if (isnullstartblock(left.br_startblock))
2809 state |= BMAP_LEFT_DELAY;
2813 * Check and set flags if the current (right) segment exists.
2814 * If it doesn't exist, we're converting the hole at end-of-file.
2816 if (*idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) {
2817 state |= BMAP_RIGHT_VALID;
2818 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx), &right);
2820 if (isnullstartblock(right.br_startblock))
2821 state |= BMAP_RIGHT_DELAY;
2825 * Set contiguity flags on the left and right neighbors.
2826 * Don't let extents get too large, even if the pieces are contiguous.
2828 if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) &&
2829 left.br_startoff + left.br_blockcount == new->br_startoff &&
2830 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2831 state |= BMAP_LEFT_CONTIG;
2833 if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) &&
2834 new->br_startoff + new->br_blockcount == right.br_startoff &&
2835 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2836 (!(state & BMAP_LEFT_CONTIG) ||
2837 (left.br_blockcount + new->br_blockcount +
2838 right.br_blockcount <= MAXEXTLEN)))
2839 state |= BMAP_RIGHT_CONTIG;
2842 * Switch out based on the contiguity flags.
2844 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2845 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2847 * New allocation is contiguous with delayed allocations
2848 * on the left and on the right.
2849 * Merge all three into a single extent record.
2852 temp = left.br_blockcount + new->br_blockcount +
2853 right.br_blockcount;
2855 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2856 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp);
2857 oldlen = startblockval(left.br_startblock) +
2858 startblockval(new->br_startblock) +
2859 startblockval(right.br_startblock);
2860 newlen = xfs_bmap_worst_indlen(ip, temp);
2861 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx),
2862 nullstartblock((int)newlen));
2863 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2865 xfs_iext_remove(ip, *idx + 1, 1, state);
2868 case BMAP_LEFT_CONTIG:
2870 * New allocation is contiguous with a delayed allocation
2872 * Merge the new allocation with the left neighbor.
2875 temp = left.br_blockcount + new->br_blockcount;
2877 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2878 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp);
2879 oldlen = startblockval(left.br_startblock) +
2880 startblockval(new->br_startblock);
2881 newlen = xfs_bmap_worst_indlen(ip, temp);
2882 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx),
2883 nullstartblock((int)newlen));
2884 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2887 case BMAP_RIGHT_CONTIG:
2889 * New allocation is contiguous with a delayed allocation
2891 * Merge the new allocation with the right neighbor.
2893 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2894 temp = new->br_blockcount + right.br_blockcount;
2895 oldlen = startblockval(new->br_startblock) +
2896 startblockval(right.br_startblock);
2897 newlen = xfs_bmap_worst_indlen(ip, temp);
2898 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx),
2900 nullstartblock((int)newlen), temp, right.br_state);
2901 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2906 * New allocation is not contiguous with another
2907 * delayed allocation.
2908 * Insert a new entry.
2910 oldlen = newlen = 0;
2911 xfs_iext_insert(ip, *idx, 1, new, state);
2914 if (oldlen != newlen) {
2915 ASSERT(oldlen > newlen);
2916 xfs_mod_fdblocks(ip->i_mount, (int64_t)(oldlen - newlen),
2919 * Nothing to do for disk quota accounting here.
2925 * Convert a hole to a real allocation.
2927 STATIC int /* error */
2928 xfs_bmap_add_extent_hole_real(
2929 struct xfs_bmalloca *bma,
2932 struct xfs_bmbt_irec *new = &bma->got;
2933 int error; /* error return value */
2934 int i; /* temp state */
2935 xfs_ifork_t *ifp; /* inode fork pointer */
2936 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2937 xfs_bmbt_irec_t right; /* right neighbor extent entry */
2938 int rval=0; /* return value (logging flags) */
2939 int state; /* state bits, accessed thru macros */
2940 struct xfs_mount *mp;
2942 mp = bma->tp ? bma->tp->t_mountp : NULL;
2943 ifp = XFS_IFORK_PTR(bma->ip, whichfork);
2945 ASSERT(bma->idx >= 0);
2946 ASSERT(bma->idx <= ifp->if_bytes / sizeof(struct xfs_bmbt_rec));
2947 ASSERT(!isnullstartblock(new->br_startblock));
2949 !(bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
2951 XFS_STATS_INC(mp, xs_add_exlist);
2954 if (whichfork == XFS_ATTR_FORK)
2955 state |= BMAP_ATTRFORK;
2958 * Check and set flags if this segment has a left neighbor.
2961 state |= BMAP_LEFT_VALID;
2962 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1), &left);
2963 if (isnullstartblock(left.br_startblock))
2964 state |= BMAP_LEFT_DELAY;
2968 * Check and set flags if this segment has a current value.
2969 * Not true if we're inserting into the "hole" at eof.
2971 if (bma->idx < ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) {
2972 state |= BMAP_RIGHT_VALID;
2973 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &right);
2974 if (isnullstartblock(right.br_startblock))
2975 state |= BMAP_RIGHT_DELAY;
2979 * We're inserting a real allocation between "left" and "right".
2980 * Set the contiguity flags. Don't let extents get too large.
2982 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2983 left.br_startoff + left.br_blockcount == new->br_startoff &&
2984 left.br_startblock + left.br_blockcount == new->br_startblock &&
2985 left.br_state == new->br_state &&
2986 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2987 state |= BMAP_LEFT_CONTIG;
2989 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2990 new->br_startoff + new->br_blockcount == right.br_startoff &&
2991 new->br_startblock + new->br_blockcount == right.br_startblock &&
2992 new->br_state == right.br_state &&
2993 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2994 (!(state & BMAP_LEFT_CONTIG) ||
2995 left.br_blockcount + new->br_blockcount +
2996 right.br_blockcount <= MAXEXTLEN))
2997 state |= BMAP_RIGHT_CONTIG;
3001 * Select which case we're in here, and implement it.
3003 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
3004 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
3006 * New allocation is contiguous with real allocations on the
3007 * left and on the right.
3008 * Merge all three into a single extent record.
3011 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
3012 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
3013 left.br_blockcount + new->br_blockcount +
3014 right.br_blockcount);
3015 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
3017 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
3019 XFS_IFORK_NEXT_SET(bma->ip, whichfork,
3020 XFS_IFORK_NEXTENTS(bma->ip, whichfork) - 1);
3021 if (bma->cur == NULL) {
3022 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
3024 rval = XFS_ILOG_CORE;
3025 error = xfs_bmbt_lookup_eq(bma->cur, right.br_startoff,
3026 right.br_startblock, right.br_blockcount,
3030 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
3031 error = xfs_btree_delete(bma->cur, &i);
3034 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
3035 error = xfs_btree_decrement(bma->cur, 0, &i);
3038 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
3039 error = xfs_bmbt_update(bma->cur, left.br_startoff,
3041 left.br_blockcount +
3042 new->br_blockcount +
3043 right.br_blockcount,
3050 case BMAP_LEFT_CONTIG:
3052 * New allocation is contiguous with a real allocation
3054 * Merge the new allocation with the left neighbor.
3057 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
3058 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
3059 left.br_blockcount + new->br_blockcount);
3060 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
3062 if (bma->cur == NULL) {
3063 rval = xfs_ilog_fext(whichfork);
3066 error = xfs_bmbt_lookup_eq(bma->cur, left.br_startoff,
3067 left.br_startblock, left.br_blockcount,
3071 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
3072 error = xfs_bmbt_update(bma->cur, left.br_startoff,
3074 left.br_blockcount +
3082 case BMAP_RIGHT_CONTIG:
3084 * New allocation is contiguous with a real allocation
3086 * Merge the new allocation with the right neighbor.
3088 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
3089 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, bma->idx),
3090 new->br_startoff, new->br_startblock,
3091 new->br_blockcount + right.br_blockcount,
3093 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
3095 if (bma->cur == NULL) {
3096 rval = xfs_ilog_fext(whichfork);
3099 error = xfs_bmbt_lookup_eq(bma->cur,
3101 right.br_startblock,
3102 right.br_blockcount, &i);
3105 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
3106 error = xfs_bmbt_update(bma->cur, new->br_startoff,
3108 new->br_blockcount +
3109 right.br_blockcount,
3118 * New allocation is not contiguous with another
3120 * Insert a new entry.
3122 xfs_iext_insert(bma->ip, bma->idx, 1, new, state);
3123 XFS_IFORK_NEXT_SET(bma->ip, whichfork,
3124 XFS_IFORK_NEXTENTS(bma->ip, whichfork) + 1);
3125 if (bma->cur == NULL) {
3126 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
3128 rval = XFS_ILOG_CORE;
3129 error = xfs_bmbt_lookup_eq(bma->cur,
3132 new->br_blockcount, &i);
3135 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
3136 bma->cur->bc_rec.b.br_state = new->br_state;
3137 error = xfs_btree_insert(bma->cur, &i);
3140 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
3145 /* convert to a btree if necessary */
3146 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
3147 int tmp_logflags; /* partial log flag return val */
3149 ASSERT(bma->cur == NULL);
3150 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
3151 bma->firstblock, bma->flist, &bma->cur,
3152 0, &tmp_logflags, whichfork);
3153 bma->logflags |= tmp_logflags;
3158 /* clear out the allocated field, done with it now in any case. */
3160 bma->cur->bc_private.b.allocated = 0;
3162 xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork);
3164 bma->logflags |= rval;
3169 * Functions used in the extent read, allocate and remove paths
3173 * Adjust the size of the new extent based on di_extsize and rt extsize.
3176 xfs_bmap_extsize_align(
3178 xfs_bmbt_irec_t *gotp, /* next extent pointer */
3179 xfs_bmbt_irec_t *prevp, /* previous extent pointer */
3180 xfs_extlen_t extsz, /* align to this extent size */
3181 int rt, /* is this a realtime inode? */
3182 int eof, /* is extent at end-of-file? */
3183 int delay, /* creating delalloc extent? */
3184 int convert, /* overwriting unwritten extent? */
3185 xfs_fileoff_t *offp, /* in/out: aligned offset */
3186 xfs_extlen_t *lenp) /* in/out: aligned length */
3188 xfs_fileoff_t orig_off; /* original offset */
3189 xfs_extlen_t orig_alen; /* original length */
3190 xfs_fileoff_t orig_end; /* original off+len */
3191 xfs_fileoff_t nexto; /* next file offset */
3192 xfs_fileoff_t prevo; /* previous file offset */
3193 xfs_fileoff_t align_off; /* temp for offset */
3194 xfs_extlen_t align_alen; /* temp for length */
3195 xfs_extlen_t temp; /* temp for calculations */
3200 orig_off = align_off = *offp;
3201 orig_alen = align_alen = *lenp;
3202 orig_end = orig_off + orig_alen;
3205 * If this request overlaps an existing extent, then don't
3206 * attempt to perform any additional alignment.
3208 if (!delay && !eof &&
3209 (orig_off >= gotp->br_startoff) &&
3210 (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
3215 * If the file offset is unaligned vs. the extent size
3216 * we need to align it. This will be possible unless
3217 * the file was previously written with a kernel that didn't
3218 * perform this alignment, or if a truncate shot us in the
3221 temp = do_mod(orig_off, extsz);
3227 /* Same adjustment for the end of the requested area. */
3228 temp = (align_alen % extsz);
3230 align_alen += extsz - temp;
3233 * For large extent hint sizes, the aligned extent might be larger than
3234 * MAXEXTLEN. In that case, reduce the size by an extsz so that it pulls
3235 * the length back under MAXEXTLEN. The outer allocation loops handle
3236 * short allocation just fine, so it is safe to do this. We only want to
3237 * do it when we are forced to, though, because it means more allocation
3238 * operations are required.
3240 while (align_alen > MAXEXTLEN)
3241 align_alen -= extsz;
3242 ASSERT(align_alen <= MAXEXTLEN);
3245 * If the previous block overlaps with this proposed allocation
3246 * then move the start forward without adjusting the length.
3248 if (prevp->br_startoff != NULLFILEOFF) {
3249 if (prevp->br_startblock == HOLESTARTBLOCK)
3250 prevo = prevp->br_startoff;
3252 prevo = prevp->br_startoff + prevp->br_blockcount;
3255 if (align_off != orig_off && align_off < prevo)
3258 * If the next block overlaps with this proposed allocation
3259 * then move the start back without adjusting the length,
3260 * but not before offset 0.
3261 * This may of course make the start overlap previous block,
3262 * and if we hit the offset 0 limit then the next block
3263 * can still overlap too.
3265 if (!eof && gotp->br_startoff != NULLFILEOFF) {
3266 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
3267 (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
3268 nexto = gotp->br_startoff + gotp->br_blockcount;
3270 nexto = gotp->br_startoff;
3272 nexto = NULLFILEOFF;
3274 align_off + align_alen != orig_end &&
3275 align_off + align_alen > nexto)
3276 align_off = nexto > align_alen ? nexto - align_alen : 0;
3278 * If we're now overlapping the next or previous extent that
3279 * means we can't fit an extsz piece in this hole. Just move
3280 * the start forward to the first valid spot and set
3281 * the length so we hit the end.
3283 if (align_off != orig_off && align_off < prevo)
3285 if (align_off + align_alen != orig_end &&
3286 align_off + align_alen > nexto &&
3287 nexto != NULLFILEOFF) {
3288 ASSERT(nexto > prevo);
3289 align_alen = nexto - align_off;
3293 * If realtime, and the result isn't a multiple of the realtime
3294 * extent size we need to remove blocks until it is.
3296 if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
3298 * We're not covering the original request, or
3299 * we won't be able to once we fix the length.
3301 if (orig_off < align_off ||
3302 orig_end > align_off + align_alen ||
3303 align_alen - temp < orig_alen)
3306 * Try to fix it by moving the start up.
3308 if (align_off + temp <= orig_off) {
3313 * Try to fix it by moving the end in.
3315 else if (align_off + align_alen - temp >= orig_end)
3318 * Set the start to the minimum then trim the length.
3321 align_alen -= orig_off - align_off;
3322 align_off = orig_off;
3323 align_alen -= align_alen % mp->m_sb.sb_rextsize;
3326 * Result doesn't cover the request, fail it.
3328 if (orig_off < align_off || orig_end > align_off + align_alen)
3331 ASSERT(orig_off >= align_off);
3332 /* see MAXEXTLEN handling above */
3333 ASSERT(orig_end <= align_off + align_alen ||
3334 align_alen + extsz > MAXEXTLEN);
3338 if (!eof && gotp->br_startoff != NULLFILEOFF)
3339 ASSERT(align_off + align_alen <= gotp->br_startoff);
3340 if (prevp->br_startoff != NULLFILEOFF)
3341 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
3349 #define XFS_ALLOC_GAP_UNITS 4
3353 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
3355 xfs_fsblock_t adjust; /* adjustment to block numbers */
3356 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
3357 xfs_mount_t *mp; /* mount point structure */
3358 int nullfb; /* true if ap->firstblock isn't set */
3359 int rt; /* true if inode is realtime */
3361 #define ISVALID(x,y) \
3363 (x) < mp->m_sb.sb_rblocks : \
3364 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
3365 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
3366 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
3368 mp = ap->ip->i_mount;
3369 nullfb = *ap->firstblock == NULLFSBLOCK;
3370 rt = XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata;
3371 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
3373 * If allocating at eof, and there's a previous real block,
3374 * try to use its last block as our starting point.
3376 if (ap->eof && ap->prev.br_startoff != NULLFILEOFF &&
3377 !isnullstartblock(ap->prev.br_startblock) &&
3378 ISVALID(ap->prev.br_startblock + ap->prev.br_blockcount,
3379 ap->prev.br_startblock)) {
3380 ap->blkno = ap->prev.br_startblock + ap->prev.br_blockcount;
3382 * Adjust for the gap between prevp and us.
3384 adjust = ap->offset -
3385 (ap->prev.br_startoff + ap->prev.br_blockcount);
3387 ISVALID(ap->blkno + adjust, ap->prev.br_startblock))
3388 ap->blkno += adjust;
3391 * If not at eof, then compare the two neighbor blocks.
3392 * Figure out whether either one gives us a good starting point,
3393 * and pick the better one.
3395 else if (!ap->eof) {
3396 xfs_fsblock_t gotbno; /* right side block number */
3397 xfs_fsblock_t gotdiff=0; /* right side difference */
3398 xfs_fsblock_t prevbno; /* left side block number */
3399 xfs_fsblock_t prevdiff=0; /* left side difference */
3402 * If there's a previous (left) block, select a requested
3403 * start block based on it.
3405 if (ap->prev.br_startoff != NULLFILEOFF &&
3406 !isnullstartblock(ap->prev.br_startblock) &&
3407 (prevbno = ap->prev.br_startblock +
3408 ap->prev.br_blockcount) &&
3409 ISVALID(prevbno, ap->prev.br_startblock)) {
3411 * Calculate gap to end of previous block.
3413 adjust = prevdiff = ap->offset -
3414 (ap->prev.br_startoff +
3415 ap->prev.br_blockcount);
3417 * Figure the startblock based on the previous block's
3418 * end and the gap size.
3420 * If the gap is large relative to the piece we're
3421 * allocating, or using it gives us an invalid block
3422 * number, then just use the end of the previous block.
3424 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
3425 ISVALID(prevbno + prevdiff,
3426 ap->prev.br_startblock))
3431 * If the firstblock forbids it, can't use it,
3434 if (!rt && !nullfb &&
3435 XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
3436 prevbno = NULLFSBLOCK;
3439 * No previous block or can't follow it, just default.
3442 prevbno = NULLFSBLOCK;
3444 * If there's a following (right) block, select a requested
3445 * start block based on it.
3447 if (!isnullstartblock(ap->got.br_startblock)) {
3449 * Calculate gap to start of next block.
3451 adjust = gotdiff = ap->got.br_startoff - ap->offset;
3453 * Figure the startblock based on the next block's
3454 * start and the gap size.
3456 gotbno = ap->got.br_startblock;
3459 * If the gap is large relative to the piece we're
3460 * allocating, or using it gives us an invalid block
3461 * number, then just use the start of the next block
3462 * offset by our length.
3464 if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
3465 ISVALID(gotbno - gotdiff, gotbno))
3467 else if (ISVALID(gotbno - ap->length, gotbno)) {
3468 gotbno -= ap->length;
3469 gotdiff += adjust - ap->length;
3473 * If the firstblock forbids it, can't use it,
3476 if (!rt && !nullfb &&
3477 XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
3478 gotbno = NULLFSBLOCK;
3481 * No next block, just default.
3484 gotbno = NULLFSBLOCK;
3486 * If both valid, pick the better one, else the only good
3487 * one, else ap->blkno is already set (to 0 or the inode block).
3489 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
3490 ap->blkno = prevdiff <= gotdiff ? prevbno : gotbno;
3491 else if (prevbno != NULLFSBLOCK)
3492 ap->blkno = prevbno;
3493 else if (gotbno != NULLFSBLOCK)
3500 xfs_bmap_longest_free_extent(
3501 struct xfs_trans *tp,
3506 struct xfs_mount *mp = tp->t_mountp;
3507 struct xfs_perag *pag;
3508 xfs_extlen_t longest;
3511 pag = xfs_perag_get(mp, ag);
3512 if (!pag->pagf_init) {
3513 error = xfs_alloc_pagf_init(mp, tp, ag, XFS_ALLOC_FLAG_TRYLOCK);
3517 if (!pag->pagf_init) {
3523 longest = xfs_alloc_longest_free_extent(mp, pag,
3524 xfs_alloc_min_freelist(mp, pag));
3525 if (*blen < longest)
3534 xfs_bmap_select_minlen(
3535 struct xfs_bmalloca *ap,
3536 struct xfs_alloc_arg *args,
3540 if (notinit || *blen < ap->minlen) {
3542 * Since we did a BUF_TRYLOCK above, it is possible that
3543 * there is space for this request.
3545 args->minlen = ap->minlen;
3546 } else if (*blen < args->maxlen) {
3548 * If the best seen length is less than the request length,
3549 * use the best as the minimum.
3551 args->minlen = *blen;
3554 * Otherwise we've seen an extent as big as maxlen, use that
3557 args->minlen = args->maxlen;
3562 xfs_bmap_btalloc_nullfb(
3563 struct xfs_bmalloca *ap,
3564 struct xfs_alloc_arg *args,
3567 struct xfs_mount *mp = ap->ip->i_mount;
3568 xfs_agnumber_t ag, startag;
3572 args->type = XFS_ALLOCTYPE_START_BNO;
3573 args->total = ap->total;
3575 startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3576 if (startag == NULLAGNUMBER)
3579 while (*blen < args->maxlen) {
3580 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3585 if (++ag == mp->m_sb.sb_agcount)
3591 xfs_bmap_select_minlen(ap, args, blen, notinit);
3596 xfs_bmap_btalloc_filestreams(
3597 struct xfs_bmalloca *ap,
3598 struct xfs_alloc_arg *args,
3601 struct xfs_mount *mp = ap->ip->i_mount;
3606 args->type = XFS_ALLOCTYPE_NEAR_BNO;
3607 args->total = ap->total;
3609 ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3610 if (ag == NULLAGNUMBER)
3613 error = xfs_bmap_longest_free_extent(args->tp, ag, blen, ¬init);
3617 if (*blen < args->maxlen) {
3618 error = xfs_filestream_new_ag(ap, &ag);
3622 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3629 xfs_bmap_select_minlen(ap, args, blen, notinit);
3632 * Set the failure fallback case to look in the selected AG as stream
3635 ap->blkno = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0);
3641 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
3643 xfs_mount_t *mp; /* mount point structure */
3644 xfs_alloctype_t atype = 0; /* type for allocation routines */
3645 xfs_extlen_t align; /* minimum allocation alignment */
3646 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
3648 xfs_alloc_arg_t args;
3650 xfs_extlen_t nextminlen = 0;
3651 int nullfb; /* true if ap->firstblock isn't set */
3659 mp = ap->ip->i_mount;
3661 /* stripe alignment for allocation is determined by mount parameters */
3663 if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC))
3664 stripe_align = mp->m_swidth;
3665 else if (mp->m_dalign)
3666 stripe_align = mp->m_dalign;
3668 align = ap->userdata ? xfs_get_extsz_hint(ap->ip) : 0;
3669 if (unlikely(align)) {
3670 error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev,
3671 align, 0, ap->eof, 0, ap->conv,
3672 &ap->offset, &ap->length);
3678 nullfb = *ap->firstblock == NULLFSBLOCK;
3679 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
3681 if (ap->userdata && xfs_inode_is_filestream(ap->ip)) {
3682 ag = xfs_filestream_lookup_ag(ap->ip);
3683 ag = (ag != NULLAGNUMBER) ? ag : 0;
3684 ap->blkno = XFS_AGB_TO_FSB(mp, ag, 0);
3686 ap->blkno = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
3689 ap->blkno = *ap->firstblock;
3691 xfs_bmap_adjacent(ap);
3694 * If allowed, use ap->blkno; otherwise must use firstblock since
3695 * it's in the right allocation group.
3697 if (nullfb || XFS_FSB_TO_AGNO(mp, ap->blkno) == fb_agno)
3700 ap->blkno = *ap->firstblock;
3702 * Normal allocation, done through xfs_alloc_vextent.
3704 tryagain = isaligned = 0;
3705 memset(&args, 0, sizeof(args));
3708 args.fsbno = ap->blkno;
3710 /* Trim the allocation back to the maximum an AG can fit. */
3711 args.maxlen = MIN(ap->length, XFS_ALLOC_AG_MAX_USABLE(mp));
3712 args.firstblock = *ap->firstblock;
3716 * Search for an allocation group with a single extent large
3717 * enough for the request. If one isn't found, then adjust
3718 * the minimum allocation size to the largest space found.
3720 if (ap->userdata && xfs_inode_is_filestream(ap->ip))
3721 error = xfs_bmap_btalloc_filestreams(ap, &args, &blen);
3723 error = xfs_bmap_btalloc_nullfb(ap, &args, &blen);
3726 } else if (ap->flist->xbf_low) {
3727 if (xfs_inode_is_filestream(ap->ip))
3728 args.type = XFS_ALLOCTYPE_FIRST_AG;
3730 args.type = XFS_ALLOCTYPE_START_BNO;
3731 args.total = args.minlen = ap->minlen;
3733 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3734 args.total = ap->total;
3735 args.minlen = ap->minlen;
3737 /* apply extent size hints if obtained earlier */
3738 if (unlikely(align)) {
3740 if ((args.mod = (xfs_extlen_t)do_mod(ap->offset, args.prod)))
3741 args.mod = (xfs_extlen_t)(args.prod - args.mod);
3742 } else if (mp->m_sb.sb_blocksize >= PAGE_CACHE_SIZE) {
3746 args.prod = PAGE_CACHE_SIZE >> mp->m_sb.sb_blocklog;
3747 if ((args.mod = (xfs_extlen_t)(do_mod(ap->offset, args.prod))))
3748 args.mod = (xfs_extlen_t)(args.prod - args.mod);
3751 * If we are not low on available data blocks, and the
3752 * underlying logical volume manager is a stripe, and
3753 * the file offset is zero then try to allocate data
3754 * blocks on stripe unit boundary.
3755 * NOTE: ap->aeof is only set if the allocation length
3756 * is >= the stripe unit and the allocation offset is
3757 * at the end of file.
3759 if (!ap->flist->xbf_low && ap->aeof) {
3761 args.alignment = stripe_align;
3765 * Adjust for alignment
3767 if (blen > args.alignment && blen <= args.maxlen)
3768 args.minlen = blen - args.alignment;
3769 args.minalignslop = 0;
3772 * First try an exact bno allocation.
3773 * If it fails then do a near or start bno
3774 * allocation with alignment turned on.
3778 args.type = XFS_ALLOCTYPE_THIS_BNO;
3781 * Compute the minlen+alignment for the
3782 * next case. Set slop so that the value
3783 * of minlen+alignment+slop doesn't go up
3784 * between the calls.
3786 if (blen > stripe_align && blen <= args.maxlen)
3787 nextminlen = blen - stripe_align;
3789 nextminlen = args.minlen;
3790 if (nextminlen + stripe_align > args.minlen + 1)
3792 nextminlen + stripe_align -
3795 args.minalignslop = 0;
3799 args.minalignslop = 0;
3801 args.minleft = ap->minleft;
3802 args.wasdel = ap->wasdel;
3804 args.userdata = ap->userdata;
3805 if (ap->userdata & XFS_ALLOC_USERDATA_ZERO)
3808 error = xfs_alloc_vextent(&args);
3812 if (tryagain && args.fsbno == NULLFSBLOCK) {
3814 * Exact allocation failed. Now try with alignment
3818 args.fsbno = ap->blkno;
3819 args.alignment = stripe_align;
3820 args.minlen = nextminlen;
3821 args.minalignslop = 0;
3823 if ((error = xfs_alloc_vextent(&args)))
3826 if (isaligned && args.fsbno == NULLFSBLOCK) {
3828 * allocation failed, so turn off alignment and
3832 args.fsbno = ap->blkno;
3834 if ((error = xfs_alloc_vextent(&args)))
3837 if (args.fsbno == NULLFSBLOCK && nullfb &&
3838 args.minlen > ap->minlen) {
3839 args.minlen = ap->minlen;
3840 args.type = XFS_ALLOCTYPE_START_BNO;
3841 args.fsbno = ap->blkno;
3842 if ((error = xfs_alloc_vextent(&args)))
3845 if (args.fsbno == NULLFSBLOCK && nullfb) {
3847 args.type = XFS_ALLOCTYPE_FIRST_AG;
3848 args.total = ap->minlen;
3850 if ((error = xfs_alloc_vextent(&args)))
3852 ap->flist->xbf_low = 1;
3854 if (args.fsbno != NULLFSBLOCK) {
3856 * check the allocation happened at the same or higher AG than
3857 * the first block that was allocated.
3859 ASSERT(*ap->firstblock == NULLFSBLOCK ||
3860 XFS_FSB_TO_AGNO(mp, *ap->firstblock) ==
3861 XFS_FSB_TO_AGNO(mp, args.fsbno) ||
3862 (ap->flist->xbf_low &&
3863 XFS_FSB_TO_AGNO(mp, *ap->firstblock) <
3864 XFS_FSB_TO_AGNO(mp, args.fsbno)));
3866 ap->blkno = args.fsbno;
3867 if (*ap->firstblock == NULLFSBLOCK)
3868 *ap->firstblock = args.fsbno;
3869 ASSERT(nullfb || fb_agno == args.agno ||
3870 (ap->flist->xbf_low && fb_agno < args.agno));
3871 ap->length = args.len;
3872 ap->ip->i_d.di_nblocks += args.len;
3873 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
3875 ap->ip->i_delayed_blks -= args.len;
3877 * Adjust the disk quota also. This was reserved
3880 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
3881 ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT :
3882 XFS_TRANS_DQ_BCOUNT,
3885 ap->blkno = NULLFSBLOCK;
3892 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
3893 * It figures out where to ask the underlying allocator to put the new extent.
3897 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
3899 if (XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata)
3900 return xfs_bmap_rtalloc(ap);
3901 return xfs_bmap_btalloc(ap);
3905 * Trim the returned map to the required bounds
3909 struct xfs_bmbt_irec *mval,
3910 struct xfs_bmbt_irec *got,
3918 if ((flags & XFS_BMAPI_ENTIRE) ||
3919 got->br_startoff + got->br_blockcount <= obno) {
3921 if (isnullstartblock(got->br_startblock))
3922 mval->br_startblock = DELAYSTARTBLOCK;
3928 ASSERT((*bno >= obno) || (n == 0));
3930 mval->br_startoff = *bno;
3931 if (isnullstartblock(got->br_startblock))
3932 mval->br_startblock = DELAYSTARTBLOCK;
3934 mval->br_startblock = got->br_startblock +
3935 (*bno - got->br_startoff);
3937 * Return the minimum of what we got and what we asked for for
3938 * the length. We can use the len variable here because it is
3939 * modified below and we could have been there before coming
3940 * here if the first part of the allocation didn't overlap what
3943 mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno,
3944 got->br_blockcount - (*bno - got->br_startoff));
3945 mval->br_state = got->br_state;
3946 ASSERT(mval->br_blockcount <= len);
3951 * Update and validate the extent map to return
3954 xfs_bmapi_update_map(
3955 struct xfs_bmbt_irec **map,
3963 xfs_bmbt_irec_t *mval = *map;
3965 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
3966 ((mval->br_startoff + mval->br_blockcount) <= end));
3967 ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) ||
3968 (mval->br_startoff < obno));
3970 *bno = mval->br_startoff + mval->br_blockcount;
3972 if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) {
3973 /* update previous map with new information */
3974 ASSERT(mval->br_startblock == mval[-1].br_startblock);
3975 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
3976 ASSERT(mval->br_state == mval[-1].br_state);
3977 mval[-1].br_blockcount = mval->br_blockcount;
3978 mval[-1].br_state = mval->br_state;
3979 } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
3980 mval[-1].br_startblock != DELAYSTARTBLOCK &&
3981 mval[-1].br_startblock != HOLESTARTBLOCK &&
3982 mval->br_startblock == mval[-1].br_startblock +
3983 mval[-1].br_blockcount &&
3984 ((flags & XFS_BMAPI_IGSTATE) ||
3985 mval[-1].br_state == mval->br_state)) {
3986 ASSERT(mval->br_startoff ==
3987 mval[-1].br_startoff + mval[-1].br_blockcount);
3988 mval[-1].br_blockcount += mval->br_blockcount;
3989 } else if (*n > 0 &&
3990 mval->br_startblock == DELAYSTARTBLOCK &&
3991 mval[-1].br_startblock == DELAYSTARTBLOCK &&
3992 mval->br_startoff ==
3993 mval[-1].br_startoff + mval[-1].br_blockcount) {
3994 mval[-1].br_blockcount += mval->br_blockcount;
3995 mval[-1].br_state = mval->br_state;
3996 } else if (!((*n == 0) &&
3997 ((mval->br_startoff + mval->br_blockcount) <=
4006 * Map file blocks to filesystem blocks without allocation.
4010 struct xfs_inode *ip,
4013 struct xfs_bmbt_irec *mval,
4017 struct xfs_mount *mp = ip->i_mount;
4018 struct xfs_ifork *ifp;
4019 struct xfs_bmbt_irec got;
4020 struct xfs_bmbt_irec prev;
4027 int whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
4028 XFS_ATTR_FORK : XFS_DATA_FORK;
4031 ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK|XFS_BMAPI_ENTIRE|
4032 XFS_BMAPI_IGSTATE)));
4033 ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED|XFS_ILOCK_EXCL));
4035 if (unlikely(XFS_TEST_ERROR(
4036 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4037 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
4038 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
4039 XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW, mp);
4040 return -EFSCORRUPTED;
4043 if (XFS_FORCED_SHUTDOWN(mp))
4046 XFS_STATS_INC(mp, xs_blk_mapr);
4048 ifp = XFS_IFORK_PTR(ip, whichfork);
4050 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4051 error = xfs_iread_extents(NULL, ip, whichfork);
4056 xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got, &prev);
4060 while (bno < end && n < *nmap) {
4061 /* Reading past eof, act as though there's a hole up to end. */
4063 got.br_startoff = end;
4064 if (got.br_startoff > bno) {
4065 /* Reading in a hole. */
4066 mval->br_startoff = bno;
4067 mval->br_startblock = HOLESTARTBLOCK;
4068 mval->br_blockcount =
4069 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
4070 mval->br_state = XFS_EXT_NORM;
4071 bno += mval->br_blockcount;
4072 len -= mval->br_blockcount;
4078 /* set up the extent map to return. */
4079 xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
4080 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4082 /* If we're done, stop now. */
4083 if (bno >= end || n >= *nmap)
4086 /* Else go on to the next record. */
4087 if (++lastx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t))
4088 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx), &got);
4097 xfs_bmapi_reserve_delalloc(
4098 struct xfs_inode *ip,
4101 struct xfs_bmbt_irec *got,
4102 struct xfs_bmbt_irec *prev,
4103 xfs_extnum_t *lastx,
4106 struct xfs_mount *mp = ip->i_mount;
4107 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
4109 xfs_extlen_t indlen;
4110 char rt = XFS_IS_REALTIME_INODE(ip);
4114 alen = XFS_FILBLKS_MIN(len, MAXEXTLEN);
4116 alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff);
4118 /* Figure out the extent size, adjust alen */
4119 extsz = xfs_get_extsz_hint(ip);
4121 error = xfs_bmap_extsize_align(mp, got, prev, extsz, rt, eof,
4122 1, 0, &aoff, &alen);
4127 extsz = alen / mp->m_sb.sb_rextsize;
4130 * Make a transaction-less quota reservation for delayed allocation
4131 * blocks. This number gets adjusted later. We return if we haven't
4132 * allocated blocks already inside this loop.
4134 error = xfs_trans_reserve_quota_nblks(NULL, ip, (long)alen, 0,
4135 rt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
4140 * Split changing sb for alen and indlen since they could be coming
4141 * from different places.
4143 indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen);
4147 error = xfs_mod_frextents(mp, -((int64_t)extsz));
4149 error = xfs_mod_fdblocks(mp, -((int64_t)alen), false);
4153 goto out_unreserve_quota;
4155 error = xfs_mod_fdblocks(mp, -((int64_t)indlen), false);
4157 goto out_unreserve_blocks;
4160 ip->i_delayed_blks += alen;
4162 got->br_startoff = aoff;
4163 got->br_startblock = nullstartblock(indlen);
4164 got->br_blockcount = alen;
4165 got->br_state = XFS_EXT_NORM;
4166 xfs_bmap_add_extent_hole_delay(ip, lastx, got);
4169 * Update our extent pointer, given that xfs_bmap_add_extent_hole_delay
4170 * might have merged it into one of the neighbouring ones.
4172 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *lastx), got);
4174 ASSERT(got->br_startoff <= aoff);
4175 ASSERT(got->br_startoff + got->br_blockcount >= aoff + alen);
4176 ASSERT(isnullstartblock(got->br_startblock));
4177 ASSERT(got->br_state == XFS_EXT_NORM);
4180 out_unreserve_blocks:
4182 xfs_mod_frextents(mp, extsz);
4184 xfs_mod_fdblocks(mp, alen, false);
4185 out_unreserve_quota:
4186 if (XFS_IS_QUOTA_ON(mp))
4187 xfs_trans_unreserve_quota_nblks(NULL, ip, (long)alen, 0, rt ?
4188 XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
4193 * Map file blocks to filesystem blocks, adding delayed allocations as needed.
4197 struct xfs_inode *ip, /* incore inode */
4198 xfs_fileoff_t bno, /* starting file offs. mapped */
4199 xfs_filblks_t len, /* length to map in file */
4200 struct xfs_bmbt_irec *mval, /* output: map values */
4201 int *nmap, /* i/o: mval size/count */
4202 int flags) /* XFS_BMAPI_... */
4204 struct xfs_mount *mp = ip->i_mount;
4205 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
4206 struct xfs_bmbt_irec got; /* current file extent record */
4207 struct xfs_bmbt_irec prev; /* previous file extent record */
4208 xfs_fileoff_t obno; /* old block number (offset) */
4209 xfs_fileoff_t end; /* end of mapped file region */
4210 xfs_extnum_t lastx; /* last useful extent number */
4211 int eof; /* we've hit the end of extents */
4212 int n = 0; /* current extent index */
4216 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP);
4217 ASSERT(!(flags & ~XFS_BMAPI_ENTIRE));
4218 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
4220 if (unlikely(XFS_TEST_ERROR(
4221 (XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_EXTENTS &&
4222 XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_BTREE),
4223 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
4224 XFS_ERROR_REPORT("xfs_bmapi_delay", XFS_ERRLEVEL_LOW, mp);
4225 return -EFSCORRUPTED;
4228 if (XFS_FORCED_SHUTDOWN(mp))
4231 XFS_STATS_INC(mp, xs_blk_mapw);
4233 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4234 error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK);
4239 xfs_bmap_search_extents(ip, bno, XFS_DATA_FORK, &eof, &lastx, &got, &prev);
4243 while (bno < end && n < *nmap) {
4244 if (eof || got.br_startoff > bno) {
4245 error = xfs_bmapi_reserve_delalloc(ip, bno, len, &got,
4246 &prev, &lastx, eof);
4256 /* set up the extent map to return. */
4257 xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
4258 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4260 /* If we're done, stop now. */
4261 if (bno >= end || n >= *nmap)
4264 /* Else go on to the next record. */
4266 if (++lastx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t))
4267 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx), &got);
4279 struct xfs_bmalloca *bma)
4281 struct xfs_mount *mp = bma->ip->i_mount;
4282 int whichfork = (bma->flags & XFS_BMAPI_ATTRFORK) ?
4283 XFS_ATTR_FORK : XFS_DATA_FORK;
4284 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
4285 int tmp_logflags = 0;
4288 ASSERT(bma->length > 0);
4291 * For the wasdelay case, we could also just allocate the stuff asked
4292 * for in this bmap call but that wouldn't be as good.
4295 bma->length = (xfs_extlen_t)bma->got.br_blockcount;
4296 bma->offset = bma->got.br_startoff;
4297 if (bma->idx != NULLEXTNUM && bma->idx) {
4298 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1),
4302 bma->length = XFS_FILBLKS_MIN(bma->length, MAXEXTLEN);
4304 bma->length = XFS_FILBLKS_MIN(bma->length,
4305 bma->got.br_startoff - bma->offset);
4309 * Indicate if this is the first user data in the file, or just any
4310 * user data. And if it is userdata, indicate whether it needs to
4311 * be initialised to zero during allocation.
4313 if (!(bma->flags & XFS_BMAPI_METADATA)) {
4314 bma->userdata = (bma->offset == 0) ?
4315 XFS_ALLOC_INITIAL_USER_DATA : XFS_ALLOC_USERDATA;
4316 if (bma->flags & XFS_BMAPI_ZERO)
4317 bma->userdata |= XFS_ALLOC_USERDATA_ZERO;
4320 bma->minlen = (bma->flags & XFS_BMAPI_CONTIG) ? bma->length : 1;
4323 * Only want to do the alignment at the eof if it is userdata and
4324 * allocation length is larger than a stripe unit.
4326 if (mp->m_dalign && bma->length >= mp->m_dalign &&
4327 !(bma->flags & XFS_BMAPI_METADATA) && whichfork == XFS_DATA_FORK) {
4328 error = xfs_bmap_isaeof(bma, whichfork);
4333 error = xfs_bmap_alloc(bma);
4337 if (bma->flist->xbf_low)
4340 bma->cur->bc_private.b.firstblock = *bma->firstblock;
4341 if (bma->blkno == NULLFSBLOCK)
4343 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4344 bma->cur = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork);
4345 bma->cur->bc_private.b.firstblock = *bma->firstblock;
4346 bma->cur->bc_private.b.flist = bma->flist;
4349 * Bump the number of extents we've allocated
4355 bma->cur->bc_private.b.flags =
4356 bma->wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
4358 bma->got.br_startoff = bma->offset;
4359 bma->got.br_startblock = bma->blkno;
4360 bma->got.br_blockcount = bma->length;
4361 bma->got.br_state = XFS_EXT_NORM;
4364 * A wasdelay extent has been initialized, so shouldn't be flagged
4367 if (!bma->wasdel && (bma->flags & XFS_BMAPI_PREALLOC) &&
4368 xfs_sb_version_hasextflgbit(&mp->m_sb))
4369 bma->got.br_state = XFS_EXT_UNWRITTEN;
4372 error = xfs_bmap_add_extent_delay_real(bma);
4374 error = xfs_bmap_add_extent_hole_real(bma, whichfork);
4376 bma->logflags |= tmp_logflags;
4381 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4382 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4383 * the neighbouring ones.
4385 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &bma->got);
4387 ASSERT(bma->got.br_startoff <= bma->offset);
4388 ASSERT(bma->got.br_startoff + bma->got.br_blockcount >=
4389 bma->offset + bma->length);
4390 ASSERT(bma->got.br_state == XFS_EXT_NORM ||
4391 bma->got.br_state == XFS_EXT_UNWRITTEN);
4396 xfs_bmapi_convert_unwritten(
4397 struct xfs_bmalloca *bma,
4398 struct xfs_bmbt_irec *mval,
4402 int whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
4403 XFS_ATTR_FORK : XFS_DATA_FORK;
4404 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
4405 int tmp_logflags = 0;
4408 /* check if we need to do unwritten->real conversion */
4409 if (mval->br_state == XFS_EXT_UNWRITTEN &&
4410 (flags & XFS_BMAPI_PREALLOC))
4413 /* check if we need to do real->unwritten conversion */
4414 if (mval->br_state == XFS_EXT_NORM &&
4415 (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) !=
4416 (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT))
4420 * Modify (by adding) the state flag, if writing.
4422 ASSERT(mval->br_blockcount <= len);
4423 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4424 bma->cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp,
4425 bma->ip, whichfork);
4426 bma->cur->bc_private.b.firstblock = *bma->firstblock;
4427 bma->cur->bc_private.b.flist = bma->flist;
4429 mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN)
4430 ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
4433 * Before insertion into the bmbt, zero the range being converted
4436 if (flags & XFS_BMAPI_ZERO) {
4437 error = xfs_zero_extent(bma->ip, mval->br_startblock,
4438 mval->br_blockcount);
4443 error = xfs_bmap_add_extent_unwritten_real(bma->tp, bma->ip, &bma->idx,
4444 &bma->cur, mval, bma->firstblock, bma->flist,
4447 * Log the inode core unconditionally in the unwritten extent conversion
4448 * path because the conversion might not have done so (e.g., if the
4449 * extent count hasn't changed). We need to make sure the inode is dirty
4450 * in the transaction for the sake of fsync(), even if nothing has
4451 * changed, because fsync() will not force the log for this transaction
4452 * unless it sees the inode pinned.
4454 bma->logflags |= tmp_logflags | XFS_ILOG_CORE;
4459 * Update our extent pointer, given that
4460 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4461 * of the neighbouring ones.
4463 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &bma->got);
4466 * We may have combined previously unwritten space with written space,
4467 * so generate another request.
4469 if (mval->br_blockcount < len)
4475 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4476 * extent state if necessary. Details behaviour is controlled by the flags
4477 * parameter. Only allocates blocks from a single allocation group, to avoid
4480 * The returned value in "firstblock" from the first call in a transaction
4481 * must be remembered and presented to subsequent calls in "firstblock".
4482 * An upper bound for the number of blocks to be allocated is supplied to
4483 * the first call in "total"; if no allocation group has that many free
4484 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4488 struct xfs_trans *tp, /* transaction pointer */
4489 struct xfs_inode *ip, /* incore inode */
4490 xfs_fileoff_t bno, /* starting file offs. mapped */
4491 xfs_filblks_t len, /* length to map in file */
4492 int flags, /* XFS_BMAPI_... */
4493 xfs_fsblock_t *firstblock, /* first allocated block
4494 controls a.g. for allocs */
4495 xfs_extlen_t total, /* total blocks needed */
4496 struct xfs_bmbt_irec *mval, /* output: map values */
4497 int *nmap, /* i/o: mval size/count */
4498 struct xfs_bmap_free *flist) /* i/o: list extents to free */
4500 struct xfs_mount *mp = ip->i_mount;
4501 struct xfs_ifork *ifp;
4502 struct xfs_bmalloca bma = { NULL }; /* args for xfs_bmap_alloc */
4503 xfs_fileoff_t end; /* end of mapped file region */
4504 int eof; /* after the end of extents */
4505 int error; /* error return */
4506 int n; /* current extent index */
4507 xfs_fileoff_t obno; /* old block number (offset) */
4508 int whichfork; /* data or attr fork */
4509 char inhole; /* current location is hole in file */
4510 char wasdelay; /* old extent was delayed */
4513 xfs_fileoff_t orig_bno; /* original block number value */
4514 int orig_flags; /* original flags arg value */
4515 xfs_filblks_t orig_len; /* original value of len arg */
4516 struct xfs_bmbt_irec *orig_mval; /* original value of mval */
4517 int orig_nmap; /* original value of *nmap */
4525 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
4526 XFS_ATTR_FORK : XFS_DATA_FORK;
4529 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP);
4530 ASSERT(!(flags & XFS_BMAPI_IGSTATE));
4533 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL);
4534 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
4536 /* zeroing is for currently only for data extents, not metadata */
4537 ASSERT((flags & (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO)) !=
4538 (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO));
4540 * we can allocate unwritten extents or pre-zero allocated blocks,
4541 * but it makes no sense to do both at once. This would result in
4542 * zeroing the unwritten extent twice, but it still being an
4543 * unwritten extent....
4545 ASSERT((flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)) !=
4546 (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO));
4548 if (unlikely(XFS_TEST_ERROR(
4549 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4550 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
4551 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
4552 XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW, mp);
4553 return -EFSCORRUPTED;
4556 if (XFS_FORCED_SHUTDOWN(mp))
4559 ifp = XFS_IFORK_PTR(ip, whichfork);
4561 XFS_STATS_INC(mp, xs_blk_mapw);
4563 if (*firstblock == NULLFSBLOCK) {
4564 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE)
4565 bma.minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1;
4572 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4573 error = xfs_iread_extents(tp, ip, whichfork);
4578 xfs_bmap_search_extents(ip, bno, whichfork, &eof, &bma.idx, &bma.got,
4589 bma.firstblock = firstblock;
4591 while (bno < end && n < *nmap) {
4592 inhole = eof || bma.got.br_startoff > bno;
4593 wasdelay = !inhole && isnullstartblock(bma.got.br_startblock);
4596 * First, deal with the hole before the allocated space
4597 * that we found, if any.
4599 if (inhole || wasdelay) {
4601 bma.conv = !!(flags & XFS_BMAPI_CONVERT);
4602 bma.wasdel = wasdelay;
4607 * There's a 32/64 bit type mismatch between the
4608 * allocation length request (which can be 64 bits in
4609 * length) and the bma length request, which is
4610 * xfs_extlen_t and therefore 32 bits. Hence we have to
4611 * check for 32-bit overflows and handle them here.
4613 if (len > (xfs_filblks_t)MAXEXTLEN)
4614 bma.length = MAXEXTLEN;
4619 ASSERT(bma.length > 0);
4620 error = xfs_bmapi_allocate(&bma);
4623 if (bma.blkno == NULLFSBLOCK)
4627 /* Deal with the allocated space we found. */
4628 xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno,
4631 /* Execute unwritten extent conversion if necessary */
4632 error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags);
4633 if (error == -EAGAIN)
4638 /* update the extent map to return */
4639 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4642 * If we're done, stop now. Stop when we've allocated
4643 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4644 * the transaction may get too big.
4646 if (bno >= end || n >= *nmap || bma.nallocs >= *nmap)
4649 /* Else go on to the next record. */
4651 if (++bma.idx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t)) {
4652 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma.idx),
4660 * Transform from btree to extents, give it cur.
4662 if (xfs_bmap_wants_extents(ip, whichfork)) {
4663 int tmp_logflags = 0;
4666 error = xfs_bmap_btree_to_extents(tp, ip, bma.cur,
4667 &tmp_logflags, whichfork);
4668 bma.logflags |= tmp_logflags;
4673 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE ||
4674 XFS_IFORK_NEXTENTS(ip, whichfork) >
4675 XFS_IFORK_MAXEXT(ip, whichfork));
4679 * Log everything. Do this after conversion, there's no point in
4680 * logging the extent records if we've converted to btree format.
4682 if ((bma.logflags & xfs_ilog_fext(whichfork)) &&
4683 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
4684 bma.logflags &= ~xfs_ilog_fext(whichfork);
4685 else if ((bma.logflags & xfs_ilog_fbroot(whichfork)) &&
4686 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
4687 bma.logflags &= ~xfs_ilog_fbroot(whichfork);
4689 * Log whatever the flags say, even if error. Otherwise we might miss
4690 * detecting a case where the data is changed, there's an error,
4691 * and it's not logged so we don't shutdown when we should.
4694 xfs_trans_log_inode(tp, ip, bma.logflags);
4698 ASSERT(*firstblock == NULLFSBLOCK ||
4699 XFS_FSB_TO_AGNO(mp, *firstblock) ==
4701 bma.cur->bc_private.b.firstblock) ||
4703 XFS_FSB_TO_AGNO(mp, *firstblock) <
4705 bma.cur->bc_private.b.firstblock)));
4706 *firstblock = bma.cur->bc_private.b.firstblock;
4708 xfs_btree_del_cursor(bma.cur,
4709 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
4712 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
4718 * Called by xfs_bmapi to update file extent records and the btree
4719 * after removing space (or undoing a delayed allocation).
4721 STATIC int /* error */
4722 xfs_bmap_del_extent(
4723 xfs_inode_t *ip, /* incore inode pointer */
4724 xfs_trans_t *tp, /* current transaction pointer */
4725 xfs_extnum_t *idx, /* extent number to update/delete */
4726 xfs_bmap_free_t *flist, /* list of extents to be freed */
4727 xfs_btree_cur_t *cur, /* if null, not a btree */
4728 xfs_bmbt_irec_t *del, /* data to remove from extents */
4729 int *logflagsp, /* inode logging flags */
4730 int whichfork) /* data or attr fork */
4732 xfs_filblks_t da_new; /* new delay-alloc indirect blocks */
4733 xfs_filblks_t da_old; /* old delay-alloc indirect blocks */
4734 xfs_fsblock_t del_endblock=0; /* first block past del */
4735 xfs_fileoff_t del_endoff; /* first offset past del */
4736 int delay; /* current block is delayed allocated */
4737 int do_fx; /* free extent at end of routine */
4738 xfs_bmbt_rec_host_t *ep; /* current extent entry pointer */
4739 int error; /* error return value */
4740 int flags; /* inode logging flags */
4741 xfs_bmbt_irec_t got; /* current extent entry */
4742 xfs_fileoff_t got_endoff; /* first offset past got */
4743 int i; /* temp state */
4744 xfs_ifork_t *ifp; /* inode fork pointer */
4745 xfs_mount_t *mp; /* mount structure */
4746 xfs_filblks_t nblks; /* quota/sb block count */
4747 xfs_bmbt_irec_t new; /* new record to be inserted */
4749 uint qfield; /* quota field to update */
4750 xfs_filblks_t temp; /* for indirect length calculations */
4751 xfs_filblks_t temp2; /* for indirect length calculations */
4755 XFS_STATS_INC(mp, xs_del_exlist);
4757 if (whichfork == XFS_ATTR_FORK)
4758 state |= BMAP_ATTRFORK;
4760 ifp = XFS_IFORK_PTR(ip, whichfork);
4761 ASSERT((*idx >= 0) && (*idx < ifp->if_bytes /
4762 (uint)sizeof(xfs_bmbt_rec_t)));
4763 ASSERT(del->br_blockcount > 0);
4764 ep = xfs_iext_get_ext(ifp, *idx);
4765 xfs_bmbt_get_all(ep, &got);
4766 ASSERT(got.br_startoff <= del->br_startoff);
4767 del_endoff = del->br_startoff + del->br_blockcount;
4768 got_endoff = got.br_startoff + got.br_blockcount;
4769 ASSERT(got_endoff >= del_endoff);
4770 delay = isnullstartblock(got.br_startblock);
4771 ASSERT(isnullstartblock(del->br_startblock) == delay);
4776 * If deleting a real allocation, must free up the disk space.
4779 flags = XFS_ILOG_CORE;
4781 * Realtime allocation. Free it and record di_nblocks update.
4783 if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) {
4787 ASSERT(do_mod(del->br_blockcount,
4788 mp->m_sb.sb_rextsize) == 0);
4789 ASSERT(do_mod(del->br_startblock,
4790 mp->m_sb.sb_rextsize) == 0);
4791 bno = del->br_startblock;
4792 len = del->br_blockcount;
4793 do_div(bno, mp->m_sb.sb_rextsize);
4794 do_div(len, mp->m_sb.sb_rextsize);
4795 error = xfs_rtfree_extent(tp, bno, (xfs_extlen_t)len);
4799 nblks = len * mp->m_sb.sb_rextsize;
4800 qfield = XFS_TRANS_DQ_RTBCOUNT;
4803 * Ordinary allocation.
4807 nblks = del->br_blockcount;
4808 qfield = XFS_TRANS_DQ_BCOUNT;
4811 * Set up del_endblock and cur for later.
4813 del_endblock = del->br_startblock + del->br_blockcount;
4815 if ((error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
4816 got.br_startblock, got.br_blockcount,
4819 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
4821 da_old = da_new = 0;
4823 da_old = startblockval(got.br_startblock);
4829 * Set flag value to use in switch statement.
4830 * Left-contig is 2, right-contig is 1.
4832 switch (((got.br_startoff == del->br_startoff) << 1) |
4833 (got_endoff == del_endoff)) {
4836 * Matches the whole extent. Delete the entry.
4838 xfs_iext_remove(ip, *idx, 1,
4839 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0);
4844 XFS_IFORK_NEXT_SET(ip, whichfork,
4845 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
4846 flags |= XFS_ILOG_CORE;
4848 flags |= xfs_ilog_fext(whichfork);
4851 if ((error = xfs_btree_delete(cur, &i)))
4853 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
4858 * Deleting the first part of the extent.
4860 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4861 xfs_bmbt_set_startoff(ep, del_endoff);
4862 temp = got.br_blockcount - del->br_blockcount;
4863 xfs_bmbt_set_blockcount(ep, temp);
4865 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
4867 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
4868 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
4872 xfs_bmbt_set_startblock(ep, del_endblock);
4873 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
4875 flags |= xfs_ilog_fext(whichfork);
4878 if ((error = xfs_bmbt_update(cur, del_endoff, del_endblock,
4879 got.br_blockcount - del->br_blockcount,
4886 * Deleting the last part of the extent.
4888 temp = got.br_blockcount - del->br_blockcount;
4889 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4890 xfs_bmbt_set_blockcount(ep, temp);
4892 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
4894 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
4895 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
4899 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
4901 flags |= xfs_ilog_fext(whichfork);
4904 if ((error = xfs_bmbt_update(cur, got.br_startoff,
4906 got.br_blockcount - del->br_blockcount,
4913 * Deleting the middle of the extent.
4915 temp = del->br_startoff - got.br_startoff;
4916 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4917 xfs_bmbt_set_blockcount(ep, temp);
4918 new.br_startoff = del_endoff;
4919 temp2 = got_endoff - del_endoff;
4920 new.br_blockcount = temp2;
4921 new.br_state = got.br_state;
4923 new.br_startblock = del_endblock;
4924 flags |= XFS_ILOG_CORE;
4926 if ((error = xfs_bmbt_update(cur,
4928 got.br_startblock, temp,
4931 if ((error = xfs_btree_increment(cur, 0, &i)))
4933 cur->bc_rec.b = new;
4934 error = xfs_btree_insert(cur, &i);
4935 if (error && error != -ENOSPC)
4938 * If get no-space back from btree insert,
4939 * it tried a split, and we have a zero
4940 * block reservation.
4941 * Fix up our state and return the error.
4943 if (error == -ENOSPC) {
4945 * Reset the cursor, don't trust
4946 * it after any insert operation.
4948 if ((error = xfs_bmbt_lookup_eq(cur,
4953 XFS_WANT_CORRUPTED_GOTO(mp,
4956 * Update the btree record back
4957 * to the original value.
4959 if ((error = xfs_bmbt_update(cur,
4966 * Reset the extent record back
4967 * to the original value.
4969 xfs_bmbt_set_blockcount(ep,
4975 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
4977 flags |= xfs_ilog_fext(whichfork);
4978 XFS_IFORK_NEXT_SET(ip, whichfork,
4979 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
4981 ASSERT(whichfork == XFS_DATA_FORK);
4982 temp = xfs_bmap_worst_indlen(ip, temp);
4983 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
4984 temp2 = xfs_bmap_worst_indlen(ip, temp2);
4985 new.br_startblock = nullstartblock((int)temp2);
4986 da_new = temp + temp2;
4987 while (da_new > da_old) {
4991 xfs_bmbt_set_startblock(ep,
4992 nullstartblock((int)temp));
4994 if (da_new == da_old)
5000 nullstartblock((int)temp2);
5004 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5005 xfs_iext_insert(ip, *idx + 1, 1, &new, state);
5010 * If we need to, add to list of extents to delete.
5013 xfs_bmap_add_free(del->br_startblock, del->br_blockcount, flist,
5016 * Adjust inode # blocks in the file.
5019 ip->i_d.di_nblocks -= nblks;
5021 * Adjust quota data.
5024 xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks);
5027 * Account for change in delayed indirect blocks.
5028 * Nothing to do for disk quota accounting here.
5030 ASSERT(da_old >= da_new);
5031 if (da_old > da_new)
5032 xfs_mod_fdblocks(mp, (int64_t)(da_old - da_new), false);
5039 * Unmap (remove) blocks from a file.
5040 * If nexts is nonzero then the number of extents to remove is limited to
5041 * that value. If not all extents in the block range can be removed then
5046 xfs_trans_t *tp, /* transaction pointer */
5047 struct xfs_inode *ip, /* incore inode */
5048 xfs_fileoff_t bno, /* starting offset to unmap */
5049 xfs_filblks_t len, /* length to unmap in file */
5050 int flags, /* misc flags */
5051 xfs_extnum_t nexts, /* number of extents max */
5052 xfs_fsblock_t *firstblock, /* first allocated block
5053 controls a.g. for allocs */
5054 xfs_bmap_free_t *flist, /* i/o: list extents to free */
5055 int *done) /* set if not done yet */
5057 xfs_btree_cur_t *cur; /* bmap btree cursor */
5058 xfs_bmbt_irec_t del; /* extent being deleted */
5059 int eof; /* is deleting at eof */
5060 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
5061 int error; /* error return value */
5062 xfs_extnum_t extno; /* extent number in list */
5063 xfs_bmbt_irec_t got; /* current extent record */
5064 xfs_ifork_t *ifp; /* inode fork pointer */
5065 int isrt; /* freeing in rt area */
5066 xfs_extnum_t lastx; /* last extent index used */
5067 int logflags; /* transaction logging flags */
5068 xfs_extlen_t mod; /* rt extent offset */
5069 xfs_mount_t *mp; /* mount structure */
5070 xfs_extnum_t nextents; /* number of file extents */
5071 xfs_bmbt_irec_t prev; /* previous extent record */
5072 xfs_fileoff_t start; /* first file offset deleted */
5073 int tmp_logflags; /* partial logging flags */
5074 int wasdel; /* was a delayed alloc extent */
5075 int whichfork; /* data or attribute fork */
5078 trace_xfs_bunmap(ip, bno, len, flags, _RET_IP_);
5080 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
5081 XFS_ATTR_FORK : XFS_DATA_FORK;
5082 ifp = XFS_IFORK_PTR(ip, whichfork);
5084 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5085 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
5086 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW,
5088 return -EFSCORRUPTED;
5091 if (XFS_FORCED_SHUTDOWN(mp))
5094 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
5098 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5099 (error = xfs_iread_extents(tp, ip, whichfork)))
5101 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5102 if (nextents == 0) {
5106 XFS_STATS_INC(mp, xs_blk_unmap);
5107 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
5109 bno = start + len - 1;
5110 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
5114 * Check to see if the given block number is past the end of the
5115 * file, back up to the last block if so...
5118 ep = xfs_iext_get_ext(ifp, --lastx);
5119 xfs_bmbt_get_all(ep, &got);
5120 bno = got.br_startoff + got.br_blockcount - 1;
5123 if (ifp->if_flags & XFS_IFBROOT) {
5124 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
5125 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5126 cur->bc_private.b.firstblock = *firstblock;
5127 cur->bc_private.b.flist = flist;
5128 cur->bc_private.b.flags = 0;
5134 * Synchronize by locking the bitmap inode.
5136 xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL);
5137 xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL);
5141 while (bno != (xfs_fileoff_t)-1 && bno >= start && lastx >= 0 &&
5142 (nexts == 0 || extno < nexts)) {
5144 * Is the found extent after a hole in which bno lives?
5145 * Just back up to the previous extent, if so.
5147 if (got.br_startoff > bno) {
5150 ep = xfs_iext_get_ext(ifp, lastx);
5151 xfs_bmbt_get_all(ep, &got);
5154 * Is the last block of this extent before the range
5155 * we're supposed to delete? If so, we're done.
5157 bno = XFS_FILEOFF_MIN(bno,
5158 got.br_startoff + got.br_blockcount - 1);
5162 * Then deal with the (possibly delayed) allocated space
5167 wasdel = isnullstartblock(del.br_startblock);
5168 if (got.br_startoff < start) {
5169 del.br_startoff = start;
5170 del.br_blockcount -= start - got.br_startoff;
5172 del.br_startblock += start - got.br_startoff;
5174 if (del.br_startoff + del.br_blockcount > bno + 1)
5175 del.br_blockcount = bno + 1 - del.br_startoff;
5176 sum = del.br_startblock + del.br_blockcount;
5178 (mod = do_mod(sum, mp->m_sb.sb_rextsize))) {
5180 * Realtime extent not lined up at the end.
5181 * The extent could have been split into written
5182 * and unwritten pieces, or we could just be
5183 * unmapping part of it. But we can't really
5184 * get rid of part of a realtime extent.
5186 if (del.br_state == XFS_EXT_UNWRITTEN ||
5187 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
5189 * This piece is unwritten, or we're not
5190 * using unwritten extents. Skip over it.
5193 bno -= mod > del.br_blockcount ?
5194 del.br_blockcount : mod;
5195 if (bno < got.br_startoff) {
5197 xfs_bmbt_get_all(xfs_iext_get_ext(
5203 * It's written, turn it unwritten.
5204 * This is better than zeroing it.
5206 ASSERT(del.br_state == XFS_EXT_NORM);
5207 ASSERT(xfs_trans_get_block_res(tp) > 0);
5209 * If this spans a realtime extent boundary,
5210 * chop it back to the start of the one we end at.
5212 if (del.br_blockcount > mod) {
5213 del.br_startoff += del.br_blockcount - mod;
5214 del.br_startblock += del.br_blockcount - mod;
5215 del.br_blockcount = mod;
5217 del.br_state = XFS_EXT_UNWRITTEN;
5218 error = xfs_bmap_add_extent_unwritten_real(tp, ip,
5219 &lastx, &cur, &del, firstblock, flist,
5225 if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) {
5227 * Realtime extent is lined up at the end but not
5228 * at the front. We'll get rid of full extents if
5231 mod = mp->m_sb.sb_rextsize - mod;
5232 if (del.br_blockcount > mod) {
5233 del.br_blockcount -= mod;
5234 del.br_startoff += mod;
5235 del.br_startblock += mod;
5236 } else if ((del.br_startoff == start &&
5237 (del.br_state == XFS_EXT_UNWRITTEN ||
5238 xfs_trans_get_block_res(tp) == 0)) ||
5239 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
5241 * Can't make it unwritten. There isn't
5242 * a full extent here so just skip it.
5244 ASSERT(bno >= del.br_blockcount);
5245 bno -= del.br_blockcount;
5246 if (got.br_startoff > bno) {
5248 ep = xfs_iext_get_ext(ifp,
5250 xfs_bmbt_get_all(ep, &got);
5254 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
5256 * This one is already unwritten.
5257 * It must have a written left neighbor.
5258 * Unwrite the killed part of that one and
5262 xfs_bmbt_get_all(xfs_iext_get_ext(ifp,
5264 ASSERT(prev.br_state == XFS_EXT_NORM);
5265 ASSERT(!isnullstartblock(prev.br_startblock));
5266 ASSERT(del.br_startblock ==
5267 prev.br_startblock + prev.br_blockcount);
5268 if (prev.br_startoff < start) {
5269 mod = start - prev.br_startoff;
5270 prev.br_blockcount -= mod;
5271 prev.br_startblock += mod;
5272 prev.br_startoff = start;
5274 prev.br_state = XFS_EXT_UNWRITTEN;
5276 error = xfs_bmap_add_extent_unwritten_real(tp,
5277 ip, &lastx, &cur, &prev,
5278 firstblock, flist, &logflags);
5283 ASSERT(del.br_state == XFS_EXT_NORM);
5284 del.br_state = XFS_EXT_UNWRITTEN;
5285 error = xfs_bmap_add_extent_unwritten_real(tp,
5286 ip, &lastx, &cur, &del,
5287 firstblock, flist, &logflags);
5294 ASSERT(startblockval(del.br_startblock) > 0);
5295 /* Update realtime/data freespace, unreserve quota */
5297 xfs_filblks_t rtexts;
5299 rtexts = XFS_FSB_TO_B(mp, del.br_blockcount);
5300 do_div(rtexts, mp->m_sb.sb_rextsize);
5301 xfs_mod_frextents(mp, (int64_t)rtexts);
5302 (void)xfs_trans_reserve_quota_nblks(NULL,
5303 ip, -((long)del.br_blockcount), 0,
5304 XFS_QMOPT_RES_RTBLKS);
5306 xfs_mod_fdblocks(mp, (int64_t)del.br_blockcount,
5308 (void)xfs_trans_reserve_quota_nblks(NULL,
5309 ip, -((long)del.br_blockcount), 0,
5310 XFS_QMOPT_RES_REGBLKS);
5312 ip->i_delayed_blks -= del.br_blockcount;
5314 cur->bc_private.b.flags |=
5315 XFS_BTCUR_BPRV_WASDEL;
5317 cur->bc_private.b.flags &= ~XFS_BTCUR_BPRV_WASDEL;
5319 * If it's the case where the directory code is running
5320 * with no block reservation, and the deleted block is in
5321 * the middle of its extent, and the resulting insert
5322 * of an extent would cause transformation to btree format,
5323 * then reject it. The calling code will then swap
5324 * blocks around instead.
5325 * We have to do this now, rather than waiting for the
5326 * conversion to btree format, since the transaction
5329 if (!wasdel && xfs_trans_get_block_res(tp) == 0 &&
5330 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
5331 XFS_IFORK_NEXTENTS(ip, whichfork) >= /* Note the >= */
5332 XFS_IFORK_MAXEXT(ip, whichfork) &&
5333 del.br_startoff > got.br_startoff &&
5334 del.br_startoff + del.br_blockcount <
5335 got.br_startoff + got.br_blockcount) {
5339 error = xfs_bmap_del_extent(ip, tp, &lastx, flist, cur, &del,
5340 &tmp_logflags, whichfork);
5341 logflags |= tmp_logflags;
5344 bno = del.br_startoff - 1;
5347 * If not done go on to the next (previous) record.
5349 if (bno != (xfs_fileoff_t)-1 && bno >= start) {
5351 ep = xfs_iext_get_ext(ifp, lastx);
5352 if (xfs_bmbt_get_startoff(ep) > bno) {
5354 ep = xfs_iext_get_ext(ifp,
5357 xfs_bmbt_get_all(ep, &got);
5362 *done = bno == (xfs_fileoff_t)-1 || bno < start || lastx < 0;
5365 * Convert to a btree if necessary.
5367 if (xfs_bmap_needs_btree(ip, whichfork)) {
5368 ASSERT(cur == NULL);
5369 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist,
5370 &cur, 0, &tmp_logflags, whichfork);
5371 logflags |= tmp_logflags;
5376 * transform from btree to extents, give it cur
5378 else if (xfs_bmap_wants_extents(ip, whichfork)) {
5379 ASSERT(cur != NULL);
5380 error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags,
5382 logflags |= tmp_logflags;
5387 * transform from extents to local?
5392 * Log everything. Do this after conversion, there's no point in
5393 * logging the extent records if we've converted to btree format.
5395 if ((logflags & xfs_ilog_fext(whichfork)) &&
5396 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
5397 logflags &= ~xfs_ilog_fext(whichfork);
5398 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
5399 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
5400 logflags &= ~xfs_ilog_fbroot(whichfork);
5402 * Log inode even in the error case, if the transaction
5403 * is dirty we'll need to shut down the filesystem.
5406 xfs_trans_log_inode(tp, ip, logflags);
5409 *firstblock = cur->bc_private.b.firstblock;
5410 cur->bc_private.b.allocated = 0;
5412 xfs_btree_del_cursor(cur,
5413 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5419 * Determine whether an extent shift can be accomplished by a merge with the
5420 * extent that precedes the target hole of the shift.
5424 struct xfs_bmbt_irec *left, /* preceding extent */
5425 struct xfs_bmbt_irec *got, /* current extent to shift */
5426 xfs_fileoff_t shift) /* shift fsb */
5428 xfs_fileoff_t startoff;
5430 startoff = got->br_startoff - shift;
5433 * The extent, once shifted, must be adjacent in-file and on-disk with
5434 * the preceding extent.
5436 if ((left->br_startoff + left->br_blockcount != startoff) ||
5437 (left->br_startblock + left->br_blockcount != got->br_startblock) ||
5438 (left->br_state != got->br_state) ||
5439 (left->br_blockcount + got->br_blockcount > MAXEXTLEN))
5446 * A bmap extent shift adjusts the file offset of an extent to fill a preceding
5447 * hole in the file. If an extent shift would result in the extent being fully
5448 * adjacent to the extent that currently precedes the hole, we can merge with
5449 * the preceding extent rather than do the shift.
5451 * This function assumes the caller has verified a shift-by-merge is possible
5452 * with the provided extents via xfs_bmse_can_merge().
5456 struct xfs_inode *ip,
5458 xfs_fileoff_t shift, /* shift fsb */
5459 int current_ext, /* idx of gotp */
5460 struct xfs_bmbt_rec_host *gotp, /* extent to shift */
5461 struct xfs_bmbt_rec_host *leftp, /* preceding extent */
5462 struct xfs_btree_cur *cur,
5463 int *logflags) /* output */
5465 struct xfs_bmbt_irec got;
5466 struct xfs_bmbt_irec left;
5467 xfs_filblks_t blockcount;
5469 struct xfs_mount *mp = ip->i_mount;
5471 xfs_bmbt_get_all(gotp, &got);
5472 xfs_bmbt_get_all(leftp, &left);
5473 blockcount = left.br_blockcount + got.br_blockcount;
5475 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
5476 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
5477 ASSERT(xfs_bmse_can_merge(&left, &got, shift));
5480 * Merge the in-core extents. Note that the host record pointers and
5481 * current_ext index are invalid once the extent has been removed via
5482 * xfs_iext_remove().
5484 xfs_bmbt_set_blockcount(leftp, blockcount);
5485 xfs_iext_remove(ip, current_ext, 1, 0);
5488 * Update the on-disk extent count, the btree if necessary and log the
5491 XFS_IFORK_NEXT_SET(ip, whichfork,
5492 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
5493 *logflags |= XFS_ILOG_CORE;
5495 *logflags |= XFS_ILOG_DEXT;
5499 /* lookup and remove the extent to merge */
5500 error = xfs_bmbt_lookup_eq(cur, got.br_startoff, got.br_startblock,
5501 got.br_blockcount, &i);
5504 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
5506 error = xfs_btree_delete(cur, &i);
5509 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
5511 /* lookup and update size of the previous extent */
5512 error = xfs_bmbt_lookup_eq(cur, left.br_startoff, left.br_startblock,
5513 left.br_blockcount, &i);
5516 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
5518 left.br_blockcount = blockcount;
5520 return xfs_bmbt_update(cur, left.br_startoff, left.br_startblock,
5521 left.br_blockcount, left.br_state);
5525 * Shift a single extent.
5529 struct xfs_inode *ip,
5531 xfs_fileoff_t offset_shift_fsb,
5533 struct xfs_bmbt_rec_host *gotp,
5534 struct xfs_btree_cur *cur,
5536 enum shift_direction direction)
5538 struct xfs_ifork *ifp;
5539 struct xfs_mount *mp;
5540 xfs_fileoff_t startoff;
5541 struct xfs_bmbt_rec_host *adj_irecp;
5542 struct xfs_bmbt_irec got;
5543 struct xfs_bmbt_irec adj_irec;
5549 ifp = XFS_IFORK_PTR(ip, whichfork);
5550 total_extents = ifp->if_bytes / sizeof(xfs_bmbt_rec_t);
5552 xfs_bmbt_get_all(gotp, &got);
5554 /* delalloc extents should be prevented by caller */
5555 XFS_WANT_CORRUPTED_RETURN(mp, !isnullstartblock(got.br_startblock));
5557 if (direction == SHIFT_LEFT) {
5558 startoff = got.br_startoff - offset_shift_fsb;
5561 * Check for merge if we've got an extent to the left,
5562 * otherwise make sure there's enough room at the start
5563 * of the file for the shift.
5565 if (!*current_ext) {
5566 if (got.br_startoff < offset_shift_fsb)
5568 goto update_current_ext;
5571 * grab the left extent and check for a large
5574 adj_irecp = xfs_iext_get_ext(ifp, *current_ext - 1);
5575 xfs_bmbt_get_all(adj_irecp, &adj_irec);
5578 adj_irec.br_startoff + adj_irec.br_blockcount)
5581 /* check whether to merge the extent or shift it down */
5582 if (xfs_bmse_can_merge(&adj_irec, &got,
5583 offset_shift_fsb)) {
5584 return xfs_bmse_merge(ip, whichfork, offset_shift_fsb,
5585 *current_ext, gotp, adj_irecp,
5589 startoff = got.br_startoff + offset_shift_fsb;
5590 /* nothing to move if this is the last extent */
5591 if (*current_ext >= (total_extents - 1))
5592 goto update_current_ext;
5594 * If this is not the last extent in the file, make sure there
5595 * is enough room between current extent and next extent for
5596 * accommodating the shift.
5598 adj_irecp = xfs_iext_get_ext(ifp, *current_ext + 1);
5599 xfs_bmbt_get_all(adj_irecp, &adj_irec);
5600 if (startoff + got.br_blockcount > adj_irec.br_startoff)
5603 * Unlike a left shift (which involves a hole punch),
5604 * a right shift does not modify extent neighbors
5605 * in any way. We should never find mergeable extents
5606 * in this scenario. Check anyways and warn if we
5607 * encounter two extents that could be one.
5609 if (xfs_bmse_can_merge(&got, &adj_irec, offset_shift_fsb))
5613 * Increment the extent index for the next iteration, update the start
5614 * offset of the in-core extent and update the btree if applicable.
5617 if (direction == SHIFT_LEFT)
5621 xfs_bmbt_set_startoff(gotp, startoff);
5622 *logflags |= XFS_ILOG_CORE;
5624 *logflags |= XFS_ILOG_DEXT;
5628 error = xfs_bmbt_lookup_eq(cur, got.br_startoff, got.br_startblock,
5629 got.br_blockcount, &i);
5632 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
5634 got.br_startoff = startoff;
5635 return xfs_bmbt_update(cur, got.br_startoff, got.br_startblock,
5636 got.br_blockcount, got.br_state);
5640 * Shift extent records to the left/right to cover/create a hole.
5642 * The maximum number of extents to be shifted in a single operation is
5643 * @num_exts. @stop_fsb specifies the file offset at which to stop shift and the
5644 * file offset where we've left off is returned in @next_fsb. @offset_shift_fsb
5645 * is the length by which each extent is shifted. If there is no hole to shift
5646 * the extents into, this will be considered invalid operation and we abort
5650 xfs_bmap_shift_extents(
5651 struct xfs_trans *tp,
5652 struct xfs_inode *ip,
5653 xfs_fileoff_t *next_fsb,
5654 xfs_fileoff_t offset_shift_fsb,
5656 xfs_fileoff_t stop_fsb,
5657 xfs_fsblock_t *firstblock,
5658 struct xfs_bmap_free *flist,
5659 enum shift_direction direction,
5662 struct xfs_btree_cur *cur = NULL;
5663 struct xfs_bmbt_rec_host *gotp;
5664 struct xfs_bmbt_irec got;
5665 struct xfs_mount *mp = ip->i_mount;
5666 struct xfs_ifork *ifp;
5667 xfs_extnum_t nexts = 0;
5668 xfs_extnum_t current_ext;
5669 xfs_extnum_t total_extents;
5670 xfs_extnum_t stop_extent;
5672 int whichfork = XFS_DATA_FORK;
5675 if (unlikely(XFS_TEST_ERROR(
5676 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5677 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
5678 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
5679 XFS_ERROR_REPORT("xfs_bmap_shift_extents",
5680 XFS_ERRLEVEL_LOW, mp);
5681 return -EFSCORRUPTED;
5684 if (XFS_FORCED_SHUTDOWN(mp))
5687 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
5688 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
5689 ASSERT(direction == SHIFT_LEFT || direction == SHIFT_RIGHT);
5690 ASSERT(*next_fsb != NULLFSBLOCK || direction == SHIFT_RIGHT);
5692 ifp = XFS_IFORK_PTR(ip, whichfork);
5693 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
5694 /* Read in all the extents */
5695 error = xfs_iread_extents(tp, ip, whichfork);
5700 if (ifp->if_flags & XFS_IFBROOT) {
5701 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5702 cur->bc_private.b.firstblock = *firstblock;
5703 cur->bc_private.b.flist = flist;
5704 cur->bc_private.b.flags = 0;
5708 * There may be delalloc extents in the data fork before the range we
5709 * are collapsing out, so we cannot use the count of real extents here.
5710 * Instead we have to calculate it from the incore fork.
5712 total_extents = ifp->if_bytes / sizeof(xfs_bmbt_rec_t);
5713 if (total_extents == 0) {
5719 * In case of first right shift, we need to initialize next_fsb
5721 if (*next_fsb == NULLFSBLOCK) {
5722 gotp = xfs_iext_get_ext(ifp, total_extents - 1);
5723 xfs_bmbt_get_all(gotp, &got);
5724 *next_fsb = got.br_startoff;
5725 if (stop_fsb > *next_fsb) {
5731 /* Lookup the extent index at which we have to stop */
5732 if (direction == SHIFT_RIGHT) {
5733 gotp = xfs_iext_bno_to_ext(ifp, stop_fsb, &stop_extent);
5734 /* Make stop_extent exclusive of shift range */
5737 stop_extent = total_extents;
5740 * Look up the extent index for the fsb where we start shifting. We can
5741 * henceforth iterate with current_ext as extent list changes are locked
5744 * gotp can be null in 2 cases: 1) if there are no extents or 2)
5745 * *next_fsb lies in a hole beyond which there are no extents. Either
5748 gotp = xfs_iext_bno_to_ext(ifp, *next_fsb, ¤t_ext);
5754 /* some sanity checking before we finally start shifting extents */
5755 if ((direction == SHIFT_LEFT && current_ext >= stop_extent) ||
5756 (direction == SHIFT_RIGHT && current_ext <= stop_extent)) {
5761 while (nexts++ < num_exts) {
5762 error = xfs_bmse_shift_one(ip, whichfork, offset_shift_fsb,
5763 ¤t_ext, gotp, cur, &logflags,
5768 * If there was an extent merge during the shift, the extent
5769 * count can change. Update the total and grade the next record.
5771 if (direction == SHIFT_LEFT) {
5772 total_extents = ifp->if_bytes / sizeof(xfs_bmbt_rec_t);
5773 stop_extent = total_extents;
5776 if (current_ext == stop_extent) {
5778 *next_fsb = NULLFSBLOCK;
5781 gotp = xfs_iext_get_ext(ifp, current_ext);
5785 xfs_bmbt_get_all(gotp, &got);
5786 *next_fsb = got.br_startoff;
5791 xfs_btree_del_cursor(cur,
5792 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5795 xfs_trans_log_inode(tp, ip, logflags);
5801 * Splits an extent into two extents at split_fsb block such that it is
5802 * the first block of the current_ext. @current_ext is a target extent
5803 * to be split. @split_fsb is a block where the extents is split.
5804 * If split_fsb lies in a hole or the first block of extents, just return 0.
5807 xfs_bmap_split_extent_at(
5808 struct xfs_trans *tp,
5809 struct xfs_inode *ip,
5810 xfs_fileoff_t split_fsb,
5811 xfs_fsblock_t *firstfsb,
5812 struct xfs_bmap_free *free_list)
5814 int whichfork = XFS_DATA_FORK;
5815 struct xfs_btree_cur *cur = NULL;
5816 struct xfs_bmbt_rec_host *gotp;
5817 struct xfs_bmbt_irec got;
5818 struct xfs_bmbt_irec new; /* split extent */
5819 struct xfs_mount *mp = ip->i_mount;
5820 struct xfs_ifork *ifp;
5821 xfs_fsblock_t gotblkcnt; /* new block count for got */
5822 xfs_extnum_t current_ext;
5827 if (unlikely(XFS_TEST_ERROR(
5828 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5829 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
5830 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
5831 XFS_ERROR_REPORT("xfs_bmap_split_extent_at",
5832 XFS_ERRLEVEL_LOW, mp);
5833 return -EFSCORRUPTED;
5836 if (XFS_FORCED_SHUTDOWN(mp))
5839 ifp = XFS_IFORK_PTR(ip, whichfork);
5840 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
5841 /* Read in all the extents */
5842 error = xfs_iread_extents(tp, ip, whichfork);
5848 * gotp can be null in 2 cases: 1) if there are no extents
5849 * or 2) split_fsb lies in a hole beyond which there are
5850 * no extents. Either way, we are done.
5852 gotp = xfs_iext_bno_to_ext(ifp, split_fsb, ¤t_ext);
5856 xfs_bmbt_get_all(gotp, &got);
5859 * Check split_fsb lies in a hole or the start boundary offset
5862 if (got.br_startoff >= split_fsb)
5865 gotblkcnt = split_fsb - got.br_startoff;
5866 new.br_startoff = split_fsb;
5867 new.br_startblock = got.br_startblock + gotblkcnt;
5868 new.br_blockcount = got.br_blockcount - gotblkcnt;
5869 new.br_state = got.br_state;
5871 if (ifp->if_flags & XFS_IFBROOT) {
5872 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5873 cur->bc_private.b.firstblock = *firstfsb;
5874 cur->bc_private.b.flist = free_list;
5875 cur->bc_private.b.flags = 0;
5876 error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
5882 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor);
5885 xfs_bmbt_set_blockcount(gotp, gotblkcnt);
5886 got.br_blockcount = gotblkcnt;
5888 logflags = XFS_ILOG_CORE;
5890 error = xfs_bmbt_update(cur, got.br_startoff,
5897 logflags |= XFS_ILOG_DEXT;
5899 /* Add new extent */
5901 xfs_iext_insert(ip, current_ext, 1, &new, 0);
5902 XFS_IFORK_NEXT_SET(ip, whichfork,
5903 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
5906 error = xfs_bmbt_lookup_eq(cur, new.br_startoff,
5907 new.br_startblock, new.br_blockcount,
5911 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, del_cursor);
5912 cur->bc_rec.b.br_state = new.br_state;
5914 error = xfs_btree_insert(cur, &i);
5917 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor);
5921 * Convert to a btree if necessary.
5923 if (xfs_bmap_needs_btree(ip, whichfork)) {
5924 int tmp_logflags; /* partial log flag return val */
5926 ASSERT(cur == NULL);
5927 error = xfs_bmap_extents_to_btree(tp, ip, firstfsb, free_list,
5928 &cur, 0, &tmp_logflags, whichfork);
5929 logflags |= tmp_logflags;
5934 cur->bc_private.b.allocated = 0;
5935 xfs_btree_del_cursor(cur,
5936 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5940 xfs_trans_log_inode(tp, ip, logflags);
5945 xfs_bmap_split_extent(
5946 struct xfs_inode *ip,
5947 xfs_fileoff_t split_fsb)
5949 struct xfs_mount *mp = ip->i_mount;
5950 struct xfs_trans *tp;
5951 struct xfs_bmap_free free_list;
5952 xfs_fsblock_t firstfsb;
5956 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
5957 error = xfs_trans_reserve(tp, &M_RES(mp)->tr_write,
5958 XFS_DIOSTRAT_SPACE_RES(mp, 0), 0);
5960 xfs_trans_cancel(tp);
5964 xfs_ilock(ip, XFS_ILOCK_EXCL);
5965 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
5967 xfs_bmap_init(&free_list, &firstfsb);
5969 error = xfs_bmap_split_extent_at(tp, ip, split_fsb,
5970 &firstfsb, &free_list);
5974 error = xfs_bmap_finish(&tp, &free_list, &committed);
5978 return xfs_trans_commit(tp);
5981 xfs_bmap_cancel(&free_list);
5982 xfs_trans_cancel(tp);