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_trans_space.h"
24 #include "xfs_trace.h"
25 #include "xfs_attr_leaf.h"
26 #include "xfs_quota_defs.h"
28 #include "xfs_ag_resv.h"
29 #include "xfs_refcount.h"
32 kmem_zone_t
*xfs_bmap_free_item_zone
;
35 * Miscellaneous helper functions
39 * Compute and fill in the value of the maximum depth of a bmap btree
40 * in this filesystem. Done once, during mount.
43 xfs_bmap_compute_maxlevels(
44 xfs_mount_t
*mp
, /* file system mount structure */
45 int whichfork
) /* data or attr fork */
47 int level
; /* btree level */
48 uint maxblocks
; /* max blocks at this level */
49 uint maxleafents
; /* max leaf entries possible */
50 int maxrootrecs
; /* max records in root block */
51 int minleafrecs
; /* min records in leaf block */
52 int minnoderecs
; /* min records in node block */
53 int sz
; /* root block size */
56 * The maximum number of extents in a file, hence the maximum
57 * number of leaf entries, is controlled by the type of di_nextents
58 * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
59 * (a signed 16-bit number, xfs_aextnum_t).
61 * Note that we can no longer assume that if we are in ATTR1 that
62 * the fork offset of all the inodes will be
63 * (xfs_default_attroffset(ip) >> 3) because we could have mounted
64 * with ATTR2 and then mounted back with ATTR1, keeping the
65 * di_forkoff's fixed but probably at various positions. Therefore,
66 * for both ATTR1 and ATTR2 we have to assume the worst case scenario
67 * of a minimum size available.
69 if (whichfork
== XFS_DATA_FORK
) {
70 maxleafents
= MAXEXTNUM
;
71 sz
= XFS_BMDR_SPACE_CALC(MINDBTPTRS
);
73 maxleafents
= MAXAEXTNUM
;
74 sz
= XFS_BMDR_SPACE_CALC(MINABTPTRS
);
76 maxrootrecs
= xfs_bmdr_maxrecs(sz
, 0);
77 minleafrecs
= mp
->m_bmap_dmnr
[0];
78 minnoderecs
= mp
->m_bmap_dmnr
[1];
79 maxblocks
= (maxleafents
+ minleafrecs
- 1) / minleafrecs
;
80 for (level
= 1; maxblocks
> 1; level
++) {
81 if (maxblocks
<= maxrootrecs
)
84 maxblocks
= (maxblocks
+ minnoderecs
- 1) / minnoderecs
;
86 mp
->m_bm_maxlevels
[whichfork
] = level
;
89 STATIC
int /* error */
91 struct xfs_btree_cur
*cur
,
92 struct xfs_bmbt_irec
*irec
,
93 int *stat
) /* success/failure */
95 cur
->bc_rec
.b
= *irec
;
96 return xfs_btree_lookup(cur
, XFS_LOOKUP_EQ
, stat
);
99 STATIC
int /* error */
100 xfs_bmbt_lookup_first(
101 struct xfs_btree_cur
*cur
,
102 int *stat
) /* success/failure */
104 cur
->bc_rec
.b
.br_startoff
= 0;
105 cur
->bc_rec
.b
.br_startblock
= 0;
106 cur
->bc_rec
.b
.br_blockcount
= 0;
107 return xfs_btree_lookup(cur
, XFS_LOOKUP_GE
, stat
);
111 * Check if the inode needs to be converted to btree format.
113 static inline bool xfs_bmap_needs_btree(struct xfs_inode
*ip
, int whichfork
)
115 return whichfork
!= XFS_COW_FORK
&&
116 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
&&
117 XFS_IFORK_NEXTENTS(ip
, whichfork
) >
118 XFS_IFORK_MAXEXT(ip
, whichfork
);
122 * Check if the inode should be converted to extent format.
124 static inline bool xfs_bmap_wants_extents(struct xfs_inode
*ip
, int whichfork
)
126 return whichfork
!= XFS_COW_FORK
&&
127 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
&&
128 XFS_IFORK_NEXTENTS(ip
, whichfork
) <=
129 XFS_IFORK_MAXEXT(ip
, whichfork
);
133 * Update the record referred to by cur to the value given by irec
134 * This either works (return 0) or gets an EFSCORRUPTED error.
138 struct xfs_btree_cur
*cur
,
139 struct xfs_bmbt_irec
*irec
)
141 union xfs_btree_rec rec
;
143 xfs_bmbt_disk_set_all(&rec
.bmbt
, irec
);
144 return xfs_btree_update(cur
, &rec
);
148 * Compute the worst-case number of indirect blocks that will be used
149 * for ip's delayed extent of length "len".
152 xfs_bmap_worst_indlen(
153 xfs_inode_t
*ip
, /* incore inode pointer */
154 xfs_filblks_t len
) /* delayed extent length */
156 int level
; /* btree level number */
157 int maxrecs
; /* maximum record count at this level */
158 xfs_mount_t
*mp
; /* mount structure */
159 xfs_filblks_t rval
; /* return value */
162 maxrecs
= mp
->m_bmap_dmxr
[0];
163 for (level
= 0, rval
= 0;
164 level
< XFS_BM_MAXLEVELS(mp
, XFS_DATA_FORK
);
167 do_div(len
, maxrecs
);
170 return rval
+ XFS_BM_MAXLEVELS(mp
, XFS_DATA_FORK
) -
173 maxrecs
= mp
->m_bmap_dmxr
[1];
179 * Calculate the default attribute fork offset for newly created inodes.
182 xfs_default_attroffset(
183 struct xfs_inode
*ip
)
185 struct xfs_mount
*mp
= ip
->i_mount
;
188 if (mp
->m_sb
.sb_inodesize
== 256) {
189 offset
= XFS_LITINO(mp
, ip
->i_d
.di_version
) -
190 XFS_BMDR_SPACE_CALC(MINABTPTRS
);
192 offset
= XFS_BMDR_SPACE_CALC(6 * MINABTPTRS
);
195 ASSERT(offset
< XFS_LITINO(mp
, ip
->i_d
.di_version
));
200 * Helper routine to reset inode di_forkoff field when switching
201 * attribute fork from local to extent format - we reset it where
202 * possible to make space available for inline data fork extents.
205 xfs_bmap_forkoff_reset(
209 if (whichfork
== XFS_ATTR_FORK
&&
210 ip
->i_d
.di_format
!= XFS_DINODE_FMT_DEV
&&
211 ip
->i_d
.di_format
!= XFS_DINODE_FMT_BTREE
) {
212 uint dfl_forkoff
= xfs_default_attroffset(ip
) >> 3;
214 if (dfl_forkoff
> ip
->i_d
.di_forkoff
)
215 ip
->i_d
.di_forkoff
= dfl_forkoff
;
220 STATIC
struct xfs_buf
*
222 struct xfs_btree_cur
*cur
,
225 struct xfs_log_item
*lip
;
231 for (i
= 0; i
< XFS_BTREE_MAXLEVELS
; i
++) {
232 if (!cur
->bc_bufs
[i
])
234 if (XFS_BUF_ADDR(cur
->bc_bufs
[i
]) == bno
)
235 return cur
->bc_bufs
[i
];
238 /* Chase down all the log items to see if the bp is there */
239 list_for_each_entry(lip
, &cur
->bc_tp
->t_items
, li_trans
) {
240 struct xfs_buf_log_item
*bip
= (struct xfs_buf_log_item
*)lip
;
242 if (bip
->bli_item
.li_type
== XFS_LI_BUF
&&
243 XFS_BUF_ADDR(bip
->bli_buf
) == bno
)
252 struct xfs_btree_block
*block
,
258 __be64
*pp
, *thispa
; /* pointer to block address */
259 xfs_bmbt_key_t
*prevp
, *keyp
;
261 ASSERT(be16_to_cpu(block
->bb_level
) > 0);
264 for( i
= 1; i
<= xfs_btree_get_numrecs(block
); i
++) {
265 dmxr
= mp
->m_bmap_dmxr
[0];
266 keyp
= XFS_BMBT_KEY_ADDR(mp
, block
, i
);
269 ASSERT(be64_to_cpu(prevp
->br_startoff
) <
270 be64_to_cpu(keyp
->br_startoff
));
275 * Compare the block numbers to see if there are dups.
278 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, i
, sz
);
280 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, i
, dmxr
);
282 for (j
= i
+1; j
<= be16_to_cpu(block
->bb_numrecs
); j
++) {
284 thispa
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, j
, sz
);
286 thispa
= XFS_BMBT_PTR_ADDR(mp
, block
, j
, dmxr
);
287 if (*thispa
== *pp
) {
288 xfs_warn(mp
, "%s: thispa(%d) == pp(%d) %Ld",
290 (unsigned long long)be64_to_cpu(*thispa
));
291 xfs_err(mp
, "%s: ptrs are equal in node\n",
293 xfs_force_shutdown(mp
, SHUTDOWN_CORRUPT_INCORE
);
300 * Check that the extents for the inode ip are in the right order in all
301 * btree leaves. THis becomes prohibitively expensive for large extent count
302 * files, so don't bother with inodes that have more than 10,000 extents in
303 * them. The btree record ordering checks will still be done, so for such large
304 * bmapbt constructs that is going to catch most corruptions.
307 xfs_bmap_check_leaf_extents(
308 xfs_btree_cur_t
*cur
, /* btree cursor or null */
309 xfs_inode_t
*ip
, /* incore inode pointer */
310 int whichfork
) /* data or attr fork */
312 struct xfs_btree_block
*block
; /* current btree block */
313 xfs_fsblock_t bno
; /* block # of "block" */
314 xfs_buf_t
*bp
; /* buffer for "block" */
315 int error
; /* error return value */
316 xfs_extnum_t i
=0, j
; /* index into the extents list */
317 struct xfs_ifork
*ifp
; /* fork structure */
318 int level
; /* btree level, for checking */
319 xfs_mount_t
*mp
; /* file system mount structure */
320 __be64
*pp
; /* pointer to block address */
321 xfs_bmbt_rec_t
*ep
; /* pointer to current extent */
322 xfs_bmbt_rec_t last
= {0, 0}; /* last extent in prev block */
323 xfs_bmbt_rec_t
*nextp
; /* pointer to next extent */
326 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
) {
330 /* skip large extent count inodes */
331 if (ip
->i_d
.di_nextents
> 10000)
336 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
337 block
= ifp
->if_broot
;
339 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
341 level
= be16_to_cpu(block
->bb_level
);
343 xfs_check_block(block
, mp
, 1, ifp
->if_broot_bytes
);
344 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, 1, ifp
->if_broot_bytes
);
345 bno
= be64_to_cpu(*pp
);
347 ASSERT(bno
!= NULLFSBLOCK
);
348 ASSERT(XFS_FSB_TO_AGNO(mp
, bno
) < mp
->m_sb
.sb_agcount
);
349 ASSERT(XFS_FSB_TO_AGBNO(mp
, bno
) < mp
->m_sb
.sb_agblocks
);
352 * Go down the tree until leaf level is reached, following the first
353 * pointer (leftmost) at each level.
355 while (level
-- > 0) {
356 /* See if buf is in cur first */
358 bp
= xfs_bmap_get_bp(cur
, XFS_FSB_TO_DADDR(mp
, bno
));
361 error
= xfs_btree_read_bufl(mp
, NULL
, bno
, &bp
,
367 block
= XFS_BUF_TO_BLOCK(bp
);
372 * Check this block for basic sanity (increasing keys and
373 * no duplicate blocks).
376 xfs_check_block(block
, mp
, 0, 0);
377 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, mp
->m_bmap_dmxr
[1]);
378 bno
= be64_to_cpu(*pp
);
379 XFS_WANT_CORRUPTED_GOTO(mp
,
380 xfs_verify_fsbno(mp
, bno
), error0
);
383 xfs_trans_brelse(NULL
, bp
);
388 * Here with bp and block set to the leftmost leaf node in the tree.
393 * Loop over all leaf nodes checking that all extents are in the right order.
396 xfs_fsblock_t nextbno
;
397 xfs_extnum_t num_recs
;
400 num_recs
= xfs_btree_get_numrecs(block
);
403 * Read-ahead the next leaf block, if any.
406 nextbno
= be64_to_cpu(block
->bb_u
.l
.bb_rightsib
);
409 * Check all the extents to make sure they are OK.
410 * If we had a previous block, the last entry should
411 * conform with the first entry in this one.
414 ep
= XFS_BMBT_REC_ADDR(mp
, block
, 1);
416 ASSERT(xfs_bmbt_disk_get_startoff(&last
) +
417 xfs_bmbt_disk_get_blockcount(&last
) <=
418 xfs_bmbt_disk_get_startoff(ep
));
420 for (j
= 1; j
< num_recs
; j
++) {
421 nextp
= XFS_BMBT_REC_ADDR(mp
, block
, j
+ 1);
422 ASSERT(xfs_bmbt_disk_get_startoff(ep
) +
423 xfs_bmbt_disk_get_blockcount(ep
) <=
424 xfs_bmbt_disk_get_startoff(nextp
));
432 xfs_trans_brelse(NULL
, bp
);
436 * If we've reached the end, stop.
438 if (bno
== NULLFSBLOCK
)
442 bp
= xfs_bmap_get_bp(cur
, XFS_FSB_TO_DADDR(mp
, bno
));
445 error
= xfs_btree_read_bufl(mp
, NULL
, bno
, &bp
,
451 block
= XFS_BUF_TO_BLOCK(bp
);
457 xfs_warn(mp
, "%s: at error0", __func__
);
459 xfs_trans_brelse(NULL
, bp
);
461 xfs_warn(mp
, "%s: BAD after btree leaves for %d extents",
463 xfs_err(mp
, "%s: CORRUPTED BTREE OR SOMETHING", __func__
);
464 xfs_force_shutdown(mp
, SHUTDOWN_CORRUPT_INCORE
);
469 * Validate that the bmbt_irecs being returned from bmapi are valid
470 * given the caller's original parameters. Specifically check the
471 * ranges of the returned irecs to ensure that they only extend beyond
472 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
475 xfs_bmap_validate_ret(
479 xfs_bmbt_irec_t
*mval
,
483 int i
; /* index to map values */
485 ASSERT(ret_nmap
<= nmap
);
487 for (i
= 0; i
< ret_nmap
; i
++) {
488 ASSERT(mval
[i
].br_blockcount
> 0);
489 if (!(flags
& XFS_BMAPI_ENTIRE
)) {
490 ASSERT(mval
[i
].br_startoff
>= bno
);
491 ASSERT(mval
[i
].br_blockcount
<= len
);
492 ASSERT(mval
[i
].br_startoff
+ mval
[i
].br_blockcount
<=
495 ASSERT(mval
[i
].br_startoff
< bno
+ len
);
496 ASSERT(mval
[i
].br_startoff
+ mval
[i
].br_blockcount
>
500 mval
[i
- 1].br_startoff
+ mval
[i
- 1].br_blockcount
==
501 mval
[i
].br_startoff
);
502 ASSERT(mval
[i
].br_startblock
!= DELAYSTARTBLOCK
&&
503 mval
[i
].br_startblock
!= HOLESTARTBLOCK
);
504 ASSERT(mval
[i
].br_state
== XFS_EXT_NORM
||
505 mval
[i
].br_state
== XFS_EXT_UNWRITTEN
);
510 #define xfs_bmap_check_leaf_extents(cur, ip, whichfork) do { } while (0)
511 #define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap) do { } while (0)
515 * bmap free list manipulation functions
519 * Add the extent to the list of extents to be free at transaction end.
520 * The list is maintained sorted (by block number).
524 struct xfs_trans
*tp
,
527 const struct xfs_owner_info
*oinfo
,
530 struct xfs_extent_free_item
*new; /* new element */
532 struct xfs_mount
*mp
= tp
->t_mountp
;
536 ASSERT(bno
!= NULLFSBLOCK
);
538 ASSERT(len
<= MAXEXTLEN
);
539 ASSERT(!isnullstartblock(bno
));
540 agno
= XFS_FSB_TO_AGNO(mp
, bno
);
541 agbno
= XFS_FSB_TO_AGBNO(mp
, bno
);
542 ASSERT(agno
< mp
->m_sb
.sb_agcount
);
543 ASSERT(agbno
< mp
->m_sb
.sb_agblocks
);
544 ASSERT(len
< mp
->m_sb
.sb_agblocks
);
545 ASSERT(agbno
+ len
<= mp
->m_sb
.sb_agblocks
);
547 ASSERT(xfs_bmap_free_item_zone
!= NULL
);
549 new = kmem_zone_alloc(xfs_bmap_free_item_zone
, 0);
550 new->xefi_startblock
= bno
;
551 new->xefi_blockcount
= (xfs_extlen_t
)len
;
553 new->xefi_oinfo
= *oinfo
;
555 new->xefi_oinfo
= XFS_RMAP_OINFO_SKIP_UPDATE
;
556 new->xefi_skip_discard
= skip_discard
;
557 trace_xfs_bmap_free_defer(tp
->t_mountp
,
558 XFS_FSB_TO_AGNO(tp
->t_mountp
, bno
), 0,
559 XFS_FSB_TO_AGBNO(tp
->t_mountp
, bno
), len
);
560 xfs_defer_add(tp
, XFS_DEFER_OPS_TYPE_FREE
, &new->xefi_list
);
564 * Inode fork format manipulation functions
568 * Convert the inode format to extent format if it currently is in btree format,
569 * but the extent list is small enough that it fits into the extent format.
571 * Since the extents are already in-core, all we have to do is give up the space
572 * for the btree root and pitch the leaf block.
574 STATIC
int /* error */
575 xfs_bmap_btree_to_extents(
576 struct xfs_trans
*tp
, /* transaction pointer */
577 struct xfs_inode
*ip
, /* incore inode pointer */
578 struct xfs_btree_cur
*cur
, /* btree cursor */
579 int *logflagsp
, /* inode logging flags */
580 int whichfork
) /* data or attr fork */
582 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
583 struct xfs_mount
*mp
= ip
->i_mount
;
584 struct xfs_btree_block
*rblock
= ifp
->if_broot
;
585 struct xfs_btree_block
*cblock
;/* child btree block */
586 xfs_fsblock_t cbno
; /* child block number */
587 xfs_buf_t
*cbp
; /* child block's buffer */
588 int error
; /* error return value */
589 __be64
*pp
; /* ptr to block address */
590 struct xfs_owner_info oinfo
;
592 /* check if we actually need the extent format first: */
593 if (!xfs_bmap_wants_extents(ip
, whichfork
))
597 ASSERT(whichfork
!= XFS_COW_FORK
);
598 ASSERT(ifp
->if_flags
& XFS_IFEXTENTS
);
599 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
);
600 ASSERT(be16_to_cpu(rblock
->bb_level
) == 1);
601 ASSERT(be16_to_cpu(rblock
->bb_numrecs
) == 1);
602 ASSERT(xfs_bmbt_maxrecs(mp
, ifp
->if_broot_bytes
, 0) == 1);
604 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, rblock
, 1, ifp
->if_broot_bytes
);
605 cbno
= be64_to_cpu(*pp
);
607 XFS_WANT_CORRUPTED_RETURN(cur
->bc_mp
,
608 xfs_btree_check_lptr(cur
, cbno
, 1));
610 error
= xfs_btree_read_bufl(mp
, tp
, cbno
, &cbp
, XFS_BMAP_BTREE_REF
,
614 cblock
= XFS_BUF_TO_BLOCK(cbp
);
615 if ((error
= xfs_btree_check_block(cur
, cblock
, 0, cbp
)))
617 xfs_rmap_ino_bmbt_owner(&oinfo
, ip
->i_ino
, whichfork
);
618 xfs_bmap_add_free(cur
->bc_tp
, cbno
, 1, &oinfo
);
619 ip
->i_d
.di_nblocks
--;
620 xfs_trans_mod_dquot_byino(tp
, ip
, XFS_TRANS_DQ_BCOUNT
, -1L);
621 xfs_trans_binval(tp
, cbp
);
622 if (cur
->bc_bufs
[0] == cbp
)
623 cur
->bc_bufs
[0] = NULL
;
624 xfs_iroot_realloc(ip
, -1, whichfork
);
625 ASSERT(ifp
->if_broot
== NULL
);
626 ASSERT((ifp
->if_flags
& XFS_IFBROOT
) == 0);
627 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_EXTENTS
);
628 *logflagsp
|= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
633 * Convert an extents-format file into a btree-format file.
634 * The new file will have a root block (in the inode) and a single child block.
636 STATIC
int /* error */
637 xfs_bmap_extents_to_btree(
638 struct xfs_trans
*tp
, /* transaction pointer */
639 struct xfs_inode
*ip
, /* incore inode pointer */
640 struct xfs_btree_cur
**curp
, /* cursor returned to caller */
641 int wasdel
, /* converting a delayed alloc */
642 int *logflagsp
, /* inode logging flags */
643 int whichfork
) /* data or attr fork */
645 struct xfs_btree_block
*ablock
; /* allocated (child) bt block */
646 struct xfs_buf
*abp
; /* buffer for ablock */
647 struct xfs_alloc_arg args
; /* allocation arguments */
648 struct xfs_bmbt_rec
*arp
; /* child record pointer */
649 struct xfs_btree_block
*block
; /* btree root block */
650 struct xfs_btree_cur
*cur
; /* bmap btree cursor */
651 int error
; /* error return value */
652 struct xfs_ifork
*ifp
; /* inode fork pointer */
653 struct xfs_bmbt_key
*kp
; /* root block key pointer */
654 struct xfs_mount
*mp
; /* mount structure */
655 xfs_bmbt_ptr_t
*pp
; /* root block address pointer */
656 struct xfs_iext_cursor icur
;
657 struct xfs_bmbt_irec rec
;
658 xfs_extnum_t cnt
= 0;
661 ASSERT(whichfork
!= XFS_COW_FORK
);
662 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
663 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
);
666 * Make space in the inode incore. This needs to be undone if we fail
667 * to expand the root.
669 xfs_iroot_realloc(ip
, 1, whichfork
);
670 ifp
->if_flags
|= XFS_IFBROOT
;
675 block
= ifp
->if_broot
;
676 xfs_btree_init_block_int(mp
, block
, XFS_BUF_DADDR_NULL
,
677 XFS_BTNUM_BMAP
, 1, 1, ip
->i_ino
,
678 XFS_BTREE_LONG_PTRS
);
680 * Need a cursor. Can't allocate until bb_level is filled in.
682 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
683 cur
->bc_private
.b
.flags
= wasdel
? XFS_BTCUR_BPRV_WASDEL
: 0;
685 * Convert to a btree with two levels, one record in root.
687 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_BTREE
);
688 memset(&args
, 0, sizeof(args
));
691 xfs_rmap_ino_bmbt_owner(&args
.oinfo
, ip
->i_ino
, whichfork
);
692 if (tp
->t_firstblock
== NULLFSBLOCK
) {
693 args
.type
= XFS_ALLOCTYPE_START_BNO
;
694 args
.fsbno
= XFS_INO_TO_FSB(mp
, ip
->i_ino
);
695 } else if (tp
->t_flags
& XFS_TRANS_LOWMODE
) {
696 args
.type
= XFS_ALLOCTYPE_START_BNO
;
697 args
.fsbno
= tp
->t_firstblock
;
699 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
700 args
.fsbno
= tp
->t_firstblock
;
702 args
.minlen
= args
.maxlen
= args
.prod
= 1;
703 args
.wasdel
= wasdel
;
705 error
= xfs_alloc_vextent(&args
);
707 goto out_root_realloc
;
709 if (WARN_ON_ONCE(args
.fsbno
== NULLFSBLOCK
)) {
711 goto out_root_realloc
;
715 * Allocation can't fail, the space was reserved.
717 ASSERT(tp
->t_firstblock
== NULLFSBLOCK
||
718 args
.agno
>= XFS_FSB_TO_AGNO(mp
, tp
->t_firstblock
));
719 tp
->t_firstblock
= args
.fsbno
;
720 cur
->bc_private
.b
.allocated
++;
721 ip
->i_d
.di_nblocks
++;
722 xfs_trans_mod_dquot_byino(tp
, ip
, XFS_TRANS_DQ_BCOUNT
, 1L);
723 abp
= xfs_btree_get_bufl(mp
, tp
, args
.fsbno
);
725 error
= -EFSCORRUPTED
;
726 goto out_unreserve_dquot
;
730 * Fill in the child block.
732 abp
->b_ops
= &xfs_bmbt_buf_ops
;
733 ablock
= XFS_BUF_TO_BLOCK(abp
);
734 xfs_btree_init_block_int(mp
, ablock
, abp
->b_bn
,
735 XFS_BTNUM_BMAP
, 0, 0, ip
->i_ino
,
736 XFS_BTREE_LONG_PTRS
);
738 for_each_xfs_iext(ifp
, &icur
, &rec
) {
739 if (isnullstartblock(rec
.br_startblock
))
741 arp
= XFS_BMBT_REC_ADDR(mp
, ablock
, 1 + cnt
);
742 xfs_bmbt_disk_set_all(arp
, &rec
);
745 ASSERT(cnt
== XFS_IFORK_NEXTENTS(ip
, whichfork
));
746 xfs_btree_set_numrecs(ablock
, cnt
);
749 * Fill in the root key and pointer.
751 kp
= XFS_BMBT_KEY_ADDR(mp
, block
, 1);
752 arp
= XFS_BMBT_REC_ADDR(mp
, ablock
, 1);
753 kp
->br_startoff
= cpu_to_be64(xfs_bmbt_disk_get_startoff(arp
));
754 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, xfs_bmbt_get_maxrecs(cur
,
755 be16_to_cpu(block
->bb_level
)));
756 *pp
= cpu_to_be64(args
.fsbno
);
759 * Do all this logging at the end so that
760 * the root is at the right level.
762 xfs_btree_log_block(cur
, abp
, XFS_BB_ALL_BITS
);
763 xfs_btree_log_recs(cur
, abp
, 1, be16_to_cpu(ablock
->bb_numrecs
));
764 ASSERT(*curp
== NULL
);
766 *logflagsp
= XFS_ILOG_CORE
| xfs_ilog_fbroot(whichfork
);
770 xfs_trans_mod_dquot_byino(tp
, ip
, XFS_TRANS_DQ_BCOUNT
, -1L);
772 xfs_iroot_realloc(ip
, -1, whichfork
);
773 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_EXTENTS
);
774 ASSERT(ifp
->if_broot
== NULL
);
775 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
781 * Convert a local file to an extents file.
782 * This code is out of bounds for data forks of regular files,
783 * since the file data needs to get logged so things will stay consistent.
784 * (The bmap-level manipulations are ok, though).
787 xfs_bmap_local_to_extents_empty(
788 struct xfs_inode
*ip
,
791 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
793 ASSERT(whichfork
!= XFS_COW_FORK
);
794 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
795 ASSERT(ifp
->if_bytes
== 0);
796 ASSERT(XFS_IFORK_NEXTENTS(ip
, whichfork
) == 0);
798 xfs_bmap_forkoff_reset(ip
, whichfork
);
799 ifp
->if_flags
&= ~XFS_IFINLINE
;
800 ifp
->if_flags
|= XFS_IFEXTENTS
;
801 ifp
->if_u1
.if_root
= NULL
;
803 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_EXTENTS
);
807 STATIC
int /* error */
808 xfs_bmap_local_to_extents(
809 xfs_trans_t
*tp
, /* transaction pointer */
810 xfs_inode_t
*ip
, /* incore inode pointer */
811 xfs_extlen_t total
, /* total blocks needed by transaction */
812 int *logflagsp
, /* inode logging flags */
814 void (*init_fn
)(struct xfs_trans
*tp
,
816 struct xfs_inode
*ip
,
817 struct xfs_ifork
*ifp
))
820 int flags
; /* logging flags returned */
821 struct xfs_ifork
*ifp
; /* inode fork pointer */
822 xfs_alloc_arg_t args
; /* allocation arguments */
823 xfs_buf_t
*bp
; /* buffer for extent block */
824 struct xfs_bmbt_irec rec
;
825 struct xfs_iext_cursor icur
;
828 * We don't want to deal with the case of keeping inode data inline yet.
829 * So sending the data fork of a regular inode is invalid.
831 ASSERT(!(S_ISREG(VFS_I(ip
)->i_mode
) && whichfork
== XFS_DATA_FORK
));
832 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
833 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
835 if (!ifp
->if_bytes
) {
836 xfs_bmap_local_to_extents_empty(ip
, whichfork
);
837 flags
= XFS_ILOG_CORE
;
843 ASSERT((ifp
->if_flags
& (XFS_IFINLINE
|XFS_IFEXTENTS
)) == XFS_IFINLINE
);
844 memset(&args
, 0, sizeof(args
));
846 args
.mp
= ip
->i_mount
;
847 xfs_rmap_ino_owner(&args
.oinfo
, ip
->i_ino
, whichfork
, 0);
849 * Allocate a block. We know we need only one, since the
850 * file currently fits in an inode.
852 if (tp
->t_firstblock
== NULLFSBLOCK
) {
853 args
.fsbno
= XFS_INO_TO_FSB(args
.mp
, ip
->i_ino
);
854 args
.type
= XFS_ALLOCTYPE_START_BNO
;
856 args
.fsbno
= tp
->t_firstblock
;
857 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
860 args
.minlen
= args
.maxlen
= args
.prod
= 1;
861 error
= xfs_alloc_vextent(&args
);
865 /* Can't fail, the space was reserved. */
866 ASSERT(args
.fsbno
!= NULLFSBLOCK
);
867 ASSERT(args
.len
== 1);
868 tp
->t_firstblock
= args
.fsbno
;
869 bp
= xfs_btree_get_bufl(args
.mp
, tp
, args
.fsbno
);
872 * Initialize the block, copy the data and log the remote buffer.
874 * The callout is responsible for logging because the remote format
875 * might differ from the local format and thus we don't know how much to
876 * log here. Note that init_fn must also set the buffer log item type
879 init_fn(tp
, bp
, ip
, ifp
);
881 /* account for the change in fork size */
882 xfs_idata_realloc(ip
, -ifp
->if_bytes
, whichfork
);
883 xfs_bmap_local_to_extents_empty(ip
, whichfork
);
884 flags
|= XFS_ILOG_CORE
;
886 ifp
->if_u1
.if_root
= NULL
;
890 rec
.br_startblock
= args
.fsbno
;
891 rec
.br_blockcount
= 1;
892 rec
.br_state
= XFS_EXT_NORM
;
893 xfs_iext_first(ifp
, &icur
);
894 xfs_iext_insert(ip
, &icur
, &rec
, 0);
896 XFS_IFORK_NEXT_SET(ip
, whichfork
, 1);
897 ip
->i_d
.di_nblocks
= 1;
898 xfs_trans_mod_dquot_byino(tp
, ip
,
899 XFS_TRANS_DQ_BCOUNT
, 1L);
900 flags
|= xfs_ilog_fext(whichfork
);
908 * Called from xfs_bmap_add_attrfork to handle btree format files.
910 STATIC
int /* error */
911 xfs_bmap_add_attrfork_btree(
912 xfs_trans_t
*tp
, /* transaction pointer */
913 xfs_inode_t
*ip
, /* incore inode pointer */
914 int *flags
) /* inode logging flags */
916 xfs_btree_cur_t
*cur
; /* btree cursor */
917 int error
; /* error return value */
918 xfs_mount_t
*mp
; /* file system mount struct */
919 int stat
; /* newroot status */
922 if (ip
->i_df
.if_broot_bytes
<= XFS_IFORK_DSIZE(ip
))
923 *flags
|= XFS_ILOG_DBROOT
;
925 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, XFS_DATA_FORK
);
926 error
= xfs_bmbt_lookup_first(cur
, &stat
);
929 /* must be at least one entry */
930 XFS_WANT_CORRUPTED_GOTO(mp
, stat
== 1, error0
);
931 if ((error
= xfs_btree_new_iroot(cur
, flags
, &stat
)))
934 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
937 cur
->bc_private
.b
.allocated
= 0;
938 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
942 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
947 * Called from xfs_bmap_add_attrfork to handle extents format files.
949 STATIC
int /* error */
950 xfs_bmap_add_attrfork_extents(
951 struct xfs_trans
*tp
, /* transaction pointer */
952 struct xfs_inode
*ip
, /* incore inode pointer */
953 int *flags
) /* inode logging flags */
955 xfs_btree_cur_t
*cur
; /* bmap btree cursor */
956 int error
; /* error return value */
958 if (ip
->i_d
.di_nextents
* sizeof(xfs_bmbt_rec_t
) <= XFS_IFORK_DSIZE(ip
))
961 error
= xfs_bmap_extents_to_btree(tp
, ip
, &cur
, 0, flags
,
964 cur
->bc_private
.b
.allocated
= 0;
965 xfs_btree_del_cursor(cur
, error
);
971 * Called from xfs_bmap_add_attrfork to handle local format files. Each
972 * different data fork content type needs a different callout to do the
973 * conversion. Some are basic and only require special block initialisation
974 * callouts for the data formating, others (directories) are so specialised they
975 * handle everything themselves.
977 * XXX (dgc): investigate whether directory conversion can use the generic
978 * formatting callout. It should be possible - it's just a very complex
981 STATIC
int /* error */
982 xfs_bmap_add_attrfork_local(
983 struct xfs_trans
*tp
, /* transaction pointer */
984 struct xfs_inode
*ip
, /* incore inode pointer */
985 int *flags
) /* inode logging flags */
987 struct xfs_da_args dargs
; /* args for dir/attr code */
989 if (ip
->i_df
.if_bytes
<= XFS_IFORK_DSIZE(ip
))
992 if (S_ISDIR(VFS_I(ip
)->i_mode
)) {
993 memset(&dargs
, 0, sizeof(dargs
));
994 dargs
.geo
= ip
->i_mount
->m_dir_geo
;
996 dargs
.total
= dargs
.geo
->fsbcount
;
997 dargs
.whichfork
= XFS_DATA_FORK
;
999 return xfs_dir2_sf_to_block(&dargs
);
1002 if (S_ISLNK(VFS_I(ip
)->i_mode
))
1003 return xfs_bmap_local_to_extents(tp
, ip
, 1, flags
,
1005 xfs_symlink_local_to_remote
);
1007 /* should only be called for types that support local format data */
1009 return -EFSCORRUPTED
;
1012 /* Set an inode attr fork off based on the format */
1014 xfs_bmap_set_attrforkoff(
1015 struct xfs_inode
*ip
,
1019 switch (ip
->i_d
.di_format
) {
1020 case XFS_DINODE_FMT_DEV
:
1021 ip
->i_d
.di_forkoff
= roundup(sizeof(xfs_dev_t
), 8) >> 3;
1023 case XFS_DINODE_FMT_LOCAL
:
1024 case XFS_DINODE_FMT_EXTENTS
:
1025 case XFS_DINODE_FMT_BTREE
:
1026 ip
->i_d
.di_forkoff
= xfs_attr_shortform_bytesfit(ip
, size
);
1027 if (!ip
->i_d
.di_forkoff
)
1028 ip
->i_d
.di_forkoff
= xfs_default_attroffset(ip
) >> 3;
1029 else if ((ip
->i_mount
->m_flags
& XFS_MOUNT_ATTR2
) && version
)
1041 * Convert inode from non-attributed to attributed.
1042 * Must not be in a transaction, ip must not be locked.
1044 int /* error code */
1045 xfs_bmap_add_attrfork(
1046 xfs_inode_t
*ip
, /* incore inode pointer */
1047 int size
, /* space new attribute needs */
1048 int rsvd
) /* xact may use reserved blks */
1050 xfs_mount_t
*mp
; /* mount structure */
1051 xfs_trans_t
*tp
; /* transaction pointer */
1052 int blks
; /* space reservation */
1053 int version
= 1; /* superblock attr version */
1054 int logflags
; /* logging flags */
1055 int error
; /* error return value */
1057 ASSERT(XFS_IFORK_Q(ip
) == 0);
1060 ASSERT(!XFS_NOT_DQATTACHED(mp
, ip
));
1062 blks
= XFS_ADDAFORK_SPACE_RES(mp
);
1064 error
= xfs_trans_alloc(mp
, &M_RES(mp
)->tr_addafork
, blks
, 0,
1065 rsvd
? XFS_TRANS_RESERVE
: 0, &tp
);
1069 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
1070 error
= xfs_trans_reserve_quota_nblks(tp
, ip
, blks
, 0, rsvd
?
1071 XFS_QMOPT_RES_REGBLKS
| XFS_QMOPT_FORCE_RES
:
1072 XFS_QMOPT_RES_REGBLKS
);
1075 if (XFS_IFORK_Q(ip
))
1077 if (ip
->i_d
.di_anextents
!= 0) {
1078 error
= -EFSCORRUPTED
;
1081 if (ip
->i_d
.di_aformat
!= XFS_DINODE_FMT_EXTENTS
) {
1083 * For inodes coming from pre-6.2 filesystems.
1085 ASSERT(ip
->i_d
.di_aformat
== 0);
1086 ip
->i_d
.di_aformat
= XFS_DINODE_FMT_EXTENTS
;
1089 xfs_trans_ijoin(tp
, ip
, 0);
1090 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
1091 error
= xfs_bmap_set_attrforkoff(ip
, size
, &version
);
1094 ASSERT(ip
->i_afp
== NULL
);
1095 ip
->i_afp
= kmem_zone_zalloc(xfs_ifork_zone
, 0);
1096 ip
->i_afp
->if_flags
= XFS_IFEXTENTS
;
1098 switch (ip
->i_d
.di_format
) {
1099 case XFS_DINODE_FMT_LOCAL
:
1100 error
= xfs_bmap_add_attrfork_local(tp
, ip
, &logflags
);
1102 case XFS_DINODE_FMT_EXTENTS
:
1103 error
= xfs_bmap_add_attrfork_extents(tp
, ip
, &logflags
);
1105 case XFS_DINODE_FMT_BTREE
:
1106 error
= xfs_bmap_add_attrfork_btree(tp
, ip
, &logflags
);
1113 xfs_trans_log_inode(tp
, ip
, logflags
);
1116 if (!xfs_sb_version_hasattr(&mp
->m_sb
) ||
1117 (!xfs_sb_version_hasattr2(&mp
->m_sb
) && version
== 2)) {
1118 bool log_sb
= false;
1120 spin_lock(&mp
->m_sb_lock
);
1121 if (!xfs_sb_version_hasattr(&mp
->m_sb
)) {
1122 xfs_sb_version_addattr(&mp
->m_sb
);
1125 if (!xfs_sb_version_hasattr2(&mp
->m_sb
) && version
== 2) {
1126 xfs_sb_version_addattr2(&mp
->m_sb
);
1129 spin_unlock(&mp
->m_sb_lock
);
1134 error
= xfs_trans_commit(tp
);
1135 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
1139 xfs_trans_cancel(tp
);
1140 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
1145 * Internal and external extent tree search functions.
1149 * Read in extents from a btree-format inode.
1153 struct xfs_trans
*tp
,
1154 struct xfs_inode
*ip
,
1157 struct xfs_mount
*mp
= ip
->i_mount
;
1158 int state
= xfs_bmap_fork_to_state(whichfork
);
1159 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1160 xfs_extnum_t nextents
= XFS_IFORK_NEXTENTS(ip
, whichfork
);
1161 struct xfs_btree_block
*block
= ifp
->if_broot
;
1162 struct xfs_iext_cursor icur
;
1163 struct xfs_bmbt_irec
new;
1171 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
1173 if (unlikely(XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)) {
1174 XFS_ERROR_REPORT(__func__
, XFS_ERRLEVEL_LOW
, mp
);
1175 return -EFSCORRUPTED
;
1179 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
1181 level
= be16_to_cpu(block
->bb_level
);
1182 if (unlikely(level
== 0)) {
1183 XFS_ERROR_REPORT(__func__
, XFS_ERRLEVEL_LOW
, mp
);
1184 return -EFSCORRUPTED
;
1186 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, 1, ifp
->if_broot_bytes
);
1187 bno
= be64_to_cpu(*pp
);
1190 * Go down the tree until leaf level is reached, following the first
1191 * pointer (leftmost) at each level.
1193 while (level
-- > 0) {
1194 error
= xfs_btree_read_bufl(mp
, tp
, bno
, &bp
,
1195 XFS_BMAP_BTREE_REF
, &xfs_bmbt_buf_ops
);
1198 block
= XFS_BUF_TO_BLOCK(bp
);
1201 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, mp
->m_bmap_dmxr
[1]);
1202 bno
= be64_to_cpu(*pp
);
1203 XFS_WANT_CORRUPTED_GOTO(mp
,
1204 xfs_verify_fsbno(mp
, bno
), out_brelse
);
1205 xfs_trans_brelse(tp
, bp
);
1209 * Here with bp and block set to the leftmost leaf node in the tree.
1212 xfs_iext_first(ifp
, &icur
);
1215 * Loop over all leaf nodes. Copy information to the extent records.
1218 xfs_bmbt_rec_t
*frp
;
1219 xfs_fsblock_t nextbno
;
1220 xfs_extnum_t num_recs
;
1222 num_recs
= xfs_btree_get_numrecs(block
);
1223 if (unlikely(i
+ num_recs
> nextents
)) {
1224 xfs_warn(ip
->i_mount
,
1225 "corrupt dinode %Lu, (btree extents).",
1226 (unsigned long long) ip
->i_ino
);
1227 xfs_inode_verifier_error(ip
, -EFSCORRUPTED
,
1228 __func__
, block
, sizeof(*block
),
1230 error
= -EFSCORRUPTED
;
1234 * Read-ahead the next leaf block, if any.
1236 nextbno
= be64_to_cpu(block
->bb_u
.l
.bb_rightsib
);
1237 if (nextbno
!= NULLFSBLOCK
)
1238 xfs_btree_reada_bufl(mp
, nextbno
, 1,
1241 * Copy records into the extent records.
1243 frp
= XFS_BMBT_REC_ADDR(mp
, block
, 1);
1244 for (j
= 0; j
< num_recs
; j
++, frp
++, i
++) {
1247 xfs_bmbt_disk_get_all(frp
, &new);
1248 fa
= xfs_bmap_validate_extent(ip
, whichfork
, &new);
1250 error
= -EFSCORRUPTED
;
1251 xfs_inode_verifier_error(ip
, error
,
1252 "xfs_iread_extents(2)",
1253 frp
, sizeof(*frp
), fa
);
1256 xfs_iext_insert(ip
, &icur
, &new, state
);
1257 trace_xfs_read_extent(ip
, &icur
, state
, _THIS_IP_
);
1258 xfs_iext_next(ifp
, &icur
);
1260 xfs_trans_brelse(tp
, bp
);
1263 * If we've reached the end, stop.
1265 if (bno
== NULLFSBLOCK
)
1267 error
= xfs_btree_read_bufl(mp
, tp
, bno
, &bp
,
1268 XFS_BMAP_BTREE_REF
, &xfs_bmbt_buf_ops
);
1271 block
= XFS_BUF_TO_BLOCK(bp
);
1274 if (i
!= XFS_IFORK_NEXTENTS(ip
, whichfork
)) {
1275 error
= -EFSCORRUPTED
;
1278 ASSERT(i
== xfs_iext_count(ifp
));
1280 ifp
->if_flags
|= XFS_IFEXTENTS
;
1284 xfs_trans_brelse(tp
, bp
);
1286 xfs_iext_destroy(ifp
);
1291 * Returns the relative block number of the first unused block(s) in the given
1292 * fork with at least "len" logically contiguous blocks free. This is the
1293 * lowest-address hole if the fork has holes, else the first block past the end
1294 * of fork. Return 0 if the fork is currently local (in-inode).
1297 xfs_bmap_first_unused(
1298 struct xfs_trans
*tp
, /* transaction pointer */
1299 struct xfs_inode
*ip
, /* incore inode */
1300 xfs_extlen_t len
, /* size of hole to find */
1301 xfs_fileoff_t
*first_unused
, /* unused block */
1302 int whichfork
) /* data or attr fork */
1304 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1305 struct xfs_bmbt_irec got
;
1306 struct xfs_iext_cursor icur
;
1307 xfs_fileoff_t lastaddr
= 0;
1308 xfs_fileoff_t lowest
, max
;
1311 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
||
1312 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
||
1313 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
1315 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
) {
1320 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
1321 error
= xfs_iread_extents(tp
, ip
, whichfork
);
1326 lowest
= max
= *first_unused
;
1327 for_each_xfs_iext(ifp
, &icur
, &got
) {
1329 * See if the hole before this extent will work.
1331 if (got
.br_startoff
>= lowest
+ len
&&
1332 got
.br_startoff
- max
>= len
)
1334 lastaddr
= got
.br_startoff
+ got
.br_blockcount
;
1335 max
= XFS_FILEOFF_MAX(lastaddr
, lowest
);
1338 *first_unused
= max
;
1343 * Returns the file-relative block number of the last block - 1 before
1344 * last_block (input value) in the file.
1345 * This is not based on i_size, it is based on the extent records.
1346 * Returns 0 for local files, as they do not have extent records.
1349 xfs_bmap_last_before(
1350 struct xfs_trans
*tp
, /* transaction pointer */
1351 struct xfs_inode
*ip
, /* incore inode */
1352 xfs_fileoff_t
*last_block
, /* last block */
1353 int whichfork
) /* data or attr fork */
1355 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1356 struct xfs_bmbt_irec got
;
1357 struct xfs_iext_cursor icur
;
1360 switch (XFS_IFORK_FORMAT(ip
, whichfork
)) {
1361 case XFS_DINODE_FMT_LOCAL
:
1364 case XFS_DINODE_FMT_BTREE
:
1365 case XFS_DINODE_FMT_EXTENTS
:
1371 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
1372 error
= xfs_iread_extents(tp
, ip
, whichfork
);
1377 if (!xfs_iext_lookup_extent_before(ip
, ifp
, last_block
, &icur
, &got
))
1383 xfs_bmap_last_extent(
1384 struct xfs_trans
*tp
,
1385 struct xfs_inode
*ip
,
1387 struct xfs_bmbt_irec
*rec
,
1390 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1391 struct xfs_iext_cursor icur
;
1394 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
1395 error
= xfs_iread_extents(tp
, ip
, whichfork
);
1400 xfs_iext_last(ifp
, &icur
);
1401 if (!xfs_iext_get_extent(ifp
, &icur
, rec
))
1409 * Check the last inode extent to determine whether this allocation will result
1410 * in blocks being allocated at the end of the file. When we allocate new data
1411 * blocks at the end of the file which do not start at the previous data block,
1412 * we will try to align the new blocks at stripe unit boundaries.
1414 * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be
1415 * at, or past the EOF.
1419 struct xfs_bmalloca
*bma
,
1422 struct xfs_bmbt_irec rec
;
1427 error
= xfs_bmap_last_extent(NULL
, bma
->ip
, whichfork
, &rec
,
1438 * Check if we are allocation or past the last extent, or at least into
1439 * the last delayed allocated extent.
1441 bma
->aeof
= bma
->offset
>= rec
.br_startoff
+ rec
.br_blockcount
||
1442 (bma
->offset
>= rec
.br_startoff
&&
1443 isnullstartblock(rec
.br_startblock
));
1448 * Returns the file-relative block number of the first block past eof in
1449 * the file. This is not based on i_size, it is based on the extent records.
1450 * Returns 0 for local files, as they do not have extent records.
1453 xfs_bmap_last_offset(
1454 struct xfs_inode
*ip
,
1455 xfs_fileoff_t
*last_block
,
1458 struct xfs_bmbt_irec rec
;
1464 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
)
1467 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
&&
1468 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
1471 error
= xfs_bmap_last_extent(NULL
, ip
, whichfork
, &rec
, &is_empty
);
1472 if (error
|| is_empty
)
1475 *last_block
= rec
.br_startoff
+ rec
.br_blockcount
;
1480 * Returns whether the selected fork of the inode has exactly one
1481 * block or not. For the data fork we check this matches di_size,
1482 * implying the file's range is 0..bsize-1.
1484 int /* 1=>1 block, 0=>otherwise */
1486 xfs_inode_t
*ip
, /* incore inode */
1487 int whichfork
) /* data or attr fork */
1489 struct xfs_ifork
*ifp
; /* inode fork pointer */
1490 int rval
; /* return value */
1491 xfs_bmbt_irec_t s
; /* internal version of extent */
1492 struct xfs_iext_cursor icur
;
1495 if (whichfork
== XFS_DATA_FORK
)
1496 return XFS_ISIZE(ip
) == ip
->i_mount
->m_sb
.sb_blocksize
;
1498 if (XFS_IFORK_NEXTENTS(ip
, whichfork
) != 1)
1500 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
1502 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1503 ASSERT(ifp
->if_flags
& XFS_IFEXTENTS
);
1504 xfs_iext_first(ifp
, &icur
);
1505 xfs_iext_get_extent(ifp
, &icur
, &s
);
1506 rval
= s
.br_startoff
== 0 && s
.br_blockcount
== 1;
1507 if (rval
&& whichfork
== XFS_DATA_FORK
)
1508 ASSERT(XFS_ISIZE(ip
) == ip
->i_mount
->m_sb
.sb_blocksize
);
1513 * Extent tree manipulation functions used during allocation.
1517 * Convert a delayed allocation to a real allocation.
1519 STATIC
int /* error */
1520 xfs_bmap_add_extent_delay_real(
1521 struct xfs_bmalloca
*bma
,
1524 struct xfs_bmbt_irec
*new = &bma
->got
;
1525 int error
; /* error return value */
1526 int i
; /* temp state */
1527 struct xfs_ifork
*ifp
; /* inode fork pointer */
1528 xfs_fileoff_t new_endoff
; /* end offset of new entry */
1529 xfs_bmbt_irec_t r
[3]; /* neighbor extent entries */
1530 /* left is 0, right is 1, prev is 2 */
1531 int rval
=0; /* return value (logging flags) */
1532 int state
= xfs_bmap_fork_to_state(whichfork
);
1533 xfs_filblks_t da_new
; /* new count del alloc blocks used */
1534 xfs_filblks_t da_old
; /* old count del alloc blocks used */
1535 xfs_filblks_t temp
=0; /* value for da_new calculations */
1536 int tmp_rval
; /* partial logging flags */
1537 struct xfs_mount
*mp
;
1538 xfs_extnum_t
*nextents
;
1539 struct xfs_bmbt_irec old
;
1541 mp
= bma
->ip
->i_mount
;
1542 ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
1543 ASSERT(whichfork
!= XFS_ATTR_FORK
);
1544 nextents
= (whichfork
== XFS_COW_FORK
? &bma
->ip
->i_cnextents
:
1545 &bma
->ip
->i_d
.di_nextents
);
1547 ASSERT(!isnullstartblock(new->br_startblock
));
1549 (bma
->cur
->bc_private
.b
.flags
& XFS_BTCUR_BPRV_WASDEL
));
1551 XFS_STATS_INC(mp
, xs_add_exlist
);
1558 * Set up a bunch of variables to make the tests simpler.
1560 xfs_iext_get_extent(ifp
, &bma
->icur
, &PREV
);
1561 new_endoff
= new->br_startoff
+ new->br_blockcount
;
1562 ASSERT(isnullstartblock(PREV
.br_startblock
));
1563 ASSERT(PREV
.br_startoff
<= new->br_startoff
);
1564 ASSERT(PREV
.br_startoff
+ PREV
.br_blockcount
>= new_endoff
);
1566 da_old
= startblockval(PREV
.br_startblock
);
1570 * Set flags determining what part of the previous delayed allocation
1571 * extent is being replaced by a real allocation.
1573 if (PREV
.br_startoff
== new->br_startoff
)
1574 state
|= BMAP_LEFT_FILLING
;
1575 if (PREV
.br_startoff
+ PREV
.br_blockcount
== new_endoff
)
1576 state
|= BMAP_RIGHT_FILLING
;
1579 * Check and set flags if this segment has a left neighbor.
1580 * Don't set contiguous if the combined extent would be too large.
1582 if (xfs_iext_peek_prev_extent(ifp
, &bma
->icur
, &LEFT
)) {
1583 state
|= BMAP_LEFT_VALID
;
1584 if (isnullstartblock(LEFT
.br_startblock
))
1585 state
|= BMAP_LEFT_DELAY
;
1588 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
1589 LEFT
.br_startoff
+ LEFT
.br_blockcount
== new->br_startoff
&&
1590 LEFT
.br_startblock
+ LEFT
.br_blockcount
== new->br_startblock
&&
1591 LEFT
.br_state
== new->br_state
&&
1592 LEFT
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
1593 state
|= BMAP_LEFT_CONTIG
;
1596 * Check and set flags if this segment has a right neighbor.
1597 * Don't set contiguous if the combined extent would be too large.
1598 * Also check for all-three-contiguous being too large.
1600 if (xfs_iext_peek_next_extent(ifp
, &bma
->icur
, &RIGHT
)) {
1601 state
|= BMAP_RIGHT_VALID
;
1602 if (isnullstartblock(RIGHT
.br_startblock
))
1603 state
|= BMAP_RIGHT_DELAY
;
1606 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
1607 new_endoff
== RIGHT
.br_startoff
&&
1608 new->br_startblock
+ new->br_blockcount
== RIGHT
.br_startblock
&&
1609 new->br_state
== RIGHT
.br_state
&&
1610 new->br_blockcount
+ RIGHT
.br_blockcount
<= MAXEXTLEN
&&
1611 ((state
& (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
1612 BMAP_RIGHT_FILLING
)) !=
1613 (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
1614 BMAP_RIGHT_FILLING
) ||
1615 LEFT
.br_blockcount
+ new->br_blockcount
+ RIGHT
.br_blockcount
1617 state
|= BMAP_RIGHT_CONTIG
;
1621 * Switch out based on the FILLING and CONTIG state bits.
1623 switch (state
& (BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
1624 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
)) {
1625 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
1626 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
1628 * Filling in all of a previously delayed allocation extent.
1629 * The left and right neighbors are both contiguous with new.
1631 LEFT
.br_blockcount
+= PREV
.br_blockcount
+ RIGHT
.br_blockcount
;
1633 xfs_iext_remove(bma
->ip
, &bma
->icur
, state
);
1634 xfs_iext_remove(bma
->ip
, &bma
->icur
, state
);
1635 xfs_iext_prev(ifp
, &bma
->icur
);
1636 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &LEFT
);
1639 if (bma
->cur
== NULL
)
1640 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1642 rval
= XFS_ILOG_CORE
;
1643 error
= xfs_bmbt_lookup_eq(bma
->cur
, &RIGHT
, &i
);
1646 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1647 error
= xfs_btree_delete(bma
->cur
, &i
);
1650 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1651 error
= xfs_btree_decrement(bma
->cur
, 0, &i
);
1654 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1655 error
= xfs_bmbt_update(bma
->cur
, &LEFT
);
1661 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
1663 * Filling in all of a previously delayed allocation extent.
1664 * The left neighbor is contiguous, the right is not.
1667 LEFT
.br_blockcount
+= PREV
.br_blockcount
;
1669 xfs_iext_remove(bma
->ip
, &bma
->icur
, state
);
1670 xfs_iext_prev(ifp
, &bma
->icur
);
1671 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &LEFT
);
1673 if (bma
->cur
== NULL
)
1674 rval
= XFS_ILOG_DEXT
;
1677 error
= xfs_bmbt_lookup_eq(bma
->cur
, &old
, &i
);
1680 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1681 error
= xfs_bmbt_update(bma
->cur
, &LEFT
);
1687 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
1689 * Filling in all of a previously delayed allocation extent.
1690 * The right neighbor is contiguous, the left is not. Take care
1691 * with delay -> unwritten extent allocation here because the
1692 * delalloc record we are overwriting is always written.
1694 PREV
.br_startblock
= new->br_startblock
;
1695 PREV
.br_blockcount
+= RIGHT
.br_blockcount
;
1696 PREV
.br_state
= new->br_state
;
1698 xfs_iext_next(ifp
, &bma
->icur
);
1699 xfs_iext_remove(bma
->ip
, &bma
->icur
, state
);
1700 xfs_iext_prev(ifp
, &bma
->icur
);
1701 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &PREV
);
1703 if (bma
->cur
== NULL
)
1704 rval
= XFS_ILOG_DEXT
;
1707 error
= xfs_bmbt_lookup_eq(bma
->cur
, &RIGHT
, &i
);
1710 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1711 error
= xfs_bmbt_update(bma
->cur
, &PREV
);
1717 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
1719 * Filling in all of a previously delayed allocation extent.
1720 * Neither the left nor right neighbors are contiguous with
1723 PREV
.br_startblock
= new->br_startblock
;
1724 PREV
.br_state
= new->br_state
;
1725 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &PREV
);
1728 if (bma
->cur
== NULL
)
1729 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1731 rval
= XFS_ILOG_CORE
;
1732 error
= xfs_bmbt_lookup_eq(bma
->cur
, new, &i
);
1735 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
1736 error
= xfs_btree_insert(bma
->cur
, &i
);
1739 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1743 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
:
1745 * Filling in the first part of a previous delayed allocation.
1746 * The left neighbor is contiguous.
1749 temp
= PREV
.br_blockcount
- new->br_blockcount
;
1750 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
1751 startblockval(PREV
.br_startblock
));
1753 LEFT
.br_blockcount
+= new->br_blockcount
;
1755 PREV
.br_blockcount
= temp
;
1756 PREV
.br_startoff
+= new->br_blockcount
;
1757 PREV
.br_startblock
= nullstartblock(da_new
);
1759 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &PREV
);
1760 xfs_iext_prev(ifp
, &bma
->icur
);
1761 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &LEFT
);
1763 if (bma
->cur
== NULL
)
1764 rval
= XFS_ILOG_DEXT
;
1767 error
= xfs_bmbt_lookup_eq(bma
->cur
, &old
, &i
);
1770 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1771 error
= xfs_bmbt_update(bma
->cur
, &LEFT
);
1777 case BMAP_LEFT_FILLING
:
1779 * Filling in the first part of a previous delayed allocation.
1780 * The left neighbor is not contiguous.
1782 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, new);
1784 if (bma
->cur
== NULL
)
1785 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1787 rval
= XFS_ILOG_CORE
;
1788 error
= xfs_bmbt_lookup_eq(bma
->cur
, new, &i
);
1791 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
1792 error
= xfs_btree_insert(bma
->cur
, &i
);
1795 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1798 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
1799 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
1800 &bma
->cur
, 1, &tmp_rval
, whichfork
);
1806 temp
= PREV
.br_blockcount
- new->br_blockcount
;
1807 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
1808 startblockval(PREV
.br_startblock
) -
1809 (bma
->cur
? bma
->cur
->bc_private
.b
.allocated
: 0));
1811 PREV
.br_startoff
= new_endoff
;
1812 PREV
.br_blockcount
= temp
;
1813 PREV
.br_startblock
= nullstartblock(da_new
);
1814 xfs_iext_next(ifp
, &bma
->icur
);
1815 xfs_iext_insert(bma
->ip
, &bma
->icur
, &PREV
, state
);
1816 xfs_iext_prev(ifp
, &bma
->icur
);
1819 case BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
1821 * Filling in the last part of a previous delayed allocation.
1822 * The right neighbor is contiguous with the new allocation.
1825 RIGHT
.br_startoff
= new->br_startoff
;
1826 RIGHT
.br_startblock
= new->br_startblock
;
1827 RIGHT
.br_blockcount
+= new->br_blockcount
;
1829 if (bma
->cur
== NULL
)
1830 rval
= XFS_ILOG_DEXT
;
1833 error
= xfs_bmbt_lookup_eq(bma
->cur
, &old
, &i
);
1836 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1837 error
= xfs_bmbt_update(bma
->cur
, &RIGHT
);
1842 temp
= PREV
.br_blockcount
- new->br_blockcount
;
1843 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
1844 startblockval(PREV
.br_startblock
));
1846 PREV
.br_blockcount
= temp
;
1847 PREV
.br_startblock
= nullstartblock(da_new
);
1849 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &PREV
);
1850 xfs_iext_next(ifp
, &bma
->icur
);
1851 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &RIGHT
);
1854 case BMAP_RIGHT_FILLING
:
1856 * Filling in the last part of a previous delayed allocation.
1857 * The right neighbor is not contiguous.
1859 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, new);
1861 if (bma
->cur
== NULL
)
1862 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1864 rval
= XFS_ILOG_CORE
;
1865 error
= xfs_bmbt_lookup_eq(bma
->cur
, new, &i
);
1868 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
1869 error
= xfs_btree_insert(bma
->cur
, &i
);
1872 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1875 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
1876 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
1877 &bma
->cur
, 1, &tmp_rval
, whichfork
);
1883 temp
= PREV
.br_blockcount
- new->br_blockcount
;
1884 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
1885 startblockval(PREV
.br_startblock
) -
1886 (bma
->cur
? bma
->cur
->bc_private
.b
.allocated
: 0));
1888 PREV
.br_startblock
= nullstartblock(da_new
);
1889 PREV
.br_blockcount
= temp
;
1890 xfs_iext_insert(bma
->ip
, &bma
->icur
, &PREV
, state
);
1891 xfs_iext_next(ifp
, &bma
->icur
);
1896 * Filling in the middle part of a previous delayed allocation.
1897 * Contiguity is impossible here.
1898 * This case is avoided almost all the time.
1900 * We start with a delayed allocation:
1902 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
1905 * and we are allocating:
1906 * +rrrrrrrrrrrrrrrrr+
1909 * and we set it up for insertion as:
1910 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
1912 * PREV @ idx LEFT RIGHT
1913 * inserted at idx + 1
1917 /* LEFT is the new middle */
1920 /* RIGHT is the new right */
1921 RIGHT
.br_state
= PREV
.br_state
;
1922 RIGHT
.br_startoff
= new_endoff
;
1923 RIGHT
.br_blockcount
=
1924 PREV
.br_startoff
+ PREV
.br_blockcount
- new_endoff
;
1925 RIGHT
.br_startblock
=
1926 nullstartblock(xfs_bmap_worst_indlen(bma
->ip
,
1927 RIGHT
.br_blockcount
));
1930 PREV
.br_blockcount
= new->br_startoff
- PREV
.br_startoff
;
1931 PREV
.br_startblock
=
1932 nullstartblock(xfs_bmap_worst_indlen(bma
->ip
,
1933 PREV
.br_blockcount
));
1934 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &PREV
);
1936 xfs_iext_next(ifp
, &bma
->icur
);
1937 xfs_iext_insert(bma
->ip
, &bma
->icur
, &RIGHT
, state
);
1938 xfs_iext_insert(bma
->ip
, &bma
->icur
, &LEFT
, state
);
1941 if (bma
->cur
== NULL
)
1942 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1944 rval
= XFS_ILOG_CORE
;
1945 error
= xfs_bmbt_lookup_eq(bma
->cur
, new, &i
);
1948 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
1949 error
= xfs_btree_insert(bma
->cur
, &i
);
1952 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1955 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
1956 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
1957 &bma
->cur
, 1, &tmp_rval
, whichfork
);
1963 da_new
= startblockval(PREV
.br_startblock
) +
1964 startblockval(RIGHT
.br_startblock
);
1967 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
1968 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
1969 case BMAP_LEFT_FILLING
| BMAP_RIGHT_CONTIG
:
1970 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
1971 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
1972 case BMAP_LEFT_CONTIG
:
1973 case BMAP_RIGHT_CONTIG
:
1975 * These cases are all impossible.
1980 /* add reverse mapping unless caller opted out */
1981 if (!(bma
->flags
& XFS_BMAPI_NORMAP
))
1982 xfs_rmap_map_extent(bma
->tp
, bma
->ip
, whichfork
, new);
1984 /* convert to a btree if necessary */
1985 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
1986 int tmp_logflags
; /* partial log flag return val */
1988 ASSERT(bma
->cur
== NULL
);
1989 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
1990 &bma
->cur
, da_old
> 0, &tmp_logflags
,
1992 bma
->logflags
|= tmp_logflags
;
1997 if (da_new
!= da_old
)
1998 xfs_mod_delalloc(mp
, (int64_t)da_new
- da_old
);
2001 da_new
+= bma
->cur
->bc_private
.b
.allocated
;
2002 bma
->cur
->bc_private
.b
.allocated
= 0;
2005 /* adjust for changes in reserved delayed indirect blocks */
2006 if (da_new
!= da_old
) {
2007 ASSERT(state
== 0 || da_new
< da_old
);
2008 error
= xfs_mod_fdblocks(mp
, (int64_t)(da_old
- da_new
),
2012 xfs_bmap_check_leaf_extents(bma
->cur
, bma
->ip
, whichfork
);
2014 if (whichfork
!= XFS_COW_FORK
)
2015 bma
->logflags
|= rval
;
2023 * Convert an unwritten allocation to a real allocation or vice versa.
2026 xfs_bmap_add_extent_unwritten_real(
2027 struct xfs_trans
*tp
,
2028 xfs_inode_t
*ip
, /* incore inode pointer */
2030 struct xfs_iext_cursor
*icur
,
2031 xfs_btree_cur_t
**curp
, /* if *curp is null, not a btree */
2032 xfs_bmbt_irec_t
*new, /* new data to add to file extents */
2033 int *logflagsp
) /* inode logging flags */
2035 xfs_btree_cur_t
*cur
; /* btree cursor */
2036 int error
; /* error return value */
2037 int i
; /* temp state */
2038 struct xfs_ifork
*ifp
; /* inode fork pointer */
2039 xfs_fileoff_t new_endoff
; /* end offset of new entry */
2040 xfs_bmbt_irec_t r
[3]; /* neighbor extent entries */
2041 /* left is 0, right is 1, prev is 2 */
2042 int rval
=0; /* return value (logging flags) */
2043 int state
= xfs_bmap_fork_to_state(whichfork
);
2044 struct xfs_mount
*mp
= ip
->i_mount
;
2045 struct xfs_bmbt_irec old
;
2050 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
2052 ASSERT(!isnullstartblock(new->br_startblock
));
2054 XFS_STATS_INC(mp
, xs_add_exlist
);
2061 * Set up a bunch of variables to make the tests simpler.
2064 xfs_iext_get_extent(ifp
, icur
, &PREV
);
2065 ASSERT(new->br_state
!= PREV
.br_state
);
2066 new_endoff
= new->br_startoff
+ new->br_blockcount
;
2067 ASSERT(PREV
.br_startoff
<= new->br_startoff
);
2068 ASSERT(PREV
.br_startoff
+ PREV
.br_blockcount
>= new_endoff
);
2071 * Set flags determining what part of the previous oldext allocation
2072 * extent is being replaced by a newext allocation.
2074 if (PREV
.br_startoff
== new->br_startoff
)
2075 state
|= BMAP_LEFT_FILLING
;
2076 if (PREV
.br_startoff
+ PREV
.br_blockcount
== new_endoff
)
2077 state
|= BMAP_RIGHT_FILLING
;
2080 * Check and set flags if this segment has a left neighbor.
2081 * Don't set contiguous if the combined extent would be too large.
2083 if (xfs_iext_peek_prev_extent(ifp
, icur
, &LEFT
)) {
2084 state
|= BMAP_LEFT_VALID
;
2085 if (isnullstartblock(LEFT
.br_startblock
))
2086 state
|= BMAP_LEFT_DELAY
;
2089 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
2090 LEFT
.br_startoff
+ LEFT
.br_blockcount
== new->br_startoff
&&
2091 LEFT
.br_startblock
+ LEFT
.br_blockcount
== new->br_startblock
&&
2092 LEFT
.br_state
== new->br_state
&&
2093 LEFT
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
2094 state
|= BMAP_LEFT_CONTIG
;
2097 * Check and set flags if this segment has a right neighbor.
2098 * Don't set contiguous if the combined extent would be too large.
2099 * Also check for all-three-contiguous being too large.
2101 if (xfs_iext_peek_next_extent(ifp
, icur
, &RIGHT
)) {
2102 state
|= BMAP_RIGHT_VALID
;
2103 if (isnullstartblock(RIGHT
.br_startblock
))
2104 state
|= BMAP_RIGHT_DELAY
;
2107 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
2108 new_endoff
== RIGHT
.br_startoff
&&
2109 new->br_startblock
+ new->br_blockcount
== RIGHT
.br_startblock
&&
2110 new->br_state
== RIGHT
.br_state
&&
2111 new->br_blockcount
+ RIGHT
.br_blockcount
<= MAXEXTLEN
&&
2112 ((state
& (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
2113 BMAP_RIGHT_FILLING
)) !=
2114 (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
2115 BMAP_RIGHT_FILLING
) ||
2116 LEFT
.br_blockcount
+ new->br_blockcount
+ RIGHT
.br_blockcount
2118 state
|= BMAP_RIGHT_CONTIG
;
2121 * Switch out based on the FILLING and CONTIG state bits.
2123 switch (state
& (BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
2124 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
)) {
2125 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
2126 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2128 * Setting all of a previous oldext extent to newext.
2129 * The left and right neighbors are both contiguous with new.
2131 LEFT
.br_blockcount
+= PREV
.br_blockcount
+ RIGHT
.br_blockcount
;
2133 xfs_iext_remove(ip
, icur
, state
);
2134 xfs_iext_remove(ip
, icur
, state
);
2135 xfs_iext_prev(ifp
, icur
);
2136 xfs_iext_update_extent(ip
, state
, icur
, &LEFT
);
2137 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2138 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 2);
2140 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2142 rval
= XFS_ILOG_CORE
;
2143 error
= xfs_bmbt_lookup_eq(cur
, &RIGHT
, &i
);
2146 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2147 if ((error
= xfs_btree_delete(cur
, &i
)))
2149 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2150 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2152 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2153 if ((error
= xfs_btree_delete(cur
, &i
)))
2155 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2156 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2158 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2159 error
= xfs_bmbt_update(cur
, &LEFT
);
2165 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
2167 * Setting all of a previous oldext extent to newext.
2168 * The left neighbor is contiguous, the right is not.
2170 LEFT
.br_blockcount
+= PREV
.br_blockcount
;
2172 xfs_iext_remove(ip
, icur
, state
);
2173 xfs_iext_prev(ifp
, icur
);
2174 xfs_iext_update_extent(ip
, state
, icur
, &LEFT
);
2175 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2176 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
2178 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2180 rval
= XFS_ILOG_CORE
;
2181 error
= xfs_bmbt_lookup_eq(cur
, &PREV
, &i
);
2184 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2185 if ((error
= xfs_btree_delete(cur
, &i
)))
2187 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2188 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2190 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2191 error
= xfs_bmbt_update(cur
, &LEFT
);
2197 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2199 * Setting all of a previous oldext extent to newext.
2200 * The right neighbor is contiguous, the left is not.
2202 PREV
.br_blockcount
+= RIGHT
.br_blockcount
;
2203 PREV
.br_state
= new->br_state
;
2205 xfs_iext_next(ifp
, icur
);
2206 xfs_iext_remove(ip
, icur
, state
);
2207 xfs_iext_prev(ifp
, icur
);
2208 xfs_iext_update_extent(ip
, state
, icur
, &PREV
);
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
, &RIGHT
, &i
);
2219 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2220 if ((error
= xfs_btree_delete(cur
, &i
)))
2222 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2223 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2225 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2226 error
= xfs_bmbt_update(cur
, &PREV
);
2232 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
2234 * Setting all of a previous oldext extent to newext.
2235 * Neither the left nor right neighbors are contiguous with
2238 PREV
.br_state
= new->br_state
;
2239 xfs_iext_update_extent(ip
, state
, icur
, &PREV
);
2242 rval
= XFS_ILOG_DEXT
;
2245 error
= xfs_bmbt_lookup_eq(cur
, new, &i
);
2248 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2249 error
= xfs_bmbt_update(cur
, &PREV
);
2255 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
:
2257 * Setting the first part of a previous oldext extent to newext.
2258 * The left neighbor is contiguous.
2260 LEFT
.br_blockcount
+= new->br_blockcount
;
2263 PREV
.br_startoff
+= new->br_blockcount
;
2264 PREV
.br_startblock
+= new->br_blockcount
;
2265 PREV
.br_blockcount
-= new->br_blockcount
;
2267 xfs_iext_update_extent(ip
, state
, icur
, &PREV
);
2268 xfs_iext_prev(ifp
, icur
);
2269 xfs_iext_update_extent(ip
, state
, icur
, &LEFT
);
2272 rval
= XFS_ILOG_DEXT
;
2275 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2278 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2279 error
= xfs_bmbt_update(cur
, &PREV
);
2282 error
= xfs_btree_decrement(cur
, 0, &i
);
2285 error
= xfs_bmbt_update(cur
, &LEFT
);
2291 case BMAP_LEFT_FILLING
:
2293 * Setting the first part of a previous oldext extent to newext.
2294 * The left neighbor is not contiguous.
2297 PREV
.br_startoff
+= new->br_blockcount
;
2298 PREV
.br_startblock
+= new->br_blockcount
;
2299 PREV
.br_blockcount
-= new->br_blockcount
;
2301 xfs_iext_update_extent(ip
, state
, icur
, &PREV
);
2302 xfs_iext_insert(ip
, icur
, new, state
);
2303 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2304 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
2306 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2308 rval
= XFS_ILOG_CORE
;
2309 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2312 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2313 error
= xfs_bmbt_update(cur
, &PREV
);
2316 cur
->bc_rec
.b
= *new;
2317 if ((error
= xfs_btree_insert(cur
, &i
)))
2319 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2323 case BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2325 * Setting the last part of a previous oldext extent to newext.
2326 * The right neighbor is contiguous with the new allocation.
2329 PREV
.br_blockcount
-= new->br_blockcount
;
2331 RIGHT
.br_startoff
= new->br_startoff
;
2332 RIGHT
.br_startblock
= new->br_startblock
;
2333 RIGHT
.br_blockcount
+= new->br_blockcount
;
2335 xfs_iext_update_extent(ip
, state
, icur
, &PREV
);
2336 xfs_iext_next(ifp
, icur
);
2337 xfs_iext_update_extent(ip
, state
, icur
, &RIGHT
);
2340 rval
= XFS_ILOG_DEXT
;
2343 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2346 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2347 error
= xfs_bmbt_update(cur
, &PREV
);
2350 error
= xfs_btree_increment(cur
, 0, &i
);
2353 error
= xfs_bmbt_update(cur
, &RIGHT
);
2359 case BMAP_RIGHT_FILLING
:
2361 * Setting the last part of a previous oldext extent to newext.
2362 * The right neighbor is not contiguous.
2365 PREV
.br_blockcount
-= new->br_blockcount
;
2367 xfs_iext_update_extent(ip
, state
, icur
, &PREV
);
2368 xfs_iext_next(ifp
, icur
);
2369 xfs_iext_insert(ip
, icur
, new, state
);
2371 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2372 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
2374 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2376 rval
= XFS_ILOG_CORE
;
2377 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2380 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2381 error
= xfs_bmbt_update(cur
, &PREV
);
2384 error
= xfs_bmbt_lookup_eq(cur
, new, &i
);
2387 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
2388 if ((error
= xfs_btree_insert(cur
, &i
)))
2390 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2396 * Setting the middle part of a previous oldext extent to
2397 * newext. Contiguity is impossible here.
2398 * One extent becomes three extents.
2401 PREV
.br_blockcount
= new->br_startoff
- PREV
.br_startoff
;
2404 r
[1].br_startoff
= new_endoff
;
2405 r
[1].br_blockcount
=
2406 old
.br_startoff
+ old
.br_blockcount
- new_endoff
;
2407 r
[1].br_startblock
= new->br_startblock
+ new->br_blockcount
;
2408 r
[1].br_state
= PREV
.br_state
;
2410 xfs_iext_update_extent(ip
, state
, icur
, &PREV
);
2411 xfs_iext_next(ifp
, icur
);
2412 xfs_iext_insert(ip
, icur
, &r
[1], state
);
2413 xfs_iext_insert(ip
, icur
, &r
[0], state
);
2415 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2416 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 2);
2418 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2420 rval
= XFS_ILOG_CORE
;
2421 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2424 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2425 /* new right extent - oldext */
2426 error
= xfs_bmbt_update(cur
, &r
[1]);
2429 /* new left extent - oldext */
2430 cur
->bc_rec
.b
= PREV
;
2431 if ((error
= xfs_btree_insert(cur
, &i
)))
2433 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2435 * Reset the cursor to the position of the new extent
2436 * we are about to insert as we can't trust it after
2437 * the previous insert.
2439 error
= xfs_bmbt_lookup_eq(cur
, new, &i
);
2442 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
2443 /* new middle extent - newext */
2444 if ((error
= xfs_btree_insert(cur
, &i
)))
2446 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2450 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2451 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2452 case BMAP_LEFT_FILLING
| BMAP_RIGHT_CONTIG
:
2453 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
2454 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2455 case BMAP_LEFT_CONTIG
:
2456 case BMAP_RIGHT_CONTIG
:
2458 * These cases are all impossible.
2463 /* update reverse mappings */
2464 xfs_rmap_convert_extent(mp
, tp
, ip
, whichfork
, new);
2466 /* convert to a btree if necessary */
2467 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
2468 int tmp_logflags
; /* partial log flag return val */
2470 ASSERT(cur
== NULL
);
2471 error
= xfs_bmap_extents_to_btree(tp
, ip
, &cur
, 0,
2472 &tmp_logflags
, whichfork
);
2473 *logflagsp
|= tmp_logflags
;
2478 /* clear out the allocated field, done with it now in any case. */
2480 cur
->bc_private
.b
.allocated
= 0;
2484 xfs_bmap_check_leaf_extents(*curp
, ip
, whichfork
);
2494 * Convert a hole to a delayed allocation.
2497 xfs_bmap_add_extent_hole_delay(
2498 xfs_inode_t
*ip
, /* incore inode pointer */
2500 struct xfs_iext_cursor
*icur
,
2501 xfs_bmbt_irec_t
*new) /* new data to add to file extents */
2503 struct xfs_ifork
*ifp
; /* inode fork pointer */
2504 xfs_bmbt_irec_t left
; /* left neighbor extent entry */
2505 xfs_filblks_t newlen
=0; /* new indirect size */
2506 xfs_filblks_t oldlen
=0; /* old indirect size */
2507 xfs_bmbt_irec_t right
; /* right neighbor extent entry */
2508 int state
= xfs_bmap_fork_to_state(whichfork
);
2509 xfs_filblks_t temp
; /* temp for indirect calculations */
2511 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
2512 ASSERT(isnullstartblock(new->br_startblock
));
2515 * Check and set flags if this segment has a left neighbor
2517 if (xfs_iext_peek_prev_extent(ifp
, icur
, &left
)) {
2518 state
|= BMAP_LEFT_VALID
;
2519 if (isnullstartblock(left
.br_startblock
))
2520 state
|= BMAP_LEFT_DELAY
;
2524 * Check and set flags if the current (right) segment exists.
2525 * If it doesn't exist, we're converting the hole at end-of-file.
2527 if (xfs_iext_get_extent(ifp
, icur
, &right
)) {
2528 state
|= BMAP_RIGHT_VALID
;
2529 if (isnullstartblock(right
.br_startblock
))
2530 state
|= BMAP_RIGHT_DELAY
;
2534 * Set contiguity flags on the left and right neighbors.
2535 * Don't let extents get too large, even if the pieces are contiguous.
2537 if ((state
& BMAP_LEFT_VALID
) && (state
& BMAP_LEFT_DELAY
) &&
2538 left
.br_startoff
+ left
.br_blockcount
== new->br_startoff
&&
2539 left
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
2540 state
|= BMAP_LEFT_CONTIG
;
2542 if ((state
& BMAP_RIGHT_VALID
) && (state
& BMAP_RIGHT_DELAY
) &&
2543 new->br_startoff
+ new->br_blockcount
== right
.br_startoff
&&
2544 new->br_blockcount
+ right
.br_blockcount
<= MAXEXTLEN
&&
2545 (!(state
& BMAP_LEFT_CONTIG
) ||
2546 (left
.br_blockcount
+ new->br_blockcount
+
2547 right
.br_blockcount
<= MAXEXTLEN
)))
2548 state
|= BMAP_RIGHT_CONTIG
;
2551 * Switch out based on the contiguity flags.
2553 switch (state
& (BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
)) {
2554 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2556 * New allocation is contiguous with delayed allocations
2557 * on the left and on the right.
2558 * Merge all three into a single extent record.
2560 temp
= left
.br_blockcount
+ new->br_blockcount
+
2561 right
.br_blockcount
;
2563 oldlen
= startblockval(left
.br_startblock
) +
2564 startblockval(new->br_startblock
) +
2565 startblockval(right
.br_startblock
);
2566 newlen
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
2568 left
.br_startblock
= nullstartblock(newlen
);
2569 left
.br_blockcount
= temp
;
2571 xfs_iext_remove(ip
, icur
, state
);
2572 xfs_iext_prev(ifp
, icur
);
2573 xfs_iext_update_extent(ip
, state
, icur
, &left
);
2576 case BMAP_LEFT_CONTIG
:
2578 * New allocation is contiguous with a delayed allocation
2580 * Merge the new allocation with the left neighbor.
2582 temp
= left
.br_blockcount
+ new->br_blockcount
;
2584 oldlen
= startblockval(left
.br_startblock
) +
2585 startblockval(new->br_startblock
);
2586 newlen
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
2588 left
.br_blockcount
= temp
;
2589 left
.br_startblock
= nullstartblock(newlen
);
2591 xfs_iext_prev(ifp
, icur
);
2592 xfs_iext_update_extent(ip
, state
, icur
, &left
);
2595 case BMAP_RIGHT_CONTIG
:
2597 * New allocation is contiguous with a delayed allocation
2599 * Merge the new allocation with the right neighbor.
2601 temp
= new->br_blockcount
+ right
.br_blockcount
;
2602 oldlen
= startblockval(new->br_startblock
) +
2603 startblockval(right
.br_startblock
);
2604 newlen
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
2606 right
.br_startoff
= new->br_startoff
;
2607 right
.br_startblock
= nullstartblock(newlen
);
2608 right
.br_blockcount
= temp
;
2609 xfs_iext_update_extent(ip
, state
, icur
, &right
);
2614 * New allocation is not contiguous with another
2615 * delayed allocation.
2616 * Insert a new entry.
2618 oldlen
= newlen
= 0;
2619 xfs_iext_insert(ip
, icur
, new, state
);
2622 if (oldlen
!= newlen
) {
2623 ASSERT(oldlen
> newlen
);
2624 xfs_mod_fdblocks(ip
->i_mount
, (int64_t)(oldlen
- newlen
),
2627 * Nothing to do for disk quota accounting here.
2629 xfs_mod_delalloc(ip
->i_mount
, (int64_t)newlen
- oldlen
);
2634 * Convert a hole to a real allocation.
2636 STATIC
int /* error */
2637 xfs_bmap_add_extent_hole_real(
2638 struct xfs_trans
*tp
,
2639 struct xfs_inode
*ip
,
2641 struct xfs_iext_cursor
*icur
,
2642 struct xfs_btree_cur
**curp
,
2643 struct xfs_bmbt_irec
*new,
2647 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
2648 struct xfs_mount
*mp
= ip
->i_mount
;
2649 struct xfs_btree_cur
*cur
= *curp
;
2650 int error
; /* error return value */
2651 int i
; /* temp state */
2652 xfs_bmbt_irec_t left
; /* left neighbor extent entry */
2653 xfs_bmbt_irec_t right
; /* right neighbor extent entry */
2654 int rval
=0; /* return value (logging flags) */
2655 int state
= xfs_bmap_fork_to_state(whichfork
);
2656 struct xfs_bmbt_irec old
;
2658 ASSERT(!isnullstartblock(new->br_startblock
));
2659 ASSERT(!cur
|| !(cur
->bc_private
.b
.flags
& XFS_BTCUR_BPRV_WASDEL
));
2661 XFS_STATS_INC(mp
, xs_add_exlist
);
2664 * Check and set flags if this segment has a left neighbor.
2666 if (xfs_iext_peek_prev_extent(ifp
, icur
, &left
)) {
2667 state
|= BMAP_LEFT_VALID
;
2668 if (isnullstartblock(left
.br_startblock
))
2669 state
|= BMAP_LEFT_DELAY
;
2673 * Check and set flags if this segment has a current value.
2674 * Not true if we're inserting into the "hole" at eof.
2676 if (xfs_iext_get_extent(ifp
, icur
, &right
)) {
2677 state
|= BMAP_RIGHT_VALID
;
2678 if (isnullstartblock(right
.br_startblock
))
2679 state
|= BMAP_RIGHT_DELAY
;
2683 * We're inserting a real allocation between "left" and "right".
2684 * Set the contiguity flags. Don't let extents get too large.
2686 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
2687 left
.br_startoff
+ left
.br_blockcount
== new->br_startoff
&&
2688 left
.br_startblock
+ left
.br_blockcount
== new->br_startblock
&&
2689 left
.br_state
== new->br_state
&&
2690 left
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
2691 state
|= BMAP_LEFT_CONTIG
;
2693 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
2694 new->br_startoff
+ new->br_blockcount
== right
.br_startoff
&&
2695 new->br_startblock
+ new->br_blockcount
== right
.br_startblock
&&
2696 new->br_state
== right
.br_state
&&
2697 new->br_blockcount
+ right
.br_blockcount
<= MAXEXTLEN
&&
2698 (!(state
& BMAP_LEFT_CONTIG
) ||
2699 left
.br_blockcount
+ new->br_blockcount
+
2700 right
.br_blockcount
<= MAXEXTLEN
))
2701 state
|= BMAP_RIGHT_CONTIG
;
2705 * Select which case we're in here, and implement it.
2707 switch (state
& (BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
)) {
2708 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2710 * New allocation is contiguous with real allocations on the
2711 * left and on the right.
2712 * Merge all three into a single extent record.
2714 left
.br_blockcount
+= new->br_blockcount
+ right
.br_blockcount
;
2716 xfs_iext_remove(ip
, icur
, state
);
2717 xfs_iext_prev(ifp
, icur
);
2718 xfs_iext_update_extent(ip
, state
, icur
, &left
);
2720 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2721 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
2723 rval
= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
2725 rval
= XFS_ILOG_CORE
;
2726 error
= xfs_bmbt_lookup_eq(cur
, &right
, &i
);
2729 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2730 error
= xfs_btree_delete(cur
, &i
);
2733 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2734 error
= xfs_btree_decrement(cur
, 0, &i
);
2737 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2738 error
= xfs_bmbt_update(cur
, &left
);
2744 case BMAP_LEFT_CONTIG
:
2746 * New allocation is contiguous with a real allocation
2748 * Merge the new allocation with the left neighbor.
2751 left
.br_blockcount
+= new->br_blockcount
;
2753 xfs_iext_prev(ifp
, icur
);
2754 xfs_iext_update_extent(ip
, state
, icur
, &left
);
2757 rval
= xfs_ilog_fext(whichfork
);
2760 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2763 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2764 error
= xfs_bmbt_update(cur
, &left
);
2770 case BMAP_RIGHT_CONTIG
:
2772 * New allocation is contiguous with a real allocation
2774 * Merge the new allocation with the right neighbor.
2778 right
.br_startoff
= new->br_startoff
;
2779 right
.br_startblock
= new->br_startblock
;
2780 right
.br_blockcount
+= new->br_blockcount
;
2781 xfs_iext_update_extent(ip
, state
, icur
, &right
);
2784 rval
= xfs_ilog_fext(whichfork
);
2787 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2790 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2791 error
= xfs_bmbt_update(cur
, &right
);
2799 * New allocation is not contiguous with another
2801 * Insert a new entry.
2803 xfs_iext_insert(ip
, icur
, new, state
);
2804 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2805 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
2807 rval
= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
2809 rval
= XFS_ILOG_CORE
;
2810 error
= xfs_bmbt_lookup_eq(cur
, new, &i
);
2813 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
2814 error
= xfs_btree_insert(cur
, &i
);
2817 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2822 /* add reverse mapping unless caller opted out */
2823 if (!(flags
& XFS_BMAPI_NORMAP
))
2824 xfs_rmap_map_extent(tp
, ip
, whichfork
, new);
2826 /* convert to a btree if necessary */
2827 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
2828 int tmp_logflags
; /* partial log flag return val */
2830 ASSERT(cur
== NULL
);
2831 error
= xfs_bmap_extents_to_btree(tp
, ip
, curp
, 0,
2832 &tmp_logflags
, whichfork
);
2833 *logflagsp
|= tmp_logflags
;
2839 /* clear out the allocated field, done with it now in any case. */
2841 cur
->bc_private
.b
.allocated
= 0;
2843 xfs_bmap_check_leaf_extents(cur
, ip
, whichfork
);
2850 * Functions used in the extent read, allocate and remove paths
2854 * Adjust the size of the new extent based on di_extsize and rt extsize.
2857 xfs_bmap_extsize_align(
2859 xfs_bmbt_irec_t
*gotp
, /* next extent pointer */
2860 xfs_bmbt_irec_t
*prevp
, /* previous extent pointer */
2861 xfs_extlen_t extsz
, /* align to this extent size */
2862 int rt
, /* is this a realtime inode? */
2863 int eof
, /* is extent at end-of-file? */
2864 int delay
, /* creating delalloc extent? */
2865 int convert
, /* overwriting unwritten extent? */
2866 xfs_fileoff_t
*offp
, /* in/out: aligned offset */
2867 xfs_extlen_t
*lenp
) /* in/out: aligned length */
2869 xfs_fileoff_t orig_off
; /* original offset */
2870 xfs_extlen_t orig_alen
; /* original length */
2871 xfs_fileoff_t orig_end
; /* original off+len */
2872 xfs_fileoff_t nexto
; /* next file offset */
2873 xfs_fileoff_t prevo
; /* previous file offset */
2874 xfs_fileoff_t align_off
; /* temp for offset */
2875 xfs_extlen_t align_alen
; /* temp for length */
2876 xfs_extlen_t temp
; /* temp for calculations */
2881 orig_off
= align_off
= *offp
;
2882 orig_alen
= align_alen
= *lenp
;
2883 orig_end
= orig_off
+ orig_alen
;
2886 * If this request overlaps an existing extent, then don't
2887 * attempt to perform any additional alignment.
2889 if (!delay
&& !eof
&&
2890 (orig_off
>= gotp
->br_startoff
) &&
2891 (orig_end
<= gotp
->br_startoff
+ gotp
->br_blockcount
)) {
2896 * If the file offset is unaligned vs. the extent size
2897 * we need to align it. This will be possible unless
2898 * the file was previously written with a kernel that didn't
2899 * perform this alignment, or if a truncate shot us in the
2902 div_u64_rem(orig_off
, extsz
, &temp
);
2908 /* Same adjustment for the end of the requested area. */
2909 temp
= (align_alen
% extsz
);
2911 align_alen
+= extsz
- temp
;
2914 * For large extent hint sizes, the aligned extent might be larger than
2915 * MAXEXTLEN. In that case, reduce the size by an extsz so that it pulls
2916 * the length back under MAXEXTLEN. The outer allocation loops handle
2917 * short allocation just fine, so it is safe to do this. We only want to
2918 * do it when we are forced to, though, because it means more allocation
2919 * operations are required.
2921 while (align_alen
> MAXEXTLEN
)
2922 align_alen
-= extsz
;
2923 ASSERT(align_alen
<= MAXEXTLEN
);
2926 * If the previous block overlaps with this proposed allocation
2927 * then move the start forward without adjusting the length.
2929 if (prevp
->br_startoff
!= NULLFILEOFF
) {
2930 if (prevp
->br_startblock
== HOLESTARTBLOCK
)
2931 prevo
= prevp
->br_startoff
;
2933 prevo
= prevp
->br_startoff
+ prevp
->br_blockcount
;
2936 if (align_off
!= orig_off
&& align_off
< prevo
)
2939 * If the next block overlaps with this proposed allocation
2940 * then move the start back without adjusting the length,
2941 * but not before offset 0.
2942 * This may of course make the start overlap previous block,
2943 * and if we hit the offset 0 limit then the next block
2944 * can still overlap too.
2946 if (!eof
&& gotp
->br_startoff
!= NULLFILEOFF
) {
2947 if ((delay
&& gotp
->br_startblock
== HOLESTARTBLOCK
) ||
2948 (!delay
&& gotp
->br_startblock
== DELAYSTARTBLOCK
))
2949 nexto
= gotp
->br_startoff
+ gotp
->br_blockcount
;
2951 nexto
= gotp
->br_startoff
;
2953 nexto
= NULLFILEOFF
;
2955 align_off
+ align_alen
!= orig_end
&&
2956 align_off
+ align_alen
> nexto
)
2957 align_off
= nexto
> align_alen
? nexto
- align_alen
: 0;
2959 * If we're now overlapping the next or previous extent that
2960 * means we can't fit an extsz piece in this hole. Just move
2961 * the start forward to the first valid spot and set
2962 * the length so we hit the end.
2964 if (align_off
!= orig_off
&& align_off
< prevo
)
2966 if (align_off
+ align_alen
!= orig_end
&&
2967 align_off
+ align_alen
> nexto
&&
2968 nexto
!= NULLFILEOFF
) {
2969 ASSERT(nexto
> prevo
);
2970 align_alen
= nexto
- align_off
;
2974 * If realtime, and the result isn't a multiple of the realtime
2975 * extent size we need to remove blocks until it is.
2977 if (rt
&& (temp
= (align_alen
% mp
->m_sb
.sb_rextsize
))) {
2979 * We're not covering the original request, or
2980 * we won't be able to once we fix the length.
2982 if (orig_off
< align_off
||
2983 orig_end
> align_off
+ align_alen
||
2984 align_alen
- temp
< orig_alen
)
2987 * Try to fix it by moving the start up.
2989 if (align_off
+ temp
<= orig_off
) {
2994 * Try to fix it by moving the end in.
2996 else if (align_off
+ align_alen
- temp
>= orig_end
)
2999 * Set the start to the minimum then trim the length.
3002 align_alen
-= orig_off
- align_off
;
3003 align_off
= orig_off
;
3004 align_alen
-= align_alen
% mp
->m_sb
.sb_rextsize
;
3007 * Result doesn't cover the request, fail it.
3009 if (orig_off
< align_off
|| orig_end
> align_off
+ align_alen
)
3012 ASSERT(orig_off
>= align_off
);
3013 /* see MAXEXTLEN handling above */
3014 ASSERT(orig_end
<= align_off
+ align_alen
||
3015 align_alen
+ extsz
> MAXEXTLEN
);
3019 if (!eof
&& gotp
->br_startoff
!= NULLFILEOFF
)
3020 ASSERT(align_off
+ align_alen
<= gotp
->br_startoff
);
3021 if (prevp
->br_startoff
!= NULLFILEOFF
)
3022 ASSERT(align_off
>= prevp
->br_startoff
+ prevp
->br_blockcount
);
3030 #define XFS_ALLOC_GAP_UNITS 4
3034 struct xfs_bmalloca
*ap
) /* bmap alloc argument struct */
3036 xfs_fsblock_t adjust
; /* adjustment to block numbers */
3037 xfs_agnumber_t fb_agno
; /* ag number of ap->firstblock */
3038 xfs_mount_t
*mp
; /* mount point structure */
3039 int nullfb
; /* true if ap->firstblock isn't set */
3040 int rt
; /* true if inode is realtime */
3042 #define ISVALID(x,y) \
3044 (x) < mp->m_sb.sb_rblocks : \
3045 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
3046 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
3047 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
3049 mp
= ap
->ip
->i_mount
;
3050 nullfb
= ap
->tp
->t_firstblock
== NULLFSBLOCK
;
3051 rt
= XFS_IS_REALTIME_INODE(ap
->ip
) &&
3052 xfs_alloc_is_userdata(ap
->datatype
);
3053 fb_agno
= nullfb
? NULLAGNUMBER
: XFS_FSB_TO_AGNO(mp
,
3054 ap
->tp
->t_firstblock
);
3056 * If allocating at eof, and there's a previous real block,
3057 * try to use its last block as our starting point.
3059 if (ap
->eof
&& ap
->prev
.br_startoff
!= NULLFILEOFF
&&
3060 !isnullstartblock(ap
->prev
.br_startblock
) &&
3061 ISVALID(ap
->prev
.br_startblock
+ ap
->prev
.br_blockcount
,
3062 ap
->prev
.br_startblock
)) {
3063 ap
->blkno
= ap
->prev
.br_startblock
+ ap
->prev
.br_blockcount
;
3065 * Adjust for the gap between prevp and us.
3067 adjust
= ap
->offset
-
3068 (ap
->prev
.br_startoff
+ ap
->prev
.br_blockcount
);
3070 ISVALID(ap
->blkno
+ adjust
, ap
->prev
.br_startblock
))
3071 ap
->blkno
+= adjust
;
3074 * If not at eof, then compare the two neighbor blocks.
3075 * Figure out whether either one gives us a good starting point,
3076 * and pick the better one.
3078 else if (!ap
->eof
) {
3079 xfs_fsblock_t gotbno
; /* right side block number */
3080 xfs_fsblock_t gotdiff
=0; /* right side difference */
3081 xfs_fsblock_t prevbno
; /* left side block number */
3082 xfs_fsblock_t prevdiff
=0; /* left side difference */
3085 * If there's a previous (left) block, select a requested
3086 * start block based on it.
3088 if (ap
->prev
.br_startoff
!= NULLFILEOFF
&&
3089 !isnullstartblock(ap
->prev
.br_startblock
) &&
3090 (prevbno
= ap
->prev
.br_startblock
+
3091 ap
->prev
.br_blockcount
) &&
3092 ISVALID(prevbno
, ap
->prev
.br_startblock
)) {
3094 * Calculate gap to end of previous block.
3096 adjust
= prevdiff
= ap
->offset
-
3097 (ap
->prev
.br_startoff
+
3098 ap
->prev
.br_blockcount
);
3100 * Figure the startblock based on the previous block's
3101 * end and the gap size.
3103 * If the gap is large relative to the piece we're
3104 * allocating, or using it gives us an invalid block
3105 * number, then just use the end of the previous block.
3107 if (prevdiff
<= XFS_ALLOC_GAP_UNITS
* ap
->length
&&
3108 ISVALID(prevbno
+ prevdiff
,
3109 ap
->prev
.br_startblock
))
3114 * If the firstblock forbids it, can't use it,
3117 if (!rt
&& !nullfb
&&
3118 XFS_FSB_TO_AGNO(mp
, prevbno
) != fb_agno
)
3119 prevbno
= NULLFSBLOCK
;
3122 * No previous block or can't follow it, just default.
3125 prevbno
= NULLFSBLOCK
;
3127 * If there's a following (right) block, select a requested
3128 * start block based on it.
3130 if (!isnullstartblock(ap
->got
.br_startblock
)) {
3132 * Calculate gap to start of next block.
3134 adjust
= gotdiff
= ap
->got
.br_startoff
- ap
->offset
;
3136 * Figure the startblock based on the next block's
3137 * start and the gap size.
3139 gotbno
= ap
->got
.br_startblock
;
3142 * If the gap is large relative to the piece we're
3143 * allocating, or using it gives us an invalid block
3144 * number, then just use the start of the next block
3145 * offset by our length.
3147 if (gotdiff
<= XFS_ALLOC_GAP_UNITS
* ap
->length
&&
3148 ISVALID(gotbno
- gotdiff
, gotbno
))
3150 else if (ISVALID(gotbno
- ap
->length
, gotbno
)) {
3151 gotbno
-= ap
->length
;
3152 gotdiff
+= adjust
- ap
->length
;
3156 * If the firstblock forbids it, can't use it,
3159 if (!rt
&& !nullfb
&&
3160 XFS_FSB_TO_AGNO(mp
, gotbno
) != fb_agno
)
3161 gotbno
= NULLFSBLOCK
;
3164 * No next block, just default.
3167 gotbno
= NULLFSBLOCK
;
3169 * If both valid, pick the better one, else the only good
3170 * one, else ap->blkno is already set (to 0 or the inode block).
3172 if (prevbno
!= NULLFSBLOCK
&& gotbno
!= NULLFSBLOCK
)
3173 ap
->blkno
= prevdiff
<= gotdiff
? prevbno
: gotbno
;
3174 else if (prevbno
!= NULLFSBLOCK
)
3175 ap
->blkno
= prevbno
;
3176 else if (gotbno
!= NULLFSBLOCK
)
3183 xfs_bmap_longest_free_extent(
3184 struct xfs_trans
*tp
,
3189 struct xfs_mount
*mp
= tp
->t_mountp
;
3190 struct xfs_perag
*pag
;
3191 xfs_extlen_t longest
;
3194 pag
= xfs_perag_get(mp
, ag
);
3195 if (!pag
->pagf_init
) {
3196 error
= xfs_alloc_pagf_init(mp
, tp
, ag
, XFS_ALLOC_FLAG_TRYLOCK
);
3200 if (!pag
->pagf_init
) {
3206 longest
= xfs_alloc_longest_free_extent(pag
,
3207 xfs_alloc_min_freelist(mp
, pag
),
3208 xfs_ag_resv_needed(pag
, XFS_AG_RESV_NONE
));
3209 if (*blen
< longest
)
3218 xfs_bmap_select_minlen(
3219 struct xfs_bmalloca
*ap
,
3220 struct xfs_alloc_arg
*args
,
3224 if (notinit
|| *blen
< ap
->minlen
) {
3226 * Since we did a BUF_TRYLOCK above, it is possible that
3227 * there is space for this request.
3229 args
->minlen
= ap
->minlen
;
3230 } else if (*blen
< args
->maxlen
) {
3232 * If the best seen length is less than the request length,
3233 * use the best as the minimum.
3235 args
->minlen
= *blen
;
3238 * Otherwise we've seen an extent as big as maxlen, use that
3241 args
->minlen
= args
->maxlen
;
3246 xfs_bmap_btalloc_nullfb(
3247 struct xfs_bmalloca
*ap
,
3248 struct xfs_alloc_arg
*args
,
3251 struct xfs_mount
*mp
= ap
->ip
->i_mount
;
3252 xfs_agnumber_t ag
, startag
;
3256 args
->type
= XFS_ALLOCTYPE_START_BNO
;
3257 args
->total
= ap
->total
;
3259 startag
= ag
= XFS_FSB_TO_AGNO(mp
, args
->fsbno
);
3260 if (startag
== NULLAGNUMBER
)
3263 while (*blen
< args
->maxlen
) {
3264 error
= xfs_bmap_longest_free_extent(args
->tp
, ag
, blen
,
3269 if (++ag
== mp
->m_sb
.sb_agcount
)
3275 xfs_bmap_select_minlen(ap
, args
, blen
, notinit
);
3280 xfs_bmap_btalloc_filestreams(
3281 struct xfs_bmalloca
*ap
,
3282 struct xfs_alloc_arg
*args
,
3285 struct xfs_mount
*mp
= ap
->ip
->i_mount
;
3290 args
->type
= XFS_ALLOCTYPE_NEAR_BNO
;
3291 args
->total
= ap
->total
;
3293 ag
= XFS_FSB_TO_AGNO(mp
, args
->fsbno
);
3294 if (ag
== NULLAGNUMBER
)
3297 error
= xfs_bmap_longest_free_extent(args
->tp
, ag
, blen
, ¬init
);
3301 if (*blen
< args
->maxlen
) {
3302 error
= xfs_filestream_new_ag(ap
, &ag
);
3306 error
= xfs_bmap_longest_free_extent(args
->tp
, ag
, blen
,
3313 xfs_bmap_select_minlen(ap
, args
, blen
, notinit
);
3316 * Set the failure fallback case to look in the selected AG as stream
3319 ap
->blkno
= args
->fsbno
= XFS_AGB_TO_FSB(mp
, ag
, 0);
3323 /* Update all inode and quota accounting for the allocation we just did. */
3325 xfs_bmap_btalloc_accounting(
3326 struct xfs_bmalloca
*ap
,
3327 struct xfs_alloc_arg
*args
)
3329 if (ap
->flags
& XFS_BMAPI_COWFORK
) {
3331 * COW fork blocks are in-core only and thus are treated as
3332 * in-core quota reservation (like delalloc blocks) even when
3333 * converted to real blocks. The quota reservation is not
3334 * accounted to disk until blocks are remapped to the data
3335 * fork. So if these blocks were previously delalloc, we
3336 * already have quota reservation and there's nothing to do
3340 xfs_mod_delalloc(ap
->ip
->i_mount
, -(int64_t)args
->len
);
3345 * Otherwise, we've allocated blocks in a hole. The transaction
3346 * has acquired in-core quota reservation for this extent.
3347 * Rather than account these as real blocks, however, we reduce
3348 * the transaction quota reservation based on the allocation.
3349 * This essentially transfers the transaction quota reservation
3350 * to that of a delalloc extent.
3352 ap
->ip
->i_delayed_blks
+= args
->len
;
3353 xfs_trans_mod_dquot_byino(ap
->tp
, ap
->ip
, XFS_TRANS_DQ_RES_BLKS
,
3358 /* data/attr fork only */
3359 ap
->ip
->i_d
.di_nblocks
+= args
->len
;
3360 xfs_trans_log_inode(ap
->tp
, ap
->ip
, XFS_ILOG_CORE
);
3362 ap
->ip
->i_delayed_blks
-= args
->len
;
3363 xfs_mod_delalloc(ap
->ip
->i_mount
, -(int64_t)args
->len
);
3365 xfs_trans_mod_dquot_byino(ap
->tp
, ap
->ip
,
3366 ap
->wasdel
? XFS_TRANS_DQ_DELBCOUNT
: XFS_TRANS_DQ_BCOUNT
,
3372 struct xfs_bmalloca
*ap
) /* bmap alloc argument struct */
3374 xfs_mount_t
*mp
; /* mount point structure */
3375 xfs_alloctype_t atype
= 0; /* type for allocation routines */
3376 xfs_extlen_t align
= 0; /* minimum allocation alignment */
3377 xfs_agnumber_t fb_agno
; /* ag number of ap->firstblock */
3379 xfs_alloc_arg_t args
;
3380 xfs_fileoff_t orig_offset
;
3381 xfs_extlen_t orig_length
;
3383 xfs_extlen_t nextminlen
= 0;
3384 int nullfb
; /* true if ap->firstblock isn't set */
3391 orig_offset
= ap
->offset
;
3392 orig_length
= ap
->length
;
3394 mp
= ap
->ip
->i_mount
;
3396 /* stripe alignment for allocation is determined by mount parameters */
3398 if (mp
->m_swidth
&& (mp
->m_flags
& XFS_MOUNT_SWALLOC
))
3399 stripe_align
= mp
->m_swidth
;
3400 else if (mp
->m_dalign
)
3401 stripe_align
= mp
->m_dalign
;
3403 if (ap
->flags
& XFS_BMAPI_COWFORK
)
3404 align
= xfs_get_cowextsz_hint(ap
->ip
);
3405 else if (xfs_alloc_is_userdata(ap
->datatype
))
3406 align
= xfs_get_extsz_hint(ap
->ip
);
3408 error
= xfs_bmap_extsize_align(mp
, &ap
->got
, &ap
->prev
,
3409 align
, 0, ap
->eof
, 0, ap
->conv
,
3410 &ap
->offset
, &ap
->length
);
3416 nullfb
= ap
->tp
->t_firstblock
== NULLFSBLOCK
;
3417 fb_agno
= nullfb
? NULLAGNUMBER
: XFS_FSB_TO_AGNO(mp
,
3418 ap
->tp
->t_firstblock
);
3420 if (xfs_alloc_is_userdata(ap
->datatype
) &&
3421 xfs_inode_is_filestream(ap
->ip
)) {
3422 ag
= xfs_filestream_lookup_ag(ap
->ip
);
3423 ag
= (ag
!= NULLAGNUMBER
) ? ag
: 0;
3424 ap
->blkno
= XFS_AGB_TO_FSB(mp
, ag
, 0);
3426 ap
->blkno
= XFS_INO_TO_FSB(mp
, ap
->ip
->i_ino
);
3429 ap
->blkno
= ap
->tp
->t_firstblock
;
3431 xfs_bmap_adjacent(ap
);
3434 * If allowed, use ap->blkno; otherwise must use firstblock since
3435 * it's in the right allocation group.
3437 if (nullfb
|| XFS_FSB_TO_AGNO(mp
, ap
->blkno
) == fb_agno
)
3440 ap
->blkno
= ap
->tp
->t_firstblock
;
3442 * Normal allocation, done through xfs_alloc_vextent.
3444 tryagain
= isaligned
= 0;
3445 memset(&args
, 0, sizeof(args
));
3448 args
.fsbno
= ap
->blkno
;
3449 args
.oinfo
= XFS_RMAP_OINFO_SKIP_UPDATE
;
3451 /* Trim the allocation back to the maximum an AG can fit. */
3452 args
.maxlen
= min(ap
->length
, mp
->m_ag_max_usable
);
3456 * Search for an allocation group with a single extent large
3457 * enough for the request. If one isn't found, then adjust
3458 * the minimum allocation size to the largest space found.
3460 if (xfs_alloc_is_userdata(ap
->datatype
) &&
3461 xfs_inode_is_filestream(ap
->ip
))
3462 error
= xfs_bmap_btalloc_filestreams(ap
, &args
, &blen
);
3464 error
= xfs_bmap_btalloc_nullfb(ap
, &args
, &blen
);
3467 } else if (ap
->tp
->t_flags
& XFS_TRANS_LOWMODE
) {
3468 if (xfs_inode_is_filestream(ap
->ip
))
3469 args
.type
= XFS_ALLOCTYPE_FIRST_AG
;
3471 args
.type
= XFS_ALLOCTYPE_START_BNO
;
3472 args
.total
= args
.minlen
= ap
->minlen
;
3474 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
3475 args
.total
= ap
->total
;
3476 args
.minlen
= ap
->minlen
;
3478 /* apply extent size hints if obtained earlier */
3481 div_u64_rem(ap
->offset
, args
.prod
, &args
.mod
);
3483 args
.mod
= args
.prod
- args
.mod
;
3484 } else if (mp
->m_sb
.sb_blocksize
>= PAGE_SIZE
) {
3488 args
.prod
= PAGE_SIZE
>> mp
->m_sb
.sb_blocklog
;
3489 div_u64_rem(ap
->offset
, args
.prod
, &args
.mod
);
3491 args
.mod
= args
.prod
- args
.mod
;
3494 * If we are not low on available data blocks, and the
3495 * underlying logical volume manager is a stripe, and
3496 * the file offset is zero then try to allocate data
3497 * blocks on stripe unit boundary.
3498 * NOTE: ap->aeof is only set if the allocation length
3499 * is >= the stripe unit and the allocation offset is
3500 * at the end of file.
3502 if (!(ap
->tp
->t_flags
& XFS_TRANS_LOWMODE
) && ap
->aeof
) {
3504 args
.alignment
= stripe_align
;
3508 * Adjust for alignment
3510 if (blen
> args
.alignment
&& blen
<= args
.maxlen
)
3511 args
.minlen
= blen
- args
.alignment
;
3512 args
.minalignslop
= 0;
3515 * First try an exact bno allocation.
3516 * If it fails then do a near or start bno
3517 * allocation with alignment turned on.
3521 args
.type
= XFS_ALLOCTYPE_THIS_BNO
;
3524 * Compute the minlen+alignment for the
3525 * next case. Set slop so that the value
3526 * of minlen+alignment+slop doesn't go up
3527 * between the calls.
3529 if (blen
> stripe_align
&& blen
<= args
.maxlen
)
3530 nextminlen
= blen
- stripe_align
;
3532 nextminlen
= args
.minlen
;
3533 if (nextminlen
+ stripe_align
> args
.minlen
+ 1)
3535 nextminlen
+ stripe_align
-
3538 args
.minalignslop
= 0;
3542 args
.minalignslop
= 0;
3544 args
.minleft
= ap
->minleft
;
3545 args
.wasdel
= ap
->wasdel
;
3546 args
.resv
= XFS_AG_RESV_NONE
;
3547 args
.datatype
= ap
->datatype
;
3548 if (ap
->datatype
& XFS_ALLOC_USERDATA_ZERO
)
3551 error
= xfs_alloc_vextent(&args
);
3555 if (tryagain
&& args
.fsbno
== NULLFSBLOCK
) {
3557 * Exact allocation failed. Now try with alignment
3561 args
.fsbno
= ap
->blkno
;
3562 args
.alignment
= stripe_align
;
3563 args
.minlen
= nextminlen
;
3564 args
.minalignslop
= 0;
3566 if ((error
= xfs_alloc_vextent(&args
)))
3569 if (isaligned
&& args
.fsbno
== NULLFSBLOCK
) {
3571 * allocation failed, so turn off alignment and
3575 args
.fsbno
= ap
->blkno
;
3577 if ((error
= xfs_alloc_vextent(&args
)))
3580 if (args
.fsbno
== NULLFSBLOCK
&& nullfb
&&
3581 args
.minlen
> ap
->minlen
) {
3582 args
.minlen
= ap
->minlen
;
3583 args
.type
= XFS_ALLOCTYPE_START_BNO
;
3584 args
.fsbno
= ap
->blkno
;
3585 if ((error
= xfs_alloc_vextent(&args
)))
3588 if (args
.fsbno
== NULLFSBLOCK
&& nullfb
) {
3590 args
.type
= XFS_ALLOCTYPE_FIRST_AG
;
3591 args
.total
= ap
->minlen
;
3592 if ((error
= xfs_alloc_vextent(&args
)))
3594 ap
->tp
->t_flags
|= XFS_TRANS_LOWMODE
;
3596 if (args
.fsbno
!= NULLFSBLOCK
) {
3598 * check the allocation happened at the same or higher AG than
3599 * the first block that was allocated.
3601 ASSERT(ap
->tp
->t_firstblock
== NULLFSBLOCK
||
3602 XFS_FSB_TO_AGNO(mp
, ap
->tp
->t_firstblock
) <=
3603 XFS_FSB_TO_AGNO(mp
, args
.fsbno
));
3605 ap
->blkno
= args
.fsbno
;
3606 if (ap
->tp
->t_firstblock
== NULLFSBLOCK
)
3607 ap
->tp
->t_firstblock
= args
.fsbno
;
3608 ASSERT(nullfb
|| fb_agno
<= args
.agno
);
3609 ap
->length
= args
.len
;
3611 * If the extent size hint is active, we tried to round the
3612 * caller's allocation request offset down to extsz and the
3613 * length up to another extsz boundary. If we found a free
3614 * extent we mapped it in starting at this new offset. If the
3615 * newly mapped space isn't long enough to cover any of the
3616 * range of offsets that was originally requested, move the
3617 * mapping up so that we can fill as much of the caller's
3618 * original request as possible. Free space is apparently
3619 * very fragmented so we're unlikely to be able to satisfy the
3622 if (ap
->length
<= orig_length
)
3623 ap
->offset
= orig_offset
;
3624 else if (ap
->offset
+ ap
->length
< orig_offset
+ orig_length
)
3625 ap
->offset
= orig_offset
+ orig_length
- ap
->length
;
3626 xfs_bmap_btalloc_accounting(ap
, &args
);
3628 ap
->blkno
= NULLFSBLOCK
;
3635 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
3636 * It figures out where to ask the underlying allocator to put the new extent.
3640 struct xfs_bmalloca
*ap
) /* bmap alloc argument struct */
3642 if (XFS_IS_REALTIME_INODE(ap
->ip
) &&
3643 xfs_alloc_is_userdata(ap
->datatype
))
3644 return xfs_bmap_rtalloc(ap
);
3645 return xfs_bmap_btalloc(ap
);
3648 /* Trim extent to fit a logical block range. */
3651 struct xfs_bmbt_irec
*irec
,
3655 xfs_fileoff_t distance
;
3656 xfs_fileoff_t end
= bno
+ len
;
3658 if (irec
->br_startoff
+ irec
->br_blockcount
<= bno
||
3659 irec
->br_startoff
>= end
) {
3660 irec
->br_blockcount
= 0;
3664 if (irec
->br_startoff
< bno
) {
3665 distance
= bno
- irec
->br_startoff
;
3666 if (isnullstartblock(irec
->br_startblock
))
3667 irec
->br_startblock
= DELAYSTARTBLOCK
;
3668 if (irec
->br_startblock
!= DELAYSTARTBLOCK
&&
3669 irec
->br_startblock
!= HOLESTARTBLOCK
)
3670 irec
->br_startblock
+= distance
;
3671 irec
->br_startoff
+= distance
;
3672 irec
->br_blockcount
-= distance
;
3675 if (end
< irec
->br_startoff
+ irec
->br_blockcount
) {
3676 distance
= irec
->br_startoff
+ irec
->br_blockcount
- end
;
3677 irec
->br_blockcount
-= distance
;
3682 * Trim the returned map to the required bounds
3686 struct xfs_bmbt_irec
*mval
,
3687 struct xfs_bmbt_irec
*got
,
3695 if ((flags
& XFS_BMAPI_ENTIRE
) ||
3696 got
->br_startoff
+ got
->br_blockcount
<= obno
) {
3698 if (isnullstartblock(got
->br_startblock
))
3699 mval
->br_startblock
= DELAYSTARTBLOCK
;
3705 ASSERT((*bno
>= obno
) || (n
== 0));
3707 mval
->br_startoff
= *bno
;
3708 if (isnullstartblock(got
->br_startblock
))
3709 mval
->br_startblock
= DELAYSTARTBLOCK
;
3711 mval
->br_startblock
= got
->br_startblock
+
3712 (*bno
- got
->br_startoff
);
3714 * Return the minimum of what we got and what we asked for for
3715 * the length. We can use the len variable here because it is
3716 * modified below and we could have been there before coming
3717 * here if the first part of the allocation didn't overlap what
3720 mval
->br_blockcount
= XFS_FILBLKS_MIN(end
- *bno
,
3721 got
->br_blockcount
- (*bno
- got
->br_startoff
));
3722 mval
->br_state
= got
->br_state
;
3723 ASSERT(mval
->br_blockcount
<= len
);
3728 * Update and validate the extent map to return
3731 xfs_bmapi_update_map(
3732 struct xfs_bmbt_irec
**map
,
3740 xfs_bmbt_irec_t
*mval
= *map
;
3742 ASSERT((flags
& XFS_BMAPI_ENTIRE
) ||
3743 ((mval
->br_startoff
+ mval
->br_blockcount
) <= end
));
3744 ASSERT((flags
& XFS_BMAPI_ENTIRE
) || (mval
->br_blockcount
<= *len
) ||
3745 (mval
->br_startoff
< obno
));
3747 *bno
= mval
->br_startoff
+ mval
->br_blockcount
;
3749 if (*n
> 0 && mval
->br_startoff
== mval
[-1].br_startoff
) {
3750 /* update previous map with new information */
3751 ASSERT(mval
->br_startblock
== mval
[-1].br_startblock
);
3752 ASSERT(mval
->br_blockcount
> mval
[-1].br_blockcount
);
3753 ASSERT(mval
->br_state
== mval
[-1].br_state
);
3754 mval
[-1].br_blockcount
= mval
->br_blockcount
;
3755 mval
[-1].br_state
= mval
->br_state
;
3756 } else if (*n
> 0 && mval
->br_startblock
!= DELAYSTARTBLOCK
&&
3757 mval
[-1].br_startblock
!= DELAYSTARTBLOCK
&&
3758 mval
[-1].br_startblock
!= HOLESTARTBLOCK
&&
3759 mval
->br_startblock
== mval
[-1].br_startblock
+
3760 mval
[-1].br_blockcount
&&
3761 mval
[-1].br_state
== mval
->br_state
) {
3762 ASSERT(mval
->br_startoff
==
3763 mval
[-1].br_startoff
+ mval
[-1].br_blockcount
);
3764 mval
[-1].br_blockcount
+= mval
->br_blockcount
;
3765 } else if (*n
> 0 &&
3766 mval
->br_startblock
== DELAYSTARTBLOCK
&&
3767 mval
[-1].br_startblock
== DELAYSTARTBLOCK
&&
3768 mval
->br_startoff
==
3769 mval
[-1].br_startoff
+ mval
[-1].br_blockcount
) {
3770 mval
[-1].br_blockcount
+= mval
->br_blockcount
;
3771 mval
[-1].br_state
= mval
->br_state
;
3772 } else if (!((*n
== 0) &&
3773 ((mval
->br_startoff
+ mval
->br_blockcount
) <=
3782 * Map file blocks to filesystem blocks without allocation.
3786 struct xfs_inode
*ip
,
3789 struct xfs_bmbt_irec
*mval
,
3793 struct xfs_mount
*mp
= ip
->i_mount
;
3794 struct xfs_ifork
*ifp
;
3795 struct xfs_bmbt_irec got
;
3798 struct xfs_iext_cursor icur
;
3802 int whichfork
= xfs_bmapi_whichfork(flags
);
3805 ASSERT(!(flags
& ~(XFS_BMAPI_ATTRFORK
|XFS_BMAPI_ENTIRE
|
3806 XFS_BMAPI_COWFORK
)));
3807 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_SHARED
|XFS_ILOCK_EXCL
));
3809 if (unlikely(XFS_TEST_ERROR(
3810 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
3811 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
3812 mp
, XFS_ERRTAG_BMAPIFORMAT
))) {
3813 XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW
, mp
);
3814 return -EFSCORRUPTED
;
3817 if (XFS_FORCED_SHUTDOWN(mp
))
3820 XFS_STATS_INC(mp
, xs_blk_mapr
);
3822 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
3824 /* No CoW fork? Return a hole. */
3825 if (whichfork
== XFS_COW_FORK
) {
3826 mval
->br_startoff
= bno
;
3827 mval
->br_startblock
= HOLESTARTBLOCK
;
3828 mval
->br_blockcount
= len
;
3829 mval
->br_state
= XFS_EXT_NORM
;
3835 * A missing attr ifork implies that the inode says we're in
3836 * extents or btree format but failed to pass the inode fork
3837 * verifier while trying to load it. Treat that as a file
3841 xfs_alert(mp
, "%s: inode %llu missing fork %d",
3842 __func__
, ip
->i_ino
, whichfork
);
3844 return -EFSCORRUPTED
;
3847 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
3848 error
= xfs_iread_extents(NULL
, ip
, whichfork
);
3853 if (!xfs_iext_lookup_extent(ip
, ifp
, bno
, &icur
, &got
))
3858 while (bno
< end
&& n
< *nmap
) {
3859 /* Reading past eof, act as though there's a hole up to end. */
3861 got
.br_startoff
= end
;
3862 if (got
.br_startoff
> bno
) {
3863 /* Reading in a hole. */
3864 mval
->br_startoff
= bno
;
3865 mval
->br_startblock
= HOLESTARTBLOCK
;
3866 mval
->br_blockcount
=
3867 XFS_FILBLKS_MIN(len
, got
.br_startoff
- bno
);
3868 mval
->br_state
= XFS_EXT_NORM
;
3869 bno
+= mval
->br_blockcount
;
3870 len
-= mval
->br_blockcount
;
3876 /* set up the extent map to return. */
3877 xfs_bmapi_trim_map(mval
, &got
, &bno
, len
, obno
, end
, n
, flags
);
3878 xfs_bmapi_update_map(&mval
, &bno
, &len
, obno
, end
, &n
, flags
);
3880 /* If we're done, stop now. */
3881 if (bno
>= end
|| n
>= *nmap
)
3884 /* Else go on to the next record. */
3885 if (!xfs_iext_next_extent(ifp
, &icur
, &got
))
3893 * Add a delayed allocation extent to an inode. Blocks are reserved from the
3894 * global pool and the extent inserted into the inode in-core extent tree.
3896 * On entry, got refers to the first extent beyond the offset of the extent to
3897 * allocate or eof is specified if no such extent exists. On return, got refers
3898 * to the extent record that was inserted to the inode fork.
3900 * Note that the allocated extent may have been merged with contiguous extents
3901 * during insertion into the inode fork. Thus, got does not reflect the current
3902 * state of the inode fork on return. If necessary, the caller can use lastx to
3903 * look up the updated record in the inode fork.
3906 xfs_bmapi_reserve_delalloc(
3907 struct xfs_inode
*ip
,
3911 xfs_filblks_t prealloc
,
3912 struct xfs_bmbt_irec
*got
,
3913 struct xfs_iext_cursor
*icur
,
3916 struct xfs_mount
*mp
= ip
->i_mount
;
3917 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
3919 xfs_extlen_t indlen
;
3921 xfs_fileoff_t aoff
= off
;
3924 * Cap the alloc length. Keep track of prealloc so we know whether to
3925 * tag the inode before we return.
3927 alen
= XFS_FILBLKS_MIN(len
+ prealloc
, MAXEXTLEN
);
3929 alen
= XFS_FILBLKS_MIN(alen
, got
->br_startoff
- aoff
);
3930 if (prealloc
&& alen
>= len
)
3931 prealloc
= alen
- len
;
3933 /* Figure out the extent size, adjust alen */
3934 if (whichfork
== XFS_COW_FORK
) {
3935 struct xfs_bmbt_irec prev
;
3936 xfs_extlen_t extsz
= xfs_get_cowextsz_hint(ip
);
3938 if (!xfs_iext_peek_prev_extent(ifp
, icur
, &prev
))
3939 prev
.br_startoff
= NULLFILEOFF
;
3941 error
= xfs_bmap_extsize_align(mp
, got
, &prev
, extsz
, 0, eof
,
3942 1, 0, &aoff
, &alen
);
3947 * Make a transaction-less quota reservation for delayed allocation
3948 * blocks. This number gets adjusted later. We return if we haven't
3949 * allocated blocks already inside this loop.
3951 error
= xfs_trans_reserve_quota_nblks(NULL
, ip
, (long)alen
, 0,
3952 XFS_QMOPT_RES_REGBLKS
);
3957 * Split changing sb for alen and indlen since they could be coming
3958 * from different places.
3960 indlen
= (xfs_extlen_t
)xfs_bmap_worst_indlen(ip
, alen
);
3963 error
= xfs_mod_fdblocks(mp
, -((int64_t)alen
), false);
3965 goto out_unreserve_quota
;
3967 error
= xfs_mod_fdblocks(mp
, -((int64_t)indlen
), false);
3969 goto out_unreserve_blocks
;
3972 ip
->i_delayed_blks
+= alen
;
3973 xfs_mod_delalloc(ip
->i_mount
, alen
+ indlen
);
3975 got
->br_startoff
= aoff
;
3976 got
->br_startblock
= nullstartblock(indlen
);
3977 got
->br_blockcount
= alen
;
3978 got
->br_state
= XFS_EXT_NORM
;
3980 xfs_bmap_add_extent_hole_delay(ip
, whichfork
, icur
, got
);
3983 * Tag the inode if blocks were preallocated. Note that COW fork
3984 * preallocation can occur at the start or end of the extent, even when
3985 * prealloc == 0, so we must also check the aligned offset and length.
3987 if (whichfork
== XFS_DATA_FORK
&& prealloc
)
3988 xfs_inode_set_eofblocks_tag(ip
);
3989 if (whichfork
== XFS_COW_FORK
&& (prealloc
|| aoff
< off
|| alen
> len
))
3990 xfs_inode_set_cowblocks_tag(ip
);
3994 out_unreserve_blocks
:
3995 xfs_mod_fdblocks(mp
, alen
, false);
3996 out_unreserve_quota
:
3997 if (XFS_IS_QUOTA_ON(mp
))
3998 xfs_trans_unreserve_quota_nblks(NULL
, ip
, (long)alen
, 0,
3999 XFS_QMOPT_RES_REGBLKS
);
4005 struct xfs_bmalloca
*bma
)
4007 struct xfs_mount
*mp
= bma
->ip
->i_mount
;
4008 int whichfork
= xfs_bmapi_whichfork(bma
->flags
);
4009 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
4010 int tmp_logflags
= 0;
4013 ASSERT(bma
->length
> 0);
4016 * For the wasdelay case, we could also just allocate the stuff asked
4017 * for in this bmap call but that wouldn't be as good.
4020 bma
->length
= (xfs_extlen_t
)bma
->got
.br_blockcount
;
4021 bma
->offset
= bma
->got
.br_startoff
;
4022 xfs_iext_peek_prev_extent(ifp
, &bma
->icur
, &bma
->prev
);
4024 bma
->length
= XFS_FILBLKS_MIN(bma
->length
, MAXEXTLEN
);
4026 bma
->length
= XFS_FILBLKS_MIN(bma
->length
,
4027 bma
->got
.br_startoff
- bma
->offset
);
4031 * Set the data type being allocated. For the data fork, the first data
4032 * in the file is treated differently to all other allocations. For the
4033 * attribute fork, we only need to ensure the allocated range is not on
4036 if (!(bma
->flags
& XFS_BMAPI_METADATA
)) {
4037 bma
->datatype
= XFS_ALLOC_NOBUSY
;
4038 if (whichfork
== XFS_DATA_FORK
&& bma
->offset
== 0)
4039 bma
->datatype
|= XFS_ALLOC_INITIAL_USER_DATA
;
4040 if (bma
->flags
& XFS_BMAPI_ZERO
)
4041 bma
->datatype
|= XFS_ALLOC_USERDATA_ZERO
;
4044 bma
->minlen
= (bma
->flags
& XFS_BMAPI_CONTIG
) ? bma
->length
: 1;
4047 * Only want to do the alignment at the eof if it is userdata and
4048 * allocation length is larger than a stripe unit.
4050 if (mp
->m_dalign
&& bma
->length
>= mp
->m_dalign
&&
4051 !(bma
->flags
& XFS_BMAPI_METADATA
) && whichfork
== XFS_DATA_FORK
) {
4052 error
= xfs_bmap_isaeof(bma
, whichfork
);
4057 error
= xfs_bmap_alloc(bma
);
4061 if (bma
->blkno
== NULLFSBLOCK
)
4063 if ((ifp
->if_flags
& XFS_IFBROOT
) && !bma
->cur
)
4064 bma
->cur
= xfs_bmbt_init_cursor(mp
, bma
->tp
, bma
->ip
, whichfork
);
4066 * Bump the number of extents we've allocated
4072 bma
->cur
->bc_private
.b
.flags
=
4073 bma
->wasdel
? XFS_BTCUR_BPRV_WASDEL
: 0;
4075 bma
->got
.br_startoff
= bma
->offset
;
4076 bma
->got
.br_startblock
= bma
->blkno
;
4077 bma
->got
.br_blockcount
= bma
->length
;
4078 bma
->got
.br_state
= XFS_EXT_NORM
;
4081 * In the data fork, a wasdelay extent has been initialized, so
4082 * shouldn't be flagged as unwritten.
4084 * For the cow fork, however, we convert delalloc reservations
4085 * (extents allocated for speculative preallocation) to
4086 * allocated unwritten extents, and only convert the unwritten
4087 * extents to real extents when we're about to write the data.
4089 if ((!bma
->wasdel
|| (bma
->flags
& XFS_BMAPI_COWFORK
)) &&
4090 (bma
->flags
& XFS_BMAPI_PREALLOC
))
4091 bma
->got
.br_state
= XFS_EXT_UNWRITTEN
;
4094 error
= xfs_bmap_add_extent_delay_real(bma
, whichfork
);
4096 error
= xfs_bmap_add_extent_hole_real(bma
->tp
, bma
->ip
,
4097 whichfork
, &bma
->icur
, &bma
->cur
, &bma
->got
,
4098 &bma
->logflags
, bma
->flags
);
4100 bma
->logflags
|= tmp_logflags
;
4105 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4106 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4107 * the neighbouring ones.
4109 xfs_iext_get_extent(ifp
, &bma
->icur
, &bma
->got
);
4111 ASSERT(bma
->got
.br_startoff
<= bma
->offset
);
4112 ASSERT(bma
->got
.br_startoff
+ bma
->got
.br_blockcount
>=
4113 bma
->offset
+ bma
->length
);
4114 ASSERT(bma
->got
.br_state
== XFS_EXT_NORM
||
4115 bma
->got
.br_state
== XFS_EXT_UNWRITTEN
);
4120 xfs_bmapi_convert_unwritten(
4121 struct xfs_bmalloca
*bma
,
4122 struct xfs_bmbt_irec
*mval
,
4126 int whichfork
= xfs_bmapi_whichfork(flags
);
4127 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
4128 int tmp_logflags
= 0;
4131 /* check if we need to do unwritten->real conversion */
4132 if (mval
->br_state
== XFS_EXT_UNWRITTEN
&&
4133 (flags
& XFS_BMAPI_PREALLOC
))
4136 /* check if we need to do real->unwritten conversion */
4137 if (mval
->br_state
== XFS_EXT_NORM
&&
4138 (flags
& (XFS_BMAPI_PREALLOC
| XFS_BMAPI_CONVERT
)) !=
4139 (XFS_BMAPI_PREALLOC
| XFS_BMAPI_CONVERT
))
4143 * Modify (by adding) the state flag, if writing.
4145 ASSERT(mval
->br_blockcount
<= len
);
4146 if ((ifp
->if_flags
& XFS_IFBROOT
) && !bma
->cur
) {
4147 bma
->cur
= xfs_bmbt_init_cursor(bma
->ip
->i_mount
, bma
->tp
,
4148 bma
->ip
, whichfork
);
4150 mval
->br_state
= (mval
->br_state
== XFS_EXT_UNWRITTEN
)
4151 ? XFS_EXT_NORM
: XFS_EXT_UNWRITTEN
;
4154 * Before insertion into the bmbt, zero the range being converted
4157 if (flags
& XFS_BMAPI_ZERO
) {
4158 error
= xfs_zero_extent(bma
->ip
, mval
->br_startblock
,
4159 mval
->br_blockcount
);
4164 error
= xfs_bmap_add_extent_unwritten_real(bma
->tp
, bma
->ip
, whichfork
,
4165 &bma
->icur
, &bma
->cur
, mval
, &tmp_logflags
);
4167 * Log the inode core unconditionally in the unwritten extent conversion
4168 * path because the conversion might not have done so (e.g., if the
4169 * extent count hasn't changed). We need to make sure the inode is dirty
4170 * in the transaction for the sake of fsync(), even if nothing has
4171 * changed, because fsync() will not force the log for this transaction
4172 * unless it sees the inode pinned.
4174 * Note: If we're only converting cow fork extents, there aren't
4175 * any on-disk updates to make, so we don't need to log anything.
4177 if (whichfork
!= XFS_COW_FORK
)
4178 bma
->logflags
|= tmp_logflags
| XFS_ILOG_CORE
;
4183 * Update our extent pointer, given that
4184 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4185 * of the neighbouring ones.
4187 xfs_iext_get_extent(ifp
, &bma
->icur
, &bma
->got
);
4190 * We may have combined previously unwritten space with written space,
4191 * so generate another request.
4193 if (mval
->br_blockcount
< len
)
4198 static inline xfs_extlen_t
4200 struct xfs_trans
*tp
,
4201 struct xfs_inode
*ip
,
4204 if (tp
&& tp
->t_firstblock
!= NULLFSBLOCK
)
4206 if (XFS_IFORK_FORMAT(ip
, fork
) != XFS_DINODE_FMT_BTREE
)
4208 return be16_to_cpu(XFS_IFORK_PTR(ip
, fork
)->if_broot
->bb_level
) + 1;
4212 * Log whatever the flags say, even if error. Otherwise we might miss detecting
4213 * a case where the data is changed, there's an error, and it's not logged so we
4214 * don't shutdown when we should. Don't bother logging extents/btree changes if
4215 * we converted to the other format.
4219 struct xfs_bmalloca
*bma
,
4223 if ((bma
->logflags
& xfs_ilog_fext(whichfork
)) &&
4224 XFS_IFORK_FORMAT(bma
->ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
4225 bma
->logflags
&= ~xfs_ilog_fext(whichfork
);
4226 else if ((bma
->logflags
& xfs_ilog_fbroot(whichfork
)) &&
4227 XFS_IFORK_FORMAT(bma
->ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)
4228 bma
->logflags
&= ~xfs_ilog_fbroot(whichfork
);
4231 xfs_trans_log_inode(bma
->tp
, bma
->ip
, bma
->logflags
);
4233 xfs_btree_del_cursor(bma
->cur
, error
);
4237 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4238 * extent state if necessary. Details behaviour is controlled by the flags
4239 * parameter. Only allocates blocks from a single allocation group, to avoid
4244 struct xfs_trans
*tp
, /* transaction pointer */
4245 struct xfs_inode
*ip
, /* incore inode */
4246 xfs_fileoff_t bno
, /* starting file offs. mapped */
4247 xfs_filblks_t len
, /* length to map in file */
4248 int flags
, /* XFS_BMAPI_... */
4249 xfs_extlen_t total
, /* total blocks needed */
4250 struct xfs_bmbt_irec
*mval
, /* output: map values */
4251 int *nmap
) /* i/o: mval size/count */
4253 struct xfs_bmalloca bma
= {
4258 struct xfs_mount
*mp
= ip
->i_mount
;
4259 struct xfs_ifork
*ifp
;
4260 xfs_fileoff_t end
; /* end of mapped file region */
4261 bool eof
= false; /* after the end of extents */
4262 int error
; /* error return */
4263 int n
; /* current extent index */
4264 xfs_fileoff_t obno
; /* old block number (offset) */
4265 int whichfork
; /* data or attr fork */
4268 xfs_fileoff_t orig_bno
; /* original block number value */
4269 int orig_flags
; /* original flags arg value */
4270 xfs_filblks_t orig_len
; /* original value of len arg */
4271 struct xfs_bmbt_irec
*orig_mval
; /* original value of mval */
4272 int orig_nmap
; /* original value of *nmap */
4280 whichfork
= xfs_bmapi_whichfork(flags
);
4283 ASSERT(*nmap
<= XFS_BMAP_MAX_NMAP
);
4286 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_LOCAL
);
4287 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
4288 ASSERT(!(flags
& XFS_BMAPI_REMAP
));
4290 /* zeroing is for currently only for data extents, not metadata */
4291 ASSERT((flags
& (XFS_BMAPI_METADATA
| XFS_BMAPI_ZERO
)) !=
4292 (XFS_BMAPI_METADATA
| XFS_BMAPI_ZERO
));
4294 * we can allocate unwritten extents or pre-zero allocated blocks,
4295 * but it makes no sense to do both at once. This would result in
4296 * zeroing the unwritten extent twice, but it still being an
4297 * unwritten extent....
4299 ASSERT((flags
& (XFS_BMAPI_PREALLOC
| XFS_BMAPI_ZERO
)) !=
4300 (XFS_BMAPI_PREALLOC
| XFS_BMAPI_ZERO
));
4302 if (unlikely(XFS_TEST_ERROR(
4303 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
4304 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
4305 mp
, XFS_ERRTAG_BMAPIFORMAT
))) {
4306 XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW
, mp
);
4307 return -EFSCORRUPTED
;
4310 if (XFS_FORCED_SHUTDOWN(mp
))
4313 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4315 XFS_STATS_INC(mp
, xs_blk_mapw
);
4317 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
4318 error
= xfs_iread_extents(tp
, ip
, whichfork
);
4323 if (!xfs_iext_lookup_extent(ip
, ifp
, bno
, &bma
.icur
, &bma
.got
))
4325 if (!xfs_iext_peek_prev_extent(ifp
, &bma
.icur
, &bma
.prev
))
4326 bma
.prev
.br_startoff
= NULLFILEOFF
;
4327 bma
.minleft
= xfs_bmapi_minleft(tp
, ip
, whichfork
);
4332 while (bno
< end
&& n
< *nmap
) {
4333 bool need_alloc
= false, wasdelay
= false;
4335 /* in hole or beyond EOF? */
4336 if (eof
|| bma
.got
.br_startoff
> bno
) {
4338 * CoW fork conversions should /never/ hit EOF or
4339 * holes. There should always be something for us
4342 ASSERT(!((flags
& XFS_BMAPI_CONVERT
) &&
4343 (flags
& XFS_BMAPI_COWFORK
)));
4346 } else if (isnullstartblock(bma
.got
.br_startblock
)) {
4351 * First, deal with the hole before the allocated space
4352 * that we found, if any.
4354 if (need_alloc
|| wasdelay
) {
4356 bma
.conv
= !!(flags
& XFS_BMAPI_CONVERT
);
4357 bma
.wasdel
= wasdelay
;
4362 * There's a 32/64 bit type mismatch between the
4363 * allocation length request (which can be 64 bits in
4364 * length) and the bma length request, which is
4365 * xfs_extlen_t and therefore 32 bits. Hence we have to
4366 * check for 32-bit overflows and handle them here.
4368 if (len
> (xfs_filblks_t
)MAXEXTLEN
)
4369 bma
.length
= MAXEXTLEN
;
4374 ASSERT(bma
.length
> 0);
4375 error
= xfs_bmapi_allocate(&bma
);
4378 if (bma
.blkno
== NULLFSBLOCK
)
4382 * If this is a CoW allocation, record the data in
4383 * the refcount btree for orphan recovery.
4385 if (whichfork
== XFS_COW_FORK
)
4386 xfs_refcount_alloc_cow_extent(tp
, bma
.blkno
,
4390 /* Deal with the allocated space we found. */
4391 xfs_bmapi_trim_map(mval
, &bma
.got
, &bno
, len
, obno
,
4394 /* Execute unwritten extent conversion if necessary */
4395 error
= xfs_bmapi_convert_unwritten(&bma
, mval
, len
, flags
);
4396 if (error
== -EAGAIN
)
4401 /* update the extent map to return */
4402 xfs_bmapi_update_map(&mval
, &bno
, &len
, obno
, end
, &n
, flags
);
4405 * If we're done, stop now. Stop when we've allocated
4406 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4407 * the transaction may get too big.
4409 if (bno
>= end
|| n
>= *nmap
|| bma
.nallocs
>= *nmap
)
4412 /* Else go on to the next record. */
4414 if (!xfs_iext_next_extent(ifp
, &bma
.icur
, &bma
.got
))
4419 error
= xfs_bmap_btree_to_extents(tp
, ip
, bma
.cur
, &bma
.logflags
,
4424 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
||
4425 XFS_IFORK_NEXTENTS(ip
, whichfork
) >
4426 XFS_IFORK_MAXEXT(ip
, whichfork
));
4427 xfs_bmapi_finish(&bma
, whichfork
, 0);
4428 xfs_bmap_validate_ret(orig_bno
, orig_len
, orig_flags
, orig_mval
,
4432 xfs_bmapi_finish(&bma
, whichfork
, error
);
4437 * Convert an existing delalloc extent to real blocks based on file offset. This
4438 * attempts to allocate the entire delalloc extent and may require multiple
4439 * invocations to allocate the target offset if a large enough physical extent
4443 xfs_bmapi_convert_delalloc(
4444 struct xfs_inode
*ip
,
4446 xfs_fileoff_t offset_fsb
,
4447 struct xfs_bmbt_irec
*imap
,
4450 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4451 struct xfs_mount
*mp
= ip
->i_mount
;
4452 struct xfs_bmalloca bma
= { NULL
};
4453 struct xfs_trans
*tp
;
4457 * Space for the extent and indirect blocks was reserved when the
4458 * delalloc extent was created so there's no need to do so here.
4460 error
= xfs_trans_alloc(mp
, &M_RES(mp
)->tr_write
, 0, 0,
4461 XFS_TRANS_RESERVE
, &tp
);
4465 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
4466 xfs_trans_ijoin(tp
, ip
, 0);
4468 if (!xfs_iext_lookup_extent(ip
, ifp
, offset_fsb
, &bma
.icur
, &bma
.got
) ||
4469 bma
.got
.br_startoff
> offset_fsb
) {
4471 * No extent found in the range we are trying to convert. This
4472 * should only happen for the COW fork, where another thread
4473 * might have moved the extent to the data fork in the meantime.
4475 WARN_ON_ONCE(whichfork
!= XFS_COW_FORK
);
4477 goto out_trans_cancel
;
4481 * If we find a real extent here we raced with another thread converting
4482 * the extent. Just return the real extent at this offset.
4484 if (!isnullstartblock(bma
.got
.br_startblock
)) {
4486 *seq
= READ_ONCE(ifp
->if_seq
);
4487 goto out_trans_cancel
;
4493 bma
.offset
= bma
.got
.br_startoff
;
4494 bma
.length
= max_t(xfs_filblks_t
, bma
.got
.br_blockcount
, MAXEXTLEN
);
4495 bma
.total
= XFS_EXTENTADD_SPACE_RES(ip
->i_mount
, XFS_DATA_FORK
);
4496 bma
.minleft
= xfs_bmapi_minleft(tp
, ip
, whichfork
);
4497 if (whichfork
== XFS_COW_FORK
)
4498 bma
.flags
= XFS_BMAPI_COWFORK
| XFS_BMAPI_PREALLOC
;
4500 if (!xfs_iext_peek_prev_extent(ifp
, &bma
.icur
, &bma
.prev
))
4501 bma
.prev
.br_startoff
= NULLFILEOFF
;
4503 error
= xfs_bmapi_allocate(&bma
);
4508 if (WARN_ON_ONCE(bma
.blkno
== NULLFSBLOCK
))
4510 error
= -EFSCORRUPTED
;
4511 if (WARN_ON_ONCE(!bma
.got
.br_startblock
&& !XFS_IS_REALTIME_INODE(ip
)))
4514 XFS_STATS_ADD(mp
, xs_xstrat_bytes
, XFS_FSB_TO_B(mp
, bma
.length
));
4515 XFS_STATS_INC(mp
, xs_xstrat_quick
);
4517 ASSERT(!isnullstartblock(bma
.got
.br_startblock
));
4519 *seq
= READ_ONCE(ifp
->if_seq
);
4521 if (whichfork
== XFS_COW_FORK
)
4522 xfs_refcount_alloc_cow_extent(tp
, bma
.blkno
, bma
.length
);
4524 error
= xfs_bmap_btree_to_extents(tp
, ip
, bma
.cur
, &bma
.logflags
,
4529 xfs_bmapi_finish(&bma
, whichfork
, 0);
4530 error
= xfs_trans_commit(tp
);
4531 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
4535 xfs_bmapi_finish(&bma
, whichfork
, error
);
4537 xfs_trans_cancel(tp
);
4538 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
4544 struct xfs_trans
*tp
,
4545 struct xfs_inode
*ip
,
4548 xfs_fsblock_t startblock
,
4551 struct xfs_mount
*mp
= ip
->i_mount
;
4552 struct xfs_ifork
*ifp
;
4553 struct xfs_btree_cur
*cur
= NULL
;
4554 struct xfs_bmbt_irec got
;
4555 struct xfs_iext_cursor icur
;
4556 int whichfork
= xfs_bmapi_whichfork(flags
);
4557 int logflags
= 0, error
;
4559 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4561 ASSERT(len
<= (xfs_filblks_t
)MAXEXTLEN
);
4562 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
4563 ASSERT(!(flags
& ~(XFS_BMAPI_ATTRFORK
| XFS_BMAPI_PREALLOC
|
4564 XFS_BMAPI_NORMAP
)));
4565 ASSERT((flags
& (XFS_BMAPI_ATTRFORK
| XFS_BMAPI_PREALLOC
)) !=
4566 (XFS_BMAPI_ATTRFORK
| XFS_BMAPI_PREALLOC
));
4568 if (unlikely(XFS_TEST_ERROR(
4569 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
4570 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
4571 mp
, XFS_ERRTAG_BMAPIFORMAT
))) {
4572 XFS_ERROR_REPORT("xfs_bmapi_remap", XFS_ERRLEVEL_LOW
, mp
);
4573 return -EFSCORRUPTED
;
4576 if (XFS_FORCED_SHUTDOWN(mp
))
4579 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
4580 error
= xfs_iread_extents(tp
, ip
, whichfork
);
4585 if (xfs_iext_lookup_extent(ip
, ifp
, bno
, &icur
, &got
)) {
4586 /* make sure we only reflink into a hole. */
4587 ASSERT(got
.br_startoff
> bno
);
4588 ASSERT(got
.br_startoff
- bno
>= len
);
4591 ip
->i_d
.di_nblocks
+= len
;
4592 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
4594 if (ifp
->if_flags
& XFS_IFBROOT
) {
4595 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
4596 cur
->bc_private
.b
.flags
= 0;
4599 got
.br_startoff
= bno
;
4600 got
.br_startblock
= startblock
;
4601 got
.br_blockcount
= len
;
4602 if (flags
& XFS_BMAPI_PREALLOC
)
4603 got
.br_state
= XFS_EXT_UNWRITTEN
;
4605 got
.br_state
= XFS_EXT_NORM
;
4607 error
= xfs_bmap_add_extent_hole_real(tp
, ip
, whichfork
, &icur
,
4608 &cur
, &got
, &logflags
, flags
);
4612 error
= xfs_bmap_btree_to_extents(tp
, ip
, cur
, &logflags
, whichfork
);
4615 if (ip
->i_d
.di_format
!= XFS_DINODE_FMT_EXTENTS
)
4616 logflags
&= ~XFS_ILOG_DEXT
;
4617 else if (ip
->i_d
.di_format
!= XFS_DINODE_FMT_BTREE
)
4618 logflags
&= ~XFS_ILOG_DBROOT
;
4621 xfs_trans_log_inode(tp
, ip
, logflags
);
4623 xfs_btree_del_cursor(cur
, error
);
4628 * When a delalloc extent is split (e.g., due to a hole punch), the original
4629 * indlen reservation must be shared across the two new extents that are left
4632 * Given the original reservation and the worst case indlen for the two new
4633 * extents (as calculated by xfs_bmap_worst_indlen()), split the original
4634 * reservation fairly across the two new extents. If necessary, steal available
4635 * blocks from a deleted extent to make up a reservation deficiency (e.g., if
4636 * ores == 1). The number of stolen blocks is returned. The availability and
4637 * subsequent accounting of stolen blocks is the responsibility of the caller.
4639 static xfs_filblks_t
4640 xfs_bmap_split_indlen(
4641 xfs_filblks_t ores
, /* original res. */
4642 xfs_filblks_t
*indlen1
, /* ext1 worst indlen */
4643 xfs_filblks_t
*indlen2
, /* ext2 worst indlen */
4644 xfs_filblks_t avail
) /* stealable blocks */
4646 xfs_filblks_t len1
= *indlen1
;
4647 xfs_filblks_t len2
= *indlen2
;
4648 xfs_filblks_t nres
= len1
+ len2
; /* new total res. */
4649 xfs_filblks_t stolen
= 0;
4650 xfs_filblks_t resfactor
;
4653 * Steal as many blocks as we can to try and satisfy the worst case
4654 * indlen for both new extents.
4656 if (ores
< nres
&& avail
)
4657 stolen
= XFS_FILBLKS_MIN(nres
- ores
, avail
);
4660 /* nothing else to do if we've satisfied the new reservation */
4665 * We can't meet the total required reservation for the two extents.
4666 * Calculate the percent of the overall shortage between both extents
4667 * and apply this percentage to each of the requested indlen values.
4668 * This distributes the shortage fairly and reduces the chances that one
4669 * of the two extents is left with nothing when extents are repeatedly
4672 resfactor
= (ores
* 100);
4673 do_div(resfactor
, nres
);
4678 ASSERT(len1
+ len2
<= ores
);
4679 ASSERT(len1
< *indlen1
&& len2
< *indlen2
);
4682 * Hand out the remainder to each extent. If one of the two reservations
4683 * is zero, we want to make sure that one gets a block first. The loop
4684 * below starts with len1, so hand len2 a block right off the bat if it
4687 ores
-= (len1
+ len2
);
4688 ASSERT((*indlen1
- len1
) + (*indlen2
- len2
) >= ores
);
4689 if (ores
&& !len2
&& *indlen2
) {
4694 if (len1
< *indlen1
) {
4700 if (len2
< *indlen2
) {
4713 xfs_bmap_del_extent_delay(
4714 struct xfs_inode
*ip
,
4716 struct xfs_iext_cursor
*icur
,
4717 struct xfs_bmbt_irec
*got
,
4718 struct xfs_bmbt_irec
*del
)
4720 struct xfs_mount
*mp
= ip
->i_mount
;
4721 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4722 struct xfs_bmbt_irec
new;
4723 int64_t da_old
, da_new
, da_diff
= 0;
4724 xfs_fileoff_t del_endoff
, got_endoff
;
4725 xfs_filblks_t got_indlen
, new_indlen
, stolen
;
4726 int state
= xfs_bmap_fork_to_state(whichfork
);
4730 XFS_STATS_INC(mp
, xs_del_exlist
);
4732 isrt
= (whichfork
== XFS_DATA_FORK
) && XFS_IS_REALTIME_INODE(ip
);
4733 del_endoff
= del
->br_startoff
+ del
->br_blockcount
;
4734 got_endoff
= got
->br_startoff
+ got
->br_blockcount
;
4735 da_old
= startblockval(got
->br_startblock
);
4738 ASSERT(del
->br_blockcount
> 0);
4739 ASSERT(got
->br_startoff
<= del
->br_startoff
);
4740 ASSERT(got_endoff
>= del_endoff
);
4743 uint64_t rtexts
= XFS_FSB_TO_B(mp
, del
->br_blockcount
);
4745 do_div(rtexts
, mp
->m_sb
.sb_rextsize
);
4746 xfs_mod_frextents(mp
, rtexts
);
4750 * Update the inode delalloc counter now and wait to update the
4751 * sb counters as we might have to borrow some blocks for the
4752 * indirect block accounting.
4754 error
= xfs_trans_reserve_quota_nblks(NULL
, ip
,
4755 -((long)del
->br_blockcount
), 0,
4756 isrt
? XFS_QMOPT_RES_RTBLKS
: XFS_QMOPT_RES_REGBLKS
);
4759 ip
->i_delayed_blks
-= del
->br_blockcount
;
4761 if (got
->br_startoff
== del
->br_startoff
)
4762 state
|= BMAP_LEFT_FILLING
;
4763 if (got_endoff
== del_endoff
)
4764 state
|= BMAP_RIGHT_FILLING
;
4766 switch (state
& (BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
)) {
4767 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
4769 * Matches the whole extent. Delete the entry.
4771 xfs_iext_remove(ip
, icur
, state
);
4772 xfs_iext_prev(ifp
, icur
);
4774 case BMAP_LEFT_FILLING
:
4776 * Deleting the first part of the extent.
4778 got
->br_startoff
= del_endoff
;
4779 got
->br_blockcount
-= del
->br_blockcount
;
4780 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
,
4781 got
->br_blockcount
), da_old
);
4782 got
->br_startblock
= nullstartblock((int)da_new
);
4783 xfs_iext_update_extent(ip
, state
, icur
, got
);
4785 case BMAP_RIGHT_FILLING
:
4787 * Deleting the last part of the extent.
4789 got
->br_blockcount
= got
->br_blockcount
- del
->br_blockcount
;
4790 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
,
4791 got
->br_blockcount
), da_old
);
4792 got
->br_startblock
= nullstartblock((int)da_new
);
4793 xfs_iext_update_extent(ip
, state
, icur
, got
);
4797 * Deleting the middle of the extent.
4799 * Distribute the original indlen reservation across the two new
4800 * extents. Steal blocks from the deleted extent if necessary.
4801 * Stealing blocks simply fudges the fdblocks accounting below.
4802 * Warn if either of the new indlen reservations is zero as this
4803 * can lead to delalloc problems.
4805 got
->br_blockcount
= del
->br_startoff
- got
->br_startoff
;
4806 got_indlen
= xfs_bmap_worst_indlen(ip
, got
->br_blockcount
);
4808 new.br_blockcount
= got_endoff
- del_endoff
;
4809 new_indlen
= xfs_bmap_worst_indlen(ip
, new.br_blockcount
);
4811 WARN_ON_ONCE(!got_indlen
|| !new_indlen
);
4812 stolen
= xfs_bmap_split_indlen(da_old
, &got_indlen
, &new_indlen
,
4813 del
->br_blockcount
);
4815 got
->br_startblock
= nullstartblock((int)got_indlen
);
4817 new.br_startoff
= del_endoff
;
4818 new.br_state
= got
->br_state
;
4819 new.br_startblock
= nullstartblock((int)new_indlen
);
4821 xfs_iext_update_extent(ip
, state
, icur
, got
);
4822 xfs_iext_next(ifp
, icur
);
4823 xfs_iext_insert(ip
, icur
, &new, state
);
4825 da_new
= got_indlen
+ new_indlen
- stolen
;
4826 del
->br_blockcount
-= stolen
;
4830 ASSERT(da_old
>= da_new
);
4831 da_diff
= da_old
- da_new
;
4833 da_diff
+= del
->br_blockcount
;
4835 xfs_mod_fdblocks(mp
, da_diff
, false);
4836 xfs_mod_delalloc(mp
, -da_diff
);
4842 xfs_bmap_del_extent_cow(
4843 struct xfs_inode
*ip
,
4844 struct xfs_iext_cursor
*icur
,
4845 struct xfs_bmbt_irec
*got
,
4846 struct xfs_bmbt_irec
*del
)
4848 struct xfs_mount
*mp
= ip
->i_mount
;
4849 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, XFS_COW_FORK
);
4850 struct xfs_bmbt_irec
new;
4851 xfs_fileoff_t del_endoff
, got_endoff
;
4852 int state
= BMAP_COWFORK
;
4854 XFS_STATS_INC(mp
, xs_del_exlist
);
4856 del_endoff
= del
->br_startoff
+ del
->br_blockcount
;
4857 got_endoff
= got
->br_startoff
+ got
->br_blockcount
;
4859 ASSERT(del
->br_blockcount
> 0);
4860 ASSERT(got
->br_startoff
<= del
->br_startoff
);
4861 ASSERT(got_endoff
>= del_endoff
);
4862 ASSERT(!isnullstartblock(got
->br_startblock
));
4864 if (got
->br_startoff
== del
->br_startoff
)
4865 state
|= BMAP_LEFT_FILLING
;
4866 if (got_endoff
== del_endoff
)
4867 state
|= BMAP_RIGHT_FILLING
;
4869 switch (state
& (BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
)) {
4870 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
4872 * Matches the whole extent. Delete the entry.
4874 xfs_iext_remove(ip
, icur
, state
);
4875 xfs_iext_prev(ifp
, icur
);
4877 case BMAP_LEFT_FILLING
:
4879 * Deleting the first part of the extent.
4881 got
->br_startoff
= del_endoff
;
4882 got
->br_blockcount
-= del
->br_blockcount
;
4883 got
->br_startblock
= del
->br_startblock
+ del
->br_blockcount
;
4884 xfs_iext_update_extent(ip
, state
, icur
, got
);
4886 case BMAP_RIGHT_FILLING
:
4888 * Deleting the last part of the extent.
4890 got
->br_blockcount
-= del
->br_blockcount
;
4891 xfs_iext_update_extent(ip
, state
, icur
, got
);
4895 * Deleting the middle of the extent.
4897 got
->br_blockcount
= del
->br_startoff
- got
->br_startoff
;
4899 new.br_startoff
= del_endoff
;
4900 new.br_blockcount
= got_endoff
- del_endoff
;
4901 new.br_state
= got
->br_state
;
4902 new.br_startblock
= del
->br_startblock
+ del
->br_blockcount
;
4904 xfs_iext_update_extent(ip
, state
, icur
, got
);
4905 xfs_iext_next(ifp
, icur
);
4906 xfs_iext_insert(ip
, icur
, &new, state
);
4909 ip
->i_delayed_blks
-= del
->br_blockcount
;
4913 * Called by xfs_bmapi to update file extent records and the btree
4914 * after removing space.
4916 STATIC
int /* error */
4917 xfs_bmap_del_extent_real(
4918 xfs_inode_t
*ip
, /* incore inode pointer */
4919 xfs_trans_t
*tp
, /* current transaction pointer */
4920 struct xfs_iext_cursor
*icur
,
4921 xfs_btree_cur_t
*cur
, /* if null, not a btree */
4922 xfs_bmbt_irec_t
*del
, /* data to remove from extents */
4923 int *logflagsp
, /* inode logging flags */
4924 int whichfork
, /* data or attr fork */
4925 int bflags
) /* bmapi flags */
4927 xfs_fsblock_t del_endblock
=0; /* first block past del */
4928 xfs_fileoff_t del_endoff
; /* first offset past del */
4929 int do_fx
; /* free extent at end of routine */
4930 int error
; /* error return value */
4931 int flags
= 0;/* inode logging flags */
4932 struct xfs_bmbt_irec got
; /* current extent entry */
4933 xfs_fileoff_t got_endoff
; /* first offset past got */
4934 int i
; /* temp state */
4935 struct xfs_ifork
*ifp
; /* inode fork pointer */
4936 xfs_mount_t
*mp
; /* mount structure */
4937 xfs_filblks_t nblks
; /* quota/sb block count */
4938 xfs_bmbt_irec_t
new; /* new record to be inserted */
4940 uint qfield
; /* quota field to update */
4941 int state
= xfs_bmap_fork_to_state(whichfork
);
4942 struct xfs_bmbt_irec old
;
4945 XFS_STATS_INC(mp
, xs_del_exlist
);
4947 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4948 ASSERT(del
->br_blockcount
> 0);
4949 xfs_iext_get_extent(ifp
, icur
, &got
);
4950 ASSERT(got
.br_startoff
<= del
->br_startoff
);
4951 del_endoff
= del
->br_startoff
+ del
->br_blockcount
;
4952 got_endoff
= got
.br_startoff
+ got
.br_blockcount
;
4953 ASSERT(got_endoff
>= del_endoff
);
4954 ASSERT(!isnullstartblock(got
.br_startblock
));
4959 * If it's the case where the directory code is running with no block
4960 * reservation, and the deleted block is in the middle of its extent,
4961 * and the resulting insert of an extent would cause transformation to
4962 * btree format, then reject it. The calling code will then swap blocks
4963 * around instead. We have to do this now, rather than waiting for the
4964 * conversion to btree format, since the transaction will be dirty then.
4966 if (tp
->t_blk_res
== 0 &&
4967 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
&&
4968 XFS_IFORK_NEXTENTS(ip
, whichfork
) >=
4969 XFS_IFORK_MAXEXT(ip
, whichfork
) &&
4970 del
->br_startoff
> got
.br_startoff
&& del_endoff
< got_endoff
)
4973 flags
= XFS_ILOG_CORE
;
4974 if (whichfork
== XFS_DATA_FORK
&& XFS_IS_REALTIME_INODE(ip
)) {
4979 bno
= div_u64_rem(del
->br_startblock
, mp
->m_sb
.sb_rextsize
,
4982 len
= div_u64_rem(del
->br_blockcount
, mp
->m_sb
.sb_rextsize
,
4986 error
= xfs_rtfree_extent(tp
, bno
, (xfs_extlen_t
)len
);
4990 nblks
= len
* mp
->m_sb
.sb_rextsize
;
4991 qfield
= XFS_TRANS_DQ_RTBCOUNT
;
4994 nblks
= del
->br_blockcount
;
4995 qfield
= XFS_TRANS_DQ_BCOUNT
;
4998 del_endblock
= del
->br_startblock
+ del
->br_blockcount
;
5000 error
= xfs_bmbt_lookup_eq(cur
, &got
, &i
);
5003 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
5006 if (got
.br_startoff
== del
->br_startoff
)
5007 state
|= BMAP_LEFT_FILLING
;
5008 if (got_endoff
== del_endoff
)
5009 state
|= BMAP_RIGHT_FILLING
;
5011 switch (state
& (BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
)) {
5012 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
5014 * Matches the whole extent. Delete the entry.
5016 xfs_iext_remove(ip
, icur
, state
);
5017 xfs_iext_prev(ifp
, icur
);
5018 XFS_IFORK_NEXT_SET(ip
, whichfork
,
5019 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
5020 flags
|= XFS_ILOG_CORE
;
5022 flags
|= xfs_ilog_fext(whichfork
);
5025 if ((error
= xfs_btree_delete(cur
, &i
)))
5027 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
5029 case BMAP_LEFT_FILLING
:
5031 * Deleting the first part of the extent.
5033 got
.br_startoff
= del_endoff
;
5034 got
.br_startblock
= del_endblock
;
5035 got
.br_blockcount
-= del
->br_blockcount
;
5036 xfs_iext_update_extent(ip
, state
, icur
, &got
);
5038 flags
|= xfs_ilog_fext(whichfork
);
5041 error
= xfs_bmbt_update(cur
, &got
);
5045 case BMAP_RIGHT_FILLING
:
5047 * Deleting the last part of the extent.
5049 got
.br_blockcount
-= del
->br_blockcount
;
5050 xfs_iext_update_extent(ip
, state
, icur
, &got
);
5052 flags
|= xfs_ilog_fext(whichfork
);
5055 error
= xfs_bmbt_update(cur
, &got
);
5061 * Deleting the middle of the extent.
5065 got
.br_blockcount
= del
->br_startoff
- got
.br_startoff
;
5066 xfs_iext_update_extent(ip
, state
, icur
, &got
);
5068 new.br_startoff
= del_endoff
;
5069 new.br_blockcount
= got_endoff
- del_endoff
;
5070 new.br_state
= got
.br_state
;
5071 new.br_startblock
= del_endblock
;
5073 flags
|= XFS_ILOG_CORE
;
5075 error
= xfs_bmbt_update(cur
, &got
);
5078 error
= xfs_btree_increment(cur
, 0, &i
);
5081 cur
->bc_rec
.b
= new;
5082 error
= xfs_btree_insert(cur
, &i
);
5083 if (error
&& error
!= -ENOSPC
)
5086 * If get no-space back from btree insert, it tried a
5087 * split, and we have a zero block reservation. Fix up
5088 * our state and return the error.
5090 if (error
== -ENOSPC
) {
5092 * Reset the cursor, don't trust it after any
5095 error
= xfs_bmbt_lookup_eq(cur
, &got
, &i
);
5098 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
5100 * Update the btree record back
5101 * to the original value.
5103 error
= xfs_bmbt_update(cur
, &old
);
5107 * Reset the extent record back
5108 * to the original value.
5110 xfs_iext_update_extent(ip
, state
, icur
, &old
);
5115 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
5117 flags
|= xfs_ilog_fext(whichfork
);
5118 XFS_IFORK_NEXT_SET(ip
, whichfork
,
5119 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
5120 xfs_iext_next(ifp
, icur
);
5121 xfs_iext_insert(ip
, icur
, &new, state
);
5125 /* remove reverse mapping */
5126 xfs_rmap_unmap_extent(tp
, ip
, whichfork
, del
);
5129 * If we need to, add to list of extents to delete.
5131 if (do_fx
&& !(bflags
& XFS_BMAPI_REMAP
)) {
5132 if (xfs_is_reflink_inode(ip
) && whichfork
== XFS_DATA_FORK
) {
5133 xfs_refcount_decrease_extent(tp
, del
);
5135 __xfs_bmap_add_free(tp
, del
->br_startblock
,
5136 del
->br_blockcount
, NULL
,
5137 (bflags
& XFS_BMAPI_NODISCARD
) ||
5138 del
->br_state
== XFS_EXT_UNWRITTEN
);
5143 * Adjust inode # blocks in the file.
5146 ip
->i_d
.di_nblocks
-= nblks
;
5148 * Adjust quota data.
5150 if (qfield
&& !(bflags
& XFS_BMAPI_REMAP
))
5151 xfs_trans_mod_dquot_byino(tp
, ip
, qfield
, (long)-nblks
);
5159 * Unmap (remove) blocks from a file.
5160 * If nexts is nonzero then the number of extents to remove is limited to
5161 * that value. If not all extents in the block range can be removed then
5166 struct xfs_trans
*tp
, /* transaction pointer */
5167 struct xfs_inode
*ip
, /* incore inode */
5168 xfs_fileoff_t start
, /* first file offset deleted */
5169 xfs_filblks_t
*rlen
, /* i/o: amount remaining */
5170 int flags
, /* misc flags */
5171 xfs_extnum_t nexts
) /* number of extents max */
5173 struct xfs_btree_cur
*cur
; /* bmap btree cursor */
5174 struct xfs_bmbt_irec del
; /* extent being deleted */
5175 int error
; /* error return value */
5176 xfs_extnum_t extno
; /* extent number in list */
5177 struct xfs_bmbt_irec got
; /* current extent record */
5178 struct xfs_ifork
*ifp
; /* inode fork pointer */
5179 int isrt
; /* freeing in rt area */
5180 int logflags
; /* transaction logging flags */
5181 xfs_extlen_t mod
; /* rt extent offset */
5182 struct xfs_mount
*mp
; /* mount structure */
5183 int tmp_logflags
; /* partial logging flags */
5184 int wasdel
; /* was a delayed alloc extent */
5185 int whichfork
; /* data or attribute fork */
5187 xfs_filblks_t len
= *rlen
; /* length to unmap in file */
5188 xfs_fileoff_t max_len
;
5189 xfs_agnumber_t prev_agno
= NULLAGNUMBER
, agno
;
5191 struct xfs_iext_cursor icur
;
5194 trace_xfs_bunmap(ip
, start
, len
, flags
, _RET_IP_
);
5196 whichfork
= xfs_bmapi_whichfork(flags
);
5197 ASSERT(whichfork
!= XFS_COW_FORK
);
5198 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5200 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
5201 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)) {
5202 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW
,
5204 return -EFSCORRUPTED
;
5207 if (XFS_FORCED_SHUTDOWN(mp
))
5210 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
5215 * Guesstimate how many blocks we can unmap without running the risk of
5216 * blowing out the transaction with a mix of EFIs and reflink
5219 if (tp
&& xfs_is_reflink_inode(ip
) && whichfork
== XFS_DATA_FORK
)
5220 max_len
= min(len
, xfs_refcount_max_unmap(tp
->t_log_res
));
5224 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
5225 (error
= xfs_iread_extents(tp
, ip
, whichfork
)))
5227 if (xfs_iext_count(ifp
) == 0) {
5231 XFS_STATS_INC(mp
, xs_blk_unmap
);
5232 isrt
= (whichfork
== XFS_DATA_FORK
) && XFS_IS_REALTIME_INODE(ip
);
5235 if (!xfs_iext_lookup_extent_before(ip
, ifp
, &end
, &icur
, &got
)) {
5242 if (ifp
->if_flags
& XFS_IFBROOT
) {
5243 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
);
5244 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
5245 cur
->bc_private
.b
.flags
= 0;
5251 * Synchronize by locking the bitmap inode.
5253 xfs_ilock(mp
->m_rbmip
, XFS_ILOCK_EXCL
|XFS_ILOCK_RTBITMAP
);
5254 xfs_trans_ijoin(tp
, mp
->m_rbmip
, XFS_ILOCK_EXCL
);
5255 xfs_ilock(mp
->m_rsumip
, XFS_ILOCK_EXCL
|XFS_ILOCK_RTSUM
);
5256 xfs_trans_ijoin(tp
, mp
->m_rsumip
, XFS_ILOCK_EXCL
);
5260 while (end
!= (xfs_fileoff_t
)-1 && end
>= start
&&
5261 (nexts
== 0 || extno
< nexts
) && max_len
> 0) {
5263 * Is the found extent after a hole in which end lives?
5264 * Just back up to the previous extent, if so.
5266 if (got
.br_startoff
> end
&&
5267 !xfs_iext_prev_extent(ifp
, &icur
, &got
)) {
5272 * Is the last block of this extent before the range
5273 * we're supposed to delete? If so, we're done.
5275 end
= XFS_FILEOFF_MIN(end
,
5276 got
.br_startoff
+ got
.br_blockcount
- 1);
5280 * Then deal with the (possibly delayed) allocated space
5284 wasdel
= isnullstartblock(del
.br_startblock
);
5287 * Make sure we don't touch multiple AGF headers out of order
5288 * in a single transaction, as that could cause AB-BA deadlocks.
5291 agno
= XFS_FSB_TO_AGNO(mp
, del
.br_startblock
);
5292 if (prev_agno
!= NULLAGNUMBER
&& prev_agno
> agno
)
5296 if (got
.br_startoff
< start
) {
5297 del
.br_startoff
= start
;
5298 del
.br_blockcount
-= start
- got
.br_startoff
;
5300 del
.br_startblock
+= start
- got
.br_startoff
;
5302 if (del
.br_startoff
+ del
.br_blockcount
> end
+ 1)
5303 del
.br_blockcount
= end
+ 1 - del
.br_startoff
;
5305 /* How much can we safely unmap? */
5306 if (max_len
< del
.br_blockcount
) {
5307 del
.br_startoff
+= del
.br_blockcount
- max_len
;
5309 del
.br_startblock
+= del
.br_blockcount
- max_len
;
5310 del
.br_blockcount
= max_len
;
5316 sum
= del
.br_startblock
+ del
.br_blockcount
;
5317 div_u64_rem(sum
, mp
->m_sb
.sb_rextsize
, &mod
);
5320 * Realtime extent not lined up at the end.
5321 * The extent could have been split into written
5322 * and unwritten pieces, or we could just be
5323 * unmapping part of it. But we can't really
5324 * get rid of part of a realtime extent.
5326 if (del
.br_state
== XFS_EXT_UNWRITTEN
) {
5328 * This piece is unwritten, or we're not
5329 * using unwritten extents. Skip over it.
5332 end
-= mod
> del
.br_blockcount
?
5333 del
.br_blockcount
: mod
;
5334 if (end
< got
.br_startoff
&&
5335 !xfs_iext_prev_extent(ifp
, &icur
, &got
)) {
5342 * It's written, turn it unwritten.
5343 * This is better than zeroing it.
5345 ASSERT(del
.br_state
== XFS_EXT_NORM
);
5346 ASSERT(tp
->t_blk_res
> 0);
5348 * If this spans a realtime extent boundary,
5349 * chop it back to the start of the one we end at.
5351 if (del
.br_blockcount
> mod
) {
5352 del
.br_startoff
+= del
.br_blockcount
- mod
;
5353 del
.br_startblock
+= del
.br_blockcount
- mod
;
5354 del
.br_blockcount
= mod
;
5356 del
.br_state
= XFS_EXT_UNWRITTEN
;
5357 error
= xfs_bmap_add_extent_unwritten_real(tp
, ip
,
5358 whichfork
, &icur
, &cur
, &del
,
5364 div_u64_rem(del
.br_startblock
, mp
->m_sb
.sb_rextsize
, &mod
);
5367 * Realtime extent is lined up at the end but not
5368 * at the front. We'll get rid of full extents if
5371 mod
= mp
->m_sb
.sb_rextsize
- mod
;
5372 if (del
.br_blockcount
> mod
) {
5373 del
.br_blockcount
-= mod
;
5374 del
.br_startoff
+= mod
;
5375 del
.br_startblock
+= mod
;
5376 } else if (del
.br_startoff
== start
&&
5377 (del
.br_state
== XFS_EXT_UNWRITTEN
||
5378 tp
->t_blk_res
== 0)) {
5380 * Can't make it unwritten. There isn't
5381 * a full extent here so just skip it.
5383 ASSERT(end
>= del
.br_blockcount
);
5384 end
-= del
.br_blockcount
;
5385 if (got
.br_startoff
> end
&&
5386 !xfs_iext_prev_extent(ifp
, &icur
, &got
)) {
5391 } else if (del
.br_state
== XFS_EXT_UNWRITTEN
) {
5392 struct xfs_bmbt_irec prev
;
5395 * This one is already unwritten.
5396 * It must have a written left neighbor.
5397 * Unwrite the killed part of that one and
5400 if (!xfs_iext_prev_extent(ifp
, &icur
, &prev
))
5402 ASSERT(prev
.br_state
== XFS_EXT_NORM
);
5403 ASSERT(!isnullstartblock(prev
.br_startblock
));
5404 ASSERT(del
.br_startblock
==
5405 prev
.br_startblock
+ prev
.br_blockcount
);
5406 if (prev
.br_startoff
< start
) {
5407 mod
= start
- prev
.br_startoff
;
5408 prev
.br_blockcount
-= mod
;
5409 prev
.br_startblock
+= mod
;
5410 prev
.br_startoff
= start
;
5412 prev
.br_state
= XFS_EXT_UNWRITTEN
;
5413 error
= xfs_bmap_add_extent_unwritten_real(tp
,
5414 ip
, whichfork
, &icur
, &cur
,
5420 ASSERT(del
.br_state
== XFS_EXT_NORM
);
5421 del
.br_state
= XFS_EXT_UNWRITTEN
;
5422 error
= xfs_bmap_add_extent_unwritten_real(tp
,
5423 ip
, whichfork
, &icur
, &cur
,
5433 error
= xfs_bmap_del_extent_delay(ip
, whichfork
, &icur
,
5436 error
= xfs_bmap_del_extent_real(ip
, tp
, &icur
, cur
,
5437 &del
, &tmp_logflags
, whichfork
,
5439 logflags
|= tmp_logflags
;
5445 max_len
-= del
.br_blockcount
;
5446 end
= del
.br_startoff
- 1;
5449 * If not done go on to the next (previous) record.
5451 if (end
!= (xfs_fileoff_t
)-1 && end
>= start
) {
5452 if (!xfs_iext_get_extent(ifp
, &icur
, &got
) ||
5453 (got
.br_startoff
> end
&&
5454 !xfs_iext_prev_extent(ifp
, &icur
, &got
))) {
5461 if (done
|| end
== (xfs_fileoff_t
)-1 || end
< start
)
5464 *rlen
= end
- start
+ 1;
5467 * Convert to a btree if necessary.
5469 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
5470 ASSERT(cur
== NULL
);
5471 error
= xfs_bmap_extents_to_btree(tp
, ip
, &cur
, 0,
5472 &tmp_logflags
, whichfork
);
5473 logflags
|= tmp_logflags
;
5475 error
= xfs_bmap_btree_to_extents(tp
, ip
, cur
, &logflags
,
5481 * Log everything. Do this after conversion, there's no point in
5482 * logging the extent records if we've converted to btree format.
5484 if ((logflags
& xfs_ilog_fext(whichfork
)) &&
5485 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
5486 logflags
&= ~xfs_ilog_fext(whichfork
);
5487 else if ((logflags
& xfs_ilog_fbroot(whichfork
)) &&
5488 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)
5489 logflags
&= ~xfs_ilog_fbroot(whichfork
);
5491 * Log inode even in the error case, if the transaction
5492 * is dirty we'll need to shut down the filesystem.
5495 xfs_trans_log_inode(tp
, ip
, logflags
);
5498 cur
->bc_private
.b
.allocated
= 0;
5499 xfs_btree_del_cursor(cur
, error
);
5504 /* Unmap a range of a file. */
5508 struct xfs_inode
*ip
,
5517 error
= __xfs_bunmapi(tp
, ip
, bno
, &len
, flags
, nexts
);
5523 * Determine whether an extent shift can be accomplished by a merge with the
5524 * extent that precedes the target hole of the shift.
5528 struct xfs_bmbt_irec
*left
, /* preceding extent */
5529 struct xfs_bmbt_irec
*got
, /* current extent to shift */
5530 xfs_fileoff_t shift
) /* shift fsb */
5532 xfs_fileoff_t startoff
;
5534 startoff
= got
->br_startoff
- shift
;
5537 * The extent, once shifted, must be adjacent in-file and on-disk with
5538 * the preceding extent.
5540 if ((left
->br_startoff
+ left
->br_blockcount
!= startoff
) ||
5541 (left
->br_startblock
+ left
->br_blockcount
!= got
->br_startblock
) ||
5542 (left
->br_state
!= got
->br_state
) ||
5543 (left
->br_blockcount
+ got
->br_blockcount
> MAXEXTLEN
))
5550 * A bmap extent shift adjusts the file offset of an extent to fill a preceding
5551 * hole in the file. If an extent shift would result in the extent being fully
5552 * adjacent to the extent that currently precedes the hole, we can merge with
5553 * the preceding extent rather than do the shift.
5555 * This function assumes the caller has verified a shift-by-merge is possible
5556 * with the provided extents via xfs_bmse_can_merge().
5560 struct xfs_trans
*tp
,
5561 struct xfs_inode
*ip
,
5563 xfs_fileoff_t shift
, /* shift fsb */
5564 struct xfs_iext_cursor
*icur
,
5565 struct xfs_bmbt_irec
*got
, /* extent to shift */
5566 struct xfs_bmbt_irec
*left
, /* preceding extent */
5567 struct xfs_btree_cur
*cur
,
5568 int *logflags
) /* output */
5570 struct xfs_bmbt_irec
new;
5571 xfs_filblks_t blockcount
;
5573 struct xfs_mount
*mp
= ip
->i_mount
;
5575 blockcount
= left
->br_blockcount
+ got
->br_blockcount
;
5577 ASSERT(xfs_isilocked(ip
, XFS_IOLOCK_EXCL
));
5578 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
5579 ASSERT(xfs_bmse_can_merge(left
, got
, shift
));
5582 new.br_blockcount
= blockcount
;
5585 * Update the on-disk extent count, the btree if necessary and log the
5588 XFS_IFORK_NEXT_SET(ip
, whichfork
,
5589 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
5590 *logflags
|= XFS_ILOG_CORE
;
5592 *logflags
|= XFS_ILOG_DEXT
;
5596 /* lookup and remove the extent to merge */
5597 error
= xfs_bmbt_lookup_eq(cur
, got
, &i
);
5600 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
5602 error
= xfs_btree_delete(cur
, &i
);
5605 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
5607 /* lookup and update size of the previous extent */
5608 error
= xfs_bmbt_lookup_eq(cur
, left
, &i
);
5611 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
5613 error
= xfs_bmbt_update(cur
, &new);
5618 xfs_iext_remove(ip
, icur
, 0);
5619 xfs_iext_prev(XFS_IFORK_PTR(ip
, whichfork
), icur
);
5620 xfs_iext_update_extent(ip
, xfs_bmap_fork_to_state(whichfork
), icur
,
5623 /* update reverse mapping. rmap functions merge the rmaps for us */
5624 xfs_rmap_unmap_extent(tp
, ip
, whichfork
, got
);
5625 memcpy(&new, got
, sizeof(new));
5626 new.br_startoff
= left
->br_startoff
+ left
->br_blockcount
;
5627 xfs_rmap_map_extent(tp
, ip
, whichfork
, &new);
5632 xfs_bmap_shift_update_extent(
5633 struct xfs_trans
*tp
,
5634 struct xfs_inode
*ip
,
5636 struct xfs_iext_cursor
*icur
,
5637 struct xfs_bmbt_irec
*got
,
5638 struct xfs_btree_cur
*cur
,
5640 xfs_fileoff_t startoff
)
5642 struct xfs_mount
*mp
= ip
->i_mount
;
5643 struct xfs_bmbt_irec prev
= *got
;
5646 *logflags
|= XFS_ILOG_CORE
;
5648 got
->br_startoff
= startoff
;
5651 error
= xfs_bmbt_lookup_eq(cur
, &prev
, &i
);
5654 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
5656 error
= xfs_bmbt_update(cur
, got
);
5660 *logflags
|= XFS_ILOG_DEXT
;
5663 xfs_iext_update_extent(ip
, xfs_bmap_fork_to_state(whichfork
), icur
,
5666 /* update reverse mapping */
5667 xfs_rmap_unmap_extent(tp
, ip
, whichfork
, &prev
);
5668 xfs_rmap_map_extent(tp
, ip
, whichfork
, got
);
5673 xfs_bmap_collapse_extents(
5674 struct xfs_trans
*tp
,
5675 struct xfs_inode
*ip
,
5676 xfs_fileoff_t
*next_fsb
,
5677 xfs_fileoff_t offset_shift_fsb
,
5680 int whichfork
= XFS_DATA_FORK
;
5681 struct xfs_mount
*mp
= ip
->i_mount
;
5682 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5683 struct xfs_btree_cur
*cur
= NULL
;
5684 struct xfs_bmbt_irec got
, prev
;
5685 struct xfs_iext_cursor icur
;
5686 xfs_fileoff_t new_startoff
;
5690 if (unlikely(XFS_TEST_ERROR(
5691 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
5692 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
5693 mp
, XFS_ERRTAG_BMAPIFORMAT
))) {
5694 XFS_ERROR_REPORT(__func__
, XFS_ERRLEVEL_LOW
, mp
);
5695 return -EFSCORRUPTED
;
5698 if (XFS_FORCED_SHUTDOWN(mp
))
5701 ASSERT(xfs_isilocked(ip
, XFS_IOLOCK_EXCL
| XFS_ILOCK_EXCL
));
5703 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
5704 error
= xfs_iread_extents(tp
, ip
, whichfork
);
5709 if (ifp
->if_flags
& XFS_IFBROOT
) {
5710 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
5711 cur
->bc_private
.b
.flags
= 0;
5714 if (!xfs_iext_lookup_extent(ip
, ifp
, *next_fsb
, &icur
, &got
)) {
5718 XFS_WANT_CORRUPTED_GOTO(mp
, !isnullstartblock(got
.br_startblock
),
5721 new_startoff
= got
.br_startoff
- offset_shift_fsb
;
5722 if (xfs_iext_peek_prev_extent(ifp
, &icur
, &prev
)) {
5723 if (new_startoff
< prev
.br_startoff
+ prev
.br_blockcount
) {
5728 if (xfs_bmse_can_merge(&prev
, &got
, offset_shift_fsb
)) {
5729 error
= xfs_bmse_merge(tp
, ip
, whichfork
,
5730 offset_shift_fsb
, &icur
, &got
, &prev
,
5737 if (got
.br_startoff
< offset_shift_fsb
) {
5743 error
= xfs_bmap_shift_update_extent(tp
, ip
, whichfork
, &icur
, &got
,
5744 cur
, &logflags
, new_startoff
);
5749 if (!xfs_iext_next_extent(ifp
, &icur
, &got
)) {
5754 *next_fsb
= got
.br_startoff
;
5757 xfs_btree_del_cursor(cur
, error
);
5759 xfs_trans_log_inode(tp
, ip
, logflags
);
5763 /* Make sure we won't be right-shifting an extent past the maximum bound. */
5765 xfs_bmap_can_insert_extents(
5766 struct xfs_inode
*ip
,
5768 xfs_fileoff_t shift
)
5770 struct xfs_bmbt_irec got
;
5774 ASSERT(xfs_isilocked(ip
, XFS_IOLOCK_EXCL
));
5776 if (XFS_FORCED_SHUTDOWN(ip
->i_mount
))
5779 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
5780 error
= xfs_bmap_last_extent(NULL
, ip
, XFS_DATA_FORK
, &got
, &is_empty
);
5781 if (!error
&& !is_empty
&& got
.br_startoff
>= off
&&
5782 ((got
.br_startoff
+ shift
) & BMBT_STARTOFF_MASK
) < got
.br_startoff
)
5784 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
5790 xfs_bmap_insert_extents(
5791 struct xfs_trans
*tp
,
5792 struct xfs_inode
*ip
,
5793 xfs_fileoff_t
*next_fsb
,
5794 xfs_fileoff_t offset_shift_fsb
,
5796 xfs_fileoff_t stop_fsb
)
5798 int whichfork
= XFS_DATA_FORK
;
5799 struct xfs_mount
*mp
= ip
->i_mount
;
5800 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5801 struct xfs_btree_cur
*cur
= NULL
;
5802 struct xfs_bmbt_irec got
, next
;
5803 struct xfs_iext_cursor icur
;
5804 xfs_fileoff_t new_startoff
;
5808 if (unlikely(XFS_TEST_ERROR(
5809 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
5810 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
5811 mp
, XFS_ERRTAG_BMAPIFORMAT
))) {
5812 XFS_ERROR_REPORT(__func__
, XFS_ERRLEVEL_LOW
, mp
);
5813 return -EFSCORRUPTED
;
5816 if (XFS_FORCED_SHUTDOWN(mp
))
5819 ASSERT(xfs_isilocked(ip
, XFS_IOLOCK_EXCL
| XFS_ILOCK_EXCL
));
5821 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
5822 error
= xfs_iread_extents(tp
, ip
, whichfork
);
5827 if (ifp
->if_flags
& XFS_IFBROOT
) {
5828 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
5829 cur
->bc_private
.b
.flags
= 0;
5832 if (*next_fsb
== NULLFSBLOCK
) {
5833 xfs_iext_last(ifp
, &icur
);
5834 if (!xfs_iext_get_extent(ifp
, &icur
, &got
) ||
5835 stop_fsb
> got
.br_startoff
) {
5840 if (!xfs_iext_lookup_extent(ip
, ifp
, *next_fsb
, &icur
, &got
)) {
5845 XFS_WANT_CORRUPTED_GOTO(mp
, !isnullstartblock(got
.br_startblock
),
5848 if (stop_fsb
>= got
.br_startoff
+ got
.br_blockcount
) {
5853 new_startoff
= got
.br_startoff
+ offset_shift_fsb
;
5854 if (xfs_iext_peek_next_extent(ifp
, &icur
, &next
)) {
5855 if (new_startoff
+ got
.br_blockcount
> next
.br_startoff
) {
5861 * Unlike a left shift (which involves a hole punch), a right
5862 * shift does not modify extent neighbors in any way. We should
5863 * never find mergeable extents in this scenario. Check anyways
5864 * and warn if we encounter two extents that could be one.
5866 if (xfs_bmse_can_merge(&got
, &next
, offset_shift_fsb
))
5870 error
= xfs_bmap_shift_update_extent(tp
, ip
, whichfork
, &icur
, &got
,
5871 cur
, &logflags
, new_startoff
);
5875 if (!xfs_iext_prev_extent(ifp
, &icur
, &got
) ||
5876 stop_fsb
>= got
.br_startoff
+ got
.br_blockcount
) {
5881 *next_fsb
= got
.br_startoff
;
5884 xfs_btree_del_cursor(cur
, error
);
5886 xfs_trans_log_inode(tp
, ip
, logflags
);
5891 * Splits an extent into two extents at split_fsb block such that it is the
5892 * first block of the current_ext. @ext is a target extent to be split.
5893 * @split_fsb is a block where the extents is split. If split_fsb lies in a
5894 * hole or the first block of extents, just return 0.
5897 xfs_bmap_split_extent_at(
5898 struct xfs_trans
*tp
,
5899 struct xfs_inode
*ip
,
5900 xfs_fileoff_t split_fsb
)
5902 int whichfork
= XFS_DATA_FORK
;
5903 struct xfs_btree_cur
*cur
= NULL
;
5904 struct xfs_bmbt_irec got
;
5905 struct xfs_bmbt_irec
new; /* split extent */
5906 struct xfs_mount
*mp
= ip
->i_mount
;
5907 struct xfs_ifork
*ifp
;
5908 xfs_fsblock_t gotblkcnt
; /* new block count for got */
5909 struct xfs_iext_cursor icur
;
5914 if (unlikely(XFS_TEST_ERROR(
5915 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
5916 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
5917 mp
, XFS_ERRTAG_BMAPIFORMAT
))) {
5918 XFS_ERROR_REPORT("xfs_bmap_split_extent_at",
5919 XFS_ERRLEVEL_LOW
, mp
);
5920 return -EFSCORRUPTED
;
5923 if (XFS_FORCED_SHUTDOWN(mp
))
5926 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5927 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
5928 /* Read in all the extents */
5929 error
= xfs_iread_extents(tp
, ip
, whichfork
);
5935 * If there are not extents, or split_fsb lies in a hole we are done.
5937 if (!xfs_iext_lookup_extent(ip
, ifp
, split_fsb
, &icur
, &got
) ||
5938 got
.br_startoff
>= split_fsb
)
5941 gotblkcnt
= split_fsb
- got
.br_startoff
;
5942 new.br_startoff
= split_fsb
;
5943 new.br_startblock
= got
.br_startblock
+ gotblkcnt
;
5944 new.br_blockcount
= got
.br_blockcount
- gotblkcnt
;
5945 new.br_state
= got
.br_state
;
5947 if (ifp
->if_flags
& XFS_IFBROOT
) {
5948 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
5949 cur
->bc_private
.b
.flags
= 0;
5950 error
= xfs_bmbt_lookup_eq(cur
, &got
, &i
);
5953 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, del_cursor
);
5956 got
.br_blockcount
= gotblkcnt
;
5957 xfs_iext_update_extent(ip
, xfs_bmap_fork_to_state(whichfork
), &icur
,
5960 logflags
= XFS_ILOG_CORE
;
5962 error
= xfs_bmbt_update(cur
, &got
);
5966 logflags
|= XFS_ILOG_DEXT
;
5968 /* Add new extent */
5969 xfs_iext_next(ifp
, &icur
);
5970 xfs_iext_insert(ip
, &icur
, &new, 0);
5971 XFS_IFORK_NEXT_SET(ip
, whichfork
,
5972 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
5975 error
= xfs_bmbt_lookup_eq(cur
, &new, &i
);
5978 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, del_cursor
);
5979 error
= xfs_btree_insert(cur
, &i
);
5982 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, del_cursor
);
5986 * Convert to a btree if necessary.
5988 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
5989 int tmp_logflags
; /* partial log flag return val */
5991 ASSERT(cur
== NULL
);
5992 error
= xfs_bmap_extents_to_btree(tp
, ip
, &cur
, 0,
5993 &tmp_logflags
, whichfork
);
5994 logflags
|= tmp_logflags
;
5999 cur
->bc_private
.b
.allocated
= 0;
6000 xfs_btree_del_cursor(cur
, error
);
6004 xfs_trans_log_inode(tp
, ip
, logflags
);
6009 xfs_bmap_split_extent(
6010 struct xfs_inode
*ip
,
6011 xfs_fileoff_t split_fsb
)
6013 struct xfs_mount
*mp
= ip
->i_mount
;
6014 struct xfs_trans
*tp
;
6017 error
= xfs_trans_alloc(mp
, &M_RES(mp
)->tr_write
,
6018 XFS_DIOSTRAT_SPACE_RES(mp
, 0), 0, 0, &tp
);
6022 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
6023 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
);
6025 error
= xfs_bmap_split_extent_at(tp
, ip
, split_fsb
);
6029 return xfs_trans_commit(tp
);
6032 xfs_trans_cancel(tp
);
6036 /* Deferred mapping is only for real extents in the data fork. */
6038 xfs_bmap_is_update_needed(
6039 struct xfs_bmbt_irec
*bmap
)
6041 return bmap
->br_startblock
!= HOLESTARTBLOCK
&&
6042 bmap
->br_startblock
!= DELAYSTARTBLOCK
;
6045 /* Record a bmap intent. */
6048 struct xfs_trans
*tp
,
6049 enum xfs_bmap_intent_type type
,
6050 struct xfs_inode
*ip
,
6052 struct xfs_bmbt_irec
*bmap
)
6054 struct xfs_bmap_intent
*bi
;
6056 trace_xfs_bmap_defer(tp
->t_mountp
,
6057 XFS_FSB_TO_AGNO(tp
->t_mountp
, bmap
->br_startblock
),
6059 XFS_FSB_TO_AGBNO(tp
->t_mountp
, bmap
->br_startblock
),
6060 ip
->i_ino
, whichfork
,
6062 bmap
->br_blockcount
,
6065 bi
= kmem_alloc(sizeof(struct xfs_bmap_intent
), KM_NOFS
);
6066 INIT_LIST_HEAD(&bi
->bi_list
);
6069 bi
->bi_whichfork
= whichfork
;
6070 bi
->bi_bmap
= *bmap
;
6072 xfs_defer_add(tp
, XFS_DEFER_OPS_TYPE_BMAP
, &bi
->bi_list
);
6076 /* Map an extent into a file. */
6078 xfs_bmap_map_extent(
6079 struct xfs_trans
*tp
,
6080 struct xfs_inode
*ip
,
6081 struct xfs_bmbt_irec
*PREV
)
6083 if (!xfs_bmap_is_update_needed(PREV
))
6086 __xfs_bmap_add(tp
, XFS_BMAP_MAP
, ip
, XFS_DATA_FORK
, PREV
);
6089 /* Unmap an extent out of a file. */
6091 xfs_bmap_unmap_extent(
6092 struct xfs_trans
*tp
,
6093 struct xfs_inode
*ip
,
6094 struct xfs_bmbt_irec
*PREV
)
6096 if (!xfs_bmap_is_update_needed(PREV
))
6099 __xfs_bmap_add(tp
, XFS_BMAP_UNMAP
, ip
, XFS_DATA_FORK
, PREV
);
6103 * Process one of the deferred bmap operations. We pass back the
6104 * btree cursor to maintain our lock on the bmapbt between calls.
6107 xfs_bmap_finish_one(
6108 struct xfs_trans
*tp
,
6109 struct xfs_inode
*ip
,
6110 enum xfs_bmap_intent_type type
,
6112 xfs_fileoff_t startoff
,
6113 xfs_fsblock_t startblock
,
6114 xfs_filblks_t
*blockcount
,
6119 ASSERT(tp
->t_firstblock
== NULLFSBLOCK
);
6121 trace_xfs_bmap_deferred(tp
->t_mountp
,
6122 XFS_FSB_TO_AGNO(tp
->t_mountp
, startblock
), type
,
6123 XFS_FSB_TO_AGBNO(tp
->t_mountp
, startblock
),
6124 ip
->i_ino
, whichfork
, startoff
, *blockcount
, state
);
6126 if (WARN_ON_ONCE(whichfork
!= XFS_DATA_FORK
))
6127 return -EFSCORRUPTED
;
6129 if (XFS_TEST_ERROR(false, tp
->t_mountp
,
6130 XFS_ERRTAG_BMAP_FINISH_ONE
))
6135 error
= xfs_bmapi_remap(tp
, ip
, startoff
, *blockcount
,
6139 case XFS_BMAP_UNMAP
:
6140 error
= __xfs_bunmapi(tp
, ip
, startoff
, blockcount
,
6141 XFS_BMAPI_REMAP
, 1);
6145 error
= -EFSCORRUPTED
;
6151 /* Check that an inode's extent does not have invalid flags or bad ranges. */
6153 xfs_bmap_validate_extent(
6154 struct xfs_inode
*ip
,
6156 struct xfs_bmbt_irec
*irec
)
6158 struct xfs_mount
*mp
= ip
->i_mount
;
6159 xfs_fsblock_t endfsb
;
6162 isrt
= XFS_IS_REALTIME_INODE(ip
);
6163 endfsb
= irec
->br_startblock
+ irec
->br_blockcount
- 1;
6165 if (!xfs_verify_rtbno(mp
, irec
->br_startblock
))
6166 return __this_address
;
6167 if (!xfs_verify_rtbno(mp
, endfsb
))
6168 return __this_address
;
6170 if (!xfs_verify_fsbno(mp
, irec
->br_startblock
))
6171 return __this_address
;
6172 if (!xfs_verify_fsbno(mp
, endfsb
))
6173 return __this_address
;
6174 if (XFS_FSB_TO_AGNO(mp
, irec
->br_startblock
) !=
6175 XFS_FSB_TO_AGNO(mp
, endfsb
))
6176 return __this_address
;
6178 if (irec
->br_state
!= XFS_EXT_NORM
&& whichfork
!= XFS_DATA_FORK
)
6179 return __this_address
;