1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
6 #include "libxfs_priv.h"
8 #include "xfs_shared.h"
9 #include "xfs_format.h"
10 #include "xfs_log_format.h"
11 #include "xfs_trans_resv.h"
14 #include "xfs_mount.h"
15 #include "xfs_defer.h"
16 #include "xfs_da_format.h"
17 #include "xfs_da_btree.h"
19 #include "xfs_inode.h"
20 #include "xfs_btree.h"
21 #include "xfs_trans.h"
22 #include "xfs_alloc.h"
24 #include "xfs_bmap_btree.h"
25 #include "xfs_errortag.h"
26 #include "xfs_trans_space.h"
27 #include "xfs_trace.h"
28 #include "xfs_attr_leaf.h"
29 #include "xfs_quota_defs.h"
31 #include "xfs_ag_resv.h"
32 #include "xfs_refcount.h"
35 kmem_zone_t
*xfs_bmap_free_item_zone
;
38 * Miscellaneous helper functions
42 * Compute and fill in the value of the maximum depth of a bmap btree
43 * in this filesystem. Done once, during mount.
46 xfs_bmap_compute_maxlevels(
47 xfs_mount_t
*mp
, /* file system mount structure */
48 int whichfork
) /* data or attr fork */
50 int level
; /* btree level */
51 uint maxblocks
; /* max blocks at this level */
52 uint maxleafents
; /* max leaf entries possible */
53 int maxrootrecs
; /* max records in root block */
54 int minleafrecs
; /* min records in leaf block */
55 int minnoderecs
; /* min records in node block */
56 int sz
; /* root block size */
59 * The maximum number of extents in a file, hence the maximum
60 * number of leaf entries, is controlled by the type of di_nextents
61 * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
62 * (a signed 16-bit number, xfs_aextnum_t).
64 * Note that we can no longer assume that if we are in ATTR1 that
65 * the fork offset of all the inodes will be
66 * (xfs_default_attroffset(ip) >> 3) because we could have mounted
67 * with ATTR2 and then mounted back with ATTR1, keeping the
68 * di_forkoff's fixed but probably at various positions. Therefore,
69 * for both ATTR1 and ATTR2 we have to assume the worst case scenario
70 * of a minimum size available.
72 if (whichfork
== XFS_DATA_FORK
) {
73 maxleafents
= MAXEXTNUM
;
74 sz
= XFS_BMDR_SPACE_CALC(MINDBTPTRS
);
76 maxleafents
= MAXAEXTNUM
;
77 sz
= XFS_BMDR_SPACE_CALC(MINABTPTRS
);
79 maxrootrecs
= xfs_bmdr_maxrecs(sz
, 0);
80 minleafrecs
= mp
->m_bmap_dmnr
[0];
81 minnoderecs
= mp
->m_bmap_dmnr
[1];
82 maxblocks
= (maxleafents
+ minleafrecs
- 1) / minleafrecs
;
83 for (level
= 1; maxblocks
> 1; level
++) {
84 if (maxblocks
<= maxrootrecs
)
87 maxblocks
= (maxblocks
+ minnoderecs
- 1) / minnoderecs
;
89 mp
->m_bm_maxlevels
[whichfork
] = level
;
92 STATIC
int /* error */
94 struct xfs_btree_cur
*cur
,
95 struct xfs_bmbt_irec
*irec
,
96 int *stat
) /* success/failure */
98 cur
->bc_rec
.b
= *irec
;
99 return xfs_btree_lookup(cur
, XFS_LOOKUP_EQ
, stat
);
102 STATIC
int /* error */
103 xfs_bmbt_lookup_first(
104 struct xfs_btree_cur
*cur
,
105 int *stat
) /* success/failure */
107 cur
->bc_rec
.b
.br_startoff
= 0;
108 cur
->bc_rec
.b
.br_startblock
= 0;
109 cur
->bc_rec
.b
.br_blockcount
= 0;
110 return xfs_btree_lookup(cur
, XFS_LOOKUP_GE
, stat
);
114 * Check if the inode needs to be converted to btree format.
116 static inline bool xfs_bmap_needs_btree(struct xfs_inode
*ip
, int whichfork
)
118 return whichfork
!= XFS_COW_FORK
&&
119 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
&&
120 XFS_IFORK_NEXTENTS(ip
, whichfork
) >
121 XFS_IFORK_MAXEXT(ip
, whichfork
);
125 * Check if the inode should be converted to extent format.
127 static inline bool xfs_bmap_wants_extents(struct xfs_inode
*ip
, int whichfork
)
129 return whichfork
!= XFS_COW_FORK
&&
130 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
&&
131 XFS_IFORK_NEXTENTS(ip
, whichfork
) <=
132 XFS_IFORK_MAXEXT(ip
, whichfork
);
136 * Update the record referred to by cur to the value given by irec
137 * This either works (return 0) or gets an EFSCORRUPTED error.
141 struct xfs_btree_cur
*cur
,
142 struct xfs_bmbt_irec
*irec
)
144 union xfs_btree_rec rec
;
146 xfs_bmbt_disk_set_all(&rec
.bmbt
, irec
);
147 return xfs_btree_update(cur
, &rec
);
151 * Compute the worst-case number of indirect blocks that will be used
152 * for ip's delayed extent of length "len".
155 xfs_bmap_worst_indlen(
156 xfs_inode_t
*ip
, /* incore inode pointer */
157 xfs_filblks_t len
) /* delayed extent length */
159 int level
; /* btree level number */
160 int maxrecs
; /* maximum record count at this level */
161 xfs_mount_t
*mp
; /* mount structure */
162 xfs_filblks_t rval
; /* return value */
165 maxrecs
= mp
->m_bmap_dmxr
[0];
166 for (level
= 0, rval
= 0;
167 level
< XFS_BM_MAXLEVELS(mp
, XFS_DATA_FORK
);
170 do_div(len
, maxrecs
);
173 return rval
+ XFS_BM_MAXLEVELS(mp
, XFS_DATA_FORK
) -
176 maxrecs
= mp
->m_bmap_dmxr
[1];
182 * Calculate the default attribute fork offset for newly created inodes.
185 xfs_default_attroffset(
186 struct xfs_inode
*ip
)
188 struct xfs_mount
*mp
= ip
->i_mount
;
191 if (mp
->m_sb
.sb_inodesize
== 256) {
192 offset
= XFS_LITINO(mp
, ip
->i_d
.di_version
) -
193 XFS_BMDR_SPACE_CALC(MINABTPTRS
);
195 offset
= XFS_BMDR_SPACE_CALC(6 * MINABTPTRS
);
198 ASSERT(offset
< XFS_LITINO(mp
, ip
->i_d
.di_version
));
203 * Helper routine to reset inode di_forkoff field when switching
204 * attribute fork from local to extent format - we reset it where
205 * possible to make space available for inline data fork extents.
208 xfs_bmap_forkoff_reset(
212 if (whichfork
== XFS_ATTR_FORK
&&
213 ip
->i_d
.di_format
!= XFS_DINODE_FMT_DEV
&&
214 ip
->i_d
.di_format
!= XFS_DINODE_FMT_BTREE
) {
215 uint dfl_forkoff
= xfs_default_attroffset(ip
) >> 3;
217 if (dfl_forkoff
> ip
->i_d
.di_forkoff
)
218 ip
->i_d
.di_forkoff
= dfl_forkoff
;
223 STATIC
struct xfs_buf
*
225 struct xfs_btree_cur
*cur
,
228 struct xfs_log_item
*lip
;
234 for (i
= 0; i
< XFS_BTREE_MAXLEVELS
; i
++) {
235 if (!cur
->bc_bufs
[i
])
237 if (XFS_BUF_ADDR(cur
->bc_bufs
[i
]) == bno
)
238 return cur
->bc_bufs
[i
];
241 /* Chase down all the log items to see if the bp is there */
242 list_for_each_entry(lip
, &cur
->bc_tp
->t_items
, li_trans
) {
243 struct xfs_buf_log_item
*bip
= (struct xfs_buf_log_item
*)lip
;
245 if (bip
->bli_item
.li_type
== XFS_LI_BUF
&&
246 XFS_BUF_ADDR(bip
->bli_buf
) == bno
)
255 struct xfs_btree_block
*block
,
261 __be64
*pp
, *thispa
; /* pointer to block address */
262 xfs_bmbt_key_t
*prevp
, *keyp
;
264 ASSERT(be16_to_cpu(block
->bb_level
) > 0);
267 for( i
= 1; i
<= xfs_btree_get_numrecs(block
); i
++) {
268 dmxr
= mp
->m_bmap_dmxr
[0];
269 keyp
= XFS_BMBT_KEY_ADDR(mp
, block
, i
);
272 ASSERT(be64_to_cpu(prevp
->br_startoff
) <
273 be64_to_cpu(keyp
->br_startoff
));
278 * Compare the block numbers to see if there are dups.
281 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, i
, sz
);
283 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, i
, dmxr
);
285 for (j
= i
+1; j
<= be16_to_cpu(block
->bb_numrecs
); j
++) {
287 thispa
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, j
, sz
);
289 thispa
= XFS_BMBT_PTR_ADDR(mp
, block
, j
, dmxr
);
290 if (*thispa
== *pp
) {
291 xfs_warn(mp
, "%s: thispa(%d) == pp(%d) %Ld",
293 (unsigned long long)be64_to_cpu(*thispa
));
294 xfs_err(mp
, "%s: ptrs are equal in node\n",
296 xfs_force_shutdown(mp
, SHUTDOWN_CORRUPT_INCORE
);
303 * Check that the extents for the inode ip are in the right order in all
304 * btree leaves. THis becomes prohibitively expensive for large extent count
305 * files, so don't bother with inodes that have more than 10,000 extents in
306 * them. The btree record ordering checks will still be done, so for such large
307 * bmapbt constructs that is going to catch most corruptions.
310 xfs_bmap_check_leaf_extents(
311 xfs_btree_cur_t
*cur
, /* btree cursor or null */
312 xfs_inode_t
*ip
, /* incore inode pointer */
313 int whichfork
) /* data or attr fork */
315 struct xfs_btree_block
*block
; /* current btree block */
316 xfs_fsblock_t bno
; /* block # of "block" */
317 xfs_buf_t
*bp
; /* buffer for "block" */
318 int error
; /* error return value */
319 xfs_extnum_t i
=0, j
; /* index into the extents list */
320 struct xfs_ifork
*ifp
; /* fork structure */
321 int level
; /* btree level, for checking */
322 xfs_mount_t
*mp
; /* file system mount structure */
323 __be64
*pp
; /* pointer to block address */
324 xfs_bmbt_rec_t
*ep
; /* pointer to current extent */
325 xfs_bmbt_rec_t last
= {0, 0}; /* last extent in prev block */
326 xfs_bmbt_rec_t
*nextp
; /* pointer to next extent */
329 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
) {
333 /* skip large extent count inodes */
334 if (ip
->i_d
.di_nextents
> 10000)
339 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
340 block
= ifp
->if_broot
;
342 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
344 level
= be16_to_cpu(block
->bb_level
);
346 xfs_check_block(block
, mp
, 1, ifp
->if_broot_bytes
);
347 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, 1, ifp
->if_broot_bytes
);
348 bno
= be64_to_cpu(*pp
);
350 ASSERT(bno
!= NULLFSBLOCK
);
351 ASSERT(XFS_FSB_TO_AGNO(mp
, bno
) < mp
->m_sb
.sb_agcount
);
352 ASSERT(XFS_FSB_TO_AGBNO(mp
, bno
) < mp
->m_sb
.sb_agblocks
);
355 * Go down the tree until leaf level is reached, following the first
356 * pointer (leftmost) at each level.
358 while (level
-- > 0) {
359 /* See if buf is in cur first */
361 bp
= xfs_bmap_get_bp(cur
, XFS_FSB_TO_DADDR(mp
, bno
));
364 error
= xfs_btree_read_bufl(mp
, NULL
, bno
, 0, &bp
,
370 block
= XFS_BUF_TO_BLOCK(bp
);
375 * Check this block for basic sanity (increasing keys and
376 * no duplicate blocks).
379 xfs_check_block(block
, mp
, 0, 0);
380 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, mp
->m_bmap_dmxr
[1]);
381 bno
= be64_to_cpu(*pp
);
382 XFS_WANT_CORRUPTED_GOTO(mp
,
383 xfs_verify_fsbno(mp
, bno
), error0
);
386 xfs_trans_brelse(NULL
, bp
);
391 * Here with bp and block set to the leftmost leaf node in the tree.
396 * Loop over all leaf nodes checking that all extents are in the right order.
399 xfs_fsblock_t nextbno
;
400 xfs_extnum_t num_recs
;
403 num_recs
= xfs_btree_get_numrecs(block
);
406 * Read-ahead the next leaf block, if any.
409 nextbno
= be64_to_cpu(block
->bb_u
.l
.bb_rightsib
);
412 * Check all the extents to make sure they are OK.
413 * If we had a previous block, the last entry should
414 * conform with the first entry in this one.
417 ep
= XFS_BMBT_REC_ADDR(mp
, block
, 1);
419 ASSERT(xfs_bmbt_disk_get_startoff(&last
) +
420 xfs_bmbt_disk_get_blockcount(&last
) <=
421 xfs_bmbt_disk_get_startoff(ep
));
423 for (j
= 1; j
< num_recs
; j
++) {
424 nextp
= XFS_BMBT_REC_ADDR(mp
, block
, j
+ 1);
425 ASSERT(xfs_bmbt_disk_get_startoff(ep
) +
426 xfs_bmbt_disk_get_blockcount(ep
) <=
427 xfs_bmbt_disk_get_startoff(nextp
));
435 xfs_trans_brelse(NULL
, bp
);
439 * If we've reached the end, stop.
441 if (bno
== NULLFSBLOCK
)
445 bp
= xfs_bmap_get_bp(cur
, XFS_FSB_TO_DADDR(mp
, bno
));
448 error
= xfs_btree_read_bufl(mp
, NULL
, bno
, 0, &bp
,
454 block
= XFS_BUF_TO_BLOCK(bp
);
460 xfs_warn(mp
, "%s: at error0", __func__
);
462 xfs_trans_brelse(NULL
, bp
);
464 xfs_warn(mp
, "%s: BAD after btree leaves for %d extents",
466 xfs_err(mp
, "%s: CORRUPTED BTREE OR SOMETHING", __func__
);
467 xfs_force_shutdown(mp
, SHUTDOWN_CORRUPT_INCORE
);
472 * Validate that the bmbt_irecs being returned from bmapi are valid
473 * given the caller's original parameters. Specifically check the
474 * ranges of the returned irecs to ensure that they only extend beyond
475 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
478 xfs_bmap_validate_ret(
482 xfs_bmbt_irec_t
*mval
,
486 int i
; /* index to map values */
488 ASSERT(ret_nmap
<= nmap
);
490 for (i
= 0; i
< ret_nmap
; i
++) {
491 ASSERT(mval
[i
].br_blockcount
> 0);
492 if (!(flags
& XFS_BMAPI_ENTIRE
)) {
493 ASSERT(mval
[i
].br_startoff
>= bno
);
494 ASSERT(mval
[i
].br_blockcount
<= len
);
495 ASSERT(mval
[i
].br_startoff
+ mval
[i
].br_blockcount
<=
498 ASSERT(mval
[i
].br_startoff
< bno
+ len
);
499 ASSERT(mval
[i
].br_startoff
+ mval
[i
].br_blockcount
>
503 mval
[i
- 1].br_startoff
+ mval
[i
- 1].br_blockcount
==
504 mval
[i
].br_startoff
);
505 ASSERT(mval
[i
].br_startblock
!= DELAYSTARTBLOCK
&&
506 mval
[i
].br_startblock
!= HOLESTARTBLOCK
);
507 ASSERT(mval
[i
].br_state
== XFS_EXT_NORM
||
508 mval
[i
].br_state
== XFS_EXT_UNWRITTEN
);
513 #define xfs_bmap_check_leaf_extents(cur, ip, whichfork) do { } while (0)
514 #define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap) do { } while (0)
518 * bmap free list manipulation functions
522 * Add the extent to the list of extents to be free at transaction end.
523 * The list is maintained sorted (by block number).
527 struct xfs_trans
*tp
,
530 struct xfs_owner_info
*oinfo
,
533 struct xfs_extent_free_item
*new; /* new element */
535 struct xfs_mount
*mp
= tp
->t_mountp
;
539 ASSERT(bno
!= NULLFSBLOCK
);
541 ASSERT(len
<= MAXEXTLEN
);
542 ASSERT(!isnullstartblock(bno
));
543 agno
= XFS_FSB_TO_AGNO(mp
, bno
);
544 agbno
= XFS_FSB_TO_AGBNO(mp
, bno
);
545 ASSERT(agno
< mp
->m_sb
.sb_agcount
);
546 ASSERT(agbno
< mp
->m_sb
.sb_agblocks
);
547 ASSERT(len
< mp
->m_sb
.sb_agblocks
);
548 ASSERT(agbno
+ len
<= mp
->m_sb
.sb_agblocks
);
550 ASSERT(xfs_bmap_free_item_zone
!= NULL
);
552 new = kmem_zone_alloc(xfs_bmap_free_item_zone
, KM_SLEEP
);
553 new->xefi_startblock
= bno
;
554 new->xefi_blockcount
= (xfs_extlen_t
)len
;
556 new->xefi_oinfo
= *oinfo
;
558 xfs_rmap_skip_owner_update(&new->xefi_oinfo
);
559 new->xefi_skip_discard
= skip_discard
;
560 trace_xfs_bmap_free_defer(tp
->t_mountp
,
561 XFS_FSB_TO_AGNO(tp
->t_mountp
, bno
), 0,
562 XFS_FSB_TO_AGBNO(tp
->t_mountp
, bno
), len
);
563 xfs_defer_add(tp
, XFS_DEFER_OPS_TYPE_FREE
, &new->xefi_list
);
567 * Inode fork format manipulation functions
571 * Transform a btree format file with only one leaf node, where the
572 * extents list will fit in the inode, into an extents format file.
573 * Since the file extents are already in-core, all we have to do is
574 * give up the space for the btree root and pitch the leaf block.
576 STATIC
int /* error */
577 xfs_bmap_btree_to_extents(
578 xfs_trans_t
*tp
, /* transaction pointer */
579 xfs_inode_t
*ip
, /* incore inode pointer */
580 xfs_btree_cur_t
*cur
, /* btree cursor */
581 int *logflagsp
, /* inode logging flags */
582 int whichfork
) /* data or attr fork */
585 struct xfs_btree_block
*cblock
;/* child btree block */
586 xfs_fsblock_t cbno
; /* child block number */
587 xfs_buf_t
*cbp
; /* child block's buffer */
588 int error
; /* error return value */
589 struct xfs_ifork
*ifp
; /* inode fork data */
590 xfs_mount_t
*mp
; /* mount point structure */
591 __be64
*pp
; /* ptr to block address */
592 struct xfs_btree_block
*rblock
;/* root btree block */
593 struct xfs_owner_info oinfo
;
596 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
597 ASSERT(whichfork
!= XFS_COW_FORK
);
598 ASSERT(ifp
->if_flags
& XFS_IFEXTENTS
);
599 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
);
600 rblock
= ifp
->if_broot
;
601 ASSERT(be16_to_cpu(rblock
->bb_level
) == 1);
602 ASSERT(be16_to_cpu(rblock
->bb_numrecs
) == 1);
603 ASSERT(xfs_bmbt_maxrecs(mp
, ifp
->if_broot_bytes
, 0) == 1);
604 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, rblock
, 1, ifp
->if_broot_bytes
);
605 cbno
= be64_to_cpu(*pp
);
608 XFS_WANT_CORRUPTED_RETURN(cur
->bc_mp
,
609 xfs_btree_check_lptr(cur
, cbno
, 1));
611 error
= xfs_btree_read_bufl(mp
, tp
, cbno
, 0, &cbp
, XFS_BMAP_BTREE_REF
,
615 cblock
= XFS_BUF_TO_BLOCK(cbp
);
616 if ((error
= xfs_btree_check_block(cur
, cblock
, 0, cbp
)))
618 xfs_rmap_ino_bmbt_owner(&oinfo
, ip
->i_ino
, whichfork
);
619 xfs_bmap_add_free(cur
->bc_tp
, cbno
, 1, &oinfo
);
620 ip
->i_d
.di_nblocks
--;
621 xfs_trans_mod_dquot_byino(tp
, ip
, XFS_TRANS_DQ_BCOUNT
, -1L);
622 xfs_trans_binval(tp
, cbp
);
623 if (cur
->bc_bufs
[0] == cbp
)
624 cur
->bc_bufs
[0] = NULL
;
625 xfs_iroot_realloc(ip
, -1, whichfork
);
626 ASSERT(ifp
->if_broot
== NULL
);
627 ASSERT((ifp
->if_flags
& XFS_IFBROOT
) == 0);
628 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_EXTENTS
);
629 *logflagsp
= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
634 * Convert an extents-format file into a btree-format file.
635 * The new file will have a root block (in the inode) and a single child block.
637 STATIC
int /* error */
638 xfs_bmap_extents_to_btree(
639 struct xfs_trans
*tp
, /* transaction pointer */
640 struct xfs_inode
*ip
, /* incore inode pointer */
641 struct xfs_btree_cur
**curp
, /* cursor returned to caller */
642 int wasdel
, /* converting a delayed alloc */
643 int *logflagsp
, /* inode logging flags */
644 int whichfork
) /* data or attr fork */
646 struct xfs_btree_block
*ablock
; /* allocated (child) bt block */
647 struct xfs_buf
*abp
; /* buffer for ablock */
648 struct xfs_alloc_arg args
; /* allocation arguments */
649 struct xfs_bmbt_rec
*arp
; /* child record pointer */
650 struct xfs_btree_block
*block
; /* btree root block */
651 struct xfs_btree_cur
*cur
; /* bmap btree cursor */
652 int error
; /* error return value */
653 struct xfs_ifork
*ifp
; /* inode fork pointer */
654 struct xfs_bmbt_key
*kp
; /* root block key pointer */
655 struct xfs_mount
*mp
; /* mount structure */
656 xfs_bmbt_ptr_t
*pp
; /* root block address pointer */
657 struct xfs_iext_cursor icur
;
658 struct xfs_bmbt_irec rec
;
659 xfs_extnum_t cnt
= 0;
662 ASSERT(whichfork
!= XFS_COW_FORK
);
663 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
664 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
);
667 * Make space in the inode incore. This needs to be undone if we fail
668 * to expand the root.
670 xfs_iroot_realloc(ip
, 1, whichfork
);
671 ifp
->if_flags
|= XFS_IFBROOT
;
676 block
= ifp
->if_broot
;
677 xfs_btree_init_block_int(mp
, block
, XFS_BUF_DADDR_NULL
,
678 XFS_BTNUM_BMAP
, 1, 1, ip
->i_ino
,
679 XFS_BTREE_LONG_PTRS
);
681 * Need a cursor. Can't allocate until bb_level is filled in.
683 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
684 cur
->bc_private
.b
.flags
= wasdel
? XFS_BTCUR_BPRV_WASDEL
: 0;
686 * Convert to a btree with two levels, one record in root.
688 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_BTREE
);
689 memset(&args
, 0, sizeof(args
));
692 xfs_rmap_ino_bmbt_owner(&args
.oinfo
, ip
->i_ino
, whichfork
);
693 if (tp
->t_firstblock
== NULLFSBLOCK
) {
694 args
.type
= XFS_ALLOCTYPE_START_BNO
;
695 args
.fsbno
= XFS_INO_TO_FSB(mp
, ip
->i_ino
);
696 } else if (tp
->t_flags
& XFS_TRANS_LOWMODE
) {
697 args
.type
= XFS_ALLOCTYPE_START_BNO
;
698 args
.fsbno
= tp
->t_firstblock
;
700 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
701 args
.fsbno
= tp
->t_firstblock
;
703 args
.minlen
= args
.maxlen
= args
.prod
= 1;
704 args
.wasdel
= wasdel
;
706 error
= xfs_alloc_vextent(&args
);
708 goto out_root_realloc
;
710 if (WARN_ON_ONCE(args
.fsbno
== NULLFSBLOCK
)) {
712 goto out_root_realloc
;
716 * Allocation can't fail, the space was reserved.
718 ASSERT(tp
->t_firstblock
== NULLFSBLOCK
||
719 args
.agno
>= XFS_FSB_TO_AGNO(mp
, tp
->t_firstblock
));
720 tp
->t_firstblock
= args
.fsbno
;
721 cur
->bc_private
.b
.allocated
++;
722 ip
->i_d
.di_nblocks
++;
723 xfs_trans_mod_dquot_byino(tp
, ip
, XFS_TRANS_DQ_BCOUNT
, 1L);
724 abp
= xfs_btree_get_bufl(mp
, tp
, args
.fsbno
, 0);
726 error
= -EFSCORRUPTED
;
727 goto out_unreserve_dquot
;
731 * Fill in the child block.
733 abp
->b_ops
= &xfs_bmbt_buf_ops
;
734 ablock
= XFS_BUF_TO_BLOCK(abp
);
735 xfs_btree_init_block_int(mp
, ablock
, abp
->b_bn
,
736 XFS_BTNUM_BMAP
, 0, 0, ip
->i_ino
,
737 XFS_BTREE_LONG_PTRS
);
739 for_each_xfs_iext(ifp
, &icur
, &rec
) {
740 if (isnullstartblock(rec
.br_startblock
))
742 arp
= XFS_BMBT_REC_ADDR(mp
, ablock
, 1 + cnt
);
743 xfs_bmbt_disk_set_all(arp
, &rec
);
746 ASSERT(cnt
== XFS_IFORK_NEXTENTS(ip
, whichfork
));
747 xfs_btree_set_numrecs(ablock
, cnt
);
750 * Fill in the root key and pointer.
752 kp
= XFS_BMBT_KEY_ADDR(mp
, block
, 1);
753 arp
= XFS_BMBT_REC_ADDR(mp
, ablock
, 1);
754 kp
->br_startoff
= cpu_to_be64(xfs_bmbt_disk_get_startoff(arp
));
755 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, xfs_bmbt_get_maxrecs(cur
,
756 be16_to_cpu(block
->bb_level
)));
757 *pp
= cpu_to_be64(args
.fsbno
);
760 * Do all this logging at the end so that
761 * the root is at the right level.
763 xfs_btree_log_block(cur
, abp
, XFS_BB_ALL_BITS
);
764 xfs_btree_log_recs(cur
, abp
, 1, be16_to_cpu(ablock
->bb_numrecs
));
765 ASSERT(*curp
== NULL
);
767 *logflagsp
= XFS_ILOG_CORE
| xfs_ilog_fbroot(whichfork
);
771 xfs_trans_mod_dquot_byino(tp
, ip
, XFS_TRANS_DQ_BCOUNT
, -1L);
773 xfs_iroot_realloc(ip
, -1, whichfork
);
774 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_EXTENTS
);
775 ASSERT(ifp
->if_broot
== NULL
);
776 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
782 * Convert a local file to an extents file.
783 * This code is out of bounds for data forks of regular files,
784 * since the file data needs to get logged so things will stay consistent.
785 * (The bmap-level manipulations are ok, though).
788 xfs_bmap_local_to_extents_empty(
789 struct xfs_inode
*ip
,
792 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
794 ASSERT(whichfork
!= XFS_COW_FORK
);
795 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
796 ASSERT(ifp
->if_bytes
== 0);
797 ASSERT(XFS_IFORK_NEXTENTS(ip
, whichfork
) == 0);
799 xfs_bmap_forkoff_reset(ip
, whichfork
);
800 ifp
->if_flags
&= ~XFS_IFINLINE
;
801 ifp
->if_flags
|= XFS_IFEXTENTS
;
802 ifp
->if_u1
.if_root
= NULL
;
804 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_EXTENTS
);
808 STATIC
int /* error */
809 xfs_bmap_local_to_extents(
810 xfs_trans_t
*tp
, /* transaction pointer */
811 xfs_inode_t
*ip
, /* incore inode pointer */
812 xfs_extlen_t total
, /* total blocks needed by transaction */
813 int *logflagsp
, /* inode logging flags */
815 void (*init_fn
)(struct xfs_trans
*tp
,
817 struct xfs_inode
*ip
,
818 struct xfs_ifork
*ifp
))
821 int flags
; /* logging flags returned */
822 struct xfs_ifork
*ifp
; /* inode fork pointer */
823 xfs_alloc_arg_t args
; /* allocation arguments */
824 xfs_buf_t
*bp
; /* buffer for extent block */
825 struct xfs_bmbt_irec rec
;
826 struct xfs_iext_cursor icur
;
829 * We don't want to deal with the case of keeping inode data inline yet.
830 * So sending the data fork of a regular inode is invalid.
832 ASSERT(!(S_ISREG(VFS_I(ip
)->i_mode
) && whichfork
== XFS_DATA_FORK
));
833 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
834 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
836 if (!ifp
->if_bytes
) {
837 xfs_bmap_local_to_extents_empty(ip
, whichfork
);
838 flags
= XFS_ILOG_CORE
;
844 ASSERT((ifp
->if_flags
& (XFS_IFINLINE
|XFS_IFEXTENTS
)) == XFS_IFINLINE
);
845 memset(&args
, 0, sizeof(args
));
847 args
.mp
= ip
->i_mount
;
848 xfs_rmap_ino_owner(&args
.oinfo
, ip
->i_ino
, whichfork
, 0);
850 * Allocate a block. We know we need only one, since the
851 * file currently fits in an inode.
853 if (tp
->t_firstblock
== NULLFSBLOCK
) {
854 args
.fsbno
= XFS_INO_TO_FSB(args
.mp
, ip
->i_ino
);
855 args
.type
= XFS_ALLOCTYPE_START_BNO
;
857 args
.fsbno
= tp
->t_firstblock
;
858 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
861 args
.minlen
= args
.maxlen
= args
.prod
= 1;
862 error
= xfs_alloc_vextent(&args
);
866 /* Can't fail, the space was reserved. */
867 ASSERT(args
.fsbno
!= NULLFSBLOCK
);
868 ASSERT(args
.len
== 1);
869 tp
->t_firstblock
= args
.fsbno
;
870 bp
= xfs_btree_get_bufl(args
.mp
, tp
, args
.fsbno
, 0);
873 * Initialize the block, copy the data and log the remote buffer.
875 * The callout is responsible for logging because the remote format
876 * might differ from the local format and thus we don't know how much to
877 * log here. Note that init_fn must also set the buffer log item type
880 init_fn(tp
, bp
, ip
, ifp
);
882 /* account for the change in fork size */
883 xfs_idata_realloc(ip
, -ifp
->if_bytes
, whichfork
);
884 xfs_bmap_local_to_extents_empty(ip
, whichfork
);
885 flags
|= XFS_ILOG_CORE
;
887 ifp
->if_u1
.if_root
= NULL
;
891 rec
.br_startblock
= args
.fsbno
;
892 rec
.br_blockcount
= 1;
893 rec
.br_state
= XFS_EXT_NORM
;
894 xfs_iext_first(ifp
, &icur
);
895 xfs_iext_insert(ip
, &icur
, &rec
, 0);
897 XFS_IFORK_NEXT_SET(ip
, whichfork
, 1);
898 ip
->i_d
.di_nblocks
= 1;
899 xfs_trans_mod_dquot_byino(tp
, ip
,
900 XFS_TRANS_DQ_BCOUNT
, 1L);
901 flags
|= xfs_ilog_fext(whichfork
);
909 * Called from xfs_bmap_add_attrfork to handle btree format files.
911 STATIC
int /* error */
912 xfs_bmap_add_attrfork_btree(
913 xfs_trans_t
*tp
, /* transaction pointer */
914 xfs_inode_t
*ip
, /* incore inode pointer */
915 int *flags
) /* inode logging flags */
917 xfs_btree_cur_t
*cur
; /* btree cursor */
918 int error
; /* error return value */
919 xfs_mount_t
*mp
; /* file system mount struct */
920 int stat
; /* newroot status */
923 if (ip
->i_df
.if_broot_bytes
<= XFS_IFORK_DSIZE(ip
))
924 *flags
|= XFS_ILOG_DBROOT
;
926 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, XFS_DATA_FORK
);
927 error
= xfs_bmbt_lookup_first(cur
, &stat
);
930 /* must be at least one entry */
931 XFS_WANT_CORRUPTED_GOTO(mp
, stat
== 1, error0
);
932 if ((error
= xfs_btree_new_iroot(cur
, flags
, &stat
)))
935 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
938 cur
->bc_private
.b
.allocated
= 0;
939 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
943 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
948 * Called from xfs_bmap_add_attrfork to handle extents format files.
950 STATIC
int /* error */
951 xfs_bmap_add_attrfork_extents(
952 struct xfs_trans
*tp
, /* transaction pointer */
953 struct xfs_inode
*ip
, /* incore inode pointer */
954 int *flags
) /* inode logging flags */
956 xfs_btree_cur_t
*cur
; /* bmap btree cursor */
957 int error
; /* error return value */
959 if (ip
->i_d
.di_nextents
* sizeof(xfs_bmbt_rec_t
) <= XFS_IFORK_DSIZE(ip
))
962 error
= xfs_bmap_extents_to_btree(tp
, ip
, &cur
, 0, flags
,
965 cur
->bc_private
.b
.allocated
= 0;
966 xfs_btree_del_cursor(cur
, error
);
972 * Called from xfs_bmap_add_attrfork to handle local format files. Each
973 * different data fork content type needs a different callout to do the
974 * conversion. Some are basic and only require special block initialisation
975 * callouts for the data formating, others (directories) are so specialised they
976 * handle everything themselves.
978 * XXX (dgc): investigate whether directory conversion can use the generic
979 * formatting callout. It should be possible - it's just a very complex
982 STATIC
int /* error */
983 xfs_bmap_add_attrfork_local(
984 struct xfs_trans
*tp
, /* transaction pointer */
985 struct xfs_inode
*ip
, /* incore inode pointer */
986 int *flags
) /* inode logging flags */
988 struct xfs_da_args dargs
; /* args for dir/attr code */
990 if (ip
->i_df
.if_bytes
<= XFS_IFORK_DSIZE(ip
))
993 if (S_ISDIR(VFS_I(ip
)->i_mode
)) {
994 memset(&dargs
, 0, sizeof(dargs
));
995 dargs
.geo
= ip
->i_mount
->m_dir_geo
;
997 dargs
.total
= dargs
.geo
->fsbcount
;
998 dargs
.whichfork
= XFS_DATA_FORK
;
1000 return xfs_dir2_sf_to_block(&dargs
);
1003 if (S_ISLNK(VFS_I(ip
)->i_mode
))
1004 return xfs_bmap_local_to_extents(tp
, ip
, 1, flags
,
1006 xfs_symlink_local_to_remote
);
1008 /* should only be called for types that support local format data */
1010 return -EFSCORRUPTED
;
1014 * Convert inode from non-attributed to attributed.
1015 * Must not be in a transaction, ip must not be locked.
1017 int /* error code */
1018 xfs_bmap_add_attrfork(
1019 xfs_inode_t
*ip
, /* incore inode pointer */
1020 int size
, /* space new attribute needs */
1021 int rsvd
) /* xact may use reserved blks */
1023 xfs_mount_t
*mp
; /* mount structure */
1024 xfs_trans_t
*tp
; /* transaction pointer */
1025 int blks
; /* space reservation */
1026 int version
= 1; /* superblock attr version */
1027 int logflags
; /* logging flags */
1028 int error
; /* error return value */
1030 ASSERT(XFS_IFORK_Q(ip
) == 0);
1033 ASSERT(!XFS_NOT_DQATTACHED(mp
, ip
));
1035 blks
= XFS_ADDAFORK_SPACE_RES(mp
);
1037 error
= xfs_trans_alloc(mp
, &M_RES(mp
)->tr_addafork
, blks
, 0,
1038 rsvd
? XFS_TRANS_RESERVE
: 0, &tp
);
1042 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
1043 error
= xfs_trans_reserve_quota_nblks(tp
, ip
, blks
, 0, rsvd
?
1044 XFS_QMOPT_RES_REGBLKS
| XFS_QMOPT_FORCE_RES
:
1045 XFS_QMOPT_RES_REGBLKS
);
1048 if (XFS_IFORK_Q(ip
))
1050 if (ip
->i_d
.di_anextents
!= 0) {
1051 error
= -EFSCORRUPTED
;
1054 if (ip
->i_d
.di_aformat
!= XFS_DINODE_FMT_EXTENTS
) {
1056 * For inodes coming from pre-6.2 filesystems.
1058 ASSERT(ip
->i_d
.di_aformat
== 0);
1059 ip
->i_d
.di_aformat
= XFS_DINODE_FMT_EXTENTS
;
1062 xfs_trans_ijoin(tp
, ip
, 0);
1063 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
1065 switch (ip
->i_d
.di_format
) {
1066 case XFS_DINODE_FMT_DEV
:
1067 ip
->i_d
.di_forkoff
= roundup(sizeof(xfs_dev_t
), 8) >> 3;
1069 case XFS_DINODE_FMT_LOCAL
:
1070 case XFS_DINODE_FMT_EXTENTS
:
1071 case XFS_DINODE_FMT_BTREE
:
1072 ip
->i_d
.di_forkoff
= xfs_attr_shortform_bytesfit(ip
, size
);
1073 if (!ip
->i_d
.di_forkoff
)
1074 ip
->i_d
.di_forkoff
= xfs_default_attroffset(ip
) >> 3;
1075 else if (mp
->m_flags
& XFS_MOUNT_ATTR2
)
1084 ASSERT(ip
->i_afp
== NULL
);
1085 ip
->i_afp
= kmem_zone_zalloc(xfs_ifork_zone
, KM_SLEEP
);
1086 ip
->i_afp
->if_flags
= XFS_IFEXTENTS
;
1088 switch (ip
->i_d
.di_format
) {
1089 case XFS_DINODE_FMT_LOCAL
:
1090 error
= xfs_bmap_add_attrfork_local(tp
, ip
, &logflags
);
1092 case XFS_DINODE_FMT_EXTENTS
:
1093 error
= xfs_bmap_add_attrfork_extents(tp
, ip
, &logflags
);
1095 case XFS_DINODE_FMT_BTREE
:
1096 error
= xfs_bmap_add_attrfork_btree(tp
, ip
, &logflags
);
1103 xfs_trans_log_inode(tp
, ip
, logflags
);
1106 if (!xfs_sb_version_hasattr(&mp
->m_sb
) ||
1107 (!xfs_sb_version_hasattr2(&mp
->m_sb
) && version
== 2)) {
1108 bool log_sb
= false;
1110 spin_lock(&mp
->m_sb_lock
);
1111 if (!xfs_sb_version_hasattr(&mp
->m_sb
)) {
1112 xfs_sb_version_addattr(&mp
->m_sb
);
1115 if (!xfs_sb_version_hasattr2(&mp
->m_sb
) && version
== 2) {
1116 xfs_sb_version_addattr2(&mp
->m_sb
);
1119 spin_unlock(&mp
->m_sb_lock
);
1124 error
= xfs_trans_commit(tp
);
1125 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
1129 xfs_trans_cancel(tp
);
1130 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
1135 * Internal and external extent tree search functions.
1139 * Read in extents from a btree-format inode.
1143 struct xfs_trans
*tp
,
1144 struct xfs_inode
*ip
,
1147 struct xfs_mount
*mp
= ip
->i_mount
;
1148 int state
= xfs_bmap_fork_to_state(whichfork
);
1149 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1150 xfs_extnum_t nextents
= XFS_IFORK_NEXTENTS(ip
, whichfork
);
1151 struct xfs_btree_block
*block
= ifp
->if_broot
;
1152 struct xfs_iext_cursor icur
;
1153 struct xfs_bmbt_irec
new;
1161 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
1163 if (unlikely(XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)) {
1164 XFS_ERROR_REPORT(__func__
, XFS_ERRLEVEL_LOW
, mp
);
1165 return -EFSCORRUPTED
;
1169 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
1171 level
= be16_to_cpu(block
->bb_level
);
1173 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, 1, ifp
->if_broot_bytes
);
1174 bno
= be64_to_cpu(*pp
);
1177 * Go down the tree until leaf level is reached, following the first
1178 * pointer (leftmost) at each level.
1180 while (level
-- > 0) {
1181 error
= xfs_btree_read_bufl(mp
, tp
, bno
, 0, &bp
,
1182 XFS_BMAP_BTREE_REF
, &xfs_bmbt_buf_ops
);
1185 block
= XFS_BUF_TO_BLOCK(bp
);
1188 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, mp
->m_bmap_dmxr
[1]);
1189 bno
= be64_to_cpu(*pp
);
1190 XFS_WANT_CORRUPTED_GOTO(mp
,
1191 xfs_verify_fsbno(mp
, bno
), out_brelse
);
1192 xfs_trans_brelse(tp
, bp
);
1196 * Here with bp and block set to the leftmost leaf node in the tree.
1199 xfs_iext_first(ifp
, &icur
);
1202 * Loop over all leaf nodes. Copy information to the extent records.
1205 xfs_bmbt_rec_t
*frp
;
1206 xfs_fsblock_t nextbno
;
1207 xfs_extnum_t num_recs
;
1209 num_recs
= xfs_btree_get_numrecs(block
);
1210 if (unlikely(i
+ num_recs
> nextents
)) {
1211 xfs_warn(ip
->i_mount
,
1212 "corrupt dinode %Lu, (btree extents).",
1213 (unsigned long long) ip
->i_ino
);
1214 xfs_inode_verifier_error(ip
, -EFSCORRUPTED
,
1215 __func__
, block
, sizeof(*block
),
1217 error
= -EFSCORRUPTED
;
1221 * Read-ahead the next leaf block, if any.
1223 nextbno
= be64_to_cpu(block
->bb_u
.l
.bb_rightsib
);
1224 if (nextbno
!= NULLFSBLOCK
)
1225 xfs_btree_reada_bufl(mp
, nextbno
, 1,
1228 * Copy records into the extent records.
1230 frp
= XFS_BMBT_REC_ADDR(mp
, block
, 1);
1231 for (j
= 0; j
< num_recs
; j
++, frp
++, i
++) {
1234 xfs_bmbt_disk_get_all(frp
, &new);
1235 fa
= xfs_bmap_validate_extent(ip
, whichfork
, &new);
1237 error
= -EFSCORRUPTED
;
1238 xfs_inode_verifier_error(ip
, error
,
1239 "xfs_iread_extents(2)",
1240 frp
, sizeof(*frp
), fa
);
1243 xfs_iext_insert(ip
, &icur
, &new, state
);
1244 trace_xfs_read_extent(ip
, &icur
, state
, _THIS_IP_
);
1245 xfs_iext_next(ifp
, &icur
);
1247 xfs_trans_brelse(tp
, bp
);
1250 * If we've reached the end, stop.
1252 if (bno
== NULLFSBLOCK
)
1254 error
= xfs_btree_read_bufl(mp
, tp
, bno
, 0, &bp
,
1255 XFS_BMAP_BTREE_REF
, &xfs_bmbt_buf_ops
);
1258 block
= XFS_BUF_TO_BLOCK(bp
);
1261 if (i
!= XFS_IFORK_NEXTENTS(ip
, whichfork
)) {
1262 error
= -EFSCORRUPTED
;
1265 ASSERT(i
== xfs_iext_count(ifp
));
1267 ifp
->if_flags
|= XFS_IFEXTENTS
;
1271 xfs_trans_brelse(tp
, bp
);
1273 xfs_iext_destroy(ifp
);
1278 * Returns the relative block number of the first unused block(s) in the given
1279 * fork with at least "len" logically contiguous blocks free. This is the
1280 * lowest-address hole if the fork has holes, else the first block past the end
1281 * of fork. Return 0 if the fork is currently local (in-inode).
1284 xfs_bmap_first_unused(
1285 struct xfs_trans
*tp
, /* transaction pointer */
1286 struct xfs_inode
*ip
, /* incore inode */
1287 xfs_extlen_t len
, /* size of hole to find */
1288 xfs_fileoff_t
*first_unused
, /* unused block */
1289 int whichfork
) /* data or attr fork */
1291 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1292 struct xfs_bmbt_irec got
;
1293 struct xfs_iext_cursor icur
;
1294 xfs_fileoff_t lastaddr
= 0;
1295 xfs_fileoff_t lowest
, max
;
1298 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
||
1299 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
||
1300 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
1302 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
) {
1307 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
1308 error
= xfs_iread_extents(tp
, ip
, whichfork
);
1313 lowest
= max
= *first_unused
;
1314 for_each_xfs_iext(ifp
, &icur
, &got
) {
1316 * See if the hole before this extent will work.
1318 if (got
.br_startoff
>= lowest
+ len
&&
1319 got
.br_startoff
- max
>= len
)
1321 lastaddr
= got
.br_startoff
+ got
.br_blockcount
;
1322 max
= XFS_FILEOFF_MAX(lastaddr
, lowest
);
1325 *first_unused
= max
;
1330 * Returns the file-relative block number of the last block - 1 before
1331 * last_block (input value) in the file.
1332 * This is not based on i_size, it is based on the extent records.
1333 * Returns 0 for local files, as they do not have extent records.
1336 xfs_bmap_last_before(
1337 struct xfs_trans
*tp
, /* transaction pointer */
1338 struct xfs_inode
*ip
, /* incore inode */
1339 xfs_fileoff_t
*last_block
, /* last block */
1340 int whichfork
) /* data or attr fork */
1342 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1343 struct xfs_bmbt_irec got
;
1344 struct xfs_iext_cursor icur
;
1347 switch (XFS_IFORK_FORMAT(ip
, whichfork
)) {
1348 case XFS_DINODE_FMT_LOCAL
:
1351 case XFS_DINODE_FMT_BTREE
:
1352 case XFS_DINODE_FMT_EXTENTS
:
1358 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
1359 error
= xfs_iread_extents(tp
, ip
, whichfork
);
1364 if (!xfs_iext_lookup_extent_before(ip
, ifp
, last_block
, &icur
, &got
))
1370 xfs_bmap_last_extent(
1371 struct xfs_trans
*tp
,
1372 struct xfs_inode
*ip
,
1374 struct xfs_bmbt_irec
*rec
,
1377 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1378 struct xfs_iext_cursor icur
;
1381 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
1382 error
= xfs_iread_extents(tp
, ip
, whichfork
);
1387 xfs_iext_last(ifp
, &icur
);
1388 if (!xfs_iext_get_extent(ifp
, &icur
, rec
))
1396 * Check the last inode extent to determine whether this allocation will result
1397 * in blocks being allocated at the end of the file. When we allocate new data
1398 * blocks at the end of the file which do not start at the previous data block,
1399 * we will try to align the new blocks at stripe unit boundaries.
1401 * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be
1402 * at, or past the EOF.
1406 struct xfs_bmalloca
*bma
,
1409 struct xfs_bmbt_irec rec
;
1414 error
= xfs_bmap_last_extent(NULL
, bma
->ip
, whichfork
, &rec
,
1425 * Check if we are allocation or past the last extent, or at least into
1426 * the last delayed allocated extent.
1428 bma
->aeof
= bma
->offset
>= rec
.br_startoff
+ rec
.br_blockcount
||
1429 (bma
->offset
>= rec
.br_startoff
&&
1430 isnullstartblock(rec
.br_startblock
));
1435 * Returns the file-relative block number of the first block past eof in
1436 * the file. This is not based on i_size, it is based on the extent records.
1437 * Returns 0 for local files, as they do not have extent records.
1440 xfs_bmap_last_offset(
1441 struct xfs_inode
*ip
,
1442 xfs_fileoff_t
*last_block
,
1445 struct xfs_bmbt_irec rec
;
1451 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
)
1454 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
&&
1455 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
1458 error
= xfs_bmap_last_extent(NULL
, ip
, whichfork
, &rec
, &is_empty
);
1459 if (error
|| is_empty
)
1462 *last_block
= rec
.br_startoff
+ rec
.br_blockcount
;
1467 * Returns whether the selected fork of the inode has exactly one
1468 * block or not. For the data fork we check this matches di_size,
1469 * implying the file's range is 0..bsize-1.
1471 int /* 1=>1 block, 0=>otherwise */
1473 xfs_inode_t
*ip
, /* incore inode */
1474 int whichfork
) /* data or attr fork */
1476 struct xfs_ifork
*ifp
; /* inode fork pointer */
1477 int rval
; /* return value */
1478 xfs_bmbt_irec_t s
; /* internal version of extent */
1479 struct xfs_iext_cursor icur
;
1482 if (whichfork
== XFS_DATA_FORK
)
1483 return XFS_ISIZE(ip
) == ip
->i_mount
->m_sb
.sb_blocksize
;
1485 if (XFS_IFORK_NEXTENTS(ip
, whichfork
) != 1)
1487 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
1489 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1490 ASSERT(ifp
->if_flags
& XFS_IFEXTENTS
);
1491 xfs_iext_first(ifp
, &icur
);
1492 xfs_iext_get_extent(ifp
, &icur
, &s
);
1493 rval
= s
.br_startoff
== 0 && s
.br_blockcount
== 1;
1494 if (rval
&& whichfork
== XFS_DATA_FORK
)
1495 ASSERT(XFS_ISIZE(ip
) == ip
->i_mount
->m_sb
.sb_blocksize
);
1500 * Extent tree manipulation functions used during allocation.
1504 * Convert a delayed allocation to a real allocation.
1506 STATIC
int /* error */
1507 xfs_bmap_add_extent_delay_real(
1508 struct xfs_bmalloca
*bma
,
1511 struct xfs_bmbt_irec
*new = &bma
->got
;
1512 int error
; /* error return value */
1513 int i
; /* temp state */
1514 struct xfs_ifork
*ifp
; /* inode fork pointer */
1515 xfs_fileoff_t new_endoff
; /* end offset of new entry */
1516 xfs_bmbt_irec_t r
[3]; /* neighbor extent entries */
1517 /* left is 0, right is 1, prev is 2 */
1518 int rval
=0; /* return value (logging flags) */
1519 int state
= xfs_bmap_fork_to_state(whichfork
);
1520 xfs_filblks_t da_new
; /* new count del alloc blocks used */
1521 xfs_filblks_t da_old
; /* old count del alloc blocks used */
1522 xfs_filblks_t temp
=0; /* value for da_new calculations */
1523 int tmp_rval
; /* partial logging flags */
1524 struct xfs_mount
*mp
;
1525 xfs_extnum_t
*nextents
;
1526 struct xfs_bmbt_irec old
;
1528 mp
= bma
->ip
->i_mount
;
1529 ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
1530 ASSERT(whichfork
!= XFS_ATTR_FORK
);
1531 nextents
= (whichfork
== XFS_COW_FORK
? &bma
->ip
->i_cnextents
:
1532 &bma
->ip
->i_d
.di_nextents
);
1534 ASSERT(!isnullstartblock(new->br_startblock
));
1536 (bma
->cur
->bc_private
.b
.flags
& XFS_BTCUR_BPRV_WASDEL
));
1538 XFS_STATS_INC(mp
, xs_add_exlist
);
1545 * Set up a bunch of variables to make the tests simpler.
1547 xfs_iext_get_extent(ifp
, &bma
->icur
, &PREV
);
1548 new_endoff
= new->br_startoff
+ new->br_blockcount
;
1549 ASSERT(isnullstartblock(PREV
.br_startblock
));
1550 ASSERT(PREV
.br_startoff
<= new->br_startoff
);
1551 ASSERT(PREV
.br_startoff
+ PREV
.br_blockcount
>= new_endoff
);
1553 da_old
= startblockval(PREV
.br_startblock
);
1557 * Set flags determining what part of the previous delayed allocation
1558 * extent is being replaced by a real allocation.
1560 if (PREV
.br_startoff
== new->br_startoff
)
1561 state
|= BMAP_LEFT_FILLING
;
1562 if (PREV
.br_startoff
+ PREV
.br_blockcount
== new_endoff
)
1563 state
|= BMAP_RIGHT_FILLING
;
1566 * Check and set flags if this segment has a left neighbor.
1567 * Don't set contiguous if the combined extent would be too large.
1569 if (xfs_iext_peek_prev_extent(ifp
, &bma
->icur
, &LEFT
)) {
1570 state
|= BMAP_LEFT_VALID
;
1571 if (isnullstartblock(LEFT
.br_startblock
))
1572 state
|= BMAP_LEFT_DELAY
;
1575 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
1576 LEFT
.br_startoff
+ LEFT
.br_blockcount
== new->br_startoff
&&
1577 LEFT
.br_startblock
+ LEFT
.br_blockcount
== new->br_startblock
&&
1578 LEFT
.br_state
== new->br_state
&&
1579 LEFT
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
1580 state
|= BMAP_LEFT_CONTIG
;
1583 * Check and set flags if this segment has a right neighbor.
1584 * Don't set contiguous if the combined extent would be too large.
1585 * Also check for all-three-contiguous being too large.
1587 if (xfs_iext_peek_next_extent(ifp
, &bma
->icur
, &RIGHT
)) {
1588 state
|= BMAP_RIGHT_VALID
;
1589 if (isnullstartblock(RIGHT
.br_startblock
))
1590 state
|= BMAP_RIGHT_DELAY
;
1593 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
1594 new_endoff
== RIGHT
.br_startoff
&&
1595 new->br_startblock
+ new->br_blockcount
== RIGHT
.br_startblock
&&
1596 new->br_state
== RIGHT
.br_state
&&
1597 new->br_blockcount
+ RIGHT
.br_blockcount
<= MAXEXTLEN
&&
1598 ((state
& (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
1599 BMAP_RIGHT_FILLING
)) !=
1600 (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
1601 BMAP_RIGHT_FILLING
) ||
1602 LEFT
.br_blockcount
+ new->br_blockcount
+ RIGHT
.br_blockcount
1604 state
|= BMAP_RIGHT_CONTIG
;
1608 * Switch out based on the FILLING and CONTIG state bits.
1610 switch (state
& (BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
1611 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
)) {
1612 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
1613 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
1615 * Filling in all of a previously delayed allocation extent.
1616 * The left and right neighbors are both contiguous with new.
1618 LEFT
.br_blockcount
+= PREV
.br_blockcount
+ RIGHT
.br_blockcount
;
1620 xfs_iext_remove(bma
->ip
, &bma
->icur
, state
);
1621 xfs_iext_remove(bma
->ip
, &bma
->icur
, state
);
1622 xfs_iext_prev(ifp
, &bma
->icur
);
1623 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &LEFT
);
1626 if (bma
->cur
== NULL
)
1627 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1629 rval
= XFS_ILOG_CORE
;
1630 error
= xfs_bmbt_lookup_eq(bma
->cur
, &RIGHT
, &i
);
1633 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1634 error
= xfs_btree_delete(bma
->cur
, &i
);
1637 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1638 error
= xfs_btree_decrement(bma
->cur
, 0, &i
);
1641 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1642 error
= xfs_bmbt_update(bma
->cur
, &LEFT
);
1648 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
1650 * Filling in all of a previously delayed allocation extent.
1651 * The left neighbor is contiguous, the right is not.
1654 LEFT
.br_blockcount
+= PREV
.br_blockcount
;
1656 xfs_iext_remove(bma
->ip
, &bma
->icur
, state
);
1657 xfs_iext_prev(ifp
, &bma
->icur
);
1658 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &LEFT
);
1660 if (bma
->cur
== NULL
)
1661 rval
= XFS_ILOG_DEXT
;
1664 error
= xfs_bmbt_lookup_eq(bma
->cur
, &old
, &i
);
1667 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1668 error
= xfs_bmbt_update(bma
->cur
, &LEFT
);
1674 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
1676 * Filling in all of a previously delayed allocation extent.
1677 * The right neighbor is contiguous, the left is not.
1679 PREV
.br_startblock
= new->br_startblock
;
1680 PREV
.br_blockcount
+= RIGHT
.br_blockcount
;
1682 xfs_iext_next(ifp
, &bma
->icur
);
1683 xfs_iext_remove(bma
->ip
, &bma
->icur
, state
);
1684 xfs_iext_prev(ifp
, &bma
->icur
);
1685 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &PREV
);
1687 if (bma
->cur
== NULL
)
1688 rval
= XFS_ILOG_DEXT
;
1691 error
= xfs_bmbt_lookup_eq(bma
->cur
, &RIGHT
, &i
);
1694 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1695 error
= xfs_bmbt_update(bma
->cur
, &PREV
);
1701 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
1703 * Filling in all of a previously delayed allocation extent.
1704 * Neither the left nor right neighbors are contiguous with
1707 PREV
.br_startblock
= new->br_startblock
;
1708 PREV
.br_state
= new->br_state
;
1709 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &PREV
);
1712 if (bma
->cur
== NULL
)
1713 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1715 rval
= XFS_ILOG_CORE
;
1716 error
= xfs_bmbt_lookup_eq(bma
->cur
, new, &i
);
1719 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
1720 error
= xfs_btree_insert(bma
->cur
, &i
);
1723 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1727 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
:
1729 * Filling in the first part of a previous delayed allocation.
1730 * The left neighbor is contiguous.
1733 temp
= PREV
.br_blockcount
- new->br_blockcount
;
1734 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
1735 startblockval(PREV
.br_startblock
));
1737 LEFT
.br_blockcount
+= new->br_blockcount
;
1739 PREV
.br_blockcount
= temp
;
1740 PREV
.br_startoff
+= new->br_blockcount
;
1741 PREV
.br_startblock
= nullstartblock(da_new
);
1743 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &PREV
);
1744 xfs_iext_prev(ifp
, &bma
->icur
);
1745 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &LEFT
);
1747 if (bma
->cur
== NULL
)
1748 rval
= XFS_ILOG_DEXT
;
1751 error
= xfs_bmbt_lookup_eq(bma
->cur
, &old
, &i
);
1754 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1755 error
= xfs_bmbt_update(bma
->cur
, &LEFT
);
1761 case BMAP_LEFT_FILLING
:
1763 * Filling in the first part of a previous delayed allocation.
1764 * The left neighbor is not contiguous.
1766 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, new);
1768 if (bma
->cur
== NULL
)
1769 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1771 rval
= XFS_ILOG_CORE
;
1772 error
= xfs_bmbt_lookup_eq(bma
->cur
, new, &i
);
1775 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
1776 error
= xfs_btree_insert(bma
->cur
, &i
);
1779 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1782 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
1783 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
1784 &bma
->cur
, 1, &tmp_rval
, whichfork
);
1790 temp
= PREV
.br_blockcount
- new->br_blockcount
;
1791 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
1792 startblockval(PREV
.br_startblock
) -
1793 (bma
->cur
? bma
->cur
->bc_private
.b
.allocated
: 0));
1795 PREV
.br_startoff
= new_endoff
;
1796 PREV
.br_blockcount
= temp
;
1797 PREV
.br_startblock
= nullstartblock(da_new
);
1798 xfs_iext_next(ifp
, &bma
->icur
);
1799 xfs_iext_insert(bma
->ip
, &bma
->icur
, &PREV
, state
);
1800 xfs_iext_prev(ifp
, &bma
->icur
);
1803 case BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
1805 * Filling in the last part of a previous delayed allocation.
1806 * The right neighbor is contiguous with the new allocation.
1809 RIGHT
.br_startoff
= new->br_startoff
;
1810 RIGHT
.br_startblock
= new->br_startblock
;
1811 RIGHT
.br_blockcount
+= new->br_blockcount
;
1813 if (bma
->cur
== NULL
)
1814 rval
= XFS_ILOG_DEXT
;
1817 error
= xfs_bmbt_lookup_eq(bma
->cur
, &old
, &i
);
1820 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1821 error
= xfs_bmbt_update(bma
->cur
, &RIGHT
);
1826 temp
= PREV
.br_blockcount
- new->br_blockcount
;
1827 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
1828 startblockval(PREV
.br_startblock
));
1830 PREV
.br_blockcount
= temp
;
1831 PREV
.br_startblock
= nullstartblock(da_new
);
1833 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &PREV
);
1834 xfs_iext_next(ifp
, &bma
->icur
);
1835 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &RIGHT
);
1838 case BMAP_RIGHT_FILLING
:
1840 * Filling in the last part of a previous delayed allocation.
1841 * The right neighbor is not contiguous.
1843 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, new);
1845 if (bma
->cur
== NULL
)
1846 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1848 rval
= XFS_ILOG_CORE
;
1849 error
= xfs_bmbt_lookup_eq(bma
->cur
, new, &i
);
1852 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
1853 error
= xfs_btree_insert(bma
->cur
, &i
);
1856 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1859 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
1860 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
1861 &bma
->cur
, 1, &tmp_rval
, whichfork
);
1867 temp
= PREV
.br_blockcount
- new->br_blockcount
;
1868 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
1869 startblockval(PREV
.br_startblock
) -
1870 (bma
->cur
? bma
->cur
->bc_private
.b
.allocated
: 0));
1872 PREV
.br_startblock
= nullstartblock(da_new
);
1873 PREV
.br_blockcount
= temp
;
1874 xfs_iext_insert(bma
->ip
, &bma
->icur
, &PREV
, state
);
1875 xfs_iext_next(ifp
, &bma
->icur
);
1880 * Filling in the middle part of a previous delayed allocation.
1881 * Contiguity is impossible here.
1882 * This case is avoided almost all the time.
1884 * We start with a delayed allocation:
1886 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
1889 * and we are allocating:
1890 * +rrrrrrrrrrrrrrrrr+
1893 * and we set it up for insertion as:
1894 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
1896 * PREV @ idx LEFT RIGHT
1897 * inserted at idx + 1
1901 /* LEFT is the new middle */
1904 /* RIGHT is the new right */
1905 RIGHT
.br_state
= PREV
.br_state
;
1906 RIGHT
.br_startoff
= new_endoff
;
1907 RIGHT
.br_blockcount
=
1908 PREV
.br_startoff
+ PREV
.br_blockcount
- new_endoff
;
1909 RIGHT
.br_startblock
=
1910 nullstartblock(xfs_bmap_worst_indlen(bma
->ip
,
1911 RIGHT
.br_blockcount
));
1914 PREV
.br_blockcount
= new->br_startoff
- PREV
.br_startoff
;
1915 PREV
.br_startblock
=
1916 nullstartblock(xfs_bmap_worst_indlen(bma
->ip
,
1917 PREV
.br_blockcount
));
1918 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &PREV
);
1920 xfs_iext_next(ifp
, &bma
->icur
);
1921 xfs_iext_insert(bma
->ip
, &bma
->icur
, &RIGHT
, state
);
1922 xfs_iext_insert(bma
->ip
, &bma
->icur
, &LEFT
, state
);
1925 if (bma
->cur
== NULL
)
1926 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1928 rval
= XFS_ILOG_CORE
;
1929 error
= xfs_bmbt_lookup_eq(bma
->cur
, new, &i
);
1932 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
1933 error
= xfs_btree_insert(bma
->cur
, &i
);
1936 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1939 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
1940 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
1941 &bma
->cur
, 1, &tmp_rval
, whichfork
);
1947 da_new
= startblockval(PREV
.br_startblock
) +
1948 startblockval(RIGHT
.br_startblock
);
1951 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
1952 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
1953 case BMAP_LEFT_FILLING
| BMAP_RIGHT_CONTIG
:
1954 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
1955 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
1956 case BMAP_LEFT_CONTIG
:
1957 case BMAP_RIGHT_CONTIG
:
1959 * These cases are all impossible.
1964 /* add reverse mapping unless caller opted out */
1965 if (!(bma
->flags
& XFS_BMAPI_NORMAP
)) {
1966 error
= xfs_rmap_map_extent(bma
->tp
, bma
->ip
, whichfork
, new);
1971 /* convert to a btree if necessary */
1972 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
1973 int tmp_logflags
; /* partial log flag return val */
1975 ASSERT(bma
->cur
== NULL
);
1976 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
1977 &bma
->cur
, da_old
> 0, &tmp_logflags
,
1979 bma
->logflags
|= tmp_logflags
;
1985 da_new
+= bma
->cur
->bc_private
.b
.allocated
;
1986 bma
->cur
->bc_private
.b
.allocated
= 0;
1989 /* adjust for changes in reserved delayed indirect blocks */
1990 if (da_new
!= da_old
) {
1991 ASSERT(state
== 0 || da_new
< da_old
);
1992 error
= xfs_mod_fdblocks(mp
, (int64_t)(da_old
- da_new
),
1996 xfs_bmap_check_leaf_extents(bma
->cur
, bma
->ip
, whichfork
);
1998 if (whichfork
!= XFS_COW_FORK
)
1999 bma
->logflags
|= rval
;
2007 * Convert an unwritten allocation to a real allocation or vice versa.
2009 STATIC
int /* error */
2010 xfs_bmap_add_extent_unwritten_real(
2011 struct xfs_trans
*tp
,
2012 xfs_inode_t
*ip
, /* incore inode pointer */
2014 struct xfs_iext_cursor
*icur
,
2015 xfs_btree_cur_t
**curp
, /* if *curp is null, not a btree */
2016 xfs_bmbt_irec_t
*new, /* new data to add to file extents */
2017 int *logflagsp
) /* inode logging flags */
2019 xfs_btree_cur_t
*cur
; /* btree cursor */
2020 int error
; /* error return value */
2021 int i
; /* temp state */
2022 struct xfs_ifork
*ifp
; /* inode fork pointer */
2023 xfs_fileoff_t new_endoff
; /* end offset of new entry */
2024 xfs_bmbt_irec_t r
[3]; /* neighbor extent entries */
2025 /* left is 0, right is 1, prev is 2 */
2026 int rval
=0; /* return value (logging flags) */
2027 int state
= xfs_bmap_fork_to_state(whichfork
);
2028 struct xfs_mount
*mp
= ip
->i_mount
;
2029 struct xfs_bmbt_irec old
;
2034 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
2036 ASSERT(!isnullstartblock(new->br_startblock
));
2038 XFS_STATS_INC(mp
, xs_add_exlist
);
2045 * Set up a bunch of variables to make the tests simpler.
2048 xfs_iext_get_extent(ifp
, icur
, &PREV
);
2049 ASSERT(new->br_state
!= PREV
.br_state
);
2050 new_endoff
= new->br_startoff
+ new->br_blockcount
;
2051 ASSERT(PREV
.br_startoff
<= new->br_startoff
);
2052 ASSERT(PREV
.br_startoff
+ PREV
.br_blockcount
>= new_endoff
);
2055 * Set flags determining what part of the previous oldext allocation
2056 * extent is being replaced by a newext allocation.
2058 if (PREV
.br_startoff
== new->br_startoff
)
2059 state
|= BMAP_LEFT_FILLING
;
2060 if (PREV
.br_startoff
+ PREV
.br_blockcount
== new_endoff
)
2061 state
|= BMAP_RIGHT_FILLING
;
2064 * Check and set flags if this segment has a left neighbor.
2065 * Don't set contiguous if the combined extent would be too large.
2067 if (xfs_iext_peek_prev_extent(ifp
, icur
, &LEFT
)) {
2068 state
|= BMAP_LEFT_VALID
;
2069 if (isnullstartblock(LEFT
.br_startblock
))
2070 state
|= BMAP_LEFT_DELAY
;
2073 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
2074 LEFT
.br_startoff
+ LEFT
.br_blockcount
== new->br_startoff
&&
2075 LEFT
.br_startblock
+ LEFT
.br_blockcount
== new->br_startblock
&&
2076 LEFT
.br_state
== new->br_state
&&
2077 LEFT
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
2078 state
|= BMAP_LEFT_CONTIG
;
2081 * Check and set flags if this segment has a right neighbor.
2082 * Don't set contiguous if the combined extent would be too large.
2083 * Also check for all-three-contiguous being too large.
2085 if (xfs_iext_peek_next_extent(ifp
, icur
, &RIGHT
)) {
2086 state
|= BMAP_RIGHT_VALID
;
2087 if (isnullstartblock(RIGHT
.br_startblock
))
2088 state
|= BMAP_RIGHT_DELAY
;
2091 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
2092 new_endoff
== RIGHT
.br_startoff
&&
2093 new->br_startblock
+ new->br_blockcount
== RIGHT
.br_startblock
&&
2094 new->br_state
== RIGHT
.br_state
&&
2095 new->br_blockcount
+ RIGHT
.br_blockcount
<= MAXEXTLEN
&&
2096 ((state
& (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
2097 BMAP_RIGHT_FILLING
)) !=
2098 (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
2099 BMAP_RIGHT_FILLING
) ||
2100 LEFT
.br_blockcount
+ new->br_blockcount
+ RIGHT
.br_blockcount
2102 state
|= BMAP_RIGHT_CONTIG
;
2105 * Switch out based on the FILLING and CONTIG state bits.
2107 switch (state
& (BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
2108 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
)) {
2109 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
2110 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2112 * Setting all of a previous oldext extent to newext.
2113 * The left and right neighbors are both contiguous with new.
2115 LEFT
.br_blockcount
+= PREV
.br_blockcount
+ RIGHT
.br_blockcount
;
2117 xfs_iext_remove(ip
, icur
, state
);
2118 xfs_iext_remove(ip
, icur
, state
);
2119 xfs_iext_prev(ifp
, icur
);
2120 xfs_iext_update_extent(ip
, state
, icur
, &LEFT
);
2121 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2122 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 2);
2124 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2126 rval
= XFS_ILOG_CORE
;
2127 error
= xfs_bmbt_lookup_eq(cur
, &RIGHT
, &i
);
2130 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2131 if ((error
= xfs_btree_delete(cur
, &i
)))
2133 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2134 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2136 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2137 if ((error
= xfs_btree_delete(cur
, &i
)))
2139 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2140 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2142 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2143 error
= xfs_bmbt_update(cur
, &LEFT
);
2149 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
2151 * Setting all of a previous oldext extent to newext.
2152 * The left neighbor is contiguous, the right is not.
2154 LEFT
.br_blockcount
+= PREV
.br_blockcount
;
2156 xfs_iext_remove(ip
, icur
, state
);
2157 xfs_iext_prev(ifp
, icur
);
2158 xfs_iext_update_extent(ip
, state
, icur
, &LEFT
);
2159 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2160 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
2162 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2164 rval
= XFS_ILOG_CORE
;
2165 error
= xfs_bmbt_lookup_eq(cur
, &PREV
, &i
);
2168 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2169 if ((error
= xfs_btree_delete(cur
, &i
)))
2171 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2172 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2174 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2175 error
= xfs_bmbt_update(cur
, &LEFT
);
2181 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2183 * Setting all of a previous oldext extent to newext.
2184 * The right neighbor is contiguous, the left is not.
2186 PREV
.br_blockcount
+= RIGHT
.br_blockcount
;
2187 PREV
.br_state
= new->br_state
;
2189 xfs_iext_next(ifp
, icur
);
2190 xfs_iext_remove(ip
, icur
, state
);
2191 xfs_iext_prev(ifp
, icur
);
2192 xfs_iext_update_extent(ip
, state
, icur
, &PREV
);
2194 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2195 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
2197 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2199 rval
= XFS_ILOG_CORE
;
2200 error
= xfs_bmbt_lookup_eq(cur
, &RIGHT
, &i
);
2203 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2204 if ((error
= xfs_btree_delete(cur
, &i
)))
2206 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2207 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2209 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2210 error
= xfs_bmbt_update(cur
, &PREV
);
2216 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
2218 * Setting all of a previous oldext extent to newext.
2219 * Neither the left nor right neighbors are contiguous with
2222 PREV
.br_state
= new->br_state
;
2223 xfs_iext_update_extent(ip
, state
, icur
, &PREV
);
2226 rval
= XFS_ILOG_DEXT
;
2229 error
= xfs_bmbt_lookup_eq(cur
, new, &i
);
2232 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2233 error
= xfs_bmbt_update(cur
, &PREV
);
2239 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
:
2241 * Setting the first part of a previous oldext extent to newext.
2242 * The left neighbor is contiguous.
2244 LEFT
.br_blockcount
+= new->br_blockcount
;
2247 PREV
.br_startoff
+= new->br_blockcount
;
2248 PREV
.br_startblock
+= new->br_blockcount
;
2249 PREV
.br_blockcount
-= new->br_blockcount
;
2251 xfs_iext_update_extent(ip
, state
, icur
, &PREV
);
2252 xfs_iext_prev(ifp
, icur
);
2253 xfs_iext_update_extent(ip
, state
, icur
, &LEFT
);
2256 rval
= XFS_ILOG_DEXT
;
2259 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2262 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2263 error
= xfs_bmbt_update(cur
, &PREV
);
2266 error
= xfs_btree_decrement(cur
, 0, &i
);
2269 error
= xfs_bmbt_update(cur
, &LEFT
);
2275 case BMAP_LEFT_FILLING
:
2277 * Setting the first part of a previous oldext extent to newext.
2278 * The left neighbor is not contiguous.
2281 PREV
.br_startoff
+= new->br_blockcount
;
2282 PREV
.br_startblock
+= new->br_blockcount
;
2283 PREV
.br_blockcount
-= new->br_blockcount
;
2285 xfs_iext_update_extent(ip
, state
, icur
, &PREV
);
2286 xfs_iext_insert(ip
, icur
, new, state
);
2287 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2288 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
2290 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2292 rval
= XFS_ILOG_CORE
;
2293 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2296 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2297 error
= xfs_bmbt_update(cur
, &PREV
);
2300 cur
->bc_rec
.b
= *new;
2301 if ((error
= xfs_btree_insert(cur
, &i
)))
2303 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2307 case BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2309 * Setting the last part of a previous oldext extent to newext.
2310 * The right neighbor is contiguous with the new allocation.
2313 PREV
.br_blockcount
-= new->br_blockcount
;
2315 RIGHT
.br_startoff
= new->br_startoff
;
2316 RIGHT
.br_startblock
= new->br_startblock
;
2317 RIGHT
.br_blockcount
+= new->br_blockcount
;
2319 xfs_iext_update_extent(ip
, state
, icur
, &PREV
);
2320 xfs_iext_next(ifp
, icur
);
2321 xfs_iext_update_extent(ip
, state
, icur
, &RIGHT
);
2324 rval
= XFS_ILOG_DEXT
;
2327 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2330 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2331 error
= xfs_bmbt_update(cur
, &PREV
);
2334 error
= xfs_btree_increment(cur
, 0, &i
);
2337 error
= xfs_bmbt_update(cur
, &RIGHT
);
2343 case BMAP_RIGHT_FILLING
:
2345 * Setting the last part of a previous oldext extent to newext.
2346 * The right neighbor is not contiguous.
2349 PREV
.br_blockcount
-= new->br_blockcount
;
2351 xfs_iext_update_extent(ip
, state
, icur
, &PREV
);
2352 xfs_iext_next(ifp
, icur
);
2353 xfs_iext_insert(ip
, icur
, new, state
);
2355 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2356 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
2358 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2360 rval
= XFS_ILOG_CORE
;
2361 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2364 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2365 error
= xfs_bmbt_update(cur
, &PREV
);
2368 error
= xfs_bmbt_lookup_eq(cur
, new, &i
);
2371 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
2372 if ((error
= xfs_btree_insert(cur
, &i
)))
2374 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2380 * Setting the middle part of a previous oldext extent to
2381 * newext. Contiguity is impossible here.
2382 * One extent becomes three extents.
2385 PREV
.br_blockcount
= new->br_startoff
- PREV
.br_startoff
;
2388 r
[1].br_startoff
= new_endoff
;
2389 r
[1].br_blockcount
=
2390 old
.br_startoff
+ old
.br_blockcount
- new_endoff
;
2391 r
[1].br_startblock
= new->br_startblock
+ new->br_blockcount
;
2392 r
[1].br_state
= PREV
.br_state
;
2394 xfs_iext_update_extent(ip
, state
, icur
, &PREV
);
2395 xfs_iext_next(ifp
, icur
);
2396 xfs_iext_insert(ip
, icur
, &r
[1], state
);
2397 xfs_iext_insert(ip
, icur
, &r
[0], state
);
2399 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2400 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 2);
2402 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2404 rval
= XFS_ILOG_CORE
;
2405 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2408 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2409 /* new right extent - oldext */
2410 error
= xfs_bmbt_update(cur
, &r
[1]);
2413 /* new left extent - oldext */
2414 cur
->bc_rec
.b
= PREV
;
2415 if ((error
= xfs_btree_insert(cur
, &i
)))
2417 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2419 * Reset the cursor to the position of the new extent
2420 * we are about to insert as we can't trust it after
2421 * the previous insert.
2423 error
= xfs_bmbt_lookup_eq(cur
, new, &i
);
2426 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
2427 /* new middle extent - newext */
2428 if ((error
= xfs_btree_insert(cur
, &i
)))
2430 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2434 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2435 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2436 case BMAP_LEFT_FILLING
| BMAP_RIGHT_CONTIG
:
2437 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
2438 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2439 case BMAP_LEFT_CONTIG
:
2440 case BMAP_RIGHT_CONTIG
:
2442 * These cases are all impossible.
2447 /* update reverse mappings */
2448 error
= xfs_rmap_convert_extent(mp
, tp
, ip
, whichfork
, new);
2452 /* convert to a btree if necessary */
2453 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
2454 int tmp_logflags
; /* partial log flag return val */
2456 ASSERT(cur
== NULL
);
2457 error
= xfs_bmap_extents_to_btree(tp
, ip
, &cur
, 0,
2458 &tmp_logflags
, whichfork
);
2459 *logflagsp
|= tmp_logflags
;
2464 /* clear out the allocated field, done with it now in any case. */
2466 cur
->bc_private
.b
.allocated
= 0;
2470 xfs_bmap_check_leaf_extents(*curp
, ip
, whichfork
);
2480 * Convert a hole to a delayed allocation.
2483 xfs_bmap_add_extent_hole_delay(
2484 xfs_inode_t
*ip
, /* incore inode pointer */
2486 struct xfs_iext_cursor
*icur
,
2487 xfs_bmbt_irec_t
*new) /* new data to add to file extents */
2489 struct xfs_ifork
*ifp
; /* inode fork pointer */
2490 xfs_bmbt_irec_t left
; /* left neighbor extent entry */
2491 xfs_filblks_t newlen
=0; /* new indirect size */
2492 xfs_filblks_t oldlen
=0; /* old indirect size */
2493 xfs_bmbt_irec_t right
; /* right neighbor extent entry */
2494 int state
= xfs_bmap_fork_to_state(whichfork
);
2495 xfs_filblks_t temp
; /* temp for indirect calculations */
2497 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
2498 ASSERT(isnullstartblock(new->br_startblock
));
2501 * Check and set flags if this segment has a left neighbor
2503 if (xfs_iext_peek_prev_extent(ifp
, icur
, &left
)) {
2504 state
|= BMAP_LEFT_VALID
;
2505 if (isnullstartblock(left
.br_startblock
))
2506 state
|= BMAP_LEFT_DELAY
;
2510 * Check and set flags if the current (right) segment exists.
2511 * If it doesn't exist, we're converting the hole at end-of-file.
2513 if (xfs_iext_get_extent(ifp
, icur
, &right
)) {
2514 state
|= BMAP_RIGHT_VALID
;
2515 if (isnullstartblock(right
.br_startblock
))
2516 state
|= BMAP_RIGHT_DELAY
;
2520 * Set contiguity flags on the left and right neighbors.
2521 * Don't let extents get too large, even if the pieces are contiguous.
2523 if ((state
& BMAP_LEFT_VALID
) && (state
& BMAP_LEFT_DELAY
) &&
2524 left
.br_startoff
+ left
.br_blockcount
== new->br_startoff
&&
2525 left
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
2526 state
|= BMAP_LEFT_CONTIG
;
2528 if ((state
& BMAP_RIGHT_VALID
) && (state
& BMAP_RIGHT_DELAY
) &&
2529 new->br_startoff
+ new->br_blockcount
== right
.br_startoff
&&
2530 new->br_blockcount
+ right
.br_blockcount
<= MAXEXTLEN
&&
2531 (!(state
& BMAP_LEFT_CONTIG
) ||
2532 (left
.br_blockcount
+ new->br_blockcount
+
2533 right
.br_blockcount
<= MAXEXTLEN
)))
2534 state
|= BMAP_RIGHT_CONTIG
;
2537 * Switch out based on the contiguity flags.
2539 switch (state
& (BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
)) {
2540 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2542 * New allocation is contiguous with delayed allocations
2543 * on the left and on the right.
2544 * Merge all three into a single extent record.
2546 temp
= left
.br_blockcount
+ new->br_blockcount
+
2547 right
.br_blockcount
;
2549 oldlen
= startblockval(left
.br_startblock
) +
2550 startblockval(new->br_startblock
) +
2551 startblockval(right
.br_startblock
);
2552 newlen
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
2554 left
.br_startblock
= nullstartblock(newlen
);
2555 left
.br_blockcount
= temp
;
2557 xfs_iext_remove(ip
, icur
, state
);
2558 xfs_iext_prev(ifp
, icur
);
2559 xfs_iext_update_extent(ip
, state
, icur
, &left
);
2562 case BMAP_LEFT_CONTIG
:
2564 * New allocation is contiguous with a delayed allocation
2566 * Merge the new allocation with the left neighbor.
2568 temp
= left
.br_blockcount
+ new->br_blockcount
;
2570 oldlen
= startblockval(left
.br_startblock
) +
2571 startblockval(new->br_startblock
);
2572 newlen
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
2574 left
.br_blockcount
= temp
;
2575 left
.br_startblock
= nullstartblock(newlen
);
2577 xfs_iext_prev(ifp
, icur
);
2578 xfs_iext_update_extent(ip
, state
, icur
, &left
);
2581 case BMAP_RIGHT_CONTIG
:
2583 * New allocation is contiguous with a delayed allocation
2585 * Merge the new allocation with the right neighbor.
2587 temp
= new->br_blockcount
+ right
.br_blockcount
;
2588 oldlen
= startblockval(new->br_startblock
) +
2589 startblockval(right
.br_startblock
);
2590 newlen
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
2592 right
.br_startoff
= new->br_startoff
;
2593 right
.br_startblock
= nullstartblock(newlen
);
2594 right
.br_blockcount
= temp
;
2595 xfs_iext_update_extent(ip
, state
, icur
, &right
);
2600 * New allocation is not contiguous with another
2601 * delayed allocation.
2602 * Insert a new entry.
2604 oldlen
= newlen
= 0;
2605 xfs_iext_insert(ip
, icur
, new, state
);
2608 if (oldlen
!= newlen
) {
2609 ASSERT(oldlen
> newlen
);
2610 xfs_mod_fdblocks(ip
->i_mount
, (int64_t)(oldlen
- newlen
),
2613 * Nothing to do for disk quota accounting here.
2619 * Convert a hole to a real allocation.
2621 STATIC
int /* error */
2622 xfs_bmap_add_extent_hole_real(
2623 struct xfs_trans
*tp
,
2624 struct xfs_inode
*ip
,
2626 struct xfs_iext_cursor
*icur
,
2627 struct xfs_btree_cur
**curp
,
2628 struct xfs_bmbt_irec
*new,
2632 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
2633 struct xfs_mount
*mp
= ip
->i_mount
;
2634 struct xfs_btree_cur
*cur
= *curp
;
2635 int error
; /* error return value */
2636 int i
; /* temp state */
2637 xfs_bmbt_irec_t left
; /* left neighbor extent entry */
2638 xfs_bmbt_irec_t right
; /* right neighbor extent entry */
2639 int rval
=0; /* return value (logging flags) */
2640 int state
= xfs_bmap_fork_to_state(whichfork
);
2641 struct xfs_bmbt_irec old
;
2643 ASSERT(!isnullstartblock(new->br_startblock
));
2644 ASSERT(!cur
|| !(cur
->bc_private
.b
.flags
& XFS_BTCUR_BPRV_WASDEL
));
2646 XFS_STATS_INC(mp
, xs_add_exlist
);
2649 * Check and set flags if this segment has a left neighbor.
2651 if (xfs_iext_peek_prev_extent(ifp
, icur
, &left
)) {
2652 state
|= BMAP_LEFT_VALID
;
2653 if (isnullstartblock(left
.br_startblock
))
2654 state
|= BMAP_LEFT_DELAY
;
2658 * Check and set flags if this segment has a current value.
2659 * Not true if we're inserting into the "hole" at eof.
2661 if (xfs_iext_get_extent(ifp
, icur
, &right
)) {
2662 state
|= BMAP_RIGHT_VALID
;
2663 if (isnullstartblock(right
.br_startblock
))
2664 state
|= BMAP_RIGHT_DELAY
;
2668 * We're inserting a real allocation between "left" and "right".
2669 * Set the contiguity flags. Don't let extents get too large.
2671 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
2672 left
.br_startoff
+ left
.br_blockcount
== new->br_startoff
&&
2673 left
.br_startblock
+ left
.br_blockcount
== new->br_startblock
&&
2674 left
.br_state
== new->br_state
&&
2675 left
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
2676 state
|= BMAP_LEFT_CONTIG
;
2678 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
2679 new->br_startoff
+ new->br_blockcount
== right
.br_startoff
&&
2680 new->br_startblock
+ new->br_blockcount
== right
.br_startblock
&&
2681 new->br_state
== right
.br_state
&&
2682 new->br_blockcount
+ right
.br_blockcount
<= MAXEXTLEN
&&
2683 (!(state
& BMAP_LEFT_CONTIG
) ||
2684 left
.br_blockcount
+ new->br_blockcount
+
2685 right
.br_blockcount
<= MAXEXTLEN
))
2686 state
|= BMAP_RIGHT_CONTIG
;
2690 * Select which case we're in here, and implement it.
2692 switch (state
& (BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
)) {
2693 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2695 * New allocation is contiguous with real allocations on the
2696 * left and on the right.
2697 * Merge all three into a single extent record.
2699 left
.br_blockcount
+= new->br_blockcount
+ right
.br_blockcount
;
2701 xfs_iext_remove(ip
, icur
, state
);
2702 xfs_iext_prev(ifp
, icur
);
2703 xfs_iext_update_extent(ip
, state
, icur
, &left
);
2705 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2706 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
2708 rval
= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
2710 rval
= XFS_ILOG_CORE
;
2711 error
= xfs_bmbt_lookup_eq(cur
, &right
, &i
);
2714 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2715 error
= xfs_btree_delete(cur
, &i
);
2718 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2719 error
= xfs_btree_decrement(cur
, 0, &i
);
2722 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2723 error
= xfs_bmbt_update(cur
, &left
);
2729 case BMAP_LEFT_CONTIG
:
2731 * New allocation is contiguous with a real allocation
2733 * Merge the new allocation with the left neighbor.
2736 left
.br_blockcount
+= new->br_blockcount
;
2738 xfs_iext_prev(ifp
, icur
);
2739 xfs_iext_update_extent(ip
, state
, icur
, &left
);
2742 rval
= xfs_ilog_fext(whichfork
);
2745 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2748 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2749 error
= xfs_bmbt_update(cur
, &left
);
2755 case BMAP_RIGHT_CONTIG
:
2757 * New allocation is contiguous with a real allocation
2759 * Merge the new allocation with the right neighbor.
2763 right
.br_startoff
= new->br_startoff
;
2764 right
.br_startblock
= new->br_startblock
;
2765 right
.br_blockcount
+= new->br_blockcount
;
2766 xfs_iext_update_extent(ip
, state
, icur
, &right
);
2769 rval
= xfs_ilog_fext(whichfork
);
2772 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2775 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2776 error
= xfs_bmbt_update(cur
, &right
);
2784 * New allocation is not contiguous with another
2786 * Insert a new entry.
2788 xfs_iext_insert(ip
, icur
, new, state
);
2789 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2790 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
2792 rval
= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
2794 rval
= XFS_ILOG_CORE
;
2795 error
= xfs_bmbt_lookup_eq(cur
, new, &i
);
2798 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
2799 error
= xfs_btree_insert(cur
, &i
);
2802 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2807 /* add reverse mapping unless caller opted out */
2808 if (!(flags
& XFS_BMAPI_NORMAP
)) {
2809 error
= xfs_rmap_map_extent(tp
, ip
, whichfork
, new);
2814 /* convert to a btree if necessary */
2815 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
2816 int tmp_logflags
; /* partial log flag return val */
2818 ASSERT(cur
== NULL
);
2819 error
= xfs_bmap_extents_to_btree(tp
, ip
, curp
, 0,
2820 &tmp_logflags
, whichfork
);
2821 *logflagsp
|= tmp_logflags
;
2827 /* clear out the allocated field, done with it now in any case. */
2829 cur
->bc_private
.b
.allocated
= 0;
2831 xfs_bmap_check_leaf_extents(cur
, ip
, whichfork
);
2838 * Functions used in the extent read, allocate and remove paths
2842 * Adjust the size of the new extent based on di_extsize and rt extsize.
2845 xfs_bmap_extsize_align(
2847 xfs_bmbt_irec_t
*gotp
, /* next extent pointer */
2848 xfs_bmbt_irec_t
*prevp
, /* previous extent pointer */
2849 xfs_extlen_t extsz
, /* align to this extent size */
2850 int rt
, /* is this a realtime inode? */
2851 int eof
, /* is extent at end-of-file? */
2852 int delay
, /* creating delalloc extent? */
2853 int convert
, /* overwriting unwritten extent? */
2854 xfs_fileoff_t
*offp
, /* in/out: aligned offset */
2855 xfs_extlen_t
*lenp
) /* in/out: aligned length */
2857 xfs_fileoff_t orig_off
; /* original offset */
2858 xfs_extlen_t orig_alen
; /* original length */
2859 xfs_fileoff_t orig_end
; /* original off+len */
2860 xfs_fileoff_t nexto
; /* next file offset */
2861 xfs_fileoff_t prevo
; /* previous file offset */
2862 xfs_fileoff_t align_off
; /* temp for offset */
2863 xfs_extlen_t align_alen
; /* temp for length */
2864 xfs_extlen_t temp
; /* temp for calculations */
2869 orig_off
= align_off
= *offp
;
2870 orig_alen
= align_alen
= *lenp
;
2871 orig_end
= orig_off
+ orig_alen
;
2874 * If this request overlaps an existing extent, then don't
2875 * attempt to perform any additional alignment.
2877 if (!delay
&& !eof
&&
2878 (orig_off
>= gotp
->br_startoff
) &&
2879 (orig_end
<= gotp
->br_startoff
+ gotp
->br_blockcount
)) {
2884 * If the file offset is unaligned vs. the extent size
2885 * we need to align it. This will be possible unless
2886 * the file was previously written with a kernel that didn't
2887 * perform this alignment, or if a truncate shot us in the
2890 div_u64_rem(orig_off
, extsz
, &temp
);
2896 /* Same adjustment for the end of the requested area. */
2897 temp
= (align_alen
% extsz
);
2899 align_alen
+= extsz
- temp
;
2902 * For large extent hint sizes, the aligned extent might be larger than
2903 * MAXEXTLEN. In that case, reduce the size by an extsz so that it pulls
2904 * the length back under MAXEXTLEN. The outer allocation loops handle
2905 * short allocation just fine, so it is safe to do this. We only want to
2906 * do it when we are forced to, though, because it means more allocation
2907 * operations are required.
2909 while (align_alen
> MAXEXTLEN
)
2910 align_alen
-= extsz
;
2911 ASSERT(align_alen
<= MAXEXTLEN
);
2914 * If the previous block overlaps with this proposed allocation
2915 * then move the start forward without adjusting the length.
2917 if (prevp
->br_startoff
!= NULLFILEOFF
) {
2918 if (prevp
->br_startblock
== HOLESTARTBLOCK
)
2919 prevo
= prevp
->br_startoff
;
2921 prevo
= prevp
->br_startoff
+ prevp
->br_blockcount
;
2924 if (align_off
!= orig_off
&& align_off
< prevo
)
2927 * If the next block overlaps with this proposed allocation
2928 * then move the start back without adjusting the length,
2929 * but not before offset 0.
2930 * This may of course make the start overlap previous block,
2931 * and if we hit the offset 0 limit then the next block
2932 * can still overlap too.
2934 if (!eof
&& gotp
->br_startoff
!= NULLFILEOFF
) {
2935 if ((delay
&& gotp
->br_startblock
== HOLESTARTBLOCK
) ||
2936 (!delay
&& gotp
->br_startblock
== DELAYSTARTBLOCK
))
2937 nexto
= gotp
->br_startoff
+ gotp
->br_blockcount
;
2939 nexto
= gotp
->br_startoff
;
2941 nexto
= NULLFILEOFF
;
2943 align_off
+ align_alen
!= orig_end
&&
2944 align_off
+ align_alen
> nexto
)
2945 align_off
= nexto
> align_alen
? nexto
- align_alen
: 0;
2947 * If we're now overlapping the next or previous extent that
2948 * means we can't fit an extsz piece in this hole. Just move
2949 * the start forward to the first valid spot and set
2950 * the length so we hit the end.
2952 if (align_off
!= orig_off
&& align_off
< prevo
)
2954 if (align_off
+ align_alen
!= orig_end
&&
2955 align_off
+ align_alen
> nexto
&&
2956 nexto
!= NULLFILEOFF
) {
2957 ASSERT(nexto
> prevo
);
2958 align_alen
= nexto
- align_off
;
2962 * If realtime, and the result isn't a multiple of the realtime
2963 * extent size we need to remove blocks until it is.
2965 if (rt
&& (temp
= (align_alen
% mp
->m_sb
.sb_rextsize
))) {
2967 * We're not covering the original request, or
2968 * we won't be able to once we fix the length.
2970 if (orig_off
< align_off
||
2971 orig_end
> align_off
+ align_alen
||
2972 align_alen
- temp
< orig_alen
)
2975 * Try to fix it by moving the start up.
2977 if (align_off
+ temp
<= orig_off
) {
2982 * Try to fix it by moving the end in.
2984 else if (align_off
+ align_alen
- temp
>= orig_end
)
2987 * Set the start to the minimum then trim the length.
2990 align_alen
-= orig_off
- align_off
;
2991 align_off
= orig_off
;
2992 align_alen
-= align_alen
% mp
->m_sb
.sb_rextsize
;
2995 * Result doesn't cover the request, fail it.
2997 if (orig_off
< align_off
|| orig_end
> align_off
+ align_alen
)
3000 ASSERT(orig_off
>= align_off
);
3001 /* see MAXEXTLEN handling above */
3002 ASSERT(orig_end
<= align_off
+ align_alen
||
3003 align_alen
+ extsz
> MAXEXTLEN
);
3007 if (!eof
&& gotp
->br_startoff
!= NULLFILEOFF
)
3008 ASSERT(align_off
+ align_alen
<= gotp
->br_startoff
);
3009 if (prevp
->br_startoff
!= NULLFILEOFF
)
3010 ASSERT(align_off
>= prevp
->br_startoff
+ prevp
->br_blockcount
);
3018 #define XFS_ALLOC_GAP_UNITS 4
3022 struct xfs_bmalloca
*ap
) /* bmap alloc argument struct */
3024 xfs_fsblock_t adjust
; /* adjustment to block numbers */
3025 xfs_agnumber_t fb_agno
; /* ag number of ap->firstblock */
3026 xfs_mount_t
*mp
; /* mount point structure */
3027 int nullfb
; /* true if ap->firstblock isn't set */
3028 int rt
; /* true if inode is realtime */
3030 #define ISVALID(x,y) \
3032 (x) < mp->m_sb.sb_rblocks : \
3033 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
3034 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
3035 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
3037 mp
= ap
->ip
->i_mount
;
3038 nullfb
= ap
->tp
->t_firstblock
== NULLFSBLOCK
;
3039 rt
= XFS_IS_REALTIME_INODE(ap
->ip
) &&
3040 xfs_alloc_is_userdata(ap
->datatype
);
3041 fb_agno
= nullfb
? NULLAGNUMBER
: XFS_FSB_TO_AGNO(mp
,
3042 ap
->tp
->t_firstblock
);
3044 * If allocating at eof, and there's a previous real block,
3045 * try to use its last block as our starting point.
3047 if (ap
->eof
&& ap
->prev
.br_startoff
!= NULLFILEOFF
&&
3048 !isnullstartblock(ap
->prev
.br_startblock
) &&
3049 ISVALID(ap
->prev
.br_startblock
+ ap
->prev
.br_blockcount
,
3050 ap
->prev
.br_startblock
)) {
3051 ap
->blkno
= ap
->prev
.br_startblock
+ ap
->prev
.br_blockcount
;
3053 * Adjust for the gap between prevp and us.
3055 adjust
= ap
->offset
-
3056 (ap
->prev
.br_startoff
+ ap
->prev
.br_blockcount
);
3058 ISVALID(ap
->blkno
+ adjust
, ap
->prev
.br_startblock
))
3059 ap
->blkno
+= adjust
;
3062 * If not at eof, then compare the two neighbor blocks.
3063 * Figure out whether either one gives us a good starting point,
3064 * and pick the better one.
3066 else if (!ap
->eof
) {
3067 xfs_fsblock_t gotbno
; /* right side block number */
3068 xfs_fsblock_t gotdiff
=0; /* right side difference */
3069 xfs_fsblock_t prevbno
; /* left side block number */
3070 xfs_fsblock_t prevdiff
=0; /* left side difference */
3073 * If there's a previous (left) block, select a requested
3074 * start block based on it.
3076 if (ap
->prev
.br_startoff
!= NULLFILEOFF
&&
3077 !isnullstartblock(ap
->prev
.br_startblock
) &&
3078 (prevbno
= ap
->prev
.br_startblock
+
3079 ap
->prev
.br_blockcount
) &&
3080 ISVALID(prevbno
, ap
->prev
.br_startblock
)) {
3082 * Calculate gap to end of previous block.
3084 adjust
= prevdiff
= ap
->offset
-
3085 (ap
->prev
.br_startoff
+
3086 ap
->prev
.br_blockcount
);
3088 * Figure the startblock based on the previous block's
3089 * end and the gap size.
3091 * If the gap is large relative to the piece we're
3092 * allocating, or using it gives us an invalid block
3093 * number, then just use the end of the previous block.
3095 if (prevdiff
<= XFS_ALLOC_GAP_UNITS
* ap
->length
&&
3096 ISVALID(prevbno
+ prevdiff
,
3097 ap
->prev
.br_startblock
))
3102 * If the firstblock forbids it, can't use it,
3105 if (!rt
&& !nullfb
&&
3106 XFS_FSB_TO_AGNO(mp
, prevbno
) != fb_agno
)
3107 prevbno
= NULLFSBLOCK
;
3110 * No previous block or can't follow it, just default.
3113 prevbno
= NULLFSBLOCK
;
3115 * If there's a following (right) block, select a requested
3116 * start block based on it.
3118 if (!isnullstartblock(ap
->got
.br_startblock
)) {
3120 * Calculate gap to start of next block.
3122 adjust
= gotdiff
= ap
->got
.br_startoff
- ap
->offset
;
3124 * Figure the startblock based on the next block's
3125 * start and the gap size.
3127 gotbno
= ap
->got
.br_startblock
;
3130 * If the gap is large relative to the piece we're
3131 * allocating, or using it gives us an invalid block
3132 * number, then just use the start of the next block
3133 * offset by our length.
3135 if (gotdiff
<= XFS_ALLOC_GAP_UNITS
* ap
->length
&&
3136 ISVALID(gotbno
- gotdiff
, gotbno
))
3138 else if (ISVALID(gotbno
- ap
->length
, gotbno
)) {
3139 gotbno
-= ap
->length
;
3140 gotdiff
+= adjust
- ap
->length
;
3144 * If the firstblock forbids it, can't use it,
3147 if (!rt
&& !nullfb
&&
3148 XFS_FSB_TO_AGNO(mp
, gotbno
) != fb_agno
)
3149 gotbno
= NULLFSBLOCK
;
3152 * No next block, just default.
3155 gotbno
= NULLFSBLOCK
;
3157 * If both valid, pick the better one, else the only good
3158 * one, else ap->blkno is already set (to 0 or the inode block).
3160 if (prevbno
!= NULLFSBLOCK
&& gotbno
!= NULLFSBLOCK
)
3161 ap
->blkno
= prevdiff
<= gotdiff
? prevbno
: gotbno
;
3162 else if (prevbno
!= NULLFSBLOCK
)
3163 ap
->blkno
= prevbno
;
3164 else if (gotbno
!= NULLFSBLOCK
)
3171 xfs_bmap_longest_free_extent(
3172 struct xfs_trans
*tp
,
3177 struct xfs_mount
*mp
= tp
->t_mountp
;
3178 struct xfs_perag
*pag
;
3179 xfs_extlen_t longest
;
3182 pag
= xfs_perag_get(mp
, ag
);
3183 if (!pag
->pagf_init
) {
3184 error
= xfs_alloc_pagf_init(mp
, tp
, ag
, XFS_ALLOC_FLAG_TRYLOCK
);
3188 if (!pag
->pagf_init
) {
3194 longest
= xfs_alloc_longest_free_extent(pag
,
3195 xfs_alloc_min_freelist(mp
, pag
),
3196 xfs_ag_resv_needed(pag
, XFS_AG_RESV_NONE
));
3197 if (*blen
< longest
)
3206 xfs_bmap_select_minlen(
3207 struct xfs_bmalloca
*ap
,
3208 struct xfs_alloc_arg
*args
,
3212 if (notinit
|| *blen
< ap
->minlen
) {
3214 * Since we did a BUF_TRYLOCK above, it is possible that
3215 * there is space for this request.
3217 args
->minlen
= ap
->minlen
;
3218 } else if (*blen
< args
->maxlen
) {
3220 * If the best seen length is less than the request length,
3221 * use the best as the minimum.
3223 args
->minlen
= *blen
;
3226 * Otherwise we've seen an extent as big as maxlen, use that
3229 args
->minlen
= args
->maxlen
;
3234 xfs_bmap_btalloc_nullfb(
3235 struct xfs_bmalloca
*ap
,
3236 struct xfs_alloc_arg
*args
,
3239 struct xfs_mount
*mp
= ap
->ip
->i_mount
;
3240 xfs_agnumber_t ag
, startag
;
3244 args
->type
= XFS_ALLOCTYPE_START_BNO
;
3245 args
->total
= ap
->total
;
3247 startag
= ag
= XFS_FSB_TO_AGNO(mp
, args
->fsbno
);
3248 if (startag
== NULLAGNUMBER
)
3251 while (*blen
< args
->maxlen
) {
3252 error
= xfs_bmap_longest_free_extent(args
->tp
, ag
, blen
,
3257 if (++ag
== mp
->m_sb
.sb_agcount
)
3263 xfs_bmap_select_minlen(ap
, args
, blen
, notinit
);
3268 xfs_bmap_btalloc_filestreams(
3269 struct xfs_bmalloca
*ap
,
3270 struct xfs_alloc_arg
*args
,
3273 struct xfs_mount
*mp
= ap
->ip
->i_mount
;
3278 args
->type
= XFS_ALLOCTYPE_NEAR_BNO
;
3279 args
->total
= ap
->total
;
3281 ag
= XFS_FSB_TO_AGNO(mp
, args
->fsbno
);
3282 if (ag
== NULLAGNUMBER
)
3285 error
= xfs_bmap_longest_free_extent(args
->tp
, ag
, blen
, ¬init
);
3289 if (*blen
< args
->maxlen
) {
3290 error
= xfs_filestream_new_ag(ap
, &ag
);
3294 error
= xfs_bmap_longest_free_extent(args
->tp
, ag
, blen
,
3301 xfs_bmap_select_minlen(ap
, args
, blen
, notinit
);
3304 * Set the failure fallback case to look in the selected AG as stream
3307 ap
->blkno
= args
->fsbno
= XFS_AGB_TO_FSB(mp
, ag
, 0);
3311 /* Update all inode and quota accounting for the allocation we just did. */
3313 xfs_bmap_btalloc_accounting(
3314 struct xfs_bmalloca
*ap
,
3315 struct xfs_alloc_arg
*args
)
3317 if (ap
->flags
& XFS_BMAPI_COWFORK
) {
3319 * COW fork blocks are in-core only and thus are treated as
3320 * in-core quota reservation (like delalloc blocks) even when
3321 * converted to real blocks. The quota reservation is not
3322 * accounted to disk until blocks are remapped to the data
3323 * fork. So if these blocks were previously delalloc, we
3324 * already have quota reservation and there's nothing to do
3331 * Otherwise, we've allocated blocks in a hole. The transaction
3332 * has acquired in-core quota reservation for this extent.
3333 * Rather than account these as real blocks, however, we reduce
3334 * the transaction quota reservation based on the allocation.
3335 * This essentially transfers the transaction quota reservation
3336 * to that of a delalloc extent.
3338 ap
->ip
->i_delayed_blks
+= args
->len
;
3339 xfs_trans_mod_dquot_byino(ap
->tp
, ap
->ip
, XFS_TRANS_DQ_RES_BLKS
,
3344 /* data/attr fork only */
3345 ap
->ip
->i_d
.di_nblocks
+= args
->len
;
3346 xfs_trans_log_inode(ap
->tp
, ap
->ip
, XFS_ILOG_CORE
);
3348 ap
->ip
->i_delayed_blks
-= args
->len
;
3349 xfs_trans_mod_dquot_byino(ap
->tp
, ap
->ip
,
3350 ap
->wasdel
? XFS_TRANS_DQ_DELBCOUNT
: XFS_TRANS_DQ_BCOUNT
,
3356 struct xfs_bmalloca
*ap
) /* bmap alloc argument struct */
3358 xfs_mount_t
*mp
; /* mount point structure */
3359 xfs_alloctype_t atype
= 0; /* type for allocation routines */
3360 xfs_extlen_t align
= 0; /* minimum allocation alignment */
3361 xfs_agnumber_t fb_agno
; /* ag number of ap->firstblock */
3363 xfs_alloc_arg_t args
;
3364 xfs_fileoff_t orig_offset
;
3365 xfs_extlen_t orig_length
;
3367 xfs_extlen_t nextminlen
= 0;
3368 int nullfb
; /* true if ap->firstblock isn't set */
3375 orig_offset
= ap
->offset
;
3376 orig_length
= ap
->length
;
3378 mp
= ap
->ip
->i_mount
;
3380 /* stripe alignment for allocation is determined by mount parameters */
3382 if (mp
->m_swidth
&& (mp
->m_flags
& XFS_MOUNT_SWALLOC
))
3383 stripe_align
= mp
->m_swidth
;
3384 else if (mp
->m_dalign
)
3385 stripe_align
= mp
->m_dalign
;
3387 if (ap
->flags
& XFS_BMAPI_COWFORK
)
3388 align
= xfs_get_cowextsz_hint(ap
->ip
);
3389 else if (xfs_alloc_is_userdata(ap
->datatype
))
3390 align
= xfs_get_extsz_hint(ap
->ip
);
3392 error
= xfs_bmap_extsize_align(mp
, &ap
->got
, &ap
->prev
,
3393 align
, 0, ap
->eof
, 0, ap
->conv
,
3394 &ap
->offset
, &ap
->length
);
3400 nullfb
= ap
->tp
->t_firstblock
== NULLFSBLOCK
;
3401 fb_agno
= nullfb
? NULLAGNUMBER
: XFS_FSB_TO_AGNO(mp
,
3402 ap
->tp
->t_firstblock
);
3404 if (xfs_alloc_is_userdata(ap
->datatype
) &&
3405 xfs_inode_is_filestream(ap
->ip
)) {
3406 ag
= xfs_filestream_lookup_ag(ap
->ip
);
3407 ag
= (ag
!= NULLAGNUMBER
) ? ag
: 0;
3408 ap
->blkno
= XFS_AGB_TO_FSB(mp
, ag
, 0);
3410 ap
->blkno
= XFS_INO_TO_FSB(mp
, ap
->ip
->i_ino
);
3413 ap
->blkno
= ap
->tp
->t_firstblock
;
3415 xfs_bmap_adjacent(ap
);
3418 * If allowed, use ap->blkno; otherwise must use firstblock since
3419 * it's in the right allocation group.
3421 if (nullfb
|| XFS_FSB_TO_AGNO(mp
, ap
->blkno
) == fb_agno
)
3424 ap
->blkno
= ap
->tp
->t_firstblock
;
3426 * Normal allocation, done through xfs_alloc_vextent.
3428 tryagain
= isaligned
= 0;
3429 memset(&args
, 0, sizeof(args
));
3432 args
.fsbno
= ap
->blkno
;
3433 xfs_rmap_skip_owner_update(&args
.oinfo
);
3435 /* Trim the allocation back to the maximum an AG can fit. */
3436 args
.maxlen
= min(ap
->length
, mp
->m_ag_max_usable
);
3440 * Search for an allocation group with a single extent large
3441 * enough for the request. If one isn't found, then adjust
3442 * the minimum allocation size to the largest space found.
3444 if (xfs_alloc_is_userdata(ap
->datatype
) &&
3445 xfs_inode_is_filestream(ap
->ip
))
3446 error
= xfs_bmap_btalloc_filestreams(ap
, &args
, &blen
);
3448 error
= xfs_bmap_btalloc_nullfb(ap
, &args
, &blen
);
3451 } else if (ap
->tp
->t_flags
& XFS_TRANS_LOWMODE
) {
3452 if (xfs_inode_is_filestream(ap
->ip
))
3453 args
.type
= XFS_ALLOCTYPE_FIRST_AG
;
3455 args
.type
= XFS_ALLOCTYPE_START_BNO
;
3456 args
.total
= args
.minlen
= ap
->minlen
;
3458 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
3459 args
.total
= ap
->total
;
3460 args
.minlen
= ap
->minlen
;
3462 /* apply extent size hints if obtained earlier */
3465 div_u64_rem(ap
->offset
, args
.prod
, &args
.mod
);
3467 args
.mod
= args
.prod
- args
.mod
;
3468 } else if (mp
->m_sb
.sb_blocksize
>= PAGE_SIZE
) {
3472 args
.prod
= PAGE_SIZE
>> mp
->m_sb
.sb_blocklog
;
3473 div_u64_rem(ap
->offset
, args
.prod
, &args
.mod
);
3475 args
.mod
= args
.prod
- args
.mod
;
3478 * If we are not low on available data blocks, and the
3479 * underlying logical volume manager is a stripe, and
3480 * the file offset is zero then try to allocate data
3481 * blocks on stripe unit boundary.
3482 * NOTE: ap->aeof is only set if the allocation length
3483 * is >= the stripe unit and the allocation offset is
3484 * at the end of file.
3486 if (!(ap
->tp
->t_flags
& XFS_TRANS_LOWMODE
) && ap
->aeof
) {
3488 args
.alignment
= stripe_align
;
3492 * Adjust for alignment
3494 if (blen
> args
.alignment
&& blen
<= args
.maxlen
)
3495 args
.minlen
= blen
- args
.alignment
;
3496 args
.minalignslop
= 0;
3499 * First try an exact bno allocation.
3500 * If it fails then do a near or start bno
3501 * allocation with alignment turned on.
3505 args
.type
= XFS_ALLOCTYPE_THIS_BNO
;
3508 * Compute the minlen+alignment for the
3509 * next case. Set slop so that the value
3510 * of minlen+alignment+slop doesn't go up
3511 * between the calls.
3513 if (blen
> stripe_align
&& blen
<= args
.maxlen
)
3514 nextminlen
= blen
- stripe_align
;
3516 nextminlen
= args
.minlen
;
3517 if (nextminlen
+ stripe_align
> args
.minlen
+ 1)
3519 nextminlen
+ stripe_align
-
3522 args
.minalignslop
= 0;
3526 args
.minalignslop
= 0;
3528 args
.minleft
= ap
->minleft
;
3529 args
.wasdel
= ap
->wasdel
;
3530 args
.resv
= XFS_AG_RESV_NONE
;
3531 args
.datatype
= ap
->datatype
;
3532 if (ap
->datatype
& XFS_ALLOC_USERDATA_ZERO
)
3535 error
= xfs_alloc_vextent(&args
);
3539 if (tryagain
&& args
.fsbno
== NULLFSBLOCK
) {
3541 * Exact allocation failed. Now try with alignment
3545 args
.fsbno
= ap
->blkno
;
3546 args
.alignment
= stripe_align
;
3547 args
.minlen
= nextminlen
;
3548 args
.minalignslop
= 0;
3550 if ((error
= xfs_alloc_vextent(&args
)))
3553 if (isaligned
&& args
.fsbno
== NULLFSBLOCK
) {
3555 * allocation failed, so turn off alignment and
3559 args
.fsbno
= ap
->blkno
;
3561 if ((error
= xfs_alloc_vextent(&args
)))
3564 if (args
.fsbno
== NULLFSBLOCK
&& nullfb
&&
3565 args
.minlen
> ap
->minlen
) {
3566 args
.minlen
= ap
->minlen
;
3567 args
.type
= XFS_ALLOCTYPE_START_BNO
;
3568 args
.fsbno
= ap
->blkno
;
3569 if ((error
= xfs_alloc_vextent(&args
)))
3572 if (args
.fsbno
== NULLFSBLOCK
&& nullfb
) {
3574 args
.type
= XFS_ALLOCTYPE_FIRST_AG
;
3575 args
.total
= ap
->minlen
;
3576 if ((error
= xfs_alloc_vextent(&args
)))
3578 ap
->tp
->t_flags
|= XFS_TRANS_LOWMODE
;
3580 if (args
.fsbno
!= NULLFSBLOCK
) {
3582 * check the allocation happened at the same or higher AG than
3583 * the first block that was allocated.
3585 ASSERT(ap
->tp
->t_firstblock
== NULLFSBLOCK
||
3586 XFS_FSB_TO_AGNO(mp
, ap
->tp
->t_firstblock
) <=
3587 XFS_FSB_TO_AGNO(mp
, args
.fsbno
));
3589 ap
->blkno
= args
.fsbno
;
3590 if (ap
->tp
->t_firstblock
== NULLFSBLOCK
)
3591 ap
->tp
->t_firstblock
= args
.fsbno
;
3592 ASSERT(nullfb
|| fb_agno
<= args
.agno
);
3593 ap
->length
= args
.len
;
3595 * If the extent size hint is active, we tried to round the
3596 * caller's allocation request offset down to extsz and the
3597 * length up to another extsz boundary. If we found a free
3598 * extent we mapped it in starting at this new offset. If the
3599 * newly mapped space isn't long enough to cover any of the
3600 * range of offsets that was originally requested, move the
3601 * mapping up so that we can fill as much of the caller's
3602 * original request as possible. Free space is apparently
3603 * very fragmented so we're unlikely to be able to satisfy the
3606 if (ap
->length
<= orig_length
)
3607 ap
->offset
= orig_offset
;
3608 else if (ap
->offset
+ ap
->length
< orig_offset
+ orig_length
)
3609 ap
->offset
= orig_offset
+ orig_length
- ap
->length
;
3610 xfs_bmap_btalloc_accounting(ap
, &args
);
3612 ap
->blkno
= NULLFSBLOCK
;
3619 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
3620 * It figures out where to ask the underlying allocator to put the new extent.
3624 struct xfs_bmalloca
*ap
) /* bmap alloc argument struct */
3626 if (XFS_IS_REALTIME_INODE(ap
->ip
) &&
3627 xfs_alloc_is_userdata(ap
->datatype
))
3628 return xfs_bmap_rtalloc(ap
);
3629 return xfs_bmap_btalloc(ap
);
3632 /* Trim extent to fit a logical block range. */
3635 struct xfs_bmbt_irec
*irec
,
3639 xfs_fileoff_t distance
;
3640 xfs_fileoff_t end
= bno
+ len
;
3642 if (irec
->br_startoff
+ irec
->br_blockcount
<= bno
||
3643 irec
->br_startoff
>= end
) {
3644 irec
->br_blockcount
= 0;
3648 if (irec
->br_startoff
< bno
) {
3649 distance
= bno
- irec
->br_startoff
;
3650 if (isnullstartblock(irec
->br_startblock
))
3651 irec
->br_startblock
= DELAYSTARTBLOCK
;
3652 if (irec
->br_startblock
!= DELAYSTARTBLOCK
&&
3653 irec
->br_startblock
!= HOLESTARTBLOCK
)
3654 irec
->br_startblock
+= distance
;
3655 irec
->br_startoff
+= distance
;
3656 irec
->br_blockcount
-= distance
;
3659 if (end
< irec
->br_startoff
+ irec
->br_blockcount
) {
3660 distance
= irec
->br_startoff
+ irec
->br_blockcount
- end
;
3661 irec
->br_blockcount
-= distance
;
3665 /* trim extent to within eof */
3667 xfs_trim_extent_eof(
3668 struct xfs_bmbt_irec
*irec
,
3669 struct xfs_inode
*ip
)
3672 xfs_trim_extent(irec
, 0, XFS_B_TO_FSB(ip
->i_mount
,
3673 i_size_read(VFS_I(ip
))));
3677 * Trim the returned map to the required bounds
3681 struct xfs_bmbt_irec
*mval
,
3682 struct xfs_bmbt_irec
*got
,
3690 if ((flags
& XFS_BMAPI_ENTIRE
) ||
3691 got
->br_startoff
+ got
->br_blockcount
<= obno
) {
3693 if (isnullstartblock(got
->br_startblock
))
3694 mval
->br_startblock
= DELAYSTARTBLOCK
;
3700 ASSERT((*bno
>= obno
) || (n
== 0));
3702 mval
->br_startoff
= *bno
;
3703 if (isnullstartblock(got
->br_startblock
))
3704 mval
->br_startblock
= DELAYSTARTBLOCK
;
3706 mval
->br_startblock
= got
->br_startblock
+
3707 (*bno
- got
->br_startoff
);
3709 * Return the minimum of what we got and what we asked for for
3710 * the length. We can use the len variable here because it is
3711 * modified below and we could have been there before coming
3712 * here if the first part of the allocation didn't overlap what
3715 mval
->br_blockcount
= XFS_FILBLKS_MIN(end
- *bno
,
3716 got
->br_blockcount
- (*bno
- got
->br_startoff
));
3717 mval
->br_state
= got
->br_state
;
3718 ASSERT(mval
->br_blockcount
<= len
);
3723 * Update and validate the extent map to return
3726 xfs_bmapi_update_map(
3727 struct xfs_bmbt_irec
**map
,
3735 xfs_bmbt_irec_t
*mval
= *map
;
3737 ASSERT((flags
& XFS_BMAPI_ENTIRE
) ||
3738 ((mval
->br_startoff
+ mval
->br_blockcount
) <= end
));
3739 ASSERT((flags
& XFS_BMAPI_ENTIRE
) || (mval
->br_blockcount
<= *len
) ||
3740 (mval
->br_startoff
< obno
));
3742 *bno
= mval
->br_startoff
+ mval
->br_blockcount
;
3744 if (*n
> 0 && mval
->br_startoff
== mval
[-1].br_startoff
) {
3745 /* update previous map with new information */
3746 ASSERT(mval
->br_startblock
== mval
[-1].br_startblock
);
3747 ASSERT(mval
->br_blockcount
> mval
[-1].br_blockcount
);
3748 ASSERT(mval
->br_state
== mval
[-1].br_state
);
3749 mval
[-1].br_blockcount
= mval
->br_blockcount
;
3750 mval
[-1].br_state
= mval
->br_state
;
3751 } else if (*n
> 0 && mval
->br_startblock
!= DELAYSTARTBLOCK
&&
3752 mval
[-1].br_startblock
!= DELAYSTARTBLOCK
&&
3753 mval
[-1].br_startblock
!= HOLESTARTBLOCK
&&
3754 mval
->br_startblock
== mval
[-1].br_startblock
+
3755 mval
[-1].br_blockcount
&&
3756 mval
[-1].br_state
== mval
->br_state
) {
3757 ASSERT(mval
->br_startoff
==
3758 mval
[-1].br_startoff
+ mval
[-1].br_blockcount
);
3759 mval
[-1].br_blockcount
+= mval
->br_blockcount
;
3760 } else if (*n
> 0 &&
3761 mval
->br_startblock
== DELAYSTARTBLOCK
&&
3762 mval
[-1].br_startblock
== DELAYSTARTBLOCK
&&
3763 mval
->br_startoff
==
3764 mval
[-1].br_startoff
+ mval
[-1].br_blockcount
) {
3765 mval
[-1].br_blockcount
+= mval
->br_blockcount
;
3766 mval
[-1].br_state
= mval
->br_state
;
3767 } else if (!((*n
== 0) &&
3768 ((mval
->br_startoff
+ mval
->br_blockcount
) <=
3777 * Map file blocks to filesystem blocks without allocation.
3781 struct xfs_inode
*ip
,
3784 struct xfs_bmbt_irec
*mval
,
3788 struct xfs_mount
*mp
= ip
->i_mount
;
3789 struct xfs_ifork
*ifp
;
3790 struct xfs_bmbt_irec got
;
3793 struct xfs_iext_cursor icur
;
3797 int whichfork
= xfs_bmapi_whichfork(flags
);
3800 ASSERT(!(flags
& ~(XFS_BMAPI_ATTRFORK
|XFS_BMAPI_ENTIRE
|
3801 XFS_BMAPI_COWFORK
)));
3802 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_SHARED
|XFS_ILOCK_EXCL
));
3804 if (unlikely(XFS_TEST_ERROR(
3805 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
3806 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
3807 mp
, XFS_ERRTAG_BMAPIFORMAT
))) {
3808 XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW
, mp
);
3809 return -EFSCORRUPTED
;
3812 if (XFS_FORCED_SHUTDOWN(mp
))
3815 XFS_STATS_INC(mp
, xs_blk_mapr
);
3817 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
3819 /* No CoW fork? Return a hole. */
3820 if (whichfork
== XFS_COW_FORK
&& !ifp
) {
3821 mval
->br_startoff
= bno
;
3822 mval
->br_startblock
= HOLESTARTBLOCK
;
3823 mval
->br_blockcount
= len
;
3824 mval
->br_state
= XFS_EXT_NORM
;
3829 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
3830 error
= xfs_iread_extents(NULL
, ip
, whichfork
);
3835 if (!xfs_iext_lookup_extent(ip
, ifp
, bno
, &icur
, &got
))
3840 while (bno
< end
&& n
< *nmap
) {
3841 /* Reading past eof, act as though there's a hole up to end. */
3843 got
.br_startoff
= end
;
3844 if (got
.br_startoff
> bno
) {
3845 /* Reading in a hole. */
3846 mval
->br_startoff
= bno
;
3847 mval
->br_startblock
= HOLESTARTBLOCK
;
3848 mval
->br_blockcount
=
3849 XFS_FILBLKS_MIN(len
, got
.br_startoff
- bno
);
3850 mval
->br_state
= XFS_EXT_NORM
;
3851 bno
+= mval
->br_blockcount
;
3852 len
-= mval
->br_blockcount
;
3858 /* set up the extent map to return. */
3859 xfs_bmapi_trim_map(mval
, &got
, &bno
, len
, obno
, end
, n
, flags
);
3860 xfs_bmapi_update_map(&mval
, &bno
, &len
, obno
, end
, &n
, flags
);
3862 /* If we're done, stop now. */
3863 if (bno
>= end
|| n
>= *nmap
)
3866 /* Else go on to the next record. */
3867 if (!xfs_iext_next_extent(ifp
, &icur
, &got
))
3875 * Add a delayed allocation extent to an inode. Blocks are reserved from the
3876 * global pool and the extent inserted into the inode in-core extent tree.
3878 * On entry, got refers to the first extent beyond the offset of the extent to
3879 * allocate or eof is specified if no such extent exists. On return, got refers
3880 * to the extent record that was inserted to the inode fork.
3882 * Note that the allocated extent may have been merged with contiguous extents
3883 * during insertion into the inode fork. Thus, got does not reflect the current
3884 * state of the inode fork on return. If necessary, the caller can use lastx to
3885 * look up the updated record in the inode fork.
3888 xfs_bmapi_reserve_delalloc(
3889 struct xfs_inode
*ip
,
3893 xfs_filblks_t prealloc
,
3894 struct xfs_bmbt_irec
*got
,
3895 struct xfs_iext_cursor
*icur
,
3898 struct xfs_mount
*mp
= ip
->i_mount
;
3899 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
3901 xfs_extlen_t indlen
;
3903 xfs_fileoff_t aoff
= off
;
3906 * Cap the alloc length. Keep track of prealloc so we know whether to
3907 * tag the inode before we return.
3909 alen
= XFS_FILBLKS_MIN(len
+ prealloc
, MAXEXTLEN
);
3911 alen
= XFS_FILBLKS_MIN(alen
, got
->br_startoff
- aoff
);
3912 if (prealloc
&& alen
>= len
)
3913 prealloc
= alen
- len
;
3915 /* Figure out the extent size, adjust alen */
3916 if (whichfork
== XFS_COW_FORK
) {
3917 struct xfs_bmbt_irec prev
;
3918 xfs_extlen_t extsz
= xfs_get_cowextsz_hint(ip
);
3920 if (!xfs_iext_peek_prev_extent(ifp
, icur
, &prev
))
3921 prev
.br_startoff
= NULLFILEOFF
;
3923 error
= xfs_bmap_extsize_align(mp
, got
, &prev
, extsz
, 0, eof
,
3924 1, 0, &aoff
, &alen
);
3929 * Make a transaction-less quota reservation for delayed allocation
3930 * blocks. This number gets adjusted later. We return if we haven't
3931 * allocated blocks already inside this loop.
3933 error
= xfs_trans_reserve_quota_nblks(NULL
, ip
, (long)alen
, 0,
3934 XFS_QMOPT_RES_REGBLKS
);
3939 * Split changing sb for alen and indlen since they could be coming
3940 * from different places.
3942 indlen
= (xfs_extlen_t
)xfs_bmap_worst_indlen(ip
, alen
);
3945 error
= xfs_mod_fdblocks(mp
, -((int64_t)alen
), false);
3947 goto out_unreserve_quota
;
3949 error
= xfs_mod_fdblocks(mp
, -((int64_t)indlen
), false);
3951 goto out_unreserve_blocks
;
3954 ip
->i_delayed_blks
+= alen
;
3956 got
->br_startoff
= aoff
;
3957 got
->br_startblock
= nullstartblock(indlen
);
3958 got
->br_blockcount
= alen
;
3959 got
->br_state
= XFS_EXT_NORM
;
3961 xfs_bmap_add_extent_hole_delay(ip
, whichfork
, icur
, got
);
3964 * Tag the inode if blocks were preallocated. Note that COW fork
3965 * preallocation can occur at the start or end of the extent, even when
3966 * prealloc == 0, so we must also check the aligned offset and length.
3968 if (whichfork
== XFS_DATA_FORK
&& prealloc
)
3969 xfs_inode_set_eofblocks_tag(ip
);
3970 if (whichfork
== XFS_COW_FORK
&& (prealloc
|| aoff
< off
|| alen
> len
))
3971 xfs_inode_set_cowblocks_tag(ip
);
3975 out_unreserve_blocks
:
3976 xfs_mod_fdblocks(mp
, alen
, false);
3977 out_unreserve_quota
:
3978 if (XFS_IS_QUOTA_ON(mp
))
3979 xfs_trans_unreserve_quota_nblks(NULL
, ip
, (long)alen
, 0,
3980 XFS_QMOPT_RES_REGBLKS
);
3986 struct xfs_bmalloca
*bma
)
3988 struct xfs_mount
*mp
= bma
->ip
->i_mount
;
3989 int whichfork
= xfs_bmapi_whichfork(bma
->flags
);
3990 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
3991 int tmp_logflags
= 0;
3994 ASSERT(bma
->length
> 0);
3997 * For the wasdelay case, we could also just allocate the stuff asked
3998 * for in this bmap call but that wouldn't be as good.
4001 bma
->length
= (xfs_extlen_t
)bma
->got
.br_blockcount
;
4002 bma
->offset
= bma
->got
.br_startoff
;
4003 xfs_iext_peek_prev_extent(ifp
, &bma
->icur
, &bma
->prev
);
4005 bma
->length
= XFS_FILBLKS_MIN(bma
->length
, MAXEXTLEN
);
4007 bma
->length
= XFS_FILBLKS_MIN(bma
->length
,
4008 bma
->got
.br_startoff
- bma
->offset
);
4012 * Set the data type being allocated. For the data fork, the first data
4013 * in the file is treated differently to all other allocations. For the
4014 * attribute fork, we only need to ensure the allocated range is not on
4017 if (!(bma
->flags
& XFS_BMAPI_METADATA
)) {
4018 bma
->datatype
= XFS_ALLOC_NOBUSY
;
4019 if (whichfork
== XFS_DATA_FORK
) {
4020 if (bma
->offset
== 0)
4021 bma
->datatype
|= XFS_ALLOC_INITIAL_USER_DATA
;
4023 bma
->datatype
|= XFS_ALLOC_USERDATA
;
4025 if (bma
->flags
& XFS_BMAPI_ZERO
)
4026 bma
->datatype
|= XFS_ALLOC_USERDATA_ZERO
;
4029 bma
->minlen
= (bma
->flags
& XFS_BMAPI_CONTIG
) ? bma
->length
: 1;
4032 * Only want to do the alignment at the eof if it is userdata and
4033 * allocation length is larger than a stripe unit.
4035 if (mp
->m_dalign
&& bma
->length
>= mp
->m_dalign
&&
4036 !(bma
->flags
& XFS_BMAPI_METADATA
) && whichfork
== XFS_DATA_FORK
) {
4037 error
= xfs_bmap_isaeof(bma
, whichfork
);
4042 error
= xfs_bmap_alloc(bma
);
4046 if (bma
->blkno
== NULLFSBLOCK
)
4048 if ((ifp
->if_flags
& XFS_IFBROOT
) && !bma
->cur
)
4049 bma
->cur
= xfs_bmbt_init_cursor(mp
, bma
->tp
, bma
->ip
, whichfork
);
4051 * Bump the number of extents we've allocated
4057 bma
->cur
->bc_private
.b
.flags
=
4058 bma
->wasdel
? XFS_BTCUR_BPRV_WASDEL
: 0;
4060 bma
->got
.br_startoff
= bma
->offset
;
4061 bma
->got
.br_startblock
= bma
->blkno
;
4062 bma
->got
.br_blockcount
= bma
->length
;
4063 bma
->got
.br_state
= XFS_EXT_NORM
;
4066 * In the data fork, a wasdelay extent has been initialized, so
4067 * shouldn't be flagged as unwritten.
4069 * For the cow fork, however, we convert delalloc reservations
4070 * (extents allocated for speculative preallocation) to
4071 * allocated unwritten extents, and only convert the unwritten
4072 * extents to real extents when we're about to write the data.
4074 if ((!bma
->wasdel
|| (bma
->flags
& XFS_BMAPI_COWFORK
)) &&
4075 (bma
->flags
& XFS_BMAPI_PREALLOC
))
4076 bma
->got
.br_state
= XFS_EXT_UNWRITTEN
;
4079 error
= xfs_bmap_add_extent_delay_real(bma
, whichfork
);
4081 error
= xfs_bmap_add_extent_hole_real(bma
->tp
, bma
->ip
,
4082 whichfork
, &bma
->icur
, &bma
->cur
, &bma
->got
,
4083 &bma
->logflags
, bma
->flags
);
4085 bma
->logflags
|= tmp_logflags
;
4090 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4091 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4092 * the neighbouring ones.
4094 xfs_iext_get_extent(ifp
, &bma
->icur
, &bma
->got
);
4096 ASSERT(bma
->got
.br_startoff
<= bma
->offset
);
4097 ASSERT(bma
->got
.br_startoff
+ bma
->got
.br_blockcount
>=
4098 bma
->offset
+ bma
->length
);
4099 ASSERT(bma
->got
.br_state
== XFS_EXT_NORM
||
4100 bma
->got
.br_state
== XFS_EXT_UNWRITTEN
);
4105 xfs_bmapi_convert_unwritten(
4106 struct xfs_bmalloca
*bma
,
4107 struct xfs_bmbt_irec
*mval
,
4111 int whichfork
= xfs_bmapi_whichfork(flags
);
4112 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
4113 int tmp_logflags
= 0;
4116 /* check if we need to do unwritten->real conversion */
4117 if (mval
->br_state
== XFS_EXT_UNWRITTEN
&&
4118 (flags
& XFS_BMAPI_PREALLOC
))
4121 /* check if we need to do real->unwritten conversion */
4122 if (mval
->br_state
== XFS_EXT_NORM
&&
4123 (flags
& (XFS_BMAPI_PREALLOC
| XFS_BMAPI_CONVERT
)) !=
4124 (XFS_BMAPI_PREALLOC
| XFS_BMAPI_CONVERT
))
4128 * Modify (by adding) the state flag, if writing.
4130 ASSERT(mval
->br_blockcount
<= len
);
4131 if ((ifp
->if_flags
& XFS_IFBROOT
) && !bma
->cur
) {
4132 bma
->cur
= xfs_bmbt_init_cursor(bma
->ip
->i_mount
, bma
->tp
,
4133 bma
->ip
, whichfork
);
4135 mval
->br_state
= (mval
->br_state
== XFS_EXT_UNWRITTEN
)
4136 ? XFS_EXT_NORM
: XFS_EXT_UNWRITTEN
;
4139 * Before insertion into the bmbt, zero the range being converted
4142 if (flags
& XFS_BMAPI_ZERO
) {
4143 error
= xfs_zero_extent(bma
->ip
, mval
->br_startblock
,
4144 mval
->br_blockcount
);
4149 error
= xfs_bmap_add_extent_unwritten_real(bma
->tp
, bma
->ip
, whichfork
,
4150 &bma
->icur
, &bma
->cur
, mval
, &tmp_logflags
);
4152 * Log the inode core unconditionally in the unwritten extent conversion
4153 * path because the conversion might not have done so (e.g., if the
4154 * extent count hasn't changed). We need to make sure the inode is dirty
4155 * in the transaction for the sake of fsync(), even if nothing has
4156 * changed, because fsync() will not force the log for this transaction
4157 * unless it sees the inode pinned.
4159 * Note: If we're only converting cow fork extents, there aren't
4160 * any on-disk updates to make, so we don't need to log anything.
4162 if (whichfork
!= XFS_COW_FORK
)
4163 bma
->logflags
|= tmp_logflags
| XFS_ILOG_CORE
;
4168 * Update our extent pointer, given that
4169 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4170 * of the neighbouring ones.
4172 xfs_iext_get_extent(ifp
, &bma
->icur
, &bma
->got
);
4175 * We may have combined previously unwritten space with written space,
4176 * so generate another request.
4178 if (mval
->br_blockcount
< len
)
4184 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4185 * extent state if necessary. Details behaviour is controlled by the flags
4186 * parameter. Only allocates blocks from a single allocation group, to avoid
4191 struct xfs_trans
*tp
, /* transaction pointer */
4192 struct xfs_inode
*ip
, /* incore inode */
4193 xfs_fileoff_t bno
, /* starting file offs. mapped */
4194 xfs_filblks_t len
, /* length to map in file */
4195 int flags
, /* XFS_BMAPI_... */
4196 xfs_extlen_t total
, /* total blocks needed */
4197 struct xfs_bmbt_irec
*mval
, /* output: map values */
4198 int *nmap
) /* i/o: mval size/count */
4200 struct xfs_mount
*mp
= ip
->i_mount
;
4201 struct xfs_ifork
*ifp
;
4202 struct xfs_bmalloca bma
= { NULL
}; /* args for xfs_bmap_alloc */
4203 xfs_fileoff_t end
; /* end of mapped file region */
4204 bool eof
= false; /* after the end of extents */
4205 int error
; /* error return */
4206 int n
; /* current extent index */
4207 xfs_fileoff_t obno
; /* old block number (offset) */
4208 int whichfork
; /* data or attr fork */
4211 xfs_fileoff_t orig_bno
; /* original block number value */
4212 int orig_flags
; /* original flags arg value */
4213 xfs_filblks_t orig_len
; /* original value of len arg */
4214 struct xfs_bmbt_irec
*orig_mval
; /* original value of mval */
4215 int orig_nmap
; /* original value of *nmap */
4223 whichfork
= xfs_bmapi_whichfork(flags
);
4226 ASSERT(*nmap
<= XFS_BMAP_MAX_NMAP
);
4227 ASSERT(tp
!= NULL
||
4228 (flags
& (XFS_BMAPI_CONVERT
| XFS_BMAPI_COWFORK
)) ==
4229 (XFS_BMAPI_CONVERT
| XFS_BMAPI_COWFORK
));
4231 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_LOCAL
);
4232 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
4233 ASSERT(!(flags
& XFS_BMAPI_REMAP
));
4235 /* zeroing is for currently only for data extents, not metadata */
4236 ASSERT((flags
& (XFS_BMAPI_METADATA
| XFS_BMAPI_ZERO
)) !=
4237 (XFS_BMAPI_METADATA
| XFS_BMAPI_ZERO
));
4239 * we can allocate unwritten extents or pre-zero allocated blocks,
4240 * but it makes no sense to do both at once. This would result in
4241 * zeroing the unwritten extent twice, but it still being an
4242 * unwritten extent....
4244 ASSERT((flags
& (XFS_BMAPI_PREALLOC
| XFS_BMAPI_ZERO
)) !=
4245 (XFS_BMAPI_PREALLOC
| XFS_BMAPI_ZERO
));
4247 if (unlikely(XFS_TEST_ERROR(
4248 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
4249 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
4250 mp
, XFS_ERRTAG_BMAPIFORMAT
))) {
4251 XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW
, mp
);
4252 return -EFSCORRUPTED
;
4255 if (XFS_FORCED_SHUTDOWN(mp
))
4258 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4260 XFS_STATS_INC(mp
, xs_blk_mapw
);
4262 if (!tp
|| tp
->t_firstblock
== NULLFSBLOCK
) {
4263 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
)
4264 bma
.minleft
= be16_to_cpu(ifp
->if_broot
->bb_level
) + 1;
4271 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
4272 error
= xfs_iread_extents(tp
, ip
, whichfork
);
4281 if (!xfs_iext_lookup_extent(ip
, ifp
, bno
, &bma
.icur
, &bma
.got
))
4283 if (!xfs_iext_peek_prev_extent(ifp
, &bma
.icur
, &bma
.prev
))
4284 bma
.prev
.br_startoff
= NULLFILEOFF
;
4290 while (bno
< end
&& n
< *nmap
) {
4291 bool need_alloc
= false, wasdelay
= false;
4293 /* in hole or beyond EOF? */
4294 if (eof
|| bma
.got
.br_startoff
> bno
) {
4296 * CoW fork conversions should /never/ hit EOF or
4297 * holes. There should always be something for us
4300 ASSERT(!((flags
& XFS_BMAPI_CONVERT
) &&
4301 (flags
& XFS_BMAPI_COWFORK
)));
4303 if (flags
& XFS_BMAPI_DELALLOC
) {
4305 * For the COW fork we can reasonably get a
4306 * request for converting an extent that races
4307 * with other threads already having converted
4308 * part of it, as there converting COW to
4309 * regular blocks is not protected using the
4312 ASSERT(flags
& XFS_BMAPI_COWFORK
);
4313 if (!(flags
& XFS_BMAPI_COWFORK
)) {
4318 if (eof
|| bno
>= end
)
4323 } else if (isnullstartblock(bma
.got
.br_startblock
)) {
4328 * First, deal with the hole before the allocated space
4329 * that we found, if any.
4331 if ((need_alloc
|| wasdelay
) &&
4332 !(flags
& XFS_BMAPI_CONVERT_ONLY
)) {
4334 bma
.conv
= !!(flags
& XFS_BMAPI_CONVERT
);
4335 bma
.wasdel
= wasdelay
;
4340 * There's a 32/64 bit type mismatch between the
4341 * allocation length request (which can be 64 bits in
4342 * length) and the bma length request, which is
4343 * xfs_extlen_t and therefore 32 bits. Hence we have to
4344 * check for 32-bit overflows and handle them here.
4346 if (len
> (xfs_filblks_t
)MAXEXTLEN
)
4347 bma
.length
= MAXEXTLEN
;
4352 ASSERT(bma
.length
> 0);
4353 error
= xfs_bmapi_allocate(&bma
);
4356 if (bma
.blkno
== NULLFSBLOCK
)
4360 * If this is a CoW allocation, record the data in
4361 * the refcount btree for orphan recovery.
4363 if (whichfork
== XFS_COW_FORK
) {
4364 error
= xfs_refcount_alloc_cow_extent(tp
,
4365 bma
.blkno
, bma
.length
);
4371 /* Deal with the allocated space we found. */
4372 xfs_bmapi_trim_map(mval
, &bma
.got
, &bno
, len
, obno
,
4375 /* Execute unwritten extent conversion if necessary */
4376 error
= xfs_bmapi_convert_unwritten(&bma
, mval
, len
, flags
);
4377 if (error
== -EAGAIN
)
4382 /* update the extent map to return */
4383 xfs_bmapi_update_map(&mval
, &bno
, &len
, obno
, end
, &n
, flags
);
4386 * If we're done, stop now. Stop when we've allocated
4387 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4388 * the transaction may get too big.
4390 if (bno
>= end
|| n
>= *nmap
|| bma
.nallocs
>= *nmap
)
4393 /* Else go on to the next record. */
4395 if (!xfs_iext_next_extent(ifp
, &bma
.icur
, &bma
.got
))
4401 * Transform from btree to extents, give it cur.
4403 if (xfs_bmap_wants_extents(ip
, whichfork
)) {
4404 int tmp_logflags
= 0;
4407 error
= xfs_bmap_btree_to_extents(tp
, ip
, bma
.cur
,
4408 &tmp_logflags
, whichfork
);
4409 bma
.logflags
|= tmp_logflags
;
4414 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
||
4415 XFS_IFORK_NEXTENTS(ip
, whichfork
) >
4416 XFS_IFORK_MAXEXT(ip
, whichfork
));
4420 * Log everything. Do this after conversion, there's no point in
4421 * logging the extent records if we've converted to btree format.
4423 if ((bma
.logflags
& xfs_ilog_fext(whichfork
)) &&
4424 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
4425 bma
.logflags
&= ~xfs_ilog_fext(whichfork
);
4426 else if ((bma
.logflags
& xfs_ilog_fbroot(whichfork
)) &&
4427 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)
4428 bma
.logflags
&= ~xfs_ilog_fbroot(whichfork
);
4430 * Log whatever the flags say, even if error. Otherwise we might miss
4431 * detecting a case where the data is changed, there's an error,
4432 * and it's not logged so we don't shutdown when we should.
4435 xfs_trans_log_inode(tp
, ip
, bma
.logflags
);
4438 xfs_btree_del_cursor(bma
.cur
, error
);
4441 xfs_bmap_validate_ret(orig_bno
, orig_len
, orig_flags
, orig_mval
,
4448 struct xfs_trans
*tp
,
4449 struct xfs_inode
*ip
,
4452 xfs_fsblock_t startblock
,
4455 struct xfs_mount
*mp
= ip
->i_mount
;
4456 struct xfs_ifork
*ifp
;
4457 struct xfs_btree_cur
*cur
= NULL
;
4458 struct xfs_bmbt_irec got
;
4459 struct xfs_iext_cursor icur
;
4460 int whichfork
= xfs_bmapi_whichfork(flags
);
4461 int logflags
= 0, error
;
4463 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4465 ASSERT(len
<= (xfs_filblks_t
)MAXEXTLEN
);
4466 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
4467 ASSERT(!(flags
& ~(XFS_BMAPI_ATTRFORK
| XFS_BMAPI_PREALLOC
|
4468 XFS_BMAPI_NORMAP
)));
4469 ASSERT((flags
& (XFS_BMAPI_ATTRFORK
| XFS_BMAPI_PREALLOC
)) !=
4470 (XFS_BMAPI_ATTRFORK
| XFS_BMAPI_PREALLOC
));
4472 if (unlikely(XFS_TEST_ERROR(
4473 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
4474 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
4475 mp
, XFS_ERRTAG_BMAPIFORMAT
))) {
4476 XFS_ERROR_REPORT("xfs_bmapi_remap", XFS_ERRLEVEL_LOW
, mp
);
4477 return -EFSCORRUPTED
;
4480 if (XFS_FORCED_SHUTDOWN(mp
))
4483 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
4484 error
= xfs_iread_extents(tp
, ip
, whichfork
);
4489 if (xfs_iext_lookup_extent(ip
, ifp
, bno
, &icur
, &got
)) {
4490 /* make sure we only reflink into a hole. */
4491 ASSERT(got
.br_startoff
> bno
);
4492 ASSERT(got
.br_startoff
- bno
>= len
);
4495 ip
->i_d
.di_nblocks
+= len
;
4496 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
4498 if (ifp
->if_flags
& XFS_IFBROOT
) {
4499 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
4500 cur
->bc_private
.b
.flags
= 0;
4503 got
.br_startoff
= bno
;
4504 got
.br_startblock
= startblock
;
4505 got
.br_blockcount
= len
;
4506 if (flags
& XFS_BMAPI_PREALLOC
)
4507 got
.br_state
= XFS_EXT_UNWRITTEN
;
4509 got
.br_state
= XFS_EXT_NORM
;
4511 error
= xfs_bmap_add_extent_hole_real(tp
, ip
, whichfork
, &icur
,
4512 &cur
, &got
, &logflags
, flags
);
4516 if (xfs_bmap_wants_extents(ip
, whichfork
)) {
4517 int tmp_logflags
= 0;
4519 error
= xfs_bmap_btree_to_extents(tp
, ip
, cur
,
4520 &tmp_logflags
, whichfork
);
4521 logflags
|= tmp_logflags
;
4525 if (ip
->i_d
.di_format
!= XFS_DINODE_FMT_EXTENTS
)
4526 logflags
&= ~XFS_ILOG_DEXT
;
4527 else if (ip
->i_d
.di_format
!= XFS_DINODE_FMT_BTREE
)
4528 logflags
&= ~XFS_ILOG_DBROOT
;
4531 xfs_trans_log_inode(tp
, ip
, logflags
);
4533 xfs_btree_del_cursor(cur
, error
);
4538 * When a delalloc extent is split (e.g., due to a hole punch), the original
4539 * indlen reservation must be shared across the two new extents that are left
4542 * Given the original reservation and the worst case indlen for the two new
4543 * extents (as calculated by xfs_bmap_worst_indlen()), split the original
4544 * reservation fairly across the two new extents. If necessary, steal available
4545 * blocks from a deleted extent to make up a reservation deficiency (e.g., if
4546 * ores == 1). The number of stolen blocks is returned. The availability and
4547 * subsequent accounting of stolen blocks is the responsibility of the caller.
4549 static xfs_filblks_t
4550 xfs_bmap_split_indlen(
4551 xfs_filblks_t ores
, /* original res. */
4552 xfs_filblks_t
*indlen1
, /* ext1 worst indlen */
4553 xfs_filblks_t
*indlen2
, /* ext2 worst indlen */
4554 xfs_filblks_t avail
) /* stealable blocks */
4556 xfs_filblks_t len1
= *indlen1
;
4557 xfs_filblks_t len2
= *indlen2
;
4558 xfs_filblks_t nres
= len1
+ len2
; /* new total res. */
4559 xfs_filblks_t stolen
= 0;
4560 xfs_filblks_t resfactor
;
4563 * Steal as many blocks as we can to try and satisfy the worst case
4564 * indlen for both new extents.
4566 if (ores
< nres
&& avail
)
4567 stolen
= XFS_FILBLKS_MIN(nres
- ores
, avail
);
4570 /* nothing else to do if we've satisfied the new reservation */
4575 * We can't meet the total required reservation for the two extents.
4576 * Calculate the percent of the overall shortage between both extents
4577 * and apply this percentage to each of the requested indlen values.
4578 * This distributes the shortage fairly and reduces the chances that one
4579 * of the two extents is left with nothing when extents are repeatedly
4582 resfactor
= (ores
* 100);
4583 do_div(resfactor
, nres
);
4588 ASSERT(len1
+ len2
<= ores
);
4589 ASSERT(len1
< *indlen1
&& len2
< *indlen2
);
4592 * Hand out the remainder to each extent. If one of the two reservations
4593 * is zero, we want to make sure that one gets a block first. The loop
4594 * below starts with len1, so hand len2 a block right off the bat if it
4597 ores
-= (len1
+ len2
);
4598 ASSERT((*indlen1
- len1
) + (*indlen2
- len2
) >= ores
);
4599 if (ores
&& !len2
&& *indlen2
) {
4604 if (len1
< *indlen1
) {
4610 if (len2
< *indlen2
) {
4623 xfs_bmap_del_extent_delay(
4624 struct xfs_inode
*ip
,
4626 struct xfs_iext_cursor
*icur
,
4627 struct xfs_bmbt_irec
*got
,
4628 struct xfs_bmbt_irec
*del
)
4630 struct xfs_mount
*mp
= ip
->i_mount
;
4631 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4632 struct xfs_bmbt_irec
new;
4633 int64_t da_old
, da_new
, da_diff
= 0;
4634 xfs_fileoff_t del_endoff
, got_endoff
;
4635 xfs_filblks_t got_indlen
, new_indlen
, stolen
;
4636 int state
= xfs_bmap_fork_to_state(whichfork
);
4640 XFS_STATS_INC(mp
, xs_del_exlist
);
4642 isrt
= (whichfork
== XFS_DATA_FORK
) && XFS_IS_REALTIME_INODE(ip
);
4643 del_endoff
= del
->br_startoff
+ del
->br_blockcount
;
4644 got_endoff
= got
->br_startoff
+ got
->br_blockcount
;
4645 da_old
= startblockval(got
->br_startblock
);
4648 ASSERT(del
->br_blockcount
> 0);
4649 ASSERT(got
->br_startoff
<= del
->br_startoff
);
4650 ASSERT(got_endoff
>= del_endoff
);
4653 uint64_t rtexts
= XFS_FSB_TO_B(mp
, del
->br_blockcount
);
4655 do_div(rtexts
, mp
->m_sb
.sb_rextsize
);
4656 xfs_mod_frextents(mp
, rtexts
);
4660 * Update the inode delalloc counter now and wait to update the
4661 * sb counters as we might have to borrow some blocks for the
4662 * indirect block accounting.
4664 error
= xfs_trans_reserve_quota_nblks(NULL
, ip
,
4665 -((long)del
->br_blockcount
), 0,
4666 isrt
? XFS_QMOPT_RES_RTBLKS
: XFS_QMOPT_RES_REGBLKS
);
4669 ip
->i_delayed_blks
-= del
->br_blockcount
;
4671 if (got
->br_startoff
== del
->br_startoff
)
4672 state
|= BMAP_LEFT_FILLING
;
4673 if (got_endoff
== del_endoff
)
4674 state
|= BMAP_RIGHT_FILLING
;
4676 switch (state
& (BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
)) {
4677 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
4679 * Matches the whole extent. Delete the entry.
4681 xfs_iext_remove(ip
, icur
, state
);
4682 xfs_iext_prev(ifp
, icur
);
4684 case BMAP_LEFT_FILLING
:
4686 * Deleting the first part of the extent.
4688 got
->br_startoff
= del_endoff
;
4689 got
->br_blockcount
-= del
->br_blockcount
;
4690 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
,
4691 got
->br_blockcount
), da_old
);
4692 got
->br_startblock
= nullstartblock((int)da_new
);
4693 xfs_iext_update_extent(ip
, state
, icur
, got
);
4695 case BMAP_RIGHT_FILLING
:
4697 * Deleting the last part of the extent.
4699 got
->br_blockcount
= got
->br_blockcount
- del
->br_blockcount
;
4700 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
,
4701 got
->br_blockcount
), da_old
);
4702 got
->br_startblock
= nullstartblock((int)da_new
);
4703 xfs_iext_update_extent(ip
, state
, icur
, got
);
4707 * Deleting the middle of the extent.
4709 * Distribute the original indlen reservation across the two new
4710 * extents. Steal blocks from the deleted extent if necessary.
4711 * Stealing blocks simply fudges the fdblocks accounting below.
4712 * Warn if either of the new indlen reservations is zero as this
4713 * can lead to delalloc problems.
4715 got
->br_blockcount
= del
->br_startoff
- got
->br_startoff
;
4716 got_indlen
= xfs_bmap_worst_indlen(ip
, got
->br_blockcount
);
4718 new.br_blockcount
= got_endoff
- del_endoff
;
4719 new_indlen
= xfs_bmap_worst_indlen(ip
, new.br_blockcount
);
4721 WARN_ON_ONCE(!got_indlen
|| !new_indlen
);
4722 stolen
= xfs_bmap_split_indlen(da_old
, &got_indlen
, &new_indlen
,
4723 del
->br_blockcount
);
4725 got
->br_startblock
= nullstartblock((int)got_indlen
);
4727 new.br_startoff
= del_endoff
;
4728 new.br_state
= got
->br_state
;
4729 new.br_startblock
= nullstartblock((int)new_indlen
);
4731 xfs_iext_update_extent(ip
, state
, icur
, got
);
4732 xfs_iext_next(ifp
, icur
);
4733 xfs_iext_insert(ip
, icur
, &new, state
);
4735 da_new
= got_indlen
+ new_indlen
- stolen
;
4736 del
->br_blockcount
-= stolen
;
4740 ASSERT(da_old
>= da_new
);
4741 da_diff
= da_old
- da_new
;
4743 da_diff
+= del
->br_blockcount
;
4745 xfs_mod_fdblocks(mp
, da_diff
, false);
4750 xfs_bmap_del_extent_cow(
4751 struct xfs_inode
*ip
,
4752 struct xfs_iext_cursor
*icur
,
4753 struct xfs_bmbt_irec
*got
,
4754 struct xfs_bmbt_irec
*del
)
4756 struct xfs_mount
*mp
= ip
->i_mount
;
4757 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, XFS_COW_FORK
);
4758 struct xfs_bmbt_irec
new;
4759 xfs_fileoff_t del_endoff
, got_endoff
;
4760 int state
= BMAP_COWFORK
;
4762 XFS_STATS_INC(mp
, xs_del_exlist
);
4764 del_endoff
= del
->br_startoff
+ del
->br_blockcount
;
4765 got_endoff
= got
->br_startoff
+ got
->br_blockcount
;
4767 ASSERT(del
->br_blockcount
> 0);
4768 ASSERT(got
->br_startoff
<= del
->br_startoff
);
4769 ASSERT(got_endoff
>= del_endoff
);
4770 ASSERT(!isnullstartblock(got
->br_startblock
));
4772 if (got
->br_startoff
== del
->br_startoff
)
4773 state
|= BMAP_LEFT_FILLING
;
4774 if (got_endoff
== del_endoff
)
4775 state
|= BMAP_RIGHT_FILLING
;
4777 switch (state
& (BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
)) {
4778 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
4780 * Matches the whole extent. Delete the entry.
4782 xfs_iext_remove(ip
, icur
, state
);
4783 xfs_iext_prev(ifp
, icur
);
4785 case BMAP_LEFT_FILLING
:
4787 * Deleting the first part of the extent.
4789 got
->br_startoff
= del_endoff
;
4790 got
->br_blockcount
-= del
->br_blockcount
;
4791 got
->br_startblock
= del
->br_startblock
+ del
->br_blockcount
;
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
-= del
->br_blockcount
;
4799 xfs_iext_update_extent(ip
, state
, icur
, got
);
4803 * Deleting the middle of the extent.
4805 got
->br_blockcount
= del
->br_startoff
- got
->br_startoff
;
4807 new.br_startoff
= del_endoff
;
4808 new.br_blockcount
= got_endoff
- del_endoff
;
4809 new.br_state
= got
->br_state
;
4810 new.br_startblock
= del
->br_startblock
+ del
->br_blockcount
;
4812 xfs_iext_update_extent(ip
, state
, icur
, got
);
4813 xfs_iext_next(ifp
, icur
);
4814 xfs_iext_insert(ip
, icur
, &new, state
);
4817 ip
->i_delayed_blks
-= del
->br_blockcount
;
4821 * Called by xfs_bmapi to update file extent records and the btree
4822 * after removing space.
4824 STATIC
int /* error */
4825 xfs_bmap_del_extent_real(
4826 xfs_inode_t
*ip
, /* incore inode pointer */
4827 xfs_trans_t
*tp
, /* current transaction pointer */
4828 struct xfs_iext_cursor
*icur
,
4829 xfs_btree_cur_t
*cur
, /* if null, not a btree */
4830 xfs_bmbt_irec_t
*del
, /* data to remove from extents */
4831 int *logflagsp
, /* inode logging flags */
4832 int whichfork
, /* data or attr fork */
4833 int bflags
) /* bmapi flags */
4835 xfs_fsblock_t del_endblock
=0; /* first block past del */
4836 xfs_fileoff_t del_endoff
; /* first offset past del */
4837 int do_fx
; /* free extent at end of routine */
4838 int error
; /* error return value */
4839 int flags
= 0;/* inode logging flags */
4840 struct xfs_bmbt_irec got
; /* current extent entry */
4841 xfs_fileoff_t got_endoff
; /* first offset past got */
4842 int i
; /* temp state */
4843 struct xfs_ifork
*ifp
; /* inode fork pointer */
4844 xfs_mount_t
*mp
; /* mount structure */
4845 xfs_filblks_t nblks
; /* quota/sb block count */
4846 xfs_bmbt_irec_t
new; /* new record to be inserted */
4848 uint qfield
; /* quota field to update */
4849 int state
= xfs_bmap_fork_to_state(whichfork
);
4850 struct xfs_bmbt_irec old
;
4853 XFS_STATS_INC(mp
, xs_del_exlist
);
4855 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4856 ASSERT(del
->br_blockcount
> 0);
4857 xfs_iext_get_extent(ifp
, icur
, &got
);
4858 ASSERT(got
.br_startoff
<= del
->br_startoff
);
4859 del_endoff
= del
->br_startoff
+ del
->br_blockcount
;
4860 got_endoff
= got
.br_startoff
+ got
.br_blockcount
;
4861 ASSERT(got_endoff
>= del_endoff
);
4862 ASSERT(!isnullstartblock(got
.br_startblock
));
4867 * If it's the case where the directory code is running with no block
4868 * reservation, and the deleted block is in the middle of its extent,
4869 * and the resulting insert of an extent would cause transformation to
4870 * btree format, then reject it. The calling code will then swap blocks
4871 * around instead. We have to do this now, rather than waiting for the
4872 * conversion to btree format, since the transaction will be dirty then.
4874 if (tp
->t_blk_res
== 0 &&
4875 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
&&
4876 XFS_IFORK_NEXTENTS(ip
, whichfork
) >=
4877 XFS_IFORK_MAXEXT(ip
, whichfork
) &&
4878 del
->br_startoff
> got
.br_startoff
&& del_endoff
< got_endoff
)
4881 flags
= XFS_ILOG_CORE
;
4882 if (whichfork
== XFS_DATA_FORK
&& XFS_IS_REALTIME_INODE(ip
)) {
4887 bno
= div_u64_rem(del
->br_startblock
, mp
->m_sb
.sb_rextsize
,
4890 len
= div_u64_rem(del
->br_blockcount
, mp
->m_sb
.sb_rextsize
,
4894 error
= xfs_rtfree_extent(tp
, bno
, (xfs_extlen_t
)len
);
4898 nblks
= len
* mp
->m_sb
.sb_rextsize
;
4899 qfield
= XFS_TRANS_DQ_RTBCOUNT
;
4902 nblks
= del
->br_blockcount
;
4903 qfield
= XFS_TRANS_DQ_BCOUNT
;
4906 del_endblock
= del
->br_startblock
+ del
->br_blockcount
;
4908 error
= xfs_bmbt_lookup_eq(cur
, &got
, &i
);
4911 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
4914 if (got
.br_startoff
== del
->br_startoff
)
4915 state
|= BMAP_LEFT_FILLING
;
4916 if (got_endoff
== del_endoff
)
4917 state
|= BMAP_RIGHT_FILLING
;
4919 switch (state
& (BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
)) {
4920 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
4922 * Matches the whole extent. Delete the entry.
4924 xfs_iext_remove(ip
, icur
, state
);
4925 xfs_iext_prev(ifp
, icur
);
4926 XFS_IFORK_NEXT_SET(ip
, whichfork
,
4927 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
4928 flags
|= XFS_ILOG_CORE
;
4930 flags
|= xfs_ilog_fext(whichfork
);
4933 if ((error
= xfs_btree_delete(cur
, &i
)))
4935 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
4937 case BMAP_LEFT_FILLING
:
4939 * Deleting the first part of the extent.
4941 got
.br_startoff
= del_endoff
;
4942 got
.br_startblock
= del_endblock
;
4943 got
.br_blockcount
-= del
->br_blockcount
;
4944 xfs_iext_update_extent(ip
, state
, icur
, &got
);
4946 flags
|= xfs_ilog_fext(whichfork
);
4949 error
= xfs_bmbt_update(cur
, &got
);
4953 case BMAP_RIGHT_FILLING
:
4955 * Deleting the last part of the extent.
4957 got
.br_blockcount
-= del
->br_blockcount
;
4958 xfs_iext_update_extent(ip
, state
, icur
, &got
);
4960 flags
|= xfs_ilog_fext(whichfork
);
4963 error
= xfs_bmbt_update(cur
, &got
);
4969 * Deleting the middle of the extent.
4973 got
.br_blockcount
= del
->br_startoff
- got
.br_startoff
;
4974 xfs_iext_update_extent(ip
, state
, icur
, &got
);
4976 new.br_startoff
= del_endoff
;
4977 new.br_blockcount
= got_endoff
- del_endoff
;
4978 new.br_state
= got
.br_state
;
4979 new.br_startblock
= del_endblock
;
4981 flags
|= XFS_ILOG_CORE
;
4983 error
= xfs_bmbt_update(cur
, &got
);
4986 error
= xfs_btree_increment(cur
, 0, &i
);
4989 cur
->bc_rec
.b
= new;
4990 error
= xfs_btree_insert(cur
, &i
);
4991 if (error
&& error
!= -ENOSPC
)
4994 * If get no-space back from btree insert, it tried a
4995 * split, and we have a zero block reservation. Fix up
4996 * our state and return the error.
4998 if (error
== -ENOSPC
) {
5000 * Reset the cursor, don't trust it after any
5003 error
= xfs_bmbt_lookup_eq(cur
, &got
, &i
);
5006 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
5008 * Update the btree record back
5009 * to the original value.
5011 error
= xfs_bmbt_update(cur
, &old
);
5015 * Reset the extent record back
5016 * to the original value.
5018 xfs_iext_update_extent(ip
, state
, icur
, &old
);
5023 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
5025 flags
|= xfs_ilog_fext(whichfork
);
5026 XFS_IFORK_NEXT_SET(ip
, whichfork
,
5027 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
5028 xfs_iext_next(ifp
, icur
);
5029 xfs_iext_insert(ip
, icur
, &new, state
);
5033 /* remove reverse mapping */
5034 error
= xfs_rmap_unmap_extent(tp
, ip
, whichfork
, del
);
5039 * If we need to, add to list of extents to delete.
5041 if (do_fx
&& !(bflags
& XFS_BMAPI_REMAP
)) {
5042 if (xfs_is_reflink_inode(ip
) && whichfork
== XFS_DATA_FORK
) {
5043 error
= xfs_refcount_decrease_extent(tp
, del
);
5047 __xfs_bmap_add_free(tp
, del
->br_startblock
,
5048 del
->br_blockcount
, NULL
,
5049 (bflags
& XFS_BMAPI_NODISCARD
) ||
5050 del
->br_state
== XFS_EXT_UNWRITTEN
);
5055 * Adjust inode # blocks in the file.
5058 ip
->i_d
.di_nblocks
-= nblks
;
5060 * Adjust quota data.
5062 if (qfield
&& !(bflags
& XFS_BMAPI_REMAP
))
5063 xfs_trans_mod_dquot_byino(tp
, ip
, qfield
, (long)-nblks
);
5071 * Unmap (remove) blocks from a file.
5072 * If nexts is nonzero then the number of extents to remove is limited to
5073 * that value. If not all extents in the block range can be removed then
5078 struct xfs_trans
*tp
, /* transaction pointer */
5079 struct xfs_inode
*ip
, /* incore inode */
5080 xfs_fileoff_t start
, /* first file offset deleted */
5081 xfs_filblks_t
*rlen
, /* i/o: amount remaining */
5082 int flags
, /* misc flags */
5083 xfs_extnum_t nexts
) /* number of extents max */
5085 struct xfs_btree_cur
*cur
; /* bmap btree cursor */
5086 struct xfs_bmbt_irec del
; /* extent being deleted */
5087 int error
; /* error return value */
5088 xfs_extnum_t extno
; /* extent number in list */
5089 struct xfs_bmbt_irec got
; /* current extent record */
5090 struct xfs_ifork
*ifp
; /* inode fork pointer */
5091 int isrt
; /* freeing in rt area */
5092 int logflags
; /* transaction logging flags */
5093 xfs_extlen_t mod
; /* rt extent offset */
5094 struct xfs_mount
*mp
; /* mount structure */
5095 int tmp_logflags
; /* partial logging flags */
5096 int wasdel
; /* was a delayed alloc extent */
5097 int whichfork
; /* data or attribute fork */
5099 xfs_filblks_t len
= *rlen
; /* length to unmap in file */
5100 xfs_fileoff_t max_len
;
5101 xfs_agnumber_t prev_agno
= NULLAGNUMBER
, agno
;
5103 struct xfs_iext_cursor icur
;
5106 trace_xfs_bunmap(ip
, start
, len
, flags
, _RET_IP_
);
5108 whichfork
= xfs_bmapi_whichfork(flags
);
5109 ASSERT(whichfork
!= XFS_COW_FORK
);
5110 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5112 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
5113 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)) {
5114 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW
,
5116 return -EFSCORRUPTED
;
5119 if (XFS_FORCED_SHUTDOWN(mp
))
5122 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
5127 * Guesstimate how many blocks we can unmap without running the risk of
5128 * blowing out the transaction with a mix of EFIs and reflink
5131 if (tp
&& xfs_is_reflink_inode(ip
) && whichfork
== XFS_DATA_FORK
)
5132 max_len
= min(len
, xfs_refcount_max_unmap(tp
->t_log_res
));
5136 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
5137 (error
= xfs_iread_extents(tp
, ip
, whichfork
)))
5139 if (xfs_iext_count(ifp
) == 0) {
5143 XFS_STATS_INC(mp
, xs_blk_unmap
);
5144 isrt
= (whichfork
== XFS_DATA_FORK
) && XFS_IS_REALTIME_INODE(ip
);
5147 if (!xfs_iext_lookup_extent_before(ip
, ifp
, &end
, &icur
, &got
)) {
5154 if (ifp
->if_flags
& XFS_IFBROOT
) {
5155 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
);
5156 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
5157 cur
->bc_private
.b
.flags
= 0;
5163 * Synchronize by locking the bitmap inode.
5165 xfs_ilock(mp
->m_rbmip
, XFS_ILOCK_EXCL
|XFS_ILOCK_RTBITMAP
);
5166 xfs_trans_ijoin(tp
, mp
->m_rbmip
, XFS_ILOCK_EXCL
);
5167 xfs_ilock(mp
->m_rsumip
, XFS_ILOCK_EXCL
|XFS_ILOCK_RTSUM
);
5168 xfs_trans_ijoin(tp
, mp
->m_rsumip
, XFS_ILOCK_EXCL
);
5172 while (end
!= (xfs_fileoff_t
)-1 && end
>= start
&&
5173 (nexts
== 0 || extno
< nexts
) && max_len
> 0) {
5175 * Is the found extent after a hole in which end lives?
5176 * Just back up to the previous extent, if so.
5178 if (got
.br_startoff
> end
&&
5179 !xfs_iext_prev_extent(ifp
, &icur
, &got
)) {
5184 * Is the last block of this extent before the range
5185 * we're supposed to delete? If so, we're done.
5187 end
= XFS_FILEOFF_MIN(end
,
5188 got
.br_startoff
+ got
.br_blockcount
- 1);
5192 * Then deal with the (possibly delayed) allocated space
5196 wasdel
= isnullstartblock(del
.br_startblock
);
5199 * Make sure we don't touch multiple AGF headers out of order
5200 * in a single transaction, as that could cause AB-BA deadlocks.
5203 agno
= XFS_FSB_TO_AGNO(mp
, del
.br_startblock
);
5204 if (prev_agno
!= NULLAGNUMBER
&& prev_agno
> agno
)
5208 if (got
.br_startoff
< start
) {
5209 del
.br_startoff
= start
;
5210 del
.br_blockcount
-= start
- got
.br_startoff
;
5212 del
.br_startblock
+= start
- got
.br_startoff
;
5214 if (del
.br_startoff
+ del
.br_blockcount
> end
+ 1)
5215 del
.br_blockcount
= end
+ 1 - del
.br_startoff
;
5217 /* How much can we safely unmap? */
5218 if (max_len
< del
.br_blockcount
) {
5219 del
.br_startoff
+= del
.br_blockcount
- max_len
;
5221 del
.br_startblock
+= del
.br_blockcount
- max_len
;
5222 del
.br_blockcount
= max_len
;
5228 sum
= del
.br_startblock
+ del
.br_blockcount
;
5229 div_u64_rem(sum
, mp
->m_sb
.sb_rextsize
, &mod
);
5232 * Realtime extent not lined up at the end.
5233 * The extent could have been split into written
5234 * and unwritten pieces, or we could just be
5235 * unmapping part of it. But we can't really
5236 * get rid of part of a realtime extent.
5238 if (del
.br_state
== XFS_EXT_UNWRITTEN
) {
5240 * This piece is unwritten, or we're not
5241 * using unwritten extents. Skip over it.
5244 end
-= mod
> del
.br_blockcount
?
5245 del
.br_blockcount
: mod
;
5246 if (end
< got
.br_startoff
&&
5247 !xfs_iext_prev_extent(ifp
, &icur
, &got
)) {
5254 * It's written, turn it unwritten.
5255 * This is better than zeroing it.
5257 ASSERT(del
.br_state
== XFS_EXT_NORM
);
5258 ASSERT(tp
->t_blk_res
> 0);
5260 * If this spans a realtime extent boundary,
5261 * chop it back to the start of the one we end at.
5263 if (del
.br_blockcount
> mod
) {
5264 del
.br_startoff
+= del
.br_blockcount
- mod
;
5265 del
.br_startblock
+= del
.br_blockcount
- mod
;
5266 del
.br_blockcount
= mod
;
5268 del
.br_state
= XFS_EXT_UNWRITTEN
;
5269 error
= xfs_bmap_add_extent_unwritten_real(tp
, ip
,
5270 whichfork
, &icur
, &cur
, &del
,
5276 div_u64_rem(del
.br_startblock
, mp
->m_sb
.sb_rextsize
, &mod
);
5279 * Realtime extent is lined up at the end but not
5280 * at the front. We'll get rid of full extents if
5283 mod
= mp
->m_sb
.sb_rextsize
- mod
;
5284 if (del
.br_blockcount
> mod
) {
5285 del
.br_blockcount
-= mod
;
5286 del
.br_startoff
+= mod
;
5287 del
.br_startblock
+= mod
;
5288 } else if (del
.br_startoff
== start
&&
5289 (del
.br_state
== XFS_EXT_UNWRITTEN
||
5290 tp
->t_blk_res
== 0)) {
5292 * Can't make it unwritten. There isn't
5293 * a full extent here so just skip it.
5295 ASSERT(end
>= del
.br_blockcount
);
5296 end
-= del
.br_blockcount
;
5297 if (got
.br_startoff
> end
&&
5298 !xfs_iext_prev_extent(ifp
, &icur
, &got
)) {
5303 } else if (del
.br_state
== XFS_EXT_UNWRITTEN
) {
5304 struct xfs_bmbt_irec prev
;
5307 * This one is already unwritten.
5308 * It must have a written left neighbor.
5309 * Unwrite the killed part of that one and
5312 if (!xfs_iext_prev_extent(ifp
, &icur
, &prev
))
5314 ASSERT(prev
.br_state
== XFS_EXT_NORM
);
5315 ASSERT(!isnullstartblock(prev
.br_startblock
));
5316 ASSERT(del
.br_startblock
==
5317 prev
.br_startblock
+ prev
.br_blockcount
);
5318 if (prev
.br_startoff
< start
) {
5319 mod
= start
- prev
.br_startoff
;
5320 prev
.br_blockcount
-= mod
;
5321 prev
.br_startblock
+= mod
;
5322 prev
.br_startoff
= start
;
5324 prev
.br_state
= XFS_EXT_UNWRITTEN
;
5325 error
= xfs_bmap_add_extent_unwritten_real(tp
,
5326 ip
, whichfork
, &icur
, &cur
,
5332 ASSERT(del
.br_state
== XFS_EXT_NORM
);
5333 del
.br_state
= XFS_EXT_UNWRITTEN
;
5334 error
= xfs_bmap_add_extent_unwritten_real(tp
,
5335 ip
, whichfork
, &icur
, &cur
,
5345 error
= xfs_bmap_del_extent_delay(ip
, whichfork
, &icur
,
5348 error
= xfs_bmap_del_extent_real(ip
, tp
, &icur
, cur
,
5349 &del
, &tmp_logflags
, whichfork
,
5351 logflags
|= tmp_logflags
;
5357 max_len
-= del
.br_blockcount
;
5358 end
= del
.br_startoff
- 1;
5361 * If not done go on to the next (previous) record.
5363 if (end
!= (xfs_fileoff_t
)-1 && end
>= start
) {
5364 if (!xfs_iext_get_extent(ifp
, &icur
, &got
) ||
5365 (got
.br_startoff
> end
&&
5366 !xfs_iext_prev_extent(ifp
, &icur
, &got
))) {
5373 if (done
|| end
== (xfs_fileoff_t
)-1 || end
< start
)
5376 *rlen
= end
- start
+ 1;
5379 * Convert to a btree if necessary.
5381 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
5382 ASSERT(cur
== NULL
);
5383 error
= xfs_bmap_extents_to_btree(tp
, ip
, &cur
, 0,
5384 &tmp_logflags
, whichfork
);
5385 logflags
|= tmp_logflags
;
5390 * transform from btree to extents, give it cur
5392 else if (xfs_bmap_wants_extents(ip
, whichfork
)) {
5393 ASSERT(cur
!= NULL
);
5394 error
= xfs_bmap_btree_to_extents(tp
, ip
, cur
, &tmp_logflags
,
5396 logflags
|= tmp_logflags
;
5401 * transform from extents to local?
5406 * Log everything. Do this after conversion, there's no point in
5407 * logging the extent records if we've converted to btree format.
5409 if ((logflags
& xfs_ilog_fext(whichfork
)) &&
5410 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
5411 logflags
&= ~xfs_ilog_fext(whichfork
);
5412 else if ((logflags
& xfs_ilog_fbroot(whichfork
)) &&
5413 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)
5414 logflags
&= ~xfs_ilog_fbroot(whichfork
);
5416 * Log inode even in the error case, if the transaction
5417 * is dirty we'll need to shut down the filesystem.
5420 xfs_trans_log_inode(tp
, ip
, logflags
);
5423 cur
->bc_private
.b
.allocated
= 0;
5424 xfs_btree_del_cursor(cur
, error
);
5429 /* Unmap a range of a file. */
5433 struct xfs_inode
*ip
,
5442 error
= __xfs_bunmapi(tp
, ip
, bno
, &len
, flags
, nexts
);
5448 * Determine whether an extent shift can be accomplished by a merge with the
5449 * extent that precedes the target hole of the shift.
5453 struct xfs_bmbt_irec
*left
, /* preceding extent */
5454 struct xfs_bmbt_irec
*got
, /* current extent to shift */
5455 xfs_fileoff_t shift
) /* shift fsb */
5457 xfs_fileoff_t startoff
;
5459 startoff
= got
->br_startoff
- shift
;
5462 * The extent, once shifted, must be adjacent in-file and on-disk with
5463 * the preceding extent.
5465 if ((left
->br_startoff
+ left
->br_blockcount
!= startoff
) ||
5466 (left
->br_startblock
+ left
->br_blockcount
!= got
->br_startblock
) ||
5467 (left
->br_state
!= got
->br_state
) ||
5468 (left
->br_blockcount
+ got
->br_blockcount
> MAXEXTLEN
))
5475 * A bmap extent shift adjusts the file offset of an extent to fill a preceding
5476 * hole in the file. If an extent shift would result in the extent being fully
5477 * adjacent to the extent that currently precedes the hole, we can merge with
5478 * the preceding extent rather than do the shift.
5480 * This function assumes the caller has verified a shift-by-merge is possible
5481 * with the provided extents via xfs_bmse_can_merge().
5485 struct xfs_trans
*tp
,
5486 struct xfs_inode
*ip
,
5488 xfs_fileoff_t shift
, /* shift fsb */
5489 struct xfs_iext_cursor
*icur
,
5490 struct xfs_bmbt_irec
*got
, /* extent to shift */
5491 struct xfs_bmbt_irec
*left
, /* preceding extent */
5492 struct xfs_btree_cur
*cur
,
5493 int *logflags
) /* output */
5495 struct xfs_bmbt_irec
new;
5496 xfs_filblks_t blockcount
;
5498 struct xfs_mount
*mp
= ip
->i_mount
;
5500 blockcount
= left
->br_blockcount
+ got
->br_blockcount
;
5502 ASSERT(xfs_isilocked(ip
, XFS_IOLOCK_EXCL
));
5503 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
5504 ASSERT(xfs_bmse_can_merge(left
, got
, shift
));
5507 new.br_blockcount
= blockcount
;
5510 * Update the on-disk extent count, the btree if necessary and log the
5513 XFS_IFORK_NEXT_SET(ip
, whichfork
,
5514 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
5515 *logflags
|= XFS_ILOG_CORE
;
5517 *logflags
|= XFS_ILOG_DEXT
;
5521 /* lookup and remove the extent to merge */
5522 error
= xfs_bmbt_lookup_eq(cur
, got
, &i
);
5525 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
5527 error
= xfs_btree_delete(cur
, &i
);
5530 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
5532 /* lookup and update size of the previous extent */
5533 error
= xfs_bmbt_lookup_eq(cur
, left
, &i
);
5536 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
5538 error
= xfs_bmbt_update(cur
, &new);
5543 xfs_iext_remove(ip
, icur
, 0);
5544 xfs_iext_prev(XFS_IFORK_PTR(ip
, whichfork
), icur
);
5545 xfs_iext_update_extent(ip
, xfs_bmap_fork_to_state(whichfork
), icur
,
5548 /* update reverse mapping. rmap functions merge the rmaps for us */
5549 error
= xfs_rmap_unmap_extent(tp
, ip
, whichfork
, got
);
5552 memcpy(&new, got
, sizeof(new));
5553 new.br_startoff
= left
->br_startoff
+ left
->br_blockcount
;
5554 return xfs_rmap_map_extent(tp
, ip
, whichfork
, &new);
5558 xfs_bmap_shift_update_extent(
5559 struct xfs_trans
*tp
,
5560 struct xfs_inode
*ip
,
5562 struct xfs_iext_cursor
*icur
,
5563 struct xfs_bmbt_irec
*got
,
5564 struct xfs_btree_cur
*cur
,
5566 xfs_fileoff_t startoff
)
5568 struct xfs_mount
*mp
= ip
->i_mount
;
5569 struct xfs_bmbt_irec prev
= *got
;
5572 *logflags
|= XFS_ILOG_CORE
;
5574 got
->br_startoff
= startoff
;
5577 error
= xfs_bmbt_lookup_eq(cur
, &prev
, &i
);
5580 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
5582 error
= xfs_bmbt_update(cur
, got
);
5586 *logflags
|= XFS_ILOG_DEXT
;
5589 xfs_iext_update_extent(ip
, xfs_bmap_fork_to_state(whichfork
), icur
,
5592 /* update reverse mapping */
5593 error
= xfs_rmap_unmap_extent(tp
, ip
, whichfork
, &prev
);
5596 return xfs_rmap_map_extent(tp
, ip
, whichfork
, got
);
5600 xfs_bmap_collapse_extents(
5601 struct xfs_trans
*tp
,
5602 struct xfs_inode
*ip
,
5603 xfs_fileoff_t
*next_fsb
,
5604 xfs_fileoff_t offset_shift_fsb
,
5607 int whichfork
= XFS_DATA_FORK
;
5608 struct xfs_mount
*mp
= ip
->i_mount
;
5609 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5610 struct xfs_btree_cur
*cur
= NULL
;
5611 struct xfs_bmbt_irec got
, prev
;
5612 struct xfs_iext_cursor icur
;
5613 xfs_fileoff_t new_startoff
;
5617 if (unlikely(XFS_TEST_ERROR(
5618 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
5619 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
5620 mp
, XFS_ERRTAG_BMAPIFORMAT
))) {
5621 XFS_ERROR_REPORT(__func__
, XFS_ERRLEVEL_LOW
, mp
);
5622 return -EFSCORRUPTED
;
5625 if (XFS_FORCED_SHUTDOWN(mp
))
5628 ASSERT(xfs_isilocked(ip
, XFS_IOLOCK_EXCL
| XFS_ILOCK_EXCL
));
5630 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
5631 error
= xfs_iread_extents(tp
, ip
, whichfork
);
5636 if (ifp
->if_flags
& XFS_IFBROOT
) {
5637 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
5638 cur
->bc_private
.b
.flags
= 0;
5641 if (!xfs_iext_lookup_extent(ip
, ifp
, *next_fsb
, &icur
, &got
)) {
5645 XFS_WANT_CORRUPTED_GOTO(mp
, !isnullstartblock(got
.br_startblock
),
5648 new_startoff
= got
.br_startoff
- offset_shift_fsb
;
5649 if (xfs_iext_peek_prev_extent(ifp
, &icur
, &prev
)) {
5650 if (new_startoff
< prev
.br_startoff
+ prev
.br_blockcount
) {
5655 if (xfs_bmse_can_merge(&prev
, &got
, offset_shift_fsb
)) {
5656 error
= xfs_bmse_merge(tp
, ip
, whichfork
,
5657 offset_shift_fsb
, &icur
, &got
, &prev
,
5664 if (got
.br_startoff
< offset_shift_fsb
) {
5670 error
= xfs_bmap_shift_update_extent(tp
, ip
, whichfork
, &icur
, &got
,
5671 cur
, &logflags
, new_startoff
);
5676 if (!xfs_iext_next_extent(ifp
, &icur
, &got
)) {
5681 *next_fsb
= got
.br_startoff
;
5684 xfs_btree_del_cursor(cur
, error
);
5686 xfs_trans_log_inode(tp
, ip
, logflags
);
5690 /* Make sure we won't be right-shifting an extent past the maximum bound. */
5692 xfs_bmap_can_insert_extents(
5693 struct xfs_inode
*ip
,
5695 xfs_fileoff_t shift
)
5697 struct xfs_bmbt_irec got
;
5701 ASSERT(xfs_isilocked(ip
, XFS_IOLOCK_EXCL
));
5703 if (XFS_FORCED_SHUTDOWN(ip
->i_mount
))
5706 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
5707 error
= xfs_bmap_last_extent(NULL
, ip
, XFS_DATA_FORK
, &got
, &is_empty
);
5708 if (!error
&& !is_empty
&& got
.br_startoff
>= off
&&
5709 ((got
.br_startoff
+ shift
) & BMBT_STARTOFF_MASK
) < got
.br_startoff
)
5711 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
5717 xfs_bmap_insert_extents(
5718 struct xfs_trans
*tp
,
5719 struct xfs_inode
*ip
,
5720 xfs_fileoff_t
*next_fsb
,
5721 xfs_fileoff_t offset_shift_fsb
,
5723 xfs_fileoff_t stop_fsb
)
5725 int whichfork
= XFS_DATA_FORK
;
5726 struct xfs_mount
*mp
= ip
->i_mount
;
5727 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5728 struct xfs_btree_cur
*cur
= NULL
;
5729 struct xfs_bmbt_irec got
, next
;
5730 struct xfs_iext_cursor icur
;
5731 xfs_fileoff_t new_startoff
;
5735 if (unlikely(XFS_TEST_ERROR(
5736 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
5737 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
5738 mp
, XFS_ERRTAG_BMAPIFORMAT
))) {
5739 XFS_ERROR_REPORT(__func__
, XFS_ERRLEVEL_LOW
, mp
);
5740 return -EFSCORRUPTED
;
5743 if (XFS_FORCED_SHUTDOWN(mp
))
5746 ASSERT(xfs_isilocked(ip
, XFS_IOLOCK_EXCL
| XFS_ILOCK_EXCL
));
5748 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
5749 error
= xfs_iread_extents(tp
, ip
, whichfork
);
5754 if (ifp
->if_flags
& XFS_IFBROOT
) {
5755 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
5756 cur
->bc_private
.b
.flags
= 0;
5759 if (*next_fsb
== NULLFSBLOCK
) {
5760 xfs_iext_last(ifp
, &icur
);
5761 if (!xfs_iext_get_extent(ifp
, &icur
, &got
) ||
5762 stop_fsb
> got
.br_startoff
) {
5767 if (!xfs_iext_lookup_extent(ip
, ifp
, *next_fsb
, &icur
, &got
)) {
5772 XFS_WANT_CORRUPTED_GOTO(mp
, !isnullstartblock(got
.br_startblock
),
5775 if (stop_fsb
>= got
.br_startoff
+ got
.br_blockcount
) {
5780 new_startoff
= got
.br_startoff
+ offset_shift_fsb
;
5781 if (xfs_iext_peek_next_extent(ifp
, &icur
, &next
)) {
5782 if (new_startoff
+ got
.br_blockcount
> next
.br_startoff
) {
5788 * Unlike a left shift (which involves a hole punch), a right
5789 * shift does not modify extent neighbors in any way. We should
5790 * never find mergeable extents in this scenario. Check anyways
5791 * and warn if we encounter two extents that could be one.
5793 if (xfs_bmse_can_merge(&got
, &next
, offset_shift_fsb
))
5797 error
= xfs_bmap_shift_update_extent(tp
, ip
, whichfork
, &icur
, &got
,
5798 cur
, &logflags
, new_startoff
);
5802 if (!xfs_iext_prev_extent(ifp
, &icur
, &got
) ||
5803 stop_fsb
>= got
.br_startoff
+ got
.br_blockcount
) {
5808 *next_fsb
= got
.br_startoff
;
5811 xfs_btree_del_cursor(cur
, error
);
5813 xfs_trans_log_inode(tp
, ip
, logflags
);
5818 * Splits an extent into two extents at split_fsb block such that it is the
5819 * first block of the current_ext. @ext is a target extent to be split.
5820 * @split_fsb is a block where the extents is split. If split_fsb lies in a
5821 * hole or the first block of extents, just return 0.
5824 xfs_bmap_split_extent_at(
5825 struct xfs_trans
*tp
,
5826 struct xfs_inode
*ip
,
5827 xfs_fileoff_t split_fsb
)
5829 int whichfork
= XFS_DATA_FORK
;
5830 struct xfs_btree_cur
*cur
= NULL
;
5831 struct xfs_bmbt_irec got
;
5832 struct xfs_bmbt_irec
new; /* split extent */
5833 struct xfs_mount
*mp
= ip
->i_mount
;
5834 struct xfs_ifork
*ifp
;
5835 xfs_fsblock_t gotblkcnt
; /* new block count for got */
5836 struct xfs_iext_cursor icur
;
5841 if (unlikely(XFS_TEST_ERROR(
5842 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
5843 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
5844 mp
, XFS_ERRTAG_BMAPIFORMAT
))) {
5845 XFS_ERROR_REPORT("xfs_bmap_split_extent_at",
5846 XFS_ERRLEVEL_LOW
, mp
);
5847 return -EFSCORRUPTED
;
5850 if (XFS_FORCED_SHUTDOWN(mp
))
5853 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5854 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
5855 /* Read in all the extents */
5856 error
= xfs_iread_extents(tp
, ip
, whichfork
);
5862 * If there are not extents, or split_fsb lies in a hole we are done.
5864 if (!xfs_iext_lookup_extent(ip
, ifp
, split_fsb
, &icur
, &got
) ||
5865 got
.br_startoff
>= split_fsb
)
5868 gotblkcnt
= split_fsb
- got
.br_startoff
;
5869 new.br_startoff
= split_fsb
;
5870 new.br_startblock
= got
.br_startblock
+ gotblkcnt
;
5871 new.br_blockcount
= got
.br_blockcount
- gotblkcnt
;
5872 new.br_state
= got
.br_state
;
5874 if (ifp
->if_flags
& XFS_IFBROOT
) {
5875 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
5876 cur
->bc_private
.b
.flags
= 0;
5877 error
= xfs_bmbt_lookup_eq(cur
, &got
, &i
);
5880 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, del_cursor
);
5883 got
.br_blockcount
= gotblkcnt
;
5884 xfs_iext_update_extent(ip
, xfs_bmap_fork_to_state(whichfork
), &icur
,
5887 logflags
= XFS_ILOG_CORE
;
5889 error
= xfs_bmbt_update(cur
, &got
);
5893 logflags
|= XFS_ILOG_DEXT
;
5895 /* Add new extent */
5896 xfs_iext_next(ifp
, &icur
);
5897 xfs_iext_insert(ip
, &icur
, &new, 0);
5898 XFS_IFORK_NEXT_SET(ip
, whichfork
,
5899 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
5902 error
= xfs_bmbt_lookup_eq(cur
, &new, &i
);
5905 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, del_cursor
);
5906 error
= xfs_btree_insert(cur
, &i
);
5909 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, del_cursor
);
5913 * Convert to a btree if necessary.
5915 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
5916 int tmp_logflags
; /* partial log flag return val */
5918 ASSERT(cur
== NULL
);
5919 error
= xfs_bmap_extents_to_btree(tp
, ip
, &cur
, 0,
5920 &tmp_logflags
, whichfork
);
5921 logflags
|= tmp_logflags
;
5926 cur
->bc_private
.b
.allocated
= 0;
5927 xfs_btree_del_cursor(cur
, error
);
5931 xfs_trans_log_inode(tp
, ip
, logflags
);
5936 xfs_bmap_split_extent(
5937 struct xfs_inode
*ip
,
5938 xfs_fileoff_t split_fsb
)
5940 struct xfs_mount
*mp
= ip
->i_mount
;
5941 struct xfs_trans
*tp
;
5944 error
= xfs_trans_alloc(mp
, &M_RES(mp
)->tr_write
,
5945 XFS_DIOSTRAT_SPACE_RES(mp
, 0), 0, 0, &tp
);
5949 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
5950 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
);
5952 error
= xfs_bmap_split_extent_at(tp
, ip
, split_fsb
);
5956 return xfs_trans_commit(tp
);
5959 xfs_trans_cancel(tp
);
5963 /* Deferred mapping is only for real extents in the data fork. */
5965 xfs_bmap_is_update_needed(
5966 struct xfs_bmbt_irec
*bmap
)
5968 return bmap
->br_startblock
!= HOLESTARTBLOCK
&&
5969 bmap
->br_startblock
!= DELAYSTARTBLOCK
;
5972 /* Record a bmap intent. */
5975 struct xfs_trans
*tp
,
5976 enum xfs_bmap_intent_type type
,
5977 struct xfs_inode
*ip
,
5979 struct xfs_bmbt_irec
*bmap
)
5981 struct xfs_bmap_intent
*bi
;
5983 trace_xfs_bmap_defer(tp
->t_mountp
,
5984 XFS_FSB_TO_AGNO(tp
->t_mountp
, bmap
->br_startblock
),
5986 XFS_FSB_TO_AGBNO(tp
->t_mountp
, bmap
->br_startblock
),
5987 ip
->i_ino
, whichfork
,
5989 bmap
->br_blockcount
,
5992 bi
= kmem_alloc(sizeof(struct xfs_bmap_intent
), KM_SLEEP
| KM_NOFS
);
5993 INIT_LIST_HEAD(&bi
->bi_list
);
5996 bi
->bi_whichfork
= whichfork
;
5997 bi
->bi_bmap
= *bmap
;
5999 xfs_defer_add(tp
, XFS_DEFER_OPS_TYPE_BMAP
, &bi
->bi_list
);
6003 /* Map an extent into a file. */
6005 xfs_bmap_map_extent(
6006 struct xfs_trans
*tp
,
6007 struct xfs_inode
*ip
,
6008 struct xfs_bmbt_irec
*PREV
)
6010 if (!xfs_bmap_is_update_needed(PREV
))
6013 return __xfs_bmap_add(tp
, XFS_BMAP_MAP
, ip
, XFS_DATA_FORK
, PREV
);
6016 /* Unmap an extent out of a file. */
6018 xfs_bmap_unmap_extent(
6019 struct xfs_trans
*tp
,
6020 struct xfs_inode
*ip
,
6021 struct xfs_bmbt_irec
*PREV
)
6023 if (!xfs_bmap_is_update_needed(PREV
))
6026 return __xfs_bmap_add(tp
, XFS_BMAP_UNMAP
, ip
, XFS_DATA_FORK
, PREV
);
6030 * Process one of the deferred bmap operations. We pass back the
6031 * btree cursor to maintain our lock on the bmapbt between calls.
6034 xfs_bmap_finish_one(
6035 struct xfs_trans
*tp
,
6036 struct xfs_inode
*ip
,
6037 enum xfs_bmap_intent_type type
,
6039 xfs_fileoff_t startoff
,
6040 xfs_fsblock_t startblock
,
6041 xfs_filblks_t
*blockcount
,
6046 ASSERT(tp
->t_firstblock
== NULLFSBLOCK
);
6048 trace_xfs_bmap_deferred(tp
->t_mountp
,
6049 XFS_FSB_TO_AGNO(tp
->t_mountp
, startblock
), type
,
6050 XFS_FSB_TO_AGBNO(tp
->t_mountp
, startblock
),
6051 ip
->i_ino
, whichfork
, startoff
, *blockcount
, state
);
6053 if (WARN_ON_ONCE(whichfork
!= XFS_DATA_FORK
))
6054 return -EFSCORRUPTED
;
6056 if (XFS_TEST_ERROR(false, tp
->t_mountp
,
6057 XFS_ERRTAG_BMAP_FINISH_ONE
))
6062 error
= xfs_bmapi_remap(tp
, ip
, startoff
, *blockcount
,
6066 case XFS_BMAP_UNMAP
:
6067 error
= __xfs_bunmapi(tp
, ip
, startoff
, blockcount
,
6068 XFS_BMAPI_REMAP
, 1);
6072 error
= -EFSCORRUPTED
;
6078 /* Check that an inode's extent does not have invalid flags or bad ranges. */
6080 xfs_bmap_validate_extent(
6081 struct xfs_inode
*ip
,
6083 struct xfs_bmbt_irec
*irec
)
6085 struct xfs_mount
*mp
= ip
->i_mount
;
6086 xfs_fsblock_t endfsb
;
6089 isrt
= XFS_IS_REALTIME_INODE(ip
);
6090 endfsb
= irec
->br_startblock
+ irec
->br_blockcount
- 1;
6092 if (!xfs_verify_rtbno(mp
, irec
->br_startblock
))
6093 return __this_address
;
6094 if (!xfs_verify_rtbno(mp
, endfsb
))
6095 return __this_address
;
6097 if (!xfs_verify_fsbno(mp
, irec
->br_startblock
))
6098 return __this_address
;
6099 if (!xfs_verify_fsbno(mp
, endfsb
))
6100 return __this_address
;
6101 if (XFS_FSB_TO_AGNO(mp
, irec
->br_startblock
) !=
6102 XFS_FSB_TO_AGNO(mp
, endfsb
))
6103 return __this_address
;
6105 if (irec
->br_state
!= XFS_EXT_NORM
&& whichfork
!= XFS_DATA_FORK
)
6106 return __this_address
;