1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
6 #include "libxfs_priv.h"
8 #include "xfs_shared.h"
9 #include "xfs_format.h"
10 #include "xfs_log_format.h"
11 #include "xfs_trans_resv.h"
14 #include "xfs_mount.h"
15 #include "xfs_defer.h"
16 #include "xfs_da_format.h"
17 #include "xfs_da_btree.h"
19 #include "xfs_inode.h"
20 #include "xfs_btree.h"
21 #include "xfs_trans.h"
22 #include "xfs_alloc.h"
24 #include "xfs_bmap_btree.h"
25 #include "xfs_errortag.h"
26 #include "xfs_trans_space.h"
27 #include "xfs_trace.h"
28 #include "xfs_attr_leaf.h"
29 #include "xfs_quota_defs.h"
31 #include "xfs_ag_resv.h"
32 #include "xfs_refcount.h"
35 kmem_zone_t
*xfs_bmap_free_item_zone
;
38 * Miscellaneous helper functions
42 * Compute and fill in the value of the maximum depth of a bmap btree
43 * in this filesystem. Done once, during mount.
46 xfs_bmap_compute_maxlevels(
47 xfs_mount_t
*mp
, /* file system mount structure */
48 int whichfork
) /* data or attr fork */
50 int level
; /* btree level */
51 uint maxblocks
; /* max blocks at this level */
52 uint maxleafents
; /* max leaf entries possible */
53 int maxrootrecs
; /* max records in root block */
54 int minleafrecs
; /* min records in leaf block */
55 int minnoderecs
; /* min records in node block */
56 int sz
; /* root block size */
59 * The maximum number of extents in a file, hence the maximum
60 * number of leaf entries, is controlled by the type of di_nextents
61 * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
62 * (a signed 16-bit number, xfs_aextnum_t).
64 * Note that we can no longer assume that if we are in ATTR1 that
65 * the fork offset of all the inodes will be
66 * (xfs_default_attroffset(ip) >> 3) because we could have mounted
67 * with ATTR2 and then mounted back with ATTR1, keeping the
68 * di_forkoff's fixed but probably at various positions. Therefore,
69 * for both ATTR1 and ATTR2 we have to assume the worst case scenario
70 * of a minimum size available.
72 if (whichfork
== XFS_DATA_FORK
) {
73 maxleafents
= MAXEXTNUM
;
74 sz
= XFS_BMDR_SPACE_CALC(MINDBTPTRS
);
76 maxleafents
= MAXAEXTNUM
;
77 sz
= XFS_BMDR_SPACE_CALC(MINABTPTRS
);
79 maxrootrecs
= xfs_bmdr_maxrecs(sz
, 0);
80 minleafrecs
= mp
->m_bmap_dmnr
[0];
81 minnoderecs
= mp
->m_bmap_dmnr
[1];
82 maxblocks
= (maxleafents
+ minleafrecs
- 1) / minleafrecs
;
83 for (level
= 1; maxblocks
> 1; level
++) {
84 if (maxblocks
<= maxrootrecs
)
87 maxblocks
= (maxblocks
+ minnoderecs
- 1) / minnoderecs
;
89 mp
->m_bm_maxlevels
[whichfork
] = level
;
92 STATIC
int /* error */
94 struct xfs_btree_cur
*cur
,
95 struct xfs_bmbt_irec
*irec
,
96 int *stat
) /* success/failure */
98 cur
->bc_rec
.b
= *irec
;
99 return xfs_btree_lookup(cur
, XFS_LOOKUP_EQ
, stat
);
102 STATIC
int /* error */
103 xfs_bmbt_lookup_first(
104 struct xfs_btree_cur
*cur
,
105 int *stat
) /* success/failure */
107 cur
->bc_rec
.b
.br_startoff
= 0;
108 cur
->bc_rec
.b
.br_startblock
= 0;
109 cur
->bc_rec
.b
.br_blockcount
= 0;
110 return xfs_btree_lookup(cur
, XFS_LOOKUP_GE
, stat
);
114 * Check if the inode needs to be converted to btree format.
116 static inline bool xfs_bmap_needs_btree(struct xfs_inode
*ip
, int whichfork
)
118 return whichfork
!= XFS_COW_FORK
&&
119 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
&&
120 XFS_IFORK_NEXTENTS(ip
, whichfork
) >
121 XFS_IFORK_MAXEXT(ip
, whichfork
);
125 * Check if the inode should be converted to extent format.
127 static inline bool xfs_bmap_wants_extents(struct xfs_inode
*ip
, int whichfork
)
129 return whichfork
!= XFS_COW_FORK
&&
130 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
&&
131 XFS_IFORK_NEXTENTS(ip
, whichfork
) <=
132 XFS_IFORK_MAXEXT(ip
, whichfork
);
136 * Update the record referred to by cur to the value given by irec
137 * This either works (return 0) or gets an EFSCORRUPTED error.
141 struct xfs_btree_cur
*cur
,
142 struct xfs_bmbt_irec
*irec
)
144 union xfs_btree_rec rec
;
146 xfs_bmbt_disk_set_all(&rec
.bmbt
, irec
);
147 return xfs_btree_update(cur
, &rec
);
151 * Compute the worst-case number of indirect blocks that will be used
152 * for ip's delayed extent of length "len".
155 xfs_bmap_worst_indlen(
156 xfs_inode_t
*ip
, /* incore inode pointer */
157 xfs_filblks_t len
) /* delayed extent length */
159 int level
; /* btree level number */
160 int maxrecs
; /* maximum record count at this level */
161 xfs_mount_t
*mp
; /* mount structure */
162 xfs_filblks_t rval
; /* return value */
165 maxrecs
= mp
->m_bmap_dmxr
[0];
166 for (level
= 0, rval
= 0;
167 level
< XFS_BM_MAXLEVELS(mp
, XFS_DATA_FORK
);
170 do_div(len
, maxrecs
);
173 return rval
+ XFS_BM_MAXLEVELS(mp
, XFS_DATA_FORK
) -
176 maxrecs
= mp
->m_bmap_dmxr
[1];
182 * Calculate the default attribute fork offset for newly created inodes.
185 xfs_default_attroffset(
186 struct xfs_inode
*ip
)
188 struct xfs_mount
*mp
= ip
->i_mount
;
191 if (mp
->m_sb
.sb_inodesize
== 256) {
192 offset
= XFS_LITINO(mp
, ip
->i_d
.di_version
) -
193 XFS_BMDR_SPACE_CALC(MINABTPTRS
);
195 offset
= XFS_BMDR_SPACE_CALC(6 * MINABTPTRS
);
198 ASSERT(offset
< XFS_LITINO(mp
, ip
->i_d
.di_version
));
203 * Helper routine to reset inode di_forkoff field when switching
204 * attribute fork from local to extent format - we reset it where
205 * possible to make space available for inline data fork extents.
208 xfs_bmap_forkoff_reset(
212 if (whichfork
== XFS_ATTR_FORK
&&
213 ip
->i_d
.di_format
!= XFS_DINODE_FMT_DEV
&&
214 ip
->i_d
.di_format
!= XFS_DINODE_FMT_BTREE
) {
215 uint dfl_forkoff
= xfs_default_attroffset(ip
) >> 3;
217 if (dfl_forkoff
> ip
->i_d
.di_forkoff
)
218 ip
->i_d
.di_forkoff
= dfl_forkoff
;
223 STATIC
struct xfs_buf
*
225 struct xfs_btree_cur
*cur
,
228 struct xfs_log_item
*lip
;
234 for (i
= 0; i
< XFS_BTREE_MAXLEVELS
; i
++) {
235 if (!cur
->bc_bufs
[i
])
237 if (XFS_BUF_ADDR(cur
->bc_bufs
[i
]) == bno
)
238 return cur
->bc_bufs
[i
];
241 /* Chase down all the log items to see if the bp is there */
242 list_for_each_entry(lip
, &cur
->bc_tp
->t_items
, li_trans
) {
243 struct xfs_buf_log_item
*bip
= (struct xfs_buf_log_item
*)lip
;
245 if (bip
->bli_item
.li_type
== XFS_LI_BUF
&&
246 XFS_BUF_ADDR(bip
->bli_buf
) == bno
)
255 struct xfs_btree_block
*block
,
261 __be64
*pp
, *thispa
; /* pointer to block address */
262 xfs_bmbt_key_t
*prevp
, *keyp
;
264 ASSERT(be16_to_cpu(block
->bb_level
) > 0);
267 for( i
= 1; i
<= xfs_btree_get_numrecs(block
); i
++) {
268 dmxr
= mp
->m_bmap_dmxr
[0];
269 keyp
= XFS_BMBT_KEY_ADDR(mp
, block
, i
);
272 ASSERT(be64_to_cpu(prevp
->br_startoff
) <
273 be64_to_cpu(keyp
->br_startoff
));
278 * Compare the block numbers to see if there are dups.
281 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, i
, sz
);
283 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, i
, dmxr
);
285 for (j
= i
+1; j
<= be16_to_cpu(block
->bb_numrecs
); j
++) {
287 thispa
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, j
, sz
);
289 thispa
= XFS_BMBT_PTR_ADDR(mp
, block
, j
, dmxr
);
290 if (*thispa
== *pp
) {
291 xfs_warn(mp
, "%s: thispa(%d) == pp(%d) %Ld",
293 (unsigned long long)be64_to_cpu(*thispa
));
294 xfs_err(mp
, "%s: ptrs are equal in node\n",
296 xfs_force_shutdown(mp
, SHUTDOWN_CORRUPT_INCORE
);
303 * Check that the extents for the inode ip are in the right order in all
304 * btree leaves. THis becomes prohibitively expensive for large extent count
305 * files, so don't bother with inodes that have more than 10,000 extents in
306 * them. The btree record ordering checks will still be done, so for such large
307 * bmapbt constructs that is going to catch most corruptions.
310 xfs_bmap_check_leaf_extents(
311 xfs_btree_cur_t
*cur
, /* btree cursor or null */
312 xfs_inode_t
*ip
, /* incore inode pointer */
313 int whichfork
) /* data or attr fork */
315 struct xfs_btree_block
*block
; /* current btree block */
316 xfs_fsblock_t bno
; /* block # of "block" */
317 xfs_buf_t
*bp
; /* buffer for "block" */
318 int error
; /* error return value */
319 xfs_extnum_t i
=0, j
; /* index into the extents list */
320 struct xfs_ifork
*ifp
; /* fork structure */
321 int level
; /* btree level, for checking */
322 xfs_mount_t
*mp
; /* file system mount structure */
323 __be64
*pp
; /* pointer to block address */
324 xfs_bmbt_rec_t
*ep
; /* pointer to current extent */
325 xfs_bmbt_rec_t last
= {0, 0}; /* last extent in prev block */
326 xfs_bmbt_rec_t
*nextp
; /* pointer to next extent */
329 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
) {
333 /* skip large extent count inodes */
334 if (ip
->i_d
.di_nextents
> 10000)
339 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
340 block
= ifp
->if_broot
;
342 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
344 level
= be16_to_cpu(block
->bb_level
);
346 xfs_check_block(block
, mp
, 1, ifp
->if_broot_bytes
);
347 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, 1, ifp
->if_broot_bytes
);
348 bno
= be64_to_cpu(*pp
);
350 ASSERT(bno
!= NULLFSBLOCK
);
351 ASSERT(XFS_FSB_TO_AGNO(mp
, bno
) < mp
->m_sb
.sb_agcount
);
352 ASSERT(XFS_FSB_TO_AGBNO(mp
, bno
) < mp
->m_sb
.sb_agblocks
);
355 * Go down the tree until leaf level is reached, following the first
356 * pointer (leftmost) at each level.
358 while (level
-- > 0) {
359 /* See if buf is in cur first */
361 bp
= xfs_bmap_get_bp(cur
, XFS_FSB_TO_DADDR(mp
, bno
));
364 error
= xfs_btree_read_bufl(mp
, NULL
, bno
, 0, &bp
,
370 block
= XFS_BUF_TO_BLOCK(bp
);
375 * Check this block for basic sanity (increasing keys and
376 * no duplicate blocks).
379 xfs_check_block(block
, mp
, 0, 0);
380 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, mp
->m_bmap_dmxr
[1]);
381 bno
= be64_to_cpu(*pp
);
382 XFS_WANT_CORRUPTED_GOTO(mp
,
383 xfs_verify_fsbno(mp
, bno
), error0
);
386 xfs_trans_brelse(NULL
, bp
);
391 * Here with bp and block set to the leftmost leaf node in the tree.
396 * Loop over all leaf nodes checking that all extents are in the right order.
399 xfs_fsblock_t nextbno
;
400 xfs_extnum_t num_recs
;
403 num_recs
= xfs_btree_get_numrecs(block
);
406 * Read-ahead the next leaf block, if any.
409 nextbno
= be64_to_cpu(block
->bb_u
.l
.bb_rightsib
);
412 * Check all the extents to make sure they are OK.
413 * If we had a previous block, the last entry should
414 * conform with the first entry in this one.
417 ep
= XFS_BMBT_REC_ADDR(mp
, block
, 1);
419 ASSERT(xfs_bmbt_disk_get_startoff(&last
) +
420 xfs_bmbt_disk_get_blockcount(&last
) <=
421 xfs_bmbt_disk_get_startoff(ep
));
423 for (j
= 1; j
< num_recs
; j
++) {
424 nextp
= XFS_BMBT_REC_ADDR(mp
, block
, j
+ 1);
425 ASSERT(xfs_bmbt_disk_get_startoff(ep
) +
426 xfs_bmbt_disk_get_blockcount(ep
) <=
427 xfs_bmbt_disk_get_startoff(nextp
));
435 xfs_trans_brelse(NULL
, bp
);
439 * If we've reached the end, stop.
441 if (bno
== NULLFSBLOCK
)
445 bp
= xfs_bmap_get_bp(cur
, XFS_FSB_TO_DADDR(mp
, bno
));
448 error
= xfs_btree_read_bufl(mp
, NULL
, bno
, 0, &bp
,
454 block
= XFS_BUF_TO_BLOCK(bp
);
460 xfs_warn(mp
, "%s: at error0", __func__
);
462 xfs_trans_brelse(NULL
, bp
);
464 xfs_warn(mp
, "%s: BAD after btree leaves for %d extents",
466 xfs_err(mp
, "%s: CORRUPTED BTREE OR SOMETHING", __func__
);
467 xfs_force_shutdown(mp
, SHUTDOWN_CORRUPT_INCORE
);
472 * Validate that the bmbt_irecs being returned from bmapi are valid
473 * given the caller's original parameters. Specifically check the
474 * ranges of the returned irecs to ensure that they only extend beyond
475 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
478 xfs_bmap_validate_ret(
482 xfs_bmbt_irec_t
*mval
,
486 int i
; /* index to map values */
488 ASSERT(ret_nmap
<= nmap
);
490 for (i
= 0; i
< ret_nmap
; i
++) {
491 ASSERT(mval
[i
].br_blockcount
> 0);
492 if (!(flags
& XFS_BMAPI_ENTIRE
)) {
493 ASSERT(mval
[i
].br_startoff
>= bno
);
494 ASSERT(mval
[i
].br_blockcount
<= len
);
495 ASSERT(mval
[i
].br_startoff
+ mval
[i
].br_blockcount
<=
498 ASSERT(mval
[i
].br_startoff
< bno
+ len
);
499 ASSERT(mval
[i
].br_startoff
+ mval
[i
].br_blockcount
>
503 mval
[i
- 1].br_startoff
+ mval
[i
- 1].br_blockcount
==
504 mval
[i
].br_startoff
);
505 ASSERT(mval
[i
].br_startblock
!= DELAYSTARTBLOCK
&&
506 mval
[i
].br_startblock
!= HOLESTARTBLOCK
);
507 ASSERT(mval
[i
].br_state
== XFS_EXT_NORM
||
508 mval
[i
].br_state
== XFS_EXT_UNWRITTEN
);
513 #define xfs_bmap_check_leaf_extents(cur, ip, whichfork) do { } while (0)
514 #define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap) do { } while (0)
518 * bmap free list manipulation functions
522 * Add the extent to the list of extents to be free at transaction end.
523 * The list is maintained sorted (by block number).
527 struct xfs_trans
*tp
,
530 const struct xfs_owner_info
*oinfo
,
533 struct xfs_extent_free_item
*new; /* new element */
535 struct xfs_mount
*mp
= tp
->t_mountp
;
539 ASSERT(bno
!= NULLFSBLOCK
);
541 ASSERT(len
<= MAXEXTLEN
);
542 ASSERT(!isnullstartblock(bno
));
543 agno
= XFS_FSB_TO_AGNO(mp
, bno
);
544 agbno
= XFS_FSB_TO_AGBNO(mp
, bno
);
545 ASSERT(agno
< mp
->m_sb
.sb_agcount
);
546 ASSERT(agbno
< mp
->m_sb
.sb_agblocks
);
547 ASSERT(len
< mp
->m_sb
.sb_agblocks
);
548 ASSERT(agbno
+ len
<= mp
->m_sb
.sb_agblocks
);
550 ASSERT(xfs_bmap_free_item_zone
!= NULL
);
552 new = kmem_zone_alloc(xfs_bmap_free_item_zone
, KM_SLEEP
);
553 new->xefi_startblock
= bno
;
554 new->xefi_blockcount
= (xfs_extlen_t
)len
;
556 new->xefi_oinfo
= *oinfo
;
558 new->xefi_oinfo
= XFS_RMAP_OINFO_SKIP_UPDATE
;
559 new->xefi_skip_discard
= skip_discard
;
560 trace_xfs_bmap_free_defer(tp
->t_mountp
,
561 XFS_FSB_TO_AGNO(tp
->t_mountp
, bno
), 0,
562 XFS_FSB_TO_AGBNO(tp
->t_mountp
, bno
), len
);
563 xfs_defer_add(tp
, XFS_DEFER_OPS_TYPE_FREE
, &new->xefi_list
);
567 * Inode fork format manipulation functions
571 * Convert the inode format to extent format if it currently is in btree format,
572 * but the extent list is small enough that it fits into the extent format.
574 * Since the extents are already in-core, all we have to do is give up the space
575 * for the btree root and pitch the leaf block.
577 STATIC
int /* error */
578 xfs_bmap_btree_to_extents(
579 struct xfs_trans
*tp
, /* transaction pointer */
580 struct xfs_inode
*ip
, /* incore inode pointer */
581 struct xfs_btree_cur
*cur
, /* btree cursor */
582 int *logflagsp
, /* inode logging flags */
583 int whichfork
) /* data or attr fork */
585 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
586 struct xfs_mount
*mp
= ip
->i_mount
;
587 struct xfs_btree_block
*rblock
= ifp
->if_broot
;
588 struct xfs_btree_block
*cblock
;/* child btree block */
589 xfs_fsblock_t cbno
; /* child block number */
590 xfs_buf_t
*cbp
; /* child block's buffer */
591 int error
; /* error return value */
592 __be64
*pp
; /* ptr to block address */
593 struct xfs_owner_info oinfo
;
595 /* check if we actually need the extent format first: */
596 if (!xfs_bmap_wants_extents(ip
, whichfork
))
600 ASSERT(whichfork
!= XFS_COW_FORK
);
601 ASSERT(ifp
->if_flags
& XFS_IFEXTENTS
);
602 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
);
603 ASSERT(be16_to_cpu(rblock
->bb_level
) == 1);
604 ASSERT(be16_to_cpu(rblock
->bb_numrecs
) == 1);
605 ASSERT(xfs_bmbt_maxrecs(mp
, ifp
->if_broot_bytes
, 0) == 1);
607 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, rblock
, 1, ifp
->if_broot_bytes
);
608 cbno
= be64_to_cpu(*pp
);
610 XFS_WANT_CORRUPTED_RETURN(cur
->bc_mp
,
611 xfs_btree_check_lptr(cur
, cbno
, 1));
613 error
= xfs_btree_read_bufl(mp
, tp
, cbno
, 0, &cbp
, XFS_BMAP_BTREE_REF
,
617 cblock
= XFS_BUF_TO_BLOCK(cbp
);
618 if ((error
= xfs_btree_check_block(cur
, cblock
, 0, cbp
)))
620 xfs_rmap_ino_bmbt_owner(&oinfo
, ip
->i_ino
, whichfork
);
621 xfs_bmap_add_free(cur
->bc_tp
, cbno
, 1, &oinfo
);
622 ip
->i_d
.di_nblocks
--;
623 xfs_trans_mod_dquot_byino(tp
, ip
, XFS_TRANS_DQ_BCOUNT
, -1L);
624 xfs_trans_binval(tp
, cbp
);
625 if (cur
->bc_bufs
[0] == cbp
)
626 cur
->bc_bufs
[0] = NULL
;
627 xfs_iroot_realloc(ip
, -1, whichfork
);
628 ASSERT(ifp
->if_broot
== NULL
);
629 ASSERT((ifp
->if_flags
& XFS_IFBROOT
) == 0);
630 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_EXTENTS
);
631 *logflagsp
|= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
636 * Convert an extents-format file into a btree-format file.
637 * The new file will have a root block (in the inode) and a single child block.
639 STATIC
int /* error */
640 xfs_bmap_extents_to_btree(
641 struct xfs_trans
*tp
, /* transaction pointer */
642 struct xfs_inode
*ip
, /* incore inode pointer */
643 struct xfs_btree_cur
**curp
, /* cursor returned to caller */
644 int wasdel
, /* converting a delayed alloc */
645 int *logflagsp
, /* inode logging flags */
646 int whichfork
) /* data or attr fork */
648 struct xfs_btree_block
*ablock
; /* allocated (child) bt block */
649 struct xfs_buf
*abp
; /* buffer for ablock */
650 struct xfs_alloc_arg args
; /* allocation arguments */
651 struct xfs_bmbt_rec
*arp
; /* child record pointer */
652 struct xfs_btree_block
*block
; /* btree root block */
653 struct xfs_btree_cur
*cur
; /* bmap btree cursor */
654 int error
; /* error return value */
655 struct xfs_ifork
*ifp
; /* inode fork pointer */
656 struct xfs_bmbt_key
*kp
; /* root block key pointer */
657 struct xfs_mount
*mp
; /* mount structure */
658 xfs_bmbt_ptr_t
*pp
; /* root block address pointer */
659 struct xfs_iext_cursor icur
;
660 struct xfs_bmbt_irec rec
;
661 xfs_extnum_t cnt
= 0;
664 ASSERT(whichfork
!= XFS_COW_FORK
);
665 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
666 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
);
669 * Make space in the inode incore. This needs to be undone if we fail
670 * to expand the root.
672 xfs_iroot_realloc(ip
, 1, whichfork
);
673 ifp
->if_flags
|= XFS_IFBROOT
;
678 block
= ifp
->if_broot
;
679 xfs_btree_init_block_int(mp
, block
, XFS_BUF_DADDR_NULL
,
680 XFS_BTNUM_BMAP
, 1, 1, ip
->i_ino
,
681 XFS_BTREE_LONG_PTRS
);
683 * Need a cursor. Can't allocate until bb_level is filled in.
685 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
686 cur
->bc_private
.b
.flags
= wasdel
? XFS_BTCUR_BPRV_WASDEL
: 0;
688 * Convert to a btree with two levels, one record in root.
690 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_BTREE
);
691 memset(&args
, 0, sizeof(args
));
694 xfs_rmap_ino_bmbt_owner(&args
.oinfo
, ip
->i_ino
, whichfork
);
695 if (tp
->t_firstblock
== NULLFSBLOCK
) {
696 args
.type
= XFS_ALLOCTYPE_START_BNO
;
697 args
.fsbno
= XFS_INO_TO_FSB(mp
, ip
->i_ino
);
698 } else if (tp
->t_flags
& XFS_TRANS_LOWMODE
) {
699 args
.type
= XFS_ALLOCTYPE_START_BNO
;
700 args
.fsbno
= tp
->t_firstblock
;
702 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
703 args
.fsbno
= tp
->t_firstblock
;
705 args
.minlen
= args
.maxlen
= args
.prod
= 1;
706 args
.wasdel
= wasdel
;
708 error
= xfs_alloc_vextent(&args
);
710 goto out_root_realloc
;
712 if (WARN_ON_ONCE(args
.fsbno
== NULLFSBLOCK
)) {
714 goto out_root_realloc
;
718 * Allocation can't fail, the space was reserved.
720 ASSERT(tp
->t_firstblock
== NULLFSBLOCK
||
721 args
.agno
>= XFS_FSB_TO_AGNO(mp
, tp
->t_firstblock
));
722 tp
->t_firstblock
= args
.fsbno
;
723 cur
->bc_private
.b
.allocated
++;
724 ip
->i_d
.di_nblocks
++;
725 xfs_trans_mod_dquot_byino(tp
, ip
, XFS_TRANS_DQ_BCOUNT
, 1L);
726 abp
= xfs_btree_get_bufl(mp
, tp
, args
.fsbno
, 0);
728 error
= -EFSCORRUPTED
;
729 goto out_unreserve_dquot
;
733 * Fill in the child block.
735 abp
->b_ops
= &xfs_bmbt_buf_ops
;
736 ablock
= XFS_BUF_TO_BLOCK(abp
);
737 xfs_btree_init_block_int(mp
, ablock
, abp
->b_bn
,
738 XFS_BTNUM_BMAP
, 0, 0, ip
->i_ino
,
739 XFS_BTREE_LONG_PTRS
);
741 for_each_xfs_iext(ifp
, &icur
, &rec
) {
742 if (isnullstartblock(rec
.br_startblock
))
744 arp
= XFS_BMBT_REC_ADDR(mp
, ablock
, 1 + cnt
);
745 xfs_bmbt_disk_set_all(arp
, &rec
);
748 ASSERT(cnt
== XFS_IFORK_NEXTENTS(ip
, whichfork
));
749 xfs_btree_set_numrecs(ablock
, cnt
);
752 * Fill in the root key and pointer.
754 kp
= XFS_BMBT_KEY_ADDR(mp
, block
, 1);
755 arp
= XFS_BMBT_REC_ADDR(mp
, ablock
, 1);
756 kp
->br_startoff
= cpu_to_be64(xfs_bmbt_disk_get_startoff(arp
));
757 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, xfs_bmbt_get_maxrecs(cur
,
758 be16_to_cpu(block
->bb_level
)));
759 *pp
= cpu_to_be64(args
.fsbno
);
762 * Do all this logging at the end so that
763 * the root is at the right level.
765 xfs_btree_log_block(cur
, abp
, XFS_BB_ALL_BITS
);
766 xfs_btree_log_recs(cur
, abp
, 1, be16_to_cpu(ablock
->bb_numrecs
));
767 ASSERT(*curp
== NULL
);
769 *logflagsp
= XFS_ILOG_CORE
| xfs_ilog_fbroot(whichfork
);
773 xfs_trans_mod_dquot_byino(tp
, ip
, XFS_TRANS_DQ_BCOUNT
, -1L);
775 xfs_iroot_realloc(ip
, -1, whichfork
);
776 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_EXTENTS
);
777 ASSERT(ifp
->if_broot
== NULL
);
778 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
784 * Convert a local file to an extents file.
785 * This code is out of bounds for data forks of regular files,
786 * since the file data needs to get logged so things will stay consistent.
787 * (The bmap-level manipulations are ok, though).
790 xfs_bmap_local_to_extents_empty(
791 struct xfs_inode
*ip
,
794 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
796 ASSERT(whichfork
!= XFS_COW_FORK
);
797 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
798 ASSERT(ifp
->if_bytes
== 0);
799 ASSERT(XFS_IFORK_NEXTENTS(ip
, whichfork
) == 0);
801 xfs_bmap_forkoff_reset(ip
, whichfork
);
802 ifp
->if_flags
&= ~XFS_IFINLINE
;
803 ifp
->if_flags
|= XFS_IFEXTENTS
;
804 ifp
->if_u1
.if_root
= NULL
;
806 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_EXTENTS
);
810 STATIC
int /* error */
811 xfs_bmap_local_to_extents(
812 xfs_trans_t
*tp
, /* transaction pointer */
813 xfs_inode_t
*ip
, /* incore inode pointer */
814 xfs_extlen_t total
, /* total blocks needed by transaction */
815 int *logflagsp
, /* inode logging flags */
817 void (*init_fn
)(struct xfs_trans
*tp
,
819 struct xfs_inode
*ip
,
820 struct xfs_ifork
*ifp
))
823 int flags
; /* logging flags returned */
824 struct xfs_ifork
*ifp
; /* inode fork pointer */
825 xfs_alloc_arg_t args
; /* allocation arguments */
826 xfs_buf_t
*bp
; /* buffer for extent block */
827 struct xfs_bmbt_irec rec
;
828 struct xfs_iext_cursor icur
;
831 * We don't want to deal with the case of keeping inode data inline yet.
832 * So sending the data fork of a regular inode is invalid.
834 ASSERT(!(S_ISREG(VFS_I(ip
)->i_mode
) && whichfork
== XFS_DATA_FORK
));
835 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
836 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
838 if (!ifp
->if_bytes
) {
839 xfs_bmap_local_to_extents_empty(ip
, whichfork
);
840 flags
= XFS_ILOG_CORE
;
846 ASSERT((ifp
->if_flags
& (XFS_IFINLINE
|XFS_IFEXTENTS
)) == XFS_IFINLINE
);
847 memset(&args
, 0, sizeof(args
));
849 args
.mp
= ip
->i_mount
;
850 xfs_rmap_ino_owner(&args
.oinfo
, ip
->i_ino
, whichfork
, 0);
852 * Allocate a block. We know we need only one, since the
853 * file currently fits in an inode.
855 if (tp
->t_firstblock
== NULLFSBLOCK
) {
856 args
.fsbno
= XFS_INO_TO_FSB(args
.mp
, ip
->i_ino
);
857 args
.type
= XFS_ALLOCTYPE_START_BNO
;
859 args
.fsbno
= tp
->t_firstblock
;
860 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
863 args
.minlen
= args
.maxlen
= args
.prod
= 1;
864 error
= xfs_alloc_vextent(&args
);
868 /* Can't fail, the space was reserved. */
869 ASSERT(args
.fsbno
!= NULLFSBLOCK
);
870 ASSERT(args
.len
== 1);
871 tp
->t_firstblock
= args
.fsbno
;
872 bp
= xfs_btree_get_bufl(args
.mp
, tp
, args
.fsbno
, 0);
875 * Initialize the block, copy the data and log the remote buffer.
877 * The callout is responsible for logging because the remote format
878 * might differ from the local format and thus we don't know how much to
879 * log here. Note that init_fn must also set the buffer log item type
882 init_fn(tp
, bp
, ip
, ifp
);
884 /* account for the change in fork size */
885 xfs_idata_realloc(ip
, -ifp
->if_bytes
, whichfork
);
886 xfs_bmap_local_to_extents_empty(ip
, whichfork
);
887 flags
|= XFS_ILOG_CORE
;
889 ifp
->if_u1
.if_root
= NULL
;
893 rec
.br_startblock
= args
.fsbno
;
894 rec
.br_blockcount
= 1;
895 rec
.br_state
= XFS_EXT_NORM
;
896 xfs_iext_first(ifp
, &icur
);
897 xfs_iext_insert(ip
, &icur
, &rec
, 0);
899 XFS_IFORK_NEXT_SET(ip
, whichfork
, 1);
900 ip
->i_d
.di_nblocks
= 1;
901 xfs_trans_mod_dquot_byino(tp
, ip
,
902 XFS_TRANS_DQ_BCOUNT
, 1L);
903 flags
|= xfs_ilog_fext(whichfork
);
911 * Called from xfs_bmap_add_attrfork to handle btree format files.
913 STATIC
int /* error */
914 xfs_bmap_add_attrfork_btree(
915 xfs_trans_t
*tp
, /* transaction pointer */
916 xfs_inode_t
*ip
, /* incore inode pointer */
917 int *flags
) /* inode logging flags */
919 xfs_btree_cur_t
*cur
; /* btree cursor */
920 int error
; /* error return value */
921 xfs_mount_t
*mp
; /* file system mount struct */
922 int stat
; /* newroot status */
925 if (ip
->i_df
.if_broot_bytes
<= XFS_IFORK_DSIZE(ip
))
926 *flags
|= XFS_ILOG_DBROOT
;
928 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, XFS_DATA_FORK
);
929 error
= xfs_bmbt_lookup_first(cur
, &stat
);
932 /* must be at least one entry */
933 XFS_WANT_CORRUPTED_GOTO(mp
, stat
== 1, error0
);
934 if ((error
= xfs_btree_new_iroot(cur
, flags
, &stat
)))
937 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
940 cur
->bc_private
.b
.allocated
= 0;
941 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
945 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
950 * Called from xfs_bmap_add_attrfork to handle extents format files.
952 STATIC
int /* error */
953 xfs_bmap_add_attrfork_extents(
954 struct xfs_trans
*tp
, /* transaction pointer */
955 struct xfs_inode
*ip
, /* incore inode pointer */
956 int *flags
) /* inode logging flags */
958 xfs_btree_cur_t
*cur
; /* bmap btree cursor */
959 int error
; /* error return value */
961 if (ip
->i_d
.di_nextents
* sizeof(xfs_bmbt_rec_t
) <= XFS_IFORK_DSIZE(ip
))
964 error
= xfs_bmap_extents_to_btree(tp
, ip
, &cur
, 0, flags
,
967 cur
->bc_private
.b
.allocated
= 0;
968 xfs_btree_del_cursor(cur
, error
);
974 * Called from xfs_bmap_add_attrfork to handle local format files. Each
975 * different data fork content type needs a different callout to do the
976 * conversion. Some are basic and only require special block initialisation
977 * callouts for the data formating, others (directories) are so specialised they
978 * handle everything themselves.
980 * XXX (dgc): investigate whether directory conversion can use the generic
981 * formatting callout. It should be possible - it's just a very complex
984 STATIC
int /* error */
985 xfs_bmap_add_attrfork_local(
986 struct xfs_trans
*tp
, /* transaction pointer */
987 struct xfs_inode
*ip
, /* incore inode pointer */
988 int *flags
) /* inode logging flags */
990 struct xfs_da_args dargs
; /* args for dir/attr code */
992 if (ip
->i_df
.if_bytes
<= XFS_IFORK_DSIZE(ip
))
995 if (S_ISDIR(VFS_I(ip
)->i_mode
)) {
996 memset(&dargs
, 0, sizeof(dargs
));
997 dargs
.geo
= ip
->i_mount
->m_dir_geo
;
999 dargs
.total
= dargs
.geo
->fsbcount
;
1000 dargs
.whichfork
= XFS_DATA_FORK
;
1002 return xfs_dir2_sf_to_block(&dargs
);
1005 if (S_ISLNK(VFS_I(ip
)->i_mode
))
1006 return xfs_bmap_local_to_extents(tp
, ip
, 1, flags
,
1008 xfs_symlink_local_to_remote
);
1010 /* should only be called for types that support local format data */
1012 return -EFSCORRUPTED
;
1015 /* Set an inode attr fork off based on the format */
1017 xfs_bmap_set_attrforkoff(
1018 struct xfs_inode
*ip
,
1022 switch (ip
->i_d
.di_format
) {
1023 case XFS_DINODE_FMT_DEV
:
1024 ip
->i_d
.di_forkoff
= roundup(sizeof(xfs_dev_t
), 8) >> 3;
1026 case XFS_DINODE_FMT_LOCAL
:
1027 case XFS_DINODE_FMT_EXTENTS
:
1028 case XFS_DINODE_FMT_BTREE
:
1029 ip
->i_d
.di_forkoff
= xfs_attr_shortform_bytesfit(ip
, size
);
1030 if (!ip
->i_d
.di_forkoff
)
1031 ip
->i_d
.di_forkoff
= xfs_default_attroffset(ip
) >> 3;
1032 else if ((ip
->i_mount
->m_flags
& XFS_MOUNT_ATTR2
) && version
)
1044 * Convert inode from non-attributed to attributed.
1045 * Must not be in a transaction, ip must not be locked.
1047 int /* error code */
1048 xfs_bmap_add_attrfork(
1049 xfs_inode_t
*ip
, /* incore inode pointer */
1050 int size
, /* space new attribute needs */
1051 int rsvd
) /* xact may use reserved blks */
1053 xfs_mount_t
*mp
; /* mount structure */
1054 xfs_trans_t
*tp
; /* transaction pointer */
1055 int blks
; /* space reservation */
1056 int version
= 1; /* superblock attr version */
1057 int logflags
; /* logging flags */
1058 int error
; /* error return value */
1060 ASSERT(XFS_IFORK_Q(ip
) == 0);
1063 ASSERT(!XFS_NOT_DQATTACHED(mp
, ip
));
1065 blks
= XFS_ADDAFORK_SPACE_RES(mp
);
1067 error
= xfs_trans_alloc(mp
, &M_RES(mp
)->tr_addafork
, blks
, 0,
1068 rsvd
? XFS_TRANS_RESERVE
: 0, &tp
);
1072 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
1073 error
= xfs_trans_reserve_quota_nblks(tp
, ip
, blks
, 0, rsvd
?
1074 XFS_QMOPT_RES_REGBLKS
| XFS_QMOPT_FORCE_RES
:
1075 XFS_QMOPT_RES_REGBLKS
);
1078 if (XFS_IFORK_Q(ip
))
1080 if (ip
->i_d
.di_anextents
!= 0) {
1081 error
= -EFSCORRUPTED
;
1084 if (ip
->i_d
.di_aformat
!= XFS_DINODE_FMT_EXTENTS
) {
1086 * For inodes coming from pre-6.2 filesystems.
1088 ASSERT(ip
->i_d
.di_aformat
== 0);
1089 ip
->i_d
.di_aformat
= XFS_DINODE_FMT_EXTENTS
;
1092 xfs_trans_ijoin(tp
, ip
, 0);
1093 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
1094 error
= xfs_bmap_set_attrforkoff(ip
, size
, &version
);
1097 ASSERT(ip
->i_afp
== NULL
);
1098 ip
->i_afp
= kmem_zone_zalloc(xfs_ifork_zone
, KM_SLEEP
);
1099 ip
->i_afp
->if_flags
= XFS_IFEXTENTS
;
1101 switch (ip
->i_d
.di_format
) {
1102 case XFS_DINODE_FMT_LOCAL
:
1103 error
= xfs_bmap_add_attrfork_local(tp
, ip
, &logflags
);
1105 case XFS_DINODE_FMT_EXTENTS
:
1106 error
= xfs_bmap_add_attrfork_extents(tp
, ip
, &logflags
);
1108 case XFS_DINODE_FMT_BTREE
:
1109 error
= xfs_bmap_add_attrfork_btree(tp
, ip
, &logflags
);
1116 xfs_trans_log_inode(tp
, ip
, logflags
);
1119 if (!xfs_sb_version_hasattr(&mp
->m_sb
) ||
1120 (!xfs_sb_version_hasattr2(&mp
->m_sb
) && version
== 2)) {
1121 bool log_sb
= false;
1123 spin_lock(&mp
->m_sb_lock
);
1124 if (!xfs_sb_version_hasattr(&mp
->m_sb
)) {
1125 xfs_sb_version_addattr(&mp
->m_sb
);
1128 if (!xfs_sb_version_hasattr2(&mp
->m_sb
) && version
== 2) {
1129 xfs_sb_version_addattr2(&mp
->m_sb
);
1132 spin_unlock(&mp
->m_sb_lock
);
1137 error
= xfs_trans_commit(tp
);
1138 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
1142 xfs_trans_cancel(tp
);
1143 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
1148 * Internal and external extent tree search functions.
1152 * Read in extents from a btree-format inode.
1156 struct xfs_trans
*tp
,
1157 struct xfs_inode
*ip
,
1160 struct xfs_mount
*mp
= ip
->i_mount
;
1161 int state
= xfs_bmap_fork_to_state(whichfork
);
1162 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1163 xfs_extnum_t nextents
= XFS_IFORK_NEXTENTS(ip
, whichfork
);
1164 struct xfs_btree_block
*block
= ifp
->if_broot
;
1165 struct xfs_iext_cursor icur
;
1166 struct xfs_bmbt_irec
new;
1174 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
1176 if (unlikely(XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)) {
1177 XFS_ERROR_REPORT(__func__
, XFS_ERRLEVEL_LOW
, mp
);
1178 return -EFSCORRUPTED
;
1182 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
1184 level
= be16_to_cpu(block
->bb_level
);
1186 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, 1, ifp
->if_broot_bytes
);
1187 bno
= be64_to_cpu(*pp
);
1190 * Go down the tree until leaf level is reached, following the first
1191 * pointer (leftmost) at each level.
1193 while (level
-- > 0) {
1194 error
= xfs_btree_read_bufl(mp
, tp
, bno
, 0, &bp
,
1195 XFS_BMAP_BTREE_REF
, &xfs_bmbt_buf_ops
);
1198 block
= XFS_BUF_TO_BLOCK(bp
);
1201 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, mp
->m_bmap_dmxr
[1]);
1202 bno
= be64_to_cpu(*pp
);
1203 XFS_WANT_CORRUPTED_GOTO(mp
,
1204 xfs_verify_fsbno(mp
, bno
), out_brelse
);
1205 xfs_trans_brelse(tp
, bp
);
1209 * Here with bp and block set to the leftmost leaf node in the tree.
1212 xfs_iext_first(ifp
, &icur
);
1215 * Loop over all leaf nodes. Copy information to the extent records.
1218 xfs_bmbt_rec_t
*frp
;
1219 xfs_fsblock_t nextbno
;
1220 xfs_extnum_t num_recs
;
1222 num_recs
= xfs_btree_get_numrecs(block
);
1223 if (unlikely(i
+ num_recs
> nextents
)) {
1224 xfs_warn(ip
->i_mount
,
1225 "corrupt dinode %Lu, (btree extents).",
1226 (unsigned long long) ip
->i_ino
);
1227 xfs_inode_verifier_error(ip
, -EFSCORRUPTED
,
1228 __func__
, block
, sizeof(*block
),
1230 error
= -EFSCORRUPTED
;
1234 * Read-ahead the next leaf block, if any.
1236 nextbno
= be64_to_cpu(block
->bb_u
.l
.bb_rightsib
);
1237 if (nextbno
!= NULLFSBLOCK
)
1238 xfs_btree_reada_bufl(mp
, nextbno
, 1,
1241 * Copy records into the extent records.
1243 frp
= XFS_BMBT_REC_ADDR(mp
, block
, 1);
1244 for (j
= 0; j
< num_recs
; j
++, frp
++, i
++) {
1247 xfs_bmbt_disk_get_all(frp
, &new);
1248 fa
= xfs_bmap_validate_extent(ip
, whichfork
, &new);
1250 error
= -EFSCORRUPTED
;
1251 xfs_inode_verifier_error(ip
, error
,
1252 "xfs_iread_extents(2)",
1253 frp
, sizeof(*frp
), fa
);
1256 xfs_iext_insert(ip
, &icur
, &new, state
);
1257 trace_xfs_read_extent(ip
, &icur
, state
, _THIS_IP_
);
1258 xfs_iext_next(ifp
, &icur
);
1260 xfs_trans_brelse(tp
, bp
);
1263 * If we've reached the end, stop.
1265 if (bno
== NULLFSBLOCK
)
1267 error
= xfs_btree_read_bufl(mp
, tp
, bno
, 0, &bp
,
1268 XFS_BMAP_BTREE_REF
, &xfs_bmbt_buf_ops
);
1271 block
= XFS_BUF_TO_BLOCK(bp
);
1274 if (i
!= XFS_IFORK_NEXTENTS(ip
, whichfork
)) {
1275 error
= -EFSCORRUPTED
;
1278 ASSERT(i
== xfs_iext_count(ifp
));
1280 ifp
->if_flags
|= XFS_IFEXTENTS
;
1284 xfs_trans_brelse(tp
, bp
);
1286 xfs_iext_destroy(ifp
);
1291 * Returns the relative block number of the first unused block(s) in the given
1292 * fork with at least "len" logically contiguous blocks free. This is the
1293 * lowest-address hole if the fork has holes, else the first block past the end
1294 * of fork. Return 0 if the fork is currently local (in-inode).
1297 xfs_bmap_first_unused(
1298 struct xfs_trans
*tp
, /* transaction pointer */
1299 struct xfs_inode
*ip
, /* incore inode */
1300 xfs_extlen_t len
, /* size of hole to find */
1301 xfs_fileoff_t
*first_unused
, /* unused block */
1302 int whichfork
) /* data or attr fork */
1304 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1305 struct xfs_bmbt_irec got
;
1306 struct xfs_iext_cursor icur
;
1307 xfs_fileoff_t lastaddr
= 0;
1308 xfs_fileoff_t lowest
, max
;
1311 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
||
1312 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
||
1313 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
1315 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
) {
1320 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
1321 error
= xfs_iread_extents(tp
, ip
, whichfork
);
1326 lowest
= max
= *first_unused
;
1327 for_each_xfs_iext(ifp
, &icur
, &got
) {
1329 * See if the hole before this extent will work.
1331 if (got
.br_startoff
>= lowest
+ len
&&
1332 got
.br_startoff
- max
>= len
)
1334 lastaddr
= got
.br_startoff
+ got
.br_blockcount
;
1335 max
= XFS_FILEOFF_MAX(lastaddr
, lowest
);
1338 *first_unused
= max
;
1343 * Returns the file-relative block number of the last block - 1 before
1344 * last_block (input value) in the file.
1345 * This is not based on i_size, it is based on the extent records.
1346 * Returns 0 for local files, as they do not have extent records.
1349 xfs_bmap_last_before(
1350 struct xfs_trans
*tp
, /* transaction pointer */
1351 struct xfs_inode
*ip
, /* incore inode */
1352 xfs_fileoff_t
*last_block
, /* last block */
1353 int whichfork
) /* data or attr fork */
1355 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1356 struct xfs_bmbt_irec got
;
1357 struct xfs_iext_cursor icur
;
1360 switch (XFS_IFORK_FORMAT(ip
, whichfork
)) {
1361 case XFS_DINODE_FMT_LOCAL
:
1364 case XFS_DINODE_FMT_BTREE
:
1365 case XFS_DINODE_FMT_EXTENTS
:
1371 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
1372 error
= xfs_iread_extents(tp
, ip
, whichfork
);
1377 if (!xfs_iext_lookup_extent_before(ip
, ifp
, last_block
, &icur
, &got
))
1383 xfs_bmap_last_extent(
1384 struct xfs_trans
*tp
,
1385 struct xfs_inode
*ip
,
1387 struct xfs_bmbt_irec
*rec
,
1390 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1391 struct xfs_iext_cursor icur
;
1394 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
1395 error
= xfs_iread_extents(tp
, ip
, whichfork
);
1400 xfs_iext_last(ifp
, &icur
);
1401 if (!xfs_iext_get_extent(ifp
, &icur
, rec
))
1409 * Check the last inode extent to determine whether this allocation will result
1410 * in blocks being allocated at the end of the file. When we allocate new data
1411 * blocks at the end of the file which do not start at the previous data block,
1412 * we will try to align the new blocks at stripe unit boundaries.
1414 * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be
1415 * at, or past the EOF.
1419 struct xfs_bmalloca
*bma
,
1422 struct xfs_bmbt_irec rec
;
1427 error
= xfs_bmap_last_extent(NULL
, bma
->ip
, whichfork
, &rec
,
1438 * Check if we are allocation or past the last extent, or at least into
1439 * the last delayed allocated extent.
1441 bma
->aeof
= bma
->offset
>= rec
.br_startoff
+ rec
.br_blockcount
||
1442 (bma
->offset
>= rec
.br_startoff
&&
1443 isnullstartblock(rec
.br_startblock
));
1448 * Returns the file-relative block number of the first block past eof in
1449 * the file. This is not based on i_size, it is based on the extent records.
1450 * Returns 0 for local files, as they do not have extent records.
1453 xfs_bmap_last_offset(
1454 struct xfs_inode
*ip
,
1455 xfs_fileoff_t
*last_block
,
1458 struct xfs_bmbt_irec rec
;
1464 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
)
1467 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
&&
1468 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
1471 error
= xfs_bmap_last_extent(NULL
, ip
, whichfork
, &rec
, &is_empty
);
1472 if (error
|| is_empty
)
1475 *last_block
= rec
.br_startoff
+ rec
.br_blockcount
;
1480 * Returns whether the selected fork of the inode has exactly one
1481 * block or not. For the data fork we check this matches di_size,
1482 * implying the file's range is 0..bsize-1.
1484 int /* 1=>1 block, 0=>otherwise */
1486 xfs_inode_t
*ip
, /* incore inode */
1487 int whichfork
) /* data or attr fork */
1489 struct xfs_ifork
*ifp
; /* inode fork pointer */
1490 int rval
; /* return value */
1491 xfs_bmbt_irec_t s
; /* internal version of extent */
1492 struct xfs_iext_cursor icur
;
1495 if (whichfork
== XFS_DATA_FORK
)
1496 return XFS_ISIZE(ip
) == ip
->i_mount
->m_sb
.sb_blocksize
;
1498 if (XFS_IFORK_NEXTENTS(ip
, whichfork
) != 1)
1500 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
1502 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1503 ASSERT(ifp
->if_flags
& XFS_IFEXTENTS
);
1504 xfs_iext_first(ifp
, &icur
);
1505 xfs_iext_get_extent(ifp
, &icur
, &s
);
1506 rval
= s
.br_startoff
== 0 && s
.br_blockcount
== 1;
1507 if (rval
&& whichfork
== XFS_DATA_FORK
)
1508 ASSERT(XFS_ISIZE(ip
) == ip
->i_mount
->m_sb
.sb_blocksize
);
1513 * Extent tree manipulation functions used during allocation.
1517 * Convert a delayed allocation to a real allocation.
1519 STATIC
int /* error */
1520 xfs_bmap_add_extent_delay_real(
1521 struct xfs_bmalloca
*bma
,
1524 struct xfs_bmbt_irec
*new = &bma
->got
;
1525 int error
; /* error return value */
1526 int i
; /* temp state */
1527 struct xfs_ifork
*ifp
; /* inode fork pointer */
1528 xfs_fileoff_t new_endoff
; /* end offset of new entry */
1529 xfs_bmbt_irec_t r
[3]; /* neighbor extent entries */
1530 /* left is 0, right is 1, prev is 2 */
1531 int rval
=0; /* return value (logging flags) */
1532 int state
= xfs_bmap_fork_to_state(whichfork
);
1533 xfs_filblks_t da_new
; /* new count del alloc blocks used */
1534 xfs_filblks_t da_old
; /* old count del alloc blocks used */
1535 xfs_filblks_t temp
=0; /* value for da_new calculations */
1536 int tmp_rval
; /* partial logging flags */
1537 struct xfs_mount
*mp
;
1538 xfs_extnum_t
*nextents
;
1539 struct xfs_bmbt_irec old
;
1541 mp
= bma
->ip
->i_mount
;
1542 ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
1543 ASSERT(whichfork
!= XFS_ATTR_FORK
);
1544 nextents
= (whichfork
== XFS_COW_FORK
? &bma
->ip
->i_cnextents
:
1545 &bma
->ip
->i_d
.di_nextents
);
1547 ASSERT(!isnullstartblock(new->br_startblock
));
1549 (bma
->cur
->bc_private
.b
.flags
& XFS_BTCUR_BPRV_WASDEL
));
1551 XFS_STATS_INC(mp
, xs_add_exlist
);
1558 * Set up a bunch of variables to make the tests simpler.
1560 xfs_iext_get_extent(ifp
, &bma
->icur
, &PREV
);
1561 new_endoff
= new->br_startoff
+ new->br_blockcount
;
1562 ASSERT(isnullstartblock(PREV
.br_startblock
));
1563 ASSERT(PREV
.br_startoff
<= new->br_startoff
);
1564 ASSERT(PREV
.br_startoff
+ PREV
.br_blockcount
>= new_endoff
);
1566 da_old
= startblockval(PREV
.br_startblock
);
1570 * Set flags determining what part of the previous delayed allocation
1571 * extent is being replaced by a real allocation.
1573 if (PREV
.br_startoff
== new->br_startoff
)
1574 state
|= BMAP_LEFT_FILLING
;
1575 if (PREV
.br_startoff
+ PREV
.br_blockcount
== new_endoff
)
1576 state
|= BMAP_RIGHT_FILLING
;
1579 * Check and set flags if this segment has a left neighbor.
1580 * Don't set contiguous if the combined extent would be too large.
1582 if (xfs_iext_peek_prev_extent(ifp
, &bma
->icur
, &LEFT
)) {
1583 state
|= BMAP_LEFT_VALID
;
1584 if (isnullstartblock(LEFT
.br_startblock
))
1585 state
|= BMAP_LEFT_DELAY
;
1588 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
1589 LEFT
.br_startoff
+ LEFT
.br_blockcount
== new->br_startoff
&&
1590 LEFT
.br_startblock
+ LEFT
.br_blockcount
== new->br_startblock
&&
1591 LEFT
.br_state
== new->br_state
&&
1592 LEFT
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
1593 state
|= BMAP_LEFT_CONTIG
;
1596 * Check and set flags if this segment has a right neighbor.
1597 * Don't set contiguous if the combined extent would be too large.
1598 * Also check for all-three-contiguous being too large.
1600 if (xfs_iext_peek_next_extent(ifp
, &bma
->icur
, &RIGHT
)) {
1601 state
|= BMAP_RIGHT_VALID
;
1602 if (isnullstartblock(RIGHT
.br_startblock
))
1603 state
|= BMAP_RIGHT_DELAY
;
1606 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
1607 new_endoff
== RIGHT
.br_startoff
&&
1608 new->br_startblock
+ new->br_blockcount
== RIGHT
.br_startblock
&&
1609 new->br_state
== RIGHT
.br_state
&&
1610 new->br_blockcount
+ RIGHT
.br_blockcount
<= MAXEXTLEN
&&
1611 ((state
& (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
1612 BMAP_RIGHT_FILLING
)) !=
1613 (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
1614 BMAP_RIGHT_FILLING
) ||
1615 LEFT
.br_blockcount
+ new->br_blockcount
+ RIGHT
.br_blockcount
1617 state
|= BMAP_RIGHT_CONTIG
;
1621 * Switch out based on the FILLING and CONTIG state bits.
1623 switch (state
& (BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
1624 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
)) {
1625 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
1626 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
1628 * Filling in all of a previously delayed allocation extent.
1629 * The left and right neighbors are both contiguous with new.
1631 LEFT
.br_blockcount
+= PREV
.br_blockcount
+ RIGHT
.br_blockcount
;
1633 xfs_iext_remove(bma
->ip
, &bma
->icur
, state
);
1634 xfs_iext_remove(bma
->ip
, &bma
->icur
, state
);
1635 xfs_iext_prev(ifp
, &bma
->icur
);
1636 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &LEFT
);
1639 if (bma
->cur
== NULL
)
1640 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1642 rval
= XFS_ILOG_CORE
;
1643 error
= xfs_bmbt_lookup_eq(bma
->cur
, &RIGHT
, &i
);
1646 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1647 error
= xfs_btree_delete(bma
->cur
, &i
);
1650 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1651 error
= xfs_btree_decrement(bma
->cur
, 0, &i
);
1654 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1655 error
= xfs_bmbt_update(bma
->cur
, &LEFT
);
1661 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
1663 * Filling in all of a previously delayed allocation extent.
1664 * The left neighbor is contiguous, the right is not.
1667 LEFT
.br_blockcount
+= PREV
.br_blockcount
;
1669 xfs_iext_remove(bma
->ip
, &bma
->icur
, state
);
1670 xfs_iext_prev(ifp
, &bma
->icur
);
1671 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &LEFT
);
1673 if (bma
->cur
== NULL
)
1674 rval
= XFS_ILOG_DEXT
;
1677 error
= xfs_bmbt_lookup_eq(bma
->cur
, &old
, &i
);
1680 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1681 error
= xfs_bmbt_update(bma
->cur
, &LEFT
);
1687 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
1689 * Filling in all of a previously delayed allocation extent.
1690 * The right neighbor is contiguous, the left is not. Take care
1691 * with delay -> unwritten extent allocation here because the
1692 * delalloc record we are overwriting is always written.
1694 PREV
.br_startblock
= new->br_startblock
;
1695 PREV
.br_blockcount
+= RIGHT
.br_blockcount
;
1696 PREV
.br_state
= new->br_state
;
1698 xfs_iext_next(ifp
, &bma
->icur
);
1699 xfs_iext_remove(bma
->ip
, &bma
->icur
, state
);
1700 xfs_iext_prev(ifp
, &bma
->icur
);
1701 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &PREV
);
1703 if (bma
->cur
== NULL
)
1704 rval
= XFS_ILOG_DEXT
;
1707 error
= xfs_bmbt_lookup_eq(bma
->cur
, &RIGHT
, &i
);
1710 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1711 error
= xfs_bmbt_update(bma
->cur
, &PREV
);
1717 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
1719 * Filling in all of a previously delayed allocation extent.
1720 * Neither the left nor right neighbors are contiguous with
1723 PREV
.br_startblock
= new->br_startblock
;
1724 PREV
.br_state
= new->br_state
;
1725 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &PREV
);
1728 if (bma
->cur
== NULL
)
1729 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1731 rval
= XFS_ILOG_CORE
;
1732 error
= xfs_bmbt_lookup_eq(bma
->cur
, new, &i
);
1735 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
1736 error
= xfs_btree_insert(bma
->cur
, &i
);
1739 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1743 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
:
1745 * Filling in the first part of a previous delayed allocation.
1746 * The left neighbor is contiguous.
1749 temp
= PREV
.br_blockcount
- new->br_blockcount
;
1750 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
1751 startblockval(PREV
.br_startblock
));
1753 LEFT
.br_blockcount
+= new->br_blockcount
;
1755 PREV
.br_blockcount
= temp
;
1756 PREV
.br_startoff
+= new->br_blockcount
;
1757 PREV
.br_startblock
= nullstartblock(da_new
);
1759 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &PREV
);
1760 xfs_iext_prev(ifp
, &bma
->icur
);
1761 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &LEFT
);
1763 if (bma
->cur
== NULL
)
1764 rval
= XFS_ILOG_DEXT
;
1767 error
= xfs_bmbt_lookup_eq(bma
->cur
, &old
, &i
);
1770 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1771 error
= xfs_bmbt_update(bma
->cur
, &LEFT
);
1777 case BMAP_LEFT_FILLING
:
1779 * Filling in the first part of a previous delayed allocation.
1780 * The left neighbor is not contiguous.
1782 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, new);
1784 if (bma
->cur
== NULL
)
1785 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1787 rval
= XFS_ILOG_CORE
;
1788 error
= xfs_bmbt_lookup_eq(bma
->cur
, new, &i
);
1791 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
1792 error
= xfs_btree_insert(bma
->cur
, &i
);
1795 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1798 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
1799 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
1800 &bma
->cur
, 1, &tmp_rval
, whichfork
);
1806 temp
= PREV
.br_blockcount
- new->br_blockcount
;
1807 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
1808 startblockval(PREV
.br_startblock
) -
1809 (bma
->cur
? bma
->cur
->bc_private
.b
.allocated
: 0));
1811 PREV
.br_startoff
= new_endoff
;
1812 PREV
.br_blockcount
= temp
;
1813 PREV
.br_startblock
= nullstartblock(da_new
);
1814 xfs_iext_next(ifp
, &bma
->icur
);
1815 xfs_iext_insert(bma
->ip
, &bma
->icur
, &PREV
, state
);
1816 xfs_iext_prev(ifp
, &bma
->icur
);
1819 case BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
1821 * Filling in the last part of a previous delayed allocation.
1822 * The right neighbor is contiguous with the new allocation.
1825 RIGHT
.br_startoff
= new->br_startoff
;
1826 RIGHT
.br_startblock
= new->br_startblock
;
1827 RIGHT
.br_blockcount
+= new->br_blockcount
;
1829 if (bma
->cur
== NULL
)
1830 rval
= XFS_ILOG_DEXT
;
1833 error
= xfs_bmbt_lookup_eq(bma
->cur
, &old
, &i
);
1836 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1837 error
= xfs_bmbt_update(bma
->cur
, &RIGHT
);
1842 temp
= PREV
.br_blockcount
- new->br_blockcount
;
1843 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
1844 startblockval(PREV
.br_startblock
));
1846 PREV
.br_blockcount
= temp
;
1847 PREV
.br_startblock
= nullstartblock(da_new
);
1849 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &PREV
);
1850 xfs_iext_next(ifp
, &bma
->icur
);
1851 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &RIGHT
);
1854 case BMAP_RIGHT_FILLING
:
1856 * Filling in the last part of a previous delayed allocation.
1857 * The right neighbor is not contiguous.
1859 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, new);
1861 if (bma
->cur
== NULL
)
1862 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1864 rval
= XFS_ILOG_CORE
;
1865 error
= xfs_bmbt_lookup_eq(bma
->cur
, new, &i
);
1868 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
1869 error
= xfs_btree_insert(bma
->cur
, &i
);
1872 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1875 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
1876 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
1877 &bma
->cur
, 1, &tmp_rval
, whichfork
);
1883 temp
= PREV
.br_blockcount
- new->br_blockcount
;
1884 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
1885 startblockval(PREV
.br_startblock
) -
1886 (bma
->cur
? bma
->cur
->bc_private
.b
.allocated
: 0));
1888 PREV
.br_startblock
= nullstartblock(da_new
);
1889 PREV
.br_blockcount
= temp
;
1890 xfs_iext_insert(bma
->ip
, &bma
->icur
, &PREV
, state
);
1891 xfs_iext_next(ifp
, &bma
->icur
);
1896 * Filling in the middle part of a previous delayed allocation.
1897 * Contiguity is impossible here.
1898 * This case is avoided almost all the time.
1900 * We start with a delayed allocation:
1902 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
1905 * and we are allocating:
1906 * +rrrrrrrrrrrrrrrrr+
1909 * and we set it up for insertion as:
1910 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
1912 * PREV @ idx LEFT RIGHT
1913 * inserted at idx + 1
1917 /* LEFT is the new middle */
1920 /* RIGHT is the new right */
1921 RIGHT
.br_state
= PREV
.br_state
;
1922 RIGHT
.br_startoff
= new_endoff
;
1923 RIGHT
.br_blockcount
=
1924 PREV
.br_startoff
+ PREV
.br_blockcount
- new_endoff
;
1925 RIGHT
.br_startblock
=
1926 nullstartblock(xfs_bmap_worst_indlen(bma
->ip
,
1927 RIGHT
.br_blockcount
));
1930 PREV
.br_blockcount
= new->br_startoff
- PREV
.br_startoff
;
1931 PREV
.br_startblock
=
1932 nullstartblock(xfs_bmap_worst_indlen(bma
->ip
,
1933 PREV
.br_blockcount
));
1934 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &PREV
);
1936 xfs_iext_next(ifp
, &bma
->icur
);
1937 xfs_iext_insert(bma
->ip
, &bma
->icur
, &RIGHT
, state
);
1938 xfs_iext_insert(bma
->ip
, &bma
->icur
, &LEFT
, state
);
1941 if (bma
->cur
== NULL
)
1942 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1944 rval
= XFS_ILOG_CORE
;
1945 error
= xfs_bmbt_lookup_eq(bma
->cur
, new, &i
);
1948 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
1949 error
= xfs_btree_insert(bma
->cur
, &i
);
1952 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1955 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
1956 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
1957 &bma
->cur
, 1, &tmp_rval
, whichfork
);
1963 da_new
= startblockval(PREV
.br_startblock
) +
1964 startblockval(RIGHT
.br_startblock
);
1967 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
1968 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
1969 case BMAP_LEFT_FILLING
| BMAP_RIGHT_CONTIG
:
1970 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
1971 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
1972 case BMAP_LEFT_CONTIG
:
1973 case BMAP_RIGHT_CONTIG
:
1975 * These cases are all impossible.
1980 /* add reverse mapping unless caller opted out */
1981 if (!(bma
->flags
& XFS_BMAPI_NORMAP
)) {
1982 error
= xfs_rmap_map_extent(bma
->tp
, bma
->ip
, whichfork
, new);
1987 /* convert to a btree if necessary */
1988 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
1989 int tmp_logflags
; /* partial log flag return val */
1991 ASSERT(bma
->cur
== NULL
);
1992 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
1993 &bma
->cur
, da_old
> 0, &tmp_logflags
,
1995 bma
->logflags
|= tmp_logflags
;
2001 da_new
+= bma
->cur
->bc_private
.b
.allocated
;
2002 bma
->cur
->bc_private
.b
.allocated
= 0;
2005 /* adjust for changes in reserved delayed indirect blocks */
2006 if (da_new
!= da_old
) {
2007 ASSERT(state
== 0 || da_new
< da_old
);
2008 error
= xfs_mod_fdblocks(mp
, (int64_t)(da_old
- da_new
),
2012 xfs_bmap_check_leaf_extents(bma
->cur
, bma
->ip
, whichfork
);
2014 if (whichfork
!= XFS_COW_FORK
)
2015 bma
->logflags
|= rval
;
2023 * Convert an unwritten allocation to a real allocation or vice versa.
2025 STATIC
int /* error */
2026 xfs_bmap_add_extent_unwritten_real(
2027 struct xfs_trans
*tp
,
2028 xfs_inode_t
*ip
, /* incore inode pointer */
2030 struct xfs_iext_cursor
*icur
,
2031 xfs_btree_cur_t
**curp
, /* if *curp is null, not a btree */
2032 xfs_bmbt_irec_t
*new, /* new data to add to file extents */
2033 int *logflagsp
) /* inode logging flags */
2035 xfs_btree_cur_t
*cur
; /* btree cursor */
2036 int error
; /* error return value */
2037 int i
; /* temp state */
2038 struct xfs_ifork
*ifp
; /* inode fork pointer */
2039 xfs_fileoff_t new_endoff
; /* end offset of new entry */
2040 xfs_bmbt_irec_t r
[3]; /* neighbor extent entries */
2041 /* left is 0, right is 1, prev is 2 */
2042 int rval
=0; /* return value (logging flags) */
2043 int state
= xfs_bmap_fork_to_state(whichfork
);
2044 struct xfs_mount
*mp
= ip
->i_mount
;
2045 struct xfs_bmbt_irec old
;
2050 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
2052 ASSERT(!isnullstartblock(new->br_startblock
));
2054 XFS_STATS_INC(mp
, xs_add_exlist
);
2061 * Set up a bunch of variables to make the tests simpler.
2064 xfs_iext_get_extent(ifp
, icur
, &PREV
);
2065 ASSERT(new->br_state
!= PREV
.br_state
);
2066 new_endoff
= new->br_startoff
+ new->br_blockcount
;
2067 ASSERT(PREV
.br_startoff
<= new->br_startoff
);
2068 ASSERT(PREV
.br_startoff
+ PREV
.br_blockcount
>= new_endoff
);
2071 * Set flags determining what part of the previous oldext allocation
2072 * extent is being replaced by a newext allocation.
2074 if (PREV
.br_startoff
== new->br_startoff
)
2075 state
|= BMAP_LEFT_FILLING
;
2076 if (PREV
.br_startoff
+ PREV
.br_blockcount
== new_endoff
)
2077 state
|= BMAP_RIGHT_FILLING
;
2080 * Check and set flags if this segment has a left neighbor.
2081 * Don't set contiguous if the combined extent would be too large.
2083 if (xfs_iext_peek_prev_extent(ifp
, icur
, &LEFT
)) {
2084 state
|= BMAP_LEFT_VALID
;
2085 if (isnullstartblock(LEFT
.br_startblock
))
2086 state
|= BMAP_LEFT_DELAY
;
2089 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
2090 LEFT
.br_startoff
+ LEFT
.br_blockcount
== new->br_startoff
&&
2091 LEFT
.br_startblock
+ LEFT
.br_blockcount
== new->br_startblock
&&
2092 LEFT
.br_state
== new->br_state
&&
2093 LEFT
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
2094 state
|= BMAP_LEFT_CONTIG
;
2097 * Check and set flags if this segment has a right neighbor.
2098 * Don't set contiguous if the combined extent would be too large.
2099 * Also check for all-three-contiguous being too large.
2101 if (xfs_iext_peek_next_extent(ifp
, icur
, &RIGHT
)) {
2102 state
|= BMAP_RIGHT_VALID
;
2103 if (isnullstartblock(RIGHT
.br_startblock
))
2104 state
|= BMAP_RIGHT_DELAY
;
2107 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
2108 new_endoff
== RIGHT
.br_startoff
&&
2109 new->br_startblock
+ new->br_blockcount
== RIGHT
.br_startblock
&&
2110 new->br_state
== RIGHT
.br_state
&&
2111 new->br_blockcount
+ RIGHT
.br_blockcount
<= MAXEXTLEN
&&
2112 ((state
& (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
2113 BMAP_RIGHT_FILLING
)) !=
2114 (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
2115 BMAP_RIGHT_FILLING
) ||
2116 LEFT
.br_blockcount
+ new->br_blockcount
+ RIGHT
.br_blockcount
2118 state
|= BMAP_RIGHT_CONTIG
;
2121 * Switch out based on the FILLING and CONTIG state bits.
2123 switch (state
& (BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
2124 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
)) {
2125 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
2126 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2128 * Setting all of a previous oldext extent to newext.
2129 * The left and right neighbors are both contiguous with new.
2131 LEFT
.br_blockcount
+= PREV
.br_blockcount
+ RIGHT
.br_blockcount
;
2133 xfs_iext_remove(ip
, icur
, state
);
2134 xfs_iext_remove(ip
, icur
, state
);
2135 xfs_iext_prev(ifp
, icur
);
2136 xfs_iext_update_extent(ip
, state
, icur
, &LEFT
);
2137 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2138 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 2);
2140 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2142 rval
= XFS_ILOG_CORE
;
2143 error
= xfs_bmbt_lookup_eq(cur
, &RIGHT
, &i
);
2146 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2147 if ((error
= xfs_btree_delete(cur
, &i
)))
2149 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2150 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2152 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2153 if ((error
= xfs_btree_delete(cur
, &i
)))
2155 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2156 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2158 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2159 error
= xfs_bmbt_update(cur
, &LEFT
);
2165 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
2167 * Setting all of a previous oldext extent to newext.
2168 * The left neighbor is contiguous, the right is not.
2170 LEFT
.br_blockcount
+= PREV
.br_blockcount
;
2172 xfs_iext_remove(ip
, icur
, state
);
2173 xfs_iext_prev(ifp
, icur
);
2174 xfs_iext_update_extent(ip
, state
, icur
, &LEFT
);
2175 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2176 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
2178 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2180 rval
= XFS_ILOG_CORE
;
2181 error
= xfs_bmbt_lookup_eq(cur
, &PREV
, &i
);
2184 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2185 if ((error
= xfs_btree_delete(cur
, &i
)))
2187 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2188 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2190 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2191 error
= xfs_bmbt_update(cur
, &LEFT
);
2197 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2199 * Setting all of a previous oldext extent to newext.
2200 * The right neighbor is contiguous, the left is not.
2202 PREV
.br_blockcount
+= RIGHT
.br_blockcount
;
2203 PREV
.br_state
= new->br_state
;
2205 xfs_iext_next(ifp
, icur
);
2206 xfs_iext_remove(ip
, icur
, state
);
2207 xfs_iext_prev(ifp
, icur
);
2208 xfs_iext_update_extent(ip
, state
, icur
, &PREV
);
2210 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2211 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
2213 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2215 rval
= XFS_ILOG_CORE
;
2216 error
= xfs_bmbt_lookup_eq(cur
, &RIGHT
, &i
);
2219 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2220 if ((error
= xfs_btree_delete(cur
, &i
)))
2222 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2223 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2225 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2226 error
= xfs_bmbt_update(cur
, &PREV
);
2232 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
2234 * Setting all of a previous oldext extent to newext.
2235 * Neither the left nor right neighbors are contiguous with
2238 PREV
.br_state
= new->br_state
;
2239 xfs_iext_update_extent(ip
, state
, icur
, &PREV
);
2242 rval
= XFS_ILOG_DEXT
;
2245 error
= xfs_bmbt_lookup_eq(cur
, new, &i
);
2248 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2249 error
= xfs_bmbt_update(cur
, &PREV
);
2255 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
:
2257 * Setting the first part of a previous oldext extent to newext.
2258 * The left neighbor is contiguous.
2260 LEFT
.br_blockcount
+= new->br_blockcount
;
2263 PREV
.br_startoff
+= new->br_blockcount
;
2264 PREV
.br_startblock
+= new->br_blockcount
;
2265 PREV
.br_blockcount
-= new->br_blockcount
;
2267 xfs_iext_update_extent(ip
, state
, icur
, &PREV
);
2268 xfs_iext_prev(ifp
, icur
);
2269 xfs_iext_update_extent(ip
, state
, icur
, &LEFT
);
2272 rval
= XFS_ILOG_DEXT
;
2275 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2278 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2279 error
= xfs_bmbt_update(cur
, &PREV
);
2282 error
= xfs_btree_decrement(cur
, 0, &i
);
2285 error
= xfs_bmbt_update(cur
, &LEFT
);
2291 case BMAP_LEFT_FILLING
:
2293 * Setting the first part of a previous oldext extent to newext.
2294 * The left neighbor is not contiguous.
2297 PREV
.br_startoff
+= new->br_blockcount
;
2298 PREV
.br_startblock
+= new->br_blockcount
;
2299 PREV
.br_blockcount
-= new->br_blockcount
;
2301 xfs_iext_update_extent(ip
, state
, icur
, &PREV
);
2302 xfs_iext_insert(ip
, icur
, new, state
);
2303 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2304 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
2306 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2308 rval
= XFS_ILOG_CORE
;
2309 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2312 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2313 error
= xfs_bmbt_update(cur
, &PREV
);
2316 cur
->bc_rec
.b
= *new;
2317 if ((error
= xfs_btree_insert(cur
, &i
)))
2319 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2323 case BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2325 * Setting the last part of a previous oldext extent to newext.
2326 * The right neighbor is contiguous with the new allocation.
2329 PREV
.br_blockcount
-= new->br_blockcount
;
2331 RIGHT
.br_startoff
= new->br_startoff
;
2332 RIGHT
.br_startblock
= new->br_startblock
;
2333 RIGHT
.br_blockcount
+= new->br_blockcount
;
2335 xfs_iext_update_extent(ip
, state
, icur
, &PREV
);
2336 xfs_iext_next(ifp
, icur
);
2337 xfs_iext_update_extent(ip
, state
, icur
, &RIGHT
);
2340 rval
= XFS_ILOG_DEXT
;
2343 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2346 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2347 error
= xfs_bmbt_update(cur
, &PREV
);
2350 error
= xfs_btree_increment(cur
, 0, &i
);
2353 error
= xfs_bmbt_update(cur
, &RIGHT
);
2359 case BMAP_RIGHT_FILLING
:
2361 * Setting the last part of a previous oldext extent to newext.
2362 * The right neighbor is not contiguous.
2365 PREV
.br_blockcount
-= new->br_blockcount
;
2367 xfs_iext_update_extent(ip
, state
, icur
, &PREV
);
2368 xfs_iext_next(ifp
, icur
);
2369 xfs_iext_insert(ip
, icur
, new, state
);
2371 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2372 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
2374 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2376 rval
= XFS_ILOG_CORE
;
2377 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2380 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2381 error
= xfs_bmbt_update(cur
, &PREV
);
2384 error
= xfs_bmbt_lookup_eq(cur
, new, &i
);
2387 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
2388 if ((error
= xfs_btree_insert(cur
, &i
)))
2390 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2396 * Setting the middle part of a previous oldext extent to
2397 * newext. Contiguity is impossible here.
2398 * One extent becomes three extents.
2401 PREV
.br_blockcount
= new->br_startoff
- PREV
.br_startoff
;
2404 r
[1].br_startoff
= new_endoff
;
2405 r
[1].br_blockcount
=
2406 old
.br_startoff
+ old
.br_blockcount
- new_endoff
;
2407 r
[1].br_startblock
= new->br_startblock
+ new->br_blockcount
;
2408 r
[1].br_state
= PREV
.br_state
;
2410 xfs_iext_update_extent(ip
, state
, icur
, &PREV
);
2411 xfs_iext_next(ifp
, icur
);
2412 xfs_iext_insert(ip
, icur
, &r
[1], state
);
2413 xfs_iext_insert(ip
, icur
, &r
[0], state
);
2415 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2416 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 2);
2418 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2420 rval
= XFS_ILOG_CORE
;
2421 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2424 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2425 /* new right extent - oldext */
2426 error
= xfs_bmbt_update(cur
, &r
[1]);
2429 /* new left extent - oldext */
2430 cur
->bc_rec
.b
= PREV
;
2431 if ((error
= xfs_btree_insert(cur
, &i
)))
2433 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2435 * Reset the cursor to the position of the new extent
2436 * we are about to insert as we can't trust it after
2437 * the previous insert.
2439 error
= xfs_bmbt_lookup_eq(cur
, new, &i
);
2442 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
2443 /* new middle extent - newext */
2444 if ((error
= xfs_btree_insert(cur
, &i
)))
2446 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2450 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2451 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2452 case BMAP_LEFT_FILLING
| BMAP_RIGHT_CONTIG
:
2453 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
2454 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2455 case BMAP_LEFT_CONTIG
:
2456 case BMAP_RIGHT_CONTIG
:
2458 * These cases are all impossible.
2463 /* update reverse mappings */
2464 error
= xfs_rmap_convert_extent(mp
, tp
, ip
, whichfork
, new);
2468 /* convert to a btree if necessary */
2469 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
2470 int tmp_logflags
; /* partial log flag return val */
2472 ASSERT(cur
== NULL
);
2473 error
= xfs_bmap_extents_to_btree(tp
, ip
, &cur
, 0,
2474 &tmp_logflags
, whichfork
);
2475 *logflagsp
|= tmp_logflags
;
2480 /* clear out the allocated field, done with it now in any case. */
2482 cur
->bc_private
.b
.allocated
= 0;
2486 xfs_bmap_check_leaf_extents(*curp
, ip
, whichfork
);
2496 * Convert a hole to a delayed allocation.
2499 xfs_bmap_add_extent_hole_delay(
2500 xfs_inode_t
*ip
, /* incore inode pointer */
2502 struct xfs_iext_cursor
*icur
,
2503 xfs_bmbt_irec_t
*new) /* new data to add to file extents */
2505 struct xfs_ifork
*ifp
; /* inode fork pointer */
2506 xfs_bmbt_irec_t left
; /* left neighbor extent entry */
2507 xfs_filblks_t newlen
=0; /* new indirect size */
2508 xfs_filblks_t oldlen
=0; /* old indirect size */
2509 xfs_bmbt_irec_t right
; /* right neighbor extent entry */
2510 int state
= xfs_bmap_fork_to_state(whichfork
);
2511 xfs_filblks_t temp
; /* temp for indirect calculations */
2513 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
2514 ASSERT(isnullstartblock(new->br_startblock
));
2517 * Check and set flags if this segment has a left neighbor
2519 if (xfs_iext_peek_prev_extent(ifp
, icur
, &left
)) {
2520 state
|= BMAP_LEFT_VALID
;
2521 if (isnullstartblock(left
.br_startblock
))
2522 state
|= BMAP_LEFT_DELAY
;
2526 * Check and set flags if the current (right) segment exists.
2527 * If it doesn't exist, we're converting the hole at end-of-file.
2529 if (xfs_iext_get_extent(ifp
, icur
, &right
)) {
2530 state
|= BMAP_RIGHT_VALID
;
2531 if (isnullstartblock(right
.br_startblock
))
2532 state
|= BMAP_RIGHT_DELAY
;
2536 * Set contiguity flags on the left and right neighbors.
2537 * Don't let extents get too large, even if the pieces are contiguous.
2539 if ((state
& BMAP_LEFT_VALID
) && (state
& BMAP_LEFT_DELAY
) &&
2540 left
.br_startoff
+ left
.br_blockcount
== new->br_startoff
&&
2541 left
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
2542 state
|= BMAP_LEFT_CONTIG
;
2544 if ((state
& BMAP_RIGHT_VALID
) && (state
& BMAP_RIGHT_DELAY
) &&
2545 new->br_startoff
+ new->br_blockcount
== right
.br_startoff
&&
2546 new->br_blockcount
+ right
.br_blockcount
<= MAXEXTLEN
&&
2547 (!(state
& BMAP_LEFT_CONTIG
) ||
2548 (left
.br_blockcount
+ new->br_blockcount
+
2549 right
.br_blockcount
<= MAXEXTLEN
)))
2550 state
|= BMAP_RIGHT_CONTIG
;
2553 * Switch out based on the contiguity flags.
2555 switch (state
& (BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
)) {
2556 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2558 * New allocation is contiguous with delayed allocations
2559 * on the left and on the right.
2560 * Merge all three into a single extent record.
2562 temp
= left
.br_blockcount
+ new->br_blockcount
+
2563 right
.br_blockcount
;
2565 oldlen
= startblockval(left
.br_startblock
) +
2566 startblockval(new->br_startblock
) +
2567 startblockval(right
.br_startblock
);
2568 newlen
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
2570 left
.br_startblock
= nullstartblock(newlen
);
2571 left
.br_blockcount
= temp
;
2573 xfs_iext_remove(ip
, icur
, state
);
2574 xfs_iext_prev(ifp
, icur
);
2575 xfs_iext_update_extent(ip
, state
, icur
, &left
);
2578 case BMAP_LEFT_CONTIG
:
2580 * New allocation is contiguous with a delayed allocation
2582 * Merge the new allocation with the left neighbor.
2584 temp
= left
.br_blockcount
+ new->br_blockcount
;
2586 oldlen
= startblockval(left
.br_startblock
) +
2587 startblockval(new->br_startblock
);
2588 newlen
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
2590 left
.br_blockcount
= temp
;
2591 left
.br_startblock
= nullstartblock(newlen
);
2593 xfs_iext_prev(ifp
, icur
);
2594 xfs_iext_update_extent(ip
, state
, icur
, &left
);
2597 case BMAP_RIGHT_CONTIG
:
2599 * New allocation is contiguous with a delayed allocation
2601 * Merge the new allocation with the right neighbor.
2603 temp
= new->br_blockcount
+ right
.br_blockcount
;
2604 oldlen
= startblockval(new->br_startblock
) +
2605 startblockval(right
.br_startblock
);
2606 newlen
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
2608 right
.br_startoff
= new->br_startoff
;
2609 right
.br_startblock
= nullstartblock(newlen
);
2610 right
.br_blockcount
= temp
;
2611 xfs_iext_update_extent(ip
, state
, icur
, &right
);
2616 * New allocation is not contiguous with another
2617 * delayed allocation.
2618 * Insert a new entry.
2620 oldlen
= newlen
= 0;
2621 xfs_iext_insert(ip
, icur
, new, state
);
2624 if (oldlen
!= newlen
) {
2625 ASSERT(oldlen
> newlen
);
2626 xfs_mod_fdblocks(ip
->i_mount
, (int64_t)(oldlen
- newlen
),
2629 * Nothing to do for disk quota accounting here.
2635 * Convert a hole to a real allocation.
2637 STATIC
int /* error */
2638 xfs_bmap_add_extent_hole_real(
2639 struct xfs_trans
*tp
,
2640 struct xfs_inode
*ip
,
2642 struct xfs_iext_cursor
*icur
,
2643 struct xfs_btree_cur
**curp
,
2644 struct xfs_bmbt_irec
*new,
2648 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
2649 struct xfs_mount
*mp
= ip
->i_mount
;
2650 struct xfs_btree_cur
*cur
= *curp
;
2651 int error
; /* error return value */
2652 int i
; /* temp state */
2653 xfs_bmbt_irec_t left
; /* left neighbor extent entry */
2654 xfs_bmbt_irec_t right
; /* right neighbor extent entry */
2655 int rval
=0; /* return value (logging flags) */
2656 int state
= xfs_bmap_fork_to_state(whichfork
);
2657 struct xfs_bmbt_irec old
;
2659 ASSERT(!isnullstartblock(new->br_startblock
));
2660 ASSERT(!cur
|| !(cur
->bc_private
.b
.flags
& XFS_BTCUR_BPRV_WASDEL
));
2662 XFS_STATS_INC(mp
, xs_add_exlist
);
2665 * Check and set flags if this segment has a left neighbor.
2667 if (xfs_iext_peek_prev_extent(ifp
, icur
, &left
)) {
2668 state
|= BMAP_LEFT_VALID
;
2669 if (isnullstartblock(left
.br_startblock
))
2670 state
|= BMAP_LEFT_DELAY
;
2674 * Check and set flags if this segment has a current value.
2675 * Not true if we're inserting into the "hole" at eof.
2677 if (xfs_iext_get_extent(ifp
, icur
, &right
)) {
2678 state
|= BMAP_RIGHT_VALID
;
2679 if (isnullstartblock(right
.br_startblock
))
2680 state
|= BMAP_RIGHT_DELAY
;
2684 * We're inserting a real allocation between "left" and "right".
2685 * Set the contiguity flags. Don't let extents get too large.
2687 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
2688 left
.br_startoff
+ left
.br_blockcount
== new->br_startoff
&&
2689 left
.br_startblock
+ left
.br_blockcount
== new->br_startblock
&&
2690 left
.br_state
== new->br_state
&&
2691 left
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
2692 state
|= BMAP_LEFT_CONTIG
;
2694 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
2695 new->br_startoff
+ new->br_blockcount
== right
.br_startoff
&&
2696 new->br_startblock
+ new->br_blockcount
== right
.br_startblock
&&
2697 new->br_state
== right
.br_state
&&
2698 new->br_blockcount
+ right
.br_blockcount
<= MAXEXTLEN
&&
2699 (!(state
& BMAP_LEFT_CONTIG
) ||
2700 left
.br_blockcount
+ new->br_blockcount
+
2701 right
.br_blockcount
<= MAXEXTLEN
))
2702 state
|= BMAP_RIGHT_CONTIG
;
2706 * Select which case we're in here, and implement it.
2708 switch (state
& (BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
)) {
2709 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2711 * New allocation is contiguous with real allocations on the
2712 * left and on the right.
2713 * Merge all three into a single extent record.
2715 left
.br_blockcount
+= new->br_blockcount
+ right
.br_blockcount
;
2717 xfs_iext_remove(ip
, icur
, state
);
2718 xfs_iext_prev(ifp
, icur
);
2719 xfs_iext_update_extent(ip
, state
, icur
, &left
);
2721 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2722 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
2724 rval
= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
2726 rval
= XFS_ILOG_CORE
;
2727 error
= xfs_bmbt_lookup_eq(cur
, &right
, &i
);
2730 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2731 error
= xfs_btree_delete(cur
, &i
);
2734 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2735 error
= xfs_btree_decrement(cur
, 0, &i
);
2738 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2739 error
= xfs_bmbt_update(cur
, &left
);
2745 case BMAP_LEFT_CONTIG
:
2747 * New allocation is contiguous with a real allocation
2749 * Merge the new allocation with the left neighbor.
2752 left
.br_blockcount
+= new->br_blockcount
;
2754 xfs_iext_prev(ifp
, icur
);
2755 xfs_iext_update_extent(ip
, state
, icur
, &left
);
2758 rval
= xfs_ilog_fext(whichfork
);
2761 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2764 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2765 error
= xfs_bmbt_update(cur
, &left
);
2771 case BMAP_RIGHT_CONTIG
:
2773 * New allocation is contiguous with a real allocation
2775 * Merge the new allocation with the right neighbor.
2779 right
.br_startoff
= new->br_startoff
;
2780 right
.br_startblock
= new->br_startblock
;
2781 right
.br_blockcount
+= new->br_blockcount
;
2782 xfs_iext_update_extent(ip
, state
, icur
, &right
);
2785 rval
= xfs_ilog_fext(whichfork
);
2788 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2791 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2792 error
= xfs_bmbt_update(cur
, &right
);
2800 * New allocation is not contiguous with another
2802 * Insert a new entry.
2804 xfs_iext_insert(ip
, icur
, new, state
);
2805 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2806 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
2808 rval
= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
2810 rval
= XFS_ILOG_CORE
;
2811 error
= xfs_bmbt_lookup_eq(cur
, new, &i
);
2814 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
2815 error
= xfs_btree_insert(cur
, &i
);
2818 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2823 /* add reverse mapping unless caller opted out */
2824 if (!(flags
& XFS_BMAPI_NORMAP
)) {
2825 error
= xfs_rmap_map_extent(tp
, ip
, whichfork
, new);
2830 /* convert to a btree if necessary */
2831 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
2832 int tmp_logflags
; /* partial log flag return val */
2834 ASSERT(cur
== NULL
);
2835 error
= xfs_bmap_extents_to_btree(tp
, ip
, curp
, 0,
2836 &tmp_logflags
, whichfork
);
2837 *logflagsp
|= tmp_logflags
;
2843 /* clear out the allocated field, done with it now in any case. */
2845 cur
->bc_private
.b
.allocated
= 0;
2847 xfs_bmap_check_leaf_extents(cur
, ip
, whichfork
);
2854 * Functions used in the extent read, allocate and remove paths
2858 * Adjust the size of the new extent based on di_extsize and rt extsize.
2861 xfs_bmap_extsize_align(
2863 xfs_bmbt_irec_t
*gotp
, /* next extent pointer */
2864 xfs_bmbt_irec_t
*prevp
, /* previous extent pointer */
2865 xfs_extlen_t extsz
, /* align to this extent size */
2866 int rt
, /* is this a realtime inode? */
2867 int eof
, /* is extent at end-of-file? */
2868 int delay
, /* creating delalloc extent? */
2869 int convert
, /* overwriting unwritten extent? */
2870 xfs_fileoff_t
*offp
, /* in/out: aligned offset */
2871 xfs_extlen_t
*lenp
) /* in/out: aligned length */
2873 xfs_fileoff_t orig_off
; /* original offset */
2874 xfs_extlen_t orig_alen
; /* original length */
2875 xfs_fileoff_t orig_end
; /* original off+len */
2876 xfs_fileoff_t nexto
; /* next file offset */
2877 xfs_fileoff_t prevo
; /* previous file offset */
2878 xfs_fileoff_t align_off
; /* temp for offset */
2879 xfs_extlen_t align_alen
; /* temp for length */
2880 xfs_extlen_t temp
; /* temp for calculations */
2885 orig_off
= align_off
= *offp
;
2886 orig_alen
= align_alen
= *lenp
;
2887 orig_end
= orig_off
+ orig_alen
;
2890 * If this request overlaps an existing extent, then don't
2891 * attempt to perform any additional alignment.
2893 if (!delay
&& !eof
&&
2894 (orig_off
>= gotp
->br_startoff
) &&
2895 (orig_end
<= gotp
->br_startoff
+ gotp
->br_blockcount
)) {
2900 * If the file offset is unaligned vs. the extent size
2901 * we need to align it. This will be possible unless
2902 * the file was previously written with a kernel that didn't
2903 * perform this alignment, or if a truncate shot us in the
2906 div_u64_rem(orig_off
, extsz
, &temp
);
2912 /* Same adjustment for the end of the requested area. */
2913 temp
= (align_alen
% extsz
);
2915 align_alen
+= extsz
- temp
;
2918 * For large extent hint sizes, the aligned extent might be larger than
2919 * MAXEXTLEN. In that case, reduce the size by an extsz so that it pulls
2920 * the length back under MAXEXTLEN. The outer allocation loops handle
2921 * short allocation just fine, so it is safe to do this. We only want to
2922 * do it when we are forced to, though, because it means more allocation
2923 * operations are required.
2925 while (align_alen
> MAXEXTLEN
)
2926 align_alen
-= extsz
;
2927 ASSERT(align_alen
<= MAXEXTLEN
);
2930 * If the previous block overlaps with this proposed allocation
2931 * then move the start forward without adjusting the length.
2933 if (prevp
->br_startoff
!= NULLFILEOFF
) {
2934 if (prevp
->br_startblock
== HOLESTARTBLOCK
)
2935 prevo
= prevp
->br_startoff
;
2937 prevo
= prevp
->br_startoff
+ prevp
->br_blockcount
;
2940 if (align_off
!= orig_off
&& align_off
< prevo
)
2943 * If the next block overlaps with this proposed allocation
2944 * then move the start back without adjusting the length,
2945 * but not before offset 0.
2946 * This may of course make the start overlap previous block,
2947 * and if we hit the offset 0 limit then the next block
2948 * can still overlap too.
2950 if (!eof
&& gotp
->br_startoff
!= NULLFILEOFF
) {
2951 if ((delay
&& gotp
->br_startblock
== HOLESTARTBLOCK
) ||
2952 (!delay
&& gotp
->br_startblock
== DELAYSTARTBLOCK
))
2953 nexto
= gotp
->br_startoff
+ gotp
->br_blockcount
;
2955 nexto
= gotp
->br_startoff
;
2957 nexto
= NULLFILEOFF
;
2959 align_off
+ align_alen
!= orig_end
&&
2960 align_off
+ align_alen
> nexto
)
2961 align_off
= nexto
> align_alen
? nexto
- align_alen
: 0;
2963 * If we're now overlapping the next or previous extent that
2964 * means we can't fit an extsz piece in this hole. Just move
2965 * the start forward to the first valid spot and set
2966 * the length so we hit the end.
2968 if (align_off
!= orig_off
&& align_off
< prevo
)
2970 if (align_off
+ align_alen
!= orig_end
&&
2971 align_off
+ align_alen
> nexto
&&
2972 nexto
!= NULLFILEOFF
) {
2973 ASSERT(nexto
> prevo
);
2974 align_alen
= nexto
- align_off
;
2978 * If realtime, and the result isn't a multiple of the realtime
2979 * extent size we need to remove blocks until it is.
2981 if (rt
&& (temp
= (align_alen
% mp
->m_sb
.sb_rextsize
))) {
2983 * We're not covering the original request, or
2984 * we won't be able to once we fix the length.
2986 if (orig_off
< align_off
||
2987 orig_end
> align_off
+ align_alen
||
2988 align_alen
- temp
< orig_alen
)
2991 * Try to fix it by moving the start up.
2993 if (align_off
+ temp
<= orig_off
) {
2998 * Try to fix it by moving the end in.
3000 else if (align_off
+ align_alen
- temp
>= orig_end
)
3003 * Set the start to the minimum then trim the length.
3006 align_alen
-= orig_off
- align_off
;
3007 align_off
= orig_off
;
3008 align_alen
-= align_alen
% mp
->m_sb
.sb_rextsize
;
3011 * Result doesn't cover the request, fail it.
3013 if (orig_off
< align_off
|| orig_end
> align_off
+ align_alen
)
3016 ASSERT(orig_off
>= align_off
);
3017 /* see MAXEXTLEN handling above */
3018 ASSERT(orig_end
<= align_off
+ align_alen
||
3019 align_alen
+ extsz
> MAXEXTLEN
);
3023 if (!eof
&& gotp
->br_startoff
!= NULLFILEOFF
)
3024 ASSERT(align_off
+ align_alen
<= gotp
->br_startoff
);
3025 if (prevp
->br_startoff
!= NULLFILEOFF
)
3026 ASSERT(align_off
>= prevp
->br_startoff
+ prevp
->br_blockcount
);
3034 #define XFS_ALLOC_GAP_UNITS 4
3038 struct xfs_bmalloca
*ap
) /* bmap alloc argument struct */
3040 xfs_fsblock_t adjust
; /* adjustment to block numbers */
3041 xfs_agnumber_t fb_agno
; /* ag number of ap->firstblock */
3042 xfs_mount_t
*mp
; /* mount point structure */
3043 int nullfb
; /* true if ap->firstblock isn't set */
3044 int rt
; /* true if inode is realtime */
3046 #define ISVALID(x,y) \
3048 (x) < mp->m_sb.sb_rblocks : \
3049 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
3050 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
3051 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
3053 mp
= ap
->ip
->i_mount
;
3054 nullfb
= ap
->tp
->t_firstblock
== NULLFSBLOCK
;
3055 rt
= XFS_IS_REALTIME_INODE(ap
->ip
) &&
3056 xfs_alloc_is_userdata(ap
->datatype
);
3057 fb_agno
= nullfb
? NULLAGNUMBER
: XFS_FSB_TO_AGNO(mp
,
3058 ap
->tp
->t_firstblock
);
3060 * If allocating at eof, and there's a previous real block,
3061 * try to use its last block as our starting point.
3063 if (ap
->eof
&& ap
->prev
.br_startoff
!= NULLFILEOFF
&&
3064 !isnullstartblock(ap
->prev
.br_startblock
) &&
3065 ISVALID(ap
->prev
.br_startblock
+ ap
->prev
.br_blockcount
,
3066 ap
->prev
.br_startblock
)) {
3067 ap
->blkno
= ap
->prev
.br_startblock
+ ap
->prev
.br_blockcount
;
3069 * Adjust for the gap between prevp and us.
3071 adjust
= ap
->offset
-
3072 (ap
->prev
.br_startoff
+ ap
->prev
.br_blockcount
);
3074 ISVALID(ap
->blkno
+ adjust
, ap
->prev
.br_startblock
))
3075 ap
->blkno
+= adjust
;
3078 * If not at eof, then compare the two neighbor blocks.
3079 * Figure out whether either one gives us a good starting point,
3080 * and pick the better one.
3082 else if (!ap
->eof
) {
3083 xfs_fsblock_t gotbno
; /* right side block number */
3084 xfs_fsblock_t gotdiff
=0; /* right side difference */
3085 xfs_fsblock_t prevbno
; /* left side block number */
3086 xfs_fsblock_t prevdiff
=0; /* left side difference */
3089 * If there's a previous (left) block, select a requested
3090 * start block based on it.
3092 if (ap
->prev
.br_startoff
!= NULLFILEOFF
&&
3093 !isnullstartblock(ap
->prev
.br_startblock
) &&
3094 (prevbno
= ap
->prev
.br_startblock
+
3095 ap
->prev
.br_blockcount
) &&
3096 ISVALID(prevbno
, ap
->prev
.br_startblock
)) {
3098 * Calculate gap to end of previous block.
3100 adjust
= prevdiff
= ap
->offset
-
3101 (ap
->prev
.br_startoff
+
3102 ap
->prev
.br_blockcount
);
3104 * Figure the startblock based on the previous block's
3105 * end and the gap size.
3107 * If the gap is large relative to the piece we're
3108 * allocating, or using it gives us an invalid block
3109 * number, then just use the end of the previous block.
3111 if (prevdiff
<= XFS_ALLOC_GAP_UNITS
* ap
->length
&&
3112 ISVALID(prevbno
+ prevdiff
,
3113 ap
->prev
.br_startblock
))
3118 * If the firstblock forbids it, can't use it,
3121 if (!rt
&& !nullfb
&&
3122 XFS_FSB_TO_AGNO(mp
, prevbno
) != fb_agno
)
3123 prevbno
= NULLFSBLOCK
;
3126 * No previous block or can't follow it, just default.
3129 prevbno
= NULLFSBLOCK
;
3131 * If there's a following (right) block, select a requested
3132 * start block based on it.
3134 if (!isnullstartblock(ap
->got
.br_startblock
)) {
3136 * Calculate gap to start of next block.
3138 adjust
= gotdiff
= ap
->got
.br_startoff
- ap
->offset
;
3140 * Figure the startblock based on the next block's
3141 * start and the gap size.
3143 gotbno
= ap
->got
.br_startblock
;
3146 * If the gap is large relative to the piece we're
3147 * allocating, or using it gives us an invalid block
3148 * number, then just use the start of the next block
3149 * offset by our length.
3151 if (gotdiff
<= XFS_ALLOC_GAP_UNITS
* ap
->length
&&
3152 ISVALID(gotbno
- gotdiff
, gotbno
))
3154 else if (ISVALID(gotbno
- ap
->length
, gotbno
)) {
3155 gotbno
-= ap
->length
;
3156 gotdiff
+= adjust
- ap
->length
;
3160 * If the firstblock forbids it, can't use it,
3163 if (!rt
&& !nullfb
&&
3164 XFS_FSB_TO_AGNO(mp
, gotbno
) != fb_agno
)
3165 gotbno
= NULLFSBLOCK
;
3168 * No next block, just default.
3171 gotbno
= NULLFSBLOCK
;
3173 * If both valid, pick the better one, else the only good
3174 * one, else ap->blkno is already set (to 0 or the inode block).
3176 if (prevbno
!= NULLFSBLOCK
&& gotbno
!= NULLFSBLOCK
)
3177 ap
->blkno
= prevdiff
<= gotdiff
? prevbno
: gotbno
;
3178 else if (prevbno
!= NULLFSBLOCK
)
3179 ap
->blkno
= prevbno
;
3180 else if (gotbno
!= NULLFSBLOCK
)
3187 xfs_bmap_longest_free_extent(
3188 struct xfs_trans
*tp
,
3193 struct xfs_mount
*mp
= tp
->t_mountp
;
3194 struct xfs_perag
*pag
;
3195 xfs_extlen_t longest
;
3198 pag
= xfs_perag_get(mp
, ag
);
3199 if (!pag
->pagf_init
) {
3200 error
= xfs_alloc_pagf_init(mp
, tp
, ag
, XFS_ALLOC_FLAG_TRYLOCK
);
3204 if (!pag
->pagf_init
) {
3210 longest
= xfs_alloc_longest_free_extent(pag
,
3211 xfs_alloc_min_freelist(mp
, pag
),
3212 xfs_ag_resv_needed(pag
, XFS_AG_RESV_NONE
));
3213 if (*blen
< longest
)
3222 xfs_bmap_select_minlen(
3223 struct xfs_bmalloca
*ap
,
3224 struct xfs_alloc_arg
*args
,
3228 if (notinit
|| *blen
< ap
->minlen
) {
3230 * Since we did a BUF_TRYLOCK above, it is possible that
3231 * there is space for this request.
3233 args
->minlen
= ap
->minlen
;
3234 } else if (*blen
< args
->maxlen
) {
3236 * If the best seen length is less than the request length,
3237 * use the best as the minimum.
3239 args
->minlen
= *blen
;
3242 * Otherwise we've seen an extent as big as maxlen, use that
3245 args
->minlen
= args
->maxlen
;
3250 xfs_bmap_btalloc_nullfb(
3251 struct xfs_bmalloca
*ap
,
3252 struct xfs_alloc_arg
*args
,
3255 struct xfs_mount
*mp
= ap
->ip
->i_mount
;
3256 xfs_agnumber_t ag
, startag
;
3260 args
->type
= XFS_ALLOCTYPE_START_BNO
;
3261 args
->total
= ap
->total
;
3263 startag
= ag
= XFS_FSB_TO_AGNO(mp
, args
->fsbno
);
3264 if (startag
== NULLAGNUMBER
)
3267 while (*blen
< args
->maxlen
) {
3268 error
= xfs_bmap_longest_free_extent(args
->tp
, ag
, blen
,
3273 if (++ag
== mp
->m_sb
.sb_agcount
)
3279 xfs_bmap_select_minlen(ap
, args
, blen
, notinit
);
3284 xfs_bmap_btalloc_filestreams(
3285 struct xfs_bmalloca
*ap
,
3286 struct xfs_alloc_arg
*args
,
3289 struct xfs_mount
*mp
= ap
->ip
->i_mount
;
3294 args
->type
= XFS_ALLOCTYPE_NEAR_BNO
;
3295 args
->total
= ap
->total
;
3297 ag
= XFS_FSB_TO_AGNO(mp
, args
->fsbno
);
3298 if (ag
== NULLAGNUMBER
)
3301 error
= xfs_bmap_longest_free_extent(args
->tp
, ag
, blen
, ¬init
);
3305 if (*blen
< args
->maxlen
) {
3306 error
= xfs_filestream_new_ag(ap
, &ag
);
3310 error
= xfs_bmap_longest_free_extent(args
->tp
, ag
, blen
,
3317 xfs_bmap_select_minlen(ap
, args
, blen
, notinit
);
3320 * Set the failure fallback case to look in the selected AG as stream
3323 ap
->blkno
= args
->fsbno
= XFS_AGB_TO_FSB(mp
, ag
, 0);
3327 /* Update all inode and quota accounting for the allocation we just did. */
3329 xfs_bmap_btalloc_accounting(
3330 struct xfs_bmalloca
*ap
,
3331 struct xfs_alloc_arg
*args
)
3333 if (ap
->flags
& XFS_BMAPI_COWFORK
) {
3335 * COW fork blocks are in-core only and thus are treated as
3336 * in-core quota reservation (like delalloc blocks) even when
3337 * converted to real blocks. The quota reservation is not
3338 * accounted to disk until blocks are remapped to the data
3339 * fork. So if these blocks were previously delalloc, we
3340 * already have quota reservation and there's nothing to do
3347 * Otherwise, we've allocated blocks in a hole. The transaction
3348 * has acquired in-core quota reservation for this extent.
3349 * Rather than account these as real blocks, however, we reduce
3350 * the transaction quota reservation based on the allocation.
3351 * This essentially transfers the transaction quota reservation
3352 * to that of a delalloc extent.
3354 ap
->ip
->i_delayed_blks
+= args
->len
;
3355 xfs_trans_mod_dquot_byino(ap
->tp
, ap
->ip
, XFS_TRANS_DQ_RES_BLKS
,
3360 /* data/attr fork only */
3361 ap
->ip
->i_d
.di_nblocks
+= args
->len
;
3362 xfs_trans_log_inode(ap
->tp
, ap
->ip
, XFS_ILOG_CORE
);
3364 ap
->ip
->i_delayed_blks
-= args
->len
;
3365 xfs_trans_mod_dquot_byino(ap
->tp
, ap
->ip
,
3366 ap
->wasdel
? XFS_TRANS_DQ_DELBCOUNT
: XFS_TRANS_DQ_BCOUNT
,
3372 struct xfs_bmalloca
*ap
) /* bmap alloc argument struct */
3374 xfs_mount_t
*mp
; /* mount point structure */
3375 xfs_alloctype_t atype
= 0; /* type for allocation routines */
3376 xfs_extlen_t align
= 0; /* minimum allocation alignment */
3377 xfs_agnumber_t fb_agno
; /* ag number of ap->firstblock */
3379 xfs_alloc_arg_t args
;
3380 xfs_fileoff_t orig_offset
;
3381 xfs_extlen_t orig_length
;
3383 xfs_extlen_t nextminlen
= 0;
3384 int nullfb
; /* true if ap->firstblock isn't set */
3391 orig_offset
= ap
->offset
;
3392 orig_length
= ap
->length
;
3394 mp
= ap
->ip
->i_mount
;
3396 /* stripe alignment for allocation is determined by mount parameters */
3398 if (mp
->m_swidth
&& (mp
->m_flags
& XFS_MOUNT_SWALLOC
))
3399 stripe_align
= mp
->m_swidth
;
3400 else if (mp
->m_dalign
)
3401 stripe_align
= mp
->m_dalign
;
3403 if (ap
->flags
& XFS_BMAPI_COWFORK
)
3404 align
= xfs_get_cowextsz_hint(ap
->ip
);
3405 else if (xfs_alloc_is_userdata(ap
->datatype
))
3406 align
= xfs_get_extsz_hint(ap
->ip
);
3408 error
= xfs_bmap_extsize_align(mp
, &ap
->got
, &ap
->prev
,
3409 align
, 0, ap
->eof
, 0, ap
->conv
,
3410 &ap
->offset
, &ap
->length
);
3416 nullfb
= ap
->tp
->t_firstblock
== NULLFSBLOCK
;
3417 fb_agno
= nullfb
? NULLAGNUMBER
: XFS_FSB_TO_AGNO(mp
,
3418 ap
->tp
->t_firstblock
);
3420 if (xfs_alloc_is_userdata(ap
->datatype
) &&
3421 xfs_inode_is_filestream(ap
->ip
)) {
3422 ag
= xfs_filestream_lookup_ag(ap
->ip
);
3423 ag
= (ag
!= NULLAGNUMBER
) ? ag
: 0;
3424 ap
->blkno
= XFS_AGB_TO_FSB(mp
, ag
, 0);
3426 ap
->blkno
= XFS_INO_TO_FSB(mp
, ap
->ip
->i_ino
);
3429 ap
->blkno
= ap
->tp
->t_firstblock
;
3431 xfs_bmap_adjacent(ap
);
3434 * If allowed, use ap->blkno; otherwise must use firstblock since
3435 * it's in the right allocation group.
3437 if (nullfb
|| XFS_FSB_TO_AGNO(mp
, ap
->blkno
) == fb_agno
)
3440 ap
->blkno
= ap
->tp
->t_firstblock
;
3442 * Normal allocation, done through xfs_alloc_vextent.
3444 tryagain
= isaligned
= 0;
3445 memset(&args
, 0, sizeof(args
));
3448 args
.fsbno
= ap
->blkno
;
3449 args
.oinfo
= XFS_RMAP_OINFO_SKIP_UPDATE
;
3451 /* Trim the allocation back to the maximum an AG can fit. */
3452 args
.maxlen
= min(ap
->length
, mp
->m_ag_max_usable
);
3456 * Search for an allocation group with a single extent large
3457 * enough for the request. If one isn't found, then adjust
3458 * the minimum allocation size to the largest space found.
3460 if (xfs_alloc_is_userdata(ap
->datatype
) &&
3461 xfs_inode_is_filestream(ap
->ip
))
3462 error
= xfs_bmap_btalloc_filestreams(ap
, &args
, &blen
);
3464 error
= xfs_bmap_btalloc_nullfb(ap
, &args
, &blen
);
3467 } else if (ap
->tp
->t_flags
& XFS_TRANS_LOWMODE
) {
3468 if (xfs_inode_is_filestream(ap
->ip
))
3469 args
.type
= XFS_ALLOCTYPE_FIRST_AG
;
3471 args
.type
= XFS_ALLOCTYPE_START_BNO
;
3472 args
.total
= args
.minlen
= ap
->minlen
;
3474 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
3475 args
.total
= ap
->total
;
3476 args
.minlen
= ap
->minlen
;
3478 /* apply extent size hints if obtained earlier */
3481 div_u64_rem(ap
->offset
, args
.prod
, &args
.mod
);
3483 args
.mod
= args
.prod
- args
.mod
;
3484 } else if (mp
->m_sb
.sb_blocksize
>= PAGE_SIZE
) {
3488 args
.prod
= PAGE_SIZE
>> mp
->m_sb
.sb_blocklog
;
3489 div_u64_rem(ap
->offset
, args
.prod
, &args
.mod
);
3491 args
.mod
= args
.prod
- args
.mod
;
3494 * If we are not low on available data blocks, and the
3495 * underlying logical volume manager is a stripe, and
3496 * the file offset is zero then try to allocate data
3497 * blocks on stripe unit boundary.
3498 * NOTE: ap->aeof is only set if the allocation length
3499 * is >= the stripe unit and the allocation offset is
3500 * at the end of file.
3502 if (!(ap
->tp
->t_flags
& XFS_TRANS_LOWMODE
) && ap
->aeof
) {
3504 args
.alignment
= stripe_align
;
3508 * Adjust for alignment
3510 if (blen
> args
.alignment
&& blen
<= args
.maxlen
)
3511 args
.minlen
= blen
- args
.alignment
;
3512 args
.minalignslop
= 0;
3515 * First try an exact bno allocation.
3516 * If it fails then do a near or start bno
3517 * allocation with alignment turned on.
3521 args
.type
= XFS_ALLOCTYPE_THIS_BNO
;
3524 * Compute the minlen+alignment for the
3525 * next case. Set slop so that the value
3526 * of minlen+alignment+slop doesn't go up
3527 * between the calls.
3529 if (blen
> stripe_align
&& blen
<= args
.maxlen
)
3530 nextminlen
= blen
- stripe_align
;
3532 nextminlen
= args
.minlen
;
3533 if (nextminlen
+ stripe_align
> args
.minlen
+ 1)
3535 nextminlen
+ stripe_align
-
3538 args
.minalignslop
= 0;
3542 args
.minalignslop
= 0;
3544 args
.minleft
= ap
->minleft
;
3545 args
.wasdel
= ap
->wasdel
;
3546 args
.resv
= XFS_AG_RESV_NONE
;
3547 args
.datatype
= ap
->datatype
;
3548 if (ap
->datatype
& XFS_ALLOC_USERDATA_ZERO
)
3551 error
= xfs_alloc_vextent(&args
);
3555 if (tryagain
&& args
.fsbno
== NULLFSBLOCK
) {
3557 * Exact allocation failed. Now try with alignment
3561 args
.fsbno
= ap
->blkno
;
3562 args
.alignment
= stripe_align
;
3563 args
.minlen
= nextminlen
;
3564 args
.minalignslop
= 0;
3566 if ((error
= xfs_alloc_vextent(&args
)))
3569 if (isaligned
&& args
.fsbno
== NULLFSBLOCK
) {
3571 * allocation failed, so turn off alignment and
3575 args
.fsbno
= ap
->blkno
;
3577 if ((error
= xfs_alloc_vextent(&args
)))
3580 if (args
.fsbno
== NULLFSBLOCK
&& nullfb
&&
3581 args
.minlen
> ap
->minlen
) {
3582 args
.minlen
= ap
->minlen
;
3583 args
.type
= XFS_ALLOCTYPE_START_BNO
;
3584 args
.fsbno
= ap
->blkno
;
3585 if ((error
= xfs_alloc_vextent(&args
)))
3588 if (args
.fsbno
== NULLFSBLOCK
&& nullfb
) {
3590 args
.type
= XFS_ALLOCTYPE_FIRST_AG
;
3591 args
.total
= ap
->minlen
;
3592 if ((error
= xfs_alloc_vextent(&args
)))
3594 ap
->tp
->t_flags
|= XFS_TRANS_LOWMODE
;
3596 if (args
.fsbno
!= NULLFSBLOCK
) {
3598 * check the allocation happened at the same or higher AG than
3599 * the first block that was allocated.
3601 ASSERT(ap
->tp
->t_firstblock
== NULLFSBLOCK
||
3602 XFS_FSB_TO_AGNO(mp
, ap
->tp
->t_firstblock
) <=
3603 XFS_FSB_TO_AGNO(mp
, args
.fsbno
));
3605 ap
->blkno
= args
.fsbno
;
3606 if (ap
->tp
->t_firstblock
== NULLFSBLOCK
)
3607 ap
->tp
->t_firstblock
= args
.fsbno
;
3608 ASSERT(nullfb
|| fb_agno
<= args
.agno
);
3609 ap
->length
= args
.len
;
3611 * If the extent size hint is active, we tried to round the
3612 * caller's allocation request offset down to extsz and the
3613 * length up to another extsz boundary. If we found a free
3614 * extent we mapped it in starting at this new offset. If the
3615 * newly mapped space isn't long enough to cover any of the
3616 * range of offsets that was originally requested, move the
3617 * mapping up so that we can fill as much of the caller's
3618 * original request as possible. Free space is apparently
3619 * very fragmented so we're unlikely to be able to satisfy the
3622 if (ap
->length
<= orig_length
)
3623 ap
->offset
= orig_offset
;
3624 else if (ap
->offset
+ ap
->length
< orig_offset
+ orig_length
)
3625 ap
->offset
= orig_offset
+ orig_length
- ap
->length
;
3626 xfs_bmap_btalloc_accounting(ap
, &args
);
3628 ap
->blkno
= NULLFSBLOCK
;
3635 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
3636 * It figures out where to ask the underlying allocator to put the new extent.
3640 struct xfs_bmalloca
*ap
) /* bmap alloc argument struct */
3642 if (XFS_IS_REALTIME_INODE(ap
->ip
) &&
3643 xfs_alloc_is_userdata(ap
->datatype
))
3644 return xfs_bmap_rtalloc(ap
);
3645 return xfs_bmap_btalloc(ap
);
3648 /* Trim extent to fit a logical block range. */
3651 struct xfs_bmbt_irec
*irec
,
3655 xfs_fileoff_t distance
;
3656 xfs_fileoff_t end
= bno
+ len
;
3658 if (irec
->br_startoff
+ irec
->br_blockcount
<= bno
||
3659 irec
->br_startoff
>= end
) {
3660 irec
->br_blockcount
= 0;
3664 if (irec
->br_startoff
< bno
) {
3665 distance
= bno
- irec
->br_startoff
;
3666 if (isnullstartblock(irec
->br_startblock
))
3667 irec
->br_startblock
= DELAYSTARTBLOCK
;
3668 if (irec
->br_startblock
!= DELAYSTARTBLOCK
&&
3669 irec
->br_startblock
!= HOLESTARTBLOCK
)
3670 irec
->br_startblock
+= distance
;
3671 irec
->br_startoff
+= distance
;
3672 irec
->br_blockcount
-= distance
;
3675 if (end
< irec
->br_startoff
+ irec
->br_blockcount
) {
3676 distance
= irec
->br_startoff
+ irec
->br_blockcount
- end
;
3677 irec
->br_blockcount
-= distance
;
3682 * Trim the returned map to the required bounds
3686 struct xfs_bmbt_irec
*mval
,
3687 struct xfs_bmbt_irec
*got
,
3695 if ((flags
& XFS_BMAPI_ENTIRE
) ||
3696 got
->br_startoff
+ got
->br_blockcount
<= obno
) {
3698 if (isnullstartblock(got
->br_startblock
))
3699 mval
->br_startblock
= DELAYSTARTBLOCK
;
3705 ASSERT((*bno
>= obno
) || (n
== 0));
3707 mval
->br_startoff
= *bno
;
3708 if (isnullstartblock(got
->br_startblock
))
3709 mval
->br_startblock
= DELAYSTARTBLOCK
;
3711 mval
->br_startblock
= got
->br_startblock
+
3712 (*bno
- got
->br_startoff
);
3714 * Return the minimum of what we got and what we asked for for
3715 * the length. We can use the len variable here because it is
3716 * modified below and we could have been there before coming
3717 * here if the first part of the allocation didn't overlap what
3720 mval
->br_blockcount
= XFS_FILBLKS_MIN(end
- *bno
,
3721 got
->br_blockcount
- (*bno
- got
->br_startoff
));
3722 mval
->br_state
= got
->br_state
;
3723 ASSERT(mval
->br_blockcount
<= len
);
3728 * Update and validate the extent map to return
3731 xfs_bmapi_update_map(
3732 struct xfs_bmbt_irec
**map
,
3740 xfs_bmbt_irec_t
*mval
= *map
;
3742 ASSERT((flags
& XFS_BMAPI_ENTIRE
) ||
3743 ((mval
->br_startoff
+ mval
->br_blockcount
) <= end
));
3744 ASSERT((flags
& XFS_BMAPI_ENTIRE
) || (mval
->br_blockcount
<= *len
) ||
3745 (mval
->br_startoff
< obno
));
3747 *bno
= mval
->br_startoff
+ mval
->br_blockcount
;
3749 if (*n
> 0 && mval
->br_startoff
== mval
[-1].br_startoff
) {
3750 /* update previous map with new information */
3751 ASSERT(mval
->br_startblock
== mval
[-1].br_startblock
);
3752 ASSERT(mval
->br_blockcount
> mval
[-1].br_blockcount
);
3753 ASSERT(mval
->br_state
== mval
[-1].br_state
);
3754 mval
[-1].br_blockcount
= mval
->br_blockcount
;
3755 mval
[-1].br_state
= mval
->br_state
;
3756 } else if (*n
> 0 && mval
->br_startblock
!= DELAYSTARTBLOCK
&&
3757 mval
[-1].br_startblock
!= DELAYSTARTBLOCK
&&
3758 mval
[-1].br_startblock
!= HOLESTARTBLOCK
&&
3759 mval
->br_startblock
== mval
[-1].br_startblock
+
3760 mval
[-1].br_blockcount
&&
3761 mval
[-1].br_state
== mval
->br_state
) {
3762 ASSERT(mval
->br_startoff
==
3763 mval
[-1].br_startoff
+ mval
[-1].br_blockcount
);
3764 mval
[-1].br_blockcount
+= mval
->br_blockcount
;
3765 } else if (*n
> 0 &&
3766 mval
->br_startblock
== DELAYSTARTBLOCK
&&
3767 mval
[-1].br_startblock
== DELAYSTARTBLOCK
&&
3768 mval
->br_startoff
==
3769 mval
[-1].br_startoff
+ mval
[-1].br_blockcount
) {
3770 mval
[-1].br_blockcount
+= mval
->br_blockcount
;
3771 mval
[-1].br_state
= mval
->br_state
;
3772 } else if (!((*n
== 0) &&
3773 ((mval
->br_startoff
+ mval
->br_blockcount
) <=
3782 * Map file blocks to filesystem blocks without allocation.
3786 struct xfs_inode
*ip
,
3789 struct xfs_bmbt_irec
*mval
,
3793 struct xfs_mount
*mp
= ip
->i_mount
;
3794 struct xfs_ifork
*ifp
;
3795 struct xfs_bmbt_irec got
;
3798 struct xfs_iext_cursor icur
;
3802 int whichfork
= xfs_bmapi_whichfork(flags
);
3805 ASSERT(!(flags
& ~(XFS_BMAPI_ATTRFORK
|XFS_BMAPI_ENTIRE
|
3806 XFS_BMAPI_COWFORK
)));
3807 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_SHARED
|XFS_ILOCK_EXCL
));
3809 if (unlikely(XFS_TEST_ERROR(
3810 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
3811 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
3812 mp
, XFS_ERRTAG_BMAPIFORMAT
))) {
3813 XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW
, mp
);
3814 return -EFSCORRUPTED
;
3817 if (XFS_FORCED_SHUTDOWN(mp
))
3820 XFS_STATS_INC(mp
, xs_blk_mapr
);
3822 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
3824 /* No CoW fork? Return a hole. */
3825 if (whichfork
== XFS_COW_FORK
&& !ifp
) {
3826 mval
->br_startoff
= bno
;
3827 mval
->br_startblock
= HOLESTARTBLOCK
;
3828 mval
->br_blockcount
= len
;
3829 mval
->br_state
= XFS_EXT_NORM
;
3834 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
3835 error
= xfs_iread_extents(NULL
, ip
, whichfork
);
3840 if (!xfs_iext_lookup_extent(ip
, ifp
, bno
, &icur
, &got
))
3845 while (bno
< end
&& n
< *nmap
) {
3846 /* Reading past eof, act as though there's a hole up to end. */
3848 got
.br_startoff
= end
;
3849 if (got
.br_startoff
> bno
) {
3850 /* Reading in a hole. */
3851 mval
->br_startoff
= bno
;
3852 mval
->br_startblock
= HOLESTARTBLOCK
;
3853 mval
->br_blockcount
=
3854 XFS_FILBLKS_MIN(len
, got
.br_startoff
- bno
);
3855 mval
->br_state
= XFS_EXT_NORM
;
3856 bno
+= mval
->br_blockcount
;
3857 len
-= mval
->br_blockcount
;
3863 /* set up the extent map to return. */
3864 xfs_bmapi_trim_map(mval
, &got
, &bno
, len
, obno
, end
, n
, flags
);
3865 xfs_bmapi_update_map(&mval
, &bno
, &len
, obno
, end
, &n
, flags
);
3867 /* If we're done, stop now. */
3868 if (bno
>= end
|| n
>= *nmap
)
3871 /* Else go on to the next record. */
3872 if (!xfs_iext_next_extent(ifp
, &icur
, &got
))
3880 * Add a delayed allocation extent to an inode. Blocks are reserved from the
3881 * global pool and the extent inserted into the inode in-core extent tree.
3883 * On entry, got refers to the first extent beyond the offset of the extent to
3884 * allocate or eof is specified if no such extent exists. On return, got refers
3885 * to the extent record that was inserted to the inode fork.
3887 * Note that the allocated extent may have been merged with contiguous extents
3888 * during insertion into the inode fork. Thus, got does not reflect the current
3889 * state of the inode fork on return. If necessary, the caller can use lastx to
3890 * look up the updated record in the inode fork.
3893 xfs_bmapi_reserve_delalloc(
3894 struct xfs_inode
*ip
,
3898 xfs_filblks_t prealloc
,
3899 struct xfs_bmbt_irec
*got
,
3900 struct xfs_iext_cursor
*icur
,
3903 struct xfs_mount
*mp
= ip
->i_mount
;
3904 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
3906 xfs_extlen_t indlen
;
3908 xfs_fileoff_t aoff
= off
;
3911 * Cap the alloc length. Keep track of prealloc so we know whether to
3912 * tag the inode before we return.
3914 alen
= XFS_FILBLKS_MIN(len
+ prealloc
, MAXEXTLEN
);
3916 alen
= XFS_FILBLKS_MIN(alen
, got
->br_startoff
- aoff
);
3917 if (prealloc
&& alen
>= len
)
3918 prealloc
= alen
- len
;
3920 /* Figure out the extent size, adjust alen */
3921 if (whichfork
== XFS_COW_FORK
) {
3922 struct xfs_bmbt_irec prev
;
3923 xfs_extlen_t extsz
= xfs_get_cowextsz_hint(ip
);
3925 if (!xfs_iext_peek_prev_extent(ifp
, icur
, &prev
))
3926 prev
.br_startoff
= NULLFILEOFF
;
3928 error
= xfs_bmap_extsize_align(mp
, got
, &prev
, extsz
, 0, eof
,
3929 1, 0, &aoff
, &alen
);
3934 * Make a transaction-less quota reservation for delayed allocation
3935 * blocks. This number gets adjusted later. We return if we haven't
3936 * allocated blocks already inside this loop.
3938 error
= xfs_trans_reserve_quota_nblks(NULL
, ip
, (long)alen
, 0,
3939 XFS_QMOPT_RES_REGBLKS
);
3944 * Split changing sb for alen and indlen since they could be coming
3945 * from different places.
3947 indlen
= (xfs_extlen_t
)xfs_bmap_worst_indlen(ip
, alen
);
3950 error
= xfs_mod_fdblocks(mp
, -((int64_t)alen
), false);
3952 goto out_unreserve_quota
;
3954 error
= xfs_mod_fdblocks(mp
, -((int64_t)indlen
), false);
3956 goto out_unreserve_blocks
;
3959 ip
->i_delayed_blks
+= alen
;
3961 got
->br_startoff
= aoff
;
3962 got
->br_startblock
= nullstartblock(indlen
);
3963 got
->br_blockcount
= alen
;
3964 got
->br_state
= XFS_EXT_NORM
;
3966 xfs_bmap_add_extent_hole_delay(ip
, whichfork
, icur
, got
);
3969 * Tag the inode if blocks were preallocated. Note that COW fork
3970 * preallocation can occur at the start or end of the extent, even when
3971 * prealloc == 0, so we must also check the aligned offset and length.
3973 if (whichfork
== XFS_DATA_FORK
&& prealloc
)
3974 xfs_inode_set_eofblocks_tag(ip
);
3975 if (whichfork
== XFS_COW_FORK
&& (prealloc
|| aoff
< off
|| alen
> len
))
3976 xfs_inode_set_cowblocks_tag(ip
);
3980 out_unreserve_blocks
:
3981 xfs_mod_fdblocks(mp
, alen
, false);
3982 out_unreserve_quota
:
3983 if (XFS_IS_QUOTA_ON(mp
))
3984 xfs_trans_unreserve_quota_nblks(NULL
, ip
, (long)alen
, 0,
3985 XFS_QMOPT_RES_REGBLKS
);
3991 struct xfs_bmalloca
*bma
)
3993 struct xfs_mount
*mp
= bma
->ip
->i_mount
;
3994 int whichfork
= xfs_bmapi_whichfork(bma
->flags
);
3995 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
3996 int tmp_logflags
= 0;
3999 ASSERT(bma
->length
> 0);
4002 * For the wasdelay case, we could also just allocate the stuff asked
4003 * for in this bmap call but that wouldn't be as good.
4006 bma
->length
= (xfs_extlen_t
)bma
->got
.br_blockcount
;
4007 bma
->offset
= bma
->got
.br_startoff
;
4008 xfs_iext_peek_prev_extent(ifp
, &bma
->icur
, &bma
->prev
);
4010 bma
->length
= XFS_FILBLKS_MIN(bma
->length
, MAXEXTLEN
);
4012 bma
->length
= XFS_FILBLKS_MIN(bma
->length
,
4013 bma
->got
.br_startoff
- bma
->offset
);
4017 * Set the data type being allocated. For the data fork, the first data
4018 * in the file is treated differently to all other allocations. For the
4019 * attribute fork, we only need to ensure the allocated range is not on
4022 if (!(bma
->flags
& XFS_BMAPI_METADATA
)) {
4023 bma
->datatype
= XFS_ALLOC_NOBUSY
;
4024 if (whichfork
== XFS_DATA_FORK
) {
4025 if (bma
->offset
== 0)
4026 bma
->datatype
|= XFS_ALLOC_INITIAL_USER_DATA
;
4028 bma
->datatype
|= XFS_ALLOC_USERDATA
;
4030 if (bma
->flags
& XFS_BMAPI_ZERO
)
4031 bma
->datatype
|= XFS_ALLOC_USERDATA_ZERO
;
4034 bma
->minlen
= (bma
->flags
& XFS_BMAPI_CONTIG
) ? bma
->length
: 1;
4037 * Only want to do the alignment at the eof if it is userdata and
4038 * allocation length is larger than a stripe unit.
4040 if (mp
->m_dalign
&& bma
->length
>= mp
->m_dalign
&&
4041 !(bma
->flags
& XFS_BMAPI_METADATA
) && whichfork
== XFS_DATA_FORK
) {
4042 error
= xfs_bmap_isaeof(bma
, whichfork
);
4047 error
= xfs_bmap_alloc(bma
);
4051 if (bma
->blkno
== NULLFSBLOCK
)
4053 if ((ifp
->if_flags
& XFS_IFBROOT
) && !bma
->cur
)
4054 bma
->cur
= xfs_bmbt_init_cursor(mp
, bma
->tp
, bma
->ip
, whichfork
);
4056 * Bump the number of extents we've allocated
4062 bma
->cur
->bc_private
.b
.flags
=
4063 bma
->wasdel
? XFS_BTCUR_BPRV_WASDEL
: 0;
4065 bma
->got
.br_startoff
= bma
->offset
;
4066 bma
->got
.br_startblock
= bma
->blkno
;
4067 bma
->got
.br_blockcount
= bma
->length
;
4068 bma
->got
.br_state
= XFS_EXT_NORM
;
4071 * In the data fork, a wasdelay extent has been initialized, so
4072 * shouldn't be flagged as unwritten.
4074 * For the cow fork, however, we convert delalloc reservations
4075 * (extents allocated for speculative preallocation) to
4076 * allocated unwritten extents, and only convert the unwritten
4077 * extents to real extents when we're about to write the data.
4079 if ((!bma
->wasdel
|| (bma
->flags
& XFS_BMAPI_COWFORK
)) &&
4080 (bma
->flags
& XFS_BMAPI_PREALLOC
))
4081 bma
->got
.br_state
= XFS_EXT_UNWRITTEN
;
4084 error
= xfs_bmap_add_extent_delay_real(bma
, whichfork
);
4086 error
= xfs_bmap_add_extent_hole_real(bma
->tp
, bma
->ip
,
4087 whichfork
, &bma
->icur
, &bma
->cur
, &bma
->got
,
4088 &bma
->logflags
, bma
->flags
);
4090 bma
->logflags
|= tmp_logflags
;
4095 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4096 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4097 * the neighbouring ones.
4099 xfs_iext_get_extent(ifp
, &bma
->icur
, &bma
->got
);
4101 ASSERT(bma
->got
.br_startoff
<= bma
->offset
);
4102 ASSERT(bma
->got
.br_startoff
+ bma
->got
.br_blockcount
>=
4103 bma
->offset
+ bma
->length
);
4104 ASSERT(bma
->got
.br_state
== XFS_EXT_NORM
||
4105 bma
->got
.br_state
== XFS_EXT_UNWRITTEN
);
4110 xfs_bmapi_convert_unwritten(
4111 struct xfs_bmalloca
*bma
,
4112 struct xfs_bmbt_irec
*mval
,
4116 int whichfork
= xfs_bmapi_whichfork(flags
);
4117 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
4118 int tmp_logflags
= 0;
4121 /* check if we need to do unwritten->real conversion */
4122 if (mval
->br_state
== XFS_EXT_UNWRITTEN
&&
4123 (flags
& XFS_BMAPI_PREALLOC
))
4126 /* check if we need to do real->unwritten conversion */
4127 if (mval
->br_state
== XFS_EXT_NORM
&&
4128 (flags
& (XFS_BMAPI_PREALLOC
| XFS_BMAPI_CONVERT
)) !=
4129 (XFS_BMAPI_PREALLOC
| XFS_BMAPI_CONVERT
))
4133 * Modify (by adding) the state flag, if writing.
4135 ASSERT(mval
->br_blockcount
<= len
);
4136 if ((ifp
->if_flags
& XFS_IFBROOT
) && !bma
->cur
) {
4137 bma
->cur
= xfs_bmbt_init_cursor(bma
->ip
->i_mount
, bma
->tp
,
4138 bma
->ip
, whichfork
);
4140 mval
->br_state
= (mval
->br_state
== XFS_EXT_UNWRITTEN
)
4141 ? XFS_EXT_NORM
: XFS_EXT_UNWRITTEN
;
4144 * Before insertion into the bmbt, zero the range being converted
4147 if (flags
& XFS_BMAPI_ZERO
) {
4148 error
= xfs_zero_extent(bma
->ip
, mval
->br_startblock
,
4149 mval
->br_blockcount
);
4154 error
= xfs_bmap_add_extent_unwritten_real(bma
->tp
, bma
->ip
, whichfork
,
4155 &bma
->icur
, &bma
->cur
, mval
, &tmp_logflags
);
4157 * Log the inode core unconditionally in the unwritten extent conversion
4158 * path because the conversion might not have done so (e.g., if the
4159 * extent count hasn't changed). We need to make sure the inode is dirty
4160 * in the transaction for the sake of fsync(), even if nothing has
4161 * changed, because fsync() will not force the log for this transaction
4162 * unless it sees the inode pinned.
4164 * Note: If we're only converting cow fork extents, there aren't
4165 * any on-disk updates to make, so we don't need to log anything.
4167 if (whichfork
!= XFS_COW_FORK
)
4168 bma
->logflags
|= tmp_logflags
| XFS_ILOG_CORE
;
4173 * Update our extent pointer, given that
4174 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4175 * of the neighbouring ones.
4177 xfs_iext_get_extent(ifp
, &bma
->icur
, &bma
->got
);
4180 * We may have combined previously unwritten space with written space,
4181 * so generate another request.
4183 if (mval
->br_blockcount
< len
)
4188 static inline xfs_extlen_t
4190 struct xfs_trans
*tp
,
4191 struct xfs_inode
*ip
,
4194 if (tp
&& tp
->t_firstblock
!= NULLFSBLOCK
)
4196 if (XFS_IFORK_FORMAT(ip
, fork
) != XFS_DINODE_FMT_BTREE
)
4198 return be16_to_cpu(XFS_IFORK_PTR(ip
, fork
)->if_broot
->bb_level
) + 1;
4202 * Log whatever the flags say, even if error. Otherwise we might miss detecting
4203 * a case where the data is changed, there's an error, and it's not logged so we
4204 * don't shutdown when we should. Don't bother logging extents/btree changes if
4205 * we converted to the other format.
4209 struct xfs_bmalloca
*bma
,
4213 if ((bma
->logflags
& xfs_ilog_fext(whichfork
)) &&
4214 XFS_IFORK_FORMAT(bma
->ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
4215 bma
->logflags
&= ~xfs_ilog_fext(whichfork
);
4216 else if ((bma
->logflags
& xfs_ilog_fbroot(whichfork
)) &&
4217 XFS_IFORK_FORMAT(bma
->ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)
4218 bma
->logflags
&= ~xfs_ilog_fbroot(whichfork
);
4221 xfs_trans_log_inode(bma
->tp
, bma
->ip
, bma
->logflags
);
4223 xfs_btree_del_cursor(bma
->cur
, error
);
4227 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4228 * extent state if necessary. Details behaviour is controlled by the flags
4229 * parameter. Only allocates blocks from a single allocation group, to avoid
4234 struct xfs_trans
*tp
, /* transaction pointer */
4235 struct xfs_inode
*ip
, /* incore inode */
4236 xfs_fileoff_t bno
, /* starting file offs. mapped */
4237 xfs_filblks_t len
, /* length to map in file */
4238 int flags
, /* XFS_BMAPI_... */
4239 xfs_extlen_t total
, /* total blocks needed */
4240 struct xfs_bmbt_irec
*mval
, /* output: map values */
4241 int *nmap
) /* i/o: mval size/count */
4243 struct xfs_mount
*mp
= ip
->i_mount
;
4244 struct xfs_ifork
*ifp
;
4245 struct xfs_bmalloca bma
= { NULL
}; /* args for xfs_bmap_alloc */
4246 xfs_fileoff_t end
; /* end of mapped file region */
4247 bool eof
= false; /* after the end of extents */
4248 int error
; /* error return */
4249 int n
; /* current extent index */
4250 xfs_fileoff_t obno
; /* old block number (offset) */
4251 int whichfork
; /* data or attr fork */
4254 xfs_fileoff_t orig_bno
; /* original block number value */
4255 int orig_flags
; /* original flags arg value */
4256 xfs_filblks_t orig_len
; /* original value of len arg */
4257 struct xfs_bmbt_irec
*orig_mval
; /* original value of mval */
4258 int orig_nmap
; /* original value of *nmap */
4266 whichfork
= xfs_bmapi_whichfork(flags
);
4269 ASSERT(*nmap
<= XFS_BMAP_MAX_NMAP
);
4270 ASSERT(tp
!= NULL
||
4271 (flags
& (XFS_BMAPI_CONVERT
| XFS_BMAPI_COWFORK
)) ==
4272 (XFS_BMAPI_CONVERT
| XFS_BMAPI_COWFORK
));
4274 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_LOCAL
);
4275 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
4276 ASSERT(!(flags
& XFS_BMAPI_REMAP
));
4278 /* zeroing is for currently only for data extents, not metadata */
4279 ASSERT((flags
& (XFS_BMAPI_METADATA
| XFS_BMAPI_ZERO
)) !=
4280 (XFS_BMAPI_METADATA
| XFS_BMAPI_ZERO
));
4282 * we can allocate unwritten extents or pre-zero allocated blocks,
4283 * but it makes no sense to do both at once. This would result in
4284 * zeroing the unwritten extent twice, but it still being an
4285 * unwritten extent....
4287 ASSERT((flags
& (XFS_BMAPI_PREALLOC
| XFS_BMAPI_ZERO
)) !=
4288 (XFS_BMAPI_PREALLOC
| XFS_BMAPI_ZERO
));
4290 if (unlikely(XFS_TEST_ERROR(
4291 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
4292 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
4293 mp
, XFS_ERRTAG_BMAPIFORMAT
))) {
4294 XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW
, mp
);
4295 return -EFSCORRUPTED
;
4298 if (XFS_FORCED_SHUTDOWN(mp
))
4301 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4303 XFS_STATS_INC(mp
, xs_blk_mapw
);
4305 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
4306 error
= xfs_iread_extents(tp
, ip
, whichfork
);
4311 if (!xfs_iext_lookup_extent(ip
, ifp
, bno
, &bma
.icur
, &bma
.got
))
4313 if (!xfs_iext_peek_prev_extent(ifp
, &bma
.icur
, &bma
.prev
))
4314 bma
.prev
.br_startoff
= NULLFILEOFF
;
4319 bma
.minleft
= xfs_bmapi_minleft(tp
, ip
, whichfork
);
4324 while (bno
< end
&& n
< *nmap
) {
4325 bool need_alloc
= false, wasdelay
= false;
4327 /* in hole or beyond EOF? */
4328 if (eof
|| bma
.got
.br_startoff
> bno
) {
4330 * CoW fork conversions should /never/ hit EOF or
4331 * holes. There should always be something for us
4334 ASSERT(!((flags
& XFS_BMAPI_CONVERT
) &&
4335 (flags
& XFS_BMAPI_COWFORK
)));
4338 } else if (isnullstartblock(bma
.got
.br_startblock
)) {
4343 * First, deal with the hole before the allocated space
4344 * that we found, if any.
4346 if ((need_alloc
|| wasdelay
) &&
4347 !(flags
& XFS_BMAPI_CONVERT_ONLY
)) {
4349 bma
.conv
= !!(flags
& XFS_BMAPI_CONVERT
);
4350 bma
.wasdel
= wasdelay
;
4355 * There's a 32/64 bit type mismatch between the
4356 * allocation length request (which can be 64 bits in
4357 * length) and the bma length request, which is
4358 * xfs_extlen_t and therefore 32 bits. Hence we have to
4359 * check for 32-bit overflows and handle them here.
4361 if (len
> (xfs_filblks_t
)MAXEXTLEN
)
4362 bma
.length
= MAXEXTLEN
;
4367 ASSERT(bma
.length
> 0);
4368 error
= xfs_bmapi_allocate(&bma
);
4371 if (bma
.blkno
== NULLFSBLOCK
)
4375 * If this is a CoW allocation, record the data in
4376 * the refcount btree for orphan recovery.
4378 if (whichfork
== XFS_COW_FORK
) {
4379 error
= xfs_refcount_alloc_cow_extent(tp
,
4380 bma
.blkno
, bma
.length
);
4386 /* Deal with the allocated space we found. */
4387 xfs_bmapi_trim_map(mval
, &bma
.got
, &bno
, len
, obno
,
4390 /* Execute unwritten extent conversion if necessary */
4391 error
= xfs_bmapi_convert_unwritten(&bma
, mval
, len
, flags
);
4392 if (error
== -EAGAIN
)
4397 /* update the extent map to return */
4398 xfs_bmapi_update_map(&mval
, &bno
, &len
, obno
, end
, &n
, flags
);
4401 * If we're done, stop now. Stop when we've allocated
4402 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4403 * the transaction may get too big.
4405 if (bno
>= end
|| n
>= *nmap
|| bma
.nallocs
>= *nmap
)
4408 /* Else go on to the next record. */
4410 if (!xfs_iext_next_extent(ifp
, &bma
.icur
, &bma
.got
))
4415 error
= xfs_bmap_btree_to_extents(tp
, ip
, bma
.cur
, &bma
.logflags
,
4420 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
||
4421 XFS_IFORK_NEXTENTS(ip
, whichfork
) >
4422 XFS_IFORK_MAXEXT(ip
, whichfork
));
4423 xfs_bmapi_finish(&bma
, whichfork
, 0);
4424 xfs_bmap_validate_ret(orig_bno
, orig_len
, orig_flags
, orig_mval
,
4428 xfs_bmapi_finish(&bma
, whichfork
, error
);
4433 * Convert an existing delalloc extent to real blocks based on file offset. This
4434 * attempts to allocate the entire delalloc extent and may require multiple
4435 * invocations to allocate the target offset if a large enough physical extent
4439 xfs_bmapi_convert_delalloc(
4440 struct xfs_inode
*ip
,
4442 xfs_fileoff_t offset_fsb
,
4443 struct xfs_bmbt_irec
*imap
,
4446 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4447 struct xfs_mount
*mp
= ip
->i_mount
;
4448 struct xfs_bmalloca bma
= { NULL
};
4449 struct xfs_trans
*tp
;
4453 * Space for the extent and indirect blocks was reserved when the
4454 * delalloc extent was created so there's no need to do so here.
4456 error
= xfs_trans_alloc(mp
, &M_RES(mp
)->tr_write
, 0, 0,
4457 XFS_TRANS_RESERVE
, &tp
);
4461 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
4462 xfs_trans_ijoin(tp
, ip
, 0);
4464 if (!xfs_iext_lookup_extent(ip
, ifp
, offset_fsb
, &bma
.icur
, &bma
.got
) ||
4465 bma
.got
.br_startoff
> offset_fsb
) {
4467 * No extent found in the range we are trying to convert. This
4468 * should only happen for the COW fork, where another thread
4469 * might have moved the extent to the data fork in the meantime.
4471 WARN_ON_ONCE(whichfork
!= XFS_COW_FORK
);
4473 goto out_trans_cancel
;
4477 * If we find a real extent here we raced with another thread converting
4478 * the extent. Just return the real extent at this offset.
4480 if (!isnullstartblock(bma
.got
.br_startblock
)) {
4482 *seq
= READ_ONCE(ifp
->if_seq
);
4483 goto out_trans_cancel
;
4489 bma
.offset
= bma
.got
.br_startoff
;
4490 bma
.length
= max_t(xfs_filblks_t
, bma
.got
.br_blockcount
, MAXEXTLEN
);
4491 bma
.total
= XFS_EXTENTADD_SPACE_RES(ip
->i_mount
, XFS_DATA_FORK
);
4492 bma
.minleft
= xfs_bmapi_minleft(tp
, ip
, whichfork
);
4493 if (whichfork
== XFS_COW_FORK
)
4494 bma
.flags
= XFS_BMAPI_COWFORK
| XFS_BMAPI_PREALLOC
;
4496 if (!xfs_iext_peek_prev_extent(ifp
, &bma
.icur
, &bma
.prev
))
4497 bma
.prev
.br_startoff
= NULLFILEOFF
;
4499 error
= xfs_bmapi_allocate(&bma
);
4504 if (WARN_ON_ONCE(bma
.blkno
== NULLFSBLOCK
))
4506 error
= -EFSCORRUPTED
;
4507 if (WARN_ON_ONCE(!bma
.got
.br_startblock
&& !XFS_IS_REALTIME_INODE(ip
)))
4510 XFS_STATS_ADD(mp
, xs_xstrat_bytes
, XFS_FSB_TO_B(mp
, bma
.length
));
4511 XFS_STATS_INC(mp
, xs_xstrat_quick
);
4513 ASSERT(!isnullstartblock(bma
.got
.br_startblock
));
4515 *seq
= READ_ONCE(ifp
->if_seq
);
4517 if (whichfork
== XFS_COW_FORK
) {
4518 error
= xfs_refcount_alloc_cow_extent(tp
, bma
.blkno
,
4524 error
= xfs_bmap_btree_to_extents(tp
, ip
, bma
.cur
, &bma
.logflags
,
4529 xfs_bmapi_finish(&bma
, whichfork
, 0);
4530 error
= xfs_trans_commit(tp
);
4531 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
4535 xfs_bmapi_finish(&bma
, whichfork
, error
);
4537 xfs_trans_cancel(tp
);
4538 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
4544 struct xfs_trans
*tp
,
4545 struct xfs_inode
*ip
,
4548 xfs_fsblock_t startblock
,
4551 struct xfs_mount
*mp
= ip
->i_mount
;
4552 struct xfs_ifork
*ifp
;
4553 struct xfs_btree_cur
*cur
= NULL
;
4554 struct xfs_bmbt_irec got
;
4555 struct xfs_iext_cursor icur
;
4556 int whichfork
= xfs_bmapi_whichfork(flags
);
4557 int logflags
= 0, error
;
4559 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4561 ASSERT(len
<= (xfs_filblks_t
)MAXEXTLEN
);
4562 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
4563 ASSERT(!(flags
& ~(XFS_BMAPI_ATTRFORK
| XFS_BMAPI_PREALLOC
|
4564 XFS_BMAPI_NORMAP
)));
4565 ASSERT((flags
& (XFS_BMAPI_ATTRFORK
| XFS_BMAPI_PREALLOC
)) !=
4566 (XFS_BMAPI_ATTRFORK
| XFS_BMAPI_PREALLOC
));
4568 if (unlikely(XFS_TEST_ERROR(
4569 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
4570 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
4571 mp
, XFS_ERRTAG_BMAPIFORMAT
))) {
4572 XFS_ERROR_REPORT("xfs_bmapi_remap", XFS_ERRLEVEL_LOW
, mp
);
4573 return -EFSCORRUPTED
;
4576 if (XFS_FORCED_SHUTDOWN(mp
))
4579 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
4580 error
= xfs_iread_extents(tp
, ip
, whichfork
);
4585 if (xfs_iext_lookup_extent(ip
, ifp
, bno
, &icur
, &got
)) {
4586 /* make sure we only reflink into a hole. */
4587 ASSERT(got
.br_startoff
> bno
);
4588 ASSERT(got
.br_startoff
- bno
>= len
);
4591 ip
->i_d
.di_nblocks
+= len
;
4592 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
4594 if (ifp
->if_flags
& XFS_IFBROOT
) {
4595 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
4596 cur
->bc_private
.b
.flags
= 0;
4599 got
.br_startoff
= bno
;
4600 got
.br_startblock
= startblock
;
4601 got
.br_blockcount
= len
;
4602 if (flags
& XFS_BMAPI_PREALLOC
)
4603 got
.br_state
= XFS_EXT_UNWRITTEN
;
4605 got
.br_state
= XFS_EXT_NORM
;
4607 error
= xfs_bmap_add_extent_hole_real(tp
, ip
, whichfork
, &icur
,
4608 &cur
, &got
, &logflags
, flags
);
4612 error
= xfs_bmap_btree_to_extents(tp
, ip
, cur
, &logflags
, whichfork
);
4615 if (ip
->i_d
.di_format
!= XFS_DINODE_FMT_EXTENTS
)
4616 logflags
&= ~XFS_ILOG_DEXT
;
4617 else if (ip
->i_d
.di_format
!= XFS_DINODE_FMT_BTREE
)
4618 logflags
&= ~XFS_ILOG_DBROOT
;
4621 xfs_trans_log_inode(tp
, ip
, logflags
);
4623 xfs_btree_del_cursor(cur
, error
);
4628 * When a delalloc extent is split (e.g., due to a hole punch), the original
4629 * indlen reservation must be shared across the two new extents that are left
4632 * Given the original reservation and the worst case indlen for the two new
4633 * extents (as calculated by xfs_bmap_worst_indlen()), split the original
4634 * reservation fairly across the two new extents. If necessary, steal available
4635 * blocks from a deleted extent to make up a reservation deficiency (e.g., if
4636 * ores == 1). The number of stolen blocks is returned. The availability and
4637 * subsequent accounting of stolen blocks is the responsibility of the caller.
4639 static xfs_filblks_t
4640 xfs_bmap_split_indlen(
4641 xfs_filblks_t ores
, /* original res. */
4642 xfs_filblks_t
*indlen1
, /* ext1 worst indlen */
4643 xfs_filblks_t
*indlen2
, /* ext2 worst indlen */
4644 xfs_filblks_t avail
) /* stealable blocks */
4646 xfs_filblks_t len1
= *indlen1
;
4647 xfs_filblks_t len2
= *indlen2
;
4648 xfs_filblks_t nres
= len1
+ len2
; /* new total res. */
4649 xfs_filblks_t stolen
= 0;
4650 xfs_filblks_t resfactor
;
4653 * Steal as many blocks as we can to try and satisfy the worst case
4654 * indlen for both new extents.
4656 if (ores
< nres
&& avail
)
4657 stolen
= XFS_FILBLKS_MIN(nres
- ores
, avail
);
4660 /* nothing else to do if we've satisfied the new reservation */
4665 * We can't meet the total required reservation for the two extents.
4666 * Calculate the percent of the overall shortage between both extents
4667 * and apply this percentage to each of the requested indlen values.
4668 * This distributes the shortage fairly and reduces the chances that one
4669 * of the two extents is left with nothing when extents are repeatedly
4672 resfactor
= (ores
* 100);
4673 do_div(resfactor
, nres
);
4678 ASSERT(len1
+ len2
<= ores
);
4679 ASSERT(len1
< *indlen1
&& len2
< *indlen2
);
4682 * Hand out the remainder to each extent. If one of the two reservations
4683 * is zero, we want to make sure that one gets a block first. The loop
4684 * below starts with len1, so hand len2 a block right off the bat if it
4687 ores
-= (len1
+ len2
);
4688 ASSERT((*indlen1
- len1
) + (*indlen2
- len2
) >= ores
);
4689 if (ores
&& !len2
&& *indlen2
) {
4694 if (len1
< *indlen1
) {
4700 if (len2
< *indlen2
) {
4713 xfs_bmap_del_extent_delay(
4714 struct xfs_inode
*ip
,
4716 struct xfs_iext_cursor
*icur
,
4717 struct xfs_bmbt_irec
*got
,
4718 struct xfs_bmbt_irec
*del
)
4720 struct xfs_mount
*mp
= ip
->i_mount
;
4721 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4722 struct xfs_bmbt_irec
new;
4723 int64_t da_old
, da_new
, da_diff
= 0;
4724 xfs_fileoff_t del_endoff
, got_endoff
;
4725 xfs_filblks_t got_indlen
, new_indlen
, stolen
;
4726 int state
= xfs_bmap_fork_to_state(whichfork
);
4730 XFS_STATS_INC(mp
, xs_del_exlist
);
4732 isrt
= (whichfork
== XFS_DATA_FORK
) && XFS_IS_REALTIME_INODE(ip
);
4733 del_endoff
= del
->br_startoff
+ del
->br_blockcount
;
4734 got_endoff
= got
->br_startoff
+ got
->br_blockcount
;
4735 da_old
= startblockval(got
->br_startblock
);
4738 ASSERT(del
->br_blockcount
> 0);
4739 ASSERT(got
->br_startoff
<= del
->br_startoff
);
4740 ASSERT(got_endoff
>= del_endoff
);
4743 uint64_t rtexts
= XFS_FSB_TO_B(mp
, del
->br_blockcount
);
4745 do_div(rtexts
, mp
->m_sb
.sb_rextsize
);
4746 xfs_mod_frextents(mp
, rtexts
);
4750 * Update the inode delalloc counter now and wait to update the
4751 * sb counters as we might have to borrow some blocks for the
4752 * indirect block accounting.
4754 error
= xfs_trans_reserve_quota_nblks(NULL
, ip
,
4755 -((long)del
->br_blockcount
), 0,
4756 isrt
? XFS_QMOPT_RES_RTBLKS
: XFS_QMOPT_RES_REGBLKS
);
4759 ip
->i_delayed_blks
-= del
->br_blockcount
;
4761 if (got
->br_startoff
== del
->br_startoff
)
4762 state
|= BMAP_LEFT_FILLING
;
4763 if (got_endoff
== del_endoff
)
4764 state
|= BMAP_RIGHT_FILLING
;
4766 switch (state
& (BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
)) {
4767 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
4769 * Matches the whole extent. Delete the entry.
4771 xfs_iext_remove(ip
, icur
, state
);
4772 xfs_iext_prev(ifp
, icur
);
4774 case BMAP_LEFT_FILLING
:
4776 * Deleting the first part of the extent.
4778 got
->br_startoff
= del_endoff
;
4779 got
->br_blockcount
-= del
->br_blockcount
;
4780 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
,
4781 got
->br_blockcount
), da_old
);
4782 got
->br_startblock
= nullstartblock((int)da_new
);
4783 xfs_iext_update_extent(ip
, state
, icur
, got
);
4785 case BMAP_RIGHT_FILLING
:
4787 * Deleting the last part of the extent.
4789 got
->br_blockcount
= got
->br_blockcount
- del
->br_blockcount
;
4790 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
,
4791 got
->br_blockcount
), da_old
);
4792 got
->br_startblock
= nullstartblock((int)da_new
);
4793 xfs_iext_update_extent(ip
, state
, icur
, got
);
4797 * Deleting the middle of the extent.
4799 * Distribute the original indlen reservation across the two new
4800 * extents. Steal blocks from the deleted extent if necessary.
4801 * Stealing blocks simply fudges the fdblocks accounting below.
4802 * Warn if either of the new indlen reservations is zero as this
4803 * can lead to delalloc problems.
4805 got
->br_blockcount
= del
->br_startoff
- got
->br_startoff
;
4806 got_indlen
= xfs_bmap_worst_indlen(ip
, got
->br_blockcount
);
4808 new.br_blockcount
= got_endoff
- del_endoff
;
4809 new_indlen
= xfs_bmap_worst_indlen(ip
, new.br_blockcount
);
4811 WARN_ON_ONCE(!got_indlen
|| !new_indlen
);
4812 stolen
= xfs_bmap_split_indlen(da_old
, &got_indlen
, &new_indlen
,
4813 del
->br_blockcount
);
4815 got
->br_startblock
= nullstartblock((int)got_indlen
);
4817 new.br_startoff
= del_endoff
;
4818 new.br_state
= got
->br_state
;
4819 new.br_startblock
= nullstartblock((int)new_indlen
);
4821 xfs_iext_update_extent(ip
, state
, icur
, got
);
4822 xfs_iext_next(ifp
, icur
);
4823 xfs_iext_insert(ip
, icur
, &new, state
);
4825 da_new
= got_indlen
+ new_indlen
- stolen
;
4826 del
->br_blockcount
-= stolen
;
4830 ASSERT(da_old
>= da_new
);
4831 da_diff
= da_old
- da_new
;
4833 da_diff
+= del
->br_blockcount
;
4835 xfs_mod_fdblocks(mp
, da_diff
, false);
4840 xfs_bmap_del_extent_cow(
4841 struct xfs_inode
*ip
,
4842 struct xfs_iext_cursor
*icur
,
4843 struct xfs_bmbt_irec
*got
,
4844 struct xfs_bmbt_irec
*del
)
4846 struct xfs_mount
*mp
= ip
->i_mount
;
4847 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, XFS_COW_FORK
);
4848 struct xfs_bmbt_irec
new;
4849 xfs_fileoff_t del_endoff
, got_endoff
;
4850 int state
= BMAP_COWFORK
;
4852 XFS_STATS_INC(mp
, xs_del_exlist
);
4854 del_endoff
= del
->br_startoff
+ del
->br_blockcount
;
4855 got_endoff
= got
->br_startoff
+ got
->br_blockcount
;
4857 ASSERT(del
->br_blockcount
> 0);
4858 ASSERT(got
->br_startoff
<= del
->br_startoff
);
4859 ASSERT(got_endoff
>= del_endoff
);
4860 ASSERT(!isnullstartblock(got
->br_startblock
));
4862 if (got
->br_startoff
== del
->br_startoff
)
4863 state
|= BMAP_LEFT_FILLING
;
4864 if (got_endoff
== del_endoff
)
4865 state
|= BMAP_RIGHT_FILLING
;
4867 switch (state
& (BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
)) {
4868 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
4870 * Matches the whole extent. Delete the entry.
4872 xfs_iext_remove(ip
, icur
, state
);
4873 xfs_iext_prev(ifp
, icur
);
4875 case BMAP_LEFT_FILLING
:
4877 * Deleting the first part of the extent.
4879 got
->br_startoff
= del_endoff
;
4880 got
->br_blockcount
-= del
->br_blockcount
;
4881 got
->br_startblock
= del
->br_startblock
+ del
->br_blockcount
;
4882 xfs_iext_update_extent(ip
, state
, icur
, got
);
4884 case BMAP_RIGHT_FILLING
:
4886 * Deleting the last part of the extent.
4888 got
->br_blockcount
-= del
->br_blockcount
;
4889 xfs_iext_update_extent(ip
, state
, icur
, got
);
4893 * Deleting the middle of the extent.
4895 got
->br_blockcount
= del
->br_startoff
- got
->br_startoff
;
4897 new.br_startoff
= del_endoff
;
4898 new.br_blockcount
= got_endoff
- del_endoff
;
4899 new.br_state
= got
->br_state
;
4900 new.br_startblock
= del
->br_startblock
+ del
->br_blockcount
;
4902 xfs_iext_update_extent(ip
, state
, icur
, got
);
4903 xfs_iext_next(ifp
, icur
);
4904 xfs_iext_insert(ip
, icur
, &new, state
);
4907 ip
->i_delayed_blks
-= del
->br_blockcount
;
4911 * Called by xfs_bmapi to update file extent records and the btree
4912 * after removing space.
4914 STATIC
int /* error */
4915 xfs_bmap_del_extent_real(
4916 xfs_inode_t
*ip
, /* incore inode pointer */
4917 xfs_trans_t
*tp
, /* current transaction pointer */
4918 struct xfs_iext_cursor
*icur
,
4919 xfs_btree_cur_t
*cur
, /* if null, not a btree */
4920 xfs_bmbt_irec_t
*del
, /* data to remove from extents */
4921 int *logflagsp
, /* inode logging flags */
4922 int whichfork
, /* data or attr fork */
4923 int bflags
) /* bmapi flags */
4925 xfs_fsblock_t del_endblock
=0; /* first block past del */
4926 xfs_fileoff_t del_endoff
; /* first offset past del */
4927 int do_fx
; /* free extent at end of routine */
4928 int error
; /* error return value */
4929 int flags
= 0;/* inode logging flags */
4930 struct xfs_bmbt_irec got
; /* current extent entry */
4931 xfs_fileoff_t got_endoff
; /* first offset past got */
4932 int i
; /* temp state */
4933 struct xfs_ifork
*ifp
; /* inode fork pointer */
4934 xfs_mount_t
*mp
; /* mount structure */
4935 xfs_filblks_t nblks
; /* quota/sb block count */
4936 xfs_bmbt_irec_t
new; /* new record to be inserted */
4938 uint qfield
; /* quota field to update */
4939 int state
= xfs_bmap_fork_to_state(whichfork
);
4940 struct xfs_bmbt_irec old
;
4943 XFS_STATS_INC(mp
, xs_del_exlist
);
4945 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4946 ASSERT(del
->br_blockcount
> 0);
4947 xfs_iext_get_extent(ifp
, icur
, &got
);
4948 ASSERT(got
.br_startoff
<= del
->br_startoff
);
4949 del_endoff
= del
->br_startoff
+ del
->br_blockcount
;
4950 got_endoff
= got
.br_startoff
+ got
.br_blockcount
;
4951 ASSERT(got_endoff
>= del_endoff
);
4952 ASSERT(!isnullstartblock(got
.br_startblock
));
4957 * If it's the case where the directory code is running with no block
4958 * reservation, and the deleted block is in the middle of its extent,
4959 * and the resulting insert of an extent would cause transformation to
4960 * btree format, then reject it. The calling code will then swap blocks
4961 * around instead. We have to do this now, rather than waiting for the
4962 * conversion to btree format, since the transaction will be dirty then.
4964 if (tp
->t_blk_res
== 0 &&
4965 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
&&
4966 XFS_IFORK_NEXTENTS(ip
, whichfork
) >=
4967 XFS_IFORK_MAXEXT(ip
, whichfork
) &&
4968 del
->br_startoff
> got
.br_startoff
&& del_endoff
< got_endoff
)
4971 flags
= XFS_ILOG_CORE
;
4972 if (whichfork
== XFS_DATA_FORK
&& XFS_IS_REALTIME_INODE(ip
)) {
4977 bno
= div_u64_rem(del
->br_startblock
, mp
->m_sb
.sb_rextsize
,
4980 len
= div_u64_rem(del
->br_blockcount
, mp
->m_sb
.sb_rextsize
,
4984 error
= xfs_rtfree_extent(tp
, bno
, (xfs_extlen_t
)len
);
4988 nblks
= len
* mp
->m_sb
.sb_rextsize
;
4989 qfield
= XFS_TRANS_DQ_RTBCOUNT
;
4992 nblks
= del
->br_blockcount
;
4993 qfield
= XFS_TRANS_DQ_BCOUNT
;
4996 del_endblock
= del
->br_startblock
+ del
->br_blockcount
;
4998 error
= xfs_bmbt_lookup_eq(cur
, &got
, &i
);
5001 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
5004 if (got
.br_startoff
== del
->br_startoff
)
5005 state
|= BMAP_LEFT_FILLING
;
5006 if (got_endoff
== del_endoff
)
5007 state
|= BMAP_RIGHT_FILLING
;
5009 switch (state
& (BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
)) {
5010 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
5012 * Matches the whole extent. Delete the entry.
5014 xfs_iext_remove(ip
, icur
, state
);
5015 xfs_iext_prev(ifp
, icur
);
5016 XFS_IFORK_NEXT_SET(ip
, whichfork
,
5017 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
5018 flags
|= XFS_ILOG_CORE
;
5020 flags
|= xfs_ilog_fext(whichfork
);
5023 if ((error
= xfs_btree_delete(cur
, &i
)))
5025 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
5027 case BMAP_LEFT_FILLING
:
5029 * Deleting the first part of the extent.
5031 got
.br_startoff
= del_endoff
;
5032 got
.br_startblock
= del_endblock
;
5033 got
.br_blockcount
-= del
->br_blockcount
;
5034 xfs_iext_update_extent(ip
, state
, icur
, &got
);
5036 flags
|= xfs_ilog_fext(whichfork
);
5039 error
= xfs_bmbt_update(cur
, &got
);
5043 case BMAP_RIGHT_FILLING
:
5045 * Deleting the last part of the extent.
5047 got
.br_blockcount
-= del
->br_blockcount
;
5048 xfs_iext_update_extent(ip
, state
, icur
, &got
);
5050 flags
|= xfs_ilog_fext(whichfork
);
5053 error
= xfs_bmbt_update(cur
, &got
);
5059 * Deleting the middle of the extent.
5063 got
.br_blockcount
= del
->br_startoff
- got
.br_startoff
;
5064 xfs_iext_update_extent(ip
, state
, icur
, &got
);
5066 new.br_startoff
= del_endoff
;
5067 new.br_blockcount
= got_endoff
- del_endoff
;
5068 new.br_state
= got
.br_state
;
5069 new.br_startblock
= del_endblock
;
5071 flags
|= XFS_ILOG_CORE
;
5073 error
= xfs_bmbt_update(cur
, &got
);
5076 error
= xfs_btree_increment(cur
, 0, &i
);
5079 cur
->bc_rec
.b
= new;
5080 error
= xfs_btree_insert(cur
, &i
);
5081 if (error
&& error
!= -ENOSPC
)
5084 * If get no-space back from btree insert, it tried a
5085 * split, and we have a zero block reservation. Fix up
5086 * our state and return the error.
5088 if (error
== -ENOSPC
) {
5090 * Reset the cursor, don't trust it after any
5093 error
= xfs_bmbt_lookup_eq(cur
, &got
, &i
);
5096 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
5098 * Update the btree record back
5099 * to the original value.
5101 error
= xfs_bmbt_update(cur
, &old
);
5105 * Reset the extent record back
5106 * to the original value.
5108 xfs_iext_update_extent(ip
, state
, icur
, &old
);
5113 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
5115 flags
|= xfs_ilog_fext(whichfork
);
5116 XFS_IFORK_NEXT_SET(ip
, whichfork
,
5117 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
5118 xfs_iext_next(ifp
, icur
);
5119 xfs_iext_insert(ip
, icur
, &new, state
);
5123 /* remove reverse mapping */
5124 error
= xfs_rmap_unmap_extent(tp
, ip
, whichfork
, del
);
5129 * If we need to, add to list of extents to delete.
5131 if (do_fx
&& !(bflags
& XFS_BMAPI_REMAP
)) {
5132 if (xfs_is_reflink_inode(ip
) && whichfork
== XFS_DATA_FORK
) {
5133 error
= xfs_refcount_decrease_extent(tp
, del
);
5137 __xfs_bmap_add_free(tp
, del
->br_startblock
,
5138 del
->br_blockcount
, NULL
,
5139 (bflags
& XFS_BMAPI_NODISCARD
) ||
5140 del
->br_state
== XFS_EXT_UNWRITTEN
);
5145 * Adjust inode # blocks in the file.
5148 ip
->i_d
.di_nblocks
-= nblks
;
5150 * Adjust quota data.
5152 if (qfield
&& !(bflags
& XFS_BMAPI_REMAP
))
5153 xfs_trans_mod_dquot_byino(tp
, ip
, qfield
, (long)-nblks
);
5161 * Unmap (remove) blocks from a file.
5162 * If nexts is nonzero then the number of extents to remove is limited to
5163 * that value. If not all extents in the block range can be removed then
5168 struct xfs_trans
*tp
, /* transaction pointer */
5169 struct xfs_inode
*ip
, /* incore inode */
5170 xfs_fileoff_t start
, /* first file offset deleted */
5171 xfs_filblks_t
*rlen
, /* i/o: amount remaining */
5172 int flags
, /* misc flags */
5173 xfs_extnum_t nexts
) /* number of extents max */
5175 struct xfs_btree_cur
*cur
; /* bmap btree cursor */
5176 struct xfs_bmbt_irec del
; /* extent being deleted */
5177 int error
; /* error return value */
5178 xfs_extnum_t extno
; /* extent number in list */
5179 struct xfs_bmbt_irec got
; /* current extent record */
5180 struct xfs_ifork
*ifp
; /* inode fork pointer */
5181 int isrt
; /* freeing in rt area */
5182 int logflags
; /* transaction logging flags */
5183 xfs_extlen_t mod
; /* rt extent offset */
5184 struct xfs_mount
*mp
; /* mount structure */
5185 int tmp_logflags
; /* partial logging flags */
5186 int wasdel
; /* was a delayed alloc extent */
5187 int whichfork
; /* data or attribute fork */
5189 xfs_filblks_t len
= *rlen
; /* length to unmap in file */
5190 xfs_fileoff_t max_len
;
5191 xfs_agnumber_t prev_agno
= NULLAGNUMBER
, agno
;
5193 struct xfs_iext_cursor icur
;
5196 trace_xfs_bunmap(ip
, start
, len
, flags
, _RET_IP_
);
5198 whichfork
= xfs_bmapi_whichfork(flags
);
5199 ASSERT(whichfork
!= XFS_COW_FORK
);
5200 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5202 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
5203 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)) {
5204 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW
,
5206 return -EFSCORRUPTED
;
5209 if (XFS_FORCED_SHUTDOWN(mp
))
5212 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
5217 * Guesstimate how many blocks we can unmap without running the risk of
5218 * blowing out the transaction with a mix of EFIs and reflink
5221 if (tp
&& xfs_is_reflink_inode(ip
) && whichfork
== XFS_DATA_FORK
)
5222 max_len
= min(len
, xfs_refcount_max_unmap(tp
->t_log_res
));
5226 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
5227 (error
= xfs_iread_extents(tp
, ip
, whichfork
)))
5229 if (xfs_iext_count(ifp
) == 0) {
5233 XFS_STATS_INC(mp
, xs_blk_unmap
);
5234 isrt
= (whichfork
== XFS_DATA_FORK
) && XFS_IS_REALTIME_INODE(ip
);
5237 if (!xfs_iext_lookup_extent_before(ip
, ifp
, &end
, &icur
, &got
)) {
5244 if (ifp
->if_flags
& XFS_IFBROOT
) {
5245 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
);
5246 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
5247 cur
->bc_private
.b
.flags
= 0;
5253 * Synchronize by locking the bitmap inode.
5255 xfs_ilock(mp
->m_rbmip
, XFS_ILOCK_EXCL
|XFS_ILOCK_RTBITMAP
);
5256 xfs_trans_ijoin(tp
, mp
->m_rbmip
, XFS_ILOCK_EXCL
);
5257 xfs_ilock(mp
->m_rsumip
, XFS_ILOCK_EXCL
|XFS_ILOCK_RTSUM
);
5258 xfs_trans_ijoin(tp
, mp
->m_rsumip
, XFS_ILOCK_EXCL
);
5262 while (end
!= (xfs_fileoff_t
)-1 && end
>= start
&&
5263 (nexts
== 0 || extno
< nexts
) && max_len
> 0) {
5265 * Is the found extent after a hole in which end lives?
5266 * Just back up to the previous extent, if so.
5268 if (got
.br_startoff
> end
&&
5269 !xfs_iext_prev_extent(ifp
, &icur
, &got
)) {
5274 * Is the last block of this extent before the range
5275 * we're supposed to delete? If so, we're done.
5277 end
= XFS_FILEOFF_MIN(end
,
5278 got
.br_startoff
+ got
.br_blockcount
- 1);
5282 * Then deal with the (possibly delayed) allocated space
5286 wasdel
= isnullstartblock(del
.br_startblock
);
5289 * Make sure we don't touch multiple AGF headers out of order
5290 * in a single transaction, as that could cause AB-BA deadlocks.
5293 agno
= XFS_FSB_TO_AGNO(mp
, del
.br_startblock
);
5294 if (prev_agno
!= NULLAGNUMBER
&& prev_agno
> agno
)
5298 if (got
.br_startoff
< start
) {
5299 del
.br_startoff
= start
;
5300 del
.br_blockcount
-= start
- got
.br_startoff
;
5302 del
.br_startblock
+= start
- got
.br_startoff
;
5304 if (del
.br_startoff
+ del
.br_blockcount
> end
+ 1)
5305 del
.br_blockcount
= end
+ 1 - del
.br_startoff
;
5307 /* How much can we safely unmap? */
5308 if (max_len
< del
.br_blockcount
) {
5309 del
.br_startoff
+= del
.br_blockcount
- max_len
;
5311 del
.br_startblock
+= del
.br_blockcount
- max_len
;
5312 del
.br_blockcount
= max_len
;
5318 sum
= del
.br_startblock
+ del
.br_blockcount
;
5319 div_u64_rem(sum
, mp
->m_sb
.sb_rextsize
, &mod
);
5322 * Realtime extent not lined up at the end.
5323 * The extent could have been split into written
5324 * and unwritten pieces, or we could just be
5325 * unmapping part of it. But we can't really
5326 * get rid of part of a realtime extent.
5328 if (del
.br_state
== XFS_EXT_UNWRITTEN
) {
5330 * This piece is unwritten, or we're not
5331 * using unwritten extents. Skip over it.
5334 end
-= mod
> del
.br_blockcount
?
5335 del
.br_blockcount
: mod
;
5336 if (end
< got
.br_startoff
&&
5337 !xfs_iext_prev_extent(ifp
, &icur
, &got
)) {
5344 * It's written, turn it unwritten.
5345 * This is better than zeroing it.
5347 ASSERT(del
.br_state
== XFS_EXT_NORM
);
5348 ASSERT(tp
->t_blk_res
> 0);
5350 * If this spans a realtime extent boundary,
5351 * chop it back to the start of the one we end at.
5353 if (del
.br_blockcount
> mod
) {
5354 del
.br_startoff
+= del
.br_blockcount
- mod
;
5355 del
.br_startblock
+= del
.br_blockcount
- mod
;
5356 del
.br_blockcount
= mod
;
5358 del
.br_state
= XFS_EXT_UNWRITTEN
;
5359 error
= xfs_bmap_add_extent_unwritten_real(tp
, ip
,
5360 whichfork
, &icur
, &cur
, &del
,
5366 div_u64_rem(del
.br_startblock
, mp
->m_sb
.sb_rextsize
, &mod
);
5369 * Realtime extent is lined up at the end but not
5370 * at the front. We'll get rid of full extents if
5373 mod
= mp
->m_sb
.sb_rextsize
- mod
;
5374 if (del
.br_blockcount
> mod
) {
5375 del
.br_blockcount
-= mod
;
5376 del
.br_startoff
+= mod
;
5377 del
.br_startblock
+= mod
;
5378 } else if (del
.br_startoff
== start
&&
5379 (del
.br_state
== XFS_EXT_UNWRITTEN
||
5380 tp
->t_blk_res
== 0)) {
5382 * Can't make it unwritten. There isn't
5383 * a full extent here so just skip it.
5385 ASSERT(end
>= del
.br_blockcount
);
5386 end
-= del
.br_blockcount
;
5387 if (got
.br_startoff
> end
&&
5388 !xfs_iext_prev_extent(ifp
, &icur
, &got
)) {
5393 } else if (del
.br_state
== XFS_EXT_UNWRITTEN
) {
5394 struct xfs_bmbt_irec prev
;
5397 * This one is already unwritten.
5398 * It must have a written left neighbor.
5399 * Unwrite the killed part of that one and
5402 if (!xfs_iext_prev_extent(ifp
, &icur
, &prev
))
5404 ASSERT(prev
.br_state
== XFS_EXT_NORM
);
5405 ASSERT(!isnullstartblock(prev
.br_startblock
));
5406 ASSERT(del
.br_startblock
==
5407 prev
.br_startblock
+ prev
.br_blockcount
);
5408 if (prev
.br_startoff
< start
) {
5409 mod
= start
- prev
.br_startoff
;
5410 prev
.br_blockcount
-= mod
;
5411 prev
.br_startblock
+= mod
;
5412 prev
.br_startoff
= start
;
5414 prev
.br_state
= XFS_EXT_UNWRITTEN
;
5415 error
= xfs_bmap_add_extent_unwritten_real(tp
,
5416 ip
, whichfork
, &icur
, &cur
,
5422 ASSERT(del
.br_state
== XFS_EXT_NORM
);
5423 del
.br_state
= XFS_EXT_UNWRITTEN
;
5424 error
= xfs_bmap_add_extent_unwritten_real(tp
,
5425 ip
, whichfork
, &icur
, &cur
,
5435 error
= xfs_bmap_del_extent_delay(ip
, whichfork
, &icur
,
5438 error
= xfs_bmap_del_extent_real(ip
, tp
, &icur
, cur
,
5439 &del
, &tmp_logflags
, whichfork
,
5441 logflags
|= tmp_logflags
;
5447 max_len
-= del
.br_blockcount
;
5448 end
= del
.br_startoff
- 1;
5451 * If not done go on to the next (previous) record.
5453 if (end
!= (xfs_fileoff_t
)-1 && end
>= start
) {
5454 if (!xfs_iext_get_extent(ifp
, &icur
, &got
) ||
5455 (got
.br_startoff
> end
&&
5456 !xfs_iext_prev_extent(ifp
, &icur
, &got
))) {
5463 if (done
|| end
== (xfs_fileoff_t
)-1 || end
< start
)
5466 *rlen
= end
- start
+ 1;
5469 * Convert to a btree if necessary.
5471 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
5472 ASSERT(cur
== NULL
);
5473 error
= xfs_bmap_extents_to_btree(tp
, ip
, &cur
, 0,
5474 &tmp_logflags
, whichfork
);
5475 logflags
|= tmp_logflags
;
5477 error
= xfs_bmap_btree_to_extents(tp
, ip
, cur
, &logflags
,
5483 * Log everything. Do this after conversion, there's no point in
5484 * logging the extent records if we've converted to btree format.
5486 if ((logflags
& xfs_ilog_fext(whichfork
)) &&
5487 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
5488 logflags
&= ~xfs_ilog_fext(whichfork
);
5489 else if ((logflags
& xfs_ilog_fbroot(whichfork
)) &&
5490 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)
5491 logflags
&= ~xfs_ilog_fbroot(whichfork
);
5493 * Log inode even in the error case, if the transaction
5494 * is dirty we'll need to shut down the filesystem.
5497 xfs_trans_log_inode(tp
, ip
, logflags
);
5500 cur
->bc_private
.b
.allocated
= 0;
5501 xfs_btree_del_cursor(cur
, error
);
5506 /* Unmap a range of a file. */
5510 struct xfs_inode
*ip
,
5519 error
= __xfs_bunmapi(tp
, ip
, bno
, &len
, flags
, nexts
);
5525 * Determine whether an extent shift can be accomplished by a merge with the
5526 * extent that precedes the target hole of the shift.
5530 struct xfs_bmbt_irec
*left
, /* preceding extent */
5531 struct xfs_bmbt_irec
*got
, /* current extent to shift */
5532 xfs_fileoff_t shift
) /* shift fsb */
5534 xfs_fileoff_t startoff
;
5536 startoff
= got
->br_startoff
- shift
;
5539 * The extent, once shifted, must be adjacent in-file and on-disk with
5540 * the preceding extent.
5542 if ((left
->br_startoff
+ left
->br_blockcount
!= startoff
) ||
5543 (left
->br_startblock
+ left
->br_blockcount
!= got
->br_startblock
) ||
5544 (left
->br_state
!= got
->br_state
) ||
5545 (left
->br_blockcount
+ got
->br_blockcount
> MAXEXTLEN
))
5552 * A bmap extent shift adjusts the file offset of an extent to fill a preceding
5553 * hole in the file. If an extent shift would result in the extent being fully
5554 * adjacent to the extent that currently precedes the hole, we can merge with
5555 * the preceding extent rather than do the shift.
5557 * This function assumes the caller has verified a shift-by-merge is possible
5558 * with the provided extents via xfs_bmse_can_merge().
5562 struct xfs_trans
*tp
,
5563 struct xfs_inode
*ip
,
5565 xfs_fileoff_t shift
, /* shift fsb */
5566 struct xfs_iext_cursor
*icur
,
5567 struct xfs_bmbt_irec
*got
, /* extent to shift */
5568 struct xfs_bmbt_irec
*left
, /* preceding extent */
5569 struct xfs_btree_cur
*cur
,
5570 int *logflags
) /* output */
5572 struct xfs_bmbt_irec
new;
5573 xfs_filblks_t blockcount
;
5575 struct xfs_mount
*mp
= ip
->i_mount
;
5577 blockcount
= left
->br_blockcount
+ got
->br_blockcount
;
5579 ASSERT(xfs_isilocked(ip
, XFS_IOLOCK_EXCL
));
5580 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
5581 ASSERT(xfs_bmse_can_merge(left
, got
, shift
));
5584 new.br_blockcount
= blockcount
;
5587 * Update the on-disk extent count, the btree if necessary and log the
5590 XFS_IFORK_NEXT_SET(ip
, whichfork
,
5591 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
5592 *logflags
|= XFS_ILOG_CORE
;
5594 *logflags
|= XFS_ILOG_DEXT
;
5598 /* lookup and remove the extent to merge */
5599 error
= xfs_bmbt_lookup_eq(cur
, got
, &i
);
5602 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
5604 error
= xfs_btree_delete(cur
, &i
);
5607 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
5609 /* lookup and update size of the previous extent */
5610 error
= xfs_bmbt_lookup_eq(cur
, left
, &i
);
5613 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
5615 error
= xfs_bmbt_update(cur
, &new);
5620 xfs_iext_remove(ip
, icur
, 0);
5621 xfs_iext_prev(XFS_IFORK_PTR(ip
, whichfork
), icur
);
5622 xfs_iext_update_extent(ip
, xfs_bmap_fork_to_state(whichfork
), icur
,
5625 /* update reverse mapping. rmap functions merge the rmaps for us */
5626 error
= xfs_rmap_unmap_extent(tp
, ip
, whichfork
, got
);
5629 memcpy(&new, got
, sizeof(new));
5630 new.br_startoff
= left
->br_startoff
+ left
->br_blockcount
;
5631 return xfs_rmap_map_extent(tp
, ip
, whichfork
, &new);
5635 xfs_bmap_shift_update_extent(
5636 struct xfs_trans
*tp
,
5637 struct xfs_inode
*ip
,
5639 struct xfs_iext_cursor
*icur
,
5640 struct xfs_bmbt_irec
*got
,
5641 struct xfs_btree_cur
*cur
,
5643 xfs_fileoff_t startoff
)
5645 struct xfs_mount
*mp
= ip
->i_mount
;
5646 struct xfs_bmbt_irec prev
= *got
;
5649 *logflags
|= XFS_ILOG_CORE
;
5651 got
->br_startoff
= startoff
;
5654 error
= xfs_bmbt_lookup_eq(cur
, &prev
, &i
);
5657 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
5659 error
= xfs_bmbt_update(cur
, got
);
5663 *logflags
|= XFS_ILOG_DEXT
;
5666 xfs_iext_update_extent(ip
, xfs_bmap_fork_to_state(whichfork
), icur
,
5669 /* update reverse mapping */
5670 error
= xfs_rmap_unmap_extent(tp
, ip
, whichfork
, &prev
);
5673 return xfs_rmap_map_extent(tp
, ip
, whichfork
, got
);
5677 xfs_bmap_collapse_extents(
5678 struct xfs_trans
*tp
,
5679 struct xfs_inode
*ip
,
5680 xfs_fileoff_t
*next_fsb
,
5681 xfs_fileoff_t offset_shift_fsb
,
5684 int whichfork
= XFS_DATA_FORK
;
5685 struct xfs_mount
*mp
= ip
->i_mount
;
5686 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5687 struct xfs_btree_cur
*cur
= NULL
;
5688 struct xfs_bmbt_irec got
, prev
;
5689 struct xfs_iext_cursor icur
;
5690 xfs_fileoff_t new_startoff
;
5694 if (unlikely(XFS_TEST_ERROR(
5695 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
5696 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
5697 mp
, XFS_ERRTAG_BMAPIFORMAT
))) {
5698 XFS_ERROR_REPORT(__func__
, XFS_ERRLEVEL_LOW
, mp
);
5699 return -EFSCORRUPTED
;
5702 if (XFS_FORCED_SHUTDOWN(mp
))
5705 ASSERT(xfs_isilocked(ip
, XFS_IOLOCK_EXCL
| XFS_ILOCK_EXCL
));
5707 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
5708 error
= xfs_iread_extents(tp
, ip
, whichfork
);
5713 if (ifp
->if_flags
& XFS_IFBROOT
) {
5714 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
5715 cur
->bc_private
.b
.flags
= 0;
5718 if (!xfs_iext_lookup_extent(ip
, ifp
, *next_fsb
, &icur
, &got
)) {
5722 XFS_WANT_CORRUPTED_GOTO(mp
, !isnullstartblock(got
.br_startblock
),
5725 new_startoff
= got
.br_startoff
- offset_shift_fsb
;
5726 if (xfs_iext_peek_prev_extent(ifp
, &icur
, &prev
)) {
5727 if (new_startoff
< prev
.br_startoff
+ prev
.br_blockcount
) {
5732 if (xfs_bmse_can_merge(&prev
, &got
, offset_shift_fsb
)) {
5733 error
= xfs_bmse_merge(tp
, ip
, whichfork
,
5734 offset_shift_fsb
, &icur
, &got
, &prev
,
5741 if (got
.br_startoff
< offset_shift_fsb
) {
5747 error
= xfs_bmap_shift_update_extent(tp
, ip
, whichfork
, &icur
, &got
,
5748 cur
, &logflags
, new_startoff
);
5753 if (!xfs_iext_next_extent(ifp
, &icur
, &got
)) {
5758 *next_fsb
= got
.br_startoff
;
5761 xfs_btree_del_cursor(cur
, error
);
5763 xfs_trans_log_inode(tp
, ip
, logflags
);
5767 /* Make sure we won't be right-shifting an extent past the maximum bound. */
5769 xfs_bmap_can_insert_extents(
5770 struct xfs_inode
*ip
,
5772 xfs_fileoff_t shift
)
5774 struct xfs_bmbt_irec got
;
5778 ASSERT(xfs_isilocked(ip
, XFS_IOLOCK_EXCL
));
5780 if (XFS_FORCED_SHUTDOWN(ip
->i_mount
))
5783 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
5784 error
= xfs_bmap_last_extent(NULL
, ip
, XFS_DATA_FORK
, &got
, &is_empty
);
5785 if (!error
&& !is_empty
&& got
.br_startoff
>= off
&&
5786 ((got
.br_startoff
+ shift
) & BMBT_STARTOFF_MASK
) < got
.br_startoff
)
5788 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
5794 xfs_bmap_insert_extents(
5795 struct xfs_trans
*tp
,
5796 struct xfs_inode
*ip
,
5797 xfs_fileoff_t
*next_fsb
,
5798 xfs_fileoff_t offset_shift_fsb
,
5800 xfs_fileoff_t stop_fsb
)
5802 int whichfork
= XFS_DATA_FORK
;
5803 struct xfs_mount
*mp
= ip
->i_mount
;
5804 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5805 struct xfs_btree_cur
*cur
= NULL
;
5806 struct xfs_bmbt_irec got
, next
;
5807 struct xfs_iext_cursor icur
;
5808 xfs_fileoff_t new_startoff
;
5812 if (unlikely(XFS_TEST_ERROR(
5813 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
5814 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
5815 mp
, XFS_ERRTAG_BMAPIFORMAT
))) {
5816 XFS_ERROR_REPORT(__func__
, XFS_ERRLEVEL_LOW
, mp
);
5817 return -EFSCORRUPTED
;
5820 if (XFS_FORCED_SHUTDOWN(mp
))
5823 ASSERT(xfs_isilocked(ip
, XFS_IOLOCK_EXCL
| XFS_ILOCK_EXCL
));
5825 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
5826 error
= xfs_iread_extents(tp
, ip
, whichfork
);
5831 if (ifp
->if_flags
& XFS_IFBROOT
) {
5832 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
5833 cur
->bc_private
.b
.flags
= 0;
5836 if (*next_fsb
== NULLFSBLOCK
) {
5837 xfs_iext_last(ifp
, &icur
);
5838 if (!xfs_iext_get_extent(ifp
, &icur
, &got
) ||
5839 stop_fsb
> got
.br_startoff
) {
5844 if (!xfs_iext_lookup_extent(ip
, ifp
, *next_fsb
, &icur
, &got
)) {
5849 XFS_WANT_CORRUPTED_GOTO(mp
, !isnullstartblock(got
.br_startblock
),
5852 if (stop_fsb
>= got
.br_startoff
+ got
.br_blockcount
) {
5857 new_startoff
= got
.br_startoff
+ offset_shift_fsb
;
5858 if (xfs_iext_peek_next_extent(ifp
, &icur
, &next
)) {
5859 if (new_startoff
+ got
.br_blockcount
> next
.br_startoff
) {
5865 * Unlike a left shift (which involves a hole punch), a right
5866 * shift does not modify extent neighbors in any way. We should
5867 * never find mergeable extents in this scenario. Check anyways
5868 * and warn if we encounter two extents that could be one.
5870 if (xfs_bmse_can_merge(&got
, &next
, offset_shift_fsb
))
5874 error
= xfs_bmap_shift_update_extent(tp
, ip
, whichfork
, &icur
, &got
,
5875 cur
, &logflags
, new_startoff
);
5879 if (!xfs_iext_prev_extent(ifp
, &icur
, &got
) ||
5880 stop_fsb
>= got
.br_startoff
+ got
.br_blockcount
) {
5885 *next_fsb
= got
.br_startoff
;
5888 xfs_btree_del_cursor(cur
, error
);
5890 xfs_trans_log_inode(tp
, ip
, logflags
);
5895 * Splits an extent into two extents at split_fsb block such that it is the
5896 * first block of the current_ext. @ext is a target extent to be split.
5897 * @split_fsb is a block where the extents is split. If split_fsb lies in a
5898 * hole or the first block of extents, just return 0.
5901 xfs_bmap_split_extent_at(
5902 struct xfs_trans
*tp
,
5903 struct xfs_inode
*ip
,
5904 xfs_fileoff_t split_fsb
)
5906 int whichfork
= XFS_DATA_FORK
;
5907 struct xfs_btree_cur
*cur
= NULL
;
5908 struct xfs_bmbt_irec got
;
5909 struct xfs_bmbt_irec
new; /* split extent */
5910 struct xfs_mount
*mp
= ip
->i_mount
;
5911 struct xfs_ifork
*ifp
;
5912 xfs_fsblock_t gotblkcnt
; /* new block count for got */
5913 struct xfs_iext_cursor icur
;
5918 if (unlikely(XFS_TEST_ERROR(
5919 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
5920 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
5921 mp
, XFS_ERRTAG_BMAPIFORMAT
))) {
5922 XFS_ERROR_REPORT("xfs_bmap_split_extent_at",
5923 XFS_ERRLEVEL_LOW
, mp
);
5924 return -EFSCORRUPTED
;
5927 if (XFS_FORCED_SHUTDOWN(mp
))
5930 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5931 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
5932 /* Read in all the extents */
5933 error
= xfs_iread_extents(tp
, ip
, whichfork
);
5939 * If there are not extents, or split_fsb lies in a hole we are done.
5941 if (!xfs_iext_lookup_extent(ip
, ifp
, split_fsb
, &icur
, &got
) ||
5942 got
.br_startoff
>= split_fsb
)
5945 gotblkcnt
= split_fsb
- got
.br_startoff
;
5946 new.br_startoff
= split_fsb
;
5947 new.br_startblock
= got
.br_startblock
+ gotblkcnt
;
5948 new.br_blockcount
= got
.br_blockcount
- gotblkcnt
;
5949 new.br_state
= got
.br_state
;
5951 if (ifp
->if_flags
& XFS_IFBROOT
) {
5952 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
5953 cur
->bc_private
.b
.flags
= 0;
5954 error
= xfs_bmbt_lookup_eq(cur
, &got
, &i
);
5957 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, del_cursor
);
5960 got
.br_blockcount
= gotblkcnt
;
5961 xfs_iext_update_extent(ip
, xfs_bmap_fork_to_state(whichfork
), &icur
,
5964 logflags
= XFS_ILOG_CORE
;
5966 error
= xfs_bmbt_update(cur
, &got
);
5970 logflags
|= XFS_ILOG_DEXT
;
5972 /* Add new extent */
5973 xfs_iext_next(ifp
, &icur
);
5974 xfs_iext_insert(ip
, &icur
, &new, 0);
5975 XFS_IFORK_NEXT_SET(ip
, whichfork
,
5976 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
5979 error
= xfs_bmbt_lookup_eq(cur
, &new, &i
);
5982 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, del_cursor
);
5983 error
= xfs_btree_insert(cur
, &i
);
5986 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, del_cursor
);
5990 * Convert to a btree if necessary.
5992 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
5993 int tmp_logflags
; /* partial log flag return val */
5995 ASSERT(cur
== NULL
);
5996 error
= xfs_bmap_extents_to_btree(tp
, ip
, &cur
, 0,
5997 &tmp_logflags
, whichfork
);
5998 logflags
|= tmp_logflags
;
6003 cur
->bc_private
.b
.allocated
= 0;
6004 xfs_btree_del_cursor(cur
, error
);
6008 xfs_trans_log_inode(tp
, ip
, logflags
);
6013 xfs_bmap_split_extent(
6014 struct xfs_inode
*ip
,
6015 xfs_fileoff_t split_fsb
)
6017 struct xfs_mount
*mp
= ip
->i_mount
;
6018 struct xfs_trans
*tp
;
6021 error
= xfs_trans_alloc(mp
, &M_RES(mp
)->tr_write
,
6022 XFS_DIOSTRAT_SPACE_RES(mp
, 0), 0, 0, &tp
);
6026 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
6027 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
);
6029 error
= xfs_bmap_split_extent_at(tp
, ip
, split_fsb
);
6033 return xfs_trans_commit(tp
);
6036 xfs_trans_cancel(tp
);
6040 /* Deferred mapping is only for real extents in the data fork. */
6042 xfs_bmap_is_update_needed(
6043 struct xfs_bmbt_irec
*bmap
)
6045 return bmap
->br_startblock
!= HOLESTARTBLOCK
&&
6046 bmap
->br_startblock
!= DELAYSTARTBLOCK
;
6049 /* Record a bmap intent. */
6052 struct xfs_trans
*tp
,
6053 enum xfs_bmap_intent_type type
,
6054 struct xfs_inode
*ip
,
6056 struct xfs_bmbt_irec
*bmap
)
6058 struct xfs_bmap_intent
*bi
;
6060 trace_xfs_bmap_defer(tp
->t_mountp
,
6061 XFS_FSB_TO_AGNO(tp
->t_mountp
, bmap
->br_startblock
),
6063 XFS_FSB_TO_AGBNO(tp
->t_mountp
, bmap
->br_startblock
),
6064 ip
->i_ino
, whichfork
,
6066 bmap
->br_blockcount
,
6069 bi
= kmem_alloc(sizeof(struct xfs_bmap_intent
), KM_SLEEP
| KM_NOFS
);
6070 INIT_LIST_HEAD(&bi
->bi_list
);
6073 bi
->bi_whichfork
= whichfork
;
6074 bi
->bi_bmap
= *bmap
;
6076 xfs_defer_add(tp
, XFS_DEFER_OPS_TYPE_BMAP
, &bi
->bi_list
);
6080 /* Map an extent into a file. */
6082 xfs_bmap_map_extent(
6083 struct xfs_trans
*tp
,
6084 struct xfs_inode
*ip
,
6085 struct xfs_bmbt_irec
*PREV
)
6087 if (!xfs_bmap_is_update_needed(PREV
))
6090 return __xfs_bmap_add(tp
, XFS_BMAP_MAP
, ip
, XFS_DATA_FORK
, PREV
);
6093 /* Unmap an extent out of a file. */
6095 xfs_bmap_unmap_extent(
6096 struct xfs_trans
*tp
,
6097 struct xfs_inode
*ip
,
6098 struct xfs_bmbt_irec
*PREV
)
6100 if (!xfs_bmap_is_update_needed(PREV
))
6103 return __xfs_bmap_add(tp
, XFS_BMAP_UNMAP
, ip
, XFS_DATA_FORK
, PREV
);
6107 * Process one of the deferred bmap operations. We pass back the
6108 * btree cursor to maintain our lock on the bmapbt between calls.
6111 xfs_bmap_finish_one(
6112 struct xfs_trans
*tp
,
6113 struct xfs_inode
*ip
,
6114 enum xfs_bmap_intent_type type
,
6116 xfs_fileoff_t startoff
,
6117 xfs_fsblock_t startblock
,
6118 xfs_filblks_t
*blockcount
,
6123 ASSERT(tp
->t_firstblock
== NULLFSBLOCK
);
6125 trace_xfs_bmap_deferred(tp
->t_mountp
,
6126 XFS_FSB_TO_AGNO(tp
->t_mountp
, startblock
), type
,
6127 XFS_FSB_TO_AGBNO(tp
->t_mountp
, startblock
),
6128 ip
->i_ino
, whichfork
, startoff
, *blockcount
, state
);
6130 if (WARN_ON_ONCE(whichfork
!= XFS_DATA_FORK
))
6131 return -EFSCORRUPTED
;
6133 if (XFS_TEST_ERROR(false, tp
->t_mountp
,
6134 XFS_ERRTAG_BMAP_FINISH_ONE
))
6139 error
= xfs_bmapi_remap(tp
, ip
, startoff
, *blockcount
,
6143 case XFS_BMAP_UNMAP
:
6144 error
= __xfs_bunmapi(tp
, ip
, startoff
, blockcount
,
6145 XFS_BMAPI_REMAP
, 1);
6149 error
= -EFSCORRUPTED
;
6155 /* Check that an inode's extent does not have invalid flags or bad ranges. */
6157 xfs_bmap_validate_extent(
6158 struct xfs_inode
*ip
,
6160 struct xfs_bmbt_irec
*irec
)
6162 struct xfs_mount
*mp
= ip
->i_mount
;
6163 xfs_fsblock_t endfsb
;
6166 isrt
= XFS_IS_REALTIME_INODE(ip
);
6167 endfsb
= irec
->br_startblock
+ irec
->br_blockcount
- 1;
6169 if (!xfs_verify_rtbno(mp
, irec
->br_startblock
))
6170 return __this_address
;
6171 if (!xfs_verify_rtbno(mp
, endfsb
))
6172 return __this_address
;
6174 if (!xfs_verify_fsbno(mp
, irec
->br_startblock
))
6175 return __this_address
;
6176 if (!xfs_verify_fsbno(mp
, endfsb
))
6177 return __this_address
;
6178 if (XFS_FSB_TO_AGNO(mp
, irec
->br_startblock
) !=
6179 XFS_FSB_TO_AGNO(mp
, endfsb
))
6180 return __this_address
;
6182 if (irec
->br_state
!= XFS_EXT_NORM
&& whichfork
!= XFS_DATA_FORK
)
6183 return __this_address
;