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"
29 #include "xfs_ag_resv.h"
30 #include "xfs_refcount.h"
33 kmem_zone_t
*xfs_bmap_free_item_zone
;
36 * Miscellaneous helper functions
40 * Compute and fill in the value of the maximum depth of a bmap btree
41 * in this filesystem. Done once, during mount.
44 xfs_bmap_compute_maxlevels(
45 xfs_mount_t
*mp
, /* file system mount structure */
46 int whichfork
) /* data or attr fork */
48 int level
; /* btree level */
49 uint maxblocks
; /* max blocks at this level */
50 uint maxleafents
; /* max leaf entries possible */
51 int maxrootrecs
; /* max records in root block */
52 int minleafrecs
; /* min records in leaf block */
53 int minnoderecs
; /* min records in node block */
54 int sz
; /* root block size */
57 * The maximum number of extents in a file, hence the maximum number of
58 * leaf entries, is controlled by the size of the on-disk extent count,
59 * either a signed 32-bit number for the data fork, or a signed 16-bit
60 * number for the attr fork.
62 * Note that we can no longer assume that if we are in ATTR1 that
63 * the fork offset of all the inodes will be
64 * (xfs_default_attroffset(ip) >> 3) because we could have mounted
65 * with ATTR2 and then mounted back with ATTR1, keeping the
66 * di_forkoff's fixed but probably at various positions. Therefore,
67 * for both ATTR1 and ATTR2 we have to assume the worst case scenario
68 * of a minimum size available.
70 if (whichfork
== XFS_DATA_FORK
) {
71 maxleafents
= MAXEXTNUM
;
72 sz
= XFS_BMDR_SPACE_CALC(MINDBTPTRS
);
74 maxleafents
= MAXAEXTNUM
;
75 sz
= XFS_BMDR_SPACE_CALC(MINABTPTRS
);
77 maxrootrecs
= xfs_bmdr_maxrecs(sz
, 0);
78 minleafrecs
= mp
->m_bmap_dmnr
[0];
79 minnoderecs
= mp
->m_bmap_dmnr
[1];
80 maxblocks
= (maxleafents
+ minleafrecs
- 1) / minleafrecs
;
81 for (level
= 1; maxblocks
> 1; level
++) {
82 if (maxblocks
<= maxrootrecs
)
85 maxblocks
= (maxblocks
+ minnoderecs
- 1) / minnoderecs
;
87 mp
->m_bm_maxlevels
[whichfork
] = level
;
90 STATIC
int /* error */
92 struct xfs_btree_cur
*cur
,
93 struct xfs_bmbt_irec
*irec
,
94 int *stat
) /* success/failure */
96 cur
->bc_rec
.b
= *irec
;
97 return xfs_btree_lookup(cur
, XFS_LOOKUP_EQ
, stat
);
100 STATIC
int /* error */
101 xfs_bmbt_lookup_first(
102 struct xfs_btree_cur
*cur
,
103 int *stat
) /* success/failure */
105 cur
->bc_rec
.b
.br_startoff
= 0;
106 cur
->bc_rec
.b
.br_startblock
= 0;
107 cur
->bc_rec
.b
.br_blockcount
= 0;
108 return xfs_btree_lookup(cur
, XFS_LOOKUP_GE
, stat
);
112 * Check if the inode needs to be converted to btree format.
114 static inline bool xfs_bmap_needs_btree(struct xfs_inode
*ip
, int whichfork
)
116 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
118 return whichfork
!= XFS_COW_FORK
&&
119 ifp
->if_format
== XFS_DINODE_FMT_EXTENTS
&&
120 ifp
->if_nextents
> XFS_IFORK_MAXEXT(ip
, whichfork
);
124 * Check if the inode should be converted to extent format.
126 static inline bool xfs_bmap_wants_extents(struct xfs_inode
*ip
, int whichfork
)
128 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
130 return whichfork
!= XFS_COW_FORK
&&
131 ifp
->if_format
== XFS_DINODE_FMT_BTREE
&&
132 ifp
->if_nextents
<= XFS_IFORK_MAXEXT(ip
, whichfork
);
136 * Update the record referred to by cur to the value given by irec
137 * This either works (return 0) or gets an EFSCORRUPTED error.
141 struct xfs_btree_cur
*cur
,
142 struct xfs_bmbt_irec
*irec
)
144 union xfs_btree_rec rec
;
146 xfs_bmbt_disk_set_all(&rec
.bmbt
, irec
);
147 return xfs_btree_update(cur
, &rec
);
151 * Compute the worst-case number of indirect blocks that will be used
152 * for ip's delayed extent of length "len".
155 xfs_bmap_worst_indlen(
156 xfs_inode_t
*ip
, /* incore inode pointer */
157 xfs_filblks_t len
) /* delayed extent length */
159 int level
; /* btree level number */
160 int maxrecs
; /* maximum record count at this level */
161 xfs_mount_t
*mp
; /* mount structure */
162 xfs_filblks_t rval
; /* return value */
165 maxrecs
= mp
->m_bmap_dmxr
[0];
166 for (level
= 0, rval
= 0;
167 level
< XFS_BM_MAXLEVELS(mp
, XFS_DATA_FORK
);
170 do_div(len
, maxrecs
);
173 return rval
+ XFS_BM_MAXLEVELS(mp
, XFS_DATA_FORK
) -
176 maxrecs
= mp
->m_bmap_dmxr
[1];
182 * Calculate the default attribute fork offset for newly created inodes.
185 xfs_default_attroffset(
186 struct xfs_inode
*ip
)
188 struct xfs_mount
*mp
= ip
->i_mount
;
191 if (mp
->m_sb
.sb_inodesize
== 256)
192 offset
= XFS_LITINO(mp
) - XFS_BMDR_SPACE_CALC(MINABTPTRS
);
194 offset
= XFS_BMDR_SPACE_CALC(6 * MINABTPTRS
);
196 ASSERT(offset
< XFS_LITINO(mp
));
201 * Helper routine to reset inode di_forkoff field when switching
202 * attribute fork from local to extent format - we reset it where
203 * possible to make space available for inline data fork extents.
206 xfs_bmap_forkoff_reset(
210 if (whichfork
== XFS_ATTR_FORK
&&
211 ip
->i_df
.if_format
!= XFS_DINODE_FMT_DEV
&&
212 ip
->i_df
.if_format
!= XFS_DINODE_FMT_BTREE
) {
213 uint dfl_forkoff
= xfs_default_attroffset(ip
) >> 3;
215 if (dfl_forkoff
> ip
->i_d
.di_forkoff
)
216 ip
->i_d
.di_forkoff
= dfl_forkoff
;
221 STATIC
struct xfs_buf
*
223 struct xfs_btree_cur
*cur
,
226 struct xfs_log_item
*lip
;
232 for (i
= 0; i
< XFS_BTREE_MAXLEVELS
; i
++) {
233 if (!cur
->bc_bufs
[i
])
235 if (XFS_BUF_ADDR(cur
->bc_bufs
[i
]) == bno
)
236 return cur
->bc_bufs
[i
];
239 /* Chase down all the log items to see if the bp is there */
240 list_for_each_entry(lip
, &cur
->bc_tp
->t_items
, li_trans
) {
241 struct xfs_buf_log_item
*bip
= (struct xfs_buf_log_item
*)lip
;
243 if (bip
->bli_item
.li_type
== XFS_LI_BUF
&&
244 XFS_BUF_ADDR(bip
->bli_buf
) == bno
)
253 struct xfs_btree_block
*block
,
259 __be64
*pp
, *thispa
; /* pointer to block address */
260 xfs_bmbt_key_t
*prevp
, *keyp
;
262 ASSERT(be16_to_cpu(block
->bb_level
) > 0);
265 for( i
= 1; i
<= xfs_btree_get_numrecs(block
); i
++) {
266 dmxr
= mp
->m_bmap_dmxr
[0];
267 keyp
= XFS_BMBT_KEY_ADDR(mp
, block
, i
);
270 ASSERT(be64_to_cpu(prevp
->br_startoff
) <
271 be64_to_cpu(keyp
->br_startoff
));
276 * Compare the block numbers to see if there are dups.
279 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, i
, sz
);
281 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, i
, dmxr
);
283 for (j
= i
+1; j
<= be16_to_cpu(block
->bb_numrecs
); j
++) {
285 thispa
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, j
, sz
);
287 thispa
= XFS_BMBT_PTR_ADDR(mp
, block
, j
, dmxr
);
288 if (*thispa
== *pp
) {
289 xfs_warn(mp
, "%s: thispa(%d) == pp(%d) %Ld",
291 (unsigned long long)be64_to_cpu(*thispa
));
292 xfs_err(mp
, "%s: ptrs are equal in node\n",
294 xfs_force_shutdown(mp
, SHUTDOWN_CORRUPT_INCORE
);
301 * Check that the extents for the inode ip are in the right order in all
302 * btree leaves. THis becomes prohibitively expensive for large extent count
303 * files, so don't bother with inodes that have more than 10,000 extents in
304 * them. The btree record ordering checks will still be done, so for such large
305 * bmapbt constructs that is going to catch most corruptions.
308 xfs_bmap_check_leaf_extents(
309 xfs_btree_cur_t
*cur
, /* btree cursor or null */
310 xfs_inode_t
*ip
, /* incore inode pointer */
311 int whichfork
) /* data or attr fork */
313 struct xfs_mount
*mp
= ip
->i_mount
;
314 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
315 struct xfs_btree_block
*block
; /* current btree block */
316 xfs_fsblock_t bno
; /* block # of "block" */
317 struct xfs_buf
*bp
; /* buffer for "block" */
318 int error
; /* error return value */
319 xfs_extnum_t i
=0, j
; /* index into the extents list */
320 int level
; /* btree level, for checking */
321 __be64
*pp
; /* pointer to block address */
322 xfs_bmbt_rec_t
*ep
; /* pointer to current extent */
323 xfs_bmbt_rec_t last
= {0, 0}; /* last extent in prev block */
324 xfs_bmbt_rec_t
*nextp
; /* pointer to next extent */
327 if (ifp
->if_format
!= XFS_DINODE_FMT_BTREE
)
330 /* skip large extent count inodes */
331 if (ip
->i_df
.if_nextents
> 10000)
335 block
= ifp
->if_broot
;
337 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
339 level
= be16_to_cpu(block
->bb_level
);
341 xfs_check_block(block
, mp
, 1, ifp
->if_broot_bytes
);
342 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, 1, ifp
->if_broot_bytes
);
343 bno
= be64_to_cpu(*pp
);
345 ASSERT(bno
!= NULLFSBLOCK
);
346 ASSERT(XFS_FSB_TO_AGNO(mp
, bno
) < mp
->m_sb
.sb_agcount
);
347 ASSERT(XFS_FSB_TO_AGBNO(mp
, bno
) < mp
->m_sb
.sb_agblocks
);
350 * Go down the tree until leaf level is reached, following the first
351 * pointer (leftmost) at each level.
353 while (level
-- > 0) {
354 /* See if buf is in cur first */
356 bp
= xfs_bmap_get_bp(cur
, XFS_FSB_TO_DADDR(mp
, bno
));
359 error
= xfs_btree_read_bufl(mp
, NULL
, bno
, &bp
,
365 block
= XFS_BUF_TO_BLOCK(bp
);
370 * Check this block for basic sanity (increasing keys and
371 * no duplicate blocks).
374 xfs_check_block(block
, mp
, 0, 0);
375 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, mp
->m_bmap_dmxr
[1]);
376 bno
= be64_to_cpu(*pp
);
377 if (XFS_IS_CORRUPT(mp
, !xfs_verify_fsbno(mp
, bno
))) {
378 error
= -EFSCORRUPTED
;
383 xfs_trans_brelse(NULL
, bp
);
388 * Here with bp and block set to the leftmost leaf node in the tree.
393 * Loop over all leaf nodes checking that all extents are in the right order.
396 xfs_fsblock_t nextbno
;
397 xfs_extnum_t num_recs
;
400 num_recs
= xfs_btree_get_numrecs(block
);
403 * Read-ahead the next leaf block, if any.
406 nextbno
= be64_to_cpu(block
->bb_u
.l
.bb_rightsib
);
409 * Check all the extents to make sure they are OK.
410 * If we had a previous block, the last entry should
411 * conform with the first entry in this one.
414 ep
= XFS_BMBT_REC_ADDR(mp
, block
, 1);
416 ASSERT(xfs_bmbt_disk_get_startoff(&last
) +
417 xfs_bmbt_disk_get_blockcount(&last
) <=
418 xfs_bmbt_disk_get_startoff(ep
));
420 for (j
= 1; j
< num_recs
; j
++) {
421 nextp
= XFS_BMBT_REC_ADDR(mp
, block
, j
+ 1);
422 ASSERT(xfs_bmbt_disk_get_startoff(ep
) +
423 xfs_bmbt_disk_get_blockcount(ep
) <=
424 xfs_bmbt_disk_get_startoff(nextp
));
432 xfs_trans_brelse(NULL
, bp
);
436 * If we've reached the end, stop.
438 if (bno
== NULLFSBLOCK
)
442 bp
= xfs_bmap_get_bp(cur
, XFS_FSB_TO_DADDR(mp
, bno
));
445 error
= xfs_btree_read_bufl(mp
, NULL
, bno
, &bp
,
451 block
= XFS_BUF_TO_BLOCK(bp
);
457 xfs_warn(mp
, "%s: at error0", __func__
);
459 xfs_trans_brelse(NULL
, bp
);
461 xfs_warn(mp
, "%s: BAD after btree leaves for %d extents",
463 xfs_err(mp
, "%s: CORRUPTED BTREE OR SOMETHING", __func__
);
464 xfs_force_shutdown(mp
, SHUTDOWN_CORRUPT_INCORE
);
469 * Validate that the bmbt_irecs being returned from bmapi are valid
470 * given the caller's original parameters. Specifically check the
471 * ranges of the returned irecs to ensure that they only extend beyond
472 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
475 xfs_bmap_validate_ret(
479 xfs_bmbt_irec_t
*mval
,
483 int i
; /* index to map values */
485 ASSERT(ret_nmap
<= nmap
);
487 for (i
= 0; i
< ret_nmap
; i
++) {
488 ASSERT(mval
[i
].br_blockcount
> 0);
489 if (!(flags
& XFS_BMAPI_ENTIRE
)) {
490 ASSERT(mval
[i
].br_startoff
>= bno
);
491 ASSERT(mval
[i
].br_blockcount
<= len
);
492 ASSERT(mval
[i
].br_startoff
+ mval
[i
].br_blockcount
<=
495 ASSERT(mval
[i
].br_startoff
< bno
+ len
);
496 ASSERT(mval
[i
].br_startoff
+ mval
[i
].br_blockcount
>
500 mval
[i
- 1].br_startoff
+ mval
[i
- 1].br_blockcount
==
501 mval
[i
].br_startoff
);
502 ASSERT(mval
[i
].br_startblock
!= DELAYSTARTBLOCK
&&
503 mval
[i
].br_startblock
!= HOLESTARTBLOCK
);
504 ASSERT(mval
[i
].br_state
== XFS_EXT_NORM
||
505 mval
[i
].br_state
== XFS_EXT_UNWRITTEN
);
510 #define xfs_bmap_check_leaf_extents(cur, ip, whichfork) do { } while (0)
511 #define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap) do { } while (0)
515 * bmap free list manipulation functions
519 * Add the extent to the list of extents to be free at transaction end.
520 * The list is maintained sorted (by block number).
524 struct xfs_trans
*tp
,
527 const struct xfs_owner_info
*oinfo
,
530 struct xfs_extent_free_item
*new; /* new element */
532 struct xfs_mount
*mp
= tp
->t_mountp
;
536 ASSERT(bno
!= NULLFSBLOCK
);
538 ASSERT(len
<= MAXEXTLEN
);
539 ASSERT(!isnullstartblock(bno
));
540 agno
= XFS_FSB_TO_AGNO(mp
, bno
);
541 agbno
= XFS_FSB_TO_AGBNO(mp
, bno
);
542 ASSERT(agno
< mp
->m_sb
.sb_agcount
);
543 ASSERT(agbno
< mp
->m_sb
.sb_agblocks
);
544 ASSERT(len
< mp
->m_sb
.sb_agblocks
);
545 ASSERT(agbno
+ len
<= mp
->m_sb
.sb_agblocks
);
547 ASSERT(xfs_bmap_free_item_zone
!= NULL
);
549 new = kmem_cache_alloc(xfs_bmap_free_item_zone
,
550 GFP_KERNEL
| __GFP_NOFAIL
);
551 new->xefi_startblock
= bno
;
552 new->xefi_blockcount
= (xfs_extlen_t
)len
;
554 new->xefi_oinfo
= *oinfo
;
556 new->xefi_oinfo
= XFS_RMAP_OINFO_SKIP_UPDATE
;
557 new->xefi_skip_discard
= skip_discard
;
558 trace_xfs_bmap_free_defer(tp
->t_mountp
,
559 XFS_FSB_TO_AGNO(tp
->t_mountp
, bno
), 0,
560 XFS_FSB_TO_AGBNO(tp
->t_mountp
, bno
), len
);
561 xfs_defer_add(tp
, XFS_DEFER_OPS_TYPE_FREE
, &new->xefi_list
);
565 * Inode fork format manipulation functions
569 * Convert the inode format to extent format if it currently is in btree format,
570 * but the extent list is small enough that it fits into the extent format.
572 * Since the extents are already in-core, all we have to do is give up the space
573 * for the btree root and pitch the leaf block.
575 STATIC
int /* error */
576 xfs_bmap_btree_to_extents(
577 struct xfs_trans
*tp
, /* transaction pointer */
578 struct xfs_inode
*ip
, /* incore inode pointer */
579 struct xfs_btree_cur
*cur
, /* btree cursor */
580 int *logflagsp
, /* inode logging flags */
581 int whichfork
) /* data or attr fork */
583 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
584 struct xfs_mount
*mp
= ip
->i_mount
;
585 struct xfs_btree_block
*rblock
= ifp
->if_broot
;
586 struct xfs_btree_block
*cblock
;/* child btree block */
587 xfs_fsblock_t cbno
; /* child block number */
588 struct xfs_buf
*cbp
; /* child block's buffer */
589 int error
; /* error return value */
590 __be64
*pp
; /* ptr to block address */
591 struct xfs_owner_info oinfo
;
593 /* check if we actually need the extent format first: */
594 if (!xfs_bmap_wants_extents(ip
, whichfork
))
598 ASSERT(whichfork
!= XFS_COW_FORK
);
599 ASSERT(ifp
->if_flags
& XFS_IFEXTENTS
);
600 ASSERT(ifp
->if_format
== XFS_DINODE_FMT_BTREE
);
601 ASSERT(be16_to_cpu(rblock
->bb_level
) == 1);
602 ASSERT(be16_to_cpu(rblock
->bb_numrecs
) == 1);
603 ASSERT(xfs_bmbt_maxrecs(mp
, ifp
->if_broot_bytes
, 0) == 1);
605 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, rblock
, 1, ifp
->if_broot_bytes
);
606 cbno
= be64_to_cpu(*pp
);
608 if (XFS_IS_CORRUPT(cur
->bc_mp
, !xfs_btree_check_lptr(cur
, cbno
, 1)))
609 return -EFSCORRUPTED
;
611 error
= xfs_btree_read_bufl(mp
, tp
, cbno
, &cbp
, XFS_BMAP_BTREE_REF
,
615 cblock
= XFS_BUF_TO_BLOCK(cbp
);
616 if ((error
= xfs_btree_check_block(cur
, cblock
, 0, cbp
)))
618 xfs_rmap_ino_bmbt_owner(&oinfo
, ip
->i_ino
, whichfork
);
619 xfs_bmap_add_free(cur
->bc_tp
, cbno
, 1, &oinfo
);
620 ip
->i_d
.di_nblocks
--;
621 xfs_trans_mod_dquot_byino(tp
, ip
, XFS_TRANS_DQ_BCOUNT
, -1L);
622 xfs_trans_binval(tp
, cbp
);
623 if (cur
->bc_bufs
[0] == cbp
)
624 cur
->bc_bufs
[0] = NULL
;
625 xfs_iroot_realloc(ip
, -1, whichfork
);
626 ASSERT(ifp
->if_broot
== NULL
);
627 ASSERT((ifp
->if_flags
& XFS_IFBROOT
) == 0);
628 ifp
->if_format
= XFS_DINODE_FMT_EXTENTS
;
629 *logflagsp
|= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
634 * Convert an extents-format file into a btree-format file.
635 * The new file will have a root block (in the inode) and a single child block.
637 STATIC
int /* error */
638 xfs_bmap_extents_to_btree(
639 struct xfs_trans
*tp
, /* transaction pointer */
640 struct xfs_inode
*ip
, /* incore inode pointer */
641 struct xfs_btree_cur
**curp
, /* cursor returned to caller */
642 int wasdel
, /* converting a delayed alloc */
643 int *logflagsp
, /* inode logging flags */
644 int whichfork
) /* data or attr fork */
646 struct xfs_btree_block
*ablock
; /* allocated (child) bt block */
647 struct xfs_buf
*abp
; /* buffer for ablock */
648 struct xfs_alloc_arg args
; /* allocation arguments */
649 struct xfs_bmbt_rec
*arp
; /* child record pointer */
650 struct xfs_btree_block
*block
; /* btree root block */
651 struct xfs_btree_cur
*cur
; /* bmap btree cursor */
652 int error
; /* error return value */
653 struct xfs_ifork
*ifp
; /* inode fork pointer */
654 struct xfs_bmbt_key
*kp
; /* root block key pointer */
655 struct xfs_mount
*mp
; /* mount structure */
656 xfs_bmbt_ptr_t
*pp
; /* root block address pointer */
657 struct xfs_iext_cursor icur
;
658 struct xfs_bmbt_irec rec
;
659 xfs_extnum_t cnt
= 0;
662 ASSERT(whichfork
!= XFS_COW_FORK
);
663 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
664 ASSERT(ifp
->if_format
== XFS_DINODE_FMT_EXTENTS
);
667 * Make space in the inode incore. This needs to be undone if we fail
668 * to expand the root.
670 xfs_iroot_realloc(ip
, 1, whichfork
);
671 ifp
->if_flags
|= XFS_IFBROOT
;
676 block
= ifp
->if_broot
;
677 xfs_btree_init_block_int(mp
, block
, XFS_BUF_DADDR_NULL
,
678 XFS_BTNUM_BMAP
, 1, 1, ip
->i_ino
,
679 XFS_BTREE_LONG_PTRS
);
681 * Need a cursor. Can't allocate until bb_level is filled in.
683 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
684 cur
->bc_ino
.flags
= wasdel
? XFS_BTCUR_BMBT_WASDEL
: 0;
686 * Convert to a btree with two levels, one record in root.
688 ifp
->if_format
= XFS_DINODE_FMT_BTREE
;
689 memset(&args
, 0, sizeof(args
));
692 xfs_rmap_ino_bmbt_owner(&args
.oinfo
, ip
->i_ino
, whichfork
);
693 if (tp
->t_firstblock
== NULLFSBLOCK
) {
694 args
.type
= XFS_ALLOCTYPE_START_BNO
;
695 args
.fsbno
= XFS_INO_TO_FSB(mp
, ip
->i_ino
);
696 } else if (tp
->t_flags
& XFS_TRANS_LOWMODE
) {
697 args
.type
= XFS_ALLOCTYPE_START_BNO
;
698 args
.fsbno
= tp
->t_firstblock
;
700 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
701 args
.fsbno
= tp
->t_firstblock
;
703 args
.minlen
= args
.maxlen
= args
.prod
= 1;
704 args
.wasdel
= wasdel
;
706 error
= xfs_alloc_vextent(&args
);
708 goto out_root_realloc
;
710 if (WARN_ON_ONCE(args
.fsbno
== NULLFSBLOCK
)) {
712 goto out_root_realloc
;
716 * Allocation can't fail, the space was reserved.
718 ASSERT(tp
->t_firstblock
== NULLFSBLOCK
||
719 args
.agno
>= XFS_FSB_TO_AGNO(mp
, tp
->t_firstblock
));
720 tp
->t_firstblock
= args
.fsbno
;
721 cur
->bc_ino
.allocated
++;
722 ip
->i_d
.di_nblocks
++;
723 xfs_trans_mod_dquot_byino(tp
, ip
, XFS_TRANS_DQ_BCOUNT
, 1L);
724 error
= xfs_trans_get_buf(tp
, mp
->m_ddev_targp
,
725 XFS_FSB_TO_DADDR(mp
, args
.fsbno
),
726 mp
->m_bsize
, 0, &abp
);
728 goto out_unreserve_dquot
;
731 * Fill in the child block.
733 abp
->b_ops
= &xfs_bmbt_buf_ops
;
734 ablock
= XFS_BUF_TO_BLOCK(abp
);
735 xfs_btree_init_block_int(mp
, ablock
, abp
->b_bn
,
736 XFS_BTNUM_BMAP
, 0, 0, ip
->i_ino
,
737 XFS_BTREE_LONG_PTRS
);
739 for_each_xfs_iext(ifp
, &icur
, &rec
) {
740 if (isnullstartblock(rec
.br_startblock
))
742 arp
= XFS_BMBT_REC_ADDR(mp
, ablock
, 1 + cnt
);
743 xfs_bmbt_disk_set_all(arp
, &rec
);
746 ASSERT(cnt
== ifp
->if_nextents
);
747 xfs_btree_set_numrecs(ablock
, cnt
);
750 * Fill in the root key and pointer.
752 kp
= XFS_BMBT_KEY_ADDR(mp
, block
, 1);
753 arp
= XFS_BMBT_REC_ADDR(mp
, ablock
, 1);
754 kp
->br_startoff
= cpu_to_be64(xfs_bmbt_disk_get_startoff(arp
));
755 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, xfs_bmbt_get_maxrecs(cur
,
756 be16_to_cpu(block
->bb_level
)));
757 *pp
= cpu_to_be64(args
.fsbno
);
760 * Do all this logging at the end so that
761 * the root is at the right level.
763 xfs_btree_log_block(cur
, abp
, XFS_BB_ALL_BITS
);
764 xfs_btree_log_recs(cur
, abp
, 1, be16_to_cpu(ablock
->bb_numrecs
));
765 ASSERT(*curp
== NULL
);
767 *logflagsp
= XFS_ILOG_CORE
| xfs_ilog_fbroot(whichfork
);
771 xfs_trans_mod_dquot_byino(tp
, ip
, XFS_TRANS_DQ_BCOUNT
, -1L);
773 xfs_iroot_realloc(ip
, -1, whichfork
);
774 ifp
->if_format
= XFS_DINODE_FMT_EXTENTS
;
775 ASSERT(ifp
->if_broot
== NULL
);
776 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
782 * Convert a local file to an extents file.
783 * This code is out of bounds for data forks of regular files,
784 * since the file data needs to get logged so things will stay consistent.
785 * (The bmap-level manipulations are ok, though).
788 xfs_bmap_local_to_extents_empty(
789 struct xfs_trans
*tp
,
790 struct xfs_inode
*ip
,
793 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
795 ASSERT(whichfork
!= XFS_COW_FORK
);
796 ASSERT(ifp
->if_format
== XFS_DINODE_FMT_LOCAL
);
797 ASSERT(ifp
->if_bytes
== 0);
798 ASSERT(ifp
->if_nextents
== 0);
800 xfs_bmap_forkoff_reset(ip
, whichfork
);
801 ifp
->if_flags
&= ~XFS_IFINLINE
;
802 ifp
->if_flags
|= XFS_IFEXTENTS
;
803 ifp
->if_u1
.if_root
= NULL
;
805 ifp
->if_format
= XFS_DINODE_FMT_EXTENTS
;
806 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
810 STATIC
int /* error */
811 xfs_bmap_local_to_extents(
812 xfs_trans_t
*tp
, /* transaction pointer */
813 xfs_inode_t
*ip
, /* incore inode pointer */
814 xfs_extlen_t total
, /* total blocks needed by transaction */
815 int *logflagsp
, /* inode logging flags */
817 void (*init_fn
)(struct xfs_trans
*tp
,
819 struct xfs_inode
*ip
,
820 struct xfs_ifork
*ifp
))
823 int flags
; /* logging flags returned */
824 struct xfs_ifork
*ifp
; /* inode fork pointer */
825 xfs_alloc_arg_t args
; /* allocation arguments */
826 struct xfs_buf
*bp
; /* buffer for extent block */
827 struct xfs_bmbt_irec rec
;
828 struct xfs_iext_cursor icur
;
831 * We don't want to deal with the case of keeping inode data inline yet.
832 * So sending the data fork of a regular inode is invalid.
834 ASSERT(!(S_ISREG(VFS_I(ip
)->i_mode
) && whichfork
== XFS_DATA_FORK
));
835 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
836 ASSERT(ifp
->if_format
== XFS_DINODE_FMT_LOCAL
);
838 if (!ifp
->if_bytes
) {
839 xfs_bmap_local_to_extents_empty(tp
, ip
, whichfork
);
840 flags
= XFS_ILOG_CORE
;
846 ASSERT((ifp
->if_flags
& (XFS_IFINLINE
|XFS_IFEXTENTS
)) == XFS_IFINLINE
);
847 memset(&args
, 0, sizeof(args
));
849 args
.mp
= ip
->i_mount
;
850 xfs_rmap_ino_owner(&args
.oinfo
, ip
->i_ino
, whichfork
, 0);
852 * Allocate a block. We know we need only one, since the
853 * file currently fits in an inode.
855 if (tp
->t_firstblock
== NULLFSBLOCK
) {
856 args
.fsbno
= XFS_INO_TO_FSB(args
.mp
, ip
->i_ino
);
857 args
.type
= XFS_ALLOCTYPE_START_BNO
;
859 args
.fsbno
= tp
->t_firstblock
;
860 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
863 args
.minlen
= args
.maxlen
= args
.prod
= 1;
864 error
= xfs_alloc_vextent(&args
);
868 /* Can't fail, the space was reserved. */
869 ASSERT(args
.fsbno
!= NULLFSBLOCK
);
870 ASSERT(args
.len
== 1);
871 tp
->t_firstblock
= args
.fsbno
;
872 error
= xfs_trans_get_buf(tp
, args
.mp
->m_ddev_targp
,
873 XFS_FSB_TO_DADDR(args
.mp
, args
.fsbno
),
874 args
.mp
->m_bsize
, 0, &bp
);
879 * Initialize the block, copy the data and log the remote buffer.
881 * The callout is responsible for logging because the remote format
882 * might differ from the local format and thus we don't know how much to
883 * log here. Note that init_fn must also set the buffer log item type
886 init_fn(tp
, bp
, ip
, ifp
);
888 /* account for the change in fork size */
889 xfs_idata_realloc(ip
, -ifp
->if_bytes
, whichfork
);
890 xfs_bmap_local_to_extents_empty(tp
, ip
, whichfork
);
891 flags
|= XFS_ILOG_CORE
;
893 ifp
->if_u1
.if_root
= NULL
;
897 rec
.br_startblock
= args
.fsbno
;
898 rec
.br_blockcount
= 1;
899 rec
.br_state
= XFS_EXT_NORM
;
900 xfs_iext_first(ifp
, &icur
);
901 xfs_iext_insert(ip
, &icur
, &rec
, 0);
903 ifp
->if_nextents
= 1;
904 ip
->i_d
.di_nblocks
= 1;
905 xfs_trans_mod_dquot_byino(tp
, ip
,
906 XFS_TRANS_DQ_BCOUNT
, 1L);
907 flags
|= xfs_ilog_fext(whichfork
);
915 * Called from xfs_bmap_add_attrfork to handle btree format files.
917 STATIC
int /* error */
918 xfs_bmap_add_attrfork_btree(
919 xfs_trans_t
*tp
, /* transaction pointer */
920 xfs_inode_t
*ip
, /* incore inode pointer */
921 int *flags
) /* inode logging flags */
923 xfs_btree_cur_t
*cur
; /* btree cursor */
924 int error
; /* error return value */
925 xfs_mount_t
*mp
; /* file system mount struct */
926 int stat
; /* newroot status */
929 if (ip
->i_df
.if_broot_bytes
<= XFS_IFORK_DSIZE(ip
))
930 *flags
|= XFS_ILOG_DBROOT
;
932 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, XFS_DATA_FORK
);
933 error
= xfs_bmbt_lookup_first(cur
, &stat
);
936 /* must be at least one entry */
937 if (XFS_IS_CORRUPT(mp
, stat
!= 1)) {
938 error
= -EFSCORRUPTED
;
941 if ((error
= xfs_btree_new_iroot(cur
, flags
, &stat
)))
944 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
947 cur
->bc_ino
.allocated
= 0;
948 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
952 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
957 * Called from xfs_bmap_add_attrfork to handle extents format files.
959 STATIC
int /* error */
960 xfs_bmap_add_attrfork_extents(
961 struct xfs_trans
*tp
, /* transaction pointer */
962 struct xfs_inode
*ip
, /* incore inode pointer */
963 int *flags
) /* inode logging flags */
965 xfs_btree_cur_t
*cur
; /* bmap btree cursor */
966 int error
; /* error return value */
968 if (ip
->i_df
.if_nextents
* sizeof(struct xfs_bmbt_rec
) <=
972 error
= xfs_bmap_extents_to_btree(tp
, ip
, &cur
, 0, flags
,
975 cur
->bc_ino
.allocated
= 0;
976 xfs_btree_del_cursor(cur
, error
);
982 * Called from xfs_bmap_add_attrfork to handle local format files. Each
983 * different data fork content type needs a different callout to do the
984 * conversion. Some are basic and only require special block initialisation
985 * callouts for the data formating, others (directories) are so specialised they
986 * handle everything themselves.
988 * XXX (dgc): investigate whether directory conversion can use the generic
989 * formatting callout. It should be possible - it's just a very complex
992 STATIC
int /* error */
993 xfs_bmap_add_attrfork_local(
994 struct xfs_trans
*tp
, /* transaction pointer */
995 struct xfs_inode
*ip
, /* incore inode pointer */
996 int *flags
) /* inode logging flags */
998 struct xfs_da_args dargs
; /* args for dir/attr code */
1000 if (ip
->i_df
.if_bytes
<= XFS_IFORK_DSIZE(ip
))
1003 if (S_ISDIR(VFS_I(ip
)->i_mode
)) {
1004 memset(&dargs
, 0, sizeof(dargs
));
1005 dargs
.geo
= ip
->i_mount
->m_dir_geo
;
1007 dargs
.total
= dargs
.geo
->fsbcount
;
1008 dargs
.whichfork
= XFS_DATA_FORK
;
1010 return xfs_dir2_sf_to_block(&dargs
);
1013 if (S_ISLNK(VFS_I(ip
)->i_mode
))
1014 return xfs_bmap_local_to_extents(tp
, ip
, 1, flags
,
1016 xfs_symlink_local_to_remote
);
1018 /* should only be called for types that support local format data */
1020 return -EFSCORRUPTED
;
1023 /* Set an inode attr fork off based on the format */
1025 xfs_bmap_set_attrforkoff(
1026 struct xfs_inode
*ip
,
1030 switch (ip
->i_df
.if_format
) {
1031 case XFS_DINODE_FMT_DEV
:
1032 ip
->i_d
.di_forkoff
= roundup(sizeof(xfs_dev_t
), 8) >> 3;
1034 case XFS_DINODE_FMT_LOCAL
:
1035 case XFS_DINODE_FMT_EXTENTS
:
1036 case XFS_DINODE_FMT_BTREE
:
1037 ip
->i_d
.di_forkoff
= xfs_attr_shortform_bytesfit(ip
, size
);
1038 if (!ip
->i_d
.di_forkoff
)
1039 ip
->i_d
.di_forkoff
= xfs_default_attroffset(ip
) >> 3;
1040 else if ((ip
->i_mount
->m_flags
& XFS_MOUNT_ATTR2
) && version
)
1052 * Convert inode from non-attributed to attributed.
1053 * Must not be in a transaction, ip must not be locked.
1055 int /* error code */
1056 xfs_bmap_add_attrfork(
1057 xfs_inode_t
*ip
, /* incore inode pointer */
1058 int size
, /* space new attribute needs */
1059 int rsvd
) /* xact may use reserved blks */
1061 xfs_mount_t
*mp
; /* mount structure */
1062 xfs_trans_t
*tp
; /* transaction pointer */
1063 int blks
; /* space reservation */
1064 int version
= 1; /* superblock attr version */
1065 int logflags
; /* logging flags */
1066 int error
; /* error return value */
1068 ASSERT(XFS_IFORK_Q(ip
) == 0);
1071 ASSERT(!XFS_NOT_DQATTACHED(mp
, ip
));
1073 blks
= XFS_ADDAFORK_SPACE_RES(mp
);
1075 error
= xfs_trans_alloc_inode(ip
, &M_RES(mp
)->tr_addafork
, blks
,
1079 if (XFS_IFORK_Q(ip
))
1082 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
1083 error
= xfs_bmap_set_attrforkoff(ip
, size
, &version
);
1086 ASSERT(ip
->i_afp
== NULL
);
1088 ip
->i_afp
= kmem_cache_zalloc(xfs_ifork_zone
,
1089 GFP_KERNEL
| __GFP_NOFAIL
);
1091 ip
->i_afp
->if_format
= XFS_DINODE_FMT_EXTENTS
;
1092 ip
->i_afp
->if_flags
= XFS_IFEXTENTS
;
1094 switch (ip
->i_df
.if_format
) {
1095 case XFS_DINODE_FMT_LOCAL
:
1096 error
= xfs_bmap_add_attrfork_local(tp
, ip
, &logflags
);
1098 case XFS_DINODE_FMT_EXTENTS
:
1099 error
= xfs_bmap_add_attrfork_extents(tp
, ip
, &logflags
);
1101 case XFS_DINODE_FMT_BTREE
:
1102 error
= xfs_bmap_add_attrfork_btree(tp
, ip
, &logflags
);
1109 xfs_trans_log_inode(tp
, ip
, logflags
);
1112 if (!xfs_sb_version_hasattr(&mp
->m_sb
) ||
1113 (!xfs_sb_version_hasattr2(&mp
->m_sb
) && version
== 2)) {
1114 bool log_sb
= false;
1116 spin_lock(&mp
->m_sb_lock
);
1117 if (!xfs_sb_version_hasattr(&mp
->m_sb
)) {
1118 xfs_sb_version_addattr(&mp
->m_sb
);
1121 if (!xfs_sb_version_hasattr2(&mp
->m_sb
) && version
== 2) {
1122 xfs_sb_version_addattr2(&mp
->m_sb
);
1125 spin_unlock(&mp
->m_sb_lock
);
1130 error
= xfs_trans_commit(tp
);
1131 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
1135 xfs_trans_cancel(tp
);
1136 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
1141 * Internal and external extent tree search functions.
1144 struct xfs_iread_state
{
1145 struct xfs_iext_cursor icur
;
1146 xfs_extnum_t loaded
;
1149 /* Stuff every bmbt record from this block into the incore extent map. */
1151 xfs_iread_bmbt_block(
1152 struct xfs_btree_cur
*cur
,
1156 struct xfs_iread_state
*ir
= priv
;
1157 struct xfs_mount
*mp
= cur
->bc_mp
;
1158 struct xfs_inode
*ip
= cur
->bc_ino
.ip
;
1159 struct xfs_btree_block
*block
;
1161 struct xfs_bmbt_rec
*frp
;
1162 xfs_extnum_t num_recs
;
1164 int whichfork
= cur
->bc_ino
.whichfork
;
1165 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1167 block
= xfs_btree_get_block(cur
, level
, &bp
);
1169 /* Abort if we find more records than nextents. */
1170 num_recs
= xfs_btree_get_numrecs(block
);
1171 if (unlikely(ir
->loaded
+ num_recs
> ifp
->if_nextents
)) {
1172 xfs_warn(ip
->i_mount
, "corrupt dinode %llu, (btree extents).",
1173 (unsigned long long)ip
->i_ino
);
1174 xfs_inode_verifier_error(ip
, -EFSCORRUPTED
, __func__
, block
,
1175 sizeof(*block
), __this_address
);
1176 return -EFSCORRUPTED
;
1179 /* Copy records into the incore cache. */
1180 frp
= XFS_BMBT_REC_ADDR(mp
, block
, 1);
1181 for (j
= 0; j
< num_recs
; j
++, frp
++, ir
->loaded
++) {
1182 struct xfs_bmbt_irec
new;
1185 xfs_bmbt_disk_get_all(frp
, &new);
1186 fa
= xfs_bmap_validate_extent(ip
, whichfork
, &new);
1188 xfs_inode_verifier_error(ip
, -EFSCORRUPTED
,
1189 "xfs_iread_extents(2)", frp
,
1191 return -EFSCORRUPTED
;
1193 xfs_iext_insert(ip
, &ir
->icur
, &new,
1194 xfs_bmap_fork_to_state(whichfork
));
1195 trace_xfs_read_extent(ip
, &ir
->icur
,
1196 xfs_bmap_fork_to_state(whichfork
), _THIS_IP_
);
1197 xfs_iext_next(ifp
, &ir
->icur
);
1204 * Read in extents from a btree-format inode.
1208 struct xfs_trans
*tp
,
1209 struct xfs_inode
*ip
,
1212 struct xfs_iread_state ir
;
1213 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1214 struct xfs_mount
*mp
= ip
->i_mount
;
1215 struct xfs_btree_cur
*cur
;
1218 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
1220 if (XFS_IS_CORRUPT(mp
, ifp
->if_format
!= XFS_DINODE_FMT_BTREE
)) {
1221 error
= -EFSCORRUPTED
;
1226 xfs_iext_first(ifp
, &ir
.icur
);
1227 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
1228 error
= xfs_btree_visit_blocks(cur
, xfs_iread_bmbt_block
,
1229 XFS_BTREE_VISIT_RECORDS
, &ir
);
1230 xfs_btree_del_cursor(cur
, error
);
1234 if (XFS_IS_CORRUPT(mp
, ir
.loaded
!= ifp
->if_nextents
)) {
1235 error
= -EFSCORRUPTED
;
1238 ASSERT(ir
.loaded
== xfs_iext_count(ifp
));
1240 ifp
->if_flags
|= XFS_IFEXTENTS
;
1243 xfs_iext_destroy(ifp
);
1248 * Returns the relative block number of the first unused block(s) in the given
1249 * fork with at least "len" logically contiguous blocks free. This is the
1250 * lowest-address hole if the fork has holes, else the first block past the end
1251 * of fork. Return 0 if the fork is currently local (in-inode).
1254 xfs_bmap_first_unused(
1255 struct xfs_trans
*tp
, /* transaction pointer */
1256 struct xfs_inode
*ip
, /* incore inode */
1257 xfs_extlen_t len
, /* size of hole to find */
1258 xfs_fileoff_t
*first_unused
, /* unused block */
1259 int whichfork
) /* data or attr fork */
1261 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1262 struct xfs_bmbt_irec got
;
1263 struct xfs_iext_cursor icur
;
1264 xfs_fileoff_t lastaddr
= 0;
1265 xfs_fileoff_t lowest
, max
;
1268 if (ifp
->if_format
== XFS_DINODE_FMT_LOCAL
) {
1273 ASSERT(xfs_ifork_has_extents(ifp
));
1275 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
1276 error
= xfs_iread_extents(tp
, ip
, whichfork
);
1281 lowest
= max
= *first_unused
;
1282 for_each_xfs_iext(ifp
, &icur
, &got
) {
1284 * See if the hole before this extent will work.
1286 if (got
.br_startoff
>= lowest
+ len
&&
1287 got
.br_startoff
- max
>= len
)
1289 lastaddr
= got
.br_startoff
+ got
.br_blockcount
;
1290 max
= XFS_FILEOFF_MAX(lastaddr
, lowest
);
1293 *first_unused
= max
;
1298 * Returns the file-relative block number of the last block - 1 before
1299 * last_block (input value) in the file.
1300 * This is not based on i_size, it is based on the extent records.
1301 * Returns 0 for local files, as they do not have extent records.
1304 xfs_bmap_last_before(
1305 struct xfs_trans
*tp
, /* transaction pointer */
1306 struct xfs_inode
*ip
, /* incore inode */
1307 xfs_fileoff_t
*last_block
, /* last block */
1308 int whichfork
) /* data or attr fork */
1310 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1311 struct xfs_bmbt_irec got
;
1312 struct xfs_iext_cursor icur
;
1315 switch (ifp
->if_format
) {
1316 case XFS_DINODE_FMT_LOCAL
:
1319 case XFS_DINODE_FMT_BTREE
:
1320 case XFS_DINODE_FMT_EXTENTS
:
1324 return -EFSCORRUPTED
;
1327 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
1328 error
= xfs_iread_extents(tp
, ip
, whichfork
);
1333 if (!xfs_iext_lookup_extent_before(ip
, ifp
, last_block
, &icur
, &got
))
1339 xfs_bmap_last_extent(
1340 struct xfs_trans
*tp
,
1341 struct xfs_inode
*ip
,
1343 struct xfs_bmbt_irec
*rec
,
1346 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1347 struct xfs_iext_cursor icur
;
1350 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
1351 error
= xfs_iread_extents(tp
, ip
, whichfork
);
1356 xfs_iext_last(ifp
, &icur
);
1357 if (!xfs_iext_get_extent(ifp
, &icur
, rec
))
1365 * Check the last inode extent to determine whether this allocation will result
1366 * in blocks being allocated at the end of the file. When we allocate new data
1367 * blocks at the end of the file which do not start at the previous data block,
1368 * we will try to align the new blocks at stripe unit boundaries.
1370 * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be
1371 * at, or past the EOF.
1375 struct xfs_bmalloca
*bma
,
1378 struct xfs_bmbt_irec rec
;
1383 error
= xfs_bmap_last_extent(NULL
, bma
->ip
, whichfork
, &rec
,
1394 * Check if we are allocation or past the last extent, or at least into
1395 * the last delayed allocated extent.
1397 bma
->aeof
= bma
->offset
>= rec
.br_startoff
+ rec
.br_blockcount
||
1398 (bma
->offset
>= rec
.br_startoff
&&
1399 isnullstartblock(rec
.br_startblock
));
1404 * Returns the file-relative block number of the first block past eof in
1405 * the file. This is not based on i_size, it is based on the extent records.
1406 * Returns 0 for local files, as they do not have extent records.
1409 xfs_bmap_last_offset(
1410 struct xfs_inode
*ip
,
1411 xfs_fileoff_t
*last_block
,
1414 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1415 struct xfs_bmbt_irec rec
;
1421 if (ifp
->if_format
== XFS_DINODE_FMT_LOCAL
)
1424 if (XFS_IS_CORRUPT(ip
->i_mount
, !xfs_ifork_has_extents(ifp
)))
1425 return -EFSCORRUPTED
;
1427 error
= xfs_bmap_last_extent(NULL
, ip
, whichfork
, &rec
, &is_empty
);
1428 if (error
|| is_empty
)
1431 *last_block
= rec
.br_startoff
+ rec
.br_blockcount
;
1436 * Returns whether the selected fork of the inode has exactly one
1437 * block or not. For the data fork we check this matches di_size,
1438 * implying the file's range is 0..bsize-1.
1440 int /* 1=>1 block, 0=>otherwise */
1442 struct xfs_inode
*ip
, /* incore inode */
1443 int whichfork
) /* data or attr fork */
1445 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1446 int rval
; /* return value */
1447 struct xfs_bmbt_irec s
; /* internal version of extent */
1448 struct xfs_iext_cursor icur
;
1451 if (whichfork
== XFS_DATA_FORK
)
1452 return XFS_ISIZE(ip
) == ip
->i_mount
->m_sb
.sb_blocksize
;
1454 if (ifp
->if_nextents
!= 1)
1456 if (ifp
->if_format
!= XFS_DINODE_FMT_EXTENTS
)
1458 ASSERT(ifp
->if_flags
& XFS_IFEXTENTS
);
1459 xfs_iext_first(ifp
, &icur
);
1460 xfs_iext_get_extent(ifp
, &icur
, &s
);
1461 rval
= s
.br_startoff
== 0 && s
.br_blockcount
== 1;
1462 if (rval
&& whichfork
== XFS_DATA_FORK
)
1463 ASSERT(XFS_ISIZE(ip
) == ip
->i_mount
->m_sb
.sb_blocksize
);
1468 * Extent tree manipulation functions used during allocation.
1472 * Convert a delayed allocation to a real allocation.
1474 STATIC
int /* error */
1475 xfs_bmap_add_extent_delay_real(
1476 struct xfs_bmalloca
*bma
,
1479 struct xfs_mount
*mp
= bma
->ip
->i_mount
;
1480 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
1481 struct xfs_bmbt_irec
*new = &bma
->got
;
1482 int error
; /* error return value */
1483 int i
; /* temp state */
1484 xfs_fileoff_t new_endoff
; /* end offset of new entry */
1485 xfs_bmbt_irec_t r
[3]; /* neighbor extent entries */
1486 /* left is 0, right is 1, prev is 2 */
1487 int rval
=0; /* return value (logging flags) */
1488 int state
= xfs_bmap_fork_to_state(whichfork
);
1489 xfs_filblks_t da_new
; /* new count del alloc blocks used */
1490 xfs_filblks_t da_old
; /* old count del alloc blocks used */
1491 xfs_filblks_t temp
=0; /* value for da_new calculations */
1492 int tmp_rval
; /* partial logging flags */
1493 struct xfs_bmbt_irec old
;
1495 ASSERT(whichfork
!= XFS_ATTR_FORK
);
1496 ASSERT(!isnullstartblock(new->br_startblock
));
1498 (bma
->cur
->bc_ino
.flags
& XFS_BTCUR_BMBT_WASDEL
));
1500 XFS_STATS_INC(mp
, xs_add_exlist
);
1507 * Set up a bunch of variables to make the tests simpler.
1509 xfs_iext_get_extent(ifp
, &bma
->icur
, &PREV
);
1510 new_endoff
= new->br_startoff
+ new->br_blockcount
;
1511 ASSERT(isnullstartblock(PREV
.br_startblock
));
1512 ASSERT(PREV
.br_startoff
<= new->br_startoff
);
1513 ASSERT(PREV
.br_startoff
+ PREV
.br_blockcount
>= new_endoff
);
1515 da_old
= startblockval(PREV
.br_startblock
);
1519 * Set flags determining what part of the previous delayed allocation
1520 * extent is being replaced by a real allocation.
1522 if (PREV
.br_startoff
== new->br_startoff
)
1523 state
|= BMAP_LEFT_FILLING
;
1524 if (PREV
.br_startoff
+ PREV
.br_blockcount
== new_endoff
)
1525 state
|= BMAP_RIGHT_FILLING
;
1528 * Check and set flags if this segment has a left neighbor.
1529 * Don't set contiguous if the combined extent would be too large.
1531 if (xfs_iext_peek_prev_extent(ifp
, &bma
->icur
, &LEFT
)) {
1532 state
|= BMAP_LEFT_VALID
;
1533 if (isnullstartblock(LEFT
.br_startblock
))
1534 state
|= BMAP_LEFT_DELAY
;
1537 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
1538 LEFT
.br_startoff
+ LEFT
.br_blockcount
== new->br_startoff
&&
1539 LEFT
.br_startblock
+ LEFT
.br_blockcount
== new->br_startblock
&&
1540 LEFT
.br_state
== new->br_state
&&
1541 LEFT
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
1542 state
|= BMAP_LEFT_CONTIG
;
1545 * Check and set flags if this segment has a right neighbor.
1546 * Don't set contiguous if the combined extent would be too large.
1547 * Also check for all-three-contiguous being too large.
1549 if (xfs_iext_peek_next_extent(ifp
, &bma
->icur
, &RIGHT
)) {
1550 state
|= BMAP_RIGHT_VALID
;
1551 if (isnullstartblock(RIGHT
.br_startblock
))
1552 state
|= BMAP_RIGHT_DELAY
;
1555 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
1556 new_endoff
== RIGHT
.br_startoff
&&
1557 new->br_startblock
+ new->br_blockcount
== RIGHT
.br_startblock
&&
1558 new->br_state
== RIGHT
.br_state
&&
1559 new->br_blockcount
+ RIGHT
.br_blockcount
<= MAXEXTLEN
&&
1560 ((state
& (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
1561 BMAP_RIGHT_FILLING
)) !=
1562 (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
1563 BMAP_RIGHT_FILLING
) ||
1564 LEFT
.br_blockcount
+ new->br_blockcount
+ RIGHT
.br_blockcount
1566 state
|= BMAP_RIGHT_CONTIG
;
1570 * Switch out based on the FILLING and CONTIG state bits.
1572 switch (state
& (BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
1573 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
)) {
1574 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
1575 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
1577 * Filling in all of a previously delayed allocation extent.
1578 * The left and right neighbors are both contiguous with new.
1580 LEFT
.br_blockcount
+= PREV
.br_blockcount
+ RIGHT
.br_blockcount
;
1582 xfs_iext_remove(bma
->ip
, &bma
->icur
, state
);
1583 xfs_iext_remove(bma
->ip
, &bma
->icur
, state
);
1584 xfs_iext_prev(ifp
, &bma
->icur
);
1585 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &LEFT
);
1588 if (bma
->cur
== NULL
)
1589 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1591 rval
= XFS_ILOG_CORE
;
1592 error
= xfs_bmbt_lookup_eq(bma
->cur
, &RIGHT
, &i
);
1595 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
1596 error
= -EFSCORRUPTED
;
1599 error
= xfs_btree_delete(bma
->cur
, &i
);
1602 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
1603 error
= -EFSCORRUPTED
;
1606 error
= xfs_btree_decrement(bma
->cur
, 0, &i
);
1609 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
1610 error
= -EFSCORRUPTED
;
1613 error
= xfs_bmbt_update(bma
->cur
, &LEFT
);
1619 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
1621 * Filling in all of a previously delayed allocation extent.
1622 * The left neighbor is contiguous, the right is not.
1625 LEFT
.br_blockcount
+= PREV
.br_blockcount
;
1627 xfs_iext_remove(bma
->ip
, &bma
->icur
, state
);
1628 xfs_iext_prev(ifp
, &bma
->icur
);
1629 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &LEFT
);
1631 if (bma
->cur
== NULL
)
1632 rval
= XFS_ILOG_DEXT
;
1635 error
= xfs_bmbt_lookup_eq(bma
->cur
, &old
, &i
);
1638 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
1639 error
= -EFSCORRUPTED
;
1642 error
= xfs_bmbt_update(bma
->cur
, &LEFT
);
1648 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
1650 * Filling in all of a previously delayed allocation extent.
1651 * The right neighbor is contiguous, the left is not. Take care
1652 * with delay -> unwritten extent allocation here because the
1653 * delalloc record we are overwriting is always written.
1655 PREV
.br_startblock
= new->br_startblock
;
1656 PREV
.br_blockcount
+= RIGHT
.br_blockcount
;
1657 PREV
.br_state
= new->br_state
;
1659 xfs_iext_next(ifp
, &bma
->icur
);
1660 xfs_iext_remove(bma
->ip
, &bma
->icur
, state
);
1661 xfs_iext_prev(ifp
, &bma
->icur
);
1662 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &PREV
);
1664 if (bma
->cur
== NULL
)
1665 rval
= XFS_ILOG_DEXT
;
1668 error
= xfs_bmbt_lookup_eq(bma
->cur
, &RIGHT
, &i
);
1671 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
1672 error
= -EFSCORRUPTED
;
1675 error
= xfs_bmbt_update(bma
->cur
, &PREV
);
1681 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
1683 * Filling in all of a previously delayed allocation extent.
1684 * Neither the left nor right neighbors are contiguous with
1687 PREV
.br_startblock
= new->br_startblock
;
1688 PREV
.br_state
= new->br_state
;
1689 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &PREV
);
1692 if (bma
->cur
== NULL
)
1693 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1695 rval
= XFS_ILOG_CORE
;
1696 error
= xfs_bmbt_lookup_eq(bma
->cur
, new, &i
);
1699 if (XFS_IS_CORRUPT(mp
, i
!= 0)) {
1700 error
= -EFSCORRUPTED
;
1703 error
= xfs_btree_insert(bma
->cur
, &i
);
1706 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
1707 error
= -EFSCORRUPTED
;
1713 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
:
1715 * Filling in the first part of a previous delayed allocation.
1716 * The left neighbor is contiguous.
1719 temp
= PREV
.br_blockcount
- new->br_blockcount
;
1720 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
1721 startblockval(PREV
.br_startblock
));
1723 LEFT
.br_blockcount
+= new->br_blockcount
;
1725 PREV
.br_blockcount
= temp
;
1726 PREV
.br_startoff
+= new->br_blockcount
;
1727 PREV
.br_startblock
= nullstartblock(da_new
);
1729 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &PREV
);
1730 xfs_iext_prev(ifp
, &bma
->icur
);
1731 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &LEFT
);
1733 if (bma
->cur
== NULL
)
1734 rval
= XFS_ILOG_DEXT
;
1737 error
= xfs_bmbt_lookup_eq(bma
->cur
, &old
, &i
);
1740 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
1741 error
= -EFSCORRUPTED
;
1744 error
= xfs_bmbt_update(bma
->cur
, &LEFT
);
1750 case BMAP_LEFT_FILLING
:
1752 * Filling in the first part of a previous delayed allocation.
1753 * The left neighbor is not contiguous.
1755 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, new);
1758 if (bma
->cur
== NULL
)
1759 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1761 rval
= XFS_ILOG_CORE
;
1762 error
= xfs_bmbt_lookup_eq(bma
->cur
, new, &i
);
1765 if (XFS_IS_CORRUPT(mp
, i
!= 0)) {
1766 error
= -EFSCORRUPTED
;
1769 error
= xfs_btree_insert(bma
->cur
, &i
);
1772 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
1773 error
= -EFSCORRUPTED
;
1778 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
1779 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
1780 &bma
->cur
, 1, &tmp_rval
, whichfork
);
1786 temp
= PREV
.br_blockcount
- new->br_blockcount
;
1787 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
1788 startblockval(PREV
.br_startblock
) -
1789 (bma
->cur
? bma
->cur
->bc_ino
.allocated
: 0));
1791 PREV
.br_startoff
= new_endoff
;
1792 PREV
.br_blockcount
= temp
;
1793 PREV
.br_startblock
= nullstartblock(da_new
);
1794 xfs_iext_next(ifp
, &bma
->icur
);
1795 xfs_iext_insert(bma
->ip
, &bma
->icur
, &PREV
, state
);
1796 xfs_iext_prev(ifp
, &bma
->icur
);
1799 case BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
1801 * Filling in the last part of a previous delayed allocation.
1802 * The right neighbor is contiguous with the new allocation.
1805 RIGHT
.br_startoff
= new->br_startoff
;
1806 RIGHT
.br_startblock
= new->br_startblock
;
1807 RIGHT
.br_blockcount
+= new->br_blockcount
;
1809 if (bma
->cur
== NULL
)
1810 rval
= XFS_ILOG_DEXT
;
1813 error
= xfs_bmbt_lookup_eq(bma
->cur
, &old
, &i
);
1816 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
1817 error
= -EFSCORRUPTED
;
1820 error
= xfs_bmbt_update(bma
->cur
, &RIGHT
);
1825 temp
= PREV
.br_blockcount
- new->br_blockcount
;
1826 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
1827 startblockval(PREV
.br_startblock
));
1829 PREV
.br_blockcount
= temp
;
1830 PREV
.br_startblock
= nullstartblock(da_new
);
1832 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &PREV
);
1833 xfs_iext_next(ifp
, &bma
->icur
);
1834 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &RIGHT
);
1837 case BMAP_RIGHT_FILLING
:
1839 * Filling in the last part of a previous delayed allocation.
1840 * The right neighbor is not contiguous.
1842 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, new);
1845 if (bma
->cur
== NULL
)
1846 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1848 rval
= XFS_ILOG_CORE
;
1849 error
= xfs_bmbt_lookup_eq(bma
->cur
, new, &i
);
1852 if (XFS_IS_CORRUPT(mp
, i
!= 0)) {
1853 error
= -EFSCORRUPTED
;
1856 error
= xfs_btree_insert(bma
->cur
, &i
);
1859 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
1860 error
= -EFSCORRUPTED
;
1865 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
1866 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
1867 &bma
->cur
, 1, &tmp_rval
, whichfork
);
1873 temp
= PREV
.br_blockcount
- new->br_blockcount
;
1874 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
1875 startblockval(PREV
.br_startblock
) -
1876 (bma
->cur
? bma
->cur
->bc_ino
.allocated
: 0));
1878 PREV
.br_startblock
= nullstartblock(da_new
);
1879 PREV
.br_blockcount
= temp
;
1880 xfs_iext_insert(bma
->ip
, &bma
->icur
, &PREV
, state
);
1881 xfs_iext_next(ifp
, &bma
->icur
);
1886 * Filling in the middle part of a previous delayed allocation.
1887 * Contiguity is impossible here.
1888 * This case is avoided almost all the time.
1890 * We start with a delayed allocation:
1892 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
1895 * and we are allocating:
1896 * +rrrrrrrrrrrrrrrrr+
1899 * and we set it up for insertion as:
1900 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
1902 * PREV @ idx LEFT RIGHT
1903 * inserted at idx + 1
1907 /* LEFT is the new middle */
1910 /* RIGHT is the new right */
1911 RIGHT
.br_state
= PREV
.br_state
;
1912 RIGHT
.br_startoff
= new_endoff
;
1913 RIGHT
.br_blockcount
=
1914 PREV
.br_startoff
+ PREV
.br_blockcount
- new_endoff
;
1915 RIGHT
.br_startblock
=
1916 nullstartblock(xfs_bmap_worst_indlen(bma
->ip
,
1917 RIGHT
.br_blockcount
));
1920 PREV
.br_blockcount
= new->br_startoff
- PREV
.br_startoff
;
1921 PREV
.br_startblock
=
1922 nullstartblock(xfs_bmap_worst_indlen(bma
->ip
,
1923 PREV
.br_blockcount
));
1924 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &PREV
);
1926 xfs_iext_next(ifp
, &bma
->icur
);
1927 xfs_iext_insert(bma
->ip
, &bma
->icur
, &RIGHT
, state
);
1928 xfs_iext_insert(bma
->ip
, &bma
->icur
, &LEFT
, state
);
1931 if (bma
->cur
== NULL
)
1932 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1934 rval
= XFS_ILOG_CORE
;
1935 error
= xfs_bmbt_lookup_eq(bma
->cur
, new, &i
);
1938 if (XFS_IS_CORRUPT(mp
, i
!= 0)) {
1939 error
= -EFSCORRUPTED
;
1942 error
= xfs_btree_insert(bma
->cur
, &i
);
1945 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
1946 error
= -EFSCORRUPTED
;
1951 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
1952 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
1953 &bma
->cur
, 1, &tmp_rval
, whichfork
);
1959 da_new
= startblockval(PREV
.br_startblock
) +
1960 startblockval(RIGHT
.br_startblock
);
1963 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
1964 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
1965 case BMAP_LEFT_FILLING
| BMAP_RIGHT_CONTIG
:
1966 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
1967 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
1968 case BMAP_LEFT_CONTIG
:
1969 case BMAP_RIGHT_CONTIG
:
1971 * These cases are all impossible.
1976 /* add reverse mapping unless caller opted out */
1977 if (!(bma
->flags
& XFS_BMAPI_NORMAP
))
1978 xfs_rmap_map_extent(bma
->tp
, bma
->ip
, whichfork
, new);
1980 /* convert to a btree if necessary */
1981 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
1982 int tmp_logflags
; /* partial log flag return val */
1984 ASSERT(bma
->cur
== NULL
);
1985 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
1986 &bma
->cur
, da_old
> 0, &tmp_logflags
,
1988 bma
->logflags
|= tmp_logflags
;
1993 if (da_new
!= da_old
)
1994 xfs_mod_delalloc(mp
, (int64_t)da_new
- da_old
);
1997 da_new
+= bma
->cur
->bc_ino
.allocated
;
1998 bma
->cur
->bc_ino
.allocated
= 0;
2001 /* adjust for changes in reserved delayed indirect blocks */
2002 if (da_new
!= da_old
) {
2003 ASSERT(state
== 0 || da_new
< da_old
);
2004 error
= xfs_mod_fdblocks(mp
, (int64_t)(da_old
- da_new
),
2008 xfs_bmap_check_leaf_extents(bma
->cur
, bma
->ip
, whichfork
);
2010 if (whichfork
!= XFS_COW_FORK
)
2011 bma
->logflags
|= rval
;
2019 * Convert an unwritten allocation to a real allocation or vice versa.
2022 xfs_bmap_add_extent_unwritten_real(
2023 struct xfs_trans
*tp
,
2024 xfs_inode_t
*ip
, /* incore inode pointer */
2026 struct xfs_iext_cursor
*icur
,
2027 xfs_btree_cur_t
**curp
, /* if *curp is null, not a btree */
2028 xfs_bmbt_irec_t
*new, /* new data to add to file extents */
2029 int *logflagsp
) /* inode logging flags */
2031 xfs_btree_cur_t
*cur
; /* btree cursor */
2032 int error
; /* error return value */
2033 int i
; /* temp state */
2034 struct xfs_ifork
*ifp
; /* inode fork pointer */
2035 xfs_fileoff_t new_endoff
; /* end offset of new entry */
2036 xfs_bmbt_irec_t r
[3]; /* neighbor extent entries */
2037 /* left is 0, right is 1, prev is 2 */
2038 int rval
=0; /* return value (logging flags) */
2039 int state
= xfs_bmap_fork_to_state(whichfork
);
2040 struct xfs_mount
*mp
= ip
->i_mount
;
2041 struct xfs_bmbt_irec old
;
2046 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
2048 ASSERT(!isnullstartblock(new->br_startblock
));
2050 XFS_STATS_INC(mp
, xs_add_exlist
);
2057 * Set up a bunch of variables to make the tests simpler.
2060 xfs_iext_get_extent(ifp
, icur
, &PREV
);
2061 ASSERT(new->br_state
!= PREV
.br_state
);
2062 new_endoff
= new->br_startoff
+ new->br_blockcount
;
2063 ASSERT(PREV
.br_startoff
<= new->br_startoff
);
2064 ASSERT(PREV
.br_startoff
+ PREV
.br_blockcount
>= new_endoff
);
2067 * Set flags determining what part of the previous oldext allocation
2068 * extent is being replaced by a newext allocation.
2070 if (PREV
.br_startoff
== new->br_startoff
)
2071 state
|= BMAP_LEFT_FILLING
;
2072 if (PREV
.br_startoff
+ PREV
.br_blockcount
== new_endoff
)
2073 state
|= BMAP_RIGHT_FILLING
;
2076 * Check and set flags if this segment has a left neighbor.
2077 * Don't set contiguous if the combined extent would be too large.
2079 if (xfs_iext_peek_prev_extent(ifp
, icur
, &LEFT
)) {
2080 state
|= BMAP_LEFT_VALID
;
2081 if (isnullstartblock(LEFT
.br_startblock
))
2082 state
|= BMAP_LEFT_DELAY
;
2085 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
2086 LEFT
.br_startoff
+ LEFT
.br_blockcount
== new->br_startoff
&&
2087 LEFT
.br_startblock
+ LEFT
.br_blockcount
== new->br_startblock
&&
2088 LEFT
.br_state
== new->br_state
&&
2089 LEFT
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
2090 state
|= BMAP_LEFT_CONTIG
;
2093 * Check and set flags if this segment has a right neighbor.
2094 * Don't set contiguous if the combined extent would be too large.
2095 * Also check for all-three-contiguous being too large.
2097 if (xfs_iext_peek_next_extent(ifp
, icur
, &RIGHT
)) {
2098 state
|= BMAP_RIGHT_VALID
;
2099 if (isnullstartblock(RIGHT
.br_startblock
))
2100 state
|= BMAP_RIGHT_DELAY
;
2103 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
2104 new_endoff
== RIGHT
.br_startoff
&&
2105 new->br_startblock
+ new->br_blockcount
== RIGHT
.br_startblock
&&
2106 new->br_state
== RIGHT
.br_state
&&
2107 new->br_blockcount
+ RIGHT
.br_blockcount
<= MAXEXTLEN
&&
2108 ((state
& (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
2109 BMAP_RIGHT_FILLING
)) !=
2110 (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
2111 BMAP_RIGHT_FILLING
) ||
2112 LEFT
.br_blockcount
+ new->br_blockcount
+ RIGHT
.br_blockcount
2114 state
|= BMAP_RIGHT_CONTIG
;
2117 * Switch out based on the FILLING and CONTIG state bits.
2119 switch (state
& (BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
2120 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
)) {
2121 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
2122 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2124 * Setting all of a previous oldext extent to newext.
2125 * The left and right neighbors are both contiguous with new.
2127 LEFT
.br_blockcount
+= PREV
.br_blockcount
+ RIGHT
.br_blockcount
;
2129 xfs_iext_remove(ip
, icur
, state
);
2130 xfs_iext_remove(ip
, icur
, state
);
2131 xfs_iext_prev(ifp
, icur
);
2132 xfs_iext_update_extent(ip
, state
, icur
, &LEFT
);
2133 ifp
->if_nextents
-= 2;
2135 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2137 rval
= XFS_ILOG_CORE
;
2138 error
= xfs_bmbt_lookup_eq(cur
, &RIGHT
, &i
);
2141 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
2142 error
= -EFSCORRUPTED
;
2145 if ((error
= xfs_btree_delete(cur
, &i
)))
2147 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
2148 error
= -EFSCORRUPTED
;
2151 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2153 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
2154 error
= -EFSCORRUPTED
;
2157 if ((error
= xfs_btree_delete(cur
, &i
)))
2159 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
2160 error
= -EFSCORRUPTED
;
2163 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2165 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
2166 error
= -EFSCORRUPTED
;
2169 error
= xfs_bmbt_update(cur
, &LEFT
);
2175 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
2177 * Setting all of a previous oldext extent to newext.
2178 * The left neighbor is contiguous, the right is not.
2180 LEFT
.br_blockcount
+= PREV
.br_blockcount
;
2182 xfs_iext_remove(ip
, icur
, state
);
2183 xfs_iext_prev(ifp
, icur
);
2184 xfs_iext_update_extent(ip
, state
, icur
, &LEFT
);
2187 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2189 rval
= XFS_ILOG_CORE
;
2190 error
= xfs_bmbt_lookup_eq(cur
, &PREV
, &i
);
2193 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
2194 error
= -EFSCORRUPTED
;
2197 if ((error
= xfs_btree_delete(cur
, &i
)))
2199 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
2200 error
= -EFSCORRUPTED
;
2203 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2205 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
2206 error
= -EFSCORRUPTED
;
2209 error
= xfs_bmbt_update(cur
, &LEFT
);
2215 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2217 * Setting all of a previous oldext extent to newext.
2218 * The right neighbor is contiguous, the left is not.
2220 PREV
.br_blockcount
+= RIGHT
.br_blockcount
;
2221 PREV
.br_state
= new->br_state
;
2223 xfs_iext_next(ifp
, icur
);
2224 xfs_iext_remove(ip
, icur
, state
);
2225 xfs_iext_prev(ifp
, icur
);
2226 xfs_iext_update_extent(ip
, state
, icur
, &PREV
);
2230 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2232 rval
= XFS_ILOG_CORE
;
2233 error
= xfs_bmbt_lookup_eq(cur
, &RIGHT
, &i
);
2236 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
2237 error
= -EFSCORRUPTED
;
2240 if ((error
= xfs_btree_delete(cur
, &i
)))
2242 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
2243 error
= -EFSCORRUPTED
;
2246 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2248 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
2249 error
= -EFSCORRUPTED
;
2252 error
= xfs_bmbt_update(cur
, &PREV
);
2258 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
2260 * Setting all of a previous oldext extent to newext.
2261 * Neither the left nor right neighbors are contiguous with
2264 PREV
.br_state
= new->br_state
;
2265 xfs_iext_update_extent(ip
, state
, icur
, &PREV
);
2268 rval
= XFS_ILOG_DEXT
;
2271 error
= xfs_bmbt_lookup_eq(cur
, new, &i
);
2274 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
2275 error
= -EFSCORRUPTED
;
2278 error
= xfs_bmbt_update(cur
, &PREV
);
2284 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
:
2286 * Setting the first part of a previous oldext extent to newext.
2287 * The left neighbor is contiguous.
2289 LEFT
.br_blockcount
+= new->br_blockcount
;
2292 PREV
.br_startoff
+= new->br_blockcount
;
2293 PREV
.br_startblock
+= new->br_blockcount
;
2294 PREV
.br_blockcount
-= new->br_blockcount
;
2296 xfs_iext_update_extent(ip
, state
, icur
, &PREV
);
2297 xfs_iext_prev(ifp
, icur
);
2298 xfs_iext_update_extent(ip
, state
, icur
, &LEFT
);
2301 rval
= XFS_ILOG_DEXT
;
2304 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2307 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
2308 error
= -EFSCORRUPTED
;
2311 error
= xfs_bmbt_update(cur
, &PREV
);
2314 error
= xfs_btree_decrement(cur
, 0, &i
);
2317 error
= xfs_bmbt_update(cur
, &LEFT
);
2323 case BMAP_LEFT_FILLING
:
2325 * Setting the first part of a previous oldext extent to newext.
2326 * The left neighbor is not contiguous.
2329 PREV
.br_startoff
+= new->br_blockcount
;
2330 PREV
.br_startblock
+= new->br_blockcount
;
2331 PREV
.br_blockcount
-= new->br_blockcount
;
2333 xfs_iext_update_extent(ip
, state
, icur
, &PREV
);
2334 xfs_iext_insert(ip
, icur
, new, state
);
2338 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2340 rval
= XFS_ILOG_CORE
;
2341 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2344 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
2345 error
= -EFSCORRUPTED
;
2348 error
= xfs_bmbt_update(cur
, &PREV
);
2351 cur
->bc_rec
.b
= *new;
2352 if ((error
= xfs_btree_insert(cur
, &i
)))
2354 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
2355 error
= -EFSCORRUPTED
;
2361 case BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2363 * Setting the last part of a previous oldext extent to newext.
2364 * The right neighbor is contiguous with the new allocation.
2367 PREV
.br_blockcount
-= new->br_blockcount
;
2369 RIGHT
.br_startoff
= new->br_startoff
;
2370 RIGHT
.br_startblock
= new->br_startblock
;
2371 RIGHT
.br_blockcount
+= new->br_blockcount
;
2373 xfs_iext_update_extent(ip
, state
, icur
, &PREV
);
2374 xfs_iext_next(ifp
, icur
);
2375 xfs_iext_update_extent(ip
, state
, icur
, &RIGHT
);
2378 rval
= XFS_ILOG_DEXT
;
2381 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2384 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
2385 error
= -EFSCORRUPTED
;
2388 error
= xfs_bmbt_update(cur
, &PREV
);
2391 error
= xfs_btree_increment(cur
, 0, &i
);
2394 error
= xfs_bmbt_update(cur
, &RIGHT
);
2400 case BMAP_RIGHT_FILLING
:
2402 * Setting the last part of a previous oldext extent to newext.
2403 * The right neighbor is not contiguous.
2406 PREV
.br_blockcount
-= new->br_blockcount
;
2408 xfs_iext_update_extent(ip
, state
, icur
, &PREV
);
2409 xfs_iext_next(ifp
, icur
);
2410 xfs_iext_insert(ip
, icur
, new, state
);
2414 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2416 rval
= XFS_ILOG_CORE
;
2417 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2420 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
2421 error
= -EFSCORRUPTED
;
2424 error
= xfs_bmbt_update(cur
, &PREV
);
2427 error
= xfs_bmbt_lookup_eq(cur
, new, &i
);
2430 if (XFS_IS_CORRUPT(mp
, i
!= 0)) {
2431 error
= -EFSCORRUPTED
;
2434 if ((error
= xfs_btree_insert(cur
, &i
)))
2436 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
2437 error
= -EFSCORRUPTED
;
2445 * Setting the middle part of a previous oldext extent to
2446 * newext. Contiguity is impossible here.
2447 * One extent becomes three extents.
2450 PREV
.br_blockcount
= new->br_startoff
- PREV
.br_startoff
;
2453 r
[1].br_startoff
= new_endoff
;
2454 r
[1].br_blockcount
=
2455 old
.br_startoff
+ old
.br_blockcount
- new_endoff
;
2456 r
[1].br_startblock
= new->br_startblock
+ new->br_blockcount
;
2457 r
[1].br_state
= PREV
.br_state
;
2459 xfs_iext_update_extent(ip
, state
, icur
, &PREV
);
2460 xfs_iext_next(ifp
, icur
);
2461 xfs_iext_insert(ip
, icur
, &r
[1], state
);
2462 xfs_iext_insert(ip
, icur
, &r
[0], state
);
2463 ifp
->if_nextents
+= 2;
2466 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2468 rval
= XFS_ILOG_CORE
;
2469 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2472 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
2473 error
= -EFSCORRUPTED
;
2476 /* new right extent - oldext */
2477 error
= xfs_bmbt_update(cur
, &r
[1]);
2480 /* new left extent - oldext */
2481 cur
->bc_rec
.b
= PREV
;
2482 if ((error
= xfs_btree_insert(cur
, &i
)))
2484 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
2485 error
= -EFSCORRUPTED
;
2489 * Reset the cursor to the position of the new extent
2490 * we are about to insert as we can't trust it after
2491 * the previous insert.
2493 error
= xfs_bmbt_lookup_eq(cur
, new, &i
);
2496 if (XFS_IS_CORRUPT(mp
, i
!= 0)) {
2497 error
= -EFSCORRUPTED
;
2500 /* new middle extent - newext */
2501 if ((error
= xfs_btree_insert(cur
, &i
)))
2503 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
2504 error
= -EFSCORRUPTED
;
2510 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2511 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2512 case BMAP_LEFT_FILLING
| BMAP_RIGHT_CONTIG
:
2513 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
2514 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2515 case BMAP_LEFT_CONTIG
:
2516 case BMAP_RIGHT_CONTIG
:
2518 * These cases are all impossible.
2523 /* update reverse mappings */
2524 xfs_rmap_convert_extent(mp
, tp
, ip
, whichfork
, new);
2526 /* convert to a btree if necessary */
2527 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
2528 int tmp_logflags
; /* partial log flag return val */
2530 ASSERT(cur
== NULL
);
2531 error
= xfs_bmap_extents_to_btree(tp
, ip
, &cur
, 0,
2532 &tmp_logflags
, whichfork
);
2533 *logflagsp
|= tmp_logflags
;
2538 /* clear out the allocated field, done with it now in any case. */
2540 cur
->bc_ino
.allocated
= 0;
2544 xfs_bmap_check_leaf_extents(*curp
, ip
, whichfork
);
2554 * Convert a hole to a delayed allocation.
2557 xfs_bmap_add_extent_hole_delay(
2558 xfs_inode_t
*ip
, /* incore inode pointer */
2560 struct xfs_iext_cursor
*icur
,
2561 xfs_bmbt_irec_t
*new) /* new data to add to file extents */
2563 struct xfs_ifork
*ifp
; /* inode fork pointer */
2564 xfs_bmbt_irec_t left
; /* left neighbor extent entry */
2565 xfs_filblks_t newlen
=0; /* new indirect size */
2566 xfs_filblks_t oldlen
=0; /* old indirect size */
2567 xfs_bmbt_irec_t right
; /* right neighbor extent entry */
2568 int state
= xfs_bmap_fork_to_state(whichfork
);
2569 xfs_filblks_t temp
; /* temp for indirect calculations */
2571 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
2572 ASSERT(isnullstartblock(new->br_startblock
));
2575 * Check and set flags if this segment has a left neighbor
2577 if (xfs_iext_peek_prev_extent(ifp
, icur
, &left
)) {
2578 state
|= BMAP_LEFT_VALID
;
2579 if (isnullstartblock(left
.br_startblock
))
2580 state
|= BMAP_LEFT_DELAY
;
2584 * Check and set flags if the current (right) segment exists.
2585 * If it doesn't exist, we're converting the hole at end-of-file.
2587 if (xfs_iext_get_extent(ifp
, icur
, &right
)) {
2588 state
|= BMAP_RIGHT_VALID
;
2589 if (isnullstartblock(right
.br_startblock
))
2590 state
|= BMAP_RIGHT_DELAY
;
2594 * Set contiguity flags on the left and right neighbors.
2595 * Don't let extents get too large, even if the pieces are contiguous.
2597 if ((state
& BMAP_LEFT_VALID
) && (state
& BMAP_LEFT_DELAY
) &&
2598 left
.br_startoff
+ left
.br_blockcount
== new->br_startoff
&&
2599 left
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
2600 state
|= BMAP_LEFT_CONTIG
;
2602 if ((state
& BMAP_RIGHT_VALID
) && (state
& BMAP_RIGHT_DELAY
) &&
2603 new->br_startoff
+ new->br_blockcount
== right
.br_startoff
&&
2604 new->br_blockcount
+ right
.br_blockcount
<= MAXEXTLEN
&&
2605 (!(state
& BMAP_LEFT_CONTIG
) ||
2606 (left
.br_blockcount
+ new->br_blockcount
+
2607 right
.br_blockcount
<= MAXEXTLEN
)))
2608 state
|= BMAP_RIGHT_CONTIG
;
2611 * Switch out based on the contiguity flags.
2613 switch (state
& (BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
)) {
2614 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2616 * New allocation is contiguous with delayed allocations
2617 * on the left and on the right.
2618 * Merge all three into a single extent record.
2620 temp
= left
.br_blockcount
+ new->br_blockcount
+
2621 right
.br_blockcount
;
2623 oldlen
= startblockval(left
.br_startblock
) +
2624 startblockval(new->br_startblock
) +
2625 startblockval(right
.br_startblock
);
2626 newlen
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
2628 left
.br_startblock
= nullstartblock(newlen
);
2629 left
.br_blockcount
= temp
;
2631 xfs_iext_remove(ip
, icur
, state
);
2632 xfs_iext_prev(ifp
, icur
);
2633 xfs_iext_update_extent(ip
, state
, icur
, &left
);
2636 case BMAP_LEFT_CONTIG
:
2638 * New allocation is contiguous with a delayed allocation
2640 * Merge the new allocation with the left neighbor.
2642 temp
= left
.br_blockcount
+ new->br_blockcount
;
2644 oldlen
= startblockval(left
.br_startblock
) +
2645 startblockval(new->br_startblock
);
2646 newlen
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
2648 left
.br_blockcount
= temp
;
2649 left
.br_startblock
= nullstartblock(newlen
);
2651 xfs_iext_prev(ifp
, icur
);
2652 xfs_iext_update_extent(ip
, state
, icur
, &left
);
2655 case BMAP_RIGHT_CONTIG
:
2657 * New allocation is contiguous with a delayed allocation
2659 * Merge the new allocation with the right neighbor.
2661 temp
= new->br_blockcount
+ right
.br_blockcount
;
2662 oldlen
= startblockval(new->br_startblock
) +
2663 startblockval(right
.br_startblock
);
2664 newlen
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
2666 right
.br_startoff
= new->br_startoff
;
2667 right
.br_startblock
= nullstartblock(newlen
);
2668 right
.br_blockcount
= temp
;
2669 xfs_iext_update_extent(ip
, state
, icur
, &right
);
2674 * New allocation is not contiguous with another
2675 * delayed allocation.
2676 * Insert a new entry.
2678 oldlen
= newlen
= 0;
2679 xfs_iext_insert(ip
, icur
, new, state
);
2682 if (oldlen
!= newlen
) {
2683 ASSERT(oldlen
> newlen
);
2684 xfs_mod_fdblocks(ip
->i_mount
, (int64_t)(oldlen
- newlen
),
2687 * Nothing to do for disk quota accounting here.
2689 xfs_mod_delalloc(ip
->i_mount
, (int64_t)newlen
- oldlen
);
2694 * Convert a hole to a real allocation.
2696 STATIC
int /* error */
2697 xfs_bmap_add_extent_hole_real(
2698 struct xfs_trans
*tp
,
2699 struct xfs_inode
*ip
,
2701 struct xfs_iext_cursor
*icur
,
2702 struct xfs_btree_cur
**curp
,
2703 struct xfs_bmbt_irec
*new,
2707 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
2708 struct xfs_mount
*mp
= ip
->i_mount
;
2709 struct xfs_btree_cur
*cur
= *curp
;
2710 int error
; /* error return value */
2711 int i
; /* temp state */
2712 xfs_bmbt_irec_t left
; /* left neighbor extent entry */
2713 xfs_bmbt_irec_t right
; /* right neighbor extent entry */
2714 int rval
=0; /* return value (logging flags) */
2715 int state
= xfs_bmap_fork_to_state(whichfork
);
2716 struct xfs_bmbt_irec old
;
2718 ASSERT(!isnullstartblock(new->br_startblock
));
2719 ASSERT(!cur
|| !(cur
->bc_ino
.flags
& XFS_BTCUR_BMBT_WASDEL
));
2721 XFS_STATS_INC(mp
, xs_add_exlist
);
2724 * Check and set flags if this segment has a left neighbor.
2726 if (xfs_iext_peek_prev_extent(ifp
, icur
, &left
)) {
2727 state
|= BMAP_LEFT_VALID
;
2728 if (isnullstartblock(left
.br_startblock
))
2729 state
|= BMAP_LEFT_DELAY
;
2733 * Check and set flags if this segment has a current value.
2734 * Not true if we're inserting into the "hole" at eof.
2736 if (xfs_iext_get_extent(ifp
, icur
, &right
)) {
2737 state
|= BMAP_RIGHT_VALID
;
2738 if (isnullstartblock(right
.br_startblock
))
2739 state
|= BMAP_RIGHT_DELAY
;
2743 * We're inserting a real allocation between "left" and "right".
2744 * Set the contiguity flags. Don't let extents get too large.
2746 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
2747 left
.br_startoff
+ left
.br_blockcount
== new->br_startoff
&&
2748 left
.br_startblock
+ left
.br_blockcount
== new->br_startblock
&&
2749 left
.br_state
== new->br_state
&&
2750 left
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
2751 state
|= BMAP_LEFT_CONTIG
;
2753 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
2754 new->br_startoff
+ new->br_blockcount
== right
.br_startoff
&&
2755 new->br_startblock
+ new->br_blockcount
== right
.br_startblock
&&
2756 new->br_state
== right
.br_state
&&
2757 new->br_blockcount
+ right
.br_blockcount
<= MAXEXTLEN
&&
2758 (!(state
& BMAP_LEFT_CONTIG
) ||
2759 left
.br_blockcount
+ new->br_blockcount
+
2760 right
.br_blockcount
<= MAXEXTLEN
))
2761 state
|= BMAP_RIGHT_CONTIG
;
2765 * Select which case we're in here, and implement it.
2767 switch (state
& (BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
)) {
2768 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2770 * New allocation is contiguous with real allocations on the
2771 * left and on the right.
2772 * Merge all three into a single extent record.
2774 left
.br_blockcount
+= new->br_blockcount
+ right
.br_blockcount
;
2776 xfs_iext_remove(ip
, icur
, state
);
2777 xfs_iext_prev(ifp
, icur
);
2778 xfs_iext_update_extent(ip
, state
, icur
, &left
);
2782 rval
= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
2784 rval
= XFS_ILOG_CORE
;
2785 error
= xfs_bmbt_lookup_eq(cur
, &right
, &i
);
2788 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
2789 error
= -EFSCORRUPTED
;
2792 error
= xfs_btree_delete(cur
, &i
);
2795 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
2796 error
= -EFSCORRUPTED
;
2799 error
= xfs_btree_decrement(cur
, 0, &i
);
2802 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
2803 error
= -EFSCORRUPTED
;
2806 error
= xfs_bmbt_update(cur
, &left
);
2812 case BMAP_LEFT_CONTIG
:
2814 * New allocation is contiguous with a real allocation
2816 * Merge the new allocation with the left neighbor.
2819 left
.br_blockcount
+= new->br_blockcount
;
2821 xfs_iext_prev(ifp
, icur
);
2822 xfs_iext_update_extent(ip
, state
, icur
, &left
);
2825 rval
= xfs_ilog_fext(whichfork
);
2828 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2831 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
2832 error
= -EFSCORRUPTED
;
2835 error
= xfs_bmbt_update(cur
, &left
);
2841 case BMAP_RIGHT_CONTIG
:
2843 * New allocation is contiguous with a real allocation
2845 * Merge the new allocation with the right neighbor.
2849 right
.br_startoff
= new->br_startoff
;
2850 right
.br_startblock
= new->br_startblock
;
2851 right
.br_blockcount
+= new->br_blockcount
;
2852 xfs_iext_update_extent(ip
, state
, icur
, &right
);
2855 rval
= xfs_ilog_fext(whichfork
);
2858 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2861 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
2862 error
= -EFSCORRUPTED
;
2865 error
= xfs_bmbt_update(cur
, &right
);
2873 * New allocation is not contiguous with another
2875 * Insert a new entry.
2877 xfs_iext_insert(ip
, icur
, new, state
);
2881 rval
= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
2883 rval
= XFS_ILOG_CORE
;
2884 error
= xfs_bmbt_lookup_eq(cur
, new, &i
);
2887 if (XFS_IS_CORRUPT(mp
, i
!= 0)) {
2888 error
= -EFSCORRUPTED
;
2891 error
= xfs_btree_insert(cur
, &i
);
2894 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
2895 error
= -EFSCORRUPTED
;
2902 /* add reverse mapping unless caller opted out */
2903 if (!(flags
& XFS_BMAPI_NORMAP
))
2904 xfs_rmap_map_extent(tp
, ip
, whichfork
, new);
2906 /* convert to a btree if necessary */
2907 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
2908 int tmp_logflags
; /* partial log flag return val */
2910 ASSERT(cur
== NULL
);
2911 error
= xfs_bmap_extents_to_btree(tp
, ip
, curp
, 0,
2912 &tmp_logflags
, whichfork
);
2913 *logflagsp
|= tmp_logflags
;
2919 /* clear out the allocated field, done with it now in any case. */
2921 cur
->bc_ino
.allocated
= 0;
2923 xfs_bmap_check_leaf_extents(cur
, ip
, whichfork
);
2930 * Functions used in the extent read, allocate and remove paths
2934 * Adjust the size of the new extent based on di_extsize and rt extsize.
2937 xfs_bmap_extsize_align(
2939 xfs_bmbt_irec_t
*gotp
, /* next extent pointer */
2940 xfs_bmbt_irec_t
*prevp
, /* previous extent pointer */
2941 xfs_extlen_t extsz
, /* align to this extent size */
2942 int rt
, /* is this a realtime inode? */
2943 int eof
, /* is extent at end-of-file? */
2944 int delay
, /* creating delalloc extent? */
2945 int convert
, /* overwriting unwritten extent? */
2946 xfs_fileoff_t
*offp
, /* in/out: aligned offset */
2947 xfs_extlen_t
*lenp
) /* in/out: aligned length */
2949 xfs_fileoff_t orig_off
; /* original offset */
2950 xfs_extlen_t orig_alen
; /* original length */
2951 xfs_fileoff_t orig_end
; /* original off+len */
2952 xfs_fileoff_t nexto
; /* next file offset */
2953 xfs_fileoff_t prevo
; /* previous file offset */
2954 xfs_fileoff_t align_off
; /* temp for offset */
2955 xfs_extlen_t align_alen
; /* temp for length */
2956 xfs_extlen_t temp
; /* temp for calculations */
2961 orig_off
= align_off
= *offp
;
2962 orig_alen
= align_alen
= *lenp
;
2963 orig_end
= orig_off
+ orig_alen
;
2966 * If this request overlaps an existing extent, then don't
2967 * attempt to perform any additional alignment.
2969 if (!delay
&& !eof
&&
2970 (orig_off
>= gotp
->br_startoff
) &&
2971 (orig_end
<= gotp
->br_startoff
+ gotp
->br_blockcount
)) {
2976 * If the file offset is unaligned vs. the extent size
2977 * we need to align it. This will be possible unless
2978 * the file was previously written with a kernel that didn't
2979 * perform this alignment, or if a truncate shot us in the
2982 div_u64_rem(orig_off
, extsz
, &temp
);
2988 /* Same adjustment for the end of the requested area. */
2989 temp
= (align_alen
% extsz
);
2991 align_alen
+= extsz
- temp
;
2994 * For large extent hint sizes, the aligned extent might be larger than
2995 * MAXEXTLEN. In that case, reduce the size by an extsz so that it pulls
2996 * the length back under MAXEXTLEN. The outer allocation loops handle
2997 * short allocation just fine, so it is safe to do this. We only want to
2998 * do it when we are forced to, though, because it means more allocation
2999 * operations are required.
3001 while (align_alen
> MAXEXTLEN
)
3002 align_alen
-= extsz
;
3003 ASSERT(align_alen
<= MAXEXTLEN
);
3006 * If the previous block overlaps with this proposed allocation
3007 * then move the start forward without adjusting the length.
3009 if (prevp
->br_startoff
!= NULLFILEOFF
) {
3010 if (prevp
->br_startblock
== HOLESTARTBLOCK
)
3011 prevo
= prevp
->br_startoff
;
3013 prevo
= prevp
->br_startoff
+ prevp
->br_blockcount
;
3016 if (align_off
!= orig_off
&& align_off
< prevo
)
3019 * If the next block overlaps with this proposed allocation
3020 * then move the start back without adjusting the length,
3021 * but not before offset 0.
3022 * This may of course make the start overlap previous block,
3023 * and if we hit the offset 0 limit then the next block
3024 * can still overlap too.
3026 if (!eof
&& gotp
->br_startoff
!= NULLFILEOFF
) {
3027 if ((delay
&& gotp
->br_startblock
== HOLESTARTBLOCK
) ||
3028 (!delay
&& gotp
->br_startblock
== DELAYSTARTBLOCK
))
3029 nexto
= gotp
->br_startoff
+ gotp
->br_blockcount
;
3031 nexto
= gotp
->br_startoff
;
3033 nexto
= NULLFILEOFF
;
3035 align_off
+ align_alen
!= orig_end
&&
3036 align_off
+ align_alen
> nexto
)
3037 align_off
= nexto
> align_alen
? nexto
- align_alen
: 0;
3039 * If we're now overlapping the next or previous extent that
3040 * means we can't fit an extsz piece in this hole. Just move
3041 * the start forward to the first valid spot and set
3042 * the length so we hit the end.
3044 if (align_off
!= orig_off
&& align_off
< prevo
)
3046 if (align_off
+ align_alen
!= orig_end
&&
3047 align_off
+ align_alen
> nexto
&&
3048 nexto
!= NULLFILEOFF
) {
3049 ASSERT(nexto
> prevo
);
3050 align_alen
= nexto
- align_off
;
3054 * If realtime, and the result isn't a multiple of the realtime
3055 * extent size we need to remove blocks until it is.
3057 if (rt
&& (temp
= (align_alen
% mp
->m_sb
.sb_rextsize
))) {
3059 * We're not covering the original request, or
3060 * we won't be able to once we fix the length.
3062 if (orig_off
< align_off
||
3063 orig_end
> align_off
+ align_alen
||
3064 align_alen
- temp
< orig_alen
)
3067 * Try to fix it by moving the start up.
3069 if (align_off
+ temp
<= orig_off
) {
3074 * Try to fix it by moving the end in.
3076 else if (align_off
+ align_alen
- temp
>= orig_end
)
3079 * Set the start to the minimum then trim the length.
3082 align_alen
-= orig_off
- align_off
;
3083 align_off
= orig_off
;
3084 align_alen
-= align_alen
% mp
->m_sb
.sb_rextsize
;
3087 * Result doesn't cover the request, fail it.
3089 if (orig_off
< align_off
|| orig_end
> align_off
+ align_alen
)
3092 ASSERT(orig_off
>= align_off
);
3093 /* see MAXEXTLEN handling above */
3094 ASSERT(orig_end
<= align_off
+ align_alen
||
3095 align_alen
+ extsz
> MAXEXTLEN
);
3099 if (!eof
&& gotp
->br_startoff
!= NULLFILEOFF
)
3100 ASSERT(align_off
+ align_alen
<= gotp
->br_startoff
);
3101 if (prevp
->br_startoff
!= NULLFILEOFF
)
3102 ASSERT(align_off
>= prevp
->br_startoff
+ prevp
->br_blockcount
);
3110 #define XFS_ALLOC_GAP_UNITS 4
3114 struct xfs_bmalloca
*ap
) /* bmap alloc argument struct */
3116 xfs_fsblock_t adjust
; /* adjustment to block numbers */
3117 xfs_agnumber_t fb_agno
; /* ag number of ap->firstblock */
3118 xfs_mount_t
*mp
; /* mount point structure */
3119 int nullfb
; /* true if ap->firstblock isn't set */
3120 int rt
; /* true if inode is realtime */
3122 #define ISVALID(x,y) \
3124 (x) < mp->m_sb.sb_rblocks : \
3125 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
3126 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
3127 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
3129 mp
= ap
->ip
->i_mount
;
3130 nullfb
= ap
->tp
->t_firstblock
== NULLFSBLOCK
;
3131 rt
= XFS_IS_REALTIME_INODE(ap
->ip
) &&
3132 (ap
->datatype
& XFS_ALLOC_USERDATA
);
3133 fb_agno
= nullfb
? NULLAGNUMBER
: XFS_FSB_TO_AGNO(mp
,
3134 ap
->tp
->t_firstblock
);
3136 * If allocating at eof, and there's a previous real block,
3137 * try to use its last block as our starting point.
3139 if (ap
->eof
&& ap
->prev
.br_startoff
!= NULLFILEOFF
&&
3140 !isnullstartblock(ap
->prev
.br_startblock
) &&
3141 ISVALID(ap
->prev
.br_startblock
+ ap
->prev
.br_blockcount
,
3142 ap
->prev
.br_startblock
)) {
3143 ap
->blkno
= ap
->prev
.br_startblock
+ ap
->prev
.br_blockcount
;
3145 * Adjust for the gap between prevp and us.
3147 adjust
= ap
->offset
-
3148 (ap
->prev
.br_startoff
+ ap
->prev
.br_blockcount
);
3150 ISVALID(ap
->blkno
+ adjust
, ap
->prev
.br_startblock
))
3151 ap
->blkno
+= adjust
;
3154 * If not at eof, then compare the two neighbor blocks.
3155 * Figure out whether either one gives us a good starting point,
3156 * and pick the better one.
3158 else if (!ap
->eof
) {
3159 xfs_fsblock_t gotbno
; /* right side block number */
3160 xfs_fsblock_t gotdiff
=0; /* right side difference */
3161 xfs_fsblock_t prevbno
; /* left side block number */
3162 xfs_fsblock_t prevdiff
=0; /* left side difference */
3165 * If there's a previous (left) block, select a requested
3166 * start block based on it.
3168 if (ap
->prev
.br_startoff
!= NULLFILEOFF
&&
3169 !isnullstartblock(ap
->prev
.br_startblock
) &&
3170 (prevbno
= ap
->prev
.br_startblock
+
3171 ap
->prev
.br_blockcount
) &&
3172 ISVALID(prevbno
, ap
->prev
.br_startblock
)) {
3174 * Calculate gap to end of previous block.
3176 adjust
= prevdiff
= ap
->offset
-
3177 (ap
->prev
.br_startoff
+
3178 ap
->prev
.br_blockcount
);
3180 * Figure the startblock based on the previous block's
3181 * end and the gap size.
3183 * If the gap is large relative to the piece we're
3184 * allocating, or using it gives us an invalid block
3185 * number, then just use the end of the previous block.
3187 if (prevdiff
<= XFS_ALLOC_GAP_UNITS
* ap
->length
&&
3188 ISVALID(prevbno
+ prevdiff
,
3189 ap
->prev
.br_startblock
))
3194 * If the firstblock forbids it, can't use it,
3197 if (!rt
&& !nullfb
&&
3198 XFS_FSB_TO_AGNO(mp
, prevbno
) != fb_agno
)
3199 prevbno
= NULLFSBLOCK
;
3202 * No previous block or can't follow it, just default.
3205 prevbno
= NULLFSBLOCK
;
3207 * If there's a following (right) block, select a requested
3208 * start block based on it.
3210 if (!isnullstartblock(ap
->got
.br_startblock
)) {
3212 * Calculate gap to start of next block.
3214 adjust
= gotdiff
= ap
->got
.br_startoff
- ap
->offset
;
3216 * Figure the startblock based on the next block's
3217 * start and the gap size.
3219 gotbno
= ap
->got
.br_startblock
;
3222 * If the gap is large relative to the piece we're
3223 * allocating, or using it gives us an invalid block
3224 * number, then just use the start of the next block
3225 * offset by our length.
3227 if (gotdiff
<= XFS_ALLOC_GAP_UNITS
* ap
->length
&&
3228 ISVALID(gotbno
- gotdiff
, gotbno
))
3230 else if (ISVALID(gotbno
- ap
->length
, gotbno
)) {
3231 gotbno
-= ap
->length
;
3232 gotdiff
+= adjust
- ap
->length
;
3236 * If the firstblock forbids it, can't use it,
3239 if (!rt
&& !nullfb
&&
3240 XFS_FSB_TO_AGNO(mp
, gotbno
) != fb_agno
)
3241 gotbno
= NULLFSBLOCK
;
3244 * No next block, just default.
3247 gotbno
= NULLFSBLOCK
;
3249 * If both valid, pick the better one, else the only good
3250 * one, else ap->blkno is already set (to 0 or the inode block).
3252 if (prevbno
!= NULLFSBLOCK
&& gotbno
!= NULLFSBLOCK
)
3253 ap
->blkno
= prevdiff
<= gotdiff
? prevbno
: gotbno
;
3254 else if (prevbno
!= NULLFSBLOCK
)
3255 ap
->blkno
= prevbno
;
3256 else if (gotbno
!= NULLFSBLOCK
)
3263 xfs_bmap_longest_free_extent(
3264 struct xfs_trans
*tp
,
3269 struct xfs_mount
*mp
= tp
->t_mountp
;
3270 struct xfs_perag
*pag
;
3271 xfs_extlen_t longest
;
3274 pag
= xfs_perag_get(mp
, ag
);
3275 if (!pag
->pagf_init
) {
3276 error
= xfs_alloc_pagf_init(mp
, tp
, ag
, XFS_ALLOC_FLAG_TRYLOCK
);
3278 /* Couldn't lock the AGF, so skip this AG. */
3279 if (error
== -EAGAIN
) {
3287 longest
= xfs_alloc_longest_free_extent(pag
,
3288 xfs_alloc_min_freelist(mp
, pag
),
3289 xfs_ag_resv_needed(pag
, XFS_AG_RESV_NONE
));
3290 if (*blen
< longest
)
3299 xfs_bmap_select_minlen(
3300 struct xfs_bmalloca
*ap
,
3301 struct xfs_alloc_arg
*args
,
3305 if (notinit
|| *blen
< ap
->minlen
) {
3307 * Since we did a BUF_TRYLOCK above, it is possible that
3308 * there is space for this request.
3310 args
->minlen
= ap
->minlen
;
3311 } else if (*blen
< args
->maxlen
) {
3313 * If the best seen length is less than the request length,
3314 * use the best as the minimum.
3316 args
->minlen
= *blen
;
3319 * Otherwise we've seen an extent as big as maxlen, use that
3322 args
->minlen
= args
->maxlen
;
3327 xfs_bmap_btalloc_nullfb(
3328 struct xfs_bmalloca
*ap
,
3329 struct xfs_alloc_arg
*args
,
3332 struct xfs_mount
*mp
= ap
->ip
->i_mount
;
3333 xfs_agnumber_t ag
, startag
;
3337 args
->type
= XFS_ALLOCTYPE_START_BNO
;
3338 args
->total
= ap
->total
;
3340 startag
= ag
= XFS_FSB_TO_AGNO(mp
, args
->fsbno
);
3341 if (startag
== NULLAGNUMBER
)
3344 while (*blen
< args
->maxlen
) {
3345 error
= xfs_bmap_longest_free_extent(args
->tp
, ag
, blen
,
3350 if (++ag
== mp
->m_sb
.sb_agcount
)
3356 xfs_bmap_select_minlen(ap
, args
, blen
, notinit
);
3361 xfs_bmap_btalloc_filestreams(
3362 struct xfs_bmalloca
*ap
,
3363 struct xfs_alloc_arg
*args
,
3366 struct xfs_mount
*mp
= ap
->ip
->i_mount
;
3371 args
->type
= XFS_ALLOCTYPE_NEAR_BNO
;
3372 args
->total
= ap
->total
;
3374 ag
= XFS_FSB_TO_AGNO(mp
, args
->fsbno
);
3375 if (ag
== NULLAGNUMBER
)
3378 error
= xfs_bmap_longest_free_extent(args
->tp
, ag
, blen
, ¬init
);
3382 if (*blen
< args
->maxlen
) {
3383 error
= xfs_filestream_new_ag(ap
, &ag
);
3387 error
= xfs_bmap_longest_free_extent(args
->tp
, ag
, blen
,
3394 xfs_bmap_select_minlen(ap
, args
, blen
, notinit
);
3397 * Set the failure fallback case to look in the selected AG as stream
3400 ap
->blkno
= args
->fsbno
= XFS_AGB_TO_FSB(mp
, ag
, 0);
3404 /* Update all inode and quota accounting for the allocation we just did. */
3406 xfs_bmap_btalloc_accounting(
3407 struct xfs_bmalloca
*ap
,
3408 struct xfs_alloc_arg
*args
)
3410 if (ap
->flags
& XFS_BMAPI_COWFORK
) {
3412 * COW fork blocks are in-core only and thus are treated as
3413 * in-core quota reservation (like delalloc blocks) even when
3414 * converted to real blocks. The quota reservation is not
3415 * accounted to disk until blocks are remapped to the data
3416 * fork. So if these blocks were previously delalloc, we
3417 * already have quota reservation and there's nothing to do
3421 xfs_mod_delalloc(ap
->ip
->i_mount
, -(int64_t)args
->len
);
3426 * Otherwise, we've allocated blocks in a hole. The transaction
3427 * has acquired in-core quota reservation for this extent.
3428 * Rather than account these as real blocks, however, we reduce
3429 * the transaction quota reservation based on the allocation.
3430 * This essentially transfers the transaction quota reservation
3431 * to that of a delalloc extent.
3433 ap
->ip
->i_delayed_blks
+= args
->len
;
3434 xfs_trans_mod_dquot_byino(ap
->tp
, ap
->ip
, XFS_TRANS_DQ_RES_BLKS
,
3439 /* data/attr fork only */
3440 ap
->ip
->i_d
.di_nblocks
+= args
->len
;
3441 xfs_trans_log_inode(ap
->tp
, ap
->ip
, XFS_ILOG_CORE
);
3443 ap
->ip
->i_delayed_blks
-= args
->len
;
3444 xfs_mod_delalloc(ap
->ip
->i_mount
, -(int64_t)args
->len
);
3446 xfs_trans_mod_dquot_byino(ap
->tp
, ap
->ip
,
3447 ap
->wasdel
? XFS_TRANS_DQ_DELBCOUNT
: XFS_TRANS_DQ_BCOUNT
,
3452 xfs_bmap_compute_alignments(
3453 struct xfs_bmalloca
*ap
,
3454 struct xfs_alloc_arg
*args
)
3456 struct xfs_mount
*mp
= args
->mp
;
3457 xfs_extlen_t align
= 0; /* minimum allocation alignment */
3458 int stripe_align
= 0;
3460 /* stripe alignment for allocation is determined by mount parameters */
3461 if (mp
->m_swidth
&& (mp
->m_flags
& XFS_MOUNT_SWALLOC
))
3462 stripe_align
= mp
->m_swidth
;
3463 else if (mp
->m_dalign
)
3464 stripe_align
= mp
->m_dalign
;
3466 if (ap
->flags
& XFS_BMAPI_COWFORK
)
3467 align
= xfs_get_cowextsz_hint(ap
->ip
);
3468 else if (ap
->datatype
& XFS_ALLOC_USERDATA
)
3469 align
= xfs_get_extsz_hint(ap
->ip
);
3471 if (xfs_bmap_extsize_align(mp
, &ap
->got
, &ap
->prev
, align
, 0,
3472 ap
->eof
, 0, ap
->conv
, &ap
->offset
,
3478 /* apply extent size hints if obtained earlier */
3481 div_u64_rem(ap
->offset
, args
->prod
, &args
->mod
);
3483 args
->mod
= args
->prod
- args
->mod
;
3484 } else if (mp
->m_sb
.sb_blocksize
>= PAGE_SIZE
) {
3488 args
->prod
= PAGE_SIZE
>> mp
->m_sb
.sb_blocklog
;
3489 div_u64_rem(ap
->offset
, args
->prod
, &args
->mod
);
3491 args
->mod
= args
->prod
- args
->mod
;
3494 return stripe_align
;
3498 xfs_bmap_process_allocated_extent(
3499 struct xfs_bmalloca
*ap
,
3500 struct xfs_alloc_arg
*args
,
3501 xfs_fileoff_t orig_offset
,
3502 xfs_extlen_t orig_length
)
3506 nullfb
= ap
->tp
->t_firstblock
== NULLFSBLOCK
;
3509 * check the allocation happened at the same or higher AG than
3510 * the first block that was allocated.
3513 XFS_FSB_TO_AGNO(args
->mp
, ap
->tp
->t_firstblock
) <=
3514 XFS_FSB_TO_AGNO(args
->mp
, args
->fsbno
));
3516 ap
->blkno
= args
->fsbno
;
3518 ap
->tp
->t_firstblock
= args
->fsbno
;
3519 ap
->length
= args
->len
;
3521 * If the extent size hint is active, we tried to round the
3522 * caller's allocation request offset down to extsz and the
3523 * length up to another extsz boundary. If we found a free
3524 * extent we mapped it in starting at this new offset. If the
3525 * newly mapped space isn't long enough to cover any of the
3526 * range of offsets that was originally requested, move the
3527 * mapping up so that we can fill as much of the caller's
3528 * original request as possible. Free space is apparently
3529 * very fragmented so we're unlikely to be able to satisfy the
3532 if (ap
->length
<= orig_length
)
3533 ap
->offset
= orig_offset
;
3534 else if (ap
->offset
+ ap
->length
< orig_offset
+ orig_length
)
3535 ap
->offset
= orig_offset
+ orig_length
- ap
->length
;
3536 xfs_bmap_btalloc_accounting(ap
, args
);
3541 xfs_bmap_exact_minlen_extent_alloc(
3542 struct xfs_bmalloca
*ap
)
3544 struct xfs_mount
*mp
= ap
->ip
->i_mount
;
3545 struct xfs_alloc_arg args
= { .tp
= ap
->tp
, .mp
= mp
};
3546 xfs_fileoff_t orig_offset
;
3547 xfs_extlen_t orig_length
;
3552 if (ap
->minlen
!= 1) {
3553 ap
->blkno
= NULLFSBLOCK
;
3558 orig_offset
= ap
->offset
;
3559 orig_length
= ap
->length
;
3561 args
.alloc_minlen_only
= 1;
3563 xfs_bmap_compute_alignments(ap
, &args
);
3565 if (ap
->tp
->t_firstblock
== NULLFSBLOCK
) {
3567 * Unlike the longest extent available in an AG, we don't track
3568 * the length of an AG's shortest extent.
3569 * XFS_ERRTAG_BMAP_ALLOC_MINLEN_EXTENT is a debug only knob and
3570 * hence we can afford to start traversing from the 0th AG since
3571 * we need not be concerned about a drop in performance in
3572 * "debug only" code paths.
3574 ap
->blkno
= XFS_AGB_TO_FSB(mp
, 0, 0);
3576 ap
->blkno
= ap
->tp
->t_firstblock
;
3579 args
.fsbno
= ap
->blkno
;
3580 args
.oinfo
= XFS_RMAP_OINFO_SKIP_UPDATE
;
3581 args
.type
= XFS_ALLOCTYPE_FIRST_AG
;
3582 args
.total
= args
.minlen
= args
.maxlen
= ap
->minlen
;
3585 args
.minalignslop
= 0;
3587 args
.minleft
= ap
->minleft
;
3588 args
.wasdel
= ap
->wasdel
;
3589 args
.resv
= XFS_AG_RESV_NONE
;
3590 args
.datatype
= ap
->datatype
;
3592 error
= xfs_alloc_vextent(&args
);
3596 if (args
.fsbno
!= NULLFSBLOCK
) {
3597 xfs_bmap_process_allocated_extent(ap
, &args
, orig_offset
,
3600 ap
->blkno
= NULLFSBLOCK
;
3608 #define xfs_bmap_exact_minlen_extent_alloc(bma) (-EFSCORRUPTED)
3614 struct xfs_bmalloca
*ap
)
3616 struct xfs_mount
*mp
= ap
->ip
->i_mount
;
3617 struct xfs_alloc_arg args
= { .tp
= ap
->tp
, .mp
= mp
};
3618 xfs_alloctype_t atype
= 0;
3619 xfs_agnumber_t fb_agno
; /* ag number of ap->firstblock */
3621 xfs_fileoff_t orig_offset
;
3622 xfs_extlen_t orig_length
;
3624 xfs_extlen_t nextminlen
= 0;
3625 int nullfb
; /* true if ap->firstblock isn't set */
3632 orig_offset
= ap
->offset
;
3633 orig_length
= ap
->length
;
3635 stripe_align
= xfs_bmap_compute_alignments(ap
, &args
);
3637 nullfb
= ap
->tp
->t_firstblock
== NULLFSBLOCK
;
3638 fb_agno
= nullfb
? NULLAGNUMBER
: XFS_FSB_TO_AGNO(mp
,
3639 ap
->tp
->t_firstblock
);
3641 if ((ap
->datatype
& XFS_ALLOC_USERDATA
) &&
3642 xfs_inode_is_filestream(ap
->ip
)) {
3643 ag
= xfs_filestream_lookup_ag(ap
->ip
);
3644 ag
= (ag
!= NULLAGNUMBER
) ? ag
: 0;
3645 ap
->blkno
= XFS_AGB_TO_FSB(mp
, ag
, 0);
3647 ap
->blkno
= XFS_INO_TO_FSB(mp
, ap
->ip
->i_ino
);
3650 ap
->blkno
= ap
->tp
->t_firstblock
;
3652 xfs_bmap_adjacent(ap
);
3655 * If allowed, use ap->blkno; otherwise must use firstblock since
3656 * it's in the right allocation group.
3658 if (nullfb
|| XFS_FSB_TO_AGNO(mp
, ap
->blkno
) == fb_agno
)
3661 ap
->blkno
= ap
->tp
->t_firstblock
;
3663 * Normal allocation, done through xfs_alloc_vextent.
3665 tryagain
= isaligned
= 0;
3666 args
.fsbno
= ap
->blkno
;
3667 args
.oinfo
= XFS_RMAP_OINFO_SKIP_UPDATE
;
3669 /* Trim the allocation back to the maximum an AG can fit. */
3670 args
.maxlen
= min(ap
->length
, mp
->m_ag_max_usable
);
3674 * Search for an allocation group with a single extent large
3675 * enough for the request. If one isn't found, then adjust
3676 * the minimum allocation size to the largest space found.
3678 if ((ap
->datatype
& XFS_ALLOC_USERDATA
) &&
3679 xfs_inode_is_filestream(ap
->ip
))
3680 error
= xfs_bmap_btalloc_filestreams(ap
, &args
, &blen
);
3682 error
= xfs_bmap_btalloc_nullfb(ap
, &args
, &blen
);
3685 } else if (ap
->tp
->t_flags
& XFS_TRANS_LOWMODE
) {
3686 if (xfs_inode_is_filestream(ap
->ip
))
3687 args
.type
= XFS_ALLOCTYPE_FIRST_AG
;
3689 args
.type
= XFS_ALLOCTYPE_START_BNO
;
3690 args
.total
= args
.minlen
= ap
->minlen
;
3692 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
3693 args
.total
= ap
->total
;
3694 args
.minlen
= ap
->minlen
;
3698 * If we are not low on available data blocks, and the underlying
3699 * logical volume manager is a stripe, and the file offset is zero then
3700 * try to allocate data blocks on stripe unit boundary. NOTE: ap->aeof
3701 * is only set if the allocation length is >= the stripe unit and the
3702 * allocation offset is at the end of file.
3704 if (!(ap
->tp
->t_flags
& XFS_TRANS_LOWMODE
) && ap
->aeof
) {
3706 args
.alignment
= stripe_align
;
3710 * Adjust minlen to try and preserve alignment if we
3711 * can't guarantee an aligned maxlen extent.
3713 if (blen
> args
.alignment
&&
3714 blen
<= args
.maxlen
+ args
.alignment
)
3715 args
.minlen
= blen
- args
.alignment
;
3716 args
.minalignslop
= 0;
3719 * First try an exact bno allocation.
3720 * If it fails then do a near or start bno
3721 * allocation with alignment turned on.
3725 args
.type
= XFS_ALLOCTYPE_THIS_BNO
;
3728 * Compute the minlen+alignment for the
3729 * next case. Set slop so that the value
3730 * of minlen+alignment+slop doesn't go up
3731 * between the calls.
3733 if (blen
> stripe_align
&& blen
<= args
.maxlen
)
3734 nextminlen
= blen
- stripe_align
;
3736 nextminlen
= args
.minlen
;
3737 if (nextminlen
+ stripe_align
> args
.minlen
+ 1)
3739 nextminlen
+ stripe_align
-
3742 args
.minalignslop
= 0;
3746 args
.minalignslop
= 0;
3748 args
.minleft
= ap
->minleft
;
3749 args
.wasdel
= ap
->wasdel
;
3750 args
.resv
= XFS_AG_RESV_NONE
;
3751 args
.datatype
= ap
->datatype
;
3753 error
= xfs_alloc_vextent(&args
);
3757 if (tryagain
&& args
.fsbno
== NULLFSBLOCK
) {
3759 * Exact allocation failed. Now try with alignment
3763 args
.fsbno
= ap
->blkno
;
3764 args
.alignment
= stripe_align
;
3765 args
.minlen
= nextminlen
;
3766 args
.minalignslop
= 0;
3768 if ((error
= xfs_alloc_vextent(&args
)))
3771 if (isaligned
&& args
.fsbno
== NULLFSBLOCK
) {
3773 * allocation failed, so turn off alignment and
3777 args
.fsbno
= ap
->blkno
;
3779 if ((error
= xfs_alloc_vextent(&args
)))
3782 if (args
.fsbno
== NULLFSBLOCK
&& nullfb
&&
3783 args
.minlen
> ap
->minlen
) {
3784 args
.minlen
= ap
->minlen
;
3785 args
.type
= XFS_ALLOCTYPE_START_BNO
;
3786 args
.fsbno
= ap
->blkno
;
3787 if ((error
= xfs_alloc_vextent(&args
)))
3790 if (args
.fsbno
== NULLFSBLOCK
&& nullfb
) {
3792 args
.type
= XFS_ALLOCTYPE_FIRST_AG
;
3793 args
.total
= ap
->minlen
;
3794 if ((error
= xfs_alloc_vextent(&args
)))
3796 ap
->tp
->t_flags
|= XFS_TRANS_LOWMODE
;
3799 if (args
.fsbno
!= NULLFSBLOCK
) {
3800 xfs_bmap_process_allocated_extent(ap
, &args
, orig_offset
,
3803 ap
->blkno
= NULLFSBLOCK
;
3809 /* Trim extent to fit a logical block range. */
3812 struct xfs_bmbt_irec
*irec
,
3816 xfs_fileoff_t distance
;
3817 xfs_fileoff_t end
= bno
+ len
;
3819 if (irec
->br_startoff
+ irec
->br_blockcount
<= bno
||
3820 irec
->br_startoff
>= end
) {
3821 irec
->br_blockcount
= 0;
3825 if (irec
->br_startoff
< bno
) {
3826 distance
= bno
- irec
->br_startoff
;
3827 if (isnullstartblock(irec
->br_startblock
))
3828 irec
->br_startblock
= DELAYSTARTBLOCK
;
3829 if (irec
->br_startblock
!= DELAYSTARTBLOCK
&&
3830 irec
->br_startblock
!= HOLESTARTBLOCK
)
3831 irec
->br_startblock
+= distance
;
3832 irec
->br_startoff
+= distance
;
3833 irec
->br_blockcount
-= distance
;
3836 if (end
< irec
->br_startoff
+ irec
->br_blockcount
) {
3837 distance
= irec
->br_startoff
+ irec
->br_blockcount
- end
;
3838 irec
->br_blockcount
-= distance
;
3843 * Trim the returned map to the required bounds
3847 struct xfs_bmbt_irec
*mval
,
3848 struct xfs_bmbt_irec
*got
,
3856 if ((flags
& XFS_BMAPI_ENTIRE
) ||
3857 got
->br_startoff
+ got
->br_blockcount
<= obno
) {
3859 if (isnullstartblock(got
->br_startblock
))
3860 mval
->br_startblock
= DELAYSTARTBLOCK
;
3866 ASSERT((*bno
>= obno
) || (n
== 0));
3868 mval
->br_startoff
= *bno
;
3869 if (isnullstartblock(got
->br_startblock
))
3870 mval
->br_startblock
= DELAYSTARTBLOCK
;
3872 mval
->br_startblock
= got
->br_startblock
+
3873 (*bno
- got
->br_startoff
);
3875 * Return the minimum of what we got and what we asked for for
3876 * the length. We can use the len variable here because it is
3877 * modified below and we could have been there before coming
3878 * here if the first part of the allocation didn't overlap what
3881 mval
->br_blockcount
= XFS_FILBLKS_MIN(end
- *bno
,
3882 got
->br_blockcount
- (*bno
- got
->br_startoff
));
3883 mval
->br_state
= got
->br_state
;
3884 ASSERT(mval
->br_blockcount
<= len
);
3889 * Update and validate the extent map to return
3892 xfs_bmapi_update_map(
3893 struct xfs_bmbt_irec
**map
,
3901 xfs_bmbt_irec_t
*mval
= *map
;
3903 ASSERT((flags
& XFS_BMAPI_ENTIRE
) ||
3904 ((mval
->br_startoff
+ mval
->br_blockcount
) <= end
));
3905 ASSERT((flags
& XFS_BMAPI_ENTIRE
) || (mval
->br_blockcount
<= *len
) ||
3906 (mval
->br_startoff
< obno
));
3908 *bno
= mval
->br_startoff
+ mval
->br_blockcount
;
3910 if (*n
> 0 && mval
->br_startoff
== mval
[-1].br_startoff
) {
3911 /* update previous map with new information */
3912 ASSERT(mval
->br_startblock
== mval
[-1].br_startblock
);
3913 ASSERT(mval
->br_blockcount
> mval
[-1].br_blockcount
);
3914 ASSERT(mval
->br_state
== mval
[-1].br_state
);
3915 mval
[-1].br_blockcount
= mval
->br_blockcount
;
3916 mval
[-1].br_state
= mval
->br_state
;
3917 } else if (*n
> 0 && mval
->br_startblock
!= DELAYSTARTBLOCK
&&
3918 mval
[-1].br_startblock
!= DELAYSTARTBLOCK
&&
3919 mval
[-1].br_startblock
!= HOLESTARTBLOCK
&&
3920 mval
->br_startblock
== mval
[-1].br_startblock
+
3921 mval
[-1].br_blockcount
&&
3922 mval
[-1].br_state
== mval
->br_state
) {
3923 ASSERT(mval
->br_startoff
==
3924 mval
[-1].br_startoff
+ mval
[-1].br_blockcount
);
3925 mval
[-1].br_blockcount
+= mval
->br_blockcount
;
3926 } else if (*n
> 0 &&
3927 mval
->br_startblock
== DELAYSTARTBLOCK
&&
3928 mval
[-1].br_startblock
== DELAYSTARTBLOCK
&&
3929 mval
->br_startoff
==
3930 mval
[-1].br_startoff
+ mval
[-1].br_blockcount
) {
3931 mval
[-1].br_blockcount
+= mval
->br_blockcount
;
3932 mval
[-1].br_state
= mval
->br_state
;
3933 } else if (!((*n
== 0) &&
3934 ((mval
->br_startoff
+ mval
->br_blockcount
) <=
3943 * Map file blocks to filesystem blocks without allocation.
3947 struct xfs_inode
*ip
,
3950 struct xfs_bmbt_irec
*mval
,
3954 struct xfs_mount
*mp
= ip
->i_mount
;
3955 int whichfork
= xfs_bmapi_whichfork(flags
);
3956 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
3957 struct xfs_bmbt_irec got
;
3960 struct xfs_iext_cursor icur
;
3966 ASSERT(!(flags
& ~(XFS_BMAPI_ATTRFORK
| XFS_BMAPI_ENTIRE
)));
3967 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_SHARED
|XFS_ILOCK_EXCL
));
3969 if (WARN_ON_ONCE(!ifp
))
3970 return -EFSCORRUPTED
;
3972 if (XFS_IS_CORRUPT(mp
, !xfs_ifork_has_extents(ifp
)) ||
3973 XFS_TEST_ERROR(false, mp
, XFS_ERRTAG_BMAPIFORMAT
))
3974 return -EFSCORRUPTED
;
3976 if (XFS_FORCED_SHUTDOWN(mp
))
3979 XFS_STATS_INC(mp
, xs_blk_mapr
);
3981 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
3982 error
= xfs_iread_extents(NULL
, ip
, whichfork
);
3987 if (!xfs_iext_lookup_extent(ip
, ifp
, bno
, &icur
, &got
))
3992 while (bno
< end
&& n
< *nmap
) {
3993 /* Reading past eof, act as though there's a hole up to end. */
3995 got
.br_startoff
= end
;
3996 if (got
.br_startoff
> bno
) {
3997 /* Reading in a hole. */
3998 mval
->br_startoff
= bno
;
3999 mval
->br_startblock
= HOLESTARTBLOCK
;
4000 mval
->br_blockcount
=
4001 XFS_FILBLKS_MIN(len
, got
.br_startoff
- bno
);
4002 mval
->br_state
= XFS_EXT_NORM
;
4003 bno
+= mval
->br_blockcount
;
4004 len
-= mval
->br_blockcount
;
4010 /* set up the extent map to return. */
4011 xfs_bmapi_trim_map(mval
, &got
, &bno
, len
, obno
, end
, n
, flags
);
4012 xfs_bmapi_update_map(&mval
, &bno
, &len
, obno
, end
, &n
, flags
);
4014 /* If we're done, stop now. */
4015 if (bno
>= end
|| n
>= *nmap
)
4018 /* Else go on to the next record. */
4019 if (!xfs_iext_next_extent(ifp
, &icur
, &got
))
4027 * Add a delayed allocation extent to an inode. Blocks are reserved from the
4028 * global pool and the extent inserted into the inode in-core extent tree.
4030 * On entry, got refers to the first extent beyond the offset of the extent to
4031 * allocate or eof is specified if no such extent exists. On return, got refers
4032 * to the extent record that was inserted to the inode fork.
4034 * Note that the allocated extent may have been merged with contiguous extents
4035 * during insertion into the inode fork. Thus, got does not reflect the current
4036 * state of the inode fork on return. If necessary, the caller can use lastx to
4037 * look up the updated record in the inode fork.
4040 xfs_bmapi_reserve_delalloc(
4041 struct xfs_inode
*ip
,
4045 xfs_filblks_t prealloc
,
4046 struct xfs_bmbt_irec
*got
,
4047 struct xfs_iext_cursor
*icur
,
4050 struct xfs_mount
*mp
= ip
->i_mount
;
4051 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4053 xfs_extlen_t indlen
;
4055 xfs_fileoff_t aoff
= off
;
4058 * Cap the alloc length. Keep track of prealloc so we know whether to
4059 * tag the inode before we return.
4061 alen
= XFS_FILBLKS_MIN(len
+ prealloc
, MAXEXTLEN
);
4063 alen
= XFS_FILBLKS_MIN(alen
, got
->br_startoff
- aoff
);
4064 if (prealloc
&& alen
>= len
)
4065 prealloc
= alen
- len
;
4067 /* Figure out the extent size, adjust alen */
4068 if (whichfork
== XFS_COW_FORK
) {
4069 struct xfs_bmbt_irec prev
;
4070 xfs_extlen_t extsz
= xfs_get_cowextsz_hint(ip
);
4072 if (!xfs_iext_peek_prev_extent(ifp
, icur
, &prev
))
4073 prev
.br_startoff
= NULLFILEOFF
;
4075 error
= xfs_bmap_extsize_align(mp
, got
, &prev
, extsz
, 0, eof
,
4076 1, 0, &aoff
, &alen
);
4081 * Make a transaction-less quota reservation for delayed allocation
4082 * blocks. This number gets adjusted later. We return if we haven't
4083 * allocated blocks already inside this loop.
4085 error
= xfs_quota_reserve_blkres(ip
, alen
);
4090 * Split changing sb for alen and indlen since they could be coming
4091 * from different places.
4093 indlen
= (xfs_extlen_t
)xfs_bmap_worst_indlen(ip
, alen
);
4096 error
= xfs_mod_fdblocks(mp
, -((int64_t)alen
), false);
4098 goto out_unreserve_quota
;
4100 error
= xfs_mod_fdblocks(mp
, -((int64_t)indlen
), false);
4102 goto out_unreserve_blocks
;
4105 ip
->i_delayed_blks
+= alen
;
4106 xfs_mod_delalloc(ip
->i_mount
, alen
+ indlen
);
4108 got
->br_startoff
= aoff
;
4109 got
->br_startblock
= nullstartblock(indlen
);
4110 got
->br_blockcount
= alen
;
4111 got
->br_state
= XFS_EXT_NORM
;
4113 xfs_bmap_add_extent_hole_delay(ip
, whichfork
, icur
, got
);
4116 * Tag the inode if blocks were preallocated. Note that COW fork
4117 * preallocation can occur at the start or end of the extent, even when
4118 * prealloc == 0, so we must also check the aligned offset and length.
4120 if (whichfork
== XFS_DATA_FORK
&& prealloc
)
4121 xfs_inode_set_eofblocks_tag(ip
);
4122 if (whichfork
== XFS_COW_FORK
&& (prealloc
|| aoff
< off
|| alen
> len
))
4123 xfs_inode_set_cowblocks_tag(ip
);
4127 out_unreserve_blocks
:
4128 xfs_mod_fdblocks(mp
, alen
, false);
4129 out_unreserve_quota
:
4130 if (XFS_IS_QUOTA_ON(mp
))
4131 xfs_quota_unreserve_blkres(ip
, alen
);
4136 xfs_bmap_alloc_userdata(
4137 struct xfs_bmalloca
*bma
)
4139 struct xfs_mount
*mp
= bma
->ip
->i_mount
;
4140 int whichfork
= xfs_bmapi_whichfork(bma
->flags
);
4144 * Set the data type being allocated. For the data fork, the first data
4145 * in the file is treated differently to all other allocations. For the
4146 * attribute fork, we only need to ensure the allocated range is not on
4149 bma
->datatype
= XFS_ALLOC_NOBUSY
;
4150 if (whichfork
== XFS_DATA_FORK
) {
4151 bma
->datatype
|= XFS_ALLOC_USERDATA
;
4152 if (bma
->offset
== 0)
4153 bma
->datatype
|= XFS_ALLOC_INITIAL_USER_DATA
;
4155 if (mp
->m_dalign
&& bma
->length
>= mp
->m_dalign
) {
4156 error
= xfs_bmap_isaeof(bma
, whichfork
);
4161 if (XFS_IS_REALTIME_INODE(bma
->ip
))
4162 return xfs_bmap_rtalloc(bma
);
4165 if (unlikely(XFS_TEST_ERROR(false, mp
,
4166 XFS_ERRTAG_BMAP_ALLOC_MINLEN_EXTENT
)))
4167 return xfs_bmap_exact_minlen_extent_alloc(bma
);
4169 return xfs_bmap_btalloc(bma
);
4174 struct xfs_bmalloca
*bma
)
4176 struct xfs_mount
*mp
= bma
->ip
->i_mount
;
4177 int whichfork
= xfs_bmapi_whichfork(bma
->flags
);
4178 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
4179 int tmp_logflags
= 0;
4182 ASSERT(bma
->length
> 0);
4185 * For the wasdelay case, we could also just allocate the stuff asked
4186 * for in this bmap call but that wouldn't be as good.
4189 bma
->length
= (xfs_extlen_t
)bma
->got
.br_blockcount
;
4190 bma
->offset
= bma
->got
.br_startoff
;
4191 if (!xfs_iext_peek_prev_extent(ifp
, &bma
->icur
, &bma
->prev
))
4192 bma
->prev
.br_startoff
= NULLFILEOFF
;
4194 bma
->length
= XFS_FILBLKS_MIN(bma
->length
, MAXEXTLEN
);
4196 bma
->length
= XFS_FILBLKS_MIN(bma
->length
,
4197 bma
->got
.br_startoff
- bma
->offset
);
4200 if (bma
->flags
& XFS_BMAPI_CONTIG
)
4201 bma
->minlen
= bma
->length
;
4205 if (bma
->flags
& XFS_BMAPI_METADATA
) {
4206 if (unlikely(XFS_TEST_ERROR(false, mp
,
4207 XFS_ERRTAG_BMAP_ALLOC_MINLEN_EXTENT
)))
4208 error
= xfs_bmap_exact_minlen_extent_alloc(bma
);
4210 error
= xfs_bmap_btalloc(bma
);
4212 error
= xfs_bmap_alloc_userdata(bma
);
4214 if (error
|| bma
->blkno
== NULLFSBLOCK
)
4217 if (bma
->flags
& XFS_BMAPI_ZERO
) {
4218 error
= xfs_zero_extent(bma
->ip
, bma
->blkno
, bma
->length
);
4223 if ((ifp
->if_flags
& XFS_IFBROOT
) && !bma
->cur
)
4224 bma
->cur
= xfs_bmbt_init_cursor(mp
, bma
->tp
, bma
->ip
, whichfork
);
4226 * Bump the number of extents we've allocated
4232 bma
->cur
->bc_ino
.flags
=
4233 bma
->wasdel
? XFS_BTCUR_BMBT_WASDEL
: 0;
4235 bma
->got
.br_startoff
= bma
->offset
;
4236 bma
->got
.br_startblock
= bma
->blkno
;
4237 bma
->got
.br_blockcount
= bma
->length
;
4238 bma
->got
.br_state
= XFS_EXT_NORM
;
4240 if (bma
->flags
& XFS_BMAPI_PREALLOC
)
4241 bma
->got
.br_state
= XFS_EXT_UNWRITTEN
;
4244 error
= xfs_bmap_add_extent_delay_real(bma
, whichfork
);
4246 error
= xfs_bmap_add_extent_hole_real(bma
->tp
, bma
->ip
,
4247 whichfork
, &bma
->icur
, &bma
->cur
, &bma
->got
,
4248 &bma
->logflags
, bma
->flags
);
4250 bma
->logflags
|= tmp_logflags
;
4255 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4256 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4257 * the neighbouring ones.
4259 xfs_iext_get_extent(ifp
, &bma
->icur
, &bma
->got
);
4261 ASSERT(bma
->got
.br_startoff
<= bma
->offset
);
4262 ASSERT(bma
->got
.br_startoff
+ bma
->got
.br_blockcount
>=
4263 bma
->offset
+ bma
->length
);
4264 ASSERT(bma
->got
.br_state
== XFS_EXT_NORM
||
4265 bma
->got
.br_state
== XFS_EXT_UNWRITTEN
);
4270 xfs_bmapi_convert_unwritten(
4271 struct xfs_bmalloca
*bma
,
4272 struct xfs_bmbt_irec
*mval
,
4276 int whichfork
= xfs_bmapi_whichfork(flags
);
4277 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
4278 int tmp_logflags
= 0;
4281 /* check if we need to do unwritten->real conversion */
4282 if (mval
->br_state
== XFS_EXT_UNWRITTEN
&&
4283 (flags
& XFS_BMAPI_PREALLOC
))
4286 /* check if we need to do real->unwritten conversion */
4287 if (mval
->br_state
== XFS_EXT_NORM
&&
4288 (flags
& (XFS_BMAPI_PREALLOC
| XFS_BMAPI_CONVERT
)) !=
4289 (XFS_BMAPI_PREALLOC
| XFS_BMAPI_CONVERT
))
4293 * Modify (by adding) the state flag, if writing.
4295 ASSERT(mval
->br_blockcount
<= len
);
4296 if ((ifp
->if_flags
& XFS_IFBROOT
) && !bma
->cur
) {
4297 bma
->cur
= xfs_bmbt_init_cursor(bma
->ip
->i_mount
, bma
->tp
,
4298 bma
->ip
, whichfork
);
4300 mval
->br_state
= (mval
->br_state
== XFS_EXT_UNWRITTEN
)
4301 ? XFS_EXT_NORM
: XFS_EXT_UNWRITTEN
;
4304 * Before insertion into the bmbt, zero the range being converted
4307 if (flags
& XFS_BMAPI_ZERO
) {
4308 error
= xfs_zero_extent(bma
->ip
, mval
->br_startblock
,
4309 mval
->br_blockcount
);
4314 error
= xfs_bmap_add_extent_unwritten_real(bma
->tp
, bma
->ip
, whichfork
,
4315 &bma
->icur
, &bma
->cur
, mval
, &tmp_logflags
);
4317 * Log the inode core unconditionally in the unwritten extent conversion
4318 * path because the conversion might not have done so (e.g., if the
4319 * extent count hasn't changed). We need to make sure the inode is dirty
4320 * in the transaction for the sake of fsync(), even if nothing has
4321 * changed, because fsync() will not force the log for this transaction
4322 * unless it sees the inode pinned.
4324 * Note: If we're only converting cow fork extents, there aren't
4325 * any on-disk updates to make, so we don't need to log anything.
4327 if (whichfork
!= XFS_COW_FORK
)
4328 bma
->logflags
|= tmp_logflags
| XFS_ILOG_CORE
;
4333 * Update our extent pointer, given that
4334 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4335 * of the neighbouring ones.
4337 xfs_iext_get_extent(ifp
, &bma
->icur
, &bma
->got
);
4340 * We may have combined previously unwritten space with written space,
4341 * so generate another request.
4343 if (mval
->br_blockcount
< len
)
4348 static inline xfs_extlen_t
4350 struct xfs_trans
*tp
,
4351 struct xfs_inode
*ip
,
4354 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, fork
);
4356 if (tp
&& tp
->t_firstblock
!= NULLFSBLOCK
)
4358 if (ifp
->if_format
!= XFS_DINODE_FMT_BTREE
)
4360 return be16_to_cpu(ifp
->if_broot
->bb_level
) + 1;
4364 * Log whatever the flags say, even if error. Otherwise we might miss detecting
4365 * a case where the data is changed, there's an error, and it's not logged so we
4366 * don't shutdown when we should. Don't bother logging extents/btree changes if
4367 * we converted to the other format.
4371 struct xfs_bmalloca
*bma
,
4375 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
4377 if ((bma
->logflags
& xfs_ilog_fext(whichfork
)) &&
4378 ifp
->if_format
!= XFS_DINODE_FMT_EXTENTS
)
4379 bma
->logflags
&= ~xfs_ilog_fext(whichfork
);
4380 else if ((bma
->logflags
& xfs_ilog_fbroot(whichfork
)) &&
4381 ifp
->if_format
!= XFS_DINODE_FMT_BTREE
)
4382 bma
->logflags
&= ~xfs_ilog_fbroot(whichfork
);
4385 xfs_trans_log_inode(bma
->tp
, bma
->ip
, bma
->logflags
);
4387 xfs_btree_del_cursor(bma
->cur
, error
);
4391 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4392 * extent state if necessary. Details behaviour is controlled by the flags
4393 * parameter. Only allocates blocks from a single allocation group, to avoid
4398 struct xfs_trans
*tp
, /* transaction pointer */
4399 struct xfs_inode
*ip
, /* incore inode */
4400 xfs_fileoff_t bno
, /* starting file offs. mapped */
4401 xfs_filblks_t len
, /* length to map in file */
4402 int flags
, /* XFS_BMAPI_... */
4403 xfs_extlen_t total
, /* total blocks needed */
4404 struct xfs_bmbt_irec
*mval
, /* output: map values */
4405 int *nmap
) /* i/o: mval size/count */
4407 struct xfs_bmalloca bma
= {
4412 struct xfs_mount
*mp
= ip
->i_mount
;
4413 int whichfork
= xfs_bmapi_whichfork(flags
);
4414 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4415 xfs_fileoff_t end
; /* end of mapped file region */
4416 bool eof
= false; /* after the end of extents */
4417 int error
; /* error return */
4418 int n
; /* current extent index */
4419 xfs_fileoff_t obno
; /* old block number (offset) */
4422 xfs_fileoff_t orig_bno
; /* original block number value */
4423 int orig_flags
; /* original flags arg value */
4424 xfs_filblks_t orig_len
; /* original value of len arg */
4425 struct xfs_bmbt_irec
*orig_mval
; /* original value of mval */
4426 int orig_nmap
; /* original value of *nmap */
4436 ASSERT(*nmap
<= XFS_BMAP_MAX_NMAP
);
4439 ASSERT(ifp
->if_format
!= XFS_DINODE_FMT_LOCAL
);
4440 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
4441 ASSERT(!(flags
& XFS_BMAPI_REMAP
));
4443 /* zeroing is for currently only for data extents, not metadata */
4444 ASSERT((flags
& (XFS_BMAPI_METADATA
| XFS_BMAPI_ZERO
)) !=
4445 (XFS_BMAPI_METADATA
| XFS_BMAPI_ZERO
));
4447 * we can allocate unwritten extents or pre-zero allocated blocks,
4448 * but it makes no sense to do both at once. This would result in
4449 * zeroing the unwritten extent twice, but it still being an
4450 * unwritten extent....
4452 ASSERT((flags
& (XFS_BMAPI_PREALLOC
| XFS_BMAPI_ZERO
)) !=
4453 (XFS_BMAPI_PREALLOC
| XFS_BMAPI_ZERO
));
4455 if (XFS_IS_CORRUPT(mp
, !xfs_ifork_has_extents(ifp
)) ||
4456 XFS_TEST_ERROR(false, mp
, XFS_ERRTAG_BMAPIFORMAT
)) {
4457 return -EFSCORRUPTED
;
4460 if (XFS_FORCED_SHUTDOWN(mp
))
4463 XFS_STATS_INC(mp
, xs_blk_mapw
);
4465 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
4466 error
= xfs_iread_extents(tp
, ip
, whichfork
);
4471 if (!xfs_iext_lookup_extent(ip
, ifp
, bno
, &bma
.icur
, &bma
.got
))
4473 if (!xfs_iext_peek_prev_extent(ifp
, &bma
.icur
, &bma
.prev
))
4474 bma
.prev
.br_startoff
= NULLFILEOFF
;
4475 bma
.minleft
= xfs_bmapi_minleft(tp
, ip
, whichfork
);
4480 while (bno
< end
&& n
< *nmap
) {
4481 bool need_alloc
= false, wasdelay
= false;
4483 /* in hole or beyond EOF? */
4484 if (eof
|| bma
.got
.br_startoff
> bno
) {
4486 * CoW fork conversions should /never/ hit EOF or
4487 * holes. There should always be something for us
4490 ASSERT(!((flags
& XFS_BMAPI_CONVERT
) &&
4491 (flags
& XFS_BMAPI_COWFORK
)));
4494 } else if (isnullstartblock(bma
.got
.br_startblock
)) {
4499 * First, deal with the hole before the allocated space
4500 * that we found, if any.
4502 if (need_alloc
|| wasdelay
) {
4504 bma
.conv
= !!(flags
& XFS_BMAPI_CONVERT
);
4505 bma
.wasdel
= wasdelay
;
4510 * There's a 32/64 bit type mismatch between the
4511 * allocation length request (which can be 64 bits in
4512 * length) and the bma length request, which is
4513 * xfs_extlen_t and therefore 32 bits. Hence we have to
4514 * check for 32-bit overflows and handle them here.
4516 if (len
> (xfs_filblks_t
)MAXEXTLEN
)
4517 bma
.length
= MAXEXTLEN
;
4522 ASSERT(bma
.length
> 0);
4523 error
= xfs_bmapi_allocate(&bma
);
4526 if (bma
.blkno
== NULLFSBLOCK
)
4530 * If this is a CoW allocation, record the data in
4531 * the refcount btree for orphan recovery.
4533 if (whichfork
== XFS_COW_FORK
)
4534 xfs_refcount_alloc_cow_extent(tp
, bma
.blkno
,
4538 /* Deal with the allocated space we found. */
4539 xfs_bmapi_trim_map(mval
, &bma
.got
, &bno
, len
, obno
,
4542 /* Execute unwritten extent conversion if necessary */
4543 error
= xfs_bmapi_convert_unwritten(&bma
, mval
, len
, flags
);
4544 if (error
== -EAGAIN
)
4549 /* update the extent map to return */
4550 xfs_bmapi_update_map(&mval
, &bno
, &len
, obno
, end
, &n
, flags
);
4553 * If we're done, stop now. Stop when we've allocated
4554 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4555 * the transaction may get too big.
4557 if (bno
>= end
|| n
>= *nmap
|| bma
.nallocs
>= *nmap
)
4560 /* Else go on to the next record. */
4562 if (!xfs_iext_next_extent(ifp
, &bma
.icur
, &bma
.got
))
4567 error
= xfs_bmap_btree_to_extents(tp
, ip
, bma
.cur
, &bma
.logflags
,
4572 ASSERT(ifp
->if_format
!= XFS_DINODE_FMT_BTREE
||
4573 ifp
->if_nextents
> XFS_IFORK_MAXEXT(ip
, whichfork
));
4574 xfs_bmapi_finish(&bma
, whichfork
, 0);
4575 xfs_bmap_validate_ret(orig_bno
, orig_len
, orig_flags
, orig_mval
,
4579 xfs_bmapi_finish(&bma
, whichfork
, error
);
4584 * Convert an existing delalloc extent to real blocks based on file offset. This
4585 * attempts to allocate the entire delalloc extent and may require multiple
4586 * invocations to allocate the target offset if a large enough physical extent
4590 xfs_bmapi_convert_delalloc(
4591 struct xfs_inode
*ip
,
4594 struct iomap
*iomap
,
4597 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4598 struct xfs_mount
*mp
= ip
->i_mount
;
4599 xfs_fileoff_t offset_fsb
= XFS_B_TO_FSBT(mp
, offset
);
4600 struct xfs_bmalloca bma
= { NULL
};
4602 struct xfs_trans
*tp
;
4605 if (whichfork
== XFS_COW_FORK
)
4606 flags
|= IOMAP_F_SHARED
;
4609 * Space for the extent and indirect blocks was reserved when the
4610 * delalloc extent was created so there's no need to do so here.
4612 error
= xfs_trans_alloc(mp
, &M_RES(mp
)->tr_write
, 0, 0,
4613 XFS_TRANS_RESERVE
, &tp
);
4617 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
4619 error
= xfs_iext_count_may_overflow(ip
, whichfork
,
4620 XFS_IEXT_ADD_NOSPLIT_CNT
);
4622 goto out_trans_cancel
;
4624 xfs_trans_ijoin(tp
, ip
, 0);
4626 if (!xfs_iext_lookup_extent(ip
, ifp
, offset_fsb
, &bma
.icur
, &bma
.got
) ||
4627 bma
.got
.br_startoff
> offset_fsb
) {
4629 * No extent found in the range we are trying to convert. This
4630 * should only happen for the COW fork, where another thread
4631 * might have moved the extent to the data fork in the meantime.
4633 WARN_ON_ONCE(whichfork
!= XFS_COW_FORK
);
4635 goto out_trans_cancel
;
4639 * If we find a real extent here we raced with another thread converting
4640 * the extent. Just return the real extent at this offset.
4642 if (!isnullstartblock(bma
.got
.br_startblock
)) {
4643 xfs_bmbt_to_iomap(ip
, iomap
, &bma
.got
, flags
);
4644 *seq
= READ_ONCE(ifp
->if_seq
);
4645 goto out_trans_cancel
;
4651 bma
.offset
= bma
.got
.br_startoff
;
4652 bma
.length
= max_t(xfs_filblks_t
, bma
.got
.br_blockcount
, MAXEXTLEN
);
4653 bma
.minleft
= xfs_bmapi_minleft(tp
, ip
, whichfork
);
4656 * When we're converting the delalloc reservations backing dirty pages
4657 * in the page cache, we must be careful about how we create the new
4660 * New CoW fork extents are created unwritten, turned into real extents
4661 * when we're about to write the data to disk, and mapped into the data
4662 * fork after the write finishes. End of story.
4664 * New data fork extents must be mapped in as unwritten and converted
4665 * to real extents after the write succeeds to avoid exposing stale
4666 * disk contents if we crash.
4668 bma
.flags
= XFS_BMAPI_PREALLOC
;
4669 if (whichfork
== XFS_COW_FORK
)
4670 bma
.flags
|= XFS_BMAPI_COWFORK
;
4672 if (!xfs_iext_peek_prev_extent(ifp
, &bma
.icur
, &bma
.prev
))
4673 bma
.prev
.br_startoff
= NULLFILEOFF
;
4675 error
= xfs_bmapi_allocate(&bma
);
4680 if (WARN_ON_ONCE(bma
.blkno
== NULLFSBLOCK
))
4682 error
= -EFSCORRUPTED
;
4683 if (WARN_ON_ONCE(!xfs_valid_startblock(ip
, bma
.got
.br_startblock
)))
4686 XFS_STATS_ADD(mp
, xs_xstrat_bytes
, XFS_FSB_TO_B(mp
, bma
.length
));
4687 XFS_STATS_INC(mp
, xs_xstrat_quick
);
4689 ASSERT(!isnullstartblock(bma
.got
.br_startblock
));
4690 xfs_bmbt_to_iomap(ip
, iomap
, &bma
.got
, flags
);
4691 *seq
= READ_ONCE(ifp
->if_seq
);
4693 if (whichfork
== XFS_COW_FORK
)
4694 xfs_refcount_alloc_cow_extent(tp
, bma
.blkno
, bma
.length
);
4696 error
= xfs_bmap_btree_to_extents(tp
, ip
, bma
.cur
, &bma
.logflags
,
4701 xfs_bmapi_finish(&bma
, whichfork
, 0);
4702 error
= xfs_trans_commit(tp
);
4703 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
4707 xfs_bmapi_finish(&bma
, whichfork
, error
);
4709 xfs_trans_cancel(tp
);
4710 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
4716 struct xfs_trans
*tp
,
4717 struct xfs_inode
*ip
,
4720 xfs_fsblock_t startblock
,
4723 struct xfs_mount
*mp
= ip
->i_mount
;
4724 struct xfs_ifork
*ifp
;
4725 struct xfs_btree_cur
*cur
= NULL
;
4726 struct xfs_bmbt_irec got
;
4727 struct xfs_iext_cursor icur
;
4728 int whichfork
= xfs_bmapi_whichfork(flags
);
4729 int logflags
= 0, error
;
4731 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4733 ASSERT(len
<= (xfs_filblks_t
)MAXEXTLEN
);
4734 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
4735 ASSERT(!(flags
& ~(XFS_BMAPI_ATTRFORK
| XFS_BMAPI_PREALLOC
|
4736 XFS_BMAPI_NORMAP
)));
4737 ASSERT((flags
& (XFS_BMAPI_ATTRFORK
| XFS_BMAPI_PREALLOC
)) !=
4738 (XFS_BMAPI_ATTRFORK
| XFS_BMAPI_PREALLOC
));
4740 if (XFS_IS_CORRUPT(mp
, !xfs_ifork_has_extents(ifp
)) ||
4741 XFS_TEST_ERROR(false, mp
, XFS_ERRTAG_BMAPIFORMAT
)) {
4742 return -EFSCORRUPTED
;
4745 if (XFS_FORCED_SHUTDOWN(mp
))
4748 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
4749 error
= xfs_iread_extents(tp
, ip
, whichfork
);
4754 if (xfs_iext_lookup_extent(ip
, ifp
, bno
, &icur
, &got
)) {
4755 /* make sure we only reflink into a hole. */
4756 ASSERT(got
.br_startoff
> bno
);
4757 ASSERT(got
.br_startoff
- bno
>= len
);
4760 ip
->i_d
.di_nblocks
+= len
;
4761 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
4763 if (ifp
->if_flags
& XFS_IFBROOT
) {
4764 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
4765 cur
->bc_ino
.flags
= 0;
4768 got
.br_startoff
= bno
;
4769 got
.br_startblock
= startblock
;
4770 got
.br_blockcount
= len
;
4771 if (flags
& XFS_BMAPI_PREALLOC
)
4772 got
.br_state
= XFS_EXT_UNWRITTEN
;
4774 got
.br_state
= XFS_EXT_NORM
;
4776 error
= xfs_bmap_add_extent_hole_real(tp
, ip
, whichfork
, &icur
,
4777 &cur
, &got
, &logflags
, flags
);
4781 error
= xfs_bmap_btree_to_extents(tp
, ip
, cur
, &logflags
, whichfork
);
4784 if (ip
->i_df
.if_format
!= XFS_DINODE_FMT_EXTENTS
)
4785 logflags
&= ~XFS_ILOG_DEXT
;
4786 else if (ip
->i_df
.if_format
!= XFS_DINODE_FMT_BTREE
)
4787 logflags
&= ~XFS_ILOG_DBROOT
;
4790 xfs_trans_log_inode(tp
, ip
, logflags
);
4792 xfs_btree_del_cursor(cur
, error
);
4797 * When a delalloc extent is split (e.g., due to a hole punch), the original
4798 * indlen reservation must be shared across the two new extents that are left
4801 * Given the original reservation and the worst case indlen for the two new
4802 * extents (as calculated by xfs_bmap_worst_indlen()), split the original
4803 * reservation fairly across the two new extents. If necessary, steal available
4804 * blocks from a deleted extent to make up a reservation deficiency (e.g., if
4805 * ores == 1). The number of stolen blocks is returned. The availability and
4806 * subsequent accounting of stolen blocks is the responsibility of the caller.
4808 static xfs_filblks_t
4809 xfs_bmap_split_indlen(
4810 xfs_filblks_t ores
, /* original res. */
4811 xfs_filblks_t
*indlen1
, /* ext1 worst indlen */
4812 xfs_filblks_t
*indlen2
, /* ext2 worst indlen */
4813 xfs_filblks_t avail
) /* stealable blocks */
4815 xfs_filblks_t len1
= *indlen1
;
4816 xfs_filblks_t len2
= *indlen2
;
4817 xfs_filblks_t nres
= len1
+ len2
; /* new total res. */
4818 xfs_filblks_t stolen
= 0;
4819 xfs_filblks_t resfactor
;
4822 * Steal as many blocks as we can to try and satisfy the worst case
4823 * indlen for both new extents.
4825 if (ores
< nres
&& avail
)
4826 stolen
= XFS_FILBLKS_MIN(nres
- ores
, avail
);
4829 /* nothing else to do if we've satisfied the new reservation */
4834 * We can't meet the total required reservation for the two extents.
4835 * Calculate the percent of the overall shortage between both extents
4836 * and apply this percentage to each of the requested indlen values.
4837 * This distributes the shortage fairly and reduces the chances that one
4838 * of the two extents is left with nothing when extents are repeatedly
4841 resfactor
= (ores
* 100);
4842 do_div(resfactor
, nres
);
4847 ASSERT(len1
+ len2
<= ores
);
4848 ASSERT(len1
< *indlen1
&& len2
< *indlen2
);
4851 * Hand out the remainder to each extent. If one of the two reservations
4852 * is zero, we want to make sure that one gets a block first. The loop
4853 * below starts with len1, so hand len2 a block right off the bat if it
4856 ores
-= (len1
+ len2
);
4857 ASSERT((*indlen1
- len1
) + (*indlen2
- len2
) >= ores
);
4858 if (ores
&& !len2
&& *indlen2
) {
4863 if (len1
< *indlen1
) {
4869 if (len2
< *indlen2
) {
4882 xfs_bmap_del_extent_delay(
4883 struct xfs_inode
*ip
,
4885 struct xfs_iext_cursor
*icur
,
4886 struct xfs_bmbt_irec
*got
,
4887 struct xfs_bmbt_irec
*del
)
4889 struct xfs_mount
*mp
= ip
->i_mount
;
4890 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4891 struct xfs_bmbt_irec
new;
4892 int64_t da_old
, da_new
, da_diff
= 0;
4893 xfs_fileoff_t del_endoff
, got_endoff
;
4894 xfs_filblks_t got_indlen
, new_indlen
, stolen
;
4895 int state
= xfs_bmap_fork_to_state(whichfork
);
4899 XFS_STATS_INC(mp
, xs_del_exlist
);
4901 isrt
= (whichfork
== XFS_DATA_FORK
) && XFS_IS_REALTIME_INODE(ip
);
4902 del_endoff
= del
->br_startoff
+ del
->br_blockcount
;
4903 got_endoff
= got
->br_startoff
+ got
->br_blockcount
;
4904 da_old
= startblockval(got
->br_startblock
);
4907 ASSERT(del
->br_blockcount
> 0);
4908 ASSERT(got
->br_startoff
<= del
->br_startoff
);
4909 ASSERT(got_endoff
>= del_endoff
);
4912 uint64_t rtexts
= XFS_FSB_TO_B(mp
, del
->br_blockcount
);
4914 do_div(rtexts
, mp
->m_sb
.sb_rextsize
);
4915 xfs_mod_frextents(mp
, rtexts
);
4919 * Update the inode delalloc counter now and wait to update the
4920 * sb counters as we might have to borrow some blocks for the
4921 * indirect block accounting.
4924 error
= xfs_quota_unreserve_blkres(ip
, del
->br_blockcount
);
4927 ip
->i_delayed_blks
-= del
->br_blockcount
;
4929 if (got
->br_startoff
== del
->br_startoff
)
4930 state
|= BMAP_LEFT_FILLING
;
4931 if (got_endoff
== del_endoff
)
4932 state
|= BMAP_RIGHT_FILLING
;
4934 switch (state
& (BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
)) {
4935 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
4937 * Matches the whole extent. Delete the entry.
4939 xfs_iext_remove(ip
, icur
, state
);
4940 xfs_iext_prev(ifp
, icur
);
4942 case BMAP_LEFT_FILLING
:
4944 * Deleting the first part of the extent.
4946 got
->br_startoff
= del_endoff
;
4947 got
->br_blockcount
-= del
->br_blockcount
;
4948 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
,
4949 got
->br_blockcount
), da_old
);
4950 got
->br_startblock
= nullstartblock((int)da_new
);
4951 xfs_iext_update_extent(ip
, state
, icur
, got
);
4953 case BMAP_RIGHT_FILLING
:
4955 * Deleting the last part of the extent.
4957 got
->br_blockcount
= got
->br_blockcount
- del
->br_blockcount
;
4958 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
,
4959 got
->br_blockcount
), da_old
);
4960 got
->br_startblock
= nullstartblock((int)da_new
);
4961 xfs_iext_update_extent(ip
, state
, icur
, got
);
4965 * Deleting the middle of the extent.
4967 * Distribute the original indlen reservation across the two new
4968 * extents. Steal blocks from the deleted extent if necessary.
4969 * Stealing blocks simply fudges the fdblocks accounting below.
4970 * Warn if either of the new indlen reservations is zero as this
4971 * can lead to delalloc problems.
4973 got
->br_blockcount
= del
->br_startoff
- got
->br_startoff
;
4974 got_indlen
= xfs_bmap_worst_indlen(ip
, got
->br_blockcount
);
4976 new.br_blockcount
= got_endoff
- del_endoff
;
4977 new_indlen
= xfs_bmap_worst_indlen(ip
, new.br_blockcount
);
4979 WARN_ON_ONCE(!got_indlen
|| !new_indlen
);
4980 stolen
= xfs_bmap_split_indlen(da_old
, &got_indlen
, &new_indlen
,
4981 del
->br_blockcount
);
4983 got
->br_startblock
= nullstartblock((int)got_indlen
);
4985 new.br_startoff
= del_endoff
;
4986 new.br_state
= got
->br_state
;
4987 new.br_startblock
= nullstartblock((int)new_indlen
);
4989 xfs_iext_update_extent(ip
, state
, icur
, got
);
4990 xfs_iext_next(ifp
, icur
);
4991 xfs_iext_insert(ip
, icur
, &new, state
);
4993 da_new
= got_indlen
+ new_indlen
- stolen
;
4994 del
->br_blockcount
-= stolen
;
4998 ASSERT(da_old
>= da_new
);
4999 da_diff
= da_old
- da_new
;
5001 da_diff
+= del
->br_blockcount
;
5003 xfs_mod_fdblocks(mp
, da_diff
, false);
5004 xfs_mod_delalloc(mp
, -da_diff
);
5010 xfs_bmap_del_extent_cow(
5011 struct xfs_inode
*ip
,
5012 struct xfs_iext_cursor
*icur
,
5013 struct xfs_bmbt_irec
*got
,
5014 struct xfs_bmbt_irec
*del
)
5016 struct xfs_mount
*mp
= ip
->i_mount
;
5017 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, XFS_COW_FORK
);
5018 struct xfs_bmbt_irec
new;
5019 xfs_fileoff_t del_endoff
, got_endoff
;
5020 int state
= BMAP_COWFORK
;
5022 XFS_STATS_INC(mp
, xs_del_exlist
);
5024 del_endoff
= del
->br_startoff
+ del
->br_blockcount
;
5025 got_endoff
= got
->br_startoff
+ got
->br_blockcount
;
5027 ASSERT(del
->br_blockcount
> 0);
5028 ASSERT(got
->br_startoff
<= del
->br_startoff
);
5029 ASSERT(got_endoff
>= del_endoff
);
5030 ASSERT(!isnullstartblock(got
->br_startblock
));
5032 if (got
->br_startoff
== del
->br_startoff
)
5033 state
|= BMAP_LEFT_FILLING
;
5034 if (got_endoff
== del_endoff
)
5035 state
|= BMAP_RIGHT_FILLING
;
5037 switch (state
& (BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
)) {
5038 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
5040 * Matches the whole extent. Delete the entry.
5042 xfs_iext_remove(ip
, icur
, state
);
5043 xfs_iext_prev(ifp
, icur
);
5045 case BMAP_LEFT_FILLING
:
5047 * Deleting the first part of the extent.
5049 got
->br_startoff
= del_endoff
;
5050 got
->br_blockcount
-= del
->br_blockcount
;
5051 got
->br_startblock
= del
->br_startblock
+ del
->br_blockcount
;
5052 xfs_iext_update_extent(ip
, state
, icur
, got
);
5054 case BMAP_RIGHT_FILLING
:
5056 * Deleting the last part of the extent.
5058 got
->br_blockcount
-= del
->br_blockcount
;
5059 xfs_iext_update_extent(ip
, state
, icur
, got
);
5063 * Deleting the middle of the extent.
5065 got
->br_blockcount
= del
->br_startoff
- got
->br_startoff
;
5067 new.br_startoff
= del_endoff
;
5068 new.br_blockcount
= got_endoff
- del_endoff
;
5069 new.br_state
= got
->br_state
;
5070 new.br_startblock
= del
->br_startblock
+ del
->br_blockcount
;
5072 xfs_iext_update_extent(ip
, state
, icur
, got
);
5073 xfs_iext_next(ifp
, icur
);
5074 xfs_iext_insert(ip
, icur
, &new, state
);
5077 ip
->i_delayed_blks
-= del
->br_blockcount
;
5081 * Called by xfs_bmapi to update file extent records and the btree
5082 * after removing space.
5084 STATIC
int /* error */
5085 xfs_bmap_del_extent_real(
5086 xfs_inode_t
*ip
, /* incore inode pointer */
5087 xfs_trans_t
*tp
, /* current transaction pointer */
5088 struct xfs_iext_cursor
*icur
,
5089 xfs_btree_cur_t
*cur
, /* if null, not a btree */
5090 xfs_bmbt_irec_t
*del
, /* data to remove from extents */
5091 int *logflagsp
, /* inode logging flags */
5092 int whichfork
, /* data or attr fork */
5093 int bflags
) /* bmapi flags */
5095 xfs_fsblock_t del_endblock
=0; /* first block past del */
5096 xfs_fileoff_t del_endoff
; /* first offset past del */
5097 int do_fx
; /* free extent at end of routine */
5098 int error
; /* error return value */
5099 int flags
= 0;/* inode logging flags */
5100 struct xfs_bmbt_irec got
; /* current extent entry */
5101 xfs_fileoff_t got_endoff
; /* first offset past got */
5102 int i
; /* temp state */
5103 struct xfs_ifork
*ifp
; /* inode fork pointer */
5104 xfs_mount_t
*mp
; /* mount structure */
5105 xfs_filblks_t nblks
; /* quota/sb block count */
5106 xfs_bmbt_irec_t
new; /* new record to be inserted */
5108 uint qfield
; /* quota field to update */
5109 int state
= xfs_bmap_fork_to_state(whichfork
);
5110 struct xfs_bmbt_irec old
;
5113 XFS_STATS_INC(mp
, xs_del_exlist
);
5115 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5116 ASSERT(del
->br_blockcount
> 0);
5117 xfs_iext_get_extent(ifp
, icur
, &got
);
5118 ASSERT(got
.br_startoff
<= del
->br_startoff
);
5119 del_endoff
= del
->br_startoff
+ del
->br_blockcount
;
5120 got_endoff
= got
.br_startoff
+ got
.br_blockcount
;
5121 ASSERT(got_endoff
>= del_endoff
);
5122 ASSERT(!isnullstartblock(got
.br_startblock
));
5127 * If it's the case where the directory code is running with no block
5128 * reservation, and the deleted block is in the middle of its extent,
5129 * and the resulting insert of an extent would cause transformation to
5130 * btree format, then reject it. The calling code will then swap blocks
5131 * around instead. We have to do this now, rather than waiting for the
5132 * conversion to btree format, since the transaction will be dirty then.
5134 if (tp
->t_blk_res
== 0 &&
5135 ifp
->if_format
== XFS_DINODE_FMT_EXTENTS
&&
5136 ifp
->if_nextents
>= XFS_IFORK_MAXEXT(ip
, whichfork
) &&
5137 del
->br_startoff
> got
.br_startoff
&& del_endoff
< got_endoff
)
5140 flags
= XFS_ILOG_CORE
;
5141 if (whichfork
== XFS_DATA_FORK
&& XFS_IS_REALTIME_INODE(ip
)) {
5145 len
= div_u64_rem(del
->br_blockcount
, mp
->m_sb
.sb_rextsize
,
5149 if (!(bflags
& XFS_BMAPI_REMAP
)) {
5152 bno
= div_u64_rem(del
->br_startblock
,
5153 mp
->m_sb
.sb_rextsize
, &mod
);
5156 error
= xfs_rtfree_extent(tp
, bno
, (xfs_extlen_t
)len
);
5162 nblks
= len
* mp
->m_sb
.sb_rextsize
;
5163 qfield
= XFS_TRANS_DQ_RTBCOUNT
;
5166 nblks
= del
->br_blockcount
;
5167 qfield
= XFS_TRANS_DQ_BCOUNT
;
5170 del_endblock
= del
->br_startblock
+ del
->br_blockcount
;
5172 error
= xfs_bmbt_lookup_eq(cur
, &got
, &i
);
5175 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
5176 error
= -EFSCORRUPTED
;
5181 if (got
.br_startoff
== del
->br_startoff
)
5182 state
|= BMAP_LEFT_FILLING
;
5183 if (got_endoff
== del_endoff
)
5184 state
|= BMAP_RIGHT_FILLING
;
5186 switch (state
& (BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
)) {
5187 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
5189 * Matches the whole extent. Delete the entry.
5191 xfs_iext_remove(ip
, icur
, state
);
5192 xfs_iext_prev(ifp
, icur
);
5195 flags
|= XFS_ILOG_CORE
;
5197 flags
|= xfs_ilog_fext(whichfork
);
5200 if ((error
= xfs_btree_delete(cur
, &i
)))
5202 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
5203 error
= -EFSCORRUPTED
;
5207 case BMAP_LEFT_FILLING
:
5209 * Deleting the first part of the extent.
5211 got
.br_startoff
= del_endoff
;
5212 got
.br_startblock
= del_endblock
;
5213 got
.br_blockcount
-= del
->br_blockcount
;
5214 xfs_iext_update_extent(ip
, state
, icur
, &got
);
5216 flags
|= xfs_ilog_fext(whichfork
);
5219 error
= xfs_bmbt_update(cur
, &got
);
5223 case BMAP_RIGHT_FILLING
:
5225 * Deleting the last part of the extent.
5227 got
.br_blockcount
-= del
->br_blockcount
;
5228 xfs_iext_update_extent(ip
, state
, icur
, &got
);
5230 flags
|= xfs_ilog_fext(whichfork
);
5233 error
= xfs_bmbt_update(cur
, &got
);
5239 * Deleting the middle of the extent.
5243 * For directories, -ENOSPC is returned since a directory entry
5244 * remove operation must not fail due to low extent count
5245 * availability. -ENOSPC will be handled by higher layers of XFS
5246 * by letting the corresponding empty Data/Free blocks to linger
5247 * until a future remove operation. Dabtree blocks would be
5248 * swapped with the last block in the leaf space and then the
5249 * new last block will be unmapped.
5251 * The above logic also applies to the source directory entry of
5252 * a rename operation.
5254 error
= xfs_iext_count_may_overflow(ip
, whichfork
, 1);
5256 ASSERT(S_ISDIR(VFS_I(ip
)->i_mode
) &&
5257 whichfork
== XFS_DATA_FORK
);
5264 got
.br_blockcount
= del
->br_startoff
- got
.br_startoff
;
5265 xfs_iext_update_extent(ip
, state
, icur
, &got
);
5267 new.br_startoff
= del_endoff
;
5268 new.br_blockcount
= got_endoff
- del_endoff
;
5269 new.br_state
= got
.br_state
;
5270 new.br_startblock
= del_endblock
;
5272 flags
|= XFS_ILOG_CORE
;
5274 error
= xfs_bmbt_update(cur
, &got
);
5277 error
= xfs_btree_increment(cur
, 0, &i
);
5280 cur
->bc_rec
.b
= new;
5281 error
= xfs_btree_insert(cur
, &i
);
5282 if (error
&& error
!= -ENOSPC
)
5285 * If get no-space back from btree insert, it tried a
5286 * split, and we have a zero block reservation. Fix up
5287 * our state and return the error.
5289 if (error
== -ENOSPC
) {
5291 * Reset the cursor, don't trust it after any
5294 error
= xfs_bmbt_lookup_eq(cur
, &got
, &i
);
5297 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
5298 error
= -EFSCORRUPTED
;
5302 * Update the btree record back
5303 * to the original value.
5305 error
= xfs_bmbt_update(cur
, &old
);
5309 * Reset the extent record back
5310 * to the original value.
5312 xfs_iext_update_extent(ip
, state
, icur
, &old
);
5317 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
5318 error
= -EFSCORRUPTED
;
5322 flags
|= xfs_ilog_fext(whichfork
);
5325 xfs_iext_next(ifp
, icur
);
5326 xfs_iext_insert(ip
, icur
, &new, state
);
5330 /* remove reverse mapping */
5331 xfs_rmap_unmap_extent(tp
, ip
, whichfork
, del
);
5334 * If we need to, add to list of extents to delete.
5336 if (do_fx
&& !(bflags
& XFS_BMAPI_REMAP
)) {
5337 if (xfs_is_reflink_inode(ip
) && whichfork
== XFS_DATA_FORK
) {
5338 xfs_refcount_decrease_extent(tp
, del
);
5340 __xfs_bmap_add_free(tp
, del
->br_startblock
,
5341 del
->br_blockcount
, NULL
,
5342 (bflags
& XFS_BMAPI_NODISCARD
) ||
5343 del
->br_state
== XFS_EXT_UNWRITTEN
);
5348 * Adjust inode # blocks in the file.
5351 ip
->i_d
.di_nblocks
-= nblks
;
5353 * Adjust quota data.
5355 if (qfield
&& !(bflags
& XFS_BMAPI_REMAP
))
5356 xfs_trans_mod_dquot_byino(tp
, ip
, qfield
, (long)-nblks
);
5364 * Unmap (remove) blocks from a file.
5365 * If nexts is nonzero then the number of extents to remove is limited to
5366 * that value. If not all extents in the block range can be removed then
5371 struct xfs_trans
*tp
, /* transaction pointer */
5372 struct xfs_inode
*ip
, /* incore inode */
5373 xfs_fileoff_t start
, /* first file offset deleted */
5374 xfs_filblks_t
*rlen
, /* i/o: amount remaining */
5375 int flags
, /* misc flags */
5376 xfs_extnum_t nexts
) /* number of extents max */
5378 struct xfs_btree_cur
*cur
; /* bmap btree cursor */
5379 struct xfs_bmbt_irec del
; /* extent being deleted */
5380 int error
; /* error return value */
5381 xfs_extnum_t extno
; /* extent number in list */
5382 struct xfs_bmbt_irec got
; /* current extent record */
5383 struct xfs_ifork
*ifp
; /* inode fork pointer */
5384 int isrt
; /* freeing in rt area */
5385 int logflags
; /* transaction logging flags */
5386 xfs_extlen_t mod
; /* rt extent offset */
5387 struct xfs_mount
*mp
= ip
->i_mount
;
5388 int tmp_logflags
; /* partial logging flags */
5389 int wasdel
; /* was a delayed alloc extent */
5390 int whichfork
; /* data or attribute fork */
5392 xfs_filblks_t len
= *rlen
; /* length to unmap in file */
5393 xfs_fileoff_t max_len
;
5394 xfs_agnumber_t prev_agno
= NULLAGNUMBER
, agno
;
5396 struct xfs_iext_cursor icur
;
5399 trace_xfs_bunmap(ip
, start
, len
, flags
, _RET_IP_
);
5401 whichfork
= xfs_bmapi_whichfork(flags
);
5402 ASSERT(whichfork
!= XFS_COW_FORK
);
5403 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5404 if (XFS_IS_CORRUPT(mp
, !xfs_ifork_has_extents(ifp
)))
5405 return -EFSCORRUPTED
;
5406 if (XFS_FORCED_SHUTDOWN(mp
))
5409 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
5414 * Guesstimate how many blocks we can unmap without running the risk of
5415 * blowing out the transaction with a mix of EFIs and reflink
5418 if (tp
&& xfs_is_reflink_inode(ip
) && whichfork
== XFS_DATA_FORK
)
5419 max_len
= min(len
, xfs_refcount_max_unmap(tp
->t_log_res
));
5423 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
5424 (error
= xfs_iread_extents(tp
, ip
, whichfork
)))
5426 if (xfs_iext_count(ifp
) == 0) {
5430 XFS_STATS_INC(mp
, xs_blk_unmap
);
5431 isrt
= (whichfork
== XFS_DATA_FORK
) && XFS_IS_REALTIME_INODE(ip
);
5434 if (!xfs_iext_lookup_extent_before(ip
, ifp
, &end
, &icur
, &got
)) {
5441 if (ifp
->if_flags
& XFS_IFBROOT
) {
5442 ASSERT(ifp
->if_format
== XFS_DINODE_FMT_BTREE
);
5443 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
5444 cur
->bc_ino
.flags
= 0;
5450 * Synchronize by locking the bitmap inode.
5452 xfs_ilock(mp
->m_rbmip
, XFS_ILOCK_EXCL
|XFS_ILOCK_RTBITMAP
);
5453 xfs_trans_ijoin(tp
, mp
->m_rbmip
, XFS_ILOCK_EXCL
);
5454 xfs_ilock(mp
->m_rsumip
, XFS_ILOCK_EXCL
|XFS_ILOCK_RTSUM
);
5455 xfs_trans_ijoin(tp
, mp
->m_rsumip
, XFS_ILOCK_EXCL
);
5459 while (end
!= (xfs_fileoff_t
)-1 && end
>= start
&&
5460 (nexts
== 0 || extno
< nexts
) && max_len
> 0) {
5462 * Is the found extent after a hole in which end lives?
5463 * Just back up to the previous extent, if so.
5465 if (got
.br_startoff
> end
&&
5466 !xfs_iext_prev_extent(ifp
, &icur
, &got
)) {
5471 * Is the last block of this extent before the range
5472 * we're supposed to delete? If so, we're done.
5474 end
= XFS_FILEOFF_MIN(end
,
5475 got
.br_startoff
+ got
.br_blockcount
- 1);
5479 * Then deal with the (possibly delayed) allocated space
5483 wasdel
= isnullstartblock(del
.br_startblock
);
5486 * Make sure we don't touch multiple AGF headers out of order
5487 * in a single transaction, as that could cause AB-BA deadlocks.
5489 if (!wasdel
&& !isrt
) {
5490 agno
= XFS_FSB_TO_AGNO(mp
, del
.br_startblock
);
5491 if (prev_agno
!= NULLAGNUMBER
&& prev_agno
> agno
)
5495 if (got
.br_startoff
< start
) {
5496 del
.br_startoff
= start
;
5497 del
.br_blockcount
-= start
- got
.br_startoff
;
5499 del
.br_startblock
+= start
- got
.br_startoff
;
5501 if (del
.br_startoff
+ del
.br_blockcount
> end
+ 1)
5502 del
.br_blockcount
= end
+ 1 - del
.br_startoff
;
5504 /* How much can we safely unmap? */
5505 if (max_len
< del
.br_blockcount
) {
5506 del
.br_startoff
+= del
.br_blockcount
- max_len
;
5508 del
.br_startblock
+= del
.br_blockcount
- max_len
;
5509 del
.br_blockcount
= max_len
;
5515 sum
= del
.br_startblock
+ del
.br_blockcount
;
5516 div_u64_rem(sum
, mp
->m_sb
.sb_rextsize
, &mod
);
5519 * Realtime extent not lined up at the end.
5520 * The extent could have been split into written
5521 * and unwritten pieces, or we could just be
5522 * unmapping part of it. But we can't really
5523 * get rid of part of a realtime extent.
5525 if (del
.br_state
== XFS_EXT_UNWRITTEN
) {
5527 * This piece is unwritten, or we're not
5528 * using unwritten extents. Skip over it.
5531 end
-= mod
> del
.br_blockcount
?
5532 del
.br_blockcount
: mod
;
5533 if (end
< got
.br_startoff
&&
5534 !xfs_iext_prev_extent(ifp
, &icur
, &got
)) {
5541 * It's written, turn it unwritten.
5542 * This is better than zeroing it.
5544 ASSERT(del
.br_state
== XFS_EXT_NORM
);
5545 ASSERT(tp
->t_blk_res
> 0);
5547 * If this spans a realtime extent boundary,
5548 * chop it back to the start of the one we end at.
5550 if (del
.br_blockcount
> mod
) {
5551 del
.br_startoff
+= del
.br_blockcount
- mod
;
5552 del
.br_startblock
+= del
.br_blockcount
- mod
;
5553 del
.br_blockcount
= mod
;
5555 del
.br_state
= XFS_EXT_UNWRITTEN
;
5556 error
= xfs_bmap_add_extent_unwritten_real(tp
, ip
,
5557 whichfork
, &icur
, &cur
, &del
,
5563 div_u64_rem(del
.br_startblock
, mp
->m_sb
.sb_rextsize
, &mod
);
5565 xfs_extlen_t off
= mp
->m_sb
.sb_rextsize
- mod
;
5568 * Realtime extent is lined up at the end but not
5569 * at the front. We'll get rid of full extents if
5572 if (del
.br_blockcount
> off
) {
5573 del
.br_blockcount
-= off
;
5574 del
.br_startoff
+= off
;
5575 del
.br_startblock
+= off
;
5576 } else if (del
.br_startoff
== start
&&
5577 (del
.br_state
== XFS_EXT_UNWRITTEN
||
5578 tp
->t_blk_res
== 0)) {
5580 * Can't make it unwritten. There isn't
5581 * a full extent here so just skip it.
5583 ASSERT(end
>= del
.br_blockcount
);
5584 end
-= del
.br_blockcount
;
5585 if (got
.br_startoff
> end
&&
5586 !xfs_iext_prev_extent(ifp
, &icur
, &got
)) {
5591 } else if (del
.br_state
== XFS_EXT_UNWRITTEN
) {
5592 struct xfs_bmbt_irec prev
;
5593 xfs_fileoff_t unwrite_start
;
5596 * This one is already unwritten.
5597 * It must have a written left neighbor.
5598 * Unwrite the killed part of that one and
5601 if (!xfs_iext_prev_extent(ifp
, &icur
, &prev
))
5603 ASSERT(prev
.br_state
== XFS_EXT_NORM
);
5604 ASSERT(!isnullstartblock(prev
.br_startblock
));
5605 ASSERT(del
.br_startblock
==
5606 prev
.br_startblock
+ prev
.br_blockcount
);
5607 unwrite_start
= max3(start
,
5608 del
.br_startoff
- mod
,
5610 mod
= unwrite_start
- prev
.br_startoff
;
5611 prev
.br_startoff
= unwrite_start
;
5612 prev
.br_startblock
+= mod
;
5613 prev
.br_blockcount
-= mod
;
5614 prev
.br_state
= XFS_EXT_UNWRITTEN
;
5615 error
= xfs_bmap_add_extent_unwritten_real(tp
,
5616 ip
, whichfork
, &icur
, &cur
,
5622 ASSERT(del
.br_state
== XFS_EXT_NORM
);
5623 del
.br_state
= XFS_EXT_UNWRITTEN
;
5624 error
= xfs_bmap_add_extent_unwritten_real(tp
,
5625 ip
, whichfork
, &icur
, &cur
,
5635 error
= xfs_bmap_del_extent_delay(ip
, whichfork
, &icur
,
5638 error
= xfs_bmap_del_extent_real(ip
, tp
, &icur
, cur
,
5639 &del
, &tmp_logflags
, whichfork
,
5641 logflags
|= tmp_logflags
;
5647 max_len
-= del
.br_blockcount
;
5648 end
= del
.br_startoff
- 1;
5651 * If not done go on to the next (previous) record.
5653 if (end
!= (xfs_fileoff_t
)-1 && end
>= start
) {
5654 if (!xfs_iext_get_extent(ifp
, &icur
, &got
) ||
5655 (got
.br_startoff
> end
&&
5656 !xfs_iext_prev_extent(ifp
, &icur
, &got
))) {
5663 if (done
|| end
== (xfs_fileoff_t
)-1 || end
< start
)
5666 *rlen
= end
- start
+ 1;
5669 * Convert to a btree if necessary.
5671 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
5672 ASSERT(cur
== NULL
);
5673 error
= xfs_bmap_extents_to_btree(tp
, ip
, &cur
, 0,
5674 &tmp_logflags
, whichfork
);
5675 logflags
|= tmp_logflags
;
5677 error
= xfs_bmap_btree_to_extents(tp
, ip
, cur
, &logflags
,
5683 * Log everything. Do this after conversion, there's no point in
5684 * logging the extent records if we've converted to btree format.
5686 if ((logflags
& xfs_ilog_fext(whichfork
)) &&
5687 ifp
->if_format
!= XFS_DINODE_FMT_EXTENTS
)
5688 logflags
&= ~xfs_ilog_fext(whichfork
);
5689 else if ((logflags
& xfs_ilog_fbroot(whichfork
)) &&
5690 ifp
->if_format
!= XFS_DINODE_FMT_BTREE
)
5691 logflags
&= ~xfs_ilog_fbroot(whichfork
);
5693 * Log inode even in the error case, if the transaction
5694 * is dirty we'll need to shut down the filesystem.
5697 xfs_trans_log_inode(tp
, ip
, logflags
);
5700 cur
->bc_ino
.allocated
= 0;
5701 xfs_btree_del_cursor(cur
, error
);
5706 /* Unmap a range of a file. */
5710 struct xfs_inode
*ip
,
5719 error
= __xfs_bunmapi(tp
, ip
, bno
, &len
, flags
, nexts
);
5725 * Determine whether an extent shift can be accomplished by a merge with the
5726 * extent that precedes the target hole of the shift.
5730 struct xfs_bmbt_irec
*left
, /* preceding extent */
5731 struct xfs_bmbt_irec
*got
, /* current extent to shift */
5732 xfs_fileoff_t shift
) /* shift fsb */
5734 xfs_fileoff_t startoff
;
5736 startoff
= got
->br_startoff
- shift
;
5739 * The extent, once shifted, must be adjacent in-file and on-disk with
5740 * the preceding extent.
5742 if ((left
->br_startoff
+ left
->br_blockcount
!= startoff
) ||
5743 (left
->br_startblock
+ left
->br_blockcount
!= got
->br_startblock
) ||
5744 (left
->br_state
!= got
->br_state
) ||
5745 (left
->br_blockcount
+ got
->br_blockcount
> MAXEXTLEN
))
5752 * A bmap extent shift adjusts the file offset of an extent to fill a preceding
5753 * hole in the file. If an extent shift would result in the extent being fully
5754 * adjacent to the extent that currently precedes the hole, we can merge with
5755 * the preceding extent rather than do the shift.
5757 * This function assumes the caller has verified a shift-by-merge is possible
5758 * with the provided extents via xfs_bmse_can_merge().
5762 struct xfs_trans
*tp
,
5763 struct xfs_inode
*ip
,
5765 xfs_fileoff_t shift
, /* shift fsb */
5766 struct xfs_iext_cursor
*icur
,
5767 struct xfs_bmbt_irec
*got
, /* extent to shift */
5768 struct xfs_bmbt_irec
*left
, /* preceding extent */
5769 struct xfs_btree_cur
*cur
,
5770 int *logflags
) /* output */
5772 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5773 struct xfs_bmbt_irec
new;
5774 xfs_filblks_t blockcount
;
5776 struct xfs_mount
*mp
= ip
->i_mount
;
5778 blockcount
= left
->br_blockcount
+ got
->br_blockcount
;
5780 ASSERT(xfs_isilocked(ip
, XFS_IOLOCK_EXCL
));
5781 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
5782 ASSERT(xfs_bmse_can_merge(left
, got
, shift
));
5785 new.br_blockcount
= blockcount
;
5788 * Update the on-disk extent count, the btree if necessary and log the
5792 *logflags
|= XFS_ILOG_CORE
;
5794 *logflags
|= XFS_ILOG_DEXT
;
5798 /* lookup and remove the extent to merge */
5799 error
= xfs_bmbt_lookup_eq(cur
, got
, &i
);
5802 if (XFS_IS_CORRUPT(mp
, i
!= 1))
5803 return -EFSCORRUPTED
;
5805 error
= xfs_btree_delete(cur
, &i
);
5808 if (XFS_IS_CORRUPT(mp
, i
!= 1))
5809 return -EFSCORRUPTED
;
5811 /* lookup and update size of the previous extent */
5812 error
= xfs_bmbt_lookup_eq(cur
, left
, &i
);
5815 if (XFS_IS_CORRUPT(mp
, i
!= 1))
5816 return -EFSCORRUPTED
;
5818 error
= xfs_bmbt_update(cur
, &new);
5822 /* change to extent format if required after extent removal */
5823 error
= xfs_bmap_btree_to_extents(tp
, ip
, cur
, logflags
, whichfork
);
5828 xfs_iext_remove(ip
, icur
, 0);
5829 xfs_iext_prev(ifp
, icur
);
5830 xfs_iext_update_extent(ip
, xfs_bmap_fork_to_state(whichfork
), icur
,
5833 /* update reverse mapping. rmap functions merge the rmaps for us */
5834 xfs_rmap_unmap_extent(tp
, ip
, whichfork
, got
);
5835 memcpy(&new, got
, sizeof(new));
5836 new.br_startoff
= left
->br_startoff
+ left
->br_blockcount
;
5837 xfs_rmap_map_extent(tp
, ip
, whichfork
, &new);
5842 xfs_bmap_shift_update_extent(
5843 struct xfs_trans
*tp
,
5844 struct xfs_inode
*ip
,
5846 struct xfs_iext_cursor
*icur
,
5847 struct xfs_bmbt_irec
*got
,
5848 struct xfs_btree_cur
*cur
,
5850 xfs_fileoff_t startoff
)
5852 struct xfs_mount
*mp
= ip
->i_mount
;
5853 struct xfs_bmbt_irec prev
= *got
;
5856 *logflags
|= XFS_ILOG_CORE
;
5858 got
->br_startoff
= startoff
;
5861 error
= xfs_bmbt_lookup_eq(cur
, &prev
, &i
);
5864 if (XFS_IS_CORRUPT(mp
, i
!= 1))
5865 return -EFSCORRUPTED
;
5867 error
= xfs_bmbt_update(cur
, got
);
5871 *logflags
|= XFS_ILOG_DEXT
;
5874 xfs_iext_update_extent(ip
, xfs_bmap_fork_to_state(whichfork
), icur
,
5877 /* update reverse mapping */
5878 xfs_rmap_unmap_extent(tp
, ip
, whichfork
, &prev
);
5879 xfs_rmap_map_extent(tp
, ip
, whichfork
, got
);
5884 xfs_bmap_collapse_extents(
5885 struct xfs_trans
*tp
,
5886 struct xfs_inode
*ip
,
5887 xfs_fileoff_t
*next_fsb
,
5888 xfs_fileoff_t offset_shift_fsb
,
5891 int whichfork
= XFS_DATA_FORK
;
5892 struct xfs_mount
*mp
= ip
->i_mount
;
5893 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5894 struct xfs_btree_cur
*cur
= NULL
;
5895 struct xfs_bmbt_irec got
, prev
;
5896 struct xfs_iext_cursor icur
;
5897 xfs_fileoff_t new_startoff
;
5901 if (XFS_IS_CORRUPT(mp
, !xfs_ifork_has_extents(ifp
)) ||
5902 XFS_TEST_ERROR(false, mp
, XFS_ERRTAG_BMAPIFORMAT
)) {
5903 return -EFSCORRUPTED
;
5906 if (XFS_FORCED_SHUTDOWN(mp
))
5909 ASSERT(xfs_isilocked(ip
, XFS_IOLOCK_EXCL
| XFS_ILOCK_EXCL
));
5911 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
5912 error
= xfs_iread_extents(tp
, ip
, whichfork
);
5917 if (ifp
->if_flags
& XFS_IFBROOT
) {
5918 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
5919 cur
->bc_ino
.flags
= 0;
5922 if (!xfs_iext_lookup_extent(ip
, ifp
, *next_fsb
, &icur
, &got
)) {
5926 if (XFS_IS_CORRUPT(mp
, isnullstartblock(got
.br_startblock
))) {
5927 error
= -EFSCORRUPTED
;
5931 new_startoff
= got
.br_startoff
- offset_shift_fsb
;
5932 if (xfs_iext_peek_prev_extent(ifp
, &icur
, &prev
)) {
5933 if (new_startoff
< prev
.br_startoff
+ prev
.br_blockcount
) {
5938 if (xfs_bmse_can_merge(&prev
, &got
, offset_shift_fsb
)) {
5939 error
= xfs_bmse_merge(tp
, ip
, whichfork
,
5940 offset_shift_fsb
, &icur
, &got
, &prev
,
5947 if (got
.br_startoff
< offset_shift_fsb
) {
5953 error
= xfs_bmap_shift_update_extent(tp
, ip
, whichfork
, &icur
, &got
,
5954 cur
, &logflags
, new_startoff
);
5959 if (!xfs_iext_next_extent(ifp
, &icur
, &got
)) {
5964 *next_fsb
= got
.br_startoff
;
5967 xfs_btree_del_cursor(cur
, error
);
5969 xfs_trans_log_inode(tp
, ip
, logflags
);
5973 /* Make sure we won't be right-shifting an extent past the maximum bound. */
5975 xfs_bmap_can_insert_extents(
5976 struct xfs_inode
*ip
,
5978 xfs_fileoff_t shift
)
5980 struct xfs_bmbt_irec got
;
5984 ASSERT(xfs_isilocked(ip
, XFS_IOLOCK_EXCL
));
5986 if (XFS_FORCED_SHUTDOWN(ip
->i_mount
))
5989 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
5990 error
= xfs_bmap_last_extent(NULL
, ip
, XFS_DATA_FORK
, &got
, &is_empty
);
5991 if (!error
&& !is_empty
&& got
.br_startoff
>= off
&&
5992 ((got
.br_startoff
+ shift
) & BMBT_STARTOFF_MASK
) < got
.br_startoff
)
5994 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
6000 xfs_bmap_insert_extents(
6001 struct xfs_trans
*tp
,
6002 struct xfs_inode
*ip
,
6003 xfs_fileoff_t
*next_fsb
,
6004 xfs_fileoff_t offset_shift_fsb
,
6006 xfs_fileoff_t stop_fsb
)
6008 int whichfork
= XFS_DATA_FORK
;
6009 struct xfs_mount
*mp
= ip
->i_mount
;
6010 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
6011 struct xfs_btree_cur
*cur
= NULL
;
6012 struct xfs_bmbt_irec got
, next
;
6013 struct xfs_iext_cursor icur
;
6014 xfs_fileoff_t new_startoff
;
6018 if (XFS_IS_CORRUPT(mp
, !xfs_ifork_has_extents(ifp
)) ||
6019 XFS_TEST_ERROR(false, mp
, XFS_ERRTAG_BMAPIFORMAT
)) {
6020 return -EFSCORRUPTED
;
6023 if (XFS_FORCED_SHUTDOWN(mp
))
6026 ASSERT(xfs_isilocked(ip
, XFS_IOLOCK_EXCL
| XFS_ILOCK_EXCL
));
6028 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
6029 error
= xfs_iread_extents(tp
, ip
, whichfork
);
6034 if (ifp
->if_flags
& XFS_IFBROOT
) {
6035 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
6036 cur
->bc_ino
.flags
= 0;
6039 if (*next_fsb
== NULLFSBLOCK
) {
6040 xfs_iext_last(ifp
, &icur
);
6041 if (!xfs_iext_get_extent(ifp
, &icur
, &got
) ||
6042 stop_fsb
> got
.br_startoff
) {
6047 if (!xfs_iext_lookup_extent(ip
, ifp
, *next_fsb
, &icur
, &got
)) {
6052 if (XFS_IS_CORRUPT(mp
, isnullstartblock(got
.br_startblock
))) {
6053 error
= -EFSCORRUPTED
;
6057 if (XFS_IS_CORRUPT(mp
, stop_fsb
> got
.br_startoff
)) {
6058 error
= -EFSCORRUPTED
;
6062 new_startoff
= got
.br_startoff
+ offset_shift_fsb
;
6063 if (xfs_iext_peek_next_extent(ifp
, &icur
, &next
)) {
6064 if (new_startoff
+ got
.br_blockcount
> next
.br_startoff
) {
6070 * Unlike a left shift (which involves a hole punch), a right
6071 * shift does not modify extent neighbors in any way. We should
6072 * never find mergeable extents in this scenario. Check anyways
6073 * and warn if we encounter two extents that could be one.
6075 if (xfs_bmse_can_merge(&got
, &next
, offset_shift_fsb
))
6079 error
= xfs_bmap_shift_update_extent(tp
, ip
, whichfork
, &icur
, &got
,
6080 cur
, &logflags
, new_startoff
);
6084 if (!xfs_iext_prev_extent(ifp
, &icur
, &got
) ||
6085 stop_fsb
>= got
.br_startoff
+ got
.br_blockcount
) {
6090 *next_fsb
= got
.br_startoff
;
6093 xfs_btree_del_cursor(cur
, error
);
6095 xfs_trans_log_inode(tp
, ip
, logflags
);
6100 * Splits an extent into two extents at split_fsb block such that it is the
6101 * first block of the current_ext. @ext is a target extent to be split.
6102 * @split_fsb is a block where the extents is split. If split_fsb lies in a
6103 * hole or the first block of extents, just return 0.
6106 xfs_bmap_split_extent(
6107 struct xfs_trans
*tp
,
6108 struct xfs_inode
*ip
,
6109 xfs_fileoff_t split_fsb
)
6111 int whichfork
= XFS_DATA_FORK
;
6112 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
6113 struct xfs_btree_cur
*cur
= NULL
;
6114 struct xfs_bmbt_irec got
;
6115 struct xfs_bmbt_irec
new; /* split extent */
6116 struct xfs_mount
*mp
= ip
->i_mount
;
6117 xfs_fsblock_t gotblkcnt
; /* new block count for got */
6118 struct xfs_iext_cursor icur
;
6123 if (XFS_IS_CORRUPT(mp
, !xfs_ifork_has_extents(ifp
)) ||
6124 XFS_TEST_ERROR(false, mp
, XFS_ERRTAG_BMAPIFORMAT
)) {
6125 return -EFSCORRUPTED
;
6128 if (XFS_FORCED_SHUTDOWN(mp
))
6131 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
6132 /* Read in all the extents */
6133 error
= xfs_iread_extents(tp
, ip
, whichfork
);
6139 * If there are not extents, or split_fsb lies in a hole we are done.
6141 if (!xfs_iext_lookup_extent(ip
, ifp
, split_fsb
, &icur
, &got
) ||
6142 got
.br_startoff
>= split_fsb
)
6145 gotblkcnt
= split_fsb
- got
.br_startoff
;
6146 new.br_startoff
= split_fsb
;
6147 new.br_startblock
= got
.br_startblock
+ gotblkcnt
;
6148 new.br_blockcount
= got
.br_blockcount
- gotblkcnt
;
6149 new.br_state
= got
.br_state
;
6151 if (ifp
->if_flags
& XFS_IFBROOT
) {
6152 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
6153 cur
->bc_ino
.flags
= 0;
6154 error
= xfs_bmbt_lookup_eq(cur
, &got
, &i
);
6157 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
6158 error
= -EFSCORRUPTED
;
6163 got
.br_blockcount
= gotblkcnt
;
6164 xfs_iext_update_extent(ip
, xfs_bmap_fork_to_state(whichfork
), &icur
,
6167 logflags
= XFS_ILOG_CORE
;
6169 error
= xfs_bmbt_update(cur
, &got
);
6173 logflags
|= XFS_ILOG_DEXT
;
6175 /* Add new extent */
6176 xfs_iext_next(ifp
, &icur
);
6177 xfs_iext_insert(ip
, &icur
, &new, 0);
6181 error
= xfs_bmbt_lookup_eq(cur
, &new, &i
);
6184 if (XFS_IS_CORRUPT(mp
, i
!= 0)) {
6185 error
= -EFSCORRUPTED
;
6188 error
= xfs_btree_insert(cur
, &i
);
6191 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
6192 error
= -EFSCORRUPTED
;
6198 * Convert to a btree if necessary.
6200 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
6201 int tmp_logflags
; /* partial log flag return val */
6203 ASSERT(cur
== NULL
);
6204 error
= xfs_bmap_extents_to_btree(tp
, ip
, &cur
, 0,
6205 &tmp_logflags
, whichfork
);
6206 logflags
|= tmp_logflags
;
6211 cur
->bc_ino
.allocated
= 0;
6212 xfs_btree_del_cursor(cur
, error
);
6216 xfs_trans_log_inode(tp
, ip
, logflags
);
6220 /* Deferred mapping is only for real extents in the data fork. */
6222 xfs_bmap_is_update_needed(
6223 struct xfs_bmbt_irec
*bmap
)
6225 return bmap
->br_startblock
!= HOLESTARTBLOCK
&&
6226 bmap
->br_startblock
!= DELAYSTARTBLOCK
;
6229 /* Record a bmap intent. */
6232 struct xfs_trans
*tp
,
6233 enum xfs_bmap_intent_type type
,
6234 struct xfs_inode
*ip
,
6236 struct xfs_bmbt_irec
*bmap
)
6238 struct xfs_bmap_intent
*bi
;
6240 trace_xfs_bmap_defer(tp
->t_mountp
,
6241 XFS_FSB_TO_AGNO(tp
->t_mountp
, bmap
->br_startblock
),
6243 XFS_FSB_TO_AGBNO(tp
->t_mountp
, bmap
->br_startblock
),
6244 ip
->i_ino
, whichfork
,
6246 bmap
->br_blockcount
,
6249 bi
= kmem_alloc(sizeof(struct xfs_bmap_intent
), KM_NOFS
);
6250 INIT_LIST_HEAD(&bi
->bi_list
);
6253 bi
->bi_whichfork
= whichfork
;
6254 bi
->bi_bmap
= *bmap
;
6256 xfs_defer_add(tp
, XFS_DEFER_OPS_TYPE_BMAP
, &bi
->bi_list
);
6260 /* Map an extent into a file. */
6262 xfs_bmap_map_extent(
6263 struct xfs_trans
*tp
,
6264 struct xfs_inode
*ip
,
6265 struct xfs_bmbt_irec
*PREV
)
6267 if (!xfs_bmap_is_update_needed(PREV
))
6270 __xfs_bmap_add(tp
, XFS_BMAP_MAP
, ip
, XFS_DATA_FORK
, PREV
);
6273 /* Unmap an extent out of a file. */
6275 xfs_bmap_unmap_extent(
6276 struct xfs_trans
*tp
,
6277 struct xfs_inode
*ip
,
6278 struct xfs_bmbt_irec
*PREV
)
6280 if (!xfs_bmap_is_update_needed(PREV
))
6283 __xfs_bmap_add(tp
, XFS_BMAP_UNMAP
, ip
, XFS_DATA_FORK
, PREV
);
6287 * Process one of the deferred bmap operations. We pass back the
6288 * btree cursor to maintain our lock on the bmapbt between calls.
6291 xfs_bmap_finish_one(
6292 struct xfs_trans
*tp
,
6293 struct xfs_inode
*ip
,
6294 enum xfs_bmap_intent_type type
,
6296 xfs_fileoff_t startoff
,
6297 xfs_fsblock_t startblock
,
6298 xfs_filblks_t
*blockcount
,
6303 ASSERT(tp
->t_firstblock
== NULLFSBLOCK
);
6305 trace_xfs_bmap_deferred(tp
->t_mountp
,
6306 XFS_FSB_TO_AGNO(tp
->t_mountp
, startblock
), type
,
6307 XFS_FSB_TO_AGBNO(tp
->t_mountp
, startblock
),
6308 ip
->i_ino
, whichfork
, startoff
, *blockcount
, state
);
6310 if (WARN_ON_ONCE(whichfork
!= XFS_DATA_FORK
))
6311 return -EFSCORRUPTED
;
6313 if (XFS_TEST_ERROR(false, tp
->t_mountp
,
6314 XFS_ERRTAG_BMAP_FINISH_ONE
))
6319 error
= xfs_bmapi_remap(tp
, ip
, startoff
, *blockcount
,
6323 case XFS_BMAP_UNMAP
:
6324 error
= __xfs_bunmapi(tp
, ip
, startoff
, blockcount
,
6325 XFS_BMAPI_REMAP
, 1);
6329 error
= -EFSCORRUPTED
;
6335 /* Check that an inode's extent does not have invalid flags or bad ranges. */
6337 xfs_bmap_validate_extent(
6338 struct xfs_inode
*ip
,
6340 struct xfs_bmbt_irec
*irec
)
6342 struct xfs_mount
*mp
= ip
->i_mount
;
6344 if (!xfs_verify_fileext(mp
, irec
->br_startoff
, irec
->br_blockcount
))
6345 return __this_address
;
6347 if (XFS_IS_REALTIME_INODE(ip
) && whichfork
== XFS_DATA_FORK
) {
6348 if (!xfs_verify_rtext(mp
, irec
->br_startblock
,
6349 irec
->br_blockcount
))
6350 return __this_address
;
6352 if (!xfs_verify_fsbext(mp
, irec
->br_startblock
,
6353 irec
->br_blockcount
))
6354 return __this_address
;
6356 if (irec
->br_state
!= XFS_EXT_NORM
&& whichfork
!= XFS_DATA_FORK
)
6357 return __this_address
;