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_trans
*tp
,
530 struct xfs_owner_info
*oinfo
,
533 struct xfs_extent_free_item
*new; /* new element */
535 struct xfs_mount
*mp
= tp
->t_mountp
;
539 ASSERT(bno
!= NULLFSBLOCK
);
541 ASSERT(len
<= MAXEXTLEN
);
542 ASSERT(!isnullstartblock(bno
));
543 agno
= XFS_FSB_TO_AGNO(mp
, bno
);
544 agbno
= XFS_FSB_TO_AGBNO(mp
, bno
);
545 ASSERT(agno
< mp
->m_sb
.sb_agcount
);
546 ASSERT(agbno
< mp
->m_sb
.sb_agblocks
);
547 ASSERT(len
< mp
->m_sb
.sb_agblocks
);
548 ASSERT(agbno
+ len
<= mp
->m_sb
.sb_agblocks
);
550 ASSERT(xfs_bmap_free_item_zone
!= NULL
);
552 new = kmem_zone_alloc(xfs_bmap_free_item_zone
, 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(tp
->t_mountp
,
561 XFS_FSB_TO_AGNO(tp
->t_mountp
, bno
), 0,
562 XFS_FSB_TO_AGBNO(tp
->t_mountp
, bno
), len
);
563 xfs_defer_add(tp
, XFS_DEFER_OPS_TYPE_FREE
, &new->xefi_list
);
567 * Inode fork format manipulation functions
571 * Transform a btree format file with only one leaf node, where the
572 * extents list will fit in the inode, into an extents format file.
573 * Since the file extents are already in-core, all we have to do is
574 * give up the space for the btree root and pitch the leaf block.
576 STATIC
int /* error */
577 xfs_bmap_btree_to_extents(
578 xfs_trans_t
*tp
, /* transaction pointer */
579 xfs_inode_t
*ip
, /* incore inode pointer */
580 xfs_btree_cur_t
*cur
, /* btree cursor */
581 int *logflagsp
, /* inode logging flags */
582 int whichfork
) /* data or attr fork */
585 struct xfs_btree_block
*cblock
;/* child btree block */
586 xfs_fsblock_t cbno
; /* child block number */
587 xfs_buf_t
*cbp
; /* child block's buffer */
588 int error
; /* error return value */
589 struct xfs_ifork
*ifp
; /* inode fork data */
590 xfs_mount_t
*mp
; /* mount point structure */
591 __be64
*pp
; /* ptr to block address */
592 struct xfs_btree_block
*rblock
;/* root btree block */
593 struct xfs_owner_info oinfo
;
596 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
597 ASSERT(whichfork
!= XFS_COW_FORK
);
598 ASSERT(ifp
->if_flags
& XFS_IFEXTENTS
);
599 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
);
600 rblock
= ifp
->if_broot
;
601 ASSERT(be16_to_cpu(rblock
->bb_level
) == 1);
602 ASSERT(be16_to_cpu(rblock
->bb_numrecs
) == 1);
603 ASSERT(xfs_bmbt_maxrecs(mp
, ifp
->if_broot_bytes
, 0) == 1);
604 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, rblock
, 1, ifp
->if_broot_bytes
);
605 cbno
= be64_to_cpu(*pp
);
608 XFS_WANT_CORRUPTED_RETURN(cur
->bc_mp
,
609 xfs_btree_check_lptr(cur
, cbno
, 1));
611 error
= xfs_btree_read_bufl(mp
, tp
, cbno
, 0, &cbp
, XFS_BMAP_BTREE_REF
,
615 cblock
= XFS_BUF_TO_BLOCK(cbp
);
616 if ((error
= xfs_btree_check_block(cur
, cblock
, 0, cbp
)))
618 xfs_rmap_ino_bmbt_owner(&oinfo
, ip
->i_ino
, whichfork
);
619 xfs_bmap_add_free(cur
->bc_tp
, cbno
, 1, &oinfo
);
620 ip
->i_d
.di_nblocks
--;
621 xfs_trans_mod_dquot_byino(tp
, ip
, XFS_TRANS_DQ_BCOUNT
, -1L);
622 xfs_trans_binval(tp
, cbp
);
623 if (cur
->bc_bufs
[0] == cbp
)
624 cur
->bc_bufs
[0] = NULL
;
625 xfs_iroot_realloc(ip
, -1, whichfork
);
626 ASSERT(ifp
->if_broot
== NULL
);
627 ASSERT((ifp
->if_flags
& XFS_IFBROOT
) == 0);
628 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_EXTENTS
);
629 *logflagsp
= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
634 * Convert an extents-format file into a btree-format file.
635 * The new file will have a root block (in the inode) and a single child block.
637 STATIC
int /* error */
638 xfs_bmap_extents_to_btree(
639 struct xfs_trans
*tp
, /* transaction pointer */
640 struct xfs_inode
*ip
, /* incore inode pointer */
641 struct xfs_btree_cur
**curp
, /* cursor returned to caller */
642 int wasdel
, /* converting a delayed alloc */
643 int *logflagsp
, /* inode logging flags */
644 int whichfork
) /* data or attr fork */
646 struct xfs_btree_block
*ablock
; /* allocated (child) bt block */
647 struct xfs_buf
*abp
; /* buffer for ablock */
648 struct xfs_alloc_arg args
; /* allocation arguments */
649 struct xfs_bmbt_rec
*arp
; /* child record pointer */
650 struct xfs_btree_block
*block
; /* btree root block */
651 struct xfs_btree_cur
*cur
; /* bmap btree cursor */
652 int error
; /* error return value */
653 struct xfs_ifork
*ifp
; /* inode fork pointer */
654 struct xfs_bmbt_key
*kp
; /* root block key pointer */
655 struct xfs_mount
*mp
; /* mount structure */
656 xfs_bmbt_ptr_t
*pp
; /* root block address pointer */
657 struct xfs_iext_cursor icur
;
658 struct xfs_bmbt_irec rec
;
659 xfs_extnum_t cnt
= 0;
662 ASSERT(whichfork
!= XFS_COW_FORK
);
663 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
664 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
);
667 * Make space in the inode incore. This needs to be undone if we fail
668 * to expand the root.
670 xfs_iroot_realloc(ip
, 1, whichfork
);
671 ifp
->if_flags
|= XFS_IFBROOT
;
676 block
= ifp
->if_broot
;
677 xfs_btree_init_block_int(mp
, block
, XFS_BUF_DADDR_NULL
,
678 XFS_BTNUM_BMAP
, 1, 1, ip
->i_ino
,
679 XFS_BTREE_LONG_PTRS
);
681 * Need a cursor. Can't allocate until bb_level is filled in.
683 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
684 cur
->bc_private
.b
.flags
= wasdel
? XFS_BTCUR_BPRV_WASDEL
: 0;
686 * Convert to a btree with two levels, one record in root.
688 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_BTREE
);
689 memset(&args
, 0, sizeof(args
));
692 xfs_rmap_ino_bmbt_owner(&args
.oinfo
, ip
->i_ino
, whichfork
);
693 if (tp
->t_firstblock
== NULLFSBLOCK
) {
694 args
.type
= XFS_ALLOCTYPE_START_BNO
;
695 args
.fsbno
= XFS_INO_TO_FSB(mp
, ip
->i_ino
);
696 } else if (tp
->t_flags
& XFS_TRANS_LOWMODE
) {
697 args
.type
= XFS_ALLOCTYPE_START_BNO
;
698 args
.fsbno
= tp
->t_firstblock
;
700 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
701 args
.fsbno
= tp
->t_firstblock
;
703 args
.minlen
= args
.maxlen
= args
.prod
= 1;
704 args
.wasdel
= wasdel
;
706 error
= xfs_alloc_vextent(&args
);
708 goto out_root_realloc
;
710 if (WARN_ON_ONCE(args
.fsbno
== NULLFSBLOCK
)) {
712 goto out_root_realloc
;
716 * Allocation can't fail, the space was reserved.
718 ASSERT(tp
->t_firstblock
== NULLFSBLOCK
||
719 args
.agno
>= XFS_FSB_TO_AGNO(mp
, tp
->t_firstblock
));
720 tp
->t_firstblock
= args
.fsbno
;
721 cur
->bc_private
.b
.allocated
++;
722 ip
->i_d
.di_nblocks
++;
723 xfs_trans_mod_dquot_byino(tp
, ip
, XFS_TRANS_DQ_BCOUNT
, 1L);
724 abp
= xfs_btree_get_bufl(mp
, tp
, args
.fsbno
, 0);
726 error
= -EFSCORRUPTED
;
727 goto out_unreserve_dquot
;
731 * Fill in the child block.
733 abp
->b_ops
= &xfs_bmbt_buf_ops
;
734 ablock
= XFS_BUF_TO_BLOCK(abp
);
735 xfs_btree_init_block_int(mp
, ablock
, abp
->b_bn
,
736 XFS_BTNUM_BMAP
, 0, 0, ip
->i_ino
,
737 XFS_BTREE_LONG_PTRS
);
739 for_each_xfs_iext(ifp
, &icur
, &rec
) {
740 if (isnullstartblock(rec
.br_startblock
))
742 arp
= XFS_BMBT_REC_ADDR(mp
, ablock
, 1 + cnt
);
743 xfs_bmbt_disk_set_all(arp
, &rec
);
746 ASSERT(cnt
== XFS_IFORK_NEXTENTS(ip
, whichfork
));
747 xfs_btree_set_numrecs(ablock
, cnt
);
750 * Fill in the root key and pointer.
752 kp
= XFS_BMBT_KEY_ADDR(mp
, block
, 1);
753 arp
= XFS_BMBT_REC_ADDR(mp
, ablock
, 1);
754 kp
->br_startoff
= cpu_to_be64(xfs_bmbt_disk_get_startoff(arp
));
755 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, xfs_bmbt_get_maxrecs(cur
,
756 be16_to_cpu(block
->bb_level
)));
757 *pp
= cpu_to_be64(args
.fsbno
);
760 * Do all this logging at the end so that
761 * the root is at the right level.
763 xfs_btree_log_block(cur
, abp
, XFS_BB_ALL_BITS
);
764 xfs_btree_log_recs(cur
, abp
, 1, be16_to_cpu(ablock
->bb_numrecs
));
765 ASSERT(*curp
== NULL
);
767 *logflagsp
= XFS_ILOG_CORE
| xfs_ilog_fbroot(whichfork
);
771 xfs_trans_mod_dquot_byino(tp
, ip
, XFS_TRANS_DQ_BCOUNT
, -1L);
773 xfs_iroot_realloc(ip
, -1, whichfork
);
774 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_EXTENTS
);
775 ASSERT(ifp
->if_broot
== NULL
);
776 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
782 * Convert a local file to an extents file.
783 * This code is out of bounds for data forks of regular files,
784 * since the file data needs to get logged so things will stay consistent.
785 * (The bmap-level manipulations are ok, though).
788 xfs_bmap_local_to_extents_empty(
789 struct xfs_inode
*ip
,
792 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
794 ASSERT(whichfork
!= XFS_COW_FORK
);
795 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
796 ASSERT(ifp
->if_bytes
== 0);
797 ASSERT(XFS_IFORK_NEXTENTS(ip
, whichfork
) == 0);
799 xfs_bmap_forkoff_reset(ip
, whichfork
);
800 ifp
->if_flags
&= ~XFS_IFINLINE
;
801 ifp
->if_flags
|= XFS_IFEXTENTS
;
802 ifp
->if_u1
.if_root
= NULL
;
804 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_EXTENTS
);
808 STATIC
int /* error */
809 xfs_bmap_local_to_extents(
810 xfs_trans_t
*tp
, /* transaction pointer */
811 xfs_inode_t
*ip
, /* incore inode pointer */
812 xfs_extlen_t total
, /* total blocks needed by transaction */
813 int *logflagsp
, /* inode logging flags */
815 void (*init_fn
)(struct xfs_trans
*tp
,
817 struct xfs_inode
*ip
,
818 struct xfs_ifork
*ifp
))
821 int flags
; /* logging flags returned */
822 struct xfs_ifork
*ifp
; /* inode fork pointer */
823 xfs_alloc_arg_t args
; /* allocation arguments */
824 xfs_buf_t
*bp
; /* buffer for extent block */
825 struct xfs_bmbt_irec rec
;
826 struct xfs_iext_cursor icur
;
829 * We don't want to deal with the case of keeping inode data inline yet.
830 * So sending the data fork of a regular inode is invalid.
832 ASSERT(!(S_ISREG(VFS_I(ip
)->i_mode
) && whichfork
== XFS_DATA_FORK
));
833 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
834 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
836 if (!ifp
->if_bytes
) {
837 xfs_bmap_local_to_extents_empty(ip
, whichfork
);
838 flags
= XFS_ILOG_CORE
;
844 ASSERT((ifp
->if_flags
& (XFS_IFINLINE
|XFS_IFEXTENTS
)) == XFS_IFINLINE
);
845 memset(&args
, 0, sizeof(args
));
847 args
.mp
= ip
->i_mount
;
848 xfs_rmap_ino_owner(&args
.oinfo
, ip
->i_ino
, whichfork
, 0);
850 * Allocate a block. We know we need only one, since the
851 * file currently fits in an inode.
853 if (tp
->t_firstblock
== NULLFSBLOCK
) {
854 args
.fsbno
= XFS_INO_TO_FSB(args
.mp
, ip
->i_ino
);
855 args
.type
= XFS_ALLOCTYPE_START_BNO
;
857 args
.fsbno
= tp
->t_firstblock
;
858 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
861 args
.minlen
= args
.maxlen
= args
.prod
= 1;
862 error
= xfs_alloc_vextent(&args
);
866 /* Can't fail, the space was reserved. */
867 ASSERT(args
.fsbno
!= NULLFSBLOCK
);
868 ASSERT(args
.len
== 1);
869 tp
->t_firstblock
= args
.fsbno
;
870 bp
= xfs_btree_get_bufl(args
.mp
, tp
, args
.fsbno
, 0);
873 * Initialize the block, copy the data and log the remote buffer.
875 * The callout is responsible for logging because the remote format
876 * might differ from the local format and thus we don't know how much to
877 * log here. Note that init_fn must also set the buffer log item type
880 init_fn(tp
, bp
, ip
, ifp
);
882 /* account for the change in fork size */
883 xfs_idata_realloc(ip
, -ifp
->if_bytes
, whichfork
);
884 xfs_bmap_local_to_extents_empty(ip
, whichfork
);
885 flags
|= XFS_ILOG_CORE
;
887 ifp
->if_u1
.if_root
= NULL
;
891 rec
.br_startblock
= args
.fsbno
;
892 rec
.br_blockcount
= 1;
893 rec
.br_state
= XFS_EXT_NORM
;
894 xfs_iext_first(ifp
, &icur
);
895 xfs_iext_insert(ip
, &icur
, &rec
, 0);
897 XFS_IFORK_NEXT_SET(ip
, whichfork
, 1);
898 ip
->i_d
.di_nblocks
= 1;
899 xfs_trans_mod_dquot_byino(tp
, ip
,
900 XFS_TRANS_DQ_BCOUNT
, 1L);
901 flags
|= xfs_ilog_fext(whichfork
);
909 * Called from xfs_bmap_add_attrfork to handle btree format files.
911 STATIC
int /* error */
912 xfs_bmap_add_attrfork_btree(
913 xfs_trans_t
*tp
, /* transaction pointer */
914 xfs_inode_t
*ip
, /* incore inode pointer */
915 int *flags
) /* inode logging flags */
917 xfs_btree_cur_t
*cur
; /* btree cursor */
918 int error
; /* error return value */
919 xfs_mount_t
*mp
; /* file system mount struct */
920 int stat
; /* newroot status */
923 if (ip
->i_df
.if_broot_bytes
<= XFS_IFORK_DSIZE(ip
))
924 *flags
|= XFS_ILOG_DBROOT
;
926 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, XFS_DATA_FORK
);
927 error
= xfs_bmbt_lookup_first(cur
, &stat
);
930 /* must be at least one entry */
931 XFS_WANT_CORRUPTED_GOTO(mp
, stat
== 1, error0
);
932 if ((error
= xfs_btree_new_iroot(cur
, flags
, &stat
)))
935 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
938 cur
->bc_private
.b
.allocated
= 0;
939 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
943 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
948 * Called from xfs_bmap_add_attrfork to handle extents format files.
950 STATIC
int /* error */
951 xfs_bmap_add_attrfork_extents(
952 struct xfs_trans
*tp
, /* transaction pointer */
953 struct xfs_inode
*ip
, /* incore inode pointer */
954 int *flags
) /* inode logging flags */
956 xfs_btree_cur_t
*cur
; /* bmap btree cursor */
957 int error
; /* error return value */
959 if (ip
->i_d
.di_nextents
* sizeof(xfs_bmbt_rec_t
) <= XFS_IFORK_DSIZE(ip
))
962 error
= xfs_bmap_extents_to_btree(tp
, ip
, &cur
, 0, flags
,
965 cur
->bc_private
.b
.allocated
= 0;
966 xfs_btree_del_cursor(cur
, error
);
972 * Called from xfs_bmap_add_attrfork to handle local format files. Each
973 * different data fork content type needs a different callout to do the
974 * conversion. Some are basic and only require special block initialisation
975 * callouts for the data formating, others (directories) are so specialised they
976 * handle everything themselves.
978 * XXX (dgc): investigate whether directory conversion can use the generic
979 * formatting callout. It should be possible - it's just a very complex
982 STATIC
int /* error */
983 xfs_bmap_add_attrfork_local(
984 struct xfs_trans
*tp
, /* transaction pointer */
985 struct xfs_inode
*ip
, /* incore inode pointer */
986 int *flags
) /* inode logging flags */
988 struct xfs_da_args dargs
; /* args for dir/attr code */
990 if (ip
->i_df
.if_bytes
<= XFS_IFORK_DSIZE(ip
))
993 if (S_ISDIR(VFS_I(ip
)->i_mode
)) {
994 memset(&dargs
, 0, sizeof(dargs
));
995 dargs
.geo
= ip
->i_mount
->m_dir_geo
;
997 dargs
.total
= dargs
.geo
->fsbcount
;
998 dargs
.whichfork
= XFS_DATA_FORK
;
1000 return xfs_dir2_sf_to_block(&dargs
);
1003 if (S_ISLNK(VFS_I(ip
)->i_mode
))
1004 return xfs_bmap_local_to_extents(tp
, ip
, 1, flags
,
1006 xfs_symlink_local_to_remote
);
1008 /* should only be called for types that support local format data */
1010 return -EFSCORRUPTED
;
1013 /* Set an inode attr fork off based on the format */
1015 xfs_bmap_set_attrforkoff(
1016 struct xfs_inode
*ip
,
1020 switch (ip
->i_d
.di_format
) {
1021 case XFS_DINODE_FMT_DEV
:
1022 ip
->i_d
.di_forkoff
= roundup(sizeof(xfs_dev_t
), 8) >> 3;
1024 case XFS_DINODE_FMT_LOCAL
:
1025 case XFS_DINODE_FMT_EXTENTS
:
1026 case XFS_DINODE_FMT_BTREE
:
1027 ip
->i_d
.di_forkoff
= xfs_attr_shortform_bytesfit(ip
, size
);
1028 if (!ip
->i_d
.di_forkoff
)
1029 ip
->i_d
.di_forkoff
= xfs_default_attroffset(ip
) >> 3;
1030 else if ((ip
->i_mount
->m_flags
& XFS_MOUNT_ATTR2
) && version
)
1042 * Convert inode from non-attributed to attributed.
1043 * Must not be in a transaction, ip must not be locked.
1045 int /* error code */
1046 xfs_bmap_add_attrfork(
1047 xfs_inode_t
*ip
, /* incore inode pointer */
1048 int size
, /* space new attribute needs */
1049 int rsvd
) /* xact may use reserved blks */
1051 xfs_mount_t
*mp
; /* mount structure */
1052 xfs_trans_t
*tp
; /* transaction pointer */
1053 int blks
; /* space reservation */
1054 int version
= 1; /* superblock attr version */
1055 int logflags
; /* logging flags */
1056 int error
; /* error return value */
1058 ASSERT(XFS_IFORK_Q(ip
) == 0);
1061 ASSERT(!XFS_NOT_DQATTACHED(mp
, ip
));
1063 blks
= XFS_ADDAFORK_SPACE_RES(mp
);
1065 error
= xfs_trans_alloc(mp
, &M_RES(mp
)->tr_addafork
, blks
, 0,
1066 rsvd
? XFS_TRANS_RESERVE
: 0, &tp
);
1070 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
1071 error
= xfs_trans_reserve_quota_nblks(tp
, ip
, blks
, 0, rsvd
?
1072 XFS_QMOPT_RES_REGBLKS
| XFS_QMOPT_FORCE_RES
:
1073 XFS_QMOPT_RES_REGBLKS
);
1076 if (XFS_IFORK_Q(ip
))
1078 if (ip
->i_d
.di_anextents
!= 0) {
1079 error
= -EFSCORRUPTED
;
1082 if (ip
->i_d
.di_aformat
!= XFS_DINODE_FMT_EXTENTS
) {
1084 * For inodes coming from pre-6.2 filesystems.
1086 ASSERT(ip
->i_d
.di_aformat
== 0);
1087 ip
->i_d
.di_aformat
= XFS_DINODE_FMT_EXTENTS
;
1090 xfs_trans_ijoin(tp
, ip
, 0);
1091 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
1092 error
= xfs_bmap_set_attrforkoff(ip
, size
, &version
);
1095 ASSERT(ip
->i_afp
== NULL
);
1096 ip
->i_afp
= kmem_zone_zalloc(xfs_ifork_zone
, KM_SLEEP
);
1097 ip
->i_afp
->if_flags
= XFS_IFEXTENTS
;
1099 switch (ip
->i_d
.di_format
) {
1100 case XFS_DINODE_FMT_LOCAL
:
1101 error
= xfs_bmap_add_attrfork_local(tp
, ip
, &logflags
);
1103 case XFS_DINODE_FMT_EXTENTS
:
1104 error
= xfs_bmap_add_attrfork_extents(tp
, ip
, &logflags
);
1106 case XFS_DINODE_FMT_BTREE
:
1107 error
= xfs_bmap_add_attrfork_btree(tp
, ip
, &logflags
);
1114 xfs_trans_log_inode(tp
, ip
, logflags
);
1117 if (!xfs_sb_version_hasattr(&mp
->m_sb
) ||
1118 (!xfs_sb_version_hasattr2(&mp
->m_sb
) && version
== 2)) {
1119 bool log_sb
= false;
1121 spin_lock(&mp
->m_sb_lock
);
1122 if (!xfs_sb_version_hasattr(&mp
->m_sb
)) {
1123 xfs_sb_version_addattr(&mp
->m_sb
);
1126 if (!xfs_sb_version_hasattr2(&mp
->m_sb
) && version
== 2) {
1127 xfs_sb_version_addattr2(&mp
->m_sb
);
1130 spin_unlock(&mp
->m_sb_lock
);
1135 error
= xfs_trans_commit(tp
);
1136 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
1140 xfs_trans_cancel(tp
);
1141 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
1146 * Internal and external extent tree search functions.
1150 * Read in extents from a btree-format inode.
1154 struct xfs_trans
*tp
,
1155 struct xfs_inode
*ip
,
1158 struct xfs_mount
*mp
= ip
->i_mount
;
1159 int state
= xfs_bmap_fork_to_state(whichfork
);
1160 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1161 xfs_extnum_t nextents
= XFS_IFORK_NEXTENTS(ip
, whichfork
);
1162 struct xfs_btree_block
*block
= ifp
->if_broot
;
1163 struct xfs_iext_cursor icur
;
1164 struct xfs_bmbt_irec
new;
1172 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
1174 if (unlikely(XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)) {
1175 XFS_ERROR_REPORT(__func__
, XFS_ERRLEVEL_LOW
, mp
);
1176 return -EFSCORRUPTED
;
1180 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
1182 level
= be16_to_cpu(block
->bb_level
);
1184 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, 1, ifp
->if_broot_bytes
);
1185 bno
= be64_to_cpu(*pp
);
1188 * Go down the tree until leaf level is reached, following the first
1189 * pointer (leftmost) at each level.
1191 while (level
-- > 0) {
1192 error
= xfs_btree_read_bufl(mp
, tp
, bno
, 0, &bp
,
1193 XFS_BMAP_BTREE_REF
, &xfs_bmbt_buf_ops
);
1196 block
= XFS_BUF_TO_BLOCK(bp
);
1199 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, mp
->m_bmap_dmxr
[1]);
1200 bno
= be64_to_cpu(*pp
);
1201 XFS_WANT_CORRUPTED_GOTO(mp
,
1202 xfs_verify_fsbno(mp
, bno
), out_brelse
);
1203 xfs_trans_brelse(tp
, bp
);
1207 * Here with bp and block set to the leftmost leaf node in the tree.
1210 xfs_iext_first(ifp
, &icur
);
1213 * Loop over all leaf nodes. Copy information to the extent records.
1216 xfs_bmbt_rec_t
*frp
;
1217 xfs_fsblock_t nextbno
;
1218 xfs_extnum_t num_recs
;
1220 num_recs
= xfs_btree_get_numrecs(block
);
1221 if (unlikely(i
+ num_recs
> nextents
)) {
1222 xfs_warn(ip
->i_mount
,
1223 "corrupt dinode %Lu, (btree extents).",
1224 (unsigned long long) ip
->i_ino
);
1225 xfs_inode_verifier_error(ip
, -EFSCORRUPTED
,
1226 __func__
, block
, sizeof(*block
),
1228 error
= -EFSCORRUPTED
;
1232 * Read-ahead the next leaf block, if any.
1234 nextbno
= be64_to_cpu(block
->bb_u
.l
.bb_rightsib
);
1235 if (nextbno
!= NULLFSBLOCK
)
1236 xfs_btree_reada_bufl(mp
, nextbno
, 1,
1239 * Copy records into the extent records.
1241 frp
= XFS_BMBT_REC_ADDR(mp
, block
, 1);
1242 for (j
= 0; j
< num_recs
; j
++, frp
++, i
++) {
1245 xfs_bmbt_disk_get_all(frp
, &new);
1246 fa
= xfs_bmap_validate_extent(ip
, whichfork
, &new);
1248 error
= -EFSCORRUPTED
;
1249 xfs_inode_verifier_error(ip
, error
,
1250 "xfs_iread_extents(2)",
1251 frp
, sizeof(*frp
), fa
);
1254 xfs_iext_insert(ip
, &icur
, &new, state
);
1255 trace_xfs_read_extent(ip
, &icur
, state
, _THIS_IP_
);
1256 xfs_iext_next(ifp
, &icur
);
1258 xfs_trans_brelse(tp
, bp
);
1261 * If we've reached the end, stop.
1263 if (bno
== NULLFSBLOCK
)
1265 error
= xfs_btree_read_bufl(mp
, tp
, bno
, 0, &bp
,
1266 XFS_BMAP_BTREE_REF
, &xfs_bmbt_buf_ops
);
1269 block
= XFS_BUF_TO_BLOCK(bp
);
1272 if (i
!= XFS_IFORK_NEXTENTS(ip
, whichfork
)) {
1273 error
= -EFSCORRUPTED
;
1276 ASSERT(i
== xfs_iext_count(ifp
));
1278 ifp
->if_flags
|= XFS_IFEXTENTS
;
1282 xfs_trans_brelse(tp
, bp
);
1284 xfs_iext_destroy(ifp
);
1289 * Returns the relative block number of the first unused block(s) in the given
1290 * fork with at least "len" logically contiguous blocks free. This is the
1291 * lowest-address hole if the fork has holes, else the first block past the end
1292 * of fork. Return 0 if the fork is currently local (in-inode).
1295 xfs_bmap_first_unused(
1296 struct xfs_trans
*tp
, /* transaction pointer */
1297 struct xfs_inode
*ip
, /* incore inode */
1298 xfs_extlen_t len
, /* size of hole to find */
1299 xfs_fileoff_t
*first_unused
, /* unused block */
1300 int whichfork
) /* data or attr fork */
1302 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1303 struct xfs_bmbt_irec got
;
1304 struct xfs_iext_cursor icur
;
1305 xfs_fileoff_t lastaddr
= 0;
1306 xfs_fileoff_t lowest
, max
;
1309 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
||
1310 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
||
1311 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
1313 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
) {
1318 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
1319 error
= xfs_iread_extents(tp
, ip
, whichfork
);
1324 lowest
= max
= *first_unused
;
1325 for_each_xfs_iext(ifp
, &icur
, &got
) {
1327 * See if the hole before this extent will work.
1329 if (got
.br_startoff
>= lowest
+ len
&&
1330 got
.br_startoff
- max
>= len
)
1332 lastaddr
= got
.br_startoff
+ got
.br_blockcount
;
1333 max
= XFS_FILEOFF_MAX(lastaddr
, lowest
);
1336 *first_unused
= max
;
1341 * Returns the file-relative block number of the last block - 1 before
1342 * last_block (input value) in the file.
1343 * This is not based on i_size, it is based on the extent records.
1344 * Returns 0 for local files, as they do not have extent records.
1347 xfs_bmap_last_before(
1348 struct xfs_trans
*tp
, /* transaction pointer */
1349 struct xfs_inode
*ip
, /* incore inode */
1350 xfs_fileoff_t
*last_block
, /* last block */
1351 int whichfork
) /* data or attr fork */
1353 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1354 struct xfs_bmbt_irec got
;
1355 struct xfs_iext_cursor icur
;
1358 switch (XFS_IFORK_FORMAT(ip
, whichfork
)) {
1359 case XFS_DINODE_FMT_LOCAL
:
1362 case XFS_DINODE_FMT_BTREE
:
1363 case XFS_DINODE_FMT_EXTENTS
:
1369 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
1370 error
= xfs_iread_extents(tp
, ip
, whichfork
);
1375 if (!xfs_iext_lookup_extent_before(ip
, ifp
, last_block
, &icur
, &got
))
1381 xfs_bmap_last_extent(
1382 struct xfs_trans
*tp
,
1383 struct xfs_inode
*ip
,
1385 struct xfs_bmbt_irec
*rec
,
1388 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1389 struct xfs_iext_cursor icur
;
1392 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
1393 error
= xfs_iread_extents(tp
, ip
, whichfork
);
1398 xfs_iext_last(ifp
, &icur
);
1399 if (!xfs_iext_get_extent(ifp
, &icur
, rec
))
1407 * Check the last inode extent to determine whether this allocation will result
1408 * in blocks being allocated at the end of the file. When we allocate new data
1409 * blocks at the end of the file which do not start at the previous data block,
1410 * we will try to align the new blocks at stripe unit boundaries.
1412 * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be
1413 * at, or past the EOF.
1417 struct xfs_bmalloca
*bma
,
1420 struct xfs_bmbt_irec rec
;
1425 error
= xfs_bmap_last_extent(NULL
, bma
->ip
, whichfork
, &rec
,
1436 * Check if we are allocation or past the last extent, or at least into
1437 * the last delayed allocated extent.
1439 bma
->aeof
= bma
->offset
>= rec
.br_startoff
+ rec
.br_blockcount
||
1440 (bma
->offset
>= rec
.br_startoff
&&
1441 isnullstartblock(rec
.br_startblock
));
1446 * Returns the file-relative block number of the first block past eof in
1447 * the file. This is not based on i_size, it is based on the extent records.
1448 * Returns 0 for local files, as they do not have extent records.
1451 xfs_bmap_last_offset(
1452 struct xfs_inode
*ip
,
1453 xfs_fileoff_t
*last_block
,
1456 struct xfs_bmbt_irec rec
;
1462 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
)
1465 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
&&
1466 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
1469 error
= xfs_bmap_last_extent(NULL
, ip
, whichfork
, &rec
, &is_empty
);
1470 if (error
|| is_empty
)
1473 *last_block
= rec
.br_startoff
+ rec
.br_blockcount
;
1478 * Returns whether the selected fork of the inode has exactly one
1479 * block or not. For the data fork we check this matches di_size,
1480 * implying the file's range is 0..bsize-1.
1482 int /* 1=>1 block, 0=>otherwise */
1484 xfs_inode_t
*ip
, /* incore inode */
1485 int whichfork
) /* data or attr fork */
1487 struct xfs_ifork
*ifp
; /* inode fork pointer */
1488 int rval
; /* return value */
1489 xfs_bmbt_irec_t s
; /* internal version of extent */
1490 struct xfs_iext_cursor icur
;
1493 if (whichfork
== XFS_DATA_FORK
)
1494 return XFS_ISIZE(ip
) == ip
->i_mount
->m_sb
.sb_blocksize
;
1496 if (XFS_IFORK_NEXTENTS(ip
, whichfork
) != 1)
1498 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
1500 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1501 ASSERT(ifp
->if_flags
& XFS_IFEXTENTS
);
1502 xfs_iext_first(ifp
, &icur
);
1503 xfs_iext_get_extent(ifp
, &icur
, &s
);
1504 rval
= s
.br_startoff
== 0 && s
.br_blockcount
== 1;
1505 if (rval
&& whichfork
== XFS_DATA_FORK
)
1506 ASSERT(XFS_ISIZE(ip
) == ip
->i_mount
->m_sb
.sb_blocksize
);
1511 * Extent tree manipulation functions used during allocation.
1515 * Convert a delayed allocation to a real allocation.
1517 STATIC
int /* error */
1518 xfs_bmap_add_extent_delay_real(
1519 struct xfs_bmalloca
*bma
,
1522 struct xfs_bmbt_irec
*new = &bma
->got
;
1523 int error
; /* error return value */
1524 int i
; /* temp state */
1525 struct xfs_ifork
*ifp
; /* inode fork pointer */
1526 xfs_fileoff_t new_endoff
; /* end offset of new entry */
1527 xfs_bmbt_irec_t r
[3]; /* neighbor extent entries */
1528 /* left is 0, right is 1, prev is 2 */
1529 int rval
=0; /* return value (logging flags) */
1530 int state
= xfs_bmap_fork_to_state(whichfork
);
1531 xfs_filblks_t da_new
; /* new count del alloc blocks used */
1532 xfs_filblks_t da_old
; /* old count del alloc blocks used */
1533 xfs_filblks_t temp
=0; /* value for da_new calculations */
1534 int tmp_rval
; /* partial logging flags */
1535 struct xfs_mount
*mp
;
1536 xfs_extnum_t
*nextents
;
1537 struct xfs_bmbt_irec old
;
1539 mp
= bma
->ip
->i_mount
;
1540 ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
1541 ASSERT(whichfork
!= XFS_ATTR_FORK
);
1542 nextents
= (whichfork
== XFS_COW_FORK
? &bma
->ip
->i_cnextents
:
1543 &bma
->ip
->i_d
.di_nextents
);
1545 ASSERT(!isnullstartblock(new->br_startblock
));
1547 (bma
->cur
->bc_private
.b
.flags
& XFS_BTCUR_BPRV_WASDEL
));
1549 XFS_STATS_INC(mp
, xs_add_exlist
);
1556 * Set up a bunch of variables to make the tests simpler.
1558 xfs_iext_get_extent(ifp
, &bma
->icur
, &PREV
);
1559 new_endoff
= new->br_startoff
+ new->br_blockcount
;
1560 ASSERT(isnullstartblock(PREV
.br_startblock
));
1561 ASSERT(PREV
.br_startoff
<= new->br_startoff
);
1562 ASSERT(PREV
.br_startoff
+ PREV
.br_blockcount
>= new_endoff
);
1564 da_old
= startblockval(PREV
.br_startblock
);
1568 * Set flags determining what part of the previous delayed allocation
1569 * extent is being replaced by a real allocation.
1571 if (PREV
.br_startoff
== new->br_startoff
)
1572 state
|= BMAP_LEFT_FILLING
;
1573 if (PREV
.br_startoff
+ PREV
.br_blockcount
== new_endoff
)
1574 state
|= BMAP_RIGHT_FILLING
;
1577 * Check and set flags if this segment has a left neighbor.
1578 * Don't set contiguous if the combined extent would be too large.
1580 if (xfs_iext_peek_prev_extent(ifp
, &bma
->icur
, &LEFT
)) {
1581 state
|= BMAP_LEFT_VALID
;
1582 if (isnullstartblock(LEFT
.br_startblock
))
1583 state
|= BMAP_LEFT_DELAY
;
1586 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
1587 LEFT
.br_startoff
+ LEFT
.br_blockcount
== new->br_startoff
&&
1588 LEFT
.br_startblock
+ LEFT
.br_blockcount
== new->br_startblock
&&
1589 LEFT
.br_state
== new->br_state
&&
1590 LEFT
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
1591 state
|= BMAP_LEFT_CONTIG
;
1594 * Check and set flags if this segment has a right neighbor.
1595 * Don't set contiguous if the combined extent would be too large.
1596 * Also check for all-three-contiguous being too large.
1598 if (xfs_iext_peek_next_extent(ifp
, &bma
->icur
, &RIGHT
)) {
1599 state
|= BMAP_RIGHT_VALID
;
1600 if (isnullstartblock(RIGHT
.br_startblock
))
1601 state
|= BMAP_RIGHT_DELAY
;
1604 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
1605 new_endoff
== RIGHT
.br_startoff
&&
1606 new->br_startblock
+ new->br_blockcount
== RIGHT
.br_startblock
&&
1607 new->br_state
== RIGHT
.br_state
&&
1608 new->br_blockcount
+ RIGHT
.br_blockcount
<= MAXEXTLEN
&&
1609 ((state
& (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
1610 BMAP_RIGHT_FILLING
)) !=
1611 (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
1612 BMAP_RIGHT_FILLING
) ||
1613 LEFT
.br_blockcount
+ new->br_blockcount
+ RIGHT
.br_blockcount
1615 state
|= BMAP_RIGHT_CONTIG
;
1619 * Switch out based on the FILLING and CONTIG state bits.
1621 switch (state
& (BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
1622 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
)) {
1623 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
1624 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
1626 * Filling in all of a previously delayed allocation extent.
1627 * The left and right neighbors are both contiguous with new.
1629 LEFT
.br_blockcount
+= PREV
.br_blockcount
+ RIGHT
.br_blockcount
;
1631 xfs_iext_remove(bma
->ip
, &bma
->icur
, state
);
1632 xfs_iext_remove(bma
->ip
, &bma
->icur
, state
);
1633 xfs_iext_prev(ifp
, &bma
->icur
);
1634 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &LEFT
);
1637 if (bma
->cur
== NULL
)
1638 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1640 rval
= XFS_ILOG_CORE
;
1641 error
= xfs_bmbt_lookup_eq(bma
->cur
, &RIGHT
, &i
);
1644 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1645 error
= xfs_btree_delete(bma
->cur
, &i
);
1648 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1649 error
= xfs_btree_decrement(bma
->cur
, 0, &i
);
1652 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1653 error
= xfs_bmbt_update(bma
->cur
, &LEFT
);
1659 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
1661 * Filling in all of a previously delayed allocation extent.
1662 * The left neighbor is contiguous, the right is not.
1665 LEFT
.br_blockcount
+= PREV
.br_blockcount
;
1667 xfs_iext_remove(bma
->ip
, &bma
->icur
, state
);
1668 xfs_iext_prev(ifp
, &bma
->icur
);
1669 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &LEFT
);
1671 if (bma
->cur
== NULL
)
1672 rval
= XFS_ILOG_DEXT
;
1675 error
= xfs_bmbt_lookup_eq(bma
->cur
, &old
, &i
);
1678 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1679 error
= xfs_bmbt_update(bma
->cur
, &LEFT
);
1685 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
1687 * Filling in all of a previously delayed allocation extent.
1688 * The right neighbor is contiguous, the left is not. Take care
1689 * with delay -> unwritten extent allocation here because the
1690 * delalloc record we are overwriting is always written.
1692 PREV
.br_startblock
= new->br_startblock
;
1693 PREV
.br_blockcount
+= RIGHT
.br_blockcount
;
1694 PREV
.br_state
= new->br_state
;
1696 xfs_iext_next(ifp
, &bma
->icur
);
1697 xfs_iext_remove(bma
->ip
, &bma
->icur
, state
);
1698 xfs_iext_prev(ifp
, &bma
->icur
);
1699 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &PREV
);
1701 if (bma
->cur
== NULL
)
1702 rval
= XFS_ILOG_DEXT
;
1705 error
= xfs_bmbt_lookup_eq(bma
->cur
, &RIGHT
, &i
);
1708 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1709 error
= xfs_bmbt_update(bma
->cur
, &PREV
);
1715 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
1717 * Filling in all of a previously delayed allocation extent.
1718 * Neither the left nor right neighbors are contiguous with
1721 PREV
.br_startblock
= new->br_startblock
;
1722 PREV
.br_state
= new->br_state
;
1723 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &PREV
);
1726 if (bma
->cur
== NULL
)
1727 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1729 rval
= XFS_ILOG_CORE
;
1730 error
= xfs_bmbt_lookup_eq(bma
->cur
, new, &i
);
1733 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
1734 error
= xfs_btree_insert(bma
->cur
, &i
);
1737 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1741 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
:
1743 * Filling in the first part of a previous delayed allocation.
1744 * The left neighbor is contiguous.
1747 temp
= PREV
.br_blockcount
- new->br_blockcount
;
1748 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
1749 startblockval(PREV
.br_startblock
));
1751 LEFT
.br_blockcount
+= new->br_blockcount
;
1753 PREV
.br_blockcount
= temp
;
1754 PREV
.br_startoff
+= new->br_blockcount
;
1755 PREV
.br_startblock
= nullstartblock(da_new
);
1757 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &PREV
);
1758 xfs_iext_prev(ifp
, &bma
->icur
);
1759 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &LEFT
);
1761 if (bma
->cur
== NULL
)
1762 rval
= XFS_ILOG_DEXT
;
1765 error
= xfs_bmbt_lookup_eq(bma
->cur
, &old
, &i
);
1768 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1769 error
= xfs_bmbt_update(bma
->cur
, &LEFT
);
1775 case BMAP_LEFT_FILLING
:
1777 * Filling in the first part of a previous delayed allocation.
1778 * The left neighbor is not contiguous.
1780 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, new);
1782 if (bma
->cur
== NULL
)
1783 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1785 rval
= XFS_ILOG_CORE
;
1786 error
= xfs_bmbt_lookup_eq(bma
->cur
, new, &i
);
1789 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
1790 error
= xfs_btree_insert(bma
->cur
, &i
);
1793 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1796 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
1797 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
1798 &bma
->cur
, 1, &tmp_rval
, whichfork
);
1804 temp
= PREV
.br_blockcount
- new->br_blockcount
;
1805 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
1806 startblockval(PREV
.br_startblock
) -
1807 (bma
->cur
? bma
->cur
->bc_private
.b
.allocated
: 0));
1809 PREV
.br_startoff
= new_endoff
;
1810 PREV
.br_blockcount
= temp
;
1811 PREV
.br_startblock
= nullstartblock(da_new
);
1812 xfs_iext_next(ifp
, &bma
->icur
);
1813 xfs_iext_insert(bma
->ip
, &bma
->icur
, &PREV
, state
);
1814 xfs_iext_prev(ifp
, &bma
->icur
);
1817 case BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
1819 * Filling in the last part of a previous delayed allocation.
1820 * The right neighbor is contiguous with the new allocation.
1823 RIGHT
.br_startoff
= new->br_startoff
;
1824 RIGHT
.br_startblock
= new->br_startblock
;
1825 RIGHT
.br_blockcount
+= new->br_blockcount
;
1827 if (bma
->cur
== NULL
)
1828 rval
= XFS_ILOG_DEXT
;
1831 error
= xfs_bmbt_lookup_eq(bma
->cur
, &old
, &i
);
1834 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1835 error
= xfs_bmbt_update(bma
->cur
, &RIGHT
);
1840 temp
= PREV
.br_blockcount
- new->br_blockcount
;
1841 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
1842 startblockval(PREV
.br_startblock
));
1844 PREV
.br_blockcount
= temp
;
1845 PREV
.br_startblock
= nullstartblock(da_new
);
1847 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &PREV
);
1848 xfs_iext_next(ifp
, &bma
->icur
);
1849 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &RIGHT
);
1852 case BMAP_RIGHT_FILLING
:
1854 * Filling in the last part of a previous delayed allocation.
1855 * The right neighbor is not contiguous.
1857 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, new);
1859 if (bma
->cur
== NULL
)
1860 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1862 rval
= XFS_ILOG_CORE
;
1863 error
= xfs_bmbt_lookup_eq(bma
->cur
, new, &i
);
1866 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
1867 error
= xfs_btree_insert(bma
->cur
, &i
);
1870 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1873 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
1874 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
1875 &bma
->cur
, 1, &tmp_rval
, whichfork
);
1881 temp
= PREV
.br_blockcount
- new->br_blockcount
;
1882 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
1883 startblockval(PREV
.br_startblock
) -
1884 (bma
->cur
? bma
->cur
->bc_private
.b
.allocated
: 0));
1886 PREV
.br_startblock
= nullstartblock(da_new
);
1887 PREV
.br_blockcount
= temp
;
1888 xfs_iext_insert(bma
->ip
, &bma
->icur
, &PREV
, state
);
1889 xfs_iext_next(ifp
, &bma
->icur
);
1894 * Filling in the middle part of a previous delayed allocation.
1895 * Contiguity is impossible here.
1896 * This case is avoided almost all the time.
1898 * We start with a delayed allocation:
1900 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
1903 * and we are allocating:
1904 * +rrrrrrrrrrrrrrrrr+
1907 * and we set it up for insertion as:
1908 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
1910 * PREV @ idx LEFT RIGHT
1911 * inserted at idx + 1
1915 /* LEFT is the new middle */
1918 /* RIGHT is the new right */
1919 RIGHT
.br_state
= PREV
.br_state
;
1920 RIGHT
.br_startoff
= new_endoff
;
1921 RIGHT
.br_blockcount
=
1922 PREV
.br_startoff
+ PREV
.br_blockcount
- new_endoff
;
1923 RIGHT
.br_startblock
=
1924 nullstartblock(xfs_bmap_worst_indlen(bma
->ip
,
1925 RIGHT
.br_blockcount
));
1928 PREV
.br_blockcount
= new->br_startoff
- PREV
.br_startoff
;
1929 PREV
.br_startblock
=
1930 nullstartblock(xfs_bmap_worst_indlen(bma
->ip
,
1931 PREV
.br_blockcount
));
1932 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &PREV
);
1934 xfs_iext_next(ifp
, &bma
->icur
);
1935 xfs_iext_insert(bma
->ip
, &bma
->icur
, &RIGHT
, state
);
1936 xfs_iext_insert(bma
->ip
, &bma
->icur
, &LEFT
, state
);
1939 if (bma
->cur
== NULL
)
1940 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1942 rval
= XFS_ILOG_CORE
;
1943 error
= xfs_bmbt_lookup_eq(bma
->cur
, new, &i
);
1946 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
1947 error
= xfs_btree_insert(bma
->cur
, &i
);
1950 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1953 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
1954 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
1955 &bma
->cur
, 1, &tmp_rval
, whichfork
);
1961 da_new
= startblockval(PREV
.br_startblock
) +
1962 startblockval(RIGHT
.br_startblock
);
1965 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
1966 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
1967 case BMAP_LEFT_FILLING
| BMAP_RIGHT_CONTIG
:
1968 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
1969 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
1970 case BMAP_LEFT_CONTIG
:
1971 case BMAP_RIGHT_CONTIG
:
1973 * These cases are all impossible.
1978 /* add reverse mapping unless caller opted out */
1979 if (!(bma
->flags
& XFS_BMAPI_NORMAP
)) {
1980 error
= xfs_rmap_map_extent(bma
->tp
, bma
->ip
, whichfork
, new);
1985 /* convert to a btree if necessary */
1986 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
1987 int tmp_logflags
; /* partial log flag return val */
1989 ASSERT(bma
->cur
== NULL
);
1990 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
1991 &bma
->cur
, da_old
> 0, &tmp_logflags
,
1993 bma
->logflags
|= tmp_logflags
;
1999 da_new
+= bma
->cur
->bc_private
.b
.allocated
;
2000 bma
->cur
->bc_private
.b
.allocated
= 0;
2003 /* adjust for changes in reserved delayed indirect blocks */
2004 if (da_new
!= da_old
) {
2005 ASSERT(state
== 0 || da_new
< da_old
);
2006 error
= xfs_mod_fdblocks(mp
, (int64_t)(da_old
- da_new
),
2010 xfs_bmap_check_leaf_extents(bma
->cur
, bma
->ip
, whichfork
);
2012 if (whichfork
!= XFS_COW_FORK
)
2013 bma
->logflags
|= rval
;
2021 * Convert an unwritten allocation to a real allocation or vice versa.
2023 STATIC
int /* error */
2024 xfs_bmap_add_extent_unwritten_real(
2025 struct xfs_trans
*tp
,
2026 xfs_inode_t
*ip
, /* incore inode pointer */
2028 struct xfs_iext_cursor
*icur
,
2029 xfs_btree_cur_t
**curp
, /* if *curp is null, not a btree */
2030 xfs_bmbt_irec_t
*new, /* new data to add to file extents */
2031 int *logflagsp
) /* inode logging flags */
2033 xfs_btree_cur_t
*cur
; /* btree cursor */
2034 int error
; /* error return value */
2035 int i
; /* temp state */
2036 struct xfs_ifork
*ifp
; /* inode fork pointer */
2037 xfs_fileoff_t new_endoff
; /* end offset of new entry */
2038 xfs_bmbt_irec_t r
[3]; /* neighbor extent entries */
2039 /* left is 0, right is 1, prev is 2 */
2040 int rval
=0; /* return value (logging flags) */
2041 int state
= xfs_bmap_fork_to_state(whichfork
);
2042 struct xfs_mount
*mp
= ip
->i_mount
;
2043 struct xfs_bmbt_irec old
;
2048 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
2050 ASSERT(!isnullstartblock(new->br_startblock
));
2052 XFS_STATS_INC(mp
, xs_add_exlist
);
2059 * Set up a bunch of variables to make the tests simpler.
2062 xfs_iext_get_extent(ifp
, icur
, &PREV
);
2063 ASSERT(new->br_state
!= PREV
.br_state
);
2064 new_endoff
= new->br_startoff
+ new->br_blockcount
;
2065 ASSERT(PREV
.br_startoff
<= new->br_startoff
);
2066 ASSERT(PREV
.br_startoff
+ PREV
.br_blockcount
>= new_endoff
);
2069 * Set flags determining what part of the previous oldext allocation
2070 * extent is being replaced by a newext allocation.
2072 if (PREV
.br_startoff
== new->br_startoff
)
2073 state
|= BMAP_LEFT_FILLING
;
2074 if (PREV
.br_startoff
+ PREV
.br_blockcount
== new_endoff
)
2075 state
|= BMAP_RIGHT_FILLING
;
2078 * Check and set flags if this segment has a left neighbor.
2079 * Don't set contiguous if the combined extent would be too large.
2081 if (xfs_iext_peek_prev_extent(ifp
, icur
, &LEFT
)) {
2082 state
|= BMAP_LEFT_VALID
;
2083 if (isnullstartblock(LEFT
.br_startblock
))
2084 state
|= BMAP_LEFT_DELAY
;
2087 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
2088 LEFT
.br_startoff
+ LEFT
.br_blockcount
== new->br_startoff
&&
2089 LEFT
.br_startblock
+ LEFT
.br_blockcount
== new->br_startblock
&&
2090 LEFT
.br_state
== new->br_state
&&
2091 LEFT
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
2092 state
|= BMAP_LEFT_CONTIG
;
2095 * Check and set flags if this segment has a right neighbor.
2096 * Don't set contiguous if the combined extent would be too large.
2097 * Also check for all-three-contiguous being too large.
2099 if (xfs_iext_peek_next_extent(ifp
, icur
, &RIGHT
)) {
2100 state
|= BMAP_RIGHT_VALID
;
2101 if (isnullstartblock(RIGHT
.br_startblock
))
2102 state
|= BMAP_RIGHT_DELAY
;
2105 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
2106 new_endoff
== RIGHT
.br_startoff
&&
2107 new->br_startblock
+ new->br_blockcount
== RIGHT
.br_startblock
&&
2108 new->br_state
== RIGHT
.br_state
&&
2109 new->br_blockcount
+ RIGHT
.br_blockcount
<= MAXEXTLEN
&&
2110 ((state
& (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
2111 BMAP_RIGHT_FILLING
)) !=
2112 (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
2113 BMAP_RIGHT_FILLING
) ||
2114 LEFT
.br_blockcount
+ new->br_blockcount
+ RIGHT
.br_blockcount
2116 state
|= BMAP_RIGHT_CONTIG
;
2119 * Switch out based on the FILLING and CONTIG state bits.
2121 switch (state
& (BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
2122 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
)) {
2123 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
2124 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2126 * Setting all of a previous oldext extent to newext.
2127 * The left and right neighbors are both contiguous with new.
2129 LEFT
.br_blockcount
+= PREV
.br_blockcount
+ RIGHT
.br_blockcount
;
2131 xfs_iext_remove(ip
, icur
, state
);
2132 xfs_iext_remove(ip
, icur
, state
);
2133 xfs_iext_prev(ifp
, icur
);
2134 xfs_iext_update_extent(ip
, state
, icur
, &LEFT
);
2135 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2136 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 2);
2138 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2140 rval
= XFS_ILOG_CORE
;
2141 error
= xfs_bmbt_lookup_eq(cur
, &RIGHT
, &i
);
2144 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2145 if ((error
= xfs_btree_delete(cur
, &i
)))
2147 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2148 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2150 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2151 if ((error
= xfs_btree_delete(cur
, &i
)))
2153 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2154 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2156 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2157 error
= xfs_bmbt_update(cur
, &LEFT
);
2163 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
2165 * Setting all of a previous oldext extent to newext.
2166 * The left neighbor is contiguous, the right is not.
2168 LEFT
.br_blockcount
+= PREV
.br_blockcount
;
2170 xfs_iext_remove(ip
, icur
, state
);
2171 xfs_iext_prev(ifp
, icur
);
2172 xfs_iext_update_extent(ip
, state
, icur
, &LEFT
);
2173 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2174 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
2176 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2178 rval
= XFS_ILOG_CORE
;
2179 error
= xfs_bmbt_lookup_eq(cur
, &PREV
, &i
);
2182 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2183 if ((error
= xfs_btree_delete(cur
, &i
)))
2185 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2186 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2188 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2189 error
= xfs_bmbt_update(cur
, &LEFT
);
2195 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2197 * Setting all of a previous oldext extent to newext.
2198 * The right neighbor is contiguous, the left is not.
2200 PREV
.br_blockcount
+= RIGHT
.br_blockcount
;
2201 PREV
.br_state
= new->br_state
;
2203 xfs_iext_next(ifp
, icur
);
2204 xfs_iext_remove(ip
, icur
, state
);
2205 xfs_iext_prev(ifp
, icur
);
2206 xfs_iext_update_extent(ip
, state
, icur
, &PREV
);
2208 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2209 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
2211 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2213 rval
= XFS_ILOG_CORE
;
2214 error
= xfs_bmbt_lookup_eq(cur
, &RIGHT
, &i
);
2217 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2218 if ((error
= xfs_btree_delete(cur
, &i
)))
2220 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2221 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2223 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2224 error
= xfs_bmbt_update(cur
, &PREV
);
2230 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
2232 * Setting all of a previous oldext extent to newext.
2233 * Neither the left nor right neighbors are contiguous with
2236 PREV
.br_state
= new->br_state
;
2237 xfs_iext_update_extent(ip
, state
, icur
, &PREV
);
2240 rval
= XFS_ILOG_DEXT
;
2243 error
= xfs_bmbt_lookup_eq(cur
, new, &i
);
2246 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2247 error
= xfs_bmbt_update(cur
, &PREV
);
2253 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
:
2255 * Setting the first part of a previous oldext extent to newext.
2256 * The left neighbor is contiguous.
2258 LEFT
.br_blockcount
+= new->br_blockcount
;
2261 PREV
.br_startoff
+= new->br_blockcount
;
2262 PREV
.br_startblock
+= new->br_blockcount
;
2263 PREV
.br_blockcount
-= new->br_blockcount
;
2265 xfs_iext_update_extent(ip
, state
, icur
, &PREV
);
2266 xfs_iext_prev(ifp
, icur
);
2267 xfs_iext_update_extent(ip
, state
, icur
, &LEFT
);
2270 rval
= XFS_ILOG_DEXT
;
2273 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2276 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2277 error
= xfs_bmbt_update(cur
, &PREV
);
2280 error
= xfs_btree_decrement(cur
, 0, &i
);
2283 error
= xfs_bmbt_update(cur
, &LEFT
);
2289 case BMAP_LEFT_FILLING
:
2291 * Setting the first part of a previous oldext extent to newext.
2292 * The left neighbor is not contiguous.
2295 PREV
.br_startoff
+= new->br_blockcount
;
2296 PREV
.br_startblock
+= new->br_blockcount
;
2297 PREV
.br_blockcount
-= new->br_blockcount
;
2299 xfs_iext_update_extent(ip
, state
, icur
, &PREV
);
2300 xfs_iext_insert(ip
, icur
, new, state
);
2301 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2302 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
2304 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2306 rval
= XFS_ILOG_CORE
;
2307 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2310 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2311 error
= xfs_bmbt_update(cur
, &PREV
);
2314 cur
->bc_rec
.b
= *new;
2315 if ((error
= xfs_btree_insert(cur
, &i
)))
2317 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2321 case BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2323 * Setting the last part of a previous oldext extent to newext.
2324 * The right neighbor is contiguous with the new allocation.
2327 PREV
.br_blockcount
-= new->br_blockcount
;
2329 RIGHT
.br_startoff
= new->br_startoff
;
2330 RIGHT
.br_startblock
= new->br_startblock
;
2331 RIGHT
.br_blockcount
+= new->br_blockcount
;
2333 xfs_iext_update_extent(ip
, state
, icur
, &PREV
);
2334 xfs_iext_next(ifp
, icur
);
2335 xfs_iext_update_extent(ip
, state
, icur
, &RIGHT
);
2338 rval
= XFS_ILOG_DEXT
;
2341 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2344 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2345 error
= xfs_bmbt_update(cur
, &PREV
);
2348 error
= xfs_btree_increment(cur
, 0, &i
);
2351 error
= xfs_bmbt_update(cur
, &RIGHT
);
2357 case BMAP_RIGHT_FILLING
:
2359 * Setting the last part of a previous oldext extent to newext.
2360 * The right neighbor is not contiguous.
2363 PREV
.br_blockcount
-= new->br_blockcount
;
2365 xfs_iext_update_extent(ip
, state
, icur
, &PREV
);
2366 xfs_iext_next(ifp
, icur
);
2367 xfs_iext_insert(ip
, icur
, new, state
);
2369 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2370 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
2372 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2374 rval
= XFS_ILOG_CORE
;
2375 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2378 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2379 error
= xfs_bmbt_update(cur
, &PREV
);
2382 error
= xfs_bmbt_lookup_eq(cur
, new, &i
);
2385 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
2386 if ((error
= xfs_btree_insert(cur
, &i
)))
2388 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2394 * Setting the middle part of a previous oldext extent to
2395 * newext. Contiguity is impossible here.
2396 * One extent becomes three extents.
2399 PREV
.br_blockcount
= new->br_startoff
- PREV
.br_startoff
;
2402 r
[1].br_startoff
= new_endoff
;
2403 r
[1].br_blockcount
=
2404 old
.br_startoff
+ old
.br_blockcount
- new_endoff
;
2405 r
[1].br_startblock
= new->br_startblock
+ new->br_blockcount
;
2406 r
[1].br_state
= PREV
.br_state
;
2408 xfs_iext_update_extent(ip
, state
, icur
, &PREV
);
2409 xfs_iext_next(ifp
, icur
);
2410 xfs_iext_insert(ip
, icur
, &r
[1], state
);
2411 xfs_iext_insert(ip
, icur
, &r
[0], state
);
2413 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2414 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 2);
2416 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2418 rval
= XFS_ILOG_CORE
;
2419 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2422 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2423 /* new right extent - oldext */
2424 error
= xfs_bmbt_update(cur
, &r
[1]);
2427 /* new left extent - oldext */
2428 cur
->bc_rec
.b
= PREV
;
2429 if ((error
= xfs_btree_insert(cur
, &i
)))
2431 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2433 * Reset the cursor to the position of the new extent
2434 * we are about to insert as we can't trust it after
2435 * the previous insert.
2437 error
= xfs_bmbt_lookup_eq(cur
, new, &i
);
2440 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
2441 /* new middle extent - newext */
2442 if ((error
= xfs_btree_insert(cur
, &i
)))
2444 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2448 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2449 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2450 case BMAP_LEFT_FILLING
| BMAP_RIGHT_CONTIG
:
2451 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
2452 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2453 case BMAP_LEFT_CONTIG
:
2454 case BMAP_RIGHT_CONTIG
:
2456 * These cases are all impossible.
2461 /* update reverse mappings */
2462 error
= xfs_rmap_convert_extent(mp
, tp
, ip
, whichfork
, new);
2466 /* convert to a btree if necessary */
2467 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
2468 int tmp_logflags
; /* partial log flag return val */
2470 ASSERT(cur
== NULL
);
2471 error
= xfs_bmap_extents_to_btree(tp
, ip
, &cur
, 0,
2472 &tmp_logflags
, whichfork
);
2473 *logflagsp
|= tmp_logflags
;
2478 /* clear out the allocated field, done with it now in any case. */
2480 cur
->bc_private
.b
.allocated
= 0;
2484 xfs_bmap_check_leaf_extents(*curp
, ip
, whichfork
);
2494 * Convert a hole to a delayed allocation.
2497 xfs_bmap_add_extent_hole_delay(
2498 xfs_inode_t
*ip
, /* incore inode pointer */
2500 struct xfs_iext_cursor
*icur
,
2501 xfs_bmbt_irec_t
*new) /* new data to add to file extents */
2503 struct xfs_ifork
*ifp
; /* inode fork pointer */
2504 xfs_bmbt_irec_t left
; /* left neighbor extent entry */
2505 xfs_filblks_t newlen
=0; /* new indirect size */
2506 xfs_filblks_t oldlen
=0; /* old indirect size */
2507 xfs_bmbt_irec_t right
; /* right neighbor extent entry */
2508 int state
= xfs_bmap_fork_to_state(whichfork
);
2509 xfs_filblks_t temp
; /* temp for indirect calculations */
2511 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
2512 ASSERT(isnullstartblock(new->br_startblock
));
2515 * Check and set flags if this segment has a left neighbor
2517 if (xfs_iext_peek_prev_extent(ifp
, icur
, &left
)) {
2518 state
|= BMAP_LEFT_VALID
;
2519 if (isnullstartblock(left
.br_startblock
))
2520 state
|= BMAP_LEFT_DELAY
;
2524 * Check and set flags if the current (right) segment exists.
2525 * If it doesn't exist, we're converting the hole at end-of-file.
2527 if (xfs_iext_get_extent(ifp
, icur
, &right
)) {
2528 state
|= BMAP_RIGHT_VALID
;
2529 if (isnullstartblock(right
.br_startblock
))
2530 state
|= BMAP_RIGHT_DELAY
;
2534 * Set contiguity flags on the left and right neighbors.
2535 * Don't let extents get too large, even if the pieces are contiguous.
2537 if ((state
& BMAP_LEFT_VALID
) && (state
& BMAP_LEFT_DELAY
) &&
2538 left
.br_startoff
+ left
.br_blockcount
== new->br_startoff
&&
2539 left
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
2540 state
|= BMAP_LEFT_CONTIG
;
2542 if ((state
& BMAP_RIGHT_VALID
) && (state
& BMAP_RIGHT_DELAY
) &&
2543 new->br_startoff
+ new->br_blockcount
== right
.br_startoff
&&
2544 new->br_blockcount
+ right
.br_blockcount
<= MAXEXTLEN
&&
2545 (!(state
& BMAP_LEFT_CONTIG
) ||
2546 (left
.br_blockcount
+ new->br_blockcount
+
2547 right
.br_blockcount
<= MAXEXTLEN
)))
2548 state
|= BMAP_RIGHT_CONTIG
;
2551 * Switch out based on the contiguity flags.
2553 switch (state
& (BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
)) {
2554 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2556 * New allocation is contiguous with delayed allocations
2557 * on the left and on the right.
2558 * Merge all three into a single extent record.
2560 temp
= left
.br_blockcount
+ new->br_blockcount
+
2561 right
.br_blockcount
;
2563 oldlen
= startblockval(left
.br_startblock
) +
2564 startblockval(new->br_startblock
) +
2565 startblockval(right
.br_startblock
);
2566 newlen
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
2568 left
.br_startblock
= nullstartblock(newlen
);
2569 left
.br_blockcount
= temp
;
2571 xfs_iext_remove(ip
, icur
, state
);
2572 xfs_iext_prev(ifp
, icur
);
2573 xfs_iext_update_extent(ip
, state
, icur
, &left
);
2576 case BMAP_LEFT_CONTIG
:
2578 * New allocation is contiguous with a delayed allocation
2580 * Merge the new allocation with the left neighbor.
2582 temp
= left
.br_blockcount
+ new->br_blockcount
;
2584 oldlen
= startblockval(left
.br_startblock
) +
2585 startblockval(new->br_startblock
);
2586 newlen
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
2588 left
.br_blockcount
= temp
;
2589 left
.br_startblock
= nullstartblock(newlen
);
2591 xfs_iext_prev(ifp
, icur
);
2592 xfs_iext_update_extent(ip
, state
, icur
, &left
);
2595 case BMAP_RIGHT_CONTIG
:
2597 * New allocation is contiguous with a delayed allocation
2599 * Merge the new allocation with the right neighbor.
2601 temp
= new->br_blockcount
+ right
.br_blockcount
;
2602 oldlen
= startblockval(new->br_startblock
) +
2603 startblockval(right
.br_startblock
);
2604 newlen
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
2606 right
.br_startoff
= new->br_startoff
;
2607 right
.br_startblock
= nullstartblock(newlen
);
2608 right
.br_blockcount
= temp
;
2609 xfs_iext_update_extent(ip
, state
, icur
, &right
);
2614 * New allocation is not contiguous with another
2615 * delayed allocation.
2616 * Insert a new entry.
2618 oldlen
= newlen
= 0;
2619 xfs_iext_insert(ip
, icur
, new, state
);
2622 if (oldlen
!= newlen
) {
2623 ASSERT(oldlen
> newlen
);
2624 xfs_mod_fdblocks(ip
->i_mount
, (int64_t)(oldlen
- newlen
),
2627 * Nothing to do for disk quota accounting here.
2633 * Convert a hole to a real allocation.
2635 STATIC
int /* error */
2636 xfs_bmap_add_extent_hole_real(
2637 struct xfs_trans
*tp
,
2638 struct xfs_inode
*ip
,
2640 struct xfs_iext_cursor
*icur
,
2641 struct xfs_btree_cur
**curp
,
2642 struct xfs_bmbt_irec
*new,
2646 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
2647 struct xfs_mount
*mp
= ip
->i_mount
;
2648 struct xfs_btree_cur
*cur
= *curp
;
2649 int error
; /* error return value */
2650 int i
; /* temp state */
2651 xfs_bmbt_irec_t left
; /* left neighbor extent entry */
2652 xfs_bmbt_irec_t right
; /* right neighbor extent entry */
2653 int rval
=0; /* return value (logging flags) */
2654 int state
= xfs_bmap_fork_to_state(whichfork
);
2655 struct xfs_bmbt_irec old
;
2657 ASSERT(!isnullstartblock(new->br_startblock
));
2658 ASSERT(!cur
|| !(cur
->bc_private
.b
.flags
& XFS_BTCUR_BPRV_WASDEL
));
2660 XFS_STATS_INC(mp
, xs_add_exlist
);
2663 * Check and set flags if this segment has a left neighbor.
2665 if (xfs_iext_peek_prev_extent(ifp
, icur
, &left
)) {
2666 state
|= BMAP_LEFT_VALID
;
2667 if (isnullstartblock(left
.br_startblock
))
2668 state
|= BMAP_LEFT_DELAY
;
2672 * Check and set flags if this segment has a current value.
2673 * Not true if we're inserting into the "hole" at eof.
2675 if (xfs_iext_get_extent(ifp
, icur
, &right
)) {
2676 state
|= BMAP_RIGHT_VALID
;
2677 if (isnullstartblock(right
.br_startblock
))
2678 state
|= BMAP_RIGHT_DELAY
;
2682 * We're inserting a real allocation between "left" and "right".
2683 * Set the contiguity flags. Don't let extents get too large.
2685 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
2686 left
.br_startoff
+ left
.br_blockcount
== new->br_startoff
&&
2687 left
.br_startblock
+ left
.br_blockcount
== new->br_startblock
&&
2688 left
.br_state
== new->br_state
&&
2689 left
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
2690 state
|= BMAP_LEFT_CONTIG
;
2692 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
2693 new->br_startoff
+ new->br_blockcount
== right
.br_startoff
&&
2694 new->br_startblock
+ new->br_blockcount
== right
.br_startblock
&&
2695 new->br_state
== right
.br_state
&&
2696 new->br_blockcount
+ right
.br_blockcount
<= MAXEXTLEN
&&
2697 (!(state
& BMAP_LEFT_CONTIG
) ||
2698 left
.br_blockcount
+ new->br_blockcount
+
2699 right
.br_blockcount
<= MAXEXTLEN
))
2700 state
|= BMAP_RIGHT_CONTIG
;
2704 * Select which case we're in here, and implement it.
2706 switch (state
& (BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
)) {
2707 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2709 * New allocation is contiguous with real allocations on the
2710 * left and on the right.
2711 * Merge all three into a single extent record.
2713 left
.br_blockcount
+= new->br_blockcount
+ right
.br_blockcount
;
2715 xfs_iext_remove(ip
, icur
, state
);
2716 xfs_iext_prev(ifp
, icur
);
2717 xfs_iext_update_extent(ip
, state
, icur
, &left
);
2719 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2720 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
2722 rval
= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
2724 rval
= XFS_ILOG_CORE
;
2725 error
= xfs_bmbt_lookup_eq(cur
, &right
, &i
);
2728 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2729 error
= xfs_btree_delete(cur
, &i
);
2732 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2733 error
= xfs_btree_decrement(cur
, 0, &i
);
2736 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2737 error
= xfs_bmbt_update(cur
, &left
);
2743 case BMAP_LEFT_CONTIG
:
2745 * New allocation is contiguous with a real allocation
2747 * Merge the new allocation with the left neighbor.
2750 left
.br_blockcount
+= new->br_blockcount
;
2752 xfs_iext_prev(ifp
, icur
);
2753 xfs_iext_update_extent(ip
, state
, icur
, &left
);
2756 rval
= xfs_ilog_fext(whichfork
);
2759 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2762 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2763 error
= xfs_bmbt_update(cur
, &left
);
2769 case BMAP_RIGHT_CONTIG
:
2771 * New allocation is contiguous with a real allocation
2773 * Merge the new allocation with the right neighbor.
2777 right
.br_startoff
= new->br_startoff
;
2778 right
.br_startblock
= new->br_startblock
;
2779 right
.br_blockcount
+= new->br_blockcount
;
2780 xfs_iext_update_extent(ip
, state
, icur
, &right
);
2783 rval
= xfs_ilog_fext(whichfork
);
2786 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2789 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2790 error
= xfs_bmbt_update(cur
, &right
);
2798 * New allocation is not contiguous with another
2800 * Insert a new entry.
2802 xfs_iext_insert(ip
, icur
, new, state
);
2803 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2804 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
2806 rval
= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
2808 rval
= XFS_ILOG_CORE
;
2809 error
= xfs_bmbt_lookup_eq(cur
, new, &i
);
2812 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
2813 error
= xfs_btree_insert(cur
, &i
);
2816 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2821 /* add reverse mapping unless caller opted out */
2822 if (!(flags
& XFS_BMAPI_NORMAP
)) {
2823 error
= xfs_rmap_map_extent(tp
, ip
, whichfork
, new);
2828 /* convert to a btree if necessary */
2829 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
2830 int tmp_logflags
; /* partial log flag return val */
2832 ASSERT(cur
== NULL
);
2833 error
= xfs_bmap_extents_to_btree(tp
, ip
, curp
, 0,
2834 &tmp_logflags
, whichfork
);
2835 *logflagsp
|= tmp_logflags
;
2841 /* clear out the allocated field, done with it now in any case. */
2843 cur
->bc_private
.b
.allocated
= 0;
2845 xfs_bmap_check_leaf_extents(cur
, ip
, whichfork
);
2852 * Functions used in the extent read, allocate and remove paths
2856 * Adjust the size of the new extent based on di_extsize and rt extsize.
2859 xfs_bmap_extsize_align(
2861 xfs_bmbt_irec_t
*gotp
, /* next extent pointer */
2862 xfs_bmbt_irec_t
*prevp
, /* previous extent pointer */
2863 xfs_extlen_t extsz
, /* align to this extent size */
2864 int rt
, /* is this a realtime inode? */
2865 int eof
, /* is extent at end-of-file? */
2866 int delay
, /* creating delalloc extent? */
2867 int convert
, /* overwriting unwritten extent? */
2868 xfs_fileoff_t
*offp
, /* in/out: aligned offset */
2869 xfs_extlen_t
*lenp
) /* in/out: aligned length */
2871 xfs_fileoff_t orig_off
; /* original offset */
2872 xfs_extlen_t orig_alen
; /* original length */
2873 xfs_fileoff_t orig_end
; /* original off+len */
2874 xfs_fileoff_t nexto
; /* next file offset */
2875 xfs_fileoff_t prevo
; /* previous file offset */
2876 xfs_fileoff_t align_off
; /* temp for offset */
2877 xfs_extlen_t align_alen
; /* temp for length */
2878 xfs_extlen_t temp
; /* temp for calculations */
2883 orig_off
= align_off
= *offp
;
2884 orig_alen
= align_alen
= *lenp
;
2885 orig_end
= orig_off
+ orig_alen
;
2888 * If this request overlaps an existing extent, then don't
2889 * attempt to perform any additional alignment.
2891 if (!delay
&& !eof
&&
2892 (orig_off
>= gotp
->br_startoff
) &&
2893 (orig_end
<= gotp
->br_startoff
+ gotp
->br_blockcount
)) {
2898 * If the file offset is unaligned vs. the extent size
2899 * we need to align it. This will be possible unless
2900 * the file was previously written with a kernel that didn't
2901 * perform this alignment, or if a truncate shot us in the
2904 div_u64_rem(orig_off
, extsz
, &temp
);
2910 /* Same adjustment for the end of the requested area. */
2911 temp
= (align_alen
% extsz
);
2913 align_alen
+= extsz
- temp
;
2916 * For large extent hint sizes, the aligned extent might be larger than
2917 * MAXEXTLEN. In that case, reduce the size by an extsz so that it pulls
2918 * the length back under MAXEXTLEN. The outer allocation loops handle
2919 * short allocation just fine, so it is safe to do this. We only want to
2920 * do it when we are forced to, though, because it means more allocation
2921 * operations are required.
2923 while (align_alen
> MAXEXTLEN
)
2924 align_alen
-= extsz
;
2925 ASSERT(align_alen
<= MAXEXTLEN
);
2928 * If the previous block overlaps with this proposed allocation
2929 * then move the start forward without adjusting the length.
2931 if (prevp
->br_startoff
!= NULLFILEOFF
) {
2932 if (prevp
->br_startblock
== HOLESTARTBLOCK
)
2933 prevo
= prevp
->br_startoff
;
2935 prevo
= prevp
->br_startoff
+ prevp
->br_blockcount
;
2938 if (align_off
!= orig_off
&& align_off
< prevo
)
2941 * If the next block overlaps with this proposed allocation
2942 * then move the start back without adjusting the length,
2943 * but not before offset 0.
2944 * This may of course make the start overlap previous block,
2945 * and if we hit the offset 0 limit then the next block
2946 * can still overlap too.
2948 if (!eof
&& gotp
->br_startoff
!= NULLFILEOFF
) {
2949 if ((delay
&& gotp
->br_startblock
== HOLESTARTBLOCK
) ||
2950 (!delay
&& gotp
->br_startblock
== DELAYSTARTBLOCK
))
2951 nexto
= gotp
->br_startoff
+ gotp
->br_blockcount
;
2953 nexto
= gotp
->br_startoff
;
2955 nexto
= NULLFILEOFF
;
2957 align_off
+ align_alen
!= orig_end
&&
2958 align_off
+ align_alen
> nexto
)
2959 align_off
= nexto
> align_alen
? nexto
- align_alen
: 0;
2961 * If we're now overlapping the next or previous extent that
2962 * means we can't fit an extsz piece in this hole. Just move
2963 * the start forward to the first valid spot and set
2964 * the length so we hit the end.
2966 if (align_off
!= orig_off
&& align_off
< prevo
)
2968 if (align_off
+ align_alen
!= orig_end
&&
2969 align_off
+ align_alen
> nexto
&&
2970 nexto
!= NULLFILEOFF
) {
2971 ASSERT(nexto
> prevo
);
2972 align_alen
= nexto
- align_off
;
2976 * If realtime, and the result isn't a multiple of the realtime
2977 * extent size we need to remove blocks until it is.
2979 if (rt
&& (temp
= (align_alen
% mp
->m_sb
.sb_rextsize
))) {
2981 * We're not covering the original request, or
2982 * we won't be able to once we fix the length.
2984 if (orig_off
< align_off
||
2985 orig_end
> align_off
+ align_alen
||
2986 align_alen
- temp
< orig_alen
)
2989 * Try to fix it by moving the start up.
2991 if (align_off
+ temp
<= orig_off
) {
2996 * Try to fix it by moving the end in.
2998 else if (align_off
+ align_alen
- temp
>= orig_end
)
3001 * Set the start to the minimum then trim the length.
3004 align_alen
-= orig_off
- align_off
;
3005 align_off
= orig_off
;
3006 align_alen
-= align_alen
% mp
->m_sb
.sb_rextsize
;
3009 * Result doesn't cover the request, fail it.
3011 if (orig_off
< align_off
|| orig_end
> align_off
+ align_alen
)
3014 ASSERT(orig_off
>= align_off
);
3015 /* see MAXEXTLEN handling above */
3016 ASSERT(orig_end
<= align_off
+ align_alen
||
3017 align_alen
+ extsz
> MAXEXTLEN
);
3021 if (!eof
&& gotp
->br_startoff
!= NULLFILEOFF
)
3022 ASSERT(align_off
+ align_alen
<= gotp
->br_startoff
);
3023 if (prevp
->br_startoff
!= NULLFILEOFF
)
3024 ASSERT(align_off
>= prevp
->br_startoff
+ prevp
->br_blockcount
);
3032 #define XFS_ALLOC_GAP_UNITS 4
3036 struct xfs_bmalloca
*ap
) /* bmap alloc argument struct */
3038 xfs_fsblock_t adjust
; /* adjustment to block numbers */
3039 xfs_agnumber_t fb_agno
; /* ag number of ap->firstblock */
3040 xfs_mount_t
*mp
; /* mount point structure */
3041 int nullfb
; /* true if ap->firstblock isn't set */
3042 int rt
; /* true if inode is realtime */
3044 #define ISVALID(x,y) \
3046 (x) < mp->m_sb.sb_rblocks : \
3047 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
3048 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
3049 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
3051 mp
= ap
->ip
->i_mount
;
3052 nullfb
= ap
->tp
->t_firstblock
== NULLFSBLOCK
;
3053 rt
= XFS_IS_REALTIME_INODE(ap
->ip
) &&
3054 xfs_alloc_is_userdata(ap
->datatype
);
3055 fb_agno
= nullfb
? NULLAGNUMBER
: XFS_FSB_TO_AGNO(mp
,
3056 ap
->tp
->t_firstblock
);
3058 * If allocating at eof, and there's a previous real block,
3059 * try to use its last block as our starting point.
3061 if (ap
->eof
&& ap
->prev
.br_startoff
!= NULLFILEOFF
&&
3062 !isnullstartblock(ap
->prev
.br_startblock
) &&
3063 ISVALID(ap
->prev
.br_startblock
+ ap
->prev
.br_blockcount
,
3064 ap
->prev
.br_startblock
)) {
3065 ap
->blkno
= ap
->prev
.br_startblock
+ ap
->prev
.br_blockcount
;
3067 * Adjust for the gap between prevp and us.
3069 adjust
= ap
->offset
-
3070 (ap
->prev
.br_startoff
+ ap
->prev
.br_blockcount
);
3072 ISVALID(ap
->blkno
+ adjust
, ap
->prev
.br_startblock
))
3073 ap
->blkno
+= adjust
;
3076 * If not at eof, then compare the two neighbor blocks.
3077 * Figure out whether either one gives us a good starting point,
3078 * and pick the better one.
3080 else if (!ap
->eof
) {
3081 xfs_fsblock_t gotbno
; /* right side block number */
3082 xfs_fsblock_t gotdiff
=0; /* right side difference */
3083 xfs_fsblock_t prevbno
; /* left side block number */
3084 xfs_fsblock_t prevdiff
=0; /* left side difference */
3087 * If there's a previous (left) block, select a requested
3088 * start block based on it.
3090 if (ap
->prev
.br_startoff
!= NULLFILEOFF
&&
3091 !isnullstartblock(ap
->prev
.br_startblock
) &&
3092 (prevbno
= ap
->prev
.br_startblock
+
3093 ap
->prev
.br_blockcount
) &&
3094 ISVALID(prevbno
, ap
->prev
.br_startblock
)) {
3096 * Calculate gap to end of previous block.
3098 adjust
= prevdiff
= ap
->offset
-
3099 (ap
->prev
.br_startoff
+
3100 ap
->prev
.br_blockcount
);
3102 * Figure the startblock based on the previous block's
3103 * end and the gap size.
3105 * If the gap is large relative to the piece we're
3106 * allocating, or using it gives us an invalid block
3107 * number, then just use the end of the previous block.
3109 if (prevdiff
<= XFS_ALLOC_GAP_UNITS
* ap
->length
&&
3110 ISVALID(prevbno
+ prevdiff
,
3111 ap
->prev
.br_startblock
))
3116 * If the firstblock forbids it, can't use it,
3119 if (!rt
&& !nullfb
&&
3120 XFS_FSB_TO_AGNO(mp
, prevbno
) != fb_agno
)
3121 prevbno
= NULLFSBLOCK
;
3124 * No previous block or can't follow it, just default.
3127 prevbno
= NULLFSBLOCK
;
3129 * If there's a following (right) block, select a requested
3130 * start block based on it.
3132 if (!isnullstartblock(ap
->got
.br_startblock
)) {
3134 * Calculate gap to start of next block.
3136 adjust
= gotdiff
= ap
->got
.br_startoff
- ap
->offset
;
3138 * Figure the startblock based on the next block's
3139 * start and the gap size.
3141 gotbno
= ap
->got
.br_startblock
;
3144 * If the gap is large relative to the piece we're
3145 * allocating, or using it gives us an invalid block
3146 * number, then just use the start of the next block
3147 * offset by our length.
3149 if (gotdiff
<= XFS_ALLOC_GAP_UNITS
* ap
->length
&&
3150 ISVALID(gotbno
- gotdiff
, gotbno
))
3152 else if (ISVALID(gotbno
- ap
->length
, gotbno
)) {
3153 gotbno
-= ap
->length
;
3154 gotdiff
+= adjust
- ap
->length
;
3158 * If the firstblock forbids it, can't use it,
3161 if (!rt
&& !nullfb
&&
3162 XFS_FSB_TO_AGNO(mp
, gotbno
) != fb_agno
)
3163 gotbno
= NULLFSBLOCK
;
3166 * No next block, just default.
3169 gotbno
= NULLFSBLOCK
;
3171 * If both valid, pick the better one, else the only good
3172 * one, else ap->blkno is already set (to 0 or the inode block).
3174 if (prevbno
!= NULLFSBLOCK
&& gotbno
!= NULLFSBLOCK
)
3175 ap
->blkno
= prevdiff
<= gotdiff
? prevbno
: gotbno
;
3176 else if (prevbno
!= NULLFSBLOCK
)
3177 ap
->blkno
= prevbno
;
3178 else if (gotbno
!= NULLFSBLOCK
)
3185 xfs_bmap_longest_free_extent(
3186 struct xfs_trans
*tp
,
3191 struct xfs_mount
*mp
= tp
->t_mountp
;
3192 struct xfs_perag
*pag
;
3193 xfs_extlen_t longest
;
3196 pag
= xfs_perag_get(mp
, ag
);
3197 if (!pag
->pagf_init
) {
3198 error
= xfs_alloc_pagf_init(mp
, tp
, ag
, XFS_ALLOC_FLAG_TRYLOCK
);
3202 if (!pag
->pagf_init
) {
3208 longest
= xfs_alloc_longest_free_extent(pag
,
3209 xfs_alloc_min_freelist(mp
, pag
),
3210 xfs_ag_resv_needed(pag
, XFS_AG_RESV_NONE
));
3211 if (*blen
< longest
)
3220 xfs_bmap_select_minlen(
3221 struct xfs_bmalloca
*ap
,
3222 struct xfs_alloc_arg
*args
,
3226 if (notinit
|| *blen
< ap
->minlen
) {
3228 * Since we did a BUF_TRYLOCK above, it is possible that
3229 * there is space for this request.
3231 args
->minlen
= ap
->minlen
;
3232 } else if (*blen
< args
->maxlen
) {
3234 * If the best seen length is less than the request length,
3235 * use the best as the minimum.
3237 args
->minlen
= *blen
;
3240 * Otherwise we've seen an extent as big as maxlen, use that
3243 args
->minlen
= args
->maxlen
;
3248 xfs_bmap_btalloc_nullfb(
3249 struct xfs_bmalloca
*ap
,
3250 struct xfs_alloc_arg
*args
,
3253 struct xfs_mount
*mp
= ap
->ip
->i_mount
;
3254 xfs_agnumber_t ag
, startag
;
3258 args
->type
= XFS_ALLOCTYPE_START_BNO
;
3259 args
->total
= ap
->total
;
3261 startag
= ag
= XFS_FSB_TO_AGNO(mp
, args
->fsbno
);
3262 if (startag
== NULLAGNUMBER
)
3265 while (*blen
< args
->maxlen
) {
3266 error
= xfs_bmap_longest_free_extent(args
->tp
, ag
, blen
,
3271 if (++ag
== mp
->m_sb
.sb_agcount
)
3277 xfs_bmap_select_minlen(ap
, args
, blen
, notinit
);
3282 xfs_bmap_btalloc_filestreams(
3283 struct xfs_bmalloca
*ap
,
3284 struct xfs_alloc_arg
*args
,
3287 struct xfs_mount
*mp
= ap
->ip
->i_mount
;
3292 args
->type
= XFS_ALLOCTYPE_NEAR_BNO
;
3293 args
->total
= ap
->total
;
3295 ag
= XFS_FSB_TO_AGNO(mp
, args
->fsbno
);
3296 if (ag
== NULLAGNUMBER
)
3299 error
= xfs_bmap_longest_free_extent(args
->tp
, ag
, blen
, ¬init
);
3303 if (*blen
< args
->maxlen
) {
3304 error
= xfs_filestream_new_ag(ap
, &ag
);
3308 error
= xfs_bmap_longest_free_extent(args
->tp
, ag
, blen
,
3315 xfs_bmap_select_minlen(ap
, args
, blen
, notinit
);
3318 * Set the failure fallback case to look in the selected AG as stream
3321 ap
->blkno
= args
->fsbno
= XFS_AGB_TO_FSB(mp
, ag
, 0);
3325 /* Update all inode and quota accounting for the allocation we just did. */
3327 xfs_bmap_btalloc_accounting(
3328 struct xfs_bmalloca
*ap
,
3329 struct xfs_alloc_arg
*args
)
3331 if (ap
->flags
& XFS_BMAPI_COWFORK
) {
3333 * COW fork blocks are in-core only and thus are treated as
3334 * in-core quota reservation (like delalloc blocks) even when
3335 * converted to real blocks. The quota reservation is not
3336 * accounted to disk until blocks are remapped to the data
3337 * fork. So if these blocks were previously delalloc, we
3338 * already have quota reservation and there's nothing to do
3345 * Otherwise, we've allocated blocks in a hole. The transaction
3346 * has acquired in-core quota reservation for this extent.
3347 * Rather than account these as real blocks, however, we reduce
3348 * the transaction quota reservation based on the allocation.
3349 * This essentially transfers the transaction quota reservation
3350 * to that of a delalloc extent.
3352 ap
->ip
->i_delayed_blks
+= args
->len
;
3353 xfs_trans_mod_dquot_byino(ap
->tp
, ap
->ip
, XFS_TRANS_DQ_RES_BLKS
,
3358 /* data/attr fork only */
3359 ap
->ip
->i_d
.di_nblocks
+= args
->len
;
3360 xfs_trans_log_inode(ap
->tp
, ap
->ip
, XFS_ILOG_CORE
);
3362 ap
->ip
->i_delayed_blks
-= args
->len
;
3363 xfs_trans_mod_dquot_byino(ap
->tp
, ap
->ip
,
3364 ap
->wasdel
? XFS_TRANS_DQ_DELBCOUNT
: XFS_TRANS_DQ_BCOUNT
,
3370 struct xfs_bmalloca
*ap
) /* bmap alloc argument struct */
3372 xfs_mount_t
*mp
; /* mount point structure */
3373 xfs_alloctype_t atype
= 0; /* type for allocation routines */
3374 xfs_extlen_t align
= 0; /* minimum allocation alignment */
3375 xfs_agnumber_t fb_agno
; /* ag number of ap->firstblock */
3377 xfs_alloc_arg_t args
;
3378 xfs_fileoff_t orig_offset
;
3379 xfs_extlen_t orig_length
;
3381 xfs_extlen_t nextminlen
= 0;
3382 int nullfb
; /* true if ap->firstblock isn't set */
3389 orig_offset
= ap
->offset
;
3390 orig_length
= ap
->length
;
3392 mp
= ap
->ip
->i_mount
;
3394 /* stripe alignment for allocation is determined by mount parameters */
3396 if (mp
->m_swidth
&& (mp
->m_flags
& XFS_MOUNT_SWALLOC
))
3397 stripe_align
= mp
->m_swidth
;
3398 else if (mp
->m_dalign
)
3399 stripe_align
= mp
->m_dalign
;
3401 if (ap
->flags
& XFS_BMAPI_COWFORK
)
3402 align
= xfs_get_cowextsz_hint(ap
->ip
);
3403 else if (xfs_alloc_is_userdata(ap
->datatype
))
3404 align
= xfs_get_extsz_hint(ap
->ip
);
3406 error
= xfs_bmap_extsize_align(mp
, &ap
->got
, &ap
->prev
,
3407 align
, 0, ap
->eof
, 0, ap
->conv
,
3408 &ap
->offset
, &ap
->length
);
3414 nullfb
= ap
->tp
->t_firstblock
== NULLFSBLOCK
;
3415 fb_agno
= nullfb
? NULLAGNUMBER
: XFS_FSB_TO_AGNO(mp
,
3416 ap
->tp
->t_firstblock
);
3418 if (xfs_alloc_is_userdata(ap
->datatype
) &&
3419 xfs_inode_is_filestream(ap
->ip
)) {
3420 ag
= xfs_filestream_lookup_ag(ap
->ip
);
3421 ag
= (ag
!= NULLAGNUMBER
) ? ag
: 0;
3422 ap
->blkno
= XFS_AGB_TO_FSB(mp
, ag
, 0);
3424 ap
->blkno
= XFS_INO_TO_FSB(mp
, ap
->ip
->i_ino
);
3427 ap
->blkno
= ap
->tp
->t_firstblock
;
3429 xfs_bmap_adjacent(ap
);
3432 * If allowed, use ap->blkno; otherwise must use firstblock since
3433 * it's in the right allocation group.
3435 if (nullfb
|| XFS_FSB_TO_AGNO(mp
, ap
->blkno
) == fb_agno
)
3438 ap
->blkno
= ap
->tp
->t_firstblock
;
3440 * Normal allocation, done through xfs_alloc_vextent.
3442 tryagain
= isaligned
= 0;
3443 memset(&args
, 0, sizeof(args
));
3446 args
.fsbno
= ap
->blkno
;
3447 xfs_rmap_skip_owner_update(&args
.oinfo
);
3449 /* Trim the allocation back to the maximum an AG can fit. */
3450 args
.maxlen
= min(ap
->length
, mp
->m_ag_max_usable
);
3454 * Search for an allocation group with a single extent large
3455 * enough for the request. If one isn't found, then adjust
3456 * the minimum allocation size to the largest space found.
3458 if (xfs_alloc_is_userdata(ap
->datatype
) &&
3459 xfs_inode_is_filestream(ap
->ip
))
3460 error
= xfs_bmap_btalloc_filestreams(ap
, &args
, &blen
);
3462 error
= xfs_bmap_btalloc_nullfb(ap
, &args
, &blen
);
3465 } else if (ap
->tp
->t_flags
& XFS_TRANS_LOWMODE
) {
3466 if (xfs_inode_is_filestream(ap
->ip
))
3467 args
.type
= XFS_ALLOCTYPE_FIRST_AG
;
3469 args
.type
= XFS_ALLOCTYPE_START_BNO
;
3470 args
.total
= args
.minlen
= ap
->minlen
;
3472 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
3473 args
.total
= ap
->total
;
3474 args
.minlen
= ap
->minlen
;
3476 /* apply extent size hints if obtained earlier */
3479 div_u64_rem(ap
->offset
, args
.prod
, &args
.mod
);
3481 args
.mod
= args
.prod
- args
.mod
;
3482 } else if (mp
->m_sb
.sb_blocksize
>= PAGE_SIZE
) {
3486 args
.prod
= PAGE_SIZE
>> mp
->m_sb
.sb_blocklog
;
3487 div_u64_rem(ap
->offset
, args
.prod
, &args
.mod
);
3489 args
.mod
= args
.prod
- args
.mod
;
3492 * If we are not low on available data blocks, and the
3493 * underlying logical volume manager is a stripe, and
3494 * the file offset is zero then try to allocate data
3495 * blocks on stripe unit boundary.
3496 * NOTE: ap->aeof is only set if the allocation length
3497 * is >= the stripe unit and the allocation offset is
3498 * at the end of file.
3500 if (!(ap
->tp
->t_flags
& XFS_TRANS_LOWMODE
) && ap
->aeof
) {
3502 args
.alignment
= stripe_align
;
3506 * Adjust for alignment
3508 if (blen
> args
.alignment
&& blen
<= args
.maxlen
)
3509 args
.minlen
= blen
- args
.alignment
;
3510 args
.minalignslop
= 0;
3513 * First try an exact bno allocation.
3514 * If it fails then do a near or start bno
3515 * allocation with alignment turned on.
3519 args
.type
= XFS_ALLOCTYPE_THIS_BNO
;
3522 * Compute the minlen+alignment for the
3523 * next case. Set slop so that the value
3524 * of minlen+alignment+slop doesn't go up
3525 * between the calls.
3527 if (blen
> stripe_align
&& blen
<= args
.maxlen
)
3528 nextminlen
= blen
- stripe_align
;
3530 nextminlen
= args
.minlen
;
3531 if (nextminlen
+ stripe_align
> args
.minlen
+ 1)
3533 nextminlen
+ stripe_align
-
3536 args
.minalignslop
= 0;
3540 args
.minalignslop
= 0;
3542 args
.minleft
= ap
->minleft
;
3543 args
.wasdel
= ap
->wasdel
;
3544 args
.resv
= XFS_AG_RESV_NONE
;
3545 args
.datatype
= ap
->datatype
;
3546 if (ap
->datatype
& XFS_ALLOC_USERDATA_ZERO
)
3549 error
= xfs_alloc_vextent(&args
);
3553 if (tryagain
&& args
.fsbno
== NULLFSBLOCK
) {
3555 * Exact allocation failed. Now try with alignment
3559 args
.fsbno
= ap
->blkno
;
3560 args
.alignment
= stripe_align
;
3561 args
.minlen
= nextminlen
;
3562 args
.minalignslop
= 0;
3564 if ((error
= xfs_alloc_vextent(&args
)))
3567 if (isaligned
&& args
.fsbno
== NULLFSBLOCK
) {
3569 * allocation failed, so turn off alignment and
3573 args
.fsbno
= ap
->blkno
;
3575 if ((error
= xfs_alloc_vextent(&args
)))
3578 if (args
.fsbno
== NULLFSBLOCK
&& nullfb
&&
3579 args
.minlen
> ap
->minlen
) {
3580 args
.minlen
= ap
->minlen
;
3581 args
.type
= XFS_ALLOCTYPE_START_BNO
;
3582 args
.fsbno
= ap
->blkno
;
3583 if ((error
= xfs_alloc_vextent(&args
)))
3586 if (args
.fsbno
== NULLFSBLOCK
&& nullfb
) {
3588 args
.type
= XFS_ALLOCTYPE_FIRST_AG
;
3589 args
.total
= ap
->minlen
;
3590 if ((error
= xfs_alloc_vextent(&args
)))
3592 ap
->tp
->t_flags
|= XFS_TRANS_LOWMODE
;
3594 if (args
.fsbno
!= NULLFSBLOCK
) {
3596 * check the allocation happened at the same or higher AG than
3597 * the first block that was allocated.
3599 ASSERT(ap
->tp
->t_firstblock
== NULLFSBLOCK
||
3600 XFS_FSB_TO_AGNO(mp
, ap
->tp
->t_firstblock
) <=
3601 XFS_FSB_TO_AGNO(mp
, args
.fsbno
));
3603 ap
->blkno
= args
.fsbno
;
3604 if (ap
->tp
->t_firstblock
== NULLFSBLOCK
)
3605 ap
->tp
->t_firstblock
= args
.fsbno
;
3606 ASSERT(nullfb
|| fb_agno
<= args
.agno
);
3607 ap
->length
= args
.len
;
3609 * If the extent size hint is active, we tried to round the
3610 * caller's allocation request offset down to extsz and the
3611 * length up to another extsz boundary. If we found a free
3612 * extent we mapped it in starting at this new offset. If the
3613 * newly mapped space isn't long enough to cover any of the
3614 * range of offsets that was originally requested, move the
3615 * mapping up so that we can fill as much of the caller's
3616 * original request as possible. Free space is apparently
3617 * very fragmented so we're unlikely to be able to satisfy the
3620 if (ap
->length
<= orig_length
)
3621 ap
->offset
= orig_offset
;
3622 else if (ap
->offset
+ ap
->length
< orig_offset
+ orig_length
)
3623 ap
->offset
= orig_offset
+ orig_length
- ap
->length
;
3624 xfs_bmap_btalloc_accounting(ap
, &args
);
3626 ap
->blkno
= NULLFSBLOCK
;
3633 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
3634 * It figures out where to ask the underlying allocator to put the new extent.
3638 struct xfs_bmalloca
*ap
) /* bmap alloc argument struct */
3640 if (XFS_IS_REALTIME_INODE(ap
->ip
) &&
3641 xfs_alloc_is_userdata(ap
->datatype
))
3642 return xfs_bmap_rtalloc(ap
);
3643 return xfs_bmap_btalloc(ap
);
3646 /* Trim extent to fit a logical block range. */
3649 struct xfs_bmbt_irec
*irec
,
3653 xfs_fileoff_t distance
;
3654 xfs_fileoff_t end
= bno
+ len
;
3656 if (irec
->br_startoff
+ irec
->br_blockcount
<= bno
||
3657 irec
->br_startoff
>= end
) {
3658 irec
->br_blockcount
= 0;
3662 if (irec
->br_startoff
< bno
) {
3663 distance
= bno
- irec
->br_startoff
;
3664 if (isnullstartblock(irec
->br_startblock
))
3665 irec
->br_startblock
= DELAYSTARTBLOCK
;
3666 if (irec
->br_startblock
!= DELAYSTARTBLOCK
&&
3667 irec
->br_startblock
!= HOLESTARTBLOCK
)
3668 irec
->br_startblock
+= distance
;
3669 irec
->br_startoff
+= distance
;
3670 irec
->br_blockcount
-= distance
;
3673 if (end
< irec
->br_startoff
+ irec
->br_blockcount
) {
3674 distance
= irec
->br_startoff
+ irec
->br_blockcount
- end
;
3675 irec
->br_blockcount
-= distance
;
3679 /* trim extent to within eof */
3681 xfs_trim_extent_eof(
3682 struct xfs_bmbt_irec
*irec
,
3683 struct xfs_inode
*ip
)
3686 xfs_trim_extent(irec
, 0, XFS_B_TO_FSB(ip
->i_mount
,
3687 i_size_read(VFS_I(ip
))));
3691 * Trim the returned map to the required bounds
3695 struct xfs_bmbt_irec
*mval
,
3696 struct xfs_bmbt_irec
*got
,
3704 if ((flags
& XFS_BMAPI_ENTIRE
) ||
3705 got
->br_startoff
+ got
->br_blockcount
<= obno
) {
3707 if (isnullstartblock(got
->br_startblock
))
3708 mval
->br_startblock
= DELAYSTARTBLOCK
;
3714 ASSERT((*bno
>= obno
) || (n
== 0));
3716 mval
->br_startoff
= *bno
;
3717 if (isnullstartblock(got
->br_startblock
))
3718 mval
->br_startblock
= DELAYSTARTBLOCK
;
3720 mval
->br_startblock
= got
->br_startblock
+
3721 (*bno
- got
->br_startoff
);
3723 * Return the minimum of what we got and what we asked for for
3724 * the length. We can use the len variable here because it is
3725 * modified below and we could have been there before coming
3726 * here if the first part of the allocation didn't overlap what
3729 mval
->br_blockcount
= XFS_FILBLKS_MIN(end
- *bno
,
3730 got
->br_blockcount
- (*bno
- got
->br_startoff
));
3731 mval
->br_state
= got
->br_state
;
3732 ASSERT(mval
->br_blockcount
<= len
);
3737 * Update and validate the extent map to return
3740 xfs_bmapi_update_map(
3741 struct xfs_bmbt_irec
**map
,
3749 xfs_bmbt_irec_t
*mval
= *map
;
3751 ASSERT((flags
& XFS_BMAPI_ENTIRE
) ||
3752 ((mval
->br_startoff
+ mval
->br_blockcount
) <= end
));
3753 ASSERT((flags
& XFS_BMAPI_ENTIRE
) || (mval
->br_blockcount
<= *len
) ||
3754 (mval
->br_startoff
< obno
));
3756 *bno
= mval
->br_startoff
+ mval
->br_blockcount
;
3758 if (*n
> 0 && mval
->br_startoff
== mval
[-1].br_startoff
) {
3759 /* update previous map with new information */
3760 ASSERT(mval
->br_startblock
== mval
[-1].br_startblock
);
3761 ASSERT(mval
->br_blockcount
> mval
[-1].br_blockcount
);
3762 ASSERT(mval
->br_state
== mval
[-1].br_state
);
3763 mval
[-1].br_blockcount
= mval
->br_blockcount
;
3764 mval
[-1].br_state
= mval
->br_state
;
3765 } else if (*n
> 0 && mval
->br_startblock
!= DELAYSTARTBLOCK
&&
3766 mval
[-1].br_startblock
!= DELAYSTARTBLOCK
&&
3767 mval
[-1].br_startblock
!= HOLESTARTBLOCK
&&
3768 mval
->br_startblock
== mval
[-1].br_startblock
+
3769 mval
[-1].br_blockcount
&&
3770 mval
[-1].br_state
== mval
->br_state
) {
3771 ASSERT(mval
->br_startoff
==
3772 mval
[-1].br_startoff
+ mval
[-1].br_blockcount
);
3773 mval
[-1].br_blockcount
+= mval
->br_blockcount
;
3774 } else if (*n
> 0 &&
3775 mval
->br_startblock
== DELAYSTARTBLOCK
&&
3776 mval
[-1].br_startblock
== DELAYSTARTBLOCK
&&
3777 mval
->br_startoff
==
3778 mval
[-1].br_startoff
+ mval
[-1].br_blockcount
) {
3779 mval
[-1].br_blockcount
+= mval
->br_blockcount
;
3780 mval
[-1].br_state
= mval
->br_state
;
3781 } else if (!((*n
== 0) &&
3782 ((mval
->br_startoff
+ mval
->br_blockcount
) <=
3791 * Map file blocks to filesystem blocks without allocation.
3795 struct xfs_inode
*ip
,
3798 struct xfs_bmbt_irec
*mval
,
3802 struct xfs_mount
*mp
= ip
->i_mount
;
3803 struct xfs_ifork
*ifp
;
3804 struct xfs_bmbt_irec got
;
3807 struct xfs_iext_cursor icur
;
3811 int whichfork
= xfs_bmapi_whichfork(flags
);
3814 ASSERT(!(flags
& ~(XFS_BMAPI_ATTRFORK
|XFS_BMAPI_ENTIRE
|
3815 XFS_BMAPI_COWFORK
)));
3816 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_SHARED
|XFS_ILOCK_EXCL
));
3818 if (unlikely(XFS_TEST_ERROR(
3819 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
3820 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
3821 mp
, XFS_ERRTAG_BMAPIFORMAT
))) {
3822 XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW
, mp
);
3823 return -EFSCORRUPTED
;
3826 if (XFS_FORCED_SHUTDOWN(mp
))
3829 XFS_STATS_INC(mp
, xs_blk_mapr
);
3831 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
3833 /* No CoW fork? Return a hole. */
3834 if (whichfork
== XFS_COW_FORK
&& !ifp
) {
3835 mval
->br_startoff
= bno
;
3836 mval
->br_startblock
= HOLESTARTBLOCK
;
3837 mval
->br_blockcount
= len
;
3838 mval
->br_state
= XFS_EXT_NORM
;
3843 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
3844 error
= xfs_iread_extents(NULL
, ip
, whichfork
);
3849 if (!xfs_iext_lookup_extent(ip
, ifp
, bno
, &icur
, &got
))
3854 while (bno
< end
&& n
< *nmap
) {
3855 /* Reading past eof, act as though there's a hole up to end. */
3857 got
.br_startoff
= end
;
3858 if (got
.br_startoff
> bno
) {
3859 /* Reading in a hole. */
3860 mval
->br_startoff
= bno
;
3861 mval
->br_startblock
= HOLESTARTBLOCK
;
3862 mval
->br_blockcount
=
3863 XFS_FILBLKS_MIN(len
, got
.br_startoff
- bno
);
3864 mval
->br_state
= XFS_EXT_NORM
;
3865 bno
+= mval
->br_blockcount
;
3866 len
-= mval
->br_blockcount
;
3872 /* set up the extent map to return. */
3873 xfs_bmapi_trim_map(mval
, &got
, &bno
, len
, obno
, end
, n
, flags
);
3874 xfs_bmapi_update_map(&mval
, &bno
, &len
, obno
, end
, &n
, flags
);
3876 /* If we're done, stop now. */
3877 if (bno
>= end
|| n
>= *nmap
)
3880 /* Else go on to the next record. */
3881 if (!xfs_iext_next_extent(ifp
, &icur
, &got
))
3889 * Add a delayed allocation extent to an inode. Blocks are reserved from the
3890 * global pool and the extent inserted into the inode in-core extent tree.
3892 * On entry, got refers to the first extent beyond the offset of the extent to
3893 * allocate or eof is specified if no such extent exists. On return, got refers
3894 * to the extent record that was inserted to the inode fork.
3896 * Note that the allocated extent may have been merged with contiguous extents
3897 * during insertion into the inode fork. Thus, got does not reflect the current
3898 * state of the inode fork on return. If necessary, the caller can use lastx to
3899 * look up the updated record in the inode fork.
3902 xfs_bmapi_reserve_delalloc(
3903 struct xfs_inode
*ip
,
3907 xfs_filblks_t prealloc
,
3908 struct xfs_bmbt_irec
*got
,
3909 struct xfs_iext_cursor
*icur
,
3912 struct xfs_mount
*mp
= ip
->i_mount
;
3913 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
3915 xfs_extlen_t indlen
;
3917 xfs_fileoff_t aoff
= off
;
3920 * Cap the alloc length. Keep track of prealloc so we know whether to
3921 * tag the inode before we return.
3923 alen
= XFS_FILBLKS_MIN(len
+ prealloc
, MAXEXTLEN
);
3925 alen
= XFS_FILBLKS_MIN(alen
, got
->br_startoff
- aoff
);
3926 if (prealloc
&& alen
>= len
)
3927 prealloc
= alen
- len
;
3929 /* Figure out the extent size, adjust alen */
3930 if (whichfork
== XFS_COW_FORK
) {
3931 struct xfs_bmbt_irec prev
;
3932 xfs_extlen_t extsz
= xfs_get_cowextsz_hint(ip
);
3934 if (!xfs_iext_peek_prev_extent(ifp
, icur
, &prev
))
3935 prev
.br_startoff
= NULLFILEOFF
;
3937 error
= xfs_bmap_extsize_align(mp
, got
, &prev
, extsz
, 0, eof
,
3938 1, 0, &aoff
, &alen
);
3943 * Make a transaction-less quota reservation for delayed allocation
3944 * blocks. This number gets adjusted later. We return if we haven't
3945 * allocated blocks already inside this loop.
3947 error
= xfs_trans_reserve_quota_nblks(NULL
, ip
, (long)alen
, 0,
3948 XFS_QMOPT_RES_REGBLKS
);
3953 * Split changing sb for alen and indlen since they could be coming
3954 * from different places.
3956 indlen
= (xfs_extlen_t
)xfs_bmap_worst_indlen(ip
, alen
);
3959 error
= xfs_mod_fdblocks(mp
, -((int64_t)alen
), false);
3961 goto out_unreserve_quota
;
3963 error
= xfs_mod_fdblocks(mp
, -((int64_t)indlen
), false);
3965 goto out_unreserve_blocks
;
3968 ip
->i_delayed_blks
+= alen
;
3970 got
->br_startoff
= aoff
;
3971 got
->br_startblock
= nullstartblock(indlen
);
3972 got
->br_blockcount
= alen
;
3973 got
->br_state
= XFS_EXT_NORM
;
3975 xfs_bmap_add_extent_hole_delay(ip
, whichfork
, icur
, got
);
3978 * Tag the inode if blocks were preallocated. Note that COW fork
3979 * preallocation can occur at the start or end of the extent, even when
3980 * prealloc == 0, so we must also check the aligned offset and length.
3982 if (whichfork
== XFS_DATA_FORK
&& prealloc
)
3983 xfs_inode_set_eofblocks_tag(ip
);
3984 if (whichfork
== XFS_COW_FORK
&& (prealloc
|| aoff
< off
|| alen
> len
))
3985 xfs_inode_set_cowblocks_tag(ip
);
3989 out_unreserve_blocks
:
3990 xfs_mod_fdblocks(mp
, alen
, false);
3991 out_unreserve_quota
:
3992 if (XFS_IS_QUOTA_ON(mp
))
3993 xfs_trans_unreserve_quota_nblks(NULL
, ip
, (long)alen
, 0,
3994 XFS_QMOPT_RES_REGBLKS
);
4000 struct xfs_bmalloca
*bma
)
4002 struct xfs_mount
*mp
= bma
->ip
->i_mount
;
4003 int whichfork
= xfs_bmapi_whichfork(bma
->flags
);
4004 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
4005 int tmp_logflags
= 0;
4008 ASSERT(bma
->length
> 0);
4011 * For the wasdelay case, we could also just allocate the stuff asked
4012 * for in this bmap call but that wouldn't be as good.
4015 bma
->length
= (xfs_extlen_t
)bma
->got
.br_blockcount
;
4016 bma
->offset
= bma
->got
.br_startoff
;
4017 xfs_iext_peek_prev_extent(ifp
, &bma
->icur
, &bma
->prev
);
4019 bma
->length
= XFS_FILBLKS_MIN(bma
->length
, MAXEXTLEN
);
4021 bma
->length
= XFS_FILBLKS_MIN(bma
->length
,
4022 bma
->got
.br_startoff
- bma
->offset
);
4026 * Set the data type being allocated. For the data fork, the first data
4027 * in the file is treated differently to all other allocations. For the
4028 * attribute fork, we only need to ensure the allocated range is not on
4031 if (!(bma
->flags
& XFS_BMAPI_METADATA
)) {
4032 bma
->datatype
= XFS_ALLOC_NOBUSY
;
4033 if (whichfork
== XFS_DATA_FORK
) {
4034 if (bma
->offset
== 0)
4035 bma
->datatype
|= XFS_ALLOC_INITIAL_USER_DATA
;
4037 bma
->datatype
|= XFS_ALLOC_USERDATA
;
4039 if (bma
->flags
& XFS_BMAPI_ZERO
)
4040 bma
->datatype
|= XFS_ALLOC_USERDATA_ZERO
;
4043 bma
->minlen
= (bma
->flags
& XFS_BMAPI_CONTIG
) ? bma
->length
: 1;
4046 * Only want to do the alignment at the eof if it is userdata and
4047 * allocation length is larger than a stripe unit.
4049 if (mp
->m_dalign
&& bma
->length
>= mp
->m_dalign
&&
4050 !(bma
->flags
& XFS_BMAPI_METADATA
) && whichfork
== XFS_DATA_FORK
) {
4051 error
= xfs_bmap_isaeof(bma
, whichfork
);
4056 error
= xfs_bmap_alloc(bma
);
4060 if (bma
->blkno
== NULLFSBLOCK
)
4062 if ((ifp
->if_flags
& XFS_IFBROOT
) && !bma
->cur
)
4063 bma
->cur
= xfs_bmbt_init_cursor(mp
, bma
->tp
, bma
->ip
, whichfork
);
4065 * Bump the number of extents we've allocated
4071 bma
->cur
->bc_private
.b
.flags
=
4072 bma
->wasdel
? XFS_BTCUR_BPRV_WASDEL
: 0;
4074 bma
->got
.br_startoff
= bma
->offset
;
4075 bma
->got
.br_startblock
= bma
->blkno
;
4076 bma
->got
.br_blockcount
= bma
->length
;
4077 bma
->got
.br_state
= XFS_EXT_NORM
;
4080 * In the data fork, a wasdelay extent has been initialized, so
4081 * shouldn't be flagged as unwritten.
4083 * For the cow fork, however, we convert delalloc reservations
4084 * (extents allocated for speculative preallocation) to
4085 * allocated unwritten extents, and only convert the unwritten
4086 * extents to real extents when we're about to write the data.
4088 if ((!bma
->wasdel
|| (bma
->flags
& XFS_BMAPI_COWFORK
)) &&
4089 (bma
->flags
& XFS_BMAPI_PREALLOC
))
4090 bma
->got
.br_state
= XFS_EXT_UNWRITTEN
;
4093 error
= xfs_bmap_add_extent_delay_real(bma
, whichfork
);
4095 error
= xfs_bmap_add_extent_hole_real(bma
->tp
, bma
->ip
,
4096 whichfork
, &bma
->icur
, &bma
->cur
, &bma
->got
,
4097 &bma
->logflags
, bma
->flags
);
4099 bma
->logflags
|= tmp_logflags
;
4104 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4105 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4106 * the neighbouring ones.
4108 xfs_iext_get_extent(ifp
, &bma
->icur
, &bma
->got
);
4110 ASSERT(bma
->got
.br_startoff
<= bma
->offset
);
4111 ASSERT(bma
->got
.br_startoff
+ bma
->got
.br_blockcount
>=
4112 bma
->offset
+ bma
->length
);
4113 ASSERT(bma
->got
.br_state
== XFS_EXT_NORM
||
4114 bma
->got
.br_state
== XFS_EXT_UNWRITTEN
);
4119 xfs_bmapi_convert_unwritten(
4120 struct xfs_bmalloca
*bma
,
4121 struct xfs_bmbt_irec
*mval
,
4125 int whichfork
= xfs_bmapi_whichfork(flags
);
4126 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
4127 int tmp_logflags
= 0;
4130 /* check if we need to do unwritten->real conversion */
4131 if (mval
->br_state
== XFS_EXT_UNWRITTEN
&&
4132 (flags
& XFS_BMAPI_PREALLOC
))
4135 /* check if we need to do real->unwritten conversion */
4136 if (mval
->br_state
== XFS_EXT_NORM
&&
4137 (flags
& (XFS_BMAPI_PREALLOC
| XFS_BMAPI_CONVERT
)) !=
4138 (XFS_BMAPI_PREALLOC
| XFS_BMAPI_CONVERT
))
4142 * Modify (by adding) the state flag, if writing.
4144 ASSERT(mval
->br_blockcount
<= len
);
4145 if ((ifp
->if_flags
& XFS_IFBROOT
) && !bma
->cur
) {
4146 bma
->cur
= xfs_bmbt_init_cursor(bma
->ip
->i_mount
, bma
->tp
,
4147 bma
->ip
, whichfork
);
4149 mval
->br_state
= (mval
->br_state
== XFS_EXT_UNWRITTEN
)
4150 ? XFS_EXT_NORM
: XFS_EXT_UNWRITTEN
;
4153 * Before insertion into the bmbt, zero the range being converted
4156 if (flags
& XFS_BMAPI_ZERO
) {
4157 error
= xfs_zero_extent(bma
->ip
, mval
->br_startblock
,
4158 mval
->br_blockcount
);
4163 error
= xfs_bmap_add_extent_unwritten_real(bma
->tp
, bma
->ip
, whichfork
,
4164 &bma
->icur
, &bma
->cur
, mval
, &tmp_logflags
);
4166 * Log the inode core unconditionally in the unwritten extent conversion
4167 * path because the conversion might not have done so (e.g., if the
4168 * extent count hasn't changed). We need to make sure the inode is dirty
4169 * in the transaction for the sake of fsync(), even if nothing has
4170 * changed, because fsync() will not force the log for this transaction
4171 * unless it sees the inode pinned.
4173 * Note: If we're only converting cow fork extents, there aren't
4174 * any on-disk updates to make, so we don't need to log anything.
4176 if (whichfork
!= XFS_COW_FORK
)
4177 bma
->logflags
|= tmp_logflags
| XFS_ILOG_CORE
;
4182 * Update our extent pointer, given that
4183 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4184 * of the neighbouring ones.
4186 xfs_iext_get_extent(ifp
, &bma
->icur
, &bma
->got
);
4189 * We may have combined previously unwritten space with written space,
4190 * so generate another request.
4192 if (mval
->br_blockcount
< len
)
4198 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4199 * extent state if necessary. Details behaviour is controlled by the flags
4200 * parameter. Only allocates blocks from a single allocation group, to avoid
4205 struct xfs_trans
*tp
, /* transaction pointer */
4206 struct xfs_inode
*ip
, /* incore inode */
4207 xfs_fileoff_t bno
, /* starting file offs. mapped */
4208 xfs_filblks_t len
, /* length to map in file */
4209 int flags
, /* XFS_BMAPI_... */
4210 xfs_extlen_t total
, /* total blocks needed */
4211 struct xfs_bmbt_irec
*mval
, /* output: map values */
4212 int *nmap
) /* i/o: mval size/count */
4214 struct xfs_mount
*mp
= ip
->i_mount
;
4215 struct xfs_ifork
*ifp
;
4216 struct xfs_bmalloca bma
= { NULL
}; /* args for xfs_bmap_alloc */
4217 xfs_fileoff_t end
; /* end of mapped file region */
4218 bool eof
= false; /* after the end of extents */
4219 int error
; /* error return */
4220 int n
; /* current extent index */
4221 xfs_fileoff_t obno
; /* old block number (offset) */
4222 int whichfork
; /* data or attr fork */
4225 xfs_fileoff_t orig_bno
; /* original block number value */
4226 int orig_flags
; /* original flags arg value */
4227 xfs_filblks_t orig_len
; /* original value of len arg */
4228 struct xfs_bmbt_irec
*orig_mval
; /* original value of mval */
4229 int orig_nmap
; /* original value of *nmap */
4237 whichfork
= xfs_bmapi_whichfork(flags
);
4240 ASSERT(*nmap
<= XFS_BMAP_MAX_NMAP
);
4241 ASSERT(tp
!= NULL
||
4242 (flags
& (XFS_BMAPI_CONVERT
| XFS_BMAPI_COWFORK
)) ==
4243 (XFS_BMAPI_CONVERT
| XFS_BMAPI_COWFORK
));
4245 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_LOCAL
);
4246 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
4247 ASSERT(!(flags
& XFS_BMAPI_REMAP
));
4249 /* zeroing is for currently only for data extents, not metadata */
4250 ASSERT((flags
& (XFS_BMAPI_METADATA
| XFS_BMAPI_ZERO
)) !=
4251 (XFS_BMAPI_METADATA
| XFS_BMAPI_ZERO
));
4253 * we can allocate unwritten extents or pre-zero allocated blocks,
4254 * but it makes no sense to do both at once. This would result in
4255 * zeroing the unwritten extent twice, but it still being an
4256 * unwritten extent....
4258 ASSERT((flags
& (XFS_BMAPI_PREALLOC
| XFS_BMAPI_ZERO
)) !=
4259 (XFS_BMAPI_PREALLOC
| XFS_BMAPI_ZERO
));
4261 if (unlikely(XFS_TEST_ERROR(
4262 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
4263 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
4264 mp
, XFS_ERRTAG_BMAPIFORMAT
))) {
4265 XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW
, mp
);
4266 return -EFSCORRUPTED
;
4269 if (XFS_FORCED_SHUTDOWN(mp
))
4272 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4274 XFS_STATS_INC(mp
, xs_blk_mapw
);
4276 if (!tp
|| tp
->t_firstblock
== NULLFSBLOCK
) {
4277 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
)
4278 bma
.minleft
= be16_to_cpu(ifp
->if_broot
->bb_level
) + 1;
4285 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
4286 error
= xfs_iread_extents(tp
, ip
, whichfork
);
4295 if (!xfs_iext_lookup_extent(ip
, ifp
, bno
, &bma
.icur
, &bma
.got
))
4297 if (!xfs_iext_peek_prev_extent(ifp
, &bma
.icur
, &bma
.prev
))
4298 bma
.prev
.br_startoff
= NULLFILEOFF
;
4304 while (bno
< end
&& n
< *nmap
) {
4305 bool need_alloc
= false, wasdelay
= false;
4307 /* in hole or beyond EOF? */
4308 if (eof
|| bma
.got
.br_startoff
> bno
) {
4310 * CoW fork conversions should /never/ hit EOF or
4311 * holes. There should always be something for us
4314 ASSERT(!((flags
& XFS_BMAPI_CONVERT
) &&
4315 (flags
& XFS_BMAPI_COWFORK
)));
4317 if (flags
& XFS_BMAPI_DELALLOC
) {
4319 * For the COW fork we can reasonably get a
4320 * request for converting an extent that races
4321 * with other threads already having converted
4322 * part of it, as there converting COW to
4323 * regular blocks is not protected using the
4326 ASSERT(flags
& XFS_BMAPI_COWFORK
);
4327 if (!(flags
& XFS_BMAPI_COWFORK
)) {
4332 if (eof
|| bno
>= end
)
4337 } else if (isnullstartblock(bma
.got
.br_startblock
)) {
4342 * First, deal with the hole before the allocated space
4343 * that we found, if any.
4345 if ((need_alloc
|| wasdelay
) &&
4346 !(flags
& XFS_BMAPI_CONVERT_ONLY
)) {
4348 bma
.conv
= !!(flags
& XFS_BMAPI_CONVERT
);
4349 bma
.wasdel
= wasdelay
;
4354 * There's a 32/64 bit type mismatch between the
4355 * allocation length request (which can be 64 bits in
4356 * length) and the bma length request, which is
4357 * xfs_extlen_t and therefore 32 bits. Hence we have to
4358 * check for 32-bit overflows and handle them here.
4360 if (len
> (xfs_filblks_t
)MAXEXTLEN
)
4361 bma
.length
= MAXEXTLEN
;
4366 ASSERT(bma
.length
> 0);
4367 error
= xfs_bmapi_allocate(&bma
);
4370 if (bma
.blkno
== NULLFSBLOCK
)
4374 * If this is a CoW allocation, record the data in
4375 * the refcount btree for orphan recovery.
4377 if (whichfork
== XFS_COW_FORK
) {
4378 error
= xfs_refcount_alloc_cow_extent(tp
,
4379 bma
.blkno
, bma
.length
);
4385 /* Deal with the allocated space we found. */
4386 xfs_bmapi_trim_map(mval
, &bma
.got
, &bno
, len
, obno
,
4389 /* Execute unwritten extent conversion if necessary */
4390 error
= xfs_bmapi_convert_unwritten(&bma
, mval
, len
, flags
);
4391 if (error
== -EAGAIN
)
4396 /* update the extent map to return */
4397 xfs_bmapi_update_map(&mval
, &bno
, &len
, obno
, end
, &n
, flags
);
4400 * If we're done, stop now. Stop when we've allocated
4401 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4402 * the transaction may get too big.
4404 if (bno
>= end
|| n
>= *nmap
|| bma
.nallocs
>= *nmap
)
4407 /* Else go on to the next record. */
4409 if (!xfs_iext_next_extent(ifp
, &bma
.icur
, &bma
.got
))
4415 * Transform from btree to extents, give it cur.
4417 if (xfs_bmap_wants_extents(ip
, whichfork
)) {
4418 int tmp_logflags
= 0;
4421 error
= xfs_bmap_btree_to_extents(tp
, ip
, bma
.cur
,
4422 &tmp_logflags
, whichfork
);
4423 bma
.logflags
|= tmp_logflags
;
4428 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
||
4429 XFS_IFORK_NEXTENTS(ip
, whichfork
) >
4430 XFS_IFORK_MAXEXT(ip
, whichfork
));
4434 * Log everything. Do this after conversion, there's no point in
4435 * logging the extent records if we've converted to btree format.
4437 if ((bma
.logflags
& xfs_ilog_fext(whichfork
)) &&
4438 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
4439 bma
.logflags
&= ~xfs_ilog_fext(whichfork
);
4440 else if ((bma
.logflags
& xfs_ilog_fbroot(whichfork
)) &&
4441 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)
4442 bma
.logflags
&= ~xfs_ilog_fbroot(whichfork
);
4444 * Log whatever the flags say, even if error. Otherwise we might miss
4445 * detecting a case where the data is changed, there's an error,
4446 * and it's not logged so we don't shutdown when we should.
4449 xfs_trans_log_inode(tp
, ip
, bma
.logflags
);
4452 xfs_btree_del_cursor(bma
.cur
, error
);
4455 xfs_bmap_validate_ret(orig_bno
, orig_len
, orig_flags
, orig_mval
,
4462 struct xfs_trans
*tp
,
4463 struct xfs_inode
*ip
,
4466 xfs_fsblock_t startblock
,
4469 struct xfs_mount
*mp
= ip
->i_mount
;
4470 struct xfs_ifork
*ifp
;
4471 struct xfs_btree_cur
*cur
= NULL
;
4472 struct xfs_bmbt_irec got
;
4473 struct xfs_iext_cursor icur
;
4474 int whichfork
= xfs_bmapi_whichfork(flags
);
4475 int logflags
= 0, error
;
4477 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4479 ASSERT(len
<= (xfs_filblks_t
)MAXEXTLEN
);
4480 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
4481 ASSERT(!(flags
& ~(XFS_BMAPI_ATTRFORK
| XFS_BMAPI_PREALLOC
|
4482 XFS_BMAPI_NORMAP
)));
4483 ASSERT((flags
& (XFS_BMAPI_ATTRFORK
| XFS_BMAPI_PREALLOC
)) !=
4484 (XFS_BMAPI_ATTRFORK
| XFS_BMAPI_PREALLOC
));
4486 if (unlikely(XFS_TEST_ERROR(
4487 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
4488 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
4489 mp
, XFS_ERRTAG_BMAPIFORMAT
))) {
4490 XFS_ERROR_REPORT("xfs_bmapi_remap", XFS_ERRLEVEL_LOW
, mp
);
4491 return -EFSCORRUPTED
;
4494 if (XFS_FORCED_SHUTDOWN(mp
))
4497 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
4498 error
= xfs_iread_extents(tp
, ip
, whichfork
);
4503 if (xfs_iext_lookup_extent(ip
, ifp
, bno
, &icur
, &got
)) {
4504 /* make sure we only reflink into a hole. */
4505 ASSERT(got
.br_startoff
> bno
);
4506 ASSERT(got
.br_startoff
- bno
>= len
);
4509 ip
->i_d
.di_nblocks
+= len
;
4510 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
4512 if (ifp
->if_flags
& XFS_IFBROOT
) {
4513 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
4514 cur
->bc_private
.b
.flags
= 0;
4517 got
.br_startoff
= bno
;
4518 got
.br_startblock
= startblock
;
4519 got
.br_blockcount
= len
;
4520 if (flags
& XFS_BMAPI_PREALLOC
)
4521 got
.br_state
= XFS_EXT_UNWRITTEN
;
4523 got
.br_state
= XFS_EXT_NORM
;
4525 error
= xfs_bmap_add_extent_hole_real(tp
, ip
, whichfork
, &icur
,
4526 &cur
, &got
, &logflags
, flags
);
4530 if (xfs_bmap_wants_extents(ip
, whichfork
)) {
4531 int tmp_logflags
= 0;
4533 error
= xfs_bmap_btree_to_extents(tp
, ip
, cur
,
4534 &tmp_logflags
, whichfork
);
4535 logflags
|= tmp_logflags
;
4539 if (ip
->i_d
.di_format
!= XFS_DINODE_FMT_EXTENTS
)
4540 logflags
&= ~XFS_ILOG_DEXT
;
4541 else if (ip
->i_d
.di_format
!= XFS_DINODE_FMT_BTREE
)
4542 logflags
&= ~XFS_ILOG_DBROOT
;
4545 xfs_trans_log_inode(tp
, ip
, logflags
);
4547 xfs_btree_del_cursor(cur
, error
);
4552 * When a delalloc extent is split (e.g., due to a hole punch), the original
4553 * indlen reservation must be shared across the two new extents that are left
4556 * Given the original reservation and the worst case indlen for the two new
4557 * extents (as calculated by xfs_bmap_worst_indlen()), split the original
4558 * reservation fairly across the two new extents. If necessary, steal available
4559 * blocks from a deleted extent to make up a reservation deficiency (e.g., if
4560 * ores == 1). The number of stolen blocks is returned. The availability and
4561 * subsequent accounting of stolen blocks is the responsibility of the caller.
4563 static xfs_filblks_t
4564 xfs_bmap_split_indlen(
4565 xfs_filblks_t ores
, /* original res. */
4566 xfs_filblks_t
*indlen1
, /* ext1 worst indlen */
4567 xfs_filblks_t
*indlen2
, /* ext2 worst indlen */
4568 xfs_filblks_t avail
) /* stealable blocks */
4570 xfs_filblks_t len1
= *indlen1
;
4571 xfs_filblks_t len2
= *indlen2
;
4572 xfs_filblks_t nres
= len1
+ len2
; /* new total res. */
4573 xfs_filblks_t stolen
= 0;
4574 xfs_filblks_t resfactor
;
4577 * Steal as many blocks as we can to try and satisfy the worst case
4578 * indlen for both new extents.
4580 if (ores
< nres
&& avail
)
4581 stolen
= XFS_FILBLKS_MIN(nres
- ores
, avail
);
4584 /* nothing else to do if we've satisfied the new reservation */
4589 * We can't meet the total required reservation for the two extents.
4590 * Calculate the percent of the overall shortage between both extents
4591 * and apply this percentage to each of the requested indlen values.
4592 * This distributes the shortage fairly and reduces the chances that one
4593 * of the two extents is left with nothing when extents are repeatedly
4596 resfactor
= (ores
* 100);
4597 do_div(resfactor
, nres
);
4602 ASSERT(len1
+ len2
<= ores
);
4603 ASSERT(len1
< *indlen1
&& len2
< *indlen2
);
4606 * Hand out the remainder to each extent. If one of the two reservations
4607 * is zero, we want to make sure that one gets a block first. The loop
4608 * below starts with len1, so hand len2 a block right off the bat if it
4611 ores
-= (len1
+ len2
);
4612 ASSERT((*indlen1
- len1
) + (*indlen2
- len2
) >= ores
);
4613 if (ores
&& !len2
&& *indlen2
) {
4618 if (len1
< *indlen1
) {
4624 if (len2
< *indlen2
) {
4637 xfs_bmap_del_extent_delay(
4638 struct xfs_inode
*ip
,
4640 struct xfs_iext_cursor
*icur
,
4641 struct xfs_bmbt_irec
*got
,
4642 struct xfs_bmbt_irec
*del
)
4644 struct xfs_mount
*mp
= ip
->i_mount
;
4645 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4646 struct xfs_bmbt_irec
new;
4647 int64_t da_old
, da_new
, da_diff
= 0;
4648 xfs_fileoff_t del_endoff
, got_endoff
;
4649 xfs_filblks_t got_indlen
, new_indlen
, stolen
;
4650 int state
= xfs_bmap_fork_to_state(whichfork
);
4654 XFS_STATS_INC(mp
, xs_del_exlist
);
4656 isrt
= (whichfork
== XFS_DATA_FORK
) && XFS_IS_REALTIME_INODE(ip
);
4657 del_endoff
= del
->br_startoff
+ del
->br_blockcount
;
4658 got_endoff
= got
->br_startoff
+ got
->br_blockcount
;
4659 da_old
= startblockval(got
->br_startblock
);
4662 ASSERT(del
->br_blockcount
> 0);
4663 ASSERT(got
->br_startoff
<= del
->br_startoff
);
4664 ASSERT(got_endoff
>= del_endoff
);
4667 uint64_t rtexts
= XFS_FSB_TO_B(mp
, del
->br_blockcount
);
4669 do_div(rtexts
, mp
->m_sb
.sb_rextsize
);
4670 xfs_mod_frextents(mp
, rtexts
);
4674 * Update the inode delalloc counter now and wait to update the
4675 * sb counters as we might have to borrow some blocks for the
4676 * indirect block accounting.
4678 error
= xfs_trans_reserve_quota_nblks(NULL
, ip
,
4679 -((long)del
->br_blockcount
), 0,
4680 isrt
? XFS_QMOPT_RES_RTBLKS
: XFS_QMOPT_RES_REGBLKS
);
4683 ip
->i_delayed_blks
-= del
->br_blockcount
;
4685 if (got
->br_startoff
== del
->br_startoff
)
4686 state
|= BMAP_LEFT_FILLING
;
4687 if (got_endoff
== del_endoff
)
4688 state
|= BMAP_RIGHT_FILLING
;
4690 switch (state
& (BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
)) {
4691 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
4693 * Matches the whole extent. Delete the entry.
4695 xfs_iext_remove(ip
, icur
, state
);
4696 xfs_iext_prev(ifp
, icur
);
4698 case BMAP_LEFT_FILLING
:
4700 * Deleting the first part of the extent.
4702 got
->br_startoff
= del_endoff
;
4703 got
->br_blockcount
-= del
->br_blockcount
;
4704 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
,
4705 got
->br_blockcount
), da_old
);
4706 got
->br_startblock
= nullstartblock((int)da_new
);
4707 xfs_iext_update_extent(ip
, state
, icur
, got
);
4709 case BMAP_RIGHT_FILLING
:
4711 * Deleting the last part of the extent.
4713 got
->br_blockcount
= got
->br_blockcount
- del
->br_blockcount
;
4714 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
,
4715 got
->br_blockcount
), da_old
);
4716 got
->br_startblock
= nullstartblock((int)da_new
);
4717 xfs_iext_update_extent(ip
, state
, icur
, got
);
4721 * Deleting the middle of the extent.
4723 * Distribute the original indlen reservation across the two new
4724 * extents. Steal blocks from the deleted extent if necessary.
4725 * Stealing blocks simply fudges the fdblocks accounting below.
4726 * Warn if either of the new indlen reservations is zero as this
4727 * can lead to delalloc problems.
4729 got
->br_blockcount
= del
->br_startoff
- got
->br_startoff
;
4730 got_indlen
= xfs_bmap_worst_indlen(ip
, got
->br_blockcount
);
4732 new.br_blockcount
= got_endoff
- del_endoff
;
4733 new_indlen
= xfs_bmap_worst_indlen(ip
, new.br_blockcount
);
4735 WARN_ON_ONCE(!got_indlen
|| !new_indlen
);
4736 stolen
= xfs_bmap_split_indlen(da_old
, &got_indlen
, &new_indlen
,
4737 del
->br_blockcount
);
4739 got
->br_startblock
= nullstartblock((int)got_indlen
);
4741 new.br_startoff
= del_endoff
;
4742 new.br_state
= got
->br_state
;
4743 new.br_startblock
= nullstartblock((int)new_indlen
);
4745 xfs_iext_update_extent(ip
, state
, icur
, got
);
4746 xfs_iext_next(ifp
, icur
);
4747 xfs_iext_insert(ip
, icur
, &new, state
);
4749 da_new
= got_indlen
+ new_indlen
- stolen
;
4750 del
->br_blockcount
-= stolen
;
4754 ASSERT(da_old
>= da_new
);
4755 da_diff
= da_old
- da_new
;
4757 da_diff
+= del
->br_blockcount
;
4759 xfs_mod_fdblocks(mp
, da_diff
, false);
4764 xfs_bmap_del_extent_cow(
4765 struct xfs_inode
*ip
,
4766 struct xfs_iext_cursor
*icur
,
4767 struct xfs_bmbt_irec
*got
,
4768 struct xfs_bmbt_irec
*del
)
4770 struct xfs_mount
*mp
= ip
->i_mount
;
4771 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, XFS_COW_FORK
);
4772 struct xfs_bmbt_irec
new;
4773 xfs_fileoff_t del_endoff
, got_endoff
;
4774 int state
= BMAP_COWFORK
;
4776 XFS_STATS_INC(mp
, xs_del_exlist
);
4778 del_endoff
= del
->br_startoff
+ del
->br_blockcount
;
4779 got_endoff
= got
->br_startoff
+ got
->br_blockcount
;
4781 ASSERT(del
->br_blockcount
> 0);
4782 ASSERT(got
->br_startoff
<= del
->br_startoff
);
4783 ASSERT(got_endoff
>= del_endoff
);
4784 ASSERT(!isnullstartblock(got
->br_startblock
));
4786 if (got
->br_startoff
== del
->br_startoff
)
4787 state
|= BMAP_LEFT_FILLING
;
4788 if (got_endoff
== del_endoff
)
4789 state
|= BMAP_RIGHT_FILLING
;
4791 switch (state
& (BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
)) {
4792 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
4794 * Matches the whole extent. Delete the entry.
4796 xfs_iext_remove(ip
, icur
, state
);
4797 xfs_iext_prev(ifp
, icur
);
4799 case BMAP_LEFT_FILLING
:
4801 * Deleting the first part of the extent.
4803 got
->br_startoff
= del_endoff
;
4804 got
->br_blockcount
-= del
->br_blockcount
;
4805 got
->br_startblock
= del
->br_startblock
+ del
->br_blockcount
;
4806 xfs_iext_update_extent(ip
, state
, icur
, got
);
4808 case BMAP_RIGHT_FILLING
:
4810 * Deleting the last part of the extent.
4812 got
->br_blockcount
-= del
->br_blockcount
;
4813 xfs_iext_update_extent(ip
, state
, icur
, got
);
4817 * Deleting the middle of the extent.
4819 got
->br_blockcount
= del
->br_startoff
- got
->br_startoff
;
4821 new.br_startoff
= del_endoff
;
4822 new.br_blockcount
= got_endoff
- del_endoff
;
4823 new.br_state
= got
->br_state
;
4824 new.br_startblock
= del
->br_startblock
+ del
->br_blockcount
;
4826 xfs_iext_update_extent(ip
, state
, icur
, got
);
4827 xfs_iext_next(ifp
, icur
);
4828 xfs_iext_insert(ip
, icur
, &new, state
);
4831 ip
->i_delayed_blks
-= del
->br_blockcount
;
4835 * Called by xfs_bmapi to update file extent records and the btree
4836 * after removing space.
4838 STATIC
int /* error */
4839 xfs_bmap_del_extent_real(
4840 xfs_inode_t
*ip
, /* incore inode pointer */
4841 xfs_trans_t
*tp
, /* current transaction pointer */
4842 struct xfs_iext_cursor
*icur
,
4843 xfs_btree_cur_t
*cur
, /* if null, not a btree */
4844 xfs_bmbt_irec_t
*del
, /* data to remove from extents */
4845 int *logflagsp
, /* inode logging flags */
4846 int whichfork
, /* data or attr fork */
4847 int bflags
) /* bmapi flags */
4849 xfs_fsblock_t del_endblock
=0; /* first block past del */
4850 xfs_fileoff_t del_endoff
; /* first offset past del */
4851 int do_fx
; /* free extent at end of routine */
4852 int error
; /* error return value */
4853 int flags
= 0;/* inode logging flags */
4854 struct xfs_bmbt_irec got
; /* current extent entry */
4855 xfs_fileoff_t got_endoff
; /* first offset past got */
4856 int i
; /* temp state */
4857 struct xfs_ifork
*ifp
; /* inode fork pointer */
4858 xfs_mount_t
*mp
; /* mount structure */
4859 xfs_filblks_t nblks
; /* quota/sb block count */
4860 xfs_bmbt_irec_t
new; /* new record to be inserted */
4862 uint qfield
; /* quota field to update */
4863 int state
= xfs_bmap_fork_to_state(whichfork
);
4864 struct xfs_bmbt_irec old
;
4867 XFS_STATS_INC(mp
, xs_del_exlist
);
4869 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4870 ASSERT(del
->br_blockcount
> 0);
4871 xfs_iext_get_extent(ifp
, icur
, &got
);
4872 ASSERT(got
.br_startoff
<= del
->br_startoff
);
4873 del_endoff
= del
->br_startoff
+ del
->br_blockcount
;
4874 got_endoff
= got
.br_startoff
+ got
.br_blockcount
;
4875 ASSERT(got_endoff
>= del_endoff
);
4876 ASSERT(!isnullstartblock(got
.br_startblock
));
4881 * If it's the case where the directory code is running with no block
4882 * reservation, and the deleted block is in the middle of its extent,
4883 * and the resulting insert of an extent would cause transformation to
4884 * btree format, then reject it. The calling code will then swap blocks
4885 * around instead. We have to do this now, rather than waiting for the
4886 * conversion to btree format, since the transaction will be dirty then.
4888 if (tp
->t_blk_res
== 0 &&
4889 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
&&
4890 XFS_IFORK_NEXTENTS(ip
, whichfork
) >=
4891 XFS_IFORK_MAXEXT(ip
, whichfork
) &&
4892 del
->br_startoff
> got
.br_startoff
&& del_endoff
< got_endoff
)
4895 flags
= XFS_ILOG_CORE
;
4896 if (whichfork
== XFS_DATA_FORK
&& XFS_IS_REALTIME_INODE(ip
)) {
4901 bno
= div_u64_rem(del
->br_startblock
, mp
->m_sb
.sb_rextsize
,
4904 len
= div_u64_rem(del
->br_blockcount
, mp
->m_sb
.sb_rextsize
,
4908 error
= xfs_rtfree_extent(tp
, bno
, (xfs_extlen_t
)len
);
4912 nblks
= len
* mp
->m_sb
.sb_rextsize
;
4913 qfield
= XFS_TRANS_DQ_RTBCOUNT
;
4916 nblks
= del
->br_blockcount
;
4917 qfield
= XFS_TRANS_DQ_BCOUNT
;
4920 del_endblock
= del
->br_startblock
+ del
->br_blockcount
;
4922 error
= xfs_bmbt_lookup_eq(cur
, &got
, &i
);
4925 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
4928 if (got
.br_startoff
== del
->br_startoff
)
4929 state
|= BMAP_LEFT_FILLING
;
4930 if (got_endoff
== del_endoff
)
4931 state
|= BMAP_RIGHT_FILLING
;
4933 switch (state
& (BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
)) {
4934 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
4936 * Matches the whole extent. Delete the entry.
4938 xfs_iext_remove(ip
, icur
, state
);
4939 xfs_iext_prev(ifp
, icur
);
4940 XFS_IFORK_NEXT_SET(ip
, whichfork
,
4941 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
4942 flags
|= XFS_ILOG_CORE
;
4944 flags
|= xfs_ilog_fext(whichfork
);
4947 if ((error
= xfs_btree_delete(cur
, &i
)))
4949 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
4951 case BMAP_LEFT_FILLING
:
4953 * Deleting the first part of the extent.
4955 got
.br_startoff
= del_endoff
;
4956 got
.br_startblock
= del_endblock
;
4957 got
.br_blockcount
-= del
->br_blockcount
;
4958 xfs_iext_update_extent(ip
, state
, icur
, &got
);
4960 flags
|= xfs_ilog_fext(whichfork
);
4963 error
= xfs_bmbt_update(cur
, &got
);
4967 case BMAP_RIGHT_FILLING
:
4969 * Deleting the last part of the extent.
4971 got
.br_blockcount
-= del
->br_blockcount
;
4972 xfs_iext_update_extent(ip
, state
, icur
, &got
);
4974 flags
|= xfs_ilog_fext(whichfork
);
4977 error
= xfs_bmbt_update(cur
, &got
);
4983 * Deleting the middle of the extent.
4987 got
.br_blockcount
= del
->br_startoff
- got
.br_startoff
;
4988 xfs_iext_update_extent(ip
, state
, icur
, &got
);
4990 new.br_startoff
= del_endoff
;
4991 new.br_blockcount
= got_endoff
- del_endoff
;
4992 new.br_state
= got
.br_state
;
4993 new.br_startblock
= del_endblock
;
4995 flags
|= XFS_ILOG_CORE
;
4997 error
= xfs_bmbt_update(cur
, &got
);
5000 error
= xfs_btree_increment(cur
, 0, &i
);
5003 cur
->bc_rec
.b
= new;
5004 error
= xfs_btree_insert(cur
, &i
);
5005 if (error
&& error
!= -ENOSPC
)
5008 * If get no-space back from btree insert, it tried a
5009 * split, and we have a zero block reservation. Fix up
5010 * our state and return the error.
5012 if (error
== -ENOSPC
) {
5014 * Reset the cursor, don't trust it after any
5017 error
= xfs_bmbt_lookup_eq(cur
, &got
, &i
);
5020 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
5022 * Update the btree record back
5023 * to the original value.
5025 error
= xfs_bmbt_update(cur
, &old
);
5029 * Reset the extent record back
5030 * to the original value.
5032 xfs_iext_update_extent(ip
, state
, icur
, &old
);
5037 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
5039 flags
|= xfs_ilog_fext(whichfork
);
5040 XFS_IFORK_NEXT_SET(ip
, whichfork
,
5041 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
5042 xfs_iext_next(ifp
, icur
);
5043 xfs_iext_insert(ip
, icur
, &new, state
);
5047 /* remove reverse mapping */
5048 error
= xfs_rmap_unmap_extent(tp
, ip
, whichfork
, del
);
5053 * If we need to, add to list of extents to delete.
5055 if (do_fx
&& !(bflags
& XFS_BMAPI_REMAP
)) {
5056 if (xfs_is_reflink_inode(ip
) && whichfork
== XFS_DATA_FORK
) {
5057 error
= xfs_refcount_decrease_extent(tp
, del
);
5061 __xfs_bmap_add_free(tp
, del
->br_startblock
,
5062 del
->br_blockcount
, NULL
,
5063 (bflags
& XFS_BMAPI_NODISCARD
) ||
5064 del
->br_state
== XFS_EXT_UNWRITTEN
);
5069 * Adjust inode # blocks in the file.
5072 ip
->i_d
.di_nblocks
-= nblks
;
5074 * Adjust quota data.
5076 if (qfield
&& !(bflags
& XFS_BMAPI_REMAP
))
5077 xfs_trans_mod_dquot_byino(tp
, ip
, qfield
, (long)-nblks
);
5085 * Unmap (remove) blocks from a file.
5086 * If nexts is nonzero then the number of extents to remove is limited to
5087 * that value. If not all extents in the block range can be removed then
5092 struct xfs_trans
*tp
, /* transaction pointer */
5093 struct xfs_inode
*ip
, /* incore inode */
5094 xfs_fileoff_t start
, /* first file offset deleted */
5095 xfs_filblks_t
*rlen
, /* i/o: amount remaining */
5096 int flags
, /* misc flags */
5097 xfs_extnum_t nexts
) /* number of extents max */
5099 struct xfs_btree_cur
*cur
; /* bmap btree cursor */
5100 struct xfs_bmbt_irec del
; /* extent being deleted */
5101 int error
; /* error return value */
5102 xfs_extnum_t extno
; /* extent number in list */
5103 struct xfs_bmbt_irec got
; /* current extent record */
5104 struct xfs_ifork
*ifp
; /* inode fork pointer */
5105 int isrt
; /* freeing in rt area */
5106 int logflags
; /* transaction logging flags */
5107 xfs_extlen_t mod
; /* rt extent offset */
5108 struct xfs_mount
*mp
; /* mount structure */
5109 int tmp_logflags
; /* partial logging flags */
5110 int wasdel
; /* was a delayed alloc extent */
5111 int whichfork
; /* data or attribute fork */
5113 xfs_filblks_t len
= *rlen
; /* length to unmap in file */
5114 xfs_fileoff_t max_len
;
5115 xfs_agnumber_t prev_agno
= NULLAGNUMBER
, agno
;
5117 struct xfs_iext_cursor icur
;
5120 trace_xfs_bunmap(ip
, start
, len
, flags
, _RET_IP_
);
5122 whichfork
= xfs_bmapi_whichfork(flags
);
5123 ASSERT(whichfork
!= XFS_COW_FORK
);
5124 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5126 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
5127 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)) {
5128 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW
,
5130 return -EFSCORRUPTED
;
5133 if (XFS_FORCED_SHUTDOWN(mp
))
5136 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
5141 * Guesstimate how many blocks we can unmap without running the risk of
5142 * blowing out the transaction with a mix of EFIs and reflink
5145 if (tp
&& xfs_is_reflink_inode(ip
) && whichfork
== XFS_DATA_FORK
)
5146 max_len
= min(len
, xfs_refcount_max_unmap(tp
->t_log_res
));
5150 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
5151 (error
= xfs_iread_extents(tp
, ip
, whichfork
)))
5153 if (xfs_iext_count(ifp
) == 0) {
5157 XFS_STATS_INC(mp
, xs_blk_unmap
);
5158 isrt
= (whichfork
== XFS_DATA_FORK
) && XFS_IS_REALTIME_INODE(ip
);
5161 if (!xfs_iext_lookup_extent_before(ip
, ifp
, &end
, &icur
, &got
)) {
5168 if (ifp
->if_flags
& XFS_IFBROOT
) {
5169 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
);
5170 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
5171 cur
->bc_private
.b
.flags
= 0;
5177 * Synchronize by locking the bitmap inode.
5179 xfs_ilock(mp
->m_rbmip
, XFS_ILOCK_EXCL
|XFS_ILOCK_RTBITMAP
);
5180 xfs_trans_ijoin(tp
, mp
->m_rbmip
, XFS_ILOCK_EXCL
);
5181 xfs_ilock(mp
->m_rsumip
, XFS_ILOCK_EXCL
|XFS_ILOCK_RTSUM
);
5182 xfs_trans_ijoin(tp
, mp
->m_rsumip
, XFS_ILOCK_EXCL
);
5186 while (end
!= (xfs_fileoff_t
)-1 && end
>= start
&&
5187 (nexts
== 0 || extno
< nexts
) && max_len
> 0) {
5189 * Is the found extent after a hole in which end lives?
5190 * Just back up to the previous extent, if so.
5192 if (got
.br_startoff
> end
&&
5193 !xfs_iext_prev_extent(ifp
, &icur
, &got
)) {
5198 * Is the last block of this extent before the range
5199 * we're supposed to delete? If so, we're done.
5201 end
= XFS_FILEOFF_MIN(end
,
5202 got
.br_startoff
+ got
.br_blockcount
- 1);
5206 * Then deal with the (possibly delayed) allocated space
5210 wasdel
= isnullstartblock(del
.br_startblock
);
5213 * Make sure we don't touch multiple AGF headers out of order
5214 * in a single transaction, as that could cause AB-BA deadlocks.
5217 agno
= XFS_FSB_TO_AGNO(mp
, del
.br_startblock
);
5218 if (prev_agno
!= NULLAGNUMBER
&& prev_agno
> agno
)
5222 if (got
.br_startoff
< start
) {
5223 del
.br_startoff
= start
;
5224 del
.br_blockcount
-= start
- got
.br_startoff
;
5226 del
.br_startblock
+= start
- got
.br_startoff
;
5228 if (del
.br_startoff
+ del
.br_blockcount
> end
+ 1)
5229 del
.br_blockcount
= end
+ 1 - del
.br_startoff
;
5231 /* How much can we safely unmap? */
5232 if (max_len
< del
.br_blockcount
) {
5233 del
.br_startoff
+= del
.br_blockcount
- max_len
;
5235 del
.br_startblock
+= del
.br_blockcount
- max_len
;
5236 del
.br_blockcount
= max_len
;
5242 sum
= del
.br_startblock
+ del
.br_blockcount
;
5243 div_u64_rem(sum
, mp
->m_sb
.sb_rextsize
, &mod
);
5246 * Realtime extent not lined up at the end.
5247 * The extent could have been split into written
5248 * and unwritten pieces, or we could just be
5249 * unmapping part of it. But we can't really
5250 * get rid of part of a realtime extent.
5252 if (del
.br_state
== XFS_EXT_UNWRITTEN
) {
5254 * This piece is unwritten, or we're not
5255 * using unwritten extents. Skip over it.
5258 end
-= mod
> del
.br_blockcount
?
5259 del
.br_blockcount
: mod
;
5260 if (end
< got
.br_startoff
&&
5261 !xfs_iext_prev_extent(ifp
, &icur
, &got
)) {
5268 * It's written, turn it unwritten.
5269 * This is better than zeroing it.
5271 ASSERT(del
.br_state
== XFS_EXT_NORM
);
5272 ASSERT(tp
->t_blk_res
> 0);
5274 * If this spans a realtime extent boundary,
5275 * chop it back to the start of the one we end at.
5277 if (del
.br_blockcount
> mod
) {
5278 del
.br_startoff
+= del
.br_blockcount
- mod
;
5279 del
.br_startblock
+= del
.br_blockcount
- mod
;
5280 del
.br_blockcount
= mod
;
5282 del
.br_state
= XFS_EXT_UNWRITTEN
;
5283 error
= xfs_bmap_add_extent_unwritten_real(tp
, ip
,
5284 whichfork
, &icur
, &cur
, &del
,
5290 div_u64_rem(del
.br_startblock
, mp
->m_sb
.sb_rextsize
, &mod
);
5293 * Realtime extent is lined up at the end but not
5294 * at the front. We'll get rid of full extents if
5297 mod
= mp
->m_sb
.sb_rextsize
- mod
;
5298 if (del
.br_blockcount
> mod
) {
5299 del
.br_blockcount
-= mod
;
5300 del
.br_startoff
+= mod
;
5301 del
.br_startblock
+= mod
;
5302 } else if (del
.br_startoff
== start
&&
5303 (del
.br_state
== XFS_EXT_UNWRITTEN
||
5304 tp
->t_blk_res
== 0)) {
5306 * Can't make it unwritten. There isn't
5307 * a full extent here so just skip it.
5309 ASSERT(end
>= del
.br_blockcount
);
5310 end
-= del
.br_blockcount
;
5311 if (got
.br_startoff
> end
&&
5312 !xfs_iext_prev_extent(ifp
, &icur
, &got
)) {
5317 } else if (del
.br_state
== XFS_EXT_UNWRITTEN
) {
5318 struct xfs_bmbt_irec prev
;
5321 * This one is already unwritten.
5322 * It must have a written left neighbor.
5323 * Unwrite the killed part of that one and
5326 if (!xfs_iext_prev_extent(ifp
, &icur
, &prev
))
5328 ASSERT(prev
.br_state
== XFS_EXT_NORM
);
5329 ASSERT(!isnullstartblock(prev
.br_startblock
));
5330 ASSERT(del
.br_startblock
==
5331 prev
.br_startblock
+ prev
.br_blockcount
);
5332 if (prev
.br_startoff
< start
) {
5333 mod
= start
- prev
.br_startoff
;
5334 prev
.br_blockcount
-= mod
;
5335 prev
.br_startblock
+= mod
;
5336 prev
.br_startoff
= start
;
5338 prev
.br_state
= XFS_EXT_UNWRITTEN
;
5339 error
= xfs_bmap_add_extent_unwritten_real(tp
,
5340 ip
, whichfork
, &icur
, &cur
,
5346 ASSERT(del
.br_state
== XFS_EXT_NORM
);
5347 del
.br_state
= XFS_EXT_UNWRITTEN
;
5348 error
= xfs_bmap_add_extent_unwritten_real(tp
,
5349 ip
, whichfork
, &icur
, &cur
,
5359 error
= xfs_bmap_del_extent_delay(ip
, whichfork
, &icur
,
5362 error
= xfs_bmap_del_extent_real(ip
, tp
, &icur
, cur
,
5363 &del
, &tmp_logflags
, whichfork
,
5365 logflags
|= tmp_logflags
;
5371 max_len
-= del
.br_blockcount
;
5372 end
= del
.br_startoff
- 1;
5375 * If not done go on to the next (previous) record.
5377 if (end
!= (xfs_fileoff_t
)-1 && end
>= start
) {
5378 if (!xfs_iext_get_extent(ifp
, &icur
, &got
) ||
5379 (got
.br_startoff
> end
&&
5380 !xfs_iext_prev_extent(ifp
, &icur
, &got
))) {
5387 if (done
|| end
== (xfs_fileoff_t
)-1 || end
< start
)
5390 *rlen
= end
- start
+ 1;
5393 * Convert to a btree if necessary.
5395 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
5396 ASSERT(cur
== NULL
);
5397 error
= xfs_bmap_extents_to_btree(tp
, ip
, &cur
, 0,
5398 &tmp_logflags
, whichfork
);
5399 logflags
|= tmp_logflags
;
5404 * transform from btree to extents, give it cur
5406 else if (xfs_bmap_wants_extents(ip
, whichfork
)) {
5407 ASSERT(cur
!= NULL
);
5408 error
= xfs_bmap_btree_to_extents(tp
, ip
, cur
, &tmp_logflags
,
5410 logflags
|= tmp_logflags
;
5415 * transform from extents to local?
5420 * Log everything. Do this after conversion, there's no point in
5421 * logging the extent records if we've converted to btree format.
5423 if ((logflags
& xfs_ilog_fext(whichfork
)) &&
5424 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
5425 logflags
&= ~xfs_ilog_fext(whichfork
);
5426 else if ((logflags
& xfs_ilog_fbroot(whichfork
)) &&
5427 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)
5428 logflags
&= ~xfs_ilog_fbroot(whichfork
);
5430 * Log inode even in the error case, if the transaction
5431 * is dirty we'll need to shut down the filesystem.
5434 xfs_trans_log_inode(tp
, ip
, logflags
);
5437 cur
->bc_private
.b
.allocated
= 0;
5438 xfs_btree_del_cursor(cur
, error
);
5443 /* Unmap a range of a file. */
5447 struct xfs_inode
*ip
,
5456 error
= __xfs_bunmapi(tp
, ip
, bno
, &len
, flags
, nexts
);
5462 * Determine whether an extent shift can be accomplished by a merge with the
5463 * extent that precedes the target hole of the shift.
5467 struct xfs_bmbt_irec
*left
, /* preceding extent */
5468 struct xfs_bmbt_irec
*got
, /* current extent to shift */
5469 xfs_fileoff_t shift
) /* shift fsb */
5471 xfs_fileoff_t startoff
;
5473 startoff
= got
->br_startoff
- shift
;
5476 * The extent, once shifted, must be adjacent in-file and on-disk with
5477 * the preceding extent.
5479 if ((left
->br_startoff
+ left
->br_blockcount
!= startoff
) ||
5480 (left
->br_startblock
+ left
->br_blockcount
!= got
->br_startblock
) ||
5481 (left
->br_state
!= got
->br_state
) ||
5482 (left
->br_blockcount
+ got
->br_blockcount
> MAXEXTLEN
))
5489 * A bmap extent shift adjusts the file offset of an extent to fill a preceding
5490 * hole in the file. If an extent shift would result in the extent being fully
5491 * adjacent to the extent that currently precedes the hole, we can merge with
5492 * the preceding extent rather than do the shift.
5494 * This function assumes the caller has verified a shift-by-merge is possible
5495 * with the provided extents via xfs_bmse_can_merge().
5499 struct xfs_trans
*tp
,
5500 struct xfs_inode
*ip
,
5502 xfs_fileoff_t shift
, /* shift fsb */
5503 struct xfs_iext_cursor
*icur
,
5504 struct xfs_bmbt_irec
*got
, /* extent to shift */
5505 struct xfs_bmbt_irec
*left
, /* preceding extent */
5506 struct xfs_btree_cur
*cur
,
5507 int *logflags
) /* output */
5509 struct xfs_bmbt_irec
new;
5510 xfs_filblks_t blockcount
;
5512 struct xfs_mount
*mp
= ip
->i_mount
;
5514 blockcount
= left
->br_blockcount
+ got
->br_blockcount
;
5516 ASSERT(xfs_isilocked(ip
, XFS_IOLOCK_EXCL
));
5517 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
5518 ASSERT(xfs_bmse_can_merge(left
, got
, shift
));
5521 new.br_blockcount
= blockcount
;
5524 * Update the on-disk extent count, the btree if necessary and log the
5527 XFS_IFORK_NEXT_SET(ip
, whichfork
,
5528 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
5529 *logflags
|= XFS_ILOG_CORE
;
5531 *logflags
|= XFS_ILOG_DEXT
;
5535 /* lookup and remove the extent to merge */
5536 error
= xfs_bmbt_lookup_eq(cur
, got
, &i
);
5539 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
5541 error
= xfs_btree_delete(cur
, &i
);
5544 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
5546 /* lookup and update size of the previous extent */
5547 error
= xfs_bmbt_lookup_eq(cur
, left
, &i
);
5550 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
5552 error
= xfs_bmbt_update(cur
, &new);
5557 xfs_iext_remove(ip
, icur
, 0);
5558 xfs_iext_prev(XFS_IFORK_PTR(ip
, whichfork
), icur
);
5559 xfs_iext_update_extent(ip
, xfs_bmap_fork_to_state(whichfork
), icur
,
5562 /* update reverse mapping. rmap functions merge the rmaps for us */
5563 error
= xfs_rmap_unmap_extent(tp
, ip
, whichfork
, got
);
5566 memcpy(&new, got
, sizeof(new));
5567 new.br_startoff
= left
->br_startoff
+ left
->br_blockcount
;
5568 return xfs_rmap_map_extent(tp
, ip
, whichfork
, &new);
5572 xfs_bmap_shift_update_extent(
5573 struct xfs_trans
*tp
,
5574 struct xfs_inode
*ip
,
5576 struct xfs_iext_cursor
*icur
,
5577 struct xfs_bmbt_irec
*got
,
5578 struct xfs_btree_cur
*cur
,
5580 xfs_fileoff_t startoff
)
5582 struct xfs_mount
*mp
= ip
->i_mount
;
5583 struct xfs_bmbt_irec prev
= *got
;
5586 *logflags
|= XFS_ILOG_CORE
;
5588 got
->br_startoff
= startoff
;
5591 error
= xfs_bmbt_lookup_eq(cur
, &prev
, &i
);
5594 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
5596 error
= xfs_bmbt_update(cur
, got
);
5600 *logflags
|= XFS_ILOG_DEXT
;
5603 xfs_iext_update_extent(ip
, xfs_bmap_fork_to_state(whichfork
), icur
,
5606 /* update reverse mapping */
5607 error
= xfs_rmap_unmap_extent(tp
, ip
, whichfork
, &prev
);
5610 return xfs_rmap_map_extent(tp
, ip
, whichfork
, got
);
5614 xfs_bmap_collapse_extents(
5615 struct xfs_trans
*tp
,
5616 struct xfs_inode
*ip
,
5617 xfs_fileoff_t
*next_fsb
,
5618 xfs_fileoff_t offset_shift_fsb
,
5621 int whichfork
= XFS_DATA_FORK
;
5622 struct xfs_mount
*mp
= ip
->i_mount
;
5623 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5624 struct xfs_btree_cur
*cur
= NULL
;
5625 struct xfs_bmbt_irec got
, prev
;
5626 struct xfs_iext_cursor icur
;
5627 xfs_fileoff_t new_startoff
;
5631 if (unlikely(XFS_TEST_ERROR(
5632 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
5633 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
5634 mp
, XFS_ERRTAG_BMAPIFORMAT
))) {
5635 XFS_ERROR_REPORT(__func__
, XFS_ERRLEVEL_LOW
, mp
);
5636 return -EFSCORRUPTED
;
5639 if (XFS_FORCED_SHUTDOWN(mp
))
5642 ASSERT(xfs_isilocked(ip
, XFS_IOLOCK_EXCL
| XFS_ILOCK_EXCL
));
5644 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
5645 error
= xfs_iread_extents(tp
, ip
, whichfork
);
5650 if (ifp
->if_flags
& XFS_IFBROOT
) {
5651 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
5652 cur
->bc_private
.b
.flags
= 0;
5655 if (!xfs_iext_lookup_extent(ip
, ifp
, *next_fsb
, &icur
, &got
)) {
5659 XFS_WANT_CORRUPTED_GOTO(mp
, !isnullstartblock(got
.br_startblock
),
5662 new_startoff
= got
.br_startoff
- offset_shift_fsb
;
5663 if (xfs_iext_peek_prev_extent(ifp
, &icur
, &prev
)) {
5664 if (new_startoff
< prev
.br_startoff
+ prev
.br_blockcount
) {
5669 if (xfs_bmse_can_merge(&prev
, &got
, offset_shift_fsb
)) {
5670 error
= xfs_bmse_merge(tp
, ip
, whichfork
,
5671 offset_shift_fsb
, &icur
, &got
, &prev
,
5678 if (got
.br_startoff
< offset_shift_fsb
) {
5684 error
= xfs_bmap_shift_update_extent(tp
, ip
, whichfork
, &icur
, &got
,
5685 cur
, &logflags
, new_startoff
);
5690 if (!xfs_iext_next_extent(ifp
, &icur
, &got
)) {
5695 *next_fsb
= got
.br_startoff
;
5698 xfs_btree_del_cursor(cur
, error
);
5700 xfs_trans_log_inode(tp
, ip
, logflags
);
5704 /* Make sure we won't be right-shifting an extent past the maximum bound. */
5706 xfs_bmap_can_insert_extents(
5707 struct xfs_inode
*ip
,
5709 xfs_fileoff_t shift
)
5711 struct xfs_bmbt_irec got
;
5715 ASSERT(xfs_isilocked(ip
, XFS_IOLOCK_EXCL
));
5717 if (XFS_FORCED_SHUTDOWN(ip
->i_mount
))
5720 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
5721 error
= xfs_bmap_last_extent(NULL
, ip
, XFS_DATA_FORK
, &got
, &is_empty
);
5722 if (!error
&& !is_empty
&& got
.br_startoff
>= off
&&
5723 ((got
.br_startoff
+ shift
) & BMBT_STARTOFF_MASK
) < got
.br_startoff
)
5725 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
5731 xfs_bmap_insert_extents(
5732 struct xfs_trans
*tp
,
5733 struct xfs_inode
*ip
,
5734 xfs_fileoff_t
*next_fsb
,
5735 xfs_fileoff_t offset_shift_fsb
,
5737 xfs_fileoff_t stop_fsb
)
5739 int whichfork
= XFS_DATA_FORK
;
5740 struct xfs_mount
*mp
= ip
->i_mount
;
5741 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5742 struct xfs_btree_cur
*cur
= NULL
;
5743 struct xfs_bmbt_irec got
, next
;
5744 struct xfs_iext_cursor icur
;
5745 xfs_fileoff_t new_startoff
;
5749 if (unlikely(XFS_TEST_ERROR(
5750 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
5751 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
5752 mp
, XFS_ERRTAG_BMAPIFORMAT
))) {
5753 XFS_ERROR_REPORT(__func__
, XFS_ERRLEVEL_LOW
, mp
);
5754 return -EFSCORRUPTED
;
5757 if (XFS_FORCED_SHUTDOWN(mp
))
5760 ASSERT(xfs_isilocked(ip
, XFS_IOLOCK_EXCL
| XFS_ILOCK_EXCL
));
5762 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
5763 error
= xfs_iread_extents(tp
, ip
, whichfork
);
5768 if (ifp
->if_flags
& XFS_IFBROOT
) {
5769 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
5770 cur
->bc_private
.b
.flags
= 0;
5773 if (*next_fsb
== NULLFSBLOCK
) {
5774 xfs_iext_last(ifp
, &icur
);
5775 if (!xfs_iext_get_extent(ifp
, &icur
, &got
) ||
5776 stop_fsb
> got
.br_startoff
) {
5781 if (!xfs_iext_lookup_extent(ip
, ifp
, *next_fsb
, &icur
, &got
)) {
5786 XFS_WANT_CORRUPTED_GOTO(mp
, !isnullstartblock(got
.br_startblock
),
5789 if (stop_fsb
>= got
.br_startoff
+ got
.br_blockcount
) {
5794 new_startoff
= got
.br_startoff
+ offset_shift_fsb
;
5795 if (xfs_iext_peek_next_extent(ifp
, &icur
, &next
)) {
5796 if (new_startoff
+ got
.br_blockcount
> next
.br_startoff
) {
5802 * Unlike a left shift (which involves a hole punch), a right
5803 * shift does not modify extent neighbors in any way. We should
5804 * never find mergeable extents in this scenario. Check anyways
5805 * and warn if we encounter two extents that could be one.
5807 if (xfs_bmse_can_merge(&got
, &next
, offset_shift_fsb
))
5811 error
= xfs_bmap_shift_update_extent(tp
, ip
, whichfork
, &icur
, &got
,
5812 cur
, &logflags
, new_startoff
);
5816 if (!xfs_iext_prev_extent(ifp
, &icur
, &got
) ||
5817 stop_fsb
>= got
.br_startoff
+ got
.br_blockcount
) {
5822 *next_fsb
= got
.br_startoff
;
5825 xfs_btree_del_cursor(cur
, error
);
5827 xfs_trans_log_inode(tp
, ip
, logflags
);
5832 * Splits an extent into two extents at split_fsb block such that it is the
5833 * first block of the current_ext. @ext is a target extent to be split.
5834 * @split_fsb is a block where the extents is split. If split_fsb lies in a
5835 * hole or the first block of extents, just return 0.
5838 xfs_bmap_split_extent_at(
5839 struct xfs_trans
*tp
,
5840 struct xfs_inode
*ip
,
5841 xfs_fileoff_t split_fsb
)
5843 int whichfork
= XFS_DATA_FORK
;
5844 struct xfs_btree_cur
*cur
= NULL
;
5845 struct xfs_bmbt_irec got
;
5846 struct xfs_bmbt_irec
new; /* split extent */
5847 struct xfs_mount
*mp
= ip
->i_mount
;
5848 struct xfs_ifork
*ifp
;
5849 xfs_fsblock_t gotblkcnt
; /* new block count for got */
5850 struct xfs_iext_cursor icur
;
5855 if (unlikely(XFS_TEST_ERROR(
5856 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
5857 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
5858 mp
, XFS_ERRTAG_BMAPIFORMAT
))) {
5859 XFS_ERROR_REPORT("xfs_bmap_split_extent_at",
5860 XFS_ERRLEVEL_LOW
, mp
);
5861 return -EFSCORRUPTED
;
5864 if (XFS_FORCED_SHUTDOWN(mp
))
5867 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5868 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
5869 /* Read in all the extents */
5870 error
= xfs_iread_extents(tp
, ip
, whichfork
);
5876 * If there are not extents, or split_fsb lies in a hole we are done.
5878 if (!xfs_iext_lookup_extent(ip
, ifp
, split_fsb
, &icur
, &got
) ||
5879 got
.br_startoff
>= split_fsb
)
5882 gotblkcnt
= split_fsb
- got
.br_startoff
;
5883 new.br_startoff
= split_fsb
;
5884 new.br_startblock
= got
.br_startblock
+ gotblkcnt
;
5885 new.br_blockcount
= got
.br_blockcount
- gotblkcnt
;
5886 new.br_state
= got
.br_state
;
5888 if (ifp
->if_flags
& XFS_IFBROOT
) {
5889 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
5890 cur
->bc_private
.b
.flags
= 0;
5891 error
= xfs_bmbt_lookup_eq(cur
, &got
, &i
);
5894 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, del_cursor
);
5897 got
.br_blockcount
= gotblkcnt
;
5898 xfs_iext_update_extent(ip
, xfs_bmap_fork_to_state(whichfork
), &icur
,
5901 logflags
= XFS_ILOG_CORE
;
5903 error
= xfs_bmbt_update(cur
, &got
);
5907 logflags
|= XFS_ILOG_DEXT
;
5909 /* Add new extent */
5910 xfs_iext_next(ifp
, &icur
);
5911 xfs_iext_insert(ip
, &icur
, &new, 0);
5912 XFS_IFORK_NEXT_SET(ip
, whichfork
,
5913 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
5916 error
= xfs_bmbt_lookup_eq(cur
, &new, &i
);
5919 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, del_cursor
);
5920 error
= xfs_btree_insert(cur
, &i
);
5923 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, del_cursor
);
5927 * Convert to a btree if necessary.
5929 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
5930 int tmp_logflags
; /* partial log flag return val */
5932 ASSERT(cur
== NULL
);
5933 error
= xfs_bmap_extents_to_btree(tp
, ip
, &cur
, 0,
5934 &tmp_logflags
, whichfork
);
5935 logflags
|= tmp_logflags
;
5940 cur
->bc_private
.b
.allocated
= 0;
5941 xfs_btree_del_cursor(cur
, error
);
5945 xfs_trans_log_inode(tp
, ip
, logflags
);
5950 xfs_bmap_split_extent(
5951 struct xfs_inode
*ip
,
5952 xfs_fileoff_t split_fsb
)
5954 struct xfs_mount
*mp
= ip
->i_mount
;
5955 struct xfs_trans
*tp
;
5958 error
= xfs_trans_alloc(mp
, &M_RES(mp
)->tr_write
,
5959 XFS_DIOSTRAT_SPACE_RES(mp
, 0), 0, 0, &tp
);
5963 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
5964 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
);
5966 error
= xfs_bmap_split_extent_at(tp
, ip
, split_fsb
);
5970 return xfs_trans_commit(tp
);
5973 xfs_trans_cancel(tp
);
5977 /* Deferred mapping is only for real extents in the data fork. */
5979 xfs_bmap_is_update_needed(
5980 struct xfs_bmbt_irec
*bmap
)
5982 return bmap
->br_startblock
!= HOLESTARTBLOCK
&&
5983 bmap
->br_startblock
!= DELAYSTARTBLOCK
;
5986 /* Record a bmap intent. */
5989 struct xfs_trans
*tp
,
5990 enum xfs_bmap_intent_type type
,
5991 struct xfs_inode
*ip
,
5993 struct xfs_bmbt_irec
*bmap
)
5995 struct xfs_bmap_intent
*bi
;
5997 trace_xfs_bmap_defer(tp
->t_mountp
,
5998 XFS_FSB_TO_AGNO(tp
->t_mountp
, bmap
->br_startblock
),
6000 XFS_FSB_TO_AGBNO(tp
->t_mountp
, bmap
->br_startblock
),
6001 ip
->i_ino
, whichfork
,
6003 bmap
->br_blockcount
,
6006 bi
= kmem_alloc(sizeof(struct xfs_bmap_intent
), KM_SLEEP
| KM_NOFS
);
6007 INIT_LIST_HEAD(&bi
->bi_list
);
6010 bi
->bi_whichfork
= whichfork
;
6011 bi
->bi_bmap
= *bmap
;
6013 xfs_defer_add(tp
, XFS_DEFER_OPS_TYPE_BMAP
, &bi
->bi_list
);
6017 /* Map an extent into a file. */
6019 xfs_bmap_map_extent(
6020 struct xfs_trans
*tp
,
6021 struct xfs_inode
*ip
,
6022 struct xfs_bmbt_irec
*PREV
)
6024 if (!xfs_bmap_is_update_needed(PREV
))
6027 return __xfs_bmap_add(tp
, XFS_BMAP_MAP
, ip
, XFS_DATA_FORK
, PREV
);
6030 /* Unmap an extent out of a file. */
6032 xfs_bmap_unmap_extent(
6033 struct xfs_trans
*tp
,
6034 struct xfs_inode
*ip
,
6035 struct xfs_bmbt_irec
*PREV
)
6037 if (!xfs_bmap_is_update_needed(PREV
))
6040 return __xfs_bmap_add(tp
, XFS_BMAP_UNMAP
, ip
, XFS_DATA_FORK
, PREV
);
6044 * Process one of the deferred bmap operations. We pass back the
6045 * btree cursor to maintain our lock on the bmapbt between calls.
6048 xfs_bmap_finish_one(
6049 struct xfs_trans
*tp
,
6050 struct xfs_inode
*ip
,
6051 enum xfs_bmap_intent_type type
,
6053 xfs_fileoff_t startoff
,
6054 xfs_fsblock_t startblock
,
6055 xfs_filblks_t
*blockcount
,
6060 ASSERT(tp
->t_firstblock
== NULLFSBLOCK
);
6062 trace_xfs_bmap_deferred(tp
->t_mountp
,
6063 XFS_FSB_TO_AGNO(tp
->t_mountp
, startblock
), type
,
6064 XFS_FSB_TO_AGBNO(tp
->t_mountp
, startblock
),
6065 ip
->i_ino
, whichfork
, startoff
, *blockcount
, state
);
6067 if (WARN_ON_ONCE(whichfork
!= XFS_DATA_FORK
))
6068 return -EFSCORRUPTED
;
6070 if (XFS_TEST_ERROR(false, tp
->t_mountp
,
6071 XFS_ERRTAG_BMAP_FINISH_ONE
))
6076 error
= xfs_bmapi_remap(tp
, ip
, startoff
, *blockcount
,
6080 case XFS_BMAP_UNMAP
:
6081 error
= __xfs_bunmapi(tp
, ip
, startoff
, blockcount
,
6082 XFS_BMAPI_REMAP
, 1);
6086 error
= -EFSCORRUPTED
;
6092 /* Check that an inode's extent does not have invalid flags or bad ranges. */
6094 xfs_bmap_validate_extent(
6095 struct xfs_inode
*ip
,
6097 struct xfs_bmbt_irec
*irec
)
6099 struct xfs_mount
*mp
= ip
->i_mount
;
6100 xfs_fsblock_t endfsb
;
6103 isrt
= XFS_IS_REALTIME_INODE(ip
);
6104 endfsb
= irec
->br_startblock
+ irec
->br_blockcount
- 1;
6106 if (!xfs_verify_rtbno(mp
, irec
->br_startblock
))
6107 return __this_address
;
6108 if (!xfs_verify_rtbno(mp
, endfsb
))
6109 return __this_address
;
6111 if (!xfs_verify_fsbno(mp
, irec
->br_startblock
))
6112 return __this_address
;
6113 if (!xfs_verify_fsbno(mp
, endfsb
))
6114 return __this_address
;
6115 if (XFS_FSB_TO_AGNO(mp
, irec
->br_startblock
) !=
6116 XFS_FSB_TO_AGNO(mp
, endfsb
))
6117 return __this_address
;
6119 if (irec
->br_state
!= XFS_EXT_NORM
&& whichfork
!= XFS_DATA_FORK
)
6120 return __this_address
;