1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
6 #include "libxfs_priv.h"
8 #include "xfs_shared.h"
9 #include "xfs_format.h"
10 #include "xfs_log_format.h"
11 #include "xfs_trans_resv.h"
14 #include "xfs_mount.h"
15 #include "xfs_defer.h"
16 #include "xfs_da_format.h"
17 #include "xfs_da_btree.h"
19 #include "xfs_inode.h"
20 #include "xfs_btree.h"
21 #include "xfs_trans.h"
22 #include "xfs_alloc.h"
24 #include "xfs_bmap_btree.h"
25 #include "xfs_errortag.h"
26 #include "xfs_trans_space.h"
27 #include "xfs_trace.h"
28 #include "xfs_attr_leaf.h"
29 #include "xfs_quota_defs.h"
31 #include "xfs_ag_resv.h"
32 #include "xfs_refcount.h"
35 kmem_zone_t
*xfs_bmap_free_item_zone
;
38 * Miscellaneous helper functions
42 * Compute and fill in the value of the maximum depth of a bmap btree
43 * in this filesystem. Done once, during mount.
46 xfs_bmap_compute_maxlevels(
47 xfs_mount_t
*mp
, /* file system mount structure */
48 int whichfork
) /* data or attr fork */
50 int level
; /* btree level */
51 uint maxblocks
; /* max blocks at this level */
52 uint maxleafents
; /* max leaf entries possible */
53 int maxrootrecs
; /* max records in root block */
54 int minleafrecs
; /* min records in leaf block */
55 int minnoderecs
; /* min records in node block */
56 int sz
; /* root block size */
59 * The maximum number of extents in a file, hence the maximum
60 * number of leaf entries, is controlled by the type of di_nextents
61 * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
62 * (a signed 16-bit number, xfs_aextnum_t).
64 * Note that we can no longer assume that if we are in ATTR1 that
65 * the fork offset of all the inodes will be
66 * (xfs_default_attroffset(ip) >> 3) because we could have mounted
67 * with ATTR2 and then mounted back with ATTR1, keeping the
68 * di_forkoff's fixed but probably at various positions. Therefore,
69 * for both ATTR1 and ATTR2 we have to assume the worst case scenario
70 * of a minimum size available.
72 if (whichfork
== XFS_DATA_FORK
) {
73 maxleafents
= MAXEXTNUM
;
74 sz
= XFS_BMDR_SPACE_CALC(MINDBTPTRS
);
76 maxleafents
= MAXAEXTNUM
;
77 sz
= XFS_BMDR_SPACE_CALC(MINABTPTRS
);
79 maxrootrecs
= xfs_bmdr_maxrecs(sz
, 0);
80 minleafrecs
= mp
->m_bmap_dmnr
[0];
81 minnoderecs
= mp
->m_bmap_dmnr
[1];
82 maxblocks
= (maxleafents
+ minleafrecs
- 1) / minleafrecs
;
83 for (level
= 1; maxblocks
> 1; level
++) {
84 if (maxblocks
<= maxrootrecs
)
87 maxblocks
= (maxblocks
+ minnoderecs
- 1) / minnoderecs
;
89 mp
->m_bm_maxlevels
[whichfork
] = level
;
92 STATIC
int /* error */
94 struct xfs_btree_cur
*cur
,
95 struct xfs_bmbt_irec
*irec
,
96 int *stat
) /* success/failure */
98 cur
->bc_rec
.b
= *irec
;
99 return xfs_btree_lookup(cur
, XFS_LOOKUP_EQ
, stat
);
102 STATIC
int /* error */
103 xfs_bmbt_lookup_first(
104 struct xfs_btree_cur
*cur
,
105 int *stat
) /* success/failure */
107 cur
->bc_rec
.b
.br_startoff
= 0;
108 cur
->bc_rec
.b
.br_startblock
= 0;
109 cur
->bc_rec
.b
.br_blockcount
= 0;
110 return xfs_btree_lookup(cur
, XFS_LOOKUP_GE
, stat
);
114 * Check if the inode needs to be converted to btree format.
116 static inline bool xfs_bmap_needs_btree(struct xfs_inode
*ip
, int whichfork
)
118 return whichfork
!= XFS_COW_FORK
&&
119 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
&&
120 XFS_IFORK_NEXTENTS(ip
, whichfork
) >
121 XFS_IFORK_MAXEXT(ip
, whichfork
);
125 * Check if the inode should be converted to extent format.
127 static inline bool xfs_bmap_wants_extents(struct xfs_inode
*ip
, int whichfork
)
129 return whichfork
!= XFS_COW_FORK
&&
130 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
&&
131 XFS_IFORK_NEXTENTS(ip
, whichfork
) <=
132 XFS_IFORK_MAXEXT(ip
, whichfork
);
136 * Update the record referred to by cur to the value given by irec
137 * This either works (return 0) or gets an EFSCORRUPTED error.
141 struct xfs_btree_cur
*cur
,
142 struct xfs_bmbt_irec
*irec
)
144 union xfs_btree_rec rec
;
146 xfs_bmbt_disk_set_all(&rec
.bmbt
, irec
);
147 return xfs_btree_update(cur
, &rec
);
151 * Compute the worst-case number of indirect blocks that will be used
152 * for ip's delayed extent of length "len".
155 xfs_bmap_worst_indlen(
156 xfs_inode_t
*ip
, /* incore inode pointer */
157 xfs_filblks_t len
) /* delayed extent length */
159 int level
; /* btree level number */
160 int maxrecs
; /* maximum record count at this level */
161 xfs_mount_t
*mp
; /* mount structure */
162 xfs_filblks_t rval
; /* return value */
165 maxrecs
= mp
->m_bmap_dmxr
[0];
166 for (level
= 0, rval
= 0;
167 level
< XFS_BM_MAXLEVELS(mp
, XFS_DATA_FORK
);
170 do_div(len
, maxrecs
);
173 return rval
+ XFS_BM_MAXLEVELS(mp
, XFS_DATA_FORK
) -
176 maxrecs
= mp
->m_bmap_dmxr
[1];
182 * Calculate the default attribute fork offset for newly created inodes.
185 xfs_default_attroffset(
186 struct xfs_inode
*ip
)
188 struct xfs_mount
*mp
= ip
->i_mount
;
191 if (mp
->m_sb
.sb_inodesize
== 256) {
192 offset
= XFS_LITINO(mp
, ip
->i_d
.di_version
) -
193 XFS_BMDR_SPACE_CALC(MINABTPTRS
);
195 offset
= XFS_BMDR_SPACE_CALC(6 * MINABTPTRS
);
198 ASSERT(offset
< XFS_LITINO(mp
, ip
->i_d
.di_version
));
203 * Helper routine to reset inode di_forkoff field when switching
204 * attribute fork from local to extent format - we reset it where
205 * possible to make space available for inline data fork extents.
208 xfs_bmap_forkoff_reset(
212 if (whichfork
== XFS_ATTR_FORK
&&
213 ip
->i_d
.di_format
!= XFS_DINODE_FMT_DEV
&&
214 ip
->i_d
.di_format
!= XFS_DINODE_FMT_BTREE
) {
215 uint dfl_forkoff
= xfs_default_attroffset(ip
) >> 3;
217 if (dfl_forkoff
> ip
->i_d
.di_forkoff
)
218 ip
->i_d
.di_forkoff
= dfl_forkoff
;
223 STATIC
struct xfs_buf
*
225 struct xfs_btree_cur
*cur
,
228 struct xfs_log_item
*lip
;
234 for (i
= 0; i
< XFS_BTREE_MAXLEVELS
; i
++) {
235 if (!cur
->bc_bufs
[i
])
237 if (XFS_BUF_ADDR(cur
->bc_bufs
[i
]) == bno
)
238 return cur
->bc_bufs
[i
];
241 /* Chase down all the log items to see if the bp is there */
242 list_for_each_entry(lip
, &cur
->bc_tp
->t_items
, li_trans
) {
243 struct xfs_buf_log_item
*bip
= (struct xfs_buf_log_item
*)lip
;
245 if (bip
->bli_item
.li_type
== XFS_LI_BUF
&&
246 XFS_BUF_ADDR(bip
->bli_buf
) == bno
)
255 struct xfs_btree_block
*block
,
261 __be64
*pp
, *thispa
; /* pointer to block address */
262 xfs_bmbt_key_t
*prevp
, *keyp
;
264 ASSERT(be16_to_cpu(block
->bb_level
) > 0);
267 for( i
= 1; i
<= xfs_btree_get_numrecs(block
); i
++) {
268 dmxr
= mp
->m_bmap_dmxr
[0];
269 keyp
= XFS_BMBT_KEY_ADDR(mp
, block
, i
);
272 ASSERT(be64_to_cpu(prevp
->br_startoff
) <
273 be64_to_cpu(keyp
->br_startoff
));
278 * Compare the block numbers to see if there are dups.
281 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, i
, sz
);
283 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, i
, dmxr
);
285 for (j
= i
+1; j
<= be16_to_cpu(block
->bb_numrecs
); j
++) {
287 thispa
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, j
, sz
);
289 thispa
= XFS_BMBT_PTR_ADDR(mp
, block
, j
, dmxr
);
290 if (*thispa
== *pp
) {
291 xfs_warn(mp
, "%s: thispa(%d) == pp(%d) %Ld",
293 (unsigned long long)be64_to_cpu(*thispa
));
294 xfs_err(mp
, "%s: ptrs are equal in node\n",
296 xfs_force_shutdown(mp
, SHUTDOWN_CORRUPT_INCORE
);
303 * Check that the extents for the inode ip are in the right order in all
304 * btree leaves. THis becomes prohibitively expensive for large extent count
305 * files, so don't bother with inodes that have more than 10,000 extents in
306 * them. The btree record ordering checks will still be done, so for such large
307 * bmapbt constructs that is going to catch most corruptions.
310 xfs_bmap_check_leaf_extents(
311 xfs_btree_cur_t
*cur
, /* btree cursor or null */
312 xfs_inode_t
*ip
, /* incore inode pointer */
313 int whichfork
) /* data or attr fork */
315 struct xfs_btree_block
*block
; /* current btree block */
316 xfs_fsblock_t bno
; /* block # of "block" */
317 xfs_buf_t
*bp
; /* buffer for "block" */
318 int error
; /* error return value */
319 xfs_extnum_t i
=0, j
; /* index into the extents list */
320 xfs_ifork_t
*ifp
; /* fork structure */
321 int level
; /* btree level, for checking */
322 xfs_mount_t
*mp
; /* file system mount structure */
323 __be64
*pp
; /* pointer to block address */
324 xfs_bmbt_rec_t
*ep
; /* pointer to current extent */
325 xfs_bmbt_rec_t last
= {0, 0}; /* last extent in prev block */
326 xfs_bmbt_rec_t
*nextp
; /* pointer to next extent */
329 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
) {
333 /* skip large extent count inodes */
334 if (ip
->i_d
.di_nextents
> 10000)
339 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
340 block
= ifp
->if_broot
;
342 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
344 level
= be16_to_cpu(block
->bb_level
);
346 xfs_check_block(block
, mp
, 1, ifp
->if_broot_bytes
);
347 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, 1, ifp
->if_broot_bytes
);
348 bno
= be64_to_cpu(*pp
);
350 ASSERT(bno
!= NULLFSBLOCK
);
351 ASSERT(XFS_FSB_TO_AGNO(mp
, bno
) < mp
->m_sb
.sb_agcount
);
352 ASSERT(XFS_FSB_TO_AGBNO(mp
, bno
) < mp
->m_sb
.sb_agblocks
);
355 * Go down the tree until leaf level is reached, following the first
356 * pointer (leftmost) at each level.
358 while (level
-- > 0) {
359 /* See if buf is in cur first */
361 bp
= xfs_bmap_get_bp(cur
, XFS_FSB_TO_DADDR(mp
, bno
));
364 error
= xfs_btree_read_bufl(mp
, NULL
, bno
, 0, &bp
,
370 block
= XFS_BUF_TO_BLOCK(bp
);
375 * Check this block for basic sanity (increasing keys and
376 * no duplicate blocks).
379 xfs_check_block(block
, mp
, 0, 0);
380 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, mp
->m_bmap_dmxr
[1]);
381 bno
= be64_to_cpu(*pp
);
382 XFS_WANT_CORRUPTED_GOTO(mp
,
383 xfs_verify_fsbno(mp
, bno
), error0
);
386 xfs_trans_brelse(NULL
, bp
);
391 * Here with bp and block set to the leftmost leaf node in the tree.
396 * Loop over all leaf nodes checking that all extents are in the right order.
399 xfs_fsblock_t nextbno
;
400 xfs_extnum_t num_recs
;
403 num_recs
= xfs_btree_get_numrecs(block
);
406 * Read-ahead the next leaf block, if any.
409 nextbno
= be64_to_cpu(block
->bb_u
.l
.bb_rightsib
);
412 * Check all the extents to make sure they are OK.
413 * If we had a previous block, the last entry should
414 * conform with the first entry in this one.
417 ep
= XFS_BMBT_REC_ADDR(mp
, block
, 1);
419 ASSERT(xfs_bmbt_disk_get_startoff(&last
) +
420 xfs_bmbt_disk_get_blockcount(&last
) <=
421 xfs_bmbt_disk_get_startoff(ep
));
423 for (j
= 1; j
< num_recs
; j
++) {
424 nextp
= XFS_BMBT_REC_ADDR(mp
, block
, j
+ 1);
425 ASSERT(xfs_bmbt_disk_get_startoff(ep
) +
426 xfs_bmbt_disk_get_blockcount(ep
) <=
427 xfs_bmbt_disk_get_startoff(nextp
));
435 xfs_trans_brelse(NULL
, bp
);
439 * If we've reached the end, stop.
441 if (bno
== NULLFSBLOCK
)
445 bp
= xfs_bmap_get_bp(cur
, XFS_FSB_TO_DADDR(mp
, bno
));
448 error
= xfs_btree_read_bufl(mp
, NULL
, bno
, 0, &bp
,
454 block
= XFS_BUF_TO_BLOCK(bp
);
460 xfs_warn(mp
, "%s: at error0", __func__
);
462 xfs_trans_brelse(NULL
, bp
);
464 xfs_warn(mp
, "%s: BAD after btree leaves for %d extents",
466 xfs_err(mp
, "%s: CORRUPTED BTREE OR SOMETHING", __func__
);
467 xfs_force_shutdown(mp
, SHUTDOWN_CORRUPT_INCORE
);
472 * Validate that the bmbt_irecs being returned from bmapi are valid
473 * given the caller's original parameters. Specifically check the
474 * ranges of the returned irecs to ensure that they only extend beyond
475 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
478 xfs_bmap_validate_ret(
482 xfs_bmbt_irec_t
*mval
,
486 int i
; /* index to map values */
488 ASSERT(ret_nmap
<= nmap
);
490 for (i
= 0; i
< ret_nmap
; i
++) {
491 ASSERT(mval
[i
].br_blockcount
> 0);
492 if (!(flags
& XFS_BMAPI_ENTIRE
)) {
493 ASSERT(mval
[i
].br_startoff
>= bno
);
494 ASSERT(mval
[i
].br_blockcount
<= len
);
495 ASSERT(mval
[i
].br_startoff
+ mval
[i
].br_blockcount
<=
498 ASSERT(mval
[i
].br_startoff
< bno
+ len
);
499 ASSERT(mval
[i
].br_startoff
+ mval
[i
].br_blockcount
>
503 mval
[i
- 1].br_startoff
+ mval
[i
- 1].br_blockcount
==
504 mval
[i
].br_startoff
);
505 ASSERT(mval
[i
].br_startblock
!= DELAYSTARTBLOCK
&&
506 mval
[i
].br_startblock
!= HOLESTARTBLOCK
);
507 ASSERT(mval
[i
].br_state
== XFS_EXT_NORM
||
508 mval
[i
].br_state
== XFS_EXT_UNWRITTEN
);
513 #define xfs_bmap_check_leaf_extents(cur, ip, whichfork) do { } while (0)
514 #define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap) do { } while (0)
518 * bmap free list manipulation functions
522 * Add the extent to the list of extents to be free at transaction end.
523 * The list is maintained sorted (by block number).
527 struct xfs_mount
*mp
,
528 struct xfs_defer_ops
*dfops
,
531 struct xfs_owner_info
*oinfo
,
534 struct xfs_extent_free_item
*new; /* new element */
539 ASSERT(bno
!= NULLFSBLOCK
);
541 ASSERT(len
<= MAXEXTLEN
);
542 ASSERT(!isnullstartblock(bno
));
543 agno
= XFS_FSB_TO_AGNO(mp
, bno
);
544 agbno
= XFS_FSB_TO_AGBNO(mp
, bno
);
545 ASSERT(agno
< mp
->m_sb
.sb_agcount
);
546 ASSERT(agbno
< mp
->m_sb
.sb_agblocks
);
547 ASSERT(len
< mp
->m_sb
.sb_agblocks
);
548 ASSERT(agbno
+ len
<= mp
->m_sb
.sb_agblocks
);
550 ASSERT(xfs_bmap_free_item_zone
!= NULL
);
552 new = kmem_zone_alloc(xfs_bmap_free_item_zone
, KM_SLEEP
);
553 new->xefi_startblock
= bno
;
554 new->xefi_blockcount
= (xfs_extlen_t
)len
;
556 new->xefi_oinfo
= *oinfo
;
558 xfs_rmap_skip_owner_update(&new->xefi_oinfo
);
559 new->xefi_skip_discard
= skip_discard
;
560 trace_xfs_bmap_free_defer(mp
, XFS_FSB_TO_AGNO(mp
, bno
), 0,
561 XFS_FSB_TO_AGBNO(mp
, bno
), len
);
562 xfs_defer_add(dfops
, XFS_DEFER_OPS_TYPE_FREE
, &new->xefi_list
);
566 * Inode fork format manipulation functions
570 * Transform a btree format file with only one leaf node, where the
571 * extents list will fit in the inode, into an extents format file.
572 * Since the file extents are already in-core, all we have to do is
573 * give up the space for the btree root and pitch the leaf block.
575 STATIC
int /* error */
576 xfs_bmap_btree_to_extents(
577 xfs_trans_t
*tp
, /* transaction pointer */
578 xfs_inode_t
*ip
, /* incore inode pointer */
579 xfs_btree_cur_t
*cur
, /* btree cursor */
580 int *logflagsp
, /* inode logging flags */
581 int whichfork
) /* data or attr fork */
584 struct xfs_btree_block
*cblock
;/* child btree block */
585 xfs_fsblock_t cbno
; /* child block number */
586 xfs_buf_t
*cbp
; /* child block's buffer */
587 int error
; /* error return value */
588 xfs_ifork_t
*ifp
; /* inode fork data */
589 xfs_mount_t
*mp
; /* mount point structure */
590 __be64
*pp
; /* ptr to block address */
591 struct xfs_btree_block
*rblock
;/* root btree block */
592 struct xfs_owner_info oinfo
;
595 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
596 ASSERT(whichfork
!= XFS_COW_FORK
);
597 ASSERT(ifp
->if_flags
& XFS_IFEXTENTS
);
598 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
);
599 rblock
= ifp
->if_broot
;
600 ASSERT(be16_to_cpu(rblock
->bb_level
) == 1);
601 ASSERT(be16_to_cpu(rblock
->bb_numrecs
) == 1);
602 ASSERT(xfs_bmbt_maxrecs(mp
, ifp
->if_broot_bytes
, 0) == 1);
603 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, rblock
, 1, ifp
->if_broot_bytes
);
604 cbno
= be64_to_cpu(*pp
);
607 XFS_WANT_CORRUPTED_RETURN(cur
->bc_mp
,
608 xfs_btree_check_lptr(cur
, cbno
, 1));
610 error
= xfs_btree_read_bufl(mp
, tp
, cbno
, 0, &cbp
, XFS_BMAP_BTREE_REF
,
614 cblock
= XFS_BUF_TO_BLOCK(cbp
);
615 if ((error
= xfs_btree_check_block(cur
, cblock
, 0, cbp
)))
617 xfs_rmap_ino_bmbt_owner(&oinfo
, ip
->i_ino
, whichfork
);
618 xfs_bmap_add_free(mp
, cur
->bc_private
.b
.dfops
, cbno
, 1, &oinfo
);
619 ip
->i_d
.di_nblocks
--;
620 xfs_trans_mod_dquot_byino(tp
, ip
, XFS_TRANS_DQ_BCOUNT
, -1L);
621 xfs_trans_binval(tp
, cbp
);
622 if (cur
->bc_bufs
[0] == cbp
)
623 cur
->bc_bufs
[0] = NULL
;
624 xfs_iroot_realloc(ip
, -1, whichfork
);
625 ASSERT(ifp
->if_broot
== NULL
);
626 ASSERT((ifp
->if_flags
& XFS_IFBROOT
) == 0);
627 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_EXTENTS
);
628 *logflagsp
= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
633 * Convert an extents-format file into a btree-format file.
634 * The new file will have a root block (in the inode) and a single child block.
636 STATIC
int /* error */
637 xfs_bmap_extents_to_btree(
638 xfs_trans_t
*tp
, /* transaction pointer */
639 xfs_inode_t
*ip
, /* incore inode pointer */
640 xfs_fsblock_t
*firstblock
, /* first-block-allocated */
641 struct xfs_defer_ops
*dfops
, /* blocks freed in xaction */
642 xfs_btree_cur_t
**curp
, /* cursor returned to caller */
643 int wasdel
, /* converting a delayed alloc */
644 int *logflagsp
, /* inode logging flags */
645 int whichfork
) /* data or attr fork */
647 struct xfs_btree_block
*ablock
; /* allocated (child) bt block */
648 xfs_buf_t
*abp
; /* buffer for ablock */
649 xfs_alloc_arg_t args
; /* allocation arguments */
650 xfs_bmbt_rec_t
*arp
; /* child record pointer */
651 struct xfs_btree_block
*block
; /* btree root block */
652 xfs_btree_cur_t
*cur
; /* bmap btree cursor */
653 int error
; /* error return value */
654 xfs_ifork_t
*ifp
; /* inode fork pointer */
655 xfs_bmbt_key_t
*kp
; /* root block key pointer */
656 xfs_mount_t
*mp
; /* mount structure */
657 xfs_bmbt_ptr_t
*pp
; /* root block address pointer */
658 struct xfs_iext_cursor icur
;
659 struct xfs_bmbt_irec rec
;
660 xfs_extnum_t cnt
= 0;
663 ASSERT(whichfork
!= XFS_COW_FORK
);
664 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
665 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
);
668 * Make space in the inode incore.
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
.firstblock
= *firstblock
;
685 cur
->bc_private
.b
.dfops
= dfops
;
686 cur
->bc_private
.b
.flags
= wasdel
? XFS_BTCUR_BPRV_WASDEL
: 0;
688 * Convert to a btree with two levels, one record in root.
690 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_BTREE
);
691 memset(&args
, 0, sizeof(args
));
694 xfs_rmap_ino_bmbt_owner(&args
.oinfo
, ip
->i_ino
, whichfork
);
695 args
.firstblock
= *firstblock
;
696 if (*firstblock
== NULLFSBLOCK
) {
697 args
.type
= XFS_ALLOCTYPE_START_BNO
;
698 args
.fsbno
= XFS_INO_TO_FSB(mp
, ip
->i_ino
);
699 } else if (dfops
->dop_low
) {
700 args
.type
= XFS_ALLOCTYPE_START_BNO
;
701 args
.fsbno
= *firstblock
;
703 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
704 args
.fsbno
= *firstblock
;
706 args
.minlen
= args
.maxlen
= args
.prod
= 1;
707 args
.wasdel
= wasdel
;
709 if ((error
= xfs_alloc_vextent(&args
))) {
710 xfs_iroot_realloc(ip
, -1, whichfork
);
711 ASSERT(ifp
->if_broot
== NULL
);
712 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_EXTENTS
);
713 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
717 if (WARN_ON_ONCE(args
.fsbno
== NULLFSBLOCK
)) {
718 xfs_iroot_realloc(ip
, -1, whichfork
);
719 ASSERT(ifp
->if_broot
== NULL
);
720 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_EXTENTS
);
721 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
725 * Allocation can't fail, the space was reserved.
727 ASSERT(*firstblock
== NULLFSBLOCK
||
728 args
.agno
>= XFS_FSB_TO_AGNO(mp
, *firstblock
));
729 *firstblock
= cur
->bc_private
.b
.firstblock
= args
.fsbno
;
730 cur
->bc_private
.b
.allocated
++;
731 ip
->i_d
.di_nblocks
++;
732 xfs_trans_mod_dquot_byino(tp
, ip
, XFS_TRANS_DQ_BCOUNT
, 1L);
733 abp
= xfs_btree_get_bufl(mp
, tp
, args
.fsbno
, 0);
735 * Fill in the child block.
737 abp
->b_ops
= &xfs_bmbt_buf_ops
;
738 ablock
= XFS_BUF_TO_BLOCK(abp
);
739 xfs_btree_init_block_int(mp
, ablock
, abp
->b_bn
,
740 XFS_BTNUM_BMAP
, 0, 0, ip
->i_ino
,
741 XFS_BTREE_LONG_PTRS
);
743 for_each_xfs_iext(ifp
, &icur
, &rec
) {
744 if (isnullstartblock(rec
.br_startblock
))
746 arp
= XFS_BMBT_REC_ADDR(mp
, ablock
, 1 + cnt
);
747 xfs_bmbt_disk_set_all(arp
, &rec
);
750 ASSERT(cnt
== XFS_IFORK_NEXTENTS(ip
, whichfork
));
751 xfs_btree_set_numrecs(ablock
, cnt
);
754 * Fill in the root key and pointer.
756 kp
= XFS_BMBT_KEY_ADDR(mp
, block
, 1);
757 arp
= XFS_BMBT_REC_ADDR(mp
, ablock
, 1);
758 kp
->br_startoff
= cpu_to_be64(xfs_bmbt_disk_get_startoff(arp
));
759 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, xfs_bmbt_get_maxrecs(cur
,
760 be16_to_cpu(block
->bb_level
)));
761 *pp
= cpu_to_be64(args
.fsbno
);
764 * Do all this logging at the end so that
765 * the root is at the right level.
767 xfs_btree_log_block(cur
, abp
, XFS_BB_ALL_BITS
);
768 xfs_btree_log_recs(cur
, abp
, 1, be16_to_cpu(ablock
->bb_numrecs
));
769 ASSERT(*curp
== NULL
);
771 *logflagsp
= XFS_ILOG_CORE
| xfs_ilog_fbroot(whichfork
);
776 * Convert a local file to an extents file.
777 * This code is out of bounds for data forks of regular files,
778 * since the file data needs to get logged so things will stay consistent.
779 * (The bmap-level manipulations are ok, though).
782 xfs_bmap_local_to_extents_empty(
783 struct xfs_inode
*ip
,
786 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
788 ASSERT(whichfork
!= XFS_COW_FORK
);
789 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
790 ASSERT(ifp
->if_bytes
== 0);
791 ASSERT(XFS_IFORK_NEXTENTS(ip
, whichfork
) == 0);
793 xfs_bmap_forkoff_reset(ip
, whichfork
);
794 ifp
->if_flags
&= ~XFS_IFINLINE
;
795 ifp
->if_flags
|= XFS_IFEXTENTS
;
796 ifp
->if_u1
.if_root
= NULL
;
798 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_EXTENTS
);
802 STATIC
int /* error */
803 xfs_bmap_local_to_extents(
804 xfs_trans_t
*tp
, /* transaction pointer */
805 xfs_inode_t
*ip
, /* incore inode pointer */
806 xfs_fsblock_t
*firstblock
, /* first block allocated in xaction */
807 xfs_extlen_t total
, /* total blocks needed by transaction */
808 int *logflagsp
, /* inode logging flags */
810 void (*init_fn
)(struct xfs_trans
*tp
,
812 struct xfs_inode
*ip
,
813 struct xfs_ifork
*ifp
))
816 int flags
; /* logging flags returned */
817 xfs_ifork_t
*ifp
; /* inode fork pointer */
818 xfs_alloc_arg_t args
; /* allocation arguments */
819 xfs_buf_t
*bp
; /* buffer for extent block */
820 struct xfs_bmbt_irec rec
;
821 struct xfs_iext_cursor icur
;
824 * We don't want to deal with the case of keeping inode data inline yet.
825 * So sending the data fork of a regular inode is invalid.
827 ASSERT(!(S_ISREG(VFS_I(ip
)->i_mode
) && whichfork
== XFS_DATA_FORK
));
828 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
829 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
831 if (!ifp
->if_bytes
) {
832 xfs_bmap_local_to_extents_empty(ip
, whichfork
);
833 flags
= XFS_ILOG_CORE
;
839 ASSERT((ifp
->if_flags
& (XFS_IFINLINE
|XFS_IFEXTENTS
)) == XFS_IFINLINE
);
840 memset(&args
, 0, sizeof(args
));
842 args
.mp
= ip
->i_mount
;
843 xfs_rmap_ino_owner(&args
.oinfo
, ip
->i_ino
, whichfork
, 0);
844 args
.firstblock
= *firstblock
;
846 * Allocate a block. We know we need only one, since the
847 * file currently fits in an inode.
849 if (*firstblock
== NULLFSBLOCK
) {
850 args
.fsbno
= XFS_INO_TO_FSB(args
.mp
, ip
->i_ino
);
851 args
.type
= XFS_ALLOCTYPE_START_BNO
;
853 args
.fsbno
= *firstblock
;
854 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
857 args
.minlen
= args
.maxlen
= args
.prod
= 1;
858 error
= xfs_alloc_vextent(&args
);
862 /* Can't fail, the space was reserved. */
863 ASSERT(args
.fsbno
!= NULLFSBLOCK
);
864 ASSERT(args
.len
== 1);
865 *firstblock
= args
.fsbno
;
866 bp
= xfs_btree_get_bufl(args
.mp
, tp
, args
.fsbno
, 0);
869 * Initialize the block, copy the data and log the remote buffer.
871 * The callout is responsible for logging because the remote format
872 * might differ from the local format and thus we don't know how much to
873 * log here. Note that init_fn must also set the buffer log item type
876 init_fn(tp
, bp
, ip
, ifp
);
878 /* account for the change in fork size */
879 xfs_idata_realloc(ip
, -ifp
->if_bytes
, whichfork
);
880 xfs_bmap_local_to_extents_empty(ip
, whichfork
);
881 flags
|= XFS_ILOG_CORE
;
883 ifp
->if_u1
.if_root
= NULL
;
887 rec
.br_startblock
= args
.fsbno
;
888 rec
.br_blockcount
= 1;
889 rec
.br_state
= XFS_EXT_NORM
;
890 xfs_iext_first(ifp
, &icur
);
891 xfs_iext_insert(ip
, &icur
, &rec
, 0);
893 XFS_IFORK_NEXT_SET(ip
, whichfork
, 1);
894 ip
->i_d
.di_nblocks
= 1;
895 xfs_trans_mod_dquot_byino(tp
, ip
,
896 XFS_TRANS_DQ_BCOUNT
, 1L);
897 flags
|= xfs_ilog_fext(whichfork
);
905 * Called from xfs_bmap_add_attrfork to handle btree format files.
907 STATIC
int /* error */
908 xfs_bmap_add_attrfork_btree(
909 xfs_trans_t
*tp
, /* transaction pointer */
910 xfs_inode_t
*ip
, /* incore inode pointer */
911 xfs_fsblock_t
*firstblock
, /* first block allocated */
912 struct xfs_defer_ops
*dfops
, /* blocks to free at commit */
913 int *flags
) /* inode logging flags */
915 xfs_btree_cur_t
*cur
; /* btree cursor */
916 int error
; /* error return value */
917 xfs_mount_t
*mp
; /* file system mount struct */
918 int stat
; /* newroot status */
921 if (ip
->i_df
.if_broot_bytes
<= XFS_IFORK_DSIZE(ip
))
922 *flags
|= XFS_ILOG_DBROOT
;
924 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, XFS_DATA_FORK
);
925 cur
->bc_private
.b
.dfops
= dfops
;
926 cur
->bc_private
.b
.firstblock
= *firstblock
;
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 *firstblock
= cur
->bc_private
.b
.firstblock
;
939 cur
->bc_private
.b
.allocated
= 0;
940 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
944 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
949 * Called from xfs_bmap_add_attrfork to handle extents format files.
951 STATIC
int /* error */
952 xfs_bmap_add_attrfork_extents(
953 xfs_trans_t
*tp
, /* transaction pointer */
954 xfs_inode_t
*ip
, /* incore inode pointer */
955 xfs_fsblock_t
*firstblock
, /* first block allocated */
956 struct xfs_defer_ops
*dfops
, /* blocks to free at commit */
957 int *flags
) /* inode logging flags */
959 xfs_btree_cur_t
*cur
; /* bmap btree cursor */
960 int error
; /* error return value */
962 if (ip
->i_d
.di_nextents
* sizeof(xfs_bmbt_rec_t
) <= XFS_IFORK_DSIZE(ip
))
965 error
= xfs_bmap_extents_to_btree(tp
, ip
, firstblock
, dfops
, &cur
, 0,
966 flags
, XFS_DATA_FORK
);
968 cur
->bc_private
.b
.allocated
= 0;
969 xfs_btree_del_cursor(cur
,
970 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
976 * Called from xfs_bmap_add_attrfork to handle local format files. Each
977 * different data fork content type needs a different callout to do the
978 * conversion. Some are basic and only require special block initialisation
979 * callouts for the data formating, others (directories) are so specialised they
980 * handle everything themselves.
982 * XXX (dgc): investigate whether directory conversion can use the generic
983 * formatting callout. It should be possible - it's just a very complex
986 STATIC
int /* error */
987 xfs_bmap_add_attrfork_local(
988 xfs_trans_t
*tp
, /* transaction pointer */
989 xfs_inode_t
*ip
, /* incore inode pointer */
990 xfs_fsblock_t
*firstblock
, /* first block allocated */
991 struct xfs_defer_ops
*dfops
, /* blocks to free at commit */
992 int *flags
) /* inode logging flags */
994 xfs_da_args_t dargs
; /* args for dir/attr code */
996 if (ip
->i_df
.if_bytes
<= XFS_IFORK_DSIZE(ip
))
999 if (S_ISDIR(VFS_I(ip
)->i_mode
)) {
1000 memset(&dargs
, 0, sizeof(dargs
));
1001 dargs
.geo
= ip
->i_mount
->m_dir_geo
;
1003 dargs
.firstblock
= firstblock
;
1004 dargs
.dfops
= dfops
;
1005 dargs
.total
= dargs
.geo
->fsbcount
;
1006 dargs
.whichfork
= XFS_DATA_FORK
;
1008 return xfs_dir2_sf_to_block(&dargs
);
1011 if (S_ISLNK(VFS_I(ip
)->i_mode
))
1012 return xfs_bmap_local_to_extents(tp
, ip
, firstblock
, 1,
1013 flags
, XFS_DATA_FORK
,
1014 xfs_symlink_local_to_remote
);
1016 /* should only be called for types that support local format data */
1018 return -EFSCORRUPTED
;
1022 * Convert inode from non-attributed to attributed.
1023 * Must not be in a transaction, ip must not be locked.
1025 int /* error code */
1026 xfs_bmap_add_attrfork(
1027 xfs_inode_t
*ip
, /* incore inode pointer */
1028 int size
, /* space new attribute needs */
1029 int rsvd
) /* xact may use reserved blks */
1031 xfs_fsblock_t firstblock
; /* 1st block/ag allocated */
1032 struct xfs_defer_ops dfops
; /* freed extent records */
1033 xfs_mount_t
*mp
; /* mount structure */
1034 xfs_trans_t
*tp
; /* transaction pointer */
1035 int blks
; /* space reservation */
1036 int version
= 1; /* superblock attr version */
1037 int logflags
; /* logging flags */
1038 int error
; /* error return value */
1040 ASSERT(XFS_IFORK_Q(ip
) == 0);
1043 ASSERT(!XFS_NOT_DQATTACHED(mp
, ip
));
1045 blks
= XFS_ADDAFORK_SPACE_RES(mp
);
1047 error
= xfs_trans_alloc(mp
, &M_RES(mp
)->tr_addafork
, blks
, 0,
1048 rsvd
? XFS_TRANS_RESERVE
: 0, &tp
);
1052 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
1053 error
= xfs_trans_reserve_quota_nblks(tp
, ip
, blks
, 0, rsvd
?
1054 XFS_QMOPT_RES_REGBLKS
| XFS_QMOPT_FORCE_RES
:
1055 XFS_QMOPT_RES_REGBLKS
);
1058 if (XFS_IFORK_Q(ip
))
1060 if (ip
->i_d
.di_anextents
!= 0) {
1061 error
= -EFSCORRUPTED
;
1064 if (ip
->i_d
.di_aformat
!= XFS_DINODE_FMT_EXTENTS
) {
1066 * For inodes coming from pre-6.2 filesystems.
1068 ASSERT(ip
->i_d
.di_aformat
== 0);
1069 ip
->i_d
.di_aformat
= XFS_DINODE_FMT_EXTENTS
;
1072 xfs_trans_ijoin(tp
, ip
, 0);
1073 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
1075 switch (ip
->i_d
.di_format
) {
1076 case XFS_DINODE_FMT_DEV
:
1077 ip
->i_d
.di_forkoff
= roundup(sizeof(xfs_dev_t
), 8) >> 3;
1079 case XFS_DINODE_FMT_LOCAL
:
1080 case XFS_DINODE_FMT_EXTENTS
:
1081 case XFS_DINODE_FMT_BTREE
:
1082 ip
->i_d
.di_forkoff
= xfs_attr_shortform_bytesfit(ip
, size
);
1083 if (!ip
->i_d
.di_forkoff
)
1084 ip
->i_d
.di_forkoff
= xfs_default_attroffset(ip
) >> 3;
1085 else if (mp
->m_flags
& XFS_MOUNT_ATTR2
)
1094 ASSERT(ip
->i_afp
== NULL
);
1095 ip
->i_afp
= kmem_zone_zalloc(xfs_ifork_zone
, KM_SLEEP
);
1096 ip
->i_afp
->if_flags
= XFS_IFEXTENTS
;
1098 xfs_defer_init(&dfops
, &firstblock
);
1099 switch (ip
->i_d
.di_format
) {
1100 case XFS_DINODE_FMT_LOCAL
:
1101 error
= xfs_bmap_add_attrfork_local(tp
, ip
, &firstblock
, &dfops
,
1104 case XFS_DINODE_FMT_EXTENTS
:
1105 error
= xfs_bmap_add_attrfork_extents(tp
, ip
, &firstblock
,
1108 case XFS_DINODE_FMT_BTREE
:
1109 error
= xfs_bmap_add_attrfork_btree(tp
, ip
, &firstblock
, &dfops
,
1117 xfs_trans_log_inode(tp
, ip
, logflags
);
1120 if (!xfs_sb_version_hasattr(&mp
->m_sb
) ||
1121 (!xfs_sb_version_hasattr2(&mp
->m_sb
) && version
== 2)) {
1122 bool log_sb
= false;
1124 spin_lock(&mp
->m_sb_lock
);
1125 if (!xfs_sb_version_hasattr(&mp
->m_sb
)) {
1126 xfs_sb_version_addattr(&mp
->m_sb
);
1129 if (!xfs_sb_version_hasattr2(&mp
->m_sb
) && version
== 2) {
1130 xfs_sb_version_addattr2(&mp
->m_sb
);
1133 spin_unlock(&mp
->m_sb_lock
);
1138 error
= xfs_defer_finish(&tp
, &dfops
);
1141 error
= xfs_trans_commit(tp
);
1142 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
1146 xfs_defer_cancel(&dfops
);
1148 xfs_trans_cancel(tp
);
1149 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
1154 * Internal and external extent tree search functions.
1158 * Read in extents from a btree-format inode.
1162 struct xfs_trans
*tp
,
1163 struct xfs_inode
*ip
,
1166 struct xfs_mount
*mp
= ip
->i_mount
;
1167 int state
= xfs_bmap_fork_to_state(whichfork
);
1168 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1169 xfs_extnum_t nextents
= XFS_IFORK_NEXTENTS(ip
, whichfork
);
1170 struct xfs_btree_block
*block
= ifp
->if_broot
;
1171 struct xfs_iext_cursor icur
;
1172 struct xfs_bmbt_irec
new;
1180 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
1182 if (unlikely(XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)) {
1183 XFS_ERROR_REPORT(__func__
, XFS_ERRLEVEL_LOW
, mp
);
1184 return -EFSCORRUPTED
;
1188 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
1190 level
= be16_to_cpu(block
->bb_level
);
1192 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, 1, ifp
->if_broot_bytes
);
1193 bno
= be64_to_cpu(*pp
);
1196 * Go down the tree until leaf level is reached, following the first
1197 * pointer (leftmost) at each level.
1199 while (level
-- > 0) {
1200 error
= xfs_btree_read_bufl(mp
, tp
, bno
, 0, &bp
,
1201 XFS_BMAP_BTREE_REF
, &xfs_bmbt_buf_ops
);
1204 block
= XFS_BUF_TO_BLOCK(bp
);
1207 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, mp
->m_bmap_dmxr
[1]);
1208 bno
= be64_to_cpu(*pp
);
1209 XFS_WANT_CORRUPTED_GOTO(mp
,
1210 xfs_verify_fsbno(mp
, bno
), out_brelse
);
1211 xfs_trans_brelse(tp
, bp
);
1215 * Here with bp and block set to the leftmost leaf node in the tree.
1218 xfs_iext_first(ifp
, &icur
);
1221 * Loop over all leaf nodes. Copy information to the extent records.
1224 xfs_bmbt_rec_t
*frp
;
1225 xfs_fsblock_t nextbno
;
1226 xfs_extnum_t num_recs
;
1228 num_recs
= xfs_btree_get_numrecs(block
);
1229 if (unlikely(i
+ num_recs
> nextents
)) {
1230 xfs_warn(ip
->i_mount
,
1231 "corrupt dinode %Lu, (btree extents).",
1232 (unsigned long long) ip
->i_ino
);
1233 xfs_inode_verifier_error(ip
, -EFSCORRUPTED
,
1234 __func__
, block
, sizeof(*block
),
1236 error
= -EFSCORRUPTED
;
1240 * Read-ahead the next leaf block, if any.
1242 nextbno
= be64_to_cpu(block
->bb_u
.l
.bb_rightsib
);
1243 if (nextbno
!= NULLFSBLOCK
)
1244 xfs_btree_reada_bufl(mp
, nextbno
, 1,
1247 * Copy records into the extent records.
1249 frp
= XFS_BMBT_REC_ADDR(mp
, block
, 1);
1250 for (j
= 0; j
< num_recs
; j
++, frp
++, i
++) {
1253 xfs_bmbt_disk_get_all(frp
, &new);
1254 fa
= xfs_bmap_validate_extent(ip
, whichfork
, &new);
1256 error
= -EFSCORRUPTED
;
1257 xfs_inode_verifier_error(ip
, error
,
1258 "xfs_iread_extents(2)",
1259 frp
, sizeof(*frp
), fa
);
1262 xfs_iext_insert(ip
, &icur
, &new, state
);
1263 trace_xfs_read_extent(ip
, &icur
, state
, _THIS_IP_
);
1264 xfs_iext_next(ifp
, &icur
);
1266 xfs_trans_brelse(tp
, bp
);
1269 * If we've reached the end, stop.
1271 if (bno
== NULLFSBLOCK
)
1273 error
= xfs_btree_read_bufl(mp
, tp
, bno
, 0, &bp
,
1274 XFS_BMAP_BTREE_REF
, &xfs_bmbt_buf_ops
);
1277 block
= XFS_BUF_TO_BLOCK(bp
);
1280 if (i
!= XFS_IFORK_NEXTENTS(ip
, whichfork
)) {
1281 error
= -EFSCORRUPTED
;
1284 ASSERT(i
== xfs_iext_count(ifp
));
1286 ifp
->if_flags
|= XFS_IFEXTENTS
;
1290 xfs_trans_brelse(tp
, bp
);
1292 xfs_iext_destroy(ifp
);
1297 * Returns the relative block number of the first unused block(s) in the given
1298 * fork with at least "len" logically contiguous blocks free. This is the
1299 * lowest-address hole if the fork has holes, else the first block past the end
1300 * of fork. Return 0 if the fork is currently local (in-inode).
1303 xfs_bmap_first_unused(
1304 struct xfs_trans
*tp
, /* transaction pointer */
1305 struct xfs_inode
*ip
, /* incore inode */
1306 xfs_extlen_t len
, /* size of hole to find */
1307 xfs_fileoff_t
*first_unused
, /* unused block */
1308 int whichfork
) /* data or attr fork */
1310 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1311 struct xfs_bmbt_irec got
;
1312 struct xfs_iext_cursor icur
;
1313 xfs_fileoff_t lastaddr
= 0;
1314 xfs_fileoff_t lowest
, max
;
1317 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
||
1318 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
||
1319 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
1321 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
) {
1326 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
1327 error
= xfs_iread_extents(tp
, ip
, whichfork
);
1332 lowest
= max
= *first_unused
;
1333 for_each_xfs_iext(ifp
, &icur
, &got
) {
1335 * See if the hole before this extent will work.
1337 if (got
.br_startoff
>= lowest
+ len
&&
1338 got
.br_startoff
- max
>= len
)
1340 lastaddr
= got
.br_startoff
+ got
.br_blockcount
;
1341 max
= XFS_FILEOFF_MAX(lastaddr
, lowest
);
1344 *first_unused
= max
;
1349 * Returns the file-relative block number of the last block - 1 before
1350 * last_block (input value) in the file.
1351 * This is not based on i_size, it is based on the extent records.
1352 * Returns 0 for local files, as they do not have extent records.
1355 xfs_bmap_last_before(
1356 struct xfs_trans
*tp
, /* transaction pointer */
1357 struct xfs_inode
*ip
, /* incore inode */
1358 xfs_fileoff_t
*last_block
, /* last block */
1359 int whichfork
) /* data or attr fork */
1361 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1362 struct xfs_bmbt_irec got
;
1363 struct xfs_iext_cursor icur
;
1366 switch (XFS_IFORK_FORMAT(ip
, whichfork
)) {
1367 case XFS_DINODE_FMT_LOCAL
:
1370 case XFS_DINODE_FMT_BTREE
:
1371 case XFS_DINODE_FMT_EXTENTS
:
1377 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
1378 error
= xfs_iread_extents(tp
, ip
, whichfork
);
1383 if (!xfs_iext_lookup_extent_before(ip
, ifp
, last_block
, &icur
, &got
))
1389 xfs_bmap_last_extent(
1390 struct xfs_trans
*tp
,
1391 struct xfs_inode
*ip
,
1393 struct xfs_bmbt_irec
*rec
,
1396 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1397 struct xfs_iext_cursor icur
;
1400 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
1401 error
= xfs_iread_extents(tp
, ip
, whichfork
);
1406 xfs_iext_last(ifp
, &icur
);
1407 if (!xfs_iext_get_extent(ifp
, &icur
, rec
))
1415 * Check the last inode extent to determine whether this allocation will result
1416 * in blocks being allocated at the end of the file. When we allocate new data
1417 * blocks at the end of the file which do not start at the previous data block,
1418 * we will try to align the new blocks at stripe unit boundaries.
1420 * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be
1421 * at, or past the EOF.
1425 struct xfs_bmalloca
*bma
,
1428 struct xfs_bmbt_irec rec
;
1433 error
= xfs_bmap_last_extent(NULL
, bma
->ip
, whichfork
, &rec
,
1444 * Check if we are allocation or past the last extent, or at least into
1445 * the last delayed allocated extent.
1447 bma
->aeof
= bma
->offset
>= rec
.br_startoff
+ rec
.br_blockcount
||
1448 (bma
->offset
>= rec
.br_startoff
&&
1449 isnullstartblock(rec
.br_startblock
));
1454 * Returns the file-relative block number of the first block past eof in
1455 * the file. This is not based on i_size, it is based on the extent records.
1456 * Returns 0 for local files, as they do not have extent records.
1459 xfs_bmap_last_offset(
1460 struct xfs_inode
*ip
,
1461 xfs_fileoff_t
*last_block
,
1464 struct xfs_bmbt_irec rec
;
1470 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
)
1473 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
&&
1474 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
1477 error
= xfs_bmap_last_extent(NULL
, ip
, whichfork
, &rec
, &is_empty
);
1478 if (error
|| is_empty
)
1481 *last_block
= rec
.br_startoff
+ rec
.br_blockcount
;
1486 * Returns whether the selected fork of the inode has exactly one
1487 * block or not. For the data fork we check this matches di_size,
1488 * implying the file's range is 0..bsize-1.
1490 int /* 1=>1 block, 0=>otherwise */
1492 xfs_inode_t
*ip
, /* incore inode */
1493 int whichfork
) /* data or attr fork */
1495 xfs_ifork_t
*ifp
; /* inode fork pointer */
1496 int rval
; /* return value */
1497 xfs_bmbt_irec_t s
; /* internal version of extent */
1498 struct xfs_iext_cursor icur
;
1501 if (whichfork
== XFS_DATA_FORK
)
1502 return XFS_ISIZE(ip
) == ip
->i_mount
->m_sb
.sb_blocksize
;
1504 if (XFS_IFORK_NEXTENTS(ip
, whichfork
) != 1)
1506 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
1508 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1509 ASSERT(ifp
->if_flags
& XFS_IFEXTENTS
);
1510 xfs_iext_first(ifp
, &icur
);
1511 xfs_iext_get_extent(ifp
, &icur
, &s
);
1512 rval
= s
.br_startoff
== 0 && s
.br_blockcount
== 1;
1513 if (rval
&& whichfork
== XFS_DATA_FORK
)
1514 ASSERT(XFS_ISIZE(ip
) == ip
->i_mount
->m_sb
.sb_blocksize
);
1519 * Extent tree manipulation functions used during allocation.
1523 * Convert a delayed allocation to a real allocation.
1525 STATIC
int /* error */
1526 xfs_bmap_add_extent_delay_real(
1527 struct xfs_bmalloca
*bma
,
1530 struct xfs_bmbt_irec
*new = &bma
->got
;
1531 int error
; /* error return value */
1532 int i
; /* temp state */
1533 xfs_ifork_t
*ifp
; /* inode fork pointer */
1534 xfs_fileoff_t new_endoff
; /* end offset of new entry */
1535 xfs_bmbt_irec_t r
[3]; /* neighbor extent entries */
1536 /* left is 0, right is 1, prev is 2 */
1537 int rval
=0; /* return value (logging flags) */
1538 int state
= xfs_bmap_fork_to_state(whichfork
);
1539 xfs_filblks_t da_new
; /* new count del alloc blocks used */
1540 xfs_filblks_t da_old
; /* old count del alloc blocks used */
1541 xfs_filblks_t temp
=0; /* value for da_new calculations */
1542 int tmp_rval
; /* partial logging flags */
1543 struct xfs_mount
*mp
;
1544 xfs_extnum_t
*nextents
;
1545 struct xfs_bmbt_irec old
;
1547 mp
= bma
->ip
->i_mount
;
1548 ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
1549 ASSERT(whichfork
!= XFS_ATTR_FORK
);
1550 nextents
= (whichfork
== XFS_COW_FORK
? &bma
->ip
->i_cnextents
:
1551 &bma
->ip
->i_d
.di_nextents
);
1553 ASSERT(!isnullstartblock(new->br_startblock
));
1555 (bma
->cur
->bc_private
.b
.flags
& XFS_BTCUR_BPRV_WASDEL
));
1557 XFS_STATS_INC(mp
, xs_add_exlist
);
1564 * Set up a bunch of variables to make the tests simpler.
1566 xfs_iext_get_extent(ifp
, &bma
->icur
, &PREV
);
1567 new_endoff
= new->br_startoff
+ new->br_blockcount
;
1568 ASSERT(isnullstartblock(PREV
.br_startblock
));
1569 ASSERT(PREV
.br_startoff
<= new->br_startoff
);
1570 ASSERT(PREV
.br_startoff
+ PREV
.br_blockcount
>= new_endoff
);
1572 da_old
= startblockval(PREV
.br_startblock
);
1576 * Set flags determining what part of the previous delayed allocation
1577 * extent is being replaced by a real allocation.
1579 if (PREV
.br_startoff
== new->br_startoff
)
1580 state
|= BMAP_LEFT_FILLING
;
1581 if (PREV
.br_startoff
+ PREV
.br_blockcount
== new_endoff
)
1582 state
|= BMAP_RIGHT_FILLING
;
1585 * Check and set flags if this segment has a left neighbor.
1586 * Don't set contiguous if the combined extent would be too large.
1588 if (xfs_iext_peek_prev_extent(ifp
, &bma
->icur
, &LEFT
)) {
1589 state
|= BMAP_LEFT_VALID
;
1590 if (isnullstartblock(LEFT
.br_startblock
))
1591 state
|= BMAP_LEFT_DELAY
;
1594 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
1595 LEFT
.br_startoff
+ LEFT
.br_blockcount
== new->br_startoff
&&
1596 LEFT
.br_startblock
+ LEFT
.br_blockcount
== new->br_startblock
&&
1597 LEFT
.br_state
== new->br_state
&&
1598 LEFT
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
1599 state
|= BMAP_LEFT_CONTIG
;
1602 * Check and set flags if this segment has a right neighbor.
1603 * Don't set contiguous if the combined extent would be too large.
1604 * Also check for all-three-contiguous being too large.
1606 if (xfs_iext_peek_next_extent(ifp
, &bma
->icur
, &RIGHT
)) {
1607 state
|= BMAP_RIGHT_VALID
;
1608 if (isnullstartblock(RIGHT
.br_startblock
))
1609 state
|= BMAP_RIGHT_DELAY
;
1612 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
1613 new_endoff
== RIGHT
.br_startoff
&&
1614 new->br_startblock
+ new->br_blockcount
== RIGHT
.br_startblock
&&
1615 new->br_state
== RIGHT
.br_state
&&
1616 new->br_blockcount
+ RIGHT
.br_blockcount
<= MAXEXTLEN
&&
1617 ((state
& (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
1618 BMAP_RIGHT_FILLING
)) !=
1619 (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
1620 BMAP_RIGHT_FILLING
) ||
1621 LEFT
.br_blockcount
+ new->br_blockcount
+ RIGHT
.br_blockcount
1623 state
|= BMAP_RIGHT_CONTIG
;
1627 * Switch out based on the FILLING and CONTIG state bits.
1629 switch (state
& (BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
1630 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
)) {
1631 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
1632 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
1634 * Filling in all of a previously delayed allocation extent.
1635 * The left and right neighbors are both contiguous with new.
1637 LEFT
.br_blockcount
+= PREV
.br_blockcount
+ RIGHT
.br_blockcount
;
1639 xfs_iext_remove(bma
->ip
, &bma
->icur
, state
);
1640 xfs_iext_remove(bma
->ip
, &bma
->icur
, state
);
1641 xfs_iext_prev(ifp
, &bma
->icur
);
1642 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &LEFT
);
1645 if (bma
->cur
== NULL
)
1646 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1648 rval
= XFS_ILOG_CORE
;
1649 error
= xfs_bmbt_lookup_eq(bma
->cur
, &RIGHT
, &i
);
1652 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1653 error
= xfs_btree_delete(bma
->cur
, &i
);
1656 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1657 error
= xfs_btree_decrement(bma
->cur
, 0, &i
);
1660 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1661 error
= xfs_bmbt_update(bma
->cur
, &LEFT
);
1667 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
1669 * Filling in all of a previously delayed allocation extent.
1670 * The left neighbor is contiguous, the right is not.
1673 LEFT
.br_blockcount
+= PREV
.br_blockcount
;
1675 xfs_iext_remove(bma
->ip
, &bma
->icur
, state
);
1676 xfs_iext_prev(ifp
, &bma
->icur
);
1677 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &LEFT
);
1679 if (bma
->cur
== NULL
)
1680 rval
= XFS_ILOG_DEXT
;
1683 error
= xfs_bmbt_lookup_eq(bma
->cur
, &old
, &i
);
1686 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1687 error
= xfs_bmbt_update(bma
->cur
, &LEFT
);
1693 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
1695 * Filling in all of a previously delayed allocation extent.
1696 * The right neighbor is contiguous, the left is not.
1698 PREV
.br_startblock
= new->br_startblock
;
1699 PREV
.br_blockcount
+= RIGHT
.br_blockcount
;
1701 xfs_iext_next(ifp
, &bma
->icur
);
1702 xfs_iext_remove(bma
->ip
, &bma
->icur
, state
);
1703 xfs_iext_prev(ifp
, &bma
->icur
);
1704 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &PREV
);
1706 if (bma
->cur
== NULL
)
1707 rval
= XFS_ILOG_DEXT
;
1710 error
= xfs_bmbt_lookup_eq(bma
->cur
, &RIGHT
, &i
);
1713 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1714 error
= xfs_bmbt_update(bma
->cur
, &PREV
);
1720 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
1722 * Filling in all of a previously delayed allocation extent.
1723 * Neither the left nor right neighbors are contiguous with
1726 PREV
.br_startblock
= new->br_startblock
;
1727 PREV
.br_state
= new->br_state
;
1728 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &PREV
);
1731 if (bma
->cur
== NULL
)
1732 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1734 rval
= XFS_ILOG_CORE
;
1735 error
= xfs_bmbt_lookup_eq(bma
->cur
, new, &i
);
1738 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
1739 error
= xfs_btree_insert(bma
->cur
, &i
);
1742 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1746 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
:
1748 * Filling in the first part of a previous delayed allocation.
1749 * The left neighbor is contiguous.
1752 temp
= PREV
.br_blockcount
- new->br_blockcount
;
1753 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
1754 startblockval(PREV
.br_startblock
));
1756 LEFT
.br_blockcount
+= new->br_blockcount
;
1758 PREV
.br_blockcount
= temp
;
1759 PREV
.br_startoff
+= new->br_blockcount
;
1760 PREV
.br_startblock
= nullstartblock(da_new
);
1762 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &PREV
);
1763 xfs_iext_prev(ifp
, &bma
->icur
);
1764 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &LEFT
);
1766 if (bma
->cur
== NULL
)
1767 rval
= XFS_ILOG_DEXT
;
1770 error
= xfs_bmbt_lookup_eq(bma
->cur
, &old
, &i
);
1773 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1774 error
= xfs_bmbt_update(bma
->cur
, &LEFT
);
1780 case BMAP_LEFT_FILLING
:
1782 * Filling in the first part of a previous delayed allocation.
1783 * The left neighbor is not contiguous.
1785 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, new);
1787 if (bma
->cur
== NULL
)
1788 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1790 rval
= XFS_ILOG_CORE
;
1791 error
= xfs_bmbt_lookup_eq(bma
->cur
, new, &i
);
1794 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
1795 error
= xfs_btree_insert(bma
->cur
, &i
);
1798 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1801 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
1802 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
1803 bma
->firstblock
, bma
->dfops
,
1804 &bma
->cur
, 1, &tmp_rval
, whichfork
);
1810 temp
= PREV
.br_blockcount
- new->br_blockcount
;
1811 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
1812 startblockval(PREV
.br_startblock
) -
1813 (bma
->cur
? bma
->cur
->bc_private
.b
.allocated
: 0));
1815 PREV
.br_startoff
= new_endoff
;
1816 PREV
.br_blockcount
= temp
;
1817 PREV
.br_startblock
= nullstartblock(da_new
);
1818 xfs_iext_next(ifp
, &bma
->icur
);
1819 xfs_iext_insert(bma
->ip
, &bma
->icur
, &PREV
, state
);
1820 xfs_iext_prev(ifp
, &bma
->icur
);
1823 case BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
1825 * Filling in the last part of a previous delayed allocation.
1826 * The right neighbor is contiguous with the new allocation.
1829 RIGHT
.br_startoff
= new->br_startoff
;
1830 RIGHT
.br_startblock
= new->br_startblock
;
1831 RIGHT
.br_blockcount
+= new->br_blockcount
;
1833 if (bma
->cur
== NULL
)
1834 rval
= XFS_ILOG_DEXT
;
1837 error
= xfs_bmbt_lookup_eq(bma
->cur
, &old
, &i
);
1840 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1841 error
= xfs_bmbt_update(bma
->cur
, &RIGHT
);
1846 temp
= PREV
.br_blockcount
- new->br_blockcount
;
1847 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
1848 startblockval(PREV
.br_startblock
));
1850 PREV
.br_blockcount
= temp
;
1851 PREV
.br_startblock
= nullstartblock(da_new
);
1853 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &PREV
);
1854 xfs_iext_next(ifp
, &bma
->icur
);
1855 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &RIGHT
);
1858 case BMAP_RIGHT_FILLING
:
1860 * Filling in the last part of a previous delayed allocation.
1861 * The right neighbor is not contiguous.
1863 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, new);
1865 if (bma
->cur
== NULL
)
1866 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1868 rval
= XFS_ILOG_CORE
;
1869 error
= xfs_bmbt_lookup_eq(bma
->cur
, new, &i
);
1872 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
1873 error
= xfs_btree_insert(bma
->cur
, &i
);
1876 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1879 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
1880 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
1881 bma
->firstblock
, bma
->dfops
, &bma
->cur
, 1,
1882 &tmp_rval
, whichfork
);
1888 temp
= PREV
.br_blockcount
- new->br_blockcount
;
1889 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
1890 startblockval(PREV
.br_startblock
) -
1891 (bma
->cur
? bma
->cur
->bc_private
.b
.allocated
: 0));
1893 PREV
.br_startblock
= nullstartblock(da_new
);
1894 PREV
.br_blockcount
= temp
;
1895 xfs_iext_insert(bma
->ip
, &bma
->icur
, &PREV
, state
);
1896 xfs_iext_next(ifp
, &bma
->icur
);
1901 * Filling in the middle part of a previous delayed allocation.
1902 * Contiguity is impossible here.
1903 * This case is avoided almost all the time.
1905 * We start with a delayed allocation:
1907 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
1910 * and we are allocating:
1911 * +rrrrrrrrrrrrrrrrr+
1914 * and we set it up for insertion as:
1915 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
1917 * PREV @ idx LEFT RIGHT
1918 * inserted at idx + 1
1922 /* LEFT is the new middle */
1925 /* RIGHT is the new right */
1926 RIGHT
.br_state
= PREV
.br_state
;
1927 RIGHT
.br_startoff
= new_endoff
;
1928 RIGHT
.br_blockcount
=
1929 PREV
.br_startoff
+ PREV
.br_blockcount
- new_endoff
;
1930 RIGHT
.br_startblock
=
1931 nullstartblock(xfs_bmap_worst_indlen(bma
->ip
,
1932 RIGHT
.br_blockcount
));
1935 PREV
.br_blockcount
= new->br_startoff
- PREV
.br_startoff
;
1936 PREV
.br_startblock
=
1937 nullstartblock(xfs_bmap_worst_indlen(bma
->ip
,
1938 PREV
.br_blockcount
));
1939 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &PREV
);
1941 xfs_iext_next(ifp
, &bma
->icur
);
1942 xfs_iext_insert(bma
->ip
, &bma
->icur
, &RIGHT
, state
);
1943 xfs_iext_insert(bma
->ip
, &bma
->icur
, &LEFT
, state
);
1946 if (bma
->cur
== NULL
)
1947 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1949 rval
= XFS_ILOG_CORE
;
1950 error
= xfs_bmbt_lookup_eq(bma
->cur
, new, &i
);
1953 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
1954 error
= xfs_btree_insert(bma
->cur
, &i
);
1957 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1960 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
1961 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
1962 bma
->firstblock
, bma
->dfops
, &bma
->cur
,
1963 1, &tmp_rval
, whichfork
);
1969 da_new
= startblockval(PREV
.br_startblock
) +
1970 startblockval(RIGHT
.br_startblock
);
1973 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
1974 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
1975 case BMAP_LEFT_FILLING
| BMAP_RIGHT_CONTIG
:
1976 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
1977 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
1978 case BMAP_LEFT_CONTIG
:
1979 case BMAP_RIGHT_CONTIG
:
1981 * These cases are all impossible.
1986 /* add reverse mapping unless caller opted out */
1987 if (!(bma
->flags
& XFS_BMAPI_NORMAP
)) {
1988 error
= xfs_rmap_map_extent(mp
, bma
->dfops
, bma
->ip
,
1994 /* convert to a btree if necessary */
1995 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
1996 int tmp_logflags
; /* partial log flag return val */
1998 ASSERT(bma
->cur
== NULL
);
1999 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
2000 bma
->firstblock
, bma
->dfops
, &bma
->cur
,
2001 da_old
> 0, &tmp_logflags
, whichfork
);
2002 bma
->logflags
|= tmp_logflags
;
2008 da_new
+= bma
->cur
->bc_private
.b
.allocated
;
2009 bma
->cur
->bc_private
.b
.allocated
= 0;
2012 /* adjust for changes in reserved delayed indirect blocks */
2013 if (da_new
!= da_old
) {
2014 ASSERT(state
== 0 || da_new
< da_old
);
2015 error
= xfs_mod_fdblocks(mp
, (int64_t)(da_old
- da_new
),
2019 xfs_bmap_check_leaf_extents(bma
->cur
, bma
->ip
, whichfork
);
2021 if (whichfork
!= XFS_COW_FORK
)
2022 bma
->logflags
|= rval
;
2030 * Convert an unwritten allocation to a real allocation or vice versa.
2032 STATIC
int /* error */
2033 xfs_bmap_add_extent_unwritten_real(
2034 struct xfs_trans
*tp
,
2035 xfs_inode_t
*ip
, /* incore inode pointer */
2037 struct xfs_iext_cursor
*icur
,
2038 xfs_btree_cur_t
**curp
, /* if *curp is null, not a btree */
2039 xfs_bmbt_irec_t
*new, /* new data to add to file extents */
2040 xfs_fsblock_t
*first
, /* pointer to firstblock variable */
2041 struct xfs_defer_ops
*dfops
, /* list of extents to be freed */
2042 int *logflagsp
) /* inode logging flags */
2044 xfs_btree_cur_t
*cur
; /* btree cursor */
2045 int error
; /* error return value */
2046 int i
; /* temp state */
2047 xfs_ifork_t
*ifp
; /* inode fork pointer */
2048 xfs_fileoff_t new_endoff
; /* end offset of new entry */
2049 xfs_bmbt_irec_t r
[3]; /* neighbor extent entries */
2050 /* left is 0, right is 1, prev is 2 */
2051 int rval
=0; /* return value (logging flags) */
2052 int state
= xfs_bmap_fork_to_state(whichfork
);
2053 struct xfs_mount
*mp
= ip
->i_mount
;
2054 struct xfs_bmbt_irec old
;
2059 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
2061 ASSERT(!isnullstartblock(new->br_startblock
));
2063 XFS_STATS_INC(mp
, xs_add_exlist
);
2070 * Set up a bunch of variables to make the tests simpler.
2073 xfs_iext_get_extent(ifp
, icur
, &PREV
);
2074 ASSERT(new->br_state
!= PREV
.br_state
);
2075 new_endoff
= new->br_startoff
+ new->br_blockcount
;
2076 ASSERT(PREV
.br_startoff
<= new->br_startoff
);
2077 ASSERT(PREV
.br_startoff
+ PREV
.br_blockcount
>= new_endoff
);
2080 * Set flags determining what part of the previous oldext allocation
2081 * extent is being replaced by a newext allocation.
2083 if (PREV
.br_startoff
== new->br_startoff
)
2084 state
|= BMAP_LEFT_FILLING
;
2085 if (PREV
.br_startoff
+ PREV
.br_blockcount
== new_endoff
)
2086 state
|= BMAP_RIGHT_FILLING
;
2089 * Check and set flags if this segment has a left neighbor.
2090 * Don't set contiguous if the combined extent would be too large.
2092 if (xfs_iext_peek_prev_extent(ifp
, icur
, &LEFT
)) {
2093 state
|= BMAP_LEFT_VALID
;
2094 if (isnullstartblock(LEFT
.br_startblock
))
2095 state
|= BMAP_LEFT_DELAY
;
2098 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
2099 LEFT
.br_startoff
+ LEFT
.br_blockcount
== new->br_startoff
&&
2100 LEFT
.br_startblock
+ LEFT
.br_blockcount
== new->br_startblock
&&
2101 LEFT
.br_state
== new->br_state
&&
2102 LEFT
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
2103 state
|= BMAP_LEFT_CONTIG
;
2106 * Check and set flags if this segment has a right neighbor.
2107 * Don't set contiguous if the combined extent would be too large.
2108 * Also check for all-three-contiguous being too large.
2110 if (xfs_iext_peek_next_extent(ifp
, icur
, &RIGHT
)) {
2111 state
|= BMAP_RIGHT_VALID
;
2112 if (isnullstartblock(RIGHT
.br_startblock
))
2113 state
|= BMAP_RIGHT_DELAY
;
2116 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
2117 new_endoff
== RIGHT
.br_startoff
&&
2118 new->br_startblock
+ new->br_blockcount
== RIGHT
.br_startblock
&&
2119 new->br_state
== RIGHT
.br_state
&&
2120 new->br_blockcount
+ RIGHT
.br_blockcount
<= MAXEXTLEN
&&
2121 ((state
& (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
2122 BMAP_RIGHT_FILLING
)) !=
2123 (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
2124 BMAP_RIGHT_FILLING
) ||
2125 LEFT
.br_blockcount
+ new->br_blockcount
+ RIGHT
.br_blockcount
2127 state
|= BMAP_RIGHT_CONTIG
;
2130 * Switch out based on the FILLING and CONTIG state bits.
2132 switch (state
& (BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
2133 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
)) {
2134 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
2135 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2137 * Setting all of a previous oldext extent to newext.
2138 * The left and right neighbors are both contiguous with new.
2140 LEFT
.br_blockcount
+= PREV
.br_blockcount
+ RIGHT
.br_blockcount
;
2142 xfs_iext_remove(ip
, icur
, state
);
2143 xfs_iext_remove(ip
, icur
, state
);
2144 xfs_iext_prev(ifp
, icur
);
2145 xfs_iext_update_extent(ip
, state
, icur
, &LEFT
);
2146 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2147 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 2);
2149 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2151 rval
= XFS_ILOG_CORE
;
2152 error
= xfs_bmbt_lookup_eq(cur
, &RIGHT
, &i
);
2155 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2156 if ((error
= xfs_btree_delete(cur
, &i
)))
2158 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2159 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2161 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2162 if ((error
= xfs_btree_delete(cur
, &i
)))
2164 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2165 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2167 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2168 error
= xfs_bmbt_update(cur
, &LEFT
);
2174 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
2176 * Setting all of a previous oldext extent to newext.
2177 * The left neighbor is contiguous, the right is not.
2179 LEFT
.br_blockcount
+= PREV
.br_blockcount
;
2181 xfs_iext_remove(ip
, icur
, state
);
2182 xfs_iext_prev(ifp
, icur
);
2183 xfs_iext_update_extent(ip
, state
, icur
, &LEFT
);
2184 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2185 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
2187 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2189 rval
= XFS_ILOG_CORE
;
2190 error
= xfs_bmbt_lookup_eq(cur
, &PREV
, &i
);
2193 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2194 if ((error
= xfs_btree_delete(cur
, &i
)))
2196 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2197 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2199 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2200 error
= xfs_bmbt_update(cur
, &LEFT
);
2206 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2208 * Setting all of a previous oldext extent to newext.
2209 * The right neighbor is contiguous, the left is not.
2211 PREV
.br_blockcount
+= RIGHT
.br_blockcount
;
2212 PREV
.br_state
= new->br_state
;
2214 xfs_iext_next(ifp
, icur
);
2215 xfs_iext_remove(ip
, icur
, state
);
2216 xfs_iext_prev(ifp
, icur
);
2217 xfs_iext_update_extent(ip
, state
, icur
, &PREV
);
2219 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2220 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
2222 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2224 rval
= XFS_ILOG_CORE
;
2225 error
= xfs_bmbt_lookup_eq(cur
, &RIGHT
, &i
);
2228 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2229 if ((error
= xfs_btree_delete(cur
, &i
)))
2231 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2232 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2234 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2235 error
= xfs_bmbt_update(cur
, &PREV
);
2241 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
2243 * Setting all of a previous oldext extent to newext.
2244 * Neither the left nor right neighbors are contiguous with
2247 PREV
.br_state
= new->br_state
;
2248 xfs_iext_update_extent(ip
, state
, icur
, &PREV
);
2251 rval
= XFS_ILOG_DEXT
;
2254 error
= xfs_bmbt_lookup_eq(cur
, new, &i
);
2257 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2258 error
= xfs_bmbt_update(cur
, &PREV
);
2264 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
:
2266 * Setting the first part of a previous oldext extent to newext.
2267 * The left neighbor is contiguous.
2269 LEFT
.br_blockcount
+= new->br_blockcount
;
2272 PREV
.br_startoff
+= new->br_blockcount
;
2273 PREV
.br_startblock
+= new->br_blockcount
;
2274 PREV
.br_blockcount
-= new->br_blockcount
;
2276 xfs_iext_update_extent(ip
, state
, icur
, &PREV
);
2277 xfs_iext_prev(ifp
, icur
);
2278 xfs_iext_update_extent(ip
, state
, icur
, &LEFT
);
2281 rval
= XFS_ILOG_DEXT
;
2284 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2287 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2288 error
= xfs_bmbt_update(cur
, &PREV
);
2291 error
= xfs_btree_decrement(cur
, 0, &i
);
2294 error
= xfs_bmbt_update(cur
, &LEFT
);
2300 case BMAP_LEFT_FILLING
:
2302 * Setting the first part of a previous oldext extent to newext.
2303 * The left neighbor is not contiguous.
2306 PREV
.br_startoff
+= new->br_blockcount
;
2307 PREV
.br_startblock
+= new->br_blockcount
;
2308 PREV
.br_blockcount
-= new->br_blockcount
;
2310 xfs_iext_update_extent(ip
, state
, icur
, &PREV
);
2311 xfs_iext_insert(ip
, icur
, new, state
);
2312 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2313 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
2315 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2317 rval
= XFS_ILOG_CORE
;
2318 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2321 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2322 error
= xfs_bmbt_update(cur
, &PREV
);
2325 cur
->bc_rec
.b
= *new;
2326 if ((error
= xfs_btree_insert(cur
, &i
)))
2328 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2332 case BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2334 * Setting the last part of a previous oldext extent to newext.
2335 * The right neighbor is contiguous with the new allocation.
2338 PREV
.br_blockcount
-= new->br_blockcount
;
2340 RIGHT
.br_startoff
= new->br_startoff
;
2341 RIGHT
.br_startblock
= new->br_startblock
;
2342 RIGHT
.br_blockcount
+= new->br_blockcount
;
2344 xfs_iext_update_extent(ip
, state
, icur
, &PREV
);
2345 xfs_iext_next(ifp
, icur
);
2346 xfs_iext_update_extent(ip
, state
, icur
, &RIGHT
);
2349 rval
= XFS_ILOG_DEXT
;
2352 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2355 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2356 error
= xfs_bmbt_update(cur
, &PREV
);
2359 error
= xfs_btree_increment(cur
, 0, &i
);
2362 error
= xfs_bmbt_update(cur
, &RIGHT
);
2368 case BMAP_RIGHT_FILLING
:
2370 * Setting the last part of a previous oldext extent to newext.
2371 * The right neighbor is not contiguous.
2374 PREV
.br_blockcount
-= new->br_blockcount
;
2376 xfs_iext_update_extent(ip
, state
, icur
, &PREV
);
2377 xfs_iext_next(ifp
, icur
);
2378 xfs_iext_insert(ip
, icur
, new, state
);
2380 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2381 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
2383 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2385 rval
= XFS_ILOG_CORE
;
2386 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2389 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2390 error
= xfs_bmbt_update(cur
, &PREV
);
2393 error
= xfs_bmbt_lookup_eq(cur
, new, &i
);
2396 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
2397 if ((error
= xfs_btree_insert(cur
, &i
)))
2399 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2405 * Setting the middle part of a previous oldext extent to
2406 * newext. Contiguity is impossible here.
2407 * One extent becomes three extents.
2410 PREV
.br_blockcount
= new->br_startoff
- PREV
.br_startoff
;
2413 r
[1].br_startoff
= new_endoff
;
2414 r
[1].br_blockcount
=
2415 old
.br_startoff
+ old
.br_blockcount
- new_endoff
;
2416 r
[1].br_startblock
= new->br_startblock
+ new->br_blockcount
;
2417 r
[1].br_state
= PREV
.br_state
;
2419 xfs_iext_update_extent(ip
, state
, icur
, &PREV
);
2420 xfs_iext_next(ifp
, icur
);
2421 xfs_iext_insert(ip
, icur
, &r
[1], state
);
2422 xfs_iext_insert(ip
, icur
, &r
[0], state
);
2424 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2425 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 2);
2427 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2429 rval
= XFS_ILOG_CORE
;
2430 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2433 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2434 /* new right extent - oldext */
2435 error
= xfs_bmbt_update(cur
, &r
[1]);
2438 /* new left extent - oldext */
2439 cur
->bc_rec
.b
= PREV
;
2440 if ((error
= xfs_btree_insert(cur
, &i
)))
2442 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2444 * Reset the cursor to the position of the new extent
2445 * we are about to insert as we can't trust it after
2446 * the previous insert.
2448 error
= xfs_bmbt_lookup_eq(cur
, new, &i
);
2451 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
2452 /* new middle extent - newext */
2453 if ((error
= xfs_btree_insert(cur
, &i
)))
2455 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2459 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2460 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2461 case BMAP_LEFT_FILLING
| BMAP_RIGHT_CONTIG
:
2462 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
2463 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2464 case BMAP_LEFT_CONTIG
:
2465 case BMAP_RIGHT_CONTIG
:
2467 * These cases are all impossible.
2472 /* update reverse mappings */
2473 error
= xfs_rmap_convert_extent(mp
, dfops
, ip
, whichfork
, new);
2477 /* convert to a btree if necessary */
2478 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
2479 int tmp_logflags
; /* partial log flag return val */
2481 ASSERT(cur
== NULL
);
2482 error
= xfs_bmap_extents_to_btree(tp
, ip
, first
, dfops
, &cur
,
2483 0, &tmp_logflags
, whichfork
);
2484 *logflagsp
|= tmp_logflags
;
2489 /* clear out the allocated field, done with it now in any case. */
2491 cur
->bc_private
.b
.allocated
= 0;
2495 xfs_bmap_check_leaf_extents(*curp
, ip
, whichfork
);
2505 * Convert a hole to a delayed allocation.
2508 xfs_bmap_add_extent_hole_delay(
2509 xfs_inode_t
*ip
, /* incore inode pointer */
2511 struct xfs_iext_cursor
*icur
,
2512 xfs_bmbt_irec_t
*new) /* new data to add to file extents */
2514 xfs_ifork_t
*ifp
; /* inode fork pointer */
2515 xfs_bmbt_irec_t left
; /* left neighbor extent entry */
2516 xfs_filblks_t newlen
=0; /* new indirect size */
2517 xfs_filblks_t oldlen
=0; /* old indirect size */
2518 xfs_bmbt_irec_t right
; /* right neighbor extent entry */
2519 int state
= xfs_bmap_fork_to_state(whichfork
);
2520 xfs_filblks_t temp
; /* temp for indirect calculations */
2522 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
2523 ASSERT(isnullstartblock(new->br_startblock
));
2526 * Check and set flags if this segment has a left neighbor
2528 if (xfs_iext_peek_prev_extent(ifp
, icur
, &left
)) {
2529 state
|= BMAP_LEFT_VALID
;
2530 if (isnullstartblock(left
.br_startblock
))
2531 state
|= BMAP_LEFT_DELAY
;
2535 * Check and set flags if the current (right) segment exists.
2536 * If it doesn't exist, we're converting the hole at end-of-file.
2538 if (xfs_iext_get_extent(ifp
, icur
, &right
)) {
2539 state
|= BMAP_RIGHT_VALID
;
2540 if (isnullstartblock(right
.br_startblock
))
2541 state
|= BMAP_RIGHT_DELAY
;
2545 * Set contiguity flags on the left and right neighbors.
2546 * Don't let extents get too large, even if the pieces are contiguous.
2548 if ((state
& BMAP_LEFT_VALID
) && (state
& BMAP_LEFT_DELAY
) &&
2549 left
.br_startoff
+ left
.br_blockcount
== new->br_startoff
&&
2550 left
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
2551 state
|= BMAP_LEFT_CONTIG
;
2553 if ((state
& BMAP_RIGHT_VALID
) && (state
& BMAP_RIGHT_DELAY
) &&
2554 new->br_startoff
+ new->br_blockcount
== right
.br_startoff
&&
2555 new->br_blockcount
+ right
.br_blockcount
<= MAXEXTLEN
&&
2556 (!(state
& BMAP_LEFT_CONTIG
) ||
2557 (left
.br_blockcount
+ new->br_blockcount
+
2558 right
.br_blockcount
<= MAXEXTLEN
)))
2559 state
|= BMAP_RIGHT_CONTIG
;
2562 * Switch out based on the contiguity flags.
2564 switch (state
& (BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
)) {
2565 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2567 * New allocation is contiguous with delayed allocations
2568 * on the left and on the right.
2569 * Merge all three into a single extent record.
2571 temp
= left
.br_blockcount
+ new->br_blockcount
+
2572 right
.br_blockcount
;
2574 oldlen
= startblockval(left
.br_startblock
) +
2575 startblockval(new->br_startblock
) +
2576 startblockval(right
.br_startblock
);
2577 newlen
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
2579 left
.br_startblock
= nullstartblock(newlen
);
2580 left
.br_blockcount
= temp
;
2582 xfs_iext_remove(ip
, icur
, state
);
2583 xfs_iext_prev(ifp
, icur
);
2584 xfs_iext_update_extent(ip
, state
, icur
, &left
);
2587 case BMAP_LEFT_CONTIG
:
2589 * New allocation is contiguous with a delayed allocation
2591 * Merge the new allocation with the left neighbor.
2593 temp
= left
.br_blockcount
+ new->br_blockcount
;
2595 oldlen
= startblockval(left
.br_startblock
) +
2596 startblockval(new->br_startblock
);
2597 newlen
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
2599 left
.br_blockcount
= temp
;
2600 left
.br_startblock
= nullstartblock(newlen
);
2602 xfs_iext_prev(ifp
, icur
);
2603 xfs_iext_update_extent(ip
, state
, icur
, &left
);
2606 case BMAP_RIGHT_CONTIG
:
2608 * New allocation is contiguous with a delayed allocation
2610 * Merge the new allocation with the right neighbor.
2612 temp
= new->br_blockcount
+ right
.br_blockcount
;
2613 oldlen
= startblockval(new->br_startblock
) +
2614 startblockval(right
.br_startblock
);
2615 newlen
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
2617 right
.br_startoff
= new->br_startoff
;
2618 right
.br_startblock
= nullstartblock(newlen
);
2619 right
.br_blockcount
= temp
;
2620 xfs_iext_update_extent(ip
, state
, icur
, &right
);
2625 * New allocation is not contiguous with another
2626 * delayed allocation.
2627 * Insert a new entry.
2629 oldlen
= newlen
= 0;
2630 xfs_iext_insert(ip
, icur
, new, state
);
2633 if (oldlen
!= newlen
) {
2634 ASSERT(oldlen
> newlen
);
2635 xfs_mod_fdblocks(ip
->i_mount
, (int64_t)(oldlen
- newlen
),
2638 * Nothing to do for disk quota accounting here.
2644 * Convert a hole to a real allocation.
2646 STATIC
int /* error */
2647 xfs_bmap_add_extent_hole_real(
2648 struct xfs_trans
*tp
,
2649 struct xfs_inode
*ip
,
2651 struct xfs_iext_cursor
*icur
,
2652 struct xfs_btree_cur
**curp
,
2653 struct xfs_bmbt_irec
*new,
2654 xfs_fsblock_t
*first
,
2655 struct xfs_defer_ops
*dfops
,
2659 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
2660 struct xfs_mount
*mp
= ip
->i_mount
;
2661 struct xfs_btree_cur
*cur
= *curp
;
2662 int error
; /* error return value */
2663 int i
; /* temp state */
2664 xfs_bmbt_irec_t left
; /* left neighbor extent entry */
2665 xfs_bmbt_irec_t right
; /* right neighbor extent entry */
2666 int rval
=0; /* return value (logging flags) */
2667 int state
= xfs_bmap_fork_to_state(whichfork
);
2668 struct xfs_bmbt_irec old
;
2670 ASSERT(!isnullstartblock(new->br_startblock
));
2671 ASSERT(!cur
|| !(cur
->bc_private
.b
.flags
& XFS_BTCUR_BPRV_WASDEL
));
2673 XFS_STATS_INC(mp
, xs_add_exlist
);
2676 * Check and set flags if this segment has a left neighbor.
2678 if (xfs_iext_peek_prev_extent(ifp
, icur
, &left
)) {
2679 state
|= BMAP_LEFT_VALID
;
2680 if (isnullstartblock(left
.br_startblock
))
2681 state
|= BMAP_LEFT_DELAY
;
2685 * Check and set flags if this segment has a current value.
2686 * Not true if we're inserting into the "hole" at eof.
2688 if (xfs_iext_get_extent(ifp
, icur
, &right
)) {
2689 state
|= BMAP_RIGHT_VALID
;
2690 if (isnullstartblock(right
.br_startblock
))
2691 state
|= BMAP_RIGHT_DELAY
;
2695 * We're inserting a real allocation between "left" and "right".
2696 * Set the contiguity flags. Don't let extents get too large.
2698 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
2699 left
.br_startoff
+ left
.br_blockcount
== new->br_startoff
&&
2700 left
.br_startblock
+ left
.br_blockcount
== new->br_startblock
&&
2701 left
.br_state
== new->br_state
&&
2702 left
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
2703 state
|= BMAP_LEFT_CONTIG
;
2705 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
2706 new->br_startoff
+ new->br_blockcount
== right
.br_startoff
&&
2707 new->br_startblock
+ new->br_blockcount
== right
.br_startblock
&&
2708 new->br_state
== right
.br_state
&&
2709 new->br_blockcount
+ right
.br_blockcount
<= MAXEXTLEN
&&
2710 (!(state
& BMAP_LEFT_CONTIG
) ||
2711 left
.br_blockcount
+ new->br_blockcount
+
2712 right
.br_blockcount
<= MAXEXTLEN
))
2713 state
|= BMAP_RIGHT_CONTIG
;
2717 * Select which case we're in here, and implement it.
2719 switch (state
& (BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
)) {
2720 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2722 * New allocation is contiguous with real allocations on the
2723 * left and on the right.
2724 * Merge all three into a single extent record.
2726 left
.br_blockcount
+= new->br_blockcount
+ right
.br_blockcount
;
2728 xfs_iext_remove(ip
, icur
, state
);
2729 xfs_iext_prev(ifp
, icur
);
2730 xfs_iext_update_extent(ip
, state
, icur
, &left
);
2732 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2733 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
2735 rval
= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
2737 rval
= XFS_ILOG_CORE
;
2738 error
= xfs_bmbt_lookup_eq(cur
, &right
, &i
);
2741 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2742 error
= xfs_btree_delete(cur
, &i
);
2745 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2746 error
= xfs_btree_decrement(cur
, 0, &i
);
2749 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2750 error
= xfs_bmbt_update(cur
, &left
);
2756 case BMAP_LEFT_CONTIG
:
2758 * New allocation is contiguous with a real allocation
2760 * Merge the new allocation with the left neighbor.
2763 left
.br_blockcount
+= new->br_blockcount
;
2765 xfs_iext_prev(ifp
, icur
);
2766 xfs_iext_update_extent(ip
, state
, icur
, &left
);
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
, &left
);
2782 case BMAP_RIGHT_CONTIG
:
2784 * New allocation is contiguous with a real allocation
2786 * Merge the new allocation with the right neighbor.
2790 right
.br_startoff
= new->br_startoff
;
2791 right
.br_startblock
= new->br_startblock
;
2792 right
.br_blockcount
+= new->br_blockcount
;
2793 xfs_iext_update_extent(ip
, state
, icur
, &right
);
2796 rval
= xfs_ilog_fext(whichfork
);
2799 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2802 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2803 error
= xfs_bmbt_update(cur
, &right
);
2811 * New allocation is not contiguous with another
2813 * Insert a new entry.
2815 xfs_iext_insert(ip
, icur
, new, state
);
2816 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2817 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
2819 rval
= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
2821 rval
= XFS_ILOG_CORE
;
2822 error
= xfs_bmbt_lookup_eq(cur
, new, &i
);
2825 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
2826 error
= xfs_btree_insert(cur
, &i
);
2829 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2834 /* add reverse mapping unless caller opted out */
2835 if (!(flags
& XFS_BMAPI_NORMAP
)) {
2836 error
= xfs_rmap_map_extent(mp
, dfops
, ip
, whichfork
, new);
2841 /* convert to a btree if necessary */
2842 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
2843 int tmp_logflags
; /* partial log flag return val */
2845 ASSERT(cur
== NULL
);
2846 error
= xfs_bmap_extents_to_btree(tp
, ip
, first
, dfops
, curp
,
2847 0, &tmp_logflags
, whichfork
);
2848 *logflagsp
|= tmp_logflags
;
2854 /* clear out the allocated field, done with it now in any case. */
2856 cur
->bc_private
.b
.allocated
= 0;
2858 xfs_bmap_check_leaf_extents(cur
, ip
, whichfork
);
2865 * Functions used in the extent read, allocate and remove paths
2869 * Adjust the size of the new extent based on di_extsize and rt extsize.
2872 xfs_bmap_extsize_align(
2874 xfs_bmbt_irec_t
*gotp
, /* next extent pointer */
2875 xfs_bmbt_irec_t
*prevp
, /* previous extent pointer */
2876 xfs_extlen_t extsz
, /* align to this extent size */
2877 int rt
, /* is this a realtime inode? */
2878 int eof
, /* is extent at end-of-file? */
2879 int delay
, /* creating delalloc extent? */
2880 int convert
, /* overwriting unwritten extent? */
2881 xfs_fileoff_t
*offp
, /* in/out: aligned offset */
2882 xfs_extlen_t
*lenp
) /* in/out: aligned length */
2884 xfs_fileoff_t orig_off
; /* original offset */
2885 xfs_extlen_t orig_alen
; /* original length */
2886 xfs_fileoff_t orig_end
; /* original off+len */
2887 xfs_fileoff_t nexto
; /* next file offset */
2888 xfs_fileoff_t prevo
; /* previous file offset */
2889 xfs_fileoff_t align_off
; /* temp for offset */
2890 xfs_extlen_t align_alen
; /* temp for length */
2891 xfs_extlen_t temp
; /* temp for calculations */
2896 orig_off
= align_off
= *offp
;
2897 orig_alen
= align_alen
= *lenp
;
2898 orig_end
= orig_off
+ orig_alen
;
2901 * If this request overlaps an existing extent, then don't
2902 * attempt to perform any additional alignment.
2904 if (!delay
&& !eof
&&
2905 (orig_off
>= gotp
->br_startoff
) &&
2906 (orig_end
<= gotp
->br_startoff
+ gotp
->br_blockcount
)) {
2911 * If the file offset is unaligned vs. the extent size
2912 * we need to align it. This will be possible unless
2913 * the file was previously written with a kernel that didn't
2914 * perform this alignment, or if a truncate shot us in the
2917 div_u64_rem(orig_off
, extsz
, &temp
);
2923 /* Same adjustment for the end of the requested area. */
2924 temp
= (align_alen
% extsz
);
2926 align_alen
+= extsz
- temp
;
2929 * For large extent hint sizes, the aligned extent might be larger than
2930 * MAXEXTLEN. In that case, reduce the size by an extsz so that it pulls
2931 * the length back under MAXEXTLEN. The outer allocation loops handle
2932 * short allocation just fine, so it is safe to do this. We only want to
2933 * do it when we are forced to, though, because it means more allocation
2934 * operations are required.
2936 while (align_alen
> MAXEXTLEN
)
2937 align_alen
-= extsz
;
2938 ASSERT(align_alen
<= MAXEXTLEN
);
2941 * If the previous block overlaps with this proposed allocation
2942 * then move the start forward without adjusting the length.
2944 if (prevp
->br_startoff
!= NULLFILEOFF
) {
2945 if (prevp
->br_startblock
== HOLESTARTBLOCK
)
2946 prevo
= prevp
->br_startoff
;
2948 prevo
= prevp
->br_startoff
+ prevp
->br_blockcount
;
2951 if (align_off
!= orig_off
&& align_off
< prevo
)
2954 * If the next block overlaps with this proposed allocation
2955 * then move the start back without adjusting the length,
2956 * but not before offset 0.
2957 * This may of course make the start overlap previous block,
2958 * and if we hit the offset 0 limit then the next block
2959 * can still overlap too.
2961 if (!eof
&& gotp
->br_startoff
!= NULLFILEOFF
) {
2962 if ((delay
&& gotp
->br_startblock
== HOLESTARTBLOCK
) ||
2963 (!delay
&& gotp
->br_startblock
== DELAYSTARTBLOCK
))
2964 nexto
= gotp
->br_startoff
+ gotp
->br_blockcount
;
2966 nexto
= gotp
->br_startoff
;
2968 nexto
= NULLFILEOFF
;
2970 align_off
+ align_alen
!= orig_end
&&
2971 align_off
+ align_alen
> nexto
)
2972 align_off
= nexto
> align_alen
? nexto
- align_alen
: 0;
2974 * If we're now overlapping the next or previous extent that
2975 * means we can't fit an extsz piece in this hole. Just move
2976 * the start forward to the first valid spot and set
2977 * the length so we hit the end.
2979 if (align_off
!= orig_off
&& align_off
< prevo
)
2981 if (align_off
+ align_alen
!= orig_end
&&
2982 align_off
+ align_alen
> nexto
&&
2983 nexto
!= NULLFILEOFF
) {
2984 ASSERT(nexto
> prevo
);
2985 align_alen
= nexto
- align_off
;
2989 * If realtime, and the result isn't a multiple of the realtime
2990 * extent size we need to remove blocks until it is.
2992 if (rt
&& (temp
= (align_alen
% mp
->m_sb
.sb_rextsize
))) {
2994 * We're not covering the original request, or
2995 * we won't be able to once we fix the length.
2997 if (orig_off
< align_off
||
2998 orig_end
> align_off
+ align_alen
||
2999 align_alen
- temp
< orig_alen
)
3002 * Try to fix it by moving the start up.
3004 if (align_off
+ temp
<= orig_off
) {
3009 * Try to fix it by moving the end in.
3011 else if (align_off
+ align_alen
- temp
>= orig_end
)
3014 * Set the start to the minimum then trim the length.
3017 align_alen
-= orig_off
- align_off
;
3018 align_off
= orig_off
;
3019 align_alen
-= align_alen
% mp
->m_sb
.sb_rextsize
;
3022 * Result doesn't cover the request, fail it.
3024 if (orig_off
< align_off
|| orig_end
> align_off
+ align_alen
)
3027 ASSERT(orig_off
>= align_off
);
3028 /* see MAXEXTLEN handling above */
3029 ASSERT(orig_end
<= align_off
+ align_alen
||
3030 align_alen
+ extsz
> MAXEXTLEN
);
3034 if (!eof
&& gotp
->br_startoff
!= NULLFILEOFF
)
3035 ASSERT(align_off
+ align_alen
<= gotp
->br_startoff
);
3036 if (prevp
->br_startoff
!= NULLFILEOFF
)
3037 ASSERT(align_off
>= prevp
->br_startoff
+ prevp
->br_blockcount
);
3045 #define XFS_ALLOC_GAP_UNITS 4
3049 struct xfs_bmalloca
*ap
) /* bmap alloc argument struct */
3051 xfs_fsblock_t adjust
; /* adjustment to block numbers */
3052 xfs_agnumber_t fb_agno
; /* ag number of ap->firstblock */
3053 xfs_mount_t
*mp
; /* mount point structure */
3054 int nullfb
; /* true if ap->firstblock isn't set */
3055 int rt
; /* true if inode is realtime */
3057 #define ISVALID(x,y) \
3059 (x) < mp->m_sb.sb_rblocks : \
3060 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
3061 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
3062 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
3064 mp
= ap
->ip
->i_mount
;
3065 nullfb
= *ap
->firstblock
== NULLFSBLOCK
;
3066 rt
= XFS_IS_REALTIME_INODE(ap
->ip
) &&
3067 xfs_alloc_is_userdata(ap
->datatype
);
3068 fb_agno
= nullfb
? NULLAGNUMBER
: XFS_FSB_TO_AGNO(mp
, *ap
->firstblock
);
3070 * If allocating at eof, and there's a previous real block,
3071 * try to use its last block as our starting point.
3073 if (ap
->eof
&& ap
->prev
.br_startoff
!= NULLFILEOFF
&&
3074 !isnullstartblock(ap
->prev
.br_startblock
) &&
3075 ISVALID(ap
->prev
.br_startblock
+ ap
->prev
.br_blockcount
,
3076 ap
->prev
.br_startblock
)) {
3077 ap
->blkno
= ap
->prev
.br_startblock
+ ap
->prev
.br_blockcount
;
3079 * Adjust for the gap between prevp and us.
3081 adjust
= ap
->offset
-
3082 (ap
->prev
.br_startoff
+ ap
->prev
.br_blockcount
);
3084 ISVALID(ap
->blkno
+ adjust
, ap
->prev
.br_startblock
))
3085 ap
->blkno
+= adjust
;
3088 * If not at eof, then compare the two neighbor blocks.
3089 * Figure out whether either one gives us a good starting point,
3090 * and pick the better one.
3092 else if (!ap
->eof
) {
3093 xfs_fsblock_t gotbno
; /* right side block number */
3094 xfs_fsblock_t gotdiff
=0; /* right side difference */
3095 xfs_fsblock_t prevbno
; /* left side block number */
3096 xfs_fsblock_t prevdiff
=0; /* left side difference */
3099 * If there's a previous (left) block, select a requested
3100 * start block based on it.
3102 if (ap
->prev
.br_startoff
!= NULLFILEOFF
&&
3103 !isnullstartblock(ap
->prev
.br_startblock
) &&
3104 (prevbno
= ap
->prev
.br_startblock
+
3105 ap
->prev
.br_blockcount
) &&
3106 ISVALID(prevbno
, ap
->prev
.br_startblock
)) {
3108 * Calculate gap to end of previous block.
3110 adjust
= prevdiff
= ap
->offset
-
3111 (ap
->prev
.br_startoff
+
3112 ap
->prev
.br_blockcount
);
3114 * Figure the startblock based on the previous block's
3115 * end and the gap size.
3117 * If the gap is large relative to the piece we're
3118 * allocating, or using it gives us an invalid block
3119 * number, then just use the end of the previous block.
3121 if (prevdiff
<= XFS_ALLOC_GAP_UNITS
* ap
->length
&&
3122 ISVALID(prevbno
+ prevdiff
,
3123 ap
->prev
.br_startblock
))
3128 * If the firstblock forbids it, can't use it,
3131 if (!rt
&& !nullfb
&&
3132 XFS_FSB_TO_AGNO(mp
, prevbno
) != fb_agno
)
3133 prevbno
= NULLFSBLOCK
;
3136 * No previous block or can't follow it, just default.
3139 prevbno
= NULLFSBLOCK
;
3141 * If there's a following (right) block, select a requested
3142 * start block based on it.
3144 if (!isnullstartblock(ap
->got
.br_startblock
)) {
3146 * Calculate gap to start of next block.
3148 adjust
= gotdiff
= ap
->got
.br_startoff
- ap
->offset
;
3150 * Figure the startblock based on the next block's
3151 * start and the gap size.
3153 gotbno
= ap
->got
.br_startblock
;
3156 * If the gap is large relative to the piece we're
3157 * allocating, or using it gives us an invalid block
3158 * number, then just use the start of the next block
3159 * offset by our length.
3161 if (gotdiff
<= XFS_ALLOC_GAP_UNITS
* ap
->length
&&
3162 ISVALID(gotbno
- gotdiff
, gotbno
))
3164 else if (ISVALID(gotbno
- ap
->length
, gotbno
)) {
3165 gotbno
-= ap
->length
;
3166 gotdiff
+= adjust
- ap
->length
;
3170 * If the firstblock forbids it, can't use it,
3173 if (!rt
&& !nullfb
&&
3174 XFS_FSB_TO_AGNO(mp
, gotbno
) != fb_agno
)
3175 gotbno
= NULLFSBLOCK
;
3178 * No next block, just default.
3181 gotbno
= NULLFSBLOCK
;
3183 * If both valid, pick the better one, else the only good
3184 * one, else ap->blkno is already set (to 0 or the inode block).
3186 if (prevbno
!= NULLFSBLOCK
&& gotbno
!= NULLFSBLOCK
)
3187 ap
->blkno
= prevdiff
<= gotdiff
? prevbno
: gotbno
;
3188 else if (prevbno
!= NULLFSBLOCK
)
3189 ap
->blkno
= prevbno
;
3190 else if (gotbno
!= NULLFSBLOCK
)
3197 xfs_bmap_longest_free_extent(
3198 struct xfs_trans
*tp
,
3203 struct xfs_mount
*mp
= tp
->t_mountp
;
3204 struct xfs_perag
*pag
;
3205 xfs_extlen_t longest
;
3208 pag
= xfs_perag_get(mp
, ag
);
3209 if (!pag
->pagf_init
) {
3210 error
= xfs_alloc_pagf_init(mp
, tp
, ag
, XFS_ALLOC_FLAG_TRYLOCK
);
3214 if (!pag
->pagf_init
) {
3220 longest
= xfs_alloc_longest_free_extent(pag
,
3221 xfs_alloc_min_freelist(mp
, pag
),
3222 xfs_ag_resv_needed(pag
, XFS_AG_RESV_NONE
));
3223 if (*blen
< longest
)
3232 xfs_bmap_select_minlen(
3233 struct xfs_bmalloca
*ap
,
3234 struct xfs_alloc_arg
*args
,
3238 if (notinit
|| *blen
< ap
->minlen
) {
3240 * Since we did a BUF_TRYLOCK above, it is possible that
3241 * there is space for this request.
3243 args
->minlen
= ap
->minlen
;
3244 } else if (*blen
< args
->maxlen
) {
3246 * If the best seen length is less than the request length,
3247 * use the best as the minimum.
3249 args
->minlen
= *blen
;
3252 * Otherwise we've seen an extent as big as maxlen, use that
3255 args
->minlen
= args
->maxlen
;
3260 xfs_bmap_btalloc_nullfb(
3261 struct xfs_bmalloca
*ap
,
3262 struct xfs_alloc_arg
*args
,
3265 struct xfs_mount
*mp
= ap
->ip
->i_mount
;
3266 xfs_agnumber_t ag
, startag
;
3270 args
->type
= XFS_ALLOCTYPE_START_BNO
;
3271 args
->total
= ap
->total
;
3273 startag
= ag
= XFS_FSB_TO_AGNO(mp
, args
->fsbno
);
3274 if (startag
== NULLAGNUMBER
)
3277 while (*blen
< args
->maxlen
) {
3278 error
= xfs_bmap_longest_free_extent(args
->tp
, ag
, blen
,
3283 if (++ag
== mp
->m_sb
.sb_agcount
)
3289 xfs_bmap_select_minlen(ap
, args
, blen
, notinit
);
3294 xfs_bmap_btalloc_filestreams(
3295 struct xfs_bmalloca
*ap
,
3296 struct xfs_alloc_arg
*args
,
3299 struct xfs_mount
*mp
= ap
->ip
->i_mount
;
3304 args
->type
= XFS_ALLOCTYPE_NEAR_BNO
;
3305 args
->total
= ap
->total
;
3307 ag
= XFS_FSB_TO_AGNO(mp
, args
->fsbno
);
3308 if (ag
== NULLAGNUMBER
)
3311 error
= xfs_bmap_longest_free_extent(args
->tp
, ag
, blen
, ¬init
);
3315 if (*blen
< args
->maxlen
) {
3316 error
= xfs_filestream_new_ag(ap
, &ag
);
3320 error
= xfs_bmap_longest_free_extent(args
->tp
, ag
, blen
,
3327 xfs_bmap_select_minlen(ap
, args
, blen
, notinit
);
3330 * Set the failure fallback case to look in the selected AG as stream
3333 ap
->blkno
= args
->fsbno
= XFS_AGB_TO_FSB(mp
, ag
, 0);
3337 /* Update all inode and quota accounting for the allocation we just did. */
3339 xfs_bmap_btalloc_accounting(
3340 struct xfs_bmalloca
*ap
,
3341 struct xfs_alloc_arg
*args
)
3343 if (ap
->flags
& XFS_BMAPI_COWFORK
) {
3345 * COW fork blocks are in-core only and thus are treated as
3346 * in-core quota reservation (like delalloc blocks) even when
3347 * converted to real blocks. The quota reservation is not
3348 * accounted to disk until blocks are remapped to the data
3349 * fork. So if these blocks were previously delalloc, we
3350 * already have quota reservation and there's nothing to do
3357 * Otherwise, we've allocated blocks in a hole. The transaction
3358 * has acquired in-core quota reservation for this extent.
3359 * Rather than account these as real blocks, however, we reduce
3360 * the transaction quota reservation based on the allocation.
3361 * This essentially transfers the transaction quota reservation
3362 * to that of a delalloc extent.
3364 ap
->ip
->i_delayed_blks
+= args
->len
;
3365 xfs_trans_mod_dquot_byino(ap
->tp
, ap
->ip
, XFS_TRANS_DQ_RES_BLKS
,
3370 /* data/attr fork only */
3371 ap
->ip
->i_d
.di_nblocks
+= args
->len
;
3372 xfs_trans_log_inode(ap
->tp
, ap
->ip
, XFS_ILOG_CORE
);
3374 ap
->ip
->i_delayed_blks
-= args
->len
;
3375 xfs_trans_mod_dquot_byino(ap
->tp
, ap
->ip
,
3376 ap
->wasdel
? XFS_TRANS_DQ_DELBCOUNT
: XFS_TRANS_DQ_BCOUNT
,
3382 struct xfs_bmalloca
*ap
) /* bmap alloc argument struct */
3384 xfs_mount_t
*mp
; /* mount point structure */
3385 xfs_alloctype_t atype
= 0; /* type for allocation routines */
3386 xfs_extlen_t align
= 0; /* minimum allocation alignment */
3387 xfs_agnumber_t fb_agno
; /* ag number of ap->firstblock */
3389 xfs_alloc_arg_t args
;
3390 xfs_fileoff_t orig_offset
;
3391 xfs_extlen_t orig_length
;
3393 xfs_extlen_t nextminlen
= 0;
3394 int nullfb
; /* true if ap->firstblock isn't set */
3401 orig_offset
= ap
->offset
;
3402 orig_length
= ap
->length
;
3404 mp
= ap
->ip
->i_mount
;
3406 /* stripe alignment for allocation is determined by mount parameters */
3408 if (mp
->m_swidth
&& (mp
->m_flags
& XFS_MOUNT_SWALLOC
))
3409 stripe_align
= mp
->m_swidth
;
3410 else if (mp
->m_dalign
)
3411 stripe_align
= mp
->m_dalign
;
3413 if (ap
->flags
& XFS_BMAPI_COWFORK
)
3414 align
= xfs_get_cowextsz_hint(ap
->ip
);
3415 else if (xfs_alloc_is_userdata(ap
->datatype
))
3416 align
= xfs_get_extsz_hint(ap
->ip
);
3418 error
= xfs_bmap_extsize_align(mp
, &ap
->got
, &ap
->prev
,
3419 align
, 0, ap
->eof
, 0, ap
->conv
,
3420 &ap
->offset
, &ap
->length
);
3426 nullfb
= *ap
->firstblock
== NULLFSBLOCK
;
3427 fb_agno
= nullfb
? NULLAGNUMBER
: XFS_FSB_TO_AGNO(mp
, *ap
->firstblock
);
3429 if (xfs_alloc_is_userdata(ap
->datatype
) &&
3430 xfs_inode_is_filestream(ap
->ip
)) {
3431 ag
= xfs_filestream_lookup_ag(ap
->ip
);
3432 ag
= (ag
!= NULLAGNUMBER
) ? ag
: 0;
3433 ap
->blkno
= XFS_AGB_TO_FSB(mp
, ag
, 0);
3435 ap
->blkno
= XFS_INO_TO_FSB(mp
, ap
->ip
->i_ino
);
3438 ap
->blkno
= *ap
->firstblock
;
3440 xfs_bmap_adjacent(ap
);
3443 * If allowed, use ap->blkno; otherwise must use firstblock since
3444 * it's in the right allocation group.
3446 if (nullfb
|| XFS_FSB_TO_AGNO(mp
, ap
->blkno
) == fb_agno
)
3449 ap
->blkno
= *ap
->firstblock
;
3451 * Normal allocation, done through xfs_alloc_vextent.
3453 tryagain
= isaligned
= 0;
3454 memset(&args
, 0, sizeof(args
));
3457 args
.fsbno
= ap
->blkno
;
3458 xfs_rmap_skip_owner_update(&args
.oinfo
);
3460 /* Trim the allocation back to the maximum an AG can fit. */
3461 args
.maxlen
= min(ap
->length
, mp
->m_ag_max_usable
);
3462 args
.firstblock
= *ap
->firstblock
;
3466 * Search for an allocation group with a single extent large
3467 * enough for the request. If one isn't found, then adjust
3468 * the minimum allocation size to the largest space found.
3470 if (xfs_alloc_is_userdata(ap
->datatype
) &&
3471 xfs_inode_is_filestream(ap
->ip
))
3472 error
= xfs_bmap_btalloc_filestreams(ap
, &args
, &blen
);
3474 error
= xfs_bmap_btalloc_nullfb(ap
, &args
, &blen
);
3477 } else if (ap
->dfops
->dop_low
) {
3478 if (xfs_inode_is_filestream(ap
->ip
))
3479 args
.type
= XFS_ALLOCTYPE_FIRST_AG
;
3481 args
.type
= XFS_ALLOCTYPE_START_BNO
;
3482 args
.total
= args
.minlen
= ap
->minlen
;
3484 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
3485 args
.total
= ap
->total
;
3486 args
.minlen
= ap
->minlen
;
3488 /* apply extent size hints if obtained earlier */
3491 div_u64_rem(ap
->offset
, args
.prod
, &args
.mod
);
3493 args
.mod
= args
.prod
- args
.mod
;
3494 } else if (mp
->m_sb
.sb_blocksize
>= PAGE_SIZE
) {
3498 args
.prod
= PAGE_SIZE
>> mp
->m_sb
.sb_blocklog
;
3499 div_u64_rem(ap
->offset
, args
.prod
, &args
.mod
);
3501 args
.mod
= args
.prod
- args
.mod
;
3504 * If we are not low on available data blocks, and the
3505 * underlying logical volume manager is a stripe, and
3506 * the file offset is zero then try to allocate data
3507 * blocks on stripe unit boundary.
3508 * NOTE: ap->aeof is only set if the allocation length
3509 * is >= the stripe unit and the allocation offset is
3510 * at the end of file.
3512 if (!ap
->dfops
->dop_low
&& ap
->aeof
) {
3514 args
.alignment
= stripe_align
;
3518 * Adjust for alignment
3520 if (blen
> args
.alignment
&& blen
<= args
.maxlen
)
3521 args
.minlen
= blen
- args
.alignment
;
3522 args
.minalignslop
= 0;
3525 * First try an exact bno allocation.
3526 * If it fails then do a near or start bno
3527 * allocation with alignment turned on.
3531 args
.type
= XFS_ALLOCTYPE_THIS_BNO
;
3534 * Compute the minlen+alignment for the
3535 * next case. Set slop so that the value
3536 * of minlen+alignment+slop doesn't go up
3537 * between the calls.
3539 if (blen
> stripe_align
&& blen
<= args
.maxlen
)
3540 nextminlen
= blen
- stripe_align
;
3542 nextminlen
= args
.minlen
;
3543 if (nextminlen
+ stripe_align
> args
.minlen
+ 1)
3545 nextminlen
+ stripe_align
-
3548 args
.minalignslop
= 0;
3552 args
.minalignslop
= 0;
3554 args
.minleft
= ap
->minleft
;
3555 args
.wasdel
= ap
->wasdel
;
3556 args
.resv
= XFS_AG_RESV_NONE
;
3557 args
.datatype
= ap
->datatype
;
3558 if (ap
->datatype
& XFS_ALLOC_USERDATA_ZERO
)
3561 error
= xfs_alloc_vextent(&args
);
3565 if (tryagain
&& args
.fsbno
== NULLFSBLOCK
) {
3567 * Exact allocation failed. Now try with alignment
3571 args
.fsbno
= ap
->blkno
;
3572 args
.alignment
= stripe_align
;
3573 args
.minlen
= nextminlen
;
3574 args
.minalignslop
= 0;
3576 if ((error
= xfs_alloc_vextent(&args
)))
3579 if (isaligned
&& args
.fsbno
== NULLFSBLOCK
) {
3581 * allocation failed, so turn off alignment and
3585 args
.fsbno
= ap
->blkno
;
3587 if ((error
= xfs_alloc_vextent(&args
)))
3590 if (args
.fsbno
== NULLFSBLOCK
&& nullfb
&&
3591 args
.minlen
> ap
->minlen
) {
3592 args
.minlen
= ap
->minlen
;
3593 args
.type
= XFS_ALLOCTYPE_START_BNO
;
3594 args
.fsbno
= ap
->blkno
;
3595 if ((error
= xfs_alloc_vextent(&args
)))
3598 if (args
.fsbno
== NULLFSBLOCK
&& nullfb
) {
3600 args
.type
= XFS_ALLOCTYPE_FIRST_AG
;
3601 args
.total
= ap
->minlen
;
3602 if ((error
= xfs_alloc_vextent(&args
)))
3604 ap
->dfops
->dop_low
= true;
3606 if (args
.fsbno
!= NULLFSBLOCK
) {
3608 * check the allocation happened at the same or higher AG than
3609 * the first block that was allocated.
3611 ASSERT(*ap
->firstblock
== NULLFSBLOCK
||
3612 XFS_FSB_TO_AGNO(mp
, *ap
->firstblock
) <=
3613 XFS_FSB_TO_AGNO(mp
, args
.fsbno
));
3615 ap
->blkno
= args
.fsbno
;
3616 if (*ap
->firstblock
== NULLFSBLOCK
)
3617 *ap
->firstblock
= args
.fsbno
;
3618 ASSERT(nullfb
|| fb_agno
<= args
.agno
);
3619 ap
->length
= args
.len
;
3621 * If the extent size hint is active, we tried to round the
3622 * caller's allocation request offset down to extsz and the
3623 * length up to another extsz boundary. If we found a free
3624 * extent we mapped it in starting at this new offset. If the
3625 * newly mapped space isn't long enough to cover any of the
3626 * range of offsets that was originally requested, move the
3627 * mapping up so that we can fill as much of the caller's
3628 * original request as possible. Free space is apparently
3629 * very fragmented so we're unlikely to be able to satisfy the
3632 if (ap
->length
<= orig_length
)
3633 ap
->offset
= orig_offset
;
3634 else if (ap
->offset
+ ap
->length
< orig_offset
+ orig_length
)
3635 ap
->offset
= orig_offset
+ orig_length
- ap
->length
;
3636 xfs_bmap_btalloc_accounting(ap
, &args
);
3638 ap
->blkno
= NULLFSBLOCK
;
3645 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
3646 * It figures out where to ask the underlying allocator to put the new extent.
3650 struct xfs_bmalloca
*ap
) /* bmap alloc argument struct */
3652 if (XFS_IS_REALTIME_INODE(ap
->ip
) &&
3653 xfs_alloc_is_userdata(ap
->datatype
))
3654 return xfs_bmap_rtalloc(ap
);
3655 return xfs_bmap_btalloc(ap
);
3658 /* Trim extent to fit a logical block range. */
3661 struct xfs_bmbt_irec
*irec
,
3665 xfs_fileoff_t distance
;
3666 xfs_fileoff_t end
= bno
+ len
;
3668 if (irec
->br_startoff
+ irec
->br_blockcount
<= bno
||
3669 irec
->br_startoff
>= end
) {
3670 irec
->br_blockcount
= 0;
3674 if (irec
->br_startoff
< bno
) {
3675 distance
= bno
- irec
->br_startoff
;
3676 if (isnullstartblock(irec
->br_startblock
))
3677 irec
->br_startblock
= DELAYSTARTBLOCK
;
3678 if (irec
->br_startblock
!= DELAYSTARTBLOCK
&&
3679 irec
->br_startblock
!= HOLESTARTBLOCK
)
3680 irec
->br_startblock
+= distance
;
3681 irec
->br_startoff
+= distance
;
3682 irec
->br_blockcount
-= distance
;
3685 if (end
< irec
->br_startoff
+ irec
->br_blockcount
) {
3686 distance
= irec
->br_startoff
+ irec
->br_blockcount
- end
;
3687 irec
->br_blockcount
-= distance
;
3691 /* trim extent to within eof */
3693 xfs_trim_extent_eof(
3694 struct xfs_bmbt_irec
*irec
,
3695 struct xfs_inode
*ip
)
3698 xfs_trim_extent(irec
, 0, XFS_B_TO_FSB(ip
->i_mount
,
3699 i_size_read(VFS_I(ip
))));
3703 * Trim the returned map to the required bounds
3707 struct xfs_bmbt_irec
*mval
,
3708 struct xfs_bmbt_irec
*got
,
3716 if ((flags
& XFS_BMAPI_ENTIRE
) ||
3717 got
->br_startoff
+ got
->br_blockcount
<= obno
) {
3719 if (isnullstartblock(got
->br_startblock
))
3720 mval
->br_startblock
= DELAYSTARTBLOCK
;
3726 ASSERT((*bno
>= obno
) || (n
== 0));
3728 mval
->br_startoff
= *bno
;
3729 if (isnullstartblock(got
->br_startblock
))
3730 mval
->br_startblock
= DELAYSTARTBLOCK
;
3732 mval
->br_startblock
= got
->br_startblock
+
3733 (*bno
- got
->br_startoff
);
3735 * Return the minimum of what we got and what we asked for for
3736 * the length. We can use the len variable here because it is
3737 * modified below and we could have been there before coming
3738 * here if the first part of the allocation didn't overlap what
3741 mval
->br_blockcount
= XFS_FILBLKS_MIN(end
- *bno
,
3742 got
->br_blockcount
- (*bno
- got
->br_startoff
));
3743 mval
->br_state
= got
->br_state
;
3744 ASSERT(mval
->br_blockcount
<= len
);
3749 * Update and validate the extent map to return
3752 xfs_bmapi_update_map(
3753 struct xfs_bmbt_irec
**map
,
3761 xfs_bmbt_irec_t
*mval
= *map
;
3763 ASSERT((flags
& XFS_BMAPI_ENTIRE
) ||
3764 ((mval
->br_startoff
+ mval
->br_blockcount
) <= end
));
3765 ASSERT((flags
& XFS_BMAPI_ENTIRE
) || (mval
->br_blockcount
<= *len
) ||
3766 (mval
->br_startoff
< obno
));
3768 *bno
= mval
->br_startoff
+ mval
->br_blockcount
;
3770 if (*n
> 0 && mval
->br_startoff
== mval
[-1].br_startoff
) {
3771 /* update previous map with new information */
3772 ASSERT(mval
->br_startblock
== mval
[-1].br_startblock
);
3773 ASSERT(mval
->br_blockcount
> mval
[-1].br_blockcount
);
3774 ASSERT(mval
->br_state
== mval
[-1].br_state
);
3775 mval
[-1].br_blockcount
= mval
->br_blockcount
;
3776 mval
[-1].br_state
= mval
->br_state
;
3777 } else if (*n
> 0 && mval
->br_startblock
!= DELAYSTARTBLOCK
&&
3778 mval
[-1].br_startblock
!= DELAYSTARTBLOCK
&&
3779 mval
[-1].br_startblock
!= HOLESTARTBLOCK
&&
3780 mval
->br_startblock
== mval
[-1].br_startblock
+
3781 mval
[-1].br_blockcount
&&
3782 ((flags
& XFS_BMAPI_IGSTATE
) ||
3783 mval
[-1].br_state
== mval
->br_state
)) {
3784 ASSERT(mval
->br_startoff
==
3785 mval
[-1].br_startoff
+ mval
[-1].br_blockcount
);
3786 mval
[-1].br_blockcount
+= mval
->br_blockcount
;
3787 } else if (*n
> 0 &&
3788 mval
->br_startblock
== DELAYSTARTBLOCK
&&
3789 mval
[-1].br_startblock
== DELAYSTARTBLOCK
&&
3790 mval
->br_startoff
==
3791 mval
[-1].br_startoff
+ mval
[-1].br_blockcount
) {
3792 mval
[-1].br_blockcount
+= mval
->br_blockcount
;
3793 mval
[-1].br_state
= mval
->br_state
;
3794 } else if (!((*n
== 0) &&
3795 ((mval
->br_startoff
+ mval
->br_blockcount
) <=
3804 * Map file blocks to filesystem blocks without allocation.
3808 struct xfs_inode
*ip
,
3811 struct xfs_bmbt_irec
*mval
,
3815 struct xfs_mount
*mp
= ip
->i_mount
;
3816 struct xfs_ifork
*ifp
;
3817 struct xfs_bmbt_irec got
;
3820 struct xfs_iext_cursor icur
;
3824 int whichfork
= xfs_bmapi_whichfork(flags
);
3827 ASSERT(!(flags
& ~(XFS_BMAPI_ATTRFORK
|XFS_BMAPI_ENTIRE
|
3828 XFS_BMAPI_IGSTATE
|XFS_BMAPI_COWFORK
)));
3829 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_SHARED
|XFS_ILOCK_EXCL
));
3831 if (unlikely(XFS_TEST_ERROR(
3832 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
3833 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
3834 mp
, XFS_ERRTAG_BMAPIFORMAT
))) {
3835 XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW
, mp
);
3836 return -EFSCORRUPTED
;
3839 if (XFS_FORCED_SHUTDOWN(mp
))
3842 XFS_STATS_INC(mp
, xs_blk_mapr
);
3844 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
3846 /* No CoW fork? Return a hole. */
3847 if (whichfork
== XFS_COW_FORK
&& !ifp
) {
3848 mval
->br_startoff
= bno
;
3849 mval
->br_startblock
= HOLESTARTBLOCK
;
3850 mval
->br_blockcount
= len
;
3851 mval
->br_state
= XFS_EXT_NORM
;
3856 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
3857 error
= xfs_iread_extents(NULL
, ip
, whichfork
);
3862 if (!xfs_iext_lookup_extent(ip
, ifp
, bno
, &icur
, &got
))
3867 while (bno
< end
&& n
< *nmap
) {
3868 /* Reading past eof, act as though there's a hole up to end. */
3870 got
.br_startoff
= end
;
3871 if (got
.br_startoff
> bno
) {
3872 /* Reading in a hole. */
3873 mval
->br_startoff
= bno
;
3874 mval
->br_startblock
= HOLESTARTBLOCK
;
3875 mval
->br_blockcount
=
3876 XFS_FILBLKS_MIN(len
, got
.br_startoff
- bno
);
3877 mval
->br_state
= XFS_EXT_NORM
;
3878 bno
+= mval
->br_blockcount
;
3879 len
-= mval
->br_blockcount
;
3885 /* set up the extent map to return. */
3886 xfs_bmapi_trim_map(mval
, &got
, &bno
, len
, obno
, end
, n
, flags
);
3887 xfs_bmapi_update_map(&mval
, &bno
, &len
, obno
, end
, &n
, flags
);
3889 /* If we're done, stop now. */
3890 if (bno
>= end
|| n
>= *nmap
)
3893 /* Else go on to the next record. */
3894 if (!xfs_iext_next_extent(ifp
, &icur
, &got
))
3902 * Add a delayed allocation extent to an inode. Blocks are reserved from the
3903 * global pool and the extent inserted into the inode in-core extent tree.
3905 * On entry, got refers to the first extent beyond the offset of the extent to
3906 * allocate or eof is specified if no such extent exists. On return, got refers
3907 * to the extent record that was inserted to the inode fork.
3909 * Note that the allocated extent may have been merged with contiguous extents
3910 * during insertion into the inode fork. Thus, got does not reflect the current
3911 * state of the inode fork on return. If necessary, the caller can use lastx to
3912 * look up the updated record in the inode fork.
3915 xfs_bmapi_reserve_delalloc(
3916 struct xfs_inode
*ip
,
3920 xfs_filblks_t prealloc
,
3921 struct xfs_bmbt_irec
*got
,
3922 struct xfs_iext_cursor
*icur
,
3925 struct xfs_mount
*mp
= ip
->i_mount
;
3926 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
3928 xfs_extlen_t indlen
;
3930 xfs_fileoff_t aoff
= off
;
3933 * Cap the alloc length. Keep track of prealloc so we know whether to
3934 * tag the inode before we return.
3936 alen
= XFS_FILBLKS_MIN(len
+ prealloc
, MAXEXTLEN
);
3938 alen
= XFS_FILBLKS_MIN(alen
, got
->br_startoff
- aoff
);
3939 if (prealloc
&& alen
>= len
)
3940 prealloc
= alen
- len
;
3942 /* Figure out the extent size, adjust alen */
3943 if (whichfork
== XFS_COW_FORK
) {
3944 struct xfs_bmbt_irec prev
;
3945 xfs_extlen_t extsz
= xfs_get_cowextsz_hint(ip
);
3947 if (!xfs_iext_peek_prev_extent(ifp
, icur
, &prev
))
3948 prev
.br_startoff
= NULLFILEOFF
;
3950 error
= xfs_bmap_extsize_align(mp
, got
, &prev
, extsz
, 0, eof
,
3951 1, 0, &aoff
, &alen
);
3956 * Make a transaction-less quota reservation for delayed allocation
3957 * blocks. This number gets adjusted later. We return if we haven't
3958 * allocated blocks already inside this loop.
3960 error
= xfs_trans_reserve_quota_nblks(NULL
, ip
, (long)alen
, 0,
3961 XFS_QMOPT_RES_REGBLKS
);
3966 * Split changing sb for alen and indlen since they could be coming
3967 * from different places.
3969 indlen
= (xfs_extlen_t
)xfs_bmap_worst_indlen(ip
, alen
);
3972 error
= xfs_mod_fdblocks(mp
, -((int64_t)alen
), false);
3974 goto out_unreserve_quota
;
3976 error
= xfs_mod_fdblocks(mp
, -((int64_t)indlen
), false);
3978 goto out_unreserve_blocks
;
3981 ip
->i_delayed_blks
+= alen
;
3983 got
->br_startoff
= aoff
;
3984 got
->br_startblock
= nullstartblock(indlen
);
3985 got
->br_blockcount
= alen
;
3986 got
->br_state
= XFS_EXT_NORM
;
3988 xfs_bmap_add_extent_hole_delay(ip
, whichfork
, icur
, got
);
3991 * Tag the inode if blocks were preallocated. Note that COW fork
3992 * preallocation can occur at the start or end of the extent, even when
3993 * prealloc == 0, so we must also check the aligned offset and length.
3995 if (whichfork
== XFS_DATA_FORK
&& prealloc
)
3996 xfs_inode_set_eofblocks_tag(ip
);
3997 if (whichfork
== XFS_COW_FORK
&& (prealloc
|| aoff
< off
|| alen
> len
))
3998 xfs_inode_set_cowblocks_tag(ip
);
4002 out_unreserve_blocks
:
4003 xfs_mod_fdblocks(mp
, alen
, false);
4004 out_unreserve_quota
:
4005 if (XFS_IS_QUOTA_ON(mp
))
4006 xfs_trans_unreserve_quota_nblks(NULL
, ip
, (long)alen
, 0,
4007 XFS_QMOPT_RES_REGBLKS
);
4013 struct xfs_bmalloca
*bma
)
4015 struct xfs_mount
*mp
= bma
->ip
->i_mount
;
4016 int whichfork
= xfs_bmapi_whichfork(bma
->flags
);
4017 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
4018 int tmp_logflags
= 0;
4021 ASSERT(bma
->length
> 0);
4024 * For the wasdelay case, we could also just allocate the stuff asked
4025 * for in this bmap call but that wouldn't be as good.
4028 bma
->length
= (xfs_extlen_t
)bma
->got
.br_blockcount
;
4029 bma
->offset
= bma
->got
.br_startoff
;
4030 xfs_iext_peek_prev_extent(ifp
, &bma
->icur
, &bma
->prev
);
4032 bma
->length
= XFS_FILBLKS_MIN(bma
->length
, MAXEXTLEN
);
4034 bma
->length
= XFS_FILBLKS_MIN(bma
->length
,
4035 bma
->got
.br_startoff
- bma
->offset
);
4039 * Set the data type being allocated. For the data fork, the first data
4040 * in the file is treated differently to all other allocations. For the
4041 * attribute fork, we only need to ensure the allocated range is not on
4044 if (!(bma
->flags
& XFS_BMAPI_METADATA
)) {
4045 bma
->datatype
= XFS_ALLOC_NOBUSY
;
4046 if (whichfork
== XFS_DATA_FORK
) {
4047 if (bma
->offset
== 0)
4048 bma
->datatype
|= XFS_ALLOC_INITIAL_USER_DATA
;
4050 bma
->datatype
|= XFS_ALLOC_USERDATA
;
4052 if (bma
->flags
& XFS_BMAPI_ZERO
)
4053 bma
->datatype
|= XFS_ALLOC_USERDATA_ZERO
;
4056 bma
->minlen
= (bma
->flags
& XFS_BMAPI_CONTIG
) ? bma
->length
: 1;
4059 * Only want to do the alignment at the eof if it is userdata and
4060 * allocation length is larger than a stripe unit.
4062 if (mp
->m_dalign
&& bma
->length
>= mp
->m_dalign
&&
4063 !(bma
->flags
& XFS_BMAPI_METADATA
) && whichfork
== XFS_DATA_FORK
) {
4064 error
= xfs_bmap_isaeof(bma
, whichfork
);
4069 error
= xfs_bmap_alloc(bma
);
4074 bma
->cur
->bc_private
.b
.firstblock
= *bma
->firstblock
;
4075 if (bma
->blkno
== NULLFSBLOCK
)
4077 if ((ifp
->if_flags
& XFS_IFBROOT
) && !bma
->cur
) {
4078 bma
->cur
= xfs_bmbt_init_cursor(mp
, bma
->tp
, bma
->ip
, whichfork
);
4079 bma
->cur
->bc_private
.b
.firstblock
= *bma
->firstblock
;
4080 bma
->cur
->bc_private
.b
.dfops
= bma
->dfops
;
4083 * Bump the number of extents we've allocated
4089 bma
->cur
->bc_private
.b
.flags
=
4090 bma
->wasdel
? XFS_BTCUR_BPRV_WASDEL
: 0;
4092 bma
->got
.br_startoff
= bma
->offset
;
4093 bma
->got
.br_startblock
= bma
->blkno
;
4094 bma
->got
.br_blockcount
= bma
->length
;
4095 bma
->got
.br_state
= XFS_EXT_NORM
;
4098 * In the data fork, a wasdelay extent has been initialized, so
4099 * shouldn't be flagged as unwritten.
4101 * For the cow fork, however, we convert delalloc reservations
4102 * (extents allocated for speculative preallocation) to
4103 * allocated unwritten extents, and only convert the unwritten
4104 * extents to real extents when we're about to write the data.
4106 if ((!bma
->wasdel
|| (bma
->flags
& XFS_BMAPI_COWFORK
)) &&
4107 (bma
->flags
& XFS_BMAPI_PREALLOC
) &&
4108 xfs_sb_version_hasextflgbit(&mp
->m_sb
))
4109 bma
->got
.br_state
= XFS_EXT_UNWRITTEN
;
4112 error
= xfs_bmap_add_extent_delay_real(bma
, whichfork
);
4114 error
= xfs_bmap_add_extent_hole_real(bma
->tp
, bma
->ip
,
4115 whichfork
, &bma
->icur
, &bma
->cur
, &bma
->got
,
4116 bma
->firstblock
, bma
->dfops
, &bma
->logflags
,
4119 bma
->logflags
|= tmp_logflags
;
4124 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4125 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4126 * the neighbouring ones.
4128 xfs_iext_get_extent(ifp
, &bma
->icur
, &bma
->got
);
4130 ASSERT(bma
->got
.br_startoff
<= bma
->offset
);
4131 ASSERT(bma
->got
.br_startoff
+ bma
->got
.br_blockcount
>=
4132 bma
->offset
+ bma
->length
);
4133 ASSERT(bma
->got
.br_state
== XFS_EXT_NORM
||
4134 bma
->got
.br_state
== XFS_EXT_UNWRITTEN
);
4139 xfs_bmapi_convert_unwritten(
4140 struct xfs_bmalloca
*bma
,
4141 struct xfs_bmbt_irec
*mval
,
4145 int whichfork
= xfs_bmapi_whichfork(flags
);
4146 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
4147 int tmp_logflags
= 0;
4150 /* check if we need to do unwritten->real conversion */
4151 if (mval
->br_state
== XFS_EXT_UNWRITTEN
&&
4152 (flags
& XFS_BMAPI_PREALLOC
))
4155 /* check if we need to do real->unwritten conversion */
4156 if (mval
->br_state
== XFS_EXT_NORM
&&
4157 (flags
& (XFS_BMAPI_PREALLOC
| XFS_BMAPI_CONVERT
)) !=
4158 (XFS_BMAPI_PREALLOC
| XFS_BMAPI_CONVERT
))
4162 * Modify (by adding) the state flag, if writing.
4164 ASSERT(mval
->br_blockcount
<= len
);
4165 if ((ifp
->if_flags
& XFS_IFBROOT
) && !bma
->cur
) {
4166 bma
->cur
= xfs_bmbt_init_cursor(bma
->ip
->i_mount
, bma
->tp
,
4167 bma
->ip
, whichfork
);
4168 bma
->cur
->bc_private
.b
.firstblock
= *bma
->firstblock
;
4169 bma
->cur
->bc_private
.b
.dfops
= bma
->dfops
;
4171 mval
->br_state
= (mval
->br_state
== XFS_EXT_UNWRITTEN
)
4172 ? XFS_EXT_NORM
: XFS_EXT_UNWRITTEN
;
4175 * Before insertion into the bmbt, zero the range being converted
4178 if (flags
& XFS_BMAPI_ZERO
) {
4179 error
= xfs_zero_extent(bma
->ip
, mval
->br_startblock
,
4180 mval
->br_blockcount
);
4185 error
= xfs_bmap_add_extent_unwritten_real(bma
->tp
, bma
->ip
, whichfork
,
4186 &bma
->icur
, &bma
->cur
, mval
, bma
->firstblock
,
4187 bma
->dfops
, &tmp_logflags
);
4189 * Log the inode core unconditionally in the unwritten extent conversion
4190 * path because the conversion might not have done so (e.g., if the
4191 * extent count hasn't changed). We need to make sure the inode is dirty
4192 * in the transaction for the sake of fsync(), even if nothing has
4193 * changed, because fsync() will not force the log for this transaction
4194 * unless it sees the inode pinned.
4196 * Note: If we're only converting cow fork extents, there aren't
4197 * any on-disk updates to make, so we don't need to log anything.
4199 if (whichfork
!= XFS_COW_FORK
)
4200 bma
->logflags
|= tmp_logflags
| XFS_ILOG_CORE
;
4205 * Update our extent pointer, given that
4206 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4207 * of the neighbouring ones.
4209 xfs_iext_get_extent(ifp
, &bma
->icur
, &bma
->got
);
4212 * We may have combined previously unwritten space with written space,
4213 * so generate another request.
4215 if (mval
->br_blockcount
< len
)
4221 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4222 * extent state if necessary. Details behaviour is controlled by the flags
4223 * parameter. Only allocates blocks from a single allocation group, to avoid
4226 * The returned value in "firstblock" from the first call in a transaction
4227 * must be remembered and presented to subsequent calls in "firstblock".
4228 * An upper bound for the number of blocks to be allocated is supplied to
4229 * the first call in "total"; if no allocation group has that many free
4230 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4234 struct xfs_trans
*tp
, /* transaction pointer */
4235 struct xfs_inode
*ip
, /* incore inode */
4236 xfs_fileoff_t bno
, /* starting file offs. mapped */
4237 xfs_filblks_t len
, /* length to map in file */
4238 int flags
, /* XFS_BMAPI_... */
4239 xfs_fsblock_t
*firstblock
, /* first allocated block
4240 controls a.g. for allocs */
4241 xfs_extlen_t total
, /* total blocks needed */
4242 struct xfs_bmbt_irec
*mval
, /* output: map values */
4243 int *nmap
, /* i/o: mval size/count */
4244 struct xfs_defer_ops
*dfops
) /* i/o: list extents to free */
4246 struct xfs_mount
*mp
= ip
->i_mount
;
4247 struct xfs_ifork
*ifp
;
4248 struct xfs_bmalloca bma
= { NULL
}; /* args for xfs_bmap_alloc */
4249 xfs_fileoff_t end
; /* end of mapped file region */
4250 bool eof
= false; /* after the end of extents */
4251 int error
; /* error return */
4252 int n
; /* current extent index */
4253 xfs_fileoff_t obno
; /* old block number (offset) */
4254 int whichfork
; /* data or attr fork */
4257 xfs_fileoff_t orig_bno
; /* original block number value */
4258 int orig_flags
; /* original flags arg value */
4259 xfs_filblks_t orig_len
; /* original value of len arg */
4260 struct xfs_bmbt_irec
*orig_mval
; /* original value of mval */
4261 int orig_nmap
; /* original value of *nmap */
4269 whichfork
= xfs_bmapi_whichfork(flags
);
4272 ASSERT(*nmap
<= XFS_BMAP_MAX_NMAP
);
4273 ASSERT(!(flags
& XFS_BMAPI_IGSTATE
));
4274 ASSERT(tp
!= NULL
||
4275 (flags
& (XFS_BMAPI_CONVERT
| XFS_BMAPI_COWFORK
)) ==
4276 (XFS_BMAPI_CONVERT
| XFS_BMAPI_COWFORK
));
4278 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_LOCAL
);
4279 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
4280 ASSERT(!(flags
& XFS_BMAPI_REMAP
));
4282 /* zeroing is for currently only for data extents, not metadata */
4283 ASSERT((flags
& (XFS_BMAPI_METADATA
| XFS_BMAPI_ZERO
)) !=
4284 (XFS_BMAPI_METADATA
| XFS_BMAPI_ZERO
));
4286 * we can allocate unwritten extents or pre-zero allocated blocks,
4287 * but it makes no sense to do both at once. This would result in
4288 * zeroing the unwritten extent twice, but it still being an
4289 * unwritten extent....
4291 ASSERT((flags
& (XFS_BMAPI_PREALLOC
| XFS_BMAPI_ZERO
)) !=
4292 (XFS_BMAPI_PREALLOC
| XFS_BMAPI_ZERO
));
4294 if (unlikely(XFS_TEST_ERROR(
4295 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
4296 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
4297 mp
, XFS_ERRTAG_BMAPIFORMAT
))) {
4298 XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW
, mp
);
4299 return -EFSCORRUPTED
;
4302 if (XFS_FORCED_SHUTDOWN(mp
))
4305 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4307 XFS_STATS_INC(mp
, xs_blk_mapw
);
4309 if (*firstblock
== NULLFSBLOCK
) {
4310 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
)
4311 bma
.minleft
= be16_to_cpu(ifp
->if_broot
->bb_level
) + 1;
4318 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
4319 error
= xfs_iread_extents(tp
, ip
, whichfork
);
4328 if (!xfs_iext_lookup_extent(ip
, ifp
, bno
, &bma
.icur
, &bma
.got
))
4330 if (!xfs_iext_peek_prev_extent(ifp
, &bma
.icur
, &bma
.prev
))
4331 bma
.prev
.br_startoff
= NULLFILEOFF
;
4337 bma
.firstblock
= firstblock
;
4339 while (bno
< end
&& n
< *nmap
) {
4340 bool need_alloc
= false, wasdelay
= false;
4342 /* in hole or beyond EOF? */
4343 if (eof
|| bma
.got
.br_startoff
> bno
) {
4345 * CoW fork conversions should /never/ hit EOF or
4346 * holes. There should always be something for us
4349 ASSERT(!((flags
& XFS_BMAPI_CONVERT
) &&
4350 (flags
& XFS_BMAPI_COWFORK
)));
4352 if (flags
& XFS_BMAPI_DELALLOC
) {
4354 * For the COW fork we can reasonably get a
4355 * request for converting an extent that races
4356 * with other threads already having converted
4357 * part of it, as there converting COW to
4358 * regular blocks is not protected using the
4361 ASSERT(flags
& XFS_BMAPI_COWFORK
);
4362 if (!(flags
& XFS_BMAPI_COWFORK
)) {
4367 if (eof
|| bno
>= end
)
4372 } else if (isnullstartblock(bma
.got
.br_startblock
)) {
4377 * First, deal with the hole before the allocated space
4378 * that we found, if any.
4380 if ((need_alloc
|| wasdelay
) &&
4381 !(flags
& XFS_BMAPI_CONVERT_ONLY
)) {
4383 bma
.conv
= !!(flags
& XFS_BMAPI_CONVERT
);
4384 bma
.wasdel
= wasdelay
;
4389 * There's a 32/64 bit type mismatch between the
4390 * allocation length request (which can be 64 bits in
4391 * length) and the bma length request, which is
4392 * xfs_extlen_t and therefore 32 bits. Hence we have to
4393 * check for 32-bit overflows and handle them here.
4395 if (len
> (xfs_filblks_t
)MAXEXTLEN
)
4396 bma
.length
= MAXEXTLEN
;
4401 ASSERT(bma
.length
> 0);
4402 error
= xfs_bmapi_allocate(&bma
);
4405 if (bma
.blkno
== NULLFSBLOCK
)
4409 * If this is a CoW allocation, record the data in
4410 * the refcount btree for orphan recovery.
4412 if (whichfork
== XFS_COW_FORK
) {
4413 error
= xfs_refcount_alloc_cow_extent(mp
, dfops
,
4414 bma
.blkno
, bma
.length
);
4420 /* Deal with the allocated space we found. */
4421 xfs_bmapi_trim_map(mval
, &bma
.got
, &bno
, len
, obno
,
4424 /* Execute unwritten extent conversion if necessary */
4425 error
= xfs_bmapi_convert_unwritten(&bma
, mval
, len
, flags
);
4426 if (error
== -EAGAIN
)
4431 /* update the extent map to return */
4432 xfs_bmapi_update_map(&mval
, &bno
, &len
, obno
, end
, &n
, flags
);
4435 * If we're done, stop now. Stop when we've allocated
4436 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4437 * the transaction may get too big.
4439 if (bno
>= end
|| n
>= *nmap
|| bma
.nallocs
>= *nmap
)
4442 /* Else go on to the next record. */
4444 if (!xfs_iext_next_extent(ifp
, &bma
.icur
, &bma
.got
))
4450 * Transform from btree to extents, give it cur.
4452 if (xfs_bmap_wants_extents(ip
, whichfork
)) {
4453 int tmp_logflags
= 0;
4456 error
= xfs_bmap_btree_to_extents(tp
, ip
, bma
.cur
,
4457 &tmp_logflags
, whichfork
);
4458 bma
.logflags
|= tmp_logflags
;
4463 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
||
4464 XFS_IFORK_NEXTENTS(ip
, whichfork
) >
4465 XFS_IFORK_MAXEXT(ip
, whichfork
));
4469 * Log everything. Do this after conversion, there's no point in
4470 * logging the extent records if we've converted to btree format.
4472 if ((bma
.logflags
& xfs_ilog_fext(whichfork
)) &&
4473 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
4474 bma
.logflags
&= ~xfs_ilog_fext(whichfork
);
4475 else if ((bma
.logflags
& xfs_ilog_fbroot(whichfork
)) &&
4476 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)
4477 bma
.logflags
&= ~xfs_ilog_fbroot(whichfork
);
4479 * Log whatever the flags say, even if error. Otherwise we might miss
4480 * detecting a case where the data is changed, there's an error,
4481 * and it's not logged so we don't shutdown when we should.
4484 xfs_trans_log_inode(tp
, ip
, bma
.logflags
);
4488 ASSERT(*firstblock
== NULLFSBLOCK
||
4489 XFS_FSB_TO_AGNO(mp
, *firstblock
) <=
4491 bma
.cur
->bc_private
.b
.firstblock
));
4492 *firstblock
= bma
.cur
->bc_private
.b
.firstblock
;
4494 xfs_btree_del_cursor(bma
.cur
,
4495 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
4498 xfs_bmap_validate_ret(orig_bno
, orig_len
, orig_flags
, orig_mval
,
4505 struct xfs_trans
*tp
,
4506 struct xfs_inode
*ip
,
4509 xfs_fsblock_t startblock
,
4510 struct xfs_defer_ops
*dfops
,
4513 struct xfs_mount
*mp
= ip
->i_mount
;
4514 struct xfs_ifork
*ifp
;
4515 struct xfs_btree_cur
*cur
= NULL
;
4516 xfs_fsblock_t firstblock
= NULLFSBLOCK
;
4517 struct xfs_bmbt_irec got
;
4518 struct xfs_iext_cursor icur
;
4519 int whichfork
= xfs_bmapi_whichfork(flags
);
4520 int logflags
= 0, error
;
4522 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4524 ASSERT(len
<= (xfs_filblks_t
)MAXEXTLEN
);
4525 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
4526 ASSERT(!(flags
& ~(XFS_BMAPI_ATTRFORK
| XFS_BMAPI_PREALLOC
|
4527 XFS_BMAPI_NORMAP
)));
4528 ASSERT((flags
& (XFS_BMAPI_ATTRFORK
| XFS_BMAPI_PREALLOC
)) !=
4529 (XFS_BMAPI_ATTRFORK
| XFS_BMAPI_PREALLOC
));
4531 if (unlikely(XFS_TEST_ERROR(
4532 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
4533 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
4534 mp
, XFS_ERRTAG_BMAPIFORMAT
))) {
4535 XFS_ERROR_REPORT("xfs_bmapi_remap", XFS_ERRLEVEL_LOW
, mp
);
4536 return -EFSCORRUPTED
;
4539 if (XFS_FORCED_SHUTDOWN(mp
))
4542 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
4543 error
= xfs_iread_extents(tp
, ip
, whichfork
);
4548 if (xfs_iext_lookup_extent(ip
, ifp
, bno
, &icur
, &got
)) {
4549 /* make sure we only reflink into a hole. */
4550 ASSERT(got
.br_startoff
> bno
);
4551 ASSERT(got
.br_startoff
- bno
>= len
);
4554 ip
->i_d
.di_nblocks
+= len
;
4555 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
4557 if (ifp
->if_flags
& XFS_IFBROOT
) {
4558 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
4559 cur
->bc_private
.b
.firstblock
= firstblock
;
4560 cur
->bc_private
.b
.dfops
= dfops
;
4561 cur
->bc_private
.b
.flags
= 0;
4564 got
.br_startoff
= bno
;
4565 got
.br_startblock
= startblock
;
4566 got
.br_blockcount
= len
;
4567 if (flags
& XFS_BMAPI_PREALLOC
)
4568 got
.br_state
= XFS_EXT_UNWRITTEN
;
4570 got
.br_state
= XFS_EXT_NORM
;
4572 error
= xfs_bmap_add_extent_hole_real(tp
, ip
, whichfork
, &icur
,
4573 &cur
, &got
, &firstblock
, dfops
, &logflags
, flags
);
4577 if (xfs_bmap_wants_extents(ip
, whichfork
)) {
4578 int tmp_logflags
= 0;
4580 error
= xfs_bmap_btree_to_extents(tp
, ip
, cur
,
4581 &tmp_logflags
, whichfork
);
4582 logflags
|= tmp_logflags
;
4586 if (ip
->i_d
.di_format
!= XFS_DINODE_FMT_EXTENTS
)
4587 logflags
&= ~XFS_ILOG_DEXT
;
4588 else if (ip
->i_d
.di_format
!= XFS_DINODE_FMT_BTREE
)
4589 logflags
&= ~XFS_ILOG_DBROOT
;
4592 xfs_trans_log_inode(tp
, ip
, logflags
);
4594 xfs_btree_del_cursor(cur
,
4595 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
4601 * When a delalloc extent is split (e.g., due to a hole punch), the original
4602 * indlen reservation must be shared across the two new extents that are left
4605 * Given the original reservation and the worst case indlen for the two new
4606 * extents (as calculated by xfs_bmap_worst_indlen()), split the original
4607 * reservation fairly across the two new extents. If necessary, steal available
4608 * blocks from a deleted extent to make up a reservation deficiency (e.g., if
4609 * ores == 1). The number of stolen blocks is returned. The availability and
4610 * subsequent accounting of stolen blocks is the responsibility of the caller.
4612 static xfs_filblks_t
4613 xfs_bmap_split_indlen(
4614 xfs_filblks_t ores
, /* original res. */
4615 xfs_filblks_t
*indlen1
, /* ext1 worst indlen */
4616 xfs_filblks_t
*indlen2
, /* ext2 worst indlen */
4617 xfs_filblks_t avail
) /* stealable blocks */
4619 xfs_filblks_t len1
= *indlen1
;
4620 xfs_filblks_t len2
= *indlen2
;
4621 xfs_filblks_t nres
= len1
+ len2
; /* new total res. */
4622 xfs_filblks_t stolen
= 0;
4623 xfs_filblks_t resfactor
;
4626 * Steal as many blocks as we can to try and satisfy the worst case
4627 * indlen for both new extents.
4629 if (ores
< nres
&& avail
)
4630 stolen
= XFS_FILBLKS_MIN(nres
- ores
, avail
);
4633 /* nothing else to do if we've satisfied the new reservation */
4638 * We can't meet the total required reservation for the two extents.
4639 * Calculate the percent of the overall shortage between both extents
4640 * and apply this percentage to each of the requested indlen values.
4641 * This distributes the shortage fairly and reduces the chances that one
4642 * of the two extents is left with nothing when extents are repeatedly
4645 resfactor
= (ores
* 100);
4646 do_div(resfactor
, nres
);
4651 ASSERT(len1
+ len2
<= ores
);
4652 ASSERT(len1
< *indlen1
&& len2
< *indlen2
);
4655 * Hand out the remainder to each extent. If one of the two reservations
4656 * is zero, we want to make sure that one gets a block first. The loop
4657 * below starts with len1, so hand len2 a block right off the bat if it
4660 ores
-= (len1
+ len2
);
4661 ASSERT((*indlen1
- len1
) + (*indlen2
- len2
) >= ores
);
4662 if (ores
&& !len2
&& *indlen2
) {
4667 if (len1
< *indlen1
) {
4673 if (len2
< *indlen2
) {
4686 xfs_bmap_del_extent_delay(
4687 struct xfs_inode
*ip
,
4689 struct xfs_iext_cursor
*icur
,
4690 struct xfs_bmbt_irec
*got
,
4691 struct xfs_bmbt_irec
*del
)
4693 struct xfs_mount
*mp
= ip
->i_mount
;
4694 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4695 struct xfs_bmbt_irec
new;
4696 int64_t da_old
, da_new
, da_diff
= 0;
4697 xfs_fileoff_t del_endoff
, got_endoff
;
4698 xfs_filblks_t got_indlen
, new_indlen
, stolen
;
4699 int state
= xfs_bmap_fork_to_state(whichfork
);
4703 XFS_STATS_INC(mp
, xs_del_exlist
);
4705 isrt
= (whichfork
== XFS_DATA_FORK
) && XFS_IS_REALTIME_INODE(ip
);
4706 del_endoff
= del
->br_startoff
+ del
->br_blockcount
;
4707 got_endoff
= got
->br_startoff
+ got
->br_blockcount
;
4708 da_old
= startblockval(got
->br_startblock
);
4711 ASSERT(del
->br_blockcount
> 0);
4712 ASSERT(got
->br_startoff
<= del
->br_startoff
);
4713 ASSERT(got_endoff
>= del_endoff
);
4716 uint64_t rtexts
= XFS_FSB_TO_B(mp
, del
->br_blockcount
);
4718 do_div(rtexts
, mp
->m_sb
.sb_rextsize
);
4719 xfs_mod_frextents(mp
, rtexts
);
4723 * Update the inode delalloc counter now and wait to update the
4724 * sb counters as we might have to borrow some blocks for the
4725 * indirect block accounting.
4727 error
= xfs_trans_reserve_quota_nblks(NULL
, ip
,
4728 -((long)del
->br_blockcount
), 0,
4729 isrt
? XFS_QMOPT_RES_RTBLKS
: XFS_QMOPT_RES_REGBLKS
);
4732 ip
->i_delayed_blks
-= del
->br_blockcount
;
4734 if (got
->br_startoff
== del
->br_startoff
)
4735 state
|= BMAP_LEFT_FILLING
;
4736 if (got_endoff
== del_endoff
)
4737 state
|= BMAP_RIGHT_FILLING
;
4739 switch (state
& (BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
)) {
4740 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
4742 * Matches the whole extent. Delete the entry.
4744 xfs_iext_remove(ip
, icur
, state
);
4745 xfs_iext_prev(ifp
, icur
);
4747 case BMAP_LEFT_FILLING
:
4749 * Deleting the first part of the extent.
4751 got
->br_startoff
= del_endoff
;
4752 got
->br_blockcount
-= del
->br_blockcount
;
4753 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
,
4754 got
->br_blockcount
), da_old
);
4755 got
->br_startblock
= nullstartblock((int)da_new
);
4756 xfs_iext_update_extent(ip
, state
, icur
, got
);
4758 case BMAP_RIGHT_FILLING
:
4760 * Deleting the last part of the extent.
4762 got
->br_blockcount
= got
->br_blockcount
- del
->br_blockcount
;
4763 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
,
4764 got
->br_blockcount
), da_old
);
4765 got
->br_startblock
= nullstartblock((int)da_new
);
4766 xfs_iext_update_extent(ip
, state
, icur
, got
);
4770 * Deleting the middle of the extent.
4772 * Distribute the original indlen reservation across the two new
4773 * extents. Steal blocks from the deleted extent if necessary.
4774 * Stealing blocks simply fudges the fdblocks accounting below.
4775 * Warn if either of the new indlen reservations is zero as this
4776 * can lead to delalloc problems.
4778 got
->br_blockcount
= del
->br_startoff
- got
->br_startoff
;
4779 got_indlen
= xfs_bmap_worst_indlen(ip
, got
->br_blockcount
);
4781 new.br_blockcount
= got_endoff
- del_endoff
;
4782 new_indlen
= xfs_bmap_worst_indlen(ip
, new.br_blockcount
);
4784 WARN_ON_ONCE(!got_indlen
|| !new_indlen
);
4785 stolen
= xfs_bmap_split_indlen(da_old
, &got_indlen
, &new_indlen
,
4786 del
->br_blockcount
);
4788 got
->br_startblock
= nullstartblock((int)got_indlen
);
4790 new.br_startoff
= del_endoff
;
4791 new.br_state
= got
->br_state
;
4792 new.br_startblock
= nullstartblock((int)new_indlen
);
4794 xfs_iext_update_extent(ip
, state
, icur
, got
);
4795 xfs_iext_next(ifp
, icur
);
4796 xfs_iext_insert(ip
, icur
, &new, state
);
4798 da_new
= got_indlen
+ new_indlen
- stolen
;
4799 del
->br_blockcount
-= stolen
;
4803 ASSERT(da_old
>= da_new
);
4804 da_diff
= da_old
- da_new
;
4806 da_diff
+= del
->br_blockcount
;
4808 xfs_mod_fdblocks(mp
, da_diff
, false);
4813 xfs_bmap_del_extent_cow(
4814 struct xfs_inode
*ip
,
4815 struct xfs_iext_cursor
*icur
,
4816 struct xfs_bmbt_irec
*got
,
4817 struct xfs_bmbt_irec
*del
)
4819 struct xfs_mount
*mp
= ip
->i_mount
;
4820 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, XFS_COW_FORK
);
4821 struct xfs_bmbt_irec
new;
4822 xfs_fileoff_t del_endoff
, got_endoff
;
4823 int state
= BMAP_COWFORK
;
4825 XFS_STATS_INC(mp
, xs_del_exlist
);
4827 del_endoff
= del
->br_startoff
+ del
->br_blockcount
;
4828 got_endoff
= got
->br_startoff
+ got
->br_blockcount
;
4830 ASSERT(del
->br_blockcount
> 0);
4831 ASSERT(got
->br_startoff
<= del
->br_startoff
);
4832 ASSERT(got_endoff
>= del_endoff
);
4833 ASSERT(!isnullstartblock(got
->br_startblock
));
4835 if (got
->br_startoff
== del
->br_startoff
)
4836 state
|= BMAP_LEFT_FILLING
;
4837 if (got_endoff
== del_endoff
)
4838 state
|= BMAP_RIGHT_FILLING
;
4840 switch (state
& (BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
)) {
4841 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
4843 * Matches the whole extent. Delete the entry.
4845 xfs_iext_remove(ip
, icur
, state
);
4846 xfs_iext_prev(ifp
, icur
);
4848 case BMAP_LEFT_FILLING
:
4850 * Deleting the first part of the extent.
4852 got
->br_startoff
= del_endoff
;
4853 got
->br_blockcount
-= del
->br_blockcount
;
4854 got
->br_startblock
= del
->br_startblock
+ del
->br_blockcount
;
4855 xfs_iext_update_extent(ip
, state
, icur
, got
);
4857 case BMAP_RIGHT_FILLING
:
4859 * Deleting the last part of the extent.
4861 got
->br_blockcount
-= del
->br_blockcount
;
4862 xfs_iext_update_extent(ip
, state
, icur
, got
);
4866 * Deleting the middle of the extent.
4868 got
->br_blockcount
= del
->br_startoff
- got
->br_startoff
;
4870 new.br_startoff
= del_endoff
;
4871 new.br_blockcount
= got_endoff
- del_endoff
;
4872 new.br_state
= got
->br_state
;
4873 new.br_startblock
= del
->br_startblock
+ del
->br_blockcount
;
4875 xfs_iext_update_extent(ip
, state
, icur
, got
);
4876 xfs_iext_next(ifp
, icur
);
4877 xfs_iext_insert(ip
, icur
, &new, state
);
4880 ip
->i_delayed_blks
-= del
->br_blockcount
;
4884 * Called by xfs_bmapi to update file extent records and the btree
4885 * after removing space.
4887 STATIC
int /* error */
4888 xfs_bmap_del_extent_real(
4889 xfs_inode_t
*ip
, /* incore inode pointer */
4890 xfs_trans_t
*tp
, /* current transaction pointer */
4891 struct xfs_iext_cursor
*icur
,
4892 struct xfs_defer_ops
*dfops
, /* list of extents to be freed */
4893 xfs_btree_cur_t
*cur
, /* if null, not a btree */
4894 xfs_bmbt_irec_t
*del
, /* data to remove from extents */
4895 int *logflagsp
, /* inode logging flags */
4896 int whichfork
, /* data or attr fork */
4897 int bflags
) /* bmapi flags */
4899 xfs_fsblock_t del_endblock
=0; /* first block past del */
4900 xfs_fileoff_t del_endoff
; /* first offset past del */
4901 int do_fx
; /* free extent at end of routine */
4902 int error
; /* error return value */
4903 int flags
= 0;/* inode logging flags */
4904 struct xfs_bmbt_irec got
; /* current extent entry */
4905 xfs_fileoff_t got_endoff
; /* first offset past got */
4906 int i
; /* temp state */
4907 xfs_ifork_t
*ifp
; /* inode fork pointer */
4908 xfs_mount_t
*mp
; /* mount structure */
4909 xfs_filblks_t nblks
; /* quota/sb block count */
4910 xfs_bmbt_irec_t
new; /* new record to be inserted */
4912 uint qfield
; /* quota field to update */
4913 int state
= xfs_bmap_fork_to_state(whichfork
);
4914 struct xfs_bmbt_irec old
;
4917 XFS_STATS_INC(mp
, xs_del_exlist
);
4919 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4920 ASSERT(del
->br_blockcount
> 0);
4921 xfs_iext_get_extent(ifp
, icur
, &got
);
4922 ASSERT(got
.br_startoff
<= del
->br_startoff
);
4923 del_endoff
= del
->br_startoff
+ del
->br_blockcount
;
4924 got_endoff
= got
.br_startoff
+ got
.br_blockcount
;
4925 ASSERT(got_endoff
>= del_endoff
);
4926 ASSERT(!isnullstartblock(got
.br_startblock
));
4931 * If it's the case where the directory code is running with no block
4932 * reservation, and the deleted block is in the middle of its extent,
4933 * and the resulting insert of an extent would cause transformation to
4934 * btree format, then reject it. The calling code will then swap blocks
4935 * around instead. We have to do this now, rather than waiting for the
4936 * conversion to btree format, since the transaction will be dirty then.
4938 if (tp
->t_blk_res
== 0 &&
4939 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
&&
4940 XFS_IFORK_NEXTENTS(ip
, whichfork
) >=
4941 XFS_IFORK_MAXEXT(ip
, whichfork
) &&
4942 del
->br_startoff
> got
.br_startoff
&& del_endoff
< got_endoff
)
4945 flags
= XFS_ILOG_CORE
;
4946 if (whichfork
== XFS_DATA_FORK
&& XFS_IS_REALTIME_INODE(ip
)) {
4951 bno
= div_u64_rem(del
->br_startblock
, mp
->m_sb
.sb_rextsize
,
4954 len
= div_u64_rem(del
->br_blockcount
, mp
->m_sb
.sb_rextsize
,
4958 error
= xfs_rtfree_extent(tp
, bno
, (xfs_extlen_t
)len
);
4962 nblks
= len
* mp
->m_sb
.sb_rextsize
;
4963 qfield
= XFS_TRANS_DQ_RTBCOUNT
;
4966 nblks
= del
->br_blockcount
;
4967 qfield
= XFS_TRANS_DQ_BCOUNT
;
4970 del_endblock
= del
->br_startblock
+ del
->br_blockcount
;
4972 error
= xfs_bmbt_lookup_eq(cur
, &got
, &i
);
4975 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
4978 if (got
.br_startoff
== del
->br_startoff
)
4979 state
|= BMAP_LEFT_FILLING
;
4980 if (got_endoff
== del_endoff
)
4981 state
|= BMAP_RIGHT_FILLING
;
4983 switch (state
& (BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
)) {
4984 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
4986 * Matches the whole extent. Delete the entry.
4988 xfs_iext_remove(ip
, icur
, state
);
4989 xfs_iext_prev(ifp
, icur
);
4990 XFS_IFORK_NEXT_SET(ip
, whichfork
,
4991 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
4992 flags
|= XFS_ILOG_CORE
;
4994 flags
|= xfs_ilog_fext(whichfork
);
4997 if ((error
= xfs_btree_delete(cur
, &i
)))
4999 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
5001 case BMAP_LEFT_FILLING
:
5003 * Deleting the first part of the extent.
5005 got
.br_startoff
= del_endoff
;
5006 got
.br_startblock
= del_endblock
;
5007 got
.br_blockcount
-= del
->br_blockcount
;
5008 xfs_iext_update_extent(ip
, state
, icur
, &got
);
5010 flags
|= xfs_ilog_fext(whichfork
);
5013 error
= xfs_bmbt_update(cur
, &got
);
5017 case BMAP_RIGHT_FILLING
:
5019 * Deleting the last part of the extent.
5021 got
.br_blockcount
-= del
->br_blockcount
;
5022 xfs_iext_update_extent(ip
, state
, icur
, &got
);
5024 flags
|= xfs_ilog_fext(whichfork
);
5027 error
= xfs_bmbt_update(cur
, &got
);
5033 * Deleting the middle of the extent.
5037 got
.br_blockcount
= del
->br_startoff
- got
.br_startoff
;
5038 xfs_iext_update_extent(ip
, state
, icur
, &got
);
5040 new.br_startoff
= del_endoff
;
5041 new.br_blockcount
= got_endoff
- del_endoff
;
5042 new.br_state
= got
.br_state
;
5043 new.br_startblock
= del_endblock
;
5045 flags
|= XFS_ILOG_CORE
;
5047 error
= xfs_bmbt_update(cur
, &got
);
5050 error
= xfs_btree_increment(cur
, 0, &i
);
5053 cur
->bc_rec
.b
= new;
5054 error
= xfs_btree_insert(cur
, &i
);
5055 if (error
&& error
!= -ENOSPC
)
5058 * If get no-space back from btree insert, it tried a
5059 * split, and we have a zero block reservation. Fix up
5060 * our state and return the error.
5062 if (error
== -ENOSPC
) {
5064 * Reset the cursor, don't trust it after any
5067 error
= xfs_bmbt_lookup_eq(cur
, &got
, &i
);
5070 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
5072 * Update the btree record back
5073 * to the original value.
5075 error
= xfs_bmbt_update(cur
, &old
);
5079 * Reset the extent record back
5080 * to the original value.
5082 xfs_iext_update_extent(ip
, state
, icur
, &old
);
5087 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
5089 flags
|= xfs_ilog_fext(whichfork
);
5090 XFS_IFORK_NEXT_SET(ip
, whichfork
,
5091 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
5092 xfs_iext_next(ifp
, icur
);
5093 xfs_iext_insert(ip
, icur
, &new, state
);
5097 /* remove reverse mapping */
5098 error
= xfs_rmap_unmap_extent(mp
, dfops
, ip
, whichfork
, del
);
5103 * If we need to, add to list of extents to delete.
5105 if (do_fx
&& !(bflags
& XFS_BMAPI_REMAP
)) {
5106 if (xfs_is_reflink_inode(ip
) && whichfork
== XFS_DATA_FORK
) {
5107 error
= xfs_refcount_decrease_extent(mp
, dfops
, del
);
5111 __xfs_bmap_add_free(mp
, dfops
, del
->br_startblock
,
5112 del
->br_blockcount
, NULL
,
5113 (bflags
& XFS_BMAPI_NODISCARD
) ||
5114 del
->br_state
== XFS_EXT_UNWRITTEN
);
5119 * Adjust inode # blocks in the file.
5122 ip
->i_d
.di_nblocks
-= nblks
;
5124 * Adjust quota data.
5126 if (qfield
&& !(bflags
& XFS_BMAPI_REMAP
))
5127 xfs_trans_mod_dquot_byino(tp
, ip
, qfield
, (long)-nblks
);
5135 * Unmap (remove) blocks from a file.
5136 * If nexts is nonzero then the number of extents to remove is limited to
5137 * that value. If not all extents in the block range can be removed then
5142 xfs_trans_t
*tp
, /* transaction pointer */
5143 struct xfs_inode
*ip
, /* incore inode */
5144 xfs_fileoff_t start
, /* first file offset deleted */
5145 xfs_filblks_t
*rlen
, /* i/o: amount remaining */
5146 int flags
, /* misc flags */
5147 xfs_extnum_t nexts
, /* number of extents max */
5148 xfs_fsblock_t
*firstblock
, /* first allocated block
5149 controls a.g. for allocs */
5150 struct xfs_defer_ops
*dfops
) /* i/o: deferred updates */
5152 xfs_btree_cur_t
*cur
; /* bmap btree cursor */
5153 xfs_bmbt_irec_t del
; /* extent being deleted */
5154 int error
; /* error return value */
5155 xfs_extnum_t extno
; /* extent number in list */
5156 xfs_bmbt_irec_t got
; /* current extent record */
5157 xfs_ifork_t
*ifp
; /* inode fork pointer */
5158 int isrt
; /* freeing in rt area */
5159 int logflags
; /* transaction logging flags */
5160 xfs_extlen_t mod
; /* rt extent offset */
5161 xfs_mount_t
*mp
; /* mount structure */
5162 int tmp_logflags
; /* partial logging flags */
5163 int wasdel
; /* was a delayed alloc extent */
5164 int whichfork
; /* data or attribute fork */
5166 xfs_filblks_t len
= *rlen
; /* length to unmap in file */
5167 xfs_fileoff_t max_len
;
5168 xfs_agnumber_t prev_agno
= NULLAGNUMBER
, agno
;
5170 struct xfs_iext_cursor icur
;
5173 trace_xfs_bunmap(ip
, start
, len
, flags
, _RET_IP_
);
5175 whichfork
= xfs_bmapi_whichfork(flags
);
5176 ASSERT(whichfork
!= XFS_COW_FORK
);
5177 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5179 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
5180 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)) {
5181 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW
,
5183 return -EFSCORRUPTED
;
5186 if (XFS_FORCED_SHUTDOWN(mp
))
5189 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
5194 * Guesstimate how many blocks we can unmap without running the risk of
5195 * blowing out the transaction with a mix of EFIs and reflink
5198 if (tp
&& xfs_is_reflink_inode(ip
) && whichfork
== XFS_DATA_FORK
)
5199 max_len
= min(len
, xfs_refcount_max_unmap(tp
->t_log_res
));
5203 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
5204 (error
= xfs_iread_extents(tp
, ip
, whichfork
)))
5206 if (xfs_iext_count(ifp
) == 0) {
5210 XFS_STATS_INC(mp
, xs_blk_unmap
);
5211 isrt
= (whichfork
== XFS_DATA_FORK
) && XFS_IS_REALTIME_INODE(ip
);
5214 if (!xfs_iext_lookup_extent_before(ip
, ifp
, &end
, &icur
, &got
)) {
5221 if (ifp
->if_flags
& XFS_IFBROOT
) {
5222 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
);
5223 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
5224 cur
->bc_private
.b
.firstblock
= *firstblock
;
5225 cur
->bc_private
.b
.dfops
= dfops
;
5226 cur
->bc_private
.b
.flags
= 0;
5232 * Synchronize by locking the bitmap inode.
5234 xfs_ilock(mp
->m_rbmip
, XFS_ILOCK_EXCL
|XFS_ILOCK_RTBITMAP
);
5235 xfs_trans_ijoin(tp
, mp
->m_rbmip
, XFS_ILOCK_EXCL
);
5236 xfs_ilock(mp
->m_rsumip
, XFS_ILOCK_EXCL
|XFS_ILOCK_RTSUM
);
5237 xfs_trans_ijoin(tp
, mp
->m_rsumip
, XFS_ILOCK_EXCL
);
5241 while (end
!= (xfs_fileoff_t
)-1 && end
>= start
&&
5242 (nexts
== 0 || extno
< nexts
) && max_len
> 0) {
5244 * Is the found extent after a hole in which end lives?
5245 * Just back up to the previous extent, if so.
5247 if (got
.br_startoff
> end
&&
5248 !xfs_iext_prev_extent(ifp
, &icur
, &got
)) {
5253 * Is the last block of this extent before the range
5254 * we're supposed to delete? If so, we're done.
5256 end
= XFS_FILEOFF_MIN(end
,
5257 got
.br_startoff
+ got
.br_blockcount
- 1);
5261 * Then deal with the (possibly delayed) allocated space
5265 wasdel
= isnullstartblock(del
.br_startblock
);
5268 * Make sure we don't touch multiple AGF headers out of order
5269 * in a single transaction, as that could cause AB-BA deadlocks.
5272 agno
= XFS_FSB_TO_AGNO(mp
, del
.br_startblock
);
5273 if (prev_agno
!= NULLAGNUMBER
&& prev_agno
> agno
)
5277 if (got
.br_startoff
< start
) {
5278 del
.br_startoff
= start
;
5279 del
.br_blockcount
-= start
- got
.br_startoff
;
5281 del
.br_startblock
+= start
- got
.br_startoff
;
5283 if (del
.br_startoff
+ del
.br_blockcount
> end
+ 1)
5284 del
.br_blockcount
= end
+ 1 - del
.br_startoff
;
5286 /* How much can we safely unmap? */
5287 if (max_len
< del
.br_blockcount
) {
5288 del
.br_startoff
+= del
.br_blockcount
- max_len
;
5290 del
.br_startblock
+= del
.br_blockcount
- max_len
;
5291 del
.br_blockcount
= max_len
;
5297 sum
= del
.br_startblock
+ del
.br_blockcount
;
5298 div_u64_rem(sum
, mp
->m_sb
.sb_rextsize
, &mod
);
5301 * Realtime extent not lined up at the end.
5302 * The extent could have been split into written
5303 * and unwritten pieces, or we could just be
5304 * unmapping part of it. But we can't really
5305 * get rid of part of a realtime extent.
5307 if (del
.br_state
== XFS_EXT_UNWRITTEN
||
5308 !xfs_sb_version_hasextflgbit(&mp
->m_sb
)) {
5310 * This piece is unwritten, or we're not
5311 * using unwritten extents. Skip over it.
5314 end
-= mod
> del
.br_blockcount
?
5315 del
.br_blockcount
: mod
;
5316 if (end
< got
.br_startoff
&&
5317 !xfs_iext_prev_extent(ifp
, &icur
, &got
)) {
5324 * It's written, turn it unwritten.
5325 * This is better than zeroing it.
5327 ASSERT(del
.br_state
== XFS_EXT_NORM
);
5328 ASSERT(tp
->t_blk_res
> 0);
5330 * If this spans a realtime extent boundary,
5331 * chop it back to the start of the one we end at.
5333 if (del
.br_blockcount
> mod
) {
5334 del
.br_startoff
+= del
.br_blockcount
- mod
;
5335 del
.br_startblock
+= del
.br_blockcount
- mod
;
5336 del
.br_blockcount
= mod
;
5338 del
.br_state
= XFS_EXT_UNWRITTEN
;
5339 error
= xfs_bmap_add_extent_unwritten_real(tp
, ip
,
5340 whichfork
, &icur
, &cur
, &del
,
5341 firstblock
, dfops
, &logflags
);
5346 div_u64_rem(del
.br_startblock
, mp
->m_sb
.sb_rextsize
, &mod
);
5349 * Realtime extent is lined up at the end but not
5350 * at the front. We'll get rid of full extents if
5353 mod
= mp
->m_sb
.sb_rextsize
- mod
;
5354 if (del
.br_blockcount
> mod
) {
5355 del
.br_blockcount
-= mod
;
5356 del
.br_startoff
+= mod
;
5357 del
.br_startblock
+= mod
;
5358 } else if ((del
.br_startoff
== start
&&
5359 (del
.br_state
== XFS_EXT_UNWRITTEN
||
5360 tp
->t_blk_res
== 0)) ||
5361 !xfs_sb_version_hasextflgbit(&mp
->m_sb
)) {
5363 * Can't make it unwritten. There isn't
5364 * a full extent here so just skip it.
5366 ASSERT(end
>= del
.br_blockcount
);
5367 end
-= del
.br_blockcount
;
5368 if (got
.br_startoff
> end
&&
5369 !xfs_iext_prev_extent(ifp
, &icur
, &got
)) {
5374 } else if (del
.br_state
== XFS_EXT_UNWRITTEN
) {
5375 struct xfs_bmbt_irec prev
;
5378 * This one is already unwritten.
5379 * It must have a written left neighbor.
5380 * Unwrite the killed part of that one and
5383 if (!xfs_iext_prev_extent(ifp
, &icur
, &prev
))
5385 ASSERT(prev
.br_state
== XFS_EXT_NORM
);
5386 ASSERT(!isnullstartblock(prev
.br_startblock
));
5387 ASSERT(del
.br_startblock
==
5388 prev
.br_startblock
+ prev
.br_blockcount
);
5389 if (prev
.br_startoff
< start
) {
5390 mod
= start
- prev
.br_startoff
;
5391 prev
.br_blockcount
-= mod
;
5392 prev
.br_startblock
+= mod
;
5393 prev
.br_startoff
= start
;
5395 prev
.br_state
= XFS_EXT_UNWRITTEN
;
5396 error
= xfs_bmap_add_extent_unwritten_real(tp
,
5397 ip
, whichfork
, &icur
, &cur
,
5398 &prev
, firstblock
, dfops
,
5404 ASSERT(del
.br_state
== XFS_EXT_NORM
);
5405 del
.br_state
= XFS_EXT_UNWRITTEN
;
5406 error
= xfs_bmap_add_extent_unwritten_real(tp
,
5407 ip
, whichfork
, &icur
, &cur
,
5408 &del
, firstblock
, dfops
,
5418 error
= xfs_bmap_del_extent_delay(ip
, whichfork
, &icur
,
5421 error
= xfs_bmap_del_extent_real(ip
, tp
, &icur
, dfops
,
5422 cur
, &del
, &tmp_logflags
, whichfork
,
5424 logflags
|= tmp_logflags
;
5430 max_len
-= del
.br_blockcount
;
5431 end
= del
.br_startoff
- 1;
5434 * If not done go on to the next (previous) record.
5436 if (end
!= (xfs_fileoff_t
)-1 && end
>= start
) {
5437 if (!xfs_iext_get_extent(ifp
, &icur
, &got
) ||
5438 (got
.br_startoff
> end
&&
5439 !xfs_iext_prev_extent(ifp
, &icur
, &got
))) {
5446 if (done
|| end
== (xfs_fileoff_t
)-1 || end
< start
)
5449 *rlen
= end
- start
+ 1;
5452 * Convert to a btree if necessary.
5454 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
5455 ASSERT(cur
== NULL
);
5456 error
= xfs_bmap_extents_to_btree(tp
, ip
, firstblock
, dfops
,
5457 &cur
, 0, &tmp_logflags
, whichfork
);
5458 logflags
|= tmp_logflags
;
5463 * transform from btree to extents, give it cur
5465 else if (xfs_bmap_wants_extents(ip
, whichfork
)) {
5466 ASSERT(cur
!= NULL
);
5467 error
= xfs_bmap_btree_to_extents(tp
, ip
, cur
, &tmp_logflags
,
5469 logflags
|= tmp_logflags
;
5474 * transform from extents to local?
5479 * Log everything. Do this after conversion, there's no point in
5480 * logging the extent records if we've converted to btree format.
5482 if ((logflags
& xfs_ilog_fext(whichfork
)) &&
5483 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
5484 logflags
&= ~xfs_ilog_fext(whichfork
);
5485 else if ((logflags
& xfs_ilog_fbroot(whichfork
)) &&
5486 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)
5487 logflags
&= ~xfs_ilog_fbroot(whichfork
);
5489 * Log inode even in the error case, if the transaction
5490 * is dirty we'll need to shut down the filesystem.
5493 xfs_trans_log_inode(tp
, ip
, logflags
);
5496 *firstblock
= cur
->bc_private
.b
.firstblock
;
5497 cur
->bc_private
.b
.allocated
= 0;
5499 xfs_btree_del_cursor(cur
,
5500 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
5505 /* Unmap a range of a file. */
5509 struct xfs_inode
*ip
,
5514 xfs_fsblock_t
*firstblock
,
5515 struct xfs_defer_ops
*dfops
,
5520 error
= __xfs_bunmapi(tp
, ip
, bno
, &len
, flags
, nexts
, firstblock
,
5527 * Determine whether an extent shift can be accomplished by a merge with the
5528 * extent that precedes the target hole of the shift.
5532 struct xfs_bmbt_irec
*left
, /* preceding extent */
5533 struct xfs_bmbt_irec
*got
, /* current extent to shift */
5534 xfs_fileoff_t shift
) /* shift fsb */
5536 xfs_fileoff_t startoff
;
5538 startoff
= got
->br_startoff
- shift
;
5541 * The extent, once shifted, must be adjacent in-file and on-disk with
5542 * the preceding extent.
5544 if ((left
->br_startoff
+ left
->br_blockcount
!= startoff
) ||
5545 (left
->br_startblock
+ left
->br_blockcount
!= got
->br_startblock
) ||
5546 (left
->br_state
!= got
->br_state
) ||
5547 (left
->br_blockcount
+ got
->br_blockcount
> MAXEXTLEN
))
5554 * A bmap extent shift adjusts the file offset of an extent to fill a preceding
5555 * hole in the file. If an extent shift would result in the extent being fully
5556 * adjacent to the extent that currently precedes the hole, we can merge with
5557 * the preceding extent rather than do the shift.
5559 * This function assumes the caller has verified a shift-by-merge is possible
5560 * with the provided extents via xfs_bmse_can_merge().
5564 struct xfs_inode
*ip
,
5566 xfs_fileoff_t shift
, /* shift fsb */
5567 struct xfs_iext_cursor
*icur
,
5568 struct xfs_bmbt_irec
*got
, /* extent to shift */
5569 struct xfs_bmbt_irec
*left
, /* preceding extent */
5570 struct xfs_btree_cur
*cur
,
5571 int *logflags
, /* output */
5572 struct xfs_defer_ops
*dfops
)
5574 struct xfs_bmbt_irec
new;
5575 xfs_filblks_t blockcount
;
5577 struct xfs_mount
*mp
= ip
->i_mount
;
5579 blockcount
= left
->br_blockcount
+ got
->br_blockcount
;
5581 ASSERT(xfs_isilocked(ip
, XFS_IOLOCK_EXCL
));
5582 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
5583 ASSERT(xfs_bmse_can_merge(left
, got
, shift
));
5586 new.br_blockcount
= blockcount
;
5589 * Update the on-disk extent count, the btree if necessary and log the
5592 XFS_IFORK_NEXT_SET(ip
, whichfork
,
5593 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
5594 *logflags
|= XFS_ILOG_CORE
;
5596 *logflags
|= XFS_ILOG_DEXT
;
5600 /* lookup and remove the extent to merge */
5601 error
= xfs_bmbt_lookup_eq(cur
, got
, &i
);
5604 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
5606 error
= xfs_btree_delete(cur
, &i
);
5609 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
5611 /* lookup and update size of the previous extent */
5612 error
= xfs_bmbt_lookup_eq(cur
, left
, &i
);
5615 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
5617 error
= xfs_bmbt_update(cur
, &new);
5622 xfs_iext_remove(ip
, icur
, 0);
5623 xfs_iext_prev(XFS_IFORK_PTR(ip
, whichfork
), icur
);
5624 xfs_iext_update_extent(ip
, xfs_bmap_fork_to_state(whichfork
), icur
,
5627 /* update reverse mapping. rmap functions merge the rmaps for us */
5628 error
= xfs_rmap_unmap_extent(mp
, dfops
, ip
, whichfork
, got
);
5631 memcpy(&new, got
, sizeof(new));
5632 new.br_startoff
= left
->br_startoff
+ left
->br_blockcount
;
5633 return xfs_rmap_map_extent(mp
, dfops
, ip
, whichfork
, &new);
5637 xfs_bmap_shift_update_extent(
5638 struct xfs_inode
*ip
,
5640 struct xfs_iext_cursor
*icur
,
5641 struct xfs_bmbt_irec
*got
,
5642 struct xfs_btree_cur
*cur
,
5644 struct xfs_defer_ops
*dfops
,
5645 xfs_fileoff_t startoff
)
5647 struct xfs_mount
*mp
= ip
->i_mount
;
5648 struct xfs_bmbt_irec prev
= *got
;
5651 *logflags
|= XFS_ILOG_CORE
;
5653 got
->br_startoff
= startoff
;
5656 error
= xfs_bmbt_lookup_eq(cur
, &prev
, &i
);
5659 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
5661 error
= xfs_bmbt_update(cur
, got
);
5665 *logflags
|= XFS_ILOG_DEXT
;
5668 xfs_iext_update_extent(ip
, xfs_bmap_fork_to_state(whichfork
), icur
,
5671 /* update reverse mapping */
5672 error
= xfs_rmap_unmap_extent(mp
, dfops
, ip
, whichfork
, &prev
);
5675 return xfs_rmap_map_extent(mp
, dfops
, ip
, whichfork
, got
);
5679 xfs_bmap_collapse_extents(
5680 struct xfs_trans
*tp
,
5681 struct xfs_inode
*ip
,
5682 xfs_fileoff_t
*next_fsb
,
5683 xfs_fileoff_t offset_shift_fsb
,
5685 xfs_fsblock_t
*firstblock
,
5686 struct xfs_defer_ops
*dfops
)
5688 int whichfork
= XFS_DATA_FORK
;
5689 struct xfs_mount
*mp
= ip
->i_mount
;
5690 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5691 struct xfs_btree_cur
*cur
= NULL
;
5692 struct xfs_bmbt_irec got
, prev
;
5693 struct xfs_iext_cursor icur
;
5694 xfs_fileoff_t new_startoff
;
5698 if (unlikely(XFS_TEST_ERROR(
5699 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
5700 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
5701 mp
, XFS_ERRTAG_BMAPIFORMAT
))) {
5702 XFS_ERROR_REPORT(__func__
, XFS_ERRLEVEL_LOW
, mp
);
5703 return -EFSCORRUPTED
;
5706 if (XFS_FORCED_SHUTDOWN(mp
))
5709 ASSERT(xfs_isilocked(ip
, XFS_IOLOCK_EXCL
| XFS_ILOCK_EXCL
));
5711 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
5712 error
= xfs_iread_extents(tp
, ip
, whichfork
);
5717 if (ifp
->if_flags
& XFS_IFBROOT
) {
5718 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
5719 cur
->bc_private
.b
.firstblock
= *firstblock
;
5720 cur
->bc_private
.b
.dfops
= dfops
;
5721 cur
->bc_private
.b
.flags
= 0;
5724 if (!xfs_iext_lookup_extent(ip
, ifp
, *next_fsb
, &icur
, &got
)) {
5728 XFS_WANT_CORRUPTED_GOTO(mp
, !isnullstartblock(got
.br_startblock
),
5731 new_startoff
= got
.br_startoff
- offset_shift_fsb
;
5732 if (xfs_iext_peek_prev_extent(ifp
, &icur
, &prev
)) {
5733 if (new_startoff
< prev
.br_startoff
+ prev
.br_blockcount
) {
5738 if (xfs_bmse_can_merge(&prev
, &got
, offset_shift_fsb
)) {
5739 error
= xfs_bmse_merge(ip
, whichfork
, offset_shift_fsb
,
5740 &icur
, &got
, &prev
, cur
, &logflags
,
5747 if (got
.br_startoff
< offset_shift_fsb
) {
5753 error
= xfs_bmap_shift_update_extent(ip
, whichfork
, &icur
, &got
, cur
,
5754 &logflags
, dfops
, new_startoff
);
5759 if (!xfs_iext_next_extent(ifp
, &icur
, &got
)) {
5764 *next_fsb
= got
.br_startoff
;
5767 xfs_btree_del_cursor(cur
,
5768 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
5770 xfs_trans_log_inode(tp
, ip
, logflags
);
5774 /* Make sure we won't be right-shifting an extent past the maximum bound. */
5776 xfs_bmap_can_insert_extents(
5777 struct xfs_inode
*ip
,
5779 xfs_fileoff_t shift
)
5781 struct xfs_bmbt_irec got
;
5785 ASSERT(xfs_isilocked(ip
, XFS_IOLOCK_EXCL
));
5787 if (XFS_FORCED_SHUTDOWN(ip
->i_mount
))
5790 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
5791 error
= xfs_bmap_last_extent(NULL
, ip
, XFS_DATA_FORK
, &got
, &is_empty
);
5792 if (!error
&& !is_empty
&& got
.br_startoff
>= off
&&
5793 ((got
.br_startoff
+ shift
) & BMBT_STARTOFF_MASK
) < got
.br_startoff
)
5795 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
5801 xfs_bmap_insert_extents(
5802 struct xfs_trans
*tp
,
5803 struct xfs_inode
*ip
,
5804 xfs_fileoff_t
*next_fsb
,
5805 xfs_fileoff_t offset_shift_fsb
,
5807 xfs_fileoff_t stop_fsb
,
5808 xfs_fsblock_t
*firstblock
,
5809 struct xfs_defer_ops
*dfops
)
5811 int whichfork
= XFS_DATA_FORK
;
5812 struct xfs_mount
*mp
= ip
->i_mount
;
5813 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5814 struct xfs_btree_cur
*cur
= NULL
;
5815 struct xfs_bmbt_irec got
, next
;
5816 struct xfs_iext_cursor icur
;
5817 xfs_fileoff_t new_startoff
;
5821 if (unlikely(XFS_TEST_ERROR(
5822 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
5823 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
5824 mp
, XFS_ERRTAG_BMAPIFORMAT
))) {
5825 XFS_ERROR_REPORT(__func__
, XFS_ERRLEVEL_LOW
, mp
);
5826 return -EFSCORRUPTED
;
5829 if (XFS_FORCED_SHUTDOWN(mp
))
5832 ASSERT(xfs_isilocked(ip
, XFS_IOLOCK_EXCL
| XFS_ILOCK_EXCL
));
5834 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
5835 error
= xfs_iread_extents(tp
, ip
, whichfork
);
5840 if (ifp
->if_flags
& XFS_IFBROOT
) {
5841 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
5842 cur
->bc_private
.b
.firstblock
= *firstblock
;
5843 cur
->bc_private
.b
.dfops
= dfops
;
5844 cur
->bc_private
.b
.flags
= 0;
5847 if (*next_fsb
== NULLFSBLOCK
) {
5848 xfs_iext_last(ifp
, &icur
);
5849 if (!xfs_iext_get_extent(ifp
, &icur
, &got
) ||
5850 stop_fsb
> got
.br_startoff
) {
5855 if (!xfs_iext_lookup_extent(ip
, ifp
, *next_fsb
, &icur
, &got
)) {
5860 XFS_WANT_CORRUPTED_GOTO(mp
, !isnullstartblock(got
.br_startblock
),
5863 if (stop_fsb
>= got
.br_startoff
+ got
.br_blockcount
) {
5868 new_startoff
= got
.br_startoff
+ offset_shift_fsb
;
5869 if (xfs_iext_peek_next_extent(ifp
, &icur
, &next
)) {
5870 if (new_startoff
+ got
.br_blockcount
> next
.br_startoff
) {
5876 * Unlike a left shift (which involves a hole punch), a right
5877 * shift does not modify extent neighbors in any way. We should
5878 * never find mergeable extents in this scenario. Check anyways
5879 * and warn if we encounter two extents that could be one.
5881 if (xfs_bmse_can_merge(&got
, &next
, offset_shift_fsb
))
5885 error
= xfs_bmap_shift_update_extent(ip
, whichfork
, &icur
, &got
, cur
,
5886 &logflags
, dfops
, new_startoff
);
5890 if (!xfs_iext_prev_extent(ifp
, &icur
, &got
) ||
5891 stop_fsb
>= got
.br_startoff
+ got
.br_blockcount
) {
5896 *next_fsb
= got
.br_startoff
;
5899 xfs_btree_del_cursor(cur
,
5900 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
5902 xfs_trans_log_inode(tp
, ip
, logflags
);
5907 * Splits an extent into two extents at split_fsb block such that it is the
5908 * first block of the current_ext. @ext is a target extent to be split.
5909 * @split_fsb is a block where the extents is split. If split_fsb lies in a
5910 * hole or the first block of extents, just return 0.
5913 xfs_bmap_split_extent_at(
5914 struct xfs_trans
*tp
,
5915 struct xfs_inode
*ip
,
5916 xfs_fileoff_t split_fsb
,
5917 xfs_fsblock_t
*firstfsb
,
5918 struct xfs_defer_ops
*dfops
)
5920 int whichfork
= XFS_DATA_FORK
;
5921 struct xfs_btree_cur
*cur
= NULL
;
5922 struct xfs_bmbt_irec got
;
5923 struct xfs_bmbt_irec
new; /* split extent */
5924 struct xfs_mount
*mp
= ip
->i_mount
;
5925 struct xfs_ifork
*ifp
;
5926 xfs_fsblock_t gotblkcnt
; /* new block count for got */
5927 struct xfs_iext_cursor icur
;
5932 if (unlikely(XFS_TEST_ERROR(
5933 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
5934 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
5935 mp
, XFS_ERRTAG_BMAPIFORMAT
))) {
5936 XFS_ERROR_REPORT("xfs_bmap_split_extent_at",
5937 XFS_ERRLEVEL_LOW
, mp
);
5938 return -EFSCORRUPTED
;
5941 if (XFS_FORCED_SHUTDOWN(mp
))
5944 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5945 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
5946 /* Read in all the extents */
5947 error
= xfs_iread_extents(tp
, ip
, whichfork
);
5953 * If there are not extents, or split_fsb lies in a hole we are done.
5955 if (!xfs_iext_lookup_extent(ip
, ifp
, split_fsb
, &icur
, &got
) ||
5956 got
.br_startoff
>= split_fsb
)
5959 gotblkcnt
= split_fsb
- got
.br_startoff
;
5960 new.br_startoff
= split_fsb
;
5961 new.br_startblock
= got
.br_startblock
+ gotblkcnt
;
5962 new.br_blockcount
= got
.br_blockcount
- gotblkcnt
;
5963 new.br_state
= got
.br_state
;
5965 if (ifp
->if_flags
& XFS_IFBROOT
) {
5966 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
5967 cur
->bc_private
.b
.firstblock
= *firstfsb
;
5968 cur
->bc_private
.b
.dfops
= dfops
;
5969 cur
->bc_private
.b
.flags
= 0;
5970 error
= xfs_bmbt_lookup_eq(cur
, &got
, &i
);
5973 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, del_cursor
);
5976 got
.br_blockcount
= gotblkcnt
;
5977 xfs_iext_update_extent(ip
, xfs_bmap_fork_to_state(whichfork
), &icur
,
5980 logflags
= XFS_ILOG_CORE
;
5982 error
= xfs_bmbt_update(cur
, &got
);
5986 logflags
|= XFS_ILOG_DEXT
;
5988 /* Add new extent */
5989 xfs_iext_next(ifp
, &icur
);
5990 xfs_iext_insert(ip
, &icur
, &new, 0);
5991 XFS_IFORK_NEXT_SET(ip
, whichfork
,
5992 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
5995 error
= xfs_bmbt_lookup_eq(cur
, &new, &i
);
5998 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, del_cursor
);
5999 error
= xfs_btree_insert(cur
, &i
);
6002 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, del_cursor
);
6006 * Convert to a btree if necessary.
6008 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
6009 int tmp_logflags
; /* partial log flag return val */
6011 ASSERT(cur
== NULL
);
6012 error
= xfs_bmap_extents_to_btree(tp
, ip
, firstfsb
, dfops
,
6013 &cur
, 0, &tmp_logflags
, whichfork
);
6014 logflags
|= tmp_logflags
;
6019 cur
->bc_private
.b
.allocated
= 0;
6020 xfs_btree_del_cursor(cur
,
6021 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
6025 xfs_trans_log_inode(tp
, ip
, logflags
);
6030 xfs_bmap_split_extent(
6031 struct xfs_inode
*ip
,
6032 xfs_fileoff_t split_fsb
)
6034 struct xfs_mount
*mp
= ip
->i_mount
;
6035 struct xfs_trans
*tp
;
6036 struct xfs_defer_ops dfops
;
6037 xfs_fsblock_t firstfsb
;
6040 error
= xfs_trans_alloc(mp
, &M_RES(mp
)->tr_write
,
6041 XFS_DIOSTRAT_SPACE_RES(mp
, 0), 0, 0, &tp
);
6045 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
6046 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
);
6048 xfs_defer_init(&dfops
, &firstfsb
);
6050 error
= xfs_bmap_split_extent_at(tp
, ip
, split_fsb
,
6055 error
= xfs_defer_finish(&tp
, &dfops
);
6059 return xfs_trans_commit(tp
);
6062 xfs_defer_cancel(&dfops
);
6063 xfs_trans_cancel(tp
);
6067 /* Deferred mapping is only for real extents in the data fork. */
6069 xfs_bmap_is_update_needed(
6070 struct xfs_bmbt_irec
*bmap
)
6072 return bmap
->br_startblock
!= HOLESTARTBLOCK
&&
6073 bmap
->br_startblock
!= DELAYSTARTBLOCK
;
6076 /* Record a bmap intent. */
6079 struct xfs_mount
*mp
,
6080 struct xfs_defer_ops
*dfops
,
6081 enum xfs_bmap_intent_type type
,
6082 struct xfs_inode
*ip
,
6084 struct xfs_bmbt_irec
*bmap
)
6087 struct xfs_bmap_intent
*bi
;
6089 trace_xfs_bmap_defer(mp
,
6090 XFS_FSB_TO_AGNO(mp
, bmap
->br_startblock
),
6092 XFS_FSB_TO_AGBNO(mp
, bmap
->br_startblock
),
6093 ip
->i_ino
, whichfork
,
6095 bmap
->br_blockcount
,
6098 bi
= kmem_alloc(sizeof(struct xfs_bmap_intent
), KM_SLEEP
| KM_NOFS
);
6099 INIT_LIST_HEAD(&bi
->bi_list
);
6102 bi
->bi_whichfork
= whichfork
;
6103 bi
->bi_bmap
= *bmap
;
6105 error
= xfs_defer_ijoin(dfops
, bi
->bi_owner
);
6111 xfs_defer_add(dfops
, XFS_DEFER_OPS_TYPE_BMAP
, &bi
->bi_list
);
6115 /* Map an extent into a file. */
6117 xfs_bmap_map_extent(
6118 struct xfs_mount
*mp
,
6119 struct xfs_defer_ops
*dfops
,
6120 struct xfs_inode
*ip
,
6121 struct xfs_bmbt_irec
*PREV
)
6123 if (!xfs_bmap_is_update_needed(PREV
))
6126 return __xfs_bmap_add(mp
, dfops
, XFS_BMAP_MAP
, ip
,
6127 XFS_DATA_FORK
, PREV
);
6130 /* Unmap an extent out of a file. */
6132 xfs_bmap_unmap_extent(
6133 struct xfs_mount
*mp
,
6134 struct xfs_defer_ops
*dfops
,
6135 struct xfs_inode
*ip
,
6136 struct xfs_bmbt_irec
*PREV
)
6138 if (!xfs_bmap_is_update_needed(PREV
))
6141 return __xfs_bmap_add(mp
, dfops
, XFS_BMAP_UNMAP
, ip
,
6142 XFS_DATA_FORK
, PREV
);
6146 * Process one of the deferred bmap operations. We pass back the
6147 * btree cursor to maintain our lock on the bmapbt between calls.
6150 xfs_bmap_finish_one(
6151 struct xfs_trans
*tp
,
6152 struct xfs_defer_ops
*dfops
,
6153 struct xfs_inode
*ip
,
6154 enum xfs_bmap_intent_type type
,
6156 xfs_fileoff_t startoff
,
6157 xfs_fsblock_t startblock
,
6158 xfs_filblks_t
*blockcount
,
6161 xfs_fsblock_t firstfsb
;
6165 * firstfsb is tied to the transaction lifetime and is used to
6166 * ensure correct AG locking order and schedule work item
6167 * continuations. XFS_BUI_MAX_FAST_EXTENTS (== 1) restricts us
6168 * to only making one bmap call per transaction, so it should
6169 * be safe to have it as a local variable here.
6171 firstfsb
= NULLFSBLOCK
;
6173 trace_xfs_bmap_deferred(tp
->t_mountp
,
6174 XFS_FSB_TO_AGNO(tp
->t_mountp
, startblock
), type
,
6175 XFS_FSB_TO_AGBNO(tp
->t_mountp
, startblock
),
6176 ip
->i_ino
, whichfork
, startoff
, *blockcount
, state
);
6178 if (WARN_ON_ONCE(whichfork
!= XFS_DATA_FORK
))
6179 return -EFSCORRUPTED
;
6181 if (XFS_TEST_ERROR(false, tp
->t_mountp
,
6182 XFS_ERRTAG_BMAP_FINISH_ONE
))
6187 error
= xfs_bmapi_remap(tp
, ip
, startoff
, *blockcount
,
6188 startblock
, dfops
, 0);
6191 case XFS_BMAP_UNMAP
:
6192 error
= __xfs_bunmapi(tp
, ip
, startoff
, blockcount
,
6193 XFS_BMAPI_REMAP
, 1, &firstfsb
, dfops
);
6197 error
= -EFSCORRUPTED
;
6203 /* Check that an inode's extent does not have invalid flags or bad ranges. */
6205 xfs_bmap_validate_extent(
6206 struct xfs_inode
*ip
,
6208 struct xfs_bmbt_irec
*irec
)
6210 struct xfs_mount
*mp
= ip
->i_mount
;
6211 xfs_fsblock_t endfsb
;
6214 isrt
= XFS_IS_REALTIME_INODE(ip
);
6215 endfsb
= irec
->br_startblock
+ irec
->br_blockcount
- 1;
6217 if (!xfs_verify_rtbno(mp
, irec
->br_startblock
))
6218 return __this_address
;
6219 if (!xfs_verify_rtbno(mp
, endfsb
))
6220 return __this_address
;
6222 if (!xfs_verify_fsbno(mp
, irec
->br_startblock
))
6223 return __this_address
;
6224 if (!xfs_verify_fsbno(mp
, endfsb
))
6225 return __this_address
;
6226 if (XFS_FSB_TO_AGNO(mp
, irec
->br_startblock
) !=
6227 XFS_FSB_TO_AGNO(mp
, endfsb
))
6228 return __this_address
;
6230 if (irec
->br_state
!= XFS_EXT_NORM
) {
6231 if (whichfork
!= XFS_DATA_FORK
)
6232 return __this_address
;
6233 if (!xfs_sb_version_hasextflgbit(&mp
->m_sb
))
6234 return __this_address
;