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_trans
*tp
,
789 struct xfs_inode
*ip
,
792 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
794 ASSERT(whichfork
!= XFS_COW_FORK
);
795 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
796 ASSERT(ifp
->if_bytes
== 0);
797 ASSERT(XFS_IFORK_NEXTENTS(ip
, whichfork
) == 0);
799 xfs_bmap_forkoff_reset(ip
, whichfork
);
800 ifp
->if_flags
&= ~XFS_IFINLINE
;
801 ifp
->if_flags
|= XFS_IFEXTENTS
;
802 ifp
->if_u1
.if_root
= NULL
;
804 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_EXTENTS
);
805 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
809 STATIC
int /* error */
810 xfs_bmap_local_to_extents(
811 xfs_trans_t
*tp
, /* transaction pointer */
812 xfs_inode_t
*ip
, /* incore inode pointer */
813 xfs_extlen_t total
, /* total blocks needed by transaction */
814 int *logflagsp
, /* inode logging flags */
816 void (*init_fn
)(struct xfs_trans
*tp
,
818 struct xfs_inode
*ip
,
819 struct xfs_ifork
*ifp
))
822 int flags
; /* logging flags returned */
823 struct xfs_ifork
*ifp
; /* inode fork pointer */
824 xfs_alloc_arg_t args
; /* allocation arguments */
825 xfs_buf_t
*bp
; /* buffer for extent block */
826 struct xfs_bmbt_irec rec
;
827 struct xfs_iext_cursor icur
;
830 * We don't want to deal with the case of keeping inode data inline yet.
831 * So sending the data fork of a regular inode is invalid.
833 ASSERT(!(S_ISREG(VFS_I(ip
)->i_mode
) && whichfork
== XFS_DATA_FORK
));
834 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
835 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
837 if (!ifp
->if_bytes
) {
838 xfs_bmap_local_to_extents_empty(tp
, ip
, whichfork
);
839 flags
= XFS_ILOG_CORE
;
845 ASSERT((ifp
->if_flags
& (XFS_IFINLINE
|XFS_IFEXTENTS
)) == XFS_IFINLINE
);
846 memset(&args
, 0, sizeof(args
));
848 args
.mp
= ip
->i_mount
;
849 xfs_rmap_ino_owner(&args
.oinfo
, ip
->i_ino
, whichfork
, 0);
851 * Allocate a block. We know we need only one, since the
852 * file currently fits in an inode.
854 if (tp
->t_firstblock
== NULLFSBLOCK
) {
855 args
.fsbno
= XFS_INO_TO_FSB(args
.mp
, ip
->i_ino
);
856 args
.type
= XFS_ALLOCTYPE_START_BNO
;
858 args
.fsbno
= tp
->t_firstblock
;
859 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
862 args
.minlen
= args
.maxlen
= args
.prod
= 1;
863 error
= xfs_alloc_vextent(&args
);
867 /* Can't fail, the space was reserved. */
868 ASSERT(args
.fsbno
!= NULLFSBLOCK
);
869 ASSERT(args
.len
== 1);
870 tp
->t_firstblock
= args
.fsbno
;
871 bp
= xfs_btree_get_bufl(args
.mp
, tp
, args
.fsbno
);
874 * Initialize the block, copy the data and log the remote buffer.
876 * The callout is responsible for logging because the remote format
877 * might differ from the local format and thus we don't know how much to
878 * log here. Note that init_fn must also set the buffer log item type
881 init_fn(tp
, bp
, ip
, ifp
);
883 /* account for the change in fork size */
884 xfs_idata_realloc(ip
, -ifp
->if_bytes
, whichfork
);
885 xfs_bmap_local_to_extents_empty(tp
, ip
, whichfork
);
886 flags
|= XFS_ILOG_CORE
;
888 ifp
->if_u1
.if_root
= NULL
;
892 rec
.br_startblock
= args
.fsbno
;
893 rec
.br_blockcount
= 1;
894 rec
.br_state
= XFS_EXT_NORM
;
895 xfs_iext_first(ifp
, &icur
);
896 xfs_iext_insert(ip
, &icur
, &rec
, 0);
898 XFS_IFORK_NEXT_SET(ip
, whichfork
, 1);
899 ip
->i_d
.di_nblocks
= 1;
900 xfs_trans_mod_dquot_byino(tp
, ip
,
901 XFS_TRANS_DQ_BCOUNT
, 1L);
902 flags
|= xfs_ilog_fext(whichfork
);
910 * Called from xfs_bmap_add_attrfork to handle btree format files.
912 STATIC
int /* error */
913 xfs_bmap_add_attrfork_btree(
914 xfs_trans_t
*tp
, /* transaction pointer */
915 xfs_inode_t
*ip
, /* incore inode pointer */
916 int *flags
) /* inode logging flags */
918 xfs_btree_cur_t
*cur
; /* btree cursor */
919 int error
; /* error return value */
920 xfs_mount_t
*mp
; /* file system mount struct */
921 int stat
; /* newroot status */
924 if (ip
->i_df
.if_broot_bytes
<= XFS_IFORK_DSIZE(ip
))
925 *flags
|= XFS_ILOG_DBROOT
;
927 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, XFS_DATA_FORK
);
928 error
= xfs_bmbt_lookup_first(cur
, &stat
);
931 /* must be at least one entry */
932 XFS_WANT_CORRUPTED_GOTO(mp
, stat
== 1, error0
);
933 if ((error
= xfs_btree_new_iroot(cur
, flags
, &stat
)))
936 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
939 cur
->bc_private
.b
.allocated
= 0;
940 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
944 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
949 * Called from xfs_bmap_add_attrfork to handle extents format files.
951 STATIC
int /* error */
952 xfs_bmap_add_attrfork_extents(
953 struct xfs_trans
*tp
, /* transaction pointer */
954 struct xfs_inode
*ip
, /* incore inode pointer */
955 int *flags
) /* inode logging flags */
957 xfs_btree_cur_t
*cur
; /* bmap btree cursor */
958 int error
; /* error return value */
960 if (ip
->i_d
.di_nextents
* sizeof(xfs_bmbt_rec_t
) <= XFS_IFORK_DSIZE(ip
))
963 error
= xfs_bmap_extents_to_btree(tp
, ip
, &cur
, 0, flags
,
966 cur
->bc_private
.b
.allocated
= 0;
967 xfs_btree_del_cursor(cur
, error
);
973 * Called from xfs_bmap_add_attrfork to handle local format files. Each
974 * different data fork content type needs a different callout to do the
975 * conversion. Some are basic and only require special block initialisation
976 * callouts for the data formating, others (directories) are so specialised they
977 * handle everything themselves.
979 * XXX (dgc): investigate whether directory conversion can use the generic
980 * formatting callout. It should be possible - it's just a very complex
983 STATIC
int /* error */
984 xfs_bmap_add_attrfork_local(
985 struct xfs_trans
*tp
, /* transaction pointer */
986 struct xfs_inode
*ip
, /* incore inode pointer */
987 int *flags
) /* inode logging flags */
989 struct xfs_da_args dargs
; /* args for dir/attr code */
991 if (ip
->i_df
.if_bytes
<= XFS_IFORK_DSIZE(ip
))
994 if (S_ISDIR(VFS_I(ip
)->i_mode
)) {
995 memset(&dargs
, 0, sizeof(dargs
));
996 dargs
.geo
= ip
->i_mount
->m_dir_geo
;
998 dargs
.total
= dargs
.geo
->fsbcount
;
999 dargs
.whichfork
= XFS_DATA_FORK
;
1001 return xfs_dir2_sf_to_block(&dargs
);
1004 if (S_ISLNK(VFS_I(ip
)->i_mode
))
1005 return xfs_bmap_local_to_extents(tp
, ip
, 1, flags
,
1007 xfs_symlink_local_to_remote
);
1009 /* should only be called for types that support local format data */
1011 return -EFSCORRUPTED
;
1014 /* Set an inode attr fork off based on the format */
1016 xfs_bmap_set_attrforkoff(
1017 struct xfs_inode
*ip
,
1021 switch (ip
->i_d
.di_format
) {
1022 case XFS_DINODE_FMT_DEV
:
1023 ip
->i_d
.di_forkoff
= roundup(sizeof(xfs_dev_t
), 8) >> 3;
1025 case XFS_DINODE_FMT_LOCAL
:
1026 case XFS_DINODE_FMT_EXTENTS
:
1027 case XFS_DINODE_FMT_BTREE
:
1028 ip
->i_d
.di_forkoff
= xfs_attr_shortform_bytesfit(ip
, size
);
1029 if (!ip
->i_d
.di_forkoff
)
1030 ip
->i_d
.di_forkoff
= xfs_default_attroffset(ip
) >> 3;
1031 else if ((ip
->i_mount
->m_flags
& XFS_MOUNT_ATTR2
) && version
)
1043 * Convert inode from non-attributed to attributed.
1044 * Must not be in a transaction, ip must not be locked.
1046 int /* error code */
1047 xfs_bmap_add_attrfork(
1048 xfs_inode_t
*ip
, /* incore inode pointer */
1049 int size
, /* space new attribute needs */
1050 int rsvd
) /* xact may use reserved blks */
1052 xfs_mount_t
*mp
; /* mount structure */
1053 xfs_trans_t
*tp
; /* transaction pointer */
1054 int blks
; /* space reservation */
1055 int version
= 1; /* superblock attr version */
1056 int logflags
; /* logging flags */
1057 int error
; /* error return value */
1059 ASSERT(XFS_IFORK_Q(ip
) == 0);
1062 ASSERT(!XFS_NOT_DQATTACHED(mp
, ip
));
1064 blks
= XFS_ADDAFORK_SPACE_RES(mp
);
1066 error
= xfs_trans_alloc(mp
, &M_RES(mp
)->tr_addafork
, blks
, 0,
1067 rsvd
? XFS_TRANS_RESERVE
: 0, &tp
);
1071 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
1072 error
= xfs_trans_reserve_quota_nblks(tp
, ip
, blks
, 0, rsvd
?
1073 XFS_QMOPT_RES_REGBLKS
| XFS_QMOPT_FORCE_RES
:
1074 XFS_QMOPT_RES_REGBLKS
);
1077 if (XFS_IFORK_Q(ip
))
1079 if (ip
->i_d
.di_anextents
!= 0) {
1080 error
= -EFSCORRUPTED
;
1083 if (ip
->i_d
.di_aformat
!= XFS_DINODE_FMT_EXTENTS
) {
1085 * For inodes coming from pre-6.2 filesystems.
1087 ASSERT(ip
->i_d
.di_aformat
== 0);
1088 ip
->i_d
.di_aformat
= XFS_DINODE_FMT_EXTENTS
;
1091 xfs_trans_ijoin(tp
, ip
, 0);
1092 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
1093 error
= xfs_bmap_set_attrforkoff(ip
, size
, &version
);
1096 ASSERT(ip
->i_afp
== NULL
);
1097 ip
->i_afp
= kmem_zone_zalloc(xfs_ifork_zone
, 0);
1098 ip
->i_afp
->if_flags
= XFS_IFEXTENTS
;
1100 switch (ip
->i_d
.di_format
) {
1101 case XFS_DINODE_FMT_LOCAL
:
1102 error
= xfs_bmap_add_attrfork_local(tp
, ip
, &logflags
);
1104 case XFS_DINODE_FMT_EXTENTS
:
1105 error
= xfs_bmap_add_attrfork_extents(tp
, ip
, &logflags
);
1107 case XFS_DINODE_FMT_BTREE
:
1108 error
= xfs_bmap_add_attrfork_btree(tp
, ip
, &logflags
);
1115 xfs_trans_log_inode(tp
, ip
, logflags
);
1118 if (!xfs_sb_version_hasattr(&mp
->m_sb
) ||
1119 (!xfs_sb_version_hasattr2(&mp
->m_sb
) && version
== 2)) {
1120 bool log_sb
= false;
1122 spin_lock(&mp
->m_sb_lock
);
1123 if (!xfs_sb_version_hasattr(&mp
->m_sb
)) {
1124 xfs_sb_version_addattr(&mp
->m_sb
);
1127 if (!xfs_sb_version_hasattr2(&mp
->m_sb
) && version
== 2) {
1128 xfs_sb_version_addattr2(&mp
->m_sb
);
1131 spin_unlock(&mp
->m_sb_lock
);
1136 error
= xfs_trans_commit(tp
);
1137 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
1141 xfs_trans_cancel(tp
);
1142 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
1147 * Internal and external extent tree search functions.
1151 * Read in extents from a btree-format inode.
1155 struct xfs_trans
*tp
,
1156 struct xfs_inode
*ip
,
1159 struct xfs_mount
*mp
= ip
->i_mount
;
1160 int state
= xfs_bmap_fork_to_state(whichfork
);
1161 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1162 xfs_extnum_t nextents
= XFS_IFORK_NEXTENTS(ip
, whichfork
);
1163 struct xfs_btree_block
*block
= ifp
->if_broot
;
1164 struct xfs_iext_cursor icur
;
1165 struct xfs_bmbt_irec
new;
1173 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
1175 if (unlikely(XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)) {
1176 XFS_ERROR_REPORT(__func__
, XFS_ERRLEVEL_LOW
, mp
);
1177 return -EFSCORRUPTED
;
1181 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
1183 level
= be16_to_cpu(block
->bb_level
);
1184 if (unlikely(level
== 0)) {
1185 XFS_ERROR_REPORT(__func__
, XFS_ERRLEVEL_LOW
, mp
);
1186 return -EFSCORRUPTED
;
1188 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, 1, ifp
->if_broot_bytes
);
1189 bno
= be64_to_cpu(*pp
);
1192 * Go down the tree until leaf level is reached, following the first
1193 * pointer (leftmost) at each level.
1195 while (level
-- > 0) {
1196 error
= xfs_btree_read_bufl(mp
, tp
, bno
, &bp
,
1197 XFS_BMAP_BTREE_REF
, &xfs_bmbt_buf_ops
);
1200 block
= XFS_BUF_TO_BLOCK(bp
);
1203 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, mp
->m_bmap_dmxr
[1]);
1204 bno
= be64_to_cpu(*pp
);
1205 XFS_WANT_CORRUPTED_GOTO(mp
,
1206 xfs_verify_fsbno(mp
, bno
), out_brelse
);
1207 xfs_trans_brelse(tp
, bp
);
1211 * Here with bp and block set to the leftmost leaf node in the tree.
1214 xfs_iext_first(ifp
, &icur
);
1217 * Loop over all leaf nodes. Copy information to the extent records.
1220 xfs_bmbt_rec_t
*frp
;
1221 xfs_fsblock_t nextbno
;
1222 xfs_extnum_t num_recs
;
1224 num_recs
= xfs_btree_get_numrecs(block
);
1225 if (unlikely(i
+ num_recs
> nextents
)) {
1226 xfs_warn(ip
->i_mount
,
1227 "corrupt dinode %Lu, (btree extents).",
1228 (unsigned long long) ip
->i_ino
);
1229 xfs_inode_verifier_error(ip
, -EFSCORRUPTED
,
1230 __func__
, block
, sizeof(*block
),
1232 error
= -EFSCORRUPTED
;
1236 * Read-ahead the next leaf block, if any.
1238 nextbno
= be64_to_cpu(block
->bb_u
.l
.bb_rightsib
);
1239 if (nextbno
!= NULLFSBLOCK
)
1240 xfs_btree_reada_bufl(mp
, nextbno
, 1,
1243 * Copy records into the extent records.
1245 frp
= XFS_BMBT_REC_ADDR(mp
, block
, 1);
1246 for (j
= 0; j
< num_recs
; j
++, frp
++, i
++) {
1249 xfs_bmbt_disk_get_all(frp
, &new);
1250 fa
= xfs_bmap_validate_extent(ip
, whichfork
, &new);
1252 error
= -EFSCORRUPTED
;
1253 xfs_inode_verifier_error(ip
, error
,
1254 "xfs_iread_extents(2)",
1255 frp
, sizeof(*frp
), fa
);
1258 xfs_iext_insert(ip
, &icur
, &new, state
);
1259 trace_xfs_read_extent(ip
, &icur
, state
, _THIS_IP_
);
1260 xfs_iext_next(ifp
, &icur
);
1262 xfs_trans_brelse(tp
, bp
);
1265 * If we've reached the end, stop.
1267 if (bno
== NULLFSBLOCK
)
1269 error
= xfs_btree_read_bufl(mp
, tp
, bno
, &bp
,
1270 XFS_BMAP_BTREE_REF
, &xfs_bmbt_buf_ops
);
1273 block
= XFS_BUF_TO_BLOCK(bp
);
1276 if (i
!= XFS_IFORK_NEXTENTS(ip
, whichfork
)) {
1277 error
= -EFSCORRUPTED
;
1280 ASSERT(i
== xfs_iext_count(ifp
));
1282 ifp
->if_flags
|= XFS_IFEXTENTS
;
1286 xfs_trans_brelse(tp
, bp
);
1288 xfs_iext_destroy(ifp
);
1293 * Returns the relative block number of the first unused block(s) in the given
1294 * fork with at least "len" logically contiguous blocks free. This is the
1295 * lowest-address hole if the fork has holes, else the first block past the end
1296 * of fork. Return 0 if the fork is currently local (in-inode).
1299 xfs_bmap_first_unused(
1300 struct xfs_trans
*tp
, /* transaction pointer */
1301 struct xfs_inode
*ip
, /* incore inode */
1302 xfs_extlen_t len
, /* size of hole to find */
1303 xfs_fileoff_t
*first_unused
, /* unused block */
1304 int whichfork
) /* data or attr fork */
1306 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1307 struct xfs_bmbt_irec got
;
1308 struct xfs_iext_cursor icur
;
1309 xfs_fileoff_t lastaddr
= 0;
1310 xfs_fileoff_t lowest
, max
;
1313 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
||
1314 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
||
1315 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
1317 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
) {
1322 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
1323 error
= xfs_iread_extents(tp
, ip
, whichfork
);
1328 lowest
= max
= *first_unused
;
1329 for_each_xfs_iext(ifp
, &icur
, &got
) {
1331 * See if the hole before this extent will work.
1333 if (got
.br_startoff
>= lowest
+ len
&&
1334 got
.br_startoff
- max
>= len
)
1336 lastaddr
= got
.br_startoff
+ got
.br_blockcount
;
1337 max
= XFS_FILEOFF_MAX(lastaddr
, lowest
);
1340 *first_unused
= max
;
1345 * Returns the file-relative block number of the last block - 1 before
1346 * last_block (input value) in the file.
1347 * This is not based on i_size, it is based on the extent records.
1348 * Returns 0 for local files, as they do not have extent records.
1351 xfs_bmap_last_before(
1352 struct xfs_trans
*tp
, /* transaction pointer */
1353 struct xfs_inode
*ip
, /* incore inode */
1354 xfs_fileoff_t
*last_block
, /* last block */
1355 int whichfork
) /* data or attr fork */
1357 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1358 struct xfs_bmbt_irec got
;
1359 struct xfs_iext_cursor icur
;
1362 switch (XFS_IFORK_FORMAT(ip
, whichfork
)) {
1363 case XFS_DINODE_FMT_LOCAL
:
1366 case XFS_DINODE_FMT_BTREE
:
1367 case XFS_DINODE_FMT_EXTENTS
:
1373 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
1374 error
= xfs_iread_extents(tp
, ip
, whichfork
);
1379 if (!xfs_iext_lookup_extent_before(ip
, ifp
, last_block
, &icur
, &got
))
1385 xfs_bmap_last_extent(
1386 struct xfs_trans
*tp
,
1387 struct xfs_inode
*ip
,
1389 struct xfs_bmbt_irec
*rec
,
1392 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1393 struct xfs_iext_cursor icur
;
1396 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
1397 error
= xfs_iread_extents(tp
, ip
, whichfork
);
1402 xfs_iext_last(ifp
, &icur
);
1403 if (!xfs_iext_get_extent(ifp
, &icur
, rec
))
1411 * Check the last inode extent to determine whether this allocation will result
1412 * in blocks being allocated at the end of the file. When we allocate new data
1413 * blocks at the end of the file which do not start at the previous data block,
1414 * we will try to align the new blocks at stripe unit boundaries.
1416 * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be
1417 * at, or past the EOF.
1421 struct xfs_bmalloca
*bma
,
1424 struct xfs_bmbt_irec rec
;
1429 error
= xfs_bmap_last_extent(NULL
, bma
->ip
, whichfork
, &rec
,
1440 * Check if we are allocation or past the last extent, or at least into
1441 * the last delayed allocated extent.
1443 bma
->aeof
= bma
->offset
>= rec
.br_startoff
+ rec
.br_blockcount
||
1444 (bma
->offset
>= rec
.br_startoff
&&
1445 isnullstartblock(rec
.br_startblock
));
1450 * Returns the file-relative block number of the first block past eof in
1451 * the file. This is not based on i_size, it is based on the extent records.
1452 * Returns 0 for local files, as they do not have extent records.
1455 xfs_bmap_last_offset(
1456 struct xfs_inode
*ip
,
1457 xfs_fileoff_t
*last_block
,
1460 struct xfs_bmbt_irec rec
;
1466 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
)
1469 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
&&
1470 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
1473 error
= xfs_bmap_last_extent(NULL
, ip
, whichfork
, &rec
, &is_empty
);
1474 if (error
|| is_empty
)
1477 *last_block
= rec
.br_startoff
+ rec
.br_blockcount
;
1482 * Returns whether the selected fork of the inode has exactly one
1483 * block or not. For the data fork we check this matches di_size,
1484 * implying the file's range is 0..bsize-1.
1486 int /* 1=>1 block, 0=>otherwise */
1488 xfs_inode_t
*ip
, /* incore inode */
1489 int whichfork
) /* data or attr fork */
1491 struct xfs_ifork
*ifp
; /* inode fork pointer */
1492 int rval
; /* return value */
1493 xfs_bmbt_irec_t s
; /* internal version of extent */
1494 struct xfs_iext_cursor icur
;
1497 if (whichfork
== XFS_DATA_FORK
)
1498 return XFS_ISIZE(ip
) == ip
->i_mount
->m_sb
.sb_blocksize
;
1500 if (XFS_IFORK_NEXTENTS(ip
, whichfork
) != 1)
1502 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
1504 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1505 ASSERT(ifp
->if_flags
& XFS_IFEXTENTS
);
1506 xfs_iext_first(ifp
, &icur
);
1507 xfs_iext_get_extent(ifp
, &icur
, &s
);
1508 rval
= s
.br_startoff
== 0 && s
.br_blockcount
== 1;
1509 if (rval
&& whichfork
== XFS_DATA_FORK
)
1510 ASSERT(XFS_ISIZE(ip
) == ip
->i_mount
->m_sb
.sb_blocksize
);
1515 * Extent tree manipulation functions used during allocation.
1519 * Convert a delayed allocation to a real allocation.
1521 STATIC
int /* error */
1522 xfs_bmap_add_extent_delay_real(
1523 struct xfs_bmalloca
*bma
,
1526 struct xfs_bmbt_irec
*new = &bma
->got
;
1527 int error
; /* error return value */
1528 int i
; /* temp state */
1529 struct xfs_ifork
*ifp
; /* inode fork pointer */
1530 xfs_fileoff_t new_endoff
; /* end offset of new entry */
1531 xfs_bmbt_irec_t r
[3]; /* neighbor extent entries */
1532 /* left is 0, right is 1, prev is 2 */
1533 int rval
=0; /* return value (logging flags) */
1534 int state
= xfs_bmap_fork_to_state(whichfork
);
1535 xfs_filblks_t da_new
; /* new count del alloc blocks used */
1536 xfs_filblks_t da_old
; /* old count del alloc blocks used */
1537 xfs_filblks_t temp
=0; /* value for da_new calculations */
1538 int tmp_rval
; /* partial logging flags */
1539 struct xfs_mount
*mp
;
1540 xfs_extnum_t
*nextents
;
1541 struct xfs_bmbt_irec old
;
1543 mp
= bma
->ip
->i_mount
;
1544 ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
1545 ASSERT(whichfork
!= XFS_ATTR_FORK
);
1546 nextents
= (whichfork
== XFS_COW_FORK
? &bma
->ip
->i_cnextents
:
1547 &bma
->ip
->i_d
.di_nextents
);
1549 ASSERT(!isnullstartblock(new->br_startblock
));
1551 (bma
->cur
->bc_private
.b
.flags
& XFS_BTCUR_BPRV_WASDEL
));
1553 XFS_STATS_INC(mp
, xs_add_exlist
);
1560 * Set up a bunch of variables to make the tests simpler.
1562 xfs_iext_get_extent(ifp
, &bma
->icur
, &PREV
);
1563 new_endoff
= new->br_startoff
+ new->br_blockcount
;
1564 ASSERT(isnullstartblock(PREV
.br_startblock
));
1565 ASSERT(PREV
.br_startoff
<= new->br_startoff
);
1566 ASSERT(PREV
.br_startoff
+ PREV
.br_blockcount
>= new_endoff
);
1568 da_old
= startblockval(PREV
.br_startblock
);
1572 * Set flags determining what part of the previous delayed allocation
1573 * extent is being replaced by a real allocation.
1575 if (PREV
.br_startoff
== new->br_startoff
)
1576 state
|= BMAP_LEFT_FILLING
;
1577 if (PREV
.br_startoff
+ PREV
.br_blockcount
== new_endoff
)
1578 state
|= BMAP_RIGHT_FILLING
;
1581 * Check and set flags if this segment has a left neighbor.
1582 * Don't set contiguous if the combined extent would be too large.
1584 if (xfs_iext_peek_prev_extent(ifp
, &bma
->icur
, &LEFT
)) {
1585 state
|= BMAP_LEFT_VALID
;
1586 if (isnullstartblock(LEFT
.br_startblock
))
1587 state
|= BMAP_LEFT_DELAY
;
1590 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
1591 LEFT
.br_startoff
+ LEFT
.br_blockcount
== new->br_startoff
&&
1592 LEFT
.br_startblock
+ LEFT
.br_blockcount
== new->br_startblock
&&
1593 LEFT
.br_state
== new->br_state
&&
1594 LEFT
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
1595 state
|= BMAP_LEFT_CONTIG
;
1598 * Check and set flags if this segment has a right neighbor.
1599 * Don't set contiguous if the combined extent would be too large.
1600 * Also check for all-three-contiguous being too large.
1602 if (xfs_iext_peek_next_extent(ifp
, &bma
->icur
, &RIGHT
)) {
1603 state
|= BMAP_RIGHT_VALID
;
1604 if (isnullstartblock(RIGHT
.br_startblock
))
1605 state
|= BMAP_RIGHT_DELAY
;
1608 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
1609 new_endoff
== RIGHT
.br_startoff
&&
1610 new->br_startblock
+ new->br_blockcount
== RIGHT
.br_startblock
&&
1611 new->br_state
== RIGHT
.br_state
&&
1612 new->br_blockcount
+ RIGHT
.br_blockcount
<= MAXEXTLEN
&&
1613 ((state
& (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
1614 BMAP_RIGHT_FILLING
)) !=
1615 (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
1616 BMAP_RIGHT_FILLING
) ||
1617 LEFT
.br_blockcount
+ new->br_blockcount
+ RIGHT
.br_blockcount
1619 state
|= BMAP_RIGHT_CONTIG
;
1623 * Switch out based on the FILLING and CONTIG state bits.
1625 switch (state
& (BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
1626 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
)) {
1627 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
1628 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
1630 * Filling in all of a previously delayed allocation extent.
1631 * The left and right neighbors are both contiguous with new.
1633 LEFT
.br_blockcount
+= PREV
.br_blockcount
+ RIGHT
.br_blockcount
;
1635 xfs_iext_remove(bma
->ip
, &bma
->icur
, state
);
1636 xfs_iext_remove(bma
->ip
, &bma
->icur
, state
);
1637 xfs_iext_prev(ifp
, &bma
->icur
);
1638 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &LEFT
);
1641 if (bma
->cur
== NULL
)
1642 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1644 rval
= XFS_ILOG_CORE
;
1645 error
= xfs_bmbt_lookup_eq(bma
->cur
, &RIGHT
, &i
);
1648 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1649 error
= xfs_btree_delete(bma
->cur
, &i
);
1652 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1653 error
= xfs_btree_decrement(bma
->cur
, 0, &i
);
1656 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1657 error
= xfs_bmbt_update(bma
->cur
, &LEFT
);
1663 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
1665 * Filling in all of a previously delayed allocation extent.
1666 * The left neighbor is contiguous, the right is not.
1669 LEFT
.br_blockcount
+= PREV
.br_blockcount
;
1671 xfs_iext_remove(bma
->ip
, &bma
->icur
, state
);
1672 xfs_iext_prev(ifp
, &bma
->icur
);
1673 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &LEFT
);
1675 if (bma
->cur
== NULL
)
1676 rval
= XFS_ILOG_DEXT
;
1679 error
= xfs_bmbt_lookup_eq(bma
->cur
, &old
, &i
);
1682 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1683 error
= xfs_bmbt_update(bma
->cur
, &LEFT
);
1689 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
1691 * Filling in all of a previously delayed allocation extent.
1692 * The right neighbor is contiguous, the left is not. Take care
1693 * with delay -> unwritten extent allocation here because the
1694 * delalloc record we are overwriting is always written.
1696 PREV
.br_startblock
= new->br_startblock
;
1697 PREV
.br_blockcount
+= RIGHT
.br_blockcount
;
1698 PREV
.br_state
= new->br_state
;
1700 xfs_iext_next(ifp
, &bma
->icur
);
1701 xfs_iext_remove(bma
->ip
, &bma
->icur
, state
);
1702 xfs_iext_prev(ifp
, &bma
->icur
);
1703 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &PREV
);
1705 if (bma
->cur
== NULL
)
1706 rval
= XFS_ILOG_DEXT
;
1709 error
= xfs_bmbt_lookup_eq(bma
->cur
, &RIGHT
, &i
);
1712 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1713 error
= xfs_bmbt_update(bma
->cur
, &PREV
);
1719 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
1721 * Filling in all of a previously delayed allocation extent.
1722 * Neither the left nor right neighbors are contiguous with
1725 PREV
.br_startblock
= new->br_startblock
;
1726 PREV
.br_state
= new->br_state
;
1727 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &PREV
);
1730 if (bma
->cur
== NULL
)
1731 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1733 rval
= XFS_ILOG_CORE
;
1734 error
= xfs_bmbt_lookup_eq(bma
->cur
, new, &i
);
1737 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
1738 error
= xfs_btree_insert(bma
->cur
, &i
);
1741 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1745 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
:
1747 * Filling in the first part of a previous delayed allocation.
1748 * The left neighbor is contiguous.
1751 temp
= PREV
.br_blockcount
- new->br_blockcount
;
1752 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
1753 startblockval(PREV
.br_startblock
));
1755 LEFT
.br_blockcount
+= new->br_blockcount
;
1757 PREV
.br_blockcount
= temp
;
1758 PREV
.br_startoff
+= new->br_blockcount
;
1759 PREV
.br_startblock
= nullstartblock(da_new
);
1761 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &PREV
);
1762 xfs_iext_prev(ifp
, &bma
->icur
);
1763 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &LEFT
);
1765 if (bma
->cur
== NULL
)
1766 rval
= XFS_ILOG_DEXT
;
1769 error
= xfs_bmbt_lookup_eq(bma
->cur
, &old
, &i
);
1772 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1773 error
= xfs_bmbt_update(bma
->cur
, &LEFT
);
1779 case BMAP_LEFT_FILLING
:
1781 * Filling in the first part of a previous delayed allocation.
1782 * The left neighbor is not contiguous.
1784 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, new);
1786 if (bma
->cur
== NULL
)
1787 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1789 rval
= XFS_ILOG_CORE
;
1790 error
= xfs_bmbt_lookup_eq(bma
->cur
, new, &i
);
1793 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
1794 error
= xfs_btree_insert(bma
->cur
, &i
);
1797 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1800 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
1801 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
1802 &bma
->cur
, 1, &tmp_rval
, whichfork
);
1808 temp
= PREV
.br_blockcount
- new->br_blockcount
;
1809 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
1810 startblockval(PREV
.br_startblock
) -
1811 (bma
->cur
? bma
->cur
->bc_private
.b
.allocated
: 0));
1813 PREV
.br_startoff
= new_endoff
;
1814 PREV
.br_blockcount
= temp
;
1815 PREV
.br_startblock
= nullstartblock(da_new
);
1816 xfs_iext_next(ifp
, &bma
->icur
);
1817 xfs_iext_insert(bma
->ip
, &bma
->icur
, &PREV
, state
);
1818 xfs_iext_prev(ifp
, &bma
->icur
);
1821 case BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
1823 * Filling in the last part of a previous delayed allocation.
1824 * The right neighbor is contiguous with the new allocation.
1827 RIGHT
.br_startoff
= new->br_startoff
;
1828 RIGHT
.br_startblock
= new->br_startblock
;
1829 RIGHT
.br_blockcount
+= new->br_blockcount
;
1831 if (bma
->cur
== NULL
)
1832 rval
= XFS_ILOG_DEXT
;
1835 error
= xfs_bmbt_lookup_eq(bma
->cur
, &old
, &i
);
1838 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1839 error
= xfs_bmbt_update(bma
->cur
, &RIGHT
);
1844 temp
= PREV
.br_blockcount
- new->br_blockcount
;
1845 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
1846 startblockval(PREV
.br_startblock
));
1848 PREV
.br_blockcount
= temp
;
1849 PREV
.br_startblock
= nullstartblock(da_new
);
1851 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &PREV
);
1852 xfs_iext_next(ifp
, &bma
->icur
);
1853 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &RIGHT
);
1856 case BMAP_RIGHT_FILLING
:
1858 * Filling in the last part of a previous delayed allocation.
1859 * The right neighbor is not contiguous.
1861 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, new);
1863 if (bma
->cur
== NULL
)
1864 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1866 rval
= XFS_ILOG_CORE
;
1867 error
= xfs_bmbt_lookup_eq(bma
->cur
, new, &i
);
1870 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
1871 error
= xfs_btree_insert(bma
->cur
, &i
);
1874 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1877 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
1878 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
1879 &bma
->cur
, 1, &tmp_rval
, whichfork
);
1885 temp
= PREV
.br_blockcount
- new->br_blockcount
;
1886 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
1887 startblockval(PREV
.br_startblock
) -
1888 (bma
->cur
? bma
->cur
->bc_private
.b
.allocated
: 0));
1890 PREV
.br_startblock
= nullstartblock(da_new
);
1891 PREV
.br_blockcount
= temp
;
1892 xfs_iext_insert(bma
->ip
, &bma
->icur
, &PREV
, state
);
1893 xfs_iext_next(ifp
, &bma
->icur
);
1898 * Filling in the middle part of a previous delayed allocation.
1899 * Contiguity is impossible here.
1900 * This case is avoided almost all the time.
1902 * We start with a delayed allocation:
1904 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
1907 * and we are allocating:
1908 * +rrrrrrrrrrrrrrrrr+
1911 * and we set it up for insertion as:
1912 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
1914 * PREV @ idx LEFT RIGHT
1915 * inserted at idx + 1
1919 /* LEFT is the new middle */
1922 /* RIGHT is the new right */
1923 RIGHT
.br_state
= PREV
.br_state
;
1924 RIGHT
.br_startoff
= new_endoff
;
1925 RIGHT
.br_blockcount
=
1926 PREV
.br_startoff
+ PREV
.br_blockcount
- new_endoff
;
1927 RIGHT
.br_startblock
=
1928 nullstartblock(xfs_bmap_worst_indlen(bma
->ip
,
1929 RIGHT
.br_blockcount
));
1932 PREV
.br_blockcount
= new->br_startoff
- PREV
.br_startoff
;
1933 PREV
.br_startblock
=
1934 nullstartblock(xfs_bmap_worst_indlen(bma
->ip
,
1935 PREV
.br_blockcount
));
1936 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &PREV
);
1938 xfs_iext_next(ifp
, &bma
->icur
);
1939 xfs_iext_insert(bma
->ip
, &bma
->icur
, &RIGHT
, state
);
1940 xfs_iext_insert(bma
->ip
, &bma
->icur
, &LEFT
, state
);
1943 if (bma
->cur
== NULL
)
1944 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1946 rval
= XFS_ILOG_CORE
;
1947 error
= xfs_bmbt_lookup_eq(bma
->cur
, new, &i
);
1950 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
1951 error
= xfs_btree_insert(bma
->cur
, &i
);
1954 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1957 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
1958 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
1959 &bma
->cur
, 1, &tmp_rval
, whichfork
);
1965 da_new
= startblockval(PREV
.br_startblock
) +
1966 startblockval(RIGHT
.br_startblock
);
1969 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
1970 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
1971 case BMAP_LEFT_FILLING
| BMAP_RIGHT_CONTIG
:
1972 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
1973 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
1974 case BMAP_LEFT_CONTIG
:
1975 case BMAP_RIGHT_CONTIG
:
1977 * These cases are all impossible.
1982 /* add reverse mapping unless caller opted out */
1983 if (!(bma
->flags
& XFS_BMAPI_NORMAP
))
1984 xfs_rmap_map_extent(bma
->tp
, bma
->ip
, whichfork
, new);
1986 /* convert to a btree if necessary */
1987 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
1988 int tmp_logflags
; /* partial log flag return val */
1990 ASSERT(bma
->cur
== NULL
);
1991 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
1992 &bma
->cur
, da_old
> 0, &tmp_logflags
,
1994 bma
->logflags
|= tmp_logflags
;
1999 if (da_new
!= da_old
)
2000 xfs_mod_delalloc(mp
, (int64_t)da_new
- da_old
);
2003 da_new
+= bma
->cur
->bc_private
.b
.allocated
;
2004 bma
->cur
->bc_private
.b
.allocated
= 0;
2007 /* adjust for changes in reserved delayed indirect blocks */
2008 if (da_new
!= da_old
) {
2009 ASSERT(state
== 0 || da_new
< da_old
);
2010 error
= xfs_mod_fdblocks(mp
, (int64_t)(da_old
- da_new
),
2014 xfs_bmap_check_leaf_extents(bma
->cur
, bma
->ip
, whichfork
);
2016 if (whichfork
!= XFS_COW_FORK
)
2017 bma
->logflags
|= rval
;
2025 * Convert an unwritten allocation to a real allocation or vice versa.
2028 xfs_bmap_add_extent_unwritten_real(
2029 struct xfs_trans
*tp
,
2030 xfs_inode_t
*ip
, /* incore inode pointer */
2032 struct xfs_iext_cursor
*icur
,
2033 xfs_btree_cur_t
**curp
, /* if *curp is null, not a btree */
2034 xfs_bmbt_irec_t
*new, /* new data to add to file extents */
2035 int *logflagsp
) /* inode logging flags */
2037 xfs_btree_cur_t
*cur
; /* btree cursor */
2038 int error
; /* error return value */
2039 int i
; /* temp state */
2040 struct xfs_ifork
*ifp
; /* inode fork pointer */
2041 xfs_fileoff_t new_endoff
; /* end offset of new entry */
2042 xfs_bmbt_irec_t r
[3]; /* neighbor extent entries */
2043 /* left is 0, right is 1, prev is 2 */
2044 int rval
=0; /* return value (logging flags) */
2045 int state
= xfs_bmap_fork_to_state(whichfork
);
2046 struct xfs_mount
*mp
= ip
->i_mount
;
2047 struct xfs_bmbt_irec old
;
2052 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
2054 ASSERT(!isnullstartblock(new->br_startblock
));
2056 XFS_STATS_INC(mp
, xs_add_exlist
);
2063 * Set up a bunch of variables to make the tests simpler.
2066 xfs_iext_get_extent(ifp
, icur
, &PREV
);
2067 ASSERT(new->br_state
!= PREV
.br_state
);
2068 new_endoff
= new->br_startoff
+ new->br_blockcount
;
2069 ASSERT(PREV
.br_startoff
<= new->br_startoff
);
2070 ASSERT(PREV
.br_startoff
+ PREV
.br_blockcount
>= new_endoff
);
2073 * Set flags determining what part of the previous oldext allocation
2074 * extent is being replaced by a newext allocation.
2076 if (PREV
.br_startoff
== new->br_startoff
)
2077 state
|= BMAP_LEFT_FILLING
;
2078 if (PREV
.br_startoff
+ PREV
.br_blockcount
== new_endoff
)
2079 state
|= BMAP_RIGHT_FILLING
;
2082 * Check and set flags if this segment has a left neighbor.
2083 * Don't set contiguous if the combined extent would be too large.
2085 if (xfs_iext_peek_prev_extent(ifp
, icur
, &LEFT
)) {
2086 state
|= BMAP_LEFT_VALID
;
2087 if (isnullstartblock(LEFT
.br_startblock
))
2088 state
|= BMAP_LEFT_DELAY
;
2091 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
2092 LEFT
.br_startoff
+ LEFT
.br_blockcount
== new->br_startoff
&&
2093 LEFT
.br_startblock
+ LEFT
.br_blockcount
== new->br_startblock
&&
2094 LEFT
.br_state
== new->br_state
&&
2095 LEFT
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
2096 state
|= BMAP_LEFT_CONTIG
;
2099 * Check and set flags if this segment has a right neighbor.
2100 * Don't set contiguous if the combined extent would be too large.
2101 * Also check for all-three-contiguous being too large.
2103 if (xfs_iext_peek_next_extent(ifp
, icur
, &RIGHT
)) {
2104 state
|= BMAP_RIGHT_VALID
;
2105 if (isnullstartblock(RIGHT
.br_startblock
))
2106 state
|= BMAP_RIGHT_DELAY
;
2109 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
2110 new_endoff
== RIGHT
.br_startoff
&&
2111 new->br_startblock
+ new->br_blockcount
== RIGHT
.br_startblock
&&
2112 new->br_state
== RIGHT
.br_state
&&
2113 new->br_blockcount
+ RIGHT
.br_blockcount
<= MAXEXTLEN
&&
2114 ((state
& (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
2115 BMAP_RIGHT_FILLING
)) !=
2116 (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
2117 BMAP_RIGHT_FILLING
) ||
2118 LEFT
.br_blockcount
+ new->br_blockcount
+ RIGHT
.br_blockcount
2120 state
|= BMAP_RIGHT_CONTIG
;
2123 * Switch out based on the FILLING and CONTIG state bits.
2125 switch (state
& (BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
2126 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
)) {
2127 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
2128 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2130 * Setting all of a previous oldext extent to newext.
2131 * The left and right neighbors are both contiguous with new.
2133 LEFT
.br_blockcount
+= PREV
.br_blockcount
+ RIGHT
.br_blockcount
;
2135 xfs_iext_remove(ip
, icur
, state
);
2136 xfs_iext_remove(ip
, icur
, state
);
2137 xfs_iext_prev(ifp
, icur
);
2138 xfs_iext_update_extent(ip
, state
, icur
, &LEFT
);
2139 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2140 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 2);
2142 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2144 rval
= XFS_ILOG_CORE
;
2145 error
= xfs_bmbt_lookup_eq(cur
, &RIGHT
, &i
);
2148 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2149 if ((error
= xfs_btree_delete(cur
, &i
)))
2151 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2152 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2154 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2155 if ((error
= xfs_btree_delete(cur
, &i
)))
2157 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2158 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2160 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2161 error
= xfs_bmbt_update(cur
, &LEFT
);
2167 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
2169 * Setting all of a previous oldext extent to newext.
2170 * The left neighbor is contiguous, the right is not.
2172 LEFT
.br_blockcount
+= PREV
.br_blockcount
;
2174 xfs_iext_remove(ip
, icur
, state
);
2175 xfs_iext_prev(ifp
, icur
);
2176 xfs_iext_update_extent(ip
, state
, icur
, &LEFT
);
2177 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2178 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
2180 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2182 rval
= XFS_ILOG_CORE
;
2183 error
= xfs_bmbt_lookup_eq(cur
, &PREV
, &i
);
2186 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2187 if ((error
= xfs_btree_delete(cur
, &i
)))
2189 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2190 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2192 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2193 error
= xfs_bmbt_update(cur
, &LEFT
);
2199 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2201 * Setting all of a previous oldext extent to newext.
2202 * The right neighbor is contiguous, the left is not.
2204 PREV
.br_blockcount
+= RIGHT
.br_blockcount
;
2205 PREV
.br_state
= new->br_state
;
2207 xfs_iext_next(ifp
, icur
);
2208 xfs_iext_remove(ip
, icur
, state
);
2209 xfs_iext_prev(ifp
, icur
);
2210 xfs_iext_update_extent(ip
, state
, icur
, &PREV
);
2212 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2213 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
2215 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2217 rval
= XFS_ILOG_CORE
;
2218 error
= xfs_bmbt_lookup_eq(cur
, &RIGHT
, &i
);
2221 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2222 if ((error
= xfs_btree_delete(cur
, &i
)))
2224 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2225 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2227 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2228 error
= xfs_bmbt_update(cur
, &PREV
);
2234 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
2236 * Setting all of a previous oldext extent to newext.
2237 * Neither the left nor right neighbors are contiguous with
2240 PREV
.br_state
= new->br_state
;
2241 xfs_iext_update_extent(ip
, state
, icur
, &PREV
);
2244 rval
= XFS_ILOG_DEXT
;
2247 error
= xfs_bmbt_lookup_eq(cur
, new, &i
);
2250 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2251 error
= xfs_bmbt_update(cur
, &PREV
);
2257 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
:
2259 * Setting the first part of a previous oldext extent to newext.
2260 * The left neighbor is contiguous.
2262 LEFT
.br_blockcount
+= new->br_blockcount
;
2265 PREV
.br_startoff
+= new->br_blockcount
;
2266 PREV
.br_startblock
+= new->br_blockcount
;
2267 PREV
.br_blockcount
-= new->br_blockcount
;
2269 xfs_iext_update_extent(ip
, state
, icur
, &PREV
);
2270 xfs_iext_prev(ifp
, icur
);
2271 xfs_iext_update_extent(ip
, state
, icur
, &LEFT
);
2274 rval
= XFS_ILOG_DEXT
;
2277 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2280 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2281 error
= xfs_bmbt_update(cur
, &PREV
);
2284 error
= xfs_btree_decrement(cur
, 0, &i
);
2287 error
= xfs_bmbt_update(cur
, &LEFT
);
2293 case BMAP_LEFT_FILLING
:
2295 * Setting the first part of a previous oldext extent to newext.
2296 * The left neighbor is not contiguous.
2299 PREV
.br_startoff
+= new->br_blockcount
;
2300 PREV
.br_startblock
+= new->br_blockcount
;
2301 PREV
.br_blockcount
-= new->br_blockcount
;
2303 xfs_iext_update_extent(ip
, state
, icur
, &PREV
);
2304 xfs_iext_insert(ip
, icur
, new, state
);
2305 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2306 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
2308 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2310 rval
= XFS_ILOG_CORE
;
2311 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2314 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2315 error
= xfs_bmbt_update(cur
, &PREV
);
2318 cur
->bc_rec
.b
= *new;
2319 if ((error
= xfs_btree_insert(cur
, &i
)))
2321 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2325 case BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2327 * Setting the last part of a previous oldext extent to newext.
2328 * The right neighbor is contiguous with the new allocation.
2331 PREV
.br_blockcount
-= new->br_blockcount
;
2333 RIGHT
.br_startoff
= new->br_startoff
;
2334 RIGHT
.br_startblock
= new->br_startblock
;
2335 RIGHT
.br_blockcount
+= new->br_blockcount
;
2337 xfs_iext_update_extent(ip
, state
, icur
, &PREV
);
2338 xfs_iext_next(ifp
, icur
);
2339 xfs_iext_update_extent(ip
, state
, icur
, &RIGHT
);
2342 rval
= XFS_ILOG_DEXT
;
2345 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2348 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2349 error
= xfs_bmbt_update(cur
, &PREV
);
2352 error
= xfs_btree_increment(cur
, 0, &i
);
2355 error
= xfs_bmbt_update(cur
, &RIGHT
);
2361 case BMAP_RIGHT_FILLING
:
2363 * Setting the last part of a previous oldext extent to newext.
2364 * The right neighbor is not contiguous.
2367 PREV
.br_blockcount
-= new->br_blockcount
;
2369 xfs_iext_update_extent(ip
, state
, icur
, &PREV
);
2370 xfs_iext_next(ifp
, icur
);
2371 xfs_iext_insert(ip
, icur
, new, state
);
2373 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2374 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
2376 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2378 rval
= XFS_ILOG_CORE
;
2379 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2382 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2383 error
= xfs_bmbt_update(cur
, &PREV
);
2386 error
= xfs_bmbt_lookup_eq(cur
, new, &i
);
2389 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
2390 if ((error
= xfs_btree_insert(cur
, &i
)))
2392 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2398 * Setting the middle part of a previous oldext extent to
2399 * newext. Contiguity is impossible here.
2400 * One extent becomes three extents.
2403 PREV
.br_blockcount
= new->br_startoff
- PREV
.br_startoff
;
2406 r
[1].br_startoff
= new_endoff
;
2407 r
[1].br_blockcount
=
2408 old
.br_startoff
+ old
.br_blockcount
- new_endoff
;
2409 r
[1].br_startblock
= new->br_startblock
+ new->br_blockcount
;
2410 r
[1].br_state
= PREV
.br_state
;
2412 xfs_iext_update_extent(ip
, state
, icur
, &PREV
);
2413 xfs_iext_next(ifp
, icur
);
2414 xfs_iext_insert(ip
, icur
, &r
[1], state
);
2415 xfs_iext_insert(ip
, icur
, &r
[0], state
);
2417 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2418 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 2);
2420 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2422 rval
= XFS_ILOG_CORE
;
2423 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2426 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2427 /* new right extent - oldext */
2428 error
= xfs_bmbt_update(cur
, &r
[1]);
2431 /* new left extent - oldext */
2432 cur
->bc_rec
.b
= PREV
;
2433 if ((error
= xfs_btree_insert(cur
, &i
)))
2435 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2437 * Reset the cursor to the position of the new extent
2438 * we are about to insert as we can't trust it after
2439 * the previous insert.
2441 error
= xfs_bmbt_lookup_eq(cur
, new, &i
);
2444 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
2445 /* new middle extent - newext */
2446 if ((error
= xfs_btree_insert(cur
, &i
)))
2448 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2452 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2453 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2454 case BMAP_LEFT_FILLING
| BMAP_RIGHT_CONTIG
:
2455 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
2456 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2457 case BMAP_LEFT_CONTIG
:
2458 case BMAP_RIGHT_CONTIG
:
2460 * These cases are all impossible.
2465 /* update reverse mappings */
2466 xfs_rmap_convert_extent(mp
, tp
, ip
, whichfork
, new);
2468 /* convert to a btree if necessary */
2469 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
2470 int tmp_logflags
; /* partial log flag return val */
2472 ASSERT(cur
== NULL
);
2473 error
= xfs_bmap_extents_to_btree(tp
, ip
, &cur
, 0,
2474 &tmp_logflags
, whichfork
);
2475 *logflagsp
|= tmp_logflags
;
2480 /* clear out the allocated field, done with it now in any case. */
2482 cur
->bc_private
.b
.allocated
= 0;
2486 xfs_bmap_check_leaf_extents(*curp
, ip
, whichfork
);
2496 * Convert a hole to a delayed allocation.
2499 xfs_bmap_add_extent_hole_delay(
2500 xfs_inode_t
*ip
, /* incore inode pointer */
2502 struct xfs_iext_cursor
*icur
,
2503 xfs_bmbt_irec_t
*new) /* new data to add to file extents */
2505 struct xfs_ifork
*ifp
; /* inode fork pointer */
2506 xfs_bmbt_irec_t left
; /* left neighbor extent entry */
2507 xfs_filblks_t newlen
=0; /* new indirect size */
2508 xfs_filblks_t oldlen
=0; /* old indirect size */
2509 xfs_bmbt_irec_t right
; /* right neighbor extent entry */
2510 int state
= xfs_bmap_fork_to_state(whichfork
);
2511 xfs_filblks_t temp
; /* temp for indirect calculations */
2513 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
2514 ASSERT(isnullstartblock(new->br_startblock
));
2517 * Check and set flags if this segment has a left neighbor
2519 if (xfs_iext_peek_prev_extent(ifp
, icur
, &left
)) {
2520 state
|= BMAP_LEFT_VALID
;
2521 if (isnullstartblock(left
.br_startblock
))
2522 state
|= BMAP_LEFT_DELAY
;
2526 * Check and set flags if the current (right) segment exists.
2527 * If it doesn't exist, we're converting the hole at end-of-file.
2529 if (xfs_iext_get_extent(ifp
, icur
, &right
)) {
2530 state
|= BMAP_RIGHT_VALID
;
2531 if (isnullstartblock(right
.br_startblock
))
2532 state
|= BMAP_RIGHT_DELAY
;
2536 * Set contiguity flags on the left and right neighbors.
2537 * Don't let extents get too large, even if the pieces are contiguous.
2539 if ((state
& BMAP_LEFT_VALID
) && (state
& BMAP_LEFT_DELAY
) &&
2540 left
.br_startoff
+ left
.br_blockcount
== new->br_startoff
&&
2541 left
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
2542 state
|= BMAP_LEFT_CONTIG
;
2544 if ((state
& BMAP_RIGHT_VALID
) && (state
& BMAP_RIGHT_DELAY
) &&
2545 new->br_startoff
+ new->br_blockcount
== right
.br_startoff
&&
2546 new->br_blockcount
+ right
.br_blockcount
<= MAXEXTLEN
&&
2547 (!(state
& BMAP_LEFT_CONTIG
) ||
2548 (left
.br_blockcount
+ new->br_blockcount
+
2549 right
.br_blockcount
<= MAXEXTLEN
)))
2550 state
|= BMAP_RIGHT_CONTIG
;
2553 * Switch out based on the contiguity flags.
2555 switch (state
& (BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
)) {
2556 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2558 * New allocation is contiguous with delayed allocations
2559 * on the left and on the right.
2560 * Merge all three into a single extent record.
2562 temp
= left
.br_blockcount
+ new->br_blockcount
+
2563 right
.br_blockcount
;
2565 oldlen
= startblockval(left
.br_startblock
) +
2566 startblockval(new->br_startblock
) +
2567 startblockval(right
.br_startblock
);
2568 newlen
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
2570 left
.br_startblock
= nullstartblock(newlen
);
2571 left
.br_blockcount
= temp
;
2573 xfs_iext_remove(ip
, icur
, state
);
2574 xfs_iext_prev(ifp
, icur
);
2575 xfs_iext_update_extent(ip
, state
, icur
, &left
);
2578 case BMAP_LEFT_CONTIG
:
2580 * New allocation is contiguous with a delayed allocation
2582 * Merge the new allocation with the left neighbor.
2584 temp
= left
.br_blockcount
+ new->br_blockcount
;
2586 oldlen
= startblockval(left
.br_startblock
) +
2587 startblockval(new->br_startblock
);
2588 newlen
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
2590 left
.br_blockcount
= temp
;
2591 left
.br_startblock
= nullstartblock(newlen
);
2593 xfs_iext_prev(ifp
, icur
);
2594 xfs_iext_update_extent(ip
, state
, icur
, &left
);
2597 case BMAP_RIGHT_CONTIG
:
2599 * New allocation is contiguous with a delayed allocation
2601 * Merge the new allocation with the right neighbor.
2603 temp
= new->br_blockcount
+ right
.br_blockcount
;
2604 oldlen
= startblockval(new->br_startblock
) +
2605 startblockval(right
.br_startblock
);
2606 newlen
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
2608 right
.br_startoff
= new->br_startoff
;
2609 right
.br_startblock
= nullstartblock(newlen
);
2610 right
.br_blockcount
= temp
;
2611 xfs_iext_update_extent(ip
, state
, icur
, &right
);
2616 * New allocation is not contiguous with another
2617 * delayed allocation.
2618 * Insert a new entry.
2620 oldlen
= newlen
= 0;
2621 xfs_iext_insert(ip
, icur
, new, state
);
2624 if (oldlen
!= newlen
) {
2625 ASSERT(oldlen
> newlen
);
2626 xfs_mod_fdblocks(ip
->i_mount
, (int64_t)(oldlen
- newlen
),
2629 * Nothing to do for disk quota accounting here.
2631 xfs_mod_delalloc(ip
->i_mount
, (int64_t)newlen
- oldlen
);
2636 * Convert a hole to a real allocation.
2638 STATIC
int /* error */
2639 xfs_bmap_add_extent_hole_real(
2640 struct xfs_trans
*tp
,
2641 struct xfs_inode
*ip
,
2643 struct xfs_iext_cursor
*icur
,
2644 struct xfs_btree_cur
**curp
,
2645 struct xfs_bmbt_irec
*new,
2649 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
2650 struct xfs_mount
*mp
= ip
->i_mount
;
2651 struct xfs_btree_cur
*cur
= *curp
;
2652 int error
; /* error return value */
2653 int i
; /* temp state */
2654 xfs_bmbt_irec_t left
; /* left neighbor extent entry */
2655 xfs_bmbt_irec_t right
; /* right neighbor extent entry */
2656 int rval
=0; /* return value (logging flags) */
2657 int state
= xfs_bmap_fork_to_state(whichfork
);
2658 struct xfs_bmbt_irec old
;
2660 ASSERT(!isnullstartblock(new->br_startblock
));
2661 ASSERT(!cur
|| !(cur
->bc_private
.b
.flags
& XFS_BTCUR_BPRV_WASDEL
));
2663 XFS_STATS_INC(mp
, xs_add_exlist
);
2666 * Check and set flags if this segment has a left neighbor.
2668 if (xfs_iext_peek_prev_extent(ifp
, icur
, &left
)) {
2669 state
|= BMAP_LEFT_VALID
;
2670 if (isnullstartblock(left
.br_startblock
))
2671 state
|= BMAP_LEFT_DELAY
;
2675 * Check and set flags if this segment has a current value.
2676 * Not true if we're inserting into the "hole" at eof.
2678 if (xfs_iext_get_extent(ifp
, icur
, &right
)) {
2679 state
|= BMAP_RIGHT_VALID
;
2680 if (isnullstartblock(right
.br_startblock
))
2681 state
|= BMAP_RIGHT_DELAY
;
2685 * We're inserting a real allocation between "left" and "right".
2686 * Set the contiguity flags. Don't let extents get too large.
2688 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
2689 left
.br_startoff
+ left
.br_blockcount
== new->br_startoff
&&
2690 left
.br_startblock
+ left
.br_blockcount
== new->br_startblock
&&
2691 left
.br_state
== new->br_state
&&
2692 left
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
2693 state
|= BMAP_LEFT_CONTIG
;
2695 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
2696 new->br_startoff
+ new->br_blockcount
== right
.br_startoff
&&
2697 new->br_startblock
+ new->br_blockcount
== right
.br_startblock
&&
2698 new->br_state
== right
.br_state
&&
2699 new->br_blockcount
+ right
.br_blockcount
<= MAXEXTLEN
&&
2700 (!(state
& BMAP_LEFT_CONTIG
) ||
2701 left
.br_blockcount
+ new->br_blockcount
+
2702 right
.br_blockcount
<= MAXEXTLEN
))
2703 state
|= BMAP_RIGHT_CONTIG
;
2707 * Select which case we're in here, and implement it.
2709 switch (state
& (BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
)) {
2710 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2712 * New allocation is contiguous with real allocations on the
2713 * left and on the right.
2714 * Merge all three into a single extent record.
2716 left
.br_blockcount
+= new->br_blockcount
+ right
.br_blockcount
;
2718 xfs_iext_remove(ip
, icur
, state
);
2719 xfs_iext_prev(ifp
, icur
);
2720 xfs_iext_update_extent(ip
, state
, icur
, &left
);
2722 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2723 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
2725 rval
= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
2727 rval
= XFS_ILOG_CORE
;
2728 error
= xfs_bmbt_lookup_eq(cur
, &right
, &i
);
2731 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2732 error
= xfs_btree_delete(cur
, &i
);
2735 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2736 error
= xfs_btree_decrement(cur
, 0, &i
);
2739 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2740 error
= xfs_bmbt_update(cur
, &left
);
2746 case BMAP_LEFT_CONTIG
:
2748 * New allocation is contiguous with a real allocation
2750 * Merge the new allocation with the left neighbor.
2753 left
.br_blockcount
+= new->br_blockcount
;
2755 xfs_iext_prev(ifp
, icur
);
2756 xfs_iext_update_extent(ip
, state
, icur
, &left
);
2759 rval
= xfs_ilog_fext(whichfork
);
2762 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2765 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2766 error
= xfs_bmbt_update(cur
, &left
);
2772 case BMAP_RIGHT_CONTIG
:
2774 * New allocation is contiguous with a real allocation
2776 * Merge the new allocation with the right neighbor.
2780 right
.br_startoff
= new->br_startoff
;
2781 right
.br_startblock
= new->br_startblock
;
2782 right
.br_blockcount
+= new->br_blockcount
;
2783 xfs_iext_update_extent(ip
, state
, icur
, &right
);
2786 rval
= xfs_ilog_fext(whichfork
);
2789 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2792 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2793 error
= xfs_bmbt_update(cur
, &right
);
2801 * New allocation is not contiguous with another
2803 * Insert a new entry.
2805 xfs_iext_insert(ip
, icur
, new, state
);
2806 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2807 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
2809 rval
= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
2811 rval
= XFS_ILOG_CORE
;
2812 error
= xfs_bmbt_lookup_eq(cur
, new, &i
);
2815 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
2816 error
= xfs_btree_insert(cur
, &i
);
2819 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2824 /* add reverse mapping unless caller opted out */
2825 if (!(flags
& XFS_BMAPI_NORMAP
))
2826 xfs_rmap_map_extent(tp
, ip
, whichfork
, new);
2828 /* convert to a btree if necessary */
2829 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
2830 int tmp_logflags
; /* partial log flag return val */
2832 ASSERT(cur
== NULL
);
2833 error
= xfs_bmap_extents_to_btree(tp
, ip
, curp
, 0,
2834 &tmp_logflags
, whichfork
);
2835 *logflagsp
|= tmp_logflags
;
2841 /* clear out the allocated field, done with it now in any case. */
2843 cur
->bc_private
.b
.allocated
= 0;
2845 xfs_bmap_check_leaf_extents(cur
, ip
, whichfork
);
2852 * Functions used in the extent read, allocate and remove paths
2856 * Adjust the size of the new extent based on di_extsize and rt extsize.
2859 xfs_bmap_extsize_align(
2861 xfs_bmbt_irec_t
*gotp
, /* next extent pointer */
2862 xfs_bmbt_irec_t
*prevp
, /* previous extent pointer */
2863 xfs_extlen_t extsz
, /* align to this extent size */
2864 int rt
, /* is this a realtime inode? */
2865 int eof
, /* is extent at end-of-file? */
2866 int delay
, /* creating delalloc extent? */
2867 int convert
, /* overwriting unwritten extent? */
2868 xfs_fileoff_t
*offp
, /* in/out: aligned offset */
2869 xfs_extlen_t
*lenp
) /* in/out: aligned length */
2871 xfs_fileoff_t orig_off
; /* original offset */
2872 xfs_extlen_t orig_alen
; /* original length */
2873 xfs_fileoff_t orig_end
; /* original off+len */
2874 xfs_fileoff_t nexto
; /* next file offset */
2875 xfs_fileoff_t prevo
; /* previous file offset */
2876 xfs_fileoff_t align_off
; /* temp for offset */
2877 xfs_extlen_t align_alen
; /* temp for length */
2878 xfs_extlen_t temp
; /* temp for calculations */
2883 orig_off
= align_off
= *offp
;
2884 orig_alen
= align_alen
= *lenp
;
2885 orig_end
= orig_off
+ orig_alen
;
2888 * If this request overlaps an existing extent, then don't
2889 * attempt to perform any additional alignment.
2891 if (!delay
&& !eof
&&
2892 (orig_off
>= gotp
->br_startoff
) &&
2893 (orig_end
<= gotp
->br_startoff
+ gotp
->br_blockcount
)) {
2898 * If the file offset is unaligned vs. the extent size
2899 * we need to align it. This will be possible unless
2900 * the file was previously written with a kernel that didn't
2901 * perform this alignment, or if a truncate shot us in the
2904 div_u64_rem(orig_off
, extsz
, &temp
);
2910 /* Same adjustment for the end of the requested area. */
2911 temp
= (align_alen
% extsz
);
2913 align_alen
+= extsz
- temp
;
2916 * For large extent hint sizes, the aligned extent might be larger than
2917 * MAXEXTLEN. In that case, reduce the size by an extsz so that it pulls
2918 * the length back under MAXEXTLEN. The outer allocation loops handle
2919 * short allocation just fine, so it is safe to do this. We only want to
2920 * do it when we are forced to, though, because it means more allocation
2921 * operations are required.
2923 while (align_alen
> MAXEXTLEN
)
2924 align_alen
-= extsz
;
2925 ASSERT(align_alen
<= MAXEXTLEN
);
2928 * If the previous block overlaps with this proposed allocation
2929 * then move the start forward without adjusting the length.
2931 if (prevp
->br_startoff
!= NULLFILEOFF
) {
2932 if (prevp
->br_startblock
== HOLESTARTBLOCK
)
2933 prevo
= prevp
->br_startoff
;
2935 prevo
= prevp
->br_startoff
+ prevp
->br_blockcount
;
2938 if (align_off
!= orig_off
&& align_off
< prevo
)
2941 * If the next block overlaps with this proposed allocation
2942 * then move the start back without adjusting the length,
2943 * but not before offset 0.
2944 * This may of course make the start overlap previous block,
2945 * and if we hit the offset 0 limit then the next block
2946 * can still overlap too.
2948 if (!eof
&& gotp
->br_startoff
!= NULLFILEOFF
) {
2949 if ((delay
&& gotp
->br_startblock
== HOLESTARTBLOCK
) ||
2950 (!delay
&& gotp
->br_startblock
== DELAYSTARTBLOCK
))
2951 nexto
= gotp
->br_startoff
+ gotp
->br_blockcount
;
2953 nexto
= gotp
->br_startoff
;
2955 nexto
= NULLFILEOFF
;
2957 align_off
+ align_alen
!= orig_end
&&
2958 align_off
+ align_alen
> nexto
)
2959 align_off
= nexto
> align_alen
? nexto
- align_alen
: 0;
2961 * If we're now overlapping the next or previous extent that
2962 * means we can't fit an extsz piece in this hole. Just move
2963 * the start forward to the first valid spot and set
2964 * the length so we hit the end.
2966 if (align_off
!= orig_off
&& align_off
< prevo
)
2968 if (align_off
+ align_alen
!= orig_end
&&
2969 align_off
+ align_alen
> nexto
&&
2970 nexto
!= NULLFILEOFF
) {
2971 ASSERT(nexto
> prevo
);
2972 align_alen
= nexto
- align_off
;
2976 * If realtime, and the result isn't a multiple of the realtime
2977 * extent size we need to remove blocks until it is.
2979 if (rt
&& (temp
= (align_alen
% mp
->m_sb
.sb_rextsize
))) {
2981 * We're not covering the original request, or
2982 * we won't be able to once we fix the length.
2984 if (orig_off
< align_off
||
2985 orig_end
> align_off
+ align_alen
||
2986 align_alen
- temp
< orig_alen
)
2989 * Try to fix it by moving the start up.
2991 if (align_off
+ temp
<= orig_off
) {
2996 * Try to fix it by moving the end in.
2998 else if (align_off
+ align_alen
- temp
>= orig_end
)
3001 * Set the start to the minimum then trim the length.
3004 align_alen
-= orig_off
- align_off
;
3005 align_off
= orig_off
;
3006 align_alen
-= align_alen
% mp
->m_sb
.sb_rextsize
;
3009 * Result doesn't cover the request, fail it.
3011 if (orig_off
< align_off
|| orig_end
> align_off
+ align_alen
)
3014 ASSERT(orig_off
>= align_off
);
3015 /* see MAXEXTLEN handling above */
3016 ASSERT(orig_end
<= align_off
+ align_alen
||
3017 align_alen
+ extsz
> MAXEXTLEN
);
3021 if (!eof
&& gotp
->br_startoff
!= NULLFILEOFF
)
3022 ASSERT(align_off
+ align_alen
<= gotp
->br_startoff
);
3023 if (prevp
->br_startoff
!= NULLFILEOFF
)
3024 ASSERT(align_off
>= prevp
->br_startoff
+ prevp
->br_blockcount
);
3032 #define XFS_ALLOC_GAP_UNITS 4
3036 struct xfs_bmalloca
*ap
) /* bmap alloc argument struct */
3038 xfs_fsblock_t adjust
; /* adjustment to block numbers */
3039 xfs_agnumber_t fb_agno
; /* ag number of ap->firstblock */
3040 xfs_mount_t
*mp
; /* mount point structure */
3041 int nullfb
; /* true if ap->firstblock isn't set */
3042 int rt
; /* true if inode is realtime */
3044 #define ISVALID(x,y) \
3046 (x) < mp->m_sb.sb_rblocks : \
3047 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
3048 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
3049 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
3051 mp
= ap
->ip
->i_mount
;
3052 nullfb
= ap
->tp
->t_firstblock
== NULLFSBLOCK
;
3053 rt
= XFS_IS_REALTIME_INODE(ap
->ip
) &&
3054 xfs_alloc_is_userdata(ap
->datatype
);
3055 fb_agno
= nullfb
? NULLAGNUMBER
: XFS_FSB_TO_AGNO(mp
,
3056 ap
->tp
->t_firstblock
);
3058 * If allocating at eof, and there's a previous real block,
3059 * try to use its last block as our starting point.
3061 if (ap
->eof
&& ap
->prev
.br_startoff
!= NULLFILEOFF
&&
3062 !isnullstartblock(ap
->prev
.br_startblock
) &&
3063 ISVALID(ap
->prev
.br_startblock
+ ap
->prev
.br_blockcount
,
3064 ap
->prev
.br_startblock
)) {
3065 ap
->blkno
= ap
->prev
.br_startblock
+ ap
->prev
.br_blockcount
;
3067 * Adjust for the gap between prevp and us.
3069 adjust
= ap
->offset
-
3070 (ap
->prev
.br_startoff
+ ap
->prev
.br_blockcount
);
3072 ISVALID(ap
->blkno
+ adjust
, ap
->prev
.br_startblock
))
3073 ap
->blkno
+= adjust
;
3076 * If not at eof, then compare the two neighbor blocks.
3077 * Figure out whether either one gives us a good starting point,
3078 * and pick the better one.
3080 else if (!ap
->eof
) {
3081 xfs_fsblock_t gotbno
; /* right side block number */
3082 xfs_fsblock_t gotdiff
=0; /* right side difference */
3083 xfs_fsblock_t prevbno
; /* left side block number */
3084 xfs_fsblock_t prevdiff
=0; /* left side difference */
3087 * If there's a previous (left) block, select a requested
3088 * start block based on it.
3090 if (ap
->prev
.br_startoff
!= NULLFILEOFF
&&
3091 !isnullstartblock(ap
->prev
.br_startblock
) &&
3092 (prevbno
= ap
->prev
.br_startblock
+
3093 ap
->prev
.br_blockcount
) &&
3094 ISVALID(prevbno
, ap
->prev
.br_startblock
)) {
3096 * Calculate gap to end of previous block.
3098 adjust
= prevdiff
= ap
->offset
-
3099 (ap
->prev
.br_startoff
+
3100 ap
->prev
.br_blockcount
);
3102 * Figure the startblock based on the previous block's
3103 * end and the gap size.
3105 * If the gap is large relative to the piece we're
3106 * allocating, or using it gives us an invalid block
3107 * number, then just use the end of the previous block.
3109 if (prevdiff
<= XFS_ALLOC_GAP_UNITS
* ap
->length
&&
3110 ISVALID(prevbno
+ prevdiff
,
3111 ap
->prev
.br_startblock
))
3116 * If the firstblock forbids it, can't use it,
3119 if (!rt
&& !nullfb
&&
3120 XFS_FSB_TO_AGNO(mp
, prevbno
) != fb_agno
)
3121 prevbno
= NULLFSBLOCK
;
3124 * No previous block or can't follow it, just default.
3127 prevbno
= NULLFSBLOCK
;
3129 * If there's a following (right) block, select a requested
3130 * start block based on it.
3132 if (!isnullstartblock(ap
->got
.br_startblock
)) {
3134 * Calculate gap to start of next block.
3136 adjust
= gotdiff
= ap
->got
.br_startoff
- ap
->offset
;
3138 * Figure the startblock based on the next block's
3139 * start and the gap size.
3141 gotbno
= ap
->got
.br_startblock
;
3144 * If the gap is large relative to the piece we're
3145 * allocating, or using it gives us an invalid block
3146 * number, then just use the start of the next block
3147 * offset by our length.
3149 if (gotdiff
<= XFS_ALLOC_GAP_UNITS
* ap
->length
&&
3150 ISVALID(gotbno
- gotdiff
, gotbno
))
3152 else if (ISVALID(gotbno
- ap
->length
, gotbno
)) {
3153 gotbno
-= ap
->length
;
3154 gotdiff
+= adjust
- ap
->length
;
3158 * If the firstblock forbids it, can't use it,
3161 if (!rt
&& !nullfb
&&
3162 XFS_FSB_TO_AGNO(mp
, gotbno
) != fb_agno
)
3163 gotbno
= NULLFSBLOCK
;
3166 * No next block, just default.
3169 gotbno
= NULLFSBLOCK
;
3171 * If both valid, pick the better one, else the only good
3172 * one, else ap->blkno is already set (to 0 or the inode block).
3174 if (prevbno
!= NULLFSBLOCK
&& gotbno
!= NULLFSBLOCK
)
3175 ap
->blkno
= prevdiff
<= gotdiff
? prevbno
: gotbno
;
3176 else if (prevbno
!= NULLFSBLOCK
)
3177 ap
->blkno
= prevbno
;
3178 else if (gotbno
!= NULLFSBLOCK
)
3185 xfs_bmap_longest_free_extent(
3186 struct xfs_trans
*tp
,
3191 struct xfs_mount
*mp
= tp
->t_mountp
;
3192 struct xfs_perag
*pag
;
3193 xfs_extlen_t longest
;
3196 pag
= xfs_perag_get(mp
, ag
);
3197 if (!pag
->pagf_init
) {
3198 error
= xfs_alloc_pagf_init(mp
, tp
, ag
, XFS_ALLOC_FLAG_TRYLOCK
);
3202 if (!pag
->pagf_init
) {
3208 longest
= xfs_alloc_longest_free_extent(pag
,
3209 xfs_alloc_min_freelist(mp
, pag
),
3210 xfs_ag_resv_needed(pag
, XFS_AG_RESV_NONE
));
3211 if (*blen
< longest
)
3220 xfs_bmap_select_minlen(
3221 struct xfs_bmalloca
*ap
,
3222 struct xfs_alloc_arg
*args
,
3226 if (notinit
|| *blen
< ap
->minlen
) {
3228 * Since we did a BUF_TRYLOCK above, it is possible that
3229 * there is space for this request.
3231 args
->minlen
= ap
->minlen
;
3232 } else if (*blen
< args
->maxlen
) {
3234 * If the best seen length is less than the request length,
3235 * use the best as the minimum.
3237 args
->minlen
= *blen
;
3240 * Otherwise we've seen an extent as big as maxlen, use that
3243 args
->minlen
= args
->maxlen
;
3248 xfs_bmap_btalloc_nullfb(
3249 struct xfs_bmalloca
*ap
,
3250 struct xfs_alloc_arg
*args
,
3253 struct xfs_mount
*mp
= ap
->ip
->i_mount
;
3254 xfs_agnumber_t ag
, startag
;
3258 args
->type
= XFS_ALLOCTYPE_START_BNO
;
3259 args
->total
= ap
->total
;
3261 startag
= ag
= XFS_FSB_TO_AGNO(mp
, args
->fsbno
);
3262 if (startag
== NULLAGNUMBER
)
3265 while (*blen
< args
->maxlen
) {
3266 error
= xfs_bmap_longest_free_extent(args
->tp
, ag
, blen
,
3271 if (++ag
== mp
->m_sb
.sb_agcount
)
3277 xfs_bmap_select_minlen(ap
, args
, blen
, notinit
);
3282 xfs_bmap_btalloc_filestreams(
3283 struct xfs_bmalloca
*ap
,
3284 struct xfs_alloc_arg
*args
,
3287 struct xfs_mount
*mp
= ap
->ip
->i_mount
;
3292 args
->type
= XFS_ALLOCTYPE_NEAR_BNO
;
3293 args
->total
= ap
->total
;
3295 ag
= XFS_FSB_TO_AGNO(mp
, args
->fsbno
);
3296 if (ag
== NULLAGNUMBER
)
3299 error
= xfs_bmap_longest_free_extent(args
->tp
, ag
, blen
, ¬init
);
3303 if (*blen
< args
->maxlen
) {
3304 error
= xfs_filestream_new_ag(ap
, &ag
);
3308 error
= xfs_bmap_longest_free_extent(args
->tp
, ag
, blen
,
3315 xfs_bmap_select_minlen(ap
, args
, blen
, notinit
);
3318 * Set the failure fallback case to look in the selected AG as stream
3321 ap
->blkno
= args
->fsbno
= XFS_AGB_TO_FSB(mp
, ag
, 0);
3325 /* Update all inode and quota accounting for the allocation we just did. */
3327 xfs_bmap_btalloc_accounting(
3328 struct xfs_bmalloca
*ap
,
3329 struct xfs_alloc_arg
*args
)
3331 if (ap
->flags
& XFS_BMAPI_COWFORK
) {
3333 * COW fork blocks are in-core only and thus are treated as
3334 * in-core quota reservation (like delalloc blocks) even when
3335 * converted to real blocks. The quota reservation is not
3336 * accounted to disk until blocks are remapped to the data
3337 * fork. So if these blocks were previously delalloc, we
3338 * already have quota reservation and there's nothing to do
3342 xfs_mod_delalloc(ap
->ip
->i_mount
, -(int64_t)args
->len
);
3347 * Otherwise, we've allocated blocks in a hole. The transaction
3348 * has acquired in-core quota reservation for this extent.
3349 * Rather than account these as real blocks, however, we reduce
3350 * the transaction quota reservation based on the allocation.
3351 * This essentially transfers the transaction quota reservation
3352 * to that of a delalloc extent.
3354 ap
->ip
->i_delayed_blks
+= args
->len
;
3355 xfs_trans_mod_dquot_byino(ap
->tp
, ap
->ip
, XFS_TRANS_DQ_RES_BLKS
,
3360 /* data/attr fork only */
3361 ap
->ip
->i_d
.di_nblocks
+= args
->len
;
3362 xfs_trans_log_inode(ap
->tp
, ap
->ip
, XFS_ILOG_CORE
);
3364 ap
->ip
->i_delayed_blks
-= args
->len
;
3365 xfs_mod_delalloc(ap
->ip
->i_mount
, -(int64_t)args
->len
);
3367 xfs_trans_mod_dquot_byino(ap
->tp
, ap
->ip
,
3368 ap
->wasdel
? XFS_TRANS_DQ_DELBCOUNT
: XFS_TRANS_DQ_BCOUNT
,
3374 struct xfs_bmalloca
*ap
) /* bmap alloc argument struct */
3376 xfs_mount_t
*mp
; /* mount point structure */
3377 xfs_alloctype_t atype
= 0; /* type for allocation routines */
3378 xfs_extlen_t align
= 0; /* minimum allocation alignment */
3379 xfs_agnumber_t fb_agno
; /* ag number of ap->firstblock */
3381 xfs_alloc_arg_t args
;
3382 xfs_fileoff_t orig_offset
;
3383 xfs_extlen_t orig_length
;
3385 xfs_extlen_t nextminlen
= 0;
3386 int nullfb
; /* true if ap->firstblock isn't set */
3393 orig_offset
= ap
->offset
;
3394 orig_length
= ap
->length
;
3396 mp
= ap
->ip
->i_mount
;
3398 /* stripe alignment for allocation is determined by mount parameters */
3400 if (mp
->m_swidth
&& (mp
->m_flags
& XFS_MOUNT_SWALLOC
))
3401 stripe_align
= mp
->m_swidth
;
3402 else if (mp
->m_dalign
)
3403 stripe_align
= mp
->m_dalign
;
3405 if (ap
->flags
& XFS_BMAPI_COWFORK
)
3406 align
= xfs_get_cowextsz_hint(ap
->ip
);
3407 else if (xfs_alloc_is_userdata(ap
->datatype
))
3408 align
= xfs_get_extsz_hint(ap
->ip
);
3410 error
= xfs_bmap_extsize_align(mp
, &ap
->got
, &ap
->prev
,
3411 align
, 0, ap
->eof
, 0, ap
->conv
,
3412 &ap
->offset
, &ap
->length
);
3418 nullfb
= ap
->tp
->t_firstblock
== NULLFSBLOCK
;
3419 fb_agno
= nullfb
? NULLAGNUMBER
: XFS_FSB_TO_AGNO(mp
,
3420 ap
->tp
->t_firstblock
);
3422 if (xfs_alloc_is_userdata(ap
->datatype
) &&
3423 xfs_inode_is_filestream(ap
->ip
)) {
3424 ag
= xfs_filestream_lookup_ag(ap
->ip
);
3425 ag
= (ag
!= NULLAGNUMBER
) ? ag
: 0;
3426 ap
->blkno
= XFS_AGB_TO_FSB(mp
, ag
, 0);
3428 ap
->blkno
= XFS_INO_TO_FSB(mp
, ap
->ip
->i_ino
);
3431 ap
->blkno
= ap
->tp
->t_firstblock
;
3433 xfs_bmap_adjacent(ap
);
3436 * If allowed, use ap->blkno; otherwise must use firstblock since
3437 * it's in the right allocation group.
3439 if (nullfb
|| XFS_FSB_TO_AGNO(mp
, ap
->blkno
) == fb_agno
)
3442 ap
->blkno
= ap
->tp
->t_firstblock
;
3444 * Normal allocation, done through xfs_alloc_vextent.
3446 tryagain
= isaligned
= 0;
3447 memset(&args
, 0, sizeof(args
));
3450 args
.fsbno
= ap
->blkno
;
3451 args
.oinfo
= XFS_RMAP_OINFO_SKIP_UPDATE
;
3453 /* Trim the allocation back to the maximum an AG can fit. */
3454 args
.maxlen
= min(ap
->length
, mp
->m_ag_max_usable
);
3458 * Search for an allocation group with a single extent large
3459 * enough for the request. If one isn't found, then adjust
3460 * the minimum allocation size to the largest space found.
3462 if (xfs_alloc_is_userdata(ap
->datatype
) &&
3463 xfs_inode_is_filestream(ap
->ip
))
3464 error
= xfs_bmap_btalloc_filestreams(ap
, &args
, &blen
);
3466 error
= xfs_bmap_btalloc_nullfb(ap
, &args
, &blen
);
3469 } else if (ap
->tp
->t_flags
& XFS_TRANS_LOWMODE
) {
3470 if (xfs_inode_is_filestream(ap
->ip
))
3471 args
.type
= XFS_ALLOCTYPE_FIRST_AG
;
3473 args
.type
= XFS_ALLOCTYPE_START_BNO
;
3474 args
.total
= args
.minlen
= ap
->minlen
;
3476 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
3477 args
.total
= ap
->total
;
3478 args
.minlen
= ap
->minlen
;
3480 /* apply extent size hints if obtained earlier */
3483 div_u64_rem(ap
->offset
, args
.prod
, &args
.mod
);
3485 args
.mod
= args
.prod
- args
.mod
;
3486 } else if (mp
->m_sb
.sb_blocksize
>= PAGE_SIZE
) {
3490 args
.prod
= PAGE_SIZE
>> mp
->m_sb
.sb_blocklog
;
3491 div_u64_rem(ap
->offset
, args
.prod
, &args
.mod
);
3493 args
.mod
= args
.prod
- args
.mod
;
3496 * If we are not low on available data blocks, and the
3497 * underlying logical volume manager is a stripe, and
3498 * the file offset is zero then try to allocate data
3499 * blocks on stripe unit boundary.
3500 * NOTE: ap->aeof is only set if the allocation length
3501 * is >= the stripe unit and the allocation offset is
3502 * at the end of file.
3504 if (!(ap
->tp
->t_flags
& XFS_TRANS_LOWMODE
) && ap
->aeof
) {
3506 args
.alignment
= stripe_align
;
3510 * Adjust for alignment
3512 if (blen
> args
.alignment
&& blen
<= args
.maxlen
)
3513 args
.minlen
= blen
- args
.alignment
;
3514 args
.minalignslop
= 0;
3517 * First try an exact bno allocation.
3518 * If it fails then do a near or start bno
3519 * allocation with alignment turned on.
3523 args
.type
= XFS_ALLOCTYPE_THIS_BNO
;
3526 * Compute the minlen+alignment for the
3527 * next case. Set slop so that the value
3528 * of minlen+alignment+slop doesn't go up
3529 * between the calls.
3531 if (blen
> stripe_align
&& blen
<= args
.maxlen
)
3532 nextminlen
= blen
- stripe_align
;
3534 nextminlen
= args
.minlen
;
3535 if (nextminlen
+ stripe_align
> args
.minlen
+ 1)
3537 nextminlen
+ stripe_align
-
3540 args
.minalignslop
= 0;
3544 args
.minalignslop
= 0;
3546 args
.minleft
= ap
->minleft
;
3547 args
.wasdel
= ap
->wasdel
;
3548 args
.resv
= XFS_AG_RESV_NONE
;
3549 args
.datatype
= ap
->datatype
;
3550 if (ap
->datatype
& XFS_ALLOC_USERDATA_ZERO
)
3553 error
= xfs_alloc_vextent(&args
);
3557 if (tryagain
&& args
.fsbno
== NULLFSBLOCK
) {
3559 * Exact allocation failed. Now try with alignment
3563 args
.fsbno
= ap
->blkno
;
3564 args
.alignment
= stripe_align
;
3565 args
.minlen
= nextminlen
;
3566 args
.minalignslop
= 0;
3568 if ((error
= xfs_alloc_vextent(&args
)))
3571 if (isaligned
&& args
.fsbno
== NULLFSBLOCK
) {
3573 * allocation failed, so turn off alignment and
3577 args
.fsbno
= ap
->blkno
;
3579 if ((error
= xfs_alloc_vextent(&args
)))
3582 if (args
.fsbno
== NULLFSBLOCK
&& nullfb
&&
3583 args
.minlen
> ap
->minlen
) {
3584 args
.minlen
= ap
->minlen
;
3585 args
.type
= XFS_ALLOCTYPE_START_BNO
;
3586 args
.fsbno
= ap
->blkno
;
3587 if ((error
= xfs_alloc_vextent(&args
)))
3590 if (args
.fsbno
== NULLFSBLOCK
&& nullfb
) {
3592 args
.type
= XFS_ALLOCTYPE_FIRST_AG
;
3593 args
.total
= ap
->minlen
;
3594 if ((error
= xfs_alloc_vextent(&args
)))
3596 ap
->tp
->t_flags
|= XFS_TRANS_LOWMODE
;
3598 if (args
.fsbno
!= NULLFSBLOCK
) {
3600 * check the allocation happened at the same or higher AG than
3601 * the first block that was allocated.
3603 ASSERT(ap
->tp
->t_firstblock
== NULLFSBLOCK
||
3604 XFS_FSB_TO_AGNO(mp
, ap
->tp
->t_firstblock
) <=
3605 XFS_FSB_TO_AGNO(mp
, args
.fsbno
));
3607 ap
->blkno
= args
.fsbno
;
3608 if (ap
->tp
->t_firstblock
== NULLFSBLOCK
)
3609 ap
->tp
->t_firstblock
= args
.fsbno
;
3610 ASSERT(nullfb
|| fb_agno
<= args
.agno
);
3611 ap
->length
= args
.len
;
3613 * If the extent size hint is active, we tried to round the
3614 * caller's allocation request offset down to extsz and the
3615 * length up to another extsz boundary. If we found a free
3616 * extent we mapped it in starting at this new offset. If the
3617 * newly mapped space isn't long enough to cover any of the
3618 * range of offsets that was originally requested, move the
3619 * mapping up so that we can fill as much of the caller's
3620 * original request as possible. Free space is apparently
3621 * very fragmented so we're unlikely to be able to satisfy the
3624 if (ap
->length
<= orig_length
)
3625 ap
->offset
= orig_offset
;
3626 else if (ap
->offset
+ ap
->length
< orig_offset
+ orig_length
)
3627 ap
->offset
= orig_offset
+ orig_length
- ap
->length
;
3628 xfs_bmap_btalloc_accounting(ap
, &args
);
3630 ap
->blkno
= NULLFSBLOCK
;
3637 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
3638 * It figures out where to ask the underlying allocator to put the new extent.
3642 struct xfs_bmalloca
*ap
) /* bmap alloc argument struct */
3644 if (XFS_IS_REALTIME_INODE(ap
->ip
) &&
3645 xfs_alloc_is_userdata(ap
->datatype
))
3646 return xfs_bmap_rtalloc(ap
);
3647 return xfs_bmap_btalloc(ap
);
3650 /* Trim extent to fit a logical block range. */
3653 struct xfs_bmbt_irec
*irec
,
3657 xfs_fileoff_t distance
;
3658 xfs_fileoff_t end
= bno
+ len
;
3660 if (irec
->br_startoff
+ irec
->br_blockcount
<= bno
||
3661 irec
->br_startoff
>= end
) {
3662 irec
->br_blockcount
= 0;
3666 if (irec
->br_startoff
< bno
) {
3667 distance
= bno
- irec
->br_startoff
;
3668 if (isnullstartblock(irec
->br_startblock
))
3669 irec
->br_startblock
= DELAYSTARTBLOCK
;
3670 if (irec
->br_startblock
!= DELAYSTARTBLOCK
&&
3671 irec
->br_startblock
!= HOLESTARTBLOCK
)
3672 irec
->br_startblock
+= distance
;
3673 irec
->br_startoff
+= distance
;
3674 irec
->br_blockcount
-= distance
;
3677 if (end
< irec
->br_startoff
+ irec
->br_blockcount
) {
3678 distance
= irec
->br_startoff
+ irec
->br_blockcount
- end
;
3679 irec
->br_blockcount
-= distance
;
3684 * Trim the returned map to the required bounds
3688 struct xfs_bmbt_irec
*mval
,
3689 struct xfs_bmbt_irec
*got
,
3697 if ((flags
& XFS_BMAPI_ENTIRE
) ||
3698 got
->br_startoff
+ got
->br_blockcount
<= obno
) {
3700 if (isnullstartblock(got
->br_startblock
))
3701 mval
->br_startblock
= DELAYSTARTBLOCK
;
3707 ASSERT((*bno
>= obno
) || (n
== 0));
3709 mval
->br_startoff
= *bno
;
3710 if (isnullstartblock(got
->br_startblock
))
3711 mval
->br_startblock
= DELAYSTARTBLOCK
;
3713 mval
->br_startblock
= got
->br_startblock
+
3714 (*bno
- got
->br_startoff
);
3716 * Return the minimum of what we got and what we asked for for
3717 * the length. We can use the len variable here because it is
3718 * modified below and we could have been there before coming
3719 * here if the first part of the allocation didn't overlap what
3722 mval
->br_blockcount
= XFS_FILBLKS_MIN(end
- *bno
,
3723 got
->br_blockcount
- (*bno
- got
->br_startoff
));
3724 mval
->br_state
= got
->br_state
;
3725 ASSERT(mval
->br_blockcount
<= len
);
3730 * Update and validate the extent map to return
3733 xfs_bmapi_update_map(
3734 struct xfs_bmbt_irec
**map
,
3742 xfs_bmbt_irec_t
*mval
= *map
;
3744 ASSERT((flags
& XFS_BMAPI_ENTIRE
) ||
3745 ((mval
->br_startoff
+ mval
->br_blockcount
) <= end
));
3746 ASSERT((flags
& XFS_BMAPI_ENTIRE
) || (mval
->br_blockcount
<= *len
) ||
3747 (mval
->br_startoff
< obno
));
3749 *bno
= mval
->br_startoff
+ mval
->br_blockcount
;
3751 if (*n
> 0 && mval
->br_startoff
== mval
[-1].br_startoff
) {
3752 /* update previous map with new information */
3753 ASSERT(mval
->br_startblock
== mval
[-1].br_startblock
);
3754 ASSERT(mval
->br_blockcount
> mval
[-1].br_blockcount
);
3755 ASSERT(mval
->br_state
== mval
[-1].br_state
);
3756 mval
[-1].br_blockcount
= mval
->br_blockcount
;
3757 mval
[-1].br_state
= mval
->br_state
;
3758 } else if (*n
> 0 && mval
->br_startblock
!= DELAYSTARTBLOCK
&&
3759 mval
[-1].br_startblock
!= DELAYSTARTBLOCK
&&
3760 mval
[-1].br_startblock
!= HOLESTARTBLOCK
&&
3761 mval
->br_startblock
== mval
[-1].br_startblock
+
3762 mval
[-1].br_blockcount
&&
3763 mval
[-1].br_state
== mval
->br_state
) {
3764 ASSERT(mval
->br_startoff
==
3765 mval
[-1].br_startoff
+ mval
[-1].br_blockcount
);
3766 mval
[-1].br_blockcount
+= mval
->br_blockcount
;
3767 } else if (*n
> 0 &&
3768 mval
->br_startblock
== DELAYSTARTBLOCK
&&
3769 mval
[-1].br_startblock
== DELAYSTARTBLOCK
&&
3770 mval
->br_startoff
==
3771 mval
[-1].br_startoff
+ mval
[-1].br_blockcount
) {
3772 mval
[-1].br_blockcount
+= mval
->br_blockcount
;
3773 mval
[-1].br_state
= mval
->br_state
;
3774 } else if (!((*n
== 0) &&
3775 ((mval
->br_startoff
+ mval
->br_blockcount
) <=
3784 * Map file blocks to filesystem blocks without allocation.
3788 struct xfs_inode
*ip
,
3791 struct xfs_bmbt_irec
*mval
,
3795 struct xfs_mount
*mp
= ip
->i_mount
;
3796 struct xfs_ifork
*ifp
;
3797 struct xfs_bmbt_irec got
;
3800 struct xfs_iext_cursor icur
;
3804 int whichfork
= xfs_bmapi_whichfork(flags
);
3807 ASSERT(!(flags
& ~(XFS_BMAPI_ATTRFORK
|XFS_BMAPI_ENTIRE
|
3808 XFS_BMAPI_COWFORK
)));
3809 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_SHARED
|XFS_ILOCK_EXCL
));
3811 if (unlikely(XFS_TEST_ERROR(
3812 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
3813 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
3814 mp
, XFS_ERRTAG_BMAPIFORMAT
))) {
3815 XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW
, mp
);
3816 return -EFSCORRUPTED
;
3819 if (XFS_FORCED_SHUTDOWN(mp
))
3822 XFS_STATS_INC(mp
, xs_blk_mapr
);
3824 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
3826 /* No CoW fork? Return a hole. */
3827 if (whichfork
== XFS_COW_FORK
) {
3828 mval
->br_startoff
= bno
;
3829 mval
->br_startblock
= HOLESTARTBLOCK
;
3830 mval
->br_blockcount
= len
;
3831 mval
->br_state
= XFS_EXT_NORM
;
3837 * A missing attr ifork implies that the inode says we're in
3838 * extents or btree format but failed to pass the inode fork
3839 * verifier while trying to load it. Treat that as a file
3843 xfs_alert(mp
, "%s: inode %llu missing fork %d",
3844 __func__
, ip
->i_ino
, whichfork
);
3846 return -EFSCORRUPTED
;
3849 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
3850 error
= xfs_iread_extents(NULL
, ip
, whichfork
);
3855 if (!xfs_iext_lookup_extent(ip
, ifp
, bno
, &icur
, &got
))
3860 while (bno
< end
&& n
< *nmap
) {
3861 /* Reading past eof, act as though there's a hole up to end. */
3863 got
.br_startoff
= end
;
3864 if (got
.br_startoff
> bno
) {
3865 /* Reading in a hole. */
3866 mval
->br_startoff
= bno
;
3867 mval
->br_startblock
= HOLESTARTBLOCK
;
3868 mval
->br_blockcount
=
3869 XFS_FILBLKS_MIN(len
, got
.br_startoff
- bno
);
3870 mval
->br_state
= XFS_EXT_NORM
;
3871 bno
+= mval
->br_blockcount
;
3872 len
-= mval
->br_blockcount
;
3878 /* set up the extent map to return. */
3879 xfs_bmapi_trim_map(mval
, &got
, &bno
, len
, obno
, end
, n
, flags
);
3880 xfs_bmapi_update_map(&mval
, &bno
, &len
, obno
, end
, &n
, flags
);
3882 /* If we're done, stop now. */
3883 if (bno
>= end
|| n
>= *nmap
)
3886 /* Else go on to the next record. */
3887 if (!xfs_iext_next_extent(ifp
, &icur
, &got
))
3895 * Add a delayed allocation extent to an inode. Blocks are reserved from the
3896 * global pool and the extent inserted into the inode in-core extent tree.
3898 * On entry, got refers to the first extent beyond the offset of the extent to
3899 * allocate or eof is specified if no such extent exists. On return, got refers
3900 * to the extent record that was inserted to the inode fork.
3902 * Note that the allocated extent may have been merged with contiguous extents
3903 * during insertion into the inode fork. Thus, got does not reflect the current
3904 * state of the inode fork on return. If necessary, the caller can use lastx to
3905 * look up the updated record in the inode fork.
3908 xfs_bmapi_reserve_delalloc(
3909 struct xfs_inode
*ip
,
3913 xfs_filblks_t prealloc
,
3914 struct xfs_bmbt_irec
*got
,
3915 struct xfs_iext_cursor
*icur
,
3918 struct xfs_mount
*mp
= ip
->i_mount
;
3919 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
3921 xfs_extlen_t indlen
;
3923 xfs_fileoff_t aoff
= off
;
3926 * Cap the alloc length. Keep track of prealloc so we know whether to
3927 * tag the inode before we return.
3929 alen
= XFS_FILBLKS_MIN(len
+ prealloc
, MAXEXTLEN
);
3931 alen
= XFS_FILBLKS_MIN(alen
, got
->br_startoff
- aoff
);
3932 if (prealloc
&& alen
>= len
)
3933 prealloc
= alen
- len
;
3935 /* Figure out the extent size, adjust alen */
3936 if (whichfork
== XFS_COW_FORK
) {
3937 struct xfs_bmbt_irec prev
;
3938 xfs_extlen_t extsz
= xfs_get_cowextsz_hint(ip
);
3940 if (!xfs_iext_peek_prev_extent(ifp
, icur
, &prev
))
3941 prev
.br_startoff
= NULLFILEOFF
;
3943 error
= xfs_bmap_extsize_align(mp
, got
, &prev
, extsz
, 0, eof
,
3944 1, 0, &aoff
, &alen
);
3949 * Make a transaction-less quota reservation for delayed allocation
3950 * blocks. This number gets adjusted later. We return if we haven't
3951 * allocated blocks already inside this loop.
3953 error
= xfs_trans_reserve_quota_nblks(NULL
, ip
, (long)alen
, 0,
3954 XFS_QMOPT_RES_REGBLKS
);
3959 * Split changing sb for alen and indlen since they could be coming
3960 * from different places.
3962 indlen
= (xfs_extlen_t
)xfs_bmap_worst_indlen(ip
, alen
);
3965 error
= xfs_mod_fdblocks(mp
, -((int64_t)alen
), false);
3967 goto out_unreserve_quota
;
3969 error
= xfs_mod_fdblocks(mp
, -((int64_t)indlen
), false);
3971 goto out_unreserve_blocks
;
3974 ip
->i_delayed_blks
+= alen
;
3975 xfs_mod_delalloc(ip
->i_mount
, alen
+ indlen
);
3977 got
->br_startoff
= aoff
;
3978 got
->br_startblock
= nullstartblock(indlen
);
3979 got
->br_blockcount
= alen
;
3980 got
->br_state
= XFS_EXT_NORM
;
3982 xfs_bmap_add_extent_hole_delay(ip
, whichfork
, icur
, got
);
3985 * Tag the inode if blocks were preallocated. Note that COW fork
3986 * preallocation can occur at the start or end of the extent, even when
3987 * prealloc == 0, so we must also check the aligned offset and length.
3989 if (whichfork
== XFS_DATA_FORK
&& prealloc
)
3990 xfs_inode_set_eofblocks_tag(ip
);
3991 if (whichfork
== XFS_COW_FORK
&& (prealloc
|| aoff
< off
|| alen
> len
))
3992 xfs_inode_set_cowblocks_tag(ip
);
3996 out_unreserve_blocks
:
3997 xfs_mod_fdblocks(mp
, alen
, false);
3998 out_unreserve_quota
:
3999 if (XFS_IS_QUOTA_ON(mp
))
4000 xfs_trans_unreserve_quota_nblks(NULL
, ip
, (long)alen
, 0,
4001 XFS_QMOPT_RES_REGBLKS
);
4007 struct xfs_bmalloca
*bma
)
4009 struct xfs_mount
*mp
= bma
->ip
->i_mount
;
4010 int whichfork
= xfs_bmapi_whichfork(bma
->flags
);
4011 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
4012 int tmp_logflags
= 0;
4015 ASSERT(bma
->length
> 0);
4018 * For the wasdelay case, we could also just allocate the stuff asked
4019 * for in this bmap call but that wouldn't be as good.
4022 bma
->length
= (xfs_extlen_t
)bma
->got
.br_blockcount
;
4023 bma
->offset
= bma
->got
.br_startoff
;
4024 xfs_iext_peek_prev_extent(ifp
, &bma
->icur
, &bma
->prev
);
4026 bma
->length
= XFS_FILBLKS_MIN(bma
->length
, MAXEXTLEN
);
4028 bma
->length
= XFS_FILBLKS_MIN(bma
->length
,
4029 bma
->got
.br_startoff
- bma
->offset
);
4033 * Set the data type being allocated. For the data fork, the first data
4034 * in the file is treated differently to all other allocations. For the
4035 * attribute fork, we only need to ensure the allocated range is not on
4038 if (!(bma
->flags
& XFS_BMAPI_METADATA
)) {
4039 bma
->datatype
= XFS_ALLOC_NOBUSY
;
4040 if (whichfork
== XFS_DATA_FORK
) {
4041 if (bma
->offset
== 0)
4042 bma
->datatype
|= XFS_ALLOC_INITIAL_USER_DATA
;
4044 bma
->datatype
|= XFS_ALLOC_USERDATA
;
4046 if (bma
->flags
& XFS_BMAPI_ZERO
)
4047 bma
->datatype
|= XFS_ALLOC_USERDATA_ZERO
;
4050 bma
->minlen
= (bma
->flags
& XFS_BMAPI_CONTIG
) ? bma
->length
: 1;
4053 * Only want to do the alignment at the eof if it is userdata and
4054 * allocation length is larger than a stripe unit.
4056 if (mp
->m_dalign
&& bma
->length
>= mp
->m_dalign
&&
4057 !(bma
->flags
& XFS_BMAPI_METADATA
) && whichfork
== XFS_DATA_FORK
) {
4058 error
= xfs_bmap_isaeof(bma
, whichfork
);
4063 error
= xfs_bmap_alloc(bma
);
4067 if (bma
->blkno
== NULLFSBLOCK
)
4069 if ((ifp
->if_flags
& XFS_IFBROOT
) && !bma
->cur
)
4070 bma
->cur
= xfs_bmbt_init_cursor(mp
, bma
->tp
, bma
->ip
, whichfork
);
4072 * Bump the number of extents we've allocated
4078 bma
->cur
->bc_private
.b
.flags
=
4079 bma
->wasdel
? XFS_BTCUR_BPRV_WASDEL
: 0;
4081 bma
->got
.br_startoff
= bma
->offset
;
4082 bma
->got
.br_startblock
= bma
->blkno
;
4083 bma
->got
.br_blockcount
= bma
->length
;
4084 bma
->got
.br_state
= XFS_EXT_NORM
;
4087 * In the data fork, a wasdelay extent has been initialized, so
4088 * shouldn't be flagged as unwritten.
4090 * For the cow fork, however, we convert delalloc reservations
4091 * (extents allocated for speculative preallocation) to
4092 * allocated unwritten extents, and only convert the unwritten
4093 * extents to real extents when we're about to write the data.
4095 if ((!bma
->wasdel
|| (bma
->flags
& XFS_BMAPI_COWFORK
)) &&
4096 (bma
->flags
& XFS_BMAPI_PREALLOC
))
4097 bma
->got
.br_state
= XFS_EXT_UNWRITTEN
;
4100 error
= xfs_bmap_add_extent_delay_real(bma
, whichfork
);
4102 error
= xfs_bmap_add_extent_hole_real(bma
->tp
, bma
->ip
,
4103 whichfork
, &bma
->icur
, &bma
->cur
, &bma
->got
,
4104 &bma
->logflags
, bma
->flags
);
4106 bma
->logflags
|= tmp_logflags
;
4111 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4112 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4113 * the neighbouring ones.
4115 xfs_iext_get_extent(ifp
, &bma
->icur
, &bma
->got
);
4117 ASSERT(bma
->got
.br_startoff
<= bma
->offset
);
4118 ASSERT(bma
->got
.br_startoff
+ bma
->got
.br_blockcount
>=
4119 bma
->offset
+ bma
->length
);
4120 ASSERT(bma
->got
.br_state
== XFS_EXT_NORM
||
4121 bma
->got
.br_state
== XFS_EXT_UNWRITTEN
);
4126 xfs_bmapi_convert_unwritten(
4127 struct xfs_bmalloca
*bma
,
4128 struct xfs_bmbt_irec
*mval
,
4132 int whichfork
= xfs_bmapi_whichfork(flags
);
4133 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
4134 int tmp_logflags
= 0;
4137 /* check if we need to do unwritten->real conversion */
4138 if (mval
->br_state
== XFS_EXT_UNWRITTEN
&&
4139 (flags
& XFS_BMAPI_PREALLOC
))
4142 /* check if we need to do real->unwritten conversion */
4143 if (mval
->br_state
== XFS_EXT_NORM
&&
4144 (flags
& (XFS_BMAPI_PREALLOC
| XFS_BMAPI_CONVERT
)) !=
4145 (XFS_BMAPI_PREALLOC
| XFS_BMAPI_CONVERT
))
4149 * Modify (by adding) the state flag, if writing.
4151 ASSERT(mval
->br_blockcount
<= len
);
4152 if ((ifp
->if_flags
& XFS_IFBROOT
) && !bma
->cur
) {
4153 bma
->cur
= xfs_bmbt_init_cursor(bma
->ip
->i_mount
, bma
->tp
,
4154 bma
->ip
, whichfork
);
4156 mval
->br_state
= (mval
->br_state
== XFS_EXT_UNWRITTEN
)
4157 ? XFS_EXT_NORM
: XFS_EXT_UNWRITTEN
;
4160 * Before insertion into the bmbt, zero the range being converted
4163 if (flags
& XFS_BMAPI_ZERO
) {
4164 error
= xfs_zero_extent(bma
->ip
, mval
->br_startblock
,
4165 mval
->br_blockcount
);
4170 error
= xfs_bmap_add_extent_unwritten_real(bma
->tp
, bma
->ip
, whichfork
,
4171 &bma
->icur
, &bma
->cur
, mval
, &tmp_logflags
);
4173 * Log the inode core unconditionally in the unwritten extent conversion
4174 * path because the conversion might not have done so (e.g., if the
4175 * extent count hasn't changed). We need to make sure the inode is dirty
4176 * in the transaction for the sake of fsync(), even if nothing has
4177 * changed, because fsync() will not force the log for this transaction
4178 * unless it sees the inode pinned.
4180 * Note: If we're only converting cow fork extents, there aren't
4181 * any on-disk updates to make, so we don't need to log anything.
4183 if (whichfork
!= XFS_COW_FORK
)
4184 bma
->logflags
|= tmp_logflags
| XFS_ILOG_CORE
;
4189 * Update our extent pointer, given that
4190 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4191 * of the neighbouring ones.
4193 xfs_iext_get_extent(ifp
, &bma
->icur
, &bma
->got
);
4196 * We may have combined previously unwritten space with written space,
4197 * so generate another request.
4199 if (mval
->br_blockcount
< len
)
4204 static inline xfs_extlen_t
4206 struct xfs_trans
*tp
,
4207 struct xfs_inode
*ip
,
4210 if (tp
&& tp
->t_firstblock
!= NULLFSBLOCK
)
4212 if (XFS_IFORK_FORMAT(ip
, fork
) != XFS_DINODE_FMT_BTREE
)
4214 return be16_to_cpu(XFS_IFORK_PTR(ip
, fork
)->if_broot
->bb_level
) + 1;
4218 * Log whatever the flags say, even if error. Otherwise we might miss detecting
4219 * a case where the data is changed, there's an error, and it's not logged so we
4220 * don't shutdown when we should. Don't bother logging extents/btree changes if
4221 * we converted to the other format.
4225 struct xfs_bmalloca
*bma
,
4229 if ((bma
->logflags
& xfs_ilog_fext(whichfork
)) &&
4230 XFS_IFORK_FORMAT(bma
->ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
4231 bma
->logflags
&= ~xfs_ilog_fext(whichfork
);
4232 else if ((bma
->logflags
& xfs_ilog_fbroot(whichfork
)) &&
4233 XFS_IFORK_FORMAT(bma
->ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)
4234 bma
->logflags
&= ~xfs_ilog_fbroot(whichfork
);
4237 xfs_trans_log_inode(bma
->tp
, bma
->ip
, bma
->logflags
);
4239 xfs_btree_del_cursor(bma
->cur
, error
);
4243 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4244 * extent state if necessary. Details behaviour is controlled by the flags
4245 * parameter. Only allocates blocks from a single allocation group, to avoid
4250 struct xfs_trans
*tp
, /* transaction pointer */
4251 struct xfs_inode
*ip
, /* incore inode */
4252 xfs_fileoff_t bno
, /* starting file offs. mapped */
4253 xfs_filblks_t len
, /* length to map in file */
4254 int flags
, /* XFS_BMAPI_... */
4255 xfs_extlen_t total
, /* total blocks needed */
4256 struct xfs_bmbt_irec
*mval
, /* output: map values */
4257 int *nmap
) /* i/o: mval size/count */
4259 struct xfs_bmalloca bma
= {
4264 struct xfs_mount
*mp
= ip
->i_mount
;
4265 struct xfs_ifork
*ifp
;
4266 xfs_fileoff_t end
; /* end of mapped file region */
4267 bool eof
= false; /* after the end of extents */
4268 int error
; /* error return */
4269 int n
; /* current extent index */
4270 xfs_fileoff_t obno
; /* old block number (offset) */
4271 int whichfork
; /* data or attr fork */
4274 xfs_fileoff_t orig_bno
; /* original block number value */
4275 int orig_flags
; /* original flags arg value */
4276 xfs_filblks_t orig_len
; /* original value of len arg */
4277 struct xfs_bmbt_irec
*orig_mval
; /* original value of mval */
4278 int orig_nmap
; /* original value of *nmap */
4286 whichfork
= xfs_bmapi_whichfork(flags
);
4289 ASSERT(*nmap
<= XFS_BMAP_MAX_NMAP
);
4292 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_LOCAL
);
4293 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
4294 ASSERT(!(flags
& XFS_BMAPI_REMAP
));
4296 /* zeroing is for currently only for data extents, not metadata */
4297 ASSERT((flags
& (XFS_BMAPI_METADATA
| XFS_BMAPI_ZERO
)) !=
4298 (XFS_BMAPI_METADATA
| XFS_BMAPI_ZERO
));
4300 * we can allocate unwritten extents or pre-zero allocated blocks,
4301 * but it makes no sense to do both at once. This would result in
4302 * zeroing the unwritten extent twice, but it still being an
4303 * unwritten extent....
4305 ASSERT((flags
& (XFS_BMAPI_PREALLOC
| XFS_BMAPI_ZERO
)) !=
4306 (XFS_BMAPI_PREALLOC
| XFS_BMAPI_ZERO
));
4308 if (unlikely(XFS_TEST_ERROR(
4309 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
4310 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
4311 mp
, XFS_ERRTAG_BMAPIFORMAT
))) {
4312 XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW
, mp
);
4313 return -EFSCORRUPTED
;
4316 if (XFS_FORCED_SHUTDOWN(mp
))
4319 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4321 XFS_STATS_INC(mp
, xs_blk_mapw
);
4323 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
4324 error
= xfs_iread_extents(tp
, ip
, whichfork
);
4329 if (!xfs_iext_lookup_extent(ip
, ifp
, bno
, &bma
.icur
, &bma
.got
))
4331 if (!xfs_iext_peek_prev_extent(ifp
, &bma
.icur
, &bma
.prev
))
4332 bma
.prev
.br_startoff
= NULLFILEOFF
;
4333 bma
.minleft
= xfs_bmapi_minleft(tp
, ip
, whichfork
);
4338 while (bno
< end
&& n
< *nmap
) {
4339 bool need_alloc
= false, wasdelay
= false;
4341 /* in hole or beyond EOF? */
4342 if (eof
|| bma
.got
.br_startoff
> bno
) {
4344 * CoW fork conversions should /never/ hit EOF or
4345 * holes. There should always be something for us
4348 ASSERT(!((flags
& XFS_BMAPI_CONVERT
) &&
4349 (flags
& XFS_BMAPI_COWFORK
)));
4352 } else if (isnullstartblock(bma
.got
.br_startblock
)) {
4357 * First, deal with the hole before the allocated space
4358 * that we found, if any.
4360 if (need_alloc
|| wasdelay
) {
4362 bma
.conv
= !!(flags
& XFS_BMAPI_CONVERT
);
4363 bma
.wasdel
= wasdelay
;
4368 * There's a 32/64 bit type mismatch between the
4369 * allocation length request (which can be 64 bits in
4370 * length) and the bma length request, which is
4371 * xfs_extlen_t and therefore 32 bits. Hence we have to
4372 * check for 32-bit overflows and handle them here.
4374 if (len
> (xfs_filblks_t
)MAXEXTLEN
)
4375 bma
.length
= MAXEXTLEN
;
4380 ASSERT(bma
.length
> 0);
4381 error
= xfs_bmapi_allocate(&bma
);
4384 if (bma
.blkno
== NULLFSBLOCK
)
4388 * If this is a CoW allocation, record the data in
4389 * the refcount btree for orphan recovery.
4391 if (whichfork
== XFS_COW_FORK
)
4392 xfs_refcount_alloc_cow_extent(tp
, bma
.blkno
,
4396 /* Deal with the allocated space we found. */
4397 xfs_bmapi_trim_map(mval
, &bma
.got
, &bno
, len
, obno
,
4400 /* Execute unwritten extent conversion if necessary */
4401 error
= xfs_bmapi_convert_unwritten(&bma
, mval
, len
, flags
);
4402 if (error
== -EAGAIN
)
4407 /* update the extent map to return */
4408 xfs_bmapi_update_map(&mval
, &bno
, &len
, obno
, end
, &n
, flags
);
4411 * If we're done, stop now. Stop when we've allocated
4412 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4413 * the transaction may get too big.
4415 if (bno
>= end
|| n
>= *nmap
|| bma
.nallocs
>= *nmap
)
4418 /* Else go on to the next record. */
4420 if (!xfs_iext_next_extent(ifp
, &bma
.icur
, &bma
.got
))
4425 error
= xfs_bmap_btree_to_extents(tp
, ip
, bma
.cur
, &bma
.logflags
,
4430 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
||
4431 XFS_IFORK_NEXTENTS(ip
, whichfork
) >
4432 XFS_IFORK_MAXEXT(ip
, whichfork
));
4433 xfs_bmapi_finish(&bma
, whichfork
, 0);
4434 xfs_bmap_validate_ret(orig_bno
, orig_len
, orig_flags
, orig_mval
,
4438 xfs_bmapi_finish(&bma
, whichfork
, error
);
4443 * Convert an existing delalloc extent to real blocks based on file offset. This
4444 * attempts to allocate the entire delalloc extent and may require multiple
4445 * invocations to allocate the target offset if a large enough physical extent
4449 xfs_bmapi_convert_delalloc(
4450 struct xfs_inode
*ip
,
4452 xfs_fileoff_t offset_fsb
,
4453 struct xfs_bmbt_irec
*imap
,
4456 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4457 struct xfs_mount
*mp
= ip
->i_mount
;
4458 struct xfs_bmalloca bma
= { NULL
};
4459 struct xfs_trans
*tp
;
4463 * Space for the extent and indirect blocks was reserved when the
4464 * delalloc extent was created so there's no need to do so here.
4466 error
= xfs_trans_alloc(mp
, &M_RES(mp
)->tr_write
, 0, 0,
4467 XFS_TRANS_RESERVE
, &tp
);
4471 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
4472 xfs_trans_ijoin(tp
, ip
, 0);
4474 if (!xfs_iext_lookup_extent(ip
, ifp
, offset_fsb
, &bma
.icur
, &bma
.got
) ||
4475 bma
.got
.br_startoff
> offset_fsb
) {
4477 * No extent found in the range we are trying to convert. This
4478 * should only happen for the COW fork, where another thread
4479 * might have moved the extent to the data fork in the meantime.
4481 WARN_ON_ONCE(whichfork
!= XFS_COW_FORK
);
4483 goto out_trans_cancel
;
4487 * If we find a real extent here we raced with another thread converting
4488 * the extent. Just return the real extent at this offset.
4490 if (!isnullstartblock(bma
.got
.br_startblock
)) {
4492 *seq
= READ_ONCE(ifp
->if_seq
);
4493 goto out_trans_cancel
;
4499 bma
.offset
= bma
.got
.br_startoff
;
4500 bma
.length
= max_t(xfs_filblks_t
, bma
.got
.br_blockcount
, MAXEXTLEN
);
4501 bma
.total
= XFS_EXTENTADD_SPACE_RES(ip
->i_mount
, XFS_DATA_FORK
);
4502 bma
.minleft
= xfs_bmapi_minleft(tp
, ip
, whichfork
);
4503 if (whichfork
== XFS_COW_FORK
)
4504 bma
.flags
= XFS_BMAPI_COWFORK
| XFS_BMAPI_PREALLOC
;
4506 if (!xfs_iext_peek_prev_extent(ifp
, &bma
.icur
, &bma
.prev
))
4507 bma
.prev
.br_startoff
= NULLFILEOFF
;
4509 error
= xfs_bmapi_allocate(&bma
);
4514 if (WARN_ON_ONCE(bma
.blkno
== NULLFSBLOCK
))
4516 error
= -EFSCORRUPTED
;
4517 if (WARN_ON_ONCE(!xfs_valid_startblock(ip
, bma
.got
.br_startblock
)))
4520 XFS_STATS_ADD(mp
, xs_xstrat_bytes
, XFS_FSB_TO_B(mp
, bma
.length
));
4521 XFS_STATS_INC(mp
, xs_xstrat_quick
);
4523 ASSERT(!isnullstartblock(bma
.got
.br_startblock
));
4525 *seq
= READ_ONCE(ifp
->if_seq
);
4527 if (whichfork
== XFS_COW_FORK
)
4528 xfs_refcount_alloc_cow_extent(tp
, bma
.blkno
, bma
.length
);
4530 error
= xfs_bmap_btree_to_extents(tp
, ip
, bma
.cur
, &bma
.logflags
,
4535 xfs_bmapi_finish(&bma
, whichfork
, 0);
4536 error
= xfs_trans_commit(tp
);
4537 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
4541 xfs_bmapi_finish(&bma
, whichfork
, error
);
4543 xfs_trans_cancel(tp
);
4544 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
4550 struct xfs_trans
*tp
,
4551 struct xfs_inode
*ip
,
4554 xfs_fsblock_t startblock
,
4557 struct xfs_mount
*mp
= ip
->i_mount
;
4558 struct xfs_ifork
*ifp
;
4559 struct xfs_btree_cur
*cur
= NULL
;
4560 struct xfs_bmbt_irec got
;
4561 struct xfs_iext_cursor icur
;
4562 int whichfork
= xfs_bmapi_whichfork(flags
);
4563 int logflags
= 0, error
;
4565 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4567 ASSERT(len
<= (xfs_filblks_t
)MAXEXTLEN
);
4568 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
4569 ASSERT(!(flags
& ~(XFS_BMAPI_ATTRFORK
| XFS_BMAPI_PREALLOC
|
4570 XFS_BMAPI_NORMAP
)));
4571 ASSERT((flags
& (XFS_BMAPI_ATTRFORK
| XFS_BMAPI_PREALLOC
)) !=
4572 (XFS_BMAPI_ATTRFORK
| XFS_BMAPI_PREALLOC
));
4574 if (unlikely(XFS_TEST_ERROR(
4575 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
4576 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
4577 mp
, XFS_ERRTAG_BMAPIFORMAT
))) {
4578 XFS_ERROR_REPORT("xfs_bmapi_remap", XFS_ERRLEVEL_LOW
, mp
);
4579 return -EFSCORRUPTED
;
4582 if (XFS_FORCED_SHUTDOWN(mp
))
4585 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
4586 error
= xfs_iread_extents(tp
, ip
, whichfork
);
4591 if (xfs_iext_lookup_extent(ip
, ifp
, bno
, &icur
, &got
)) {
4592 /* make sure we only reflink into a hole. */
4593 ASSERT(got
.br_startoff
> bno
);
4594 ASSERT(got
.br_startoff
- bno
>= len
);
4597 ip
->i_d
.di_nblocks
+= len
;
4598 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
4600 if (ifp
->if_flags
& XFS_IFBROOT
) {
4601 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
4602 cur
->bc_private
.b
.flags
= 0;
4605 got
.br_startoff
= bno
;
4606 got
.br_startblock
= startblock
;
4607 got
.br_blockcount
= len
;
4608 if (flags
& XFS_BMAPI_PREALLOC
)
4609 got
.br_state
= XFS_EXT_UNWRITTEN
;
4611 got
.br_state
= XFS_EXT_NORM
;
4613 error
= xfs_bmap_add_extent_hole_real(tp
, ip
, whichfork
, &icur
,
4614 &cur
, &got
, &logflags
, flags
);
4618 error
= xfs_bmap_btree_to_extents(tp
, ip
, cur
, &logflags
, whichfork
);
4621 if (ip
->i_d
.di_format
!= XFS_DINODE_FMT_EXTENTS
)
4622 logflags
&= ~XFS_ILOG_DEXT
;
4623 else if (ip
->i_d
.di_format
!= XFS_DINODE_FMT_BTREE
)
4624 logflags
&= ~XFS_ILOG_DBROOT
;
4627 xfs_trans_log_inode(tp
, ip
, logflags
);
4629 xfs_btree_del_cursor(cur
, error
);
4634 * When a delalloc extent is split (e.g., due to a hole punch), the original
4635 * indlen reservation must be shared across the two new extents that are left
4638 * Given the original reservation and the worst case indlen for the two new
4639 * extents (as calculated by xfs_bmap_worst_indlen()), split the original
4640 * reservation fairly across the two new extents. If necessary, steal available
4641 * blocks from a deleted extent to make up a reservation deficiency (e.g., if
4642 * ores == 1). The number of stolen blocks is returned. The availability and
4643 * subsequent accounting of stolen blocks is the responsibility of the caller.
4645 static xfs_filblks_t
4646 xfs_bmap_split_indlen(
4647 xfs_filblks_t ores
, /* original res. */
4648 xfs_filblks_t
*indlen1
, /* ext1 worst indlen */
4649 xfs_filblks_t
*indlen2
, /* ext2 worst indlen */
4650 xfs_filblks_t avail
) /* stealable blocks */
4652 xfs_filblks_t len1
= *indlen1
;
4653 xfs_filblks_t len2
= *indlen2
;
4654 xfs_filblks_t nres
= len1
+ len2
; /* new total res. */
4655 xfs_filblks_t stolen
= 0;
4656 xfs_filblks_t resfactor
;
4659 * Steal as many blocks as we can to try and satisfy the worst case
4660 * indlen for both new extents.
4662 if (ores
< nres
&& avail
)
4663 stolen
= XFS_FILBLKS_MIN(nres
- ores
, avail
);
4666 /* nothing else to do if we've satisfied the new reservation */
4671 * We can't meet the total required reservation for the two extents.
4672 * Calculate the percent of the overall shortage between both extents
4673 * and apply this percentage to each of the requested indlen values.
4674 * This distributes the shortage fairly and reduces the chances that one
4675 * of the two extents is left with nothing when extents are repeatedly
4678 resfactor
= (ores
* 100);
4679 do_div(resfactor
, nres
);
4684 ASSERT(len1
+ len2
<= ores
);
4685 ASSERT(len1
< *indlen1
&& len2
< *indlen2
);
4688 * Hand out the remainder to each extent. If one of the two reservations
4689 * is zero, we want to make sure that one gets a block first. The loop
4690 * below starts with len1, so hand len2 a block right off the bat if it
4693 ores
-= (len1
+ len2
);
4694 ASSERT((*indlen1
- len1
) + (*indlen2
- len2
) >= ores
);
4695 if (ores
&& !len2
&& *indlen2
) {
4700 if (len1
< *indlen1
) {
4706 if (len2
< *indlen2
) {
4719 xfs_bmap_del_extent_delay(
4720 struct xfs_inode
*ip
,
4722 struct xfs_iext_cursor
*icur
,
4723 struct xfs_bmbt_irec
*got
,
4724 struct xfs_bmbt_irec
*del
)
4726 struct xfs_mount
*mp
= ip
->i_mount
;
4727 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4728 struct xfs_bmbt_irec
new;
4729 int64_t da_old
, da_new
, da_diff
= 0;
4730 xfs_fileoff_t del_endoff
, got_endoff
;
4731 xfs_filblks_t got_indlen
, new_indlen
, stolen
;
4732 int state
= xfs_bmap_fork_to_state(whichfork
);
4736 XFS_STATS_INC(mp
, xs_del_exlist
);
4738 isrt
= (whichfork
== XFS_DATA_FORK
) && XFS_IS_REALTIME_INODE(ip
);
4739 del_endoff
= del
->br_startoff
+ del
->br_blockcount
;
4740 got_endoff
= got
->br_startoff
+ got
->br_blockcount
;
4741 da_old
= startblockval(got
->br_startblock
);
4744 ASSERT(del
->br_blockcount
> 0);
4745 ASSERT(got
->br_startoff
<= del
->br_startoff
);
4746 ASSERT(got_endoff
>= del_endoff
);
4749 uint64_t rtexts
= XFS_FSB_TO_B(mp
, del
->br_blockcount
);
4751 do_div(rtexts
, mp
->m_sb
.sb_rextsize
);
4752 xfs_mod_frextents(mp
, rtexts
);
4756 * Update the inode delalloc counter now and wait to update the
4757 * sb counters as we might have to borrow some blocks for the
4758 * indirect block accounting.
4760 error
= xfs_trans_reserve_quota_nblks(NULL
, ip
,
4761 -((long)del
->br_blockcount
), 0,
4762 isrt
? XFS_QMOPT_RES_RTBLKS
: XFS_QMOPT_RES_REGBLKS
);
4765 ip
->i_delayed_blks
-= del
->br_blockcount
;
4767 if (got
->br_startoff
== del
->br_startoff
)
4768 state
|= BMAP_LEFT_FILLING
;
4769 if (got_endoff
== del_endoff
)
4770 state
|= BMAP_RIGHT_FILLING
;
4772 switch (state
& (BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
)) {
4773 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
4775 * Matches the whole extent. Delete the entry.
4777 xfs_iext_remove(ip
, icur
, state
);
4778 xfs_iext_prev(ifp
, icur
);
4780 case BMAP_LEFT_FILLING
:
4782 * Deleting the first part of the extent.
4784 got
->br_startoff
= del_endoff
;
4785 got
->br_blockcount
-= del
->br_blockcount
;
4786 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
,
4787 got
->br_blockcount
), da_old
);
4788 got
->br_startblock
= nullstartblock((int)da_new
);
4789 xfs_iext_update_extent(ip
, state
, icur
, got
);
4791 case BMAP_RIGHT_FILLING
:
4793 * Deleting the last part of the extent.
4795 got
->br_blockcount
= got
->br_blockcount
- del
->br_blockcount
;
4796 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
,
4797 got
->br_blockcount
), da_old
);
4798 got
->br_startblock
= nullstartblock((int)da_new
);
4799 xfs_iext_update_extent(ip
, state
, icur
, got
);
4803 * Deleting the middle of the extent.
4805 * Distribute the original indlen reservation across the two new
4806 * extents. Steal blocks from the deleted extent if necessary.
4807 * Stealing blocks simply fudges the fdblocks accounting below.
4808 * Warn if either of the new indlen reservations is zero as this
4809 * can lead to delalloc problems.
4811 got
->br_blockcount
= del
->br_startoff
- got
->br_startoff
;
4812 got_indlen
= xfs_bmap_worst_indlen(ip
, got
->br_blockcount
);
4814 new.br_blockcount
= got_endoff
- del_endoff
;
4815 new_indlen
= xfs_bmap_worst_indlen(ip
, new.br_blockcount
);
4817 WARN_ON_ONCE(!got_indlen
|| !new_indlen
);
4818 stolen
= xfs_bmap_split_indlen(da_old
, &got_indlen
, &new_indlen
,
4819 del
->br_blockcount
);
4821 got
->br_startblock
= nullstartblock((int)got_indlen
);
4823 new.br_startoff
= del_endoff
;
4824 new.br_state
= got
->br_state
;
4825 new.br_startblock
= nullstartblock((int)new_indlen
);
4827 xfs_iext_update_extent(ip
, state
, icur
, got
);
4828 xfs_iext_next(ifp
, icur
);
4829 xfs_iext_insert(ip
, icur
, &new, state
);
4831 da_new
= got_indlen
+ new_indlen
- stolen
;
4832 del
->br_blockcount
-= stolen
;
4836 ASSERT(da_old
>= da_new
);
4837 da_diff
= da_old
- da_new
;
4839 da_diff
+= del
->br_blockcount
;
4841 xfs_mod_fdblocks(mp
, da_diff
, false);
4842 xfs_mod_delalloc(mp
, -da_diff
);
4848 xfs_bmap_del_extent_cow(
4849 struct xfs_inode
*ip
,
4850 struct xfs_iext_cursor
*icur
,
4851 struct xfs_bmbt_irec
*got
,
4852 struct xfs_bmbt_irec
*del
)
4854 struct xfs_mount
*mp
= ip
->i_mount
;
4855 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, XFS_COW_FORK
);
4856 struct xfs_bmbt_irec
new;
4857 xfs_fileoff_t del_endoff
, got_endoff
;
4858 int state
= BMAP_COWFORK
;
4860 XFS_STATS_INC(mp
, xs_del_exlist
);
4862 del_endoff
= del
->br_startoff
+ del
->br_blockcount
;
4863 got_endoff
= got
->br_startoff
+ got
->br_blockcount
;
4865 ASSERT(del
->br_blockcount
> 0);
4866 ASSERT(got
->br_startoff
<= del
->br_startoff
);
4867 ASSERT(got_endoff
>= del_endoff
);
4868 ASSERT(!isnullstartblock(got
->br_startblock
));
4870 if (got
->br_startoff
== del
->br_startoff
)
4871 state
|= BMAP_LEFT_FILLING
;
4872 if (got_endoff
== del_endoff
)
4873 state
|= BMAP_RIGHT_FILLING
;
4875 switch (state
& (BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
)) {
4876 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
4878 * Matches the whole extent. Delete the entry.
4880 xfs_iext_remove(ip
, icur
, state
);
4881 xfs_iext_prev(ifp
, icur
);
4883 case BMAP_LEFT_FILLING
:
4885 * Deleting the first part of the extent.
4887 got
->br_startoff
= del_endoff
;
4888 got
->br_blockcount
-= del
->br_blockcount
;
4889 got
->br_startblock
= del
->br_startblock
+ del
->br_blockcount
;
4890 xfs_iext_update_extent(ip
, state
, icur
, got
);
4892 case BMAP_RIGHT_FILLING
:
4894 * Deleting the last part of the extent.
4896 got
->br_blockcount
-= del
->br_blockcount
;
4897 xfs_iext_update_extent(ip
, state
, icur
, got
);
4901 * Deleting the middle of the extent.
4903 got
->br_blockcount
= del
->br_startoff
- got
->br_startoff
;
4905 new.br_startoff
= del_endoff
;
4906 new.br_blockcount
= got_endoff
- del_endoff
;
4907 new.br_state
= got
->br_state
;
4908 new.br_startblock
= del
->br_startblock
+ del
->br_blockcount
;
4910 xfs_iext_update_extent(ip
, state
, icur
, got
);
4911 xfs_iext_next(ifp
, icur
);
4912 xfs_iext_insert(ip
, icur
, &new, state
);
4915 ip
->i_delayed_blks
-= del
->br_blockcount
;
4919 * Called by xfs_bmapi to update file extent records and the btree
4920 * after removing space.
4922 STATIC
int /* error */
4923 xfs_bmap_del_extent_real(
4924 xfs_inode_t
*ip
, /* incore inode pointer */
4925 xfs_trans_t
*tp
, /* current transaction pointer */
4926 struct xfs_iext_cursor
*icur
,
4927 xfs_btree_cur_t
*cur
, /* if null, not a btree */
4928 xfs_bmbt_irec_t
*del
, /* data to remove from extents */
4929 int *logflagsp
, /* inode logging flags */
4930 int whichfork
, /* data or attr fork */
4931 int bflags
) /* bmapi flags */
4933 xfs_fsblock_t del_endblock
=0; /* first block past del */
4934 xfs_fileoff_t del_endoff
; /* first offset past del */
4935 int do_fx
; /* free extent at end of routine */
4936 int error
; /* error return value */
4937 int flags
= 0;/* inode logging flags */
4938 struct xfs_bmbt_irec got
; /* current extent entry */
4939 xfs_fileoff_t got_endoff
; /* first offset past got */
4940 int i
; /* temp state */
4941 struct xfs_ifork
*ifp
; /* inode fork pointer */
4942 xfs_mount_t
*mp
; /* mount structure */
4943 xfs_filblks_t nblks
; /* quota/sb block count */
4944 xfs_bmbt_irec_t
new; /* new record to be inserted */
4946 uint qfield
; /* quota field to update */
4947 int state
= xfs_bmap_fork_to_state(whichfork
);
4948 struct xfs_bmbt_irec old
;
4951 XFS_STATS_INC(mp
, xs_del_exlist
);
4953 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4954 ASSERT(del
->br_blockcount
> 0);
4955 xfs_iext_get_extent(ifp
, icur
, &got
);
4956 ASSERT(got
.br_startoff
<= del
->br_startoff
);
4957 del_endoff
= del
->br_startoff
+ del
->br_blockcount
;
4958 got_endoff
= got
.br_startoff
+ got
.br_blockcount
;
4959 ASSERT(got_endoff
>= del_endoff
);
4960 ASSERT(!isnullstartblock(got
.br_startblock
));
4965 * If it's the case where the directory code is running with no block
4966 * reservation, and the deleted block is in the middle of its extent,
4967 * and the resulting insert of an extent would cause transformation to
4968 * btree format, then reject it. The calling code will then swap blocks
4969 * around instead. We have to do this now, rather than waiting for the
4970 * conversion to btree format, since the transaction will be dirty then.
4972 if (tp
->t_blk_res
== 0 &&
4973 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
&&
4974 XFS_IFORK_NEXTENTS(ip
, whichfork
) >=
4975 XFS_IFORK_MAXEXT(ip
, whichfork
) &&
4976 del
->br_startoff
> got
.br_startoff
&& del_endoff
< got_endoff
)
4979 flags
= XFS_ILOG_CORE
;
4980 if (whichfork
== XFS_DATA_FORK
&& XFS_IS_REALTIME_INODE(ip
)) {
4985 bno
= div_u64_rem(del
->br_startblock
, mp
->m_sb
.sb_rextsize
,
4988 len
= div_u64_rem(del
->br_blockcount
, mp
->m_sb
.sb_rextsize
,
4992 error
= xfs_rtfree_extent(tp
, bno
, (xfs_extlen_t
)len
);
4996 nblks
= len
* mp
->m_sb
.sb_rextsize
;
4997 qfield
= XFS_TRANS_DQ_RTBCOUNT
;
5000 nblks
= del
->br_blockcount
;
5001 qfield
= XFS_TRANS_DQ_BCOUNT
;
5004 del_endblock
= del
->br_startblock
+ del
->br_blockcount
;
5006 error
= xfs_bmbt_lookup_eq(cur
, &got
, &i
);
5009 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
5012 if (got
.br_startoff
== del
->br_startoff
)
5013 state
|= BMAP_LEFT_FILLING
;
5014 if (got_endoff
== del_endoff
)
5015 state
|= BMAP_RIGHT_FILLING
;
5017 switch (state
& (BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
)) {
5018 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
5020 * Matches the whole extent. Delete the entry.
5022 xfs_iext_remove(ip
, icur
, state
);
5023 xfs_iext_prev(ifp
, icur
);
5024 XFS_IFORK_NEXT_SET(ip
, whichfork
,
5025 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
5026 flags
|= XFS_ILOG_CORE
;
5028 flags
|= xfs_ilog_fext(whichfork
);
5031 if ((error
= xfs_btree_delete(cur
, &i
)))
5033 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
5035 case BMAP_LEFT_FILLING
:
5037 * Deleting the first part of the extent.
5039 got
.br_startoff
= del_endoff
;
5040 got
.br_startblock
= del_endblock
;
5041 got
.br_blockcount
-= del
->br_blockcount
;
5042 xfs_iext_update_extent(ip
, state
, icur
, &got
);
5044 flags
|= xfs_ilog_fext(whichfork
);
5047 error
= xfs_bmbt_update(cur
, &got
);
5051 case BMAP_RIGHT_FILLING
:
5053 * Deleting the last part of the extent.
5055 got
.br_blockcount
-= del
->br_blockcount
;
5056 xfs_iext_update_extent(ip
, state
, icur
, &got
);
5058 flags
|= xfs_ilog_fext(whichfork
);
5061 error
= xfs_bmbt_update(cur
, &got
);
5067 * Deleting the middle of the extent.
5071 got
.br_blockcount
= del
->br_startoff
- got
.br_startoff
;
5072 xfs_iext_update_extent(ip
, state
, icur
, &got
);
5074 new.br_startoff
= del_endoff
;
5075 new.br_blockcount
= got_endoff
- del_endoff
;
5076 new.br_state
= got
.br_state
;
5077 new.br_startblock
= del_endblock
;
5079 flags
|= XFS_ILOG_CORE
;
5081 error
= xfs_bmbt_update(cur
, &got
);
5084 error
= xfs_btree_increment(cur
, 0, &i
);
5087 cur
->bc_rec
.b
= new;
5088 error
= xfs_btree_insert(cur
, &i
);
5089 if (error
&& error
!= -ENOSPC
)
5092 * If get no-space back from btree insert, it tried a
5093 * split, and we have a zero block reservation. Fix up
5094 * our state and return the error.
5096 if (error
== -ENOSPC
) {
5098 * Reset the cursor, don't trust it after any
5101 error
= xfs_bmbt_lookup_eq(cur
, &got
, &i
);
5104 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
5106 * Update the btree record back
5107 * to the original value.
5109 error
= xfs_bmbt_update(cur
, &old
);
5113 * Reset the extent record back
5114 * to the original value.
5116 xfs_iext_update_extent(ip
, state
, icur
, &old
);
5121 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
5123 flags
|= xfs_ilog_fext(whichfork
);
5124 XFS_IFORK_NEXT_SET(ip
, whichfork
,
5125 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
5126 xfs_iext_next(ifp
, icur
);
5127 xfs_iext_insert(ip
, icur
, &new, state
);
5131 /* remove reverse mapping */
5132 xfs_rmap_unmap_extent(tp
, ip
, whichfork
, del
);
5135 * If we need to, add to list of extents to delete.
5137 if (do_fx
&& !(bflags
& XFS_BMAPI_REMAP
)) {
5138 if (xfs_is_reflink_inode(ip
) && whichfork
== XFS_DATA_FORK
) {
5139 xfs_refcount_decrease_extent(tp
, del
);
5141 __xfs_bmap_add_free(tp
, del
->br_startblock
,
5142 del
->br_blockcount
, NULL
,
5143 (bflags
& XFS_BMAPI_NODISCARD
) ||
5144 del
->br_state
== XFS_EXT_UNWRITTEN
);
5149 * Adjust inode # blocks in the file.
5152 ip
->i_d
.di_nblocks
-= nblks
;
5154 * Adjust quota data.
5156 if (qfield
&& !(bflags
& XFS_BMAPI_REMAP
))
5157 xfs_trans_mod_dquot_byino(tp
, ip
, qfield
, (long)-nblks
);
5165 * Unmap (remove) blocks from a file.
5166 * If nexts is nonzero then the number of extents to remove is limited to
5167 * that value. If not all extents in the block range can be removed then
5172 struct xfs_trans
*tp
, /* transaction pointer */
5173 struct xfs_inode
*ip
, /* incore inode */
5174 xfs_fileoff_t start
, /* first file offset deleted */
5175 xfs_filblks_t
*rlen
, /* i/o: amount remaining */
5176 int flags
, /* misc flags */
5177 xfs_extnum_t nexts
) /* number of extents max */
5179 struct xfs_btree_cur
*cur
; /* bmap btree cursor */
5180 struct xfs_bmbt_irec del
; /* extent being deleted */
5181 int error
; /* error return value */
5182 xfs_extnum_t extno
; /* extent number in list */
5183 struct xfs_bmbt_irec got
; /* current extent record */
5184 struct xfs_ifork
*ifp
; /* inode fork pointer */
5185 int isrt
; /* freeing in rt area */
5186 int logflags
; /* transaction logging flags */
5187 xfs_extlen_t mod
; /* rt extent offset */
5188 struct xfs_mount
*mp
; /* mount structure */
5189 int tmp_logflags
; /* partial logging flags */
5190 int wasdel
; /* was a delayed alloc extent */
5191 int whichfork
; /* data or attribute fork */
5193 xfs_filblks_t len
= *rlen
; /* length to unmap in file */
5194 xfs_fileoff_t max_len
;
5195 xfs_agnumber_t prev_agno
= NULLAGNUMBER
, agno
;
5197 struct xfs_iext_cursor icur
;
5200 trace_xfs_bunmap(ip
, start
, len
, flags
, _RET_IP_
);
5202 whichfork
= xfs_bmapi_whichfork(flags
);
5203 ASSERT(whichfork
!= XFS_COW_FORK
);
5204 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5206 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
5207 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)) {
5208 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW
,
5210 return -EFSCORRUPTED
;
5213 if (XFS_FORCED_SHUTDOWN(mp
))
5216 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
5221 * Guesstimate how many blocks we can unmap without running the risk of
5222 * blowing out the transaction with a mix of EFIs and reflink
5225 if (tp
&& xfs_is_reflink_inode(ip
) && whichfork
== XFS_DATA_FORK
)
5226 max_len
= min(len
, xfs_refcount_max_unmap(tp
->t_log_res
));
5230 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
5231 (error
= xfs_iread_extents(tp
, ip
, whichfork
)))
5233 if (xfs_iext_count(ifp
) == 0) {
5237 XFS_STATS_INC(mp
, xs_blk_unmap
);
5238 isrt
= (whichfork
== XFS_DATA_FORK
) && XFS_IS_REALTIME_INODE(ip
);
5241 if (!xfs_iext_lookup_extent_before(ip
, ifp
, &end
, &icur
, &got
)) {
5248 if (ifp
->if_flags
& XFS_IFBROOT
) {
5249 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
);
5250 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
5251 cur
->bc_private
.b
.flags
= 0;
5257 * Synchronize by locking the bitmap inode.
5259 xfs_ilock(mp
->m_rbmip
, XFS_ILOCK_EXCL
|XFS_ILOCK_RTBITMAP
);
5260 xfs_trans_ijoin(tp
, mp
->m_rbmip
, XFS_ILOCK_EXCL
);
5261 xfs_ilock(mp
->m_rsumip
, XFS_ILOCK_EXCL
|XFS_ILOCK_RTSUM
);
5262 xfs_trans_ijoin(tp
, mp
->m_rsumip
, XFS_ILOCK_EXCL
);
5266 while (end
!= (xfs_fileoff_t
)-1 && end
>= start
&&
5267 (nexts
== 0 || extno
< nexts
) && max_len
> 0) {
5269 * Is the found extent after a hole in which end lives?
5270 * Just back up to the previous extent, if so.
5272 if (got
.br_startoff
> end
&&
5273 !xfs_iext_prev_extent(ifp
, &icur
, &got
)) {
5278 * Is the last block of this extent before the range
5279 * we're supposed to delete? If so, we're done.
5281 end
= XFS_FILEOFF_MIN(end
,
5282 got
.br_startoff
+ got
.br_blockcount
- 1);
5286 * Then deal with the (possibly delayed) allocated space
5290 wasdel
= isnullstartblock(del
.br_startblock
);
5293 * Make sure we don't touch multiple AGF headers out of order
5294 * in a single transaction, as that could cause AB-BA deadlocks.
5297 agno
= XFS_FSB_TO_AGNO(mp
, del
.br_startblock
);
5298 if (prev_agno
!= NULLAGNUMBER
&& prev_agno
> agno
)
5302 if (got
.br_startoff
< start
) {
5303 del
.br_startoff
= start
;
5304 del
.br_blockcount
-= start
- got
.br_startoff
;
5306 del
.br_startblock
+= start
- got
.br_startoff
;
5308 if (del
.br_startoff
+ del
.br_blockcount
> end
+ 1)
5309 del
.br_blockcount
= end
+ 1 - del
.br_startoff
;
5311 /* How much can we safely unmap? */
5312 if (max_len
< del
.br_blockcount
) {
5313 del
.br_startoff
+= del
.br_blockcount
- max_len
;
5315 del
.br_startblock
+= del
.br_blockcount
- max_len
;
5316 del
.br_blockcount
= max_len
;
5322 sum
= del
.br_startblock
+ del
.br_blockcount
;
5323 div_u64_rem(sum
, mp
->m_sb
.sb_rextsize
, &mod
);
5326 * Realtime extent not lined up at the end.
5327 * The extent could have been split into written
5328 * and unwritten pieces, or we could just be
5329 * unmapping part of it. But we can't really
5330 * get rid of part of a realtime extent.
5332 if (del
.br_state
== XFS_EXT_UNWRITTEN
) {
5334 * This piece is unwritten, or we're not
5335 * using unwritten extents. Skip over it.
5338 end
-= mod
> del
.br_blockcount
?
5339 del
.br_blockcount
: mod
;
5340 if (end
< got
.br_startoff
&&
5341 !xfs_iext_prev_extent(ifp
, &icur
, &got
)) {
5348 * It's written, turn it unwritten.
5349 * This is better than zeroing it.
5351 ASSERT(del
.br_state
== XFS_EXT_NORM
);
5352 ASSERT(tp
->t_blk_res
> 0);
5354 * If this spans a realtime extent boundary,
5355 * chop it back to the start of the one we end at.
5357 if (del
.br_blockcount
> mod
) {
5358 del
.br_startoff
+= del
.br_blockcount
- mod
;
5359 del
.br_startblock
+= del
.br_blockcount
- mod
;
5360 del
.br_blockcount
= mod
;
5362 del
.br_state
= XFS_EXT_UNWRITTEN
;
5363 error
= xfs_bmap_add_extent_unwritten_real(tp
, ip
,
5364 whichfork
, &icur
, &cur
, &del
,
5370 div_u64_rem(del
.br_startblock
, mp
->m_sb
.sb_rextsize
, &mod
);
5373 * Realtime extent is lined up at the end but not
5374 * at the front. We'll get rid of full extents if
5377 mod
= mp
->m_sb
.sb_rextsize
- mod
;
5378 if (del
.br_blockcount
> mod
) {
5379 del
.br_blockcount
-= mod
;
5380 del
.br_startoff
+= mod
;
5381 del
.br_startblock
+= mod
;
5382 } else if (del
.br_startoff
== start
&&
5383 (del
.br_state
== XFS_EXT_UNWRITTEN
||
5384 tp
->t_blk_res
== 0)) {
5386 * Can't make it unwritten. There isn't
5387 * a full extent here so just skip it.
5389 ASSERT(end
>= del
.br_blockcount
);
5390 end
-= del
.br_blockcount
;
5391 if (got
.br_startoff
> end
&&
5392 !xfs_iext_prev_extent(ifp
, &icur
, &got
)) {
5397 } else if (del
.br_state
== XFS_EXT_UNWRITTEN
) {
5398 struct xfs_bmbt_irec prev
;
5401 * This one is already unwritten.
5402 * It must have a written left neighbor.
5403 * Unwrite the killed part of that one and
5406 if (!xfs_iext_prev_extent(ifp
, &icur
, &prev
))
5408 ASSERT(prev
.br_state
== XFS_EXT_NORM
);
5409 ASSERT(!isnullstartblock(prev
.br_startblock
));
5410 ASSERT(del
.br_startblock
==
5411 prev
.br_startblock
+ prev
.br_blockcount
);
5412 if (prev
.br_startoff
< start
) {
5413 mod
= start
- prev
.br_startoff
;
5414 prev
.br_blockcount
-= mod
;
5415 prev
.br_startblock
+= mod
;
5416 prev
.br_startoff
= start
;
5418 prev
.br_state
= XFS_EXT_UNWRITTEN
;
5419 error
= xfs_bmap_add_extent_unwritten_real(tp
,
5420 ip
, whichfork
, &icur
, &cur
,
5426 ASSERT(del
.br_state
== XFS_EXT_NORM
);
5427 del
.br_state
= XFS_EXT_UNWRITTEN
;
5428 error
= xfs_bmap_add_extent_unwritten_real(tp
,
5429 ip
, whichfork
, &icur
, &cur
,
5439 error
= xfs_bmap_del_extent_delay(ip
, whichfork
, &icur
,
5442 error
= xfs_bmap_del_extent_real(ip
, tp
, &icur
, cur
,
5443 &del
, &tmp_logflags
, whichfork
,
5445 logflags
|= tmp_logflags
;
5451 max_len
-= del
.br_blockcount
;
5452 end
= del
.br_startoff
- 1;
5455 * If not done go on to the next (previous) record.
5457 if (end
!= (xfs_fileoff_t
)-1 && end
>= start
) {
5458 if (!xfs_iext_get_extent(ifp
, &icur
, &got
) ||
5459 (got
.br_startoff
> end
&&
5460 !xfs_iext_prev_extent(ifp
, &icur
, &got
))) {
5467 if (done
|| end
== (xfs_fileoff_t
)-1 || end
< start
)
5470 *rlen
= end
- start
+ 1;
5473 * Convert to a btree if necessary.
5475 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
5476 ASSERT(cur
== NULL
);
5477 error
= xfs_bmap_extents_to_btree(tp
, ip
, &cur
, 0,
5478 &tmp_logflags
, whichfork
);
5479 logflags
|= tmp_logflags
;
5481 error
= xfs_bmap_btree_to_extents(tp
, ip
, cur
, &logflags
,
5487 * Log everything. Do this after conversion, there's no point in
5488 * logging the extent records if we've converted to btree format.
5490 if ((logflags
& xfs_ilog_fext(whichfork
)) &&
5491 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
5492 logflags
&= ~xfs_ilog_fext(whichfork
);
5493 else if ((logflags
& xfs_ilog_fbroot(whichfork
)) &&
5494 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)
5495 logflags
&= ~xfs_ilog_fbroot(whichfork
);
5497 * Log inode even in the error case, if the transaction
5498 * is dirty we'll need to shut down the filesystem.
5501 xfs_trans_log_inode(tp
, ip
, logflags
);
5504 cur
->bc_private
.b
.allocated
= 0;
5505 xfs_btree_del_cursor(cur
, error
);
5510 /* Unmap a range of a file. */
5514 struct xfs_inode
*ip
,
5523 error
= __xfs_bunmapi(tp
, ip
, bno
, &len
, flags
, nexts
);
5529 * Determine whether an extent shift can be accomplished by a merge with the
5530 * extent that precedes the target hole of the shift.
5534 struct xfs_bmbt_irec
*left
, /* preceding extent */
5535 struct xfs_bmbt_irec
*got
, /* current extent to shift */
5536 xfs_fileoff_t shift
) /* shift fsb */
5538 xfs_fileoff_t startoff
;
5540 startoff
= got
->br_startoff
- shift
;
5543 * The extent, once shifted, must be adjacent in-file and on-disk with
5544 * the preceding extent.
5546 if ((left
->br_startoff
+ left
->br_blockcount
!= startoff
) ||
5547 (left
->br_startblock
+ left
->br_blockcount
!= got
->br_startblock
) ||
5548 (left
->br_state
!= got
->br_state
) ||
5549 (left
->br_blockcount
+ got
->br_blockcount
> MAXEXTLEN
))
5556 * A bmap extent shift adjusts the file offset of an extent to fill a preceding
5557 * hole in the file. If an extent shift would result in the extent being fully
5558 * adjacent to the extent that currently precedes the hole, we can merge with
5559 * the preceding extent rather than do the shift.
5561 * This function assumes the caller has verified a shift-by-merge is possible
5562 * with the provided extents via xfs_bmse_can_merge().
5566 struct xfs_trans
*tp
,
5567 struct xfs_inode
*ip
,
5569 xfs_fileoff_t shift
, /* shift fsb */
5570 struct xfs_iext_cursor
*icur
,
5571 struct xfs_bmbt_irec
*got
, /* extent to shift */
5572 struct xfs_bmbt_irec
*left
, /* preceding extent */
5573 struct xfs_btree_cur
*cur
,
5574 int *logflags
) /* output */
5576 struct xfs_bmbt_irec
new;
5577 xfs_filblks_t blockcount
;
5579 struct xfs_mount
*mp
= ip
->i_mount
;
5581 blockcount
= left
->br_blockcount
+ got
->br_blockcount
;
5583 ASSERT(xfs_isilocked(ip
, XFS_IOLOCK_EXCL
));
5584 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
5585 ASSERT(xfs_bmse_can_merge(left
, got
, shift
));
5588 new.br_blockcount
= blockcount
;
5591 * Update the on-disk extent count, the btree if necessary and log the
5594 XFS_IFORK_NEXT_SET(ip
, whichfork
,
5595 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
5596 *logflags
|= XFS_ILOG_CORE
;
5598 *logflags
|= XFS_ILOG_DEXT
;
5602 /* lookup and remove the extent to merge */
5603 error
= xfs_bmbt_lookup_eq(cur
, got
, &i
);
5606 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
5608 error
= xfs_btree_delete(cur
, &i
);
5611 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
5613 /* lookup and update size of the previous extent */
5614 error
= xfs_bmbt_lookup_eq(cur
, left
, &i
);
5617 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
5619 error
= xfs_bmbt_update(cur
, &new);
5623 /* change to extent format if required after extent removal */
5624 error
= xfs_bmap_btree_to_extents(tp
, ip
, cur
, logflags
, whichfork
);
5629 xfs_iext_remove(ip
, icur
, 0);
5630 xfs_iext_prev(XFS_IFORK_PTR(ip
, whichfork
), icur
);
5631 xfs_iext_update_extent(ip
, xfs_bmap_fork_to_state(whichfork
), icur
,
5634 /* update reverse mapping. rmap functions merge the rmaps for us */
5635 xfs_rmap_unmap_extent(tp
, ip
, whichfork
, got
);
5636 memcpy(&new, got
, sizeof(new));
5637 new.br_startoff
= left
->br_startoff
+ left
->br_blockcount
;
5638 xfs_rmap_map_extent(tp
, ip
, whichfork
, &new);
5643 xfs_bmap_shift_update_extent(
5644 struct xfs_trans
*tp
,
5645 struct xfs_inode
*ip
,
5647 struct xfs_iext_cursor
*icur
,
5648 struct xfs_bmbt_irec
*got
,
5649 struct xfs_btree_cur
*cur
,
5651 xfs_fileoff_t startoff
)
5653 struct xfs_mount
*mp
= ip
->i_mount
;
5654 struct xfs_bmbt_irec prev
= *got
;
5657 *logflags
|= XFS_ILOG_CORE
;
5659 got
->br_startoff
= startoff
;
5662 error
= xfs_bmbt_lookup_eq(cur
, &prev
, &i
);
5665 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
5667 error
= xfs_bmbt_update(cur
, got
);
5671 *logflags
|= XFS_ILOG_DEXT
;
5674 xfs_iext_update_extent(ip
, xfs_bmap_fork_to_state(whichfork
), icur
,
5677 /* update reverse mapping */
5678 xfs_rmap_unmap_extent(tp
, ip
, whichfork
, &prev
);
5679 xfs_rmap_map_extent(tp
, ip
, whichfork
, got
);
5684 xfs_bmap_collapse_extents(
5685 struct xfs_trans
*tp
,
5686 struct xfs_inode
*ip
,
5687 xfs_fileoff_t
*next_fsb
,
5688 xfs_fileoff_t offset_shift_fsb
,
5691 int whichfork
= XFS_DATA_FORK
;
5692 struct xfs_mount
*mp
= ip
->i_mount
;
5693 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5694 struct xfs_btree_cur
*cur
= NULL
;
5695 struct xfs_bmbt_irec got
, prev
;
5696 struct xfs_iext_cursor icur
;
5697 xfs_fileoff_t new_startoff
;
5701 if (unlikely(XFS_TEST_ERROR(
5702 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
5703 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
5704 mp
, XFS_ERRTAG_BMAPIFORMAT
))) {
5705 XFS_ERROR_REPORT(__func__
, XFS_ERRLEVEL_LOW
, mp
);
5706 return -EFSCORRUPTED
;
5709 if (XFS_FORCED_SHUTDOWN(mp
))
5712 ASSERT(xfs_isilocked(ip
, XFS_IOLOCK_EXCL
| XFS_ILOCK_EXCL
));
5714 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
5715 error
= xfs_iread_extents(tp
, ip
, whichfork
);
5720 if (ifp
->if_flags
& XFS_IFBROOT
) {
5721 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
5722 cur
->bc_private
.b
.flags
= 0;
5725 if (!xfs_iext_lookup_extent(ip
, ifp
, *next_fsb
, &icur
, &got
)) {
5729 XFS_WANT_CORRUPTED_GOTO(mp
, !isnullstartblock(got
.br_startblock
),
5732 new_startoff
= got
.br_startoff
- offset_shift_fsb
;
5733 if (xfs_iext_peek_prev_extent(ifp
, &icur
, &prev
)) {
5734 if (new_startoff
< prev
.br_startoff
+ prev
.br_blockcount
) {
5739 if (xfs_bmse_can_merge(&prev
, &got
, offset_shift_fsb
)) {
5740 error
= xfs_bmse_merge(tp
, ip
, whichfork
,
5741 offset_shift_fsb
, &icur
, &got
, &prev
,
5748 if (got
.br_startoff
< offset_shift_fsb
) {
5754 error
= xfs_bmap_shift_update_extent(tp
, ip
, whichfork
, &icur
, &got
,
5755 cur
, &logflags
, new_startoff
);
5760 if (!xfs_iext_next_extent(ifp
, &icur
, &got
)) {
5765 *next_fsb
= got
.br_startoff
;
5768 xfs_btree_del_cursor(cur
, error
);
5770 xfs_trans_log_inode(tp
, ip
, logflags
);
5774 /* Make sure we won't be right-shifting an extent past the maximum bound. */
5776 xfs_bmap_can_insert_extents(
5777 struct xfs_inode
*ip
,
5779 xfs_fileoff_t shift
)
5781 struct xfs_bmbt_irec got
;
5785 ASSERT(xfs_isilocked(ip
, XFS_IOLOCK_EXCL
));
5787 if (XFS_FORCED_SHUTDOWN(ip
->i_mount
))
5790 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
5791 error
= xfs_bmap_last_extent(NULL
, ip
, XFS_DATA_FORK
, &got
, &is_empty
);
5792 if (!error
&& !is_empty
&& got
.br_startoff
>= off
&&
5793 ((got
.br_startoff
+ shift
) & BMBT_STARTOFF_MASK
) < got
.br_startoff
)
5795 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
5801 xfs_bmap_insert_extents(
5802 struct xfs_trans
*tp
,
5803 struct xfs_inode
*ip
,
5804 xfs_fileoff_t
*next_fsb
,
5805 xfs_fileoff_t offset_shift_fsb
,
5807 xfs_fileoff_t stop_fsb
)
5809 int whichfork
= XFS_DATA_FORK
;
5810 struct xfs_mount
*mp
= ip
->i_mount
;
5811 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5812 struct xfs_btree_cur
*cur
= NULL
;
5813 struct xfs_bmbt_irec got
, next
;
5814 struct xfs_iext_cursor icur
;
5815 xfs_fileoff_t new_startoff
;
5819 if (unlikely(XFS_TEST_ERROR(
5820 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
5821 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
5822 mp
, XFS_ERRTAG_BMAPIFORMAT
))) {
5823 XFS_ERROR_REPORT(__func__
, XFS_ERRLEVEL_LOW
, mp
);
5824 return -EFSCORRUPTED
;
5827 if (XFS_FORCED_SHUTDOWN(mp
))
5830 ASSERT(xfs_isilocked(ip
, XFS_IOLOCK_EXCL
| XFS_ILOCK_EXCL
));
5832 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
5833 error
= xfs_iread_extents(tp
, ip
, whichfork
);
5838 if (ifp
->if_flags
& XFS_IFBROOT
) {
5839 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
5840 cur
->bc_private
.b
.flags
= 0;
5843 if (*next_fsb
== NULLFSBLOCK
) {
5844 xfs_iext_last(ifp
, &icur
);
5845 if (!xfs_iext_get_extent(ifp
, &icur
, &got
) ||
5846 stop_fsb
> got
.br_startoff
) {
5851 if (!xfs_iext_lookup_extent(ip
, ifp
, *next_fsb
, &icur
, &got
)) {
5856 XFS_WANT_CORRUPTED_GOTO(mp
, !isnullstartblock(got
.br_startblock
),
5859 if (stop_fsb
>= got
.br_startoff
+ got
.br_blockcount
) {
5864 new_startoff
= got
.br_startoff
+ offset_shift_fsb
;
5865 if (xfs_iext_peek_next_extent(ifp
, &icur
, &next
)) {
5866 if (new_startoff
+ got
.br_blockcount
> next
.br_startoff
) {
5872 * Unlike a left shift (which involves a hole punch), a right
5873 * shift does not modify extent neighbors in any way. We should
5874 * never find mergeable extents in this scenario. Check anyways
5875 * and warn if we encounter two extents that could be one.
5877 if (xfs_bmse_can_merge(&got
, &next
, offset_shift_fsb
))
5881 error
= xfs_bmap_shift_update_extent(tp
, ip
, whichfork
, &icur
, &got
,
5882 cur
, &logflags
, new_startoff
);
5886 if (!xfs_iext_prev_extent(ifp
, &icur
, &got
) ||
5887 stop_fsb
>= got
.br_startoff
+ got
.br_blockcount
) {
5892 *next_fsb
= got
.br_startoff
;
5895 xfs_btree_del_cursor(cur
, error
);
5897 xfs_trans_log_inode(tp
, ip
, logflags
);
5902 * Splits an extent into two extents at split_fsb block such that it is the
5903 * first block of the current_ext. @ext is a target extent to be split.
5904 * @split_fsb is a block where the extents is split. If split_fsb lies in a
5905 * hole or the first block of extents, just return 0.
5908 xfs_bmap_split_extent_at(
5909 struct xfs_trans
*tp
,
5910 struct xfs_inode
*ip
,
5911 xfs_fileoff_t split_fsb
)
5913 int whichfork
= XFS_DATA_FORK
;
5914 struct xfs_btree_cur
*cur
= NULL
;
5915 struct xfs_bmbt_irec got
;
5916 struct xfs_bmbt_irec
new; /* split extent */
5917 struct xfs_mount
*mp
= ip
->i_mount
;
5918 struct xfs_ifork
*ifp
;
5919 xfs_fsblock_t gotblkcnt
; /* new block count for got */
5920 struct xfs_iext_cursor icur
;
5925 if (unlikely(XFS_TEST_ERROR(
5926 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
5927 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
5928 mp
, XFS_ERRTAG_BMAPIFORMAT
))) {
5929 XFS_ERROR_REPORT("xfs_bmap_split_extent_at",
5930 XFS_ERRLEVEL_LOW
, mp
);
5931 return -EFSCORRUPTED
;
5934 if (XFS_FORCED_SHUTDOWN(mp
))
5937 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5938 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
5939 /* Read in all the extents */
5940 error
= xfs_iread_extents(tp
, ip
, whichfork
);
5946 * If there are not extents, or split_fsb lies in a hole we are done.
5948 if (!xfs_iext_lookup_extent(ip
, ifp
, split_fsb
, &icur
, &got
) ||
5949 got
.br_startoff
>= split_fsb
)
5952 gotblkcnt
= split_fsb
- got
.br_startoff
;
5953 new.br_startoff
= split_fsb
;
5954 new.br_startblock
= got
.br_startblock
+ gotblkcnt
;
5955 new.br_blockcount
= got
.br_blockcount
- gotblkcnt
;
5956 new.br_state
= got
.br_state
;
5958 if (ifp
->if_flags
& XFS_IFBROOT
) {
5959 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
5960 cur
->bc_private
.b
.flags
= 0;
5961 error
= xfs_bmbt_lookup_eq(cur
, &got
, &i
);
5964 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, del_cursor
);
5967 got
.br_blockcount
= gotblkcnt
;
5968 xfs_iext_update_extent(ip
, xfs_bmap_fork_to_state(whichfork
), &icur
,
5971 logflags
= XFS_ILOG_CORE
;
5973 error
= xfs_bmbt_update(cur
, &got
);
5977 logflags
|= XFS_ILOG_DEXT
;
5979 /* Add new extent */
5980 xfs_iext_next(ifp
, &icur
);
5981 xfs_iext_insert(ip
, &icur
, &new, 0);
5982 XFS_IFORK_NEXT_SET(ip
, whichfork
,
5983 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
5986 error
= xfs_bmbt_lookup_eq(cur
, &new, &i
);
5989 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, del_cursor
);
5990 error
= xfs_btree_insert(cur
, &i
);
5993 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, del_cursor
);
5997 * Convert to a btree if necessary.
5999 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
6000 int tmp_logflags
; /* partial log flag return val */
6002 ASSERT(cur
== NULL
);
6003 error
= xfs_bmap_extents_to_btree(tp
, ip
, &cur
, 0,
6004 &tmp_logflags
, whichfork
);
6005 logflags
|= tmp_logflags
;
6010 cur
->bc_private
.b
.allocated
= 0;
6011 xfs_btree_del_cursor(cur
, error
);
6015 xfs_trans_log_inode(tp
, ip
, logflags
);
6020 xfs_bmap_split_extent(
6021 struct xfs_inode
*ip
,
6022 xfs_fileoff_t split_fsb
)
6024 struct xfs_mount
*mp
= ip
->i_mount
;
6025 struct xfs_trans
*tp
;
6028 error
= xfs_trans_alloc(mp
, &M_RES(mp
)->tr_write
,
6029 XFS_DIOSTRAT_SPACE_RES(mp
, 0), 0, 0, &tp
);
6033 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
6034 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
);
6036 error
= xfs_bmap_split_extent_at(tp
, ip
, split_fsb
);
6040 return xfs_trans_commit(tp
);
6043 xfs_trans_cancel(tp
);
6047 /* Deferred mapping is only for real extents in the data fork. */
6049 xfs_bmap_is_update_needed(
6050 struct xfs_bmbt_irec
*bmap
)
6052 return bmap
->br_startblock
!= HOLESTARTBLOCK
&&
6053 bmap
->br_startblock
!= DELAYSTARTBLOCK
;
6056 /* Record a bmap intent. */
6059 struct xfs_trans
*tp
,
6060 enum xfs_bmap_intent_type type
,
6061 struct xfs_inode
*ip
,
6063 struct xfs_bmbt_irec
*bmap
)
6065 struct xfs_bmap_intent
*bi
;
6067 trace_xfs_bmap_defer(tp
->t_mountp
,
6068 XFS_FSB_TO_AGNO(tp
->t_mountp
, bmap
->br_startblock
),
6070 XFS_FSB_TO_AGBNO(tp
->t_mountp
, bmap
->br_startblock
),
6071 ip
->i_ino
, whichfork
,
6073 bmap
->br_blockcount
,
6076 bi
= kmem_alloc(sizeof(struct xfs_bmap_intent
), KM_NOFS
);
6077 INIT_LIST_HEAD(&bi
->bi_list
);
6080 bi
->bi_whichfork
= whichfork
;
6081 bi
->bi_bmap
= *bmap
;
6083 xfs_defer_add(tp
, XFS_DEFER_OPS_TYPE_BMAP
, &bi
->bi_list
);
6087 /* Map an extent into a file. */
6089 xfs_bmap_map_extent(
6090 struct xfs_trans
*tp
,
6091 struct xfs_inode
*ip
,
6092 struct xfs_bmbt_irec
*PREV
)
6094 if (!xfs_bmap_is_update_needed(PREV
))
6097 __xfs_bmap_add(tp
, XFS_BMAP_MAP
, ip
, XFS_DATA_FORK
, PREV
);
6100 /* Unmap an extent out of a file. */
6102 xfs_bmap_unmap_extent(
6103 struct xfs_trans
*tp
,
6104 struct xfs_inode
*ip
,
6105 struct xfs_bmbt_irec
*PREV
)
6107 if (!xfs_bmap_is_update_needed(PREV
))
6110 __xfs_bmap_add(tp
, XFS_BMAP_UNMAP
, ip
, XFS_DATA_FORK
, PREV
);
6114 * Process one of the deferred bmap operations. We pass back the
6115 * btree cursor to maintain our lock on the bmapbt between calls.
6118 xfs_bmap_finish_one(
6119 struct xfs_trans
*tp
,
6120 struct xfs_inode
*ip
,
6121 enum xfs_bmap_intent_type type
,
6123 xfs_fileoff_t startoff
,
6124 xfs_fsblock_t startblock
,
6125 xfs_filblks_t
*blockcount
,
6130 ASSERT(tp
->t_firstblock
== NULLFSBLOCK
);
6132 trace_xfs_bmap_deferred(tp
->t_mountp
,
6133 XFS_FSB_TO_AGNO(tp
->t_mountp
, startblock
), type
,
6134 XFS_FSB_TO_AGBNO(tp
->t_mountp
, startblock
),
6135 ip
->i_ino
, whichfork
, startoff
, *blockcount
, state
);
6137 if (WARN_ON_ONCE(whichfork
!= XFS_DATA_FORK
))
6138 return -EFSCORRUPTED
;
6140 if (XFS_TEST_ERROR(false, tp
->t_mountp
,
6141 XFS_ERRTAG_BMAP_FINISH_ONE
))
6146 error
= xfs_bmapi_remap(tp
, ip
, startoff
, *blockcount
,
6150 case XFS_BMAP_UNMAP
:
6151 error
= __xfs_bunmapi(tp
, ip
, startoff
, blockcount
,
6152 XFS_BMAPI_REMAP
, 1);
6156 error
= -EFSCORRUPTED
;
6162 /* Check that an inode's extent does not have invalid flags or bad ranges. */
6164 xfs_bmap_validate_extent(
6165 struct xfs_inode
*ip
,
6167 struct xfs_bmbt_irec
*irec
)
6169 struct xfs_mount
*mp
= ip
->i_mount
;
6170 xfs_fsblock_t endfsb
;
6173 isrt
= XFS_IS_REALTIME_INODE(ip
);
6174 endfsb
= irec
->br_startblock
+ irec
->br_blockcount
- 1;
6176 if (!xfs_verify_rtbno(mp
, irec
->br_startblock
))
6177 return __this_address
;
6178 if (!xfs_verify_rtbno(mp
, endfsb
))
6179 return __this_address
;
6181 if (!xfs_verify_fsbno(mp
, irec
->br_startblock
))
6182 return __this_address
;
6183 if (!xfs_verify_fsbno(mp
, endfsb
))
6184 return __this_address
;
6185 if (XFS_FSB_TO_AGNO(mp
, irec
->br_startblock
) !=
6186 XFS_FSB_TO_AGNO(mp
, endfsb
))
6187 return __this_address
;
6189 if (irec
->br_state
!= XFS_EXT_NORM
&& whichfork
!= XFS_DATA_FORK
)
6190 return __this_address
;