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 xfs_ifork_t
*ifp
; /* fork structure */
321 int level
; /* btree level, for checking */
322 xfs_mount_t
*mp
; /* file system mount structure */
323 __be64
*pp
; /* pointer to block address */
324 xfs_bmbt_rec_t
*ep
; /* pointer to current extent */
325 xfs_bmbt_rec_t last
= {0, 0}; /* last extent in prev block */
326 xfs_bmbt_rec_t
*nextp
; /* pointer to next extent */
329 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
) {
333 /* skip large extent count inodes */
334 if (ip
->i_d
.di_nextents
> 10000)
339 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
340 block
= ifp
->if_broot
;
342 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
344 level
= be16_to_cpu(block
->bb_level
);
346 xfs_check_block(block
, mp
, 1, ifp
->if_broot_bytes
);
347 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, 1, ifp
->if_broot_bytes
);
348 bno
= be64_to_cpu(*pp
);
350 ASSERT(bno
!= NULLFSBLOCK
);
351 ASSERT(XFS_FSB_TO_AGNO(mp
, bno
) < mp
->m_sb
.sb_agcount
);
352 ASSERT(XFS_FSB_TO_AGBNO(mp
, bno
) < mp
->m_sb
.sb_agblocks
);
355 * Go down the tree until leaf level is reached, following the first
356 * pointer (leftmost) at each level.
358 while (level
-- > 0) {
359 /* See if buf is in cur first */
361 bp
= xfs_bmap_get_bp(cur
, XFS_FSB_TO_DADDR(mp
, bno
));
364 error
= xfs_btree_read_bufl(mp
, NULL
, bno
, 0, &bp
,
370 block
= XFS_BUF_TO_BLOCK(bp
);
375 * Check this block for basic sanity (increasing keys and
376 * no duplicate blocks).
379 xfs_check_block(block
, mp
, 0, 0);
380 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, mp
->m_bmap_dmxr
[1]);
381 bno
= be64_to_cpu(*pp
);
382 XFS_WANT_CORRUPTED_GOTO(mp
,
383 xfs_verify_fsbno(mp
, bno
), error0
);
386 xfs_trans_brelse(NULL
, bp
);
391 * Here with bp and block set to the leftmost leaf node in the tree.
396 * Loop over all leaf nodes checking that all extents are in the right order.
399 xfs_fsblock_t nextbno
;
400 xfs_extnum_t num_recs
;
403 num_recs
= xfs_btree_get_numrecs(block
);
406 * Read-ahead the next leaf block, if any.
409 nextbno
= be64_to_cpu(block
->bb_u
.l
.bb_rightsib
);
412 * Check all the extents to make sure they are OK.
413 * If we had a previous block, the last entry should
414 * conform with the first entry in this one.
417 ep
= XFS_BMBT_REC_ADDR(mp
, block
, 1);
419 ASSERT(xfs_bmbt_disk_get_startoff(&last
) +
420 xfs_bmbt_disk_get_blockcount(&last
) <=
421 xfs_bmbt_disk_get_startoff(ep
));
423 for (j
= 1; j
< num_recs
; j
++) {
424 nextp
= XFS_BMBT_REC_ADDR(mp
, block
, j
+ 1);
425 ASSERT(xfs_bmbt_disk_get_startoff(ep
) +
426 xfs_bmbt_disk_get_blockcount(ep
) <=
427 xfs_bmbt_disk_get_startoff(nextp
));
435 xfs_trans_brelse(NULL
, bp
);
439 * If we've reached the end, stop.
441 if (bno
== NULLFSBLOCK
)
445 bp
= xfs_bmap_get_bp(cur
, XFS_FSB_TO_DADDR(mp
, bno
));
448 error
= xfs_btree_read_bufl(mp
, NULL
, bno
, 0, &bp
,
454 block
= XFS_BUF_TO_BLOCK(bp
);
460 xfs_warn(mp
, "%s: at error0", __func__
);
462 xfs_trans_brelse(NULL
, bp
);
464 xfs_warn(mp
, "%s: BAD after btree leaves for %d extents",
466 xfs_err(mp
, "%s: CORRUPTED BTREE OR SOMETHING", __func__
);
467 xfs_force_shutdown(mp
, SHUTDOWN_CORRUPT_INCORE
);
472 * Validate that the bmbt_irecs being returned from bmapi are valid
473 * given the caller's original parameters. Specifically check the
474 * ranges of the returned irecs to ensure that they only extend beyond
475 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
478 xfs_bmap_validate_ret(
482 xfs_bmbt_irec_t
*mval
,
486 int i
; /* index to map values */
488 ASSERT(ret_nmap
<= nmap
);
490 for (i
= 0; i
< ret_nmap
; i
++) {
491 ASSERT(mval
[i
].br_blockcount
> 0);
492 if (!(flags
& XFS_BMAPI_ENTIRE
)) {
493 ASSERT(mval
[i
].br_startoff
>= bno
);
494 ASSERT(mval
[i
].br_blockcount
<= len
);
495 ASSERT(mval
[i
].br_startoff
+ mval
[i
].br_blockcount
<=
498 ASSERT(mval
[i
].br_startoff
< bno
+ len
);
499 ASSERT(mval
[i
].br_startoff
+ mval
[i
].br_blockcount
>
503 mval
[i
- 1].br_startoff
+ mval
[i
- 1].br_blockcount
==
504 mval
[i
].br_startoff
);
505 ASSERT(mval
[i
].br_startblock
!= DELAYSTARTBLOCK
&&
506 mval
[i
].br_startblock
!= HOLESTARTBLOCK
);
507 ASSERT(mval
[i
].br_state
== XFS_EXT_NORM
||
508 mval
[i
].br_state
== XFS_EXT_UNWRITTEN
);
513 #define xfs_bmap_check_leaf_extents(cur, ip, whichfork) do { } while (0)
514 #define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap) do { } while (0)
518 * bmap free list manipulation functions
522 * Add the extent to the list of extents to be free at transaction end.
523 * The list is maintained sorted (by block number).
527 struct xfs_mount
*mp
,
528 struct xfs_defer_ops
*dfops
,
531 struct xfs_owner_info
*oinfo
,
534 struct xfs_extent_free_item
*new; /* new element */
539 ASSERT(bno
!= NULLFSBLOCK
);
541 ASSERT(len
<= MAXEXTLEN
);
542 ASSERT(!isnullstartblock(bno
));
543 agno
= XFS_FSB_TO_AGNO(mp
, bno
);
544 agbno
= XFS_FSB_TO_AGBNO(mp
, bno
);
545 ASSERT(agno
< mp
->m_sb
.sb_agcount
);
546 ASSERT(agbno
< mp
->m_sb
.sb_agblocks
);
547 ASSERT(len
< mp
->m_sb
.sb_agblocks
);
548 ASSERT(agbno
+ len
<= mp
->m_sb
.sb_agblocks
);
550 ASSERT(xfs_bmap_free_item_zone
!= NULL
);
552 new = kmem_zone_alloc(xfs_bmap_free_item_zone
, KM_SLEEP
);
553 new->xefi_startblock
= bno
;
554 new->xefi_blockcount
= (xfs_extlen_t
)len
;
556 new->xefi_oinfo
= *oinfo
;
558 xfs_rmap_skip_owner_update(&new->xefi_oinfo
);
559 new->xefi_skip_discard
= skip_discard
;
560 trace_xfs_bmap_free_defer(mp
, XFS_FSB_TO_AGNO(mp
, bno
), 0,
561 XFS_FSB_TO_AGBNO(mp
, bno
), len
);
562 xfs_defer_add(dfops
, XFS_DEFER_OPS_TYPE_FREE
, &new->xefi_list
);
566 * Inode fork format manipulation functions
570 * Transform a btree format file with only one leaf node, where the
571 * extents list will fit in the inode, into an extents format file.
572 * Since the file extents are already in-core, all we have to do is
573 * give up the space for the btree root and pitch the leaf block.
575 STATIC
int /* error */
576 xfs_bmap_btree_to_extents(
577 xfs_trans_t
*tp
, /* transaction pointer */
578 xfs_inode_t
*ip
, /* incore inode pointer */
579 xfs_btree_cur_t
*cur
, /* btree cursor */
580 int *logflagsp
, /* inode logging flags */
581 int whichfork
) /* data or attr fork */
584 struct xfs_btree_block
*cblock
;/* child btree block */
585 xfs_fsblock_t cbno
; /* child block number */
586 xfs_buf_t
*cbp
; /* child block's buffer */
587 int error
; /* error return value */
588 xfs_ifork_t
*ifp
; /* inode fork data */
589 xfs_mount_t
*mp
; /* mount point structure */
590 __be64
*pp
; /* ptr to block address */
591 struct xfs_btree_block
*rblock
;/* root btree block */
592 struct xfs_owner_info oinfo
;
595 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
596 ASSERT(whichfork
!= XFS_COW_FORK
);
597 ASSERT(ifp
->if_flags
& XFS_IFEXTENTS
);
598 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
);
599 rblock
= ifp
->if_broot
;
600 ASSERT(be16_to_cpu(rblock
->bb_level
) == 1);
601 ASSERT(be16_to_cpu(rblock
->bb_numrecs
) == 1);
602 ASSERT(xfs_bmbt_maxrecs(mp
, ifp
->if_broot_bytes
, 0) == 1);
603 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, rblock
, 1, ifp
->if_broot_bytes
);
604 cbno
= be64_to_cpu(*pp
);
607 XFS_WANT_CORRUPTED_RETURN(cur
->bc_mp
,
608 xfs_btree_check_lptr(cur
, cbno
, 1));
610 error
= xfs_btree_read_bufl(mp
, tp
, cbno
, 0, &cbp
, XFS_BMAP_BTREE_REF
,
614 cblock
= XFS_BUF_TO_BLOCK(cbp
);
615 if ((error
= xfs_btree_check_block(cur
, cblock
, 0, cbp
)))
617 xfs_rmap_ino_bmbt_owner(&oinfo
, ip
->i_ino
, whichfork
);
618 xfs_bmap_add_free(mp
, cur
->bc_tp
->t_dfops
, cbno
, 1, &oinfo
);
619 ip
->i_d
.di_nblocks
--;
620 xfs_trans_mod_dquot_byino(tp
, ip
, XFS_TRANS_DQ_BCOUNT
, -1L);
621 xfs_trans_binval(tp
, cbp
);
622 if (cur
->bc_bufs
[0] == cbp
)
623 cur
->bc_bufs
[0] = NULL
;
624 xfs_iroot_realloc(ip
, -1, whichfork
);
625 ASSERT(ifp
->if_broot
== NULL
);
626 ASSERT((ifp
->if_flags
& XFS_IFBROOT
) == 0);
627 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_EXTENTS
);
628 *logflagsp
= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
633 * Convert an extents-format file into a btree-format file.
634 * The new file will have a root block (in the inode) and a single child block.
636 STATIC
int /* error */
637 xfs_bmap_extents_to_btree(
638 struct xfs_trans
*tp
, /* transaction pointer */
639 struct xfs_inode
*ip
, /* incore inode pointer */
640 struct xfs_btree_cur
**curp
, /* cursor returned to caller */
641 int wasdel
, /* converting a delayed alloc */
642 int *logflagsp
, /* inode logging flags */
643 int whichfork
) /* data or attr fork */
645 struct xfs_btree_block
*ablock
; /* allocated (child) bt block */
646 struct xfs_buf
*abp
; /* buffer for ablock */
647 struct xfs_alloc_arg args
; /* allocation arguments */
648 struct xfs_bmbt_rec
*arp
; /* child record pointer */
649 struct xfs_btree_block
*block
; /* btree root block */
650 struct xfs_btree_cur
*cur
; /* bmap btree cursor */
651 int error
; /* error return value */
652 struct xfs_ifork
*ifp
; /* inode fork pointer */
653 struct xfs_bmbt_key
*kp
; /* root block key pointer */
654 struct xfs_mount
*mp
; /* mount structure */
655 xfs_bmbt_ptr_t
*pp
; /* root block address pointer */
656 struct xfs_iext_cursor icur
;
657 struct xfs_bmbt_irec rec
;
658 xfs_extnum_t cnt
= 0;
661 ASSERT(whichfork
!= XFS_COW_FORK
);
662 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
663 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
);
666 * Make space in the inode incore.
668 xfs_iroot_realloc(ip
, 1, whichfork
);
669 ifp
->if_flags
|= XFS_IFBROOT
;
674 block
= ifp
->if_broot
;
675 xfs_btree_init_block_int(mp
, block
, XFS_BUF_DADDR_NULL
,
676 XFS_BTNUM_BMAP
, 1, 1, ip
->i_ino
,
677 XFS_BTREE_LONG_PTRS
);
679 * Need a cursor. Can't allocate until bb_level is filled in.
681 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
682 cur
->bc_private
.b
.flags
= wasdel
? XFS_BTCUR_BPRV_WASDEL
: 0;
684 * Convert to a btree with two levels, one record in root.
686 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_BTREE
);
687 memset(&args
, 0, sizeof(args
));
690 xfs_rmap_ino_bmbt_owner(&args
.oinfo
, ip
->i_ino
, whichfork
);
691 if (tp
->t_firstblock
== NULLFSBLOCK
) {
692 args
.type
= XFS_ALLOCTYPE_START_BNO
;
693 args
.fsbno
= XFS_INO_TO_FSB(mp
, ip
->i_ino
);
694 } else if (tp
->t_dfops
->dop_low
) {
695 args
.type
= XFS_ALLOCTYPE_START_BNO
;
696 args
.fsbno
= tp
->t_firstblock
;
698 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
699 args
.fsbno
= tp
->t_firstblock
;
701 args
.minlen
= args
.maxlen
= args
.prod
= 1;
702 args
.wasdel
= wasdel
;
704 if ((error
= xfs_alloc_vextent(&args
))) {
705 xfs_iroot_realloc(ip
, -1, whichfork
);
706 ASSERT(ifp
->if_broot
== NULL
);
707 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_EXTENTS
);
708 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
712 if (WARN_ON_ONCE(args
.fsbno
== NULLFSBLOCK
)) {
713 xfs_iroot_realloc(ip
, -1, whichfork
);
714 ASSERT(ifp
->if_broot
== NULL
);
715 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_EXTENTS
);
716 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
720 * Allocation can't fail, the space was reserved.
722 ASSERT(tp
->t_firstblock
== NULLFSBLOCK
||
723 args
.agno
>= XFS_FSB_TO_AGNO(mp
, tp
->t_firstblock
));
724 tp
->t_firstblock
= args
.fsbno
;
725 cur
->bc_private
.b
.allocated
++;
726 ip
->i_d
.di_nblocks
++;
727 xfs_trans_mod_dquot_byino(tp
, ip
, XFS_TRANS_DQ_BCOUNT
, 1L);
728 abp
= xfs_btree_get_bufl(mp
, tp
, args
.fsbno
, 0);
730 * Fill in the child block.
732 abp
->b_ops
= &xfs_bmbt_buf_ops
;
733 ablock
= XFS_BUF_TO_BLOCK(abp
);
734 xfs_btree_init_block_int(mp
, ablock
, abp
->b_bn
,
735 XFS_BTNUM_BMAP
, 0, 0, ip
->i_ino
,
736 XFS_BTREE_LONG_PTRS
);
738 for_each_xfs_iext(ifp
, &icur
, &rec
) {
739 if (isnullstartblock(rec
.br_startblock
))
741 arp
= XFS_BMBT_REC_ADDR(mp
, ablock
, 1 + cnt
);
742 xfs_bmbt_disk_set_all(arp
, &rec
);
745 ASSERT(cnt
== XFS_IFORK_NEXTENTS(ip
, whichfork
));
746 xfs_btree_set_numrecs(ablock
, cnt
);
749 * Fill in the root key and pointer.
751 kp
= XFS_BMBT_KEY_ADDR(mp
, block
, 1);
752 arp
= XFS_BMBT_REC_ADDR(mp
, ablock
, 1);
753 kp
->br_startoff
= cpu_to_be64(xfs_bmbt_disk_get_startoff(arp
));
754 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, xfs_bmbt_get_maxrecs(cur
,
755 be16_to_cpu(block
->bb_level
)));
756 *pp
= cpu_to_be64(args
.fsbno
);
759 * Do all this logging at the end so that
760 * the root is at the right level.
762 xfs_btree_log_block(cur
, abp
, XFS_BB_ALL_BITS
);
763 xfs_btree_log_recs(cur
, abp
, 1, be16_to_cpu(ablock
->bb_numrecs
));
764 ASSERT(*curp
== NULL
);
766 *logflagsp
= XFS_ILOG_CORE
| xfs_ilog_fbroot(whichfork
);
771 * Convert a local file to an extents file.
772 * This code is out of bounds for data forks of regular files,
773 * since the file data needs to get logged so things will stay consistent.
774 * (The bmap-level manipulations are ok, though).
777 xfs_bmap_local_to_extents_empty(
778 struct xfs_inode
*ip
,
781 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
783 ASSERT(whichfork
!= XFS_COW_FORK
);
784 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
785 ASSERT(ifp
->if_bytes
== 0);
786 ASSERT(XFS_IFORK_NEXTENTS(ip
, whichfork
) == 0);
788 xfs_bmap_forkoff_reset(ip
, whichfork
);
789 ifp
->if_flags
&= ~XFS_IFINLINE
;
790 ifp
->if_flags
|= XFS_IFEXTENTS
;
791 ifp
->if_u1
.if_root
= NULL
;
793 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_EXTENTS
);
797 STATIC
int /* error */
798 xfs_bmap_local_to_extents(
799 xfs_trans_t
*tp
, /* transaction pointer */
800 xfs_inode_t
*ip
, /* incore inode pointer */
801 xfs_extlen_t total
, /* total blocks needed by transaction */
802 int *logflagsp
, /* inode logging flags */
804 void (*init_fn
)(struct xfs_trans
*tp
,
806 struct xfs_inode
*ip
,
807 struct xfs_ifork
*ifp
))
810 int flags
; /* logging flags returned */
811 xfs_ifork_t
*ifp
; /* inode fork pointer */
812 xfs_alloc_arg_t args
; /* allocation arguments */
813 xfs_buf_t
*bp
; /* buffer for extent block */
814 struct xfs_bmbt_irec rec
;
815 struct xfs_iext_cursor icur
;
818 * We don't want to deal with the case of keeping inode data inline yet.
819 * So sending the data fork of a regular inode is invalid.
821 ASSERT(!(S_ISREG(VFS_I(ip
)->i_mode
) && whichfork
== XFS_DATA_FORK
));
822 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
823 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
825 if (!ifp
->if_bytes
) {
826 xfs_bmap_local_to_extents_empty(ip
, whichfork
);
827 flags
= XFS_ILOG_CORE
;
833 ASSERT((ifp
->if_flags
& (XFS_IFINLINE
|XFS_IFEXTENTS
)) == XFS_IFINLINE
);
834 memset(&args
, 0, sizeof(args
));
836 args
.mp
= ip
->i_mount
;
837 xfs_rmap_ino_owner(&args
.oinfo
, ip
->i_ino
, whichfork
, 0);
839 * Allocate a block. We know we need only one, since the
840 * file currently fits in an inode.
842 if (tp
->t_firstblock
== NULLFSBLOCK
) {
843 args
.fsbno
= XFS_INO_TO_FSB(args
.mp
, ip
->i_ino
);
844 args
.type
= XFS_ALLOCTYPE_START_BNO
;
846 args
.fsbno
= tp
->t_firstblock
;
847 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
850 args
.minlen
= args
.maxlen
= args
.prod
= 1;
851 error
= xfs_alloc_vextent(&args
);
855 /* Can't fail, the space was reserved. */
856 ASSERT(args
.fsbno
!= NULLFSBLOCK
);
857 ASSERT(args
.len
== 1);
858 tp
->t_firstblock
= args
.fsbno
;
859 bp
= xfs_btree_get_bufl(args
.mp
, tp
, args
.fsbno
, 0);
862 * Initialize the block, copy the data and log the remote buffer.
864 * The callout is responsible for logging because the remote format
865 * might differ from the local format and thus we don't know how much to
866 * log here. Note that init_fn must also set the buffer log item type
869 init_fn(tp
, bp
, ip
, ifp
);
871 /* account for the change in fork size */
872 xfs_idata_realloc(ip
, -ifp
->if_bytes
, whichfork
);
873 xfs_bmap_local_to_extents_empty(ip
, whichfork
);
874 flags
|= XFS_ILOG_CORE
;
876 ifp
->if_u1
.if_root
= NULL
;
880 rec
.br_startblock
= args
.fsbno
;
881 rec
.br_blockcount
= 1;
882 rec
.br_state
= XFS_EXT_NORM
;
883 xfs_iext_first(ifp
, &icur
);
884 xfs_iext_insert(ip
, &icur
, &rec
, 0);
886 XFS_IFORK_NEXT_SET(ip
, whichfork
, 1);
887 ip
->i_d
.di_nblocks
= 1;
888 xfs_trans_mod_dquot_byino(tp
, ip
,
889 XFS_TRANS_DQ_BCOUNT
, 1L);
890 flags
|= xfs_ilog_fext(whichfork
);
898 * Called from xfs_bmap_add_attrfork to handle btree format files.
900 STATIC
int /* error */
901 xfs_bmap_add_attrfork_btree(
902 xfs_trans_t
*tp
, /* transaction pointer */
903 xfs_inode_t
*ip
, /* incore inode pointer */
904 int *flags
) /* inode logging flags */
906 xfs_btree_cur_t
*cur
; /* btree cursor */
907 int error
; /* error return value */
908 xfs_mount_t
*mp
; /* file system mount struct */
909 int stat
; /* newroot status */
912 if (ip
->i_df
.if_broot_bytes
<= XFS_IFORK_DSIZE(ip
))
913 *flags
|= XFS_ILOG_DBROOT
;
915 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, XFS_DATA_FORK
);
916 error
= xfs_bmbt_lookup_first(cur
, &stat
);
919 /* must be at least one entry */
920 XFS_WANT_CORRUPTED_GOTO(mp
, stat
== 1, error0
);
921 if ((error
= xfs_btree_new_iroot(cur
, flags
, &stat
)))
924 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
927 cur
->bc_private
.b
.allocated
= 0;
928 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
932 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
937 * Called from xfs_bmap_add_attrfork to handle extents format files.
939 STATIC
int /* error */
940 xfs_bmap_add_attrfork_extents(
941 struct xfs_trans
*tp
, /* transaction pointer */
942 struct xfs_inode
*ip
, /* incore inode pointer */
943 int *flags
) /* inode logging flags */
945 xfs_btree_cur_t
*cur
; /* bmap btree cursor */
946 int error
; /* error return value */
948 if (ip
->i_d
.di_nextents
* sizeof(xfs_bmbt_rec_t
) <= XFS_IFORK_DSIZE(ip
))
951 error
= xfs_bmap_extents_to_btree(tp
, ip
, &cur
, 0, flags
,
954 cur
->bc_private
.b
.allocated
= 0;
955 xfs_btree_del_cursor(cur
, error
);
961 * Called from xfs_bmap_add_attrfork to handle local format files. Each
962 * different data fork content type needs a different callout to do the
963 * conversion. Some are basic and only require special block initialisation
964 * callouts for the data formating, others (directories) are so specialised they
965 * handle everything themselves.
967 * XXX (dgc): investigate whether directory conversion can use the generic
968 * formatting callout. It should be possible - it's just a very complex
971 STATIC
int /* error */
972 xfs_bmap_add_attrfork_local(
973 struct xfs_trans
*tp
, /* transaction pointer */
974 struct xfs_inode
*ip
, /* incore inode pointer */
975 int *flags
) /* inode logging flags */
977 struct xfs_da_args dargs
; /* args for dir/attr code */
979 if (ip
->i_df
.if_bytes
<= XFS_IFORK_DSIZE(ip
))
982 if (S_ISDIR(VFS_I(ip
)->i_mode
)) {
983 memset(&dargs
, 0, sizeof(dargs
));
984 dargs
.geo
= ip
->i_mount
->m_dir_geo
;
986 dargs
.total
= dargs
.geo
->fsbcount
;
987 dargs
.whichfork
= XFS_DATA_FORK
;
989 return xfs_dir2_sf_to_block(&dargs
);
992 if (S_ISLNK(VFS_I(ip
)->i_mode
))
993 return xfs_bmap_local_to_extents(tp
, ip
, 1, flags
,
995 xfs_symlink_local_to_remote
);
997 /* should only be called for types that support local format data */
999 return -EFSCORRUPTED
;
1003 * Convert inode from non-attributed to attributed.
1004 * Must not be in a transaction, ip must not be locked.
1006 int /* error code */
1007 xfs_bmap_add_attrfork(
1008 xfs_inode_t
*ip
, /* incore inode pointer */
1009 int size
, /* space new attribute needs */
1010 int rsvd
) /* xact may use reserved blks */
1012 struct xfs_defer_ops dfops
; /* freed extent records */
1013 xfs_mount_t
*mp
; /* mount structure */
1014 xfs_trans_t
*tp
; /* transaction pointer */
1015 int blks
; /* space reservation */
1016 int version
= 1; /* superblock attr version */
1017 int logflags
; /* logging flags */
1018 int error
; /* error return value */
1020 ASSERT(XFS_IFORK_Q(ip
) == 0);
1023 ASSERT(!XFS_NOT_DQATTACHED(mp
, ip
));
1025 blks
= XFS_ADDAFORK_SPACE_RES(mp
);
1027 error
= xfs_trans_alloc(mp
, &M_RES(mp
)->tr_addafork
, blks
, 0,
1028 rsvd
? XFS_TRANS_RESERVE
: 0, &tp
);
1031 xfs_defer_init(tp
, &dfops
);
1033 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
1034 error
= xfs_trans_reserve_quota_nblks(tp
, ip
, blks
, 0, rsvd
?
1035 XFS_QMOPT_RES_REGBLKS
| XFS_QMOPT_FORCE_RES
:
1036 XFS_QMOPT_RES_REGBLKS
);
1039 if (XFS_IFORK_Q(ip
))
1041 if (ip
->i_d
.di_anextents
!= 0) {
1042 error
= -EFSCORRUPTED
;
1045 if (ip
->i_d
.di_aformat
!= XFS_DINODE_FMT_EXTENTS
) {
1047 * For inodes coming from pre-6.2 filesystems.
1049 ASSERT(ip
->i_d
.di_aformat
== 0);
1050 ip
->i_d
.di_aformat
= XFS_DINODE_FMT_EXTENTS
;
1053 xfs_trans_ijoin(tp
, ip
, 0);
1054 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
1056 switch (ip
->i_d
.di_format
) {
1057 case XFS_DINODE_FMT_DEV
:
1058 ip
->i_d
.di_forkoff
= roundup(sizeof(xfs_dev_t
), 8) >> 3;
1060 case XFS_DINODE_FMT_LOCAL
:
1061 case XFS_DINODE_FMT_EXTENTS
:
1062 case XFS_DINODE_FMT_BTREE
:
1063 ip
->i_d
.di_forkoff
= xfs_attr_shortform_bytesfit(ip
, size
);
1064 if (!ip
->i_d
.di_forkoff
)
1065 ip
->i_d
.di_forkoff
= xfs_default_attroffset(ip
) >> 3;
1066 else if (mp
->m_flags
& XFS_MOUNT_ATTR2
)
1075 ASSERT(ip
->i_afp
== NULL
);
1076 ip
->i_afp
= kmem_zone_zalloc(xfs_ifork_zone
, KM_SLEEP
);
1077 ip
->i_afp
->if_flags
= XFS_IFEXTENTS
;
1079 switch (ip
->i_d
.di_format
) {
1080 case XFS_DINODE_FMT_LOCAL
:
1081 error
= xfs_bmap_add_attrfork_local(tp
, ip
, &logflags
);
1083 case XFS_DINODE_FMT_EXTENTS
:
1084 error
= xfs_bmap_add_attrfork_extents(tp
, ip
, &logflags
);
1086 case XFS_DINODE_FMT_BTREE
:
1087 error
= xfs_bmap_add_attrfork_btree(tp
, ip
, &logflags
);
1094 xfs_trans_log_inode(tp
, ip
, logflags
);
1097 if (!xfs_sb_version_hasattr(&mp
->m_sb
) ||
1098 (!xfs_sb_version_hasattr2(&mp
->m_sb
) && version
== 2)) {
1099 bool log_sb
= false;
1101 spin_lock(&mp
->m_sb_lock
);
1102 if (!xfs_sb_version_hasattr(&mp
->m_sb
)) {
1103 xfs_sb_version_addattr(&mp
->m_sb
);
1106 if (!xfs_sb_version_hasattr2(&mp
->m_sb
) && version
== 2) {
1107 xfs_sb_version_addattr2(&mp
->m_sb
);
1110 spin_unlock(&mp
->m_sb_lock
);
1115 error
= xfs_defer_finish(&tp
, &dfops
);
1118 error
= xfs_trans_commit(tp
);
1119 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
1123 xfs_defer_cancel(&dfops
);
1125 xfs_trans_cancel(tp
);
1126 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
1131 * Internal and external extent tree search functions.
1135 * Read in extents from a btree-format inode.
1139 struct xfs_trans
*tp
,
1140 struct xfs_inode
*ip
,
1143 struct xfs_mount
*mp
= ip
->i_mount
;
1144 int state
= xfs_bmap_fork_to_state(whichfork
);
1145 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1146 xfs_extnum_t nextents
= XFS_IFORK_NEXTENTS(ip
, whichfork
);
1147 struct xfs_btree_block
*block
= ifp
->if_broot
;
1148 struct xfs_iext_cursor icur
;
1149 struct xfs_bmbt_irec
new;
1157 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
1159 if (unlikely(XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)) {
1160 XFS_ERROR_REPORT(__func__
, XFS_ERRLEVEL_LOW
, mp
);
1161 return -EFSCORRUPTED
;
1165 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
1167 level
= be16_to_cpu(block
->bb_level
);
1169 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, 1, ifp
->if_broot_bytes
);
1170 bno
= be64_to_cpu(*pp
);
1173 * Go down the tree until leaf level is reached, following the first
1174 * pointer (leftmost) at each level.
1176 while (level
-- > 0) {
1177 error
= xfs_btree_read_bufl(mp
, tp
, bno
, 0, &bp
,
1178 XFS_BMAP_BTREE_REF
, &xfs_bmbt_buf_ops
);
1181 block
= XFS_BUF_TO_BLOCK(bp
);
1184 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, mp
->m_bmap_dmxr
[1]);
1185 bno
= be64_to_cpu(*pp
);
1186 XFS_WANT_CORRUPTED_GOTO(mp
,
1187 xfs_verify_fsbno(mp
, bno
), out_brelse
);
1188 xfs_trans_brelse(tp
, bp
);
1192 * Here with bp and block set to the leftmost leaf node in the tree.
1195 xfs_iext_first(ifp
, &icur
);
1198 * Loop over all leaf nodes. Copy information to the extent records.
1201 xfs_bmbt_rec_t
*frp
;
1202 xfs_fsblock_t nextbno
;
1203 xfs_extnum_t num_recs
;
1205 num_recs
= xfs_btree_get_numrecs(block
);
1206 if (unlikely(i
+ num_recs
> nextents
)) {
1207 xfs_warn(ip
->i_mount
,
1208 "corrupt dinode %Lu, (btree extents).",
1209 (unsigned long long) ip
->i_ino
);
1210 xfs_inode_verifier_error(ip
, -EFSCORRUPTED
,
1211 __func__
, block
, sizeof(*block
),
1213 error
= -EFSCORRUPTED
;
1217 * Read-ahead the next leaf block, if any.
1219 nextbno
= be64_to_cpu(block
->bb_u
.l
.bb_rightsib
);
1220 if (nextbno
!= NULLFSBLOCK
)
1221 xfs_btree_reada_bufl(mp
, nextbno
, 1,
1224 * Copy records into the extent records.
1226 frp
= XFS_BMBT_REC_ADDR(mp
, block
, 1);
1227 for (j
= 0; j
< num_recs
; j
++, frp
++, i
++) {
1230 xfs_bmbt_disk_get_all(frp
, &new);
1231 fa
= xfs_bmap_validate_extent(ip
, whichfork
, &new);
1233 error
= -EFSCORRUPTED
;
1234 xfs_inode_verifier_error(ip
, error
,
1235 "xfs_iread_extents(2)",
1236 frp
, sizeof(*frp
), fa
);
1239 xfs_iext_insert(ip
, &icur
, &new, state
);
1240 trace_xfs_read_extent(ip
, &icur
, state
, _THIS_IP_
);
1241 xfs_iext_next(ifp
, &icur
);
1243 xfs_trans_brelse(tp
, bp
);
1246 * If we've reached the end, stop.
1248 if (bno
== NULLFSBLOCK
)
1250 error
= xfs_btree_read_bufl(mp
, tp
, bno
, 0, &bp
,
1251 XFS_BMAP_BTREE_REF
, &xfs_bmbt_buf_ops
);
1254 block
= XFS_BUF_TO_BLOCK(bp
);
1257 if (i
!= XFS_IFORK_NEXTENTS(ip
, whichfork
)) {
1258 error
= -EFSCORRUPTED
;
1261 ASSERT(i
== xfs_iext_count(ifp
));
1263 ifp
->if_flags
|= XFS_IFEXTENTS
;
1267 xfs_trans_brelse(tp
, bp
);
1269 xfs_iext_destroy(ifp
);
1274 * Returns the relative block number of the first unused block(s) in the given
1275 * fork with at least "len" logically contiguous blocks free. This is the
1276 * lowest-address hole if the fork has holes, else the first block past the end
1277 * of fork. Return 0 if the fork is currently local (in-inode).
1280 xfs_bmap_first_unused(
1281 struct xfs_trans
*tp
, /* transaction pointer */
1282 struct xfs_inode
*ip
, /* incore inode */
1283 xfs_extlen_t len
, /* size of hole to find */
1284 xfs_fileoff_t
*first_unused
, /* unused block */
1285 int whichfork
) /* data or attr fork */
1287 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1288 struct xfs_bmbt_irec got
;
1289 struct xfs_iext_cursor icur
;
1290 xfs_fileoff_t lastaddr
= 0;
1291 xfs_fileoff_t lowest
, max
;
1294 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
||
1295 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
||
1296 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
1298 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
) {
1303 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
1304 error
= xfs_iread_extents(tp
, ip
, whichfork
);
1309 lowest
= max
= *first_unused
;
1310 for_each_xfs_iext(ifp
, &icur
, &got
) {
1312 * See if the hole before this extent will work.
1314 if (got
.br_startoff
>= lowest
+ len
&&
1315 got
.br_startoff
- max
>= len
)
1317 lastaddr
= got
.br_startoff
+ got
.br_blockcount
;
1318 max
= XFS_FILEOFF_MAX(lastaddr
, lowest
);
1321 *first_unused
= max
;
1326 * Returns the file-relative block number of the last block - 1 before
1327 * last_block (input value) in the file.
1328 * This is not based on i_size, it is based on the extent records.
1329 * Returns 0 for local files, as they do not have extent records.
1332 xfs_bmap_last_before(
1333 struct xfs_trans
*tp
, /* transaction pointer */
1334 struct xfs_inode
*ip
, /* incore inode */
1335 xfs_fileoff_t
*last_block
, /* last block */
1336 int whichfork
) /* data or attr fork */
1338 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1339 struct xfs_bmbt_irec got
;
1340 struct xfs_iext_cursor icur
;
1343 switch (XFS_IFORK_FORMAT(ip
, whichfork
)) {
1344 case XFS_DINODE_FMT_LOCAL
:
1347 case XFS_DINODE_FMT_BTREE
:
1348 case XFS_DINODE_FMT_EXTENTS
:
1354 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
1355 error
= xfs_iread_extents(tp
, ip
, whichfork
);
1360 if (!xfs_iext_lookup_extent_before(ip
, ifp
, last_block
, &icur
, &got
))
1366 xfs_bmap_last_extent(
1367 struct xfs_trans
*tp
,
1368 struct xfs_inode
*ip
,
1370 struct xfs_bmbt_irec
*rec
,
1373 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1374 struct xfs_iext_cursor icur
;
1377 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
1378 error
= xfs_iread_extents(tp
, ip
, whichfork
);
1383 xfs_iext_last(ifp
, &icur
);
1384 if (!xfs_iext_get_extent(ifp
, &icur
, rec
))
1392 * Check the last inode extent to determine whether this allocation will result
1393 * in blocks being allocated at the end of the file. When we allocate new data
1394 * blocks at the end of the file which do not start at the previous data block,
1395 * we will try to align the new blocks at stripe unit boundaries.
1397 * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be
1398 * at, or past the EOF.
1402 struct xfs_bmalloca
*bma
,
1405 struct xfs_bmbt_irec rec
;
1410 error
= xfs_bmap_last_extent(NULL
, bma
->ip
, whichfork
, &rec
,
1421 * Check if we are allocation or past the last extent, or at least into
1422 * the last delayed allocated extent.
1424 bma
->aeof
= bma
->offset
>= rec
.br_startoff
+ rec
.br_blockcount
||
1425 (bma
->offset
>= rec
.br_startoff
&&
1426 isnullstartblock(rec
.br_startblock
));
1431 * Returns the file-relative block number of the first block past eof in
1432 * the file. This is not based on i_size, it is based on the extent records.
1433 * Returns 0 for local files, as they do not have extent records.
1436 xfs_bmap_last_offset(
1437 struct xfs_inode
*ip
,
1438 xfs_fileoff_t
*last_block
,
1441 struct xfs_bmbt_irec rec
;
1447 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
)
1450 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
&&
1451 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
1454 error
= xfs_bmap_last_extent(NULL
, ip
, whichfork
, &rec
, &is_empty
);
1455 if (error
|| is_empty
)
1458 *last_block
= rec
.br_startoff
+ rec
.br_blockcount
;
1463 * Returns whether the selected fork of the inode has exactly one
1464 * block or not. For the data fork we check this matches di_size,
1465 * implying the file's range is 0..bsize-1.
1467 int /* 1=>1 block, 0=>otherwise */
1469 xfs_inode_t
*ip
, /* incore inode */
1470 int whichfork
) /* data or attr fork */
1472 xfs_ifork_t
*ifp
; /* inode fork pointer */
1473 int rval
; /* return value */
1474 xfs_bmbt_irec_t s
; /* internal version of extent */
1475 struct xfs_iext_cursor icur
;
1478 if (whichfork
== XFS_DATA_FORK
)
1479 return XFS_ISIZE(ip
) == ip
->i_mount
->m_sb
.sb_blocksize
;
1481 if (XFS_IFORK_NEXTENTS(ip
, whichfork
) != 1)
1483 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
1485 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1486 ASSERT(ifp
->if_flags
& XFS_IFEXTENTS
);
1487 xfs_iext_first(ifp
, &icur
);
1488 xfs_iext_get_extent(ifp
, &icur
, &s
);
1489 rval
= s
.br_startoff
== 0 && s
.br_blockcount
== 1;
1490 if (rval
&& whichfork
== XFS_DATA_FORK
)
1491 ASSERT(XFS_ISIZE(ip
) == ip
->i_mount
->m_sb
.sb_blocksize
);
1496 * Extent tree manipulation functions used during allocation.
1500 * Convert a delayed allocation to a real allocation.
1502 STATIC
int /* error */
1503 xfs_bmap_add_extent_delay_real(
1504 struct xfs_bmalloca
*bma
,
1507 struct xfs_bmbt_irec
*new = &bma
->got
;
1508 int error
; /* error return value */
1509 int i
; /* temp state */
1510 xfs_ifork_t
*ifp
; /* inode fork pointer */
1511 xfs_fileoff_t new_endoff
; /* end offset of new entry */
1512 xfs_bmbt_irec_t r
[3]; /* neighbor extent entries */
1513 /* left is 0, right is 1, prev is 2 */
1514 int rval
=0; /* return value (logging flags) */
1515 int state
= xfs_bmap_fork_to_state(whichfork
);
1516 xfs_filblks_t da_new
; /* new count del alloc blocks used */
1517 xfs_filblks_t da_old
; /* old count del alloc blocks used */
1518 xfs_filblks_t temp
=0; /* value for da_new calculations */
1519 int tmp_rval
; /* partial logging flags */
1520 struct xfs_mount
*mp
;
1521 xfs_extnum_t
*nextents
;
1522 struct xfs_bmbt_irec old
;
1524 mp
= bma
->ip
->i_mount
;
1525 ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
1526 ASSERT(whichfork
!= XFS_ATTR_FORK
);
1527 nextents
= (whichfork
== XFS_COW_FORK
? &bma
->ip
->i_cnextents
:
1528 &bma
->ip
->i_d
.di_nextents
);
1530 ASSERT(!isnullstartblock(new->br_startblock
));
1532 (bma
->cur
->bc_private
.b
.flags
& XFS_BTCUR_BPRV_WASDEL
));
1534 XFS_STATS_INC(mp
, xs_add_exlist
);
1541 * Set up a bunch of variables to make the tests simpler.
1543 xfs_iext_get_extent(ifp
, &bma
->icur
, &PREV
);
1544 new_endoff
= new->br_startoff
+ new->br_blockcount
;
1545 ASSERT(isnullstartblock(PREV
.br_startblock
));
1546 ASSERT(PREV
.br_startoff
<= new->br_startoff
);
1547 ASSERT(PREV
.br_startoff
+ PREV
.br_blockcount
>= new_endoff
);
1549 da_old
= startblockval(PREV
.br_startblock
);
1553 * Set flags determining what part of the previous delayed allocation
1554 * extent is being replaced by a real allocation.
1556 if (PREV
.br_startoff
== new->br_startoff
)
1557 state
|= BMAP_LEFT_FILLING
;
1558 if (PREV
.br_startoff
+ PREV
.br_blockcount
== new_endoff
)
1559 state
|= BMAP_RIGHT_FILLING
;
1562 * Check and set flags if this segment has a left neighbor.
1563 * Don't set contiguous if the combined extent would be too large.
1565 if (xfs_iext_peek_prev_extent(ifp
, &bma
->icur
, &LEFT
)) {
1566 state
|= BMAP_LEFT_VALID
;
1567 if (isnullstartblock(LEFT
.br_startblock
))
1568 state
|= BMAP_LEFT_DELAY
;
1571 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
1572 LEFT
.br_startoff
+ LEFT
.br_blockcount
== new->br_startoff
&&
1573 LEFT
.br_startblock
+ LEFT
.br_blockcount
== new->br_startblock
&&
1574 LEFT
.br_state
== new->br_state
&&
1575 LEFT
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
1576 state
|= BMAP_LEFT_CONTIG
;
1579 * Check and set flags if this segment has a right neighbor.
1580 * Don't set contiguous if the combined extent would be too large.
1581 * Also check for all-three-contiguous being too large.
1583 if (xfs_iext_peek_next_extent(ifp
, &bma
->icur
, &RIGHT
)) {
1584 state
|= BMAP_RIGHT_VALID
;
1585 if (isnullstartblock(RIGHT
.br_startblock
))
1586 state
|= BMAP_RIGHT_DELAY
;
1589 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
1590 new_endoff
== RIGHT
.br_startoff
&&
1591 new->br_startblock
+ new->br_blockcount
== RIGHT
.br_startblock
&&
1592 new->br_state
== RIGHT
.br_state
&&
1593 new->br_blockcount
+ RIGHT
.br_blockcount
<= MAXEXTLEN
&&
1594 ((state
& (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
1595 BMAP_RIGHT_FILLING
)) !=
1596 (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
1597 BMAP_RIGHT_FILLING
) ||
1598 LEFT
.br_blockcount
+ new->br_blockcount
+ RIGHT
.br_blockcount
1600 state
|= BMAP_RIGHT_CONTIG
;
1604 * Switch out based on the FILLING and CONTIG state bits.
1606 switch (state
& (BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
1607 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
)) {
1608 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
1609 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
1611 * Filling in all of a previously delayed allocation extent.
1612 * The left and right neighbors are both contiguous with new.
1614 LEFT
.br_blockcount
+= PREV
.br_blockcount
+ RIGHT
.br_blockcount
;
1616 xfs_iext_remove(bma
->ip
, &bma
->icur
, state
);
1617 xfs_iext_remove(bma
->ip
, &bma
->icur
, state
);
1618 xfs_iext_prev(ifp
, &bma
->icur
);
1619 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &LEFT
);
1622 if (bma
->cur
== NULL
)
1623 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1625 rval
= XFS_ILOG_CORE
;
1626 error
= xfs_bmbt_lookup_eq(bma
->cur
, &RIGHT
, &i
);
1629 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1630 error
= xfs_btree_delete(bma
->cur
, &i
);
1633 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1634 error
= xfs_btree_decrement(bma
->cur
, 0, &i
);
1637 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1638 error
= xfs_bmbt_update(bma
->cur
, &LEFT
);
1644 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
1646 * Filling in all of a previously delayed allocation extent.
1647 * The left neighbor is contiguous, the right is not.
1650 LEFT
.br_blockcount
+= PREV
.br_blockcount
;
1652 xfs_iext_remove(bma
->ip
, &bma
->icur
, state
);
1653 xfs_iext_prev(ifp
, &bma
->icur
);
1654 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &LEFT
);
1656 if (bma
->cur
== NULL
)
1657 rval
= XFS_ILOG_DEXT
;
1660 error
= xfs_bmbt_lookup_eq(bma
->cur
, &old
, &i
);
1663 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1664 error
= xfs_bmbt_update(bma
->cur
, &LEFT
);
1670 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
1672 * Filling in all of a previously delayed allocation extent.
1673 * The right neighbor is contiguous, the left is not.
1675 PREV
.br_startblock
= new->br_startblock
;
1676 PREV
.br_blockcount
+= RIGHT
.br_blockcount
;
1678 xfs_iext_next(ifp
, &bma
->icur
);
1679 xfs_iext_remove(bma
->ip
, &bma
->icur
, state
);
1680 xfs_iext_prev(ifp
, &bma
->icur
);
1681 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &PREV
);
1683 if (bma
->cur
== NULL
)
1684 rval
= XFS_ILOG_DEXT
;
1687 error
= xfs_bmbt_lookup_eq(bma
->cur
, &RIGHT
, &i
);
1690 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1691 error
= xfs_bmbt_update(bma
->cur
, &PREV
);
1697 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
1699 * Filling in all of a previously delayed allocation extent.
1700 * Neither the left nor right neighbors are contiguous with
1703 PREV
.br_startblock
= new->br_startblock
;
1704 PREV
.br_state
= new->br_state
;
1705 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &PREV
);
1708 if (bma
->cur
== NULL
)
1709 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1711 rval
= XFS_ILOG_CORE
;
1712 error
= xfs_bmbt_lookup_eq(bma
->cur
, new, &i
);
1715 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
1716 error
= xfs_btree_insert(bma
->cur
, &i
);
1719 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1723 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
:
1725 * Filling in the first part of a previous delayed allocation.
1726 * The left neighbor is contiguous.
1729 temp
= PREV
.br_blockcount
- new->br_blockcount
;
1730 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
1731 startblockval(PREV
.br_startblock
));
1733 LEFT
.br_blockcount
+= new->br_blockcount
;
1735 PREV
.br_blockcount
= temp
;
1736 PREV
.br_startoff
+= new->br_blockcount
;
1737 PREV
.br_startblock
= nullstartblock(da_new
);
1739 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &PREV
);
1740 xfs_iext_prev(ifp
, &bma
->icur
);
1741 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &LEFT
);
1743 if (bma
->cur
== NULL
)
1744 rval
= XFS_ILOG_DEXT
;
1747 error
= xfs_bmbt_lookup_eq(bma
->cur
, &old
, &i
);
1750 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1751 error
= xfs_bmbt_update(bma
->cur
, &LEFT
);
1757 case BMAP_LEFT_FILLING
:
1759 * Filling in the first part of a previous delayed allocation.
1760 * The left neighbor is not contiguous.
1762 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, new);
1764 if (bma
->cur
== NULL
)
1765 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1767 rval
= XFS_ILOG_CORE
;
1768 error
= xfs_bmbt_lookup_eq(bma
->cur
, new, &i
);
1771 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
1772 error
= xfs_btree_insert(bma
->cur
, &i
);
1775 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1778 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
1779 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
1780 &bma
->cur
, 1, &tmp_rval
, whichfork
);
1786 temp
= PREV
.br_blockcount
- new->br_blockcount
;
1787 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
1788 startblockval(PREV
.br_startblock
) -
1789 (bma
->cur
? bma
->cur
->bc_private
.b
.allocated
: 0));
1791 PREV
.br_startoff
= new_endoff
;
1792 PREV
.br_blockcount
= temp
;
1793 PREV
.br_startblock
= nullstartblock(da_new
);
1794 xfs_iext_next(ifp
, &bma
->icur
);
1795 xfs_iext_insert(bma
->ip
, &bma
->icur
, &PREV
, state
);
1796 xfs_iext_prev(ifp
, &bma
->icur
);
1799 case BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
1801 * Filling in the last part of a previous delayed allocation.
1802 * The right neighbor is contiguous with the new allocation.
1805 RIGHT
.br_startoff
= new->br_startoff
;
1806 RIGHT
.br_startblock
= new->br_startblock
;
1807 RIGHT
.br_blockcount
+= new->br_blockcount
;
1809 if (bma
->cur
== NULL
)
1810 rval
= XFS_ILOG_DEXT
;
1813 error
= xfs_bmbt_lookup_eq(bma
->cur
, &old
, &i
);
1816 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1817 error
= xfs_bmbt_update(bma
->cur
, &RIGHT
);
1822 temp
= PREV
.br_blockcount
- new->br_blockcount
;
1823 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
1824 startblockval(PREV
.br_startblock
));
1826 PREV
.br_blockcount
= temp
;
1827 PREV
.br_startblock
= nullstartblock(da_new
);
1829 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &PREV
);
1830 xfs_iext_next(ifp
, &bma
->icur
);
1831 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &RIGHT
);
1834 case BMAP_RIGHT_FILLING
:
1836 * Filling in the last part of a previous delayed allocation.
1837 * The right neighbor is not contiguous.
1839 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, new);
1841 if (bma
->cur
== NULL
)
1842 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1844 rval
= XFS_ILOG_CORE
;
1845 error
= xfs_bmbt_lookup_eq(bma
->cur
, new, &i
);
1848 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
1849 error
= xfs_btree_insert(bma
->cur
, &i
);
1852 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1855 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
1856 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
1857 &bma
->cur
, 1, &tmp_rval
, whichfork
);
1863 temp
= PREV
.br_blockcount
- new->br_blockcount
;
1864 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
1865 startblockval(PREV
.br_startblock
) -
1866 (bma
->cur
? bma
->cur
->bc_private
.b
.allocated
: 0));
1868 PREV
.br_startblock
= nullstartblock(da_new
);
1869 PREV
.br_blockcount
= temp
;
1870 xfs_iext_insert(bma
->ip
, &bma
->icur
, &PREV
, state
);
1871 xfs_iext_next(ifp
, &bma
->icur
);
1876 * Filling in the middle part of a previous delayed allocation.
1877 * Contiguity is impossible here.
1878 * This case is avoided almost all the time.
1880 * We start with a delayed allocation:
1882 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
1885 * and we are allocating:
1886 * +rrrrrrrrrrrrrrrrr+
1889 * and we set it up for insertion as:
1890 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
1892 * PREV @ idx LEFT RIGHT
1893 * inserted at idx + 1
1897 /* LEFT is the new middle */
1900 /* RIGHT is the new right */
1901 RIGHT
.br_state
= PREV
.br_state
;
1902 RIGHT
.br_startoff
= new_endoff
;
1903 RIGHT
.br_blockcount
=
1904 PREV
.br_startoff
+ PREV
.br_blockcount
- new_endoff
;
1905 RIGHT
.br_startblock
=
1906 nullstartblock(xfs_bmap_worst_indlen(bma
->ip
,
1907 RIGHT
.br_blockcount
));
1910 PREV
.br_blockcount
= new->br_startoff
- PREV
.br_startoff
;
1911 PREV
.br_startblock
=
1912 nullstartblock(xfs_bmap_worst_indlen(bma
->ip
,
1913 PREV
.br_blockcount
));
1914 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &PREV
);
1916 xfs_iext_next(ifp
, &bma
->icur
);
1917 xfs_iext_insert(bma
->ip
, &bma
->icur
, &RIGHT
, state
);
1918 xfs_iext_insert(bma
->ip
, &bma
->icur
, &LEFT
, state
);
1921 if (bma
->cur
== NULL
)
1922 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1924 rval
= XFS_ILOG_CORE
;
1925 error
= xfs_bmbt_lookup_eq(bma
->cur
, new, &i
);
1928 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
1929 error
= xfs_btree_insert(bma
->cur
, &i
);
1932 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1935 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
1936 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
1937 &bma
->cur
, 1, &tmp_rval
, whichfork
);
1943 da_new
= startblockval(PREV
.br_startblock
) +
1944 startblockval(RIGHT
.br_startblock
);
1947 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
1948 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
1949 case BMAP_LEFT_FILLING
| BMAP_RIGHT_CONTIG
:
1950 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
1951 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
1952 case BMAP_LEFT_CONTIG
:
1953 case BMAP_RIGHT_CONTIG
:
1955 * These cases are all impossible.
1960 /* add reverse mapping unless caller opted out */
1961 if (!(bma
->flags
& XFS_BMAPI_NORMAP
)) {
1962 error
= xfs_rmap_map_extent(mp
, bma
->tp
->t_dfops
, bma
->ip
,
1968 /* convert to a btree if necessary */
1969 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
1970 int tmp_logflags
; /* partial log flag return val */
1972 ASSERT(bma
->cur
== NULL
);
1973 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
1974 &bma
->cur
, da_old
> 0, &tmp_logflags
,
1976 bma
->logflags
|= tmp_logflags
;
1982 da_new
+= bma
->cur
->bc_private
.b
.allocated
;
1983 bma
->cur
->bc_private
.b
.allocated
= 0;
1986 /* adjust for changes in reserved delayed indirect blocks */
1987 if (da_new
!= da_old
) {
1988 ASSERT(state
== 0 || da_new
< da_old
);
1989 error
= xfs_mod_fdblocks(mp
, (int64_t)(da_old
- da_new
),
1993 xfs_bmap_check_leaf_extents(bma
->cur
, bma
->ip
, whichfork
);
1995 if (whichfork
!= XFS_COW_FORK
)
1996 bma
->logflags
|= rval
;
2004 * Convert an unwritten allocation to a real allocation or vice versa.
2006 STATIC
int /* error */
2007 xfs_bmap_add_extent_unwritten_real(
2008 struct xfs_trans
*tp
,
2009 xfs_inode_t
*ip
, /* incore inode pointer */
2011 struct xfs_iext_cursor
*icur
,
2012 xfs_btree_cur_t
**curp
, /* if *curp is null, not a btree */
2013 xfs_bmbt_irec_t
*new, /* new data to add to file extents */
2014 int *logflagsp
) /* inode logging flags */
2016 xfs_btree_cur_t
*cur
; /* btree cursor */
2017 int error
; /* error return value */
2018 int i
; /* temp state */
2019 xfs_ifork_t
*ifp
; /* inode fork pointer */
2020 xfs_fileoff_t new_endoff
; /* end offset of new entry */
2021 xfs_bmbt_irec_t r
[3]; /* neighbor extent entries */
2022 /* left is 0, right is 1, prev is 2 */
2023 int rval
=0; /* return value (logging flags) */
2024 int state
= xfs_bmap_fork_to_state(whichfork
);
2025 struct xfs_mount
*mp
= ip
->i_mount
;
2026 struct xfs_bmbt_irec old
;
2027 struct xfs_defer_ops
*dfops
= tp
? tp
->t_dfops
: NULL
;
2032 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
2034 ASSERT(!isnullstartblock(new->br_startblock
));
2036 XFS_STATS_INC(mp
, xs_add_exlist
);
2043 * Set up a bunch of variables to make the tests simpler.
2046 xfs_iext_get_extent(ifp
, icur
, &PREV
);
2047 ASSERT(new->br_state
!= PREV
.br_state
);
2048 new_endoff
= new->br_startoff
+ new->br_blockcount
;
2049 ASSERT(PREV
.br_startoff
<= new->br_startoff
);
2050 ASSERT(PREV
.br_startoff
+ PREV
.br_blockcount
>= new_endoff
);
2053 * Set flags determining what part of the previous oldext allocation
2054 * extent is being replaced by a newext allocation.
2056 if (PREV
.br_startoff
== new->br_startoff
)
2057 state
|= BMAP_LEFT_FILLING
;
2058 if (PREV
.br_startoff
+ PREV
.br_blockcount
== new_endoff
)
2059 state
|= BMAP_RIGHT_FILLING
;
2062 * Check and set flags if this segment has a left neighbor.
2063 * Don't set contiguous if the combined extent would be too large.
2065 if (xfs_iext_peek_prev_extent(ifp
, icur
, &LEFT
)) {
2066 state
|= BMAP_LEFT_VALID
;
2067 if (isnullstartblock(LEFT
.br_startblock
))
2068 state
|= BMAP_LEFT_DELAY
;
2071 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
2072 LEFT
.br_startoff
+ LEFT
.br_blockcount
== new->br_startoff
&&
2073 LEFT
.br_startblock
+ LEFT
.br_blockcount
== new->br_startblock
&&
2074 LEFT
.br_state
== new->br_state
&&
2075 LEFT
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
2076 state
|= BMAP_LEFT_CONTIG
;
2079 * Check and set flags if this segment has a right neighbor.
2080 * Don't set contiguous if the combined extent would be too large.
2081 * Also check for all-three-contiguous being too large.
2083 if (xfs_iext_peek_next_extent(ifp
, icur
, &RIGHT
)) {
2084 state
|= BMAP_RIGHT_VALID
;
2085 if (isnullstartblock(RIGHT
.br_startblock
))
2086 state
|= BMAP_RIGHT_DELAY
;
2089 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
2090 new_endoff
== RIGHT
.br_startoff
&&
2091 new->br_startblock
+ new->br_blockcount
== RIGHT
.br_startblock
&&
2092 new->br_state
== RIGHT
.br_state
&&
2093 new->br_blockcount
+ RIGHT
.br_blockcount
<= MAXEXTLEN
&&
2094 ((state
& (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
2095 BMAP_RIGHT_FILLING
)) !=
2096 (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
2097 BMAP_RIGHT_FILLING
) ||
2098 LEFT
.br_blockcount
+ new->br_blockcount
+ RIGHT
.br_blockcount
2100 state
|= BMAP_RIGHT_CONTIG
;
2103 * Switch out based on the FILLING and CONTIG state bits.
2105 switch (state
& (BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
2106 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
)) {
2107 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
2108 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2110 * Setting all of a previous oldext extent to newext.
2111 * The left and right neighbors are both contiguous with new.
2113 LEFT
.br_blockcount
+= PREV
.br_blockcount
+ RIGHT
.br_blockcount
;
2115 xfs_iext_remove(ip
, icur
, state
);
2116 xfs_iext_remove(ip
, icur
, state
);
2117 xfs_iext_prev(ifp
, icur
);
2118 xfs_iext_update_extent(ip
, state
, icur
, &LEFT
);
2119 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2120 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 2);
2122 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2124 rval
= XFS_ILOG_CORE
;
2125 error
= xfs_bmbt_lookup_eq(cur
, &RIGHT
, &i
);
2128 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2129 if ((error
= xfs_btree_delete(cur
, &i
)))
2131 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2132 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2134 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2135 if ((error
= xfs_btree_delete(cur
, &i
)))
2137 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2138 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2140 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2141 error
= xfs_bmbt_update(cur
, &LEFT
);
2147 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
2149 * Setting all of a previous oldext extent to newext.
2150 * The left neighbor is contiguous, the right is not.
2152 LEFT
.br_blockcount
+= PREV
.br_blockcount
;
2154 xfs_iext_remove(ip
, icur
, state
);
2155 xfs_iext_prev(ifp
, icur
);
2156 xfs_iext_update_extent(ip
, state
, icur
, &LEFT
);
2157 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2158 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
2160 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2162 rval
= XFS_ILOG_CORE
;
2163 error
= xfs_bmbt_lookup_eq(cur
, &PREV
, &i
);
2166 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2167 if ((error
= xfs_btree_delete(cur
, &i
)))
2169 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2170 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2172 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2173 error
= xfs_bmbt_update(cur
, &LEFT
);
2179 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2181 * Setting all of a previous oldext extent to newext.
2182 * The right neighbor is contiguous, the left is not.
2184 PREV
.br_blockcount
+= RIGHT
.br_blockcount
;
2185 PREV
.br_state
= new->br_state
;
2187 xfs_iext_next(ifp
, icur
);
2188 xfs_iext_remove(ip
, icur
, state
);
2189 xfs_iext_prev(ifp
, icur
);
2190 xfs_iext_update_extent(ip
, state
, icur
, &PREV
);
2192 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2193 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
2195 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2197 rval
= XFS_ILOG_CORE
;
2198 error
= xfs_bmbt_lookup_eq(cur
, &RIGHT
, &i
);
2201 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2202 if ((error
= xfs_btree_delete(cur
, &i
)))
2204 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2205 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2207 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2208 error
= xfs_bmbt_update(cur
, &PREV
);
2214 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
2216 * Setting all of a previous oldext extent to newext.
2217 * Neither the left nor right neighbors are contiguous with
2220 PREV
.br_state
= new->br_state
;
2221 xfs_iext_update_extent(ip
, state
, icur
, &PREV
);
2224 rval
= XFS_ILOG_DEXT
;
2227 error
= xfs_bmbt_lookup_eq(cur
, new, &i
);
2230 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2231 error
= xfs_bmbt_update(cur
, &PREV
);
2237 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
:
2239 * Setting the first part of a previous oldext extent to newext.
2240 * The left neighbor is contiguous.
2242 LEFT
.br_blockcount
+= new->br_blockcount
;
2245 PREV
.br_startoff
+= new->br_blockcount
;
2246 PREV
.br_startblock
+= new->br_blockcount
;
2247 PREV
.br_blockcount
-= new->br_blockcount
;
2249 xfs_iext_update_extent(ip
, state
, icur
, &PREV
);
2250 xfs_iext_prev(ifp
, icur
);
2251 xfs_iext_update_extent(ip
, state
, icur
, &LEFT
);
2254 rval
= XFS_ILOG_DEXT
;
2257 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2260 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2261 error
= xfs_bmbt_update(cur
, &PREV
);
2264 error
= xfs_btree_decrement(cur
, 0, &i
);
2267 error
= xfs_bmbt_update(cur
, &LEFT
);
2273 case BMAP_LEFT_FILLING
:
2275 * Setting the first part of a previous oldext extent to newext.
2276 * The left neighbor is not contiguous.
2279 PREV
.br_startoff
+= new->br_blockcount
;
2280 PREV
.br_startblock
+= new->br_blockcount
;
2281 PREV
.br_blockcount
-= new->br_blockcount
;
2283 xfs_iext_update_extent(ip
, state
, icur
, &PREV
);
2284 xfs_iext_insert(ip
, icur
, new, state
);
2285 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2286 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
2288 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2290 rval
= XFS_ILOG_CORE
;
2291 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2294 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2295 error
= xfs_bmbt_update(cur
, &PREV
);
2298 cur
->bc_rec
.b
= *new;
2299 if ((error
= xfs_btree_insert(cur
, &i
)))
2301 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2305 case BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2307 * Setting the last part of a previous oldext extent to newext.
2308 * The right neighbor is contiguous with the new allocation.
2311 PREV
.br_blockcount
-= new->br_blockcount
;
2313 RIGHT
.br_startoff
= new->br_startoff
;
2314 RIGHT
.br_startblock
= new->br_startblock
;
2315 RIGHT
.br_blockcount
+= new->br_blockcount
;
2317 xfs_iext_update_extent(ip
, state
, icur
, &PREV
);
2318 xfs_iext_next(ifp
, icur
);
2319 xfs_iext_update_extent(ip
, state
, icur
, &RIGHT
);
2322 rval
= XFS_ILOG_DEXT
;
2325 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2328 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2329 error
= xfs_bmbt_update(cur
, &PREV
);
2332 error
= xfs_btree_increment(cur
, 0, &i
);
2335 error
= xfs_bmbt_update(cur
, &RIGHT
);
2341 case BMAP_RIGHT_FILLING
:
2343 * Setting the last part of a previous oldext extent to newext.
2344 * The right neighbor is not contiguous.
2347 PREV
.br_blockcount
-= new->br_blockcount
;
2349 xfs_iext_update_extent(ip
, state
, icur
, &PREV
);
2350 xfs_iext_next(ifp
, icur
);
2351 xfs_iext_insert(ip
, icur
, new, state
);
2353 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2354 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
2356 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2358 rval
= XFS_ILOG_CORE
;
2359 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2362 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2363 error
= xfs_bmbt_update(cur
, &PREV
);
2366 error
= xfs_bmbt_lookup_eq(cur
, new, &i
);
2369 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
2370 if ((error
= xfs_btree_insert(cur
, &i
)))
2372 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2378 * Setting the middle part of a previous oldext extent to
2379 * newext. Contiguity is impossible here.
2380 * One extent becomes three extents.
2383 PREV
.br_blockcount
= new->br_startoff
- PREV
.br_startoff
;
2386 r
[1].br_startoff
= new_endoff
;
2387 r
[1].br_blockcount
=
2388 old
.br_startoff
+ old
.br_blockcount
- new_endoff
;
2389 r
[1].br_startblock
= new->br_startblock
+ new->br_blockcount
;
2390 r
[1].br_state
= PREV
.br_state
;
2392 xfs_iext_update_extent(ip
, state
, icur
, &PREV
);
2393 xfs_iext_next(ifp
, icur
);
2394 xfs_iext_insert(ip
, icur
, &r
[1], state
);
2395 xfs_iext_insert(ip
, icur
, &r
[0], state
);
2397 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2398 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 2);
2400 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2402 rval
= XFS_ILOG_CORE
;
2403 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2406 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2407 /* new right extent - oldext */
2408 error
= xfs_bmbt_update(cur
, &r
[1]);
2411 /* new left extent - oldext */
2412 cur
->bc_rec
.b
= PREV
;
2413 if ((error
= xfs_btree_insert(cur
, &i
)))
2415 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2417 * Reset the cursor to the position of the new extent
2418 * we are about to insert as we can't trust it after
2419 * the previous insert.
2421 error
= xfs_bmbt_lookup_eq(cur
, new, &i
);
2424 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
2425 /* new middle extent - newext */
2426 if ((error
= xfs_btree_insert(cur
, &i
)))
2428 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2432 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2433 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2434 case BMAP_LEFT_FILLING
| BMAP_RIGHT_CONTIG
:
2435 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
2436 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2437 case BMAP_LEFT_CONTIG
:
2438 case BMAP_RIGHT_CONTIG
:
2440 * These cases are all impossible.
2445 /* update reverse mappings */
2446 error
= xfs_rmap_convert_extent(mp
, dfops
, ip
, whichfork
, new);
2450 /* convert to a btree if necessary */
2451 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
2452 int tmp_logflags
; /* partial log flag return val */
2454 ASSERT(cur
== NULL
);
2455 error
= xfs_bmap_extents_to_btree(tp
, ip
, &cur
, 0,
2456 &tmp_logflags
, whichfork
);
2457 *logflagsp
|= tmp_logflags
;
2462 /* clear out the allocated field, done with it now in any case. */
2464 cur
->bc_private
.b
.allocated
= 0;
2468 xfs_bmap_check_leaf_extents(*curp
, ip
, whichfork
);
2478 * Convert a hole to a delayed allocation.
2481 xfs_bmap_add_extent_hole_delay(
2482 xfs_inode_t
*ip
, /* incore inode pointer */
2484 struct xfs_iext_cursor
*icur
,
2485 xfs_bmbt_irec_t
*new) /* new data to add to file extents */
2487 xfs_ifork_t
*ifp
; /* inode fork pointer */
2488 xfs_bmbt_irec_t left
; /* left neighbor extent entry */
2489 xfs_filblks_t newlen
=0; /* new indirect size */
2490 xfs_filblks_t oldlen
=0; /* old indirect size */
2491 xfs_bmbt_irec_t right
; /* right neighbor extent entry */
2492 int state
= xfs_bmap_fork_to_state(whichfork
);
2493 xfs_filblks_t temp
; /* temp for indirect calculations */
2495 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
2496 ASSERT(isnullstartblock(new->br_startblock
));
2499 * Check and set flags if this segment has a left neighbor
2501 if (xfs_iext_peek_prev_extent(ifp
, icur
, &left
)) {
2502 state
|= BMAP_LEFT_VALID
;
2503 if (isnullstartblock(left
.br_startblock
))
2504 state
|= BMAP_LEFT_DELAY
;
2508 * Check and set flags if the current (right) segment exists.
2509 * If it doesn't exist, we're converting the hole at end-of-file.
2511 if (xfs_iext_get_extent(ifp
, icur
, &right
)) {
2512 state
|= BMAP_RIGHT_VALID
;
2513 if (isnullstartblock(right
.br_startblock
))
2514 state
|= BMAP_RIGHT_DELAY
;
2518 * Set contiguity flags on the left and right neighbors.
2519 * Don't let extents get too large, even if the pieces are contiguous.
2521 if ((state
& BMAP_LEFT_VALID
) && (state
& BMAP_LEFT_DELAY
) &&
2522 left
.br_startoff
+ left
.br_blockcount
== new->br_startoff
&&
2523 left
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
2524 state
|= BMAP_LEFT_CONTIG
;
2526 if ((state
& BMAP_RIGHT_VALID
) && (state
& BMAP_RIGHT_DELAY
) &&
2527 new->br_startoff
+ new->br_blockcount
== right
.br_startoff
&&
2528 new->br_blockcount
+ right
.br_blockcount
<= MAXEXTLEN
&&
2529 (!(state
& BMAP_LEFT_CONTIG
) ||
2530 (left
.br_blockcount
+ new->br_blockcount
+
2531 right
.br_blockcount
<= MAXEXTLEN
)))
2532 state
|= BMAP_RIGHT_CONTIG
;
2535 * Switch out based on the contiguity flags.
2537 switch (state
& (BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
)) {
2538 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2540 * New allocation is contiguous with delayed allocations
2541 * on the left and on the right.
2542 * Merge all three into a single extent record.
2544 temp
= left
.br_blockcount
+ new->br_blockcount
+
2545 right
.br_blockcount
;
2547 oldlen
= startblockval(left
.br_startblock
) +
2548 startblockval(new->br_startblock
) +
2549 startblockval(right
.br_startblock
);
2550 newlen
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
2552 left
.br_startblock
= nullstartblock(newlen
);
2553 left
.br_blockcount
= temp
;
2555 xfs_iext_remove(ip
, icur
, state
);
2556 xfs_iext_prev(ifp
, icur
);
2557 xfs_iext_update_extent(ip
, state
, icur
, &left
);
2560 case BMAP_LEFT_CONTIG
:
2562 * New allocation is contiguous with a delayed allocation
2564 * Merge the new allocation with the left neighbor.
2566 temp
= left
.br_blockcount
+ new->br_blockcount
;
2568 oldlen
= startblockval(left
.br_startblock
) +
2569 startblockval(new->br_startblock
);
2570 newlen
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
2572 left
.br_blockcount
= temp
;
2573 left
.br_startblock
= nullstartblock(newlen
);
2575 xfs_iext_prev(ifp
, icur
);
2576 xfs_iext_update_extent(ip
, state
, icur
, &left
);
2579 case BMAP_RIGHT_CONTIG
:
2581 * New allocation is contiguous with a delayed allocation
2583 * Merge the new allocation with the right neighbor.
2585 temp
= new->br_blockcount
+ right
.br_blockcount
;
2586 oldlen
= startblockval(new->br_startblock
) +
2587 startblockval(right
.br_startblock
);
2588 newlen
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
2590 right
.br_startoff
= new->br_startoff
;
2591 right
.br_startblock
= nullstartblock(newlen
);
2592 right
.br_blockcount
= temp
;
2593 xfs_iext_update_extent(ip
, state
, icur
, &right
);
2598 * New allocation is not contiguous with another
2599 * delayed allocation.
2600 * Insert a new entry.
2602 oldlen
= newlen
= 0;
2603 xfs_iext_insert(ip
, icur
, new, state
);
2606 if (oldlen
!= newlen
) {
2607 ASSERT(oldlen
> newlen
);
2608 xfs_mod_fdblocks(ip
->i_mount
, (int64_t)(oldlen
- newlen
),
2611 * Nothing to do for disk quota accounting here.
2617 * Convert a hole to a real allocation.
2619 STATIC
int /* error */
2620 xfs_bmap_add_extent_hole_real(
2621 struct xfs_trans
*tp
,
2622 struct xfs_inode
*ip
,
2624 struct xfs_iext_cursor
*icur
,
2625 struct xfs_btree_cur
**curp
,
2626 struct xfs_bmbt_irec
*new,
2630 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
2631 struct xfs_mount
*mp
= ip
->i_mount
;
2632 struct xfs_btree_cur
*cur
= *curp
;
2633 int error
; /* error return value */
2634 int i
; /* temp state */
2635 xfs_bmbt_irec_t left
; /* left neighbor extent entry */
2636 xfs_bmbt_irec_t right
; /* right neighbor extent entry */
2637 int rval
=0; /* return value (logging flags) */
2638 int state
= xfs_bmap_fork_to_state(whichfork
);
2639 struct xfs_bmbt_irec old
;
2641 ASSERT(!isnullstartblock(new->br_startblock
));
2642 ASSERT(!cur
|| !(cur
->bc_private
.b
.flags
& XFS_BTCUR_BPRV_WASDEL
));
2644 XFS_STATS_INC(mp
, xs_add_exlist
);
2647 * Check and set flags if this segment has a left neighbor.
2649 if (xfs_iext_peek_prev_extent(ifp
, icur
, &left
)) {
2650 state
|= BMAP_LEFT_VALID
;
2651 if (isnullstartblock(left
.br_startblock
))
2652 state
|= BMAP_LEFT_DELAY
;
2656 * Check and set flags if this segment has a current value.
2657 * Not true if we're inserting into the "hole" at eof.
2659 if (xfs_iext_get_extent(ifp
, icur
, &right
)) {
2660 state
|= BMAP_RIGHT_VALID
;
2661 if (isnullstartblock(right
.br_startblock
))
2662 state
|= BMAP_RIGHT_DELAY
;
2666 * We're inserting a real allocation between "left" and "right".
2667 * Set the contiguity flags. Don't let extents get too large.
2669 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
2670 left
.br_startoff
+ left
.br_blockcount
== new->br_startoff
&&
2671 left
.br_startblock
+ left
.br_blockcount
== new->br_startblock
&&
2672 left
.br_state
== new->br_state
&&
2673 left
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
2674 state
|= BMAP_LEFT_CONTIG
;
2676 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
2677 new->br_startoff
+ new->br_blockcount
== right
.br_startoff
&&
2678 new->br_startblock
+ new->br_blockcount
== right
.br_startblock
&&
2679 new->br_state
== right
.br_state
&&
2680 new->br_blockcount
+ right
.br_blockcount
<= MAXEXTLEN
&&
2681 (!(state
& BMAP_LEFT_CONTIG
) ||
2682 left
.br_blockcount
+ new->br_blockcount
+
2683 right
.br_blockcount
<= MAXEXTLEN
))
2684 state
|= BMAP_RIGHT_CONTIG
;
2688 * Select which case we're in here, and implement it.
2690 switch (state
& (BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
)) {
2691 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2693 * New allocation is contiguous with real allocations on the
2694 * left and on the right.
2695 * Merge all three into a single extent record.
2697 left
.br_blockcount
+= new->br_blockcount
+ right
.br_blockcount
;
2699 xfs_iext_remove(ip
, icur
, state
);
2700 xfs_iext_prev(ifp
, icur
);
2701 xfs_iext_update_extent(ip
, state
, icur
, &left
);
2703 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2704 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
2706 rval
= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
2708 rval
= XFS_ILOG_CORE
;
2709 error
= xfs_bmbt_lookup_eq(cur
, &right
, &i
);
2712 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2713 error
= xfs_btree_delete(cur
, &i
);
2716 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2717 error
= xfs_btree_decrement(cur
, 0, &i
);
2720 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2721 error
= xfs_bmbt_update(cur
, &left
);
2727 case BMAP_LEFT_CONTIG
:
2729 * New allocation is contiguous with a real allocation
2731 * Merge the new allocation with the left neighbor.
2734 left
.br_blockcount
+= new->br_blockcount
;
2736 xfs_iext_prev(ifp
, icur
);
2737 xfs_iext_update_extent(ip
, state
, icur
, &left
);
2740 rval
= xfs_ilog_fext(whichfork
);
2743 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2746 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2747 error
= xfs_bmbt_update(cur
, &left
);
2753 case BMAP_RIGHT_CONTIG
:
2755 * New allocation is contiguous with a real allocation
2757 * Merge the new allocation with the right neighbor.
2761 right
.br_startoff
= new->br_startoff
;
2762 right
.br_startblock
= new->br_startblock
;
2763 right
.br_blockcount
+= new->br_blockcount
;
2764 xfs_iext_update_extent(ip
, state
, icur
, &right
);
2767 rval
= xfs_ilog_fext(whichfork
);
2770 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2773 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2774 error
= xfs_bmbt_update(cur
, &right
);
2782 * New allocation is not contiguous with another
2784 * Insert a new entry.
2786 xfs_iext_insert(ip
, icur
, new, state
);
2787 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2788 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
2790 rval
= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
2792 rval
= XFS_ILOG_CORE
;
2793 error
= xfs_bmbt_lookup_eq(cur
, new, &i
);
2796 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
2797 error
= xfs_btree_insert(cur
, &i
);
2800 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2805 /* add reverse mapping unless caller opted out */
2806 if (!(flags
& XFS_BMAPI_NORMAP
)) {
2807 error
= xfs_rmap_map_extent(mp
, tp
->t_dfops
, ip
, whichfork
,
2813 /* convert to a btree if necessary */
2814 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
2815 int tmp_logflags
; /* partial log flag return val */
2817 ASSERT(cur
== NULL
);
2818 error
= xfs_bmap_extents_to_btree(tp
, ip
, curp
, 0,
2819 &tmp_logflags
, whichfork
);
2820 *logflagsp
|= tmp_logflags
;
2826 /* clear out the allocated field, done with it now in any case. */
2828 cur
->bc_private
.b
.allocated
= 0;
2830 xfs_bmap_check_leaf_extents(cur
, ip
, whichfork
);
2837 * Functions used in the extent read, allocate and remove paths
2841 * Adjust the size of the new extent based on di_extsize and rt extsize.
2844 xfs_bmap_extsize_align(
2846 xfs_bmbt_irec_t
*gotp
, /* next extent pointer */
2847 xfs_bmbt_irec_t
*prevp
, /* previous extent pointer */
2848 xfs_extlen_t extsz
, /* align to this extent size */
2849 int rt
, /* is this a realtime inode? */
2850 int eof
, /* is extent at end-of-file? */
2851 int delay
, /* creating delalloc extent? */
2852 int convert
, /* overwriting unwritten extent? */
2853 xfs_fileoff_t
*offp
, /* in/out: aligned offset */
2854 xfs_extlen_t
*lenp
) /* in/out: aligned length */
2856 xfs_fileoff_t orig_off
; /* original offset */
2857 xfs_extlen_t orig_alen
; /* original length */
2858 xfs_fileoff_t orig_end
; /* original off+len */
2859 xfs_fileoff_t nexto
; /* next file offset */
2860 xfs_fileoff_t prevo
; /* previous file offset */
2861 xfs_fileoff_t align_off
; /* temp for offset */
2862 xfs_extlen_t align_alen
; /* temp for length */
2863 xfs_extlen_t temp
; /* temp for calculations */
2868 orig_off
= align_off
= *offp
;
2869 orig_alen
= align_alen
= *lenp
;
2870 orig_end
= orig_off
+ orig_alen
;
2873 * If this request overlaps an existing extent, then don't
2874 * attempt to perform any additional alignment.
2876 if (!delay
&& !eof
&&
2877 (orig_off
>= gotp
->br_startoff
) &&
2878 (orig_end
<= gotp
->br_startoff
+ gotp
->br_blockcount
)) {
2883 * If the file offset is unaligned vs. the extent size
2884 * we need to align it. This will be possible unless
2885 * the file was previously written with a kernel that didn't
2886 * perform this alignment, or if a truncate shot us in the
2889 div_u64_rem(orig_off
, extsz
, &temp
);
2895 /* Same adjustment for the end of the requested area. */
2896 temp
= (align_alen
% extsz
);
2898 align_alen
+= extsz
- temp
;
2901 * For large extent hint sizes, the aligned extent might be larger than
2902 * MAXEXTLEN. In that case, reduce the size by an extsz so that it pulls
2903 * the length back under MAXEXTLEN. The outer allocation loops handle
2904 * short allocation just fine, so it is safe to do this. We only want to
2905 * do it when we are forced to, though, because it means more allocation
2906 * operations are required.
2908 while (align_alen
> MAXEXTLEN
)
2909 align_alen
-= extsz
;
2910 ASSERT(align_alen
<= MAXEXTLEN
);
2913 * If the previous block overlaps with this proposed allocation
2914 * then move the start forward without adjusting the length.
2916 if (prevp
->br_startoff
!= NULLFILEOFF
) {
2917 if (prevp
->br_startblock
== HOLESTARTBLOCK
)
2918 prevo
= prevp
->br_startoff
;
2920 prevo
= prevp
->br_startoff
+ prevp
->br_blockcount
;
2923 if (align_off
!= orig_off
&& align_off
< prevo
)
2926 * If the next block overlaps with this proposed allocation
2927 * then move the start back without adjusting the length,
2928 * but not before offset 0.
2929 * This may of course make the start overlap previous block,
2930 * and if we hit the offset 0 limit then the next block
2931 * can still overlap too.
2933 if (!eof
&& gotp
->br_startoff
!= NULLFILEOFF
) {
2934 if ((delay
&& gotp
->br_startblock
== HOLESTARTBLOCK
) ||
2935 (!delay
&& gotp
->br_startblock
== DELAYSTARTBLOCK
))
2936 nexto
= gotp
->br_startoff
+ gotp
->br_blockcount
;
2938 nexto
= gotp
->br_startoff
;
2940 nexto
= NULLFILEOFF
;
2942 align_off
+ align_alen
!= orig_end
&&
2943 align_off
+ align_alen
> nexto
)
2944 align_off
= nexto
> align_alen
? nexto
- align_alen
: 0;
2946 * If we're now overlapping the next or previous extent that
2947 * means we can't fit an extsz piece in this hole. Just move
2948 * the start forward to the first valid spot and set
2949 * the length so we hit the end.
2951 if (align_off
!= orig_off
&& align_off
< prevo
)
2953 if (align_off
+ align_alen
!= orig_end
&&
2954 align_off
+ align_alen
> nexto
&&
2955 nexto
!= NULLFILEOFF
) {
2956 ASSERT(nexto
> prevo
);
2957 align_alen
= nexto
- align_off
;
2961 * If realtime, and the result isn't a multiple of the realtime
2962 * extent size we need to remove blocks until it is.
2964 if (rt
&& (temp
= (align_alen
% mp
->m_sb
.sb_rextsize
))) {
2966 * We're not covering the original request, or
2967 * we won't be able to once we fix the length.
2969 if (orig_off
< align_off
||
2970 orig_end
> align_off
+ align_alen
||
2971 align_alen
- temp
< orig_alen
)
2974 * Try to fix it by moving the start up.
2976 if (align_off
+ temp
<= orig_off
) {
2981 * Try to fix it by moving the end in.
2983 else if (align_off
+ align_alen
- temp
>= orig_end
)
2986 * Set the start to the minimum then trim the length.
2989 align_alen
-= orig_off
- align_off
;
2990 align_off
= orig_off
;
2991 align_alen
-= align_alen
% mp
->m_sb
.sb_rextsize
;
2994 * Result doesn't cover the request, fail it.
2996 if (orig_off
< align_off
|| orig_end
> align_off
+ align_alen
)
2999 ASSERT(orig_off
>= align_off
);
3000 /* see MAXEXTLEN handling above */
3001 ASSERT(orig_end
<= align_off
+ align_alen
||
3002 align_alen
+ extsz
> MAXEXTLEN
);
3006 if (!eof
&& gotp
->br_startoff
!= NULLFILEOFF
)
3007 ASSERT(align_off
+ align_alen
<= gotp
->br_startoff
);
3008 if (prevp
->br_startoff
!= NULLFILEOFF
)
3009 ASSERT(align_off
>= prevp
->br_startoff
+ prevp
->br_blockcount
);
3017 #define XFS_ALLOC_GAP_UNITS 4
3021 struct xfs_bmalloca
*ap
) /* bmap alloc argument struct */
3023 xfs_fsblock_t adjust
; /* adjustment to block numbers */
3024 xfs_agnumber_t fb_agno
; /* ag number of ap->firstblock */
3025 xfs_mount_t
*mp
; /* mount point structure */
3026 int nullfb
; /* true if ap->firstblock isn't set */
3027 int rt
; /* true if inode is realtime */
3029 #define ISVALID(x,y) \
3031 (x) < mp->m_sb.sb_rblocks : \
3032 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
3033 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
3034 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
3036 mp
= ap
->ip
->i_mount
;
3037 nullfb
= ap
->tp
->t_firstblock
== NULLFSBLOCK
;
3038 rt
= XFS_IS_REALTIME_INODE(ap
->ip
) &&
3039 xfs_alloc_is_userdata(ap
->datatype
);
3040 fb_agno
= nullfb
? NULLAGNUMBER
: XFS_FSB_TO_AGNO(mp
,
3041 ap
->tp
->t_firstblock
);
3043 * If allocating at eof, and there's a previous real block,
3044 * try to use its last block as our starting point.
3046 if (ap
->eof
&& ap
->prev
.br_startoff
!= NULLFILEOFF
&&
3047 !isnullstartblock(ap
->prev
.br_startblock
) &&
3048 ISVALID(ap
->prev
.br_startblock
+ ap
->prev
.br_blockcount
,
3049 ap
->prev
.br_startblock
)) {
3050 ap
->blkno
= ap
->prev
.br_startblock
+ ap
->prev
.br_blockcount
;
3052 * Adjust for the gap between prevp and us.
3054 adjust
= ap
->offset
-
3055 (ap
->prev
.br_startoff
+ ap
->prev
.br_blockcount
);
3057 ISVALID(ap
->blkno
+ adjust
, ap
->prev
.br_startblock
))
3058 ap
->blkno
+= adjust
;
3061 * If not at eof, then compare the two neighbor blocks.
3062 * Figure out whether either one gives us a good starting point,
3063 * and pick the better one.
3065 else if (!ap
->eof
) {
3066 xfs_fsblock_t gotbno
; /* right side block number */
3067 xfs_fsblock_t gotdiff
=0; /* right side difference */
3068 xfs_fsblock_t prevbno
; /* left side block number */
3069 xfs_fsblock_t prevdiff
=0; /* left side difference */
3072 * If there's a previous (left) block, select a requested
3073 * start block based on it.
3075 if (ap
->prev
.br_startoff
!= NULLFILEOFF
&&
3076 !isnullstartblock(ap
->prev
.br_startblock
) &&
3077 (prevbno
= ap
->prev
.br_startblock
+
3078 ap
->prev
.br_blockcount
) &&
3079 ISVALID(prevbno
, ap
->prev
.br_startblock
)) {
3081 * Calculate gap to end of previous block.
3083 adjust
= prevdiff
= ap
->offset
-
3084 (ap
->prev
.br_startoff
+
3085 ap
->prev
.br_blockcount
);
3087 * Figure the startblock based on the previous block's
3088 * end and the gap size.
3090 * If the gap is large relative to the piece we're
3091 * allocating, or using it gives us an invalid block
3092 * number, then just use the end of the previous block.
3094 if (prevdiff
<= XFS_ALLOC_GAP_UNITS
* ap
->length
&&
3095 ISVALID(prevbno
+ prevdiff
,
3096 ap
->prev
.br_startblock
))
3101 * If the firstblock forbids it, can't use it,
3104 if (!rt
&& !nullfb
&&
3105 XFS_FSB_TO_AGNO(mp
, prevbno
) != fb_agno
)
3106 prevbno
= NULLFSBLOCK
;
3109 * No previous block or can't follow it, just default.
3112 prevbno
= NULLFSBLOCK
;
3114 * If there's a following (right) block, select a requested
3115 * start block based on it.
3117 if (!isnullstartblock(ap
->got
.br_startblock
)) {
3119 * Calculate gap to start of next block.
3121 adjust
= gotdiff
= ap
->got
.br_startoff
- ap
->offset
;
3123 * Figure the startblock based on the next block's
3124 * start and the gap size.
3126 gotbno
= ap
->got
.br_startblock
;
3129 * If the gap is large relative to the piece we're
3130 * allocating, or using it gives us an invalid block
3131 * number, then just use the start of the next block
3132 * offset by our length.
3134 if (gotdiff
<= XFS_ALLOC_GAP_UNITS
* ap
->length
&&
3135 ISVALID(gotbno
- gotdiff
, gotbno
))
3137 else if (ISVALID(gotbno
- ap
->length
, gotbno
)) {
3138 gotbno
-= ap
->length
;
3139 gotdiff
+= adjust
- ap
->length
;
3143 * If the firstblock forbids it, can't use it,
3146 if (!rt
&& !nullfb
&&
3147 XFS_FSB_TO_AGNO(mp
, gotbno
) != fb_agno
)
3148 gotbno
= NULLFSBLOCK
;
3151 * No next block, just default.
3154 gotbno
= NULLFSBLOCK
;
3156 * If both valid, pick the better one, else the only good
3157 * one, else ap->blkno is already set (to 0 or the inode block).
3159 if (prevbno
!= NULLFSBLOCK
&& gotbno
!= NULLFSBLOCK
)
3160 ap
->blkno
= prevdiff
<= gotdiff
? prevbno
: gotbno
;
3161 else if (prevbno
!= NULLFSBLOCK
)
3162 ap
->blkno
= prevbno
;
3163 else if (gotbno
!= NULLFSBLOCK
)
3170 xfs_bmap_longest_free_extent(
3171 struct xfs_trans
*tp
,
3176 struct xfs_mount
*mp
= tp
->t_mountp
;
3177 struct xfs_perag
*pag
;
3178 xfs_extlen_t longest
;
3181 pag
= xfs_perag_get(mp
, ag
);
3182 if (!pag
->pagf_init
) {
3183 error
= xfs_alloc_pagf_init(mp
, tp
, ag
, XFS_ALLOC_FLAG_TRYLOCK
);
3187 if (!pag
->pagf_init
) {
3193 longest
= xfs_alloc_longest_free_extent(pag
,
3194 xfs_alloc_min_freelist(mp
, pag
),
3195 xfs_ag_resv_needed(pag
, XFS_AG_RESV_NONE
));
3196 if (*blen
< longest
)
3205 xfs_bmap_select_minlen(
3206 struct xfs_bmalloca
*ap
,
3207 struct xfs_alloc_arg
*args
,
3211 if (notinit
|| *blen
< ap
->minlen
) {
3213 * Since we did a BUF_TRYLOCK above, it is possible that
3214 * there is space for this request.
3216 args
->minlen
= ap
->minlen
;
3217 } else if (*blen
< args
->maxlen
) {
3219 * If the best seen length is less than the request length,
3220 * use the best as the minimum.
3222 args
->minlen
= *blen
;
3225 * Otherwise we've seen an extent as big as maxlen, use that
3228 args
->minlen
= args
->maxlen
;
3233 xfs_bmap_btalloc_nullfb(
3234 struct xfs_bmalloca
*ap
,
3235 struct xfs_alloc_arg
*args
,
3238 struct xfs_mount
*mp
= ap
->ip
->i_mount
;
3239 xfs_agnumber_t ag
, startag
;
3243 args
->type
= XFS_ALLOCTYPE_START_BNO
;
3244 args
->total
= ap
->total
;
3246 startag
= ag
= XFS_FSB_TO_AGNO(mp
, args
->fsbno
);
3247 if (startag
== NULLAGNUMBER
)
3250 while (*blen
< args
->maxlen
) {
3251 error
= xfs_bmap_longest_free_extent(args
->tp
, ag
, blen
,
3256 if (++ag
== mp
->m_sb
.sb_agcount
)
3262 xfs_bmap_select_minlen(ap
, args
, blen
, notinit
);
3267 xfs_bmap_btalloc_filestreams(
3268 struct xfs_bmalloca
*ap
,
3269 struct xfs_alloc_arg
*args
,
3272 struct xfs_mount
*mp
= ap
->ip
->i_mount
;
3277 args
->type
= XFS_ALLOCTYPE_NEAR_BNO
;
3278 args
->total
= ap
->total
;
3280 ag
= XFS_FSB_TO_AGNO(mp
, args
->fsbno
);
3281 if (ag
== NULLAGNUMBER
)
3284 error
= xfs_bmap_longest_free_extent(args
->tp
, ag
, blen
, ¬init
);
3288 if (*blen
< args
->maxlen
) {
3289 error
= xfs_filestream_new_ag(ap
, &ag
);
3293 error
= xfs_bmap_longest_free_extent(args
->tp
, ag
, blen
,
3300 xfs_bmap_select_minlen(ap
, args
, blen
, notinit
);
3303 * Set the failure fallback case to look in the selected AG as stream
3306 ap
->blkno
= args
->fsbno
= XFS_AGB_TO_FSB(mp
, ag
, 0);
3310 /* Update all inode and quota accounting for the allocation we just did. */
3312 xfs_bmap_btalloc_accounting(
3313 struct xfs_bmalloca
*ap
,
3314 struct xfs_alloc_arg
*args
)
3316 if (ap
->flags
& XFS_BMAPI_COWFORK
) {
3318 * COW fork blocks are in-core only and thus are treated as
3319 * in-core quota reservation (like delalloc blocks) even when
3320 * converted to real blocks. The quota reservation is not
3321 * accounted to disk until blocks are remapped to the data
3322 * fork. So if these blocks were previously delalloc, we
3323 * already have quota reservation and there's nothing to do
3330 * Otherwise, we've allocated blocks in a hole. The transaction
3331 * has acquired in-core quota reservation for this extent.
3332 * Rather than account these as real blocks, however, we reduce
3333 * the transaction quota reservation based on the allocation.
3334 * This essentially transfers the transaction quota reservation
3335 * to that of a delalloc extent.
3337 ap
->ip
->i_delayed_blks
+= args
->len
;
3338 xfs_trans_mod_dquot_byino(ap
->tp
, ap
->ip
, XFS_TRANS_DQ_RES_BLKS
,
3343 /* data/attr fork only */
3344 ap
->ip
->i_d
.di_nblocks
+= args
->len
;
3345 xfs_trans_log_inode(ap
->tp
, ap
->ip
, XFS_ILOG_CORE
);
3347 ap
->ip
->i_delayed_blks
-= args
->len
;
3348 xfs_trans_mod_dquot_byino(ap
->tp
, ap
->ip
,
3349 ap
->wasdel
? XFS_TRANS_DQ_DELBCOUNT
: XFS_TRANS_DQ_BCOUNT
,
3355 struct xfs_bmalloca
*ap
) /* bmap alloc argument struct */
3357 xfs_mount_t
*mp
; /* mount point structure */
3358 xfs_alloctype_t atype
= 0; /* type for allocation routines */
3359 xfs_extlen_t align
= 0; /* minimum allocation alignment */
3360 xfs_agnumber_t fb_agno
; /* ag number of ap->firstblock */
3362 xfs_alloc_arg_t args
;
3363 xfs_fileoff_t orig_offset
;
3364 xfs_extlen_t orig_length
;
3366 xfs_extlen_t nextminlen
= 0;
3367 int nullfb
; /* true if ap->firstblock isn't set */
3374 orig_offset
= ap
->offset
;
3375 orig_length
= ap
->length
;
3377 mp
= ap
->ip
->i_mount
;
3379 /* stripe alignment for allocation is determined by mount parameters */
3381 if (mp
->m_swidth
&& (mp
->m_flags
& XFS_MOUNT_SWALLOC
))
3382 stripe_align
= mp
->m_swidth
;
3383 else if (mp
->m_dalign
)
3384 stripe_align
= mp
->m_dalign
;
3386 if (ap
->flags
& XFS_BMAPI_COWFORK
)
3387 align
= xfs_get_cowextsz_hint(ap
->ip
);
3388 else if (xfs_alloc_is_userdata(ap
->datatype
))
3389 align
= xfs_get_extsz_hint(ap
->ip
);
3391 error
= xfs_bmap_extsize_align(mp
, &ap
->got
, &ap
->prev
,
3392 align
, 0, ap
->eof
, 0, ap
->conv
,
3393 &ap
->offset
, &ap
->length
);
3399 nullfb
= ap
->tp
->t_firstblock
== NULLFSBLOCK
;
3400 fb_agno
= nullfb
? NULLAGNUMBER
: XFS_FSB_TO_AGNO(mp
,
3401 ap
->tp
->t_firstblock
);
3403 if (xfs_alloc_is_userdata(ap
->datatype
) &&
3404 xfs_inode_is_filestream(ap
->ip
)) {
3405 ag
= xfs_filestream_lookup_ag(ap
->ip
);
3406 ag
= (ag
!= NULLAGNUMBER
) ? ag
: 0;
3407 ap
->blkno
= XFS_AGB_TO_FSB(mp
, ag
, 0);
3409 ap
->blkno
= XFS_INO_TO_FSB(mp
, ap
->ip
->i_ino
);
3412 ap
->blkno
= ap
->tp
->t_firstblock
;
3414 xfs_bmap_adjacent(ap
);
3417 * If allowed, use ap->blkno; otherwise must use firstblock since
3418 * it's in the right allocation group.
3420 if (nullfb
|| XFS_FSB_TO_AGNO(mp
, ap
->blkno
) == fb_agno
)
3423 ap
->blkno
= ap
->tp
->t_firstblock
;
3425 * Normal allocation, done through xfs_alloc_vextent.
3427 tryagain
= isaligned
= 0;
3428 memset(&args
, 0, sizeof(args
));
3431 args
.fsbno
= ap
->blkno
;
3432 xfs_rmap_skip_owner_update(&args
.oinfo
);
3434 /* Trim the allocation back to the maximum an AG can fit. */
3435 args
.maxlen
= min(ap
->length
, mp
->m_ag_max_usable
);
3439 * Search for an allocation group with a single extent large
3440 * enough for the request. If one isn't found, then adjust
3441 * the minimum allocation size to the largest space found.
3443 if (xfs_alloc_is_userdata(ap
->datatype
) &&
3444 xfs_inode_is_filestream(ap
->ip
))
3445 error
= xfs_bmap_btalloc_filestreams(ap
, &args
, &blen
);
3447 error
= xfs_bmap_btalloc_nullfb(ap
, &args
, &blen
);
3450 } else if (ap
->tp
->t_dfops
->dop_low
) {
3451 if (xfs_inode_is_filestream(ap
->ip
))
3452 args
.type
= XFS_ALLOCTYPE_FIRST_AG
;
3454 args
.type
= XFS_ALLOCTYPE_START_BNO
;
3455 args
.total
= args
.minlen
= ap
->minlen
;
3457 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
3458 args
.total
= ap
->total
;
3459 args
.minlen
= ap
->minlen
;
3461 /* apply extent size hints if obtained earlier */
3464 div_u64_rem(ap
->offset
, args
.prod
, &args
.mod
);
3466 args
.mod
= args
.prod
- args
.mod
;
3467 } else if (mp
->m_sb
.sb_blocksize
>= PAGE_SIZE
) {
3471 args
.prod
= PAGE_SIZE
>> mp
->m_sb
.sb_blocklog
;
3472 div_u64_rem(ap
->offset
, args
.prod
, &args
.mod
);
3474 args
.mod
= args
.prod
- args
.mod
;
3477 * If we are not low on available data blocks, and the
3478 * underlying logical volume manager is a stripe, and
3479 * the file offset is zero then try to allocate data
3480 * blocks on stripe unit boundary.
3481 * NOTE: ap->aeof is only set if the allocation length
3482 * is >= the stripe unit and the allocation offset is
3483 * at the end of file.
3485 if (!ap
->tp
->t_dfops
->dop_low
&& ap
->aeof
) {
3487 args
.alignment
= stripe_align
;
3491 * Adjust for alignment
3493 if (blen
> args
.alignment
&& blen
<= args
.maxlen
)
3494 args
.minlen
= blen
- args
.alignment
;
3495 args
.minalignslop
= 0;
3498 * First try an exact bno allocation.
3499 * If it fails then do a near or start bno
3500 * allocation with alignment turned on.
3504 args
.type
= XFS_ALLOCTYPE_THIS_BNO
;
3507 * Compute the minlen+alignment for the
3508 * next case. Set slop so that the value
3509 * of minlen+alignment+slop doesn't go up
3510 * between the calls.
3512 if (blen
> stripe_align
&& blen
<= args
.maxlen
)
3513 nextminlen
= blen
- stripe_align
;
3515 nextminlen
= args
.minlen
;
3516 if (nextminlen
+ stripe_align
> args
.minlen
+ 1)
3518 nextminlen
+ stripe_align
-
3521 args
.minalignslop
= 0;
3525 args
.minalignslop
= 0;
3527 args
.minleft
= ap
->minleft
;
3528 args
.wasdel
= ap
->wasdel
;
3529 args
.resv
= XFS_AG_RESV_NONE
;
3530 args
.datatype
= ap
->datatype
;
3531 if (ap
->datatype
& XFS_ALLOC_USERDATA_ZERO
)
3534 error
= xfs_alloc_vextent(&args
);
3538 if (tryagain
&& args
.fsbno
== NULLFSBLOCK
) {
3540 * Exact allocation failed. Now try with alignment
3544 args
.fsbno
= ap
->blkno
;
3545 args
.alignment
= stripe_align
;
3546 args
.minlen
= nextminlen
;
3547 args
.minalignslop
= 0;
3549 if ((error
= xfs_alloc_vextent(&args
)))
3552 if (isaligned
&& args
.fsbno
== NULLFSBLOCK
) {
3554 * allocation failed, so turn off alignment and
3558 args
.fsbno
= ap
->blkno
;
3560 if ((error
= xfs_alloc_vextent(&args
)))
3563 if (args
.fsbno
== NULLFSBLOCK
&& nullfb
&&
3564 args
.minlen
> ap
->minlen
) {
3565 args
.minlen
= ap
->minlen
;
3566 args
.type
= XFS_ALLOCTYPE_START_BNO
;
3567 args
.fsbno
= ap
->blkno
;
3568 if ((error
= xfs_alloc_vextent(&args
)))
3571 if (args
.fsbno
== NULLFSBLOCK
&& nullfb
) {
3573 args
.type
= XFS_ALLOCTYPE_FIRST_AG
;
3574 args
.total
= ap
->minlen
;
3575 if ((error
= xfs_alloc_vextent(&args
)))
3577 ap
->tp
->t_dfops
->dop_low
= true;
3579 if (args
.fsbno
!= NULLFSBLOCK
) {
3581 * check the allocation happened at the same or higher AG than
3582 * the first block that was allocated.
3584 ASSERT(ap
->tp
->t_firstblock
== NULLFSBLOCK
||
3585 XFS_FSB_TO_AGNO(mp
, ap
->tp
->t_firstblock
) <=
3586 XFS_FSB_TO_AGNO(mp
, args
.fsbno
));
3588 ap
->blkno
= args
.fsbno
;
3589 if (ap
->tp
->t_firstblock
== NULLFSBLOCK
)
3590 ap
->tp
->t_firstblock
= args
.fsbno
;
3591 ASSERT(nullfb
|| fb_agno
<= args
.agno
);
3592 ap
->length
= args
.len
;
3594 * If the extent size hint is active, we tried to round the
3595 * caller's allocation request offset down to extsz and the
3596 * length up to another extsz boundary. If we found a free
3597 * extent we mapped it in starting at this new offset. If the
3598 * newly mapped space isn't long enough to cover any of the
3599 * range of offsets that was originally requested, move the
3600 * mapping up so that we can fill as much of the caller's
3601 * original request as possible. Free space is apparently
3602 * very fragmented so we're unlikely to be able to satisfy the
3605 if (ap
->length
<= orig_length
)
3606 ap
->offset
= orig_offset
;
3607 else if (ap
->offset
+ ap
->length
< orig_offset
+ orig_length
)
3608 ap
->offset
= orig_offset
+ orig_length
- ap
->length
;
3609 xfs_bmap_btalloc_accounting(ap
, &args
);
3611 ap
->blkno
= NULLFSBLOCK
;
3618 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
3619 * It figures out where to ask the underlying allocator to put the new extent.
3623 struct xfs_bmalloca
*ap
) /* bmap alloc argument struct */
3625 if (XFS_IS_REALTIME_INODE(ap
->ip
) &&
3626 xfs_alloc_is_userdata(ap
->datatype
))
3627 return xfs_bmap_rtalloc(ap
);
3628 return xfs_bmap_btalloc(ap
);
3631 /* Trim extent to fit a logical block range. */
3634 struct xfs_bmbt_irec
*irec
,
3638 xfs_fileoff_t distance
;
3639 xfs_fileoff_t end
= bno
+ len
;
3641 if (irec
->br_startoff
+ irec
->br_blockcount
<= bno
||
3642 irec
->br_startoff
>= end
) {
3643 irec
->br_blockcount
= 0;
3647 if (irec
->br_startoff
< bno
) {
3648 distance
= bno
- irec
->br_startoff
;
3649 if (isnullstartblock(irec
->br_startblock
))
3650 irec
->br_startblock
= DELAYSTARTBLOCK
;
3651 if (irec
->br_startblock
!= DELAYSTARTBLOCK
&&
3652 irec
->br_startblock
!= HOLESTARTBLOCK
)
3653 irec
->br_startblock
+= distance
;
3654 irec
->br_startoff
+= distance
;
3655 irec
->br_blockcount
-= distance
;
3658 if (end
< irec
->br_startoff
+ irec
->br_blockcount
) {
3659 distance
= irec
->br_startoff
+ irec
->br_blockcount
- end
;
3660 irec
->br_blockcount
-= distance
;
3664 /* trim extent to within eof */
3666 xfs_trim_extent_eof(
3667 struct xfs_bmbt_irec
*irec
,
3668 struct xfs_inode
*ip
)
3671 xfs_trim_extent(irec
, 0, XFS_B_TO_FSB(ip
->i_mount
,
3672 i_size_read(VFS_I(ip
))));
3676 * Trim the returned map to the required bounds
3680 struct xfs_bmbt_irec
*mval
,
3681 struct xfs_bmbt_irec
*got
,
3689 if ((flags
& XFS_BMAPI_ENTIRE
) ||
3690 got
->br_startoff
+ got
->br_blockcount
<= obno
) {
3692 if (isnullstartblock(got
->br_startblock
))
3693 mval
->br_startblock
= DELAYSTARTBLOCK
;
3699 ASSERT((*bno
>= obno
) || (n
== 0));
3701 mval
->br_startoff
= *bno
;
3702 if (isnullstartblock(got
->br_startblock
))
3703 mval
->br_startblock
= DELAYSTARTBLOCK
;
3705 mval
->br_startblock
= got
->br_startblock
+
3706 (*bno
- got
->br_startoff
);
3708 * Return the minimum of what we got and what we asked for for
3709 * the length. We can use the len variable here because it is
3710 * modified below and we could have been there before coming
3711 * here if the first part of the allocation didn't overlap what
3714 mval
->br_blockcount
= XFS_FILBLKS_MIN(end
- *bno
,
3715 got
->br_blockcount
- (*bno
- got
->br_startoff
));
3716 mval
->br_state
= got
->br_state
;
3717 ASSERT(mval
->br_blockcount
<= len
);
3722 * Update and validate the extent map to return
3725 xfs_bmapi_update_map(
3726 struct xfs_bmbt_irec
**map
,
3734 xfs_bmbt_irec_t
*mval
= *map
;
3736 ASSERT((flags
& XFS_BMAPI_ENTIRE
) ||
3737 ((mval
->br_startoff
+ mval
->br_blockcount
) <= end
));
3738 ASSERT((flags
& XFS_BMAPI_ENTIRE
) || (mval
->br_blockcount
<= *len
) ||
3739 (mval
->br_startoff
< obno
));
3741 *bno
= mval
->br_startoff
+ mval
->br_blockcount
;
3743 if (*n
> 0 && mval
->br_startoff
== mval
[-1].br_startoff
) {
3744 /* update previous map with new information */
3745 ASSERT(mval
->br_startblock
== mval
[-1].br_startblock
);
3746 ASSERT(mval
->br_blockcount
> mval
[-1].br_blockcount
);
3747 ASSERT(mval
->br_state
== mval
[-1].br_state
);
3748 mval
[-1].br_blockcount
= mval
->br_blockcount
;
3749 mval
[-1].br_state
= mval
->br_state
;
3750 } else if (*n
> 0 && mval
->br_startblock
!= DELAYSTARTBLOCK
&&
3751 mval
[-1].br_startblock
!= DELAYSTARTBLOCK
&&
3752 mval
[-1].br_startblock
!= HOLESTARTBLOCK
&&
3753 mval
->br_startblock
== mval
[-1].br_startblock
+
3754 mval
[-1].br_blockcount
&&
3755 mval
[-1].br_state
== mval
->br_state
) {
3756 ASSERT(mval
->br_startoff
==
3757 mval
[-1].br_startoff
+ mval
[-1].br_blockcount
);
3758 mval
[-1].br_blockcount
+= mval
->br_blockcount
;
3759 } else if (*n
> 0 &&
3760 mval
->br_startblock
== DELAYSTARTBLOCK
&&
3761 mval
[-1].br_startblock
== DELAYSTARTBLOCK
&&
3762 mval
->br_startoff
==
3763 mval
[-1].br_startoff
+ mval
[-1].br_blockcount
) {
3764 mval
[-1].br_blockcount
+= mval
->br_blockcount
;
3765 mval
[-1].br_state
= mval
->br_state
;
3766 } else if (!((*n
== 0) &&
3767 ((mval
->br_startoff
+ mval
->br_blockcount
) <=
3776 * Map file blocks to filesystem blocks without allocation.
3780 struct xfs_inode
*ip
,
3783 struct xfs_bmbt_irec
*mval
,
3787 struct xfs_mount
*mp
= ip
->i_mount
;
3788 struct xfs_ifork
*ifp
;
3789 struct xfs_bmbt_irec got
;
3792 struct xfs_iext_cursor icur
;
3796 int whichfork
= xfs_bmapi_whichfork(flags
);
3799 ASSERT(!(flags
& ~(XFS_BMAPI_ATTRFORK
|XFS_BMAPI_ENTIRE
|
3800 XFS_BMAPI_COWFORK
)));
3801 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_SHARED
|XFS_ILOCK_EXCL
));
3803 if (unlikely(XFS_TEST_ERROR(
3804 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
3805 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
3806 mp
, XFS_ERRTAG_BMAPIFORMAT
))) {
3807 XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW
, mp
);
3808 return -EFSCORRUPTED
;
3811 if (XFS_FORCED_SHUTDOWN(mp
))
3814 XFS_STATS_INC(mp
, xs_blk_mapr
);
3816 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
3818 /* No CoW fork? Return a hole. */
3819 if (whichfork
== XFS_COW_FORK
&& !ifp
) {
3820 mval
->br_startoff
= bno
;
3821 mval
->br_startblock
= HOLESTARTBLOCK
;
3822 mval
->br_blockcount
= len
;
3823 mval
->br_state
= XFS_EXT_NORM
;
3828 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
3829 error
= xfs_iread_extents(NULL
, ip
, whichfork
);
3834 if (!xfs_iext_lookup_extent(ip
, ifp
, bno
, &icur
, &got
))
3839 while (bno
< end
&& n
< *nmap
) {
3840 /* Reading past eof, act as though there's a hole up to end. */
3842 got
.br_startoff
= end
;
3843 if (got
.br_startoff
> bno
) {
3844 /* Reading in a hole. */
3845 mval
->br_startoff
= bno
;
3846 mval
->br_startblock
= HOLESTARTBLOCK
;
3847 mval
->br_blockcount
=
3848 XFS_FILBLKS_MIN(len
, got
.br_startoff
- bno
);
3849 mval
->br_state
= XFS_EXT_NORM
;
3850 bno
+= mval
->br_blockcount
;
3851 len
-= mval
->br_blockcount
;
3857 /* set up the extent map to return. */
3858 xfs_bmapi_trim_map(mval
, &got
, &bno
, len
, obno
, end
, n
, flags
);
3859 xfs_bmapi_update_map(&mval
, &bno
, &len
, obno
, end
, &n
, flags
);
3861 /* If we're done, stop now. */
3862 if (bno
>= end
|| n
>= *nmap
)
3865 /* Else go on to the next record. */
3866 if (!xfs_iext_next_extent(ifp
, &icur
, &got
))
3874 * Add a delayed allocation extent to an inode. Blocks are reserved from the
3875 * global pool and the extent inserted into the inode in-core extent tree.
3877 * On entry, got refers to the first extent beyond the offset of the extent to
3878 * allocate or eof is specified if no such extent exists. On return, got refers
3879 * to the extent record that was inserted to the inode fork.
3881 * Note that the allocated extent may have been merged with contiguous extents
3882 * during insertion into the inode fork. Thus, got does not reflect the current
3883 * state of the inode fork on return. If necessary, the caller can use lastx to
3884 * look up the updated record in the inode fork.
3887 xfs_bmapi_reserve_delalloc(
3888 struct xfs_inode
*ip
,
3892 xfs_filblks_t prealloc
,
3893 struct xfs_bmbt_irec
*got
,
3894 struct xfs_iext_cursor
*icur
,
3897 struct xfs_mount
*mp
= ip
->i_mount
;
3898 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
3900 xfs_extlen_t indlen
;
3902 xfs_fileoff_t aoff
= off
;
3905 * Cap the alloc length. Keep track of prealloc so we know whether to
3906 * tag the inode before we return.
3908 alen
= XFS_FILBLKS_MIN(len
+ prealloc
, MAXEXTLEN
);
3910 alen
= XFS_FILBLKS_MIN(alen
, got
->br_startoff
- aoff
);
3911 if (prealloc
&& alen
>= len
)
3912 prealloc
= alen
- len
;
3914 /* Figure out the extent size, adjust alen */
3915 if (whichfork
== XFS_COW_FORK
) {
3916 struct xfs_bmbt_irec prev
;
3917 xfs_extlen_t extsz
= xfs_get_cowextsz_hint(ip
);
3919 if (!xfs_iext_peek_prev_extent(ifp
, icur
, &prev
))
3920 prev
.br_startoff
= NULLFILEOFF
;
3922 error
= xfs_bmap_extsize_align(mp
, got
, &prev
, extsz
, 0, eof
,
3923 1, 0, &aoff
, &alen
);
3928 * Make a transaction-less quota reservation for delayed allocation
3929 * blocks. This number gets adjusted later. We return if we haven't
3930 * allocated blocks already inside this loop.
3932 error
= xfs_trans_reserve_quota_nblks(NULL
, ip
, (long)alen
, 0,
3933 XFS_QMOPT_RES_REGBLKS
);
3938 * Split changing sb for alen and indlen since they could be coming
3939 * from different places.
3941 indlen
= (xfs_extlen_t
)xfs_bmap_worst_indlen(ip
, alen
);
3944 error
= xfs_mod_fdblocks(mp
, -((int64_t)alen
), false);
3946 goto out_unreserve_quota
;
3948 error
= xfs_mod_fdblocks(mp
, -((int64_t)indlen
), false);
3950 goto out_unreserve_blocks
;
3953 ip
->i_delayed_blks
+= alen
;
3955 got
->br_startoff
= aoff
;
3956 got
->br_startblock
= nullstartblock(indlen
);
3957 got
->br_blockcount
= alen
;
3958 got
->br_state
= XFS_EXT_NORM
;
3960 xfs_bmap_add_extent_hole_delay(ip
, whichfork
, icur
, got
);
3963 * Tag the inode if blocks were preallocated. Note that COW fork
3964 * preallocation can occur at the start or end of the extent, even when
3965 * prealloc == 0, so we must also check the aligned offset and length.
3967 if (whichfork
== XFS_DATA_FORK
&& prealloc
)
3968 xfs_inode_set_eofblocks_tag(ip
);
3969 if (whichfork
== XFS_COW_FORK
&& (prealloc
|| aoff
< off
|| alen
> len
))
3970 xfs_inode_set_cowblocks_tag(ip
);
3974 out_unreserve_blocks
:
3975 xfs_mod_fdblocks(mp
, alen
, false);
3976 out_unreserve_quota
:
3977 if (XFS_IS_QUOTA_ON(mp
))
3978 xfs_trans_unreserve_quota_nblks(NULL
, ip
, (long)alen
, 0,
3979 XFS_QMOPT_RES_REGBLKS
);
3985 struct xfs_bmalloca
*bma
)
3987 struct xfs_mount
*mp
= bma
->ip
->i_mount
;
3988 int whichfork
= xfs_bmapi_whichfork(bma
->flags
);
3989 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
3990 int tmp_logflags
= 0;
3993 ASSERT(bma
->length
> 0);
3996 * For the wasdelay case, we could also just allocate the stuff asked
3997 * for in this bmap call but that wouldn't be as good.
4000 bma
->length
= (xfs_extlen_t
)bma
->got
.br_blockcount
;
4001 bma
->offset
= bma
->got
.br_startoff
;
4002 xfs_iext_peek_prev_extent(ifp
, &bma
->icur
, &bma
->prev
);
4004 bma
->length
= XFS_FILBLKS_MIN(bma
->length
, MAXEXTLEN
);
4006 bma
->length
= XFS_FILBLKS_MIN(bma
->length
,
4007 bma
->got
.br_startoff
- bma
->offset
);
4011 * Set the data type being allocated. For the data fork, the first data
4012 * in the file is treated differently to all other allocations. For the
4013 * attribute fork, we only need to ensure the allocated range is not on
4016 if (!(bma
->flags
& XFS_BMAPI_METADATA
)) {
4017 bma
->datatype
= XFS_ALLOC_NOBUSY
;
4018 if (whichfork
== XFS_DATA_FORK
) {
4019 if (bma
->offset
== 0)
4020 bma
->datatype
|= XFS_ALLOC_INITIAL_USER_DATA
;
4022 bma
->datatype
|= XFS_ALLOC_USERDATA
;
4024 if (bma
->flags
& XFS_BMAPI_ZERO
)
4025 bma
->datatype
|= XFS_ALLOC_USERDATA_ZERO
;
4028 bma
->minlen
= (bma
->flags
& XFS_BMAPI_CONTIG
) ? bma
->length
: 1;
4031 * Only want to do the alignment at the eof if it is userdata and
4032 * allocation length is larger than a stripe unit.
4034 if (mp
->m_dalign
&& bma
->length
>= mp
->m_dalign
&&
4035 !(bma
->flags
& XFS_BMAPI_METADATA
) && whichfork
== XFS_DATA_FORK
) {
4036 error
= xfs_bmap_isaeof(bma
, whichfork
);
4041 error
= xfs_bmap_alloc(bma
);
4045 if (bma
->blkno
== NULLFSBLOCK
)
4047 if ((ifp
->if_flags
& XFS_IFBROOT
) && !bma
->cur
)
4048 bma
->cur
= xfs_bmbt_init_cursor(mp
, bma
->tp
, bma
->ip
, whichfork
);
4050 * Bump the number of extents we've allocated
4056 bma
->cur
->bc_private
.b
.flags
=
4057 bma
->wasdel
? XFS_BTCUR_BPRV_WASDEL
: 0;
4059 bma
->got
.br_startoff
= bma
->offset
;
4060 bma
->got
.br_startblock
= bma
->blkno
;
4061 bma
->got
.br_blockcount
= bma
->length
;
4062 bma
->got
.br_state
= XFS_EXT_NORM
;
4065 * In the data fork, a wasdelay extent has been initialized, so
4066 * shouldn't be flagged as unwritten.
4068 * For the cow fork, however, we convert delalloc reservations
4069 * (extents allocated for speculative preallocation) to
4070 * allocated unwritten extents, and only convert the unwritten
4071 * extents to real extents when we're about to write the data.
4073 if ((!bma
->wasdel
|| (bma
->flags
& XFS_BMAPI_COWFORK
)) &&
4074 (bma
->flags
& XFS_BMAPI_PREALLOC
) &&
4075 xfs_sb_version_hasextflgbit(&mp
->m_sb
))
4076 bma
->got
.br_state
= XFS_EXT_UNWRITTEN
;
4079 error
= xfs_bmap_add_extent_delay_real(bma
, whichfork
);
4081 error
= xfs_bmap_add_extent_hole_real(bma
->tp
, bma
->ip
,
4082 whichfork
, &bma
->icur
, &bma
->cur
, &bma
->got
,
4083 &bma
->logflags
, bma
->flags
);
4085 bma
->logflags
|= tmp_logflags
;
4090 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4091 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4092 * the neighbouring ones.
4094 xfs_iext_get_extent(ifp
, &bma
->icur
, &bma
->got
);
4096 ASSERT(bma
->got
.br_startoff
<= bma
->offset
);
4097 ASSERT(bma
->got
.br_startoff
+ bma
->got
.br_blockcount
>=
4098 bma
->offset
+ bma
->length
);
4099 ASSERT(bma
->got
.br_state
== XFS_EXT_NORM
||
4100 bma
->got
.br_state
== XFS_EXT_UNWRITTEN
);
4105 xfs_bmapi_convert_unwritten(
4106 struct xfs_bmalloca
*bma
,
4107 struct xfs_bmbt_irec
*mval
,
4111 int whichfork
= xfs_bmapi_whichfork(flags
);
4112 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
4113 int tmp_logflags
= 0;
4116 /* check if we need to do unwritten->real conversion */
4117 if (mval
->br_state
== XFS_EXT_UNWRITTEN
&&
4118 (flags
& XFS_BMAPI_PREALLOC
))
4121 /* check if we need to do real->unwritten conversion */
4122 if (mval
->br_state
== XFS_EXT_NORM
&&
4123 (flags
& (XFS_BMAPI_PREALLOC
| XFS_BMAPI_CONVERT
)) !=
4124 (XFS_BMAPI_PREALLOC
| XFS_BMAPI_CONVERT
))
4128 * Modify (by adding) the state flag, if writing.
4130 ASSERT(mval
->br_blockcount
<= len
);
4131 if ((ifp
->if_flags
& XFS_IFBROOT
) && !bma
->cur
) {
4132 bma
->cur
= xfs_bmbt_init_cursor(bma
->ip
->i_mount
, bma
->tp
,
4133 bma
->ip
, whichfork
);
4135 mval
->br_state
= (mval
->br_state
== XFS_EXT_UNWRITTEN
)
4136 ? XFS_EXT_NORM
: XFS_EXT_UNWRITTEN
;
4139 * Before insertion into the bmbt, zero the range being converted
4142 if (flags
& XFS_BMAPI_ZERO
) {
4143 error
= xfs_zero_extent(bma
->ip
, mval
->br_startblock
,
4144 mval
->br_blockcount
);
4149 error
= xfs_bmap_add_extent_unwritten_real(bma
->tp
, bma
->ip
, whichfork
,
4150 &bma
->icur
, &bma
->cur
, mval
, &tmp_logflags
);
4152 * Log the inode core unconditionally in the unwritten extent conversion
4153 * path because the conversion might not have done so (e.g., if the
4154 * extent count hasn't changed). We need to make sure the inode is dirty
4155 * in the transaction for the sake of fsync(), even if nothing has
4156 * changed, because fsync() will not force the log for this transaction
4157 * unless it sees the inode pinned.
4159 * Note: If we're only converting cow fork extents, there aren't
4160 * any on-disk updates to make, so we don't need to log anything.
4162 if (whichfork
!= XFS_COW_FORK
)
4163 bma
->logflags
|= tmp_logflags
| XFS_ILOG_CORE
;
4168 * Update our extent pointer, given that
4169 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4170 * of the neighbouring ones.
4172 xfs_iext_get_extent(ifp
, &bma
->icur
, &bma
->got
);
4175 * We may have combined previously unwritten space with written space,
4176 * so generate another request.
4178 if (mval
->br_blockcount
< len
)
4184 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4185 * extent state if necessary. Details behaviour is controlled by the flags
4186 * parameter. Only allocates blocks from a single allocation group, to avoid
4191 struct xfs_trans
*tp
, /* transaction pointer */
4192 struct xfs_inode
*ip
, /* incore inode */
4193 xfs_fileoff_t bno
, /* starting file offs. mapped */
4194 xfs_filblks_t len
, /* length to map in file */
4195 int flags
, /* XFS_BMAPI_... */
4196 xfs_extlen_t total
, /* total blocks needed */
4197 struct xfs_bmbt_irec
*mval
, /* output: map values */
4198 int *nmap
) /* i/o: mval size/count */
4200 struct xfs_mount
*mp
= ip
->i_mount
;
4201 struct xfs_ifork
*ifp
;
4202 struct xfs_bmalloca bma
= { NULL
}; /* args for xfs_bmap_alloc */
4203 xfs_fileoff_t end
; /* end of mapped file region */
4204 bool eof
= false; /* after the end of extents */
4205 int error
; /* error return */
4206 int n
; /* current extent index */
4207 xfs_fileoff_t obno
; /* old block number (offset) */
4208 int whichfork
; /* data or attr fork */
4211 xfs_fileoff_t orig_bno
; /* original block number value */
4212 int orig_flags
; /* original flags arg value */
4213 xfs_filblks_t orig_len
; /* original value of len arg */
4214 struct xfs_bmbt_irec
*orig_mval
; /* original value of mval */
4215 int orig_nmap
; /* original value of *nmap */
4223 whichfork
= xfs_bmapi_whichfork(flags
);
4226 ASSERT(*nmap
<= XFS_BMAP_MAX_NMAP
);
4227 ASSERT(tp
!= NULL
||
4228 (flags
& (XFS_BMAPI_CONVERT
| XFS_BMAPI_COWFORK
)) ==
4229 (XFS_BMAPI_CONVERT
| XFS_BMAPI_COWFORK
));
4231 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_LOCAL
);
4232 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
4233 ASSERT(!(flags
& XFS_BMAPI_REMAP
));
4235 /* zeroing is for currently only for data extents, not metadata */
4236 ASSERT((flags
& (XFS_BMAPI_METADATA
| XFS_BMAPI_ZERO
)) !=
4237 (XFS_BMAPI_METADATA
| XFS_BMAPI_ZERO
));
4239 * we can allocate unwritten extents or pre-zero allocated blocks,
4240 * but it makes no sense to do both at once. This would result in
4241 * zeroing the unwritten extent twice, but it still being an
4242 * unwritten extent....
4244 ASSERT((flags
& (XFS_BMAPI_PREALLOC
| XFS_BMAPI_ZERO
)) !=
4245 (XFS_BMAPI_PREALLOC
| XFS_BMAPI_ZERO
));
4247 if (unlikely(XFS_TEST_ERROR(
4248 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
4249 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
4250 mp
, XFS_ERRTAG_BMAPIFORMAT
))) {
4251 XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW
, mp
);
4252 return -EFSCORRUPTED
;
4255 if (XFS_FORCED_SHUTDOWN(mp
))
4258 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4260 XFS_STATS_INC(mp
, xs_blk_mapw
);
4262 if (!tp
|| tp
->t_firstblock
== NULLFSBLOCK
) {
4263 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
)
4264 bma
.minleft
= be16_to_cpu(ifp
->if_broot
->bb_level
) + 1;
4271 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
4272 error
= xfs_iread_extents(tp
, ip
, whichfork
);
4281 if (!xfs_iext_lookup_extent(ip
, ifp
, bno
, &bma
.icur
, &bma
.got
))
4283 if (!xfs_iext_peek_prev_extent(ifp
, &bma
.icur
, &bma
.prev
))
4284 bma
.prev
.br_startoff
= NULLFILEOFF
;
4289 ASSERT(!tp
|| tp
->t_dfops
);
4291 while (bno
< end
&& n
< *nmap
) {
4292 bool need_alloc
= false, wasdelay
= false;
4294 /* in hole or beyond EOF? */
4295 if (eof
|| bma
.got
.br_startoff
> bno
) {
4297 * CoW fork conversions should /never/ hit EOF or
4298 * holes. There should always be something for us
4301 ASSERT(!((flags
& XFS_BMAPI_CONVERT
) &&
4302 (flags
& XFS_BMAPI_COWFORK
)));
4304 if (flags
& XFS_BMAPI_DELALLOC
) {
4306 * For the COW fork we can reasonably get a
4307 * request for converting an extent that races
4308 * with other threads already having converted
4309 * part of it, as there converting COW to
4310 * regular blocks is not protected using the
4313 ASSERT(flags
& XFS_BMAPI_COWFORK
);
4314 if (!(flags
& XFS_BMAPI_COWFORK
)) {
4319 if (eof
|| bno
>= end
)
4324 } else if (isnullstartblock(bma
.got
.br_startblock
)) {
4329 * First, deal with the hole before the allocated space
4330 * that we found, if any.
4332 if ((need_alloc
|| wasdelay
) &&
4333 !(flags
& XFS_BMAPI_CONVERT_ONLY
)) {
4335 bma
.conv
= !!(flags
& XFS_BMAPI_CONVERT
);
4336 bma
.wasdel
= wasdelay
;
4341 * There's a 32/64 bit type mismatch between the
4342 * allocation length request (which can be 64 bits in
4343 * length) and the bma length request, which is
4344 * xfs_extlen_t and therefore 32 bits. Hence we have to
4345 * check for 32-bit overflows and handle them here.
4347 if (len
> (xfs_filblks_t
)MAXEXTLEN
)
4348 bma
.length
= MAXEXTLEN
;
4353 ASSERT(bma
.length
> 0);
4354 error
= xfs_bmapi_allocate(&bma
);
4357 if (bma
.blkno
== NULLFSBLOCK
)
4361 * If this is a CoW allocation, record the data in
4362 * the refcount btree for orphan recovery.
4364 if (whichfork
== XFS_COW_FORK
) {
4365 error
= xfs_refcount_alloc_cow_extent(mp
,
4366 tp
->t_dfops
, bma
.blkno
,
4373 /* Deal with the allocated space we found. */
4374 xfs_bmapi_trim_map(mval
, &bma
.got
, &bno
, len
, obno
,
4377 /* Execute unwritten extent conversion if necessary */
4378 error
= xfs_bmapi_convert_unwritten(&bma
, mval
, len
, flags
);
4379 if (error
== -EAGAIN
)
4384 /* update the extent map to return */
4385 xfs_bmapi_update_map(&mval
, &bno
, &len
, obno
, end
, &n
, flags
);
4388 * If we're done, stop now. Stop when we've allocated
4389 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4390 * the transaction may get too big.
4392 if (bno
>= end
|| n
>= *nmap
|| bma
.nallocs
>= *nmap
)
4395 /* Else go on to the next record. */
4397 if (!xfs_iext_next_extent(ifp
, &bma
.icur
, &bma
.got
))
4403 * Transform from btree to extents, give it cur.
4405 if (xfs_bmap_wants_extents(ip
, whichfork
)) {
4406 int tmp_logflags
= 0;
4409 error
= xfs_bmap_btree_to_extents(tp
, ip
, bma
.cur
,
4410 &tmp_logflags
, whichfork
);
4411 bma
.logflags
|= tmp_logflags
;
4416 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
||
4417 XFS_IFORK_NEXTENTS(ip
, whichfork
) >
4418 XFS_IFORK_MAXEXT(ip
, whichfork
));
4422 * Log everything. Do this after conversion, there's no point in
4423 * logging the extent records if we've converted to btree format.
4425 if ((bma
.logflags
& xfs_ilog_fext(whichfork
)) &&
4426 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
4427 bma
.logflags
&= ~xfs_ilog_fext(whichfork
);
4428 else if ((bma
.logflags
& xfs_ilog_fbroot(whichfork
)) &&
4429 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)
4430 bma
.logflags
&= ~xfs_ilog_fbroot(whichfork
);
4432 * Log whatever the flags say, even if error. Otherwise we might miss
4433 * detecting a case where the data is changed, there's an error,
4434 * and it's not logged so we don't shutdown when we should.
4437 xfs_trans_log_inode(tp
, ip
, bma
.logflags
);
4440 xfs_btree_del_cursor(bma
.cur
, error
);
4443 xfs_bmap_validate_ret(orig_bno
, orig_len
, orig_flags
, orig_mval
,
4450 struct xfs_trans
*tp
,
4451 struct xfs_inode
*ip
,
4454 xfs_fsblock_t startblock
,
4457 struct xfs_mount
*mp
= ip
->i_mount
;
4458 struct xfs_ifork
*ifp
;
4459 struct xfs_btree_cur
*cur
= NULL
;
4460 struct xfs_bmbt_irec got
;
4461 struct xfs_iext_cursor icur
;
4462 int whichfork
= xfs_bmapi_whichfork(flags
);
4463 int logflags
= 0, error
;
4465 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4467 ASSERT(len
<= (xfs_filblks_t
)MAXEXTLEN
);
4468 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
4469 ASSERT(!(flags
& ~(XFS_BMAPI_ATTRFORK
| XFS_BMAPI_PREALLOC
|
4470 XFS_BMAPI_NORMAP
)));
4471 ASSERT((flags
& (XFS_BMAPI_ATTRFORK
| XFS_BMAPI_PREALLOC
)) !=
4472 (XFS_BMAPI_ATTRFORK
| XFS_BMAPI_PREALLOC
));
4474 if (unlikely(XFS_TEST_ERROR(
4475 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
4476 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
4477 mp
, XFS_ERRTAG_BMAPIFORMAT
))) {
4478 XFS_ERROR_REPORT("xfs_bmapi_remap", XFS_ERRLEVEL_LOW
, mp
);
4479 return -EFSCORRUPTED
;
4482 if (XFS_FORCED_SHUTDOWN(mp
))
4485 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
4486 error
= xfs_iread_extents(tp
, ip
, whichfork
);
4491 if (xfs_iext_lookup_extent(ip
, ifp
, bno
, &icur
, &got
)) {
4492 /* make sure we only reflink into a hole. */
4493 ASSERT(got
.br_startoff
> bno
);
4494 ASSERT(got
.br_startoff
- bno
>= len
);
4497 ip
->i_d
.di_nblocks
+= len
;
4498 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
4500 if (ifp
->if_flags
& XFS_IFBROOT
) {
4501 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
4502 cur
->bc_private
.b
.flags
= 0;
4505 got
.br_startoff
= bno
;
4506 got
.br_startblock
= startblock
;
4507 got
.br_blockcount
= len
;
4508 if (flags
& XFS_BMAPI_PREALLOC
)
4509 got
.br_state
= XFS_EXT_UNWRITTEN
;
4511 got
.br_state
= XFS_EXT_NORM
;
4513 error
= xfs_bmap_add_extent_hole_real(tp
, ip
, whichfork
, &icur
,
4514 &cur
, &got
, &logflags
, flags
);
4518 if (xfs_bmap_wants_extents(ip
, whichfork
)) {
4519 int tmp_logflags
= 0;
4521 error
= xfs_bmap_btree_to_extents(tp
, ip
, cur
,
4522 &tmp_logflags
, whichfork
);
4523 logflags
|= tmp_logflags
;
4527 if (ip
->i_d
.di_format
!= XFS_DINODE_FMT_EXTENTS
)
4528 logflags
&= ~XFS_ILOG_DEXT
;
4529 else if (ip
->i_d
.di_format
!= XFS_DINODE_FMT_BTREE
)
4530 logflags
&= ~XFS_ILOG_DBROOT
;
4533 xfs_trans_log_inode(tp
, ip
, logflags
);
4535 xfs_btree_del_cursor(cur
, error
);
4540 * When a delalloc extent is split (e.g., due to a hole punch), the original
4541 * indlen reservation must be shared across the two new extents that are left
4544 * Given the original reservation and the worst case indlen for the two new
4545 * extents (as calculated by xfs_bmap_worst_indlen()), split the original
4546 * reservation fairly across the two new extents. If necessary, steal available
4547 * blocks from a deleted extent to make up a reservation deficiency (e.g., if
4548 * ores == 1). The number of stolen blocks is returned. The availability and
4549 * subsequent accounting of stolen blocks is the responsibility of the caller.
4551 static xfs_filblks_t
4552 xfs_bmap_split_indlen(
4553 xfs_filblks_t ores
, /* original res. */
4554 xfs_filblks_t
*indlen1
, /* ext1 worst indlen */
4555 xfs_filblks_t
*indlen2
, /* ext2 worst indlen */
4556 xfs_filblks_t avail
) /* stealable blocks */
4558 xfs_filblks_t len1
= *indlen1
;
4559 xfs_filblks_t len2
= *indlen2
;
4560 xfs_filblks_t nres
= len1
+ len2
; /* new total res. */
4561 xfs_filblks_t stolen
= 0;
4562 xfs_filblks_t resfactor
;
4565 * Steal as many blocks as we can to try and satisfy the worst case
4566 * indlen for both new extents.
4568 if (ores
< nres
&& avail
)
4569 stolen
= XFS_FILBLKS_MIN(nres
- ores
, avail
);
4572 /* nothing else to do if we've satisfied the new reservation */
4577 * We can't meet the total required reservation for the two extents.
4578 * Calculate the percent of the overall shortage between both extents
4579 * and apply this percentage to each of the requested indlen values.
4580 * This distributes the shortage fairly and reduces the chances that one
4581 * of the two extents is left with nothing when extents are repeatedly
4584 resfactor
= (ores
* 100);
4585 do_div(resfactor
, nres
);
4590 ASSERT(len1
+ len2
<= ores
);
4591 ASSERT(len1
< *indlen1
&& len2
< *indlen2
);
4594 * Hand out the remainder to each extent. If one of the two reservations
4595 * is zero, we want to make sure that one gets a block first. The loop
4596 * below starts with len1, so hand len2 a block right off the bat if it
4599 ores
-= (len1
+ len2
);
4600 ASSERT((*indlen1
- len1
) + (*indlen2
- len2
) >= ores
);
4601 if (ores
&& !len2
&& *indlen2
) {
4606 if (len1
< *indlen1
) {
4612 if (len2
< *indlen2
) {
4625 xfs_bmap_del_extent_delay(
4626 struct xfs_inode
*ip
,
4628 struct xfs_iext_cursor
*icur
,
4629 struct xfs_bmbt_irec
*got
,
4630 struct xfs_bmbt_irec
*del
)
4632 struct xfs_mount
*mp
= ip
->i_mount
;
4633 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4634 struct xfs_bmbt_irec
new;
4635 int64_t da_old
, da_new
, da_diff
= 0;
4636 xfs_fileoff_t del_endoff
, got_endoff
;
4637 xfs_filblks_t got_indlen
, new_indlen
, stolen
;
4638 int state
= xfs_bmap_fork_to_state(whichfork
);
4642 XFS_STATS_INC(mp
, xs_del_exlist
);
4644 isrt
= (whichfork
== XFS_DATA_FORK
) && XFS_IS_REALTIME_INODE(ip
);
4645 del_endoff
= del
->br_startoff
+ del
->br_blockcount
;
4646 got_endoff
= got
->br_startoff
+ got
->br_blockcount
;
4647 da_old
= startblockval(got
->br_startblock
);
4650 ASSERT(del
->br_blockcount
> 0);
4651 ASSERT(got
->br_startoff
<= del
->br_startoff
);
4652 ASSERT(got_endoff
>= del_endoff
);
4655 uint64_t rtexts
= XFS_FSB_TO_B(mp
, del
->br_blockcount
);
4657 do_div(rtexts
, mp
->m_sb
.sb_rextsize
);
4658 xfs_mod_frextents(mp
, rtexts
);
4662 * Update the inode delalloc counter now and wait to update the
4663 * sb counters as we might have to borrow some blocks for the
4664 * indirect block accounting.
4666 error
= xfs_trans_reserve_quota_nblks(NULL
, ip
,
4667 -((long)del
->br_blockcount
), 0,
4668 isrt
? XFS_QMOPT_RES_RTBLKS
: XFS_QMOPT_RES_REGBLKS
);
4671 ip
->i_delayed_blks
-= del
->br_blockcount
;
4673 if (got
->br_startoff
== del
->br_startoff
)
4674 state
|= BMAP_LEFT_FILLING
;
4675 if (got_endoff
== del_endoff
)
4676 state
|= BMAP_RIGHT_FILLING
;
4678 switch (state
& (BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
)) {
4679 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
4681 * Matches the whole extent. Delete the entry.
4683 xfs_iext_remove(ip
, icur
, state
);
4684 xfs_iext_prev(ifp
, icur
);
4686 case BMAP_LEFT_FILLING
:
4688 * Deleting the first part of the extent.
4690 got
->br_startoff
= del_endoff
;
4691 got
->br_blockcount
-= del
->br_blockcount
;
4692 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
,
4693 got
->br_blockcount
), da_old
);
4694 got
->br_startblock
= nullstartblock((int)da_new
);
4695 xfs_iext_update_extent(ip
, state
, icur
, got
);
4697 case BMAP_RIGHT_FILLING
:
4699 * Deleting the last part of the extent.
4701 got
->br_blockcount
= got
->br_blockcount
- del
->br_blockcount
;
4702 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
,
4703 got
->br_blockcount
), da_old
);
4704 got
->br_startblock
= nullstartblock((int)da_new
);
4705 xfs_iext_update_extent(ip
, state
, icur
, got
);
4709 * Deleting the middle of the extent.
4711 * Distribute the original indlen reservation across the two new
4712 * extents. Steal blocks from the deleted extent if necessary.
4713 * Stealing blocks simply fudges the fdblocks accounting below.
4714 * Warn if either of the new indlen reservations is zero as this
4715 * can lead to delalloc problems.
4717 got
->br_blockcount
= del
->br_startoff
- got
->br_startoff
;
4718 got_indlen
= xfs_bmap_worst_indlen(ip
, got
->br_blockcount
);
4720 new.br_blockcount
= got_endoff
- del_endoff
;
4721 new_indlen
= xfs_bmap_worst_indlen(ip
, new.br_blockcount
);
4723 WARN_ON_ONCE(!got_indlen
|| !new_indlen
);
4724 stolen
= xfs_bmap_split_indlen(da_old
, &got_indlen
, &new_indlen
,
4725 del
->br_blockcount
);
4727 got
->br_startblock
= nullstartblock((int)got_indlen
);
4729 new.br_startoff
= del_endoff
;
4730 new.br_state
= got
->br_state
;
4731 new.br_startblock
= nullstartblock((int)new_indlen
);
4733 xfs_iext_update_extent(ip
, state
, icur
, got
);
4734 xfs_iext_next(ifp
, icur
);
4735 xfs_iext_insert(ip
, icur
, &new, state
);
4737 da_new
= got_indlen
+ new_indlen
- stolen
;
4738 del
->br_blockcount
-= stolen
;
4742 ASSERT(da_old
>= da_new
);
4743 da_diff
= da_old
- da_new
;
4745 da_diff
+= del
->br_blockcount
;
4747 xfs_mod_fdblocks(mp
, da_diff
, false);
4752 xfs_bmap_del_extent_cow(
4753 struct xfs_inode
*ip
,
4754 struct xfs_iext_cursor
*icur
,
4755 struct xfs_bmbt_irec
*got
,
4756 struct xfs_bmbt_irec
*del
)
4758 struct xfs_mount
*mp
= ip
->i_mount
;
4759 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, XFS_COW_FORK
);
4760 struct xfs_bmbt_irec
new;
4761 xfs_fileoff_t del_endoff
, got_endoff
;
4762 int state
= BMAP_COWFORK
;
4764 XFS_STATS_INC(mp
, xs_del_exlist
);
4766 del_endoff
= del
->br_startoff
+ del
->br_blockcount
;
4767 got_endoff
= got
->br_startoff
+ got
->br_blockcount
;
4769 ASSERT(del
->br_blockcount
> 0);
4770 ASSERT(got
->br_startoff
<= del
->br_startoff
);
4771 ASSERT(got_endoff
>= del_endoff
);
4772 ASSERT(!isnullstartblock(got
->br_startblock
));
4774 if (got
->br_startoff
== del
->br_startoff
)
4775 state
|= BMAP_LEFT_FILLING
;
4776 if (got_endoff
== del_endoff
)
4777 state
|= BMAP_RIGHT_FILLING
;
4779 switch (state
& (BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
)) {
4780 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
4782 * Matches the whole extent. Delete the entry.
4784 xfs_iext_remove(ip
, icur
, state
);
4785 xfs_iext_prev(ifp
, icur
);
4787 case BMAP_LEFT_FILLING
:
4789 * Deleting the first part of the extent.
4791 got
->br_startoff
= del_endoff
;
4792 got
->br_blockcount
-= del
->br_blockcount
;
4793 got
->br_startblock
= del
->br_startblock
+ del
->br_blockcount
;
4794 xfs_iext_update_extent(ip
, state
, icur
, got
);
4796 case BMAP_RIGHT_FILLING
:
4798 * Deleting the last part of the extent.
4800 got
->br_blockcount
-= del
->br_blockcount
;
4801 xfs_iext_update_extent(ip
, state
, icur
, got
);
4805 * Deleting the middle of the extent.
4807 got
->br_blockcount
= del
->br_startoff
- got
->br_startoff
;
4809 new.br_startoff
= del_endoff
;
4810 new.br_blockcount
= got_endoff
- del_endoff
;
4811 new.br_state
= got
->br_state
;
4812 new.br_startblock
= del
->br_startblock
+ del
->br_blockcount
;
4814 xfs_iext_update_extent(ip
, state
, icur
, got
);
4815 xfs_iext_next(ifp
, icur
);
4816 xfs_iext_insert(ip
, icur
, &new, state
);
4819 ip
->i_delayed_blks
-= del
->br_blockcount
;
4823 * Called by xfs_bmapi to update file extent records and the btree
4824 * after removing space.
4826 STATIC
int /* error */
4827 xfs_bmap_del_extent_real(
4828 xfs_inode_t
*ip
, /* incore inode pointer */
4829 xfs_trans_t
*tp
, /* current transaction pointer */
4830 struct xfs_iext_cursor
*icur
,
4831 xfs_btree_cur_t
*cur
, /* if null, not a btree */
4832 xfs_bmbt_irec_t
*del
, /* data to remove from extents */
4833 int *logflagsp
, /* inode logging flags */
4834 int whichfork
, /* data or attr fork */
4835 int bflags
) /* bmapi flags */
4837 xfs_fsblock_t del_endblock
=0; /* first block past del */
4838 xfs_fileoff_t del_endoff
; /* first offset past del */
4839 int do_fx
; /* free extent at end of routine */
4840 int error
; /* error return value */
4841 int flags
= 0;/* inode logging flags */
4842 struct xfs_bmbt_irec got
; /* current extent entry */
4843 xfs_fileoff_t got_endoff
; /* first offset past got */
4844 int i
; /* temp state */
4845 xfs_ifork_t
*ifp
; /* inode fork pointer */
4846 xfs_mount_t
*mp
; /* mount structure */
4847 xfs_filblks_t nblks
; /* quota/sb block count */
4848 xfs_bmbt_irec_t
new; /* new record to be inserted */
4850 uint qfield
; /* quota field to update */
4851 int state
= xfs_bmap_fork_to_state(whichfork
);
4852 struct xfs_bmbt_irec old
;
4853 struct xfs_defer_ops
*dfops
= tp
? tp
->t_dfops
: NULL
;
4856 XFS_STATS_INC(mp
, xs_del_exlist
);
4858 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4859 ASSERT(del
->br_blockcount
> 0);
4860 xfs_iext_get_extent(ifp
, icur
, &got
);
4861 ASSERT(got
.br_startoff
<= del
->br_startoff
);
4862 del_endoff
= del
->br_startoff
+ del
->br_blockcount
;
4863 got_endoff
= got
.br_startoff
+ got
.br_blockcount
;
4864 ASSERT(got_endoff
>= del_endoff
);
4865 ASSERT(!isnullstartblock(got
.br_startblock
));
4870 * If it's the case where the directory code is running with no block
4871 * reservation, and the deleted block is in the middle of its extent,
4872 * and the resulting insert of an extent would cause transformation to
4873 * btree format, then reject it. The calling code will then swap blocks
4874 * around instead. We have to do this now, rather than waiting for the
4875 * conversion to btree format, since the transaction will be dirty then.
4877 if (tp
->t_blk_res
== 0 &&
4878 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
&&
4879 XFS_IFORK_NEXTENTS(ip
, whichfork
) >=
4880 XFS_IFORK_MAXEXT(ip
, whichfork
) &&
4881 del
->br_startoff
> got
.br_startoff
&& del_endoff
< got_endoff
)
4884 flags
= XFS_ILOG_CORE
;
4885 if (whichfork
== XFS_DATA_FORK
&& XFS_IS_REALTIME_INODE(ip
)) {
4890 bno
= div_u64_rem(del
->br_startblock
, mp
->m_sb
.sb_rextsize
,
4893 len
= div_u64_rem(del
->br_blockcount
, mp
->m_sb
.sb_rextsize
,
4897 error
= xfs_rtfree_extent(tp
, bno
, (xfs_extlen_t
)len
);
4901 nblks
= len
* mp
->m_sb
.sb_rextsize
;
4902 qfield
= XFS_TRANS_DQ_RTBCOUNT
;
4905 nblks
= del
->br_blockcount
;
4906 qfield
= XFS_TRANS_DQ_BCOUNT
;
4909 del_endblock
= del
->br_startblock
+ del
->br_blockcount
;
4911 error
= xfs_bmbt_lookup_eq(cur
, &got
, &i
);
4914 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
4917 if (got
.br_startoff
== del
->br_startoff
)
4918 state
|= BMAP_LEFT_FILLING
;
4919 if (got_endoff
== del_endoff
)
4920 state
|= BMAP_RIGHT_FILLING
;
4922 switch (state
& (BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
)) {
4923 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
4925 * Matches the whole extent. Delete the entry.
4927 xfs_iext_remove(ip
, icur
, state
);
4928 xfs_iext_prev(ifp
, icur
);
4929 XFS_IFORK_NEXT_SET(ip
, whichfork
,
4930 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
4931 flags
|= XFS_ILOG_CORE
;
4933 flags
|= xfs_ilog_fext(whichfork
);
4936 if ((error
= xfs_btree_delete(cur
, &i
)))
4938 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
4940 case BMAP_LEFT_FILLING
:
4942 * Deleting the first part of the extent.
4944 got
.br_startoff
= del_endoff
;
4945 got
.br_startblock
= del_endblock
;
4946 got
.br_blockcount
-= del
->br_blockcount
;
4947 xfs_iext_update_extent(ip
, state
, icur
, &got
);
4949 flags
|= xfs_ilog_fext(whichfork
);
4952 error
= xfs_bmbt_update(cur
, &got
);
4956 case BMAP_RIGHT_FILLING
:
4958 * Deleting the last part of the extent.
4960 got
.br_blockcount
-= del
->br_blockcount
;
4961 xfs_iext_update_extent(ip
, state
, icur
, &got
);
4963 flags
|= xfs_ilog_fext(whichfork
);
4966 error
= xfs_bmbt_update(cur
, &got
);
4972 * Deleting the middle of the extent.
4976 got
.br_blockcount
= del
->br_startoff
- got
.br_startoff
;
4977 xfs_iext_update_extent(ip
, state
, icur
, &got
);
4979 new.br_startoff
= del_endoff
;
4980 new.br_blockcount
= got_endoff
- del_endoff
;
4981 new.br_state
= got
.br_state
;
4982 new.br_startblock
= del_endblock
;
4984 flags
|= XFS_ILOG_CORE
;
4986 error
= xfs_bmbt_update(cur
, &got
);
4989 error
= xfs_btree_increment(cur
, 0, &i
);
4992 cur
->bc_rec
.b
= new;
4993 error
= xfs_btree_insert(cur
, &i
);
4994 if (error
&& error
!= -ENOSPC
)
4997 * If get no-space back from btree insert, it tried a
4998 * split, and we have a zero block reservation. Fix up
4999 * our state and return the error.
5001 if (error
== -ENOSPC
) {
5003 * Reset the cursor, don't trust it after any
5006 error
= xfs_bmbt_lookup_eq(cur
, &got
, &i
);
5009 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
5011 * Update the btree record back
5012 * to the original value.
5014 error
= xfs_bmbt_update(cur
, &old
);
5018 * Reset the extent record back
5019 * to the original value.
5021 xfs_iext_update_extent(ip
, state
, icur
, &old
);
5026 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
5028 flags
|= xfs_ilog_fext(whichfork
);
5029 XFS_IFORK_NEXT_SET(ip
, whichfork
,
5030 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
5031 xfs_iext_next(ifp
, icur
);
5032 xfs_iext_insert(ip
, icur
, &new, state
);
5036 /* remove reverse mapping */
5037 error
= xfs_rmap_unmap_extent(mp
, dfops
, ip
, whichfork
, del
);
5042 * If we need to, add to list of extents to delete.
5044 if (do_fx
&& !(bflags
& XFS_BMAPI_REMAP
)) {
5045 if (xfs_is_reflink_inode(ip
) && whichfork
== XFS_DATA_FORK
) {
5046 error
= xfs_refcount_decrease_extent(mp
, dfops
, del
);
5050 __xfs_bmap_add_free(mp
, dfops
, del
->br_startblock
,
5051 del
->br_blockcount
, NULL
,
5052 (bflags
& XFS_BMAPI_NODISCARD
) ||
5053 del
->br_state
== XFS_EXT_UNWRITTEN
);
5058 * Adjust inode # blocks in the file.
5061 ip
->i_d
.di_nblocks
-= nblks
;
5063 * Adjust quota data.
5065 if (qfield
&& !(bflags
& XFS_BMAPI_REMAP
))
5066 xfs_trans_mod_dquot_byino(tp
, ip
, qfield
, (long)-nblks
);
5074 * Unmap (remove) blocks from a file.
5075 * If nexts is nonzero then the number of extents to remove is limited to
5076 * that value. If not all extents in the block range can be removed then
5081 struct xfs_trans
*tp
, /* transaction pointer */
5082 struct xfs_inode
*ip
, /* incore inode */
5083 xfs_fileoff_t start
, /* first file offset deleted */
5084 xfs_filblks_t
*rlen
, /* i/o: amount remaining */
5085 int flags
, /* misc flags */
5086 xfs_extnum_t nexts
) /* number of extents max */
5088 struct xfs_btree_cur
*cur
; /* bmap btree cursor */
5089 struct xfs_bmbt_irec del
; /* extent being deleted */
5090 int error
; /* error return value */
5091 xfs_extnum_t extno
; /* extent number in list */
5092 struct xfs_bmbt_irec got
; /* current extent record */
5093 xfs_ifork_t
*ifp
; /* inode fork pointer */
5094 int isrt
; /* freeing in rt area */
5095 int logflags
; /* transaction logging flags */
5096 xfs_extlen_t mod
; /* rt extent offset */
5097 struct xfs_mount
*mp
; /* mount structure */
5098 int tmp_logflags
; /* partial logging flags */
5099 int wasdel
; /* was a delayed alloc extent */
5100 int whichfork
; /* data or attribute fork */
5102 xfs_filblks_t len
= *rlen
; /* length to unmap in file */
5103 xfs_fileoff_t max_len
;
5104 xfs_agnumber_t prev_agno
= NULLAGNUMBER
, agno
;
5106 struct xfs_iext_cursor icur
;
5109 trace_xfs_bunmap(ip
, start
, len
, flags
, _RET_IP_
);
5111 whichfork
= xfs_bmapi_whichfork(flags
);
5112 ASSERT(whichfork
!= XFS_COW_FORK
);
5113 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5115 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
5116 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)) {
5117 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW
,
5119 return -EFSCORRUPTED
;
5122 if (XFS_FORCED_SHUTDOWN(mp
))
5125 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
5130 * Guesstimate how many blocks we can unmap without running the risk of
5131 * blowing out the transaction with a mix of EFIs and reflink
5134 if (tp
&& xfs_is_reflink_inode(ip
) && whichfork
== XFS_DATA_FORK
)
5135 max_len
= min(len
, xfs_refcount_max_unmap(tp
->t_log_res
));
5139 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
5140 (error
= xfs_iread_extents(tp
, ip
, whichfork
)))
5142 if (xfs_iext_count(ifp
) == 0) {
5146 XFS_STATS_INC(mp
, xs_blk_unmap
);
5147 isrt
= (whichfork
== XFS_DATA_FORK
) && XFS_IS_REALTIME_INODE(ip
);
5150 if (!xfs_iext_lookup_extent_before(ip
, ifp
, &end
, &icur
, &got
)) {
5157 if (ifp
->if_flags
& XFS_IFBROOT
) {
5158 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
);
5159 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
5160 cur
->bc_private
.b
.flags
= 0;
5166 * Synchronize by locking the bitmap inode.
5168 xfs_ilock(mp
->m_rbmip
, XFS_ILOCK_EXCL
|XFS_ILOCK_RTBITMAP
);
5169 xfs_trans_ijoin(tp
, mp
->m_rbmip
, XFS_ILOCK_EXCL
);
5170 xfs_ilock(mp
->m_rsumip
, XFS_ILOCK_EXCL
|XFS_ILOCK_RTSUM
);
5171 xfs_trans_ijoin(tp
, mp
->m_rsumip
, XFS_ILOCK_EXCL
);
5175 while (end
!= (xfs_fileoff_t
)-1 && end
>= start
&&
5176 (nexts
== 0 || extno
< nexts
) && max_len
> 0) {
5178 * Is the found extent after a hole in which end lives?
5179 * Just back up to the previous extent, if so.
5181 if (got
.br_startoff
> end
&&
5182 !xfs_iext_prev_extent(ifp
, &icur
, &got
)) {
5187 * Is the last block of this extent before the range
5188 * we're supposed to delete? If so, we're done.
5190 end
= XFS_FILEOFF_MIN(end
,
5191 got
.br_startoff
+ got
.br_blockcount
- 1);
5195 * Then deal with the (possibly delayed) allocated space
5199 wasdel
= isnullstartblock(del
.br_startblock
);
5202 * Make sure we don't touch multiple AGF headers out of order
5203 * in a single transaction, as that could cause AB-BA deadlocks.
5206 agno
= XFS_FSB_TO_AGNO(mp
, del
.br_startblock
);
5207 if (prev_agno
!= NULLAGNUMBER
&& prev_agno
> agno
)
5211 if (got
.br_startoff
< start
) {
5212 del
.br_startoff
= start
;
5213 del
.br_blockcount
-= start
- got
.br_startoff
;
5215 del
.br_startblock
+= start
- got
.br_startoff
;
5217 if (del
.br_startoff
+ del
.br_blockcount
> end
+ 1)
5218 del
.br_blockcount
= end
+ 1 - del
.br_startoff
;
5220 /* How much can we safely unmap? */
5221 if (max_len
< del
.br_blockcount
) {
5222 del
.br_startoff
+= del
.br_blockcount
- max_len
;
5224 del
.br_startblock
+= del
.br_blockcount
- max_len
;
5225 del
.br_blockcount
= max_len
;
5231 sum
= del
.br_startblock
+ del
.br_blockcount
;
5232 div_u64_rem(sum
, mp
->m_sb
.sb_rextsize
, &mod
);
5235 * Realtime extent not lined up at the end.
5236 * The extent could have been split into written
5237 * and unwritten pieces, or we could just be
5238 * unmapping part of it. But we can't really
5239 * get rid of part of a realtime extent.
5241 if (del
.br_state
== XFS_EXT_UNWRITTEN
||
5242 !xfs_sb_version_hasextflgbit(&mp
->m_sb
)) {
5244 * This piece is unwritten, or we're not
5245 * using unwritten extents. Skip over it.
5248 end
-= mod
> del
.br_blockcount
?
5249 del
.br_blockcount
: mod
;
5250 if (end
< got
.br_startoff
&&
5251 !xfs_iext_prev_extent(ifp
, &icur
, &got
)) {
5258 * It's written, turn it unwritten.
5259 * This is better than zeroing it.
5261 ASSERT(del
.br_state
== XFS_EXT_NORM
);
5262 ASSERT(tp
->t_blk_res
> 0);
5264 * If this spans a realtime extent boundary,
5265 * chop it back to the start of the one we end at.
5267 if (del
.br_blockcount
> mod
) {
5268 del
.br_startoff
+= del
.br_blockcount
- mod
;
5269 del
.br_startblock
+= del
.br_blockcount
- mod
;
5270 del
.br_blockcount
= mod
;
5272 del
.br_state
= XFS_EXT_UNWRITTEN
;
5273 error
= xfs_bmap_add_extent_unwritten_real(tp
, ip
,
5274 whichfork
, &icur
, &cur
, &del
,
5280 div_u64_rem(del
.br_startblock
, mp
->m_sb
.sb_rextsize
, &mod
);
5283 * Realtime extent is lined up at the end but not
5284 * at the front. We'll get rid of full extents if
5287 mod
= mp
->m_sb
.sb_rextsize
- mod
;
5288 if (del
.br_blockcount
> mod
) {
5289 del
.br_blockcount
-= mod
;
5290 del
.br_startoff
+= mod
;
5291 del
.br_startblock
+= mod
;
5292 } else if ((del
.br_startoff
== start
&&
5293 (del
.br_state
== XFS_EXT_UNWRITTEN
||
5294 tp
->t_blk_res
== 0)) ||
5295 !xfs_sb_version_hasextflgbit(&mp
->m_sb
)) {
5297 * Can't make it unwritten. There isn't
5298 * a full extent here so just skip it.
5300 ASSERT(end
>= del
.br_blockcount
);
5301 end
-= del
.br_blockcount
;
5302 if (got
.br_startoff
> end
&&
5303 !xfs_iext_prev_extent(ifp
, &icur
, &got
)) {
5308 } else if (del
.br_state
== XFS_EXT_UNWRITTEN
) {
5309 struct xfs_bmbt_irec prev
;
5312 * This one is already unwritten.
5313 * It must have a written left neighbor.
5314 * Unwrite the killed part of that one and
5317 if (!xfs_iext_prev_extent(ifp
, &icur
, &prev
))
5319 ASSERT(prev
.br_state
== XFS_EXT_NORM
);
5320 ASSERT(!isnullstartblock(prev
.br_startblock
));
5321 ASSERT(del
.br_startblock
==
5322 prev
.br_startblock
+ prev
.br_blockcount
);
5323 if (prev
.br_startoff
< start
) {
5324 mod
= start
- prev
.br_startoff
;
5325 prev
.br_blockcount
-= mod
;
5326 prev
.br_startblock
+= mod
;
5327 prev
.br_startoff
= start
;
5329 prev
.br_state
= XFS_EXT_UNWRITTEN
;
5330 error
= xfs_bmap_add_extent_unwritten_real(tp
,
5331 ip
, whichfork
, &icur
, &cur
,
5337 ASSERT(del
.br_state
== XFS_EXT_NORM
);
5338 del
.br_state
= XFS_EXT_UNWRITTEN
;
5339 error
= xfs_bmap_add_extent_unwritten_real(tp
,
5340 ip
, whichfork
, &icur
, &cur
,
5350 error
= xfs_bmap_del_extent_delay(ip
, whichfork
, &icur
,
5353 error
= xfs_bmap_del_extent_real(ip
, tp
, &icur
, cur
,
5354 &del
, &tmp_logflags
, whichfork
,
5356 logflags
|= tmp_logflags
;
5362 max_len
-= del
.br_blockcount
;
5363 end
= del
.br_startoff
- 1;
5366 * If not done go on to the next (previous) record.
5368 if (end
!= (xfs_fileoff_t
)-1 && end
>= start
) {
5369 if (!xfs_iext_get_extent(ifp
, &icur
, &got
) ||
5370 (got
.br_startoff
> end
&&
5371 !xfs_iext_prev_extent(ifp
, &icur
, &got
))) {
5378 if (done
|| end
== (xfs_fileoff_t
)-1 || end
< start
)
5381 *rlen
= end
- start
+ 1;
5384 * Convert to a btree if necessary.
5386 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
5387 ASSERT(cur
== NULL
);
5388 error
= xfs_bmap_extents_to_btree(tp
, ip
, &cur
, 0,
5389 &tmp_logflags
, whichfork
);
5390 logflags
|= tmp_logflags
;
5395 * transform from btree to extents, give it cur
5397 else if (xfs_bmap_wants_extents(ip
, whichfork
)) {
5398 ASSERT(cur
!= NULL
);
5399 error
= xfs_bmap_btree_to_extents(tp
, ip
, cur
, &tmp_logflags
,
5401 logflags
|= tmp_logflags
;
5406 * transform from extents to local?
5411 * Log everything. Do this after conversion, there's no point in
5412 * logging the extent records if we've converted to btree format.
5414 if ((logflags
& xfs_ilog_fext(whichfork
)) &&
5415 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
5416 logflags
&= ~xfs_ilog_fext(whichfork
);
5417 else if ((logflags
& xfs_ilog_fbroot(whichfork
)) &&
5418 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)
5419 logflags
&= ~xfs_ilog_fbroot(whichfork
);
5421 * Log inode even in the error case, if the transaction
5422 * is dirty we'll need to shut down the filesystem.
5425 xfs_trans_log_inode(tp
, ip
, logflags
);
5428 cur
->bc_private
.b
.allocated
= 0;
5429 xfs_btree_del_cursor(cur
, error
);
5434 /* Unmap a range of a file. */
5438 struct xfs_inode
*ip
,
5447 error
= __xfs_bunmapi(tp
, ip
, bno
, &len
, flags
, nexts
);
5453 * Determine whether an extent shift can be accomplished by a merge with the
5454 * extent that precedes the target hole of the shift.
5458 struct xfs_bmbt_irec
*left
, /* preceding extent */
5459 struct xfs_bmbt_irec
*got
, /* current extent to shift */
5460 xfs_fileoff_t shift
) /* shift fsb */
5462 xfs_fileoff_t startoff
;
5464 startoff
= got
->br_startoff
- shift
;
5467 * The extent, once shifted, must be adjacent in-file and on-disk with
5468 * the preceding extent.
5470 if ((left
->br_startoff
+ left
->br_blockcount
!= startoff
) ||
5471 (left
->br_startblock
+ left
->br_blockcount
!= got
->br_startblock
) ||
5472 (left
->br_state
!= got
->br_state
) ||
5473 (left
->br_blockcount
+ got
->br_blockcount
> MAXEXTLEN
))
5480 * A bmap extent shift adjusts the file offset of an extent to fill a preceding
5481 * hole in the file. If an extent shift would result in the extent being fully
5482 * adjacent to the extent that currently precedes the hole, we can merge with
5483 * the preceding extent rather than do the shift.
5485 * This function assumes the caller has verified a shift-by-merge is possible
5486 * with the provided extents via xfs_bmse_can_merge().
5490 struct xfs_inode
*ip
,
5492 xfs_fileoff_t shift
, /* shift fsb */
5493 struct xfs_iext_cursor
*icur
,
5494 struct xfs_bmbt_irec
*got
, /* extent to shift */
5495 struct xfs_bmbt_irec
*left
, /* preceding extent */
5496 struct xfs_btree_cur
*cur
,
5497 int *logflags
, /* output */
5498 struct xfs_defer_ops
*dfops
)
5500 struct xfs_bmbt_irec
new;
5501 xfs_filblks_t blockcount
;
5503 struct xfs_mount
*mp
= ip
->i_mount
;
5505 blockcount
= left
->br_blockcount
+ got
->br_blockcount
;
5507 ASSERT(xfs_isilocked(ip
, XFS_IOLOCK_EXCL
));
5508 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
5509 ASSERT(xfs_bmse_can_merge(left
, got
, shift
));
5512 new.br_blockcount
= blockcount
;
5515 * Update the on-disk extent count, the btree if necessary and log the
5518 XFS_IFORK_NEXT_SET(ip
, whichfork
,
5519 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
5520 *logflags
|= XFS_ILOG_CORE
;
5522 *logflags
|= XFS_ILOG_DEXT
;
5526 /* lookup and remove the extent to merge */
5527 error
= xfs_bmbt_lookup_eq(cur
, got
, &i
);
5530 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
5532 error
= xfs_btree_delete(cur
, &i
);
5535 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
5537 /* lookup and update size of the previous extent */
5538 error
= xfs_bmbt_lookup_eq(cur
, left
, &i
);
5541 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
5543 error
= xfs_bmbt_update(cur
, &new);
5548 xfs_iext_remove(ip
, icur
, 0);
5549 xfs_iext_prev(XFS_IFORK_PTR(ip
, whichfork
), icur
);
5550 xfs_iext_update_extent(ip
, xfs_bmap_fork_to_state(whichfork
), icur
,
5553 /* update reverse mapping. rmap functions merge the rmaps for us */
5554 error
= xfs_rmap_unmap_extent(mp
, dfops
, ip
, whichfork
, got
);
5557 memcpy(&new, got
, sizeof(new));
5558 new.br_startoff
= left
->br_startoff
+ left
->br_blockcount
;
5559 return xfs_rmap_map_extent(mp
, dfops
, ip
, whichfork
, &new);
5563 xfs_bmap_shift_update_extent(
5564 struct xfs_inode
*ip
,
5566 struct xfs_iext_cursor
*icur
,
5567 struct xfs_bmbt_irec
*got
,
5568 struct xfs_btree_cur
*cur
,
5570 struct xfs_defer_ops
*dfops
,
5571 xfs_fileoff_t startoff
)
5573 struct xfs_mount
*mp
= ip
->i_mount
;
5574 struct xfs_bmbt_irec prev
= *got
;
5577 *logflags
|= XFS_ILOG_CORE
;
5579 got
->br_startoff
= startoff
;
5582 error
= xfs_bmbt_lookup_eq(cur
, &prev
, &i
);
5585 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
5587 error
= xfs_bmbt_update(cur
, got
);
5591 *logflags
|= XFS_ILOG_DEXT
;
5594 xfs_iext_update_extent(ip
, xfs_bmap_fork_to_state(whichfork
), icur
,
5597 /* update reverse mapping */
5598 error
= xfs_rmap_unmap_extent(mp
, dfops
, ip
, whichfork
, &prev
);
5601 return xfs_rmap_map_extent(mp
, dfops
, ip
, whichfork
, got
);
5605 xfs_bmap_collapse_extents(
5606 struct xfs_trans
*tp
,
5607 struct xfs_inode
*ip
,
5608 xfs_fileoff_t
*next_fsb
,
5609 xfs_fileoff_t offset_shift_fsb
,
5612 int whichfork
= XFS_DATA_FORK
;
5613 struct xfs_mount
*mp
= ip
->i_mount
;
5614 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5615 struct xfs_btree_cur
*cur
= NULL
;
5616 struct xfs_bmbt_irec got
, prev
;
5617 struct xfs_iext_cursor icur
;
5618 xfs_fileoff_t new_startoff
;
5622 if (unlikely(XFS_TEST_ERROR(
5623 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
5624 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
5625 mp
, XFS_ERRTAG_BMAPIFORMAT
))) {
5626 XFS_ERROR_REPORT(__func__
, XFS_ERRLEVEL_LOW
, mp
);
5627 return -EFSCORRUPTED
;
5630 if (XFS_FORCED_SHUTDOWN(mp
))
5633 ASSERT(xfs_isilocked(ip
, XFS_IOLOCK_EXCL
| XFS_ILOCK_EXCL
));
5635 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
5636 error
= xfs_iread_extents(tp
, ip
, whichfork
);
5641 if (ifp
->if_flags
& XFS_IFBROOT
) {
5642 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
5643 cur
->bc_private
.b
.flags
= 0;
5646 if (!xfs_iext_lookup_extent(ip
, ifp
, *next_fsb
, &icur
, &got
)) {
5650 XFS_WANT_CORRUPTED_GOTO(mp
, !isnullstartblock(got
.br_startblock
),
5653 new_startoff
= got
.br_startoff
- offset_shift_fsb
;
5654 if (xfs_iext_peek_prev_extent(ifp
, &icur
, &prev
)) {
5655 if (new_startoff
< prev
.br_startoff
+ prev
.br_blockcount
) {
5660 if (xfs_bmse_can_merge(&prev
, &got
, offset_shift_fsb
)) {
5661 error
= xfs_bmse_merge(ip
, whichfork
, offset_shift_fsb
,
5662 &icur
, &got
, &prev
, cur
, &logflags
,
5669 if (got
.br_startoff
< offset_shift_fsb
) {
5675 error
= xfs_bmap_shift_update_extent(ip
, whichfork
, &icur
, &got
, cur
,
5676 &logflags
, tp
->t_dfops
, new_startoff
);
5681 if (!xfs_iext_next_extent(ifp
, &icur
, &got
)) {
5686 *next_fsb
= got
.br_startoff
;
5689 xfs_btree_del_cursor(cur
, error
);
5691 xfs_trans_log_inode(tp
, ip
, logflags
);
5695 /* Make sure we won't be right-shifting an extent past the maximum bound. */
5697 xfs_bmap_can_insert_extents(
5698 struct xfs_inode
*ip
,
5700 xfs_fileoff_t shift
)
5702 struct xfs_bmbt_irec got
;
5706 ASSERT(xfs_isilocked(ip
, XFS_IOLOCK_EXCL
));
5708 if (XFS_FORCED_SHUTDOWN(ip
->i_mount
))
5711 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
5712 error
= xfs_bmap_last_extent(NULL
, ip
, XFS_DATA_FORK
, &got
, &is_empty
);
5713 if (!error
&& !is_empty
&& got
.br_startoff
>= off
&&
5714 ((got
.br_startoff
+ shift
) & BMBT_STARTOFF_MASK
) < got
.br_startoff
)
5716 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
5722 xfs_bmap_insert_extents(
5723 struct xfs_trans
*tp
,
5724 struct xfs_inode
*ip
,
5725 xfs_fileoff_t
*next_fsb
,
5726 xfs_fileoff_t offset_shift_fsb
,
5728 xfs_fileoff_t stop_fsb
)
5730 int whichfork
= XFS_DATA_FORK
;
5731 struct xfs_mount
*mp
= ip
->i_mount
;
5732 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5733 struct xfs_btree_cur
*cur
= NULL
;
5734 struct xfs_bmbt_irec got
, next
;
5735 struct xfs_iext_cursor icur
;
5736 xfs_fileoff_t new_startoff
;
5740 if (unlikely(XFS_TEST_ERROR(
5741 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
5742 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
5743 mp
, XFS_ERRTAG_BMAPIFORMAT
))) {
5744 XFS_ERROR_REPORT(__func__
, XFS_ERRLEVEL_LOW
, mp
);
5745 return -EFSCORRUPTED
;
5748 if (XFS_FORCED_SHUTDOWN(mp
))
5751 ASSERT(xfs_isilocked(ip
, XFS_IOLOCK_EXCL
| XFS_ILOCK_EXCL
));
5753 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
5754 error
= xfs_iread_extents(tp
, ip
, whichfork
);
5759 if (ifp
->if_flags
& XFS_IFBROOT
) {
5760 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
5761 cur
->bc_private
.b
.flags
= 0;
5764 if (*next_fsb
== NULLFSBLOCK
) {
5765 xfs_iext_last(ifp
, &icur
);
5766 if (!xfs_iext_get_extent(ifp
, &icur
, &got
) ||
5767 stop_fsb
> got
.br_startoff
) {
5772 if (!xfs_iext_lookup_extent(ip
, ifp
, *next_fsb
, &icur
, &got
)) {
5777 XFS_WANT_CORRUPTED_GOTO(mp
, !isnullstartblock(got
.br_startblock
),
5780 if (stop_fsb
>= got
.br_startoff
+ got
.br_blockcount
) {
5785 new_startoff
= got
.br_startoff
+ offset_shift_fsb
;
5786 if (xfs_iext_peek_next_extent(ifp
, &icur
, &next
)) {
5787 if (new_startoff
+ got
.br_blockcount
> next
.br_startoff
) {
5793 * Unlike a left shift (which involves a hole punch), a right
5794 * shift does not modify extent neighbors in any way. We should
5795 * never find mergeable extents in this scenario. Check anyways
5796 * and warn if we encounter two extents that could be one.
5798 if (xfs_bmse_can_merge(&got
, &next
, offset_shift_fsb
))
5802 error
= xfs_bmap_shift_update_extent(ip
, whichfork
, &icur
, &got
, cur
,
5803 &logflags
, tp
->t_dfops
, new_startoff
);
5807 if (!xfs_iext_prev_extent(ifp
, &icur
, &got
) ||
5808 stop_fsb
>= got
.br_startoff
+ got
.br_blockcount
) {
5813 *next_fsb
= got
.br_startoff
;
5816 xfs_btree_del_cursor(cur
, error
);
5818 xfs_trans_log_inode(tp
, ip
, logflags
);
5823 * Splits an extent into two extents at split_fsb block such that it is the
5824 * first block of the current_ext. @ext is a target extent to be split.
5825 * @split_fsb is a block where the extents is split. If split_fsb lies in a
5826 * hole or the first block of extents, just return 0.
5829 xfs_bmap_split_extent_at(
5830 struct xfs_trans
*tp
,
5831 struct xfs_inode
*ip
,
5832 xfs_fileoff_t split_fsb
)
5834 int whichfork
= XFS_DATA_FORK
;
5835 struct xfs_btree_cur
*cur
= NULL
;
5836 struct xfs_bmbt_irec got
;
5837 struct xfs_bmbt_irec
new; /* split extent */
5838 struct xfs_mount
*mp
= ip
->i_mount
;
5839 struct xfs_ifork
*ifp
;
5840 xfs_fsblock_t gotblkcnt
; /* new block count for got */
5841 struct xfs_iext_cursor icur
;
5846 if (unlikely(XFS_TEST_ERROR(
5847 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
5848 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
5849 mp
, XFS_ERRTAG_BMAPIFORMAT
))) {
5850 XFS_ERROR_REPORT("xfs_bmap_split_extent_at",
5851 XFS_ERRLEVEL_LOW
, mp
);
5852 return -EFSCORRUPTED
;
5855 if (XFS_FORCED_SHUTDOWN(mp
))
5858 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5859 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
5860 /* Read in all the extents */
5861 error
= xfs_iread_extents(tp
, ip
, whichfork
);
5867 * If there are not extents, or split_fsb lies in a hole we are done.
5869 if (!xfs_iext_lookup_extent(ip
, ifp
, split_fsb
, &icur
, &got
) ||
5870 got
.br_startoff
>= split_fsb
)
5873 gotblkcnt
= split_fsb
- got
.br_startoff
;
5874 new.br_startoff
= split_fsb
;
5875 new.br_startblock
= got
.br_startblock
+ gotblkcnt
;
5876 new.br_blockcount
= got
.br_blockcount
- gotblkcnt
;
5877 new.br_state
= got
.br_state
;
5879 if (ifp
->if_flags
& XFS_IFBROOT
) {
5880 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
5881 cur
->bc_private
.b
.flags
= 0;
5882 error
= xfs_bmbt_lookup_eq(cur
, &got
, &i
);
5885 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, del_cursor
);
5888 got
.br_blockcount
= gotblkcnt
;
5889 xfs_iext_update_extent(ip
, xfs_bmap_fork_to_state(whichfork
), &icur
,
5892 logflags
= XFS_ILOG_CORE
;
5894 error
= xfs_bmbt_update(cur
, &got
);
5898 logflags
|= XFS_ILOG_DEXT
;
5900 /* Add new extent */
5901 xfs_iext_next(ifp
, &icur
);
5902 xfs_iext_insert(ip
, &icur
, &new, 0);
5903 XFS_IFORK_NEXT_SET(ip
, whichfork
,
5904 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
5907 error
= xfs_bmbt_lookup_eq(cur
, &new, &i
);
5910 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, del_cursor
);
5911 error
= xfs_btree_insert(cur
, &i
);
5914 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, del_cursor
);
5918 * Convert to a btree if necessary.
5920 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
5921 int tmp_logflags
; /* partial log flag return val */
5923 ASSERT(cur
== NULL
);
5924 error
= xfs_bmap_extents_to_btree(tp
, ip
, &cur
, 0,
5925 &tmp_logflags
, whichfork
);
5926 logflags
|= tmp_logflags
;
5931 cur
->bc_private
.b
.allocated
= 0;
5932 xfs_btree_del_cursor(cur
, error
);
5936 xfs_trans_log_inode(tp
, ip
, logflags
);
5941 xfs_bmap_split_extent(
5942 struct xfs_inode
*ip
,
5943 xfs_fileoff_t split_fsb
)
5945 struct xfs_mount
*mp
= ip
->i_mount
;
5946 struct xfs_trans
*tp
;
5947 struct xfs_defer_ops dfops
;
5950 error
= xfs_trans_alloc(mp
, &M_RES(mp
)->tr_write
,
5951 XFS_DIOSTRAT_SPACE_RES(mp
, 0), 0, 0, &tp
);
5954 xfs_defer_init(tp
, &dfops
);
5956 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
5957 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
);
5959 error
= xfs_bmap_split_extent_at(tp
, ip
, split_fsb
);
5963 error
= xfs_defer_finish(&tp
, &dfops
);
5967 return xfs_trans_commit(tp
);
5970 xfs_defer_cancel(&dfops
);
5971 xfs_trans_cancel(tp
);
5975 /* Deferred mapping is only for real extents in the data fork. */
5977 xfs_bmap_is_update_needed(
5978 struct xfs_bmbt_irec
*bmap
)
5980 return bmap
->br_startblock
!= HOLESTARTBLOCK
&&
5981 bmap
->br_startblock
!= DELAYSTARTBLOCK
;
5984 /* Record a bmap intent. */
5987 struct xfs_mount
*mp
,
5988 struct xfs_defer_ops
*dfops
,
5989 enum xfs_bmap_intent_type type
,
5990 struct xfs_inode
*ip
,
5992 struct xfs_bmbt_irec
*bmap
)
5995 struct xfs_bmap_intent
*bi
;
5997 trace_xfs_bmap_defer(mp
,
5998 XFS_FSB_TO_AGNO(mp
, bmap
->br_startblock
),
6000 XFS_FSB_TO_AGBNO(mp
, 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 error
= xfs_defer_ijoin(dfops
, bi
->bi_owner
);
6019 xfs_defer_add(dfops
, XFS_DEFER_OPS_TYPE_BMAP
, &bi
->bi_list
);
6023 /* Map an extent into a file. */
6025 xfs_bmap_map_extent(
6026 struct xfs_mount
*mp
,
6027 struct xfs_defer_ops
*dfops
,
6028 struct xfs_inode
*ip
,
6029 struct xfs_bmbt_irec
*PREV
)
6031 if (!xfs_bmap_is_update_needed(PREV
))
6034 return __xfs_bmap_add(mp
, dfops
, XFS_BMAP_MAP
, ip
,
6035 XFS_DATA_FORK
, PREV
);
6038 /* Unmap an extent out of a file. */
6040 xfs_bmap_unmap_extent(
6041 struct xfs_mount
*mp
,
6042 struct xfs_defer_ops
*dfops
,
6043 struct xfs_inode
*ip
,
6044 struct xfs_bmbt_irec
*PREV
)
6046 if (!xfs_bmap_is_update_needed(PREV
))
6049 return __xfs_bmap_add(mp
, dfops
, XFS_BMAP_UNMAP
, ip
,
6050 XFS_DATA_FORK
, PREV
);
6054 * Process one of the deferred bmap operations. We pass back the
6055 * btree cursor to maintain our lock on the bmapbt between calls.
6058 xfs_bmap_finish_one(
6059 struct xfs_trans
*tp
,
6060 struct xfs_defer_ops
*dfops
,
6061 struct xfs_inode
*ip
,
6062 enum xfs_bmap_intent_type type
,
6064 xfs_fileoff_t startoff
,
6065 xfs_fsblock_t startblock
,
6066 xfs_filblks_t
*blockcount
,
6071 ASSERT(tp
->t_firstblock
== NULLFSBLOCK
);
6073 trace_xfs_bmap_deferred(tp
->t_mountp
,
6074 XFS_FSB_TO_AGNO(tp
->t_mountp
, startblock
), type
,
6075 XFS_FSB_TO_AGBNO(tp
->t_mountp
, startblock
),
6076 ip
->i_ino
, whichfork
, startoff
, *blockcount
, state
);
6078 if (WARN_ON_ONCE(whichfork
!= XFS_DATA_FORK
))
6079 return -EFSCORRUPTED
;
6081 if (XFS_TEST_ERROR(false, tp
->t_mountp
,
6082 XFS_ERRTAG_BMAP_FINISH_ONE
))
6087 ASSERT(dfops
== tp
->t_dfops
);
6088 error
= xfs_bmapi_remap(tp
, ip
, startoff
, *blockcount
,
6092 case XFS_BMAP_UNMAP
:
6093 error
= __xfs_bunmapi(tp
, ip
, startoff
, blockcount
,
6094 XFS_BMAPI_REMAP
, 1);
6098 error
= -EFSCORRUPTED
;
6104 /* Check that an inode's extent does not have invalid flags or bad ranges. */
6106 xfs_bmap_validate_extent(
6107 struct xfs_inode
*ip
,
6109 struct xfs_bmbt_irec
*irec
)
6111 struct xfs_mount
*mp
= ip
->i_mount
;
6112 xfs_fsblock_t endfsb
;
6115 isrt
= XFS_IS_REALTIME_INODE(ip
);
6116 endfsb
= irec
->br_startblock
+ irec
->br_blockcount
- 1;
6118 if (!xfs_verify_rtbno(mp
, irec
->br_startblock
))
6119 return __this_address
;
6120 if (!xfs_verify_rtbno(mp
, endfsb
))
6121 return __this_address
;
6123 if (!xfs_verify_fsbno(mp
, irec
->br_startblock
))
6124 return __this_address
;
6125 if (!xfs_verify_fsbno(mp
, endfsb
))
6126 return __this_address
;
6127 if (XFS_FSB_TO_AGNO(mp
, irec
->br_startblock
) !=
6128 XFS_FSB_TO_AGNO(mp
, endfsb
))
6129 return __this_address
;
6131 if (irec
->br_state
!= XFS_EXT_NORM
) {
6132 if (whichfork
!= XFS_DATA_FORK
)
6133 return __this_address
;
6134 if (!xfs_sb_version_hasextflgbit(&mp
->m_sb
))
6135 return __this_address
;