2 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 #include "xfs_types.h"
24 #include "xfs_trans.h"
28 #include "xfs_da_btree.h"
29 #include "xfs_bmap_btree.h"
30 #include "xfs_alloc_btree.h"
31 #include "xfs_ialloc_btree.h"
32 #include "xfs_dinode.h"
33 #include "xfs_inode.h"
34 #include "xfs_btree.h"
35 #include "xfs_mount.h"
36 #include "xfs_itable.h"
37 #include "xfs_inode_item.h"
38 #include "xfs_extfree_item.h"
39 #include "xfs_alloc.h"
41 #include "xfs_rtalloc.h"
42 #include "xfs_error.h"
43 #include "xfs_attr_leaf.h"
44 #include "xfs_quota.h"
45 #include "xfs_trans_space.h"
46 #include "xfs_buf_item.h"
47 #include "xfs_filestream.h"
48 #include "xfs_vnodeops.h"
49 #include "xfs_trace.h"
50 #include "xfs_symlink.h"
53 kmem_zone_t
*xfs_bmap_free_item_zone
;
56 * Miscellaneous helper functions
60 * Compute and fill in the value of the maximum depth of a bmap btree
61 * in this filesystem. Done once, during mount.
64 xfs_bmap_compute_maxlevels(
65 xfs_mount_t
*mp
, /* file system mount structure */
66 int whichfork
) /* data or attr fork */
68 int level
; /* btree level */
69 uint maxblocks
; /* max blocks at this level */
70 uint maxleafents
; /* max leaf entries possible */
71 int maxrootrecs
; /* max records in root block */
72 int minleafrecs
; /* min records in leaf block */
73 int minnoderecs
; /* min records in node block */
74 int sz
; /* root block size */
77 * The maximum number of extents in a file, hence the maximum
78 * number of leaf entries, is controlled by the type of di_nextents
79 * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
80 * (a signed 16-bit number, xfs_aextnum_t).
82 * Note that we can no longer assume that if we are in ATTR1 that
83 * the fork offset of all the inodes will be
84 * (xfs_default_attroffset(ip) >> 3) because we could have mounted
85 * with ATTR2 and then mounted back with ATTR1, keeping the
86 * di_forkoff's fixed but probably at various positions. Therefore,
87 * for both ATTR1 and ATTR2 we have to assume the worst case scenario
88 * of a minimum size available.
90 if (whichfork
== XFS_DATA_FORK
) {
91 maxleafents
= MAXEXTNUM
;
92 sz
= XFS_BMDR_SPACE_CALC(MINDBTPTRS
);
94 maxleafents
= MAXAEXTNUM
;
95 sz
= XFS_BMDR_SPACE_CALC(MINABTPTRS
);
97 maxrootrecs
= xfs_bmdr_maxrecs(mp
, sz
, 0);
98 minleafrecs
= mp
->m_bmap_dmnr
[0];
99 minnoderecs
= mp
->m_bmap_dmnr
[1];
100 maxblocks
= (maxleafents
+ minleafrecs
- 1) / minleafrecs
;
101 for (level
= 1; maxblocks
> 1; level
++) {
102 if (maxblocks
<= maxrootrecs
)
105 maxblocks
= (maxblocks
+ minnoderecs
- 1) / minnoderecs
;
107 mp
->m_bm_maxlevels
[whichfork
] = level
;
111 * Convert the given file system block to a disk block. We have to treat it
112 * differently based on whether the file is a real time file or not, because the
116 xfs_fsb_to_db(struct xfs_inode
*ip
, xfs_fsblock_t fsb
)
118 return (XFS_IS_REALTIME_INODE(ip
) ? \
119 (xfs_daddr_t
)XFS_FSB_TO_BB((ip
)->i_mount
, (fsb
)) : \
120 XFS_FSB_TO_DADDR((ip
)->i_mount
, (fsb
)));
123 STATIC
int /* error */
125 struct xfs_btree_cur
*cur
,
129 int *stat
) /* success/failure */
131 cur
->bc_rec
.b
.br_startoff
= off
;
132 cur
->bc_rec
.b
.br_startblock
= bno
;
133 cur
->bc_rec
.b
.br_blockcount
= len
;
134 return xfs_btree_lookup(cur
, XFS_LOOKUP_EQ
, stat
);
137 STATIC
int /* error */
139 struct xfs_btree_cur
*cur
,
143 int *stat
) /* success/failure */
145 cur
->bc_rec
.b
.br_startoff
= off
;
146 cur
->bc_rec
.b
.br_startblock
= bno
;
147 cur
->bc_rec
.b
.br_blockcount
= len
;
148 return xfs_btree_lookup(cur
, XFS_LOOKUP_GE
, stat
);
152 * Check if the inode needs to be converted to btree format.
154 static inline bool xfs_bmap_needs_btree(struct xfs_inode
*ip
, int whichfork
)
156 return XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
&&
157 XFS_IFORK_NEXTENTS(ip
, whichfork
) >
158 XFS_IFORK_MAXEXT(ip
, whichfork
);
162 * Check if the inode should be converted to extent format.
164 static inline bool xfs_bmap_wants_extents(struct xfs_inode
*ip
, int whichfork
)
166 return XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
&&
167 XFS_IFORK_NEXTENTS(ip
, whichfork
) <=
168 XFS_IFORK_MAXEXT(ip
, whichfork
);
172 * Update the record referred to by cur to the value given
173 * by [off, bno, len, state].
174 * This either works (return 0) or gets an EFSCORRUPTED error.
178 struct xfs_btree_cur
*cur
,
184 union xfs_btree_rec rec
;
186 xfs_bmbt_disk_set_allf(&rec
.bmbt
, off
, bno
, len
, state
);
187 return xfs_btree_update(cur
, &rec
);
191 * Compute the worst-case number of indirect blocks that will be used
192 * for ip's delayed extent of length "len".
195 xfs_bmap_worst_indlen(
196 xfs_inode_t
*ip
, /* incore inode pointer */
197 xfs_filblks_t len
) /* delayed extent length */
199 int level
; /* btree level number */
200 int maxrecs
; /* maximum record count at this level */
201 xfs_mount_t
*mp
; /* mount structure */
202 xfs_filblks_t rval
; /* return value */
205 maxrecs
= mp
->m_bmap_dmxr
[0];
206 for (level
= 0, rval
= 0;
207 level
< XFS_BM_MAXLEVELS(mp
, XFS_DATA_FORK
);
210 do_div(len
, maxrecs
);
213 return rval
+ XFS_BM_MAXLEVELS(mp
, XFS_DATA_FORK
) -
216 maxrecs
= mp
->m_bmap_dmxr
[1];
222 * Calculate the default attribute fork offset for newly created inodes.
225 xfs_default_attroffset(
226 struct xfs_inode
*ip
)
228 struct xfs_mount
*mp
= ip
->i_mount
;
231 if (mp
->m_sb
.sb_inodesize
== 256) {
232 offset
= XFS_LITINO(mp
, ip
->i_d
.di_version
) -
233 XFS_BMDR_SPACE_CALC(MINABTPTRS
);
235 offset
= XFS_BMDR_SPACE_CALC(6 * MINABTPTRS
);
238 ASSERT(offset
< XFS_LITINO(mp
, ip
->i_d
.di_version
));
243 * Helper routine to reset inode di_forkoff field when switching
244 * attribute fork from local to extent format - we reset it where
245 * possible to make space available for inline data fork extents.
248 xfs_bmap_forkoff_reset(
253 if (whichfork
== XFS_ATTR_FORK
&&
254 ip
->i_d
.di_format
!= XFS_DINODE_FMT_DEV
&&
255 ip
->i_d
.di_format
!= XFS_DINODE_FMT_UUID
&&
256 ip
->i_d
.di_format
!= XFS_DINODE_FMT_BTREE
) {
257 uint dfl_forkoff
= xfs_default_attroffset(ip
) >> 3;
259 if (dfl_forkoff
> ip
->i_d
.di_forkoff
)
260 ip
->i_d
.di_forkoff
= dfl_forkoff
;
265 * Extent tree block counting routines.
269 * Count leaf blocks given a range of extent records.
272 xfs_bmap_count_leaves(
280 for (b
= 0; b
< numrecs
; b
++) {
281 xfs_bmbt_rec_host_t
*frp
= xfs_iext_get_ext(ifp
, idx
+ b
);
282 *count
+= xfs_bmbt_get_blockcount(frp
);
287 * Count leaf blocks given a range of extent records originally
291 xfs_bmap_disk_count_leaves(
292 struct xfs_mount
*mp
,
293 struct xfs_btree_block
*block
,
300 for (b
= 1; b
<= numrecs
; b
++) {
301 frp
= XFS_BMBT_REC_ADDR(mp
, block
, b
);
302 *count
+= xfs_bmbt_disk_get_blockcount(frp
);
307 * Recursively walks each level of a btree
308 * to count total fsblocks is use.
310 STATIC
int /* error */
312 xfs_mount_t
*mp
, /* file system mount point */
313 xfs_trans_t
*tp
, /* transaction pointer */
314 xfs_ifork_t
*ifp
, /* inode fork pointer */
315 xfs_fsblock_t blockno
, /* file system block number */
316 int levelin
, /* level in btree */
317 int *count
) /* Count of blocks */
323 xfs_fsblock_t bno
= blockno
;
324 xfs_fsblock_t nextbno
;
325 struct xfs_btree_block
*block
, *nextblock
;
328 error
= xfs_btree_read_bufl(mp
, tp
, bno
, 0, &bp
, XFS_BMAP_BTREE_REF
,
333 block
= XFS_BUF_TO_BLOCK(bp
);
336 /* Not at node above leaves, count this level of nodes */
337 nextbno
= be64_to_cpu(block
->bb_u
.l
.bb_rightsib
);
338 while (nextbno
!= NULLFSBLOCK
) {
339 error
= xfs_btree_read_bufl(mp
, tp
, nextbno
, 0, &nbp
,
345 nextblock
= XFS_BUF_TO_BLOCK(nbp
);
346 nextbno
= be64_to_cpu(nextblock
->bb_u
.l
.bb_rightsib
);
347 xfs_trans_brelse(tp
, nbp
);
350 /* Dive to the next level */
351 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, mp
->m_bmap_dmxr
[1]);
352 bno
= be64_to_cpu(*pp
);
353 if (unlikely((error
=
354 xfs_bmap_count_tree(mp
, tp
, ifp
, bno
, level
, count
)) < 0)) {
355 xfs_trans_brelse(tp
, bp
);
356 XFS_ERROR_REPORT("xfs_bmap_count_tree(1)",
357 XFS_ERRLEVEL_LOW
, mp
);
358 return XFS_ERROR(EFSCORRUPTED
);
360 xfs_trans_brelse(tp
, bp
);
362 /* count all level 1 nodes and their leaves */
364 nextbno
= be64_to_cpu(block
->bb_u
.l
.bb_rightsib
);
365 numrecs
= be16_to_cpu(block
->bb_numrecs
);
366 xfs_bmap_disk_count_leaves(mp
, block
, numrecs
, count
);
367 xfs_trans_brelse(tp
, bp
);
368 if (nextbno
== NULLFSBLOCK
)
371 error
= xfs_btree_read_bufl(mp
, tp
, bno
, 0, &bp
,
377 block
= XFS_BUF_TO_BLOCK(bp
);
384 * Count fsblocks of the given fork.
387 xfs_bmap_count_blocks(
388 xfs_trans_t
*tp
, /* transaction pointer */
389 xfs_inode_t
*ip
, /* incore inode */
390 int whichfork
, /* data or attr fork */
391 int *count
) /* out: count of blocks */
393 struct xfs_btree_block
*block
; /* current btree block */
394 xfs_fsblock_t bno
; /* block # of "block" */
395 xfs_ifork_t
*ifp
; /* fork structure */
396 int level
; /* btree level, for checking */
397 xfs_mount_t
*mp
; /* file system mount structure */
398 __be64
*pp
; /* pointer to block address */
402 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
403 if ( XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
) {
404 xfs_bmap_count_leaves(ifp
, 0,
405 ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
),
411 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
413 block
= ifp
->if_broot
;
414 level
= be16_to_cpu(block
->bb_level
);
416 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, 1, ifp
->if_broot_bytes
);
417 bno
= be64_to_cpu(*pp
);
418 ASSERT(bno
!= NULLDFSBNO
);
419 ASSERT(XFS_FSB_TO_AGNO(mp
, bno
) < mp
->m_sb
.sb_agcount
);
420 ASSERT(XFS_FSB_TO_AGBNO(mp
, bno
) < mp
->m_sb
.sb_agblocks
);
422 if (unlikely(xfs_bmap_count_tree(mp
, tp
, ifp
, bno
, level
, count
) < 0)) {
423 XFS_ERROR_REPORT("xfs_bmap_count_blocks(2)", XFS_ERRLEVEL_LOW
,
425 return XFS_ERROR(EFSCORRUPTED
);
432 * Debug/sanity checking code
436 xfs_bmap_sanity_check(
437 struct xfs_mount
*mp
,
441 struct xfs_btree_block
*block
= XFS_BUF_TO_BLOCK(bp
);
443 if (block
->bb_magic
!= cpu_to_be32(XFS_BMAP_CRC_MAGIC
) &&
444 block
->bb_magic
!= cpu_to_be32(XFS_BMAP_MAGIC
))
447 if (be16_to_cpu(block
->bb_level
) != level
||
448 be16_to_cpu(block
->bb_numrecs
) == 0 ||
449 be16_to_cpu(block
->bb_numrecs
) > mp
->m_bmap_dmxr
[level
!= 0])
456 STATIC
struct xfs_buf
*
458 struct xfs_btree_cur
*cur
,
461 struct xfs_log_item_desc
*lidp
;
467 for (i
= 0; i
< XFS_BTREE_MAXLEVELS
; i
++) {
468 if (!cur
->bc_bufs
[i
])
470 if (XFS_BUF_ADDR(cur
->bc_bufs
[i
]) == bno
)
471 return cur
->bc_bufs
[i
];
474 /* Chase down all the log items to see if the bp is there */
475 list_for_each_entry(lidp
, &cur
->bc_tp
->t_items
, lid_trans
) {
476 struct xfs_buf_log_item
*bip
;
477 bip
= (struct xfs_buf_log_item
*)lidp
->lid_item
;
478 if (bip
->bli_item
.li_type
== XFS_LI_BUF
&&
479 XFS_BUF_ADDR(bip
->bli_buf
) == bno
)
488 struct xfs_btree_block
*block
,
494 __be64
*pp
, *thispa
; /* pointer to block address */
495 xfs_bmbt_key_t
*prevp
, *keyp
;
497 ASSERT(be16_to_cpu(block
->bb_level
) > 0);
500 for( i
= 1; i
<= xfs_btree_get_numrecs(block
); i
++) {
501 dmxr
= mp
->m_bmap_dmxr
[0];
502 keyp
= XFS_BMBT_KEY_ADDR(mp
, block
, i
);
505 ASSERT(be64_to_cpu(prevp
->br_startoff
) <
506 be64_to_cpu(keyp
->br_startoff
));
511 * Compare the block numbers to see if there are dups.
514 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, i
, sz
);
516 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, i
, dmxr
);
518 for (j
= i
+1; j
<= be16_to_cpu(block
->bb_numrecs
); j
++) {
520 thispa
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, j
, sz
);
522 thispa
= XFS_BMBT_PTR_ADDR(mp
, block
, j
, dmxr
);
523 if (*thispa
== *pp
) {
524 xfs_warn(mp
, "%s: thispa(%d) == pp(%d) %Ld",
526 (unsigned long long)be64_to_cpu(*thispa
));
527 panic("%s: ptrs are equal in node\n",
535 * Check that the extents for the inode ip are in the right order in all
540 xfs_bmap_check_leaf_extents(
541 xfs_btree_cur_t
*cur
, /* btree cursor or null */
542 xfs_inode_t
*ip
, /* incore inode pointer */
543 int whichfork
) /* data or attr fork */
545 struct xfs_btree_block
*block
; /* current btree block */
546 xfs_fsblock_t bno
; /* block # of "block" */
547 xfs_buf_t
*bp
; /* buffer for "block" */
548 int error
; /* error return value */
549 xfs_extnum_t i
=0, j
; /* index into the extents list */
550 xfs_ifork_t
*ifp
; /* fork structure */
551 int level
; /* btree level, for checking */
552 xfs_mount_t
*mp
; /* file system mount structure */
553 __be64
*pp
; /* pointer to block address */
554 xfs_bmbt_rec_t
*ep
; /* pointer to current extent */
555 xfs_bmbt_rec_t last
= {0, 0}; /* last extent in prev block */
556 xfs_bmbt_rec_t
*nextp
; /* pointer to next extent */
559 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
) {
565 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
566 block
= ifp
->if_broot
;
568 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
570 level
= be16_to_cpu(block
->bb_level
);
572 xfs_check_block(block
, mp
, 1, ifp
->if_broot_bytes
);
573 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, 1, ifp
->if_broot_bytes
);
574 bno
= be64_to_cpu(*pp
);
576 ASSERT(bno
!= NULLDFSBNO
);
577 ASSERT(XFS_FSB_TO_AGNO(mp
, bno
) < mp
->m_sb
.sb_agcount
);
578 ASSERT(XFS_FSB_TO_AGBNO(mp
, bno
) < mp
->m_sb
.sb_agblocks
);
581 * Go down the tree until leaf level is reached, following the first
582 * pointer (leftmost) at each level.
584 while (level
-- > 0) {
585 /* See if buf is in cur first */
587 bp
= xfs_bmap_get_bp(cur
, XFS_FSB_TO_DADDR(mp
, bno
));
590 error
= xfs_btree_read_bufl(mp
, NULL
, bno
, 0, &bp
,
596 block
= XFS_BUF_TO_BLOCK(bp
);
597 XFS_WANT_CORRUPTED_GOTO(
598 xfs_bmap_sanity_check(mp
, bp
, level
),
604 * Check this block for basic sanity (increasing keys and
605 * no duplicate blocks).
608 xfs_check_block(block
, mp
, 0, 0);
609 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, mp
->m_bmap_dmxr
[1]);
610 bno
= be64_to_cpu(*pp
);
611 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp
, bno
), error0
);
614 xfs_trans_brelse(NULL
, bp
);
619 * Here with bp and block set to the leftmost leaf node in the tree.
624 * Loop over all leaf nodes checking that all extents are in the right order.
627 xfs_fsblock_t nextbno
;
628 xfs_extnum_t num_recs
;
631 num_recs
= xfs_btree_get_numrecs(block
);
634 * Read-ahead the next leaf block, if any.
637 nextbno
= be64_to_cpu(block
->bb_u
.l
.bb_rightsib
);
640 * Check all the extents to make sure they are OK.
641 * If we had a previous block, the last entry should
642 * conform with the first entry in this one.
645 ep
= XFS_BMBT_REC_ADDR(mp
, block
, 1);
647 ASSERT(xfs_bmbt_disk_get_startoff(&last
) +
648 xfs_bmbt_disk_get_blockcount(&last
) <=
649 xfs_bmbt_disk_get_startoff(ep
));
651 for (j
= 1; j
< num_recs
; j
++) {
652 nextp
= XFS_BMBT_REC_ADDR(mp
, block
, j
+ 1);
653 ASSERT(xfs_bmbt_disk_get_startoff(ep
) +
654 xfs_bmbt_disk_get_blockcount(ep
) <=
655 xfs_bmbt_disk_get_startoff(nextp
));
663 xfs_trans_brelse(NULL
, bp
);
667 * If we've reached the end, stop.
669 if (bno
== NULLFSBLOCK
)
673 bp
= xfs_bmap_get_bp(cur
, XFS_FSB_TO_DADDR(mp
, bno
));
676 error
= xfs_btree_read_bufl(mp
, NULL
, bno
, 0, &bp
,
682 block
= XFS_BUF_TO_BLOCK(bp
);
686 xfs_trans_brelse(NULL
, bp
);
691 xfs_warn(mp
, "%s: at error0", __func__
);
693 xfs_trans_brelse(NULL
, bp
);
695 xfs_warn(mp
, "%s: BAD after btree leaves for %d extents",
697 panic("%s: CORRUPTED BTREE OR SOMETHING", __func__
);
702 * Add bmap trace insert entries for all the contents of the extent records.
705 xfs_bmap_trace_exlist(
706 xfs_inode_t
*ip
, /* incore inode pointer */
707 xfs_extnum_t cnt
, /* count of entries in the list */
708 int whichfork
, /* data or attr fork */
709 unsigned long caller_ip
)
711 xfs_extnum_t idx
; /* extent record index */
712 xfs_ifork_t
*ifp
; /* inode fork pointer */
715 if (whichfork
== XFS_ATTR_FORK
)
716 state
|= BMAP_ATTRFORK
;
718 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
719 ASSERT(cnt
== (ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
)));
720 for (idx
= 0; idx
< cnt
; idx
++)
721 trace_xfs_extlist(ip
, idx
, whichfork
, caller_ip
);
725 * Validate that the bmbt_irecs being returned from bmapi are valid
726 * given the callers original parameters. Specifically check the
727 * ranges of the returned irecs to ensure that they only extent beyond
728 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
731 xfs_bmap_validate_ret(
735 xfs_bmbt_irec_t
*mval
,
739 int i
; /* index to map values */
741 ASSERT(ret_nmap
<= nmap
);
743 for (i
= 0; i
< ret_nmap
; i
++) {
744 ASSERT(mval
[i
].br_blockcount
> 0);
745 if (!(flags
& XFS_BMAPI_ENTIRE
)) {
746 ASSERT(mval
[i
].br_startoff
>= bno
);
747 ASSERT(mval
[i
].br_blockcount
<= len
);
748 ASSERT(mval
[i
].br_startoff
+ mval
[i
].br_blockcount
<=
751 ASSERT(mval
[i
].br_startoff
< bno
+ len
);
752 ASSERT(mval
[i
].br_startoff
+ mval
[i
].br_blockcount
>
756 mval
[i
- 1].br_startoff
+ mval
[i
- 1].br_blockcount
==
757 mval
[i
].br_startoff
);
758 ASSERT(mval
[i
].br_startblock
!= DELAYSTARTBLOCK
&&
759 mval
[i
].br_startblock
!= HOLESTARTBLOCK
);
760 ASSERT(mval
[i
].br_state
== XFS_EXT_NORM
||
761 mval
[i
].br_state
== XFS_EXT_UNWRITTEN
);
766 #define xfs_bmap_check_leaf_extents(cur, ip, whichfork) do { } while (0)
767 #define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap)
771 * bmap free list manipulation functions
775 * Add the extent to the list of extents to be free at transaction end.
776 * The list is maintained sorted (by block number).
780 xfs_fsblock_t bno
, /* fs block number of extent */
781 xfs_filblks_t len
, /* length of extent */
782 xfs_bmap_free_t
*flist
, /* list of extents */
783 xfs_mount_t
*mp
) /* mount point structure */
785 xfs_bmap_free_item_t
*cur
; /* current (next) element */
786 xfs_bmap_free_item_t
*new; /* new element */
787 xfs_bmap_free_item_t
*prev
; /* previous element */
792 ASSERT(bno
!= NULLFSBLOCK
);
794 ASSERT(len
<= MAXEXTLEN
);
795 ASSERT(!isnullstartblock(bno
));
796 agno
= XFS_FSB_TO_AGNO(mp
, bno
);
797 agbno
= XFS_FSB_TO_AGBNO(mp
, bno
);
798 ASSERT(agno
< mp
->m_sb
.sb_agcount
);
799 ASSERT(agbno
< mp
->m_sb
.sb_agblocks
);
800 ASSERT(len
< mp
->m_sb
.sb_agblocks
);
801 ASSERT(agbno
+ len
<= mp
->m_sb
.sb_agblocks
);
803 ASSERT(xfs_bmap_free_item_zone
!= NULL
);
804 new = kmem_zone_alloc(xfs_bmap_free_item_zone
, KM_SLEEP
);
805 new->xbfi_startblock
= bno
;
806 new->xbfi_blockcount
= (xfs_extlen_t
)len
;
807 for (prev
= NULL
, cur
= flist
->xbf_first
;
809 prev
= cur
, cur
= cur
->xbfi_next
) {
810 if (cur
->xbfi_startblock
>= bno
)
814 prev
->xbfi_next
= new;
816 flist
->xbf_first
= new;
817 new->xbfi_next
= cur
;
822 * Remove the entry "free" from the free item list. Prev points to the
823 * previous entry, unless "free" is the head of the list.
827 xfs_bmap_free_t
*flist
, /* free item list header */
828 xfs_bmap_free_item_t
*prev
, /* previous item on list, if any */
829 xfs_bmap_free_item_t
*free
) /* list item to be freed */
832 prev
->xbfi_next
= free
->xbfi_next
;
834 flist
->xbf_first
= free
->xbfi_next
;
836 kmem_zone_free(xfs_bmap_free_item_zone
, free
);
841 * Routine to be called at transaction's end by xfs_bmapi, xfs_bunmapi
842 * caller. Frees all the extents that need freeing, which must be done
843 * last due to locking considerations. We never free any extents in
844 * the first transaction.
846 * Return 1 if the given transaction was committed and a new one
847 * started, and 0 otherwise in the committed parameter.
851 xfs_trans_t
**tp
, /* transaction pointer addr */
852 xfs_bmap_free_t
*flist
, /* i/o: list extents to free */
853 int *committed
) /* xact committed or not */
855 xfs_efd_log_item_t
*efd
; /* extent free data */
856 xfs_efi_log_item_t
*efi
; /* extent free intention */
857 int error
; /* error return value */
858 xfs_bmap_free_item_t
*free
; /* free extent item */
859 unsigned int logres
; /* new log reservation */
860 unsigned int logcount
; /* new log count */
861 xfs_mount_t
*mp
; /* filesystem mount structure */
862 xfs_bmap_free_item_t
*next
; /* next item on free list */
863 xfs_trans_t
*ntp
; /* new transaction pointer */
865 ASSERT((*tp
)->t_flags
& XFS_TRANS_PERM_LOG_RES
);
866 if (flist
->xbf_count
== 0) {
871 efi
= xfs_trans_get_efi(ntp
, flist
->xbf_count
);
872 for (free
= flist
->xbf_first
; free
; free
= free
->xbfi_next
)
873 xfs_trans_log_efi_extent(ntp
, efi
, free
->xbfi_startblock
,
874 free
->xbfi_blockcount
);
875 logres
= ntp
->t_log_res
;
876 logcount
= ntp
->t_log_count
;
877 ntp
= xfs_trans_dup(*tp
);
878 error
= xfs_trans_commit(*tp
, 0);
882 * We have a new transaction, so we should return committed=1,
883 * even though we're returning an error.
889 * transaction commit worked ok so we can drop the extra ticket
890 * reference that we gained in xfs_trans_dup()
892 xfs_log_ticket_put(ntp
->t_ticket
);
894 if ((error
= xfs_trans_reserve(ntp
, 0, logres
, 0, XFS_TRANS_PERM_LOG_RES
,
897 efd
= xfs_trans_get_efd(ntp
, efi
, flist
->xbf_count
);
898 for (free
= flist
->xbf_first
; free
!= NULL
; free
= next
) {
899 next
= free
->xbfi_next
;
900 if ((error
= xfs_free_extent(ntp
, free
->xbfi_startblock
,
901 free
->xbfi_blockcount
))) {
903 * The bmap free list will be cleaned up at a
904 * higher level. The EFI will be canceled when
905 * this transaction is aborted.
906 * Need to force shutdown here to make sure it
907 * happens, since this transaction may not be
911 if (!XFS_FORCED_SHUTDOWN(mp
))
912 xfs_force_shutdown(mp
,
913 (error
== EFSCORRUPTED
) ?
914 SHUTDOWN_CORRUPT_INCORE
:
915 SHUTDOWN_META_IO_ERROR
);
918 xfs_trans_log_efd_extent(ntp
, efd
, free
->xbfi_startblock
,
919 free
->xbfi_blockcount
);
920 xfs_bmap_del_free(flist
, NULL
, free
);
926 * Free up any items left in the list.
930 xfs_bmap_free_t
*flist
) /* list of bmap_free_items */
932 xfs_bmap_free_item_t
*free
; /* free list item */
933 xfs_bmap_free_item_t
*next
;
935 if (flist
->xbf_count
== 0)
937 ASSERT(flist
->xbf_first
!= NULL
);
938 for (free
= flist
->xbf_first
; free
; free
= next
) {
939 next
= free
->xbfi_next
;
940 xfs_bmap_del_free(flist
, NULL
, free
);
942 ASSERT(flist
->xbf_count
== 0);
946 * Inode fork format manipulation functions
950 * Transform a btree format file with only one leaf node, where the
951 * extents list will fit in the inode, into an extents format file.
952 * Since the file extents are already in-core, all we have to do is
953 * give up the space for the btree root and pitch the leaf block.
955 STATIC
int /* error */
956 xfs_bmap_btree_to_extents(
957 xfs_trans_t
*tp
, /* transaction pointer */
958 xfs_inode_t
*ip
, /* incore inode pointer */
959 xfs_btree_cur_t
*cur
, /* btree cursor */
960 int *logflagsp
, /* inode logging flags */
961 int whichfork
) /* data or attr fork */
964 struct xfs_btree_block
*cblock
;/* child btree block */
965 xfs_fsblock_t cbno
; /* child block number */
966 xfs_buf_t
*cbp
; /* child block's buffer */
967 int error
; /* error return value */
968 xfs_ifork_t
*ifp
; /* inode fork data */
969 xfs_mount_t
*mp
; /* mount point structure */
970 __be64
*pp
; /* ptr to block address */
971 struct xfs_btree_block
*rblock
;/* root btree block */
974 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
975 ASSERT(ifp
->if_flags
& XFS_IFEXTENTS
);
976 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
);
977 rblock
= ifp
->if_broot
;
978 ASSERT(be16_to_cpu(rblock
->bb_level
) == 1);
979 ASSERT(be16_to_cpu(rblock
->bb_numrecs
) == 1);
980 ASSERT(xfs_bmbt_maxrecs(mp
, ifp
->if_broot_bytes
, 0) == 1);
981 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, rblock
, 1, ifp
->if_broot_bytes
);
982 cbno
= be64_to_cpu(*pp
);
985 if ((error
= xfs_btree_check_lptr(cur
, cbno
, 1)))
988 error
= xfs_btree_read_bufl(mp
, tp
, cbno
, 0, &cbp
, XFS_BMAP_BTREE_REF
,
992 cblock
= XFS_BUF_TO_BLOCK(cbp
);
993 if ((error
= xfs_btree_check_block(cur
, cblock
, 0, cbp
)))
995 xfs_bmap_add_free(cbno
, 1, cur
->bc_private
.b
.flist
, mp
);
996 ip
->i_d
.di_nblocks
--;
997 xfs_trans_mod_dquot_byino(tp
, ip
, XFS_TRANS_DQ_BCOUNT
, -1L);
998 xfs_trans_binval(tp
, cbp
);
999 if (cur
->bc_bufs
[0] == cbp
)
1000 cur
->bc_bufs
[0] = NULL
;
1001 xfs_iroot_realloc(ip
, -1, whichfork
);
1002 ASSERT(ifp
->if_broot
== NULL
);
1003 ASSERT((ifp
->if_flags
& XFS_IFBROOT
) == 0);
1004 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_EXTENTS
);
1005 *logflagsp
= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
1010 * Convert an extents-format file into a btree-format file.
1011 * The new file will have a root block (in the inode) and a single child block.
1013 STATIC
int /* error */
1014 xfs_bmap_extents_to_btree(
1015 xfs_trans_t
*tp
, /* transaction pointer */
1016 xfs_inode_t
*ip
, /* incore inode pointer */
1017 xfs_fsblock_t
*firstblock
, /* first-block-allocated */
1018 xfs_bmap_free_t
*flist
, /* blocks freed in xaction */
1019 xfs_btree_cur_t
**curp
, /* cursor returned to caller */
1020 int wasdel
, /* converting a delayed alloc */
1021 int *logflagsp
, /* inode logging flags */
1022 int whichfork
) /* data or attr fork */
1024 struct xfs_btree_block
*ablock
; /* allocated (child) bt block */
1025 xfs_buf_t
*abp
; /* buffer for ablock */
1026 xfs_alloc_arg_t args
; /* allocation arguments */
1027 xfs_bmbt_rec_t
*arp
; /* child record pointer */
1028 struct xfs_btree_block
*block
; /* btree root block */
1029 xfs_btree_cur_t
*cur
; /* bmap btree cursor */
1030 xfs_bmbt_rec_host_t
*ep
; /* extent record pointer */
1031 int error
; /* error return value */
1032 xfs_extnum_t i
, cnt
; /* extent record index */
1033 xfs_ifork_t
*ifp
; /* inode fork pointer */
1034 xfs_bmbt_key_t
*kp
; /* root block key pointer */
1035 xfs_mount_t
*mp
; /* mount structure */
1036 xfs_extnum_t nextents
; /* number of file extents */
1037 xfs_bmbt_ptr_t
*pp
; /* root block address pointer */
1040 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1041 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
);
1044 * Make space in the inode incore.
1046 xfs_iroot_realloc(ip
, 1, whichfork
);
1047 ifp
->if_flags
|= XFS_IFBROOT
;
1052 block
= ifp
->if_broot
;
1053 if (xfs_sb_version_hascrc(&mp
->m_sb
))
1054 xfs_btree_init_block_int(mp
, block
, XFS_BUF_DADDR_NULL
,
1055 XFS_BMAP_CRC_MAGIC
, 1, 1, ip
->i_ino
,
1056 XFS_BTREE_LONG_PTRS
| XFS_BTREE_CRC_BLOCKS
);
1058 xfs_btree_init_block_int(mp
, block
, XFS_BUF_DADDR_NULL
,
1059 XFS_BMAP_MAGIC
, 1, 1, ip
->i_ino
,
1060 XFS_BTREE_LONG_PTRS
);
1063 * Need a cursor. Can't allocate until bb_level is filled in.
1065 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
1066 cur
->bc_private
.b
.firstblock
= *firstblock
;
1067 cur
->bc_private
.b
.flist
= flist
;
1068 cur
->bc_private
.b
.flags
= wasdel
? XFS_BTCUR_BPRV_WASDEL
: 0;
1070 * Convert to a btree with two levels, one record in root.
1072 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_BTREE
);
1073 memset(&args
, 0, sizeof(args
));
1076 args
.firstblock
= *firstblock
;
1077 if (*firstblock
== NULLFSBLOCK
) {
1078 args
.type
= XFS_ALLOCTYPE_START_BNO
;
1079 args
.fsbno
= XFS_INO_TO_FSB(mp
, ip
->i_ino
);
1080 } else if (flist
->xbf_low
) {
1081 args
.type
= XFS_ALLOCTYPE_START_BNO
;
1082 args
.fsbno
= *firstblock
;
1084 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
1085 args
.fsbno
= *firstblock
;
1087 args
.minlen
= args
.maxlen
= args
.prod
= 1;
1088 args
.wasdel
= wasdel
;
1090 if ((error
= xfs_alloc_vextent(&args
))) {
1091 xfs_iroot_realloc(ip
, -1, whichfork
);
1092 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
1096 * Allocation can't fail, the space was reserved.
1098 ASSERT(args
.fsbno
!= NULLFSBLOCK
);
1099 ASSERT(*firstblock
== NULLFSBLOCK
||
1100 args
.agno
== XFS_FSB_TO_AGNO(mp
, *firstblock
) ||
1102 args
.agno
> XFS_FSB_TO_AGNO(mp
, *firstblock
)));
1103 *firstblock
= cur
->bc_private
.b
.firstblock
= args
.fsbno
;
1104 cur
->bc_private
.b
.allocated
++;
1105 ip
->i_d
.di_nblocks
++;
1106 xfs_trans_mod_dquot_byino(tp
, ip
, XFS_TRANS_DQ_BCOUNT
, 1L);
1107 abp
= xfs_btree_get_bufl(mp
, tp
, args
.fsbno
, 0);
1109 * Fill in the child block.
1111 abp
->b_ops
= &xfs_bmbt_buf_ops
;
1112 ablock
= XFS_BUF_TO_BLOCK(abp
);
1113 if (xfs_sb_version_hascrc(&mp
->m_sb
))
1114 xfs_btree_init_block_int(mp
, ablock
, abp
->b_bn
,
1115 XFS_BMAP_CRC_MAGIC
, 0, 0, ip
->i_ino
,
1116 XFS_BTREE_LONG_PTRS
| XFS_BTREE_CRC_BLOCKS
);
1118 xfs_btree_init_block_int(mp
, ablock
, abp
->b_bn
,
1119 XFS_BMAP_MAGIC
, 0, 0, ip
->i_ino
,
1120 XFS_BTREE_LONG_PTRS
);
1122 arp
= XFS_BMBT_REC_ADDR(mp
, ablock
, 1);
1123 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
1124 for (cnt
= i
= 0; i
< nextents
; i
++) {
1125 ep
= xfs_iext_get_ext(ifp
, i
);
1126 if (!isnullstartblock(xfs_bmbt_get_startblock(ep
))) {
1127 arp
->l0
= cpu_to_be64(ep
->l0
);
1128 arp
->l1
= cpu_to_be64(ep
->l1
);
1132 ASSERT(cnt
== XFS_IFORK_NEXTENTS(ip
, whichfork
));
1133 xfs_btree_set_numrecs(ablock
, cnt
);
1136 * Fill in the root key and pointer.
1138 kp
= XFS_BMBT_KEY_ADDR(mp
, block
, 1);
1139 arp
= XFS_BMBT_REC_ADDR(mp
, ablock
, 1);
1140 kp
->br_startoff
= cpu_to_be64(xfs_bmbt_disk_get_startoff(arp
));
1141 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, xfs_bmbt_get_maxrecs(cur
,
1142 be16_to_cpu(block
->bb_level
)));
1143 *pp
= cpu_to_be64(args
.fsbno
);
1146 * Do all this logging at the end so that
1147 * the root is at the right level.
1149 xfs_btree_log_block(cur
, abp
, XFS_BB_ALL_BITS
);
1150 xfs_btree_log_recs(cur
, abp
, 1, be16_to_cpu(ablock
->bb_numrecs
));
1151 ASSERT(*curp
== NULL
);
1153 *logflagsp
= XFS_ILOG_CORE
| xfs_ilog_fbroot(whichfork
);
1158 * Convert a local file to an extents file.
1159 * This code is out of bounds for data forks of regular files,
1160 * since the file data needs to get logged so things will stay consistent.
1161 * (The bmap-level manipulations are ok, though).
1163 STATIC
int /* error */
1164 xfs_bmap_local_to_extents(
1165 xfs_trans_t
*tp
, /* transaction pointer */
1166 xfs_inode_t
*ip
, /* incore inode pointer */
1167 xfs_fsblock_t
*firstblock
, /* first block allocated in xaction */
1168 xfs_extlen_t total
, /* total blocks needed by transaction */
1169 int *logflagsp
, /* inode logging flags */
1171 void (*init_fn
)(struct xfs_trans
*tp
,
1173 struct xfs_inode
*ip
,
1174 struct xfs_ifork
*ifp
))
1176 int error
; /* error return value */
1177 int flags
; /* logging flags returned */
1178 xfs_ifork_t
*ifp
; /* inode fork pointer */
1181 * We don't want to deal with the case of keeping inode data inline yet.
1182 * So sending the data fork of a regular inode is invalid.
1184 ASSERT(!(S_ISREG(ip
->i_d
.di_mode
) && whichfork
== XFS_DATA_FORK
));
1185 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1186 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
1189 if (ifp
->if_bytes
) {
1190 xfs_alloc_arg_t args
; /* allocation arguments */
1191 xfs_buf_t
*bp
; /* buffer for extent block */
1192 xfs_bmbt_rec_host_t
*ep
;/* extent record pointer */
1194 ASSERT((ifp
->if_flags
&
1195 (XFS_IFINLINE
|XFS_IFEXTENTS
|XFS_IFEXTIREC
)) == XFS_IFINLINE
);
1196 memset(&args
, 0, sizeof(args
));
1198 args
.mp
= ip
->i_mount
;
1199 args
.firstblock
= *firstblock
;
1201 * Allocate a block. We know we need only one, since the
1202 * file currently fits in an inode.
1204 if (*firstblock
== NULLFSBLOCK
) {
1205 args
.fsbno
= XFS_INO_TO_FSB(args
.mp
, ip
->i_ino
);
1206 args
.type
= XFS_ALLOCTYPE_START_BNO
;
1208 args
.fsbno
= *firstblock
;
1209 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
1212 args
.minlen
= args
.maxlen
= args
.prod
= 1;
1213 error
= xfs_alloc_vextent(&args
);
1217 /* Can't fail, the space was reserved. */
1218 ASSERT(args
.fsbno
!= NULLFSBLOCK
);
1219 ASSERT(args
.len
== 1);
1220 *firstblock
= args
.fsbno
;
1221 bp
= xfs_btree_get_bufl(args
.mp
, tp
, args
.fsbno
, 0);
1223 /* initialise the block and copy the data */
1224 init_fn(tp
, bp
, ip
, ifp
);
1226 /* account for the change in fork size and log everything */
1227 xfs_trans_log_buf(tp
, bp
, 0, ifp
->if_bytes
- 1);
1228 xfs_bmap_forkoff_reset(args
.mp
, ip
, whichfork
);
1229 xfs_idata_realloc(ip
, -ifp
->if_bytes
, whichfork
);
1230 xfs_iext_add(ifp
, 0, 1);
1231 ep
= xfs_iext_get_ext(ifp
, 0);
1232 xfs_bmbt_set_allf(ep
, 0, args
.fsbno
, 1, XFS_EXT_NORM
);
1233 trace_xfs_bmap_post_update(ip
, 0,
1234 whichfork
== XFS_ATTR_FORK
? BMAP_ATTRFORK
: 0,
1236 XFS_IFORK_NEXT_SET(ip
, whichfork
, 1);
1237 ip
->i_d
.di_nblocks
= 1;
1238 xfs_trans_mod_dquot_byino(tp
, ip
,
1239 XFS_TRANS_DQ_BCOUNT
, 1L);
1240 flags
|= xfs_ilog_fext(whichfork
);
1242 ASSERT(XFS_IFORK_NEXTENTS(ip
, whichfork
) == 0);
1243 xfs_bmap_forkoff_reset(ip
->i_mount
, ip
, whichfork
);
1245 ifp
->if_flags
&= ~XFS_IFINLINE
;
1246 ifp
->if_flags
|= XFS_IFEXTENTS
;
1247 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_EXTENTS
);
1248 flags
|= XFS_ILOG_CORE
;
1255 * Called from xfs_bmap_add_attrfork to handle btree format files.
1257 STATIC
int /* error */
1258 xfs_bmap_add_attrfork_btree(
1259 xfs_trans_t
*tp
, /* transaction pointer */
1260 xfs_inode_t
*ip
, /* incore inode pointer */
1261 xfs_fsblock_t
*firstblock
, /* first block allocated */
1262 xfs_bmap_free_t
*flist
, /* blocks to free at commit */
1263 int *flags
) /* inode logging flags */
1265 xfs_btree_cur_t
*cur
; /* btree cursor */
1266 int error
; /* error return value */
1267 xfs_mount_t
*mp
; /* file system mount struct */
1268 int stat
; /* newroot status */
1271 if (ip
->i_df
.if_broot_bytes
<= XFS_IFORK_DSIZE(ip
))
1272 *flags
|= XFS_ILOG_DBROOT
;
1274 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, XFS_DATA_FORK
);
1275 cur
->bc_private
.b
.flist
= flist
;
1276 cur
->bc_private
.b
.firstblock
= *firstblock
;
1277 if ((error
= xfs_bmbt_lookup_ge(cur
, 0, 0, 0, &stat
)))
1279 /* must be at least one entry */
1280 XFS_WANT_CORRUPTED_GOTO(stat
== 1, error0
);
1281 if ((error
= xfs_btree_new_iroot(cur
, flags
, &stat
)))
1284 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
1285 return XFS_ERROR(ENOSPC
);
1287 *firstblock
= cur
->bc_private
.b
.firstblock
;
1288 cur
->bc_private
.b
.allocated
= 0;
1289 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
1293 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
1298 * Called from xfs_bmap_add_attrfork to handle extents format files.
1300 STATIC
int /* error */
1301 xfs_bmap_add_attrfork_extents(
1302 xfs_trans_t
*tp
, /* transaction pointer */
1303 xfs_inode_t
*ip
, /* incore inode pointer */
1304 xfs_fsblock_t
*firstblock
, /* first block allocated */
1305 xfs_bmap_free_t
*flist
, /* blocks to free at commit */
1306 int *flags
) /* inode logging flags */
1308 xfs_btree_cur_t
*cur
; /* bmap btree cursor */
1309 int error
; /* error return value */
1311 if (ip
->i_d
.di_nextents
* sizeof(xfs_bmbt_rec_t
) <= XFS_IFORK_DSIZE(ip
))
1314 error
= xfs_bmap_extents_to_btree(tp
, ip
, firstblock
, flist
, &cur
, 0,
1315 flags
, XFS_DATA_FORK
);
1317 cur
->bc_private
.b
.allocated
= 0;
1318 xfs_btree_del_cursor(cur
,
1319 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
1325 * Block initialisation function for local to extent format conversion.
1327 * This shouldn't actually be called by anyone, so make sure debug kernels cause
1328 * a noticable failure.
1331 xfs_bmap_local_to_extents_init_fn(
1332 struct xfs_trans
*tp
,
1334 struct xfs_inode
*ip
,
1335 struct xfs_ifork
*ifp
)
1338 bp
->b_ops
= &xfs_bmbt_buf_ops
;
1339 memcpy(bp
->b_addr
, ifp
->if_u1
.if_data
, ifp
->if_bytes
);
1340 xfs_trans_buf_set_type(tp
, bp
, XFS_BLF_BTREE_BUF
);
1344 * Called from xfs_bmap_add_attrfork to handle local format files. Each
1345 * different data fork content type needs a different callout to do the
1346 * conversion. Some are basic and only require special block initialisation
1347 * callouts for the data formating, others (directories) are so specialised they
1348 * handle everything themselves.
1350 * XXX (dgc): investigate whether directory conversion can use the generic
1351 * formatting callout. It should be possible - it's just a very complex
1354 STATIC
int /* error */
1355 xfs_bmap_add_attrfork_local(
1356 xfs_trans_t
*tp
, /* transaction pointer */
1357 xfs_inode_t
*ip
, /* incore inode pointer */
1358 xfs_fsblock_t
*firstblock
, /* first block allocated */
1359 xfs_bmap_free_t
*flist
, /* blocks to free at commit */
1360 int *flags
) /* inode logging flags */
1362 xfs_da_args_t dargs
; /* args for dir/attr code */
1364 if (ip
->i_df
.if_bytes
<= XFS_IFORK_DSIZE(ip
))
1367 if (S_ISDIR(ip
->i_d
.di_mode
)) {
1368 memset(&dargs
, 0, sizeof(dargs
));
1370 dargs
.firstblock
= firstblock
;
1371 dargs
.flist
= flist
;
1372 dargs
.total
= ip
->i_mount
->m_dirblkfsbs
;
1373 dargs
.whichfork
= XFS_DATA_FORK
;
1375 return xfs_dir2_sf_to_block(&dargs
);
1378 if (S_ISLNK(ip
->i_d
.di_mode
))
1379 return xfs_bmap_local_to_extents(tp
, ip
, firstblock
, 1,
1380 flags
, XFS_DATA_FORK
,
1381 xfs_symlink_local_to_remote
);
1383 return xfs_bmap_local_to_extents(tp
, ip
, firstblock
, 1, flags
,
1385 xfs_bmap_local_to_extents_init_fn
);
1389 * Convert inode from non-attributed to attributed.
1390 * Must not be in a transaction, ip must not be locked.
1392 int /* error code */
1393 xfs_bmap_add_attrfork(
1394 xfs_inode_t
*ip
, /* incore inode pointer */
1395 int size
, /* space new attribute needs */
1396 int rsvd
) /* xact may use reserved blks */
1398 xfs_fsblock_t firstblock
; /* 1st block/ag allocated */
1399 xfs_bmap_free_t flist
; /* freed extent records */
1400 xfs_mount_t
*mp
; /* mount structure */
1401 xfs_trans_t
*tp
; /* transaction pointer */
1402 int blks
; /* space reservation */
1403 int version
= 1; /* superblock attr version */
1404 int committed
; /* xaction was committed */
1405 int logflags
; /* logging flags */
1406 int error
; /* error return value */
1408 ASSERT(XFS_IFORK_Q(ip
) == 0);
1411 ASSERT(!XFS_NOT_DQATTACHED(mp
, ip
));
1412 tp
= xfs_trans_alloc(mp
, XFS_TRANS_ADDAFORK
);
1413 blks
= XFS_ADDAFORK_SPACE_RES(mp
);
1415 tp
->t_flags
|= XFS_TRANS_RESERVE
;
1416 if ((error
= xfs_trans_reserve(tp
, blks
, XFS_ADDAFORK_LOG_RES(mp
), 0,
1417 XFS_TRANS_PERM_LOG_RES
, XFS_ADDAFORK_LOG_COUNT
)))
1419 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
1420 error
= xfs_trans_reserve_quota_nblks(tp
, ip
, blks
, 0, rsvd
?
1421 XFS_QMOPT_RES_REGBLKS
| XFS_QMOPT_FORCE_RES
:
1422 XFS_QMOPT_RES_REGBLKS
);
1424 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
1425 xfs_trans_cancel(tp
, XFS_TRANS_RELEASE_LOG_RES
);
1428 if (XFS_IFORK_Q(ip
))
1430 if (ip
->i_d
.di_aformat
!= XFS_DINODE_FMT_EXTENTS
) {
1432 * For inodes coming from pre-6.2 filesystems.
1434 ASSERT(ip
->i_d
.di_aformat
== 0);
1435 ip
->i_d
.di_aformat
= XFS_DINODE_FMT_EXTENTS
;
1437 ASSERT(ip
->i_d
.di_anextents
== 0);
1439 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
);
1440 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
1442 switch (ip
->i_d
.di_format
) {
1443 case XFS_DINODE_FMT_DEV
:
1444 ip
->i_d
.di_forkoff
= roundup(sizeof(xfs_dev_t
), 8) >> 3;
1446 case XFS_DINODE_FMT_UUID
:
1447 ip
->i_d
.di_forkoff
= roundup(sizeof(uuid_t
), 8) >> 3;
1449 case XFS_DINODE_FMT_LOCAL
:
1450 case XFS_DINODE_FMT_EXTENTS
:
1451 case XFS_DINODE_FMT_BTREE
:
1452 ip
->i_d
.di_forkoff
= xfs_attr_shortform_bytesfit(ip
, size
);
1453 if (!ip
->i_d
.di_forkoff
)
1454 ip
->i_d
.di_forkoff
= xfs_default_attroffset(ip
) >> 3;
1455 else if (mp
->m_flags
& XFS_MOUNT_ATTR2
)
1460 error
= XFS_ERROR(EINVAL
);
1464 ASSERT(ip
->i_afp
== NULL
);
1465 ip
->i_afp
= kmem_zone_zalloc(xfs_ifork_zone
, KM_SLEEP
);
1466 ip
->i_afp
->if_flags
= XFS_IFEXTENTS
;
1468 xfs_bmap_init(&flist
, &firstblock
);
1469 switch (ip
->i_d
.di_format
) {
1470 case XFS_DINODE_FMT_LOCAL
:
1471 error
= xfs_bmap_add_attrfork_local(tp
, ip
, &firstblock
, &flist
,
1474 case XFS_DINODE_FMT_EXTENTS
:
1475 error
= xfs_bmap_add_attrfork_extents(tp
, ip
, &firstblock
,
1478 case XFS_DINODE_FMT_BTREE
:
1479 error
= xfs_bmap_add_attrfork_btree(tp
, ip
, &firstblock
, &flist
,
1487 xfs_trans_log_inode(tp
, ip
, logflags
);
1490 if (!xfs_sb_version_hasattr(&mp
->m_sb
) ||
1491 (!xfs_sb_version_hasattr2(&mp
->m_sb
) && version
== 2)) {
1492 __int64_t sbfields
= 0;
1494 spin_lock(&mp
->m_sb_lock
);
1495 if (!xfs_sb_version_hasattr(&mp
->m_sb
)) {
1496 xfs_sb_version_addattr(&mp
->m_sb
);
1497 sbfields
|= XFS_SB_VERSIONNUM
;
1499 if (!xfs_sb_version_hasattr2(&mp
->m_sb
) && version
== 2) {
1500 xfs_sb_version_addattr2(&mp
->m_sb
);
1501 sbfields
|= (XFS_SB_VERSIONNUM
| XFS_SB_FEATURES2
);
1504 spin_unlock(&mp
->m_sb_lock
);
1505 xfs_mod_sb(tp
, sbfields
);
1507 spin_unlock(&mp
->m_sb_lock
);
1510 error
= xfs_bmap_finish(&tp
, &flist
, &committed
);
1513 return xfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
);
1515 xfs_bmap_cancel(&flist
);
1517 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
1519 xfs_trans_cancel(tp
, XFS_TRANS_RELEASE_LOG_RES
|XFS_TRANS_ABORT
);
1524 * Internal and external extent tree search functions.
1528 * Read in the extents to if_extents.
1529 * All inode fields are set up by caller, we just traverse the btree
1530 * and copy the records in. If the file system cannot contain unwritten
1531 * extents, the records are checked for no "state" flags.
1534 xfs_bmap_read_extents(
1535 xfs_trans_t
*tp
, /* transaction pointer */
1536 xfs_inode_t
*ip
, /* incore inode */
1537 int whichfork
) /* data or attr fork */
1539 struct xfs_btree_block
*block
; /* current btree block */
1540 xfs_fsblock_t bno
; /* block # of "block" */
1541 xfs_buf_t
*bp
; /* buffer for "block" */
1542 int error
; /* error return value */
1543 xfs_exntfmt_t exntf
; /* XFS_EXTFMT_NOSTATE, if checking */
1544 xfs_extnum_t i
, j
; /* index into the extents list */
1545 xfs_ifork_t
*ifp
; /* fork structure */
1546 int level
; /* btree level, for checking */
1547 xfs_mount_t
*mp
; /* file system mount structure */
1548 __be64
*pp
; /* pointer to block address */
1550 xfs_extnum_t room
; /* number of entries there's room for */
1554 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1555 exntf
= (whichfork
!= XFS_DATA_FORK
) ? XFS_EXTFMT_NOSTATE
:
1556 XFS_EXTFMT_INODE(ip
);
1557 block
= ifp
->if_broot
;
1559 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
1561 level
= be16_to_cpu(block
->bb_level
);
1563 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, 1, ifp
->if_broot_bytes
);
1564 bno
= be64_to_cpu(*pp
);
1565 ASSERT(bno
!= NULLDFSBNO
);
1566 ASSERT(XFS_FSB_TO_AGNO(mp
, bno
) < mp
->m_sb
.sb_agcount
);
1567 ASSERT(XFS_FSB_TO_AGBNO(mp
, bno
) < mp
->m_sb
.sb_agblocks
);
1569 * Go down the tree until leaf level is reached, following the first
1570 * pointer (leftmost) at each level.
1572 while (level
-- > 0) {
1573 error
= xfs_btree_read_bufl(mp
, tp
, bno
, 0, &bp
,
1574 XFS_BMAP_BTREE_REF
, &xfs_bmbt_buf_ops
);
1577 block
= XFS_BUF_TO_BLOCK(bp
);
1578 XFS_WANT_CORRUPTED_GOTO(
1579 xfs_bmap_sanity_check(mp
, bp
, level
),
1583 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, mp
->m_bmap_dmxr
[1]);
1584 bno
= be64_to_cpu(*pp
);
1585 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp
, bno
), error0
);
1586 xfs_trans_brelse(tp
, bp
);
1589 * Here with bp and block set to the leftmost leaf node in the tree.
1591 room
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
1594 * Loop over all leaf nodes. Copy information to the extent records.
1597 xfs_bmbt_rec_t
*frp
;
1598 xfs_fsblock_t nextbno
;
1599 xfs_extnum_t num_recs
;
1602 num_recs
= xfs_btree_get_numrecs(block
);
1603 if (unlikely(i
+ num_recs
> room
)) {
1604 ASSERT(i
+ num_recs
<= room
);
1605 xfs_warn(ip
->i_mount
,
1606 "corrupt dinode %Lu, (btree extents).",
1607 (unsigned long long) ip
->i_ino
);
1608 XFS_CORRUPTION_ERROR("xfs_bmap_read_extents(1)",
1609 XFS_ERRLEVEL_LOW
, ip
->i_mount
, block
);
1612 XFS_WANT_CORRUPTED_GOTO(
1613 xfs_bmap_sanity_check(mp
, bp
, 0),
1616 * Read-ahead the next leaf block, if any.
1618 nextbno
= be64_to_cpu(block
->bb_u
.l
.bb_rightsib
);
1619 if (nextbno
!= NULLFSBLOCK
)
1620 xfs_btree_reada_bufl(mp
, nextbno
, 1,
1623 * Copy records into the extent records.
1625 frp
= XFS_BMBT_REC_ADDR(mp
, block
, 1);
1627 for (j
= 0; j
< num_recs
; j
++, i
++, frp
++) {
1628 xfs_bmbt_rec_host_t
*trp
= xfs_iext_get_ext(ifp
, i
);
1629 trp
->l0
= be64_to_cpu(frp
->l0
);
1630 trp
->l1
= be64_to_cpu(frp
->l1
);
1632 if (exntf
== XFS_EXTFMT_NOSTATE
) {
1634 * Check all attribute bmap btree records and
1635 * any "older" data bmap btree records for a
1636 * set bit in the "extent flag" position.
1638 if (unlikely(xfs_check_nostate_extents(ifp
,
1639 start
, num_recs
))) {
1640 XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
1646 xfs_trans_brelse(tp
, bp
);
1649 * If we've reached the end, stop.
1651 if (bno
== NULLFSBLOCK
)
1653 error
= xfs_btree_read_bufl(mp
, tp
, bno
, 0, &bp
,
1654 XFS_BMAP_BTREE_REF
, &xfs_bmbt_buf_ops
);
1657 block
= XFS_BUF_TO_BLOCK(bp
);
1659 ASSERT(i
== (ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
)));
1660 ASSERT(i
== XFS_IFORK_NEXTENTS(ip
, whichfork
));
1661 XFS_BMAP_TRACE_EXLIST(ip
, i
, whichfork
);
1664 xfs_trans_brelse(tp
, bp
);
1665 return XFS_ERROR(EFSCORRUPTED
);
1670 * Search the extent records for the entry containing block bno.
1671 * If bno lies in a hole, point to the next entry. If bno lies
1672 * past eof, *eofp will be set, and *prevp will contain the last
1673 * entry (null if none). Else, *lastxp will be set to the index
1674 * of the found entry; *gotp will contain the entry.
1676 STATIC xfs_bmbt_rec_host_t
* /* pointer to found extent entry */
1677 xfs_bmap_search_multi_extents(
1678 xfs_ifork_t
*ifp
, /* inode fork pointer */
1679 xfs_fileoff_t bno
, /* block number searched for */
1680 int *eofp
, /* out: end of file found */
1681 xfs_extnum_t
*lastxp
, /* out: last extent index */
1682 xfs_bmbt_irec_t
*gotp
, /* out: extent entry found */
1683 xfs_bmbt_irec_t
*prevp
) /* out: previous extent entry found */
1685 xfs_bmbt_rec_host_t
*ep
; /* extent record pointer */
1686 xfs_extnum_t lastx
; /* last extent index */
1689 * Initialize the extent entry structure to catch access to
1690 * uninitialized br_startblock field.
1692 gotp
->br_startoff
= 0xffa5a5a5a5a5a5a5LL
;
1693 gotp
->br_blockcount
= 0xa55a5a5a5a5a5a5aLL
;
1694 gotp
->br_state
= XFS_EXT_INVALID
;
1696 gotp
->br_startblock
= 0xffffa5a5a5a5a5a5LL
;
1698 gotp
->br_startblock
= 0xffffa5a5;
1700 prevp
->br_startoff
= NULLFILEOFF
;
1702 ep
= xfs_iext_bno_to_ext(ifp
, bno
, &lastx
);
1704 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, lastx
- 1), prevp
);
1706 if (lastx
< (ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
))) {
1707 xfs_bmbt_get_all(ep
, gotp
);
1721 * Search the extents list for the inode, for the extent containing bno.
1722 * If bno lies in a hole, point to the next entry. If bno lies past eof,
1723 * *eofp will be set, and *prevp will contain the last entry (null if none).
1724 * Else, *lastxp will be set to the index of the found
1725 * entry; *gotp will contain the entry.
1727 STATIC xfs_bmbt_rec_host_t
* /* pointer to found extent entry */
1728 xfs_bmap_search_extents(
1729 xfs_inode_t
*ip
, /* incore inode pointer */
1730 xfs_fileoff_t bno
, /* block number searched for */
1731 int fork
, /* data or attr fork */
1732 int *eofp
, /* out: end of file found */
1733 xfs_extnum_t
*lastxp
, /* out: last extent index */
1734 xfs_bmbt_irec_t
*gotp
, /* out: extent entry found */
1735 xfs_bmbt_irec_t
*prevp
) /* out: previous extent entry found */
1737 xfs_ifork_t
*ifp
; /* inode fork pointer */
1738 xfs_bmbt_rec_host_t
*ep
; /* extent record pointer */
1740 XFS_STATS_INC(xs_look_exlist
);
1741 ifp
= XFS_IFORK_PTR(ip
, fork
);
1743 ep
= xfs_bmap_search_multi_extents(ifp
, bno
, eofp
, lastxp
, gotp
, prevp
);
1745 if (unlikely(!(gotp
->br_startblock
) && (*lastxp
!= NULLEXTNUM
) &&
1746 !(XFS_IS_REALTIME_INODE(ip
) && fork
== XFS_DATA_FORK
))) {
1747 xfs_alert_tag(ip
->i_mount
, XFS_PTAG_FSBLOCK_ZERO
,
1748 "Access to block zero in inode %llu "
1749 "start_block: %llx start_off: %llx "
1750 "blkcnt: %llx extent-state: %x lastx: %x\n",
1751 (unsigned long long)ip
->i_ino
,
1752 (unsigned long long)gotp
->br_startblock
,
1753 (unsigned long long)gotp
->br_startoff
,
1754 (unsigned long long)gotp
->br_blockcount
,
1755 gotp
->br_state
, *lastxp
);
1756 *lastxp
= NULLEXTNUM
;
1764 * Returns the file-relative block number of the first unused block(s)
1765 * in the file with at least "len" logically contiguous blocks free.
1766 * This is the lowest-address hole if the file has holes, else the first block
1767 * past the end of file.
1768 * Return 0 if the file is currently local (in-inode).
1771 xfs_bmap_first_unused(
1772 xfs_trans_t
*tp
, /* transaction pointer */
1773 xfs_inode_t
*ip
, /* incore inode */
1774 xfs_extlen_t len
, /* size of hole to find */
1775 xfs_fileoff_t
*first_unused
, /* unused block */
1776 int whichfork
) /* data or attr fork */
1778 int error
; /* error return value */
1779 int idx
; /* extent record index */
1780 xfs_ifork_t
*ifp
; /* inode fork pointer */
1781 xfs_fileoff_t lastaddr
; /* last block number seen */
1782 xfs_fileoff_t lowest
; /* lowest useful block */
1783 xfs_fileoff_t max
; /* starting useful block */
1784 xfs_fileoff_t off
; /* offset for this block */
1785 xfs_extnum_t nextents
; /* number of extent entries */
1787 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
||
1788 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
||
1789 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
1790 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
) {
1794 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1795 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
1796 (error
= xfs_iread_extents(tp
, ip
, whichfork
)))
1798 lowest
= *first_unused
;
1799 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
1800 for (idx
= 0, lastaddr
= 0, max
= lowest
; idx
< nextents
; idx
++) {
1801 xfs_bmbt_rec_host_t
*ep
= xfs_iext_get_ext(ifp
, idx
);
1802 off
= xfs_bmbt_get_startoff(ep
);
1804 * See if the hole before this extent will work.
1806 if (off
>= lowest
+ len
&& off
- max
>= len
) {
1807 *first_unused
= max
;
1810 lastaddr
= off
+ xfs_bmbt_get_blockcount(ep
);
1811 max
= XFS_FILEOFF_MAX(lastaddr
, lowest
);
1813 *first_unused
= max
;
1818 * Returns the file-relative block number of the last block + 1 before
1819 * last_block (input value) in the file.
1820 * This is not based on i_size, it is based on the extent records.
1821 * Returns 0 for local files, as they do not have extent records.
1824 xfs_bmap_last_before(
1825 xfs_trans_t
*tp
, /* transaction pointer */
1826 xfs_inode_t
*ip
, /* incore inode */
1827 xfs_fileoff_t
*last_block
, /* last block */
1828 int whichfork
) /* data or attr fork */
1830 xfs_fileoff_t bno
; /* input file offset */
1831 int eof
; /* hit end of file */
1832 xfs_bmbt_rec_host_t
*ep
; /* pointer to last extent */
1833 int error
; /* error return value */
1834 xfs_bmbt_irec_t got
; /* current extent value */
1835 xfs_ifork_t
*ifp
; /* inode fork pointer */
1836 xfs_extnum_t lastx
; /* last extent used */
1837 xfs_bmbt_irec_t prev
; /* previous extent value */
1839 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
&&
1840 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
1841 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_LOCAL
)
1842 return XFS_ERROR(EIO
);
1843 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
) {
1847 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1848 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
1849 (error
= xfs_iread_extents(tp
, ip
, whichfork
)))
1851 bno
= *last_block
- 1;
1852 ep
= xfs_bmap_search_extents(ip
, bno
, whichfork
, &eof
, &lastx
, &got
,
1854 if (eof
|| xfs_bmbt_get_startoff(ep
) > bno
) {
1855 if (prev
.br_startoff
== NULLFILEOFF
)
1858 *last_block
= prev
.br_startoff
+ prev
.br_blockcount
;
1861 * Otherwise *last_block is already the right answer.
1867 xfs_bmap_last_extent(
1868 struct xfs_trans
*tp
,
1869 struct xfs_inode
*ip
,
1871 struct xfs_bmbt_irec
*rec
,
1874 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1878 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
1879 error
= xfs_iread_extents(tp
, ip
, whichfork
);
1884 nextents
= ifp
->if_bytes
/ sizeof(xfs_bmbt_rec_t
);
1885 if (nextents
== 0) {
1890 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, nextents
- 1), rec
);
1896 * Check the last inode extent to determine whether this allocation will result
1897 * in blocks being allocated at the end of the file. When we allocate new data
1898 * blocks at the end of the file which do not start at the previous data block,
1899 * we will try to align the new blocks at stripe unit boundaries.
1901 * Returns 0 in bma->aeof if the file (fork) is empty as any new write will be
1902 * at, or past the EOF.
1906 struct xfs_bmalloca
*bma
,
1909 struct xfs_bmbt_irec rec
;
1914 error
= xfs_bmap_last_extent(NULL
, bma
->ip
, whichfork
, &rec
,
1916 if (error
|| is_empty
)
1920 * Check if we are allocation or past the last extent, or at least into
1921 * the last delayed allocated extent.
1923 bma
->aeof
= bma
->offset
>= rec
.br_startoff
+ rec
.br_blockcount
||
1924 (bma
->offset
>= rec
.br_startoff
&&
1925 isnullstartblock(rec
.br_startblock
));
1930 * Check if the endoff is outside the last extent. If so the caller will grow
1931 * the allocation to a stripe unit boundary. All offsets are considered outside
1932 * the end of file for an empty fork, so 1 is returned in *eof in that case.
1936 struct xfs_inode
*ip
,
1937 xfs_fileoff_t endoff
,
1941 struct xfs_bmbt_irec rec
;
1944 error
= xfs_bmap_last_extent(NULL
, ip
, whichfork
, &rec
, eof
);
1948 *eof
= endoff
>= rec
.br_startoff
+ rec
.br_blockcount
;
1953 * Returns the file-relative block number of the first block past eof in
1954 * the file. This is not based on i_size, it is based on the extent records.
1955 * Returns 0 for local files, as they do not have extent records.
1958 xfs_bmap_last_offset(
1959 struct xfs_trans
*tp
,
1960 struct xfs_inode
*ip
,
1961 xfs_fileoff_t
*last_block
,
1964 struct xfs_bmbt_irec rec
;
1970 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
)
1973 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
&&
1974 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
1975 return XFS_ERROR(EIO
);
1977 error
= xfs_bmap_last_extent(NULL
, ip
, whichfork
, &rec
, &is_empty
);
1978 if (error
|| is_empty
)
1981 *last_block
= rec
.br_startoff
+ rec
.br_blockcount
;
1986 * Returns whether the selected fork of the inode has exactly one
1987 * block or not. For the data fork we check this matches di_size,
1988 * implying the file's range is 0..bsize-1.
1990 int /* 1=>1 block, 0=>otherwise */
1992 xfs_inode_t
*ip
, /* incore inode */
1993 int whichfork
) /* data or attr fork */
1995 xfs_bmbt_rec_host_t
*ep
; /* ptr to fork's extent */
1996 xfs_ifork_t
*ifp
; /* inode fork pointer */
1997 int rval
; /* return value */
1998 xfs_bmbt_irec_t s
; /* internal version of extent */
2001 if (whichfork
== XFS_DATA_FORK
)
2002 return XFS_ISIZE(ip
) == ip
->i_mount
->m_sb
.sb_blocksize
;
2004 if (XFS_IFORK_NEXTENTS(ip
, whichfork
) != 1)
2006 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
2008 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
2009 ASSERT(ifp
->if_flags
& XFS_IFEXTENTS
);
2010 ep
= xfs_iext_get_ext(ifp
, 0);
2011 xfs_bmbt_get_all(ep
, &s
);
2012 rval
= s
.br_startoff
== 0 && s
.br_blockcount
== 1;
2013 if (rval
&& whichfork
== XFS_DATA_FORK
)
2014 ASSERT(XFS_ISIZE(ip
) == ip
->i_mount
->m_sb
.sb_blocksize
);
2019 * Extent tree manipulation functions used during allocation.
2023 * Convert a delayed allocation to a real allocation.
2025 STATIC
int /* error */
2026 xfs_bmap_add_extent_delay_real(
2027 struct xfs_bmalloca
*bma
)
2029 struct xfs_bmbt_irec
*new = &bma
->got
;
2030 int diff
; /* temp value */
2031 xfs_bmbt_rec_host_t
*ep
; /* extent entry for idx */
2032 int error
; /* error return value */
2033 int i
; /* temp state */
2034 xfs_ifork_t
*ifp
; /* inode fork pointer */
2035 xfs_fileoff_t new_endoff
; /* end offset of new entry */
2036 xfs_bmbt_irec_t r
[3]; /* neighbor extent entries */
2037 /* left is 0, right is 1, prev is 2 */
2038 int rval
=0; /* return value (logging flags) */
2039 int state
= 0;/* state bits, accessed thru macros */
2040 xfs_filblks_t da_new
; /* new count del alloc blocks used */
2041 xfs_filblks_t da_old
; /* old count del alloc blocks used */
2042 xfs_filblks_t temp
=0; /* value for da_new calculations */
2043 xfs_filblks_t temp2
=0;/* value for da_new calculations */
2044 int tmp_rval
; /* partial logging flags */
2046 ifp
= XFS_IFORK_PTR(bma
->ip
, XFS_DATA_FORK
);
2048 ASSERT(bma
->idx
>= 0);
2049 ASSERT(bma
->idx
<= ifp
->if_bytes
/ sizeof(struct xfs_bmbt_rec
));
2050 ASSERT(!isnullstartblock(new->br_startblock
));
2052 (bma
->cur
->bc_private
.b
.flags
& XFS_BTCUR_BPRV_WASDEL
));
2054 XFS_STATS_INC(xs_add_exlist
);
2061 * Set up a bunch of variables to make the tests simpler.
2063 ep
= xfs_iext_get_ext(ifp
, bma
->idx
);
2064 xfs_bmbt_get_all(ep
, &PREV
);
2065 new_endoff
= new->br_startoff
+ new->br_blockcount
;
2066 ASSERT(PREV
.br_startoff
<= new->br_startoff
);
2067 ASSERT(PREV
.br_startoff
+ PREV
.br_blockcount
>= new_endoff
);
2069 da_old
= startblockval(PREV
.br_startblock
);
2073 * Set flags determining what part of the previous delayed allocation
2074 * extent is being replaced by a real allocation.
2076 if (PREV
.br_startoff
== new->br_startoff
)
2077 state
|= BMAP_LEFT_FILLING
;
2078 if (PREV
.br_startoff
+ PREV
.br_blockcount
== new_endoff
)
2079 state
|= BMAP_RIGHT_FILLING
;
2082 * Check and set flags if this segment has a left neighbor.
2083 * Don't set contiguous if the combined extent would be too large.
2086 state
|= BMAP_LEFT_VALID
;
2087 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
- 1), &LEFT
);
2089 if (isnullstartblock(LEFT
.br_startblock
))
2090 state
|= BMAP_LEFT_DELAY
;
2093 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
2094 LEFT
.br_startoff
+ LEFT
.br_blockcount
== new->br_startoff
&&
2095 LEFT
.br_startblock
+ LEFT
.br_blockcount
== new->br_startblock
&&
2096 LEFT
.br_state
== new->br_state
&&
2097 LEFT
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
2098 state
|= BMAP_LEFT_CONTIG
;
2101 * Check and set flags if this segment has a right neighbor.
2102 * Don't set contiguous if the combined extent would be too large.
2103 * Also check for all-three-contiguous being too large.
2105 if (bma
->idx
< bma
->ip
->i_df
.if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
) - 1) {
2106 state
|= BMAP_RIGHT_VALID
;
2107 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
+ 1), &RIGHT
);
2109 if (isnullstartblock(RIGHT
.br_startblock
))
2110 state
|= BMAP_RIGHT_DELAY
;
2113 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
2114 new_endoff
== RIGHT
.br_startoff
&&
2115 new->br_startblock
+ new->br_blockcount
== RIGHT
.br_startblock
&&
2116 new->br_state
== RIGHT
.br_state
&&
2117 new->br_blockcount
+ RIGHT
.br_blockcount
<= MAXEXTLEN
&&
2118 ((state
& (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
2119 BMAP_RIGHT_FILLING
)) !=
2120 (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
2121 BMAP_RIGHT_FILLING
) ||
2122 LEFT
.br_blockcount
+ new->br_blockcount
+ RIGHT
.br_blockcount
2124 state
|= BMAP_RIGHT_CONTIG
;
2128 * Switch out based on the FILLING and CONTIG state bits.
2130 switch (state
& (BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
2131 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
)) {
2132 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
2133 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2135 * Filling in all of a previously delayed allocation extent.
2136 * The left and right neighbors are both contiguous with new.
2139 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2140 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, bma
->idx
),
2141 LEFT
.br_blockcount
+ PREV
.br_blockcount
+
2142 RIGHT
.br_blockcount
);
2143 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2145 xfs_iext_remove(bma
->ip
, bma
->idx
+ 1, 2, state
);
2146 bma
->ip
->i_d
.di_nextents
--;
2147 if (bma
->cur
== NULL
)
2148 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2150 rval
= XFS_ILOG_CORE
;
2151 error
= xfs_bmbt_lookup_eq(bma
->cur
, RIGHT
.br_startoff
,
2152 RIGHT
.br_startblock
,
2153 RIGHT
.br_blockcount
, &i
);
2156 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2157 error
= xfs_btree_delete(bma
->cur
, &i
);
2160 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2161 error
= xfs_btree_decrement(bma
->cur
, 0, &i
);
2164 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2165 error
= xfs_bmbt_update(bma
->cur
, LEFT
.br_startoff
,
2167 LEFT
.br_blockcount
+
2168 PREV
.br_blockcount
+
2169 RIGHT
.br_blockcount
, LEFT
.br_state
);
2175 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
2177 * Filling in all of a previously delayed allocation extent.
2178 * The left neighbor is contiguous, the right is not.
2182 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2183 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, bma
->idx
),
2184 LEFT
.br_blockcount
+ PREV
.br_blockcount
);
2185 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2187 xfs_iext_remove(bma
->ip
, bma
->idx
+ 1, 1, state
);
2188 if (bma
->cur
== NULL
)
2189 rval
= XFS_ILOG_DEXT
;
2192 error
= xfs_bmbt_lookup_eq(bma
->cur
, LEFT
.br_startoff
,
2193 LEFT
.br_startblock
, LEFT
.br_blockcount
,
2197 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2198 error
= xfs_bmbt_update(bma
->cur
, LEFT
.br_startoff
,
2200 LEFT
.br_blockcount
+
2201 PREV
.br_blockcount
, LEFT
.br_state
);
2207 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2209 * Filling in all of a previously delayed allocation extent.
2210 * The right neighbor is contiguous, the left is not.
2212 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2213 xfs_bmbt_set_startblock(ep
, new->br_startblock
);
2214 xfs_bmbt_set_blockcount(ep
,
2215 PREV
.br_blockcount
+ RIGHT
.br_blockcount
);
2216 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2218 xfs_iext_remove(bma
->ip
, bma
->idx
+ 1, 1, state
);
2219 if (bma
->cur
== NULL
)
2220 rval
= XFS_ILOG_DEXT
;
2223 error
= xfs_bmbt_lookup_eq(bma
->cur
, RIGHT
.br_startoff
,
2224 RIGHT
.br_startblock
,
2225 RIGHT
.br_blockcount
, &i
);
2228 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2229 error
= xfs_bmbt_update(bma
->cur
, PREV
.br_startoff
,
2231 PREV
.br_blockcount
+
2232 RIGHT
.br_blockcount
, PREV
.br_state
);
2238 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
2240 * Filling in all of a previously delayed allocation extent.
2241 * Neither the left nor right neighbors are contiguous with
2244 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2245 xfs_bmbt_set_startblock(ep
, new->br_startblock
);
2246 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2248 bma
->ip
->i_d
.di_nextents
++;
2249 if (bma
->cur
== NULL
)
2250 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2252 rval
= XFS_ILOG_CORE
;
2253 error
= xfs_bmbt_lookup_eq(bma
->cur
, new->br_startoff
,
2254 new->br_startblock
, new->br_blockcount
,
2258 XFS_WANT_CORRUPTED_GOTO(i
== 0, done
);
2259 bma
->cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
2260 error
= xfs_btree_insert(bma
->cur
, &i
);
2263 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2267 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
:
2269 * Filling in the first part of a previous delayed allocation.
2270 * The left neighbor is contiguous.
2272 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
- 1, state
, _THIS_IP_
);
2273 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, bma
->idx
- 1),
2274 LEFT
.br_blockcount
+ new->br_blockcount
);
2275 xfs_bmbt_set_startoff(ep
,
2276 PREV
.br_startoff
+ new->br_blockcount
);
2277 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
- 1, state
, _THIS_IP_
);
2279 temp
= PREV
.br_blockcount
- new->br_blockcount
;
2280 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2281 xfs_bmbt_set_blockcount(ep
, temp
);
2282 if (bma
->cur
== NULL
)
2283 rval
= XFS_ILOG_DEXT
;
2286 error
= xfs_bmbt_lookup_eq(bma
->cur
, LEFT
.br_startoff
,
2287 LEFT
.br_startblock
, LEFT
.br_blockcount
,
2291 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2292 error
= xfs_bmbt_update(bma
->cur
, LEFT
.br_startoff
,
2294 LEFT
.br_blockcount
+
2300 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
2301 startblockval(PREV
.br_startblock
));
2302 xfs_bmbt_set_startblock(ep
, nullstartblock(da_new
));
2303 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2308 case BMAP_LEFT_FILLING
:
2310 * Filling in the first part of a previous delayed allocation.
2311 * The left neighbor is not contiguous.
2313 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2314 xfs_bmbt_set_startoff(ep
, new_endoff
);
2315 temp
= PREV
.br_blockcount
- new->br_blockcount
;
2316 xfs_bmbt_set_blockcount(ep
, temp
);
2317 xfs_iext_insert(bma
->ip
, bma
->idx
, 1, new, state
);
2318 bma
->ip
->i_d
.di_nextents
++;
2319 if (bma
->cur
== NULL
)
2320 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2322 rval
= XFS_ILOG_CORE
;
2323 error
= xfs_bmbt_lookup_eq(bma
->cur
, new->br_startoff
,
2324 new->br_startblock
, new->br_blockcount
,
2328 XFS_WANT_CORRUPTED_GOTO(i
== 0, done
);
2329 bma
->cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
2330 error
= xfs_btree_insert(bma
->cur
, &i
);
2333 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2336 if (xfs_bmap_needs_btree(bma
->ip
, XFS_DATA_FORK
)) {
2337 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
2338 bma
->firstblock
, bma
->flist
,
2339 &bma
->cur
, 1, &tmp_rval
, XFS_DATA_FORK
);
2344 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
2345 startblockval(PREV
.br_startblock
) -
2346 (bma
->cur
? bma
->cur
->bc_private
.b
.allocated
: 0));
2347 ep
= xfs_iext_get_ext(ifp
, bma
->idx
+ 1);
2348 xfs_bmbt_set_startblock(ep
, nullstartblock(da_new
));
2349 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
+ 1, state
, _THIS_IP_
);
2352 case BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2354 * Filling in the last part of a previous delayed allocation.
2355 * The right neighbor is contiguous with the new allocation.
2357 temp
= PREV
.br_blockcount
- new->br_blockcount
;
2358 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
+ 1, state
, _THIS_IP_
);
2359 xfs_bmbt_set_blockcount(ep
, temp
);
2360 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp
, bma
->idx
+ 1),
2361 new->br_startoff
, new->br_startblock
,
2362 new->br_blockcount
+ RIGHT
.br_blockcount
,
2364 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
+ 1, state
, _THIS_IP_
);
2365 if (bma
->cur
== NULL
)
2366 rval
= XFS_ILOG_DEXT
;
2369 error
= xfs_bmbt_lookup_eq(bma
->cur
, RIGHT
.br_startoff
,
2370 RIGHT
.br_startblock
,
2371 RIGHT
.br_blockcount
, &i
);
2374 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2375 error
= xfs_bmbt_update(bma
->cur
, new->br_startoff
,
2377 new->br_blockcount
+
2378 RIGHT
.br_blockcount
,
2384 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
2385 startblockval(PREV
.br_startblock
));
2386 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2387 xfs_bmbt_set_startblock(ep
, nullstartblock(da_new
));
2388 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2393 case BMAP_RIGHT_FILLING
:
2395 * Filling in the last part of a previous delayed allocation.
2396 * The right neighbor is not contiguous.
2398 temp
= PREV
.br_blockcount
- new->br_blockcount
;
2399 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2400 xfs_bmbt_set_blockcount(ep
, temp
);
2401 xfs_iext_insert(bma
->ip
, bma
->idx
+ 1, 1, new, state
);
2402 bma
->ip
->i_d
.di_nextents
++;
2403 if (bma
->cur
== NULL
)
2404 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2406 rval
= XFS_ILOG_CORE
;
2407 error
= xfs_bmbt_lookup_eq(bma
->cur
, new->br_startoff
,
2408 new->br_startblock
, new->br_blockcount
,
2412 XFS_WANT_CORRUPTED_GOTO(i
== 0, done
);
2413 bma
->cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
2414 error
= xfs_btree_insert(bma
->cur
, &i
);
2417 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2420 if (xfs_bmap_needs_btree(bma
->ip
, XFS_DATA_FORK
)) {
2421 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
2422 bma
->firstblock
, bma
->flist
, &bma
->cur
, 1,
2423 &tmp_rval
, XFS_DATA_FORK
);
2428 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
2429 startblockval(PREV
.br_startblock
) -
2430 (bma
->cur
? bma
->cur
->bc_private
.b
.allocated
: 0));
2431 ep
= xfs_iext_get_ext(ifp
, bma
->idx
);
2432 xfs_bmbt_set_startblock(ep
, nullstartblock(da_new
));
2433 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2440 * Filling in the middle part of a previous delayed allocation.
2441 * Contiguity is impossible here.
2442 * This case is avoided almost all the time.
2444 * We start with a delayed allocation:
2446 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
2449 * and we are allocating:
2450 * +rrrrrrrrrrrrrrrrr+
2453 * and we set it up for insertion as:
2454 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
2456 * PREV @ idx LEFT RIGHT
2457 * inserted at idx + 1
2459 temp
= new->br_startoff
- PREV
.br_startoff
;
2460 temp2
= PREV
.br_startoff
+ PREV
.br_blockcount
- new_endoff
;
2461 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, 0, _THIS_IP_
);
2462 xfs_bmbt_set_blockcount(ep
, temp
); /* truncate PREV */
2464 RIGHT
.br_state
= PREV
.br_state
;
2465 RIGHT
.br_startblock
= nullstartblock(
2466 (int)xfs_bmap_worst_indlen(bma
->ip
, temp2
));
2467 RIGHT
.br_startoff
= new_endoff
;
2468 RIGHT
.br_blockcount
= temp2
;
2469 /* insert LEFT (r[0]) and RIGHT (r[1]) at the same time */
2470 xfs_iext_insert(bma
->ip
, bma
->idx
+ 1, 2, &LEFT
, state
);
2471 bma
->ip
->i_d
.di_nextents
++;
2472 if (bma
->cur
== NULL
)
2473 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2475 rval
= XFS_ILOG_CORE
;
2476 error
= xfs_bmbt_lookup_eq(bma
->cur
, new->br_startoff
,
2477 new->br_startblock
, new->br_blockcount
,
2481 XFS_WANT_CORRUPTED_GOTO(i
== 0, done
);
2482 bma
->cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
2483 error
= xfs_btree_insert(bma
->cur
, &i
);
2486 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2489 if (xfs_bmap_needs_btree(bma
->ip
, XFS_DATA_FORK
)) {
2490 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
2491 bma
->firstblock
, bma
->flist
, &bma
->cur
,
2492 1, &tmp_rval
, XFS_DATA_FORK
);
2497 temp
= xfs_bmap_worst_indlen(bma
->ip
, temp
);
2498 temp2
= xfs_bmap_worst_indlen(bma
->ip
, temp2
);
2499 diff
= (int)(temp
+ temp2
- startblockval(PREV
.br_startblock
) -
2500 (bma
->cur
? bma
->cur
->bc_private
.b
.allocated
: 0));
2502 error
= xfs_icsb_modify_counters(bma
->ip
->i_mount
,
2504 -((int64_t)diff
), 0);
2510 ep
= xfs_iext_get_ext(ifp
, bma
->idx
);
2511 xfs_bmbt_set_startblock(ep
, nullstartblock((int)temp
));
2512 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2513 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
+ 2, state
, _THIS_IP_
);
2514 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp
, bma
->idx
+ 2),
2515 nullstartblock((int)temp2
));
2516 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
+ 2, state
, _THIS_IP_
);
2519 da_new
= temp
+ temp2
;
2522 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2523 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2524 case BMAP_LEFT_FILLING
| BMAP_RIGHT_CONTIG
:
2525 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
2526 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2527 case BMAP_LEFT_CONTIG
:
2528 case BMAP_RIGHT_CONTIG
:
2530 * These cases are all impossible.
2535 /* convert to a btree if necessary */
2536 if (xfs_bmap_needs_btree(bma
->ip
, XFS_DATA_FORK
)) {
2537 int tmp_logflags
; /* partial log flag return val */
2539 ASSERT(bma
->cur
== NULL
);
2540 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
2541 bma
->firstblock
, bma
->flist
, &bma
->cur
,
2542 da_old
> 0, &tmp_logflags
, XFS_DATA_FORK
);
2543 bma
->logflags
|= tmp_logflags
;
2548 /* adjust for changes in reserved delayed indirect blocks */
2549 if (da_old
|| da_new
) {
2552 temp
+= bma
->cur
->bc_private
.b
.allocated
;
2553 ASSERT(temp
<= da_old
);
2555 xfs_icsb_modify_counters(bma
->ip
->i_mount
,
2557 (int64_t)(da_old
- temp
), 0);
2560 /* clear out the allocated field, done with it now in any case. */
2562 bma
->cur
->bc_private
.b
.allocated
= 0;
2564 xfs_bmap_check_leaf_extents(bma
->cur
, bma
->ip
, XFS_DATA_FORK
);
2566 bma
->logflags
|= rval
;
2574 * Convert an unwritten allocation to a real allocation or vice versa.
2576 STATIC
int /* error */
2577 xfs_bmap_add_extent_unwritten_real(
2578 struct xfs_trans
*tp
,
2579 xfs_inode_t
*ip
, /* incore inode pointer */
2580 xfs_extnum_t
*idx
, /* extent number to update/insert */
2581 xfs_btree_cur_t
**curp
, /* if *curp is null, not a btree */
2582 xfs_bmbt_irec_t
*new, /* new data to add to file extents */
2583 xfs_fsblock_t
*first
, /* pointer to firstblock variable */
2584 xfs_bmap_free_t
*flist
, /* list of extents to be freed */
2585 int *logflagsp
) /* inode logging flags */
2587 xfs_btree_cur_t
*cur
; /* btree cursor */
2588 xfs_bmbt_rec_host_t
*ep
; /* extent entry for idx */
2589 int error
; /* error return value */
2590 int i
; /* temp state */
2591 xfs_ifork_t
*ifp
; /* inode fork pointer */
2592 xfs_fileoff_t new_endoff
; /* end offset of new entry */
2593 xfs_exntst_t newext
; /* new extent state */
2594 xfs_exntst_t oldext
; /* old extent state */
2595 xfs_bmbt_irec_t r
[3]; /* neighbor extent entries */
2596 /* left is 0, right is 1, prev is 2 */
2597 int rval
=0; /* return value (logging flags) */
2598 int state
= 0;/* state bits, accessed thru macros */
2603 ifp
= XFS_IFORK_PTR(ip
, XFS_DATA_FORK
);
2606 ASSERT(*idx
<= ifp
->if_bytes
/ sizeof(struct xfs_bmbt_rec
));
2607 ASSERT(!isnullstartblock(new->br_startblock
));
2609 XFS_STATS_INC(xs_add_exlist
);
2616 * Set up a bunch of variables to make the tests simpler.
2619 ep
= xfs_iext_get_ext(ifp
, *idx
);
2620 xfs_bmbt_get_all(ep
, &PREV
);
2621 newext
= new->br_state
;
2622 oldext
= (newext
== XFS_EXT_UNWRITTEN
) ?
2623 XFS_EXT_NORM
: XFS_EXT_UNWRITTEN
;
2624 ASSERT(PREV
.br_state
== oldext
);
2625 new_endoff
= new->br_startoff
+ new->br_blockcount
;
2626 ASSERT(PREV
.br_startoff
<= new->br_startoff
);
2627 ASSERT(PREV
.br_startoff
+ PREV
.br_blockcount
>= new_endoff
);
2630 * Set flags determining what part of the previous oldext allocation
2631 * extent is being replaced by a newext allocation.
2633 if (PREV
.br_startoff
== new->br_startoff
)
2634 state
|= BMAP_LEFT_FILLING
;
2635 if (PREV
.br_startoff
+ PREV
.br_blockcount
== new_endoff
)
2636 state
|= BMAP_RIGHT_FILLING
;
2639 * Check and set flags if this segment has a left neighbor.
2640 * Don't set contiguous if the combined extent would be too large.
2643 state
|= BMAP_LEFT_VALID
;
2644 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, *idx
- 1), &LEFT
);
2646 if (isnullstartblock(LEFT
.br_startblock
))
2647 state
|= BMAP_LEFT_DELAY
;
2650 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
2651 LEFT
.br_startoff
+ LEFT
.br_blockcount
== new->br_startoff
&&
2652 LEFT
.br_startblock
+ LEFT
.br_blockcount
== new->br_startblock
&&
2653 LEFT
.br_state
== newext
&&
2654 LEFT
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
2655 state
|= BMAP_LEFT_CONTIG
;
2658 * Check and set flags if this segment has a right neighbor.
2659 * Don't set contiguous if the combined extent would be too large.
2660 * Also check for all-three-contiguous being too large.
2662 if (*idx
< ip
->i_df
.if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
) - 1) {
2663 state
|= BMAP_RIGHT_VALID
;
2664 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, *idx
+ 1), &RIGHT
);
2665 if (isnullstartblock(RIGHT
.br_startblock
))
2666 state
|= BMAP_RIGHT_DELAY
;
2669 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
2670 new_endoff
== RIGHT
.br_startoff
&&
2671 new->br_startblock
+ new->br_blockcount
== RIGHT
.br_startblock
&&
2672 newext
== RIGHT
.br_state
&&
2673 new->br_blockcount
+ RIGHT
.br_blockcount
<= MAXEXTLEN
&&
2674 ((state
& (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
2675 BMAP_RIGHT_FILLING
)) !=
2676 (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
2677 BMAP_RIGHT_FILLING
) ||
2678 LEFT
.br_blockcount
+ new->br_blockcount
+ RIGHT
.br_blockcount
2680 state
|= BMAP_RIGHT_CONTIG
;
2683 * Switch out based on the FILLING and CONTIG state bits.
2685 switch (state
& (BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
2686 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
)) {
2687 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
2688 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2690 * Setting all of a previous oldext extent to newext.
2691 * The left and right neighbors are both contiguous with new.
2695 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2696 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
),
2697 LEFT
.br_blockcount
+ PREV
.br_blockcount
+
2698 RIGHT
.br_blockcount
);
2699 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2701 xfs_iext_remove(ip
, *idx
+ 1, 2, state
);
2702 ip
->i_d
.di_nextents
-= 2;
2704 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2706 rval
= XFS_ILOG_CORE
;
2707 if ((error
= xfs_bmbt_lookup_eq(cur
, RIGHT
.br_startoff
,
2708 RIGHT
.br_startblock
,
2709 RIGHT
.br_blockcount
, &i
)))
2711 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2712 if ((error
= xfs_btree_delete(cur
, &i
)))
2714 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2715 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2717 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2718 if ((error
= xfs_btree_delete(cur
, &i
)))
2720 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2721 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2723 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2724 if ((error
= xfs_bmbt_update(cur
, LEFT
.br_startoff
,
2726 LEFT
.br_blockcount
+ PREV
.br_blockcount
+
2727 RIGHT
.br_blockcount
, LEFT
.br_state
)))
2732 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
2734 * Setting all of a previous oldext extent to newext.
2735 * The left neighbor is contiguous, the right is not.
2739 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2740 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
),
2741 LEFT
.br_blockcount
+ PREV
.br_blockcount
);
2742 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2744 xfs_iext_remove(ip
, *idx
+ 1, 1, state
);
2745 ip
->i_d
.di_nextents
--;
2747 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2749 rval
= XFS_ILOG_CORE
;
2750 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2751 PREV
.br_startblock
, PREV
.br_blockcount
,
2754 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2755 if ((error
= xfs_btree_delete(cur
, &i
)))
2757 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2758 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2760 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2761 if ((error
= xfs_bmbt_update(cur
, LEFT
.br_startoff
,
2763 LEFT
.br_blockcount
+ PREV
.br_blockcount
,
2769 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2771 * Setting all of a previous oldext extent to newext.
2772 * The right neighbor is contiguous, the left is not.
2774 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2775 xfs_bmbt_set_blockcount(ep
,
2776 PREV
.br_blockcount
+ RIGHT
.br_blockcount
);
2777 xfs_bmbt_set_state(ep
, newext
);
2778 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2779 xfs_iext_remove(ip
, *idx
+ 1, 1, state
);
2780 ip
->i_d
.di_nextents
--;
2782 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2784 rval
= XFS_ILOG_CORE
;
2785 if ((error
= xfs_bmbt_lookup_eq(cur
, RIGHT
.br_startoff
,
2786 RIGHT
.br_startblock
,
2787 RIGHT
.br_blockcount
, &i
)))
2789 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2790 if ((error
= xfs_btree_delete(cur
, &i
)))
2792 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2793 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2795 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2796 if ((error
= xfs_bmbt_update(cur
, new->br_startoff
,
2798 new->br_blockcount
+ RIGHT
.br_blockcount
,
2804 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
2806 * Setting all of a previous oldext extent to newext.
2807 * Neither the left nor right neighbors are contiguous with
2810 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2811 xfs_bmbt_set_state(ep
, newext
);
2812 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2815 rval
= XFS_ILOG_DEXT
;
2818 if ((error
= xfs_bmbt_lookup_eq(cur
, new->br_startoff
,
2819 new->br_startblock
, new->br_blockcount
,
2822 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2823 if ((error
= xfs_bmbt_update(cur
, new->br_startoff
,
2824 new->br_startblock
, new->br_blockcount
,
2830 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
:
2832 * Setting the first part of a previous oldext extent to newext.
2833 * The left neighbor is contiguous.
2835 trace_xfs_bmap_pre_update(ip
, *idx
- 1, state
, _THIS_IP_
);
2836 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
- 1),
2837 LEFT
.br_blockcount
+ new->br_blockcount
);
2838 xfs_bmbt_set_startoff(ep
,
2839 PREV
.br_startoff
+ new->br_blockcount
);
2840 trace_xfs_bmap_post_update(ip
, *idx
- 1, state
, _THIS_IP_
);
2842 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2843 xfs_bmbt_set_startblock(ep
,
2844 new->br_startblock
+ new->br_blockcount
);
2845 xfs_bmbt_set_blockcount(ep
,
2846 PREV
.br_blockcount
- new->br_blockcount
);
2847 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2852 rval
= XFS_ILOG_DEXT
;
2855 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2856 PREV
.br_startblock
, PREV
.br_blockcount
,
2859 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2860 if ((error
= xfs_bmbt_update(cur
,
2861 PREV
.br_startoff
+ new->br_blockcount
,
2862 PREV
.br_startblock
+ new->br_blockcount
,
2863 PREV
.br_blockcount
- new->br_blockcount
,
2866 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2868 error
= xfs_bmbt_update(cur
, LEFT
.br_startoff
,
2870 LEFT
.br_blockcount
+ new->br_blockcount
,
2877 case BMAP_LEFT_FILLING
:
2879 * Setting the first part of a previous oldext extent to newext.
2880 * The left neighbor is not contiguous.
2882 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2883 ASSERT(ep
&& xfs_bmbt_get_state(ep
) == oldext
);
2884 xfs_bmbt_set_startoff(ep
, new_endoff
);
2885 xfs_bmbt_set_blockcount(ep
,
2886 PREV
.br_blockcount
- new->br_blockcount
);
2887 xfs_bmbt_set_startblock(ep
,
2888 new->br_startblock
+ new->br_blockcount
);
2889 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2891 xfs_iext_insert(ip
, *idx
, 1, new, state
);
2892 ip
->i_d
.di_nextents
++;
2894 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2896 rval
= XFS_ILOG_CORE
;
2897 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2898 PREV
.br_startblock
, PREV
.br_blockcount
,
2901 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2902 if ((error
= xfs_bmbt_update(cur
,
2903 PREV
.br_startoff
+ new->br_blockcount
,
2904 PREV
.br_startblock
+ new->br_blockcount
,
2905 PREV
.br_blockcount
- new->br_blockcount
,
2908 cur
->bc_rec
.b
= *new;
2909 if ((error
= xfs_btree_insert(cur
, &i
)))
2911 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2915 case BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2917 * Setting the last part of a previous oldext extent to newext.
2918 * The right neighbor is contiguous with the new allocation.
2920 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2921 xfs_bmbt_set_blockcount(ep
,
2922 PREV
.br_blockcount
- new->br_blockcount
);
2923 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2927 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2928 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp
, *idx
),
2929 new->br_startoff
, new->br_startblock
,
2930 new->br_blockcount
+ RIGHT
.br_blockcount
, newext
);
2931 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2934 rval
= XFS_ILOG_DEXT
;
2937 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2939 PREV
.br_blockcount
, &i
)))
2941 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2942 if ((error
= xfs_bmbt_update(cur
, PREV
.br_startoff
,
2944 PREV
.br_blockcount
- new->br_blockcount
,
2947 if ((error
= xfs_btree_increment(cur
, 0, &i
)))
2949 if ((error
= xfs_bmbt_update(cur
, new->br_startoff
,
2951 new->br_blockcount
+ RIGHT
.br_blockcount
,
2957 case BMAP_RIGHT_FILLING
:
2959 * Setting the last part of a previous oldext extent to newext.
2960 * The right neighbor is not contiguous.
2962 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2963 xfs_bmbt_set_blockcount(ep
,
2964 PREV
.br_blockcount
- new->br_blockcount
);
2965 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2968 xfs_iext_insert(ip
, *idx
, 1, new, state
);
2970 ip
->i_d
.di_nextents
++;
2972 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2974 rval
= XFS_ILOG_CORE
;
2975 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2976 PREV
.br_startblock
, PREV
.br_blockcount
,
2979 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2980 if ((error
= xfs_bmbt_update(cur
, PREV
.br_startoff
,
2982 PREV
.br_blockcount
- new->br_blockcount
,
2985 if ((error
= xfs_bmbt_lookup_eq(cur
, new->br_startoff
,
2986 new->br_startblock
, new->br_blockcount
,
2989 XFS_WANT_CORRUPTED_GOTO(i
== 0, done
);
2990 cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
2991 if ((error
= xfs_btree_insert(cur
, &i
)))
2993 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2999 * Setting the middle part of a previous oldext extent to
3000 * newext. Contiguity is impossible here.
3001 * One extent becomes three extents.
3003 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
3004 xfs_bmbt_set_blockcount(ep
,
3005 new->br_startoff
- PREV
.br_startoff
);
3006 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
3009 r
[1].br_startoff
= new_endoff
;
3010 r
[1].br_blockcount
=
3011 PREV
.br_startoff
+ PREV
.br_blockcount
- new_endoff
;
3012 r
[1].br_startblock
= new->br_startblock
+ new->br_blockcount
;
3013 r
[1].br_state
= oldext
;
3016 xfs_iext_insert(ip
, *idx
, 2, &r
[0], state
);
3018 ip
->i_d
.di_nextents
+= 2;
3020 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
3022 rval
= XFS_ILOG_CORE
;
3023 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
3024 PREV
.br_startblock
, PREV
.br_blockcount
,
3027 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
3028 /* new right extent - oldext */
3029 if ((error
= xfs_bmbt_update(cur
, r
[1].br_startoff
,
3030 r
[1].br_startblock
, r
[1].br_blockcount
,
3033 /* new left extent - oldext */
3034 cur
->bc_rec
.b
= PREV
;
3035 cur
->bc_rec
.b
.br_blockcount
=
3036 new->br_startoff
- PREV
.br_startoff
;
3037 if ((error
= xfs_btree_insert(cur
, &i
)))
3039 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
3041 * Reset the cursor to the position of the new extent
3042 * we are about to insert as we can't trust it after
3043 * the previous insert.
3045 if ((error
= xfs_bmbt_lookup_eq(cur
, new->br_startoff
,
3046 new->br_startblock
, new->br_blockcount
,
3049 XFS_WANT_CORRUPTED_GOTO(i
== 0, done
);
3050 /* new middle extent - newext */
3051 cur
->bc_rec
.b
.br_state
= new->br_state
;
3052 if ((error
= xfs_btree_insert(cur
, &i
)))
3054 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
3058 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
3059 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
3060 case BMAP_LEFT_FILLING
| BMAP_RIGHT_CONTIG
:
3061 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
3062 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
3063 case BMAP_LEFT_CONTIG
:
3064 case BMAP_RIGHT_CONTIG
:
3066 * These cases are all impossible.
3071 /* convert to a btree if necessary */
3072 if (xfs_bmap_needs_btree(ip
, XFS_DATA_FORK
)) {
3073 int tmp_logflags
; /* partial log flag return val */
3075 ASSERT(cur
== NULL
);
3076 error
= xfs_bmap_extents_to_btree(tp
, ip
, first
, flist
, &cur
,
3077 0, &tmp_logflags
, XFS_DATA_FORK
);
3078 *logflagsp
|= tmp_logflags
;
3083 /* clear out the allocated field, done with it now in any case. */
3085 cur
->bc_private
.b
.allocated
= 0;
3089 xfs_bmap_check_leaf_extents(*curp
, ip
, XFS_DATA_FORK
);
3099 * Convert a hole to a delayed allocation.
3102 xfs_bmap_add_extent_hole_delay(
3103 xfs_inode_t
*ip
, /* incore inode pointer */
3104 xfs_extnum_t
*idx
, /* extent number to update/insert */
3105 xfs_bmbt_irec_t
*new) /* new data to add to file extents */
3107 xfs_ifork_t
*ifp
; /* inode fork pointer */
3108 xfs_bmbt_irec_t left
; /* left neighbor extent entry */
3109 xfs_filblks_t newlen
=0; /* new indirect size */
3110 xfs_filblks_t oldlen
=0; /* old indirect size */
3111 xfs_bmbt_irec_t right
; /* right neighbor extent entry */
3112 int state
; /* state bits, accessed thru macros */
3113 xfs_filblks_t temp
=0; /* temp for indirect calculations */
3115 ifp
= XFS_IFORK_PTR(ip
, XFS_DATA_FORK
);
3117 ASSERT(isnullstartblock(new->br_startblock
));
3120 * Check and set flags if this segment has a left neighbor
3123 state
|= BMAP_LEFT_VALID
;
3124 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, *idx
- 1), &left
);
3126 if (isnullstartblock(left
.br_startblock
))
3127 state
|= BMAP_LEFT_DELAY
;
3131 * Check and set flags if the current (right) segment exists.
3132 * If it doesn't exist, we're converting the hole at end-of-file.
3134 if (*idx
< ip
->i_df
.if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
)) {
3135 state
|= BMAP_RIGHT_VALID
;
3136 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, *idx
), &right
);
3138 if (isnullstartblock(right
.br_startblock
))
3139 state
|= BMAP_RIGHT_DELAY
;
3143 * Set contiguity flags on the left and right neighbors.
3144 * Don't let extents get too large, even if the pieces are contiguous.
3146 if ((state
& BMAP_LEFT_VALID
) && (state
& BMAP_LEFT_DELAY
) &&
3147 left
.br_startoff
+ left
.br_blockcount
== new->br_startoff
&&
3148 left
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
3149 state
|= BMAP_LEFT_CONTIG
;
3151 if ((state
& BMAP_RIGHT_VALID
) && (state
& BMAP_RIGHT_DELAY
) &&
3152 new->br_startoff
+ new->br_blockcount
== right
.br_startoff
&&
3153 new->br_blockcount
+ right
.br_blockcount
<= MAXEXTLEN
&&
3154 (!(state
& BMAP_LEFT_CONTIG
) ||
3155 (left
.br_blockcount
+ new->br_blockcount
+
3156 right
.br_blockcount
<= MAXEXTLEN
)))
3157 state
|= BMAP_RIGHT_CONTIG
;
3160 * Switch out based on the contiguity flags.
3162 switch (state
& (BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
)) {
3163 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
3165 * New allocation is contiguous with delayed allocations
3166 * on the left and on the right.
3167 * Merge all three into a single extent record.
3170 temp
= left
.br_blockcount
+ new->br_blockcount
+
3171 right
.br_blockcount
;
3173 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
3174 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
), temp
);
3175 oldlen
= startblockval(left
.br_startblock
) +
3176 startblockval(new->br_startblock
) +
3177 startblockval(right
.br_startblock
);
3178 newlen
= xfs_bmap_worst_indlen(ip
, temp
);
3179 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp
, *idx
),
3180 nullstartblock((int)newlen
));
3181 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
3183 xfs_iext_remove(ip
, *idx
+ 1, 1, state
);
3186 case BMAP_LEFT_CONTIG
:
3188 * New allocation is contiguous with a delayed allocation
3190 * Merge the new allocation with the left neighbor.
3193 temp
= left
.br_blockcount
+ new->br_blockcount
;
3195 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
3196 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
), temp
);
3197 oldlen
= startblockval(left
.br_startblock
) +
3198 startblockval(new->br_startblock
);
3199 newlen
= xfs_bmap_worst_indlen(ip
, temp
);
3200 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp
, *idx
),
3201 nullstartblock((int)newlen
));
3202 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
3205 case BMAP_RIGHT_CONTIG
:
3207 * New allocation is contiguous with a delayed allocation
3209 * Merge the new allocation with the right neighbor.
3211 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
3212 temp
= new->br_blockcount
+ right
.br_blockcount
;
3213 oldlen
= startblockval(new->br_startblock
) +
3214 startblockval(right
.br_startblock
);
3215 newlen
= xfs_bmap_worst_indlen(ip
, temp
);
3216 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp
, *idx
),
3218 nullstartblock((int)newlen
), temp
, right
.br_state
);
3219 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
3224 * New allocation is not contiguous with another
3225 * delayed allocation.
3226 * Insert a new entry.
3228 oldlen
= newlen
= 0;
3229 xfs_iext_insert(ip
, *idx
, 1, new, state
);
3232 if (oldlen
!= newlen
) {
3233 ASSERT(oldlen
> newlen
);
3234 xfs_icsb_modify_counters(ip
->i_mount
, XFS_SBS_FDBLOCKS
,
3235 (int64_t)(oldlen
- newlen
), 0);
3237 * Nothing to do for disk quota accounting here.
3243 * Convert a hole to a real allocation.
3245 STATIC
int /* error */
3246 xfs_bmap_add_extent_hole_real(
3247 struct xfs_bmalloca
*bma
,
3250 struct xfs_bmbt_irec
*new = &bma
->got
;
3251 int error
; /* error return value */
3252 int i
; /* temp state */
3253 xfs_ifork_t
*ifp
; /* inode fork pointer */
3254 xfs_bmbt_irec_t left
; /* left neighbor extent entry */
3255 xfs_bmbt_irec_t right
; /* right neighbor extent entry */
3256 int rval
=0; /* return value (logging flags) */
3257 int state
; /* state bits, accessed thru macros */
3259 ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
3261 ASSERT(bma
->idx
>= 0);
3262 ASSERT(bma
->idx
<= ifp
->if_bytes
/ sizeof(struct xfs_bmbt_rec
));
3263 ASSERT(!isnullstartblock(new->br_startblock
));
3265 !(bma
->cur
->bc_private
.b
.flags
& XFS_BTCUR_BPRV_WASDEL
));
3267 XFS_STATS_INC(xs_add_exlist
);
3270 if (whichfork
== XFS_ATTR_FORK
)
3271 state
|= BMAP_ATTRFORK
;
3274 * Check and set flags if this segment has a left neighbor.
3277 state
|= BMAP_LEFT_VALID
;
3278 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
- 1), &left
);
3279 if (isnullstartblock(left
.br_startblock
))
3280 state
|= BMAP_LEFT_DELAY
;
3284 * Check and set flags if this segment has a current value.
3285 * Not true if we're inserting into the "hole" at eof.
3287 if (bma
->idx
< ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
)) {
3288 state
|= BMAP_RIGHT_VALID
;
3289 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
), &right
);
3290 if (isnullstartblock(right
.br_startblock
))
3291 state
|= BMAP_RIGHT_DELAY
;
3295 * We're inserting a real allocation between "left" and "right".
3296 * Set the contiguity flags. Don't let extents get too large.
3298 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
3299 left
.br_startoff
+ left
.br_blockcount
== new->br_startoff
&&
3300 left
.br_startblock
+ left
.br_blockcount
== new->br_startblock
&&
3301 left
.br_state
== new->br_state
&&
3302 left
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
3303 state
|= BMAP_LEFT_CONTIG
;
3305 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
3306 new->br_startoff
+ new->br_blockcount
== right
.br_startoff
&&
3307 new->br_startblock
+ new->br_blockcount
== right
.br_startblock
&&
3308 new->br_state
== right
.br_state
&&
3309 new->br_blockcount
+ right
.br_blockcount
<= MAXEXTLEN
&&
3310 (!(state
& BMAP_LEFT_CONTIG
) ||
3311 left
.br_blockcount
+ new->br_blockcount
+
3312 right
.br_blockcount
<= MAXEXTLEN
))
3313 state
|= BMAP_RIGHT_CONTIG
;
3317 * Select which case we're in here, and implement it.
3319 switch (state
& (BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
)) {
3320 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
3322 * New allocation is contiguous with real allocations on the
3323 * left and on the right.
3324 * Merge all three into a single extent record.
3327 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
3328 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, bma
->idx
),
3329 left
.br_blockcount
+ new->br_blockcount
+
3330 right
.br_blockcount
);
3331 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
3333 xfs_iext_remove(bma
->ip
, bma
->idx
+ 1, 1, state
);
3335 XFS_IFORK_NEXT_SET(bma
->ip
, whichfork
,
3336 XFS_IFORK_NEXTENTS(bma
->ip
, whichfork
) - 1);
3337 if (bma
->cur
== NULL
) {
3338 rval
= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
3340 rval
= XFS_ILOG_CORE
;
3341 error
= xfs_bmbt_lookup_eq(bma
->cur
, right
.br_startoff
,
3342 right
.br_startblock
, right
.br_blockcount
,
3346 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
3347 error
= xfs_btree_delete(bma
->cur
, &i
);
3350 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
3351 error
= xfs_btree_decrement(bma
->cur
, 0, &i
);
3354 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
3355 error
= xfs_bmbt_update(bma
->cur
, left
.br_startoff
,
3357 left
.br_blockcount
+
3358 new->br_blockcount
+
3359 right
.br_blockcount
,
3366 case BMAP_LEFT_CONTIG
:
3368 * New allocation is contiguous with a real allocation
3370 * Merge the new allocation with the left neighbor.
3373 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
3374 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, bma
->idx
),
3375 left
.br_blockcount
+ new->br_blockcount
);
3376 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
3378 if (bma
->cur
== NULL
) {
3379 rval
= xfs_ilog_fext(whichfork
);
3382 error
= xfs_bmbt_lookup_eq(bma
->cur
, left
.br_startoff
,
3383 left
.br_startblock
, left
.br_blockcount
,
3387 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
3388 error
= xfs_bmbt_update(bma
->cur
, left
.br_startoff
,
3390 left
.br_blockcount
+
3398 case BMAP_RIGHT_CONTIG
:
3400 * New allocation is contiguous with a real allocation
3402 * Merge the new allocation with the right neighbor.
3404 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
3405 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp
, bma
->idx
),
3406 new->br_startoff
, new->br_startblock
,
3407 new->br_blockcount
+ right
.br_blockcount
,
3409 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
3411 if (bma
->cur
== NULL
) {
3412 rval
= xfs_ilog_fext(whichfork
);
3415 error
= xfs_bmbt_lookup_eq(bma
->cur
,
3417 right
.br_startblock
,
3418 right
.br_blockcount
, &i
);
3421 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
3422 error
= xfs_bmbt_update(bma
->cur
, new->br_startoff
,
3424 new->br_blockcount
+
3425 right
.br_blockcount
,
3434 * New allocation is not contiguous with another
3436 * Insert a new entry.
3438 xfs_iext_insert(bma
->ip
, bma
->idx
, 1, new, state
);
3439 XFS_IFORK_NEXT_SET(bma
->ip
, whichfork
,
3440 XFS_IFORK_NEXTENTS(bma
->ip
, whichfork
) + 1);
3441 if (bma
->cur
== NULL
) {
3442 rval
= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
3444 rval
= XFS_ILOG_CORE
;
3445 error
= xfs_bmbt_lookup_eq(bma
->cur
,
3448 new->br_blockcount
, &i
);
3451 XFS_WANT_CORRUPTED_GOTO(i
== 0, done
);
3452 bma
->cur
->bc_rec
.b
.br_state
= new->br_state
;
3453 error
= xfs_btree_insert(bma
->cur
, &i
);
3456 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
3461 /* convert to a btree if necessary */
3462 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
3463 int tmp_logflags
; /* partial log flag return val */
3465 ASSERT(bma
->cur
== NULL
);
3466 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
3467 bma
->firstblock
, bma
->flist
, &bma
->cur
,
3468 0, &tmp_logflags
, whichfork
);
3469 bma
->logflags
|= tmp_logflags
;
3474 /* clear out the allocated field, done with it now in any case. */
3476 bma
->cur
->bc_private
.b
.allocated
= 0;
3478 xfs_bmap_check_leaf_extents(bma
->cur
, bma
->ip
, whichfork
);
3480 bma
->logflags
|= rval
;
3485 * Functions used in the extent read, allocate and remove paths
3489 * Adjust the size of the new extent based on di_extsize and rt extsize.
3492 xfs_bmap_extsize_align(
3494 xfs_bmbt_irec_t
*gotp
, /* next extent pointer */
3495 xfs_bmbt_irec_t
*prevp
, /* previous extent pointer */
3496 xfs_extlen_t extsz
, /* align to this extent size */
3497 int rt
, /* is this a realtime inode? */
3498 int eof
, /* is extent at end-of-file? */
3499 int delay
, /* creating delalloc extent? */
3500 int convert
, /* overwriting unwritten extent? */
3501 xfs_fileoff_t
*offp
, /* in/out: aligned offset */
3502 xfs_extlen_t
*lenp
) /* in/out: aligned length */
3504 xfs_fileoff_t orig_off
; /* original offset */
3505 xfs_extlen_t orig_alen
; /* original length */
3506 xfs_fileoff_t orig_end
; /* original off+len */
3507 xfs_fileoff_t nexto
; /* next file offset */
3508 xfs_fileoff_t prevo
; /* previous file offset */
3509 xfs_fileoff_t align_off
; /* temp for offset */
3510 xfs_extlen_t align_alen
; /* temp for length */
3511 xfs_extlen_t temp
; /* temp for calculations */
3516 orig_off
= align_off
= *offp
;
3517 orig_alen
= align_alen
= *lenp
;
3518 orig_end
= orig_off
+ orig_alen
;
3521 * If this request overlaps an existing extent, then don't
3522 * attempt to perform any additional alignment.
3524 if (!delay
&& !eof
&&
3525 (orig_off
>= gotp
->br_startoff
) &&
3526 (orig_end
<= gotp
->br_startoff
+ gotp
->br_blockcount
)) {
3531 * If the file offset is unaligned vs. the extent size
3532 * we need to align it. This will be possible unless
3533 * the file was previously written with a kernel that didn't
3534 * perform this alignment, or if a truncate shot us in the
3537 temp
= do_mod(orig_off
, extsz
);
3543 * Same adjustment for the end of the requested area.
3545 if ((temp
= (align_alen
% extsz
))) {
3546 align_alen
+= extsz
- temp
;
3549 * If the previous block overlaps with this proposed allocation
3550 * then move the start forward without adjusting the length.
3552 if (prevp
->br_startoff
!= NULLFILEOFF
) {
3553 if (prevp
->br_startblock
== HOLESTARTBLOCK
)
3554 prevo
= prevp
->br_startoff
;
3556 prevo
= prevp
->br_startoff
+ prevp
->br_blockcount
;
3559 if (align_off
!= orig_off
&& align_off
< prevo
)
3562 * If the next block overlaps with this proposed allocation
3563 * then move the start back without adjusting the length,
3564 * but not before offset 0.
3565 * This may of course make the start overlap previous block,
3566 * and if we hit the offset 0 limit then the next block
3567 * can still overlap too.
3569 if (!eof
&& gotp
->br_startoff
!= NULLFILEOFF
) {
3570 if ((delay
&& gotp
->br_startblock
== HOLESTARTBLOCK
) ||
3571 (!delay
&& gotp
->br_startblock
== DELAYSTARTBLOCK
))
3572 nexto
= gotp
->br_startoff
+ gotp
->br_blockcount
;
3574 nexto
= gotp
->br_startoff
;
3576 nexto
= NULLFILEOFF
;
3578 align_off
+ align_alen
!= orig_end
&&
3579 align_off
+ align_alen
> nexto
)
3580 align_off
= nexto
> align_alen
? nexto
- align_alen
: 0;
3582 * If we're now overlapping the next or previous extent that
3583 * means we can't fit an extsz piece in this hole. Just move
3584 * the start forward to the first valid spot and set
3585 * the length so we hit the end.
3587 if (align_off
!= orig_off
&& align_off
< prevo
)
3589 if (align_off
+ align_alen
!= orig_end
&&
3590 align_off
+ align_alen
> nexto
&&
3591 nexto
!= NULLFILEOFF
) {
3592 ASSERT(nexto
> prevo
);
3593 align_alen
= nexto
- align_off
;
3597 * If realtime, and the result isn't a multiple of the realtime
3598 * extent size we need to remove blocks until it is.
3600 if (rt
&& (temp
= (align_alen
% mp
->m_sb
.sb_rextsize
))) {
3602 * We're not covering the original request, or
3603 * we won't be able to once we fix the length.
3605 if (orig_off
< align_off
||
3606 orig_end
> align_off
+ align_alen
||
3607 align_alen
- temp
< orig_alen
)
3608 return XFS_ERROR(EINVAL
);
3610 * Try to fix it by moving the start up.
3612 if (align_off
+ temp
<= orig_off
) {
3617 * Try to fix it by moving the end in.
3619 else if (align_off
+ align_alen
- temp
>= orig_end
)
3622 * Set the start to the minimum then trim the length.
3625 align_alen
-= orig_off
- align_off
;
3626 align_off
= orig_off
;
3627 align_alen
-= align_alen
% mp
->m_sb
.sb_rextsize
;
3630 * Result doesn't cover the request, fail it.
3632 if (orig_off
< align_off
|| orig_end
> align_off
+ align_alen
)
3633 return XFS_ERROR(EINVAL
);
3635 ASSERT(orig_off
>= align_off
);
3636 ASSERT(orig_end
<= align_off
+ align_alen
);
3640 if (!eof
&& gotp
->br_startoff
!= NULLFILEOFF
)
3641 ASSERT(align_off
+ align_alen
<= gotp
->br_startoff
);
3642 if (prevp
->br_startoff
!= NULLFILEOFF
)
3643 ASSERT(align_off
>= prevp
->br_startoff
+ prevp
->br_blockcount
);
3651 #define XFS_ALLOC_GAP_UNITS 4
3655 xfs_bmalloca_t
*ap
) /* bmap alloc argument struct */
3657 xfs_fsblock_t adjust
; /* adjustment to block numbers */
3658 xfs_agnumber_t fb_agno
; /* ag number of ap->firstblock */
3659 xfs_mount_t
*mp
; /* mount point structure */
3660 int nullfb
; /* true if ap->firstblock isn't set */
3661 int rt
; /* true if inode is realtime */
3663 #define ISVALID(x,y) \
3665 (x) < mp->m_sb.sb_rblocks : \
3666 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
3667 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
3668 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
3670 mp
= ap
->ip
->i_mount
;
3671 nullfb
= *ap
->firstblock
== NULLFSBLOCK
;
3672 rt
= XFS_IS_REALTIME_INODE(ap
->ip
) && ap
->userdata
;
3673 fb_agno
= nullfb
? NULLAGNUMBER
: XFS_FSB_TO_AGNO(mp
, *ap
->firstblock
);
3675 * If allocating at eof, and there's a previous real block,
3676 * try to use its last block as our starting point.
3678 if (ap
->eof
&& ap
->prev
.br_startoff
!= NULLFILEOFF
&&
3679 !isnullstartblock(ap
->prev
.br_startblock
) &&
3680 ISVALID(ap
->prev
.br_startblock
+ ap
->prev
.br_blockcount
,
3681 ap
->prev
.br_startblock
)) {
3682 ap
->blkno
= ap
->prev
.br_startblock
+ ap
->prev
.br_blockcount
;
3684 * Adjust for the gap between prevp and us.
3686 adjust
= ap
->offset
-
3687 (ap
->prev
.br_startoff
+ ap
->prev
.br_blockcount
);
3689 ISVALID(ap
->blkno
+ adjust
, ap
->prev
.br_startblock
))
3690 ap
->blkno
+= adjust
;
3693 * If not at eof, then compare the two neighbor blocks.
3694 * Figure out whether either one gives us a good starting point,
3695 * and pick the better one.
3697 else if (!ap
->eof
) {
3698 xfs_fsblock_t gotbno
; /* right side block number */
3699 xfs_fsblock_t gotdiff
=0; /* right side difference */
3700 xfs_fsblock_t prevbno
; /* left side block number */
3701 xfs_fsblock_t prevdiff
=0; /* left side difference */
3704 * If there's a previous (left) block, select a requested
3705 * start block based on it.
3707 if (ap
->prev
.br_startoff
!= NULLFILEOFF
&&
3708 !isnullstartblock(ap
->prev
.br_startblock
) &&
3709 (prevbno
= ap
->prev
.br_startblock
+
3710 ap
->prev
.br_blockcount
) &&
3711 ISVALID(prevbno
, ap
->prev
.br_startblock
)) {
3713 * Calculate gap to end of previous block.
3715 adjust
= prevdiff
= ap
->offset
-
3716 (ap
->prev
.br_startoff
+
3717 ap
->prev
.br_blockcount
);
3719 * Figure the startblock based on the previous block's
3720 * end and the gap size.
3722 * If the gap is large relative to the piece we're
3723 * allocating, or using it gives us an invalid block
3724 * number, then just use the end of the previous block.
3726 if (prevdiff
<= XFS_ALLOC_GAP_UNITS
* ap
->length
&&
3727 ISVALID(prevbno
+ prevdiff
,
3728 ap
->prev
.br_startblock
))
3733 * If the firstblock forbids it, can't use it,
3736 if (!rt
&& !nullfb
&&
3737 XFS_FSB_TO_AGNO(mp
, prevbno
) != fb_agno
)
3738 prevbno
= NULLFSBLOCK
;
3741 * No previous block or can't follow it, just default.
3744 prevbno
= NULLFSBLOCK
;
3746 * If there's a following (right) block, select a requested
3747 * start block based on it.
3749 if (!isnullstartblock(ap
->got
.br_startblock
)) {
3751 * Calculate gap to start of next block.
3753 adjust
= gotdiff
= ap
->got
.br_startoff
- ap
->offset
;
3755 * Figure the startblock based on the next block's
3756 * start and the gap size.
3758 gotbno
= ap
->got
.br_startblock
;
3761 * If the gap is large relative to the piece we're
3762 * allocating, or using it gives us an invalid block
3763 * number, then just use the start of the next block
3764 * offset by our length.
3766 if (gotdiff
<= XFS_ALLOC_GAP_UNITS
* ap
->length
&&
3767 ISVALID(gotbno
- gotdiff
, gotbno
))
3769 else if (ISVALID(gotbno
- ap
->length
, gotbno
)) {
3770 gotbno
-= ap
->length
;
3771 gotdiff
+= adjust
- ap
->length
;
3775 * If the firstblock forbids it, can't use it,
3778 if (!rt
&& !nullfb
&&
3779 XFS_FSB_TO_AGNO(mp
, gotbno
) != fb_agno
)
3780 gotbno
= NULLFSBLOCK
;
3783 * No next block, just default.
3786 gotbno
= NULLFSBLOCK
;
3788 * If both valid, pick the better one, else the only good
3789 * one, else ap->blkno is already set (to 0 or the inode block).
3791 if (prevbno
!= NULLFSBLOCK
&& gotbno
!= NULLFSBLOCK
)
3792 ap
->blkno
= prevdiff
<= gotdiff
? prevbno
: gotbno
;
3793 else if (prevbno
!= NULLFSBLOCK
)
3794 ap
->blkno
= prevbno
;
3795 else if (gotbno
!= NULLFSBLOCK
)
3803 xfs_bmalloca_t
*ap
) /* bmap alloc argument struct */
3805 xfs_alloctype_t atype
= 0; /* type for allocation routines */
3806 int error
; /* error return value */
3807 xfs_mount_t
*mp
; /* mount point structure */
3808 xfs_extlen_t prod
= 0; /* product factor for allocators */
3809 xfs_extlen_t ralen
= 0; /* realtime allocation length */
3810 xfs_extlen_t align
; /* minimum allocation alignment */
3813 mp
= ap
->ip
->i_mount
;
3814 align
= xfs_get_extsz_hint(ap
->ip
);
3815 prod
= align
/ mp
->m_sb
.sb_rextsize
;
3816 error
= xfs_bmap_extsize_align(mp
, &ap
->got
, &ap
->prev
,
3817 align
, 1, ap
->eof
, 0,
3818 ap
->conv
, &ap
->offset
, &ap
->length
);
3822 ASSERT(ap
->length
% mp
->m_sb
.sb_rextsize
== 0);
3825 * If the offset & length are not perfectly aligned
3826 * then kill prod, it will just get us in trouble.
3828 if (do_mod(ap
->offset
, align
) || ap
->length
% align
)
3831 * Set ralen to be the actual requested length in rtextents.
3833 ralen
= ap
->length
/ mp
->m_sb
.sb_rextsize
;
3835 * If the old value was close enough to MAXEXTLEN that
3836 * we rounded up to it, cut it back so it's valid again.
3837 * Note that if it's a really large request (bigger than
3838 * MAXEXTLEN), we don't hear about that number, and can't
3839 * adjust the starting point to match it.
3841 if (ralen
* mp
->m_sb
.sb_rextsize
>= MAXEXTLEN
)
3842 ralen
= MAXEXTLEN
/ mp
->m_sb
.sb_rextsize
;
3845 * Lock out other modifications to the RT bitmap inode.
3847 xfs_ilock(mp
->m_rbmip
, XFS_ILOCK_EXCL
);
3848 xfs_trans_ijoin(ap
->tp
, mp
->m_rbmip
, XFS_ILOCK_EXCL
);
3851 * If it's an allocation to an empty file at offset 0,
3852 * pick an extent that will space things out in the rt area.
3854 if (ap
->eof
&& ap
->offset
== 0) {
3855 xfs_rtblock_t
uninitialized_var(rtx
); /* realtime extent no */
3857 error
= xfs_rtpick_extent(mp
, ap
->tp
, ralen
, &rtx
);
3860 ap
->blkno
= rtx
* mp
->m_sb
.sb_rextsize
;
3865 xfs_bmap_adjacent(ap
);
3868 * Realtime allocation, done through xfs_rtallocate_extent.
3870 atype
= ap
->blkno
== 0 ? XFS_ALLOCTYPE_ANY_AG
: XFS_ALLOCTYPE_NEAR_BNO
;
3871 do_div(ap
->blkno
, mp
->m_sb
.sb_rextsize
);
3874 if ((error
= xfs_rtallocate_extent(ap
->tp
, ap
->blkno
, 1, ap
->length
,
3875 &ralen
, atype
, ap
->wasdel
, prod
, &rtb
)))
3877 if (rtb
== NULLFSBLOCK
&& prod
> 1 &&
3878 (error
= xfs_rtallocate_extent(ap
->tp
, ap
->blkno
, 1,
3879 ap
->length
, &ralen
, atype
,
3880 ap
->wasdel
, 1, &rtb
)))
3883 if (ap
->blkno
!= NULLFSBLOCK
) {
3884 ap
->blkno
*= mp
->m_sb
.sb_rextsize
;
3885 ralen
*= mp
->m_sb
.sb_rextsize
;
3887 ap
->ip
->i_d
.di_nblocks
+= ralen
;
3888 xfs_trans_log_inode(ap
->tp
, ap
->ip
, XFS_ILOG_CORE
);
3890 ap
->ip
->i_delayed_blks
-= ralen
;
3892 * Adjust the disk quota also. This was reserved
3895 xfs_trans_mod_dquot_byino(ap
->tp
, ap
->ip
,
3896 ap
->wasdel
? XFS_TRANS_DQ_DELRTBCOUNT
:
3897 XFS_TRANS_DQ_RTBCOUNT
, (long) ralen
);
3905 xfs_bmap_btalloc_nullfb(
3906 struct xfs_bmalloca
*ap
,
3907 struct xfs_alloc_arg
*args
,
3910 struct xfs_mount
*mp
= ap
->ip
->i_mount
;
3911 struct xfs_perag
*pag
;
3912 xfs_agnumber_t ag
, startag
;
3916 if (ap
->userdata
&& xfs_inode_is_filestream(ap
->ip
))
3917 args
->type
= XFS_ALLOCTYPE_NEAR_BNO
;
3919 args
->type
= XFS_ALLOCTYPE_START_BNO
;
3920 args
->total
= ap
->total
;
3923 * Search for an allocation group with a single extent large enough
3924 * for the request. If one isn't found, then adjust the minimum
3925 * allocation size to the largest space found.
3927 startag
= ag
= XFS_FSB_TO_AGNO(mp
, args
->fsbno
);
3928 if (startag
== NULLAGNUMBER
)
3931 pag
= xfs_perag_get(mp
, ag
);
3932 while (*blen
< args
->maxlen
) {
3933 if (!pag
->pagf_init
) {
3934 error
= xfs_alloc_pagf_init(mp
, args
->tp
, ag
,
3935 XFS_ALLOC_FLAG_TRYLOCK
);
3943 * See xfs_alloc_fix_freelist...
3945 if (pag
->pagf_init
) {
3946 xfs_extlen_t longest
;
3947 longest
= xfs_alloc_longest_free_extent(mp
, pag
);
3948 if (*blen
< longest
)
3953 if (xfs_inode_is_filestream(ap
->ip
)) {
3954 if (*blen
>= args
->maxlen
)
3959 * If startag is an invalid AG, we've
3960 * come here once before and
3961 * xfs_filestream_new_ag picked the
3962 * best currently available.
3964 * Don't continue looping, since we
3965 * could loop forever.
3967 if (startag
== NULLAGNUMBER
)
3970 error
= xfs_filestream_new_ag(ap
, &ag
);
3975 /* loop again to set 'blen'*/
3976 startag
= NULLAGNUMBER
;
3977 pag
= xfs_perag_get(mp
, ag
);
3981 if (++ag
== mp
->m_sb
.sb_agcount
)
3986 pag
= xfs_perag_get(mp
, ag
);
3991 * Since the above loop did a BUF_TRYLOCK, it is
3992 * possible that there is space for this request.
3994 if (notinit
|| *blen
< ap
->minlen
)
3995 args
->minlen
= ap
->minlen
;
3997 * If the best seen length is less than the request
3998 * length, use the best as the minimum.
4000 else if (*blen
< args
->maxlen
)
4001 args
->minlen
= *blen
;
4003 * Otherwise we've seen an extent as big as maxlen,
4004 * use that as the minimum.
4007 args
->minlen
= args
->maxlen
;
4010 * set the failure fallback case to look in the selected
4011 * AG as the stream may have moved.
4013 if (xfs_inode_is_filestream(ap
->ip
))
4014 ap
->blkno
= args
->fsbno
= XFS_AGB_TO_FSB(mp
, ag
, 0);
4021 xfs_bmalloca_t
*ap
) /* bmap alloc argument struct */
4023 xfs_mount_t
*mp
; /* mount point structure */
4024 xfs_alloctype_t atype
= 0; /* type for allocation routines */
4025 xfs_extlen_t align
; /* minimum allocation alignment */
4026 xfs_agnumber_t fb_agno
; /* ag number of ap->firstblock */
4028 xfs_alloc_arg_t args
;
4030 xfs_extlen_t nextminlen
= 0;
4031 int nullfb
; /* true if ap->firstblock isn't set */
4038 mp
= ap
->ip
->i_mount
;
4039 align
= ap
->userdata
? xfs_get_extsz_hint(ap
->ip
) : 0;
4040 if (unlikely(align
)) {
4041 error
= xfs_bmap_extsize_align(mp
, &ap
->got
, &ap
->prev
,
4042 align
, 0, ap
->eof
, 0, ap
->conv
,
4043 &ap
->offset
, &ap
->length
);
4047 nullfb
= *ap
->firstblock
== NULLFSBLOCK
;
4048 fb_agno
= nullfb
? NULLAGNUMBER
: XFS_FSB_TO_AGNO(mp
, *ap
->firstblock
);
4050 if (ap
->userdata
&& xfs_inode_is_filestream(ap
->ip
)) {
4051 ag
= xfs_filestream_lookup_ag(ap
->ip
);
4052 ag
= (ag
!= NULLAGNUMBER
) ? ag
: 0;
4053 ap
->blkno
= XFS_AGB_TO_FSB(mp
, ag
, 0);
4055 ap
->blkno
= XFS_INO_TO_FSB(mp
, ap
->ip
->i_ino
);
4058 ap
->blkno
= *ap
->firstblock
;
4060 xfs_bmap_adjacent(ap
);
4063 * If allowed, use ap->blkno; otherwise must use firstblock since
4064 * it's in the right allocation group.
4066 if (nullfb
|| XFS_FSB_TO_AGNO(mp
, ap
->blkno
) == fb_agno
)
4069 ap
->blkno
= *ap
->firstblock
;
4071 * Normal allocation, done through xfs_alloc_vextent.
4073 tryagain
= isaligned
= 0;
4074 memset(&args
, 0, sizeof(args
));
4077 args
.fsbno
= ap
->blkno
;
4079 /* Trim the allocation back to the maximum an AG can fit. */
4080 args
.maxlen
= MIN(ap
->length
, XFS_ALLOC_AG_MAX_USABLE(mp
));
4081 args
.firstblock
= *ap
->firstblock
;
4084 error
= xfs_bmap_btalloc_nullfb(ap
, &args
, &blen
);
4087 } else if (ap
->flist
->xbf_low
) {
4088 if (xfs_inode_is_filestream(ap
->ip
))
4089 args
.type
= XFS_ALLOCTYPE_FIRST_AG
;
4091 args
.type
= XFS_ALLOCTYPE_START_BNO
;
4092 args
.total
= args
.minlen
= ap
->minlen
;
4094 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
4095 args
.total
= ap
->total
;
4096 args
.minlen
= ap
->minlen
;
4098 /* apply extent size hints if obtained earlier */
4099 if (unlikely(align
)) {
4101 if ((args
.mod
= (xfs_extlen_t
)do_mod(ap
->offset
, args
.prod
)))
4102 args
.mod
= (xfs_extlen_t
)(args
.prod
- args
.mod
);
4103 } else if (mp
->m_sb
.sb_blocksize
>= PAGE_CACHE_SIZE
) {
4107 args
.prod
= PAGE_CACHE_SIZE
>> mp
->m_sb
.sb_blocklog
;
4108 if ((args
.mod
= (xfs_extlen_t
)(do_mod(ap
->offset
, args
.prod
))))
4109 args
.mod
= (xfs_extlen_t
)(args
.prod
- args
.mod
);
4112 * If we are not low on available data blocks, and the
4113 * underlying logical volume manager is a stripe, and
4114 * the file offset is zero then try to allocate data
4115 * blocks on stripe unit boundary.
4116 * NOTE: ap->aeof is only set if the allocation length
4117 * is >= the stripe unit and the allocation offset is
4118 * at the end of file.
4120 if (!ap
->flist
->xbf_low
&& ap
->aeof
) {
4122 args
.alignment
= mp
->m_dalign
;
4126 * Adjust for alignment
4128 if (blen
> args
.alignment
&& blen
<= args
.maxlen
)
4129 args
.minlen
= blen
- args
.alignment
;
4130 args
.minalignslop
= 0;
4133 * First try an exact bno allocation.
4134 * If it fails then do a near or start bno
4135 * allocation with alignment turned on.
4139 args
.type
= XFS_ALLOCTYPE_THIS_BNO
;
4142 * Compute the minlen+alignment for the
4143 * next case. Set slop so that the value
4144 * of minlen+alignment+slop doesn't go up
4145 * between the calls.
4147 if (blen
> mp
->m_dalign
&& blen
<= args
.maxlen
)
4148 nextminlen
= blen
- mp
->m_dalign
;
4150 nextminlen
= args
.minlen
;
4151 if (nextminlen
+ mp
->m_dalign
> args
.minlen
+ 1)
4153 nextminlen
+ mp
->m_dalign
-
4156 args
.minalignslop
= 0;
4160 args
.minalignslop
= 0;
4162 args
.minleft
= ap
->minleft
;
4163 args
.wasdel
= ap
->wasdel
;
4165 args
.userdata
= ap
->userdata
;
4166 if ((error
= xfs_alloc_vextent(&args
)))
4168 if (tryagain
&& args
.fsbno
== NULLFSBLOCK
) {
4170 * Exact allocation failed. Now try with alignment
4174 args
.fsbno
= ap
->blkno
;
4175 args
.alignment
= mp
->m_dalign
;
4176 args
.minlen
= nextminlen
;
4177 args
.minalignslop
= 0;
4179 if ((error
= xfs_alloc_vextent(&args
)))
4182 if (isaligned
&& args
.fsbno
== NULLFSBLOCK
) {
4184 * allocation failed, so turn off alignment and
4188 args
.fsbno
= ap
->blkno
;
4190 if ((error
= xfs_alloc_vextent(&args
)))
4193 if (args
.fsbno
== NULLFSBLOCK
&& nullfb
&&
4194 args
.minlen
> ap
->minlen
) {
4195 args
.minlen
= ap
->minlen
;
4196 args
.type
= XFS_ALLOCTYPE_START_BNO
;
4197 args
.fsbno
= ap
->blkno
;
4198 if ((error
= xfs_alloc_vextent(&args
)))
4201 if (args
.fsbno
== NULLFSBLOCK
&& nullfb
) {
4203 args
.type
= XFS_ALLOCTYPE_FIRST_AG
;
4204 args
.total
= ap
->minlen
;
4206 if ((error
= xfs_alloc_vextent(&args
)))
4208 ap
->flist
->xbf_low
= 1;
4210 if (args
.fsbno
!= NULLFSBLOCK
) {
4212 * check the allocation happened at the same or higher AG than
4213 * the first block that was allocated.
4215 ASSERT(*ap
->firstblock
== NULLFSBLOCK
||
4216 XFS_FSB_TO_AGNO(mp
, *ap
->firstblock
) ==
4217 XFS_FSB_TO_AGNO(mp
, args
.fsbno
) ||
4218 (ap
->flist
->xbf_low
&&
4219 XFS_FSB_TO_AGNO(mp
, *ap
->firstblock
) <
4220 XFS_FSB_TO_AGNO(mp
, args
.fsbno
)));
4222 ap
->blkno
= args
.fsbno
;
4223 if (*ap
->firstblock
== NULLFSBLOCK
)
4224 *ap
->firstblock
= args
.fsbno
;
4225 ASSERT(nullfb
|| fb_agno
== args
.agno
||
4226 (ap
->flist
->xbf_low
&& fb_agno
< args
.agno
));
4227 ap
->length
= args
.len
;
4228 ap
->ip
->i_d
.di_nblocks
+= args
.len
;
4229 xfs_trans_log_inode(ap
->tp
, ap
->ip
, XFS_ILOG_CORE
);
4231 ap
->ip
->i_delayed_blks
-= args
.len
;
4233 * Adjust the disk quota also. This was reserved
4236 xfs_trans_mod_dquot_byino(ap
->tp
, ap
->ip
,
4237 ap
->wasdel
? XFS_TRANS_DQ_DELBCOUNT
:
4238 XFS_TRANS_DQ_BCOUNT
,
4241 ap
->blkno
= NULLFSBLOCK
;
4248 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
4249 * It figures out where to ask the underlying allocator to put the new extent.
4253 xfs_bmalloca_t
*ap
) /* bmap alloc argument struct */
4255 if (XFS_IS_REALTIME_INODE(ap
->ip
) && ap
->userdata
)
4256 return xfs_bmap_rtalloc(ap
);
4257 return xfs_bmap_btalloc(ap
);
4261 * Trim the returned map to the required bounds
4265 struct xfs_bmbt_irec
*mval
,
4266 struct xfs_bmbt_irec
*got
,
4274 if ((flags
& XFS_BMAPI_ENTIRE
) ||
4275 got
->br_startoff
+ got
->br_blockcount
<= obno
) {
4277 if (isnullstartblock(got
->br_startblock
))
4278 mval
->br_startblock
= DELAYSTARTBLOCK
;
4284 ASSERT((*bno
>= obno
) || (n
== 0));
4286 mval
->br_startoff
= *bno
;
4287 if (isnullstartblock(got
->br_startblock
))
4288 mval
->br_startblock
= DELAYSTARTBLOCK
;
4290 mval
->br_startblock
= got
->br_startblock
+
4291 (*bno
- got
->br_startoff
);
4293 * Return the minimum of what we got and what we asked for for
4294 * the length. We can use the len variable here because it is
4295 * modified below and we could have been there before coming
4296 * here if the first part of the allocation didn't overlap what
4299 mval
->br_blockcount
= XFS_FILBLKS_MIN(end
- *bno
,
4300 got
->br_blockcount
- (*bno
- got
->br_startoff
));
4301 mval
->br_state
= got
->br_state
;
4302 ASSERT(mval
->br_blockcount
<= len
);
4307 * Update and validate the extent map to return
4310 xfs_bmapi_update_map(
4311 struct xfs_bmbt_irec
**map
,
4319 xfs_bmbt_irec_t
*mval
= *map
;
4321 ASSERT((flags
& XFS_BMAPI_ENTIRE
) ||
4322 ((mval
->br_startoff
+ mval
->br_blockcount
) <= end
));
4323 ASSERT((flags
& XFS_BMAPI_ENTIRE
) || (mval
->br_blockcount
<= *len
) ||
4324 (mval
->br_startoff
< obno
));
4326 *bno
= mval
->br_startoff
+ mval
->br_blockcount
;
4328 if (*n
> 0 && mval
->br_startoff
== mval
[-1].br_startoff
) {
4329 /* update previous map with new information */
4330 ASSERT(mval
->br_startblock
== mval
[-1].br_startblock
);
4331 ASSERT(mval
->br_blockcount
> mval
[-1].br_blockcount
);
4332 ASSERT(mval
->br_state
== mval
[-1].br_state
);
4333 mval
[-1].br_blockcount
= mval
->br_blockcount
;
4334 mval
[-1].br_state
= mval
->br_state
;
4335 } else if (*n
> 0 && mval
->br_startblock
!= DELAYSTARTBLOCK
&&
4336 mval
[-1].br_startblock
!= DELAYSTARTBLOCK
&&
4337 mval
[-1].br_startblock
!= HOLESTARTBLOCK
&&
4338 mval
->br_startblock
== mval
[-1].br_startblock
+
4339 mval
[-1].br_blockcount
&&
4340 ((flags
& XFS_BMAPI_IGSTATE
) ||
4341 mval
[-1].br_state
== mval
->br_state
)) {
4342 ASSERT(mval
->br_startoff
==
4343 mval
[-1].br_startoff
+ mval
[-1].br_blockcount
);
4344 mval
[-1].br_blockcount
+= mval
->br_blockcount
;
4345 } else if (*n
> 0 &&
4346 mval
->br_startblock
== DELAYSTARTBLOCK
&&
4347 mval
[-1].br_startblock
== DELAYSTARTBLOCK
&&
4348 mval
->br_startoff
==
4349 mval
[-1].br_startoff
+ mval
[-1].br_blockcount
) {
4350 mval
[-1].br_blockcount
+= mval
->br_blockcount
;
4351 mval
[-1].br_state
= mval
->br_state
;
4352 } else if (!((*n
== 0) &&
4353 ((mval
->br_startoff
+ mval
->br_blockcount
) <=
4362 * Map file blocks to filesystem blocks without allocation.
4366 struct xfs_inode
*ip
,
4369 struct xfs_bmbt_irec
*mval
,
4373 struct xfs_mount
*mp
= ip
->i_mount
;
4374 struct xfs_ifork
*ifp
;
4375 struct xfs_bmbt_irec got
;
4376 struct xfs_bmbt_irec prev
;
4383 int whichfork
= (flags
& XFS_BMAPI_ATTRFORK
) ?
4384 XFS_ATTR_FORK
: XFS_DATA_FORK
;
4387 ASSERT(!(flags
& ~(XFS_BMAPI_ATTRFORK
|XFS_BMAPI_ENTIRE
|
4388 XFS_BMAPI_IGSTATE
)));
4390 if (unlikely(XFS_TEST_ERROR(
4391 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
4392 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
4393 mp
, XFS_ERRTAG_BMAPIFORMAT
, XFS_RANDOM_BMAPIFORMAT
))) {
4394 XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW
, mp
);
4395 return XFS_ERROR(EFSCORRUPTED
);
4398 if (XFS_FORCED_SHUTDOWN(mp
))
4399 return XFS_ERROR(EIO
);
4401 XFS_STATS_INC(xs_blk_mapr
);
4403 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4405 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
4406 error
= xfs_iread_extents(NULL
, ip
, whichfork
);
4411 xfs_bmap_search_extents(ip
, bno
, whichfork
, &eof
, &lastx
, &got
, &prev
);
4415 while (bno
< end
&& n
< *nmap
) {
4416 /* Reading past eof, act as though there's a hole up to end. */
4418 got
.br_startoff
= end
;
4419 if (got
.br_startoff
> bno
) {
4420 /* Reading in a hole. */
4421 mval
->br_startoff
= bno
;
4422 mval
->br_startblock
= HOLESTARTBLOCK
;
4423 mval
->br_blockcount
=
4424 XFS_FILBLKS_MIN(len
, got
.br_startoff
- bno
);
4425 mval
->br_state
= XFS_EXT_NORM
;
4426 bno
+= mval
->br_blockcount
;
4427 len
-= mval
->br_blockcount
;
4433 /* set up the extent map to return. */
4434 xfs_bmapi_trim_map(mval
, &got
, &bno
, len
, obno
, end
, n
, flags
);
4435 xfs_bmapi_update_map(&mval
, &bno
, &len
, obno
, end
, &n
, flags
);
4437 /* If we're done, stop now. */
4438 if (bno
>= end
|| n
>= *nmap
)
4441 /* Else go on to the next record. */
4442 if (++lastx
< ifp
->if_bytes
/ sizeof(xfs_bmbt_rec_t
))
4443 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, lastx
), &got
);
4452 xfs_bmapi_reserve_delalloc(
4453 struct xfs_inode
*ip
,
4456 struct xfs_bmbt_irec
*got
,
4457 struct xfs_bmbt_irec
*prev
,
4458 xfs_extnum_t
*lastx
,
4461 struct xfs_mount
*mp
= ip
->i_mount
;
4462 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, XFS_DATA_FORK
);
4464 xfs_extlen_t indlen
;
4465 char rt
= XFS_IS_REALTIME_INODE(ip
);
4469 alen
= XFS_FILBLKS_MIN(len
, MAXEXTLEN
);
4471 alen
= XFS_FILBLKS_MIN(alen
, got
->br_startoff
- aoff
);
4473 /* Figure out the extent size, adjust alen */
4474 extsz
= xfs_get_extsz_hint(ip
);
4477 * Make sure we don't exceed a single extent length when we
4478 * align the extent by reducing length we are going to
4479 * allocate by the maximum amount extent size aligment may
4482 alen
= XFS_FILBLKS_MIN(len
, MAXEXTLEN
- (2 * extsz
- 1));
4483 error
= xfs_bmap_extsize_align(mp
, got
, prev
, extsz
, rt
, eof
,
4484 1, 0, &aoff
, &alen
);
4489 extsz
= alen
/ mp
->m_sb
.sb_rextsize
;
4492 * Make a transaction-less quota reservation for delayed allocation
4493 * blocks. This number gets adjusted later. We return if we haven't
4494 * allocated blocks already inside this loop.
4496 error
= xfs_trans_reserve_quota_nblks(NULL
, ip
, (long)alen
, 0,
4497 rt
? XFS_QMOPT_RES_RTBLKS
: XFS_QMOPT_RES_REGBLKS
);
4502 * Split changing sb for alen and indlen since they could be coming
4503 * from different places.
4505 indlen
= (xfs_extlen_t
)xfs_bmap_worst_indlen(ip
, alen
);
4509 error
= xfs_mod_incore_sb(mp
, XFS_SBS_FREXTENTS
,
4510 -((int64_t)extsz
), 0);
4512 error
= xfs_icsb_modify_counters(mp
, XFS_SBS_FDBLOCKS
,
4513 -((int64_t)alen
), 0);
4517 goto out_unreserve_quota
;
4519 error
= xfs_icsb_modify_counters(mp
, XFS_SBS_FDBLOCKS
,
4520 -((int64_t)indlen
), 0);
4522 goto out_unreserve_blocks
;
4525 ip
->i_delayed_blks
+= alen
;
4527 got
->br_startoff
= aoff
;
4528 got
->br_startblock
= nullstartblock(indlen
);
4529 got
->br_blockcount
= alen
;
4530 got
->br_state
= XFS_EXT_NORM
;
4531 xfs_bmap_add_extent_hole_delay(ip
, lastx
, got
);
4534 * Update our extent pointer, given that xfs_bmap_add_extent_hole_delay
4535 * might have merged it into one of the neighbouring ones.
4537 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, *lastx
), got
);
4539 ASSERT(got
->br_startoff
<= aoff
);
4540 ASSERT(got
->br_startoff
+ got
->br_blockcount
>= aoff
+ alen
);
4541 ASSERT(isnullstartblock(got
->br_startblock
));
4542 ASSERT(got
->br_state
== XFS_EXT_NORM
);
4545 out_unreserve_blocks
:
4547 xfs_mod_incore_sb(mp
, XFS_SBS_FREXTENTS
, extsz
, 0);
4549 xfs_icsb_modify_counters(mp
, XFS_SBS_FDBLOCKS
, alen
, 0);
4550 out_unreserve_quota
:
4551 if (XFS_IS_QUOTA_ON(mp
))
4552 xfs_trans_unreserve_quota_nblks(NULL
, ip
, (long)alen
, 0, rt
?
4553 XFS_QMOPT_RES_RTBLKS
: XFS_QMOPT_RES_REGBLKS
);
4558 * Map file blocks to filesystem blocks, adding delayed allocations as needed.
4562 struct xfs_inode
*ip
, /* incore inode */
4563 xfs_fileoff_t bno
, /* starting file offs. mapped */
4564 xfs_filblks_t len
, /* length to map in file */
4565 struct xfs_bmbt_irec
*mval
, /* output: map values */
4566 int *nmap
, /* i/o: mval size/count */
4567 int flags
) /* XFS_BMAPI_... */
4569 struct xfs_mount
*mp
= ip
->i_mount
;
4570 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, XFS_DATA_FORK
);
4571 struct xfs_bmbt_irec got
; /* current file extent record */
4572 struct xfs_bmbt_irec prev
; /* previous file extent record */
4573 xfs_fileoff_t obno
; /* old block number (offset) */
4574 xfs_fileoff_t end
; /* end of mapped file region */
4575 xfs_extnum_t lastx
; /* last useful extent number */
4576 int eof
; /* we've hit the end of extents */
4577 int n
= 0; /* current extent index */
4581 ASSERT(*nmap
<= XFS_BMAP_MAX_NMAP
);
4582 ASSERT(!(flags
& ~XFS_BMAPI_ENTIRE
));
4584 if (unlikely(XFS_TEST_ERROR(
4585 (XFS_IFORK_FORMAT(ip
, XFS_DATA_FORK
) != XFS_DINODE_FMT_EXTENTS
&&
4586 XFS_IFORK_FORMAT(ip
, XFS_DATA_FORK
) != XFS_DINODE_FMT_BTREE
),
4587 mp
, XFS_ERRTAG_BMAPIFORMAT
, XFS_RANDOM_BMAPIFORMAT
))) {
4588 XFS_ERROR_REPORT("xfs_bmapi_delay", XFS_ERRLEVEL_LOW
, mp
);
4589 return XFS_ERROR(EFSCORRUPTED
);
4592 if (XFS_FORCED_SHUTDOWN(mp
))
4593 return XFS_ERROR(EIO
);
4595 XFS_STATS_INC(xs_blk_mapw
);
4597 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
4598 error
= xfs_iread_extents(NULL
, ip
, XFS_DATA_FORK
);
4603 xfs_bmap_search_extents(ip
, bno
, XFS_DATA_FORK
, &eof
, &lastx
, &got
, &prev
);
4607 while (bno
< end
&& n
< *nmap
) {
4608 if (eof
|| got
.br_startoff
> bno
) {
4609 error
= xfs_bmapi_reserve_delalloc(ip
, bno
, len
, &got
,
4610 &prev
, &lastx
, eof
);
4620 /* set up the extent map to return. */
4621 xfs_bmapi_trim_map(mval
, &got
, &bno
, len
, obno
, end
, n
, flags
);
4622 xfs_bmapi_update_map(&mval
, &bno
, &len
, obno
, end
, &n
, flags
);
4624 /* If we're done, stop now. */
4625 if (bno
>= end
|| n
>= *nmap
)
4628 /* Else go on to the next record. */
4630 if (++lastx
< ifp
->if_bytes
/ sizeof(xfs_bmbt_rec_t
))
4631 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, lastx
), &got
);
4642 __xfs_bmapi_allocate(
4643 struct xfs_bmalloca
*bma
)
4645 struct xfs_mount
*mp
= bma
->ip
->i_mount
;
4646 int whichfork
= (bma
->flags
& XFS_BMAPI_ATTRFORK
) ?
4647 XFS_ATTR_FORK
: XFS_DATA_FORK
;
4648 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
4649 int tmp_logflags
= 0;
4653 ASSERT(bma
->length
> 0);
4655 rt
= (whichfork
== XFS_DATA_FORK
) && XFS_IS_REALTIME_INODE(bma
->ip
);
4658 * For the wasdelay case, we could also just allocate the stuff asked
4659 * for in this bmap call but that wouldn't be as good.
4662 bma
->length
= (xfs_extlen_t
)bma
->got
.br_blockcount
;
4663 bma
->offset
= bma
->got
.br_startoff
;
4664 if (bma
->idx
!= NULLEXTNUM
&& bma
->idx
) {
4665 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
- 1),
4669 bma
->length
= XFS_FILBLKS_MIN(bma
->length
, MAXEXTLEN
);
4671 bma
->length
= XFS_FILBLKS_MIN(bma
->length
,
4672 bma
->got
.br_startoff
- bma
->offset
);
4676 * Indicate if this is the first user data in the file, or just any
4679 if (!(bma
->flags
& XFS_BMAPI_METADATA
)) {
4680 bma
->userdata
= (bma
->offset
== 0) ?
4681 XFS_ALLOC_INITIAL_USER_DATA
: XFS_ALLOC_USERDATA
;
4684 bma
->minlen
= (bma
->flags
& XFS_BMAPI_CONTIG
) ? bma
->length
: 1;
4687 * Only want to do the alignment at the eof if it is userdata and
4688 * allocation length is larger than a stripe unit.
4690 if (mp
->m_dalign
&& bma
->length
>= mp
->m_dalign
&&
4691 !(bma
->flags
& XFS_BMAPI_METADATA
) && whichfork
== XFS_DATA_FORK
) {
4692 error
= xfs_bmap_isaeof(bma
, whichfork
);
4697 error
= xfs_bmap_alloc(bma
);
4701 if (bma
->flist
->xbf_low
)
4704 bma
->cur
->bc_private
.b
.firstblock
= *bma
->firstblock
;
4705 if (bma
->blkno
== NULLFSBLOCK
)
4707 if ((ifp
->if_flags
& XFS_IFBROOT
) && !bma
->cur
) {
4708 bma
->cur
= xfs_bmbt_init_cursor(mp
, bma
->tp
, bma
->ip
, whichfork
);
4709 bma
->cur
->bc_private
.b
.firstblock
= *bma
->firstblock
;
4710 bma
->cur
->bc_private
.b
.flist
= bma
->flist
;
4713 * Bump the number of extents we've allocated
4719 bma
->cur
->bc_private
.b
.flags
=
4720 bma
->wasdel
? XFS_BTCUR_BPRV_WASDEL
: 0;
4722 bma
->got
.br_startoff
= bma
->offset
;
4723 bma
->got
.br_startblock
= bma
->blkno
;
4724 bma
->got
.br_blockcount
= bma
->length
;
4725 bma
->got
.br_state
= XFS_EXT_NORM
;
4728 * A wasdelay extent has been initialized, so shouldn't be flagged
4731 if (!bma
->wasdel
&& (bma
->flags
& XFS_BMAPI_PREALLOC
) &&
4732 xfs_sb_version_hasextflgbit(&mp
->m_sb
))
4733 bma
->got
.br_state
= XFS_EXT_UNWRITTEN
;
4736 error
= xfs_bmap_add_extent_delay_real(bma
);
4738 error
= xfs_bmap_add_extent_hole_real(bma
, whichfork
);
4740 bma
->logflags
|= tmp_logflags
;
4745 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4746 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4747 * the neighbouring ones.
4749 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
), &bma
->got
);
4751 ASSERT(bma
->got
.br_startoff
<= bma
->offset
);
4752 ASSERT(bma
->got
.br_startoff
+ bma
->got
.br_blockcount
>=
4753 bma
->offset
+ bma
->length
);
4754 ASSERT(bma
->got
.br_state
== XFS_EXT_NORM
||
4755 bma
->got
.br_state
== XFS_EXT_UNWRITTEN
);
4760 xfs_bmapi_allocate_worker(
4761 struct work_struct
*work
)
4763 struct xfs_bmalloca
*args
= container_of(work
,
4764 struct xfs_bmalloca
, work
);
4765 unsigned long pflags
;
4767 /* we are in a transaction context here */
4768 current_set_flags_nested(&pflags
, PF_FSTRANS
);
4770 args
->result
= __xfs_bmapi_allocate(args
);
4771 complete(args
->done
);
4773 current_restore_flags_nested(&pflags
, PF_FSTRANS
);
4777 * Some allocation requests often come in with little stack to work on. Push
4778 * them off to a worker thread so there is lots of stack to use. Otherwise just
4779 * call directly to avoid the context switch overhead here.
4783 struct xfs_bmalloca
*args
)
4785 DECLARE_COMPLETION_ONSTACK(done
);
4787 if (!args
->stack_switch
)
4788 return __xfs_bmapi_allocate(args
);
4792 INIT_WORK_ONSTACK(&args
->work
, xfs_bmapi_allocate_worker
);
4793 queue_work(xfs_alloc_wq
, &args
->work
);
4794 wait_for_completion(&done
);
4795 return args
->result
;
4799 xfs_bmapi_convert_unwritten(
4800 struct xfs_bmalloca
*bma
,
4801 struct xfs_bmbt_irec
*mval
,
4805 int whichfork
= (flags
& XFS_BMAPI_ATTRFORK
) ?
4806 XFS_ATTR_FORK
: XFS_DATA_FORK
;
4807 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
4808 int tmp_logflags
= 0;
4811 /* check if we need to do unwritten->real conversion */
4812 if (mval
->br_state
== XFS_EXT_UNWRITTEN
&&
4813 (flags
& XFS_BMAPI_PREALLOC
))
4816 /* check if we need to do real->unwritten conversion */
4817 if (mval
->br_state
== XFS_EXT_NORM
&&
4818 (flags
& (XFS_BMAPI_PREALLOC
| XFS_BMAPI_CONVERT
)) !=
4819 (XFS_BMAPI_PREALLOC
| XFS_BMAPI_CONVERT
))
4823 * Modify (by adding) the state flag, if writing.
4825 ASSERT(mval
->br_blockcount
<= len
);
4826 if ((ifp
->if_flags
& XFS_IFBROOT
) && !bma
->cur
) {
4827 bma
->cur
= xfs_bmbt_init_cursor(bma
->ip
->i_mount
, bma
->tp
,
4828 bma
->ip
, whichfork
);
4829 bma
->cur
->bc_private
.b
.firstblock
= *bma
->firstblock
;
4830 bma
->cur
->bc_private
.b
.flist
= bma
->flist
;
4832 mval
->br_state
= (mval
->br_state
== XFS_EXT_UNWRITTEN
)
4833 ? XFS_EXT_NORM
: XFS_EXT_UNWRITTEN
;
4835 error
= xfs_bmap_add_extent_unwritten_real(bma
->tp
, bma
->ip
, &bma
->idx
,
4836 &bma
->cur
, mval
, bma
->firstblock
, bma
->flist
,
4838 bma
->logflags
|= tmp_logflags
;
4843 * Update our extent pointer, given that
4844 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4845 * of the neighbouring ones.
4847 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
), &bma
->got
);
4850 * We may have combined previously unwritten space with written space,
4851 * so generate another request.
4853 if (mval
->br_blockcount
< len
)
4859 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4860 * extent state if necessary. Details behaviour is controlled by the flags
4861 * parameter. Only allocates blocks from a single allocation group, to avoid
4864 * The returned value in "firstblock" from the first call in a transaction
4865 * must be remembered and presented to subsequent calls in "firstblock".
4866 * An upper bound for the number of blocks to be allocated is supplied to
4867 * the first call in "total"; if no allocation group has that many free
4868 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4872 struct xfs_trans
*tp
, /* transaction pointer */
4873 struct xfs_inode
*ip
, /* incore inode */
4874 xfs_fileoff_t bno
, /* starting file offs. mapped */
4875 xfs_filblks_t len
, /* length to map in file */
4876 int flags
, /* XFS_BMAPI_... */
4877 xfs_fsblock_t
*firstblock
, /* first allocated block
4878 controls a.g. for allocs */
4879 xfs_extlen_t total
, /* total blocks needed */
4880 struct xfs_bmbt_irec
*mval
, /* output: map values */
4881 int *nmap
, /* i/o: mval size/count */
4882 struct xfs_bmap_free
*flist
) /* i/o: list extents to free */
4884 struct xfs_mount
*mp
= ip
->i_mount
;
4885 struct xfs_ifork
*ifp
;
4886 struct xfs_bmalloca bma
= { 0 }; /* args for xfs_bmap_alloc */
4887 xfs_fileoff_t end
; /* end of mapped file region */
4888 int eof
; /* after the end of extents */
4889 int error
; /* error return */
4890 int n
; /* current extent index */
4891 xfs_fileoff_t obno
; /* old block number (offset) */
4892 int whichfork
; /* data or attr fork */
4893 char inhole
; /* current location is hole in file */
4894 char wasdelay
; /* old extent was delayed */
4897 xfs_fileoff_t orig_bno
; /* original block number value */
4898 int orig_flags
; /* original flags arg value */
4899 xfs_filblks_t orig_len
; /* original value of len arg */
4900 struct xfs_bmbt_irec
*orig_mval
; /* original value of mval */
4901 int orig_nmap
; /* original value of *nmap */
4911 ASSERT(*nmap
<= XFS_BMAP_MAX_NMAP
);
4912 ASSERT(!(flags
& XFS_BMAPI_IGSTATE
));
4916 whichfork
= (flags
& XFS_BMAPI_ATTRFORK
) ?
4917 XFS_ATTR_FORK
: XFS_DATA_FORK
;
4919 if (unlikely(XFS_TEST_ERROR(
4920 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
4921 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
&&
4922 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_LOCAL
),
4923 mp
, XFS_ERRTAG_BMAPIFORMAT
, XFS_RANDOM_BMAPIFORMAT
))) {
4924 XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW
, mp
);
4925 return XFS_ERROR(EFSCORRUPTED
);
4928 if (XFS_FORCED_SHUTDOWN(mp
))
4929 return XFS_ERROR(EIO
);
4931 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4933 XFS_STATS_INC(xs_blk_mapw
);
4935 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
) {
4937 * XXX (dgc): This assumes we are only called for inodes that
4938 * contain content neutral data in local format. Anything that
4939 * contains caller-specific data in local format that needs
4940 * transformation to move to a block format needs to do the
4941 * conversion to extent format itself.
4943 * Directory data forks and attribute forks handle this
4944 * themselves, but with the addition of metadata verifiers every
4945 * data fork in local format now contains caller specific data
4946 * and as such conversion through this function is likely to be
4949 * The only likely user of this branch is for remote symlinks,
4950 * but we cannot overwrite the data fork contents of the symlink
4951 * (EEXIST occurs higher up the stack) and so it will never go
4952 * from local format to extent format here. Hence I don't think
4953 * this branch is ever executed intentionally and we should
4954 * consider removing it and asserting that xfs_bmapi_write()
4955 * cannot be called directly on local format forks. i.e. callers
4956 * are completely responsible for local to extent format
4957 * conversion, not xfs_bmapi_write().
4959 error
= xfs_bmap_local_to_extents(tp
, ip
, firstblock
, total
,
4960 &bma
.logflags
, whichfork
,
4961 xfs_bmap_local_to_extents_init_fn
);
4966 if (*firstblock
== NULLFSBLOCK
) {
4967 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
)
4968 bma
.minleft
= be16_to_cpu(ifp
->if_broot
->bb_level
) + 1;
4975 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
4976 error
= xfs_iread_extents(tp
, ip
, whichfork
);
4981 xfs_bmap_search_extents(ip
, bno
, whichfork
, &eof
, &bma
.idx
, &bma
.got
,
4992 bma
.firstblock
= firstblock
;
4994 if (flags
& XFS_BMAPI_STACK_SWITCH
)
4995 bma
.stack_switch
= 1;
4997 while (bno
< end
&& n
< *nmap
) {
4998 inhole
= eof
|| bma
.got
.br_startoff
> bno
;
4999 wasdelay
= !inhole
&& isnullstartblock(bma
.got
.br_startblock
);
5002 * First, deal with the hole before the allocated space
5003 * that we found, if any.
5005 if (inhole
|| wasdelay
) {
5007 bma
.conv
= !!(flags
& XFS_BMAPI_CONVERT
);
5008 bma
.wasdel
= wasdelay
;
5013 * There's a 32/64 bit type mismatch between the
5014 * allocation length request (which can be 64 bits in
5015 * length) and the bma length request, which is
5016 * xfs_extlen_t and therefore 32 bits. Hence we have to
5017 * check for 32-bit overflows and handle them here.
5019 if (len
> (xfs_filblks_t
)MAXEXTLEN
)
5020 bma
.length
= MAXEXTLEN
;
5025 ASSERT(bma
.length
> 0);
5026 error
= xfs_bmapi_allocate(&bma
);
5029 if (bma
.blkno
== NULLFSBLOCK
)
5033 /* Deal with the allocated space we found. */
5034 xfs_bmapi_trim_map(mval
, &bma
.got
, &bno
, len
, obno
,
5037 /* Execute unwritten extent conversion if necessary */
5038 error
= xfs_bmapi_convert_unwritten(&bma
, mval
, len
, flags
);
5039 if (error
== EAGAIN
)
5044 /* update the extent map to return */
5045 xfs_bmapi_update_map(&mval
, &bno
, &len
, obno
, end
, &n
, flags
);
5048 * If we're done, stop now. Stop when we've allocated
5049 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
5050 * the transaction may get too big.
5052 if (bno
>= end
|| n
>= *nmap
|| bma
.nallocs
>= *nmap
)
5055 /* Else go on to the next record. */
5057 if (++bma
.idx
< ifp
->if_bytes
/ sizeof(xfs_bmbt_rec_t
)) {
5058 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
.idx
),
5066 * Transform from btree to extents, give it cur.
5068 if (xfs_bmap_wants_extents(ip
, whichfork
)) {
5069 int tmp_logflags
= 0;
5072 error
= xfs_bmap_btree_to_extents(tp
, ip
, bma
.cur
,
5073 &tmp_logflags
, whichfork
);
5074 bma
.logflags
|= tmp_logflags
;
5079 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
||
5080 XFS_IFORK_NEXTENTS(ip
, whichfork
) >
5081 XFS_IFORK_MAXEXT(ip
, whichfork
));
5085 * Log everything. Do this after conversion, there's no point in
5086 * logging the extent records if we've converted to btree format.
5088 if ((bma
.logflags
& xfs_ilog_fext(whichfork
)) &&
5089 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
5090 bma
.logflags
&= ~xfs_ilog_fext(whichfork
);
5091 else if ((bma
.logflags
& xfs_ilog_fbroot(whichfork
)) &&
5092 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)
5093 bma
.logflags
&= ~xfs_ilog_fbroot(whichfork
);
5095 * Log whatever the flags say, even if error. Otherwise we might miss
5096 * detecting a case where the data is changed, there's an error,
5097 * and it's not logged so we don't shutdown when we should.
5100 xfs_trans_log_inode(tp
, ip
, bma
.logflags
);
5104 ASSERT(*firstblock
== NULLFSBLOCK
||
5105 XFS_FSB_TO_AGNO(mp
, *firstblock
) ==
5107 bma
.cur
->bc_private
.b
.firstblock
) ||
5109 XFS_FSB_TO_AGNO(mp
, *firstblock
) <
5111 bma
.cur
->bc_private
.b
.firstblock
)));
5112 *firstblock
= bma
.cur
->bc_private
.b
.firstblock
;
5114 xfs_btree_del_cursor(bma
.cur
,
5115 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
5118 xfs_bmap_validate_ret(orig_bno
, orig_len
, orig_flags
, orig_mval
,
5124 * Called by xfs_bmapi to update file extent records and the btree
5125 * after removing space (or undoing a delayed allocation).
5127 STATIC
int /* error */
5128 xfs_bmap_del_extent(
5129 xfs_inode_t
*ip
, /* incore inode pointer */
5130 xfs_trans_t
*tp
, /* current transaction pointer */
5131 xfs_extnum_t
*idx
, /* extent number to update/delete */
5132 xfs_bmap_free_t
*flist
, /* list of extents to be freed */
5133 xfs_btree_cur_t
*cur
, /* if null, not a btree */
5134 xfs_bmbt_irec_t
*del
, /* data to remove from extents */
5135 int *logflagsp
, /* inode logging flags */
5136 int whichfork
) /* data or attr fork */
5138 xfs_filblks_t da_new
; /* new delay-alloc indirect blocks */
5139 xfs_filblks_t da_old
; /* old delay-alloc indirect blocks */
5140 xfs_fsblock_t del_endblock
=0; /* first block past del */
5141 xfs_fileoff_t del_endoff
; /* first offset past del */
5142 int delay
; /* current block is delayed allocated */
5143 int do_fx
; /* free extent at end of routine */
5144 xfs_bmbt_rec_host_t
*ep
; /* current extent entry pointer */
5145 int error
; /* error return value */
5146 int flags
; /* inode logging flags */
5147 xfs_bmbt_irec_t got
; /* current extent entry */
5148 xfs_fileoff_t got_endoff
; /* first offset past got */
5149 int i
; /* temp state */
5150 xfs_ifork_t
*ifp
; /* inode fork pointer */
5151 xfs_mount_t
*mp
; /* mount structure */
5152 xfs_filblks_t nblks
; /* quota/sb block count */
5153 xfs_bmbt_irec_t
new; /* new record to be inserted */
5155 uint qfield
; /* quota field to update */
5156 xfs_filblks_t temp
; /* for indirect length calculations */
5157 xfs_filblks_t temp2
; /* for indirect length calculations */
5160 XFS_STATS_INC(xs_del_exlist
);
5162 if (whichfork
== XFS_ATTR_FORK
)
5163 state
|= BMAP_ATTRFORK
;
5166 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5167 ASSERT((*idx
>= 0) && (*idx
< ifp
->if_bytes
/
5168 (uint
)sizeof(xfs_bmbt_rec_t
)));
5169 ASSERT(del
->br_blockcount
> 0);
5170 ep
= xfs_iext_get_ext(ifp
, *idx
);
5171 xfs_bmbt_get_all(ep
, &got
);
5172 ASSERT(got
.br_startoff
<= del
->br_startoff
);
5173 del_endoff
= del
->br_startoff
+ del
->br_blockcount
;
5174 got_endoff
= got
.br_startoff
+ got
.br_blockcount
;
5175 ASSERT(got_endoff
>= del_endoff
);
5176 delay
= isnullstartblock(got
.br_startblock
);
5177 ASSERT(isnullstartblock(del
->br_startblock
) == delay
);
5182 * If deleting a real allocation, must free up the disk space.
5185 flags
= XFS_ILOG_CORE
;
5187 * Realtime allocation. Free it and record di_nblocks update.
5189 if (whichfork
== XFS_DATA_FORK
&& XFS_IS_REALTIME_INODE(ip
)) {
5193 ASSERT(do_mod(del
->br_blockcount
,
5194 mp
->m_sb
.sb_rextsize
) == 0);
5195 ASSERT(do_mod(del
->br_startblock
,
5196 mp
->m_sb
.sb_rextsize
) == 0);
5197 bno
= del
->br_startblock
;
5198 len
= del
->br_blockcount
;
5199 do_div(bno
, mp
->m_sb
.sb_rextsize
);
5200 do_div(len
, mp
->m_sb
.sb_rextsize
);
5201 error
= xfs_rtfree_extent(tp
, bno
, (xfs_extlen_t
)len
);
5205 nblks
= len
* mp
->m_sb
.sb_rextsize
;
5206 qfield
= XFS_TRANS_DQ_RTBCOUNT
;
5209 * Ordinary allocation.
5213 nblks
= del
->br_blockcount
;
5214 qfield
= XFS_TRANS_DQ_BCOUNT
;
5217 * Set up del_endblock and cur for later.
5219 del_endblock
= del
->br_startblock
+ del
->br_blockcount
;
5221 if ((error
= xfs_bmbt_lookup_eq(cur
, got
.br_startoff
,
5222 got
.br_startblock
, got
.br_blockcount
,
5225 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
5227 da_old
= da_new
= 0;
5229 da_old
= startblockval(got
.br_startblock
);
5235 * Set flag value to use in switch statement.
5236 * Left-contig is 2, right-contig is 1.
5238 switch (((got
.br_startoff
== del
->br_startoff
) << 1) |
5239 (got_endoff
== del_endoff
)) {
5242 * Matches the whole extent. Delete the entry.
5244 xfs_iext_remove(ip
, *idx
, 1,
5245 whichfork
== XFS_ATTR_FORK
? BMAP_ATTRFORK
: 0);
5250 XFS_IFORK_NEXT_SET(ip
, whichfork
,
5251 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
5252 flags
|= XFS_ILOG_CORE
;
5254 flags
|= xfs_ilog_fext(whichfork
);
5257 if ((error
= xfs_btree_delete(cur
, &i
)))
5259 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
5264 * Deleting the first part of the extent.
5266 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
5267 xfs_bmbt_set_startoff(ep
, del_endoff
);
5268 temp
= got
.br_blockcount
- del
->br_blockcount
;
5269 xfs_bmbt_set_blockcount(ep
, temp
);
5271 temp
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
5273 xfs_bmbt_set_startblock(ep
, nullstartblock((int)temp
));
5274 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
5278 xfs_bmbt_set_startblock(ep
, del_endblock
);
5279 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
5281 flags
|= xfs_ilog_fext(whichfork
);
5284 if ((error
= xfs_bmbt_update(cur
, del_endoff
, del_endblock
,
5285 got
.br_blockcount
- del
->br_blockcount
,
5292 * Deleting the last part of the extent.
5294 temp
= got
.br_blockcount
- del
->br_blockcount
;
5295 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
5296 xfs_bmbt_set_blockcount(ep
, temp
);
5298 temp
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
5300 xfs_bmbt_set_startblock(ep
, nullstartblock((int)temp
));
5301 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
5305 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
5307 flags
|= xfs_ilog_fext(whichfork
);
5310 if ((error
= xfs_bmbt_update(cur
, got
.br_startoff
,
5312 got
.br_blockcount
- del
->br_blockcount
,
5319 * Deleting the middle of the extent.
5321 temp
= del
->br_startoff
- got
.br_startoff
;
5322 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
5323 xfs_bmbt_set_blockcount(ep
, temp
);
5324 new.br_startoff
= del_endoff
;
5325 temp2
= got_endoff
- del_endoff
;
5326 new.br_blockcount
= temp2
;
5327 new.br_state
= got
.br_state
;
5329 new.br_startblock
= del_endblock
;
5330 flags
|= XFS_ILOG_CORE
;
5332 if ((error
= xfs_bmbt_update(cur
,
5334 got
.br_startblock
, temp
,
5337 if ((error
= xfs_btree_increment(cur
, 0, &i
)))
5339 cur
->bc_rec
.b
= new;
5340 error
= xfs_btree_insert(cur
, &i
);
5341 if (error
&& error
!= ENOSPC
)
5344 * If get no-space back from btree insert,
5345 * it tried a split, and we have a zero
5346 * block reservation.
5347 * Fix up our state and return the error.
5349 if (error
== ENOSPC
) {
5351 * Reset the cursor, don't trust
5352 * it after any insert operation.
5354 if ((error
= xfs_bmbt_lookup_eq(cur
,
5359 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
5361 * Update the btree record back
5362 * to the original value.
5364 if ((error
= xfs_bmbt_update(cur
,
5371 * Reset the extent record back
5372 * to the original value.
5374 xfs_bmbt_set_blockcount(ep
,
5377 error
= XFS_ERROR(ENOSPC
);
5380 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
5382 flags
|= xfs_ilog_fext(whichfork
);
5383 XFS_IFORK_NEXT_SET(ip
, whichfork
,
5384 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
5386 ASSERT(whichfork
== XFS_DATA_FORK
);
5387 temp
= xfs_bmap_worst_indlen(ip
, temp
);
5388 xfs_bmbt_set_startblock(ep
, nullstartblock((int)temp
));
5389 temp2
= xfs_bmap_worst_indlen(ip
, temp2
);
5390 new.br_startblock
= nullstartblock((int)temp2
);
5391 da_new
= temp
+ temp2
;
5392 while (da_new
> da_old
) {
5396 xfs_bmbt_set_startblock(ep
,
5397 nullstartblock((int)temp
));
5399 if (da_new
== da_old
)
5405 nullstartblock((int)temp2
);
5409 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
5410 xfs_iext_insert(ip
, *idx
+ 1, 1, &new, state
);
5415 * If we need to, add to list of extents to delete.
5418 xfs_bmap_add_free(del
->br_startblock
, del
->br_blockcount
, flist
,
5421 * Adjust inode # blocks in the file.
5424 ip
->i_d
.di_nblocks
-= nblks
;
5426 * Adjust quota data.
5429 xfs_trans_mod_dquot_byino(tp
, ip
, qfield
, (long)-nblks
);
5432 * Account for change in delayed indirect blocks.
5433 * Nothing to do for disk quota accounting here.
5435 ASSERT(da_old
>= da_new
);
5436 if (da_old
> da_new
) {
5437 xfs_icsb_modify_counters(mp
, XFS_SBS_FDBLOCKS
,
5438 (int64_t)(da_old
- da_new
), 0);
5446 * Unmap (remove) blocks from a file.
5447 * If nexts is nonzero then the number of extents to remove is limited to
5448 * that value. If not all extents in the block range can be removed then
5453 xfs_trans_t
*tp
, /* transaction pointer */
5454 struct xfs_inode
*ip
, /* incore inode */
5455 xfs_fileoff_t bno
, /* starting offset to unmap */
5456 xfs_filblks_t len
, /* length to unmap in file */
5457 int flags
, /* misc flags */
5458 xfs_extnum_t nexts
, /* number of extents max */
5459 xfs_fsblock_t
*firstblock
, /* first allocated block
5460 controls a.g. for allocs */
5461 xfs_bmap_free_t
*flist
, /* i/o: list extents to free */
5462 int *done
) /* set if not done yet */
5464 xfs_btree_cur_t
*cur
; /* bmap btree cursor */
5465 xfs_bmbt_irec_t del
; /* extent being deleted */
5466 int eof
; /* is deleting at eof */
5467 xfs_bmbt_rec_host_t
*ep
; /* extent record pointer */
5468 int error
; /* error return value */
5469 xfs_extnum_t extno
; /* extent number in list */
5470 xfs_bmbt_irec_t got
; /* current extent record */
5471 xfs_ifork_t
*ifp
; /* inode fork pointer */
5472 int isrt
; /* freeing in rt area */
5473 xfs_extnum_t lastx
; /* last extent index used */
5474 int logflags
; /* transaction logging flags */
5475 xfs_extlen_t mod
; /* rt extent offset */
5476 xfs_mount_t
*mp
; /* mount structure */
5477 xfs_extnum_t nextents
; /* number of file extents */
5478 xfs_bmbt_irec_t prev
; /* previous extent record */
5479 xfs_fileoff_t start
; /* first file offset deleted */
5480 int tmp_logflags
; /* partial logging flags */
5481 int wasdel
; /* was a delayed alloc extent */
5482 int whichfork
; /* data or attribute fork */
5485 trace_xfs_bunmap(ip
, bno
, len
, flags
, _RET_IP_
);
5487 whichfork
= (flags
& XFS_BMAPI_ATTRFORK
) ?
5488 XFS_ATTR_FORK
: XFS_DATA_FORK
;
5489 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5491 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
5492 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)) {
5493 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW
,
5495 return XFS_ERROR(EFSCORRUPTED
);
5498 if (XFS_FORCED_SHUTDOWN(mp
))
5499 return XFS_ERROR(EIO
);
5504 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
5505 (error
= xfs_iread_extents(tp
, ip
, whichfork
)))
5507 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
5508 if (nextents
== 0) {
5512 XFS_STATS_INC(xs_blk_unmap
);
5513 isrt
= (whichfork
== XFS_DATA_FORK
) && XFS_IS_REALTIME_INODE(ip
);
5515 bno
= start
+ len
- 1;
5516 ep
= xfs_bmap_search_extents(ip
, bno
, whichfork
, &eof
, &lastx
, &got
,
5520 * Check to see if the given block number is past the end of the
5521 * file, back up to the last block if so...
5524 ep
= xfs_iext_get_ext(ifp
, --lastx
);
5525 xfs_bmbt_get_all(ep
, &got
);
5526 bno
= got
.br_startoff
+ got
.br_blockcount
- 1;
5529 if (ifp
->if_flags
& XFS_IFBROOT
) {
5530 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
);
5531 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
5532 cur
->bc_private
.b
.firstblock
= *firstblock
;
5533 cur
->bc_private
.b
.flist
= flist
;
5534 cur
->bc_private
.b
.flags
= 0;
5540 * Synchronize by locking the bitmap inode.
5542 xfs_ilock(mp
->m_rbmip
, XFS_ILOCK_EXCL
);
5543 xfs_trans_ijoin(tp
, mp
->m_rbmip
, XFS_ILOCK_EXCL
);
5547 while (bno
!= (xfs_fileoff_t
)-1 && bno
>= start
&& lastx
>= 0 &&
5548 (nexts
== 0 || extno
< nexts
)) {
5550 * Is the found extent after a hole in which bno lives?
5551 * Just back up to the previous extent, if so.
5553 if (got
.br_startoff
> bno
) {
5556 ep
= xfs_iext_get_ext(ifp
, lastx
);
5557 xfs_bmbt_get_all(ep
, &got
);
5560 * Is the last block of this extent before the range
5561 * we're supposed to delete? If so, we're done.
5563 bno
= XFS_FILEOFF_MIN(bno
,
5564 got
.br_startoff
+ got
.br_blockcount
- 1);
5568 * Then deal with the (possibly delayed) allocated space
5573 wasdel
= isnullstartblock(del
.br_startblock
);
5574 if (got
.br_startoff
< start
) {
5575 del
.br_startoff
= start
;
5576 del
.br_blockcount
-= start
- got
.br_startoff
;
5578 del
.br_startblock
+= start
- got
.br_startoff
;
5580 if (del
.br_startoff
+ del
.br_blockcount
> bno
+ 1)
5581 del
.br_blockcount
= bno
+ 1 - del
.br_startoff
;
5582 sum
= del
.br_startblock
+ del
.br_blockcount
;
5584 (mod
= do_mod(sum
, mp
->m_sb
.sb_rextsize
))) {
5586 * Realtime extent not lined up at the end.
5587 * The extent could have been split into written
5588 * and unwritten pieces, or we could just be
5589 * unmapping part of it. But we can't really
5590 * get rid of part of a realtime extent.
5592 if (del
.br_state
== XFS_EXT_UNWRITTEN
||
5593 !xfs_sb_version_hasextflgbit(&mp
->m_sb
)) {
5595 * This piece is unwritten, or we're not
5596 * using unwritten extents. Skip over it.
5599 bno
-= mod
> del
.br_blockcount
?
5600 del
.br_blockcount
: mod
;
5601 if (bno
< got
.br_startoff
) {
5603 xfs_bmbt_get_all(xfs_iext_get_ext(
5609 * It's written, turn it unwritten.
5610 * This is better than zeroing it.
5612 ASSERT(del
.br_state
== XFS_EXT_NORM
);
5613 ASSERT(xfs_trans_get_block_res(tp
) > 0);
5615 * If this spans a realtime extent boundary,
5616 * chop it back to the start of the one we end at.
5618 if (del
.br_blockcount
> mod
) {
5619 del
.br_startoff
+= del
.br_blockcount
- mod
;
5620 del
.br_startblock
+= del
.br_blockcount
- mod
;
5621 del
.br_blockcount
= mod
;
5623 del
.br_state
= XFS_EXT_UNWRITTEN
;
5624 error
= xfs_bmap_add_extent_unwritten_real(tp
, ip
,
5625 &lastx
, &cur
, &del
, firstblock
, flist
,
5631 if (isrt
&& (mod
= do_mod(del
.br_startblock
, mp
->m_sb
.sb_rextsize
))) {
5633 * Realtime extent is lined up at the end but not
5634 * at the front. We'll get rid of full extents if
5637 mod
= mp
->m_sb
.sb_rextsize
- mod
;
5638 if (del
.br_blockcount
> mod
) {
5639 del
.br_blockcount
-= mod
;
5640 del
.br_startoff
+= mod
;
5641 del
.br_startblock
+= mod
;
5642 } else if ((del
.br_startoff
== start
&&
5643 (del
.br_state
== XFS_EXT_UNWRITTEN
||
5644 xfs_trans_get_block_res(tp
) == 0)) ||
5645 !xfs_sb_version_hasextflgbit(&mp
->m_sb
)) {
5647 * Can't make it unwritten. There isn't
5648 * a full extent here so just skip it.
5650 ASSERT(bno
>= del
.br_blockcount
);
5651 bno
-= del
.br_blockcount
;
5652 if (got
.br_startoff
> bno
) {
5654 ep
= xfs_iext_get_ext(ifp
,
5656 xfs_bmbt_get_all(ep
, &got
);
5660 } else if (del
.br_state
== XFS_EXT_UNWRITTEN
) {
5662 * This one is already unwritten.
5663 * It must have a written left neighbor.
5664 * Unwrite the killed part of that one and
5668 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
,
5670 ASSERT(prev
.br_state
== XFS_EXT_NORM
);
5671 ASSERT(!isnullstartblock(prev
.br_startblock
));
5672 ASSERT(del
.br_startblock
==
5673 prev
.br_startblock
+ prev
.br_blockcount
);
5674 if (prev
.br_startoff
< start
) {
5675 mod
= start
- prev
.br_startoff
;
5676 prev
.br_blockcount
-= mod
;
5677 prev
.br_startblock
+= mod
;
5678 prev
.br_startoff
= start
;
5680 prev
.br_state
= XFS_EXT_UNWRITTEN
;
5682 error
= xfs_bmap_add_extent_unwritten_real(tp
,
5683 ip
, &lastx
, &cur
, &prev
,
5684 firstblock
, flist
, &logflags
);
5689 ASSERT(del
.br_state
== XFS_EXT_NORM
);
5690 del
.br_state
= XFS_EXT_UNWRITTEN
;
5691 error
= xfs_bmap_add_extent_unwritten_real(tp
,
5692 ip
, &lastx
, &cur
, &del
,
5693 firstblock
, flist
, &logflags
);
5700 ASSERT(startblockval(del
.br_startblock
) > 0);
5701 /* Update realtime/data freespace, unreserve quota */
5703 xfs_filblks_t rtexts
;
5705 rtexts
= XFS_FSB_TO_B(mp
, del
.br_blockcount
);
5706 do_div(rtexts
, mp
->m_sb
.sb_rextsize
);
5707 xfs_mod_incore_sb(mp
, XFS_SBS_FREXTENTS
,
5708 (int64_t)rtexts
, 0);
5709 (void)xfs_trans_reserve_quota_nblks(NULL
,
5710 ip
, -((long)del
.br_blockcount
), 0,
5711 XFS_QMOPT_RES_RTBLKS
);
5713 xfs_icsb_modify_counters(mp
, XFS_SBS_FDBLOCKS
,
5714 (int64_t)del
.br_blockcount
, 0);
5715 (void)xfs_trans_reserve_quota_nblks(NULL
,
5716 ip
, -((long)del
.br_blockcount
), 0,
5717 XFS_QMOPT_RES_REGBLKS
);
5719 ip
->i_delayed_blks
-= del
.br_blockcount
;
5721 cur
->bc_private
.b
.flags
|=
5722 XFS_BTCUR_BPRV_WASDEL
;
5724 cur
->bc_private
.b
.flags
&= ~XFS_BTCUR_BPRV_WASDEL
;
5726 * If it's the case where the directory code is running
5727 * with no block reservation, and the deleted block is in
5728 * the middle of its extent, and the resulting insert
5729 * of an extent would cause transformation to btree format,
5730 * then reject it. The calling code will then swap
5731 * blocks around instead.
5732 * We have to do this now, rather than waiting for the
5733 * conversion to btree format, since the transaction
5736 if (!wasdel
&& xfs_trans_get_block_res(tp
) == 0 &&
5737 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
&&
5738 XFS_IFORK_NEXTENTS(ip
, whichfork
) >= /* Note the >= */
5739 XFS_IFORK_MAXEXT(ip
, whichfork
) &&
5740 del
.br_startoff
> got
.br_startoff
&&
5741 del
.br_startoff
+ del
.br_blockcount
<
5742 got
.br_startoff
+ got
.br_blockcount
) {
5743 error
= XFS_ERROR(ENOSPC
);
5746 error
= xfs_bmap_del_extent(ip
, tp
, &lastx
, flist
, cur
, &del
,
5747 &tmp_logflags
, whichfork
);
5748 logflags
|= tmp_logflags
;
5751 bno
= del
.br_startoff
- 1;
5754 * If not done go on to the next (previous) record.
5756 if (bno
!= (xfs_fileoff_t
)-1 && bno
>= start
) {
5758 ep
= xfs_iext_get_ext(ifp
, lastx
);
5759 if (xfs_bmbt_get_startoff(ep
) > bno
) {
5761 ep
= xfs_iext_get_ext(ifp
,
5764 xfs_bmbt_get_all(ep
, &got
);
5769 *done
= bno
== (xfs_fileoff_t
)-1 || bno
< start
|| lastx
< 0;
5772 * Convert to a btree if necessary.
5774 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
5775 ASSERT(cur
== NULL
);
5776 error
= xfs_bmap_extents_to_btree(tp
, ip
, firstblock
, flist
,
5777 &cur
, 0, &tmp_logflags
, whichfork
);
5778 logflags
|= tmp_logflags
;
5783 * transform from btree to extents, give it cur
5785 else if (xfs_bmap_wants_extents(ip
, whichfork
)) {
5786 ASSERT(cur
!= NULL
);
5787 error
= xfs_bmap_btree_to_extents(tp
, ip
, cur
, &tmp_logflags
,
5789 logflags
|= tmp_logflags
;
5794 * transform from extents to local?
5799 * Log everything. Do this after conversion, there's no point in
5800 * logging the extent records if we've converted to btree format.
5802 if ((logflags
& xfs_ilog_fext(whichfork
)) &&
5803 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
5804 logflags
&= ~xfs_ilog_fext(whichfork
);
5805 else if ((logflags
& xfs_ilog_fbroot(whichfork
)) &&
5806 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)
5807 logflags
&= ~xfs_ilog_fbroot(whichfork
);
5809 * Log inode even in the error case, if the transaction
5810 * is dirty we'll need to shut down the filesystem.
5813 xfs_trans_log_inode(tp
, ip
, logflags
);
5816 *firstblock
= cur
->bc_private
.b
.firstblock
;
5817 cur
->bc_private
.b
.allocated
= 0;
5819 xfs_btree_del_cursor(cur
,
5820 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
5826 * returns 1 for success, 0 if we failed to map the extent.
5829 xfs_getbmapx_fix_eof_hole(
5830 xfs_inode_t
*ip
, /* xfs incore inode pointer */
5831 struct getbmapx
*out
, /* output structure */
5832 int prealloced
, /* this is a file with
5833 * preallocated data space */
5834 __int64_t end
, /* last block requested */
5835 xfs_fsblock_t startblock
)
5838 xfs_mount_t
*mp
; /* file system mount point */
5839 xfs_ifork_t
*ifp
; /* inode fork pointer */
5840 xfs_extnum_t lastx
; /* last extent pointer */
5841 xfs_fileoff_t fileblock
;
5843 if (startblock
== HOLESTARTBLOCK
) {
5845 out
->bmv_block
= -1;
5846 fixlen
= XFS_FSB_TO_BB(mp
, XFS_B_TO_FSB(mp
, XFS_ISIZE(ip
)));
5847 fixlen
-= out
->bmv_offset
;
5848 if (prealloced
&& out
->bmv_offset
+ out
->bmv_length
== end
) {
5849 /* Came to hole at EOF. Trim it. */
5852 out
->bmv_length
= fixlen
;
5855 if (startblock
== DELAYSTARTBLOCK
)
5856 out
->bmv_block
= -2;
5858 out
->bmv_block
= xfs_fsb_to_db(ip
, startblock
);
5859 fileblock
= XFS_BB_TO_FSB(ip
->i_mount
, out
->bmv_offset
);
5860 ifp
= XFS_IFORK_PTR(ip
, XFS_DATA_FORK
);
5861 if (xfs_iext_bno_to_ext(ifp
, fileblock
, &lastx
) &&
5862 (lastx
== (ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
))-1))
5863 out
->bmv_oflags
|= BMV_OF_LAST
;
5870 * Get inode's extents as described in bmv, and format for output.
5871 * Calls formatter to fill the user's buffer until all extents
5872 * are mapped, until the passed-in bmv->bmv_count slots have
5873 * been filled, or until the formatter short-circuits the loop,
5874 * if it is tracking filled-in extents on its own.
5876 int /* error code */
5879 struct getbmapx
*bmv
, /* user bmap structure */
5880 xfs_bmap_format_t formatter
, /* format to user */
5881 void *arg
) /* formatter arg */
5883 __int64_t bmvend
; /* last block requested */
5884 int error
= 0; /* return value */
5885 __int64_t fixlen
; /* length for -1 case */
5886 int i
; /* extent number */
5887 int lock
; /* lock state */
5888 xfs_bmbt_irec_t
*map
; /* buffer for user's data */
5889 xfs_mount_t
*mp
; /* file system mount point */
5890 int nex
; /* # of user extents can do */
5891 int nexleft
; /* # of user extents left */
5892 int subnex
; /* # of bmapi's can do */
5893 int nmap
; /* number of map entries */
5894 struct getbmapx
*out
; /* output structure */
5895 int whichfork
; /* data or attr fork */
5896 int prealloced
; /* this is a file with
5897 * preallocated data space */
5898 int iflags
; /* interface flags */
5899 int bmapi_flags
; /* flags for xfs_bmapi */
5903 iflags
= bmv
->bmv_iflags
;
5904 whichfork
= iflags
& BMV_IF_ATTRFORK
? XFS_ATTR_FORK
: XFS_DATA_FORK
;
5906 if (whichfork
== XFS_ATTR_FORK
) {
5907 if (XFS_IFORK_Q(ip
)) {
5908 if (ip
->i_d
.di_aformat
!= XFS_DINODE_FMT_EXTENTS
&&
5909 ip
->i_d
.di_aformat
!= XFS_DINODE_FMT_BTREE
&&
5910 ip
->i_d
.di_aformat
!= XFS_DINODE_FMT_LOCAL
)
5911 return XFS_ERROR(EINVAL
);
5912 } else if (unlikely(
5913 ip
->i_d
.di_aformat
!= 0 &&
5914 ip
->i_d
.di_aformat
!= XFS_DINODE_FMT_EXTENTS
)) {
5915 XFS_ERROR_REPORT("xfs_getbmap", XFS_ERRLEVEL_LOW
,
5917 return XFS_ERROR(EFSCORRUPTED
);
5923 if (ip
->i_d
.di_format
!= XFS_DINODE_FMT_EXTENTS
&&
5924 ip
->i_d
.di_format
!= XFS_DINODE_FMT_BTREE
&&
5925 ip
->i_d
.di_format
!= XFS_DINODE_FMT_LOCAL
)
5926 return XFS_ERROR(EINVAL
);
5928 if (xfs_get_extsz_hint(ip
) ||
5929 ip
->i_d
.di_flags
& (XFS_DIFLAG_PREALLOC
|XFS_DIFLAG_APPEND
)){
5931 fixlen
= mp
->m_super
->s_maxbytes
;
5934 fixlen
= XFS_ISIZE(ip
);
5938 if (bmv
->bmv_length
== -1) {
5939 fixlen
= XFS_FSB_TO_BB(mp
, XFS_B_TO_FSB(mp
, fixlen
));
5941 max_t(__int64_t
, fixlen
- bmv
->bmv_offset
, 0);
5942 } else if (bmv
->bmv_length
== 0) {
5943 bmv
->bmv_entries
= 0;
5945 } else if (bmv
->bmv_length
< 0) {
5946 return XFS_ERROR(EINVAL
);
5949 nex
= bmv
->bmv_count
- 1;
5951 return XFS_ERROR(EINVAL
);
5952 bmvend
= bmv
->bmv_offset
+ bmv
->bmv_length
;
5955 if (bmv
->bmv_count
> ULONG_MAX
/ sizeof(struct getbmapx
))
5956 return XFS_ERROR(ENOMEM
);
5957 out
= kmem_zalloc(bmv
->bmv_count
* sizeof(struct getbmapx
), KM_MAYFAIL
);
5959 out
= kmem_zalloc_large(bmv
->bmv_count
*
5960 sizeof(struct getbmapx
));
5962 return XFS_ERROR(ENOMEM
);
5965 xfs_ilock(ip
, XFS_IOLOCK_SHARED
);
5966 if (whichfork
== XFS_DATA_FORK
&& !(iflags
& BMV_IF_DELALLOC
)) {
5967 if (ip
->i_delayed_blks
|| XFS_ISIZE(ip
) > ip
->i_d
.di_size
) {
5968 error
= -filemap_write_and_wait(VFS_I(ip
)->i_mapping
);
5970 goto out_unlock_iolock
;
5973 * even after flushing the inode, there can still be delalloc
5974 * blocks on the inode beyond EOF due to speculative
5975 * preallocation. These are not removed until the release
5976 * function is called or the inode is inactivated. Hence we
5977 * cannot assert here that ip->i_delayed_blks == 0.
5981 lock
= xfs_ilock_map_shared(ip
);
5984 * Don't let nex be bigger than the number of extents
5985 * we can have assuming alternating holes and real extents.
5987 if (nex
> XFS_IFORK_NEXTENTS(ip
, whichfork
) * 2 + 1)
5988 nex
= XFS_IFORK_NEXTENTS(ip
, whichfork
) * 2 + 1;
5990 bmapi_flags
= xfs_bmapi_aflag(whichfork
);
5991 if (!(iflags
& BMV_IF_PREALLOC
))
5992 bmapi_flags
|= XFS_BMAPI_IGSTATE
;
5995 * Allocate enough space to handle "subnex" maps at a time.
5999 map
= kmem_alloc(subnex
* sizeof(*map
), KM_MAYFAIL
| KM_NOFS
);
6001 goto out_unlock_ilock
;
6003 bmv
->bmv_entries
= 0;
6005 if (XFS_IFORK_NEXTENTS(ip
, whichfork
) == 0 &&
6006 (whichfork
== XFS_ATTR_FORK
|| !(iflags
& BMV_IF_DELALLOC
))) {
6014 nmap
= (nexleft
> subnex
) ? subnex
: nexleft
;
6015 error
= xfs_bmapi_read(ip
, XFS_BB_TO_FSBT(mp
, bmv
->bmv_offset
),
6016 XFS_BB_TO_FSB(mp
, bmv
->bmv_length
),
6017 map
, &nmap
, bmapi_flags
);
6020 ASSERT(nmap
<= subnex
);
6022 for (i
= 0; i
< nmap
&& nexleft
&& bmv
->bmv_length
; i
++) {
6023 out
[cur_ext
].bmv_oflags
= 0;
6024 if (map
[i
].br_state
== XFS_EXT_UNWRITTEN
)
6025 out
[cur_ext
].bmv_oflags
|= BMV_OF_PREALLOC
;
6026 else if (map
[i
].br_startblock
== DELAYSTARTBLOCK
)
6027 out
[cur_ext
].bmv_oflags
|= BMV_OF_DELALLOC
;
6028 out
[cur_ext
].bmv_offset
=
6029 XFS_FSB_TO_BB(mp
, map
[i
].br_startoff
);
6030 out
[cur_ext
].bmv_length
=
6031 XFS_FSB_TO_BB(mp
, map
[i
].br_blockcount
);
6032 out
[cur_ext
].bmv_unused1
= 0;
6033 out
[cur_ext
].bmv_unused2
= 0;
6036 * delayed allocation extents that start beyond EOF can
6037 * occur due to speculative EOF allocation when the
6038 * delalloc extent is larger than the largest freespace
6039 * extent at conversion time. These extents cannot be
6040 * converted by data writeback, so can exist here even
6041 * if we are not supposed to be finding delalloc
6044 if (map
[i
].br_startblock
== DELAYSTARTBLOCK
&&
6045 map
[i
].br_startoff
<= XFS_B_TO_FSB(mp
, XFS_ISIZE(ip
)))
6046 ASSERT((iflags
& BMV_IF_DELALLOC
) != 0);
6048 if (map
[i
].br_startblock
== HOLESTARTBLOCK
&&
6049 whichfork
== XFS_ATTR_FORK
) {
6050 /* came to the end of attribute fork */
6051 out
[cur_ext
].bmv_oflags
|= BMV_OF_LAST
;
6055 if (!xfs_getbmapx_fix_eof_hole(ip
, &out
[cur_ext
],
6057 map
[i
].br_startblock
))
6061 out
[cur_ext
].bmv_offset
+
6062 out
[cur_ext
].bmv_length
;
6064 max_t(__int64_t
, 0, bmvend
- bmv
->bmv_offset
);
6067 * In case we don't want to return the hole,
6068 * don't increase cur_ext so that we can reuse
6069 * it in the next loop.
6071 if ((iflags
& BMV_IF_NO_HOLES
) &&
6072 map
[i
].br_startblock
== HOLESTARTBLOCK
) {
6073 memset(&out
[cur_ext
], 0, sizeof(out
[cur_ext
]));
6081 } while (nmap
&& nexleft
&& bmv
->bmv_length
);
6086 xfs_iunlock_map_shared(ip
, lock
);
6088 xfs_iunlock(ip
, XFS_IOLOCK_SHARED
);
6090 for (i
= 0; i
< cur_ext
; i
++) {
6091 int full
= 0; /* user array is full */
6093 /* format results & advance arg */
6094 error
= formatter(&arg
, &out
[i
], &full
);
6099 if (is_vmalloc_addr(out
))
6100 kmem_free_large(out
);
6107 * dead simple method of punching delalyed allocation blocks from a range in
6108 * the inode. Walks a block at a time so will be slow, but is only executed in
6109 * rare error cases so the overhead is not critical. This will alays punch out
6110 * both the start and end blocks, even if the ranges only partially overlap
6111 * them, so it is up to the caller to ensure that partial blocks are not
6115 xfs_bmap_punch_delalloc_range(
6116 struct xfs_inode
*ip
,
6117 xfs_fileoff_t start_fsb
,
6118 xfs_fileoff_t length
)
6120 xfs_fileoff_t remaining
= length
;
6123 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
6127 xfs_bmbt_irec_t imap
;
6129 xfs_fsblock_t firstblock
;
6130 xfs_bmap_free_t flist
;
6133 * Map the range first and check that it is a delalloc extent
6134 * before trying to unmap the range. Otherwise we will be
6135 * trying to remove a real extent (which requires a
6136 * transaction) or a hole, which is probably a bad idea...
6138 error
= xfs_bmapi_read(ip
, start_fsb
, 1, &imap
, &nimaps
,
6142 /* something screwed, just bail */
6143 if (!XFS_FORCED_SHUTDOWN(ip
->i_mount
)) {
6144 xfs_alert(ip
->i_mount
,
6145 "Failed delalloc mapping lookup ino %lld fsb %lld.",
6146 ip
->i_ino
, start_fsb
);
6154 if (imap
.br_startblock
!= DELAYSTARTBLOCK
) {
6155 /* been converted, ignore */
6158 WARN_ON(imap
.br_blockcount
== 0);
6161 * Note: while we initialise the firstblock/flist pair, they
6162 * should never be used because blocks should never be
6163 * allocated or freed for a delalloc extent and hence we need
6164 * don't cancel or finish them after the xfs_bunmapi() call.
6166 xfs_bmap_init(&flist
, &firstblock
);
6167 error
= xfs_bunmapi(NULL
, ip
, start_fsb
, 1, 0, 1, &firstblock
,
6172 ASSERT(!flist
.xbf_count
&& !flist
.xbf_first
);
6176 } while(remaining
> 0);