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
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_inode_item.h"
35 #include "xfs_extfree_item.h"
36 #include "xfs_alloc.h"
38 #include "xfs_bmap_util.h"
39 #include "xfs_bmap_btree.h"
40 #include "xfs_rtalloc.h"
41 #include "xfs_error.h"
42 #include "xfs_quota.h"
43 #include "xfs_trans_space.h"
44 #include "xfs_buf_item.h"
45 #include "xfs_trace.h"
46 #include "xfs_symlink.h"
47 #include "xfs_attr_leaf.h"
48 #include "xfs_filestream.h"
50 #include "xfs_ag_resv.h"
51 #include "xfs_refcount.h"
52 #include "xfs_rmap_btree.h"
53 #include "xfs_icache.h"
56 kmem_zone_t
*xfs_bmap_free_item_zone
;
59 * Miscellaneous helper functions
63 * Compute and fill in the value of the maximum depth of a bmap btree
64 * in this filesystem. Done once, during mount.
67 xfs_bmap_compute_maxlevels(
68 xfs_mount_t
*mp
, /* file system mount structure */
69 int whichfork
) /* data or attr fork */
71 int level
; /* btree level */
72 uint maxblocks
; /* max blocks at this level */
73 uint maxleafents
; /* max leaf entries possible */
74 int maxrootrecs
; /* max records in root block */
75 int minleafrecs
; /* min records in leaf block */
76 int minnoderecs
; /* min records in node block */
77 int sz
; /* root block size */
80 * The maximum number of extents in a file, hence the maximum
81 * number of leaf entries, is controlled by the type of di_nextents
82 * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
83 * (a signed 16-bit number, xfs_aextnum_t).
85 * Note that we can no longer assume that if we are in ATTR1 that
86 * the fork offset of all the inodes will be
87 * (xfs_default_attroffset(ip) >> 3) because we could have mounted
88 * with ATTR2 and then mounted back with ATTR1, keeping the
89 * di_forkoff's fixed but probably at various positions. Therefore,
90 * for both ATTR1 and ATTR2 we have to assume the worst case scenario
91 * of a minimum size available.
93 if (whichfork
== XFS_DATA_FORK
) {
94 maxleafents
= MAXEXTNUM
;
95 sz
= XFS_BMDR_SPACE_CALC(MINDBTPTRS
);
97 maxleafents
= MAXAEXTNUM
;
98 sz
= XFS_BMDR_SPACE_CALC(MINABTPTRS
);
100 maxrootrecs
= xfs_bmdr_maxrecs(sz
, 0);
101 minleafrecs
= mp
->m_bmap_dmnr
[0];
102 minnoderecs
= mp
->m_bmap_dmnr
[1];
103 maxblocks
= (maxleafents
+ minleafrecs
- 1) / minleafrecs
;
104 for (level
= 1; maxblocks
> 1; level
++) {
105 if (maxblocks
<= maxrootrecs
)
108 maxblocks
= (maxblocks
+ minnoderecs
- 1) / minnoderecs
;
110 mp
->m_bm_maxlevels
[whichfork
] = level
;
113 STATIC
int /* error */
115 struct xfs_btree_cur
*cur
,
119 int *stat
) /* success/failure */
121 cur
->bc_rec
.b
.br_startoff
= off
;
122 cur
->bc_rec
.b
.br_startblock
= bno
;
123 cur
->bc_rec
.b
.br_blockcount
= len
;
124 return xfs_btree_lookup(cur
, XFS_LOOKUP_EQ
, stat
);
127 STATIC
int /* error */
129 struct xfs_btree_cur
*cur
,
133 int *stat
) /* success/failure */
135 cur
->bc_rec
.b
.br_startoff
= off
;
136 cur
->bc_rec
.b
.br_startblock
= bno
;
137 cur
->bc_rec
.b
.br_blockcount
= len
;
138 return xfs_btree_lookup(cur
, XFS_LOOKUP_GE
, stat
);
142 * Check if the inode needs to be converted to btree format.
144 static inline bool xfs_bmap_needs_btree(struct xfs_inode
*ip
, int whichfork
)
146 return whichfork
!= XFS_COW_FORK
&&
147 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
&&
148 XFS_IFORK_NEXTENTS(ip
, whichfork
) >
149 XFS_IFORK_MAXEXT(ip
, whichfork
);
153 * Check if the inode should be converted to extent format.
155 static inline bool xfs_bmap_wants_extents(struct xfs_inode
*ip
, int whichfork
)
157 return whichfork
!= XFS_COW_FORK
&&
158 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
&&
159 XFS_IFORK_NEXTENTS(ip
, whichfork
) <=
160 XFS_IFORK_MAXEXT(ip
, whichfork
);
164 * Update the record referred to by cur to the value given
165 * by [off, bno, len, state].
166 * This either works (return 0) or gets an EFSCORRUPTED error.
170 struct xfs_btree_cur
*cur
,
176 union xfs_btree_rec rec
;
178 xfs_bmbt_disk_set_allf(&rec
.bmbt
, off
, bno
, len
, state
);
179 return xfs_btree_update(cur
, &rec
);
183 * Compute the worst-case number of indirect blocks that will be used
184 * for ip's delayed extent of length "len".
187 xfs_bmap_worst_indlen(
188 xfs_inode_t
*ip
, /* incore inode pointer */
189 xfs_filblks_t len
) /* delayed extent length */
191 int level
; /* btree level number */
192 int maxrecs
; /* maximum record count at this level */
193 xfs_mount_t
*mp
; /* mount structure */
194 xfs_filblks_t rval
; /* return value */
195 xfs_filblks_t orig_len
;
199 /* Calculate the worst-case size of the bmbt. */
201 maxrecs
= mp
->m_bmap_dmxr
[0];
202 for (level
= 0, rval
= 0;
203 level
< XFS_BM_MAXLEVELS(mp
, XFS_DATA_FORK
);
206 do_div(len
, maxrecs
);
209 rval
+= XFS_BM_MAXLEVELS(mp
, XFS_DATA_FORK
) -
214 maxrecs
= mp
->m_bmap_dmxr
[1];
217 /* Calculate the worst-case size of the rmapbt. */
218 if (xfs_sb_version_hasrmapbt(&mp
->m_sb
))
219 rval
+= 1 + xfs_rmapbt_calc_size(mp
, orig_len
) +
220 mp
->m_rmap_maxlevels
;
226 * Calculate the default attribute fork offset for newly created inodes.
229 xfs_default_attroffset(
230 struct xfs_inode
*ip
)
232 struct xfs_mount
*mp
= ip
->i_mount
;
235 if (mp
->m_sb
.sb_inodesize
== 256) {
236 offset
= XFS_LITINO(mp
, ip
->i_d
.di_version
) -
237 XFS_BMDR_SPACE_CALC(MINABTPTRS
);
239 offset
= XFS_BMDR_SPACE_CALC(6 * MINABTPTRS
);
242 ASSERT(offset
< XFS_LITINO(mp
, ip
->i_d
.di_version
));
247 * Helper routine to reset inode di_forkoff field when switching
248 * attribute fork from local to extent format - we reset it where
249 * possible to make space available for inline data fork extents.
252 xfs_bmap_forkoff_reset(
256 if (whichfork
== XFS_ATTR_FORK
&&
257 ip
->i_d
.di_format
!= XFS_DINODE_FMT_DEV
&&
258 ip
->i_d
.di_format
!= XFS_DINODE_FMT_UUID
&&
259 ip
->i_d
.di_format
!= XFS_DINODE_FMT_BTREE
) {
260 uint dfl_forkoff
= xfs_default_attroffset(ip
) >> 3;
262 if (dfl_forkoff
> ip
->i_d
.di_forkoff
)
263 ip
->i_d
.di_forkoff
= dfl_forkoff
;
268 STATIC
struct xfs_buf
*
270 struct xfs_btree_cur
*cur
,
273 struct xfs_log_item_desc
*lidp
;
279 for (i
= 0; i
< XFS_BTREE_MAXLEVELS
; i
++) {
280 if (!cur
->bc_bufs
[i
])
282 if (XFS_BUF_ADDR(cur
->bc_bufs
[i
]) == bno
)
283 return cur
->bc_bufs
[i
];
286 /* Chase down all the log items to see if the bp is there */
287 list_for_each_entry(lidp
, &cur
->bc_tp
->t_items
, lid_trans
) {
288 struct xfs_buf_log_item
*bip
;
289 bip
= (struct xfs_buf_log_item
*)lidp
->lid_item
;
290 if (bip
->bli_item
.li_type
== XFS_LI_BUF
&&
291 XFS_BUF_ADDR(bip
->bli_buf
) == bno
)
300 struct xfs_btree_block
*block
,
306 __be64
*pp
, *thispa
; /* pointer to block address */
307 xfs_bmbt_key_t
*prevp
, *keyp
;
309 ASSERT(be16_to_cpu(block
->bb_level
) > 0);
312 for( i
= 1; i
<= xfs_btree_get_numrecs(block
); i
++) {
313 dmxr
= mp
->m_bmap_dmxr
[0];
314 keyp
= XFS_BMBT_KEY_ADDR(mp
, block
, i
);
317 ASSERT(be64_to_cpu(prevp
->br_startoff
) <
318 be64_to_cpu(keyp
->br_startoff
));
323 * Compare the block numbers to see if there are dups.
326 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, i
, sz
);
328 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, i
, dmxr
);
330 for (j
= i
+1; j
<= be16_to_cpu(block
->bb_numrecs
); j
++) {
332 thispa
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, j
, sz
);
334 thispa
= XFS_BMBT_PTR_ADDR(mp
, block
, j
, dmxr
);
335 if (*thispa
== *pp
) {
336 xfs_warn(mp
, "%s: thispa(%d) == pp(%d) %Ld",
338 (unsigned long long)be64_to_cpu(*thispa
));
339 panic("%s: ptrs are equal in node\n",
347 * Check that the extents for the inode ip are in the right order in all
348 * btree leaves. THis becomes prohibitively expensive for large extent count
349 * files, so don't bother with inodes that have more than 10,000 extents in
350 * them. The btree record ordering checks will still be done, so for such large
351 * bmapbt constructs that is going to catch most corruptions.
354 xfs_bmap_check_leaf_extents(
355 xfs_btree_cur_t
*cur
, /* btree cursor or null */
356 xfs_inode_t
*ip
, /* incore inode pointer */
357 int whichfork
) /* data or attr fork */
359 struct xfs_btree_block
*block
; /* current btree block */
360 xfs_fsblock_t bno
; /* block # of "block" */
361 xfs_buf_t
*bp
; /* buffer for "block" */
362 int error
; /* error return value */
363 xfs_extnum_t i
=0, j
; /* index into the extents list */
364 xfs_ifork_t
*ifp
; /* fork structure */
365 int level
; /* btree level, for checking */
366 xfs_mount_t
*mp
; /* file system mount structure */
367 __be64
*pp
; /* pointer to block address */
368 xfs_bmbt_rec_t
*ep
; /* pointer to current extent */
369 xfs_bmbt_rec_t last
= {0, 0}; /* last extent in prev block */
370 xfs_bmbt_rec_t
*nextp
; /* pointer to next extent */
373 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
) {
377 /* skip large extent count inodes */
378 if (ip
->i_d
.di_nextents
> 10000)
383 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
384 block
= ifp
->if_broot
;
386 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
388 level
= be16_to_cpu(block
->bb_level
);
390 xfs_check_block(block
, mp
, 1, ifp
->if_broot_bytes
);
391 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, 1, ifp
->if_broot_bytes
);
392 bno
= be64_to_cpu(*pp
);
394 ASSERT(bno
!= NULLFSBLOCK
);
395 ASSERT(XFS_FSB_TO_AGNO(mp
, bno
) < mp
->m_sb
.sb_agcount
);
396 ASSERT(XFS_FSB_TO_AGBNO(mp
, bno
) < mp
->m_sb
.sb_agblocks
);
399 * Go down the tree until leaf level is reached, following the first
400 * pointer (leftmost) at each level.
402 while (level
-- > 0) {
403 /* See if buf is in cur first */
405 bp
= xfs_bmap_get_bp(cur
, XFS_FSB_TO_DADDR(mp
, bno
));
408 error
= xfs_btree_read_bufl(mp
, NULL
, bno
, 0, &bp
,
414 block
= XFS_BUF_TO_BLOCK(bp
);
419 * Check this block for basic sanity (increasing keys and
420 * no duplicate blocks).
423 xfs_check_block(block
, mp
, 0, 0);
424 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, mp
->m_bmap_dmxr
[1]);
425 bno
= be64_to_cpu(*pp
);
426 XFS_WANT_CORRUPTED_GOTO(mp
,
427 XFS_FSB_SANITY_CHECK(mp
, bno
), error0
);
430 xfs_trans_brelse(NULL
, bp
);
435 * Here with bp and block set to the leftmost leaf node in the tree.
440 * Loop over all leaf nodes checking that all extents are in the right order.
443 xfs_fsblock_t nextbno
;
444 xfs_extnum_t num_recs
;
447 num_recs
= xfs_btree_get_numrecs(block
);
450 * Read-ahead the next leaf block, if any.
453 nextbno
= be64_to_cpu(block
->bb_u
.l
.bb_rightsib
);
456 * Check all the extents to make sure they are OK.
457 * If we had a previous block, the last entry should
458 * conform with the first entry in this one.
461 ep
= XFS_BMBT_REC_ADDR(mp
, block
, 1);
463 ASSERT(xfs_bmbt_disk_get_startoff(&last
) +
464 xfs_bmbt_disk_get_blockcount(&last
) <=
465 xfs_bmbt_disk_get_startoff(ep
));
467 for (j
= 1; j
< num_recs
; j
++) {
468 nextp
= XFS_BMBT_REC_ADDR(mp
, block
, j
+ 1);
469 ASSERT(xfs_bmbt_disk_get_startoff(ep
) +
470 xfs_bmbt_disk_get_blockcount(ep
) <=
471 xfs_bmbt_disk_get_startoff(nextp
));
479 xfs_trans_brelse(NULL
, bp
);
483 * If we've reached the end, stop.
485 if (bno
== NULLFSBLOCK
)
489 bp
= xfs_bmap_get_bp(cur
, XFS_FSB_TO_DADDR(mp
, bno
));
492 error
= xfs_btree_read_bufl(mp
, NULL
, bno
, 0, &bp
,
498 block
= XFS_BUF_TO_BLOCK(bp
);
504 xfs_warn(mp
, "%s: at error0", __func__
);
506 xfs_trans_brelse(NULL
, bp
);
508 xfs_warn(mp
, "%s: BAD after btree leaves for %d extents",
510 panic("%s: CORRUPTED BTREE OR SOMETHING", __func__
);
515 * Add bmap trace insert entries for all the contents of the extent records.
518 xfs_bmap_trace_exlist(
519 xfs_inode_t
*ip
, /* incore inode pointer */
520 xfs_extnum_t cnt
, /* count of entries in the list */
521 int whichfork
, /* data or attr or cow fork */
522 unsigned long caller_ip
)
524 xfs_extnum_t idx
; /* extent record index */
525 xfs_ifork_t
*ifp
; /* inode fork pointer */
528 if (whichfork
== XFS_ATTR_FORK
)
529 state
|= BMAP_ATTRFORK
;
530 else if (whichfork
== XFS_COW_FORK
)
531 state
|= BMAP_COWFORK
;
533 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
534 ASSERT(cnt
== xfs_iext_count(ifp
));
535 for (idx
= 0; idx
< cnt
; idx
++)
536 trace_xfs_extlist(ip
, idx
, state
, caller_ip
);
540 * Validate that the bmbt_irecs being returned from bmapi are valid
541 * given the caller's original parameters. Specifically check the
542 * ranges of the returned irecs to ensure that they only extend beyond
543 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
546 xfs_bmap_validate_ret(
550 xfs_bmbt_irec_t
*mval
,
554 int i
; /* index to map values */
556 ASSERT(ret_nmap
<= nmap
);
558 for (i
= 0; i
< ret_nmap
; i
++) {
559 ASSERT(mval
[i
].br_blockcount
> 0);
560 if (!(flags
& XFS_BMAPI_ENTIRE
)) {
561 ASSERT(mval
[i
].br_startoff
>= bno
);
562 ASSERT(mval
[i
].br_blockcount
<= len
);
563 ASSERT(mval
[i
].br_startoff
+ mval
[i
].br_blockcount
<=
566 ASSERT(mval
[i
].br_startoff
< bno
+ len
);
567 ASSERT(mval
[i
].br_startoff
+ mval
[i
].br_blockcount
>
571 mval
[i
- 1].br_startoff
+ mval
[i
- 1].br_blockcount
==
572 mval
[i
].br_startoff
);
573 ASSERT(mval
[i
].br_startblock
!= DELAYSTARTBLOCK
&&
574 mval
[i
].br_startblock
!= HOLESTARTBLOCK
);
575 ASSERT(mval
[i
].br_state
== XFS_EXT_NORM
||
576 mval
[i
].br_state
== XFS_EXT_UNWRITTEN
);
581 #define xfs_bmap_check_leaf_extents(cur, ip, whichfork) do { } while (0)
582 #define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap)
586 * bmap free list manipulation functions
590 * Add the extent to the list of extents to be free at transaction end.
591 * The list is maintained sorted (by block number).
595 struct xfs_mount
*mp
,
596 struct xfs_defer_ops
*dfops
,
599 struct xfs_owner_info
*oinfo
)
601 struct xfs_extent_free_item
*new; /* new element */
606 ASSERT(bno
!= NULLFSBLOCK
);
608 ASSERT(len
<= MAXEXTLEN
);
609 ASSERT(!isnullstartblock(bno
));
610 agno
= XFS_FSB_TO_AGNO(mp
, bno
);
611 agbno
= XFS_FSB_TO_AGBNO(mp
, bno
);
612 ASSERT(agno
< mp
->m_sb
.sb_agcount
);
613 ASSERT(agbno
< mp
->m_sb
.sb_agblocks
);
614 ASSERT(len
< mp
->m_sb
.sb_agblocks
);
615 ASSERT(agbno
+ len
<= mp
->m_sb
.sb_agblocks
);
617 ASSERT(xfs_bmap_free_item_zone
!= NULL
);
619 new = kmem_zone_alloc(xfs_bmap_free_item_zone
, KM_SLEEP
);
620 new->xefi_startblock
= bno
;
621 new->xefi_blockcount
= (xfs_extlen_t
)len
;
623 new->xefi_oinfo
= *oinfo
;
625 xfs_rmap_skip_owner_update(&new->xefi_oinfo
);
626 trace_xfs_bmap_free_defer(mp
, XFS_FSB_TO_AGNO(mp
, bno
), 0,
627 XFS_FSB_TO_AGBNO(mp
, bno
), len
);
628 xfs_defer_add(dfops
, XFS_DEFER_OPS_TYPE_FREE
, &new->xefi_list
);
632 * Inode fork format manipulation functions
636 * Transform a btree format file with only one leaf node, where the
637 * extents list will fit in the inode, into an extents format file.
638 * Since the file extents are already in-core, all we have to do is
639 * give up the space for the btree root and pitch the leaf block.
641 STATIC
int /* error */
642 xfs_bmap_btree_to_extents(
643 xfs_trans_t
*tp
, /* transaction pointer */
644 xfs_inode_t
*ip
, /* incore inode pointer */
645 xfs_btree_cur_t
*cur
, /* btree cursor */
646 int *logflagsp
, /* inode logging flags */
647 int whichfork
) /* data or attr fork */
650 struct xfs_btree_block
*cblock
;/* child btree block */
651 xfs_fsblock_t cbno
; /* child block number */
652 xfs_buf_t
*cbp
; /* child block's buffer */
653 int error
; /* error return value */
654 xfs_ifork_t
*ifp
; /* inode fork data */
655 xfs_mount_t
*mp
; /* mount point structure */
656 __be64
*pp
; /* ptr to block address */
657 struct xfs_btree_block
*rblock
;/* root btree block */
658 struct xfs_owner_info oinfo
;
661 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
662 ASSERT(whichfork
!= XFS_COW_FORK
);
663 ASSERT(ifp
->if_flags
& XFS_IFEXTENTS
);
664 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
);
665 rblock
= ifp
->if_broot
;
666 ASSERT(be16_to_cpu(rblock
->bb_level
) == 1);
667 ASSERT(be16_to_cpu(rblock
->bb_numrecs
) == 1);
668 ASSERT(xfs_bmbt_maxrecs(mp
, ifp
->if_broot_bytes
, 0) == 1);
669 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, rblock
, 1, ifp
->if_broot_bytes
);
670 cbno
= be64_to_cpu(*pp
);
673 if ((error
= xfs_btree_check_lptr(cur
, cbno
, 1)))
676 error
= xfs_btree_read_bufl(mp
, tp
, cbno
, 0, &cbp
, XFS_BMAP_BTREE_REF
,
680 cblock
= XFS_BUF_TO_BLOCK(cbp
);
681 if ((error
= xfs_btree_check_block(cur
, cblock
, 0, cbp
)))
683 xfs_rmap_ino_bmbt_owner(&oinfo
, ip
->i_ino
, whichfork
);
684 xfs_bmap_add_free(mp
, cur
->bc_private
.b
.dfops
, cbno
, 1, &oinfo
);
685 ip
->i_d
.di_nblocks
--;
686 xfs_trans_mod_dquot_byino(tp
, ip
, XFS_TRANS_DQ_BCOUNT
, -1L);
687 xfs_trans_binval(tp
, cbp
);
688 if (cur
->bc_bufs
[0] == cbp
)
689 cur
->bc_bufs
[0] = NULL
;
690 xfs_iroot_realloc(ip
, -1, whichfork
);
691 ASSERT(ifp
->if_broot
== NULL
);
692 ASSERT((ifp
->if_flags
& XFS_IFBROOT
) == 0);
693 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_EXTENTS
);
694 *logflagsp
= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
699 * Convert an extents-format file into a btree-format file.
700 * The new file will have a root block (in the inode) and a single child block.
702 STATIC
int /* error */
703 xfs_bmap_extents_to_btree(
704 xfs_trans_t
*tp
, /* transaction pointer */
705 xfs_inode_t
*ip
, /* incore inode pointer */
706 xfs_fsblock_t
*firstblock
, /* first-block-allocated */
707 struct xfs_defer_ops
*dfops
, /* blocks freed in xaction */
708 xfs_btree_cur_t
**curp
, /* cursor returned to caller */
709 int wasdel
, /* converting a delayed alloc */
710 int *logflagsp
, /* inode logging flags */
711 int whichfork
) /* data or attr fork */
713 struct xfs_btree_block
*ablock
; /* allocated (child) bt block */
714 xfs_buf_t
*abp
; /* buffer for ablock */
715 xfs_alloc_arg_t args
; /* allocation arguments */
716 xfs_bmbt_rec_t
*arp
; /* child record pointer */
717 struct xfs_btree_block
*block
; /* btree root block */
718 xfs_btree_cur_t
*cur
; /* bmap btree cursor */
719 xfs_bmbt_rec_host_t
*ep
; /* extent record pointer */
720 int error
; /* error return value */
721 xfs_extnum_t i
, cnt
; /* extent record index */
722 xfs_ifork_t
*ifp
; /* inode fork pointer */
723 xfs_bmbt_key_t
*kp
; /* root block key pointer */
724 xfs_mount_t
*mp
; /* mount structure */
725 xfs_extnum_t nextents
; /* number of file extents */
726 xfs_bmbt_ptr_t
*pp
; /* root block address pointer */
729 ASSERT(whichfork
!= XFS_COW_FORK
);
730 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
731 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
);
734 * Make space in the inode incore.
736 xfs_iroot_realloc(ip
, 1, whichfork
);
737 ifp
->if_flags
|= XFS_IFBROOT
;
742 block
= ifp
->if_broot
;
743 xfs_btree_init_block_int(mp
, block
, XFS_BUF_DADDR_NULL
,
744 XFS_BTNUM_BMAP
, 1, 1, ip
->i_ino
,
745 XFS_BTREE_LONG_PTRS
);
747 * Need a cursor. Can't allocate until bb_level is filled in.
749 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
750 cur
->bc_private
.b
.firstblock
= *firstblock
;
751 cur
->bc_private
.b
.dfops
= dfops
;
752 cur
->bc_private
.b
.flags
= wasdel
? XFS_BTCUR_BPRV_WASDEL
: 0;
754 * Convert to a btree with two levels, one record in root.
756 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_BTREE
);
757 memset(&args
, 0, sizeof(args
));
760 xfs_rmap_ino_bmbt_owner(&args
.oinfo
, ip
->i_ino
, whichfork
);
761 args
.firstblock
= *firstblock
;
762 if (*firstblock
== NULLFSBLOCK
) {
763 args
.type
= XFS_ALLOCTYPE_START_BNO
;
764 args
.fsbno
= XFS_INO_TO_FSB(mp
, ip
->i_ino
);
765 } else if (dfops
->dop_low
) {
767 args
.type
= XFS_ALLOCTYPE_START_BNO
;
768 args
.fsbno
= *firstblock
;
770 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
771 args
.fsbno
= *firstblock
;
773 args
.minlen
= args
.maxlen
= args
.prod
= 1;
774 args
.wasdel
= wasdel
;
776 if ((error
= xfs_alloc_vextent(&args
))) {
777 xfs_iroot_realloc(ip
, -1, whichfork
);
778 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
783 * During a CoW operation, the allocation and bmbt updates occur in
784 * different transactions. The mapping code tries to put new bmbt
785 * blocks near extents being mapped, but the only way to guarantee this
786 * is if the alloc and the mapping happen in a single transaction that
787 * has a block reservation. That isn't the case here, so if we run out
788 * of space we'll try again with another AG.
790 if (xfs_sb_version_hasreflink(&cur
->bc_mp
->m_sb
) &&
791 args
.fsbno
== NULLFSBLOCK
&&
792 args
.type
== XFS_ALLOCTYPE_NEAR_BNO
) {
793 dfops
->dop_low
= true;
797 * Allocation can't fail, the space was reserved.
799 ASSERT(args
.fsbno
!= NULLFSBLOCK
);
800 ASSERT(*firstblock
== NULLFSBLOCK
||
801 args
.agno
>= XFS_FSB_TO_AGNO(mp
, *firstblock
));
802 *firstblock
= cur
->bc_private
.b
.firstblock
= args
.fsbno
;
803 cur
->bc_private
.b
.allocated
++;
804 ip
->i_d
.di_nblocks
++;
805 xfs_trans_mod_dquot_byino(tp
, ip
, XFS_TRANS_DQ_BCOUNT
, 1L);
806 abp
= xfs_btree_get_bufl(mp
, tp
, args
.fsbno
, 0);
808 * Fill in the child block.
810 abp
->b_ops
= &xfs_bmbt_buf_ops
;
811 ablock
= XFS_BUF_TO_BLOCK(abp
);
812 xfs_btree_init_block_int(mp
, ablock
, abp
->b_bn
,
813 XFS_BTNUM_BMAP
, 0, 0, ip
->i_ino
,
814 XFS_BTREE_LONG_PTRS
);
816 arp
= XFS_BMBT_REC_ADDR(mp
, ablock
, 1);
817 nextents
= xfs_iext_count(ifp
);
818 for (cnt
= i
= 0; i
< nextents
; i
++) {
819 ep
= xfs_iext_get_ext(ifp
, i
);
820 if (!isnullstartblock(xfs_bmbt_get_startblock(ep
))) {
821 arp
->l0
= cpu_to_be64(ep
->l0
);
822 arp
->l1
= cpu_to_be64(ep
->l1
);
826 ASSERT(cnt
== XFS_IFORK_NEXTENTS(ip
, whichfork
));
827 xfs_btree_set_numrecs(ablock
, cnt
);
830 * Fill in the root key and pointer.
832 kp
= XFS_BMBT_KEY_ADDR(mp
, block
, 1);
833 arp
= XFS_BMBT_REC_ADDR(mp
, ablock
, 1);
834 kp
->br_startoff
= cpu_to_be64(xfs_bmbt_disk_get_startoff(arp
));
835 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, xfs_bmbt_get_maxrecs(cur
,
836 be16_to_cpu(block
->bb_level
)));
837 *pp
= cpu_to_be64(args
.fsbno
);
840 * Do all this logging at the end so that
841 * the root is at the right level.
843 xfs_btree_log_block(cur
, abp
, XFS_BB_ALL_BITS
);
844 xfs_btree_log_recs(cur
, abp
, 1, be16_to_cpu(ablock
->bb_numrecs
));
845 ASSERT(*curp
== NULL
);
847 *logflagsp
= XFS_ILOG_CORE
| xfs_ilog_fbroot(whichfork
);
852 * Convert a local file to an extents file.
853 * This code is out of bounds for data forks of regular files,
854 * since the file data needs to get logged so things will stay consistent.
855 * (The bmap-level manipulations are ok, though).
858 xfs_bmap_local_to_extents_empty(
859 struct xfs_inode
*ip
,
862 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
864 ASSERT(whichfork
!= XFS_COW_FORK
);
865 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
866 ASSERT(ifp
->if_bytes
== 0);
867 ASSERT(XFS_IFORK_NEXTENTS(ip
, whichfork
) == 0);
869 xfs_bmap_forkoff_reset(ip
, whichfork
);
870 ifp
->if_flags
&= ~XFS_IFINLINE
;
871 ifp
->if_flags
|= XFS_IFEXTENTS
;
872 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_EXTENTS
);
876 STATIC
int /* error */
877 xfs_bmap_local_to_extents(
878 xfs_trans_t
*tp
, /* transaction pointer */
879 xfs_inode_t
*ip
, /* incore inode pointer */
880 xfs_fsblock_t
*firstblock
, /* first block allocated in xaction */
881 xfs_extlen_t total
, /* total blocks needed by transaction */
882 int *logflagsp
, /* inode logging flags */
884 void (*init_fn
)(struct xfs_trans
*tp
,
886 struct xfs_inode
*ip
,
887 struct xfs_ifork
*ifp
))
890 int flags
; /* logging flags returned */
891 xfs_ifork_t
*ifp
; /* inode fork pointer */
892 xfs_alloc_arg_t args
; /* allocation arguments */
893 xfs_buf_t
*bp
; /* buffer for extent block */
894 xfs_bmbt_rec_host_t
*ep
; /* extent record pointer */
897 * We don't want to deal with the case of keeping inode data inline yet.
898 * So sending the data fork of a regular inode is invalid.
900 ASSERT(!(S_ISREG(VFS_I(ip
)->i_mode
) && whichfork
== XFS_DATA_FORK
));
901 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
902 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
904 if (!ifp
->if_bytes
) {
905 xfs_bmap_local_to_extents_empty(ip
, whichfork
);
906 flags
= XFS_ILOG_CORE
;
912 ASSERT((ifp
->if_flags
& (XFS_IFINLINE
|XFS_IFEXTENTS
|XFS_IFEXTIREC
)) ==
914 memset(&args
, 0, sizeof(args
));
916 args
.mp
= ip
->i_mount
;
917 xfs_rmap_ino_owner(&args
.oinfo
, ip
->i_ino
, whichfork
, 0);
918 args
.firstblock
= *firstblock
;
920 * Allocate a block. We know we need only one, since the
921 * file currently fits in an inode.
923 if (*firstblock
== NULLFSBLOCK
) {
925 args
.fsbno
= XFS_INO_TO_FSB(args
.mp
, ip
->i_ino
);
926 args
.type
= XFS_ALLOCTYPE_START_BNO
;
928 args
.fsbno
= *firstblock
;
929 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
932 args
.minlen
= args
.maxlen
= args
.prod
= 1;
933 error
= xfs_alloc_vextent(&args
);
938 * During a CoW operation, the allocation and bmbt updates occur in
939 * different transactions. The mapping code tries to put new bmbt
940 * blocks near extents being mapped, but the only way to guarantee this
941 * is if the alloc and the mapping happen in a single transaction that
942 * has a block reservation. That isn't the case here, so if we run out
943 * of space we'll try again with another AG.
945 if (xfs_sb_version_hasreflink(&ip
->i_mount
->m_sb
) &&
946 args
.fsbno
== NULLFSBLOCK
&&
947 args
.type
== XFS_ALLOCTYPE_NEAR_BNO
) {
950 /* Can't fail, the space was reserved. */
951 ASSERT(args
.fsbno
!= NULLFSBLOCK
);
952 ASSERT(args
.len
== 1);
953 *firstblock
= args
.fsbno
;
954 bp
= xfs_btree_get_bufl(args
.mp
, tp
, args
.fsbno
, 0);
957 * Initialize the block, copy the data and log the remote buffer.
959 * The callout is responsible for logging because the remote format
960 * might differ from the local format and thus we don't know how much to
961 * log here. Note that init_fn must also set the buffer log item type
964 init_fn(tp
, bp
, ip
, ifp
);
966 /* account for the change in fork size */
967 xfs_idata_realloc(ip
, -ifp
->if_bytes
, whichfork
);
968 xfs_bmap_local_to_extents_empty(ip
, whichfork
);
969 flags
|= XFS_ILOG_CORE
;
971 xfs_iext_add(ifp
, 0, 1);
972 ep
= xfs_iext_get_ext(ifp
, 0);
973 xfs_bmbt_set_allf(ep
, 0, args
.fsbno
, 1, XFS_EXT_NORM
);
974 trace_xfs_bmap_post_update(ip
, 0,
975 whichfork
== XFS_ATTR_FORK
? BMAP_ATTRFORK
: 0,
977 XFS_IFORK_NEXT_SET(ip
, whichfork
, 1);
978 ip
->i_d
.di_nblocks
= 1;
979 xfs_trans_mod_dquot_byino(tp
, ip
,
980 XFS_TRANS_DQ_BCOUNT
, 1L);
981 flags
|= xfs_ilog_fext(whichfork
);
989 * Called from xfs_bmap_add_attrfork to handle btree format files.
991 STATIC
int /* error */
992 xfs_bmap_add_attrfork_btree(
993 xfs_trans_t
*tp
, /* transaction pointer */
994 xfs_inode_t
*ip
, /* incore inode pointer */
995 xfs_fsblock_t
*firstblock
, /* first block allocated */
996 struct xfs_defer_ops
*dfops
, /* blocks to free at commit */
997 int *flags
) /* inode logging flags */
999 xfs_btree_cur_t
*cur
; /* btree cursor */
1000 int error
; /* error return value */
1001 xfs_mount_t
*mp
; /* file system mount struct */
1002 int stat
; /* newroot status */
1005 if (ip
->i_df
.if_broot_bytes
<= XFS_IFORK_DSIZE(ip
))
1006 *flags
|= XFS_ILOG_DBROOT
;
1008 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, XFS_DATA_FORK
);
1009 cur
->bc_private
.b
.dfops
= dfops
;
1010 cur
->bc_private
.b
.firstblock
= *firstblock
;
1011 if ((error
= xfs_bmbt_lookup_ge(cur
, 0, 0, 0, &stat
)))
1013 /* must be at least one entry */
1014 XFS_WANT_CORRUPTED_GOTO(mp
, stat
== 1, error0
);
1015 if ((error
= xfs_btree_new_iroot(cur
, flags
, &stat
)))
1018 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
1021 *firstblock
= cur
->bc_private
.b
.firstblock
;
1022 cur
->bc_private
.b
.allocated
= 0;
1023 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
1027 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
1032 * Called from xfs_bmap_add_attrfork to handle extents format files.
1034 STATIC
int /* error */
1035 xfs_bmap_add_attrfork_extents(
1036 xfs_trans_t
*tp
, /* transaction pointer */
1037 xfs_inode_t
*ip
, /* incore inode pointer */
1038 xfs_fsblock_t
*firstblock
, /* first block allocated */
1039 struct xfs_defer_ops
*dfops
, /* blocks to free at commit */
1040 int *flags
) /* inode logging flags */
1042 xfs_btree_cur_t
*cur
; /* bmap btree cursor */
1043 int error
; /* error return value */
1045 if (ip
->i_d
.di_nextents
* sizeof(xfs_bmbt_rec_t
) <= XFS_IFORK_DSIZE(ip
))
1048 error
= xfs_bmap_extents_to_btree(tp
, ip
, firstblock
, dfops
, &cur
, 0,
1049 flags
, XFS_DATA_FORK
);
1051 cur
->bc_private
.b
.allocated
= 0;
1052 xfs_btree_del_cursor(cur
,
1053 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
1059 * Called from xfs_bmap_add_attrfork to handle local format files. Each
1060 * different data fork content type needs a different callout to do the
1061 * conversion. Some are basic and only require special block initialisation
1062 * callouts for the data formating, others (directories) are so specialised they
1063 * handle everything themselves.
1065 * XXX (dgc): investigate whether directory conversion can use the generic
1066 * formatting callout. It should be possible - it's just a very complex
1069 STATIC
int /* error */
1070 xfs_bmap_add_attrfork_local(
1071 xfs_trans_t
*tp
, /* transaction pointer */
1072 xfs_inode_t
*ip
, /* incore inode pointer */
1073 xfs_fsblock_t
*firstblock
, /* first block allocated */
1074 struct xfs_defer_ops
*dfops
, /* blocks to free at commit */
1075 int *flags
) /* inode logging flags */
1077 xfs_da_args_t dargs
; /* args for dir/attr code */
1079 if (ip
->i_df
.if_bytes
<= XFS_IFORK_DSIZE(ip
))
1082 if (S_ISDIR(VFS_I(ip
)->i_mode
)) {
1083 memset(&dargs
, 0, sizeof(dargs
));
1084 dargs
.geo
= ip
->i_mount
->m_dir_geo
;
1086 dargs
.firstblock
= firstblock
;
1087 dargs
.dfops
= dfops
;
1088 dargs
.total
= dargs
.geo
->fsbcount
;
1089 dargs
.whichfork
= XFS_DATA_FORK
;
1091 return xfs_dir2_sf_to_block(&dargs
);
1094 if (S_ISLNK(VFS_I(ip
)->i_mode
))
1095 return xfs_bmap_local_to_extents(tp
, ip
, firstblock
, 1,
1096 flags
, XFS_DATA_FORK
,
1097 xfs_symlink_local_to_remote
);
1099 /* should only be called for types that support local format data */
1101 return -EFSCORRUPTED
;
1105 * Convert inode from non-attributed to attributed.
1106 * Must not be in a transaction, ip must not be locked.
1108 int /* error code */
1109 xfs_bmap_add_attrfork(
1110 xfs_inode_t
*ip
, /* incore inode pointer */
1111 int size
, /* space new attribute needs */
1112 int rsvd
) /* xact may use reserved blks */
1114 xfs_fsblock_t firstblock
; /* 1st block/ag allocated */
1115 struct xfs_defer_ops dfops
; /* freed extent records */
1116 xfs_mount_t
*mp
; /* mount structure */
1117 xfs_trans_t
*tp
; /* transaction pointer */
1118 int blks
; /* space reservation */
1119 int version
= 1; /* superblock attr version */
1120 int logflags
; /* logging flags */
1121 int error
; /* error return value */
1123 ASSERT(XFS_IFORK_Q(ip
) == 0);
1126 ASSERT(!XFS_NOT_DQATTACHED(mp
, ip
));
1128 blks
= XFS_ADDAFORK_SPACE_RES(mp
);
1130 error
= xfs_trans_alloc(mp
, &M_RES(mp
)->tr_addafork
, blks
, 0,
1131 rsvd
? XFS_TRANS_RESERVE
: 0, &tp
);
1135 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
1136 error
= xfs_trans_reserve_quota_nblks(tp
, ip
, blks
, 0, rsvd
?
1137 XFS_QMOPT_RES_REGBLKS
| XFS_QMOPT_FORCE_RES
:
1138 XFS_QMOPT_RES_REGBLKS
);
1141 if (XFS_IFORK_Q(ip
))
1143 if (ip
->i_d
.di_anextents
!= 0) {
1144 error
= -EFSCORRUPTED
;
1147 if (ip
->i_d
.di_aformat
!= XFS_DINODE_FMT_EXTENTS
) {
1149 * For inodes coming from pre-6.2 filesystems.
1151 ASSERT(ip
->i_d
.di_aformat
== 0);
1152 ip
->i_d
.di_aformat
= XFS_DINODE_FMT_EXTENTS
;
1155 xfs_trans_ijoin(tp
, ip
, 0);
1156 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
1158 switch (ip
->i_d
.di_format
) {
1159 case XFS_DINODE_FMT_DEV
:
1160 ip
->i_d
.di_forkoff
= roundup(sizeof(xfs_dev_t
), 8) >> 3;
1162 case XFS_DINODE_FMT_UUID
:
1163 ip
->i_d
.di_forkoff
= roundup(sizeof(uuid_t
), 8) >> 3;
1165 case XFS_DINODE_FMT_LOCAL
:
1166 case XFS_DINODE_FMT_EXTENTS
:
1167 case XFS_DINODE_FMT_BTREE
:
1168 ip
->i_d
.di_forkoff
= xfs_attr_shortform_bytesfit(ip
, size
);
1169 if (!ip
->i_d
.di_forkoff
)
1170 ip
->i_d
.di_forkoff
= xfs_default_attroffset(ip
) >> 3;
1171 else if (mp
->m_flags
& XFS_MOUNT_ATTR2
)
1180 ASSERT(ip
->i_afp
== NULL
);
1181 ip
->i_afp
= kmem_zone_zalloc(xfs_ifork_zone
, KM_SLEEP
);
1182 ip
->i_afp
->if_flags
= XFS_IFEXTENTS
;
1184 xfs_defer_init(&dfops
, &firstblock
);
1185 switch (ip
->i_d
.di_format
) {
1186 case XFS_DINODE_FMT_LOCAL
:
1187 error
= xfs_bmap_add_attrfork_local(tp
, ip
, &firstblock
, &dfops
,
1190 case XFS_DINODE_FMT_EXTENTS
:
1191 error
= xfs_bmap_add_attrfork_extents(tp
, ip
, &firstblock
,
1194 case XFS_DINODE_FMT_BTREE
:
1195 error
= xfs_bmap_add_attrfork_btree(tp
, ip
, &firstblock
, &dfops
,
1203 xfs_trans_log_inode(tp
, ip
, logflags
);
1206 if (!xfs_sb_version_hasattr(&mp
->m_sb
) ||
1207 (!xfs_sb_version_hasattr2(&mp
->m_sb
) && version
== 2)) {
1208 bool log_sb
= false;
1210 spin_lock(&mp
->m_sb_lock
);
1211 if (!xfs_sb_version_hasattr(&mp
->m_sb
)) {
1212 xfs_sb_version_addattr(&mp
->m_sb
);
1215 if (!xfs_sb_version_hasattr2(&mp
->m_sb
) && version
== 2) {
1216 xfs_sb_version_addattr2(&mp
->m_sb
);
1219 spin_unlock(&mp
->m_sb_lock
);
1224 error
= xfs_defer_finish(&tp
, &dfops
, NULL
);
1227 error
= xfs_trans_commit(tp
);
1228 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
1232 xfs_defer_cancel(&dfops
);
1234 xfs_trans_cancel(tp
);
1235 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
1240 * Internal and external extent tree search functions.
1244 * Read in the extents to if_extents.
1245 * All inode fields are set up by caller, we just traverse the btree
1246 * and copy the records in. If the file system cannot contain unwritten
1247 * extents, the records are checked for no "state" flags.
1250 xfs_bmap_read_extents(
1251 xfs_trans_t
*tp
, /* transaction pointer */
1252 xfs_inode_t
*ip
, /* incore inode */
1253 int whichfork
) /* data or attr fork */
1255 struct xfs_btree_block
*block
; /* current btree block */
1256 xfs_fsblock_t bno
; /* block # of "block" */
1257 xfs_buf_t
*bp
; /* buffer for "block" */
1258 int error
; /* error return value */
1259 xfs_exntfmt_t exntf
; /* XFS_EXTFMT_NOSTATE, if checking */
1260 xfs_extnum_t i
, j
; /* index into the extents list */
1261 xfs_ifork_t
*ifp
; /* fork structure */
1262 int level
; /* btree level, for checking */
1263 xfs_mount_t
*mp
; /* file system mount structure */
1264 __be64
*pp
; /* pointer to block address */
1266 xfs_extnum_t room
; /* number of entries there's room for */
1269 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1270 exntf
= (whichfork
!= XFS_DATA_FORK
) ? XFS_EXTFMT_NOSTATE
:
1271 XFS_EXTFMT_INODE(ip
);
1272 block
= ifp
->if_broot
;
1274 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
1276 level
= be16_to_cpu(block
->bb_level
);
1278 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, 1, ifp
->if_broot_bytes
);
1279 bno
= be64_to_cpu(*pp
);
1282 * Go down the tree until leaf level is reached, following the first
1283 * pointer (leftmost) at each level.
1285 while (level
-- > 0) {
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 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, mp
->m_bmap_dmxr
[1]);
1294 bno
= be64_to_cpu(*pp
);
1295 XFS_WANT_CORRUPTED_GOTO(mp
,
1296 XFS_FSB_SANITY_CHECK(mp
, bno
), error0
);
1297 xfs_trans_brelse(tp
, bp
);
1300 * Here with bp and block set to the leftmost leaf node in the tree.
1302 room
= xfs_iext_count(ifp
);
1305 * Loop over all leaf nodes. Copy information to the extent records.
1308 xfs_bmbt_rec_t
*frp
;
1309 xfs_fsblock_t nextbno
;
1310 xfs_extnum_t num_recs
;
1313 num_recs
= xfs_btree_get_numrecs(block
);
1314 if (unlikely(i
+ num_recs
> room
)) {
1315 ASSERT(i
+ num_recs
<= room
);
1316 xfs_warn(ip
->i_mount
,
1317 "corrupt dinode %Lu, (btree extents).",
1318 (unsigned long long) ip
->i_ino
);
1319 XFS_CORRUPTION_ERROR("xfs_bmap_read_extents(1)",
1320 XFS_ERRLEVEL_LOW
, ip
->i_mount
, block
);
1324 * Read-ahead the next leaf block, if any.
1326 nextbno
= be64_to_cpu(block
->bb_u
.l
.bb_rightsib
);
1327 if (nextbno
!= NULLFSBLOCK
)
1328 xfs_btree_reada_bufl(mp
, nextbno
, 1,
1331 * Copy records into the extent records.
1333 frp
= XFS_BMBT_REC_ADDR(mp
, block
, 1);
1335 for (j
= 0; j
< num_recs
; j
++, i
++, frp
++) {
1336 xfs_bmbt_rec_host_t
*trp
= xfs_iext_get_ext(ifp
, i
);
1337 trp
->l0
= be64_to_cpu(frp
->l0
);
1338 trp
->l1
= be64_to_cpu(frp
->l1
);
1340 if (exntf
== XFS_EXTFMT_NOSTATE
) {
1342 * Check all attribute bmap btree records and
1343 * any "older" data bmap btree records for a
1344 * set bit in the "extent flag" position.
1346 if (unlikely(xfs_check_nostate_extents(ifp
,
1347 start
, num_recs
))) {
1348 XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
1354 xfs_trans_brelse(tp
, bp
);
1357 * If we've reached the end, stop.
1359 if (bno
== NULLFSBLOCK
)
1361 error
= xfs_btree_read_bufl(mp
, tp
, bno
, 0, &bp
,
1362 XFS_BMAP_BTREE_REF
, &xfs_bmbt_buf_ops
);
1365 block
= XFS_BUF_TO_BLOCK(bp
);
1367 if (i
!= XFS_IFORK_NEXTENTS(ip
, whichfork
))
1368 return -EFSCORRUPTED
;
1369 ASSERT(i
== xfs_iext_count(ifp
));
1370 XFS_BMAP_TRACE_EXLIST(ip
, i
, whichfork
);
1373 xfs_trans_brelse(tp
, bp
);
1374 return -EFSCORRUPTED
;
1378 * Returns the file-relative block number of the first unused block(s)
1379 * in the file with at least "len" logically contiguous blocks free.
1380 * This is the lowest-address hole if the file has holes, else the first block
1381 * past the end of file.
1382 * Return 0 if the file is currently local (in-inode).
1385 xfs_bmap_first_unused(
1386 xfs_trans_t
*tp
, /* transaction pointer */
1387 xfs_inode_t
*ip
, /* incore inode */
1388 xfs_extlen_t len
, /* size of hole to find */
1389 xfs_fileoff_t
*first_unused
, /* unused block */
1390 int whichfork
) /* data or attr fork */
1392 int error
; /* error return value */
1393 int idx
; /* extent record index */
1394 xfs_ifork_t
*ifp
; /* inode fork pointer */
1395 xfs_fileoff_t lastaddr
; /* last block number seen */
1396 xfs_fileoff_t lowest
; /* lowest useful block */
1397 xfs_fileoff_t max
; /* starting useful block */
1398 xfs_fileoff_t off
; /* offset for this block */
1399 xfs_extnum_t nextents
; /* number of extent entries */
1401 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
||
1402 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
||
1403 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
1404 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
) {
1408 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1409 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
1410 (error
= xfs_iread_extents(tp
, ip
, whichfork
)))
1412 lowest
= *first_unused
;
1413 nextents
= xfs_iext_count(ifp
);
1414 for (idx
= 0, lastaddr
= 0, max
= lowest
; idx
< nextents
; idx
++) {
1415 xfs_bmbt_rec_host_t
*ep
= xfs_iext_get_ext(ifp
, idx
);
1416 off
= xfs_bmbt_get_startoff(ep
);
1418 * See if the hole before this extent will work.
1420 if (off
>= lowest
+ len
&& off
- max
>= len
) {
1421 *first_unused
= max
;
1424 lastaddr
= off
+ xfs_bmbt_get_blockcount(ep
);
1425 max
= XFS_FILEOFF_MAX(lastaddr
, lowest
);
1427 *first_unused
= max
;
1432 * Returns the file-relative block number of the last block - 1 before
1433 * last_block (input value) in the file.
1434 * This is not based on i_size, it is based on the extent records.
1435 * Returns 0 for local files, as they do not have extent records.
1438 xfs_bmap_last_before(
1439 struct xfs_trans
*tp
, /* transaction pointer */
1440 struct xfs_inode
*ip
, /* incore inode */
1441 xfs_fileoff_t
*last_block
, /* last block */
1442 int whichfork
) /* data or attr fork */
1444 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1445 struct xfs_bmbt_irec got
;
1449 switch (XFS_IFORK_FORMAT(ip
, whichfork
)) {
1450 case XFS_DINODE_FMT_LOCAL
:
1453 case XFS_DINODE_FMT_BTREE
:
1454 case XFS_DINODE_FMT_EXTENTS
:
1460 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
1461 error
= xfs_iread_extents(tp
, ip
, whichfork
);
1466 if (xfs_iext_lookup_extent(ip
, ifp
, *last_block
- 1, &idx
, &got
)) {
1467 if (got
.br_startoff
<= *last_block
- 1)
1471 if (xfs_iext_get_extent(ifp
, idx
- 1, &got
)) {
1472 *last_block
= got
.br_startoff
+ got
.br_blockcount
;
1481 xfs_bmap_last_extent(
1482 struct xfs_trans
*tp
,
1483 struct xfs_inode
*ip
,
1485 struct xfs_bmbt_irec
*rec
,
1488 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1492 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
1493 error
= xfs_iread_extents(tp
, ip
, whichfork
);
1498 nextents
= xfs_iext_count(ifp
);
1499 if (nextents
== 0) {
1504 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, nextents
- 1), rec
);
1510 * Check the last inode extent to determine whether this allocation will result
1511 * in blocks being allocated at the end of the file. When we allocate new data
1512 * blocks at the end of the file which do not start at the previous data block,
1513 * we will try to align the new blocks at stripe unit boundaries.
1515 * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be
1516 * at, or past the EOF.
1520 struct xfs_bmalloca
*bma
,
1523 struct xfs_bmbt_irec rec
;
1528 error
= xfs_bmap_last_extent(NULL
, bma
->ip
, whichfork
, &rec
,
1539 * Check if we are allocation or past the last extent, or at least into
1540 * the last delayed allocated extent.
1542 bma
->aeof
= bma
->offset
>= rec
.br_startoff
+ rec
.br_blockcount
||
1543 (bma
->offset
>= rec
.br_startoff
&&
1544 isnullstartblock(rec
.br_startblock
));
1549 * Returns the file-relative block number of the first block past eof in
1550 * the file. This is not based on i_size, it is based on the extent records.
1551 * Returns 0 for local files, as they do not have extent records.
1554 xfs_bmap_last_offset(
1555 struct xfs_inode
*ip
,
1556 xfs_fileoff_t
*last_block
,
1559 struct xfs_bmbt_irec rec
;
1565 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
)
1568 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
&&
1569 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
1572 error
= xfs_bmap_last_extent(NULL
, ip
, whichfork
, &rec
, &is_empty
);
1573 if (error
|| is_empty
)
1576 *last_block
= rec
.br_startoff
+ rec
.br_blockcount
;
1581 * Returns whether the selected fork of the inode has exactly one
1582 * block or not. For the data fork we check this matches di_size,
1583 * implying the file's range is 0..bsize-1.
1585 int /* 1=>1 block, 0=>otherwise */
1587 xfs_inode_t
*ip
, /* incore inode */
1588 int whichfork
) /* data or attr fork */
1590 xfs_bmbt_rec_host_t
*ep
; /* ptr to fork's extent */
1591 xfs_ifork_t
*ifp
; /* inode fork pointer */
1592 int rval
; /* return value */
1593 xfs_bmbt_irec_t s
; /* internal version of extent */
1596 if (whichfork
== XFS_DATA_FORK
)
1597 return XFS_ISIZE(ip
) == ip
->i_mount
->m_sb
.sb_blocksize
;
1599 if (XFS_IFORK_NEXTENTS(ip
, whichfork
) != 1)
1601 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
1603 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1604 ASSERT(ifp
->if_flags
& XFS_IFEXTENTS
);
1605 ep
= xfs_iext_get_ext(ifp
, 0);
1606 xfs_bmbt_get_all(ep
, &s
);
1607 rval
= s
.br_startoff
== 0 && s
.br_blockcount
== 1;
1608 if (rval
&& whichfork
== XFS_DATA_FORK
)
1609 ASSERT(XFS_ISIZE(ip
) == ip
->i_mount
->m_sb
.sb_blocksize
);
1614 * Extent tree manipulation functions used during allocation.
1618 * Convert a delayed allocation to a real allocation.
1620 STATIC
int /* error */
1621 xfs_bmap_add_extent_delay_real(
1622 struct xfs_bmalloca
*bma
,
1625 struct xfs_bmbt_irec
*new = &bma
->got
;
1626 int diff
; /* temp value */
1627 xfs_bmbt_rec_host_t
*ep
; /* extent entry for idx */
1628 int error
; /* error return value */
1629 int i
; /* temp state */
1630 xfs_ifork_t
*ifp
; /* inode fork pointer */
1631 xfs_fileoff_t new_endoff
; /* end offset of new entry */
1632 xfs_bmbt_irec_t r
[3]; /* neighbor extent entries */
1633 /* left is 0, right is 1, prev is 2 */
1634 int rval
=0; /* return value (logging flags) */
1635 int state
= 0;/* state bits, accessed thru macros */
1636 xfs_filblks_t da_new
; /* new count del alloc blocks used */
1637 xfs_filblks_t da_old
; /* old count del alloc blocks used */
1638 xfs_filblks_t temp
=0; /* value for da_new calculations */
1639 xfs_filblks_t temp2
=0;/* value for da_new calculations */
1640 int tmp_rval
; /* partial logging flags */
1641 struct xfs_mount
*mp
;
1642 xfs_extnum_t
*nextents
;
1644 mp
= bma
->ip
->i_mount
;
1645 ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
1646 ASSERT(whichfork
!= XFS_ATTR_FORK
);
1647 nextents
= (whichfork
== XFS_COW_FORK
? &bma
->ip
->i_cnextents
:
1648 &bma
->ip
->i_d
.di_nextents
);
1650 ASSERT(bma
->idx
>= 0);
1651 ASSERT(bma
->idx
<= xfs_iext_count(ifp
));
1652 ASSERT(!isnullstartblock(new->br_startblock
));
1654 (bma
->cur
->bc_private
.b
.flags
& XFS_BTCUR_BPRV_WASDEL
));
1656 XFS_STATS_INC(mp
, xs_add_exlist
);
1662 if (whichfork
== XFS_COW_FORK
)
1663 state
|= BMAP_COWFORK
;
1666 * Set up a bunch of variables to make the tests simpler.
1668 ep
= xfs_iext_get_ext(ifp
, bma
->idx
);
1669 xfs_bmbt_get_all(ep
, &PREV
);
1670 new_endoff
= new->br_startoff
+ new->br_blockcount
;
1671 ASSERT(PREV
.br_startoff
<= new->br_startoff
);
1672 ASSERT(PREV
.br_startoff
+ PREV
.br_blockcount
>= new_endoff
);
1674 da_old
= startblockval(PREV
.br_startblock
);
1678 * Set flags determining what part of the previous delayed allocation
1679 * extent is being replaced by a real allocation.
1681 if (PREV
.br_startoff
== new->br_startoff
)
1682 state
|= BMAP_LEFT_FILLING
;
1683 if (PREV
.br_startoff
+ PREV
.br_blockcount
== new_endoff
)
1684 state
|= BMAP_RIGHT_FILLING
;
1687 * Check and set flags if this segment has a left neighbor.
1688 * Don't set contiguous if the combined extent would be too large.
1691 state
|= BMAP_LEFT_VALID
;
1692 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
- 1), &LEFT
);
1694 if (isnullstartblock(LEFT
.br_startblock
))
1695 state
|= BMAP_LEFT_DELAY
;
1698 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
1699 LEFT
.br_startoff
+ LEFT
.br_blockcount
== new->br_startoff
&&
1700 LEFT
.br_startblock
+ LEFT
.br_blockcount
== new->br_startblock
&&
1701 LEFT
.br_state
== new->br_state
&&
1702 LEFT
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
1703 state
|= BMAP_LEFT_CONTIG
;
1706 * Check and set flags if this segment has a right neighbor.
1707 * Don't set contiguous if the combined extent would be too large.
1708 * Also check for all-three-contiguous being too large.
1710 if (bma
->idx
< xfs_iext_count(ifp
) - 1) {
1711 state
|= BMAP_RIGHT_VALID
;
1712 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
+ 1), &RIGHT
);
1714 if (isnullstartblock(RIGHT
.br_startblock
))
1715 state
|= BMAP_RIGHT_DELAY
;
1718 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
1719 new_endoff
== RIGHT
.br_startoff
&&
1720 new->br_startblock
+ new->br_blockcount
== RIGHT
.br_startblock
&&
1721 new->br_state
== RIGHT
.br_state
&&
1722 new->br_blockcount
+ RIGHT
.br_blockcount
<= MAXEXTLEN
&&
1723 ((state
& (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
1724 BMAP_RIGHT_FILLING
)) !=
1725 (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
1726 BMAP_RIGHT_FILLING
) ||
1727 LEFT
.br_blockcount
+ new->br_blockcount
+ RIGHT
.br_blockcount
1729 state
|= BMAP_RIGHT_CONTIG
;
1733 * Switch out based on the FILLING and CONTIG state bits.
1735 switch (state
& (BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
1736 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
)) {
1737 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
1738 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
1740 * Filling in all of a previously delayed allocation extent.
1741 * The left and right neighbors are both contiguous with new.
1744 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1745 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, bma
->idx
),
1746 LEFT
.br_blockcount
+ PREV
.br_blockcount
+
1747 RIGHT
.br_blockcount
);
1748 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1750 xfs_iext_remove(bma
->ip
, bma
->idx
+ 1, 2, state
);
1752 if (bma
->cur
== NULL
)
1753 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1755 rval
= XFS_ILOG_CORE
;
1756 error
= xfs_bmbt_lookup_eq(bma
->cur
, RIGHT
.br_startoff
,
1757 RIGHT
.br_startblock
,
1758 RIGHT
.br_blockcount
, &i
);
1761 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1762 error
= xfs_btree_delete(bma
->cur
, &i
);
1765 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1766 error
= xfs_btree_decrement(bma
->cur
, 0, &i
);
1769 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1770 error
= xfs_bmbt_update(bma
->cur
, LEFT
.br_startoff
,
1772 LEFT
.br_blockcount
+
1773 PREV
.br_blockcount
+
1774 RIGHT
.br_blockcount
, LEFT
.br_state
);
1780 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
1782 * Filling in all of a previously delayed allocation extent.
1783 * The left neighbor is contiguous, the right is not.
1787 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1788 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, bma
->idx
),
1789 LEFT
.br_blockcount
+ PREV
.br_blockcount
);
1790 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1792 xfs_iext_remove(bma
->ip
, bma
->idx
+ 1, 1, state
);
1793 if (bma
->cur
== NULL
)
1794 rval
= XFS_ILOG_DEXT
;
1797 error
= xfs_bmbt_lookup_eq(bma
->cur
, LEFT
.br_startoff
,
1798 LEFT
.br_startblock
, LEFT
.br_blockcount
,
1802 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1803 error
= xfs_bmbt_update(bma
->cur
, LEFT
.br_startoff
,
1805 LEFT
.br_blockcount
+
1806 PREV
.br_blockcount
, LEFT
.br_state
);
1812 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
1814 * Filling in all of a previously delayed allocation extent.
1815 * The right neighbor is contiguous, the left is not.
1817 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1818 xfs_bmbt_set_startblock(ep
, new->br_startblock
);
1819 xfs_bmbt_set_blockcount(ep
,
1820 PREV
.br_blockcount
+ RIGHT
.br_blockcount
);
1821 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1823 xfs_iext_remove(bma
->ip
, bma
->idx
+ 1, 1, state
);
1824 if (bma
->cur
== NULL
)
1825 rval
= XFS_ILOG_DEXT
;
1828 error
= xfs_bmbt_lookup_eq(bma
->cur
, RIGHT
.br_startoff
,
1829 RIGHT
.br_startblock
,
1830 RIGHT
.br_blockcount
, &i
);
1833 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1834 error
= xfs_bmbt_update(bma
->cur
, PREV
.br_startoff
,
1836 PREV
.br_blockcount
+
1837 RIGHT
.br_blockcount
, PREV
.br_state
);
1843 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
1845 * Filling in all of a previously delayed allocation extent.
1846 * Neither the left nor right neighbors are contiguous with
1849 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1850 xfs_bmbt_set_startblock(ep
, new->br_startblock
);
1851 xfs_bmbt_set_state(ep
, new->br_state
);
1852 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1855 if (bma
->cur
== NULL
)
1856 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1858 rval
= XFS_ILOG_CORE
;
1859 error
= xfs_bmbt_lookup_eq(bma
->cur
, new->br_startoff
,
1860 new->br_startblock
, new->br_blockcount
,
1864 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
1865 bma
->cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
1866 error
= xfs_btree_insert(bma
->cur
, &i
);
1869 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1873 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
:
1875 * Filling in the first part of a previous delayed allocation.
1876 * The left neighbor is contiguous.
1878 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
- 1, state
, _THIS_IP_
);
1879 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, bma
->idx
- 1),
1880 LEFT
.br_blockcount
+ new->br_blockcount
);
1881 xfs_bmbt_set_startoff(ep
,
1882 PREV
.br_startoff
+ new->br_blockcount
);
1883 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
- 1, state
, _THIS_IP_
);
1885 temp
= PREV
.br_blockcount
- new->br_blockcount
;
1886 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1887 xfs_bmbt_set_blockcount(ep
, temp
);
1888 if (bma
->cur
== NULL
)
1889 rval
= XFS_ILOG_DEXT
;
1892 error
= xfs_bmbt_lookup_eq(bma
->cur
, LEFT
.br_startoff
,
1893 LEFT
.br_startblock
, LEFT
.br_blockcount
,
1897 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1898 error
= xfs_bmbt_update(bma
->cur
, LEFT
.br_startoff
,
1900 LEFT
.br_blockcount
+
1906 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
1907 startblockval(PREV
.br_startblock
));
1908 xfs_bmbt_set_startblock(ep
, nullstartblock(da_new
));
1909 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1914 case BMAP_LEFT_FILLING
:
1916 * Filling in the first part of a previous delayed allocation.
1917 * The left neighbor is not contiguous.
1919 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1920 xfs_bmbt_set_startoff(ep
, new_endoff
);
1921 temp
= PREV
.br_blockcount
- new->br_blockcount
;
1922 xfs_bmbt_set_blockcount(ep
, temp
);
1923 xfs_iext_insert(bma
->ip
, bma
->idx
, 1, new, state
);
1925 if (bma
->cur
== NULL
)
1926 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1928 rval
= XFS_ILOG_CORE
;
1929 error
= xfs_bmbt_lookup_eq(bma
->cur
, new->br_startoff
,
1930 new->br_startblock
, new->br_blockcount
,
1934 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
1935 bma
->cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
1936 error
= xfs_btree_insert(bma
->cur
, &i
);
1939 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1942 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
1943 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
1944 bma
->firstblock
, bma
->dfops
,
1945 &bma
->cur
, 1, &tmp_rval
, whichfork
);
1950 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
1951 startblockval(PREV
.br_startblock
) -
1952 (bma
->cur
? bma
->cur
->bc_private
.b
.allocated
: 0));
1953 ep
= xfs_iext_get_ext(ifp
, bma
->idx
+ 1);
1954 xfs_bmbt_set_startblock(ep
, nullstartblock(da_new
));
1955 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
+ 1, state
, _THIS_IP_
);
1958 case BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
1960 * Filling in the last part of a previous delayed allocation.
1961 * The right neighbor is contiguous with the new allocation.
1963 temp
= PREV
.br_blockcount
- new->br_blockcount
;
1964 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
+ 1, state
, _THIS_IP_
);
1965 xfs_bmbt_set_blockcount(ep
, temp
);
1966 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp
, bma
->idx
+ 1),
1967 new->br_startoff
, new->br_startblock
,
1968 new->br_blockcount
+ RIGHT
.br_blockcount
,
1970 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
+ 1, state
, _THIS_IP_
);
1971 if (bma
->cur
== NULL
)
1972 rval
= XFS_ILOG_DEXT
;
1975 error
= xfs_bmbt_lookup_eq(bma
->cur
, RIGHT
.br_startoff
,
1976 RIGHT
.br_startblock
,
1977 RIGHT
.br_blockcount
, &i
);
1980 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1981 error
= xfs_bmbt_update(bma
->cur
, new->br_startoff
,
1983 new->br_blockcount
+
1984 RIGHT
.br_blockcount
,
1990 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
1991 startblockval(PREV
.br_startblock
));
1992 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1993 xfs_bmbt_set_startblock(ep
, nullstartblock(da_new
));
1994 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1999 case BMAP_RIGHT_FILLING
:
2001 * Filling in the last part of a previous delayed allocation.
2002 * The right neighbor is not contiguous.
2004 temp
= PREV
.br_blockcount
- new->br_blockcount
;
2005 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2006 xfs_bmbt_set_blockcount(ep
, temp
);
2007 xfs_iext_insert(bma
->ip
, bma
->idx
+ 1, 1, new, state
);
2009 if (bma
->cur
== NULL
)
2010 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2012 rval
= XFS_ILOG_CORE
;
2013 error
= xfs_bmbt_lookup_eq(bma
->cur
, new->br_startoff
,
2014 new->br_startblock
, new->br_blockcount
,
2018 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
2019 bma
->cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
2020 error
= xfs_btree_insert(bma
->cur
, &i
);
2023 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2026 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
2027 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
2028 bma
->firstblock
, bma
->dfops
, &bma
->cur
, 1,
2029 &tmp_rval
, whichfork
);
2034 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
2035 startblockval(PREV
.br_startblock
) -
2036 (bma
->cur
? bma
->cur
->bc_private
.b
.allocated
: 0));
2037 ep
= xfs_iext_get_ext(ifp
, bma
->idx
);
2038 xfs_bmbt_set_startblock(ep
, nullstartblock(da_new
));
2039 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2046 * Filling in the middle part of a previous delayed allocation.
2047 * Contiguity is impossible here.
2048 * This case is avoided almost all the time.
2050 * We start with a delayed allocation:
2052 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
2055 * and we are allocating:
2056 * +rrrrrrrrrrrrrrrrr+
2059 * and we set it up for insertion as:
2060 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
2062 * PREV @ idx LEFT RIGHT
2063 * inserted at idx + 1
2065 temp
= new->br_startoff
- PREV
.br_startoff
;
2066 temp2
= PREV
.br_startoff
+ PREV
.br_blockcount
- new_endoff
;
2067 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, 0, _THIS_IP_
);
2068 xfs_bmbt_set_blockcount(ep
, temp
); /* truncate PREV */
2070 RIGHT
.br_state
= PREV
.br_state
;
2071 RIGHT
.br_startblock
= nullstartblock(
2072 (int)xfs_bmap_worst_indlen(bma
->ip
, temp2
));
2073 RIGHT
.br_startoff
= new_endoff
;
2074 RIGHT
.br_blockcount
= temp2
;
2075 /* insert LEFT (r[0]) and RIGHT (r[1]) at the same time */
2076 xfs_iext_insert(bma
->ip
, bma
->idx
+ 1, 2, &LEFT
, state
);
2078 if (bma
->cur
== NULL
)
2079 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2081 rval
= XFS_ILOG_CORE
;
2082 error
= xfs_bmbt_lookup_eq(bma
->cur
, new->br_startoff
,
2083 new->br_startblock
, new->br_blockcount
,
2087 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
2088 bma
->cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
2089 error
= xfs_btree_insert(bma
->cur
, &i
);
2092 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2095 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
2096 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
2097 bma
->firstblock
, bma
->dfops
, &bma
->cur
,
2098 1, &tmp_rval
, whichfork
);
2103 temp
= xfs_bmap_worst_indlen(bma
->ip
, temp
);
2104 temp2
= xfs_bmap_worst_indlen(bma
->ip
, temp2
);
2105 diff
= (int)(temp
+ temp2
- startblockval(PREV
.br_startblock
) -
2106 (bma
->cur
? bma
->cur
->bc_private
.b
.allocated
: 0));
2108 error
= xfs_mod_fdblocks(bma
->ip
->i_mount
,
2109 -((int64_t)diff
), false);
2115 ep
= xfs_iext_get_ext(ifp
, bma
->idx
);
2116 xfs_bmbt_set_startblock(ep
, nullstartblock((int)temp
));
2117 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2118 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
+ 2, state
, _THIS_IP_
);
2119 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp
, bma
->idx
+ 2),
2120 nullstartblock((int)temp2
));
2121 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
+ 2, state
, _THIS_IP_
);
2124 da_new
= temp
+ temp2
;
2127 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2128 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2129 case BMAP_LEFT_FILLING
| BMAP_RIGHT_CONTIG
:
2130 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
2131 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2132 case BMAP_LEFT_CONTIG
:
2133 case BMAP_RIGHT_CONTIG
:
2135 * These cases are all impossible.
2140 /* add reverse mapping */
2141 error
= xfs_rmap_map_extent(mp
, bma
->dfops
, bma
->ip
, whichfork
, new);
2145 /* convert to a btree if necessary */
2146 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
2147 int tmp_logflags
; /* partial log flag return val */
2149 ASSERT(bma
->cur
== NULL
);
2150 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
2151 bma
->firstblock
, bma
->dfops
, &bma
->cur
,
2152 da_old
> 0, &tmp_logflags
, whichfork
);
2153 bma
->logflags
|= tmp_logflags
;
2158 /* adjust for changes in reserved delayed indirect blocks */
2159 if (da_old
|| da_new
) {
2162 temp
+= bma
->cur
->bc_private
.b
.allocated
;
2163 ASSERT(temp
<= da_old
);
2165 xfs_mod_fdblocks(bma
->ip
->i_mount
,
2166 (int64_t)(da_old
- temp
), false);
2169 /* clear out the allocated field, done with it now in any case. */
2171 bma
->cur
->bc_private
.b
.allocated
= 0;
2173 xfs_bmap_check_leaf_extents(bma
->cur
, bma
->ip
, whichfork
);
2175 if (whichfork
!= XFS_COW_FORK
)
2176 bma
->logflags
|= rval
;
2184 * Convert an unwritten allocation to a real allocation or vice versa.
2186 STATIC
int /* error */
2187 xfs_bmap_add_extent_unwritten_real(
2188 struct xfs_trans
*tp
,
2189 xfs_inode_t
*ip
, /* incore inode pointer */
2191 xfs_extnum_t
*idx
, /* extent number to update/insert */
2192 xfs_btree_cur_t
**curp
, /* if *curp is null, not a btree */
2193 xfs_bmbt_irec_t
*new, /* new data to add to file extents */
2194 xfs_fsblock_t
*first
, /* pointer to firstblock variable */
2195 struct xfs_defer_ops
*dfops
, /* list of extents to be freed */
2196 int *logflagsp
) /* inode logging flags */
2198 xfs_btree_cur_t
*cur
; /* btree cursor */
2199 xfs_bmbt_rec_host_t
*ep
; /* extent entry for idx */
2200 int error
; /* error return value */
2201 int i
; /* temp state */
2202 xfs_ifork_t
*ifp
; /* inode fork pointer */
2203 xfs_fileoff_t new_endoff
; /* end offset of new entry */
2204 xfs_exntst_t newext
; /* new extent state */
2205 xfs_exntst_t oldext
; /* old extent state */
2206 xfs_bmbt_irec_t r
[3]; /* neighbor extent entries */
2207 /* left is 0, right is 1, prev is 2 */
2208 int rval
=0; /* return value (logging flags) */
2209 int state
= 0;/* state bits, accessed thru macros */
2210 struct xfs_mount
*mp
= ip
->i_mount
;
2215 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
2216 if (whichfork
== XFS_COW_FORK
)
2217 state
|= BMAP_COWFORK
;
2220 ASSERT(*idx
<= xfs_iext_count(ifp
));
2221 ASSERT(!isnullstartblock(new->br_startblock
));
2223 XFS_STATS_INC(mp
, xs_add_exlist
);
2230 * Set up a bunch of variables to make the tests simpler.
2233 ep
= xfs_iext_get_ext(ifp
, *idx
);
2234 xfs_bmbt_get_all(ep
, &PREV
);
2235 newext
= new->br_state
;
2236 oldext
= (newext
== XFS_EXT_UNWRITTEN
) ?
2237 XFS_EXT_NORM
: XFS_EXT_UNWRITTEN
;
2238 ASSERT(PREV
.br_state
== oldext
);
2239 new_endoff
= new->br_startoff
+ new->br_blockcount
;
2240 ASSERT(PREV
.br_startoff
<= new->br_startoff
);
2241 ASSERT(PREV
.br_startoff
+ PREV
.br_blockcount
>= new_endoff
);
2244 * Set flags determining what part of the previous oldext allocation
2245 * extent is being replaced by a newext allocation.
2247 if (PREV
.br_startoff
== new->br_startoff
)
2248 state
|= BMAP_LEFT_FILLING
;
2249 if (PREV
.br_startoff
+ PREV
.br_blockcount
== new_endoff
)
2250 state
|= BMAP_RIGHT_FILLING
;
2253 * Check and set flags if this segment has a left neighbor.
2254 * Don't set contiguous if the combined extent would be too large.
2257 state
|= BMAP_LEFT_VALID
;
2258 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, *idx
- 1), &LEFT
);
2260 if (isnullstartblock(LEFT
.br_startblock
))
2261 state
|= BMAP_LEFT_DELAY
;
2264 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
2265 LEFT
.br_startoff
+ LEFT
.br_blockcount
== new->br_startoff
&&
2266 LEFT
.br_startblock
+ LEFT
.br_blockcount
== new->br_startblock
&&
2267 LEFT
.br_state
== newext
&&
2268 LEFT
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
2269 state
|= BMAP_LEFT_CONTIG
;
2272 * Check and set flags if this segment has a right neighbor.
2273 * Don't set contiguous if the combined extent would be too large.
2274 * Also check for all-three-contiguous being too large.
2276 if (*idx
< xfs_iext_count(ifp
) - 1) {
2277 state
|= BMAP_RIGHT_VALID
;
2278 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, *idx
+ 1), &RIGHT
);
2279 if (isnullstartblock(RIGHT
.br_startblock
))
2280 state
|= BMAP_RIGHT_DELAY
;
2283 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
2284 new_endoff
== RIGHT
.br_startoff
&&
2285 new->br_startblock
+ new->br_blockcount
== RIGHT
.br_startblock
&&
2286 newext
== RIGHT
.br_state
&&
2287 new->br_blockcount
+ RIGHT
.br_blockcount
<= MAXEXTLEN
&&
2288 ((state
& (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
2289 BMAP_RIGHT_FILLING
)) !=
2290 (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
2291 BMAP_RIGHT_FILLING
) ||
2292 LEFT
.br_blockcount
+ new->br_blockcount
+ RIGHT
.br_blockcount
2294 state
|= BMAP_RIGHT_CONTIG
;
2297 * Switch out based on the FILLING and CONTIG state bits.
2299 switch (state
& (BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
2300 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
)) {
2301 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
2302 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2304 * Setting all of a previous oldext extent to newext.
2305 * The left and right neighbors are both contiguous with new.
2309 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2310 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
),
2311 LEFT
.br_blockcount
+ PREV
.br_blockcount
+
2312 RIGHT
.br_blockcount
);
2313 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2315 xfs_iext_remove(ip
, *idx
+ 1, 2, state
);
2316 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2317 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 2);
2319 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2321 rval
= XFS_ILOG_CORE
;
2322 if ((error
= xfs_bmbt_lookup_eq(cur
, RIGHT
.br_startoff
,
2323 RIGHT
.br_startblock
,
2324 RIGHT
.br_blockcount
, &i
)))
2326 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2327 if ((error
= xfs_btree_delete(cur
, &i
)))
2329 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2330 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2332 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2333 if ((error
= xfs_btree_delete(cur
, &i
)))
2335 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2336 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2338 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2339 if ((error
= xfs_bmbt_update(cur
, LEFT
.br_startoff
,
2341 LEFT
.br_blockcount
+ PREV
.br_blockcount
+
2342 RIGHT
.br_blockcount
, LEFT
.br_state
)))
2347 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
2349 * Setting all of a previous oldext extent to newext.
2350 * The left neighbor is contiguous, the right is not.
2354 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2355 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
),
2356 LEFT
.br_blockcount
+ PREV
.br_blockcount
);
2357 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2359 xfs_iext_remove(ip
, *idx
+ 1, 1, state
);
2360 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2361 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
2363 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2365 rval
= XFS_ILOG_CORE
;
2366 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2367 PREV
.br_startblock
, PREV
.br_blockcount
,
2370 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2371 if ((error
= xfs_btree_delete(cur
, &i
)))
2373 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2374 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2376 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2377 if ((error
= xfs_bmbt_update(cur
, LEFT
.br_startoff
,
2379 LEFT
.br_blockcount
+ PREV
.br_blockcount
,
2385 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2387 * Setting all of a previous oldext extent to newext.
2388 * The right neighbor is contiguous, the left is not.
2390 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2391 xfs_bmbt_set_blockcount(ep
,
2392 PREV
.br_blockcount
+ RIGHT
.br_blockcount
);
2393 xfs_bmbt_set_state(ep
, newext
);
2394 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2395 xfs_iext_remove(ip
, *idx
+ 1, 1, state
);
2396 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2397 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
2399 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2401 rval
= XFS_ILOG_CORE
;
2402 if ((error
= xfs_bmbt_lookup_eq(cur
, RIGHT
.br_startoff
,
2403 RIGHT
.br_startblock
,
2404 RIGHT
.br_blockcount
, &i
)))
2406 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2407 if ((error
= xfs_btree_delete(cur
, &i
)))
2409 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2410 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2412 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2413 if ((error
= xfs_bmbt_update(cur
, new->br_startoff
,
2415 new->br_blockcount
+ RIGHT
.br_blockcount
,
2421 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
2423 * Setting all of a previous oldext extent to newext.
2424 * Neither the left nor right neighbors are contiguous with
2427 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2428 xfs_bmbt_set_state(ep
, newext
);
2429 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2432 rval
= XFS_ILOG_DEXT
;
2435 if ((error
= xfs_bmbt_lookup_eq(cur
, new->br_startoff
,
2436 new->br_startblock
, new->br_blockcount
,
2439 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2440 if ((error
= xfs_bmbt_update(cur
, new->br_startoff
,
2441 new->br_startblock
, new->br_blockcount
,
2447 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
:
2449 * Setting the first part of a previous oldext extent to newext.
2450 * The left neighbor is contiguous.
2452 trace_xfs_bmap_pre_update(ip
, *idx
- 1, state
, _THIS_IP_
);
2453 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
- 1),
2454 LEFT
.br_blockcount
+ new->br_blockcount
);
2455 xfs_bmbt_set_startoff(ep
,
2456 PREV
.br_startoff
+ new->br_blockcount
);
2457 trace_xfs_bmap_post_update(ip
, *idx
- 1, state
, _THIS_IP_
);
2459 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2460 xfs_bmbt_set_startblock(ep
,
2461 new->br_startblock
+ new->br_blockcount
);
2462 xfs_bmbt_set_blockcount(ep
,
2463 PREV
.br_blockcount
- new->br_blockcount
);
2464 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2469 rval
= XFS_ILOG_DEXT
;
2472 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2473 PREV
.br_startblock
, PREV
.br_blockcount
,
2476 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2477 if ((error
= xfs_bmbt_update(cur
,
2478 PREV
.br_startoff
+ new->br_blockcount
,
2479 PREV
.br_startblock
+ new->br_blockcount
,
2480 PREV
.br_blockcount
- new->br_blockcount
,
2483 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2485 error
= xfs_bmbt_update(cur
, LEFT
.br_startoff
,
2487 LEFT
.br_blockcount
+ new->br_blockcount
,
2494 case BMAP_LEFT_FILLING
:
2496 * Setting the first part of a previous oldext extent to newext.
2497 * The left neighbor is not contiguous.
2499 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2500 ASSERT(ep
&& xfs_bmbt_get_state(ep
) == oldext
);
2501 xfs_bmbt_set_startoff(ep
, new_endoff
);
2502 xfs_bmbt_set_blockcount(ep
,
2503 PREV
.br_blockcount
- new->br_blockcount
);
2504 xfs_bmbt_set_startblock(ep
,
2505 new->br_startblock
+ new->br_blockcount
);
2506 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2508 xfs_iext_insert(ip
, *idx
, 1, new, state
);
2509 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2510 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
2512 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2514 rval
= XFS_ILOG_CORE
;
2515 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2516 PREV
.br_startblock
, PREV
.br_blockcount
,
2519 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2520 if ((error
= xfs_bmbt_update(cur
,
2521 PREV
.br_startoff
+ new->br_blockcount
,
2522 PREV
.br_startblock
+ new->br_blockcount
,
2523 PREV
.br_blockcount
- new->br_blockcount
,
2526 cur
->bc_rec
.b
= *new;
2527 if ((error
= xfs_btree_insert(cur
, &i
)))
2529 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2533 case BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2535 * Setting the last part of a previous oldext extent to newext.
2536 * The right neighbor is contiguous with the new allocation.
2538 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2539 xfs_bmbt_set_blockcount(ep
,
2540 PREV
.br_blockcount
- new->br_blockcount
);
2541 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2545 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2546 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp
, *idx
),
2547 new->br_startoff
, new->br_startblock
,
2548 new->br_blockcount
+ RIGHT
.br_blockcount
, newext
);
2549 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2552 rval
= XFS_ILOG_DEXT
;
2555 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2557 PREV
.br_blockcount
, &i
)))
2559 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2560 if ((error
= xfs_bmbt_update(cur
, PREV
.br_startoff
,
2562 PREV
.br_blockcount
- new->br_blockcount
,
2565 if ((error
= xfs_btree_increment(cur
, 0, &i
)))
2567 if ((error
= xfs_bmbt_update(cur
, new->br_startoff
,
2569 new->br_blockcount
+ RIGHT
.br_blockcount
,
2575 case BMAP_RIGHT_FILLING
:
2577 * Setting the last part of a previous oldext extent to newext.
2578 * The right neighbor is not contiguous.
2580 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2581 xfs_bmbt_set_blockcount(ep
,
2582 PREV
.br_blockcount
- new->br_blockcount
);
2583 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2586 xfs_iext_insert(ip
, *idx
, 1, new, state
);
2588 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2589 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
2591 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2593 rval
= XFS_ILOG_CORE
;
2594 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2595 PREV
.br_startblock
, PREV
.br_blockcount
,
2598 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2599 if ((error
= xfs_bmbt_update(cur
, PREV
.br_startoff
,
2601 PREV
.br_blockcount
- new->br_blockcount
,
2604 if ((error
= xfs_bmbt_lookup_eq(cur
, new->br_startoff
,
2605 new->br_startblock
, new->br_blockcount
,
2608 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
2609 cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
2610 if ((error
= xfs_btree_insert(cur
, &i
)))
2612 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2618 * Setting the middle part of a previous oldext extent to
2619 * newext. Contiguity is impossible here.
2620 * One extent becomes three extents.
2622 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2623 xfs_bmbt_set_blockcount(ep
,
2624 new->br_startoff
- PREV
.br_startoff
);
2625 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2628 r
[1].br_startoff
= new_endoff
;
2629 r
[1].br_blockcount
=
2630 PREV
.br_startoff
+ PREV
.br_blockcount
- new_endoff
;
2631 r
[1].br_startblock
= new->br_startblock
+ new->br_blockcount
;
2632 r
[1].br_state
= oldext
;
2635 xfs_iext_insert(ip
, *idx
, 2, &r
[0], state
);
2637 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2638 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 2);
2640 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2642 rval
= XFS_ILOG_CORE
;
2643 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2644 PREV
.br_startblock
, PREV
.br_blockcount
,
2647 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2648 /* new right extent - oldext */
2649 if ((error
= xfs_bmbt_update(cur
, r
[1].br_startoff
,
2650 r
[1].br_startblock
, r
[1].br_blockcount
,
2653 /* new left extent - oldext */
2654 cur
->bc_rec
.b
= PREV
;
2655 cur
->bc_rec
.b
.br_blockcount
=
2656 new->br_startoff
- PREV
.br_startoff
;
2657 if ((error
= xfs_btree_insert(cur
, &i
)))
2659 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2661 * Reset the cursor to the position of the new extent
2662 * we are about to insert as we can't trust it after
2663 * the previous insert.
2665 if ((error
= xfs_bmbt_lookup_eq(cur
, new->br_startoff
,
2666 new->br_startblock
, new->br_blockcount
,
2669 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
2670 /* new middle extent - newext */
2671 cur
->bc_rec
.b
.br_state
= new->br_state
;
2672 if ((error
= xfs_btree_insert(cur
, &i
)))
2674 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2678 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2679 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2680 case BMAP_LEFT_FILLING
| BMAP_RIGHT_CONTIG
:
2681 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
2682 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2683 case BMAP_LEFT_CONTIG
:
2684 case BMAP_RIGHT_CONTIG
:
2686 * These cases are all impossible.
2691 /* update reverse mappings */
2692 error
= xfs_rmap_convert_extent(mp
, dfops
, ip
, whichfork
, new);
2696 /* convert to a btree if necessary */
2697 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
2698 int tmp_logflags
; /* partial log flag return val */
2700 ASSERT(cur
== NULL
);
2701 error
= xfs_bmap_extents_to_btree(tp
, ip
, first
, dfops
, &cur
,
2702 0, &tmp_logflags
, whichfork
);
2703 *logflagsp
|= tmp_logflags
;
2708 /* clear out the allocated field, done with it now in any case. */
2710 cur
->bc_private
.b
.allocated
= 0;
2714 xfs_bmap_check_leaf_extents(*curp
, ip
, whichfork
);
2724 * Convert a hole to a delayed allocation.
2727 xfs_bmap_add_extent_hole_delay(
2728 xfs_inode_t
*ip
, /* incore inode pointer */
2730 xfs_extnum_t
*idx
, /* extent number to update/insert */
2731 xfs_bmbt_irec_t
*new) /* new data to add to file extents */
2733 xfs_ifork_t
*ifp
; /* inode fork pointer */
2734 xfs_bmbt_irec_t left
; /* left neighbor extent entry */
2735 xfs_filblks_t newlen
=0; /* new indirect size */
2736 xfs_filblks_t oldlen
=0; /* old indirect size */
2737 xfs_bmbt_irec_t right
; /* right neighbor extent entry */
2738 int state
; /* state bits, accessed thru macros */
2739 xfs_filblks_t temp
=0; /* temp for indirect calculations */
2741 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
2743 if (whichfork
== XFS_COW_FORK
)
2744 state
|= BMAP_COWFORK
;
2745 ASSERT(isnullstartblock(new->br_startblock
));
2748 * Check and set flags if this segment has a left neighbor
2751 state
|= BMAP_LEFT_VALID
;
2752 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, *idx
- 1), &left
);
2754 if (isnullstartblock(left
.br_startblock
))
2755 state
|= BMAP_LEFT_DELAY
;
2759 * Check and set flags if the current (right) segment exists.
2760 * If it doesn't exist, we're converting the hole at end-of-file.
2762 if (*idx
< xfs_iext_count(ifp
)) {
2763 state
|= BMAP_RIGHT_VALID
;
2764 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, *idx
), &right
);
2766 if (isnullstartblock(right
.br_startblock
))
2767 state
|= BMAP_RIGHT_DELAY
;
2771 * Set contiguity flags on the left and right neighbors.
2772 * Don't let extents get too large, even if the pieces are contiguous.
2774 if ((state
& BMAP_LEFT_VALID
) && (state
& BMAP_LEFT_DELAY
) &&
2775 left
.br_startoff
+ left
.br_blockcount
== new->br_startoff
&&
2776 left
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
2777 state
|= BMAP_LEFT_CONTIG
;
2779 if ((state
& BMAP_RIGHT_VALID
) && (state
& BMAP_RIGHT_DELAY
) &&
2780 new->br_startoff
+ new->br_blockcount
== right
.br_startoff
&&
2781 new->br_blockcount
+ right
.br_blockcount
<= MAXEXTLEN
&&
2782 (!(state
& BMAP_LEFT_CONTIG
) ||
2783 (left
.br_blockcount
+ new->br_blockcount
+
2784 right
.br_blockcount
<= MAXEXTLEN
)))
2785 state
|= BMAP_RIGHT_CONTIG
;
2788 * Switch out based on the contiguity flags.
2790 switch (state
& (BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
)) {
2791 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2793 * New allocation is contiguous with delayed allocations
2794 * on the left and on the right.
2795 * Merge all three into a single extent record.
2798 temp
= left
.br_blockcount
+ new->br_blockcount
+
2799 right
.br_blockcount
;
2801 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2802 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
), temp
);
2803 oldlen
= startblockval(left
.br_startblock
) +
2804 startblockval(new->br_startblock
) +
2805 startblockval(right
.br_startblock
);
2806 newlen
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
2808 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp
, *idx
),
2809 nullstartblock((int)newlen
));
2810 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2812 xfs_iext_remove(ip
, *idx
+ 1, 1, state
);
2815 case BMAP_LEFT_CONTIG
:
2817 * New allocation is contiguous with a delayed allocation
2819 * Merge the new allocation with the left neighbor.
2822 temp
= left
.br_blockcount
+ new->br_blockcount
;
2824 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2825 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
), temp
);
2826 oldlen
= startblockval(left
.br_startblock
) +
2827 startblockval(new->br_startblock
);
2828 newlen
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
2830 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp
, *idx
),
2831 nullstartblock((int)newlen
));
2832 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2835 case BMAP_RIGHT_CONTIG
:
2837 * New allocation is contiguous with a delayed allocation
2839 * Merge the new allocation with the right neighbor.
2841 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2842 temp
= new->br_blockcount
+ right
.br_blockcount
;
2843 oldlen
= startblockval(new->br_startblock
) +
2844 startblockval(right
.br_startblock
);
2845 newlen
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
2847 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp
, *idx
),
2849 nullstartblock((int)newlen
), temp
, right
.br_state
);
2850 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2855 * New allocation is not contiguous with another
2856 * delayed allocation.
2857 * Insert a new entry.
2859 oldlen
= newlen
= 0;
2860 xfs_iext_insert(ip
, *idx
, 1, new, state
);
2863 if (oldlen
!= newlen
) {
2864 ASSERT(oldlen
> newlen
);
2865 xfs_mod_fdblocks(ip
->i_mount
, (int64_t)(oldlen
- newlen
),
2868 * Nothing to do for disk quota accounting here.
2874 * Convert a hole to a real allocation.
2876 STATIC
int /* error */
2877 xfs_bmap_add_extent_hole_real(
2878 struct xfs_bmalloca
*bma
,
2881 struct xfs_bmbt_irec
*new = &bma
->got
;
2882 int error
; /* error return value */
2883 int i
; /* temp state */
2884 xfs_ifork_t
*ifp
; /* inode fork pointer */
2885 xfs_bmbt_irec_t left
; /* left neighbor extent entry */
2886 xfs_bmbt_irec_t right
; /* right neighbor extent entry */
2887 int rval
=0; /* return value (logging flags) */
2888 int state
; /* state bits, accessed thru macros */
2889 struct xfs_mount
*mp
;
2891 mp
= bma
->ip
->i_mount
;
2892 ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
2894 ASSERT(bma
->idx
>= 0);
2895 ASSERT(bma
->idx
<= xfs_iext_count(ifp
));
2896 ASSERT(!isnullstartblock(new->br_startblock
));
2898 !(bma
->cur
->bc_private
.b
.flags
& XFS_BTCUR_BPRV_WASDEL
));
2900 XFS_STATS_INC(mp
, xs_add_exlist
);
2903 if (whichfork
== XFS_ATTR_FORK
)
2904 state
|= BMAP_ATTRFORK
;
2905 if (whichfork
== XFS_COW_FORK
)
2906 state
|= BMAP_COWFORK
;
2909 * Check and set flags if this segment has a left neighbor.
2912 state
|= BMAP_LEFT_VALID
;
2913 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
- 1), &left
);
2914 if (isnullstartblock(left
.br_startblock
))
2915 state
|= BMAP_LEFT_DELAY
;
2919 * Check and set flags if this segment has a current value.
2920 * Not true if we're inserting into the "hole" at eof.
2922 if (bma
->idx
< xfs_iext_count(ifp
)) {
2923 state
|= BMAP_RIGHT_VALID
;
2924 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
), &right
);
2925 if (isnullstartblock(right
.br_startblock
))
2926 state
|= BMAP_RIGHT_DELAY
;
2930 * We're inserting a real allocation between "left" and "right".
2931 * Set the contiguity flags. Don't let extents get too large.
2933 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
2934 left
.br_startoff
+ left
.br_blockcount
== new->br_startoff
&&
2935 left
.br_startblock
+ left
.br_blockcount
== new->br_startblock
&&
2936 left
.br_state
== new->br_state
&&
2937 left
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
2938 state
|= BMAP_LEFT_CONTIG
;
2940 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
2941 new->br_startoff
+ new->br_blockcount
== right
.br_startoff
&&
2942 new->br_startblock
+ new->br_blockcount
== right
.br_startblock
&&
2943 new->br_state
== right
.br_state
&&
2944 new->br_blockcount
+ right
.br_blockcount
<= MAXEXTLEN
&&
2945 (!(state
& BMAP_LEFT_CONTIG
) ||
2946 left
.br_blockcount
+ new->br_blockcount
+
2947 right
.br_blockcount
<= MAXEXTLEN
))
2948 state
|= BMAP_RIGHT_CONTIG
;
2952 * Select which case we're in here, and implement it.
2954 switch (state
& (BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
)) {
2955 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2957 * New allocation is contiguous with real allocations on the
2958 * left and on the right.
2959 * Merge all three into a single extent record.
2962 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2963 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, bma
->idx
),
2964 left
.br_blockcount
+ new->br_blockcount
+
2965 right
.br_blockcount
);
2966 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2968 xfs_iext_remove(bma
->ip
, bma
->idx
+ 1, 1, state
);
2970 XFS_IFORK_NEXT_SET(bma
->ip
, whichfork
,
2971 XFS_IFORK_NEXTENTS(bma
->ip
, whichfork
) - 1);
2972 if (bma
->cur
== NULL
) {
2973 rval
= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
2975 rval
= XFS_ILOG_CORE
;
2976 error
= xfs_bmbt_lookup_eq(bma
->cur
, right
.br_startoff
,
2977 right
.br_startblock
, right
.br_blockcount
,
2981 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2982 error
= xfs_btree_delete(bma
->cur
, &i
);
2985 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2986 error
= xfs_btree_decrement(bma
->cur
, 0, &i
);
2989 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2990 error
= xfs_bmbt_update(bma
->cur
, left
.br_startoff
,
2992 left
.br_blockcount
+
2993 new->br_blockcount
+
2994 right
.br_blockcount
,
3001 case BMAP_LEFT_CONTIG
:
3003 * New allocation is contiguous with a real allocation
3005 * Merge the new allocation with the left neighbor.
3008 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
3009 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, bma
->idx
),
3010 left
.br_blockcount
+ new->br_blockcount
);
3011 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
3013 if (bma
->cur
== NULL
) {
3014 rval
= xfs_ilog_fext(whichfork
);
3017 error
= xfs_bmbt_lookup_eq(bma
->cur
, left
.br_startoff
,
3018 left
.br_startblock
, left
.br_blockcount
,
3022 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
3023 error
= xfs_bmbt_update(bma
->cur
, left
.br_startoff
,
3025 left
.br_blockcount
+
3033 case BMAP_RIGHT_CONTIG
:
3035 * New allocation is contiguous with a real allocation
3037 * Merge the new allocation with the right neighbor.
3039 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
3040 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp
, bma
->idx
),
3041 new->br_startoff
, new->br_startblock
,
3042 new->br_blockcount
+ right
.br_blockcount
,
3044 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
3046 if (bma
->cur
== NULL
) {
3047 rval
= xfs_ilog_fext(whichfork
);
3050 error
= xfs_bmbt_lookup_eq(bma
->cur
,
3052 right
.br_startblock
,
3053 right
.br_blockcount
, &i
);
3056 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
3057 error
= xfs_bmbt_update(bma
->cur
, new->br_startoff
,
3059 new->br_blockcount
+
3060 right
.br_blockcount
,
3069 * New allocation is not contiguous with another
3071 * Insert a new entry.
3073 xfs_iext_insert(bma
->ip
, bma
->idx
, 1, new, state
);
3074 XFS_IFORK_NEXT_SET(bma
->ip
, whichfork
,
3075 XFS_IFORK_NEXTENTS(bma
->ip
, whichfork
) + 1);
3076 if (bma
->cur
== NULL
) {
3077 rval
= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
3079 rval
= XFS_ILOG_CORE
;
3080 error
= xfs_bmbt_lookup_eq(bma
->cur
,
3083 new->br_blockcount
, &i
);
3086 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
3087 bma
->cur
->bc_rec
.b
.br_state
= new->br_state
;
3088 error
= xfs_btree_insert(bma
->cur
, &i
);
3091 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
3096 /* add reverse mapping */
3097 error
= xfs_rmap_map_extent(mp
, bma
->dfops
, bma
->ip
, whichfork
, new);
3101 /* convert to a btree if necessary */
3102 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
3103 int tmp_logflags
; /* partial log flag return val */
3105 ASSERT(bma
->cur
== NULL
);
3106 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
3107 bma
->firstblock
, bma
->dfops
, &bma
->cur
,
3108 0, &tmp_logflags
, whichfork
);
3109 bma
->logflags
|= tmp_logflags
;
3114 /* clear out the allocated field, done with it now in any case. */
3116 bma
->cur
->bc_private
.b
.allocated
= 0;
3118 xfs_bmap_check_leaf_extents(bma
->cur
, bma
->ip
, whichfork
);
3120 bma
->logflags
|= rval
;
3125 * Functions used in the extent read, allocate and remove paths
3129 * Adjust the size of the new extent based on di_extsize and rt extsize.
3132 xfs_bmap_extsize_align(
3134 xfs_bmbt_irec_t
*gotp
, /* next extent pointer */
3135 xfs_bmbt_irec_t
*prevp
, /* previous extent pointer */
3136 xfs_extlen_t extsz
, /* align to this extent size */
3137 int rt
, /* is this a realtime inode? */
3138 int eof
, /* is extent at end-of-file? */
3139 int delay
, /* creating delalloc extent? */
3140 int convert
, /* overwriting unwritten extent? */
3141 xfs_fileoff_t
*offp
, /* in/out: aligned offset */
3142 xfs_extlen_t
*lenp
) /* in/out: aligned length */
3144 xfs_fileoff_t orig_off
; /* original offset */
3145 xfs_extlen_t orig_alen
; /* original length */
3146 xfs_fileoff_t orig_end
; /* original off+len */
3147 xfs_fileoff_t nexto
; /* next file offset */
3148 xfs_fileoff_t prevo
; /* previous file offset */
3149 xfs_fileoff_t align_off
; /* temp for offset */
3150 xfs_extlen_t align_alen
; /* temp for length */
3151 xfs_extlen_t temp
; /* temp for calculations */
3156 orig_off
= align_off
= *offp
;
3157 orig_alen
= align_alen
= *lenp
;
3158 orig_end
= orig_off
+ orig_alen
;
3161 * If this request overlaps an existing extent, then don't
3162 * attempt to perform any additional alignment.
3164 if (!delay
&& !eof
&&
3165 (orig_off
>= gotp
->br_startoff
) &&
3166 (orig_end
<= gotp
->br_startoff
+ gotp
->br_blockcount
)) {
3171 * If the file offset is unaligned vs. the extent size
3172 * we need to align it. This will be possible unless
3173 * the file was previously written with a kernel that didn't
3174 * perform this alignment, or if a truncate shot us in the
3177 temp
= do_mod(orig_off
, extsz
);
3183 /* Same adjustment for the end of the requested area. */
3184 temp
= (align_alen
% extsz
);
3186 align_alen
+= extsz
- temp
;
3189 * For large extent hint sizes, the aligned extent might be larger than
3190 * MAXEXTLEN. In that case, reduce the size by an extsz so that it pulls
3191 * the length back under MAXEXTLEN. The outer allocation loops handle
3192 * short allocation just fine, so it is safe to do this. We only want to
3193 * do it when we are forced to, though, because it means more allocation
3194 * operations are required.
3196 while (align_alen
> MAXEXTLEN
)
3197 align_alen
-= extsz
;
3198 ASSERT(align_alen
<= MAXEXTLEN
);
3201 * If the previous block overlaps with this proposed allocation
3202 * then move the start forward without adjusting the length.
3204 if (prevp
->br_startoff
!= NULLFILEOFF
) {
3205 if (prevp
->br_startblock
== HOLESTARTBLOCK
)
3206 prevo
= prevp
->br_startoff
;
3208 prevo
= prevp
->br_startoff
+ prevp
->br_blockcount
;
3211 if (align_off
!= orig_off
&& align_off
< prevo
)
3214 * If the next block overlaps with this proposed allocation
3215 * then move the start back without adjusting the length,
3216 * but not before offset 0.
3217 * This may of course make the start overlap previous block,
3218 * and if we hit the offset 0 limit then the next block
3219 * can still overlap too.
3221 if (!eof
&& gotp
->br_startoff
!= NULLFILEOFF
) {
3222 if ((delay
&& gotp
->br_startblock
== HOLESTARTBLOCK
) ||
3223 (!delay
&& gotp
->br_startblock
== DELAYSTARTBLOCK
))
3224 nexto
= gotp
->br_startoff
+ gotp
->br_blockcount
;
3226 nexto
= gotp
->br_startoff
;
3228 nexto
= NULLFILEOFF
;
3230 align_off
+ align_alen
!= orig_end
&&
3231 align_off
+ align_alen
> nexto
)
3232 align_off
= nexto
> align_alen
? nexto
- align_alen
: 0;
3234 * If we're now overlapping the next or previous extent that
3235 * means we can't fit an extsz piece in this hole. Just move
3236 * the start forward to the first valid spot and set
3237 * the length so we hit the end.
3239 if (align_off
!= orig_off
&& align_off
< prevo
)
3241 if (align_off
+ align_alen
!= orig_end
&&
3242 align_off
+ align_alen
> nexto
&&
3243 nexto
!= NULLFILEOFF
) {
3244 ASSERT(nexto
> prevo
);
3245 align_alen
= nexto
- align_off
;
3249 * If realtime, and the result isn't a multiple of the realtime
3250 * extent size we need to remove blocks until it is.
3252 if (rt
&& (temp
= (align_alen
% mp
->m_sb
.sb_rextsize
))) {
3254 * We're not covering the original request, or
3255 * we won't be able to once we fix the length.
3257 if (orig_off
< align_off
||
3258 orig_end
> align_off
+ align_alen
||
3259 align_alen
- temp
< orig_alen
)
3262 * Try to fix it by moving the start up.
3264 if (align_off
+ temp
<= orig_off
) {
3269 * Try to fix it by moving the end in.
3271 else if (align_off
+ align_alen
- temp
>= orig_end
)
3274 * Set the start to the minimum then trim the length.
3277 align_alen
-= orig_off
- align_off
;
3278 align_off
= orig_off
;
3279 align_alen
-= align_alen
% mp
->m_sb
.sb_rextsize
;
3282 * Result doesn't cover the request, fail it.
3284 if (orig_off
< align_off
|| orig_end
> align_off
+ align_alen
)
3287 ASSERT(orig_off
>= align_off
);
3288 /* see MAXEXTLEN handling above */
3289 ASSERT(orig_end
<= align_off
+ align_alen
||
3290 align_alen
+ extsz
> MAXEXTLEN
);
3294 if (!eof
&& gotp
->br_startoff
!= NULLFILEOFF
)
3295 ASSERT(align_off
+ align_alen
<= gotp
->br_startoff
);
3296 if (prevp
->br_startoff
!= NULLFILEOFF
)
3297 ASSERT(align_off
>= prevp
->br_startoff
+ prevp
->br_blockcount
);
3305 #define XFS_ALLOC_GAP_UNITS 4
3309 struct xfs_bmalloca
*ap
) /* bmap alloc argument struct */
3311 xfs_fsblock_t adjust
; /* adjustment to block numbers */
3312 xfs_agnumber_t fb_agno
; /* ag number of ap->firstblock */
3313 xfs_mount_t
*mp
; /* mount point structure */
3314 int nullfb
; /* true if ap->firstblock isn't set */
3315 int rt
; /* true if inode is realtime */
3317 #define ISVALID(x,y) \
3319 (x) < mp->m_sb.sb_rblocks : \
3320 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
3321 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
3322 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
3324 mp
= ap
->ip
->i_mount
;
3325 nullfb
= *ap
->firstblock
== NULLFSBLOCK
;
3326 rt
= XFS_IS_REALTIME_INODE(ap
->ip
) &&
3327 xfs_alloc_is_userdata(ap
->datatype
);
3328 fb_agno
= nullfb
? NULLAGNUMBER
: XFS_FSB_TO_AGNO(mp
, *ap
->firstblock
);
3330 * If allocating at eof, and there's a previous real block,
3331 * try to use its last block as our starting point.
3333 if (ap
->eof
&& ap
->prev
.br_startoff
!= NULLFILEOFF
&&
3334 !isnullstartblock(ap
->prev
.br_startblock
) &&
3335 ISVALID(ap
->prev
.br_startblock
+ ap
->prev
.br_blockcount
,
3336 ap
->prev
.br_startblock
)) {
3337 ap
->blkno
= ap
->prev
.br_startblock
+ ap
->prev
.br_blockcount
;
3339 * Adjust for the gap between prevp and us.
3341 adjust
= ap
->offset
-
3342 (ap
->prev
.br_startoff
+ ap
->prev
.br_blockcount
);
3344 ISVALID(ap
->blkno
+ adjust
, ap
->prev
.br_startblock
))
3345 ap
->blkno
+= adjust
;
3348 * If not at eof, then compare the two neighbor blocks.
3349 * Figure out whether either one gives us a good starting point,
3350 * and pick the better one.
3352 else if (!ap
->eof
) {
3353 xfs_fsblock_t gotbno
; /* right side block number */
3354 xfs_fsblock_t gotdiff
=0; /* right side difference */
3355 xfs_fsblock_t prevbno
; /* left side block number */
3356 xfs_fsblock_t prevdiff
=0; /* left side difference */
3359 * If there's a previous (left) block, select a requested
3360 * start block based on it.
3362 if (ap
->prev
.br_startoff
!= NULLFILEOFF
&&
3363 !isnullstartblock(ap
->prev
.br_startblock
) &&
3364 (prevbno
= ap
->prev
.br_startblock
+
3365 ap
->prev
.br_blockcount
) &&
3366 ISVALID(prevbno
, ap
->prev
.br_startblock
)) {
3368 * Calculate gap to end of previous block.
3370 adjust
= prevdiff
= ap
->offset
-
3371 (ap
->prev
.br_startoff
+
3372 ap
->prev
.br_blockcount
);
3374 * Figure the startblock based on the previous block's
3375 * end and the gap size.
3377 * If the gap is large relative to the piece we're
3378 * allocating, or using it gives us an invalid block
3379 * number, then just use the end of the previous block.
3381 if (prevdiff
<= XFS_ALLOC_GAP_UNITS
* ap
->length
&&
3382 ISVALID(prevbno
+ prevdiff
,
3383 ap
->prev
.br_startblock
))
3388 * If the firstblock forbids it, can't use it,
3391 if (!rt
&& !nullfb
&&
3392 XFS_FSB_TO_AGNO(mp
, prevbno
) != fb_agno
)
3393 prevbno
= NULLFSBLOCK
;
3396 * No previous block or can't follow it, just default.
3399 prevbno
= NULLFSBLOCK
;
3401 * If there's a following (right) block, select a requested
3402 * start block based on it.
3404 if (!isnullstartblock(ap
->got
.br_startblock
)) {
3406 * Calculate gap to start of next block.
3408 adjust
= gotdiff
= ap
->got
.br_startoff
- ap
->offset
;
3410 * Figure the startblock based on the next block's
3411 * start and the gap size.
3413 gotbno
= ap
->got
.br_startblock
;
3416 * If the gap is large relative to the piece we're
3417 * allocating, or using it gives us an invalid block
3418 * number, then just use the start of the next block
3419 * offset by our length.
3421 if (gotdiff
<= XFS_ALLOC_GAP_UNITS
* ap
->length
&&
3422 ISVALID(gotbno
- gotdiff
, gotbno
))
3424 else if (ISVALID(gotbno
- ap
->length
, gotbno
)) {
3425 gotbno
-= ap
->length
;
3426 gotdiff
+= adjust
- ap
->length
;
3430 * If the firstblock forbids it, can't use it,
3433 if (!rt
&& !nullfb
&&
3434 XFS_FSB_TO_AGNO(mp
, gotbno
) != fb_agno
)
3435 gotbno
= NULLFSBLOCK
;
3438 * No next block, just default.
3441 gotbno
= NULLFSBLOCK
;
3443 * If both valid, pick the better one, else the only good
3444 * one, else ap->blkno is already set (to 0 or the inode block).
3446 if (prevbno
!= NULLFSBLOCK
&& gotbno
!= NULLFSBLOCK
)
3447 ap
->blkno
= prevdiff
<= gotdiff
? prevbno
: gotbno
;
3448 else if (prevbno
!= NULLFSBLOCK
)
3449 ap
->blkno
= prevbno
;
3450 else if (gotbno
!= NULLFSBLOCK
)
3457 xfs_bmap_longest_free_extent(
3458 struct xfs_trans
*tp
,
3463 struct xfs_mount
*mp
= tp
->t_mountp
;
3464 struct xfs_perag
*pag
;
3465 xfs_extlen_t longest
;
3468 pag
= xfs_perag_get(mp
, ag
);
3469 if (!pag
->pagf_init
) {
3470 error
= xfs_alloc_pagf_init(mp
, tp
, ag
, XFS_ALLOC_FLAG_TRYLOCK
);
3474 if (!pag
->pagf_init
) {
3480 longest
= xfs_alloc_longest_free_extent(mp
, pag
,
3481 xfs_alloc_min_freelist(mp
, pag
),
3482 xfs_ag_resv_needed(pag
, XFS_AG_RESV_NONE
));
3483 if (*blen
< longest
)
3492 xfs_bmap_select_minlen(
3493 struct xfs_bmalloca
*ap
,
3494 struct xfs_alloc_arg
*args
,
3498 if (notinit
|| *blen
< ap
->minlen
) {
3500 * Since we did a BUF_TRYLOCK above, it is possible that
3501 * there is space for this request.
3503 args
->minlen
= ap
->minlen
;
3504 } else if (*blen
< args
->maxlen
) {
3506 * If the best seen length is less than the request length,
3507 * use the best as the minimum.
3509 args
->minlen
= *blen
;
3512 * Otherwise we've seen an extent as big as maxlen, use that
3515 args
->minlen
= args
->maxlen
;
3520 xfs_bmap_btalloc_nullfb(
3521 struct xfs_bmalloca
*ap
,
3522 struct xfs_alloc_arg
*args
,
3525 struct xfs_mount
*mp
= ap
->ip
->i_mount
;
3526 xfs_agnumber_t ag
, startag
;
3530 args
->type
= XFS_ALLOCTYPE_START_BNO
;
3531 args
->total
= ap
->total
;
3533 startag
= ag
= XFS_FSB_TO_AGNO(mp
, args
->fsbno
);
3534 if (startag
== NULLAGNUMBER
)
3537 while (*blen
< args
->maxlen
) {
3538 error
= xfs_bmap_longest_free_extent(args
->tp
, ag
, blen
,
3543 if (++ag
== mp
->m_sb
.sb_agcount
)
3549 xfs_bmap_select_minlen(ap
, args
, blen
, notinit
);
3554 xfs_bmap_btalloc_filestreams(
3555 struct xfs_bmalloca
*ap
,
3556 struct xfs_alloc_arg
*args
,
3559 struct xfs_mount
*mp
= ap
->ip
->i_mount
;
3564 args
->type
= XFS_ALLOCTYPE_NEAR_BNO
;
3565 args
->total
= ap
->total
;
3567 ag
= XFS_FSB_TO_AGNO(mp
, args
->fsbno
);
3568 if (ag
== NULLAGNUMBER
)
3571 error
= xfs_bmap_longest_free_extent(args
->tp
, ag
, blen
, ¬init
);
3575 if (*blen
< args
->maxlen
) {
3576 error
= xfs_filestream_new_ag(ap
, &ag
);
3580 error
= xfs_bmap_longest_free_extent(args
->tp
, ag
, blen
,
3587 xfs_bmap_select_minlen(ap
, args
, blen
, notinit
);
3590 * Set the failure fallback case to look in the selected AG as stream
3593 ap
->blkno
= args
->fsbno
= XFS_AGB_TO_FSB(mp
, ag
, 0);
3599 struct xfs_bmalloca
*ap
) /* bmap alloc argument struct */
3601 xfs_mount_t
*mp
; /* mount point structure */
3602 xfs_alloctype_t atype
= 0; /* type for allocation routines */
3603 xfs_extlen_t align
= 0; /* minimum allocation alignment */
3604 xfs_agnumber_t fb_agno
; /* ag number of ap->firstblock */
3606 xfs_alloc_arg_t args
;
3608 xfs_extlen_t nextminlen
= 0;
3609 int nullfb
; /* true if ap->firstblock isn't set */
3617 mp
= ap
->ip
->i_mount
;
3619 /* stripe alignment for allocation is determined by mount parameters */
3621 if (mp
->m_swidth
&& (mp
->m_flags
& XFS_MOUNT_SWALLOC
))
3622 stripe_align
= mp
->m_swidth
;
3623 else if (mp
->m_dalign
)
3624 stripe_align
= mp
->m_dalign
;
3626 if (ap
->flags
& XFS_BMAPI_COWFORK
)
3627 align
= xfs_get_cowextsz_hint(ap
->ip
);
3628 else if (xfs_alloc_is_userdata(ap
->datatype
))
3629 align
= xfs_get_extsz_hint(ap
->ip
);
3631 error
= xfs_bmap_extsize_align(mp
, &ap
->got
, &ap
->prev
,
3632 align
, 0, ap
->eof
, 0, ap
->conv
,
3633 &ap
->offset
, &ap
->length
);
3639 nullfb
= *ap
->firstblock
== NULLFSBLOCK
;
3640 fb_agno
= nullfb
? NULLAGNUMBER
: XFS_FSB_TO_AGNO(mp
, *ap
->firstblock
);
3642 if (xfs_alloc_is_userdata(ap
->datatype
) &&
3643 xfs_inode_is_filestream(ap
->ip
)) {
3644 ag
= xfs_filestream_lookup_ag(ap
->ip
);
3645 ag
= (ag
!= NULLAGNUMBER
) ? ag
: 0;
3646 ap
->blkno
= XFS_AGB_TO_FSB(mp
, ag
, 0);
3648 ap
->blkno
= XFS_INO_TO_FSB(mp
, ap
->ip
->i_ino
);
3651 ap
->blkno
= *ap
->firstblock
;
3653 xfs_bmap_adjacent(ap
);
3656 * If allowed, use ap->blkno; otherwise must use firstblock since
3657 * it's in the right allocation group.
3659 if (nullfb
|| XFS_FSB_TO_AGNO(mp
, ap
->blkno
) == fb_agno
)
3662 ap
->blkno
= *ap
->firstblock
;
3664 * Normal allocation, done through xfs_alloc_vextent.
3666 tryagain
= isaligned
= 0;
3667 memset(&args
, 0, sizeof(args
));
3670 args
.fsbno
= ap
->blkno
;
3671 xfs_rmap_skip_owner_update(&args
.oinfo
);
3673 /* Trim the allocation back to the maximum an AG can fit. */
3674 args
.maxlen
= MIN(ap
->length
, mp
->m_ag_max_usable
);
3675 args
.firstblock
= *ap
->firstblock
;
3679 * Search for an allocation group with a single extent large
3680 * enough for the request. If one isn't found, then adjust
3681 * the minimum allocation size to the largest space found.
3683 if (xfs_alloc_is_userdata(ap
->datatype
) &&
3684 xfs_inode_is_filestream(ap
->ip
))
3685 error
= xfs_bmap_btalloc_filestreams(ap
, &args
, &blen
);
3687 error
= xfs_bmap_btalloc_nullfb(ap
, &args
, &blen
);
3690 } else if (ap
->dfops
->dop_low
) {
3691 if (xfs_inode_is_filestream(ap
->ip
))
3692 args
.type
= XFS_ALLOCTYPE_FIRST_AG
;
3694 args
.type
= XFS_ALLOCTYPE_START_BNO
;
3695 args
.total
= args
.minlen
= ap
->minlen
;
3697 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
3698 args
.total
= ap
->total
;
3699 args
.minlen
= ap
->minlen
;
3701 /* apply extent size hints if obtained earlier */
3704 if ((args
.mod
= (xfs_extlen_t
)do_mod(ap
->offset
, args
.prod
)))
3705 args
.mod
= (xfs_extlen_t
)(args
.prod
- args
.mod
);
3706 } else if (mp
->m_sb
.sb_blocksize
>= PAGE_SIZE
) {
3710 args
.prod
= PAGE_SIZE
>> mp
->m_sb
.sb_blocklog
;
3711 if ((args
.mod
= (xfs_extlen_t
)(do_mod(ap
->offset
, args
.prod
))))
3712 args
.mod
= (xfs_extlen_t
)(args
.prod
- args
.mod
);
3715 * If we are not low on available data blocks, and the
3716 * underlying logical volume manager is a stripe, and
3717 * the file offset is zero then try to allocate data
3718 * blocks on stripe unit boundary.
3719 * NOTE: ap->aeof is only set if the allocation length
3720 * is >= the stripe unit and the allocation offset is
3721 * at the end of file.
3723 if (!ap
->dfops
->dop_low
&& ap
->aeof
) {
3725 args
.alignment
= stripe_align
;
3729 * Adjust for alignment
3731 if (blen
> args
.alignment
&& blen
<= args
.maxlen
)
3732 args
.minlen
= blen
- args
.alignment
;
3733 args
.minalignslop
= 0;
3736 * First try an exact bno allocation.
3737 * If it fails then do a near or start bno
3738 * allocation with alignment turned on.
3742 args
.type
= XFS_ALLOCTYPE_THIS_BNO
;
3745 * Compute the minlen+alignment for the
3746 * next case. Set slop so that the value
3747 * of minlen+alignment+slop doesn't go up
3748 * between the calls.
3750 if (blen
> stripe_align
&& blen
<= args
.maxlen
)
3751 nextminlen
= blen
- stripe_align
;
3753 nextminlen
= args
.minlen
;
3754 if (nextminlen
+ stripe_align
> args
.minlen
+ 1)
3756 nextminlen
+ stripe_align
-
3759 args
.minalignslop
= 0;
3763 args
.minalignslop
= 0;
3765 args
.minleft
= ap
->minleft
;
3766 args
.wasdel
= ap
->wasdel
;
3767 args
.resv
= XFS_AG_RESV_NONE
;
3768 args
.datatype
= ap
->datatype
;
3769 if (ap
->datatype
& XFS_ALLOC_USERDATA_ZERO
)
3772 error
= xfs_alloc_vextent(&args
);
3776 if (tryagain
&& args
.fsbno
== NULLFSBLOCK
) {
3778 * Exact allocation failed. Now try with alignment
3782 args
.fsbno
= ap
->blkno
;
3783 args
.alignment
= stripe_align
;
3784 args
.minlen
= nextminlen
;
3785 args
.minalignslop
= 0;
3787 if ((error
= xfs_alloc_vextent(&args
)))
3790 if (isaligned
&& args
.fsbno
== NULLFSBLOCK
) {
3792 * allocation failed, so turn off alignment and
3796 args
.fsbno
= ap
->blkno
;
3798 if ((error
= xfs_alloc_vextent(&args
)))
3801 if (args
.fsbno
== NULLFSBLOCK
&& nullfb
&&
3802 args
.minlen
> ap
->minlen
) {
3803 args
.minlen
= ap
->minlen
;
3804 args
.type
= XFS_ALLOCTYPE_START_BNO
;
3805 args
.fsbno
= ap
->blkno
;
3806 if ((error
= xfs_alloc_vextent(&args
)))
3809 if (args
.fsbno
== NULLFSBLOCK
&& nullfb
) {
3811 args
.type
= XFS_ALLOCTYPE_FIRST_AG
;
3812 args
.total
= ap
->minlen
;
3813 if ((error
= xfs_alloc_vextent(&args
)))
3815 ap
->dfops
->dop_low
= true;
3817 if (args
.fsbno
!= NULLFSBLOCK
) {
3819 * check the allocation happened at the same or higher AG than
3820 * the first block that was allocated.
3822 ASSERT(*ap
->firstblock
== NULLFSBLOCK
||
3823 XFS_FSB_TO_AGNO(mp
, *ap
->firstblock
) <=
3824 XFS_FSB_TO_AGNO(mp
, args
.fsbno
));
3826 ap
->blkno
= args
.fsbno
;
3827 if (*ap
->firstblock
== NULLFSBLOCK
)
3828 *ap
->firstblock
= args
.fsbno
;
3829 ASSERT(nullfb
|| fb_agno
<= args
.agno
);
3830 ap
->length
= args
.len
;
3831 if (!(ap
->flags
& XFS_BMAPI_COWFORK
))
3832 ap
->ip
->i_d
.di_nblocks
+= args
.len
;
3833 xfs_trans_log_inode(ap
->tp
, ap
->ip
, XFS_ILOG_CORE
);
3835 ap
->ip
->i_delayed_blks
-= args
.len
;
3837 * Adjust the disk quota also. This was reserved
3840 xfs_trans_mod_dquot_byino(ap
->tp
, ap
->ip
,
3841 ap
->wasdel
? XFS_TRANS_DQ_DELBCOUNT
:
3842 XFS_TRANS_DQ_BCOUNT
,
3845 ap
->blkno
= NULLFSBLOCK
;
3852 * For a remap operation, just "allocate" an extent at the address that the
3853 * caller passed in, and ensure that the AGFL is the right size. The caller
3854 * will then map the "allocated" extent into the file somewhere.
3857 xfs_bmap_remap_alloc(
3858 struct xfs_bmalloca
*ap
)
3860 struct xfs_trans
*tp
= ap
->tp
;
3861 struct xfs_mount
*mp
= tp
->t_mountp
;
3863 struct xfs_alloc_arg args
;
3867 * validate that the block number is legal - the enables us to detect
3868 * and handle a silent filesystem corruption rather than crashing.
3870 memset(&args
, 0, sizeof(struct xfs_alloc_arg
));
3872 args
.mp
= ap
->tp
->t_mountp
;
3873 bno
= *ap
->firstblock
;
3874 args
.agno
= XFS_FSB_TO_AGNO(mp
, bno
);
3875 args
.agbno
= XFS_FSB_TO_AGBNO(mp
, bno
);
3876 if (args
.agno
>= mp
->m_sb
.sb_agcount
||
3877 args
.agbno
>= mp
->m_sb
.sb_agblocks
)
3878 return -EFSCORRUPTED
;
3880 /* "Allocate" the extent from the range we passed in. */
3881 trace_xfs_bmap_remap_alloc(ap
->ip
, *ap
->firstblock
, ap
->length
);
3883 ap
->ip
->i_d
.di_nblocks
+= ap
->length
;
3884 xfs_trans_log_inode(ap
->tp
, ap
->ip
, XFS_ILOG_CORE
);
3886 /* Fix the freelist, like a real allocator does. */
3887 args
.datatype
= ap
->datatype
;
3888 args
.pag
= xfs_perag_get(args
.mp
, args
.agno
);
3892 * The freelist fixing code will decline the allocation if
3893 * the size and shape of the free space doesn't allow for
3894 * allocating the extent and updating all the metadata that
3895 * happens during an allocation. We're remapping, not
3896 * allocating, so skip that check by pretending to be freeing.
3898 error
= xfs_alloc_fix_freelist(&args
, XFS_ALLOC_FLAG_FREEING
);
3899 xfs_perag_put(args
.pag
);
3901 trace_xfs_bmap_remap_alloc_error(ap
->ip
, error
, _RET_IP_
);
3906 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
3907 * It figures out where to ask the underlying allocator to put the new extent.
3911 struct xfs_bmalloca
*ap
) /* bmap alloc argument struct */
3913 if (ap
->flags
& XFS_BMAPI_REMAP
)
3914 return xfs_bmap_remap_alloc(ap
);
3915 if (XFS_IS_REALTIME_INODE(ap
->ip
) &&
3916 xfs_alloc_is_userdata(ap
->datatype
))
3917 return xfs_bmap_rtalloc(ap
);
3918 return xfs_bmap_btalloc(ap
);
3921 /* Trim extent to fit a logical block range. */
3924 struct xfs_bmbt_irec
*irec
,
3928 xfs_fileoff_t distance
;
3929 xfs_fileoff_t end
= bno
+ len
;
3931 if (irec
->br_startoff
+ irec
->br_blockcount
<= bno
||
3932 irec
->br_startoff
>= end
) {
3933 irec
->br_blockcount
= 0;
3937 if (irec
->br_startoff
< bno
) {
3938 distance
= bno
- irec
->br_startoff
;
3939 if (isnullstartblock(irec
->br_startblock
))
3940 irec
->br_startblock
= DELAYSTARTBLOCK
;
3941 if (irec
->br_startblock
!= DELAYSTARTBLOCK
&&
3942 irec
->br_startblock
!= HOLESTARTBLOCK
)
3943 irec
->br_startblock
+= distance
;
3944 irec
->br_startoff
+= distance
;
3945 irec
->br_blockcount
-= distance
;
3948 if (end
< irec
->br_startoff
+ irec
->br_blockcount
) {
3949 distance
= irec
->br_startoff
+ irec
->br_blockcount
- end
;
3950 irec
->br_blockcount
-= distance
;
3955 * Trim the returned map to the required bounds
3959 struct xfs_bmbt_irec
*mval
,
3960 struct xfs_bmbt_irec
*got
,
3968 if ((flags
& XFS_BMAPI_ENTIRE
) ||
3969 got
->br_startoff
+ got
->br_blockcount
<= obno
) {
3971 if (isnullstartblock(got
->br_startblock
))
3972 mval
->br_startblock
= DELAYSTARTBLOCK
;
3978 ASSERT((*bno
>= obno
) || (n
== 0));
3980 mval
->br_startoff
= *bno
;
3981 if (isnullstartblock(got
->br_startblock
))
3982 mval
->br_startblock
= DELAYSTARTBLOCK
;
3984 mval
->br_startblock
= got
->br_startblock
+
3985 (*bno
- got
->br_startoff
);
3987 * Return the minimum of what we got and what we asked for for
3988 * the length. We can use the len variable here because it is
3989 * modified below and we could have been there before coming
3990 * here if the first part of the allocation didn't overlap what
3993 mval
->br_blockcount
= XFS_FILBLKS_MIN(end
- *bno
,
3994 got
->br_blockcount
- (*bno
- got
->br_startoff
));
3995 mval
->br_state
= got
->br_state
;
3996 ASSERT(mval
->br_blockcount
<= len
);
4001 * Update and validate the extent map to return
4004 xfs_bmapi_update_map(
4005 struct xfs_bmbt_irec
**map
,
4013 xfs_bmbt_irec_t
*mval
= *map
;
4015 ASSERT((flags
& XFS_BMAPI_ENTIRE
) ||
4016 ((mval
->br_startoff
+ mval
->br_blockcount
) <= end
));
4017 ASSERT((flags
& XFS_BMAPI_ENTIRE
) || (mval
->br_blockcount
<= *len
) ||
4018 (mval
->br_startoff
< obno
));
4020 *bno
= mval
->br_startoff
+ mval
->br_blockcount
;
4022 if (*n
> 0 && mval
->br_startoff
== mval
[-1].br_startoff
) {
4023 /* update previous map with new information */
4024 ASSERT(mval
->br_startblock
== mval
[-1].br_startblock
);
4025 ASSERT(mval
->br_blockcount
> mval
[-1].br_blockcount
);
4026 ASSERT(mval
->br_state
== mval
[-1].br_state
);
4027 mval
[-1].br_blockcount
= mval
->br_blockcount
;
4028 mval
[-1].br_state
= mval
->br_state
;
4029 } else if (*n
> 0 && mval
->br_startblock
!= DELAYSTARTBLOCK
&&
4030 mval
[-1].br_startblock
!= DELAYSTARTBLOCK
&&
4031 mval
[-1].br_startblock
!= HOLESTARTBLOCK
&&
4032 mval
->br_startblock
== mval
[-1].br_startblock
+
4033 mval
[-1].br_blockcount
&&
4034 ((flags
& XFS_BMAPI_IGSTATE
) ||
4035 mval
[-1].br_state
== mval
->br_state
)) {
4036 ASSERT(mval
->br_startoff
==
4037 mval
[-1].br_startoff
+ mval
[-1].br_blockcount
);
4038 mval
[-1].br_blockcount
+= mval
->br_blockcount
;
4039 } else if (*n
> 0 &&
4040 mval
->br_startblock
== DELAYSTARTBLOCK
&&
4041 mval
[-1].br_startblock
== DELAYSTARTBLOCK
&&
4042 mval
->br_startoff
==
4043 mval
[-1].br_startoff
+ mval
[-1].br_blockcount
) {
4044 mval
[-1].br_blockcount
+= mval
->br_blockcount
;
4045 mval
[-1].br_state
= mval
->br_state
;
4046 } else if (!((*n
== 0) &&
4047 ((mval
->br_startoff
+ mval
->br_blockcount
) <=
4056 * Map file blocks to filesystem blocks without allocation.
4060 struct xfs_inode
*ip
,
4063 struct xfs_bmbt_irec
*mval
,
4067 struct xfs_mount
*mp
= ip
->i_mount
;
4068 struct xfs_ifork
*ifp
;
4069 struct xfs_bmbt_irec got
;
4076 int whichfork
= xfs_bmapi_whichfork(flags
);
4079 ASSERT(!(flags
& ~(XFS_BMAPI_ATTRFORK
|XFS_BMAPI_ENTIRE
|
4080 XFS_BMAPI_IGSTATE
|XFS_BMAPI_COWFORK
)));
4081 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_SHARED
|XFS_ILOCK_EXCL
));
4083 if (unlikely(XFS_TEST_ERROR(
4084 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
4085 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
4086 mp
, XFS_ERRTAG_BMAPIFORMAT
, XFS_RANDOM_BMAPIFORMAT
))) {
4087 XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW
, mp
);
4088 return -EFSCORRUPTED
;
4091 if (XFS_FORCED_SHUTDOWN(mp
))
4094 XFS_STATS_INC(mp
, xs_blk_mapr
);
4096 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4098 /* No CoW fork? Return a hole. */
4099 if (whichfork
== XFS_COW_FORK
&& !ifp
) {
4100 mval
->br_startoff
= bno
;
4101 mval
->br_startblock
= HOLESTARTBLOCK
;
4102 mval
->br_blockcount
= len
;
4103 mval
->br_state
= XFS_EXT_NORM
;
4108 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
4109 error
= xfs_iread_extents(NULL
, ip
, whichfork
);
4114 if (!xfs_iext_lookup_extent(ip
, ifp
, bno
, &idx
, &got
))
4119 while (bno
< end
&& n
< *nmap
) {
4120 /* Reading past eof, act as though there's a hole up to end. */
4122 got
.br_startoff
= end
;
4123 if (got
.br_startoff
> bno
) {
4124 /* Reading in a hole. */
4125 mval
->br_startoff
= bno
;
4126 mval
->br_startblock
= HOLESTARTBLOCK
;
4127 mval
->br_blockcount
=
4128 XFS_FILBLKS_MIN(len
, got
.br_startoff
- bno
);
4129 mval
->br_state
= XFS_EXT_NORM
;
4130 bno
+= mval
->br_blockcount
;
4131 len
-= mval
->br_blockcount
;
4137 /* set up the extent map to return. */
4138 xfs_bmapi_trim_map(mval
, &got
, &bno
, len
, obno
, end
, n
, flags
);
4139 xfs_bmapi_update_map(&mval
, &bno
, &len
, obno
, end
, &n
, flags
);
4141 /* If we're done, stop now. */
4142 if (bno
>= end
|| n
>= *nmap
)
4145 /* Else go on to the next record. */
4146 if (!xfs_iext_get_extent(ifp
, ++idx
, &got
))
4154 xfs_bmapi_reserve_delalloc(
4155 struct xfs_inode
*ip
,
4159 xfs_filblks_t prealloc
,
4160 struct xfs_bmbt_irec
*got
,
4161 xfs_extnum_t
*lastx
,
4164 struct xfs_mount
*mp
= ip
->i_mount
;
4165 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4167 xfs_extlen_t indlen
;
4168 char rt
= XFS_IS_REALTIME_INODE(ip
);
4171 xfs_fileoff_t aoff
= off
;
4174 * Cap the alloc length. Keep track of prealloc so we know whether to
4175 * tag the inode before we return.
4177 alen
= XFS_FILBLKS_MIN(len
+ prealloc
, MAXEXTLEN
);
4179 alen
= XFS_FILBLKS_MIN(alen
, got
->br_startoff
- aoff
);
4180 if (prealloc
&& alen
>= len
)
4181 prealloc
= alen
- len
;
4183 /* Figure out the extent size, adjust alen */
4184 if (whichfork
== XFS_COW_FORK
)
4185 extsz
= xfs_get_cowextsz_hint(ip
);
4187 extsz
= xfs_get_extsz_hint(ip
);
4189 struct xfs_bmbt_irec prev
;
4191 if (!xfs_iext_get_extent(ifp
, *lastx
- 1, &prev
))
4192 prev
.br_startoff
= NULLFILEOFF
;
4194 error
= xfs_bmap_extsize_align(mp
, got
, &prev
, extsz
, rt
, eof
,
4195 1, 0, &aoff
, &alen
);
4200 extsz
= alen
/ mp
->m_sb
.sb_rextsize
;
4203 * Make a transaction-less quota reservation for delayed allocation
4204 * blocks. This number gets adjusted later. We return if we haven't
4205 * allocated blocks already inside this loop.
4207 error
= xfs_trans_reserve_quota_nblks(NULL
, ip
, (long)alen
, 0,
4208 rt
? XFS_QMOPT_RES_RTBLKS
: XFS_QMOPT_RES_REGBLKS
);
4213 * Split changing sb for alen and indlen since they could be coming
4214 * from different places.
4216 indlen
= (xfs_extlen_t
)xfs_bmap_worst_indlen(ip
, alen
);
4220 error
= xfs_mod_frextents(mp
, -((int64_t)extsz
));
4222 error
= xfs_mod_fdblocks(mp
, -((int64_t)alen
), false);
4226 goto out_unreserve_quota
;
4228 error
= xfs_mod_fdblocks(mp
, -((int64_t)indlen
), false);
4230 goto out_unreserve_blocks
;
4233 ip
->i_delayed_blks
+= alen
;
4235 got
->br_startoff
= aoff
;
4236 got
->br_startblock
= nullstartblock(indlen
);
4237 got
->br_blockcount
= alen
;
4238 got
->br_state
= XFS_EXT_NORM
;
4239 xfs_bmap_add_extent_hole_delay(ip
, whichfork
, lastx
, got
);
4242 * Update our extent pointer, given that xfs_bmap_add_extent_hole_delay
4243 * might have merged it into one of the neighbouring ones.
4245 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, *lastx
), got
);
4248 * Tag the inode if blocks were preallocated. Note that COW fork
4249 * preallocation can occur at the start or end of the extent, even when
4250 * prealloc == 0, so we must also check the aligned offset and length.
4252 if (whichfork
== XFS_DATA_FORK
&& prealloc
)
4253 xfs_inode_set_eofblocks_tag(ip
);
4254 if (whichfork
== XFS_COW_FORK
&& (prealloc
|| aoff
< off
|| alen
> len
))
4255 xfs_inode_set_cowblocks_tag(ip
);
4257 ASSERT(got
->br_startoff
<= aoff
);
4258 ASSERT(got
->br_startoff
+ got
->br_blockcount
>= aoff
+ alen
);
4259 ASSERT(isnullstartblock(got
->br_startblock
));
4260 ASSERT(got
->br_state
== XFS_EXT_NORM
);
4263 out_unreserve_blocks
:
4265 xfs_mod_frextents(mp
, extsz
);
4267 xfs_mod_fdblocks(mp
, alen
, false);
4268 out_unreserve_quota
:
4269 if (XFS_IS_QUOTA_ON(mp
))
4270 xfs_trans_unreserve_quota_nblks(NULL
, ip
, (long)alen
, 0, rt
?
4271 XFS_QMOPT_RES_RTBLKS
: XFS_QMOPT_RES_REGBLKS
);
4277 struct xfs_bmalloca
*bma
)
4279 struct xfs_mount
*mp
= bma
->ip
->i_mount
;
4280 int whichfork
= xfs_bmapi_whichfork(bma
->flags
);
4281 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
4282 int tmp_logflags
= 0;
4285 ASSERT(bma
->length
> 0);
4288 * For the wasdelay case, we could also just allocate the stuff asked
4289 * for in this bmap call but that wouldn't be as good.
4292 bma
->length
= (xfs_extlen_t
)bma
->got
.br_blockcount
;
4293 bma
->offset
= bma
->got
.br_startoff
;
4295 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
- 1),
4299 bma
->length
= XFS_FILBLKS_MIN(bma
->length
, MAXEXTLEN
);
4301 bma
->length
= XFS_FILBLKS_MIN(bma
->length
,
4302 bma
->got
.br_startoff
- bma
->offset
);
4306 * Set the data type being allocated. For the data fork, the first data
4307 * in the file is treated differently to all other allocations. For the
4308 * attribute fork, we only need to ensure the allocated range is not on
4311 if (!(bma
->flags
& XFS_BMAPI_METADATA
)) {
4312 bma
->datatype
= XFS_ALLOC_NOBUSY
;
4313 if (whichfork
== XFS_DATA_FORK
) {
4314 if (bma
->offset
== 0)
4315 bma
->datatype
|= XFS_ALLOC_INITIAL_USER_DATA
;
4317 bma
->datatype
|= XFS_ALLOC_USERDATA
;
4319 if (bma
->flags
& XFS_BMAPI_ZERO
)
4320 bma
->datatype
|= XFS_ALLOC_USERDATA_ZERO
;
4323 bma
->minlen
= (bma
->flags
& XFS_BMAPI_CONTIG
) ? bma
->length
: 1;
4326 * Only want to do the alignment at the eof if it is userdata and
4327 * allocation length is larger than a stripe unit.
4329 if (mp
->m_dalign
&& bma
->length
>= mp
->m_dalign
&&
4330 !(bma
->flags
& XFS_BMAPI_METADATA
) && whichfork
== XFS_DATA_FORK
) {
4331 error
= xfs_bmap_isaeof(bma
, whichfork
);
4336 error
= xfs_bmap_alloc(bma
);
4341 bma
->cur
->bc_private
.b
.firstblock
= *bma
->firstblock
;
4342 if (bma
->blkno
== NULLFSBLOCK
)
4344 if ((ifp
->if_flags
& XFS_IFBROOT
) && !bma
->cur
) {
4345 bma
->cur
= xfs_bmbt_init_cursor(mp
, bma
->tp
, bma
->ip
, whichfork
);
4346 bma
->cur
->bc_private
.b
.firstblock
= *bma
->firstblock
;
4347 bma
->cur
->bc_private
.b
.dfops
= bma
->dfops
;
4350 * Bump the number of extents we've allocated
4356 bma
->cur
->bc_private
.b
.flags
=
4357 bma
->wasdel
? XFS_BTCUR_BPRV_WASDEL
: 0;
4359 bma
->got
.br_startoff
= bma
->offset
;
4360 bma
->got
.br_startblock
= bma
->blkno
;
4361 bma
->got
.br_blockcount
= bma
->length
;
4362 bma
->got
.br_state
= XFS_EXT_NORM
;
4365 * In the data fork, a wasdelay extent has been initialized, so
4366 * shouldn't be flagged as unwritten.
4368 * For the cow fork, however, we convert delalloc reservations
4369 * (extents allocated for speculative preallocation) to
4370 * allocated unwritten extents, and only convert the unwritten
4371 * extents to real extents when we're about to write the data.
4373 if ((!bma
->wasdel
|| (bma
->flags
& XFS_BMAPI_COWFORK
)) &&
4374 (bma
->flags
& XFS_BMAPI_PREALLOC
) &&
4375 xfs_sb_version_hasextflgbit(&mp
->m_sb
))
4376 bma
->got
.br_state
= XFS_EXT_UNWRITTEN
;
4379 error
= xfs_bmap_add_extent_delay_real(bma
, whichfork
);
4381 error
= xfs_bmap_add_extent_hole_real(bma
, whichfork
);
4383 bma
->logflags
|= tmp_logflags
;
4388 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4389 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4390 * the neighbouring ones.
4392 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
), &bma
->got
);
4394 ASSERT(bma
->got
.br_startoff
<= bma
->offset
);
4395 ASSERT(bma
->got
.br_startoff
+ bma
->got
.br_blockcount
>=
4396 bma
->offset
+ bma
->length
);
4397 ASSERT(bma
->got
.br_state
== XFS_EXT_NORM
||
4398 bma
->got
.br_state
== XFS_EXT_UNWRITTEN
);
4403 xfs_bmapi_convert_unwritten(
4404 struct xfs_bmalloca
*bma
,
4405 struct xfs_bmbt_irec
*mval
,
4409 int whichfork
= xfs_bmapi_whichfork(flags
);
4410 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
4411 int tmp_logflags
= 0;
4414 /* check if we need to do unwritten->real conversion */
4415 if (mval
->br_state
== XFS_EXT_UNWRITTEN
&&
4416 (flags
& XFS_BMAPI_PREALLOC
))
4419 /* check if we need to do real->unwritten conversion */
4420 if (mval
->br_state
== XFS_EXT_NORM
&&
4421 (flags
& (XFS_BMAPI_PREALLOC
| XFS_BMAPI_CONVERT
)) !=
4422 (XFS_BMAPI_PREALLOC
| XFS_BMAPI_CONVERT
))
4426 * Modify (by adding) the state flag, if writing.
4428 ASSERT(mval
->br_blockcount
<= len
);
4429 if ((ifp
->if_flags
& XFS_IFBROOT
) && !bma
->cur
) {
4430 bma
->cur
= xfs_bmbt_init_cursor(bma
->ip
->i_mount
, bma
->tp
,
4431 bma
->ip
, whichfork
);
4432 bma
->cur
->bc_private
.b
.firstblock
= *bma
->firstblock
;
4433 bma
->cur
->bc_private
.b
.dfops
= bma
->dfops
;
4435 mval
->br_state
= (mval
->br_state
== XFS_EXT_UNWRITTEN
)
4436 ? XFS_EXT_NORM
: XFS_EXT_UNWRITTEN
;
4439 * Before insertion into the bmbt, zero the range being converted
4442 if (flags
& XFS_BMAPI_ZERO
) {
4443 error
= xfs_zero_extent(bma
->ip
, mval
->br_startblock
,
4444 mval
->br_blockcount
);
4449 error
= xfs_bmap_add_extent_unwritten_real(bma
->tp
, bma
->ip
, whichfork
,
4450 &bma
->idx
, &bma
->cur
, mval
, bma
->firstblock
, bma
->dfops
,
4453 * Log the inode core unconditionally in the unwritten extent conversion
4454 * path because the conversion might not have done so (e.g., if the
4455 * extent count hasn't changed). We need to make sure the inode is dirty
4456 * in the transaction for the sake of fsync(), even if nothing has
4457 * changed, because fsync() will not force the log for this transaction
4458 * unless it sees the inode pinned.
4460 * Note: If we're only converting cow fork extents, there aren't
4461 * any on-disk updates to make, so we don't need to log anything.
4463 if (whichfork
!= XFS_COW_FORK
)
4464 bma
->logflags
|= tmp_logflags
| XFS_ILOG_CORE
;
4469 * Update our extent pointer, given that
4470 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4471 * of the neighbouring ones.
4473 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
), &bma
->got
);
4476 * We may have combined previously unwritten space with written space,
4477 * so generate another request.
4479 if (mval
->br_blockcount
< len
)
4485 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4486 * extent state if necessary. Details behaviour is controlled by the flags
4487 * parameter. Only allocates blocks from a single allocation group, to avoid
4490 * The returned value in "firstblock" from the first call in a transaction
4491 * must be remembered and presented to subsequent calls in "firstblock".
4492 * An upper bound for the number of blocks to be allocated is supplied to
4493 * the first call in "total"; if no allocation group has that many free
4494 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4498 struct xfs_trans
*tp
, /* transaction pointer */
4499 struct xfs_inode
*ip
, /* incore inode */
4500 xfs_fileoff_t bno
, /* starting file offs. mapped */
4501 xfs_filblks_t len
, /* length to map in file */
4502 int flags
, /* XFS_BMAPI_... */
4503 xfs_fsblock_t
*firstblock
, /* first allocated block
4504 controls a.g. for allocs */
4505 xfs_extlen_t total
, /* total blocks needed */
4506 struct xfs_bmbt_irec
*mval
, /* output: map values */
4507 int *nmap
, /* i/o: mval size/count */
4508 struct xfs_defer_ops
*dfops
) /* i/o: list extents to free */
4510 struct xfs_mount
*mp
= ip
->i_mount
;
4511 struct xfs_ifork
*ifp
;
4512 struct xfs_bmalloca bma
= { NULL
}; /* args for xfs_bmap_alloc */
4513 xfs_fileoff_t end
; /* end of mapped file region */
4514 bool eof
= false; /* after the end of extents */
4515 int error
; /* error return */
4516 int n
; /* current extent index */
4517 xfs_fileoff_t obno
; /* old block number (offset) */
4518 int whichfork
; /* data or attr fork */
4521 xfs_fileoff_t orig_bno
; /* original block number value */
4522 int orig_flags
; /* original flags arg value */
4523 xfs_filblks_t orig_len
; /* original value of len arg */
4524 struct xfs_bmbt_irec
*orig_mval
; /* original value of mval */
4525 int orig_nmap
; /* original value of *nmap */
4533 whichfork
= xfs_bmapi_whichfork(flags
);
4536 ASSERT(*nmap
<= XFS_BMAP_MAX_NMAP
);
4537 ASSERT(!(flags
& XFS_BMAPI_IGSTATE
));
4538 ASSERT(tp
!= NULL
||
4539 (flags
& (XFS_BMAPI_CONVERT
| XFS_BMAPI_COWFORK
)) ==
4540 (XFS_BMAPI_CONVERT
| XFS_BMAPI_COWFORK
));
4542 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_LOCAL
);
4543 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
4544 ASSERT(!(flags
& XFS_BMAPI_REMAP
) || whichfork
== XFS_DATA_FORK
);
4545 ASSERT(!(flags
& XFS_BMAPI_PREALLOC
) || !(flags
& XFS_BMAPI_REMAP
));
4546 ASSERT(!(flags
& XFS_BMAPI_CONVERT
) || !(flags
& XFS_BMAPI_REMAP
));
4548 /* zeroing is for currently only for data extents, not metadata */
4549 ASSERT((flags
& (XFS_BMAPI_METADATA
| XFS_BMAPI_ZERO
)) !=
4550 (XFS_BMAPI_METADATA
| XFS_BMAPI_ZERO
));
4552 * we can allocate unwritten extents or pre-zero allocated blocks,
4553 * but it makes no sense to do both at once. This would result in
4554 * zeroing the unwritten extent twice, but it still being an
4555 * unwritten extent....
4557 ASSERT((flags
& (XFS_BMAPI_PREALLOC
| XFS_BMAPI_ZERO
)) !=
4558 (XFS_BMAPI_PREALLOC
| XFS_BMAPI_ZERO
));
4560 if (unlikely(XFS_TEST_ERROR(
4561 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
4562 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
4563 mp
, XFS_ERRTAG_BMAPIFORMAT
, XFS_RANDOM_BMAPIFORMAT
))) {
4564 XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW
, mp
);
4565 return -EFSCORRUPTED
;
4568 if (XFS_FORCED_SHUTDOWN(mp
))
4571 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4573 XFS_STATS_INC(mp
, xs_blk_mapw
);
4575 if (*firstblock
== NULLFSBLOCK
) {
4576 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
)
4577 bma
.minleft
= be16_to_cpu(ifp
->if_broot
->bb_level
) + 1;
4584 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
4585 error
= xfs_iread_extents(tp
, ip
, whichfork
);
4594 if (!xfs_iext_lookup_extent(ip
, ifp
, bno
, &bma
.idx
, &bma
.got
))
4596 if (!xfs_iext_get_extent(ifp
, bma
.idx
- 1, &bma
.prev
))
4597 bma
.prev
.br_startoff
= NULLFILEOFF
;
4603 bma
.firstblock
= firstblock
;
4605 while (bno
< end
&& n
< *nmap
) {
4606 bool need_alloc
= false, wasdelay
= false;
4608 /* in hole or beyoned EOF? */
4609 if (eof
|| bma
.got
.br_startoff
> bno
) {
4610 if (flags
& XFS_BMAPI_DELALLOC
) {
4612 * For the COW fork we can reasonably get a
4613 * request for converting an extent that races
4614 * with other threads already having converted
4615 * part of it, as there converting COW to
4616 * regular blocks is not protected using the
4619 ASSERT(flags
& XFS_BMAPI_COWFORK
);
4620 if (!(flags
& XFS_BMAPI_COWFORK
)) {
4625 if (eof
|| bno
>= end
)
4632 * Make sure we only reflink into a hole.
4634 ASSERT(!(flags
& XFS_BMAPI_REMAP
));
4635 if (isnullstartblock(bma
.got
.br_startblock
))
4640 * First, deal with the hole before the allocated space
4641 * that we found, if any.
4643 if (need_alloc
|| wasdelay
) {
4645 bma
.conv
= !!(flags
& XFS_BMAPI_CONVERT
);
4646 bma
.wasdel
= wasdelay
;
4651 * There's a 32/64 bit type mismatch between the
4652 * allocation length request (which can be 64 bits in
4653 * length) and the bma length request, which is
4654 * xfs_extlen_t and therefore 32 bits. Hence we have to
4655 * check for 32-bit overflows and handle them here.
4657 if (len
> (xfs_filblks_t
)MAXEXTLEN
)
4658 bma
.length
= MAXEXTLEN
;
4663 ASSERT(bma
.length
> 0);
4664 error
= xfs_bmapi_allocate(&bma
);
4667 if (bma
.blkno
== NULLFSBLOCK
)
4671 * If this is a CoW allocation, record the data in
4672 * the refcount btree for orphan recovery.
4674 if (whichfork
== XFS_COW_FORK
) {
4675 error
= xfs_refcount_alloc_cow_extent(mp
, dfops
,
4676 bma
.blkno
, bma
.length
);
4682 /* Deal with the allocated space we found. */
4683 xfs_bmapi_trim_map(mval
, &bma
.got
, &bno
, len
, obno
,
4686 /* Execute unwritten extent conversion if necessary */
4687 error
= xfs_bmapi_convert_unwritten(&bma
, mval
, len
, flags
);
4688 if (error
== -EAGAIN
)
4693 /* update the extent map to return */
4694 xfs_bmapi_update_map(&mval
, &bno
, &len
, obno
, end
, &n
, flags
);
4697 * If we're done, stop now. Stop when we've allocated
4698 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4699 * the transaction may get too big.
4701 if (bno
>= end
|| n
>= *nmap
|| bma
.nallocs
>= *nmap
)
4704 /* Else go on to the next record. */
4706 if (!xfs_iext_get_extent(ifp
, ++bma
.idx
, &bma
.got
))
4712 * Transform from btree to extents, give it cur.
4714 if (xfs_bmap_wants_extents(ip
, whichfork
)) {
4715 int tmp_logflags
= 0;
4718 error
= xfs_bmap_btree_to_extents(tp
, ip
, bma
.cur
,
4719 &tmp_logflags
, whichfork
);
4720 bma
.logflags
|= tmp_logflags
;
4725 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
||
4726 XFS_IFORK_NEXTENTS(ip
, whichfork
) >
4727 XFS_IFORK_MAXEXT(ip
, whichfork
));
4731 * Log everything. Do this after conversion, there's no point in
4732 * logging the extent records if we've converted to btree format.
4734 if ((bma
.logflags
& xfs_ilog_fext(whichfork
)) &&
4735 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
4736 bma
.logflags
&= ~xfs_ilog_fext(whichfork
);
4737 else if ((bma
.logflags
& xfs_ilog_fbroot(whichfork
)) &&
4738 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)
4739 bma
.logflags
&= ~xfs_ilog_fbroot(whichfork
);
4741 * Log whatever the flags say, even if error. Otherwise we might miss
4742 * detecting a case where the data is changed, there's an error,
4743 * and it's not logged so we don't shutdown when we should.
4746 xfs_trans_log_inode(tp
, ip
, bma
.logflags
);
4750 ASSERT(*firstblock
== NULLFSBLOCK
||
4751 XFS_FSB_TO_AGNO(mp
, *firstblock
) <=
4753 bma
.cur
->bc_private
.b
.firstblock
));
4754 *firstblock
= bma
.cur
->bc_private
.b
.firstblock
;
4756 xfs_btree_del_cursor(bma
.cur
,
4757 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
4760 xfs_bmap_validate_ret(orig_bno
, orig_len
, orig_flags
, orig_mval
,
4766 * When a delalloc extent is split (e.g., due to a hole punch), the original
4767 * indlen reservation must be shared across the two new extents that are left
4770 * Given the original reservation and the worst case indlen for the two new
4771 * extents (as calculated by xfs_bmap_worst_indlen()), split the original
4772 * reservation fairly across the two new extents. If necessary, steal available
4773 * blocks from a deleted extent to make up a reservation deficiency (e.g., if
4774 * ores == 1). The number of stolen blocks is returned. The availability and
4775 * subsequent accounting of stolen blocks is the responsibility of the caller.
4777 static xfs_filblks_t
4778 xfs_bmap_split_indlen(
4779 xfs_filblks_t ores
, /* original res. */
4780 xfs_filblks_t
*indlen1
, /* ext1 worst indlen */
4781 xfs_filblks_t
*indlen2
, /* ext2 worst indlen */
4782 xfs_filblks_t avail
) /* stealable blocks */
4784 xfs_filblks_t len1
= *indlen1
;
4785 xfs_filblks_t len2
= *indlen2
;
4786 xfs_filblks_t nres
= len1
+ len2
; /* new total res. */
4787 xfs_filblks_t stolen
= 0;
4788 xfs_filblks_t resfactor
;
4791 * Steal as many blocks as we can to try and satisfy the worst case
4792 * indlen for both new extents.
4794 if (ores
< nres
&& avail
)
4795 stolen
= XFS_FILBLKS_MIN(nres
- ores
, avail
);
4798 /* nothing else to do if we've satisfied the new reservation */
4803 * We can't meet the total required reservation for the two extents.
4804 * Calculate the percent of the overall shortage between both extents
4805 * and apply this percentage to each of the requested indlen values.
4806 * This distributes the shortage fairly and reduces the chances that one
4807 * of the two extents is left with nothing when extents are repeatedly
4810 resfactor
= (ores
* 100);
4811 do_div(resfactor
, nres
);
4816 ASSERT(len1
+ len2
<= ores
);
4817 ASSERT(len1
< *indlen1
&& len2
< *indlen2
);
4820 * Hand out the remainder to each extent. If one of the two reservations
4821 * is zero, we want to make sure that one gets a block first. The loop
4822 * below starts with len1, so hand len2 a block right off the bat if it
4825 ores
-= (len1
+ len2
);
4826 ASSERT((*indlen1
- len1
) + (*indlen2
- len2
) >= ores
);
4827 if (ores
&& !len2
&& *indlen2
) {
4832 if (len1
< *indlen1
) {
4838 if (len2
< *indlen2
) {
4851 xfs_bmap_del_extent_delay(
4852 struct xfs_inode
*ip
,
4855 struct xfs_bmbt_irec
*got
,
4856 struct xfs_bmbt_irec
*del
)
4858 struct xfs_mount
*mp
= ip
->i_mount
;
4859 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4860 struct xfs_bmbt_irec
new;
4861 int64_t da_old
, da_new
, da_diff
= 0;
4862 xfs_fileoff_t del_endoff
, got_endoff
;
4863 xfs_filblks_t got_indlen
, new_indlen
, stolen
;
4864 int error
= 0, state
= 0;
4867 XFS_STATS_INC(mp
, xs_del_exlist
);
4869 isrt
= (whichfork
== XFS_DATA_FORK
) && XFS_IS_REALTIME_INODE(ip
);
4870 del_endoff
= del
->br_startoff
+ del
->br_blockcount
;
4871 got_endoff
= got
->br_startoff
+ got
->br_blockcount
;
4872 da_old
= startblockval(got
->br_startblock
);
4876 ASSERT(*idx
<= xfs_iext_count(ifp
));
4877 ASSERT(del
->br_blockcount
> 0);
4878 ASSERT(got
->br_startoff
<= del
->br_startoff
);
4879 ASSERT(got_endoff
>= del_endoff
);
4882 int64_t rtexts
= XFS_FSB_TO_B(mp
, del
->br_blockcount
);
4884 do_div(rtexts
, mp
->m_sb
.sb_rextsize
);
4885 xfs_mod_frextents(mp
, rtexts
);
4889 * Update the inode delalloc counter now and wait to update the
4890 * sb counters as we might have to borrow some blocks for the
4891 * indirect block accounting.
4893 error
= xfs_trans_reserve_quota_nblks(NULL
, ip
,
4894 -((long)del
->br_blockcount
), 0,
4895 isrt
? XFS_QMOPT_RES_RTBLKS
: XFS_QMOPT_RES_REGBLKS
);
4898 ip
->i_delayed_blks
-= del
->br_blockcount
;
4900 if (whichfork
== XFS_COW_FORK
)
4901 state
|= BMAP_COWFORK
;
4903 if (got
->br_startoff
== del
->br_startoff
)
4904 state
|= BMAP_LEFT_CONTIG
;
4905 if (got_endoff
== del_endoff
)
4906 state
|= BMAP_RIGHT_CONTIG
;
4908 switch (state
& (BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
)) {
4909 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
4911 * Matches the whole extent. Delete the entry.
4913 xfs_iext_remove(ip
, *idx
, 1, state
);
4916 case BMAP_LEFT_CONTIG
:
4918 * Deleting the first part of the extent.
4920 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
4921 got
->br_startoff
= del_endoff
;
4922 got
->br_blockcount
-= del
->br_blockcount
;
4923 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
,
4924 got
->br_blockcount
), da_old
);
4925 got
->br_startblock
= nullstartblock((int)da_new
);
4926 xfs_bmbt_set_all(xfs_iext_get_ext(ifp
, *idx
), got
);
4927 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
4929 case BMAP_RIGHT_CONTIG
:
4931 * Deleting the last part of the extent.
4933 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
4934 got
->br_blockcount
= got
->br_blockcount
- del
->br_blockcount
;
4935 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
,
4936 got
->br_blockcount
), da_old
);
4937 got
->br_startblock
= nullstartblock((int)da_new
);
4938 xfs_bmbt_set_all(xfs_iext_get_ext(ifp
, *idx
), got
);
4939 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
4943 * Deleting the middle of the extent.
4945 * Distribute the original indlen reservation across the two new
4946 * extents. Steal blocks from the deleted extent if necessary.
4947 * Stealing blocks simply fudges the fdblocks accounting below.
4948 * Warn if either of the new indlen reservations is zero as this
4949 * can lead to delalloc problems.
4951 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
4953 got
->br_blockcount
= del
->br_startoff
- got
->br_startoff
;
4954 got_indlen
= xfs_bmap_worst_indlen(ip
, got
->br_blockcount
);
4956 new.br_blockcount
= got_endoff
- del_endoff
;
4957 new_indlen
= xfs_bmap_worst_indlen(ip
, new.br_blockcount
);
4959 WARN_ON_ONCE(!got_indlen
|| !new_indlen
);
4960 stolen
= xfs_bmap_split_indlen(da_old
, &got_indlen
, &new_indlen
,
4961 del
->br_blockcount
);
4963 got
->br_startblock
= nullstartblock((int)got_indlen
);
4964 xfs_bmbt_set_all(xfs_iext_get_ext(ifp
, *idx
), got
);
4965 trace_xfs_bmap_post_update(ip
, *idx
, 0, _THIS_IP_
);
4967 new.br_startoff
= del_endoff
;
4968 new.br_state
= got
->br_state
;
4969 new.br_startblock
= nullstartblock((int)new_indlen
);
4972 xfs_iext_insert(ip
, *idx
, 1, &new, state
);
4974 da_new
= got_indlen
+ new_indlen
- stolen
;
4975 del
->br_blockcount
-= stolen
;
4979 ASSERT(da_old
>= da_new
);
4980 da_diff
= da_old
- da_new
;
4982 da_diff
+= del
->br_blockcount
;
4984 xfs_mod_fdblocks(mp
, da_diff
, false);
4989 xfs_bmap_del_extent_cow(
4990 struct xfs_inode
*ip
,
4992 struct xfs_bmbt_irec
*got
,
4993 struct xfs_bmbt_irec
*del
)
4995 struct xfs_mount
*mp
= ip
->i_mount
;
4996 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, XFS_COW_FORK
);
4997 struct xfs_bmbt_irec
new;
4998 xfs_fileoff_t del_endoff
, got_endoff
;
4999 int state
= BMAP_COWFORK
;
5001 XFS_STATS_INC(mp
, xs_del_exlist
);
5003 del_endoff
= del
->br_startoff
+ del
->br_blockcount
;
5004 got_endoff
= got
->br_startoff
+ got
->br_blockcount
;
5007 ASSERT(*idx
<= xfs_iext_count(ifp
));
5008 ASSERT(del
->br_blockcount
> 0);
5009 ASSERT(got
->br_startoff
<= del
->br_startoff
);
5010 ASSERT(got_endoff
>= del_endoff
);
5011 ASSERT(!isnullstartblock(got
->br_startblock
));
5013 if (got
->br_startoff
== del
->br_startoff
)
5014 state
|= BMAP_LEFT_CONTIG
;
5015 if (got_endoff
== del_endoff
)
5016 state
|= BMAP_RIGHT_CONTIG
;
5018 switch (state
& (BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
)) {
5019 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
5021 * Matches the whole extent. Delete the entry.
5023 xfs_iext_remove(ip
, *idx
, 1, state
);
5026 case BMAP_LEFT_CONTIG
:
5028 * Deleting the first part of the extent.
5030 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
5031 got
->br_startoff
= del_endoff
;
5032 got
->br_blockcount
-= del
->br_blockcount
;
5033 got
->br_startblock
= del
->br_startblock
+ del
->br_blockcount
;
5034 xfs_bmbt_set_all(xfs_iext_get_ext(ifp
, *idx
), got
);
5035 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
5037 case BMAP_RIGHT_CONTIG
:
5039 * Deleting the last part of the extent.
5041 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
5042 got
->br_blockcount
-= del
->br_blockcount
;
5043 xfs_bmbt_set_all(xfs_iext_get_ext(ifp
, *idx
), got
);
5044 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
5048 * Deleting the middle of the extent.
5050 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
5051 got
->br_blockcount
= del
->br_startoff
- got
->br_startoff
;
5052 xfs_bmbt_set_all(xfs_iext_get_ext(ifp
, *idx
), got
);
5053 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
5055 new.br_startoff
= del_endoff
;
5056 new.br_blockcount
= got_endoff
- del_endoff
;
5057 new.br_state
= got
->br_state
;
5058 new.br_startblock
= del
->br_startblock
+ del
->br_blockcount
;
5061 xfs_iext_insert(ip
, *idx
, 1, &new, state
);
5067 * Called by xfs_bmapi to update file extent records and the btree
5068 * after removing space (or undoing a delayed allocation).
5070 STATIC
int /* error */
5071 xfs_bmap_del_extent(
5072 xfs_inode_t
*ip
, /* incore inode pointer */
5073 xfs_trans_t
*tp
, /* current transaction pointer */
5074 xfs_extnum_t
*idx
, /* extent number to update/delete */
5075 struct xfs_defer_ops
*dfops
, /* list of extents to be freed */
5076 xfs_btree_cur_t
*cur
, /* if null, not a btree */
5077 xfs_bmbt_irec_t
*del
, /* data to remove from extents */
5078 int *logflagsp
, /* inode logging flags */
5079 int whichfork
, /* data or attr fork */
5080 int bflags
) /* bmapi flags */
5082 xfs_filblks_t da_new
; /* new delay-alloc indirect blocks */
5083 xfs_filblks_t da_old
; /* old delay-alloc indirect blocks */
5084 xfs_fsblock_t del_endblock
=0; /* first block past del */
5085 xfs_fileoff_t del_endoff
; /* first offset past del */
5086 int delay
; /* current block is delayed allocated */
5087 int do_fx
; /* free extent at end of routine */
5088 xfs_bmbt_rec_host_t
*ep
; /* current extent entry pointer */
5089 int error
; /* error return value */
5090 int flags
; /* inode logging flags */
5091 xfs_bmbt_irec_t got
; /* current extent entry */
5092 xfs_fileoff_t got_endoff
; /* first offset past got */
5093 int i
; /* temp state */
5094 xfs_ifork_t
*ifp
; /* inode fork pointer */
5095 xfs_mount_t
*mp
; /* mount structure */
5096 xfs_filblks_t nblks
; /* quota/sb block count */
5097 xfs_bmbt_irec_t
new; /* new record to be inserted */
5099 uint qfield
; /* quota field to update */
5100 xfs_filblks_t temp
; /* for indirect length calculations */
5101 xfs_filblks_t temp2
; /* for indirect length calculations */
5105 XFS_STATS_INC(mp
, xs_del_exlist
);
5107 if (whichfork
== XFS_ATTR_FORK
)
5108 state
|= BMAP_ATTRFORK
;
5109 else if (whichfork
== XFS_COW_FORK
)
5110 state
|= BMAP_COWFORK
;
5112 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5113 ASSERT((*idx
>= 0) && (*idx
< xfs_iext_count(ifp
)));
5114 ASSERT(del
->br_blockcount
> 0);
5115 ep
= xfs_iext_get_ext(ifp
, *idx
);
5116 xfs_bmbt_get_all(ep
, &got
);
5117 ASSERT(got
.br_startoff
<= del
->br_startoff
);
5118 del_endoff
= del
->br_startoff
+ del
->br_blockcount
;
5119 got_endoff
= got
.br_startoff
+ got
.br_blockcount
;
5120 ASSERT(got_endoff
>= del_endoff
);
5121 delay
= isnullstartblock(got
.br_startblock
);
5122 ASSERT(isnullstartblock(del
->br_startblock
) == delay
);
5127 * If deleting a real allocation, must free up the disk space.
5130 flags
= XFS_ILOG_CORE
;
5132 * Realtime allocation. Free it and record di_nblocks update.
5134 if (whichfork
== XFS_DATA_FORK
&& XFS_IS_REALTIME_INODE(ip
)) {
5138 ASSERT(do_mod(del
->br_blockcount
,
5139 mp
->m_sb
.sb_rextsize
) == 0);
5140 ASSERT(do_mod(del
->br_startblock
,
5141 mp
->m_sb
.sb_rextsize
) == 0);
5142 bno
= del
->br_startblock
;
5143 len
= del
->br_blockcount
;
5144 do_div(bno
, mp
->m_sb
.sb_rextsize
);
5145 do_div(len
, mp
->m_sb
.sb_rextsize
);
5146 error
= xfs_rtfree_extent(tp
, bno
, (xfs_extlen_t
)len
);
5150 nblks
= len
* mp
->m_sb
.sb_rextsize
;
5151 qfield
= XFS_TRANS_DQ_RTBCOUNT
;
5154 * Ordinary allocation.
5158 nblks
= del
->br_blockcount
;
5159 qfield
= XFS_TRANS_DQ_BCOUNT
;
5162 * Set up del_endblock and cur for later.
5164 del_endblock
= del
->br_startblock
+ del
->br_blockcount
;
5166 if ((error
= xfs_bmbt_lookup_eq(cur
, got
.br_startoff
,
5167 got
.br_startblock
, got
.br_blockcount
,
5170 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
5172 da_old
= da_new
= 0;
5174 da_old
= startblockval(got
.br_startblock
);
5181 * Set flag value to use in switch statement.
5182 * Left-contig is 2, right-contig is 1.
5184 switch (((got
.br_startoff
== del
->br_startoff
) << 1) |
5185 (got_endoff
== del_endoff
)) {
5188 * Matches the whole extent. Delete the entry.
5190 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
5191 xfs_iext_remove(ip
, *idx
, 1,
5192 whichfork
== XFS_ATTR_FORK
? BMAP_ATTRFORK
: 0);
5197 XFS_IFORK_NEXT_SET(ip
, whichfork
,
5198 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
5199 flags
|= XFS_ILOG_CORE
;
5201 flags
|= xfs_ilog_fext(whichfork
);
5204 if ((error
= xfs_btree_delete(cur
, &i
)))
5206 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
5211 * Deleting the first part of the extent.
5213 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
5214 xfs_bmbt_set_startoff(ep
, del_endoff
);
5215 temp
= got
.br_blockcount
- del
->br_blockcount
;
5216 xfs_bmbt_set_blockcount(ep
, temp
);
5218 temp
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
5220 xfs_bmbt_set_startblock(ep
, nullstartblock((int)temp
));
5221 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
5225 xfs_bmbt_set_startblock(ep
, del_endblock
);
5226 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
5228 flags
|= xfs_ilog_fext(whichfork
);
5231 if ((error
= xfs_bmbt_update(cur
, del_endoff
, del_endblock
,
5232 got
.br_blockcount
- del
->br_blockcount
,
5239 * Deleting the last part of the extent.
5241 temp
= got
.br_blockcount
- del
->br_blockcount
;
5242 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
5243 xfs_bmbt_set_blockcount(ep
, temp
);
5245 temp
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
5247 xfs_bmbt_set_startblock(ep
, nullstartblock((int)temp
));
5248 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
5252 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
5254 flags
|= xfs_ilog_fext(whichfork
);
5257 if ((error
= xfs_bmbt_update(cur
, got
.br_startoff
,
5259 got
.br_blockcount
- del
->br_blockcount
,
5266 * Deleting the middle of the extent.
5268 temp
= del
->br_startoff
- got
.br_startoff
;
5269 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
5270 xfs_bmbt_set_blockcount(ep
, temp
);
5271 new.br_startoff
= del_endoff
;
5272 temp2
= got_endoff
- del_endoff
;
5273 new.br_blockcount
= temp2
;
5274 new.br_state
= got
.br_state
;
5276 new.br_startblock
= del_endblock
;
5277 flags
|= XFS_ILOG_CORE
;
5279 if ((error
= xfs_bmbt_update(cur
,
5281 got
.br_startblock
, temp
,
5284 if ((error
= xfs_btree_increment(cur
, 0, &i
)))
5286 cur
->bc_rec
.b
= new;
5287 error
= xfs_btree_insert(cur
, &i
);
5288 if (error
&& error
!= -ENOSPC
)
5291 * If get no-space back from btree insert,
5292 * it tried a split, and we have a zero
5293 * block reservation.
5294 * Fix up our state and return the error.
5296 if (error
== -ENOSPC
) {
5298 * Reset the cursor, don't trust
5299 * it after any insert operation.
5301 if ((error
= xfs_bmbt_lookup_eq(cur
,
5306 XFS_WANT_CORRUPTED_GOTO(mp
,
5309 * Update the btree record back
5310 * to the original value.
5312 if ((error
= xfs_bmbt_update(cur
,
5319 * Reset the extent record back
5320 * to the original value.
5322 xfs_bmbt_set_blockcount(ep
,
5328 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
5330 flags
|= xfs_ilog_fext(whichfork
);
5331 XFS_IFORK_NEXT_SET(ip
, whichfork
,
5332 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
5334 xfs_filblks_t stolen
;
5335 ASSERT(whichfork
== XFS_DATA_FORK
);
5338 * Distribute the original indlen reservation across the
5339 * two new extents. Steal blocks from the deleted extent
5340 * if necessary. Stealing blocks simply fudges the
5341 * fdblocks accounting in xfs_bunmapi().
5343 temp
= xfs_bmap_worst_indlen(ip
, got
.br_blockcount
);
5344 temp2
= xfs_bmap_worst_indlen(ip
, new.br_blockcount
);
5345 stolen
= xfs_bmap_split_indlen(da_old
, &temp
, &temp2
,
5346 del
->br_blockcount
);
5347 da_new
= temp
+ temp2
- stolen
;
5348 del
->br_blockcount
-= stolen
;
5351 * Set the reservation for each extent. Warn if either
5352 * is zero as this can lead to delalloc problems.
5354 WARN_ON_ONCE(!temp
|| !temp2
);
5355 xfs_bmbt_set_startblock(ep
, nullstartblock((int)temp
));
5356 new.br_startblock
= nullstartblock((int)temp2
);
5358 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
5359 xfs_iext_insert(ip
, *idx
+ 1, 1, &new, state
);
5364 /* remove reverse mapping */
5366 error
= xfs_rmap_unmap_extent(mp
, dfops
, ip
, whichfork
, del
);
5372 * If we need to, add to list of extents to delete.
5374 if (do_fx
&& !(bflags
& XFS_BMAPI_REMAP
)) {
5375 if (xfs_is_reflink_inode(ip
) && whichfork
== XFS_DATA_FORK
) {
5376 error
= xfs_refcount_decrease_extent(mp
, dfops
, del
);
5380 xfs_bmap_add_free(mp
, dfops
, del
->br_startblock
,
5381 del
->br_blockcount
, NULL
);
5385 * Adjust inode # blocks in the file.
5388 ip
->i_d
.di_nblocks
-= nblks
;
5390 * Adjust quota data.
5392 if (qfield
&& !(bflags
& XFS_BMAPI_REMAP
))
5393 xfs_trans_mod_dquot_byino(tp
, ip
, qfield
, (long)-nblks
);
5396 * Account for change in delayed indirect blocks.
5397 * Nothing to do for disk quota accounting here.
5399 ASSERT(da_old
>= da_new
);
5400 if (da_old
> da_new
)
5401 xfs_mod_fdblocks(mp
, (int64_t)(da_old
- da_new
), false);
5408 * Unmap (remove) blocks from a file.
5409 * If nexts is nonzero then the number of extents to remove is limited to
5410 * that value. If not all extents in the block range can be removed then
5415 xfs_trans_t
*tp
, /* transaction pointer */
5416 struct xfs_inode
*ip
, /* incore inode */
5417 xfs_fileoff_t bno
, /* starting offset to unmap */
5418 xfs_filblks_t
*rlen
, /* i/o: amount remaining */
5419 int flags
, /* misc flags */
5420 xfs_extnum_t nexts
, /* number of extents max */
5421 xfs_fsblock_t
*firstblock
, /* first allocated block
5422 controls a.g. for allocs */
5423 struct xfs_defer_ops
*dfops
) /* i/o: deferred updates */
5425 xfs_btree_cur_t
*cur
; /* bmap btree cursor */
5426 xfs_bmbt_irec_t del
; /* extent being deleted */
5427 int error
; /* error return value */
5428 xfs_extnum_t extno
; /* extent number in list */
5429 xfs_bmbt_irec_t got
; /* current extent record */
5430 xfs_ifork_t
*ifp
; /* inode fork pointer */
5431 int isrt
; /* freeing in rt area */
5432 xfs_extnum_t lastx
; /* last extent index used */
5433 int logflags
; /* transaction logging flags */
5434 xfs_extlen_t mod
; /* rt extent offset */
5435 xfs_mount_t
*mp
; /* mount structure */
5436 xfs_fileoff_t start
; /* first file offset deleted */
5437 int tmp_logflags
; /* partial logging flags */
5438 int wasdel
; /* was a delayed alloc extent */
5439 int whichfork
; /* data or attribute fork */
5441 xfs_filblks_t len
= *rlen
; /* length to unmap in file */
5443 trace_xfs_bunmap(ip
, bno
, len
, flags
, _RET_IP_
);
5445 whichfork
= xfs_bmapi_whichfork(flags
);
5446 ASSERT(whichfork
!= XFS_COW_FORK
);
5447 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5449 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
5450 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)) {
5451 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW
,
5453 return -EFSCORRUPTED
;
5456 if (XFS_FORCED_SHUTDOWN(mp
))
5459 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
5463 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
5464 (error
= xfs_iread_extents(tp
, ip
, whichfork
)))
5466 if (xfs_iext_count(ifp
) == 0) {
5470 XFS_STATS_INC(mp
, xs_blk_unmap
);
5471 isrt
= (whichfork
== XFS_DATA_FORK
) && XFS_IS_REALTIME_INODE(ip
);
5473 bno
= start
+ len
- 1;
5476 * Check to see if the given block number is past the end of the
5477 * file, back up to the last block if so...
5479 if (!xfs_iext_lookup_extent(ip
, ifp
, bno
, &lastx
, &got
)) {
5481 xfs_iext_get_extent(ifp
, --lastx
, &got
);
5482 bno
= got
.br_startoff
+ got
.br_blockcount
- 1;
5486 if (ifp
->if_flags
& XFS_IFBROOT
) {
5487 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
);
5488 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
5489 cur
->bc_private
.b
.firstblock
= *firstblock
;
5490 cur
->bc_private
.b
.dfops
= dfops
;
5491 cur
->bc_private
.b
.flags
= 0;
5497 * Synchronize by locking the bitmap inode.
5499 xfs_ilock(mp
->m_rbmip
, XFS_ILOCK_EXCL
|XFS_ILOCK_RTBITMAP
);
5500 xfs_trans_ijoin(tp
, mp
->m_rbmip
, XFS_ILOCK_EXCL
);
5501 xfs_ilock(mp
->m_rsumip
, XFS_ILOCK_EXCL
|XFS_ILOCK_RTSUM
);
5502 xfs_trans_ijoin(tp
, mp
->m_rsumip
, XFS_ILOCK_EXCL
);
5506 while (bno
!= (xfs_fileoff_t
)-1 && bno
>= start
&& lastx
>= 0 &&
5507 (nexts
== 0 || extno
< nexts
)) {
5509 * Is the found extent after a hole in which bno lives?
5510 * Just back up to the previous extent, if so.
5512 if (got
.br_startoff
> bno
) {
5515 xfs_iext_get_extent(ifp
, lastx
, &got
);
5518 * Is the last block of this extent before the range
5519 * we're supposed to delete? If so, we're done.
5521 bno
= XFS_FILEOFF_MIN(bno
,
5522 got
.br_startoff
+ got
.br_blockcount
- 1);
5526 * Then deal with the (possibly delayed) allocated space
5530 wasdel
= isnullstartblock(del
.br_startblock
);
5531 if (got
.br_startoff
< start
) {
5532 del
.br_startoff
= start
;
5533 del
.br_blockcount
-= start
- got
.br_startoff
;
5535 del
.br_startblock
+= start
- got
.br_startoff
;
5537 if (del
.br_startoff
+ del
.br_blockcount
> bno
+ 1)
5538 del
.br_blockcount
= bno
+ 1 - del
.br_startoff
;
5539 sum
= del
.br_startblock
+ del
.br_blockcount
;
5541 (mod
= do_mod(sum
, mp
->m_sb
.sb_rextsize
))) {
5543 * Realtime extent not lined up at the end.
5544 * The extent could have been split into written
5545 * and unwritten pieces, or we could just be
5546 * unmapping part of it. But we can't really
5547 * get rid of part of a realtime extent.
5549 if (del
.br_state
== XFS_EXT_UNWRITTEN
||
5550 !xfs_sb_version_hasextflgbit(&mp
->m_sb
)) {
5552 * This piece is unwritten, or we're not
5553 * using unwritten extents. Skip over it.
5556 bno
-= mod
> del
.br_blockcount
?
5557 del
.br_blockcount
: mod
;
5558 if (bno
< got
.br_startoff
) {
5560 xfs_bmbt_get_all(xfs_iext_get_ext(
5566 * It's written, turn it unwritten.
5567 * This is better than zeroing it.
5569 ASSERT(del
.br_state
== XFS_EXT_NORM
);
5570 ASSERT(tp
->t_blk_res
> 0);
5572 * If this spans a realtime extent boundary,
5573 * chop it back to the start of the one we end at.
5575 if (del
.br_blockcount
> mod
) {
5576 del
.br_startoff
+= del
.br_blockcount
- mod
;
5577 del
.br_startblock
+= del
.br_blockcount
- mod
;
5578 del
.br_blockcount
= mod
;
5580 del
.br_state
= XFS_EXT_UNWRITTEN
;
5581 error
= xfs_bmap_add_extent_unwritten_real(tp
, ip
,
5582 whichfork
, &lastx
, &cur
, &del
,
5583 firstblock
, dfops
, &logflags
);
5588 if (isrt
&& (mod
= do_mod(del
.br_startblock
, mp
->m_sb
.sb_rextsize
))) {
5590 * Realtime extent is lined up at the end but not
5591 * at the front. We'll get rid of full extents if
5594 mod
= mp
->m_sb
.sb_rextsize
- mod
;
5595 if (del
.br_blockcount
> mod
) {
5596 del
.br_blockcount
-= mod
;
5597 del
.br_startoff
+= mod
;
5598 del
.br_startblock
+= mod
;
5599 } else if ((del
.br_startoff
== start
&&
5600 (del
.br_state
== XFS_EXT_UNWRITTEN
||
5601 tp
->t_blk_res
== 0)) ||
5602 !xfs_sb_version_hasextflgbit(&mp
->m_sb
)) {
5604 * Can't make it unwritten. There isn't
5605 * a full extent here so just skip it.
5607 ASSERT(bno
>= del
.br_blockcount
);
5608 bno
-= del
.br_blockcount
;
5609 if (got
.br_startoff
> bno
&& --lastx
>= 0)
5610 xfs_iext_get_extent(ifp
, lastx
, &got
);
5612 } else if (del
.br_state
== XFS_EXT_UNWRITTEN
) {
5613 struct xfs_bmbt_irec prev
;
5616 * This one is already unwritten.
5617 * It must have a written left neighbor.
5618 * Unwrite the killed part of that one and
5622 xfs_iext_get_extent(ifp
, lastx
- 1, &prev
);
5623 ASSERT(prev
.br_state
== XFS_EXT_NORM
);
5624 ASSERT(!isnullstartblock(prev
.br_startblock
));
5625 ASSERT(del
.br_startblock
==
5626 prev
.br_startblock
+ prev
.br_blockcount
);
5627 if (prev
.br_startoff
< start
) {
5628 mod
= start
- prev
.br_startoff
;
5629 prev
.br_blockcount
-= mod
;
5630 prev
.br_startblock
+= mod
;
5631 prev
.br_startoff
= start
;
5633 prev
.br_state
= XFS_EXT_UNWRITTEN
;
5635 error
= xfs_bmap_add_extent_unwritten_real(tp
,
5636 ip
, whichfork
, &lastx
, &cur
,
5637 &prev
, firstblock
, dfops
,
5643 ASSERT(del
.br_state
== XFS_EXT_NORM
);
5644 del
.br_state
= XFS_EXT_UNWRITTEN
;
5645 error
= xfs_bmap_add_extent_unwritten_real(tp
,
5646 ip
, whichfork
, &lastx
, &cur
,
5647 &del
, firstblock
, dfops
,
5656 * If it's the case where the directory code is running
5657 * with no block reservation, and the deleted block is in
5658 * the middle of its extent, and the resulting insert
5659 * of an extent would cause transformation to btree format,
5660 * then reject it. The calling code will then swap
5661 * blocks around instead.
5662 * We have to do this now, rather than waiting for the
5663 * conversion to btree format, since the transaction
5666 if (!wasdel
&& tp
->t_blk_res
== 0 &&
5667 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
&&
5668 XFS_IFORK_NEXTENTS(ip
, whichfork
) >= /* Note the >= */
5669 XFS_IFORK_MAXEXT(ip
, whichfork
) &&
5670 del
.br_startoff
> got
.br_startoff
&&
5671 del
.br_startoff
+ del
.br_blockcount
<
5672 got
.br_startoff
+ got
.br_blockcount
) {
5678 * Unreserve quota and update realtime free space, if
5679 * appropriate. If delayed allocation, update the inode delalloc
5680 * counter now and wait to update the sb counters as
5681 * xfs_bmap_del_extent() might need to borrow some blocks.
5684 ASSERT(startblockval(del
.br_startblock
) > 0);
5686 xfs_filblks_t rtexts
;
5688 rtexts
= XFS_FSB_TO_B(mp
, del
.br_blockcount
);
5689 do_div(rtexts
, mp
->m_sb
.sb_rextsize
);
5690 xfs_mod_frextents(mp
, (int64_t)rtexts
);
5691 (void)xfs_trans_reserve_quota_nblks(NULL
,
5692 ip
, -((long)del
.br_blockcount
), 0,
5693 XFS_QMOPT_RES_RTBLKS
);
5695 (void)xfs_trans_reserve_quota_nblks(NULL
,
5696 ip
, -((long)del
.br_blockcount
), 0,
5697 XFS_QMOPT_RES_REGBLKS
);
5699 ip
->i_delayed_blks
-= del
.br_blockcount
;
5701 cur
->bc_private
.b
.flags
|=
5702 XFS_BTCUR_BPRV_WASDEL
;
5704 cur
->bc_private
.b
.flags
&= ~XFS_BTCUR_BPRV_WASDEL
;
5706 error
= xfs_bmap_del_extent(ip
, tp
, &lastx
, dfops
, cur
, &del
,
5707 &tmp_logflags
, whichfork
, flags
);
5708 logflags
|= tmp_logflags
;
5712 if (!isrt
&& wasdel
)
5713 xfs_mod_fdblocks(mp
, (int64_t)del
.br_blockcount
, false);
5715 bno
= del
.br_startoff
- 1;
5718 * If not done go on to the next (previous) record.
5720 if (bno
!= (xfs_fileoff_t
)-1 && bno
>= start
) {
5722 xfs_iext_get_extent(ifp
, lastx
, &got
);
5723 if (got
.br_startoff
> bno
&& --lastx
>= 0)
5724 xfs_iext_get_extent(ifp
, lastx
, &got
);
5729 if (bno
== (xfs_fileoff_t
)-1 || bno
< start
|| lastx
< 0)
5732 *rlen
= bno
- start
+ 1;
5735 * Convert to a btree if necessary.
5737 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
5738 ASSERT(cur
== NULL
);
5739 error
= xfs_bmap_extents_to_btree(tp
, ip
, firstblock
, dfops
,
5740 &cur
, 0, &tmp_logflags
, whichfork
);
5741 logflags
|= tmp_logflags
;
5746 * transform from btree to extents, give it cur
5748 else if (xfs_bmap_wants_extents(ip
, whichfork
)) {
5749 ASSERT(cur
!= NULL
);
5750 error
= xfs_bmap_btree_to_extents(tp
, ip
, cur
, &tmp_logflags
,
5752 logflags
|= tmp_logflags
;
5757 * transform from extents to local?
5762 * Log everything. Do this after conversion, there's no point in
5763 * logging the extent records if we've converted to btree format.
5765 if ((logflags
& xfs_ilog_fext(whichfork
)) &&
5766 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
5767 logflags
&= ~xfs_ilog_fext(whichfork
);
5768 else if ((logflags
& xfs_ilog_fbroot(whichfork
)) &&
5769 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)
5770 logflags
&= ~xfs_ilog_fbroot(whichfork
);
5772 * Log inode even in the error case, if the transaction
5773 * is dirty we'll need to shut down the filesystem.
5776 xfs_trans_log_inode(tp
, ip
, logflags
);
5779 *firstblock
= cur
->bc_private
.b
.firstblock
;
5780 cur
->bc_private
.b
.allocated
= 0;
5782 xfs_btree_del_cursor(cur
,
5783 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
5788 /* Unmap a range of a file. */
5792 struct xfs_inode
*ip
,
5797 xfs_fsblock_t
*firstblock
,
5798 struct xfs_defer_ops
*dfops
,
5803 error
= __xfs_bunmapi(tp
, ip
, bno
, &len
, flags
, nexts
, firstblock
,
5810 * Determine whether an extent shift can be accomplished by a merge with the
5811 * extent that precedes the target hole of the shift.
5815 struct xfs_bmbt_irec
*left
, /* preceding extent */
5816 struct xfs_bmbt_irec
*got
, /* current extent to shift */
5817 xfs_fileoff_t shift
) /* shift fsb */
5819 xfs_fileoff_t startoff
;
5821 startoff
= got
->br_startoff
- shift
;
5824 * The extent, once shifted, must be adjacent in-file and on-disk with
5825 * the preceding extent.
5827 if ((left
->br_startoff
+ left
->br_blockcount
!= startoff
) ||
5828 (left
->br_startblock
+ left
->br_blockcount
!= got
->br_startblock
) ||
5829 (left
->br_state
!= got
->br_state
) ||
5830 (left
->br_blockcount
+ got
->br_blockcount
> MAXEXTLEN
))
5837 * A bmap extent shift adjusts the file offset of an extent to fill a preceding
5838 * hole in the file. If an extent shift would result in the extent being fully
5839 * adjacent to the extent that currently precedes the hole, we can merge with
5840 * the preceding extent rather than do the shift.
5842 * This function assumes the caller has verified a shift-by-merge is possible
5843 * with the provided extents via xfs_bmse_can_merge().
5847 struct xfs_inode
*ip
,
5849 xfs_fileoff_t shift
, /* shift fsb */
5850 int current_ext
, /* idx of gotp */
5851 struct xfs_bmbt_rec_host
*gotp
, /* extent to shift */
5852 struct xfs_bmbt_rec_host
*leftp
, /* preceding extent */
5853 struct xfs_btree_cur
*cur
,
5854 int *logflags
) /* output */
5856 struct xfs_bmbt_irec got
;
5857 struct xfs_bmbt_irec left
;
5858 xfs_filblks_t blockcount
;
5860 struct xfs_mount
*mp
= ip
->i_mount
;
5862 xfs_bmbt_get_all(gotp
, &got
);
5863 xfs_bmbt_get_all(leftp
, &left
);
5864 blockcount
= left
.br_blockcount
+ got
.br_blockcount
;
5866 ASSERT(xfs_isilocked(ip
, XFS_IOLOCK_EXCL
));
5867 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
5868 ASSERT(xfs_bmse_can_merge(&left
, &got
, shift
));
5871 * Merge the in-core extents. Note that the host record pointers and
5872 * current_ext index are invalid once the extent has been removed via
5873 * xfs_iext_remove().
5875 xfs_bmbt_set_blockcount(leftp
, blockcount
);
5876 xfs_iext_remove(ip
, current_ext
, 1, 0);
5879 * Update the on-disk extent count, the btree if necessary and log the
5882 XFS_IFORK_NEXT_SET(ip
, whichfork
,
5883 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
5884 *logflags
|= XFS_ILOG_CORE
;
5886 *logflags
|= XFS_ILOG_DEXT
;
5890 /* lookup and remove the extent to merge */
5891 error
= xfs_bmbt_lookup_eq(cur
, got
.br_startoff
, got
.br_startblock
,
5892 got
.br_blockcount
, &i
);
5895 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
5897 error
= xfs_btree_delete(cur
, &i
);
5900 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
5902 /* lookup and update size of the previous extent */
5903 error
= xfs_bmbt_lookup_eq(cur
, left
.br_startoff
, left
.br_startblock
,
5904 left
.br_blockcount
, &i
);
5907 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
5909 left
.br_blockcount
= blockcount
;
5911 return xfs_bmbt_update(cur
, left
.br_startoff
, left
.br_startblock
,
5912 left
.br_blockcount
, left
.br_state
);
5916 * Shift a single extent.
5920 struct xfs_inode
*ip
,
5922 xfs_fileoff_t offset_shift_fsb
,
5924 struct xfs_bmbt_rec_host
*gotp
,
5925 struct xfs_btree_cur
*cur
,
5927 enum shift_direction direction
,
5928 struct xfs_defer_ops
*dfops
)
5930 struct xfs_ifork
*ifp
;
5931 struct xfs_mount
*mp
;
5932 xfs_fileoff_t startoff
;
5933 struct xfs_bmbt_rec_host
*adj_irecp
;
5934 struct xfs_bmbt_irec got
;
5935 struct xfs_bmbt_irec adj_irec
;
5941 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5942 total_extents
= xfs_iext_count(ifp
);
5944 xfs_bmbt_get_all(gotp
, &got
);
5946 /* delalloc extents should be prevented by caller */
5947 XFS_WANT_CORRUPTED_RETURN(mp
, !isnullstartblock(got
.br_startblock
));
5949 if (direction
== SHIFT_LEFT
) {
5950 startoff
= got
.br_startoff
- offset_shift_fsb
;
5953 * Check for merge if we've got an extent to the left,
5954 * otherwise make sure there's enough room at the start
5955 * of the file for the shift.
5957 if (!*current_ext
) {
5958 if (got
.br_startoff
< offset_shift_fsb
)
5960 goto update_current_ext
;
5963 * grab the left extent and check for a large
5966 adj_irecp
= xfs_iext_get_ext(ifp
, *current_ext
- 1);
5967 xfs_bmbt_get_all(adj_irecp
, &adj_irec
);
5970 adj_irec
.br_startoff
+ adj_irec
.br_blockcount
)
5973 /* check whether to merge the extent or shift it down */
5974 if (xfs_bmse_can_merge(&adj_irec
, &got
,
5975 offset_shift_fsb
)) {
5976 error
= xfs_bmse_merge(ip
, whichfork
, offset_shift_fsb
,
5977 *current_ext
, gotp
, adj_irecp
,
5985 startoff
= got
.br_startoff
+ offset_shift_fsb
;
5986 /* nothing to move if this is the last extent */
5987 if (*current_ext
>= (total_extents
- 1))
5988 goto update_current_ext
;
5990 * If this is not the last extent in the file, make sure there
5991 * is enough room between current extent and next extent for
5992 * accommodating the shift.
5994 adj_irecp
= xfs_iext_get_ext(ifp
, *current_ext
+ 1);
5995 xfs_bmbt_get_all(adj_irecp
, &adj_irec
);
5996 if (startoff
+ got
.br_blockcount
> adj_irec
.br_startoff
)
5999 * Unlike a left shift (which involves a hole punch),
6000 * a right shift does not modify extent neighbors
6001 * in any way. We should never find mergeable extents
6002 * in this scenario. Check anyways and warn if we
6003 * encounter two extents that could be one.
6005 if (xfs_bmse_can_merge(&got
, &adj_irec
, offset_shift_fsb
))
6009 * Increment the extent index for the next iteration, update the start
6010 * offset of the in-core extent and update the btree if applicable.
6013 if (direction
== SHIFT_LEFT
)
6017 xfs_bmbt_set_startoff(gotp
, startoff
);
6018 *logflags
|= XFS_ILOG_CORE
;
6021 *logflags
|= XFS_ILOG_DEXT
;
6025 error
= xfs_bmbt_lookup_eq(cur
, got
.br_startoff
, got
.br_startblock
,
6026 got
.br_blockcount
, &i
);
6029 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
6031 got
.br_startoff
= startoff
;
6032 error
= xfs_bmbt_update(cur
, got
.br_startoff
, got
.br_startblock
,
6033 got
.br_blockcount
, got
.br_state
);
6038 /* update reverse mapping */
6039 error
= xfs_rmap_unmap_extent(mp
, dfops
, ip
, whichfork
, &adj_irec
);
6042 adj_irec
.br_startoff
= startoff
;
6043 return xfs_rmap_map_extent(mp
, dfops
, ip
, whichfork
, &adj_irec
);
6047 * Shift extent records to the left/right to cover/create a hole.
6049 * The maximum number of extents to be shifted in a single operation is
6050 * @num_exts. @stop_fsb specifies the file offset at which to stop shift and the
6051 * file offset where we've left off is returned in @next_fsb. @offset_shift_fsb
6052 * is the length by which each extent is shifted. If there is no hole to shift
6053 * the extents into, this will be considered invalid operation and we abort
6057 xfs_bmap_shift_extents(
6058 struct xfs_trans
*tp
,
6059 struct xfs_inode
*ip
,
6060 xfs_fileoff_t
*next_fsb
,
6061 xfs_fileoff_t offset_shift_fsb
,
6063 xfs_fileoff_t stop_fsb
,
6064 xfs_fsblock_t
*firstblock
,
6065 struct xfs_defer_ops
*dfops
,
6066 enum shift_direction direction
,
6069 struct xfs_btree_cur
*cur
= NULL
;
6070 struct xfs_bmbt_rec_host
*gotp
;
6071 struct xfs_bmbt_irec got
;
6072 struct xfs_mount
*mp
= ip
->i_mount
;
6073 struct xfs_ifork
*ifp
;
6074 xfs_extnum_t nexts
= 0;
6075 xfs_extnum_t current_ext
;
6076 xfs_extnum_t total_extents
;
6077 xfs_extnum_t stop_extent
;
6079 int whichfork
= XFS_DATA_FORK
;
6082 if (unlikely(XFS_TEST_ERROR(
6083 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
6084 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
6085 mp
, XFS_ERRTAG_BMAPIFORMAT
, XFS_RANDOM_BMAPIFORMAT
))) {
6086 XFS_ERROR_REPORT("xfs_bmap_shift_extents",
6087 XFS_ERRLEVEL_LOW
, mp
);
6088 return -EFSCORRUPTED
;
6091 if (XFS_FORCED_SHUTDOWN(mp
))
6094 ASSERT(xfs_isilocked(ip
, XFS_IOLOCK_EXCL
));
6095 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
6096 ASSERT(direction
== SHIFT_LEFT
|| direction
== SHIFT_RIGHT
);
6097 ASSERT(*next_fsb
!= NULLFSBLOCK
|| direction
== SHIFT_RIGHT
);
6099 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
6100 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
6101 /* Read in all the extents */
6102 error
= xfs_iread_extents(tp
, ip
, whichfork
);
6107 if (ifp
->if_flags
& XFS_IFBROOT
) {
6108 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
6109 cur
->bc_private
.b
.firstblock
= *firstblock
;
6110 cur
->bc_private
.b
.dfops
= dfops
;
6111 cur
->bc_private
.b
.flags
= 0;
6115 * There may be delalloc extents in the data fork before the range we
6116 * are collapsing out, so we cannot use the count of real extents here.
6117 * Instead we have to calculate it from the incore fork.
6119 total_extents
= xfs_iext_count(ifp
);
6120 if (total_extents
== 0) {
6126 * In case of first right shift, we need to initialize next_fsb
6128 if (*next_fsb
== NULLFSBLOCK
) {
6129 gotp
= xfs_iext_get_ext(ifp
, total_extents
- 1);
6130 xfs_bmbt_get_all(gotp
, &got
);
6131 *next_fsb
= got
.br_startoff
;
6132 if (stop_fsb
> *next_fsb
) {
6138 /* Lookup the extent index at which we have to stop */
6139 if (direction
== SHIFT_RIGHT
) {
6140 gotp
= xfs_iext_bno_to_ext(ifp
, stop_fsb
, &stop_extent
);
6141 /* Make stop_extent exclusive of shift range */
6144 stop_extent
= total_extents
;
6147 * Look up the extent index for the fsb where we start shifting. We can
6148 * henceforth iterate with current_ext as extent list changes are locked
6151 * gotp can be null in 2 cases: 1) if there are no extents or 2)
6152 * *next_fsb lies in a hole beyond which there are no extents. Either
6155 gotp
= xfs_iext_bno_to_ext(ifp
, *next_fsb
, ¤t_ext
);
6161 /* some sanity checking before we finally start shifting extents */
6162 if ((direction
== SHIFT_LEFT
&& current_ext
>= stop_extent
) ||
6163 (direction
== SHIFT_RIGHT
&& current_ext
<= stop_extent
)) {
6168 while (nexts
++ < num_exts
) {
6169 error
= xfs_bmse_shift_one(ip
, whichfork
, offset_shift_fsb
,
6170 ¤t_ext
, gotp
, cur
, &logflags
,
6175 * If there was an extent merge during the shift, the extent
6176 * count can change. Update the total and grade the next record.
6178 if (direction
== SHIFT_LEFT
) {
6179 total_extents
= xfs_iext_count(ifp
);
6180 stop_extent
= total_extents
;
6183 if (current_ext
== stop_extent
) {
6185 *next_fsb
= NULLFSBLOCK
;
6188 gotp
= xfs_iext_get_ext(ifp
, current_ext
);
6192 xfs_bmbt_get_all(gotp
, &got
);
6193 *next_fsb
= got
.br_startoff
;
6198 xfs_btree_del_cursor(cur
,
6199 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
6202 xfs_trans_log_inode(tp
, ip
, logflags
);
6208 * Splits an extent into two extents at split_fsb block such that it is
6209 * the first block of the current_ext. @current_ext is a target extent
6210 * to be split. @split_fsb is a block where the extents is split.
6211 * If split_fsb lies in a hole or the first block of extents, just return 0.
6214 xfs_bmap_split_extent_at(
6215 struct xfs_trans
*tp
,
6216 struct xfs_inode
*ip
,
6217 xfs_fileoff_t split_fsb
,
6218 xfs_fsblock_t
*firstfsb
,
6219 struct xfs_defer_ops
*dfops
)
6221 int whichfork
= XFS_DATA_FORK
;
6222 struct xfs_btree_cur
*cur
= NULL
;
6223 struct xfs_bmbt_rec_host
*gotp
;
6224 struct xfs_bmbt_irec got
;
6225 struct xfs_bmbt_irec
new; /* split extent */
6226 struct xfs_mount
*mp
= ip
->i_mount
;
6227 struct xfs_ifork
*ifp
;
6228 xfs_fsblock_t gotblkcnt
; /* new block count for got */
6229 xfs_extnum_t current_ext
;
6234 if (unlikely(XFS_TEST_ERROR(
6235 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
6236 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
6237 mp
, XFS_ERRTAG_BMAPIFORMAT
, XFS_RANDOM_BMAPIFORMAT
))) {
6238 XFS_ERROR_REPORT("xfs_bmap_split_extent_at",
6239 XFS_ERRLEVEL_LOW
, mp
);
6240 return -EFSCORRUPTED
;
6243 if (XFS_FORCED_SHUTDOWN(mp
))
6246 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
6247 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
6248 /* Read in all the extents */
6249 error
= xfs_iread_extents(tp
, ip
, whichfork
);
6255 * gotp can be null in 2 cases: 1) if there are no extents
6256 * or 2) split_fsb lies in a hole beyond which there are
6257 * no extents. Either way, we are done.
6259 gotp
= xfs_iext_bno_to_ext(ifp
, split_fsb
, ¤t_ext
);
6263 xfs_bmbt_get_all(gotp
, &got
);
6266 * Check split_fsb lies in a hole or the start boundary offset
6269 if (got
.br_startoff
>= split_fsb
)
6272 gotblkcnt
= split_fsb
- got
.br_startoff
;
6273 new.br_startoff
= split_fsb
;
6274 new.br_startblock
= got
.br_startblock
+ gotblkcnt
;
6275 new.br_blockcount
= got
.br_blockcount
- gotblkcnt
;
6276 new.br_state
= got
.br_state
;
6278 if (ifp
->if_flags
& XFS_IFBROOT
) {
6279 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
6280 cur
->bc_private
.b
.firstblock
= *firstfsb
;
6281 cur
->bc_private
.b
.dfops
= dfops
;
6282 cur
->bc_private
.b
.flags
= 0;
6283 error
= xfs_bmbt_lookup_eq(cur
, got
.br_startoff
,
6289 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, del_cursor
);
6292 xfs_bmbt_set_blockcount(gotp
, gotblkcnt
);
6293 got
.br_blockcount
= gotblkcnt
;
6295 logflags
= XFS_ILOG_CORE
;
6297 error
= xfs_bmbt_update(cur
, got
.br_startoff
,
6304 logflags
|= XFS_ILOG_DEXT
;
6306 /* Add new extent */
6308 xfs_iext_insert(ip
, current_ext
, 1, &new, 0);
6309 XFS_IFORK_NEXT_SET(ip
, whichfork
,
6310 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
6313 error
= xfs_bmbt_lookup_eq(cur
, new.br_startoff
,
6314 new.br_startblock
, new.br_blockcount
,
6318 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, del_cursor
);
6319 cur
->bc_rec
.b
.br_state
= new.br_state
;
6321 error
= xfs_btree_insert(cur
, &i
);
6324 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, del_cursor
);
6328 * Convert to a btree if necessary.
6330 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
6331 int tmp_logflags
; /* partial log flag return val */
6333 ASSERT(cur
== NULL
);
6334 error
= xfs_bmap_extents_to_btree(tp
, ip
, firstfsb
, dfops
,
6335 &cur
, 0, &tmp_logflags
, whichfork
);
6336 logflags
|= tmp_logflags
;
6341 cur
->bc_private
.b
.allocated
= 0;
6342 xfs_btree_del_cursor(cur
,
6343 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
6347 xfs_trans_log_inode(tp
, ip
, logflags
);
6352 xfs_bmap_split_extent(
6353 struct xfs_inode
*ip
,
6354 xfs_fileoff_t split_fsb
)
6356 struct xfs_mount
*mp
= ip
->i_mount
;
6357 struct xfs_trans
*tp
;
6358 struct xfs_defer_ops dfops
;
6359 xfs_fsblock_t firstfsb
;
6362 error
= xfs_trans_alloc(mp
, &M_RES(mp
)->tr_write
,
6363 XFS_DIOSTRAT_SPACE_RES(mp
, 0), 0, 0, &tp
);
6367 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
6368 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
);
6370 xfs_defer_init(&dfops
, &firstfsb
);
6372 error
= xfs_bmap_split_extent_at(tp
, ip
, split_fsb
,
6377 error
= xfs_defer_finish(&tp
, &dfops
, NULL
);
6381 return xfs_trans_commit(tp
);
6384 xfs_defer_cancel(&dfops
);
6385 xfs_trans_cancel(tp
);
6389 /* Deferred mapping is only for real extents in the data fork. */
6391 xfs_bmap_is_update_needed(
6392 struct xfs_bmbt_irec
*bmap
)
6394 return bmap
->br_startblock
!= HOLESTARTBLOCK
&&
6395 bmap
->br_startblock
!= DELAYSTARTBLOCK
;
6398 /* Record a bmap intent. */
6401 struct xfs_mount
*mp
,
6402 struct xfs_defer_ops
*dfops
,
6403 enum xfs_bmap_intent_type type
,
6404 struct xfs_inode
*ip
,
6406 struct xfs_bmbt_irec
*bmap
)
6409 struct xfs_bmap_intent
*bi
;
6411 trace_xfs_bmap_defer(mp
,
6412 XFS_FSB_TO_AGNO(mp
, bmap
->br_startblock
),
6414 XFS_FSB_TO_AGBNO(mp
, bmap
->br_startblock
),
6415 ip
->i_ino
, whichfork
,
6417 bmap
->br_blockcount
,
6420 bi
= kmem_alloc(sizeof(struct xfs_bmap_intent
), KM_SLEEP
| KM_NOFS
);
6421 INIT_LIST_HEAD(&bi
->bi_list
);
6424 bi
->bi_whichfork
= whichfork
;
6425 bi
->bi_bmap
= *bmap
;
6427 error
= xfs_defer_join(dfops
, bi
->bi_owner
);
6433 xfs_defer_add(dfops
, XFS_DEFER_OPS_TYPE_BMAP
, &bi
->bi_list
);
6437 /* Map an extent into a file. */
6439 xfs_bmap_map_extent(
6440 struct xfs_mount
*mp
,
6441 struct xfs_defer_ops
*dfops
,
6442 struct xfs_inode
*ip
,
6443 struct xfs_bmbt_irec
*PREV
)
6445 if (!xfs_bmap_is_update_needed(PREV
))
6448 return __xfs_bmap_add(mp
, dfops
, XFS_BMAP_MAP
, ip
,
6449 XFS_DATA_FORK
, PREV
);
6452 /* Unmap an extent out of a file. */
6454 xfs_bmap_unmap_extent(
6455 struct xfs_mount
*mp
,
6456 struct xfs_defer_ops
*dfops
,
6457 struct xfs_inode
*ip
,
6458 struct xfs_bmbt_irec
*PREV
)
6460 if (!xfs_bmap_is_update_needed(PREV
))
6463 return __xfs_bmap_add(mp
, dfops
, XFS_BMAP_UNMAP
, ip
,
6464 XFS_DATA_FORK
, PREV
);
6468 * Process one of the deferred bmap operations. We pass back the
6469 * btree cursor to maintain our lock on the bmapbt between calls.
6472 xfs_bmap_finish_one(
6473 struct xfs_trans
*tp
,
6474 struct xfs_defer_ops
*dfops
,
6475 struct xfs_inode
*ip
,
6476 enum xfs_bmap_intent_type type
,
6478 xfs_fileoff_t startoff
,
6479 xfs_fsblock_t startblock
,
6480 xfs_filblks_t blockcount
,
6483 struct xfs_bmbt_irec bmap
;
6485 xfs_fsblock_t firstfsb
;
6486 int flags
= XFS_BMAPI_REMAP
;
6490 bmap
.br_startblock
= startblock
;
6491 bmap
.br_startoff
= startoff
;
6492 bmap
.br_blockcount
= blockcount
;
6493 bmap
.br_state
= state
;
6495 trace_xfs_bmap_deferred(tp
->t_mountp
,
6496 XFS_FSB_TO_AGNO(tp
->t_mountp
, startblock
), type
,
6497 XFS_FSB_TO_AGBNO(tp
->t_mountp
, startblock
),
6498 ip
->i_ino
, whichfork
, startoff
, blockcount
, state
);
6500 if (whichfork
!= XFS_DATA_FORK
&& whichfork
!= XFS_ATTR_FORK
)
6501 return -EFSCORRUPTED
;
6502 if (whichfork
== XFS_ATTR_FORK
)
6503 flags
|= XFS_BMAPI_ATTRFORK
;
6505 if (XFS_TEST_ERROR(false, tp
->t_mountp
,
6506 XFS_ERRTAG_BMAP_FINISH_ONE
,
6507 XFS_RANDOM_BMAP_FINISH_ONE
))
6512 firstfsb
= bmap
.br_startblock
;
6513 error
= xfs_bmapi_write(tp
, ip
, bmap
.br_startoff
,
6514 bmap
.br_blockcount
, flags
, &firstfsb
,
6515 bmap
.br_blockcount
, &bmap
, &nimaps
,
6518 case XFS_BMAP_UNMAP
:
6519 error
= xfs_bunmapi(tp
, ip
, bmap
.br_startoff
,
6520 bmap
.br_blockcount
, flags
, 1, &firstfsb
,
6526 error
= -EFSCORRUPTED
;