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"
32 #include "xfs_rtbitmap.h"
34 struct kmem_cache
*xfs_bmap_intent_cache
;
37 * Miscellaneous helper functions
41 * Compute and fill in the value of the maximum depth of a bmap btree
42 * in this filesystem. Done once, during mount.
45 xfs_bmap_compute_maxlevels(
46 xfs_mount_t
*mp
, /* file system mount structure */
47 int whichfork
) /* data or attr fork */
49 uint64_t maxblocks
; /* max blocks at this level */
50 xfs_extnum_t maxleafents
; /* max leaf entries possible */
51 int level
; /* btree level */
52 int maxrootrecs
; /* max records in root block */
53 int minleafrecs
; /* min records in leaf block */
54 int minnoderecs
; /* min records in node block */
55 int sz
; /* root block size */
58 * The maximum number of extents in a fork, hence the maximum number of
59 * leaf entries, is controlled by the size of the on-disk extent count.
61 * Note that we can no longer assume that if we are in ATTR1 that the
62 * fork offset of all the inodes will be
63 * (xfs_default_attroffset(ip) >> 3) because we could have mounted with
64 * ATTR2 and then mounted back with ATTR1, keeping the i_forkoff's fixed
65 * but probably at various positions. Therefore, for both ATTR1 and
66 * ATTR2 we have to assume the worst case scenario of a minimum size
69 maxleafents
= xfs_iext_max_nextents(xfs_has_large_extent_counts(mp
),
71 if (whichfork
== XFS_DATA_FORK
)
72 sz
= XFS_BMDR_SPACE_CALC(MINDBTPTRS
);
74 sz
= XFS_BMDR_SPACE_CALC(MINABTPTRS
);
76 maxrootrecs
= xfs_bmdr_maxrecs(sz
, 0);
77 minleafrecs
= mp
->m_bmap_dmnr
[0];
78 minnoderecs
= mp
->m_bmap_dmnr
[1];
79 maxblocks
= howmany_64(maxleafents
, minleafrecs
);
80 for (level
= 1; maxblocks
> 1; level
++) {
81 if (maxblocks
<= maxrootrecs
)
84 maxblocks
= howmany_64(maxblocks
, minnoderecs
);
86 mp
->m_bm_maxlevels
[whichfork
] = level
;
87 ASSERT(mp
->m_bm_maxlevels
[whichfork
] <= xfs_bmbt_maxlevels_ondisk());
91 xfs_bmap_compute_attr_offset(
94 if (mp
->m_sb
.sb_inodesize
== 256)
95 return XFS_LITINO(mp
) - XFS_BMDR_SPACE_CALC(MINABTPTRS
);
96 return XFS_BMDR_SPACE_CALC(6 * MINABTPTRS
);
99 STATIC
int /* error */
101 struct xfs_btree_cur
*cur
,
102 struct xfs_bmbt_irec
*irec
,
103 int *stat
) /* success/failure */
105 cur
->bc_rec
.b
= *irec
;
106 return xfs_btree_lookup(cur
, XFS_LOOKUP_EQ
, stat
);
109 STATIC
int /* error */
110 xfs_bmbt_lookup_first(
111 struct xfs_btree_cur
*cur
,
112 int *stat
) /* success/failure */
114 cur
->bc_rec
.b
.br_startoff
= 0;
115 cur
->bc_rec
.b
.br_startblock
= 0;
116 cur
->bc_rec
.b
.br_blockcount
= 0;
117 return xfs_btree_lookup(cur
, XFS_LOOKUP_GE
, stat
);
121 * Check if the inode needs to be converted to btree format.
123 static inline bool xfs_bmap_needs_btree(struct xfs_inode
*ip
, int whichfork
)
125 struct xfs_ifork
*ifp
= xfs_ifork_ptr(ip
, whichfork
);
127 return whichfork
!= XFS_COW_FORK
&&
128 ifp
->if_format
== XFS_DINODE_FMT_EXTENTS
&&
129 ifp
->if_nextents
> XFS_IFORK_MAXEXT(ip
, whichfork
);
133 * Check if the inode should be converted to extent format.
135 static inline bool xfs_bmap_wants_extents(struct xfs_inode
*ip
, int whichfork
)
137 struct xfs_ifork
*ifp
= xfs_ifork_ptr(ip
, whichfork
);
139 return whichfork
!= XFS_COW_FORK
&&
140 ifp
->if_format
== XFS_DINODE_FMT_BTREE
&&
141 ifp
->if_nextents
<= XFS_IFORK_MAXEXT(ip
, whichfork
);
145 * Update the record referred to by cur to the value given by irec
146 * This either works (return 0) or gets an EFSCORRUPTED error.
150 struct xfs_btree_cur
*cur
,
151 struct xfs_bmbt_irec
*irec
)
153 union xfs_btree_rec rec
;
155 xfs_bmbt_disk_set_all(&rec
.bmbt
, irec
);
156 return xfs_btree_update(cur
, &rec
);
160 * Compute the worst-case number of indirect blocks that will be used
161 * for ip's delayed extent of length "len".
164 xfs_bmap_worst_indlen(
165 xfs_inode_t
*ip
, /* incore inode pointer */
166 xfs_filblks_t len
) /* delayed extent length */
168 int level
; /* btree level number */
169 int maxrecs
; /* maximum record count at this level */
170 xfs_mount_t
*mp
; /* mount structure */
171 xfs_filblks_t rval
; /* return value */
174 maxrecs
= mp
->m_bmap_dmxr
[0];
175 for (level
= 0, rval
= 0;
176 level
< XFS_BM_MAXLEVELS(mp
, XFS_DATA_FORK
);
179 do_div(len
, maxrecs
);
182 return rval
+ XFS_BM_MAXLEVELS(mp
, XFS_DATA_FORK
) -
185 maxrecs
= mp
->m_bmap_dmxr
[1];
191 * Calculate the default attribute fork offset for newly created inodes.
194 xfs_default_attroffset(
195 struct xfs_inode
*ip
)
197 if (ip
->i_df
.if_format
== XFS_DINODE_FMT_DEV
)
198 return roundup(sizeof(xfs_dev_t
), 8);
199 return M_IGEO(ip
->i_mount
)->attr_fork_offset
;
203 * Helper routine to reset inode i_forkoff field when switching attribute fork
204 * from local to extent format - we reset it where possible to make space
205 * available for inline data fork extents.
208 xfs_bmap_forkoff_reset(
212 if (whichfork
== XFS_ATTR_FORK
&&
213 ip
->i_df
.if_format
!= XFS_DINODE_FMT_DEV
&&
214 ip
->i_df
.if_format
!= XFS_DINODE_FMT_BTREE
) {
215 uint dfl_forkoff
= xfs_default_attroffset(ip
) >> 3;
217 if (dfl_forkoff
> ip
->i_forkoff
)
218 ip
->i_forkoff
= dfl_forkoff
;
223 STATIC
struct xfs_buf
*
225 struct xfs_btree_cur
*cur
,
228 struct xfs_log_item
*lip
;
234 for (i
= 0; i
< cur
->bc_maxlevels
; i
++) {
235 if (!cur
->bc_levels
[i
].bp
)
237 if (xfs_buf_daddr(cur
->bc_levels
[i
].bp
) == bno
)
238 return cur
->bc_levels
[i
].bp
;
241 /* Chase down all the log items to see if the bp is there */
242 list_for_each_entry(lip
, &cur
->bc_tp
->t_items
, li_trans
) {
243 struct xfs_buf_log_item
*bip
= (struct xfs_buf_log_item
*)lip
;
245 if (bip
->bli_item
.li_type
== XFS_LI_BUF
&&
246 xfs_buf_daddr(bip
->bli_buf
) == bno
)
255 struct xfs_btree_block
*block
,
261 __be64
*pp
, *thispa
; /* pointer to block address */
262 xfs_bmbt_key_t
*prevp
, *keyp
;
264 ASSERT(be16_to_cpu(block
->bb_level
) > 0);
267 for( i
= 1; i
<= xfs_btree_get_numrecs(block
); i
++) {
268 dmxr
= mp
->m_bmap_dmxr
[0];
269 keyp
= XFS_BMBT_KEY_ADDR(mp
, block
, i
);
272 ASSERT(be64_to_cpu(prevp
->br_startoff
) <
273 be64_to_cpu(keyp
->br_startoff
));
278 * Compare the block numbers to see if there are dups.
281 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, i
, sz
);
283 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, i
, dmxr
);
285 for (j
= i
+1; j
<= be16_to_cpu(block
->bb_numrecs
); j
++) {
287 thispa
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, j
, sz
);
289 thispa
= XFS_BMBT_PTR_ADDR(mp
, block
, j
, dmxr
);
290 if (*thispa
== *pp
) {
291 xfs_warn(mp
, "%s: thispa(%d) == pp(%d) %lld",
293 (unsigned long long)be64_to_cpu(*thispa
));
294 xfs_err(mp
, "%s: ptrs are equal in node\n",
296 xfs_force_shutdown(mp
, SHUTDOWN_CORRUPT_INCORE
);
303 * Check that the extents for the inode ip are in the right order in all
304 * btree leaves. THis becomes prohibitively expensive for large extent count
305 * files, so don't bother with inodes that have more than 10,000 extents in
306 * them. The btree record ordering checks will still be done, so for such large
307 * bmapbt constructs that is going to catch most corruptions.
310 xfs_bmap_check_leaf_extents(
311 struct xfs_btree_cur
*cur
, /* btree cursor or null */
312 xfs_inode_t
*ip
, /* incore inode pointer */
313 int whichfork
) /* data or attr fork */
315 struct xfs_mount
*mp
= ip
->i_mount
;
316 struct xfs_ifork
*ifp
= xfs_ifork_ptr(ip
, whichfork
);
317 struct xfs_btree_block
*block
; /* current btree block */
318 xfs_fsblock_t bno
; /* block # of "block" */
319 struct xfs_buf
*bp
; /* buffer for "block" */
320 int error
; /* error return value */
321 xfs_extnum_t i
=0, j
; /* index into the extents list */
322 int level
; /* btree level, for checking */
323 __be64
*pp
; /* pointer to block address */
324 xfs_bmbt_rec_t
*ep
; /* pointer to current extent */
325 xfs_bmbt_rec_t last
= {0, 0}; /* last extent in prev block */
326 xfs_bmbt_rec_t
*nextp
; /* pointer to next extent */
329 if (ifp
->if_format
!= XFS_DINODE_FMT_BTREE
)
332 /* skip large extent count inodes */
333 if (ip
->i_df
.if_nextents
> 10000)
337 block
= ifp
->if_broot
;
339 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
341 level
= be16_to_cpu(block
->bb_level
);
343 xfs_check_block(block
, mp
, 1, ifp
->if_broot_bytes
);
344 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, 1, ifp
->if_broot_bytes
);
345 bno
= be64_to_cpu(*pp
);
347 ASSERT(bno
!= NULLFSBLOCK
);
348 ASSERT(XFS_FSB_TO_AGNO(mp
, bno
) < mp
->m_sb
.sb_agcount
);
349 ASSERT(XFS_FSB_TO_AGBNO(mp
, bno
) < mp
->m_sb
.sb_agblocks
);
352 * Go down the tree until leaf level is reached, following the first
353 * pointer (leftmost) at each level.
355 while (level
-- > 0) {
356 /* See if buf is in cur first */
358 bp
= xfs_bmap_get_bp(cur
, XFS_FSB_TO_DADDR(mp
, bno
));
361 error
= xfs_btree_read_bufl(mp
, NULL
, bno
, &bp
,
367 block
= XFS_BUF_TO_BLOCK(bp
);
372 * Check this block for basic sanity (increasing keys and
373 * no duplicate blocks).
376 xfs_check_block(block
, mp
, 0, 0);
377 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, mp
->m_bmap_dmxr
[1]);
378 bno
= be64_to_cpu(*pp
);
379 if (XFS_IS_CORRUPT(mp
, !xfs_verify_fsbno(mp
, bno
))) {
380 error
= -EFSCORRUPTED
;
385 xfs_trans_brelse(NULL
, bp
);
390 * Here with bp and block set to the leftmost leaf node in the tree.
395 * Loop over all leaf nodes checking that all extents are in the right order.
398 xfs_fsblock_t nextbno
;
399 xfs_extnum_t num_recs
;
402 num_recs
= xfs_btree_get_numrecs(block
);
405 * Read-ahead the next leaf block, if any.
408 nextbno
= be64_to_cpu(block
->bb_u
.l
.bb_rightsib
);
411 * Check all the extents to make sure they are OK.
412 * If we had a previous block, the last entry should
413 * conform with the first entry in this one.
416 ep
= XFS_BMBT_REC_ADDR(mp
, block
, 1);
418 ASSERT(xfs_bmbt_disk_get_startoff(&last
) +
419 xfs_bmbt_disk_get_blockcount(&last
) <=
420 xfs_bmbt_disk_get_startoff(ep
));
422 for (j
= 1; j
< num_recs
; j
++) {
423 nextp
= XFS_BMBT_REC_ADDR(mp
, block
, j
+ 1);
424 ASSERT(xfs_bmbt_disk_get_startoff(ep
) +
425 xfs_bmbt_disk_get_blockcount(ep
) <=
426 xfs_bmbt_disk_get_startoff(nextp
));
434 xfs_trans_brelse(NULL
, bp
);
438 * If we've reached the end, stop.
440 if (bno
== NULLFSBLOCK
)
444 bp
= xfs_bmap_get_bp(cur
, XFS_FSB_TO_DADDR(mp
, bno
));
447 error
= xfs_btree_read_bufl(mp
, NULL
, bno
, &bp
,
453 block
= XFS_BUF_TO_BLOCK(bp
);
459 xfs_warn(mp
, "%s: at error0", __func__
);
461 xfs_trans_brelse(NULL
, bp
);
463 xfs_warn(mp
, "%s: BAD after btree leaves for %llu extents",
465 xfs_err(mp
, "%s: CORRUPTED BTREE OR SOMETHING", __func__
);
466 xfs_force_shutdown(mp
, SHUTDOWN_CORRUPT_INCORE
);
471 * Validate that the bmbt_irecs being returned from bmapi are valid
472 * given the caller's original parameters. Specifically check the
473 * ranges of the returned irecs to ensure that they only extend beyond
474 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
477 xfs_bmap_validate_ret(
481 xfs_bmbt_irec_t
*mval
,
485 int i
; /* index to map values */
487 ASSERT(ret_nmap
<= nmap
);
489 for (i
= 0; i
< ret_nmap
; i
++) {
490 ASSERT(mval
[i
].br_blockcount
> 0);
491 if (!(flags
& XFS_BMAPI_ENTIRE
)) {
492 ASSERT(mval
[i
].br_startoff
>= bno
);
493 ASSERT(mval
[i
].br_blockcount
<= len
);
494 ASSERT(mval
[i
].br_startoff
+ mval
[i
].br_blockcount
<=
497 ASSERT(mval
[i
].br_startoff
< bno
+ len
);
498 ASSERT(mval
[i
].br_startoff
+ mval
[i
].br_blockcount
>
502 mval
[i
- 1].br_startoff
+ mval
[i
- 1].br_blockcount
==
503 mval
[i
].br_startoff
);
504 ASSERT(mval
[i
].br_startblock
!= DELAYSTARTBLOCK
&&
505 mval
[i
].br_startblock
!= HOLESTARTBLOCK
);
506 ASSERT(mval
[i
].br_state
== XFS_EXT_NORM
||
507 mval
[i
].br_state
== XFS_EXT_UNWRITTEN
);
512 #define xfs_bmap_check_leaf_extents(cur, ip, whichfork) do { } while (0)
513 #define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap) do { } while (0)
517 * Inode fork format manipulation functions
521 * Convert the inode format to extent format if it currently is in btree format,
522 * but the extent list is small enough that it fits into the extent format.
524 * Since the extents are already in-core, all we have to do is give up the space
525 * for the btree root and pitch the leaf block.
527 STATIC
int /* error */
528 xfs_bmap_btree_to_extents(
529 struct xfs_trans
*tp
, /* transaction pointer */
530 struct xfs_inode
*ip
, /* incore inode pointer */
531 struct xfs_btree_cur
*cur
, /* btree cursor */
532 int *logflagsp
, /* inode logging flags */
533 int whichfork
) /* data or attr fork */
535 struct xfs_ifork
*ifp
= xfs_ifork_ptr(ip
, whichfork
);
536 struct xfs_mount
*mp
= ip
->i_mount
;
537 struct xfs_btree_block
*rblock
= ifp
->if_broot
;
538 struct xfs_btree_block
*cblock
;/* child btree block */
539 xfs_fsblock_t cbno
; /* child block number */
540 struct xfs_buf
*cbp
; /* child block's buffer */
541 int error
; /* error return value */
542 __be64
*pp
; /* ptr to block address */
543 struct xfs_owner_info oinfo
;
545 /* check if we actually need the extent format first: */
546 if (!xfs_bmap_wants_extents(ip
, whichfork
))
550 ASSERT(whichfork
!= XFS_COW_FORK
);
551 ASSERT(ifp
->if_format
== XFS_DINODE_FMT_BTREE
);
552 ASSERT(be16_to_cpu(rblock
->bb_level
) == 1);
553 ASSERT(be16_to_cpu(rblock
->bb_numrecs
) == 1);
554 ASSERT(xfs_bmbt_maxrecs(mp
, ifp
->if_broot_bytes
, 0) == 1);
556 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, rblock
, 1, ifp
->if_broot_bytes
);
557 cbno
= be64_to_cpu(*pp
);
559 if (XFS_IS_CORRUPT(cur
->bc_mp
, !xfs_btree_check_lptr(cur
, cbno
, 1)))
560 return -EFSCORRUPTED
;
562 error
= xfs_btree_read_bufl(mp
, tp
, cbno
, &cbp
, XFS_BMAP_BTREE_REF
,
566 cblock
= XFS_BUF_TO_BLOCK(cbp
);
567 if ((error
= xfs_btree_check_block(cur
, cblock
, 0, cbp
)))
570 xfs_rmap_ino_bmbt_owner(&oinfo
, ip
->i_ino
, whichfork
);
571 error
= xfs_free_extent_later(cur
->bc_tp
, cbno
, 1, &oinfo
,
577 xfs_trans_mod_dquot_byino(tp
, ip
, XFS_TRANS_DQ_BCOUNT
, -1L);
578 xfs_trans_binval(tp
, cbp
);
579 if (cur
->bc_levels
[0].bp
== cbp
)
580 cur
->bc_levels
[0].bp
= NULL
;
581 xfs_iroot_realloc(ip
, -1, whichfork
);
582 ASSERT(ifp
->if_broot
== NULL
);
583 ifp
->if_format
= XFS_DINODE_FMT_EXTENTS
;
584 *logflagsp
|= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
589 * Convert an extents-format file into a btree-format file.
590 * The new file will have a root block (in the inode) and a single child block.
592 STATIC
int /* error */
593 xfs_bmap_extents_to_btree(
594 struct xfs_trans
*tp
, /* transaction pointer */
595 struct xfs_inode
*ip
, /* incore inode pointer */
596 struct xfs_btree_cur
**curp
, /* cursor returned to caller */
597 int wasdel
, /* converting a delayed alloc */
598 int *logflagsp
, /* inode logging flags */
599 int whichfork
) /* data or attr fork */
601 struct xfs_btree_block
*ablock
; /* allocated (child) bt block */
602 struct xfs_buf
*abp
; /* buffer for ablock */
603 struct xfs_alloc_arg args
; /* allocation arguments */
604 struct xfs_bmbt_rec
*arp
; /* child record pointer */
605 struct xfs_btree_block
*block
; /* btree root block */
606 struct xfs_btree_cur
*cur
; /* bmap btree cursor */
607 int error
; /* error return value */
608 struct xfs_ifork
*ifp
; /* inode fork pointer */
609 struct xfs_bmbt_key
*kp
; /* root block key pointer */
610 struct xfs_mount
*mp
; /* mount structure */
611 xfs_bmbt_ptr_t
*pp
; /* root block address pointer */
612 struct xfs_iext_cursor icur
;
613 struct xfs_bmbt_irec rec
;
614 xfs_extnum_t cnt
= 0;
617 ASSERT(whichfork
!= XFS_COW_FORK
);
618 ifp
= xfs_ifork_ptr(ip
, whichfork
);
619 ASSERT(ifp
->if_format
== XFS_DINODE_FMT_EXTENTS
);
622 * Make space in the inode incore. This needs to be undone if we fail
623 * to expand the root.
625 xfs_iroot_realloc(ip
, 1, whichfork
);
630 block
= ifp
->if_broot
;
631 xfs_btree_init_block_int(mp
, block
, XFS_BUF_DADDR_NULL
,
632 XFS_BTNUM_BMAP
, 1, 1, ip
->i_ino
,
633 XFS_BTREE_LONG_PTRS
);
635 * Need a cursor. Can't allocate until bb_level is filled in.
637 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
638 cur
->bc_ino
.flags
= wasdel
? XFS_BTCUR_BMBT_WASDEL
: 0;
640 * Convert to a btree with two levels, one record in root.
642 ifp
->if_format
= XFS_DINODE_FMT_BTREE
;
643 memset(&args
, 0, sizeof(args
));
646 xfs_rmap_ino_bmbt_owner(&args
.oinfo
, ip
->i_ino
, whichfork
);
648 args
.minlen
= args
.maxlen
= args
.prod
= 1;
649 args
.wasdel
= wasdel
;
651 error
= xfs_alloc_vextent_start_ag(&args
,
652 XFS_INO_TO_FSB(mp
, ip
->i_ino
));
654 goto out_root_realloc
;
657 * Allocation can't fail, the space was reserved.
659 if (WARN_ON_ONCE(args
.fsbno
== NULLFSBLOCK
)) {
661 goto out_root_realloc
;
664 cur
->bc_ino
.allocated
++;
666 xfs_trans_mod_dquot_byino(tp
, ip
, XFS_TRANS_DQ_BCOUNT
, 1L);
667 error
= xfs_trans_get_buf(tp
, mp
->m_ddev_targp
,
668 XFS_FSB_TO_DADDR(mp
, args
.fsbno
),
669 mp
->m_bsize
, 0, &abp
);
671 goto out_unreserve_dquot
;
674 * Fill in the child block.
676 abp
->b_ops
= &xfs_bmbt_buf_ops
;
677 ablock
= XFS_BUF_TO_BLOCK(abp
);
678 xfs_btree_init_block_int(mp
, ablock
, xfs_buf_daddr(abp
),
679 XFS_BTNUM_BMAP
, 0, 0, ip
->i_ino
,
680 XFS_BTREE_LONG_PTRS
);
682 for_each_xfs_iext(ifp
, &icur
, &rec
) {
683 if (isnullstartblock(rec
.br_startblock
))
685 arp
= XFS_BMBT_REC_ADDR(mp
, ablock
, 1 + cnt
);
686 xfs_bmbt_disk_set_all(arp
, &rec
);
689 ASSERT(cnt
== ifp
->if_nextents
);
690 xfs_btree_set_numrecs(ablock
, cnt
);
693 * Fill in the root key and pointer.
695 kp
= XFS_BMBT_KEY_ADDR(mp
, block
, 1);
696 arp
= XFS_BMBT_REC_ADDR(mp
, ablock
, 1);
697 kp
->br_startoff
= cpu_to_be64(xfs_bmbt_disk_get_startoff(arp
));
698 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, xfs_bmbt_get_maxrecs(cur
,
699 be16_to_cpu(block
->bb_level
)));
700 *pp
= cpu_to_be64(args
.fsbno
);
703 * Do all this logging at the end so that
704 * the root is at the right level.
706 xfs_btree_log_block(cur
, abp
, XFS_BB_ALL_BITS
);
707 xfs_btree_log_recs(cur
, abp
, 1, be16_to_cpu(ablock
->bb_numrecs
));
708 ASSERT(*curp
== NULL
);
710 *logflagsp
= XFS_ILOG_CORE
| xfs_ilog_fbroot(whichfork
);
714 xfs_trans_mod_dquot_byino(tp
, ip
, XFS_TRANS_DQ_BCOUNT
, -1L);
716 xfs_iroot_realloc(ip
, -1, whichfork
);
717 ifp
->if_format
= XFS_DINODE_FMT_EXTENTS
;
718 ASSERT(ifp
->if_broot
== NULL
);
719 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
725 * Convert a local file to an extents file.
726 * This code is out of bounds for data forks of regular files,
727 * since the file data needs to get logged so things will stay consistent.
728 * (The bmap-level manipulations are ok, though).
731 xfs_bmap_local_to_extents_empty(
732 struct xfs_trans
*tp
,
733 struct xfs_inode
*ip
,
736 struct xfs_ifork
*ifp
= xfs_ifork_ptr(ip
, whichfork
);
738 ASSERT(whichfork
!= XFS_COW_FORK
);
739 ASSERT(ifp
->if_format
== XFS_DINODE_FMT_LOCAL
);
740 ASSERT(ifp
->if_bytes
== 0);
741 ASSERT(ifp
->if_nextents
== 0);
743 xfs_bmap_forkoff_reset(ip
, whichfork
);
744 ifp
->if_u1
.if_root
= NULL
;
746 ifp
->if_format
= XFS_DINODE_FMT_EXTENTS
;
747 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
751 STATIC
int /* error */
752 xfs_bmap_local_to_extents(
753 xfs_trans_t
*tp
, /* transaction pointer */
754 xfs_inode_t
*ip
, /* incore inode pointer */
755 xfs_extlen_t total
, /* total blocks needed by transaction */
756 int *logflagsp
, /* inode logging flags */
758 void (*init_fn
)(struct xfs_trans
*tp
,
760 struct xfs_inode
*ip
,
761 struct xfs_ifork
*ifp
))
764 int flags
; /* logging flags returned */
765 struct xfs_ifork
*ifp
; /* inode fork pointer */
766 xfs_alloc_arg_t args
; /* allocation arguments */
767 struct xfs_buf
*bp
; /* buffer for extent block */
768 struct xfs_bmbt_irec rec
;
769 struct xfs_iext_cursor icur
;
772 * We don't want to deal with the case of keeping inode data inline yet.
773 * So sending the data fork of a regular inode is invalid.
775 ASSERT(!(S_ISREG(VFS_I(ip
)->i_mode
) && whichfork
== XFS_DATA_FORK
));
776 ifp
= xfs_ifork_ptr(ip
, whichfork
);
777 ASSERT(ifp
->if_format
== XFS_DINODE_FMT_LOCAL
);
779 if (!ifp
->if_bytes
) {
780 xfs_bmap_local_to_extents_empty(tp
, ip
, whichfork
);
781 flags
= XFS_ILOG_CORE
;
787 memset(&args
, 0, sizeof(args
));
789 args
.mp
= ip
->i_mount
;
791 args
.minlen
= args
.maxlen
= args
.prod
= 1;
792 xfs_rmap_ino_owner(&args
.oinfo
, ip
->i_ino
, whichfork
, 0);
795 * Allocate a block. We know we need only one, since the
796 * file currently fits in an inode.
799 args
.minlen
= args
.maxlen
= args
.prod
= 1;
800 error
= xfs_alloc_vextent_start_ag(&args
,
801 XFS_INO_TO_FSB(args
.mp
, ip
->i_ino
));
805 /* Can't fail, the space was reserved. */
806 ASSERT(args
.fsbno
!= NULLFSBLOCK
);
807 ASSERT(args
.len
== 1);
808 error
= xfs_trans_get_buf(tp
, args
.mp
->m_ddev_targp
,
809 XFS_FSB_TO_DADDR(args
.mp
, args
.fsbno
),
810 args
.mp
->m_bsize
, 0, &bp
);
815 * Initialize the block, copy the data and log the remote buffer.
817 * The callout is responsible for logging because the remote format
818 * might differ from the local format and thus we don't know how much to
819 * log here. Note that init_fn must also set the buffer log item type
822 init_fn(tp
, bp
, ip
, ifp
);
824 /* account for the change in fork size */
825 xfs_idata_realloc(ip
, -ifp
->if_bytes
, whichfork
);
826 xfs_bmap_local_to_extents_empty(tp
, ip
, whichfork
);
827 flags
|= XFS_ILOG_CORE
;
829 ifp
->if_u1
.if_root
= NULL
;
833 rec
.br_startblock
= args
.fsbno
;
834 rec
.br_blockcount
= 1;
835 rec
.br_state
= XFS_EXT_NORM
;
836 xfs_iext_first(ifp
, &icur
);
837 xfs_iext_insert(ip
, &icur
, &rec
, 0);
839 ifp
->if_nextents
= 1;
841 xfs_trans_mod_dquot_byino(tp
, ip
, XFS_TRANS_DQ_BCOUNT
, 1L);
842 flags
|= xfs_ilog_fext(whichfork
);
850 * Called from xfs_bmap_add_attrfork to handle btree format files.
852 STATIC
int /* error */
853 xfs_bmap_add_attrfork_btree(
854 xfs_trans_t
*tp
, /* transaction pointer */
855 xfs_inode_t
*ip
, /* incore inode pointer */
856 int *flags
) /* inode logging flags */
858 struct xfs_btree_block
*block
= ip
->i_df
.if_broot
;
859 struct xfs_btree_cur
*cur
; /* btree cursor */
860 int error
; /* error return value */
861 xfs_mount_t
*mp
; /* file system mount struct */
862 int stat
; /* newroot status */
866 if (XFS_BMAP_BMDR_SPACE(block
) <= xfs_inode_data_fork_size(ip
))
867 *flags
|= XFS_ILOG_DBROOT
;
869 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, XFS_DATA_FORK
);
870 error
= xfs_bmbt_lookup_first(cur
, &stat
);
873 /* must be at least one entry */
874 if (XFS_IS_CORRUPT(mp
, stat
!= 1)) {
875 error
= -EFSCORRUPTED
;
878 if ((error
= xfs_btree_new_iroot(cur
, flags
, &stat
)))
881 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
884 cur
->bc_ino
.allocated
= 0;
885 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
889 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
894 * Called from xfs_bmap_add_attrfork to handle extents format files.
896 STATIC
int /* error */
897 xfs_bmap_add_attrfork_extents(
898 struct xfs_trans
*tp
, /* transaction pointer */
899 struct xfs_inode
*ip
, /* incore inode pointer */
900 int *flags
) /* inode logging flags */
902 struct xfs_btree_cur
*cur
; /* bmap btree cursor */
903 int error
; /* error return value */
905 if (ip
->i_df
.if_nextents
* sizeof(struct xfs_bmbt_rec
) <=
906 xfs_inode_data_fork_size(ip
))
909 error
= xfs_bmap_extents_to_btree(tp
, ip
, &cur
, 0, flags
,
912 cur
->bc_ino
.allocated
= 0;
913 xfs_btree_del_cursor(cur
, error
);
919 * Called from xfs_bmap_add_attrfork to handle local format files. Each
920 * different data fork content type needs a different callout to do the
921 * conversion. Some are basic and only require special block initialisation
922 * callouts for the data formating, others (directories) are so specialised they
923 * handle everything themselves.
925 * XXX (dgc): investigate whether directory conversion can use the generic
926 * formatting callout. It should be possible - it's just a very complex
929 STATIC
int /* error */
930 xfs_bmap_add_attrfork_local(
931 struct xfs_trans
*tp
, /* transaction pointer */
932 struct xfs_inode
*ip
, /* incore inode pointer */
933 int *flags
) /* inode logging flags */
935 struct xfs_da_args dargs
; /* args for dir/attr code */
937 if (ip
->i_df
.if_bytes
<= xfs_inode_data_fork_size(ip
))
940 if (S_ISDIR(VFS_I(ip
)->i_mode
)) {
941 memset(&dargs
, 0, sizeof(dargs
));
942 dargs
.geo
= ip
->i_mount
->m_dir_geo
;
944 dargs
.total
= dargs
.geo
->fsbcount
;
945 dargs
.whichfork
= XFS_DATA_FORK
;
947 return xfs_dir2_sf_to_block(&dargs
);
950 if (S_ISLNK(VFS_I(ip
)->i_mode
))
951 return xfs_bmap_local_to_extents(tp
, ip
, 1, flags
,
953 xfs_symlink_local_to_remote
);
955 /* should only be called for types that support local format data */
957 return -EFSCORRUPTED
;
961 * Set an inode attr fork offset based on the format of the data fork.
964 xfs_bmap_set_attrforkoff(
965 struct xfs_inode
*ip
,
969 int default_size
= xfs_default_attroffset(ip
) >> 3;
971 switch (ip
->i_df
.if_format
) {
972 case XFS_DINODE_FMT_DEV
:
973 ip
->i_forkoff
= default_size
;
975 case XFS_DINODE_FMT_LOCAL
:
976 case XFS_DINODE_FMT_EXTENTS
:
977 case XFS_DINODE_FMT_BTREE
:
978 ip
->i_forkoff
= xfs_attr_shortform_bytesfit(ip
, size
);
980 ip
->i_forkoff
= default_size
;
981 else if (xfs_has_attr2(ip
->i_mount
) && version
)
993 * Convert inode from non-attributed to attributed.
994 * Must not be in a transaction, ip must not be locked.
997 xfs_bmap_add_attrfork(
998 xfs_inode_t
*ip
, /* incore inode pointer */
999 int size
, /* space new attribute needs */
1000 int rsvd
) /* xact may use reserved blks */
1002 xfs_mount_t
*mp
; /* mount structure */
1003 xfs_trans_t
*tp
; /* transaction pointer */
1004 int blks
; /* space reservation */
1005 int version
= 1; /* superblock attr version */
1006 int logflags
; /* logging flags */
1007 int error
; /* error return value */
1009 ASSERT(xfs_inode_has_attr_fork(ip
) == 0);
1012 ASSERT(!XFS_NOT_DQATTACHED(mp
, ip
));
1014 blks
= XFS_ADDAFORK_SPACE_RES(mp
);
1016 error
= xfs_trans_alloc_inode(ip
, &M_RES(mp
)->tr_addafork
, blks
, 0,
1020 if (xfs_inode_has_attr_fork(ip
))
1023 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
1024 error
= xfs_bmap_set_attrforkoff(ip
, size
, &version
);
1028 xfs_ifork_init_attr(ip
, XFS_DINODE_FMT_EXTENTS
, 0);
1030 switch (ip
->i_df
.if_format
) {
1031 case XFS_DINODE_FMT_LOCAL
:
1032 error
= xfs_bmap_add_attrfork_local(tp
, ip
, &logflags
);
1034 case XFS_DINODE_FMT_EXTENTS
:
1035 error
= xfs_bmap_add_attrfork_extents(tp
, ip
, &logflags
);
1037 case XFS_DINODE_FMT_BTREE
:
1038 error
= xfs_bmap_add_attrfork_btree(tp
, ip
, &logflags
);
1045 xfs_trans_log_inode(tp
, ip
, logflags
);
1048 if (!xfs_has_attr(mp
) ||
1049 (!xfs_has_attr2(mp
) && version
== 2)) {
1050 bool log_sb
= false;
1052 spin_lock(&mp
->m_sb_lock
);
1053 if (!xfs_has_attr(mp
)) {
1057 if (!xfs_has_attr2(mp
) && version
== 2) {
1061 spin_unlock(&mp
->m_sb_lock
);
1066 error
= xfs_trans_commit(tp
);
1067 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
1071 xfs_trans_cancel(tp
);
1072 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
1077 * Internal and external extent tree search functions.
1080 struct xfs_iread_state
{
1081 struct xfs_iext_cursor icur
;
1082 xfs_extnum_t loaded
;
1086 xfs_bmap_complain_bad_rec(
1087 struct xfs_inode
*ip
,
1090 const struct xfs_bmbt_irec
*irec
)
1092 struct xfs_mount
*mp
= ip
->i_mount
;
1093 const char *forkname
;
1095 switch (whichfork
) {
1096 case XFS_DATA_FORK
: forkname
= "data"; break;
1097 case XFS_ATTR_FORK
: forkname
= "attr"; break;
1098 case XFS_COW_FORK
: forkname
= "CoW"; break;
1099 default: forkname
= "???"; break;
1103 "Bmap BTree record corruption in inode 0x%llx %s fork detected at %pS!",
1104 ip
->i_ino
, forkname
, fa
);
1106 "Offset 0x%llx, start block 0x%llx, block count 0x%llx state 0x%x",
1107 irec
->br_startoff
, irec
->br_startblock
, irec
->br_blockcount
,
1110 return -EFSCORRUPTED
;
1113 /* Stuff every bmbt record from this block into the incore extent map. */
1115 xfs_iread_bmbt_block(
1116 struct xfs_btree_cur
*cur
,
1120 struct xfs_iread_state
*ir
= priv
;
1121 struct xfs_mount
*mp
= cur
->bc_mp
;
1122 struct xfs_inode
*ip
= cur
->bc_ino
.ip
;
1123 struct xfs_btree_block
*block
;
1125 struct xfs_bmbt_rec
*frp
;
1126 xfs_extnum_t num_recs
;
1128 int whichfork
= cur
->bc_ino
.whichfork
;
1129 struct xfs_ifork
*ifp
= xfs_ifork_ptr(ip
, whichfork
);
1131 block
= xfs_btree_get_block(cur
, level
, &bp
);
1133 /* Abort if we find more records than nextents. */
1134 num_recs
= xfs_btree_get_numrecs(block
);
1135 if (unlikely(ir
->loaded
+ num_recs
> ifp
->if_nextents
)) {
1136 xfs_warn(ip
->i_mount
, "corrupt dinode %llu, (btree extents).",
1137 (unsigned long long)ip
->i_ino
);
1138 xfs_inode_verifier_error(ip
, -EFSCORRUPTED
, __func__
, block
,
1139 sizeof(*block
), __this_address
);
1140 return -EFSCORRUPTED
;
1143 /* Copy records into the incore cache. */
1144 frp
= XFS_BMBT_REC_ADDR(mp
, block
, 1);
1145 for (j
= 0; j
< num_recs
; j
++, frp
++, ir
->loaded
++) {
1146 struct xfs_bmbt_irec
new;
1149 xfs_bmbt_disk_get_all(frp
, &new);
1150 fa
= xfs_bmap_validate_extent(ip
, whichfork
, &new);
1152 xfs_inode_verifier_error(ip
, -EFSCORRUPTED
,
1153 "xfs_iread_extents(2)", frp
,
1155 return xfs_bmap_complain_bad_rec(ip
, whichfork
, fa
,
1158 xfs_iext_insert(ip
, &ir
->icur
, &new,
1159 xfs_bmap_fork_to_state(whichfork
));
1160 trace_xfs_read_extent(ip
, &ir
->icur
,
1161 xfs_bmap_fork_to_state(whichfork
), _THIS_IP_
);
1162 xfs_iext_next(ifp
, &ir
->icur
);
1169 * Read in extents from a btree-format inode.
1173 struct xfs_trans
*tp
,
1174 struct xfs_inode
*ip
,
1177 struct xfs_iread_state ir
;
1178 struct xfs_ifork
*ifp
= xfs_ifork_ptr(ip
, whichfork
);
1179 struct xfs_mount
*mp
= ip
->i_mount
;
1180 struct xfs_btree_cur
*cur
;
1183 if (!xfs_need_iread_extents(ifp
))
1186 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
1189 xfs_iext_first(ifp
, &ir
.icur
);
1190 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
1191 error
= xfs_btree_visit_blocks(cur
, xfs_iread_bmbt_block
,
1192 XFS_BTREE_VISIT_RECORDS
, &ir
);
1193 xfs_btree_del_cursor(cur
, error
);
1197 if (XFS_IS_CORRUPT(mp
, ir
.loaded
!= ifp
->if_nextents
)) {
1198 error
= -EFSCORRUPTED
;
1201 ASSERT(ir
.loaded
== xfs_iext_count(ifp
));
1203 * Use release semantics so that we can use acquire semantics in
1204 * xfs_need_iread_extents and be guaranteed to see a valid mapping tree
1207 smp_store_release(&ifp
->if_needextents
, 0);
1210 xfs_iext_destroy(ifp
);
1215 * Returns the relative block number of the first unused block(s) in the given
1216 * fork with at least "len" logically contiguous blocks free. This is the
1217 * lowest-address hole if the fork has holes, else the first block past the end
1218 * of fork. Return 0 if the fork is currently local (in-inode).
1221 xfs_bmap_first_unused(
1222 struct xfs_trans
*tp
, /* transaction pointer */
1223 struct xfs_inode
*ip
, /* incore inode */
1224 xfs_extlen_t len
, /* size of hole to find */
1225 xfs_fileoff_t
*first_unused
, /* unused block */
1226 int whichfork
) /* data or attr fork */
1228 struct xfs_ifork
*ifp
= xfs_ifork_ptr(ip
, whichfork
);
1229 struct xfs_bmbt_irec got
;
1230 struct xfs_iext_cursor icur
;
1231 xfs_fileoff_t lastaddr
= 0;
1232 xfs_fileoff_t lowest
, max
;
1235 if (ifp
->if_format
== XFS_DINODE_FMT_LOCAL
) {
1240 ASSERT(xfs_ifork_has_extents(ifp
));
1242 error
= xfs_iread_extents(tp
, ip
, whichfork
);
1246 lowest
= max
= *first_unused
;
1247 for_each_xfs_iext(ifp
, &icur
, &got
) {
1249 * See if the hole before this extent will work.
1251 if (got
.br_startoff
>= lowest
+ len
&&
1252 got
.br_startoff
- max
>= len
)
1254 lastaddr
= got
.br_startoff
+ got
.br_blockcount
;
1255 max
= XFS_FILEOFF_MAX(lastaddr
, lowest
);
1258 *first_unused
= max
;
1263 * Returns the file-relative block number of the last block - 1 before
1264 * last_block (input value) in the file.
1265 * This is not based on i_size, it is based on the extent records.
1266 * Returns 0 for local files, as they do not have extent records.
1269 xfs_bmap_last_before(
1270 struct xfs_trans
*tp
, /* transaction pointer */
1271 struct xfs_inode
*ip
, /* incore inode */
1272 xfs_fileoff_t
*last_block
, /* last block */
1273 int whichfork
) /* data or attr fork */
1275 struct xfs_ifork
*ifp
= xfs_ifork_ptr(ip
, whichfork
);
1276 struct xfs_bmbt_irec got
;
1277 struct xfs_iext_cursor icur
;
1280 switch (ifp
->if_format
) {
1281 case XFS_DINODE_FMT_LOCAL
:
1284 case XFS_DINODE_FMT_BTREE
:
1285 case XFS_DINODE_FMT_EXTENTS
:
1289 return -EFSCORRUPTED
;
1292 error
= xfs_iread_extents(tp
, ip
, whichfork
);
1296 if (!xfs_iext_lookup_extent_before(ip
, ifp
, last_block
, &icur
, &got
))
1302 xfs_bmap_last_extent(
1303 struct xfs_trans
*tp
,
1304 struct xfs_inode
*ip
,
1306 struct xfs_bmbt_irec
*rec
,
1309 struct xfs_ifork
*ifp
= xfs_ifork_ptr(ip
, whichfork
);
1310 struct xfs_iext_cursor icur
;
1313 error
= xfs_iread_extents(tp
, ip
, whichfork
);
1317 xfs_iext_last(ifp
, &icur
);
1318 if (!xfs_iext_get_extent(ifp
, &icur
, rec
))
1326 * Check the last inode extent to determine whether this allocation will result
1327 * in blocks being allocated at the end of the file. When we allocate new data
1328 * blocks at the end of the file which do not start at the previous data block,
1329 * we will try to align the new blocks at stripe unit boundaries.
1331 * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be
1332 * at, or past the EOF.
1336 struct xfs_bmalloca
*bma
,
1339 struct xfs_bmbt_irec rec
;
1344 error
= xfs_bmap_last_extent(NULL
, bma
->ip
, whichfork
, &rec
,
1355 * Check if we are allocation or past the last extent, or at least into
1356 * the last delayed allocated extent.
1358 bma
->aeof
= bma
->offset
>= rec
.br_startoff
+ rec
.br_blockcount
||
1359 (bma
->offset
>= rec
.br_startoff
&&
1360 isnullstartblock(rec
.br_startblock
));
1365 * Returns the file-relative block number of the first block past eof in
1366 * the file. This is not based on i_size, it is based on the extent records.
1367 * Returns 0 for local files, as they do not have extent records.
1370 xfs_bmap_last_offset(
1371 struct xfs_inode
*ip
,
1372 xfs_fileoff_t
*last_block
,
1375 struct xfs_ifork
*ifp
= xfs_ifork_ptr(ip
, whichfork
);
1376 struct xfs_bmbt_irec rec
;
1382 if (ifp
->if_format
== XFS_DINODE_FMT_LOCAL
)
1385 if (XFS_IS_CORRUPT(ip
->i_mount
, !xfs_ifork_has_extents(ifp
)))
1386 return -EFSCORRUPTED
;
1388 error
= xfs_bmap_last_extent(NULL
, ip
, whichfork
, &rec
, &is_empty
);
1389 if (error
|| is_empty
)
1392 *last_block
= rec
.br_startoff
+ rec
.br_blockcount
;
1397 * Extent tree manipulation functions used during allocation.
1401 * Convert a delayed allocation to a real allocation.
1403 STATIC
int /* error */
1404 xfs_bmap_add_extent_delay_real(
1405 struct xfs_bmalloca
*bma
,
1408 struct xfs_mount
*mp
= bma
->ip
->i_mount
;
1409 struct xfs_ifork
*ifp
= xfs_ifork_ptr(bma
->ip
, whichfork
);
1410 struct xfs_bmbt_irec
*new = &bma
->got
;
1411 int error
; /* error return value */
1412 int i
; /* temp state */
1413 xfs_fileoff_t new_endoff
; /* end offset of new entry */
1414 xfs_bmbt_irec_t r
[3]; /* neighbor extent entries */
1415 /* left is 0, right is 1, prev is 2 */
1416 int rval
=0; /* return value (logging flags) */
1417 uint32_t state
= xfs_bmap_fork_to_state(whichfork
);
1418 xfs_filblks_t da_new
; /* new count del alloc blocks used */
1419 xfs_filblks_t da_old
; /* old count del alloc blocks used */
1420 xfs_filblks_t temp
=0; /* value for da_new calculations */
1421 int tmp_rval
; /* partial logging flags */
1422 struct xfs_bmbt_irec old
;
1424 ASSERT(whichfork
!= XFS_ATTR_FORK
);
1425 ASSERT(!isnullstartblock(new->br_startblock
));
1427 (bma
->cur
->bc_ino
.flags
& XFS_BTCUR_BMBT_WASDEL
));
1429 XFS_STATS_INC(mp
, xs_add_exlist
);
1436 * Set up a bunch of variables to make the tests simpler.
1438 xfs_iext_get_extent(ifp
, &bma
->icur
, &PREV
);
1439 new_endoff
= new->br_startoff
+ new->br_blockcount
;
1440 ASSERT(isnullstartblock(PREV
.br_startblock
));
1441 ASSERT(PREV
.br_startoff
<= new->br_startoff
);
1442 ASSERT(PREV
.br_startoff
+ PREV
.br_blockcount
>= new_endoff
);
1444 da_old
= startblockval(PREV
.br_startblock
);
1448 * Set flags determining what part of the previous delayed allocation
1449 * extent is being replaced by a real allocation.
1451 if (PREV
.br_startoff
== new->br_startoff
)
1452 state
|= BMAP_LEFT_FILLING
;
1453 if (PREV
.br_startoff
+ PREV
.br_blockcount
== new_endoff
)
1454 state
|= BMAP_RIGHT_FILLING
;
1457 * Check and set flags if this segment has a left neighbor.
1458 * Don't set contiguous if the combined extent would be too large.
1460 if (xfs_iext_peek_prev_extent(ifp
, &bma
->icur
, &LEFT
)) {
1461 state
|= BMAP_LEFT_VALID
;
1462 if (isnullstartblock(LEFT
.br_startblock
))
1463 state
|= BMAP_LEFT_DELAY
;
1466 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
1467 LEFT
.br_startoff
+ LEFT
.br_blockcount
== new->br_startoff
&&
1468 LEFT
.br_startblock
+ LEFT
.br_blockcount
== new->br_startblock
&&
1469 LEFT
.br_state
== new->br_state
&&
1470 LEFT
.br_blockcount
+ new->br_blockcount
<= XFS_MAX_BMBT_EXTLEN
)
1471 state
|= BMAP_LEFT_CONTIG
;
1474 * Check and set flags if this segment has a right neighbor.
1475 * Don't set contiguous if the combined extent would be too large.
1476 * Also check for all-three-contiguous being too large.
1478 if (xfs_iext_peek_next_extent(ifp
, &bma
->icur
, &RIGHT
)) {
1479 state
|= BMAP_RIGHT_VALID
;
1480 if (isnullstartblock(RIGHT
.br_startblock
))
1481 state
|= BMAP_RIGHT_DELAY
;
1484 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
1485 new_endoff
== RIGHT
.br_startoff
&&
1486 new->br_startblock
+ new->br_blockcount
== RIGHT
.br_startblock
&&
1487 new->br_state
== RIGHT
.br_state
&&
1488 new->br_blockcount
+ RIGHT
.br_blockcount
<= XFS_MAX_BMBT_EXTLEN
&&
1489 ((state
& (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
1490 BMAP_RIGHT_FILLING
)) !=
1491 (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
1492 BMAP_RIGHT_FILLING
) ||
1493 LEFT
.br_blockcount
+ new->br_blockcount
+ RIGHT
.br_blockcount
1494 <= XFS_MAX_BMBT_EXTLEN
))
1495 state
|= BMAP_RIGHT_CONTIG
;
1499 * Switch out based on the FILLING and CONTIG state bits.
1501 switch (state
& (BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
1502 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
)) {
1503 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
1504 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
1506 * Filling in all of a previously delayed allocation extent.
1507 * The left and right neighbors are both contiguous with new.
1509 LEFT
.br_blockcount
+= PREV
.br_blockcount
+ RIGHT
.br_blockcount
;
1511 xfs_iext_remove(bma
->ip
, &bma
->icur
, state
);
1512 xfs_iext_remove(bma
->ip
, &bma
->icur
, state
);
1513 xfs_iext_prev(ifp
, &bma
->icur
);
1514 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &LEFT
);
1517 if (bma
->cur
== NULL
)
1518 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1520 rval
= XFS_ILOG_CORE
;
1521 error
= xfs_bmbt_lookup_eq(bma
->cur
, &RIGHT
, &i
);
1524 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
1525 error
= -EFSCORRUPTED
;
1528 error
= xfs_btree_delete(bma
->cur
, &i
);
1531 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
1532 error
= -EFSCORRUPTED
;
1535 error
= xfs_btree_decrement(bma
->cur
, 0, &i
);
1538 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
1539 error
= -EFSCORRUPTED
;
1542 error
= xfs_bmbt_update(bma
->cur
, &LEFT
);
1548 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
1550 * Filling in all of a previously delayed allocation extent.
1551 * The left neighbor is contiguous, the right is not.
1554 LEFT
.br_blockcount
+= PREV
.br_blockcount
;
1556 xfs_iext_remove(bma
->ip
, &bma
->icur
, state
);
1557 xfs_iext_prev(ifp
, &bma
->icur
);
1558 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &LEFT
);
1560 if (bma
->cur
== NULL
)
1561 rval
= XFS_ILOG_DEXT
;
1564 error
= xfs_bmbt_lookup_eq(bma
->cur
, &old
, &i
);
1567 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
1568 error
= -EFSCORRUPTED
;
1571 error
= xfs_bmbt_update(bma
->cur
, &LEFT
);
1577 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
1579 * Filling in all of a previously delayed allocation extent.
1580 * The right neighbor is contiguous, the left is not. Take care
1581 * with delay -> unwritten extent allocation here because the
1582 * delalloc record we are overwriting is always written.
1584 PREV
.br_startblock
= new->br_startblock
;
1585 PREV
.br_blockcount
+= RIGHT
.br_blockcount
;
1586 PREV
.br_state
= new->br_state
;
1588 xfs_iext_next(ifp
, &bma
->icur
);
1589 xfs_iext_remove(bma
->ip
, &bma
->icur
, state
);
1590 xfs_iext_prev(ifp
, &bma
->icur
);
1591 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &PREV
);
1593 if (bma
->cur
== NULL
)
1594 rval
= XFS_ILOG_DEXT
;
1597 error
= xfs_bmbt_lookup_eq(bma
->cur
, &RIGHT
, &i
);
1600 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
1601 error
= -EFSCORRUPTED
;
1604 error
= xfs_bmbt_update(bma
->cur
, &PREV
);
1610 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
1612 * Filling in all of a previously delayed allocation extent.
1613 * Neither the left nor right neighbors are contiguous with
1616 PREV
.br_startblock
= new->br_startblock
;
1617 PREV
.br_state
= new->br_state
;
1618 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &PREV
);
1621 if (bma
->cur
== NULL
)
1622 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1624 rval
= XFS_ILOG_CORE
;
1625 error
= xfs_bmbt_lookup_eq(bma
->cur
, new, &i
);
1628 if (XFS_IS_CORRUPT(mp
, i
!= 0)) {
1629 error
= -EFSCORRUPTED
;
1632 error
= xfs_btree_insert(bma
->cur
, &i
);
1635 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
1636 error
= -EFSCORRUPTED
;
1642 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
:
1644 * Filling in the first part of a previous delayed allocation.
1645 * The left neighbor is contiguous.
1648 temp
= PREV
.br_blockcount
- new->br_blockcount
;
1649 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
1650 startblockval(PREV
.br_startblock
));
1652 LEFT
.br_blockcount
+= new->br_blockcount
;
1654 PREV
.br_blockcount
= temp
;
1655 PREV
.br_startoff
+= new->br_blockcount
;
1656 PREV
.br_startblock
= nullstartblock(da_new
);
1658 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &PREV
);
1659 xfs_iext_prev(ifp
, &bma
->icur
);
1660 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &LEFT
);
1662 if (bma
->cur
== NULL
)
1663 rval
= XFS_ILOG_DEXT
;
1666 error
= xfs_bmbt_lookup_eq(bma
->cur
, &old
, &i
);
1669 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
1670 error
= -EFSCORRUPTED
;
1673 error
= xfs_bmbt_update(bma
->cur
, &LEFT
);
1679 case BMAP_LEFT_FILLING
:
1681 * Filling in the first part of a previous delayed allocation.
1682 * The left neighbor is not contiguous.
1684 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, new);
1687 if (bma
->cur
== NULL
)
1688 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1690 rval
= XFS_ILOG_CORE
;
1691 error
= xfs_bmbt_lookup_eq(bma
->cur
, new, &i
);
1694 if (XFS_IS_CORRUPT(mp
, i
!= 0)) {
1695 error
= -EFSCORRUPTED
;
1698 error
= xfs_btree_insert(bma
->cur
, &i
);
1701 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
1702 error
= -EFSCORRUPTED
;
1707 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
1708 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
1709 &bma
->cur
, 1, &tmp_rval
, whichfork
);
1715 temp
= PREV
.br_blockcount
- new->br_blockcount
;
1716 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
1717 startblockval(PREV
.br_startblock
) -
1718 (bma
->cur
? bma
->cur
->bc_ino
.allocated
: 0));
1720 PREV
.br_startoff
= new_endoff
;
1721 PREV
.br_blockcount
= temp
;
1722 PREV
.br_startblock
= nullstartblock(da_new
);
1723 xfs_iext_next(ifp
, &bma
->icur
);
1724 xfs_iext_insert(bma
->ip
, &bma
->icur
, &PREV
, state
);
1725 xfs_iext_prev(ifp
, &bma
->icur
);
1728 case BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
1730 * Filling in the last part of a previous delayed allocation.
1731 * The right neighbor is contiguous with the new allocation.
1734 RIGHT
.br_startoff
= new->br_startoff
;
1735 RIGHT
.br_startblock
= new->br_startblock
;
1736 RIGHT
.br_blockcount
+= new->br_blockcount
;
1738 if (bma
->cur
== NULL
)
1739 rval
= XFS_ILOG_DEXT
;
1742 error
= xfs_bmbt_lookup_eq(bma
->cur
, &old
, &i
);
1745 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
1746 error
= -EFSCORRUPTED
;
1749 error
= xfs_bmbt_update(bma
->cur
, &RIGHT
);
1754 temp
= PREV
.br_blockcount
- new->br_blockcount
;
1755 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
1756 startblockval(PREV
.br_startblock
));
1758 PREV
.br_blockcount
= temp
;
1759 PREV
.br_startblock
= nullstartblock(da_new
);
1761 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &PREV
);
1762 xfs_iext_next(ifp
, &bma
->icur
);
1763 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &RIGHT
);
1766 case BMAP_RIGHT_FILLING
:
1768 * Filling in the last part of a previous delayed allocation.
1769 * The right neighbor is not contiguous.
1771 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, new);
1774 if (bma
->cur
== NULL
)
1775 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1777 rval
= XFS_ILOG_CORE
;
1778 error
= xfs_bmbt_lookup_eq(bma
->cur
, new, &i
);
1781 if (XFS_IS_CORRUPT(mp
, i
!= 0)) {
1782 error
= -EFSCORRUPTED
;
1785 error
= xfs_btree_insert(bma
->cur
, &i
);
1788 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
1789 error
= -EFSCORRUPTED
;
1794 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
1795 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
1796 &bma
->cur
, 1, &tmp_rval
, whichfork
);
1802 temp
= PREV
.br_blockcount
- new->br_blockcount
;
1803 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
1804 startblockval(PREV
.br_startblock
) -
1805 (bma
->cur
? bma
->cur
->bc_ino
.allocated
: 0));
1807 PREV
.br_startblock
= nullstartblock(da_new
);
1808 PREV
.br_blockcount
= temp
;
1809 xfs_iext_insert(bma
->ip
, &bma
->icur
, &PREV
, state
);
1810 xfs_iext_next(ifp
, &bma
->icur
);
1815 * Filling in the middle part of a previous delayed allocation.
1816 * Contiguity is impossible here.
1817 * This case is avoided almost all the time.
1819 * We start with a delayed allocation:
1821 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
1824 * and we are allocating:
1825 * +rrrrrrrrrrrrrrrrr+
1828 * and we set it up for insertion as:
1829 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
1831 * PREV @ idx LEFT RIGHT
1832 * inserted at idx + 1
1836 /* LEFT is the new middle */
1839 /* RIGHT is the new right */
1840 RIGHT
.br_state
= PREV
.br_state
;
1841 RIGHT
.br_startoff
= new_endoff
;
1842 RIGHT
.br_blockcount
=
1843 PREV
.br_startoff
+ PREV
.br_blockcount
- new_endoff
;
1844 RIGHT
.br_startblock
=
1845 nullstartblock(xfs_bmap_worst_indlen(bma
->ip
,
1846 RIGHT
.br_blockcount
));
1849 PREV
.br_blockcount
= new->br_startoff
- PREV
.br_startoff
;
1850 PREV
.br_startblock
=
1851 nullstartblock(xfs_bmap_worst_indlen(bma
->ip
,
1852 PREV
.br_blockcount
));
1853 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &PREV
);
1855 xfs_iext_next(ifp
, &bma
->icur
);
1856 xfs_iext_insert(bma
->ip
, &bma
->icur
, &RIGHT
, state
);
1857 xfs_iext_insert(bma
->ip
, &bma
->icur
, &LEFT
, state
);
1860 if (bma
->cur
== NULL
)
1861 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1863 rval
= XFS_ILOG_CORE
;
1864 error
= xfs_bmbt_lookup_eq(bma
->cur
, new, &i
);
1867 if (XFS_IS_CORRUPT(mp
, i
!= 0)) {
1868 error
= -EFSCORRUPTED
;
1871 error
= xfs_btree_insert(bma
->cur
, &i
);
1874 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
1875 error
= -EFSCORRUPTED
;
1880 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
1881 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
1882 &bma
->cur
, 1, &tmp_rval
, whichfork
);
1888 da_new
= startblockval(PREV
.br_startblock
) +
1889 startblockval(RIGHT
.br_startblock
);
1892 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
1893 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
1894 case BMAP_LEFT_FILLING
| BMAP_RIGHT_CONTIG
:
1895 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
1896 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
1897 case BMAP_LEFT_CONTIG
:
1898 case BMAP_RIGHT_CONTIG
:
1900 * These cases are all impossible.
1905 /* add reverse mapping unless caller opted out */
1906 if (!(bma
->flags
& XFS_BMAPI_NORMAP
))
1907 xfs_rmap_map_extent(bma
->tp
, bma
->ip
, whichfork
, new);
1909 /* convert to a btree if necessary */
1910 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
1911 int tmp_logflags
; /* partial log flag return val */
1913 ASSERT(bma
->cur
== NULL
);
1914 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
1915 &bma
->cur
, da_old
> 0, &tmp_logflags
,
1917 bma
->logflags
|= tmp_logflags
;
1922 if (da_new
!= da_old
)
1923 xfs_mod_delalloc(mp
, (int64_t)da_new
- da_old
);
1926 da_new
+= bma
->cur
->bc_ino
.allocated
;
1927 bma
->cur
->bc_ino
.allocated
= 0;
1930 /* adjust for changes in reserved delayed indirect blocks */
1931 if (da_new
!= da_old
) {
1932 ASSERT(state
== 0 || da_new
< da_old
);
1933 error
= xfs_mod_fdblocks(mp
, (int64_t)(da_old
- da_new
),
1937 xfs_bmap_check_leaf_extents(bma
->cur
, bma
->ip
, whichfork
);
1939 if (whichfork
!= XFS_COW_FORK
)
1940 bma
->logflags
|= rval
;
1948 * Convert an unwritten allocation to a real allocation or vice versa.
1951 xfs_bmap_add_extent_unwritten_real(
1952 struct xfs_trans
*tp
,
1953 xfs_inode_t
*ip
, /* incore inode pointer */
1955 struct xfs_iext_cursor
*icur
,
1956 struct xfs_btree_cur
**curp
, /* if *curp is null, not a btree */
1957 xfs_bmbt_irec_t
*new, /* new data to add to file extents */
1958 int *logflagsp
) /* inode logging flags */
1960 struct xfs_btree_cur
*cur
; /* btree cursor */
1961 int error
; /* error return value */
1962 int i
; /* temp state */
1963 struct xfs_ifork
*ifp
; /* inode fork pointer */
1964 xfs_fileoff_t new_endoff
; /* end offset of new entry */
1965 xfs_bmbt_irec_t r
[3]; /* neighbor extent entries */
1966 /* left is 0, right is 1, prev is 2 */
1967 int rval
=0; /* return value (logging flags) */
1968 uint32_t state
= xfs_bmap_fork_to_state(whichfork
);
1969 struct xfs_mount
*mp
= ip
->i_mount
;
1970 struct xfs_bmbt_irec old
;
1975 ifp
= xfs_ifork_ptr(ip
, whichfork
);
1977 ASSERT(!isnullstartblock(new->br_startblock
));
1979 XFS_STATS_INC(mp
, xs_add_exlist
);
1986 * Set up a bunch of variables to make the tests simpler.
1989 xfs_iext_get_extent(ifp
, icur
, &PREV
);
1990 ASSERT(new->br_state
!= PREV
.br_state
);
1991 new_endoff
= new->br_startoff
+ new->br_blockcount
;
1992 ASSERT(PREV
.br_startoff
<= new->br_startoff
);
1993 ASSERT(PREV
.br_startoff
+ PREV
.br_blockcount
>= new_endoff
);
1996 * Set flags determining what part of the previous oldext allocation
1997 * extent is being replaced by a newext allocation.
1999 if (PREV
.br_startoff
== new->br_startoff
)
2000 state
|= BMAP_LEFT_FILLING
;
2001 if (PREV
.br_startoff
+ PREV
.br_blockcount
== new_endoff
)
2002 state
|= BMAP_RIGHT_FILLING
;
2005 * Check and set flags if this segment has a left neighbor.
2006 * Don't set contiguous if the combined extent would be too large.
2008 if (xfs_iext_peek_prev_extent(ifp
, icur
, &LEFT
)) {
2009 state
|= BMAP_LEFT_VALID
;
2010 if (isnullstartblock(LEFT
.br_startblock
))
2011 state
|= BMAP_LEFT_DELAY
;
2014 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
2015 LEFT
.br_startoff
+ LEFT
.br_blockcount
== new->br_startoff
&&
2016 LEFT
.br_startblock
+ LEFT
.br_blockcount
== new->br_startblock
&&
2017 LEFT
.br_state
== new->br_state
&&
2018 LEFT
.br_blockcount
+ new->br_blockcount
<= XFS_MAX_BMBT_EXTLEN
)
2019 state
|= BMAP_LEFT_CONTIG
;
2022 * Check and set flags if this segment has a right neighbor.
2023 * Don't set contiguous if the combined extent would be too large.
2024 * Also check for all-three-contiguous being too large.
2026 if (xfs_iext_peek_next_extent(ifp
, icur
, &RIGHT
)) {
2027 state
|= BMAP_RIGHT_VALID
;
2028 if (isnullstartblock(RIGHT
.br_startblock
))
2029 state
|= BMAP_RIGHT_DELAY
;
2032 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
2033 new_endoff
== RIGHT
.br_startoff
&&
2034 new->br_startblock
+ new->br_blockcount
== RIGHT
.br_startblock
&&
2035 new->br_state
== RIGHT
.br_state
&&
2036 new->br_blockcount
+ RIGHT
.br_blockcount
<= XFS_MAX_BMBT_EXTLEN
&&
2037 ((state
& (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
2038 BMAP_RIGHT_FILLING
)) !=
2039 (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
2040 BMAP_RIGHT_FILLING
) ||
2041 LEFT
.br_blockcount
+ new->br_blockcount
+ RIGHT
.br_blockcount
2042 <= XFS_MAX_BMBT_EXTLEN
))
2043 state
|= BMAP_RIGHT_CONTIG
;
2046 * Switch out based on the FILLING and CONTIG state bits.
2048 switch (state
& (BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
2049 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
)) {
2050 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
2051 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2053 * Setting all of a previous oldext extent to newext.
2054 * The left and right neighbors are both contiguous with new.
2056 LEFT
.br_blockcount
+= PREV
.br_blockcount
+ RIGHT
.br_blockcount
;
2058 xfs_iext_remove(ip
, icur
, state
);
2059 xfs_iext_remove(ip
, icur
, state
);
2060 xfs_iext_prev(ifp
, icur
);
2061 xfs_iext_update_extent(ip
, state
, icur
, &LEFT
);
2062 ifp
->if_nextents
-= 2;
2064 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2066 rval
= XFS_ILOG_CORE
;
2067 error
= xfs_bmbt_lookup_eq(cur
, &RIGHT
, &i
);
2070 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
2071 error
= -EFSCORRUPTED
;
2074 if ((error
= xfs_btree_delete(cur
, &i
)))
2076 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
2077 error
= -EFSCORRUPTED
;
2080 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2082 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
2083 error
= -EFSCORRUPTED
;
2086 if ((error
= xfs_btree_delete(cur
, &i
)))
2088 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
2089 error
= -EFSCORRUPTED
;
2092 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2094 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
2095 error
= -EFSCORRUPTED
;
2098 error
= xfs_bmbt_update(cur
, &LEFT
);
2104 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
2106 * Setting all of a previous oldext extent to newext.
2107 * The left neighbor is contiguous, the right is not.
2109 LEFT
.br_blockcount
+= PREV
.br_blockcount
;
2111 xfs_iext_remove(ip
, icur
, state
);
2112 xfs_iext_prev(ifp
, icur
);
2113 xfs_iext_update_extent(ip
, state
, icur
, &LEFT
);
2116 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2118 rval
= XFS_ILOG_CORE
;
2119 error
= xfs_bmbt_lookup_eq(cur
, &PREV
, &i
);
2122 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
2123 error
= -EFSCORRUPTED
;
2126 if ((error
= xfs_btree_delete(cur
, &i
)))
2128 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
2129 error
= -EFSCORRUPTED
;
2132 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2134 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
2135 error
= -EFSCORRUPTED
;
2138 error
= xfs_bmbt_update(cur
, &LEFT
);
2144 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2146 * Setting all of a previous oldext extent to newext.
2147 * The right neighbor is contiguous, the left is not.
2149 PREV
.br_blockcount
+= RIGHT
.br_blockcount
;
2150 PREV
.br_state
= new->br_state
;
2152 xfs_iext_next(ifp
, icur
);
2153 xfs_iext_remove(ip
, icur
, state
);
2154 xfs_iext_prev(ifp
, icur
);
2155 xfs_iext_update_extent(ip
, state
, icur
, &PREV
);
2159 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2161 rval
= XFS_ILOG_CORE
;
2162 error
= xfs_bmbt_lookup_eq(cur
, &RIGHT
, &i
);
2165 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
2166 error
= -EFSCORRUPTED
;
2169 if ((error
= xfs_btree_delete(cur
, &i
)))
2171 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
2172 error
= -EFSCORRUPTED
;
2175 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2177 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
2178 error
= -EFSCORRUPTED
;
2181 error
= xfs_bmbt_update(cur
, &PREV
);
2187 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
2189 * Setting all of a previous oldext extent to newext.
2190 * Neither the left nor right neighbors are contiguous with
2193 PREV
.br_state
= new->br_state
;
2194 xfs_iext_update_extent(ip
, state
, icur
, &PREV
);
2197 rval
= XFS_ILOG_DEXT
;
2200 error
= xfs_bmbt_lookup_eq(cur
, new, &i
);
2203 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
2204 error
= -EFSCORRUPTED
;
2207 error
= xfs_bmbt_update(cur
, &PREV
);
2213 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
:
2215 * Setting the first part of a previous oldext extent to newext.
2216 * The left neighbor is contiguous.
2218 LEFT
.br_blockcount
+= new->br_blockcount
;
2221 PREV
.br_startoff
+= new->br_blockcount
;
2222 PREV
.br_startblock
+= new->br_blockcount
;
2223 PREV
.br_blockcount
-= new->br_blockcount
;
2225 xfs_iext_update_extent(ip
, state
, icur
, &PREV
);
2226 xfs_iext_prev(ifp
, icur
);
2227 xfs_iext_update_extent(ip
, state
, icur
, &LEFT
);
2230 rval
= XFS_ILOG_DEXT
;
2233 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2236 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
2237 error
= -EFSCORRUPTED
;
2240 error
= xfs_bmbt_update(cur
, &PREV
);
2243 error
= xfs_btree_decrement(cur
, 0, &i
);
2246 error
= xfs_bmbt_update(cur
, &LEFT
);
2252 case BMAP_LEFT_FILLING
:
2254 * Setting the first part of a previous oldext extent to newext.
2255 * The left neighbor is not contiguous.
2258 PREV
.br_startoff
+= new->br_blockcount
;
2259 PREV
.br_startblock
+= new->br_blockcount
;
2260 PREV
.br_blockcount
-= new->br_blockcount
;
2262 xfs_iext_update_extent(ip
, state
, icur
, &PREV
);
2263 xfs_iext_insert(ip
, icur
, new, state
);
2267 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2269 rval
= XFS_ILOG_CORE
;
2270 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2273 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
2274 error
= -EFSCORRUPTED
;
2277 error
= xfs_bmbt_update(cur
, &PREV
);
2280 cur
->bc_rec
.b
= *new;
2281 if ((error
= xfs_btree_insert(cur
, &i
)))
2283 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
2284 error
= -EFSCORRUPTED
;
2290 case BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2292 * Setting the last part of a previous oldext extent to newext.
2293 * The right neighbor is contiguous with the new allocation.
2296 PREV
.br_blockcount
-= new->br_blockcount
;
2298 RIGHT
.br_startoff
= new->br_startoff
;
2299 RIGHT
.br_startblock
= new->br_startblock
;
2300 RIGHT
.br_blockcount
+= new->br_blockcount
;
2302 xfs_iext_update_extent(ip
, state
, icur
, &PREV
);
2303 xfs_iext_next(ifp
, icur
);
2304 xfs_iext_update_extent(ip
, state
, icur
, &RIGHT
);
2307 rval
= XFS_ILOG_DEXT
;
2310 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2313 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
2314 error
= -EFSCORRUPTED
;
2317 error
= xfs_bmbt_update(cur
, &PREV
);
2320 error
= xfs_btree_increment(cur
, 0, &i
);
2323 error
= xfs_bmbt_update(cur
, &RIGHT
);
2329 case BMAP_RIGHT_FILLING
:
2331 * Setting the last part of a previous oldext extent to newext.
2332 * The right neighbor is not contiguous.
2335 PREV
.br_blockcount
-= new->br_blockcount
;
2337 xfs_iext_update_extent(ip
, state
, icur
, &PREV
);
2338 xfs_iext_next(ifp
, icur
);
2339 xfs_iext_insert(ip
, icur
, new, state
);
2343 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2345 rval
= XFS_ILOG_CORE
;
2346 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2349 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
2350 error
= -EFSCORRUPTED
;
2353 error
= xfs_bmbt_update(cur
, &PREV
);
2356 error
= xfs_bmbt_lookup_eq(cur
, new, &i
);
2359 if (XFS_IS_CORRUPT(mp
, i
!= 0)) {
2360 error
= -EFSCORRUPTED
;
2363 if ((error
= xfs_btree_insert(cur
, &i
)))
2365 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
2366 error
= -EFSCORRUPTED
;
2374 * Setting the middle part of a previous oldext extent to
2375 * newext. Contiguity is impossible here.
2376 * One extent becomes three extents.
2379 PREV
.br_blockcount
= new->br_startoff
- PREV
.br_startoff
;
2382 r
[1].br_startoff
= new_endoff
;
2383 r
[1].br_blockcount
=
2384 old
.br_startoff
+ old
.br_blockcount
- new_endoff
;
2385 r
[1].br_startblock
= new->br_startblock
+ new->br_blockcount
;
2386 r
[1].br_state
= PREV
.br_state
;
2388 xfs_iext_update_extent(ip
, state
, icur
, &PREV
);
2389 xfs_iext_next(ifp
, icur
);
2390 xfs_iext_insert(ip
, icur
, &r
[1], state
);
2391 xfs_iext_insert(ip
, icur
, &r
[0], state
);
2392 ifp
->if_nextents
+= 2;
2395 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2397 rval
= XFS_ILOG_CORE
;
2398 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2401 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
2402 error
= -EFSCORRUPTED
;
2405 /* new right extent - oldext */
2406 error
= xfs_bmbt_update(cur
, &r
[1]);
2409 /* new left extent - oldext */
2410 cur
->bc_rec
.b
= PREV
;
2411 if ((error
= xfs_btree_insert(cur
, &i
)))
2413 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
2414 error
= -EFSCORRUPTED
;
2418 * Reset the cursor to the position of the new extent
2419 * we are about to insert as we can't trust it after
2420 * the previous insert.
2422 error
= xfs_bmbt_lookup_eq(cur
, new, &i
);
2425 if (XFS_IS_CORRUPT(mp
, i
!= 0)) {
2426 error
= -EFSCORRUPTED
;
2429 /* new middle extent - newext */
2430 if ((error
= xfs_btree_insert(cur
, &i
)))
2432 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
2433 error
= -EFSCORRUPTED
;
2439 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2440 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2441 case BMAP_LEFT_FILLING
| BMAP_RIGHT_CONTIG
:
2442 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
2443 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2444 case BMAP_LEFT_CONTIG
:
2445 case BMAP_RIGHT_CONTIG
:
2447 * These cases are all impossible.
2452 /* update reverse mappings */
2453 xfs_rmap_convert_extent(mp
, tp
, ip
, whichfork
, new);
2455 /* convert to a btree if necessary */
2456 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
2457 int tmp_logflags
; /* partial log flag return val */
2459 ASSERT(cur
== NULL
);
2460 error
= xfs_bmap_extents_to_btree(tp
, ip
, &cur
, 0,
2461 &tmp_logflags
, whichfork
);
2462 *logflagsp
|= tmp_logflags
;
2467 /* clear out the allocated field, done with it now in any case. */
2469 cur
->bc_ino
.allocated
= 0;
2473 xfs_bmap_check_leaf_extents(*curp
, ip
, whichfork
);
2483 * Convert a hole to a delayed allocation.
2486 xfs_bmap_add_extent_hole_delay(
2487 xfs_inode_t
*ip
, /* incore inode pointer */
2489 struct xfs_iext_cursor
*icur
,
2490 xfs_bmbt_irec_t
*new) /* new data to add to file extents */
2492 struct xfs_ifork
*ifp
; /* inode fork pointer */
2493 xfs_bmbt_irec_t left
; /* left neighbor extent entry */
2494 xfs_filblks_t newlen
=0; /* new indirect size */
2495 xfs_filblks_t oldlen
=0; /* old indirect size */
2496 xfs_bmbt_irec_t right
; /* right neighbor extent entry */
2497 uint32_t state
= xfs_bmap_fork_to_state(whichfork
);
2498 xfs_filblks_t temp
; /* temp for indirect calculations */
2500 ifp
= xfs_ifork_ptr(ip
, whichfork
);
2501 ASSERT(isnullstartblock(new->br_startblock
));
2504 * Check and set flags if this segment has a left neighbor
2506 if (xfs_iext_peek_prev_extent(ifp
, icur
, &left
)) {
2507 state
|= BMAP_LEFT_VALID
;
2508 if (isnullstartblock(left
.br_startblock
))
2509 state
|= BMAP_LEFT_DELAY
;
2513 * Check and set flags if the current (right) segment exists.
2514 * If it doesn't exist, we're converting the hole at end-of-file.
2516 if (xfs_iext_get_extent(ifp
, icur
, &right
)) {
2517 state
|= BMAP_RIGHT_VALID
;
2518 if (isnullstartblock(right
.br_startblock
))
2519 state
|= BMAP_RIGHT_DELAY
;
2523 * Set contiguity flags on the left and right neighbors.
2524 * Don't let extents get too large, even if the pieces are contiguous.
2526 if ((state
& BMAP_LEFT_VALID
) && (state
& BMAP_LEFT_DELAY
) &&
2527 left
.br_startoff
+ left
.br_blockcount
== new->br_startoff
&&
2528 left
.br_blockcount
+ new->br_blockcount
<= XFS_MAX_BMBT_EXTLEN
)
2529 state
|= BMAP_LEFT_CONTIG
;
2531 if ((state
& BMAP_RIGHT_VALID
) && (state
& BMAP_RIGHT_DELAY
) &&
2532 new->br_startoff
+ new->br_blockcount
== right
.br_startoff
&&
2533 new->br_blockcount
+ right
.br_blockcount
<= XFS_MAX_BMBT_EXTLEN
&&
2534 (!(state
& BMAP_LEFT_CONTIG
) ||
2535 (left
.br_blockcount
+ new->br_blockcount
+
2536 right
.br_blockcount
<= XFS_MAX_BMBT_EXTLEN
)))
2537 state
|= BMAP_RIGHT_CONTIG
;
2540 * Switch out based on the contiguity flags.
2542 switch (state
& (BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
)) {
2543 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2545 * New allocation is contiguous with delayed allocations
2546 * on the left and on the right.
2547 * Merge all three into a single extent record.
2549 temp
= left
.br_blockcount
+ new->br_blockcount
+
2550 right
.br_blockcount
;
2552 oldlen
= startblockval(left
.br_startblock
) +
2553 startblockval(new->br_startblock
) +
2554 startblockval(right
.br_startblock
);
2555 newlen
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
2557 left
.br_startblock
= nullstartblock(newlen
);
2558 left
.br_blockcount
= temp
;
2560 xfs_iext_remove(ip
, icur
, state
);
2561 xfs_iext_prev(ifp
, icur
);
2562 xfs_iext_update_extent(ip
, state
, icur
, &left
);
2565 case BMAP_LEFT_CONTIG
:
2567 * New allocation is contiguous with a delayed allocation
2569 * Merge the new allocation with the left neighbor.
2571 temp
= left
.br_blockcount
+ new->br_blockcount
;
2573 oldlen
= startblockval(left
.br_startblock
) +
2574 startblockval(new->br_startblock
);
2575 newlen
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
2577 left
.br_blockcount
= temp
;
2578 left
.br_startblock
= nullstartblock(newlen
);
2580 xfs_iext_prev(ifp
, icur
);
2581 xfs_iext_update_extent(ip
, state
, icur
, &left
);
2584 case BMAP_RIGHT_CONTIG
:
2586 * New allocation is contiguous with a delayed allocation
2588 * Merge the new allocation with the right neighbor.
2590 temp
= new->br_blockcount
+ right
.br_blockcount
;
2591 oldlen
= startblockval(new->br_startblock
) +
2592 startblockval(right
.br_startblock
);
2593 newlen
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
2595 right
.br_startoff
= new->br_startoff
;
2596 right
.br_startblock
= nullstartblock(newlen
);
2597 right
.br_blockcount
= temp
;
2598 xfs_iext_update_extent(ip
, state
, icur
, &right
);
2603 * New allocation is not contiguous with another
2604 * delayed allocation.
2605 * Insert a new entry.
2607 oldlen
= newlen
= 0;
2608 xfs_iext_insert(ip
, icur
, new, state
);
2611 if (oldlen
!= newlen
) {
2612 ASSERT(oldlen
> newlen
);
2613 xfs_mod_fdblocks(ip
->i_mount
, (int64_t)(oldlen
- newlen
),
2616 * Nothing to do for disk quota accounting here.
2618 xfs_mod_delalloc(ip
->i_mount
, (int64_t)newlen
- oldlen
);
2623 * Convert a hole to a real allocation.
2625 STATIC
int /* error */
2626 xfs_bmap_add_extent_hole_real(
2627 struct xfs_trans
*tp
,
2628 struct xfs_inode
*ip
,
2630 struct xfs_iext_cursor
*icur
,
2631 struct xfs_btree_cur
**curp
,
2632 struct xfs_bmbt_irec
*new,
2636 struct xfs_ifork
*ifp
= xfs_ifork_ptr(ip
, whichfork
);
2637 struct xfs_mount
*mp
= ip
->i_mount
;
2638 struct xfs_btree_cur
*cur
= *curp
;
2639 int error
; /* error return value */
2640 int i
; /* temp state */
2641 xfs_bmbt_irec_t left
; /* left neighbor extent entry */
2642 xfs_bmbt_irec_t right
; /* right neighbor extent entry */
2643 int rval
=0; /* return value (logging flags) */
2644 uint32_t state
= xfs_bmap_fork_to_state(whichfork
);
2645 struct xfs_bmbt_irec old
;
2647 ASSERT(!isnullstartblock(new->br_startblock
));
2648 ASSERT(!cur
|| !(cur
->bc_ino
.flags
& XFS_BTCUR_BMBT_WASDEL
));
2650 XFS_STATS_INC(mp
, xs_add_exlist
);
2653 * Check and set flags if this segment has a left neighbor.
2655 if (xfs_iext_peek_prev_extent(ifp
, icur
, &left
)) {
2656 state
|= BMAP_LEFT_VALID
;
2657 if (isnullstartblock(left
.br_startblock
))
2658 state
|= BMAP_LEFT_DELAY
;
2662 * Check and set flags if this segment has a current value.
2663 * Not true if we're inserting into the "hole" at eof.
2665 if (xfs_iext_get_extent(ifp
, icur
, &right
)) {
2666 state
|= BMAP_RIGHT_VALID
;
2667 if (isnullstartblock(right
.br_startblock
))
2668 state
|= BMAP_RIGHT_DELAY
;
2672 * We're inserting a real allocation between "left" and "right".
2673 * Set the contiguity flags. Don't let extents get too large.
2675 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
2676 left
.br_startoff
+ left
.br_blockcount
== new->br_startoff
&&
2677 left
.br_startblock
+ left
.br_blockcount
== new->br_startblock
&&
2678 left
.br_state
== new->br_state
&&
2679 left
.br_blockcount
+ new->br_blockcount
<= XFS_MAX_BMBT_EXTLEN
)
2680 state
|= BMAP_LEFT_CONTIG
;
2682 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
2683 new->br_startoff
+ new->br_blockcount
== right
.br_startoff
&&
2684 new->br_startblock
+ new->br_blockcount
== right
.br_startblock
&&
2685 new->br_state
== right
.br_state
&&
2686 new->br_blockcount
+ right
.br_blockcount
<= XFS_MAX_BMBT_EXTLEN
&&
2687 (!(state
& BMAP_LEFT_CONTIG
) ||
2688 left
.br_blockcount
+ new->br_blockcount
+
2689 right
.br_blockcount
<= XFS_MAX_BMBT_EXTLEN
))
2690 state
|= BMAP_RIGHT_CONTIG
;
2694 * Select which case we're in here, and implement it.
2696 switch (state
& (BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
)) {
2697 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2699 * New allocation is contiguous with real allocations on the
2700 * left and on the right.
2701 * Merge all three into a single extent record.
2703 left
.br_blockcount
+= new->br_blockcount
+ right
.br_blockcount
;
2705 xfs_iext_remove(ip
, icur
, state
);
2706 xfs_iext_prev(ifp
, icur
);
2707 xfs_iext_update_extent(ip
, state
, icur
, &left
);
2711 rval
= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
2713 rval
= XFS_ILOG_CORE
;
2714 error
= xfs_bmbt_lookup_eq(cur
, &right
, &i
);
2717 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
2718 error
= -EFSCORRUPTED
;
2721 error
= xfs_btree_delete(cur
, &i
);
2724 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
2725 error
= -EFSCORRUPTED
;
2728 error
= xfs_btree_decrement(cur
, 0, &i
);
2731 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
2732 error
= -EFSCORRUPTED
;
2735 error
= xfs_bmbt_update(cur
, &left
);
2741 case BMAP_LEFT_CONTIG
:
2743 * New allocation is contiguous with a real allocation
2745 * Merge the new allocation with the left neighbor.
2748 left
.br_blockcount
+= new->br_blockcount
;
2750 xfs_iext_prev(ifp
, icur
);
2751 xfs_iext_update_extent(ip
, state
, icur
, &left
);
2754 rval
= xfs_ilog_fext(whichfork
);
2757 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2760 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
2761 error
= -EFSCORRUPTED
;
2764 error
= xfs_bmbt_update(cur
, &left
);
2770 case BMAP_RIGHT_CONTIG
:
2772 * New allocation is contiguous with a real allocation
2774 * Merge the new allocation with the right neighbor.
2778 right
.br_startoff
= new->br_startoff
;
2779 right
.br_startblock
= new->br_startblock
;
2780 right
.br_blockcount
+= new->br_blockcount
;
2781 xfs_iext_update_extent(ip
, state
, icur
, &right
);
2784 rval
= xfs_ilog_fext(whichfork
);
2787 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2790 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
2791 error
= -EFSCORRUPTED
;
2794 error
= xfs_bmbt_update(cur
, &right
);
2802 * New allocation is not contiguous with another
2804 * Insert a new entry.
2806 xfs_iext_insert(ip
, icur
, new, state
);
2810 rval
= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
2812 rval
= XFS_ILOG_CORE
;
2813 error
= xfs_bmbt_lookup_eq(cur
, new, &i
);
2816 if (XFS_IS_CORRUPT(mp
, i
!= 0)) {
2817 error
= -EFSCORRUPTED
;
2820 error
= xfs_btree_insert(cur
, &i
);
2823 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
2824 error
= -EFSCORRUPTED
;
2831 /* add reverse mapping unless caller opted out */
2832 if (!(flags
& XFS_BMAPI_NORMAP
))
2833 xfs_rmap_map_extent(tp
, ip
, whichfork
, new);
2835 /* convert to a btree if necessary */
2836 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
2837 int tmp_logflags
; /* partial log flag return val */
2839 ASSERT(cur
== NULL
);
2840 error
= xfs_bmap_extents_to_btree(tp
, ip
, curp
, 0,
2841 &tmp_logflags
, whichfork
);
2842 *logflagsp
|= tmp_logflags
;
2848 /* clear out the allocated field, done with it now in any case. */
2850 cur
->bc_ino
.allocated
= 0;
2852 xfs_bmap_check_leaf_extents(cur
, ip
, whichfork
);
2859 * Functions used in the extent read, allocate and remove paths
2863 * Adjust the size of the new extent based on i_extsize and rt extsize.
2866 xfs_bmap_extsize_align(
2868 xfs_bmbt_irec_t
*gotp
, /* next extent pointer */
2869 xfs_bmbt_irec_t
*prevp
, /* previous extent pointer */
2870 xfs_extlen_t extsz
, /* align to this extent size */
2871 int rt
, /* is this a realtime inode? */
2872 int eof
, /* is extent at end-of-file? */
2873 int delay
, /* creating delalloc extent? */
2874 int convert
, /* overwriting unwritten extent? */
2875 xfs_fileoff_t
*offp
, /* in/out: aligned offset */
2876 xfs_extlen_t
*lenp
) /* in/out: aligned length */
2878 xfs_fileoff_t orig_off
; /* original offset */
2879 xfs_extlen_t orig_alen
; /* original length */
2880 xfs_fileoff_t orig_end
; /* original off+len */
2881 xfs_fileoff_t nexto
; /* next file offset */
2882 xfs_fileoff_t prevo
; /* previous file offset */
2883 xfs_fileoff_t align_off
; /* temp for offset */
2884 xfs_extlen_t align_alen
; /* temp for length */
2885 xfs_extlen_t temp
; /* temp for calculations */
2890 orig_off
= align_off
= *offp
;
2891 orig_alen
= align_alen
= *lenp
;
2892 orig_end
= orig_off
+ orig_alen
;
2895 * If this request overlaps an existing extent, then don't
2896 * attempt to perform any additional alignment.
2898 if (!delay
&& !eof
&&
2899 (orig_off
>= gotp
->br_startoff
) &&
2900 (orig_end
<= gotp
->br_startoff
+ gotp
->br_blockcount
)) {
2905 * If the file offset is unaligned vs. the extent size
2906 * we need to align it. This will be possible unless
2907 * the file was previously written with a kernel that didn't
2908 * perform this alignment, or if a truncate shot us in the
2911 div_u64_rem(orig_off
, extsz
, &temp
);
2917 /* Same adjustment for the end of the requested area. */
2918 temp
= (align_alen
% extsz
);
2920 align_alen
+= extsz
- temp
;
2923 * For large extent hint sizes, the aligned extent might be larger than
2924 * XFS_BMBT_MAX_EXTLEN. In that case, reduce the size by an extsz so
2925 * that it pulls the length back under XFS_BMBT_MAX_EXTLEN. The outer
2926 * allocation loops handle short allocation just fine, so it is safe to
2927 * do this. We only want to do it when we are forced to, though, because
2928 * it means more allocation operations are required.
2930 while (align_alen
> XFS_MAX_BMBT_EXTLEN
)
2931 align_alen
-= extsz
;
2932 ASSERT(align_alen
<= XFS_MAX_BMBT_EXTLEN
);
2935 * If the previous block overlaps with this proposed allocation
2936 * then move the start forward without adjusting the length.
2938 if (prevp
->br_startoff
!= NULLFILEOFF
) {
2939 if (prevp
->br_startblock
== HOLESTARTBLOCK
)
2940 prevo
= prevp
->br_startoff
;
2942 prevo
= prevp
->br_startoff
+ prevp
->br_blockcount
;
2945 if (align_off
!= orig_off
&& align_off
< prevo
)
2948 * If the next block overlaps with this proposed allocation
2949 * then move the start back without adjusting the length,
2950 * but not before offset 0.
2951 * This may of course make the start overlap previous block,
2952 * and if we hit the offset 0 limit then the next block
2953 * can still overlap too.
2955 if (!eof
&& gotp
->br_startoff
!= NULLFILEOFF
) {
2956 if ((delay
&& gotp
->br_startblock
== HOLESTARTBLOCK
) ||
2957 (!delay
&& gotp
->br_startblock
== DELAYSTARTBLOCK
))
2958 nexto
= gotp
->br_startoff
+ gotp
->br_blockcount
;
2960 nexto
= gotp
->br_startoff
;
2962 nexto
= NULLFILEOFF
;
2964 align_off
+ align_alen
!= orig_end
&&
2965 align_off
+ align_alen
> nexto
)
2966 align_off
= nexto
> align_alen
? nexto
- align_alen
: 0;
2968 * If we're now overlapping the next or previous extent that
2969 * means we can't fit an extsz piece in this hole. Just move
2970 * the start forward to the first valid spot and set
2971 * the length so we hit the end.
2973 if (align_off
!= orig_off
&& align_off
< prevo
)
2975 if (align_off
+ align_alen
!= orig_end
&&
2976 align_off
+ align_alen
> nexto
&&
2977 nexto
!= NULLFILEOFF
) {
2978 ASSERT(nexto
> prevo
);
2979 align_alen
= nexto
- align_off
;
2983 * If realtime, and the result isn't a multiple of the realtime
2984 * extent size we need to remove blocks until it is.
2986 if (rt
&& (temp
= xfs_extlen_to_rtxmod(mp
, align_alen
))) {
2988 * We're not covering the original request, or
2989 * we won't be able to once we fix the length.
2991 if (orig_off
< align_off
||
2992 orig_end
> align_off
+ align_alen
||
2993 align_alen
- temp
< orig_alen
)
2996 * Try to fix it by moving the start up.
2998 if (align_off
+ temp
<= orig_off
) {
3003 * Try to fix it by moving the end in.
3005 else if (align_off
+ align_alen
- temp
>= orig_end
)
3008 * Set the start to the minimum then trim the length.
3011 align_alen
-= orig_off
- align_off
;
3012 align_off
= orig_off
;
3013 align_alen
-= xfs_extlen_to_rtxmod(mp
, align_alen
);
3016 * Result doesn't cover the request, fail it.
3018 if (orig_off
< align_off
|| orig_end
> align_off
+ align_alen
)
3021 ASSERT(orig_off
>= align_off
);
3022 /* see XFS_BMBT_MAX_EXTLEN handling above */
3023 ASSERT(orig_end
<= align_off
+ align_alen
||
3024 align_alen
+ extsz
> XFS_MAX_BMBT_EXTLEN
);
3028 if (!eof
&& gotp
->br_startoff
!= NULLFILEOFF
)
3029 ASSERT(align_off
+ align_alen
<= gotp
->br_startoff
);
3030 if (prevp
->br_startoff
!= NULLFILEOFF
)
3031 ASSERT(align_off
>= prevp
->br_startoff
+ prevp
->br_blockcount
);
3039 #define XFS_ALLOC_GAP_UNITS 4
3043 struct xfs_bmalloca
*ap
) /* bmap alloc argument struct */
3045 xfs_fsblock_t adjust
; /* adjustment to block numbers */
3046 xfs_mount_t
*mp
; /* mount point structure */
3047 int rt
; /* true if inode is realtime */
3049 #define ISVALID(x,y) \
3051 (x) < mp->m_sb.sb_rblocks : \
3052 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
3053 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
3054 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
3056 mp
= ap
->ip
->i_mount
;
3057 rt
= XFS_IS_REALTIME_INODE(ap
->ip
) &&
3058 (ap
->datatype
& XFS_ALLOC_USERDATA
);
3060 * If allocating at eof, and there's a previous real block,
3061 * try to use its last block as our starting point.
3063 if (ap
->eof
&& ap
->prev
.br_startoff
!= NULLFILEOFF
&&
3064 !isnullstartblock(ap
->prev
.br_startblock
) &&
3065 ISVALID(ap
->prev
.br_startblock
+ ap
->prev
.br_blockcount
,
3066 ap
->prev
.br_startblock
)) {
3067 ap
->blkno
= ap
->prev
.br_startblock
+ ap
->prev
.br_blockcount
;
3069 * Adjust for the gap between prevp and us.
3071 adjust
= ap
->offset
-
3072 (ap
->prev
.br_startoff
+ ap
->prev
.br_blockcount
);
3074 ISVALID(ap
->blkno
+ adjust
, ap
->prev
.br_startblock
))
3075 ap
->blkno
+= adjust
;
3078 * If not at eof, then compare the two neighbor blocks.
3079 * Figure out whether either one gives us a good starting point,
3080 * and pick the better one.
3082 else if (!ap
->eof
) {
3083 xfs_fsblock_t gotbno
; /* right side block number */
3084 xfs_fsblock_t gotdiff
=0; /* right side difference */
3085 xfs_fsblock_t prevbno
; /* left side block number */
3086 xfs_fsblock_t prevdiff
=0; /* left side difference */
3089 * If there's a previous (left) block, select a requested
3090 * start block based on it.
3092 if (ap
->prev
.br_startoff
!= NULLFILEOFF
&&
3093 !isnullstartblock(ap
->prev
.br_startblock
) &&
3094 (prevbno
= ap
->prev
.br_startblock
+
3095 ap
->prev
.br_blockcount
) &&
3096 ISVALID(prevbno
, ap
->prev
.br_startblock
)) {
3098 * Calculate gap to end of previous block.
3100 adjust
= prevdiff
= ap
->offset
-
3101 (ap
->prev
.br_startoff
+
3102 ap
->prev
.br_blockcount
);
3104 * Figure the startblock based on the previous block's
3105 * end and the gap size.
3107 * If the gap is large relative to the piece we're
3108 * allocating, or using it gives us an invalid block
3109 * number, then just use the end of the previous block.
3111 if (prevdiff
<= XFS_ALLOC_GAP_UNITS
* ap
->length
&&
3112 ISVALID(prevbno
+ prevdiff
,
3113 ap
->prev
.br_startblock
))
3119 * No previous block or can't follow it, just default.
3122 prevbno
= NULLFSBLOCK
;
3124 * If there's a following (right) block, select a requested
3125 * start block based on it.
3127 if (!isnullstartblock(ap
->got
.br_startblock
)) {
3129 * Calculate gap to start of next block.
3131 adjust
= gotdiff
= ap
->got
.br_startoff
- ap
->offset
;
3133 * Figure the startblock based on the next block's
3134 * start and the gap size.
3136 gotbno
= ap
->got
.br_startblock
;
3139 * If the gap is large relative to the piece we're
3140 * allocating, or using it gives us an invalid block
3141 * number, then just use the start of the next block
3142 * offset by our length.
3144 if (gotdiff
<= XFS_ALLOC_GAP_UNITS
* ap
->length
&&
3145 ISVALID(gotbno
- gotdiff
, gotbno
))
3147 else if (ISVALID(gotbno
- ap
->length
, gotbno
)) {
3148 gotbno
-= ap
->length
;
3149 gotdiff
+= adjust
- ap
->length
;
3154 * No next block, just default.
3157 gotbno
= NULLFSBLOCK
;
3159 * If both valid, pick the better one, else the only good
3160 * one, else ap->blkno is already set (to 0 or the inode block).
3162 if (prevbno
!= NULLFSBLOCK
&& gotbno
!= NULLFSBLOCK
)
3163 ap
->blkno
= prevdiff
<= gotdiff
? prevbno
: gotbno
;
3164 else if (prevbno
!= NULLFSBLOCK
)
3165 ap
->blkno
= prevbno
;
3166 else if (gotbno
!= NULLFSBLOCK
)
3173 xfs_bmap_longest_free_extent(
3174 struct xfs_perag
*pag
,
3175 struct xfs_trans
*tp
,
3178 xfs_extlen_t longest
;
3181 if (!xfs_perag_initialised_agf(pag
)) {
3182 error
= xfs_alloc_read_agf(pag
, tp
, XFS_ALLOC_FLAG_TRYLOCK
,
3188 longest
= xfs_alloc_longest_free_extent(pag
,
3189 xfs_alloc_min_freelist(pag
->pag_mount
, pag
),
3190 xfs_ag_resv_needed(pag
, XFS_AG_RESV_NONE
));
3191 if (*blen
< longest
)
3198 xfs_bmap_select_minlen(
3199 struct xfs_bmalloca
*ap
,
3200 struct xfs_alloc_arg
*args
,
3205 * Since we used XFS_ALLOC_FLAG_TRYLOCK in _longest_free_extent(), it is
3206 * possible that there is enough contiguous free space for this request.
3208 if (blen
< ap
->minlen
)
3212 * If the best seen length is less than the request length,
3213 * use the best as the minimum, otherwise we've got the maxlen we
3216 if (blen
< args
->maxlen
)
3218 return args
->maxlen
;
3222 xfs_bmap_btalloc_select_lengths(
3223 struct xfs_bmalloca
*ap
,
3224 struct xfs_alloc_arg
*args
,
3227 struct xfs_mount
*mp
= args
->mp
;
3228 struct xfs_perag
*pag
;
3229 xfs_agnumber_t agno
, startag
;
3232 if (ap
->tp
->t_flags
& XFS_TRANS_LOWMODE
) {
3233 args
->total
= ap
->minlen
;
3234 args
->minlen
= ap
->minlen
;
3238 args
->total
= ap
->total
;
3239 startag
= XFS_FSB_TO_AGNO(mp
, ap
->blkno
);
3240 if (startag
== NULLAGNUMBER
)
3244 for_each_perag_wrap(mp
, startag
, agno
, pag
) {
3245 error
= xfs_bmap_longest_free_extent(pag
, args
->tp
, blen
);
3246 if (error
&& error
!= -EAGAIN
)
3249 if (*blen
>= args
->maxlen
)
3253 xfs_perag_rele(pag
);
3255 args
->minlen
= xfs_bmap_select_minlen(ap
, args
, *blen
);
3259 /* Update all inode and quota accounting for the allocation we just did. */
3261 xfs_bmap_btalloc_accounting(
3262 struct xfs_bmalloca
*ap
,
3263 struct xfs_alloc_arg
*args
)
3265 if (ap
->flags
& XFS_BMAPI_COWFORK
) {
3267 * COW fork blocks are in-core only and thus are treated as
3268 * in-core quota reservation (like delalloc blocks) even when
3269 * converted to real blocks. The quota reservation is not
3270 * accounted to disk until blocks are remapped to the data
3271 * fork. So if these blocks were previously delalloc, we
3272 * already have quota reservation and there's nothing to do
3276 xfs_mod_delalloc(ap
->ip
->i_mount
, -(int64_t)args
->len
);
3281 * Otherwise, we've allocated blocks in a hole. The transaction
3282 * has acquired in-core quota reservation for this extent.
3283 * Rather than account these as real blocks, however, we reduce
3284 * the transaction quota reservation based on the allocation.
3285 * This essentially transfers the transaction quota reservation
3286 * to that of a delalloc extent.
3288 ap
->ip
->i_delayed_blks
+= args
->len
;
3289 xfs_trans_mod_dquot_byino(ap
->tp
, ap
->ip
, XFS_TRANS_DQ_RES_BLKS
,
3294 /* data/attr fork only */
3295 ap
->ip
->i_nblocks
+= args
->len
;
3296 xfs_trans_log_inode(ap
->tp
, ap
->ip
, XFS_ILOG_CORE
);
3298 ap
->ip
->i_delayed_blks
-= args
->len
;
3299 xfs_mod_delalloc(ap
->ip
->i_mount
, -(int64_t)args
->len
);
3301 xfs_trans_mod_dquot_byino(ap
->tp
, ap
->ip
,
3302 ap
->wasdel
? XFS_TRANS_DQ_DELBCOUNT
: XFS_TRANS_DQ_BCOUNT
,
3307 xfs_bmap_compute_alignments(
3308 struct xfs_bmalloca
*ap
,
3309 struct xfs_alloc_arg
*args
)
3311 struct xfs_mount
*mp
= args
->mp
;
3312 xfs_extlen_t align
= 0; /* minimum allocation alignment */
3313 int stripe_align
= 0;
3315 /* stripe alignment for allocation is determined by mount parameters */
3316 if (mp
->m_swidth
&& xfs_has_swalloc(mp
))
3317 stripe_align
= mp
->m_swidth
;
3318 else if (mp
->m_dalign
)
3319 stripe_align
= mp
->m_dalign
;
3321 if (ap
->flags
& XFS_BMAPI_COWFORK
)
3322 align
= xfs_get_cowextsz_hint(ap
->ip
);
3323 else if (ap
->datatype
& XFS_ALLOC_USERDATA
)
3324 align
= xfs_get_extsz_hint(ap
->ip
);
3326 if (xfs_bmap_extsize_align(mp
, &ap
->got
, &ap
->prev
, align
, 0,
3327 ap
->eof
, 0, ap
->conv
, &ap
->offset
,
3333 /* apply extent size hints if obtained earlier */
3336 div_u64_rem(ap
->offset
, args
->prod
, &args
->mod
);
3338 args
->mod
= args
->prod
- args
->mod
;
3339 } else if (mp
->m_sb
.sb_blocksize
>= PAGE_SIZE
) {
3343 args
->prod
= PAGE_SIZE
>> mp
->m_sb
.sb_blocklog
;
3344 div_u64_rem(ap
->offset
, args
->prod
, &args
->mod
);
3346 args
->mod
= args
->prod
- args
->mod
;
3349 return stripe_align
;
3353 xfs_bmap_process_allocated_extent(
3354 struct xfs_bmalloca
*ap
,
3355 struct xfs_alloc_arg
*args
,
3356 xfs_fileoff_t orig_offset
,
3357 xfs_extlen_t orig_length
)
3359 ap
->blkno
= args
->fsbno
;
3360 ap
->length
= args
->len
;
3362 * If the extent size hint is active, we tried to round the
3363 * caller's allocation request offset down to extsz and the
3364 * length up to another extsz boundary. If we found a free
3365 * extent we mapped it in starting at this new offset. If the
3366 * newly mapped space isn't long enough to cover any of the
3367 * range of offsets that was originally requested, move the
3368 * mapping up so that we can fill as much of the caller's
3369 * original request as possible. Free space is apparently
3370 * very fragmented so we're unlikely to be able to satisfy the
3373 if (ap
->length
<= orig_length
)
3374 ap
->offset
= orig_offset
;
3375 else if (ap
->offset
+ ap
->length
< orig_offset
+ orig_length
)
3376 ap
->offset
= orig_offset
+ orig_length
- ap
->length
;
3377 xfs_bmap_btalloc_accounting(ap
, args
);
3382 xfs_bmap_exact_minlen_extent_alloc(
3383 struct xfs_bmalloca
*ap
)
3385 struct xfs_mount
*mp
= ap
->ip
->i_mount
;
3386 struct xfs_alloc_arg args
= { .tp
= ap
->tp
, .mp
= mp
};
3387 xfs_fileoff_t orig_offset
;
3388 xfs_extlen_t orig_length
;
3393 if (ap
->minlen
!= 1) {
3394 ap
->blkno
= NULLFSBLOCK
;
3399 orig_offset
= ap
->offset
;
3400 orig_length
= ap
->length
;
3402 args
.alloc_minlen_only
= 1;
3404 xfs_bmap_compute_alignments(ap
, &args
);
3407 * Unlike the longest extent available in an AG, we don't track
3408 * the length of an AG's shortest extent.
3409 * XFS_ERRTAG_BMAP_ALLOC_MINLEN_EXTENT is a debug only knob and
3410 * hence we can afford to start traversing from the 0th AG since
3411 * we need not be concerned about a drop in performance in
3412 * "debug only" code paths.
3414 ap
->blkno
= XFS_AGB_TO_FSB(mp
, 0, 0);
3416 args
.oinfo
= XFS_RMAP_OINFO_SKIP_UPDATE
;
3417 args
.minlen
= args
.maxlen
= ap
->minlen
;
3418 args
.total
= ap
->total
;
3421 args
.minalignslop
= 0;
3423 args
.minleft
= ap
->minleft
;
3424 args
.wasdel
= ap
->wasdel
;
3425 args
.resv
= XFS_AG_RESV_NONE
;
3426 args
.datatype
= ap
->datatype
;
3428 error
= xfs_alloc_vextent_first_ag(&args
, ap
->blkno
);
3432 if (args
.fsbno
!= NULLFSBLOCK
) {
3433 xfs_bmap_process_allocated_extent(ap
, &args
, orig_offset
,
3436 ap
->blkno
= NULLFSBLOCK
;
3444 #define xfs_bmap_exact_minlen_extent_alloc(bma) (-EFSCORRUPTED)
3449 * If we are not low on available data blocks and we are allocating at
3450 * EOF, optimise allocation for contiguous file extension and/or stripe
3451 * alignment of the new extent.
3453 * NOTE: ap->aeof is only set if the allocation length is >= the
3454 * stripe unit and the allocation offset is at the end of file.
3457 xfs_bmap_btalloc_at_eof(
3458 struct xfs_bmalloca
*ap
,
3459 struct xfs_alloc_arg
*args
,
3464 struct xfs_mount
*mp
= args
->mp
;
3465 struct xfs_perag
*caller_pag
= args
->pag
;
3469 * If there are already extents in the file, try an exact EOF block
3470 * allocation to extend the file as a contiguous extent. If that fails,
3471 * or it's the first allocation in a file, just try for a stripe aligned
3475 xfs_extlen_t nextminlen
= 0;
3478 * Compute the minlen+alignment for the next case. Set slop so
3479 * that the value of minlen+alignment+slop doesn't go up between
3482 args
->alignment
= 1;
3483 if (blen
> stripe_align
&& blen
<= args
->maxlen
)
3484 nextminlen
= blen
- stripe_align
;
3486 nextminlen
= args
->minlen
;
3487 if (nextminlen
+ stripe_align
> args
->minlen
+ 1)
3488 args
->minalignslop
= nextminlen
+ stripe_align
-
3491 args
->minalignslop
= 0;
3494 args
->pag
= xfs_perag_get(mp
, XFS_FSB_TO_AGNO(mp
, ap
->blkno
));
3495 error
= xfs_alloc_vextent_exact_bno(args
, ap
->blkno
);
3497 xfs_perag_put(args
->pag
);
3503 if (args
->fsbno
!= NULLFSBLOCK
)
3506 * Exact allocation failed. Reset to try an aligned allocation
3507 * according to the original allocation specification.
3509 args
->alignment
= stripe_align
;
3510 args
->minlen
= nextminlen
;
3511 args
->minalignslop
= 0;
3514 * Adjust minlen to try and preserve alignment if we
3515 * can't guarantee an aligned maxlen extent.
3517 args
->alignment
= stripe_align
;
3518 if (blen
> args
->alignment
&&
3519 blen
<= args
->maxlen
+ args
->alignment
)
3520 args
->minlen
= blen
- args
->alignment
;
3521 args
->minalignslop
= 0;
3525 error
= xfs_alloc_vextent_near_bno(args
, ap
->blkno
);
3528 error
= xfs_alloc_vextent_start_ag(args
, ap
->blkno
);
3529 ASSERT(args
->pag
== NULL
);
3530 args
->pag
= caller_pag
;
3535 if (args
->fsbno
!= NULLFSBLOCK
)
3539 * Allocation failed, so turn return the allocation args to their
3540 * original non-aligned state so the caller can proceed on allocation
3541 * failure as if this function was never called.
3543 args
->alignment
= 1;
3548 * We have failed multiple allocation attempts so now are in a low space
3549 * allocation situation. Try a locality first full filesystem minimum length
3550 * allocation whilst still maintaining necessary total block reservation
3553 * If that fails, we are now critically low on space, so perform a last resort
3554 * allocation attempt: no reserve, no locality, blocking, minimum length, full
3555 * filesystem free space scan. We also indicate to future allocations in this
3556 * transaction that we are critically low on space so they don't waste time on
3557 * allocation modes that are unlikely to succeed.
3560 xfs_bmap_btalloc_low_space(
3561 struct xfs_bmalloca
*ap
,
3562 struct xfs_alloc_arg
*args
)
3566 if (args
->minlen
> ap
->minlen
) {
3567 args
->minlen
= ap
->minlen
;
3568 error
= xfs_alloc_vextent_start_ag(args
, ap
->blkno
);
3569 if (error
|| args
->fsbno
!= NULLFSBLOCK
)
3573 /* Last ditch attempt before failure is declared. */
3574 args
->total
= ap
->minlen
;
3575 error
= xfs_alloc_vextent_first_ag(args
, 0);
3578 ap
->tp
->t_flags
|= XFS_TRANS_LOWMODE
;
3583 xfs_bmap_btalloc_filestreams(
3584 struct xfs_bmalloca
*ap
,
3585 struct xfs_alloc_arg
*args
,
3588 xfs_extlen_t blen
= 0;
3592 error
= xfs_filestream_select_ag(ap
, args
, &blen
);
3598 * If we are in low space mode, then optimal allocation will fail so
3599 * prepare for minimal allocation and jump to the low space algorithm
3602 if (ap
->tp
->t_flags
& XFS_TRANS_LOWMODE
) {
3603 args
->minlen
= ap
->minlen
;
3604 ASSERT(args
->fsbno
== NULLFSBLOCK
);
3608 args
->minlen
= xfs_bmap_select_minlen(ap
, args
, blen
);
3610 error
= xfs_bmap_btalloc_at_eof(ap
, args
, blen
, stripe_align
,
3613 if (!error
&& args
->fsbno
== NULLFSBLOCK
)
3614 error
= xfs_alloc_vextent_near_bno(args
, ap
->blkno
);
3618 * We are now done with the perag reference for the filestreams
3619 * association provided by xfs_filestream_select_ag(). Release it now as
3620 * we've either succeeded, had a fatal error or we are out of space and
3621 * need to do a full filesystem scan for free space which will take it's
3624 xfs_perag_rele(args
->pag
);
3626 if (error
|| args
->fsbno
!= NULLFSBLOCK
)
3629 return xfs_bmap_btalloc_low_space(ap
, args
);
3633 xfs_bmap_btalloc_best_length(
3634 struct xfs_bmalloca
*ap
,
3635 struct xfs_alloc_arg
*args
,
3638 xfs_extlen_t blen
= 0;
3641 ap
->blkno
= XFS_INO_TO_FSB(args
->mp
, ap
->ip
->i_ino
);
3642 xfs_bmap_adjacent(ap
);
3645 * Search for an allocation group with a single extent large enough for
3646 * the request. If one isn't found, then adjust the minimum allocation
3647 * size to the largest space found.
3649 error
= xfs_bmap_btalloc_select_lengths(ap
, args
, &blen
);
3654 * Don't attempt optimal EOF allocation if previous allocations barely
3655 * succeeded due to being near ENOSPC. It is highly unlikely we'll get
3656 * optimal or even aligned allocations in this case, so don't waste time
3659 if (ap
->aeof
&& !(ap
->tp
->t_flags
& XFS_TRANS_LOWMODE
)) {
3660 error
= xfs_bmap_btalloc_at_eof(ap
, args
, blen
, stripe_align
,
3662 if (error
|| args
->fsbno
!= NULLFSBLOCK
)
3666 error
= xfs_alloc_vextent_start_ag(args
, ap
->blkno
);
3667 if (error
|| args
->fsbno
!= NULLFSBLOCK
)
3670 return xfs_bmap_btalloc_low_space(ap
, args
);
3675 struct xfs_bmalloca
*ap
)
3677 struct xfs_mount
*mp
= ap
->ip
->i_mount
;
3678 struct xfs_alloc_arg args
= {
3681 .fsbno
= NULLFSBLOCK
,
3682 .oinfo
= XFS_RMAP_OINFO_SKIP_UPDATE
,
3683 .minleft
= ap
->minleft
,
3684 .wasdel
= ap
->wasdel
,
3685 .resv
= XFS_AG_RESV_NONE
,
3686 .datatype
= ap
->datatype
,
3690 xfs_fileoff_t orig_offset
;
3691 xfs_extlen_t orig_length
;
3696 orig_offset
= ap
->offset
;
3697 orig_length
= ap
->length
;
3699 stripe_align
= xfs_bmap_compute_alignments(ap
, &args
);
3701 /* Trim the allocation back to the maximum an AG can fit. */
3702 args
.maxlen
= min(ap
->length
, mp
->m_ag_max_usable
);
3704 if ((ap
->datatype
& XFS_ALLOC_USERDATA
) &&
3705 xfs_inode_is_filestream(ap
->ip
))
3706 error
= xfs_bmap_btalloc_filestreams(ap
, &args
, stripe_align
);
3708 error
= xfs_bmap_btalloc_best_length(ap
, &args
, stripe_align
);
3712 if (args
.fsbno
!= NULLFSBLOCK
) {
3713 xfs_bmap_process_allocated_extent(ap
, &args
, orig_offset
,
3716 ap
->blkno
= NULLFSBLOCK
;
3722 /* Trim extent to fit a logical block range. */
3725 struct xfs_bmbt_irec
*irec
,
3729 xfs_fileoff_t distance
;
3730 xfs_fileoff_t end
= bno
+ len
;
3732 if (irec
->br_startoff
+ irec
->br_blockcount
<= bno
||
3733 irec
->br_startoff
>= end
) {
3734 irec
->br_blockcount
= 0;
3738 if (irec
->br_startoff
< bno
) {
3739 distance
= bno
- irec
->br_startoff
;
3740 if (isnullstartblock(irec
->br_startblock
))
3741 irec
->br_startblock
= DELAYSTARTBLOCK
;
3742 if (irec
->br_startblock
!= DELAYSTARTBLOCK
&&
3743 irec
->br_startblock
!= HOLESTARTBLOCK
)
3744 irec
->br_startblock
+= distance
;
3745 irec
->br_startoff
+= distance
;
3746 irec
->br_blockcount
-= distance
;
3749 if (end
< irec
->br_startoff
+ irec
->br_blockcount
) {
3750 distance
= irec
->br_startoff
+ irec
->br_blockcount
- end
;
3751 irec
->br_blockcount
-= distance
;
3756 * Trim the returned map to the required bounds
3760 struct xfs_bmbt_irec
*mval
,
3761 struct xfs_bmbt_irec
*got
,
3769 if ((flags
& XFS_BMAPI_ENTIRE
) ||
3770 got
->br_startoff
+ got
->br_blockcount
<= obno
) {
3772 if (isnullstartblock(got
->br_startblock
))
3773 mval
->br_startblock
= DELAYSTARTBLOCK
;
3779 ASSERT((*bno
>= obno
) || (n
== 0));
3781 mval
->br_startoff
= *bno
;
3782 if (isnullstartblock(got
->br_startblock
))
3783 mval
->br_startblock
= DELAYSTARTBLOCK
;
3785 mval
->br_startblock
= got
->br_startblock
+
3786 (*bno
- got
->br_startoff
);
3788 * Return the minimum of what we got and what we asked for for
3789 * the length. We can use the len variable here because it is
3790 * modified below and we could have been there before coming
3791 * here if the first part of the allocation didn't overlap what
3794 mval
->br_blockcount
= XFS_FILBLKS_MIN(end
- *bno
,
3795 got
->br_blockcount
- (*bno
- got
->br_startoff
));
3796 mval
->br_state
= got
->br_state
;
3797 ASSERT(mval
->br_blockcount
<= len
);
3802 * Update and validate the extent map to return
3805 xfs_bmapi_update_map(
3806 struct xfs_bmbt_irec
**map
,
3814 xfs_bmbt_irec_t
*mval
= *map
;
3816 ASSERT((flags
& XFS_BMAPI_ENTIRE
) ||
3817 ((mval
->br_startoff
+ mval
->br_blockcount
) <= end
));
3818 ASSERT((flags
& XFS_BMAPI_ENTIRE
) || (mval
->br_blockcount
<= *len
) ||
3819 (mval
->br_startoff
< obno
));
3821 *bno
= mval
->br_startoff
+ mval
->br_blockcount
;
3823 if (*n
> 0 && mval
->br_startoff
== mval
[-1].br_startoff
) {
3824 /* update previous map with new information */
3825 ASSERT(mval
->br_startblock
== mval
[-1].br_startblock
);
3826 ASSERT(mval
->br_blockcount
> mval
[-1].br_blockcount
);
3827 ASSERT(mval
->br_state
== mval
[-1].br_state
);
3828 mval
[-1].br_blockcount
= mval
->br_blockcount
;
3829 mval
[-1].br_state
= mval
->br_state
;
3830 } else if (*n
> 0 && mval
->br_startblock
!= DELAYSTARTBLOCK
&&
3831 mval
[-1].br_startblock
!= DELAYSTARTBLOCK
&&
3832 mval
[-1].br_startblock
!= HOLESTARTBLOCK
&&
3833 mval
->br_startblock
== mval
[-1].br_startblock
+
3834 mval
[-1].br_blockcount
&&
3835 mval
[-1].br_state
== mval
->br_state
) {
3836 ASSERT(mval
->br_startoff
==
3837 mval
[-1].br_startoff
+ mval
[-1].br_blockcount
);
3838 mval
[-1].br_blockcount
+= mval
->br_blockcount
;
3839 } else if (*n
> 0 &&
3840 mval
->br_startblock
== DELAYSTARTBLOCK
&&
3841 mval
[-1].br_startblock
== DELAYSTARTBLOCK
&&
3842 mval
->br_startoff
==
3843 mval
[-1].br_startoff
+ mval
[-1].br_blockcount
) {
3844 mval
[-1].br_blockcount
+= mval
->br_blockcount
;
3845 mval
[-1].br_state
= mval
->br_state
;
3846 } else if (!((*n
== 0) &&
3847 ((mval
->br_startoff
+ mval
->br_blockcount
) <=
3856 * Map file blocks to filesystem blocks without allocation.
3860 struct xfs_inode
*ip
,
3863 struct xfs_bmbt_irec
*mval
,
3867 struct xfs_mount
*mp
= ip
->i_mount
;
3868 int whichfork
= xfs_bmapi_whichfork(flags
);
3869 struct xfs_ifork
*ifp
= xfs_ifork_ptr(ip
, whichfork
);
3870 struct xfs_bmbt_irec got
;
3873 struct xfs_iext_cursor icur
;
3879 ASSERT(!(flags
& ~(XFS_BMAPI_ATTRFORK
| XFS_BMAPI_ENTIRE
)));
3880 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_SHARED
|XFS_ILOCK_EXCL
));
3882 if (WARN_ON_ONCE(!ifp
))
3883 return -EFSCORRUPTED
;
3885 if (XFS_IS_CORRUPT(mp
, !xfs_ifork_has_extents(ifp
)) ||
3886 XFS_TEST_ERROR(false, mp
, XFS_ERRTAG_BMAPIFORMAT
))
3887 return -EFSCORRUPTED
;
3889 if (xfs_is_shutdown(mp
))
3892 XFS_STATS_INC(mp
, xs_blk_mapr
);
3894 error
= xfs_iread_extents(NULL
, ip
, whichfork
);
3898 if (!xfs_iext_lookup_extent(ip
, ifp
, bno
, &icur
, &got
))
3903 while (bno
< end
&& n
< *nmap
) {
3904 /* Reading past eof, act as though there's a hole up to end. */
3906 got
.br_startoff
= end
;
3907 if (got
.br_startoff
> bno
) {
3908 /* Reading in a hole. */
3909 mval
->br_startoff
= bno
;
3910 mval
->br_startblock
= HOLESTARTBLOCK
;
3911 mval
->br_blockcount
=
3912 XFS_FILBLKS_MIN(len
, got
.br_startoff
- bno
);
3913 mval
->br_state
= XFS_EXT_NORM
;
3914 bno
+= mval
->br_blockcount
;
3915 len
-= mval
->br_blockcount
;
3921 /* set up the extent map to return. */
3922 xfs_bmapi_trim_map(mval
, &got
, &bno
, len
, obno
, end
, n
, flags
);
3923 xfs_bmapi_update_map(&mval
, &bno
, &len
, obno
, end
, &n
, flags
);
3925 /* If we're done, stop now. */
3926 if (bno
>= end
|| n
>= *nmap
)
3929 /* Else go on to the next record. */
3930 if (!xfs_iext_next_extent(ifp
, &icur
, &got
))
3938 * Add a delayed allocation extent to an inode. Blocks are reserved from the
3939 * global pool and the extent inserted into the inode in-core extent tree.
3941 * On entry, got refers to the first extent beyond the offset of the extent to
3942 * allocate or eof is specified if no such extent exists. On return, got refers
3943 * to the extent record that was inserted to the inode fork.
3945 * Note that the allocated extent may have been merged with contiguous extents
3946 * during insertion into the inode fork. Thus, got does not reflect the current
3947 * state of the inode fork on return. If necessary, the caller can use lastx to
3948 * look up the updated record in the inode fork.
3951 xfs_bmapi_reserve_delalloc(
3952 struct xfs_inode
*ip
,
3956 xfs_filblks_t prealloc
,
3957 struct xfs_bmbt_irec
*got
,
3958 struct xfs_iext_cursor
*icur
,
3961 struct xfs_mount
*mp
= ip
->i_mount
;
3962 struct xfs_ifork
*ifp
= xfs_ifork_ptr(ip
, whichfork
);
3964 xfs_extlen_t indlen
;
3966 xfs_fileoff_t aoff
= off
;
3969 * Cap the alloc length. Keep track of prealloc so we know whether to
3970 * tag the inode before we return.
3972 alen
= XFS_FILBLKS_MIN(len
+ prealloc
, XFS_MAX_BMBT_EXTLEN
);
3974 alen
= XFS_FILBLKS_MIN(alen
, got
->br_startoff
- aoff
);
3975 if (prealloc
&& alen
>= len
)
3976 prealloc
= alen
- len
;
3978 /* Figure out the extent size, adjust alen */
3979 if (whichfork
== XFS_COW_FORK
) {
3980 struct xfs_bmbt_irec prev
;
3981 xfs_extlen_t extsz
= xfs_get_cowextsz_hint(ip
);
3983 if (!xfs_iext_peek_prev_extent(ifp
, icur
, &prev
))
3984 prev
.br_startoff
= NULLFILEOFF
;
3986 error
= xfs_bmap_extsize_align(mp
, got
, &prev
, extsz
, 0, eof
,
3987 1, 0, &aoff
, &alen
);
3992 * Make a transaction-less quota reservation for delayed allocation
3993 * blocks. This number gets adjusted later. We return if we haven't
3994 * allocated blocks already inside this loop.
3996 error
= xfs_quota_reserve_blkres(ip
, alen
);
4001 * Split changing sb for alen and indlen since they could be coming
4002 * from different places.
4004 indlen
= (xfs_extlen_t
)xfs_bmap_worst_indlen(ip
, alen
);
4007 error
= xfs_mod_fdblocks(mp
, -((int64_t)alen
), false);
4009 goto out_unreserve_quota
;
4011 error
= xfs_mod_fdblocks(mp
, -((int64_t)indlen
), false);
4013 goto out_unreserve_blocks
;
4016 ip
->i_delayed_blks
+= alen
;
4017 xfs_mod_delalloc(ip
->i_mount
, alen
+ indlen
);
4019 got
->br_startoff
= aoff
;
4020 got
->br_startblock
= nullstartblock(indlen
);
4021 got
->br_blockcount
= alen
;
4022 got
->br_state
= XFS_EXT_NORM
;
4024 xfs_bmap_add_extent_hole_delay(ip
, whichfork
, icur
, got
);
4027 * Tag the inode if blocks were preallocated. Note that COW fork
4028 * preallocation can occur at the start or end of the extent, even when
4029 * prealloc == 0, so we must also check the aligned offset and length.
4031 if (whichfork
== XFS_DATA_FORK
&& prealloc
)
4032 xfs_inode_set_eofblocks_tag(ip
);
4033 if (whichfork
== XFS_COW_FORK
&& (prealloc
|| aoff
< off
|| alen
> len
))
4034 xfs_inode_set_cowblocks_tag(ip
);
4038 out_unreserve_blocks
:
4039 xfs_mod_fdblocks(mp
, alen
, false);
4040 out_unreserve_quota
:
4041 if (XFS_IS_QUOTA_ON(mp
))
4042 xfs_quota_unreserve_blkres(ip
, alen
);
4047 xfs_bmap_alloc_userdata(
4048 struct xfs_bmalloca
*bma
)
4050 struct xfs_mount
*mp
= bma
->ip
->i_mount
;
4051 int whichfork
= xfs_bmapi_whichfork(bma
->flags
);
4055 * Set the data type being allocated. For the data fork, the first data
4056 * in the file is treated differently to all other allocations. For the
4057 * attribute fork, we only need to ensure the allocated range is not on
4060 bma
->datatype
= XFS_ALLOC_NOBUSY
;
4061 if (whichfork
== XFS_DATA_FORK
|| whichfork
== XFS_COW_FORK
) {
4062 bma
->datatype
|= XFS_ALLOC_USERDATA
;
4063 if (bma
->offset
== 0)
4064 bma
->datatype
|= XFS_ALLOC_INITIAL_USER_DATA
;
4066 if (mp
->m_dalign
&& bma
->length
>= mp
->m_dalign
) {
4067 error
= xfs_bmap_isaeof(bma
, whichfork
);
4072 if (XFS_IS_REALTIME_INODE(bma
->ip
))
4073 return xfs_bmap_rtalloc(bma
);
4076 if (unlikely(XFS_TEST_ERROR(false, mp
,
4077 XFS_ERRTAG_BMAP_ALLOC_MINLEN_EXTENT
)))
4078 return xfs_bmap_exact_minlen_extent_alloc(bma
);
4080 return xfs_bmap_btalloc(bma
);
4085 struct xfs_bmalloca
*bma
)
4087 struct xfs_mount
*mp
= bma
->ip
->i_mount
;
4088 int whichfork
= xfs_bmapi_whichfork(bma
->flags
);
4089 struct xfs_ifork
*ifp
= xfs_ifork_ptr(bma
->ip
, whichfork
);
4090 int tmp_logflags
= 0;
4093 ASSERT(bma
->length
> 0);
4096 * For the wasdelay case, we could also just allocate the stuff asked
4097 * for in this bmap call but that wouldn't be as good.
4100 bma
->length
= (xfs_extlen_t
)bma
->got
.br_blockcount
;
4101 bma
->offset
= bma
->got
.br_startoff
;
4102 if (!xfs_iext_peek_prev_extent(ifp
, &bma
->icur
, &bma
->prev
))
4103 bma
->prev
.br_startoff
= NULLFILEOFF
;
4105 bma
->length
= XFS_FILBLKS_MIN(bma
->length
, XFS_MAX_BMBT_EXTLEN
);
4107 bma
->length
= XFS_FILBLKS_MIN(bma
->length
,
4108 bma
->got
.br_startoff
- bma
->offset
);
4111 if (bma
->flags
& XFS_BMAPI_CONTIG
)
4112 bma
->minlen
= bma
->length
;
4116 if (bma
->flags
& XFS_BMAPI_METADATA
) {
4117 if (unlikely(XFS_TEST_ERROR(false, mp
,
4118 XFS_ERRTAG_BMAP_ALLOC_MINLEN_EXTENT
)))
4119 error
= xfs_bmap_exact_minlen_extent_alloc(bma
);
4121 error
= xfs_bmap_btalloc(bma
);
4123 error
= xfs_bmap_alloc_userdata(bma
);
4125 if (error
|| bma
->blkno
== NULLFSBLOCK
)
4128 if (bma
->flags
& XFS_BMAPI_ZERO
) {
4129 error
= xfs_zero_extent(bma
->ip
, bma
->blkno
, bma
->length
);
4134 if (ifp
->if_format
== XFS_DINODE_FMT_BTREE
&& !bma
->cur
)
4135 bma
->cur
= xfs_bmbt_init_cursor(mp
, bma
->tp
, bma
->ip
, whichfork
);
4137 * Bump the number of extents we've allocated
4143 bma
->cur
->bc_ino
.flags
=
4144 bma
->wasdel
? XFS_BTCUR_BMBT_WASDEL
: 0;
4146 bma
->got
.br_startoff
= bma
->offset
;
4147 bma
->got
.br_startblock
= bma
->blkno
;
4148 bma
->got
.br_blockcount
= bma
->length
;
4149 bma
->got
.br_state
= XFS_EXT_NORM
;
4151 if (bma
->flags
& XFS_BMAPI_PREALLOC
)
4152 bma
->got
.br_state
= XFS_EXT_UNWRITTEN
;
4155 error
= xfs_bmap_add_extent_delay_real(bma
, whichfork
);
4157 error
= xfs_bmap_add_extent_hole_real(bma
->tp
, bma
->ip
,
4158 whichfork
, &bma
->icur
, &bma
->cur
, &bma
->got
,
4159 &bma
->logflags
, bma
->flags
);
4161 bma
->logflags
|= tmp_logflags
;
4166 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4167 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4168 * the neighbouring ones.
4170 xfs_iext_get_extent(ifp
, &bma
->icur
, &bma
->got
);
4172 ASSERT(bma
->got
.br_startoff
<= bma
->offset
);
4173 ASSERT(bma
->got
.br_startoff
+ bma
->got
.br_blockcount
>=
4174 bma
->offset
+ bma
->length
);
4175 ASSERT(bma
->got
.br_state
== XFS_EXT_NORM
||
4176 bma
->got
.br_state
== XFS_EXT_UNWRITTEN
);
4181 xfs_bmapi_convert_unwritten(
4182 struct xfs_bmalloca
*bma
,
4183 struct xfs_bmbt_irec
*mval
,
4187 int whichfork
= xfs_bmapi_whichfork(flags
);
4188 struct xfs_ifork
*ifp
= xfs_ifork_ptr(bma
->ip
, whichfork
);
4189 int tmp_logflags
= 0;
4192 /* check if we need to do unwritten->real conversion */
4193 if (mval
->br_state
== XFS_EXT_UNWRITTEN
&&
4194 (flags
& XFS_BMAPI_PREALLOC
))
4197 /* check if we need to do real->unwritten conversion */
4198 if (mval
->br_state
== XFS_EXT_NORM
&&
4199 (flags
& (XFS_BMAPI_PREALLOC
| XFS_BMAPI_CONVERT
)) !=
4200 (XFS_BMAPI_PREALLOC
| XFS_BMAPI_CONVERT
))
4204 * Modify (by adding) the state flag, if writing.
4206 ASSERT(mval
->br_blockcount
<= len
);
4207 if (ifp
->if_format
== XFS_DINODE_FMT_BTREE
&& !bma
->cur
) {
4208 bma
->cur
= xfs_bmbt_init_cursor(bma
->ip
->i_mount
, bma
->tp
,
4209 bma
->ip
, whichfork
);
4211 mval
->br_state
= (mval
->br_state
== XFS_EXT_UNWRITTEN
)
4212 ? XFS_EXT_NORM
: XFS_EXT_UNWRITTEN
;
4215 * Before insertion into the bmbt, zero the range being converted
4218 if (flags
& XFS_BMAPI_ZERO
) {
4219 error
= xfs_zero_extent(bma
->ip
, mval
->br_startblock
,
4220 mval
->br_blockcount
);
4225 error
= xfs_bmap_add_extent_unwritten_real(bma
->tp
, bma
->ip
, whichfork
,
4226 &bma
->icur
, &bma
->cur
, mval
, &tmp_logflags
);
4228 * Log the inode core unconditionally in the unwritten extent conversion
4229 * path because the conversion might not have done so (e.g., if the
4230 * extent count hasn't changed). We need to make sure the inode is dirty
4231 * in the transaction for the sake of fsync(), even if nothing has
4232 * changed, because fsync() will not force the log for this transaction
4233 * unless it sees the inode pinned.
4235 * Note: If we're only converting cow fork extents, there aren't
4236 * any on-disk updates to make, so we don't need to log anything.
4238 if (whichfork
!= XFS_COW_FORK
)
4239 bma
->logflags
|= tmp_logflags
| XFS_ILOG_CORE
;
4244 * Update our extent pointer, given that
4245 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4246 * of the neighbouring ones.
4248 xfs_iext_get_extent(ifp
, &bma
->icur
, &bma
->got
);
4251 * We may have combined previously unwritten space with written space,
4252 * so generate another request.
4254 if (mval
->br_blockcount
< len
)
4261 struct xfs_trans
*tp
,
4262 struct xfs_inode
*ip
,
4265 struct xfs_ifork
*ifp
= xfs_ifork_ptr(ip
, fork
);
4267 if (tp
&& tp
->t_highest_agno
!= NULLAGNUMBER
)
4269 if (ifp
->if_format
!= XFS_DINODE_FMT_BTREE
)
4271 return be16_to_cpu(ifp
->if_broot
->bb_level
) + 1;
4275 * Log whatever the flags say, even if error. Otherwise we might miss detecting
4276 * a case where the data is changed, there's an error, and it's not logged so we
4277 * don't shutdown when we should. Don't bother logging extents/btree changes if
4278 * we converted to the other format.
4282 struct xfs_bmalloca
*bma
,
4286 struct xfs_ifork
*ifp
= xfs_ifork_ptr(bma
->ip
, whichfork
);
4288 if ((bma
->logflags
& xfs_ilog_fext(whichfork
)) &&
4289 ifp
->if_format
!= XFS_DINODE_FMT_EXTENTS
)
4290 bma
->logflags
&= ~xfs_ilog_fext(whichfork
);
4291 else if ((bma
->logflags
& xfs_ilog_fbroot(whichfork
)) &&
4292 ifp
->if_format
!= XFS_DINODE_FMT_BTREE
)
4293 bma
->logflags
&= ~xfs_ilog_fbroot(whichfork
);
4296 xfs_trans_log_inode(bma
->tp
, bma
->ip
, bma
->logflags
);
4298 xfs_btree_del_cursor(bma
->cur
, error
);
4302 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4303 * extent state if necessary. Details behaviour is controlled by the flags
4304 * parameter. Only allocates blocks from a single allocation group, to avoid
4309 struct xfs_trans
*tp
, /* transaction pointer */
4310 struct xfs_inode
*ip
, /* incore inode */
4311 xfs_fileoff_t bno
, /* starting file offs. mapped */
4312 xfs_filblks_t len
, /* length to map in file */
4313 uint32_t flags
, /* XFS_BMAPI_... */
4314 xfs_extlen_t total
, /* total blocks needed */
4315 struct xfs_bmbt_irec
*mval
, /* output: map values */
4316 int *nmap
) /* i/o: mval size/count */
4318 struct xfs_bmalloca bma
= {
4323 struct xfs_mount
*mp
= ip
->i_mount
;
4324 int whichfork
= xfs_bmapi_whichfork(flags
);
4325 struct xfs_ifork
*ifp
= xfs_ifork_ptr(ip
, whichfork
);
4326 xfs_fileoff_t end
; /* end of mapped file region */
4327 bool eof
= false; /* after the end of extents */
4328 int error
; /* error return */
4329 int n
; /* current extent index */
4330 xfs_fileoff_t obno
; /* old block number (offset) */
4333 xfs_fileoff_t orig_bno
; /* original block number value */
4334 int orig_flags
; /* original flags arg value */
4335 xfs_filblks_t orig_len
; /* original value of len arg */
4336 struct xfs_bmbt_irec
*orig_mval
; /* original value of mval */
4337 int orig_nmap
; /* original value of *nmap */
4347 ASSERT(*nmap
<= XFS_BMAP_MAX_NMAP
);
4350 ASSERT(ifp
->if_format
!= XFS_DINODE_FMT_LOCAL
);
4351 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
4352 ASSERT(!(flags
& XFS_BMAPI_REMAP
));
4354 /* zeroing is for currently only for data extents, not metadata */
4355 ASSERT((flags
& (XFS_BMAPI_METADATA
| XFS_BMAPI_ZERO
)) !=
4356 (XFS_BMAPI_METADATA
| XFS_BMAPI_ZERO
));
4358 * we can allocate unwritten extents or pre-zero allocated blocks,
4359 * but it makes no sense to do both at once. This would result in
4360 * zeroing the unwritten extent twice, but it still being an
4361 * unwritten extent....
4363 ASSERT((flags
& (XFS_BMAPI_PREALLOC
| XFS_BMAPI_ZERO
)) !=
4364 (XFS_BMAPI_PREALLOC
| XFS_BMAPI_ZERO
));
4366 if (XFS_IS_CORRUPT(mp
, !xfs_ifork_has_extents(ifp
)) ||
4367 XFS_TEST_ERROR(false, mp
, XFS_ERRTAG_BMAPIFORMAT
)) {
4368 return -EFSCORRUPTED
;
4371 if (xfs_is_shutdown(mp
))
4374 XFS_STATS_INC(mp
, xs_blk_mapw
);
4376 error
= xfs_iread_extents(tp
, ip
, whichfork
);
4380 if (!xfs_iext_lookup_extent(ip
, ifp
, bno
, &bma
.icur
, &bma
.got
))
4382 if (!xfs_iext_peek_prev_extent(ifp
, &bma
.icur
, &bma
.prev
))
4383 bma
.prev
.br_startoff
= NULLFILEOFF
;
4384 bma
.minleft
= xfs_bmapi_minleft(tp
, ip
, whichfork
);
4389 while (bno
< end
&& n
< *nmap
) {
4390 bool need_alloc
= false, wasdelay
= false;
4392 /* in hole or beyond EOF? */
4393 if (eof
|| bma
.got
.br_startoff
> bno
) {
4395 * CoW fork conversions should /never/ hit EOF or
4396 * holes. There should always be something for us
4399 ASSERT(!((flags
& XFS_BMAPI_CONVERT
) &&
4400 (flags
& XFS_BMAPI_COWFORK
)));
4403 } else if (isnullstartblock(bma
.got
.br_startblock
)) {
4408 * First, deal with the hole before the allocated space
4409 * that we found, if any.
4411 if (need_alloc
|| wasdelay
) {
4413 bma
.conv
= !!(flags
& XFS_BMAPI_CONVERT
);
4414 bma
.wasdel
= wasdelay
;
4419 * There's a 32/64 bit type mismatch between the
4420 * allocation length request (which can be 64 bits in
4421 * length) and the bma length request, which is
4422 * xfs_extlen_t and therefore 32 bits. Hence we have to
4423 * check for 32-bit overflows and handle them here.
4425 if (len
> (xfs_filblks_t
)XFS_MAX_BMBT_EXTLEN
)
4426 bma
.length
= XFS_MAX_BMBT_EXTLEN
;
4431 ASSERT(bma
.length
> 0);
4432 error
= xfs_bmapi_allocate(&bma
);
4435 if (bma
.blkno
== NULLFSBLOCK
)
4439 * If this is a CoW allocation, record the data in
4440 * the refcount btree for orphan recovery.
4442 if (whichfork
== XFS_COW_FORK
)
4443 xfs_refcount_alloc_cow_extent(tp
, bma
.blkno
,
4447 /* Deal with the allocated space we found. */
4448 xfs_bmapi_trim_map(mval
, &bma
.got
, &bno
, len
, obno
,
4451 /* Execute unwritten extent conversion if necessary */
4452 error
= xfs_bmapi_convert_unwritten(&bma
, mval
, len
, flags
);
4453 if (error
== -EAGAIN
)
4458 /* update the extent map to return */
4459 xfs_bmapi_update_map(&mval
, &bno
, &len
, obno
, end
, &n
, flags
);
4462 * If we're done, stop now. Stop when we've allocated
4463 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4464 * the transaction may get too big.
4466 if (bno
>= end
|| n
>= *nmap
|| bma
.nallocs
>= *nmap
)
4469 /* Else go on to the next record. */
4471 if (!xfs_iext_next_extent(ifp
, &bma
.icur
, &bma
.got
))
4476 error
= xfs_bmap_btree_to_extents(tp
, ip
, bma
.cur
, &bma
.logflags
,
4481 ASSERT(ifp
->if_format
!= XFS_DINODE_FMT_BTREE
||
4482 ifp
->if_nextents
> XFS_IFORK_MAXEXT(ip
, whichfork
));
4483 xfs_bmapi_finish(&bma
, whichfork
, 0);
4484 xfs_bmap_validate_ret(orig_bno
, orig_len
, orig_flags
, orig_mval
,
4488 xfs_bmapi_finish(&bma
, whichfork
, error
);
4493 * Convert an existing delalloc extent to real blocks based on file offset. This
4494 * attempts to allocate the entire delalloc extent and may require multiple
4495 * invocations to allocate the target offset if a large enough physical extent
4499 xfs_bmapi_convert_delalloc(
4500 struct xfs_inode
*ip
,
4503 struct iomap
*iomap
,
4506 struct xfs_ifork
*ifp
= xfs_ifork_ptr(ip
, whichfork
);
4507 struct xfs_mount
*mp
= ip
->i_mount
;
4508 xfs_fileoff_t offset_fsb
= XFS_B_TO_FSBT(mp
, offset
);
4509 struct xfs_bmalloca bma
= { NULL
};
4511 struct xfs_trans
*tp
;
4514 if (whichfork
== XFS_COW_FORK
)
4515 flags
|= IOMAP_F_SHARED
;
4518 * Space for the extent and indirect blocks was reserved when the
4519 * delalloc extent was created so there's no need to do so here.
4521 error
= xfs_trans_alloc(mp
, &M_RES(mp
)->tr_write
, 0, 0,
4522 XFS_TRANS_RESERVE
, &tp
);
4526 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
4527 xfs_trans_ijoin(tp
, ip
, 0);
4529 error
= xfs_iext_count_may_overflow(ip
, whichfork
,
4530 XFS_IEXT_ADD_NOSPLIT_CNT
);
4531 if (error
== -EFBIG
)
4532 error
= xfs_iext_count_upgrade(tp
, ip
,
4533 XFS_IEXT_ADD_NOSPLIT_CNT
);
4535 goto out_trans_cancel
;
4537 if (!xfs_iext_lookup_extent(ip
, ifp
, offset_fsb
, &bma
.icur
, &bma
.got
) ||
4538 bma
.got
.br_startoff
> offset_fsb
) {
4540 * No extent found in the range we are trying to convert. This
4541 * should only happen for the COW fork, where another thread
4542 * might have moved the extent to the data fork in the meantime.
4544 WARN_ON_ONCE(whichfork
!= XFS_COW_FORK
);
4546 goto out_trans_cancel
;
4550 * If we find a real extent here we raced with another thread converting
4551 * the extent. Just return the real extent at this offset.
4553 if (!isnullstartblock(bma
.got
.br_startblock
)) {
4554 xfs_bmbt_to_iomap(ip
, iomap
, &bma
.got
, 0, flags
,
4555 xfs_iomap_inode_sequence(ip
, flags
));
4556 *seq
= READ_ONCE(ifp
->if_seq
);
4557 goto out_trans_cancel
;
4563 bma
.offset
= bma
.got
.br_startoff
;
4564 bma
.length
= max_t(xfs_filblks_t
, bma
.got
.br_blockcount
,
4565 XFS_MAX_BMBT_EXTLEN
);
4566 bma
.minleft
= xfs_bmapi_minleft(tp
, ip
, whichfork
);
4569 * When we're converting the delalloc reservations backing dirty pages
4570 * in the page cache, we must be careful about how we create the new
4573 * New CoW fork extents are created unwritten, turned into real extents
4574 * when we're about to write the data to disk, and mapped into the data
4575 * fork after the write finishes. End of story.
4577 * New data fork extents must be mapped in as unwritten and converted
4578 * to real extents after the write succeeds to avoid exposing stale
4579 * disk contents if we crash.
4581 bma
.flags
= XFS_BMAPI_PREALLOC
;
4582 if (whichfork
== XFS_COW_FORK
)
4583 bma
.flags
|= XFS_BMAPI_COWFORK
;
4585 if (!xfs_iext_peek_prev_extent(ifp
, &bma
.icur
, &bma
.prev
))
4586 bma
.prev
.br_startoff
= NULLFILEOFF
;
4588 error
= xfs_bmapi_allocate(&bma
);
4593 if (WARN_ON_ONCE(bma
.blkno
== NULLFSBLOCK
))
4595 error
= -EFSCORRUPTED
;
4596 if (WARN_ON_ONCE(!xfs_valid_startblock(ip
, bma
.got
.br_startblock
)))
4599 XFS_STATS_ADD(mp
, xs_xstrat_bytes
, XFS_FSB_TO_B(mp
, bma
.length
));
4600 XFS_STATS_INC(mp
, xs_xstrat_quick
);
4602 ASSERT(!isnullstartblock(bma
.got
.br_startblock
));
4603 xfs_bmbt_to_iomap(ip
, iomap
, &bma
.got
, 0, flags
,
4604 xfs_iomap_inode_sequence(ip
, flags
));
4605 *seq
= READ_ONCE(ifp
->if_seq
);
4607 if (whichfork
== XFS_COW_FORK
)
4608 xfs_refcount_alloc_cow_extent(tp
, bma
.blkno
, bma
.length
);
4610 error
= xfs_bmap_btree_to_extents(tp
, ip
, bma
.cur
, &bma
.logflags
,
4615 xfs_bmapi_finish(&bma
, whichfork
, 0);
4616 error
= xfs_trans_commit(tp
);
4617 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
4621 xfs_bmapi_finish(&bma
, whichfork
, error
);
4623 xfs_trans_cancel(tp
);
4624 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
4630 struct xfs_trans
*tp
,
4631 struct xfs_inode
*ip
,
4634 xfs_fsblock_t startblock
,
4637 struct xfs_mount
*mp
= ip
->i_mount
;
4638 struct xfs_ifork
*ifp
;
4639 struct xfs_btree_cur
*cur
= NULL
;
4640 struct xfs_bmbt_irec got
;
4641 struct xfs_iext_cursor icur
;
4642 int whichfork
= xfs_bmapi_whichfork(flags
);
4643 int logflags
= 0, error
;
4645 ifp
= xfs_ifork_ptr(ip
, whichfork
);
4647 ASSERT(len
<= (xfs_filblks_t
)XFS_MAX_BMBT_EXTLEN
);
4648 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
4649 ASSERT(!(flags
& ~(XFS_BMAPI_ATTRFORK
| XFS_BMAPI_PREALLOC
|
4650 XFS_BMAPI_NORMAP
)));
4651 ASSERT((flags
& (XFS_BMAPI_ATTRFORK
| XFS_BMAPI_PREALLOC
)) !=
4652 (XFS_BMAPI_ATTRFORK
| XFS_BMAPI_PREALLOC
));
4654 if (XFS_IS_CORRUPT(mp
, !xfs_ifork_has_extents(ifp
)) ||
4655 XFS_TEST_ERROR(false, mp
, XFS_ERRTAG_BMAPIFORMAT
)) {
4656 return -EFSCORRUPTED
;
4659 if (xfs_is_shutdown(mp
))
4662 error
= xfs_iread_extents(tp
, ip
, whichfork
);
4666 if (xfs_iext_lookup_extent(ip
, ifp
, bno
, &icur
, &got
)) {
4667 /* make sure we only reflink into a hole. */
4668 ASSERT(got
.br_startoff
> bno
);
4669 ASSERT(got
.br_startoff
- bno
>= len
);
4672 ip
->i_nblocks
+= len
;
4673 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
4675 if (ifp
->if_format
== XFS_DINODE_FMT_BTREE
) {
4676 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
4677 cur
->bc_ino
.flags
= 0;
4680 got
.br_startoff
= bno
;
4681 got
.br_startblock
= startblock
;
4682 got
.br_blockcount
= len
;
4683 if (flags
& XFS_BMAPI_PREALLOC
)
4684 got
.br_state
= XFS_EXT_UNWRITTEN
;
4686 got
.br_state
= XFS_EXT_NORM
;
4688 error
= xfs_bmap_add_extent_hole_real(tp
, ip
, whichfork
, &icur
,
4689 &cur
, &got
, &logflags
, flags
);
4693 error
= xfs_bmap_btree_to_extents(tp
, ip
, cur
, &logflags
, whichfork
);
4696 if (ip
->i_df
.if_format
!= XFS_DINODE_FMT_EXTENTS
)
4697 logflags
&= ~XFS_ILOG_DEXT
;
4698 else if (ip
->i_df
.if_format
!= XFS_DINODE_FMT_BTREE
)
4699 logflags
&= ~XFS_ILOG_DBROOT
;
4702 xfs_trans_log_inode(tp
, ip
, logflags
);
4704 xfs_btree_del_cursor(cur
, error
);
4709 * When a delalloc extent is split (e.g., due to a hole punch), the original
4710 * indlen reservation must be shared across the two new extents that are left
4713 * Given the original reservation and the worst case indlen for the two new
4714 * extents (as calculated by xfs_bmap_worst_indlen()), split the original
4715 * reservation fairly across the two new extents. If necessary, steal available
4716 * blocks from a deleted extent to make up a reservation deficiency (e.g., if
4717 * ores == 1). The number of stolen blocks is returned. The availability and
4718 * subsequent accounting of stolen blocks is the responsibility of the caller.
4720 static xfs_filblks_t
4721 xfs_bmap_split_indlen(
4722 xfs_filblks_t ores
, /* original res. */
4723 xfs_filblks_t
*indlen1
, /* ext1 worst indlen */
4724 xfs_filblks_t
*indlen2
, /* ext2 worst indlen */
4725 xfs_filblks_t avail
) /* stealable blocks */
4727 xfs_filblks_t len1
= *indlen1
;
4728 xfs_filblks_t len2
= *indlen2
;
4729 xfs_filblks_t nres
= len1
+ len2
; /* new total res. */
4730 xfs_filblks_t stolen
= 0;
4731 xfs_filblks_t resfactor
;
4734 * Steal as many blocks as we can to try and satisfy the worst case
4735 * indlen for both new extents.
4737 if (ores
< nres
&& avail
)
4738 stolen
= XFS_FILBLKS_MIN(nres
- ores
, avail
);
4741 /* nothing else to do if we've satisfied the new reservation */
4746 * We can't meet the total required reservation for the two extents.
4747 * Calculate the percent of the overall shortage between both extents
4748 * and apply this percentage to each of the requested indlen values.
4749 * This distributes the shortage fairly and reduces the chances that one
4750 * of the two extents is left with nothing when extents are repeatedly
4753 resfactor
= (ores
* 100);
4754 do_div(resfactor
, nres
);
4759 ASSERT(len1
+ len2
<= ores
);
4760 ASSERT(len1
< *indlen1
&& len2
< *indlen2
);
4763 * Hand out the remainder to each extent. If one of the two reservations
4764 * is zero, we want to make sure that one gets a block first. The loop
4765 * below starts with len1, so hand len2 a block right off the bat if it
4768 ores
-= (len1
+ len2
);
4769 ASSERT((*indlen1
- len1
) + (*indlen2
- len2
) >= ores
);
4770 if (ores
&& !len2
&& *indlen2
) {
4775 if (len1
< *indlen1
) {
4781 if (len2
< *indlen2
) {
4794 xfs_bmap_del_extent_delay(
4795 struct xfs_inode
*ip
,
4797 struct xfs_iext_cursor
*icur
,
4798 struct xfs_bmbt_irec
*got
,
4799 struct xfs_bmbt_irec
*del
)
4801 struct xfs_mount
*mp
= ip
->i_mount
;
4802 struct xfs_ifork
*ifp
= xfs_ifork_ptr(ip
, whichfork
);
4803 struct xfs_bmbt_irec
new;
4804 int64_t da_old
, da_new
, da_diff
= 0;
4805 xfs_fileoff_t del_endoff
, got_endoff
;
4806 xfs_filblks_t got_indlen
, new_indlen
, stolen
;
4807 uint32_t state
= xfs_bmap_fork_to_state(whichfork
);
4811 XFS_STATS_INC(mp
, xs_del_exlist
);
4813 isrt
= (whichfork
== XFS_DATA_FORK
) && XFS_IS_REALTIME_INODE(ip
);
4814 del_endoff
= del
->br_startoff
+ del
->br_blockcount
;
4815 got_endoff
= got
->br_startoff
+ got
->br_blockcount
;
4816 da_old
= startblockval(got
->br_startblock
);
4819 ASSERT(del
->br_blockcount
> 0);
4820 ASSERT(got
->br_startoff
<= del
->br_startoff
);
4821 ASSERT(got_endoff
>= del_endoff
);
4824 uint64_t rtexts
= del
->br_blockcount
;
4826 do_div(rtexts
, mp
->m_sb
.sb_rextsize
);
4827 xfs_mod_frextents(mp
, rtexts
);
4831 * Update the inode delalloc counter now and wait to update the
4832 * sb counters as we might have to borrow some blocks for the
4833 * indirect block accounting.
4836 error
= xfs_quota_unreserve_blkres(ip
, del
->br_blockcount
);
4839 ip
->i_delayed_blks
-= del
->br_blockcount
;
4841 if (got
->br_startoff
== del
->br_startoff
)
4842 state
|= BMAP_LEFT_FILLING
;
4843 if (got_endoff
== del_endoff
)
4844 state
|= BMAP_RIGHT_FILLING
;
4846 switch (state
& (BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
)) {
4847 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
4849 * Matches the whole extent. Delete the entry.
4851 xfs_iext_remove(ip
, icur
, state
);
4852 xfs_iext_prev(ifp
, icur
);
4854 case BMAP_LEFT_FILLING
:
4856 * Deleting the first part of the extent.
4858 got
->br_startoff
= del_endoff
;
4859 got
->br_blockcount
-= del
->br_blockcount
;
4860 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
,
4861 got
->br_blockcount
), da_old
);
4862 got
->br_startblock
= nullstartblock((int)da_new
);
4863 xfs_iext_update_extent(ip
, state
, icur
, got
);
4865 case BMAP_RIGHT_FILLING
:
4867 * Deleting the last part of the extent.
4869 got
->br_blockcount
= got
->br_blockcount
- del
->br_blockcount
;
4870 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
,
4871 got
->br_blockcount
), da_old
);
4872 got
->br_startblock
= nullstartblock((int)da_new
);
4873 xfs_iext_update_extent(ip
, state
, icur
, got
);
4877 * Deleting the middle of the extent.
4879 * Distribute the original indlen reservation across the two new
4880 * extents. Steal blocks from the deleted extent if necessary.
4881 * Stealing blocks simply fudges the fdblocks accounting below.
4882 * Warn if either of the new indlen reservations is zero as this
4883 * can lead to delalloc problems.
4885 got
->br_blockcount
= del
->br_startoff
- got
->br_startoff
;
4886 got_indlen
= xfs_bmap_worst_indlen(ip
, got
->br_blockcount
);
4888 new.br_blockcount
= got_endoff
- del_endoff
;
4889 new_indlen
= xfs_bmap_worst_indlen(ip
, new.br_blockcount
);
4891 WARN_ON_ONCE(!got_indlen
|| !new_indlen
);
4892 stolen
= xfs_bmap_split_indlen(da_old
, &got_indlen
, &new_indlen
,
4893 del
->br_blockcount
);
4895 got
->br_startblock
= nullstartblock((int)got_indlen
);
4897 new.br_startoff
= del_endoff
;
4898 new.br_state
= got
->br_state
;
4899 new.br_startblock
= nullstartblock((int)new_indlen
);
4901 xfs_iext_update_extent(ip
, state
, icur
, got
);
4902 xfs_iext_next(ifp
, icur
);
4903 xfs_iext_insert(ip
, icur
, &new, state
);
4905 da_new
= got_indlen
+ new_indlen
- stolen
;
4906 del
->br_blockcount
-= stolen
;
4910 ASSERT(da_old
>= da_new
);
4911 da_diff
= da_old
- da_new
;
4913 da_diff
+= del
->br_blockcount
;
4915 xfs_mod_fdblocks(mp
, da_diff
, false);
4916 xfs_mod_delalloc(mp
, -da_diff
);
4922 xfs_bmap_del_extent_cow(
4923 struct xfs_inode
*ip
,
4924 struct xfs_iext_cursor
*icur
,
4925 struct xfs_bmbt_irec
*got
,
4926 struct xfs_bmbt_irec
*del
)
4928 struct xfs_mount
*mp
= ip
->i_mount
;
4929 struct xfs_ifork
*ifp
= xfs_ifork_ptr(ip
, XFS_COW_FORK
);
4930 struct xfs_bmbt_irec
new;
4931 xfs_fileoff_t del_endoff
, got_endoff
;
4932 uint32_t state
= BMAP_COWFORK
;
4934 XFS_STATS_INC(mp
, xs_del_exlist
);
4936 del_endoff
= del
->br_startoff
+ del
->br_blockcount
;
4937 got_endoff
= got
->br_startoff
+ got
->br_blockcount
;
4939 ASSERT(del
->br_blockcount
> 0);
4940 ASSERT(got
->br_startoff
<= del
->br_startoff
);
4941 ASSERT(got_endoff
>= del_endoff
);
4942 ASSERT(!isnullstartblock(got
->br_startblock
));
4944 if (got
->br_startoff
== del
->br_startoff
)
4945 state
|= BMAP_LEFT_FILLING
;
4946 if (got_endoff
== del_endoff
)
4947 state
|= BMAP_RIGHT_FILLING
;
4949 switch (state
& (BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
)) {
4950 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
4952 * Matches the whole extent. Delete the entry.
4954 xfs_iext_remove(ip
, icur
, state
);
4955 xfs_iext_prev(ifp
, icur
);
4957 case BMAP_LEFT_FILLING
:
4959 * Deleting the first part of the extent.
4961 got
->br_startoff
= del_endoff
;
4962 got
->br_blockcount
-= del
->br_blockcount
;
4963 got
->br_startblock
= del
->br_startblock
+ del
->br_blockcount
;
4964 xfs_iext_update_extent(ip
, state
, icur
, got
);
4966 case BMAP_RIGHT_FILLING
:
4968 * Deleting the last part of the extent.
4970 got
->br_blockcount
-= del
->br_blockcount
;
4971 xfs_iext_update_extent(ip
, state
, icur
, got
);
4975 * Deleting the middle of the extent.
4977 got
->br_blockcount
= del
->br_startoff
- got
->br_startoff
;
4979 new.br_startoff
= del_endoff
;
4980 new.br_blockcount
= got_endoff
- del_endoff
;
4981 new.br_state
= got
->br_state
;
4982 new.br_startblock
= del
->br_startblock
+ del
->br_blockcount
;
4984 xfs_iext_update_extent(ip
, state
, icur
, got
);
4985 xfs_iext_next(ifp
, icur
);
4986 xfs_iext_insert(ip
, icur
, &new, state
);
4989 ip
->i_delayed_blks
-= del
->br_blockcount
;
4993 * Called by xfs_bmapi to update file extent records and the btree
4994 * after removing space.
4996 STATIC
int /* error */
4997 xfs_bmap_del_extent_real(
4998 xfs_inode_t
*ip
, /* incore inode pointer */
4999 xfs_trans_t
*tp
, /* current transaction pointer */
5000 struct xfs_iext_cursor
*icur
,
5001 struct xfs_btree_cur
*cur
, /* if null, not a btree */
5002 xfs_bmbt_irec_t
*del
, /* data to remove from extents */
5003 int *logflagsp
, /* inode logging flags */
5004 int whichfork
, /* data or attr fork */
5005 uint32_t bflags
) /* bmapi flags */
5007 xfs_fsblock_t del_endblock
=0; /* first block past del */
5008 xfs_fileoff_t del_endoff
; /* first offset past del */
5009 int do_fx
; /* free extent at end of routine */
5010 int error
; /* error return value */
5011 int flags
= 0;/* inode logging flags */
5012 struct xfs_bmbt_irec got
; /* current extent entry */
5013 xfs_fileoff_t got_endoff
; /* first offset past got */
5014 int i
; /* temp state */
5015 struct xfs_ifork
*ifp
; /* inode fork pointer */
5016 xfs_mount_t
*mp
; /* mount structure */
5017 xfs_filblks_t nblks
; /* quota/sb block count */
5018 xfs_bmbt_irec_t
new; /* new record to be inserted */
5020 uint qfield
; /* quota field to update */
5021 uint32_t state
= xfs_bmap_fork_to_state(whichfork
);
5022 struct xfs_bmbt_irec old
;
5025 XFS_STATS_INC(mp
, xs_del_exlist
);
5027 ifp
= xfs_ifork_ptr(ip
, whichfork
);
5028 ASSERT(del
->br_blockcount
> 0);
5029 xfs_iext_get_extent(ifp
, icur
, &got
);
5030 ASSERT(got
.br_startoff
<= del
->br_startoff
);
5031 del_endoff
= del
->br_startoff
+ del
->br_blockcount
;
5032 got_endoff
= got
.br_startoff
+ got
.br_blockcount
;
5033 ASSERT(got_endoff
>= del_endoff
);
5034 ASSERT(!isnullstartblock(got
.br_startblock
));
5039 * If it's the case where the directory code is running with no block
5040 * reservation, and the deleted block is in the middle of its extent,
5041 * and the resulting insert of an extent would cause transformation to
5042 * btree format, then reject it. The calling code will then swap blocks
5043 * around instead. We have to do this now, rather than waiting for the
5044 * conversion to btree format, since the transaction will be dirty then.
5046 if (tp
->t_blk_res
== 0 &&
5047 ifp
->if_format
== XFS_DINODE_FMT_EXTENTS
&&
5048 ifp
->if_nextents
>= XFS_IFORK_MAXEXT(ip
, whichfork
) &&
5049 del
->br_startoff
> got
.br_startoff
&& del_endoff
< got_endoff
)
5052 flags
= XFS_ILOG_CORE
;
5053 if (whichfork
== XFS_DATA_FORK
&& XFS_IS_REALTIME_INODE(ip
)) {
5054 if (!(bflags
& XFS_BMAPI_REMAP
)) {
5055 error
= xfs_rtfree_blocks(tp
, del
->br_startblock
,
5056 del
->br_blockcount
);
5062 qfield
= XFS_TRANS_DQ_RTBCOUNT
;
5065 qfield
= XFS_TRANS_DQ_BCOUNT
;
5067 nblks
= del
->br_blockcount
;
5069 del_endblock
= del
->br_startblock
+ del
->br_blockcount
;
5071 error
= xfs_bmbt_lookup_eq(cur
, &got
, &i
);
5074 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
5075 error
= -EFSCORRUPTED
;
5080 if (got
.br_startoff
== del
->br_startoff
)
5081 state
|= BMAP_LEFT_FILLING
;
5082 if (got_endoff
== del_endoff
)
5083 state
|= BMAP_RIGHT_FILLING
;
5085 switch (state
& (BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
)) {
5086 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
5088 * Matches the whole extent. Delete the entry.
5090 xfs_iext_remove(ip
, icur
, state
);
5091 xfs_iext_prev(ifp
, icur
);
5094 flags
|= XFS_ILOG_CORE
;
5096 flags
|= xfs_ilog_fext(whichfork
);
5099 if ((error
= xfs_btree_delete(cur
, &i
)))
5101 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
5102 error
= -EFSCORRUPTED
;
5106 case BMAP_LEFT_FILLING
:
5108 * Deleting the first part of the extent.
5110 got
.br_startoff
= del_endoff
;
5111 got
.br_startblock
= del_endblock
;
5112 got
.br_blockcount
-= del
->br_blockcount
;
5113 xfs_iext_update_extent(ip
, state
, icur
, &got
);
5115 flags
|= xfs_ilog_fext(whichfork
);
5118 error
= xfs_bmbt_update(cur
, &got
);
5122 case BMAP_RIGHT_FILLING
:
5124 * Deleting the last part of the extent.
5126 got
.br_blockcount
-= del
->br_blockcount
;
5127 xfs_iext_update_extent(ip
, state
, icur
, &got
);
5129 flags
|= xfs_ilog_fext(whichfork
);
5132 error
= xfs_bmbt_update(cur
, &got
);
5138 * Deleting the middle of the extent.
5143 got
.br_blockcount
= del
->br_startoff
- got
.br_startoff
;
5144 xfs_iext_update_extent(ip
, state
, icur
, &got
);
5146 new.br_startoff
= del_endoff
;
5147 new.br_blockcount
= got_endoff
- del_endoff
;
5148 new.br_state
= got
.br_state
;
5149 new.br_startblock
= del_endblock
;
5151 flags
|= XFS_ILOG_CORE
;
5153 error
= xfs_bmbt_update(cur
, &got
);
5156 error
= xfs_btree_increment(cur
, 0, &i
);
5159 cur
->bc_rec
.b
= new;
5160 error
= xfs_btree_insert(cur
, &i
);
5161 if (error
&& error
!= -ENOSPC
)
5164 * If get no-space back from btree insert, it tried a
5165 * split, and we have a zero block reservation. Fix up
5166 * our state and return the error.
5168 if (error
== -ENOSPC
) {
5170 * Reset the cursor, don't trust it after any
5173 error
= xfs_bmbt_lookup_eq(cur
, &got
, &i
);
5176 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
5177 error
= -EFSCORRUPTED
;
5181 * Update the btree record back
5182 * to the original value.
5184 error
= xfs_bmbt_update(cur
, &old
);
5188 * Reset the extent record back
5189 * to the original value.
5191 xfs_iext_update_extent(ip
, state
, icur
, &old
);
5196 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
5197 error
= -EFSCORRUPTED
;
5201 flags
|= xfs_ilog_fext(whichfork
);
5204 xfs_iext_next(ifp
, icur
);
5205 xfs_iext_insert(ip
, icur
, &new, state
);
5209 /* remove reverse mapping */
5210 xfs_rmap_unmap_extent(tp
, ip
, whichfork
, del
);
5213 * If we need to, add to list of extents to delete.
5215 if (do_fx
&& !(bflags
& XFS_BMAPI_REMAP
)) {
5216 if (xfs_is_reflink_inode(ip
) && whichfork
== XFS_DATA_FORK
) {
5217 xfs_refcount_decrease_extent(tp
, del
);
5219 error
= __xfs_free_extent_later(tp
, del
->br_startblock
,
5220 del
->br_blockcount
, NULL
,
5222 ((bflags
& XFS_BMAPI_NODISCARD
) ||
5223 del
->br_state
== XFS_EXT_UNWRITTEN
));
5230 * Adjust inode # blocks in the file.
5233 ip
->i_nblocks
-= nblks
;
5235 * Adjust quota data.
5237 if (qfield
&& !(bflags
& XFS_BMAPI_REMAP
))
5238 xfs_trans_mod_dquot_byino(tp
, ip
, qfield
, (long)-nblks
);
5246 * Unmap (remove) blocks from a file.
5247 * If nexts is nonzero then the number of extents to remove is limited to
5248 * that value. If not all extents in the block range can be removed then
5253 struct xfs_trans
*tp
, /* transaction pointer */
5254 struct xfs_inode
*ip
, /* incore inode */
5255 xfs_fileoff_t start
, /* first file offset deleted */
5256 xfs_filblks_t
*rlen
, /* i/o: amount remaining */
5257 uint32_t flags
, /* misc flags */
5258 xfs_extnum_t nexts
) /* number of extents max */
5260 struct xfs_btree_cur
*cur
; /* bmap btree cursor */
5261 struct xfs_bmbt_irec del
; /* extent being deleted */
5262 int error
; /* error return value */
5263 xfs_extnum_t extno
; /* extent number in list */
5264 struct xfs_bmbt_irec got
; /* current extent record */
5265 struct xfs_ifork
*ifp
; /* inode fork pointer */
5266 int isrt
; /* freeing in rt area */
5267 int logflags
; /* transaction logging flags */
5268 xfs_extlen_t mod
; /* rt extent offset */
5269 struct xfs_mount
*mp
= ip
->i_mount
;
5270 int tmp_logflags
; /* partial logging flags */
5271 int wasdel
; /* was a delayed alloc extent */
5272 int whichfork
; /* data or attribute fork */
5274 xfs_filblks_t len
= *rlen
; /* length to unmap in file */
5276 struct xfs_iext_cursor icur
;
5279 trace_xfs_bunmap(ip
, start
, len
, flags
, _RET_IP_
);
5281 whichfork
= xfs_bmapi_whichfork(flags
);
5282 ASSERT(whichfork
!= XFS_COW_FORK
);
5283 ifp
= xfs_ifork_ptr(ip
, whichfork
);
5284 if (XFS_IS_CORRUPT(mp
, !xfs_ifork_has_extents(ifp
)))
5285 return -EFSCORRUPTED
;
5286 if (xfs_is_shutdown(mp
))
5289 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
5293 error
= xfs_iread_extents(tp
, ip
, whichfork
);
5297 if (xfs_iext_count(ifp
) == 0) {
5301 XFS_STATS_INC(mp
, xs_blk_unmap
);
5302 isrt
= (whichfork
== XFS_DATA_FORK
) && XFS_IS_REALTIME_INODE(ip
);
5305 if (!xfs_iext_lookup_extent_before(ip
, ifp
, &end
, &icur
, &got
)) {
5312 if (ifp
->if_format
== XFS_DINODE_FMT_BTREE
) {
5313 ASSERT(ifp
->if_format
== XFS_DINODE_FMT_BTREE
);
5314 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
5315 cur
->bc_ino
.flags
= 0;
5321 * Synchronize by locking the bitmap inode.
5323 xfs_ilock(mp
->m_rbmip
, XFS_ILOCK_EXCL
|XFS_ILOCK_RTBITMAP
);
5324 xfs_trans_ijoin(tp
, mp
->m_rbmip
, XFS_ILOCK_EXCL
);
5325 xfs_ilock(mp
->m_rsumip
, XFS_ILOCK_EXCL
|XFS_ILOCK_RTSUM
);
5326 xfs_trans_ijoin(tp
, mp
->m_rsumip
, XFS_ILOCK_EXCL
);
5330 while (end
!= (xfs_fileoff_t
)-1 && end
>= start
&&
5331 (nexts
== 0 || extno
< nexts
)) {
5333 * Is the found extent after a hole in which end lives?
5334 * Just back up to the previous extent, if so.
5336 if (got
.br_startoff
> end
&&
5337 !xfs_iext_prev_extent(ifp
, &icur
, &got
)) {
5342 * Is the last block of this extent before the range
5343 * we're supposed to delete? If so, we're done.
5345 end
= XFS_FILEOFF_MIN(end
,
5346 got
.br_startoff
+ got
.br_blockcount
- 1);
5350 * Then deal with the (possibly delayed) allocated space
5354 wasdel
= isnullstartblock(del
.br_startblock
);
5356 if (got
.br_startoff
< start
) {
5357 del
.br_startoff
= start
;
5358 del
.br_blockcount
-= start
- got
.br_startoff
;
5360 del
.br_startblock
+= start
- got
.br_startoff
;
5362 if (del
.br_startoff
+ del
.br_blockcount
> end
+ 1)
5363 del
.br_blockcount
= end
+ 1 - del
.br_startoff
;
5368 sum
= del
.br_startblock
+ del
.br_blockcount
;
5369 div_u64_rem(sum
, mp
->m_sb
.sb_rextsize
, &mod
);
5372 * Realtime extent not lined up at the end.
5373 * The extent could have been split into written
5374 * and unwritten pieces, or we could just be
5375 * unmapping part of it. But we can't really
5376 * get rid of part of a realtime extent.
5378 if (del
.br_state
== XFS_EXT_UNWRITTEN
) {
5380 * This piece is unwritten, or we're not
5381 * using unwritten extents. Skip over it.
5384 end
-= mod
> del
.br_blockcount
?
5385 del
.br_blockcount
: mod
;
5386 if (end
< got
.br_startoff
&&
5387 !xfs_iext_prev_extent(ifp
, &icur
, &got
)) {
5394 * It's written, turn it unwritten.
5395 * This is better than zeroing it.
5397 ASSERT(del
.br_state
== XFS_EXT_NORM
);
5398 ASSERT(tp
->t_blk_res
> 0);
5400 * If this spans a realtime extent boundary,
5401 * chop it back to the start of the one we end at.
5403 if (del
.br_blockcount
> mod
) {
5404 del
.br_startoff
+= del
.br_blockcount
- mod
;
5405 del
.br_startblock
+= del
.br_blockcount
- mod
;
5406 del
.br_blockcount
= mod
;
5408 del
.br_state
= XFS_EXT_UNWRITTEN
;
5409 error
= xfs_bmap_add_extent_unwritten_real(tp
, ip
,
5410 whichfork
, &icur
, &cur
, &del
,
5416 div_u64_rem(del
.br_startblock
, mp
->m_sb
.sb_rextsize
, &mod
);
5418 xfs_extlen_t off
= mp
->m_sb
.sb_rextsize
- mod
;
5421 * Realtime extent is lined up at the end but not
5422 * at the front. We'll get rid of full extents if
5425 if (del
.br_blockcount
> off
) {
5426 del
.br_blockcount
-= off
;
5427 del
.br_startoff
+= off
;
5428 del
.br_startblock
+= off
;
5429 } else if (del
.br_startoff
== start
&&
5430 (del
.br_state
== XFS_EXT_UNWRITTEN
||
5431 tp
->t_blk_res
== 0)) {
5433 * Can't make it unwritten. There isn't
5434 * a full extent here so just skip it.
5436 ASSERT(end
>= del
.br_blockcount
);
5437 end
-= del
.br_blockcount
;
5438 if (got
.br_startoff
> end
&&
5439 !xfs_iext_prev_extent(ifp
, &icur
, &got
)) {
5444 } else if (del
.br_state
== XFS_EXT_UNWRITTEN
) {
5445 struct xfs_bmbt_irec prev
;
5446 xfs_fileoff_t unwrite_start
;
5449 * This one is already unwritten.
5450 * It must have a written left neighbor.
5451 * Unwrite the killed part of that one and
5454 if (!xfs_iext_prev_extent(ifp
, &icur
, &prev
))
5456 ASSERT(prev
.br_state
== XFS_EXT_NORM
);
5457 ASSERT(!isnullstartblock(prev
.br_startblock
));
5458 ASSERT(del
.br_startblock
==
5459 prev
.br_startblock
+ prev
.br_blockcount
);
5460 unwrite_start
= max3(start
,
5461 del
.br_startoff
- mod
,
5463 mod
= unwrite_start
- prev
.br_startoff
;
5464 prev
.br_startoff
= unwrite_start
;
5465 prev
.br_startblock
+= mod
;
5466 prev
.br_blockcount
-= mod
;
5467 prev
.br_state
= XFS_EXT_UNWRITTEN
;
5468 error
= xfs_bmap_add_extent_unwritten_real(tp
,
5469 ip
, whichfork
, &icur
, &cur
,
5475 ASSERT(del
.br_state
== XFS_EXT_NORM
);
5476 del
.br_state
= XFS_EXT_UNWRITTEN
;
5477 error
= xfs_bmap_add_extent_unwritten_real(tp
,
5478 ip
, whichfork
, &icur
, &cur
,
5488 error
= xfs_bmap_del_extent_delay(ip
, whichfork
, &icur
,
5491 error
= xfs_bmap_del_extent_real(ip
, tp
, &icur
, cur
,
5492 &del
, &tmp_logflags
, whichfork
,
5494 logflags
|= tmp_logflags
;
5500 end
= del
.br_startoff
- 1;
5503 * If not done go on to the next (previous) record.
5505 if (end
!= (xfs_fileoff_t
)-1 && end
>= start
) {
5506 if (!xfs_iext_get_extent(ifp
, &icur
, &got
) ||
5507 (got
.br_startoff
> end
&&
5508 !xfs_iext_prev_extent(ifp
, &icur
, &got
))) {
5515 if (done
|| end
== (xfs_fileoff_t
)-1 || end
< start
)
5518 *rlen
= end
- start
+ 1;
5521 * Convert to a btree if necessary.
5523 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
5524 ASSERT(cur
== NULL
);
5525 error
= xfs_bmap_extents_to_btree(tp
, ip
, &cur
, 0,
5526 &tmp_logflags
, whichfork
);
5527 logflags
|= tmp_logflags
;
5529 error
= xfs_bmap_btree_to_extents(tp
, ip
, cur
, &logflags
,
5535 * Log everything. Do this after conversion, there's no point in
5536 * logging the extent records if we've converted to btree format.
5538 if ((logflags
& xfs_ilog_fext(whichfork
)) &&
5539 ifp
->if_format
!= XFS_DINODE_FMT_EXTENTS
)
5540 logflags
&= ~xfs_ilog_fext(whichfork
);
5541 else if ((logflags
& xfs_ilog_fbroot(whichfork
)) &&
5542 ifp
->if_format
!= XFS_DINODE_FMT_BTREE
)
5543 logflags
&= ~xfs_ilog_fbroot(whichfork
);
5545 * Log inode even in the error case, if the transaction
5546 * is dirty we'll need to shut down the filesystem.
5549 xfs_trans_log_inode(tp
, ip
, logflags
);
5552 cur
->bc_ino
.allocated
= 0;
5553 xfs_btree_del_cursor(cur
, error
);
5558 /* Unmap a range of a file. */
5562 struct xfs_inode
*ip
,
5571 error
= __xfs_bunmapi(tp
, ip
, bno
, &len
, flags
, nexts
);
5577 * Determine whether an extent shift can be accomplished by a merge with the
5578 * extent that precedes the target hole of the shift.
5582 struct xfs_bmbt_irec
*left
, /* preceding extent */
5583 struct xfs_bmbt_irec
*got
, /* current extent to shift */
5584 xfs_fileoff_t shift
) /* shift fsb */
5586 xfs_fileoff_t startoff
;
5588 startoff
= got
->br_startoff
- shift
;
5591 * The extent, once shifted, must be adjacent in-file and on-disk with
5592 * the preceding extent.
5594 if ((left
->br_startoff
+ left
->br_blockcount
!= startoff
) ||
5595 (left
->br_startblock
+ left
->br_blockcount
!= got
->br_startblock
) ||
5596 (left
->br_state
!= got
->br_state
) ||
5597 (left
->br_blockcount
+ got
->br_blockcount
> XFS_MAX_BMBT_EXTLEN
))
5604 * A bmap extent shift adjusts the file offset of an extent to fill a preceding
5605 * hole in the file. If an extent shift would result in the extent being fully
5606 * adjacent to the extent that currently precedes the hole, we can merge with
5607 * the preceding extent rather than do the shift.
5609 * This function assumes the caller has verified a shift-by-merge is possible
5610 * with the provided extents via xfs_bmse_can_merge().
5614 struct xfs_trans
*tp
,
5615 struct xfs_inode
*ip
,
5617 xfs_fileoff_t shift
, /* shift fsb */
5618 struct xfs_iext_cursor
*icur
,
5619 struct xfs_bmbt_irec
*got
, /* extent to shift */
5620 struct xfs_bmbt_irec
*left
, /* preceding extent */
5621 struct xfs_btree_cur
*cur
,
5622 int *logflags
) /* output */
5624 struct xfs_ifork
*ifp
= xfs_ifork_ptr(ip
, whichfork
);
5625 struct xfs_bmbt_irec
new;
5626 xfs_filblks_t blockcount
;
5628 struct xfs_mount
*mp
= ip
->i_mount
;
5630 blockcount
= left
->br_blockcount
+ got
->br_blockcount
;
5632 ASSERT(xfs_isilocked(ip
, XFS_IOLOCK_EXCL
));
5633 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
5634 ASSERT(xfs_bmse_can_merge(left
, got
, shift
));
5637 new.br_blockcount
= blockcount
;
5640 * Update the on-disk extent count, the btree if necessary and log the
5644 *logflags
|= XFS_ILOG_CORE
;
5646 *logflags
|= XFS_ILOG_DEXT
;
5650 /* lookup and remove the extent to merge */
5651 error
= xfs_bmbt_lookup_eq(cur
, got
, &i
);
5654 if (XFS_IS_CORRUPT(mp
, i
!= 1))
5655 return -EFSCORRUPTED
;
5657 error
= xfs_btree_delete(cur
, &i
);
5660 if (XFS_IS_CORRUPT(mp
, i
!= 1))
5661 return -EFSCORRUPTED
;
5663 /* lookup and update size of the previous extent */
5664 error
= xfs_bmbt_lookup_eq(cur
, left
, &i
);
5667 if (XFS_IS_CORRUPT(mp
, i
!= 1))
5668 return -EFSCORRUPTED
;
5670 error
= xfs_bmbt_update(cur
, &new);
5674 /* change to extent format if required after extent removal */
5675 error
= xfs_bmap_btree_to_extents(tp
, ip
, cur
, logflags
, whichfork
);
5680 xfs_iext_remove(ip
, icur
, 0);
5681 xfs_iext_prev(ifp
, icur
);
5682 xfs_iext_update_extent(ip
, xfs_bmap_fork_to_state(whichfork
), icur
,
5685 /* update reverse mapping. rmap functions merge the rmaps for us */
5686 xfs_rmap_unmap_extent(tp
, ip
, whichfork
, got
);
5687 memcpy(&new, got
, sizeof(new));
5688 new.br_startoff
= left
->br_startoff
+ left
->br_blockcount
;
5689 xfs_rmap_map_extent(tp
, ip
, whichfork
, &new);
5694 xfs_bmap_shift_update_extent(
5695 struct xfs_trans
*tp
,
5696 struct xfs_inode
*ip
,
5698 struct xfs_iext_cursor
*icur
,
5699 struct xfs_bmbt_irec
*got
,
5700 struct xfs_btree_cur
*cur
,
5702 xfs_fileoff_t startoff
)
5704 struct xfs_mount
*mp
= ip
->i_mount
;
5705 struct xfs_bmbt_irec prev
= *got
;
5708 *logflags
|= XFS_ILOG_CORE
;
5710 got
->br_startoff
= startoff
;
5713 error
= xfs_bmbt_lookup_eq(cur
, &prev
, &i
);
5716 if (XFS_IS_CORRUPT(mp
, i
!= 1))
5717 return -EFSCORRUPTED
;
5719 error
= xfs_bmbt_update(cur
, got
);
5723 *logflags
|= XFS_ILOG_DEXT
;
5726 xfs_iext_update_extent(ip
, xfs_bmap_fork_to_state(whichfork
), icur
,
5729 /* update reverse mapping */
5730 xfs_rmap_unmap_extent(tp
, ip
, whichfork
, &prev
);
5731 xfs_rmap_map_extent(tp
, ip
, whichfork
, got
);
5736 xfs_bmap_collapse_extents(
5737 struct xfs_trans
*tp
,
5738 struct xfs_inode
*ip
,
5739 xfs_fileoff_t
*next_fsb
,
5740 xfs_fileoff_t offset_shift_fsb
,
5743 int whichfork
= XFS_DATA_FORK
;
5744 struct xfs_mount
*mp
= ip
->i_mount
;
5745 struct xfs_ifork
*ifp
= xfs_ifork_ptr(ip
, whichfork
);
5746 struct xfs_btree_cur
*cur
= NULL
;
5747 struct xfs_bmbt_irec got
, prev
;
5748 struct xfs_iext_cursor icur
;
5749 xfs_fileoff_t new_startoff
;
5753 if (XFS_IS_CORRUPT(mp
, !xfs_ifork_has_extents(ifp
)) ||
5754 XFS_TEST_ERROR(false, mp
, XFS_ERRTAG_BMAPIFORMAT
)) {
5755 return -EFSCORRUPTED
;
5758 if (xfs_is_shutdown(mp
))
5761 ASSERT(xfs_isilocked(ip
, XFS_IOLOCK_EXCL
| XFS_ILOCK_EXCL
));
5763 error
= xfs_iread_extents(tp
, ip
, whichfork
);
5767 if (ifp
->if_format
== XFS_DINODE_FMT_BTREE
) {
5768 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
5769 cur
->bc_ino
.flags
= 0;
5772 if (!xfs_iext_lookup_extent(ip
, ifp
, *next_fsb
, &icur
, &got
)) {
5776 if (XFS_IS_CORRUPT(mp
, isnullstartblock(got
.br_startblock
))) {
5777 error
= -EFSCORRUPTED
;
5781 new_startoff
= got
.br_startoff
- offset_shift_fsb
;
5782 if (xfs_iext_peek_prev_extent(ifp
, &icur
, &prev
)) {
5783 if (new_startoff
< prev
.br_startoff
+ prev
.br_blockcount
) {
5788 if (xfs_bmse_can_merge(&prev
, &got
, offset_shift_fsb
)) {
5789 error
= xfs_bmse_merge(tp
, ip
, whichfork
,
5790 offset_shift_fsb
, &icur
, &got
, &prev
,
5797 if (got
.br_startoff
< offset_shift_fsb
) {
5803 error
= xfs_bmap_shift_update_extent(tp
, ip
, whichfork
, &icur
, &got
,
5804 cur
, &logflags
, new_startoff
);
5809 if (!xfs_iext_next_extent(ifp
, &icur
, &got
)) {
5814 *next_fsb
= got
.br_startoff
;
5817 xfs_btree_del_cursor(cur
, error
);
5819 xfs_trans_log_inode(tp
, ip
, logflags
);
5823 /* Make sure we won't be right-shifting an extent past the maximum bound. */
5825 xfs_bmap_can_insert_extents(
5826 struct xfs_inode
*ip
,
5828 xfs_fileoff_t shift
)
5830 struct xfs_bmbt_irec got
;
5834 ASSERT(xfs_isilocked(ip
, XFS_IOLOCK_EXCL
));
5836 if (xfs_is_shutdown(ip
->i_mount
))
5839 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
5840 error
= xfs_bmap_last_extent(NULL
, ip
, XFS_DATA_FORK
, &got
, &is_empty
);
5841 if (!error
&& !is_empty
&& got
.br_startoff
>= off
&&
5842 ((got
.br_startoff
+ shift
) & BMBT_STARTOFF_MASK
) < got
.br_startoff
)
5844 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
5850 xfs_bmap_insert_extents(
5851 struct xfs_trans
*tp
,
5852 struct xfs_inode
*ip
,
5853 xfs_fileoff_t
*next_fsb
,
5854 xfs_fileoff_t offset_shift_fsb
,
5856 xfs_fileoff_t stop_fsb
)
5858 int whichfork
= XFS_DATA_FORK
;
5859 struct xfs_mount
*mp
= ip
->i_mount
;
5860 struct xfs_ifork
*ifp
= xfs_ifork_ptr(ip
, whichfork
);
5861 struct xfs_btree_cur
*cur
= NULL
;
5862 struct xfs_bmbt_irec got
, next
;
5863 struct xfs_iext_cursor icur
;
5864 xfs_fileoff_t new_startoff
;
5868 if (XFS_IS_CORRUPT(mp
, !xfs_ifork_has_extents(ifp
)) ||
5869 XFS_TEST_ERROR(false, mp
, XFS_ERRTAG_BMAPIFORMAT
)) {
5870 return -EFSCORRUPTED
;
5873 if (xfs_is_shutdown(mp
))
5876 ASSERT(xfs_isilocked(ip
, XFS_IOLOCK_EXCL
| XFS_ILOCK_EXCL
));
5878 error
= xfs_iread_extents(tp
, ip
, whichfork
);
5882 if (ifp
->if_format
== XFS_DINODE_FMT_BTREE
) {
5883 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
5884 cur
->bc_ino
.flags
= 0;
5887 if (*next_fsb
== NULLFSBLOCK
) {
5888 xfs_iext_last(ifp
, &icur
);
5889 if (!xfs_iext_get_extent(ifp
, &icur
, &got
) ||
5890 stop_fsb
> got
.br_startoff
) {
5895 if (!xfs_iext_lookup_extent(ip
, ifp
, *next_fsb
, &icur
, &got
)) {
5900 if (XFS_IS_CORRUPT(mp
, isnullstartblock(got
.br_startblock
))) {
5901 error
= -EFSCORRUPTED
;
5905 if (XFS_IS_CORRUPT(mp
, stop_fsb
> got
.br_startoff
)) {
5906 error
= -EFSCORRUPTED
;
5910 new_startoff
= got
.br_startoff
+ offset_shift_fsb
;
5911 if (xfs_iext_peek_next_extent(ifp
, &icur
, &next
)) {
5912 if (new_startoff
+ got
.br_blockcount
> next
.br_startoff
) {
5918 * Unlike a left shift (which involves a hole punch), a right
5919 * shift does not modify extent neighbors in any way. We should
5920 * never find mergeable extents in this scenario. Check anyways
5921 * and warn if we encounter two extents that could be one.
5923 if (xfs_bmse_can_merge(&got
, &next
, offset_shift_fsb
))
5927 error
= xfs_bmap_shift_update_extent(tp
, ip
, whichfork
, &icur
, &got
,
5928 cur
, &logflags
, new_startoff
);
5932 if (!xfs_iext_prev_extent(ifp
, &icur
, &got
) ||
5933 stop_fsb
>= got
.br_startoff
+ got
.br_blockcount
) {
5938 *next_fsb
= got
.br_startoff
;
5941 xfs_btree_del_cursor(cur
, error
);
5943 xfs_trans_log_inode(tp
, ip
, logflags
);
5948 * Splits an extent into two extents at split_fsb block such that it is the
5949 * first block of the current_ext. @ext is a target extent to be split.
5950 * @split_fsb is a block where the extents is split. If split_fsb lies in a
5951 * hole or the first block of extents, just return 0.
5954 xfs_bmap_split_extent(
5955 struct xfs_trans
*tp
,
5956 struct xfs_inode
*ip
,
5957 xfs_fileoff_t split_fsb
)
5959 int whichfork
= XFS_DATA_FORK
;
5960 struct xfs_ifork
*ifp
= xfs_ifork_ptr(ip
, whichfork
);
5961 struct xfs_btree_cur
*cur
= NULL
;
5962 struct xfs_bmbt_irec got
;
5963 struct xfs_bmbt_irec
new; /* split extent */
5964 struct xfs_mount
*mp
= ip
->i_mount
;
5965 xfs_fsblock_t gotblkcnt
; /* new block count for got */
5966 struct xfs_iext_cursor icur
;
5971 if (XFS_IS_CORRUPT(mp
, !xfs_ifork_has_extents(ifp
)) ||
5972 XFS_TEST_ERROR(false, mp
, XFS_ERRTAG_BMAPIFORMAT
)) {
5973 return -EFSCORRUPTED
;
5976 if (xfs_is_shutdown(mp
))
5979 /* Read in all the extents */
5980 error
= xfs_iread_extents(tp
, ip
, whichfork
);
5985 * If there are not extents, or split_fsb lies in a hole we are done.
5987 if (!xfs_iext_lookup_extent(ip
, ifp
, split_fsb
, &icur
, &got
) ||
5988 got
.br_startoff
>= split_fsb
)
5991 gotblkcnt
= split_fsb
- got
.br_startoff
;
5992 new.br_startoff
= split_fsb
;
5993 new.br_startblock
= got
.br_startblock
+ gotblkcnt
;
5994 new.br_blockcount
= got
.br_blockcount
- gotblkcnt
;
5995 new.br_state
= got
.br_state
;
5997 if (ifp
->if_format
== XFS_DINODE_FMT_BTREE
) {
5998 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
5999 cur
->bc_ino
.flags
= 0;
6000 error
= xfs_bmbt_lookup_eq(cur
, &got
, &i
);
6003 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
6004 error
= -EFSCORRUPTED
;
6009 got
.br_blockcount
= gotblkcnt
;
6010 xfs_iext_update_extent(ip
, xfs_bmap_fork_to_state(whichfork
), &icur
,
6013 logflags
= XFS_ILOG_CORE
;
6015 error
= xfs_bmbt_update(cur
, &got
);
6019 logflags
|= XFS_ILOG_DEXT
;
6021 /* Add new extent */
6022 xfs_iext_next(ifp
, &icur
);
6023 xfs_iext_insert(ip
, &icur
, &new, 0);
6027 error
= xfs_bmbt_lookup_eq(cur
, &new, &i
);
6030 if (XFS_IS_CORRUPT(mp
, i
!= 0)) {
6031 error
= -EFSCORRUPTED
;
6034 error
= xfs_btree_insert(cur
, &i
);
6037 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
6038 error
= -EFSCORRUPTED
;
6044 * Convert to a btree if necessary.
6046 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
6047 int tmp_logflags
; /* partial log flag return val */
6049 ASSERT(cur
== NULL
);
6050 error
= xfs_bmap_extents_to_btree(tp
, ip
, &cur
, 0,
6051 &tmp_logflags
, whichfork
);
6052 logflags
|= tmp_logflags
;
6057 cur
->bc_ino
.allocated
= 0;
6058 xfs_btree_del_cursor(cur
, error
);
6062 xfs_trans_log_inode(tp
, ip
, logflags
);
6066 /* Deferred mapping is only for real extents in the data fork. */
6068 xfs_bmap_is_update_needed(
6069 struct xfs_bmbt_irec
*bmap
)
6071 return bmap
->br_startblock
!= HOLESTARTBLOCK
&&
6072 bmap
->br_startblock
!= DELAYSTARTBLOCK
;
6075 /* Record a bmap intent. */
6078 struct xfs_trans
*tp
,
6079 enum xfs_bmap_intent_type type
,
6080 struct xfs_inode
*ip
,
6082 struct xfs_bmbt_irec
*bmap
)
6084 struct xfs_bmap_intent
*bi
;
6086 trace_xfs_bmap_defer(tp
->t_mountp
,
6087 XFS_FSB_TO_AGNO(tp
->t_mountp
, bmap
->br_startblock
),
6089 XFS_FSB_TO_AGBNO(tp
->t_mountp
, bmap
->br_startblock
),
6090 ip
->i_ino
, whichfork
,
6092 bmap
->br_blockcount
,
6095 bi
= kmem_cache_alloc(xfs_bmap_intent_cache
, GFP_NOFS
| __GFP_NOFAIL
);
6096 INIT_LIST_HEAD(&bi
->bi_list
);
6099 bi
->bi_whichfork
= whichfork
;
6100 bi
->bi_bmap
= *bmap
;
6102 xfs_bmap_update_get_group(tp
->t_mountp
, bi
);
6103 xfs_defer_add(tp
, XFS_DEFER_OPS_TYPE_BMAP
, &bi
->bi_list
);
6107 /* Map an extent into a file. */
6109 xfs_bmap_map_extent(
6110 struct xfs_trans
*tp
,
6111 struct xfs_inode
*ip
,
6112 struct xfs_bmbt_irec
*PREV
)
6114 if (!xfs_bmap_is_update_needed(PREV
))
6117 __xfs_bmap_add(tp
, XFS_BMAP_MAP
, ip
, XFS_DATA_FORK
, PREV
);
6120 /* Unmap an extent out of a file. */
6122 xfs_bmap_unmap_extent(
6123 struct xfs_trans
*tp
,
6124 struct xfs_inode
*ip
,
6125 struct xfs_bmbt_irec
*PREV
)
6127 if (!xfs_bmap_is_update_needed(PREV
))
6130 __xfs_bmap_add(tp
, XFS_BMAP_UNMAP
, ip
, XFS_DATA_FORK
, PREV
);
6134 * Process one of the deferred bmap operations. We pass back the
6135 * btree cursor to maintain our lock on the bmapbt between calls.
6138 xfs_bmap_finish_one(
6139 struct xfs_trans
*tp
,
6140 struct xfs_bmap_intent
*bi
)
6142 struct xfs_bmbt_irec
*bmap
= &bi
->bi_bmap
;
6145 ASSERT(tp
->t_highest_agno
== NULLAGNUMBER
);
6147 trace_xfs_bmap_deferred(tp
->t_mountp
,
6148 XFS_FSB_TO_AGNO(tp
->t_mountp
, bmap
->br_startblock
),
6150 XFS_FSB_TO_AGBNO(tp
->t_mountp
, bmap
->br_startblock
),
6151 bi
->bi_owner
->i_ino
, bi
->bi_whichfork
,
6152 bmap
->br_startoff
, bmap
->br_blockcount
,
6155 if (WARN_ON_ONCE(bi
->bi_whichfork
!= XFS_DATA_FORK
))
6156 return -EFSCORRUPTED
;
6158 if (XFS_TEST_ERROR(false, tp
->t_mountp
,
6159 XFS_ERRTAG_BMAP_FINISH_ONE
))
6162 switch (bi
->bi_type
) {
6164 error
= xfs_bmapi_remap(tp
, bi
->bi_owner
, bmap
->br_startoff
,
6165 bmap
->br_blockcount
, bmap
->br_startblock
, 0);
6166 bmap
->br_blockcount
= 0;
6168 case XFS_BMAP_UNMAP
:
6169 error
= __xfs_bunmapi(tp
, bi
->bi_owner
, bmap
->br_startoff
,
6170 &bmap
->br_blockcount
, XFS_BMAPI_REMAP
, 1);
6174 error
= -EFSCORRUPTED
;
6180 /* Check that an inode's extent does not have invalid flags or bad ranges. */
6182 xfs_bmap_validate_extent(
6183 struct xfs_inode
*ip
,
6185 struct xfs_bmbt_irec
*irec
)
6187 struct xfs_mount
*mp
= ip
->i_mount
;
6189 if (!xfs_verify_fileext(mp
, irec
->br_startoff
, irec
->br_blockcount
))
6190 return __this_address
;
6192 if (XFS_IS_REALTIME_INODE(ip
) && whichfork
== XFS_DATA_FORK
) {
6193 if (!xfs_verify_rtbext(mp
, irec
->br_startblock
,
6194 irec
->br_blockcount
))
6195 return __this_address
;
6197 if (!xfs_verify_fsbext(mp
, irec
->br_startblock
,
6198 irec
->br_blockcount
))
6199 return __this_address
;
6201 if (irec
->br_state
!= XFS_EXT_NORM
&& whichfork
!= XFS_DATA_FORK
)
6202 return __this_address
;
6207 xfs_bmap_intent_init_cache(void)
6209 xfs_bmap_intent_cache
= kmem_cache_create("xfs_bmap_intent",
6210 sizeof(struct xfs_bmap_intent
),
6213 return xfs_bmap_intent_cache
!= NULL
? 0 : -ENOMEM
;
6217 xfs_bmap_intent_destroy_cache(void)
6219 kmem_cache_destroy(xfs_bmap_intent_cache
);
6220 xfs_bmap_intent_cache
= NULL
;