1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
6 #include "libxfs_priv.h"
8 #include "xfs_shared.h"
9 #include "xfs_format.h"
10 #include "xfs_log_format.h"
11 #include "xfs_trans_resv.h"
14 #include "xfs_mount.h"
15 #include "xfs_defer.h"
17 #include "xfs_inode.h"
18 #include "xfs_btree.h"
19 #include "xfs_trans.h"
20 #include "xfs_alloc.h"
22 #include "xfs_bmap_btree.h"
23 #include "xfs_errortag.h"
24 #include "xfs_trans_space.h"
25 #include "xfs_trace.h"
26 #include "xfs_attr_leaf.h"
27 #include "xfs_quota_defs.h"
30 #include "xfs_ag_resv.h"
31 #include "xfs_refcount.h"
33 struct kmem_cache
*xfs_bmap_intent_cache
;
36 * Miscellaneous helper functions
40 * Compute and fill in the value of the maximum depth of a bmap btree
41 * in this filesystem. Done once, during mount.
44 xfs_bmap_compute_maxlevels(
45 xfs_mount_t
*mp
, /* file system mount structure */
46 int whichfork
) /* data or attr fork */
48 uint64_t maxblocks
; /* max blocks at this level */
49 xfs_extnum_t maxleafents
; /* max leaf entries possible */
50 int level
; /* btree level */
51 int maxrootrecs
; /* max records in root block */
52 int minleafrecs
; /* min records in leaf block */
53 int minnoderecs
; /* min records in node block */
54 int sz
; /* root block size */
57 * The maximum number of extents in a fork, hence the maximum number of
58 * leaf entries, is controlled by the size of the on-disk extent count.
60 * Note that we can no longer assume that if we are in ATTR1 that the
61 * fork offset of all the inodes will be
62 * (xfs_default_attroffset(ip) >> 3) because we could have mounted with
63 * ATTR2 and then mounted back with ATTR1, keeping the i_forkoff's fixed
64 * but probably at various positions. Therefore, for both ATTR1 and
65 * ATTR2 we have to assume the worst case scenario of a minimum size
68 maxleafents
= xfs_iext_max_nextents(xfs_has_large_extent_counts(mp
),
70 if (whichfork
== XFS_DATA_FORK
)
71 sz
= XFS_BMDR_SPACE_CALC(MINDBTPTRS
);
73 sz
= XFS_BMDR_SPACE_CALC(MINABTPTRS
);
75 maxrootrecs
= xfs_bmdr_maxrecs(sz
, 0);
76 minleafrecs
= mp
->m_bmap_dmnr
[0];
77 minnoderecs
= mp
->m_bmap_dmnr
[1];
78 maxblocks
= howmany_64(maxleafents
, minleafrecs
);
79 for (level
= 1; maxblocks
> 1; level
++) {
80 if (maxblocks
<= maxrootrecs
)
83 maxblocks
= howmany_64(maxblocks
, minnoderecs
);
85 mp
->m_bm_maxlevels
[whichfork
] = level
;
86 ASSERT(mp
->m_bm_maxlevels
[whichfork
] <= xfs_bmbt_maxlevels_ondisk());
90 xfs_bmap_compute_attr_offset(
93 if (mp
->m_sb
.sb_inodesize
== 256)
94 return XFS_LITINO(mp
) - XFS_BMDR_SPACE_CALC(MINABTPTRS
);
95 return XFS_BMDR_SPACE_CALC(6 * MINABTPTRS
);
98 STATIC
int /* error */
100 struct xfs_btree_cur
*cur
,
101 struct xfs_bmbt_irec
*irec
,
102 int *stat
) /* success/failure */
104 cur
->bc_rec
.b
= *irec
;
105 return xfs_btree_lookup(cur
, XFS_LOOKUP_EQ
, stat
);
108 STATIC
int /* error */
109 xfs_bmbt_lookup_first(
110 struct xfs_btree_cur
*cur
,
111 int *stat
) /* success/failure */
113 cur
->bc_rec
.b
.br_startoff
= 0;
114 cur
->bc_rec
.b
.br_startblock
= 0;
115 cur
->bc_rec
.b
.br_blockcount
= 0;
116 return xfs_btree_lookup(cur
, XFS_LOOKUP_GE
, stat
);
120 * Check if the inode needs to be converted to btree format.
122 static inline bool xfs_bmap_needs_btree(struct xfs_inode
*ip
, int whichfork
)
124 struct xfs_ifork
*ifp
= xfs_ifork_ptr(ip
, whichfork
);
126 return whichfork
!= XFS_COW_FORK
&&
127 ifp
->if_format
== XFS_DINODE_FMT_EXTENTS
&&
128 ifp
->if_nextents
> XFS_IFORK_MAXEXT(ip
, whichfork
);
132 * Check if the inode should be converted to extent format.
134 static inline bool xfs_bmap_wants_extents(struct xfs_inode
*ip
, int whichfork
)
136 struct xfs_ifork
*ifp
= xfs_ifork_ptr(ip
, whichfork
);
138 return whichfork
!= XFS_COW_FORK
&&
139 ifp
->if_format
== XFS_DINODE_FMT_BTREE
&&
140 ifp
->if_nextents
<= XFS_IFORK_MAXEXT(ip
, whichfork
);
144 * Update the record referred to by cur to the value given by irec
145 * This either works (return 0) or gets an EFSCORRUPTED error.
149 struct xfs_btree_cur
*cur
,
150 struct xfs_bmbt_irec
*irec
)
152 union xfs_btree_rec rec
;
154 xfs_bmbt_disk_set_all(&rec
.bmbt
, irec
);
155 return xfs_btree_update(cur
, &rec
);
159 * Compute the worst-case number of indirect blocks that will be used
160 * for ip's delayed extent of length "len".
163 xfs_bmap_worst_indlen(
164 xfs_inode_t
*ip
, /* incore inode pointer */
165 xfs_filblks_t len
) /* delayed extent length */
167 int level
; /* btree level number */
168 int maxrecs
; /* maximum record count at this level */
169 xfs_mount_t
*mp
; /* mount structure */
170 xfs_filblks_t rval
; /* return value */
173 maxrecs
= mp
->m_bmap_dmxr
[0];
174 for (level
= 0, rval
= 0;
175 level
< XFS_BM_MAXLEVELS(mp
, XFS_DATA_FORK
);
178 do_div(len
, maxrecs
);
181 return rval
+ XFS_BM_MAXLEVELS(mp
, XFS_DATA_FORK
) -
184 maxrecs
= mp
->m_bmap_dmxr
[1];
190 * Calculate the default attribute fork offset for newly created inodes.
193 xfs_default_attroffset(
194 struct xfs_inode
*ip
)
196 if (ip
->i_df
.if_format
== XFS_DINODE_FMT_DEV
)
197 return roundup(sizeof(xfs_dev_t
), 8);
198 return M_IGEO(ip
->i_mount
)->attr_fork_offset
;
202 * Helper routine to reset inode i_forkoff field when switching attribute fork
203 * from local to extent format - we reset it where possible to make space
204 * available for inline data fork extents.
207 xfs_bmap_forkoff_reset(
211 if (whichfork
== XFS_ATTR_FORK
&&
212 ip
->i_df
.if_format
!= XFS_DINODE_FMT_DEV
&&
213 ip
->i_df
.if_format
!= XFS_DINODE_FMT_BTREE
) {
214 uint dfl_forkoff
= xfs_default_attroffset(ip
) >> 3;
216 if (dfl_forkoff
> ip
->i_forkoff
)
217 ip
->i_forkoff
= dfl_forkoff
;
222 STATIC
struct xfs_buf
*
224 struct xfs_btree_cur
*cur
,
227 struct xfs_log_item
*lip
;
233 for (i
= 0; i
< cur
->bc_maxlevels
; i
++) {
234 if (!cur
->bc_levels
[i
].bp
)
236 if (xfs_buf_daddr(cur
->bc_levels
[i
].bp
) == bno
)
237 return cur
->bc_levels
[i
].bp
;
240 /* Chase down all the log items to see if the bp is there */
241 list_for_each_entry(lip
, &cur
->bc_tp
->t_items
, li_trans
) {
242 struct xfs_buf_log_item
*bip
= (struct xfs_buf_log_item
*)lip
;
244 if (bip
->bli_item
.li_type
== XFS_LI_BUF
&&
245 xfs_buf_daddr(bip
->bli_buf
) == bno
)
254 struct xfs_btree_block
*block
,
260 __be64
*pp
, *thispa
; /* pointer to block address */
261 xfs_bmbt_key_t
*prevp
, *keyp
;
263 ASSERT(be16_to_cpu(block
->bb_level
) > 0);
266 for( i
= 1; i
<= xfs_btree_get_numrecs(block
); i
++) {
267 dmxr
= mp
->m_bmap_dmxr
[0];
268 keyp
= XFS_BMBT_KEY_ADDR(mp
, block
, i
);
271 ASSERT(be64_to_cpu(prevp
->br_startoff
) <
272 be64_to_cpu(keyp
->br_startoff
));
277 * Compare the block numbers to see if there are dups.
280 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, i
, sz
);
282 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, i
, dmxr
);
284 for (j
= i
+1; j
<= be16_to_cpu(block
->bb_numrecs
); j
++) {
286 thispa
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, j
, sz
);
288 thispa
= XFS_BMBT_PTR_ADDR(mp
, block
, j
, dmxr
);
289 if (*thispa
== *pp
) {
290 xfs_warn(mp
, "%s: thispa(%d) == pp(%d) %lld",
292 (unsigned long long)be64_to_cpu(*thispa
));
293 xfs_err(mp
, "%s: ptrs are equal in node\n",
295 xfs_force_shutdown(mp
, SHUTDOWN_CORRUPT_INCORE
);
302 * Check that the extents for the inode ip are in the right order in all
303 * btree leaves. THis becomes prohibitively expensive for large extent count
304 * files, so don't bother with inodes that have more than 10,000 extents in
305 * them. The btree record ordering checks will still be done, so for such large
306 * bmapbt constructs that is going to catch most corruptions.
309 xfs_bmap_check_leaf_extents(
310 struct xfs_btree_cur
*cur
, /* btree cursor or null */
311 xfs_inode_t
*ip
, /* incore inode pointer */
312 int whichfork
) /* data or attr fork */
314 struct xfs_mount
*mp
= ip
->i_mount
;
315 struct xfs_ifork
*ifp
= xfs_ifork_ptr(ip
, whichfork
);
316 struct xfs_btree_block
*block
; /* current btree block */
317 xfs_fsblock_t bno
; /* block # of "block" */
318 struct xfs_buf
*bp
; /* buffer for "block" */
319 int error
; /* error return value */
320 xfs_extnum_t i
=0, j
; /* index into the extents list */
321 int level
; /* btree level, for checking */
322 __be64
*pp
; /* pointer to block address */
323 xfs_bmbt_rec_t
*ep
; /* pointer to current extent */
324 xfs_bmbt_rec_t last
= {0, 0}; /* last extent in prev block */
325 xfs_bmbt_rec_t
*nextp
; /* pointer to next extent */
328 if (ifp
->if_format
!= XFS_DINODE_FMT_BTREE
)
331 /* skip large extent count inodes */
332 if (ip
->i_df
.if_nextents
> 10000)
336 block
= ifp
->if_broot
;
338 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
340 level
= be16_to_cpu(block
->bb_level
);
342 xfs_check_block(block
, mp
, 1, ifp
->if_broot_bytes
);
343 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, 1, ifp
->if_broot_bytes
);
344 bno
= be64_to_cpu(*pp
);
346 ASSERT(bno
!= NULLFSBLOCK
);
347 ASSERT(XFS_FSB_TO_AGNO(mp
, bno
) < mp
->m_sb
.sb_agcount
);
348 ASSERT(XFS_FSB_TO_AGBNO(mp
, bno
) < mp
->m_sb
.sb_agblocks
);
351 * Go down the tree until leaf level is reached, following the first
352 * pointer (leftmost) at each level.
354 while (level
-- > 0) {
355 /* See if buf is in cur first */
357 bp
= xfs_bmap_get_bp(cur
, XFS_FSB_TO_DADDR(mp
, bno
));
360 error
= xfs_btree_read_bufl(mp
, NULL
, bno
, &bp
,
366 block
= XFS_BUF_TO_BLOCK(bp
);
371 * Check this block for basic sanity (increasing keys and
372 * no duplicate blocks).
375 xfs_check_block(block
, mp
, 0, 0);
376 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, mp
->m_bmap_dmxr
[1]);
377 bno
= be64_to_cpu(*pp
);
378 if (XFS_IS_CORRUPT(mp
, !xfs_verify_fsbno(mp
, bno
))) {
379 error
= -EFSCORRUPTED
;
384 xfs_trans_brelse(NULL
, bp
);
389 * Here with bp and block set to the leftmost leaf node in the tree.
394 * Loop over all leaf nodes checking that all extents are in the right order.
397 xfs_fsblock_t nextbno
;
398 xfs_extnum_t num_recs
;
401 num_recs
= xfs_btree_get_numrecs(block
);
404 * Read-ahead the next leaf block, if any.
407 nextbno
= be64_to_cpu(block
->bb_u
.l
.bb_rightsib
);
410 * Check all the extents to make sure they are OK.
411 * If we had a previous block, the last entry should
412 * conform with the first entry in this one.
415 ep
= XFS_BMBT_REC_ADDR(mp
, block
, 1);
417 ASSERT(xfs_bmbt_disk_get_startoff(&last
) +
418 xfs_bmbt_disk_get_blockcount(&last
) <=
419 xfs_bmbt_disk_get_startoff(ep
));
421 for (j
= 1; j
< num_recs
; j
++) {
422 nextp
= XFS_BMBT_REC_ADDR(mp
, block
, j
+ 1);
423 ASSERT(xfs_bmbt_disk_get_startoff(ep
) +
424 xfs_bmbt_disk_get_blockcount(ep
) <=
425 xfs_bmbt_disk_get_startoff(nextp
));
433 xfs_trans_brelse(NULL
, bp
);
437 * If we've reached the end, stop.
439 if (bno
== NULLFSBLOCK
)
443 bp
= xfs_bmap_get_bp(cur
, XFS_FSB_TO_DADDR(mp
, bno
));
446 error
= xfs_btree_read_bufl(mp
, NULL
, bno
, &bp
,
452 block
= XFS_BUF_TO_BLOCK(bp
);
458 xfs_warn(mp
, "%s: at error0", __func__
);
460 xfs_trans_brelse(NULL
, bp
);
462 xfs_warn(mp
, "%s: BAD after btree leaves for %llu extents",
464 xfs_err(mp
, "%s: CORRUPTED BTREE OR SOMETHING", __func__
);
465 xfs_force_shutdown(mp
, SHUTDOWN_CORRUPT_INCORE
);
470 * Validate that the bmbt_irecs being returned from bmapi are valid
471 * given the caller's original parameters. Specifically check the
472 * ranges of the returned irecs to ensure that they only extend beyond
473 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
476 xfs_bmap_validate_ret(
480 xfs_bmbt_irec_t
*mval
,
484 int i
; /* index to map values */
486 ASSERT(ret_nmap
<= nmap
);
488 for (i
= 0; i
< ret_nmap
; i
++) {
489 ASSERT(mval
[i
].br_blockcount
> 0);
490 if (!(flags
& XFS_BMAPI_ENTIRE
)) {
491 ASSERT(mval
[i
].br_startoff
>= bno
);
492 ASSERT(mval
[i
].br_blockcount
<= len
);
493 ASSERT(mval
[i
].br_startoff
+ mval
[i
].br_blockcount
<=
496 ASSERT(mval
[i
].br_startoff
< bno
+ len
);
497 ASSERT(mval
[i
].br_startoff
+ mval
[i
].br_blockcount
>
501 mval
[i
- 1].br_startoff
+ mval
[i
- 1].br_blockcount
==
502 mval
[i
].br_startoff
);
503 ASSERT(mval
[i
].br_startblock
!= DELAYSTARTBLOCK
&&
504 mval
[i
].br_startblock
!= HOLESTARTBLOCK
);
505 ASSERT(mval
[i
].br_state
== XFS_EXT_NORM
||
506 mval
[i
].br_state
== XFS_EXT_UNWRITTEN
);
511 #define xfs_bmap_check_leaf_extents(cur, ip, whichfork) do { } while (0)
512 #define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap) do { } while (0)
516 * Inode fork format manipulation functions
520 * Convert the inode format to extent format if it currently is in btree format,
521 * but the extent list is small enough that it fits into the extent format.
523 * Since the extents are already in-core, all we have to do is give up the space
524 * for the btree root and pitch the leaf block.
526 STATIC
int /* error */
527 xfs_bmap_btree_to_extents(
528 struct xfs_trans
*tp
, /* transaction pointer */
529 struct xfs_inode
*ip
, /* incore inode pointer */
530 struct xfs_btree_cur
*cur
, /* btree cursor */
531 int *logflagsp
, /* inode logging flags */
532 int whichfork
) /* data or attr fork */
534 struct xfs_ifork
*ifp
= xfs_ifork_ptr(ip
, whichfork
);
535 struct xfs_mount
*mp
= ip
->i_mount
;
536 struct xfs_btree_block
*rblock
= ifp
->if_broot
;
537 struct xfs_btree_block
*cblock
;/* child btree block */
538 xfs_fsblock_t cbno
; /* child block number */
539 struct xfs_buf
*cbp
; /* child block's buffer */
540 int error
; /* error return value */
541 __be64
*pp
; /* ptr to block address */
542 struct xfs_owner_info oinfo
;
544 /* check if we actually need the extent format first: */
545 if (!xfs_bmap_wants_extents(ip
, whichfork
))
549 ASSERT(whichfork
!= XFS_COW_FORK
);
550 ASSERT(ifp
->if_format
== XFS_DINODE_FMT_BTREE
);
551 ASSERT(be16_to_cpu(rblock
->bb_level
) == 1);
552 ASSERT(be16_to_cpu(rblock
->bb_numrecs
) == 1);
553 ASSERT(xfs_bmbt_maxrecs(mp
, ifp
->if_broot_bytes
, 0) == 1);
555 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, rblock
, 1, ifp
->if_broot_bytes
);
556 cbno
= be64_to_cpu(*pp
);
558 if (XFS_IS_CORRUPT(cur
->bc_mp
, !xfs_btree_check_lptr(cur
, cbno
, 1)))
559 return -EFSCORRUPTED
;
561 error
= xfs_btree_read_bufl(mp
, tp
, cbno
, &cbp
, XFS_BMAP_BTREE_REF
,
565 cblock
= XFS_BUF_TO_BLOCK(cbp
);
566 if ((error
= xfs_btree_check_block(cur
, cblock
, 0, cbp
)))
568 xfs_rmap_ino_bmbt_owner(&oinfo
, ip
->i_ino
, whichfork
);
569 xfs_free_extent_later(cur
->bc_tp
, cbno
, 1, &oinfo
);
571 xfs_trans_mod_dquot_byino(tp
, ip
, XFS_TRANS_DQ_BCOUNT
, -1L);
572 xfs_trans_binval(tp
, cbp
);
573 if (cur
->bc_levels
[0].bp
== cbp
)
574 cur
->bc_levels
[0].bp
= NULL
;
575 xfs_iroot_realloc(ip
, -1, whichfork
);
576 ASSERT(ifp
->if_broot
== NULL
);
577 ifp
->if_format
= XFS_DINODE_FMT_EXTENTS
;
578 *logflagsp
|= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
583 * Convert an extents-format file into a btree-format file.
584 * The new file will have a root block (in the inode) and a single child block.
586 STATIC
int /* error */
587 xfs_bmap_extents_to_btree(
588 struct xfs_trans
*tp
, /* transaction pointer */
589 struct xfs_inode
*ip
, /* incore inode pointer */
590 struct xfs_btree_cur
**curp
, /* cursor returned to caller */
591 int wasdel
, /* converting a delayed alloc */
592 int *logflagsp
, /* inode logging flags */
593 int whichfork
) /* data or attr fork */
595 struct xfs_btree_block
*ablock
; /* allocated (child) bt block */
596 struct xfs_buf
*abp
; /* buffer for ablock */
597 struct xfs_alloc_arg args
; /* allocation arguments */
598 struct xfs_bmbt_rec
*arp
; /* child record pointer */
599 struct xfs_btree_block
*block
; /* btree root block */
600 struct xfs_btree_cur
*cur
; /* bmap btree cursor */
601 int error
; /* error return value */
602 struct xfs_ifork
*ifp
; /* inode fork pointer */
603 struct xfs_bmbt_key
*kp
; /* root block key pointer */
604 struct xfs_mount
*mp
; /* mount structure */
605 xfs_bmbt_ptr_t
*pp
; /* root block address pointer */
606 struct xfs_iext_cursor icur
;
607 struct xfs_bmbt_irec rec
;
608 xfs_extnum_t cnt
= 0;
611 ASSERT(whichfork
!= XFS_COW_FORK
);
612 ifp
= xfs_ifork_ptr(ip
, whichfork
);
613 ASSERT(ifp
->if_format
== XFS_DINODE_FMT_EXTENTS
);
616 * Make space in the inode incore. This needs to be undone if we fail
617 * to expand the root.
619 xfs_iroot_realloc(ip
, 1, whichfork
);
624 block
= ifp
->if_broot
;
625 xfs_btree_init_block_int(mp
, block
, XFS_BUF_DADDR_NULL
,
626 XFS_BTNUM_BMAP
, 1, 1, ip
->i_ino
,
627 XFS_BTREE_LONG_PTRS
);
629 * Need a cursor. Can't allocate until bb_level is filled in.
631 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
632 cur
->bc_ino
.flags
= wasdel
? XFS_BTCUR_BMBT_WASDEL
: 0;
634 * Convert to a btree with two levels, one record in root.
636 ifp
->if_format
= XFS_DINODE_FMT_BTREE
;
637 memset(&args
, 0, sizeof(args
));
640 xfs_rmap_ino_bmbt_owner(&args
.oinfo
, ip
->i_ino
, whichfork
);
641 if (tp
->t_firstblock
== NULLFSBLOCK
) {
642 args
.type
= XFS_ALLOCTYPE_START_BNO
;
643 args
.fsbno
= XFS_INO_TO_FSB(mp
, ip
->i_ino
);
644 } else if (tp
->t_flags
& XFS_TRANS_LOWMODE
) {
645 args
.type
= XFS_ALLOCTYPE_START_BNO
;
646 args
.fsbno
= tp
->t_firstblock
;
648 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
649 args
.fsbno
= tp
->t_firstblock
;
651 args
.minlen
= args
.maxlen
= args
.prod
= 1;
652 args
.wasdel
= wasdel
;
654 error
= xfs_alloc_vextent(&args
);
656 goto out_root_realloc
;
658 if (WARN_ON_ONCE(args
.fsbno
== NULLFSBLOCK
)) {
660 goto out_root_realloc
;
664 * Allocation can't fail, the space was reserved.
666 ASSERT(tp
->t_firstblock
== NULLFSBLOCK
||
667 args
.agno
>= XFS_FSB_TO_AGNO(mp
, tp
->t_firstblock
));
668 tp
->t_firstblock
= args
.fsbno
;
669 cur
->bc_ino
.allocated
++;
671 xfs_trans_mod_dquot_byino(tp
, ip
, XFS_TRANS_DQ_BCOUNT
, 1L);
672 error
= xfs_trans_get_buf(tp
, mp
->m_ddev_targp
,
673 XFS_FSB_TO_DADDR(mp
, args
.fsbno
),
674 mp
->m_bsize
, 0, &abp
);
676 goto out_unreserve_dquot
;
679 * Fill in the child block.
681 abp
->b_ops
= &xfs_bmbt_buf_ops
;
682 ablock
= XFS_BUF_TO_BLOCK(abp
);
683 xfs_btree_init_block_int(mp
, ablock
, xfs_buf_daddr(abp
),
684 XFS_BTNUM_BMAP
, 0, 0, ip
->i_ino
,
685 XFS_BTREE_LONG_PTRS
);
687 for_each_xfs_iext(ifp
, &icur
, &rec
) {
688 if (isnullstartblock(rec
.br_startblock
))
690 arp
= XFS_BMBT_REC_ADDR(mp
, ablock
, 1 + cnt
);
691 xfs_bmbt_disk_set_all(arp
, &rec
);
694 ASSERT(cnt
== ifp
->if_nextents
);
695 xfs_btree_set_numrecs(ablock
, cnt
);
698 * Fill in the root key and pointer.
700 kp
= XFS_BMBT_KEY_ADDR(mp
, block
, 1);
701 arp
= XFS_BMBT_REC_ADDR(mp
, ablock
, 1);
702 kp
->br_startoff
= cpu_to_be64(xfs_bmbt_disk_get_startoff(arp
));
703 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, xfs_bmbt_get_maxrecs(cur
,
704 be16_to_cpu(block
->bb_level
)));
705 *pp
= cpu_to_be64(args
.fsbno
);
708 * Do all this logging at the end so that
709 * the root is at the right level.
711 xfs_btree_log_block(cur
, abp
, XFS_BB_ALL_BITS
);
712 xfs_btree_log_recs(cur
, abp
, 1, be16_to_cpu(ablock
->bb_numrecs
));
713 ASSERT(*curp
== NULL
);
715 *logflagsp
= XFS_ILOG_CORE
| xfs_ilog_fbroot(whichfork
);
719 xfs_trans_mod_dquot_byino(tp
, ip
, XFS_TRANS_DQ_BCOUNT
, -1L);
721 xfs_iroot_realloc(ip
, -1, whichfork
);
722 ifp
->if_format
= XFS_DINODE_FMT_EXTENTS
;
723 ASSERT(ifp
->if_broot
== NULL
);
724 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
730 * Convert a local file to an extents file.
731 * This code is out of bounds for data forks of regular files,
732 * since the file data needs to get logged so things will stay consistent.
733 * (The bmap-level manipulations are ok, though).
736 xfs_bmap_local_to_extents_empty(
737 struct xfs_trans
*tp
,
738 struct xfs_inode
*ip
,
741 struct xfs_ifork
*ifp
= xfs_ifork_ptr(ip
, whichfork
);
743 ASSERT(whichfork
!= XFS_COW_FORK
);
744 ASSERT(ifp
->if_format
== XFS_DINODE_FMT_LOCAL
);
745 ASSERT(ifp
->if_bytes
== 0);
746 ASSERT(ifp
->if_nextents
== 0);
748 xfs_bmap_forkoff_reset(ip
, whichfork
);
749 ifp
->if_u1
.if_root
= NULL
;
751 ifp
->if_format
= XFS_DINODE_FMT_EXTENTS
;
752 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
756 STATIC
int /* error */
757 xfs_bmap_local_to_extents(
758 xfs_trans_t
*tp
, /* transaction pointer */
759 xfs_inode_t
*ip
, /* incore inode pointer */
760 xfs_extlen_t total
, /* total blocks needed by transaction */
761 int *logflagsp
, /* inode logging flags */
763 void (*init_fn
)(struct xfs_trans
*tp
,
765 struct xfs_inode
*ip
,
766 struct xfs_ifork
*ifp
))
769 int flags
; /* logging flags returned */
770 struct xfs_ifork
*ifp
; /* inode fork pointer */
771 xfs_alloc_arg_t args
; /* allocation arguments */
772 struct xfs_buf
*bp
; /* buffer for extent block */
773 struct xfs_bmbt_irec rec
;
774 struct xfs_iext_cursor icur
;
777 * We don't want to deal with the case of keeping inode data inline yet.
778 * So sending the data fork of a regular inode is invalid.
780 ASSERT(!(S_ISREG(VFS_I(ip
)->i_mode
) && whichfork
== XFS_DATA_FORK
));
781 ifp
= xfs_ifork_ptr(ip
, whichfork
);
782 ASSERT(ifp
->if_format
== XFS_DINODE_FMT_LOCAL
);
784 if (!ifp
->if_bytes
) {
785 xfs_bmap_local_to_extents_empty(tp
, ip
, whichfork
);
786 flags
= XFS_ILOG_CORE
;
792 memset(&args
, 0, sizeof(args
));
794 args
.mp
= ip
->i_mount
;
795 xfs_rmap_ino_owner(&args
.oinfo
, ip
->i_ino
, whichfork
, 0);
797 * Allocate a block. We know we need only one, since the
798 * file currently fits in an inode.
800 if (tp
->t_firstblock
== NULLFSBLOCK
) {
801 args
.fsbno
= XFS_INO_TO_FSB(args
.mp
, ip
->i_ino
);
802 args
.type
= XFS_ALLOCTYPE_START_BNO
;
804 args
.fsbno
= tp
->t_firstblock
;
805 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
808 args
.minlen
= args
.maxlen
= args
.prod
= 1;
809 error
= xfs_alloc_vextent(&args
);
813 /* Can't fail, the space was reserved. */
814 ASSERT(args
.fsbno
!= NULLFSBLOCK
);
815 ASSERT(args
.len
== 1);
816 tp
->t_firstblock
= args
.fsbno
;
817 error
= xfs_trans_get_buf(tp
, args
.mp
->m_ddev_targp
,
818 XFS_FSB_TO_DADDR(args
.mp
, args
.fsbno
),
819 args
.mp
->m_bsize
, 0, &bp
);
824 * Initialize the block, copy the data and log the remote buffer.
826 * The callout is responsible for logging because the remote format
827 * might differ from the local format and thus we don't know how much to
828 * log here. Note that init_fn must also set the buffer log item type
831 init_fn(tp
, bp
, ip
, ifp
);
833 /* account for the change in fork size */
834 xfs_idata_realloc(ip
, -ifp
->if_bytes
, whichfork
);
835 xfs_bmap_local_to_extents_empty(tp
, ip
, whichfork
);
836 flags
|= XFS_ILOG_CORE
;
838 ifp
->if_u1
.if_root
= NULL
;
842 rec
.br_startblock
= args
.fsbno
;
843 rec
.br_blockcount
= 1;
844 rec
.br_state
= XFS_EXT_NORM
;
845 xfs_iext_first(ifp
, &icur
);
846 xfs_iext_insert(ip
, &icur
, &rec
, 0);
848 ifp
->if_nextents
= 1;
850 xfs_trans_mod_dquot_byino(tp
, ip
,
851 XFS_TRANS_DQ_BCOUNT
, 1L);
852 flags
|= xfs_ilog_fext(whichfork
);
860 * Called from xfs_bmap_add_attrfork to handle btree format files.
862 STATIC
int /* error */
863 xfs_bmap_add_attrfork_btree(
864 xfs_trans_t
*tp
, /* transaction pointer */
865 xfs_inode_t
*ip
, /* incore inode pointer */
866 int *flags
) /* inode logging flags */
868 struct xfs_btree_block
*block
= ip
->i_df
.if_broot
;
869 struct xfs_btree_cur
*cur
; /* btree cursor */
870 int error
; /* error return value */
871 xfs_mount_t
*mp
; /* file system mount struct */
872 int stat
; /* newroot status */
876 if (XFS_BMAP_BMDR_SPACE(block
) <= xfs_inode_data_fork_size(ip
))
877 *flags
|= XFS_ILOG_DBROOT
;
879 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, XFS_DATA_FORK
);
880 error
= xfs_bmbt_lookup_first(cur
, &stat
);
883 /* must be at least one entry */
884 if (XFS_IS_CORRUPT(mp
, stat
!= 1)) {
885 error
= -EFSCORRUPTED
;
888 if ((error
= xfs_btree_new_iroot(cur
, flags
, &stat
)))
891 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
894 cur
->bc_ino
.allocated
= 0;
895 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
899 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
904 * Called from xfs_bmap_add_attrfork to handle extents format files.
906 STATIC
int /* error */
907 xfs_bmap_add_attrfork_extents(
908 struct xfs_trans
*tp
, /* transaction pointer */
909 struct xfs_inode
*ip
, /* incore inode pointer */
910 int *flags
) /* inode logging flags */
912 struct xfs_btree_cur
*cur
; /* bmap btree cursor */
913 int error
; /* error return value */
915 if (ip
->i_df
.if_nextents
* sizeof(struct xfs_bmbt_rec
) <=
916 xfs_inode_data_fork_size(ip
))
919 error
= xfs_bmap_extents_to_btree(tp
, ip
, &cur
, 0, flags
,
922 cur
->bc_ino
.allocated
= 0;
923 xfs_btree_del_cursor(cur
, error
);
929 * Called from xfs_bmap_add_attrfork to handle local format files. Each
930 * different data fork content type needs a different callout to do the
931 * conversion. Some are basic and only require special block initialisation
932 * callouts for the data formating, others (directories) are so specialised they
933 * handle everything themselves.
935 * XXX (dgc): investigate whether directory conversion can use the generic
936 * formatting callout. It should be possible - it's just a very complex
939 STATIC
int /* error */
940 xfs_bmap_add_attrfork_local(
941 struct xfs_trans
*tp
, /* transaction pointer */
942 struct xfs_inode
*ip
, /* incore inode pointer */
943 int *flags
) /* inode logging flags */
945 struct xfs_da_args dargs
; /* args for dir/attr code */
947 if (ip
->i_df
.if_bytes
<= xfs_inode_data_fork_size(ip
))
950 if (S_ISDIR(VFS_I(ip
)->i_mode
)) {
951 memset(&dargs
, 0, sizeof(dargs
));
952 dargs
.geo
= ip
->i_mount
->m_dir_geo
;
954 dargs
.total
= dargs
.geo
->fsbcount
;
955 dargs
.whichfork
= XFS_DATA_FORK
;
957 return xfs_dir2_sf_to_block(&dargs
);
960 if (S_ISLNK(VFS_I(ip
)->i_mode
))
961 return xfs_bmap_local_to_extents(tp
, ip
, 1, flags
,
963 xfs_symlink_local_to_remote
);
965 /* should only be called for types that support local format data */
967 return -EFSCORRUPTED
;
971 * Set an inode attr fork offset based on the format of the data fork.
974 xfs_bmap_set_attrforkoff(
975 struct xfs_inode
*ip
,
979 int default_size
= xfs_default_attroffset(ip
) >> 3;
981 switch (ip
->i_df
.if_format
) {
982 case XFS_DINODE_FMT_DEV
:
983 ip
->i_forkoff
= default_size
;
985 case XFS_DINODE_FMT_LOCAL
:
986 case XFS_DINODE_FMT_EXTENTS
:
987 case XFS_DINODE_FMT_BTREE
:
988 ip
->i_forkoff
= xfs_attr_shortform_bytesfit(ip
, size
);
990 ip
->i_forkoff
= default_size
;
991 else if (xfs_has_attr2(ip
->i_mount
) && version
)
1003 * Convert inode from non-attributed to attributed.
1004 * Must not be in a transaction, ip must not be locked.
1006 int /* error code */
1007 xfs_bmap_add_attrfork(
1008 xfs_inode_t
*ip
, /* incore inode pointer */
1009 int size
, /* space new attribute needs */
1010 int rsvd
) /* xact may use reserved blks */
1012 xfs_mount_t
*mp
; /* mount structure */
1013 xfs_trans_t
*tp
; /* transaction pointer */
1014 int blks
; /* space reservation */
1015 int version
= 1; /* superblock attr version */
1016 int logflags
; /* logging flags */
1017 int error
; /* error return value */
1019 ASSERT(xfs_inode_has_attr_fork(ip
) == 0);
1022 ASSERT(!XFS_NOT_DQATTACHED(mp
, ip
));
1024 blks
= XFS_ADDAFORK_SPACE_RES(mp
);
1026 error
= xfs_trans_alloc_inode(ip
, &M_RES(mp
)->tr_addafork
, blks
, 0,
1030 if (xfs_inode_has_attr_fork(ip
))
1033 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
1034 error
= xfs_bmap_set_attrforkoff(ip
, size
, &version
);
1038 xfs_ifork_init_attr(ip
, XFS_DINODE_FMT_EXTENTS
, 0);
1040 switch (ip
->i_df
.if_format
) {
1041 case XFS_DINODE_FMT_LOCAL
:
1042 error
= xfs_bmap_add_attrfork_local(tp
, ip
, &logflags
);
1044 case XFS_DINODE_FMT_EXTENTS
:
1045 error
= xfs_bmap_add_attrfork_extents(tp
, ip
, &logflags
);
1047 case XFS_DINODE_FMT_BTREE
:
1048 error
= xfs_bmap_add_attrfork_btree(tp
, ip
, &logflags
);
1055 xfs_trans_log_inode(tp
, ip
, logflags
);
1058 if (!xfs_has_attr(mp
) ||
1059 (!xfs_has_attr2(mp
) && version
== 2)) {
1060 bool log_sb
= false;
1062 spin_lock(&mp
->m_sb_lock
);
1063 if (!xfs_has_attr(mp
)) {
1067 if (!xfs_has_attr2(mp
) && version
== 2) {
1071 spin_unlock(&mp
->m_sb_lock
);
1076 error
= xfs_trans_commit(tp
);
1077 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
1081 xfs_trans_cancel(tp
);
1082 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
1087 * Internal and external extent tree search functions.
1090 struct xfs_iread_state
{
1091 struct xfs_iext_cursor icur
;
1092 xfs_extnum_t loaded
;
1095 /* Stuff every bmbt record from this block into the incore extent map. */
1097 xfs_iread_bmbt_block(
1098 struct xfs_btree_cur
*cur
,
1102 struct xfs_iread_state
*ir
= priv
;
1103 struct xfs_mount
*mp
= cur
->bc_mp
;
1104 struct xfs_inode
*ip
= cur
->bc_ino
.ip
;
1105 struct xfs_btree_block
*block
;
1107 struct xfs_bmbt_rec
*frp
;
1108 xfs_extnum_t num_recs
;
1110 int whichfork
= cur
->bc_ino
.whichfork
;
1111 struct xfs_ifork
*ifp
= xfs_ifork_ptr(ip
, whichfork
);
1113 block
= xfs_btree_get_block(cur
, level
, &bp
);
1115 /* Abort if we find more records than nextents. */
1116 num_recs
= xfs_btree_get_numrecs(block
);
1117 if (unlikely(ir
->loaded
+ num_recs
> ifp
->if_nextents
)) {
1118 xfs_warn(ip
->i_mount
, "corrupt dinode %llu, (btree extents).",
1119 (unsigned long long)ip
->i_ino
);
1120 xfs_inode_verifier_error(ip
, -EFSCORRUPTED
, __func__
, block
,
1121 sizeof(*block
), __this_address
);
1122 return -EFSCORRUPTED
;
1125 /* Copy records into the incore cache. */
1126 frp
= XFS_BMBT_REC_ADDR(mp
, block
, 1);
1127 for (j
= 0; j
< num_recs
; j
++, frp
++, ir
->loaded
++) {
1128 struct xfs_bmbt_irec
new;
1131 xfs_bmbt_disk_get_all(frp
, &new);
1132 fa
= xfs_bmap_validate_extent(ip
, whichfork
, &new);
1134 xfs_inode_verifier_error(ip
, -EFSCORRUPTED
,
1135 "xfs_iread_extents(2)", frp
,
1137 return -EFSCORRUPTED
;
1139 xfs_iext_insert(ip
, &ir
->icur
, &new,
1140 xfs_bmap_fork_to_state(whichfork
));
1141 trace_xfs_read_extent(ip
, &ir
->icur
,
1142 xfs_bmap_fork_to_state(whichfork
), _THIS_IP_
);
1143 xfs_iext_next(ifp
, &ir
->icur
);
1150 * Read in extents from a btree-format inode.
1154 struct xfs_trans
*tp
,
1155 struct xfs_inode
*ip
,
1158 struct xfs_iread_state ir
;
1159 struct xfs_ifork
*ifp
= xfs_ifork_ptr(ip
, whichfork
);
1160 struct xfs_mount
*mp
= ip
->i_mount
;
1161 struct xfs_btree_cur
*cur
;
1164 if (!xfs_need_iread_extents(ifp
))
1167 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
1170 xfs_iext_first(ifp
, &ir
.icur
);
1171 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
1172 error
= xfs_btree_visit_blocks(cur
, xfs_iread_bmbt_block
,
1173 XFS_BTREE_VISIT_RECORDS
, &ir
);
1174 xfs_btree_del_cursor(cur
, error
);
1178 if (XFS_IS_CORRUPT(mp
, ir
.loaded
!= ifp
->if_nextents
)) {
1179 error
= -EFSCORRUPTED
;
1182 ASSERT(ir
.loaded
== xfs_iext_count(ifp
));
1185 xfs_iext_destroy(ifp
);
1190 * Returns the relative block number of the first unused block(s) in the given
1191 * fork with at least "len" logically contiguous blocks free. This is the
1192 * lowest-address hole if the fork has holes, else the first block past the end
1193 * of fork. Return 0 if the fork is currently local (in-inode).
1196 xfs_bmap_first_unused(
1197 struct xfs_trans
*tp
, /* transaction pointer */
1198 struct xfs_inode
*ip
, /* incore inode */
1199 xfs_extlen_t len
, /* size of hole to find */
1200 xfs_fileoff_t
*first_unused
, /* unused block */
1201 int whichfork
) /* data or attr fork */
1203 struct xfs_ifork
*ifp
= xfs_ifork_ptr(ip
, whichfork
);
1204 struct xfs_bmbt_irec got
;
1205 struct xfs_iext_cursor icur
;
1206 xfs_fileoff_t lastaddr
= 0;
1207 xfs_fileoff_t lowest
, max
;
1210 if (ifp
->if_format
== XFS_DINODE_FMT_LOCAL
) {
1215 ASSERT(xfs_ifork_has_extents(ifp
));
1217 error
= xfs_iread_extents(tp
, ip
, whichfork
);
1221 lowest
= max
= *first_unused
;
1222 for_each_xfs_iext(ifp
, &icur
, &got
) {
1224 * See if the hole before this extent will work.
1226 if (got
.br_startoff
>= lowest
+ len
&&
1227 got
.br_startoff
- max
>= len
)
1229 lastaddr
= got
.br_startoff
+ got
.br_blockcount
;
1230 max
= XFS_FILEOFF_MAX(lastaddr
, lowest
);
1233 *first_unused
= max
;
1238 * Returns the file-relative block number of the last block - 1 before
1239 * last_block (input value) in the file.
1240 * This is not based on i_size, it is based on the extent records.
1241 * Returns 0 for local files, as they do not have extent records.
1244 xfs_bmap_last_before(
1245 struct xfs_trans
*tp
, /* transaction pointer */
1246 struct xfs_inode
*ip
, /* incore inode */
1247 xfs_fileoff_t
*last_block
, /* last block */
1248 int whichfork
) /* data or attr fork */
1250 struct xfs_ifork
*ifp
= xfs_ifork_ptr(ip
, whichfork
);
1251 struct xfs_bmbt_irec got
;
1252 struct xfs_iext_cursor icur
;
1255 switch (ifp
->if_format
) {
1256 case XFS_DINODE_FMT_LOCAL
:
1259 case XFS_DINODE_FMT_BTREE
:
1260 case XFS_DINODE_FMT_EXTENTS
:
1264 return -EFSCORRUPTED
;
1267 error
= xfs_iread_extents(tp
, ip
, whichfork
);
1271 if (!xfs_iext_lookup_extent_before(ip
, ifp
, last_block
, &icur
, &got
))
1277 xfs_bmap_last_extent(
1278 struct xfs_trans
*tp
,
1279 struct xfs_inode
*ip
,
1281 struct xfs_bmbt_irec
*rec
,
1284 struct xfs_ifork
*ifp
= xfs_ifork_ptr(ip
, whichfork
);
1285 struct xfs_iext_cursor icur
;
1288 error
= xfs_iread_extents(tp
, ip
, whichfork
);
1292 xfs_iext_last(ifp
, &icur
);
1293 if (!xfs_iext_get_extent(ifp
, &icur
, rec
))
1301 * Check the last inode extent to determine whether this allocation will result
1302 * in blocks being allocated at the end of the file. When we allocate new data
1303 * blocks at the end of the file which do not start at the previous data block,
1304 * we will try to align the new blocks at stripe unit boundaries.
1306 * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be
1307 * at, or past the EOF.
1311 struct xfs_bmalloca
*bma
,
1314 struct xfs_bmbt_irec rec
;
1319 error
= xfs_bmap_last_extent(NULL
, bma
->ip
, whichfork
, &rec
,
1330 * Check if we are allocation or past the last extent, or at least into
1331 * the last delayed allocated extent.
1333 bma
->aeof
= bma
->offset
>= rec
.br_startoff
+ rec
.br_blockcount
||
1334 (bma
->offset
>= rec
.br_startoff
&&
1335 isnullstartblock(rec
.br_startblock
));
1340 * Returns the file-relative block number of the first block past eof in
1341 * the file. This is not based on i_size, it is based on the extent records.
1342 * Returns 0 for local files, as they do not have extent records.
1345 xfs_bmap_last_offset(
1346 struct xfs_inode
*ip
,
1347 xfs_fileoff_t
*last_block
,
1350 struct xfs_ifork
*ifp
= xfs_ifork_ptr(ip
, whichfork
);
1351 struct xfs_bmbt_irec rec
;
1357 if (ifp
->if_format
== XFS_DINODE_FMT_LOCAL
)
1360 if (XFS_IS_CORRUPT(ip
->i_mount
, !xfs_ifork_has_extents(ifp
)))
1361 return -EFSCORRUPTED
;
1363 error
= xfs_bmap_last_extent(NULL
, ip
, whichfork
, &rec
, &is_empty
);
1364 if (error
|| is_empty
)
1367 *last_block
= rec
.br_startoff
+ rec
.br_blockcount
;
1372 * Extent tree manipulation functions used during allocation.
1376 * Convert a delayed allocation to a real allocation.
1378 STATIC
int /* error */
1379 xfs_bmap_add_extent_delay_real(
1380 struct xfs_bmalloca
*bma
,
1383 struct xfs_mount
*mp
= bma
->ip
->i_mount
;
1384 struct xfs_ifork
*ifp
= xfs_ifork_ptr(bma
->ip
, whichfork
);
1385 struct xfs_bmbt_irec
*new = &bma
->got
;
1386 int error
; /* error return value */
1387 int i
; /* temp state */
1388 xfs_fileoff_t new_endoff
; /* end offset of new entry */
1389 xfs_bmbt_irec_t r
[3]; /* neighbor extent entries */
1390 /* left is 0, right is 1, prev is 2 */
1391 int rval
=0; /* return value (logging flags) */
1392 uint32_t state
= xfs_bmap_fork_to_state(whichfork
);
1393 xfs_filblks_t da_new
; /* new count del alloc blocks used */
1394 xfs_filblks_t da_old
; /* old count del alloc blocks used */
1395 xfs_filblks_t temp
=0; /* value for da_new calculations */
1396 int tmp_rval
; /* partial logging flags */
1397 struct xfs_bmbt_irec old
;
1399 ASSERT(whichfork
!= XFS_ATTR_FORK
);
1400 ASSERT(!isnullstartblock(new->br_startblock
));
1402 (bma
->cur
->bc_ino
.flags
& XFS_BTCUR_BMBT_WASDEL
));
1404 XFS_STATS_INC(mp
, xs_add_exlist
);
1411 * Set up a bunch of variables to make the tests simpler.
1413 xfs_iext_get_extent(ifp
, &bma
->icur
, &PREV
);
1414 new_endoff
= new->br_startoff
+ new->br_blockcount
;
1415 ASSERT(isnullstartblock(PREV
.br_startblock
));
1416 ASSERT(PREV
.br_startoff
<= new->br_startoff
);
1417 ASSERT(PREV
.br_startoff
+ PREV
.br_blockcount
>= new_endoff
);
1419 da_old
= startblockval(PREV
.br_startblock
);
1423 * Set flags determining what part of the previous delayed allocation
1424 * extent is being replaced by a real allocation.
1426 if (PREV
.br_startoff
== new->br_startoff
)
1427 state
|= BMAP_LEFT_FILLING
;
1428 if (PREV
.br_startoff
+ PREV
.br_blockcount
== new_endoff
)
1429 state
|= BMAP_RIGHT_FILLING
;
1432 * Check and set flags if this segment has a left neighbor.
1433 * Don't set contiguous if the combined extent would be too large.
1435 if (xfs_iext_peek_prev_extent(ifp
, &bma
->icur
, &LEFT
)) {
1436 state
|= BMAP_LEFT_VALID
;
1437 if (isnullstartblock(LEFT
.br_startblock
))
1438 state
|= BMAP_LEFT_DELAY
;
1441 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
1442 LEFT
.br_startoff
+ LEFT
.br_blockcount
== new->br_startoff
&&
1443 LEFT
.br_startblock
+ LEFT
.br_blockcount
== new->br_startblock
&&
1444 LEFT
.br_state
== new->br_state
&&
1445 LEFT
.br_blockcount
+ new->br_blockcount
<= XFS_MAX_BMBT_EXTLEN
)
1446 state
|= BMAP_LEFT_CONTIG
;
1449 * Check and set flags if this segment has a right neighbor.
1450 * Don't set contiguous if the combined extent would be too large.
1451 * Also check for all-three-contiguous being too large.
1453 if (xfs_iext_peek_next_extent(ifp
, &bma
->icur
, &RIGHT
)) {
1454 state
|= BMAP_RIGHT_VALID
;
1455 if (isnullstartblock(RIGHT
.br_startblock
))
1456 state
|= BMAP_RIGHT_DELAY
;
1459 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
1460 new_endoff
== RIGHT
.br_startoff
&&
1461 new->br_startblock
+ new->br_blockcount
== RIGHT
.br_startblock
&&
1462 new->br_state
== RIGHT
.br_state
&&
1463 new->br_blockcount
+ RIGHT
.br_blockcount
<= XFS_MAX_BMBT_EXTLEN
&&
1464 ((state
& (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
1465 BMAP_RIGHT_FILLING
)) !=
1466 (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
1467 BMAP_RIGHT_FILLING
) ||
1468 LEFT
.br_blockcount
+ new->br_blockcount
+ RIGHT
.br_blockcount
1469 <= XFS_MAX_BMBT_EXTLEN
))
1470 state
|= BMAP_RIGHT_CONTIG
;
1474 * Switch out based on the FILLING and CONTIG state bits.
1476 switch (state
& (BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
1477 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
)) {
1478 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
1479 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
1481 * Filling in all of a previously delayed allocation extent.
1482 * The left and right neighbors are both contiguous with new.
1484 LEFT
.br_blockcount
+= PREV
.br_blockcount
+ RIGHT
.br_blockcount
;
1486 xfs_iext_remove(bma
->ip
, &bma
->icur
, state
);
1487 xfs_iext_remove(bma
->ip
, &bma
->icur
, state
);
1488 xfs_iext_prev(ifp
, &bma
->icur
);
1489 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &LEFT
);
1492 if (bma
->cur
== NULL
)
1493 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1495 rval
= XFS_ILOG_CORE
;
1496 error
= xfs_bmbt_lookup_eq(bma
->cur
, &RIGHT
, &i
);
1499 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
1500 error
= -EFSCORRUPTED
;
1503 error
= xfs_btree_delete(bma
->cur
, &i
);
1506 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
1507 error
= -EFSCORRUPTED
;
1510 error
= xfs_btree_decrement(bma
->cur
, 0, &i
);
1513 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
1514 error
= -EFSCORRUPTED
;
1517 error
= xfs_bmbt_update(bma
->cur
, &LEFT
);
1523 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
1525 * Filling in all of a previously delayed allocation extent.
1526 * The left neighbor is contiguous, the right is not.
1529 LEFT
.br_blockcount
+= PREV
.br_blockcount
;
1531 xfs_iext_remove(bma
->ip
, &bma
->icur
, state
);
1532 xfs_iext_prev(ifp
, &bma
->icur
);
1533 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &LEFT
);
1535 if (bma
->cur
== NULL
)
1536 rval
= XFS_ILOG_DEXT
;
1539 error
= xfs_bmbt_lookup_eq(bma
->cur
, &old
, &i
);
1542 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
1543 error
= -EFSCORRUPTED
;
1546 error
= xfs_bmbt_update(bma
->cur
, &LEFT
);
1552 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
1554 * Filling in all of a previously delayed allocation extent.
1555 * The right neighbor is contiguous, the left is not. Take care
1556 * with delay -> unwritten extent allocation here because the
1557 * delalloc record we are overwriting is always written.
1559 PREV
.br_startblock
= new->br_startblock
;
1560 PREV
.br_blockcount
+= RIGHT
.br_blockcount
;
1561 PREV
.br_state
= new->br_state
;
1563 xfs_iext_next(ifp
, &bma
->icur
);
1564 xfs_iext_remove(bma
->ip
, &bma
->icur
, state
);
1565 xfs_iext_prev(ifp
, &bma
->icur
);
1566 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &PREV
);
1568 if (bma
->cur
== NULL
)
1569 rval
= XFS_ILOG_DEXT
;
1572 error
= xfs_bmbt_lookup_eq(bma
->cur
, &RIGHT
, &i
);
1575 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
1576 error
= -EFSCORRUPTED
;
1579 error
= xfs_bmbt_update(bma
->cur
, &PREV
);
1585 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
1587 * Filling in all of a previously delayed allocation extent.
1588 * Neither the left nor right neighbors are contiguous with
1591 PREV
.br_startblock
= new->br_startblock
;
1592 PREV
.br_state
= new->br_state
;
1593 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &PREV
);
1596 if (bma
->cur
== NULL
)
1597 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1599 rval
= XFS_ILOG_CORE
;
1600 error
= xfs_bmbt_lookup_eq(bma
->cur
, new, &i
);
1603 if (XFS_IS_CORRUPT(mp
, i
!= 0)) {
1604 error
= -EFSCORRUPTED
;
1607 error
= xfs_btree_insert(bma
->cur
, &i
);
1610 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
1611 error
= -EFSCORRUPTED
;
1617 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
:
1619 * Filling in the first part of a previous delayed allocation.
1620 * The left neighbor is contiguous.
1623 temp
= PREV
.br_blockcount
- new->br_blockcount
;
1624 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
1625 startblockval(PREV
.br_startblock
));
1627 LEFT
.br_blockcount
+= new->br_blockcount
;
1629 PREV
.br_blockcount
= temp
;
1630 PREV
.br_startoff
+= new->br_blockcount
;
1631 PREV
.br_startblock
= nullstartblock(da_new
);
1633 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &PREV
);
1634 xfs_iext_prev(ifp
, &bma
->icur
);
1635 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &LEFT
);
1637 if (bma
->cur
== NULL
)
1638 rval
= XFS_ILOG_DEXT
;
1641 error
= xfs_bmbt_lookup_eq(bma
->cur
, &old
, &i
);
1644 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
1645 error
= -EFSCORRUPTED
;
1648 error
= xfs_bmbt_update(bma
->cur
, &LEFT
);
1654 case BMAP_LEFT_FILLING
:
1656 * Filling in the first part of a previous delayed allocation.
1657 * The left neighbor is not contiguous.
1659 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, new);
1662 if (bma
->cur
== NULL
)
1663 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1665 rval
= XFS_ILOG_CORE
;
1666 error
= xfs_bmbt_lookup_eq(bma
->cur
, new, &i
);
1669 if (XFS_IS_CORRUPT(mp
, i
!= 0)) {
1670 error
= -EFSCORRUPTED
;
1673 error
= xfs_btree_insert(bma
->cur
, &i
);
1676 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
1677 error
= -EFSCORRUPTED
;
1682 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
1683 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
1684 &bma
->cur
, 1, &tmp_rval
, whichfork
);
1690 temp
= PREV
.br_blockcount
- new->br_blockcount
;
1691 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
1692 startblockval(PREV
.br_startblock
) -
1693 (bma
->cur
? bma
->cur
->bc_ino
.allocated
: 0));
1695 PREV
.br_startoff
= new_endoff
;
1696 PREV
.br_blockcount
= temp
;
1697 PREV
.br_startblock
= nullstartblock(da_new
);
1698 xfs_iext_next(ifp
, &bma
->icur
);
1699 xfs_iext_insert(bma
->ip
, &bma
->icur
, &PREV
, state
);
1700 xfs_iext_prev(ifp
, &bma
->icur
);
1703 case BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
1705 * Filling in the last part of a previous delayed allocation.
1706 * The right neighbor is contiguous with the new allocation.
1709 RIGHT
.br_startoff
= new->br_startoff
;
1710 RIGHT
.br_startblock
= new->br_startblock
;
1711 RIGHT
.br_blockcount
+= new->br_blockcount
;
1713 if (bma
->cur
== NULL
)
1714 rval
= XFS_ILOG_DEXT
;
1717 error
= xfs_bmbt_lookup_eq(bma
->cur
, &old
, &i
);
1720 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
1721 error
= -EFSCORRUPTED
;
1724 error
= xfs_bmbt_update(bma
->cur
, &RIGHT
);
1729 temp
= PREV
.br_blockcount
- new->br_blockcount
;
1730 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
1731 startblockval(PREV
.br_startblock
));
1733 PREV
.br_blockcount
= temp
;
1734 PREV
.br_startblock
= nullstartblock(da_new
);
1736 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &PREV
);
1737 xfs_iext_next(ifp
, &bma
->icur
);
1738 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &RIGHT
);
1741 case BMAP_RIGHT_FILLING
:
1743 * Filling in the last part of a previous delayed allocation.
1744 * The right neighbor is not contiguous.
1746 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, new);
1749 if (bma
->cur
== NULL
)
1750 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1752 rval
= XFS_ILOG_CORE
;
1753 error
= xfs_bmbt_lookup_eq(bma
->cur
, new, &i
);
1756 if (XFS_IS_CORRUPT(mp
, i
!= 0)) {
1757 error
= -EFSCORRUPTED
;
1760 error
= xfs_btree_insert(bma
->cur
, &i
);
1763 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
1764 error
= -EFSCORRUPTED
;
1769 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
1770 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
1771 &bma
->cur
, 1, &tmp_rval
, whichfork
);
1777 temp
= PREV
.br_blockcount
- new->br_blockcount
;
1778 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
1779 startblockval(PREV
.br_startblock
) -
1780 (bma
->cur
? bma
->cur
->bc_ino
.allocated
: 0));
1782 PREV
.br_startblock
= nullstartblock(da_new
);
1783 PREV
.br_blockcount
= temp
;
1784 xfs_iext_insert(bma
->ip
, &bma
->icur
, &PREV
, state
);
1785 xfs_iext_next(ifp
, &bma
->icur
);
1790 * Filling in the middle part of a previous delayed allocation.
1791 * Contiguity is impossible here.
1792 * This case is avoided almost all the time.
1794 * We start with a delayed allocation:
1796 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
1799 * and we are allocating:
1800 * +rrrrrrrrrrrrrrrrr+
1803 * and we set it up for insertion as:
1804 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
1806 * PREV @ idx LEFT RIGHT
1807 * inserted at idx + 1
1811 /* LEFT is the new middle */
1814 /* RIGHT is the new right */
1815 RIGHT
.br_state
= PREV
.br_state
;
1816 RIGHT
.br_startoff
= new_endoff
;
1817 RIGHT
.br_blockcount
=
1818 PREV
.br_startoff
+ PREV
.br_blockcount
- new_endoff
;
1819 RIGHT
.br_startblock
=
1820 nullstartblock(xfs_bmap_worst_indlen(bma
->ip
,
1821 RIGHT
.br_blockcount
));
1824 PREV
.br_blockcount
= new->br_startoff
- PREV
.br_startoff
;
1825 PREV
.br_startblock
=
1826 nullstartblock(xfs_bmap_worst_indlen(bma
->ip
,
1827 PREV
.br_blockcount
));
1828 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &PREV
);
1830 xfs_iext_next(ifp
, &bma
->icur
);
1831 xfs_iext_insert(bma
->ip
, &bma
->icur
, &RIGHT
, state
);
1832 xfs_iext_insert(bma
->ip
, &bma
->icur
, &LEFT
, state
);
1835 if (bma
->cur
== NULL
)
1836 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1838 rval
= XFS_ILOG_CORE
;
1839 error
= xfs_bmbt_lookup_eq(bma
->cur
, new, &i
);
1842 if (XFS_IS_CORRUPT(mp
, i
!= 0)) {
1843 error
= -EFSCORRUPTED
;
1846 error
= xfs_btree_insert(bma
->cur
, &i
);
1849 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
1850 error
= -EFSCORRUPTED
;
1855 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
1856 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
1857 &bma
->cur
, 1, &tmp_rval
, whichfork
);
1863 da_new
= startblockval(PREV
.br_startblock
) +
1864 startblockval(RIGHT
.br_startblock
);
1867 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
1868 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
1869 case BMAP_LEFT_FILLING
| BMAP_RIGHT_CONTIG
:
1870 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
1871 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
1872 case BMAP_LEFT_CONTIG
:
1873 case BMAP_RIGHT_CONTIG
:
1875 * These cases are all impossible.
1880 /* add reverse mapping unless caller opted out */
1881 if (!(bma
->flags
& XFS_BMAPI_NORMAP
))
1882 xfs_rmap_map_extent(bma
->tp
, bma
->ip
, whichfork
, new);
1884 /* convert to a btree if necessary */
1885 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
1886 int tmp_logflags
; /* partial log flag return val */
1888 ASSERT(bma
->cur
== NULL
);
1889 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
1890 &bma
->cur
, da_old
> 0, &tmp_logflags
,
1892 bma
->logflags
|= tmp_logflags
;
1897 if (da_new
!= da_old
)
1898 xfs_mod_delalloc(mp
, (int64_t)da_new
- da_old
);
1901 da_new
+= bma
->cur
->bc_ino
.allocated
;
1902 bma
->cur
->bc_ino
.allocated
= 0;
1905 /* adjust for changes in reserved delayed indirect blocks */
1906 if (da_new
!= da_old
) {
1907 ASSERT(state
== 0 || da_new
< da_old
);
1908 error
= xfs_mod_fdblocks(mp
, (int64_t)(da_old
- da_new
),
1912 xfs_bmap_check_leaf_extents(bma
->cur
, bma
->ip
, whichfork
);
1914 if (whichfork
!= XFS_COW_FORK
)
1915 bma
->logflags
|= rval
;
1923 * Convert an unwritten allocation to a real allocation or vice versa.
1926 xfs_bmap_add_extent_unwritten_real(
1927 struct xfs_trans
*tp
,
1928 xfs_inode_t
*ip
, /* incore inode pointer */
1930 struct xfs_iext_cursor
*icur
,
1931 struct xfs_btree_cur
**curp
, /* if *curp is null, not a btree */
1932 xfs_bmbt_irec_t
*new, /* new data to add to file extents */
1933 int *logflagsp
) /* inode logging flags */
1935 struct xfs_btree_cur
*cur
; /* btree cursor */
1936 int error
; /* error return value */
1937 int i
; /* temp state */
1938 struct xfs_ifork
*ifp
; /* inode fork pointer */
1939 xfs_fileoff_t new_endoff
; /* end offset of new entry */
1940 xfs_bmbt_irec_t r
[3]; /* neighbor extent entries */
1941 /* left is 0, right is 1, prev is 2 */
1942 int rval
=0; /* return value (logging flags) */
1943 uint32_t state
= xfs_bmap_fork_to_state(whichfork
);
1944 struct xfs_mount
*mp
= ip
->i_mount
;
1945 struct xfs_bmbt_irec old
;
1950 ifp
= xfs_ifork_ptr(ip
, whichfork
);
1952 ASSERT(!isnullstartblock(new->br_startblock
));
1954 XFS_STATS_INC(mp
, xs_add_exlist
);
1961 * Set up a bunch of variables to make the tests simpler.
1964 xfs_iext_get_extent(ifp
, icur
, &PREV
);
1965 ASSERT(new->br_state
!= PREV
.br_state
);
1966 new_endoff
= new->br_startoff
+ new->br_blockcount
;
1967 ASSERT(PREV
.br_startoff
<= new->br_startoff
);
1968 ASSERT(PREV
.br_startoff
+ PREV
.br_blockcount
>= new_endoff
);
1971 * Set flags determining what part of the previous oldext allocation
1972 * extent is being replaced by a newext allocation.
1974 if (PREV
.br_startoff
== new->br_startoff
)
1975 state
|= BMAP_LEFT_FILLING
;
1976 if (PREV
.br_startoff
+ PREV
.br_blockcount
== new_endoff
)
1977 state
|= BMAP_RIGHT_FILLING
;
1980 * Check and set flags if this segment has a left neighbor.
1981 * Don't set contiguous if the combined extent would be too large.
1983 if (xfs_iext_peek_prev_extent(ifp
, icur
, &LEFT
)) {
1984 state
|= BMAP_LEFT_VALID
;
1985 if (isnullstartblock(LEFT
.br_startblock
))
1986 state
|= BMAP_LEFT_DELAY
;
1989 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
1990 LEFT
.br_startoff
+ LEFT
.br_blockcount
== new->br_startoff
&&
1991 LEFT
.br_startblock
+ LEFT
.br_blockcount
== new->br_startblock
&&
1992 LEFT
.br_state
== new->br_state
&&
1993 LEFT
.br_blockcount
+ new->br_blockcount
<= XFS_MAX_BMBT_EXTLEN
)
1994 state
|= BMAP_LEFT_CONTIG
;
1997 * Check and set flags if this segment has a right neighbor.
1998 * Don't set contiguous if the combined extent would be too large.
1999 * Also check for all-three-contiguous being too large.
2001 if (xfs_iext_peek_next_extent(ifp
, icur
, &RIGHT
)) {
2002 state
|= BMAP_RIGHT_VALID
;
2003 if (isnullstartblock(RIGHT
.br_startblock
))
2004 state
|= BMAP_RIGHT_DELAY
;
2007 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
2008 new_endoff
== RIGHT
.br_startoff
&&
2009 new->br_startblock
+ new->br_blockcount
== RIGHT
.br_startblock
&&
2010 new->br_state
== RIGHT
.br_state
&&
2011 new->br_blockcount
+ RIGHT
.br_blockcount
<= XFS_MAX_BMBT_EXTLEN
&&
2012 ((state
& (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
2013 BMAP_RIGHT_FILLING
)) !=
2014 (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
2015 BMAP_RIGHT_FILLING
) ||
2016 LEFT
.br_blockcount
+ new->br_blockcount
+ RIGHT
.br_blockcount
2017 <= XFS_MAX_BMBT_EXTLEN
))
2018 state
|= BMAP_RIGHT_CONTIG
;
2021 * Switch out based on the FILLING and CONTIG state bits.
2023 switch (state
& (BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
2024 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
)) {
2025 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
2026 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2028 * Setting all of a previous oldext extent to newext.
2029 * The left and right neighbors are both contiguous with new.
2031 LEFT
.br_blockcount
+= PREV
.br_blockcount
+ RIGHT
.br_blockcount
;
2033 xfs_iext_remove(ip
, icur
, state
);
2034 xfs_iext_remove(ip
, icur
, state
);
2035 xfs_iext_prev(ifp
, icur
);
2036 xfs_iext_update_extent(ip
, state
, icur
, &LEFT
);
2037 ifp
->if_nextents
-= 2;
2039 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2041 rval
= XFS_ILOG_CORE
;
2042 error
= xfs_bmbt_lookup_eq(cur
, &RIGHT
, &i
);
2045 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
2046 error
= -EFSCORRUPTED
;
2049 if ((error
= xfs_btree_delete(cur
, &i
)))
2051 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
2052 error
= -EFSCORRUPTED
;
2055 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2057 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
2058 error
= -EFSCORRUPTED
;
2061 if ((error
= xfs_btree_delete(cur
, &i
)))
2063 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
2064 error
= -EFSCORRUPTED
;
2067 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2069 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
2070 error
= -EFSCORRUPTED
;
2073 error
= xfs_bmbt_update(cur
, &LEFT
);
2079 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
2081 * Setting all of a previous oldext extent to newext.
2082 * The left neighbor is contiguous, the right is not.
2084 LEFT
.br_blockcount
+= PREV
.br_blockcount
;
2086 xfs_iext_remove(ip
, icur
, state
);
2087 xfs_iext_prev(ifp
, icur
);
2088 xfs_iext_update_extent(ip
, state
, icur
, &LEFT
);
2091 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2093 rval
= XFS_ILOG_CORE
;
2094 error
= xfs_bmbt_lookup_eq(cur
, &PREV
, &i
);
2097 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
2098 error
= -EFSCORRUPTED
;
2101 if ((error
= xfs_btree_delete(cur
, &i
)))
2103 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
2104 error
= -EFSCORRUPTED
;
2107 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2109 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
2110 error
= -EFSCORRUPTED
;
2113 error
= xfs_bmbt_update(cur
, &LEFT
);
2119 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2121 * Setting all of a previous oldext extent to newext.
2122 * The right neighbor is contiguous, the left is not.
2124 PREV
.br_blockcount
+= RIGHT
.br_blockcount
;
2125 PREV
.br_state
= new->br_state
;
2127 xfs_iext_next(ifp
, icur
);
2128 xfs_iext_remove(ip
, icur
, state
);
2129 xfs_iext_prev(ifp
, icur
);
2130 xfs_iext_update_extent(ip
, state
, icur
, &PREV
);
2134 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2136 rval
= XFS_ILOG_CORE
;
2137 error
= xfs_bmbt_lookup_eq(cur
, &RIGHT
, &i
);
2140 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
2141 error
= -EFSCORRUPTED
;
2144 if ((error
= xfs_btree_delete(cur
, &i
)))
2146 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
2147 error
= -EFSCORRUPTED
;
2150 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2152 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
2153 error
= -EFSCORRUPTED
;
2156 error
= xfs_bmbt_update(cur
, &PREV
);
2162 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
2164 * Setting all of a previous oldext extent to newext.
2165 * Neither the left nor right neighbors are contiguous with
2168 PREV
.br_state
= new->br_state
;
2169 xfs_iext_update_extent(ip
, state
, icur
, &PREV
);
2172 rval
= XFS_ILOG_DEXT
;
2175 error
= xfs_bmbt_lookup_eq(cur
, new, &i
);
2178 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
2179 error
= -EFSCORRUPTED
;
2182 error
= xfs_bmbt_update(cur
, &PREV
);
2188 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
:
2190 * Setting the first part of a previous oldext extent to newext.
2191 * The left neighbor is contiguous.
2193 LEFT
.br_blockcount
+= new->br_blockcount
;
2196 PREV
.br_startoff
+= new->br_blockcount
;
2197 PREV
.br_startblock
+= new->br_blockcount
;
2198 PREV
.br_blockcount
-= new->br_blockcount
;
2200 xfs_iext_update_extent(ip
, state
, icur
, &PREV
);
2201 xfs_iext_prev(ifp
, icur
);
2202 xfs_iext_update_extent(ip
, state
, icur
, &LEFT
);
2205 rval
= XFS_ILOG_DEXT
;
2208 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2211 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
2212 error
= -EFSCORRUPTED
;
2215 error
= xfs_bmbt_update(cur
, &PREV
);
2218 error
= xfs_btree_decrement(cur
, 0, &i
);
2221 error
= xfs_bmbt_update(cur
, &LEFT
);
2227 case BMAP_LEFT_FILLING
:
2229 * Setting the first part of a previous oldext extent to newext.
2230 * The left neighbor is not contiguous.
2233 PREV
.br_startoff
+= new->br_blockcount
;
2234 PREV
.br_startblock
+= new->br_blockcount
;
2235 PREV
.br_blockcount
-= new->br_blockcount
;
2237 xfs_iext_update_extent(ip
, state
, icur
, &PREV
);
2238 xfs_iext_insert(ip
, icur
, new, state
);
2242 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2244 rval
= XFS_ILOG_CORE
;
2245 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2248 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
2249 error
= -EFSCORRUPTED
;
2252 error
= xfs_bmbt_update(cur
, &PREV
);
2255 cur
->bc_rec
.b
= *new;
2256 if ((error
= xfs_btree_insert(cur
, &i
)))
2258 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
2259 error
= -EFSCORRUPTED
;
2265 case BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2267 * Setting the last part of a previous oldext extent to newext.
2268 * The right neighbor is contiguous with the new allocation.
2271 PREV
.br_blockcount
-= new->br_blockcount
;
2273 RIGHT
.br_startoff
= new->br_startoff
;
2274 RIGHT
.br_startblock
= new->br_startblock
;
2275 RIGHT
.br_blockcount
+= new->br_blockcount
;
2277 xfs_iext_update_extent(ip
, state
, icur
, &PREV
);
2278 xfs_iext_next(ifp
, icur
);
2279 xfs_iext_update_extent(ip
, state
, icur
, &RIGHT
);
2282 rval
= XFS_ILOG_DEXT
;
2285 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2288 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
2289 error
= -EFSCORRUPTED
;
2292 error
= xfs_bmbt_update(cur
, &PREV
);
2295 error
= xfs_btree_increment(cur
, 0, &i
);
2298 error
= xfs_bmbt_update(cur
, &RIGHT
);
2304 case BMAP_RIGHT_FILLING
:
2306 * Setting the last part of a previous oldext extent to newext.
2307 * The right neighbor is not contiguous.
2310 PREV
.br_blockcount
-= new->br_blockcount
;
2312 xfs_iext_update_extent(ip
, state
, icur
, &PREV
);
2313 xfs_iext_next(ifp
, icur
);
2314 xfs_iext_insert(ip
, icur
, new, state
);
2318 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2320 rval
= XFS_ILOG_CORE
;
2321 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2324 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
2325 error
= -EFSCORRUPTED
;
2328 error
= xfs_bmbt_update(cur
, &PREV
);
2331 error
= xfs_bmbt_lookup_eq(cur
, new, &i
);
2334 if (XFS_IS_CORRUPT(mp
, i
!= 0)) {
2335 error
= -EFSCORRUPTED
;
2338 if ((error
= xfs_btree_insert(cur
, &i
)))
2340 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
2341 error
= -EFSCORRUPTED
;
2349 * Setting the middle part of a previous oldext extent to
2350 * newext. Contiguity is impossible here.
2351 * One extent becomes three extents.
2354 PREV
.br_blockcount
= new->br_startoff
- PREV
.br_startoff
;
2357 r
[1].br_startoff
= new_endoff
;
2358 r
[1].br_blockcount
=
2359 old
.br_startoff
+ old
.br_blockcount
- new_endoff
;
2360 r
[1].br_startblock
= new->br_startblock
+ new->br_blockcount
;
2361 r
[1].br_state
= PREV
.br_state
;
2363 xfs_iext_update_extent(ip
, state
, icur
, &PREV
);
2364 xfs_iext_next(ifp
, icur
);
2365 xfs_iext_insert(ip
, icur
, &r
[1], state
);
2366 xfs_iext_insert(ip
, icur
, &r
[0], state
);
2367 ifp
->if_nextents
+= 2;
2370 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2372 rval
= XFS_ILOG_CORE
;
2373 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2376 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
2377 error
= -EFSCORRUPTED
;
2380 /* new right extent - oldext */
2381 error
= xfs_bmbt_update(cur
, &r
[1]);
2384 /* new left extent - oldext */
2385 cur
->bc_rec
.b
= PREV
;
2386 if ((error
= xfs_btree_insert(cur
, &i
)))
2388 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
2389 error
= -EFSCORRUPTED
;
2393 * Reset the cursor to the position of the new extent
2394 * we are about to insert as we can't trust it after
2395 * the previous insert.
2397 error
= xfs_bmbt_lookup_eq(cur
, new, &i
);
2400 if (XFS_IS_CORRUPT(mp
, i
!= 0)) {
2401 error
= -EFSCORRUPTED
;
2404 /* new middle extent - newext */
2405 if ((error
= xfs_btree_insert(cur
, &i
)))
2407 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
2408 error
= -EFSCORRUPTED
;
2414 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2415 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2416 case BMAP_LEFT_FILLING
| BMAP_RIGHT_CONTIG
:
2417 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
2418 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2419 case BMAP_LEFT_CONTIG
:
2420 case BMAP_RIGHT_CONTIG
:
2422 * These cases are all impossible.
2427 /* update reverse mappings */
2428 xfs_rmap_convert_extent(mp
, tp
, ip
, whichfork
, new);
2430 /* convert to a btree if necessary */
2431 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
2432 int tmp_logflags
; /* partial log flag return val */
2434 ASSERT(cur
== NULL
);
2435 error
= xfs_bmap_extents_to_btree(tp
, ip
, &cur
, 0,
2436 &tmp_logflags
, whichfork
);
2437 *logflagsp
|= tmp_logflags
;
2442 /* clear out the allocated field, done with it now in any case. */
2444 cur
->bc_ino
.allocated
= 0;
2448 xfs_bmap_check_leaf_extents(*curp
, ip
, whichfork
);
2458 * Convert a hole to a delayed allocation.
2461 xfs_bmap_add_extent_hole_delay(
2462 xfs_inode_t
*ip
, /* incore inode pointer */
2464 struct xfs_iext_cursor
*icur
,
2465 xfs_bmbt_irec_t
*new) /* new data to add to file extents */
2467 struct xfs_ifork
*ifp
; /* inode fork pointer */
2468 xfs_bmbt_irec_t left
; /* left neighbor extent entry */
2469 xfs_filblks_t newlen
=0; /* new indirect size */
2470 xfs_filblks_t oldlen
=0; /* old indirect size */
2471 xfs_bmbt_irec_t right
; /* right neighbor extent entry */
2472 uint32_t state
= xfs_bmap_fork_to_state(whichfork
);
2473 xfs_filblks_t temp
; /* temp for indirect calculations */
2475 ifp
= xfs_ifork_ptr(ip
, whichfork
);
2476 ASSERT(isnullstartblock(new->br_startblock
));
2479 * Check and set flags if this segment has a left neighbor
2481 if (xfs_iext_peek_prev_extent(ifp
, icur
, &left
)) {
2482 state
|= BMAP_LEFT_VALID
;
2483 if (isnullstartblock(left
.br_startblock
))
2484 state
|= BMAP_LEFT_DELAY
;
2488 * Check and set flags if the current (right) segment exists.
2489 * If it doesn't exist, we're converting the hole at end-of-file.
2491 if (xfs_iext_get_extent(ifp
, icur
, &right
)) {
2492 state
|= BMAP_RIGHT_VALID
;
2493 if (isnullstartblock(right
.br_startblock
))
2494 state
|= BMAP_RIGHT_DELAY
;
2498 * Set contiguity flags on the left and right neighbors.
2499 * Don't let extents get too large, even if the pieces are contiguous.
2501 if ((state
& BMAP_LEFT_VALID
) && (state
& BMAP_LEFT_DELAY
) &&
2502 left
.br_startoff
+ left
.br_blockcount
== new->br_startoff
&&
2503 left
.br_blockcount
+ new->br_blockcount
<= XFS_MAX_BMBT_EXTLEN
)
2504 state
|= BMAP_LEFT_CONTIG
;
2506 if ((state
& BMAP_RIGHT_VALID
) && (state
& BMAP_RIGHT_DELAY
) &&
2507 new->br_startoff
+ new->br_blockcount
== right
.br_startoff
&&
2508 new->br_blockcount
+ right
.br_blockcount
<= XFS_MAX_BMBT_EXTLEN
&&
2509 (!(state
& BMAP_LEFT_CONTIG
) ||
2510 (left
.br_blockcount
+ new->br_blockcount
+
2511 right
.br_blockcount
<= XFS_MAX_BMBT_EXTLEN
)))
2512 state
|= BMAP_RIGHT_CONTIG
;
2515 * Switch out based on the contiguity flags.
2517 switch (state
& (BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
)) {
2518 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2520 * New allocation is contiguous with delayed allocations
2521 * on the left and on the right.
2522 * Merge all three into a single extent record.
2524 temp
= left
.br_blockcount
+ new->br_blockcount
+
2525 right
.br_blockcount
;
2527 oldlen
= startblockval(left
.br_startblock
) +
2528 startblockval(new->br_startblock
) +
2529 startblockval(right
.br_startblock
);
2530 newlen
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
2532 left
.br_startblock
= nullstartblock(newlen
);
2533 left
.br_blockcount
= temp
;
2535 xfs_iext_remove(ip
, icur
, state
);
2536 xfs_iext_prev(ifp
, icur
);
2537 xfs_iext_update_extent(ip
, state
, icur
, &left
);
2540 case BMAP_LEFT_CONTIG
:
2542 * New allocation is contiguous with a delayed allocation
2544 * Merge the new allocation with the left neighbor.
2546 temp
= left
.br_blockcount
+ new->br_blockcount
;
2548 oldlen
= startblockval(left
.br_startblock
) +
2549 startblockval(new->br_startblock
);
2550 newlen
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
2552 left
.br_blockcount
= temp
;
2553 left
.br_startblock
= nullstartblock(newlen
);
2555 xfs_iext_prev(ifp
, icur
);
2556 xfs_iext_update_extent(ip
, state
, icur
, &left
);
2559 case BMAP_RIGHT_CONTIG
:
2561 * New allocation is contiguous with a delayed allocation
2563 * Merge the new allocation with the right neighbor.
2565 temp
= new->br_blockcount
+ right
.br_blockcount
;
2566 oldlen
= startblockval(new->br_startblock
) +
2567 startblockval(right
.br_startblock
);
2568 newlen
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
2570 right
.br_startoff
= new->br_startoff
;
2571 right
.br_startblock
= nullstartblock(newlen
);
2572 right
.br_blockcount
= temp
;
2573 xfs_iext_update_extent(ip
, state
, icur
, &right
);
2578 * New allocation is not contiguous with another
2579 * delayed allocation.
2580 * Insert a new entry.
2582 oldlen
= newlen
= 0;
2583 xfs_iext_insert(ip
, icur
, new, state
);
2586 if (oldlen
!= newlen
) {
2587 ASSERT(oldlen
> newlen
);
2588 xfs_mod_fdblocks(ip
->i_mount
, (int64_t)(oldlen
- newlen
),
2591 * Nothing to do for disk quota accounting here.
2593 xfs_mod_delalloc(ip
->i_mount
, (int64_t)newlen
- oldlen
);
2598 * Convert a hole to a real allocation.
2600 STATIC
int /* error */
2601 xfs_bmap_add_extent_hole_real(
2602 struct xfs_trans
*tp
,
2603 struct xfs_inode
*ip
,
2605 struct xfs_iext_cursor
*icur
,
2606 struct xfs_btree_cur
**curp
,
2607 struct xfs_bmbt_irec
*new,
2611 struct xfs_ifork
*ifp
= xfs_ifork_ptr(ip
, whichfork
);
2612 struct xfs_mount
*mp
= ip
->i_mount
;
2613 struct xfs_btree_cur
*cur
= *curp
;
2614 int error
; /* error return value */
2615 int i
; /* temp state */
2616 xfs_bmbt_irec_t left
; /* left neighbor extent entry */
2617 xfs_bmbt_irec_t right
; /* right neighbor extent entry */
2618 int rval
=0; /* return value (logging flags) */
2619 uint32_t state
= xfs_bmap_fork_to_state(whichfork
);
2620 struct xfs_bmbt_irec old
;
2622 ASSERT(!isnullstartblock(new->br_startblock
));
2623 ASSERT(!cur
|| !(cur
->bc_ino
.flags
& XFS_BTCUR_BMBT_WASDEL
));
2625 XFS_STATS_INC(mp
, xs_add_exlist
);
2628 * Check and set flags if this segment has a left neighbor.
2630 if (xfs_iext_peek_prev_extent(ifp
, icur
, &left
)) {
2631 state
|= BMAP_LEFT_VALID
;
2632 if (isnullstartblock(left
.br_startblock
))
2633 state
|= BMAP_LEFT_DELAY
;
2637 * Check and set flags if this segment has a current value.
2638 * Not true if we're inserting into the "hole" at eof.
2640 if (xfs_iext_get_extent(ifp
, icur
, &right
)) {
2641 state
|= BMAP_RIGHT_VALID
;
2642 if (isnullstartblock(right
.br_startblock
))
2643 state
|= BMAP_RIGHT_DELAY
;
2647 * We're inserting a real allocation between "left" and "right".
2648 * Set the contiguity flags. Don't let extents get too large.
2650 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
2651 left
.br_startoff
+ left
.br_blockcount
== new->br_startoff
&&
2652 left
.br_startblock
+ left
.br_blockcount
== new->br_startblock
&&
2653 left
.br_state
== new->br_state
&&
2654 left
.br_blockcount
+ new->br_blockcount
<= XFS_MAX_BMBT_EXTLEN
)
2655 state
|= BMAP_LEFT_CONTIG
;
2657 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
2658 new->br_startoff
+ new->br_blockcount
== right
.br_startoff
&&
2659 new->br_startblock
+ new->br_blockcount
== right
.br_startblock
&&
2660 new->br_state
== right
.br_state
&&
2661 new->br_blockcount
+ right
.br_blockcount
<= XFS_MAX_BMBT_EXTLEN
&&
2662 (!(state
& BMAP_LEFT_CONTIG
) ||
2663 left
.br_blockcount
+ new->br_blockcount
+
2664 right
.br_blockcount
<= XFS_MAX_BMBT_EXTLEN
))
2665 state
|= BMAP_RIGHT_CONTIG
;
2669 * Select which case we're in here, and implement it.
2671 switch (state
& (BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
)) {
2672 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2674 * New allocation is contiguous with real allocations on the
2675 * left and on the right.
2676 * Merge all three into a single extent record.
2678 left
.br_blockcount
+= new->br_blockcount
+ right
.br_blockcount
;
2680 xfs_iext_remove(ip
, icur
, state
);
2681 xfs_iext_prev(ifp
, icur
);
2682 xfs_iext_update_extent(ip
, state
, icur
, &left
);
2686 rval
= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
2688 rval
= XFS_ILOG_CORE
;
2689 error
= xfs_bmbt_lookup_eq(cur
, &right
, &i
);
2692 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
2693 error
= -EFSCORRUPTED
;
2696 error
= xfs_btree_delete(cur
, &i
);
2699 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
2700 error
= -EFSCORRUPTED
;
2703 error
= xfs_btree_decrement(cur
, 0, &i
);
2706 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
2707 error
= -EFSCORRUPTED
;
2710 error
= xfs_bmbt_update(cur
, &left
);
2716 case BMAP_LEFT_CONTIG
:
2718 * New allocation is contiguous with a real allocation
2720 * Merge the new allocation with the left neighbor.
2723 left
.br_blockcount
+= new->br_blockcount
;
2725 xfs_iext_prev(ifp
, icur
);
2726 xfs_iext_update_extent(ip
, state
, icur
, &left
);
2729 rval
= xfs_ilog_fext(whichfork
);
2732 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2735 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
2736 error
= -EFSCORRUPTED
;
2739 error
= xfs_bmbt_update(cur
, &left
);
2745 case BMAP_RIGHT_CONTIG
:
2747 * New allocation is contiguous with a real allocation
2749 * Merge the new allocation with the right neighbor.
2753 right
.br_startoff
= new->br_startoff
;
2754 right
.br_startblock
= new->br_startblock
;
2755 right
.br_blockcount
+= new->br_blockcount
;
2756 xfs_iext_update_extent(ip
, state
, icur
, &right
);
2759 rval
= xfs_ilog_fext(whichfork
);
2762 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2765 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
2766 error
= -EFSCORRUPTED
;
2769 error
= xfs_bmbt_update(cur
, &right
);
2777 * New allocation is not contiguous with another
2779 * Insert a new entry.
2781 xfs_iext_insert(ip
, icur
, new, state
);
2785 rval
= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
2787 rval
= XFS_ILOG_CORE
;
2788 error
= xfs_bmbt_lookup_eq(cur
, new, &i
);
2791 if (XFS_IS_CORRUPT(mp
, i
!= 0)) {
2792 error
= -EFSCORRUPTED
;
2795 error
= xfs_btree_insert(cur
, &i
);
2798 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
2799 error
= -EFSCORRUPTED
;
2806 /* add reverse mapping unless caller opted out */
2807 if (!(flags
& XFS_BMAPI_NORMAP
))
2808 xfs_rmap_map_extent(tp
, ip
, whichfork
, new);
2810 /* convert to a btree if necessary */
2811 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
2812 int tmp_logflags
; /* partial log flag return val */
2814 ASSERT(cur
== NULL
);
2815 error
= xfs_bmap_extents_to_btree(tp
, ip
, curp
, 0,
2816 &tmp_logflags
, whichfork
);
2817 *logflagsp
|= tmp_logflags
;
2823 /* clear out the allocated field, done with it now in any case. */
2825 cur
->bc_ino
.allocated
= 0;
2827 xfs_bmap_check_leaf_extents(cur
, ip
, whichfork
);
2834 * Functions used in the extent read, allocate and remove paths
2838 * Adjust the size of the new extent based on i_extsize and rt extsize.
2841 xfs_bmap_extsize_align(
2843 xfs_bmbt_irec_t
*gotp
, /* next extent pointer */
2844 xfs_bmbt_irec_t
*prevp
, /* previous extent pointer */
2845 xfs_extlen_t extsz
, /* align to this extent size */
2846 int rt
, /* is this a realtime inode? */
2847 int eof
, /* is extent at end-of-file? */
2848 int delay
, /* creating delalloc extent? */
2849 int convert
, /* overwriting unwritten extent? */
2850 xfs_fileoff_t
*offp
, /* in/out: aligned offset */
2851 xfs_extlen_t
*lenp
) /* in/out: aligned length */
2853 xfs_fileoff_t orig_off
; /* original offset */
2854 xfs_extlen_t orig_alen
; /* original length */
2855 xfs_fileoff_t orig_end
; /* original off+len */
2856 xfs_fileoff_t nexto
; /* next file offset */
2857 xfs_fileoff_t prevo
; /* previous file offset */
2858 xfs_fileoff_t align_off
; /* temp for offset */
2859 xfs_extlen_t align_alen
; /* temp for length */
2860 xfs_extlen_t temp
; /* temp for calculations */
2865 orig_off
= align_off
= *offp
;
2866 orig_alen
= align_alen
= *lenp
;
2867 orig_end
= orig_off
+ orig_alen
;
2870 * If this request overlaps an existing extent, then don't
2871 * attempt to perform any additional alignment.
2873 if (!delay
&& !eof
&&
2874 (orig_off
>= gotp
->br_startoff
) &&
2875 (orig_end
<= gotp
->br_startoff
+ gotp
->br_blockcount
)) {
2880 * If the file offset is unaligned vs. the extent size
2881 * we need to align it. This will be possible unless
2882 * the file was previously written with a kernel that didn't
2883 * perform this alignment, or if a truncate shot us in the
2886 div_u64_rem(orig_off
, extsz
, &temp
);
2892 /* Same adjustment for the end of the requested area. */
2893 temp
= (align_alen
% extsz
);
2895 align_alen
+= extsz
- temp
;
2898 * For large extent hint sizes, the aligned extent might be larger than
2899 * XFS_BMBT_MAX_EXTLEN. In that case, reduce the size by an extsz so
2900 * that it pulls the length back under XFS_BMBT_MAX_EXTLEN. The outer
2901 * allocation loops handle short allocation just fine, so it is safe to
2902 * do this. We only want to do it when we are forced to, though, because
2903 * it means more allocation operations are required.
2905 while (align_alen
> XFS_MAX_BMBT_EXTLEN
)
2906 align_alen
-= extsz
;
2907 ASSERT(align_alen
<= XFS_MAX_BMBT_EXTLEN
);
2910 * If the previous block overlaps with this proposed allocation
2911 * then move the start forward without adjusting the length.
2913 if (prevp
->br_startoff
!= NULLFILEOFF
) {
2914 if (prevp
->br_startblock
== HOLESTARTBLOCK
)
2915 prevo
= prevp
->br_startoff
;
2917 prevo
= prevp
->br_startoff
+ prevp
->br_blockcount
;
2920 if (align_off
!= orig_off
&& align_off
< prevo
)
2923 * If the next block overlaps with this proposed allocation
2924 * then move the start back without adjusting the length,
2925 * but not before offset 0.
2926 * This may of course make the start overlap previous block,
2927 * and if we hit the offset 0 limit then the next block
2928 * can still overlap too.
2930 if (!eof
&& gotp
->br_startoff
!= NULLFILEOFF
) {
2931 if ((delay
&& gotp
->br_startblock
== HOLESTARTBLOCK
) ||
2932 (!delay
&& gotp
->br_startblock
== DELAYSTARTBLOCK
))
2933 nexto
= gotp
->br_startoff
+ gotp
->br_blockcount
;
2935 nexto
= gotp
->br_startoff
;
2937 nexto
= NULLFILEOFF
;
2939 align_off
+ align_alen
!= orig_end
&&
2940 align_off
+ align_alen
> nexto
)
2941 align_off
= nexto
> align_alen
? nexto
- align_alen
: 0;
2943 * If we're now overlapping the next or previous extent that
2944 * means we can't fit an extsz piece in this hole. Just move
2945 * the start forward to the first valid spot and set
2946 * the length so we hit the end.
2948 if (align_off
!= orig_off
&& align_off
< prevo
)
2950 if (align_off
+ align_alen
!= orig_end
&&
2951 align_off
+ align_alen
> nexto
&&
2952 nexto
!= NULLFILEOFF
) {
2953 ASSERT(nexto
> prevo
);
2954 align_alen
= nexto
- align_off
;
2958 * If realtime, and the result isn't a multiple of the realtime
2959 * extent size we need to remove blocks until it is.
2961 if (rt
&& (temp
= (align_alen
% mp
->m_sb
.sb_rextsize
))) {
2963 * We're not covering the original request, or
2964 * we won't be able to once we fix the length.
2966 if (orig_off
< align_off
||
2967 orig_end
> align_off
+ align_alen
||
2968 align_alen
- temp
< orig_alen
)
2971 * Try to fix it by moving the start up.
2973 if (align_off
+ temp
<= orig_off
) {
2978 * Try to fix it by moving the end in.
2980 else if (align_off
+ align_alen
- temp
>= orig_end
)
2983 * Set the start to the minimum then trim the length.
2986 align_alen
-= orig_off
- align_off
;
2987 align_off
= orig_off
;
2988 align_alen
-= align_alen
% mp
->m_sb
.sb_rextsize
;
2991 * Result doesn't cover the request, fail it.
2993 if (orig_off
< align_off
|| orig_end
> align_off
+ align_alen
)
2996 ASSERT(orig_off
>= align_off
);
2997 /* see XFS_BMBT_MAX_EXTLEN handling above */
2998 ASSERT(orig_end
<= align_off
+ align_alen
||
2999 align_alen
+ extsz
> XFS_MAX_BMBT_EXTLEN
);
3003 if (!eof
&& gotp
->br_startoff
!= NULLFILEOFF
)
3004 ASSERT(align_off
+ align_alen
<= gotp
->br_startoff
);
3005 if (prevp
->br_startoff
!= NULLFILEOFF
)
3006 ASSERT(align_off
>= prevp
->br_startoff
+ prevp
->br_blockcount
);
3014 #define XFS_ALLOC_GAP_UNITS 4
3018 struct xfs_bmalloca
*ap
) /* bmap alloc argument struct */
3020 xfs_fsblock_t adjust
; /* adjustment to block numbers */
3021 xfs_agnumber_t fb_agno
; /* ag number of ap->firstblock */
3022 xfs_mount_t
*mp
; /* mount point structure */
3023 int nullfb
; /* true if ap->firstblock isn't set */
3024 int rt
; /* true if inode is realtime */
3026 #define ISVALID(x,y) \
3028 (x) < mp->m_sb.sb_rblocks : \
3029 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
3030 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
3031 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
3033 mp
= ap
->ip
->i_mount
;
3034 nullfb
= ap
->tp
->t_firstblock
== NULLFSBLOCK
;
3035 rt
= XFS_IS_REALTIME_INODE(ap
->ip
) &&
3036 (ap
->datatype
& XFS_ALLOC_USERDATA
);
3037 fb_agno
= nullfb
? NULLAGNUMBER
: XFS_FSB_TO_AGNO(mp
,
3038 ap
->tp
->t_firstblock
);
3040 * If allocating at eof, and there's a previous real block,
3041 * try to use its last block as our starting point.
3043 if (ap
->eof
&& ap
->prev
.br_startoff
!= NULLFILEOFF
&&
3044 !isnullstartblock(ap
->prev
.br_startblock
) &&
3045 ISVALID(ap
->prev
.br_startblock
+ ap
->prev
.br_blockcount
,
3046 ap
->prev
.br_startblock
)) {
3047 ap
->blkno
= ap
->prev
.br_startblock
+ ap
->prev
.br_blockcount
;
3049 * Adjust for the gap between prevp and us.
3051 adjust
= ap
->offset
-
3052 (ap
->prev
.br_startoff
+ ap
->prev
.br_blockcount
);
3054 ISVALID(ap
->blkno
+ adjust
, ap
->prev
.br_startblock
))
3055 ap
->blkno
+= adjust
;
3058 * If not at eof, then compare the two neighbor blocks.
3059 * Figure out whether either one gives us a good starting point,
3060 * and pick the better one.
3062 else if (!ap
->eof
) {
3063 xfs_fsblock_t gotbno
; /* right side block number */
3064 xfs_fsblock_t gotdiff
=0; /* right side difference */
3065 xfs_fsblock_t prevbno
; /* left side block number */
3066 xfs_fsblock_t prevdiff
=0; /* left side difference */
3069 * If there's a previous (left) block, select a requested
3070 * start block based on it.
3072 if (ap
->prev
.br_startoff
!= NULLFILEOFF
&&
3073 !isnullstartblock(ap
->prev
.br_startblock
) &&
3074 (prevbno
= ap
->prev
.br_startblock
+
3075 ap
->prev
.br_blockcount
) &&
3076 ISVALID(prevbno
, ap
->prev
.br_startblock
)) {
3078 * Calculate gap to end of previous block.
3080 adjust
= prevdiff
= ap
->offset
-
3081 (ap
->prev
.br_startoff
+
3082 ap
->prev
.br_blockcount
);
3084 * Figure the startblock based on the previous block's
3085 * end and the gap size.
3087 * If the gap is large relative to the piece we're
3088 * allocating, or using it gives us an invalid block
3089 * number, then just use the end of the previous block.
3091 if (prevdiff
<= XFS_ALLOC_GAP_UNITS
* ap
->length
&&
3092 ISVALID(prevbno
+ prevdiff
,
3093 ap
->prev
.br_startblock
))
3098 * If the firstblock forbids it, can't use it,
3101 if (!rt
&& !nullfb
&&
3102 XFS_FSB_TO_AGNO(mp
, prevbno
) != fb_agno
)
3103 prevbno
= NULLFSBLOCK
;
3106 * No previous block or can't follow it, just default.
3109 prevbno
= NULLFSBLOCK
;
3111 * If there's a following (right) block, select a requested
3112 * start block based on it.
3114 if (!isnullstartblock(ap
->got
.br_startblock
)) {
3116 * Calculate gap to start of next block.
3118 adjust
= gotdiff
= ap
->got
.br_startoff
- ap
->offset
;
3120 * Figure the startblock based on the next block's
3121 * start and the gap size.
3123 gotbno
= ap
->got
.br_startblock
;
3126 * If the gap is large relative to the piece we're
3127 * allocating, or using it gives us an invalid block
3128 * number, then just use the start of the next block
3129 * offset by our length.
3131 if (gotdiff
<= XFS_ALLOC_GAP_UNITS
* ap
->length
&&
3132 ISVALID(gotbno
- gotdiff
, gotbno
))
3134 else if (ISVALID(gotbno
- ap
->length
, gotbno
)) {
3135 gotbno
-= ap
->length
;
3136 gotdiff
+= adjust
- ap
->length
;
3140 * If the firstblock forbids it, can't use it,
3143 if (!rt
&& !nullfb
&&
3144 XFS_FSB_TO_AGNO(mp
, gotbno
) != fb_agno
)
3145 gotbno
= NULLFSBLOCK
;
3148 * No next block, just default.
3151 gotbno
= NULLFSBLOCK
;
3153 * If both valid, pick the better one, else the only good
3154 * one, else ap->blkno is already set (to 0 or the inode block).
3156 if (prevbno
!= NULLFSBLOCK
&& gotbno
!= NULLFSBLOCK
)
3157 ap
->blkno
= prevdiff
<= gotdiff
? prevbno
: gotbno
;
3158 else if (prevbno
!= NULLFSBLOCK
)
3159 ap
->blkno
= prevbno
;
3160 else if (gotbno
!= NULLFSBLOCK
)
3167 xfs_bmap_longest_free_extent(
3168 struct xfs_trans
*tp
,
3173 struct xfs_mount
*mp
= tp
->t_mountp
;
3174 struct xfs_perag
*pag
;
3175 xfs_extlen_t longest
;
3178 pag
= xfs_perag_get(mp
, ag
);
3179 if (!pag
->pagf_init
) {
3180 error
= xfs_alloc_read_agf(pag
, tp
, XFS_ALLOC_FLAG_TRYLOCK
,
3183 /* Couldn't lock the AGF, so skip this AG. */
3184 if (error
== -EAGAIN
) {
3192 longest
= xfs_alloc_longest_free_extent(pag
,
3193 xfs_alloc_min_freelist(mp
, pag
),
3194 xfs_ag_resv_needed(pag
, XFS_AG_RESV_NONE
));
3195 if (*blen
< longest
)
3204 xfs_bmap_select_minlen(
3205 struct xfs_bmalloca
*ap
,
3206 struct xfs_alloc_arg
*args
,
3210 if (notinit
|| *blen
< ap
->minlen
) {
3212 * Since we did a BUF_TRYLOCK above, it is possible that
3213 * there is space for this request.
3215 args
->minlen
= ap
->minlen
;
3216 } else if (*blen
< args
->maxlen
) {
3218 * If the best seen length is less than the request length,
3219 * use the best as the minimum.
3221 args
->minlen
= *blen
;
3224 * Otherwise we've seen an extent as big as maxlen, use that
3227 args
->minlen
= args
->maxlen
;
3232 xfs_bmap_btalloc_nullfb(
3233 struct xfs_bmalloca
*ap
,
3234 struct xfs_alloc_arg
*args
,
3237 struct xfs_mount
*mp
= ap
->ip
->i_mount
;
3238 xfs_agnumber_t ag
, startag
;
3242 args
->type
= XFS_ALLOCTYPE_START_BNO
;
3243 args
->total
= ap
->total
;
3245 startag
= ag
= XFS_FSB_TO_AGNO(mp
, args
->fsbno
);
3246 if (startag
== NULLAGNUMBER
)
3249 while (*blen
< args
->maxlen
) {
3250 error
= xfs_bmap_longest_free_extent(args
->tp
, ag
, blen
,
3255 if (++ag
== mp
->m_sb
.sb_agcount
)
3261 xfs_bmap_select_minlen(ap
, args
, blen
, notinit
);
3266 xfs_bmap_btalloc_filestreams(
3267 struct xfs_bmalloca
*ap
,
3268 struct xfs_alloc_arg
*args
,
3271 struct xfs_mount
*mp
= ap
->ip
->i_mount
;
3276 args
->type
= XFS_ALLOCTYPE_NEAR_BNO
;
3277 args
->total
= ap
->total
;
3279 ag
= XFS_FSB_TO_AGNO(mp
, args
->fsbno
);
3280 if (ag
== NULLAGNUMBER
)
3283 error
= xfs_bmap_longest_free_extent(args
->tp
, ag
, blen
, ¬init
);
3287 if (*blen
< args
->maxlen
) {
3288 error
= xfs_filestream_new_ag(ap
, &ag
);
3292 error
= xfs_bmap_longest_free_extent(args
->tp
, ag
, blen
,
3299 xfs_bmap_select_minlen(ap
, args
, blen
, notinit
);
3302 * Set the failure fallback case to look in the selected AG as stream
3305 ap
->blkno
= args
->fsbno
= XFS_AGB_TO_FSB(mp
, ag
, 0);
3309 /* Update all inode and quota accounting for the allocation we just did. */
3311 xfs_bmap_btalloc_accounting(
3312 struct xfs_bmalloca
*ap
,
3313 struct xfs_alloc_arg
*args
)
3315 if (ap
->flags
& XFS_BMAPI_COWFORK
) {
3317 * COW fork blocks are in-core only and thus are treated as
3318 * in-core quota reservation (like delalloc blocks) even when
3319 * converted to real blocks. The quota reservation is not
3320 * accounted to disk until blocks are remapped to the data
3321 * fork. So if these blocks were previously delalloc, we
3322 * already have quota reservation and there's nothing to do
3326 xfs_mod_delalloc(ap
->ip
->i_mount
, -(int64_t)args
->len
);
3331 * Otherwise, we've allocated blocks in a hole. The transaction
3332 * has acquired in-core quota reservation for this extent.
3333 * Rather than account these as real blocks, however, we reduce
3334 * the transaction quota reservation based on the allocation.
3335 * This essentially transfers the transaction quota reservation
3336 * to that of a delalloc extent.
3338 ap
->ip
->i_delayed_blks
+= args
->len
;
3339 xfs_trans_mod_dquot_byino(ap
->tp
, ap
->ip
, XFS_TRANS_DQ_RES_BLKS
,
3344 /* data/attr fork only */
3345 ap
->ip
->i_nblocks
+= args
->len
;
3346 xfs_trans_log_inode(ap
->tp
, ap
->ip
, XFS_ILOG_CORE
);
3348 ap
->ip
->i_delayed_blks
-= args
->len
;
3349 xfs_mod_delalloc(ap
->ip
->i_mount
, -(int64_t)args
->len
);
3351 xfs_trans_mod_dquot_byino(ap
->tp
, ap
->ip
,
3352 ap
->wasdel
? XFS_TRANS_DQ_DELBCOUNT
: XFS_TRANS_DQ_BCOUNT
,
3357 xfs_bmap_compute_alignments(
3358 struct xfs_bmalloca
*ap
,
3359 struct xfs_alloc_arg
*args
)
3361 struct xfs_mount
*mp
= args
->mp
;
3362 xfs_extlen_t align
= 0; /* minimum allocation alignment */
3363 int stripe_align
= 0;
3365 /* stripe alignment for allocation is determined by mount parameters */
3366 if (mp
->m_swidth
&& xfs_has_swalloc(mp
))
3367 stripe_align
= mp
->m_swidth
;
3368 else if (mp
->m_dalign
)
3369 stripe_align
= mp
->m_dalign
;
3371 if (ap
->flags
& XFS_BMAPI_COWFORK
)
3372 align
= xfs_get_cowextsz_hint(ap
->ip
);
3373 else if (ap
->datatype
& XFS_ALLOC_USERDATA
)
3374 align
= xfs_get_extsz_hint(ap
->ip
);
3376 if (xfs_bmap_extsize_align(mp
, &ap
->got
, &ap
->prev
, align
, 0,
3377 ap
->eof
, 0, ap
->conv
, &ap
->offset
,
3383 /* apply extent size hints if obtained earlier */
3386 div_u64_rem(ap
->offset
, args
->prod
, &args
->mod
);
3388 args
->mod
= args
->prod
- args
->mod
;
3389 } else if (mp
->m_sb
.sb_blocksize
>= PAGE_SIZE
) {
3393 args
->prod
= PAGE_SIZE
>> mp
->m_sb
.sb_blocklog
;
3394 div_u64_rem(ap
->offset
, args
->prod
, &args
->mod
);
3396 args
->mod
= args
->prod
- args
->mod
;
3399 return stripe_align
;
3403 xfs_bmap_process_allocated_extent(
3404 struct xfs_bmalloca
*ap
,
3405 struct xfs_alloc_arg
*args
,
3406 xfs_fileoff_t orig_offset
,
3407 xfs_extlen_t orig_length
)
3411 nullfb
= ap
->tp
->t_firstblock
== NULLFSBLOCK
;
3414 * check the allocation happened at the same or higher AG than
3415 * the first block that was allocated.
3418 XFS_FSB_TO_AGNO(args
->mp
, ap
->tp
->t_firstblock
) <=
3419 XFS_FSB_TO_AGNO(args
->mp
, args
->fsbno
));
3421 ap
->blkno
= args
->fsbno
;
3423 ap
->tp
->t_firstblock
= args
->fsbno
;
3424 ap
->length
= args
->len
;
3426 * If the extent size hint is active, we tried to round the
3427 * caller's allocation request offset down to extsz and the
3428 * length up to another extsz boundary. If we found a free
3429 * extent we mapped it in starting at this new offset. If the
3430 * newly mapped space isn't long enough to cover any of the
3431 * range of offsets that was originally requested, move the
3432 * mapping up so that we can fill as much of the caller's
3433 * original request as possible. Free space is apparently
3434 * very fragmented so we're unlikely to be able to satisfy the
3437 if (ap
->length
<= orig_length
)
3438 ap
->offset
= orig_offset
;
3439 else if (ap
->offset
+ ap
->length
< orig_offset
+ orig_length
)
3440 ap
->offset
= orig_offset
+ orig_length
- ap
->length
;
3441 xfs_bmap_btalloc_accounting(ap
, args
);
3446 xfs_bmap_exact_minlen_extent_alloc(
3447 struct xfs_bmalloca
*ap
)
3449 struct xfs_mount
*mp
= ap
->ip
->i_mount
;
3450 struct xfs_alloc_arg args
= { .tp
= ap
->tp
, .mp
= mp
};
3451 xfs_fileoff_t orig_offset
;
3452 xfs_extlen_t orig_length
;
3457 if (ap
->minlen
!= 1) {
3458 ap
->blkno
= NULLFSBLOCK
;
3463 orig_offset
= ap
->offset
;
3464 orig_length
= ap
->length
;
3466 args
.alloc_minlen_only
= 1;
3468 xfs_bmap_compute_alignments(ap
, &args
);
3470 if (ap
->tp
->t_firstblock
== NULLFSBLOCK
) {
3472 * Unlike the longest extent available in an AG, we don't track
3473 * the length of an AG's shortest extent.
3474 * XFS_ERRTAG_BMAP_ALLOC_MINLEN_EXTENT is a debug only knob and
3475 * hence we can afford to start traversing from the 0th AG since
3476 * we need not be concerned about a drop in performance in
3477 * "debug only" code paths.
3479 ap
->blkno
= XFS_AGB_TO_FSB(mp
, 0, 0);
3481 ap
->blkno
= ap
->tp
->t_firstblock
;
3484 args
.fsbno
= ap
->blkno
;
3485 args
.oinfo
= XFS_RMAP_OINFO_SKIP_UPDATE
;
3486 args
.type
= XFS_ALLOCTYPE_FIRST_AG
;
3487 args
.minlen
= args
.maxlen
= ap
->minlen
;
3488 args
.total
= ap
->total
;
3491 args
.minalignslop
= 0;
3493 args
.minleft
= ap
->minleft
;
3494 args
.wasdel
= ap
->wasdel
;
3495 args
.resv
= XFS_AG_RESV_NONE
;
3496 args
.datatype
= ap
->datatype
;
3498 error
= xfs_alloc_vextent(&args
);
3502 if (args
.fsbno
!= NULLFSBLOCK
) {
3503 xfs_bmap_process_allocated_extent(ap
, &args
, orig_offset
,
3506 ap
->blkno
= NULLFSBLOCK
;
3514 #define xfs_bmap_exact_minlen_extent_alloc(bma) (-EFSCORRUPTED)
3520 struct xfs_bmalloca
*ap
)
3522 struct xfs_mount
*mp
= ap
->ip
->i_mount
;
3523 struct xfs_alloc_arg args
= { .tp
= ap
->tp
, .mp
= mp
};
3524 xfs_alloctype_t atype
= 0;
3525 xfs_agnumber_t fb_agno
; /* ag number of ap->firstblock */
3527 xfs_fileoff_t orig_offset
;
3528 xfs_extlen_t orig_length
;
3530 xfs_extlen_t nextminlen
= 0;
3531 int nullfb
; /* true if ap->firstblock isn't set */
3538 orig_offset
= ap
->offset
;
3539 orig_length
= ap
->length
;
3541 stripe_align
= xfs_bmap_compute_alignments(ap
, &args
);
3543 nullfb
= ap
->tp
->t_firstblock
== NULLFSBLOCK
;
3544 fb_agno
= nullfb
? NULLAGNUMBER
: XFS_FSB_TO_AGNO(mp
,
3545 ap
->tp
->t_firstblock
);
3547 if ((ap
->datatype
& XFS_ALLOC_USERDATA
) &&
3548 xfs_inode_is_filestream(ap
->ip
)) {
3549 ag
= xfs_filestream_lookup_ag(ap
->ip
);
3550 ag
= (ag
!= NULLAGNUMBER
) ? ag
: 0;
3551 ap
->blkno
= XFS_AGB_TO_FSB(mp
, ag
, 0);
3553 ap
->blkno
= XFS_INO_TO_FSB(mp
, ap
->ip
->i_ino
);
3556 ap
->blkno
= ap
->tp
->t_firstblock
;
3558 xfs_bmap_adjacent(ap
);
3561 * If allowed, use ap->blkno; otherwise must use firstblock since
3562 * it's in the right allocation group.
3564 if (nullfb
|| XFS_FSB_TO_AGNO(mp
, ap
->blkno
) == fb_agno
)
3567 ap
->blkno
= ap
->tp
->t_firstblock
;
3569 * Normal allocation, done through xfs_alloc_vextent.
3571 tryagain
= isaligned
= 0;
3572 args
.fsbno
= ap
->blkno
;
3573 args
.oinfo
= XFS_RMAP_OINFO_SKIP_UPDATE
;
3575 /* Trim the allocation back to the maximum an AG can fit. */
3576 args
.maxlen
= min(ap
->length
, mp
->m_ag_max_usable
);
3580 * Search for an allocation group with a single extent large
3581 * enough for the request. If one isn't found, then adjust
3582 * the minimum allocation size to the largest space found.
3584 if ((ap
->datatype
& XFS_ALLOC_USERDATA
) &&
3585 xfs_inode_is_filestream(ap
->ip
))
3586 error
= xfs_bmap_btalloc_filestreams(ap
, &args
, &blen
);
3588 error
= xfs_bmap_btalloc_nullfb(ap
, &args
, &blen
);
3591 } else if (ap
->tp
->t_flags
& XFS_TRANS_LOWMODE
) {
3592 if (xfs_inode_is_filestream(ap
->ip
))
3593 args
.type
= XFS_ALLOCTYPE_FIRST_AG
;
3595 args
.type
= XFS_ALLOCTYPE_START_BNO
;
3596 args
.total
= args
.minlen
= ap
->minlen
;
3598 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
3599 args
.total
= ap
->total
;
3600 args
.minlen
= ap
->minlen
;
3604 * If we are not low on available data blocks, and the underlying
3605 * logical volume manager is a stripe, and the file offset is zero then
3606 * try to allocate data blocks on stripe unit boundary. NOTE: ap->aeof
3607 * is only set if the allocation length is >= the stripe unit and the
3608 * allocation offset is at the end of file.
3610 if (!(ap
->tp
->t_flags
& XFS_TRANS_LOWMODE
) && ap
->aeof
) {
3612 args
.alignment
= stripe_align
;
3616 * Adjust minlen to try and preserve alignment if we
3617 * can't guarantee an aligned maxlen extent.
3619 if (blen
> args
.alignment
&&
3620 blen
<= args
.maxlen
+ args
.alignment
)
3621 args
.minlen
= blen
- args
.alignment
;
3622 args
.minalignslop
= 0;
3625 * First try an exact bno allocation.
3626 * If it fails then do a near or start bno
3627 * allocation with alignment turned on.
3631 args
.type
= XFS_ALLOCTYPE_THIS_BNO
;
3634 * Compute the minlen+alignment for the
3635 * next case. Set slop so that the value
3636 * of minlen+alignment+slop doesn't go up
3637 * between the calls.
3639 if (blen
> stripe_align
&& blen
<= args
.maxlen
)
3640 nextminlen
= blen
- stripe_align
;
3642 nextminlen
= args
.minlen
;
3643 if (nextminlen
+ stripe_align
> args
.minlen
+ 1)
3645 nextminlen
+ stripe_align
-
3648 args
.minalignslop
= 0;
3652 args
.minalignslop
= 0;
3654 args
.minleft
= ap
->minleft
;
3655 args
.wasdel
= ap
->wasdel
;
3656 args
.resv
= XFS_AG_RESV_NONE
;
3657 args
.datatype
= ap
->datatype
;
3659 error
= xfs_alloc_vextent(&args
);
3663 if (tryagain
&& args
.fsbno
== NULLFSBLOCK
) {
3665 * Exact allocation failed. Now try with alignment
3669 args
.fsbno
= ap
->blkno
;
3670 args
.alignment
= stripe_align
;
3671 args
.minlen
= nextminlen
;
3672 args
.minalignslop
= 0;
3674 if ((error
= xfs_alloc_vextent(&args
)))
3677 if (isaligned
&& args
.fsbno
== NULLFSBLOCK
) {
3679 * allocation failed, so turn off alignment and
3683 args
.fsbno
= ap
->blkno
;
3685 if ((error
= xfs_alloc_vextent(&args
)))
3688 if (args
.fsbno
== NULLFSBLOCK
&& nullfb
&&
3689 args
.minlen
> ap
->minlen
) {
3690 args
.minlen
= ap
->minlen
;
3691 args
.type
= XFS_ALLOCTYPE_START_BNO
;
3692 args
.fsbno
= ap
->blkno
;
3693 if ((error
= xfs_alloc_vextent(&args
)))
3696 if (args
.fsbno
== NULLFSBLOCK
&& nullfb
) {
3698 args
.type
= XFS_ALLOCTYPE_FIRST_AG
;
3699 args
.total
= ap
->minlen
;
3700 if ((error
= xfs_alloc_vextent(&args
)))
3702 ap
->tp
->t_flags
|= XFS_TRANS_LOWMODE
;
3705 if (args
.fsbno
!= NULLFSBLOCK
) {
3706 xfs_bmap_process_allocated_extent(ap
, &args
, orig_offset
,
3709 ap
->blkno
= NULLFSBLOCK
;
3715 /* Trim extent to fit a logical block range. */
3718 struct xfs_bmbt_irec
*irec
,
3722 xfs_fileoff_t distance
;
3723 xfs_fileoff_t end
= bno
+ len
;
3725 if (irec
->br_startoff
+ irec
->br_blockcount
<= bno
||
3726 irec
->br_startoff
>= end
) {
3727 irec
->br_blockcount
= 0;
3731 if (irec
->br_startoff
< bno
) {
3732 distance
= bno
- irec
->br_startoff
;
3733 if (isnullstartblock(irec
->br_startblock
))
3734 irec
->br_startblock
= DELAYSTARTBLOCK
;
3735 if (irec
->br_startblock
!= DELAYSTARTBLOCK
&&
3736 irec
->br_startblock
!= HOLESTARTBLOCK
)
3737 irec
->br_startblock
+= distance
;
3738 irec
->br_startoff
+= distance
;
3739 irec
->br_blockcount
-= distance
;
3742 if (end
< irec
->br_startoff
+ irec
->br_blockcount
) {
3743 distance
= irec
->br_startoff
+ irec
->br_blockcount
- end
;
3744 irec
->br_blockcount
-= distance
;
3749 * Trim the returned map to the required bounds
3753 struct xfs_bmbt_irec
*mval
,
3754 struct xfs_bmbt_irec
*got
,
3762 if ((flags
& XFS_BMAPI_ENTIRE
) ||
3763 got
->br_startoff
+ got
->br_blockcount
<= obno
) {
3765 if (isnullstartblock(got
->br_startblock
))
3766 mval
->br_startblock
= DELAYSTARTBLOCK
;
3772 ASSERT((*bno
>= obno
) || (n
== 0));
3774 mval
->br_startoff
= *bno
;
3775 if (isnullstartblock(got
->br_startblock
))
3776 mval
->br_startblock
= DELAYSTARTBLOCK
;
3778 mval
->br_startblock
= got
->br_startblock
+
3779 (*bno
- got
->br_startoff
);
3781 * Return the minimum of what we got and what we asked for for
3782 * the length. We can use the len variable here because it is
3783 * modified below and we could have been there before coming
3784 * here if the first part of the allocation didn't overlap what
3787 mval
->br_blockcount
= XFS_FILBLKS_MIN(end
- *bno
,
3788 got
->br_blockcount
- (*bno
- got
->br_startoff
));
3789 mval
->br_state
= got
->br_state
;
3790 ASSERT(mval
->br_blockcount
<= len
);
3795 * Update and validate the extent map to return
3798 xfs_bmapi_update_map(
3799 struct xfs_bmbt_irec
**map
,
3807 xfs_bmbt_irec_t
*mval
= *map
;
3809 ASSERT((flags
& XFS_BMAPI_ENTIRE
) ||
3810 ((mval
->br_startoff
+ mval
->br_blockcount
) <= end
));
3811 ASSERT((flags
& XFS_BMAPI_ENTIRE
) || (mval
->br_blockcount
<= *len
) ||
3812 (mval
->br_startoff
< obno
));
3814 *bno
= mval
->br_startoff
+ mval
->br_blockcount
;
3816 if (*n
> 0 && mval
->br_startoff
== mval
[-1].br_startoff
) {
3817 /* update previous map with new information */
3818 ASSERT(mval
->br_startblock
== mval
[-1].br_startblock
);
3819 ASSERT(mval
->br_blockcount
> mval
[-1].br_blockcount
);
3820 ASSERT(mval
->br_state
== mval
[-1].br_state
);
3821 mval
[-1].br_blockcount
= mval
->br_blockcount
;
3822 mval
[-1].br_state
= mval
->br_state
;
3823 } else if (*n
> 0 && mval
->br_startblock
!= DELAYSTARTBLOCK
&&
3824 mval
[-1].br_startblock
!= DELAYSTARTBLOCK
&&
3825 mval
[-1].br_startblock
!= HOLESTARTBLOCK
&&
3826 mval
->br_startblock
== mval
[-1].br_startblock
+
3827 mval
[-1].br_blockcount
&&
3828 mval
[-1].br_state
== mval
->br_state
) {
3829 ASSERT(mval
->br_startoff
==
3830 mval
[-1].br_startoff
+ mval
[-1].br_blockcount
);
3831 mval
[-1].br_blockcount
+= mval
->br_blockcount
;
3832 } else if (*n
> 0 &&
3833 mval
->br_startblock
== DELAYSTARTBLOCK
&&
3834 mval
[-1].br_startblock
== DELAYSTARTBLOCK
&&
3835 mval
->br_startoff
==
3836 mval
[-1].br_startoff
+ mval
[-1].br_blockcount
) {
3837 mval
[-1].br_blockcount
+= mval
->br_blockcount
;
3838 mval
[-1].br_state
= mval
->br_state
;
3839 } else if (!((*n
== 0) &&
3840 ((mval
->br_startoff
+ mval
->br_blockcount
) <=
3849 * Map file blocks to filesystem blocks without allocation.
3853 struct xfs_inode
*ip
,
3856 struct xfs_bmbt_irec
*mval
,
3860 struct xfs_mount
*mp
= ip
->i_mount
;
3861 int whichfork
= xfs_bmapi_whichfork(flags
);
3862 struct xfs_ifork
*ifp
= xfs_ifork_ptr(ip
, whichfork
);
3863 struct xfs_bmbt_irec got
;
3866 struct xfs_iext_cursor icur
;
3872 ASSERT(!(flags
& ~(XFS_BMAPI_ATTRFORK
| XFS_BMAPI_ENTIRE
)));
3873 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_SHARED
|XFS_ILOCK_EXCL
));
3875 if (WARN_ON_ONCE(!ifp
))
3876 return -EFSCORRUPTED
;
3878 if (XFS_IS_CORRUPT(mp
, !xfs_ifork_has_extents(ifp
)) ||
3879 XFS_TEST_ERROR(false, mp
, XFS_ERRTAG_BMAPIFORMAT
))
3880 return -EFSCORRUPTED
;
3882 if (xfs_is_shutdown(mp
))
3885 XFS_STATS_INC(mp
, xs_blk_mapr
);
3887 error
= xfs_iread_extents(NULL
, ip
, whichfork
);
3891 if (!xfs_iext_lookup_extent(ip
, ifp
, bno
, &icur
, &got
))
3896 while (bno
< end
&& n
< *nmap
) {
3897 /* Reading past eof, act as though there's a hole up to end. */
3899 got
.br_startoff
= end
;
3900 if (got
.br_startoff
> bno
) {
3901 /* Reading in a hole. */
3902 mval
->br_startoff
= bno
;
3903 mval
->br_startblock
= HOLESTARTBLOCK
;
3904 mval
->br_blockcount
=
3905 XFS_FILBLKS_MIN(len
, got
.br_startoff
- bno
);
3906 mval
->br_state
= XFS_EXT_NORM
;
3907 bno
+= mval
->br_blockcount
;
3908 len
-= mval
->br_blockcount
;
3914 /* set up the extent map to return. */
3915 xfs_bmapi_trim_map(mval
, &got
, &bno
, len
, obno
, end
, n
, flags
);
3916 xfs_bmapi_update_map(&mval
, &bno
, &len
, obno
, end
, &n
, flags
);
3918 /* If we're done, stop now. */
3919 if (bno
>= end
|| n
>= *nmap
)
3922 /* Else go on to the next record. */
3923 if (!xfs_iext_next_extent(ifp
, &icur
, &got
))
3931 * Add a delayed allocation extent to an inode. Blocks are reserved from the
3932 * global pool and the extent inserted into the inode in-core extent tree.
3934 * On entry, got refers to the first extent beyond the offset of the extent to
3935 * allocate or eof is specified if no such extent exists. On return, got refers
3936 * to the extent record that was inserted to the inode fork.
3938 * Note that the allocated extent may have been merged with contiguous extents
3939 * during insertion into the inode fork. Thus, got does not reflect the current
3940 * state of the inode fork on return. If necessary, the caller can use lastx to
3941 * look up the updated record in the inode fork.
3944 xfs_bmapi_reserve_delalloc(
3945 struct xfs_inode
*ip
,
3949 xfs_filblks_t prealloc
,
3950 struct xfs_bmbt_irec
*got
,
3951 struct xfs_iext_cursor
*icur
,
3954 struct xfs_mount
*mp
= ip
->i_mount
;
3955 struct xfs_ifork
*ifp
= xfs_ifork_ptr(ip
, whichfork
);
3957 xfs_extlen_t indlen
;
3959 xfs_fileoff_t aoff
= off
;
3962 * Cap the alloc length. Keep track of prealloc so we know whether to
3963 * tag the inode before we return.
3965 alen
= XFS_FILBLKS_MIN(len
+ prealloc
, XFS_MAX_BMBT_EXTLEN
);
3967 alen
= XFS_FILBLKS_MIN(alen
, got
->br_startoff
- aoff
);
3968 if (prealloc
&& alen
>= len
)
3969 prealloc
= alen
- len
;
3971 /* Figure out the extent size, adjust alen */
3972 if (whichfork
== XFS_COW_FORK
) {
3973 struct xfs_bmbt_irec prev
;
3974 xfs_extlen_t extsz
= xfs_get_cowextsz_hint(ip
);
3976 if (!xfs_iext_peek_prev_extent(ifp
, icur
, &prev
))
3977 prev
.br_startoff
= NULLFILEOFF
;
3979 error
= xfs_bmap_extsize_align(mp
, got
, &prev
, extsz
, 0, eof
,
3980 1, 0, &aoff
, &alen
);
3985 * Make a transaction-less quota reservation for delayed allocation
3986 * blocks. This number gets adjusted later. We return if we haven't
3987 * allocated blocks already inside this loop.
3989 error
= xfs_quota_reserve_blkres(ip
, alen
);
3994 * Split changing sb for alen and indlen since they could be coming
3995 * from different places.
3997 indlen
= (xfs_extlen_t
)xfs_bmap_worst_indlen(ip
, alen
);
4000 error
= xfs_mod_fdblocks(mp
, -((int64_t)alen
), false);
4002 goto out_unreserve_quota
;
4004 error
= xfs_mod_fdblocks(mp
, -((int64_t)indlen
), false);
4006 goto out_unreserve_blocks
;
4009 ip
->i_delayed_blks
+= alen
;
4010 xfs_mod_delalloc(ip
->i_mount
, alen
+ indlen
);
4012 got
->br_startoff
= aoff
;
4013 got
->br_startblock
= nullstartblock(indlen
);
4014 got
->br_blockcount
= alen
;
4015 got
->br_state
= XFS_EXT_NORM
;
4017 xfs_bmap_add_extent_hole_delay(ip
, whichfork
, icur
, got
);
4020 * Tag the inode if blocks were preallocated. Note that COW fork
4021 * preallocation can occur at the start or end of the extent, even when
4022 * prealloc == 0, so we must also check the aligned offset and length.
4024 if (whichfork
== XFS_DATA_FORK
&& prealloc
)
4025 xfs_inode_set_eofblocks_tag(ip
);
4026 if (whichfork
== XFS_COW_FORK
&& (prealloc
|| aoff
< off
|| alen
> len
))
4027 xfs_inode_set_cowblocks_tag(ip
);
4031 out_unreserve_blocks
:
4032 xfs_mod_fdblocks(mp
, alen
, false);
4033 out_unreserve_quota
:
4034 if (XFS_IS_QUOTA_ON(mp
))
4035 xfs_quota_unreserve_blkres(ip
, alen
);
4040 xfs_bmap_alloc_userdata(
4041 struct xfs_bmalloca
*bma
)
4043 struct xfs_mount
*mp
= bma
->ip
->i_mount
;
4044 int whichfork
= xfs_bmapi_whichfork(bma
->flags
);
4048 * Set the data type being allocated. For the data fork, the first data
4049 * in the file is treated differently to all other allocations. For the
4050 * attribute fork, we only need to ensure the allocated range is not on
4053 bma
->datatype
= XFS_ALLOC_NOBUSY
;
4054 if (whichfork
== XFS_DATA_FORK
) {
4055 bma
->datatype
|= XFS_ALLOC_USERDATA
;
4056 if (bma
->offset
== 0)
4057 bma
->datatype
|= XFS_ALLOC_INITIAL_USER_DATA
;
4059 if (mp
->m_dalign
&& bma
->length
>= mp
->m_dalign
) {
4060 error
= xfs_bmap_isaeof(bma
, whichfork
);
4065 if (XFS_IS_REALTIME_INODE(bma
->ip
))
4066 return xfs_bmap_rtalloc(bma
);
4069 if (unlikely(XFS_TEST_ERROR(false, mp
,
4070 XFS_ERRTAG_BMAP_ALLOC_MINLEN_EXTENT
)))
4071 return xfs_bmap_exact_minlen_extent_alloc(bma
);
4073 return xfs_bmap_btalloc(bma
);
4078 struct xfs_bmalloca
*bma
)
4080 struct xfs_mount
*mp
= bma
->ip
->i_mount
;
4081 int whichfork
= xfs_bmapi_whichfork(bma
->flags
);
4082 struct xfs_ifork
*ifp
= xfs_ifork_ptr(bma
->ip
, whichfork
);
4083 int tmp_logflags
= 0;
4086 ASSERT(bma
->length
> 0);
4089 * For the wasdelay case, we could also just allocate the stuff asked
4090 * for in this bmap call but that wouldn't be as good.
4093 bma
->length
= (xfs_extlen_t
)bma
->got
.br_blockcount
;
4094 bma
->offset
= bma
->got
.br_startoff
;
4095 if (!xfs_iext_peek_prev_extent(ifp
, &bma
->icur
, &bma
->prev
))
4096 bma
->prev
.br_startoff
= NULLFILEOFF
;
4098 bma
->length
= XFS_FILBLKS_MIN(bma
->length
, XFS_MAX_BMBT_EXTLEN
);
4100 bma
->length
= XFS_FILBLKS_MIN(bma
->length
,
4101 bma
->got
.br_startoff
- bma
->offset
);
4104 if (bma
->flags
& XFS_BMAPI_CONTIG
)
4105 bma
->minlen
= bma
->length
;
4109 if (bma
->flags
& XFS_BMAPI_METADATA
) {
4110 if (unlikely(XFS_TEST_ERROR(false, mp
,
4111 XFS_ERRTAG_BMAP_ALLOC_MINLEN_EXTENT
)))
4112 error
= xfs_bmap_exact_minlen_extent_alloc(bma
);
4114 error
= xfs_bmap_btalloc(bma
);
4116 error
= xfs_bmap_alloc_userdata(bma
);
4118 if (error
|| bma
->blkno
== NULLFSBLOCK
)
4121 if (bma
->flags
& XFS_BMAPI_ZERO
) {
4122 error
= xfs_zero_extent(bma
->ip
, bma
->blkno
, bma
->length
);
4127 if (ifp
->if_format
== XFS_DINODE_FMT_BTREE
&& !bma
->cur
)
4128 bma
->cur
= xfs_bmbt_init_cursor(mp
, bma
->tp
, bma
->ip
, whichfork
);
4130 * Bump the number of extents we've allocated
4136 bma
->cur
->bc_ino
.flags
=
4137 bma
->wasdel
? XFS_BTCUR_BMBT_WASDEL
: 0;
4139 bma
->got
.br_startoff
= bma
->offset
;
4140 bma
->got
.br_startblock
= bma
->blkno
;
4141 bma
->got
.br_blockcount
= bma
->length
;
4142 bma
->got
.br_state
= XFS_EXT_NORM
;
4144 if (bma
->flags
& XFS_BMAPI_PREALLOC
)
4145 bma
->got
.br_state
= XFS_EXT_UNWRITTEN
;
4148 error
= xfs_bmap_add_extent_delay_real(bma
, whichfork
);
4150 error
= xfs_bmap_add_extent_hole_real(bma
->tp
, bma
->ip
,
4151 whichfork
, &bma
->icur
, &bma
->cur
, &bma
->got
,
4152 &bma
->logflags
, bma
->flags
);
4154 bma
->logflags
|= tmp_logflags
;
4159 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4160 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4161 * the neighbouring ones.
4163 xfs_iext_get_extent(ifp
, &bma
->icur
, &bma
->got
);
4165 ASSERT(bma
->got
.br_startoff
<= bma
->offset
);
4166 ASSERT(bma
->got
.br_startoff
+ bma
->got
.br_blockcount
>=
4167 bma
->offset
+ bma
->length
);
4168 ASSERT(bma
->got
.br_state
== XFS_EXT_NORM
||
4169 bma
->got
.br_state
== XFS_EXT_UNWRITTEN
);
4174 xfs_bmapi_convert_unwritten(
4175 struct xfs_bmalloca
*bma
,
4176 struct xfs_bmbt_irec
*mval
,
4180 int whichfork
= xfs_bmapi_whichfork(flags
);
4181 struct xfs_ifork
*ifp
= xfs_ifork_ptr(bma
->ip
, whichfork
);
4182 int tmp_logflags
= 0;
4185 /* check if we need to do unwritten->real conversion */
4186 if (mval
->br_state
== XFS_EXT_UNWRITTEN
&&
4187 (flags
& XFS_BMAPI_PREALLOC
))
4190 /* check if we need to do real->unwritten conversion */
4191 if (mval
->br_state
== XFS_EXT_NORM
&&
4192 (flags
& (XFS_BMAPI_PREALLOC
| XFS_BMAPI_CONVERT
)) !=
4193 (XFS_BMAPI_PREALLOC
| XFS_BMAPI_CONVERT
))
4197 * Modify (by adding) the state flag, if writing.
4199 ASSERT(mval
->br_blockcount
<= len
);
4200 if (ifp
->if_format
== XFS_DINODE_FMT_BTREE
&& !bma
->cur
) {
4201 bma
->cur
= xfs_bmbt_init_cursor(bma
->ip
->i_mount
, bma
->tp
,
4202 bma
->ip
, whichfork
);
4204 mval
->br_state
= (mval
->br_state
== XFS_EXT_UNWRITTEN
)
4205 ? XFS_EXT_NORM
: XFS_EXT_UNWRITTEN
;
4208 * Before insertion into the bmbt, zero the range being converted
4211 if (flags
& XFS_BMAPI_ZERO
) {
4212 error
= xfs_zero_extent(bma
->ip
, mval
->br_startblock
,
4213 mval
->br_blockcount
);
4218 error
= xfs_bmap_add_extent_unwritten_real(bma
->tp
, bma
->ip
, whichfork
,
4219 &bma
->icur
, &bma
->cur
, mval
, &tmp_logflags
);
4221 * Log the inode core unconditionally in the unwritten extent conversion
4222 * path because the conversion might not have done so (e.g., if the
4223 * extent count hasn't changed). We need to make sure the inode is dirty
4224 * in the transaction for the sake of fsync(), even if nothing has
4225 * changed, because fsync() will not force the log for this transaction
4226 * unless it sees the inode pinned.
4228 * Note: If we're only converting cow fork extents, there aren't
4229 * any on-disk updates to make, so we don't need to log anything.
4231 if (whichfork
!= XFS_COW_FORK
)
4232 bma
->logflags
|= tmp_logflags
| XFS_ILOG_CORE
;
4237 * Update our extent pointer, given that
4238 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4239 * of the neighbouring ones.
4241 xfs_iext_get_extent(ifp
, &bma
->icur
, &bma
->got
);
4244 * We may have combined previously unwritten space with written space,
4245 * so generate another request.
4247 if (mval
->br_blockcount
< len
)
4252 static inline xfs_extlen_t
4254 struct xfs_trans
*tp
,
4255 struct xfs_inode
*ip
,
4258 struct xfs_ifork
*ifp
= xfs_ifork_ptr(ip
, fork
);
4260 if (tp
&& tp
->t_firstblock
!= NULLFSBLOCK
)
4262 if (ifp
->if_format
!= XFS_DINODE_FMT_BTREE
)
4264 return be16_to_cpu(ifp
->if_broot
->bb_level
) + 1;
4268 * Log whatever the flags say, even if error. Otherwise we might miss detecting
4269 * a case where the data is changed, there's an error, and it's not logged so we
4270 * don't shutdown when we should. Don't bother logging extents/btree changes if
4271 * we converted to the other format.
4275 struct xfs_bmalloca
*bma
,
4279 struct xfs_ifork
*ifp
= xfs_ifork_ptr(bma
->ip
, whichfork
);
4281 if ((bma
->logflags
& xfs_ilog_fext(whichfork
)) &&
4282 ifp
->if_format
!= XFS_DINODE_FMT_EXTENTS
)
4283 bma
->logflags
&= ~xfs_ilog_fext(whichfork
);
4284 else if ((bma
->logflags
& xfs_ilog_fbroot(whichfork
)) &&
4285 ifp
->if_format
!= XFS_DINODE_FMT_BTREE
)
4286 bma
->logflags
&= ~xfs_ilog_fbroot(whichfork
);
4289 xfs_trans_log_inode(bma
->tp
, bma
->ip
, bma
->logflags
);
4291 xfs_btree_del_cursor(bma
->cur
, error
);
4295 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4296 * extent state if necessary. Details behaviour is controlled by the flags
4297 * parameter. Only allocates blocks from a single allocation group, to avoid
4302 struct xfs_trans
*tp
, /* transaction pointer */
4303 struct xfs_inode
*ip
, /* incore inode */
4304 xfs_fileoff_t bno
, /* starting file offs. mapped */
4305 xfs_filblks_t len
, /* length to map in file */
4306 uint32_t flags
, /* XFS_BMAPI_... */
4307 xfs_extlen_t total
, /* total blocks needed */
4308 struct xfs_bmbt_irec
*mval
, /* output: map values */
4309 int *nmap
) /* i/o: mval size/count */
4311 struct xfs_bmalloca bma
= {
4316 struct xfs_mount
*mp
= ip
->i_mount
;
4317 int whichfork
= xfs_bmapi_whichfork(flags
);
4318 struct xfs_ifork
*ifp
= xfs_ifork_ptr(ip
, whichfork
);
4319 xfs_fileoff_t end
; /* end of mapped file region */
4320 bool eof
= false; /* after the end of extents */
4321 int error
; /* error return */
4322 int n
; /* current extent index */
4323 xfs_fileoff_t obno
; /* old block number (offset) */
4326 xfs_fileoff_t orig_bno
; /* original block number value */
4327 int orig_flags
; /* original flags arg value */
4328 xfs_filblks_t orig_len
; /* original value of len arg */
4329 struct xfs_bmbt_irec
*orig_mval
; /* original value of mval */
4330 int orig_nmap
; /* original value of *nmap */
4340 ASSERT(*nmap
<= XFS_BMAP_MAX_NMAP
);
4343 ASSERT(ifp
->if_format
!= XFS_DINODE_FMT_LOCAL
);
4344 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
4345 ASSERT(!(flags
& XFS_BMAPI_REMAP
));
4347 /* zeroing is for currently only for data extents, not metadata */
4348 ASSERT((flags
& (XFS_BMAPI_METADATA
| XFS_BMAPI_ZERO
)) !=
4349 (XFS_BMAPI_METADATA
| XFS_BMAPI_ZERO
));
4351 * we can allocate unwritten extents or pre-zero allocated blocks,
4352 * but it makes no sense to do both at once. This would result in
4353 * zeroing the unwritten extent twice, but it still being an
4354 * unwritten extent....
4356 ASSERT((flags
& (XFS_BMAPI_PREALLOC
| XFS_BMAPI_ZERO
)) !=
4357 (XFS_BMAPI_PREALLOC
| XFS_BMAPI_ZERO
));
4359 if (XFS_IS_CORRUPT(mp
, !xfs_ifork_has_extents(ifp
)) ||
4360 XFS_TEST_ERROR(false, mp
, XFS_ERRTAG_BMAPIFORMAT
)) {
4361 return -EFSCORRUPTED
;
4364 if (xfs_is_shutdown(mp
))
4367 XFS_STATS_INC(mp
, xs_blk_mapw
);
4369 error
= xfs_iread_extents(tp
, ip
, whichfork
);
4373 if (!xfs_iext_lookup_extent(ip
, ifp
, bno
, &bma
.icur
, &bma
.got
))
4375 if (!xfs_iext_peek_prev_extent(ifp
, &bma
.icur
, &bma
.prev
))
4376 bma
.prev
.br_startoff
= NULLFILEOFF
;
4377 bma
.minleft
= xfs_bmapi_minleft(tp
, ip
, whichfork
);
4382 while (bno
< end
&& n
< *nmap
) {
4383 bool need_alloc
= false, wasdelay
= false;
4385 /* in hole or beyond EOF? */
4386 if (eof
|| bma
.got
.br_startoff
> bno
) {
4388 * CoW fork conversions should /never/ hit EOF or
4389 * holes. There should always be something for us
4392 ASSERT(!((flags
& XFS_BMAPI_CONVERT
) &&
4393 (flags
& XFS_BMAPI_COWFORK
)));
4396 } else if (isnullstartblock(bma
.got
.br_startblock
)) {
4401 * First, deal with the hole before the allocated space
4402 * that we found, if any.
4404 if (need_alloc
|| wasdelay
) {
4406 bma
.conv
= !!(flags
& XFS_BMAPI_CONVERT
);
4407 bma
.wasdel
= wasdelay
;
4412 * There's a 32/64 bit type mismatch between the
4413 * allocation length request (which can be 64 bits in
4414 * length) and the bma length request, which is
4415 * xfs_extlen_t and therefore 32 bits. Hence we have to
4416 * check for 32-bit overflows and handle them here.
4418 if (len
> (xfs_filblks_t
)XFS_MAX_BMBT_EXTLEN
)
4419 bma
.length
= XFS_MAX_BMBT_EXTLEN
;
4424 ASSERT(bma
.length
> 0);
4425 error
= xfs_bmapi_allocate(&bma
);
4428 if (bma
.blkno
== NULLFSBLOCK
)
4432 * If this is a CoW allocation, record the data in
4433 * the refcount btree for orphan recovery.
4435 if (whichfork
== XFS_COW_FORK
)
4436 xfs_refcount_alloc_cow_extent(tp
, bma
.blkno
,
4440 /* Deal with the allocated space we found. */
4441 xfs_bmapi_trim_map(mval
, &bma
.got
, &bno
, len
, obno
,
4444 /* Execute unwritten extent conversion if necessary */
4445 error
= xfs_bmapi_convert_unwritten(&bma
, mval
, len
, flags
);
4446 if (error
== -EAGAIN
)
4451 /* update the extent map to return */
4452 xfs_bmapi_update_map(&mval
, &bno
, &len
, obno
, end
, &n
, flags
);
4455 * If we're done, stop now. Stop when we've allocated
4456 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4457 * the transaction may get too big.
4459 if (bno
>= end
|| n
>= *nmap
|| bma
.nallocs
>= *nmap
)
4462 /* Else go on to the next record. */
4464 if (!xfs_iext_next_extent(ifp
, &bma
.icur
, &bma
.got
))
4469 error
= xfs_bmap_btree_to_extents(tp
, ip
, bma
.cur
, &bma
.logflags
,
4474 ASSERT(ifp
->if_format
!= XFS_DINODE_FMT_BTREE
||
4475 ifp
->if_nextents
> XFS_IFORK_MAXEXT(ip
, whichfork
));
4476 xfs_bmapi_finish(&bma
, whichfork
, 0);
4477 xfs_bmap_validate_ret(orig_bno
, orig_len
, orig_flags
, orig_mval
,
4481 xfs_bmapi_finish(&bma
, whichfork
, error
);
4486 * Convert an existing delalloc extent to real blocks based on file offset. This
4487 * attempts to allocate the entire delalloc extent and may require multiple
4488 * invocations to allocate the target offset if a large enough physical extent
4492 xfs_bmapi_convert_delalloc(
4493 struct xfs_inode
*ip
,
4496 struct iomap
*iomap
,
4499 struct xfs_ifork
*ifp
= xfs_ifork_ptr(ip
, whichfork
);
4500 struct xfs_mount
*mp
= ip
->i_mount
;
4501 xfs_fileoff_t offset_fsb
= XFS_B_TO_FSBT(mp
, offset
);
4502 struct xfs_bmalloca bma
= { NULL
};
4504 struct xfs_trans
*tp
;
4507 if (whichfork
== XFS_COW_FORK
)
4508 flags
|= IOMAP_F_SHARED
;
4511 * Space for the extent and indirect blocks was reserved when the
4512 * delalloc extent was created so there's no need to do so here.
4514 error
= xfs_trans_alloc(mp
, &M_RES(mp
)->tr_write
, 0, 0,
4515 XFS_TRANS_RESERVE
, &tp
);
4519 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
4520 xfs_trans_ijoin(tp
, ip
, 0);
4522 error
= xfs_iext_count_may_overflow(ip
, whichfork
,
4523 XFS_IEXT_ADD_NOSPLIT_CNT
);
4524 if (error
== -EFBIG
)
4525 error
= xfs_iext_count_upgrade(tp
, ip
,
4526 XFS_IEXT_ADD_NOSPLIT_CNT
);
4528 goto out_trans_cancel
;
4530 if (!xfs_iext_lookup_extent(ip
, ifp
, offset_fsb
, &bma
.icur
, &bma
.got
) ||
4531 bma
.got
.br_startoff
> offset_fsb
) {
4533 * No extent found in the range we are trying to convert. This
4534 * should only happen for the COW fork, where another thread
4535 * might have moved the extent to the data fork in the meantime.
4537 WARN_ON_ONCE(whichfork
!= XFS_COW_FORK
);
4539 goto out_trans_cancel
;
4543 * If we find a real extent here we raced with another thread converting
4544 * the extent. Just return the real extent at this offset.
4546 if (!isnullstartblock(bma
.got
.br_startblock
)) {
4547 xfs_bmbt_to_iomap(ip
, iomap
, &bma
.got
, 0, flags
,
4548 xfs_iomap_inode_sequence(ip
, flags
));
4549 *seq
= READ_ONCE(ifp
->if_seq
);
4550 goto out_trans_cancel
;
4556 bma
.offset
= bma
.got
.br_startoff
;
4557 bma
.length
= max_t(xfs_filblks_t
, bma
.got
.br_blockcount
,
4558 XFS_MAX_BMBT_EXTLEN
);
4559 bma
.minleft
= xfs_bmapi_minleft(tp
, ip
, whichfork
);
4562 * When we're converting the delalloc reservations backing dirty pages
4563 * in the page cache, we must be careful about how we create the new
4566 * New CoW fork extents are created unwritten, turned into real extents
4567 * when we're about to write the data to disk, and mapped into the data
4568 * fork after the write finishes. End of story.
4570 * New data fork extents must be mapped in as unwritten and converted
4571 * to real extents after the write succeeds to avoid exposing stale
4572 * disk contents if we crash.
4574 bma
.flags
= XFS_BMAPI_PREALLOC
;
4575 if (whichfork
== XFS_COW_FORK
)
4576 bma
.flags
|= XFS_BMAPI_COWFORK
;
4578 if (!xfs_iext_peek_prev_extent(ifp
, &bma
.icur
, &bma
.prev
))
4579 bma
.prev
.br_startoff
= NULLFILEOFF
;
4581 error
= xfs_bmapi_allocate(&bma
);
4586 if (WARN_ON_ONCE(bma
.blkno
== NULLFSBLOCK
))
4588 error
= -EFSCORRUPTED
;
4589 if (WARN_ON_ONCE(!xfs_valid_startblock(ip
, bma
.got
.br_startblock
)))
4592 XFS_STATS_ADD(mp
, xs_xstrat_bytes
, XFS_FSB_TO_B(mp
, bma
.length
));
4593 XFS_STATS_INC(mp
, xs_xstrat_quick
);
4595 ASSERT(!isnullstartblock(bma
.got
.br_startblock
));
4596 xfs_bmbt_to_iomap(ip
, iomap
, &bma
.got
, 0, flags
,
4597 xfs_iomap_inode_sequence(ip
, flags
));
4598 *seq
= READ_ONCE(ifp
->if_seq
);
4600 if (whichfork
== XFS_COW_FORK
)
4601 xfs_refcount_alloc_cow_extent(tp
, bma
.blkno
, bma
.length
);
4603 error
= xfs_bmap_btree_to_extents(tp
, ip
, bma
.cur
, &bma
.logflags
,
4608 xfs_bmapi_finish(&bma
, whichfork
, 0);
4609 error
= xfs_trans_commit(tp
);
4610 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
4614 xfs_bmapi_finish(&bma
, whichfork
, error
);
4616 xfs_trans_cancel(tp
);
4617 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
4623 struct xfs_trans
*tp
,
4624 struct xfs_inode
*ip
,
4627 xfs_fsblock_t startblock
,
4630 struct xfs_mount
*mp
= ip
->i_mount
;
4631 struct xfs_ifork
*ifp
;
4632 struct xfs_btree_cur
*cur
= NULL
;
4633 struct xfs_bmbt_irec got
;
4634 struct xfs_iext_cursor icur
;
4635 int whichfork
= xfs_bmapi_whichfork(flags
);
4636 int logflags
= 0, error
;
4638 ifp
= xfs_ifork_ptr(ip
, whichfork
);
4640 ASSERT(len
<= (xfs_filblks_t
)XFS_MAX_BMBT_EXTLEN
);
4641 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
4642 ASSERT(!(flags
& ~(XFS_BMAPI_ATTRFORK
| XFS_BMAPI_PREALLOC
|
4643 XFS_BMAPI_NORMAP
)));
4644 ASSERT((flags
& (XFS_BMAPI_ATTRFORK
| XFS_BMAPI_PREALLOC
)) !=
4645 (XFS_BMAPI_ATTRFORK
| XFS_BMAPI_PREALLOC
));
4647 if (XFS_IS_CORRUPT(mp
, !xfs_ifork_has_extents(ifp
)) ||
4648 XFS_TEST_ERROR(false, mp
, XFS_ERRTAG_BMAPIFORMAT
)) {
4649 return -EFSCORRUPTED
;
4652 if (xfs_is_shutdown(mp
))
4655 error
= xfs_iread_extents(tp
, ip
, whichfork
);
4659 if (xfs_iext_lookup_extent(ip
, ifp
, bno
, &icur
, &got
)) {
4660 /* make sure we only reflink into a hole. */
4661 ASSERT(got
.br_startoff
> bno
);
4662 ASSERT(got
.br_startoff
- bno
>= len
);
4665 ip
->i_nblocks
+= len
;
4666 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
4668 if (ifp
->if_format
== XFS_DINODE_FMT_BTREE
) {
4669 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
4670 cur
->bc_ino
.flags
= 0;
4673 got
.br_startoff
= bno
;
4674 got
.br_startblock
= startblock
;
4675 got
.br_blockcount
= len
;
4676 if (flags
& XFS_BMAPI_PREALLOC
)
4677 got
.br_state
= XFS_EXT_UNWRITTEN
;
4679 got
.br_state
= XFS_EXT_NORM
;
4681 error
= xfs_bmap_add_extent_hole_real(tp
, ip
, whichfork
, &icur
,
4682 &cur
, &got
, &logflags
, flags
);
4686 error
= xfs_bmap_btree_to_extents(tp
, ip
, cur
, &logflags
, whichfork
);
4689 if (ip
->i_df
.if_format
!= XFS_DINODE_FMT_EXTENTS
)
4690 logflags
&= ~XFS_ILOG_DEXT
;
4691 else if (ip
->i_df
.if_format
!= XFS_DINODE_FMT_BTREE
)
4692 logflags
&= ~XFS_ILOG_DBROOT
;
4695 xfs_trans_log_inode(tp
, ip
, logflags
);
4697 xfs_btree_del_cursor(cur
, error
);
4702 * When a delalloc extent is split (e.g., due to a hole punch), the original
4703 * indlen reservation must be shared across the two new extents that are left
4706 * Given the original reservation and the worst case indlen for the two new
4707 * extents (as calculated by xfs_bmap_worst_indlen()), split the original
4708 * reservation fairly across the two new extents. If necessary, steal available
4709 * blocks from a deleted extent to make up a reservation deficiency (e.g., if
4710 * ores == 1). The number of stolen blocks is returned. The availability and
4711 * subsequent accounting of stolen blocks is the responsibility of the caller.
4713 static xfs_filblks_t
4714 xfs_bmap_split_indlen(
4715 xfs_filblks_t ores
, /* original res. */
4716 xfs_filblks_t
*indlen1
, /* ext1 worst indlen */
4717 xfs_filblks_t
*indlen2
, /* ext2 worst indlen */
4718 xfs_filblks_t avail
) /* stealable blocks */
4720 xfs_filblks_t len1
= *indlen1
;
4721 xfs_filblks_t len2
= *indlen2
;
4722 xfs_filblks_t nres
= len1
+ len2
; /* new total res. */
4723 xfs_filblks_t stolen
= 0;
4724 xfs_filblks_t resfactor
;
4727 * Steal as many blocks as we can to try and satisfy the worst case
4728 * indlen for both new extents.
4730 if (ores
< nres
&& avail
)
4731 stolen
= XFS_FILBLKS_MIN(nres
- ores
, avail
);
4734 /* nothing else to do if we've satisfied the new reservation */
4739 * We can't meet the total required reservation for the two extents.
4740 * Calculate the percent of the overall shortage between both extents
4741 * and apply this percentage to each of the requested indlen values.
4742 * This distributes the shortage fairly and reduces the chances that one
4743 * of the two extents is left with nothing when extents are repeatedly
4746 resfactor
= (ores
* 100);
4747 do_div(resfactor
, nres
);
4752 ASSERT(len1
+ len2
<= ores
);
4753 ASSERT(len1
< *indlen1
&& len2
< *indlen2
);
4756 * Hand out the remainder to each extent. If one of the two reservations
4757 * is zero, we want to make sure that one gets a block first. The loop
4758 * below starts with len1, so hand len2 a block right off the bat if it
4761 ores
-= (len1
+ len2
);
4762 ASSERT((*indlen1
- len1
) + (*indlen2
- len2
) >= ores
);
4763 if (ores
&& !len2
&& *indlen2
) {
4768 if (len1
< *indlen1
) {
4774 if (len2
< *indlen2
) {
4787 xfs_bmap_del_extent_delay(
4788 struct xfs_inode
*ip
,
4790 struct xfs_iext_cursor
*icur
,
4791 struct xfs_bmbt_irec
*got
,
4792 struct xfs_bmbt_irec
*del
)
4794 struct xfs_mount
*mp
= ip
->i_mount
;
4795 struct xfs_ifork
*ifp
= xfs_ifork_ptr(ip
, whichfork
);
4796 struct xfs_bmbt_irec
new;
4797 int64_t da_old
, da_new
, da_diff
= 0;
4798 xfs_fileoff_t del_endoff
, got_endoff
;
4799 xfs_filblks_t got_indlen
, new_indlen
, stolen
;
4800 uint32_t state
= xfs_bmap_fork_to_state(whichfork
);
4804 XFS_STATS_INC(mp
, xs_del_exlist
);
4806 isrt
= (whichfork
== XFS_DATA_FORK
) && XFS_IS_REALTIME_INODE(ip
);
4807 del_endoff
= del
->br_startoff
+ del
->br_blockcount
;
4808 got_endoff
= got
->br_startoff
+ got
->br_blockcount
;
4809 da_old
= startblockval(got
->br_startblock
);
4812 ASSERT(del
->br_blockcount
> 0);
4813 ASSERT(got
->br_startoff
<= del
->br_startoff
);
4814 ASSERT(got_endoff
>= del_endoff
);
4817 uint64_t rtexts
= XFS_FSB_TO_B(mp
, del
->br_blockcount
);
4819 do_div(rtexts
, mp
->m_sb
.sb_rextsize
);
4820 xfs_mod_frextents(mp
, rtexts
);
4824 * Update the inode delalloc counter now and wait to update the
4825 * sb counters as we might have to borrow some blocks for the
4826 * indirect block accounting.
4829 error
= xfs_quota_unreserve_blkres(ip
, del
->br_blockcount
);
4832 ip
->i_delayed_blks
-= del
->br_blockcount
;
4834 if (got
->br_startoff
== del
->br_startoff
)
4835 state
|= BMAP_LEFT_FILLING
;
4836 if (got_endoff
== del_endoff
)
4837 state
|= BMAP_RIGHT_FILLING
;
4839 switch (state
& (BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
)) {
4840 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
4842 * Matches the whole extent. Delete the entry.
4844 xfs_iext_remove(ip
, icur
, state
);
4845 xfs_iext_prev(ifp
, icur
);
4847 case BMAP_LEFT_FILLING
:
4849 * Deleting the first part of the extent.
4851 got
->br_startoff
= del_endoff
;
4852 got
->br_blockcount
-= del
->br_blockcount
;
4853 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
,
4854 got
->br_blockcount
), da_old
);
4855 got
->br_startblock
= nullstartblock((int)da_new
);
4856 xfs_iext_update_extent(ip
, state
, icur
, got
);
4858 case BMAP_RIGHT_FILLING
:
4860 * Deleting the last part of the extent.
4862 got
->br_blockcount
= got
->br_blockcount
- del
->br_blockcount
;
4863 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
,
4864 got
->br_blockcount
), da_old
);
4865 got
->br_startblock
= nullstartblock((int)da_new
);
4866 xfs_iext_update_extent(ip
, state
, icur
, got
);
4870 * Deleting the middle of the extent.
4872 * Distribute the original indlen reservation across the two new
4873 * extents. Steal blocks from the deleted extent if necessary.
4874 * Stealing blocks simply fudges the fdblocks accounting below.
4875 * Warn if either of the new indlen reservations is zero as this
4876 * can lead to delalloc problems.
4878 got
->br_blockcount
= del
->br_startoff
- got
->br_startoff
;
4879 got_indlen
= xfs_bmap_worst_indlen(ip
, got
->br_blockcount
);
4881 new.br_blockcount
= got_endoff
- del_endoff
;
4882 new_indlen
= xfs_bmap_worst_indlen(ip
, new.br_blockcount
);
4884 WARN_ON_ONCE(!got_indlen
|| !new_indlen
);
4885 stolen
= xfs_bmap_split_indlen(da_old
, &got_indlen
, &new_indlen
,
4886 del
->br_blockcount
);
4888 got
->br_startblock
= nullstartblock((int)got_indlen
);
4890 new.br_startoff
= del_endoff
;
4891 new.br_state
= got
->br_state
;
4892 new.br_startblock
= nullstartblock((int)new_indlen
);
4894 xfs_iext_update_extent(ip
, state
, icur
, got
);
4895 xfs_iext_next(ifp
, icur
);
4896 xfs_iext_insert(ip
, icur
, &new, state
);
4898 da_new
= got_indlen
+ new_indlen
- stolen
;
4899 del
->br_blockcount
-= stolen
;
4903 ASSERT(da_old
>= da_new
);
4904 da_diff
= da_old
- da_new
;
4906 da_diff
+= del
->br_blockcount
;
4908 xfs_mod_fdblocks(mp
, da_diff
, false);
4909 xfs_mod_delalloc(mp
, -da_diff
);
4915 xfs_bmap_del_extent_cow(
4916 struct xfs_inode
*ip
,
4917 struct xfs_iext_cursor
*icur
,
4918 struct xfs_bmbt_irec
*got
,
4919 struct xfs_bmbt_irec
*del
)
4921 struct xfs_mount
*mp
= ip
->i_mount
;
4922 struct xfs_ifork
*ifp
= xfs_ifork_ptr(ip
, XFS_COW_FORK
);
4923 struct xfs_bmbt_irec
new;
4924 xfs_fileoff_t del_endoff
, got_endoff
;
4925 uint32_t state
= BMAP_COWFORK
;
4927 XFS_STATS_INC(mp
, xs_del_exlist
);
4929 del_endoff
= del
->br_startoff
+ del
->br_blockcount
;
4930 got_endoff
= got
->br_startoff
+ got
->br_blockcount
;
4932 ASSERT(del
->br_blockcount
> 0);
4933 ASSERT(got
->br_startoff
<= del
->br_startoff
);
4934 ASSERT(got_endoff
>= del_endoff
);
4935 ASSERT(!isnullstartblock(got
->br_startblock
));
4937 if (got
->br_startoff
== del
->br_startoff
)
4938 state
|= BMAP_LEFT_FILLING
;
4939 if (got_endoff
== del_endoff
)
4940 state
|= BMAP_RIGHT_FILLING
;
4942 switch (state
& (BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
)) {
4943 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
4945 * Matches the whole extent. Delete the entry.
4947 xfs_iext_remove(ip
, icur
, state
);
4948 xfs_iext_prev(ifp
, icur
);
4950 case BMAP_LEFT_FILLING
:
4952 * Deleting the first part of the extent.
4954 got
->br_startoff
= del_endoff
;
4955 got
->br_blockcount
-= del
->br_blockcount
;
4956 got
->br_startblock
= del
->br_startblock
+ del
->br_blockcount
;
4957 xfs_iext_update_extent(ip
, state
, icur
, got
);
4959 case BMAP_RIGHT_FILLING
:
4961 * Deleting the last part of the extent.
4963 got
->br_blockcount
-= del
->br_blockcount
;
4964 xfs_iext_update_extent(ip
, state
, icur
, got
);
4968 * Deleting the middle of the extent.
4970 got
->br_blockcount
= del
->br_startoff
- got
->br_startoff
;
4972 new.br_startoff
= del_endoff
;
4973 new.br_blockcount
= got_endoff
- del_endoff
;
4974 new.br_state
= got
->br_state
;
4975 new.br_startblock
= del
->br_startblock
+ del
->br_blockcount
;
4977 xfs_iext_update_extent(ip
, state
, icur
, got
);
4978 xfs_iext_next(ifp
, icur
);
4979 xfs_iext_insert(ip
, icur
, &new, state
);
4982 ip
->i_delayed_blks
-= del
->br_blockcount
;
4986 * Called by xfs_bmapi to update file extent records and the btree
4987 * after removing space.
4989 STATIC
int /* error */
4990 xfs_bmap_del_extent_real(
4991 xfs_inode_t
*ip
, /* incore inode pointer */
4992 xfs_trans_t
*tp
, /* current transaction pointer */
4993 struct xfs_iext_cursor
*icur
,
4994 struct xfs_btree_cur
*cur
, /* if null, not a btree */
4995 xfs_bmbt_irec_t
*del
, /* data to remove from extents */
4996 int *logflagsp
, /* inode logging flags */
4997 int whichfork
, /* data or attr fork */
4998 uint32_t bflags
) /* bmapi flags */
5000 xfs_fsblock_t del_endblock
=0; /* first block past del */
5001 xfs_fileoff_t del_endoff
; /* first offset past del */
5002 int do_fx
; /* free extent at end of routine */
5003 int error
; /* error return value */
5004 int flags
= 0;/* inode logging flags */
5005 struct xfs_bmbt_irec got
; /* current extent entry */
5006 xfs_fileoff_t got_endoff
; /* first offset past got */
5007 int i
; /* temp state */
5008 struct xfs_ifork
*ifp
; /* inode fork pointer */
5009 xfs_mount_t
*mp
; /* mount structure */
5010 xfs_filblks_t nblks
; /* quota/sb block count */
5011 xfs_bmbt_irec_t
new; /* new record to be inserted */
5013 uint qfield
; /* quota field to update */
5014 uint32_t state
= xfs_bmap_fork_to_state(whichfork
);
5015 struct xfs_bmbt_irec old
;
5018 XFS_STATS_INC(mp
, xs_del_exlist
);
5020 ifp
= xfs_ifork_ptr(ip
, whichfork
);
5021 ASSERT(del
->br_blockcount
> 0);
5022 xfs_iext_get_extent(ifp
, icur
, &got
);
5023 ASSERT(got
.br_startoff
<= del
->br_startoff
);
5024 del_endoff
= del
->br_startoff
+ del
->br_blockcount
;
5025 got_endoff
= got
.br_startoff
+ got
.br_blockcount
;
5026 ASSERT(got_endoff
>= del_endoff
);
5027 ASSERT(!isnullstartblock(got
.br_startblock
));
5032 * If it's the case where the directory code is running with no block
5033 * reservation, and the deleted block is in the middle of its extent,
5034 * and the resulting insert of an extent would cause transformation to
5035 * btree format, then reject it. The calling code will then swap blocks
5036 * around instead. We have to do this now, rather than waiting for the
5037 * conversion to btree format, since the transaction will be dirty then.
5039 if (tp
->t_blk_res
== 0 &&
5040 ifp
->if_format
== XFS_DINODE_FMT_EXTENTS
&&
5041 ifp
->if_nextents
>= XFS_IFORK_MAXEXT(ip
, whichfork
) &&
5042 del
->br_startoff
> got
.br_startoff
&& del_endoff
< got_endoff
)
5045 flags
= XFS_ILOG_CORE
;
5046 if (whichfork
== XFS_DATA_FORK
&& XFS_IS_REALTIME_INODE(ip
)) {
5050 len
= div_u64_rem(del
->br_blockcount
, mp
->m_sb
.sb_rextsize
,
5054 if (!(bflags
& XFS_BMAPI_REMAP
)) {
5057 bno
= div_u64_rem(del
->br_startblock
,
5058 mp
->m_sb
.sb_rextsize
, &mod
);
5061 error
= xfs_rtfree_extent(tp
, bno
, (xfs_extlen_t
)len
);
5067 nblks
= len
* mp
->m_sb
.sb_rextsize
;
5068 qfield
= XFS_TRANS_DQ_RTBCOUNT
;
5071 nblks
= del
->br_blockcount
;
5072 qfield
= XFS_TRANS_DQ_BCOUNT
;
5075 del_endblock
= del
->br_startblock
+ del
->br_blockcount
;
5077 error
= xfs_bmbt_lookup_eq(cur
, &got
, &i
);
5080 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
5081 error
= -EFSCORRUPTED
;
5086 if (got
.br_startoff
== del
->br_startoff
)
5087 state
|= BMAP_LEFT_FILLING
;
5088 if (got_endoff
== del_endoff
)
5089 state
|= BMAP_RIGHT_FILLING
;
5091 switch (state
& (BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
)) {
5092 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
5094 * Matches the whole extent. Delete the entry.
5096 xfs_iext_remove(ip
, icur
, state
);
5097 xfs_iext_prev(ifp
, icur
);
5100 flags
|= XFS_ILOG_CORE
;
5102 flags
|= xfs_ilog_fext(whichfork
);
5105 if ((error
= xfs_btree_delete(cur
, &i
)))
5107 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
5108 error
= -EFSCORRUPTED
;
5112 case BMAP_LEFT_FILLING
:
5114 * Deleting the first part of the extent.
5116 got
.br_startoff
= del_endoff
;
5117 got
.br_startblock
= del_endblock
;
5118 got
.br_blockcount
-= del
->br_blockcount
;
5119 xfs_iext_update_extent(ip
, state
, icur
, &got
);
5121 flags
|= xfs_ilog_fext(whichfork
);
5124 error
= xfs_bmbt_update(cur
, &got
);
5128 case BMAP_RIGHT_FILLING
:
5130 * Deleting the last part of the extent.
5132 got
.br_blockcount
-= del
->br_blockcount
;
5133 xfs_iext_update_extent(ip
, state
, icur
, &got
);
5135 flags
|= xfs_ilog_fext(whichfork
);
5138 error
= xfs_bmbt_update(cur
, &got
);
5144 * Deleting the middle of the extent.
5149 got
.br_blockcount
= del
->br_startoff
- got
.br_startoff
;
5150 xfs_iext_update_extent(ip
, state
, icur
, &got
);
5152 new.br_startoff
= del_endoff
;
5153 new.br_blockcount
= got_endoff
- del_endoff
;
5154 new.br_state
= got
.br_state
;
5155 new.br_startblock
= del_endblock
;
5157 flags
|= XFS_ILOG_CORE
;
5159 error
= xfs_bmbt_update(cur
, &got
);
5162 error
= xfs_btree_increment(cur
, 0, &i
);
5165 cur
->bc_rec
.b
= new;
5166 error
= xfs_btree_insert(cur
, &i
);
5167 if (error
&& error
!= -ENOSPC
)
5170 * If get no-space back from btree insert, it tried a
5171 * split, and we have a zero block reservation. Fix up
5172 * our state and return the error.
5174 if (error
== -ENOSPC
) {
5176 * Reset the cursor, don't trust it after any
5179 error
= xfs_bmbt_lookup_eq(cur
, &got
, &i
);
5182 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
5183 error
= -EFSCORRUPTED
;
5187 * Update the btree record back
5188 * to the original value.
5190 error
= xfs_bmbt_update(cur
, &old
);
5194 * Reset the extent record back
5195 * to the original value.
5197 xfs_iext_update_extent(ip
, state
, icur
, &old
);
5202 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
5203 error
= -EFSCORRUPTED
;
5207 flags
|= xfs_ilog_fext(whichfork
);
5210 xfs_iext_next(ifp
, icur
);
5211 xfs_iext_insert(ip
, icur
, &new, state
);
5215 /* remove reverse mapping */
5216 xfs_rmap_unmap_extent(tp
, ip
, whichfork
, del
);
5219 * If we need to, add to list of extents to delete.
5221 if (do_fx
&& !(bflags
& XFS_BMAPI_REMAP
)) {
5222 if (xfs_is_reflink_inode(ip
) && whichfork
== XFS_DATA_FORK
) {
5223 xfs_refcount_decrease_extent(tp
, del
);
5225 __xfs_free_extent_later(tp
, del
->br_startblock
,
5226 del
->br_blockcount
, NULL
,
5227 (bflags
& XFS_BMAPI_NODISCARD
) ||
5228 del
->br_state
== XFS_EXT_UNWRITTEN
);
5233 * Adjust inode # blocks in the file.
5236 ip
->i_nblocks
-= nblks
;
5238 * Adjust quota data.
5240 if (qfield
&& !(bflags
& XFS_BMAPI_REMAP
))
5241 xfs_trans_mod_dquot_byino(tp
, ip
, qfield
, (long)-nblks
);
5249 * Unmap (remove) blocks from a file.
5250 * If nexts is nonzero then the number of extents to remove is limited to
5251 * that value. If not all extents in the block range can be removed then
5256 struct xfs_trans
*tp
, /* transaction pointer */
5257 struct xfs_inode
*ip
, /* incore inode */
5258 xfs_fileoff_t start
, /* first file offset deleted */
5259 xfs_filblks_t
*rlen
, /* i/o: amount remaining */
5260 uint32_t flags
, /* misc flags */
5261 xfs_extnum_t nexts
) /* number of extents max */
5263 struct xfs_btree_cur
*cur
; /* bmap btree cursor */
5264 struct xfs_bmbt_irec del
; /* extent being deleted */
5265 int error
; /* error return value */
5266 xfs_extnum_t extno
; /* extent number in list */
5267 struct xfs_bmbt_irec got
; /* current extent record */
5268 struct xfs_ifork
*ifp
; /* inode fork pointer */
5269 int isrt
; /* freeing in rt area */
5270 int logflags
; /* transaction logging flags */
5271 xfs_extlen_t mod
; /* rt extent offset */
5272 struct xfs_mount
*mp
= ip
->i_mount
;
5273 int tmp_logflags
; /* partial logging flags */
5274 int wasdel
; /* was a delayed alloc extent */
5275 int whichfork
; /* data or attribute fork */
5277 xfs_filblks_t len
= *rlen
; /* length to unmap in file */
5279 struct xfs_iext_cursor icur
;
5282 trace_xfs_bunmap(ip
, start
, len
, flags
, _RET_IP_
);
5284 whichfork
= xfs_bmapi_whichfork(flags
);
5285 ASSERT(whichfork
!= XFS_COW_FORK
);
5286 ifp
= xfs_ifork_ptr(ip
, whichfork
);
5287 if (XFS_IS_CORRUPT(mp
, !xfs_ifork_has_extents(ifp
)))
5288 return -EFSCORRUPTED
;
5289 if (xfs_is_shutdown(mp
))
5292 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
5296 error
= xfs_iread_extents(tp
, ip
, whichfork
);
5300 if (xfs_iext_count(ifp
) == 0) {
5304 XFS_STATS_INC(mp
, xs_blk_unmap
);
5305 isrt
= (whichfork
== XFS_DATA_FORK
) && XFS_IS_REALTIME_INODE(ip
);
5308 if (!xfs_iext_lookup_extent_before(ip
, ifp
, &end
, &icur
, &got
)) {
5315 if (ifp
->if_format
== XFS_DINODE_FMT_BTREE
) {
5316 ASSERT(ifp
->if_format
== XFS_DINODE_FMT_BTREE
);
5317 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
5318 cur
->bc_ino
.flags
= 0;
5324 * Synchronize by locking the bitmap inode.
5326 xfs_ilock(mp
->m_rbmip
, XFS_ILOCK_EXCL
|XFS_ILOCK_RTBITMAP
);
5327 xfs_trans_ijoin(tp
, mp
->m_rbmip
, XFS_ILOCK_EXCL
);
5328 xfs_ilock(mp
->m_rsumip
, XFS_ILOCK_EXCL
|XFS_ILOCK_RTSUM
);
5329 xfs_trans_ijoin(tp
, mp
->m_rsumip
, XFS_ILOCK_EXCL
);
5333 while (end
!= (xfs_fileoff_t
)-1 && end
>= start
&&
5334 (nexts
== 0 || extno
< nexts
)) {
5336 * Is the found extent after a hole in which end lives?
5337 * Just back up to the previous extent, if so.
5339 if (got
.br_startoff
> end
&&
5340 !xfs_iext_prev_extent(ifp
, &icur
, &got
)) {
5345 * Is the last block of this extent before the range
5346 * we're supposed to delete? If so, we're done.
5348 end
= XFS_FILEOFF_MIN(end
,
5349 got
.br_startoff
+ got
.br_blockcount
- 1);
5353 * Then deal with the (possibly delayed) allocated space
5357 wasdel
= isnullstartblock(del
.br_startblock
);
5359 if (got
.br_startoff
< start
) {
5360 del
.br_startoff
= start
;
5361 del
.br_blockcount
-= start
- got
.br_startoff
;
5363 del
.br_startblock
+= start
- got
.br_startoff
;
5365 if (del
.br_startoff
+ del
.br_blockcount
> end
+ 1)
5366 del
.br_blockcount
= end
+ 1 - del
.br_startoff
;
5371 sum
= del
.br_startblock
+ del
.br_blockcount
;
5372 div_u64_rem(sum
, mp
->m_sb
.sb_rextsize
, &mod
);
5375 * Realtime extent not lined up at the end.
5376 * The extent could have been split into written
5377 * and unwritten pieces, or we could just be
5378 * unmapping part of it. But we can't really
5379 * get rid of part of a realtime extent.
5381 if (del
.br_state
== XFS_EXT_UNWRITTEN
) {
5383 * This piece is unwritten, or we're not
5384 * using unwritten extents. Skip over it.
5387 end
-= mod
> del
.br_blockcount
?
5388 del
.br_blockcount
: mod
;
5389 if (end
< got
.br_startoff
&&
5390 !xfs_iext_prev_extent(ifp
, &icur
, &got
)) {
5397 * It's written, turn it unwritten.
5398 * This is better than zeroing it.
5400 ASSERT(del
.br_state
== XFS_EXT_NORM
);
5401 ASSERT(tp
->t_blk_res
> 0);
5403 * If this spans a realtime extent boundary,
5404 * chop it back to the start of the one we end at.
5406 if (del
.br_blockcount
> mod
) {
5407 del
.br_startoff
+= del
.br_blockcount
- mod
;
5408 del
.br_startblock
+= del
.br_blockcount
- mod
;
5409 del
.br_blockcount
= mod
;
5411 del
.br_state
= XFS_EXT_UNWRITTEN
;
5412 error
= xfs_bmap_add_extent_unwritten_real(tp
, ip
,
5413 whichfork
, &icur
, &cur
, &del
,
5419 div_u64_rem(del
.br_startblock
, mp
->m_sb
.sb_rextsize
, &mod
);
5421 xfs_extlen_t off
= mp
->m_sb
.sb_rextsize
- mod
;
5424 * Realtime extent is lined up at the end but not
5425 * at the front. We'll get rid of full extents if
5428 if (del
.br_blockcount
> off
) {
5429 del
.br_blockcount
-= off
;
5430 del
.br_startoff
+= off
;
5431 del
.br_startblock
+= off
;
5432 } else if (del
.br_startoff
== start
&&
5433 (del
.br_state
== XFS_EXT_UNWRITTEN
||
5434 tp
->t_blk_res
== 0)) {
5436 * Can't make it unwritten. There isn't
5437 * a full extent here so just skip it.
5439 ASSERT(end
>= del
.br_blockcount
);
5440 end
-= del
.br_blockcount
;
5441 if (got
.br_startoff
> end
&&
5442 !xfs_iext_prev_extent(ifp
, &icur
, &got
)) {
5447 } else if (del
.br_state
== XFS_EXT_UNWRITTEN
) {
5448 struct xfs_bmbt_irec prev
;
5449 xfs_fileoff_t unwrite_start
;
5452 * This one is already unwritten.
5453 * It must have a written left neighbor.
5454 * Unwrite the killed part of that one and
5457 if (!xfs_iext_prev_extent(ifp
, &icur
, &prev
))
5459 ASSERT(prev
.br_state
== XFS_EXT_NORM
);
5460 ASSERT(!isnullstartblock(prev
.br_startblock
));
5461 ASSERT(del
.br_startblock
==
5462 prev
.br_startblock
+ prev
.br_blockcount
);
5463 unwrite_start
= max3(start
,
5464 del
.br_startoff
- mod
,
5466 mod
= unwrite_start
- prev
.br_startoff
;
5467 prev
.br_startoff
= unwrite_start
;
5468 prev
.br_startblock
+= mod
;
5469 prev
.br_blockcount
-= mod
;
5470 prev
.br_state
= XFS_EXT_UNWRITTEN
;
5471 error
= xfs_bmap_add_extent_unwritten_real(tp
,
5472 ip
, whichfork
, &icur
, &cur
,
5478 ASSERT(del
.br_state
== XFS_EXT_NORM
);
5479 del
.br_state
= XFS_EXT_UNWRITTEN
;
5480 error
= xfs_bmap_add_extent_unwritten_real(tp
,
5481 ip
, whichfork
, &icur
, &cur
,
5491 error
= xfs_bmap_del_extent_delay(ip
, whichfork
, &icur
,
5494 error
= xfs_bmap_del_extent_real(ip
, tp
, &icur
, cur
,
5495 &del
, &tmp_logflags
, whichfork
,
5497 logflags
|= tmp_logflags
;
5503 end
= del
.br_startoff
- 1;
5506 * If not done go on to the next (previous) record.
5508 if (end
!= (xfs_fileoff_t
)-1 && end
>= start
) {
5509 if (!xfs_iext_get_extent(ifp
, &icur
, &got
) ||
5510 (got
.br_startoff
> end
&&
5511 !xfs_iext_prev_extent(ifp
, &icur
, &got
))) {
5518 if (done
|| end
== (xfs_fileoff_t
)-1 || end
< start
)
5521 *rlen
= end
- start
+ 1;
5524 * Convert to a btree if necessary.
5526 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
5527 ASSERT(cur
== NULL
);
5528 error
= xfs_bmap_extents_to_btree(tp
, ip
, &cur
, 0,
5529 &tmp_logflags
, whichfork
);
5530 logflags
|= tmp_logflags
;
5532 error
= xfs_bmap_btree_to_extents(tp
, ip
, cur
, &logflags
,
5538 * Log everything. Do this after conversion, there's no point in
5539 * logging the extent records if we've converted to btree format.
5541 if ((logflags
& xfs_ilog_fext(whichfork
)) &&
5542 ifp
->if_format
!= XFS_DINODE_FMT_EXTENTS
)
5543 logflags
&= ~xfs_ilog_fext(whichfork
);
5544 else if ((logflags
& xfs_ilog_fbroot(whichfork
)) &&
5545 ifp
->if_format
!= XFS_DINODE_FMT_BTREE
)
5546 logflags
&= ~xfs_ilog_fbroot(whichfork
);
5548 * Log inode even in the error case, if the transaction
5549 * is dirty we'll need to shut down the filesystem.
5552 xfs_trans_log_inode(tp
, ip
, logflags
);
5555 cur
->bc_ino
.allocated
= 0;
5556 xfs_btree_del_cursor(cur
, error
);
5561 /* Unmap a range of a file. */
5565 struct xfs_inode
*ip
,
5574 error
= __xfs_bunmapi(tp
, ip
, bno
, &len
, flags
, nexts
);
5580 * Determine whether an extent shift can be accomplished by a merge with the
5581 * extent that precedes the target hole of the shift.
5585 struct xfs_bmbt_irec
*left
, /* preceding extent */
5586 struct xfs_bmbt_irec
*got
, /* current extent to shift */
5587 xfs_fileoff_t shift
) /* shift fsb */
5589 xfs_fileoff_t startoff
;
5591 startoff
= got
->br_startoff
- shift
;
5594 * The extent, once shifted, must be adjacent in-file and on-disk with
5595 * the preceding extent.
5597 if ((left
->br_startoff
+ left
->br_blockcount
!= startoff
) ||
5598 (left
->br_startblock
+ left
->br_blockcount
!= got
->br_startblock
) ||
5599 (left
->br_state
!= got
->br_state
) ||
5600 (left
->br_blockcount
+ got
->br_blockcount
> XFS_MAX_BMBT_EXTLEN
))
5607 * A bmap extent shift adjusts the file offset of an extent to fill a preceding
5608 * hole in the file. If an extent shift would result in the extent being fully
5609 * adjacent to the extent that currently precedes the hole, we can merge with
5610 * the preceding extent rather than do the shift.
5612 * This function assumes the caller has verified a shift-by-merge is possible
5613 * with the provided extents via xfs_bmse_can_merge().
5617 struct xfs_trans
*tp
,
5618 struct xfs_inode
*ip
,
5620 xfs_fileoff_t shift
, /* shift fsb */
5621 struct xfs_iext_cursor
*icur
,
5622 struct xfs_bmbt_irec
*got
, /* extent to shift */
5623 struct xfs_bmbt_irec
*left
, /* preceding extent */
5624 struct xfs_btree_cur
*cur
,
5625 int *logflags
) /* output */
5627 struct xfs_ifork
*ifp
= xfs_ifork_ptr(ip
, whichfork
);
5628 struct xfs_bmbt_irec
new;
5629 xfs_filblks_t blockcount
;
5631 struct xfs_mount
*mp
= ip
->i_mount
;
5633 blockcount
= left
->br_blockcount
+ got
->br_blockcount
;
5635 ASSERT(xfs_isilocked(ip
, XFS_IOLOCK_EXCL
));
5636 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
5637 ASSERT(xfs_bmse_can_merge(left
, got
, shift
));
5640 new.br_blockcount
= blockcount
;
5643 * Update the on-disk extent count, the btree if necessary and log the
5647 *logflags
|= XFS_ILOG_CORE
;
5649 *logflags
|= XFS_ILOG_DEXT
;
5653 /* lookup and remove the extent to merge */
5654 error
= xfs_bmbt_lookup_eq(cur
, got
, &i
);
5657 if (XFS_IS_CORRUPT(mp
, i
!= 1))
5658 return -EFSCORRUPTED
;
5660 error
= xfs_btree_delete(cur
, &i
);
5663 if (XFS_IS_CORRUPT(mp
, i
!= 1))
5664 return -EFSCORRUPTED
;
5666 /* lookup and update size of the previous extent */
5667 error
= xfs_bmbt_lookup_eq(cur
, left
, &i
);
5670 if (XFS_IS_CORRUPT(mp
, i
!= 1))
5671 return -EFSCORRUPTED
;
5673 error
= xfs_bmbt_update(cur
, &new);
5677 /* change to extent format if required after extent removal */
5678 error
= xfs_bmap_btree_to_extents(tp
, ip
, cur
, logflags
, whichfork
);
5683 xfs_iext_remove(ip
, icur
, 0);
5684 xfs_iext_prev(ifp
, icur
);
5685 xfs_iext_update_extent(ip
, xfs_bmap_fork_to_state(whichfork
), icur
,
5688 /* update reverse mapping. rmap functions merge the rmaps for us */
5689 xfs_rmap_unmap_extent(tp
, ip
, whichfork
, got
);
5690 memcpy(&new, got
, sizeof(new));
5691 new.br_startoff
= left
->br_startoff
+ left
->br_blockcount
;
5692 xfs_rmap_map_extent(tp
, ip
, whichfork
, &new);
5697 xfs_bmap_shift_update_extent(
5698 struct xfs_trans
*tp
,
5699 struct xfs_inode
*ip
,
5701 struct xfs_iext_cursor
*icur
,
5702 struct xfs_bmbt_irec
*got
,
5703 struct xfs_btree_cur
*cur
,
5705 xfs_fileoff_t startoff
)
5707 struct xfs_mount
*mp
= ip
->i_mount
;
5708 struct xfs_bmbt_irec prev
= *got
;
5711 *logflags
|= XFS_ILOG_CORE
;
5713 got
->br_startoff
= startoff
;
5716 error
= xfs_bmbt_lookup_eq(cur
, &prev
, &i
);
5719 if (XFS_IS_CORRUPT(mp
, i
!= 1))
5720 return -EFSCORRUPTED
;
5722 error
= xfs_bmbt_update(cur
, got
);
5726 *logflags
|= XFS_ILOG_DEXT
;
5729 xfs_iext_update_extent(ip
, xfs_bmap_fork_to_state(whichfork
), icur
,
5732 /* update reverse mapping */
5733 xfs_rmap_unmap_extent(tp
, ip
, whichfork
, &prev
);
5734 xfs_rmap_map_extent(tp
, ip
, whichfork
, got
);
5739 xfs_bmap_collapse_extents(
5740 struct xfs_trans
*tp
,
5741 struct xfs_inode
*ip
,
5742 xfs_fileoff_t
*next_fsb
,
5743 xfs_fileoff_t offset_shift_fsb
,
5746 int whichfork
= XFS_DATA_FORK
;
5747 struct xfs_mount
*mp
= ip
->i_mount
;
5748 struct xfs_ifork
*ifp
= xfs_ifork_ptr(ip
, whichfork
);
5749 struct xfs_btree_cur
*cur
= NULL
;
5750 struct xfs_bmbt_irec got
, prev
;
5751 struct xfs_iext_cursor icur
;
5752 xfs_fileoff_t new_startoff
;
5756 if (XFS_IS_CORRUPT(mp
, !xfs_ifork_has_extents(ifp
)) ||
5757 XFS_TEST_ERROR(false, mp
, XFS_ERRTAG_BMAPIFORMAT
)) {
5758 return -EFSCORRUPTED
;
5761 if (xfs_is_shutdown(mp
))
5764 ASSERT(xfs_isilocked(ip
, XFS_IOLOCK_EXCL
| XFS_ILOCK_EXCL
));
5766 error
= xfs_iread_extents(tp
, ip
, whichfork
);
5770 if (ifp
->if_format
== XFS_DINODE_FMT_BTREE
) {
5771 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
5772 cur
->bc_ino
.flags
= 0;
5775 if (!xfs_iext_lookup_extent(ip
, ifp
, *next_fsb
, &icur
, &got
)) {
5779 if (XFS_IS_CORRUPT(mp
, isnullstartblock(got
.br_startblock
))) {
5780 error
= -EFSCORRUPTED
;
5784 new_startoff
= got
.br_startoff
- offset_shift_fsb
;
5785 if (xfs_iext_peek_prev_extent(ifp
, &icur
, &prev
)) {
5786 if (new_startoff
< prev
.br_startoff
+ prev
.br_blockcount
) {
5791 if (xfs_bmse_can_merge(&prev
, &got
, offset_shift_fsb
)) {
5792 error
= xfs_bmse_merge(tp
, ip
, whichfork
,
5793 offset_shift_fsb
, &icur
, &got
, &prev
,
5800 if (got
.br_startoff
< offset_shift_fsb
) {
5806 error
= xfs_bmap_shift_update_extent(tp
, ip
, whichfork
, &icur
, &got
,
5807 cur
, &logflags
, new_startoff
);
5812 if (!xfs_iext_next_extent(ifp
, &icur
, &got
)) {
5817 *next_fsb
= got
.br_startoff
;
5820 xfs_btree_del_cursor(cur
, error
);
5822 xfs_trans_log_inode(tp
, ip
, logflags
);
5826 /* Make sure we won't be right-shifting an extent past the maximum bound. */
5828 xfs_bmap_can_insert_extents(
5829 struct xfs_inode
*ip
,
5831 xfs_fileoff_t shift
)
5833 struct xfs_bmbt_irec got
;
5837 ASSERT(xfs_isilocked(ip
, XFS_IOLOCK_EXCL
));
5839 if (xfs_is_shutdown(ip
->i_mount
))
5842 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
5843 error
= xfs_bmap_last_extent(NULL
, ip
, XFS_DATA_FORK
, &got
, &is_empty
);
5844 if (!error
&& !is_empty
&& got
.br_startoff
>= off
&&
5845 ((got
.br_startoff
+ shift
) & BMBT_STARTOFF_MASK
) < got
.br_startoff
)
5847 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
5853 xfs_bmap_insert_extents(
5854 struct xfs_trans
*tp
,
5855 struct xfs_inode
*ip
,
5856 xfs_fileoff_t
*next_fsb
,
5857 xfs_fileoff_t offset_shift_fsb
,
5859 xfs_fileoff_t stop_fsb
)
5861 int whichfork
= XFS_DATA_FORK
;
5862 struct xfs_mount
*mp
= ip
->i_mount
;
5863 struct xfs_ifork
*ifp
= xfs_ifork_ptr(ip
, whichfork
);
5864 struct xfs_btree_cur
*cur
= NULL
;
5865 struct xfs_bmbt_irec got
, next
;
5866 struct xfs_iext_cursor icur
;
5867 xfs_fileoff_t new_startoff
;
5871 if (XFS_IS_CORRUPT(mp
, !xfs_ifork_has_extents(ifp
)) ||
5872 XFS_TEST_ERROR(false, mp
, XFS_ERRTAG_BMAPIFORMAT
)) {
5873 return -EFSCORRUPTED
;
5876 if (xfs_is_shutdown(mp
))
5879 ASSERT(xfs_isilocked(ip
, XFS_IOLOCK_EXCL
| XFS_ILOCK_EXCL
));
5881 error
= xfs_iread_extents(tp
, ip
, whichfork
);
5885 if (ifp
->if_format
== XFS_DINODE_FMT_BTREE
) {
5886 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
5887 cur
->bc_ino
.flags
= 0;
5890 if (*next_fsb
== NULLFSBLOCK
) {
5891 xfs_iext_last(ifp
, &icur
);
5892 if (!xfs_iext_get_extent(ifp
, &icur
, &got
) ||
5893 stop_fsb
> got
.br_startoff
) {
5898 if (!xfs_iext_lookup_extent(ip
, ifp
, *next_fsb
, &icur
, &got
)) {
5903 if (XFS_IS_CORRUPT(mp
, isnullstartblock(got
.br_startblock
))) {
5904 error
= -EFSCORRUPTED
;
5908 if (XFS_IS_CORRUPT(mp
, stop_fsb
> got
.br_startoff
)) {
5909 error
= -EFSCORRUPTED
;
5913 new_startoff
= got
.br_startoff
+ offset_shift_fsb
;
5914 if (xfs_iext_peek_next_extent(ifp
, &icur
, &next
)) {
5915 if (new_startoff
+ got
.br_blockcount
> next
.br_startoff
) {
5921 * Unlike a left shift (which involves a hole punch), a right
5922 * shift does not modify extent neighbors in any way. We should
5923 * never find mergeable extents in this scenario. Check anyways
5924 * and warn if we encounter two extents that could be one.
5926 if (xfs_bmse_can_merge(&got
, &next
, offset_shift_fsb
))
5930 error
= xfs_bmap_shift_update_extent(tp
, ip
, whichfork
, &icur
, &got
,
5931 cur
, &logflags
, new_startoff
);
5935 if (!xfs_iext_prev_extent(ifp
, &icur
, &got
) ||
5936 stop_fsb
>= got
.br_startoff
+ got
.br_blockcount
) {
5941 *next_fsb
= got
.br_startoff
;
5944 xfs_btree_del_cursor(cur
, error
);
5946 xfs_trans_log_inode(tp
, ip
, logflags
);
5951 * Splits an extent into two extents at split_fsb block such that it is the
5952 * first block of the current_ext. @ext is a target extent to be split.
5953 * @split_fsb is a block where the extents is split. If split_fsb lies in a
5954 * hole or the first block of extents, just return 0.
5957 xfs_bmap_split_extent(
5958 struct xfs_trans
*tp
,
5959 struct xfs_inode
*ip
,
5960 xfs_fileoff_t split_fsb
)
5962 int whichfork
= XFS_DATA_FORK
;
5963 struct xfs_ifork
*ifp
= xfs_ifork_ptr(ip
, whichfork
);
5964 struct xfs_btree_cur
*cur
= NULL
;
5965 struct xfs_bmbt_irec got
;
5966 struct xfs_bmbt_irec
new; /* split extent */
5967 struct xfs_mount
*mp
= ip
->i_mount
;
5968 xfs_fsblock_t gotblkcnt
; /* new block count for got */
5969 struct xfs_iext_cursor icur
;
5974 if (XFS_IS_CORRUPT(mp
, !xfs_ifork_has_extents(ifp
)) ||
5975 XFS_TEST_ERROR(false, mp
, XFS_ERRTAG_BMAPIFORMAT
)) {
5976 return -EFSCORRUPTED
;
5979 if (xfs_is_shutdown(mp
))
5982 /* Read in all the extents */
5983 error
= xfs_iread_extents(tp
, ip
, whichfork
);
5988 * If there are not extents, or split_fsb lies in a hole we are done.
5990 if (!xfs_iext_lookup_extent(ip
, ifp
, split_fsb
, &icur
, &got
) ||
5991 got
.br_startoff
>= split_fsb
)
5994 gotblkcnt
= split_fsb
- got
.br_startoff
;
5995 new.br_startoff
= split_fsb
;
5996 new.br_startblock
= got
.br_startblock
+ gotblkcnt
;
5997 new.br_blockcount
= got
.br_blockcount
- gotblkcnt
;
5998 new.br_state
= got
.br_state
;
6000 if (ifp
->if_format
== XFS_DINODE_FMT_BTREE
) {
6001 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
6002 cur
->bc_ino
.flags
= 0;
6003 error
= xfs_bmbt_lookup_eq(cur
, &got
, &i
);
6006 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
6007 error
= -EFSCORRUPTED
;
6012 got
.br_blockcount
= gotblkcnt
;
6013 xfs_iext_update_extent(ip
, xfs_bmap_fork_to_state(whichfork
), &icur
,
6016 logflags
= XFS_ILOG_CORE
;
6018 error
= xfs_bmbt_update(cur
, &got
);
6022 logflags
|= XFS_ILOG_DEXT
;
6024 /* Add new extent */
6025 xfs_iext_next(ifp
, &icur
);
6026 xfs_iext_insert(ip
, &icur
, &new, 0);
6030 error
= xfs_bmbt_lookup_eq(cur
, &new, &i
);
6033 if (XFS_IS_CORRUPT(mp
, i
!= 0)) {
6034 error
= -EFSCORRUPTED
;
6037 error
= xfs_btree_insert(cur
, &i
);
6040 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
6041 error
= -EFSCORRUPTED
;
6047 * Convert to a btree if necessary.
6049 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
6050 int tmp_logflags
; /* partial log flag return val */
6052 ASSERT(cur
== NULL
);
6053 error
= xfs_bmap_extents_to_btree(tp
, ip
, &cur
, 0,
6054 &tmp_logflags
, whichfork
);
6055 logflags
|= tmp_logflags
;
6060 cur
->bc_ino
.allocated
= 0;
6061 xfs_btree_del_cursor(cur
, error
);
6065 xfs_trans_log_inode(tp
, ip
, logflags
);
6069 /* Deferred mapping is only for real extents in the data fork. */
6071 xfs_bmap_is_update_needed(
6072 struct xfs_bmbt_irec
*bmap
)
6074 return bmap
->br_startblock
!= HOLESTARTBLOCK
&&
6075 bmap
->br_startblock
!= DELAYSTARTBLOCK
;
6078 /* Record a bmap intent. */
6081 struct xfs_trans
*tp
,
6082 enum xfs_bmap_intent_type type
,
6083 struct xfs_inode
*ip
,
6085 struct xfs_bmbt_irec
*bmap
)
6087 struct xfs_bmap_intent
*bi
;
6089 trace_xfs_bmap_defer(tp
->t_mountp
,
6090 XFS_FSB_TO_AGNO(tp
->t_mountp
, bmap
->br_startblock
),
6092 XFS_FSB_TO_AGBNO(tp
->t_mountp
, bmap
->br_startblock
),
6093 ip
->i_ino
, whichfork
,
6095 bmap
->br_blockcount
,
6098 bi
= kmem_cache_alloc(xfs_bmap_intent_cache
, GFP_NOFS
| __GFP_NOFAIL
);
6099 INIT_LIST_HEAD(&bi
->bi_list
);
6102 bi
->bi_whichfork
= whichfork
;
6103 bi
->bi_bmap
= *bmap
;
6105 xfs_defer_add(tp
, XFS_DEFER_OPS_TYPE_BMAP
, &bi
->bi_list
);
6109 /* Map an extent into a file. */
6111 xfs_bmap_map_extent(
6112 struct xfs_trans
*tp
,
6113 struct xfs_inode
*ip
,
6114 struct xfs_bmbt_irec
*PREV
)
6116 if (!xfs_bmap_is_update_needed(PREV
))
6119 __xfs_bmap_add(tp
, XFS_BMAP_MAP
, ip
, XFS_DATA_FORK
, PREV
);
6122 /* Unmap an extent out of a file. */
6124 xfs_bmap_unmap_extent(
6125 struct xfs_trans
*tp
,
6126 struct xfs_inode
*ip
,
6127 struct xfs_bmbt_irec
*PREV
)
6129 if (!xfs_bmap_is_update_needed(PREV
))
6132 __xfs_bmap_add(tp
, XFS_BMAP_UNMAP
, ip
, XFS_DATA_FORK
, PREV
);
6136 * Process one of the deferred bmap operations. We pass back the
6137 * btree cursor to maintain our lock on the bmapbt between calls.
6140 xfs_bmap_finish_one(
6141 struct xfs_trans
*tp
,
6142 struct xfs_inode
*ip
,
6143 enum xfs_bmap_intent_type type
,
6145 xfs_fileoff_t startoff
,
6146 xfs_fsblock_t startblock
,
6147 xfs_filblks_t
*blockcount
,
6152 ASSERT(tp
->t_firstblock
== NULLFSBLOCK
);
6154 trace_xfs_bmap_deferred(tp
->t_mountp
,
6155 XFS_FSB_TO_AGNO(tp
->t_mountp
, startblock
), type
,
6156 XFS_FSB_TO_AGBNO(tp
->t_mountp
, startblock
),
6157 ip
->i_ino
, whichfork
, startoff
, *blockcount
, state
);
6159 if (WARN_ON_ONCE(whichfork
!= XFS_DATA_FORK
))
6160 return -EFSCORRUPTED
;
6162 if (XFS_TEST_ERROR(false, tp
->t_mountp
,
6163 XFS_ERRTAG_BMAP_FINISH_ONE
))
6168 error
= xfs_bmapi_remap(tp
, ip
, startoff
, *blockcount
,
6172 case XFS_BMAP_UNMAP
:
6173 error
= __xfs_bunmapi(tp
, ip
, startoff
, blockcount
,
6174 XFS_BMAPI_REMAP
, 1);
6178 error
= -EFSCORRUPTED
;
6184 /* Check that an inode's extent does not have invalid flags or bad ranges. */
6186 xfs_bmap_validate_extent(
6187 struct xfs_inode
*ip
,
6189 struct xfs_bmbt_irec
*irec
)
6191 struct xfs_mount
*mp
= ip
->i_mount
;
6193 if (!xfs_verify_fileext(mp
, irec
->br_startoff
, irec
->br_blockcount
))
6194 return __this_address
;
6196 if (XFS_IS_REALTIME_INODE(ip
) && whichfork
== XFS_DATA_FORK
) {
6197 if (!xfs_verify_rtext(mp
, irec
->br_startblock
,
6198 irec
->br_blockcount
))
6199 return __this_address
;
6201 if (!xfs_verify_fsbext(mp
, irec
->br_startblock
,
6202 irec
->br_blockcount
))
6203 return __this_address
;
6205 if (irec
->br_state
!= XFS_EXT_NORM
&& whichfork
!= XFS_DATA_FORK
)
6206 return __this_address
;
6211 xfs_bmap_intent_init_cache(void)
6213 xfs_bmap_intent_cache
= kmem_cache_create("xfs_bmap_intent",
6214 sizeof(struct xfs_bmap_intent
),
6217 return xfs_bmap_intent_cache
!= NULL
? 0 : -ENOMEM
;
6221 xfs_bmap_intent_destroy_cache(void)
6223 kmem_cache_destroy(xfs_bmap_intent_cache
);
6224 xfs_bmap_intent_cache
= NULL
;