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
328 * btree leaves. THis becomes prohibitively expensive for large extent count
329 * files, so don't bother with inodes that have more than 10,000 extents in
330 * them. The btree record ordering checks will still be done, so for such large
331 * bmapbt constructs that is going to catch most corruptions.
334 xfs_bmap_check_leaf_extents(
335 xfs_btree_cur_t *cur, /* btree cursor or null */
336 xfs_inode_t *ip, /* incore inode pointer */
337 int whichfork) /* data or attr fork */
339 struct xfs_btree_block *block; /* current btree block */
340 xfs_fsblock_t bno; /* block # of "block" */
341 xfs_buf_t *bp; /* buffer for "block" */
342 int error; /* error return value */
343 xfs_extnum_t i=0, j; /* index into the extents list */
344 xfs_ifork_t *ifp; /* fork structure */
345 int level; /* btree level, for checking */
346 xfs_mount_t *mp; /* file system mount structure */
347 __be64 *pp; /* pointer to block address */
348 xfs_bmbt_rec_t *ep; /* pointer to current extent */
349 xfs_bmbt_rec_t last = {0, 0}; /* last extent in prev block */
350 xfs_bmbt_rec_t *nextp; /* pointer to next extent */
353 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) {
357 /* skip large extent count inodes */
358 if (ip->i_d.di_nextents > 10000)
363 ifp = XFS_IFORK_PTR(ip, whichfork);
364 block = ifp->if_broot;
366 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
368 level = be16_to_cpu(block->bb_level);
370 xfs_check_block(block, mp, 1, ifp->if_broot_bytes);
371 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
372 bno = be64_to_cpu(*pp);
374 ASSERT(bno != NULLFSBLOCK);
375 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
376 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
379 * Go down the tree until leaf level is reached, following the first
380 * pointer (leftmost) at each level.
382 while (level-- > 0) {
383 /* See if buf is in cur first */
385 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
388 error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
394 block = XFS_BUF_TO_BLOCK(bp);
399 * Check this block for basic sanity (increasing keys and
400 * no duplicate blocks).
403 xfs_check_block(block, mp, 0, 0);
404 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
405 bno = be64_to_cpu(*pp);
406 XFS_WANT_CORRUPTED_GOTO(mp,
407 XFS_FSB_SANITY_CHECK(mp, bno), error0);
410 xfs_trans_brelse(NULL, bp);
415 * Here with bp and block set to the leftmost leaf node in the tree.
420 * Loop over all leaf nodes checking that all extents are in the right order.
423 xfs_fsblock_t nextbno;
424 xfs_extnum_t num_recs;
427 num_recs = xfs_btree_get_numrecs(block);
430 * Read-ahead the next leaf block, if any.
433 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
436 * Check all the extents to make sure they are OK.
437 * If we had a previous block, the last entry should
438 * conform with the first entry in this one.
441 ep = XFS_BMBT_REC_ADDR(mp, block, 1);
443 ASSERT(xfs_bmbt_disk_get_startoff(&last) +
444 xfs_bmbt_disk_get_blockcount(&last) <=
445 xfs_bmbt_disk_get_startoff(ep));
447 for (j = 1; j < num_recs; j++) {
448 nextp = XFS_BMBT_REC_ADDR(mp, block, j + 1);
449 ASSERT(xfs_bmbt_disk_get_startoff(ep) +
450 xfs_bmbt_disk_get_blockcount(ep) <=
451 xfs_bmbt_disk_get_startoff(nextp));
459 xfs_trans_brelse(NULL, bp);
463 * If we've reached the end, stop.
465 if (bno == NULLFSBLOCK)
469 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
472 error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
478 block = XFS_BUF_TO_BLOCK(bp);
482 xfs_trans_brelse(NULL, bp);
487 xfs_warn(mp, "%s: at error0", __func__);
489 xfs_trans_brelse(NULL, bp);
491 xfs_warn(mp, "%s: BAD after btree leaves for %d extents",
493 panic("%s: CORRUPTED BTREE OR SOMETHING", __func__);
498 * Add bmap trace insert entries for all the contents of the extent records.
501 xfs_bmap_trace_exlist(
502 xfs_inode_t *ip, /* incore inode pointer */
503 xfs_extnum_t cnt, /* count of entries in the list */
504 int whichfork, /* data or attr fork */
505 unsigned long caller_ip)
507 xfs_extnum_t idx; /* extent record index */
508 xfs_ifork_t *ifp; /* inode fork pointer */
511 if (whichfork == XFS_ATTR_FORK)
512 state |= BMAP_ATTRFORK;
514 ifp = XFS_IFORK_PTR(ip, whichfork);
515 ASSERT(cnt == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
516 for (idx = 0; idx < cnt; idx++)
517 trace_xfs_extlist(ip, idx, whichfork, caller_ip);
521 * Validate that the bmbt_irecs being returned from bmapi are valid
522 * given the caller's original parameters. Specifically check the
523 * ranges of the returned irecs to ensure that they only extend beyond
524 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
527 xfs_bmap_validate_ret(
531 xfs_bmbt_irec_t *mval,
535 int i; /* index to map values */
537 ASSERT(ret_nmap <= nmap);
539 for (i = 0; i < ret_nmap; i++) {
540 ASSERT(mval[i].br_blockcount > 0);
541 if (!(flags & XFS_BMAPI_ENTIRE)) {
542 ASSERT(mval[i].br_startoff >= bno);
543 ASSERT(mval[i].br_blockcount <= len);
544 ASSERT(mval[i].br_startoff + mval[i].br_blockcount <=
547 ASSERT(mval[i].br_startoff < bno + len);
548 ASSERT(mval[i].br_startoff + mval[i].br_blockcount >
552 mval[i - 1].br_startoff + mval[i - 1].br_blockcount ==
553 mval[i].br_startoff);
554 ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK &&
555 mval[i].br_startblock != HOLESTARTBLOCK);
556 ASSERT(mval[i].br_state == XFS_EXT_NORM ||
557 mval[i].br_state == XFS_EXT_UNWRITTEN);
562 #define xfs_bmap_check_leaf_extents(cur, ip, whichfork) do { } while (0)
563 #define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap)
567 * bmap free list manipulation functions
571 * Add the extent to the list of extents to be free at transaction end.
572 * The list is maintained sorted (by block number).
576 xfs_fsblock_t bno, /* fs block number of extent */
577 xfs_filblks_t len, /* length of extent */
578 xfs_bmap_free_t *flist, /* list of extents */
579 xfs_mount_t *mp) /* mount point structure */
581 xfs_bmap_free_item_t *cur; /* current (next) element */
582 xfs_bmap_free_item_t *new; /* new element */
583 xfs_bmap_free_item_t *prev; /* previous element */
588 ASSERT(bno != NULLFSBLOCK);
590 ASSERT(len <= MAXEXTLEN);
591 ASSERT(!isnullstartblock(bno));
592 agno = XFS_FSB_TO_AGNO(mp, bno);
593 agbno = XFS_FSB_TO_AGBNO(mp, bno);
594 ASSERT(agno < mp->m_sb.sb_agcount);
595 ASSERT(agbno < mp->m_sb.sb_agblocks);
596 ASSERT(len < mp->m_sb.sb_agblocks);
597 ASSERT(agbno + len <= mp->m_sb.sb_agblocks);
599 ASSERT(xfs_bmap_free_item_zone != NULL);
600 new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP);
601 new->xbfi_startblock = bno;
602 new->xbfi_blockcount = (xfs_extlen_t)len;
603 for (prev = NULL, cur = flist->xbf_first;
605 prev = cur, cur = cur->xbfi_next) {
606 if (cur->xbfi_startblock >= bno)
610 prev->xbfi_next = new;
612 flist->xbf_first = new;
613 new->xbfi_next = cur;
618 * Remove the entry "free" from the free item list. Prev points to the
619 * previous entry, unless "free" is the head of the list.
623 xfs_bmap_free_t *flist, /* free item list header */
624 xfs_bmap_free_item_t *prev, /* previous item on list, if any */
625 xfs_bmap_free_item_t *free) /* list item to be freed */
628 prev->xbfi_next = free->xbfi_next;
630 flist->xbf_first = free->xbfi_next;
632 kmem_zone_free(xfs_bmap_free_item_zone, free);
636 * Free up any items left in the list.
640 xfs_bmap_free_t *flist) /* list of bmap_free_items */
642 xfs_bmap_free_item_t *free; /* free list item */
643 xfs_bmap_free_item_t *next;
645 if (flist->xbf_count == 0)
647 ASSERT(flist->xbf_first != NULL);
648 for (free = flist->xbf_first; free; free = next) {
649 next = free->xbfi_next;
650 xfs_bmap_del_free(flist, NULL, free);
652 ASSERT(flist->xbf_count == 0);
656 * Inode fork format manipulation functions
660 * Transform a btree format file with only one leaf node, where the
661 * extents list will fit in the inode, into an extents format file.
662 * Since the file extents are already in-core, all we have to do is
663 * give up the space for the btree root and pitch the leaf block.
665 STATIC int /* error */
666 xfs_bmap_btree_to_extents(
667 xfs_trans_t *tp, /* transaction pointer */
668 xfs_inode_t *ip, /* incore inode pointer */
669 xfs_btree_cur_t *cur, /* btree cursor */
670 int *logflagsp, /* inode logging flags */
671 int whichfork) /* data or attr fork */
674 struct xfs_btree_block *cblock;/* child btree block */
675 xfs_fsblock_t cbno; /* child block number */
676 xfs_buf_t *cbp; /* child block's buffer */
677 int error; /* error return value */
678 xfs_ifork_t *ifp; /* inode fork data */
679 xfs_mount_t *mp; /* mount point structure */
680 __be64 *pp; /* ptr to block address */
681 struct xfs_btree_block *rblock;/* root btree block */
684 ifp = XFS_IFORK_PTR(ip, whichfork);
685 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
686 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
687 rblock = ifp->if_broot;
688 ASSERT(be16_to_cpu(rblock->bb_level) == 1);
689 ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1);
690 ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0) == 1);
691 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, ifp->if_broot_bytes);
692 cbno = be64_to_cpu(*pp);
695 if ((error = xfs_btree_check_lptr(cur, cbno, 1)))
698 error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp, XFS_BMAP_BTREE_REF,
702 cblock = XFS_BUF_TO_BLOCK(cbp);
703 if ((error = xfs_btree_check_block(cur, cblock, 0, cbp)))
705 xfs_bmap_add_free(cbno, 1, cur->bc_private.b.flist, mp);
706 ip->i_d.di_nblocks--;
707 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
708 xfs_trans_binval(tp, cbp);
709 if (cur->bc_bufs[0] == cbp)
710 cur->bc_bufs[0] = NULL;
711 xfs_iroot_realloc(ip, -1, whichfork);
712 ASSERT(ifp->if_broot == NULL);
713 ASSERT((ifp->if_flags & XFS_IFBROOT) == 0);
714 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
715 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
720 * Convert an extents-format file into a btree-format file.
721 * The new file will have a root block (in the inode) and a single child block.
723 STATIC int /* error */
724 xfs_bmap_extents_to_btree(
725 xfs_trans_t *tp, /* transaction pointer */
726 xfs_inode_t *ip, /* incore inode pointer */
727 xfs_fsblock_t *firstblock, /* first-block-allocated */
728 xfs_bmap_free_t *flist, /* blocks freed in xaction */
729 xfs_btree_cur_t **curp, /* cursor returned to caller */
730 int wasdel, /* converting a delayed alloc */
731 int *logflagsp, /* inode logging flags */
732 int whichfork) /* data or attr fork */
734 struct xfs_btree_block *ablock; /* allocated (child) bt block */
735 xfs_buf_t *abp; /* buffer for ablock */
736 xfs_alloc_arg_t args; /* allocation arguments */
737 xfs_bmbt_rec_t *arp; /* child record pointer */
738 struct xfs_btree_block *block; /* btree root block */
739 xfs_btree_cur_t *cur; /* bmap btree cursor */
740 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
741 int error; /* error return value */
742 xfs_extnum_t i, cnt; /* extent record index */
743 xfs_ifork_t *ifp; /* inode fork pointer */
744 xfs_bmbt_key_t *kp; /* root block key pointer */
745 xfs_mount_t *mp; /* mount structure */
746 xfs_extnum_t nextents; /* number of file extents */
747 xfs_bmbt_ptr_t *pp; /* root block address pointer */
750 ifp = XFS_IFORK_PTR(ip, whichfork);
751 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS);
754 * Make space in the inode incore.
756 xfs_iroot_realloc(ip, 1, whichfork);
757 ifp->if_flags |= XFS_IFBROOT;
762 block = ifp->if_broot;
763 if (xfs_sb_version_hascrc(&mp->m_sb))
764 xfs_btree_init_block_int(mp, block, XFS_BUF_DADDR_NULL,
765 XFS_BMAP_CRC_MAGIC, 1, 1, ip->i_ino,
766 XFS_BTREE_LONG_PTRS | XFS_BTREE_CRC_BLOCKS);
768 xfs_btree_init_block_int(mp, block, XFS_BUF_DADDR_NULL,
769 XFS_BMAP_MAGIC, 1, 1, ip->i_ino,
770 XFS_BTREE_LONG_PTRS);
773 * Need a cursor. Can't allocate until bb_level is filled in.
775 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
776 cur->bc_private.b.firstblock = *firstblock;
777 cur->bc_private.b.flist = flist;
778 cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
780 * Convert to a btree with two levels, one record in root.
782 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE);
783 memset(&args, 0, sizeof(args));
786 args.firstblock = *firstblock;
787 if (*firstblock == NULLFSBLOCK) {
788 args.type = XFS_ALLOCTYPE_START_BNO;
789 args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino);
790 } else if (flist->xbf_low) {
791 args.type = XFS_ALLOCTYPE_START_BNO;
792 args.fsbno = *firstblock;
794 args.type = XFS_ALLOCTYPE_NEAR_BNO;
795 args.fsbno = *firstblock;
797 args.minlen = args.maxlen = args.prod = 1;
798 args.wasdel = wasdel;
800 if ((error = xfs_alloc_vextent(&args))) {
801 xfs_iroot_realloc(ip, -1, whichfork);
802 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
806 * Allocation can't fail, the space was reserved.
808 ASSERT(args.fsbno != NULLFSBLOCK);
809 ASSERT(*firstblock == NULLFSBLOCK ||
810 args.agno == XFS_FSB_TO_AGNO(mp, *firstblock) ||
812 args.agno > XFS_FSB_TO_AGNO(mp, *firstblock)));
813 *firstblock = cur->bc_private.b.firstblock = args.fsbno;
814 cur->bc_private.b.allocated++;
815 ip->i_d.di_nblocks++;
816 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
817 abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0);
819 * Fill in the child block.
821 abp->b_ops = &xfs_bmbt_buf_ops;
822 ablock = XFS_BUF_TO_BLOCK(abp);
823 if (xfs_sb_version_hascrc(&mp->m_sb))
824 xfs_btree_init_block_int(mp, ablock, abp->b_bn,
825 XFS_BMAP_CRC_MAGIC, 0, 0, ip->i_ino,
826 XFS_BTREE_LONG_PTRS | XFS_BTREE_CRC_BLOCKS);
828 xfs_btree_init_block_int(mp, ablock, abp->b_bn,
829 XFS_BMAP_MAGIC, 0, 0, ip->i_ino,
830 XFS_BTREE_LONG_PTRS);
832 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
833 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
834 for (cnt = i = 0; i < nextents; i++) {
835 ep = xfs_iext_get_ext(ifp, i);
836 if (!isnullstartblock(xfs_bmbt_get_startblock(ep))) {
837 arp->l0 = cpu_to_be64(ep->l0);
838 arp->l1 = cpu_to_be64(ep->l1);
842 ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork));
843 xfs_btree_set_numrecs(ablock, cnt);
846 * Fill in the root key and pointer.
848 kp = XFS_BMBT_KEY_ADDR(mp, block, 1);
849 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
850 kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp));
851 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur,
852 be16_to_cpu(block->bb_level)));
853 *pp = cpu_to_be64(args.fsbno);
856 * Do all this logging at the end so that
857 * the root is at the right level.
859 xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS);
860 xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs));
861 ASSERT(*curp == NULL);
863 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork);
868 * Convert a local file to an extents file.
869 * This code is out of bounds for data forks of regular files,
870 * since the file data needs to get logged so things will stay consistent.
871 * (The bmap-level manipulations are ok, though).
874 xfs_bmap_local_to_extents_empty(
875 struct xfs_inode *ip,
878 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
880 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
881 ASSERT(ifp->if_bytes == 0);
882 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0);
884 xfs_bmap_forkoff_reset(ip, whichfork);
885 ifp->if_flags &= ~XFS_IFINLINE;
886 ifp->if_flags |= XFS_IFEXTENTS;
887 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
891 STATIC int /* error */
892 xfs_bmap_local_to_extents(
893 xfs_trans_t *tp, /* transaction pointer */
894 xfs_inode_t *ip, /* incore inode pointer */
895 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
896 xfs_extlen_t total, /* total blocks needed by transaction */
897 int *logflagsp, /* inode logging flags */
899 void (*init_fn)(struct xfs_trans *tp,
901 struct xfs_inode *ip,
902 struct xfs_ifork *ifp))
905 int flags; /* logging flags returned */
906 xfs_ifork_t *ifp; /* inode fork pointer */
907 xfs_alloc_arg_t args; /* allocation arguments */
908 xfs_buf_t *bp; /* buffer for extent block */
909 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
912 * We don't want to deal with the case of keeping inode data inline yet.
913 * So sending the data fork of a regular inode is invalid.
915 ASSERT(!(S_ISREG(VFS_I(ip)->i_mode) && whichfork == XFS_DATA_FORK));
916 ifp = XFS_IFORK_PTR(ip, whichfork);
917 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
919 if (!ifp->if_bytes) {
920 xfs_bmap_local_to_extents_empty(ip, whichfork);
921 flags = XFS_ILOG_CORE;
927 ASSERT((ifp->if_flags & (XFS_IFINLINE|XFS_IFEXTENTS|XFS_IFEXTIREC)) ==
929 memset(&args, 0, sizeof(args));
931 args.mp = ip->i_mount;
932 args.firstblock = *firstblock;
934 * Allocate a block. We know we need only one, since the
935 * file currently fits in an inode.
937 if (*firstblock == NULLFSBLOCK) {
938 args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino);
939 args.type = XFS_ALLOCTYPE_START_BNO;
941 args.fsbno = *firstblock;
942 args.type = XFS_ALLOCTYPE_NEAR_BNO;
945 args.minlen = args.maxlen = args.prod = 1;
946 error = xfs_alloc_vextent(&args);
950 /* Can't fail, the space was reserved. */
951 ASSERT(args.fsbno != NULLFSBLOCK);
952 ASSERT(args.len == 1);
953 *firstblock = args.fsbno;
954 bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0);
957 * Initialize the block, copy the data and log the remote buffer.
959 * The callout is responsible for logging because the remote format
960 * might differ from the local format and thus we don't know how much to
961 * log here. Note that init_fn must also set the buffer log item type
964 init_fn(tp, bp, ip, ifp);
966 /* account for the change in fork size */
967 xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
968 xfs_bmap_local_to_extents_empty(ip, whichfork);
969 flags |= XFS_ILOG_CORE;
971 xfs_iext_add(ifp, 0, 1);
972 ep = xfs_iext_get_ext(ifp, 0);
973 xfs_bmbt_set_allf(ep, 0, args.fsbno, 1, XFS_EXT_NORM);
974 trace_xfs_bmap_post_update(ip, 0,
975 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0,
977 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
978 ip->i_d.di_nblocks = 1;
979 xfs_trans_mod_dquot_byino(tp, ip,
980 XFS_TRANS_DQ_BCOUNT, 1L);
981 flags |= xfs_ilog_fext(whichfork);
989 * Called from xfs_bmap_add_attrfork to handle btree format files.
991 STATIC int /* error */
992 xfs_bmap_add_attrfork_btree(
993 xfs_trans_t *tp, /* transaction pointer */
994 xfs_inode_t *ip, /* incore inode pointer */
995 xfs_fsblock_t *firstblock, /* first block allocated */
996 xfs_bmap_free_t *flist, /* blocks to free at commit */
997 int *flags) /* inode logging flags */
999 xfs_btree_cur_t *cur; /* btree cursor */
1000 int error; /* error return value */
1001 xfs_mount_t *mp; /* file system mount struct */
1002 int stat; /* newroot status */
1005 if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip))
1006 *flags |= XFS_ILOG_DBROOT;
1008 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
1009 cur->bc_private.b.flist = flist;
1010 cur->bc_private.b.firstblock = *firstblock;
1011 if ((error = xfs_bmbt_lookup_ge(cur, 0, 0, 0, &stat)))
1013 /* must be at least one entry */
1014 XFS_WANT_CORRUPTED_GOTO(mp, stat == 1, error0);
1015 if ((error = xfs_btree_new_iroot(cur, flags, &stat)))
1018 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1021 *firstblock = cur->bc_private.b.firstblock;
1022 cur->bc_private.b.allocated = 0;
1023 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1027 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
1032 * Called from xfs_bmap_add_attrfork to handle extents format files.
1034 STATIC int /* error */
1035 xfs_bmap_add_attrfork_extents(
1036 xfs_trans_t *tp, /* transaction pointer */
1037 xfs_inode_t *ip, /* incore inode pointer */
1038 xfs_fsblock_t *firstblock, /* first block allocated */
1039 xfs_bmap_free_t *flist, /* blocks to free at commit */
1040 int *flags) /* inode logging flags */
1042 xfs_btree_cur_t *cur; /* bmap btree cursor */
1043 int error; /* error return value */
1045 if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip))
1048 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist, &cur, 0,
1049 flags, XFS_DATA_FORK);
1051 cur->bc_private.b.allocated = 0;
1052 xfs_btree_del_cursor(cur,
1053 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
1059 * Called from xfs_bmap_add_attrfork to handle local format files. Each
1060 * different data fork content type needs a different callout to do the
1061 * conversion. Some are basic and only require special block initialisation
1062 * callouts for the data formating, others (directories) are so specialised they
1063 * handle everything themselves.
1065 * XXX (dgc): investigate whether directory conversion can use the generic
1066 * formatting callout. It should be possible - it's just a very complex
1069 STATIC int /* error */
1070 xfs_bmap_add_attrfork_local(
1071 xfs_trans_t *tp, /* transaction pointer */
1072 xfs_inode_t *ip, /* incore inode pointer */
1073 xfs_fsblock_t *firstblock, /* first block allocated */
1074 xfs_bmap_free_t *flist, /* blocks to free at commit */
1075 int *flags) /* inode logging flags */
1077 xfs_da_args_t dargs; /* args for dir/attr code */
1079 if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip))
1082 if (S_ISDIR(VFS_I(ip)->i_mode)) {
1083 memset(&dargs, 0, sizeof(dargs));
1084 dargs.geo = ip->i_mount->m_dir_geo;
1086 dargs.firstblock = firstblock;
1087 dargs.flist = flist;
1088 dargs.total = dargs.geo->fsbcount;
1089 dargs.whichfork = XFS_DATA_FORK;
1091 return xfs_dir2_sf_to_block(&dargs);
1094 if (S_ISLNK(VFS_I(ip)->i_mode))
1095 return xfs_bmap_local_to_extents(tp, ip, firstblock, 1,
1096 flags, XFS_DATA_FORK,
1097 xfs_symlink_local_to_remote);
1099 /* should only be called for types that support local format data */
1101 return -EFSCORRUPTED;
1105 * Convert inode from non-attributed to attributed.
1106 * Must not be in a transaction, ip must not be locked.
1108 int /* error code */
1109 xfs_bmap_add_attrfork(
1110 xfs_inode_t *ip, /* incore inode pointer */
1111 int size, /* space new attribute needs */
1112 int rsvd) /* xact may use reserved blks */
1114 xfs_fsblock_t firstblock; /* 1st block/ag allocated */
1115 xfs_bmap_free_t flist; /* freed extent records */
1116 xfs_mount_t *mp; /* mount structure */
1117 xfs_trans_t *tp; /* transaction pointer */
1118 int blks; /* space reservation */
1119 int version = 1; /* superblock attr version */
1120 int logflags; /* logging flags */
1121 int error; /* error return value */
1123 ASSERT(XFS_IFORK_Q(ip) == 0);
1126 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
1127 tp = xfs_trans_alloc(mp, XFS_TRANS_ADDAFORK);
1128 blks = XFS_ADDAFORK_SPACE_RES(mp);
1130 tp->t_flags |= XFS_TRANS_RESERVE;
1131 error = xfs_trans_reserve(tp, &M_RES(mp)->tr_addafork, blks, 0);
1133 xfs_trans_cancel(tp);
1136 xfs_ilock(ip, XFS_ILOCK_EXCL);
1137 error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ?
1138 XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
1139 XFS_QMOPT_RES_REGBLKS);
1142 if (XFS_IFORK_Q(ip))
1144 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) {
1146 * For inodes coming from pre-6.2 filesystems.
1148 ASSERT(ip->i_d.di_aformat == 0);
1149 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
1151 ASSERT(ip->i_d.di_anextents == 0);
1153 xfs_trans_ijoin(tp, ip, 0);
1154 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1156 switch (ip->i_d.di_format) {
1157 case XFS_DINODE_FMT_DEV:
1158 ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
1160 case XFS_DINODE_FMT_UUID:
1161 ip->i_d.di_forkoff = roundup(sizeof(uuid_t), 8) >> 3;
1163 case XFS_DINODE_FMT_LOCAL:
1164 case XFS_DINODE_FMT_EXTENTS:
1165 case XFS_DINODE_FMT_BTREE:
1166 ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size);
1167 if (!ip->i_d.di_forkoff)
1168 ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3;
1169 else if (mp->m_flags & XFS_MOUNT_ATTR2)
1178 ASSERT(ip->i_afp == NULL);
1179 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP);
1180 ip->i_afp->if_flags = XFS_IFEXTENTS;
1182 xfs_bmap_init(&flist, &firstblock);
1183 switch (ip->i_d.di_format) {
1184 case XFS_DINODE_FMT_LOCAL:
1185 error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &flist,
1188 case XFS_DINODE_FMT_EXTENTS:
1189 error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock,
1192 case XFS_DINODE_FMT_BTREE:
1193 error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &flist,
1201 xfs_trans_log_inode(tp, ip, logflags);
1204 if (!xfs_sb_version_hasattr(&mp->m_sb) ||
1205 (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) {
1206 bool log_sb = false;
1208 spin_lock(&mp->m_sb_lock);
1209 if (!xfs_sb_version_hasattr(&mp->m_sb)) {
1210 xfs_sb_version_addattr(&mp->m_sb);
1213 if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) {
1214 xfs_sb_version_addattr2(&mp->m_sb);
1217 spin_unlock(&mp->m_sb_lock);
1222 error = xfs_bmap_finish(&tp, &flist, NULL);
1225 error = xfs_trans_commit(tp);
1226 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1230 xfs_bmap_cancel(&flist);
1232 xfs_trans_cancel(tp);
1233 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1238 * Internal and external extent tree search functions.
1242 * Read in the extents to if_extents.
1243 * All inode fields are set up by caller, we just traverse the btree
1244 * and copy the records in. If the file system cannot contain unwritten
1245 * extents, the records are checked for no "state" flags.
1248 xfs_bmap_read_extents(
1249 xfs_trans_t *tp, /* transaction pointer */
1250 xfs_inode_t *ip, /* incore inode */
1251 int whichfork) /* data or attr fork */
1253 struct xfs_btree_block *block; /* current btree block */
1254 xfs_fsblock_t bno; /* block # of "block" */
1255 xfs_buf_t *bp; /* buffer for "block" */
1256 int error; /* error return value */
1257 xfs_exntfmt_t exntf; /* XFS_EXTFMT_NOSTATE, if checking */
1258 xfs_extnum_t i, j; /* index into the extents list */
1259 xfs_ifork_t *ifp; /* fork structure */
1260 int level; /* btree level, for checking */
1261 xfs_mount_t *mp; /* file system mount structure */
1262 __be64 *pp; /* pointer to block address */
1264 xfs_extnum_t room; /* number of entries there's room for */
1268 ifp = XFS_IFORK_PTR(ip, whichfork);
1269 exntf = (whichfork != XFS_DATA_FORK) ? XFS_EXTFMT_NOSTATE :
1270 XFS_EXTFMT_INODE(ip);
1271 block = ifp->if_broot;
1273 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
1275 level = be16_to_cpu(block->bb_level);
1277 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
1278 bno = be64_to_cpu(*pp);
1279 ASSERT(bno != NULLFSBLOCK);
1280 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
1281 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
1283 * Go down the tree until leaf level is reached, following the first
1284 * pointer (leftmost) at each level.
1286 while (level-- > 0) {
1287 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
1288 XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
1291 block = XFS_BUF_TO_BLOCK(bp);
1294 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
1295 bno = be64_to_cpu(*pp);
1296 XFS_WANT_CORRUPTED_GOTO(mp,
1297 XFS_FSB_SANITY_CHECK(mp, bno), error0);
1298 xfs_trans_brelse(tp, bp);
1301 * Here with bp and block set to the leftmost leaf node in the tree.
1303 room = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
1306 * Loop over all leaf nodes. Copy information to the extent records.
1309 xfs_bmbt_rec_t *frp;
1310 xfs_fsblock_t nextbno;
1311 xfs_extnum_t num_recs;
1314 num_recs = xfs_btree_get_numrecs(block);
1315 if (unlikely(i + num_recs > room)) {
1316 ASSERT(i + num_recs <= room);
1317 xfs_warn(ip->i_mount,
1318 "corrupt dinode %Lu, (btree extents).",
1319 (unsigned long long) ip->i_ino);
1320 XFS_CORRUPTION_ERROR("xfs_bmap_read_extents(1)",
1321 XFS_ERRLEVEL_LOW, ip->i_mount, block);
1325 * Read-ahead the next leaf block, if any.
1327 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
1328 if (nextbno != NULLFSBLOCK)
1329 xfs_btree_reada_bufl(mp, nextbno, 1,
1332 * Copy records into the extent records.
1334 frp = XFS_BMBT_REC_ADDR(mp, block, 1);
1336 for (j = 0; j < num_recs; j++, i++, frp++) {
1337 xfs_bmbt_rec_host_t *trp = xfs_iext_get_ext(ifp, i);
1338 trp->l0 = be64_to_cpu(frp->l0);
1339 trp->l1 = be64_to_cpu(frp->l1);
1341 if (exntf == XFS_EXTFMT_NOSTATE) {
1343 * Check all attribute bmap btree records and
1344 * any "older" data bmap btree records for a
1345 * set bit in the "extent flag" position.
1347 if (unlikely(xfs_check_nostate_extents(ifp,
1348 start, num_recs))) {
1349 XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
1355 xfs_trans_brelse(tp, bp);
1358 * If we've reached the end, stop.
1360 if (bno == NULLFSBLOCK)
1362 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
1363 XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
1366 block = XFS_BUF_TO_BLOCK(bp);
1368 ASSERT(i == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
1369 ASSERT(i == XFS_IFORK_NEXTENTS(ip, whichfork));
1370 XFS_BMAP_TRACE_EXLIST(ip, i, whichfork);
1373 xfs_trans_brelse(tp, bp);
1374 return -EFSCORRUPTED;
1379 * Search the extent records for the entry containing block bno.
1380 * If bno lies in a hole, point to the next entry. If bno lies
1381 * past eof, *eofp will be set, and *prevp will contain the last
1382 * entry (null if none). Else, *lastxp will be set to the index
1383 * of the found entry; *gotp will contain the entry.
1385 STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
1386 xfs_bmap_search_multi_extents(
1387 xfs_ifork_t *ifp, /* inode fork pointer */
1388 xfs_fileoff_t bno, /* block number searched for */
1389 int *eofp, /* out: end of file found */
1390 xfs_extnum_t *lastxp, /* out: last extent index */
1391 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
1392 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
1394 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
1395 xfs_extnum_t lastx; /* last extent index */
1398 * Initialize the extent entry structure to catch access to
1399 * uninitialized br_startblock field.
1401 gotp->br_startoff = 0xffa5a5a5a5a5a5a5LL;
1402 gotp->br_blockcount = 0xa55a5a5a5a5a5a5aLL;
1403 gotp->br_state = XFS_EXT_INVALID;
1404 gotp->br_startblock = 0xffffa5a5a5a5a5a5LL;
1405 prevp->br_startoff = NULLFILEOFF;
1407 ep = xfs_iext_bno_to_ext(ifp, bno, &lastx);
1409 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx - 1), prevp);
1411 if (lastx < (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) {
1412 xfs_bmbt_get_all(ep, gotp);
1426 * Search the extents list for the inode, for the extent containing bno.
1427 * If bno lies in a hole, point to the next entry. If bno lies past eof,
1428 * *eofp will be set, and *prevp will contain the last entry (null if none).
1429 * Else, *lastxp will be set to the index of the found
1430 * entry; *gotp will contain the entry.
1432 STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
1433 xfs_bmap_search_extents(
1434 xfs_inode_t *ip, /* incore inode pointer */
1435 xfs_fileoff_t bno, /* block number searched for */
1436 int fork, /* data or attr fork */
1437 int *eofp, /* out: end of file found */
1438 xfs_extnum_t *lastxp, /* out: last extent index */
1439 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
1440 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
1442 xfs_ifork_t *ifp; /* inode fork pointer */
1443 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
1445 XFS_STATS_INC(ip->i_mount, xs_look_exlist);
1446 ifp = XFS_IFORK_PTR(ip, fork);
1448 ep = xfs_bmap_search_multi_extents(ifp, bno, eofp, lastxp, gotp, prevp);
1450 if (unlikely(!(gotp->br_startblock) && (*lastxp != NULLEXTNUM) &&
1451 !(XFS_IS_REALTIME_INODE(ip) && fork == XFS_DATA_FORK))) {
1452 xfs_alert_tag(ip->i_mount, XFS_PTAG_FSBLOCK_ZERO,
1453 "Access to block zero in inode %llu "
1454 "start_block: %llx start_off: %llx "
1455 "blkcnt: %llx extent-state: %x lastx: %x",
1456 (unsigned long long)ip->i_ino,
1457 (unsigned long long)gotp->br_startblock,
1458 (unsigned long long)gotp->br_startoff,
1459 (unsigned long long)gotp->br_blockcount,
1460 gotp->br_state, *lastxp);
1461 *lastxp = NULLEXTNUM;
1469 * Returns the file-relative block number of the first unused block(s)
1470 * in the file with at least "len" logically contiguous blocks free.
1471 * This is the lowest-address hole if the file has holes, else the first block
1472 * past the end of file.
1473 * Return 0 if the file is currently local (in-inode).
1476 xfs_bmap_first_unused(
1477 xfs_trans_t *tp, /* transaction pointer */
1478 xfs_inode_t *ip, /* incore inode */
1479 xfs_extlen_t len, /* size of hole to find */
1480 xfs_fileoff_t *first_unused, /* unused block */
1481 int whichfork) /* data or attr fork */
1483 int error; /* error return value */
1484 int idx; /* extent record index */
1485 xfs_ifork_t *ifp; /* inode fork pointer */
1486 xfs_fileoff_t lastaddr; /* last block number seen */
1487 xfs_fileoff_t lowest; /* lowest useful block */
1488 xfs_fileoff_t max; /* starting useful block */
1489 xfs_fileoff_t off; /* offset for this block */
1490 xfs_extnum_t nextents; /* number of extent entries */
1492 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE ||
1493 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ||
1494 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
1495 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
1499 ifp = XFS_IFORK_PTR(ip, whichfork);
1500 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
1501 (error = xfs_iread_extents(tp, ip, whichfork)))
1503 lowest = *first_unused;
1504 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
1505 for (idx = 0, lastaddr = 0, max = lowest; idx < nextents; idx++) {
1506 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, idx);
1507 off = xfs_bmbt_get_startoff(ep);
1509 * See if the hole before this extent will work.
1511 if (off >= lowest + len && off - max >= len) {
1512 *first_unused = max;
1515 lastaddr = off + xfs_bmbt_get_blockcount(ep);
1516 max = XFS_FILEOFF_MAX(lastaddr, lowest);
1518 *first_unused = max;
1523 * Returns the file-relative block number of the last block - 1 before
1524 * last_block (input value) in the file.
1525 * This is not based on i_size, it is based on the extent records.
1526 * Returns 0 for local files, as they do not have extent records.
1529 xfs_bmap_last_before(
1530 xfs_trans_t *tp, /* transaction pointer */
1531 xfs_inode_t *ip, /* incore inode */
1532 xfs_fileoff_t *last_block, /* last block */
1533 int whichfork) /* data or attr fork */
1535 xfs_fileoff_t bno; /* input file offset */
1536 int eof; /* hit end of file */
1537 xfs_bmbt_rec_host_t *ep; /* pointer to last extent */
1538 int error; /* error return value */
1539 xfs_bmbt_irec_t got; /* current extent value */
1540 xfs_ifork_t *ifp; /* inode fork pointer */
1541 xfs_extnum_t lastx; /* last extent used */
1542 xfs_bmbt_irec_t prev; /* previous extent value */
1544 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
1545 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
1546 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
1548 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
1552 ifp = XFS_IFORK_PTR(ip, whichfork);
1553 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
1554 (error = xfs_iread_extents(tp, ip, whichfork)))
1556 bno = *last_block - 1;
1557 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
1559 if (eof || xfs_bmbt_get_startoff(ep) > bno) {
1560 if (prev.br_startoff == NULLFILEOFF)
1563 *last_block = prev.br_startoff + prev.br_blockcount;
1566 * Otherwise *last_block is already the right answer.
1572 xfs_bmap_last_extent(
1573 struct xfs_trans *tp,
1574 struct xfs_inode *ip,
1576 struct xfs_bmbt_irec *rec,
1579 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1583 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1584 error = xfs_iread_extents(tp, ip, whichfork);
1589 nextents = ifp->if_bytes / sizeof(xfs_bmbt_rec_t);
1590 if (nextents == 0) {
1595 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, nextents - 1), rec);
1601 * Check the last inode extent to determine whether this allocation will result
1602 * in blocks being allocated at the end of the file. When we allocate new data
1603 * blocks at the end of the file which do not start at the previous data block,
1604 * we will try to align the new blocks at stripe unit boundaries.
1606 * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be
1607 * at, or past the EOF.
1611 struct xfs_bmalloca *bma,
1614 struct xfs_bmbt_irec rec;
1619 error = xfs_bmap_last_extent(NULL, bma->ip, whichfork, &rec,
1630 * Check if we are allocation or past the last extent, or at least into
1631 * the last delayed allocated extent.
1633 bma->aeof = bma->offset >= rec.br_startoff + rec.br_blockcount ||
1634 (bma->offset >= rec.br_startoff &&
1635 isnullstartblock(rec.br_startblock));
1640 * Returns the file-relative block number of the first block past eof in
1641 * the file. This is not based on i_size, it is based on the extent records.
1642 * Returns 0 for local files, as they do not have extent records.
1645 xfs_bmap_last_offset(
1646 struct xfs_inode *ip,
1647 xfs_fileoff_t *last_block,
1650 struct xfs_bmbt_irec rec;
1656 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL)
1659 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
1660 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
1663 error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, &is_empty);
1664 if (error || is_empty)
1667 *last_block = rec.br_startoff + rec.br_blockcount;
1672 * Returns whether the selected fork of the inode has exactly one
1673 * block or not. For the data fork we check this matches di_size,
1674 * implying the file's range is 0..bsize-1.
1676 int /* 1=>1 block, 0=>otherwise */
1678 xfs_inode_t *ip, /* incore inode */
1679 int whichfork) /* data or attr fork */
1681 xfs_bmbt_rec_host_t *ep; /* ptr to fork's extent */
1682 xfs_ifork_t *ifp; /* inode fork pointer */
1683 int rval; /* return value */
1684 xfs_bmbt_irec_t s; /* internal version of extent */
1687 if (whichfork == XFS_DATA_FORK)
1688 return XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize;
1690 if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1)
1692 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
1694 ifp = XFS_IFORK_PTR(ip, whichfork);
1695 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
1696 ep = xfs_iext_get_ext(ifp, 0);
1697 xfs_bmbt_get_all(ep, &s);
1698 rval = s.br_startoff == 0 && s.br_blockcount == 1;
1699 if (rval && whichfork == XFS_DATA_FORK)
1700 ASSERT(XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize);
1705 * Extent tree manipulation functions used during allocation.
1709 * Convert a delayed allocation to a real allocation.
1711 STATIC int /* error */
1712 xfs_bmap_add_extent_delay_real(
1713 struct xfs_bmalloca *bma)
1715 struct xfs_bmbt_irec *new = &bma->got;
1716 int diff; /* temp value */
1717 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
1718 int error; /* error return value */
1719 int i; /* temp state */
1720 xfs_ifork_t *ifp; /* inode fork pointer */
1721 xfs_fileoff_t new_endoff; /* end offset of new entry */
1722 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
1723 /* left is 0, right is 1, prev is 2 */
1724 int rval=0; /* return value (logging flags) */
1725 int state = 0;/* state bits, accessed thru macros */
1726 xfs_filblks_t da_new; /* new count del alloc blocks used */
1727 xfs_filblks_t da_old; /* old count del alloc blocks used */
1728 xfs_filblks_t temp=0; /* value for da_new calculations */
1729 xfs_filblks_t temp2=0;/* value for da_new calculations */
1730 int tmp_rval; /* partial logging flags */
1731 int whichfork = XFS_DATA_FORK;
1732 struct xfs_mount *mp;
1734 mp = bma->ip->i_mount;
1735 ifp = XFS_IFORK_PTR(bma->ip, whichfork);
1737 ASSERT(bma->idx >= 0);
1738 ASSERT(bma->idx <= ifp->if_bytes / sizeof(struct xfs_bmbt_rec));
1739 ASSERT(!isnullstartblock(new->br_startblock));
1741 (bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
1743 XFS_STATS_INC(mp, xs_add_exlist);
1750 * Set up a bunch of variables to make the tests simpler.
1752 ep = xfs_iext_get_ext(ifp, bma->idx);
1753 xfs_bmbt_get_all(ep, &PREV);
1754 new_endoff = new->br_startoff + new->br_blockcount;
1755 ASSERT(PREV.br_startoff <= new->br_startoff);
1756 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
1758 da_old = startblockval(PREV.br_startblock);
1762 * Set flags determining what part of the previous delayed allocation
1763 * extent is being replaced by a real allocation.
1765 if (PREV.br_startoff == new->br_startoff)
1766 state |= BMAP_LEFT_FILLING;
1767 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
1768 state |= BMAP_RIGHT_FILLING;
1771 * Check and set flags if this segment has a left neighbor.
1772 * Don't set contiguous if the combined extent would be too large.
1775 state |= BMAP_LEFT_VALID;
1776 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1), &LEFT);
1778 if (isnullstartblock(LEFT.br_startblock))
1779 state |= BMAP_LEFT_DELAY;
1782 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1783 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1784 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
1785 LEFT.br_state == new->br_state &&
1786 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1787 state |= BMAP_LEFT_CONTIG;
1790 * Check and set flags if this segment has a right neighbor.
1791 * Don't set contiguous if the combined extent would be too large.
1792 * Also check for all-three-contiguous being too large.
1794 if (bma->idx < ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1) {
1795 state |= BMAP_RIGHT_VALID;
1796 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx + 1), &RIGHT);
1798 if (isnullstartblock(RIGHT.br_startblock))
1799 state |= BMAP_RIGHT_DELAY;
1802 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1803 new_endoff == RIGHT.br_startoff &&
1804 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
1805 new->br_state == RIGHT.br_state &&
1806 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
1807 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1808 BMAP_RIGHT_FILLING)) !=
1809 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1810 BMAP_RIGHT_FILLING) ||
1811 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
1813 state |= BMAP_RIGHT_CONTIG;
1817 * Switch out based on the FILLING and CONTIG state bits.
1819 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1820 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
1821 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1822 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1824 * Filling in all of a previously delayed allocation extent.
1825 * The left and right neighbors are both contiguous with new.
1828 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
1829 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
1830 LEFT.br_blockcount + PREV.br_blockcount +
1831 RIGHT.br_blockcount);
1832 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
1834 xfs_iext_remove(bma->ip, bma->idx + 1, 2, state);
1835 bma->ip->i_d.di_nextents--;
1836 if (bma->cur == NULL)
1837 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1839 rval = XFS_ILOG_CORE;
1840 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
1841 RIGHT.br_startblock,
1842 RIGHT.br_blockcount, &i);
1845 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
1846 error = xfs_btree_delete(bma->cur, &i);
1849 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
1850 error = xfs_btree_decrement(bma->cur, 0, &i);
1853 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
1854 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
1856 LEFT.br_blockcount +
1857 PREV.br_blockcount +
1858 RIGHT.br_blockcount, LEFT.br_state);
1864 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1866 * Filling in all of a previously delayed allocation extent.
1867 * The left neighbor is contiguous, the right is not.
1871 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
1872 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
1873 LEFT.br_blockcount + PREV.br_blockcount);
1874 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
1876 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
1877 if (bma->cur == NULL)
1878 rval = XFS_ILOG_DEXT;
1881 error = xfs_bmbt_lookup_eq(bma->cur, LEFT.br_startoff,
1882 LEFT.br_startblock, LEFT.br_blockcount,
1886 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
1887 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
1889 LEFT.br_blockcount +
1890 PREV.br_blockcount, LEFT.br_state);
1896 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1898 * Filling in all of a previously delayed allocation extent.
1899 * The right neighbor is contiguous, the left is not.
1901 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
1902 xfs_bmbt_set_startblock(ep, new->br_startblock);
1903 xfs_bmbt_set_blockcount(ep,
1904 PREV.br_blockcount + RIGHT.br_blockcount);
1905 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
1907 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
1908 if (bma->cur == NULL)
1909 rval = XFS_ILOG_DEXT;
1912 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
1913 RIGHT.br_startblock,
1914 RIGHT.br_blockcount, &i);
1917 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
1918 error = xfs_bmbt_update(bma->cur, PREV.br_startoff,
1920 PREV.br_blockcount +
1921 RIGHT.br_blockcount, PREV.br_state);
1927 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
1929 * Filling in all of a previously delayed allocation extent.
1930 * Neither the left nor right neighbors are contiguous with
1933 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
1934 xfs_bmbt_set_startblock(ep, new->br_startblock);
1935 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
1937 bma->ip->i_d.di_nextents++;
1938 if (bma->cur == NULL)
1939 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1941 rval = XFS_ILOG_CORE;
1942 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
1943 new->br_startblock, new->br_blockcount,
1947 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
1948 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
1949 error = xfs_btree_insert(bma->cur, &i);
1952 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
1956 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
1958 * Filling in the first part of a previous delayed allocation.
1959 * The left neighbor is contiguous.
1961 trace_xfs_bmap_pre_update(bma->ip, bma->idx - 1, state, _THIS_IP_);
1962 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx - 1),
1963 LEFT.br_blockcount + new->br_blockcount);
1964 xfs_bmbt_set_startoff(ep,
1965 PREV.br_startoff + new->br_blockcount);
1966 trace_xfs_bmap_post_update(bma->ip, bma->idx - 1, state, _THIS_IP_);
1968 temp = PREV.br_blockcount - new->br_blockcount;
1969 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
1970 xfs_bmbt_set_blockcount(ep, temp);
1971 if (bma->cur == NULL)
1972 rval = XFS_ILOG_DEXT;
1975 error = xfs_bmbt_lookup_eq(bma->cur, LEFT.br_startoff,
1976 LEFT.br_startblock, LEFT.br_blockcount,
1980 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
1981 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
1983 LEFT.br_blockcount +
1989 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
1990 startblockval(PREV.br_startblock));
1991 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
1992 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
1997 case BMAP_LEFT_FILLING:
1999 * Filling in the first part of a previous delayed allocation.
2000 * The left neighbor is not contiguous.
2002 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
2003 xfs_bmbt_set_startoff(ep, new_endoff);
2004 temp = PREV.br_blockcount - new->br_blockcount;
2005 xfs_bmbt_set_blockcount(ep, temp);
2006 xfs_iext_insert(bma->ip, bma->idx, 1, new, state);
2007 bma->ip->i_d.di_nextents++;
2008 if (bma->cur == NULL)
2009 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2011 rval = XFS_ILOG_CORE;
2012 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
2013 new->br_startblock, new->br_blockcount,
2017 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
2018 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
2019 error = xfs_btree_insert(bma->cur, &i);
2022 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2025 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
2026 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
2027 bma->firstblock, bma->flist,
2028 &bma->cur, 1, &tmp_rval, whichfork);
2033 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
2034 startblockval(PREV.br_startblock) -
2035 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
2036 ep = xfs_iext_get_ext(ifp, bma->idx + 1);
2037 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
2038 trace_xfs_bmap_post_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
2041 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
2043 * Filling in the last part of a previous delayed allocation.
2044 * The right neighbor is contiguous with the new allocation.
2046 temp = PREV.br_blockcount - new->br_blockcount;
2047 trace_xfs_bmap_pre_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
2048 xfs_bmbt_set_blockcount(ep, temp);
2049 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, bma->idx + 1),
2050 new->br_startoff, new->br_startblock,
2051 new->br_blockcount + RIGHT.br_blockcount,
2053 trace_xfs_bmap_post_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
2054 if (bma->cur == NULL)
2055 rval = XFS_ILOG_DEXT;
2058 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
2059 RIGHT.br_startblock,
2060 RIGHT.br_blockcount, &i);
2063 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2064 error = xfs_bmbt_update(bma->cur, new->br_startoff,
2066 new->br_blockcount +
2067 RIGHT.br_blockcount,
2073 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
2074 startblockval(PREV.br_startblock));
2075 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
2076 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
2077 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
2082 case BMAP_RIGHT_FILLING:
2084 * Filling in the last part of a previous delayed allocation.
2085 * The right neighbor is not contiguous.
2087 temp = PREV.br_blockcount - new->br_blockcount;
2088 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
2089 xfs_bmbt_set_blockcount(ep, temp);
2090 xfs_iext_insert(bma->ip, bma->idx + 1, 1, new, state);
2091 bma->ip->i_d.di_nextents++;
2092 if (bma->cur == NULL)
2093 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2095 rval = XFS_ILOG_CORE;
2096 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
2097 new->br_startblock, new->br_blockcount,
2101 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
2102 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
2103 error = xfs_btree_insert(bma->cur, &i);
2106 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2109 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
2110 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
2111 bma->firstblock, bma->flist, &bma->cur, 1,
2112 &tmp_rval, whichfork);
2117 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
2118 startblockval(PREV.br_startblock) -
2119 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
2120 ep = xfs_iext_get_ext(ifp, bma->idx);
2121 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
2122 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
2129 * Filling in the middle part of a previous delayed allocation.
2130 * Contiguity is impossible here.
2131 * This case is avoided almost all the time.
2133 * We start with a delayed allocation:
2135 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
2138 * and we are allocating:
2139 * +rrrrrrrrrrrrrrrrr+
2142 * and we set it up for insertion as:
2143 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
2145 * PREV @ idx LEFT RIGHT
2146 * inserted at idx + 1
2148 temp = new->br_startoff - PREV.br_startoff;
2149 temp2 = PREV.br_startoff + PREV.br_blockcount - new_endoff;
2150 trace_xfs_bmap_pre_update(bma->ip, bma->idx, 0, _THIS_IP_);
2151 xfs_bmbt_set_blockcount(ep, temp); /* truncate PREV */
2153 RIGHT.br_state = PREV.br_state;
2154 RIGHT.br_startblock = nullstartblock(
2155 (int)xfs_bmap_worst_indlen(bma->ip, temp2));
2156 RIGHT.br_startoff = new_endoff;
2157 RIGHT.br_blockcount = temp2;
2158 /* insert LEFT (r[0]) and RIGHT (r[1]) at the same time */
2159 xfs_iext_insert(bma->ip, bma->idx + 1, 2, &LEFT, state);
2160 bma->ip->i_d.di_nextents++;
2161 if (bma->cur == NULL)
2162 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2164 rval = XFS_ILOG_CORE;
2165 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
2166 new->br_startblock, new->br_blockcount,
2170 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
2171 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
2172 error = xfs_btree_insert(bma->cur, &i);
2175 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2178 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
2179 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
2180 bma->firstblock, bma->flist, &bma->cur,
2181 1, &tmp_rval, whichfork);
2186 temp = xfs_bmap_worst_indlen(bma->ip, temp);
2187 temp2 = xfs_bmap_worst_indlen(bma->ip, temp2);
2188 diff = (int)(temp + temp2 - startblockval(PREV.br_startblock) -
2189 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
2191 error = xfs_mod_fdblocks(bma->ip->i_mount,
2192 -((int64_t)diff), false);
2198 ep = xfs_iext_get_ext(ifp, bma->idx);
2199 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
2200 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
2201 trace_xfs_bmap_pre_update(bma->ip, bma->idx + 2, state, _THIS_IP_);
2202 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, bma->idx + 2),
2203 nullstartblock((int)temp2));
2204 trace_xfs_bmap_post_update(bma->ip, bma->idx + 2, state, _THIS_IP_);
2207 da_new = temp + temp2;
2210 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2211 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2212 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
2213 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2214 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2215 case BMAP_LEFT_CONTIG:
2216 case BMAP_RIGHT_CONTIG:
2218 * These cases are all impossible.
2223 /* convert to a btree if necessary */
2224 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
2225 int tmp_logflags; /* partial log flag return val */
2227 ASSERT(bma->cur == NULL);
2228 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
2229 bma->firstblock, bma->flist, &bma->cur,
2230 da_old > 0, &tmp_logflags, whichfork);
2231 bma->logflags |= tmp_logflags;
2236 /* adjust for changes in reserved delayed indirect blocks */
2237 if (da_old || da_new) {
2240 temp += bma->cur->bc_private.b.allocated;
2241 ASSERT(temp <= da_old);
2243 xfs_mod_fdblocks(bma->ip->i_mount,
2244 (int64_t)(da_old - temp), false);
2247 /* clear out the allocated field, done with it now in any case. */
2249 bma->cur->bc_private.b.allocated = 0;
2251 xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork);
2253 bma->logflags |= rval;
2261 * Convert an unwritten allocation to a real allocation or vice versa.
2263 STATIC int /* error */
2264 xfs_bmap_add_extent_unwritten_real(
2265 struct xfs_trans *tp,
2266 xfs_inode_t *ip, /* incore inode pointer */
2267 xfs_extnum_t *idx, /* extent number to update/insert */
2268 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
2269 xfs_bmbt_irec_t *new, /* new data to add to file extents */
2270 xfs_fsblock_t *first, /* pointer to firstblock variable */
2271 xfs_bmap_free_t *flist, /* list of extents to be freed */
2272 int *logflagsp) /* inode logging flags */
2274 xfs_btree_cur_t *cur; /* btree cursor */
2275 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
2276 int error; /* error return value */
2277 int i; /* temp state */
2278 xfs_ifork_t *ifp; /* inode fork pointer */
2279 xfs_fileoff_t new_endoff; /* end offset of new entry */
2280 xfs_exntst_t newext; /* new extent state */
2281 xfs_exntst_t oldext; /* old extent state */
2282 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
2283 /* left is 0, right is 1, prev is 2 */
2284 int rval=0; /* return value (logging flags) */
2285 int state = 0;/* state bits, accessed thru macros */
2286 struct xfs_mount *mp = tp->t_mountp;
2291 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
2294 ASSERT(*idx <= ifp->if_bytes / sizeof(struct xfs_bmbt_rec));
2295 ASSERT(!isnullstartblock(new->br_startblock));
2297 XFS_STATS_INC(mp, xs_add_exlist);
2304 * Set up a bunch of variables to make the tests simpler.
2307 ep = xfs_iext_get_ext(ifp, *idx);
2308 xfs_bmbt_get_all(ep, &PREV);
2309 newext = new->br_state;
2310 oldext = (newext == XFS_EXT_UNWRITTEN) ?
2311 XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
2312 ASSERT(PREV.br_state == oldext);
2313 new_endoff = new->br_startoff + new->br_blockcount;
2314 ASSERT(PREV.br_startoff <= new->br_startoff);
2315 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
2318 * Set flags determining what part of the previous oldext allocation
2319 * extent is being replaced by a newext allocation.
2321 if (PREV.br_startoff == new->br_startoff)
2322 state |= BMAP_LEFT_FILLING;
2323 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
2324 state |= BMAP_RIGHT_FILLING;
2327 * Check and set flags if this segment has a left neighbor.
2328 * Don't set contiguous if the combined extent would be too large.
2331 state |= BMAP_LEFT_VALID;
2332 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &LEFT);
2334 if (isnullstartblock(LEFT.br_startblock))
2335 state |= BMAP_LEFT_DELAY;
2338 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2339 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
2340 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
2341 LEFT.br_state == newext &&
2342 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2343 state |= BMAP_LEFT_CONTIG;
2346 * Check and set flags if this segment has a right neighbor.
2347 * Don't set contiguous if the combined extent would be too large.
2348 * Also check for all-three-contiguous being too large.
2350 if (*idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1) {
2351 state |= BMAP_RIGHT_VALID;
2352 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx + 1), &RIGHT);
2353 if (isnullstartblock(RIGHT.br_startblock))
2354 state |= BMAP_RIGHT_DELAY;
2357 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2358 new_endoff == RIGHT.br_startoff &&
2359 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
2360 newext == RIGHT.br_state &&
2361 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
2362 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2363 BMAP_RIGHT_FILLING)) !=
2364 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2365 BMAP_RIGHT_FILLING) ||
2366 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
2368 state |= BMAP_RIGHT_CONTIG;
2371 * Switch out based on the FILLING and CONTIG state bits.
2373 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2374 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
2375 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2376 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
2378 * Setting all of a previous oldext extent to newext.
2379 * The left and right neighbors are both contiguous with new.
2383 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2384 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
2385 LEFT.br_blockcount + PREV.br_blockcount +
2386 RIGHT.br_blockcount);
2387 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2389 xfs_iext_remove(ip, *idx + 1, 2, state);
2390 ip->i_d.di_nextents -= 2;
2392 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2394 rval = XFS_ILOG_CORE;
2395 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
2396 RIGHT.br_startblock,
2397 RIGHT.br_blockcount, &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_btree_delete(cur, &i)))
2408 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2409 if ((error = xfs_btree_decrement(cur, 0, &i)))
2411 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2412 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
2414 LEFT.br_blockcount + PREV.br_blockcount +
2415 RIGHT.br_blockcount, LEFT.br_state)))
2420 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2422 * Setting all of a previous oldext extent to newext.
2423 * The left neighbor is contiguous, the right is not.
2427 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2428 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
2429 LEFT.br_blockcount + PREV.br_blockcount);
2430 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2432 xfs_iext_remove(ip, *idx + 1, 1, state);
2433 ip->i_d.di_nextents--;
2435 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2437 rval = XFS_ILOG_CORE;
2438 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2439 PREV.br_startblock, PREV.br_blockcount,
2442 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2443 if ((error = xfs_btree_delete(cur, &i)))
2445 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2446 if ((error = xfs_btree_decrement(cur, 0, &i)))
2448 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2449 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
2451 LEFT.br_blockcount + PREV.br_blockcount,
2457 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
2459 * Setting all of a previous oldext extent to newext.
2460 * The right neighbor is contiguous, the left is not.
2462 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2463 xfs_bmbt_set_blockcount(ep,
2464 PREV.br_blockcount + RIGHT.br_blockcount);
2465 xfs_bmbt_set_state(ep, newext);
2466 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2467 xfs_iext_remove(ip, *idx + 1, 1, state);
2468 ip->i_d.di_nextents--;
2470 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2472 rval = XFS_ILOG_CORE;
2473 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
2474 RIGHT.br_startblock,
2475 RIGHT.br_blockcount, &i)))
2477 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2478 if ((error = xfs_btree_delete(cur, &i)))
2480 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2481 if ((error = xfs_btree_decrement(cur, 0, &i)))
2483 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2484 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2486 new->br_blockcount + RIGHT.br_blockcount,
2492 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
2494 * Setting all of a previous oldext extent to newext.
2495 * Neither the left nor right neighbors are contiguous with
2498 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2499 xfs_bmbt_set_state(ep, newext);
2500 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2503 rval = XFS_ILOG_DEXT;
2506 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
2507 new->br_startblock, new->br_blockcount,
2510 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2511 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2512 new->br_startblock, new->br_blockcount,
2518 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
2520 * Setting the first part of a previous oldext extent to newext.
2521 * The left neighbor is contiguous.
2523 trace_xfs_bmap_pre_update(ip, *idx - 1, state, _THIS_IP_);
2524 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx - 1),
2525 LEFT.br_blockcount + new->br_blockcount);
2526 xfs_bmbt_set_startoff(ep,
2527 PREV.br_startoff + new->br_blockcount);
2528 trace_xfs_bmap_post_update(ip, *idx - 1, state, _THIS_IP_);
2530 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2531 xfs_bmbt_set_startblock(ep,
2532 new->br_startblock + new->br_blockcount);
2533 xfs_bmbt_set_blockcount(ep,
2534 PREV.br_blockcount - new->br_blockcount);
2535 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2540 rval = XFS_ILOG_DEXT;
2543 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2544 PREV.br_startblock, PREV.br_blockcount,
2547 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2548 if ((error = xfs_bmbt_update(cur,
2549 PREV.br_startoff + new->br_blockcount,
2550 PREV.br_startblock + new->br_blockcount,
2551 PREV.br_blockcount - new->br_blockcount,
2554 if ((error = xfs_btree_decrement(cur, 0, &i)))
2556 error = xfs_bmbt_update(cur, LEFT.br_startoff,
2558 LEFT.br_blockcount + new->br_blockcount,
2565 case BMAP_LEFT_FILLING:
2567 * Setting the first part of a previous oldext extent to newext.
2568 * The left neighbor is not contiguous.
2570 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2571 ASSERT(ep && xfs_bmbt_get_state(ep) == oldext);
2572 xfs_bmbt_set_startoff(ep, new_endoff);
2573 xfs_bmbt_set_blockcount(ep,
2574 PREV.br_blockcount - new->br_blockcount);
2575 xfs_bmbt_set_startblock(ep,
2576 new->br_startblock + new->br_blockcount);
2577 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2579 xfs_iext_insert(ip, *idx, 1, new, state);
2580 ip->i_d.di_nextents++;
2582 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2584 rval = XFS_ILOG_CORE;
2585 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2586 PREV.br_startblock, PREV.br_blockcount,
2589 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2590 if ((error = xfs_bmbt_update(cur,
2591 PREV.br_startoff + new->br_blockcount,
2592 PREV.br_startblock + new->br_blockcount,
2593 PREV.br_blockcount - new->br_blockcount,
2596 cur->bc_rec.b = *new;
2597 if ((error = xfs_btree_insert(cur, &i)))
2599 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2603 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
2605 * Setting the last part of a previous oldext extent to newext.
2606 * The right neighbor is contiguous with the new allocation.
2608 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2609 xfs_bmbt_set_blockcount(ep,
2610 PREV.br_blockcount - new->br_blockcount);
2611 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2615 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2616 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx),
2617 new->br_startoff, new->br_startblock,
2618 new->br_blockcount + RIGHT.br_blockcount, newext);
2619 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2622 rval = XFS_ILOG_DEXT;
2625 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2627 PREV.br_blockcount, &i)))
2629 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2630 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
2632 PREV.br_blockcount - new->br_blockcount,
2635 if ((error = xfs_btree_increment(cur, 0, &i)))
2637 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2639 new->br_blockcount + RIGHT.br_blockcount,
2645 case BMAP_RIGHT_FILLING:
2647 * Setting the last part of a previous oldext extent to newext.
2648 * The right neighbor is not contiguous.
2650 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2651 xfs_bmbt_set_blockcount(ep,
2652 PREV.br_blockcount - new->br_blockcount);
2653 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2656 xfs_iext_insert(ip, *idx, 1, new, state);
2658 ip->i_d.di_nextents++;
2660 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2662 rval = XFS_ILOG_CORE;
2663 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2664 PREV.br_startblock, PREV.br_blockcount,
2667 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2668 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
2670 PREV.br_blockcount - new->br_blockcount,
2673 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
2674 new->br_startblock, new->br_blockcount,
2677 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
2678 cur->bc_rec.b.br_state = XFS_EXT_NORM;
2679 if ((error = xfs_btree_insert(cur, &i)))
2681 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2687 * Setting the middle part of a previous oldext extent to
2688 * newext. Contiguity is impossible here.
2689 * One extent becomes three extents.
2691 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2692 xfs_bmbt_set_blockcount(ep,
2693 new->br_startoff - PREV.br_startoff);
2694 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2697 r[1].br_startoff = new_endoff;
2698 r[1].br_blockcount =
2699 PREV.br_startoff + PREV.br_blockcount - new_endoff;
2700 r[1].br_startblock = new->br_startblock + new->br_blockcount;
2701 r[1].br_state = oldext;
2704 xfs_iext_insert(ip, *idx, 2, &r[0], state);
2706 ip->i_d.di_nextents += 2;
2708 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2710 rval = XFS_ILOG_CORE;
2711 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2712 PREV.br_startblock, PREV.br_blockcount,
2715 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2716 /* new right extent - oldext */
2717 if ((error = xfs_bmbt_update(cur, r[1].br_startoff,
2718 r[1].br_startblock, r[1].br_blockcount,
2721 /* new left extent - oldext */
2722 cur->bc_rec.b = PREV;
2723 cur->bc_rec.b.br_blockcount =
2724 new->br_startoff - PREV.br_startoff;
2725 if ((error = xfs_btree_insert(cur, &i)))
2727 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2729 * Reset the cursor to the position of the new extent
2730 * we are about to insert as we can't trust it after
2731 * the previous insert.
2733 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
2734 new->br_startblock, new->br_blockcount,
2737 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
2738 /* new middle extent - newext */
2739 cur->bc_rec.b.br_state = new->br_state;
2740 if ((error = xfs_btree_insert(cur, &i)))
2742 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2746 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2747 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2748 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
2749 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2750 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2751 case BMAP_LEFT_CONTIG:
2752 case BMAP_RIGHT_CONTIG:
2754 * These cases are all impossible.
2759 /* convert to a btree if necessary */
2760 if (xfs_bmap_needs_btree(ip, XFS_DATA_FORK)) {
2761 int tmp_logflags; /* partial log flag return val */
2763 ASSERT(cur == NULL);
2764 error = xfs_bmap_extents_to_btree(tp, ip, first, flist, &cur,
2765 0, &tmp_logflags, XFS_DATA_FORK);
2766 *logflagsp |= tmp_logflags;
2771 /* clear out the allocated field, done with it now in any case. */
2773 cur->bc_private.b.allocated = 0;
2777 xfs_bmap_check_leaf_extents(*curp, ip, XFS_DATA_FORK);
2787 * Convert a hole to a delayed allocation.
2790 xfs_bmap_add_extent_hole_delay(
2791 xfs_inode_t *ip, /* incore inode pointer */
2792 xfs_extnum_t *idx, /* extent number to update/insert */
2793 xfs_bmbt_irec_t *new) /* new data to add to file extents */
2795 xfs_ifork_t *ifp; /* inode fork pointer */
2796 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2797 xfs_filblks_t newlen=0; /* new indirect size */
2798 xfs_filblks_t oldlen=0; /* old indirect size */
2799 xfs_bmbt_irec_t right; /* right neighbor extent entry */
2800 int state; /* state bits, accessed thru macros */
2801 xfs_filblks_t temp=0; /* temp for indirect calculations */
2803 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
2805 ASSERT(isnullstartblock(new->br_startblock));
2808 * Check and set flags if this segment has a left neighbor
2811 state |= BMAP_LEFT_VALID;
2812 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &left);
2814 if (isnullstartblock(left.br_startblock))
2815 state |= BMAP_LEFT_DELAY;
2819 * Check and set flags if the current (right) segment exists.
2820 * If it doesn't exist, we're converting the hole at end-of-file.
2822 if (*idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) {
2823 state |= BMAP_RIGHT_VALID;
2824 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx), &right);
2826 if (isnullstartblock(right.br_startblock))
2827 state |= BMAP_RIGHT_DELAY;
2831 * Set contiguity flags on the left and right neighbors.
2832 * Don't let extents get too large, even if the pieces are contiguous.
2834 if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) &&
2835 left.br_startoff + left.br_blockcount == new->br_startoff &&
2836 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2837 state |= BMAP_LEFT_CONTIG;
2839 if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) &&
2840 new->br_startoff + new->br_blockcount == right.br_startoff &&
2841 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2842 (!(state & BMAP_LEFT_CONTIG) ||
2843 (left.br_blockcount + new->br_blockcount +
2844 right.br_blockcount <= MAXEXTLEN)))
2845 state |= BMAP_RIGHT_CONTIG;
2848 * Switch out based on the contiguity flags.
2850 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2851 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2853 * New allocation is contiguous with delayed allocations
2854 * on the left and on the right.
2855 * Merge all three into a single extent record.
2858 temp = left.br_blockcount + new->br_blockcount +
2859 right.br_blockcount;
2861 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2862 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp);
2863 oldlen = startblockval(left.br_startblock) +
2864 startblockval(new->br_startblock) +
2865 startblockval(right.br_startblock);
2866 newlen = xfs_bmap_worst_indlen(ip, temp);
2867 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx),
2868 nullstartblock((int)newlen));
2869 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2871 xfs_iext_remove(ip, *idx + 1, 1, state);
2874 case BMAP_LEFT_CONTIG:
2876 * New allocation is contiguous with a delayed allocation
2878 * Merge the new allocation with the left neighbor.
2881 temp = left.br_blockcount + new->br_blockcount;
2883 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2884 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp);
2885 oldlen = startblockval(left.br_startblock) +
2886 startblockval(new->br_startblock);
2887 newlen = xfs_bmap_worst_indlen(ip, temp);
2888 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx),
2889 nullstartblock((int)newlen));
2890 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2893 case BMAP_RIGHT_CONTIG:
2895 * New allocation is contiguous with a delayed allocation
2897 * Merge the new allocation with the right neighbor.
2899 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2900 temp = new->br_blockcount + right.br_blockcount;
2901 oldlen = startblockval(new->br_startblock) +
2902 startblockval(right.br_startblock);
2903 newlen = xfs_bmap_worst_indlen(ip, temp);
2904 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx),
2906 nullstartblock((int)newlen), temp, right.br_state);
2907 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2912 * New allocation is not contiguous with another
2913 * delayed allocation.
2914 * Insert a new entry.
2916 oldlen = newlen = 0;
2917 xfs_iext_insert(ip, *idx, 1, new, state);
2920 if (oldlen != newlen) {
2921 ASSERT(oldlen > newlen);
2922 xfs_mod_fdblocks(ip->i_mount, (int64_t)(oldlen - newlen),
2925 * Nothing to do for disk quota accounting here.
2931 * Convert a hole to a real allocation.
2933 STATIC int /* error */
2934 xfs_bmap_add_extent_hole_real(
2935 struct xfs_bmalloca *bma,
2938 struct xfs_bmbt_irec *new = &bma->got;
2939 int error; /* error return value */
2940 int i; /* temp state */
2941 xfs_ifork_t *ifp; /* inode fork pointer */
2942 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2943 xfs_bmbt_irec_t right; /* right neighbor extent entry */
2944 int rval=0; /* return value (logging flags) */
2945 int state; /* state bits, accessed thru macros */
2946 struct xfs_mount *mp;
2948 mp = bma->ip->i_mount;
2949 ifp = XFS_IFORK_PTR(bma->ip, whichfork);
2951 ASSERT(bma->idx >= 0);
2952 ASSERT(bma->idx <= ifp->if_bytes / sizeof(struct xfs_bmbt_rec));
2953 ASSERT(!isnullstartblock(new->br_startblock));
2955 !(bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
2957 XFS_STATS_INC(mp, xs_add_exlist);
2960 if (whichfork == XFS_ATTR_FORK)
2961 state |= BMAP_ATTRFORK;
2964 * Check and set flags if this segment has a left neighbor.
2967 state |= BMAP_LEFT_VALID;
2968 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1), &left);
2969 if (isnullstartblock(left.br_startblock))
2970 state |= BMAP_LEFT_DELAY;
2974 * Check and set flags if this segment has a current value.
2975 * Not true if we're inserting into the "hole" at eof.
2977 if (bma->idx < ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) {
2978 state |= BMAP_RIGHT_VALID;
2979 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &right);
2980 if (isnullstartblock(right.br_startblock))
2981 state |= BMAP_RIGHT_DELAY;
2985 * We're inserting a real allocation between "left" and "right".
2986 * Set the contiguity flags. Don't let extents get too large.
2988 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2989 left.br_startoff + left.br_blockcount == new->br_startoff &&
2990 left.br_startblock + left.br_blockcount == new->br_startblock &&
2991 left.br_state == new->br_state &&
2992 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2993 state |= BMAP_LEFT_CONTIG;
2995 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2996 new->br_startoff + new->br_blockcount == right.br_startoff &&
2997 new->br_startblock + new->br_blockcount == right.br_startblock &&
2998 new->br_state == right.br_state &&
2999 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
3000 (!(state & BMAP_LEFT_CONTIG) ||
3001 left.br_blockcount + new->br_blockcount +
3002 right.br_blockcount <= MAXEXTLEN))
3003 state |= BMAP_RIGHT_CONTIG;
3007 * Select which case we're in here, and implement it.
3009 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
3010 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
3012 * New allocation is contiguous with real allocations on the
3013 * left and on the right.
3014 * Merge all three into a single extent record.
3017 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
3018 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
3019 left.br_blockcount + new->br_blockcount +
3020 right.br_blockcount);
3021 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
3023 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
3025 XFS_IFORK_NEXT_SET(bma->ip, whichfork,
3026 XFS_IFORK_NEXTENTS(bma->ip, whichfork) - 1);
3027 if (bma->cur == NULL) {
3028 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
3030 rval = XFS_ILOG_CORE;
3031 error = xfs_bmbt_lookup_eq(bma->cur, right.br_startoff,
3032 right.br_startblock, right.br_blockcount,
3036 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
3037 error = xfs_btree_delete(bma->cur, &i);
3040 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
3041 error = xfs_btree_decrement(bma->cur, 0, &i);
3044 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
3045 error = xfs_bmbt_update(bma->cur, left.br_startoff,
3047 left.br_blockcount +
3048 new->br_blockcount +
3049 right.br_blockcount,
3056 case BMAP_LEFT_CONTIG:
3058 * New allocation is contiguous with a real allocation
3060 * Merge the new allocation with the left neighbor.
3063 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
3064 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
3065 left.br_blockcount + new->br_blockcount);
3066 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
3068 if (bma->cur == NULL) {
3069 rval = xfs_ilog_fext(whichfork);
3072 error = xfs_bmbt_lookup_eq(bma->cur, left.br_startoff,
3073 left.br_startblock, left.br_blockcount,
3077 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
3078 error = xfs_bmbt_update(bma->cur, left.br_startoff,
3080 left.br_blockcount +
3088 case BMAP_RIGHT_CONTIG:
3090 * New allocation is contiguous with a real allocation
3092 * Merge the new allocation with the right neighbor.
3094 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
3095 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, bma->idx),
3096 new->br_startoff, new->br_startblock,
3097 new->br_blockcount + right.br_blockcount,
3099 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
3101 if (bma->cur == NULL) {
3102 rval = xfs_ilog_fext(whichfork);
3105 error = xfs_bmbt_lookup_eq(bma->cur,
3107 right.br_startblock,
3108 right.br_blockcount, &i);
3111 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
3112 error = xfs_bmbt_update(bma->cur, new->br_startoff,
3114 new->br_blockcount +
3115 right.br_blockcount,
3124 * New allocation is not contiguous with another
3126 * Insert a new entry.
3128 xfs_iext_insert(bma->ip, bma->idx, 1, new, state);
3129 XFS_IFORK_NEXT_SET(bma->ip, whichfork,
3130 XFS_IFORK_NEXTENTS(bma->ip, whichfork) + 1);
3131 if (bma->cur == NULL) {
3132 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
3134 rval = XFS_ILOG_CORE;
3135 error = xfs_bmbt_lookup_eq(bma->cur,
3138 new->br_blockcount, &i);
3141 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
3142 bma->cur->bc_rec.b.br_state = new->br_state;
3143 error = xfs_btree_insert(bma->cur, &i);
3146 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
3151 /* convert to a btree if necessary */
3152 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
3153 int tmp_logflags; /* partial log flag return val */
3155 ASSERT(bma->cur == NULL);
3156 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
3157 bma->firstblock, bma->flist, &bma->cur,
3158 0, &tmp_logflags, whichfork);
3159 bma->logflags |= tmp_logflags;
3164 /* clear out the allocated field, done with it now in any case. */
3166 bma->cur->bc_private.b.allocated = 0;
3168 xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork);
3170 bma->logflags |= rval;
3175 * Functions used in the extent read, allocate and remove paths
3179 * Adjust the size of the new extent based on di_extsize and rt extsize.
3182 xfs_bmap_extsize_align(
3184 xfs_bmbt_irec_t *gotp, /* next extent pointer */
3185 xfs_bmbt_irec_t *prevp, /* previous extent pointer */
3186 xfs_extlen_t extsz, /* align to this extent size */
3187 int rt, /* is this a realtime inode? */
3188 int eof, /* is extent at end-of-file? */
3189 int delay, /* creating delalloc extent? */
3190 int convert, /* overwriting unwritten extent? */
3191 xfs_fileoff_t *offp, /* in/out: aligned offset */
3192 xfs_extlen_t *lenp) /* in/out: aligned length */
3194 xfs_fileoff_t orig_off; /* original offset */
3195 xfs_extlen_t orig_alen; /* original length */
3196 xfs_fileoff_t orig_end; /* original off+len */
3197 xfs_fileoff_t nexto; /* next file offset */
3198 xfs_fileoff_t prevo; /* previous file offset */
3199 xfs_fileoff_t align_off; /* temp for offset */
3200 xfs_extlen_t align_alen; /* temp for length */
3201 xfs_extlen_t temp; /* temp for calculations */
3206 orig_off = align_off = *offp;
3207 orig_alen = align_alen = *lenp;
3208 orig_end = orig_off + orig_alen;
3211 * If this request overlaps an existing extent, then don't
3212 * attempt to perform any additional alignment.
3214 if (!delay && !eof &&
3215 (orig_off >= gotp->br_startoff) &&
3216 (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
3221 * If the file offset is unaligned vs. the extent size
3222 * we need to align it. This will be possible unless
3223 * the file was previously written with a kernel that didn't
3224 * perform this alignment, or if a truncate shot us in the
3227 temp = do_mod(orig_off, extsz);
3233 /* Same adjustment for the end of the requested area. */
3234 temp = (align_alen % extsz);
3236 align_alen += extsz - temp;
3239 * For large extent hint sizes, the aligned extent might be larger than
3240 * MAXEXTLEN. In that case, reduce the size by an extsz so that it pulls
3241 * the length back under MAXEXTLEN. The outer allocation loops handle
3242 * short allocation just fine, so it is safe to do this. We only want to
3243 * do it when we are forced to, though, because it means more allocation
3244 * operations are required.
3246 while (align_alen > MAXEXTLEN)
3247 align_alen -= extsz;
3248 ASSERT(align_alen <= MAXEXTLEN);
3251 * If the previous block overlaps with this proposed allocation
3252 * then move the start forward without adjusting the length.
3254 if (prevp->br_startoff != NULLFILEOFF) {
3255 if (prevp->br_startblock == HOLESTARTBLOCK)
3256 prevo = prevp->br_startoff;
3258 prevo = prevp->br_startoff + prevp->br_blockcount;
3261 if (align_off != orig_off && align_off < prevo)
3264 * If the next block overlaps with this proposed allocation
3265 * then move the start back without adjusting the length,
3266 * but not before offset 0.
3267 * This may of course make the start overlap previous block,
3268 * and if we hit the offset 0 limit then the next block
3269 * can still overlap too.
3271 if (!eof && gotp->br_startoff != NULLFILEOFF) {
3272 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
3273 (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
3274 nexto = gotp->br_startoff + gotp->br_blockcount;
3276 nexto = gotp->br_startoff;
3278 nexto = NULLFILEOFF;
3280 align_off + align_alen != orig_end &&
3281 align_off + align_alen > nexto)
3282 align_off = nexto > align_alen ? nexto - align_alen : 0;
3284 * If we're now overlapping the next or previous extent that
3285 * means we can't fit an extsz piece in this hole. Just move
3286 * the start forward to the first valid spot and set
3287 * the length so we hit the end.
3289 if (align_off != orig_off && align_off < prevo)
3291 if (align_off + align_alen != orig_end &&
3292 align_off + align_alen > nexto &&
3293 nexto != NULLFILEOFF) {
3294 ASSERT(nexto > prevo);
3295 align_alen = nexto - align_off;
3299 * If realtime, and the result isn't a multiple of the realtime
3300 * extent size we need to remove blocks until it is.
3302 if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
3304 * We're not covering the original request, or
3305 * we won't be able to once we fix the length.
3307 if (orig_off < align_off ||
3308 orig_end > align_off + align_alen ||
3309 align_alen - temp < orig_alen)
3312 * Try to fix it by moving the start up.
3314 if (align_off + temp <= orig_off) {
3319 * Try to fix it by moving the end in.
3321 else if (align_off + align_alen - temp >= orig_end)
3324 * Set the start to the minimum then trim the length.
3327 align_alen -= orig_off - align_off;
3328 align_off = orig_off;
3329 align_alen -= align_alen % mp->m_sb.sb_rextsize;
3332 * Result doesn't cover the request, fail it.
3334 if (orig_off < align_off || orig_end > align_off + align_alen)
3337 ASSERT(orig_off >= align_off);
3338 /* see MAXEXTLEN handling above */
3339 ASSERT(orig_end <= align_off + align_alen ||
3340 align_alen + extsz > MAXEXTLEN);
3344 if (!eof && gotp->br_startoff != NULLFILEOFF)
3345 ASSERT(align_off + align_alen <= gotp->br_startoff);
3346 if (prevp->br_startoff != NULLFILEOFF)
3347 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
3355 #define XFS_ALLOC_GAP_UNITS 4
3359 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
3361 xfs_fsblock_t adjust; /* adjustment to block numbers */
3362 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
3363 xfs_mount_t *mp; /* mount point structure */
3364 int nullfb; /* true if ap->firstblock isn't set */
3365 int rt; /* true if inode is realtime */
3367 #define ISVALID(x,y) \
3369 (x) < mp->m_sb.sb_rblocks : \
3370 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
3371 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
3372 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
3374 mp = ap->ip->i_mount;
3375 nullfb = *ap->firstblock == NULLFSBLOCK;
3376 rt = XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata;
3377 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
3379 * If allocating at eof, and there's a previous real block,
3380 * try to use its last block as our starting point.
3382 if (ap->eof && ap->prev.br_startoff != NULLFILEOFF &&
3383 !isnullstartblock(ap->prev.br_startblock) &&
3384 ISVALID(ap->prev.br_startblock + ap->prev.br_blockcount,
3385 ap->prev.br_startblock)) {
3386 ap->blkno = ap->prev.br_startblock + ap->prev.br_blockcount;
3388 * Adjust for the gap between prevp and us.
3390 adjust = ap->offset -
3391 (ap->prev.br_startoff + ap->prev.br_blockcount);
3393 ISVALID(ap->blkno + adjust, ap->prev.br_startblock))
3394 ap->blkno += adjust;
3397 * If not at eof, then compare the two neighbor blocks.
3398 * Figure out whether either one gives us a good starting point,
3399 * and pick the better one.
3401 else if (!ap->eof) {
3402 xfs_fsblock_t gotbno; /* right side block number */
3403 xfs_fsblock_t gotdiff=0; /* right side difference */
3404 xfs_fsblock_t prevbno; /* left side block number */
3405 xfs_fsblock_t prevdiff=0; /* left side difference */
3408 * If there's a previous (left) block, select a requested
3409 * start block based on it.
3411 if (ap->prev.br_startoff != NULLFILEOFF &&
3412 !isnullstartblock(ap->prev.br_startblock) &&
3413 (prevbno = ap->prev.br_startblock +
3414 ap->prev.br_blockcount) &&
3415 ISVALID(prevbno, ap->prev.br_startblock)) {
3417 * Calculate gap to end of previous block.
3419 adjust = prevdiff = ap->offset -
3420 (ap->prev.br_startoff +
3421 ap->prev.br_blockcount);
3423 * Figure the startblock based on the previous block's
3424 * end and the gap size.
3426 * If the gap is large relative to the piece we're
3427 * allocating, or using it gives us an invalid block
3428 * number, then just use the end of the previous block.
3430 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
3431 ISVALID(prevbno + prevdiff,
3432 ap->prev.br_startblock))
3437 * If the firstblock forbids it, can't use it,
3440 if (!rt && !nullfb &&
3441 XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
3442 prevbno = NULLFSBLOCK;
3445 * No previous block or can't follow it, just default.
3448 prevbno = NULLFSBLOCK;
3450 * If there's a following (right) block, select a requested
3451 * start block based on it.
3453 if (!isnullstartblock(ap->got.br_startblock)) {
3455 * Calculate gap to start of next block.
3457 adjust = gotdiff = ap->got.br_startoff - ap->offset;
3459 * Figure the startblock based on the next block's
3460 * start and the gap size.
3462 gotbno = ap->got.br_startblock;
3465 * If the gap is large relative to the piece we're
3466 * allocating, or using it gives us an invalid block
3467 * number, then just use the start of the next block
3468 * offset by our length.
3470 if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
3471 ISVALID(gotbno - gotdiff, gotbno))
3473 else if (ISVALID(gotbno - ap->length, gotbno)) {
3474 gotbno -= ap->length;
3475 gotdiff += adjust - ap->length;
3479 * If the firstblock forbids it, can't use it,
3482 if (!rt && !nullfb &&
3483 XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
3484 gotbno = NULLFSBLOCK;
3487 * No next block, just default.
3490 gotbno = NULLFSBLOCK;
3492 * If both valid, pick the better one, else the only good
3493 * one, else ap->blkno is already set (to 0 or the inode block).
3495 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
3496 ap->blkno = prevdiff <= gotdiff ? prevbno : gotbno;
3497 else if (prevbno != NULLFSBLOCK)
3498 ap->blkno = prevbno;
3499 else if (gotbno != NULLFSBLOCK)
3506 xfs_bmap_longest_free_extent(
3507 struct xfs_trans *tp,
3512 struct xfs_mount *mp = tp->t_mountp;
3513 struct xfs_perag *pag;
3514 xfs_extlen_t longest;
3517 pag = xfs_perag_get(mp, ag);
3518 if (!pag->pagf_init) {
3519 error = xfs_alloc_pagf_init(mp, tp, ag, XFS_ALLOC_FLAG_TRYLOCK);
3523 if (!pag->pagf_init) {
3529 longest = xfs_alloc_longest_free_extent(mp, pag,
3530 xfs_alloc_min_freelist(mp, pag));
3531 if (*blen < longest)
3540 xfs_bmap_select_minlen(
3541 struct xfs_bmalloca *ap,
3542 struct xfs_alloc_arg *args,
3546 if (notinit || *blen < ap->minlen) {
3548 * Since we did a BUF_TRYLOCK above, it is possible that
3549 * there is space for this request.
3551 args->minlen = ap->minlen;
3552 } else if (*blen < args->maxlen) {
3554 * If the best seen length is less than the request length,
3555 * use the best as the minimum.
3557 args->minlen = *blen;
3560 * Otherwise we've seen an extent as big as maxlen, use that
3563 args->minlen = args->maxlen;
3568 xfs_bmap_btalloc_nullfb(
3569 struct xfs_bmalloca *ap,
3570 struct xfs_alloc_arg *args,
3573 struct xfs_mount *mp = ap->ip->i_mount;
3574 xfs_agnumber_t ag, startag;
3578 args->type = XFS_ALLOCTYPE_START_BNO;
3579 args->total = ap->total;
3581 startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3582 if (startag == NULLAGNUMBER)
3585 while (*blen < args->maxlen) {
3586 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3591 if (++ag == mp->m_sb.sb_agcount)
3597 xfs_bmap_select_minlen(ap, args, blen, notinit);
3602 xfs_bmap_btalloc_filestreams(
3603 struct xfs_bmalloca *ap,
3604 struct xfs_alloc_arg *args,
3607 struct xfs_mount *mp = ap->ip->i_mount;
3612 args->type = XFS_ALLOCTYPE_NEAR_BNO;
3613 args->total = ap->total;
3615 ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3616 if (ag == NULLAGNUMBER)
3619 error = xfs_bmap_longest_free_extent(args->tp, ag, blen, ¬init);
3623 if (*blen < args->maxlen) {
3624 error = xfs_filestream_new_ag(ap, &ag);
3628 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3635 xfs_bmap_select_minlen(ap, args, blen, notinit);
3638 * Set the failure fallback case to look in the selected AG as stream
3641 ap->blkno = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0);
3647 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
3649 xfs_mount_t *mp; /* mount point structure */
3650 xfs_alloctype_t atype = 0; /* type for allocation routines */
3651 xfs_extlen_t align; /* minimum allocation alignment */
3652 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
3654 xfs_alloc_arg_t args;
3656 xfs_extlen_t nextminlen = 0;
3657 int nullfb; /* true if ap->firstblock isn't set */
3665 mp = ap->ip->i_mount;
3667 /* stripe alignment for allocation is determined by mount parameters */
3669 if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC))
3670 stripe_align = mp->m_swidth;
3671 else if (mp->m_dalign)
3672 stripe_align = mp->m_dalign;
3674 align = ap->userdata ? xfs_get_extsz_hint(ap->ip) : 0;
3675 if (unlikely(align)) {
3676 error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev,
3677 align, 0, ap->eof, 0, ap->conv,
3678 &ap->offset, &ap->length);
3684 nullfb = *ap->firstblock == NULLFSBLOCK;
3685 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
3687 if (ap->userdata && xfs_inode_is_filestream(ap->ip)) {
3688 ag = xfs_filestream_lookup_ag(ap->ip);
3689 ag = (ag != NULLAGNUMBER) ? ag : 0;
3690 ap->blkno = XFS_AGB_TO_FSB(mp, ag, 0);
3692 ap->blkno = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
3695 ap->blkno = *ap->firstblock;
3697 xfs_bmap_adjacent(ap);
3700 * If allowed, use ap->blkno; otherwise must use firstblock since
3701 * it's in the right allocation group.
3703 if (nullfb || XFS_FSB_TO_AGNO(mp, ap->blkno) == fb_agno)
3706 ap->blkno = *ap->firstblock;
3708 * Normal allocation, done through xfs_alloc_vextent.
3710 tryagain = isaligned = 0;
3711 memset(&args, 0, sizeof(args));
3714 args.fsbno = ap->blkno;
3716 /* Trim the allocation back to the maximum an AG can fit. */
3717 args.maxlen = MIN(ap->length, XFS_ALLOC_AG_MAX_USABLE(mp));
3718 args.firstblock = *ap->firstblock;
3722 * Search for an allocation group with a single extent large
3723 * enough for the request. If one isn't found, then adjust
3724 * the minimum allocation size to the largest space found.
3726 if (ap->userdata && xfs_inode_is_filestream(ap->ip))
3727 error = xfs_bmap_btalloc_filestreams(ap, &args, &blen);
3729 error = xfs_bmap_btalloc_nullfb(ap, &args, &blen);
3732 } else if (ap->flist->xbf_low) {
3733 if (xfs_inode_is_filestream(ap->ip))
3734 args.type = XFS_ALLOCTYPE_FIRST_AG;
3736 args.type = XFS_ALLOCTYPE_START_BNO;
3737 args.total = args.minlen = ap->minlen;
3739 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3740 args.total = ap->total;
3741 args.minlen = ap->minlen;
3743 /* apply extent size hints if obtained earlier */
3744 if (unlikely(align)) {
3746 if ((args.mod = (xfs_extlen_t)do_mod(ap->offset, args.prod)))
3747 args.mod = (xfs_extlen_t)(args.prod - args.mod);
3748 } else if (mp->m_sb.sb_blocksize >= PAGE_CACHE_SIZE) {
3752 args.prod = PAGE_CACHE_SIZE >> mp->m_sb.sb_blocklog;
3753 if ((args.mod = (xfs_extlen_t)(do_mod(ap->offset, args.prod))))
3754 args.mod = (xfs_extlen_t)(args.prod - args.mod);
3757 * If we are not low on available data blocks, and the
3758 * underlying logical volume manager is a stripe, and
3759 * the file offset is zero then try to allocate data
3760 * blocks on stripe unit boundary.
3761 * NOTE: ap->aeof is only set if the allocation length
3762 * is >= the stripe unit and the allocation offset is
3763 * at the end of file.
3765 if (!ap->flist->xbf_low && ap->aeof) {
3767 args.alignment = stripe_align;
3771 * Adjust for alignment
3773 if (blen > args.alignment && blen <= args.maxlen)
3774 args.minlen = blen - args.alignment;
3775 args.minalignslop = 0;
3778 * First try an exact bno allocation.
3779 * If it fails then do a near or start bno
3780 * allocation with alignment turned on.
3784 args.type = XFS_ALLOCTYPE_THIS_BNO;
3787 * Compute the minlen+alignment for the
3788 * next case. Set slop so that the value
3789 * of minlen+alignment+slop doesn't go up
3790 * between the calls.
3792 if (blen > stripe_align && blen <= args.maxlen)
3793 nextminlen = blen - stripe_align;
3795 nextminlen = args.minlen;
3796 if (nextminlen + stripe_align > args.minlen + 1)
3798 nextminlen + stripe_align -
3801 args.minalignslop = 0;
3805 args.minalignslop = 0;
3807 args.minleft = ap->minleft;
3808 args.wasdel = ap->wasdel;
3810 args.userdata = ap->userdata;
3811 if (ap->userdata & XFS_ALLOC_USERDATA_ZERO)
3814 error = xfs_alloc_vextent(&args);
3818 if (tryagain && args.fsbno == NULLFSBLOCK) {
3820 * Exact allocation failed. Now try with alignment
3824 args.fsbno = ap->blkno;
3825 args.alignment = stripe_align;
3826 args.minlen = nextminlen;
3827 args.minalignslop = 0;
3829 if ((error = xfs_alloc_vextent(&args)))
3832 if (isaligned && args.fsbno == NULLFSBLOCK) {
3834 * allocation failed, so turn off alignment and
3838 args.fsbno = ap->blkno;
3840 if ((error = xfs_alloc_vextent(&args)))
3843 if (args.fsbno == NULLFSBLOCK && nullfb &&
3844 args.minlen > ap->minlen) {
3845 args.minlen = ap->minlen;
3846 args.type = XFS_ALLOCTYPE_START_BNO;
3847 args.fsbno = ap->blkno;
3848 if ((error = xfs_alloc_vextent(&args)))
3851 if (args.fsbno == NULLFSBLOCK && nullfb) {
3853 args.type = XFS_ALLOCTYPE_FIRST_AG;
3854 args.total = ap->minlen;
3856 if ((error = xfs_alloc_vextent(&args)))
3858 ap->flist->xbf_low = 1;
3860 if (args.fsbno != NULLFSBLOCK) {
3862 * check the allocation happened at the same or higher AG than
3863 * the first block that was allocated.
3865 ASSERT(*ap->firstblock == NULLFSBLOCK ||
3866 XFS_FSB_TO_AGNO(mp, *ap->firstblock) ==
3867 XFS_FSB_TO_AGNO(mp, args.fsbno) ||
3868 (ap->flist->xbf_low &&
3869 XFS_FSB_TO_AGNO(mp, *ap->firstblock) <
3870 XFS_FSB_TO_AGNO(mp, args.fsbno)));
3872 ap->blkno = args.fsbno;
3873 if (*ap->firstblock == NULLFSBLOCK)
3874 *ap->firstblock = args.fsbno;
3875 ASSERT(nullfb || fb_agno == args.agno ||
3876 (ap->flist->xbf_low && fb_agno < args.agno));
3877 ap->length = args.len;
3878 ap->ip->i_d.di_nblocks += args.len;
3879 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
3881 ap->ip->i_delayed_blks -= args.len;
3883 * Adjust the disk quota also. This was reserved
3886 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
3887 ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT :
3888 XFS_TRANS_DQ_BCOUNT,
3891 ap->blkno = NULLFSBLOCK;
3898 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
3899 * It figures out where to ask the underlying allocator to put the new extent.
3903 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
3905 if (XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata)
3906 return xfs_bmap_rtalloc(ap);
3907 return xfs_bmap_btalloc(ap);
3911 * Trim the returned map to the required bounds
3915 struct xfs_bmbt_irec *mval,
3916 struct xfs_bmbt_irec *got,
3924 if ((flags & XFS_BMAPI_ENTIRE) ||
3925 got->br_startoff + got->br_blockcount <= obno) {
3927 if (isnullstartblock(got->br_startblock))
3928 mval->br_startblock = DELAYSTARTBLOCK;
3934 ASSERT((*bno >= obno) || (n == 0));
3936 mval->br_startoff = *bno;
3937 if (isnullstartblock(got->br_startblock))
3938 mval->br_startblock = DELAYSTARTBLOCK;
3940 mval->br_startblock = got->br_startblock +
3941 (*bno - got->br_startoff);
3943 * Return the minimum of what we got and what we asked for for
3944 * the length. We can use the len variable here because it is
3945 * modified below and we could have been there before coming
3946 * here if the first part of the allocation didn't overlap what
3949 mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno,
3950 got->br_blockcount - (*bno - got->br_startoff));
3951 mval->br_state = got->br_state;
3952 ASSERT(mval->br_blockcount <= len);
3957 * Update and validate the extent map to return
3960 xfs_bmapi_update_map(
3961 struct xfs_bmbt_irec **map,
3969 xfs_bmbt_irec_t *mval = *map;
3971 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
3972 ((mval->br_startoff + mval->br_blockcount) <= end));
3973 ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) ||
3974 (mval->br_startoff < obno));
3976 *bno = mval->br_startoff + mval->br_blockcount;
3978 if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) {
3979 /* update previous map with new information */
3980 ASSERT(mval->br_startblock == mval[-1].br_startblock);
3981 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
3982 ASSERT(mval->br_state == mval[-1].br_state);
3983 mval[-1].br_blockcount = mval->br_blockcount;
3984 mval[-1].br_state = mval->br_state;
3985 } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
3986 mval[-1].br_startblock != DELAYSTARTBLOCK &&
3987 mval[-1].br_startblock != HOLESTARTBLOCK &&
3988 mval->br_startblock == mval[-1].br_startblock +
3989 mval[-1].br_blockcount &&
3990 ((flags & XFS_BMAPI_IGSTATE) ||
3991 mval[-1].br_state == mval->br_state)) {
3992 ASSERT(mval->br_startoff ==
3993 mval[-1].br_startoff + mval[-1].br_blockcount);
3994 mval[-1].br_blockcount += mval->br_blockcount;
3995 } else if (*n > 0 &&
3996 mval->br_startblock == DELAYSTARTBLOCK &&
3997 mval[-1].br_startblock == DELAYSTARTBLOCK &&
3998 mval->br_startoff ==
3999 mval[-1].br_startoff + mval[-1].br_blockcount) {
4000 mval[-1].br_blockcount += mval->br_blockcount;
4001 mval[-1].br_state = mval->br_state;
4002 } else if (!((*n == 0) &&
4003 ((mval->br_startoff + mval->br_blockcount) <=
4012 * Map file blocks to filesystem blocks without allocation.
4016 struct xfs_inode *ip,
4019 struct xfs_bmbt_irec *mval,
4023 struct xfs_mount *mp = ip->i_mount;
4024 struct xfs_ifork *ifp;
4025 struct xfs_bmbt_irec got;
4026 struct xfs_bmbt_irec prev;
4033 int whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
4034 XFS_ATTR_FORK : XFS_DATA_FORK;
4037 ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK|XFS_BMAPI_ENTIRE|
4038 XFS_BMAPI_IGSTATE)));
4039 ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED|XFS_ILOCK_EXCL));
4041 if (unlikely(XFS_TEST_ERROR(
4042 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4043 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
4044 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
4045 XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW, mp);
4046 return -EFSCORRUPTED;
4049 if (XFS_FORCED_SHUTDOWN(mp))
4052 XFS_STATS_INC(mp, xs_blk_mapr);
4054 ifp = XFS_IFORK_PTR(ip, whichfork);
4056 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4057 error = xfs_iread_extents(NULL, ip, whichfork);
4062 xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got, &prev);
4066 while (bno < end && n < *nmap) {
4067 /* Reading past eof, act as though there's a hole up to end. */
4069 got.br_startoff = end;
4070 if (got.br_startoff > bno) {
4071 /* Reading in a hole. */
4072 mval->br_startoff = bno;
4073 mval->br_startblock = HOLESTARTBLOCK;
4074 mval->br_blockcount =
4075 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
4076 mval->br_state = XFS_EXT_NORM;
4077 bno += mval->br_blockcount;
4078 len -= mval->br_blockcount;
4084 /* set up the extent map to return. */
4085 xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
4086 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4088 /* If we're done, stop now. */
4089 if (bno >= end || n >= *nmap)
4092 /* Else go on to the next record. */
4093 if (++lastx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t))
4094 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx), &got);
4103 xfs_bmapi_reserve_delalloc(
4104 struct xfs_inode *ip,
4107 struct xfs_bmbt_irec *got,
4108 struct xfs_bmbt_irec *prev,
4109 xfs_extnum_t *lastx,
4112 struct xfs_mount *mp = ip->i_mount;
4113 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
4115 xfs_extlen_t indlen;
4116 char rt = XFS_IS_REALTIME_INODE(ip);
4120 alen = XFS_FILBLKS_MIN(len, MAXEXTLEN);
4122 alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff);
4124 /* Figure out the extent size, adjust alen */
4125 extsz = xfs_get_extsz_hint(ip);
4127 error = xfs_bmap_extsize_align(mp, got, prev, extsz, rt, eof,
4128 1, 0, &aoff, &alen);
4133 extsz = alen / mp->m_sb.sb_rextsize;
4136 * Make a transaction-less quota reservation for delayed allocation
4137 * blocks. This number gets adjusted later. We return if we haven't
4138 * allocated blocks already inside this loop.
4140 error = xfs_trans_reserve_quota_nblks(NULL, ip, (long)alen, 0,
4141 rt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
4146 * Split changing sb for alen and indlen since they could be coming
4147 * from different places.
4149 indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen);
4153 error = xfs_mod_frextents(mp, -((int64_t)extsz));
4155 error = xfs_mod_fdblocks(mp, -((int64_t)alen), false);
4159 goto out_unreserve_quota;
4161 error = xfs_mod_fdblocks(mp, -((int64_t)indlen), false);
4163 goto out_unreserve_blocks;
4166 ip->i_delayed_blks += alen;
4168 got->br_startoff = aoff;
4169 got->br_startblock = nullstartblock(indlen);
4170 got->br_blockcount = alen;
4171 got->br_state = XFS_EXT_NORM;
4172 xfs_bmap_add_extent_hole_delay(ip, lastx, got);
4175 * Update our extent pointer, given that xfs_bmap_add_extent_hole_delay
4176 * might have merged it into one of the neighbouring ones.
4178 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *lastx), got);
4180 ASSERT(got->br_startoff <= aoff);
4181 ASSERT(got->br_startoff + got->br_blockcount >= aoff + alen);
4182 ASSERT(isnullstartblock(got->br_startblock));
4183 ASSERT(got->br_state == XFS_EXT_NORM);
4186 out_unreserve_blocks:
4188 xfs_mod_frextents(mp, extsz);
4190 xfs_mod_fdblocks(mp, alen, false);
4191 out_unreserve_quota:
4192 if (XFS_IS_QUOTA_ON(mp))
4193 xfs_trans_unreserve_quota_nblks(NULL, ip, (long)alen, 0, rt ?
4194 XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
4199 * Map file blocks to filesystem blocks, adding delayed allocations as needed.
4203 struct xfs_inode *ip, /* incore inode */
4204 xfs_fileoff_t bno, /* starting file offs. mapped */
4205 xfs_filblks_t len, /* length to map in file */
4206 struct xfs_bmbt_irec *mval, /* output: map values */
4207 int *nmap, /* i/o: mval size/count */
4208 int flags) /* XFS_BMAPI_... */
4210 struct xfs_mount *mp = ip->i_mount;
4211 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
4212 struct xfs_bmbt_irec got; /* current file extent record */
4213 struct xfs_bmbt_irec prev; /* previous file extent record */
4214 xfs_fileoff_t obno; /* old block number (offset) */
4215 xfs_fileoff_t end; /* end of mapped file region */
4216 xfs_extnum_t lastx; /* last useful extent number */
4217 int eof; /* we've hit the end of extents */
4218 int n = 0; /* current extent index */
4222 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP);
4223 ASSERT(!(flags & ~XFS_BMAPI_ENTIRE));
4224 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
4226 if (unlikely(XFS_TEST_ERROR(
4227 (XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_EXTENTS &&
4228 XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_BTREE),
4229 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
4230 XFS_ERROR_REPORT("xfs_bmapi_delay", XFS_ERRLEVEL_LOW, mp);
4231 return -EFSCORRUPTED;
4234 if (XFS_FORCED_SHUTDOWN(mp))
4237 XFS_STATS_INC(mp, xs_blk_mapw);
4239 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4240 error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK);
4245 xfs_bmap_search_extents(ip, bno, XFS_DATA_FORK, &eof, &lastx, &got, &prev);
4249 while (bno < end && n < *nmap) {
4250 if (eof || got.br_startoff > bno) {
4251 error = xfs_bmapi_reserve_delalloc(ip, bno, len, &got,
4252 &prev, &lastx, eof);
4262 /* set up the extent map to return. */
4263 xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
4264 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4266 /* If we're done, stop now. */
4267 if (bno >= end || n >= *nmap)
4270 /* Else go on to the next record. */
4272 if (++lastx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t))
4273 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx), &got);
4285 struct xfs_bmalloca *bma)
4287 struct xfs_mount *mp = bma->ip->i_mount;
4288 int whichfork = (bma->flags & XFS_BMAPI_ATTRFORK) ?
4289 XFS_ATTR_FORK : XFS_DATA_FORK;
4290 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
4291 int tmp_logflags = 0;
4294 ASSERT(bma->length > 0);
4297 * For the wasdelay case, we could also just allocate the stuff asked
4298 * for in this bmap call but that wouldn't be as good.
4301 bma->length = (xfs_extlen_t)bma->got.br_blockcount;
4302 bma->offset = bma->got.br_startoff;
4303 if (bma->idx != NULLEXTNUM && bma->idx) {
4304 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1),
4308 bma->length = XFS_FILBLKS_MIN(bma->length, MAXEXTLEN);
4310 bma->length = XFS_FILBLKS_MIN(bma->length,
4311 bma->got.br_startoff - bma->offset);
4315 * Indicate if this is the first user data in the file, or just any
4316 * user data. And if it is userdata, indicate whether it needs to
4317 * be initialised to zero during allocation.
4319 if (!(bma->flags & XFS_BMAPI_METADATA)) {
4320 bma->userdata = (bma->offset == 0) ?
4321 XFS_ALLOC_INITIAL_USER_DATA : XFS_ALLOC_USERDATA;
4322 if (bma->flags & XFS_BMAPI_ZERO)
4323 bma->userdata |= XFS_ALLOC_USERDATA_ZERO;
4326 bma->minlen = (bma->flags & XFS_BMAPI_CONTIG) ? bma->length : 1;
4329 * Only want to do the alignment at the eof if it is userdata and
4330 * allocation length is larger than a stripe unit.
4332 if (mp->m_dalign && bma->length >= mp->m_dalign &&
4333 !(bma->flags & XFS_BMAPI_METADATA) && whichfork == XFS_DATA_FORK) {
4334 error = xfs_bmap_isaeof(bma, whichfork);
4339 error = xfs_bmap_alloc(bma);
4343 if (bma->flist->xbf_low)
4346 bma->cur->bc_private.b.firstblock = *bma->firstblock;
4347 if (bma->blkno == NULLFSBLOCK)
4349 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4350 bma->cur = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork);
4351 bma->cur->bc_private.b.firstblock = *bma->firstblock;
4352 bma->cur->bc_private.b.flist = bma->flist;
4355 * Bump the number of extents we've allocated
4361 bma->cur->bc_private.b.flags =
4362 bma->wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
4364 bma->got.br_startoff = bma->offset;
4365 bma->got.br_startblock = bma->blkno;
4366 bma->got.br_blockcount = bma->length;
4367 bma->got.br_state = XFS_EXT_NORM;
4370 * A wasdelay extent has been initialized, so shouldn't be flagged
4373 if (!bma->wasdel && (bma->flags & XFS_BMAPI_PREALLOC) &&
4374 xfs_sb_version_hasextflgbit(&mp->m_sb))
4375 bma->got.br_state = XFS_EXT_UNWRITTEN;
4378 error = xfs_bmap_add_extent_delay_real(bma);
4380 error = xfs_bmap_add_extent_hole_real(bma, whichfork);
4382 bma->logflags |= tmp_logflags;
4387 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4388 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4389 * the neighbouring ones.
4391 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &bma->got);
4393 ASSERT(bma->got.br_startoff <= bma->offset);
4394 ASSERT(bma->got.br_startoff + bma->got.br_blockcount >=
4395 bma->offset + bma->length);
4396 ASSERT(bma->got.br_state == XFS_EXT_NORM ||
4397 bma->got.br_state == XFS_EXT_UNWRITTEN);
4402 xfs_bmapi_convert_unwritten(
4403 struct xfs_bmalloca *bma,
4404 struct xfs_bmbt_irec *mval,
4408 int whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
4409 XFS_ATTR_FORK : XFS_DATA_FORK;
4410 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
4411 int tmp_logflags = 0;
4414 /* check if we need to do unwritten->real conversion */
4415 if (mval->br_state == XFS_EXT_UNWRITTEN &&
4416 (flags & XFS_BMAPI_PREALLOC))
4419 /* check if we need to do real->unwritten conversion */
4420 if (mval->br_state == XFS_EXT_NORM &&
4421 (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) !=
4422 (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT))
4426 * Modify (by adding) the state flag, if writing.
4428 ASSERT(mval->br_blockcount <= len);
4429 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4430 bma->cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp,
4431 bma->ip, whichfork);
4432 bma->cur->bc_private.b.firstblock = *bma->firstblock;
4433 bma->cur->bc_private.b.flist = bma->flist;
4435 mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN)
4436 ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
4439 * Before insertion into the bmbt, zero the range being converted
4442 if (flags & XFS_BMAPI_ZERO) {
4443 error = xfs_zero_extent(bma->ip, mval->br_startblock,
4444 mval->br_blockcount);
4449 error = xfs_bmap_add_extent_unwritten_real(bma->tp, bma->ip, &bma->idx,
4450 &bma->cur, mval, bma->firstblock, bma->flist,
4453 * Log the inode core unconditionally in the unwritten extent conversion
4454 * path because the conversion might not have done so (e.g., if the
4455 * extent count hasn't changed). We need to make sure the inode is dirty
4456 * in the transaction for the sake of fsync(), even if nothing has
4457 * changed, because fsync() will not force the log for this transaction
4458 * unless it sees the inode pinned.
4460 bma->logflags |= tmp_logflags | XFS_ILOG_CORE;
4465 * Update our extent pointer, given that
4466 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4467 * of the neighbouring ones.
4469 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &bma->got);
4472 * We may have combined previously unwritten space with written space,
4473 * so generate another request.
4475 if (mval->br_blockcount < len)
4481 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4482 * extent state if necessary. Details behaviour is controlled by the flags
4483 * parameter. Only allocates blocks from a single allocation group, to avoid
4486 * The returned value in "firstblock" from the first call in a transaction
4487 * must be remembered and presented to subsequent calls in "firstblock".
4488 * An upper bound for the number of blocks to be allocated is supplied to
4489 * the first call in "total"; if no allocation group has that many free
4490 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4494 struct xfs_trans *tp, /* transaction pointer */
4495 struct xfs_inode *ip, /* incore inode */
4496 xfs_fileoff_t bno, /* starting file offs. mapped */
4497 xfs_filblks_t len, /* length to map in file */
4498 int flags, /* XFS_BMAPI_... */
4499 xfs_fsblock_t *firstblock, /* first allocated block
4500 controls a.g. for allocs */
4501 xfs_extlen_t total, /* total blocks needed */
4502 struct xfs_bmbt_irec *mval, /* output: map values */
4503 int *nmap, /* i/o: mval size/count */
4504 struct xfs_bmap_free *flist) /* i/o: list extents to free */
4506 struct xfs_mount *mp = ip->i_mount;
4507 struct xfs_ifork *ifp;
4508 struct xfs_bmalloca bma = { NULL }; /* args for xfs_bmap_alloc */
4509 xfs_fileoff_t end; /* end of mapped file region */
4510 int eof; /* after the end of extents */
4511 int error; /* error return */
4512 int n; /* current extent index */
4513 xfs_fileoff_t obno; /* old block number (offset) */
4514 int whichfork; /* data or attr fork */
4515 char inhole; /* current location is hole in file */
4516 char wasdelay; /* old extent was delayed */
4519 xfs_fileoff_t orig_bno; /* original block number value */
4520 int orig_flags; /* original flags arg value */
4521 xfs_filblks_t orig_len; /* original value of len arg */
4522 struct xfs_bmbt_irec *orig_mval; /* original value of mval */
4523 int orig_nmap; /* original value of *nmap */
4531 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
4532 XFS_ATTR_FORK : XFS_DATA_FORK;
4535 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP);
4536 ASSERT(!(flags & XFS_BMAPI_IGSTATE));
4539 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL);
4540 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
4542 /* zeroing is for currently only for data extents, not metadata */
4543 ASSERT((flags & (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO)) !=
4544 (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO));
4546 * we can allocate unwritten extents or pre-zero allocated blocks,
4547 * but it makes no sense to do both at once. This would result in
4548 * zeroing the unwritten extent twice, but it still being an
4549 * unwritten extent....
4551 ASSERT((flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)) !=
4552 (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO));
4554 if (unlikely(XFS_TEST_ERROR(
4555 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4556 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
4557 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
4558 XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW, mp);
4559 return -EFSCORRUPTED;
4562 if (XFS_FORCED_SHUTDOWN(mp))
4565 ifp = XFS_IFORK_PTR(ip, whichfork);
4567 XFS_STATS_INC(mp, xs_blk_mapw);
4569 if (*firstblock == NULLFSBLOCK) {
4570 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE)
4571 bma.minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1;
4578 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4579 error = xfs_iread_extents(tp, ip, whichfork);
4584 xfs_bmap_search_extents(ip, bno, whichfork, &eof, &bma.idx, &bma.got,
4595 bma.firstblock = firstblock;
4597 while (bno < end && n < *nmap) {
4598 inhole = eof || bma.got.br_startoff > bno;
4599 wasdelay = !inhole && isnullstartblock(bma.got.br_startblock);
4602 * First, deal with the hole before the allocated space
4603 * that we found, if any.
4605 if (inhole || wasdelay) {
4607 bma.conv = !!(flags & XFS_BMAPI_CONVERT);
4608 bma.wasdel = wasdelay;
4613 * There's a 32/64 bit type mismatch between the
4614 * allocation length request (which can be 64 bits in
4615 * length) and the bma length request, which is
4616 * xfs_extlen_t and therefore 32 bits. Hence we have to
4617 * check for 32-bit overflows and handle them here.
4619 if (len > (xfs_filblks_t)MAXEXTLEN)
4620 bma.length = MAXEXTLEN;
4625 ASSERT(bma.length > 0);
4626 error = xfs_bmapi_allocate(&bma);
4629 if (bma.blkno == NULLFSBLOCK)
4633 /* Deal with the allocated space we found. */
4634 xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno,
4637 /* Execute unwritten extent conversion if necessary */
4638 error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags);
4639 if (error == -EAGAIN)
4644 /* update the extent map to return */
4645 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4648 * If we're done, stop now. Stop when we've allocated
4649 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4650 * the transaction may get too big.
4652 if (bno >= end || n >= *nmap || bma.nallocs >= *nmap)
4655 /* Else go on to the next record. */
4657 if (++bma.idx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t)) {
4658 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma.idx),
4666 * Transform from btree to extents, give it cur.
4668 if (xfs_bmap_wants_extents(ip, whichfork)) {
4669 int tmp_logflags = 0;
4672 error = xfs_bmap_btree_to_extents(tp, ip, bma.cur,
4673 &tmp_logflags, whichfork);
4674 bma.logflags |= tmp_logflags;
4679 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE ||
4680 XFS_IFORK_NEXTENTS(ip, whichfork) >
4681 XFS_IFORK_MAXEXT(ip, whichfork));
4685 * Log everything. Do this after conversion, there's no point in
4686 * logging the extent records if we've converted to btree format.
4688 if ((bma.logflags & xfs_ilog_fext(whichfork)) &&
4689 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
4690 bma.logflags &= ~xfs_ilog_fext(whichfork);
4691 else if ((bma.logflags & xfs_ilog_fbroot(whichfork)) &&
4692 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
4693 bma.logflags &= ~xfs_ilog_fbroot(whichfork);
4695 * Log whatever the flags say, even if error. Otherwise we might miss
4696 * detecting a case where the data is changed, there's an error,
4697 * and it's not logged so we don't shutdown when we should.
4700 xfs_trans_log_inode(tp, ip, bma.logflags);
4704 ASSERT(*firstblock == NULLFSBLOCK ||
4705 XFS_FSB_TO_AGNO(mp, *firstblock) ==
4707 bma.cur->bc_private.b.firstblock) ||
4709 XFS_FSB_TO_AGNO(mp, *firstblock) <
4711 bma.cur->bc_private.b.firstblock)));
4712 *firstblock = bma.cur->bc_private.b.firstblock;
4714 xfs_btree_del_cursor(bma.cur,
4715 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
4718 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
4724 * Called by xfs_bmapi to update file extent records and the btree
4725 * after removing space (or undoing a delayed allocation).
4727 STATIC int /* error */
4728 xfs_bmap_del_extent(
4729 xfs_inode_t *ip, /* incore inode pointer */
4730 xfs_trans_t *tp, /* current transaction pointer */
4731 xfs_extnum_t *idx, /* extent number to update/delete */
4732 xfs_bmap_free_t *flist, /* list of extents to be freed */
4733 xfs_btree_cur_t *cur, /* if null, not a btree */
4734 xfs_bmbt_irec_t *del, /* data to remove from extents */
4735 int *logflagsp, /* inode logging flags */
4736 int whichfork) /* data or attr fork */
4738 xfs_filblks_t da_new; /* new delay-alloc indirect blocks */
4739 xfs_filblks_t da_old; /* old delay-alloc indirect blocks */
4740 xfs_fsblock_t del_endblock=0; /* first block past del */
4741 xfs_fileoff_t del_endoff; /* first offset past del */
4742 int delay; /* current block is delayed allocated */
4743 int do_fx; /* free extent at end of routine */
4744 xfs_bmbt_rec_host_t *ep; /* current extent entry pointer */
4745 int error; /* error return value */
4746 int flags; /* inode logging flags */
4747 xfs_bmbt_irec_t got; /* current extent entry */
4748 xfs_fileoff_t got_endoff; /* first offset past got */
4749 int i; /* temp state */
4750 xfs_ifork_t *ifp; /* inode fork pointer */
4751 xfs_mount_t *mp; /* mount structure */
4752 xfs_filblks_t nblks; /* quota/sb block count */
4753 xfs_bmbt_irec_t new; /* new record to be inserted */
4755 uint qfield; /* quota field to update */
4756 xfs_filblks_t temp; /* for indirect length calculations */
4757 xfs_filblks_t temp2; /* for indirect length calculations */
4761 XFS_STATS_INC(mp, xs_del_exlist);
4763 if (whichfork == XFS_ATTR_FORK)
4764 state |= BMAP_ATTRFORK;
4766 ifp = XFS_IFORK_PTR(ip, whichfork);
4767 ASSERT((*idx >= 0) && (*idx < ifp->if_bytes /
4768 (uint)sizeof(xfs_bmbt_rec_t)));
4769 ASSERT(del->br_blockcount > 0);
4770 ep = xfs_iext_get_ext(ifp, *idx);
4771 xfs_bmbt_get_all(ep, &got);
4772 ASSERT(got.br_startoff <= del->br_startoff);
4773 del_endoff = del->br_startoff + del->br_blockcount;
4774 got_endoff = got.br_startoff + got.br_blockcount;
4775 ASSERT(got_endoff >= del_endoff);
4776 delay = isnullstartblock(got.br_startblock);
4777 ASSERT(isnullstartblock(del->br_startblock) == delay);
4782 * If deleting a real allocation, must free up the disk space.
4785 flags = XFS_ILOG_CORE;
4787 * Realtime allocation. Free it and record di_nblocks update.
4789 if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) {
4793 ASSERT(do_mod(del->br_blockcount,
4794 mp->m_sb.sb_rextsize) == 0);
4795 ASSERT(do_mod(del->br_startblock,
4796 mp->m_sb.sb_rextsize) == 0);
4797 bno = del->br_startblock;
4798 len = del->br_blockcount;
4799 do_div(bno, mp->m_sb.sb_rextsize);
4800 do_div(len, mp->m_sb.sb_rextsize);
4801 error = xfs_rtfree_extent(tp, bno, (xfs_extlen_t)len);
4805 nblks = len * mp->m_sb.sb_rextsize;
4806 qfield = XFS_TRANS_DQ_RTBCOUNT;
4809 * Ordinary allocation.
4813 nblks = del->br_blockcount;
4814 qfield = XFS_TRANS_DQ_BCOUNT;
4817 * Set up del_endblock and cur for later.
4819 del_endblock = del->br_startblock + del->br_blockcount;
4821 if ((error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
4822 got.br_startblock, got.br_blockcount,
4825 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
4827 da_old = da_new = 0;
4829 da_old = startblockval(got.br_startblock);
4835 * Set flag value to use in switch statement.
4836 * Left-contig is 2, right-contig is 1.
4838 switch (((got.br_startoff == del->br_startoff) << 1) |
4839 (got_endoff == del_endoff)) {
4842 * Matches the whole extent. Delete the entry.
4844 xfs_iext_remove(ip, *idx, 1,
4845 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0);
4850 XFS_IFORK_NEXT_SET(ip, whichfork,
4851 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
4852 flags |= XFS_ILOG_CORE;
4854 flags |= xfs_ilog_fext(whichfork);
4857 if ((error = xfs_btree_delete(cur, &i)))
4859 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
4864 * Deleting the first part of the extent.
4866 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4867 xfs_bmbt_set_startoff(ep, del_endoff);
4868 temp = got.br_blockcount - del->br_blockcount;
4869 xfs_bmbt_set_blockcount(ep, temp);
4871 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
4873 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
4874 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
4878 xfs_bmbt_set_startblock(ep, del_endblock);
4879 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
4881 flags |= xfs_ilog_fext(whichfork);
4884 if ((error = xfs_bmbt_update(cur, del_endoff, del_endblock,
4885 got.br_blockcount - del->br_blockcount,
4892 * Deleting the last part of the extent.
4894 temp = got.br_blockcount - del->br_blockcount;
4895 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4896 xfs_bmbt_set_blockcount(ep, temp);
4898 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
4900 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
4901 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
4905 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
4907 flags |= xfs_ilog_fext(whichfork);
4910 if ((error = xfs_bmbt_update(cur, got.br_startoff,
4912 got.br_blockcount - del->br_blockcount,
4919 * Deleting the middle of the extent.
4921 temp = del->br_startoff - got.br_startoff;
4922 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4923 xfs_bmbt_set_blockcount(ep, temp);
4924 new.br_startoff = del_endoff;
4925 temp2 = got_endoff - del_endoff;
4926 new.br_blockcount = temp2;
4927 new.br_state = got.br_state;
4929 new.br_startblock = del_endblock;
4930 flags |= XFS_ILOG_CORE;
4932 if ((error = xfs_bmbt_update(cur,
4934 got.br_startblock, temp,
4937 if ((error = xfs_btree_increment(cur, 0, &i)))
4939 cur->bc_rec.b = new;
4940 error = xfs_btree_insert(cur, &i);
4941 if (error && error != -ENOSPC)
4944 * If get no-space back from btree insert,
4945 * it tried a split, and we have a zero
4946 * block reservation.
4947 * Fix up our state and return the error.
4949 if (error == -ENOSPC) {
4951 * Reset the cursor, don't trust
4952 * it after any insert operation.
4954 if ((error = xfs_bmbt_lookup_eq(cur,
4959 XFS_WANT_CORRUPTED_GOTO(mp,
4962 * Update the btree record back
4963 * to the original value.
4965 if ((error = xfs_bmbt_update(cur,
4972 * Reset the extent record back
4973 * to the original value.
4975 xfs_bmbt_set_blockcount(ep,
4981 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
4983 flags |= xfs_ilog_fext(whichfork);
4984 XFS_IFORK_NEXT_SET(ip, whichfork,
4985 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
4987 ASSERT(whichfork == XFS_DATA_FORK);
4988 temp = xfs_bmap_worst_indlen(ip, temp);
4989 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
4990 temp2 = xfs_bmap_worst_indlen(ip, temp2);
4991 new.br_startblock = nullstartblock((int)temp2);
4992 da_new = temp + temp2;
4993 while (da_new > da_old) {
4997 xfs_bmbt_set_startblock(ep,
4998 nullstartblock((int)temp));
5000 if (da_new == da_old)
5006 nullstartblock((int)temp2);
5010 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5011 xfs_iext_insert(ip, *idx + 1, 1, &new, state);
5016 * If we need to, add to list of extents to delete.
5019 xfs_bmap_add_free(del->br_startblock, del->br_blockcount, flist,
5022 * Adjust inode # blocks in the file.
5025 ip->i_d.di_nblocks -= nblks;
5027 * Adjust quota data.
5030 xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks);
5033 * Account for change in delayed indirect blocks.
5034 * Nothing to do for disk quota accounting here.
5036 ASSERT(da_old >= da_new);
5037 if (da_old > da_new)
5038 xfs_mod_fdblocks(mp, (int64_t)(da_old - da_new), false);
5045 * Unmap (remove) blocks from a file.
5046 * If nexts is nonzero then the number of extents to remove is limited to
5047 * that value. If not all extents in the block range can be removed then
5052 xfs_trans_t *tp, /* transaction pointer */
5053 struct xfs_inode *ip, /* incore inode */
5054 xfs_fileoff_t bno, /* starting offset to unmap */
5055 xfs_filblks_t len, /* length to unmap in file */
5056 int flags, /* misc flags */
5057 xfs_extnum_t nexts, /* number of extents max */
5058 xfs_fsblock_t *firstblock, /* first allocated block
5059 controls a.g. for allocs */
5060 xfs_bmap_free_t *flist, /* i/o: list extents to free */
5061 int *done) /* set if not done yet */
5063 xfs_btree_cur_t *cur; /* bmap btree cursor */
5064 xfs_bmbt_irec_t del; /* extent being deleted */
5065 int eof; /* is deleting at eof */
5066 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
5067 int error; /* error return value */
5068 xfs_extnum_t extno; /* extent number in list */
5069 xfs_bmbt_irec_t got; /* current extent record */
5070 xfs_ifork_t *ifp; /* inode fork pointer */
5071 int isrt; /* freeing in rt area */
5072 xfs_extnum_t lastx; /* last extent index used */
5073 int logflags; /* transaction logging flags */
5074 xfs_extlen_t mod; /* rt extent offset */
5075 xfs_mount_t *mp; /* mount structure */
5076 xfs_extnum_t nextents; /* number of file extents */
5077 xfs_bmbt_irec_t prev; /* previous extent record */
5078 xfs_fileoff_t start; /* first file offset deleted */
5079 int tmp_logflags; /* partial logging flags */
5080 int wasdel; /* was a delayed alloc extent */
5081 int whichfork; /* data or attribute fork */
5084 trace_xfs_bunmap(ip, bno, len, flags, _RET_IP_);
5086 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
5087 XFS_ATTR_FORK : XFS_DATA_FORK;
5088 ifp = XFS_IFORK_PTR(ip, whichfork);
5090 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5091 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
5092 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW,
5094 return -EFSCORRUPTED;
5097 if (XFS_FORCED_SHUTDOWN(mp))
5100 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
5104 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5105 (error = xfs_iread_extents(tp, ip, whichfork)))
5107 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5108 if (nextents == 0) {
5112 XFS_STATS_INC(mp, xs_blk_unmap);
5113 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
5115 bno = start + len - 1;
5116 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
5120 * Check to see if the given block number is past the end of the
5121 * file, back up to the last block if so...
5124 ep = xfs_iext_get_ext(ifp, --lastx);
5125 xfs_bmbt_get_all(ep, &got);
5126 bno = got.br_startoff + got.br_blockcount - 1;
5129 if (ifp->if_flags & XFS_IFBROOT) {
5130 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
5131 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5132 cur->bc_private.b.firstblock = *firstblock;
5133 cur->bc_private.b.flist = flist;
5134 cur->bc_private.b.flags = 0;
5140 * Synchronize by locking the bitmap inode.
5142 xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL);
5143 xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL);
5147 while (bno != (xfs_fileoff_t)-1 && bno >= start && lastx >= 0 &&
5148 (nexts == 0 || extno < nexts)) {
5150 * Is the found extent after a hole in which bno lives?
5151 * Just back up to the previous extent, if so.
5153 if (got.br_startoff > bno) {
5156 ep = xfs_iext_get_ext(ifp, lastx);
5157 xfs_bmbt_get_all(ep, &got);
5160 * Is the last block of this extent before the range
5161 * we're supposed to delete? If so, we're done.
5163 bno = XFS_FILEOFF_MIN(bno,
5164 got.br_startoff + got.br_blockcount - 1);
5168 * Then deal with the (possibly delayed) allocated space
5173 wasdel = isnullstartblock(del.br_startblock);
5174 if (got.br_startoff < start) {
5175 del.br_startoff = start;
5176 del.br_blockcount -= start - got.br_startoff;
5178 del.br_startblock += start - got.br_startoff;
5180 if (del.br_startoff + del.br_blockcount > bno + 1)
5181 del.br_blockcount = bno + 1 - del.br_startoff;
5182 sum = del.br_startblock + del.br_blockcount;
5184 (mod = do_mod(sum, mp->m_sb.sb_rextsize))) {
5186 * Realtime extent not lined up at the end.
5187 * The extent could have been split into written
5188 * and unwritten pieces, or we could just be
5189 * unmapping part of it. But we can't really
5190 * get rid of part of a realtime extent.
5192 if (del.br_state == XFS_EXT_UNWRITTEN ||
5193 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
5195 * This piece is unwritten, or we're not
5196 * using unwritten extents. Skip over it.
5199 bno -= mod > del.br_blockcount ?
5200 del.br_blockcount : mod;
5201 if (bno < got.br_startoff) {
5203 xfs_bmbt_get_all(xfs_iext_get_ext(
5209 * It's written, turn it unwritten.
5210 * This is better than zeroing it.
5212 ASSERT(del.br_state == XFS_EXT_NORM);
5213 ASSERT(xfs_trans_get_block_res(tp) > 0);
5215 * If this spans a realtime extent boundary,
5216 * chop it back to the start of the one we end at.
5218 if (del.br_blockcount > mod) {
5219 del.br_startoff += del.br_blockcount - mod;
5220 del.br_startblock += del.br_blockcount - mod;
5221 del.br_blockcount = mod;
5223 del.br_state = XFS_EXT_UNWRITTEN;
5224 error = xfs_bmap_add_extent_unwritten_real(tp, ip,
5225 &lastx, &cur, &del, firstblock, flist,
5231 if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) {
5233 * Realtime extent is lined up at the end but not
5234 * at the front. We'll get rid of full extents if
5237 mod = mp->m_sb.sb_rextsize - mod;
5238 if (del.br_blockcount > mod) {
5239 del.br_blockcount -= mod;
5240 del.br_startoff += mod;
5241 del.br_startblock += mod;
5242 } else if ((del.br_startoff == start &&
5243 (del.br_state == XFS_EXT_UNWRITTEN ||
5244 xfs_trans_get_block_res(tp) == 0)) ||
5245 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
5247 * Can't make it unwritten. There isn't
5248 * a full extent here so just skip it.
5250 ASSERT(bno >= del.br_blockcount);
5251 bno -= del.br_blockcount;
5252 if (got.br_startoff > bno) {
5254 ep = xfs_iext_get_ext(ifp,
5256 xfs_bmbt_get_all(ep, &got);
5260 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
5262 * This one is already unwritten.
5263 * It must have a written left neighbor.
5264 * Unwrite the killed part of that one and
5268 xfs_bmbt_get_all(xfs_iext_get_ext(ifp,
5270 ASSERT(prev.br_state == XFS_EXT_NORM);
5271 ASSERT(!isnullstartblock(prev.br_startblock));
5272 ASSERT(del.br_startblock ==
5273 prev.br_startblock + prev.br_blockcount);
5274 if (prev.br_startoff < start) {
5275 mod = start - prev.br_startoff;
5276 prev.br_blockcount -= mod;
5277 prev.br_startblock += mod;
5278 prev.br_startoff = start;
5280 prev.br_state = XFS_EXT_UNWRITTEN;
5282 error = xfs_bmap_add_extent_unwritten_real(tp,
5283 ip, &lastx, &cur, &prev,
5284 firstblock, flist, &logflags);
5289 ASSERT(del.br_state == XFS_EXT_NORM);
5290 del.br_state = XFS_EXT_UNWRITTEN;
5291 error = xfs_bmap_add_extent_unwritten_real(tp,
5292 ip, &lastx, &cur, &del,
5293 firstblock, flist, &logflags);
5300 ASSERT(startblockval(del.br_startblock) > 0);
5301 /* Update realtime/data freespace, unreserve quota */
5303 xfs_filblks_t rtexts;
5305 rtexts = XFS_FSB_TO_B(mp, del.br_blockcount);
5306 do_div(rtexts, mp->m_sb.sb_rextsize);
5307 xfs_mod_frextents(mp, (int64_t)rtexts);
5308 (void)xfs_trans_reserve_quota_nblks(NULL,
5309 ip, -((long)del.br_blockcount), 0,
5310 XFS_QMOPT_RES_RTBLKS);
5312 xfs_mod_fdblocks(mp, (int64_t)del.br_blockcount,
5314 (void)xfs_trans_reserve_quota_nblks(NULL,
5315 ip, -((long)del.br_blockcount), 0,
5316 XFS_QMOPT_RES_REGBLKS);
5318 ip->i_delayed_blks -= del.br_blockcount;
5320 cur->bc_private.b.flags |=
5321 XFS_BTCUR_BPRV_WASDEL;
5323 cur->bc_private.b.flags &= ~XFS_BTCUR_BPRV_WASDEL;
5325 * If it's the case where the directory code is running
5326 * with no block reservation, and the deleted block is in
5327 * the middle of its extent, and the resulting insert
5328 * of an extent would cause transformation to btree format,
5329 * then reject it. The calling code will then swap
5330 * blocks around instead.
5331 * We have to do this now, rather than waiting for the
5332 * conversion to btree format, since the transaction
5335 if (!wasdel && xfs_trans_get_block_res(tp) == 0 &&
5336 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
5337 XFS_IFORK_NEXTENTS(ip, whichfork) >= /* Note the >= */
5338 XFS_IFORK_MAXEXT(ip, whichfork) &&
5339 del.br_startoff > got.br_startoff &&
5340 del.br_startoff + del.br_blockcount <
5341 got.br_startoff + got.br_blockcount) {
5345 error = xfs_bmap_del_extent(ip, tp, &lastx, flist, cur, &del,
5346 &tmp_logflags, whichfork);
5347 logflags |= tmp_logflags;
5350 bno = del.br_startoff - 1;
5353 * If not done go on to the next (previous) record.
5355 if (bno != (xfs_fileoff_t)-1 && bno >= start) {
5357 ep = xfs_iext_get_ext(ifp, lastx);
5358 if (xfs_bmbt_get_startoff(ep) > bno) {
5360 ep = xfs_iext_get_ext(ifp,
5363 xfs_bmbt_get_all(ep, &got);
5368 *done = bno == (xfs_fileoff_t)-1 || bno < start || lastx < 0;
5371 * Convert to a btree if necessary.
5373 if (xfs_bmap_needs_btree(ip, whichfork)) {
5374 ASSERT(cur == NULL);
5375 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist,
5376 &cur, 0, &tmp_logflags, whichfork);
5377 logflags |= tmp_logflags;
5382 * transform from btree to extents, give it cur
5384 else if (xfs_bmap_wants_extents(ip, whichfork)) {
5385 ASSERT(cur != NULL);
5386 error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags,
5388 logflags |= tmp_logflags;
5393 * transform from extents to local?
5398 * Log everything. Do this after conversion, there's no point in
5399 * logging the extent records if we've converted to btree format.
5401 if ((logflags & xfs_ilog_fext(whichfork)) &&
5402 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
5403 logflags &= ~xfs_ilog_fext(whichfork);
5404 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
5405 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
5406 logflags &= ~xfs_ilog_fbroot(whichfork);
5408 * Log inode even in the error case, if the transaction
5409 * is dirty we'll need to shut down the filesystem.
5412 xfs_trans_log_inode(tp, ip, logflags);
5415 *firstblock = cur->bc_private.b.firstblock;
5416 cur->bc_private.b.allocated = 0;
5418 xfs_btree_del_cursor(cur,
5419 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5425 * Determine whether an extent shift can be accomplished by a merge with the
5426 * extent that precedes the target hole of the shift.
5430 struct xfs_bmbt_irec *left, /* preceding extent */
5431 struct xfs_bmbt_irec *got, /* current extent to shift */
5432 xfs_fileoff_t shift) /* shift fsb */
5434 xfs_fileoff_t startoff;
5436 startoff = got->br_startoff - shift;
5439 * The extent, once shifted, must be adjacent in-file and on-disk with
5440 * the preceding extent.
5442 if ((left->br_startoff + left->br_blockcount != startoff) ||
5443 (left->br_startblock + left->br_blockcount != got->br_startblock) ||
5444 (left->br_state != got->br_state) ||
5445 (left->br_blockcount + got->br_blockcount > MAXEXTLEN))
5452 * A bmap extent shift adjusts the file offset of an extent to fill a preceding
5453 * hole in the file. If an extent shift would result in the extent being fully
5454 * adjacent to the extent that currently precedes the hole, we can merge with
5455 * the preceding extent rather than do the shift.
5457 * This function assumes the caller has verified a shift-by-merge is possible
5458 * with the provided extents via xfs_bmse_can_merge().
5462 struct xfs_inode *ip,
5464 xfs_fileoff_t shift, /* shift fsb */
5465 int current_ext, /* idx of gotp */
5466 struct xfs_bmbt_rec_host *gotp, /* extent to shift */
5467 struct xfs_bmbt_rec_host *leftp, /* preceding extent */
5468 struct xfs_btree_cur *cur,
5469 int *logflags) /* output */
5471 struct xfs_bmbt_irec got;
5472 struct xfs_bmbt_irec left;
5473 xfs_filblks_t blockcount;
5475 struct xfs_mount *mp = ip->i_mount;
5477 xfs_bmbt_get_all(gotp, &got);
5478 xfs_bmbt_get_all(leftp, &left);
5479 blockcount = left.br_blockcount + got.br_blockcount;
5481 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
5482 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
5483 ASSERT(xfs_bmse_can_merge(&left, &got, shift));
5486 * Merge the in-core extents. Note that the host record pointers and
5487 * current_ext index are invalid once the extent has been removed via
5488 * xfs_iext_remove().
5490 xfs_bmbt_set_blockcount(leftp, blockcount);
5491 xfs_iext_remove(ip, current_ext, 1, 0);
5494 * Update the on-disk extent count, the btree if necessary and log the
5497 XFS_IFORK_NEXT_SET(ip, whichfork,
5498 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
5499 *logflags |= XFS_ILOG_CORE;
5501 *logflags |= XFS_ILOG_DEXT;
5505 /* lookup and remove the extent to merge */
5506 error = xfs_bmbt_lookup_eq(cur, got.br_startoff, got.br_startblock,
5507 got.br_blockcount, &i);
5510 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
5512 error = xfs_btree_delete(cur, &i);
5515 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
5517 /* lookup and update size of the previous extent */
5518 error = xfs_bmbt_lookup_eq(cur, left.br_startoff, left.br_startblock,
5519 left.br_blockcount, &i);
5522 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
5524 left.br_blockcount = blockcount;
5526 return xfs_bmbt_update(cur, left.br_startoff, left.br_startblock,
5527 left.br_blockcount, left.br_state);
5531 * Shift a single extent.
5535 struct xfs_inode *ip,
5537 xfs_fileoff_t offset_shift_fsb,
5539 struct xfs_bmbt_rec_host *gotp,
5540 struct xfs_btree_cur *cur,
5542 enum shift_direction direction)
5544 struct xfs_ifork *ifp;
5545 struct xfs_mount *mp;
5546 xfs_fileoff_t startoff;
5547 struct xfs_bmbt_rec_host *adj_irecp;
5548 struct xfs_bmbt_irec got;
5549 struct xfs_bmbt_irec adj_irec;
5555 ifp = XFS_IFORK_PTR(ip, whichfork);
5556 total_extents = ifp->if_bytes / sizeof(xfs_bmbt_rec_t);
5558 xfs_bmbt_get_all(gotp, &got);
5560 /* delalloc extents should be prevented by caller */
5561 XFS_WANT_CORRUPTED_RETURN(mp, !isnullstartblock(got.br_startblock));
5563 if (direction == SHIFT_LEFT) {
5564 startoff = got.br_startoff - offset_shift_fsb;
5567 * Check for merge if we've got an extent to the left,
5568 * otherwise make sure there's enough room at the start
5569 * of the file for the shift.
5571 if (!*current_ext) {
5572 if (got.br_startoff < offset_shift_fsb)
5574 goto update_current_ext;
5577 * grab the left extent and check for a large
5580 adj_irecp = xfs_iext_get_ext(ifp, *current_ext - 1);
5581 xfs_bmbt_get_all(adj_irecp, &adj_irec);
5584 adj_irec.br_startoff + adj_irec.br_blockcount)
5587 /* check whether to merge the extent or shift it down */
5588 if (xfs_bmse_can_merge(&adj_irec, &got,
5589 offset_shift_fsb)) {
5590 return xfs_bmse_merge(ip, whichfork, offset_shift_fsb,
5591 *current_ext, gotp, adj_irecp,
5595 startoff = got.br_startoff + offset_shift_fsb;
5596 /* nothing to move if this is the last extent */
5597 if (*current_ext >= (total_extents - 1))
5598 goto update_current_ext;
5600 * If this is not the last extent in the file, make sure there
5601 * is enough room between current extent and next extent for
5602 * accommodating the shift.
5604 adj_irecp = xfs_iext_get_ext(ifp, *current_ext + 1);
5605 xfs_bmbt_get_all(adj_irecp, &adj_irec);
5606 if (startoff + got.br_blockcount > adj_irec.br_startoff)
5609 * Unlike a left shift (which involves a hole punch),
5610 * a right shift does not modify extent neighbors
5611 * in any way. We should never find mergeable extents
5612 * in this scenario. Check anyways and warn if we
5613 * encounter two extents that could be one.
5615 if (xfs_bmse_can_merge(&got, &adj_irec, offset_shift_fsb))
5619 * Increment the extent index for the next iteration, update the start
5620 * offset of the in-core extent and update the btree if applicable.
5623 if (direction == SHIFT_LEFT)
5627 xfs_bmbt_set_startoff(gotp, startoff);
5628 *logflags |= XFS_ILOG_CORE;
5630 *logflags |= XFS_ILOG_DEXT;
5634 error = xfs_bmbt_lookup_eq(cur, got.br_startoff, got.br_startblock,
5635 got.br_blockcount, &i);
5638 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
5640 got.br_startoff = startoff;
5641 return xfs_bmbt_update(cur, got.br_startoff, got.br_startblock,
5642 got.br_blockcount, got.br_state);
5646 * Shift extent records to the left/right to cover/create a hole.
5648 * The maximum number of extents to be shifted in a single operation is
5649 * @num_exts. @stop_fsb specifies the file offset at which to stop shift and the
5650 * file offset where we've left off is returned in @next_fsb. @offset_shift_fsb
5651 * is the length by which each extent is shifted. If there is no hole to shift
5652 * the extents into, this will be considered invalid operation and we abort
5656 xfs_bmap_shift_extents(
5657 struct xfs_trans *tp,
5658 struct xfs_inode *ip,
5659 xfs_fileoff_t *next_fsb,
5660 xfs_fileoff_t offset_shift_fsb,
5662 xfs_fileoff_t stop_fsb,
5663 xfs_fsblock_t *firstblock,
5664 struct xfs_bmap_free *flist,
5665 enum shift_direction direction,
5668 struct xfs_btree_cur *cur = NULL;
5669 struct xfs_bmbt_rec_host *gotp;
5670 struct xfs_bmbt_irec got;
5671 struct xfs_mount *mp = ip->i_mount;
5672 struct xfs_ifork *ifp;
5673 xfs_extnum_t nexts = 0;
5674 xfs_extnum_t current_ext;
5675 xfs_extnum_t total_extents;
5676 xfs_extnum_t stop_extent;
5678 int whichfork = XFS_DATA_FORK;
5681 if (unlikely(XFS_TEST_ERROR(
5682 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5683 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
5684 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
5685 XFS_ERROR_REPORT("xfs_bmap_shift_extents",
5686 XFS_ERRLEVEL_LOW, mp);
5687 return -EFSCORRUPTED;
5690 if (XFS_FORCED_SHUTDOWN(mp))
5693 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
5694 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
5695 ASSERT(direction == SHIFT_LEFT || direction == SHIFT_RIGHT);
5696 ASSERT(*next_fsb != NULLFSBLOCK || direction == SHIFT_RIGHT);
5698 ifp = XFS_IFORK_PTR(ip, whichfork);
5699 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
5700 /* Read in all the extents */
5701 error = xfs_iread_extents(tp, ip, whichfork);
5706 if (ifp->if_flags & XFS_IFBROOT) {
5707 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5708 cur->bc_private.b.firstblock = *firstblock;
5709 cur->bc_private.b.flist = flist;
5710 cur->bc_private.b.flags = 0;
5714 * There may be delalloc extents in the data fork before the range we
5715 * are collapsing out, so we cannot use the count of real extents here.
5716 * Instead we have to calculate it from the incore fork.
5718 total_extents = ifp->if_bytes / sizeof(xfs_bmbt_rec_t);
5719 if (total_extents == 0) {
5725 * In case of first right shift, we need to initialize next_fsb
5727 if (*next_fsb == NULLFSBLOCK) {
5728 gotp = xfs_iext_get_ext(ifp, total_extents - 1);
5729 xfs_bmbt_get_all(gotp, &got);
5730 *next_fsb = got.br_startoff;
5731 if (stop_fsb > *next_fsb) {
5737 /* Lookup the extent index at which we have to stop */
5738 if (direction == SHIFT_RIGHT) {
5739 gotp = xfs_iext_bno_to_ext(ifp, stop_fsb, &stop_extent);
5740 /* Make stop_extent exclusive of shift range */
5743 stop_extent = total_extents;
5746 * Look up the extent index for the fsb where we start shifting. We can
5747 * henceforth iterate with current_ext as extent list changes are locked
5750 * gotp can be null in 2 cases: 1) if there are no extents or 2)
5751 * *next_fsb lies in a hole beyond which there are no extents. Either
5754 gotp = xfs_iext_bno_to_ext(ifp, *next_fsb, ¤t_ext);
5760 /* some sanity checking before we finally start shifting extents */
5761 if ((direction == SHIFT_LEFT && current_ext >= stop_extent) ||
5762 (direction == SHIFT_RIGHT && current_ext <= stop_extent)) {
5767 while (nexts++ < num_exts) {
5768 error = xfs_bmse_shift_one(ip, whichfork, offset_shift_fsb,
5769 ¤t_ext, gotp, cur, &logflags,
5774 * If there was an extent merge during the shift, the extent
5775 * count can change. Update the total and grade the next record.
5777 if (direction == SHIFT_LEFT) {
5778 total_extents = ifp->if_bytes / sizeof(xfs_bmbt_rec_t);
5779 stop_extent = total_extents;
5782 if (current_ext == stop_extent) {
5784 *next_fsb = NULLFSBLOCK;
5787 gotp = xfs_iext_get_ext(ifp, current_ext);
5791 xfs_bmbt_get_all(gotp, &got);
5792 *next_fsb = got.br_startoff;
5797 xfs_btree_del_cursor(cur,
5798 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5801 xfs_trans_log_inode(tp, ip, logflags);
5807 * Splits an extent into two extents at split_fsb block such that it is
5808 * the first block of the current_ext. @current_ext is a target extent
5809 * to be split. @split_fsb is a block where the extents is split.
5810 * If split_fsb lies in a hole or the first block of extents, just return 0.
5813 xfs_bmap_split_extent_at(
5814 struct xfs_trans *tp,
5815 struct xfs_inode *ip,
5816 xfs_fileoff_t split_fsb,
5817 xfs_fsblock_t *firstfsb,
5818 struct xfs_bmap_free *free_list)
5820 int whichfork = XFS_DATA_FORK;
5821 struct xfs_btree_cur *cur = NULL;
5822 struct xfs_bmbt_rec_host *gotp;
5823 struct xfs_bmbt_irec got;
5824 struct xfs_bmbt_irec new; /* split extent */
5825 struct xfs_mount *mp = ip->i_mount;
5826 struct xfs_ifork *ifp;
5827 xfs_fsblock_t gotblkcnt; /* new block count for got */
5828 xfs_extnum_t current_ext;
5833 if (unlikely(XFS_TEST_ERROR(
5834 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5835 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
5836 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
5837 XFS_ERROR_REPORT("xfs_bmap_split_extent_at",
5838 XFS_ERRLEVEL_LOW, mp);
5839 return -EFSCORRUPTED;
5842 if (XFS_FORCED_SHUTDOWN(mp))
5845 ifp = XFS_IFORK_PTR(ip, whichfork);
5846 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
5847 /* Read in all the extents */
5848 error = xfs_iread_extents(tp, ip, whichfork);
5854 * gotp can be null in 2 cases: 1) if there are no extents
5855 * or 2) split_fsb lies in a hole beyond which there are
5856 * no extents. Either way, we are done.
5858 gotp = xfs_iext_bno_to_ext(ifp, split_fsb, ¤t_ext);
5862 xfs_bmbt_get_all(gotp, &got);
5865 * Check split_fsb lies in a hole or the start boundary offset
5868 if (got.br_startoff >= split_fsb)
5871 gotblkcnt = split_fsb - got.br_startoff;
5872 new.br_startoff = split_fsb;
5873 new.br_startblock = got.br_startblock + gotblkcnt;
5874 new.br_blockcount = got.br_blockcount - gotblkcnt;
5875 new.br_state = got.br_state;
5877 if (ifp->if_flags & XFS_IFBROOT) {
5878 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5879 cur->bc_private.b.firstblock = *firstfsb;
5880 cur->bc_private.b.flist = free_list;
5881 cur->bc_private.b.flags = 0;
5882 error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
5888 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor);
5891 xfs_bmbt_set_blockcount(gotp, gotblkcnt);
5892 got.br_blockcount = gotblkcnt;
5894 logflags = XFS_ILOG_CORE;
5896 error = xfs_bmbt_update(cur, got.br_startoff,
5903 logflags |= XFS_ILOG_DEXT;
5905 /* Add new extent */
5907 xfs_iext_insert(ip, current_ext, 1, &new, 0);
5908 XFS_IFORK_NEXT_SET(ip, whichfork,
5909 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
5912 error = xfs_bmbt_lookup_eq(cur, new.br_startoff,
5913 new.br_startblock, new.br_blockcount,
5917 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, del_cursor);
5918 cur->bc_rec.b.br_state = new.br_state;
5920 error = xfs_btree_insert(cur, &i);
5923 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor);
5927 * Convert to a btree if necessary.
5929 if (xfs_bmap_needs_btree(ip, whichfork)) {
5930 int tmp_logflags; /* partial log flag return val */
5932 ASSERT(cur == NULL);
5933 error = xfs_bmap_extents_to_btree(tp, ip, firstfsb, free_list,
5934 &cur, 0, &tmp_logflags, whichfork);
5935 logflags |= tmp_logflags;
5940 cur->bc_private.b.allocated = 0;
5941 xfs_btree_del_cursor(cur,
5942 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5946 xfs_trans_log_inode(tp, ip, logflags);
5951 xfs_bmap_split_extent(
5952 struct xfs_inode *ip,
5953 xfs_fileoff_t split_fsb)
5955 struct xfs_mount *mp = ip->i_mount;
5956 struct xfs_trans *tp;
5957 struct xfs_bmap_free free_list;
5958 xfs_fsblock_t firstfsb;
5961 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
5962 error = xfs_trans_reserve(tp, &M_RES(mp)->tr_write,
5963 XFS_DIOSTRAT_SPACE_RES(mp, 0), 0);
5965 xfs_trans_cancel(tp);
5969 xfs_ilock(ip, XFS_ILOCK_EXCL);
5970 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
5972 xfs_bmap_init(&free_list, &firstfsb);
5974 error = xfs_bmap_split_extent_at(tp, ip, split_fsb,
5975 &firstfsb, &free_list);
5979 error = xfs_bmap_finish(&tp, &free_list, NULL);
5983 return xfs_trans_commit(tp);
5986 xfs_bmap_cancel(&free_list);
5987 xfs_trans_cancel(tp);