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"
16 #include "xfs_da_format.h"
17 #include "xfs_da_btree.h"
19 #include "xfs_inode.h"
20 #include "xfs_btree.h"
21 #include "xfs_trans.h"
22 #include "xfs_alloc.h"
24 #include "xfs_bmap_btree.h"
25 #include "xfs_errortag.h"
26 #include "xfs_trans_space.h"
27 #include "xfs_trace.h"
28 #include "xfs_attr_leaf.h"
29 #include "xfs_quota_defs.h"
31 #include "xfs_ag_resv.h"
32 #include "xfs_refcount.h"
35 kmem_zone_t
*xfs_bmap_free_item_zone
;
38 * Miscellaneous helper functions
42 * Compute and fill in the value of the maximum depth of a bmap btree
43 * in this filesystem. Done once, during mount.
46 xfs_bmap_compute_maxlevels(
47 xfs_mount_t
*mp
, /* file system mount structure */
48 int whichfork
) /* data or attr fork */
50 int level
; /* btree level */
51 uint maxblocks
; /* max blocks at this level */
52 uint maxleafents
; /* max leaf entries possible */
53 int maxrootrecs
; /* max records in root block */
54 int minleafrecs
; /* min records in leaf block */
55 int minnoderecs
; /* min records in node block */
56 int sz
; /* root block size */
59 * The maximum number of extents in a file, hence the maximum
60 * number of leaf entries, is controlled by the type of di_nextents
61 * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
62 * (a signed 16-bit number, xfs_aextnum_t).
64 * Note that we can no longer assume that if we are in ATTR1 that
65 * the fork offset of all the inodes will be
66 * (xfs_default_attroffset(ip) >> 3) because we could have mounted
67 * with ATTR2 and then mounted back with ATTR1, keeping the
68 * di_forkoff's fixed but probably at various positions. Therefore,
69 * for both ATTR1 and ATTR2 we have to assume the worst case scenario
70 * of a minimum size available.
72 if (whichfork
== XFS_DATA_FORK
) {
73 maxleafents
= MAXEXTNUM
;
74 sz
= XFS_BMDR_SPACE_CALC(MINDBTPTRS
);
76 maxleafents
= MAXAEXTNUM
;
77 sz
= XFS_BMDR_SPACE_CALC(MINABTPTRS
);
79 maxrootrecs
= xfs_bmdr_maxrecs(sz
, 0);
80 minleafrecs
= mp
->m_bmap_dmnr
[0];
81 minnoderecs
= mp
->m_bmap_dmnr
[1];
82 maxblocks
= (maxleafents
+ minleafrecs
- 1) / minleafrecs
;
83 for (level
= 1; maxblocks
> 1; level
++) {
84 if (maxblocks
<= maxrootrecs
)
87 maxblocks
= (maxblocks
+ minnoderecs
- 1) / minnoderecs
;
89 mp
->m_bm_maxlevels
[whichfork
] = level
;
92 STATIC
int /* error */
94 struct xfs_btree_cur
*cur
,
95 struct xfs_bmbt_irec
*irec
,
96 int *stat
) /* success/failure */
98 cur
->bc_rec
.b
= *irec
;
99 return xfs_btree_lookup(cur
, XFS_LOOKUP_EQ
, stat
);
102 STATIC
int /* error */
103 xfs_bmbt_lookup_first(
104 struct xfs_btree_cur
*cur
,
105 int *stat
) /* success/failure */
107 cur
->bc_rec
.b
.br_startoff
= 0;
108 cur
->bc_rec
.b
.br_startblock
= 0;
109 cur
->bc_rec
.b
.br_blockcount
= 0;
110 return xfs_btree_lookup(cur
, XFS_LOOKUP_GE
, stat
);
114 * Check if the inode needs to be converted to btree format.
116 static inline bool xfs_bmap_needs_btree(struct xfs_inode
*ip
, int whichfork
)
118 return whichfork
!= XFS_COW_FORK
&&
119 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
&&
120 XFS_IFORK_NEXTENTS(ip
, whichfork
) >
121 XFS_IFORK_MAXEXT(ip
, whichfork
);
125 * Check if the inode should be converted to extent format.
127 static inline bool xfs_bmap_wants_extents(struct xfs_inode
*ip
, int whichfork
)
129 return whichfork
!= XFS_COW_FORK
&&
130 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
&&
131 XFS_IFORK_NEXTENTS(ip
, whichfork
) <=
132 XFS_IFORK_MAXEXT(ip
, whichfork
);
136 * Update the record referred to by cur to the value given by irec
137 * This either works (return 0) or gets an EFSCORRUPTED error.
141 struct xfs_btree_cur
*cur
,
142 struct xfs_bmbt_irec
*irec
)
144 union xfs_btree_rec rec
;
146 xfs_bmbt_disk_set_all(&rec
.bmbt
, irec
);
147 return xfs_btree_update(cur
, &rec
);
151 * Compute the worst-case number of indirect blocks that will be used
152 * for ip's delayed extent of length "len".
155 xfs_bmap_worst_indlen(
156 xfs_inode_t
*ip
, /* incore inode pointer */
157 xfs_filblks_t len
) /* delayed extent length */
159 int level
; /* btree level number */
160 int maxrecs
; /* maximum record count at this level */
161 xfs_mount_t
*mp
; /* mount structure */
162 xfs_filblks_t rval
; /* return value */
165 maxrecs
= mp
->m_bmap_dmxr
[0];
166 for (level
= 0, rval
= 0;
167 level
< XFS_BM_MAXLEVELS(mp
, XFS_DATA_FORK
);
170 do_div(len
, maxrecs
);
173 return rval
+ XFS_BM_MAXLEVELS(mp
, XFS_DATA_FORK
) -
176 maxrecs
= mp
->m_bmap_dmxr
[1];
182 * Calculate the default attribute fork offset for newly created inodes.
185 xfs_default_attroffset(
186 struct xfs_inode
*ip
)
188 struct xfs_mount
*mp
= ip
->i_mount
;
191 if (mp
->m_sb
.sb_inodesize
== 256) {
192 offset
= XFS_LITINO(mp
, ip
->i_d
.di_version
) -
193 XFS_BMDR_SPACE_CALC(MINABTPTRS
);
195 offset
= XFS_BMDR_SPACE_CALC(6 * MINABTPTRS
);
198 ASSERT(offset
< XFS_LITINO(mp
, ip
->i_d
.di_version
));
203 * Helper routine to reset inode di_forkoff field when switching
204 * attribute fork from local to extent format - we reset it where
205 * possible to make space available for inline data fork extents.
208 xfs_bmap_forkoff_reset(
212 if (whichfork
== XFS_ATTR_FORK
&&
213 ip
->i_d
.di_format
!= XFS_DINODE_FMT_DEV
&&
214 ip
->i_d
.di_format
!= XFS_DINODE_FMT_BTREE
) {
215 uint dfl_forkoff
= xfs_default_attroffset(ip
) >> 3;
217 if (dfl_forkoff
> ip
->i_d
.di_forkoff
)
218 ip
->i_d
.di_forkoff
= dfl_forkoff
;
223 STATIC
struct xfs_buf
*
225 struct xfs_btree_cur
*cur
,
228 struct xfs_log_item
*lip
;
234 for (i
= 0; i
< XFS_BTREE_MAXLEVELS
; i
++) {
235 if (!cur
->bc_bufs
[i
])
237 if (XFS_BUF_ADDR(cur
->bc_bufs
[i
]) == bno
)
238 return cur
->bc_bufs
[i
];
241 /* Chase down all the log items to see if the bp is there */
242 list_for_each_entry(lip
, &cur
->bc_tp
->t_items
, li_trans
) {
243 struct xfs_buf_log_item
*bip
= (struct xfs_buf_log_item
*)lip
;
245 if (bip
->bli_item
.li_type
== XFS_LI_BUF
&&
246 XFS_BUF_ADDR(bip
->bli_buf
) == bno
)
255 struct xfs_btree_block
*block
,
261 __be64
*pp
, *thispa
; /* pointer to block address */
262 xfs_bmbt_key_t
*prevp
, *keyp
;
264 ASSERT(be16_to_cpu(block
->bb_level
) > 0);
267 for( i
= 1; i
<= xfs_btree_get_numrecs(block
); i
++) {
268 dmxr
= mp
->m_bmap_dmxr
[0];
269 keyp
= XFS_BMBT_KEY_ADDR(mp
, block
, i
);
272 ASSERT(be64_to_cpu(prevp
->br_startoff
) <
273 be64_to_cpu(keyp
->br_startoff
));
278 * Compare the block numbers to see if there are dups.
281 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, i
, sz
);
283 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, i
, dmxr
);
285 for (j
= i
+1; j
<= be16_to_cpu(block
->bb_numrecs
); j
++) {
287 thispa
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, j
, sz
);
289 thispa
= XFS_BMBT_PTR_ADDR(mp
, block
, j
, dmxr
);
290 if (*thispa
== *pp
) {
291 xfs_warn(mp
, "%s: thispa(%d) == pp(%d) %Ld",
293 (unsigned long long)be64_to_cpu(*thispa
));
294 xfs_err(mp
, "%s: ptrs are equal in node\n",
296 xfs_force_shutdown(mp
, SHUTDOWN_CORRUPT_INCORE
);
303 * Check that the extents for the inode ip are in the right order in all
304 * btree leaves. THis becomes prohibitively expensive for large extent count
305 * files, so don't bother with inodes that have more than 10,000 extents in
306 * them. The btree record ordering checks will still be done, so for such large
307 * bmapbt constructs that is going to catch most corruptions.
310 xfs_bmap_check_leaf_extents(
311 xfs_btree_cur_t
*cur
, /* btree cursor or null */
312 xfs_inode_t
*ip
, /* incore inode pointer */
313 int whichfork
) /* data or attr fork */
315 struct xfs_btree_block
*block
; /* current btree block */
316 xfs_fsblock_t bno
; /* block # of "block" */
317 xfs_buf_t
*bp
; /* buffer for "block" */
318 int error
; /* error return value */
319 xfs_extnum_t i
=0, j
; /* index into the extents list */
320 struct xfs_ifork
*ifp
; /* fork structure */
321 int level
; /* btree level, for checking */
322 xfs_mount_t
*mp
; /* file system mount structure */
323 __be64
*pp
; /* pointer to block address */
324 xfs_bmbt_rec_t
*ep
; /* pointer to current extent */
325 xfs_bmbt_rec_t last
= {0, 0}; /* last extent in prev block */
326 xfs_bmbt_rec_t
*nextp
; /* pointer to next extent */
329 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
) {
333 /* skip large extent count inodes */
334 if (ip
->i_d
.di_nextents
> 10000)
339 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
340 block
= ifp
->if_broot
;
342 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
344 level
= be16_to_cpu(block
->bb_level
);
346 xfs_check_block(block
, mp
, 1, ifp
->if_broot_bytes
);
347 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, 1, ifp
->if_broot_bytes
);
348 bno
= be64_to_cpu(*pp
);
350 ASSERT(bno
!= NULLFSBLOCK
);
351 ASSERT(XFS_FSB_TO_AGNO(mp
, bno
) < mp
->m_sb
.sb_agcount
);
352 ASSERT(XFS_FSB_TO_AGBNO(mp
, bno
) < mp
->m_sb
.sb_agblocks
);
355 * Go down the tree until leaf level is reached, following the first
356 * pointer (leftmost) at each level.
358 while (level
-- > 0) {
359 /* See if buf is in cur first */
361 bp
= xfs_bmap_get_bp(cur
, XFS_FSB_TO_DADDR(mp
, bno
));
364 error
= xfs_btree_read_bufl(mp
, NULL
, bno
, 0, &bp
,
370 block
= XFS_BUF_TO_BLOCK(bp
);
375 * Check this block for basic sanity (increasing keys and
376 * no duplicate blocks).
379 xfs_check_block(block
, mp
, 0, 0);
380 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, mp
->m_bmap_dmxr
[1]);
381 bno
= be64_to_cpu(*pp
);
382 XFS_WANT_CORRUPTED_GOTO(mp
,
383 xfs_verify_fsbno(mp
, bno
), error0
);
386 xfs_trans_brelse(NULL
, bp
);
391 * Here with bp and block set to the leftmost leaf node in the tree.
396 * Loop over all leaf nodes checking that all extents are in the right order.
399 xfs_fsblock_t nextbno
;
400 xfs_extnum_t num_recs
;
403 num_recs
= xfs_btree_get_numrecs(block
);
406 * Read-ahead the next leaf block, if any.
409 nextbno
= be64_to_cpu(block
->bb_u
.l
.bb_rightsib
);
412 * Check all the extents to make sure they are OK.
413 * If we had a previous block, the last entry should
414 * conform with the first entry in this one.
417 ep
= XFS_BMBT_REC_ADDR(mp
, block
, 1);
419 ASSERT(xfs_bmbt_disk_get_startoff(&last
) +
420 xfs_bmbt_disk_get_blockcount(&last
) <=
421 xfs_bmbt_disk_get_startoff(ep
));
423 for (j
= 1; j
< num_recs
; j
++) {
424 nextp
= XFS_BMBT_REC_ADDR(mp
, block
, j
+ 1);
425 ASSERT(xfs_bmbt_disk_get_startoff(ep
) +
426 xfs_bmbt_disk_get_blockcount(ep
) <=
427 xfs_bmbt_disk_get_startoff(nextp
));
435 xfs_trans_brelse(NULL
, bp
);
439 * If we've reached the end, stop.
441 if (bno
== NULLFSBLOCK
)
445 bp
= xfs_bmap_get_bp(cur
, XFS_FSB_TO_DADDR(mp
, bno
));
448 error
= xfs_btree_read_bufl(mp
, NULL
, bno
, 0, &bp
,
454 block
= XFS_BUF_TO_BLOCK(bp
);
460 xfs_warn(mp
, "%s: at error0", __func__
);
462 xfs_trans_brelse(NULL
, bp
);
464 xfs_warn(mp
, "%s: BAD after btree leaves for %d extents",
466 xfs_err(mp
, "%s: CORRUPTED BTREE OR SOMETHING", __func__
);
467 xfs_force_shutdown(mp
, SHUTDOWN_CORRUPT_INCORE
);
472 * Validate that the bmbt_irecs being returned from bmapi are valid
473 * given the caller's original parameters. Specifically check the
474 * ranges of the returned irecs to ensure that they only extend beyond
475 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
478 xfs_bmap_validate_ret(
482 xfs_bmbt_irec_t
*mval
,
486 int i
; /* index to map values */
488 ASSERT(ret_nmap
<= nmap
);
490 for (i
= 0; i
< ret_nmap
; i
++) {
491 ASSERT(mval
[i
].br_blockcount
> 0);
492 if (!(flags
& XFS_BMAPI_ENTIRE
)) {
493 ASSERT(mval
[i
].br_startoff
>= bno
);
494 ASSERT(mval
[i
].br_blockcount
<= len
);
495 ASSERT(mval
[i
].br_startoff
+ mval
[i
].br_blockcount
<=
498 ASSERT(mval
[i
].br_startoff
< bno
+ len
);
499 ASSERT(mval
[i
].br_startoff
+ mval
[i
].br_blockcount
>
503 mval
[i
- 1].br_startoff
+ mval
[i
- 1].br_blockcount
==
504 mval
[i
].br_startoff
);
505 ASSERT(mval
[i
].br_startblock
!= DELAYSTARTBLOCK
&&
506 mval
[i
].br_startblock
!= HOLESTARTBLOCK
);
507 ASSERT(mval
[i
].br_state
== XFS_EXT_NORM
||
508 mval
[i
].br_state
== XFS_EXT_UNWRITTEN
);
513 #define xfs_bmap_check_leaf_extents(cur, ip, whichfork) do { } while (0)
514 #define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap) do { } while (0)
518 * bmap free list manipulation functions
522 * Add the extent to the list of extents to be free at transaction end.
523 * The list is maintained sorted (by block number).
527 struct xfs_mount
*mp
,
528 struct xfs_defer_ops
*dfops
,
531 struct xfs_owner_info
*oinfo
,
534 struct xfs_extent_free_item
*new; /* new element */
539 ASSERT(bno
!= NULLFSBLOCK
);
541 ASSERT(len
<= MAXEXTLEN
);
542 ASSERT(!isnullstartblock(bno
));
543 agno
= XFS_FSB_TO_AGNO(mp
, bno
);
544 agbno
= XFS_FSB_TO_AGBNO(mp
, bno
);
545 ASSERT(agno
< mp
->m_sb
.sb_agcount
);
546 ASSERT(agbno
< mp
->m_sb
.sb_agblocks
);
547 ASSERT(len
< mp
->m_sb
.sb_agblocks
);
548 ASSERT(agbno
+ len
<= mp
->m_sb
.sb_agblocks
);
550 ASSERT(xfs_bmap_free_item_zone
!= NULL
);
552 new = kmem_zone_alloc(xfs_bmap_free_item_zone
, KM_SLEEP
);
553 new->xefi_startblock
= bno
;
554 new->xefi_blockcount
= (xfs_extlen_t
)len
;
556 new->xefi_oinfo
= *oinfo
;
558 xfs_rmap_skip_owner_update(&new->xefi_oinfo
);
559 new->xefi_skip_discard
= skip_discard
;
560 trace_xfs_bmap_free_defer(mp
, XFS_FSB_TO_AGNO(mp
, bno
), 0,
561 XFS_FSB_TO_AGBNO(mp
, bno
), len
);
562 xfs_defer_add(dfops
, XFS_DEFER_OPS_TYPE_FREE
, &new->xefi_list
);
566 * Inode fork format manipulation functions
570 * Transform a btree format file with only one leaf node, where the
571 * extents list will fit in the inode, into an extents format file.
572 * Since the file extents are already in-core, all we have to do is
573 * give up the space for the btree root and pitch the leaf block.
575 STATIC
int /* error */
576 xfs_bmap_btree_to_extents(
577 xfs_trans_t
*tp
, /* transaction pointer */
578 xfs_inode_t
*ip
, /* incore inode pointer */
579 xfs_btree_cur_t
*cur
, /* btree cursor */
580 int *logflagsp
, /* inode logging flags */
581 int whichfork
) /* data or attr fork */
584 struct xfs_btree_block
*cblock
;/* child btree block */
585 xfs_fsblock_t cbno
; /* child block number */
586 xfs_buf_t
*cbp
; /* child block's buffer */
587 int error
; /* error return value */
588 struct xfs_ifork
*ifp
; /* inode fork data */
589 xfs_mount_t
*mp
; /* mount point structure */
590 __be64
*pp
; /* ptr to block address */
591 struct xfs_btree_block
*rblock
;/* root btree block */
592 struct xfs_owner_info oinfo
;
595 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
596 ASSERT(whichfork
!= XFS_COW_FORK
);
597 ASSERT(ifp
->if_flags
& XFS_IFEXTENTS
);
598 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
);
599 rblock
= ifp
->if_broot
;
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);
603 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, rblock
, 1, ifp
->if_broot_bytes
);
604 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
, 0, &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(mp
, cur
->bc_tp
->t_dfops
, 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.
668 xfs_iroot_realloc(ip
, 1, whichfork
);
669 ifp
->if_flags
|= XFS_IFBROOT
;
674 block
= ifp
->if_broot
;
675 xfs_btree_init_block_int(mp
, block
, XFS_BUF_DADDR_NULL
,
676 XFS_BTNUM_BMAP
, 1, 1, ip
->i_ino
,
677 XFS_BTREE_LONG_PTRS
);
679 * Need a cursor. Can't allocate until bb_level is filled in.
681 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
682 cur
->bc_private
.b
.flags
= wasdel
? XFS_BTCUR_BPRV_WASDEL
: 0;
684 * Convert to a btree with two levels, one record in root.
686 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_BTREE
);
687 memset(&args
, 0, sizeof(args
));
690 xfs_rmap_ino_bmbt_owner(&args
.oinfo
, ip
->i_ino
, whichfork
);
691 if (tp
->t_firstblock
== NULLFSBLOCK
) {
692 args
.type
= XFS_ALLOCTYPE_START_BNO
;
693 args
.fsbno
= XFS_INO_TO_FSB(mp
, ip
->i_ino
);
694 } else if (tp
->t_flags
& XFS_TRANS_LOWMODE
) {
695 args
.type
= XFS_ALLOCTYPE_START_BNO
;
696 args
.fsbno
= tp
->t_firstblock
;
698 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
699 args
.fsbno
= tp
->t_firstblock
;
701 args
.minlen
= args
.maxlen
= args
.prod
= 1;
702 args
.wasdel
= wasdel
;
704 if ((error
= xfs_alloc_vextent(&args
))) {
705 xfs_iroot_realloc(ip
, -1, whichfork
);
706 ASSERT(ifp
->if_broot
== NULL
);
707 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_EXTENTS
);
708 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
712 if (WARN_ON_ONCE(args
.fsbno
== NULLFSBLOCK
)) {
713 xfs_iroot_realloc(ip
, -1, whichfork
);
714 ASSERT(ifp
->if_broot
== NULL
);
715 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_EXTENTS
);
716 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
720 * Allocation can't fail, the space was reserved.
722 ASSERT(tp
->t_firstblock
== NULLFSBLOCK
||
723 args
.agno
>= XFS_FSB_TO_AGNO(mp
, tp
->t_firstblock
));
724 tp
->t_firstblock
= args
.fsbno
;
725 cur
->bc_private
.b
.allocated
++;
726 ip
->i_d
.di_nblocks
++;
727 xfs_trans_mod_dquot_byino(tp
, ip
, XFS_TRANS_DQ_BCOUNT
, 1L);
728 abp
= xfs_btree_get_bufl(mp
, tp
, args
.fsbno
, 0);
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
);
771 * Convert a local file to an extents file.
772 * This code is out of bounds for data forks of regular files,
773 * since the file data needs to get logged so things will stay consistent.
774 * (The bmap-level manipulations are ok, though).
777 xfs_bmap_local_to_extents_empty(
778 struct xfs_inode
*ip
,
781 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
783 ASSERT(whichfork
!= XFS_COW_FORK
);
784 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
785 ASSERT(ifp
->if_bytes
== 0);
786 ASSERT(XFS_IFORK_NEXTENTS(ip
, whichfork
) == 0);
788 xfs_bmap_forkoff_reset(ip
, whichfork
);
789 ifp
->if_flags
&= ~XFS_IFINLINE
;
790 ifp
->if_flags
|= XFS_IFEXTENTS
;
791 ifp
->if_u1
.if_root
= NULL
;
793 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_EXTENTS
);
797 STATIC
int /* error */
798 xfs_bmap_local_to_extents(
799 xfs_trans_t
*tp
, /* transaction pointer */
800 xfs_inode_t
*ip
, /* incore inode pointer */
801 xfs_extlen_t total
, /* total blocks needed by transaction */
802 int *logflagsp
, /* inode logging flags */
804 void (*init_fn
)(struct xfs_trans
*tp
,
806 struct xfs_inode
*ip
,
807 struct xfs_ifork
*ifp
))
810 int flags
; /* logging flags returned */
811 struct xfs_ifork
*ifp
; /* inode fork pointer */
812 xfs_alloc_arg_t args
; /* allocation arguments */
813 xfs_buf_t
*bp
; /* buffer for extent block */
814 struct xfs_bmbt_irec rec
;
815 struct xfs_iext_cursor icur
;
818 * We don't want to deal with the case of keeping inode data inline yet.
819 * So sending the data fork of a regular inode is invalid.
821 ASSERT(!(S_ISREG(VFS_I(ip
)->i_mode
) && whichfork
== XFS_DATA_FORK
));
822 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
823 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
825 if (!ifp
->if_bytes
) {
826 xfs_bmap_local_to_extents_empty(ip
, whichfork
);
827 flags
= XFS_ILOG_CORE
;
833 ASSERT((ifp
->if_flags
& (XFS_IFINLINE
|XFS_IFEXTENTS
)) == XFS_IFINLINE
);
834 memset(&args
, 0, sizeof(args
));
836 args
.mp
= ip
->i_mount
;
837 xfs_rmap_ino_owner(&args
.oinfo
, ip
->i_ino
, whichfork
, 0);
839 * Allocate a block. We know we need only one, since the
840 * file currently fits in an inode.
842 if (tp
->t_firstblock
== NULLFSBLOCK
) {
843 args
.fsbno
= XFS_INO_TO_FSB(args
.mp
, ip
->i_ino
);
844 args
.type
= XFS_ALLOCTYPE_START_BNO
;
846 args
.fsbno
= tp
->t_firstblock
;
847 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
850 args
.minlen
= args
.maxlen
= args
.prod
= 1;
851 error
= xfs_alloc_vextent(&args
);
855 /* Can't fail, the space was reserved. */
856 ASSERT(args
.fsbno
!= NULLFSBLOCK
);
857 ASSERT(args
.len
== 1);
858 tp
->t_firstblock
= args
.fsbno
;
859 bp
= xfs_btree_get_bufl(args
.mp
, tp
, args
.fsbno
, 0);
862 * Initialize the block, copy the data and log the remote buffer.
864 * The callout is responsible for logging because the remote format
865 * might differ from the local format and thus we don't know how much to
866 * log here. Note that init_fn must also set the buffer log item type
869 init_fn(tp
, bp
, ip
, ifp
);
871 /* account for the change in fork size */
872 xfs_idata_realloc(ip
, -ifp
->if_bytes
, whichfork
);
873 xfs_bmap_local_to_extents_empty(ip
, whichfork
);
874 flags
|= XFS_ILOG_CORE
;
876 ifp
->if_u1
.if_root
= NULL
;
880 rec
.br_startblock
= args
.fsbno
;
881 rec
.br_blockcount
= 1;
882 rec
.br_state
= XFS_EXT_NORM
;
883 xfs_iext_first(ifp
, &icur
);
884 xfs_iext_insert(ip
, &icur
, &rec
, 0);
886 XFS_IFORK_NEXT_SET(ip
, whichfork
, 1);
887 ip
->i_d
.di_nblocks
= 1;
888 xfs_trans_mod_dquot_byino(tp
, ip
,
889 XFS_TRANS_DQ_BCOUNT
, 1L);
890 flags
|= xfs_ilog_fext(whichfork
);
898 * Called from xfs_bmap_add_attrfork to handle btree format files.
900 STATIC
int /* error */
901 xfs_bmap_add_attrfork_btree(
902 xfs_trans_t
*tp
, /* transaction pointer */
903 xfs_inode_t
*ip
, /* incore inode pointer */
904 int *flags
) /* inode logging flags */
906 xfs_btree_cur_t
*cur
; /* btree cursor */
907 int error
; /* error return value */
908 xfs_mount_t
*mp
; /* file system mount struct */
909 int stat
; /* newroot status */
912 if (ip
->i_df
.if_broot_bytes
<= XFS_IFORK_DSIZE(ip
))
913 *flags
|= XFS_ILOG_DBROOT
;
915 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, XFS_DATA_FORK
);
916 error
= xfs_bmbt_lookup_first(cur
, &stat
);
919 /* must be at least one entry */
920 XFS_WANT_CORRUPTED_GOTO(mp
, stat
== 1, error0
);
921 if ((error
= xfs_btree_new_iroot(cur
, flags
, &stat
)))
924 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
927 cur
->bc_private
.b
.allocated
= 0;
928 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
932 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
937 * Called from xfs_bmap_add_attrfork to handle extents format files.
939 STATIC
int /* error */
940 xfs_bmap_add_attrfork_extents(
941 struct xfs_trans
*tp
, /* transaction pointer */
942 struct xfs_inode
*ip
, /* incore inode pointer */
943 int *flags
) /* inode logging flags */
945 xfs_btree_cur_t
*cur
; /* bmap btree cursor */
946 int error
; /* error return value */
948 if (ip
->i_d
.di_nextents
* sizeof(xfs_bmbt_rec_t
) <= XFS_IFORK_DSIZE(ip
))
951 error
= xfs_bmap_extents_to_btree(tp
, ip
, &cur
, 0, flags
,
954 cur
->bc_private
.b
.allocated
= 0;
955 xfs_btree_del_cursor(cur
, error
);
961 * Called from xfs_bmap_add_attrfork to handle local format files. Each
962 * different data fork content type needs a different callout to do the
963 * conversion. Some are basic and only require special block initialisation
964 * callouts for the data formating, others (directories) are so specialised they
965 * handle everything themselves.
967 * XXX (dgc): investigate whether directory conversion can use the generic
968 * formatting callout. It should be possible - it's just a very complex
971 STATIC
int /* error */
972 xfs_bmap_add_attrfork_local(
973 struct xfs_trans
*tp
, /* transaction pointer */
974 struct xfs_inode
*ip
, /* incore inode pointer */
975 int *flags
) /* inode logging flags */
977 struct xfs_da_args dargs
; /* args for dir/attr code */
979 if (ip
->i_df
.if_bytes
<= XFS_IFORK_DSIZE(ip
))
982 if (S_ISDIR(VFS_I(ip
)->i_mode
)) {
983 memset(&dargs
, 0, sizeof(dargs
));
984 dargs
.geo
= ip
->i_mount
->m_dir_geo
;
986 dargs
.total
= dargs
.geo
->fsbcount
;
987 dargs
.whichfork
= XFS_DATA_FORK
;
989 return xfs_dir2_sf_to_block(&dargs
);
992 if (S_ISLNK(VFS_I(ip
)->i_mode
))
993 return xfs_bmap_local_to_extents(tp
, ip
, 1, flags
,
995 xfs_symlink_local_to_remote
);
997 /* should only be called for types that support local format data */
999 return -EFSCORRUPTED
;
1003 * Convert inode from non-attributed to attributed.
1004 * Must not be in a transaction, ip must not be locked.
1006 int /* error code */
1007 xfs_bmap_add_attrfork(
1008 xfs_inode_t
*ip
, /* incore inode pointer */
1009 int size
, /* space new attribute needs */
1010 int rsvd
) /* xact may use reserved blks */
1012 xfs_mount_t
*mp
; /* mount structure */
1013 xfs_trans_t
*tp
; /* transaction pointer */
1014 int blks
; /* space reservation */
1015 int version
= 1; /* superblock attr version */
1016 int logflags
; /* logging flags */
1017 int error
; /* error return value */
1019 ASSERT(XFS_IFORK_Q(ip
) == 0);
1022 ASSERT(!XFS_NOT_DQATTACHED(mp
, ip
));
1024 blks
= XFS_ADDAFORK_SPACE_RES(mp
);
1026 error
= xfs_trans_alloc(mp
, &M_RES(mp
)->tr_addafork
, blks
, 0,
1027 rsvd
? XFS_TRANS_RESERVE
: 0, &tp
);
1031 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
1032 error
= xfs_trans_reserve_quota_nblks(tp
, ip
, blks
, 0, rsvd
?
1033 XFS_QMOPT_RES_REGBLKS
| XFS_QMOPT_FORCE_RES
:
1034 XFS_QMOPT_RES_REGBLKS
);
1037 if (XFS_IFORK_Q(ip
))
1039 if (ip
->i_d
.di_anextents
!= 0) {
1040 error
= -EFSCORRUPTED
;
1043 if (ip
->i_d
.di_aformat
!= XFS_DINODE_FMT_EXTENTS
) {
1045 * For inodes coming from pre-6.2 filesystems.
1047 ASSERT(ip
->i_d
.di_aformat
== 0);
1048 ip
->i_d
.di_aformat
= XFS_DINODE_FMT_EXTENTS
;
1051 xfs_trans_ijoin(tp
, ip
, 0);
1052 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
1054 switch (ip
->i_d
.di_format
) {
1055 case XFS_DINODE_FMT_DEV
:
1056 ip
->i_d
.di_forkoff
= roundup(sizeof(xfs_dev_t
), 8) >> 3;
1058 case XFS_DINODE_FMT_LOCAL
:
1059 case XFS_DINODE_FMT_EXTENTS
:
1060 case XFS_DINODE_FMT_BTREE
:
1061 ip
->i_d
.di_forkoff
= xfs_attr_shortform_bytesfit(ip
, size
);
1062 if (!ip
->i_d
.di_forkoff
)
1063 ip
->i_d
.di_forkoff
= xfs_default_attroffset(ip
) >> 3;
1064 else if (mp
->m_flags
& XFS_MOUNT_ATTR2
)
1073 ASSERT(ip
->i_afp
== NULL
);
1074 ip
->i_afp
= kmem_zone_zalloc(xfs_ifork_zone
, KM_SLEEP
);
1075 ip
->i_afp
->if_flags
= XFS_IFEXTENTS
;
1077 switch (ip
->i_d
.di_format
) {
1078 case XFS_DINODE_FMT_LOCAL
:
1079 error
= xfs_bmap_add_attrfork_local(tp
, ip
, &logflags
);
1081 case XFS_DINODE_FMT_EXTENTS
:
1082 error
= xfs_bmap_add_attrfork_extents(tp
, ip
, &logflags
);
1084 case XFS_DINODE_FMT_BTREE
:
1085 error
= xfs_bmap_add_attrfork_btree(tp
, ip
, &logflags
);
1092 xfs_trans_log_inode(tp
, ip
, logflags
);
1095 if (!xfs_sb_version_hasattr(&mp
->m_sb
) ||
1096 (!xfs_sb_version_hasattr2(&mp
->m_sb
) && version
== 2)) {
1097 bool log_sb
= false;
1099 spin_lock(&mp
->m_sb_lock
);
1100 if (!xfs_sb_version_hasattr(&mp
->m_sb
)) {
1101 xfs_sb_version_addattr(&mp
->m_sb
);
1104 if (!xfs_sb_version_hasattr2(&mp
->m_sb
) && version
== 2) {
1105 xfs_sb_version_addattr2(&mp
->m_sb
);
1108 spin_unlock(&mp
->m_sb_lock
);
1113 xfs_defer_ijoin(tp
->t_dfops
, ip
);
1114 error
= xfs_trans_commit(tp
);
1115 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
1119 xfs_trans_cancel(tp
);
1120 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
1125 * Internal and external extent tree search functions.
1129 * Read in extents from a btree-format inode.
1133 struct xfs_trans
*tp
,
1134 struct xfs_inode
*ip
,
1137 struct xfs_mount
*mp
= ip
->i_mount
;
1138 int state
= xfs_bmap_fork_to_state(whichfork
);
1139 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1140 xfs_extnum_t nextents
= XFS_IFORK_NEXTENTS(ip
, whichfork
);
1141 struct xfs_btree_block
*block
= ifp
->if_broot
;
1142 struct xfs_iext_cursor icur
;
1143 struct xfs_bmbt_irec
new;
1151 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
1153 if (unlikely(XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)) {
1154 XFS_ERROR_REPORT(__func__
, XFS_ERRLEVEL_LOW
, mp
);
1155 return -EFSCORRUPTED
;
1159 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
1161 level
= be16_to_cpu(block
->bb_level
);
1163 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, 1, ifp
->if_broot_bytes
);
1164 bno
= be64_to_cpu(*pp
);
1167 * Go down the tree until leaf level is reached, following the first
1168 * pointer (leftmost) at each level.
1170 while (level
-- > 0) {
1171 error
= xfs_btree_read_bufl(mp
, tp
, bno
, 0, &bp
,
1172 XFS_BMAP_BTREE_REF
, &xfs_bmbt_buf_ops
);
1175 block
= XFS_BUF_TO_BLOCK(bp
);
1178 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, mp
->m_bmap_dmxr
[1]);
1179 bno
= be64_to_cpu(*pp
);
1180 XFS_WANT_CORRUPTED_GOTO(mp
,
1181 xfs_verify_fsbno(mp
, bno
), out_brelse
);
1182 xfs_trans_brelse(tp
, bp
);
1186 * Here with bp and block set to the leftmost leaf node in the tree.
1189 xfs_iext_first(ifp
, &icur
);
1192 * Loop over all leaf nodes. Copy information to the extent records.
1195 xfs_bmbt_rec_t
*frp
;
1196 xfs_fsblock_t nextbno
;
1197 xfs_extnum_t num_recs
;
1199 num_recs
= xfs_btree_get_numrecs(block
);
1200 if (unlikely(i
+ num_recs
> nextents
)) {
1201 xfs_warn(ip
->i_mount
,
1202 "corrupt dinode %Lu, (btree extents).",
1203 (unsigned long long) ip
->i_ino
);
1204 xfs_inode_verifier_error(ip
, -EFSCORRUPTED
,
1205 __func__
, block
, sizeof(*block
),
1207 error
= -EFSCORRUPTED
;
1211 * Read-ahead the next leaf block, if any.
1213 nextbno
= be64_to_cpu(block
->bb_u
.l
.bb_rightsib
);
1214 if (nextbno
!= NULLFSBLOCK
)
1215 xfs_btree_reada_bufl(mp
, nextbno
, 1,
1218 * Copy records into the extent records.
1220 frp
= XFS_BMBT_REC_ADDR(mp
, block
, 1);
1221 for (j
= 0; j
< num_recs
; j
++, frp
++, i
++) {
1224 xfs_bmbt_disk_get_all(frp
, &new);
1225 fa
= xfs_bmap_validate_extent(ip
, whichfork
, &new);
1227 error
= -EFSCORRUPTED
;
1228 xfs_inode_verifier_error(ip
, error
,
1229 "xfs_iread_extents(2)",
1230 frp
, sizeof(*frp
), fa
);
1233 xfs_iext_insert(ip
, &icur
, &new, state
);
1234 trace_xfs_read_extent(ip
, &icur
, state
, _THIS_IP_
);
1235 xfs_iext_next(ifp
, &icur
);
1237 xfs_trans_brelse(tp
, bp
);
1240 * If we've reached the end, stop.
1242 if (bno
== NULLFSBLOCK
)
1244 error
= xfs_btree_read_bufl(mp
, tp
, bno
, 0, &bp
,
1245 XFS_BMAP_BTREE_REF
, &xfs_bmbt_buf_ops
);
1248 block
= XFS_BUF_TO_BLOCK(bp
);
1251 if (i
!= XFS_IFORK_NEXTENTS(ip
, whichfork
)) {
1252 error
= -EFSCORRUPTED
;
1255 ASSERT(i
== xfs_iext_count(ifp
));
1257 ifp
->if_flags
|= XFS_IFEXTENTS
;
1261 xfs_trans_brelse(tp
, bp
);
1263 xfs_iext_destroy(ifp
);
1268 * Returns the relative block number of the first unused block(s) in the given
1269 * fork with at least "len" logically contiguous blocks free. This is the
1270 * lowest-address hole if the fork has holes, else the first block past the end
1271 * of fork. Return 0 if the fork is currently local (in-inode).
1274 xfs_bmap_first_unused(
1275 struct xfs_trans
*tp
, /* transaction pointer */
1276 struct xfs_inode
*ip
, /* incore inode */
1277 xfs_extlen_t len
, /* size of hole to find */
1278 xfs_fileoff_t
*first_unused
, /* unused block */
1279 int whichfork
) /* data or attr fork */
1281 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1282 struct xfs_bmbt_irec got
;
1283 struct xfs_iext_cursor icur
;
1284 xfs_fileoff_t lastaddr
= 0;
1285 xfs_fileoff_t lowest
, max
;
1288 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
||
1289 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
||
1290 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
1292 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
) {
1297 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
1298 error
= xfs_iread_extents(tp
, ip
, whichfork
);
1303 lowest
= max
= *first_unused
;
1304 for_each_xfs_iext(ifp
, &icur
, &got
) {
1306 * See if the hole before this extent will work.
1308 if (got
.br_startoff
>= lowest
+ len
&&
1309 got
.br_startoff
- max
>= len
)
1311 lastaddr
= got
.br_startoff
+ got
.br_blockcount
;
1312 max
= XFS_FILEOFF_MAX(lastaddr
, lowest
);
1315 *first_unused
= max
;
1320 * Returns the file-relative block number of the last block - 1 before
1321 * last_block (input value) in the file.
1322 * This is not based on i_size, it is based on the extent records.
1323 * Returns 0 for local files, as they do not have extent records.
1326 xfs_bmap_last_before(
1327 struct xfs_trans
*tp
, /* transaction pointer */
1328 struct xfs_inode
*ip
, /* incore inode */
1329 xfs_fileoff_t
*last_block
, /* last block */
1330 int whichfork
) /* data or attr fork */
1332 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1333 struct xfs_bmbt_irec got
;
1334 struct xfs_iext_cursor icur
;
1337 switch (XFS_IFORK_FORMAT(ip
, whichfork
)) {
1338 case XFS_DINODE_FMT_LOCAL
:
1341 case XFS_DINODE_FMT_BTREE
:
1342 case XFS_DINODE_FMT_EXTENTS
:
1348 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
1349 error
= xfs_iread_extents(tp
, ip
, whichfork
);
1354 if (!xfs_iext_lookup_extent_before(ip
, ifp
, last_block
, &icur
, &got
))
1360 xfs_bmap_last_extent(
1361 struct xfs_trans
*tp
,
1362 struct xfs_inode
*ip
,
1364 struct xfs_bmbt_irec
*rec
,
1367 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1368 struct xfs_iext_cursor icur
;
1371 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
1372 error
= xfs_iread_extents(tp
, ip
, whichfork
);
1377 xfs_iext_last(ifp
, &icur
);
1378 if (!xfs_iext_get_extent(ifp
, &icur
, rec
))
1386 * Check the last inode extent to determine whether this allocation will result
1387 * in blocks being allocated at the end of the file. When we allocate new data
1388 * blocks at the end of the file which do not start at the previous data block,
1389 * we will try to align the new blocks at stripe unit boundaries.
1391 * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be
1392 * at, or past the EOF.
1396 struct xfs_bmalloca
*bma
,
1399 struct xfs_bmbt_irec rec
;
1404 error
= xfs_bmap_last_extent(NULL
, bma
->ip
, whichfork
, &rec
,
1415 * Check if we are allocation or past the last extent, or at least into
1416 * the last delayed allocated extent.
1418 bma
->aeof
= bma
->offset
>= rec
.br_startoff
+ rec
.br_blockcount
||
1419 (bma
->offset
>= rec
.br_startoff
&&
1420 isnullstartblock(rec
.br_startblock
));
1425 * Returns the file-relative block number of the first block past eof in
1426 * the file. This is not based on i_size, it is based on the extent records.
1427 * Returns 0 for local files, as they do not have extent records.
1430 xfs_bmap_last_offset(
1431 struct xfs_inode
*ip
,
1432 xfs_fileoff_t
*last_block
,
1435 struct xfs_bmbt_irec rec
;
1441 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
)
1444 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
&&
1445 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
1448 error
= xfs_bmap_last_extent(NULL
, ip
, whichfork
, &rec
, &is_empty
);
1449 if (error
|| is_empty
)
1452 *last_block
= rec
.br_startoff
+ rec
.br_blockcount
;
1457 * Returns whether the selected fork of the inode has exactly one
1458 * block or not. For the data fork we check this matches di_size,
1459 * implying the file's range is 0..bsize-1.
1461 int /* 1=>1 block, 0=>otherwise */
1463 xfs_inode_t
*ip
, /* incore inode */
1464 int whichfork
) /* data or attr fork */
1466 struct xfs_ifork
*ifp
; /* inode fork pointer */
1467 int rval
; /* return value */
1468 xfs_bmbt_irec_t s
; /* internal version of extent */
1469 struct xfs_iext_cursor icur
;
1472 if (whichfork
== XFS_DATA_FORK
)
1473 return XFS_ISIZE(ip
) == ip
->i_mount
->m_sb
.sb_blocksize
;
1475 if (XFS_IFORK_NEXTENTS(ip
, whichfork
) != 1)
1477 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
1479 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1480 ASSERT(ifp
->if_flags
& XFS_IFEXTENTS
);
1481 xfs_iext_first(ifp
, &icur
);
1482 xfs_iext_get_extent(ifp
, &icur
, &s
);
1483 rval
= s
.br_startoff
== 0 && s
.br_blockcount
== 1;
1484 if (rval
&& whichfork
== XFS_DATA_FORK
)
1485 ASSERT(XFS_ISIZE(ip
) == ip
->i_mount
->m_sb
.sb_blocksize
);
1490 * Extent tree manipulation functions used during allocation.
1494 * Convert a delayed allocation to a real allocation.
1496 STATIC
int /* error */
1497 xfs_bmap_add_extent_delay_real(
1498 struct xfs_bmalloca
*bma
,
1501 struct xfs_bmbt_irec
*new = &bma
->got
;
1502 int error
; /* error return value */
1503 int i
; /* temp state */
1504 struct xfs_ifork
*ifp
; /* inode fork pointer */
1505 xfs_fileoff_t new_endoff
; /* end offset of new entry */
1506 xfs_bmbt_irec_t r
[3]; /* neighbor extent entries */
1507 /* left is 0, right is 1, prev is 2 */
1508 int rval
=0; /* return value (logging flags) */
1509 int state
= xfs_bmap_fork_to_state(whichfork
);
1510 xfs_filblks_t da_new
; /* new count del alloc blocks used */
1511 xfs_filblks_t da_old
; /* old count del alloc blocks used */
1512 xfs_filblks_t temp
=0; /* value for da_new calculations */
1513 int tmp_rval
; /* partial logging flags */
1514 struct xfs_mount
*mp
;
1515 xfs_extnum_t
*nextents
;
1516 struct xfs_bmbt_irec old
;
1518 mp
= bma
->ip
->i_mount
;
1519 ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
1520 ASSERT(whichfork
!= XFS_ATTR_FORK
);
1521 nextents
= (whichfork
== XFS_COW_FORK
? &bma
->ip
->i_cnextents
:
1522 &bma
->ip
->i_d
.di_nextents
);
1524 ASSERT(!isnullstartblock(new->br_startblock
));
1526 (bma
->cur
->bc_private
.b
.flags
& XFS_BTCUR_BPRV_WASDEL
));
1528 XFS_STATS_INC(mp
, xs_add_exlist
);
1535 * Set up a bunch of variables to make the tests simpler.
1537 xfs_iext_get_extent(ifp
, &bma
->icur
, &PREV
);
1538 new_endoff
= new->br_startoff
+ new->br_blockcount
;
1539 ASSERT(isnullstartblock(PREV
.br_startblock
));
1540 ASSERT(PREV
.br_startoff
<= new->br_startoff
);
1541 ASSERT(PREV
.br_startoff
+ PREV
.br_blockcount
>= new_endoff
);
1543 da_old
= startblockval(PREV
.br_startblock
);
1547 * Set flags determining what part of the previous delayed allocation
1548 * extent is being replaced by a real allocation.
1550 if (PREV
.br_startoff
== new->br_startoff
)
1551 state
|= BMAP_LEFT_FILLING
;
1552 if (PREV
.br_startoff
+ PREV
.br_blockcount
== new_endoff
)
1553 state
|= BMAP_RIGHT_FILLING
;
1556 * Check and set flags if this segment has a left neighbor.
1557 * Don't set contiguous if the combined extent would be too large.
1559 if (xfs_iext_peek_prev_extent(ifp
, &bma
->icur
, &LEFT
)) {
1560 state
|= BMAP_LEFT_VALID
;
1561 if (isnullstartblock(LEFT
.br_startblock
))
1562 state
|= BMAP_LEFT_DELAY
;
1565 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
1566 LEFT
.br_startoff
+ LEFT
.br_blockcount
== new->br_startoff
&&
1567 LEFT
.br_startblock
+ LEFT
.br_blockcount
== new->br_startblock
&&
1568 LEFT
.br_state
== new->br_state
&&
1569 LEFT
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
1570 state
|= BMAP_LEFT_CONTIG
;
1573 * Check and set flags if this segment has a right neighbor.
1574 * Don't set contiguous if the combined extent would be too large.
1575 * Also check for all-three-contiguous being too large.
1577 if (xfs_iext_peek_next_extent(ifp
, &bma
->icur
, &RIGHT
)) {
1578 state
|= BMAP_RIGHT_VALID
;
1579 if (isnullstartblock(RIGHT
.br_startblock
))
1580 state
|= BMAP_RIGHT_DELAY
;
1583 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
1584 new_endoff
== RIGHT
.br_startoff
&&
1585 new->br_startblock
+ new->br_blockcount
== RIGHT
.br_startblock
&&
1586 new->br_state
== RIGHT
.br_state
&&
1587 new->br_blockcount
+ RIGHT
.br_blockcount
<= MAXEXTLEN
&&
1588 ((state
& (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
1589 BMAP_RIGHT_FILLING
)) !=
1590 (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
1591 BMAP_RIGHT_FILLING
) ||
1592 LEFT
.br_blockcount
+ new->br_blockcount
+ RIGHT
.br_blockcount
1594 state
|= BMAP_RIGHT_CONTIG
;
1598 * Switch out based on the FILLING and CONTIG state bits.
1600 switch (state
& (BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
1601 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
)) {
1602 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
1603 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
1605 * Filling in all of a previously delayed allocation extent.
1606 * The left and right neighbors are both contiguous with new.
1608 LEFT
.br_blockcount
+= PREV
.br_blockcount
+ RIGHT
.br_blockcount
;
1610 xfs_iext_remove(bma
->ip
, &bma
->icur
, state
);
1611 xfs_iext_remove(bma
->ip
, &bma
->icur
, state
);
1612 xfs_iext_prev(ifp
, &bma
->icur
);
1613 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &LEFT
);
1616 if (bma
->cur
== NULL
)
1617 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1619 rval
= XFS_ILOG_CORE
;
1620 error
= xfs_bmbt_lookup_eq(bma
->cur
, &RIGHT
, &i
);
1623 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1624 error
= xfs_btree_delete(bma
->cur
, &i
);
1627 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1628 error
= xfs_btree_decrement(bma
->cur
, 0, &i
);
1631 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1632 error
= xfs_bmbt_update(bma
->cur
, &LEFT
);
1638 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
1640 * Filling in all of a previously delayed allocation extent.
1641 * The left neighbor is contiguous, the right is not.
1644 LEFT
.br_blockcount
+= PREV
.br_blockcount
;
1646 xfs_iext_remove(bma
->ip
, &bma
->icur
, state
);
1647 xfs_iext_prev(ifp
, &bma
->icur
);
1648 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &LEFT
);
1650 if (bma
->cur
== NULL
)
1651 rval
= XFS_ILOG_DEXT
;
1654 error
= xfs_bmbt_lookup_eq(bma
->cur
, &old
, &i
);
1657 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1658 error
= xfs_bmbt_update(bma
->cur
, &LEFT
);
1664 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
1666 * Filling in all of a previously delayed allocation extent.
1667 * The right neighbor is contiguous, the left is not.
1669 PREV
.br_startblock
= new->br_startblock
;
1670 PREV
.br_blockcount
+= RIGHT
.br_blockcount
;
1672 xfs_iext_next(ifp
, &bma
->icur
);
1673 xfs_iext_remove(bma
->ip
, &bma
->icur
, state
);
1674 xfs_iext_prev(ifp
, &bma
->icur
);
1675 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &PREV
);
1677 if (bma
->cur
== NULL
)
1678 rval
= XFS_ILOG_DEXT
;
1681 error
= xfs_bmbt_lookup_eq(bma
->cur
, &RIGHT
, &i
);
1684 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1685 error
= xfs_bmbt_update(bma
->cur
, &PREV
);
1691 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
1693 * Filling in all of a previously delayed allocation extent.
1694 * Neither the left nor right neighbors are contiguous with
1697 PREV
.br_startblock
= new->br_startblock
;
1698 PREV
.br_state
= new->br_state
;
1699 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &PREV
);
1702 if (bma
->cur
== NULL
)
1703 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1705 rval
= XFS_ILOG_CORE
;
1706 error
= xfs_bmbt_lookup_eq(bma
->cur
, new, &i
);
1709 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
1710 error
= xfs_btree_insert(bma
->cur
, &i
);
1713 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1717 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
:
1719 * Filling in the first part of a previous delayed allocation.
1720 * The left neighbor is contiguous.
1723 temp
= PREV
.br_blockcount
- new->br_blockcount
;
1724 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
1725 startblockval(PREV
.br_startblock
));
1727 LEFT
.br_blockcount
+= new->br_blockcount
;
1729 PREV
.br_blockcount
= temp
;
1730 PREV
.br_startoff
+= new->br_blockcount
;
1731 PREV
.br_startblock
= nullstartblock(da_new
);
1733 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &PREV
);
1734 xfs_iext_prev(ifp
, &bma
->icur
);
1735 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &LEFT
);
1737 if (bma
->cur
== NULL
)
1738 rval
= XFS_ILOG_DEXT
;
1741 error
= xfs_bmbt_lookup_eq(bma
->cur
, &old
, &i
);
1744 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1745 error
= xfs_bmbt_update(bma
->cur
, &LEFT
);
1751 case BMAP_LEFT_FILLING
:
1753 * Filling in the first part of a previous delayed allocation.
1754 * The left neighbor is not contiguous.
1756 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, new);
1758 if (bma
->cur
== NULL
)
1759 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1761 rval
= XFS_ILOG_CORE
;
1762 error
= xfs_bmbt_lookup_eq(bma
->cur
, new, &i
);
1765 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
1766 error
= xfs_btree_insert(bma
->cur
, &i
);
1769 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1772 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
1773 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
1774 &bma
->cur
, 1, &tmp_rval
, whichfork
);
1780 temp
= PREV
.br_blockcount
- new->br_blockcount
;
1781 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
1782 startblockval(PREV
.br_startblock
) -
1783 (bma
->cur
? bma
->cur
->bc_private
.b
.allocated
: 0));
1785 PREV
.br_startoff
= new_endoff
;
1786 PREV
.br_blockcount
= temp
;
1787 PREV
.br_startblock
= nullstartblock(da_new
);
1788 xfs_iext_next(ifp
, &bma
->icur
);
1789 xfs_iext_insert(bma
->ip
, &bma
->icur
, &PREV
, state
);
1790 xfs_iext_prev(ifp
, &bma
->icur
);
1793 case BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
1795 * Filling in the last part of a previous delayed allocation.
1796 * The right neighbor is contiguous with the new allocation.
1799 RIGHT
.br_startoff
= new->br_startoff
;
1800 RIGHT
.br_startblock
= new->br_startblock
;
1801 RIGHT
.br_blockcount
+= new->br_blockcount
;
1803 if (bma
->cur
== NULL
)
1804 rval
= XFS_ILOG_DEXT
;
1807 error
= xfs_bmbt_lookup_eq(bma
->cur
, &old
, &i
);
1810 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1811 error
= xfs_bmbt_update(bma
->cur
, &RIGHT
);
1816 temp
= PREV
.br_blockcount
- new->br_blockcount
;
1817 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
1818 startblockval(PREV
.br_startblock
));
1820 PREV
.br_blockcount
= temp
;
1821 PREV
.br_startblock
= nullstartblock(da_new
);
1823 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &PREV
);
1824 xfs_iext_next(ifp
, &bma
->icur
);
1825 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &RIGHT
);
1828 case BMAP_RIGHT_FILLING
:
1830 * Filling in the last part of a previous delayed allocation.
1831 * The right neighbor is not contiguous.
1833 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, new);
1835 if (bma
->cur
== NULL
)
1836 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1838 rval
= XFS_ILOG_CORE
;
1839 error
= xfs_bmbt_lookup_eq(bma
->cur
, new, &i
);
1842 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
1843 error
= xfs_btree_insert(bma
->cur
, &i
);
1846 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1849 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
1850 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
1851 &bma
->cur
, 1, &tmp_rval
, whichfork
);
1857 temp
= PREV
.br_blockcount
- new->br_blockcount
;
1858 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
1859 startblockval(PREV
.br_startblock
) -
1860 (bma
->cur
? bma
->cur
->bc_private
.b
.allocated
: 0));
1862 PREV
.br_startblock
= nullstartblock(da_new
);
1863 PREV
.br_blockcount
= temp
;
1864 xfs_iext_insert(bma
->ip
, &bma
->icur
, &PREV
, state
);
1865 xfs_iext_next(ifp
, &bma
->icur
);
1870 * Filling in the middle part of a previous delayed allocation.
1871 * Contiguity is impossible here.
1872 * This case is avoided almost all the time.
1874 * We start with a delayed allocation:
1876 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
1879 * and we are allocating:
1880 * +rrrrrrrrrrrrrrrrr+
1883 * and we set it up for insertion as:
1884 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
1886 * PREV @ idx LEFT RIGHT
1887 * inserted at idx + 1
1891 /* LEFT is the new middle */
1894 /* RIGHT is the new right */
1895 RIGHT
.br_state
= PREV
.br_state
;
1896 RIGHT
.br_startoff
= new_endoff
;
1897 RIGHT
.br_blockcount
=
1898 PREV
.br_startoff
+ PREV
.br_blockcount
- new_endoff
;
1899 RIGHT
.br_startblock
=
1900 nullstartblock(xfs_bmap_worst_indlen(bma
->ip
,
1901 RIGHT
.br_blockcount
));
1904 PREV
.br_blockcount
= new->br_startoff
- PREV
.br_startoff
;
1905 PREV
.br_startblock
=
1906 nullstartblock(xfs_bmap_worst_indlen(bma
->ip
,
1907 PREV
.br_blockcount
));
1908 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &PREV
);
1910 xfs_iext_next(ifp
, &bma
->icur
);
1911 xfs_iext_insert(bma
->ip
, &bma
->icur
, &RIGHT
, state
);
1912 xfs_iext_insert(bma
->ip
, &bma
->icur
, &LEFT
, state
);
1915 if (bma
->cur
== NULL
)
1916 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1918 rval
= XFS_ILOG_CORE
;
1919 error
= xfs_bmbt_lookup_eq(bma
->cur
, new, &i
);
1922 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
1923 error
= xfs_btree_insert(bma
->cur
, &i
);
1926 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1929 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
1930 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
1931 &bma
->cur
, 1, &tmp_rval
, whichfork
);
1937 da_new
= startblockval(PREV
.br_startblock
) +
1938 startblockval(RIGHT
.br_startblock
);
1941 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
1942 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
1943 case BMAP_LEFT_FILLING
| BMAP_RIGHT_CONTIG
:
1944 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
1945 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
1946 case BMAP_LEFT_CONTIG
:
1947 case BMAP_RIGHT_CONTIG
:
1949 * These cases are all impossible.
1954 /* add reverse mapping unless caller opted out */
1955 if (!(bma
->flags
& XFS_BMAPI_NORMAP
)) {
1956 error
= xfs_rmap_map_extent(mp
, bma
->tp
->t_dfops
, bma
->ip
,
1962 /* convert to a btree if necessary */
1963 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
1964 int tmp_logflags
; /* partial log flag return val */
1966 ASSERT(bma
->cur
== NULL
);
1967 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
1968 &bma
->cur
, da_old
> 0, &tmp_logflags
,
1970 bma
->logflags
|= tmp_logflags
;
1976 da_new
+= bma
->cur
->bc_private
.b
.allocated
;
1977 bma
->cur
->bc_private
.b
.allocated
= 0;
1980 /* adjust for changes in reserved delayed indirect blocks */
1981 if (da_new
!= da_old
) {
1982 ASSERT(state
== 0 || da_new
< da_old
);
1983 error
= xfs_mod_fdblocks(mp
, (int64_t)(da_old
- da_new
),
1987 xfs_bmap_check_leaf_extents(bma
->cur
, bma
->ip
, whichfork
);
1989 if (whichfork
!= XFS_COW_FORK
)
1990 bma
->logflags
|= rval
;
1998 * Convert an unwritten allocation to a real allocation or vice versa.
2000 STATIC
int /* error */
2001 xfs_bmap_add_extent_unwritten_real(
2002 struct xfs_trans
*tp
,
2003 xfs_inode_t
*ip
, /* incore inode pointer */
2005 struct xfs_iext_cursor
*icur
,
2006 xfs_btree_cur_t
**curp
, /* if *curp is null, not a btree */
2007 xfs_bmbt_irec_t
*new, /* new data to add to file extents */
2008 int *logflagsp
) /* inode logging flags */
2010 xfs_btree_cur_t
*cur
; /* btree cursor */
2011 int error
; /* error return value */
2012 int i
; /* temp state */
2013 struct xfs_ifork
*ifp
; /* inode fork pointer */
2014 xfs_fileoff_t new_endoff
; /* end offset of new entry */
2015 xfs_bmbt_irec_t r
[3]; /* neighbor extent entries */
2016 /* left is 0, right is 1, prev is 2 */
2017 int rval
=0; /* return value (logging flags) */
2018 int state
= xfs_bmap_fork_to_state(whichfork
);
2019 struct xfs_mount
*mp
= ip
->i_mount
;
2020 struct xfs_bmbt_irec old
;
2021 struct xfs_defer_ops
*dfops
= tp
? tp
->t_dfops
: NULL
;
2026 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
2028 ASSERT(!isnullstartblock(new->br_startblock
));
2030 XFS_STATS_INC(mp
, xs_add_exlist
);
2037 * Set up a bunch of variables to make the tests simpler.
2040 xfs_iext_get_extent(ifp
, icur
, &PREV
);
2041 ASSERT(new->br_state
!= PREV
.br_state
);
2042 new_endoff
= new->br_startoff
+ new->br_blockcount
;
2043 ASSERT(PREV
.br_startoff
<= new->br_startoff
);
2044 ASSERT(PREV
.br_startoff
+ PREV
.br_blockcount
>= new_endoff
);
2047 * Set flags determining what part of the previous oldext allocation
2048 * extent is being replaced by a newext allocation.
2050 if (PREV
.br_startoff
== new->br_startoff
)
2051 state
|= BMAP_LEFT_FILLING
;
2052 if (PREV
.br_startoff
+ PREV
.br_blockcount
== new_endoff
)
2053 state
|= BMAP_RIGHT_FILLING
;
2056 * Check and set flags if this segment has a left neighbor.
2057 * Don't set contiguous if the combined extent would be too large.
2059 if (xfs_iext_peek_prev_extent(ifp
, icur
, &LEFT
)) {
2060 state
|= BMAP_LEFT_VALID
;
2061 if (isnullstartblock(LEFT
.br_startblock
))
2062 state
|= BMAP_LEFT_DELAY
;
2065 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
2066 LEFT
.br_startoff
+ LEFT
.br_blockcount
== new->br_startoff
&&
2067 LEFT
.br_startblock
+ LEFT
.br_blockcount
== new->br_startblock
&&
2068 LEFT
.br_state
== new->br_state
&&
2069 LEFT
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
2070 state
|= BMAP_LEFT_CONTIG
;
2073 * Check and set flags if this segment has a right neighbor.
2074 * Don't set contiguous if the combined extent would be too large.
2075 * Also check for all-three-contiguous being too large.
2077 if (xfs_iext_peek_next_extent(ifp
, icur
, &RIGHT
)) {
2078 state
|= BMAP_RIGHT_VALID
;
2079 if (isnullstartblock(RIGHT
.br_startblock
))
2080 state
|= BMAP_RIGHT_DELAY
;
2083 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
2084 new_endoff
== RIGHT
.br_startoff
&&
2085 new->br_startblock
+ new->br_blockcount
== RIGHT
.br_startblock
&&
2086 new->br_state
== RIGHT
.br_state
&&
2087 new->br_blockcount
+ RIGHT
.br_blockcount
<= MAXEXTLEN
&&
2088 ((state
& (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
2089 BMAP_RIGHT_FILLING
)) !=
2090 (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
2091 BMAP_RIGHT_FILLING
) ||
2092 LEFT
.br_blockcount
+ new->br_blockcount
+ RIGHT
.br_blockcount
2094 state
|= BMAP_RIGHT_CONTIG
;
2097 * Switch out based on the FILLING and CONTIG state bits.
2099 switch (state
& (BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
2100 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
)) {
2101 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
2102 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2104 * Setting all of a previous oldext extent to newext.
2105 * The left and right neighbors are both contiguous with new.
2107 LEFT
.br_blockcount
+= PREV
.br_blockcount
+ RIGHT
.br_blockcount
;
2109 xfs_iext_remove(ip
, icur
, state
);
2110 xfs_iext_remove(ip
, icur
, state
);
2111 xfs_iext_prev(ifp
, icur
);
2112 xfs_iext_update_extent(ip
, state
, icur
, &LEFT
);
2113 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2114 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 2);
2116 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2118 rval
= XFS_ILOG_CORE
;
2119 error
= xfs_bmbt_lookup_eq(cur
, &RIGHT
, &i
);
2122 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2123 if ((error
= xfs_btree_delete(cur
, &i
)))
2125 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2126 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2128 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2129 if ((error
= xfs_btree_delete(cur
, &i
)))
2131 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2132 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2134 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2135 error
= xfs_bmbt_update(cur
, &LEFT
);
2141 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
2143 * Setting all of a previous oldext extent to newext.
2144 * The left neighbor is contiguous, the right is not.
2146 LEFT
.br_blockcount
+= PREV
.br_blockcount
;
2148 xfs_iext_remove(ip
, icur
, state
);
2149 xfs_iext_prev(ifp
, icur
);
2150 xfs_iext_update_extent(ip
, state
, icur
, &LEFT
);
2151 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2152 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
2154 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2156 rval
= XFS_ILOG_CORE
;
2157 error
= xfs_bmbt_lookup_eq(cur
, &PREV
, &i
);
2160 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2161 if ((error
= xfs_btree_delete(cur
, &i
)))
2163 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2164 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2166 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2167 error
= xfs_bmbt_update(cur
, &LEFT
);
2173 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2175 * Setting all of a previous oldext extent to newext.
2176 * The right neighbor is contiguous, the left is not.
2178 PREV
.br_blockcount
+= RIGHT
.br_blockcount
;
2179 PREV
.br_state
= new->br_state
;
2181 xfs_iext_next(ifp
, icur
);
2182 xfs_iext_remove(ip
, icur
, state
);
2183 xfs_iext_prev(ifp
, icur
);
2184 xfs_iext_update_extent(ip
, state
, icur
, &PREV
);
2186 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2187 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
2189 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2191 rval
= XFS_ILOG_CORE
;
2192 error
= xfs_bmbt_lookup_eq(cur
, &RIGHT
, &i
);
2195 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2196 if ((error
= xfs_btree_delete(cur
, &i
)))
2198 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2199 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2201 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2202 error
= xfs_bmbt_update(cur
, &PREV
);
2208 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
2210 * Setting all of a previous oldext extent to newext.
2211 * Neither the left nor right neighbors are contiguous with
2214 PREV
.br_state
= new->br_state
;
2215 xfs_iext_update_extent(ip
, state
, icur
, &PREV
);
2218 rval
= XFS_ILOG_DEXT
;
2221 error
= xfs_bmbt_lookup_eq(cur
, new, &i
);
2224 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2225 error
= xfs_bmbt_update(cur
, &PREV
);
2231 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
:
2233 * Setting the first part of a previous oldext extent to newext.
2234 * The left neighbor is contiguous.
2236 LEFT
.br_blockcount
+= new->br_blockcount
;
2239 PREV
.br_startoff
+= new->br_blockcount
;
2240 PREV
.br_startblock
+= new->br_blockcount
;
2241 PREV
.br_blockcount
-= new->br_blockcount
;
2243 xfs_iext_update_extent(ip
, state
, icur
, &PREV
);
2244 xfs_iext_prev(ifp
, icur
);
2245 xfs_iext_update_extent(ip
, state
, icur
, &LEFT
);
2248 rval
= XFS_ILOG_DEXT
;
2251 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2254 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2255 error
= xfs_bmbt_update(cur
, &PREV
);
2258 error
= xfs_btree_decrement(cur
, 0, &i
);
2261 error
= xfs_bmbt_update(cur
, &LEFT
);
2267 case BMAP_LEFT_FILLING
:
2269 * Setting the first part of a previous oldext extent to newext.
2270 * The left neighbor is not contiguous.
2273 PREV
.br_startoff
+= new->br_blockcount
;
2274 PREV
.br_startblock
+= new->br_blockcount
;
2275 PREV
.br_blockcount
-= new->br_blockcount
;
2277 xfs_iext_update_extent(ip
, state
, icur
, &PREV
);
2278 xfs_iext_insert(ip
, icur
, new, state
);
2279 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2280 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
2282 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2284 rval
= XFS_ILOG_CORE
;
2285 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2288 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2289 error
= xfs_bmbt_update(cur
, &PREV
);
2292 cur
->bc_rec
.b
= *new;
2293 if ((error
= xfs_btree_insert(cur
, &i
)))
2295 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2299 case BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2301 * Setting the last part of a previous oldext extent to newext.
2302 * The right neighbor is contiguous with the new allocation.
2305 PREV
.br_blockcount
-= new->br_blockcount
;
2307 RIGHT
.br_startoff
= new->br_startoff
;
2308 RIGHT
.br_startblock
= new->br_startblock
;
2309 RIGHT
.br_blockcount
+= new->br_blockcount
;
2311 xfs_iext_update_extent(ip
, state
, icur
, &PREV
);
2312 xfs_iext_next(ifp
, icur
);
2313 xfs_iext_update_extent(ip
, state
, icur
, &RIGHT
);
2316 rval
= XFS_ILOG_DEXT
;
2319 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2322 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2323 error
= xfs_bmbt_update(cur
, &PREV
);
2326 error
= xfs_btree_increment(cur
, 0, &i
);
2329 error
= xfs_bmbt_update(cur
, &RIGHT
);
2335 case BMAP_RIGHT_FILLING
:
2337 * Setting the last part of a previous oldext extent to newext.
2338 * The right neighbor is not contiguous.
2341 PREV
.br_blockcount
-= new->br_blockcount
;
2343 xfs_iext_update_extent(ip
, state
, icur
, &PREV
);
2344 xfs_iext_next(ifp
, icur
);
2345 xfs_iext_insert(ip
, icur
, new, state
);
2347 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2348 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
2350 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2352 rval
= XFS_ILOG_CORE
;
2353 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2356 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2357 error
= xfs_bmbt_update(cur
, &PREV
);
2360 error
= xfs_bmbt_lookup_eq(cur
, new, &i
);
2363 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
2364 if ((error
= xfs_btree_insert(cur
, &i
)))
2366 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2372 * Setting the middle part of a previous oldext extent to
2373 * newext. Contiguity is impossible here.
2374 * One extent becomes three extents.
2377 PREV
.br_blockcount
= new->br_startoff
- PREV
.br_startoff
;
2380 r
[1].br_startoff
= new_endoff
;
2381 r
[1].br_blockcount
=
2382 old
.br_startoff
+ old
.br_blockcount
- new_endoff
;
2383 r
[1].br_startblock
= new->br_startblock
+ new->br_blockcount
;
2384 r
[1].br_state
= PREV
.br_state
;
2386 xfs_iext_update_extent(ip
, state
, icur
, &PREV
);
2387 xfs_iext_next(ifp
, icur
);
2388 xfs_iext_insert(ip
, icur
, &r
[1], state
);
2389 xfs_iext_insert(ip
, icur
, &r
[0], state
);
2391 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2392 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 2);
2394 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2396 rval
= XFS_ILOG_CORE
;
2397 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2400 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2401 /* new right extent - oldext */
2402 error
= xfs_bmbt_update(cur
, &r
[1]);
2405 /* new left extent - oldext */
2406 cur
->bc_rec
.b
= PREV
;
2407 if ((error
= xfs_btree_insert(cur
, &i
)))
2409 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2411 * Reset the cursor to the position of the new extent
2412 * we are about to insert as we can't trust it after
2413 * the previous insert.
2415 error
= xfs_bmbt_lookup_eq(cur
, new, &i
);
2418 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
2419 /* new middle extent - newext */
2420 if ((error
= xfs_btree_insert(cur
, &i
)))
2422 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2426 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2427 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2428 case BMAP_LEFT_FILLING
| BMAP_RIGHT_CONTIG
:
2429 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
2430 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2431 case BMAP_LEFT_CONTIG
:
2432 case BMAP_RIGHT_CONTIG
:
2434 * These cases are all impossible.
2439 /* update reverse mappings */
2440 error
= xfs_rmap_convert_extent(mp
, dfops
, ip
, whichfork
, new);
2444 /* convert to a btree if necessary */
2445 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
2446 int tmp_logflags
; /* partial log flag return val */
2448 ASSERT(cur
== NULL
);
2449 error
= xfs_bmap_extents_to_btree(tp
, ip
, &cur
, 0,
2450 &tmp_logflags
, whichfork
);
2451 *logflagsp
|= tmp_logflags
;
2456 /* clear out the allocated field, done with it now in any case. */
2458 cur
->bc_private
.b
.allocated
= 0;
2462 xfs_bmap_check_leaf_extents(*curp
, ip
, whichfork
);
2472 * Convert a hole to a delayed allocation.
2475 xfs_bmap_add_extent_hole_delay(
2476 xfs_inode_t
*ip
, /* incore inode pointer */
2478 struct xfs_iext_cursor
*icur
,
2479 xfs_bmbt_irec_t
*new) /* new data to add to file extents */
2481 struct xfs_ifork
*ifp
; /* inode fork pointer */
2482 xfs_bmbt_irec_t left
; /* left neighbor extent entry */
2483 xfs_filblks_t newlen
=0; /* new indirect size */
2484 xfs_filblks_t oldlen
=0; /* old indirect size */
2485 xfs_bmbt_irec_t right
; /* right neighbor extent entry */
2486 int state
= xfs_bmap_fork_to_state(whichfork
);
2487 xfs_filblks_t temp
; /* temp for indirect calculations */
2489 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
2490 ASSERT(isnullstartblock(new->br_startblock
));
2493 * Check and set flags if this segment has a left neighbor
2495 if (xfs_iext_peek_prev_extent(ifp
, icur
, &left
)) {
2496 state
|= BMAP_LEFT_VALID
;
2497 if (isnullstartblock(left
.br_startblock
))
2498 state
|= BMAP_LEFT_DELAY
;
2502 * Check and set flags if the current (right) segment exists.
2503 * If it doesn't exist, we're converting the hole at end-of-file.
2505 if (xfs_iext_get_extent(ifp
, icur
, &right
)) {
2506 state
|= BMAP_RIGHT_VALID
;
2507 if (isnullstartblock(right
.br_startblock
))
2508 state
|= BMAP_RIGHT_DELAY
;
2512 * Set contiguity flags on the left and right neighbors.
2513 * Don't let extents get too large, even if the pieces are contiguous.
2515 if ((state
& BMAP_LEFT_VALID
) && (state
& BMAP_LEFT_DELAY
) &&
2516 left
.br_startoff
+ left
.br_blockcount
== new->br_startoff
&&
2517 left
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
2518 state
|= BMAP_LEFT_CONTIG
;
2520 if ((state
& BMAP_RIGHT_VALID
) && (state
& BMAP_RIGHT_DELAY
) &&
2521 new->br_startoff
+ new->br_blockcount
== right
.br_startoff
&&
2522 new->br_blockcount
+ right
.br_blockcount
<= MAXEXTLEN
&&
2523 (!(state
& BMAP_LEFT_CONTIG
) ||
2524 (left
.br_blockcount
+ new->br_blockcount
+
2525 right
.br_blockcount
<= MAXEXTLEN
)))
2526 state
|= BMAP_RIGHT_CONTIG
;
2529 * Switch out based on the contiguity flags.
2531 switch (state
& (BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
)) {
2532 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2534 * New allocation is contiguous with delayed allocations
2535 * on the left and on the right.
2536 * Merge all three into a single extent record.
2538 temp
= left
.br_blockcount
+ new->br_blockcount
+
2539 right
.br_blockcount
;
2541 oldlen
= startblockval(left
.br_startblock
) +
2542 startblockval(new->br_startblock
) +
2543 startblockval(right
.br_startblock
);
2544 newlen
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
2546 left
.br_startblock
= nullstartblock(newlen
);
2547 left
.br_blockcount
= temp
;
2549 xfs_iext_remove(ip
, icur
, state
);
2550 xfs_iext_prev(ifp
, icur
);
2551 xfs_iext_update_extent(ip
, state
, icur
, &left
);
2554 case BMAP_LEFT_CONTIG
:
2556 * New allocation is contiguous with a delayed allocation
2558 * Merge the new allocation with the left neighbor.
2560 temp
= left
.br_blockcount
+ new->br_blockcount
;
2562 oldlen
= startblockval(left
.br_startblock
) +
2563 startblockval(new->br_startblock
);
2564 newlen
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
2566 left
.br_blockcount
= temp
;
2567 left
.br_startblock
= nullstartblock(newlen
);
2569 xfs_iext_prev(ifp
, icur
);
2570 xfs_iext_update_extent(ip
, state
, icur
, &left
);
2573 case BMAP_RIGHT_CONTIG
:
2575 * New allocation is contiguous with a delayed allocation
2577 * Merge the new allocation with the right neighbor.
2579 temp
= new->br_blockcount
+ right
.br_blockcount
;
2580 oldlen
= startblockval(new->br_startblock
) +
2581 startblockval(right
.br_startblock
);
2582 newlen
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
2584 right
.br_startoff
= new->br_startoff
;
2585 right
.br_startblock
= nullstartblock(newlen
);
2586 right
.br_blockcount
= temp
;
2587 xfs_iext_update_extent(ip
, state
, icur
, &right
);
2592 * New allocation is not contiguous with another
2593 * delayed allocation.
2594 * Insert a new entry.
2596 oldlen
= newlen
= 0;
2597 xfs_iext_insert(ip
, icur
, new, state
);
2600 if (oldlen
!= newlen
) {
2601 ASSERT(oldlen
> newlen
);
2602 xfs_mod_fdblocks(ip
->i_mount
, (int64_t)(oldlen
- newlen
),
2605 * Nothing to do for disk quota accounting here.
2611 * Convert a hole to a real allocation.
2613 STATIC
int /* error */
2614 xfs_bmap_add_extent_hole_real(
2615 struct xfs_trans
*tp
,
2616 struct xfs_inode
*ip
,
2618 struct xfs_iext_cursor
*icur
,
2619 struct xfs_btree_cur
**curp
,
2620 struct xfs_bmbt_irec
*new,
2624 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
2625 struct xfs_mount
*mp
= ip
->i_mount
;
2626 struct xfs_btree_cur
*cur
= *curp
;
2627 int error
; /* error return value */
2628 int i
; /* temp state */
2629 xfs_bmbt_irec_t left
; /* left neighbor extent entry */
2630 xfs_bmbt_irec_t right
; /* right neighbor extent entry */
2631 int rval
=0; /* return value (logging flags) */
2632 int state
= xfs_bmap_fork_to_state(whichfork
);
2633 struct xfs_bmbt_irec old
;
2635 ASSERT(!isnullstartblock(new->br_startblock
));
2636 ASSERT(!cur
|| !(cur
->bc_private
.b
.flags
& XFS_BTCUR_BPRV_WASDEL
));
2638 XFS_STATS_INC(mp
, xs_add_exlist
);
2641 * Check and set flags if this segment has a left neighbor.
2643 if (xfs_iext_peek_prev_extent(ifp
, icur
, &left
)) {
2644 state
|= BMAP_LEFT_VALID
;
2645 if (isnullstartblock(left
.br_startblock
))
2646 state
|= BMAP_LEFT_DELAY
;
2650 * Check and set flags if this segment has a current value.
2651 * Not true if we're inserting into the "hole" at eof.
2653 if (xfs_iext_get_extent(ifp
, icur
, &right
)) {
2654 state
|= BMAP_RIGHT_VALID
;
2655 if (isnullstartblock(right
.br_startblock
))
2656 state
|= BMAP_RIGHT_DELAY
;
2660 * We're inserting a real allocation between "left" and "right".
2661 * Set the contiguity flags. Don't let extents get too large.
2663 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
2664 left
.br_startoff
+ left
.br_blockcount
== new->br_startoff
&&
2665 left
.br_startblock
+ left
.br_blockcount
== new->br_startblock
&&
2666 left
.br_state
== new->br_state
&&
2667 left
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
2668 state
|= BMAP_LEFT_CONTIG
;
2670 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
2671 new->br_startoff
+ new->br_blockcount
== right
.br_startoff
&&
2672 new->br_startblock
+ new->br_blockcount
== right
.br_startblock
&&
2673 new->br_state
== right
.br_state
&&
2674 new->br_blockcount
+ right
.br_blockcount
<= MAXEXTLEN
&&
2675 (!(state
& BMAP_LEFT_CONTIG
) ||
2676 left
.br_blockcount
+ new->br_blockcount
+
2677 right
.br_blockcount
<= MAXEXTLEN
))
2678 state
|= BMAP_RIGHT_CONTIG
;
2682 * Select which case we're in here, and implement it.
2684 switch (state
& (BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
)) {
2685 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2687 * New allocation is contiguous with real allocations on the
2688 * left and on the right.
2689 * Merge all three into a single extent record.
2691 left
.br_blockcount
+= new->br_blockcount
+ right
.br_blockcount
;
2693 xfs_iext_remove(ip
, icur
, state
);
2694 xfs_iext_prev(ifp
, icur
);
2695 xfs_iext_update_extent(ip
, state
, icur
, &left
);
2697 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2698 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
2700 rval
= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
2702 rval
= XFS_ILOG_CORE
;
2703 error
= xfs_bmbt_lookup_eq(cur
, &right
, &i
);
2706 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2707 error
= xfs_btree_delete(cur
, &i
);
2710 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2711 error
= xfs_btree_decrement(cur
, 0, &i
);
2714 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2715 error
= xfs_bmbt_update(cur
, &left
);
2721 case BMAP_LEFT_CONTIG
:
2723 * New allocation is contiguous with a real allocation
2725 * Merge the new allocation with the left neighbor.
2728 left
.br_blockcount
+= new->br_blockcount
;
2730 xfs_iext_prev(ifp
, icur
);
2731 xfs_iext_update_extent(ip
, state
, icur
, &left
);
2734 rval
= xfs_ilog_fext(whichfork
);
2737 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2740 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2741 error
= xfs_bmbt_update(cur
, &left
);
2747 case BMAP_RIGHT_CONTIG
:
2749 * New allocation is contiguous with a real allocation
2751 * Merge the new allocation with the right neighbor.
2755 right
.br_startoff
= new->br_startoff
;
2756 right
.br_startblock
= new->br_startblock
;
2757 right
.br_blockcount
+= new->br_blockcount
;
2758 xfs_iext_update_extent(ip
, state
, icur
, &right
);
2761 rval
= xfs_ilog_fext(whichfork
);
2764 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2767 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2768 error
= xfs_bmbt_update(cur
, &right
);
2776 * New allocation is not contiguous with another
2778 * Insert a new entry.
2780 xfs_iext_insert(ip
, icur
, new, state
);
2781 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2782 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
2784 rval
= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
2786 rval
= XFS_ILOG_CORE
;
2787 error
= xfs_bmbt_lookup_eq(cur
, new, &i
);
2790 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
2791 error
= xfs_btree_insert(cur
, &i
);
2794 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2799 /* add reverse mapping unless caller opted out */
2800 if (!(flags
& XFS_BMAPI_NORMAP
)) {
2801 error
= xfs_rmap_map_extent(mp
, tp
->t_dfops
, ip
, whichfork
,
2807 /* convert to a btree if necessary */
2808 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
2809 int tmp_logflags
; /* partial log flag return val */
2811 ASSERT(cur
== NULL
);
2812 error
= xfs_bmap_extents_to_btree(tp
, ip
, curp
, 0,
2813 &tmp_logflags
, whichfork
);
2814 *logflagsp
|= tmp_logflags
;
2820 /* clear out the allocated field, done with it now in any case. */
2822 cur
->bc_private
.b
.allocated
= 0;
2824 xfs_bmap_check_leaf_extents(cur
, ip
, whichfork
);
2831 * Functions used in the extent read, allocate and remove paths
2835 * Adjust the size of the new extent based on di_extsize and rt extsize.
2838 xfs_bmap_extsize_align(
2840 xfs_bmbt_irec_t
*gotp
, /* next extent pointer */
2841 xfs_bmbt_irec_t
*prevp
, /* previous extent pointer */
2842 xfs_extlen_t extsz
, /* align to this extent size */
2843 int rt
, /* is this a realtime inode? */
2844 int eof
, /* is extent at end-of-file? */
2845 int delay
, /* creating delalloc extent? */
2846 int convert
, /* overwriting unwritten extent? */
2847 xfs_fileoff_t
*offp
, /* in/out: aligned offset */
2848 xfs_extlen_t
*lenp
) /* in/out: aligned length */
2850 xfs_fileoff_t orig_off
; /* original offset */
2851 xfs_extlen_t orig_alen
; /* original length */
2852 xfs_fileoff_t orig_end
; /* original off+len */
2853 xfs_fileoff_t nexto
; /* next file offset */
2854 xfs_fileoff_t prevo
; /* previous file offset */
2855 xfs_fileoff_t align_off
; /* temp for offset */
2856 xfs_extlen_t align_alen
; /* temp for length */
2857 xfs_extlen_t temp
; /* temp for calculations */
2862 orig_off
= align_off
= *offp
;
2863 orig_alen
= align_alen
= *lenp
;
2864 orig_end
= orig_off
+ orig_alen
;
2867 * If this request overlaps an existing extent, then don't
2868 * attempt to perform any additional alignment.
2870 if (!delay
&& !eof
&&
2871 (orig_off
>= gotp
->br_startoff
) &&
2872 (orig_end
<= gotp
->br_startoff
+ gotp
->br_blockcount
)) {
2877 * If the file offset is unaligned vs. the extent size
2878 * we need to align it. This will be possible unless
2879 * the file was previously written with a kernel that didn't
2880 * perform this alignment, or if a truncate shot us in the
2883 div_u64_rem(orig_off
, extsz
, &temp
);
2889 /* Same adjustment for the end of the requested area. */
2890 temp
= (align_alen
% extsz
);
2892 align_alen
+= extsz
- temp
;
2895 * For large extent hint sizes, the aligned extent might be larger than
2896 * MAXEXTLEN. In that case, reduce the size by an extsz so that it pulls
2897 * the length back under MAXEXTLEN. The outer allocation loops handle
2898 * short allocation just fine, so it is safe to do this. We only want to
2899 * do it when we are forced to, though, because it means more allocation
2900 * operations are required.
2902 while (align_alen
> MAXEXTLEN
)
2903 align_alen
-= extsz
;
2904 ASSERT(align_alen
<= MAXEXTLEN
);
2907 * If the previous block overlaps with this proposed allocation
2908 * then move the start forward without adjusting the length.
2910 if (prevp
->br_startoff
!= NULLFILEOFF
) {
2911 if (prevp
->br_startblock
== HOLESTARTBLOCK
)
2912 prevo
= prevp
->br_startoff
;
2914 prevo
= prevp
->br_startoff
+ prevp
->br_blockcount
;
2917 if (align_off
!= orig_off
&& align_off
< prevo
)
2920 * If the next block overlaps with this proposed allocation
2921 * then move the start back without adjusting the length,
2922 * but not before offset 0.
2923 * This may of course make the start overlap previous block,
2924 * and if we hit the offset 0 limit then the next block
2925 * can still overlap too.
2927 if (!eof
&& gotp
->br_startoff
!= NULLFILEOFF
) {
2928 if ((delay
&& gotp
->br_startblock
== HOLESTARTBLOCK
) ||
2929 (!delay
&& gotp
->br_startblock
== DELAYSTARTBLOCK
))
2930 nexto
= gotp
->br_startoff
+ gotp
->br_blockcount
;
2932 nexto
= gotp
->br_startoff
;
2934 nexto
= NULLFILEOFF
;
2936 align_off
+ align_alen
!= orig_end
&&
2937 align_off
+ align_alen
> nexto
)
2938 align_off
= nexto
> align_alen
? nexto
- align_alen
: 0;
2940 * If we're now overlapping the next or previous extent that
2941 * means we can't fit an extsz piece in this hole. Just move
2942 * the start forward to the first valid spot and set
2943 * the length so we hit the end.
2945 if (align_off
!= orig_off
&& align_off
< prevo
)
2947 if (align_off
+ align_alen
!= orig_end
&&
2948 align_off
+ align_alen
> nexto
&&
2949 nexto
!= NULLFILEOFF
) {
2950 ASSERT(nexto
> prevo
);
2951 align_alen
= nexto
- align_off
;
2955 * If realtime, and the result isn't a multiple of the realtime
2956 * extent size we need to remove blocks until it is.
2958 if (rt
&& (temp
= (align_alen
% mp
->m_sb
.sb_rextsize
))) {
2960 * We're not covering the original request, or
2961 * we won't be able to once we fix the length.
2963 if (orig_off
< align_off
||
2964 orig_end
> align_off
+ align_alen
||
2965 align_alen
- temp
< orig_alen
)
2968 * Try to fix it by moving the start up.
2970 if (align_off
+ temp
<= orig_off
) {
2975 * Try to fix it by moving the end in.
2977 else if (align_off
+ align_alen
- temp
>= orig_end
)
2980 * Set the start to the minimum then trim the length.
2983 align_alen
-= orig_off
- align_off
;
2984 align_off
= orig_off
;
2985 align_alen
-= align_alen
% mp
->m_sb
.sb_rextsize
;
2988 * Result doesn't cover the request, fail it.
2990 if (orig_off
< align_off
|| orig_end
> align_off
+ align_alen
)
2993 ASSERT(orig_off
>= align_off
);
2994 /* see MAXEXTLEN handling above */
2995 ASSERT(orig_end
<= align_off
+ align_alen
||
2996 align_alen
+ extsz
> MAXEXTLEN
);
3000 if (!eof
&& gotp
->br_startoff
!= NULLFILEOFF
)
3001 ASSERT(align_off
+ align_alen
<= gotp
->br_startoff
);
3002 if (prevp
->br_startoff
!= NULLFILEOFF
)
3003 ASSERT(align_off
>= prevp
->br_startoff
+ prevp
->br_blockcount
);
3011 #define XFS_ALLOC_GAP_UNITS 4
3015 struct xfs_bmalloca
*ap
) /* bmap alloc argument struct */
3017 xfs_fsblock_t adjust
; /* adjustment to block numbers */
3018 xfs_agnumber_t fb_agno
; /* ag number of ap->firstblock */
3019 xfs_mount_t
*mp
; /* mount point structure */
3020 int nullfb
; /* true if ap->firstblock isn't set */
3021 int rt
; /* true if inode is realtime */
3023 #define ISVALID(x,y) \
3025 (x) < mp->m_sb.sb_rblocks : \
3026 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
3027 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
3028 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
3030 mp
= ap
->ip
->i_mount
;
3031 nullfb
= ap
->tp
->t_firstblock
== NULLFSBLOCK
;
3032 rt
= XFS_IS_REALTIME_INODE(ap
->ip
) &&
3033 xfs_alloc_is_userdata(ap
->datatype
);
3034 fb_agno
= nullfb
? NULLAGNUMBER
: XFS_FSB_TO_AGNO(mp
,
3035 ap
->tp
->t_firstblock
);
3037 * If allocating at eof, and there's a previous real block,
3038 * try to use its last block as our starting point.
3040 if (ap
->eof
&& ap
->prev
.br_startoff
!= NULLFILEOFF
&&
3041 !isnullstartblock(ap
->prev
.br_startblock
) &&
3042 ISVALID(ap
->prev
.br_startblock
+ ap
->prev
.br_blockcount
,
3043 ap
->prev
.br_startblock
)) {
3044 ap
->blkno
= ap
->prev
.br_startblock
+ ap
->prev
.br_blockcount
;
3046 * Adjust for the gap between prevp and us.
3048 adjust
= ap
->offset
-
3049 (ap
->prev
.br_startoff
+ ap
->prev
.br_blockcount
);
3051 ISVALID(ap
->blkno
+ adjust
, ap
->prev
.br_startblock
))
3052 ap
->blkno
+= adjust
;
3055 * If not at eof, then compare the two neighbor blocks.
3056 * Figure out whether either one gives us a good starting point,
3057 * and pick the better one.
3059 else if (!ap
->eof
) {
3060 xfs_fsblock_t gotbno
; /* right side block number */
3061 xfs_fsblock_t gotdiff
=0; /* right side difference */
3062 xfs_fsblock_t prevbno
; /* left side block number */
3063 xfs_fsblock_t prevdiff
=0; /* left side difference */
3066 * If there's a previous (left) block, select a requested
3067 * start block based on it.
3069 if (ap
->prev
.br_startoff
!= NULLFILEOFF
&&
3070 !isnullstartblock(ap
->prev
.br_startblock
) &&
3071 (prevbno
= ap
->prev
.br_startblock
+
3072 ap
->prev
.br_blockcount
) &&
3073 ISVALID(prevbno
, ap
->prev
.br_startblock
)) {
3075 * Calculate gap to end of previous block.
3077 adjust
= prevdiff
= ap
->offset
-
3078 (ap
->prev
.br_startoff
+
3079 ap
->prev
.br_blockcount
);
3081 * Figure the startblock based on the previous block's
3082 * end and the gap size.
3084 * If the gap is large relative to the piece we're
3085 * allocating, or using it gives us an invalid block
3086 * number, then just use the end of the previous block.
3088 if (prevdiff
<= XFS_ALLOC_GAP_UNITS
* ap
->length
&&
3089 ISVALID(prevbno
+ prevdiff
,
3090 ap
->prev
.br_startblock
))
3095 * If the firstblock forbids it, can't use it,
3098 if (!rt
&& !nullfb
&&
3099 XFS_FSB_TO_AGNO(mp
, prevbno
) != fb_agno
)
3100 prevbno
= NULLFSBLOCK
;
3103 * No previous block or can't follow it, just default.
3106 prevbno
= NULLFSBLOCK
;
3108 * If there's a following (right) block, select a requested
3109 * start block based on it.
3111 if (!isnullstartblock(ap
->got
.br_startblock
)) {
3113 * Calculate gap to start of next block.
3115 adjust
= gotdiff
= ap
->got
.br_startoff
- ap
->offset
;
3117 * Figure the startblock based on the next block's
3118 * start and the gap size.
3120 gotbno
= ap
->got
.br_startblock
;
3123 * If the gap is large relative to the piece we're
3124 * allocating, or using it gives us an invalid block
3125 * number, then just use the start of the next block
3126 * offset by our length.
3128 if (gotdiff
<= XFS_ALLOC_GAP_UNITS
* ap
->length
&&
3129 ISVALID(gotbno
- gotdiff
, gotbno
))
3131 else if (ISVALID(gotbno
- ap
->length
, gotbno
)) {
3132 gotbno
-= ap
->length
;
3133 gotdiff
+= adjust
- ap
->length
;
3137 * If the firstblock forbids it, can't use it,
3140 if (!rt
&& !nullfb
&&
3141 XFS_FSB_TO_AGNO(mp
, gotbno
) != fb_agno
)
3142 gotbno
= NULLFSBLOCK
;
3145 * No next block, just default.
3148 gotbno
= NULLFSBLOCK
;
3150 * If both valid, pick the better one, else the only good
3151 * one, else ap->blkno is already set (to 0 or the inode block).
3153 if (prevbno
!= NULLFSBLOCK
&& gotbno
!= NULLFSBLOCK
)
3154 ap
->blkno
= prevdiff
<= gotdiff
? prevbno
: gotbno
;
3155 else if (prevbno
!= NULLFSBLOCK
)
3156 ap
->blkno
= prevbno
;
3157 else if (gotbno
!= NULLFSBLOCK
)
3164 xfs_bmap_longest_free_extent(
3165 struct xfs_trans
*tp
,
3170 struct xfs_mount
*mp
= tp
->t_mountp
;
3171 struct xfs_perag
*pag
;
3172 xfs_extlen_t longest
;
3175 pag
= xfs_perag_get(mp
, ag
);
3176 if (!pag
->pagf_init
) {
3177 error
= xfs_alloc_pagf_init(mp
, tp
, ag
, XFS_ALLOC_FLAG_TRYLOCK
);
3181 if (!pag
->pagf_init
) {
3187 longest
= xfs_alloc_longest_free_extent(pag
,
3188 xfs_alloc_min_freelist(mp
, pag
),
3189 xfs_ag_resv_needed(pag
, XFS_AG_RESV_NONE
));
3190 if (*blen
< longest
)
3199 xfs_bmap_select_minlen(
3200 struct xfs_bmalloca
*ap
,
3201 struct xfs_alloc_arg
*args
,
3205 if (notinit
|| *blen
< ap
->minlen
) {
3207 * Since we did a BUF_TRYLOCK above, it is possible that
3208 * there is space for this request.
3210 args
->minlen
= ap
->minlen
;
3211 } else if (*blen
< args
->maxlen
) {
3213 * If the best seen length is less than the request length,
3214 * use the best as the minimum.
3216 args
->minlen
= *blen
;
3219 * Otherwise we've seen an extent as big as maxlen, use that
3222 args
->minlen
= args
->maxlen
;
3227 xfs_bmap_btalloc_nullfb(
3228 struct xfs_bmalloca
*ap
,
3229 struct xfs_alloc_arg
*args
,
3232 struct xfs_mount
*mp
= ap
->ip
->i_mount
;
3233 xfs_agnumber_t ag
, startag
;
3237 args
->type
= XFS_ALLOCTYPE_START_BNO
;
3238 args
->total
= ap
->total
;
3240 startag
= ag
= XFS_FSB_TO_AGNO(mp
, args
->fsbno
);
3241 if (startag
== NULLAGNUMBER
)
3244 while (*blen
< args
->maxlen
) {
3245 error
= xfs_bmap_longest_free_extent(args
->tp
, ag
, blen
,
3250 if (++ag
== mp
->m_sb
.sb_agcount
)
3256 xfs_bmap_select_minlen(ap
, args
, blen
, notinit
);
3261 xfs_bmap_btalloc_filestreams(
3262 struct xfs_bmalloca
*ap
,
3263 struct xfs_alloc_arg
*args
,
3266 struct xfs_mount
*mp
= ap
->ip
->i_mount
;
3271 args
->type
= XFS_ALLOCTYPE_NEAR_BNO
;
3272 args
->total
= ap
->total
;
3274 ag
= XFS_FSB_TO_AGNO(mp
, args
->fsbno
);
3275 if (ag
== NULLAGNUMBER
)
3278 error
= xfs_bmap_longest_free_extent(args
->tp
, ag
, blen
, ¬init
);
3282 if (*blen
< args
->maxlen
) {
3283 error
= xfs_filestream_new_ag(ap
, &ag
);
3287 error
= xfs_bmap_longest_free_extent(args
->tp
, ag
, blen
,
3294 xfs_bmap_select_minlen(ap
, args
, blen
, notinit
);
3297 * Set the failure fallback case to look in the selected AG as stream
3300 ap
->blkno
= args
->fsbno
= XFS_AGB_TO_FSB(mp
, ag
, 0);
3304 /* Update all inode and quota accounting for the allocation we just did. */
3306 xfs_bmap_btalloc_accounting(
3307 struct xfs_bmalloca
*ap
,
3308 struct xfs_alloc_arg
*args
)
3310 if (ap
->flags
& XFS_BMAPI_COWFORK
) {
3312 * COW fork blocks are in-core only and thus are treated as
3313 * in-core quota reservation (like delalloc blocks) even when
3314 * converted to real blocks. The quota reservation is not
3315 * accounted to disk until blocks are remapped to the data
3316 * fork. So if these blocks were previously delalloc, we
3317 * already have quota reservation and there's nothing to do
3324 * Otherwise, we've allocated blocks in a hole. The transaction
3325 * has acquired in-core quota reservation for this extent.
3326 * Rather than account these as real blocks, however, we reduce
3327 * the transaction quota reservation based on the allocation.
3328 * This essentially transfers the transaction quota reservation
3329 * to that of a delalloc extent.
3331 ap
->ip
->i_delayed_blks
+= args
->len
;
3332 xfs_trans_mod_dquot_byino(ap
->tp
, ap
->ip
, XFS_TRANS_DQ_RES_BLKS
,
3337 /* data/attr fork only */
3338 ap
->ip
->i_d
.di_nblocks
+= args
->len
;
3339 xfs_trans_log_inode(ap
->tp
, ap
->ip
, XFS_ILOG_CORE
);
3341 ap
->ip
->i_delayed_blks
-= args
->len
;
3342 xfs_trans_mod_dquot_byino(ap
->tp
, ap
->ip
,
3343 ap
->wasdel
? XFS_TRANS_DQ_DELBCOUNT
: XFS_TRANS_DQ_BCOUNT
,
3349 struct xfs_bmalloca
*ap
) /* bmap alloc argument struct */
3351 xfs_mount_t
*mp
; /* mount point structure */
3352 xfs_alloctype_t atype
= 0; /* type for allocation routines */
3353 xfs_extlen_t align
= 0; /* minimum allocation alignment */
3354 xfs_agnumber_t fb_agno
; /* ag number of ap->firstblock */
3356 xfs_alloc_arg_t args
;
3357 xfs_fileoff_t orig_offset
;
3358 xfs_extlen_t orig_length
;
3360 xfs_extlen_t nextminlen
= 0;
3361 int nullfb
; /* true if ap->firstblock isn't set */
3368 orig_offset
= ap
->offset
;
3369 orig_length
= ap
->length
;
3371 mp
= ap
->ip
->i_mount
;
3373 /* stripe alignment for allocation is determined by mount parameters */
3375 if (mp
->m_swidth
&& (mp
->m_flags
& XFS_MOUNT_SWALLOC
))
3376 stripe_align
= mp
->m_swidth
;
3377 else if (mp
->m_dalign
)
3378 stripe_align
= mp
->m_dalign
;
3380 if (ap
->flags
& XFS_BMAPI_COWFORK
)
3381 align
= xfs_get_cowextsz_hint(ap
->ip
);
3382 else if (xfs_alloc_is_userdata(ap
->datatype
))
3383 align
= xfs_get_extsz_hint(ap
->ip
);
3385 error
= xfs_bmap_extsize_align(mp
, &ap
->got
, &ap
->prev
,
3386 align
, 0, ap
->eof
, 0, ap
->conv
,
3387 &ap
->offset
, &ap
->length
);
3393 nullfb
= ap
->tp
->t_firstblock
== NULLFSBLOCK
;
3394 fb_agno
= nullfb
? NULLAGNUMBER
: XFS_FSB_TO_AGNO(mp
,
3395 ap
->tp
->t_firstblock
);
3397 if (xfs_alloc_is_userdata(ap
->datatype
) &&
3398 xfs_inode_is_filestream(ap
->ip
)) {
3399 ag
= xfs_filestream_lookup_ag(ap
->ip
);
3400 ag
= (ag
!= NULLAGNUMBER
) ? ag
: 0;
3401 ap
->blkno
= XFS_AGB_TO_FSB(mp
, ag
, 0);
3403 ap
->blkno
= XFS_INO_TO_FSB(mp
, ap
->ip
->i_ino
);
3406 ap
->blkno
= ap
->tp
->t_firstblock
;
3408 xfs_bmap_adjacent(ap
);
3411 * If allowed, use ap->blkno; otherwise must use firstblock since
3412 * it's in the right allocation group.
3414 if (nullfb
|| XFS_FSB_TO_AGNO(mp
, ap
->blkno
) == fb_agno
)
3417 ap
->blkno
= ap
->tp
->t_firstblock
;
3419 * Normal allocation, done through xfs_alloc_vextent.
3421 tryagain
= isaligned
= 0;
3422 memset(&args
, 0, sizeof(args
));
3425 args
.fsbno
= ap
->blkno
;
3426 xfs_rmap_skip_owner_update(&args
.oinfo
);
3428 /* Trim the allocation back to the maximum an AG can fit. */
3429 args
.maxlen
= min(ap
->length
, mp
->m_ag_max_usable
);
3433 * Search for an allocation group with a single extent large
3434 * enough for the request. If one isn't found, then adjust
3435 * the minimum allocation size to the largest space found.
3437 if (xfs_alloc_is_userdata(ap
->datatype
) &&
3438 xfs_inode_is_filestream(ap
->ip
))
3439 error
= xfs_bmap_btalloc_filestreams(ap
, &args
, &blen
);
3441 error
= xfs_bmap_btalloc_nullfb(ap
, &args
, &blen
);
3444 } else if (ap
->tp
->t_flags
& XFS_TRANS_LOWMODE
) {
3445 if (xfs_inode_is_filestream(ap
->ip
))
3446 args
.type
= XFS_ALLOCTYPE_FIRST_AG
;
3448 args
.type
= XFS_ALLOCTYPE_START_BNO
;
3449 args
.total
= args
.minlen
= ap
->minlen
;
3451 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
3452 args
.total
= ap
->total
;
3453 args
.minlen
= ap
->minlen
;
3455 /* apply extent size hints if obtained earlier */
3458 div_u64_rem(ap
->offset
, args
.prod
, &args
.mod
);
3460 args
.mod
= args
.prod
- args
.mod
;
3461 } else if (mp
->m_sb
.sb_blocksize
>= PAGE_SIZE
) {
3465 args
.prod
= PAGE_SIZE
>> mp
->m_sb
.sb_blocklog
;
3466 div_u64_rem(ap
->offset
, args
.prod
, &args
.mod
);
3468 args
.mod
= args
.prod
- args
.mod
;
3471 * If we are not low on available data blocks, and the
3472 * underlying logical volume manager is a stripe, and
3473 * the file offset is zero then try to allocate data
3474 * blocks on stripe unit boundary.
3475 * NOTE: ap->aeof is only set if the allocation length
3476 * is >= the stripe unit and the allocation offset is
3477 * at the end of file.
3479 if (!(ap
->tp
->t_flags
& XFS_TRANS_LOWMODE
) && ap
->aeof
) {
3481 args
.alignment
= stripe_align
;
3485 * Adjust for alignment
3487 if (blen
> args
.alignment
&& blen
<= args
.maxlen
)
3488 args
.minlen
= blen
- args
.alignment
;
3489 args
.minalignslop
= 0;
3492 * First try an exact bno allocation.
3493 * If it fails then do a near or start bno
3494 * allocation with alignment turned on.
3498 args
.type
= XFS_ALLOCTYPE_THIS_BNO
;
3501 * Compute the minlen+alignment for the
3502 * next case. Set slop so that the value
3503 * of minlen+alignment+slop doesn't go up
3504 * between the calls.
3506 if (blen
> stripe_align
&& blen
<= args
.maxlen
)
3507 nextminlen
= blen
- stripe_align
;
3509 nextminlen
= args
.minlen
;
3510 if (nextminlen
+ stripe_align
> args
.minlen
+ 1)
3512 nextminlen
+ stripe_align
-
3515 args
.minalignslop
= 0;
3519 args
.minalignslop
= 0;
3521 args
.minleft
= ap
->minleft
;
3522 args
.wasdel
= ap
->wasdel
;
3523 args
.resv
= XFS_AG_RESV_NONE
;
3524 args
.datatype
= ap
->datatype
;
3525 if (ap
->datatype
& XFS_ALLOC_USERDATA_ZERO
)
3528 error
= xfs_alloc_vextent(&args
);
3532 if (tryagain
&& args
.fsbno
== NULLFSBLOCK
) {
3534 * Exact allocation failed. Now try with alignment
3538 args
.fsbno
= ap
->blkno
;
3539 args
.alignment
= stripe_align
;
3540 args
.minlen
= nextminlen
;
3541 args
.minalignslop
= 0;
3543 if ((error
= xfs_alloc_vextent(&args
)))
3546 if (isaligned
&& args
.fsbno
== NULLFSBLOCK
) {
3548 * allocation failed, so turn off alignment and
3552 args
.fsbno
= ap
->blkno
;
3554 if ((error
= xfs_alloc_vextent(&args
)))
3557 if (args
.fsbno
== NULLFSBLOCK
&& nullfb
&&
3558 args
.minlen
> ap
->minlen
) {
3559 args
.minlen
= ap
->minlen
;
3560 args
.type
= XFS_ALLOCTYPE_START_BNO
;
3561 args
.fsbno
= ap
->blkno
;
3562 if ((error
= xfs_alloc_vextent(&args
)))
3565 if (args
.fsbno
== NULLFSBLOCK
&& nullfb
) {
3567 args
.type
= XFS_ALLOCTYPE_FIRST_AG
;
3568 args
.total
= ap
->minlen
;
3569 if ((error
= xfs_alloc_vextent(&args
)))
3571 ap
->tp
->t_flags
|= XFS_TRANS_LOWMODE
;
3573 if (args
.fsbno
!= NULLFSBLOCK
) {
3575 * check the allocation happened at the same or higher AG than
3576 * the first block that was allocated.
3578 ASSERT(ap
->tp
->t_firstblock
== NULLFSBLOCK
||
3579 XFS_FSB_TO_AGNO(mp
, ap
->tp
->t_firstblock
) <=
3580 XFS_FSB_TO_AGNO(mp
, args
.fsbno
));
3582 ap
->blkno
= args
.fsbno
;
3583 if (ap
->tp
->t_firstblock
== NULLFSBLOCK
)
3584 ap
->tp
->t_firstblock
= args
.fsbno
;
3585 ASSERT(nullfb
|| fb_agno
<= args
.agno
);
3586 ap
->length
= args
.len
;
3588 * If the extent size hint is active, we tried to round the
3589 * caller's allocation request offset down to extsz and the
3590 * length up to another extsz boundary. If we found a free
3591 * extent we mapped it in starting at this new offset. If the
3592 * newly mapped space isn't long enough to cover any of the
3593 * range of offsets that was originally requested, move the
3594 * mapping up so that we can fill as much of the caller's
3595 * original request as possible. Free space is apparently
3596 * very fragmented so we're unlikely to be able to satisfy the
3599 if (ap
->length
<= orig_length
)
3600 ap
->offset
= orig_offset
;
3601 else if (ap
->offset
+ ap
->length
< orig_offset
+ orig_length
)
3602 ap
->offset
= orig_offset
+ orig_length
- ap
->length
;
3603 xfs_bmap_btalloc_accounting(ap
, &args
);
3605 ap
->blkno
= NULLFSBLOCK
;
3612 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
3613 * It figures out where to ask the underlying allocator to put the new extent.
3617 struct xfs_bmalloca
*ap
) /* bmap alloc argument struct */
3619 if (XFS_IS_REALTIME_INODE(ap
->ip
) &&
3620 xfs_alloc_is_userdata(ap
->datatype
))
3621 return xfs_bmap_rtalloc(ap
);
3622 return xfs_bmap_btalloc(ap
);
3625 /* Trim extent to fit a logical block range. */
3628 struct xfs_bmbt_irec
*irec
,
3632 xfs_fileoff_t distance
;
3633 xfs_fileoff_t end
= bno
+ len
;
3635 if (irec
->br_startoff
+ irec
->br_blockcount
<= bno
||
3636 irec
->br_startoff
>= end
) {
3637 irec
->br_blockcount
= 0;
3641 if (irec
->br_startoff
< bno
) {
3642 distance
= bno
- irec
->br_startoff
;
3643 if (isnullstartblock(irec
->br_startblock
))
3644 irec
->br_startblock
= DELAYSTARTBLOCK
;
3645 if (irec
->br_startblock
!= DELAYSTARTBLOCK
&&
3646 irec
->br_startblock
!= HOLESTARTBLOCK
)
3647 irec
->br_startblock
+= distance
;
3648 irec
->br_startoff
+= distance
;
3649 irec
->br_blockcount
-= distance
;
3652 if (end
< irec
->br_startoff
+ irec
->br_blockcount
) {
3653 distance
= irec
->br_startoff
+ irec
->br_blockcount
- end
;
3654 irec
->br_blockcount
-= distance
;
3658 /* trim extent to within eof */
3660 xfs_trim_extent_eof(
3661 struct xfs_bmbt_irec
*irec
,
3662 struct xfs_inode
*ip
)
3665 xfs_trim_extent(irec
, 0, XFS_B_TO_FSB(ip
->i_mount
,
3666 i_size_read(VFS_I(ip
))));
3670 * Trim the returned map to the required bounds
3674 struct xfs_bmbt_irec
*mval
,
3675 struct xfs_bmbt_irec
*got
,
3683 if ((flags
& XFS_BMAPI_ENTIRE
) ||
3684 got
->br_startoff
+ got
->br_blockcount
<= obno
) {
3686 if (isnullstartblock(got
->br_startblock
))
3687 mval
->br_startblock
= DELAYSTARTBLOCK
;
3693 ASSERT((*bno
>= obno
) || (n
== 0));
3695 mval
->br_startoff
= *bno
;
3696 if (isnullstartblock(got
->br_startblock
))
3697 mval
->br_startblock
= DELAYSTARTBLOCK
;
3699 mval
->br_startblock
= got
->br_startblock
+
3700 (*bno
- got
->br_startoff
);
3702 * Return the minimum of what we got and what we asked for for
3703 * the length. We can use the len variable here because it is
3704 * modified below and we could have been there before coming
3705 * here if the first part of the allocation didn't overlap what
3708 mval
->br_blockcount
= XFS_FILBLKS_MIN(end
- *bno
,
3709 got
->br_blockcount
- (*bno
- got
->br_startoff
));
3710 mval
->br_state
= got
->br_state
;
3711 ASSERT(mval
->br_blockcount
<= len
);
3716 * Update and validate the extent map to return
3719 xfs_bmapi_update_map(
3720 struct xfs_bmbt_irec
**map
,
3728 xfs_bmbt_irec_t
*mval
= *map
;
3730 ASSERT((flags
& XFS_BMAPI_ENTIRE
) ||
3731 ((mval
->br_startoff
+ mval
->br_blockcount
) <= end
));
3732 ASSERT((flags
& XFS_BMAPI_ENTIRE
) || (mval
->br_blockcount
<= *len
) ||
3733 (mval
->br_startoff
< obno
));
3735 *bno
= mval
->br_startoff
+ mval
->br_blockcount
;
3737 if (*n
> 0 && mval
->br_startoff
== mval
[-1].br_startoff
) {
3738 /* update previous map with new information */
3739 ASSERT(mval
->br_startblock
== mval
[-1].br_startblock
);
3740 ASSERT(mval
->br_blockcount
> mval
[-1].br_blockcount
);
3741 ASSERT(mval
->br_state
== mval
[-1].br_state
);
3742 mval
[-1].br_blockcount
= mval
->br_blockcount
;
3743 mval
[-1].br_state
= mval
->br_state
;
3744 } else if (*n
> 0 && mval
->br_startblock
!= DELAYSTARTBLOCK
&&
3745 mval
[-1].br_startblock
!= DELAYSTARTBLOCK
&&
3746 mval
[-1].br_startblock
!= HOLESTARTBLOCK
&&
3747 mval
->br_startblock
== mval
[-1].br_startblock
+
3748 mval
[-1].br_blockcount
&&
3749 mval
[-1].br_state
== mval
->br_state
) {
3750 ASSERT(mval
->br_startoff
==
3751 mval
[-1].br_startoff
+ mval
[-1].br_blockcount
);
3752 mval
[-1].br_blockcount
+= mval
->br_blockcount
;
3753 } else if (*n
> 0 &&
3754 mval
->br_startblock
== DELAYSTARTBLOCK
&&
3755 mval
[-1].br_startblock
== DELAYSTARTBLOCK
&&
3756 mval
->br_startoff
==
3757 mval
[-1].br_startoff
+ mval
[-1].br_blockcount
) {
3758 mval
[-1].br_blockcount
+= mval
->br_blockcount
;
3759 mval
[-1].br_state
= mval
->br_state
;
3760 } else if (!((*n
== 0) &&
3761 ((mval
->br_startoff
+ mval
->br_blockcount
) <=
3770 * Map file blocks to filesystem blocks without allocation.
3774 struct xfs_inode
*ip
,
3777 struct xfs_bmbt_irec
*mval
,
3781 struct xfs_mount
*mp
= ip
->i_mount
;
3782 struct xfs_ifork
*ifp
;
3783 struct xfs_bmbt_irec got
;
3786 struct xfs_iext_cursor icur
;
3790 int whichfork
= xfs_bmapi_whichfork(flags
);
3793 ASSERT(!(flags
& ~(XFS_BMAPI_ATTRFORK
|XFS_BMAPI_ENTIRE
|
3794 XFS_BMAPI_COWFORK
)));
3795 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_SHARED
|XFS_ILOCK_EXCL
));
3797 if (unlikely(XFS_TEST_ERROR(
3798 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
3799 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
3800 mp
, XFS_ERRTAG_BMAPIFORMAT
))) {
3801 XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW
, mp
);
3802 return -EFSCORRUPTED
;
3805 if (XFS_FORCED_SHUTDOWN(mp
))
3808 XFS_STATS_INC(mp
, xs_blk_mapr
);
3810 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
3812 /* No CoW fork? Return a hole. */
3813 if (whichfork
== XFS_COW_FORK
&& !ifp
) {
3814 mval
->br_startoff
= bno
;
3815 mval
->br_startblock
= HOLESTARTBLOCK
;
3816 mval
->br_blockcount
= len
;
3817 mval
->br_state
= XFS_EXT_NORM
;
3822 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
3823 error
= xfs_iread_extents(NULL
, ip
, whichfork
);
3828 if (!xfs_iext_lookup_extent(ip
, ifp
, bno
, &icur
, &got
))
3833 while (bno
< end
&& n
< *nmap
) {
3834 /* Reading past eof, act as though there's a hole up to end. */
3836 got
.br_startoff
= end
;
3837 if (got
.br_startoff
> bno
) {
3838 /* Reading in a hole. */
3839 mval
->br_startoff
= bno
;
3840 mval
->br_startblock
= HOLESTARTBLOCK
;
3841 mval
->br_blockcount
=
3842 XFS_FILBLKS_MIN(len
, got
.br_startoff
- bno
);
3843 mval
->br_state
= XFS_EXT_NORM
;
3844 bno
+= mval
->br_blockcount
;
3845 len
-= mval
->br_blockcount
;
3851 /* set up the extent map to return. */
3852 xfs_bmapi_trim_map(mval
, &got
, &bno
, len
, obno
, end
, n
, flags
);
3853 xfs_bmapi_update_map(&mval
, &bno
, &len
, obno
, end
, &n
, flags
);
3855 /* If we're done, stop now. */
3856 if (bno
>= end
|| n
>= *nmap
)
3859 /* Else go on to the next record. */
3860 if (!xfs_iext_next_extent(ifp
, &icur
, &got
))
3868 * Add a delayed allocation extent to an inode. Blocks are reserved from the
3869 * global pool and the extent inserted into the inode in-core extent tree.
3871 * On entry, got refers to the first extent beyond the offset of the extent to
3872 * allocate or eof is specified if no such extent exists. On return, got refers
3873 * to the extent record that was inserted to the inode fork.
3875 * Note that the allocated extent may have been merged with contiguous extents
3876 * during insertion into the inode fork. Thus, got does not reflect the current
3877 * state of the inode fork on return. If necessary, the caller can use lastx to
3878 * look up the updated record in the inode fork.
3881 xfs_bmapi_reserve_delalloc(
3882 struct xfs_inode
*ip
,
3886 xfs_filblks_t prealloc
,
3887 struct xfs_bmbt_irec
*got
,
3888 struct xfs_iext_cursor
*icur
,
3891 struct xfs_mount
*mp
= ip
->i_mount
;
3892 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
3894 xfs_extlen_t indlen
;
3896 xfs_fileoff_t aoff
= off
;
3899 * Cap the alloc length. Keep track of prealloc so we know whether to
3900 * tag the inode before we return.
3902 alen
= XFS_FILBLKS_MIN(len
+ prealloc
, MAXEXTLEN
);
3904 alen
= XFS_FILBLKS_MIN(alen
, got
->br_startoff
- aoff
);
3905 if (prealloc
&& alen
>= len
)
3906 prealloc
= alen
- len
;
3908 /* Figure out the extent size, adjust alen */
3909 if (whichfork
== XFS_COW_FORK
) {
3910 struct xfs_bmbt_irec prev
;
3911 xfs_extlen_t extsz
= xfs_get_cowextsz_hint(ip
);
3913 if (!xfs_iext_peek_prev_extent(ifp
, icur
, &prev
))
3914 prev
.br_startoff
= NULLFILEOFF
;
3916 error
= xfs_bmap_extsize_align(mp
, got
, &prev
, extsz
, 0, eof
,
3917 1, 0, &aoff
, &alen
);
3922 * Make a transaction-less quota reservation for delayed allocation
3923 * blocks. This number gets adjusted later. We return if we haven't
3924 * allocated blocks already inside this loop.
3926 error
= xfs_trans_reserve_quota_nblks(NULL
, ip
, (long)alen
, 0,
3927 XFS_QMOPT_RES_REGBLKS
);
3932 * Split changing sb for alen and indlen since they could be coming
3933 * from different places.
3935 indlen
= (xfs_extlen_t
)xfs_bmap_worst_indlen(ip
, alen
);
3938 error
= xfs_mod_fdblocks(mp
, -((int64_t)alen
), false);
3940 goto out_unreserve_quota
;
3942 error
= xfs_mod_fdblocks(mp
, -((int64_t)indlen
), false);
3944 goto out_unreserve_blocks
;
3947 ip
->i_delayed_blks
+= alen
;
3949 got
->br_startoff
= aoff
;
3950 got
->br_startblock
= nullstartblock(indlen
);
3951 got
->br_blockcount
= alen
;
3952 got
->br_state
= XFS_EXT_NORM
;
3954 xfs_bmap_add_extent_hole_delay(ip
, whichfork
, icur
, got
);
3957 * Tag the inode if blocks were preallocated. Note that COW fork
3958 * preallocation can occur at the start or end of the extent, even when
3959 * prealloc == 0, so we must also check the aligned offset and length.
3961 if (whichfork
== XFS_DATA_FORK
&& prealloc
)
3962 xfs_inode_set_eofblocks_tag(ip
);
3963 if (whichfork
== XFS_COW_FORK
&& (prealloc
|| aoff
< off
|| alen
> len
))
3964 xfs_inode_set_cowblocks_tag(ip
);
3968 out_unreserve_blocks
:
3969 xfs_mod_fdblocks(mp
, alen
, false);
3970 out_unreserve_quota
:
3971 if (XFS_IS_QUOTA_ON(mp
))
3972 xfs_trans_unreserve_quota_nblks(NULL
, ip
, (long)alen
, 0,
3973 XFS_QMOPT_RES_REGBLKS
);
3979 struct xfs_bmalloca
*bma
)
3981 struct xfs_mount
*mp
= bma
->ip
->i_mount
;
3982 int whichfork
= xfs_bmapi_whichfork(bma
->flags
);
3983 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
3984 int tmp_logflags
= 0;
3987 ASSERT(bma
->length
> 0);
3990 * For the wasdelay case, we could also just allocate the stuff asked
3991 * for in this bmap call but that wouldn't be as good.
3994 bma
->length
= (xfs_extlen_t
)bma
->got
.br_blockcount
;
3995 bma
->offset
= bma
->got
.br_startoff
;
3996 xfs_iext_peek_prev_extent(ifp
, &bma
->icur
, &bma
->prev
);
3998 bma
->length
= XFS_FILBLKS_MIN(bma
->length
, MAXEXTLEN
);
4000 bma
->length
= XFS_FILBLKS_MIN(bma
->length
,
4001 bma
->got
.br_startoff
- bma
->offset
);
4005 * Set the data type being allocated. For the data fork, the first data
4006 * in the file is treated differently to all other allocations. For the
4007 * attribute fork, we only need to ensure the allocated range is not on
4010 if (!(bma
->flags
& XFS_BMAPI_METADATA
)) {
4011 bma
->datatype
= XFS_ALLOC_NOBUSY
;
4012 if (whichfork
== XFS_DATA_FORK
) {
4013 if (bma
->offset
== 0)
4014 bma
->datatype
|= XFS_ALLOC_INITIAL_USER_DATA
;
4016 bma
->datatype
|= XFS_ALLOC_USERDATA
;
4018 if (bma
->flags
& XFS_BMAPI_ZERO
)
4019 bma
->datatype
|= XFS_ALLOC_USERDATA_ZERO
;
4022 bma
->minlen
= (bma
->flags
& XFS_BMAPI_CONTIG
) ? bma
->length
: 1;
4025 * Only want to do the alignment at the eof if it is userdata and
4026 * allocation length is larger than a stripe unit.
4028 if (mp
->m_dalign
&& bma
->length
>= mp
->m_dalign
&&
4029 !(bma
->flags
& XFS_BMAPI_METADATA
) && whichfork
== XFS_DATA_FORK
) {
4030 error
= xfs_bmap_isaeof(bma
, whichfork
);
4035 error
= xfs_bmap_alloc(bma
);
4039 if (bma
->blkno
== NULLFSBLOCK
)
4041 if ((ifp
->if_flags
& XFS_IFBROOT
) && !bma
->cur
)
4042 bma
->cur
= xfs_bmbt_init_cursor(mp
, bma
->tp
, bma
->ip
, whichfork
);
4044 * Bump the number of extents we've allocated
4050 bma
->cur
->bc_private
.b
.flags
=
4051 bma
->wasdel
? XFS_BTCUR_BPRV_WASDEL
: 0;
4053 bma
->got
.br_startoff
= bma
->offset
;
4054 bma
->got
.br_startblock
= bma
->blkno
;
4055 bma
->got
.br_blockcount
= bma
->length
;
4056 bma
->got
.br_state
= XFS_EXT_NORM
;
4059 * In the data fork, a wasdelay extent has been initialized, so
4060 * shouldn't be flagged as unwritten.
4062 * For the cow fork, however, we convert delalloc reservations
4063 * (extents allocated for speculative preallocation) to
4064 * allocated unwritten extents, and only convert the unwritten
4065 * extents to real extents when we're about to write the data.
4067 if ((!bma
->wasdel
|| (bma
->flags
& XFS_BMAPI_COWFORK
)) &&
4068 (bma
->flags
& XFS_BMAPI_PREALLOC
) &&
4069 xfs_sb_version_hasextflgbit(&mp
->m_sb
))
4070 bma
->got
.br_state
= XFS_EXT_UNWRITTEN
;
4073 error
= xfs_bmap_add_extent_delay_real(bma
, whichfork
);
4075 error
= xfs_bmap_add_extent_hole_real(bma
->tp
, bma
->ip
,
4076 whichfork
, &bma
->icur
, &bma
->cur
, &bma
->got
,
4077 &bma
->logflags
, bma
->flags
);
4079 bma
->logflags
|= tmp_logflags
;
4084 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4085 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4086 * the neighbouring ones.
4088 xfs_iext_get_extent(ifp
, &bma
->icur
, &bma
->got
);
4090 ASSERT(bma
->got
.br_startoff
<= bma
->offset
);
4091 ASSERT(bma
->got
.br_startoff
+ bma
->got
.br_blockcount
>=
4092 bma
->offset
+ bma
->length
);
4093 ASSERT(bma
->got
.br_state
== XFS_EXT_NORM
||
4094 bma
->got
.br_state
== XFS_EXT_UNWRITTEN
);
4099 xfs_bmapi_convert_unwritten(
4100 struct xfs_bmalloca
*bma
,
4101 struct xfs_bmbt_irec
*mval
,
4105 int whichfork
= xfs_bmapi_whichfork(flags
);
4106 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
4107 int tmp_logflags
= 0;
4110 /* check if we need to do unwritten->real conversion */
4111 if (mval
->br_state
== XFS_EXT_UNWRITTEN
&&
4112 (flags
& XFS_BMAPI_PREALLOC
))
4115 /* check if we need to do real->unwritten conversion */
4116 if (mval
->br_state
== XFS_EXT_NORM
&&
4117 (flags
& (XFS_BMAPI_PREALLOC
| XFS_BMAPI_CONVERT
)) !=
4118 (XFS_BMAPI_PREALLOC
| XFS_BMAPI_CONVERT
))
4122 * Modify (by adding) the state flag, if writing.
4124 ASSERT(mval
->br_blockcount
<= len
);
4125 if ((ifp
->if_flags
& XFS_IFBROOT
) && !bma
->cur
) {
4126 bma
->cur
= xfs_bmbt_init_cursor(bma
->ip
->i_mount
, bma
->tp
,
4127 bma
->ip
, whichfork
);
4129 mval
->br_state
= (mval
->br_state
== XFS_EXT_UNWRITTEN
)
4130 ? XFS_EXT_NORM
: XFS_EXT_UNWRITTEN
;
4133 * Before insertion into the bmbt, zero the range being converted
4136 if (flags
& XFS_BMAPI_ZERO
) {
4137 error
= xfs_zero_extent(bma
->ip
, mval
->br_startblock
,
4138 mval
->br_blockcount
);
4143 error
= xfs_bmap_add_extent_unwritten_real(bma
->tp
, bma
->ip
, whichfork
,
4144 &bma
->icur
, &bma
->cur
, mval
, &tmp_logflags
);
4146 * Log the inode core unconditionally in the unwritten extent conversion
4147 * path because the conversion might not have done so (e.g., if the
4148 * extent count hasn't changed). We need to make sure the inode is dirty
4149 * in the transaction for the sake of fsync(), even if nothing has
4150 * changed, because fsync() will not force the log for this transaction
4151 * unless it sees the inode pinned.
4153 * Note: If we're only converting cow fork extents, there aren't
4154 * any on-disk updates to make, so we don't need to log anything.
4156 if (whichfork
!= XFS_COW_FORK
)
4157 bma
->logflags
|= tmp_logflags
| XFS_ILOG_CORE
;
4162 * Update our extent pointer, given that
4163 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4164 * of the neighbouring ones.
4166 xfs_iext_get_extent(ifp
, &bma
->icur
, &bma
->got
);
4169 * We may have combined previously unwritten space with written space,
4170 * so generate another request.
4172 if (mval
->br_blockcount
< len
)
4178 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4179 * extent state if necessary. Details behaviour is controlled by the flags
4180 * parameter. Only allocates blocks from a single allocation group, to avoid
4185 struct xfs_trans
*tp
, /* transaction pointer */
4186 struct xfs_inode
*ip
, /* incore inode */
4187 xfs_fileoff_t bno
, /* starting file offs. mapped */
4188 xfs_filblks_t len
, /* length to map in file */
4189 int flags
, /* XFS_BMAPI_... */
4190 xfs_extlen_t total
, /* total blocks needed */
4191 struct xfs_bmbt_irec
*mval
, /* output: map values */
4192 int *nmap
) /* i/o: mval size/count */
4194 struct xfs_mount
*mp
= ip
->i_mount
;
4195 struct xfs_ifork
*ifp
;
4196 struct xfs_bmalloca bma
= { NULL
}; /* args for xfs_bmap_alloc */
4197 xfs_fileoff_t end
; /* end of mapped file region */
4198 bool eof
= false; /* after the end of extents */
4199 int error
; /* error return */
4200 int n
; /* current extent index */
4201 xfs_fileoff_t obno
; /* old block number (offset) */
4202 int whichfork
; /* data or attr fork */
4205 xfs_fileoff_t orig_bno
; /* original block number value */
4206 int orig_flags
; /* original flags arg value */
4207 xfs_filblks_t orig_len
; /* original value of len arg */
4208 struct xfs_bmbt_irec
*orig_mval
; /* original value of mval */
4209 int orig_nmap
; /* original value of *nmap */
4217 whichfork
= xfs_bmapi_whichfork(flags
);
4220 ASSERT(*nmap
<= XFS_BMAP_MAX_NMAP
);
4221 ASSERT(tp
!= NULL
||
4222 (flags
& (XFS_BMAPI_CONVERT
| XFS_BMAPI_COWFORK
)) ==
4223 (XFS_BMAPI_CONVERT
| XFS_BMAPI_COWFORK
));
4225 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_LOCAL
);
4226 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
4227 ASSERT(!(flags
& XFS_BMAPI_REMAP
));
4229 /* zeroing is for currently only for data extents, not metadata */
4230 ASSERT((flags
& (XFS_BMAPI_METADATA
| XFS_BMAPI_ZERO
)) !=
4231 (XFS_BMAPI_METADATA
| XFS_BMAPI_ZERO
));
4233 * we can allocate unwritten extents or pre-zero allocated blocks,
4234 * but it makes no sense to do both at once. This would result in
4235 * zeroing the unwritten extent twice, but it still being an
4236 * unwritten extent....
4238 ASSERT((flags
& (XFS_BMAPI_PREALLOC
| XFS_BMAPI_ZERO
)) !=
4239 (XFS_BMAPI_PREALLOC
| XFS_BMAPI_ZERO
));
4241 if (unlikely(XFS_TEST_ERROR(
4242 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
4243 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
4244 mp
, XFS_ERRTAG_BMAPIFORMAT
))) {
4245 XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW
, mp
);
4246 return -EFSCORRUPTED
;
4249 if (XFS_FORCED_SHUTDOWN(mp
))
4252 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4254 XFS_STATS_INC(mp
, xs_blk_mapw
);
4256 if (!tp
|| tp
->t_firstblock
== NULLFSBLOCK
) {
4257 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
)
4258 bma
.minleft
= be16_to_cpu(ifp
->if_broot
->bb_level
) + 1;
4265 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
4266 error
= xfs_iread_extents(tp
, ip
, whichfork
);
4275 if (!xfs_iext_lookup_extent(ip
, ifp
, bno
, &bma
.icur
, &bma
.got
))
4277 if (!xfs_iext_peek_prev_extent(ifp
, &bma
.icur
, &bma
.prev
))
4278 bma
.prev
.br_startoff
= NULLFILEOFF
;
4283 ASSERT(!tp
|| tp
->t_dfops
);
4285 while (bno
< end
&& n
< *nmap
) {
4286 bool need_alloc
= false, wasdelay
= false;
4288 /* in hole or beyond EOF? */
4289 if (eof
|| bma
.got
.br_startoff
> bno
) {
4291 * CoW fork conversions should /never/ hit EOF or
4292 * holes. There should always be something for us
4295 ASSERT(!((flags
& XFS_BMAPI_CONVERT
) &&
4296 (flags
& XFS_BMAPI_COWFORK
)));
4298 if (flags
& XFS_BMAPI_DELALLOC
) {
4300 * For the COW fork we can reasonably get a
4301 * request for converting an extent that races
4302 * with other threads already having converted
4303 * part of it, as there converting COW to
4304 * regular blocks is not protected using the
4307 ASSERT(flags
& XFS_BMAPI_COWFORK
);
4308 if (!(flags
& XFS_BMAPI_COWFORK
)) {
4313 if (eof
|| bno
>= end
)
4318 } else if (isnullstartblock(bma
.got
.br_startblock
)) {
4323 * First, deal with the hole before the allocated space
4324 * that we found, if any.
4326 if ((need_alloc
|| wasdelay
) &&
4327 !(flags
& XFS_BMAPI_CONVERT_ONLY
)) {
4329 bma
.conv
= !!(flags
& XFS_BMAPI_CONVERT
);
4330 bma
.wasdel
= wasdelay
;
4335 * There's a 32/64 bit type mismatch between the
4336 * allocation length request (which can be 64 bits in
4337 * length) and the bma length request, which is
4338 * xfs_extlen_t and therefore 32 bits. Hence we have to
4339 * check for 32-bit overflows and handle them here.
4341 if (len
> (xfs_filblks_t
)MAXEXTLEN
)
4342 bma
.length
= MAXEXTLEN
;
4347 ASSERT(bma
.length
> 0);
4348 error
= xfs_bmapi_allocate(&bma
);
4351 if (bma
.blkno
== NULLFSBLOCK
)
4355 * If this is a CoW allocation, record the data in
4356 * the refcount btree for orphan recovery.
4358 if (whichfork
== XFS_COW_FORK
) {
4359 error
= xfs_refcount_alloc_cow_extent(mp
,
4360 tp
->t_dfops
, bma
.blkno
,
4367 /* Deal with the allocated space we found. */
4368 xfs_bmapi_trim_map(mval
, &bma
.got
, &bno
, len
, obno
,
4371 /* Execute unwritten extent conversion if necessary */
4372 error
= xfs_bmapi_convert_unwritten(&bma
, mval
, len
, flags
);
4373 if (error
== -EAGAIN
)
4378 /* update the extent map to return */
4379 xfs_bmapi_update_map(&mval
, &bno
, &len
, obno
, end
, &n
, flags
);
4382 * If we're done, stop now. Stop when we've allocated
4383 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4384 * the transaction may get too big.
4386 if (bno
>= end
|| n
>= *nmap
|| bma
.nallocs
>= *nmap
)
4389 /* Else go on to the next record. */
4391 if (!xfs_iext_next_extent(ifp
, &bma
.icur
, &bma
.got
))
4397 * Transform from btree to extents, give it cur.
4399 if (xfs_bmap_wants_extents(ip
, whichfork
)) {
4400 int tmp_logflags
= 0;
4403 error
= xfs_bmap_btree_to_extents(tp
, ip
, bma
.cur
,
4404 &tmp_logflags
, whichfork
);
4405 bma
.logflags
|= tmp_logflags
;
4410 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
||
4411 XFS_IFORK_NEXTENTS(ip
, whichfork
) >
4412 XFS_IFORK_MAXEXT(ip
, whichfork
));
4416 * Log everything. Do this after conversion, there's no point in
4417 * logging the extent records if we've converted to btree format.
4419 if ((bma
.logflags
& xfs_ilog_fext(whichfork
)) &&
4420 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
4421 bma
.logflags
&= ~xfs_ilog_fext(whichfork
);
4422 else if ((bma
.logflags
& xfs_ilog_fbroot(whichfork
)) &&
4423 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)
4424 bma
.logflags
&= ~xfs_ilog_fbroot(whichfork
);
4426 * Log whatever the flags say, even if error. Otherwise we might miss
4427 * detecting a case where the data is changed, there's an error,
4428 * and it's not logged so we don't shutdown when we should.
4431 xfs_trans_log_inode(tp
, ip
, bma
.logflags
);
4434 xfs_btree_del_cursor(bma
.cur
, error
);
4437 xfs_bmap_validate_ret(orig_bno
, orig_len
, orig_flags
, orig_mval
,
4444 struct xfs_trans
*tp
,
4445 struct xfs_inode
*ip
,
4448 xfs_fsblock_t startblock
,
4451 struct xfs_mount
*mp
= ip
->i_mount
;
4452 struct xfs_ifork
*ifp
;
4453 struct xfs_btree_cur
*cur
= NULL
;
4454 struct xfs_bmbt_irec got
;
4455 struct xfs_iext_cursor icur
;
4456 int whichfork
= xfs_bmapi_whichfork(flags
);
4457 int logflags
= 0, error
;
4459 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4461 ASSERT(len
<= (xfs_filblks_t
)MAXEXTLEN
);
4462 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
4463 ASSERT(!(flags
& ~(XFS_BMAPI_ATTRFORK
| XFS_BMAPI_PREALLOC
|
4464 XFS_BMAPI_NORMAP
)));
4465 ASSERT((flags
& (XFS_BMAPI_ATTRFORK
| XFS_BMAPI_PREALLOC
)) !=
4466 (XFS_BMAPI_ATTRFORK
| XFS_BMAPI_PREALLOC
));
4468 if (unlikely(XFS_TEST_ERROR(
4469 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
4470 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
4471 mp
, XFS_ERRTAG_BMAPIFORMAT
))) {
4472 XFS_ERROR_REPORT("xfs_bmapi_remap", XFS_ERRLEVEL_LOW
, mp
);
4473 return -EFSCORRUPTED
;
4476 if (XFS_FORCED_SHUTDOWN(mp
))
4479 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
4480 error
= xfs_iread_extents(tp
, ip
, whichfork
);
4485 if (xfs_iext_lookup_extent(ip
, ifp
, bno
, &icur
, &got
)) {
4486 /* make sure we only reflink into a hole. */
4487 ASSERT(got
.br_startoff
> bno
);
4488 ASSERT(got
.br_startoff
- bno
>= len
);
4491 ip
->i_d
.di_nblocks
+= len
;
4492 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
4494 if (ifp
->if_flags
& XFS_IFBROOT
) {
4495 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
4496 cur
->bc_private
.b
.flags
= 0;
4499 got
.br_startoff
= bno
;
4500 got
.br_startblock
= startblock
;
4501 got
.br_blockcount
= len
;
4502 if (flags
& XFS_BMAPI_PREALLOC
)
4503 got
.br_state
= XFS_EXT_UNWRITTEN
;
4505 got
.br_state
= XFS_EXT_NORM
;
4507 error
= xfs_bmap_add_extent_hole_real(tp
, ip
, whichfork
, &icur
,
4508 &cur
, &got
, &logflags
, flags
);
4512 if (xfs_bmap_wants_extents(ip
, whichfork
)) {
4513 int tmp_logflags
= 0;
4515 error
= xfs_bmap_btree_to_extents(tp
, ip
, cur
,
4516 &tmp_logflags
, whichfork
);
4517 logflags
|= tmp_logflags
;
4521 if (ip
->i_d
.di_format
!= XFS_DINODE_FMT_EXTENTS
)
4522 logflags
&= ~XFS_ILOG_DEXT
;
4523 else if (ip
->i_d
.di_format
!= XFS_DINODE_FMT_BTREE
)
4524 logflags
&= ~XFS_ILOG_DBROOT
;
4527 xfs_trans_log_inode(tp
, ip
, logflags
);
4529 xfs_btree_del_cursor(cur
, error
);
4534 * When a delalloc extent is split (e.g., due to a hole punch), the original
4535 * indlen reservation must be shared across the two new extents that are left
4538 * Given the original reservation and the worst case indlen for the two new
4539 * extents (as calculated by xfs_bmap_worst_indlen()), split the original
4540 * reservation fairly across the two new extents. If necessary, steal available
4541 * blocks from a deleted extent to make up a reservation deficiency (e.g., if
4542 * ores == 1). The number of stolen blocks is returned. The availability and
4543 * subsequent accounting of stolen blocks is the responsibility of the caller.
4545 static xfs_filblks_t
4546 xfs_bmap_split_indlen(
4547 xfs_filblks_t ores
, /* original res. */
4548 xfs_filblks_t
*indlen1
, /* ext1 worst indlen */
4549 xfs_filblks_t
*indlen2
, /* ext2 worst indlen */
4550 xfs_filblks_t avail
) /* stealable blocks */
4552 xfs_filblks_t len1
= *indlen1
;
4553 xfs_filblks_t len2
= *indlen2
;
4554 xfs_filblks_t nres
= len1
+ len2
; /* new total res. */
4555 xfs_filblks_t stolen
= 0;
4556 xfs_filblks_t resfactor
;
4559 * Steal as many blocks as we can to try and satisfy the worst case
4560 * indlen for both new extents.
4562 if (ores
< nres
&& avail
)
4563 stolen
= XFS_FILBLKS_MIN(nres
- ores
, avail
);
4566 /* nothing else to do if we've satisfied the new reservation */
4571 * We can't meet the total required reservation for the two extents.
4572 * Calculate the percent of the overall shortage between both extents
4573 * and apply this percentage to each of the requested indlen values.
4574 * This distributes the shortage fairly and reduces the chances that one
4575 * of the two extents is left with nothing when extents are repeatedly
4578 resfactor
= (ores
* 100);
4579 do_div(resfactor
, nres
);
4584 ASSERT(len1
+ len2
<= ores
);
4585 ASSERT(len1
< *indlen1
&& len2
< *indlen2
);
4588 * Hand out the remainder to each extent. If one of the two reservations
4589 * is zero, we want to make sure that one gets a block first. The loop
4590 * below starts with len1, so hand len2 a block right off the bat if it
4593 ores
-= (len1
+ len2
);
4594 ASSERT((*indlen1
- len1
) + (*indlen2
- len2
) >= ores
);
4595 if (ores
&& !len2
&& *indlen2
) {
4600 if (len1
< *indlen1
) {
4606 if (len2
< *indlen2
) {
4619 xfs_bmap_del_extent_delay(
4620 struct xfs_inode
*ip
,
4622 struct xfs_iext_cursor
*icur
,
4623 struct xfs_bmbt_irec
*got
,
4624 struct xfs_bmbt_irec
*del
)
4626 struct xfs_mount
*mp
= ip
->i_mount
;
4627 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4628 struct xfs_bmbt_irec
new;
4629 int64_t da_old
, da_new
, da_diff
= 0;
4630 xfs_fileoff_t del_endoff
, got_endoff
;
4631 xfs_filblks_t got_indlen
, new_indlen
, stolen
;
4632 int state
= xfs_bmap_fork_to_state(whichfork
);
4636 XFS_STATS_INC(mp
, xs_del_exlist
);
4638 isrt
= (whichfork
== XFS_DATA_FORK
) && XFS_IS_REALTIME_INODE(ip
);
4639 del_endoff
= del
->br_startoff
+ del
->br_blockcount
;
4640 got_endoff
= got
->br_startoff
+ got
->br_blockcount
;
4641 da_old
= startblockval(got
->br_startblock
);
4644 ASSERT(del
->br_blockcount
> 0);
4645 ASSERT(got
->br_startoff
<= del
->br_startoff
);
4646 ASSERT(got_endoff
>= del_endoff
);
4649 uint64_t rtexts
= XFS_FSB_TO_B(mp
, del
->br_blockcount
);
4651 do_div(rtexts
, mp
->m_sb
.sb_rextsize
);
4652 xfs_mod_frextents(mp
, rtexts
);
4656 * Update the inode delalloc counter now and wait to update the
4657 * sb counters as we might have to borrow some blocks for the
4658 * indirect block accounting.
4660 error
= xfs_trans_reserve_quota_nblks(NULL
, ip
,
4661 -((long)del
->br_blockcount
), 0,
4662 isrt
? XFS_QMOPT_RES_RTBLKS
: XFS_QMOPT_RES_REGBLKS
);
4665 ip
->i_delayed_blks
-= del
->br_blockcount
;
4667 if (got
->br_startoff
== del
->br_startoff
)
4668 state
|= BMAP_LEFT_FILLING
;
4669 if (got_endoff
== del_endoff
)
4670 state
|= BMAP_RIGHT_FILLING
;
4672 switch (state
& (BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
)) {
4673 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
4675 * Matches the whole extent. Delete the entry.
4677 xfs_iext_remove(ip
, icur
, state
);
4678 xfs_iext_prev(ifp
, icur
);
4680 case BMAP_LEFT_FILLING
:
4682 * Deleting the first part of the extent.
4684 got
->br_startoff
= del_endoff
;
4685 got
->br_blockcount
-= del
->br_blockcount
;
4686 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
,
4687 got
->br_blockcount
), da_old
);
4688 got
->br_startblock
= nullstartblock((int)da_new
);
4689 xfs_iext_update_extent(ip
, state
, icur
, got
);
4691 case BMAP_RIGHT_FILLING
:
4693 * Deleting the last part of the extent.
4695 got
->br_blockcount
= got
->br_blockcount
- del
->br_blockcount
;
4696 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
,
4697 got
->br_blockcount
), da_old
);
4698 got
->br_startblock
= nullstartblock((int)da_new
);
4699 xfs_iext_update_extent(ip
, state
, icur
, got
);
4703 * Deleting the middle of the extent.
4705 * Distribute the original indlen reservation across the two new
4706 * extents. Steal blocks from the deleted extent if necessary.
4707 * Stealing blocks simply fudges the fdblocks accounting below.
4708 * Warn if either of the new indlen reservations is zero as this
4709 * can lead to delalloc problems.
4711 got
->br_blockcount
= del
->br_startoff
- got
->br_startoff
;
4712 got_indlen
= xfs_bmap_worst_indlen(ip
, got
->br_blockcount
);
4714 new.br_blockcount
= got_endoff
- del_endoff
;
4715 new_indlen
= xfs_bmap_worst_indlen(ip
, new.br_blockcount
);
4717 WARN_ON_ONCE(!got_indlen
|| !new_indlen
);
4718 stolen
= xfs_bmap_split_indlen(da_old
, &got_indlen
, &new_indlen
,
4719 del
->br_blockcount
);
4721 got
->br_startblock
= nullstartblock((int)got_indlen
);
4723 new.br_startoff
= del_endoff
;
4724 new.br_state
= got
->br_state
;
4725 new.br_startblock
= nullstartblock((int)new_indlen
);
4727 xfs_iext_update_extent(ip
, state
, icur
, got
);
4728 xfs_iext_next(ifp
, icur
);
4729 xfs_iext_insert(ip
, icur
, &new, state
);
4731 da_new
= got_indlen
+ new_indlen
- stolen
;
4732 del
->br_blockcount
-= stolen
;
4736 ASSERT(da_old
>= da_new
);
4737 da_diff
= da_old
- da_new
;
4739 da_diff
+= del
->br_blockcount
;
4741 xfs_mod_fdblocks(mp
, da_diff
, false);
4746 xfs_bmap_del_extent_cow(
4747 struct xfs_inode
*ip
,
4748 struct xfs_iext_cursor
*icur
,
4749 struct xfs_bmbt_irec
*got
,
4750 struct xfs_bmbt_irec
*del
)
4752 struct xfs_mount
*mp
= ip
->i_mount
;
4753 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, XFS_COW_FORK
);
4754 struct xfs_bmbt_irec
new;
4755 xfs_fileoff_t del_endoff
, got_endoff
;
4756 int state
= BMAP_COWFORK
;
4758 XFS_STATS_INC(mp
, xs_del_exlist
);
4760 del_endoff
= del
->br_startoff
+ del
->br_blockcount
;
4761 got_endoff
= got
->br_startoff
+ got
->br_blockcount
;
4763 ASSERT(del
->br_blockcount
> 0);
4764 ASSERT(got
->br_startoff
<= del
->br_startoff
);
4765 ASSERT(got_endoff
>= del_endoff
);
4766 ASSERT(!isnullstartblock(got
->br_startblock
));
4768 if (got
->br_startoff
== del
->br_startoff
)
4769 state
|= BMAP_LEFT_FILLING
;
4770 if (got_endoff
== del_endoff
)
4771 state
|= BMAP_RIGHT_FILLING
;
4773 switch (state
& (BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
)) {
4774 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
4776 * Matches the whole extent. Delete the entry.
4778 xfs_iext_remove(ip
, icur
, state
);
4779 xfs_iext_prev(ifp
, icur
);
4781 case BMAP_LEFT_FILLING
:
4783 * Deleting the first part of the extent.
4785 got
->br_startoff
= del_endoff
;
4786 got
->br_blockcount
-= del
->br_blockcount
;
4787 got
->br_startblock
= del
->br_startblock
+ del
->br_blockcount
;
4788 xfs_iext_update_extent(ip
, state
, icur
, got
);
4790 case BMAP_RIGHT_FILLING
:
4792 * Deleting the last part of the extent.
4794 got
->br_blockcount
-= del
->br_blockcount
;
4795 xfs_iext_update_extent(ip
, state
, icur
, got
);
4799 * Deleting the middle of the extent.
4801 got
->br_blockcount
= del
->br_startoff
- got
->br_startoff
;
4803 new.br_startoff
= del_endoff
;
4804 new.br_blockcount
= got_endoff
- del_endoff
;
4805 new.br_state
= got
->br_state
;
4806 new.br_startblock
= del
->br_startblock
+ del
->br_blockcount
;
4808 xfs_iext_update_extent(ip
, state
, icur
, got
);
4809 xfs_iext_next(ifp
, icur
);
4810 xfs_iext_insert(ip
, icur
, &new, state
);
4813 ip
->i_delayed_blks
-= del
->br_blockcount
;
4817 * Called by xfs_bmapi to update file extent records and the btree
4818 * after removing space.
4820 STATIC
int /* error */
4821 xfs_bmap_del_extent_real(
4822 xfs_inode_t
*ip
, /* incore inode pointer */
4823 xfs_trans_t
*tp
, /* current transaction pointer */
4824 struct xfs_iext_cursor
*icur
,
4825 xfs_btree_cur_t
*cur
, /* if null, not a btree */
4826 xfs_bmbt_irec_t
*del
, /* data to remove from extents */
4827 int *logflagsp
, /* inode logging flags */
4828 int whichfork
, /* data or attr fork */
4829 int bflags
) /* bmapi flags */
4831 xfs_fsblock_t del_endblock
=0; /* first block past del */
4832 xfs_fileoff_t del_endoff
; /* first offset past del */
4833 int do_fx
; /* free extent at end of routine */
4834 int error
; /* error return value */
4835 int flags
= 0;/* inode logging flags */
4836 struct xfs_bmbt_irec got
; /* current extent entry */
4837 xfs_fileoff_t got_endoff
; /* first offset past got */
4838 int i
; /* temp state */
4839 struct xfs_ifork
*ifp
; /* inode fork pointer */
4840 xfs_mount_t
*mp
; /* mount structure */
4841 xfs_filblks_t nblks
; /* quota/sb block count */
4842 xfs_bmbt_irec_t
new; /* new record to be inserted */
4844 uint qfield
; /* quota field to update */
4845 int state
= xfs_bmap_fork_to_state(whichfork
);
4846 struct xfs_bmbt_irec old
;
4847 struct xfs_defer_ops
*dfops
= tp
? tp
->t_dfops
: NULL
;
4850 XFS_STATS_INC(mp
, xs_del_exlist
);
4852 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4853 ASSERT(del
->br_blockcount
> 0);
4854 xfs_iext_get_extent(ifp
, icur
, &got
);
4855 ASSERT(got
.br_startoff
<= del
->br_startoff
);
4856 del_endoff
= del
->br_startoff
+ del
->br_blockcount
;
4857 got_endoff
= got
.br_startoff
+ got
.br_blockcount
;
4858 ASSERT(got_endoff
>= del_endoff
);
4859 ASSERT(!isnullstartblock(got
.br_startblock
));
4864 * If it's the case where the directory code is running with no block
4865 * reservation, and the deleted block is in the middle of its extent,
4866 * and the resulting insert of an extent would cause transformation to
4867 * btree format, then reject it. The calling code will then swap blocks
4868 * around instead. We have to do this now, rather than waiting for the
4869 * conversion to btree format, since the transaction will be dirty then.
4871 if (tp
->t_blk_res
== 0 &&
4872 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
&&
4873 XFS_IFORK_NEXTENTS(ip
, whichfork
) >=
4874 XFS_IFORK_MAXEXT(ip
, whichfork
) &&
4875 del
->br_startoff
> got
.br_startoff
&& del_endoff
< got_endoff
)
4878 flags
= XFS_ILOG_CORE
;
4879 if (whichfork
== XFS_DATA_FORK
&& XFS_IS_REALTIME_INODE(ip
)) {
4884 bno
= div_u64_rem(del
->br_startblock
, mp
->m_sb
.sb_rextsize
,
4887 len
= div_u64_rem(del
->br_blockcount
, mp
->m_sb
.sb_rextsize
,
4891 error
= xfs_rtfree_extent(tp
, bno
, (xfs_extlen_t
)len
);
4895 nblks
= len
* mp
->m_sb
.sb_rextsize
;
4896 qfield
= XFS_TRANS_DQ_RTBCOUNT
;
4899 nblks
= del
->br_blockcount
;
4900 qfield
= XFS_TRANS_DQ_BCOUNT
;
4903 del_endblock
= del
->br_startblock
+ del
->br_blockcount
;
4905 error
= xfs_bmbt_lookup_eq(cur
, &got
, &i
);
4908 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
4911 if (got
.br_startoff
== del
->br_startoff
)
4912 state
|= BMAP_LEFT_FILLING
;
4913 if (got_endoff
== del_endoff
)
4914 state
|= BMAP_RIGHT_FILLING
;
4916 switch (state
& (BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
)) {
4917 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
4919 * Matches the whole extent. Delete the entry.
4921 xfs_iext_remove(ip
, icur
, state
);
4922 xfs_iext_prev(ifp
, icur
);
4923 XFS_IFORK_NEXT_SET(ip
, whichfork
,
4924 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
4925 flags
|= XFS_ILOG_CORE
;
4927 flags
|= xfs_ilog_fext(whichfork
);
4930 if ((error
= xfs_btree_delete(cur
, &i
)))
4932 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
4934 case BMAP_LEFT_FILLING
:
4936 * Deleting the first part of the extent.
4938 got
.br_startoff
= del_endoff
;
4939 got
.br_startblock
= del_endblock
;
4940 got
.br_blockcount
-= del
->br_blockcount
;
4941 xfs_iext_update_extent(ip
, state
, icur
, &got
);
4943 flags
|= xfs_ilog_fext(whichfork
);
4946 error
= xfs_bmbt_update(cur
, &got
);
4950 case BMAP_RIGHT_FILLING
:
4952 * Deleting the last part of the extent.
4954 got
.br_blockcount
-= del
->br_blockcount
;
4955 xfs_iext_update_extent(ip
, state
, icur
, &got
);
4957 flags
|= xfs_ilog_fext(whichfork
);
4960 error
= xfs_bmbt_update(cur
, &got
);
4966 * Deleting the middle of the extent.
4970 got
.br_blockcount
= del
->br_startoff
- got
.br_startoff
;
4971 xfs_iext_update_extent(ip
, state
, icur
, &got
);
4973 new.br_startoff
= del_endoff
;
4974 new.br_blockcount
= got_endoff
- del_endoff
;
4975 new.br_state
= got
.br_state
;
4976 new.br_startblock
= del_endblock
;
4978 flags
|= XFS_ILOG_CORE
;
4980 error
= xfs_bmbt_update(cur
, &got
);
4983 error
= xfs_btree_increment(cur
, 0, &i
);
4986 cur
->bc_rec
.b
= new;
4987 error
= xfs_btree_insert(cur
, &i
);
4988 if (error
&& error
!= -ENOSPC
)
4991 * If get no-space back from btree insert, it tried a
4992 * split, and we have a zero block reservation. Fix up
4993 * our state and return the error.
4995 if (error
== -ENOSPC
) {
4997 * Reset the cursor, don't trust it after any
5000 error
= xfs_bmbt_lookup_eq(cur
, &got
, &i
);
5003 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
5005 * Update the btree record back
5006 * to the original value.
5008 error
= xfs_bmbt_update(cur
, &old
);
5012 * Reset the extent record back
5013 * to the original value.
5015 xfs_iext_update_extent(ip
, state
, icur
, &old
);
5020 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
5022 flags
|= xfs_ilog_fext(whichfork
);
5023 XFS_IFORK_NEXT_SET(ip
, whichfork
,
5024 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
5025 xfs_iext_next(ifp
, icur
);
5026 xfs_iext_insert(ip
, icur
, &new, state
);
5030 /* remove reverse mapping */
5031 error
= xfs_rmap_unmap_extent(mp
, dfops
, ip
, whichfork
, del
);
5036 * If we need to, add to list of extents to delete.
5038 if (do_fx
&& !(bflags
& XFS_BMAPI_REMAP
)) {
5039 if (xfs_is_reflink_inode(ip
) && whichfork
== XFS_DATA_FORK
) {
5040 error
= xfs_refcount_decrease_extent(mp
, dfops
, del
);
5044 __xfs_bmap_add_free(mp
, dfops
, del
->br_startblock
,
5045 del
->br_blockcount
, NULL
,
5046 (bflags
& XFS_BMAPI_NODISCARD
) ||
5047 del
->br_state
== XFS_EXT_UNWRITTEN
);
5052 * Adjust inode # blocks in the file.
5055 ip
->i_d
.di_nblocks
-= nblks
;
5057 * Adjust quota data.
5059 if (qfield
&& !(bflags
& XFS_BMAPI_REMAP
))
5060 xfs_trans_mod_dquot_byino(tp
, ip
, qfield
, (long)-nblks
);
5068 * Unmap (remove) blocks from a file.
5069 * If nexts is nonzero then the number of extents to remove is limited to
5070 * that value. If not all extents in the block range can be removed then
5075 struct xfs_trans
*tp
, /* transaction pointer */
5076 struct xfs_inode
*ip
, /* incore inode */
5077 xfs_fileoff_t start
, /* first file offset deleted */
5078 xfs_filblks_t
*rlen
, /* i/o: amount remaining */
5079 int flags
, /* misc flags */
5080 xfs_extnum_t nexts
) /* number of extents max */
5082 struct xfs_btree_cur
*cur
; /* bmap btree cursor */
5083 struct xfs_bmbt_irec del
; /* extent being deleted */
5084 int error
; /* error return value */
5085 xfs_extnum_t extno
; /* extent number in list */
5086 struct xfs_bmbt_irec got
; /* current extent record */
5087 struct xfs_ifork
*ifp
; /* inode fork pointer */
5088 int isrt
; /* freeing in rt area */
5089 int logflags
; /* transaction logging flags */
5090 xfs_extlen_t mod
; /* rt extent offset */
5091 struct xfs_mount
*mp
; /* mount structure */
5092 int tmp_logflags
; /* partial logging flags */
5093 int wasdel
; /* was a delayed alloc extent */
5094 int whichfork
; /* data or attribute fork */
5096 xfs_filblks_t len
= *rlen
; /* length to unmap in file */
5097 xfs_fileoff_t max_len
;
5098 xfs_agnumber_t prev_agno
= NULLAGNUMBER
, agno
;
5100 struct xfs_iext_cursor icur
;
5103 trace_xfs_bunmap(ip
, start
, len
, flags
, _RET_IP_
);
5105 whichfork
= xfs_bmapi_whichfork(flags
);
5106 ASSERT(whichfork
!= XFS_COW_FORK
);
5107 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5109 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
5110 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)) {
5111 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW
,
5113 return -EFSCORRUPTED
;
5116 if (XFS_FORCED_SHUTDOWN(mp
))
5119 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
5124 * Guesstimate how many blocks we can unmap without running the risk of
5125 * blowing out the transaction with a mix of EFIs and reflink
5128 if (tp
&& xfs_is_reflink_inode(ip
) && whichfork
== XFS_DATA_FORK
)
5129 max_len
= min(len
, xfs_refcount_max_unmap(tp
->t_log_res
));
5133 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
5134 (error
= xfs_iread_extents(tp
, ip
, whichfork
)))
5136 if (xfs_iext_count(ifp
) == 0) {
5140 XFS_STATS_INC(mp
, xs_blk_unmap
);
5141 isrt
= (whichfork
== XFS_DATA_FORK
) && XFS_IS_REALTIME_INODE(ip
);
5144 if (!xfs_iext_lookup_extent_before(ip
, ifp
, &end
, &icur
, &got
)) {
5151 if (ifp
->if_flags
& XFS_IFBROOT
) {
5152 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
);
5153 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
5154 cur
->bc_private
.b
.flags
= 0;
5160 * Synchronize by locking the bitmap inode.
5162 xfs_ilock(mp
->m_rbmip
, XFS_ILOCK_EXCL
|XFS_ILOCK_RTBITMAP
);
5163 xfs_trans_ijoin(tp
, mp
->m_rbmip
, XFS_ILOCK_EXCL
);
5164 xfs_ilock(mp
->m_rsumip
, XFS_ILOCK_EXCL
|XFS_ILOCK_RTSUM
);
5165 xfs_trans_ijoin(tp
, mp
->m_rsumip
, XFS_ILOCK_EXCL
);
5169 while (end
!= (xfs_fileoff_t
)-1 && end
>= start
&&
5170 (nexts
== 0 || extno
< nexts
) && max_len
> 0) {
5172 * Is the found extent after a hole in which end lives?
5173 * Just back up to the previous extent, if so.
5175 if (got
.br_startoff
> end
&&
5176 !xfs_iext_prev_extent(ifp
, &icur
, &got
)) {
5181 * Is the last block of this extent before the range
5182 * we're supposed to delete? If so, we're done.
5184 end
= XFS_FILEOFF_MIN(end
,
5185 got
.br_startoff
+ got
.br_blockcount
- 1);
5189 * Then deal with the (possibly delayed) allocated space
5193 wasdel
= isnullstartblock(del
.br_startblock
);
5196 * Make sure we don't touch multiple AGF headers out of order
5197 * in a single transaction, as that could cause AB-BA deadlocks.
5200 agno
= XFS_FSB_TO_AGNO(mp
, del
.br_startblock
);
5201 if (prev_agno
!= NULLAGNUMBER
&& prev_agno
> agno
)
5205 if (got
.br_startoff
< start
) {
5206 del
.br_startoff
= start
;
5207 del
.br_blockcount
-= start
- got
.br_startoff
;
5209 del
.br_startblock
+= start
- got
.br_startoff
;
5211 if (del
.br_startoff
+ del
.br_blockcount
> end
+ 1)
5212 del
.br_blockcount
= end
+ 1 - del
.br_startoff
;
5214 /* How much can we safely unmap? */
5215 if (max_len
< del
.br_blockcount
) {
5216 del
.br_startoff
+= del
.br_blockcount
- max_len
;
5218 del
.br_startblock
+= del
.br_blockcount
- max_len
;
5219 del
.br_blockcount
= max_len
;
5225 sum
= del
.br_startblock
+ del
.br_blockcount
;
5226 div_u64_rem(sum
, mp
->m_sb
.sb_rextsize
, &mod
);
5229 * Realtime extent not lined up at the end.
5230 * The extent could have been split into written
5231 * and unwritten pieces, or we could just be
5232 * unmapping part of it. But we can't really
5233 * get rid of part of a realtime extent.
5235 if (del
.br_state
== XFS_EXT_UNWRITTEN
||
5236 !xfs_sb_version_hasextflgbit(&mp
->m_sb
)) {
5238 * This piece is unwritten, or we're not
5239 * using unwritten extents. Skip over it.
5242 end
-= mod
> del
.br_blockcount
?
5243 del
.br_blockcount
: mod
;
5244 if (end
< got
.br_startoff
&&
5245 !xfs_iext_prev_extent(ifp
, &icur
, &got
)) {
5252 * It's written, turn it unwritten.
5253 * This is better than zeroing it.
5255 ASSERT(del
.br_state
== XFS_EXT_NORM
);
5256 ASSERT(tp
->t_blk_res
> 0);
5258 * If this spans a realtime extent boundary,
5259 * chop it back to the start of the one we end at.
5261 if (del
.br_blockcount
> mod
) {
5262 del
.br_startoff
+= del
.br_blockcount
- mod
;
5263 del
.br_startblock
+= del
.br_blockcount
- mod
;
5264 del
.br_blockcount
= mod
;
5266 del
.br_state
= XFS_EXT_UNWRITTEN
;
5267 error
= xfs_bmap_add_extent_unwritten_real(tp
, ip
,
5268 whichfork
, &icur
, &cur
, &del
,
5274 div_u64_rem(del
.br_startblock
, mp
->m_sb
.sb_rextsize
, &mod
);
5277 * Realtime extent is lined up at the end but not
5278 * at the front. We'll get rid of full extents if
5281 mod
= mp
->m_sb
.sb_rextsize
- mod
;
5282 if (del
.br_blockcount
> mod
) {
5283 del
.br_blockcount
-= mod
;
5284 del
.br_startoff
+= mod
;
5285 del
.br_startblock
+= mod
;
5286 } else if ((del
.br_startoff
== start
&&
5287 (del
.br_state
== XFS_EXT_UNWRITTEN
||
5288 tp
->t_blk_res
== 0)) ||
5289 !xfs_sb_version_hasextflgbit(&mp
->m_sb
)) {
5291 * Can't make it unwritten. There isn't
5292 * a full extent here so just skip it.
5294 ASSERT(end
>= del
.br_blockcount
);
5295 end
-= del
.br_blockcount
;
5296 if (got
.br_startoff
> end
&&
5297 !xfs_iext_prev_extent(ifp
, &icur
, &got
)) {
5302 } else if (del
.br_state
== XFS_EXT_UNWRITTEN
) {
5303 struct xfs_bmbt_irec prev
;
5306 * This one is already unwritten.
5307 * It must have a written left neighbor.
5308 * Unwrite the killed part of that one and
5311 if (!xfs_iext_prev_extent(ifp
, &icur
, &prev
))
5313 ASSERT(prev
.br_state
== XFS_EXT_NORM
);
5314 ASSERT(!isnullstartblock(prev
.br_startblock
));
5315 ASSERT(del
.br_startblock
==
5316 prev
.br_startblock
+ prev
.br_blockcount
);
5317 if (prev
.br_startoff
< start
) {
5318 mod
= start
- prev
.br_startoff
;
5319 prev
.br_blockcount
-= mod
;
5320 prev
.br_startblock
+= mod
;
5321 prev
.br_startoff
= start
;
5323 prev
.br_state
= XFS_EXT_UNWRITTEN
;
5324 error
= xfs_bmap_add_extent_unwritten_real(tp
,
5325 ip
, whichfork
, &icur
, &cur
,
5331 ASSERT(del
.br_state
== XFS_EXT_NORM
);
5332 del
.br_state
= XFS_EXT_UNWRITTEN
;
5333 error
= xfs_bmap_add_extent_unwritten_real(tp
,
5334 ip
, whichfork
, &icur
, &cur
,
5344 error
= xfs_bmap_del_extent_delay(ip
, whichfork
, &icur
,
5347 error
= xfs_bmap_del_extent_real(ip
, tp
, &icur
, cur
,
5348 &del
, &tmp_logflags
, whichfork
,
5350 logflags
|= tmp_logflags
;
5356 max_len
-= del
.br_blockcount
;
5357 end
= del
.br_startoff
- 1;
5360 * If not done go on to the next (previous) record.
5362 if (end
!= (xfs_fileoff_t
)-1 && end
>= start
) {
5363 if (!xfs_iext_get_extent(ifp
, &icur
, &got
) ||
5364 (got
.br_startoff
> end
&&
5365 !xfs_iext_prev_extent(ifp
, &icur
, &got
))) {
5372 if (done
|| end
== (xfs_fileoff_t
)-1 || end
< start
)
5375 *rlen
= end
- start
+ 1;
5378 * Convert to a btree if necessary.
5380 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
5381 ASSERT(cur
== NULL
);
5382 error
= xfs_bmap_extents_to_btree(tp
, ip
, &cur
, 0,
5383 &tmp_logflags
, whichfork
);
5384 logflags
|= tmp_logflags
;
5389 * transform from btree to extents, give it cur
5391 else if (xfs_bmap_wants_extents(ip
, whichfork
)) {
5392 ASSERT(cur
!= NULL
);
5393 error
= xfs_bmap_btree_to_extents(tp
, ip
, cur
, &tmp_logflags
,
5395 logflags
|= tmp_logflags
;
5400 * transform from extents to local?
5405 * Log everything. Do this after conversion, there's no point in
5406 * logging the extent records if we've converted to btree format.
5408 if ((logflags
& xfs_ilog_fext(whichfork
)) &&
5409 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
5410 logflags
&= ~xfs_ilog_fext(whichfork
);
5411 else if ((logflags
& xfs_ilog_fbroot(whichfork
)) &&
5412 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)
5413 logflags
&= ~xfs_ilog_fbroot(whichfork
);
5415 * Log inode even in the error case, if the transaction
5416 * is dirty we'll need to shut down the filesystem.
5419 xfs_trans_log_inode(tp
, ip
, logflags
);
5422 cur
->bc_private
.b
.allocated
= 0;
5423 xfs_btree_del_cursor(cur
, error
);
5428 /* Unmap a range of a file. */
5432 struct xfs_inode
*ip
,
5441 error
= __xfs_bunmapi(tp
, ip
, bno
, &len
, flags
, nexts
);
5447 * Determine whether an extent shift can be accomplished by a merge with the
5448 * extent that precedes the target hole of the shift.
5452 struct xfs_bmbt_irec
*left
, /* preceding extent */
5453 struct xfs_bmbt_irec
*got
, /* current extent to shift */
5454 xfs_fileoff_t shift
) /* shift fsb */
5456 xfs_fileoff_t startoff
;
5458 startoff
= got
->br_startoff
- shift
;
5461 * The extent, once shifted, must be adjacent in-file and on-disk with
5462 * the preceding extent.
5464 if ((left
->br_startoff
+ left
->br_blockcount
!= startoff
) ||
5465 (left
->br_startblock
+ left
->br_blockcount
!= got
->br_startblock
) ||
5466 (left
->br_state
!= got
->br_state
) ||
5467 (left
->br_blockcount
+ got
->br_blockcount
> MAXEXTLEN
))
5474 * A bmap extent shift adjusts the file offset of an extent to fill a preceding
5475 * hole in the file. If an extent shift would result in the extent being fully
5476 * adjacent to the extent that currently precedes the hole, we can merge with
5477 * the preceding extent rather than do the shift.
5479 * This function assumes the caller has verified a shift-by-merge is possible
5480 * with the provided extents via xfs_bmse_can_merge().
5484 struct xfs_inode
*ip
,
5486 xfs_fileoff_t shift
, /* shift fsb */
5487 struct xfs_iext_cursor
*icur
,
5488 struct xfs_bmbt_irec
*got
, /* extent to shift */
5489 struct xfs_bmbt_irec
*left
, /* preceding extent */
5490 struct xfs_btree_cur
*cur
,
5491 int *logflags
, /* output */
5492 struct xfs_defer_ops
*dfops
)
5494 struct xfs_bmbt_irec
new;
5495 xfs_filblks_t blockcount
;
5497 struct xfs_mount
*mp
= ip
->i_mount
;
5499 blockcount
= left
->br_blockcount
+ got
->br_blockcount
;
5501 ASSERT(xfs_isilocked(ip
, XFS_IOLOCK_EXCL
));
5502 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
5503 ASSERT(xfs_bmse_can_merge(left
, got
, shift
));
5506 new.br_blockcount
= blockcount
;
5509 * Update the on-disk extent count, the btree if necessary and log the
5512 XFS_IFORK_NEXT_SET(ip
, whichfork
,
5513 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
5514 *logflags
|= XFS_ILOG_CORE
;
5516 *logflags
|= XFS_ILOG_DEXT
;
5520 /* lookup and remove the extent to merge */
5521 error
= xfs_bmbt_lookup_eq(cur
, got
, &i
);
5524 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
5526 error
= xfs_btree_delete(cur
, &i
);
5529 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
5531 /* lookup and update size of the previous extent */
5532 error
= xfs_bmbt_lookup_eq(cur
, left
, &i
);
5535 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
5537 error
= xfs_bmbt_update(cur
, &new);
5542 xfs_iext_remove(ip
, icur
, 0);
5543 xfs_iext_prev(XFS_IFORK_PTR(ip
, whichfork
), icur
);
5544 xfs_iext_update_extent(ip
, xfs_bmap_fork_to_state(whichfork
), icur
,
5547 /* update reverse mapping. rmap functions merge the rmaps for us */
5548 error
= xfs_rmap_unmap_extent(mp
, dfops
, ip
, whichfork
, got
);
5551 memcpy(&new, got
, sizeof(new));
5552 new.br_startoff
= left
->br_startoff
+ left
->br_blockcount
;
5553 return xfs_rmap_map_extent(mp
, dfops
, ip
, whichfork
, &new);
5557 xfs_bmap_shift_update_extent(
5558 struct xfs_inode
*ip
,
5560 struct xfs_iext_cursor
*icur
,
5561 struct xfs_bmbt_irec
*got
,
5562 struct xfs_btree_cur
*cur
,
5564 struct xfs_defer_ops
*dfops
,
5565 xfs_fileoff_t startoff
)
5567 struct xfs_mount
*mp
= ip
->i_mount
;
5568 struct xfs_bmbt_irec prev
= *got
;
5571 *logflags
|= XFS_ILOG_CORE
;
5573 got
->br_startoff
= startoff
;
5576 error
= xfs_bmbt_lookup_eq(cur
, &prev
, &i
);
5579 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
5581 error
= xfs_bmbt_update(cur
, got
);
5585 *logflags
|= XFS_ILOG_DEXT
;
5588 xfs_iext_update_extent(ip
, xfs_bmap_fork_to_state(whichfork
), icur
,
5591 /* update reverse mapping */
5592 error
= xfs_rmap_unmap_extent(mp
, dfops
, ip
, whichfork
, &prev
);
5595 return xfs_rmap_map_extent(mp
, dfops
, ip
, whichfork
, got
);
5599 xfs_bmap_collapse_extents(
5600 struct xfs_trans
*tp
,
5601 struct xfs_inode
*ip
,
5602 xfs_fileoff_t
*next_fsb
,
5603 xfs_fileoff_t offset_shift_fsb
,
5606 int whichfork
= XFS_DATA_FORK
;
5607 struct xfs_mount
*mp
= ip
->i_mount
;
5608 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5609 struct xfs_btree_cur
*cur
= NULL
;
5610 struct xfs_bmbt_irec got
, prev
;
5611 struct xfs_iext_cursor icur
;
5612 xfs_fileoff_t new_startoff
;
5616 if (unlikely(XFS_TEST_ERROR(
5617 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
5618 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
5619 mp
, XFS_ERRTAG_BMAPIFORMAT
))) {
5620 XFS_ERROR_REPORT(__func__
, XFS_ERRLEVEL_LOW
, mp
);
5621 return -EFSCORRUPTED
;
5624 if (XFS_FORCED_SHUTDOWN(mp
))
5627 ASSERT(xfs_isilocked(ip
, XFS_IOLOCK_EXCL
| XFS_ILOCK_EXCL
));
5629 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
5630 error
= xfs_iread_extents(tp
, ip
, whichfork
);
5635 if (ifp
->if_flags
& XFS_IFBROOT
) {
5636 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
5637 cur
->bc_private
.b
.flags
= 0;
5640 if (!xfs_iext_lookup_extent(ip
, ifp
, *next_fsb
, &icur
, &got
)) {
5644 XFS_WANT_CORRUPTED_GOTO(mp
, !isnullstartblock(got
.br_startblock
),
5647 new_startoff
= got
.br_startoff
- offset_shift_fsb
;
5648 if (xfs_iext_peek_prev_extent(ifp
, &icur
, &prev
)) {
5649 if (new_startoff
< prev
.br_startoff
+ prev
.br_blockcount
) {
5654 if (xfs_bmse_can_merge(&prev
, &got
, offset_shift_fsb
)) {
5655 error
= xfs_bmse_merge(ip
, whichfork
, offset_shift_fsb
,
5656 &icur
, &got
, &prev
, cur
, &logflags
,
5663 if (got
.br_startoff
< offset_shift_fsb
) {
5669 error
= xfs_bmap_shift_update_extent(ip
, whichfork
, &icur
, &got
, cur
,
5670 &logflags
, tp
->t_dfops
, new_startoff
);
5675 if (!xfs_iext_next_extent(ifp
, &icur
, &got
)) {
5680 *next_fsb
= got
.br_startoff
;
5683 xfs_btree_del_cursor(cur
, error
);
5685 xfs_trans_log_inode(tp
, ip
, logflags
);
5689 /* Make sure we won't be right-shifting an extent past the maximum bound. */
5691 xfs_bmap_can_insert_extents(
5692 struct xfs_inode
*ip
,
5694 xfs_fileoff_t shift
)
5696 struct xfs_bmbt_irec got
;
5700 ASSERT(xfs_isilocked(ip
, XFS_IOLOCK_EXCL
));
5702 if (XFS_FORCED_SHUTDOWN(ip
->i_mount
))
5705 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
5706 error
= xfs_bmap_last_extent(NULL
, ip
, XFS_DATA_FORK
, &got
, &is_empty
);
5707 if (!error
&& !is_empty
&& got
.br_startoff
>= off
&&
5708 ((got
.br_startoff
+ shift
) & BMBT_STARTOFF_MASK
) < got
.br_startoff
)
5710 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
5716 xfs_bmap_insert_extents(
5717 struct xfs_trans
*tp
,
5718 struct xfs_inode
*ip
,
5719 xfs_fileoff_t
*next_fsb
,
5720 xfs_fileoff_t offset_shift_fsb
,
5722 xfs_fileoff_t stop_fsb
)
5724 int whichfork
= XFS_DATA_FORK
;
5725 struct xfs_mount
*mp
= ip
->i_mount
;
5726 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5727 struct xfs_btree_cur
*cur
= NULL
;
5728 struct xfs_bmbt_irec got
, next
;
5729 struct xfs_iext_cursor icur
;
5730 xfs_fileoff_t new_startoff
;
5734 if (unlikely(XFS_TEST_ERROR(
5735 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
5736 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
5737 mp
, XFS_ERRTAG_BMAPIFORMAT
))) {
5738 XFS_ERROR_REPORT(__func__
, XFS_ERRLEVEL_LOW
, mp
);
5739 return -EFSCORRUPTED
;
5742 if (XFS_FORCED_SHUTDOWN(mp
))
5745 ASSERT(xfs_isilocked(ip
, XFS_IOLOCK_EXCL
| XFS_ILOCK_EXCL
));
5747 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
5748 error
= xfs_iread_extents(tp
, ip
, whichfork
);
5753 if (ifp
->if_flags
& XFS_IFBROOT
) {
5754 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
5755 cur
->bc_private
.b
.flags
= 0;
5758 if (*next_fsb
== NULLFSBLOCK
) {
5759 xfs_iext_last(ifp
, &icur
);
5760 if (!xfs_iext_get_extent(ifp
, &icur
, &got
) ||
5761 stop_fsb
> got
.br_startoff
) {
5766 if (!xfs_iext_lookup_extent(ip
, ifp
, *next_fsb
, &icur
, &got
)) {
5771 XFS_WANT_CORRUPTED_GOTO(mp
, !isnullstartblock(got
.br_startblock
),
5774 if (stop_fsb
>= got
.br_startoff
+ got
.br_blockcount
) {
5779 new_startoff
= got
.br_startoff
+ offset_shift_fsb
;
5780 if (xfs_iext_peek_next_extent(ifp
, &icur
, &next
)) {
5781 if (new_startoff
+ got
.br_blockcount
> next
.br_startoff
) {
5787 * Unlike a left shift (which involves a hole punch), a right
5788 * shift does not modify extent neighbors in any way. We should
5789 * never find mergeable extents in this scenario. Check anyways
5790 * and warn if we encounter two extents that could be one.
5792 if (xfs_bmse_can_merge(&got
, &next
, offset_shift_fsb
))
5796 error
= xfs_bmap_shift_update_extent(ip
, whichfork
, &icur
, &got
, cur
,
5797 &logflags
, tp
->t_dfops
, new_startoff
);
5801 if (!xfs_iext_prev_extent(ifp
, &icur
, &got
) ||
5802 stop_fsb
>= got
.br_startoff
+ got
.br_blockcount
) {
5807 *next_fsb
= got
.br_startoff
;
5810 xfs_btree_del_cursor(cur
, error
);
5812 xfs_trans_log_inode(tp
, ip
, logflags
);
5817 * Splits an extent into two extents at split_fsb block such that it is the
5818 * first block of the current_ext. @ext is a target extent to be split.
5819 * @split_fsb is a block where the extents is split. If split_fsb lies in a
5820 * hole or the first block of extents, just return 0.
5823 xfs_bmap_split_extent_at(
5824 struct xfs_trans
*tp
,
5825 struct xfs_inode
*ip
,
5826 xfs_fileoff_t split_fsb
)
5828 int whichfork
= XFS_DATA_FORK
;
5829 struct xfs_btree_cur
*cur
= NULL
;
5830 struct xfs_bmbt_irec got
;
5831 struct xfs_bmbt_irec
new; /* split extent */
5832 struct xfs_mount
*mp
= ip
->i_mount
;
5833 struct xfs_ifork
*ifp
;
5834 xfs_fsblock_t gotblkcnt
; /* new block count for got */
5835 struct xfs_iext_cursor icur
;
5840 if (unlikely(XFS_TEST_ERROR(
5841 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
5842 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
5843 mp
, XFS_ERRTAG_BMAPIFORMAT
))) {
5844 XFS_ERROR_REPORT("xfs_bmap_split_extent_at",
5845 XFS_ERRLEVEL_LOW
, mp
);
5846 return -EFSCORRUPTED
;
5849 if (XFS_FORCED_SHUTDOWN(mp
))
5852 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5853 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
5854 /* Read in all the extents */
5855 error
= xfs_iread_extents(tp
, ip
, whichfork
);
5861 * If there are not extents, or split_fsb lies in a hole we are done.
5863 if (!xfs_iext_lookup_extent(ip
, ifp
, split_fsb
, &icur
, &got
) ||
5864 got
.br_startoff
>= split_fsb
)
5867 gotblkcnt
= split_fsb
- got
.br_startoff
;
5868 new.br_startoff
= split_fsb
;
5869 new.br_startblock
= got
.br_startblock
+ gotblkcnt
;
5870 new.br_blockcount
= got
.br_blockcount
- gotblkcnt
;
5871 new.br_state
= got
.br_state
;
5873 if (ifp
->if_flags
& XFS_IFBROOT
) {
5874 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
5875 cur
->bc_private
.b
.flags
= 0;
5876 error
= xfs_bmbt_lookup_eq(cur
, &got
, &i
);
5879 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, del_cursor
);
5882 got
.br_blockcount
= gotblkcnt
;
5883 xfs_iext_update_extent(ip
, xfs_bmap_fork_to_state(whichfork
), &icur
,
5886 logflags
= XFS_ILOG_CORE
;
5888 error
= xfs_bmbt_update(cur
, &got
);
5892 logflags
|= XFS_ILOG_DEXT
;
5894 /* Add new extent */
5895 xfs_iext_next(ifp
, &icur
);
5896 xfs_iext_insert(ip
, &icur
, &new, 0);
5897 XFS_IFORK_NEXT_SET(ip
, whichfork
,
5898 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
5901 error
= xfs_bmbt_lookup_eq(cur
, &new, &i
);
5904 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, del_cursor
);
5905 error
= xfs_btree_insert(cur
, &i
);
5908 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, del_cursor
);
5912 * Convert to a btree if necessary.
5914 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
5915 int tmp_logflags
; /* partial log flag return val */
5917 ASSERT(cur
== NULL
);
5918 error
= xfs_bmap_extents_to_btree(tp
, ip
, &cur
, 0,
5919 &tmp_logflags
, whichfork
);
5920 logflags
|= tmp_logflags
;
5925 cur
->bc_private
.b
.allocated
= 0;
5926 xfs_btree_del_cursor(cur
, error
);
5930 xfs_trans_log_inode(tp
, ip
, logflags
);
5935 xfs_bmap_split_extent(
5936 struct xfs_inode
*ip
,
5937 xfs_fileoff_t split_fsb
)
5939 struct xfs_mount
*mp
= ip
->i_mount
;
5940 struct xfs_trans
*tp
;
5943 error
= xfs_trans_alloc(mp
, &M_RES(mp
)->tr_write
,
5944 XFS_DIOSTRAT_SPACE_RES(mp
, 0), 0, 0, &tp
);
5948 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
5949 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
);
5951 error
= xfs_bmap_split_extent_at(tp
, ip
, split_fsb
);
5955 return xfs_trans_commit(tp
);
5958 xfs_trans_cancel(tp
);
5962 /* Deferred mapping is only for real extents in the data fork. */
5964 xfs_bmap_is_update_needed(
5965 struct xfs_bmbt_irec
*bmap
)
5967 return bmap
->br_startblock
!= HOLESTARTBLOCK
&&
5968 bmap
->br_startblock
!= DELAYSTARTBLOCK
;
5971 /* Record a bmap intent. */
5974 struct xfs_mount
*mp
,
5975 struct xfs_defer_ops
*dfops
,
5976 enum xfs_bmap_intent_type type
,
5977 struct xfs_inode
*ip
,
5979 struct xfs_bmbt_irec
*bmap
)
5982 struct xfs_bmap_intent
*bi
;
5984 trace_xfs_bmap_defer(mp
,
5985 XFS_FSB_TO_AGNO(mp
, bmap
->br_startblock
),
5987 XFS_FSB_TO_AGBNO(mp
, bmap
->br_startblock
),
5988 ip
->i_ino
, whichfork
,
5990 bmap
->br_blockcount
,
5993 bi
= kmem_alloc(sizeof(struct xfs_bmap_intent
), KM_SLEEP
| KM_NOFS
);
5994 INIT_LIST_HEAD(&bi
->bi_list
);
5997 bi
->bi_whichfork
= whichfork
;
5998 bi
->bi_bmap
= *bmap
;
6000 error
= xfs_defer_ijoin(dfops
, bi
->bi_owner
);
6006 xfs_defer_add(dfops
, XFS_DEFER_OPS_TYPE_BMAP
, &bi
->bi_list
);
6010 /* Map an extent into a file. */
6012 xfs_bmap_map_extent(
6013 struct xfs_mount
*mp
,
6014 struct xfs_defer_ops
*dfops
,
6015 struct xfs_inode
*ip
,
6016 struct xfs_bmbt_irec
*PREV
)
6018 if (!xfs_bmap_is_update_needed(PREV
))
6021 return __xfs_bmap_add(mp
, dfops
, XFS_BMAP_MAP
, ip
,
6022 XFS_DATA_FORK
, PREV
);
6025 /* Unmap an extent out of a file. */
6027 xfs_bmap_unmap_extent(
6028 struct xfs_mount
*mp
,
6029 struct xfs_defer_ops
*dfops
,
6030 struct xfs_inode
*ip
,
6031 struct xfs_bmbt_irec
*PREV
)
6033 if (!xfs_bmap_is_update_needed(PREV
))
6036 return __xfs_bmap_add(mp
, dfops
, XFS_BMAP_UNMAP
, ip
,
6037 XFS_DATA_FORK
, PREV
);
6041 * Process one of the deferred bmap operations. We pass back the
6042 * btree cursor to maintain our lock on the bmapbt between calls.
6045 xfs_bmap_finish_one(
6046 struct xfs_trans
*tp
,
6047 struct xfs_defer_ops
*dfops
,
6048 struct xfs_inode
*ip
,
6049 enum xfs_bmap_intent_type type
,
6051 xfs_fileoff_t startoff
,
6052 xfs_fsblock_t startblock
,
6053 xfs_filblks_t
*blockcount
,
6058 ASSERT(tp
->t_firstblock
== NULLFSBLOCK
);
6060 trace_xfs_bmap_deferred(tp
->t_mountp
,
6061 XFS_FSB_TO_AGNO(tp
->t_mountp
, startblock
), type
,
6062 XFS_FSB_TO_AGBNO(tp
->t_mountp
, startblock
),
6063 ip
->i_ino
, whichfork
, startoff
, *blockcount
, state
);
6065 if (WARN_ON_ONCE(whichfork
!= XFS_DATA_FORK
))
6066 return -EFSCORRUPTED
;
6068 if (XFS_TEST_ERROR(false, tp
->t_mountp
,
6069 XFS_ERRTAG_BMAP_FINISH_ONE
))
6074 ASSERT(dfops
== tp
->t_dfops
);
6075 error
= xfs_bmapi_remap(tp
, ip
, startoff
, *blockcount
,
6079 case XFS_BMAP_UNMAP
:
6080 error
= __xfs_bunmapi(tp
, ip
, startoff
, blockcount
,
6081 XFS_BMAPI_REMAP
, 1);
6085 error
= -EFSCORRUPTED
;
6091 /* Check that an inode's extent does not have invalid flags or bad ranges. */
6093 xfs_bmap_validate_extent(
6094 struct xfs_inode
*ip
,
6096 struct xfs_bmbt_irec
*irec
)
6098 struct xfs_mount
*mp
= ip
->i_mount
;
6099 xfs_fsblock_t endfsb
;
6102 isrt
= XFS_IS_REALTIME_INODE(ip
);
6103 endfsb
= irec
->br_startblock
+ irec
->br_blockcount
- 1;
6105 if (!xfs_verify_rtbno(mp
, irec
->br_startblock
))
6106 return __this_address
;
6107 if (!xfs_verify_rtbno(mp
, endfsb
))
6108 return __this_address
;
6110 if (!xfs_verify_fsbno(mp
, irec
->br_startblock
))
6111 return __this_address
;
6112 if (!xfs_verify_fsbno(mp
, endfsb
))
6113 return __this_address
;
6114 if (XFS_FSB_TO_AGNO(mp
, irec
->br_startblock
) !=
6115 XFS_FSB_TO_AGNO(mp
, endfsb
))
6116 return __this_address
;
6118 if (irec
->br_state
!= XFS_EXT_NORM
) {
6119 if (whichfork
!= XFS_DATA_FORK
)
6120 return __this_address
;
6121 if (!xfs_sb_version_hasextflgbit(&mp
->m_sb
))
6122 return __this_address
;