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
);
1185 if (unlikely(level
== 0)) {
1186 XFS_ERROR_REPORT(__func__
, XFS_ERRLEVEL_LOW
, mp
);
1187 return -EFSCORRUPTED
;
1189 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, 1, ifp
->if_broot_bytes
);
1190 bno
= be64_to_cpu(*pp
);
1193 * Go down the tree until leaf level is reached, following the first
1194 * pointer (leftmost) at each level.
1196 while (level
-- > 0) {
1197 error
= xfs_btree_read_bufl(mp
, tp
, bno
, 0, &bp
,
1198 XFS_BMAP_BTREE_REF
, &xfs_bmbt_buf_ops
);
1201 block
= XFS_BUF_TO_BLOCK(bp
);
1204 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, mp
->m_bmap_dmxr
[1]);
1205 bno
= be64_to_cpu(*pp
);
1206 XFS_WANT_CORRUPTED_GOTO(mp
,
1207 xfs_verify_fsbno(mp
, bno
), out_brelse
);
1208 xfs_trans_brelse(tp
, bp
);
1212 * Here with bp and block set to the leftmost leaf node in the tree.
1215 xfs_iext_first(ifp
, &icur
);
1218 * Loop over all leaf nodes. Copy information to the extent records.
1221 xfs_bmbt_rec_t
*frp
;
1222 xfs_fsblock_t nextbno
;
1223 xfs_extnum_t num_recs
;
1225 num_recs
= xfs_btree_get_numrecs(block
);
1226 if (unlikely(i
+ num_recs
> nextents
)) {
1227 xfs_warn(ip
->i_mount
,
1228 "corrupt dinode %Lu, (btree extents).",
1229 (unsigned long long) ip
->i_ino
);
1230 xfs_inode_verifier_error(ip
, -EFSCORRUPTED
,
1231 __func__
, block
, sizeof(*block
),
1233 error
= -EFSCORRUPTED
;
1237 * Read-ahead the next leaf block, if any.
1239 nextbno
= be64_to_cpu(block
->bb_u
.l
.bb_rightsib
);
1240 if (nextbno
!= NULLFSBLOCK
)
1241 xfs_btree_reada_bufl(mp
, nextbno
, 1,
1244 * Copy records into the extent records.
1246 frp
= XFS_BMBT_REC_ADDR(mp
, block
, 1);
1247 for (j
= 0; j
< num_recs
; j
++, frp
++, i
++) {
1250 xfs_bmbt_disk_get_all(frp
, &new);
1251 fa
= xfs_bmap_validate_extent(ip
, whichfork
, &new);
1253 error
= -EFSCORRUPTED
;
1254 xfs_inode_verifier_error(ip
, error
,
1255 "xfs_iread_extents(2)",
1256 frp
, sizeof(*frp
), fa
);
1259 xfs_iext_insert(ip
, &icur
, &new, state
);
1260 trace_xfs_read_extent(ip
, &icur
, state
, _THIS_IP_
);
1261 xfs_iext_next(ifp
, &icur
);
1263 xfs_trans_brelse(tp
, bp
);
1266 * If we've reached the end, stop.
1268 if (bno
== NULLFSBLOCK
)
1270 error
= xfs_btree_read_bufl(mp
, tp
, bno
, 0, &bp
,
1271 XFS_BMAP_BTREE_REF
, &xfs_bmbt_buf_ops
);
1274 block
= XFS_BUF_TO_BLOCK(bp
);
1277 if (i
!= XFS_IFORK_NEXTENTS(ip
, whichfork
)) {
1278 error
= -EFSCORRUPTED
;
1281 ASSERT(i
== xfs_iext_count(ifp
));
1283 ifp
->if_flags
|= XFS_IFEXTENTS
;
1287 xfs_trans_brelse(tp
, bp
);
1289 xfs_iext_destroy(ifp
);
1294 * Returns the relative block number of the first unused block(s) in the given
1295 * fork with at least "len" logically contiguous blocks free. This is the
1296 * lowest-address hole if the fork has holes, else the first block past the end
1297 * of fork. Return 0 if the fork is currently local (in-inode).
1300 xfs_bmap_first_unused(
1301 struct xfs_trans
*tp
, /* transaction pointer */
1302 struct xfs_inode
*ip
, /* incore inode */
1303 xfs_extlen_t len
, /* size of hole to find */
1304 xfs_fileoff_t
*first_unused
, /* unused block */
1305 int whichfork
) /* data or attr fork */
1307 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1308 struct xfs_bmbt_irec got
;
1309 struct xfs_iext_cursor icur
;
1310 xfs_fileoff_t lastaddr
= 0;
1311 xfs_fileoff_t lowest
, max
;
1314 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
||
1315 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
||
1316 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
1318 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
) {
1323 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
1324 error
= xfs_iread_extents(tp
, ip
, whichfork
);
1329 lowest
= max
= *first_unused
;
1330 for_each_xfs_iext(ifp
, &icur
, &got
) {
1332 * See if the hole before this extent will work.
1334 if (got
.br_startoff
>= lowest
+ len
&&
1335 got
.br_startoff
- max
>= len
)
1337 lastaddr
= got
.br_startoff
+ got
.br_blockcount
;
1338 max
= XFS_FILEOFF_MAX(lastaddr
, lowest
);
1341 *first_unused
= max
;
1346 * Returns the file-relative block number of the last block - 1 before
1347 * last_block (input value) in the file.
1348 * This is not based on i_size, it is based on the extent records.
1349 * Returns 0 for local files, as they do not have extent records.
1352 xfs_bmap_last_before(
1353 struct xfs_trans
*tp
, /* transaction pointer */
1354 struct xfs_inode
*ip
, /* incore inode */
1355 xfs_fileoff_t
*last_block
, /* last block */
1356 int whichfork
) /* data or attr fork */
1358 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1359 struct xfs_bmbt_irec got
;
1360 struct xfs_iext_cursor icur
;
1363 switch (XFS_IFORK_FORMAT(ip
, whichfork
)) {
1364 case XFS_DINODE_FMT_LOCAL
:
1367 case XFS_DINODE_FMT_BTREE
:
1368 case XFS_DINODE_FMT_EXTENTS
:
1374 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
1375 error
= xfs_iread_extents(tp
, ip
, whichfork
);
1380 if (!xfs_iext_lookup_extent_before(ip
, ifp
, last_block
, &icur
, &got
))
1386 xfs_bmap_last_extent(
1387 struct xfs_trans
*tp
,
1388 struct xfs_inode
*ip
,
1390 struct xfs_bmbt_irec
*rec
,
1393 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1394 struct xfs_iext_cursor icur
;
1397 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
1398 error
= xfs_iread_extents(tp
, ip
, whichfork
);
1403 xfs_iext_last(ifp
, &icur
);
1404 if (!xfs_iext_get_extent(ifp
, &icur
, rec
))
1412 * Check the last inode extent to determine whether this allocation will result
1413 * in blocks being allocated at the end of the file. When we allocate new data
1414 * blocks at the end of the file which do not start at the previous data block,
1415 * we will try to align the new blocks at stripe unit boundaries.
1417 * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be
1418 * at, or past the EOF.
1422 struct xfs_bmalloca
*bma
,
1425 struct xfs_bmbt_irec rec
;
1430 error
= xfs_bmap_last_extent(NULL
, bma
->ip
, whichfork
, &rec
,
1441 * Check if we are allocation or past the last extent, or at least into
1442 * the last delayed allocated extent.
1444 bma
->aeof
= bma
->offset
>= rec
.br_startoff
+ rec
.br_blockcount
||
1445 (bma
->offset
>= rec
.br_startoff
&&
1446 isnullstartblock(rec
.br_startblock
));
1451 * Returns the file-relative block number of the first block past eof in
1452 * the file. This is not based on i_size, it is based on the extent records.
1453 * Returns 0 for local files, as they do not have extent records.
1456 xfs_bmap_last_offset(
1457 struct xfs_inode
*ip
,
1458 xfs_fileoff_t
*last_block
,
1461 struct xfs_bmbt_irec rec
;
1467 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
)
1470 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
&&
1471 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
1474 error
= xfs_bmap_last_extent(NULL
, ip
, whichfork
, &rec
, &is_empty
);
1475 if (error
|| is_empty
)
1478 *last_block
= rec
.br_startoff
+ rec
.br_blockcount
;
1483 * Returns whether the selected fork of the inode has exactly one
1484 * block or not. For the data fork we check this matches di_size,
1485 * implying the file's range is 0..bsize-1.
1487 int /* 1=>1 block, 0=>otherwise */
1489 xfs_inode_t
*ip
, /* incore inode */
1490 int whichfork
) /* data or attr fork */
1492 struct xfs_ifork
*ifp
; /* inode fork pointer */
1493 int rval
; /* return value */
1494 xfs_bmbt_irec_t s
; /* internal version of extent */
1495 struct xfs_iext_cursor icur
;
1498 if (whichfork
== XFS_DATA_FORK
)
1499 return XFS_ISIZE(ip
) == ip
->i_mount
->m_sb
.sb_blocksize
;
1501 if (XFS_IFORK_NEXTENTS(ip
, whichfork
) != 1)
1503 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
1505 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1506 ASSERT(ifp
->if_flags
& XFS_IFEXTENTS
);
1507 xfs_iext_first(ifp
, &icur
);
1508 xfs_iext_get_extent(ifp
, &icur
, &s
);
1509 rval
= s
.br_startoff
== 0 && s
.br_blockcount
== 1;
1510 if (rval
&& whichfork
== XFS_DATA_FORK
)
1511 ASSERT(XFS_ISIZE(ip
) == ip
->i_mount
->m_sb
.sb_blocksize
);
1516 * Extent tree manipulation functions used during allocation.
1520 * Convert a delayed allocation to a real allocation.
1522 STATIC
int /* error */
1523 xfs_bmap_add_extent_delay_real(
1524 struct xfs_bmalloca
*bma
,
1527 struct xfs_bmbt_irec
*new = &bma
->got
;
1528 int error
; /* error return value */
1529 int i
; /* temp state */
1530 struct xfs_ifork
*ifp
; /* inode fork pointer */
1531 xfs_fileoff_t new_endoff
; /* end offset of new entry */
1532 xfs_bmbt_irec_t r
[3]; /* neighbor extent entries */
1533 /* left is 0, right is 1, prev is 2 */
1534 int rval
=0; /* return value (logging flags) */
1535 int state
= xfs_bmap_fork_to_state(whichfork
);
1536 xfs_filblks_t da_new
; /* new count del alloc blocks used */
1537 xfs_filblks_t da_old
; /* old count del alloc blocks used */
1538 xfs_filblks_t temp
=0; /* value for da_new calculations */
1539 int tmp_rval
; /* partial logging flags */
1540 struct xfs_mount
*mp
;
1541 xfs_extnum_t
*nextents
;
1542 struct xfs_bmbt_irec old
;
1544 mp
= bma
->ip
->i_mount
;
1545 ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
1546 ASSERT(whichfork
!= XFS_ATTR_FORK
);
1547 nextents
= (whichfork
== XFS_COW_FORK
? &bma
->ip
->i_cnextents
:
1548 &bma
->ip
->i_d
.di_nextents
);
1550 ASSERT(!isnullstartblock(new->br_startblock
));
1552 (bma
->cur
->bc_private
.b
.flags
& XFS_BTCUR_BPRV_WASDEL
));
1554 XFS_STATS_INC(mp
, xs_add_exlist
);
1561 * Set up a bunch of variables to make the tests simpler.
1563 xfs_iext_get_extent(ifp
, &bma
->icur
, &PREV
);
1564 new_endoff
= new->br_startoff
+ new->br_blockcount
;
1565 ASSERT(isnullstartblock(PREV
.br_startblock
));
1566 ASSERT(PREV
.br_startoff
<= new->br_startoff
);
1567 ASSERT(PREV
.br_startoff
+ PREV
.br_blockcount
>= new_endoff
);
1569 da_old
= startblockval(PREV
.br_startblock
);
1573 * Set flags determining what part of the previous delayed allocation
1574 * extent is being replaced by a real allocation.
1576 if (PREV
.br_startoff
== new->br_startoff
)
1577 state
|= BMAP_LEFT_FILLING
;
1578 if (PREV
.br_startoff
+ PREV
.br_blockcount
== new_endoff
)
1579 state
|= BMAP_RIGHT_FILLING
;
1582 * Check and set flags if this segment has a left neighbor.
1583 * Don't set contiguous if the combined extent would be too large.
1585 if (xfs_iext_peek_prev_extent(ifp
, &bma
->icur
, &LEFT
)) {
1586 state
|= BMAP_LEFT_VALID
;
1587 if (isnullstartblock(LEFT
.br_startblock
))
1588 state
|= BMAP_LEFT_DELAY
;
1591 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
1592 LEFT
.br_startoff
+ LEFT
.br_blockcount
== new->br_startoff
&&
1593 LEFT
.br_startblock
+ LEFT
.br_blockcount
== new->br_startblock
&&
1594 LEFT
.br_state
== new->br_state
&&
1595 LEFT
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
1596 state
|= BMAP_LEFT_CONTIG
;
1599 * Check and set flags if this segment has a right neighbor.
1600 * Don't set contiguous if the combined extent would be too large.
1601 * Also check for all-three-contiguous being too large.
1603 if (xfs_iext_peek_next_extent(ifp
, &bma
->icur
, &RIGHT
)) {
1604 state
|= BMAP_RIGHT_VALID
;
1605 if (isnullstartblock(RIGHT
.br_startblock
))
1606 state
|= BMAP_RIGHT_DELAY
;
1609 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
1610 new_endoff
== RIGHT
.br_startoff
&&
1611 new->br_startblock
+ new->br_blockcount
== RIGHT
.br_startblock
&&
1612 new->br_state
== RIGHT
.br_state
&&
1613 new->br_blockcount
+ RIGHT
.br_blockcount
<= MAXEXTLEN
&&
1614 ((state
& (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
1615 BMAP_RIGHT_FILLING
)) !=
1616 (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
1617 BMAP_RIGHT_FILLING
) ||
1618 LEFT
.br_blockcount
+ new->br_blockcount
+ RIGHT
.br_blockcount
1620 state
|= BMAP_RIGHT_CONTIG
;
1624 * Switch out based on the FILLING and CONTIG state bits.
1626 switch (state
& (BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
1627 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
)) {
1628 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
1629 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
1631 * Filling in all of a previously delayed allocation extent.
1632 * The left and right neighbors are both contiguous with new.
1634 LEFT
.br_blockcount
+= PREV
.br_blockcount
+ RIGHT
.br_blockcount
;
1636 xfs_iext_remove(bma
->ip
, &bma
->icur
, state
);
1637 xfs_iext_remove(bma
->ip
, &bma
->icur
, state
);
1638 xfs_iext_prev(ifp
, &bma
->icur
);
1639 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &LEFT
);
1642 if (bma
->cur
== NULL
)
1643 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1645 rval
= XFS_ILOG_CORE
;
1646 error
= xfs_bmbt_lookup_eq(bma
->cur
, &RIGHT
, &i
);
1649 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1650 error
= xfs_btree_delete(bma
->cur
, &i
);
1653 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1654 error
= xfs_btree_decrement(bma
->cur
, 0, &i
);
1657 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1658 error
= xfs_bmbt_update(bma
->cur
, &LEFT
);
1664 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
1666 * Filling in all of a previously delayed allocation extent.
1667 * The left neighbor is contiguous, the right is not.
1670 LEFT
.br_blockcount
+= PREV
.br_blockcount
;
1672 xfs_iext_remove(bma
->ip
, &bma
->icur
, state
);
1673 xfs_iext_prev(ifp
, &bma
->icur
);
1674 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &LEFT
);
1676 if (bma
->cur
== NULL
)
1677 rval
= XFS_ILOG_DEXT
;
1680 error
= xfs_bmbt_lookup_eq(bma
->cur
, &old
, &i
);
1683 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1684 error
= xfs_bmbt_update(bma
->cur
, &LEFT
);
1690 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
1692 * Filling in all of a previously delayed allocation extent.
1693 * The right neighbor is contiguous, the left is not. Take care
1694 * with delay -> unwritten extent allocation here because the
1695 * delalloc record we are overwriting is always written.
1697 PREV
.br_startblock
= new->br_startblock
;
1698 PREV
.br_blockcount
+= RIGHT
.br_blockcount
;
1699 PREV
.br_state
= new->br_state
;
1701 xfs_iext_next(ifp
, &bma
->icur
);
1702 xfs_iext_remove(bma
->ip
, &bma
->icur
, state
);
1703 xfs_iext_prev(ifp
, &bma
->icur
);
1704 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &PREV
);
1706 if (bma
->cur
== NULL
)
1707 rval
= XFS_ILOG_DEXT
;
1710 error
= xfs_bmbt_lookup_eq(bma
->cur
, &RIGHT
, &i
);
1713 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1714 error
= xfs_bmbt_update(bma
->cur
, &PREV
);
1720 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
1722 * Filling in all of a previously delayed allocation extent.
1723 * Neither the left nor right neighbors are contiguous with
1726 PREV
.br_startblock
= new->br_startblock
;
1727 PREV
.br_state
= new->br_state
;
1728 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &PREV
);
1731 if (bma
->cur
== NULL
)
1732 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1734 rval
= XFS_ILOG_CORE
;
1735 error
= xfs_bmbt_lookup_eq(bma
->cur
, new, &i
);
1738 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
1739 error
= xfs_btree_insert(bma
->cur
, &i
);
1742 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1746 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
:
1748 * Filling in the first part of a previous delayed allocation.
1749 * The left neighbor is contiguous.
1752 temp
= PREV
.br_blockcount
- new->br_blockcount
;
1753 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
1754 startblockval(PREV
.br_startblock
));
1756 LEFT
.br_blockcount
+= new->br_blockcount
;
1758 PREV
.br_blockcount
= temp
;
1759 PREV
.br_startoff
+= new->br_blockcount
;
1760 PREV
.br_startblock
= nullstartblock(da_new
);
1762 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &PREV
);
1763 xfs_iext_prev(ifp
, &bma
->icur
);
1764 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &LEFT
);
1766 if (bma
->cur
== NULL
)
1767 rval
= XFS_ILOG_DEXT
;
1770 error
= xfs_bmbt_lookup_eq(bma
->cur
, &old
, &i
);
1773 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1774 error
= xfs_bmbt_update(bma
->cur
, &LEFT
);
1780 case BMAP_LEFT_FILLING
:
1782 * Filling in the first part of a previous delayed allocation.
1783 * The left neighbor is not contiguous.
1785 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, new);
1787 if (bma
->cur
== NULL
)
1788 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1790 rval
= XFS_ILOG_CORE
;
1791 error
= xfs_bmbt_lookup_eq(bma
->cur
, new, &i
);
1794 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
1795 error
= xfs_btree_insert(bma
->cur
, &i
);
1798 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1801 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
1802 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
1803 &bma
->cur
, 1, &tmp_rval
, whichfork
);
1809 temp
= PREV
.br_blockcount
- new->br_blockcount
;
1810 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
1811 startblockval(PREV
.br_startblock
) -
1812 (bma
->cur
? bma
->cur
->bc_private
.b
.allocated
: 0));
1814 PREV
.br_startoff
= new_endoff
;
1815 PREV
.br_blockcount
= temp
;
1816 PREV
.br_startblock
= nullstartblock(da_new
);
1817 xfs_iext_next(ifp
, &bma
->icur
);
1818 xfs_iext_insert(bma
->ip
, &bma
->icur
, &PREV
, state
);
1819 xfs_iext_prev(ifp
, &bma
->icur
);
1822 case BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
1824 * Filling in the last part of a previous delayed allocation.
1825 * The right neighbor is contiguous with the new allocation.
1828 RIGHT
.br_startoff
= new->br_startoff
;
1829 RIGHT
.br_startblock
= new->br_startblock
;
1830 RIGHT
.br_blockcount
+= new->br_blockcount
;
1832 if (bma
->cur
== NULL
)
1833 rval
= XFS_ILOG_DEXT
;
1836 error
= xfs_bmbt_lookup_eq(bma
->cur
, &old
, &i
);
1839 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1840 error
= xfs_bmbt_update(bma
->cur
, &RIGHT
);
1845 temp
= PREV
.br_blockcount
- new->br_blockcount
;
1846 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
1847 startblockval(PREV
.br_startblock
));
1849 PREV
.br_blockcount
= temp
;
1850 PREV
.br_startblock
= nullstartblock(da_new
);
1852 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &PREV
);
1853 xfs_iext_next(ifp
, &bma
->icur
);
1854 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &RIGHT
);
1857 case BMAP_RIGHT_FILLING
:
1859 * Filling in the last part of a previous delayed allocation.
1860 * The right neighbor is not contiguous.
1862 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, new);
1864 if (bma
->cur
== NULL
)
1865 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1867 rval
= XFS_ILOG_CORE
;
1868 error
= xfs_bmbt_lookup_eq(bma
->cur
, new, &i
);
1871 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
1872 error
= xfs_btree_insert(bma
->cur
, &i
);
1875 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1878 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
1879 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
1880 &bma
->cur
, 1, &tmp_rval
, whichfork
);
1886 temp
= PREV
.br_blockcount
- new->br_blockcount
;
1887 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
1888 startblockval(PREV
.br_startblock
) -
1889 (bma
->cur
? bma
->cur
->bc_private
.b
.allocated
: 0));
1891 PREV
.br_startblock
= nullstartblock(da_new
);
1892 PREV
.br_blockcount
= temp
;
1893 xfs_iext_insert(bma
->ip
, &bma
->icur
, &PREV
, state
);
1894 xfs_iext_next(ifp
, &bma
->icur
);
1899 * Filling in the middle part of a previous delayed allocation.
1900 * Contiguity is impossible here.
1901 * This case is avoided almost all the time.
1903 * We start with a delayed allocation:
1905 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
1908 * and we are allocating:
1909 * +rrrrrrrrrrrrrrrrr+
1912 * and we set it up for insertion as:
1913 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
1915 * PREV @ idx LEFT RIGHT
1916 * inserted at idx + 1
1920 /* LEFT is the new middle */
1923 /* RIGHT is the new right */
1924 RIGHT
.br_state
= PREV
.br_state
;
1925 RIGHT
.br_startoff
= new_endoff
;
1926 RIGHT
.br_blockcount
=
1927 PREV
.br_startoff
+ PREV
.br_blockcount
- new_endoff
;
1928 RIGHT
.br_startblock
=
1929 nullstartblock(xfs_bmap_worst_indlen(bma
->ip
,
1930 RIGHT
.br_blockcount
));
1933 PREV
.br_blockcount
= new->br_startoff
- PREV
.br_startoff
;
1934 PREV
.br_startblock
=
1935 nullstartblock(xfs_bmap_worst_indlen(bma
->ip
,
1936 PREV
.br_blockcount
));
1937 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &PREV
);
1939 xfs_iext_next(ifp
, &bma
->icur
);
1940 xfs_iext_insert(bma
->ip
, &bma
->icur
, &RIGHT
, state
);
1941 xfs_iext_insert(bma
->ip
, &bma
->icur
, &LEFT
, state
);
1944 if (bma
->cur
== NULL
)
1945 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1947 rval
= XFS_ILOG_CORE
;
1948 error
= xfs_bmbt_lookup_eq(bma
->cur
, new, &i
);
1951 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
1952 error
= xfs_btree_insert(bma
->cur
, &i
);
1955 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1958 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
1959 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
1960 &bma
->cur
, 1, &tmp_rval
, whichfork
);
1966 da_new
= startblockval(PREV
.br_startblock
) +
1967 startblockval(RIGHT
.br_startblock
);
1970 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
1971 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
1972 case BMAP_LEFT_FILLING
| BMAP_RIGHT_CONTIG
:
1973 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
1974 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
1975 case BMAP_LEFT_CONTIG
:
1976 case BMAP_RIGHT_CONTIG
:
1978 * These cases are all impossible.
1983 /* add reverse mapping unless caller opted out */
1984 if (!(bma
->flags
& XFS_BMAPI_NORMAP
)) {
1985 error
= xfs_rmap_map_extent(bma
->tp
, bma
->ip
, whichfork
, new);
1990 /* convert to a btree if necessary */
1991 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
1992 int tmp_logflags
; /* partial log flag return val */
1994 ASSERT(bma
->cur
== NULL
);
1995 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
1996 &bma
->cur
, da_old
> 0, &tmp_logflags
,
1998 bma
->logflags
|= tmp_logflags
;
2003 if (da_new
!= da_old
)
2004 xfs_mod_delalloc(mp
, (int64_t)da_new
- da_old
);
2007 da_new
+= bma
->cur
->bc_private
.b
.allocated
;
2008 bma
->cur
->bc_private
.b
.allocated
= 0;
2011 /* adjust for changes in reserved delayed indirect blocks */
2012 if (da_new
!= da_old
) {
2013 ASSERT(state
== 0 || da_new
< da_old
);
2014 error
= xfs_mod_fdblocks(mp
, (int64_t)(da_old
- da_new
),
2018 xfs_bmap_check_leaf_extents(bma
->cur
, bma
->ip
, whichfork
);
2020 if (whichfork
!= XFS_COW_FORK
)
2021 bma
->logflags
|= rval
;
2029 * Convert an unwritten allocation to a real allocation or vice versa.
2032 xfs_bmap_add_extent_unwritten_real(
2033 struct xfs_trans
*tp
,
2034 xfs_inode_t
*ip
, /* incore inode pointer */
2036 struct xfs_iext_cursor
*icur
,
2037 xfs_btree_cur_t
**curp
, /* if *curp is null, not a btree */
2038 xfs_bmbt_irec_t
*new, /* new data to add to file extents */
2039 int *logflagsp
) /* inode logging flags */
2041 xfs_btree_cur_t
*cur
; /* btree cursor */
2042 int error
; /* error return value */
2043 int i
; /* temp state */
2044 struct xfs_ifork
*ifp
; /* inode fork pointer */
2045 xfs_fileoff_t new_endoff
; /* end offset of new entry */
2046 xfs_bmbt_irec_t r
[3]; /* neighbor extent entries */
2047 /* left is 0, right is 1, prev is 2 */
2048 int rval
=0; /* return value (logging flags) */
2049 int state
= xfs_bmap_fork_to_state(whichfork
);
2050 struct xfs_mount
*mp
= ip
->i_mount
;
2051 struct xfs_bmbt_irec old
;
2056 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
2058 ASSERT(!isnullstartblock(new->br_startblock
));
2060 XFS_STATS_INC(mp
, xs_add_exlist
);
2067 * Set up a bunch of variables to make the tests simpler.
2070 xfs_iext_get_extent(ifp
, icur
, &PREV
);
2071 ASSERT(new->br_state
!= PREV
.br_state
);
2072 new_endoff
= new->br_startoff
+ new->br_blockcount
;
2073 ASSERT(PREV
.br_startoff
<= new->br_startoff
);
2074 ASSERT(PREV
.br_startoff
+ PREV
.br_blockcount
>= new_endoff
);
2077 * Set flags determining what part of the previous oldext allocation
2078 * extent is being replaced by a newext allocation.
2080 if (PREV
.br_startoff
== new->br_startoff
)
2081 state
|= BMAP_LEFT_FILLING
;
2082 if (PREV
.br_startoff
+ PREV
.br_blockcount
== new_endoff
)
2083 state
|= BMAP_RIGHT_FILLING
;
2086 * Check and set flags if this segment has a left neighbor.
2087 * Don't set contiguous if the combined extent would be too large.
2089 if (xfs_iext_peek_prev_extent(ifp
, icur
, &LEFT
)) {
2090 state
|= BMAP_LEFT_VALID
;
2091 if (isnullstartblock(LEFT
.br_startblock
))
2092 state
|= BMAP_LEFT_DELAY
;
2095 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
2096 LEFT
.br_startoff
+ LEFT
.br_blockcount
== new->br_startoff
&&
2097 LEFT
.br_startblock
+ LEFT
.br_blockcount
== new->br_startblock
&&
2098 LEFT
.br_state
== new->br_state
&&
2099 LEFT
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
2100 state
|= BMAP_LEFT_CONTIG
;
2103 * Check and set flags if this segment has a right neighbor.
2104 * Don't set contiguous if the combined extent would be too large.
2105 * Also check for all-three-contiguous being too large.
2107 if (xfs_iext_peek_next_extent(ifp
, icur
, &RIGHT
)) {
2108 state
|= BMAP_RIGHT_VALID
;
2109 if (isnullstartblock(RIGHT
.br_startblock
))
2110 state
|= BMAP_RIGHT_DELAY
;
2113 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
2114 new_endoff
== RIGHT
.br_startoff
&&
2115 new->br_startblock
+ new->br_blockcount
== RIGHT
.br_startblock
&&
2116 new->br_state
== RIGHT
.br_state
&&
2117 new->br_blockcount
+ RIGHT
.br_blockcount
<= MAXEXTLEN
&&
2118 ((state
& (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
2119 BMAP_RIGHT_FILLING
)) !=
2120 (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
2121 BMAP_RIGHT_FILLING
) ||
2122 LEFT
.br_blockcount
+ new->br_blockcount
+ RIGHT
.br_blockcount
2124 state
|= BMAP_RIGHT_CONTIG
;
2127 * Switch out based on the FILLING and CONTIG state bits.
2129 switch (state
& (BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
2130 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
)) {
2131 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
2132 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2134 * Setting all of a previous oldext extent to newext.
2135 * The left and right neighbors are both contiguous with new.
2137 LEFT
.br_blockcount
+= PREV
.br_blockcount
+ RIGHT
.br_blockcount
;
2139 xfs_iext_remove(ip
, icur
, state
);
2140 xfs_iext_remove(ip
, icur
, state
);
2141 xfs_iext_prev(ifp
, icur
);
2142 xfs_iext_update_extent(ip
, state
, icur
, &LEFT
);
2143 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2144 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 2);
2146 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2148 rval
= XFS_ILOG_CORE
;
2149 error
= xfs_bmbt_lookup_eq(cur
, &RIGHT
, &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 if ((error
= xfs_btree_delete(cur
, &i
)))
2161 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2162 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2164 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2165 error
= xfs_bmbt_update(cur
, &LEFT
);
2171 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
2173 * Setting all of a previous oldext extent to newext.
2174 * The left neighbor is contiguous, the right is not.
2176 LEFT
.br_blockcount
+= PREV
.br_blockcount
;
2178 xfs_iext_remove(ip
, icur
, state
);
2179 xfs_iext_prev(ifp
, icur
);
2180 xfs_iext_update_extent(ip
, state
, icur
, &LEFT
);
2181 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2182 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
2184 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2186 rval
= XFS_ILOG_CORE
;
2187 error
= xfs_bmbt_lookup_eq(cur
, &PREV
, &i
);
2190 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2191 if ((error
= xfs_btree_delete(cur
, &i
)))
2193 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2194 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2196 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2197 error
= xfs_bmbt_update(cur
, &LEFT
);
2203 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2205 * Setting all of a previous oldext extent to newext.
2206 * The right neighbor is contiguous, the left is not.
2208 PREV
.br_blockcount
+= RIGHT
.br_blockcount
;
2209 PREV
.br_state
= new->br_state
;
2211 xfs_iext_next(ifp
, icur
);
2212 xfs_iext_remove(ip
, icur
, state
);
2213 xfs_iext_prev(ifp
, icur
);
2214 xfs_iext_update_extent(ip
, state
, icur
, &PREV
);
2216 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2217 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
2219 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2221 rval
= XFS_ILOG_CORE
;
2222 error
= xfs_bmbt_lookup_eq(cur
, &RIGHT
, &i
);
2225 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2226 if ((error
= xfs_btree_delete(cur
, &i
)))
2228 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2229 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2231 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2232 error
= xfs_bmbt_update(cur
, &PREV
);
2238 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
2240 * Setting all of a previous oldext extent to newext.
2241 * Neither the left nor right neighbors are contiguous with
2244 PREV
.br_state
= new->br_state
;
2245 xfs_iext_update_extent(ip
, state
, icur
, &PREV
);
2248 rval
= XFS_ILOG_DEXT
;
2251 error
= xfs_bmbt_lookup_eq(cur
, new, &i
);
2254 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2255 error
= xfs_bmbt_update(cur
, &PREV
);
2261 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
:
2263 * Setting the first part of a previous oldext extent to newext.
2264 * The left neighbor is contiguous.
2266 LEFT
.br_blockcount
+= new->br_blockcount
;
2269 PREV
.br_startoff
+= new->br_blockcount
;
2270 PREV
.br_startblock
+= new->br_blockcount
;
2271 PREV
.br_blockcount
-= new->br_blockcount
;
2273 xfs_iext_update_extent(ip
, state
, icur
, &PREV
);
2274 xfs_iext_prev(ifp
, icur
);
2275 xfs_iext_update_extent(ip
, state
, icur
, &LEFT
);
2278 rval
= XFS_ILOG_DEXT
;
2281 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2284 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2285 error
= xfs_bmbt_update(cur
, &PREV
);
2288 error
= xfs_btree_decrement(cur
, 0, &i
);
2291 error
= xfs_bmbt_update(cur
, &LEFT
);
2297 case BMAP_LEFT_FILLING
:
2299 * Setting the first part of a previous oldext extent to newext.
2300 * The left neighbor is not contiguous.
2303 PREV
.br_startoff
+= new->br_blockcount
;
2304 PREV
.br_startblock
+= new->br_blockcount
;
2305 PREV
.br_blockcount
-= new->br_blockcount
;
2307 xfs_iext_update_extent(ip
, state
, icur
, &PREV
);
2308 xfs_iext_insert(ip
, icur
, new, state
);
2309 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2310 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
2312 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2314 rval
= XFS_ILOG_CORE
;
2315 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2318 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2319 error
= xfs_bmbt_update(cur
, &PREV
);
2322 cur
->bc_rec
.b
= *new;
2323 if ((error
= xfs_btree_insert(cur
, &i
)))
2325 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2329 case BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2331 * Setting the last part of a previous oldext extent to newext.
2332 * The right neighbor is contiguous with the new allocation.
2335 PREV
.br_blockcount
-= new->br_blockcount
;
2337 RIGHT
.br_startoff
= new->br_startoff
;
2338 RIGHT
.br_startblock
= new->br_startblock
;
2339 RIGHT
.br_blockcount
+= new->br_blockcount
;
2341 xfs_iext_update_extent(ip
, state
, icur
, &PREV
);
2342 xfs_iext_next(ifp
, icur
);
2343 xfs_iext_update_extent(ip
, state
, icur
, &RIGHT
);
2346 rval
= XFS_ILOG_DEXT
;
2349 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2352 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2353 error
= xfs_bmbt_update(cur
, &PREV
);
2356 error
= xfs_btree_increment(cur
, 0, &i
);
2359 error
= xfs_bmbt_update(cur
, &RIGHT
);
2365 case BMAP_RIGHT_FILLING
:
2367 * Setting the last part of a previous oldext extent to newext.
2368 * The right neighbor is not contiguous.
2371 PREV
.br_blockcount
-= new->br_blockcount
;
2373 xfs_iext_update_extent(ip
, state
, icur
, &PREV
);
2374 xfs_iext_next(ifp
, icur
);
2375 xfs_iext_insert(ip
, icur
, new, state
);
2377 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2378 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
2380 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2382 rval
= XFS_ILOG_CORE
;
2383 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2386 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2387 error
= xfs_bmbt_update(cur
, &PREV
);
2390 error
= xfs_bmbt_lookup_eq(cur
, new, &i
);
2393 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
2394 if ((error
= xfs_btree_insert(cur
, &i
)))
2396 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2402 * Setting the middle part of a previous oldext extent to
2403 * newext. Contiguity is impossible here.
2404 * One extent becomes three extents.
2407 PREV
.br_blockcount
= new->br_startoff
- PREV
.br_startoff
;
2410 r
[1].br_startoff
= new_endoff
;
2411 r
[1].br_blockcount
=
2412 old
.br_startoff
+ old
.br_blockcount
- new_endoff
;
2413 r
[1].br_startblock
= new->br_startblock
+ new->br_blockcount
;
2414 r
[1].br_state
= PREV
.br_state
;
2416 xfs_iext_update_extent(ip
, state
, icur
, &PREV
);
2417 xfs_iext_next(ifp
, icur
);
2418 xfs_iext_insert(ip
, icur
, &r
[1], state
);
2419 xfs_iext_insert(ip
, icur
, &r
[0], state
);
2421 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2422 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 2);
2424 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2426 rval
= XFS_ILOG_CORE
;
2427 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2430 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2431 /* new right extent - oldext */
2432 error
= xfs_bmbt_update(cur
, &r
[1]);
2435 /* new left extent - oldext */
2436 cur
->bc_rec
.b
= PREV
;
2437 if ((error
= xfs_btree_insert(cur
, &i
)))
2439 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2441 * Reset the cursor to the position of the new extent
2442 * we are about to insert as we can't trust it after
2443 * the previous insert.
2445 error
= xfs_bmbt_lookup_eq(cur
, new, &i
);
2448 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
2449 /* new middle extent - newext */
2450 if ((error
= xfs_btree_insert(cur
, &i
)))
2452 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2456 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2457 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2458 case BMAP_LEFT_FILLING
| BMAP_RIGHT_CONTIG
:
2459 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
2460 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2461 case BMAP_LEFT_CONTIG
:
2462 case BMAP_RIGHT_CONTIG
:
2464 * These cases are all impossible.
2469 /* update reverse mappings */
2470 error
= xfs_rmap_convert_extent(mp
, tp
, ip
, whichfork
, new);
2474 /* convert to a btree if necessary */
2475 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
2476 int tmp_logflags
; /* partial log flag return val */
2478 ASSERT(cur
== NULL
);
2479 error
= xfs_bmap_extents_to_btree(tp
, ip
, &cur
, 0,
2480 &tmp_logflags
, whichfork
);
2481 *logflagsp
|= tmp_logflags
;
2486 /* clear out the allocated field, done with it now in any case. */
2488 cur
->bc_private
.b
.allocated
= 0;
2492 xfs_bmap_check_leaf_extents(*curp
, ip
, whichfork
);
2502 * Convert a hole to a delayed allocation.
2505 xfs_bmap_add_extent_hole_delay(
2506 xfs_inode_t
*ip
, /* incore inode pointer */
2508 struct xfs_iext_cursor
*icur
,
2509 xfs_bmbt_irec_t
*new) /* new data to add to file extents */
2511 struct xfs_ifork
*ifp
; /* inode fork pointer */
2512 xfs_bmbt_irec_t left
; /* left neighbor extent entry */
2513 xfs_filblks_t newlen
=0; /* new indirect size */
2514 xfs_filblks_t oldlen
=0; /* old indirect size */
2515 xfs_bmbt_irec_t right
; /* right neighbor extent entry */
2516 int state
= xfs_bmap_fork_to_state(whichfork
);
2517 xfs_filblks_t temp
; /* temp for indirect calculations */
2519 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
2520 ASSERT(isnullstartblock(new->br_startblock
));
2523 * Check and set flags if this segment has a left neighbor
2525 if (xfs_iext_peek_prev_extent(ifp
, icur
, &left
)) {
2526 state
|= BMAP_LEFT_VALID
;
2527 if (isnullstartblock(left
.br_startblock
))
2528 state
|= BMAP_LEFT_DELAY
;
2532 * Check and set flags if the current (right) segment exists.
2533 * If it doesn't exist, we're converting the hole at end-of-file.
2535 if (xfs_iext_get_extent(ifp
, icur
, &right
)) {
2536 state
|= BMAP_RIGHT_VALID
;
2537 if (isnullstartblock(right
.br_startblock
))
2538 state
|= BMAP_RIGHT_DELAY
;
2542 * Set contiguity flags on the left and right neighbors.
2543 * Don't let extents get too large, even if the pieces are contiguous.
2545 if ((state
& BMAP_LEFT_VALID
) && (state
& BMAP_LEFT_DELAY
) &&
2546 left
.br_startoff
+ left
.br_blockcount
== new->br_startoff
&&
2547 left
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
2548 state
|= BMAP_LEFT_CONTIG
;
2550 if ((state
& BMAP_RIGHT_VALID
) && (state
& BMAP_RIGHT_DELAY
) &&
2551 new->br_startoff
+ new->br_blockcount
== right
.br_startoff
&&
2552 new->br_blockcount
+ right
.br_blockcount
<= MAXEXTLEN
&&
2553 (!(state
& BMAP_LEFT_CONTIG
) ||
2554 (left
.br_blockcount
+ new->br_blockcount
+
2555 right
.br_blockcount
<= MAXEXTLEN
)))
2556 state
|= BMAP_RIGHT_CONTIG
;
2559 * Switch out based on the contiguity flags.
2561 switch (state
& (BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
)) {
2562 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2564 * New allocation is contiguous with delayed allocations
2565 * on the left and on the right.
2566 * Merge all three into a single extent record.
2568 temp
= left
.br_blockcount
+ new->br_blockcount
+
2569 right
.br_blockcount
;
2571 oldlen
= startblockval(left
.br_startblock
) +
2572 startblockval(new->br_startblock
) +
2573 startblockval(right
.br_startblock
);
2574 newlen
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
2576 left
.br_startblock
= nullstartblock(newlen
);
2577 left
.br_blockcount
= temp
;
2579 xfs_iext_remove(ip
, icur
, state
);
2580 xfs_iext_prev(ifp
, icur
);
2581 xfs_iext_update_extent(ip
, state
, icur
, &left
);
2584 case BMAP_LEFT_CONTIG
:
2586 * New allocation is contiguous with a delayed allocation
2588 * Merge the new allocation with the left neighbor.
2590 temp
= left
.br_blockcount
+ new->br_blockcount
;
2592 oldlen
= startblockval(left
.br_startblock
) +
2593 startblockval(new->br_startblock
);
2594 newlen
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
2596 left
.br_blockcount
= temp
;
2597 left
.br_startblock
= nullstartblock(newlen
);
2599 xfs_iext_prev(ifp
, icur
);
2600 xfs_iext_update_extent(ip
, state
, icur
, &left
);
2603 case BMAP_RIGHT_CONTIG
:
2605 * New allocation is contiguous with a delayed allocation
2607 * Merge the new allocation with the right neighbor.
2609 temp
= new->br_blockcount
+ right
.br_blockcount
;
2610 oldlen
= startblockval(new->br_startblock
) +
2611 startblockval(right
.br_startblock
);
2612 newlen
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
2614 right
.br_startoff
= new->br_startoff
;
2615 right
.br_startblock
= nullstartblock(newlen
);
2616 right
.br_blockcount
= temp
;
2617 xfs_iext_update_extent(ip
, state
, icur
, &right
);
2622 * New allocation is not contiguous with another
2623 * delayed allocation.
2624 * Insert a new entry.
2626 oldlen
= newlen
= 0;
2627 xfs_iext_insert(ip
, icur
, new, state
);
2630 if (oldlen
!= newlen
) {
2631 ASSERT(oldlen
> newlen
);
2632 xfs_mod_fdblocks(ip
->i_mount
, (int64_t)(oldlen
- newlen
),
2635 * Nothing to do for disk quota accounting here.
2637 xfs_mod_delalloc(ip
->i_mount
, (int64_t)newlen
- oldlen
);
2642 * Convert a hole to a real allocation.
2644 STATIC
int /* error */
2645 xfs_bmap_add_extent_hole_real(
2646 struct xfs_trans
*tp
,
2647 struct xfs_inode
*ip
,
2649 struct xfs_iext_cursor
*icur
,
2650 struct xfs_btree_cur
**curp
,
2651 struct xfs_bmbt_irec
*new,
2655 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
2656 struct xfs_mount
*mp
= ip
->i_mount
;
2657 struct xfs_btree_cur
*cur
= *curp
;
2658 int error
; /* error return value */
2659 int i
; /* temp state */
2660 xfs_bmbt_irec_t left
; /* left neighbor extent entry */
2661 xfs_bmbt_irec_t right
; /* right neighbor extent entry */
2662 int rval
=0; /* return value (logging flags) */
2663 int state
= xfs_bmap_fork_to_state(whichfork
);
2664 struct xfs_bmbt_irec old
;
2666 ASSERT(!isnullstartblock(new->br_startblock
));
2667 ASSERT(!cur
|| !(cur
->bc_private
.b
.flags
& XFS_BTCUR_BPRV_WASDEL
));
2669 XFS_STATS_INC(mp
, xs_add_exlist
);
2672 * Check and set flags if this segment has a left neighbor.
2674 if (xfs_iext_peek_prev_extent(ifp
, icur
, &left
)) {
2675 state
|= BMAP_LEFT_VALID
;
2676 if (isnullstartblock(left
.br_startblock
))
2677 state
|= BMAP_LEFT_DELAY
;
2681 * Check and set flags if this segment has a current value.
2682 * Not true if we're inserting into the "hole" at eof.
2684 if (xfs_iext_get_extent(ifp
, icur
, &right
)) {
2685 state
|= BMAP_RIGHT_VALID
;
2686 if (isnullstartblock(right
.br_startblock
))
2687 state
|= BMAP_RIGHT_DELAY
;
2691 * We're inserting a real allocation between "left" and "right".
2692 * Set the contiguity flags. Don't let extents get too large.
2694 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
2695 left
.br_startoff
+ left
.br_blockcount
== new->br_startoff
&&
2696 left
.br_startblock
+ left
.br_blockcount
== new->br_startblock
&&
2697 left
.br_state
== new->br_state
&&
2698 left
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
2699 state
|= BMAP_LEFT_CONTIG
;
2701 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
2702 new->br_startoff
+ new->br_blockcount
== right
.br_startoff
&&
2703 new->br_startblock
+ new->br_blockcount
== right
.br_startblock
&&
2704 new->br_state
== right
.br_state
&&
2705 new->br_blockcount
+ right
.br_blockcount
<= MAXEXTLEN
&&
2706 (!(state
& BMAP_LEFT_CONTIG
) ||
2707 left
.br_blockcount
+ new->br_blockcount
+
2708 right
.br_blockcount
<= MAXEXTLEN
))
2709 state
|= BMAP_RIGHT_CONTIG
;
2713 * Select which case we're in here, and implement it.
2715 switch (state
& (BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
)) {
2716 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2718 * New allocation is contiguous with real allocations on the
2719 * left and on the right.
2720 * Merge all three into a single extent record.
2722 left
.br_blockcount
+= new->br_blockcount
+ right
.br_blockcount
;
2724 xfs_iext_remove(ip
, icur
, state
);
2725 xfs_iext_prev(ifp
, icur
);
2726 xfs_iext_update_extent(ip
, state
, icur
, &left
);
2728 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2729 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
2731 rval
= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
2733 rval
= XFS_ILOG_CORE
;
2734 error
= xfs_bmbt_lookup_eq(cur
, &right
, &i
);
2737 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2738 error
= xfs_btree_delete(cur
, &i
);
2741 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2742 error
= xfs_btree_decrement(cur
, 0, &i
);
2745 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2746 error
= xfs_bmbt_update(cur
, &left
);
2752 case BMAP_LEFT_CONTIG
:
2754 * New allocation is contiguous with a real allocation
2756 * Merge the new allocation with the left neighbor.
2759 left
.br_blockcount
+= new->br_blockcount
;
2761 xfs_iext_prev(ifp
, icur
);
2762 xfs_iext_update_extent(ip
, state
, icur
, &left
);
2765 rval
= xfs_ilog_fext(whichfork
);
2768 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2771 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2772 error
= xfs_bmbt_update(cur
, &left
);
2778 case BMAP_RIGHT_CONTIG
:
2780 * New allocation is contiguous with a real allocation
2782 * Merge the new allocation with the right neighbor.
2786 right
.br_startoff
= new->br_startoff
;
2787 right
.br_startblock
= new->br_startblock
;
2788 right
.br_blockcount
+= new->br_blockcount
;
2789 xfs_iext_update_extent(ip
, state
, icur
, &right
);
2792 rval
= xfs_ilog_fext(whichfork
);
2795 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2798 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2799 error
= xfs_bmbt_update(cur
, &right
);
2807 * New allocation is not contiguous with another
2809 * Insert a new entry.
2811 xfs_iext_insert(ip
, icur
, new, state
);
2812 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2813 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
2815 rval
= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
2817 rval
= XFS_ILOG_CORE
;
2818 error
= xfs_bmbt_lookup_eq(cur
, new, &i
);
2821 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
2822 error
= xfs_btree_insert(cur
, &i
);
2825 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2830 /* add reverse mapping unless caller opted out */
2831 if (!(flags
& XFS_BMAPI_NORMAP
)) {
2832 error
= xfs_rmap_map_extent(tp
, ip
, whichfork
, new);
2837 /* convert to a btree if necessary */
2838 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
2839 int tmp_logflags
; /* partial log flag return val */
2841 ASSERT(cur
== NULL
);
2842 error
= xfs_bmap_extents_to_btree(tp
, ip
, curp
, 0,
2843 &tmp_logflags
, whichfork
);
2844 *logflagsp
|= tmp_logflags
;
2850 /* clear out the allocated field, done with it now in any case. */
2852 cur
->bc_private
.b
.allocated
= 0;
2854 xfs_bmap_check_leaf_extents(cur
, ip
, whichfork
);
2861 * Functions used in the extent read, allocate and remove paths
2865 * Adjust the size of the new extent based on di_extsize and rt extsize.
2868 xfs_bmap_extsize_align(
2870 xfs_bmbt_irec_t
*gotp
, /* next extent pointer */
2871 xfs_bmbt_irec_t
*prevp
, /* previous extent pointer */
2872 xfs_extlen_t extsz
, /* align to this extent size */
2873 int rt
, /* is this a realtime inode? */
2874 int eof
, /* is extent at end-of-file? */
2875 int delay
, /* creating delalloc extent? */
2876 int convert
, /* overwriting unwritten extent? */
2877 xfs_fileoff_t
*offp
, /* in/out: aligned offset */
2878 xfs_extlen_t
*lenp
) /* in/out: aligned length */
2880 xfs_fileoff_t orig_off
; /* original offset */
2881 xfs_extlen_t orig_alen
; /* original length */
2882 xfs_fileoff_t orig_end
; /* original off+len */
2883 xfs_fileoff_t nexto
; /* next file offset */
2884 xfs_fileoff_t prevo
; /* previous file offset */
2885 xfs_fileoff_t align_off
; /* temp for offset */
2886 xfs_extlen_t align_alen
; /* temp for length */
2887 xfs_extlen_t temp
; /* temp for calculations */
2892 orig_off
= align_off
= *offp
;
2893 orig_alen
= align_alen
= *lenp
;
2894 orig_end
= orig_off
+ orig_alen
;
2897 * If this request overlaps an existing extent, then don't
2898 * attempt to perform any additional alignment.
2900 if (!delay
&& !eof
&&
2901 (orig_off
>= gotp
->br_startoff
) &&
2902 (orig_end
<= gotp
->br_startoff
+ gotp
->br_blockcount
)) {
2907 * If the file offset is unaligned vs. the extent size
2908 * we need to align it. This will be possible unless
2909 * the file was previously written with a kernel that didn't
2910 * perform this alignment, or if a truncate shot us in the
2913 div_u64_rem(orig_off
, extsz
, &temp
);
2919 /* Same adjustment for the end of the requested area. */
2920 temp
= (align_alen
% extsz
);
2922 align_alen
+= extsz
- temp
;
2925 * For large extent hint sizes, the aligned extent might be larger than
2926 * MAXEXTLEN. In that case, reduce the size by an extsz so that it pulls
2927 * the length back under MAXEXTLEN. The outer allocation loops handle
2928 * short allocation just fine, so it is safe to do this. We only want to
2929 * do it when we are forced to, though, because it means more allocation
2930 * operations are required.
2932 while (align_alen
> MAXEXTLEN
)
2933 align_alen
-= extsz
;
2934 ASSERT(align_alen
<= MAXEXTLEN
);
2937 * If the previous block overlaps with this proposed allocation
2938 * then move the start forward without adjusting the length.
2940 if (prevp
->br_startoff
!= NULLFILEOFF
) {
2941 if (prevp
->br_startblock
== HOLESTARTBLOCK
)
2942 prevo
= prevp
->br_startoff
;
2944 prevo
= prevp
->br_startoff
+ prevp
->br_blockcount
;
2947 if (align_off
!= orig_off
&& align_off
< prevo
)
2950 * If the next block overlaps with this proposed allocation
2951 * then move the start back without adjusting the length,
2952 * but not before offset 0.
2953 * This may of course make the start overlap previous block,
2954 * and if we hit the offset 0 limit then the next block
2955 * can still overlap too.
2957 if (!eof
&& gotp
->br_startoff
!= NULLFILEOFF
) {
2958 if ((delay
&& gotp
->br_startblock
== HOLESTARTBLOCK
) ||
2959 (!delay
&& gotp
->br_startblock
== DELAYSTARTBLOCK
))
2960 nexto
= gotp
->br_startoff
+ gotp
->br_blockcount
;
2962 nexto
= gotp
->br_startoff
;
2964 nexto
= NULLFILEOFF
;
2966 align_off
+ align_alen
!= orig_end
&&
2967 align_off
+ align_alen
> nexto
)
2968 align_off
= nexto
> align_alen
? nexto
- align_alen
: 0;
2970 * If we're now overlapping the next or previous extent that
2971 * means we can't fit an extsz piece in this hole. Just move
2972 * the start forward to the first valid spot and set
2973 * the length so we hit the end.
2975 if (align_off
!= orig_off
&& align_off
< prevo
)
2977 if (align_off
+ align_alen
!= orig_end
&&
2978 align_off
+ align_alen
> nexto
&&
2979 nexto
!= NULLFILEOFF
) {
2980 ASSERT(nexto
> prevo
);
2981 align_alen
= nexto
- align_off
;
2985 * If realtime, and the result isn't a multiple of the realtime
2986 * extent size we need to remove blocks until it is.
2988 if (rt
&& (temp
= (align_alen
% mp
->m_sb
.sb_rextsize
))) {
2990 * We're not covering the original request, or
2991 * we won't be able to once we fix the length.
2993 if (orig_off
< align_off
||
2994 orig_end
> align_off
+ align_alen
||
2995 align_alen
- temp
< orig_alen
)
2998 * Try to fix it by moving the start up.
3000 if (align_off
+ temp
<= orig_off
) {
3005 * Try to fix it by moving the end in.
3007 else if (align_off
+ align_alen
- temp
>= orig_end
)
3010 * Set the start to the minimum then trim the length.
3013 align_alen
-= orig_off
- align_off
;
3014 align_off
= orig_off
;
3015 align_alen
-= align_alen
% mp
->m_sb
.sb_rextsize
;
3018 * Result doesn't cover the request, fail it.
3020 if (orig_off
< align_off
|| orig_end
> align_off
+ align_alen
)
3023 ASSERT(orig_off
>= align_off
);
3024 /* see MAXEXTLEN handling above */
3025 ASSERT(orig_end
<= align_off
+ align_alen
||
3026 align_alen
+ extsz
> MAXEXTLEN
);
3030 if (!eof
&& gotp
->br_startoff
!= NULLFILEOFF
)
3031 ASSERT(align_off
+ align_alen
<= gotp
->br_startoff
);
3032 if (prevp
->br_startoff
!= NULLFILEOFF
)
3033 ASSERT(align_off
>= prevp
->br_startoff
+ prevp
->br_blockcount
);
3041 #define XFS_ALLOC_GAP_UNITS 4
3045 struct xfs_bmalloca
*ap
) /* bmap alloc argument struct */
3047 xfs_fsblock_t adjust
; /* adjustment to block numbers */
3048 xfs_agnumber_t fb_agno
; /* ag number of ap->firstblock */
3049 xfs_mount_t
*mp
; /* mount point structure */
3050 int nullfb
; /* true if ap->firstblock isn't set */
3051 int rt
; /* true if inode is realtime */
3053 #define ISVALID(x,y) \
3055 (x) < mp->m_sb.sb_rblocks : \
3056 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
3057 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
3058 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
3060 mp
= ap
->ip
->i_mount
;
3061 nullfb
= ap
->tp
->t_firstblock
== NULLFSBLOCK
;
3062 rt
= XFS_IS_REALTIME_INODE(ap
->ip
) &&
3063 xfs_alloc_is_userdata(ap
->datatype
);
3064 fb_agno
= nullfb
? NULLAGNUMBER
: XFS_FSB_TO_AGNO(mp
,
3065 ap
->tp
->t_firstblock
);
3067 * If allocating at eof, and there's a previous real block,
3068 * try to use its last block as our starting point.
3070 if (ap
->eof
&& ap
->prev
.br_startoff
!= NULLFILEOFF
&&
3071 !isnullstartblock(ap
->prev
.br_startblock
) &&
3072 ISVALID(ap
->prev
.br_startblock
+ ap
->prev
.br_blockcount
,
3073 ap
->prev
.br_startblock
)) {
3074 ap
->blkno
= ap
->prev
.br_startblock
+ ap
->prev
.br_blockcount
;
3076 * Adjust for the gap between prevp and us.
3078 adjust
= ap
->offset
-
3079 (ap
->prev
.br_startoff
+ ap
->prev
.br_blockcount
);
3081 ISVALID(ap
->blkno
+ adjust
, ap
->prev
.br_startblock
))
3082 ap
->blkno
+= adjust
;
3085 * If not at eof, then compare the two neighbor blocks.
3086 * Figure out whether either one gives us a good starting point,
3087 * and pick the better one.
3089 else if (!ap
->eof
) {
3090 xfs_fsblock_t gotbno
; /* right side block number */
3091 xfs_fsblock_t gotdiff
=0; /* right side difference */
3092 xfs_fsblock_t prevbno
; /* left side block number */
3093 xfs_fsblock_t prevdiff
=0; /* left side difference */
3096 * If there's a previous (left) block, select a requested
3097 * start block based on it.
3099 if (ap
->prev
.br_startoff
!= NULLFILEOFF
&&
3100 !isnullstartblock(ap
->prev
.br_startblock
) &&
3101 (prevbno
= ap
->prev
.br_startblock
+
3102 ap
->prev
.br_blockcount
) &&
3103 ISVALID(prevbno
, ap
->prev
.br_startblock
)) {
3105 * Calculate gap to end of previous block.
3107 adjust
= prevdiff
= ap
->offset
-
3108 (ap
->prev
.br_startoff
+
3109 ap
->prev
.br_blockcount
);
3111 * Figure the startblock based on the previous block's
3112 * end and the gap size.
3114 * If the gap is large relative to the piece we're
3115 * allocating, or using it gives us an invalid block
3116 * number, then just use the end of the previous block.
3118 if (prevdiff
<= XFS_ALLOC_GAP_UNITS
* ap
->length
&&
3119 ISVALID(prevbno
+ prevdiff
,
3120 ap
->prev
.br_startblock
))
3125 * If the firstblock forbids it, can't use it,
3128 if (!rt
&& !nullfb
&&
3129 XFS_FSB_TO_AGNO(mp
, prevbno
) != fb_agno
)
3130 prevbno
= NULLFSBLOCK
;
3133 * No previous block or can't follow it, just default.
3136 prevbno
= NULLFSBLOCK
;
3138 * If there's a following (right) block, select a requested
3139 * start block based on it.
3141 if (!isnullstartblock(ap
->got
.br_startblock
)) {
3143 * Calculate gap to start of next block.
3145 adjust
= gotdiff
= ap
->got
.br_startoff
- ap
->offset
;
3147 * Figure the startblock based on the next block's
3148 * start and the gap size.
3150 gotbno
= ap
->got
.br_startblock
;
3153 * If the gap is large relative to the piece we're
3154 * allocating, or using it gives us an invalid block
3155 * number, then just use the start of the next block
3156 * offset by our length.
3158 if (gotdiff
<= XFS_ALLOC_GAP_UNITS
* ap
->length
&&
3159 ISVALID(gotbno
- gotdiff
, gotbno
))
3161 else if (ISVALID(gotbno
- ap
->length
, gotbno
)) {
3162 gotbno
-= ap
->length
;
3163 gotdiff
+= adjust
- ap
->length
;
3167 * If the firstblock forbids it, can't use it,
3170 if (!rt
&& !nullfb
&&
3171 XFS_FSB_TO_AGNO(mp
, gotbno
) != fb_agno
)
3172 gotbno
= NULLFSBLOCK
;
3175 * No next block, just default.
3178 gotbno
= NULLFSBLOCK
;
3180 * If both valid, pick the better one, else the only good
3181 * one, else ap->blkno is already set (to 0 or the inode block).
3183 if (prevbno
!= NULLFSBLOCK
&& gotbno
!= NULLFSBLOCK
)
3184 ap
->blkno
= prevdiff
<= gotdiff
? prevbno
: gotbno
;
3185 else if (prevbno
!= NULLFSBLOCK
)
3186 ap
->blkno
= prevbno
;
3187 else if (gotbno
!= NULLFSBLOCK
)
3194 xfs_bmap_longest_free_extent(
3195 struct xfs_trans
*tp
,
3200 struct xfs_mount
*mp
= tp
->t_mountp
;
3201 struct xfs_perag
*pag
;
3202 xfs_extlen_t longest
;
3205 pag
= xfs_perag_get(mp
, ag
);
3206 if (!pag
->pagf_init
) {
3207 error
= xfs_alloc_pagf_init(mp
, tp
, ag
, XFS_ALLOC_FLAG_TRYLOCK
);
3211 if (!pag
->pagf_init
) {
3217 longest
= xfs_alloc_longest_free_extent(pag
,
3218 xfs_alloc_min_freelist(mp
, pag
),
3219 xfs_ag_resv_needed(pag
, XFS_AG_RESV_NONE
));
3220 if (*blen
< longest
)
3229 xfs_bmap_select_minlen(
3230 struct xfs_bmalloca
*ap
,
3231 struct xfs_alloc_arg
*args
,
3235 if (notinit
|| *blen
< ap
->minlen
) {
3237 * Since we did a BUF_TRYLOCK above, it is possible that
3238 * there is space for this request.
3240 args
->minlen
= ap
->minlen
;
3241 } else if (*blen
< args
->maxlen
) {
3243 * If the best seen length is less than the request length,
3244 * use the best as the minimum.
3246 args
->minlen
= *blen
;
3249 * Otherwise we've seen an extent as big as maxlen, use that
3252 args
->minlen
= args
->maxlen
;
3257 xfs_bmap_btalloc_nullfb(
3258 struct xfs_bmalloca
*ap
,
3259 struct xfs_alloc_arg
*args
,
3262 struct xfs_mount
*mp
= ap
->ip
->i_mount
;
3263 xfs_agnumber_t ag
, startag
;
3267 args
->type
= XFS_ALLOCTYPE_START_BNO
;
3268 args
->total
= ap
->total
;
3270 startag
= ag
= XFS_FSB_TO_AGNO(mp
, args
->fsbno
);
3271 if (startag
== NULLAGNUMBER
)
3274 while (*blen
< args
->maxlen
) {
3275 error
= xfs_bmap_longest_free_extent(args
->tp
, ag
, blen
,
3280 if (++ag
== mp
->m_sb
.sb_agcount
)
3286 xfs_bmap_select_minlen(ap
, args
, blen
, notinit
);
3291 xfs_bmap_btalloc_filestreams(
3292 struct xfs_bmalloca
*ap
,
3293 struct xfs_alloc_arg
*args
,
3296 struct xfs_mount
*mp
= ap
->ip
->i_mount
;
3301 args
->type
= XFS_ALLOCTYPE_NEAR_BNO
;
3302 args
->total
= ap
->total
;
3304 ag
= XFS_FSB_TO_AGNO(mp
, args
->fsbno
);
3305 if (ag
== NULLAGNUMBER
)
3308 error
= xfs_bmap_longest_free_extent(args
->tp
, ag
, blen
, ¬init
);
3312 if (*blen
< args
->maxlen
) {
3313 error
= xfs_filestream_new_ag(ap
, &ag
);
3317 error
= xfs_bmap_longest_free_extent(args
->tp
, ag
, blen
,
3324 xfs_bmap_select_minlen(ap
, args
, blen
, notinit
);
3327 * Set the failure fallback case to look in the selected AG as stream
3330 ap
->blkno
= args
->fsbno
= XFS_AGB_TO_FSB(mp
, ag
, 0);
3334 /* Update all inode and quota accounting for the allocation we just did. */
3336 xfs_bmap_btalloc_accounting(
3337 struct xfs_bmalloca
*ap
,
3338 struct xfs_alloc_arg
*args
)
3340 if (ap
->flags
& XFS_BMAPI_COWFORK
) {
3342 * COW fork blocks are in-core only and thus are treated as
3343 * in-core quota reservation (like delalloc blocks) even when
3344 * converted to real blocks. The quota reservation is not
3345 * accounted to disk until blocks are remapped to the data
3346 * fork. So if these blocks were previously delalloc, we
3347 * already have quota reservation and there's nothing to do
3351 xfs_mod_delalloc(ap
->ip
->i_mount
, -(int64_t)args
->len
);
3356 * Otherwise, we've allocated blocks in a hole. The transaction
3357 * has acquired in-core quota reservation for this extent.
3358 * Rather than account these as real blocks, however, we reduce
3359 * the transaction quota reservation based on the allocation.
3360 * This essentially transfers the transaction quota reservation
3361 * to that of a delalloc extent.
3363 ap
->ip
->i_delayed_blks
+= args
->len
;
3364 xfs_trans_mod_dquot_byino(ap
->tp
, ap
->ip
, XFS_TRANS_DQ_RES_BLKS
,
3369 /* data/attr fork only */
3370 ap
->ip
->i_d
.di_nblocks
+= args
->len
;
3371 xfs_trans_log_inode(ap
->tp
, ap
->ip
, XFS_ILOG_CORE
);
3373 ap
->ip
->i_delayed_blks
-= args
->len
;
3374 xfs_mod_delalloc(ap
->ip
->i_mount
, -(int64_t)args
->len
);
3376 xfs_trans_mod_dquot_byino(ap
->tp
, ap
->ip
,
3377 ap
->wasdel
? XFS_TRANS_DQ_DELBCOUNT
: XFS_TRANS_DQ_BCOUNT
,
3383 struct xfs_bmalloca
*ap
) /* bmap alloc argument struct */
3385 xfs_mount_t
*mp
; /* mount point structure */
3386 xfs_alloctype_t atype
= 0; /* type for allocation routines */
3387 xfs_extlen_t align
= 0; /* minimum allocation alignment */
3388 xfs_agnumber_t fb_agno
; /* ag number of ap->firstblock */
3390 xfs_alloc_arg_t args
;
3391 xfs_fileoff_t orig_offset
;
3392 xfs_extlen_t orig_length
;
3394 xfs_extlen_t nextminlen
= 0;
3395 int nullfb
; /* true if ap->firstblock isn't set */
3402 orig_offset
= ap
->offset
;
3403 orig_length
= ap
->length
;
3405 mp
= ap
->ip
->i_mount
;
3407 /* stripe alignment for allocation is determined by mount parameters */
3409 if (mp
->m_swidth
&& (mp
->m_flags
& XFS_MOUNT_SWALLOC
))
3410 stripe_align
= mp
->m_swidth
;
3411 else if (mp
->m_dalign
)
3412 stripe_align
= mp
->m_dalign
;
3414 if (ap
->flags
& XFS_BMAPI_COWFORK
)
3415 align
= xfs_get_cowextsz_hint(ap
->ip
);
3416 else if (xfs_alloc_is_userdata(ap
->datatype
))
3417 align
= xfs_get_extsz_hint(ap
->ip
);
3419 error
= xfs_bmap_extsize_align(mp
, &ap
->got
, &ap
->prev
,
3420 align
, 0, ap
->eof
, 0, ap
->conv
,
3421 &ap
->offset
, &ap
->length
);
3427 nullfb
= ap
->tp
->t_firstblock
== NULLFSBLOCK
;
3428 fb_agno
= nullfb
? NULLAGNUMBER
: XFS_FSB_TO_AGNO(mp
,
3429 ap
->tp
->t_firstblock
);
3431 if (xfs_alloc_is_userdata(ap
->datatype
) &&
3432 xfs_inode_is_filestream(ap
->ip
)) {
3433 ag
= xfs_filestream_lookup_ag(ap
->ip
);
3434 ag
= (ag
!= NULLAGNUMBER
) ? ag
: 0;
3435 ap
->blkno
= XFS_AGB_TO_FSB(mp
, ag
, 0);
3437 ap
->blkno
= XFS_INO_TO_FSB(mp
, ap
->ip
->i_ino
);
3440 ap
->blkno
= ap
->tp
->t_firstblock
;
3442 xfs_bmap_adjacent(ap
);
3445 * If allowed, use ap->blkno; otherwise must use firstblock since
3446 * it's in the right allocation group.
3448 if (nullfb
|| XFS_FSB_TO_AGNO(mp
, ap
->blkno
) == fb_agno
)
3451 ap
->blkno
= ap
->tp
->t_firstblock
;
3453 * Normal allocation, done through xfs_alloc_vextent.
3455 tryagain
= isaligned
= 0;
3456 memset(&args
, 0, sizeof(args
));
3459 args
.fsbno
= ap
->blkno
;
3460 args
.oinfo
= XFS_RMAP_OINFO_SKIP_UPDATE
;
3462 /* Trim the allocation back to the maximum an AG can fit. */
3463 args
.maxlen
= min(ap
->length
, mp
->m_ag_max_usable
);
3467 * Search for an allocation group with a single extent large
3468 * enough for the request. If one isn't found, then adjust
3469 * the minimum allocation size to the largest space found.
3471 if (xfs_alloc_is_userdata(ap
->datatype
) &&
3472 xfs_inode_is_filestream(ap
->ip
))
3473 error
= xfs_bmap_btalloc_filestreams(ap
, &args
, &blen
);
3475 error
= xfs_bmap_btalloc_nullfb(ap
, &args
, &blen
);
3478 } else if (ap
->tp
->t_flags
& XFS_TRANS_LOWMODE
) {
3479 if (xfs_inode_is_filestream(ap
->ip
))
3480 args
.type
= XFS_ALLOCTYPE_FIRST_AG
;
3482 args
.type
= XFS_ALLOCTYPE_START_BNO
;
3483 args
.total
= args
.minlen
= ap
->minlen
;
3485 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
3486 args
.total
= ap
->total
;
3487 args
.minlen
= ap
->minlen
;
3489 /* apply extent size hints if obtained earlier */
3492 div_u64_rem(ap
->offset
, args
.prod
, &args
.mod
);
3494 args
.mod
= args
.prod
- args
.mod
;
3495 } else if (mp
->m_sb
.sb_blocksize
>= PAGE_SIZE
) {
3499 args
.prod
= PAGE_SIZE
>> mp
->m_sb
.sb_blocklog
;
3500 div_u64_rem(ap
->offset
, args
.prod
, &args
.mod
);
3502 args
.mod
= args
.prod
- args
.mod
;
3505 * If we are not low on available data blocks, and the
3506 * underlying logical volume manager is a stripe, and
3507 * the file offset is zero then try to allocate data
3508 * blocks on stripe unit boundary.
3509 * NOTE: ap->aeof is only set if the allocation length
3510 * is >= the stripe unit and the allocation offset is
3511 * at the end of file.
3513 if (!(ap
->tp
->t_flags
& XFS_TRANS_LOWMODE
) && ap
->aeof
) {
3515 args
.alignment
= stripe_align
;
3519 * Adjust for alignment
3521 if (blen
> args
.alignment
&& blen
<= args
.maxlen
)
3522 args
.minlen
= blen
- args
.alignment
;
3523 args
.minalignslop
= 0;
3526 * First try an exact bno allocation.
3527 * If it fails then do a near or start bno
3528 * allocation with alignment turned on.
3532 args
.type
= XFS_ALLOCTYPE_THIS_BNO
;
3535 * Compute the minlen+alignment for the
3536 * next case. Set slop so that the value
3537 * of minlen+alignment+slop doesn't go up
3538 * between the calls.
3540 if (blen
> stripe_align
&& blen
<= args
.maxlen
)
3541 nextminlen
= blen
- stripe_align
;
3543 nextminlen
= args
.minlen
;
3544 if (nextminlen
+ stripe_align
> args
.minlen
+ 1)
3546 nextminlen
+ stripe_align
-
3549 args
.minalignslop
= 0;
3553 args
.minalignslop
= 0;
3555 args
.minleft
= ap
->minleft
;
3556 args
.wasdel
= ap
->wasdel
;
3557 args
.resv
= XFS_AG_RESV_NONE
;
3558 args
.datatype
= ap
->datatype
;
3559 if (ap
->datatype
& XFS_ALLOC_USERDATA_ZERO
)
3562 error
= xfs_alloc_vextent(&args
);
3566 if (tryagain
&& args
.fsbno
== NULLFSBLOCK
) {
3568 * Exact allocation failed. Now try with alignment
3572 args
.fsbno
= ap
->blkno
;
3573 args
.alignment
= stripe_align
;
3574 args
.minlen
= nextminlen
;
3575 args
.minalignslop
= 0;
3577 if ((error
= xfs_alloc_vextent(&args
)))
3580 if (isaligned
&& args
.fsbno
== NULLFSBLOCK
) {
3582 * allocation failed, so turn off alignment and
3586 args
.fsbno
= ap
->blkno
;
3588 if ((error
= xfs_alloc_vextent(&args
)))
3591 if (args
.fsbno
== NULLFSBLOCK
&& nullfb
&&
3592 args
.minlen
> ap
->minlen
) {
3593 args
.minlen
= ap
->minlen
;
3594 args
.type
= XFS_ALLOCTYPE_START_BNO
;
3595 args
.fsbno
= ap
->blkno
;
3596 if ((error
= xfs_alloc_vextent(&args
)))
3599 if (args
.fsbno
== NULLFSBLOCK
&& nullfb
) {
3601 args
.type
= XFS_ALLOCTYPE_FIRST_AG
;
3602 args
.total
= ap
->minlen
;
3603 if ((error
= xfs_alloc_vextent(&args
)))
3605 ap
->tp
->t_flags
|= XFS_TRANS_LOWMODE
;
3607 if (args
.fsbno
!= NULLFSBLOCK
) {
3609 * check the allocation happened at the same or higher AG than
3610 * the first block that was allocated.
3612 ASSERT(ap
->tp
->t_firstblock
== NULLFSBLOCK
||
3613 XFS_FSB_TO_AGNO(mp
, ap
->tp
->t_firstblock
) <=
3614 XFS_FSB_TO_AGNO(mp
, args
.fsbno
));
3616 ap
->blkno
= args
.fsbno
;
3617 if (ap
->tp
->t_firstblock
== NULLFSBLOCK
)
3618 ap
->tp
->t_firstblock
= args
.fsbno
;
3619 ASSERT(nullfb
|| fb_agno
<= args
.agno
);
3620 ap
->length
= args
.len
;
3622 * If the extent size hint is active, we tried to round the
3623 * caller's allocation request offset down to extsz and the
3624 * length up to another extsz boundary. If we found a free
3625 * extent we mapped it in starting at this new offset. If the
3626 * newly mapped space isn't long enough to cover any of the
3627 * range of offsets that was originally requested, move the
3628 * mapping up so that we can fill as much of the caller's
3629 * original request as possible. Free space is apparently
3630 * very fragmented so we're unlikely to be able to satisfy the
3633 if (ap
->length
<= orig_length
)
3634 ap
->offset
= orig_offset
;
3635 else if (ap
->offset
+ ap
->length
< orig_offset
+ orig_length
)
3636 ap
->offset
= orig_offset
+ orig_length
- ap
->length
;
3637 xfs_bmap_btalloc_accounting(ap
, &args
);
3639 ap
->blkno
= NULLFSBLOCK
;
3646 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
3647 * It figures out where to ask the underlying allocator to put the new extent.
3651 struct xfs_bmalloca
*ap
) /* bmap alloc argument struct */
3653 if (XFS_IS_REALTIME_INODE(ap
->ip
) &&
3654 xfs_alloc_is_userdata(ap
->datatype
))
3655 return xfs_bmap_rtalloc(ap
);
3656 return xfs_bmap_btalloc(ap
);
3659 /* Trim extent to fit a logical block range. */
3662 struct xfs_bmbt_irec
*irec
,
3666 xfs_fileoff_t distance
;
3667 xfs_fileoff_t end
= bno
+ len
;
3669 if (irec
->br_startoff
+ irec
->br_blockcount
<= bno
||
3670 irec
->br_startoff
>= end
) {
3671 irec
->br_blockcount
= 0;
3675 if (irec
->br_startoff
< bno
) {
3676 distance
= bno
- irec
->br_startoff
;
3677 if (isnullstartblock(irec
->br_startblock
))
3678 irec
->br_startblock
= DELAYSTARTBLOCK
;
3679 if (irec
->br_startblock
!= DELAYSTARTBLOCK
&&
3680 irec
->br_startblock
!= HOLESTARTBLOCK
)
3681 irec
->br_startblock
+= distance
;
3682 irec
->br_startoff
+= distance
;
3683 irec
->br_blockcount
-= distance
;
3686 if (end
< irec
->br_startoff
+ irec
->br_blockcount
) {
3687 distance
= irec
->br_startoff
+ irec
->br_blockcount
- end
;
3688 irec
->br_blockcount
-= distance
;
3693 * Trim the returned map to the required bounds
3697 struct xfs_bmbt_irec
*mval
,
3698 struct xfs_bmbt_irec
*got
,
3706 if ((flags
& XFS_BMAPI_ENTIRE
) ||
3707 got
->br_startoff
+ got
->br_blockcount
<= obno
) {
3709 if (isnullstartblock(got
->br_startblock
))
3710 mval
->br_startblock
= DELAYSTARTBLOCK
;
3716 ASSERT((*bno
>= obno
) || (n
== 0));
3718 mval
->br_startoff
= *bno
;
3719 if (isnullstartblock(got
->br_startblock
))
3720 mval
->br_startblock
= DELAYSTARTBLOCK
;
3722 mval
->br_startblock
= got
->br_startblock
+
3723 (*bno
- got
->br_startoff
);
3725 * Return the minimum of what we got and what we asked for for
3726 * the length. We can use the len variable here because it is
3727 * modified below and we could have been there before coming
3728 * here if the first part of the allocation didn't overlap what
3731 mval
->br_blockcount
= XFS_FILBLKS_MIN(end
- *bno
,
3732 got
->br_blockcount
- (*bno
- got
->br_startoff
));
3733 mval
->br_state
= got
->br_state
;
3734 ASSERT(mval
->br_blockcount
<= len
);
3739 * Update and validate the extent map to return
3742 xfs_bmapi_update_map(
3743 struct xfs_bmbt_irec
**map
,
3751 xfs_bmbt_irec_t
*mval
= *map
;
3753 ASSERT((flags
& XFS_BMAPI_ENTIRE
) ||
3754 ((mval
->br_startoff
+ mval
->br_blockcount
) <= end
));
3755 ASSERT((flags
& XFS_BMAPI_ENTIRE
) || (mval
->br_blockcount
<= *len
) ||
3756 (mval
->br_startoff
< obno
));
3758 *bno
= mval
->br_startoff
+ mval
->br_blockcount
;
3760 if (*n
> 0 && mval
->br_startoff
== mval
[-1].br_startoff
) {
3761 /* update previous map with new information */
3762 ASSERT(mval
->br_startblock
== mval
[-1].br_startblock
);
3763 ASSERT(mval
->br_blockcount
> mval
[-1].br_blockcount
);
3764 ASSERT(mval
->br_state
== mval
[-1].br_state
);
3765 mval
[-1].br_blockcount
= mval
->br_blockcount
;
3766 mval
[-1].br_state
= mval
->br_state
;
3767 } else if (*n
> 0 && mval
->br_startblock
!= DELAYSTARTBLOCK
&&
3768 mval
[-1].br_startblock
!= DELAYSTARTBLOCK
&&
3769 mval
[-1].br_startblock
!= HOLESTARTBLOCK
&&
3770 mval
->br_startblock
== mval
[-1].br_startblock
+
3771 mval
[-1].br_blockcount
&&
3772 mval
[-1].br_state
== mval
->br_state
) {
3773 ASSERT(mval
->br_startoff
==
3774 mval
[-1].br_startoff
+ mval
[-1].br_blockcount
);
3775 mval
[-1].br_blockcount
+= mval
->br_blockcount
;
3776 } else if (*n
> 0 &&
3777 mval
->br_startblock
== DELAYSTARTBLOCK
&&
3778 mval
[-1].br_startblock
== DELAYSTARTBLOCK
&&
3779 mval
->br_startoff
==
3780 mval
[-1].br_startoff
+ mval
[-1].br_blockcount
) {
3781 mval
[-1].br_blockcount
+= mval
->br_blockcount
;
3782 mval
[-1].br_state
= mval
->br_state
;
3783 } else if (!((*n
== 0) &&
3784 ((mval
->br_startoff
+ mval
->br_blockcount
) <=
3793 * Map file blocks to filesystem blocks without allocation.
3797 struct xfs_inode
*ip
,
3800 struct xfs_bmbt_irec
*mval
,
3804 struct xfs_mount
*mp
= ip
->i_mount
;
3805 struct xfs_ifork
*ifp
;
3806 struct xfs_bmbt_irec got
;
3809 struct xfs_iext_cursor icur
;
3813 int whichfork
= xfs_bmapi_whichfork(flags
);
3816 ASSERT(!(flags
& ~(XFS_BMAPI_ATTRFORK
|XFS_BMAPI_ENTIRE
|
3817 XFS_BMAPI_COWFORK
)));
3818 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_SHARED
|XFS_ILOCK_EXCL
));
3820 if (unlikely(XFS_TEST_ERROR(
3821 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
3822 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
3823 mp
, XFS_ERRTAG_BMAPIFORMAT
))) {
3824 XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW
, mp
);
3825 return -EFSCORRUPTED
;
3828 if (XFS_FORCED_SHUTDOWN(mp
))
3831 XFS_STATS_INC(mp
, xs_blk_mapr
);
3833 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
3835 /* No CoW fork? Return a hole. */
3836 if (whichfork
== XFS_COW_FORK
&& !ifp
) {
3837 mval
->br_startoff
= bno
;
3838 mval
->br_startblock
= HOLESTARTBLOCK
;
3839 mval
->br_blockcount
= len
;
3840 mval
->br_state
= XFS_EXT_NORM
;
3845 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
3846 error
= xfs_iread_extents(NULL
, ip
, whichfork
);
3851 if (!xfs_iext_lookup_extent(ip
, ifp
, bno
, &icur
, &got
))
3856 while (bno
< end
&& n
< *nmap
) {
3857 /* Reading past eof, act as though there's a hole up to end. */
3859 got
.br_startoff
= end
;
3860 if (got
.br_startoff
> bno
) {
3861 /* Reading in a hole. */
3862 mval
->br_startoff
= bno
;
3863 mval
->br_startblock
= HOLESTARTBLOCK
;
3864 mval
->br_blockcount
=
3865 XFS_FILBLKS_MIN(len
, got
.br_startoff
- bno
);
3866 mval
->br_state
= XFS_EXT_NORM
;
3867 bno
+= mval
->br_blockcount
;
3868 len
-= mval
->br_blockcount
;
3874 /* set up the extent map to return. */
3875 xfs_bmapi_trim_map(mval
, &got
, &bno
, len
, obno
, end
, n
, flags
);
3876 xfs_bmapi_update_map(&mval
, &bno
, &len
, obno
, end
, &n
, flags
);
3878 /* If we're done, stop now. */
3879 if (bno
>= end
|| n
>= *nmap
)
3882 /* Else go on to the next record. */
3883 if (!xfs_iext_next_extent(ifp
, &icur
, &got
))
3891 * Add a delayed allocation extent to an inode. Blocks are reserved from the
3892 * global pool and the extent inserted into the inode in-core extent tree.
3894 * On entry, got refers to the first extent beyond the offset of the extent to
3895 * allocate or eof is specified if no such extent exists. On return, got refers
3896 * to the extent record that was inserted to the inode fork.
3898 * Note that the allocated extent may have been merged with contiguous extents
3899 * during insertion into the inode fork. Thus, got does not reflect the current
3900 * state of the inode fork on return. If necessary, the caller can use lastx to
3901 * look up the updated record in the inode fork.
3904 xfs_bmapi_reserve_delalloc(
3905 struct xfs_inode
*ip
,
3909 xfs_filblks_t prealloc
,
3910 struct xfs_bmbt_irec
*got
,
3911 struct xfs_iext_cursor
*icur
,
3914 struct xfs_mount
*mp
= ip
->i_mount
;
3915 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
3917 xfs_extlen_t indlen
;
3919 xfs_fileoff_t aoff
= off
;
3922 * Cap the alloc length. Keep track of prealloc so we know whether to
3923 * tag the inode before we return.
3925 alen
= XFS_FILBLKS_MIN(len
+ prealloc
, MAXEXTLEN
);
3927 alen
= XFS_FILBLKS_MIN(alen
, got
->br_startoff
- aoff
);
3928 if (prealloc
&& alen
>= len
)
3929 prealloc
= alen
- len
;
3931 /* Figure out the extent size, adjust alen */
3932 if (whichfork
== XFS_COW_FORK
) {
3933 struct xfs_bmbt_irec prev
;
3934 xfs_extlen_t extsz
= xfs_get_cowextsz_hint(ip
);
3936 if (!xfs_iext_peek_prev_extent(ifp
, icur
, &prev
))
3937 prev
.br_startoff
= NULLFILEOFF
;
3939 error
= xfs_bmap_extsize_align(mp
, got
, &prev
, extsz
, 0, eof
,
3940 1, 0, &aoff
, &alen
);
3945 * Make a transaction-less quota reservation for delayed allocation
3946 * blocks. This number gets adjusted later. We return if we haven't
3947 * allocated blocks already inside this loop.
3949 error
= xfs_trans_reserve_quota_nblks(NULL
, ip
, (long)alen
, 0,
3950 XFS_QMOPT_RES_REGBLKS
);
3955 * Split changing sb for alen and indlen since they could be coming
3956 * from different places.
3958 indlen
= (xfs_extlen_t
)xfs_bmap_worst_indlen(ip
, alen
);
3961 error
= xfs_mod_fdblocks(mp
, -((int64_t)alen
), false);
3963 goto out_unreserve_quota
;
3965 error
= xfs_mod_fdblocks(mp
, -((int64_t)indlen
), false);
3967 goto out_unreserve_blocks
;
3970 ip
->i_delayed_blks
+= alen
;
3971 xfs_mod_delalloc(ip
->i_mount
, alen
+ indlen
);
3973 got
->br_startoff
= aoff
;
3974 got
->br_startblock
= nullstartblock(indlen
);
3975 got
->br_blockcount
= alen
;
3976 got
->br_state
= XFS_EXT_NORM
;
3978 xfs_bmap_add_extent_hole_delay(ip
, whichfork
, icur
, got
);
3981 * Tag the inode if blocks were preallocated. Note that COW fork
3982 * preallocation can occur at the start or end of the extent, even when
3983 * prealloc == 0, so we must also check the aligned offset and length.
3985 if (whichfork
== XFS_DATA_FORK
&& prealloc
)
3986 xfs_inode_set_eofblocks_tag(ip
);
3987 if (whichfork
== XFS_COW_FORK
&& (prealloc
|| aoff
< off
|| alen
> len
))
3988 xfs_inode_set_cowblocks_tag(ip
);
3992 out_unreserve_blocks
:
3993 xfs_mod_fdblocks(mp
, alen
, false);
3994 out_unreserve_quota
:
3995 if (XFS_IS_QUOTA_ON(mp
))
3996 xfs_trans_unreserve_quota_nblks(NULL
, ip
, (long)alen
, 0,
3997 XFS_QMOPT_RES_REGBLKS
);
4003 struct xfs_bmalloca
*bma
)
4005 struct xfs_mount
*mp
= bma
->ip
->i_mount
;
4006 int whichfork
= xfs_bmapi_whichfork(bma
->flags
);
4007 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
4008 int tmp_logflags
= 0;
4011 ASSERT(bma
->length
> 0);
4014 * For the wasdelay case, we could also just allocate the stuff asked
4015 * for in this bmap call but that wouldn't be as good.
4018 bma
->length
= (xfs_extlen_t
)bma
->got
.br_blockcount
;
4019 bma
->offset
= bma
->got
.br_startoff
;
4020 xfs_iext_peek_prev_extent(ifp
, &bma
->icur
, &bma
->prev
);
4022 bma
->length
= XFS_FILBLKS_MIN(bma
->length
, MAXEXTLEN
);
4024 bma
->length
= XFS_FILBLKS_MIN(bma
->length
,
4025 bma
->got
.br_startoff
- bma
->offset
);
4029 * Set the data type being allocated. For the data fork, the first data
4030 * in the file is treated differently to all other allocations. For the
4031 * attribute fork, we only need to ensure the allocated range is not on
4034 if (!(bma
->flags
& XFS_BMAPI_METADATA
)) {
4035 bma
->datatype
= XFS_ALLOC_NOBUSY
;
4036 if (whichfork
== XFS_DATA_FORK
) {
4037 if (bma
->offset
== 0)
4038 bma
->datatype
|= XFS_ALLOC_INITIAL_USER_DATA
;
4040 bma
->datatype
|= XFS_ALLOC_USERDATA
;
4042 if (bma
->flags
& XFS_BMAPI_ZERO
)
4043 bma
->datatype
|= XFS_ALLOC_USERDATA_ZERO
;
4046 bma
->minlen
= (bma
->flags
& XFS_BMAPI_CONTIG
) ? bma
->length
: 1;
4049 * Only want to do the alignment at the eof if it is userdata and
4050 * allocation length is larger than a stripe unit.
4052 if (mp
->m_dalign
&& bma
->length
>= mp
->m_dalign
&&
4053 !(bma
->flags
& XFS_BMAPI_METADATA
) && whichfork
== XFS_DATA_FORK
) {
4054 error
= xfs_bmap_isaeof(bma
, whichfork
);
4059 error
= xfs_bmap_alloc(bma
);
4063 if (bma
->blkno
== NULLFSBLOCK
)
4065 if ((ifp
->if_flags
& XFS_IFBROOT
) && !bma
->cur
)
4066 bma
->cur
= xfs_bmbt_init_cursor(mp
, bma
->tp
, bma
->ip
, whichfork
);
4068 * Bump the number of extents we've allocated
4074 bma
->cur
->bc_private
.b
.flags
=
4075 bma
->wasdel
? XFS_BTCUR_BPRV_WASDEL
: 0;
4077 bma
->got
.br_startoff
= bma
->offset
;
4078 bma
->got
.br_startblock
= bma
->blkno
;
4079 bma
->got
.br_blockcount
= bma
->length
;
4080 bma
->got
.br_state
= XFS_EXT_NORM
;
4083 * In the data fork, a wasdelay extent has been initialized, so
4084 * shouldn't be flagged as unwritten.
4086 * For the cow fork, however, we convert delalloc reservations
4087 * (extents allocated for speculative preallocation) to
4088 * allocated unwritten extents, and only convert the unwritten
4089 * extents to real extents when we're about to write the data.
4091 if ((!bma
->wasdel
|| (bma
->flags
& XFS_BMAPI_COWFORK
)) &&
4092 (bma
->flags
& XFS_BMAPI_PREALLOC
))
4093 bma
->got
.br_state
= XFS_EXT_UNWRITTEN
;
4096 error
= xfs_bmap_add_extent_delay_real(bma
, whichfork
);
4098 error
= xfs_bmap_add_extent_hole_real(bma
->tp
, bma
->ip
,
4099 whichfork
, &bma
->icur
, &bma
->cur
, &bma
->got
,
4100 &bma
->logflags
, bma
->flags
);
4102 bma
->logflags
|= tmp_logflags
;
4107 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4108 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4109 * the neighbouring ones.
4111 xfs_iext_get_extent(ifp
, &bma
->icur
, &bma
->got
);
4113 ASSERT(bma
->got
.br_startoff
<= bma
->offset
);
4114 ASSERT(bma
->got
.br_startoff
+ bma
->got
.br_blockcount
>=
4115 bma
->offset
+ bma
->length
);
4116 ASSERT(bma
->got
.br_state
== XFS_EXT_NORM
||
4117 bma
->got
.br_state
== XFS_EXT_UNWRITTEN
);
4122 xfs_bmapi_convert_unwritten(
4123 struct xfs_bmalloca
*bma
,
4124 struct xfs_bmbt_irec
*mval
,
4128 int whichfork
= xfs_bmapi_whichfork(flags
);
4129 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
4130 int tmp_logflags
= 0;
4133 /* check if we need to do unwritten->real conversion */
4134 if (mval
->br_state
== XFS_EXT_UNWRITTEN
&&
4135 (flags
& XFS_BMAPI_PREALLOC
))
4138 /* check if we need to do real->unwritten conversion */
4139 if (mval
->br_state
== XFS_EXT_NORM
&&
4140 (flags
& (XFS_BMAPI_PREALLOC
| XFS_BMAPI_CONVERT
)) !=
4141 (XFS_BMAPI_PREALLOC
| XFS_BMAPI_CONVERT
))
4145 * Modify (by adding) the state flag, if writing.
4147 ASSERT(mval
->br_blockcount
<= len
);
4148 if ((ifp
->if_flags
& XFS_IFBROOT
) && !bma
->cur
) {
4149 bma
->cur
= xfs_bmbt_init_cursor(bma
->ip
->i_mount
, bma
->tp
,
4150 bma
->ip
, whichfork
);
4152 mval
->br_state
= (mval
->br_state
== XFS_EXT_UNWRITTEN
)
4153 ? XFS_EXT_NORM
: XFS_EXT_UNWRITTEN
;
4156 * Before insertion into the bmbt, zero the range being converted
4159 if (flags
& XFS_BMAPI_ZERO
) {
4160 error
= xfs_zero_extent(bma
->ip
, mval
->br_startblock
,
4161 mval
->br_blockcount
);
4166 error
= xfs_bmap_add_extent_unwritten_real(bma
->tp
, bma
->ip
, whichfork
,
4167 &bma
->icur
, &bma
->cur
, mval
, &tmp_logflags
);
4169 * Log the inode core unconditionally in the unwritten extent conversion
4170 * path because the conversion might not have done so (e.g., if the
4171 * extent count hasn't changed). We need to make sure the inode is dirty
4172 * in the transaction for the sake of fsync(), even if nothing has
4173 * changed, because fsync() will not force the log for this transaction
4174 * unless it sees the inode pinned.
4176 * Note: If we're only converting cow fork extents, there aren't
4177 * any on-disk updates to make, so we don't need to log anything.
4179 if (whichfork
!= XFS_COW_FORK
)
4180 bma
->logflags
|= tmp_logflags
| XFS_ILOG_CORE
;
4185 * Update our extent pointer, given that
4186 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4187 * of the neighbouring ones.
4189 xfs_iext_get_extent(ifp
, &bma
->icur
, &bma
->got
);
4192 * We may have combined previously unwritten space with written space,
4193 * so generate another request.
4195 if (mval
->br_blockcount
< len
)
4200 static inline xfs_extlen_t
4202 struct xfs_trans
*tp
,
4203 struct xfs_inode
*ip
,
4206 if (tp
&& tp
->t_firstblock
!= NULLFSBLOCK
)
4208 if (XFS_IFORK_FORMAT(ip
, fork
) != XFS_DINODE_FMT_BTREE
)
4210 return be16_to_cpu(XFS_IFORK_PTR(ip
, fork
)->if_broot
->bb_level
) + 1;
4214 * Log whatever the flags say, even if error. Otherwise we might miss detecting
4215 * a case where the data is changed, there's an error, and it's not logged so we
4216 * don't shutdown when we should. Don't bother logging extents/btree changes if
4217 * we converted to the other format.
4221 struct xfs_bmalloca
*bma
,
4225 if ((bma
->logflags
& xfs_ilog_fext(whichfork
)) &&
4226 XFS_IFORK_FORMAT(bma
->ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
4227 bma
->logflags
&= ~xfs_ilog_fext(whichfork
);
4228 else if ((bma
->logflags
& xfs_ilog_fbroot(whichfork
)) &&
4229 XFS_IFORK_FORMAT(bma
->ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)
4230 bma
->logflags
&= ~xfs_ilog_fbroot(whichfork
);
4233 xfs_trans_log_inode(bma
->tp
, bma
->ip
, bma
->logflags
);
4235 xfs_btree_del_cursor(bma
->cur
, error
);
4239 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4240 * extent state if necessary. Details behaviour is controlled by the flags
4241 * parameter. Only allocates blocks from a single allocation group, to avoid
4246 struct xfs_trans
*tp
, /* transaction pointer */
4247 struct xfs_inode
*ip
, /* incore inode */
4248 xfs_fileoff_t bno
, /* starting file offs. mapped */
4249 xfs_filblks_t len
, /* length to map in file */
4250 int flags
, /* XFS_BMAPI_... */
4251 xfs_extlen_t total
, /* total blocks needed */
4252 struct xfs_bmbt_irec
*mval
, /* output: map values */
4253 int *nmap
) /* i/o: mval size/count */
4255 struct xfs_bmalloca bma
= {
4260 struct xfs_mount
*mp
= ip
->i_mount
;
4261 struct xfs_ifork
*ifp
;
4262 xfs_fileoff_t end
; /* end of mapped file region */
4263 bool eof
= false; /* after the end of extents */
4264 int error
; /* error return */
4265 int n
; /* current extent index */
4266 xfs_fileoff_t obno
; /* old block number (offset) */
4267 int whichfork
; /* data or attr fork */
4270 xfs_fileoff_t orig_bno
; /* original block number value */
4271 int orig_flags
; /* original flags arg value */
4272 xfs_filblks_t orig_len
; /* original value of len arg */
4273 struct xfs_bmbt_irec
*orig_mval
; /* original value of mval */
4274 int orig_nmap
; /* original value of *nmap */
4282 whichfork
= xfs_bmapi_whichfork(flags
);
4285 ASSERT(*nmap
<= XFS_BMAP_MAX_NMAP
);
4288 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_LOCAL
);
4289 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
4290 ASSERT(!(flags
& XFS_BMAPI_REMAP
));
4292 /* zeroing is for currently only for data extents, not metadata */
4293 ASSERT((flags
& (XFS_BMAPI_METADATA
| XFS_BMAPI_ZERO
)) !=
4294 (XFS_BMAPI_METADATA
| XFS_BMAPI_ZERO
));
4296 * we can allocate unwritten extents or pre-zero allocated blocks,
4297 * but it makes no sense to do both at once. This would result in
4298 * zeroing the unwritten extent twice, but it still being an
4299 * unwritten extent....
4301 ASSERT((flags
& (XFS_BMAPI_PREALLOC
| XFS_BMAPI_ZERO
)) !=
4302 (XFS_BMAPI_PREALLOC
| XFS_BMAPI_ZERO
));
4304 if (unlikely(XFS_TEST_ERROR(
4305 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
4306 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
4307 mp
, XFS_ERRTAG_BMAPIFORMAT
))) {
4308 XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW
, mp
);
4309 return -EFSCORRUPTED
;
4312 if (XFS_FORCED_SHUTDOWN(mp
))
4315 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4317 XFS_STATS_INC(mp
, xs_blk_mapw
);
4319 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
4320 error
= xfs_iread_extents(tp
, ip
, whichfork
);
4325 if (!xfs_iext_lookup_extent(ip
, ifp
, bno
, &bma
.icur
, &bma
.got
))
4327 if (!xfs_iext_peek_prev_extent(ifp
, &bma
.icur
, &bma
.prev
))
4328 bma
.prev
.br_startoff
= NULLFILEOFF
;
4329 bma
.minleft
= xfs_bmapi_minleft(tp
, ip
, whichfork
);
4334 while (bno
< end
&& n
< *nmap
) {
4335 bool need_alloc
= false, wasdelay
= false;
4337 /* in hole or beyond EOF? */
4338 if (eof
|| bma
.got
.br_startoff
> bno
) {
4340 * CoW fork conversions should /never/ hit EOF or
4341 * holes. There should always be something for us
4344 ASSERT(!((flags
& XFS_BMAPI_CONVERT
) &&
4345 (flags
& XFS_BMAPI_COWFORK
)));
4348 } else if (isnullstartblock(bma
.got
.br_startblock
)) {
4353 * First, deal with the hole before the allocated space
4354 * that we found, if any.
4356 if (need_alloc
|| wasdelay
) {
4358 bma
.conv
= !!(flags
& XFS_BMAPI_CONVERT
);
4359 bma
.wasdel
= wasdelay
;
4364 * There's a 32/64 bit type mismatch between the
4365 * allocation length request (which can be 64 bits in
4366 * length) and the bma length request, which is
4367 * xfs_extlen_t and therefore 32 bits. Hence we have to
4368 * check for 32-bit overflows and handle them here.
4370 if (len
> (xfs_filblks_t
)MAXEXTLEN
)
4371 bma
.length
= MAXEXTLEN
;
4376 ASSERT(bma
.length
> 0);
4377 error
= xfs_bmapi_allocate(&bma
);
4380 if (bma
.blkno
== NULLFSBLOCK
)
4384 * If this is a CoW allocation, record the data in
4385 * the refcount btree for orphan recovery.
4387 if (whichfork
== XFS_COW_FORK
) {
4388 error
= xfs_refcount_alloc_cow_extent(tp
,
4389 bma
.blkno
, bma
.length
);
4395 /* Deal with the allocated space we found. */
4396 xfs_bmapi_trim_map(mval
, &bma
.got
, &bno
, len
, obno
,
4399 /* Execute unwritten extent conversion if necessary */
4400 error
= xfs_bmapi_convert_unwritten(&bma
, mval
, len
, flags
);
4401 if (error
== -EAGAIN
)
4406 /* update the extent map to return */
4407 xfs_bmapi_update_map(&mval
, &bno
, &len
, obno
, end
, &n
, flags
);
4410 * If we're done, stop now. Stop when we've allocated
4411 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4412 * the transaction may get too big.
4414 if (bno
>= end
|| n
>= *nmap
|| bma
.nallocs
>= *nmap
)
4417 /* Else go on to the next record. */
4419 if (!xfs_iext_next_extent(ifp
, &bma
.icur
, &bma
.got
))
4424 error
= xfs_bmap_btree_to_extents(tp
, ip
, bma
.cur
, &bma
.logflags
,
4429 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
||
4430 XFS_IFORK_NEXTENTS(ip
, whichfork
) >
4431 XFS_IFORK_MAXEXT(ip
, whichfork
));
4432 xfs_bmapi_finish(&bma
, whichfork
, 0);
4433 xfs_bmap_validate_ret(orig_bno
, orig_len
, orig_flags
, orig_mval
,
4437 xfs_bmapi_finish(&bma
, whichfork
, error
);
4442 * Convert an existing delalloc extent to real blocks based on file offset. This
4443 * attempts to allocate the entire delalloc extent and may require multiple
4444 * invocations to allocate the target offset if a large enough physical extent
4448 xfs_bmapi_convert_delalloc(
4449 struct xfs_inode
*ip
,
4451 xfs_fileoff_t offset_fsb
,
4452 struct xfs_bmbt_irec
*imap
,
4455 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4456 struct xfs_mount
*mp
= ip
->i_mount
;
4457 struct xfs_bmalloca bma
= { NULL
};
4458 struct xfs_trans
*tp
;
4462 * Space for the extent and indirect blocks was reserved when the
4463 * delalloc extent was created so there's no need to do so here.
4465 error
= xfs_trans_alloc(mp
, &M_RES(mp
)->tr_write
, 0, 0,
4466 XFS_TRANS_RESERVE
, &tp
);
4470 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
4471 xfs_trans_ijoin(tp
, ip
, 0);
4473 if (!xfs_iext_lookup_extent(ip
, ifp
, offset_fsb
, &bma
.icur
, &bma
.got
) ||
4474 bma
.got
.br_startoff
> offset_fsb
) {
4476 * No extent found in the range we are trying to convert. This
4477 * should only happen for the COW fork, where another thread
4478 * might have moved the extent to the data fork in the meantime.
4480 WARN_ON_ONCE(whichfork
!= XFS_COW_FORK
);
4482 goto out_trans_cancel
;
4486 * If we find a real extent here we raced with another thread converting
4487 * the extent. Just return the real extent at this offset.
4489 if (!isnullstartblock(bma
.got
.br_startblock
)) {
4491 *seq
= READ_ONCE(ifp
->if_seq
);
4492 goto out_trans_cancel
;
4498 bma
.offset
= bma
.got
.br_startoff
;
4499 bma
.length
= max_t(xfs_filblks_t
, bma
.got
.br_blockcount
, MAXEXTLEN
);
4500 bma
.total
= XFS_EXTENTADD_SPACE_RES(ip
->i_mount
, XFS_DATA_FORK
);
4501 bma
.minleft
= xfs_bmapi_minleft(tp
, ip
, whichfork
);
4502 if (whichfork
== XFS_COW_FORK
)
4503 bma
.flags
= XFS_BMAPI_COWFORK
| XFS_BMAPI_PREALLOC
;
4505 if (!xfs_iext_peek_prev_extent(ifp
, &bma
.icur
, &bma
.prev
))
4506 bma
.prev
.br_startoff
= NULLFILEOFF
;
4508 error
= xfs_bmapi_allocate(&bma
);
4513 if (WARN_ON_ONCE(bma
.blkno
== NULLFSBLOCK
))
4515 error
= -EFSCORRUPTED
;
4516 if (WARN_ON_ONCE(!bma
.got
.br_startblock
&& !XFS_IS_REALTIME_INODE(ip
)))
4519 XFS_STATS_ADD(mp
, xs_xstrat_bytes
, XFS_FSB_TO_B(mp
, bma
.length
));
4520 XFS_STATS_INC(mp
, xs_xstrat_quick
);
4522 ASSERT(!isnullstartblock(bma
.got
.br_startblock
));
4524 *seq
= READ_ONCE(ifp
->if_seq
);
4526 if (whichfork
== XFS_COW_FORK
) {
4527 error
= xfs_refcount_alloc_cow_extent(tp
, bma
.blkno
,
4533 error
= xfs_bmap_btree_to_extents(tp
, ip
, bma
.cur
, &bma
.logflags
,
4538 xfs_bmapi_finish(&bma
, whichfork
, 0);
4539 error
= xfs_trans_commit(tp
);
4540 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
4544 xfs_bmapi_finish(&bma
, whichfork
, error
);
4546 xfs_trans_cancel(tp
);
4547 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
4553 struct xfs_trans
*tp
,
4554 struct xfs_inode
*ip
,
4557 xfs_fsblock_t startblock
,
4560 struct xfs_mount
*mp
= ip
->i_mount
;
4561 struct xfs_ifork
*ifp
;
4562 struct xfs_btree_cur
*cur
= NULL
;
4563 struct xfs_bmbt_irec got
;
4564 struct xfs_iext_cursor icur
;
4565 int whichfork
= xfs_bmapi_whichfork(flags
);
4566 int logflags
= 0, error
;
4568 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4570 ASSERT(len
<= (xfs_filblks_t
)MAXEXTLEN
);
4571 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
4572 ASSERT(!(flags
& ~(XFS_BMAPI_ATTRFORK
| XFS_BMAPI_PREALLOC
|
4573 XFS_BMAPI_NORMAP
)));
4574 ASSERT((flags
& (XFS_BMAPI_ATTRFORK
| XFS_BMAPI_PREALLOC
)) !=
4575 (XFS_BMAPI_ATTRFORK
| XFS_BMAPI_PREALLOC
));
4577 if (unlikely(XFS_TEST_ERROR(
4578 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
4579 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
4580 mp
, XFS_ERRTAG_BMAPIFORMAT
))) {
4581 XFS_ERROR_REPORT("xfs_bmapi_remap", XFS_ERRLEVEL_LOW
, mp
);
4582 return -EFSCORRUPTED
;
4585 if (XFS_FORCED_SHUTDOWN(mp
))
4588 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
4589 error
= xfs_iread_extents(tp
, ip
, whichfork
);
4594 if (xfs_iext_lookup_extent(ip
, ifp
, bno
, &icur
, &got
)) {
4595 /* make sure we only reflink into a hole. */
4596 ASSERT(got
.br_startoff
> bno
);
4597 ASSERT(got
.br_startoff
- bno
>= len
);
4600 ip
->i_d
.di_nblocks
+= len
;
4601 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
4603 if (ifp
->if_flags
& XFS_IFBROOT
) {
4604 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
4605 cur
->bc_private
.b
.flags
= 0;
4608 got
.br_startoff
= bno
;
4609 got
.br_startblock
= startblock
;
4610 got
.br_blockcount
= len
;
4611 if (flags
& XFS_BMAPI_PREALLOC
)
4612 got
.br_state
= XFS_EXT_UNWRITTEN
;
4614 got
.br_state
= XFS_EXT_NORM
;
4616 error
= xfs_bmap_add_extent_hole_real(tp
, ip
, whichfork
, &icur
,
4617 &cur
, &got
, &logflags
, flags
);
4621 error
= xfs_bmap_btree_to_extents(tp
, ip
, cur
, &logflags
, whichfork
);
4624 if (ip
->i_d
.di_format
!= XFS_DINODE_FMT_EXTENTS
)
4625 logflags
&= ~XFS_ILOG_DEXT
;
4626 else if (ip
->i_d
.di_format
!= XFS_DINODE_FMT_BTREE
)
4627 logflags
&= ~XFS_ILOG_DBROOT
;
4630 xfs_trans_log_inode(tp
, ip
, logflags
);
4632 xfs_btree_del_cursor(cur
, error
);
4637 * When a delalloc extent is split (e.g., due to a hole punch), the original
4638 * indlen reservation must be shared across the two new extents that are left
4641 * Given the original reservation and the worst case indlen for the two new
4642 * extents (as calculated by xfs_bmap_worst_indlen()), split the original
4643 * reservation fairly across the two new extents. If necessary, steal available
4644 * blocks from a deleted extent to make up a reservation deficiency (e.g., if
4645 * ores == 1). The number of stolen blocks is returned. The availability and
4646 * subsequent accounting of stolen blocks is the responsibility of the caller.
4648 static xfs_filblks_t
4649 xfs_bmap_split_indlen(
4650 xfs_filblks_t ores
, /* original res. */
4651 xfs_filblks_t
*indlen1
, /* ext1 worst indlen */
4652 xfs_filblks_t
*indlen2
, /* ext2 worst indlen */
4653 xfs_filblks_t avail
) /* stealable blocks */
4655 xfs_filblks_t len1
= *indlen1
;
4656 xfs_filblks_t len2
= *indlen2
;
4657 xfs_filblks_t nres
= len1
+ len2
; /* new total res. */
4658 xfs_filblks_t stolen
= 0;
4659 xfs_filblks_t resfactor
;
4662 * Steal as many blocks as we can to try and satisfy the worst case
4663 * indlen for both new extents.
4665 if (ores
< nres
&& avail
)
4666 stolen
= XFS_FILBLKS_MIN(nres
- ores
, avail
);
4669 /* nothing else to do if we've satisfied the new reservation */
4674 * We can't meet the total required reservation for the two extents.
4675 * Calculate the percent of the overall shortage between both extents
4676 * and apply this percentage to each of the requested indlen values.
4677 * This distributes the shortage fairly and reduces the chances that one
4678 * of the two extents is left with nothing when extents are repeatedly
4681 resfactor
= (ores
* 100);
4682 do_div(resfactor
, nres
);
4687 ASSERT(len1
+ len2
<= ores
);
4688 ASSERT(len1
< *indlen1
&& len2
< *indlen2
);
4691 * Hand out the remainder to each extent. If one of the two reservations
4692 * is zero, we want to make sure that one gets a block first. The loop
4693 * below starts with len1, so hand len2 a block right off the bat if it
4696 ores
-= (len1
+ len2
);
4697 ASSERT((*indlen1
- len1
) + (*indlen2
- len2
) >= ores
);
4698 if (ores
&& !len2
&& *indlen2
) {
4703 if (len1
< *indlen1
) {
4709 if (len2
< *indlen2
) {
4722 xfs_bmap_del_extent_delay(
4723 struct xfs_inode
*ip
,
4725 struct xfs_iext_cursor
*icur
,
4726 struct xfs_bmbt_irec
*got
,
4727 struct xfs_bmbt_irec
*del
)
4729 struct xfs_mount
*mp
= ip
->i_mount
;
4730 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4731 struct xfs_bmbt_irec
new;
4732 int64_t da_old
, da_new
, da_diff
= 0;
4733 xfs_fileoff_t del_endoff
, got_endoff
;
4734 xfs_filblks_t got_indlen
, new_indlen
, stolen
;
4735 int state
= xfs_bmap_fork_to_state(whichfork
);
4739 XFS_STATS_INC(mp
, xs_del_exlist
);
4741 isrt
= (whichfork
== XFS_DATA_FORK
) && XFS_IS_REALTIME_INODE(ip
);
4742 del_endoff
= del
->br_startoff
+ del
->br_blockcount
;
4743 got_endoff
= got
->br_startoff
+ got
->br_blockcount
;
4744 da_old
= startblockval(got
->br_startblock
);
4747 ASSERT(del
->br_blockcount
> 0);
4748 ASSERT(got
->br_startoff
<= del
->br_startoff
);
4749 ASSERT(got_endoff
>= del_endoff
);
4752 uint64_t rtexts
= XFS_FSB_TO_B(mp
, del
->br_blockcount
);
4754 do_div(rtexts
, mp
->m_sb
.sb_rextsize
);
4755 xfs_mod_frextents(mp
, rtexts
);
4759 * Update the inode delalloc counter now and wait to update the
4760 * sb counters as we might have to borrow some blocks for the
4761 * indirect block accounting.
4763 error
= xfs_trans_reserve_quota_nblks(NULL
, ip
,
4764 -((long)del
->br_blockcount
), 0,
4765 isrt
? XFS_QMOPT_RES_RTBLKS
: XFS_QMOPT_RES_REGBLKS
);
4768 ip
->i_delayed_blks
-= del
->br_blockcount
;
4770 if (got
->br_startoff
== del
->br_startoff
)
4771 state
|= BMAP_LEFT_FILLING
;
4772 if (got_endoff
== del_endoff
)
4773 state
|= BMAP_RIGHT_FILLING
;
4775 switch (state
& (BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
)) {
4776 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
4778 * Matches the whole extent. Delete the entry.
4780 xfs_iext_remove(ip
, icur
, state
);
4781 xfs_iext_prev(ifp
, icur
);
4783 case BMAP_LEFT_FILLING
:
4785 * Deleting the first part of the extent.
4787 got
->br_startoff
= del_endoff
;
4788 got
->br_blockcount
-= del
->br_blockcount
;
4789 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
,
4790 got
->br_blockcount
), da_old
);
4791 got
->br_startblock
= nullstartblock((int)da_new
);
4792 xfs_iext_update_extent(ip
, state
, icur
, got
);
4794 case BMAP_RIGHT_FILLING
:
4796 * Deleting the last part of the extent.
4798 got
->br_blockcount
= got
->br_blockcount
- del
->br_blockcount
;
4799 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
,
4800 got
->br_blockcount
), da_old
);
4801 got
->br_startblock
= nullstartblock((int)da_new
);
4802 xfs_iext_update_extent(ip
, state
, icur
, got
);
4806 * Deleting the middle of the extent.
4808 * Distribute the original indlen reservation across the two new
4809 * extents. Steal blocks from the deleted extent if necessary.
4810 * Stealing blocks simply fudges the fdblocks accounting below.
4811 * Warn if either of the new indlen reservations is zero as this
4812 * can lead to delalloc problems.
4814 got
->br_blockcount
= del
->br_startoff
- got
->br_startoff
;
4815 got_indlen
= xfs_bmap_worst_indlen(ip
, got
->br_blockcount
);
4817 new.br_blockcount
= got_endoff
- del_endoff
;
4818 new_indlen
= xfs_bmap_worst_indlen(ip
, new.br_blockcount
);
4820 WARN_ON_ONCE(!got_indlen
|| !new_indlen
);
4821 stolen
= xfs_bmap_split_indlen(da_old
, &got_indlen
, &new_indlen
,
4822 del
->br_blockcount
);
4824 got
->br_startblock
= nullstartblock((int)got_indlen
);
4826 new.br_startoff
= del_endoff
;
4827 new.br_state
= got
->br_state
;
4828 new.br_startblock
= nullstartblock((int)new_indlen
);
4830 xfs_iext_update_extent(ip
, state
, icur
, got
);
4831 xfs_iext_next(ifp
, icur
);
4832 xfs_iext_insert(ip
, icur
, &new, state
);
4834 da_new
= got_indlen
+ new_indlen
- stolen
;
4835 del
->br_blockcount
-= stolen
;
4839 ASSERT(da_old
>= da_new
);
4840 da_diff
= da_old
- da_new
;
4842 da_diff
+= del
->br_blockcount
;
4844 xfs_mod_fdblocks(mp
, da_diff
, false);
4845 xfs_mod_delalloc(mp
, -da_diff
);
4851 xfs_bmap_del_extent_cow(
4852 struct xfs_inode
*ip
,
4853 struct xfs_iext_cursor
*icur
,
4854 struct xfs_bmbt_irec
*got
,
4855 struct xfs_bmbt_irec
*del
)
4857 struct xfs_mount
*mp
= ip
->i_mount
;
4858 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, XFS_COW_FORK
);
4859 struct xfs_bmbt_irec
new;
4860 xfs_fileoff_t del_endoff
, got_endoff
;
4861 int state
= BMAP_COWFORK
;
4863 XFS_STATS_INC(mp
, xs_del_exlist
);
4865 del_endoff
= del
->br_startoff
+ del
->br_blockcount
;
4866 got_endoff
= got
->br_startoff
+ got
->br_blockcount
;
4868 ASSERT(del
->br_blockcount
> 0);
4869 ASSERT(got
->br_startoff
<= del
->br_startoff
);
4870 ASSERT(got_endoff
>= del_endoff
);
4871 ASSERT(!isnullstartblock(got
->br_startblock
));
4873 if (got
->br_startoff
== del
->br_startoff
)
4874 state
|= BMAP_LEFT_FILLING
;
4875 if (got_endoff
== del_endoff
)
4876 state
|= BMAP_RIGHT_FILLING
;
4878 switch (state
& (BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
)) {
4879 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
4881 * Matches the whole extent. Delete the entry.
4883 xfs_iext_remove(ip
, icur
, state
);
4884 xfs_iext_prev(ifp
, icur
);
4886 case BMAP_LEFT_FILLING
:
4888 * Deleting the first part of the extent.
4890 got
->br_startoff
= del_endoff
;
4891 got
->br_blockcount
-= del
->br_blockcount
;
4892 got
->br_startblock
= del
->br_startblock
+ del
->br_blockcount
;
4893 xfs_iext_update_extent(ip
, state
, icur
, got
);
4895 case BMAP_RIGHT_FILLING
:
4897 * Deleting the last part of the extent.
4899 got
->br_blockcount
-= del
->br_blockcount
;
4900 xfs_iext_update_extent(ip
, state
, icur
, got
);
4904 * Deleting the middle of the extent.
4906 got
->br_blockcount
= del
->br_startoff
- got
->br_startoff
;
4908 new.br_startoff
= del_endoff
;
4909 new.br_blockcount
= got_endoff
- del_endoff
;
4910 new.br_state
= got
->br_state
;
4911 new.br_startblock
= del
->br_startblock
+ del
->br_blockcount
;
4913 xfs_iext_update_extent(ip
, state
, icur
, got
);
4914 xfs_iext_next(ifp
, icur
);
4915 xfs_iext_insert(ip
, icur
, &new, state
);
4918 ip
->i_delayed_blks
-= del
->br_blockcount
;
4922 * Called by xfs_bmapi to update file extent records and the btree
4923 * after removing space.
4925 STATIC
int /* error */
4926 xfs_bmap_del_extent_real(
4927 xfs_inode_t
*ip
, /* incore inode pointer */
4928 xfs_trans_t
*tp
, /* current transaction pointer */
4929 struct xfs_iext_cursor
*icur
,
4930 xfs_btree_cur_t
*cur
, /* if null, not a btree */
4931 xfs_bmbt_irec_t
*del
, /* data to remove from extents */
4932 int *logflagsp
, /* inode logging flags */
4933 int whichfork
, /* data or attr fork */
4934 int bflags
) /* bmapi flags */
4936 xfs_fsblock_t del_endblock
=0; /* first block past del */
4937 xfs_fileoff_t del_endoff
; /* first offset past del */
4938 int do_fx
; /* free extent at end of routine */
4939 int error
; /* error return value */
4940 int flags
= 0;/* inode logging flags */
4941 struct xfs_bmbt_irec got
; /* current extent entry */
4942 xfs_fileoff_t got_endoff
; /* first offset past got */
4943 int i
; /* temp state */
4944 struct xfs_ifork
*ifp
; /* inode fork pointer */
4945 xfs_mount_t
*mp
; /* mount structure */
4946 xfs_filblks_t nblks
; /* quota/sb block count */
4947 xfs_bmbt_irec_t
new; /* new record to be inserted */
4949 uint qfield
; /* quota field to update */
4950 int state
= xfs_bmap_fork_to_state(whichfork
);
4951 struct xfs_bmbt_irec old
;
4954 XFS_STATS_INC(mp
, xs_del_exlist
);
4956 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4957 ASSERT(del
->br_blockcount
> 0);
4958 xfs_iext_get_extent(ifp
, icur
, &got
);
4959 ASSERT(got
.br_startoff
<= del
->br_startoff
);
4960 del_endoff
= del
->br_startoff
+ del
->br_blockcount
;
4961 got_endoff
= got
.br_startoff
+ got
.br_blockcount
;
4962 ASSERT(got_endoff
>= del_endoff
);
4963 ASSERT(!isnullstartblock(got
.br_startblock
));
4968 * If it's the case where the directory code is running with no block
4969 * reservation, and the deleted block is in the middle of its extent,
4970 * and the resulting insert of an extent would cause transformation to
4971 * btree format, then reject it. The calling code will then swap blocks
4972 * around instead. We have to do this now, rather than waiting for the
4973 * conversion to btree format, since the transaction will be dirty then.
4975 if (tp
->t_blk_res
== 0 &&
4976 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
&&
4977 XFS_IFORK_NEXTENTS(ip
, whichfork
) >=
4978 XFS_IFORK_MAXEXT(ip
, whichfork
) &&
4979 del
->br_startoff
> got
.br_startoff
&& del_endoff
< got_endoff
)
4982 flags
= XFS_ILOG_CORE
;
4983 if (whichfork
== XFS_DATA_FORK
&& XFS_IS_REALTIME_INODE(ip
)) {
4988 bno
= div_u64_rem(del
->br_startblock
, mp
->m_sb
.sb_rextsize
,
4991 len
= div_u64_rem(del
->br_blockcount
, mp
->m_sb
.sb_rextsize
,
4995 error
= xfs_rtfree_extent(tp
, bno
, (xfs_extlen_t
)len
);
4999 nblks
= len
* mp
->m_sb
.sb_rextsize
;
5000 qfield
= XFS_TRANS_DQ_RTBCOUNT
;
5003 nblks
= del
->br_blockcount
;
5004 qfield
= XFS_TRANS_DQ_BCOUNT
;
5007 del_endblock
= del
->br_startblock
+ del
->br_blockcount
;
5009 error
= xfs_bmbt_lookup_eq(cur
, &got
, &i
);
5012 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
5015 if (got
.br_startoff
== del
->br_startoff
)
5016 state
|= BMAP_LEFT_FILLING
;
5017 if (got_endoff
== del_endoff
)
5018 state
|= BMAP_RIGHT_FILLING
;
5020 switch (state
& (BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
)) {
5021 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
5023 * Matches the whole extent. Delete the entry.
5025 xfs_iext_remove(ip
, icur
, state
);
5026 xfs_iext_prev(ifp
, icur
);
5027 XFS_IFORK_NEXT_SET(ip
, whichfork
,
5028 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
5029 flags
|= XFS_ILOG_CORE
;
5031 flags
|= xfs_ilog_fext(whichfork
);
5034 if ((error
= xfs_btree_delete(cur
, &i
)))
5036 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
5038 case BMAP_LEFT_FILLING
:
5040 * Deleting the first part of the extent.
5042 got
.br_startoff
= del_endoff
;
5043 got
.br_startblock
= del_endblock
;
5044 got
.br_blockcount
-= del
->br_blockcount
;
5045 xfs_iext_update_extent(ip
, state
, icur
, &got
);
5047 flags
|= xfs_ilog_fext(whichfork
);
5050 error
= xfs_bmbt_update(cur
, &got
);
5054 case BMAP_RIGHT_FILLING
:
5056 * Deleting the last part of the extent.
5058 got
.br_blockcount
-= del
->br_blockcount
;
5059 xfs_iext_update_extent(ip
, state
, icur
, &got
);
5061 flags
|= xfs_ilog_fext(whichfork
);
5064 error
= xfs_bmbt_update(cur
, &got
);
5070 * Deleting the middle of the extent.
5074 got
.br_blockcount
= del
->br_startoff
- got
.br_startoff
;
5075 xfs_iext_update_extent(ip
, state
, icur
, &got
);
5077 new.br_startoff
= del_endoff
;
5078 new.br_blockcount
= got_endoff
- del_endoff
;
5079 new.br_state
= got
.br_state
;
5080 new.br_startblock
= del_endblock
;
5082 flags
|= XFS_ILOG_CORE
;
5084 error
= xfs_bmbt_update(cur
, &got
);
5087 error
= xfs_btree_increment(cur
, 0, &i
);
5090 cur
->bc_rec
.b
= new;
5091 error
= xfs_btree_insert(cur
, &i
);
5092 if (error
&& error
!= -ENOSPC
)
5095 * If get no-space back from btree insert, it tried a
5096 * split, and we have a zero block reservation. Fix up
5097 * our state and return the error.
5099 if (error
== -ENOSPC
) {
5101 * Reset the cursor, don't trust it after any
5104 error
= xfs_bmbt_lookup_eq(cur
, &got
, &i
);
5107 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
5109 * Update the btree record back
5110 * to the original value.
5112 error
= xfs_bmbt_update(cur
, &old
);
5116 * Reset the extent record back
5117 * to the original value.
5119 xfs_iext_update_extent(ip
, state
, icur
, &old
);
5124 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
5126 flags
|= xfs_ilog_fext(whichfork
);
5127 XFS_IFORK_NEXT_SET(ip
, whichfork
,
5128 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
5129 xfs_iext_next(ifp
, icur
);
5130 xfs_iext_insert(ip
, icur
, &new, state
);
5134 /* remove reverse mapping */
5135 error
= xfs_rmap_unmap_extent(tp
, ip
, whichfork
, del
);
5140 * If we need to, add to list of extents to delete.
5142 if (do_fx
&& !(bflags
& XFS_BMAPI_REMAP
)) {
5143 if (xfs_is_reflink_inode(ip
) && whichfork
== XFS_DATA_FORK
) {
5144 error
= xfs_refcount_decrease_extent(tp
, del
);
5148 __xfs_bmap_add_free(tp
, del
->br_startblock
,
5149 del
->br_blockcount
, NULL
,
5150 (bflags
& XFS_BMAPI_NODISCARD
) ||
5151 del
->br_state
== XFS_EXT_UNWRITTEN
);
5156 * Adjust inode # blocks in the file.
5159 ip
->i_d
.di_nblocks
-= nblks
;
5161 * Adjust quota data.
5163 if (qfield
&& !(bflags
& XFS_BMAPI_REMAP
))
5164 xfs_trans_mod_dquot_byino(tp
, ip
, qfield
, (long)-nblks
);
5172 * Unmap (remove) blocks from a file.
5173 * If nexts is nonzero then the number of extents to remove is limited to
5174 * that value. If not all extents in the block range can be removed then
5179 struct xfs_trans
*tp
, /* transaction pointer */
5180 struct xfs_inode
*ip
, /* incore inode */
5181 xfs_fileoff_t start
, /* first file offset deleted */
5182 xfs_filblks_t
*rlen
, /* i/o: amount remaining */
5183 int flags
, /* misc flags */
5184 xfs_extnum_t nexts
) /* number of extents max */
5186 struct xfs_btree_cur
*cur
; /* bmap btree cursor */
5187 struct xfs_bmbt_irec del
; /* extent being deleted */
5188 int error
; /* error return value */
5189 xfs_extnum_t extno
; /* extent number in list */
5190 struct xfs_bmbt_irec got
; /* current extent record */
5191 struct xfs_ifork
*ifp
; /* inode fork pointer */
5192 int isrt
; /* freeing in rt area */
5193 int logflags
; /* transaction logging flags */
5194 xfs_extlen_t mod
; /* rt extent offset */
5195 struct xfs_mount
*mp
; /* mount structure */
5196 int tmp_logflags
; /* partial logging flags */
5197 int wasdel
; /* was a delayed alloc extent */
5198 int whichfork
; /* data or attribute fork */
5200 xfs_filblks_t len
= *rlen
; /* length to unmap in file */
5201 xfs_fileoff_t max_len
;
5202 xfs_agnumber_t prev_agno
= NULLAGNUMBER
, agno
;
5204 struct xfs_iext_cursor icur
;
5207 trace_xfs_bunmap(ip
, start
, len
, flags
, _RET_IP_
);
5209 whichfork
= xfs_bmapi_whichfork(flags
);
5210 ASSERT(whichfork
!= XFS_COW_FORK
);
5211 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5213 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
5214 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)) {
5215 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW
,
5217 return -EFSCORRUPTED
;
5220 if (XFS_FORCED_SHUTDOWN(mp
))
5223 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
5228 * Guesstimate how many blocks we can unmap without running the risk of
5229 * blowing out the transaction with a mix of EFIs and reflink
5232 if (tp
&& xfs_is_reflink_inode(ip
) && whichfork
== XFS_DATA_FORK
)
5233 max_len
= min(len
, xfs_refcount_max_unmap(tp
->t_log_res
));
5237 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
5238 (error
= xfs_iread_extents(tp
, ip
, whichfork
)))
5240 if (xfs_iext_count(ifp
) == 0) {
5244 XFS_STATS_INC(mp
, xs_blk_unmap
);
5245 isrt
= (whichfork
== XFS_DATA_FORK
) && XFS_IS_REALTIME_INODE(ip
);
5248 if (!xfs_iext_lookup_extent_before(ip
, ifp
, &end
, &icur
, &got
)) {
5255 if (ifp
->if_flags
& XFS_IFBROOT
) {
5256 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
);
5257 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
5258 cur
->bc_private
.b
.flags
= 0;
5264 * Synchronize by locking the bitmap inode.
5266 xfs_ilock(mp
->m_rbmip
, XFS_ILOCK_EXCL
|XFS_ILOCK_RTBITMAP
);
5267 xfs_trans_ijoin(tp
, mp
->m_rbmip
, XFS_ILOCK_EXCL
);
5268 xfs_ilock(mp
->m_rsumip
, XFS_ILOCK_EXCL
|XFS_ILOCK_RTSUM
);
5269 xfs_trans_ijoin(tp
, mp
->m_rsumip
, XFS_ILOCK_EXCL
);
5273 while (end
!= (xfs_fileoff_t
)-1 && end
>= start
&&
5274 (nexts
== 0 || extno
< nexts
) && max_len
> 0) {
5276 * Is the found extent after a hole in which end lives?
5277 * Just back up to the previous extent, if so.
5279 if (got
.br_startoff
> end
&&
5280 !xfs_iext_prev_extent(ifp
, &icur
, &got
)) {
5285 * Is the last block of this extent before the range
5286 * we're supposed to delete? If so, we're done.
5288 end
= XFS_FILEOFF_MIN(end
,
5289 got
.br_startoff
+ got
.br_blockcount
- 1);
5293 * Then deal with the (possibly delayed) allocated space
5297 wasdel
= isnullstartblock(del
.br_startblock
);
5300 * Make sure we don't touch multiple AGF headers out of order
5301 * in a single transaction, as that could cause AB-BA deadlocks.
5304 agno
= XFS_FSB_TO_AGNO(mp
, del
.br_startblock
);
5305 if (prev_agno
!= NULLAGNUMBER
&& prev_agno
> agno
)
5309 if (got
.br_startoff
< start
) {
5310 del
.br_startoff
= start
;
5311 del
.br_blockcount
-= start
- got
.br_startoff
;
5313 del
.br_startblock
+= start
- got
.br_startoff
;
5315 if (del
.br_startoff
+ del
.br_blockcount
> end
+ 1)
5316 del
.br_blockcount
= end
+ 1 - del
.br_startoff
;
5318 /* How much can we safely unmap? */
5319 if (max_len
< del
.br_blockcount
) {
5320 del
.br_startoff
+= del
.br_blockcount
- max_len
;
5322 del
.br_startblock
+= del
.br_blockcount
- max_len
;
5323 del
.br_blockcount
= max_len
;
5329 sum
= del
.br_startblock
+ del
.br_blockcount
;
5330 div_u64_rem(sum
, mp
->m_sb
.sb_rextsize
, &mod
);
5333 * Realtime extent not lined up at the end.
5334 * The extent could have been split into written
5335 * and unwritten pieces, or we could just be
5336 * unmapping part of it. But we can't really
5337 * get rid of part of a realtime extent.
5339 if (del
.br_state
== XFS_EXT_UNWRITTEN
) {
5341 * This piece is unwritten, or we're not
5342 * using unwritten extents. Skip over it.
5345 end
-= mod
> del
.br_blockcount
?
5346 del
.br_blockcount
: mod
;
5347 if (end
< got
.br_startoff
&&
5348 !xfs_iext_prev_extent(ifp
, &icur
, &got
)) {
5355 * It's written, turn it unwritten.
5356 * This is better than zeroing it.
5358 ASSERT(del
.br_state
== XFS_EXT_NORM
);
5359 ASSERT(tp
->t_blk_res
> 0);
5361 * If this spans a realtime extent boundary,
5362 * chop it back to the start of the one we end at.
5364 if (del
.br_blockcount
> mod
) {
5365 del
.br_startoff
+= del
.br_blockcount
- mod
;
5366 del
.br_startblock
+= del
.br_blockcount
- mod
;
5367 del
.br_blockcount
= mod
;
5369 del
.br_state
= XFS_EXT_UNWRITTEN
;
5370 error
= xfs_bmap_add_extent_unwritten_real(tp
, ip
,
5371 whichfork
, &icur
, &cur
, &del
,
5377 div_u64_rem(del
.br_startblock
, mp
->m_sb
.sb_rextsize
, &mod
);
5380 * Realtime extent is lined up at the end but not
5381 * at the front. We'll get rid of full extents if
5384 mod
= mp
->m_sb
.sb_rextsize
- mod
;
5385 if (del
.br_blockcount
> mod
) {
5386 del
.br_blockcount
-= mod
;
5387 del
.br_startoff
+= mod
;
5388 del
.br_startblock
+= mod
;
5389 } else if (del
.br_startoff
== start
&&
5390 (del
.br_state
== XFS_EXT_UNWRITTEN
||
5391 tp
->t_blk_res
== 0)) {
5393 * Can't make it unwritten. There isn't
5394 * a full extent here so just skip it.
5396 ASSERT(end
>= del
.br_blockcount
);
5397 end
-= del
.br_blockcount
;
5398 if (got
.br_startoff
> end
&&
5399 !xfs_iext_prev_extent(ifp
, &icur
, &got
)) {
5404 } else if (del
.br_state
== XFS_EXT_UNWRITTEN
) {
5405 struct xfs_bmbt_irec prev
;
5408 * This one is already unwritten.
5409 * It must have a written left neighbor.
5410 * Unwrite the killed part of that one and
5413 if (!xfs_iext_prev_extent(ifp
, &icur
, &prev
))
5415 ASSERT(prev
.br_state
== XFS_EXT_NORM
);
5416 ASSERT(!isnullstartblock(prev
.br_startblock
));
5417 ASSERT(del
.br_startblock
==
5418 prev
.br_startblock
+ prev
.br_blockcount
);
5419 if (prev
.br_startoff
< start
) {
5420 mod
= start
- prev
.br_startoff
;
5421 prev
.br_blockcount
-= mod
;
5422 prev
.br_startblock
+= mod
;
5423 prev
.br_startoff
= start
;
5425 prev
.br_state
= XFS_EXT_UNWRITTEN
;
5426 error
= xfs_bmap_add_extent_unwritten_real(tp
,
5427 ip
, whichfork
, &icur
, &cur
,
5433 ASSERT(del
.br_state
== XFS_EXT_NORM
);
5434 del
.br_state
= XFS_EXT_UNWRITTEN
;
5435 error
= xfs_bmap_add_extent_unwritten_real(tp
,
5436 ip
, whichfork
, &icur
, &cur
,
5446 error
= xfs_bmap_del_extent_delay(ip
, whichfork
, &icur
,
5449 error
= xfs_bmap_del_extent_real(ip
, tp
, &icur
, cur
,
5450 &del
, &tmp_logflags
, whichfork
,
5452 logflags
|= tmp_logflags
;
5458 max_len
-= del
.br_blockcount
;
5459 end
= del
.br_startoff
- 1;
5462 * If not done go on to the next (previous) record.
5464 if (end
!= (xfs_fileoff_t
)-1 && end
>= start
) {
5465 if (!xfs_iext_get_extent(ifp
, &icur
, &got
) ||
5466 (got
.br_startoff
> end
&&
5467 !xfs_iext_prev_extent(ifp
, &icur
, &got
))) {
5474 if (done
|| end
== (xfs_fileoff_t
)-1 || end
< start
)
5477 *rlen
= end
- start
+ 1;
5480 * Convert to a btree if necessary.
5482 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
5483 ASSERT(cur
== NULL
);
5484 error
= xfs_bmap_extents_to_btree(tp
, ip
, &cur
, 0,
5485 &tmp_logflags
, whichfork
);
5486 logflags
|= tmp_logflags
;
5488 error
= xfs_bmap_btree_to_extents(tp
, ip
, cur
, &logflags
,
5494 * Log everything. Do this after conversion, there's no point in
5495 * logging the extent records if we've converted to btree format.
5497 if ((logflags
& xfs_ilog_fext(whichfork
)) &&
5498 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
5499 logflags
&= ~xfs_ilog_fext(whichfork
);
5500 else if ((logflags
& xfs_ilog_fbroot(whichfork
)) &&
5501 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)
5502 logflags
&= ~xfs_ilog_fbroot(whichfork
);
5504 * Log inode even in the error case, if the transaction
5505 * is dirty we'll need to shut down the filesystem.
5508 xfs_trans_log_inode(tp
, ip
, logflags
);
5511 cur
->bc_private
.b
.allocated
= 0;
5512 xfs_btree_del_cursor(cur
, error
);
5517 /* Unmap a range of a file. */
5521 struct xfs_inode
*ip
,
5530 error
= __xfs_bunmapi(tp
, ip
, bno
, &len
, flags
, nexts
);
5536 * Determine whether an extent shift can be accomplished by a merge with the
5537 * extent that precedes the target hole of the shift.
5541 struct xfs_bmbt_irec
*left
, /* preceding extent */
5542 struct xfs_bmbt_irec
*got
, /* current extent to shift */
5543 xfs_fileoff_t shift
) /* shift fsb */
5545 xfs_fileoff_t startoff
;
5547 startoff
= got
->br_startoff
- shift
;
5550 * The extent, once shifted, must be adjacent in-file and on-disk with
5551 * the preceding extent.
5553 if ((left
->br_startoff
+ left
->br_blockcount
!= startoff
) ||
5554 (left
->br_startblock
+ left
->br_blockcount
!= got
->br_startblock
) ||
5555 (left
->br_state
!= got
->br_state
) ||
5556 (left
->br_blockcount
+ got
->br_blockcount
> MAXEXTLEN
))
5563 * A bmap extent shift adjusts the file offset of an extent to fill a preceding
5564 * hole in the file. If an extent shift would result in the extent being fully
5565 * adjacent to the extent that currently precedes the hole, we can merge with
5566 * the preceding extent rather than do the shift.
5568 * This function assumes the caller has verified a shift-by-merge is possible
5569 * with the provided extents via xfs_bmse_can_merge().
5573 struct xfs_trans
*tp
,
5574 struct xfs_inode
*ip
,
5576 xfs_fileoff_t shift
, /* shift fsb */
5577 struct xfs_iext_cursor
*icur
,
5578 struct xfs_bmbt_irec
*got
, /* extent to shift */
5579 struct xfs_bmbt_irec
*left
, /* preceding extent */
5580 struct xfs_btree_cur
*cur
,
5581 int *logflags
) /* output */
5583 struct xfs_bmbt_irec
new;
5584 xfs_filblks_t blockcount
;
5586 struct xfs_mount
*mp
= ip
->i_mount
;
5588 blockcount
= left
->br_blockcount
+ got
->br_blockcount
;
5590 ASSERT(xfs_isilocked(ip
, XFS_IOLOCK_EXCL
));
5591 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
5592 ASSERT(xfs_bmse_can_merge(left
, got
, shift
));
5595 new.br_blockcount
= blockcount
;
5598 * Update the on-disk extent count, the btree if necessary and log the
5601 XFS_IFORK_NEXT_SET(ip
, whichfork
,
5602 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
5603 *logflags
|= XFS_ILOG_CORE
;
5605 *logflags
|= XFS_ILOG_DEXT
;
5609 /* lookup and remove the extent to merge */
5610 error
= xfs_bmbt_lookup_eq(cur
, got
, &i
);
5613 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
5615 error
= xfs_btree_delete(cur
, &i
);
5618 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
5620 /* lookup and update size of the previous extent */
5621 error
= xfs_bmbt_lookup_eq(cur
, left
, &i
);
5624 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
5626 error
= xfs_bmbt_update(cur
, &new);
5631 xfs_iext_remove(ip
, icur
, 0);
5632 xfs_iext_prev(XFS_IFORK_PTR(ip
, whichfork
), icur
);
5633 xfs_iext_update_extent(ip
, xfs_bmap_fork_to_state(whichfork
), icur
,
5636 /* update reverse mapping. rmap functions merge the rmaps for us */
5637 error
= xfs_rmap_unmap_extent(tp
, ip
, whichfork
, got
);
5640 memcpy(&new, got
, sizeof(new));
5641 new.br_startoff
= left
->br_startoff
+ left
->br_blockcount
;
5642 return xfs_rmap_map_extent(tp
, ip
, whichfork
, &new);
5646 xfs_bmap_shift_update_extent(
5647 struct xfs_trans
*tp
,
5648 struct xfs_inode
*ip
,
5650 struct xfs_iext_cursor
*icur
,
5651 struct xfs_bmbt_irec
*got
,
5652 struct xfs_btree_cur
*cur
,
5654 xfs_fileoff_t startoff
)
5656 struct xfs_mount
*mp
= ip
->i_mount
;
5657 struct xfs_bmbt_irec prev
= *got
;
5660 *logflags
|= XFS_ILOG_CORE
;
5662 got
->br_startoff
= startoff
;
5665 error
= xfs_bmbt_lookup_eq(cur
, &prev
, &i
);
5668 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
5670 error
= xfs_bmbt_update(cur
, got
);
5674 *logflags
|= XFS_ILOG_DEXT
;
5677 xfs_iext_update_extent(ip
, xfs_bmap_fork_to_state(whichfork
), icur
,
5680 /* update reverse mapping */
5681 error
= xfs_rmap_unmap_extent(tp
, ip
, whichfork
, &prev
);
5684 return xfs_rmap_map_extent(tp
, ip
, whichfork
, got
);
5688 xfs_bmap_collapse_extents(
5689 struct xfs_trans
*tp
,
5690 struct xfs_inode
*ip
,
5691 xfs_fileoff_t
*next_fsb
,
5692 xfs_fileoff_t offset_shift_fsb
,
5695 int whichfork
= XFS_DATA_FORK
;
5696 struct xfs_mount
*mp
= ip
->i_mount
;
5697 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5698 struct xfs_btree_cur
*cur
= NULL
;
5699 struct xfs_bmbt_irec got
, prev
;
5700 struct xfs_iext_cursor icur
;
5701 xfs_fileoff_t new_startoff
;
5705 if (unlikely(XFS_TEST_ERROR(
5706 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
5707 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
5708 mp
, XFS_ERRTAG_BMAPIFORMAT
))) {
5709 XFS_ERROR_REPORT(__func__
, XFS_ERRLEVEL_LOW
, mp
);
5710 return -EFSCORRUPTED
;
5713 if (XFS_FORCED_SHUTDOWN(mp
))
5716 ASSERT(xfs_isilocked(ip
, XFS_IOLOCK_EXCL
| XFS_ILOCK_EXCL
));
5718 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
5719 error
= xfs_iread_extents(tp
, ip
, whichfork
);
5724 if (ifp
->if_flags
& XFS_IFBROOT
) {
5725 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
5726 cur
->bc_private
.b
.flags
= 0;
5729 if (!xfs_iext_lookup_extent(ip
, ifp
, *next_fsb
, &icur
, &got
)) {
5733 XFS_WANT_CORRUPTED_GOTO(mp
, !isnullstartblock(got
.br_startblock
),
5736 new_startoff
= got
.br_startoff
- offset_shift_fsb
;
5737 if (xfs_iext_peek_prev_extent(ifp
, &icur
, &prev
)) {
5738 if (new_startoff
< prev
.br_startoff
+ prev
.br_blockcount
) {
5743 if (xfs_bmse_can_merge(&prev
, &got
, offset_shift_fsb
)) {
5744 error
= xfs_bmse_merge(tp
, ip
, whichfork
,
5745 offset_shift_fsb
, &icur
, &got
, &prev
,
5752 if (got
.br_startoff
< offset_shift_fsb
) {
5758 error
= xfs_bmap_shift_update_extent(tp
, ip
, whichfork
, &icur
, &got
,
5759 cur
, &logflags
, new_startoff
);
5764 if (!xfs_iext_next_extent(ifp
, &icur
, &got
)) {
5769 *next_fsb
= got
.br_startoff
;
5772 xfs_btree_del_cursor(cur
, error
);
5774 xfs_trans_log_inode(tp
, ip
, logflags
);
5778 /* Make sure we won't be right-shifting an extent past the maximum bound. */
5780 xfs_bmap_can_insert_extents(
5781 struct xfs_inode
*ip
,
5783 xfs_fileoff_t shift
)
5785 struct xfs_bmbt_irec got
;
5789 ASSERT(xfs_isilocked(ip
, XFS_IOLOCK_EXCL
));
5791 if (XFS_FORCED_SHUTDOWN(ip
->i_mount
))
5794 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
5795 error
= xfs_bmap_last_extent(NULL
, ip
, XFS_DATA_FORK
, &got
, &is_empty
);
5796 if (!error
&& !is_empty
&& got
.br_startoff
>= off
&&
5797 ((got
.br_startoff
+ shift
) & BMBT_STARTOFF_MASK
) < got
.br_startoff
)
5799 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
5805 xfs_bmap_insert_extents(
5806 struct xfs_trans
*tp
,
5807 struct xfs_inode
*ip
,
5808 xfs_fileoff_t
*next_fsb
,
5809 xfs_fileoff_t offset_shift_fsb
,
5811 xfs_fileoff_t stop_fsb
)
5813 int whichfork
= XFS_DATA_FORK
;
5814 struct xfs_mount
*mp
= ip
->i_mount
;
5815 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5816 struct xfs_btree_cur
*cur
= NULL
;
5817 struct xfs_bmbt_irec got
, next
;
5818 struct xfs_iext_cursor icur
;
5819 xfs_fileoff_t new_startoff
;
5823 if (unlikely(XFS_TEST_ERROR(
5824 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
5825 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
5826 mp
, XFS_ERRTAG_BMAPIFORMAT
))) {
5827 XFS_ERROR_REPORT(__func__
, XFS_ERRLEVEL_LOW
, mp
);
5828 return -EFSCORRUPTED
;
5831 if (XFS_FORCED_SHUTDOWN(mp
))
5834 ASSERT(xfs_isilocked(ip
, XFS_IOLOCK_EXCL
| XFS_ILOCK_EXCL
));
5836 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
5837 error
= xfs_iread_extents(tp
, ip
, whichfork
);
5842 if (ifp
->if_flags
& XFS_IFBROOT
) {
5843 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
5844 cur
->bc_private
.b
.flags
= 0;
5847 if (*next_fsb
== NULLFSBLOCK
) {
5848 xfs_iext_last(ifp
, &icur
);
5849 if (!xfs_iext_get_extent(ifp
, &icur
, &got
) ||
5850 stop_fsb
> got
.br_startoff
) {
5855 if (!xfs_iext_lookup_extent(ip
, ifp
, *next_fsb
, &icur
, &got
)) {
5860 XFS_WANT_CORRUPTED_GOTO(mp
, !isnullstartblock(got
.br_startblock
),
5863 if (stop_fsb
>= got
.br_startoff
+ got
.br_blockcount
) {
5868 new_startoff
= got
.br_startoff
+ offset_shift_fsb
;
5869 if (xfs_iext_peek_next_extent(ifp
, &icur
, &next
)) {
5870 if (new_startoff
+ got
.br_blockcount
> next
.br_startoff
) {
5876 * Unlike a left shift (which involves a hole punch), a right
5877 * shift does not modify extent neighbors in any way. We should
5878 * never find mergeable extents in this scenario. Check anyways
5879 * and warn if we encounter two extents that could be one.
5881 if (xfs_bmse_can_merge(&got
, &next
, offset_shift_fsb
))
5885 error
= xfs_bmap_shift_update_extent(tp
, ip
, whichfork
, &icur
, &got
,
5886 cur
, &logflags
, new_startoff
);
5890 if (!xfs_iext_prev_extent(ifp
, &icur
, &got
) ||
5891 stop_fsb
>= got
.br_startoff
+ got
.br_blockcount
) {
5896 *next_fsb
= got
.br_startoff
;
5899 xfs_btree_del_cursor(cur
, error
);
5901 xfs_trans_log_inode(tp
, ip
, logflags
);
5906 * Splits an extent into two extents at split_fsb block such that it is the
5907 * first block of the current_ext. @ext is a target extent to be split.
5908 * @split_fsb is a block where the extents is split. If split_fsb lies in a
5909 * hole or the first block of extents, just return 0.
5912 xfs_bmap_split_extent_at(
5913 struct xfs_trans
*tp
,
5914 struct xfs_inode
*ip
,
5915 xfs_fileoff_t split_fsb
)
5917 int whichfork
= XFS_DATA_FORK
;
5918 struct xfs_btree_cur
*cur
= NULL
;
5919 struct xfs_bmbt_irec got
;
5920 struct xfs_bmbt_irec
new; /* split extent */
5921 struct xfs_mount
*mp
= ip
->i_mount
;
5922 struct xfs_ifork
*ifp
;
5923 xfs_fsblock_t gotblkcnt
; /* new block count for got */
5924 struct xfs_iext_cursor icur
;
5929 if (unlikely(XFS_TEST_ERROR(
5930 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
5931 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
5932 mp
, XFS_ERRTAG_BMAPIFORMAT
))) {
5933 XFS_ERROR_REPORT("xfs_bmap_split_extent_at",
5934 XFS_ERRLEVEL_LOW
, mp
);
5935 return -EFSCORRUPTED
;
5938 if (XFS_FORCED_SHUTDOWN(mp
))
5941 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5942 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
5943 /* Read in all the extents */
5944 error
= xfs_iread_extents(tp
, ip
, whichfork
);
5950 * If there are not extents, or split_fsb lies in a hole we are done.
5952 if (!xfs_iext_lookup_extent(ip
, ifp
, split_fsb
, &icur
, &got
) ||
5953 got
.br_startoff
>= split_fsb
)
5956 gotblkcnt
= split_fsb
- got
.br_startoff
;
5957 new.br_startoff
= split_fsb
;
5958 new.br_startblock
= got
.br_startblock
+ gotblkcnt
;
5959 new.br_blockcount
= got
.br_blockcount
- gotblkcnt
;
5960 new.br_state
= got
.br_state
;
5962 if (ifp
->if_flags
& XFS_IFBROOT
) {
5963 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
5964 cur
->bc_private
.b
.flags
= 0;
5965 error
= xfs_bmbt_lookup_eq(cur
, &got
, &i
);
5968 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, del_cursor
);
5971 got
.br_blockcount
= gotblkcnt
;
5972 xfs_iext_update_extent(ip
, xfs_bmap_fork_to_state(whichfork
), &icur
,
5975 logflags
= XFS_ILOG_CORE
;
5977 error
= xfs_bmbt_update(cur
, &got
);
5981 logflags
|= XFS_ILOG_DEXT
;
5983 /* Add new extent */
5984 xfs_iext_next(ifp
, &icur
);
5985 xfs_iext_insert(ip
, &icur
, &new, 0);
5986 XFS_IFORK_NEXT_SET(ip
, whichfork
,
5987 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
5990 error
= xfs_bmbt_lookup_eq(cur
, &new, &i
);
5993 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, del_cursor
);
5994 error
= xfs_btree_insert(cur
, &i
);
5997 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, del_cursor
);
6001 * Convert to a btree if necessary.
6003 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
6004 int tmp_logflags
; /* partial log flag return val */
6006 ASSERT(cur
== NULL
);
6007 error
= xfs_bmap_extents_to_btree(tp
, ip
, &cur
, 0,
6008 &tmp_logflags
, whichfork
);
6009 logflags
|= tmp_logflags
;
6014 cur
->bc_private
.b
.allocated
= 0;
6015 xfs_btree_del_cursor(cur
, error
);
6019 xfs_trans_log_inode(tp
, ip
, logflags
);
6024 xfs_bmap_split_extent(
6025 struct xfs_inode
*ip
,
6026 xfs_fileoff_t split_fsb
)
6028 struct xfs_mount
*mp
= ip
->i_mount
;
6029 struct xfs_trans
*tp
;
6032 error
= xfs_trans_alloc(mp
, &M_RES(mp
)->tr_write
,
6033 XFS_DIOSTRAT_SPACE_RES(mp
, 0), 0, 0, &tp
);
6037 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
6038 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
);
6040 error
= xfs_bmap_split_extent_at(tp
, ip
, split_fsb
);
6044 return xfs_trans_commit(tp
);
6047 xfs_trans_cancel(tp
);
6051 /* Deferred mapping is only for real extents in the data fork. */
6053 xfs_bmap_is_update_needed(
6054 struct xfs_bmbt_irec
*bmap
)
6056 return bmap
->br_startblock
!= HOLESTARTBLOCK
&&
6057 bmap
->br_startblock
!= DELAYSTARTBLOCK
;
6060 /* Record a bmap intent. */
6063 struct xfs_trans
*tp
,
6064 enum xfs_bmap_intent_type type
,
6065 struct xfs_inode
*ip
,
6067 struct xfs_bmbt_irec
*bmap
)
6069 struct xfs_bmap_intent
*bi
;
6071 trace_xfs_bmap_defer(tp
->t_mountp
,
6072 XFS_FSB_TO_AGNO(tp
->t_mountp
, bmap
->br_startblock
),
6074 XFS_FSB_TO_AGBNO(tp
->t_mountp
, bmap
->br_startblock
),
6075 ip
->i_ino
, whichfork
,
6077 bmap
->br_blockcount
,
6080 bi
= kmem_alloc(sizeof(struct xfs_bmap_intent
), KM_SLEEP
| KM_NOFS
);
6081 INIT_LIST_HEAD(&bi
->bi_list
);
6084 bi
->bi_whichfork
= whichfork
;
6085 bi
->bi_bmap
= *bmap
;
6087 xfs_defer_add(tp
, XFS_DEFER_OPS_TYPE_BMAP
, &bi
->bi_list
);
6091 /* Map an extent into a file. */
6093 xfs_bmap_map_extent(
6094 struct xfs_trans
*tp
,
6095 struct xfs_inode
*ip
,
6096 struct xfs_bmbt_irec
*PREV
)
6098 if (!xfs_bmap_is_update_needed(PREV
))
6101 return __xfs_bmap_add(tp
, XFS_BMAP_MAP
, ip
, XFS_DATA_FORK
, PREV
);
6104 /* Unmap an extent out of a file. */
6106 xfs_bmap_unmap_extent(
6107 struct xfs_trans
*tp
,
6108 struct xfs_inode
*ip
,
6109 struct xfs_bmbt_irec
*PREV
)
6111 if (!xfs_bmap_is_update_needed(PREV
))
6114 return __xfs_bmap_add(tp
, XFS_BMAP_UNMAP
, ip
, XFS_DATA_FORK
, PREV
);
6118 * Process one of the deferred bmap operations. We pass back the
6119 * btree cursor to maintain our lock on the bmapbt between calls.
6122 xfs_bmap_finish_one(
6123 struct xfs_trans
*tp
,
6124 struct xfs_inode
*ip
,
6125 enum xfs_bmap_intent_type type
,
6127 xfs_fileoff_t startoff
,
6128 xfs_fsblock_t startblock
,
6129 xfs_filblks_t
*blockcount
,
6134 ASSERT(tp
->t_firstblock
== NULLFSBLOCK
);
6136 trace_xfs_bmap_deferred(tp
->t_mountp
,
6137 XFS_FSB_TO_AGNO(tp
->t_mountp
, startblock
), type
,
6138 XFS_FSB_TO_AGBNO(tp
->t_mountp
, startblock
),
6139 ip
->i_ino
, whichfork
, startoff
, *blockcount
, state
);
6141 if (WARN_ON_ONCE(whichfork
!= XFS_DATA_FORK
))
6142 return -EFSCORRUPTED
;
6144 if (XFS_TEST_ERROR(false, tp
->t_mountp
,
6145 XFS_ERRTAG_BMAP_FINISH_ONE
))
6150 error
= xfs_bmapi_remap(tp
, ip
, startoff
, *blockcount
,
6154 case XFS_BMAP_UNMAP
:
6155 error
= __xfs_bunmapi(tp
, ip
, startoff
, blockcount
,
6156 XFS_BMAPI_REMAP
, 1);
6160 error
= -EFSCORRUPTED
;
6166 /* Check that an inode's extent does not have invalid flags or bad ranges. */
6168 xfs_bmap_validate_extent(
6169 struct xfs_inode
*ip
,
6171 struct xfs_bmbt_irec
*irec
)
6173 struct xfs_mount
*mp
= ip
->i_mount
;
6174 xfs_fsblock_t endfsb
;
6177 isrt
= XFS_IS_REALTIME_INODE(ip
);
6178 endfsb
= irec
->br_startblock
+ irec
->br_blockcount
- 1;
6180 if (!xfs_verify_rtbno(mp
, irec
->br_startblock
))
6181 return __this_address
;
6182 if (!xfs_verify_rtbno(mp
, endfsb
))
6183 return __this_address
;
6185 if (!xfs_verify_fsbno(mp
, irec
->br_startblock
))
6186 return __this_address
;
6187 if (!xfs_verify_fsbno(mp
, endfsb
))
6188 return __this_address
;
6189 if (XFS_FSB_TO_AGNO(mp
, irec
->br_startblock
) !=
6190 XFS_FSB_TO_AGNO(mp
, endfsb
))
6191 return __this_address
;
6193 if (irec
->br_state
!= XFS_EXT_NORM
&& whichfork
!= XFS_DATA_FORK
)
6194 return __this_address
;