2 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 #include "libxfs_priv.h"
20 #include "xfs_shared.h"
21 #include "xfs_format.h"
22 #include "xfs_log_format.h"
23 #include "xfs_trans_resv.h"
26 #include "xfs_mount.h"
27 #include "xfs_defer.h"
28 #include "xfs_da_format.h"
29 #include "xfs_da_btree.h"
31 #include "xfs_inode.h"
32 #include "xfs_btree.h"
33 #include "xfs_trans.h"
34 #include "xfs_alloc.h"
36 #include "xfs_bmap_btree.h"
37 #include "xfs_trans_space.h"
38 #include "xfs_trace.h"
39 #include "xfs_attr_leaf.h"
40 #include "xfs_quota_defs.h"
42 #include "xfs_ag_resv.h"
43 #include "xfs_refcount.h"
44 #include "xfs_rmap_btree.h"
47 kmem_zone_t
*xfs_bmap_free_item_zone
;
50 * Miscellaneous helper functions
54 * Compute and fill in the value of the maximum depth of a bmap btree
55 * in this filesystem. Done once, during mount.
58 xfs_bmap_compute_maxlevels(
59 xfs_mount_t
*mp
, /* file system mount structure */
60 int whichfork
) /* data or attr fork */
62 int level
; /* btree level */
63 uint maxblocks
; /* max blocks at this level */
64 uint maxleafents
; /* max leaf entries possible */
65 int maxrootrecs
; /* max records in root block */
66 int minleafrecs
; /* min records in leaf block */
67 int minnoderecs
; /* min records in node block */
68 int sz
; /* root block size */
71 * The maximum number of extents in a file, hence the maximum
72 * number of leaf entries, is controlled by the type of di_nextents
73 * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
74 * (a signed 16-bit number, xfs_aextnum_t).
76 * Note that we can no longer assume that if we are in ATTR1 that
77 * the fork offset of all the inodes will be
78 * (xfs_default_attroffset(ip) >> 3) because we could have mounted
79 * with ATTR2 and then mounted back with ATTR1, keeping the
80 * di_forkoff's fixed but probably at various positions. Therefore,
81 * for both ATTR1 and ATTR2 we have to assume the worst case scenario
82 * of a minimum size available.
84 if (whichfork
== XFS_DATA_FORK
) {
85 maxleafents
= MAXEXTNUM
;
86 sz
= XFS_BMDR_SPACE_CALC(MINDBTPTRS
);
88 maxleafents
= MAXAEXTNUM
;
89 sz
= XFS_BMDR_SPACE_CALC(MINABTPTRS
);
91 maxrootrecs
= xfs_bmdr_maxrecs(sz
, 0);
92 minleafrecs
= mp
->m_bmap_dmnr
[0];
93 minnoderecs
= mp
->m_bmap_dmnr
[1];
94 maxblocks
= (maxleafents
+ minleafrecs
- 1) / minleafrecs
;
95 for (level
= 1; maxblocks
> 1; level
++) {
96 if (maxblocks
<= maxrootrecs
)
99 maxblocks
= (maxblocks
+ minnoderecs
- 1) / minnoderecs
;
101 mp
->m_bm_maxlevels
[whichfork
] = level
;
104 STATIC
int /* error */
106 struct xfs_btree_cur
*cur
,
110 int *stat
) /* success/failure */
112 cur
->bc_rec
.b
.br_startoff
= off
;
113 cur
->bc_rec
.b
.br_startblock
= bno
;
114 cur
->bc_rec
.b
.br_blockcount
= len
;
115 return xfs_btree_lookup(cur
, XFS_LOOKUP_EQ
, stat
);
118 STATIC
int /* error */
120 struct xfs_btree_cur
*cur
,
124 int *stat
) /* success/failure */
126 cur
->bc_rec
.b
.br_startoff
= off
;
127 cur
->bc_rec
.b
.br_startblock
= bno
;
128 cur
->bc_rec
.b
.br_blockcount
= len
;
129 return xfs_btree_lookup(cur
, XFS_LOOKUP_GE
, stat
);
133 * Check if the inode needs to be converted to btree format.
135 static inline bool xfs_bmap_needs_btree(struct xfs_inode
*ip
, int whichfork
)
137 return whichfork
!= XFS_COW_FORK
&&
138 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
&&
139 XFS_IFORK_NEXTENTS(ip
, whichfork
) >
140 XFS_IFORK_MAXEXT(ip
, whichfork
);
144 * Check if the inode should be converted to extent format.
146 static inline bool xfs_bmap_wants_extents(struct xfs_inode
*ip
, int whichfork
)
148 return whichfork
!= XFS_COW_FORK
&&
149 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
&&
150 XFS_IFORK_NEXTENTS(ip
, whichfork
) <=
151 XFS_IFORK_MAXEXT(ip
, whichfork
);
155 * Update the record referred to by cur to the value given
156 * by [off, bno, len, state].
157 * This either works (return 0) or gets an EFSCORRUPTED error.
161 struct xfs_btree_cur
*cur
,
167 union xfs_btree_rec rec
;
169 xfs_bmbt_disk_set_allf(&rec
.bmbt
, off
, bno
, len
, state
);
170 return xfs_btree_update(cur
, &rec
);
174 * Compute the worst-case number of indirect blocks that will be used
175 * for ip's delayed extent of length "len".
178 xfs_bmap_worst_indlen(
179 xfs_inode_t
*ip
, /* incore inode pointer */
180 xfs_filblks_t len
) /* delayed extent length */
182 int level
; /* btree level number */
183 int maxrecs
; /* maximum record count at this level */
184 xfs_mount_t
*mp
; /* mount structure */
185 xfs_filblks_t rval
; /* return value */
186 xfs_filblks_t orig_len
;
190 /* Calculate the worst-case size of the bmbt. */
192 maxrecs
= mp
->m_bmap_dmxr
[0];
193 for (level
= 0, rval
= 0;
194 level
< XFS_BM_MAXLEVELS(mp
, XFS_DATA_FORK
);
197 do_div(len
, maxrecs
);
200 rval
+= XFS_BM_MAXLEVELS(mp
, XFS_DATA_FORK
) -
205 maxrecs
= mp
->m_bmap_dmxr
[1];
208 /* Calculate the worst-case size of the rmapbt. */
209 if (xfs_sb_version_hasrmapbt(&mp
->m_sb
))
210 rval
+= 1 + xfs_rmapbt_calc_size(mp
, orig_len
) +
211 mp
->m_rmap_maxlevels
;
217 * Calculate the default attribute fork offset for newly created inodes.
220 xfs_default_attroffset(
221 struct xfs_inode
*ip
)
223 struct xfs_mount
*mp
= ip
->i_mount
;
226 if (mp
->m_sb
.sb_inodesize
== 256) {
227 offset
= XFS_LITINO(mp
, ip
->i_d
.di_version
) -
228 XFS_BMDR_SPACE_CALC(MINABTPTRS
);
230 offset
= XFS_BMDR_SPACE_CALC(6 * MINABTPTRS
);
233 ASSERT(offset
< XFS_LITINO(mp
, ip
->i_d
.di_version
));
238 * Helper routine to reset inode di_forkoff field when switching
239 * attribute fork from local to extent format - we reset it where
240 * possible to make space available for inline data fork extents.
243 xfs_bmap_forkoff_reset(
247 if (whichfork
== XFS_ATTR_FORK
&&
248 ip
->i_d
.di_format
!= XFS_DINODE_FMT_DEV
&&
249 ip
->i_d
.di_format
!= XFS_DINODE_FMT_UUID
&&
250 ip
->i_d
.di_format
!= XFS_DINODE_FMT_BTREE
) {
251 uint dfl_forkoff
= xfs_default_attroffset(ip
) >> 3;
253 if (dfl_forkoff
> ip
->i_d
.di_forkoff
)
254 ip
->i_d
.di_forkoff
= dfl_forkoff
;
259 STATIC
struct xfs_buf
*
261 struct xfs_btree_cur
*cur
,
264 struct xfs_log_item_desc
*lidp
;
270 for (i
= 0; i
< XFS_BTREE_MAXLEVELS
; i
++) {
271 if (!cur
->bc_bufs
[i
])
273 if (XFS_BUF_ADDR(cur
->bc_bufs
[i
]) == bno
)
274 return cur
->bc_bufs
[i
];
277 /* Chase down all the log items to see if the bp is there */
278 list_for_each_entry(lidp
, &cur
->bc_tp
->t_items
, lid_trans
) {
279 struct xfs_buf_log_item
*bip
;
280 bip
= (struct xfs_buf_log_item
*)lidp
->lid_item
;
281 if (bip
->bli_item
.li_type
== XFS_LI_BUF
&&
282 XFS_BUF_ADDR(bip
->bli_buf
) == bno
)
291 struct xfs_btree_block
*block
,
297 __be64
*pp
, *thispa
; /* pointer to block address */
298 xfs_bmbt_key_t
*prevp
, *keyp
;
300 ASSERT(be16_to_cpu(block
->bb_level
) > 0);
303 for( i
= 1; i
<= xfs_btree_get_numrecs(block
); i
++) {
304 dmxr
= mp
->m_bmap_dmxr
[0];
305 keyp
= XFS_BMBT_KEY_ADDR(mp
, block
, i
);
308 ASSERT(be64_to_cpu(prevp
->br_startoff
) <
309 be64_to_cpu(keyp
->br_startoff
));
314 * Compare the block numbers to see if there are dups.
317 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, i
, sz
);
319 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, i
, dmxr
);
321 for (j
= i
+1; j
<= be16_to_cpu(block
->bb_numrecs
); j
++) {
323 thispa
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, j
, sz
);
325 thispa
= XFS_BMBT_PTR_ADDR(mp
, block
, j
, dmxr
);
326 if (*thispa
== *pp
) {
327 xfs_warn(mp
, "%s: thispa(%d) == pp(%d) %Ld",
329 (unsigned long long)be64_to_cpu(*thispa
));
330 panic("%s: ptrs are equal in node\n",
338 * Check that the extents for the inode ip are in the right order in all
339 * btree leaves. THis becomes prohibitively expensive for large extent count
340 * files, so don't bother with inodes that have more than 10,000 extents in
341 * them. The btree record ordering checks will still be done, so for such large
342 * bmapbt constructs that is going to catch most corruptions.
345 xfs_bmap_check_leaf_extents(
346 xfs_btree_cur_t
*cur
, /* btree cursor or null */
347 xfs_inode_t
*ip
, /* incore inode pointer */
348 int whichfork
) /* data or attr fork */
350 struct xfs_btree_block
*block
; /* current btree block */
351 xfs_fsblock_t bno
; /* block # of "block" */
352 xfs_buf_t
*bp
; /* buffer for "block" */
353 int error
; /* error return value */
354 xfs_extnum_t i
=0, j
; /* index into the extents list */
355 xfs_ifork_t
*ifp
; /* fork structure */
356 int level
; /* btree level, for checking */
357 xfs_mount_t
*mp
; /* file system mount structure */
358 __be64
*pp
; /* pointer to block address */
359 xfs_bmbt_rec_t
*ep
; /* pointer to current extent */
360 xfs_bmbt_rec_t last
= {0, 0}; /* last extent in prev block */
361 xfs_bmbt_rec_t
*nextp
; /* pointer to next extent */
364 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
) {
368 /* skip large extent count inodes */
369 if (ip
->i_d
.di_nextents
> 10000)
374 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
375 block
= ifp
->if_broot
;
377 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
379 level
= be16_to_cpu(block
->bb_level
);
381 xfs_check_block(block
, mp
, 1, ifp
->if_broot_bytes
);
382 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, 1, ifp
->if_broot_bytes
);
383 bno
= be64_to_cpu(*pp
);
385 ASSERT(bno
!= NULLFSBLOCK
);
386 ASSERT(XFS_FSB_TO_AGNO(mp
, bno
) < mp
->m_sb
.sb_agcount
);
387 ASSERT(XFS_FSB_TO_AGBNO(mp
, bno
) < mp
->m_sb
.sb_agblocks
);
390 * Go down the tree until leaf level is reached, following the first
391 * pointer (leftmost) at each level.
393 while (level
-- > 0) {
394 /* See if buf is in cur first */
396 bp
= xfs_bmap_get_bp(cur
, XFS_FSB_TO_DADDR(mp
, bno
));
399 error
= xfs_btree_read_bufl(mp
, NULL
, bno
, 0, &bp
,
405 block
= XFS_BUF_TO_BLOCK(bp
);
410 * Check this block for basic sanity (increasing keys and
411 * no duplicate blocks).
414 xfs_check_block(block
, mp
, 0, 0);
415 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, mp
->m_bmap_dmxr
[1]);
416 bno
= be64_to_cpu(*pp
);
417 XFS_WANT_CORRUPTED_GOTO(mp
,
418 XFS_FSB_SANITY_CHECK(mp
, bno
), error0
);
421 xfs_trans_brelse(NULL
, bp
);
426 * Here with bp and block set to the leftmost leaf node in the tree.
431 * Loop over all leaf nodes checking that all extents are in the right order.
434 xfs_fsblock_t nextbno
;
435 xfs_extnum_t num_recs
;
438 num_recs
= xfs_btree_get_numrecs(block
);
441 * Read-ahead the next leaf block, if any.
444 nextbno
= be64_to_cpu(block
->bb_u
.l
.bb_rightsib
);
447 * Check all the extents to make sure they are OK.
448 * If we had a previous block, the last entry should
449 * conform with the first entry in this one.
452 ep
= XFS_BMBT_REC_ADDR(mp
, block
, 1);
454 ASSERT(xfs_bmbt_disk_get_startoff(&last
) +
455 xfs_bmbt_disk_get_blockcount(&last
) <=
456 xfs_bmbt_disk_get_startoff(ep
));
458 for (j
= 1; j
< num_recs
; j
++) {
459 nextp
= XFS_BMBT_REC_ADDR(mp
, block
, j
+ 1);
460 ASSERT(xfs_bmbt_disk_get_startoff(ep
) +
461 xfs_bmbt_disk_get_blockcount(ep
) <=
462 xfs_bmbt_disk_get_startoff(nextp
));
470 xfs_trans_brelse(NULL
, bp
);
474 * If we've reached the end, stop.
476 if (bno
== NULLFSBLOCK
)
480 bp
= xfs_bmap_get_bp(cur
, XFS_FSB_TO_DADDR(mp
, bno
));
483 error
= xfs_btree_read_bufl(mp
, NULL
, bno
, 0, &bp
,
489 block
= XFS_BUF_TO_BLOCK(bp
);
495 xfs_warn(mp
, "%s: at error0", __func__
);
497 xfs_trans_brelse(NULL
, bp
);
499 xfs_warn(mp
, "%s: BAD after btree leaves for %d extents",
501 panic("%s: CORRUPTED BTREE OR SOMETHING", __func__
);
506 * Add bmap trace insert entries for all the contents of the extent records.
509 xfs_bmap_trace_exlist(
510 xfs_inode_t
*ip
, /* incore inode pointer */
511 xfs_extnum_t cnt
, /* count of entries in the list */
512 int whichfork
, /* data or attr or cow fork */
513 unsigned long caller_ip
)
515 xfs_extnum_t idx
; /* extent record index */
516 xfs_ifork_t
*ifp
; /* inode fork pointer */
519 if (whichfork
== XFS_ATTR_FORK
)
520 state
|= BMAP_ATTRFORK
;
521 else if (whichfork
== XFS_COW_FORK
)
522 state
|= BMAP_COWFORK
;
524 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
525 ASSERT(cnt
== xfs_iext_count(ifp
));
526 for (idx
= 0; idx
< cnt
; idx
++)
527 trace_xfs_extlist(ip
, idx
, state
, caller_ip
);
531 * Validate that the bmbt_irecs being returned from bmapi are valid
532 * given the caller's original parameters. Specifically check the
533 * ranges of the returned irecs to ensure that they only extend beyond
534 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
537 xfs_bmap_validate_ret(
541 xfs_bmbt_irec_t
*mval
,
545 int i
; /* index to map values */
547 ASSERT(ret_nmap
<= nmap
);
549 for (i
= 0; i
< ret_nmap
; i
++) {
550 ASSERT(mval
[i
].br_blockcount
> 0);
551 if (!(flags
& XFS_BMAPI_ENTIRE
)) {
552 ASSERT(mval
[i
].br_startoff
>= bno
);
553 ASSERT(mval
[i
].br_blockcount
<= len
);
554 ASSERT(mval
[i
].br_startoff
+ mval
[i
].br_blockcount
<=
557 ASSERT(mval
[i
].br_startoff
< bno
+ len
);
558 ASSERT(mval
[i
].br_startoff
+ mval
[i
].br_blockcount
>
562 mval
[i
- 1].br_startoff
+ mval
[i
- 1].br_blockcount
==
563 mval
[i
].br_startoff
);
564 ASSERT(mval
[i
].br_startblock
!= DELAYSTARTBLOCK
&&
565 mval
[i
].br_startblock
!= HOLESTARTBLOCK
);
566 ASSERT(mval
[i
].br_state
== XFS_EXT_NORM
||
567 mval
[i
].br_state
== XFS_EXT_UNWRITTEN
);
572 #define xfs_bmap_check_leaf_extents(cur, ip, whichfork) do { } while (0)
573 #define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap)
577 * bmap free list manipulation functions
581 * Add the extent to the list of extents to be free at transaction end.
582 * The list is maintained sorted (by block number).
586 struct xfs_mount
*mp
,
587 struct xfs_defer_ops
*dfops
,
590 struct xfs_owner_info
*oinfo
)
592 struct xfs_extent_free_item
*new; /* new element */
597 ASSERT(bno
!= NULLFSBLOCK
);
599 ASSERT(len
<= MAXEXTLEN
);
600 ASSERT(!isnullstartblock(bno
));
601 agno
= XFS_FSB_TO_AGNO(mp
, bno
);
602 agbno
= XFS_FSB_TO_AGBNO(mp
, bno
);
603 ASSERT(agno
< mp
->m_sb
.sb_agcount
);
604 ASSERT(agbno
< mp
->m_sb
.sb_agblocks
);
605 ASSERT(len
< mp
->m_sb
.sb_agblocks
);
606 ASSERT(agbno
+ len
<= mp
->m_sb
.sb_agblocks
);
608 ASSERT(xfs_bmap_free_item_zone
!= NULL
);
610 new = kmem_zone_alloc(xfs_bmap_free_item_zone
, KM_SLEEP
);
611 new->xefi_startblock
= bno
;
612 new->xefi_blockcount
= (xfs_extlen_t
)len
;
614 new->xefi_oinfo
= *oinfo
;
616 xfs_rmap_skip_owner_update(&new->xefi_oinfo
);
617 trace_xfs_bmap_free_defer(mp
, XFS_FSB_TO_AGNO(mp
, bno
), 0,
618 XFS_FSB_TO_AGBNO(mp
, bno
), len
);
619 xfs_defer_add(dfops
, XFS_DEFER_OPS_TYPE_FREE
, &new->xefi_list
);
623 * Inode fork format manipulation functions
627 * Transform a btree format file with only one leaf node, where the
628 * extents list will fit in the inode, into an extents format file.
629 * Since the file extents are already in-core, all we have to do is
630 * give up the space for the btree root and pitch the leaf block.
632 STATIC
int /* error */
633 xfs_bmap_btree_to_extents(
634 xfs_trans_t
*tp
, /* transaction pointer */
635 xfs_inode_t
*ip
, /* incore inode pointer */
636 xfs_btree_cur_t
*cur
, /* btree cursor */
637 int *logflagsp
, /* inode logging flags */
638 int whichfork
) /* data or attr fork */
641 struct xfs_btree_block
*cblock
;/* child btree block */
642 xfs_fsblock_t cbno
; /* child block number */
643 xfs_buf_t
*cbp
; /* child block's buffer */
644 int error
; /* error return value */
645 xfs_ifork_t
*ifp
; /* inode fork data */
646 xfs_mount_t
*mp
; /* mount point structure */
647 __be64
*pp
; /* ptr to block address */
648 struct xfs_btree_block
*rblock
;/* root btree block */
649 struct xfs_owner_info oinfo
;
652 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
653 ASSERT(whichfork
!= XFS_COW_FORK
);
654 ASSERT(ifp
->if_flags
& XFS_IFEXTENTS
);
655 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
);
656 rblock
= ifp
->if_broot
;
657 ASSERT(be16_to_cpu(rblock
->bb_level
) == 1);
658 ASSERT(be16_to_cpu(rblock
->bb_numrecs
) == 1);
659 ASSERT(xfs_bmbt_maxrecs(mp
, ifp
->if_broot_bytes
, 0) == 1);
660 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, rblock
, 1, ifp
->if_broot_bytes
);
661 cbno
= be64_to_cpu(*pp
);
664 if ((error
= xfs_btree_check_lptr(cur
, cbno
, 1)))
667 error
= xfs_btree_read_bufl(mp
, tp
, cbno
, 0, &cbp
, XFS_BMAP_BTREE_REF
,
671 cblock
= XFS_BUF_TO_BLOCK(cbp
);
672 if ((error
= xfs_btree_check_block(cur
, cblock
, 0, cbp
)))
674 xfs_rmap_ino_bmbt_owner(&oinfo
, ip
->i_ino
, whichfork
);
675 xfs_bmap_add_free(mp
, cur
->bc_private
.b
.dfops
, cbno
, 1, &oinfo
);
676 ip
->i_d
.di_nblocks
--;
677 xfs_trans_mod_dquot_byino(tp
, ip
, XFS_TRANS_DQ_BCOUNT
, -1L);
678 xfs_trans_binval(tp
, cbp
);
679 if (cur
->bc_bufs
[0] == cbp
)
680 cur
->bc_bufs
[0] = NULL
;
681 xfs_iroot_realloc(ip
, -1, whichfork
);
682 ASSERT(ifp
->if_broot
== NULL
);
683 ASSERT((ifp
->if_flags
& XFS_IFBROOT
) == 0);
684 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_EXTENTS
);
685 *logflagsp
= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
690 * Convert an extents-format file into a btree-format file.
691 * The new file will have a root block (in the inode) and a single child block.
693 STATIC
int /* error */
694 xfs_bmap_extents_to_btree(
695 xfs_trans_t
*tp
, /* transaction pointer */
696 xfs_inode_t
*ip
, /* incore inode pointer */
697 xfs_fsblock_t
*firstblock
, /* first-block-allocated */
698 struct xfs_defer_ops
*dfops
, /* blocks freed in xaction */
699 xfs_btree_cur_t
**curp
, /* cursor returned to caller */
700 int wasdel
, /* converting a delayed alloc */
701 int *logflagsp
, /* inode logging flags */
702 int whichfork
) /* data or attr fork */
704 struct xfs_btree_block
*ablock
; /* allocated (child) bt block */
705 xfs_buf_t
*abp
; /* buffer for ablock */
706 xfs_alloc_arg_t args
; /* allocation arguments */
707 xfs_bmbt_rec_t
*arp
; /* child record pointer */
708 struct xfs_btree_block
*block
; /* btree root block */
709 xfs_btree_cur_t
*cur
; /* bmap btree cursor */
710 xfs_bmbt_rec_host_t
*ep
; /* extent record pointer */
711 int error
; /* error return value */
712 xfs_extnum_t i
, cnt
; /* extent record index */
713 xfs_ifork_t
*ifp
; /* inode fork pointer */
714 xfs_bmbt_key_t
*kp
; /* root block key pointer */
715 xfs_mount_t
*mp
; /* mount structure */
716 xfs_extnum_t nextents
; /* number of file extents */
717 xfs_bmbt_ptr_t
*pp
; /* root block address pointer */
720 ASSERT(whichfork
!= XFS_COW_FORK
);
721 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
722 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
);
725 * Make space in the inode incore.
727 xfs_iroot_realloc(ip
, 1, whichfork
);
728 ifp
->if_flags
|= XFS_IFBROOT
;
733 block
= ifp
->if_broot
;
734 xfs_btree_init_block_int(mp
, block
, XFS_BUF_DADDR_NULL
,
735 XFS_BTNUM_BMAP
, 1, 1, ip
->i_ino
,
736 XFS_BTREE_LONG_PTRS
);
738 * Need a cursor. Can't allocate until bb_level is filled in.
740 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
741 cur
->bc_private
.b
.firstblock
= *firstblock
;
742 cur
->bc_private
.b
.dfops
= dfops
;
743 cur
->bc_private
.b
.flags
= wasdel
? XFS_BTCUR_BPRV_WASDEL
: 0;
745 * Convert to a btree with two levels, one record in root.
747 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_BTREE
);
748 memset(&args
, 0, sizeof(args
));
751 xfs_rmap_ino_bmbt_owner(&args
.oinfo
, ip
->i_ino
, whichfork
);
752 args
.firstblock
= *firstblock
;
753 if (*firstblock
== NULLFSBLOCK
) {
754 args
.type
= XFS_ALLOCTYPE_START_BNO
;
755 args
.fsbno
= XFS_INO_TO_FSB(mp
, ip
->i_ino
);
756 } else if (dfops
->dop_low
) {
757 args
.type
= XFS_ALLOCTYPE_START_BNO
;
759 args
.fsbno
= *firstblock
;
761 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
762 args
.fsbno
= *firstblock
;
764 args
.minlen
= args
.maxlen
= args
.prod
= 1;
765 args
.wasdel
= wasdel
;
767 if ((error
= xfs_alloc_vextent(&args
))) {
768 xfs_iroot_realloc(ip
, -1, whichfork
);
769 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
774 * During a CoW operation, the allocation and bmbt updates occur in
775 * different transactions. The mapping code tries to put new bmbt
776 * blocks near extents being mapped, but the only way to guarantee this
777 * is if the alloc and the mapping happen in a single transaction that
778 * has a block reservation. That isn't the case here, so if we run out
779 * of space we'll try again with another AG.
781 if (xfs_sb_version_hasreflink(&cur
->bc_mp
->m_sb
) &&
782 args
.fsbno
== NULLFSBLOCK
&&
783 args
.type
== XFS_ALLOCTYPE_NEAR_BNO
) {
784 args
.type
= XFS_ALLOCTYPE_FIRST_AG
;
787 if (WARN_ON_ONCE(args
.fsbno
== NULLFSBLOCK
)) {
788 xfs_iroot_realloc(ip
, -1, whichfork
);
789 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
793 * Allocation can't fail, the space was reserved.
795 ASSERT(*firstblock
== NULLFSBLOCK
||
796 args
.agno
>= XFS_FSB_TO_AGNO(mp
, *firstblock
));
797 *firstblock
= cur
->bc_private
.b
.firstblock
= args
.fsbno
;
798 cur
->bc_private
.b
.allocated
++;
799 ip
->i_d
.di_nblocks
++;
800 xfs_trans_mod_dquot_byino(tp
, ip
, XFS_TRANS_DQ_BCOUNT
, 1L);
801 abp
= xfs_btree_get_bufl(mp
, tp
, args
.fsbno
, 0);
803 * Fill in the child block.
805 abp
->b_ops
= &xfs_bmbt_buf_ops
;
806 ablock
= XFS_BUF_TO_BLOCK(abp
);
807 xfs_btree_init_block_int(mp
, ablock
, abp
->b_bn
,
808 XFS_BTNUM_BMAP
, 0, 0, ip
->i_ino
,
809 XFS_BTREE_LONG_PTRS
);
811 arp
= XFS_BMBT_REC_ADDR(mp
, ablock
, 1);
812 nextents
= xfs_iext_count(ifp
);
813 for (cnt
= i
= 0; i
< nextents
; i
++) {
814 ep
= xfs_iext_get_ext(ifp
, i
);
815 if (!isnullstartblock(xfs_bmbt_get_startblock(ep
))) {
816 arp
->l0
= cpu_to_be64(ep
->l0
);
817 arp
->l1
= cpu_to_be64(ep
->l1
);
821 ASSERT(cnt
== XFS_IFORK_NEXTENTS(ip
, whichfork
));
822 xfs_btree_set_numrecs(ablock
, cnt
);
825 * Fill in the root key and pointer.
827 kp
= XFS_BMBT_KEY_ADDR(mp
, block
, 1);
828 arp
= XFS_BMBT_REC_ADDR(mp
, ablock
, 1);
829 kp
->br_startoff
= cpu_to_be64(xfs_bmbt_disk_get_startoff(arp
));
830 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, xfs_bmbt_get_maxrecs(cur
,
831 be16_to_cpu(block
->bb_level
)));
832 *pp
= cpu_to_be64(args
.fsbno
);
835 * Do all this logging at the end so that
836 * the root is at the right level.
838 xfs_btree_log_block(cur
, abp
, XFS_BB_ALL_BITS
);
839 xfs_btree_log_recs(cur
, abp
, 1, be16_to_cpu(ablock
->bb_numrecs
));
840 ASSERT(*curp
== NULL
);
842 *logflagsp
= XFS_ILOG_CORE
| xfs_ilog_fbroot(whichfork
);
847 * Convert a local file to an extents file.
848 * This code is out of bounds for data forks of regular files,
849 * since the file data needs to get logged so things will stay consistent.
850 * (The bmap-level manipulations are ok, though).
853 xfs_bmap_local_to_extents_empty(
854 struct xfs_inode
*ip
,
857 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
859 ASSERT(whichfork
!= XFS_COW_FORK
);
860 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
861 ASSERT(ifp
->if_bytes
== 0);
862 ASSERT(XFS_IFORK_NEXTENTS(ip
, whichfork
) == 0);
864 xfs_bmap_forkoff_reset(ip
, whichfork
);
865 ifp
->if_flags
&= ~XFS_IFINLINE
;
866 ifp
->if_flags
|= XFS_IFEXTENTS
;
867 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_EXTENTS
);
871 STATIC
int /* error */
872 xfs_bmap_local_to_extents(
873 xfs_trans_t
*tp
, /* transaction pointer */
874 xfs_inode_t
*ip
, /* incore inode pointer */
875 xfs_fsblock_t
*firstblock
, /* first block allocated in xaction */
876 xfs_extlen_t total
, /* total blocks needed by transaction */
877 int *logflagsp
, /* inode logging flags */
879 void (*init_fn
)(struct xfs_trans
*tp
,
881 struct xfs_inode
*ip
,
882 struct xfs_ifork
*ifp
))
885 int flags
; /* logging flags returned */
886 xfs_ifork_t
*ifp
; /* inode fork pointer */
887 xfs_alloc_arg_t args
; /* allocation arguments */
888 xfs_buf_t
*bp
; /* buffer for extent block */
889 xfs_bmbt_rec_host_t
*ep
; /* extent record pointer */
892 * We don't want to deal with the case of keeping inode data inline yet.
893 * So sending the data fork of a regular inode is invalid.
895 ASSERT(!(S_ISREG(VFS_I(ip
)->i_mode
) && whichfork
== XFS_DATA_FORK
));
896 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
897 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
899 if (!ifp
->if_bytes
) {
900 xfs_bmap_local_to_extents_empty(ip
, whichfork
);
901 flags
= XFS_ILOG_CORE
;
907 ASSERT((ifp
->if_flags
& (XFS_IFINLINE
|XFS_IFEXTENTS
|XFS_IFEXTIREC
)) ==
909 memset(&args
, 0, sizeof(args
));
911 args
.mp
= ip
->i_mount
;
912 xfs_rmap_ino_owner(&args
.oinfo
, ip
->i_ino
, whichfork
, 0);
913 args
.firstblock
= *firstblock
;
915 * Allocate a block. We know we need only one, since the
916 * file currently fits in an inode.
918 if (*firstblock
== NULLFSBLOCK
) {
920 args
.fsbno
= XFS_INO_TO_FSB(args
.mp
, ip
->i_ino
);
921 args
.type
= XFS_ALLOCTYPE_START_BNO
;
923 args
.fsbno
= *firstblock
;
924 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
927 args
.minlen
= args
.maxlen
= args
.prod
= 1;
928 error
= xfs_alloc_vextent(&args
);
933 * During a CoW operation, the allocation and bmbt updates occur in
934 * different transactions. The mapping code tries to put new bmbt
935 * blocks near extents being mapped, but the only way to guarantee this
936 * is if the alloc and the mapping happen in a single transaction that
937 * has a block reservation. That isn't the case here, so if we run out
938 * of space we'll try again with another AG.
940 if (xfs_sb_version_hasreflink(&ip
->i_mount
->m_sb
) &&
941 args
.fsbno
== NULLFSBLOCK
&&
942 args
.type
== XFS_ALLOCTYPE_NEAR_BNO
) {
945 /* Can't fail, the space was reserved. */
946 ASSERT(args
.fsbno
!= NULLFSBLOCK
);
947 ASSERT(args
.len
== 1);
948 *firstblock
= args
.fsbno
;
949 bp
= xfs_btree_get_bufl(args
.mp
, tp
, args
.fsbno
, 0);
952 * Initialize the block, copy the data and log the remote buffer.
954 * The callout is responsible for logging because the remote format
955 * might differ from the local format and thus we don't know how much to
956 * log here. Note that init_fn must also set the buffer log item type
959 init_fn(tp
, bp
, ip
, ifp
);
961 /* account for the change in fork size */
962 xfs_idata_realloc(ip
, -ifp
->if_bytes
, whichfork
);
963 xfs_bmap_local_to_extents_empty(ip
, whichfork
);
964 flags
|= XFS_ILOG_CORE
;
966 xfs_iext_add(ifp
, 0, 1);
967 ep
= xfs_iext_get_ext(ifp
, 0);
968 xfs_bmbt_set_allf(ep
, 0, args
.fsbno
, 1, XFS_EXT_NORM
);
969 trace_xfs_bmap_post_update(ip
, 0,
970 whichfork
== XFS_ATTR_FORK
? BMAP_ATTRFORK
: 0,
972 XFS_IFORK_NEXT_SET(ip
, whichfork
, 1);
973 ip
->i_d
.di_nblocks
= 1;
974 xfs_trans_mod_dquot_byino(tp
, ip
,
975 XFS_TRANS_DQ_BCOUNT
, 1L);
976 flags
|= xfs_ilog_fext(whichfork
);
984 * Called from xfs_bmap_add_attrfork to handle btree format files.
986 STATIC
int /* error */
987 xfs_bmap_add_attrfork_btree(
988 xfs_trans_t
*tp
, /* transaction pointer */
989 xfs_inode_t
*ip
, /* incore inode pointer */
990 xfs_fsblock_t
*firstblock
, /* first block allocated */
991 struct xfs_defer_ops
*dfops
, /* blocks to free at commit */
992 int *flags
) /* inode logging flags */
994 xfs_btree_cur_t
*cur
; /* btree cursor */
995 int error
; /* error return value */
996 xfs_mount_t
*mp
; /* file system mount struct */
997 int stat
; /* newroot status */
1000 if (ip
->i_df
.if_broot_bytes
<= XFS_IFORK_DSIZE(ip
))
1001 *flags
|= XFS_ILOG_DBROOT
;
1003 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, XFS_DATA_FORK
);
1004 cur
->bc_private
.b
.dfops
= dfops
;
1005 cur
->bc_private
.b
.firstblock
= *firstblock
;
1006 if ((error
= xfs_bmbt_lookup_ge(cur
, 0, 0, 0, &stat
)))
1008 /* must be at least one entry */
1009 XFS_WANT_CORRUPTED_GOTO(mp
, stat
== 1, error0
);
1010 if ((error
= xfs_btree_new_iroot(cur
, flags
, &stat
)))
1013 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
1016 *firstblock
= cur
->bc_private
.b
.firstblock
;
1017 cur
->bc_private
.b
.allocated
= 0;
1018 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
1022 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
1027 * Called from xfs_bmap_add_attrfork to handle extents format files.
1029 STATIC
int /* error */
1030 xfs_bmap_add_attrfork_extents(
1031 xfs_trans_t
*tp
, /* transaction pointer */
1032 xfs_inode_t
*ip
, /* incore inode pointer */
1033 xfs_fsblock_t
*firstblock
, /* first block allocated */
1034 struct xfs_defer_ops
*dfops
, /* blocks to free at commit */
1035 int *flags
) /* inode logging flags */
1037 xfs_btree_cur_t
*cur
; /* bmap btree cursor */
1038 int error
; /* error return value */
1040 if (ip
->i_d
.di_nextents
* sizeof(xfs_bmbt_rec_t
) <= XFS_IFORK_DSIZE(ip
))
1043 error
= xfs_bmap_extents_to_btree(tp
, ip
, firstblock
, dfops
, &cur
, 0,
1044 flags
, XFS_DATA_FORK
);
1046 cur
->bc_private
.b
.allocated
= 0;
1047 xfs_btree_del_cursor(cur
,
1048 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
1054 * Called from xfs_bmap_add_attrfork to handle local format files. Each
1055 * different data fork content type needs a different callout to do the
1056 * conversion. Some are basic and only require special block initialisation
1057 * callouts for the data formating, others (directories) are so specialised they
1058 * handle everything themselves.
1060 * XXX (dgc): investigate whether directory conversion can use the generic
1061 * formatting callout. It should be possible - it's just a very complex
1064 STATIC
int /* error */
1065 xfs_bmap_add_attrfork_local(
1066 xfs_trans_t
*tp
, /* transaction pointer */
1067 xfs_inode_t
*ip
, /* incore inode pointer */
1068 xfs_fsblock_t
*firstblock
, /* first block allocated */
1069 struct xfs_defer_ops
*dfops
, /* blocks to free at commit */
1070 int *flags
) /* inode logging flags */
1072 xfs_da_args_t dargs
; /* args for dir/attr code */
1074 if (ip
->i_df
.if_bytes
<= XFS_IFORK_DSIZE(ip
))
1077 if (S_ISDIR(VFS_I(ip
)->i_mode
)) {
1078 memset(&dargs
, 0, sizeof(dargs
));
1079 dargs
.geo
= ip
->i_mount
->m_dir_geo
;
1081 dargs
.firstblock
= firstblock
;
1082 dargs
.dfops
= dfops
;
1083 dargs
.total
= dargs
.geo
->fsbcount
;
1084 dargs
.whichfork
= XFS_DATA_FORK
;
1086 return xfs_dir2_sf_to_block(&dargs
);
1089 if (S_ISLNK(VFS_I(ip
)->i_mode
))
1090 return xfs_bmap_local_to_extents(tp
, ip
, firstblock
, 1,
1091 flags
, XFS_DATA_FORK
,
1092 xfs_symlink_local_to_remote
);
1094 /* should only be called for types that support local format data */
1096 return -EFSCORRUPTED
;
1100 * Convert inode from non-attributed to attributed.
1101 * Must not be in a transaction, ip must not be locked.
1103 int /* error code */
1104 xfs_bmap_add_attrfork(
1105 xfs_inode_t
*ip
, /* incore inode pointer */
1106 int size
, /* space new attribute needs */
1107 int rsvd
) /* xact may use reserved blks */
1109 xfs_fsblock_t firstblock
; /* 1st block/ag allocated */
1110 struct xfs_defer_ops dfops
; /* freed extent records */
1111 xfs_mount_t
*mp
; /* mount structure */
1112 xfs_trans_t
*tp
; /* transaction pointer */
1113 int blks
; /* space reservation */
1114 int version
= 1; /* superblock attr version */
1115 int logflags
; /* logging flags */
1116 int error
; /* error return value */
1118 ASSERT(XFS_IFORK_Q(ip
) == 0);
1121 ASSERT(!XFS_NOT_DQATTACHED(mp
, ip
));
1123 blks
= XFS_ADDAFORK_SPACE_RES(mp
);
1125 error
= xfs_trans_alloc(mp
, &M_RES(mp
)->tr_addafork
, blks
, 0,
1126 rsvd
? XFS_TRANS_RESERVE
: 0, &tp
);
1130 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
1131 error
= xfs_trans_reserve_quota_nblks(tp
, ip
, blks
, 0, rsvd
?
1132 XFS_QMOPT_RES_REGBLKS
| XFS_QMOPT_FORCE_RES
:
1133 XFS_QMOPT_RES_REGBLKS
);
1136 if (XFS_IFORK_Q(ip
))
1138 if (ip
->i_d
.di_anextents
!= 0) {
1139 error
= -EFSCORRUPTED
;
1142 if (ip
->i_d
.di_aformat
!= XFS_DINODE_FMT_EXTENTS
) {
1144 * For inodes coming from pre-6.2 filesystems.
1146 ASSERT(ip
->i_d
.di_aformat
== 0);
1147 ip
->i_d
.di_aformat
= XFS_DINODE_FMT_EXTENTS
;
1150 xfs_trans_ijoin(tp
, ip
, 0);
1151 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
1153 switch (ip
->i_d
.di_format
) {
1154 case XFS_DINODE_FMT_DEV
:
1155 ip
->i_d
.di_forkoff
= roundup(sizeof(xfs_dev_t
), 8) >> 3;
1157 case XFS_DINODE_FMT_UUID
:
1158 ip
->i_d
.di_forkoff
= roundup(sizeof(uuid_t
), 8) >> 3;
1160 case XFS_DINODE_FMT_LOCAL
:
1161 case XFS_DINODE_FMT_EXTENTS
:
1162 case XFS_DINODE_FMT_BTREE
:
1163 ip
->i_d
.di_forkoff
= xfs_attr_shortform_bytesfit(ip
, size
);
1164 if (!ip
->i_d
.di_forkoff
)
1165 ip
->i_d
.di_forkoff
= xfs_default_attroffset(ip
) >> 3;
1166 else if (mp
->m_flags
& XFS_MOUNT_ATTR2
)
1175 ASSERT(ip
->i_afp
== NULL
);
1176 ip
->i_afp
= kmem_zone_zalloc(xfs_ifork_zone
, KM_SLEEP
);
1177 ip
->i_afp
->if_flags
= XFS_IFEXTENTS
;
1179 xfs_defer_init(&dfops
, &firstblock
);
1180 switch (ip
->i_d
.di_format
) {
1181 case XFS_DINODE_FMT_LOCAL
:
1182 error
= xfs_bmap_add_attrfork_local(tp
, ip
, &firstblock
, &dfops
,
1185 case XFS_DINODE_FMT_EXTENTS
:
1186 error
= xfs_bmap_add_attrfork_extents(tp
, ip
, &firstblock
,
1189 case XFS_DINODE_FMT_BTREE
:
1190 error
= xfs_bmap_add_attrfork_btree(tp
, ip
, &firstblock
, &dfops
,
1198 xfs_trans_log_inode(tp
, ip
, logflags
);
1201 if (!xfs_sb_version_hasattr(&mp
->m_sb
) ||
1202 (!xfs_sb_version_hasattr2(&mp
->m_sb
) && version
== 2)) {
1203 bool log_sb
= false;
1205 spin_lock(&mp
->m_sb_lock
);
1206 if (!xfs_sb_version_hasattr(&mp
->m_sb
)) {
1207 xfs_sb_version_addattr(&mp
->m_sb
);
1210 if (!xfs_sb_version_hasattr2(&mp
->m_sb
) && version
== 2) {
1211 xfs_sb_version_addattr2(&mp
->m_sb
);
1214 spin_unlock(&mp
->m_sb_lock
);
1219 error
= xfs_defer_finish(&tp
, &dfops
, NULL
);
1222 error
= xfs_trans_commit(tp
);
1223 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
1227 xfs_defer_cancel(&dfops
);
1229 xfs_trans_cancel(tp
);
1230 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
1235 * Internal and external extent tree search functions.
1239 * Read in the extents to if_extents.
1240 * All inode fields are set up by caller, we just traverse the btree
1241 * and copy the records in. If the file system cannot contain unwritten
1242 * extents, the records are checked for no "state" flags.
1245 xfs_bmap_read_extents(
1246 xfs_trans_t
*tp
, /* transaction pointer */
1247 xfs_inode_t
*ip
, /* incore inode */
1248 int whichfork
) /* data or attr fork */
1250 struct xfs_btree_block
*block
; /* current btree block */
1251 xfs_fsblock_t bno
; /* block # of "block" */
1252 xfs_buf_t
*bp
; /* buffer for "block" */
1253 int error
; /* error return value */
1254 xfs_exntfmt_t exntf
; /* XFS_EXTFMT_NOSTATE, if checking */
1255 xfs_extnum_t i
, j
; /* index into the extents list */
1256 xfs_ifork_t
*ifp
; /* fork structure */
1257 int level
; /* btree level, for checking */
1258 xfs_mount_t
*mp
; /* file system mount structure */
1259 __be64
*pp
; /* pointer to block address */
1261 xfs_extnum_t room
; /* number of entries there's room for */
1264 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1265 exntf
= (whichfork
!= XFS_DATA_FORK
) ? XFS_EXTFMT_NOSTATE
:
1266 XFS_EXTFMT_INODE(ip
);
1267 block
= ifp
->if_broot
;
1269 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
1271 level
= be16_to_cpu(block
->bb_level
);
1273 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, 1, ifp
->if_broot_bytes
);
1274 bno
= be64_to_cpu(*pp
);
1277 * Go down the tree until leaf level is reached, following the first
1278 * pointer (leftmost) at each level.
1280 while (level
-- > 0) {
1281 error
= xfs_btree_read_bufl(mp
, tp
, bno
, 0, &bp
,
1282 XFS_BMAP_BTREE_REF
, &xfs_bmbt_buf_ops
);
1285 block
= XFS_BUF_TO_BLOCK(bp
);
1288 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, mp
->m_bmap_dmxr
[1]);
1289 bno
= be64_to_cpu(*pp
);
1290 XFS_WANT_CORRUPTED_GOTO(mp
,
1291 XFS_FSB_SANITY_CHECK(mp
, bno
), error0
);
1292 xfs_trans_brelse(tp
, bp
);
1295 * Here with bp and block set to the leftmost leaf node in the tree.
1297 room
= xfs_iext_count(ifp
);
1300 * Loop over all leaf nodes. Copy information to the extent records.
1303 xfs_bmbt_rec_t
*frp
;
1304 xfs_fsblock_t nextbno
;
1305 xfs_extnum_t num_recs
;
1308 num_recs
= xfs_btree_get_numrecs(block
);
1309 if (unlikely(i
+ num_recs
> room
)) {
1310 ASSERT(i
+ num_recs
<= room
);
1311 xfs_warn(ip
->i_mount
,
1312 "corrupt dinode %Lu, (btree extents).",
1313 (unsigned long long) ip
->i_ino
);
1314 XFS_CORRUPTION_ERROR("xfs_bmap_read_extents(1)",
1315 XFS_ERRLEVEL_LOW
, ip
->i_mount
, block
);
1319 * Read-ahead the next leaf block, if any.
1321 nextbno
= be64_to_cpu(block
->bb_u
.l
.bb_rightsib
);
1322 if (nextbno
!= NULLFSBLOCK
)
1323 xfs_btree_reada_bufl(mp
, nextbno
, 1,
1326 * Copy records into the extent records.
1328 frp
= XFS_BMBT_REC_ADDR(mp
, block
, 1);
1330 for (j
= 0; j
< num_recs
; j
++, i
++, frp
++) {
1331 xfs_bmbt_rec_host_t
*trp
= xfs_iext_get_ext(ifp
, i
);
1332 trp
->l0
= be64_to_cpu(frp
->l0
);
1333 trp
->l1
= be64_to_cpu(frp
->l1
);
1335 if (exntf
== XFS_EXTFMT_NOSTATE
) {
1337 * Check all attribute bmap btree records and
1338 * any "older" data bmap btree records for a
1339 * set bit in the "extent flag" position.
1341 if (unlikely(xfs_check_nostate_extents(ifp
,
1342 start
, num_recs
))) {
1343 XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
1349 xfs_trans_brelse(tp
, bp
);
1352 * If we've reached the end, stop.
1354 if (bno
== NULLFSBLOCK
)
1356 error
= xfs_btree_read_bufl(mp
, tp
, bno
, 0, &bp
,
1357 XFS_BMAP_BTREE_REF
, &xfs_bmbt_buf_ops
);
1360 block
= XFS_BUF_TO_BLOCK(bp
);
1362 if (i
!= XFS_IFORK_NEXTENTS(ip
, whichfork
))
1363 return -EFSCORRUPTED
;
1364 ASSERT(i
== xfs_iext_count(ifp
));
1365 XFS_BMAP_TRACE_EXLIST(ip
, i
, whichfork
);
1368 xfs_trans_brelse(tp
, bp
);
1369 return -EFSCORRUPTED
;
1373 * Returns the file-relative block number of the first unused block(s)
1374 * in the file with at least "len" logically contiguous blocks free.
1375 * This is the lowest-address hole if the file has holes, else the first block
1376 * past the end of file.
1377 * Return 0 if the file is currently local (in-inode).
1380 xfs_bmap_first_unused(
1381 xfs_trans_t
*tp
, /* transaction pointer */
1382 xfs_inode_t
*ip
, /* incore inode */
1383 xfs_extlen_t len
, /* size of hole to find */
1384 xfs_fileoff_t
*first_unused
, /* unused block */
1385 int whichfork
) /* data or attr fork */
1387 int error
; /* error return value */
1388 int idx
; /* extent record index */
1389 xfs_ifork_t
*ifp
; /* inode fork pointer */
1390 xfs_fileoff_t lastaddr
; /* last block number seen */
1391 xfs_fileoff_t lowest
; /* lowest useful block */
1392 xfs_fileoff_t max
; /* starting useful block */
1393 xfs_fileoff_t off
; /* offset for this block */
1394 xfs_extnum_t nextents
; /* number of extent entries */
1396 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
||
1397 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
||
1398 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
1399 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
) {
1403 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1404 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
1405 (error
= xfs_iread_extents(tp
, ip
, whichfork
)))
1407 lowest
= *first_unused
;
1408 nextents
= xfs_iext_count(ifp
);
1409 for (idx
= 0, lastaddr
= 0, max
= lowest
; idx
< nextents
; idx
++) {
1410 xfs_bmbt_rec_host_t
*ep
= xfs_iext_get_ext(ifp
, idx
);
1411 off
= xfs_bmbt_get_startoff(ep
);
1413 * See if the hole before this extent will work.
1415 if (off
>= lowest
+ len
&& off
- max
>= len
) {
1416 *first_unused
= max
;
1419 lastaddr
= off
+ xfs_bmbt_get_blockcount(ep
);
1420 max
= XFS_FILEOFF_MAX(lastaddr
, lowest
);
1422 *first_unused
= max
;
1427 * Returns the file-relative block number of the last block - 1 before
1428 * last_block (input value) in the file.
1429 * This is not based on i_size, it is based on the extent records.
1430 * Returns 0 for local files, as they do not have extent records.
1433 xfs_bmap_last_before(
1434 struct xfs_trans
*tp
, /* transaction pointer */
1435 struct xfs_inode
*ip
, /* incore inode */
1436 xfs_fileoff_t
*last_block
, /* last block */
1437 int whichfork
) /* data or attr fork */
1439 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1440 struct xfs_bmbt_irec got
;
1444 switch (XFS_IFORK_FORMAT(ip
, whichfork
)) {
1445 case XFS_DINODE_FMT_LOCAL
:
1448 case XFS_DINODE_FMT_BTREE
:
1449 case XFS_DINODE_FMT_EXTENTS
:
1455 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
1456 error
= xfs_iread_extents(tp
, ip
, whichfork
);
1461 if (xfs_iext_lookup_extent(ip
, ifp
, *last_block
- 1, &idx
, &got
)) {
1462 if (got
.br_startoff
<= *last_block
- 1)
1466 if (xfs_iext_get_extent(ifp
, idx
- 1, &got
)) {
1467 *last_block
= got
.br_startoff
+ got
.br_blockcount
;
1476 xfs_bmap_last_extent(
1477 struct xfs_trans
*tp
,
1478 struct xfs_inode
*ip
,
1480 struct xfs_bmbt_irec
*rec
,
1483 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1487 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
1488 error
= xfs_iread_extents(tp
, ip
, whichfork
);
1493 nextents
= xfs_iext_count(ifp
);
1494 if (nextents
== 0) {
1499 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, nextents
- 1), rec
);
1505 * Check the last inode extent to determine whether this allocation will result
1506 * in blocks being allocated at the end of the file. When we allocate new data
1507 * blocks at the end of the file which do not start at the previous data block,
1508 * we will try to align the new blocks at stripe unit boundaries.
1510 * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be
1511 * at, or past the EOF.
1515 struct xfs_bmalloca
*bma
,
1518 struct xfs_bmbt_irec rec
;
1523 error
= xfs_bmap_last_extent(NULL
, bma
->ip
, whichfork
, &rec
,
1534 * Check if we are allocation or past the last extent, or at least into
1535 * the last delayed allocated extent.
1537 bma
->aeof
= bma
->offset
>= rec
.br_startoff
+ rec
.br_blockcount
||
1538 (bma
->offset
>= rec
.br_startoff
&&
1539 isnullstartblock(rec
.br_startblock
));
1544 * Returns the file-relative block number of the first block past eof in
1545 * the file. This is not based on i_size, it is based on the extent records.
1546 * Returns 0 for local files, as they do not have extent records.
1549 xfs_bmap_last_offset(
1550 struct xfs_inode
*ip
,
1551 xfs_fileoff_t
*last_block
,
1554 struct xfs_bmbt_irec rec
;
1560 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
)
1563 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
&&
1564 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
1567 error
= xfs_bmap_last_extent(NULL
, ip
, whichfork
, &rec
, &is_empty
);
1568 if (error
|| is_empty
)
1571 *last_block
= rec
.br_startoff
+ rec
.br_blockcount
;
1576 * Returns whether the selected fork of the inode has exactly one
1577 * block or not. For the data fork we check this matches di_size,
1578 * implying the file's range is 0..bsize-1.
1580 int /* 1=>1 block, 0=>otherwise */
1582 xfs_inode_t
*ip
, /* incore inode */
1583 int whichfork
) /* data or attr fork */
1585 xfs_bmbt_rec_host_t
*ep
; /* ptr to fork's extent */
1586 xfs_ifork_t
*ifp
; /* inode fork pointer */
1587 int rval
; /* return value */
1588 xfs_bmbt_irec_t s
; /* internal version of extent */
1591 if (whichfork
== XFS_DATA_FORK
)
1592 return XFS_ISIZE(ip
) == ip
->i_mount
->m_sb
.sb_blocksize
;
1594 if (XFS_IFORK_NEXTENTS(ip
, whichfork
) != 1)
1596 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
1598 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1599 ASSERT(ifp
->if_flags
& XFS_IFEXTENTS
);
1600 ep
= xfs_iext_get_ext(ifp
, 0);
1601 xfs_bmbt_get_all(ep
, &s
);
1602 rval
= s
.br_startoff
== 0 && s
.br_blockcount
== 1;
1603 if (rval
&& whichfork
== XFS_DATA_FORK
)
1604 ASSERT(XFS_ISIZE(ip
) == ip
->i_mount
->m_sb
.sb_blocksize
);
1609 * Extent tree manipulation functions used during allocation.
1613 * Convert a delayed allocation to a real allocation.
1615 STATIC
int /* error */
1616 xfs_bmap_add_extent_delay_real(
1617 struct xfs_bmalloca
*bma
,
1620 struct xfs_bmbt_irec
*new = &bma
->got
;
1621 int diff
; /* temp value */
1622 xfs_bmbt_rec_host_t
*ep
; /* extent entry for idx */
1623 int error
; /* error return value */
1624 int i
; /* temp state */
1625 xfs_ifork_t
*ifp
; /* inode fork pointer */
1626 xfs_fileoff_t new_endoff
; /* end offset of new entry */
1627 xfs_bmbt_irec_t r
[3]; /* neighbor extent entries */
1628 /* left is 0, right is 1, prev is 2 */
1629 int rval
=0; /* return value (logging flags) */
1630 int state
= 0;/* state bits, accessed thru macros */
1631 xfs_filblks_t da_new
; /* new count del alloc blocks used */
1632 xfs_filblks_t da_old
; /* old count del alloc blocks used */
1633 xfs_filblks_t temp
=0; /* value for da_new calculations */
1634 xfs_filblks_t temp2
=0;/* value for da_new calculations */
1635 int tmp_rval
; /* partial logging flags */
1636 struct xfs_mount
*mp
;
1637 xfs_extnum_t
*nextents
;
1639 mp
= bma
->ip
->i_mount
;
1640 ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
1641 ASSERT(whichfork
!= XFS_ATTR_FORK
);
1642 nextents
= (whichfork
== XFS_COW_FORK
? &bma
->ip
->i_cnextents
:
1643 &bma
->ip
->i_d
.di_nextents
);
1645 ASSERT(bma
->idx
>= 0);
1646 ASSERT(bma
->idx
<= xfs_iext_count(ifp
));
1647 ASSERT(!isnullstartblock(new->br_startblock
));
1649 (bma
->cur
->bc_private
.b
.flags
& XFS_BTCUR_BPRV_WASDEL
));
1651 XFS_STATS_INC(mp
, xs_add_exlist
);
1657 if (whichfork
== XFS_COW_FORK
)
1658 state
|= BMAP_COWFORK
;
1661 * Set up a bunch of variables to make the tests simpler.
1663 ep
= xfs_iext_get_ext(ifp
, bma
->idx
);
1664 xfs_bmbt_get_all(ep
, &PREV
);
1665 new_endoff
= new->br_startoff
+ new->br_blockcount
;
1666 ASSERT(PREV
.br_startoff
<= new->br_startoff
);
1667 ASSERT(PREV
.br_startoff
+ PREV
.br_blockcount
>= new_endoff
);
1669 da_old
= startblockval(PREV
.br_startblock
);
1673 * Set flags determining what part of the previous delayed allocation
1674 * extent is being replaced by a real allocation.
1676 if (PREV
.br_startoff
== new->br_startoff
)
1677 state
|= BMAP_LEFT_FILLING
;
1678 if (PREV
.br_startoff
+ PREV
.br_blockcount
== new_endoff
)
1679 state
|= BMAP_RIGHT_FILLING
;
1682 * Check and set flags if this segment has a left neighbor.
1683 * Don't set contiguous if the combined extent would be too large.
1686 state
|= BMAP_LEFT_VALID
;
1687 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
- 1), &LEFT
);
1689 if (isnullstartblock(LEFT
.br_startblock
))
1690 state
|= BMAP_LEFT_DELAY
;
1693 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
1694 LEFT
.br_startoff
+ LEFT
.br_blockcount
== new->br_startoff
&&
1695 LEFT
.br_startblock
+ LEFT
.br_blockcount
== new->br_startblock
&&
1696 LEFT
.br_state
== new->br_state
&&
1697 LEFT
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
1698 state
|= BMAP_LEFT_CONTIG
;
1701 * Check and set flags if this segment has a right neighbor.
1702 * Don't set contiguous if the combined extent would be too large.
1703 * Also check for all-three-contiguous being too large.
1705 if (bma
->idx
< xfs_iext_count(ifp
) - 1) {
1706 state
|= BMAP_RIGHT_VALID
;
1707 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
+ 1), &RIGHT
);
1709 if (isnullstartblock(RIGHT
.br_startblock
))
1710 state
|= BMAP_RIGHT_DELAY
;
1713 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
1714 new_endoff
== RIGHT
.br_startoff
&&
1715 new->br_startblock
+ new->br_blockcount
== RIGHT
.br_startblock
&&
1716 new->br_state
== RIGHT
.br_state
&&
1717 new->br_blockcount
+ RIGHT
.br_blockcount
<= MAXEXTLEN
&&
1718 ((state
& (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
1719 BMAP_RIGHT_FILLING
)) !=
1720 (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
1721 BMAP_RIGHT_FILLING
) ||
1722 LEFT
.br_blockcount
+ new->br_blockcount
+ RIGHT
.br_blockcount
1724 state
|= BMAP_RIGHT_CONTIG
;
1728 * Switch out based on the FILLING and CONTIG state bits.
1730 switch (state
& (BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
1731 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
)) {
1732 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
1733 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
1735 * Filling in all of a previously delayed allocation extent.
1736 * The left and right neighbors are both contiguous with new.
1739 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1740 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, bma
->idx
),
1741 LEFT
.br_blockcount
+ PREV
.br_blockcount
+
1742 RIGHT
.br_blockcount
);
1743 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1745 xfs_iext_remove(bma
->ip
, bma
->idx
+ 1, 2, state
);
1747 if (bma
->cur
== NULL
)
1748 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1750 rval
= XFS_ILOG_CORE
;
1751 error
= xfs_bmbt_lookup_eq(bma
->cur
, RIGHT
.br_startoff
,
1752 RIGHT
.br_startblock
,
1753 RIGHT
.br_blockcount
, &i
);
1756 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1757 error
= xfs_btree_delete(bma
->cur
, &i
);
1760 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1761 error
= xfs_btree_decrement(bma
->cur
, 0, &i
);
1764 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1765 error
= xfs_bmbt_update(bma
->cur
, LEFT
.br_startoff
,
1767 LEFT
.br_blockcount
+
1768 PREV
.br_blockcount
+
1769 RIGHT
.br_blockcount
, LEFT
.br_state
);
1775 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
1777 * Filling in all of a previously delayed allocation extent.
1778 * The left neighbor is contiguous, the right is not.
1782 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1783 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, bma
->idx
),
1784 LEFT
.br_blockcount
+ PREV
.br_blockcount
);
1785 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1787 xfs_iext_remove(bma
->ip
, bma
->idx
+ 1, 1, state
);
1788 if (bma
->cur
== NULL
)
1789 rval
= XFS_ILOG_DEXT
;
1792 error
= xfs_bmbt_lookup_eq(bma
->cur
, LEFT
.br_startoff
,
1793 LEFT
.br_startblock
, LEFT
.br_blockcount
,
1797 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1798 error
= xfs_bmbt_update(bma
->cur
, LEFT
.br_startoff
,
1800 LEFT
.br_blockcount
+
1801 PREV
.br_blockcount
, LEFT
.br_state
);
1807 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
1809 * Filling in all of a previously delayed allocation extent.
1810 * The right neighbor is contiguous, the left is not.
1812 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1813 xfs_bmbt_set_startblock(ep
, new->br_startblock
);
1814 xfs_bmbt_set_blockcount(ep
,
1815 PREV
.br_blockcount
+ RIGHT
.br_blockcount
);
1816 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1818 xfs_iext_remove(bma
->ip
, bma
->idx
+ 1, 1, state
);
1819 if (bma
->cur
== NULL
)
1820 rval
= XFS_ILOG_DEXT
;
1823 error
= xfs_bmbt_lookup_eq(bma
->cur
, RIGHT
.br_startoff
,
1824 RIGHT
.br_startblock
,
1825 RIGHT
.br_blockcount
, &i
);
1828 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1829 error
= xfs_bmbt_update(bma
->cur
, PREV
.br_startoff
,
1831 PREV
.br_blockcount
+
1832 RIGHT
.br_blockcount
, PREV
.br_state
);
1838 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
1840 * Filling in all of a previously delayed allocation extent.
1841 * Neither the left nor right neighbors are contiguous with
1844 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1845 xfs_bmbt_set_startblock(ep
, new->br_startblock
);
1846 xfs_bmbt_set_state(ep
, new->br_state
);
1847 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1850 if (bma
->cur
== NULL
)
1851 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1853 rval
= XFS_ILOG_CORE
;
1854 error
= xfs_bmbt_lookup_eq(bma
->cur
, new->br_startoff
,
1855 new->br_startblock
, new->br_blockcount
,
1859 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
1860 bma
->cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
1861 error
= xfs_btree_insert(bma
->cur
, &i
);
1864 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1868 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
:
1870 * Filling in the first part of a previous delayed allocation.
1871 * The left neighbor is contiguous.
1873 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
- 1, state
, _THIS_IP_
);
1874 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, bma
->idx
- 1),
1875 LEFT
.br_blockcount
+ new->br_blockcount
);
1876 xfs_bmbt_set_startoff(ep
,
1877 PREV
.br_startoff
+ new->br_blockcount
);
1878 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
- 1, state
, _THIS_IP_
);
1880 temp
= PREV
.br_blockcount
- new->br_blockcount
;
1881 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1882 xfs_bmbt_set_blockcount(ep
, temp
);
1883 if (bma
->cur
== NULL
)
1884 rval
= XFS_ILOG_DEXT
;
1887 error
= xfs_bmbt_lookup_eq(bma
->cur
, LEFT
.br_startoff
,
1888 LEFT
.br_startblock
, LEFT
.br_blockcount
,
1892 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1893 error
= xfs_bmbt_update(bma
->cur
, LEFT
.br_startoff
,
1895 LEFT
.br_blockcount
+
1901 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
1902 startblockval(PREV
.br_startblock
));
1903 xfs_bmbt_set_startblock(ep
, nullstartblock(da_new
));
1904 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1909 case BMAP_LEFT_FILLING
:
1911 * Filling in the first part of a previous delayed allocation.
1912 * The left neighbor is not contiguous.
1914 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1915 xfs_bmbt_set_startoff(ep
, new_endoff
);
1916 temp
= PREV
.br_blockcount
- new->br_blockcount
;
1917 xfs_bmbt_set_blockcount(ep
, temp
);
1918 xfs_iext_insert(bma
->ip
, bma
->idx
, 1, new, state
);
1920 if (bma
->cur
== NULL
)
1921 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1923 rval
= XFS_ILOG_CORE
;
1924 error
= xfs_bmbt_lookup_eq(bma
->cur
, new->br_startoff
,
1925 new->br_startblock
, new->br_blockcount
,
1929 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
1930 bma
->cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
1931 error
= xfs_btree_insert(bma
->cur
, &i
);
1934 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1937 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
1938 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
1939 bma
->firstblock
, bma
->dfops
,
1940 &bma
->cur
, 1, &tmp_rval
, whichfork
);
1945 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
1946 startblockval(PREV
.br_startblock
) -
1947 (bma
->cur
? bma
->cur
->bc_private
.b
.allocated
: 0));
1948 ep
= xfs_iext_get_ext(ifp
, bma
->idx
+ 1);
1949 xfs_bmbt_set_startblock(ep
, nullstartblock(da_new
));
1950 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
+ 1, state
, _THIS_IP_
);
1953 case BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
1955 * Filling in the last part of a previous delayed allocation.
1956 * The right neighbor is contiguous with the new allocation.
1958 temp
= PREV
.br_blockcount
- new->br_blockcount
;
1959 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
+ 1, state
, _THIS_IP_
);
1960 xfs_bmbt_set_blockcount(ep
, temp
);
1961 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp
, bma
->idx
+ 1),
1962 new->br_startoff
, new->br_startblock
,
1963 new->br_blockcount
+ RIGHT
.br_blockcount
,
1965 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
+ 1, state
, _THIS_IP_
);
1966 if (bma
->cur
== NULL
)
1967 rval
= XFS_ILOG_DEXT
;
1970 error
= xfs_bmbt_lookup_eq(bma
->cur
, RIGHT
.br_startoff
,
1971 RIGHT
.br_startblock
,
1972 RIGHT
.br_blockcount
, &i
);
1975 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1976 error
= xfs_bmbt_update(bma
->cur
, new->br_startoff
,
1978 new->br_blockcount
+
1979 RIGHT
.br_blockcount
,
1985 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
1986 startblockval(PREV
.br_startblock
));
1987 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1988 xfs_bmbt_set_startblock(ep
, nullstartblock(da_new
));
1989 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1994 case BMAP_RIGHT_FILLING
:
1996 * Filling in the last part of a previous delayed allocation.
1997 * The right neighbor is not contiguous.
1999 temp
= PREV
.br_blockcount
- new->br_blockcount
;
2000 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2001 xfs_bmbt_set_blockcount(ep
, temp
);
2002 xfs_iext_insert(bma
->ip
, bma
->idx
+ 1, 1, new, state
);
2004 if (bma
->cur
== NULL
)
2005 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2007 rval
= XFS_ILOG_CORE
;
2008 error
= xfs_bmbt_lookup_eq(bma
->cur
, new->br_startoff
,
2009 new->br_startblock
, new->br_blockcount
,
2013 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
2014 bma
->cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
2015 error
= xfs_btree_insert(bma
->cur
, &i
);
2018 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2021 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
2022 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
2023 bma
->firstblock
, bma
->dfops
, &bma
->cur
, 1,
2024 &tmp_rval
, whichfork
);
2029 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
2030 startblockval(PREV
.br_startblock
) -
2031 (bma
->cur
? bma
->cur
->bc_private
.b
.allocated
: 0));
2032 ep
= xfs_iext_get_ext(ifp
, bma
->idx
);
2033 xfs_bmbt_set_startblock(ep
, nullstartblock(da_new
));
2034 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2041 * Filling in the middle part of a previous delayed allocation.
2042 * Contiguity is impossible here.
2043 * This case is avoided almost all the time.
2045 * We start with a delayed allocation:
2047 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
2050 * and we are allocating:
2051 * +rrrrrrrrrrrrrrrrr+
2054 * and we set it up for insertion as:
2055 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
2057 * PREV @ idx LEFT RIGHT
2058 * inserted at idx + 1
2060 temp
= new->br_startoff
- PREV
.br_startoff
;
2061 temp2
= PREV
.br_startoff
+ PREV
.br_blockcount
- new_endoff
;
2062 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, 0, _THIS_IP_
);
2063 xfs_bmbt_set_blockcount(ep
, temp
); /* truncate PREV */
2065 RIGHT
.br_state
= PREV
.br_state
;
2066 RIGHT
.br_startblock
= nullstartblock(
2067 (int)xfs_bmap_worst_indlen(bma
->ip
, temp2
));
2068 RIGHT
.br_startoff
= new_endoff
;
2069 RIGHT
.br_blockcount
= temp2
;
2070 /* insert LEFT (r[0]) and RIGHT (r[1]) at the same time */
2071 xfs_iext_insert(bma
->ip
, bma
->idx
+ 1, 2, &LEFT
, state
);
2073 if (bma
->cur
== NULL
)
2074 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2076 rval
= XFS_ILOG_CORE
;
2077 error
= xfs_bmbt_lookup_eq(bma
->cur
, new->br_startoff
,
2078 new->br_startblock
, new->br_blockcount
,
2082 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
2083 bma
->cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
2084 error
= xfs_btree_insert(bma
->cur
, &i
);
2087 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2090 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
2091 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
2092 bma
->firstblock
, bma
->dfops
, &bma
->cur
,
2093 1, &tmp_rval
, whichfork
);
2098 temp
= xfs_bmap_worst_indlen(bma
->ip
, temp
);
2099 temp2
= xfs_bmap_worst_indlen(bma
->ip
, temp2
);
2100 diff
= (int)(temp
+ temp2
- startblockval(PREV
.br_startblock
) -
2101 (bma
->cur
? bma
->cur
->bc_private
.b
.allocated
: 0));
2103 error
= xfs_mod_fdblocks(bma
->ip
->i_mount
,
2104 -((int64_t)diff
), false);
2110 ep
= xfs_iext_get_ext(ifp
, bma
->idx
);
2111 xfs_bmbt_set_startblock(ep
, nullstartblock((int)temp
));
2112 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2113 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
+ 2, state
, _THIS_IP_
);
2114 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp
, bma
->idx
+ 2),
2115 nullstartblock((int)temp2
));
2116 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
+ 2, state
, _THIS_IP_
);
2119 da_new
= temp
+ temp2
;
2122 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2123 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2124 case BMAP_LEFT_FILLING
| BMAP_RIGHT_CONTIG
:
2125 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
2126 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2127 case BMAP_LEFT_CONTIG
:
2128 case BMAP_RIGHT_CONTIG
:
2130 * These cases are all impossible.
2135 /* add reverse mapping */
2136 error
= xfs_rmap_map_extent(mp
, bma
->dfops
, bma
->ip
, whichfork
, new);
2140 /* convert to a btree if necessary */
2141 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
2142 int tmp_logflags
; /* partial log flag return val */
2144 ASSERT(bma
->cur
== NULL
);
2145 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
2146 bma
->firstblock
, bma
->dfops
, &bma
->cur
,
2147 da_old
> 0, &tmp_logflags
, whichfork
);
2148 bma
->logflags
|= tmp_logflags
;
2153 /* adjust for changes in reserved delayed indirect blocks */
2154 if (da_old
|| da_new
) {
2157 temp
+= bma
->cur
->bc_private
.b
.allocated
;
2158 ASSERT(temp
<= da_old
);
2160 xfs_mod_fdblocks(bma
->ip
->i_mount
,
2161 (int64_t)(da_old
- temp
), false);
2164 /* clear out the allocated field, done with it now in any case. */
2166 bma
->cur
->bc_private
.b
.allocated
= 0;
2168 xfs_bmap_check_leaf_extents(bma
->cur
, bma
->ip
, whichfork
);
2170 if (whichfork
!= XFS_COW_FORK
)
2171 bma
->logflags
|= rval
;
2179 * Convert an unwritten allocation to a real allocation or vice versa.
2181 STATIC
int /* error */
2182 xfs_bmap_add_extent_unwritten_real(
2183 struct xfs_trans
*tp
,
2184 xfs_inode_t
*ip
, /* incore inode pointer */
2186 xfs_extnum_t
*idx
, /* extent number to update/insert */
2187 xfs_btree_cur_t
**curp
, /* if *curp is null, not a btree */
2188 xfs_bmbt_irec_t
*new, /* new data to add to file extents */
2189 xfs_fsblock_t
*first
, /* pointer to firstblock variable */
2190 struct xfs_defer_ops
*dfops
, /* list of extents to be freed */
2191 int *logflagsp
) /* inode logging flags */
2193 xfs_btree_cur_t
*cur
; /* btree cursor */
2194 xfs_bmbt_rec_host_t
*ep
; /* extent entry for idx */
2195 int error
; /* error return value */
2196 int i
; /* temp state */
2197 xfs_ifork_t
*ifp
; /* inode fork pointer */
2198 xfs_fileoff_t new_endoff
; /* end offset of new entry */
2199 xfs_exntst_t newext
; /* new extent state */
2200 xfs_exntst_t oldext
; /* old extent state */
2201 xfs_bmbt_irec_t r
[3]; /* neighbor extent entries */
2202 /* left is 0, right is 1, prev is 2 */
2203 int rval
=0; /* return value (logging flags) */
2204 int state
= 0;/* state bits, accessed thru macros */
2205 struct xfs_mount
*mp
= ip
->i_mount
;
2210 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
2211 if (whichfork
== XFS_COW_FORK
)
2212 state
|= BMAP_COWFORK
;
2215 ASSERT(*idx
<= xfs_iext_count(ifp
));
2216 ASSERT(!isnullstartblock(new->br_startblock
));
2218 XFS_STATS_INC(mp
, xs_add_exlist
);
2225 * Set up a bunch of variables to make the tests simpler.
2228 ep
= xfs_iext_get_ext(ifp
, *idx
);
2229 xfs_bmbt_get_all(ep
, &PREV
);
2230 newext
= new->br_state
;
2231 oldext
= (newext
== XFS_EXT_UNWRITTEN
) ?
2232 XFS_EXT_NORM
: XFS_EXT_UNWRITTEN
;
2233 ASSERT(PREV
.br_state
== oldext
);
2234 new_endoff
= new->br_startoff
+ new->br_blockcount
;
2235 ASSERT(PREV
.br_startoff
<= new->br_startoff
);
2236 ASSERT(PREV
.br_startoff
+ PREV
.br_blockcount
>= new_endoff
);
2239 * Set flags determining what part of the previous oldext allocation
2240 * extent is being replaced by a newext allocation.
2242 if (PREV
.br_startoff
== new->br_startoff
)
2243 state
|= BMAP_LEFT_FILLING
;
2244 if (PREV
.br_startoff
+ PREV
.br_blockcount
== new_endoff
)
2245 state
|= BMAP_RIGHT_FILLING
;
2248 * Check and set flags if this segment has a left neighbor.
2249 * Don't set contiguous if the combined extent would be too large.
2252 state
|= BMAP_LEFT_VALID
;
2253 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, *idx
- 1), &LEFT
);
2255 if (isnullstartblock(LEFT
.br_startblock
))
2256 state
|= BMAP_LEFT_DELAY
;
2259 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
2260 LEFT
.br_startoff
+ LEFT
.br_blockcount
== new->br_startoff
&&
2261 LEFT
.br_startblock
+ LEFT
.br_blockcount
== new->br_startblock
&&
2262 LEFT
.br_state
== newext
&&
2263 LEFT
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
2264 state
|= BMAP_LEFT_CONTIG
;
2267 * Check and set flags if this segment has a right neighbor.
2268 * Don't set contiguous if the combined extent would be too large.
2269 * Also check for all-three-contiguous being too large.
2271 if (*idx
< xfs_iext_count(ifp
) - 1) {
2272 state
|= BMAP_RIGHT_VALID
;
2273 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, *idx
+ 1), &RIGHT
);
2274 if (isnullstartblock(RIGHT
.br_startblock
))
2275 state
|= BMAP_RIGHT_DELAY
;
2278 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
2279 new_endoff
== RIGHT
.br_startoff
&&
2280 new->br_startblock
+ new->br_blockcount
== RIGHT
.br_startblock
&&
2281 newext
== RIGHT
.br_state
&&
2282 new->br_blockcount
+ RIGHT
.br_blockcount
<= MAXEXTLEN
&&
2283 ((state
& (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
2284 BMAP_RIGHT_FILLING
)) !=
2285 (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
2286 BMAP_RIGHT_FILLING
) ||
2287 LEFT
.br_blockcount
+ new->br_blockcount
+ RIGHT
.br_blockcount
2289 state
|= BMAP_RIGHT_CONTIG
;
2292 * Switch out based on the FILLING and CONTIG state bits.
2294 switch (state
& (BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
2295 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
)) {
2296 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
2297 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2299 * Setting all of a previous oldext extent to newext.
2300 * The left and right neighbors are both contiguous with new.
2304 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2305 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
),
2306 LEFT
.br_blockcount
+ PREV
.br_blockcount
+
2307 RIGHT
.br_blockcount
);
2308 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2310 xfs_iext_remove(ip
, *idx
+ 1, 2, state
);
2311 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2312 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 2);
2314 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2316 rval
= XFS_ILOG_CORE
;
2317 if ((error
= xfs_bmbt_lookup_eq(cur
, RIGHT
.br_startoff
,
2318 RIGHT
.br_startblock
,
2319 RIGHT
.br_blockcount
, &i
)))
2321 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2322 if ((error
= xfs_btree_delete(cur
, &i
)))
2324 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2325 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2327 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2328 if ((error
= xfs_btree_delete(cur
, &i
)))
2330 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2331 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2333 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2334 if ((error
= xfs_bmbt_update(cur
, LEFT
.br_startoff
,
2336 LEFT
.br_blockcount
+ PREV
.br_blockcount
+
2337 RIGHT
.br_blockcount
, LEFT
.br_state
)))
2342 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
2344 * Setting all of a previous oldext extent to newext.
2345 * The left neighbor is contiguous, the right is not.
2349 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2350 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
),
2351 LEFT
.br_blockcount
+ PREV
.br_blockcount
);
2352 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2354 xfs_iext_remove(ip
, *idx
+ 1, 1, state
);
2355 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2356 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
2358 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2360 rval
= XFS_ILOG_CORE
;
2361 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2362 PREV
.br_startblock
, PREV
.br_blockcount
,
2365 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2366 if ((error
= xfs_btree_delete(cur
, &i
)))
2368 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2369 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2371 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2372 if ((error
= xfs_bmbt_update(cur
, LEFT
.br_startoff
,
2374 LEFT
.br_blockcount
+ PREV
.br_blockcount
,
2380 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2382 * Setting all of a previous oldext extent to newext.
2383 * The right neighbor is contiguous, the left is not.
2385 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2386 xfs_bmbt_set_blockcount(ep
,
2387 PREV
.br_blockcount
+ RIGHT
.br_blockcount
);
2388 xfs_bmbt_set_state(ep
, newext
);
2389 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2390 xfs_iext_remove(ip
, *idx
+ 1, 1, state
);
2391 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2392 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
2394 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2396 rval
= XFS_ILOG_CORE
;
2397 if ((error
= xfs_bmbt_lookup_eq(cur
, RIGHT
.br_startoff
,
2398 RIGHT
.br_startblock
,
2399 RIGHT
.br_blockcount
, &i
)))
2401 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2402 if ((error
= xfs_btree_delete(cur
, &i
)))
2404 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2405 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2407 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2408 if ((error
= xfs_bmbt_update(cur
, new->br_startoff
,
2410 new->br_blockcount
+ RIGHT
.br_blockcount
,
2416 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
2418 * Setting all of a previous oldext extent to newext.
2419 * Neither the left nor right neighbors are contiguous with
2422 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2423 xfs_bmbt_set_state(ep
, newext
);
2424 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2427 rval
= XFS_ILOG_DEXT
;
2430 if ((error
= xfs_bmbt_lookup_eq(cur
, new->br_startoff
,
2431 new->br_startblock
, new->br_blockcount
,
2434 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2435 if ((error
= xfs_bmbt_update(cur
, new->br_startoff
,
2436 new->br_startblock
, new->br_blockcount
,
2442 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
:
2444 * Setting the first part of a previous oldext extent to newext.
2445 * The left neighbor is contiguous.
2447 trace_xfs_bmap_pre_update(ip
, *idx
- 1, state
, _THIS_IP_
);
2448 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
- 1),
2449 LEFT
.br_blockcount
+ new->br_blockcount
);
2450 xfs_bmbt_set_startoff(ep
,
2451 PREV
.br_startoff
+ new->br_blockcount
);
2452 trace_xfs_bmap_post_update(ip
, *idx
- 1, state
, _THIS_IP_
);
2454 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2455 xfs_bmbt_set_startblock(ep
,
2456 new->br_startblock
+ new->br_blockcount
);
2457 xfs_bmbt_set_blockcount(ep
,
2458 PREV
.br_blockcount
- new->br_blockcount
);
2459 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2464 rval
= XFS_ILOG_DEXT
;
2467 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2468 PREV
.br_startblock
, PREV
.br_blockcount
,
2471 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2472 if ((error
= xfs_bmbt_update(cur
,
2473 PREV
.br_startoff
+ new->br_blockcount
,
2474 PREV
.br_startblock
+ new->br_blockcount
,
2475 PREV
.br_blockcount
- new->br_blockcount
,
2478 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2480 error
= xfs_bmbt_update(cur
, LEFT
.br_startoff
,
2482 LEFT
.br_blockcount
+ new->br_blockcount
,
2489 case BMAP_LEFT_FILLING
:
2491 * Setting the first part of a previous oldext extent to newext.
2492 * The left neighbor is not contiguous.
2494 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2495 ASSERT(ep
&& xfs_bmbt_get_state(ep
) == oldext
);
2496 xfs_bmbt_set_startoff(ep
, new_endoff
);
2497 xfs_bmbt_set_blockcount(ep
,
2498 PREV
.br_blockcount
- new->br_blockcount
);
2499 xfs_bmbt_set_startblock(ep
,
2500 new->br_startblock
+ new->br_blockcount
);
2501 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2503 xfs_iext_insert(ip
, *idx
, 1, new, state
);
2504 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2505 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
2507 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2509 rval
= XFS_ILOG_CORE
;
2510 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2511 PREV
.br_startblock
, PREV
.br_blockcount
,
2514 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2515 if ((error
= xfs_bmbt_update(cur
,
2516 PREV
.br_startoff
+ new->br_blockcount
,
2517 PREV
.br_startblock
+ new->br_blockcount
,
2518 PREV
.br_blockcount
- new->br_blockcount
,
2521 cur
->bc_rec
.b
= *new;
2522 if ((error
= xfs_btree_insert(cur
, &i
)))
2524 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2528 case BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2530 * Setting the last part of a previous oldext extent to newext.
2531 * The right neighbor is contiguous with the new allocation.
2533 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2534 xfs_bmbt_set_blockcount(ep
,
2535 PREV
.br_blockcount
- new->br_blockcount
);
2536 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2540 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2541 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp
, *idx
),
2542 new->br_startoff
, new->br_startblock
,
2543 new->br_blockcount
+ RIGHT
.br_blockcount
, newext
);
2544 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2547 rval
= XFS_ILOG_DEXT
;
2550 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2552 PREV
.br_blockcount
, &i
)))
2554 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2555 if ((error
= xfs_bmbt_update(cur
, PREV
.br_startoff
,
2557 PREV
.br_blockcount
- new->br_blockcount
,
2560 if ((error
= xfs_btree_increment(cur
, 0, &i
)))
2562 if ((error
= xfs_bmbt_update(cur
, new->br_startoff
,
2564 new->br_blockcount
+ RIGHT
.br_blockcount
,
2570 case BMAP_RIGHT_FILLING
:
2572 * Setting the last part of a previous oldext extent to newext.
2573 * The right neighbor is not contiguous.
2575 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2576 xfs_bmbt_set_blockcount(ep
,
2577 PREV
.br_blockcount
- new->br_blockcount
);
2578 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2581 xfs_iext_insert(ip
, *idx
, 1, new, state
);
2583 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2584 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
2586 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2588 rval
= XFS_ILOG_CORE
;
2589 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2590 PREV
.br_startblock
, PREV
.br_blockcount
,
2593 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2594 if ((error
= xfs_bmbt_update(cur
, PREV
.br_startoff
,
2596 PREV
.br_blockcount
- new->br_blockcount
,
2599 if ((error
= xfs_bmbt_lookup_eq(cur
, new->br_startoff
,
2600 new->br_startblock
, new->br_blockcount
,
2603 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
2604 cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
2605 if ((error
= xfs_btree_insert(cur
, &i
)))
2607 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2613 * Setting the middle part of a previous oldext extent to
2614 * newext. Contiguity is impossible here.
2615 * One extent becomes three extents.
2617 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2618 xfs_bmbt_set_blockcount(ep
,
2619 new->br_startoff
- PREV
.br_startoff
);
2620 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2623 r
[1].br_startoff
= new_endoff
;
2624 r
[1].br_blockcount
=
2625 PREV
.br_startoff
+ PREV
.br_blockcount
- new_endoff
;
2626 r
[1].br_startblock
= new->br_startblock
+ new->br_blockcount
;
2627 r
[1].br_state
= oldext
;
2630 xfs_iext_insert(ip
, *idx
, 2, &r
[0], state
);
2632 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2633 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 2);
2635 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2637 rval
= XFS_ILOG_CORE
;
2638 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2639 PREV
.br_startblock
, PREV
.br_blockcount
,
2642 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2643 /* new right extent - oldext */
2644 if ((error
= xfs_bmbt_update(cur
, r
[1].br_startoff
,
2645 r
[1].br_startblock
, r
[1].br_blockcount
,
2648 /* new left extent - oldext */
2649 cur
->bc_rec
.b
= PREV
;
2650 cur
->bc_rec
.b
.br_blockcount
=
2651 new->br_startoff
- PREV
.br_startoff
;
2652 if ((error
= xfs_btree_insert(cur
, &i
)))
2654 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2656 * Reset the cursor to the position of the new extent
2657 * we are about to insert as we can't trust it after
2658 * the previous insert.
2660 if ((error
= xfs_bmbt_lookup_eq(cur
, new->br_startoff
,
2661 new->br_startblock
, new->br_blockcount
,
2664 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
2665 /* new middle extent - newext */
2666 cur
->bc_rec
.b
.br_state
= new->br_state
;
2667 if ((error
= xfs_btree_insert(cur
, &i
)))
2669 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2673 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2674 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2675 case BMAP_LEFT_FILLING
| BMAP_RIGHT_CONTIG
:
2676 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
2677 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2678 case BMAP_LEFT_CONTIG
:
2679 case BMAP_RIGHT_CONTIG
:
2681 * These cases are all impossible.
2686 /* update reverse mappings */
2687 error
= xfs_rmap_convert_extent(mp
, dfops
, ip
, whichfork
, new);
2691 /* convert to a btree if necessary */
2692 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
2693 int tmp_logflags
; /* partial log flag return val */
2695 ASSERT(cur
== NULL
);
2696 error
= xfs_bmap_extents_to_btree(tp
, ip
, first
, dfops
, &cur
,
2697 0, &tmp_logflags
, whichfork
);
2698 *logflagsp
|= tmp_logflags
;
2703 /* clear out the allocated field, done with it now in any case. */
2705 cur
->bc_private
.b
.allocated
= 0;
2709 xfs_bmap_check_leaf_extents(*curp
, ip
, whichfork
);
2719 * Convert a hole to a delayed allocation.
2722 xfs_bmap_add_extent_hole_delay(
2723 xfs_inode_t
*ip
, /* incore inode pointer */
2725 xfs_extnum_t
*idx
, /* extent number to update/insert */
2726 xfs_bmbt_irec_t
*new) /* new data to add to file extents */
2728 xfs_ifork_t
*ifp
; /* inode fork pointer */
2729 xfs_bmbt_irec_t left
; /* left neighbor extent entry */
2730 xfs_filblks_t newlen
=0; /* new indirect size */
2731 xfs_filblks_t oldlen
=0; /* old indirect size */
2732 xfs_bmbt_irec_t right
; /* right neighbor extent entry */
2733 int state
; /* state bits, accessed thru macros */
2734 xfs_filblks_t temp
=0; /* temp for indirect calculations */
2736 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
2738 if (whichfork
== XFS_COW_FORK
)
2739 state
|= BMAP_COWFORK
;
2740 ASSERT(isnullstartblock(new->br_startblock
));
2743 * Check and set flags if this segment has a left neighbor
2746 state
|= BMAP_LEFT_VALID
;
2747 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, *idx
- 1), &left
);
2749 if (isnullstartblock(left
.br_startblock
))
2750 state
|= BMAP_LEFT_DELAY
;
2754 * Check and set flags if the current (right) segment exists.
2755 * If it doesn't exist, we're converting the hole at end-of-file.
2757 if (*idx
< xfs_iext_count(ifp
)) {
2758 state
|= BMAP_RIGHT_VALID
;
2759 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, *idx
), &right
);
2761 if (isnullstartblock(right
.br_startblock
))
2762 state
|= BMAP_RIGHT_DELAY
;
2766 * Set contiguity flags on the left and right neighbors.
2767 * Don't let extents get too large, even if the pieces are contiguous.
2769 if ((state
& BMAP_LEFT_VALID
) && (state
& BMAP_LEFT_DELAY
) &&
2770 left
.br_startoff
+ left
.br_blockcount
== new->br_startoff
&&
2771 left
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
2772 state
|= BMAP_LEFT_CONTIG
;
2774 if ((state
& BMAP_RIGHT_VALID
) && (state
& BMAP_RIGHT_DELAY
) &&
2775 new->br_startoff
+ new->br_blockcount
== right
.br_startoff
&&
2776 new->br_blockcount
+ right
.br_blockcount
<= MAXEXTLEN
&&
2777 (!(state
& BMAP_LEFT_CONTIG
) ||
2778 (left
.br_blockcount
+ new->br_blockcount
+
2779 right
.br_blockcount
<= MAXEXTLEN
)))
2780 state
|= BMAP_RIGHT_CONTIG
;
2783 * Switch out based on the contiguity flags.
2785 switch (state
& (BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
)) {
2786 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2788 * New allocation is contiguous with delayed allocations
2789 * on the left and on the right.
2790 * Merge all three into a single extent record.
2793 temp
= left
.br_blockcount
+ new->br_blockcount
+
2794 right
.br_blockcount
;
2796 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2797 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
), temp
);
2798 oldlen
= startblockval(left
.br_startblock
) +
2799 startblockval(new->br_startblock
) +
2800 startblockval(right
.br_startblock
);
2801 newlen
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
2803 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp
, *idx
),
2804 nullstartblock((int)newlen
));
2805 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2807 xfs_iext_remove(ip
, *idx
+ 1, 1, state
);
2810 case BMAP_LEFT_CONTIG
:
2812 * New allocation is contiguous with a delayed allocation
2814 * Merge the new allocation with the left neighbor.
2817 temp
= left
.br_blockcount
+ new->br_blockcount
;
2819 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2820 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
), temp
);
2821 oldlen
= startblockval(left
.br_startblock
) +
2822 startblockval(new->br_startblock
);
2823 newlen
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
2825 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp
, *idx
),
2826 nullstartblock((int)newlen
));
2827 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2830 case BMAP_RIGHT_CONTIG
:
2832 * New allocation is contiguous with a delayed allocation
2834 * Merge the new allocation with the right neighbor.
2836 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2837 temp
= new->br_blockcount
+ right
.br_blockcount
;
2838 oldlen
= startblockval(new->br_startblock
) +
2839 startblockval(right
.br_startblock
);
2840 newlen
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
2842 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp
, *idx
),
2844 nullstartblock((int)newlen
), temp
, right
.br_state
);
2845 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2850 * New allocation is not contiguous with another
2851 * delayed allocation.
2852 * Insert a new entry.
2854 oldlen
= newlen
= 0;
2855 xfs_iext_insert(ip
, *idx
, 1, new, state
);
2858 if (oldlen
!= newlen
) {
2859 ASSERT(oldlen
> newlen
);
2860 xfs_mod_fdblocks(ip
->i_mount
, (int64_t)(oldlen
- newlen
),
2863 * Nothing to do for disk quota accounting here.
2869 * Convert a hole to a real allocation.
2871 STATIC
int /* error */
2872 xfs_bmap_add_extent_hole_real(
2873 struct xfs_bmalloca
*bma
,
2876 struct xfs_bmbt_irec
*new = &bma
->got
;
2877 int error
; /* error return value */
2878 int i
; /* temp state */
2879 xfs_ifork_t
*ifp
; /* inode fork pointer */
2880 xfs_bmbt_irec_t left
; /* left neighbor extent entry */
2881 xfs_bmbt_irec_t right
; /* right neighbor extent entry */
2882 int rval
=0; /* return value (logging flags) */
2883 int state
; /* state bits, accessed thru macros */
2884 struct xfs_mount
*mp
;
2886 mp
= bma
->ip
->i_mount
;
2887 ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
2889 ASSERT(bma
->idx
>= 0);
2890 ASSERT(bma
->idx
<= xfs_iext_count(ifp
));
2891 ASSERT(!isnullstartblock(new->br_startblock
));
2893 !(bma
->cur
->bc_private
.b
.flags
& XFS_BTCUR_BPRV_WASDEL
));
2895 XFS_STATS_INC(mp
, xs_add_exlist
);
2898 if (whichfork
== XFS_ATTR_FORK
)
2899 state
|= BMAP_ATTRFORK
;
2900 if (whichfork
== XFS_COW_FORK
)
2901 state
|= BMAP_COWFORK
;
2904 * Check and set flags if this segment has a left neighbor.
2907 state
|= BMAP_LEFT_VALID
;
2908 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
- 1), &left
);
2909 if (isnullstartblock(left
.br_startblock
))
2910 state
|= BMAP_LEFT_DELAY
;
2914 * Check and set flags if this segment has a current value.
2915 * Not true if we're inserting into the "hole" at eof.
2917 if (bma
->idx
< xfs_iext_count(ifp
)) {
2918 state
|= BMAP_RIGHT_VALID
;
2919 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
), &right
);
2920 if (isnullstartblock(right
.br_startblock
))
2921 state
|= BMAP_RIGHT_DELAY
;
2925 * We're inserting a real allocation between "left" and "right".
2926 * Set the contiguity flags. Don't let extents get too large.
2928 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
2929 left
.br_startoff
+ left
.br_blockcount
== new->br_startoff
&&
2930 left
.br_startblock
+ left
.br_blockcount
== new->br_startblock
&&
2931 left
.br_state
== new->br_state
&&
2932 left
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
2933 state
|= BMAP_LEFT_CONTIG
;
2935 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
2936 new->br_startoff
+ new->br_blockcount
== right
.br_startoff
&&
2937 new->br_startblock
+ new->br_blockcount
== right
.br_startblock
&&
2938 new->br_state
== right
.br_state
&&
2939 new->br_blockcount
+ right
.br_blockcount
<= MAXEXTLEN
&&
2940 (!(state
& BMAP_LEFT_CONTIG
) ||
2941 left
.br_blockcount
+ new->br_blockcount
+
2942 right
.br_blockcount
<= MAXEXTLEN
))
2943 state
|= BMAP_RIGHT_CONTIG
;
2947 * Select which case we're in here, and implement it.
2949 switch (state
& (BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
)) {
2950 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2952 * New allocation is contiguous with real allocations on the
2953 * left and on the right.
2954 * Merge all three into a single extent record.
2957 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2958 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, bma
->idx
),
2959 left
.br_blockcount
+ new->br_blockcount
+
2960 right
.br_blockcount
);
2961 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2963 xfs_iext_remove(bma
->ip
, bma
->idx
+ 1, 1, state
);
2965 XFS_IFORK_NEXT_SET(bma
->ip
, whichfork
,
2966 XFS_IFORK_NEXTENTS(bma
->ip
, whichfork
) - 1);
2967 if (bma
->cur
== NULL
) {
2968 rval
= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
2970 rval
= XFS_ILOG_CORE
;
2971 error
= xfs_bmbt_lookup_eq(bma
->cur
, right
.br_startoff
,
2972 right
.br_startblock
, right
.br_blockcount
,
2976 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2977 error
= xfs_btree_delete(bma
->cur
, &i
);
2980 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2981 error
= xfs_btree_decrement(bma
->cur
, 0, &i
);
2984 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2985 error
= xfs_bmbt_update(bma
->cur
, left
.br_startoff
,
2987 left
.br_blockcount
+
2988 new->br_blockcount
+
2989 right
.br_blockcount
,
2996 case BMAP_LEFT_CONTIG
:
2998 * New allocation is contiguous with a real allocation
3000 * Merge the new allocation with the left neighbor.
3003 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
3004 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, bma
->idx
),
3005 left
.br_blockcount
+ new->br_blockcount
);
3006 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
3008 if (bma
->cur
== NULL
) {
3009 rval
= xfs_ilog_fext(whichfork
);
3012 error
= xfs_bmbt_lookup_eq(bma
->cur
, left
.br_startoff
,
3013 left
.br_startblock
, left
.br_blockcount
,
3017 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
3018 error
= xfs_bmbt_update(bma
->cur
, left
.br_startoff
,
3020 left
.br_blockcount
+
3028 case BMAP_RIGHT_CONTIG
:
3030 * New allocation is contiguous with a real allocation
3032 * Merge the new allocation with the right neighbor.
3034 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
3035 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp
, bma
->idx
),
3036 new->br_startoff
, new->br_startblock
,
3037 new->br_blockcount
+ right
.br_blockcount
,
3039 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
3041 if (bma
->cur
== NULL
) {
3042 rval
= xfs_ilog_fext(whichfork
);
3045 error
= xfs_bmbt_lookup_eq(bma
->cur
,
3047 right
.br_startblock
,
3048 right
.br_blockcount
, &i
);
3051 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
3052 error
= xfs_bmbt_update(bma
->cur
, new->br_startoff
,
3054 new->br_blockcount
+
3055 right
.br_blockcount
,
3064 * New allocation is not contiguous with another
3066 * Insert a new entry.
3068 xfs_iext_insert(bma
->ip
, bma
->idx
, 1, new, state
);
3069 XFS_IFORK_NEXT_SET(bma
->ip
, whichfork
,
3070 XFS_IFORK_NEXTENTS(bma
->ip
, whichfork
) + 1);
3071 if (bma
->cur
== NULL
) {
3072 rval
= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
3074 rval
= XFS_ILOG_CORE
;
3075 error
= xfs_bmbt_lookup_eq(bma
->cur
,
3078 new->br_blockcount
, &i
);
3081 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
3082 bma
->cur
->bc_rec
.b
.br_state
= new->br_state
;
3083 error
= xfs_btree_insert(bma
->cur
, &i
);
3086 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
3091 /* add reverse mapping */
3092 error
= xfs_rmap_map_extent(mp
, bma
->dfops
, bma
->ip
, whichfork
, new);
3096 /* convert to a btree if necessary */
3097 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
3098 int tmp_logflags
; /* partial log flag return val */
3100 ASSERT(bma
->cur
== NULL
);
3101 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
3102 bma
->firstblock
, bma
->dfops
, &bma
->cur
,
3103 0, &tmp_logflags
, whichfork
);
3104 bma
->logflags
|= tmp_logflags
;
3109 /* clear out the allocated field, done with it now in any case. */
3111 bma
->cur
->bc_private
.b
.allocated
= 0;
3113 xfs_bmap_check_leaf_extents(bma
->cur
, bma
->ip
, whichfork
);
3115 bma
->logflags
|= rval
;
3120 * Functions used in the extent read, allocate and remove paths
3124 * Adjust the size of the new extent based on di_extsize and rt extsize.
3127 xfs_bmap_extsize_align(
3129 xfs_bmbt_irec_t
*gotp
, /* next extent pointer */
3130 xfs_bmbt_irec_t
*prevp
, /* previous extent pointer */
3131 xfs_extlen_t extsz
, /* align to this extent size */
3132 int rt
, /* is this a realtime inode? */
3133 int eof
, /* is extent at end-of-file? */
3134 int delay
, /* creating delalloc extent? */
3135 int convert
, /* overwriting unwritten extent? */
3136 xfs_fileoff_t
*offp
, /* in/out: aligned offset */
3137 xfs_extlen_t
*lenp
) /* in/out: aligned length */
3139 xfs_fileoff_t orig_off
; /* original offset */
3140 xfs_extlen_t orig_alen
; /* original length */
3141 xfs_fileoff_t orig_end
; /* original off+len */
3142 xfs_fileoff_t nexto
; /* next file offset */
3143 xfs_fileoff_t prevo
; /* previous file offset */
3144 xfs_fileoff_t align_off
; /* temp for offset */
3145 xfs_extlen_t align_alen
; /* temp for length */
3146 xfs_extlen_t temp
; /* temp for calculations */
3151 orig_off
= align_off
= *offp
;
3152 orig_alen
= align_alen
= *lenp
;
3153 orig_end
= orig_off
+ orig_alen
;
3156 * If this request overlaps an existing extent, then don't
3157 * attempt to perform any additional alignment.
3159 if (!delay
&& !eof
&&
3160 (orig_off
>= gotp
->br_startoff
) &&
3161 (orig_end
<= gotp
->br_startoff
+ gotp
->br_blockcount
)) {
3166 * If the file offset is unaligned vs. the extent size
3167 * we need to align it. This will be possible unless
3168 * the file was previously written with a kernel that didn't
3169 * perform this alignment, or if a truncate shot us in the
3172 temp
= do_mod(orig_off
, extsz
);
3178 /* Same adjustment for the end of the requested area. */
3179 temp
= (align_alen
% extsz
);
3181 align_alen
+= extsz
- temp
;
3184 * For large extent hint sizes, the aligned extent might be larger than
3185 * MAXEXTLEN. In that case, reduce the size by an extsz so that it pulls
3186 * the length back under MAXEXTLEN. The outer allocation loops handle
3187 * short allocation just fine, so it is safe to do this. We only want to
3188 * do it when we are forced to, though, because it means more allocation
3189 * operations are required.
3191 while (align_alen
> MAXEXTLEN
)
3192 align_alen
-= extsz
;
3193 ASSERT(align_alen
<= MAXEXTLEN
);
3196 * If the previous block overlaps with this proposed allocation
3197 * then move the start forward without adjusting the length.
3199 if (prevp
->br_startoff
!= NULLFILEOFF
) {
3200 if (prevp
->br_startblock
== HOLESTARTBLOCK
)
3201 prevo
= prevp
->br_startoff
;
3203 prevo
= prevp
->br_startoff
+ prevp
->br_blockcount
;
3206 if (align_off
!= orig_off
&& align_off
< prevo
)
3209 * If the next block overlaps with this proposed allocation
3210 * then move the start back without adjusting the length,
3211 * but not before offset 0.
3212 * This may of course make the start overlap previous block,
3213 * and if we hit the offset 0 limit then the next block
3214 * can still overlap too.
3216 if (!eof
&& gotp
->br_startoff
!= NULLFILEOFF
) {
3217 if ((delay
&& gotp
->br_startblock
== HOLESTARTBLOCK
) ||
3218 (!delay
&& gotp
->br_startblock
== DELAYSTARTBLOCK
))
3219 nexto
= gotp
->br_startoff
+ gotp
->br_blockcount
;
3221 nexto
= gotp
->br_startoff
;
3223 nexto
= NULLFILEOFF
;
3225 align_off
+ align_alen
!= orig_end
&&
3226 align_off
+ align_alen
> nexto
)
3227 align_off
= nexto
> align_alen
? nexto
- align_alen
: 0;
3229 * If we're now overlapping the next or previous extent that
3230 * means we can't fit an extsz piece in this hole. Just move
3231 * the start forward to the first valid spot and set
3232 * the length so we hit the end.
3234 if (align_off
!= orig_off
&& align_off
< prevo
)
3236 if (align_off
+ align_alen
!= orig_end
&&
3237 align_off
+ align_alen
> nexto
&&
3238 nexto
!= NULLFILEOFF
) {
3239 ASSERT(nexto
> prevo
);
3240 align_alen
= nexto
- align_off
;
3244 * If realtime, and the result isn't a multiple of the realtime
3245 * extent size we need to remove blocks until it is.
3247 if (rt
&& (temp
= (align_alen
% mp
->m_sb
.sb_rextsize
))) {
3249 * We're not covering the original request, or
3250 * we won't be able to once we fix the length.
3252 if (orig_off
< align_off
||
3253 orig_end
> align_off
+ align_alen
||
3254 align_alen
- temp
< orig_alen
)
3257 * Try to fix it by moving the start up.
3259 if (align_off
+ temp
<= orig_off
) {
3264 * Try to fix it by moving the end in.
3266 else if (align_off
+ align_alen
- temp
>= orig_end
)
3269 * Set the start to the minimum then trim the length.
3272 align_alen
-= orig_off
- align_off
;
3273 align_off
= orig_off
;
3274 align_alen
-= align_alen
% mp
->m_sb
.sb_rextsize
;
3277 * Result doesn't cover the request, fail it.
3279 if (orig_off
< align_off
|| orig_end
> align_off
+ align_alen
)
3282 ASSERT(orig_off
>= align_off
);
3283 /* see MAXEXTLEN handling above */
3284 ASSERT(orig_end
<= align_off
+ align_alen
||
3285 align_alen
+ extsz
> MAXEXTLEN
);
3289 if (!eof
&& gotp
->br_startoff
!= NULLFILEOFF
)
3290 ASSERT(align_off
+ align_alen
<= gotp
->br_startoff
);
3291 if (prevp
->br_startoff
!= NULLFILEOFF
)
3292 ASSERT(align_off
>= prevp
->br_startoff
+ prevp
->br_blockcount
);
3300 #define XFS_ALLOC_GAP_UNITS 4
3304 struct xfs_bmalloca
*ap
) /* bmap alloc argument struct */
3306 xfs_fsblock_t adjust
; /* adjustment to block numbers */
3307 xfs_agnumber_t fb_agno
; /* ag number of ap->firstblock */
3308 xfs_mount_t
*mp
; /* mount point structure */
3309 int nullfb
; /* true if ap->firstblock isn't set */
3310 int rt
; /* true if inode is realtime */
3312 #define ISVALID(x,y) \
3314 (x) < mp->m_sb.sb_rblocks : \
3315 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
3316 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
3317 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
3319 mp
= ap
->ip
->i_mount
;
3320 nullfb
= *ap
->firstblock
== NULLFSBLOCK
;
3321 rt
= XFS_IS_REALTIME_INODE(ap
->ip
) &&
3322 xfs_alloc_is_userdata(ap
->datatype
);
3323 fb_agno
= nullfb
? NULLAGNUMBER
: XFS_FSB_TO_AGNO(mp
, *ap
->firstblock
);
3325 * If allocating at eof, and there's a previous real block,
3326 * try to use its last block as our starting point.
3328 if (ap
->eof
&& ap
->prev
.br_startoff
!= NULLFILEOFF
&&
3329 !isnullstartblock(ap
->prev
.br_startblock
) &&
3330 ISVALID(ap
->prev
.br_startblock
+ ap
->prev
.br_blockcount
,
3331 ap
->prev
.br_startblock
)) {
3332 ap
->blkno
= ap
->prev
.br_startblock
+ ap
->prev
.br_blockcount
;
3334 * Adjust for the gap between prevp and us.
3336 adjust
= ap
->offset
-
3337 (ap
->prev
.br_startoff
+ ap
->prev
.br_blockcount
);
3339 ISVALID(ap
->blkno
+ adjust
, ap
->prev
.br_startblock
))
3340 ap
->blkno
+= adjust
;
3343 * If not at eof, then compare the two neighbor blocks.
3344 * Figure out whether either one gives us a good starting point,
3345 * and pick the better one.
3347 else if (!ap
->eof
) {
3348 xfs_fsblock_t gotbno
; /* right side block number */
3349 xfs_fsblock_t gotdiff
=0; /* right side difference */
3350 xfs_fsblock_t prevbno
; /* left side block number */
3351 xfs_fsblock_t prevdiff
=0; /* left side difference */
3354 * If there's a previous (left) block, select a requested
3355 * start block based on it.
3357 if (ap
->prev
.br_startoff
!= NULLFILEOFF
&&
3358 !isnullstartblock(ap
->prev
.br_startblock
) &&
3359 (prevbno
= ap
->prev
.br_startblock
+
3360 ap
->prev
.br_blockcount
) &&
3361 ISVALID(prevbno
, ap
->prev
.br_startblock
)) {
3363 * Calculate gap to end of previous block.
3365 adjust
= prevdiff
= ap
->offset
-
3366 (ap
->prev
.br_startoff
+
3367 ap
->prev
.br_blockcount
);
3369 * Figure the startblock based on the previous block's
3370 * end and the gap size.
3372 * If the gap is large relative to the piece we're
3373 * allocating, or using it gives us an invalid block
3374 * number, then just use the end of the previous block.
3376 if (prevdiff
<= XFS_ALLOC_GAP_UNITS
* ap
->length
&&
3377 ISVALID(prevbno
+ prevdiff
,
3378 ap
->prev
.br_startblock
))
3383 * If the firstblock forbids it, can't use it,
3386 if (!rt
&& !nullfb
&&
3387 XFS_FSB_TO_AGNO(mp
, prevbno
) != fb_agno
)
3388 prevbno
= NULLFSBLOCK
;
3391 * No previous block or can't follow it, just default.
3394 prevbno
= NULLFSBLOCK
;
3396 * If there's a following (right) block, select a requested
3397 * start block based on it.
3399 if (!isnullstartblock(ap
->got
.br_startblock
)) {
3401 * Calculate gap to start of next block.
3403 adjust
= gotdiff
= ap
->got
.br_startoff
- ap
->offset
;
3405 * Figure the startblock based on the next block's
3406 * start and the gap size.
3408 gotbno
= ap
->got
.br_startblock
;
3411 * If the gap is large relative to the piece we're
3412 * allocating, or using it gives us an invalid block
3413 * number, then just use the start of the next block
3414 * offset by our length.
3416 if (gotdiff
<= XFS_ALLOC_GAP_UNITS
* ap
->length
&&
3417 ISVALID(gotbno
- gotdiff
, gotbno
))
3419 else if (ISVALID(gotbno
- ap
->length
, gotbno
)) {
3420 gotbno
-= ap
->length
;
3421 gotdiff
+= adjust
- ap
->length
;
3425 * If the firstblock forbids it, can't use it,
3428 if (!rt
&& !nullfb
&&
3429 XFS_FSB_TO_AGNO(mp
, gotbno
) != fb_agno
)
3430 gotbno
= NULLFSBLOCK
;
3433 * No next block, just default.
3436 gotbno
= NULLFSBLOCK
;
3438 * If both valid, pick the better one, else the only good
3439 * one, else ap->blkno is already set (to 0 or the inode block).
3441 if (prevbno
!= NULLFSBLOCK
&& gotbno
!= NULLFSBLOCK
)
3442 ap
->blkno
= prevdiff
<= gotdiff
? prevbno
: gotbno
;
3443 else if (prevbno
!= NULLFSBLOCK
)
3444 ap
->blkno
= prevbno
;
3445 else if (gotbno
!= NULLFSBLOCK
)
3452 xfs_bmap_longest_free_extent(
3453 struct xfs_trans
*tp
,
3458 struct xfs_mount
*mp
= tp
->t_mountp
;
3459 struct xfs_perag
*pag
;
3460 xfs_extlen_t longest
;
3463 pag
= xfs_perag_get(mp
, ag
);
3464 if (!pag
->pagf_init
) {
3465 error
= xfs_alloc_pagf_init(mp
, tp
, ag
, XFS_ALLOC_FLAG_TRYLOCK
);
3469 if (!pag
->pagf_init
) {
3475 longest
= xfs_alloc_longest_free_extent(mp
, pag
,
3476 xfs_alloc_min_freelist(mp
, pag
),
3477 xfs_ag_resv_needed(pag
, XFS_AG_RESV_NONE
));
3478 if (*blen
< longest
)
3487 xfs_bmap_select_minlen(
3488 struct xfs_bmalloca
*ap
,
3489 struct xfs_alloc_arg
*args
,
3493 if (notinit
|| *blen
< ap
->minlen
) {
3495 * Since we did a BUF_TRYLOCK above, it is possible that
3496 * there is space for this request.
3498 args
->minlen
= ap
->minlen
;
3499 } else if (*blen
< args
->maxlen
) {
3501 * If the best seen length is less than the request length,
3502 * use the best as the minimum.
3504 args
->minlen
= *blen
;
3507 * Otherwise we've seen an extent as big as maxlen, use that
3510 args
->minlen
= args
->maxlen
;
3515 xfs_bmap_btalloc_nullfb(
3516 struct xfs_bmalloca
*ap
,
3517 struct xfs_alloc_arg
*args
,
3520 struct xfs_mount
*mp
= ap
->ip
->i_mount
;
3521 xfs_agnumber_t ag
, startag
;
3525 args
->type
= XFS_ALLOCTYPE_START_BNO
;
3526 args
->total
= ap
->total
;
3528 startag
= ag
= XFS_FSB_TO_AGNO(mp
, args
->fsbno
);
3529 if (startag
== NULLAGNUMBER
)
3532 while (*blen
< args
->maxlen
) {
3533 error
= xfs_bmap_longest_free_extent(args
->tp
, ag
, blen
,
3538 if (++ag
== mp
->m_sb
.sb_agcount
)
3544 xfs_bmap_select_minlen(ap
, args
, blen
, notinit
);
3549 xfs_bmap_btalloc_filestreams(
3550 struct xfs_bmalloca
*ap
,
3551 struct xfs_alloc_arg
*args
,
3554 struct xfs_mount
*mp
= ap
->ip
->i_mount
;
3559 args
->type
= XFS_ALLOCTYPE_NEAR_BNO
;
3560 args
->total
= ap
->total
;
3562 ag
= XFS_FSB_TO_AGNO(mp
, args
->fsbno
);
3563 if (ag
== NULLAGNUMBER
)
3566 error
= xfs_bmap_longest_free_extent(args
->tp
, ag
, blen
, ¬init
);
3570 if (*blen
< args
->maxlen
) {
3571 error
= xfs_filestream_new_ag(ap
, &ag
);
3575 error
= xfs_bmap_longest_free_extent(args
->tp
, ag
, blen
,
3582 xfs_bmap_select_minlen(ap
, args
, blen
, notinit
);
3585 * Set the failure fallback case to look in the selected AG as stream
3588 ap
->blkno
= args
->fsbno
= XFS_AGB_TO_FSB(mp
, ag
, 0);
3594 struct xfs_bmalloca
*ap
) /* bmap alloc argument struct */
3596 xfs_mount_t
*mp
; /* mount point structure */
3597 xfs_alloctype_t atype
= 0; /* type for allocation routines */
3598 xfs_extlen_t align
= 0; /* minimum allocation alignment */
3599 xfs_agnumber_t fb_agno
; /* ag number of ap->firstblock */
3601 xfs_alloc_arg_t args
;
3603 xfs_extlen_t nextminlen
= 0;
3604 int nullfb
; /* true if ap->firstblock isn't set */
3612 mp
= ap
->ip
->i_mount
;
3614 /* stripe alignment for allocation is determined by mount parameters */
3616 if (mp
->m_swidth
&& (mp
->m_flags
& XFS_MOUNT_SWALLOC
))
3617 stripe_align
= mp
->m_swidth
;
3618 else if (mp
->m_dalign
)
3619 stripe_align
= mp
->m_dalign
;
3621 if (ap
->flags
& XFS_BMAPI_COWFORK
)
3622 align
= xfs_get_cowextsz_hint(ap
->ip
);
3623 else if (xfs_alloc_is_userdata(ap
->datatype
))
3624 align
= xfs_get_extsz_hint(ap
->ip
);
3626 error
= xfs_bmap_extsize_align(mp
, &ap
->got
, &ap
->prev
,
3627 align
, 0, ap
->eof
, 0, ap
->conv
,
3628 &ap
->offset
, &ap
->length
);
3634 nullfb
= *ap
->firstblock
== NULLFSBLOCK
;
3635 fb_agno
= nullfb
? NULLAGNUMBER
: XFS_FSB_TO_AGNO(mp
, *ap
->firstblock
);
3637 if (xfs_alloc_is_userdata(ap
->datatype
) &&
3638 xfs_inode_is_filestream(ap
->ip
)) {
3639 ag
= xfs_filestream_lookup_ag(ap
->ip
);
3640 ag
= (ag
!= NULLAGNUMBER
) ? ag
: 0;
3641 ap
->blkno
= XFS_AGB_TO_FSB(mp
, ag
, 0);
3643 ap
->blkno
= XFS_INO_TO_FSB(mp
, ap
->ip
->i_ino
);
3646 ap
->blkno
= *ap
->firstblock
;
3648 xfs_bmap_adjacent(ap
);
3651 * If allowed, use ap->blkno; otherwise must use firstblock since
3652 * it's in the right allocation group.
3654 if (nullfb
|| XFS_FSB_TO_AGNO(mp
, ap
->blkno
) == fb_agno
)
3657 ap
->blkno
= *ap
->firstblock
;
3659 * Normal allocation, done through xfs_alloc_vextent.
3661 tryagain
= isaligned
= 0;
3662 memset(&args
, 0, sizeof(args
));
3665 args
.fsbno
= ap
->blkno
;
3666 xfs_rmap_skip_owner_update(&args
.oinfo
);
3668 /* Trim the allocation back to the maximum an AG can fit. */
3669 args
.maxlen
= MIN(ap
->length
, mp
->m_ag_max_usable
);
3670 args
.firstblock
= *ap
->firstblock
;
3674 * Search for an allocation group with a single extent large
3675 * enough for the request. If one isn't found, then adjust
3676 * the minimum allocation size to the largest space found.
3678 if (xfs_alloc_is_userdata(ap
->datatype
) &&
3679 xfs_inode_is_filestream(ap
->ip
))
3680 error
= xfs_bmap_btalloc_filestreams(ap
, &args
, &blen
);
3682 error
= xfs_bmap_btalloc_nullfb(ap
, &args
, &blen
);
3685 } else if (ap
->dfops
->dop_low
) {
3686 if (xfs_inode_is_filestream(ap
->ip
))
3687 args
.type
= XFS_ALLOCTYPE_FIRST_AG
;
3689 args
.type
= XFS_ALLOCTYPE_START_BNO
;
3690 args
.total
= args
.minlen
= ap
->minlen
;
3692 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
3693 args
.total
= ap
->total
;
3694 args
.minlen
= ap
->minlen
;
3696 /* apply extent size hints if obtained earlier */
3699 if ((args
.mod
= (xfs_extlen_t
)do_mod(ap
->offset
, args
.prod
)))
3700 args
.mod
= (xfs_extlen_t
)(args
.prod
- args
.mod
);
3701 } else if (mp
->m_sb
.sb_blocksize
>= PAGE_SIZE
) {
3705 args
.prod
= PAGE_SIZE
>> mp
->m_sb
.sb_blocklog
;
3706 if ((args
.mod
= (xfs_extlen_t
)(do_mod(ap
->offset
, args
.prod
))))
3707 args
.mod
= (xfs_extlen_t
)(args
.prod
- args
.mod
);
3710 * If we are not low on available data blocks, and the
3711 * underlying logical volume manager is a stripe, and
3712 * the file offset is zero then try to allocate data
3713 * blocks on stripe unit boundary.
3714 * NOTE: ap->aeof is only set if the allocation length
3715 * is >= the stripe unit and the allocation offset is
3716 * at the end of file.
3718 if (!ap
->dfops
->dop_low
&& ap
->aeof
) {
3720 args
.alignment
= stripe_align
;
3724 * Adjust for alignment
3726 if (blen
> args
.alignment
&& blen
<= args
.maxlen
)
3727 args
.minlen
= blen
- args
.alignment
;
3728 args
.minalignslop
= 0;
3731 * First try an exact bno allocation.
3732 * If it fails then do a near or start bno
3733 * allocation with alignment turned on.
3737 args
.type
= XFS_ALLOCTYPE_THIS_BNO
;
3740 * Compute the minlen+alignment for the
3741 * next case. Set slop so that the value
3742 * of minlen+alignment+slop doesn't go up
3743 * between the calls.
3745 if (blen
> stripe_align
&& blen
<= args
.maxlen
)
3746 nextminlen
= blen
- stripe_align
;
3748 nextminlen
= args
.minlen
;
3749 if (nextminlen
+ stripe_align
> args
.minlen
+ 1)
3751 nextminlen
+ stripe_align
-
3754 args
.minalignslop
= 0;
3758 args
.minalignslop
= 0;
3760 args
.minleft
= ap
->minleft
;
3761 args
.wasdel
= ap
->wasdel
;
3762 args
.resv
= XFS_AG_RESV_NONE
;
3763 args
.datatype
= ap
->datatype
;
3764 if (ap
->datatype
& XFS_ALLOC_USERDATA_ZERO
)
3767 error
= xfs_alloc_vextent(&args
);
3771 if (tryagain
&& args
.fsbno
== NULLFSBLOCK
) {
3773 * Exact allocation failed. Now try with alignment
3777 args
.fsbno
= ap
->blkno
;
3778 args
.alignment
= stripe_align
;
3779 args
.minlen
= nextminlen
;
3780 args
.minalignslop
= 0;
3782 if ((error
= xfs_alloc_vextent(&args
)))
3785 if (isaligned
&& args
.fsbno
== NULLFSBLOCK
) {
3787 * allocation failed, so turn off alignment and
3791 args
.fsbno
= ap
->blkno
;
3793 if ((error
= xfs_alloc_vextent(&args
)))
3796 if (args
.fsbno
== NULLFSBLOCK
&& nullfb
&&
3797 args
.minlen
> ap
->minlen
) {
3798 args
.minlen
= ap
->minlen
;
3799 args
.type
= XFS_ALLOCTYPE_START_BNO
;
3800 args
.fsbno
= ap
->blkno
;
3801 if ((error
= xfs_alloc_vextent(&args
)))
3804 if (args
.fsbno
== NULLFSBLOCK
&& nullfb
) {
3806 args
.type
= XFS_ALLOCTYPE_FIRST_AG
;
3807 args
.total
= ap
->minlen
;
3808 if ((error
= xfs_alloc_vextent(&args
)))
3810 ap
->dfops
->dop_low
= true;
3812 if (args
.fsbno
!= NULLFSBLOCK
) {
3814 * check the allocation happened at the same or higher AG than
3815 * the first block that was allocated.
3817 ASSERT(*ap
->firstblock
== NULLFSBLOCK
||
3818 XFS_FSB_TO_AGNO(mp
, *ap
->firstblock
) <=
3819 XFS_FSB_TO_AGNO(mp
, args
.fsbno
));
3821 ap
->blkno
= args
.fsbno
;
3822 if (*ap
->firstblock
== NULLFSBLOCK
)
3823 *ap
->firstblock
= args
.fsbno
;
3824 ASSERT(nullfb
|| fb_agno
<= args
.agno
);
3825 ap
->length
= args
.len
;
3826 if (!(ap
->flags
& XFS_BMAPI_COWFORK
))
3827 ap
->ip
->i_d
.di_nblocks
+= args
.len
;
3828 xfs_trans_log_inode(ap
->tp
, ap
->ip
, XFS_ILOG_CORE
);
3830 ap
->ip
->i_delayed_blks
-= args
.len
;
3832 * Adjust the disk quota also. This was reserved
3835 xfs_trans_mod_dquot_byino(ap
->tp
, ap
->ip
,
3836 ap
->wasdel
? XFS_TRANS_DQ_DELBCOUNT
:
3837 XFS_TRANS_DQ_BCOUNT
,
3840 ap
->blkno
= NULLFSBLOCK
;
3847 * For a remap operation, just "allocate" an extent at the address that the
3848 * caller passed in, and ensure that the AGFL is the right size. The caller
3849 * will then map the "allocated" extent into the file somewhere.
3852 xfs_bmap_remap_alloc(
3853 struct xfs_bmalloca
*ap
)
3855 struct xfs_trans
*tp
= ap
->tp
;
3856 struct xfs_mount
*mp
= tp
->t_mountp
;
3858 struct xfs_alloc_arg args
;
3862 * validate that the block number is legal - the enables us to detect
3863 * and handle a silent filesystem corruption rather than crashing.
3865 memset(&args
, 0, sizeof(struct xfs_alloc_arg
));
3867 args
.mp
= ap
->tp
->t_mountp
;
3868 bno
= *ap
->firstblock
;
3869 args
.agno
= XFS_FSB_TO_AGNO(mp
, bno
);
3870 args
.agbno
= XFS_FSB_TO_AGBNO(mp
, bno
);
3871 if (args
.agno
>= mp
->m_sb
.sb_agcount
||
3872 args
.agbno
>= mp
->m_sb
.sb_agblocks
)
3873 return -EFSCORRUPTED
;
3875 /* "Allocate" the extent from the range we passed in. */
3876 trace_xfs_bmap_remap_alloc(ap
->ip
, *ap
->firstblock
, ap
->length
);
3878 ap
->ip
->i_d
.di_nblocks
+= ap
->length
;
3879 xfs_trans_log_inode(ap
->tp
, ap
->ip
, XFS_ILOG_CORE
);
3881 /* Fix the freelist, like a real allocator does. */
3882 args
.datatype
= ap
->datatype
;
3883 args
.pag
= xfs_perag_get(args
.mp
, args
.agno
);
3887 * The freelist fixing code will decline the allocation if
3888 * the size and shape of the free space doesn't allow for
3889 * allocating the extent and updating all the metadata that
3890 * happens during an allocation. We're remapping, not
3891 * allocating, so skip that check by pretending to be freeing.
3893 error
= xfs_alloc_fix_freelist(&args
, XFS_ALLOC_FLAG_FREEING
);
3894 xfs_perag_put(args
.pag
);
3896 trace_xfs_bmap_remap_alloc_error(ap
->ip
, error
, _RET_IP_
);
3901 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
3902 * It figures out where to ask the underlying allocator to put the new extent.
3906 struct xfs_bmalloca
*ap
) /* bmap alloc argument struct */
3908 if (ap
->flags
& XFS_BMAPI_REMAP
)
3909 return xfs_bmap_remap_alloc(ap
);
3910 if (XFS_IS_REALTIME_INODE(ap
->ip
) &&
3911 xfs_alloc_is_userdata(ap
->datatype
))
3912 return xfs_bmap_rtalloc(ap
);
3913 return xfs_bmap_btalloc(ap
);
3916 /* Trim extent to fit a logical block range. */
3919 struct xfs_bmbt_irec
*irec
,
3923 xfs_fileoff_t distance
;
3924 xfs_fileoff_t end
= bno
+ len
;
3926 if (irec
->br_startoff
+ irec
->br_blockcount
<= bno
||
3927 irec
->br_startoff
>= end
) {
3928 irec
->br_blockcount
= 0;
3932 if (irec
->br_startoff
< bno
) {
3933 distance
= bno
- irec
->br_startoff
;
3934 if (isnullstartblock(irec
->br_startblock
))
3935 irec
->br_startblock
= DELAYSTARTBLOCK
;
3936 if (irec
->br_startblock
!= DELAYSTARTBLOCK
&&
3937 irec
->br_startblock
!= HOLESTARTBLOCK
)
3938 irec
->br_startblock
+= distance
;
3939 irec
->br_startoff
+= distance
;
3940 irec
->br_blockcount
-= distance
;
3943 if (end
< irec
->br_startoff
+ irec
->br_blockcount
) {
3944 distance
= irec
->br_startoff
+ irec
->br_blockcount
- end
;
3945 irec
->br_blockcount
-= distance
;
3950 * Trim the returned map to the required bounds
3954 struct xfs_bmbt_irec
*mval
,
3955 struct xfs_bmbt_irec
*got
,
3963 if ((flags
& XFS_BMAPI_ENTIRE
) ||
3964 got
->br_startoff
+ got
->br_blockcount
<= obno
) {
3966 if (isnullstartblock(got
->br_startblock
))
3967 mval
->br_startblock
= DELAYSTARTBLOCK
;
3973 ASSERT((*bno
>= obno
) || (n
== 0));
3975 mval
->br_startoff
= *bno
;
3976 if (isnullstartblock(got
->br_startblock
))
3977 mval
->br_startblock
= DELAYSTARTBLOCK
;
3979 mval
->br_startblock
= got
->br_startblock
+
3980 (*bno
- got
->br_startoff
);
3982 * Return the minimum of what we got and what we asked for for
3983 * the length. We can use the len variable here because it is
3984 * modified below and we could have been there before coming
3985 * here if the first part of the allocation didn't overlap what
3988 mval
->br_blockcount
= XFS_FILBLKS_MIN(end
- *bno
,
3989 got
->br_blockcount
- (*bno
- got
->br_startoff
));
3990 mval
->br_state
= got
->br_state
;
3991 ASSERT(mval
->br_blockcount
<= len
);
3996 * Update and validate the extent map to return
3999 xfs_bmapi_update_map(
4000 struct xfs_bmbt_irec
**map
,
4008 xfs_bmbt_irec_t
*mval
= *map
;
4010 ASSERT((flags
& XFS_BMAPI_ENTIRE
) ||
4011 ((mval
->br_startoff
+ mval
->br_blockcount
) <= end
));
4012 ASSERT((flags
& XFS_BMAPI_ENTIRE
) || (mval
->br_blockcount
<= *len
) ||
4013 (mval
->br_startoff
< obno
));
4015 *bno
= mval
->br_startoff
+ mval
->br_blockcount
;
4017 if (*n
> 0 && mval
->br_startoff
== mval
[-1].br_startoff
) {
4018 /* update previous map with new information */
4019 ASSERT(mval
->br_startblock
== mval
[-1].br_startblock
);
4020 ASSERT(mval
->br_blockcount
> mval
[-1].br_blockcount
);
4021 ASSERT(mval
->br_state
== mval
[-1].br_state
);
4022 mval
[-1].br_blockcount
= mval
->br_blockcount
;
4023 mval
[-1].br_state
= mval
->br_state
;
4024 } else if (*n
> 0 && mval
->br_startblock
!= DELAYSTARTBLOCK
&&
4025 mval
[-1].br_startblock
!= DELAYSTARTBLOCK
&&
4026 mval
[-1].br_startblock
!= HOLESTARTBLOCK
&&
4027 mval
->br_startblock
== mval
[-1].br_startblock
+
4028 mval
[-1].br_blockcount
&&
4029 ((flags
& XFS_BMAPI_IGSTATE
) ||
4030 mval
[-1].br_state
== mval
->br_state
)) {
4031 ASSERT(mval
->br_startoff
==
4032 mval
[-1].br_startoff
+ mval
[-1].br_blockcount
);
4033 mval
[-1].br_blockcount
+= mval
->br_blockcount
;
4034 } else if (*n
> 0 &&
4035 mval
->br_startblock
== DELAYSTARTBLOCK
&&
4036 mval
[-1].br_startblock
== DELAYSTARTBLOCK
&&
4037 mval
->br_startoff
==
4038 mval
[-1].br_startoff
+ mval
[-1].br_blockcount
) {
4039 mval
[-1].br_blockcount
+= mval
->br_blockcount
;
4040 mval
[-1].br_state
= mval
->br_state
;
4041 } else if (!((*n
== 0) &&
4042 ((mval
->br_startoff
+ mval
->br_blockcount
) <=
4051 * Map file blocks to filesystem blocks without allocation.
4055 struct xfs_inode
*ip
,
4058 struct xfs_bmbt_irec
*mval
,
4062 struct xfs_mount
*mp
= ip
->i_mount
;
4063 struct xfs_ifork
*ifp
;
4064 struct xfs_bmbt_irec got
;
4071 int whichfork
= xfs_bmapi_whichfork(flags
);
4074 ASSERT(!(flags
& ~(XFS_BMAPI_ATTRFORK
|XFS_BMAPI_ENTIRE
|
4075 XFS_BMAPI_IGSTATE
|XFS_BMAPI_COWFORK
)));
4076 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_SHARED
|XFS_ILOCK_EXCL
));
4078 if (unlikely(XFS_TEST_ERROR(
4079 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
4080 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
4081 mp
, XFS_ERRTAG_BMAPIFORMAT
, XFS_RANDOM_BMAPIFORMAT
))) {
4082 XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW
, mp
);
4083 return -EFSCORRUPTED
;
4086 if (XFS_FORCED_SHUTDOWN(mp
))
4089 XFS_STATS_INC(mp
, xs_blk_mapr
);
4091 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4093 /* No CoW fork? Return a hole. */
4094 if (whichfork
== XFS_COW_FORK
&& !ifp
) {
4095 mval
->br_startoff
= bno
;
4096 mval
->br_startblock
= HOLESTARTBLOCK
;
4097 mval
->br_blockcount
= len
;
4098 mval
->br_state
= XFS_EXT_NORM
;
4103 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
4104 error
= xfs_iread_extents(NULL
, ip
, whichfork
);
4109 if (!xfs_iext_lookup_extent(ip
, ifp
, bno
, &idx
, &got
))
4114 while (bno
< end
&& n
< *nmap
) {
4115 /* Reading past eof, act as though there's a hole up to end. */
4117 got
.br_startoff
= end
;
4118 if (got
.br_startoff
> bno
) {
4119 /* Reading in a hole. */
4120 mval
->br_startoff
= bno
;
4121 mval
->br_startblock
= HOLESTARTBLOCK
;
4122 mval
->br_blockcount
=
4123 XFS_FILBLKS_MIN(len
, got
.br_startoff
- bno
);
4124 mval
->br_state
= XFS_EXT_NORM
;
4125 bno
+= mval
->br_blockcount
;
4126 len
-= mval
->br_blockcount
;
4132 /* set up the extent map to return. */
4133 xfs_bmapi_trim_map(mval
, &got
, &bno
, len
, obno
, end
, n
, flags
);
4134 xfs_bmapi_update_map(&mval
, &bno
, &len
, obno
, end
, &n
, flags
);
4136 /* If we're done, stop now. */
4137 if (bno
>= end
|| n
>= *nmap
)
4140 /* Else go on to the next record. */
4141 if (!xfs_iext_get_extent(ifp
, ++idx
, &got
))
4149 * Add a delayed allocation extent to an inode. Blocks are reserved from the
4150 * global pool and the extent inserted into the inode in-core extent tree.
4152 * On entry, got refers to the first extent beyond the offset of the extent to
4153 * allocate or eof is specified if no such extent exists. On return, got refers
4154 * to the extent record that was inserted to the inode fork.
4156 * Note that the allocated extent may have been merged with contiguous extents
4157 * during insertion into the inode fork. Thus, got does not reflect the current
4158 * state of the inode fork on return. If necessary, the caller can use lastx to
4159 * look up the updated record in the inode fork.
4162 xfs_bmapi_reserve_delalloc(
4163 struct xfs_inode
*ip
,
4167 xfs_filblks_t prealloc
,
4168 struct xfs_bmbt_irec
*got
,
4169 xfs_extnum_t
*lastx
,
4172 struct xfs_mount
*mp
= ip
->i_mount
;
4173 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4175 xfs_extlen_t indlen
;
4176 char rt
= XFS_IS_REALTIME_INODE(ip
);
4179 xfs_fileoff_t aoff
= off
;
4182 * Cap the alloc length. Keep track of prealloc so we know whether to
4183 * tag the inode before we return.
4185 alen
= XFS_FILBLKS_MIN(len
+ prealloc
, MAXEXTLEN
);
4187 alen
= XFS_FILBLKS_MIN(alen
, got
->br_startoff
- aoff
);
4188 if (prealloc
&& alen
>= len
)
4189 prealloc
= alen
- len
;
4191 /* Figure out the extent size, adjust alen */
4192 if (whichfork
== XFS_COW_FORK
)
4193 extsz
= xfs_get_cowextsz_hint(ip
);
4195 extsz
= xfs_get_extsz_hint(ip
);
4197 struct xfs_bmbt_irec prev
;
4199 if (!xfs_iext_get_extent(ifp
, *lastx
- 1, &prev
))
4200 prev
.br_startoff
= NULLFILEOFF
;
4202 error
= xfs_bmap_extsize_align(mp
, got
, &prev
, extsz
, rt
, eof
,
4203 1, 0, &aoff
, &alen
);
4208 extsz
= alen
/ mp
->m_sb
.sb_rextsize
;
4211 * Make a transaction-less quota reservation for delayed allocation
4212 * blocks. This number gets adjusted later. We return if we haven't
4213 * allocated blocks already inside this loop.
4215 error
= xfs_trans_reserve_quota_nblks(NULL
, ip
, (long)alen
, 0,
4216 rt
? XFS_QMOPT_RES_RTBLKS
: XFS_QMOPT_RES_REGBLKS
);
4221 * Split changing sb for alen and indlen since they could be coming
4222 * from different places.
4224 indlen
= (xfs_extlen_t
)xfs_bmap_worst_indlen(ip
, alen
);
4228 error
= xfs_mod_frextents(mp
, -((int64_t)extsz
));
4230 error
= xfs_mod_fdblocks(mp
, -((int64_t)alen
), false);
4234 goto out_unreserve_quota
;
4236 error
= xfs_mod_fdblocks(mp
, -((int64_t)indlen
), false);
4238 goto out_unreserve_blocks
;
4241 ip
->i_delayed_blks
+= alen
;
4243 got
->br_startoff
= aoff
;
4244 got
->br_startblock
= nullstartblock(indlen
);
4245 got
->br_blockcount
= alen
;
4246 got
->br_state
= XFS_EXT_NORM
;
4248 xfs_bmap_add_extent_hole_delay(ip
, whichfork
, lastx
, got
);
4251 * Tag the inode if blocks were preallocated. Note that COW fork
4252 * preallocation can occur at the start or end of the extent, even when
4253 * prealloc == 0, so we must also check the aligned offset and length.
4255 if (whichfork
== XFS_DATA_FORK
&& prealloc
)
4256 xfs_inode_set_eofblocks_tag(ip
);
4257 if (whichfork
== XFS_COW_FORK
&& (prealloc
|| aoff
< off
|| alen
> len
))
4258 xfs_inode_set_cowblocks_tag(ip
);
4262 out_unreserve_blocks
:
4264 xfs_mod_frextents(mp
, extsz
);
4266 xfs_mod_fdblocks(mp
, alen
, false);
4267 out_unreserve_quota
:
4268 if (XFS_IS_QUOTA_ON(mp
))
4269 xfs_trans_unreserve_quota_nblks(NULL
, ip
, (long)alen
, 0, rt
?
4270 XFS_QMOPT_RES_RTBLKS
: XFS_QMOPT_RES_REGBLKS
);
4276 struct xfs_bmalloca
*bma
)
4278 struct xfs_mount
*mp
= bma
->ip
->i_mount
;
4279 int whichfork
= xfs_bmapi_whichfork(bma
->flags
);
4280 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
4281 int tmp_logflags
= 0;
4284 ASSERT(bma
->length
> 0);
4287 * For the wasdelay case, we could also just allocate the stuff asked
4288 * for in this bmap call but that wouldn't be as good.
4291 bma
->length
= (xfs_extlen_t
)bma
->got
.br_blockcount
;
4292 bma
->offset
= bma
->got
.br_startoff
;
4294 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
- 1),
4298 bma
->length
= XFS_FILBLKS_MIN(bma
->length
, MAXEXTLEN
);
4300 bma
->length
= XFS_FILBLKS_MIN(bma
->length
,
4301 bma
->got
.br_startoff
- bma
->offset
);
4305 * Set the data type being allocated. For the data fork, the first data
4306 * in the file is treated differently to all other allocations. For the
4307 * attribute fork, we only need to ensure the allocated range is not on
4310 if (!(bma
->flags
& XFS_BMAPI_METADATA
)) {
4311 bma
->datatype
= XFS_ALLOC_NOBUSY
;
4312 if (whichfork
== XFS_DATA_FORK
) {
4313 if (bma
->offset
== 0)
4314 bma
->datatype
|= XFS_ALLOC_INITIAL_USER_DATA
;
4316 bma
->datatype
|= XFS_ALLOC_USERDATA
;
4318 if (bma
->flags
& XFS_BMAPI_ZERO
)
4319 bma
->datatype
|= XFS_ALLOC_USERDATA_ZERO
;
4322 bma
->minlen
= (bma
->flags
& XFS_BMAPI_CONTIG
) ? bma
->length
: 1;
4325 * Only want to do the alignment at the eof if it is userdata and
4326 * allocation length is larger than a stripe unit.
4328 if (mp
->m_dalign
&& bma
->length
>= mp
->m_dalign
&&
4329 !(bma
->flags
& XFS_BMAPI_METADATA
) && whichfork
== XFS_DATA_FORK
) {
4330 error
= xfs_bmap_isaeof(bma
, whichfork
);
4335 error
= xfs_bmap_alloc(bma
);
4340 bma
->cur
->bc_private
.b
.firstblock
= *bma
->firstblock
;
4341 if (bma
->blkno
== NULLFSBLOCK
)
4343 if ((ifp
->if_flags
& XFS_IFBROOT
) && !bma
->cur
) {
4344 bma
->cur
= xfs_bmbt_init_cursor(mp
, bma
->tp
, bma
->ip
, whichfork
);
4345 bma
->cur
->bc_private
.b
.firstblock
= *bma
->firstblock
;
4346 bma
->cur
->bc_private
.b
.dfops
= bma
->dfops
;
4349 * Bump the number of extents we've allocated
4355 bma
->cur
->bc_private
.b
.flags
=
4356 bma
->wasdel
? XFS_BTCUR_BPRV_WASDEL
: 0;
4358 bma
->got
.br_startoff
= bma
->offset
;
4359 bma
->got
.br_startblock
= bma
->blkno
;
4360 bma
->got
.br_blockcount
= bma
->length
;
4361 bma
->got
.br_state
= XFS_EXT_NORM
;
4364 * In the data fork, a wasdelay extent has been initialized, so
4365 * shouldn't be flagged as unwritten.
4367 * For the cow fork, however, we convert delalloc reservations
4368 * (extents allocated for speculative preallocation) to
4369 * allocated unwritten extents, and only convert the unwritten
4370 * extents to real extents when we're about to write the data.
4372 if ((!bma
->wasdel
|| (bma
->flags
& XFS_BMAPI_COWFORK
)) &&
4373 (bma
->flags
& XFS_BMAPI_PREALLOC
) &&
4374 xfs_sb_version_hasextflgbit(&mp
->m_sb
))
4375 bma
->got
.br_state
= XFS_EXT_UNWRITTEN
;
4378 error
= xfs_bmap_add_extent_delay_real(bma
, whichfork
);
4380 error
= xfs_bmap_add_extent_hole_real(bma
, whichfork
);
4382 bma
->logflags
|= tmp_logflags
;
4387 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4388 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4389 * the neighbouring ones.
4391 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
), &bma
->got
);
4393 ASSERT(bma
->got
.br_startoff
<= bma
->offset
);
4394 ASSERT(bma
->got
.br_startoff
+ bma
->got
.br_blockcount
>=
4395 bma
->offset
+ bma
->length
);
4396 ASSERT(bma
->got
.br_state
== XFS_EXT_NORM
||
4397 bma
->got
.br_state
== XFS_EXT_UNWRITTEN
);
4402 xfs_bmapi_convert_unwritten(
4403 struct xfs_bmalloca
*bma
,
4404 struct xfs_bmbt_irec
*mval
,
4408 int whichfork
= xfs_bmapi_whichfork(flags
);
4409 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
4410 int tmp_logflags
= 0;
4413 /* check if we need to do unwritten->real conversion */
4414 if (mval
->br_state
== XFS_EXT_UNWRITTEN
&&
4415 (flags
& XFS_BMAPI_PREALLOC
))
4418 /* check if we need to do real->unwritten conversion */
4419 if (mval
->br_state
== XFS_EXT_NORM
&&
4420 (flags
& (XFS_BMAPI_PREALLOC
| XFS_BMAPI_CONVERT
)) !=
4421 (XFS_BMAPI_PREALLOC
| XFS_BMAPI_CONVERT
))
4425 * Modify (by adding) the state flag, if writing.
4427 ASSERT(mval
->br_blockcount
<= len
);
4428 if ((ifp
->if_flags
& XFS_IFBROOT
) && !bma
->cur
) {
4429 bma
->cur
= xfs_bmbt_init_cursor(bma
->ip
->i_mount
, bma
->tp
,
4430 bma
->ip
, whichfork
);
4431 bma
->cur
->bc_private
.b
.firstblock
= *bma
->firstblock
;
4432 bma
->cur
->bc_private
.b
.dfops
= bma
->dfops
;
4434 mval
->br_state
= (mval
->br_state
== XFS_EXT_UNWRITTEN
)
4435 ? XFS_EXT_NORM
: XFS_EXT_UNWRITTEN
;
4438 * Before insertion into the bmbt, zero the range being converted
4441 if (flags
& XFS_BMAPI_ZERO
) {
4442 error
= xfs_zero_extent(bma
->ip
, mval
->br_startblock
,
4443 mval
->br_blockcount
);
4448 error
= xfs_bmap_add_extent_unwritten_real(bma
->tp
, bma
->ip
, whichfork
,
4449 &bma
->idx
, &bma
->cur
, mval
, bma
->firstblock
, bma
->dfops
,
4452 * Log the inode core unconditionally in the unwritten extent conversion
4453 * path because the conversion might not have done so (e.g., if the
4454 * extent count hasn't changed). We need to make sure the inode is dirty
4455 * in the transaction for the sake of fsync(), even if nothing has
4456 * changed, because fsync() will not force the log for this transaction
4457 * unless it sees the inode pinned.
4459 * Note: If we're only converting cow fork extents, there aren't
4460 * any on-disk updates to make, so we don't need to log anything.
4462 if (whichfork
!= XFS_COW_FORK
)
4463 bma
->logflags
|= tmp_logflags
| XFS_ILOG_CORE
;
4468 * Update our extent pointer, given that
4469 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4470 * of the neighbouring ones.
4472 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
), &bma
->got
);
4475 * We may have combined previously unwritten space with written space,
4476 * so generate another request.
4478 if (mval
->br_blockcount
< len
)
4484 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4485 * extent state if necessary. Details behaviour is controlled by the flags
4486 * parameter. Only allocates blocks from a single allocation group, to avoid
4489 * The returned value in "firstblock" from the first call in a transaction
4490 * must be remembered and presented to subsequent calls in "firstblock".
4491 * An upper bound for the number of blocks to be allocated is supplied to
4492 * the first call in "total"; if no allocation group has that many free
4493 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4497 struct xfs_trans
*tp
, /* transaction pointer */
4498 struct xfs_inode
*ip
, /* incore inode */
4499 xfs_fileoff_t bno
, /* starting file offs. mapped */
4500 xfs_filblks_t len
, /* length to map in file */
4501 int flags
, /* XFS_BMAPI_... */
4502 xfs_fsblock_t
*firstblock
, /* first allocated block
4503 controls a.g. for allocs */
4504 xfs_extlen_t total
, /* total blocks needed */
4505 struct xfs_bmbt_irec
*mval
, /* output: map values */
4506 int *nmap
, /* i/o: mval size/count */
4507 struct xfs_defer_ops
*dfops
) /* i/o: list extents to free */
4509 struct xfs_mount
*mp
= ip
->i_mount
;
4510 struct xfs_ifork
*ifp
;
4511 struct xfs_bmalloca bma
= { NULL
}; /* args for xfs_bmap_alloc */
4512 xfs_fileoff_t end
; /* end of mapped file region */
4513 bool eof
= false; /* after the end of extents */
4514 int error
; /* error return */
4515 int n
; /* current extent index */
4516 xfs_fileoff_t obno
; /* old block number (offset) */
4517 int whichfork
; /* data or attr fork */
4520 xfs_fileoff_t orig_bno
; /* original block number value */
4521 int orig_flags
; /* original flags arg value */
4522 xfs_filblks_t orig_len
; /* original value of len arg */
4523 struct xfs_bmbt_irec
*orig_mval
; /* original value of mval */
4524 int orig_nmap
; /* original value of *nmap */
4532 whichfork
= xfs_bmapi_whichfork(flags
);
4535 ASSERT(*nmap
<= XFS_BMAP_MAX_NMAP
);
4536 ASSERT(!(flags
& XFS_BMAPI_IGSTATE
));
4537 ASSERT(tp
!= NULL
||
4538 (flags
& (XFS_BMAPI_CONVERT
| XFS_BMAPI_COWFORK
)) ==
4539 (XFS_BMAPI_CONVERT
| XFS_BMAPI_COWFORK
));
4541 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_LOCAL
);
4542 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
4543 ASSERT(!(flags
& XFS_BMAPI_REMAP
) || whichfork
== XFS_DATA_FORK
);
4544 ASSERT(!(flags
& XFS_BMAPI_PREALLOC
) || !(flags
& XFS_BMAPI_REMAP
));
4545 ASSERT(!(flags
& XFS_BMAPI_CONVERT
) || !(flags
& XFS_BMAPI_REMAP
));
4547 /* zeroing is for currently only for data extents, not metadata */
4548 ASSERT((flags
& (XFS_BMAPI_METADATA
| XFS_BMAPI_ZERO
)) !=
4549 (XFS_BMAPI_METADATA
| XFS_BMAPI_ZERO
));
4551 * we can allocate unwritten extents or pre-zero allocated blocks,
4552 * but it makes no sense to do both at once. This would result in
4553 * zeroing the unwritten extent twice, but it still being an
4554 * unwritten extent....
4556 ASSERT((flags
& (XFS_BMAPI_PREALLOC
| XFS_BMAPI_ZERO
)) !=
4557 (XFS_BMAPI_PREALLOC
| XFS_BMAPI_ZERO
));
4559 if (unlikely(XFS_TEST_ERROR(
4560 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
4561 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
4562 mp
, XFS_ERRTAG_BMAPIFORMAT
, XFS_RANDOM_BMAPIFORMAT
))) {
4563 XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW
, mp
);
4564 return -EFSCORRUPTED
;
4567 if (XFS_FORCED_SHUTDOWN(mp
))
4570 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4572 XFS_STATS_INC(mp
, xs_blk_mapw
);
4574 if (*firstblock
== NULLFSBLOCK
) {
4575 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
)
4576 bma
.minleft
= be16_to_cpu(ifp
->if_broot
->bb_level
) + 1;
4583 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
4584 error
= xfs_iread_extents(tp
, ip
, whichfork
);
4593 if (!xfs_iext_lookup_extent(ip
, ifp
, bno
, &bma
.idx
, &bma
.got
))
4595 if (!xfs_iext_get_extent(ifp
, bma
.idx
- 1, &bma
.prev
))
4596 bma
.prev
.br_startoff
= NULLFILEOFF
;
4602 bma
.firstblock
= firstblock
;
4604 while (bno
< end
&& n
< *nmap
) {
4605 bool need_alloc
= false, wasdelay
= false;
4607 /* in hole or beyoned EOF? */
4608 if (eof
|| bma
.got
.br_startoff
> bno
) {
4609 if (flags
& XFS_BMAPI_DELALLOC
) {
4611 * For the COW fork we can reasonably get a
4612 * request for converting an extent that races
4613 * with other threads already having converted
4614 * part of it, as there converting COW to
4615 * regular blocks is not protected using the
4618 ASSERT(flags
& XFS_BMAPI_COWFORK
);
4619 if (!(flags
& XFS_BMAPI_COWFORK
)) {
4624 if (eof
|| bno
>= end
)
4631 * Make sure we only reflink into a hole.
4633 ASSERT(!(flags
& XFS_BMAPI_REMAP
));
4634 if (isnullstartblock(bma
.got
.br_startblock
))
4639 * First, deal with the hole before the allocated space
4640 * that we found, if any.
4642 if (need_alloc
|| wasdelay
) {
4644 bma
.conv
= !!(flags
& XFS_BMAPI_CONVERT
);
4645 bma
.wasdel
= wasdelay
;
4650 * There's a 32/64 bit type mismatch between the
4651 * allocation length request (which can be 64 bits in
4652 * length) and the bma length request, which is
4653 * xfs_extlen_t and therefore 32 bits. Hence we have to
4654 * check for 32-bit overflows and handle them here.
4656 if (len
> (xfs_filblks_t
)MAXEXTLEN
)
4657 bma
.length
= MAXEXTLEN
;
4662 ASSERT(bma
.length
> 0);
4663 error
= xfs_bmapi_allocate(&bma
);
4666 if (bma
.blkno
== NULLFSBLOCK
)
4670 * If this is a CoW allocation, record the data in
4671 * the refcount btree for orphan recovery.
4673 if (whichfork
== XFS_COW_FORK
) {
4674 error
= xfs_refcount_alloc_cow_extent(mp
, dfops
,
4675 bma
.blkno
, bma
.length
);
4681 /* Deal with the allocated space we found. */
4682 xfs_bmapi_trim_map(mval
, &bma
.got
, &bno
, len
, obno
,
4685 /* Execute unwritten extent conversion if necessary */
4686 error
= xfs_bmapi_convert_unwritten(&bma
, mval
, len
, flags
);
4687 if (error
== -EAGAIN
)
4692 /* update the extent map to return */
4693 xfs_bmapi_update_map(&mval
, &bno
, &len
, obno
, end
, &n
, flags
);
4696 * If we're done, stop now. Stop when we've allocated
4697 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4698 * the transaction may get too big.
4700 if (bno
>= end
|| n
>= *nmap
|| bma
.nallocs
>= *nmap
)
4703 /* Else go on to the next record. */
4705 if (!xfs_iext_get_extent(ifp
, ++bma
.idx
, &bma
.got
))
4711 * Transform from btree to extents, give it cur.
4713 if (xfs_bmap_wants_extents(ip
, whichfork
)) {
4714 int tmp_logflags
= 0;
4717 error
= xfs_bmap_btree_to_extents(tp
, ip
, bma
.cur
,
4718 &tmp_logflags
, whichfork
);
4719 bma
.logflags
|= tmp_logflags
;
4724 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
||
4725 XFS_IFORK_NEXTENTS(ip
, whichfork
) >
4726 XFS_IFORK_MAXEXT(ip
, whichfork
));
4730 * Log everything. Do this after conversion, there's no point in
4731 * logging the extent records if we've converted to btree format.
4733 if ((bma
.logflags
& xfs_ilog_fext(whichfork
)) &&
4734 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
4735 bma
.logflags
&= ~xfs_ilog_fext(whichfork
);
4736 else if ((bma
.logflags
& xfs_ilog_fbroot(whichfork
)) &&
4737 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)
4738 bma
.logflags
&= ~xfs_ilog_fbroot(whichfork
);
4740 * Log whatever the flags say, even if error. Otherwise we might miss
4741 * detecting a case where the data is changed, there's an error,
4742 * and it's not logged so we don't shutdown when we should.
4745 xfs_trans_log_inode(tp
, ip
, bma
.logflags
);
4749 ASSERT(*firstblock
== NULLFSBLOCK
||
4750 XFS_FSB_TO_AGNO(mp
, *firstblock
) <=
4752 bma
.cur
->bc_private
.b
.firstblock
));
4753 *firstblock
= bma
.cur
->bc_private
.b
.firstblock
;
4755 xfs_btree_del_cursor(bma
.cur
,
4756 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
4759 xfs_bmap_validate_ret(orig_bno
, orig_len
, orig_flags
, orig_mval
,
4765 * When a delalloc extent is split (e.g., due to a hole punch), the original
4766 * indlen reservation must be shared across the two new extents that are left
4769 * Given the original reservation and the worst case indlen for the two new
4770 * extents (as calculated by xfs_bmap_worst_indlen()), split the original
4771 * reservation fairly across the two new extents. If necessary, steal available
4772 * blocks from a deleted extent to make up a reservation deficiency (e.g., if
4773 * ores == 1). The number of stolen blocks is returned. The availability and
4774 * subsequent accounting of stolen blocks is the responsibility of the caller.
4776 static xfs_filblks_t
4777 xfs_bmap_split_indlen(
4778 xfs_filblks_t ores
, /* original res. */
4779 xfs_filblks_t
*indlen1
, /* ext1 worst indlen */
4780 xfs_filblks_t
*indlen2
, /* ext2 worst indlen */
4781 xfs_filblks_t avail
) /* stealable blocks */
4783 xfs_filblks_t len1
= *indlen1
;
4784 xfs_filblks_t len2
= *indlen2
;
4785 xfs_filblks_t nres
= len1
+ len2
; /* new total res. */
4786 xfs_filblks_t stolen
= 0;
4787 xfs_filblks_t resfactor
;
4790 * Steal as many blocks as we can to try and satisfy the worst case
4791 * indlen for both new extents.
4793 if (ores
< nres
&& avail
)
4794 stolen
= XFS_FILBLKS_MIN(nres
- ores
, avail
);
4797 /* nothing else to do if we've satisfied the new reservation */
4802 * We can't meet the total required reservation for the two extents.
4803 * Calculate the percent of the overall shortage between both extents
4804 * and apply this percentage to each of the requested indlen values.
4805 * This distributes the shortage fairly and reduces the chances that one
4806 * of the two extents is left with nothing when extents are repeatedly
4809 resfactor
= (ores
* 100);
4810 do_div(resfactor
, nres
);
4815 ASSERT(len1
+ len2
<= ores
);
4816 ASSERT(len1
< *indlen1
&& len2
< *indlen2
);
4819 * Hand out the remainder to each extent. If one of the two reservations
4820 * is zero, we want to make sure that one gets a block first. The loop
4821 * below starts with len1, so hand len2 a block right off the bat if it
4824 ores
-= (len1
+ len2
);
4825 ASSERT((*indlen1
- len1
) + (*indlen2
- len2
) >= ores
);
4826 if (ores
&& !len2
&& *indlen2
) {
4831 if (len1
< *indlen1
) {
4837 if (len2
< *indlen2
) {
4850 xfs_bmap_del_extent_delay(
4851 struct xfs_inode
*ip
,
4854 struct xfs_bmbt_irec
*got
,
4855 struct xfs_bmbt_irec
*del
)
4857 struct xfs_mount
*mp
= ip
->i_mount
;
4858 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4859 struct xfs_bmbt_irec
new;
4860 int64_t da_old
, da_new
, da_diff
= 0;
4861 xfs_fileoff_t del_endoff
, got_endoff
;
4862 xfs_filblks_t got_indlen
, new_indlen
, stolen
;
4863 int error
= 0, state
= 0;
4866 XFS_STATS_INC(mp
, xs_del_exlist
);
4868 isrt
= (whichfork
== XFS_DATA_FORK
) && XFS_IS_REALTIME_INODE(ip
);
4869 del_endoff
= del
->br_startoff
+ del
->br_blockcount
;
4870 got_endoff
= got
->br_startoff
+ got
->br_blockcount
;
4871 da_old
= startblockval(got
->br_startblock
);
4875 ASSERT(*idx
<= xfs_iext_count(ifp
));
4876 ASSERT(del
->br_blockcount
> 0);
4877 ASSERT(got
->br_startoff
<= del
->br_startoff
);
4878 ASSERT(got_endoff
>= del_endoff
);
4881 int64_t rtexts
= XFS_FSB_TO_B(mp
, del
->br_blockcount
);
4883 do_div(rtexts
, mp
->m_sb
.sb_rextsize
);
4884 xfs_mod_frextents(mp
, rtexts
);
4888 * Update the inode delalloc counter now and wait to update the
4889 * sb counters as we might have to borrow some blocks for the
4890 * indirect block accounting.
4892 error
= xfs_trans_reserve_quota_nblks(NULL
, ip
,
4893 -((long)del
->br_blockcount
), 0,
4894 isrt
? XFS_QMOPT_RES_RTBLKS
: XFS_QMOPT_RES_REGBLKS
);
4897 ip
->i_delayed_blks
-= del
->br_blockcount
;
4899 if (whichfork
== XFS_COW_FORK
)
4900 state
|= BMAP_COWFORK
;
4902 if (got
->br_startoff
== del
->br_startoff
)
4903 state
|= BMAP_LEFT_CONTIG
;
4904 if (got_endoff
== del_endoff
)
4905 state
|= BMAP_RIGHT_CONTIG
;
4907 switch (state
& (BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
)) {
4908 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
4910 * Matches the whole extent. Delete the entry.
4912 xfs_iext_remove(ip
, *idx
, 1, state
);
4915 case BMAP_LEFT_CONTIG
:
4917 * Deleting the first part of the extent.
4919 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
4920 got
->br_startoff
= del_endoff
;
4921 got
->br_blockcount
-= del
->br_blockcount
;
4922 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
,
4923 got
->br_blockcount
), da_old
);
4924 got
->br_startblock
= nullstartblock((int)da_new
);
4925 xfs_bmbt_set_all(xfs_iext_get_ext(ifp
, *idx
), got
);
4926 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
4928 case BMAP_RIGHT_CONTIG
:
4930 * Deleting the last part of the extent.
4932 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
4933 got
->br_blockcount
= got
->br_blockcount
- del
->br_blockcount
;
4934 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
,
4935 got
->br_blockcount
), da_old
);
4936 got
->br_startblock
= nullstartblock((int)da_new
);
4937 xfs_bmbt_set_all(xfs_iext_get_ext(ifp
, *idx
), got
);
4938 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
4942 * Deleting the middle of the extent.
4944 * Distribute the original indlen reservation across the two new
4945 * extents. Steal blocks from the deleted extent if necessary.
4946 * Stealing blocks simply fudges the fdblocks accounting below.
4947 * Warn if either of the new indlen reservations is zero as this
4948 * can lead to delalloc problems.
4950 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
4952 got
->br_blockcount
= del
->br_startoff
- got
->br_startoff
;
4953 got_indlen
= xfs_bmap_worst_indlen(ip
, got
->br_blockcount
);
4955 new.br_blockcount
= got_endoff
- del_endoff
;
4956 new_indlen
= xfs_bmap_worst_indlen(ip
, new.br_blockcount
);
4958 WARN_ON_ONCE(!got_indlen
|| !new_indlen
);
4959 stolen
= xfs_bmap_split_indlen(da_old
, &got_indlen
, &new_indlen
,
4960 del
->br_blockcount
);
4962 got
->br_startblock
= nullstartblock((int)got_indlen
);
4963 xfs_bmbt_set_all(xfs_iext_get_ext(ifp
, *idx
), got
);
4964 trace_xfs_bmap_post_update(ip
, *idx
, 0, _THIS_IP_
);
4966 new.br_startoff
= del_endoff
;
4967 new.br_state
= got
->br_state
;
4968 new.br_startblock
= nullstartblock((int)new_indlen
);
4971 xfs_iext_insert(ip
, *idx
, 1, &new, state
);
4973 da_new
= got_indlen
+ new_indlen
- stolen
;
4974 del
->br_blockcount
-= stolen
;
4978 ASSERT(da_old
>= da_new
);
4979 da_diff
= da_old
- da_new
;
4981 da_diff
+= del
->br_blockcount
;
4983 xfs_mod_fdblocks(mp
, da_diff
, false);
4988 xfs_bmap_del_extent_cow(
4989 struct xfs_inode
*ip
,
4991 struct xfs_bmbt_irec
*got
,
4992 struct xfs_bmbt_irec
*del
)
4994 struct xfs_mount
*mp
= ip
->i_mount
;
4995 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, XFS_COW_FORK
);
4996 struct xfs_bmbt_irec
new;
4997 xfs_fileoff_t del_endoff
, got_endoff
;
4998 int state
= BMAP_COWFORK
;
5000 XFS_STATS_INC(mp
, xs_del_exlist
);
5002 del_endoff
= del
->br_startoff
+ del
->br_blockcount
;
5003 got_endoff
= got
->br_startoff
+ got
->br_blockcount
;
5006 ASSERT(*idx
<= xfs_iext_count(ifp
));
5007 ASSERT(del
->br_blockcount
> 0);
5008 ASSERT(got
->br_startoff
<= del
->br_startoff
);
5009 ASSERT(got_endoff
>= del_endoff
);
5010 ASSERT(!isnullstartblock(got
->br_startblock
));
5012 if (got
->br_startoff
== del
->br_startoff
)
5013 state
|= BMAP_LEFT_CONTIG
;
5014 if (got_endoff
== del_endoff
)
5015 state
|= BMAP_RIGHT_CONTIG
;
5017 switch (state
& (BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
)) {
5018 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
5020 * Matches the whole extent. Delete the entry.
5022 xfs_iext_remove(ip
, *idx
, 1, state
);
5025 case BMAP_LEFT_CONTIG
:
5027 * Deleting the first part of the extent.
5029 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
5030 got
->br_startoff
= del_endoff
;
5031 got
->br_blockcount
-= del
->br_blockcount
;
5032 got
->br_startblock
= del
->br_startblock
+ del
->br_blockcount
;
5033 xfs_bmbt_set_all(xfs_iext_get_ext(ifp
, *idx
), got
);
5034 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
5036 case BMAP_RIGHT_CONTIG
:
5038 * Deleting the last part of the extent.
5040 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
5041 got
->br_blockcount
-= del
->br_blockcount
;
5042 xfs_bmbt_set_all(xfs_iext_get_ext(ifp
, *idx
), got
);
5043 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
5047 * Deleting the middle of the extent.
5049 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
5050 got
->br_blockcount
= del
->br_startoff
- got
->br_startoff
;
5051 xfs_bmbt_set_all(xfs_iext_get_ext(ifp
, *idx
), got
);
5052 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
5054 new.br_startoff
= del_endoff
;
5055 new.br_blockcount
= got_endoff
- del_endoff
;
5056 new.br_state
= got
->br_state
;
5057 new.br_startblock
= del
->br_startblock
+ del
->br_blockcount
;
5060 xfs_iext_insert(ip
, *idx
, 1, &new, state
);
5066 * Called by xfs_bmapi to update file extent records and the btree
5067 * after removing space (or undoing a delayed allocation).
5069 STATIC
int /* error */
5070 xfs_bmap_del_extent(
5071 xfs_inode_t
*ip
, /* incore inode pointer */
5072 xfs_trans_t
*tp
, /* current transaction pointer */
5073 xfs_extnum_t
*idx
, /* extent number to update/delete */
5074 struct xfs_defer_ops
*dfops
, /* list of extents to be freed */
5075 xfs_btree_cur_t
*cur
, /* if null, not a btree */
5076 xfs_bmbt_irec_t
*del
, /* data to remove from extents */
5077 int *logflagsp
, /* inode logging flags */
5078 int whichfork
, /* data or attr fork */
5079 int bflags
) /* bmapi flags */
5081 xfs_filblks_t da_new
; /* new delay-alloc indirect blocks */
5082 xfs_filblks_t da_old
; /* old delay-alloc indirect blocks */
5083 xfs_fsblock_t del_endblock
=0; /* first block past del */
5084 xfs_fileoff_t del_endoff
; /* first offset past del */
5085 int delay
; /* current block is delayed allocated */
5086 int do_fx
; /* free extent at end of routine */
5087 xfs_bmbt_rec_host_t
*ep
; /* current extent entry pointer */
5088 int error
; /* error return value */
5089 int flags
; /* inode logging flags */
5090 xfs_bmbt_irec_t got
; /* current extent entry */
5091 xfs_fileoff_t got_endoff
; /* first offset past got */
5092 int i
; /* temp state */
5093 xfs_ifork_t
*ifp
; /* inode fork pointer */
5094 xfs_mount_t
*mp
; /* mount structure */
5095 xfs_filblks_t nblks
; /* quota/sb block count */
5096 xfs_bmbt_irec_t
new; /* new record to be inserted */
5098 uint qfield
; /* quota field to update */
5099 xfs_filblks_t temp
; /* for indirect length calculations */
5100 xfs_filblks_t temp2
; /* for indirect length calculations */
5104 XFS_STATS_INC(mp
, xs_del_exlist
);
5106 if (whichfork
== XFS_ATTR_FORK
)
5107 state
|= BMAP_ATTRFORK
;
5108 else if (whichfork
== XFS_COW_FORK
)
5109 state
|= BMAP_COWFORK
;
5111 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5112 ASSERT((*idx
>= 0) && (*idx
< xfs_iext_count(ifp
)));
5113 ASSERT(del
->br_blockcount
> 0);
5114 ep
= xfs_iext_get_ext(ifp
, *idx
);
5115 xfs_bmbt_get_all(ep
, &got
);
5116 ASSERT(got
.br_startoff
<= del
->br_startoff
);
5117 del_endoff
= del
->br_startoff
+ del
->br_blockcount
;
5118 got_endoff
= got
.br_startoff
+ got
.br_blockcount
;
5119 ASSERT(got_endoff
>= del_endoff
);
5120 delay
= isnullstartblock(got
.br_startblock
);
5121 ASSERT(isnullstartblock(del
->br_startblock
) == delay
);
5126 * If deleting a real allocation, must free up the disk space.
5129 flags
= XFS_ILOG_CORE
;
5131 * Realtime allocation. Free it and record di_nblocks update.
5133 if (whichfork
== XFS_DATA_FORK
&& XFS_IS_REALTIME_INODE(ip
)) {
5137 ASSERT(do_mod(del
->br_blockcount
,
5138 mp
->m_sb
.sb_rextsize
) == 0);
5139 ASSERT(do_mod(del
->br_startblock
,
5140 mp
->m_sb
.sb_rextsize
) == 0);
5141 bno
= del
->br_startblock
;
5142 len
= del
->br_blockcount
;
5143 do_div(bno
, mp
->m_sb
.sb_rextsize
);
5144 do_div(len
, mp
->m_sb
.sb_rextsize
);
5145 error
= xfs_rtfree_extent(tp
, bno
, (xfs_extlen_t
)len
);
5149 nblks
= len
* mp
->m_sb
.sb_rextsize
;
5150 qfield
= XFS_TRANS_DQ_RTBCOUNT
;
5153 * Ordinary allocation.
5157 nblks
= del
->br_blockcount
;
5158 qfield
= XFS_TRANS_DQ_BCOUNT
;
5161 * Set up del_endblock and cur for later.
5163 del_endblock
= del
->br_startblock
+ del
->br_blockcount
;
5165 if ((error
= xfs_bmbt_lookup_eq(cur
, got
.br_startoff
,
5166 got
.br_startblock
, got
.br_blockcount
,
5169 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
5171 da_old
= da_new
= 0;
5173 da_old
= startblockval(got
.br_startblock
);
5180 * Set flag value to use in switch statement.
5181 * Left-contig is 2, right-contig is 1.
5183 switch (((got
.br_startoff
== del
->br_startoff
) << 1) |
5184 (got_endoff
== del_endoff
)) {
5187 * Matches the whole extent. Delete the entry.
5189 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
5190 xfs_iext_remove(ip
, *idx
, 1,
5191 whichfork
== XFS_ATTR_FORK
? BMAP_ATTRFORK
: 0);
5196 XFS_IFORK_NEXT_SET(ip
, whichfork
,
5197 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
5198 flags
|= XFS_ILOG_CORE
;
5200 flags
|= xfs_ilog_fext(whichfork
);
5203 if ((error
= xfs_btree_delete(cur
, &i
)))
5205 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
5210 * Deleting the first part of the extent.
5212 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
5213 xfs_bmbt_set_startoff(ep
, del_endoff
);
5214 temp
= got
.br_blockcount
- del
->br_blockcount
;
5215 xfs_bmbt_set_blockcount(ep
, temp
);
5217 temp
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
5219 xfs_bmbt_set_startblock(ep
, nullstartblock((int)temp
));
5220 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
5224 xfs_bmbt_set_startblock(ep
, del_endblock
);
5225 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
5227 flags
|= xfs_ilog_fext(whichfork
);
5230 if ((error
= xfs_bmbt_update(cur
, del_endoff
, del_endblock
,
5231 got
.br_blockcount
- del
->br_blockcount
,
5238 * Deleting the last part of the extent.
5240 temp
= got
.br_blockcount
- del
->br_blockcount
;
5241 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
5242 xfs_bmbt_set_blockcount(ep
, temp
);
5244 temp
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
5246 xfs_bmbt_set_startblock(ep
, nullstartblock((int)temp
));
5247 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
5251 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
5253 flags
|= xfs_ilog_fext(whichfork
);
5256 if ((error
= xfs_bmbt_update(cur
, got
.br_startoff
,
5258 got
.br_blockcount
- del
->br_blockcount
,
5265 * Deleting the middle of the extent.
5267 temp
= del
->br_startoff
- got
.br_startoff
;
5268 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
5269 xfs_bmbt_set_blockcount(ep
, temp
);
5270 new.br_startoff
= del_endoff
;
5271 temp2
= got_endoff
- del_endoff
;
5272 new.br_blockcount
= temp2
;
5273 new.br_state
= got
.br_state
;
5275 new.br_startblock
= del_endblock
;
5276 flags
|= XFS_ILOG_CORE
;
5278 if ((error
= xfs_bmbt_update(cur
,
5280 got
.br_startblock
, temp
,
5283 if ((error
= xfs_btree_increment(cur
, 0, &i
)))
5285 cur
->bc_rec
.b
= new;
5286 error
= xfs_btree_insert(cur
, &i
);
5287 if (error
&& error
!= -ENOSPC
)
5290 * If get no-space back from btree insert,
5291 * it tried a split, and we have a zero
5292 * block reservation.
5293 * Fix up our state and return the error.
5295 if (error
== -ENOSPC
) {
5297 * Reset the cursor, don't trust
5298 * it after any insert operation.
5300 if ((error
= xfs_bmbt_lookup_eq(cur
,
5305 XFS_WANT_CORRUPTED_GOTO(mp
,
5308 * Update the btree record back
5309 * to the original value.
5311 if ((error
= xfs_bmbt_update(cur
,
5318 * Reset the extent record back
5319 * to the original value.
5321 xfs_bmbt_set_blockcount(ep
,
5327 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
5329 flags
|= xfs_ilog_fext(whichfork
);
5330 XFS_IFORK_NEXT_SET(ip
, whichfork
,
5331 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
5333 xfs_filblks_t stolen
;
5334 ASSERT(whichfork
== XFS_DATA_FORK
);
5337 * Distribute the original indlen reservation across the
5338 * two new extents. Steal blocks from the deleted extent
5339 * if necessary. Stealing blocks simply fudges the
5340 * fdblocks accounting in xfs_bunmapi().
5342 temp
= xfs_bmap_worst_indlen(ip
, got
.br_blockcount
);
5343 temp2
= xfs_bmap_worst_indlen(ip
, new.br_blockcount
);
5344 stolen
= xfs_bmap_split_indlen(da_old
, &temp
, &temp2
,
5345 del
->br_blockcount
);
5346 da_new
= temp
+ temp2
- stolen
;
5347 del
->br_blockcount
-= stolen
;
5350 * Set the reservation for each extent. Warn if either
5351 * is zero as this can lead to delalloc problems.
5353 WARN_ON_ONCE(!temp
|| !temp2
);
5354 xfs_bmbt_set_startblock(ep
, nullstartblock((int)temp
));
5355 new.br_startblock
= nullstartblock((int)temp2
);
5357 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
5358 xfs_iext_insert(ip
, *idx
+ 1, 1, &new, state
);
5363 /* remove reverse mapping */
5365 error
= xfs_rmap_unmap_extent(mp
, dfops
, ip
, whichfork
, del
);
5371 * If we need to, add to list of extents to delete.
5373 if (do_fx
&& !(bflags
& XFS_BMAPI_REMAP
)) {
5374 if (xfs_is_reflink_inode(ip
) && whichfork
== XFS_DATA_FORK
) {
5375 error
= xfs_refcount_decrease_extent(mp
, dfops
, del
);
5379 xfs_bmap_add_free(mp
, dfops
, del
->br_startblock
,
5380 del
->br_blockcount
, NULL
);
5384 * Adjust inode # blocks in the file.
5387 ip
->i_d
.di_nblocks
-= nblks
;
5389 * Adjust quota data.
5391 if (qfield
&& !(bflags
& XFS_BMAPI_REMAP
))
5392 xfs_trans_mod_dquot_byino(tp
, ip
, qfield
, (long)-nblks
);
5395 * Account for change in delayed indirect blocks.
5396 * Nothing to do for disk quota accounting here.
5398 ASSERT(da_old
>= da_new
);
5399 if (da_old
> da_new
)
5400 xfs_mod_fdblocks(mp
, (int64_t)(da_old
- da_new
), false);
5407 * Unmap (remove) blocks from a file.
5408 * If nexts is nonzero then the number of extents to remove is limited to
5409 * that value. If not all extents in the block range can be removed then
5414 xfs_trans_t
*tp
, /* transaction pointer */
5415 struct xfs_inode
*ip
, /* incore inode */
5416 xfs_fileoff_t bno
, /* starting offset to unmap */
5417 xfs_filblks_t
*rlen
, /* i/o: amount remaining */
5418 int flags
, /* misc flags */
5419 xfs_extnum_t nexts
, /* number of extents max */
5420 xfs_fsblock_t
*firstblock
, /* first allocated block
5421 controls a.g. for allocs */
5422 struct xfs_defer_ops
*dfops
) /* i/o: deferred updates */
5424 xfs_btree_cur_t
*cur
; /* bmap btree cursor */
5425 xfs_bmbt_irec_t del
; /* extent being deleted */
5426 int error
; /* error return value */
5427 xfs_extnum_t extno
; /* extent number in list */
5428 xfs_bmbt_irec_t got
; /* current extent record */
5429 xfs_ifork_t
*ifp
; /* inode fork pointer */
5430 int isrt
; /* freeing in rt area */
5431 xfs_extnum_t lastx
; /* last extent index used */
5432 int logflags
; /* transaction logging flags */
5433 xfs_extlen_t mod
; /* rt extent offset */
5434 xfs_mount_t
*mp
; /* mount structure */
5435 xfs_fileoff_t start
; /* first file offset deleted */
5436 int tmp_logflags
; /* partial logging flags */
5437 int wasdel
; /* was a delayed alloc extent */
5438 int whichfork
; /* data or attribute fork */
5440 xfs_filblks_t len
= *rlen
; /* length to unmap in file */
5442 trace_xfs_bunmap(ip
, bno
, len
, flags
, _RET_IP_
);
5444 whichfork
= xfs_bmapi_whichfork(flags
);
5445 ASSERT(whichfork
!= XFS_COW_FORK
);
5446 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5448 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
5449 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)) {
5450 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW
,
5452 return -EFSCORRUPTED
;
5455 if (XFS_FORCED_SHUTDOWN(mp
))
5458 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
5462 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
5463 (error
= xfs_iread_extents(tp
, ip
, whichfork
)))
5465 if (xfs_iext_count(ifp
) == 0) {
5469 XFS_STATS_INC(mp
, xs_blk_unmap
);
5470 isrt
= (whichfork
== XFS_DATA_FORK
) && XFS_IS_REALTIME_INODE(ip
);
5472 bno
= start
+ len
- 1;
5475 * Check to see if the given block number is past the end of the
5476 * file, back up to the last block if so...
5478 if (!xfs_iext_lookup_extent(ip
, ifp
, bno
, &lastx
, &got
)) {
5480 xfs_iext_get_extent(ifp
, --lastx
, &got
);
5481 bno
= got
.br_startoff
+ got
.br_blockcount
- 1;
5485 if (ifp
->if_flags
& XFS_IFBROOT
) {
5486 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
);
5487 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
5488 cur
->bc_private
.b
.firstblock
= *firstblock
;
5489 cur
->bc_private
.b
.dfops
= dfops
;
5490 cur
->bc_private
.b
.flags
= 0;
5496 * Synchronize by locking the bitmap inode.
5498 xfs_ilock(mp
->m_rbmip
, XFS_ILOCK_EXCL
|XFS_ILOCK_RTBITMAP
);
5499 xfs_trans_ijoin(tp
, mp
->m_rbmip
, XFS_ILOCK_EXCL
);
5500 xfs_ilock(mp
->m_rsumip
, XFS_ILOCK_EXCL
|XFS_ILOCK_RTSUM
);
5501 xfs_trans_ijoin(tp
, mp
->m_rsumip
, XFS_ILOCK_EXCL
);
5505 while (bno
!= (xfs_fileoff_t
)-1 && bno
>= start
&& lastx
>= 0 &&
5506 (nexts
== 0 || extno
< nexts
)) {
5508 * Is the found extent after a hole in which bno lives?
5509 * Just back up to the previous extent, if so.
5511 if (got
.br_startoff
> bno
) {
5514 xfs_iext_get_extent(ifp
, lastx
, &got
);
5517 * Is the last block of this extent before the range
5518 * we're supposed to delete? If so, we're done.
5520 bno
= XFS_FILEOFF_MIN(bno
,
5521 got
.br_startoff
+ got
.br_blockcount
- 1);
5525 * Then deal with the (possibly delayed) allocated space
5529 wasdel
= isnullstartblock(del
.br_startblock
);
5530 if (got
.br_startoff
< start
) {
5531 del
.br_startoff
= start
;
5532 del
.br_blockcount
-= start
- got
.br_startoff
;
5534 del
.br_startblock
+= start
- got
.br_startoff
;
5536 if (del
.br_startoff
+ del
.br_blockcount
> bno
+ 1)
5537 del
.br_blockcount
= bno
+ 1 - del
.br_startoff
;
5538 sum
= del
.br_startblock
+ del
.br_blockcount
;
5540 (mod
= do_mod(sum
, mp
->m_sb
.sb_rextsize
))) {
5542 * Realtime extent not lined up at the end.
5543 * The extent could have been split into written
5544 * and unwritten pieces, or we could just be
5545 * unmapping part of it. But we can't really
5546 * get rid of part of a realtime extent.
5548 if (del
.br_state
== XFS_EXT_UNWRITTEN
||
5549 !xfs_sb_version_hasextflgbit(&mp
->m_sb
)) {
5551 * This piece is unwritten, or we're not
5552 * using unwritten extents. Skip over it.
5555 bno
-= mod
> del
.br_blockcount
?
5556 del
.br_blockcount
: mod
;
5557 if (bno
< got
.br_startoff
) {
5559 xfs_bmbt_get_all(xfs_iext_get_ext(
5565 * It's written, turn it unwritten.
5566 * This is better than zeroing it.
5568 ASSERT(del
.br_state
== XFS_EXT_NORM
);
5569 ASSERT(tp
->t_blk_res
> 0);
5571 * If this spans a realtime extent boundary,
5572 * chop it back to the start of the one we end at.
5574 if (del
.br_blockcount
> mod
) {
5575 del
.br_startoff
+= del
.br_blockcount
- mod
;
5576 del
.br_startblock
+= del
.br_blockcount
- mod
;
5577 del
.br_blockcount
= mod
;
5579 del
.br_state
= XFS_EXT_UNWRITTEN
;
5580 error
= xfs_bmap_add_extent_unwritten_real(tp
, ip
,
5581 whichfork
, &lastx
, &cur
, &del
,
5582 firstblock
, dfops
, &logflags
);
5587 if (isrt
&& (mod
= do_mod(del
.br_startblock
, mp
->m_sb
.sb_rextsize
))) {
5589 * Realtime extent is lined up at the end but not
5590 * at the front. We'll get rid of full extents if
5593 mod
= mp
->m_sb
.sb_rextsize
- mod
;
5594 if (del
.br_blockcount
> mod
) {
5595 del
.br_blockcount
-= mod
;
5596 del
.br_startoff
+= mod
;
5597 del
.br_startblock
+= mod
;
5598 } else if ((del
.br_startoff
== start
&&
5599 (del
.br_state
== XFS_EXT_UNWRITTEN
||
5600 tp
->t_blk_res
== 0)) ||
5601 !xfs_sb_version_hasextflgbit(&mp
->m_sb
)) {
5603 * Can't make it unwritten. There isn't
5604 * a full extent here so just skip it.
5606 ASSERT(bno
>= del
.br_blockcount
);
5607 bno
-= del
.br_blockcount
;
5608 if (got
.br_startoff
> bno
&& --lastx
>= 0)
5609 xfs_iext_get_extent(ifp
, lastx
, &got
);
5611 } else if (del
.br_state
== XFS_EXT_UNWRITTEN
) {
5612 struct xfs_bmbt_irec prev
;
5615 * This one is already unwritten.
5616 * It must have a written left neighbor.
5617 * Unwrite the killed part of that one and
5621 xfs_iext_get_extent(ifp
, lastx
- 1, &prev
);
5622 ASSERT(prev
.br_state
== XFS_EXT_NORM
);
5623 ASSERT(!isnullstartblock(prev
.br_startblock
));
5624 ASSERT(del
.br_startblock
==
5625 prev
.br_startblock
+ prev
.br_blockcount
);
5626 if (prev
.br_startoff
< start
) {
5627 mod
= start
- prev
.br_startoff
;
5628 prev
.br_blockcount
-= mod
;
5629 prev
.br_startblock
+= mod
;
5630 prev
.br_startoff
= start
;
5632 prev
.br_state
= XFS_EXT_UNWRITTEN
;
5634 error
= xfs_bmap_add_extent_unwritten_real(tp
,
5635 ip
, whichfork
, &lastx
, &cur
,
5636 &prev
, firstblock
, dfops
,
5642 ASSERT(del
.br_state
== XFS_EXT_NORM
);
5643 del
.br_state
= XFS_EXT_UNWRITTEN
;
5644 error
= xfs_bmap_add_extent_unwritten_real(tp
,
5645 ip
, whichfork
, &lastx
, &cur
,
5646 &del
, firstblock
, dfops
,
5655 * If it's the case where the directory code is running
5656 * with no block reservation, and the deleted block is in
5657 * the middle of its extent, and the resulting insert
5658 * of an extent would cause transformation to btree format,
5659 * then reject it. The calling code will then swap
5660 * blocks around instead.
5661 * We have to do this now, rather than waiting for the
5662 * conversion to btree format, since the transaction
5665 if (!wasdel
&& tp
->t_blk_res
== 0 &&
5666 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
&&
5667 XFS_IFORK_NEXTENTS(ip
, whichfork
) >= /* Note the >= */
5668 XFS_IFORK_MAXEXT(ip
, whichfork
) &&
5669 del
.br_startoff
> got
.br_startoff
&&
5670 del
.br_startoff
+ del
.br_blockcount
<
5671 got
.br_startoff
+ got
.br_blockcount
) {
5677 * Unreserve quota and update realtime free space, if
5678 * appropriate. If delayed allocation, update the inode delalloc
5679 * counter now and wait to update the sb counters as
5680 * xfs_bmap_del_extent() might need to borrow some blocks.
5683 ASSERT(startblockval(del
.br_startblock
) > 0);
5685 xfs_filblks_t rtexts
;
5687 rtexts
= XFS_FSB_TO_B(mp
, del
.br_blockcount
);
5688 do_div(rtexts
, mp
->m_sb
.sb_rextsize
);
5689 xfs_mod_frextents(mp
, (int64_t)rtexts
);
5690 (void)xfs_trans_reserve_quota_nblks(NULL
,
5691 ip
, -((long)del
.br_blockcount
), 0,
5692 XFS_QMOPT_RES_RTBLKS
);
5694 (void)xfs_trans_reserve_quota_nblks(NULL
,
5695 ip
, -((long)del
.br_blockcount
), 0,
5696 XFS_QMOPT_RES_REGBLKS
);
5698 ip
->i_delayed_blks
-= del
.br_blockcount
;
5700 cur
->bc_private
.b
.flags
|=
5701 XFS_BTCUR_BPRV_WASDEL
;
5703 cur
->bc_private
.b
.flags
&= ~XFS_BTCUR_BPRV_WASDEL
;
5705 error
= xfs_bmap_del_extent(ip
, tp
, &lastx
, dfops
, cur
, &del
,
5706 &tmp_logflags
, whichfork
, flags
);
5707 logflags
|= tmp_logflags
;
5711 if (!isrt
&& wasdel
)
5712 xfs_mod_fdblocks(mp
, (int64_t)del
.br_blockcount
, false);
5714 bno
= del
.br_startoff
- 1;
5717 * If not done go on to the next (previous) record.
5719 if (bno
!= (xfs_fileoff_t
)-1 && bno
>= start
) {
5721 xfs_iext_get_extent(ifp
, lastx
, &got
);
5722 if (got
.br_startoff
> bno
&& --lastx
>= 0)
5723 xfs_iext_get_extent(ifp
, lastx
, &got
);
5728 if (bno
== (xfs_fileoff_t
)-1 || bno
< start
|| lastx
< 0)
5731 *rlen
= bno
- start
+ 1;
5734 * Convert to a btree if necessary.
5736 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
5737 ASSERT(cur
== NULL
);
5738 error
= xfs_bmap_extents_to_btree(tp
, ip
, firstblock
, dfops
,
5739 &cur
, 0, &tmp_logflags
, whichfork
);
5740 logflags
|= tmp_logflags
;
5745 * transform from btree to extents, give it cur
5747 else if (xfs_bmap_wants_extents(ip
, whichfork
)) {
5748 ASSERT(cur
!= NULL
);
5749 error
= xfs_bmap_btree_to_extents(tp
, ip
, cur
, &tmp_logflags
,
5751 logflags
|= tmp_logflags
;
5756 * transform from extents to local?
5761 * Log everything. Do this after conversion, there's no point in
5762 * logging the extent records if we've converted to btree format.
5764 if ((logflags
& xfs_ilog_fext(whichfork
)) &&
5765 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
5766 logflags
&= ~xfs_ilog_fext(whichfork
);
5767 else if ((logflags
& xfs_ilog_fbroot(whichfork
)) &&
5768 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)
5769 logflags
&= ~xfs_ilog_fbroot(whichfork
);
5771 * Log inode even in the error case, if the transaction
5772 * is dirty we'll need to shut down the filesystem.
5775 xfs_trans_log_inode(tp
, ip
, logflags
);
5778 *firstblock
= cur
->bc_private
.b
.firstblock
;
5779 cur
->bc_private
.b
.allocated
= 0;
5781 xfs_btree_del_cursor(cur
,
5782 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
5787 /* Unmap a range of a file. */
5791 struct xfs_inode
*ip
,
5796 xfs_fsblock_t
*firstblock
,
5797 struct xfs_defer_ops
*dfops
,
5802 error
= __xfs_bunmapi(tp
, ip
, bno
, &len
, flags
, nexts
, firstblock
,
5809 * Determine whether an extent shift can be accomplished by a merge with the
5810 * extent that precedes the target hole of the shift.
5814 struct xfs_bmbt_irec
*left
, /* preceding extent */
5815 struct xfs_bmbt_irec
*got
, /* current extent to shift */
5816 xfs_fileoff_t shift
) /* shift fsb */
5818 xfs_fileoff_t startoff
;
5820 startoff
= got
->br_startoff
- shift
;
5823 * The extent, once shifted, must be adjacent in-file and on-disk with
5824 * the preceding extent.
5826 if ((left
->br_startoff
+ left
->br_blockcount
!= startoff
) ||
5827 (left
->br_startblock
+ left
->br_blockcount
!= got
->br_startblock
) ||
5828 (left
->br_state
!= got
->br_state
) ||
5829 (left
->br_blockcount
+ got
->br_blockcount
> MAXEXTLEN
))
5836 * A bmap extent shift adjusts the file offset of an extent to fill a preceding
5837 * hole in the file. If an extent shift would result in the extent being fully
5838 * adjacent to the extent that currently precedes the hole, we can merge with
5839 * the preceding extent rather than do the shift.
5841 * This function assumes the caller has verified a shift-by-merge is possible
5842 * with the provided extents via xfs_bmse_can_merge().
5846 struct xfs_inode
*ip
,
5848 xfs_fileoff_t shift
, /* shift fsb */
5849 int current_ext
, /* idx of gotp */
5850 struct xfs_bmbt_rec_host
*gotp
, /* extent to shift */
5851 struct xfs_bmbt_rec_host
*leftp
, /* preceding extent */
5852 struct xfs_btree_cur
*cur
,
5853 int *logflags
) /* output */
5855 struct xfs_bmbt_irec got
;
5856 struct xfs_bmbt_irec left
;
5857 xfs_filblks_t blockcount
;
5859 struct xfs_mount
*mp
= ip
->i_mount
;
5861 xfs_bmbt_get_all(gotp
, &got
);
5862 xfs_bmbt_get_all(leftp
, &left
);
5863 blockcount
= left
.br_blockcount
+ got
.br_blockcount
;
5865 ASSERT(xfs_isilocked(ip
, XFS_IOLOCK_EXCL
));
5866 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
5867 ASSERT(xfs_bmse_can_merge(&left
, &got
, shift
));
5870 * Merge the in-core extents. Note that the host record pointers and
5871 * current_ext index are invalid once the extent has been removed via
5872 * xfs_iext_remove().
5874 xfs_bmbt_set_blockcount(leftp
, blockcount
);
5875 xfs_iext_remove(ip
, current_ext
, 1, 0);
5878 * Update the on-disk extent count, the btree if necessary and log the
5881 XFS_IFORK_NEXT_SET(ip
, whichfork
,
5882 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
5883 *logflags
|= XFS_ILOG_CORE
;
5885 *logflags
|= XFS_ILOG_DEXT
;
5889 /* lookup and remove the extent to merge */
5890 error
= xfs_bmbt_lookup_eq(cur
, got
.br_startoff
, got
.br_startblock
,
5891 got
.br_blockcount
, &i
);
5894 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
5896 error
= xfs_btree_delete(cur
, &i
);
5899 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
5901 /* lookup and update size of the previous extent */
5902 error
= xfs_bmbt_lookup_eq(cur
, left
.br_startoff
, left
.br_startblock
,
5903 left
.br_blockcount
, &i
);
5906 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
5908 left
.br_blockcount
= blockcount
;
5910 return xfs_bmbt_update(cur
, left
.br_startoff
, left
.br_startblock
,
5911 left
.br_blockcount
, left
.br_state
);
5915 * Shift a single extent.
5919 struct xfs_inode
*ip
,
5921 xfs_fileoff_t offset_shift_fsb
,
5923 struct xfs_bmbt_rec_host
*gotp
,
5924 struct xfs_btree_cur
*cur
,
5926 enum shift_direction direction
,
5927 struct xfs_defer_ops
*dfops
)
5929 struct xfs_ifork
*ifp
;
5930 struct xfs_mount
*mp
;
5931 xfs_fileoff_t startoff
;
5932 struct xfs_bmbt_rec_host
*adj_irecp
;
5933 struct xfs_bmbt_irec got
;
5934 struct xfs_bmbt_irec adj_irec
;
5940 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5941 total_extents
= xfs_iext_count(ifp
);
5943 xfs_bmbt_get_all(gotp
, &got
);
5945 /* delalloc extents should be prevented by caller */
5946 XFS_WANT_CORRUPTED_RETURN(mp
, !isnullstartblock(got
.br_startblock
));
5948 if (direction
== SHIFT_LEFT
) {
5949 startoff
= got
.br_startoff
- offset_shift_fsb
;
5952 * Check for merge if we've got an extent to the left,
5953 * otherwise make sure there's enough room at the start
5954 * of the file for the shift.
5956 if (!*current_ext
) {
5957 if (got
.br_startoff
< offset_shift_fsb
)
5959 goto update_current_ext
;
5962 * grab the left extent and check for a large
5965 adj_irecp
= xfs_iext_get_ext(ifp
, *current_ext
- 1);
5966 xfs_bmbt_get_all(adj_irecp
, &adj_irec
);
5969 adj_irec
.br_startoff
+ adj_irec
.br_blockcount
)
5972 /* check whether to merge the extent or shift it down */
5973 if (xfs_bmse_can_merge(&adj_irec
, &got
,
5974 offset_shift_fsb
)) {
5975 error
= xfs_bmse_merge(ip
, whichfork
, offset_shift_fsb
,
5976 *current_ext
, gotp
, adj_irecp
,
5984 startoff
= got
.br_startoff
+ offset_shift_fsb
;
5985 /* nothing to move if this is the last extent */
5986 if (*current_ext
>= (total_extents
- 1))
5987 goto update_current_ext
;
5989 * If this is not the last extent in the file, make sure there
5990 * is enough room between current extent and next extent for
5991 * accommodating the shift.
5993 adj_irecp
= xfs_iext_get_ext(ifp
, *current_ext
+ 1);
5994 xfs_bmbt_get_all(adj_irecp
, &adj_irec
);
5995 if (startoff
+ got
.br_blockcount
> adj_irec
.br_startoff
)
5998 * Unlike a left shift (which involves a hole punch),
5999 * a right shift does not modify extent neighbors
6000 * in any way. We should never find mergeable extents
6001 * in this scenario. Check anyways and warn if we
6002 * encounter two extents that could be one.
6004 if (xfs_bmse_can_merge(&got
, &adj_irec
, offset_shift_fsb
))
6008 * Increment the extent index for the next iteration, update the start
6009 * offset of the in-core extent and update the btree if applicable.
6012 if (direction
== SHIFT_LEFT
)
6016 xfs_bmbt_set_startoff(gotp
, startoff
);
6017 *logflags
|= XFS_ILOG_CORE
;
6020 *logflags
|= XFS_ILOG_DEXT
;
6024 error
= xfs_bmbt_lookup_eq(cur
, got
.br_startoff
, got
.br_startblock
,
6025 got
.br_blockcount
, &i
);
6028 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
6030 got
.br_startoff
= startoff
;
6031 error
= xfs_bmbt_update(cur
, got
.br_startoff
, got
.br_startblock
,
6032 got
.br_blockcount
, got
.br_state
);
6037 /* update reverse mapping */
6038 error
= xfs_rmap_unmap_extent(mp
, dfops
, ip
, whichfork
, &adj_irec
);
6041 adj_irec
.br_startoff
= startoff
;
6042 return xfs_rmap_map_extent(mp
, dfops
, ip
, whichfork
, &adj_irec
);
6046 * Shift extent records to the left/right to cover/create a hole.
6048 * The maximum number of extents to be shifted in a single operation is
6049 * @num_exts. @stop_fsb specifies the file offset at which to stop shift and the
6050 * file offset where we've left off is returned in @next_fsb. @offset_shift_fsb
6051 * is the length by which each extent is shifted. If there is no hole to shift
6052 * the extents into, this will be considered invalid operation and we abort
6056 xfs_bmap_shift_extents(
6057 struct xfs_trans
*tp
,
6058 struct xfs_inode
*ip
,
6059 xfs_fileoff_t
*next_fsb
,
6060 xfs_fileoff_t offset_shift_fsb
,
6062 xfs_fileoff_t stop_fsb
,
6063 xfs_fsblock_t
*firstblock
,
6064 struct xfs_defer_ops
*dfops
,
6065 enum shift_direction direction
,
6068 struct xfs_btree_cur
*cur
= NULL
;
6069 struct xfs_bmbt_rec_host
*gotp
;
6070 struct xfs_bmbt_irec got
;
6071 struct xfs_mount
*mp
= ip
->i_mount
;
6072 struct xfs_ifork
*ifp
;
6073 xfs_extnum_t nexts
= 0;
6074 xfs_extnum_t current_ext
;
6075 xfs_extnum_t total_extents
;
6076 xfs_extnum_t stop_extent
;
6078 int whichfork
= XFS_DATA_FORK
;
6081 if (unlikely(XFS_TEST_ERROR(
6082 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
6083 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
6084 mp
, XFS_ERRTAG_BMAPIFORMAT
, XFS_RANDOM_BMAPIFORMAT
))) {
6085 XFS_ERROR_REPORT("xfs_bmap_shift_extents",
6086 XFS_ERRLEVEL_LOW
, mp
);
6087 return -EFSCORRUPTED
;
6090 if (XFS_FORCED_SHUTDOWN(mp
))
6093 ASSERT(xfs_isilocked(ip
, XFS_IOLOCK_EXCL
));
6094 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
6095 ASSERT(direction
== SHIFT_LEFT
|| direction
== SHIFT_RIGHT
);
6096 ASSERT(*next_fsb
!= NULLFSBLOCK
|| direction
== SHIFT_RIGHT
);
6098 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
6099 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
6100 /* Read in all the extents */
6101 error
= xfs_iread_extents(tp
, ip
, whichfork
);
6106 if (ifp
->if_flags
& XFS_IFBROOT
) {
6107 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
6108 cur
->bc_private
.b
.firstblock
= *firstblock
;
6109 cur
->bc_private
.b
.dfops
= dfops
;
6110 cur
->bc_private
.b
.flags
= 0;
6114 * There may be delalloc extents in the data fork before the range we
6115 * are collapsing out, so we cannot use the count of real extents here.
6116 * Instead we have to calculate it from the incore fork.
6118 total_extents
= xfs_iext_count(ifp
);
6119 if (total_extents
== 0) {
6125 * In case of first right shift, we need to initialize next_fsb
6127 if (*next_fsb
== NULLFSBLOCK
) {
6128 gotp
= xfs_iext_get_ext(ifp
, total_extents
- 1);
6129 xfs_bmbt_get_all(gotp
, &got
);
6130 *next_fsb
= got
.br_startoff
;
6131 if (stop_fsb
> *next_fsb
) {
6137 /* Lookup the extent index at which we have to stop */
6138 if (direction
== SHIFT_RIGHT
) {
6139 gotp
= xfs_iext_bno_to_ext(ifp
, stop_fsb
, &stop_extent
);
6140 /* Make stop_extent exclusive of shift range */
6143 stop_extent
= total_extents
;
6146 * Look up the extent index for the fsb where we start shifting. We can
6147 * henceforth iterate with current_ext as extent list changes are locked
6150 * gotp can be null in 2 cases: 1) if there are no extents or 2)
6151 * *next_fsb lies in a hole beyond which there are no extents. Either
6154 gotp
= xfs_iext_bno_to_ext(ifp
, *next_fsb
, ¤t_ext
);
6160 /* some sanity checking before we finally start shifting extents */
6161 if ((direction
== SHIFT_LEFT
&& current_ext
>= stop_extent
) ||
6162 (direction
== SHIFT_RIGHT
&& current_ext
<= stop_extent
)) {
6167 while (nexts
++ < num_exts
) {
6168 error
= xfs_bmse_shift_one(ip
, whichfork
, offset_shift_fsb
,
6169 ¤t_ext
, gotp
, cur
, &logflags
,
6174 * If there was an extent merge during the shift, the extent
6175 * count can change. Update the total and grade the next record.
6177 if (direction
== SHIFT_LEFT
) {
6178 total_extents
= xfs_iext_count(ifp
);
6179 stop_extent
= total_extents
;
6182 if (current_ext
== stop_extent
) {
6184 *next_fsb
= NULLFSBLOCK
;
6187 gotp
= xfs_iext_get_ext(ifp
, current_ext
);
6191 xfs_bmbt_get_all(gotp
, &got
);
6192 *next_fsb
= got
.br_startoff
;
6197 xfs_btree_del_cursor(cur
,
6198 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
6201 xfs_trans_log_inode(tp
, ip
, logflags
);
6207 * Splits an extent into two extents at split_fsb block such that it is
6208 * the first block of the current_ext. @current_ext is a target extent
6209 * to be split. @split_fsb is a block where the extents is split.
6210 * If split_fsb lies in a hole or the first block of extents, just return 0.
6213 xfs_bmap_split_extent_at(
6214 struct xfs_trans
*tp
,
6215 struct xfs_inode
*ip
,
6216 xfs_fileoff_t split_fsb
,
6217 xfs_fsblock_t
*firstfsb
,
6218 struct xfs_defer_ops
*dfops
)
6220 int whichfork
= XFS_DATA_FORK
;
6221 struct xfs_btree_cur
*cur
= NULL
;
6222 struct xfs_bmbt_rec_host
*gotp
;
6223 struct xfs_bmbt_irec got
;
6224 struct xfs_bmbt_irec
new; /* split extent */
6225 struct xfs_mount
*mp
= ip
->i_mount
;
6226 struct xfs_ifork
*ifp
;
6227 xfs_fsblock_t gotblkcnt
; /* new block count for got */
6228 xfs_extnum_t current_ext
;
6233 if (unlikely(XFS_TEST_ERROR(
6234 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
6235 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
6236 mp
, XFS_ERRTAG_BMAPIFORMAT
, XFS_RANDOM_BMAPIFORMAT
))) {
6237 XFS_ERROR_REPORT("xfs_bmap_split_extent_at",
6238 XFS_ERRLEVEL_LOW
, mp
);
6239 return -EFSCORRUPTED
;
6242 if (XFS_FORCED_SHUTDOWN(mp
))
6245 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
6246 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
6247 /* Read in all the extents */
6248 error
= xfs_iread_extents(tp
, ip
, whichfork
);
6254 * gotp can be null in 2 cases: 1) if there are no extents
6255 * or 2) split_fsb lies in a hole beyond which there are
6256 * no extents. Either way, we are done.
6258 gotp
= xfs_iext_bno_to_ext(ifp
, split_fsb
, ¤t_ext
);
6262 xfs_bmbt_get_all(gotp
, &got
);
6265 * Check split_fsb lies in a hole or the start boundary offset
6268 if (got
.br_startoff
>= split_fsb
)
6271 gotblkcnt
= split_fsb
- got
.br_startoff
;
6272 new.br_startoff
= split_fsb
;
6273 new.br_startblock
= got
.br_startblock
+ gotblkcnt
;
6274 new.br_blockcount
= got
.br_blockcount
- gotblkcnt
;
6275 new.br_state
= got
.br_state
;
6277 if (ifp
->if_flags
& XFS_IFBROOT
) {
6278 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
6279 cur
->bc_private
.b
.firstblock
= *firstfsb
;
6280 cur
->bc_private
.b
.dfops
= dfops
;
6281 cur
->bc_private
.b
.flags
= 0;
6282 error
= xfs_bmbt_lookup_eq(cur
, got
.br_startoff
,
6288 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, del_cursor
);
6291 xfs_bmbt_set_blockcount(gotp
, gotblkcnt
);
6292 got
.br_blockcount
= gotblkcnt
;
6294 logflags
= XFS_ILOG_CORE
;
6296 error
= xfs_bmbt_update(cur
, got
.br_startoff
,
6303 logflags
|= XFS_ILOG_DEXT
;
6305 /* Add new extent */
6307 xfs_iext_insert(ip
, current_ext
, 1, &new, 0);
6308 XFS_IFORK_NEXT_SET(ip
, whichfork
,
6309 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
6312 error
= xfs_bmbt_lookup_eq(cur
, new.br_startoff
,
6313 new.br_startblock
, new.br_blockcount
,
6317 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, del_cursor
);
6318 cur
->bc_rec
.b
.br_state
= new.br_state
;
6320 error
= xfs_btree_insert(cur
, &i
);
6323 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, del_cursor
);
6327 * Convert to a btree if necessary.
6329 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
6330 int tmp_logflags
; /* partial log flag return val */
6332 ASSERT(cur
== NULL
);
6333 error
= xfs_bmap_extents_to_btree(tp
, ip
, firstfsb
, dfops
,
6334 &cur
, 0, &tmp_logflags
, whichfork
);
6335 logflags
|= tmp_logflags
;
6340 cur
->bc_private
.b
.allocated
= 0;
6341 xfs_btree_del_cursor(cur
,
6342 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
6346 xfs_trans_log_inode(tp
, ip
, logflags
);
6351 xfs_bmap_split_extent(
6352 struct xfs_inode
*ip
,
6353 xfs_fileoff_t split_fsb
)
6355 struct xfs_mount
*mp
= ip
->i_mount
;
6356 struct xfs_trans
*tp
;
6357 struct xfs_defer_ops dfops
;
6358 xfs_fsblock_t firstfsb
;
6361 error
= xfs_trans_alloc(mp
, &M_RES(mp
)->tr_write
,
6362 XFS_DIOSTRAT_SPACE_RES(mp
, 0), 0, 0, &tp
);
6366 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
6367 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
);
6369 xfs_defer_init(&dfops
, &firstfsb
);
6371 error
= xfs_bmap_split_extent_at(tp
, ip
, split_fsb
,
6376 error
= xfs_defer_finish(&tp
, &dfops
, NULL
);
6380 return xfs_trans_commit(tp
);
6383 xfs_defer_cancel(&dfops
);
6384 xfs_trans_cancel(tp
);
6388 /* Deferred mapping is only for real extents in the data fork. */
6390 xfs_bmap_is_update_needed(
6391 struct xfs_bmbt_irec
*bmap
)
6393 return bmap
->br_startblock
!= HOLESTARTBLOCK
&&
6394 bmap
->br_startblock
!= DELAYSTARTBLOCK
;
6397 /* Record a bmap intent. */
6400 struct xfs_mount
*mp
,
6401 struct xfs_defer_ops
*dfops
,
6402 enum xfs_bmap_intent_type type
,
6403 struct xfs_inode
*ip
,
6405 struct xfs_bmbt_irec
*bmap
)
6408 struct xfs_bmap_intent
*bi
;
6410 trace_xfs_bmap_defer(mp
,
6411 XFS_FSB_TO_AGNO(mp
, bmap
->br_startblock
),
6413 XFS_FSB_TO_AGBNO(mp
, bmap
->br_startblock
),
6414 ip
->i_ino
, whichfork
,
6416 bmap
->br_blockcount
,
6419 bi
= kmem_alloc(sizeof(struct xfs_bmap_intent
), KM_SLEEP
| KM_NOFS
);
6420 INIT_LIST_HEAD(&bi
->bi_list
);
6423 bi
->bi_whichfork
= whichfork
;
6424 bi
->bi_bmap
= *bmap
;
6426 error
= xfs_defer_join(dfops
, bi
->bi_owner
);
6432 xfs_defer_add(dfops
, XFS_DEFER_OPS_TYPE_BMAP
, &bi
->bi_list
);
6436 /* Map an extent into a file. */
6438 xfs_bmap_map_extent(
6439 struct xfs_mount
*mp
,
6440 struct xfs_defer_ops
*dfops
,
6441 struct xfs_inode
*ip
,
6442 struct xfs_bmbt_irec
*PREV
)
6444 if (!xfs_bmap_is_update_needed(PREV
))
6447 return __xfs_bmap_add(mp
, dfops
, XFS_BMAP_MAP
, ip
,
6448 XFS_DATA_FORK
, PREV
);
6451 /* Unmap an extent out of a file. */
6453 xfs_bmap_unmap_extent(
6454 struct xfs_mount
*mp
,
6455 struct xfs_defer_ops
*dfops
,
6456 struct xfs_inode
*ip
,
6457 struct xfs_bmbt_irec
*PREV
)
6459 if (!xfs_bmap_is_update_needed(PREV
))
6462 return __xfs_bmap_add(mp
, dfops
, XFS_BMAP_UNMAP
, ip
,
6463 XFS_DATA_FORK
, PREV
);
6467 * Process one of the deferred bmap operations. We pass back the
6468 * btree cursor to maintain our lock on the bmapbt between calls.
6471 xfs_bmap_finish_one(
6472 struct xfs_trans
*tp
,
6473 struct xfs_defer_ops
*dfops
,
6474 struct xfs_inode
*ip
,
6475 enum xfs_bmap_intent_type type
,
6477 xfs_fileoff_t startoff
,
6478 xfs_fsblock_t startblock
,
6479 xfs_filblks_t blockcount
,
6482 struct xfs_bmbt_irec bmap
;
6484 xfs_fsblock_t firstfsb
;
6485 int flags
= XFS_BMAPI_REMAP
;
6489 bmap
.br_startblock
= startblock
;
6490 bmap
.br_startoff
= startoff
;
6491 bmap
.br_blockcount
= blockcount
;
6492 bmap
.br_state
= state
;
6494 trace_xfs_bmap_deferred(tp
->t_mountp
,
6495 XFS_FSB_TO_AGNO(tp
->t_mountp
, startblock
), type
,
6496 XFS_FSB_TO_AGBNO(tp
->t_mountp
, startblock
),
6497 ip
->i_ino
, whichfork
, startoff
, blockcount
, state
);
6499 if (whichfork
!= XFS_DATA_FORK
&& whichfork
!= XFS_ATTR_FORK
)
6500 return -EFSCORRUPTED
;
6501 if (whichfork
== XFS_ATTR_FORK
)
6502 flags
|= XFS_BMAPI_ATTRFORK
;
6504 if (XFS_TEST_ERROR(false, tp
->t_mountp
,
6505 XFS_ERRTAG_BMAP_FINISH_ONE
,
6506 XFS_RANDOM_BMAP_FINISH_ONE
))
6511 firstfsb
= bmap
.br_startblock
;
6512 error
= xfs_bmapi_write(tp
, ip
, bmap
.br_startoff
,
6513 bmap
.br_blockcount
, flags
, &firstfsb
,
6514 bmap
.br_blockcount
, &bmap
, &nimaps
,
6517 case XFS_BMAP_UNMAP
:
6518 error
= xfs_bunmapi(tp
, ip
, bmap
.br_startoff
,
6519 bmap
.br_blockcount
, flags
, 1, &firstfsb
,
6525 error
= -EFSCORRUPTED
;