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
58 * number of leaf entries, is controlled by the type of di_nextents
59 * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
60 * (a signed 16-bit number, xfs_aextnum_t).
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 return whichfork
!= XFS_COW_FORK
&&
117 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
&&
118 XFS_IFORK_NEXTENTS(ip
, whichfork
) >
119 XFS_IFORK_MAXEXT(ip
, whichfork
);
123 * Check if the inode should be converted to extent format.
125 static inline bool xfs_bmap_wants_extents(struct xfs_inode
*ip
, int whichfork
)
127 return whichfork
!= XFS_COW_FORK
&&
128 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
&&
129 XFS_IFORK_NEXTENTS(ip
, whichfork
) <=
130 XFS_IFORK_MAXEXT(ip
, whichfork
);
134 * Update the record referred to by cur to the value given by irec
135 * This either works (return 0) or gets an EFSCORRUPTED error.
139 struct xfs_btree_cur
*cur
,
140 struct xfs_bmbt_irec
*irec
)
142 union xfs_btree_rec rec
;
144 xfs_bmbt_disk_set_all(&rec
.bmbt
, irec
);
145 return xfs_btree_update(cur
, &rec
);
149 * Compute the worst-case number of indirect blocks that will be used
150 * for ip's delayed extent of length "len".
153 xfs_bmap_worst_indlen(
154 xfs_inode_t
*ip
, /* incore inode pointer */
155 xfs_filblks_t len
) /* delayed extent length */
157 int level
; /* btree level number */
158 int maxrecs
; /* maximum record count at this level */
159 xfs_mount_t
*mp
; /* mount structure */
160 xfs_filblks_t rval
; /* return value */
163 maxrecs
= mp
->m_bmap_dmxr
[0];
164 for (level
= 0, rval
= 0;
165 level
< XFS_BM_MAXLEVELS(mp
, XFS_DATA_FORK
);
168 do_div(len
, maxrecs
);
171 return rval
+ XFS_BM_MAXLEVELS(mp
, XFS_DATA_FORK
) -
174 maxrecs
= mp
->m_bmap_dmxr
[1];
180 * Calculate the default attribute fork offset for newly created inodes.
183 xfs_default_attroffset(
184 struct xfs_inode
*ip
)
186 struct xfs_mount
*mp
= ip
->i_mount
;
189 if (mp
->m_sb
.sb_inodesize
== 256) {
190 offset
= XFS_LITINO(mp
, ip
->i_d
.di_version
) -
191 XFS_BMDR_SPACE_CALC(MINABTPTRS
);
193 offset
= XFS_BMDR_SPACE_CALC(6 * MINABTPTRS
);
196 ASSERT(offset
< XFS_LITINO(mp
, ip
->i_d
.di_version
));
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_d
.di_format
!= XFS_DINODE_FMT_DEV
&&
212 ip
->i_d
.di_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_btree_block
*block
; /* current btree block */
314 xfs_fsblock_t bno
; /* block # of "block" */
315 xfs_buf_t
*bp
; /* buffer for "block" */
316 int error
; /* error return value */
317 xfs_extnum_t i
=0, j
; /* index into the extents list */
318 struct xfs_ifork
*ifp
; /* fork structure */
319 int level
; /* btree level, for checking */
320 xfs_mount_t
*mp
; /* file system mount structure */
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 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
) {
331 /* skip large extent count inodes */
332 if (ip
->i_d
.di_nextents
> 10000)
337 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
338 block
= ifp
->if_broot
;
340 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
342 level
= be16_to_cpu(block
->bb_level
);
344 xfs_check_block(block
, mp
, 1, ifp
->if_broot_bytes
);
345 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, 1, ifp
->if_broot_bytes
);
346 bno
= be64_to_cpu(*pp
);
348 ASSERT(bno
!= NULLFSBLOCK
);
349 ASSERT(XFS_FSB_TO_AGNO(mp
, bno
) < mp
->m_sb
.sb_agcount
);
350 ASSERT(XFS_FSB_TO_AGBNO(mp
, bno
) < mp
->m_sb
.sb_agblocks
);
353 * Go down the tree until leaf level is reached, following the first
354 * pointer (leftmost) at each level.
356 while (level
-- > 0) {
357 /* See if buf is in cur first */
359 bp
= xfs_bmap_get_bp(cur
, XFS_FSB_TO_DADDR(mp
, bno
));
362 error
= xfs_btree_read_bufl(mp
, NULL
, bno
, &bp
,
368 block
= XFS_BUF_TO_BLOCK(bp
);
373 * Check this block for basic sanity (increasing keys and
374 * no duplicate blocks).
377 xfs_check_block(block
, mp
, 0, 0);
378 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, mp
->m_bmap_dmxr
[1]);
379 bno
= be64_to_cpu(*pp
);
380 if (XFS_IS_CORRUPT(mp
, !xfs_verify_fsbno(mp
, bno
))) {
381 error
= -EFSCORRUPTED
;
386 xfs_trans_brelse(NULL
, bp
);
391 * Here with bp and block set to the leftmost leaf node in the tree.
396 * Loop over all leaf nodes checking that all extents are in the right order.
399 xfs_fsblock_t nextbno
;
400 xfs_extnum_t num_recs
;
403 num_recs
= xfs_btree_get_numrecs(block
);
406 * Read-ahead the next leaf block, if any.
409 nextbno
= be64_to_cpu(block
->bb_u
.l
.bb_rightsib
);
412 * Check all the extents to make sure they are OK.
413 * If we had a previous block, the last entry should
414 * conform with the first entry in this one.
417 ep
= XFS_BMBT_REC_ADDR(mp
, block
, 1);
419 ASSERT(xfs_bmbt_disk_get_startoff(&last
) +
420 xfs_bmbt_disk_get_blockcount(&last
) <=
421 xfs_bmbt_disk_get_startoff(ep
));
423 for (j
= 1; j
< num_recs
; j
++) {
424 nextp
= XFS_BMBT_REC_ADDR(mp
, block
, j
+ 1);
425 ASSERT(xfs_bmbt_disk_get_startoff(ep
) +
426 xfs_bmbt_disk_get_blockcount(ep
) <=
427 xfs_bmbt_disk_get_startoff(nextp
));
435 xfs_trans_brelse(NULL
, bp
);
439 * If we've reached the end, stop.
441 if (bno
== NULLFSBLOCK
)
445 bp
= xfs_bmap_get_bp(cur
, XFS_FSB_TO_DADDR(mp
, bno
));
448 error
= xfs_btree_read_bufl(mp
, NULL
, bno
, &bp
,
454 block
= XFS_BUF_TO_BLOCK(bp
);
460 xfs_warn(mp
, "%s: at error0", __func__
);
462 xfs_trans_brelse(NULL
, bp
);
464 xfs_warn(mp
, "%s: BAD after btree leaves for %d extents",
466 xfs_err(mp
, "%s: CORRUPTED BTREE OR SOMETHING", __func__
);
467 xfs_force_shutdown(mp
, SHUTDOWN_CORRUPT_INCORE
);
472 * Validate that the bmbt_irecs being returned from bmapi are valid
473 * given the caller's original parameters. Specifically check the
474 * ranges of the returned irecs to ensure that they only extend beyond
475 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
478 xfs_bmap_validate_ret(
482 xfs_bmbt_irec_t
*mval
,
486 int i
; /* index to map values */
488 ASSERT(ret_nmap
<= nmap
);
490 for (i
= 0; i
< ret_nmap
; i
++) {
491 ASSERT(mval
[i
].br_blockcount
> 0);
492 if (!(flags
& XFS_BMAPI_ENTIRE
)) {
493 ASSERT(mval
[i
].br_startoff
>= bno
);
494 ASSERT(mval
[i
].br_blockcount
<= len
);
495 ASSERT(mval
[i
].br_startoff
+ mval
[i
].br_blockcount
<=
498 ASSERT(mval
[i
].br_startoff
< bno
+ len
);
499 ASSERT(mval
[i
].br_startoff
+ mval
[i
].br_blockcount
>
503 mval
[i
- 1].br_startoff
+ mval
[i
- 1].br_blockcount
==
504 mval
[i
].br_startoff
);
505 ASSERT(mval
[i
].br_startblock
!= DELAYSTARTBLOCK
&&
506 mval
[i
].br_startblock
!= HOLESTARTBLOCK
);
507 ASSERT(mval
[i
].br_state
== XFS_EXT_NORM
||
508 mval
[i
].br_state
== XFS_EXT_UNWRITTEN
);
513 #define xfs_bmap_check_leaf_extents(cur, ip, whichfork) do { } while (0)
514 #define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap) do { } while (0)
518 * bmap free list manipulation functions
522 * Add the extent to the list of extents to be free at transaction end.
523 * The list is maintained sorted (by block number).
527 struct xfs_trans
*tp
,
530 const struct xfs_owner_info
*oinfo
,
533 struct xfs_extent_free_item
*new; /* new element */
535 struct xfs_mount
*mp
= tp
->t_mountp
;
539 ASSERT(bno
!= NULLFSBLOCK
);
541 ASSERT(len
<= MAXEXTLEN
);
542 ASSERT(!isnullstartblock(bno
));
543 agno
= XFS_FSB_TO_AGNO(mp
, bno
);
544 agbno
= XFS_FSB_TO_AGBNO(mp
, bno
);
545 ASSERT(agno
< mp
->m_sb
.sb_agcount
);
546 ASSERT(agbno
< mp
->m_sb
.sb_agblocks
);
547 ASSERT(len
< mp
->m_sb
.sb_agblocks
);
548 ASSERT(agbno
+ len
<= mp
->m_sb
.sb_agblocks
);
550 ASSERT(xfs_bmap_free_item_zone
!= NULL
);
552 new = kmem_zone_alloc(xfs_bmap_free_item_zone
, 0);
553 new->xefi_startblock
= bno
;
554 new->xefi_blockcount
= (xfs_extlen_t
)len
;
556 new->xefi_oinfo
= *oinfo
;
558 new->xefi_oinfo
= XFS_RMAP_OINFO_SKIP_UPDATE
;
559 new->xefi_skip_discard
= skip_discard
;
560 trace_xfs_bmap_free_defer(tp
->t_mountp
,
561 XFS_FSB_TO_AGNO(tp
->t_mountp
, bno
), 0,
562 XFS_FSB_TO_AGBNO(tp
->t_mountp
, bno
), len
);
563 xfs_defer_add(tp
, XFS_DEFER_OPS_TYPE_FREE
, &new->xefi_list
);
567 * Inode fork format manipulation functions
571 * Convert the inode format to extent format if it currently is in btree format,
572 * but the extent list is small enough that it fits into the extent format.
574 * Since the extents are already in-core, all we have to do is give up the space
575 * for the btree root and pitch the leaf block.
577 STATIC
int /* error */
578 xfs_bmap_btree_to_extents(
579 struct xfs_trans
*tp
, /* transaction pointer */
580 struct xfs_inode
*ip
, /* incore inode pointer */
581 struct xfs_btree_cur
*cur
, /* btree cursor */
582 int *logflagsp
, /* inode logging flags */
583 int whichfork
) /* data or attr fork */
585 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
586 struct xfs_mount
*mp
= ip
->i_mount
;
587 struct xfs_btree_block
*rblock
= ifp
->if_broot
;
588 struct xfs_btree_block
*cblock
;/* child btree block */
589 xfs_fsblock_t cbno
; /* child block number */
590 xfs_buf_t
*cbp
; /* child block's buffer */
591 int error
; /* error return value */
592 __be64
*pp
; /* ptr to block address */
593 struct xfs_owner_info oinfo
;
595 /* check if we actually need the extent format first: */
596 if (!xfs_bmap_wants_extents(ip
, whichfork
))
600 ASSERT(whichfork
!= XFS_COW_FORK
);
601 ASSERT(ifp
->if_flags
& XFS_IFEXTENTS
);
602 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
);
603 ASSERT(be16_to_cpu(rblock
->bb_level
) == 1);
604 ASSERT(be16_to_cpu(rblock
->bb_numrecs
) == 1);
605 ASSERT(xfs_bmbt_maxrecs(mp
, ifp
->if_broot_bytes
, 0) == 1);
607 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, rblock
, 1, ifp
->if_broot_bytes
);
608 cbno
= be64_to_cpu(*pp
);
610 if (XFS_IS_CORRUPT(cur
->bc_mp
, !xfs_btree_check_lptr(cur
, cbno
, 1)))
611 return -EFSCORRUPTED
;
613 error
= xfs_btree_read_bufl(mp
, tp
, cbno
, &cbp
, XFS_BMAP_BTREE_REF
,
617 cblock
= XFS_BUF_TO_BLOCK(cbp
);
618 if ((error
= xfs_btree_check_block(cur
, cblock
, 0, cbp
)))
620 xfs_rmap_ino_bmbt_owner(&oinfo
, ip
->i_ino
, whichfork
);
621 xfs_bmap_add_free(cur
->bc_tp
, cbno
, 1, &oinfo
);
622 ip
->i_d
.di_nblocks
--;
623 xfs_trans_mod_dquot_byino(tp
, ip
, XFS_TRANS_DQ_BCOUNT
, -1L);
624 xfs_trans_binval(tp
, cbp
);
625 if (cur
->bc_bufs
[0] == cbp
)
626 cur
->bc_bufs
[0] = NULL
;
627 xfs_iroot_realloc(ip
, -1, whichfork
);
628 ASSERT(ifp
->if_broot
== NULL
);
629 ASSERT((ifp
->if_flags
& XFS_IFBROOT
) == 0);
630 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_EXTENTS
);
631 *logflagsp
|= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
636 * Convert an extents-format file into a btree-format file.
637 * The new file will have a root block (in the inode) and a single child block.
639 STATIC
int /* error */
640 xfs_bmap_extents_to_btree(
641 struct xfs_trans
*tp
, /* transaction pointer */
642 struct xfs_inode
*ip
, /* incore inode pointer */
643 struct xfs_btree_cur
**curp
, /* cursor returned to caller */
644 int wasdel
, /* converting a delayed alloc */
645 int *logflagsp
, /* inode logging flags */
646 int whichfork
) /* data or attr fork */
648 struct xfs_btree_block
*ablock
; /* allocated (child) bt block */
649 struct xfs_buf
*abp
; /* buffer for ablock */
650 struct xfs_alloc_arg args
; /* allocation arguments */
651 struct xfs_bmbt_rec
*arp
; /* child record pointer */
652 struct xfs_btree_block
*block
; /* btree root block */
653 struct xfs_btree_cur
*cur
; /* bmap btree cursor */
654 int error
; /* error return value */
655 struct xfs_ifork
*ifp
; /* inode fork pointer */
656 struct xfs_bmbt_key
*kp
; /* root block key pointer */
657 struct xfs_mount
*mp
; /* mount structure */
658 xfs_bmbt_ptr_t
*pp
; /* root block address pointer */
659 struct xfs_iext_cursor icur
;
660 struct xfs_bmbt_irec rec
;
661 xfs_extnum_t cnt
= 0;
664 ASSERT(whichfork
!= XFS_COW_FORK
);
665 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
666 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
);
669 * Make space in the inode incore. This needs to be undone if we fail
670 * to expand the root.
672 xfs_iroot_realloc(ip
, 1, whichfork
);
673 ifp
->if_flags
|= XFS_IFBROOT
;
678 block
= ifp
->if_broot
;
679 xfs_btree_init_block_int(mp
, block
, XFS_BUF_DADDR_NULL
,
680 XFS_BTNUM_BMAP
, 1, 1, ip
->i_ino
,
681 XFS_BTREE_LONG_PTRS
);
683 * Need a cursor. Can't allocate until bb_level is filled in.
685 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
686 cur
->bc_private
.b
.flags
= wasdel
? XFS_BTCUR_BPRV_WASDEL
: 0;
688 * Convert to a btree with two levels, one record in root.
690 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_BTREE
);
691 memset(&args
, 0, sizeof(args
));
694 xfs_rmap_ino_bmbt_owner(&args
.oinfo
, ip
->i_ino
, whichfork
);
695 if (tp
->t_firstblock
== NULLFSBLOCK
) {
696 args
.type
= XFS_ALLOCTYPE_START_BNO
;
697 args
.fsbno
= XFS_INO_TO_FSB(mp
, ip
->i_ino
);
698 } else if (tp
->t_flags
& XFS_TRANS_LOWMODE
) {
699 args
.type
= XFS_ALLOCTYPE_START_BNO
;
700 args
.fsbno
= tp
->t_firstblock
;
702 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
703 args
.fsbno
= tp
->t_firstblock
;
705 args
.minlen
= args
.maxlen
= args
.prod
= 1;
706 args
.wasdel
= wasdel
;
708 error
= xfs_alloc_vextent(&args
);
710 goto out_root_realloc
;
712 if (WARN_ON_ONCE(args
.fsbno
== NULLFSBLOCK
)) {
714 goto out_root_realloc
;
718 * Allocation can't fail, the space was reserved.
720 ASSERT(tp
->t_firstblock
== NULLFSBLOCK
||
721 args
.agno
>= XFS_FSB_TO_AGNO(mp
, tp
->t_firstblock
));
722 tp
->t_firstblock
= args
.fsbno
;
723 cur
->bc_private
.b
.allocated
++;
724 ip
->i_d
.di_nblocks
++;
725 xfs_trans_mod_dquot_byino(tp
, ip
, XFS_TRANS_DQ_BCOUNT
, 1L);
726 error
= xfs_trans_get_buf(tp
, mp
->m_ddev_targp
,
727 XFS_FSB_TO_DADDR(mp
, args
.fsbno
),
728 mp
->m_bsize
, 0, &abp
);
730 goto out_unreserve_dquot
;
733 * Fill in the child block.
735 abp
->b_ops
= &xfs_bmbt_buf_ops
;
736 ablock
= XFS_BUF_TO_BLOCK(abp
);
737 xfs_btree_init_block_int(mp
, ablock
, abp
->b_bn
,
738 XFS_BTNUM_BMAP
, 0, 0, ip
->i_ino
,
739 XFS_BTREE_LONG_PTRS
);
741 for_each_xfs_iext(ifp
, &icur
, &rec
) {
742 if (isnullstartblock(rec
.br_startblock
))
744 arp
= XFS_BMBT_REC_ADDR(mp
, ablock
, 1 + cnt
);
745 xfs_bmbt_disk_set_all(arp
, &rec
);
748 ASSERT(cnt
== XFS_IFORK_NEXTENTS(ip
, whichfork
));
749 xfs_btree_set_numrecs(ablock
, cnt
);
752 * Fill in the root key and pointer.
754 kp
= XFS_BMBT_KEY_ADDR(mp
, block
, 1);
755 arp
= XFS_BMBT_REC_ADDR(mp
, ablock
, 1);
756 kp
->br_startoff
= cpu_to_be64(xfs_bmbt_disk_get_startoff(arp
));
757 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, xfs_bmbt_get_maxrecs(cur
,
758 be16_to_cpu(block
->bb_level
)));
759 *pp
= cpu_to_be64(args
.fsbno
);
762 * Do all this logging at the end so that
763 * the root is at the right level.
765 xfs_btree_log_block(cur
, abp
, XFS_BB_ALL_BITS
);
766 xfs_btree_log_recs(cur
, abp
, 1, be16_to_cpu(ablock
->bb_numrecs
));
767 ASSERT(*curp
== NULL
);
769 *logflagsp
= XFS_ILOG_CORE
| xfs_ilog_fbroot(whichfork
);
773 xfs_trans_mod_dquot_byino(tp
, ip
, XFS_TRANS_DQ_BCOUNT
, -1L);
775 xfs_iroot_realloc(ip
, -1, whichfork
);
776 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_EXTENTS
);
777 ASSERT(ifp
->if_broot
== NULL
);
778 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
784 * Convert a local file to an extents file.
785 * This code is out of bounds for data forks of regular files,
786 * since the file data needs to get logged so things will stay consistent.
787 * (The bmap-level manipulations are ok, though).
790 xfs_bmap_local_to_extents_empty(
791 struct xfs_trans
*tp
,
792 struct xfs_inode
*ip
,
795 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
797 ASSERT(whichfork
!= XFS_COW_FORK
);
798 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
799 ASSERT(ifp
->if_bytes
== 0);
800 ASSERT(XFS_IFORK_NEXTENTS(ip
, whichfork
) == 0);
802 xfs_bmap_forkoff_reset(ip
, whichfork
);
803 ifp
->if_flags
&= ~XFS_IFINLINE
;
804 ifp
->if_flags
|= XFS_IFEXTENTS
;
805 ifp
->if_u1
.if_root
= NULL
;
807 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_EXTENTS
);
808 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
812 STATIC
int /* error */
813 xfs_bmap_local_to_extents(
814 xfs_trans_t
*tp
, /* transaction pointer */
815 xfs_inode_t
*ip
, /* incore inode pointer */
816 xfs_extlen_t total
, /* total blocks needed by transaction */
817 int *logflagsp
, /* inode logging flags */
819 void (*init_fn
)(struct xfs_trans
*tp
,
821 struct xfs_inode
*ip
,
822 struct xfs_ifork
*ifp
))
825 int flags
; /* logging flags returned */
826 struct xfs_ifork
*ifp
; /* inode fork pointer */
827 xfs_alloc_arg_t args
; /* allocation arguments */
828 xfs_buf_t
*bp
; /* buffer for extent block */
829 struct xfs_bmbt_irec rec
;
830 struct xfs_iext_cursor icur
;
833 * We don't want to deal with the case of keeping inode data inline yet.
834 * So sending the data fork of a regular inode is invalid.
836 ASSERT(!(S_ISREG(VFS_I(ip
)->i_mode
) && whichfork
== XFS_DATA_FORK
));
837 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
838 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
840 if (!ifp
->if_bytes
) {
841 xfs_bmap_local_to_extents_empty(tp
, ip
, whichfork
);
842 flags
= XFS_ILOG_CORE
;
848 ASSERT((ifp
->if_flags
& (XFS_IFINLINE
|XFS_IFEXTENTS
)) == XFS_IFINLINE
);
849 memset(&args
, 0, sizeof(args
));
851 args
.mp
= ip
->i_mount
;
852 xfs_rmap_ino_owner(&args
.oinfo
, ip
->i_ino
, whichfork
, 0);
854 * Allocate a block. We know we need only one, since the
855 * file currently fits in an inode.
857 if (tp
->t_firstblock
== NULLFSBLOCK
) {
858 args
.fsbno
= XFS_INO_TO_FSB(args
.mp
, ip
->i_ino
);
859 args
.type
= XFS_ALLOCTYPE_START_BNO
;
861 args
.fsbno
= tp
->t_firstblock
;
862 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
865 args
.minlen
= args
.maxlen
= args
.prod
= 1;
866 error
= xfs_alloc_vextent(&args
);
870 /* Can't fail, the space was reserved. */
871 ASSERT(args
.fsbno
!= NULLFSBLOCK
);
872 ASSERT(args
.len
== 1);
873 tp
->t_firstblock
= args
.fsbno
;
874 error
= xfs_trans_get_buf(tp
, args
.mp
->m_ddev_targp
,
875 XFS_FSB_TO_DADDR(args
.mp
, args
.fsbno
),
876 args
.mp
->m_bsize
, 0, &bp
);
881 * Initialize the block, copy the data and log the remote buffer.
883 * The callout is responsible for logging because the remote format
884 * might differ from the local format and thus we don't know how much to
885 * log here. Note that init_fn must also set the buffer log item type
888 init_fn(tp
, bp
, ip
, ifp
);
890 /* account for the change in fork size */
891 xfs_idata_realloc(ip
, -ifp
->if_bytes
, whichfork
);
892 xfs_bmap_local_to_extents_empty(tp
, ip
, whichfork
);
893 flags
|= XFS_ILOG_CORE
;
895 ifp
->if_u1
.if_root
= NULL
;
899 rec
.br_startblock
= args
.fsbno
;
900 rec
.br_blockcount
= 1;
901 rec
.br_state
= XFS_EXT_NORM
;
902 xfs_iext_first(ifp
, &icur
);
903 xfs_iext_insert(ip
, &icur
, &rec
, 0);
905 XFS_IFORK_NEXT_SET(ip
, whichfork
, 1);
906 ip
->i_d
.di_nblocks
= 1;
907 xfs_trans_mod_dquot_byino(tp
, ip
,
908 XFS_TRANS_DQ_BCOUNT
, 1L);
909 flags
|= xfs_ilog_fext(whichfork
);
917 * Called from xfs_bmap_add_attrfork to handle btree format files.
919 STATIC
int /* error */
920 xfs_bmap_add_attrfork_btree(
921 xfs_trans_t
*tp
, /* transaction pointer */
922 xfs_inode_t
*ip
, /* incore inode pointer */
923 int *flags
) /* inode logging flags */
925 xfs_btree_cur_t
*cur
; /* btree cursor */
926 int error
; /* error return value */
927 xfs_mount_t
*mp
; /* file system mount struct */
928 int stat
; /* newroot status */
931 if (ip
->i_df
.if_broot_bytes
<= XFS_IFORK_DSIZE(ip
))
932 *flags
|= XFS_ILOG_DBROOT
;
934 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, XFS_DATA_FORK
);
935 error
= xfs_bmbt_lookup_first(cur
, &stat
);
938 /* must be at least one entry */
939 if (XFS_IS_CORRUPT(mp
, stat
!= 1)) {
940 error
= -EFSCORRUPTED
;
943 if ((error
= xfs_btree_new_iroot(cur
, flags
, &stat
)))
946 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
949 cur
->bc_private
.b
.allocated
= 0;
950 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
954 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
959 * Called from xfs_bmap_add_attrfork to handle extents format files.
961 STATIC
int /* error */
962 xfs_bmap_add_attrfork_extents(
963 struct xfs_trans
*tp
, /* transaction pointer */
964 struct xfs_inode
*ip
, /* incore inode pointer */
965 int *flags
) /* inode logging flags */
967 xfs_btree_cur_t
*cur
; /* bmap btree cursor */
968 int error
; /* error return value */
970 if (ip
->i_d
.di_nextents
* sizeof(xfs_bmbt_rec_t
) <= XFS_IFORK_DSIZE(ip
))
973 error
= xfs_bmap_extents_to_btree(tp
, ip
, &cur
, 0, flags
,
976 cur
->bc_private
.b
.allocated
= 0;
977 xfs_btree_del_cursor(cur
, error
);
983 * Called from xfs_bmap_add_attrfork to handle local format files. Each
984 * different data fork content type needs a different callout to do the
985 * conversion. Some are basic and only require special block initialisation
986 * callouts for the data formating, others (directories) are so specialised they
987 * handle everything themselves.
989 * XXX (dgc): investigate whether directory conversion can use the generic
990 * formatting callout. It should be possible - it's just a very complex
993 STATIC
int /* error */
994 xfs_bmap_add_attrfork_local(
995 struct xfs_trans
*tp
, /* transaction pointer */
996 struct xfs_inode
*ip
, /* incore inode pointer */
997 int *flags
) /* inode logging flags */
999 struct xfs_da_args dargs
; /* args for dir/attr code */
1001 if (ip
->i_df
.if_bytes
<= XFS_IFORK_DSIZE(ip
))
1004 if (S_ISDIR(VFS_I(ip
)->i_mode
)) {
1005 memset(&dargs
, 0, sizeof(dargs
));
1006 dargs
.geo
= ip
->i_mount
->m_dir_geo
;
1008 dargs
.total
= dargs
.geo
->fsbcount
;
1009 dargs
.whichfork
= XFS_DATA_FORK
;
1011 return xfs_dir2_sf_to_block(&dargs
);
1014 if (S_ISLNK(VFS_I(ip
)->i_mode
))
1015 return xfs_bmap_local_to_extents(tp
, ip
, 1, flags
,
1017 xfs_symlink_local_to_remote
);
1019 /* should only be called for types that support local format data */
1021 return -EFSCORRUPTED
;
1024 /* Set an inode attr fork off based on the format */
1026 xfs_bmap_set_attrforkoff(
1027 struct xfs_inode
*ip
,
1031 switch (ip
->i_d
.di_format
) {
1032 case XFS_DINODE_FMT_DEV
:
1033 ip
->i_d
.di_forkoff
= roundup(sizeof(xfs_dev_t
), 8) >> 3;
1035 case XFS_DINODE_FMT_LOCAL
:
1036 case XFS_DINODE_FMT_EXTENTS
:
1037 case XFS_DINODE_FMT_BTREE
:
1038 ip
->i_d
.di_forkoff
= xfs_attr_shortform_bytesfit(ip
, size
);
1039 if (!ip
->i_d
.di_forkoff
)
1040 ip
->i_d
.di_forkoff
= xfs_default_attroffset(ip
) >> 3;
1041 else if ((ip
->i_mount
->m_flags
& XFS_MOUNT_ATTR2
) && version
)
1053 * Convert inode from non-attributed to attributed.
1054 * Must not be in a transaction, ip must not be locked.
1056 int /* error code */
1057 xfs_bmap_add_attrfork(
1058 xfs_inode_t
*ip
, /* incore inode pointer */
1059 int size
, /* space new attribute needs */
1060 int rsvd
) /* xact may use reserved blks */
1062 xfs_mount_t
*mp
; /* mount structure */
1063 xfs_trans_t
*tp
; /* transaction pointer */
1064 int blks
; /* space reservation */
1065 int version
= 1; /* superblock attr version */
1066 int logflags
; /* logging flags */
1067 int error
; /* error return value */
1069 ASSERT(XFS_IFORK_Q(ip
) == 0);
1072 ASSERT(!XFS_NOT_DQATTACHED(mp
, ip
));
1074 blks
= XFS_ADDAFORK_SPACE_RES(mp
);
1076 error
= xfs_trans_alloc(mp
, &M_RES(mp
)->tr_addafork
, blks
, 0,
1077 rsvd
? XFS_TRANS_RESERVE
: 0, &tp
);
1081 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
1082 error
= xfs_trans_reserve_quota_nblks(tp
, ip
, blks
, 0, rsvd
?
1083 XFS_QMOPT_RES_REGBLKS
| XFS_QMOPT_FORCE_RES
:
1084 XFS_QMOPT_RES_REGBLKS
);
1087 if (XFS_IFORK_Q(ip
))
1089 if (XFS_IS_CORRUPT(mp
, ip
->i_d
.di_anextents
!= 0)) {
1090 error
= -EFSCORRUPTED
;
1093 if (ip
->i_d
.di_aformat
!= XFS_DINODE_FMT_EXTENTS
) {
1095 * For inodes coming from pre-6.2 filesystems.
1097 ASSERT(ip
->i_d
.di_aformat
== 0);
1098 ip
->i_d
.di_aformat
= XFS_DINODE_FMT_EXTENTS
;
1101 xfs_trans_ijoin(tp
, ip
, 0);
1102 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
1103 error
= xfs_bmap_set_attrforkoff(ip
, size
, &version
);
1106 ASSERT(ip
->i_afp
== NULL
);
1107 ip
->i_afp
= kmem_zone_zalloc(xfs_ifork_zone
, 0);
1108 ip
->i_afp
->if_flags
= XFS_IFEXTENTS
;
1110 switch (ip
->i_d
.di_format
) {
1111 case XFS_DINODE_FMT_LOCAL
:
1112 error
= xfs_bmap_add_attrfork_local(tp
, ip
, &logflags
);
1114 case XFS_DINODE_FMT_EXTENTS
:
1115 error
= xfs_bmap_add_attrfork_extents(tp
, ip
, &logflags
);
1117 case XFS_DINODE_FMT_BTREE
:
1118 error
= xfs_bmap_add_attrfork_btree(tp
, ip
, &logflags
);
1125 xfs_trans_log_inode(tp
, ip
, logflags
);
1128 if (!xfs_sb_version_hasattr(&mp
->m_sb
) ||
1129 (!xfs_sb_version_hasattr2(&mp
->m_sb
) && version
== 2)) {
1130 bool log_sb
= false;
1132 spin_lock(&mp
->m_sb_lock
);
1133 if (!xfs_sb_version_hasattr(&mp
->m_sb
)) {
1134 xfs_sb_version_addattr(&mp
->m_sb
);
1137 if (!xfs_sb_version_hasattr2(&mp
->m_sb
) && version
== 2) {
1138 xfs_sb_version_addattr2(&mp
->m_sb
);
1141 spin_unlock(&mp
->m_sb_lock
);
1146 error
= xfs_trans_commit(tp
);
1147 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
1151 xfs_trans_cancel(tp
);
1152 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
1157 * Internal and external extent tree search functions.
1160 struct xfs_iread_state
{
1161 struct xfs_iext_cursor icur
;
1162 xfs_extnum_t loaded
;
1165 /* Stuff every bmbt record from this block into the incore extent map. */
1167 xfs_iread_bmbt_block(
1168 struct xfs_btree_cur
*cur
,
1172 struct xfs_iread_state
*ir
= priv
;
1173 struct xfs_mount
*mp
= cur
->bc_mp
;
1174 struct xfs_inode
*ip
= cur
->bc_private
.b
.ip
;
1175 struct xfs_btree_block
*block
;
1177 struct xfs_bmbt_rec
*frp
;
1178 xfs_extnum_t num_recs
;
1180 int whichfork
= cur
->bc_private
.b
.whichfork
;
1182 block
= xfs_btree_get_block(cur
, level
, &bp
);
1184 /* Abort if we find more records than nextents. */
1185 num_recs
= xfs_btree_get_numrecs(block
);
1186 if (unlikely(ir
->loaded
+ num_recs
>
1187 XFS_IFORK_NEXTENTS(ip
, whichfork
))) {
1188 xfs_warn(ip
->i_mount
, "corrupt dinode %llu, (btree extents).",
1189 (unsigned long long)ip
->i_ino
);
1190 xfs_inode_verifier_error(ip
, -EFSCORRUPTED
, __func__
, block
,
1191 sizeof(*block
), __this_address
);
1192 return -EFSCORRUPTED
;
1195 /* Copy records into the incore cache. */
1196 frp
= XFS_BMBT_REC_ADDR(mp
, block
, 1);
1197 for (j
= 0; j
< num_recs
; j
++, frp
++, ir
->loaded
++) {
1198 struct xfs_bmbt_irec
new;
1201 xfs_bmbt_disk_get_all(frp
, &new);
1202 fa
= xfs_bmap_validate_extent(ip
, whichfork
, &new);
1204 xfs_inode_verifier_error(ip
, -EFSCORRUPTED
,
1205 "xfs_iread_extents(2)", frp
,
1207 return -EFSCORRUPTED
;
1209 xfs_iext_insert(ip
, &ir
->icur
, &new,
1210 xfs_bmap_fork_to_state(whichfork
));
1211 trace_xfs_read_extent(ip
, &ir
->icur
,
1212 xfs_bmap_fork_to_state(whichfork
), _THIS_IP_
);
1213 xfs_iext_next(XFS_IFORK_PTR(ip
, whichfork
), &ir
->icur
);
1220 * Read in extents from a btree-format inode.
1224 struct xfs_trans
*tp
,
1225 struct xfs_inode
*ip
,
1228 struct xfs_iread_state ir
;
1229 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1230 struct xfs_mount
*mp
= ip
->i_mount
;
1231 struct xfs_btree_cur
*cur
;
1234 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
1236 if (XFS_IS_CORRUPT(mp
,
1237 XFS_IFORK_FORMAT(ip
, whichfork
) !=
1238 XFS_DINODE_FMT_BTREE
)) {
1239 error
= -EFSCORRUPTED
;
1244 xfs_iext_first(ifp
, &ir
.icur
);
1245 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
1246 error
= xfs_btree_visit_blocks(cur
, xfs_iread_bmbt_block
,
1247 XFS_BTREE_VISIT_RECORDS
, &ir
);
1248 xfs_btree_del_cursor(cur
, error
);
1252 if (XFS_IS_CORRUPT(mp
,
1253 ir
.loaded
!= XFS_IFORK_NEXTENTS(ip
, whichfork
))) {
1254 error
= -EFSCORRUPTED
;
1257 ASSERT(ir
.loaded
== xfs_iext_count(ifp
));
1259 ifp
->if_flags
|= XFS_IFEXTENTS
;
1262 xfs_iext_destroy(ifp
);
1267 * Returns the relative block number of the first unused block(s) in the given
1268 * fork with at least "len" logically contiguous blocks free. This is the
1269 * lowest-address hole if the fork has holes, else the first block past the end
1270 * of fork. Return 0 if the fork is currently local (in-inode).
1273 xfs_bmap_first_unused(
1274 struct xfs_trans
*tp
, /* transaction pointer */
1275 struct xfs_inode
*ip
, /* incore inode */
1276 xfs_extlen_t len
, /* size of hole to find */
1277 xfs_fileoff_t
*first_unused
, /* unused block */
1278 int whichfork
) /* data or attr fork */
1280 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1281 struct xfs_bmbt_irec got
;
1282 struct xfs_iext_cursor icur
;
1283 xfs_fileoff_t lastaddr
= 0;
1284 xfs_fileoff_t lowest
, max
;
1287 ASSERT(xfs_ifork_has_extents(ip
, whichfork
) ||
1288 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
1290 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
) {
1295 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
1296 error
= xfs_iread_extents(tp
, ip
, whichfork
);
1301 lowest
= max
= *first_unused
;
1302 for_each_xfs_iext(ifp
, &icur
, &got
) {
1304 * See if the hole before this extent will work.
1306 if (got
.br_startoff
>= lowest
+ len
&&
1307 got
.br_startoff
- max
>= len
)
1309 lastaddr
= got
.br_startoff
+ got
.br_blockcount
;
1310 max
= XFS_FILEOFF_MAX(lastaddr
, lowest
);
1313 *first_unused
= max
;
1318 * Returns the file-relative block number of the last block - 1 before
1319 * last_block (input value) in the file.
1320 * This is not based on i_size, it is based on the extent records.
1321 * Returns 0 for local files, as they do not have extent records.
1324 xfs_bmap_last_before(
1325 struct xfs_trans
*tp
, /* transaction pointer */
1326 struct xfs_inode
*ip
, /* incore inode */
1327 xfs_fileoff_t
*last_block
, /* last block */
1328 int whichfork
) /* data or attr fork */
1330 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1331 struct xfs_bmbt_irec got
;
1332 struct xfs_iext_cursor icur
;
1335 switch (XFS_IFORK_FORMAT(ip
, whichfork
)) {
1336 case XFS_DINODE_FMT_LOCAL
:
1339 case XFS_DINODE_FMT_BTREE
:
1340 case XFS_DINODE_FMT_EXTENTS
:
1344 return -EFSCORRUPTED
;
1347 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
1348 error
= xfs_iread_extents(tp
, ip
, whichfork
);
1353 if (!xfs_iext_lookup_extent_before(ip
, ifp
, last_block
, &icur
, &got
))
1359 xfs_bmap_last_extent(
1360 struct xfs_trans
*tp
,
1361 struct xfs_inode
*ip
,
1363 struct xfs_bmbt_irec
*rec
,
1366 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1367 struct xfs_iext_cursor icur
;
1370 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
1371 error
= xfs_iread_extents(tp
, ip
, whichfork
);
1376 xfs_iext_last(ifp
, &icur
);
1377 if (!xfs_iext_get_extent(ifp
, &icur
, rec
))
1385 * Check the last inode extent to determine whether this allocation will result
1386 * in blocks being allocated at the end of the file. When we allocate new data
1387 * blocks at the end of the file which do not start at the previous data block,
1388 * we will try to align the new blocks at stripe unit boundaries.
1390 * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be
1391 * at, or past the EOF.
1395 struct xfs_bmalloca
*bma
,
1398 struct xfs_bmbt_irec rec
;
1403 error
= xfs_bmap_last_extent(NULL
, bma
->ip
, whichfork
, &rec
,
1414 * Check if we are allocation or past the last extent, or at least into
1415 * the last delayed allocated extent.
1417 bma
->aeof
= bma
->offset
>= rec
.br_startoff
+ rec
.br_blockcount
||
1418 (bma
->offset
>= rec
.br_startoff
&&
1419 isnullstartblock(rec
.br_startblock
));
1424 * Returns the file-relative block number of the first block past eof in
1425 * the file. This is not based on i_size, it is based on the extent records.
1426 * Returns 0 for local files, as they do not have extent records.
1429 xfs_bmap_last_offset(
1430 struct xfs_inode
*ip
,
1431 xfs_fileoff_t
*last_block
,
1434 struct xfs_bmbt_irec rec
;
1440 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
)
1443 if (XFS_IS_CORRUPT(ip
->i_mount
, !xfs_ifork_has_extents(ip
, whichfork
)))
1444 return -EFSCORRUPTED
;
1446 error
= xfs_bmap_last_extent(NULL
, ip
, whichfork
, &rec
, &is_empty
);
1447 if (error
|| is_empty
)
1450 *last_block
= rec
.br_startoff
+ rec
.br_blockcount
;
1455 * Returns whether the selected fork of the inode has exactly one
1456 * block or not. For the data fork we check this matches di_size,
1457 * implying the file's range is 0..bsize-1.
1459 int /* 1=>1 block, 0=>otherwise */
1461 xfs_inode_t
*ip
, /* incore inode */
1462 int whichfork
) /* data or attr fork */
1464 struct xfs_ifork
*ifp
; /* inode fork pointer */
1465 int rval
; /* return value */
1466 xfs_bmbt_irec_t s
; /* internal version of extent */
1467 struct xfs_iext_cursor icur
;
1470 if (whichfork
== XFS_DATA_FORK
)
1471 return XFS_ISIZE(ip
) == ip
->i_mount
->m_sb
.sb_blocksize
;
1473 if (XFS_IFORK_NEXTENTS(ip
, whichfork
) != 1)
1475 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
1477 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1478 ASSERT(ifp
->if_flags
& XFS_IFEXTENTS
);
1479 xfs_iext_first(ifp
, &icur
);
1480 xfs_iext_get_extent(ifp
, &icur
, &s
);
1481 rval
= s
.br_startoff
== 0 && s
.br_blockcount
== 1;
1482 if (rval
&& whichfork
== XFS_DATA_FORK
)
1483 ASSERT(XFS_ISIZE(ip
) == ip
->i_mount
->m_sb
.sb_blocksize
);
1488 * Extent tree manipulation functions used during allocation.
1492 * Convert a delayed allocation to a real allocation.
1494 STATIC
int /* error */
1495 xfs_bmap_add_extent_delay_real(
1496 struct xfs_bmalloca
*bma
,
1499 struct xfs_bmbt_irec
*new = &bma
->got
;
1500 int error
; /* error return value */
1501 int i
; /* temp state */
1502 struct xfs_ifork
*ifp
; /* inode fork pointer */
1503 xfs_fileoff_t new_endoff
; /* end offset of new entry */
1504 xfs_bmbt_irec_t r
[3]; /* neighbor extent entries */
1505 /* left is 0, right is 1, prev is 2 */
1506 int rval
=0; /* return value (logging flags) */
1507 int state
= xfs_bmap_fork_to_state(whichfork
);
1508 xfs_filblks_t da_new
; /* new count del alloc blocks used */
1509 xfs_filblks_t da_old
; /* old count del alloc blocks used */
1510 xfs_filblks_t temp
=0; /* value for da_new calculations */
1511 int tmp_rval
; /* partial logging flags */
1512 struct xfs_mount
*mp
;
1513 xfs_extnum_t
*nextents
;
1514 struct xfs_bmbt_irec old
;
1516 mp
= bma
->ip
->i_mount
;
1517 ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
1518 ASSERT(whichfork
!= XFS_ATTR_FORK
);
1519 nextents
= (whichfork
== XFS_COW_FORK
? &bma
->ip
->i_cnextents
:
1520 &bma
->ip
->i_d
.di_nextents
);
1522 ASSERT(!isnullstartblock(new->br_startblock
));
1524 (bma
->cur
->bc_private
.b
.flags
& XFS_BTCUR_BPRV_WASDEL
));
1526 XFS_STATS_INC(mp
, xs_add_exlist
);
1533 * Set up a bunch of variables to make the tests simpler.
1535 xfs_iext_get_extent(ifp
, &bma
->icur
, &PREV
);
1536 new_endoff
= new->br_startoff
+ new->br_blockcount
;
1537 ASSERT(isnullstartblock(PREV
.br_startblock
));
1538 ASSERT(PREV
.br_startoff
<= new->br_startoff
);
1539 ASSERT(PREV
.br_startoff
+ PREV
.br_blockcount
>= new_endoff
);
1541 da_old
= startblockval(PREV
.br_startblock
);
1545 * Set flags determining what part of the previous delayed allocation
1546 * extent is being replaced by a real allocation.
1548 if (PREV
.br_startoff
== new->br_startoff
)
1549 state
|= BMAP_LEFT_FILLING
;
1550 if (PREV
.br_startoff
+ PREV
.br_blockcount
== new_endoff
)
1551 state
|= BMAP_RIGHT_FILLING
;
1554 * Check and set flags if this segment has a left neighbor.
1555 * Don't set contiguous if the combined extent would be too large.
1557 if (xfs_iext_peek_prev_extent(ifp
, &bma
->icur
, &LEFT
)) {
1558 state
|= BMAP_LEFT_VALID
;
1559 if (isnullstartblock(LEFT
.br_startblock
))
1560 state
|= BMAP_LEFT_DELAY
;
1563 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
1564 LEFT
.br_startoff
+ LEFT
.br_blockcount
== new->br_startoff
&&
1565 LEFT
.br_startblock
+ LEFT
.br_blockcount
== new->br_startblock
&&
1566 LEFT
.br_state
== new->br_state
&&
1567 LEFT
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
1568 state
|= BMAP_LEFT_CONTIG
;
1571 * Check and set flags if this segment has a right neighbor.
1572 * Don't set contiguous if the combined extent would be too large.
1573 * Also check for all-three-contiguous being too large.
1575 if (xfs_iext_peek_next_extent(ifp
, &bma
->icur
, &RIGHT
)) {
1576 state
|= BMAP_RIGHT_VALID
;
1577 if (isnullstartblock(RIGHT
.br_startblock
))
1578 state
|= BMAP_RIGHT_DELAY
;
1581 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
1582 new_endoff
== RIGHT
.br_startoff
&&
1583 new->br_startblock
+ new->br_blockcount
== RIGHT
.br_startblock
&&
1584 new->br_state
== RIGHT
.br_state
&&
1585 new->br_blockcount
+ RIGHT
.br_blockcount
<= MAXEXTLEN
&&
1586 ((state
& (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
1587 BMAP_RIGHT_FILLING
)) !=
1588 (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
1589 BMAP_RIGHT_FILLING
) ||
1590 LEFT
.br_blockcount
+ new->br_blockcount
+ RIGHT
.br_blockcount
1592 state
|= BMAP_RIGHT_CONTIG
;
1596 * Switch out based on the FILLING and CONTIG state bits.
1598 switch (state
& (BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
1599 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
)) {
1600 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
1601 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
1603 * Filling in all of a previously delayed allocation extent.
1604 * The left and right neighbors are both contiguous with new.
1606 LEFT
.br_blockcount
+= PREV
.br_blockcount
+ RIGHT
.br_blockcount
;
1608 xfs_iext_remove(bma
->ip
, &bma
->icur
, state
);
1609 xfs_iext_remove(bma
->ip
, &bma
->icur
, state
);
1610 xfs_iext_prev(ifp
, &bma
->icur
);
1611 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &LEFT
);
1614 if (bma
->cur
== NULL
)
1615 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1617 rval
= XFS_ILOG_CORE
;
1618 error
= xfs_bmbt_lookup_eq(bma
->cur
, &RIGHT
, &i
);
1621 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
1622 error
= -EFSCORRUPTED
;
1625 error
= xfs_btree_delete(bma
->cur
, &i
);
1628 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
1629 error
= -EFSCORRUPTED
;
1632 error
= xfs_btree_decrement(bma
->cur
, 0, &i
);
1635 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
1636 error
= -EFSCORRUPTED
;
1639 error
= xfs_bmbt_update(bma
->cur
, &LEFT
);
1645 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
1647 * Filling in all of a previously delayed allocation extent.
1648 * The left neighbor is contiguous, the right is not.
1651 LEFT
.br_blockcount
+= PREV
.br_blockcount
;
1653 xfs_iext_remove(bma
->ip
, &bma
->icur
, state
);
1654 xfs_iext_prev(ifp
, &bma
->icur
);
1655 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &LEFT
);
1657 if (bma
->cur
== NULL
)
1658 rval
= XFS_ILOG_DEXT
;
1661 error
= xfs_bmbt_lookup_eq(bma
->cur
, &old
, &i
);
1664 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
1665 error
= -EFSCORRUPTED
;
1668 error
= xfs_bmbt_update(bma
->cur
, &LEFT
);
1674 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
1676 * Filling in all of a previously delayed allocation extent.
1677 * The right neighbor is contiguous, the left is not. Take care
1678 * with delay -> unwritten extent allocation here because the
1679 * delalloc record we are overwriting is always written.
1681 PREV
.br_startblock
= new->br_startblock
;
1682 PREV
.br_blockcount
+= RIGHT
.br_blockcount
;
1683 PREV
.br_state
= new->br_state
;
1685 xfs_iext_next(ifp
, &bma
->icur
);
1686 xfs_iext_remove(bma
->ip
, &bma
->icur
, state
);
1687 xfs_iext_prev(ifp
, &bma
->icur
);
1688 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &PREV
);
1690 if (bma
->cur
== NULL
)
1691 rval
= XFS_ILOG_DEXT
;
1694 error
= xfs_bmbt_lookup_eq(bma
->cur
, &RIGHT
, &i
);
1697 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
1698 error
= -EFSCORRUPTED
;
1701 error
= xfs_bmbt_update(bma
->cur
, &PREV
);
1707 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
1709 * Filling in all of a previously delayed allocation extent.
1710 * Neither the left nor right neighbors are contiguous with
1713 PREV
.br_startblock
= new->br_startblock
;
1714 PREV
.br_state
= new->br_state
;
1715 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &PREV
);
1718 if (bma
->cur
== NULL
)
1719 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1721 rval
= XFS_ILOG_CORE
;
1722 error
= xfs_bmbt_lookup_eq(bma
->cur
, new, &i
);
1725 if (XFS_IS_CORRUPT(mp
, i
!= 0)) {
1726 error
= -EFSCORRUPTED
;
1729 error
= xfs_btree_insert(bma
->cur
, &i
);
1732 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
1733 error
= -EFSCORRUPTED
;
1739 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
:
1741 * Filling in the first part of a previous delayed allocation.
1742 * The left neighbor is contiguous.
1745 temp
= PREV
.br_blockcount
- new->br_blockcount
;
1746 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
1747 startblockval(PREV
.br_startblock
));
1749 LEFT
.br_blockcount
+= new->br_blockcount
;
1751 PREV
.br_blockcount
= temp
;
1752 PREV
.br_startoff
+= new->br_blockcount
;
1753 PREV
.br_startblock
= nullstartblock(da_new
);
1755 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &PREV
);
1756 xfs_iext_prev(ifp
, &bma
->icur
);
1757 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &LEFT
);
1759 if (bma
->cur
== NULL
)
1760 rval
= XFS_ILOG_DEXT
;
1763 error
= xfs_bmbt_lookup_eq(bma
->cur
, &old
, &i
);
1766 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
1767 error
= -EFSCORRUPTED
;
1770 error
= xfs_bmbt_update(bma
->cur
, &LEFT
);
1776 case BMAP_LEFT_FILLING
:
1778 * Filling in the first part of a previous delayed allocation.
1779 * The left neighbor is not contiguous.
1781 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, new);
1783 if (bma
->cur
== NULL
)
1784 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1786 rval
= XFS_ILOG_CORE
;
1787 error
= xfs_bmbt_lookup_eq(bma
->cur
, new, &i
);
1790 if (XFS_IS_CORRUPT(mp
, i
!= 0)) {
1791 error
= -EFSCORRUPTED
;
1794 error
= xfs_btree_insert(bma
->cur
, &i
);
1797 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
1798 error
= -EFSCORRUPTED
;
1803 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
1804 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
1805 &bma
->cur
, 1, &tmp_rval
, whichfork
);
1811 temp
= PREV
.br_blockcount
- new->br_blockcount
;
1812 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
1813 startblockval(PREV
.br_startblock
) -
1814 (bma
->cur
? bma
->cur
->bc_private
.b
.allocated
: 0));
1816 PREV
.br_startoff
= new_endoff
;
1817 PREV
.br_blockcount
= temp
;
1818 PREV
.br_startblock
= nullstartblock(da_new
);
1819 xfs_iext_next(ifp
, &bma
->icur
);
1820 xfs_iext_insert(bma
->ip
, &bma
->icur
, &PREV
, state
);
1821 xfs_iext_prev(ifp
, &bma
->icur
);
1824 case BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
1826 * Filling in the last part of a previous delayed allocation.
1827 * The right neighbor is contiguous with the new allocation.
1830 RIGHT
.br_startoff
= new->br_startoff
;
1831 RIGHT
.br_startblock
= new->br_startblock
;
1832 RIGHT
.br_blockcount
+= new->br_blockcount
;
1834 if (bma
->cur
== NULL
)
1835 rval
= XFS_ILOG_DEXT
;
1838 error
= xfs_bmbt_lookup_eq(bma
->cur
, &old
, &i
);
1841 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
1842 error
= -EFSCORRUPTED
;
1845 error
= xfs_bmbt_update(bma
->cur
, &RIGHT
);
1850 temp
= PREV
.br_blockcount
- new->br_blockcount
;
1851 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
1852 startblockval(PREV
.br_startblock
));
1854 PREV
.br_blockcount
= temp
;
1855 PREV
.br_startblock
= nullstartblock(da_new
);
1857 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &PREV
);
1858 xfs_iext_next(ifp
, &bma
->icur
);
1859 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &RIGHT
);
1862 case BMAP_RIGHT_FILLING
:
1864 * Filling in the last part of a previous delayed allocation.
1865 * The right neighbor is not contiguous.
1867 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, new);
1869 if (bma
->cur
== NULL
)
1870 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1872 rval
= XFS_ILOG_CORE
;
1873 error
= xfs_bmbt_lookup_eq(bma
->cur
, new, &i
);
1876 if (XFS_IS_CORRUPT(mp
, i
!= 0)) {
1877 error
= -EFSCORRUPTED
;
1880 error
= xfs_btree_insert(bma
->cur
, &i
);
1883 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
1884 error
= -EFSCORRUPTED
;
1889 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
1890 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
1891 &bma
->cur
, 1, &tmp_rval
, whichfork
);
1897 temp
= PREV
.br_blockcount
- new->br_blockcount
;
1898 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
1899 startblockval(PREV
.br_startblock
) -
1900 (bma
->cur
? bma
->cur
->bc_private
.b
.allocated
: 0));
1902 PREV
.br_startblock
= nullstartblock(da_new
);
1903 PREV
.br_blockcount
= temp
;
1904 xfs_iext_insert(bma
->ip
, &bma
->icur
, &PREV
, state
);
1905 xfs_iext_next(ifp
, &bma
->icur
);
1910 * Filling in the middle part of a previous delayed allocation.
1911 * Contiguity is impossible here.
1912 * This case is avoided almost all the time.
1914 * We start with a delayed allocation:
1916 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
1919 * and we are allocating:
1920 * +rrrrrrrrrrrrrrrrr+
1923 * and we set it up for insertion as:
1924 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
1926 * PREV @ idx LEFT RIGHT
1927 * inserted at idx + 1
1931 /* LEFT is the new middle */
1934 /* RIGHT is the new right */
1935 RIGHT
.br_state
= PREV
.br_state
;
1936 RIGHT
.br_startoff
= new_endoff
;
1937 RIGHT
.br_blockcount
=
1938 PREV
.br_startoff
+ PREV
.br_blockcount
- new_endoff
;
1939 RIGHT
.br_startblock
=
1940 nullstartblock(xfs_bmap_worst_indlen(bma
->ip
,
1941 RIGHT
.br_blockcount
));
1944 PREV
.br_blockcount
= new->br_startoff
- PREV
.br_startoff
;
1945 PREV
.br_startblock
=
1946 nullstartblock(xfs_bmap_worst_indlen(bma
->ip
,
1947 PREV
.br_blockcount
));
1948 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &PREV
);
1950 xfs_iext_next(ifp
, &bma
->icur
);
1951 xfs_iext_insert(bma
->ip
, &bma
->icur
, &RIGHT
, state
);
1952 xfs_iext_insert(bma
->ip
, &bma
->icur
, &LEFT
, state
);
1955 if (bma
->cur
== NULL
)
1956 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1958 rval
= XFS_ILOG_CORE
;
1959 error
= xfs_bmbt_lookup_eq(bma
->cur
, new, &i
);
1962 if (XFS_IS_CORRUPT(mp
, i
!= 0)) {
1963 error
= -EFSCORRUPTED
;
1966 error
= xfs_btree_insert(bma
->cur
, &i
);
1969 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
1970 error
= -EFSCORRUPTED
;
1975 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
1976 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
1977 &bma
->cur
, 1, &tmp_rval
, whichfork
);
1983 da_new
= startblockval(PREV
.br_startblock
) +
1984 startblockval(RIGHT
.br_startblock
);
1987 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
1988 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
1989 case BMAP_LEFT_FILLING
| BMAP_RIGHT_CONTIG
:
1990 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
1991 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
1992 case BMAP_LEFT_CONTIG
:
1993 case BMAP_RIGHT_CONTIG
:
1995 * These cases are all impossible.
2000 /* add reverse mapping unless caller opted out */
2001 if (!(bma
->flags
& XFS_BMAPI_NORMAP
))
2002 xfs_rmap_map_extent(bma
->tp
, bma
->ip
, whichfork
, new);
2004 /* convert to a btree if necessary */
2005 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
2006 int tmp_logflags
; /* partial log flag return val */
2008 ASSERT(bma
->cur
== NULL
);
2009 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
2010 &bma
->cur
, da_old
> 0, &tmp_logflags
,
2012 bma
->logflags
|= tmp_logflags
;
2017 if (da_new
!= da_old
)
2018 xfs_mod_delalloc(mp
, (int64_t)da_new
- da_old
);
2021 da_new
+= bma
->cur
->bc_private
.b
.allocated
;
2022 bma
->cur
->bc_private
.b
.allocated
= 0;
2025 /* adjust for changes in reserved delayed indirect blocks */
2026 if (da_new
!= da_old
) {
2027 ASSERT(state
== 0 || da_new
< da_old
);
2028 error
= xfs_mod_fdblocks(mp
, (int64_t)(da_old
- da_new
),
2032 xfs_bmap_check_leaf_extents(bma
->cur
, bma
->ip
, whichfork
);
2034 if (whichfork
!= XFS_COW_FORK
)
2035 bma
->logflags
|= rval
;
2043 * Convert an unwritten allocation to a real allocation or vice versa.
2046 xfs_bmap_add_extent_unwritten_real(
2047 struct xfs_trans
*tp
,
2048 xfs_inode_t
*ip
, /* incore inode pointer */
2050 struct xfs_iext_cursor
*icur
,
2051 xfs_btree_cur_t
**curp
, /* if *curp is null, not a btree */
2052 xfs_bmbt_irec_t
*new, /* new data to add to file extents */
2053 int *logflagsp
) /* inode logging flags */
2055 xfs_btree_cur_t
*cur
; /* btree cursor */
2056 int error
; /* error return value */
2057 int i
; /* temp state */
2058 struct xfs_ifork
*ifp
; /* inode fork pointer */
2059 xfs_fileoff_t new_endoff
; /* end offset of new entry */
2060 xfs_bmbt_irec_t r
[3]; /* neighbor extent entries */
2061 /* left is 0, right is 1, prev is 2 */
2062 int rval
=0; /* return value (logging flags) */
2063 int state
= xfs_bmap_fork_to_state(whichfork
);
2064 struct xfs_mount
*mp
= ip
->i_mount
;
2065 struct xfs_bmbt_irec old
;
2070 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
2072 ASSERT(!isnullstartblock(new->br_startblock
));
2074 XFS_STATS_INC(mp
, xs_add_exlist
);
2081 * Set up a bunch of variables to make the tests simpler.
2084 xfs_iext_get_extent(ifp
, icur
, &PREV
);
2085 ASSERT(new->br_state
!= PREV
.br_state
);
2086 new_endoff
= new->br_startoff
+ new->br_blockcount
;
2087 ASSERT(PREV
.br_startoff
<= new->br_startoff
);
2088 ASSERT(PREV
.br_startoff
+ PREV
.br_blockcount
>= new_endoff
);
2091 * Set flags determining what part of the previous oldext allocation
2092 * extent is being replaced by a newext allocation.
2094 if (PREV
.br_startoff
== new->br_startoff
)
2095 state
|= BMAP_LEFT_FILLING
;
2096 if (PREV
.br_startoff
+ PREV
.br_blockcount
== new_endoff
)
2097 state
|= BMAP_RIGHT_FILLING
;
2100 * Check and set flags if this segment has a left neighbor.
2101 * Don't set contiguous if the combined extent would be too large.
2103 if (xfs_iext_peek_prev_extent(ifp
, icur
, &LEFT
)) {
2104 state
|= BMAP_LEFT_VALID
;
2105 if (isnullstartblock(LEFT
.br_startblock
))
2106 state
|= BMAP_LEFT_DELAY
;
2109 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
2110 LEFT
.br_startoff
+ LEFT
.br_blockcount
== new->br_startoff
&&
2111 LEFT
.br_startblock
+ LEFT
.br_blockcount
== new->br_startblock
&&
2112 LEFT
.br_state
== new->br_state
&&
2113 LEFT
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
2114 state
|= BMAP_LEFT_CONTIG
;
2117 * Check and set flags if this segment has a right neighbor.
2118 * Don't set contiguous if the combined extent would be too large.
2119 * Also check for all-three-contiguous being too large.
2121 if (xfs_iext_peek_next_extent(ifp
, icur
, &RIGHT
)) {
2122 state
|= BMAP_RIGHT_VALID
;
2123 if (isnullstartblock(RIGHT
.br_startblock
))
2124 state
|= BMAP_RIGHT_DELAY
;
2127 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
2128 new_endoff
== RIGHT
.br_startoff
&&
2129 new->br_startblock
+ new->br_blockcount
== RIGHT
.br_startblock
&&
2130 new->br_state
== RIGHT
.br_state
&&
2131 new->br_blockcount
+ RIGHT
.br_blockcount
<= MAXEXTLEN
&&
2132 ((state
& (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
2133 BMAP_RIGHT_FILLING
)) !=
2134 (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
2135 BMAP_RIGHT_FILLING
) ||
2136 LEFT
.br_blockcount
+ new->br_blockcount
+ RIGHT
.br_blockcount
2138 state
|= BMAP_RIGHT_CONTIG
;
2141 * Switch out based on the FILLING and CONTIG state bits.
2143 switch (state
& (BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
2144 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
)) {
2145 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
2146 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2148 * Setting all of a previous oldext extent to newext.
2149 * The left and right neighbors are both contiguous with new.
2151 LEFT
.br_blockcount
+= PREV
.br_blockcount
+ RIGHT
.br_blockcount
;
2153 xfs_iext_remove(ip
, icur
, state
);
2154 xfs_iext_remove(ip
, icur
, state
);
2155 xfs_iext_prev(ifp
, icur
);
2156 xfs_iext_update_extent(ip
, state
, icur
, &LEFT
);
2157 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2158 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 2);
2160 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2162 rval
= XFS_ILOG_CORE
;
2163 error
= xfs_bmbt_lookup_eq(cur
, &RIGHT
, &i
);
2166 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
2167 error
= -EFSCORRUPTED
;
2170 if ((error
= xfs_btree_delete(cur
, &i
)))
2172 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
2173 error
= -EFSCORRUPTED
;
2176 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2178 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
2179 error
= -EFSCORRUPTED
;
2182 if ((error
= xfs_btree_delete(cur
, &i
)))
2184 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
2185 error
= -EFSCORRUPTED
;
2188 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2190 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
2191 error
= -EFSCORRUPTED
;
2194 error
= xfs_bmbt_update(cur
, &LEFT
);
2200 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
2202 * Setting all of a previous oldext extent to newext.
2203 * The left neighbor is contiguous, the right is not.
2205 LEFT
.br_blockcount
+= PREV
.br_blockcount
;
2207 xfs_iext_remove(ip
, icur
, state
);
2208 xfs_iext_prev(ifp
, icur
);
2209 xfs_iext_update_extent(ip
, state
, icur
, &LEFT
);
2210 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2211 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
2213 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2215 rval
= XFS_ILOG_CORE
;
2216 error
= xfs_bmbt_lookup_eq(cur
, &PREV
, &i
);
2219 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
2220 error
= -EFSCORRUPTED
;
2223 if ((error
= xfs_btree_delete(cur
, &i
)))
2225 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
2226 error
= -EFSCORRUPTED
;
2229 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2231 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
2232 error
= -EFSCORRUPTED
;
2235 error
= xfs_bmbt_update(cur
, &LEFT
);
2241 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2243 * Setting all of a previous oldext extent to newext.
2244 * The right neighbor is contiguous, the left is not.
2246 PREV
.br_blockcount
+= RIGHT
.br_blockcount
;
2247 PREV
.br_state
= new->br_state
;
2249 xfs_iext_next(ifp
, icur
);
2250 xfs_iext_remove(ip
, icur
, state
);
2251 xfs_iext_prev(ifp
, icur
);
2252 xfs_iext_update_extent(ip
, state
, icur
, &PREV
);
2254 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2255 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
2257 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2259 rval
= XFS_ILOG_CORE
;
2260 error
= xfs_bmbt_lookup_eq(cur
, &RIGHT
, &i
);
2263 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
2264 error
= -EFSCORRUPTED
;
2267 if ((error
= xfs_btree_delete(cur
, &i
)))
2269 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
2270 error
= -EFSCORRUPTED
;
2273 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2275 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
2276 error
= -EFSCORRUPTED
;
2279 error
= xfs_bmbt_update(cur
, &PREV
);
2285 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
2287 * Setting all of a previous oldext extent to newext.
2288 * Neither the left nor right neighbors are contiguous with
2291 PREV
.br_state
= new->br_state
;
2292 xfs_iext_update_extent(ip
, state
, icur
, &PREV
);
2295 rval
= XFS_ILOG_DEXT
;
2298 error
= xfs_bmbt_lookup_eq(cur
, new, &i
);
2301 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
2302 error
= -EFSCORRUPTED
;
2305 error
= xfs_bmbt_update(cur
, &PREV
);
2311 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
:
2313 * Setting the first part of a previous oldext extent to newext.
2314 * The left neighbor is contiguous.
2316 LEFT
.br_blockcount
+= new->br_blockcount
;
2319 PREV
.br_startoff
+= new->br_blockcount
;
2320 PREV
.br_startblock
+= new->br_blockcount
;
2321 PREV
.br_blockcount
-= new->br_blockcount
;
2323 xfs_iext_update_extent(ip
, state
, icur
, &PREV
);
2324 xfs_iext_prev(ifp
, icur
);
2325 xfs_iext_update_extent(ip
, state
, icur
, &LEFT
);
2328 rval
= XFS_ILOG_DEXT
;
2331 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2334 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
2335 error
= -EFSCORRUPTED
;
2338 error
= xfs_bmbt_update(cur
, &PREV
);
2341 error
= xfs_btree_decrement(cur
, 0, &i
);
2344 error
= xfs_bmbt_update(cur
, &LEFT
);
2350 case BMAP_LEFT_FILLING
:
2352 * Setting the first part of a previous oldext extent to newext.
2353 * The left neighbor is not contiguous.
2356 PREV
.br_startoff
+= new->br_blockcount
;
2357 PREV
.br_startblock
+= new->br_blockcount
;
2358 PREV
.br_blockcount
-= new->br_blockcount
;
2360 xfs_iext_update_extent(ip
, state
, icur
, &PREV
);
2361 xfs_iext_insert(ip
, icur
, new, state
);
2362 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2363 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
2365 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2367 rval
= XFS_ILOG_CORE
;
2368 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2371 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
2372 error
= -EFSCORRUPTED
;
2375 error
= xfs_bmbt_update(cur
, &PREV
);
2378 cur
->bc_rec
.b
= *new;
2379 if ((error
= xfs_btree_insert(cur
, &i
)))
2381 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
2382 error
= -EFSCORRUPTED
;
2388 case BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2390 * Setting the last part of a previous oldext extent to newext.
2391 * The right neighbor is contiguous with the new allocation.
2394 PREV
.br_blockcount
-= new->br_blockcount
;
2396 RIGHT
.br_startoff
= new->br_startoff
;
2397 RIGHT
.br_startblock
= new->br_startblock
;
2398 RIGHT
.br_blockcount
+= new->br_blockcount
;
2400 xfs_iext_update_extent(ip
, state
, icur
, &PREV
);
2401 xfs_iext_next(ifp
, icur
);
2402 xfs_iext_update_extent(ip
, state
, icur
, &RIGHT
);
2405 rval
= XFS_ILOG_DEXT
;
2408 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2411 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
2412 error
= -EFSCORRUPTED
;
2415 error
= xfs_bmbt_update(cur
, &PREV
);
2418 error
= xfs_btree_increment(cur
, 0, &i
);
2421 error
= xfs_bmbt_update(cur
, &RIGHT
);
2427 case BMAP_RIGHT_FILLING
:
2429 * Setting the last part of a previous oldext extent to newext.
2430 * The right neighbor is not contiguous.
2433 PREV
.br_blockcount
-= new->br_blockcount
;
2435 xfs_iext_update_extent(ip
, state
, icur
, &PREV
);
2436 xfs_iext_next(ifp
, icur
);
2437 xfs_iext_insert(ip
, icur
, new, state
);
2439 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2440 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
2442 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2444 rval
= XFS_ILOG_CORE
;
2445 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2448 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
2449 error
= -EFSCORRUPTED
;
2452 error
= xfs_bmbt_update(cur
, &PREV
);
2455 error
= xfs_bmbt_lookup_eq(cur
, new, &i
);
2458 if (XFS_IS_CORRUPT(mp
, i
!= 0)) {
2459 error
= -EFSCORRUPTED
;
2462 if ((error
= xfs_btree_insert(cur
, &i
)))
2464 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
2465 error
= -EFSCORRUPTED
;
2473 * Setting the middle part of a previous oldext extent to
2474 * newext. Contiguity is impossible here.
2475 * One extent becomes three extents.
2478 PREV
.br_blockcount
= new->br_startoff
- PREV
.br_startoff
;
2481 r
[1].br_startoff
= new_endoff
;
2482 r
[1].br_blockcount
=
2483 old
.br_startoff
+ old
.br_blockcount
- new_endoff
;
2484 r
[1].br_startblock
= new->br_startblock
+ new->br_blockcount
;
2485 r
[1].br_state
= PREV
.br_state
;
2487 xfs_iext_update_extent(ip
, state
, icur
, &PREV
);
2488 xfs_iext_next(ifp
, icur
);
2489 xfs_iext_insert(ip
, icur
, &r
[1], state
);
2490 xfs_iext_insert(ip
, icur
, &r
[0], state
);
2492 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2493 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 2);
2495 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2497 rval
= XFS_ILOG_CORE
;
2498 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2501 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
2502 error
= -EFSCORRUPTED
;
2505 /* new right extent - oldext */
2506 error
= xfs_bmbt_update(cur
, &r
[1]);
2509 /* new left extent - oldext */
2510 cur
->bc_rec
.b
= PREV
;
2511 if ((error
= xfs_btree_insert(cur
, &i
)))
2513 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
2514 error
= -EFSCORRUPTED
;
2518 * Reset the cursor to the position of the new extent
2519 * we are about to insert as we can't trust it after
2520 * the previous insert.
2522 error
= xfs_bmbt_lookup_eq(cur
, new, &i
);
2525 if (XFS_IS_CORRUPT(mp
, i
!= 0)) {
2526 error
= -EFSCORRUPTED
;
2529 /* new middle extent - newext */
2530 if ((error
= xfs_btree_insert(cur
, &i
)))
2532 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
2533 error
= -EFSCORRUPTED
;
2539 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2540 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2541 case BMAP_LEFT_FILLING
| BMAP_RIGHT_CONTIG
:
2542 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
2543 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2544 case BMAP_LEFT_CONTIG
:
2545 case BMAP_RIGHT_CONTIG
:
2547 * These cases are all impossible.
2552 /* update reverse mappings */
2553 xfs_rmap_convert_extent(mp
, tp
, ip
, whichfork
, new);
2555 /* convert to a btree if necessary */
2556 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
2557 int tmp_logflags
; /* partial log flag return val */
2559 ASSERT(cur
== NULL
);
2560 error
= xfs_bmap_extents_to_btree(tp
, ip
, &cur
, 0,
2561 &tmp_logflags
, whichfork
);
2562 *logflagsp
|= tmp_logflags
;
2567 /* clear out the allocated field, done with it now in any case. */
2569 cur
->bc_private
.b
.allocated
= 0;
2573 xfs_bmap_check_leaf_extents(*curp
, ip
, whichfork
);
2583 * Convert a hole to a delayed allocation.
2586 xfs_bmap_add_extent_hole_delay(
2587 xfs_inode_t
*ip
, /* incore inode pointer */
2589 struct xfs_iext_cursor
*icur
,
2590 xfs_bmbt_irec_t
*new) /* new data to add to file extents */
2592 struct xfs_ifork
*ifp
; /* inode fork pointer */
2593 xfs_bmbt_irec_t left
; /* left neighbor extent entry */
2594 xfs_filblks_t newlen
=0; /* new indirect size */
2595 xfs_filblks_t oldlen
=0; /* old indirect size */
2596 xfs_bmbt_irec_t right
; /* right neighbor extent entry */
2597 int state
= xfs_bmap_fork_to_state(whichfork
);
2598 xfs_filblks_t temp
; /* temp for indirect calculations */
2600 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
2601 ASSERT(isnullstartblock(new->br_startblock
));
2604 * Check and set flags if this segment has a left neighbor
2606 if (xfs_iext_peek_prev_extent(ifp
, icur
, &left
)) {
2607 state
|= BMAP_LEFT_VALID
;
2608 if (isnullstartblock(left
.br_startblock
))
2609 state
|= BMAP_LEFT_DELAY
;
2613 * Check and set flags if the current (right) segment exists.
2614 * If it doesn't exist, we're converting the hole at end-of-file.
2616 if (xfs_iext_get_extent(ifp
, icur
, &right
)) {
2617 state
|= BMAP_RIGHT_VALID
;
2618 if (isnullstartblock(right
.br_startblock
))
2619 state
|= BMAP_RIGHT_DELAY
;
2623 * Set contiguity flags on the left and right neighbors.
2624 * Don't let extents get too large, even if the pieces are contiguous.
2626 if ((state
& BMAP_LEFT_VALID
) && (state
& BMAP_LEFT_DELAY
) &&
2627 left
.br_startoff
+ left
.br_blockcount
== new->br_startoff
&&
2628 left
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
2629 state
|= BMAP_LEFT_CONTIG
;
2631 if ((state
& BMAP_RIGHT_VALID
) && (state
& BMAP_RIGHT_DELAY
) &&
2632 new->br_startoff
+ new->br_blockcount
== right
.br_startoff
&&
2633 new->br_blockcount
+ right
.br_blockcount
<= MAXEXTLEN
&&
2634 (!(state
& BMAP_LEFT_CONTIG
) ||
2635 (left
.br_blockcount
+ new->br_blockcount
+
2636 right
.br_blockcount
<= MAXEXTLEN
)))
2637 state
|= BMAP_RIGHT_CONTIG
;
2640 * Switch out based on the contiguity flags.
2642 switch (state
& (BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
)) {
2643 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2645 * New allocation is contiguous with delayed allocations
2646 * on the left and on the right.
2647 * Merge all three into a single extent record.
2649 temp
= left
.br_blockcount
+ new->br_blockcount
+
2650 right
.br_blockcount
;
2652 oldlen
= startblockval(left
.br_startblock
) +
2653 startblockval(new->br_startblock
) +
2654 startblockval(right
.br_startblock
);
2655 newlen
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
2657 left
.br_startblock
= nullstartblock(newlen
);
2658 left
.br_blockcount
= temp
;
2660 xfs_iext_remove(ip
, icur
, state
);
2661 xfs_iext_prev(ifp
, icur
);
2662 xfs_iext_update_extent(ip
, state
, icur
, &left
);
2665 case BMAP_LEFT_CONTIG
:
2667 * New allocation is contiguous with a delayed allocation
2669 * Merge the new allocation with the left neighbor.
2671 temp
= left
.br_blockcount
+ new->br_blockcount
;
2673 oldlen
= startblockval(left
.br_startblock
) +
2674 startblockval(new->br_startblock
);
2675 newlen
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
2677 left
.br_blockcount
= temp
;
2678 left
.br_startblock
= nullstartblock(newlen
);
2680 xfs_iext_prev(ifp
, icur
);
2681 xfs_iext_update_extent(ip
, state
, icur
, &left
);
2684 case BMAP_RIGHT_CONTIG
:
2686 * New allocation is contiguous with a delayed allocation
2688 * Merge the new allocation with the right neighbor.
2690 temp
= new->br_blockcount
+ right
.br_blockcount
;
2691 oldlen
= startblockval(new->br_startblock
) +
2692 startblockval(right
.br_startblock
);
2693 newlen
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
2695 right
.br_startoff
= new->br_startoff
;
2696 right
.br_startblock
= nullstartblock(newlen
);
2697 right
.br_blockcount
= temp
;
2698 xfs_iext_update_extent(ip
, state
, icur
, &right
);
2703 * New allocation is not contiguous with another
2704 * delayed allocation.
2705 * Insert a new entry.
2707 oldlen
= newlen
= 0;
2708 xfs_iext_insert(ip
, icur
, new, state
);
2711 if (oldlen
!= newlen
) {
2712 ASSERT(oldlen
> newlen
);
2713 xfs_mod_fdblocks(ip
->i_mount
, (int64_t)(oldlen
- newlen
),
2716 * Nothing to do for disk quota accounting here.
2718 xfs_mod_delalloc(ip
->i_mount
, (int64_t)newlen
- oldlen
);
2723 * Convert a hole to a real allocation.
2725 STATIC
int /* error */
2726 xfs_bmap_add_extent_hole_real(
2727 struct xfs_trans
*tp
,
2728 struct xfs_inode
*ip
,
2730 struct xfs_iext_cursor
*icur
,
2731 struct xfs_btree_cur
**curp
,
2732 struct xfs_bmbt_irec
*new,
2736 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
2737 struct xfs_mount
*mp
= ip
->i_mount
;
2738 struct xfs_btree_cur
*cur
= *curp
;
2739 int error
; /* error return value */
2740 int i
; /* temp state */
2741 xfs_bmbt_irec_t left
; /* left neighbor extent entry */
2742 xfs_bmbt_irec_t right
; /* right neighbor extent entry */
2743 int rval
=0; /* return value (logging flags) */
2744 int state
= xfs_bmap_fork_to_state(whichfork
);
2745 struct xfs_bmbt_irec old
;
2747 ASSERT(!isnullstartblock(new->br_startblock
));
2748 ASSERT(!cur
|| !(cur
->bc_private
.b
.flags
& XFS_BTCUR_BPRV_WASDEL
));
2750 XFS_STATS_INC(mp
, xs_add_exlist
);
2753 * Check and set flags if this segment has a left neighbor.
2755 if (xfs_iext_peek_prev_extent(ifp
, icur
, &left
)) {
2756 state
|= BMAP_LEFT_VALID
;
2757 if (isnullstartblock(left
.br_startblock
))
2758 state
|= BMAP_LEFT_DELAY
;
2762 * Check and set flags if this segment has a current value.
2763 * Not true if we're inserting into the "hole" at eof.
2765 if (xfs_iext_get_extent(ifp
, icur
, &right
)) {
2766 state
|= BMAP_RIGHT_VALID
;
2767 if (isnullstartblock(right
.br_startblock
))
2768 state
|= BMAP_RIGHT_DELAY
;
2772 * We're inserting a real allocation between "left" and "right".
2773 * Set the contiguity flags. Don't let extents get too large.
2775 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
2776 left
.br_startoff
+ left
.br_blockcount
== new->br_startoff
&&
2777 left
.br_startblock
+ left
.br_blockcount
== new->br_startblock
&&
2778 left
.br_state
== new->br_state
&&
2779 left
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
2780 state
|= BMAP_LEFT_CONTIG
;
2782 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
2783 new->br_startoff
+ new->br_blockcount
== right
.br_startoff
&&
2784 new->br_startblock
+ new->br_blockcount
== right
.br_startblock
&&
2785 new->br_state
== right
.br_state
&&
2786 new->br_blockcount
+ right
.br_blockcount
<= MAXEXTLEN
&&
2787 (!(state
& BMAP_LEFT_CONTIG
) ||
2788 left
.br_blockcount
+ new->br_blockcount
+
2789 right
.br_blockcount
<= MAXEXTLEN
))
2790 state
|= BMAP_RIGHT_CONTIG
;
2794 * Select which case we're in here, and implement it.
2796 switch (state
& (BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
)) {
2797 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2799 * New allocation is contiguous with real allocations on the
2800 * left and on the right.
2801 * Merge all three into a single extent record.
2803 left
.br_blockcount
+= new->br_blockcount
+ right
.br_blockcount
;
2805 xfs_iext_remove(ip
, icur
, state
);
2806 xfs_iext_prev(ifp
, icur
);
2807 xfs_iext_update_extent(ip
, state
, icur
, &left
);
2809 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2810 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
2812 rval
= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
2814 rval
= XFS_ILOG_CORE
;
2815 error
= xfs_bmbt_lookup_eq(cur
, &right
, &i
);
2818 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
2819 error
= -EFSCORRUPTED
;
2822 error
= xfs_btree_delete(cur
, &i
);
2825 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
2826 error
= -EFSCORRUPTED
;
2829 error
= xfs_btree_decrement(cur
, 0, &i
);
2832 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
2833 error
= -EFSCORRUPTED
;
2836 error
= xfs_bmbt_update(cur
, &left
);
2842 case BMAP_LEFT_CONTIG
:
2844 * New allocation is contiguous with a real allocation
2846 * Merge the new allocation with the left neighbor.
2849 left
.br_blockcount
+= new->br_blockcount
;
2851 xfs_iext_prev(ifp
, icur
);
2852 xfs_iext_update_extent(ip
, state
, icur
, &left
);
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
, &left
);
2871 case BMAP_RIGHT_CONTIG
:
2873 * New allocation is contiguous with a real allocation
2875 * Merge the new allocation with the right neighbor.
2879 right
.br_startoff
= new->br_startoff
;
2880 right
.br_startblock
= new->br_startblock
;
2881 right
.br_blockcount
+= new->br_blockcount
;
2882 xfs_iext_update_extent(ip
, state
, icur
, &right
);
2885 rval
= xfs_ilog_fext(whichfork
);
2888 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2891 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
2892 error
= -EFSCORRUPTED
;
2895 error
= xfs_bmbt_update(cur
, &right
);
2903 * New allocation is not contiguous with another
2905 * Insert a new entry.
2907 xfs_iext_insert(ip
, icur
, new, state
);
2908 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2909 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
2911 rval
= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
2913 rval
= XFS_ILOG_CORE
;
2914 error
= xfs_bmbt_lookup_eq(cur
, new, &i
);
2917 if (XFS_IS_CORRUPT(mp
, i
!= 0)) {
2918 error
= -EFSCORRUPTED
;
2921 error
= xfs_btree_insert(cur
, &i
);
2924 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
2925 error
= -EFSCORRUPTED
;
2932 /* add reverse mapping unless caller opted out */
2933 if (!(flags
& XFS_BMAPI_NORMAP
))
2934 xfs_rmap_map_extent(tp
, ip
, whichfork
, new);
2936 /* convert to a btree if necessary */
2937 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
2938 int tmp_logflags
; /* partial log flag return val */
2940 ASSERT(cur
== NULL
);
2941 error
= xfs_bmap_extents_to_btree(tp
, ip
, curp
, 0,
2942 &tmp_logflags
, whichfork
);
2943 *logflagsp
|= tmp_logflags
;
2949 /* clear out the allocated field, done with it now in any case. */
2951 cur
->bc_private
.b
.allocated
= 0;
2953 xfs_bmap_check_leaf_extents(cur
, ip
, whichfork
);
2960 * Functions used in the extent read, allocate and remove paths
2964 * Adjust the size of the new extent based on di_extsize and rt extsize.
2967 xfs_bmap_extsize_align(
2969 xfs_bmbt_irec_t
*gotp
, /* next extent pointer */
2970 xfs_bmbt_irec_t
*prevp
, /* previous extent pointer */
2971 xfs_extlen_t extsz
, /* align to this extent size */
2972 int rt
, /* is this a realtime inode? */
2973 int eof
, /* is extent at end-of-file? */
2974 int delay
, /* creating delalloc extent? */
2975 int convert
, /* overwriting unwritten extent? */
2976 xfs_fileoff_t
*offp
, /* in/out: aligned offset */
2977 xfs_extlen_t
*lenp
) /* in/out: aligned length */
2979 xfs_fileoff_t orig_off
; /* original offset */
2980 xfs_extlen_t orig_alen
; /* original length */
2981 xfs_fileoff_t orig_end
; /* original off+len */
2982 xfs_fileoff_t nexto
; /* next file offset */
2983 xfs_fileoff_t prevo
; /* previous file offset */
2984 xfs_fileoff_t align_off
; /* temp for offset */
2985 xfs_extlen_t align_alen
; /* temp for length */
2986 xfs_extlen_t temp
; /* temp for calculations */
2991 orig_off
= align_off
= *offp
;
2992 orig_alen
= align_alen
= *lenp
;
2993 orig_end
= orig_off
+ orig_alen
;
2996 * If this request overlaps an existing extent, then don't
2997 * attempt to perform any additional alignment.
2999 if (!delay
&& !eof
&&
3000 (orig_off
>= gotp
->br_startoff
) &&
3001 (orig_end
<= gotp
->br_startoff
+ gotp
->br_blockcount
)) {
3006 * If the file offset is unaligned vs. the extent size
3007 * we need to align it. This will be possible unless
3008 * the file was previously written with a kernel that didn't
3009 * perform this alignment, or if a truncate shot us in the
3012 div_u64_rem(orig_off
, extsz
, &temp
);
3018 /* Same adjustment for the end of the requested area. */
3019 temp
= (align_alen
% extsz
);
3021 align_alen
+= extsz
- temp
;
3024 * For large extent hint sizes, the aligned extent might be larger than
3025 * MAXEXTLEN. In that case, reduce the size by an extsz so that it pulls
3026 * the length back under MAXEXTLEN. The outer allocation loops handle
3027 * short allocation just fine, so it is safe to do this. We only want to
3028 * do it when we are forced to, though, because it means more allocation
3029 * operations are required.
3031 while (align_alen
> MAXEXTLEN
)
3032 align_alen
-= extsz
;
3033 ASSERT(align_alen
<= MAXEXTLEN
);
3036 * If the previous block overlaps with this proposed allocation
3037 * then move the start forward without adjusting the length.
3039 if (prevp
->br_startoff
!= NULLFILEOFF
) {
3040 if (prevp
->br_startblock
== HOLESTARTBLOCK
)
3041 prevo
= prevp
->br_startoff
;
3043 prevo
= prevp
->br_startoff
+ prevp
->br_blockcount
;
3046 if (align_off
!= orig_off
&& align_off
< prevo
)
3049 * If the next block overlaps with this proposed allocation
3050 * then move the start back without adjusting the length,
3051 * but not before offset 0.
3052 * This may of course make the start overlap previous block,
3053 * and if we hit the offset 0 limit then the next block
3054 * can still overlap too.
3056 if (!eof
&& gotp
->br_startoff
!= NULLFILEOFF
) {
3057 if ((delay
&& gotp
->br_startblock
== HOLESTARTBLOCK
) ||
3058 (!delay
&& gotp
->br_startblock
== DELAYSTARTBLOCK
))
3059 nexto
= gotp
->br_startoff
+ gotp
->br_blockcount
;
3061 nexto
= gotp
->br_startoff
;
3063 nexto
= NULLFILEOFF
;
3065 align_off
+ align_alen
!= orig_end
&&
3066 align_off
+ align_alen
> nexto
)
3067 align_off
= nexto
> align_alen
? nexto
- align_alen
: 0;
3069 * If we're now overlapping the next or previous extent that
3070 * means we can't fit an extsz piece in this hole. Just move
3071 * the start forward to the first valid spot and set
3072 * the length so we hit the end.
3074 if (align_off
!= orig_off
&& align_off
< prevo
)
3076 if (align_off
+ align_alen
!= orig_end
&&
3077 align_off
+ align_alen
> nexto
&&
3078 nexto
!= NULLFILEOFF
) {
3079 ASSERT(nexto
> prevo
);
3080 align_alen
= nexto
- align_off
;
3084 * If realtime, and the result isn't a multiple of the realtime
3085 * extent size we need to remove blocks until it is.
3087 if (rt
&& (temp
= (align_alen
% mp
->m_sb
.sb_rextsize
))) {
3089 * We're not covering the original request, or
3090 * we won't be able to once we fix the length.
3092 if (orig_off
< align_off
||
3093 orig_end
> align_off
+ align_alen
||
3094 align_alen
- temp
< orig_alen
)
3097 * Try to fix it by moving the start up.
3099 if (align_off
+ temp
<= orig_off
) {
3104 * Try to fix it by moving the end in.
3106 else if (align_off
+ align_alen
- temp
>= orig_end
)
3109 * Set the start to the minimum then trim the length.
3112 align_alen
-= orig_off
- align_off
;
3113 align_off
= orig_off
;
3114 align_alen
-= align_alen
% mp
->m_sb
.sb_rextsize
;
3117 * Result doesn't cover the request, fail it.
3119 if (orig_off
< align_off
|| orig_end
> align_off
+ align_alen
)
3122 ASSERT(orig_off
>= align_off
);
3123 /* see MAXEXTLEN handling above */
3124 ASSERT(orig_end
<= align_off
+ align_alen
||
3125 align_alen
+ extsz
> MAXEXTLEN
);
3129 if (!eof
&& gotp
->br_startoff
!= NULLFILEOFF
)
3130 ASSERT(align_off
+ align_alen
<= gotp
->br_startoff
);
3131 if (prevp
->br_startoff
!= NULLFILEOFF
)
3132 ASSERT(align_off
>= prevp
->br_startoff
+ prevp
->br_blockcount
);
3140 #define XFS_ALLOC_GAP_UNITS 4
3144 struct xfs_bmalloca
*ap
) /* bmap alloc argument struct */
3146 xfs_fsblock_t adjust
; /* adjustment to block numbers */
3147 xfs_agnumber_t fb_agno
; /* ag number of ap->firstblock */
3148 xfs_mount_t
*mp
; /* mount point structure */
3149 int nullfb
; /* true if ap->firstblock isn't set */
3150 int rt
; /* true if inode is realtime */
3152 #define ISVALID(x,y) \
3154 (x) < mp->m_sb.sb_rblocks : \
3155 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
3156 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
3157 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
3159 mp
= ap
->ip
->i_mount
;
3160 nullfb
= ap
->tp
->t_firstblock
== NULLFSBLOCK
;
3161 rt
= XFS_IS_REALTIME_INODE(ap
->ip
) &&
3162 (ap
->datatype
& XFS_ALLOC_USERDATA
);
3163 fb_agno
= nullfb
? NULLAGNUMBER
: XFS_FSB_TO_AGNO(mp
,
3164 ap
->tp
->t_firstblock
);
3166 * If allocating at eof, and there's a previous real block,
3167 * try to use its last block as our starting point.
3169 if (ap
->eof
&& ap
->prev
.br_startoff
!= NULLFILEOFF
&&
3170 !isnullstartblock(ap
->prev
.br_startblock
) &&
3171 ISVALID(ap
->prev
.br_startblock
+ ap
->prev
.br_blockcount
,
3172 ap
->prev
.br_startblock
)) {
3173 ap
->blkno
= ap
->prev
.br_startblock
+ ap
->prev
.br_blockcount
;
3175 * Adjust for the gap between prevp and us.
3177 adjust
= ap
->offset
-
3178 (ap
->prev
.br_startoff
+ ap
->prev
.br_blockcount
);
3180 ISVALID(ap
->blkno
+ adjust
, ap
->prev
.br_startblock
))
3181 ap
->blkno
+= adjust
;
3184 * If not at eof, then compare the two neighbor blocks.
3185 * Figure out whether either one gives us a good starting point,
3186 * and pick the better one.
3188 else if (!ap
->eof
) {
3189 xfs_fsblock_t gotbno
; /* right side block number */
3190 xfs_fsblock_t gotdiff
=0; /* right side difference */
3191 xfs_fsblock_t prevbno
; /* left side block number */
3192 xfs_fsblock_t prevdiff
=0; /* left side difference */
3195 * If there's a previous (left) block, select a requested
3196 * start block based on it.
3198 if (ap
->prev
.br_startoff
!= NULLFILEOFF
&&
3199 !isnullstartblock(ap
->prev
.br_startblock
) &&
3200 (prevbno
= ap
->prev
.br_startblock
+
3201 ap
->prev
.br_blockcount
) &&
3202 ISVALID(prevbno
, ap
->prev
.br_startblock
)) {
3204 * Calculate gap to end of previous block.
3206 adjust
= prevdiff
= ap
->offset
-
3207 (ap
->prev
.br_startoff
+
3208 ap
->prev
.br_blockcount
);
3210 * Figure the startblock based on the previous block's
3211 * end and the gap size.
3213 * If the gap is large relative to the piece we're
3214 * allocating, or using it gives us an invalid block
3215 * number, then just use the end of the previous block.
3217 if (prevdiff
<= XFS_ALLOC_GAP_UNITS
* ap
->length
&&
3218 ISVALID(prevbno
+ prevdiff
,
3219 ap
->prev
.br_startblock
))
3224 * If the firstblock forbids it, can't use it,
3227 if (!rt
&& !nullfb
&&
3228 XFS_FSB_TO_AGNO(mp
, prevbno
) != fb_agno
)
3229 prevbno
= NULLFSBLOCK
;
3232 * No previous block or can't follow it, just default.
3235 prevbno
= NULLFSBLOCK
;
3237 * If there's a following (right) block, select a requested
3238 * start block based on it.
3240 if (!isnullstartblock(ap
->got
.br_startblock
)) {
3242 * Calculate gap to start of next block.
3244 adjust
= gotdiff
= ap
->got
.br_startoff
- ap
->offset
;
3246 * Figure the startblock based on the next block's
3247 * start and the gap size.
3249 gotbno
= ap
->got
.br_startblock
;
3252 * If the gap is large relative to the piece we're
3253 * allocating, or using it gives us an invalid block
3254 * number, then just use the start of the next block
3255 * offset by our length.
3257 if (gotdiff
<= XFS_ALLOC_GAP_UNITS
* ap
->length
&&
3258 ISVALID(gotbno
- gotdiff
, gotbno
))
3260 else if (ISVALID(gotbno
- ap
->length
, gotbno
)) {
3261 gotbno
-= ap
->length
;
3262 gotdiff
+= adjust
- ap
->length
;
3266 * If the firstblock forbids it, can't use it,
3269 if (!rt
&& !nullfb
&&
3270 XFS_FSB_TO_AGNO(mp
, gotbno
) != fb_agno
)
3271 gotbno
= NULLFSBLOCK
;
3274 * No next block, just default.
3277 gotbno
= NULLFSBLOCK
;
3279 * If both valid, pick the better one, else the only good
3280 * one, else ap->blkno is already set (to 0 or the inode block).
3282 if (prevbno
!= NULLFSBLOCK
&& gotbno
!= NULLFSBLOCK
)
3283 ap
->blkno
= prevdiff
<= gotdiff
? prevbno
: gotbno
;
3284 else if (prevbno
!= NULLFSBLOCK
)
3285 ap
->blkno
= prevbno
;
3286 else if (gotbno
!= NULLFSBLOCK
)
3293 xfs_bmap_longest_free_extent(
3294 struct xfs_trans
*tp
,
3299 struct xfs_mount
*mp
= tp
->t_mountp
;
3300 struct xfs_perag
*pag
;
3301 xfs_extlen_t longest
;
3304 pag
= xfs_perag_get(mp
, ag
);
3305 if (!pag
->pagf_init
) {
3306 error
= xfs_alloc_pagf_init(mp
, tp
, ag
, XFS_ALLOC_FLAG_TRYLOCK
);
3308 /* Couldn't lock the AGF, so skip this AG. */
3309 if (error
== -EAGAIN
) {
3317 longest
= xfs_alloc_longest_free_extent(pag
,
3318 xfs_alloc_min_freelist(mp
, pag
),
3319 xfs_ag_resv_needed(pag
, XFS_AG_RESV_NONE
));
3320 if (*blen
< longest
)
3329 xfs_bmap_select_minlen(
3330 struct xfs_bmalloca
*ap
,
3331 struct xfs_alloc_arg
*args
,
3335 if (notinit
|| *blen
< ap
->minlen
) {
3337 * Since we did a BUF_TRYLOCK above, it is possible that
3338 * there is space for this request.
3340 args
->minlen
= ap
->minlen
;
3341 } else if (*blen
< args
->maxlen
) {
3343 * If the best seen length is less than the request length,
3344 * use the best as the minimum.
3346 args
->minlen
= *blen
;
3349 * Otherwise we've seen an extent as big as maxlen, use that
3352 args
->minlen
= args
->maxlen
;
3357 xfs_bmap_btalloc_nullfb(
3358 struct xfs_bmalloca
*ap
,
3359 struct xfs_alloc_arg
*args
,
3362 struct xfs_mount
*mp
= ap
->ip
->i_mount
;
3363 xfs_agnumber_t ag
, startag
;
3367 args
->type
= XFS_ALLOCTYPE_START_BNO
;
3368 args
->total
= ap
->total
;
3370 startag
= ag
= XFS_FSB_TO_AGNO(mp
, args
->fsbno
);
3371 if (startag
== NULLAGNUMBER
)
3374 while (*blen
< args
->maxlen
) {
3375 error
= xfs_bmap_longest_free_extent(args
->tp
, ag
, blen
,
3380 if (++ag
== mp
->m_sb
.sb_agcount
)
3386 xfs_bmap_select_minlen(ap
, args
, blen
, notinit
);
3391 xfs_bmap_btalloc_filestreams(
3392 struct xfs_bmalloca
*ap
,
3393 struct xfs_alloc_arg
*args
,
3396 struct xfs_mount
*mp
= ap
->ip
->i_mount
;
3401 args
->type
= XFS_ALLOCTYPE_NEAR_BNO
;
3402 args
->total
= ap
->total
;
3404 ag
= XFS_FSB_TO_AGNO(mp
, args
->fsbno
);
3405 if (ag
== NULLAGNUMBER
)
3408 error
= xfs_bmap_longest_free_extent(args
->tp
, ag
, blen
, ¬init
);
3412 if (*blen
< args
->maxlen
) {
3413 error
= xfs_filestream_new_ag(ap
, &ag
);
3417 error
= xfs_bmap_longest_free_extent(args
->tp
, ag
, blen
,
3424 xfs_bmap_select_minlen(ap
, args
, blen
, notinit
);
3427 * Set the failure fallback case to look in the selected AG as stream
3430 ap
->blkno
= args
->fsbno
= XFS_AGB_TO_FSB(mp
, ag
, 0);
3434 /* Update all inode and quota accounting for the allocation we just did. */
3436 xfs_bmap_btalloc_accounting(
3437 struct xfs_bmalloca
*ap
,
3438 struct xfs_alloc_arg
*args
)
3440 if (ap
->flags
& XFS_BMAPI_COWFORK
) {
3442 * COW fork blocks are in-core only and thus are treated as
3443 * in-core quota reservation (like delalloc blocks) even when
3444 * converted to real blocks. The quota reservation is not
3445 * accounted to disk until blocks are remapped to the data
3446 * fork. So if these blocks were previously delalloc, we
3447 * already have quota reservation and there's nothing to do
3451 xfs_mod_delalloc(ap
->ip
->i_mount
, -(int64_t)args
->len
);
3456 * Otherwise, we've allocated blocks in a hole. The transaction
3457 * has acquired in-core quota reservation for this extent.
3458 * Rather than account these as real blocks, however, we reduce
3459 * the transaction quota reservation based on the allocation.
3460 * This essentially transfers the transaction quota reservation
3461 * to that of a delalloc extent.
3463 ap
->ip
->i_delayed_blks
+= args
->len
;
3464 xfs_trans_mod_dquot_byino(ap
->tp
, ap
->ip
, XFS_TRANS_DQ_RES_BLKS
,
3469 /* data/attr fork only */
3470 ap
->ip
->i_d
.di_nblocks
+= args
->len
;
3471 xfs_trans_log_inode(ap
->tp
, ap
->ip
, XFS_ILOG_CORE
);
3473 ap
->ip
->i_delayed_blks
-= args
->len
;
3474 xfs_mod_delalloc(ap
->ip
->i_mount
, -(int64_t)args
->len
);
3476 xfs_trans_mod_dquot_byino(ap
->tp
, ap
->ip
,
3477 ap
->wasdel
? XFS_TRANS_DQ_DELBCOUNT
: XFS_TRANS_DQ_BCOUNT
,
3483 struct xfs_bmalloca
*ap
) /* bmap alloc argument struct */
3485 xfs_mount_t
*mp
; /* mount point structure */
3486 xfs_alloctype_t atype
= 0; /* type for allocation routines */
3487 xfs_extlen_t align
= 0; /* minimum allocation alignment */
3488 xfs_agnumber_t fb_agno
; /* ag number of ap->firstblock */
3490 xfs_alloc_arg_t args
;
3491 xfs_fileoff_t orig_offset
;
3492 xfs_extlen_t orig_length
;
3494 xfs_extlen_t nextminlen
= 0;
3495 int nullfb
; /* true if ap->firstblock isn't set */
3502 orig_offset
= ap
->offset
;
3503 orig_length
= ap
->length
;
3505 mp
= ap
->ip
->i_mount
;
3507 /* stripe alignment for allocation is determined by mount parameters */
3509 if (mp
->m_swidth
&& (mp
->m_flags
& XFS_MOUNT_SWALLOC
))
3510 stripe_align
= mp
->m_swidth
;
3511 else if (mp
->m_dalign
)
3512 stripe_align
= mp
->m_dalign
;
3514 if (ap
->flags
& XFS_BMAPI_COWFORK
)
3515 align
= xfs_get_cowextsz_hint(ap
->ip
);
3516 else if (ap
->datatype
& XFS_ALLOC_USERDATA
)
3517 align
= xfs_get_extsz_hint(ap
->ip
);
3519 error
= xfs_bmap_extsize_align(mp
, &ap
->got
, &ap
->prev
,
3520 align
, 0, ap
->eof
, 0, ap
->conv
,
3521 &ap
->offset
, &ap
->length
);
3527 nullfb
= ap
->tp
->t_firstblock
== NULLFSBLOCK
;
3528 fb_agno
= nullfb
? NULLAGNUMBER
: XFS_FSB_TO_AGNO(mp
,
3529 ap
->tp
->t_firstblock
);
3531 if ((ap
->datatype
& XFS_ALLOC_USERDATA
) &&
3532 xfs_inode_is_filestream(ap
->ip
)) {
3533 ag
= xfs_filestream_lookup_ag(ap
->ip
);
3534 ag
= (ag
!= NULLAGNUMBER
) ? ag
: 0;
3535 ap
->blkno
= XFS_AGB_TO_FSB(mp
, ag
, 0);
3537 ap
->blkno
= XFS_INO_TO_FSB(mp
, ap
->ip
->i_ino
);
3540 ap
->blkno
= ap
->tp
->t_firstblock
;
3542 xfs_bmap_adjacent(ap
);
3545 * If allowed, use ap->blkno; otherwise must use firstblock since
3546 * it's in the right allocation group.
3548 if (nullfb
|| XFS_FSB_TO_AGNO(mp
, ap
->blkno
) == fb_agno
)
3551 ap
->blkno
= ap
->tp
->t_firstblock
;
3553 * Normal allocation, done through xfs_alloc_vextent.
3555 tryagain
= isaligned
= 0;
3556 memset(&args
, 0, sizeof(args
));
3559 args
.fsbno
= ap
->blkno
;
3560 args
.oinfo
= XFS_RMAP_OINFO_SKIP_UPDATE
;
3562 /* Trim the allocation back to the maximum an AG can fit. */
3563 args
.maxlen
= min(ap
->length
, mp
->m_ag_max_usable
);
3567 * Search for an allocation group with a single extent large
3568 * enough for the request. If one isn't found, then adjust
3569 * the minimum allocation size to the largest space found.
3571 if ((ap
->datatype
& XFS_ALLOC_USERDATA
) &&
3572 xfs_inode_is_filestream(ap
->ip
))
3573 error
= xfs_bmap_btalloc_filestreams(ap
, &args
, &blen
);
3575 error
= xfs_bmap_btalloc_nullfb(ap
, &args
, &blen
);
3578 } else if (ap
->tp
->t_flags
& XFS_TRANS_LOWMODE
) {
3579 if (xfs_inode_is_filestream(ap
->ip
))
3580 args
.type
= XFS_ALLOCTYPE_FIRST_AG
;
3582 args
.type
= XFS_ALLOCTYPE_START_BNO
;
3583 args
.total
= args
.minlen
= ap
->minlen
;
3585 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
3586 args
.total
= ap
->total
;
3587 args
.minlen
= ap
->minlen
;
3589 /* apply extent size hints if obtained earlier */
3592 div_u64_rem(ap
->offset
, args
.prod
, &args
.mod
);
3594 args
.mod
= args
.prod
- args
.mod
;
3595 } else if (mp
->m_sb
.sb_blocksize
>= PAGE_SIZE
) {
3599 args
.prod
= PAGE_SIZE
>> mp
->m_sb
.sb_blocklog
;
3600 div_u64_rem(ap
->offset
, args
.prod
, &args
.mod
);
3602 args
.mod
= args
.prod
- args
.mod
;
3605 * If we are not low on available data blocks, and the underlying
3606 * logical volume manager is a stripe, and the file offset is zero then
3607 * try to allocate data blocks on stripe unit boundary. NOTE: ap->aeof
3608 * is only set if the allocation length is >= the stripe unit and the
3609 * allocation offset is at the end of file.
3611 if (!(ap
->tp
->t_flags
& XFS_TRANS_LOWMODE
) && ap
->aeof
) {
3613 args
.alignment
= stripe_align
;
3617 * Adjust minlen to try and preserve alignment if we
3618 * can't guarantee an aligned maxlen extent.
3620 if (blen
> args
.alignment
&&
3621 blen
<= args
.maxlen
+ args
.alignment
)
3622 args
.minlen
= blen
- args
.alignment
;
3623 args
.minalignslop
= 0;
3626 * First try an exact bno allocation.
3627 * If it fails then do a near or start bno
3628 * allocation with alignment turned on.
3632 args
.type
= XFS_ALLOCTYPE_THIS_BNO
;
3635 * Compute the minlen+alignment for the
3636 * next case. Set slop so that the value
3637 * of minlen+alignment+slop doesn't go up
3638 * between the calls.
3640 if (blen
> stripe_align
&& blen
<= args
.maxlen
)
3641 nextminlen
= blen
- stripe_align
;
3643 nextminlen
= args
.minlen
;
3644 if (nextminlen
+ stripe_align
> args
.minlen
+ 1)
3646 nextminlen
+ stripe_align
-
3649 args
.minalignslop
= 0;
3653 args
.minalignslop
= 0;
3655 args
.minleft
= ap
->minleft
;
3656 args
.wasdel
= ap
->wasdel
;
3657 args
.resv
= XFS_AG_RESV_NONE
;
3658 args
.datatype
= ap
->datatype
;
3660 error
= xfs_alloc_vextent(&args
);
3664 if (tryagain
&& args
.fsbno
== NULLFSBLOCK
) {
3666 * Exact allocation failed. Now try with alignment
3670 args
.fsbno
= ap
->blkno
;
3671 args
.alignment
= stripe_align
;
3672 args
.minlen
= nextminlen
;
3673 args
.minalignslop
= 0;
3675 if ((error
= xfs_alloc_vextent(&args
)))
3678 if (isaligned
&& args
.fsbno
== NULLFSBLOCK
) {
3680 * allocation failed, so turn off alignment and
3684 args
.fsbno
= ap
->blkno
;
3686 if ((error
= xfs_alloc_vextent(&args
)))
3689 if (args
.fsbno
== NULLFSBLOCK
&& nullfb
&&
3690 args
.minlen
> ap
->minlen
) {
3691 args
.minlen
= ap
->minlen
;
3692 args
.type
= XFS_ALLOCTYPE_START_BNO
;
3693 args
.fsbno
= ap
->blkno
;
3694 if ((error
= xfs_alloc_vextent(&args
)))
3697 if (args
.fsbno
== NULLFSBLOCK
&& nullfb
) {
3699 args
.type
= XFS_ALLOCTYPE_FIRST_AG
;
3700 args
.total
= ap
->minlen
;
3701 if ((error
= xfs_alloc_vextent(&args
)))
3703 ap
->tp
->t_flags
|= XFS_TRANS_LOWMODE
;
3705 if (args
.fsbno
!= NULLFSBLOCK
) {
3707 * check the allocation happened at the same or higher AG than
3708 * the first block that was allocated.
3710 ASSERT(ap
->tp
->t_firstblock
== NULLFSBLOCK
||
3711 XFS_FSB_TO_AGNO(mp
, ap
->tp
->t_firstblock
) <=
3712 XFS_FSB_TO_AGNO(mp
, args
.fsbno
));
3714 ap
->blkno
= args
.fsbno
;
3715 if (ap
->tp
->t_firstblock
== NULLFSBLOCK
)
3716 ap
->tp
->t_firstblock
= args
.fsbno
;
3717 ASSERT(nullfb
|| fb_agno
<= args
.agno
);
3718 ap
->length
= args
.len
;
3720 * If the extent size hint is active, we tried to round the
3721 * caller's allocation request offset down to extsz and the
3722 * length up to another extsz boundary. If we found a free
3723 * extent we mapped it in starting at this new offset. If the
3724 * newly mapped space isn't long enough to cover any of the
3725 * range of offsets that was originally requested, move the
3726 * mapping up so that we can fill as much of the caller's
3727 * original request as possible. Free space is apparently
3728 * very fragmented so we're unlikely to be able to satisfy the
3731 if (ap
->length
<= orig_length
)
3732 ap
->offset
= orig_offset
;
3733 else if (ap
->offset
+ ap
->length
< orig_offset
+ orig_length
)
3734 ap
->offset
= orig_offset
+ orig_length
- ap
->length
;
3735 xfs_bmap_btalloc_accounting(ap
, &args
);
3737 ap
->blkno
= NULLFSBLOCK
;
3743 /* Trim extent to fit a logical block range. */
3746 struct xfs_bmbt_irec
*irec
,
3750 xfs_fileoff_t distance
;
3751 xfs_fileoff_t end
= bno
+ len
;
3753 if (irec
->br_startoff
+ irec
->br_blockcount
<= bno
||
3754 irec
->br_startoff
>= end
) {
3755 irec
->br_blockcount
= 0;
3759 if (irec
->br_startoff
< bno
) {
3760 distance
= bno
- irec
->br_startoff
;
3761 if (isnullstartblock(irec
->br_startblock
))
3762 irec
->br_startblock
= DELAYSTARTBLOCK
;
3763 if (irec
->br_startblock
!= DELAYSTARTBLOCK
&&
3764 irec
->br_startblock
!= HOLESTARTBLOCK
)
3765 irec
->br_startblock
+= distance
;
3766 irec
->br_startoff
+= distance
;
3767 irec
->br_blockcount
-= distance
;
3770 if (end
< irec
->br_startoff
+ irec
->br_blockcount
) {
3771 distance
= irec
->br_startoff
+ irec
->br_blockcount
- end
;
3772 irec
->br_blockcount
-= distance
;
3777 * Trim the returned map to the required bounds
3781 struct xfs_bmbt_irec
*mval
,
3782 struct xfs_bmbt_irec
*got
,
3790 if ((flags
& XFS_BMAPI_ENTIRE
) ||
3791 got
->br_startoff
+ got
->br_blockcount
<= obno
) {
3793 if (isnullstartblock(got
->br_startblock
))
3794 mval
->br_startblock
= DELAYSTARTBLOCK
;
3800 ASSERT((*bno
>= obno
) || (n
== 0));
3802 mval
->br_startoff
= *bno
;
3803 if (isnullstartblock(got
->br_startblock
))
3804 mval
->br_startblock
= DELAYSTARTBLOCK
;
3806 mval
->br_startblock
= got
->br_startblock
+
3807 (*bno
- got
->br_startoff
);
3809 * Return the minimum of what we got and what we asked for for
3810 * the length. We can use the len variable here because it is
3811 * modified below and we could have been there before coming
3812 * here if the first part of the allocation didn't overlap what
3815 mval
->br_blockcount
= XFS_FILBLKS_MIN(end
- *bno
,
3816 got
->br_blockcount
- (*bno
- got
->br_startoff
));
3817 mval
->br_state
= got
->br_state
;
3818 ASSERT(mval
->br_blockcount
<= len
);
3823 * Update and validate the extent map to return
3826 xfs_bmapi_update_map(
3827 struct xfs_bmbt_irec
**map
,
3835 xfs_bmbt_irec_t
*mval
= *map
;
3837 ASSERT((flags
& XFS_BMAPI_ENTIRE
) ||
3838 ((mval
->br_startoff
+ mval
->br_blockcount
) <= end
));
3839 ASSERT((flags
& XFS_BMAPI_ENTIRE
) || (mval
->br_blockcount
<= *len
) ||
3840 (mval
->br_startoff
< obno
));
3842 *bno
= mval
->br_startoff
+ mval
->br_blockcount
;
3844 if (*n
> 0 && mval
->br_startoff
== mval
[-1].br_startoff
) {
3845 /* update previous map with new information */
3846 ASSERT(mval
->br_startblock
== mval
[-1].br_startblock
);
3847 ASSERT(mval
->br_blockcount
> mval
[-1].br_blockcount
);
3848 ASSERT(mval
->br_state
== mval
[-1].br_state
);
3849 mval
[-1].br_blockcount
= mval
->br_blockcount
;
3850 mval
[-1].br_state
= mval
->br_state
;
3851 } else if (*n
> 0 && mval
->br_startblock
!= DELAYSTARTBLOCK
&&
3852 mval
[-1].br_startblock
!= DELAYSTARTBLOCK
&&
3853 mval
[-1].br_startblock
!= HOLESTARTBLOCK
&&
3854 mval
->br_startblock
== mval
[-1].br_startblock
+
3855 mval
[-1].br_blockcount
&&
3856 mval
[-1].br_state
== mval
->br_state
) {
3857 ASSERT(mval
->br_startoff
==
3858 mval
[-1].br_startoff
+ mval
[-1].br_blockcount
);
3859 mval
[-1].br_blockcount
+= mval
->br_blockcount
;
3860 } else if (*n
> 0 &&
3861 mval
->br_startblock
== DELAYSTARTBLOCK
&&
3862 mval
[-1].br_startblock
== DELAYSTARTBLOCK
&&
3863 mval
->br_startoff
==
3864 mval
[-1].br_startoff
+ mval
[-1].br_blockcount
) {
3865 mval
[-1].br_blockcount
+= mval
->br_blockcount
;
3866 mval
[-1].br_state
= mval
->br_state
;
3867 } else if (!((*n
== 0) &&
3868 ((mval
->br_startoff
+ mval
->br_blockcount
) <=
3877 * Map file blocks to filesystem blocks without allocation.
3881 struct xfs_inode
*ip
,
3884 struct xfs_bmbt_irec
*mval
,
3888 struct xfs_mount
*mp
= ip
->i_mount
;
3889 struct xfs_ifork
*ifp
;
3890 struct xfs_bmbt_irec got
;
3893 struct xfs_iext_cursor icur
;
3897 int whichfork
= xfs_bmapi_whichfork(flags
);
3900 ASSERT(!(flags
& ~(XFS_BMAPI_ATTRFORK
|XFS_BMAPI_ENTIRE
|
3901 XFS_BMAPI_COWFORK
)));
3902 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_SHARED
|XFS_ILOCK_EXCL
));
3904 if (XFS_IS_CORRUPT(mp
, !xfs_ifork_has_extents(ip
, whichfork
)) ||
3905 XFS_TEST_ERROR(false, mp
, XFS_ERRTAG_BMAPIFORMAT
)) {
3906 return -EFSCORRUPTED
;
3909 if (XFS_FORCED_SHUTDOWN(mp
))
3912 XFS_STATS_INC(mp
, xs_blk_mapr
);
3914 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
3916 /* No CoW fork? Return a hole. */
3917 if (whichfork
== XFS_COW_FORK
) {
3918 mval
->br_startoff
= bno
;
3919 mval
->br_startblock
= HOLESTARTBLOCK
;
3920 mval
->br_blockcount
= len
;
3921 mval
->br_state
= XFS_EXT_NORM
;
3927 * A missing attr ifork implies that the inode says we're in
3928 * extents or btree format but failed to pass the inode fork
3929 * verifier while trying to load it. Treat that as a file
3933 xfs_alert(mp
, "%s: inode %llu missing fork %d",
3934 __func__
, ip
->i_ino
, whichfork
);
3936 return -EFSCORRUPTED
;
3939 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
3940 error
= xfs_iread_extents(NULL
, ip
, whichfork
);
3945 if (!xfs_iext_lookup_extent(ip
, ifp
, bno
, &icur
, &got
))
3950 while (bno
< end
&& n
< *nmap
) {
3951 /* Reading past eof, act as though there's a hole up to end. */
3953 got
.br_startoff
= end
;
3954 if (got
.br_startoff
> bno
) {
3955 /* Reading in a hole. */
3956 mval
->br_startoff
= bno
;
3957 mval
->br_startblock
= HOLESTARTBLOCK
;
3958 mval
->br_blockcount
=
3959 XFS_FILBLKS_MIN(len
, got
.br_startoff
- bno
);
3960 mval
->br_state
= XFS_EXT_NORM
;
3961 bno
+= mval
->br_blockcount
;
3962 len
-= mval
->br_blockcount
;
3968 /* set up the extent map to return. */
3969 xfs_bmapi_trim_map(mval
, &got
, &bno
, len
, obno
, end
, n
, flags
);
3970 xfs_bmapi_update_map(&mval
, &bno
, &len
, obno
, end
, &n
, flags
);
3972 /* If we're done, stop now. */
3973 if (bno
>= end
|| n
>= *nmap
)
3976 /* Else go on to the next record. */
3977 if (!xfs_iext_next_extent(ifp
, &icur
, &got
))
3985 * Add a delayed allocation extent to an inode. Blocks are reserved from the
3986 * global pool and the extent inserted into the inode in-core extent tree.
3988 * On entry, got refers to the first extent beyond the offset of the extent to
3989 * allocate or eof is specified if no such extent exists. On return, got refers
3990 * to the extent record that was inserted to the inode fork.
3992 * Note that the allocated extent may have been merged with contiguous extents
3993 * during insertion into the inode fork. Thus, got does not reflect the current
3994 * state of the inode fork on return. If necessary, the caller can use lastx to
3995 * look up the updated record in the inode fork.
3998 xfs_bmapi_reserve_delalloc(
3999 struct xfs_inode
*ip
,
4003 xfs_filblks_t prealloc
,
4004 struct xfs_bmbt_irec
*got
,
4005 struct xfs_iext_cursor
*icur
,
4008 struct xfs_mount
*mp
= ip
->i_mount
;
4009 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4011 xfs_extlen_t indlen
;
4013 xfs_fileoff_t aoff
= off
;
4016 * Cap the alloc length. Keep track of prealloc so we know whether to
4017 * tag the inode before we return.
4019 alen
= XFS_FILBLKS_MIN(len
+ prealloc
, MAXEXTLEN
);
4021 alen
= XFS_FILBLKS_MIN(alen
, got
->br_startoff
- aoff
);
4022 if (prealloc
&& alen
>= len
)
4023 prealloc
= alen
- len
;
4025 /* Figure out the extent size, adjust alen */
4026 if (whichfork
== XFS_COW_FORK
) {
4027 struct xfs_bmbt_irec prev
;
4028 xfs_extlen_t extsz
= xfs_get_cowextsz_hint(ip
);
4030 if (!xfs_iext_peek_prev_extent(ifp
, icur
, &prev
))
4031 prev
.br_startoff
= NULLFILEOFF
;
4033 error
= xfs_bmap_extsize_align(mp
, got
, &prev
, extsz
, 0, eof
,
4034 1, 0, &aoff
, &alen
);
4039 * Make a transaction-less quota reservation for delayed allocation
4040 * blocks. This number gets adjusted later. We return if we haven't
4041 * allocated blocks already inside this loop.
4043 error
= xfs_trans_reserve_quota_nblks(NULL
, ip
, (long)alen
, 0,
4044 XFS_QMOPT_RES_REGBLKS
);
4049 * Split changing sb for alen and indlen since they could be coming
4050 * from different places.
4052 indlen
= (xfs_extlen_t
)xfs_bmap_worst_indlen(ip
, alen
);
4055 error
= xfs_mod_fdblocks(mp
, -((int64_t)alen
), false);
4057 goto out_unreserve_quota
;
4059 error
= xfs_mod_fdblocks(mp
, -((int64_t)indlen
), false);
4061 goto out_unreserve_blocks
;
4064 ip
->i_delayed_blks
+= alen
;
4065 xfs_mod_delalloc(ip
->i_mount
, alen
+ indlen
);
4067 got
->br_startoff
= aoff
;
4068 got
->br_startblock
= nullstartblock(indlen
);
4069 got
->br_blockcount
= alen
;
4070 got
->br_state
= XFS_EXT_NORM
;
4072 xfs_bmap_add_extent_hole_delay(ip
, whichfork
, icur
, got
);
4075 * Tag the inode if blocks were preallocated. Note that COW fork
4076 * preallocation can occur at the start or end of the extent, even when
4077 * prealloc == 0, so we must also check the aligned offset and length.
4079 if (whichfork
== XFS_DATA_FORK
&& prealloc
)
4080 xfs_inode_set_eofblocks_tag(ip
);
4081 if (whichfork
== XFS_COW_FORK
&& (prealloc
|| aoff
< off
|| alen
> len
))
4082 xfs_inode_set_cowblocks_tag(ip
);
4086 out_unreserve_blocks
:
4087 xfs_mod_fdblocks(mp
, alen
, false);
4088 out_unreserve_quota
:
4089 if (XFS_IS_QUOTA_ON(mp
))
4090 xfs_trans_unreserve_quota_nblks(NULL
, ip
, (long)alen
, 0,
4091 XFS_QMOPT_RES_REGBLKS
);
4096 xfs_bmap_alloc_userdata(
4097 struct xfs_bmalloca
*bma
)
4099 struct xfs_mount
*mp
= bma
->ip
->i_mount
;
4100 int whichfork
= xfs_bmapi_whichfork(bma
->flags
);
4104 * Set the data type being allocated. For the data fork, the first data
4105 * in the file is treated differently to all other allocations. For the
4106 * attribute fork, we only need to ensure the allocated range is not on
4109 bma
->datatype
= XFS_ALLOC_NOBUSY
;
4110 if (whichfork
== XFS_DATA_FORK
) {
4111 bma
->datatype
|= XFS_ALLOC_USERDATA
;
4112 if (bma
->offset
== 0)
4113 bma
->datatype
|= XFS_ALLOC_INITIAL_USER_DATA
;
4115 if (mp
->m_dalign
&& bma
->length
>= mp
->m_dalign
) {
4116 error
= xfs_bmap_isaeof(bma
, whichfork
);
4121 if (XFS_IS_REALTIME_INODE(bma
->ip
))
4122 return xfs_bmap_rtalloc(bma
);
4125 return xfs_bmap_btalloc(bma
);
4130 struct xfs_bmalloca
*bma
)
4132 struct xfs_mount
*mp
= bma
->ip
->i_mount
;
4133 int whichfork
= xfs_bmapi_whichfork(bma
->flags
);
4134 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
4135 int tmp_logflags
= 0;
4138 ASSERT(bma
->length
> 0);
4141 * For the wasdelay case, we could also just allocate the stuff asked
4142 * for in this bmap call but that wouldn't be as good.
4145 bma
->length
= (xfs_extlen_t
)bma
->got
.br_blockcount
;
4146 bma
->offset
= bma
->got
.br_startoff
;
4147 if (!xfs_iext_peek_prev_extent(ifp
, &bma
->icur
, &bma
->prev
))
4148 bma
->prev
.br_startoff
= NULLFILEOFF
;
4150 bma
->length
= XFS_FILBLKS_MIN(bma
->length
, MAXEXTLEN
);
4152 bma
->length
= XFS_FILBLKS_MIN(bma
->length
,
4153 bma
->got
.br_startoff
- bma
->offset
);
4156 if (bma
->flags
& XFS_BMAPI_CONTIG
)
4157 bma
->minlen
= bma
->length
;
4161 if (bma
->flags
& XFS_BMAPI_METADATA
)
4162 error
= xfs_bmap_btalloc(bma
);
4164 error
= xfs_bmap_alloc_userdata(bma
);
4165 if (error
|| bma
->blkno
== NULLFSBLOCK
)
4168 if (bma
->flags
& XFS_BMAPI_ZERO
) {
4169 error
= xfs_zero_extent(bma
->ip
, bma
->blkno
, bma
->length
);
4174 if ((ifp
->if_flags
& XFS_IFBROOT
) && !bma
->cur
)
4175 bma
->cur
= xfs_bmbt_init_cursor(mp
, bma
->tp
, bma
->ip
, whichfork
);
4177 * Bump the number of extents we've allocated
4183 bma
->cur
->bc_private
.b
.flags
=
4184 bma
->wasdel
? XFS_BTCUR_BPRV_WASDEL
: 0;
4186 bma
->got
.br_startoff
= bma
->offset
;
4187 bma
->got
.br_startblock
= bma
->blkno
;
4188 bma
->got
.br_blockcount
= bma
->length
;
4189 bma
->got
.br_state
= XFS_EXT_NORM
;
4192 * In the data fork, a wasdelay extent has been initialized, so
4193 * shouldn't be flagged as unwritten.
4195 * For the cow fork, however, we convert delalloc reservations
4196 * (extents allocated for speculative preallocation) to
4197 * allocated unwritten extents, and only convert the unwritten
4198 * extents to real extents when we're about to write the data.
4200 if ((!bma
->wasdel
|| (bma
->flags
& XFS_BMAPI_COWFORK
)) &&
4201 (bma
->flags
& XFS_BMAPI_PREALLOC
))
4202 bma
->got
.br_state
= XFS_EXT_UNWRITTEN
;
4205 error
= xfs_bmap_add_extent_delay_real(bma
, whichfork
);
4207 error
= xfs_bmap_add_extent_hole_real(bma
->tp
, bma
->ip
,
4208 whichfork
, &bma
->icur
, &bma
->cur
, &bma
->got
,
4209 &bma
->logflags
, bma
->flags
);
4211 bma
->logflags
|= tmp_logflags
;
4216 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4217 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4218 * the neighbouring ones.
4220 xfs_iext_get_extent(ifp
, &bma
->icur
, &bma
->got
);
4222 ASSERT(bma
->got
.br_startoff
<= bma
->offset
);
4223 ASSERT(bma
->got
.br_startoff
+ bma
->got
.br_blockcount
>=
4224 bma
->offset
+ bma
->length
);
4225 ASSERT(bma
->got
.br_state
== XFS_EXT_NORM
||
4226 bma
->got
.br_state
== XFS_EXT_UNWRITTEN
);
4231 xfs_bmapi_convert_unwritten(
4232 struct xfs_bmalloca
*bma
,
4233 struct xfs_bmbt_irec
*mval
,
4237 int whichfork
= xfs_bmapi_whichfork(flags
);
4238 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
4239 int tmp_logflags
= 0;
4242 /* check if we need to do unwritten->real conversion */
4243 if (mval
->br_state
== XFS_EXT_UNWRITTEN
&&
4244 (flags
& XFS_BMAPI_PREALLOC
))
4247 /* check if we need to do real->unwritten conversion */
4248 if (mval
->br_state
== XFS_EXT_NORM
&&
4249 (flags
& (XFS_BMAPI_PREALLOC
| XFS_BMAPI_CONVERT
)) !=
4250 (XFS_BMAPI_PREALLOC
| XFS_BMAPI_CONVERT
))
4254 * Modify (by adding) the state flag, if writing.
4256 ASSERT(mval
->br_blockcount
<= len
);
4257 if ((ifp
->if_flags
& XFS_IFBROOT
) && !bma
->cur
) {
4258 bma
->cur
= xfs_bmbt_init_cursor(bma
->ip
->i_mount
, bma
->tp
,
4259 bma
->ip
, whichfork
);
4261 mval
->br_state
= (mval
->br_state
== XFS_EXT_UNWRITTEN
)
4262 ? XFS_EXT_NORM
: XFS_EXT_UNWRITTEN
;
4265 * Before insertion into the bmbt, zero the range being converted
4268 if (flags
& XFS_BMAPI_ZERO
) {
4269 error
= xfs_zero_extent(bma
->ip
, mval
->br_startblock
,
4270 mval
->br_blockcount
);
4275 error
= xfs_bmap_add_extent_unwritten_real(bma
->tp
, bma
->ip
, whichfork
,
4276 &bma
->icur
, &bma
->cur
, mval
, &tmp_logflags
);
4278 * Log the inode core unconditionally in the unwritten extent conversion
4279 * path because the conversion might not have done so (e.g., if the
4280 * extent count hasn't changed). We need to make sure the inode is dirty
4281 * in the transaction for the sake of fsync(), even if nothing has
4282 * changed, because fsync() will not force the log for this transaction
4283 * unless it sees the inode pinned.
4285 * Note: If we're only converting cow fork extents, there aren't
4286 * any on-disk updates to make, so we don't need to log anything.
4288 if (whichfork
!= XFS_COW_FORK
)
4289 bma
->logflags
|= tmp_logflags
| XFS_ILOG_CORE
;
4294 * Update our extent pointer, given that
4295 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4296 * of the neighbouring ones.
4298 xfs_iext_get_extent(ifp
, &bma
->icur
, &bma
->got
);
4301 * We may have combined previously unwritten space with written space,
4302 * so generate another request.
4304 if (mval
->br_blockcount
< len
)
4309 static inline xfs_extlen_t
4311 struct xfs_trans
*tp
,
4312 struct xfs_inode
*ip
,
4315 if (tp
&& tp
->t_firstblock
!= NULLFSBLOCK
)
4317 if (XFS_IFORK_FORMAT(ip
, fork
) != XFS_DINODE_FMT_BTREE
)
4319 return be16_to_cpu(XFS_IFORK_PTR(ip
, fork
)->if_broot
->bb_level
) + 1;
4323 * Log whatever the flags say, even if error. Otherwise we might miss detecting
4324 * a case where the data is changed, there's an error, and it's not logged so we
4325 * don't shutdown when we should. Don't bother logging extents/btree changes if
4326 * we converted to the other format.
4330 struct xfs_bmalloca
*bma
,
4334 if ((bma
->logflags
& xfs_ilog_fext(whichfork
)) &&
4335 XFS_IFORK_FORMAT(bma
->ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
4336 bma
->logflags
&= ~xfs_ilog_fext(whichfork
);
4337 else if ((bma
->logflags
& xfs_ilog_fbroot(whichfork
)) &&
4338 XFS_IFORK_FORMAT(bma
->ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)
4339 bma
->logflags
&= ~xfs_ilog_fbroot(whichfork
);
4342 xfs_trans_log_inode(bma
->tp
, bma
->ip
, bma
->logflags
);
4344 xfs_btree_del_cursor(bma
->cur
, error
);
4348 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4349 * extent state if necessary. Details behaviour is controlled by the flags
4350 * parameter. Only allocates blocks from a single allocation group, to avoid
4355 struct xfs_trans
*tp
, /* transaction pointer */
4356 struct xfs_inode
*ip
, /* incore inode */
4357 xfs_fileoff_t bno
, /* starting file offs. mapped */
4358 xfs_filblks_t len
, /* length to map in file */
4359 int flags
, /* XFS_BMAPI_... */
4360 xfs_extlen_t total
, /* total blocks needed */
4361 struct xfs_bmbt_irec
*mval
, /* output: map values */
4362 int *nmap
) /* i/o: mval size/count */
4364 struct xfs_bmalloca bma
= {
4369 struct xfs_mount
*mp
= ip
->i_mount
;
4370 struct xfs_ifork
*ifp
;
4371 xfs_fileoff_t end
; /* end of mapped file region */
4372 bool eof
= false; /* after the end of extents */
4373 int error
; /* error return */
4374 int n
; /* current extent index */
4375 xfs_fileoff_t obno
; /* old block number (offset) */
4376 int whichfork
; /* data or attr fork */
4379 xfs_fileoff_t orig_bno
; /* original block number value */
4380 int orig_flags
; /* original flags arg value */
4381 xfs_filblks_t orig_len
; /* original value of len arg */
4382 struct xfs_bmbt_irec
*orig_mval
; /* original value of mval */
4383 int orig_nmap
; /* original value of *nmap */
4391 whichfork
= xfs_bmapi_whichfork(flags
);
4394 ASSERT(*nmap
<= XFS_BMAP_MAX_NMAP
);
4397 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_LOCAL
);
4398 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
4399 ASSERT(!(flags
& XFS_BMAPI_REMAP
));
4401 /* zeroing is for currently only for data extents, not metadata */
4402 ASSERT((flags
& (XFS_BMAPI_METADATA
| XFS_BMAPI_ZERO
)) !=
4403 (XFS_BMAPI_METADATA
| XFS_BMAPI_ZERO
));
4405 * we can allocate unwritten extents or pre-zero allocated blocks,
4406 * but it makes no sense to do both at once. This would result in
4407 * zeroing the unwritten extent twice, but it still being an
4408 * unwritten extent....
4410 ASSERT((flags
& (XFS_BMAPI_PREALLOC
| XFS_BMAPI_ZERO
)) !=
4411 (XFS_BMAPI_PREALLOC
| XFS_BMAPI_ZERO
));
4413 if (XFS_IS_CORRUPT(mp
, !xfs_ifork_has_extents(ip
, whichfork
)) ||
4414 XFS_TEST_ERROR(false, mp
, XFS_ERRTAG_BMAPIFORMAT
)) {
4415 return -EFSCORRUPTED
;
4418 if (XFS_FORCED_SHUTDOWN(mp
))
4421 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4423 XFS_STATS_INC(mp
, xs_blk_mapw
);
4425 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
4426 error
= xfs_iread_extents(tp
, ip
, whichfork
);
4431 if (!xfs_iext_lookup_extent(ip
, ifp
, bno
, &bma
.icur
, &bma
.got
))
4433 if (!xfs_iext_peek_prev_extent(ifp
, &bma
.icur
, &bma
.prev
))
4434 bma
.prev
.br_startoff
= NULLFILEOFF
;
4435 bma
.minleft
= xfs_bmapi_minleft(tp
, ip
, whichfork
);
4440 while (bno
< end
&& n
< *nmap
) {
4441 bool need_alloc
= false, wasdelay
= false;
4443 /* in hole or beyond EOF? */
4444 if (eof
|| bma
.got
.br_startoff
> bno
) {
4446 * CoW fork conversions should /never/ hit EOF or
4447 * holes. There should always be something for us
4450 ASSERT(!((flags
& XFS_BMAPI_CONVERT
) &&
4451 (flags
& XFS_BMAPI_COWFORK
)));
4454 } else if (isnullstartblock(bma
.got
.br_startblock
)) {
4459 * First, deal with the hole before the allocated space
4460 * that we found, if any.
4462 if (need_alloc
|| wasdelay
) {
4464 bma
.conv
= !!(flags
& XFS_BMAPI_CONVERT
);
4465 bma
.wasdel
= wasdelay
;
4470 * There's a 32/64 bit type mismatch between the
4471 * allocation length request (which can be 64 bits in
4472 * length) and the bma length request, which is
4473 * xfs_extlen_t and therefore 32 bits. Hence we have to
4474 * check for 32-bit overflows and handle them here.
4476 if (len
> (xfs_filblks_t
)MAXEXTLEN
)
4477 bma
.length
= MAXEXTLEN
;
4482 ASSERT(bma
.length
> 0);
4483 error
= xfs_bmapi_allocate(&bma
);
4486 if (bma
.blkno
== NULLFSBLOCK
)
4490 * If this is a CoW allocation, record the data in
4491 * the refcount btree for orphan recovery.
4493 if (whichfork
== XFS_COW_FORK
)
4494 xfs_refcount_alloc_cow_extent(tp
, bma
.blkno
,
4498 /* Deal with the allocated space we found. */
4499 xfs_bmapi_trim_map(mval
, &bma
.got
, &bno
, len
, obno
,
4502 /* Execute unwritten extent conversion if necessary */
4503 error
= xfs_bmapi_convert_unwritten(&bma
, mval
, len
, flags
);
4504 if (error
== -EAGAIN
)
4509 /* update the extent map to return */
4510 xfs_bmapi_update_map(&mval
, &bno
, &len
, obno
, end
, &n
, flags
);
4513 * If we're done, stop now. Stop when we've allocated
4514 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4515 * the transaction may get too big.
4517 if (bno
>= end
|| n
>= *nmap
|| bma
.nallocs
>= *nmap
)
4520 /* Else go on to the next record. */
4522 if (!xfs_iext_next_extent(ifp
, &bma
.icur
, &bma
.got
))
4527 error
= xfs_bmap_btree_to_extents(tp
, ip
, bma
.cur
, &bma
.logflags
,
4532 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
||
4533 XFS_IFORK_NEXTENTS(ip
, whichfork
) >
4534 XFS_IFORK_MAXEXT(ip
, whichfork
));
4535 xfs_bmapi_finish(&bma
, whichfork
, 0);
4536 xfs_bmap_validate_ret(orig_bno
, orig_len
, orig_flags
, orig_mval
,
4540 xfs_bmapi_finish(&bma
, whichfork
, error
);
4545 * Convert an existing delalloc extent to real blocks based on file offset. This
4546 * attempts to allocate the entire delalloc extent and may require multiple
4547 * invocations to allocate the target offset if a large enough physical extent
4551 xfs_bmapi_convert_delalloc(
4552 struct xfs_inode
*ip
,
4555 struct iomap
*iomap
,
4558 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4559 struct xfs_mount
*mp
= ip
->i_mount
;
4560 xfs_fileoff_t offset_fsb
= XFS_B_TO_FSBT(mp
, offset
);
4561 struct xfs_bmalloca bma
= { NULL
};
4563 struct xfs_trans
*tp
;
4566 if (whichfork
== XFS_COW_FORK
)
4567 flags
|= IOMAP_F_SHARED
;
4570 * Space for the extent and indirect blocks was reserved when the
4571 * delalloc extent was created so there's no need to do so here.
4573 error
= xfs_trans_alloc(mp
, &M_RES(mp
)->tr_write
, 0, 0,
4574 XFS_TRANS_RESERVE
, &tp
);
4578 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
4579 xfs_trans_ijoin(tp
, ip
, 0);
4581 if (!xfs_iext_lookup_extent(ip
, ifp
, offset_fsb
, &bma
.icur
, &bma
.got
) ||
4582 bma
.got
.br_startoff
> offset_fsb
) {
4584 * No extent found in the range we are trying to convert. This
4585 * should only happen for the COW fork, where another thread
4586 * might have moved the extent to the data fork in the meantime.
4588 WARN_ON_ONCE(whichfork
!= XFS_COW_FORK
);
4590 goto out_trans_cancel
;
4594 * If we find a real extent here we raced with another thread converting
4595 * the extent. Just return the real extent at this offset.
4597 if (!isnullstartblock(bma
.got
.br_startblock
)) {
4598 xfs_bmbt_to_iomap(ip
, iomap
, &bma
.got
, flags
);
4599 *seq
= READ_ONCE(ifp
->if_seq
);
4600 goto out_trans_cancel
;
4606 bma
.offset
= bma
.got
.br_startoff
;
4607 bma
.length
= max_t(xfs_filblks_t
, bma
.got
.br_blockcount
, MAXEXTLEN
);
4608 bma
.minleft
= xfs_bmapi_minleft(tp
, ip
, whichfork
);
4609 if (whichfork
== XFS_COW_FORK
)
4610 bma
.flags
= XFS_BMAPI_COWFORK
| XFS_BMAPI_PREALLOC
;
4612 if (!xfs_iext_peek_prev_extent(ifp
, &bma
.icur
, &bma
.prev
))
4613 bma
.prev
.br_startoff
= NULLFILEOFF
;
4615 error
= xfs_bmapi_allocate(&bma
);
4620 if (WARN_ON_ONCE(bma
.blkno
== NULLFSBLOCK
))
4622 error
= -EFSCORRUPTED
;
4623 if (WARN_ON_ONCE(!xfs_valid_startblock(ip
, bma
.got
.br_startblock
)))
4626 XFS_STATS_ADD(mp
, xs_xstrat_bytes
, XFS_FSB_TO_B(mp
, bma
.length
));
4627 XFS_STATS_INC(mp
, xs_xstrat_quick
);
4629 ASSERT(!isnullstartblock(bma
.got
.br_startblock
));
4630 xfs_bmbt_to_iomap(ip
, iomap
, &bma
.got
, flags
);
4631 *seq
= READ_ONCE(ifp
->if_seq
);
4633 if (whichfork
== XFS_COW_FORK
)
4634 xfs_refcount_alloc_cow_extent(tp
, bma
.blkno
, bma
.length
);
4636 error
= xfs_bmap_btree_to_extents(tp
, ip
, bma
.cur
, &bma
.logflags
,
4641 xfs_bmapi_finish(&bma
, whichfork
, 0);
4642 error
= xfs_trans_commit(tp
);
4643 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
4647 xfs_bmapi_finish(&bma
, whichfork
, error
);
4649 xfs_trans_cancel(tp
);
4650 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
4656 struct xfs_trans
*tp
,
4657 struct xfs_inode
*ip
,
4660 xfs_fsblock_t startblock
,
4663 struct xfs_mount
*mp
= ip
->i_mount
;
4664 struct xfs_ifork
*ifp
;
4665 struct xfs_btree_cur
*cur
= NULL
;
4666 struct xfs_bmbt_irec got
;
4667 struct xfs_iext_cursor icur
;
4668 int whichfork
= xfs_bmapi_whichfork(flags
);
4669 int logflags
= 0, error
;
4671 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4673 ASSERT(len
<= (xfs_filblks_t
)MAXEXTLEN
);
4674 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
4675 ASSERT(!(flags
& ~(XFS_BMAPI_ATTRFORK
| XFS_BMAPI_PREALLOC
|
4676 XFS_BMAPI_NORMAP
)));
4677 ASSERT((flags
& (XFS_BMAPI_ATTRFORK
| XFS_BMAPI_PREALLOC
)) !=
4678 (XFS_BMAPI_ATTRFORK
| XFS_BMAPI_PREALLOC
));
4680 if (XFS_IS_CORRUPT(mp
, !xfs_ifork_has_extents(ip
, whichfork
)) ||
4681 XFS_TEST_ERROR(false, mp
, XFS_ERRTAG_BMAPIFORMAT
)) {
4682 return -EFSCORRUPTED
;
4685 if (XFS_FORCED_SHUTDOWN(mp
))
4688 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
4689 error
= xfs_iread_extents(tp
, ip
, whichfork
);
4694 if (xfs_iext_lookup_extent(ip
, ifp
, bno
, &icur
, &got
)) {
4695 /* make sure we only reflink into a hole. */
4696 ASSERT(got
.br_startoff
> bno
);
4697 ASSERT(got
.br_startoff
- bno
>= len
);
4700 ip
->i_d
.di_nblocks
+= len
;
4701 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
4703 if (ifp
->if_flags
& XFS_IFBROOT
) {
4704 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
4705 cur
->bc_private
.b
.flags
= 0;
4708 got
.br_startoff
= bno
;
4709 got
.br_startblock
= startblock
;
4710 got
.br_blockcount
= len
;
4711 if (flags
& XFS_BMAPI_PREALLOC
)
4712 got
.br_state
= XFS_EXT_UNWRITTEN
;
4714 got
.br_state
= XFS_EXT_NORM
;
4716 error
= xfs_bmap_add_extent_hole_real(tp
, ip
, whichfork
, &icur
,
4717 &cur
, &got
, &logflags
, flags
);
4721 error
= xfs_bmap_btree_to_extents(tp
, ip
, cur
, &logflags
, whichfork
);
4724 if (ip
->i_d
.di_format
!= XFS_DINODE_FMT_EXTENTS
)
4725 logflags
&= ~XFS_ILOG_DEXT
;
4726 else if (ip
->i_d
.di_format
!= XFS_DINODE_FMT_BTREE
)
4727 logflags
&= ~XFS_ILOG_DBROOT
;
4730 xfs_trans_log_inode(tp
, ip
, logflags
);
4732 xfs_btree_del_cursor(cur
, error
);
4737 * When a delalloc extent is split (e.g., due to a hole punch), the original
4738 * indlen reservation must be shared across the two new extents that are left
4741 * Given the original reservation and the worst case indlen for the two new
4742 * extents (as calculated by xfs_bmap_worst_indlen()), split the original
4743 * reservation fairly across the two new extents. If necessary, steal available
4744 * blocks from a deleted extent to make up a reservation deficiency (e.g., if
4745 * ores == 1). The number of stolen blocks is returned. The availability and
4746 * subsequent accounting of stolen blocks is the responsibility of the caller.
4748 static xfs_filblks_t
4749 xfs_bmap_split_indlen(
4750 xfs_filblks_t ores
, /* original res. */
4751 xfs_filblks_t
*indlen1
, /* ext1 worst indlen */
4752 xfs_filblks_t
*indlen2
, /* ext2 worst indlen */
4753 xfs_filblks_t avail
) /* stealable blocks */
4755 xfs_filblks_t len1
= *indlen1
;
4756 xfs_filblks_t len2
= *indlen2
;
4757 xfs_filblks_t nres
= len1
+ len2
; /* new total res. */
4758 xfs_filblks_t stolen
= 0;
4759 xfs_filblks_t resfactor
;
4762 * Steal as many blocks as we can to try and satisfy the worst case
4763 * indlen for both new extents.
4765 if (ores
< nres
&& avail
)
4766 stolen
= XFS_FILBLKS_MIN(nres
- ores
, avail
);
4769 /* nothing else to do if we've satisfied the new reservation */
4774 * We can't meet the total required reservation for the two extents.
4775 * Calculate the percent of the overall shortage between both extents
4776 * and apply this percentage to each of the requested indlen values.
4777 * This distributes the shortage fairly and reduces the chances that one
4778 * of the two extents is left with nothing when extents are repeatedly
4781 resfactor
= (ores
* 100);
4782 do_div(resfactor
, nres
);
4787 ASSERT(len1
+ len2
<= ores
);
4788 ASSERT(len1
< *indlen1
&& len2
< *indlen2
);
4791 * Hand out the remainder to each extent. If one of the two reservations
4792 * is zero, we want to make sure that one gets a block first. The loop
4793 * below starts with len1, so hand len2 a block right off the bat if it
4796 ores
-= (len1
+ len2
);
4797 ASSERT((*indlen1
- len1
) + (*indlen2
- len2
) >= ores
);
4798 if (ores
&& !len2
&& *indlen2
) {
4803 if (len1
< *indlen1
) {
4809 if (len2
< *indlen2
) {
4822 xfs_bmap_del_extent_delay(
4823 struct xfs_inode
*ip
,
4825 struct xfs_iext_cursor
*icur
,
4826 struct xfs_bmbt_irec
*got
,
4827 struct xfs_bmbt_irec
*del
)
4829 struct xfs_mount
*mp
= ip
->i_mount
;
4830 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4831 struct xfs_bmbt_irec
new;
4832 int64_t da_old
, da_new
, da_diff
= 0;
4833 xfs_fileoff_t del_endoff
, got_endoff
;
4834 xfs_filblks_t got_indlen
, new_indlen
, stolen
;
4835 int state
= xfs_bmap_fork_to_state(whichfork
);
4839 XFS_STATS_INC(mp
, xs_del_exlist
);
4841 isrt
= (whichfork
== XFS_DATA_FORK
) && XFS_IS_REALTIME_INODE(ip
);
4842 del_endoff
= del
->br_startoff
+ del
->br_blockcount
;
4843 got_endoff
= got
->br_startoff
+ got
->br_blockcount
;
4844 da_old
= startblockval(got
->br_startblock
);
4847 ASSERT(del
->br_blockcount
> 0);
4848 ASSERT(got
->br_startoff
<= del
->br_startoff
);
4849 ASSERT(got_endoff
>= del_endoff
);
4852 uint64_t rtexts
= XFS_FSB_TO_B(mp
, del
->br_blockcount
);
4854 do_div(rtexts
, mp
->m_sb
.sb_rextsize
);
4855 xfs_mod_frextents(mp
, rtexts
);
4859 * Update the inode delalloc counter now and wait to update the
4860 * sb counters as we might have to borrow some blocks for the
4861 * indirect block accounting.
4863 error
= xfs_trans_reserve_quota_nblks(NULL
, ip
,
4864 -((long)del
->br_blockcount
), 0,
4865 isrt
? XFS_QMOPT_RES_RTBLKS
: XFS_QMOPT_RES_REGBLKS
);
4868 ip
->i_delayed_blks
-= del
->br_blockcount
;
4870 if (got
->br_startoff
== del
->br_startoff
)
4871 state
|= BMAP_LEFT_FILLING
;
4872 if (got_endoff
== del_endoff
)
4873 state
|= BMAP_RIGHT_FILLING
;
4875 switch (state
& (BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
)) {
4876 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
4878 * Matches the whole extent. Delete the entry.
4880 xfs_iext_remove(ip
, icur
, state
);
4881 xfs_iext_prev(ifp
, icur
);
4883 case BMAP_LEFT_FILLING
:
4885 * Deleting the first part of the extent.
4887 got
->br_startoff
= del_endoff
;
4888 got
->br_blockcount
-= del
->br_blockcount
;
4889 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
,
4890 got
->br_blockcount
), da_old
);
4891 got
->br_startblock
= nullstartblock((int)da_new
);
4892 xfs_iext_update_extent(ip
, state
, icur
, got
);
4894 case BMAP_RIGHT_FILLING
:
4896 * Deleting the last part of the extent.
4898 got
->br_blockcount
= got
->br_blockcount
- del
->br_blockcount
;
4899 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
,
4900 got
->br_blockcount
), da_old
);
4901 got
->br_startblock
= nullstartblock((int)da_new
);
4902 xfs_iext_update_extent(ip
, state
, icur
, got
);
4906 * Deleting the middle of the extent.
4908 * Distribute the original indlen reservation across the two new
4909 * extents. Steal blocks from the deleted extent if necessary.
4910 * Stealing blocks simply fudges the fdblocks accounting below.
4911 * Warn if either of the new indlen reservations is zero as this
4912 * can lead to delalloc problems.
4914 got
->br_blockcount
= del
->br_startoff
- got
->br_startoff
;
4915 got_indlen
= xfs_bmap_worst_indlen(ip
, got
->br_blockcount
);
4917 new.br_blockcount
= got_endoff
- del_endoff
;
4918 new_indlen
= xfs_bmap_worst_indlen(ip
, new.br_blockcount
);
4920 WARN_ON_ONCE(!got_indlen
|| !new_indlen
);
4921 stolen
= xfs_bmap_split_indlen(da_old
, &got_indlen
, &new_indlen
,
4922 del
->br_blockcount
);
4924 got
->br_startblock
= nullstartblock((int)got_indlen
);
4926 new.br_startoff
= del_endoff
;
4927 new.br_state
= got
->br_state
;
4928 new.br_startblock
= nullstartblock((int)new_indlen
);
4930 xfs_iext_update_extent(ip
, state
, icur
, got
);
4931 xfs_iext_next(ifp
, icur
);
4932 xfs_iext_insert(ip
, icur
, &new, state
);
4934 da_new
= got_indlen
+ new_indlen
- stolen
;
4935 del
->br_blockcount
-= stolen
;
4939 ASSERT(da_old
>= da_new
);
4940 da_diff
= da_old
- da_new
;
4942 da_diff
+= del
->br_blockcount
;
4944 xfs_mod_fdblocks(mp
, da_diff
, false);
4945 xfs_mod_delalloc(mp
, -da_diff
);
4951 xfs_bmap_del_extent_cow(
4952 struct xfs_inode
*ip
,
4953 struct xfs_iext_cursor
*icur
,
4954 struct xfs_bmbt_irec
*got
,
4955 struct xfs_bmbt_irec
*del
)
4957 struct xfs_mount
*mp
= ip
->i_mount
;
4958 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, XFS_COW_FORK
);
4959 struct xfs_bmbt_irec
new;
4960 xfs_fileoff_t del_endoff
, got_endoff
;
4961 int state
= BMAP_COWFORK
;
4963 XFS_STATS_INC(mp
, xs_del_exlist
);
4965 del_endoff
= del
->br_startoff
+ del
->br_blockcount
;
4966 got_endoff
= got
->br_startoff
+ got
->br_blockcount
;
4968 ASSERT(del
->br_blockcount
> 0);
4969 ASSERT(got
->br_startoff
<= del
->br_startoff
);
4970 ASSERT(got_endoff
>= del_endoff
);
4971 ASSERT(!isnullstartblock(got
->br_startblock
));
4973 if (got
->br_startoff
== del
->br_startoff
)
4974 state
|= BMAP_LEFT_FILLING
;
4975 if (got_endoff
== del_endoff
)
4976 state
|= BMAP_RIGHT_FILLING
;
4978 switch (state
& (BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
)) {
4979 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
4981 * Matches the whole extent. Delete the entry.
4983 xfs_iext_remove(ip
, icur
, state
);
4984 xfs_iext_prev(ifp
, icur
);
4986 case BMAP_LEFT_FILLING
:
4988 * Deleting the first part of the extent.
4990 got
->br_startoff
= del_endoff
;
4991 got
->br_blockcount
-= del
->br_blockcount
;
4992 got
->br_startblock
= del
->br_startblock
+ del
->br_blockcount
;
4993 xfs_iext_update_extent(ip
, state
, icur
, got
);
4995 case BMAP_RIGHT_FILLING
:
4997 * Deleting the last part of the extent.
4999 got
->br_blockcount
-= del
->br_blockcount
;
5000 xfs_iext_update_extent(ip
, state
, icur
, got
);
5004 * Deleting the middle of the extent.
5006 got
->br_blockcount
= del
->br_startoff
- got
->br_startoff
;
5008 new.br_startoff
= del_endoff
;
5009 new.br_blockcount
= got_endoff
- del_endoff
;
5010 new.br_state
= got
->br_state
;
5011 new.br_startblock
= del
->br_startblock
+ del
->br_blockcount
;
5013 xfs_iext_update_extent(ip
, state
, icur
, got
);
5014 xfs_iext_next(ifp
, icur
);
5015 xfs_iext_insert(ip
, icur
, &new, state
);
5018 ip
->i_delayed_blks
-= del
->br_blockcount
;
5022 * Called by xfs_bmapi to update file extent records and the btree
5023 * after removing space.
5025 STATIC
int /* error */
5026 xfs_bmap_del_extent_real(
5027 xfs_inode_t
*ip
, /* incore inode pointer */
5028 xfs_trans_t
*tp
, /* current transaction pointer */
5029 struct xfs_iext_cursor
*icur
,
5030 xfs_btree_cur_t
*cur
, /* if null, not a btree */
5031 xfs_bmbt_irec_t
*del
, /* data to remove from extents */
5032 int *logflagsp
, /* inode logging flags */
5033 int whichfork
, /* data or attr fork */
5034 int bflags
) /* bmapi flags */
5036 xfs_fsblock_t del_endblock
=0; /* first block past del */
5037 xfs_fileoff_t del_endoff
; /* first offset past del */
5038 int do_fx
; /* free extent at end of routine */
5039 int error
; /* error return value */
5040 int flags
= 0;/* inode logging flags */
5041 struct xfs_bmbt_irec got
; /* current extent entry */
5042 xfs_fileoff_t got_endoff
; /* first offset past got */
5043 int i
; /* temp state */
5044 struct xfs_ifork
*ifp
; /* inode fork pointer */
5045 xfs_mount_t
*mp
; /* mount structure */
5046 xfs_filblks_t nblks
; /* quota/sb block count */
5047 xfs_bmbt_irec_t
new; /* new record to be inserted */
5049 uint qfield
; /* quota field to update */
5050 int state
= xfs_bmap_fork_to_state(whichfork
);
5051 struct xfs_bmbt_irec old
;
5054 XFS_STATS_INC(mp
, xs_del_exlist
);
5056 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5057 ASSERT(del
->br_blockcount
> 0);
5058 xfs_iext_get_extent(ifp
, icur
, &got
);
5059 ASSERT(got
.br_startoff
<= del
->br_startoff
);
5060 del_endoff
= del
->br_startoff
+ del
->br_blockcount
;
5061 got_endoff
= got
.br_startoff
+ got
.br_blockcount
;
5062 ASSERT(got_endoff
>= del_endoff
);
5063 ASSERT(!isnullstartblock(got
.br_startblock
));
5068 * If it's the case where the directory code is running with no block
5069 * reservation, and the deleted block is in the middle of its extent,
5070 * and the resulting insert of an extent would cause transformation to
5071 * btree format, then reject it. The calling code will then swap blocks
5072 * around instead. We have to do this now, rather than waiting for the
5073 * conversion to btree format, since the transaction will be dirty then.
5075 if (tp
->t_blk_res
== 0 &&
5076 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
&&
5077 XFS_IFORK_NEXTENTS(ip
, whichfork
) >=
5078 XFS_IFORK_MAXEXT(ip
, whichfork
) &&
5079 del
->br_startoff
> got
.br_startoff
&& del_endoff
< got_endoff
)
5082 flags
= XFS_ILOG_CORE
;
5083 if (whichfork
== XFS_DATA_FORK
&& XFS_IS_REALTIME_INODE(ip
)) {
5088 bno
= div_u64_rem(del
->br_startblock
, mp
->m_sb
.sb_rextsize
,
5091 len
= div_u64_rem(del
->br_blockcount
, mp
->m_sb
.sb_rextsize
,
5095 error
= xfs_rtfree_extent(tp
, bno
, (xfs_extlen_t
)len
);
5099 nblks
= len
* mp
->m_sb
.sb_rextsize
;
5100 qfield
= XFS_TRANS_DQ_RTBCOUNT
;
5103 nblks
= del
->br_blockcount
;
5104 qfield
= XFS_TRANS_DQ_BCOUNT
;
5107 del_endblock
= del
->br_startblock
+ del
->br_blockcount
;
5109 error
= xfs_bmbt_lookup_eq(cur
, &got
, &i
);
5112 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
5113 error
= -EFSCORRUPTED
;
5118 if (got
.br_startoff
== del
->br_startoff
)
5119 state
|= BMAP_LEFT_FILLING
;
5120 if (got_endoff
== del_endoff
)
5121 state
|= BMAP_RIGHT_FILLING
;
5123 switch (state
& (BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
)) {
5124 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
5126 * Matches the whole extent. Delete the entry.
5128 xfs_iext_remove(ip
, icur
, state
);
5129 xfs_iext_prev(ifp
, icur
);
5130 XFS_IFORK_NEXT_SET(ip
, whichfork
,
5131 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
5132 flags
|= XFS_ILOG_CORE
;
5134 flags
|= xfs_ilog_fext(whichfork
);
5137 if ((error
= xfs_btree_delete(cur
, &i
)))
5139 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
5140 error
= -EFSCORRUPTED
;
5144 case BMAP_LEFT_FILLING
:
5146 * Deleting the first part of the extent.
5148 got
.br_startoff
= del_endoff
;
5149 got
.br_startblock
= del_endblock
;
5150 got
.br_blockcount
-= del
->br_blockcount
;
5151 xfs_iext_update_extent(ip
, state
, icur
, &got
);
5153 flags
|= xfs_ilog_fext(whichfork
);
5156 error
= xfs_bmbt_update(cur
, &got
);
5160 case BMAP_RIGHT_FILLING
:
5162 * Deleting the last part of the extent.
5164 got
.br_blockcount
-= del
->br_blockcount
;
5165 xfs_iext_update_extent(ip
, state
, icur
, &got
);
5167 flags
|= xfs_ilog_fext(whichfork
);
5170 error
= xfs_bmbt_update(cur
, &got
);
5176 * Deleting the middle of the extent.
5180 got
.br_blockcount
= del
->br_startoff
- got
.br_startoff
;
5181 xfs_iext_update_extent(ip
, state
, icur
, &got
);
5183 new.br_startoff
= del_endoff
;
5184 new.br_blockcount
= got_endoff
- del_endoff
;
5185 new.br_state
= got
.br_state
;
5186 new.br_startblock
= del_endblock
;
5188 flags
|= XFS_ILOG_CORE
;
5190 error
= xfs_bmbt_update(cur
, &got
);
5193 error
= xfs_btree_increment(cur
, 0, &i
);
5196 cur
->bc_rec
.b
= new;
5197 error
= xfs_btree_insert(cur
, &i
);
5198 if (error
&& error
!= -ENOSPC
)
5201 * If get no-space back from btree insert, it tried a
5202 * split, and we have a zero block reservation. Fix up
5203 * our state and return the error.
5205 if (error
== -ENOSPC
) {
5207 * Reset the cursor, don't trust it after any
5210 error
= xfs_bmbt_lookup_eq(cur
, &got
, &i
);
5213 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
5214 error
= -EFSCORRUPTED
;
5218 * Update the btree record back
5219 * to the original value.
5221 error
= xfs_bmbt_update(cur
, &old
);
5225 * Reset the extent record back
5226 * to the original value.
5228 xfs_iext_update_extent(ip
, state
, icur
, &old
);
5233 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
5234 error
= -EFSCORRUPTED
;
5238 flags
|= xfs_ilog_fext(whichfork
);
5239 XFS_IFORK_NEXT_SET(ip
, whichfork
,
5240 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
5241 xfs_iext_next(ifp
, icur
);
5242 xfs_iext_insert(ip
, icur
, &new, state
);
5246 /* remove reverse mapping */
5247 xfs_rmap_unmap_extent(tp
, ip
, whichfork
, del
);
5250 * If we need to, add to list of extents to delete.
5252 if (do_fx
&& !(bflags
& XFS_BMAPI_REMAP
)) {
5253 if (xfs_is_reflink_inode(ip
) && whichfork
== XFS_DATA_FORK
) {
5254 xfs_refcount_decrease_extent(tp
, del
);
5256 __xfs_bmap_add_free(tp
, del
->br_startblock
,
5257 del
->br_blockcount
, NULL
,
5258 (bflags
& XFS_BMAPI_NODISCARD
) ||
5259 del
->br_state
== XFS_EXT_UNWRITTEN
);
5264 * Adjust inode # blocks in the file.
5267 ip
->i_d
.di_nblocks
-= nblks
;
5269 * Adjust quota data.
5271 if (qfield
&& !(bflags
& XFS_BMAPI_REMAP
))
5272 xfs_trans_mod_dquot_byino(tp
, ip
, qfield
, (long)-nblks
);
5280 * Unmap (remove) blocks from a file.
5281 * If nexts is nonzero then the number of extents to remove is limited to
5282 * that value. If not all extents in the block range can be removed then
5287 struct xfs_trans
*tp
, /* transaction pointer */
5288 struct xfs_inode
*ip
, /* incore inode */
5289 xfs_fileoff_t start
, /* first file offset deleted */
5290 xfs_filblks_t
*rlen
, /* i/o: amount remaining */
5291 int flags
, /* misc flags */
5292 xfs_extnum_t nexts
) /* number of extents max */
5294 struct xfs_btree_cur
*cur
; /* bmap btree cursor */
5295 struct xfs_bmbt_irec del
; /* extent being deleted */
5296 int error
; /* error return value */
5297 xfs_extnum_t extno
; /* extent number in list */
5298 struct xfs_bmbt_irec got
; /* current extent record */
5299 struct xfs_ifork
*ifp
; /* inode fork pointer */
5300 int isrt
; /* freeing in rt area */
5301 int logflags
; /* transaction logging flags */
5302 xfs_extlen_t mod
; /* rt extent offset */
5303 struct xfs_mount
*mp
= ip
->i_mount
;
5304 int tmp_logflags
; /* partial logging flags */
5305 int wasdel
; /* was a delayed alloc extent */
5306 int whichfork
; /* data or attribute fork */
5308 xfs_filblks_t len
= *rlen
; /* length to unmap in file */
5309 xfs_fileoff_t max_len
;
5310 xfs_agnumber_t prev_agno
= NULLAGNUMBER
, agno
;
5312 struct xfs_iext_cursor icur
;
5315 trace_xfs_bunmap(ip
, start
, len
, flags
, _RET_IP_
);
5317 whichfork
= xfs_bmapi_whichfork(flags
);
5318 ASSERT(whichfork
!= XFS_COW_FORK
);
5319 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5320 if (XFS_IS_CORRUPT(mp
, !xfs_ifork_has_extents(ip
, whichfork
)))
5321 return -EFSCORRUPTED
;
5322 if (XFS_FORCED_SHUTDOWN(mp
))
5325 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
5330 * Guesstimate how many blocks we can unmap without running the risk of
5331 * blowing out the transaction with a mix of EFIs and reflink
5334 if (tp
&& xfs_is_reflink_inode(ip
) && whichfork
== XFS_DATA_FORK
)
5335 max_len
= min(len
, xfs_refcount_max_unmap(tp
->t_log_res
));
5339 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
5340 (error
= xfs_iread_extents(tp
, ip
, whichfork
)))
5342 if (xfs_iext_count(ifp
) == 0) {
5346 XFS_STATS_INC(mp
, xs_blk_unmap
);
5347 isrt
= (whichfork
== XFS_DATA_FORK
) && XFS_IS_REALTIME_INODE(ip
);
5350 if (!xfs_iext_lookup_extent_before(ip
, ifp
, &end
, &icur
, &got
)) {
5357 if (ifp
->if_flags
& XFS_IFBROOT
) {
5358 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
);
5359 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
5360 cur
->bc_private
.b
.flags
= 0;
5366 * Synchronize by locking the bitmap inode.
5368 xfs_ilock(mp
->m_rbmip
, XFS_ILOCK_EXCL
|XFS_ILOCK_RTBITMAP
);
5369 xfs_trans_ijoin(tp
, mp
->m_rbmip
, XFS_ILOCK_EXCL
);
5370 xfs_ilock(mp
->m_rsumip
, XFS_ILOCK_EXCL
|XFS_ILOCK_RTSUM
);
5371 xfs_trans_ijoin(tp
, mp
->m_rsumip
, XFS_ILOCK_EXCL
);
5375 while (end
!= (xfs_fileoff_t
)-1 && end
>= start
&&
5376 (nexts
== 0 || extno
< nexts
) && max_len
> 0) {
5378 * Is the found extent after a hole in which end lives?
5379 * Just back up to the previous extent, if so.
5381 if (got
.br_startoff
> end
&&
5382 !xfs_iext_prev_extent(ifp
, &icur
, &got
)) {
5387 * Is the last block of this extent before the range
5388 * we're supposed to delete? If so, we're done.
5390 end
= XFS_FILEOFF_MIN(end
,
5391 got
.br_startoff
+ got
.br_blockcount
- 1);
5395 * Then deal with the (possibly delayed) allocated space
5399 wasdel
= isnullstartblock(del
.br_startblock
);
5402 * Make sure we don't touch multiple AGF headers out of order
5403 * in a single transaction, as that could cause AB-BA deadlocks.
5405 if (!wasdel
&& !isrt
) {
5406 agno
= XFS_FSB_TO_AGNO(mp
, del
.br_startblock
);
5407 if (prev_agno
!= NULLAGNUMBER
&& prev_agno
> agno
)
5411 if (got
.br_startoff
< start
) {
5412 del
.br_startoff
= start
;
5413 del
.br_blockcount
-= start
- got
.br_startoff
;
5415 del
.br_startblock
+= start
- got
.br_startoff
;
5417 if (del
.br_startoff
+ del
.br_blockcount
> end
+ 1)
5418 del
.br_blockcount
= end
+ 1 - del
.br_startoff
;
5420 /* How much can we safely unmap? */
5421 if (max_len
< del
.br_blockcount
) {
5422 del
.br_startoff
+= del
.br_blockcount
- max_len
;
5424 del
.br_startblock
+= del
.br_blockcount
- max_len
;
5425 del
.br_blockcount
= max_len
;
5431 sum
= del
.br_startblock
+ del
.br_blockcount
;
5432 div_u64_rem(sum
, mp
->m_sb
.sb_rextsize
, &mod
);
5435 * Realtime extent not lined up at the end.
5436 * The extent could have been split into written
5437 * and unwritten pieces, or we could just be
5438 * unmapping part of it. But we can't really
5439 * get rid of part of a realtime extent.
5441 if (del
.br_state
== XFS_EXT_UNWRITTEN
) {
5443 * This piece is unwritten, or we're not
5444 * using unwritten extents. Skip over it.
5447 end
-= mod
> del
.br_blockcount
?
5448 del
.br_blockcount
: mod
;
5449 if (end
< got
.br_startoff
&&
5450 !xfs_iext_prev_extent(ifp
, &icur
, &got
)) {
5457 * It's written, turn it unwritten.
5458 * This is better than zeroing it.
5460 ASSERT(del
.br_state
== XFS_EXT_NORM
);
5461 ASSERT(tp
->t_blk_res
> 0);
5463 * If this spans a realtime extent boundary,
5464 * chop it back to the start of the one we end at.
5466 if (del
.br_blockcount
> mod
) {
5467 del
.br_startoff
+= del
.br_blockcount
- mod
;
5468 del
.br_startblock
+= del
.br_blockcount
- mod
;
5469 del
.br_blockcount
= mod
;
5471 del
.br_state
= XFS_EXT_UNWRITTEN
;
5472 error
= xfs_bmap_add_extent_unwritten_real(tp
, ip
,
5473 whichfork
, &icur
, &cur
, &del
,
5479 div_u64_rem(del
.br_startblock
, mp
->m_sb
.sb_rextsize
, &mod
);
5481 xfs_extlen_t off
= mp
->m_sb
.sb_rextsize
- mod
;
5484 * Realtime extent is lined up at the end but not
5485 * at the front. We'll get rid of full extents if
5488 if (del
.br_blockcount
> off
) {
5489 del
.br_blockcount
-= off
;
5490 del
.br_startoff
+= off
;
5491 del
.br_startblock
+= off
;
5492 } else if (del
.br_startoff
== start
&&
5493 (del
.br_state
== XFS_EXT_UNWRITTEN
||
5494 tp
->t_blk_res
== 0)) {
5496 * Can't make it unwritten. There isn't
5497 * a full extent here so just skip it.
5499 ASSERT(end
>= del
.br_blockcount
);
5500 end
-= del
.br_blockcount
;
5501 if (got
.br_startoff
> end
&&
5502 !xfs_iext_prev_extent(ifp
, &icur
, &got
)) {
5507 } else if (del
.br_state
== XFS_EXT_UNWRITTEN
) {
5508 struct xfs_bmbt_irec prev
;
5509 xfs_fileoff_t unwrite_start
;
5512 * This one is already unwritten.
5513 * It must have a written left neighbor.
5514 * Unwrite the killed part of that one and
5517 if (!xfs_iext_prev_extent(ifp
, &icur
, &prev
))
5519 ASSERT(prev
.br_state
== XFS_EXT_NORM
);
5520 ASSERT(!isnullstartblock(prev
.br_startblock
));
5521 ASSERT(del
.br_startblock
==
5522 prev
.br_startblock
+ prev
.br_blockcount
);
5523 unwrite_start
= max3(start
,
5524 del
.br_startoff
- mod
,
5526 mod
= unwrite_start
- prev
.br_startoff
;
5527 prev
.br_startoff
= unwrite_start
;
5528 prev
.br_startblock
+= mod
;
5529 prev
.br_blockcount
-= mod
;
5530 prev
.br_state
= XFS_EXT_UNWRITTEN
;
5531 error
= xfs_bmap_add_extent_unwritten_real(tp
,
5532 ip
, whichfork
, &icur
, &cur
,
5538 ASSERT(del
.br_state
== XFS_EXT_NORM
);
5539 del
.br_state
= XFS_EXT_UNWRITTEN
;
5540 error
= xfs_bmap_add_extent_unwritten_real(tp
,
5541 ip
, whichfork
, &icur
, &cur
,
5551 error
= xfs_bmap_del_extent_delay(ip
, whichfork
, &icur
,
5554 error
= xfs_bmap_del_extent_real(ip
, tp
, &icur
, cur
,
5555 &del
, &tmp_logflags
, whichfork
,
5557 logflags
|= tmp_logflags
;
5563 max_len
-= del
.br_blockcount
;
5564 end
= del
.br_startoff
- 1;
5567 * If not done go on to the next (previous) record.
5569 if (end
!= (xfs_fileoff_t
)-1 && end
>= start
) {
5570 if (!xfs_iext_get_extent(ifp
, &icur
, &got
) ||
5571 (got
.br_startoff
> end
&&
5572 !xfs_iext_prev_extent(ifp
, &icur
, &got
))) {
5579 if (done
|| end
== (xfs_fileoff_t
)-1 || end
< start
)
5582 *rlen
= end
- start
+ 1;
5585 * Convert to a btree if necessary.
5587 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
5588 ASSERT(cur
== NULL
);
5589 error
= xfs_bmap_extents_to_btree(tp
, ip
, &cur
, 0,
5590 &tmp_logflags
, whichfork
);
5591 logflags
|= tmp_logflags
;
5593 error
= xfs_bmap_btree_to_extents(tp
, ip
, cur
, &logflags
,
5599 * Log everything. Do this after conversion, there's no point in
5600 * logging the extent records if we've converted to btree format.
5602 if ((logflags
& xfs_ilog_fext(whichfork
)) &&
5603 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
5604 logflags
&= ~xfs_ilog_fext(whichfork
);
5605 else if ((logflags
& xfs_ilog_fbroot(whichfork
)) &&
5606 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)
5607 logflags
&= ~xfs_ilog_fbroot(whichfork
);
5609 * Log inode even in the error case, if the transaction
5610 * is dirty we'll need to shut down the filesystem.
5613 xfs_trans_log_inode(tp
, ip
, logflags
);
5616 cur
->bc_private
.b
.allocated
= 0;
5617 xfs_btree_del_cursor(cur
, error
);
5622 /* Unmap a range of a file. */
5626 struct xfs_inode
*ip
,
5635 error
= __xfs_bunmapi(tp
, ip
, bno
, &len
, flags
, nexts
);
5641 * Determine whether an extent shift can be accomplished by a merge with the
5642 * extent that precedes the target hole of the shift.
5646 struct xfs_bmbt_irec
*left
, /* preceding extent */
5647 struct xfs_bmbt_irec
*got
, /* current extent to shift */
5648 xfs_fileoff_t shift
) /* shift fsb */
5650 xfs_fileoff_t startoff
;
5652 startoff
= got
->br_startoff
- shift
;
5655 * The extent, once shifted, must be adjacent in-file and on-disk with
5656 * the preceding extent.
5658 if ((left
->br_startoff
+ left
->br_blockcount
!= startoff
) ||
5659 (left
->br_startblock
+ left
->br_blockcount
!= got
->br_startblock
) ||
5660 (left
->br_state
!= got
->br_state
) ||
5661 (left
->br_blockcount
+ got
->br_blockcount
> MAXEXTLEN
))
5668 * A bmap extent shift adjusts the file offset of an extent to fill a preceding
5669 * hole in the file. If an extent shift would result in the extent being fully
5670 * adjacent to the extent that currently precedes the hole, we can merge with
5671 * the preceding extent rather than do the shift.
5673 * This function assumes the caller has verified a shift-by-merge is possible
5674 * with the provided extents via xfs_bmse_can_merge().
5678 struct xfs_trans
*tp
,
5679 struct xfs_inode
*ip
,
5681 xfs_fileoff_t shift
, /* shift fsb */
5682 struct xfs_iext_cursor
*icur
,
5683 struct xfs_bmbt_irec
*got
, /* extent to shift */
5684 struct xfs_bmbt_irec
*left
, /* preceding extent */
5685 struct xfs_btree_cur
*cur
,
5686 int *logflags
) /* output */
5688 struct xfs_bmbt_irec
new;
5689 xfs_filblks_t blockcount
;
5691 struct xfs_mount
*mp
= ip
->i_mount
;
5693 blockcount
= left
->br_blockcount
+ got
->br_blockcount
;
5695 ASSERT(xfs_isilocked(ip
, XFS_IOLOCK_EXCL
));
5696 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
5697 ASSERT(xfs_bmse_can_merge(left
, got
, shift
));
5700 new.br_blockcount
= blockcount
;
5703 * Update the on-disk extent count, the btree if necessary and log the
5706 XFS_IFORK_NEXT_SET(ip
, whichfork
,
5707 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
5708 *logflags
|= XFS_ILOG_CORE
;
5710 *logflags
|= XFS_ILOG_DEXT
;
5714 /* lookup and remove the extent to merge */
5715 error
= xfs_bmbt_lookup_eq(cur
, got
, &i
);
5718 if (XFS_IS_CORRUPT(mp
, i
!= 1))
5719 return -EFSCORRUPTED
;
5721 error
= xfs_btree_delete(cur
, &i
);
5724 if (XFS_IS_CORRUPT(mp
, i
!= 1))
5725 return -EFSCORRUPTED
;
5727 /* lookup and update size of the previous extent */
5728 error
= xfs_bmbt_lookup_eq(cur
, left
, &i
);
5731 if (XFS_IS_CORRUPT(mp
, i
!= 1))
5732 return -EFSCORRUPTED
;
5734 error
= xfs_bmbt_update(cur
, &new);
5738 /* change to extent format if required after extent removal */
5739 error
= xfs_bmap_btree_to_extents(tp
, ip
, cur
, logflags
, whichfork
);
5744 xfs_iext_remove(ip
, icur
, 0);
5745 xfs_iext_prev(XFS_IFORK_PTR(ip
, whichfork
), icur
);
5746 xfs_iext_update_extent(ip
, xfs_bmap_fork_to_state(whichfork
), icur
,
5749 /* update reverse mapping. rmap functions merge the rmaps for us */
5750 xfs_rmap_unmap_extent(tp
, ip
, whichfork
, got
);
5751 memcpy(&new, got
, sizeof(new));
5752 new.br_startoff
= left
->br_startoff
+ left
->br_blockcount
;
5753 xfs_rmap_map_extent(tp
, ip
, whichfork
, &new);
5758 xfs_bmap_shift_update_extent(
5759 struct xfs_trans
*tp
,
5760 struct xfs_inode
*ip
,
5762 struct xfs_iext_cursor
*icur
,
5763 struct xfs_bmbt_irec
*got
,
5764 struct xfs_btree_cur
*cur
,
5766 xfs_fileoff_t startoff
)
5768 struct xfs_mount
*mp
= ip
->i_mount
;
5769 struct xfs_bmbt_irec prev
= *got
;
5772 *logflags
|= XFS_ILOG_CORE
;
5774 got
->br_startoff
= startoff
;
5777 error
= xfs_bmbt_lookup_eq(cur
, &prev
, &i
);
5780 if (XFS_IS_CORRUPT(mp
, i
!= 1))
5781 return -EFSCORRUPTED
;
5783 error
= xfs_bmbt_update(cur
, got
);
5787 *logflags
|= XFS_ILOG_DEXT
;
5790 xfs_iext_update_extent(ip
, xfs_bmap_fork_to_state(whichfork
), icur
,
5793 /* update reverse mapping */
5794 xfs_rmap_unmap_extent(tp
, ip
, whichfork
, &prev
);
5795 xfs_rmap_map_extent(tp
, ip
, whichfork
, got
);
5800 xfs_bmap_collapse_extents(
5801 struct xfs_trans
*tp
,
5802 struct xfs_inode
*ip
,
5803 xfs_fileoff_t
*next_fsb
,
5804 xfs_fileoff_t offset_shift_fsb
,
5807 int whichfork
= XFS_DATA_FORK
;
5808 struct xfs_mount
*mp
= ip
->i_mount
;
5809 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5810 struct xfs_btree_cur
*cur
= NULL
;
5811 struct xfs_bmbt_irec got
, prev
;
5812 struct xfs_iext_cursor icur
;
5813 xfs_fileoff_t new_startoff
;
5817 if (XFS_IS_CORRUPT(mp
, !xfs_ifork_has_extents(ip
, whichfork
)) ||
5818 XFS_TEST_ERROR(false, mp
, XFS_ERRTAG_BMAPIFORMAT
)) {
5819 return -EFSCORRUPTED
;
5822 if (XFS_FORCED_SHUTDOWN(mp
))
5825 ASSERT(xfs_isilocked(ip
, XFS_IOLOCK_EXCL
| XFS_ILOCK_EXCL
));
5827 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
5828 error
= xfs_iread_extents(tp
, ip
, whichfork
);
5833 if (ifp
->if_flags
& XFS_IFBROOT
) {
5834 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
5835 cur
->bc_private
.b
.flags
= 0;
5838 if (!xfs_iext_lookup_extent(ip
, ifp
, *next_fsb
, &icur
, &got
)) {
5842 if (XFS_IS_CORRUPT(mp
, isnullstartblock(got
.br_startblock
))) {
5843 error
= -EFSCORRUPTED
;
5847 new_startoff
= got
.br_startoff
- offset_shift_fsb
;
5848 if (xfs_iext_peek_prev_extent(ifp
, &icur
, &prev
)) {
5849 if (new_startoff
< prev
.br_startoff
+ prev
.br_blockcount
) {
5854 if (xfs_bmse_can_merge(&prev
, &got
, offset_shift_fsb
)) {
5855 error
= xfs_bmse_merge(tp
, ip
, whichfork
,
5856 offset_shift_fsb
, &icur
, &got
, &prev
,
5863 if (got
.br_startoff
< offset_shift_fsb
) {
5869 error
= xfs_bmap_shift_update_extent(tp
, ip
, whichfork
, &icur
, &got
,
5870 cur
, &logflags
, new_startoff
);
5875 if (!xfs_iext_next_extent(ifp
, &icur
, &got
)) {
5880 *next_fsb
= got
.br_startoff
;
5883 xfs_btree_del_cursor(cur
, error
);
5885 xfs_trans_log_inode(tp
, ip
, logflags
);
5889 /* Make sure we won't be right-shifting an extent past the maximum bound. */
5891 xfs_bmap_can_insert_extents(
5892 struct xfs_inode
*ip
,
5894 xfs_fileoff_t shift
)
5896 struct xfs_bmbt_irec got
;
5900 ASSERT(xfs_isilocked(ip
, XFS_IOLOCK_EXCL
));
5902 if (XFS_FORCED_SHUTDOWN(ip
->i_mount
))
5905 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
5906 error
= xfs_bmap_last_extent(NULL
, ip
, XFS_DATA_FORK
, &got
, &is_empty
);
5907 if (!error
&& !is_empty
&& got
.br_startoff
>= off
&&
5908 ((got
.br_startoff
+ shift
) & BMBT_STARTOFF_MASK
) < got
.br_startoff
)
5910 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
5916 xfs_bmap_insert_extents(
5917 struct xfs_trans
*tp
,
5918 struct xfs_inode
*ip
,
5919 xfs_fileoff_t
*next_fsb
,
5920 xfs_fileoff_t offset_shift_fsb
,
5922 xfs_fileoff_t stop_fsb
)
5924 int whichfork
= XFS_DATA_FORK
;
5925 struct xfs_mount
*mp
= ip
->i_mount
;
5926 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5927 struct xfs_btree_cur
*cur
= NULL
;
5928 struct xfs_bmbt_irec got
, next
;
5929 struct xfs_iext_cursor icur
;
5930 xfs_fileoff_t new_startoff
;
5934 if (XFS_IS_CORRUPT(mp
, !xfs_ifork_has_extents(ip
, whichfork
)) ||
5935 XFS_TEST_ERROR(false, mp
, XFS_ERRTAG_BMAPIFORMAT
)) {
5936 return -EFSCORRUPTED
;
5939 if (XFS_FORCED_SHUTDOWN(mp
))
5942 ASSERT(xfs_isilocked(ip
, XFS_IOLOCK_EXCL
| XFS_ILOCK_EXCL
));
5944 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
5945 error
= xfs_iread_extents(tp
, ip
, whichfork
);
5950 if (ifp
->if_flags
& XFS_IFBROOT
) {
5951 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
5952 cur
->bc_private
.b
.flags
= 0;
5955 if (*next_fsb
== NULLFSBLOCK
) {
5956 xfs_iext_last(ifp
, &icur
);
5957 if (!xfs_iext_get_extent(ifp
, &icur
, &got
) ||
5958 stop_fsb
> got
.br_startoff
) {
5963 if (!xfs_iext_lookup_extent(ip
, ifp
, *next_fsb
, &icur
, &got
)) {
5968 if (XFS_IS_CORRUPT(mp
, isnullstartblock(got
.br_startblock
))) {
5969 error
= -EFSCORRUPTED
;
5973 if (XFS_IS_CORRUPT(mp
, stop_fsb
> got
.br_startoff
)) {
5974 error
= -EFSCORRUPTED
;
5978 new_startoff
= got
.br_startoff
+ offset_shift_fsb
;
5979 if (xfs_iext_peek_next_extent(ifp
, &icur
, &next
)) {
5980 if (new_startoff
+ got
.br_blockcount
> next
.br_startoff
) {
5986 * Unlike a left shift (which involves a hole punch), a right
5987 * shift does not modify extent neighbors in any way. We should
5988 * never find mergeable extents in this scenario. Check anyways
5989 * and warn if we encounter two extents that could be one.
5991 if (xfs_bmse_can_merge(&got
, &next
, offset_shift_fsb
))
5995 error
= xfs_bmap_shift_update_extent(tp
, ip
, whichfork
, &icur
, &got
,
5996 cur
, &logflags
, new_startoff
);
6000 if (!xfs_iext_prev_extent(ifp
, &icur
, &got
) ||
6001 stop_fsb
>= got
.br_startoff
+ got
.br_blockcount
) {
6006 *next_fsb
= got
.br_startoff
;
6009 xfs_btree_del_cursor(cur
, error
);
6011 xfs_trans_log_inode(tp
, ip
, logflags
);
6016 * Splits an extent into two extents at split_fsb block such that it is the
6017 * first block of the current_ext. @ext is a target extent to be split.
6018 * @split_fsb is a block where the extents is split. If split_fsb lies in a
6019 * hole or the first block of extents, just return 0.
6022 xfs_bmap_split_extent(
6023 struct xfs_trans
*tp
,
6024 struct xfs_inode
*ip
,
6025 xfs_fileoff_t split_fsb
)
6027 int whichfork
= XFS_DATA_FORK
;
6028 struct xfs_btree_cur
*cur
= NULL
;
6029 struct xfs_bmbt_irec got
;
6030 struct xfs_bmbt_irec
new; /* split extent */
6031 struct xfs_mount
*mp
= ip
->i_mount
;
6032 struct xfs_ifork
*ifp
;
6033 xfs_fsblock_t gotblkcnt
; /* new block count for got */
6034 struct xfs_iext_cursor icur
;
6039 if (XFS_IS_CORRUPT(mp
, !xfs_ifork_has_extents(ip
, whichfork
)) ||
6040 XFS_TEST_ERROR(false, mp
, XFS_ERRTAG_BMAPIFORMAT
)) {
6041 return -EFSCORRUPTED
;
6044 if (XFS_FORCED_SHUTDOWN(mp
))
6047 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
6048 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
6049 /* Read in all the extents */
6050 error
= xfs_iread_extents(tp
, ip
, whichfork
);
6056 * If there are not extents, or split_fsb lies in a hole we are done.
6058 if (!xfs_iext_lookup_extent(ip
, ifp
, split_fsb
, &icur
, &got
) ||
6059 got
.br_startoff
>= split_fsb
)
6062 gotblkcnt
= split_fsb
- got
.br_startoff
;
6063 new.br_startoff
= split_fsb
;
6064 new.br_startblock
= got
.br_startblock
+ gotblkcnt
;
6065 new.br_blockcount
= got
.br_blockcount
- gotblkcnt
;
6066 new.br_state
= got
.br_state
;
6068 if (ifp
->if_flags
& XFS_IFBROOT
) {
6069 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
6070 cur
->bc_private
.b
.flags
= 0;
6071 error
= xfs_bmbt_lookup_eq(cur
, &got
, &i
);
6074 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
6075 error
= -EFSCORRUPTED
;
6080 got
.br_blockcount
= gotblkcnt
;
6081 xfs_iext_update_extent(ip
, xfs_bmap_fork_to_state(whichfork
), &icur
,
6084 logflags
= XFS_ILOG_CORE
;
6086 error
= xfs_bmbt_update(cur
, &got
);
6090 logflags
|= XFS_ILOG_DEXT
;
6092 /* Add new extent */
6093 xfs_iext_next(ifp
, &icur
);
6094 xfs_iext_insert(ip
, &icur
, &new, 0);
6095 XFS_IFORK_NEXT_SET(ip
, whichfork
,
6096 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
6099 error
= xfs_bmbt_lookup_eq(cur
, &new, &i
);
6102 if (XFS_IS_CORRUPT(mp
, i
!= 0)) {
6103 error
= -EFSCORRUPTED
;
6106 error
= xfs_btree_insert(cur
, &i
);
6109 if (XFS_IS_CORRUPT(mp
, i
!= 1)) {
6110 error
= -EFSCORRUPTED
;
6116 * Convert to a btree if necessary.
6118 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
6119 int tmp_logflags
; /* partial log flag return val */
6121 ASSERT(cur
== NULL
);
6122 error
= xfs_bmap_extents_to_btree(tp
, ip
, &cur
, 0,
6123 &tmp_logflags
, whichfork
);
6124 logflags
|= tmp_logflags
;
6129 cur
->bc_private
.b
.allocated
= 0;
6130 xfs_btree_del_cursor(cur
, error
);
6134 xfs_trans_log_inode(tp
, ip
, logflags
);
6138 /* Deferred mapping is only for real extents in the data fork. */
6140 xfs_bmap_is_update_needed(
6141 struct xfs_bmbt_irec
*bmap
)
6143 return bmap
->br_startblock
!= HOLESTARTBLOCK
&&
6144 bmap
->br_startblock
!= DELAYSTARTBLOCK
;
6147 /* Record a bmap intent. */
6150 struct xfs_trans
*tp
,
6151 enum xfs_bmap_intent_type type
,
6152 struct xfs_inode
*ip
,
6154 struct xfs_bmbt_irec
*bmap
)
6156 struct xfs_bmap_intent
*bi
;
6158 trace_xfs_bmap_defer(tp
->t_mountp
,
6159 XFS_FSB_TO_AGNO(tp
->t_mountp
, bmap
->br_startblock
),
6161 XFS_FSB_TO_AGBNO(tp
->t_mountp
, bmap
->br_startblock
),
6162 ip
->i_ino
, whichfork
,
6164 bmap
->br_blockcount
,
6167 bi
= kmem_alloc(sizeof(struct xfs_bmap_intent
), KM_NOFS
);
6168 INIT_LIST_HEAD(&bi
->bi_list
);
6171 bi
->bi_whichfork
= whichfork
;
6172 bi
->bi_bmap
= *bmap
;
6174 xfs_defer_add(tp
, XFS_DEFER_OPS_TYPE_BMAP
, &bi
->bi_list
);
6178 /* Map an extent into a file. */
6180 xfs_bmap_map_extent(
6181 struct xfs_trans
*tp
,
6182 struct xfs_inode
*ip
,
6183 struct xfs_bmbt_irec
*PREV
)
6185 if (!xfs_bmap_is_update_needed(PREV
))
6188 __xfs_bmap_add(tp
, XFS_BMAP_MAP
, ip
, XFS_DATA_FORK
, PREV
);
6191 /* Unmap an extent out of a file. */
6193 xfs_bmap_unmap_extent(
6194 struct xfs_trans
*tp
,
6195 struct xfs_inode
*ip
,
6196 struct xfs_bmbt_irec
*PREV
)
6198 if (!xfs_bmap_is_update_needed(PREV
))
6201 __xfs_bmap_add(tp
, XFS_BMAP_UNMAP
, ip
, XFS_DATA_FORK
, PREV
);
6205 * Process one of the deferred bmap operations. We pass back the
6206 * btree cursor to maintain our lock on the bmapbt between calls.
6209 xfs_bmap_finish_one(
6210 struct xfs_trans
*tp
,
6211 struct xfs_inode
*ip
,
6212 enum xfs_bmap_intent_type type
,
6214 xfs_fileoff_t startoff
,
6215 xfs_fsblock_t startblock
,
6216 xfs_filblks_t
*blockcount
,
6221 ASSERT(tp
->t_firstblock
== NULLFSBLOCK
);
6223 trace_xfs_bmap_deferred(tp
->t_mountp
,
6224 XFS_FSB_TO_AGNO(tp
->t_mountp
, startblock
), type
,
6225 XFS_FSB_TO_AGBNO(tp
->t_mountp
, startblock
),
6226 ip
->i_ino
, whichfork
, startoff
, *blockcount
, state
);
6228 if (WARN_ON_ONCE(whichfork
!= XFS_DATA_FORK
))
6229 return -EFSCORRUPTED
;
6231 if (XFS_TEST_ERROR(false, tp
->t_mountp
,
6232 XFS_ERRTAG_BMAP_FINISH_ONE
))
6237 error
= xfs_bmapi_remap(tp
, ip
, startoff
, *blockcount
,
6241 case XFS_BMAP_UNMAP
:
6242 error
= __xfs_bunmapi(tp
, ip
, startoff
, blockcount
,
6243 XFS_BMAPI_REMAP
, 1);
6247 error
= -EFSCORRUPTED
;
6253 /* Check that an inode's extent does not have invalid flags or bad ranges. */
6255 xfs_bmap_validate_extent(
6256 struct xfs_inode
*ip
,
6258 struct xfs_bmbt_irec
*irec
)
6260 struct xfs_mount
*mp
= ip
->i_mount
;
6261 xfs_fsblock_t endfsb
;
6264 isrt
= XFS_IS_REALTIME_INODE(ip
);
6265 endfsb
= irec
->br_startblock
+ irec
->br_blockcount
- 1;
6267 if (!xfs_verify_rtbno(mp
, irec
->br_startblock
))
6268 return __this_address
;
6269 if (!xfs_verify_rtbno(mp
, endfsb
))
6270 return __this_address
;
6272 if (!xfs_verify_fsbno(mp
, irec
->br_startblock
))
6273 return __this_address
;
6274 if (!xfs_verify_fsbno(mp
, endfsb
))
6275 return __this_address
;
6276 if (XFS_FSB_TO_AGNO(mp
, irec
->br_startblock
) !=
6277 XFS_FSB_TO_AGNO(mp
, endfsb
))
6278 return __this_address
;
6280 if (irec
->br_state
!= XFS_EXT_NORM
&& whichfork
!= XFS_DATA_FORK
)
6281 return __this_address
;