2 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 #include "libxfs_priv.h"
20 #include "xfs_shared.h"
21 #include "xfs_format.h"
22 #include "xfs_log_format.h"
23 #include "xfs_trans_resv.h"
26 #include "xfs_mount.h"
27 #include "xfs_defer.h"
28 #include "xfs_da_format.h"
29 #include "xfs_da_btree.h"
31 #include "xfs_inode.h"
32 #include "xfs_btree.h"
33 #include "xfs_trans.h"
34 #include "xfs_alloc.h"
36 #include "xfs_bmap_btree.h"
37 #include "xfs_errortag.h"
38 #include "xfs_trans_space.h"
39 #include "xfs_trace.h"
40 #include "xfs_attr_leaf.h"
41 #include "xfs_quota_defs.h"
43 #include "xfs_ag_resv.h"
44 #include "xfs_refcount.h"
47 kmem_zone_t
*xfs_bmap_free_item_zone
;
50 * Miscellaneous helper functions
54 * Compute and fill in the value of the maximum depth of a bmap btree
55 * in this filesystem. Done once, during mount.
58 xfs_bmap_compute_maxlevels(
59 xfs_mount_t
*mp
, /* file system mount structure */
60 int whichfork
) /* data or attr fork */
62 int level
; /* btree level */
63 uint maxblocks
; /* max blocks at this level */
64 uint maxleafents
; /* max leaf entries possible */
65 int maxrootrecs
; /* max records in root block */
66 int minleafrecs
; /* min records in leaf block */
67 int minnoderecs
; /* min records in node block */
68 int sz
; /* root block size */
71 * The maximum number of extents in a file, hence the maximum
72 * number of leaf entries, is controlled by the type of di_nextents
73 * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
74 * (a signed 16-bit number, xfs_aextnum_t).
76 * Note that we can no longer assume that if we are in ATTR1 that
77 * the fork offset of all the inodes will be
78 * (xfs_default_attroffset(ip) >> 3) because we could have mounted
79 * with ATTR2 and then mounted back with ATTR1, keeping the
80 * di_forkoff's fixed but probably at various positions. Therefore,
81 * for both ATTR1 and ATTR2 we have to assume the worst case scenario
82 * of a minimum size available.
84 if (whichfork
== XFS_DATA_FORK
) {
85 maxleafents
= MAXEXTNUM
;
86 sz
= XFS_BMDR_SPACE_CALC(MINDBTPTRS
);
88 maxleafents
= MAXAEXTNUM
;
89 sz
= XFS_BMDR_SPACE_CALC(MINABTPTRS
);
91 maxrootrecs
= xfs_bmdr_maxrecs(sz
, 0);
92 minleafrecs
= mp
->m_bmap_dmnr
[0];
93 minnoderecs
= mp
->m_bmap_dmnr
[1];
94 maxblocks
= (maxleafents
+ minleafrecs
- 1) / minleafrecs
;
95 for (level
= 1; maxblocks
> 1; level
++) {
96 if (maxblocks
<= maxrootrecs
)
99 maxblocks
= (maxblocks
+ minnoderecs
- 1) / minnoderecs
;
101 mp
->m_bm_maxlevels
[whichfork
] = level
;
104 STATIC
int /* error */
106 struct xfs_btree_cur
*cur
,
107 struct xfs_bmbt_irec
*irec
,
108 int *stat
) /* success/failure */
110 cur
->bc_rec
.b
= *irec
;
111 return xfs_btree_lookup(cur
, XFS_LOOKUP_EQ
, stat
);
114 STATIC
int /* error */
115 xfs_bmbt_lookup_first(
116 struct xfs_btree_cur
*cur
,
117 int *stat
) /* success/failure */
119 cur
->bc_rec
.b
.br_startoff
= 0;
120 cur
->bc_rec
.b
.br_startblock
= 0;
121 cur
->bc_rec
.b
.br_blockcount
= 0;
122 return xfs_btree_lookup(cur
, XFS_LOOKUP_GE
, stat
);
126 * Check if the inode needs to be converted to btree format.
128 static inline bool xfs_bmap_needs_btree(struct xfs_inode
*ip
, int whichfork
)
130 return whichfork
!= XFS_COW_FORK
&&
131 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
&&
132 XFS_IFORK_NEXTENTS(ip
, whichfork
) >
133 XFS_IFORK_MAXEXT(ip
, whichfork
);
137 * Check if the inode should be converted to extent format.
139 static inline bool xfs_bmap_wants_extents(struct xfs_inode
*ip
, int whichfork
)
141 return whichfork
!= XFS_COW_FORK
&&
142 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
&&
143 XFS_IFORK_NEXTENTS(ip
, whichfork
) <=
144 XFS_IFORK_MAXEXT(ip
, whichfork
);
148 * Update the record referred to by cur to the value given by irec
149 * This either works (return 0) or gets an EFSCORRUPTED error.
153 struct xfs_btree_cur
*cur
,
154 struct xfs_bmbt_irec
*irec
)
156 union xfs_btree_rec rec
;
158 xfs_bmbt_disk_set_all(&rec
.bmbt
, irec
);
159 return xfs_btree_update(cur
, &rec
);
163 * Compute the worst-case number of indirect blocks that will be used
164 * for ip's delayed extent of length "len".
167 xfs_bmap_worst_indlen(
168 xfs_inode_t
*ip
, /* incore inode pointer */
169 xfs_filblks_t len
) /* delayed extent length */
171 int level
; /* btree level number */
172 int maxrecs
; /* maximum record count at this level */
173 xfs_mount_t
*mp
; /* mount structure */
174 xfs_filblks_t rval
; /* return value */
177 maxrecs
= mp
->m_bmap_dmxr
[0];
178 for (level
= 0, rval
= 0;
179 level
< XFS_BM_MAXLEVELS(mp
, XFS_DATA_FORK
);
182 do_div(len
, maxrecs
);
185 return rval
+ XFS_BM_MAXLEVELS(mp
, XFS_DATA_FORK
) -
188 maxrecs
= mp
->m_bmap_dmxr
[1];
194 * Calculate the default attribute fork offset for newly created inodes.
197 xfs_default_attroffset(
198 struct xfs_inode
*ip
)
200 struct xfs_mount
*mp
= ip
->i_mount
;
203 if (mp
->m_sb
.sb_inodesize
== 256) {
204 offset
= XFS_LITINO(mp
, ip
->i_d
.di_version
) -
205 XFS_BMDR_SPACE_CALC(MINABTPTRS
);
207 offset
= XFS_BMDR_SPACE_CALC(6 * MINABTPTRS
);
210 ASSERT(offset
< XFS_LITINO(mp
, ip
->i_d
.di_version
));
215 * Helper routine to reset inode di_forkoff field when switching
216 * attribute fork from local to extent format - we reset it where
217 * possible to make space available for inline data fork extents.
220 xfs_bmap_forkoff_reset(
224 if (whichfork
== XFS_ATTR_FORK
&&
225 ip
->i_d
.di_format
!= XFS_DINODE_FMT_DEV
&&
226 ip
->i_d
.di_format
!= XFS_DINODE_FMT_BTREE
) {
227 uint dfl_forkoff
= xfs_default_attroffset(ip
) >> 3;
229 if (dfl_forkoff
> ip
->i_d
.di_forkoff
)
230 ip
->i_d
.di_forkoff
= dfl_forkoff
;
235 STATIC
struct xfs_buf
*
237 struct xfs_btree_cur
*cur
,
240 struct xfs_log_item
*lip
;
246 for (i
= 0; i
< XFS_BTREE_MAXLEVELS
; i
++) {
247 if (!cur
->bc_bufs
[i
])
249 if (XFS_BUF_ADDR(cur
->bc_bufs
[i
]) == bno
)
250 return cur
->bc_bufs
[i
];
253 /* Chase down all the log items to see if the bp is there */
254 list_for_each_entry(lip
, &cur
->bc_tp
->t_items
, li_trans
) {
255 struct xfs_buf_log_item
*bip
= (struct xfs_buf_log_item
*)lip
;
257 if (bip
->bli_item
.li_type
== XFS_LI_BUF
&&
258 XFS_BUF_ADDR(bip
->bli_buf
) == bno
)
267 struct xfs_btree_block
*block
,
273 __be64
*pp
, *thispa
; /* pointer to block address */
274 xfs_bmbt_key_t
*prevp
, *keyp
;
276 ASSERT(be16_to_cpu(block
->bb_level
) > 0);
279 for( i
= 1; i
<= xfs_btree_get_numrecs(block
); i
++) {
280 dmxr
= mp
->m_bmap_dmxr
[0];
281 keyp
= XFS_BMBT_KEY_ADDR(mp
, block
, i
);
284 ASSERT(be64_to_cpu(prevp
->br_startoff
) <
285 be64_to_cpu(keyp
->br_startoff
));
290 * Compare the block numbers to see if there are dups.
293 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, i
, sz
);
295 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, i
, dmxr
);
297 for (j
= i
+1; j
<= be16_to_cpu(block
->bb_numrecs
); j
++) {
299 thispa
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, j
, sz
);
301 thispa
= XFS_BMBT_PTR_ADDR(mp
, block
, j
, dmxr
);
302 if (*thispa
== *pp
) {
303 xfs_warn(mp
, "%s: thispa(%d) == pp(%d) %Ld",
305 (unsigned long long)be64_to_cpu(*thispa
));
306 xfs_err(mp
, "%s: ptrs are equal in node\n",
308 xfs_force_shutdown(mp
, SHUTDOWN_CORRUPT_INCORE
);
315 * Check that the extents for the inode ip are in the right order in all
316 * btree leaves. THis becomes prohibitively expensive for large extent count
317 * files, so don't bother with inodes that have more than 10,000 extents in
318 * them. The btree record ordering checks will still be done, so for such large
319 * bmapbt constructs that is going to catch most corruptions.
322 xfs_bmap_check_leaf_extents(
323 xfs_btree_cur_t
*cur
, /* btree cursor or null */
324 xfs_inode_t
*ip
, /* incore inode pointer */
325 int whichfork
) /* data or attr fork */
327 struct xfs_btree_block
*block
; /* current btree block */
328 xfs_fsblock_t bno
; /* block # of "block" */
329 xfs_buf_t
*bp
; /* buffer for "block" */
330 int error
; /* error return value */
331 xfs_extnum_t i
=0, j
; /* index into the extents list */
332 xfs_ifork_t
*ifp
; /* fork structure */
333 int level
; /* btree level, for checking */
334 xfs_mount_t
*mp
; /* file system mount structure */
335 __be64
*pp
; /* pointer to block address */
336 xfs_bmbt_rec_t
*ep
; /* pointer to current extent */
337 xfs_bmbt_rec_t last
= {0, 0}; /* last extent in prev block */
338 xfs_bmbt_rec_t
*nextp
; /* pointer to next extent */
341 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
) {
345 /* skip large extent count inodes */
346 if (ip
->i_d
.di_nextents
> 10000)
351 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
352 block
= ifp
->if_broot
;
354 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
356 level
= be16_to_cpu(block
->bb_level
);
358 xfs_check_block(block
, mp
, 1, ifp
->if_broot_bytes
);
359 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, 1, ifp
->if_broot_bytes
);
360 bno
= be64_to_cpu(*pp
);
362 ASSERT(bno
!= NULLFSBLOCK
);
363 ASSERT(XFS_FSB_TO_AGNO(mp
, bno
) < mp
->m_sb
.sb_agcount
);
364 ASSERT(XFS_FSB_TO_AGBNO(mp
, bno
) < mp
->m_sb
.sb_agblocks
);
367 * Go down the tree until leaf level is reached, following the first
368 * pointer (leftmost) at each level.
370 while (level
-- > 0) {
371 /* See if buf is in cur first */
373 bp
= xfs_bmap_get_bp(cur
, XFS_FSB_TO_DADDR(mp
, bno
));
376 error
= xfs_btree_read_bufl(mp
, NULL
, bno
, 0, &bp
,
382 block
= XFS_BUF_TO_BLOCK(bp
);
387 * Check this block for basic sanity (increasing keys and
388 * no duplicate blocks).
391 xfs_check_block(block
, mp
, 0, 0);
392 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, mp
->m_bmap_dmxr
[1]);
393 bno
= be64_to_cpu(*pp
);
394 XFS_WANT_CORRUPTED_GOTO(mp
,
395 xfs_verify_fsbno(mp
, bno
), error0
);
398 xfs_trans_brelse(NULL
, bp
);
403 * Here with bp and block set to the leftmost leaf node in the tree.
408 * Loop over all leaf nodes checking that all extents are in the right order.
411 xfs_fsblock_t nextbno
;
412 xfs_extnum_t num_recs
;
415 num_recs
= xfs_btree_get_numrecs(block
);
418 * Read-ahead the next leaf block, if any.
421 nextbno
= be64_to_cpu(block
->bb_u
.l
.bb_rightsib
);
424 * Check all the extents to make sure they are OK.
425 * If we had a previous block, the last entry should
426 * conform with the first entry in this one.
429 ep
= XFS_BMBT_REC_ADDR(mp
, block
, 1);
431 ASSERT(xfs_bmbt_disk_get_startoff(&last
) +
432 xfs_bmbt_disk_get_blockcount(&last
) <=
433 xfs_bmbt_disk_get_startoff(ep
));
435 for (j
= 1; j
< num_recs
; j
++) {
436 nextp
= XFS_BMBT_REC_ADDR(mp
, block
, j
+ 1);
437 ASSERT(xfs_bmbt_disk_get_startoff(ep
) +
438 xfs_bmbt_disk_get_blockcount(ep
) <=
439 xfs_bmbt_disk_get_startoff(nextp
));
447 xfs_trans_brelse(NULL
, bp
);
451 * If we've reached the end, stop.
453 if (bno
== NULLFSBLOCK
)
457 bp
= xfs_bmap_get_bp(cur
, XFS_FSB_TO_DADDR(mp
, bno
));
460 error
= xfs_btree_read_bufl(mp
, NULL
, bno
, 0, &bp
,
466 block
= XFS_BUF_TO_BLOCK(bp
);
472 xfs_warn(mp
, "%s: at error0", __func__
);
474 xfs_trans_brelse(NULL
, bp
);
476 xfs_warn(mp
, "%s: BAD after btree leaves for %d extents",
478 xfs_err(mp
, "%s: CORRUPTED BTREE OR SOMETHING", __func__
);
479 xfs_force_shutdown(mp
, SHUTDOWN_CORRUPT_INCORE
);
484 * Validate that the bmbt_irecs being returned from bmapi are valid
485 * given the caller's original parameters. Specifically check the
486 * ranges of the returned irecs to ensure that they only extend beyond
487 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
490 xfs_bmap_validate_ret(
494 xfs_bmbt_irec_t
*mval
,
498 int i
; /* index to map values */
500 ASSERT(ret_nmap
<= nmap
);
502 for (i
= 0; i
< ret_nmap
; i
++) {
503 ASSERT(mval
[i
].br_blockcount
> 0);
504 if (!(flags
& XFS_BMAPI_ENTIRE
)) {
505 ASSERT(mval
[i
].br_startoff
>= bno
);
506 ASSERT(mval
[i
].br_blockcount
<= len
);
507 ASSERT(mval
[i
].br_startoff
+ mval
[i
].br_blockcount
<=
510 ASSERT(mval
[i
].br_startoff
< bno
+ len
);
511 ASSERT(mval
[i
].br_startoff
+ mval
[i
].br_blockcount
>
515 mval
[i
- 1].br_startoff
+ mval
[i
- 1].br_blockcount
==
516 mval
[i
].br_startoff
);
517 ASSERT(mval
[i
].br_startblock
!= DELAYSTARTBLOCK
&&
518 mval
[i
].br_startblock
!= HOLESTARTBLOCK
);
519 ASSERT(mval
[i
].br_state
== XFS_EXT_NORM
||
520 mval
[i
].br_state
== XFS_EXT_UNWRITTEN
);
525 #define xfs_bmap_check_leaf_extents(cur, ip, whichfork) do { } while (0)
526 #define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap) do { } while (0)
530 * bmap free list manipulation functions
534 * Add the extent to the list of extents to be free at transaction end.
535 * The list is maintained sorted (by block number).
539 struct xfs_mount
*mp
,
540 struct xfs_defer_ops
*dfops
,
543 struct xfs_owner_info
*oinfo
,
546 struct xfs_extent_free_item
*new; /* new element */
551 ASSERT(bno
!= NULLFSBLOCK
);
553 ASSERT(len
<= MAXEXTLEN
);
554 ASSERT(!isnullstartblock(bno
));
555 agno
= XFS_FSB_TO_AGNO(mp
, bno
);
556 agbno
= XFS_FSB_TO_AGBNO(mp
, bno
);
557 ASSERT(agno
< mp
->m_sb
.sb_agcount
);
558 ASSERT(agbno
< mp
->m_sb
.sb_agblocks
);
559 ASSERT(len
< mp
->m_sb
.sb_agblocks
);
560 ASSERT(agbno
+ len
<= mp
->m_sb
.sb_agblocks
);
562 ASSERT(xfs_bmap_free_item_zone
!= NULL
);
564 new = kmem_zone_alloc(xfs_bmap_free_item_zone
, KM_SLEEP
);
565 new->xefi_startblock
= bno
;
566 new->xefi_blockcount
= (xfs_extlen_t
)len
;
568 new->xefi_oinfo
= *oinfo
;
570 xfs_rmap_skip_owner_update(&new->xefi_oinfo
);
571 new->xefi_skip_discard
= skip_discard
;
572 trace_xfs_bmap_free_defer(mp
, XFS_FSB_TO_AGNO(mp
, bno
), 0,
573 XFS_FSB_TO_AGBNO(mp
, bno
), len
);
574 xfs_defer_add(dfops
, XFS_DEFER_OPS_TYPE_FREE
, &new->xefi_list
);
578 * Inode fork format manipulation functions
582 * Transform a btree format file with only one leaf node, where the
583 * extents list will fit in the inode, into an extents format file.
584 * Since the file extents are already in-core, all we have to do is
585 * give up the space for the btree root and pitch the leaf block.
587 STATIC
int /* error */
588 xfs_bmap_btree_to_extents(
589 xfs_trans_t
*tp
, /* transaction pointer */
590 xfs_inode_t
*ip
, /* incore inode pointer */
591 xfs_btree_cur_t
*cur
, /* btree cursor */
592 int *logflagsp
, /* inode logging flags */
593 int whichfork
) /* data or attr fork */
596 struct xfs_btree_block
*cblock
;/* child btree block */
597 xfs_fsblock_t cbno
; /* child block number */
598 xfs_buf_t
*cbp
; /* child block's buffer */
599 int error
; /* error return value */
600 xfs_ifork_t
*ifp
; /* inode fork data */
601 xfs_mount_t
*mp
; /* mount point structure */
602 __be64
*pp
; /* ptr to block address */
603 struct xfs_btree_block
*rblock
;/* root btree block */
604 struct xfs_owner_info oinfo
;
607 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
608 ASSERT(whichfork
!= XFS_COW_FORK
);
609 ASSERT(ifp
->if_flags
& XFS_IFEXTENTS
);
610 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
);
611 rblock
= ifp
->if_broot
;
612 ASSERT(be16_to_cpu(rblock
->bb_level
) == 1);
613 ASSERT(be16_to_cpu(rblock
->bb_numrecs
) == 1);
614 ASSERT(xfs_bmbt_maxrecs(mp
, ifp
->if_broot_bytes
, 0) == 1);
615 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, rblock
, 1, ifp
->if_broot_bytes
);
616 cbno
= be64_to_cpu(*pp
);
619 XFS_WANT_CORRUPTED_RETURN(cur
->bc_mp
,
620 xfs_btree_check_lptr(cur
, cbno
, 1));
622 error
= xfs_btree_read_bufl(mp
, tp
, cbno
, 0, &cbp
, XFS_BMAP_BTREE_REF
,
626 cblock
= XFS_BUF_TO_BLOCK(cbp
);
627 if ((error
= xfs_btree_check_block(cur
, cblock
, 0, cbp
)))
629 xfs_rmap_ino_bmbt_owner(&oinfo
, ip
->i_ino
, whichfork
);
630 xfs_bmap_add_free(mp
, cur
->bc_private
.b
.dfops
, cbno
, 1, &oinfo
);
631 ip
->i_d
.di_nblocks
--;
632 xfs_trans_mod_dquot_byino(tp
, ip
, XFS_TRANS_DQ_BCOUNT
, -1L);
633 xfs_trans_binval(tp
, cbp
);
634 if (cur
->bc_bufs
[0] == cbp
)
635 cur
->bc_bufs
[0] = NULL
;
636 xfs_iroot_realloc(ip
, -1, whichfork
);
637 ASSERT(ifp
->if_broot
== NULL
);
638 ASSERT((ifp
->if_flags
& XFS_IFBROOT
) == 0);
639 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_EXTENTS
);
640 *logflagsp
= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
645 * Convert an extents-format file into a btree-format file.
646 * The new file will have a root block (in the inode) and a single child block.
648 STATIC
int /* error */
649 xfs_bmap_extents_to_btree(
650 xfs_trans_t
*tp
, /* transaction pointer */
651 xfs_inode_t
*ip
, /* incore inode pointer */
652 xfs_fsblock_t
*firstblock
, /* first-block-allocated */
653 struct xfs_defer_ops
*dfops
, /* blocks freed in xaction */
654 xfs_btree_cur_t
**curp
, /* cursor returned to caller */
655 int wasdel
, /* converting a delayed alloc */
656 int *logflagsp
, /* inode logging flags */
657 int whichfork
) /* data or attr fork */
659 struct xfs_btree_block
*ablock
; /* allocated (child) bt block */
660 xfs_buf_t
*abp
; /* buffer for ablock */
661 xfs_alloc_arg_t args
; /* allocation arguments */
662 xfs_bmbt_rec_t
*arp
; /* child record pointer */
663 struct xfs_btree_block
*block
; /* btree root block */
664 xfs_btree_cur_t
*cur
; /* bmap btree cursor */
665 int error
; /* error return value */
666 xfs_ifork_t
*ifp
; /* inode fork pointer */
667 xfs_bmbt_key_t
*kp
; /* root block key pointer */
668 xfs_mount_t
*mp
; /* mount structure */
669 xfs_bmbt_ptr_t
*pp
; /* root block address pointer */
670 struct xfs_iext_cursor icur
;
671 struct xfs_bmbt_irec rec
;
672 xfs_extnum_t cnt
= 0;
675 ASSERT(whichfork
!= XFS_COW_FORK
);
676 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
677 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
);
680 * Make space in the inode incore.
682 xfs_iroot_realloc(ip
, 1, whichfork
);
683 ifp
->if_flags
|= XFS_IFBROOT
;
688 block
= ifp
->if_broot
;
689 xfs_btree_init_block_int(mp
, block
, XFS_BUF_DADDR_NULL
,
690 XFS_BTNUM_BMAP
, 1, 1, ip
->i_ino
,
691 XFS_BTREE_LONG_PTRS
);
693 * Need a cursor. Can't allocate until bb_level is filled in.
695 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
696 cur
->bc_private
.b
.firstblock
= *firstblock
;
697 cur
->bc_private
.b
.dfops
= dfops
;
698 cur
->bc_private
.b
.flags
= wasdel
? XFS_BTCUR_BPRV_WASDEL
: 0;
700 * Convert to a btree with two levels, one record in root.
702 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_BTREE
);
703 memset(&args
, 0, sizeof(args
));
706 xfs_rmap_ino_bmbt_owner(&args
.oinfo
, ip
->i_ino
, whichfork
);
707 args
.firstblock
= *firstblock
;
708 if (*firstblock
== NULLFSBLOCK
) {
709 args
.type
= XFS_ALLOCTYPE_START_BNO
;
710 args
.fsbno
= XFS_INO_TO_FSB(mp
, ip
->i_ino
);
711 } else if (dfops
->dop_low
) {
712 args
.type
= XFS_ALLOCTYPE_START_BNO
;
713 args
.fsbno
= *firstblock
;
715 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
716 args
.fsbno
= *firstblock
;
718 args
.minlen
= args
.maxlen
= args
.prod
= 1;
719 args
.wasdel
= wasdel
;
721 if ((error
= xfs_alloc_vextent(&args
))) {
722 xfs_iroot_realloc(ip
, -1, whichfork
);
723 ASSERT(ifp
->if_broot
== NULL
);
724 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_EXTENTS
);
725 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
729 if (WARN_ON_ONCE(args
.fsbno
== NULLFSBLOCK
)) {
730 xfs_iroot_realloc(ip
, -1, whichfork
);
731 ASSERT(ifp
->if_broot
== NULL
);
732 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_EXTENTS
);
733 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
737 * Allocation can't fail, the space was reserved.
739 ASSERT(*firstblock
== NULLFSBLOCK
||
740 args
.agno
>= XFS_FSB_TO_AGNO(mp
, *firstblock
));
741 *firstblock
= cur
->bc_private
.b
.firstblock
= args
.fsbno
;
742 cur
->bc_private
.b
.allocated
++;
743 ip
->i_d
.di_nblocks
++;
744 xfs_trans_mod_dquot_byino(tp
, ip
, XFS_TRANS_DQ_BCOUNT
, 1L);
745 abp
= xfs_btree_get_bufl(mp
, tp
, args
.fsbno
, 0);
747 * Fill in the child block.
749 abp
->b_ops
= &xfs_bmbt_buf_ops
;
750 ablock
= XFS_BUF_TO_BLOCK(abp
);
751 xfs_btree_init_block_int(mp
, ablock
, abp
->b_bn
,
752 XFS_BTNUM_BMAP
, 0, 0, ip
->i_ino
,
753 XFS_BTREE_LONG_PTRS
);
755 for_each_xfs_iext(ifp
, &icur
, &rec
) {
756 if (isnullstartblock(rec
.br_startblock
))
758 arp
= XFS_BMBT_REC_ADDR(mp
, ablock
, 1 + cnt
);
759 xfs_bmbt_disk_set_all(arp
, &rec
);
762 ASSERT(cnt
== XFS_IFORK_NEXTENTS(ip
, whichfork
));
763 xfs_btree_set_numrecs(ablock
, cnt
);
766 * Fill in the root key and pointer.
768 kp
= XFS_BMBT_KEY_ADDR(mp
, block
, 1);
769 arp
= XFS_BMBT_REC_ADDR(mp
, ablock
, 1);
770 kp
->br_startoff
= cpu_to_be64(xfs_bmbt_disk_get_startoff(arp
));
771 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, xfs_bmbt_get_maxrecs(cur
,
772 be16_to_cpu(block
->bb_level
)));
773 *pp
= cpu_to_be64(args
.fsbno
);
776 * Do all this logging at the end so that
777 * the root is at the right level.
779 xfs_btree_log_block(cur
, abp
, XFS_BB_ALL_BITS
);
780 xfs_btree_log_recs(cur
, abp
, 1, be16_to_cpu(ablock
->bb_numrecs
));
781 ASSERT(*curp
== NULL
);
783 *logflagsp
= XFS_ILOG_CORE
| xfs_ilog_fbroot(whichfork
);
788 * Convert a local file to an extents file.
789 * This code is out of bounds for data forks of regular files,
790 * since the file data needs to get logged so things will stay consistent.
791 * (The bmap-level manipulations are ok, though).
794 xfs_bmap_local_to_extents_empty(
795 struct xfs_inode
*ip
,
798 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
800 ASSERT(whichfork
!= XFS_COW_FORK
);
801 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
802 ASSERT(ifp
->if_bytes
== 0);
803 ASSERT(XFS_IFORK_NEXTENTS(ip
, whichfork
) == 0);
805 xfs_bmap_forkoff_reset(ip
, whichfork
);
806 ifp
->if_flags
&= ~XFS_IFINLINE
;
807 ifp
->if_flags
|= XFS_IFEXTENTS
;
808 ifp
->if_u1
.if_root
= NULL
;
810 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_EXTENTS
);
814 STATIC
int /* error */
815 xfs_bmap_local_to_extents(
816 xfs_trans_t
*tp
, /* transaction pointer */
817 xfs_inode_t
*ip
, /* incore inode pointer */
818 xfs_fsblock_t
*firstblock
, /* first block allocated in xaction */
819 xfs_extlen_t total
, /* total blocks needed by transaction */
820 int *logflagsp
, /* inode logging flags */
822 void (*init_fn
)(struct xfs_trans
*tp
,
824 struct xfs_inode
*ip
,
825 struct xfs_ifork
*ifp
))
828 int flags
; /* logging flags returned */
829 xfs_ifork_t
*ifp
; /* inode fork pointer */
830 xfs_alloc_arg_t args
; /* allocation arguments */
831 xfs_buf_t
*bp
; /* buffer for extent block */
832 struct xfs_bmbt_irec rec
;
833 struct xfs_iext_cursor icur
;
836 * We don't want to deal with the case of keeping inode data inline yet.
837 * So sending the data fork of a regular inode is invalid.
839 ASSERT(!(S_ISREG(VFS_I(ip
)->i_mode
) && whichfork
== XFS_DATA_FORK
));
840 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
841 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
843 if (!ifp
->if_bytes
) {
844 xfs_bmap_local_to_extents_empty(ip
, whichfork
);
845 flags
= XFS_ILOG_CORE
;
851 ASSERT((ifp
->if_flags
& (XFS_IFINLINE
|XFS_IFEXTENTS
)) == XFS_IFINLINE
);
852 memset(&args
, 0, sizeof(args
));
854 args
.mp
= ip
->i_mount
;
855 xfs_rmap_ino_owner(&args
.oinfo
, ip
->i_ino
, whichfork
, 0);
856 args
.firstblock
= *firstblock
;
858 * Allocate a block. We know we need only one, since the
859 * file currently fits in an inode.
861 if (*firstblock
== NULLFSBLOCK
) {
862 args
.fsbno
= XFS_INO_TO_FSB(args
.mp
, ip
->i_ino
);
863 args
.type
= XFS_ALLOCTYPE_START_BNO
;
865 args
.fsbno
= *firstblock
;
866 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
869 args
.minlen
= args
.maxlen
= args
.prod
= 1;
870 error
= xfs_alloc_vextent(&args
);
874 /* Can't fail, the space was reserved. */
875 ASSERT(args
.fsbno
!= NULLFSBLOCK
);
876 ASSERT(args
.len
== 1);
877 *firstblock
= args
.fsbno
;
878 bp
= xfs_btree_get_bufl(args
.mp
, tp
, args
.fsbno
, 0);
881 * Initialize the block, copy the data and log the remote buffer.
883 * The callout is responsible for logging because the remote format
884 * might differ from the local format and thus we don't know how much to
885 * log here. Note that init_fn must also set the buffer log item type
888 init_fn(tp
, bp
, ip
, ifp
);
890 /* account for the change in fork size */
891 xfs_idata_realloc(ip
, -ifp
->if_bytes
, whichfork
);
892 xfs_bmap_local_to_extents_empty(ip
, whichfork
);
893 flags
|= XFS_ILOG_CORE
;
895 ifp
->if_u1
.if_root
= NULL
;
899 rec
.br_startblock
= args
.fsbno
;
900 rec
.br_blockcount
= 1;
901 rec
.br_state
= XFS_EXT_NORM
;
902 xfs_iext_first(ifp
, &icur
);
903 xfs_iext_insert(ip
, &icur
, &rec
, 0);
905 XFS_IFORK_NEXT_SET(ip
, whichfork
, 1);
906 ip
->i_d
.di_nblocks
= 1;
907 xfs_trans_mod_dquot_byino(tp
, ip
,
908 XFS_TRANS_DQ_BCOUNT
, 1L);
909 flags
|= xfs_ilog_fext(whichfork
);
917 * Called from xfs_bmap_add_attrfork to handle btree format files.
919 STATIC
int /* error */
920 xfs_bmap_add_attrfork_btree(
921 xfs_trans_t
*tp
, /* transaction pointer */
922 xfs_inode_t
*ip
, /* incore inode pointer */
923 xfs_fsblock_t
*firstblock
, /* first block allocated */
924 struct xfs_defer_ops
*dfops
, /* blocks to free at commit */
925 int *flags
) /* inode logging flags */
927 xfs_btree_cur_t
*cur
; /* btree cursor */
928 int error
; /* error return value */
929 xfs_mount_t
*mp
; /* file system mount struct */
930 int stat
; /* newroot status */
933 if (ip
->i_df
.if_broot_bytes
<= XFS_IFORK_DSIZE(ip
))
934 *flags
|= XFS_ILOG_DBROOT
;
936 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, XFS_DATA_FORK
);
937 cur
->bc_private
.b
.dfops
= dfops
;
938 cur
->bc_private
.b
.firstblock
= *firstblock
;
939 error
= xfs_bmbt_lookup_first(cur
, &stat
);
942 /* must be at least one entry */
943 XFS_WANT_CORRUPTED_GOTO(mp
, stat
== 1, error0
);
944 if ((error
= xfs_btree_new_iroot(cur
, flags
, &stat
)))
947 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
950 *firstblock
= cur
->bc_private
.b
.firstblock
;
951 cur
->bc_private
.b
.allocated
= 0;
952 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
956 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
961 * Called from xfs_bmap_add_attrfork to handle extents format files.
963 STATIC
int /* error */
964 xfs_bmap_add_attrfork_extents(
965 xfs_trans_t
*tp
, /* transaction pointer */
966 xfs_inode_t
*ip
, /* incore inode pointer */
967 xfs_fsblock_t
*firstblock
, /* first block allocated */
968 struct xfs_defer_ops
*dfops
, /* blocks to free at commit */
969 int *flags
) /* inode logging flags */
971 xfs_btree_cur_t
*cur
; /* bmap btree cursor */
972 int error
; /* error return value */
974 if (ip
->i_d
.di_nextents
* sizeof(xfs_bmbt_rec_t
) <= XFS_IFORK_DSIZE(ip
))
977 error
= xfs_bmap_extents_to_btree(tp
, ip
, firstblock
, dfops
, &cur
, 0,
978 flags
, XFS_DATA_FORK
);
980 cur
->bc_private
.b
.allocated
= 0;
981 xfs_btree_del_cursor(cur
,
982 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
988 * Called from xfs_bmap_add_attrfork to handle local format files. Each
989 * different data fork content type needs a different callout to do the
990 * conversion. Some are basic and only require special block initialisation
991 * callouts for the data formating, others (directories) are so specialised they
992 * handle everything themselves.
994 * XXX (dgc): investigate whether directory conversion can use the generic
995 * formatting callout. It should be possible - it's just a very complex
998 STATIC
int /* error */
999 xfs_bmap_add_attrfork_local(
1000 xfs_trans_t
*tp
, /* transaction pointer */
1001 xfs_inode_t
*ip
, /* incore inode pointer */
1002 xfs_fsblock_t
*firstblock
, /* first block allocated */
1003 struct xfs_defer_ops
*dfops
, /* blocks to free at commit */
1004 int *flags
) /* inode logging flags */
1006 xfs_da_args_t dargs
; /* args for dir/attr code */
1008 if (ip
->i_df
.if_bytes
<= XFS_IFORK_DSIZE(ip
))
1011 if (S_ISDIR(VFS_I(ip
)->i_mode
)) {
1012 memset(&dargs
, 0, sizeof(dargs
));
1013 dargs
.geo
= ip
->i_mount
->m_dir_geo
;
1015 dargs
.firstblock
= firstblock
;
1016 dargs
.dfops
= dfops
;
1017 dargs
.total
= dargs
.geo
->fsbcount
;
1018 dargs
.whichfork
= XFS_DATA_FORK
;
1020 return xfs_dir2_sf_to_block(&dargs
);
1023 if (S_ISLNK(VFS_I(ip
)->i_mode
))
1024 return xfs_bmap_local_to_extents(tp
, ip
, firstblock
, 1,
1025 flags
, XFS_DATA_FORK
,
1026 xfs_symlink_local_to_remote
);
1028 /* should only be called for types that support local format data */
1030 return -EFSCORRUPTED
;
1034 * Convert inode from non-attributed to attributed.
1035 * Must not be in a transaction, ip must not be locked.
1037 int /* error code */
1038 xfs_bmap_add_attrfork(
1039 xfs_inode_t
*ip
, /* incore inode pointer */
1040 int size
, /* space new attribute needs */
1041 int rsvd
) /* xact may use reserved blks */
1043 xfs_fsblock_t firstblock
; /* 1st block/ag allocated */
1044 struct xfs_defer_ops dfops
; /* freed extent records */
1045 xfs_mount_t
*mp
; /* mount structure */
1046 xfs_trans_t
*tp
; /* transaction pointer */
1047 int blks
; /* space reservation */
1048 int version
= 1; /* superblock attr version */
1049 int logflags
; /* logging flags */
1050 int error
; /* error return value */
1052 ASSERT(XFS_IFORK_Q(ip
) == 0);
1055 ASSERT(!XFS_NOT_DQATTACHED(mp
, ip
));
1057 blks
= XFS_ADDAFORK_SPACE_RES(mp
);
1059 error
= xfs_trans_alloc(mp
, &M_RES(mp
)->tr_addafork
, blks
, 0,
1060 rsvd
? XFS_TRANS_RESERVE
: 0, &tp
);
1064 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
1065 error
= xfs_trans_reserve_quota_nblks(tp
, ip
, blks
, 0, rsvd
?
1066 XFS_QMOPT_RES_REGBLKS
| XFS_QMOPT_FORCE_RES
:
1067 XFS_QMOPT_RES_REGBLKS
);
1070 if (XFS_IFORK_Q(ip
))
1072 if (ip
->i_d
.di_anextents
!= 0) {
1073 error
= -EFSCORRUPTED
;
1076 if (ip
->i_d
.di_aformat
!= XFS_DINODE_FMT_EXTENTS
) {
1078 * For inodes coming from pre-6.2 filesystems.
1080 ASSERT(ip
->i_d
.di_aformat
== 0);
1081 ip
->i_d
.di_aformat
= XFS_DINODE_FMT_EXTENTS
;
1084 xfs_trans_ijoin(tp
, ip
, 0);
1085 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
1087 switch (ip
->i_d
.di_format
) {
1088 case XFS_DINODE_FMT_DEV
:
1089 ip
->i_d
.di_forkoff
= roundup(sizeof(xfs_dev_t
), 8) >> 3;
1091 case XFS_DINODE_FMT_LOCAL
:
1092 case XFS_DINODE_FMT_EXTENTS
:
1093 case XFS_DINODE_FMT_BTREE
:
1094 ip
->i_d
.di_forkoff
= xfs_attr_shortform_bytesfit(ip
, size
);
1095 if (!ip
->i_d
.di_forkoff
)
1096 ip
->i_d
.di_forkoff
= xfs_default_attroffset(ip
) >> 3;
1097 else if (mp
->m_flags
& XFS_MOUNT_ATTR2
)
1106 ASSERT(ip
->i_afp
== NULL
);
1107 ip
->i_afp
= kmem_zone_zalloc(xfs_ifork_zone
, KM_SLEEP
);
1108 ip
->i_afp
->if_flags
= XFS_IFEXTENTS
;
1110 xfs_defer_init(&dfops
, &firstblock
);
1111 switch (ip
->i_d
.di_format
) {
1112 case XFS_DINODE_FMT_LOCAL
:
1113 error
= xfs_bmap_add_attrfork_local(tp
, ip
, &firstblock
, &dfops
,
1116 case XFS_DINODE_FMT_EXTENTS
:
1117 error
= xfs_bmap_add_attrfork_extents(tp
, ip
, &firstblock
,
1120 case XFS_DINODE_FMT_BTREE
:
1121 error
= xfs_bmap_add_attrfork_btree(tp
, ip
, &firstblock
, &dfops
,
1129 xfs_trans_log_inode(tp
, ip
, logflags
);
1132 if (!xfs_sb_version_hasattr(&mp
->m_sb
) ||
1133 (!xfs_sb_version_hasattr2(&mp
->m_sb
) && version
== 2)) {
1134 bool log_sb
= false;
1136 spin_lock(&mp
->m_sb_lock
);
1137 if (!xfs_sb_version_hasattr(&mp
->m_sb
)) {
1138 xfs_sb_version_addattr(&mp
->m_sb
);
1141 if (!xfs_sb_version_hasattr2(&mp
->m_sb
) && version
== 2) {
1142 xfs_sb_version_addattr2(&mp
->m_sb
);
1145 spin_unlock(&mp
->m_sb_lock
);
1150 error
= xfs_defer_finish(&tp
, &dfops
);
1153 error
= xfs_trans_commit(tp
);
1154 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
1158 xfs_defer_cancel(&dfops
);
1160 xfs_trans_cancel(tp
);
1161 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
1166 * Internal and external extent tree search functions.
1170 * Read in extents from a btree-format inode.
1174 struct xfs_trans
*tp
,
1175 struct xfs_inode
*ip
,
1178 struct xfs_mount
*mp
= ip
->i_mount
;
1179 int state
= xfs_bmap_fork_to_state(whichfork
);
1180 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1181 xfs_extnum_t nextents
= XFS_IFORK_NEXTENTS(ip
, whichfork
);
1182 struct xfs_btree_block
*block
= ifp
->if_broot
;
1183 struct xfs_iext_cursor icur
;
1184 struct xfs_bmbt_irec
new;
1192 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
1194 if (unlikely(XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)) {
1195 XFS_ERROR_REPORT(__func__
, XFS_ERRLEVEL_LOW
, mp
);
1196 return -EFSCORRUPTED
;
1200 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
1202 level
= be16_to_cpu(block
->bb_level
);
1204 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, 1, ifp
->if_broot_bytes
);
1205 bno
= be64_to_cpu(*pp
);
1208 * Go down the tree until leaf level is reached, following the first
1209 * pointer (leftmost) at each level.
1211 while (level
-- > 0) {
1212 error
= xfs_btree_read_bufl(mp
, tp
, bno
, 0, &bp
,
1213 XFS_BMAP_BTREE_REF
, &xfs_bmbt_buf_ops
);
1216 block
= XFS_BUF_TO_BLOCK(bp
);
1219 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, mp
->m_bmap_dmxr
[1]);
1220 bno
= be64_to_cpu(*pp
);
1221 XFS_WANT_CORRUPTED_GOTO(mp
,
1222 xfs_verify_fsbno(mp
, bno
), out_brelse
);
1223 xfs_trans_brelse(tp
, bp
);
1227 * Here with bp and block set to the leftmost leaf node in the tree.
1230 xfs_iext_first(ifp
, &icur
);
1233 * Loop over all leaf nodes. Copy information to the extent records.
1236 xfs_bmbt_rec_t
*frp
;
1237 xfs_fsblock_t nextbno
;
1238 xfs_extnum_t num_recs
;
1240 num_recs
= xfs_btree_get_numrecs(block
);
1241 if (unlikely(i
+ num_recs
> nextents
)) {
1242 ASSERT(i
+ num_recs
<= nextents
);
1243 xfs_warn(ip
->i_mount
,
1244 "corrupt dinode %Lu, (btree extents).",
1245 (unsigned long long) ip
->i_ino
);
1246 xfs_inode_verifier_error(ip
, -EFSCORRUPTED
,
1247 __func__
, block
, sizeof(*block
),
1249 error
= -EFSCORRUPTED
;
1253 * Read-ahead the next leaf block, if any.
1255 nextbno
= be64_to_cpu(block
->bb_u
.l
.bb_rightsib
);
1256 if (nextbno
!= NULLFSBLOCK
)
1257 xfs_btree_reada_bufl(mp
, nextbno
, 1,
1260 * Copy records into the extent records.
1262 frp
= XFS_BMBT_REC_ADDR(mp
, block
, 1);
1263 for (j
= 0; j
< num_recs
; j
++, frp
++, i
++) {
1266 xfs_bmbt_disk_get_all(frp
, &new);
1267 fa
= xfs_bmap_validate_extent(ip
, whichfork
, &new);
1269 error
= -EFSCORRUPTED
;
1270 xfs_inode_verifier_error(ip
, error
,
1271 "xfs_iread_extents(2)",
1272 frp
, sizeof(*frp
), fa
);
1275 xfs_iext_insert(ip
, &icur
, &new, state
);
1276 trace_xfs_read_extent(ip
, &icur
, state
, _THIS_IP_
);
1277 xfs_iext_next(ifp
, &icur
);
1279 xfs_trans_brelse(tp
, bp
);
1282 * If we've reached the end, stop.
1284 if (bno
== NULLFSBLOCK
)
1286 error
= xfs_btree_read_bufl(mp
, tp
, bno
, 0, &bp
,
1287 XFS_BMAP_BTREE_REF
, &xfs_bmbt_buf_ops
);
1290 block
= XFS_BUF_TO_BLOCK(bp
);
1293 if (i
!= XFS_IFORK_NEXTENTS(ip
, whichfork
)) {
1294 error
= -EFSCORRUPTED
;
1297 ASSERT(i
== xfs_iext_count(ifp
));
1299 ifp
->if_flags
|= XFS_IFEXTENTS
;
1303 xfs_trans_brelse(tp
, bp
);
1305 xfs_iext_destroy(ifp
);
1310 * Returns the relative block number of the first unused block(s) in the given
1311 * fork with at least "len" logically contiguous blocks free. This is the
1312 * lowest-address hole if the fork has holes, else the first block past the end
1313 * of fork. Return 0 if the fork is currently local (in-inode).
1316 xfs_bmap_first_unused(
1317 struct xfs_trans
*tp
, /* transaction pointer */
1318 struct xfs_inode
*ip
, /* incore inode */
1319 xfs_extlen_t len
, /* size of hole to find */
1320 xfs_fileoff_t
*first_unused
, /* unused block */
1321 int whichfork
) /* data or attr fork */
1323 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1324 struct xfs_bmbt_irec got
;
1325 struct xfs_iext_cursor icur
;
1326 xfs_fileoff_t lastaddr
= 0;
1327 xfs_fileoff_t lowest
, max
;
1330 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
||
1331 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
||
1332 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
1334 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
) {
1339 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
1340 error
= xfs_iread_extents(tp
, ip
, whichfork
);
1345 lowest
= max
= *first_unused
;
1346 for_each_xfs_iext(ifp
, &icur
, &got
) {
1348 * See if the hole before this extent will work.
1350 if (got
.br_startoff
>= lowest
+ len
&&
1351 got
.br_startoff
- max
>= len
)
1353 lastaddr
= got
.br_startoff
+ got
.br_blockcount
;
1354 max
= XFS_FILEOFF_MAX(lastaddr
, lowest
);
1357 *first_unused
= max
;
1362 * Returns the file-relative block number of the last block - 1 before
1363 * last_block (input value) in the file.
1364 * This is not based on i_size, it is based on the extent records.
1365 * Returns 0 for local files, as they do not have extent records.
1368 xfs_bmap_last_before(
1369 struct xfs_trans
*tp
, /* transaction pointer */
1370 struct xfs_inode
*ip
, /* incore inode */
1371 xfs_fileoff_t
*last_block
, /* last block */
1372 int whichfork
) /* data or attr fork */
1374 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1375 struct xfs_bmbt_irec got
;
1376 struct xfs_iext_cursor icur
;
1379 switch (XFS_IFORK_FORMAT(ip
, whichfork
)) {
1380 case XFS_DINODE_FMT_LOCAL
:
1383 case XFS_DINODE_FMT_BTREE
:
1384 case XFS_DINODE_FMT_EXTENTS
:
1390 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
1391 error
= xfs_iread_extents(tp
, ip
, whichfork
);
1396 if (!xfs_iext_lookup_extent_before(ip
, ifp
, last_block
, &icur
, &got
))
1402 xfs_bmap_last_extent(
1403 struct xfs_trans
*tp
,
1404 struct xfs_inode
*ip
,
1406 struct xfs_bmbt_irec
*rec
,
1409 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1410 struct xfs_iext_cursor icur
;
1413 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
1414 error
= xfs_iread_extents(tp
, ip
, whichfork
);
1419 xfs_iext_last(ifp
, &icur
);
1420 if (!xfs_iext_get_extent(ifp
, &icur
, rec
))
1428 * Check the last inode extent to determine whether this allocation will result
1429 * in blocks being allocated at the end of the file. When we allocate new data
1430 * blocks at the end of the file which do not start at the previous data block,
1431 * we will try to align the new blocks at stripe unit boundaries.
1433 * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be
1434 * at, or past the EOF.
1438 struct xfs_bmalloca
*bma
,
1441 struct xfs_bmbt_irec rec
;
1446 error
= xfs_bmap_last_extent(NULL
, bma
->ip
, whichfork
, &rec
,
1457 * Check if we are allocation or past the last extent, or at least into
1458 * the last delayed allocated extent.
1460 bma
->aeof
= bma
->offset
>= rec
.br_startoff
+ rec
.br_blockcount
||
1461 (bma
->offset
>= rec
.br_startoff
&&
1462 isnullstartblock(rec
.br_startblock
));
1467 * Returns the file-relative block number of the first block past eof in
1468 * the file. This is not based on i_size, it is based on the extent records.
1469 * Returns 0 for local files, as they do not have extent records.
1472 xfs_bmap_last_offset(
1473 struct xfs_inode
*ip
,
1474 xfs_fileoff_t
*last_block
,
1477 struct xfs_bmbt_irec rec
;
1483 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
)
1486 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
&&
1487 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
1490 error
= xfs_bmap_last_extent(NULL
, ip
, whichfork
, &rec
, &is_empty
);
1491 if (error
|| is_empty
)
1494 *last_block
= rec
.br_startoff
+ rec
.br_blockcount
;
1499 * Returns whether the selected fork of the inode has exactly one
1500 * block or not. For the data fork we check this matches di_size,
1501 * implying the file's range is 0..bsize-1.
1503 int /* 1=>1 block, 0=>otherwise */
1505 xfs_inode_t
*ip
, /* incore inode */
1506 int whichfork
) /* data or attr fork */
1508 xfs_ifork_t
*ifp
; /* inode fork pointer */
1509 int rval
; /* return value */
1510 xfs_bmbt_irec_t s
; /* internal version of extent */
1511 struct xfs_iext_cursor icur
;
1514 if (whichfork
== XFS_DATA_FORK
)
1515 return XFS_ISIZE(ip
) == ip
->i_mount
->m_sb
.sb_blocksize
;
1517 if (XFS_IFORK_NEXTENTS(ip
, whichfork
) != 1)
1519 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
1521 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1522 ASSERT(ifp
->if_flags
& XFS_IFEXTENTS
);
1523 xfs_iext_first(ifp
, &icur
);
1524 xfs_iext_get_extent(ifp
, &icur
, &s
);
1525 rval
= s
.br_startoff
== 0 && s
.br_blockcount
== 1;
1526 if (rval
&& whichfork
== XFS_DATA_FORK
)
1527 ASSERT(XFS_ISIZE(ip
) == ip
->i_mount
->m_sb
.sb_blocksize
);
1532 * Extent tree manipulation functions used during allocation.
1536 * Convert a delayed allocation to a real allocation.
1538 STATIC
int /* error */
1539 xfs_bmap_add_extent_delay_real(
1540 struct xfs_bmalloca
*bma
,
1543 struct xfs_bmbt_irec
*new = &bma
->got
;
1544 int error
; /* error return value */
1545 int i
; /* temp state */
1546 xfs_ifork_t
*ifp
; /* inode fork pointer */
1547 xfs_fileoff_t new_endoff
; /* end offset of new entry */
1548 xfs_bmbt_irec_t r
[3]; /* neighbor extent entries */
1549 /* left is 0, right is 1, prev is 2 */
1550 int rval
=0; /* return value (logging flags) */
1551 int state
= xfs_bmap_fork_to_state(whichfork
);
1552 xfs_filblks_t da_new
; /* new count del alloc blocks used */
1553 xfs_filblks_t da_old
; /* old count del alloc blocks used */
1554 xfs_filblks_t temp
=0; /* value for da_new calculations */
1555 int tmp_rval
; /* partial logging flags */
1556 struct xfs_mount
*mp
;
1557 xfs_extnum_t
*nextents
;
1558 struct xfs_bmbt_irec old
;
1560 mp
= bma
->ip
->i_mount
;
1561 ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
1562 ASSERT(whichfork
!= XFS_ATTR_FORK
);
1563 nextents
= (whichfork
== XFS_COW_FORK
? &bma
->ip
->i_cnextents
:
1564 &bma
->ip
->i_d
.di_nextents
);
1566 ASSERT(!isnullstartblock(new->br_startblock
));
1568 (bma
->cur
->bc_private
.b
.flags
& XFS_BTCUR_BPRV_WASDEL
));
1570 XFS_STATS_INC(mp
, xs_add_exlist
);
1577 * Set up a bunch of variables to make the tests simpler.
1579 xfs_iext_get_extent(ifp
, &bma
->icur
, &PREV
);
1580 new_endoff
= new->br_startoff
+ new->br_blockcount
;
1581 ASSERT(isnullstartblock(PREV
.br_startblock
));
1582 ASSERT(PREV
.br_startoff
<= new->br_startoff
);
1583 ASSERT(PREV
.br_startoff
+ PREV
.br_blockcount
>= new_endoff
);
1585 da_old
= startblockval(PREV
.br_startblock
);
1589 * Set flags determining what part of the previous delayed allocation
1590 * extent is being replaced by a real allocation.
1592 if (PREV
.br_startoff
== new->br_startoff
)
1593 state
|= BMAP_LEFT_FILLING
;
1594 if (PREV
.br_startoff
+ PREV
.br_blockcount
== new_endoff
)
1595 state
|= BMAP_RIGHT_FILLING
;
1598 * Check and set flags if this segment has a left neighbor.
1599 * Don't set contiguous if the combined extent would be too large.
1601 if (xfs_iext_peek_prev_extent(ifp
, &bma
->icur
, &LEFT
)) {
1602 state
|= BMAP_LEFT_VALID
;
1603 if (isnullstartblock(LEFT
.br_startblock
))
1604 state
|= BMAP_LEFT_DELAY
;
1607 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
1608 LEFT
.br_startoff
+ LEFT
.br_blockcount
== new->br_startoff
&&
1609 LEFT
.br_startblock
+ LEFT
.br_blockcount
== new->br_startblock
&&
1610 LEFT
.br_state
== new->br_state
&&
1611 LEFT
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
1612 state
|= BMAP_LEFT_CONTIG
;
1615 * Check and set flags if this segment has a right neighbor.
1616 * Don't set contiguous if the combined extent would be too large.
1617 * Also check for all-three-contiguous being too large.
1619 if (xfs_iext_peek_next_extent(ifp
, &bma
->icur
, &RIGHT
)) {
1620 state
|= BMAP_RIGHT_VALID
;
1621 if (isnullstartblock(RIGHT
.br_startblock
))
1622 state
|= BMAP_RIGHT_DELAY
;
1625 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
1626 new_endoff
== RIGHT
.br_startoff
&&
1627 new->br_startblock
+ new->br_blockcount
== RIGHT
.br_startblock
&&
1628 new->br_state
== RIGHT
.br_state
&&
1629 new->br_blockcount
+ RIGHT
.br_blockcount
<= MAXEXTLEN
&&
1630 ((state
& (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
1631 BMAP_RIGHT_FILLING
)) !=
1632 (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
1633 BMAP_RIGHT_FILLING
) ||
1634 LEFT
.br_blockcount
+ new->br_blockcount
+ RIGHT
.br_blockcount
1636 state
|= BMAP_RIGHT_CONTIG
;
1640 * Switch out based on the FILLING and CONTIG state bits.
1642 switch (state
& (BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
1643 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
)) {
1644 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
1645 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
1647 * Filling in all of a previously delayed allocation extent.
1648 * The left and right neighbors are both contiguous with new.
1650 LEFT
.br_blockcount
+= PREV
.br_blockcount
+ RIGHT
.br_blockcount
;
1652 xfs_iext_remove(bma
->ip
, &bma
->icur
, state
);
1653 xfs_iext_remove(bma
->ip
, &bma
->icur
, state
);
1654 xfs_iext_prev(ifp
, &bma
->icur
);
1655 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &LEFT
);
1658 if (bma
->cur
== NULL
)
1659 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1661 rval
= XFS_ILOG_CORE
;
1662 error
= xfs_bmbt_lookup_eq(bma
->cur
, &RIGHT
, &i
);
1665 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1666 error
= xfs_btree_delete(bma
->cur
, &i
);
1669 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1670 error
= xfs_btree_decrement(bma
->cur
, 0, &i
);
1673 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1674 error
= xfs_bmbt_update(bma
->cur
, &LEFT
);
1680 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
1682 * Filling in all of a previously delayed allocation extent.
1683 * The left neighbor is contiguous, the right is not.
1686 LEFT
.br_blockcount
+= PREV
.br_blockcount
;
1688 xfs_iext_remove(bma
->ip
, &bma
->icur
, state
);
1689 xfs_iext_prev(ifp
, &bma
->icur
);
1690 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &LEFT
);
1692 if (bma
->cur
== NULL
)
1693 rval
= XFS_ILOG_DEXT
;
1696 error
= xfs_bmbt_lookup_eq(bma
->cur
, &old
, &i
);
1699 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1700 error
= xfs_bmbt_update(bma
->cur
, &LEFT
);
1706 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
1708 * Filling in all of a previously delayed allocation extent.
1709 * The right neighbor is contiguous, the left is not.
1711 PREV
.br_startblock
= new->br_startblock
;
1712 PREV
.br_blockcount
+= RIGHT
.br_blockcount
;
1714 xfs_iext_next(ifp
, &bma
->icur
);
1715 xfs_iext_remove(bma
->ip
, &bma
->icur
, state
);
1716 xfs_iext_prev(ifp
, &bma
->icur
);
1717 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &PREV
);
1719 if (bma
->cur
== NULL
)
1720 rval
= XFS_ILOG_DEXT
;
1723 error
= xfs_bmbt_lookup_eq(bma
->cur
, &RIGHT
, &i
);
1726 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1727 error
= xfs_bmbt_update(bma
->cur
, &PREV
);
1733 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
1735 * Filling in all of a previously delayed allocation extent.
1736 * Neither the left nor right neighbors are contiguous with
1739 PREV
.br_startblock
= new->br_startblock
;
1740 PREV
.br_state
= new->br_state
;
1741 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &PREV
);
1744 if (bma
->cur
== NULL
)
1745 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1747 rval
= XFS_ILOG_CORE
;
1748 error
= xfs_bmbt_lookup_eq(bma
->cur
, new, &i
);
1751 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
1752 error
= xfs_btree_insert(bma
->cur
, &i
);
1755 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1759 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
:
1761 * Filling in the first part of a previous delayed allocation.
1762 * The left neighbor is contiguous.
1765 temp
= PREV
.br_blockcount
- new->br_blockcount
;
1766 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
1767 startblockval(PREV
.br_startblock
));
1769 LEFT
.br_blockcount
+= new->br_blockcount
;
1771 PREV
.br_blockcount
= temp
;
1772 PREV
.br_startoff
+= new->br_blockcount
;
1773 PREV
.br_startblock
= nullstartblock(da_new
);
1775 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &PREV
);
1776 xfs_iext_prev(ifp
, &bma
->icur
);
1777 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &LEFT
);
1779 if (bma
->cur
== NULL
)
1780 rval
= XFS_ILOG_DEXT
;
1783 error
= xfs_bmbt_lookup_eq(bma
->cur
, &old
, &i
);
1786 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1787 error
= xfs_bmbt_update(bma
->cur
, &LEFT
);
1793 case BMAP_LEFT_FILLING
:
1795 * Filling in the first part of a previous delayed allocation.
1796 * The left neighbor is not contiguous.
1798 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, new);
1800 if (bma
->cur
== NULL
)
1801 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1803 rval
= XFS_ILOG_CORE
;
1804 error
= xfs_bmbt_lookup_eq(bma
->cur
, new, &i
);
1807 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
1808 error
= xfs_btree_insert(bma
->cur
, &i
);
1811 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1814 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
1815 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
1816 bma
->firstblock
, bma
->dfops
,
1817 &bma
->cur
, 1, &tmp_rval
, whichfork
);
1823 temp
= PREV
.br_blockcount
- new->br_blockcount
;
1824 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
1825 startblockval(PREV
.br_startblock
) -
1826 (bma
->cur
? bma
->cur
->bc_private
.b
.allocated
: 0));
1828 PREV
.br_startoff
= new_endoff
;
1829 PREV
.br_blockcount
= temp
;
1830 PREV
.br_startblock
= nullstartblock(da_new
);
1831 xfs_iext_next(ifp
, &bma
->icur
);
1832 xfs_iext_insert(bma
->ip
, &bma
->icur
, &PREV
, state
);
1833 xfs_iext_prev(ifp
, &bma
->icur
);
1836 case BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
1838 * Filling in the last part of a previous delayed allocation.
1839 * The right neighbor is contiguous with the new allocation.
1842 RIGHT
.br_startoff
= new->br_startoff
;
1843 RIGHT
.br_startblock
= new->br_startblock
;
1844 RIGHT
.br_blockcount
+= new->br_blockcount
;
1846 if (bma
->cur
== NULL
)
1847 rval
= XFS_ILOG_DEXT
;
1850 error
= xfs_bmbt_lookup_eq(bma
->cur
, &old
, &i
);
1853 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1854 error
= xfs_bmbt_update(bma
->cur
, &RIGHT
);
1859 temp
= PREV
.br_blockcount
- new->br_blockcount
;
1860 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
1861 startblockval(PREV
.br_startblock
));
1863 PREV
.br_blockcount
= temp
;
1864 PREV
.br_startblock
= nullstartblock(da_new
);
1866 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &PREV
);
1867 xfs_iext_next(ifp
, &bma
->icur
);
1868 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &RIGHT
);
1871 case BMAP_RIGHT_FILLING
:
1873 * Filling in the last part of a previous delayed allocation.
1874 * The right neighbor is not contiguous.
1876 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, new);
1878 if (bma
->cur
== NULL
)
1879 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1881 rval
= XFS_ILOG_CORE
;
1882 error
= xfs_bmbt_lookup_eq(bma
->cur
, new, &i
);
1885 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
1886 error
= xfs_btree_insert(bma
->cur
, &i
);
1889 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1892 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
1893 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
1894 bma
->firstblock
, bma
->dfops
, &bma
->cur
, 1,
1895 &tmp_rval
, whichfork
);
1901 temp
= PREV
.br_blockcount
- new->br_blockcount
;
1902 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
1903 startblockval(PREV
.br_startblock
) -
1904 (bma
->cur
? bma
->cur
->bc_private
.b
.allocated
: 0));
1906 PREV
.br_startblock
= nullstartblock(da_new
);
1907 PREV
.br_blockcount
= temp
;
1908 xfs_iext_insert(bma
->ip
, &bma
->icur
, &PREV
, state
);
1909 xfs_iext_next(ifp
, &bma
->icur
);
1914 * Filling in the middle part of a previous delayed allocation.
1915 * Contiguity is impossible here.
1916 * This case is avoided almost all the time.
1918 * We start with a delayed allocation:
1920 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
1923 * and we are allocating:
1924 * +rrrrrrrrrrrrrrrrr+
1927 * and we set it up for insertion as:
1928 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
1930 * PREV @ idx LEFT RIGHT
1931 * inserted at idx + 1
1935 /* LEFT is the new middle */
1938 /* RIGHT is the new right */
1939 RIGHT
.br_state
= PREV
.br_state
;
1940 RIGHT
.br_startoff
= new_endoff
;
1941 RIGHT
.br_blockcount
=
1942 PREV
.br_startoff
+ PREV
.br_blockcount
- new_endoff
;
1943 RIGHT
.br_startblock
=
1944 nullstartblock(xfs_bmap_worst_indlen(bma
->ip
,
1945 RIGHT
.br_blockcount
));
1948 PREV
.br_blockcount
= new->br_startoff
- PREV
.br_startoff
;
1949 PREV
.br_startblock
=
1950 nullstartblock(xfs_bmap_worst_indlen(bma
->ip
,
1951 PREV
.br_blockcount
));
1952 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &PREV
);
1954 xfs_iext_next(ifp
, &bma
->icur
);
1955 xfs_iext_insert(bma
->ip
, &bma
->icur
, &RIGHT
, state
);
1956 xfs_iext_insert(bma
->ip
, &bma
->icur
, &LEFT
, state
);
1959 if (bma
->cur
== NULL
)
1960 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1962 rval
= XFS_ILOG_CORE
;
1963 error
= xfs_bmbt_lookup_eq(bma
->cur
, new, &i
);
1966 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
1967 error
= xfs_btree_insert(bma
->cur
, &i
);
1970 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1973 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
1974 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
1975 bma
->firstblock
, bma
->dfops
, &bma
->cur
,
1976 1, &tmp_rval
, whichfork
);
1982 da_new
= startblockval(PREV
.br_startblock
) +
1983 startblockval(RIGHT
.br_startblock
);
1986 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
1987 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
1988 case BMAP_LEFT_FILLING
| BMAP_RIGHT_CONTIG
:
1989 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
1990 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
1991 case BMAP_LEFT_CONTIG
:
1992 case BMAP_RIGHT_CONTIG
:
1994 * These cases are all impossible.
1999 /* add reverse mapping unless caller opted out */
2000 if (!(bma
->flags
& XFS_BMAPI_NORMAP
)) {
2001 error
= xfs_rmap_map_extent(mp
, bma
->dfops
, bma
->ip
,
2007 /* convert to a btree if necessary */
2008 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
2009 int tmp_logflags
; /* partial log flag return val */
2011 ASSERT(bma
->cur
== NULL
);
2012 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
2013 bma
->firstblock
, bma
->dfops
, &bma
->cur
,
2014 da_old
> 0, &tmp_logflags
, whichfork
);
2015 bma
->logflags
|= tmp_logflags
;
2021 da_new
+= bma
->cur
->bc_private
.b
.allocated
;
2022 bma
->cur
->bc_private
.b
.allocated
= 0;
2025 /* adjust for changes in reserved delayed indirect blocks */
2026 if (da_new
!= da_old
) {
2027 ASSERT(state
== 0 || da_new
< da_old
);
2028 error
= xfs_mod_fdblocks(mp
, (int64_t)(da_old
- da_new
),
2032 xfs_bmap_check_leaf_extents(bma
->cur
, bma
->ip
, whichfork
);
2034 if (whichfork
!= XFS_COW_FORK
)
2035 bma
->logflags
|= rval
;
2043 * Convert an unwritten allocation to a real allocation or vice versa.
2045 STATIC
int /* error */
2046 xfs_bmap_add_extent_unwritten_real(
2047 struct xfs_trans
*tp
,
2048 xfs_inode_t
*ip
, /* incore inode pointer */
2050 struct xfs_iext_cursor
*icur
,
2051 xfs_btree_cur_t
**curp
, /* if *curp is null, not a btree */
2052 xfs_bmbt_irec_t
*new, /* new data to add to file extents */
2053 xfs_fsblock_t
*first
, /* pointer to firstblock variable */
2054 struct xfs_defer_ops
*dfops
, /* list of extents to be freed */
2055 int *logflagsp
) /* inode logging flags */
2057 xfs_btree_cur_t
*cur
; /* btree cursor */
2058 int error
; /* error return value */
2059 int i
; /* temp state */
2060 xfs_ifork_t
*ifp
; /* inode fork pointer */
2061 xfs_fileoff_t new_endoff
; /* end offset of new entry */
2062 xfs_bmbt_irec_t r
[3]; /* neighbor extent entries */
2063 /* left is 0, right is 1, prev is 2 */
2064 int rval
=0; /* return value (logging flags) */
2065 int state
= xfs_bmap_fork_to_state(whichfork
);
2066 struct xfs_mount
*mp
= ip
->i_mount
;
2067 struct xfs_bmbt_irec old
;
2072 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
2074 ASSERT(!isnullstartblock(new->br_startblock
));
2076 XFS_STATS_INC(mp
, xs_add_exlist
);
2083 * Set up a bunch of variables to make the tests simpler.
2086 xfs_iext_get_extent(ifp
, icur
, &PREV
);
2087 ASSERT(new->br_state
!= PREV
.br_state
);
2088 new_endoff
= new->br_startoff
+ new->br_blockcount
;
2089 ASSERT(PREV
.br_startoff
<= new->br_startoff
);
2090 ASSERT(PREV
.br_startoff
+ PREV
.br_blockcount
>= new_endoff
);
2093 * Set flags determining what part of the previous oldext allocation
2094 * extent is being replaced by a newext allocation.
2096 if (PREV
.br_startoff
== new->br_startoff
)
2097 state
|= BMAP_LEFT_FILLING
;
2098 if (PREV
.br_startoff
+ PREV
.br_blockcount
== new_endoff
)
2099 state
|= BMAP_RIGHT_FILLING
;
2102 * Check and set flags if this segment has a left neighbor.
2103 * Don't set contiguous if the combined extent would be too large.
2105 if (xfs_iext_peek_prev_extent(ifp
, icur
, &LEFT
)) {
2106 state
|= BMAP_LEFT_VALID
;
2107 if (isnullstartblock(LEFT
.br_startblock
))
2108 state
|= BMAP_LEFT_DELAY
;
2111 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
2112 LEFT
.br_startoff
+ LEFT
.br_blockcount
== new->br_startoff
&&
2113 LEFT
.br_startblock
+ LEFT
.br_blockcount
== new->br_startblock
&&
2114 LEFT
.br_state
== new->br_state
&&
2115 LEFT
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
2116 state
|= BMAP_LEFT_CONTIG
;
2119 * Check and set flags if this segment has a right neighbor.
2120 * Don't set contiguous if the combined extent would be too large.
2121 * Also check for all-three-contiguous being too large.
2123 if (xfs_iext_peek_next_extent(ifp
, icur
, &RIGHT
)) {
2124 state
|= BMAP_RIGHT_VALID
;
2125 if (isnullstartblock(RIGHT
.br_startblock
))
2126 state
|= BMAP_RIGHT_DELAY
;
2129 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
2130 new_endoff
== RIGHT
.br_startoff
&&
2131 new->br_startblock
+ new->br_blockcount
== RIGHT
.br_startblock
&&
2132 new->br_state
== RIGHT
.br_state
&&
2133 new->br_blockcount
+ RIGHT
.br_blockcount
<= MAXEXTLEN
&&
2134 ((state
& (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
2135 BMAP_RIGHT_FILLING
)) !=
2136 (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
2137 BMAP_RIGHT_FILLING
) ||
2138 LEFT
.br_blockcount
+ new->br_blockcount
+ RIGHT
.br_blockcount
2140 state
|= BMAP_RIGHT_CONTIG
;
2143 * Switch out based on the FILLING and CONTIG state bits.
2145 switch (state
& (BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
2146 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
)) {
2147 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
2148 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2150 * Setting all of a previous oldext extent to newext.
2151 * The left and right neighbors are both contiguous with new.
2153 LEFT
.br_blockcount
+= PREV
.br_blockcount
+ RIGHT
.br_blockcount
;
2155 xfs_iext_remove(ip
, icur
, state
);
2156 xfs_iext_remove(ip
, icur
, state
);
2157 xfs_iext_prev(ifp
, icur
);
2158 xfs_iext_update_extent(ip
, state
, icur
, &LEFT
);
2159 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2160 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 2);
2162 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2164 rval
= XFS_ILOG_CORE
;
2165 error
= xfs_bmbt_lookup_eq(cur
, &RIGHT
, &i
);
2168 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2169 if ((error
= xfs_btree_delete(cur
, &i
)))
2171 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2172 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2174 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2175 if ((error
= xfs_btree_delete(cur
, &i
)))
2177 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2178 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2180 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2181 error
= xfs_bmbt_update(cur
, &LEFT
);
2187 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
2189 * Setting all of a previous oldext extent to newext.
2190 * The left neighbor is contiguous, the right is not.
2192 LEFT
.br_blockcount
+= PREV
.br_blockcount
;
2194 xfs_iext_remove(ip
, icur
, state
);
2195 xfs_iext_prev(ifp
, icur
);
2196 xfs_iext_update_extent(ip
, state
, icur
, &LEFT
);
2197 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2198 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
2200 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2202 rval
= XFS_ILOG_CORE
;
2203 error
= xfs_bmbt_lookup_eq(cur
, &PREV
, &i
);
2206 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2207 if ((error
= xfs_btree_delete(cur
, &i
)))
2209 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2210 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2212 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2213 error
= xfs_bmbt_update(cur
, &LEFT
);
2219 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2221 * Setting all of a previous oldext extent to newext.
2222 * The right neighbor is contiguous, the left is not.
2224 PREV
.br_blockcount
+= RIGHT
.br_blockcount
;
2225 PREV
.br_state
= new->br_state
;
2227 xfs_iext_next(ifp
, icur
);
2228 xfs_iext_remove(ip
, icur
, state
);
2229 xfs_iext_prev(ifp
, icur
);
2230 xfs_iext_update_extent(ip
, state
, icur
, &PREV
);
2232 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2233 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
2235 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2237 rval
= XFS_ILOG_CORE
;
2238 error
= xfs_bmbt_lookup_eq(cur
, &RIGHT
, &i
);
2241 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2242 if ((error
= xfs_btree_delete(cur
, &i
)))
2244 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2245 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2247 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2248 error
= xfs_bmbt_update(cur
, &PREV
);
2254 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
2256 * Setting all of a previous oldext extent to newext.
2257 * Neither the left nor right neighbors are contiguous with
2260 PREV
.br_state
= new->br_state
;
2261 xfs_iext_update_extent(ip
, state
, icur
, &PREV
);
2264 rval
= XFS_ILOG_DEXT
;
2267 error
= xfs_bmbt_lookup_eq(cur
, new, &i
);
2270 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2271 error
= xfs_bmbt_update(cur
, &PREV
);
2277 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
:
2279 * Setting the first part of a previous oldext extent to newext.
2280 * The left neighbor is contiguous.
2282 LEFT
.br_blockcount
+= new->br_blockcount
;
2285 PREV
.br_startoff
+= new->br_blockcount
;
2286 PREV
.br_startblock
+= new->br_blockcount
;
2287 PREV
.br_blockcount
-= new->br_blockcount
;
2289 xfs_iext_update_extent(ip
, state
, icur
, &PREV
);
2290 xfs_iext_prev(ifp
, icur
);
2291 xfs_iext_update_extent(ip
, state
, icur
, &LEFT
);
2294 rval
= XFS_ILOG_DEXT
;
2297 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2300 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2301 error
= xfs_bmbt_update(cur
, &PREV
);
2304 error
= xfs_btree_decrement(cur
, 0, &i
);
2307 error
= xfs_bmbt_update(cur
, &LEFT
);
2313 case BMAP_LEFT_FILLING
:
2315 * Setting the first part of a previous oldext extent to newext.
2316 * The left neighbor is not contiguous.
2319 PREV
.br_startoff
+= new->br_blockcount
;
2320 PREV
.br_startblock
+= new->br_blockcount
;
2321 PREV
.br_blockcount
-= new->br_blockcount
;
2323 xfs_iext_update_extent(ip
, state
, icur
, &PREV
);
2324 xfs_iext_insert(ip
, icur
, new, state
);
2325 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2326 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
2328 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2330 rval
= XFS_ILOG_CORE
;
2331 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2334 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2335 error
= xfs_bmbt_update(cur
, &PREV
);
2338 cur
->bc_rec
.b
= *new;
2339 if ((error
= xfs_btree_insert(cur
, &i
)))
2341 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2345 case BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2347 * Setting the last part of a previous oldext extent to newext.
2348 * The right neighbor is contiguous with the new allocation.
2351 PREV
.br_blockcount
-= new->br_blockcount
;
2353 RIGHT
.br_startoff
= new->br_startoff
;
2354 RIGHT
.br_startblock
= new->br_startblock
;
2355 RIGHT
.br_blockcount
+= new->br_blockcount
;
2357 xfs_iext_update_extent(ip
, state
, icur
, &PREV
);
2358 xfs_iext_next(ifp
, icur
);
2359 xfs_iext_update_extent(ip
, state
, icur
, &RIGHT
);
2362 rval
= XFS_ILOG_DEXT
;
2365 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2368 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2369 error
= xfs_bmbt_update(cur
, &PREV
);
2372 error
= xfs_btree_increment(cur
, 0, &i
);
2375 error
= xfs_bmbt_update(cur
, &RIGHT
);
2381 case BMAP_RIGHT_FILLING
:
2383 * Setting the last part of a previous oldext extent to newext.
2384 * The right neighbor is not contiguous.
2387 PREV
.br_blockcount
-= new->br_blockcount
;
2389 xfs_iext_update_extent(ip
, state
, icur
, &PREV
);
2390 xfs_iext_next(ifp
, icur
);
2391 xfs_iext_insert(ip
, icur
, new, state
);
2393 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2394 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
2396 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2398 rval
= XFS_ILOG_CORE
;
2399 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2402 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2403 error
= xfs_bmbt_update(cur
, &PREV
);
2406 error
= xfs_bmbt_lookup_eq(cur
, new, &i
);
2409 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
2410 if ((error
= xfs_btree_insert(cur
, &i
)))
2412 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2418 * Setting the middle part of a previous oldext extent to
2419 * newext. Contiguity is impossible here.
2420 * One extent becomes three extents.
2423 PREV
.br_blockcount
= new->br_startoff
- PREV
.br_startoff
;
2426 r
[1].br_startoff
= new_endoff
;
2427 r
[1].br_blockcount
=
2428 old
.br_startoff
+ old
.br_blockcount
- new_endoff
;
2429 r
[1].br_startblock
= new->br_startblock
+ new->br_blockcount
;
2430 r
[1].br_state
= PREV
.br_state
;
2432 xfs_iext_update_extent(ip
, state
, icur
, &PREV
);
2433 xfs_iext_next(ifp
, icur
);
2434 xfs_iext_insert(ip
, icur
, &r
[1], state
);
2435 xfs_iext_insert(ip
, icur
, &r
[0], state
);
2437 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2438 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 2);
2440 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2442 rval
= XFS_ILOG_CORE
;
2443 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2446 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2447 /* new right extent - oldext */
2448 error
= xfs_bmbt_update(cur
, &r
[1]);
2451 /* new left extent - oldext */
2452 cur
->bc_rec
.b
= PREV
;
2453 if ((error
= xfs_btree_insert(cur
, &i
)))
2455 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2457 * Reset the cursor to the position of the new extent
2458 * we are about to insert as we can't trust it after
2459 * the previous insert.
2461 error
= xfs_bmbt_lookup_eq(cur
, new, &i
);
2464 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
2465 /* new middle extent - newext */
2466 if ((error
= xfs_btree_insert(cur
, &i
)))
2468 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2472 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2473 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2474 case BMAP_LEFT_FILLING
| BMAP_RIGHT_CONTIG
:
2475 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
2476 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2477 case BMAP_LEFT_CONTIG
:
2478 case BMAP_RIGHT_CONTIG
:
2480 * These cases are all impossible.
2485 /* update reverse mappings */
2486 error
= xfs_rmap_convert_extent(mp
, dfops
, ip
, whichfork
, new);
2490 /* convert to a btree if necessary */
2491 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
2492 int tmp_logflags
; /* partial log flag return val */
2494 ASSERT(cur
== NULL
);
2495 error
= xfs_bmap_extents_to_btree(tp
, ip
, first
, dfops
, &cur
,
2496 0, &tmp_logflags
, whichfork
);
2497 *logflagsp
|= tmp_logflags
;
2502 /* clear out the allocated field, done with it now in any case. */
2504 cur
->bc_private
.b
.allocated
= 0;
2508 xfs_bmap_check_leaf_extents(*curp
, ip
, whichfork
);
2518 * Convert a hole to a delayed allocation.
2521 xfs_bmap_add_extent_hole_delay(
2522 xfs_inode_t
*ip
, /* incore inode pointer */
2524 struct xfs_iext_cursor
*icur
,
2525 xfs_bmbt_irec_t
*new) /* new data to add to file extents */
2527 xfs_ifork_t
*ifp
; /* inode fork pointer */
2528 xfs_bmbt_irec_t left
; /* left neighbor extent entry */
2529 xfs_filblks_t newlen
=0; /* new indirect size */
2530 xfs_filblks_t oldlen
=0; /* old indirect size */
2531 xfs_bmbt_irec_t right
; /* right neighbor extent entry */
2532 int state
= xfs_bmap_fork_to_state(whichfork
);
2533 xfs_filblks_t temp
; /* temp for indirect calculations */
2535 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
2536 ASSERT(isnullstartblock(new->br_startblock
));
2539 * Check and set flags if this segment has a left neighbor
2541 if (xfs_iext_peek_prev_extent(ifp
, icur
, &left
)) {
2542 state
|= BMAP_LEFT_VALID
;
2543 if (isnullstartblock(left
.br_startblock
))
2544 state
|= BMAP_LEFT_DELAY
;
2548 * Check and set flags if the current (right) segment exists.
2549 * If it doesn't exist, we're converting the hole at end-of-file.
2551 if (xfs_iext_get_extent(ifp
, icur
, &right
)) {
2552 state
|= BMAP_RIGHT_VALID
;
2553 if (isnullstartblock(right
.br_startblock
))
2554 state
|= BMAP_RIGHT_DELAY
;
2558 * Set contiguity flags on the left and right neighbors.
2559 * Don't let extents get too large, even if the pieces are contiguous.
2561 if ((state
& BMAP_LEFT_VALID
) && (state
& BMAP_LEFT_DELAY
) &&
2562 left
.br_startoff
+ left
.br_blockcount
== new->br_startoff
&&
2563 left
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
2564 state
|= BMAP_LEFT_CONTIG
;
2566 if ((state
& BMAP_RIGHT_VALID
) && (state
& BMAP_RIGHT_DELAY
) &&
2567 new->br_startoff
+ new->br_blockcount
== right
.br_startoff
&&
2568 new->br_blockcount
+ right
.br_blockcount
<= MAXEXTLEN
&&
2569 (!(state
& BMAP_LEFT_CONTIG
) ||
2570 (left
.br_blockcount
+ new->br_blockcount
+
2571 right
.br_blockcount
<= MAXEXTLEN
)))
2572 state
|= BMAP_RIGHT_CONTIG
;
2575 * Switch out based on the contiguity flags.
2577 switch (state
& (BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
)) {
2578 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2580 * New allocation is contiguous with delayed allocations
2581 * on the left and on the right.
2582 * Merge all three into a single extent record.
2584 temp
= left
.br_blockcount
+ new->br_blockcount
+
2585 right
.br_blockcount
;
2587 oldlen
= startblockval(left
.br_startblock
) +
2588 startblockval(new->br_startblock
) +
2589 startblockval(right
.br_startblock
);
2590 newlen
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
2592 left
.br_startblock
= nullstartblock(newlen
);
2593 left
.br_blockcount
= temp
;
2595 xfs_iext_remove(ip
, icur
, state
);
2596 xfs_iext_prev(ifp
, icur
);
2597 xfs_iext_update_extent(ip
, state
, icur
, &left
);
2600 case BMAP_LEFT_CONTIG
:
2602 * New allocation is contiguous with a delayed allocation
2604 * Merge the new allocation with the left neighbor.
2606 temp
= left
.br_blockcount
+ new->br_blockcount
;
2608 oldlen
= startblockval(left
.br_startblock
) +
2609 startblockval(new->br_startblock
);
2610 newlen
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
2612 left
.br_blockcount
= temp
;
2613 left
.br_startblock
= nullstartblock(newlen
);
2615 xfs_iext_prev(ifp
, icur
);
2616 xfs_iext_update_extent(ip
, state
, icur
, &left
);
2619 case BMAP_RIGHT_CONTIG
:
2621 * New allocation is contiguous with a delayed allocation
2623 * Merge the new allocation with the right neighbor.
2625 temp
= new->br_blockcount
+ right
.br_blockcount
;
2626 oldlen
= startblockval(new->br_startblock
) +
2627 startblockval(right
.br_startblock
);
2628 newlen
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
2630 right
.br_startoff
= new->br_startoff
;
2631 right
.br_startblock
= nullstartblock(newlen
);
2632 right
.br_blockcount
= temp
;
2633 xfs_iext_update_extent(ip
, state
, icur
, &right
);
2638 * New allocation is not contiguous with another
2639 * delayed allocation.
2640 * Insert a new entry.
2642 oldlen
= newlen
= 0;
2643 xfs_iext_insert(ip
, icur
, new, state
);
2646 if (oldlen
!= newlen
) {
2647 ASSERT(oldlen
> newlen
);
2648 xfs_mod_fdblocks(ip
->i_mount
, (int64_t)(oldlen
- newlen
),
2651 * Nothing to do for disk quota accounting here.
2657 * Convert a hole to a real allocation.
2659 STATIC
int /* error */
2660 xfs_bmap_add_extent_hole_real(
2661 struct xfs_trans
*tp
,
2662 struct xfs_inode
*ip
,
2664 struct xfs_iext_cursor
*icur
,
2665 struct xfs_btree_cur
**curp
,
2666 struct xfs_bmbt_irec
*new,
2667 xfs_fsblock_t
*first
,
2668 struct xfs_defer_ops
*dfops
,
2672 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
2673 struct xfs_mount
*mp
= ip
->i_mount
;
2674 struct xfs_btree_cur
*cur
= *curp
;
2675 int error
; /* error return value */
2676 int i
; /* temp state */
2677 xfs_bmbt_irec_t left
; /* left neighbor extent entry */
2678 xfs_bmbt_irec_t right
; /* right neighbor extent entry */
2679 int rval
=0; /* return value (logging flags) */
2680 int state
= xfs_bmap_fork_to_state(whichfork
);
2681 struct xfs_bmbt_irec old
;
2683 ASSERT(!isnullstartblock(new->br_startblock
));
2684 ASSERT(!cur
|| !(cur
->bc_private
.b
.flags
& XFS_BTCUR_BPRV_WASDEL
));
2686 XFS_STATS_INC(mp
, xs_add_exlist
);
2689 * Check and set flags if this segment has a left neighbor.
2691 if (xfs_iext_peek_prev_extent(ifp
, icur
, &left
)) {
2692 state
|= BMAP_LEFT_VALID
;
2693 if (isnullstartblock(left
.br_startblock
))
2694 state
|= BMAP_LEFT_DELAY
;
2698 * Check and set flags if this segment has a current value.
2699 * Not true if we're inserting into the "hole" at eof.
2701 if (xfs_iext_get_extent(ifp
, icur
, &right
)) {
2702 state
|= BMAP_RIGHT_VALID
;
2703 if (isnullstartblock(right
.br_startblock
))
2704 state
|= BMAP_RIGHT_DELAY
;
2708 * We're inserting a real allocation between "left" and "right".
2709 * Set the contiguity flags. Don't let extents get too large.
2711 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
2712 left
.br_startoff
+ left
.br_blockcount
== new->br_startoff
&&
2713 left
.br_startblock
+ left
.br_blockcount
== new->br_startblock
&&
2714 left
.br_state
== new->br_state
&&
2715 left
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
2716 state
|= BMAP_LEFT_CONTIG
;
2718 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
2719 new->br_startoff
+ new->br_blockcount
== right
.br_startoff
&&
2720 new->br_startblock
+ new->br_blockcount
== right
.br_startblock
&&
2721 new->br_state
== right
.br_state
&&
2722 new->br_blockcount
+ right
.br_blockcount
<= MAXEXTLEN
&&
2723 (!(state
& BMAP_LEFT_CONTIG
) ||
2724 left
.br_blockcount
+ new->br_blockcount
+
2725 right
.br_blockcount
<= MAXEXTLEN
))
2726 state
|= BMAP_RIGHT_CONTIG
;
2730 * Select which case we're in here, and implement it.
2732 switch (state
& (BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
)) {
2733 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2735 * New allocation is contiguous with real allocations on the
2736 * left and on the right.
2737 * Merge all three into a single extent record.
2739 left
.br_blockcount
+= new->br_blockcount
+ right
.br_blockcount
;
2741 xfs_iext_remove(ip
, icur
, state
);
2742 xfs_iext_prev(ifp
, icur
);
2743 xfs_iext_update_extent(ip
, state
, icur
, &left
);
2745 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2746 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
2748 rval
= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
2750 rval
= XFS_ILOG_CORE
;
2751 error
= xfs_bmbt_lookup_eq(cur
, &right
, &i
);
2754 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2755 error
= xfs_btree_delete(cur
, &i
);
2758 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2759 error
= xfs_btree_decrement(cur
, 0, &i
);
2762 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2763 error
= xfs_bmbt_update(cur
, &left
);
2769 case BMAP_LEFT_CONTIG
:
2771 * New allocation is contiguous with a real allocation
2773 * Merge the new allocation with the left neighbor.
2776 left
.br_blockcount
+= new->br_blockcount
;
2778 xfs_iext_prev(ifp
, icur
);
2779 xfs_iext_update_extent(ip
, state
, icur
, &left
);
2782 rval
= xfs_ilog_fext(whichfork
);
2785 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2788 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2789 error
= xfs_bmbt_update(cur
, &left
);
2795 case BMAP_RIGHT_CONTIG
:
2797 * New allocation is contiguous with a real allocation
2799 * Merge the new allocation with the right neighbor.
2803 right
.br_startoff
= new->br_startoff
;
2804 right
.br_startblock
= new->br_startblock
;
2805 right
.br_blockcount
+= new->br_blockcount
;
2806 xfs_iext_update_extent(ip
, state
, icur
, &right
);
2809 rval
= xfs_ilog_fext(whichfork
);
2812 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2815 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2816 error
= xfs_bmbt_update(cur
, &right
);
2824 * New allocation is not contiguous with another
2826 * Insert a new entry.
2828 xfs_iext_insert(ip
, icur
, new, state
);
2829 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2830 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
2832 rval
= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
2834 rval
= XFS_ILOG_CORE
;
2835 error
= xfs_bmbt_lookup_eq(cur
, new, &i
);
2838 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
2839 error
= xfs_btree_insert(cur
, &i
);
2842 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2847 /* add reverse mapping unless caller opted out */
2848 if (!(flags
& XFS_BMAPI_NORMAP
)) {
2849 error
= xfs_rmap_map_extent(mp
, dfops
, ip
, whichfork
, new);
2854 /* convert to a btree if necessary */
2855 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
2856 int tmp_logflags
; /* partial log flag return val */
2858 ASSERT(cur
== NULL
);
2859 error
= xfs_bmap_extents_to_btree(tp
, ip
, first
, dfops
, curp
,
2860 0, &tmp_logflags
, whichfork
);
2861 *logflagsp
|= tmp_logflags
;
2867 /* clear out the allocated field, done with it now in any case. */
2869 cur
->bc_private
.b
.allocated
= 0;
2871 xfs_bmap_check_leaf_extents(cur
, ip
, whichfork
);
2878 * Functions used in the extent read, allocate and remove paths
2882 * Adjust the size of the new extent based on di_extsize and rt extsize.
2885 xfs_bmap_extsize_align(
2887 xfs_bmbt_irec_t
*gotp
, /* next extent pointer */
2888 xfs_bmbt_irec_t
*prevp
, /* previous extent pointer */
2889 xfs_extlen_t extsz
, /* align to this extent size */
2890 int rt
, /* is this a realtime inode? */
2891 int eof
, /* is extent at end-of-file? */
2892 int delay
, /* creating delalloc extent? */
2893 int convert
, /* overwriting unwritten extent? */
2894 xfs_fileoff_t
*offp
, /* in/out: aligned offset */
2895 xfs_extlen_t
*lenp
) /* in/out: aligned length */
2897 xfs_fileoff_t orig_off
; /* original offset */
2898 xfs_extlen_t orig_alen
; /* original length */
2899 xfs_fileoff_t orig_end
; /* original off+len */
2900 xfs_fileoff_t nexto
; /* next file offset */
2901 xfs_fileoff_t prevo
; /* previous file offset */
2902 xfs_fileoff_t align_off
; /* temp for offset */
2903 xfs_extlen_t align_alen
; /* temp for length */
2904 xfs_extlen_t temp
; /* temp for calculations */
2909 orig_off
= align_off
= *offp
;
2910 orig_alen
= align_alen
= *lenp
;
2911 orig_end
= orig_off
+ orig_alen
;
2914 * If this request overlaps an existing extent, then don't
2915 * attempt to perform any additional alignment.
2917 if (!delay
&& !eof
&&
2918 (orig_off
>= gotp
->br_startoff
) &&
2919 (orig_end
<= gotp
->br_startoff
+ gotp
->br_blockcount
)) {
2924 * If the file offset is unaligned vs. the extent size
2925 * we need to align it. This will be possible unless
2926 * the file was previously written with a kernel that didn't
2927 * perform this alignment, or if a truncate shot us in the
2930 temp
= do_mod(orig_off
, extsz
);
2936 /* Same adjustment for the end of the requested area. */
2937 temp
= (align_alen
% extsz
);
2939 align_alen
+= extsz
- temp
;
2942 * For large extent hint sizes, the aligned extent might be larger than
2943 * MAXEXTLEN. In that case, reduce the size by an extsz so that it pulls
2944 * the length back under MAXEXTLEN. The outer allocation loops handle
2945 * short allocation just fine, so it is safe to do this. We only want to
2946 * do it when we are forced to, though, because it means more allocation
2947 * operations are required.
2949 while (align_alen
> MAXEXTLEN
)
2950 align_alen
-= extsz
;
2951 ASSERT(align_alen
<= MAXEXTLEN
);
2954 * If the previous block overlaps with this proposed allocation
2955 * then move the start forward without adjusting the length.
2957 if (prevp
->br_startoff
!= NULLFILEOFF
) {
2958 if (prevp
->br_startblock
== HOLESTARTBLOCK
)
2959 prevo
= prevp
->br_startoff
;
2961 prevo
= prevp
->br_startoff
+ prevp
->br_blockcount
;
2964 if (align_off
!= orig_off
&& align_off
< prevo
)
2967 * If the next block overlaps with this proposed allocation
2968 * then move the start back without adjusting the length,
2969 * but not before offset 0.
2970 * This may of course make the start overlap previous block,
2971 * and if we hit the offset 0 limit then the next block
2972 * can still overlap too.
2974 if (!eof
&& gotp
->br_startoff
!= NULLFILEOFF
) {
2975 if ((delay
&& gotp
->br_startblock
== HOLESTARTBLOCK
) ||
2976 (!delay
&& gotp
->br_startblock
== DELAYSTARTBLOCK
))
2977 nexto
= gotp
->br_startoff
+ gotp
->br_blockcount
;
2979 nexto
= gotp
->br_startoff
;
2981 nexto
= NULLFILEOFF
;
2983 align_off
+ align_alen
!= orig_end
&&
2984 align_off
+ align_alen
> nexto
)
2985 align_off
= nexto
> align_alen
? nexto
- align_alen
: 0;
2987 * If we're now overlapping the next or previous extent that
2988 * means we can't fit an extsz piece in this hole. Just move
2989 * the start forward to the first valid spot and set
2990 * the length so we hit the end.
2992 if (align_off
!= orig_off
&& align_off
< prevo
)
2994 if (align_off
+ align_alen
!= orig_end
&&
2995 align_off
+ align_alen
> nexto
&&
2996 nexto
!= NULLFILEOFF
) {
2997 ASSERT(nexto
> prevo
);
2998 align_alen
= nexto
- align_off
;
3002 * If realtime, and the result isn't a multiple of the realtime
3003 * extent size we need to remove blocks until it is.
3005 if (rt
&& (temp
= (align_alen
% mp
->m_sb
.sb_rextsize
))) {
3007 * We're not covering the original request, or
3008 * we won't be able to once we fix the length.
3010 if (orig_off
< align_off
||
3011 orig_end
> align_off
+ align_alen
||
3012 align_alen
- temp
< orig_alen
)
3015 * Try to fix it by moving the start up.
3017 if (align_off
+ temp
<= orig_off
) {
3022 * Try to fix it by moving the end in.
3024 else if (align_off
+ align_alen
- temp
>= orig_end
)
3027 * Set the start to the minimum then trim the length.
3030 align_alen
-= orig_off
- align_off
;
3031 align_off
= orig_off
;
3032 align_alen
-= align_alen
% mp
->m_sb
.sb_rextsize
;
3035 * Result doesn't cover the request, fail it.
3037 if (orig_off
< align_off
|| orig_end
> align_off
+ align_alen
)
3040 ASSERT(orig_off
>= align_off
);
3041 /* see MAXEXTLEN handling above */
3042 ASSERT(orig_end
<= align_off
+ align_alen
||
3043 align_alen
+ extsz
> MAXEXTLEN
);
3047 if (!eof
&& gotp
->br_startoff
!= NULLFILEOFF
)
3048 ASSERT(align_off
+ align_alen
<= gotp
->br_startoff
);
3049 if (prevp
->br_startoff
!= NULLFILEOFF
)
3050 ASSERT(align_off
>= prevp
->br_startoff
+ prevp
->br_blockcount
);
3058 #define XFS_ALLOC_GAP_UNITS 4
3062 struct xfs_bmalloca
*ap
) /* bmap alloc argument struct */
3064 xfs_fsblock_t adjust
; /* adjustment to block numbers */
3065 xfs_agnumber_t fb_agno
; /* ag number of ap->firstblock */
3066 xfs_mount_t
*mp
; /* mount point structure */
3067 int nullfb
; /* true if ap->firstblock isn't set */
3068 int rt
; /* true if inode is realtime */
3070 #define ISVALID(x,y) \
3072 (x) < mp->m_sb.sb_rblocks : \
3073 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
3074 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
3075 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
3077 mp
= ap
->ip
->i_mount
;
3078 nullfb
= *ap
->firstblock
== NULLFSBLOCK
;
3079 rt
= XFS_IS_REALTIME_INODE(ap
->ip
) &&
3080 xfs_alloc_is_userdata(ap
->datatype
);
3081 fb_agno
= nullfb
? NULLAGNUMBER
: XFS_FSB_TO_AGNO(mp
, *ap
->firstblock
);
3083 * If allocating at eof, and there's a previous real block,
3084 * try to use its last block as our starting point.
3086 if (ap
->eof
&& ap
->prev
.br_startoff
!= NULLFILEOFF
&&
3087 !isnullstartblock(ap
->prev
.br_startblock
) &&
3088 ISVALID(ap
->prev
.br_startblock
+ ap
->prev
.br_blockcount
,
3089 ap
->prev
.br_startblock
)) {
3090 ap
->blkno
= ap
->prev
.br_startblock
+ ap
->prev
.br_blockcount
;
3092 * Adjust for the gap between prevp and us.
3094 adjust
= ap
->offset
-
3095 (ap
->prev
.br_startoff
+ ap
->prev
.br_blockcount
);
3097 ISVALID(ap
->blkno
+ adjust
, ap
->prev
.br_startblock
))
3098 ap
->blkno
+= adjust
;
3101 * If not at eof, then compare the two neighbor blocks.
3102 * Figure out whether either one gives us a good starting point,
3103 * and pick the better one.
3105 else if (!ap
->eof
) {
3106 xfs_fsblock_t gotbno
; /* right side block number */
3107 xfs_fsblock_t gotdiff
=0; /* right side difference */
3108 xfs_fsblock_t prevbno
; /* left side block number */
3109 xfs_fsblock_t prevdiff
=0; /* left side difference */
3112 * If there's a previous (left) block, select a requested
3113 * start block based on it.
3115 if (ap
->prev
.br_startoff
!= NULLFILEOFF
&&
3116 !isnullstartblock(ap
->prev
.br_startblock
) &&
3117 (prevbno
= ap
->prev
.br_startblock
+
3118 ap
->prev
.br_blockcount
) &&
3119 ISVALID(prevbno
, ap
->prev
.br_startblock
)) {
3121 * Calculate gap to end of previous block.
3123 adjust
= prevdiff
= ap
->offset
-
3124 (ap
->prev
.br_startoff
+
3125 ap
->prev
.br_blockcount
);
3127 * Figure the startblock based on the previous block's
3128 * end and the gap size.
3130 * If the gap is large relative to the piece we're
3131 * allocating, or using it gives us an invalid block
3132 * number, then just use the end of the previous block.
3134 if (prevdiff
<= XFS_ALLOC_GAP_UNITS
* ap
->length
&&
3135 ISVALID(prevbno
+ prevdiff
,
3136 ap
->prev
.br_startblock
))
3141 * If the firstblock forbids it, can't use it,
3144 if (!rt
&& !nullfb
&&
3145 XFS_FSB_TO_AGNO(mp
, prevbno
) != fb_agno
)
3146 prevbno
= NULLFSBLOCK
;
3149 * No previous block or can't follow it, just default.
3152 prevbno
= NULLFSBLOCK
;
3154 * If there's a following (right) block, select a requested
3155 * start block based on it.
3157 if (!isnullstartblock(ap
->got
.br_startblock
)) {
3159 * Calculate gap to start of next block.
3161 adjust
= gotdiff
= ap
->got
.br_startoff
- ap
->offset
;
3163 * Figure the startblock based on the next block's
3164 * start and the gap size.
3166 gotbno
= ap
->got
.br_startblock
;
3169 * If the gap is large relative to the piece we're
3170 * allocating, or using it gives us an invalid block
3171 * number, then just use the start of the next block
3172 * offset by our length.
3174 if (gotdiff
<= XFS_ALLOC_GAP_UNITS
* ap
->length
&&
3175 ISVALID(gotbno
- gotdiff
, gotbno
))
3177 else if (ISVALID(gotbno
- ap
->length
, gotbno
)) {
3178 gotbno
-= ap
->length
;
3179 gotdiff
+= adjust
- ap
->length
;
3183 * If the firstblock forbids it, can't use it,
3186 if (!rt
&& !nullfb
&&
3187 XFS_FSB_TO_AGNO(mp
, gotbno
) != fb_agno
)
3188 gotbno
= NULLFSBLOCK
;
3191 * No next block, just default.
3194 gotbno
= NULLFSBLOCK
;
3196 * If both valid, pick the better one, else the only good
3197 * one, else ap->blkno is already set (to 0 or the inode block).
3199 if (prevbno
!= NULLFSBLOCK
&& gotbno
!= NULLFSBLOCK
)
3200 ap
->blkno
= prevdiff
<= gotdiff
? prevbno
: gotbno
;
3201 else if (prevbno
!= NULLFSBLOCK
)
3202 ap
->blkno
= prevbno
;
3203 else if (gotbno
!= NULLFSBLOCK
)
3210 xfs_bmap_longest_free_extent(
3211 struct xfs_trans
*tp
,
3216 struct xfs_mount
*mp
= tp
->t_mountp
;
3217 struct xfs_perag
*pag
;
3218 xfs_extlen_t longest
;
3221 pag
= xfs_perag_get(mp
, ag
);
3222 if (!pag
->pagf_init
) {
3223 error
= xfs_alloc_pagf_init(mp
, tp
, ag
, XFS_ALLOC_FLAG_TRYLOCK
);
3227 if (!pag
->pagf_init
) {
3233 longest
= xfs_alloc_longest_free_extent(pag
,
3234 xfs_alloc_min_freelist(mp
, pag
),
3235 xfs_ag_resv_needed(pag
, XFS_AG_RESV_NONE
));
3236 if (*blen
< longest
)
3245 xfs_bmap_select_minlen(
3246 struct xfs_bmalloca
*ap
,
3247 struct xfs_alloc_arg
*args
,
3251 if (notinit
|| *blen
< ap
->minlen
) {
3253 * Since we did a BUF_TRYLOCK above, it is possible that
3254 * there is space for this request.
3256 args
->minlen
= ap
->minlen
;
3257 } else if (*blen
< args
->maxlen
) {
3259 * If the best seen length is less than the request length,
3260 * use the best as the minimum.
3262 args
->minlen
= *blen
;
3265 * Otherwise we've seen an extent as big as maxlen, use that
3268 args
->minlen
= args
->maxlen
;
3273 xfs_bmap_btalloc_nullfb(
3274 struct xfs_bmalloca
*ap
,
3275 struct xfs_alloc_arg
*args
,
3278 struct xfs_mount
*mp
= ap
->ip
->i_mount
;
3279 xfs_agnumber_t ag
, startag
;
3283 args
->type
= XFS_ALLOCTYPE_START_BNO
;
3284 args
->total
= ap
->total
;
3286 startag
= ag
= XFS_FSB_TO_AGNO(mp
, args
->fsbno
);
3287 if (startag
== NULLAGNUMBER
)
3290 while (*blen
< args
->maxlen
) {
3291 error
= xfs_bmap_longest_free_extent(args
->tp
, ag
, blen
,
3296 if (++ag
== mp
->m_sb
.sb_agcount
)
3302 xfs_bmap_select_minlen(ap
, args
, blen
, notinit
);
3307 xfs_bmap_btalloc_filestreams(
3308 struct xfs_bmalloca
*ap
,
3309 struct xfs_alloc_arg
*args
,
3312 struct xfs_mount
*mp
= ap
->ip
->i_mount
;
3317 args
->type
= XFS_ALLOCTYPE_NEAR_BNO
;
3318 args
->total
= ap
->total
;
3320 ag
= XFS_FSB_TO_AGNO(mp
, args
->fsbno
);
3321 if (ag
== NULLAGNUMBER
)
3324 error
= xfs_bmap_longest_free_extent(args
->tp
, ag
, blen
, ¬init
);
3328 if (*blen
< args
->maxlen
) {
3329 error
= xfs_filestream_new_ag(ap
, &ag
);
3333 error
= xfs_bmap_longest_free_extent(args
->tp
, ag
, blen
,
3340 xfs_bmap_select_minlen(ap
, args
, blen
, notinit
);
3343 * Set the failure fallback case to look in the selected AG as stream
3346 ap
->blkno
= args
->fsbno
= XFS_AGB_TO_FSB(mp
, ag
, 0);
3350 /* Update all inode and quota accounting for the allocation we just did. */
3352 xfs_bmap_btalloc_accounting(
3353 struct xfs_bmalloca
*ap
,
3354 struct xfs_alloc_arg
*args
)
3356 if (ap
->flags
& XFS_BMAPI_COWFORK
) {
3358 * COW fork blocks are in-core only and thus are treated as
3359 * in-core quota reservation (like delalloc blocks) even when
3360 * converted to real blocks. The quota reservation is not
3361 * accounted to disk until blocks are remapped to the data
3362 * fork. So if these blocks were previously delalloc, we
3363 * already have quota reservation and there's nothing to do
3370 * Otherwise, we've allocated blocks in a hole. The transaction
3371 * has acquired in-core quota reservation for this extent.
3372 * Rather than account these as real blocks, however, we reduce
3373 * the transaction quota reservation based on the allocation.
3374 * This essentially transfers the transaction quota reservation
3375 * to that of a delalloc extent.
3377 ap
->ip
->i_delayed_blks
+= args
->len
;
3378 xfs_trans_mod_dquot_byino(ap
->tp
, ap
->ip
, XFS_TRANS_DQ_RES_BLKS
,
3383 /* data/attr fork only */
3384 ap
->ip
->i_d
.di_nblocks
+= args
->len
;
3385 xfs_trans_log_inode(ap
->tp
, ap
->ip
, XFS_ILOG_CORE
);
3387 ap
->ip
->i_delayed_blks
-= args
->len
;
3388 xfs_trans_mod_dquot_byino(ap
->tp
, ap
->ip
,
3389 ap
->wasdel
? XFS_TRANS_DQ_DELBCOUNT
: XFS_TRANS_DQ_BCOUNT
,
3395 struct xfs_bmalloca
*ap
) /* bmap alloc argument struct */
3397 xfs_mount_t
*mp
; /* mount point structure */
3398 xfs_alloctype_t atype
= 0; /* type for allocation routines */
3399 xfs_extlen_t align
= 0; /* minimum allocation alignment */
3400 xfs_agnumber_t fb_agno
; /* ag number of ap->firstblock */
3402 xfs_alloc_arg_t args
;
3403 xfs_fileoff_t orig_offset
;
3404 xfs_extlen_t orig_length
;
3406 xfs_extlen_t nextminlen
= 0;
3407 int nullfb
; /* true if ap->firstblock isn't set */
3414 orig_offset
= ap
->offset
;
3415 orig_length
= ap
->length
;
3417 mp
= ap
->ip
->i_mount
;
3419 /* stripe alignment for allocation is determined by mount parameters */
3421 if (mp
->m_swidth
&& (mp
->m_flags
& XFS_MOUNT_SWALLOC
))
3422 stripe_align
= mp
->m_swidth
;
3423 else if (mp
->m_dalign
)
3424 stripe_align
= mp
->m_dalign
;
3426 if (ap
->flags
& XFS_BMAPI_COWFORK
)
3427 align
= xfs_get_cowextsz_hint(ap
->ip
);
3428 else if (xfs_alloc_is_userdata(ap
->datatype
))
3429 align
= xfs_get_extsz_hint(ap
->ip
);
3431 error
= xfs_bmap_extsize_align(mp
, &ap
->got
, &ap
->prev
,
3432 align
, 0, ap
->eof
, 0, ap
->conv
,
3433 &ap
->offset
, &ap
->length
);
3439 nullfb
= *ap
->firstblock
== NULLFSBLOCK
;
3440 fb_agno
= nullfb
? NULLAGNUMBER
: XFS_FSB_TO_AGNO(mp
, *ap
->firstblock
);
3442 if (xfs_alloc_is_userdata(ap
->datatype
) &&
3443 xfs_inode_is_filestream(ap
->ip
)) {
3444 ag
= xfs_filestream_lookup_ag(ap
->ip
);
3445 ag
= (ag
!= NULLAGNUMBER
) ? ag
: 0;
3446 ap
->blkno
= XFS_AGB_TO_FSB(mp
, ag
, 0);
3448 ap
->blkno
= XFS_INO_TO_FSB(mp
, ap
->ip
->i_ino
);
3451 ap
->blkno
= *ap
->firstblock
;
3453 xfs_bmap_adjacent(ap
);
3456 * If allowed, use ap->blkno; otherwise must use firstblock since
3457 * it's in the right allocation group.
3459 if (nullfb
|| XFS_FSB_TO_AGNO(mp
, ap
->blkno
) == fb_agno
)
3462 ap
->blkno
= *ap
->firstblock
;
3464 * Normal allocation, done through xfs_alloc_vextent.
3466 tryagain
= isaligned
= 0;
3467 memset(&args
, 0, sizeof(args
));
3470 args
.fsbno
= ap
->blkno
;
3471 xfs_rmap_skip_owner_update(&args
.oinfo
);
3473 /* Trim the allocation back to the maximum an AG can fit. */
3474 args
.maxlen
= MIN(ap
->length
, mp
->m_ag_max_usable
);
3475 args
.firstblock
= *ap
->firstblock
;
3479 * Search for an allocation group with a single extent large
3480 * enough for the request. If one isn't found, then adjust
3481 * the minimum allocation size to the largest space found.
3483 if (xfs_alloc_is_userdata(ap
->datatype
) &&
3484 xfs_inode_is_filestream(ap
->ip
))
3485 error
= xfs_bmap_btalloc_filestreams(ap
, &args
, &blen
);
3487 error
= xfs_bmap_btalloc_nullfb(ap
, &args
, &blen
);
3490 } else if (ap
->dfops
->dop_low
) {
3491 if (xfs_inode_is_filestream(ap
->ip
))
3492 args
.type
= XFS_ALLOCTYPE_FIRST_AG
;
3494 args
.type
= XFS_ALLOCTYPE_START_BNO
;
3495 args
.total
= args
.minlen
= ap
->minlen
;
3497 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
3498 args
.total
= ap
->total
;
3499 args
.minlen
= ap
->minlen
;
3501 /* apply extent size hints if obtained earlier */
3504 if ((args
.mod
= (xfs_extlen_t
)do_mod(ap
->offset
, args
.prod
)))
3505 args
.mod
= (xfs_extlen_t
)(args
.prod
- args
.mod
);
3506 } else if (mp
->m_sb
.sb_blocksize
>= PAGE_SIZE
) {
3510 args
.prod
= PAGE_SIZE
>> mp
->m_sb
.sb_blocklog
;
3511 if ((args
.mod
= (xfs_extlen_t
)(do_mod(ap
->offset
, args
.prod
))))
3512 args
.mod
= (xfs_extlen_t
)(args
.prod
- args
.mod
);
3515 * If we are not low on available data blocks, and the
3516 * underlying logical volume manager is a stripe, and
3517 * the file offset is zero then try to allocate data
3518 * blocks on stripe unit boundary.
3519 * NOTE: ap->aeof is only set if the allocation length
3520 * is >= the stripe unit and the allocation offset is
3521 * at the end of file.
3523 if (!ap
->dfops
->dop_low
&& ap
->aeof
) {
3525 args
.alignment
= stripe_align
;
3529 * Adjust for alignment
3531 if (blen
> args
.alignment
&& blen
<= args
.maxlen
)
3532 args
.minlen
= blen
- args
.alignment
;
3533 args
.minalignslop
= 0;
3536 * First try an exact bno allocation.
3537 * If it fails then do a near or start bno
3538 * allocation with alignment turned on.
3542 args
.type
= XFS_ALLOCTYPE_THIS_BNO
;
3545 * Compute the minlen+alignment for the
3546 * next case. Set slop so that the value
3547 * of minlen+alignment+slop doesn't go up
3548 * between the calls.
3550 if (blen
> stripe_align
&& blen
<= args
.maxlen
)
3551 nextminlen
= blen
- stripe_align
;
3553 nextminlen
= args
.minlen
;
3554 if (nextminlen
+ stripe_align
> args
.minlen
+ 1)
3556 nextminlen
+ stripe_align
-
3559 args
.minalignslop
= 0;
3563 args
.minalignslop
= 0;
3565 args
.minleft
= ap
->minleft
;
3566 args
.wasdel
= ap
->wasdel
;
3567 args
.resv
= XFS_AG_RESV_NONE
;
3568 args
.datatype
= ap
->datatype
;
3569 if (ap
->datatype
& XFS_ALLOC_USERDATA_ZERO
)
3572 error
= xfs_alloc_vextent(&args
);
3576 if (tryagain
&& args
.fsbno
== NULLFSBLOCK
) {
3578 * Exact allocation failed. Now try with alignment
3582 args
.fsbno
= ap
->blkno
;
3583 args
.alignment
= stripe_align
;
3584 args
.minlen
= nextminlen
;
3585 args
.minalignslop
= 0;
3587 if ((error
= xfs_alloc_vextent(&args
)))
3590 if (isaligned
&& args
.fsbno
== NULLFSBLOCK
) {
3592 * allocation failed, so turn off alignment and
3596 args
.fsbno
= ap
->blkno
;
3598 if ((error
= xfs_alloc_vextent(&args
)))
3601 if (args
.fsbno
== NULLFSBLOCK
&& nullfb
&&
3602 args
.minlen
> ap
->minlen
) {
3603 args
.minlen
= ap
->minlen
;
3604 args
.type
= XFS_ALLOCTYPE_START_BNO
;
3605 args
.fsbno
= ap
->blkno
;
3606 if ((error
= xfs_alloc_vextent(&args
)))
3609 if (args
.fsbno
== NULLFSBLOCK
&& nullfb
) {
3611 args
.type
= XFS_ALLOCTYPE_FIRST_AG
;
3612 args
.total
= ap
->minlen
;
3613 if ((error
= xfs_alloc_vextent(&args
)))
3615 ap
->dfops
->dop_low
= true;
3617 if (args
.fsbno
!= NULLFSBLOCK
) {
3619 * check the allocation happened at the same or higher AG than
3620 * the first block that was allocated.
3622 ASSERT(*ap
->firstblock
== NULLFSBLOCK
||
3623 XFS_FSB_TO_AGNO(mp
, *ap
->firstblock
) <=
3624 XFS_FSB_TO_AGNO(mp
, args
.fsbno
));
3626 ap
->blkno
= args
.fsbno
;
3627 if (*ap
->firstblock
== NULLFSBLOCK
)
3628 *ap
->firstblock
= args
.fsbno
;
3629 ASSERT(nullfb
|| fb_agno
<= args
.agno
);
3630 ap
->length
= args
.len
;
3632 * If the extent size hint is active, we tried to round the
3633 * caller's allocation request offset down to extsz and the
3634 * length up to another extsz boundary. If we found a free
3635 * extent we mapped it in starting at this new offset. If the
3636 * newly mapped space isn't long enough to cover any of the
3637 * range of offsets that was originally requested, move the
3638 * mapping up so that we can fill as much of the caller's
3639 * original request as possible. Free space is apparently
3640 * very fragmented so we're unlikely to be able to satisfy the
3643 if (ap
->length
<= orig_length
)
3644 ap
->offset
= orig_offset
;
3645 else if (ap
->offset
+ ap
->length
< orig_offset
+ orig_length
)
3646 ap
->offset
= orig_offset
+ orig_length
- ap
->length
;
3647 xfs_bmap_btalloc_accounting(ap
, &args
);
3649 ap
->blkno
= NULLFSBLOCK
;
3656 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
3657 * It figures out where to ask the underlying allocator to put the new extent.
3661 struct xfs_bmalloca
*ap
) /* bmap alloc argument struct */
3663 if (XFS_IS_REALTIME_INODE(ap
->ip
) &&
3664 xfs_alloc_is_userdata(ap
->datatype
))
3665 return xfs_bmap_rtalloc(ap
);
3666 return xfs_bmap_btalloc(ap
);
3669 /* Trim extent to fit a logical block range. */
3672 struct xfs_bmbt_irec
*irec
,
3676 xfs_fileoff_t distance
;
3677 xfs_fileoff_t end
= bno
+ len
;
3679 if (irec
->br_startoff
+ irec
->br_blockcount
<= bno
||
3680 irec
->br_startoff
>= end
) {
3681 irec
->br_blockcount
= 0;
3685 if (irec
->br_startoff
< bno
) {
3686 distance
= bno
- irec
->br_startoff
;
3687 if (isnullstartblock(irec
->br_startblock
))
3688 irec
->br_startblock
= DELAYSTARTBLOCK
;
3689 if (irec
->br_startblock
!= DELAYSTARTBLOCK
&&
3690 irec
->br_startblock
!= HOLESTARTBLOCK
)
3691 irec
->br_startblock
+= distance
;
3692 irec
->br_startoff
+= distance
;
3693 irec
->br_blockcount
-= distance
;
3696 if (end
< irec
->br_startoff
+ irec
->br_blockcount
) {
3697 distance
= irec
->br_startoff
+ irec
->br_blockcount
- end
;
3698 irec
->br_blockcount
-= distance
;
3702 /* trim extent to within eof */
3704 xfs_trim_extent_eof(
3705 struct xfs_bmbt_irec
*irec
,
3706 struct xfs_inode
*ip
)
3709 xfs_trim_extent(irec
, 0, XFS_B_TO_FSB(ip
->i_mount
,
3710 i_size_read(VFS_I(ip
))));
3714 * Trim the returned map to the required bounds
3718 struct xfs_bmbt_irec
*mval
,
3719 struct xfs_bmbt_irec
*got
,
3727 if ((flags
& XFS_BMAPI_ENTIRE
) ||
3728 got
->br_startoff
+ got
->br_blockcount
<= obno
) {
3730 if (isnullstartblock(got
->br_startblock
))
3731 mval
->br_startblock
= DELAYSTARTBLOCK
;
3737 ASSERT((*bno
>= obno
) || (n
== 0));
3739 mval
->br_startoff
= *bno
;
3740 if (isnullstartblock(got
->br_startblock
))
3741 mval
->br_startblock
= DELAYSTARTBLOCK
;
3743 mval
->br_startblock
= got
->br_startblock
+
3744 (*bno
- got
->br_startoff
);
3746 * Return the minimum of what we got and what we asked for for
3747 * the length. We can use the len variable here because it is
3748 * modified below and we could have been there before coming
3749 * here if the first part of the allocation didn't overlap what
3752 mval
->br_blockcount
= XFS_FILBLKS_MIN(end
- *bno
,
3753 got
->br_blockcount
- (*bno
- got
->br_startoff
));
3754 mval
->br_state
= got
->br_state
;
3755 ASSERT(mval
->br_blockcount
<= len
);
3760 * Update and validate the extent map to return
3763 xfs_bmapi_update_map(
3764 struct xfs_bmbt_irec
**map
,
3772 xfs_bmbt_irec_t
*mval
= *map
;
3774 ASSERT((flags
& XFS_BMAPI_ENTIRE
) ||
3775 ((mval
->br_startoff
+ mval
->br_blockcount
) <= end
));
3776 ASSERT((flags
& XFS_BMAPI_ENTIRE
) || (mval
->br_blockcount
<= *len
) ||
3777 (mval
->br_startoff
< obno
));
3779 *bno
= mval
->br_startoff
+ mval
->br_blockcount
;
3781 if (*n
> 0 && mval
->br_startoff
== mval
[-1].br_startoff
) {
3782 /* update previous map with new information */
3783 ASSERT(mval
->br_startblock
== mval
[-1].br_startblock
);
3784 ASSERT(mval
->br_blockcount
> mval
[-1].br_blockcount
);
3785 ASSERT(mval
->br_state
== mval
[-1].br_state
);
3786 mval
[-1].br_blockcount
= mval
->br_blockcount
;
3787 mval
[-1].br_state
= mval
->br_state
;
3788 } else if (*n
> 0 && mval
->br_startblock
!= DELAYSTARTBLOCK
&&
3789 mval
[-1].br_startblock
!= DELAYSTARTBLOCK
&&
3790 mval
[-1].br_startblock
!= HOLESTARTBLOCK
&&
3791 mval
->br_startblock
== mval
[-1].br_startblock
+
3792 mval
[-1].br_blockcount
&&
3793 ((flags
& XFS_BMAPI_IGSTATE
) ||
3794 mval
[-1].br_state
== mval
->br_state
)) {
3795 ASSERT(mval
->br_startoff
==
3796 mval
[-1].br_startoff
+ mval
[-1].br_blockcount
);
3797 mval
[-1].br_blockcount
+= mval
->br_blockcount
;
3798 } else if (*n
> 0 &&
3799 mval
->br_startblock
== DELAYSTARTBLOCK
&&
3800 mval
[-1].br_startblock
== DELAYSTARTBLOCK
&&
3801 mval
->br_startoff
==
3802 mval
[-1].br_startoff
+ mval
[-1].br_blockcount
) {
3803 mval
[-1].br_blockcount
+= mval
->br_blockcount
;
3804 mval
[-1].br_state
= mval
->br_state
;
3805 } else if (!((*n
== 0) &&
3806 ((mval
->br_startoff
+ mval
->br_blockcount
) <=
3815 * Map file blocks to filesystem blocks without allocation.
3819 struct xfs_inode
*ip
,
3822 struct xfs_bmbt_irec
*mval
,
3826 struct xfs_mount
*mp
= ip
->i_mount
;
3827 struct xfs_ifork
*ifp
;
3828 struct xfs_bmbt_irec got
;
3831 struct xfs_iext_cursor icur
;
3835 int whichfork
= xfs_bmapi_whichfork(flags
);
3838 ASSERT(!(flags
& ~(XFS_BMAPI_ATTRFORK
|XFS_BMAPI_ENTIRE
|
3839 XFS_BMAPI_IGSTATE
|XFS_BMAPI_COWFORK
)));
3840 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_SHARED
|XFS_ILOCK_EXCL
));
3842 if (unlikely(XFS_TEST_ERROR(
3843 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
3844 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
3845 mp
, XFS_ERRTAG_BMAPIFORMAT
))) {
3846 XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW
, mp
);
3847 return -EFSCORRUPTED
;
3850 if (XFS_FORCED_SHUTDOWN(mp
))
3853 XFS_STATS_INC(mp
, xs_blk_mapr
);
3855 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
3857 /* No CoW fork? Return a hole. */
3858 if (whichfork
== XFS_COW_FORK
&& !ifp
) {
3859 mval
->br_startoff
= bno
;
3860 mval
->br_startblock
= HOLESTARTBLOCK
;
3861 mval
->br_blockcount
= len
;
3862 mval
->br_state
= XFS_EXT_NORM
;
3867 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
3868 error
= xfs_iread_extents(NULL
, ip
, whichfork
);
3873 if (!xfs_iext_lookup_extent(ip
, ifp
, bno
, &icur
, &got
))
3878 while (bno
< end
&& n
< *nmap
) {
3879 /* Reading past eof, act as though there's a hole up to end. */
3881 got
.br_startoff
= end
;
3882 if (got
.br_startoff
> bno
) {
3883 /* Reading in a hole. */
3884 mval
->br_startoff
= bno
;
3885 mval
->br_startblock
= HOLESTARTBLOCK
;
3886 mval
->br_blockcount
=
3887 XFS_FILBLKS_MIN(len
, got
.br_startoff
- bno
);
3888 mval
->br_state
= XFS_EXT_NORM
;
3889 bno
+= mval
->br_blockcount
;
3890 len
-= mval
->br_blockcount
;
3896 /* set up the extent map to return. */
3897 xfs_bmapi_trim_map(mval
, &got
, &bno
, len
, obno
, end
, n
, flags
);
3898 xfs_bmapi_update_map(&mval
, &bno
, &len
, obno
, end
, &n
, flags
);
3900 /* If we're done, stop now. */
3901 if (bno
>= end
|| n
>= *nmap
)
3904 /* Else go on to the next record. */
3905 if (!xfs_iext_next_extent(ifp
, &icur
, &got
))
3913 * Add a delayed allocation extent to an inode. Blocks are reserved from the
3914 * global pool and the extent inserted into the inode in-core extent tree.
3916 * On entry, got refers to the first extent beyond the offset of the extent to
3917 * allocate or eof is specified if no such extent exists. On return, got refers
3918 * to the extent record that was inserted to the inode fork.
3920 * Note that the allocated extent may have been merged with contiguous extents
3921 * during insertion into the inode fork. Thus, got does not reflect the current
3922 * state of the inode fork on return. If necessary, the caller can use lastx to
3923 * look up the updated record in the inode fork.
3926 xfs_bmapi_reserve_delalloc(
3927 struct xfs_inode
*ip
,
3931 xfs_filblks_t prealloc
,
3932 struct xfs_bmbt_irec
*got
,
3933 struct xfs_iext_cursor
*icur
,
3936 struct xfs_mount
*mp
= ip
->i_mount
;
3937 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
3939 xfs_extlen_t indlen
;
3941 xfs_fileoff_t aoff
= off
;
3944 * Cap the alloc length. Keep track of prealloc so we know whether to
3945 * tag the inode before we return.
3947 alen
= XFS_FILBLKS_MIN(len
+ prealloc
, MAXEXTLEN
);
3949 alen
= XFS_FILBLKS_MIN(alen
, got
->br_startoff
- aoff
);
3950 if (prealloc
&& alen
>= len
)
3951 prealloc
= alen
- len
;
3953 /* Figure out the extent size, adjust alen */
3954 if (whichfork
== XFS_COW_FORK
) {
3955 struct xfs_bmbt_irec prev
;
3956 xfs_extlen_t extsz
= xfs_get_cowextsz_hint(ip
);
3958 if (!xfs_iext_peek_prev_extent(ifp
, icur
, &prev
))
3959 prev
.br_startoff
= NULLFILEOFF
;
3961 error
= xfs_bmap_extsize_align(mp
, got
, &prev
, extsz
, 0, eof
,
3962 1, 0, &aoff
, &alen
);
3967 * Make a transaction-less quota reservation for delayed allocation
3968 * blocks. This number gets adjusted later. We return if we haven't
3969 * allocated blocks already inside this loop.
3971 error
= xfs_trans_reserve_quota_nblks(NULL
, ip
, (long)alen
, 0,
3972 XFS_QMOPT_RES_REGBLKS
);
3977 * Split changing sb for alen and indlen since they could be coming
3978 * from different places.
3980 indlen
= (xfs_extlen_t
)xfs_bmap_worst_indlen(ip
, alen
);
3983 error
= xfs_mod_fdblocks(mp
, -((int64_t)alen
), false);
3985 goto out_unreserve_quota
;
3987 error
= xfs_mod_fdblocks(mp
, -((int64_t)indlen
), false);
3989 goto out_unreserve_blocks
;
3992 ip
->i_delayed_blks
+= alen
;
3994 got
->br_startoff
= aoff
;
3995 got
->br_startblock
= nullstartblock(indlen
);
3996 got
->br_blockcount
= alen
;
3997 got
->br_state
= XFS_EXT_NORM
;
3999 xfs_bmap_add_extent_hole_delay(ip
, whichfork
, icur
, got
);
4002 * Tag the inode if blocks were preallocated. Note that COW fork
4003 * preallocation can occur at the start or end of the extent, even when
4004 * prealloc == 0, so we must also check the aligned offset and length.
4006 if (whichfork
== XFS_DATA_FORK
&& prealloc
)
4007 xfs_inode_set_eofblocks_tag(ip
);
4008 if (whichfork
== XFS_COW_FORK
&& (prealloc
|| aoff
< off
|| alen
> len
))
4009 xfs_inode_set_cowblocks_tag(ip
);
4013 out_unreserve_blocks
:
4014 xfs_mod_fdblocks(mp
, alen
, false);
4015 out_unreserve_quota
:
4016 if (XFS_IS_QUOTA_ON(mp
))
4017 xfs_trans_unreserve_quota_nblks(NULL
, ip
, (long)alen
, 0,
4018 XFS_QMOPT_RES_REGBLKS
);
4024 struct xfs_bmalloca
*bma
)
4026 struct xfs_mount
*mp
= bma
->ip
->i_mount
;
4027 int whichfork
= xfs_bmapi_whichfork(bma
->flags
);
4028 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
4029 int tmp_logflags
= 0;
4032 ASSERT(bma
->length
> 0);
4035 * For the wasdelay case, we could also just allocate the stuff asked
4036 * for in this bmap call but that wouldn't be as good.
4039 bma
->length
= (xfs_extlen_t
)bma
->got
.br_blockcount
;
4040 bma
->offset
= bma
->got
.br_startoff
;
4041 xfs_iext_peek_prev_extent(ifp
, &bma
->icur
, &bma
->prev
);
4043 bma
->length
= XFS_FILBLKS_MIN(bma
->length
, MAXEXTLEN
);
4045 bma
->length
= XFS_FILBLKS_MIN(bma
->length
,
4046 bma
->got
.br_startoff
- bma
->offset
);
4050 * Set the data type being allocated. For the data fork, the first data
4051 * in the file is treated differently to all other allocations. For the
4052 * attribute fork, we only need to ensure the allocated range is not on
4055 if (!(bma
->flags
& XFS_BMAPI_METADATA
)) {
4056 bma
->datatype
= XFS_ALLOC_NOBUSY
;
4057 if (whichfork
== XFS_DATA_FORK
) {
4058 if (bma
->offset
== 0)
4059 bma
->datatype
|= XFS_ALLOC_INITIAL_USER_DATA
;
4061 bma
->datatype
|= XFS_ALLOC_USERDATA
;
4063 if (bma
->flags
& XFS_BMAPI_ZERO
)
4064 bma
->datatype
|= XFS_ALLOC_USERDATA_ZERO
;
4067 bma
->minlen
= (bma
->flags
& XFS_BMAPI_CONTIG
) ? bma
->length
: 1;
4070 * Only want to do the alignment at the eof if it is userdata and
4071 * allocation length is larger than a stripe unit.
4073 if (mp
->m_dalign
&& bma
->length
>= mp
->m_dalign
&&
4074 !(bma
->flags
& XFS_BMAPI_METADATA
) && whichfork
== XFS_DATA_FORK
) {
4075 error
= xfs_bmap_isaeof(bma
, whichfork
);
4080 error
= xfs_bmap_alloc(bma
);
4085 bma
->cur
->bc_private
.b
.firstblock
= *bma
->firstblock
;
4086 if (bma
->blkno
== NULLFSBLOCK
)
4088 if ((ifp
->if_flags
& XFS_IFBROOT
) && !bma
->cur
) {
4089 bma
->cur
= xfs_bmbt_init_cursor(mp
, bma
->tp
, bma
->ip
, whichfork
);
4090 bma
->cur
->bc_private
.b
.firstblock
= *bma
->firstblock
;
4091 bma
->cur
->bc_private
.b
.dfops
= bma
->dfops
;
4094 * Bump the number of extents we've allocated
4100 bma
->cur
->bc_private
.b
.flags
=
4101 bma
->wasdel
? XFS_BTCUR_BPRV_WASDEL
: 0;
4103 bma
->got
.br_startoff
= bma
->offset
;
4104 bma
->got
.br_startblock
= bma
->blkno
;
4105 bma
->got
.br_blockcount
= bma
->length
;
4106 bma
->got
.br_state
= XFS_EXT_NORM
;
4109 * In the data fork, a wasdelay extent has been initialized, so
4110 * shouldn't be flagged as unwritten.
4112 * For the cow fork, however, we convert delalloc reservations
4113 * (extents allocated for speculative preallocation) to
4114 * allocated unwritten extents, and only convert the unwritten
4115 * extents to real extents when we're about to write the data.
4117 if ((!bma
->wasdel
|| (bma
->flags
& XFS_BMAPI_COWFORK
)) &&
4118 (bma
->flags
& XFS_BMAPI_PREALLOC
) &&
4119 xfs_sb_version_hasextflgbit(&mp
->m_sb
))
4120 bma
->got
.br_state
= XFS_EXT_UNWRITTEN
;
4123 error
= xfs_bmap_add_extent_delay_real(bma
, whichfork
);
4125 error
= xfs_bmap_add_extent_hole_real(bma
->tp
, bma
->ip
,
4126 whichfork
, &bma
->icur
, &bma
->cur
, &bma
->got
,
4127 bma
->firstblock
, bma
->dfops
, &bma
->logflags
,
4130 bma
->logflags
|= tmp_logflags
;
4135 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4136 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4137 * the neighbouring ones.
4139 xfs_iext_get_extent(ifp
, &bma
->icur
, &bma
->got
);
4141 ASSERT(bma
->got
.br_startoff
<= bma
->offset
);
4142 ASSERT(bma
->got
.br_startoff
+ bma
->got
.br_blockcount
>=
4143 bma
->offset
+ bma
->length
);
4144 ASSERT(bma
->got
.br_state
== XFS_EXT_NORM
||
4145 bma
->got
.br_state
== XFS_EXT_UNWRITTEN
);
4150 xfs_bmapi_convert_unwritten(
4151 struct xfs_bmalloca
*bma
,
4152 struct xfs_bmbt_irec
*mval
,
4156 int whichfork
= xfs_bmapi_whichfork(flags
);
4157 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
4158 int tmp_logflags
= 0;
4161 /* check if we need to do unwritten->real conversion */
4162 if (mval
->br_state
== XFS_EXT_UNWRITTEN
&&
4163 (flags
& XFS_BMAPI_PREALLOC
))
4166 /* check if we need to do real->unwritten conversion */
4167 if (mval
->br_state
== XFS_EXT_NORM
&&
4168 (flags
& (XFS_BMAPI_PREALLOC
| XFS_BMAPI_CONVERT
)) !=
4169 (XFS_BMAPI_PREALLOC
| XFS_BMAPI_CONVERT
))
4173 * Modify (by adding) the state flag, if writing.
4175 ASSERT(mval
->br_blockcount
<= len
);
4176 if ((ifp
->if_flags
& XFS_IFBROOT
) && !bma
->cur
) {
4177 bma
->cur
= xfs_bmbt_init_cursor(bma
->ip
->i_mount
, bma
->tp
,
4178 bma
->ip
, whichfork
);
4179 bma
->cur
->bc_private
.b
.firstblock
= *bma
->firstblock
;
4180 bma
->cur
->bc_private
.b
.dfops
= bma
->dfops
;
4182 mval
->br_state
= (mval
->br_state
== XFS_EXT_UNWRITTEN
)
4183 ? XFS_EXT_NORM
: XFS_EXT_UNWRITTEN
;
4186 * Before insertion into the bmbt, zero the range being converted
4189 if (flags
& XFS_BMAPI_ZERO
) {
4190 error
= xfs_zero_extent(bma
->ip
, mval
->br_startblock
,
4191 mval
->br_blockcount
);
4196 error
= xfs_bmap_add_extent_unwritten_real(bma
->tp
, bma
->ip
, whichfork
,
4197 &bma
->icur
, &bma
->cur
, mval
, bma
->firstblock
,
4198 bma
->dfops
, &tmp_logflags
);
4200 * Log the inode core unconditionally in the unwritten extent conversion
4201 * path because the conversion might not have done so (e.g., if the
4202 * extent count hasn't changed). We need to make sure the inode is dirty
4203 * in the transaction for the sake of fsync(), even if nothing has
4204 * changed, because fsync() will not force the log for this transaction
4205 * unless it sees the inode pinned.
4207 * Note: If we're only converting cow fork extents, there aren't
4208 * any on-disk updates to make, so we don't need to log anything.
4210 if (whichfork
!= XFS_COW_FORK
)
4211 bma
->logflags
|= tmp_logflags
| XFS_ILOG_CORE
;
4216 * Update our extent pointer, given that
4217 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4218 * of the neighbouring ones.
4220 xfs_iext_get_extent(ifp
, &bma
->icur
, &bma
->got
);
4223 * We may have combined previously unwritten space with written space,
4224 * so generate another request.
4226 if (mval
->br_blockcount
< len
)
4232 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4233 * extent state if necessary. Details behaviour is controlled by the flags
4234 * parameter. Only allocates blocks from a single allocation group, to avoid
4237 * The returned value in "firstblock" from the first call in a transaction
4238 * must be remembered and presented to subsequent calls in "firstblock".
4239 * An upper bound for the number of blocks to be allocated is supplied to
4240 * the first call in "total"; if no allocation group has that many free
4241 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4245 struct xfs_trans
*tp
, /* transaction pointer */
4246 struct xfs_inode
*ip
, /* incore inode */
4247 xfs_fileoff_t bno
, /* starting file offs. mapped */
4248 xfs_filblks_t len
, /* length to map in file */
4249 int flags
, /* XFS_BMAPI_... */
4250 xfs_fsblock_t
*firstblock
, /* first allocated block
4251 controls a.g. for allocs */
4252 xfs_extlen_t total
, /* total blocks needed */
4253 struct xfs_bmbt_irec
*mval
, /* output: map values */
4254 int *nmap
, /* i/o: mval size/count */
4255 struct xfs_defer_ops
*dfops
) /* i/o: list extents to free */
4257 struct xfs_mount
*mp
= ip
->i_mount
;
4258 struct xfs_ifork
*ifp
;
4259 struct xfs_bmalloca bma
= { NULL
}; /* args for xfs_bmap_alloc */
4260 xfs_fileoff_t end
; /* end of mapped file region */
4261 bool eof
= false; /* after the end of extents */
4262 int error
; /* error return */
4263 int n
; /* current extent index */
4264 xfs_fileoff_t obno
; /* old block number (offset) */
4265 int whichfork
; /* data or attr fork */
4268 xfs_fileoff_t orig_bno
; /* original block number value */
4269 int orig_flags
; /* original flags arg value */
4270 xfs_filblks_t orig_len
; /* original value of len arg */
4271 struct xfs_bmbt_irec
*orig_mval
; /* original value of mval */
4272 int orig_nmap
; /* original value of *nmap */
4280 whichfork
= xfs_bmapi_whichfork(flags
);
4283 ASSERT(*nmap
<= XFS_BMAP_MAX_NMAP
);
4284 ASSERT(!(flags
& XFS_BMAPI_IGSTATE
));
4285 ASSERT(tp
!= NULL
||
4286 (flags
& (XFS_BMAPI_CONVERT
| XFS_BMAPI_COWFORK
)) ==
4287 (XFS_BMAPI_CONVERT
| XFS_BMAPI_COWFORK
));
4289 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_LOCAL
);
4290 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
4291 ASSERT(!(flags
& XFS_BMAPI_REMAP
));
4293 /* zeroing is for currently only for data extents, not metadata */
4294 ASSERT((flags
& (XFS_BMAPI_METADATA
| XFS_BMAPI_ZERO
)) !=
4295 (XFS_BMAPI_METADATA
| XFS_BMAPI_ZERO
));
4297 * we can allocate unwritten extents or pre-zero allocated blocks,
4298 * but it makes no sense to do both at once. This would result in
4299 * zeroing the unwritten extent twice, but it still being an
4300 * unwritten extent....
4302 ASSERT((flags
& (XFS_BMAPI_PREALLOC
| XFS_BMAPI_ZERO
)) !=
4303 (XFS_BMAPI_PREALLOC
| XFS_BMAPI_ZERO
));
4305 if (unlikely(XFS_TEST_ERROR(
4306 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
4307 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
4308 mp
, XFS_ERRTAG_BMAPIFORMAT
))) {
4309 XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW
, mp
);
4310 return -EFSCORRUPTED
;
4313 if (XFS_FORCED_SHUTDOWN(mp
))
4316 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4318 XFS_STATS_INC(mp
, xs_blk_mapw
);
4320 if (*firstblock
== NULLFSBLOCK
) {
4321 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
)
4322 bma
.minleft
= be16_to_cpu(ifp
->if_broot
->bb_level
) + 1;
4329 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
4330 error
= xfs_iread_extents(tp
, ip
, whichfork
);
4339 if (!xfs_iext_lookup_extent(ip
, ifp
, bno
, &bma
.icur
, &bma
.got
))
4341 if (!xfs_iext_peek_prev_extent(ifp
, &bma
.icur
, &bma
.prev
))
4342 bma
.prev
.br_startoff
= NULLFILEOFF
;
4348 bma
.firstblock
= firstblock
;
4350 while (bno
< end
&& n
< *nmap
) {
4351 bool need_alloc
= false, wasdelay
= false;
4353 /* in hole or beyond EOF? */
4354 if (eof
|| bma
.got
.br_startoff
> bno
) {
4356 * CoW fork conversions should /never/ hit EOF or
4357 * holes. There should always be something for us
4360 ASSERT(!((flags
& XFS_BMAPI_CONVERT
) &&
4361 (flags
& XFS_BMAPI_COWFORK
)));
4363 if (flags
& XFS_BMAPI_DELALLOC
) {
4365 * For the COW fork we can reasonably get a
4366 * request for converting an extent that races
4367 * with other threads already having converted
4368 * part of it, as there converting COW to
4369 * regular blocks is not protected using the
4372 ASSERT(flags
& XFS_BMAPI_COWFORK
);
4373 if (!(flags
& XFS_BMAPI_COWFORK
)) {
4378 if (eof
|| bno
>= end
)
4383 } else if (isnullstartblock(bma
.got
.br_startblock
)) {
4388 * First, deal with the hole before the allocated space
4389 * that we found, if any.
4391 if ((need_alloc
|| wasdelay
) &&
4392 !(flags
& XFS_BMAPI_CONVERT_ONLY
)) {
4394 bma
.conv
= !!(flags
& XFS_BMAPI_CONVERT
);
4395 bma
.wasdel
= wasdelay
;
4400 * There's a 32/64 bit type mismatch between the
4401 * allocation length request (which can be 64 bits in
4402 * length) and the bma length request, which is
4403 * xfs_extlen_t and therefore 32 bits. Hence we have to
4404 * check for 32-bit overflows and handle them here.
4406 if (len
> (xfs_filblks_t
)MAXEXTLEN
)
4407 bma
.length
= MAXEXTLEN
;
4412 ASSERT(bma
.length
> 0);
4413 error
= xfs_bmapi_allocate(&bma
);
4416 if (bma
.blkno
== NULLFSBLOCK
)
4420 * If this is a CoW allocation, record the data in
4421 * the refcount btree for orphan recovery.
4423 if (whichfork
== XFS_COW_FORK
) {
4424 error
= xfs_refcount_alloc_cow_extent(mp
, dfops
,
4425 bma
.blkno
, bma
.length
);
4431 /* Deal with the allocated space we found. */
4432 xfs_bmapi_trim_map(mval
, &bma
.got
, &bno
, len
, obno
,
4435 /* Execute unwritten extent conversion if necessary */
4436 error
= xfs_bmapi_convert_unwritten(&bma
, mval
, len
, flags
);
4437 if (error
== -EAGAIN
)
4442 /* update the extent map to return */
4443 xfs_bmapi_update_map(&mval
, &bno
, &len
, obno
, end
, &n
, flags
);
4446 * If we're done, stop now. Stop when we've allocated
4447 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4448 * the transaction may get too big.
4450 if (bno
>= end
|| n
>= *nmap
|| bma
.nallocs
>= *nmap
)
4453 /* Else go on to the next record. */
4455 if (!xfs_iext_next_extent(ifp
, &bma
.icur
, &bma
.got
))
4461 * Transform from btree to extents, give it cur.
4463 if (xfs_bmap_wants_extents(ip
, whichfork
)) {
4464 int tmp_logflags
= 0;
4467 error
= xfs_bmap_btree_to_extents(tp
, ip
, bma
.cur
,
4468 &tmp_logflags
, whichfork
);
4469 bma
.logflags
|= tmp_logflags
;
4474 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
||
4475 XFS_IFORK_NEXTENTS(ip
, whichfork
) >
4476 XFS_IFORK_MAXEXT(ip
, whichfork
));
4480 * Log everything. Do this after conversion, there's no point in
4481 * logging the extent records if we've converted to btree format.
4483 if ((bma
.logflags
& xfs_ilog_fext(whichfork
)) &&
4484 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
4485 bma
.logflags
&= ~xfs_ilog_fext(whichfork
);
4486 else if ((bma
.logflags
& xfs_ilog_fbroot(whichfork
)) &&
4487 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)
4488 bma
.logflags
&= ~xfs_ilog_fbroot(whichfork
);
4490 * Log whatever the flags say, even if error. Otherwise we might miss
4491 * detecting a case where the data is changed, there's an error,
4492 * and it's not logged so we don't shutdown when we should.
4495 xfs_trans_log_inode(tp
, ip
, bma
.logflags
);
4499 ASSERT(*firstblock
== NULLFSBLOCK
||
4500 XFS_FSB_TO_AGNO(mp
, *firstblock
) <=
4502 bma
.cur
->bc_private
.b
.firstblock
));
4503 *firstblock
= bma
.cur
->bc_private
.b
.firstblock
;
4505 xfs_btree_del_cursor(bma
.cur
,
4506 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
4509 xfs_bmap_validate_ret(orig_bno
, orig_len
, orig_flags
, orig_mval
,
4516 struct xfs_trans
*tp
,
4517 struct xfs_inode
*ip
,
4520 xfs_fsblock_t startblock
,
4521 struct xfs_defer_ops
*dfops
,
4524 struct xfs_mount
*mp
= ip
->i_mount
;
4525 struct xfs_ifork
*ifp
;
4526 struct xfs_btree_cur
*cur
= NULL
;
4527 xfs_fsblock_t firstblock
= NULLFSBLOCK
;
4528 struct xfs_bmbt_irec got
;
4529 struct xfs_iext_cursor icur
;
4530 int whichfork
= xfs_bmapi_whichfork(flags
);
4531 int logflags
= 0, error
;
4533 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4535 ASSERT(len
<= (xfs_filblks_t
)MAXEXTLEN
);
4536 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
4537 ASSERT(!(flags
& ~(XFS_BMAPI_ATTRFORK
| XFS_BMAPI_PREALLOC
|
4538 XFS_BMAPI_NORMAP
)));
4539 ASSERT((flags
& (XFS_BMAPI_ATTRFORK
| XFS_BMAPI_PREALLOC
)) !=
4540 (XFS_BMAPI_ATTRFORK
| XFS_BMAPI_PREALLOC
));
4542 if (unlikely(XFS_TEST_ERROR(
4543 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
4544 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
4545 mp
, XFS_ERRTAG_BMAPIFORMAT
))) {
4546 XFS_ERROR_REPORT("xfs_bmapi_remap", XFS_ERRLEVEL_LOW
, mp
);
4547 return -EFSCORRUPTED
;
4550 if (XFS_FORCED_SHUTDOWN(mp
))
4553 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
4554 error
= xfs_iread_extents(tp
, ip
, whichfork
);
4559 if (xfs_iext_lookup_extent(ip
, ifp
, bno
, &icur
, &got
)) {
4560 /* make sure we only reflink into a hole. */
4561 ASSERT(got
.br_startoff
> bno
);
4562 ASSERT(got
.br_startoff
- bno
>= len
);
4565 ip
->i_d
.di_nblocks
+= len
;
4566 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
4568 if (ifp
->if_flags
& XFS_IFBROOT
) {
4569 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
4570 cur
->bc_private
.b
.firstblock
= firstblock
;
4571 cur
->bc_private
.b
.dfops
= dfops
;
4572 cur
->bc_private
.b
.flags
= 0;
4575 got
.br_startoff
= bno
;
4576 got
.br_startblock
= startblock
;
4577 got
.br_blockcount
= len
;
4578 if (flags
& XFS_BMAPI_PREALLOC
)
4579 got
.br_state
= XFS_EXT_UNWRITTEN
;
4581 got
.br_state
= XFS_EXT_NORM
;
4583 error
= xfs_bmap_add_extent_hole_real(tp
, ip
, whichfork
, &icur
,
4584 &cur
, &got
, &firstblock
, dfops
, &logflags
, flags
);
4588 if (xfs_bmap_wants_extents(ip
, whichfork
)) {
4589 int tmp_logflags
= 0;
4591 error
= xfs_bmap_btree_to_extents(tp
, ip
, cur
,
4592 &tmp_logflags
, whichfork
);
4593 logflags
|= tmp_logflags
;
4597 if (ip
->i_d
.di_format
!= XFS_DINODE_FMT_EXTENTS
)
4598 logflags
&= ~XFS_ILOG_DEXT
;
4599 else if (ip
->i_d
.di_format
!= XFS_DINODE_FMT_BTREE
)
4600 logflags
&= ~XFS_ILOG_DBROOT
;
4603 xfs_trans_log_inode(tp
, ip
, logflags
);
4605 xfs_btree_del_cursor(cur
,
4606 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
4612 * When a delalloc extent is split (e.g., due to a hole punch), the original
4613 * indlen reservation must be shared across the two new extents that are left
4616 * Given the original reservation and the worst case indlen for the two new
4617 * extents (as calculated by xfs_bmap_worst_indlen()), split the original
4618 * reservation fairly across the two new extents. If necessary, steal available
4619 * blocks from a deleted extent to make up a reservation deficiency (e.g., if
4620 * ores == 1). The number of stolen blocks is returned. The availability and
4621 * subsequent accounting of stolen blocks is the responsibility of the caller.
4623 static xfs_filblks_t
4624 xfs_bmap_split_indlen(
4625 xfs_filblks_t ores
, /* original res. */
4626 xfs_filblks_t
*indlen1
, /* ext1 worst indlen */
4627 xfs_filblks_t
*indlen2
, /* ext2 worst indlen */
4628 xfs_filblks_t avail
) /* stealable blocks */
4630 xfs_filblks_t len1
= *indlen1
;
4631 xfs_filblks_t len2
= *indlen2
;
4632 xfs_filblks_t nres
= len1
+ len2
; /* new total res. */
4633 xfs_filblks_t stolen
= 0;
4634 xfs_filblks_t resfactor
;
4637 * Steal as many blocks as we can to try and satisfy the worst case
4638 * indlen for both new extents.
4640 if (ores
< nres
&& avail
)
4641 stolen
= XFS_FILBLKS_MIN(nres
- ores
, avail
);
4644 /* nothing else to do if we've satisfied the new reservation */
4649 * We can't meet the total required reservation for the two extents.
4650 * Calculate the percent of the overall shortage between both extents
4651 * and apply this percentage to each of the requested indlen values.
4652 * This distributes the shortage fairly and reduces the chances that one
4653 * of the two extents is left with nothing when extents are repeatedly
4656 resfactor
= (ores
* 100);
4657 do_div(resfactor
, nres
);
4662 ASSERT(len1
+ len2
<= ores
);
4663 ASSERT(len1
< *indlen1
&& len2
< *indlen2
);
4666 * Hand out the remainder to each extent. If one of the two reservations
4667 * is zero, we want to make sure that one gets a block first. The loop
4668 * below starts with len1, so hand len2 a block right off the bat if it
4671 ores
-= (len1
+ len2
);
4672 ASSERT((*indlen1
- len1
) + (*indlen2
- len2
) >= ores
);
4673 if (ores
&& !len2
&& *indlen2
) {
4678 if (len1
< *indlen1
) {
4684 if (len2
< *indlen2
) {
4697 xfs_bmap_del_extent_delay(
4698 struct xfs_inode
*ip
,
4700 struct xfs_iext_cursor
*icur
,
4701 struct xfs_bmbt_irec
*got
,
4702 struct xfs_bmbt_irec
*del
)
4704 struct xfs_mount
*mp
= ip
->i_mount
;
4705 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4706 struct xfs_bmbt_irec
new;
4707 int64_t da_old
, da_new
, da_diff
= 0;
4708 xfs_fileoff_t del_endoff
, got_endoff
;
4709 xfs_filblks_t got_indlen
, new_indlen
, stolen
;
4710 int state
= xfs_bmap_fork_to_state(whichfork
);
4714 XFS_STATS_INC(mp
, xs_del_exlist
);
4716 isrt
= (whichfork
== XFS_DATA_FORK
) && XFS_IS_REALTIME_INODE(ip
);
4717 del_endoff
= del
->br_startoff
+ del
->br_blockcount
;
4718 got_endoff
= got
->br_startoff
+ got
->br_blockcount
;
4719 da_old
= startblockval(got
->br_startblock
);
4722 ASSERT(del
->br_blockcount
> 0);
4723 ASSERT(got
->br_startoff
<= del
->br_startoff
);
4724 ASSERT(got_endoff
>= del_endoff
);
4727 uint64_t rtexts
= XFS_FSB_TO_B(mp
, del
->br_blockcount
);
4729 do_div(rtexts
, mp
->m_sb
.sb_rextsize
);
4730 xfs_mod_frextents(mp
, rtexts
);
4734 * Update the inode delalloc counter now and wait to update the
4735 * sb counters as we might have to borrow some blocks for the
4736 * indirect block accounting.
4738 error
= xfs_trans_reserve_quota_nblks(NULL
, ip
,
4739 -((long)del
->br_blockcount
), 0,
4740 isrt
? XFS_QMOPT_RES_RTBLKS
: XFS_QMOPT_RES_REGBLKS
);
4743 ip
->i_delayed_blks
-= del
->br_blockcount
;
4745 if (got
->br_startoff
== del
->br_startoff
)
4746 state
|= BMAP_LEFT_FILLING
;
4747 if (got_endoff
== del_endoff
)
4748 state
|= BMAP_RIGHT_FILLING
;
4750 switch (state
& (BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
)) {
4751 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
4753 * Matches the whole extent. Delete the entry.
4755 xfs_iext_remove(ip
, icur
, state
);
4756 xfs_iext_prev(ifp
, icur
);
4758 case BMAP_LEFT_FILLING
:
4760 * Deleting the first part of the extent.
4762 got
->br_startoff
= del_endoff
;
4763 got
->br_blockcount
-= del
->br_blockcount
;
4764 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
,
4765 got
->br_blockcount
), da_old
);
4766 got
->br_startblock
= nullstartblock((int)da_new
);
4767 xfs_iext_update_extent(ip
, state
, icur
, got
);
4769 case BMAP_RIGHT_FILLING
:
4771 * Deleting the last part of the extent.
4773 got
->br_blockcount
= got
->br_blockcount
- del
->br_blockcount
;
4774 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
,
4775 got
->br_blockcount
), da_old
);
4776 got
->br_startblock
= nullstartblock((int)da_new
);
4777 xfs_iext_update_extent(ip
, state
, icur
, got
);
4781 * Deleting the middle of the extent.
4783 * Distribute the original indlen reservation across the two new
4784 * extents. Steal blocks from the deleted extent if necessary.
4785 * Stealing blocks simply fudges the fdblocks accounting below.
4786 * Warn if either of the new indlen reservations is zero as this
4787 * can lead to delalloc problems.
4789 got
->br_blockcount
= del
->br_startoff
- got
->br_startoff
;
4790 got_indlen
= xfs_bmap_worst_indlen(ip
, got
->br_blockcount
);
4792 new.br_blockcount
= got_endoff
- del_endoff
;
4793 new_indlen
= xfs_bmap_worst_indlen(ip
, new.br_blockcount
);
4795 WARN_ON_ONCE(!got_indlen
|| !new_indlen
);
4796 stolen
= xfs_bmap_split_indlen(da_old
, &got_indlen
, &new_indlen
,
4797 del
->br_blockcount
);
4799 got
->br_startblock
= nullstartblock((int)got_indlen
);
4801 new.br_startoff
= del_endoff
;
4802 new.br_state
= got
->br_state
;
4803 new.br_startblock
= nullstartblock((int)new_indlen
);
4805 xfs_iext_update_extent(ip
, state
, icur
, got
);
4806 xfs_iext_next(ifp
, icur
);
4807 xfs_iext_insert(ip
, icur
, &new, state
);
4809 da_new
= got_indlen
+ new_indlen
- stolen
;
4810 del
->br_blockcount
-= stolen
;
4814 ASSERT(da_old
>= da_new
);
4815 da_diff
= da_old
- da_new
;
4817 da_diff
+= del
->br_blockcount
;
4819 xfs_mod_fdblocks(mp
, da_diff
, false);
4824 xfs_bmap_del_extent_cow(
4825 struct xfs_inode
*ip
,
4826 struct xfs_iext_cursor
*icur
,
4827 struct xfs_bmbt_irec
*got
,
4828 struct xfs_bmbt_irec
*del
)
4830 struct xfs_mount
*mp
= ip
->i_mount
;
4831 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, XFS_COW_FORK
);
4832 struct xfs_bmbt_irec
new;
4833 xfs_fileoff_t del_endoff
, got_endoff
;
4834 int state
= BMAP_COWFORK
;
4836 XFS_STATS_INC(mp
, xs_del_exlist
);
4838 del_endoff
= del
->br_startoff
+ del
->br_blockcount
;
4839 got_endoff
= got
->br_startoff
+ got
->br_blockcount
;
4841 ASSERT(del
->br_blockcount
> 0);
4842 ASSERT(got
->br_startoff
<= del
->br_startoff
);
4843 ASSERT(got_endoff
>= del_endoff
);
4844 ASSERT(!isnullstartblock(got
->br_startblock
));
4846 if (got
->br_startoff
== del
->br_startoff
)
4847 state
|= BMAP_LEFT_FILLING
;
4848 if (got_endoff
== del_endoff
)
4849 state
|= BMAP_RIGHT_FILLING
;
4851 switch (state
& (BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
)) {
4852 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
4854 * Matches the whole extent. Delete the entry.
4856 xfs_iext_remove(ip
, icur
, state
);
4857 xfs_iext_prev(ifp
, icur
);
4859 case BMAP_LEFT_FILLING
:
4861 * Deleting the first part of the extent.
4863 got
->br_startoff
= del_endoff
;
4864 got
->br_blockcount
-= del
->br_blockcount
;
4865 got
->br_startblock
= del
->br_startblock
+ del
->br_blockcount
;
4866 xfs_iext_update_extent(ip
, state
, icur
, got
);
4868 case BMAP_RIGHT_FILLING
:
4870 * Deleting the last part of the extent.
4872 got
->br_blockcount
-= del
->br_blockcount
;
4873 xfs_iext_update_extent(ip
, state
, icur
, got
);
4877 * Deleting the middle of the extent.
4879 got
->br_blockcount
= del
->br_startoff
- got
->br_startoff
;
4881 new.br_startoff
= del_endoff
;
4882 new.br_blockcount
= got_endoff
- del_endoff
;
4883 new.br_state
= got
->br_state
;
4884 new.br_startblock
= del
->br_startblock
+ del
->br_blockcount
;
4886 xfs_iext_update_extent(ip
, state
, icur
, got
);
4887 xfs_iext_next(ifp
, icur
);
4888 xfs_iext_insert(ip
, icur
, &new, state
);
4891 ip
->i_delayed_blks
-= del
->br_blockcount
;
4895 * Called by xfs_bmapi to update file extent records and the btree
4896 * after removing space.
4898 STATIC
int /* error */
4899 xfs_bmap_del_extent_real(
4900 xfs_inode_t
*ip
, /* incore inode pointer */
4901 xfs_trans_t
*tp
, /* current transaction pointer */
4902 struct xfs_iext_cursor
*icur
,
4903 struct xfs_defer_ops
*dfops
, /* list of extents to be freed */
4904 xfs_btree_cur_t
*cur
, /* if null, not a btree */
4905 xfs_bmbt_irec_t
*del
, /* data to remove from extents */
4906 int *logflagsp
, /* inode logging flags */
4907 int whichfork
, /* data or attr fork */
4908 int bflags
) /* bmapi flags */
4910 xfs_fsblock_t del_endblock
=0; /* first block past del */
4911 xfs_fileoff_t del_endoff
; /* first offset past del */
4912 int do_fx
; /* free extent at end of routine */
4913 int error
; /* error return value */
4914 int flags
= 0;/* inode logging flags */
4915 struct xfs_bmbt_irec got
; /* current extent entry */
4916 xfs_fileoff_t got_endoff
; /* first offset past got */
4917 int i
; /* temp state */
4918 xfs_ifork_t
*ifp
; /* inode fork pointer */
4919 xfs_mount_t
*mp
; /* mount structure */
4920 xfs_filblks_t nblks
; /* quota/sb block count */
4921 xfs_bmbt_irec_t
new; /* new record to be inserted */
4923 uint qfield
; /* quota field to update */
4924 int state
= xfs_bmap_fork_to_state(whichfork
);
4925 struct xfs_bmbt_irec old
;
4928 XFS_STATS_INC(mp
, xs_del_exlist
);
4930 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4931 ASSERT(del
->br_blockcount
> 0);
4932 xfs_iext_get_extent(ifp
, icur
, &got
);
4933 ASSERT(got
.br_startoff
<= del
->br_startoff
);
4934 del_endoff
= del
->br_startoff
+ del
->br_blockcount
;
4935 got_endoff
= got
.br_startoff
+ got
.br_blockcount
;
4936 ASSERT(got_endoff
>= del_endoff
);
4937 ASSERT(!isnullstartblock(got
.br_startblock
));
4942 * If it's the case where the directory code is running with no block
4943 * reservation, and the deleted block is in the middle of its extent,
4944 * and the resulting insert of an extent would cause transformation to
4945 * btree format, then reject it. The calling code will then swap blocks
4946 * around instead. We have to do this now, rather than waiting for the
4947 * conversion to btree format, since the transaction will be dirty then.
4949 if (tp
->t_blk_res
== 0 &&
4950 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
&&
4951 XFS_IFORK_NEXTENTS(ip
, whichfork
) >=
4952 XFS_IFORK_MAXEXT(ip
, whichfork
) &&
4953 del
->br_startoff
> got
.br_startoff
&& del_endoff
< got_endoff
)
4956 flags
= XFS_ILOG_CORE
;
4957 if (whichfork
== XFS_DATA_FORK
&& XFS_IS_REALTIME_INODE(ip
)) {
4961 ASSERT(do_mod(del
->br_blockcount
, mp
->m_sb
.sb_rextsize
) == 0);
4962 ASSERT(do_mod(del
->br_startblock
, mp
->m_sb
.sb_rextsize
) == 0);
4963 bno
= del
->br_startblock
;
4964 len
= del
->br_blockcount
;
4965 do_div(bno
, mp
->m_sb
.sb_rextsize
);
4966 do_div(len
, mp
->m_sb
.sb_rextsize
);
4967 error
= xfs_rtfree_extent(tp
, bno
, (xfs_extlen_t
)len
);
4971 nblks
= len
* mp
->m_sb
.sb_rextsize
;
4972 qfield
= XFS_TRANS_DQ_RTBCOUNT
;
4975 nblks
= del
->br_blockcount
;
4976 qfield
= XFS_TRANS_DQ_BCOUNT
;
4979 del_endblock
= del
->br_startblock
+ del
->br_blockcount
;
4981 error
= xfs_bmbt_lookup_eq(cur
, &got
, &i
);
4984 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
4987 if (got
.br_startoff
== del
->br_startoff
)
4988 state
|= BMAP_LEFT_FILLING
;
4989 if (got_endoff
== del_endoff
)
4990 state
|= BMAP_RIGHT_FILLING
;
4992 switch (state
& (BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
)) {
4993 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
4995 * Matches the whole extent. Delete the entry.
4997 xfs_iext_remove(ip
, icur
, state
);
4998 xfs_iext_prev(ifp
, icur
);
4999 XFS_IFORK_NEXT_SET(ip
, whichfork
,
5000 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
5001 flags
|= XFS_ILOG_CORE
;
5003 flags
|= xfs_ilog_fext(whichfork
);
5006 if ((error
= xfs_btree_delete(cur
, &i
)))
5008 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
5010 case BMAP_LEFT_FILLING
:
5012 * Deleting the first part of the extent.
5014 got
.br_startoff
= del_endoff
;
5015 got
.br_startblock
= del_endblock
;
5016 got
.br_blockcount
-= del
->br_blockcount
;
5017 xfs_iext_update_extent(ip
, state
, icur
, &got
);
5019 flags
|= xfs_ilog_fext(whichfork
);
5022 error
= xfs_bmbt_update(cur
, &got
);
5026 case BMAP_RIGHT_FILLING
:
5028 * Deleting the last part of the extent.
5030 got
.br_blockcount
-= del
->br_blockcount
;
5031 xfs_iext_update_extent(ip
, state
, icur
, &got
);
5033 flags
|= xfs_ilog_fext(whichfork
);
5036 error
= xfs_bmbt_update(cur
, &got
);
5042 * Deleting the middle of the extent.
5046 got
.br_blockcount
= del
->br_startoff
- got
.br_startoff
;
5047 xfs_iext_update_extent(ip
, state
, icur
, &got
);
5049 new.br_startoff
= del_endoff
;
5050 new.br_blockcount
= got_endoff
- del_endoff
;
5051 new.br_state
= got
.br_state
;
5052 new.br_startblock
= del_endblock
;
5054 flags
|= XFS_ILOG_CORE
;
5056 error
= xfs_bmbt_update(cur
, &got
);
5059 error
= xfs_btree_increment(cur
, 0, &i
);
5062 cur
->bc_rec
.b
= new;
5063 error
= xfs_btree_insert(cur
, &i
);
5064 if (error
&& error
!= -ENOSPC
)
5067 * If get no-space back from btree insert, it tried a
5068 * split, and we have a zero block reservation. Fix up
5069 * our state and return the error.
5071 if (error
== -ENOSPC
) {
5073 * Reset the cursor, don't trust it after any
5076 error
= xfs_bmbt_lookup_eq(cur
, &got
, &i
);
5079 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
5081 * Update the btree record back
5082 * to the original value.
5084 error
= xfs_bmbt_update(cur
, &old
);
5088 * Reset the extent record back
5089 * to the original value.
5091 xfs_iext_update_extent(ip
, state
, icur
, &old
);
5096 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
5098 flags
|= xfs_ilog_fext(whichfork
);
5099 XFS_IFORK_NEXT_SET(ip
, whichfork
,
5100 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
5101 xfs_iext_next(ifp
, icur
);
5102 xfs_iext_insert(ip
, icur
, &new, state
);
5106 /* remove reverse mapping */
5107 error
= xfs_rmap_unmap_extent(mp
, dfops
, ip
, whichfork
, del
);
5112 * If we need to, add to list of extents to delete.
5114 if (do_fx
&& !(bflags
& XFS_BMAPI_REMAP
)) {
5115 if (xfs_is_reflink_inode(ip
) && whichfork
== XFS_DATA_FORK
) {
5116 error
= xfs_refcount_decrease_extent(mp
, dfops
, del
);
5120 __xfs_bmap_add_free(mp
, dfops
, del
->br_startblock
,
5121 del
->br_blockcount
, NULL
,
5122 (bflags
& XFS_BMAPI_NODISCARD
) ||
5123 del
->br_state
== XFS_EXT_UNWRITTEN
);
5128 * Adjust inode # blocks in the file.
5131 ip
->i_d
.di_nblocks
-= nblks
;
5133 * Adjust quota data.
5135 if (qfield
&& !(bflags
& XFS_BMAPI_REMAP
))
5136 xfs_trans_mod_dquot_byino(tp
, ip
, qfield
, (long)-nblks
);
5144 * Unmap (remove) blocks from a file.
5145 * If nexts is nonzero then the number of extents to remove is limited to
5146 * that value. If not all extents in the block range can be removed then
5151 xfs_trans_t
*tp
, /* transaction pointer */
5152 struct xfs_inode
*ip
, /* incore inode */
5153 xfs_fileoff_t start
, /* first file offset deleted */
5154 xfs_filblks_t
*rlen
, /* i/o: amount remaining */
5155 int flags
, /* misc flags */
5156 xfs_extnum_t nexts
, /* number of extents max */
5157 xfs_fsblock_t
*firstblock
, /* first allocated block
5158 controls a.g. for allocs */
5159 struct xfs_defer_ops
*dfops
) /* i/o: deferred updates */
5161 xfs_btree_cur_t
*cur
; /* bmap btree cursor */
5162 xfs_bmbt_irec_t del
; /* extent being deleted */
5163 int error
; /* error return value */
5164 xfs_extnum_t extno
; /* extent number in list */
5165 xfs_bmbt_irec_t got
; /* current extent record */
5166 xfs_ifork_t
*ifp
; /* inode fork pointer */
5167 int isrt
; /* freeing in rt area */
5168 int logflags
; /* transaction logging flags */
5169 xfs_extlen_t mod
; /* rt extent offset */
5170 xfs_mount_t
*mp
; /* mount structure */
5171 int tmp_logflags
; /* partial logging flags */
5172 int wasdel
; /* was a delayed alloc extent */
5173 int whichfork
; /* data or attribute fork */
5175 xfs_filblks_t len
= *rlen
; /* length to unmap in file */
5176 xfs_fileoff_t max_len
;
5177 xfs_agnumber_t prev_agno
= NULLAGNUMBER
, agno
;
5179 struct xfs_iext_cursor icur
;
5182 trace_xfs_bunmap(ip
, start
, len
, flags
, _RET_IP_
);
5184 whichfork
= xfs_bmapi_whichfork(flags
);
5185 ASSERT(whichfork
!= XFS_COW_FORK
);
5186 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5188 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
5189 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)) {
5190 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW
,
5192 return -EFSCORRUPTED
;
5195 if (XFS_FORCED_SHUTDOWN(mp
))
5198 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
5203 * Guesstimate how many blocks we can unmap without running the risk of
5204 * blowing out the transaction with a mix of EFIs and reflink
5207 if (tp
&& xfs_is_reflink_inode(ip
) && whichfork
== XFS_DATA_FORK
)
5208 max_len
= min(len
, xfs_refcount_max_unmap(tp
->t_log_res
));
5212 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
5213 (error
= xfs_iread_extents(tp
, ip
, whichfork
)))
5215 if (xfs_iext_count(ifp
) == 0) {
5219 XFS_STATS_INC(mp
, xs_blk_unmap
);
5220 isrt
= (whichfork
== XFS_DATA_FORK
) && XFS_IS_REALTIME_INODE(ip
);
5223 if (!xfs_iext_lookup_extent_before(ip
, ifp
, &end
, &icur
, &got
)) {
5230 if (ifp
->if_flags
& XFS_IFBROOT
) {
5231 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
);
5232 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
5233 cur
->bc_private
.b
.firstblock
= *firstblock
;
5234 cur
->bc_private
.b
.dfops
= dfops
;
5235 cur
->bc_private
.b
.flags
= 0;
5241 * Synchronize by locking the bitmap inode.
5243 xfs_ilock(mp
->m_rbmip
, XFS_ILOCK_EXCL
|XFS_ILOCK_RTBITMAP
);
5244 xfs_trans_ijoin(tp
, mp
->m_rbmip
, XFS_ILOCK_EXCL
);
5245 xfs_ilock(mp
->m_rsumip
, XFS_ILOCK_EXCL
|XFS_ILOCK_RTSUM
);
5246 xfs_trans_ijoin(tp
, mp
->m_rsumip
, XFS_ILOCK_EXCL
);
5250 while (end
!= (xfs_fileoff_t
)-1 && end
>= start
&&
5251 (nexts
== 0 || extno
< nexts
) && max_len
> 0) {
5253 * Is the found extent after a hole in which end lives?
5254 * Just back up to the previous extent, if so.
5256 if (got
.br_startoff
> end
&&
5257 !xfs_iext_prev_extent(ifp
, &icur
, &got
)) {
5262 * Is the last block of this extent before the range
5263 * we're supposed to delete? If so, we're done.
5265 end
= XFS_FILEOFF_MIN(end
,
5266 got
.br_startoff
+ got
.br_blockcount
- 1);
5270 * Then deal with the (possibly delayed) allocated space
5274 wasdel
= isnullstartblock(del
.br_startblock
);
5277 * Make sure we don't touch multiple AGF headers out of order
5278 * in a single transaction, as that could cause AB-BA deadlocks.
5281 agno
= XFS_FSB_TO_AGNO(mp
, del
.br_startblock
);
5282 if (prev_agno
!= NULLAGNUMBER
&& prev_agno
> agno
)
5286 if (got
.br_startoff
< start
) {
5287 del
.br_startoff
= start
;
5288 del
.br_blockcount
-= start
- got
.br_startoff
;
5290 del
.br_startblock
+= start
- got
.br_startoff
;
5292 if (del
.br_startoff
+ del
.br_blockcount
> end
+ 1)
5293 del
.br_blockcount
= end
+ 1 - del
.br_startoff
;
5295 /* How much can we safely unmap? */
5296 if (max_len
< del
.br_blockcount
) {
5297 del
.br_startoff
+= del
.br_blockcount
- max_len
;
5299 del
.br_startblock
+= del
.br_blockcount
- max_len
;
5300 del
.br_blockcount
= max_len
;
5303 sum
= del
.br_startblock
+ del
.br_blockcount
;
5305 (mod
= do_mod(sum
, mp
->m_sb
.sb_rextsize
))) {
5307 * Realtime extent not lined up at the end.
5308 * The extent could have been split into written
5309 * and unwritten pieces, or we could just be
5310 * unmapping part of it. But we can't really
5311 * get rid of part of a realtime extent.
5313 if (del
.br_state
== XFS_EXT_UNWRITTEN
||
5314 !xfs_sb_version_hasextflgbit(&mp
->m_sb
)) {
5316 * This piece is unwritten, or we're not
5317 * using unwritten extents. Skip over it.
5320 end
-= mod
> del
.br_blockcount
?
5321 del
.br_blockcount
: mod
;
5322 if (end
< got
.br_startoff
&&
5323 !xfs_iext_prev_extent(ifp
, &icur
, &got
)) {
5330 * It's written, turn it unwritten.
5331 * This is better than zeroing it.
5333 ASSERT(del
.br_state
== XFS_EXT_NORM
);
5334 ASSERT(tp
->t_blk_res
> 0);
5336 * If this spans a realtime extent boundary,
5337 * chop it back to the start of the one we end at.
5339 if (del
.br_blockcount
> mod
) {
5340 del
.br_startoff
+= del
.br_blockcount
- mod
;
5341 del
.br_startblock
+= del
.br_blockcount
- mod
;
5342 del
.br_blockcount
= mod
;
5344 del
.br_state
= XFS_EXT_UNWRITTEN
;
5345 error
= xfs_bmap_add_extent_unwritten_real(tp
, ip
,
5346 whichfork
, &icur
, &cur
, &del
,
5347 firstblock
, dfops
, &logflags
);
5352 if (isrt
&& (mod
= do_mod(del
.br_startblock
, mp
->m_sb
.sb_rextsize
))) {
5354 * Realtime extent is lined up at the end but not
5355 * at the front. We'll get rid of full extents if
5358 mod
= mp
->m_sb
.sb_rextsize
- mod
;
5359 if (del
.br_blockcount
> mod
) {
5360 del
.br_blockcount
-= mod
;
5361 del
.br_startoff
+= mod
;
5362 del
.br_startblock
+= mod
;
5363 } else if ((del
.br_startoff
== start
&&
5364 (del
.br_state
== XFS_EXT_UNWRITTEN
||
5365 tp
->t_blk_res
== 0)) ||
5366 !xfs_sb_version_hasextflgbit(&mp
->m_sb
)) {
5368 * Can't make it unwritten. There isn't
5369 * a full extent here so just skip it.
5371 ASSERT(end
>= del
.br_blockcount
);
5372 end
-= del
.br_blockcount
;
5373 if (got
.br_startoff
> end
&&
5374 !xfs_iext_prev_extent(ifp
, &icur
, &got
)) {
5379 } else if (del
.br_state
== XFS_EXT_UNWRITTEN
) {
5380 struct xfs_bmbt_irec prev
;
5383 * This one is already unwritten.
5384 * It must have a written left neighbor.
5385 * Unwrite the killed part of that one and
5388 if (!xfs_iext_prev_extent(ifp
, &icur
, &prev
))
5390 ASSERT(prev
.br_state
== XFS_EXT_NORM
);
5391 ASSERT(!isnullstartblock(prev
.br_startblock
));
5392 ASSERT(del
.br_startblock
==
5393 prev
.br_startblock
+ prev
.br_blockcount
);
5394 if (prev
.br_startoff
< start
) {
5395 mod
= start
- prev
.br_startoff
;
5396 prev
.br_blockcount
-= mod
;
5397 prev
.br_startblock
+= mod
;
5398 prev
.br_startoff
= start
;
5400 prev
.br_state
= XFS_EXT_UNWRITTEN
;
5401 error
= xfs_bmap_add_extent_unwritten_real(tp
,
5402 ip
, whichfork
, &icur
, &cur
,
5403 &prev
, firstblock
, dfops
,
5409 ASSERT(del
.br_state
== XFS_EXT_NORM
);
5410 del
.br_state
= XFS_EXT_UNWRITTEN
;
5411 error
= xfs_bmap_add_extent_unwritten_real(tp
,
5412 ip
, whichfork
, &icur
, &cur
,
5413 &del
, firstblock
, dfops
,
5422 error
= xfs_bmap_del_extent_delay(ip
, whichfork
, &icur
,
5425 error
= xfs_bmap_del_extent_real(ip
, tp
, &icur
, dfops
,
5426 cur
, &del
, &tmp_logflags
, whichfork
,
5428 logflags
|= tmp_logflags
;
5434 max_len
-= del
.br_blockcount
;
5435 end
= del
.br_startoff
- 1;
5438 * If not done go on to the next (previous) record.
5440 if (end
!= (xfs_fileoff_t
)-1 && end
>= start
) {
5441 if (!xfs_iext_get_extent(ifp
, &icur
, &got
) ||
5442 (got
.br_startoff
> end
&&
5443 !xfs_iext_prev_extent(ifp
, &icur
, &got
))) {
5450 if (done
|| end
== (xfs_fileoff_t
)-1 || end
< start
)
5453 *rlen
= end
- start
+ 1;
5456 * Convert to a btree if necessary.
5458 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
5459 ASSERT(cur
== NULL
);
5460 error
= xfs_bmap_extents_to_btree(tp
, ip
, firstblock
, dfops
,
5461 &cur
, 0, &tmp_logflags
, whichfork
);
5462 logflags
|= tmp_logflags
;
5467 * transform from btree to extents, give it cur
5469 else if (xfs_bmap_wants_extents(ip
, whichfork
)) {
5470 ASSERT(cur
!= NULL
);
5471 error
= xfs_bmap_btree_to_extents(tp
, ip
, cur
, &tmp_logflags
,
5473 logflags
|= tmp_logflags
;
5478 * transform from extents to local?
5483 * Log everything. Do this after conversion, there's no point in
5484 * logging the extent records if we've converted to btree format.
5486 if ((logflags
& xfs_ilog_fext(whichfork
)) &&
5487 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
5488 logflags
&= ~xfs_ilog_fext(whichfork
);
5489 else if ((logflags
& xfs_ilog_fbroot(whichfork
)) &&
5490 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)
5491 logflags
&= ~xfs_ilog_fbroot(whichfork
);
5493 * Log inode even in the error case, if the transaction
5494 * is dirty we'll need to shut down the filesystem.
5497 xfs_trans_log_inode(tp
, ip
, logflags
);
5500 *firstblock
= cur
->bc_private
.b
.firstblock
;
5501 cur
->bc_private
.b
.allocated
= 0;
5503 xfs_btree_del_cursor(cur
,
5504 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
5509 /* Unmap a range of a file. */
5513 struct xfs_inode
*ip
,
5518 xfs_fsblock_t
*firstblock
,
5519 struct xfs_defer_ops
*dfops
,
5524 error
= __xfs_bunmapi(tp
, ip
, bno
, &len
, flags
, nexts
, firstblock
,
5531 * Determine whether an extent shift can be accomplished by a merge with the
5532 * extent that precedes the target hole of the shift.
5536 struct xfs_bmbt_irec
*left
, /* preceding extent */
5537 struct xfs_bmbt_irec
*got
, /* current extent to shift */
5538 xfs_fileoff_t shift
) /* shift fsb */
5540 xfs_fileoff_t startoff
;
5542 startoff
= got
->br_startoff
- shift
;
5545 * The extent, once shifted, must be adjacent in-file and on-disk with
5546 * the preceding extent.
5548 if ((left
->br_startoff
+ left
->br_blockcount
!= startoff
) ||
5549 (left
->br_startblock
+ left
->br_blockcount
!= got
->br_startblock
) ||
5550 (left
->br_state
!= got
->br_state
) ||
5551 (left
->br_blockcount
+ got
->br_blockcount
> MAXEXTLEN
))
5558 * A bmap extent shift adjusts the file offset of an extent to fill a preceding
5559 * hole in the file. If an extent shift would result in the extent being fully
5560 * adjacent to the extent that currently precedes the hole, we can merge with
5561 * the preceding extent rather than do the shift.
5563 * This function assumes the caller has verified a shift-by-merge is possible
5564 * with the provided extents via xfs_bmse_can_merge().
5568 struct xfs_inode
*ip
,
5570 xfs_fileoff_t shift
, /* shift fsb */
5571 struct xfs_iext_cursor
*icur
,
5572 struct xfs_bmbt_irec
*got
, /* extent to shift */
5573 struct xfs_bmbt_irec
*left
, /* preceding extent */
5574 struct xfs_btree_cur
*cur
,
5575 int *logflags
, /* output */
5576 struct xfs_defer_ops
*dfops
)
5578 struct xfs_bmbt_irec
new;
5579 xfs_filblks_t blockcount
;
5581 struct xfs_mount
*mp
= ip
->i_mount
;
5583 blockcount
= left
->br_blockcount
+ got
->br_blockcount
;
5585 ASSERT(xfs_isilocked(ip
, XFS_IOLOCK_EXCL
));
5586 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
5587 ASSERT(xfs_bmse_can_merge(left
, got
, shift
));
5590 new.br_blockcount
= blockcount
;
5593 * Update the on-disk extent count, the btree if necessary and log the
5596 XFS_IFORK_NEXT_SET(ip
, whichfork
,
5597 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
5598 *logflags
|= XFS_ILOG_CORE
;
5600 *logflags
|= XFS_ILOG_DEXT
;
5604 /* lookup and remove the extent to merge */
5605 error
= xfs_bmbt_lookup_eq(cur
, got
, &i
);
5608 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
5610 error
= xfs_btree_delete(cur
, &i
);
5613 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
5615 /* lookup and update size of the previous extent */
5616 error
= xfs_bmbt_lookup_eq(cur
, left
, &i
);
5619 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
5621 error
= xfs_bmbt_update(cur
, &new);
5626 xfs_iext_remove(ip
, icur
, 0);
5627 xfs_iext_prev(XFS_IFORK_PTR(ip
, whichfork
), icur
);
5628 xfs_iext_update_extent(ip
, xfs_bmap_fork_to_state(whichfork
), icur
,
5631 /* update reverse mapping. rmap functions merge the rmaps for us */
5632 error
= xfs_rmap_unmap_extent(mp
, dfops
, ip
, whichfork
, got
);
5635 memcpy(&new, got
, sizeof(new));
5636 new.br_startoff
= left
->br_startoff
+ left
->br_blockcount
;
5637 return xfs_rmap_map_extent(mp
, dfops
, ip
, whichfork
, &new);
5641 xfs_bmap_shift_update_extent(
5642 struct xfs_inode
*ip
,
5644 struct xfs_iext_cursor
*icur
,
5645 struct xfs_bmbt_irec
*got
,
5646 struct xfs_btree_cur
*cur
,
5648 struct xfs_defer_ops
*dfops
,
5649 xfs_fileoff_t startoff
)
5651 struct xfs_mount
*mp
= ip
->i_mount
;
5652 struct xfs_bmbt_irec prev
= *got
;
5655 *logflags
|= XFS_ILOG_CORE
;
5657 got
->br_startoff
= startoff
;
5660 error
= xfs_bmbt_lookup_eq(cur
, &prev
, &i
);
5663 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
5665 error
= xfs_bmbt_update(cur
, got
);
5669 *logflags
|= XFS_ILOG_DEXT
;
5672 xfs_iext_update_extent(ip
, xfs_bmap_fork_to_state(whichfork
), icur
,
5675 /* update reverse mapping */
5676 error
= xfs_rmap_unmap_extent(mp
, dfops
, ip
, whichfork
, &prev
);
5679 return xfs_rmap_map_extent(mp
, dfops
, ip
, whichfork
, got
);
5683 xfs_bmap_collapse_extents(
5684 struct xfs_trans
*tp
,
5685 struct xfs_inode
*ip
,
5686 xfs_fileoff_t
*next_fsb
,
5687 xfs_fileoff_t offset_shift_fsb
,
5689 xfs_fsblock_t
*firstblock
,
5690 struct xfs_defer_ops
*dfops
)
5692 int whichfork
= XFS_DATA_FORK
;
5693 struct xfs_mount
*mp
= ip
->i_mount
;
5694 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5695 struct xfs_btree_cur
*cur
= NULL
;
5696 struct xfs_bmbt_irec got
, prev
;
5697 struct xfs_iext_cursor icur
;
5698 xfs_fileoff_t new_startoff
;
5702 if (unlikely(XFS_TEST_ERROR(
5703 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
5704 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
5705 mp
, XFS_ERRTAG_BMAPIFORMAT
))) {
5706 XFS_ERROR_REPORT(__func__
, XFS_ERRLEVEL_LOW
, mp
);
5707 return -EFSCORRUPTED
;
5710 if (XFS_FORCED_SHUTDOWN(mp
))
5713 ASSERT(xfs_isilocked(ip
, XFS_IOLOCK_EXCL
| XFS_ILOCK_EXCL
));
5715 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
5716 error
= xfs_iread_extents(tp
, ip
, whichfork
);
5721 if (ifp
->if_flags
& XFS_IFBROOT
) {
5722 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
5723 cur
->bc_private
.b
.firstblock
= *firstblock
;
5724 cur
->bc_private
.b
.dfops
= dfops
;
5725 cur
->bc_private
.b
.flags
= 0;
5728 if (!xfs_iext_lookup_extent(ip
, ifp
, *next_fsb
, &icur
, &got
)) {
5732 XFS_WANT_CORRUPTED_GOTO(mp
, !isnullstartblock(got
.br_startblock
),
5735 new_startoff
= got
.br_startoff
- offset_shift_fsb
;
5736 if (xfs_iext_peek_prev_extent(ifp
, &icur
, &prev
)) {
5737 if (new_startoff
< prev
.br_startoff
+ prev
.br_blockcount
) {
5742 if (xfs_bmse_can_merge(&prev
, &got
, offset_shift_fsb
)) {
5743 error
= xfs_bmse_merge(ip
, whichfork
, offset_shift_fsb
,
5744 &icur
, &got
, &prev
, cur
, &logflags
,
5751 if (got
.br_startoff
< offset_shift_fsb
) {
5757 error
= xfs_bmap_shift_update_extent(ip
, whichfork
, &icur
, &got
, cur
,
5758 &logflags
, dfops
, new_startoff
);
5763 if (!xfs_iext_next_extent(ifp
, &icur
, &got
)) {
5768 *next_fsb
= got
.br_startoff
;
5771 xfs_btree_del_cursor(cur
,
5772 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
5774 xfs_trans_log_inode(tp
, ip
, logflags
);
5779 xfs_bmap_insert_extents(
5780 struct xfs_trans
*tp
,
5781 struct xfs_inode
*ip
,
5782 xfs_fileoff_t
*next_fsb
,
5783 xfs_fileoff_t offset_shift_fsb
,
5785 xfs_fileoff_t stop_fsb
,
5786 xfs_fsblock_t
*firstblock
,
5787 struct xfs_defer_ops
*dfops
)
5789 int whichfork
= XFS_DATA_FORK
;
5790 struct xfs_mount
*mp
= ip
->i_mount
;
5791 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5792 struct xfs_btree_cur
*cur
= NULL
;
5793 struct xfs_bmbt_irec got
, next
;
5794 struct xfs_iext_cursor icur
;
5795 xfs_fileoff_t new_startoff
;
5799 if (unlikely(XFS_TEST_ERROR(
5800 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
5801 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
5802 mp
, XFS_ERRTAG_BMAPIFORMAT
))) {
5803 XFS_ERROR_REPORT(__func__
, XFS_ERRLEVEL_LOW
, mp
);
5804 return -EFSCORRUPTED
;
5807 if (XFS_FORCED_SHUTDOWN(mp
))
5810 ASSERT(xfs_isilocked(ip
, XFS_IOLOCK_EXCL
| XFS_ILOCK_EXCL
));
5812 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
5813 error
= xfs_iread_extents(tp
, ip
, whichfork
);
5818 if (ifp
->if_flags
& XFS_IFBROOT
) {
5819 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
5820 cur
->bc_private
.b
.firstblock
= *firstblock
;
5821 cur
->bc_private
.b
.dfops
= dfops
;
5822 cur
->bc_private
.b
.flags
= 0;
5825 if (*next_fsb
== NULLFSBLOCK
) {
5826 xfs_iext_last(ifp
, &icur
);
5827 if (!xfs_iext_get_extent(ifp
, &icur
, &got
) ||
5828 stop_fsb
> got
.br_startoff
) {
5833 if (!xfs_iext_lookup_extent(ip
, ifp
, *next_fsb
, &icur
, &got
)) {
5838 XFS_WANT_CORRUPTED_GOTO(mp
, !isnullstartblock(got
.br_startblock
),
5841 if (stop_fsb
>= got
.br_startoff
+ got
.br_blockcount
) {
5846 new_startoff
= got
.br_startoff
+ offset_shift_fsb
;
5847 if (xfs_iext_peek_next_extent(ifp
, &icur
, &next
)) {
5848 if (new_startoff
+ got
.br_blockcount
> next
.br_startoff
) {
5854 * Unlike a left shift (which involves a hole punch), a right
5855 * shift does not modify extent neighbors in any way. We should
5856 * never find mergeable extents in this scenario. Check anyways
5857 * and warn if we encounter two extents that could be one.
5859 if (xfs_bmse_can_merge(&got
, &next
, offset_shift_fsb
))
5863 error
= xfs_bmap_shift_update_extent(ip
, whichfork
, &icur
, &got
, cur
,
5864 &logflags
, dfops
, new_startoff
);
5868 if (!xfs_iext_prev_extent(ifp
, &icur
, &got
) ||
5869 stop_fsb
>= got
.br_startoff
+ got
.br_blockcount
) {
5874 *next_fsb
= got
.br_startoff
;
5877 xfs_btree_del_cursor(cur
,
5878 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
5880 xfs_trans_log_inode(tp
, ip
, logflags
);
5885 * Splits an extent into two extents at split_fsb block such that it is the
5886 * first block of the current_ext. @ext is a target extent to be split.
5887 * @split_fsb is a block where the extents is split. If split_fsb lies in a
5888 * hole or the first block of extents, just return 0.
5891 xfs_bmap_split_extent_at(
5892 struct xfs_trans
*tp
,
5893 struct xfs_inode
*ip
,
5894 xfs_fileoff_t split_fsb
,
5895 xfs_fsblock_t
*firstfsb
,
5896 struct xfs_defer_ops
*dfops
)
5898 int whichfork
= XFS_DATA_FORK
;
5899 struct xfs_btree_cur
*cur
= NULL
;
5900 struct xfs_bmbt_irec got
;
5901 struct xfs_bmbt_irec
new; /* split extent */
5902 struct xfs_mount
*mp
= ip
->i_mount
;
5903 struct xfs_ifork
*ifp
;
5904 xfs_fsblock_t gotblkcnt
; /* new block count for got */
5905 struct xfs_iext_cursor icur
;
5910 if (unlikely(XFS_TEST_ERROR(
5911 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
5912 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
5913 mp
, XFS_ERRTAG_BMAPIFORMAT
))) {
5914 XFS_ERROR_REPORT("xfs_bmap_split_extent_at",
5915 XFS_ERRLEVEL_LOW
, mp
);
5916 return -EFSCORRUPTED
;
5919 if (XFS_FORCED_SHUTDOWN(mp
))
5922 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5923 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
5924 /* Read in all the extents */
5925 error
= xfs_iread_extents(tp
, ip
, whichfork
);
5931 * If there are not extents, or split_fsb lies in a hole we are done.
5933 if (!xfs_iext_lookup_extent(ip
, ifp
, split_fsb
, &icur
, &got
) ||
5934 got
.br_startoff
>= split_fsb
)
5937 gotblkcnt
= split_fsb
- got
.br_startoff
;
5938 new.br_startoff
= split_fsb
;
5939 new.br_startblock
= got
.br_startblock
+ gotblkcnt
;
5940 new.br_blockcount
= got
.br_blockcount
- gotblkcnt
;
5941 new.br_state
= got
.br_state
;
5943 if (ifp
->if_flags
& XFS_IFBROOT
) {
5944 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
5945 cur
->bc_private
.b
.firstblock
= *firstfsb
;
5946 cur
->bc_private
.b
.dfops
= dfops
;
5947 cur
->bc_private
.b
.flags
= 0;
5948 error
= xfs_bmbt_lookup_eq(cur
, &got
, &i
);
5951 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, del_cursor
);
5954 got
.br_blockcount
= gotblkcnt
;
5955 xfs_iext_update_extent(ip
, xfs_bmap_fork_to_state(whichfork
), &icur
,
5958 logflags
= XFS_ILOG_CORE
;
5960 error
= xfs_bmbt_update(cur
, &got
);
5964 logflags
|= XFS_ILOG_DEXT
;
5966 /* Add new extent */
5967 xfs_iext_next(ifp
, &icur
);
5968 xfs_iext_insert(ip
, &icur
, &new, 0);
5969 XFS_IFORK_NEXT_SET(ip
, whichfork
,
5970 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
5973 error
= xfs_bmbt_lookup_eq(cur
, &new, &i
);
5976 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, del_cursor
);
5977 error
= xfs_btree_insert(cur
, &i
);
5980 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, del_cursor
);
5984 * Convert to a btree if necessary.
5986 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
5987 int tmp_logflags
; /* partial log flag return val */
5989 ASSERT(cur
== NULL
);
5990 error
= xfs_bmap_extents_to_btree(tp
, ip
, firstfsb
, dfops
,
5991 &cur
, 0, &tmp_logflags
, whichfork
);
5992 logflags
|= tmp_logflags
;
5997 cur
->bc_private
.b
.allocated
= 0;
5998 xfs_btree_del_cursor(cur
,
5999 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
6003 xfs_trans_log_inode(tp
, ip
, logflags
);
6008 xfs_bmap_split_extent(
6009 struct xfs_inode
*ip
,
6010 xfs_fileoff_t split_fsb
)
6012 struct xfs_mount
*mp
= ip
->i_mount
;
6013 struct xfs_trans
*tp
;
6014 struct xfs_defer_ops dfops
;
6015 xfs_fsblock_t firstfsb
;
6018 error
= xfs_trans_alloc(mp
, &M_RES(mp
)->tr_write
,
6019 XFS_DIOSTRAT_SPACE_RES(mp
, 0), 0, 0, &tp
);
6023 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
6024 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
);
6026 xfs_defer_init(&dfops
, &firstfsb
);
6028 error
= xfs_bmap_split_extent_at(tp
, ip
, split_fsb
,
6033 error
= xfs_defer_finish(&tp
, &dfops
);
6037 return xfs_trans_commit(tp
);
6040 xfs_defer_cancel(&dfops
);
6041 xfs_trans_cancel(tp
);
6045 /* Deferred mapping is only for real extents in the data fork. */
6047 xfs_bmap_is_update_needed(
6048 struct xfs_bmbt_irec
*bmap
)
6050 return bmap
->br_startblock
!= HOLESTARTBLOCK
&&
6051 bmap
->br_startblock
!= DELAYSTARTBLOCK
;
6054 /* Record a bmap intent. */
6057 struct xfs_mount
*mp
,
6058 struct xfs_defer_ops
*dfops
,
6059 enum xfs_bmap_intent_type type
,
6060 struct xfs_inode
*ip
,
6062 struct xfs_bmbt_irec
*bmap
)
6065 struct xfs_bmap_intent
*bi
;
6067 trace_xfs_bmap_defer(mp
,
6068 XFS_FSB_TO_AGNO(mp
, bmap
->br_startblock
),
6070 XFS_FSB_TO_AGBNO(mp
, bmap
->br_startblock
),
6071 ip
->i_ino
, whichfork
,
6073 bmap
->br_blockcount
,
6076 bi
= kmem_alloc(sizeof(struct xfs_bmap_intent
), KM_SLEEP
| KM_NOFS
);
6077 INIT_LIST_HEAD(&bi
->bi_list
);
6080 bi
->bi_whichfork
= whichfork
;
6081 bi
->bi_bmap
= *bmap
;
6083 error
= xfs_defer_ijoin(dfops
, bi
->bi_owner
);
6089 xfs_defer_add(dfops
, XFS_DEFER_OPS_TYPE_BMAP
, &bi
->bi_list
);
6093 /* Map an extent into a file. */
6095 xfs_bmap_map_extent(
6096 struct xfs_mount
*mp
,
6097 struct xfs_defer_ops
*dfops
,
6098 struct xfs_inode
*ip
,
6099 struct xfs_bmbt_irec
*PREV
)
6101 if (!xfs_bmap_is_update_needed(PREV
))
6104 return __xfs_bmap_add(mp
, dfops
, XFS_BMAP_MAP
, ip
,
6105 XFS_DATA_FORK
, PREV
);
6108 /* Unmap an extent out of a file. */
6110 xfs_bmap_unmap_extent(
6111 struct xfs_mount
*mp
,
6112 struct xfs_defer_ops
*dfops
,
6113 struct xfs_inode
*ip
,
6114 struct xfs_bmbt_irec
*PREV
)
6116 if (!xfs_bmap_is_update_needed(PREV
))
6119 return __xfs_bmap_add(mp
, dfops
, XFS_BMAP_UNMAP
, ip
,
6120 XFS_DATA_FORK
, PREV
);
6124 * Process one of the deferred bmap operations. We pass back the
6125 * btree cursor to maintain our lock on the bmapbt between calls.
6128 xfs_bmap_finish_one(
6129 struct xfs_trans
*tp
,
6130 struct xfs_defer_ops
*dfops
,
6131 struct xfs_inode
*ip
,
6132 enum xfs_bmap_intent_type type
,
6134 xfs_fileoff_t startoff
,
6135 xfs_fsblock_t startblock
,
6136 xfs_filblks_t
*blockcount
,
6139 xfs_fsblock_t firstfsb
;
6143 * firstfsb is tied to the transaction lifetime and is used to
6144 * ensure correct AG locking order and schedule work item
6145 * continuations. XFS_BUI_MAX_FAST_EXTENTS (== 1) restricts us
6146 * to only making one bmap call per transaction, so it should
6147 * be safe to have it as a local variable here.
6149 firstfsb
= NULLFSBLOCK
;
6151 trace_xfs_bmap_deferred(tp
->t_mountp
,
6152 XFS_FSB_TO_AGNO(tp
->t_mountp
, startblock
), type
,
6153 XFS_FSB_TO_AGBNO(tp
->t_mountp
, startblock
),
6154 ip
->i_ino
, whichfork
, startoff
, *blockcount
, state
);
6156 if (WARN_ON_ONCE(whichfork
!= XFS_DATA_FORK
))
6157 return -EFSCORRUPTED
;
6159 if (XFS_TEST_ERROR(false, tp
->t_mountp
,
6160 XFS_ERRTAG_BMAP_FINISH_ONE
))
6165 error
= xfs_bmapi_remap(tp
, ip
, startoff
, *blockcount
,
6166 startblock
, dfops
, 0);
6169 case XFS_BMAP_UNMAP
:
6170 error
= __xfs_bunmapi(tp
, ip
, startoff
, blockcount
,
6171 XFS_BMAPI_REMAP
, 1, &firstfsb
, dfops
);
6175 error
= -EFSCORRUPTED
;
6181 /* Check that an inode's extent does not have invalid flags or bad ranges. */
6183 xfs_bmap_validate_extent(
6184 struct xfs_inode
*ip
,
6186 struct xfs_bmbt_irec
*irec
)
6188 struct xfs_mount
*mp
= ip
->i_mount
;
6189 xfs_fsblock_t endfsb
;
6192 isrt
= XFS_IS_REALTIME_INODE(ip
);
6193 endfsb
= irec
->br_startblock
+ irec
->br_blockcount
- 1;
6195 if (!xfs_verify_rtbno(mp
, irec
->br_startblock
))
6196 return __this_address
;
6197 if (!xfs_verify_rtbno(mp
, endfsb
))
6198 return __this_address
;
6200 if (!xfs_verify_fsbno(mp
, irec
->br_startblock
))
6201 return __this_address
;
6202 if (!xfs_verify_fsbno(mp
, endfsb
))
6203 return __this_address
;
6204 if (XFS_FSB_TO_AGNO(mp
, irec
->br_startblock
) !=
6205 XFS_FSB_TO_AGNO(mp
, endfsb
))
6206 return __this_address
;
6208 if (irec
->br_state
!= XFS_EXT_NORM
) {
6209 if (whichfork
!= XFS_DATA_FORK
)
6210 return __this_address
;
6211 if (!xfs_sb_version_hasextflgbit(&mp
->m_sb
))
6212 return __this_address
;