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_mount.h"
29 #include "xfs_da_btree.h"
30 #include "xfs_bmap_btree.h"
31 #include "xfs_alloc_btree.h"
32 #include "xfs_ialloc_btree.h"
33 #include "xfs_dinode.h"
34 #include "xfs_inode.h"
35 #include "xfs_btree.h"
36 #include "xfs_mount.h"
37 #include "xfs_itable.h"
38 #include "xfs_inode_item.h"
39 #include "xfs_extfree_item.h"
40 #include "xfs_alloc.h"
42 #include "xfs_rtalloc.h"
43 #include "xfs_error.h"
44 #include "xfs_attr_leaf.h"
45 #include "xfs_quota.h"
46 #include "xfs_trans_space.h"
47 #include "xfs_buf_item.h"
48 #include "xfs_filestream.h"
49 #include "xfs_vnodeops.h"
50 #include "xfs_trace.h"
51 #include "xfs_symlink.h"
54 kmem_zone_t
*xfs_bmap_free_item_zone
;
57 * Miscellaneous helper functions
61 * Compute and fill in the value of the maximum depth of a bmap btree
62 * in this filesystem. Done once, during mount.
65 xfs_bmap_compute_maxlevels(
66 xfs_mount_t
*mp
, /* file system mount structure */
67 int whichfork
) /* data or attr fork */
69 int level
; /* btree level */
70 uint maxblocks
; /* max blocks at this level */
71 uint maxleafents
; /* max leaf entries possible */
72 int maxrootrecs
; /* max records in root block */
73 int minleafrecs
; /* min records in leaf block */
74 int minnoderecs
; /* min records in node block */
75 int sz
; /* root block size */
78 * The maximum number of extents in a file, hence the maximum
79 * number of leaf entries, is controlled by the type of di_nextents
80 * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
81 * (a signed 16-bit number, xfs_aextnum_t).
83 * Note that we can no longer assume that if we are in ATTR1 that
84 * the fork offset of all the inodes will be
85 * (xfs_default_attroffset(ip) >> 3) because we could have mounted
86 * with ATTR2 and then mounted back with ATTR1, keeping the
87 * di_forkoff's fixed but probably at various positions. Therefore,
88 * for both ATTR1 and ATTR2 we have to assume the worst case scenario
89 * of a minimum size available.
91 if (whichfork
== XFS_DATA_FORK
) {
92 maxleafents
= MAXEXTNUM
;
93 sz
= XFS_BMDR_SPACE_CALC(MINDBTPTRS
);
95 maxleafents
= MAXAEXTNUM
;
96 sz
= XFS_BMDR_SPACE_CALC(MINABTPTRS
);
98 maxrootrecs
= xfs_bmdr_maxrecs(mp
, sz
, 0);
99 minleafrecs
= mp
->m_bmap_dmnr
[0];
100 minnoderecs
= mp
->m_bmap_dmnr
[1];
101 maxblocks
= (maxleafents
+ minleafrecs
- 1) / minleafrecs
;
102 for (level
= 1; maxblocks
> 1; level
++) {
103 if (maxblocks
<= maxrootrecs
)
106 maxblocks
= (maxblocks
+ minnoderecs
- 1) / minnoderecs
;
108 mp
->m_bm_maxlevels
[whichfork
] = level
;
112 * Convert the given file system block to a disk block. We have to treat it
113 * differently based on whether the file is a real time file or not, because the
117 xfs_fsb_to_db(struct xfs_inode
*ip
, xfs_fsblock_t fsb
)
119 return (XFS_IS_REALTIME_INODE(ip
) ? \
120 (xfs_daddr_t
)XFS_FSB_TO_BB((ip
)->i_mount
, (fsb
)) : \
121 XFS_FSB_TO_DADDR((ip
)->i_mount
, (fsb
)));
124 STATIC
int /* error */
126 struct xfs_btree_cur
*cur
,
130 int *stat
) /* success/failure */
132 cur
->bc_rec
.b
.br_startoff
= off
;
133 cur
->bc_rec
.b
.br_startblock
= bno
;
134 cur
->bc_rec
.b
.br_blockcount
= len
;
135 return xfs_btree_lookup(cur
, XFS_LOOKUP_EQ
, stat
);
138 STATIC
int /* error */
140 struct xfs_btree_cur
*cur
,
144 int *stat
) /* success/failure */
146 cur
->bc_rec
.b
.br_startoff
= off
;
147 cur
->bc_rec
.b
.br_startblock
= bno
;
148 cur
->bc_rec
.b
.br_blockcount
= len
;
149 return xfs_btree_lookup(cur
, XFS_LOOKUP_GE
, stat
);
153 * Check if the inode needs to be converted to btree format.
155 static inline bool xfs_bmap_needs_btree(struct xfs_inode
*ip
, int whichfork
)
157 return XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
&&
158 XFS_IFORK_NEXTENTS(ip
, whichfork
) >
159 XFS_IFORK_MAXEXT(ip
, whichfork
);
163 * Check if the inode should be converted to extent format.
165 static inline bool xfs_bmap_wants_extents(struct xfs_inode
*ip
, int whichfork
)
167 return XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
&&
168 XFS_IFORK_NEXTENTS(ip
, whichfork
) <=
169 XFS_IFORK_MAXEXT(ip
, whichfork
);
173 * Update the record referred to by cur to the value given
174 * by [off, bno, len, state].
175 * This either works (return 0) or gets an EFSCORRUPTED error.
179 struct xfs_btree_cur
*cur
,
185 union xfs_btree_rec rec
;
187 xfs_bmbt_disk_set_allf(&rec
.bmbt
, off
, bno
, len
, state
);
188 return xfs_btree_update(cur
, &rec
);
192 * Compute the worst-case number of indirect blocks that will be used
193 * for ip's delayed extent of length "len".
196 xfs_bmap_worst_indlen(
197 xfs_inode_t
*ip
, /* incore inode pointer */
198 xfs_filblks_t len
) /* delayed extent length */
200 int level
; /* btree level number */
201 int maxrecs
; /* maximum record count at this level */
202 xfs_mount_t
*mp
; /* mount structure */
203 xfs_filblks_t rval
; /* return value */
206 maxrecs
= mp
->m_bmap_dmxr
[0];
207 for (level
= 0, rval
= 0;
208 level
< XFS_BM_MAXLEVELS(mp
, XFS_DATA_FORK
);
211 do_div(len
, maxrecs
);
214 return rval
+ XFS_BM_MAXLEVELS(mp
, XFS_DATA_FORK
) -
217 maxrecs
= mp
->m_bmap_dmxr
[1];
223 * Calculate the default attribute fork offset for newly created inodes.
226 xfs_default_attroffset(
227 struct xfs_inode
*ip
)
229 struct xfs_mount
*mp
= ip
->i_mount
;
232 if (mp
->m_sb
.sb_inodesize
== 256) {
233 offset
= XFS_LITINO(mp
, ip
->i_d
.di_version
) -
234 XFS_BMDR_SPACE_CALC(MINABTPTRS
);
236 offset
= XFS_BMDR_SPACE_CALC(6 * MINABTPTRS
);
239 ASSERT(offset
< XFS_LITINO(mp
, ip
->i_d
.di_version
));
244 * Helper routine to reset inode di_forkoff field when switching
245 * attribute fork from local to extent format - we reset it where
246 * possible to make space available for inline data fork extents.
249 xfs_bmap_forkoff_reset(
254 if (whichfork
== XFS_ATTR_FORK
&&
255 ip
->i_d
.di_format
!= XFS_DINODE_FMT_DEV
&&
256 ip
->i_d
.di_format
!= XFS_DINODE_FMT_UUID
&&
257 ip
->i_d
.di_format
!= XFS_DINODE_FMT_BTREE
) {
258 uint dfl_forkoff
= xfs_default_attroffset(ip
) >> 3;
260 if (dfl_forkoff
> ip
->i_d
.di_forkoff
)
261 ip
->i_d
.di_forkoff
= dfl_forkoff
;
266 * Extent tree block counting routines.
270 * Count leaf blocks given a range of extent records.
273 xfs_bmap_count_leaves(
281 for (b
= 0; b
< numrecs
; b
++) {
282 xfs_bmbt_rec_host_t
*frp
= xfs_iext_get_ext(ifp
, idx
+ b
);
283 *count
+= xfs_bmbt_get_blockcount(frp
);
288 * Count leaf blocks given a range of extent records originally
292 xfs_bmap_disk_count_leaves(
293 struct xfs_mount
*mp
,
294 struct xfs_btree_block
*block
,
301 for (b
= 1; b
<= numrecs
; b
++) {
302 frp
= XFS_BMBT_REC_ADDR(mp
, block
, b
);
303 *count
+= xfs_bmbt_disk_get_blockcount(frp
);
308 * Recursively walks each level of a btree
309 * to count total fsblocks is use.
311 STATIC
int /* error */
313 xfs_mount_t
*mp
, /* file system mount point */
314 xfs_trans_t
*tp
, /* transaction pointer */
315 xfs_ifork_t
*ifp
, /* inode fork pointer */
316 xfs_fsblock_t blockno
, /* file system block number */
317 int levelin
, /* level in btree */
318 int *count
) /* Count of blocks */
324 xfs_fsblock_t bno
= blockno
;
325 xfs_fsblock_t nextbno
;
326 struct xfs_btree_block
*block
, *nextblock
;
329 error
= xfs_btree_read_bufl(mp
, tp
, bno
, 0, &bp
, XFS_BMAP_BTREE_REF
,
334 block
= XFS_BUF_TO_BLOCK(bp
);
337 /* Not at node above leaves, count this level of nodes */
338 nextbno
= be64_to_cpu(block
->bb_u
.l
.bb_rightsib
);
339 while (nextbno
!= NULLFSBLOCK
) {
340 error
= xfs_btree_read_bufl(mp
, tp
, nextbno
, 0, &nbp
,
346 nextblock
= XFS_BUF_TO_BLOCK(nbp
);
347 nextbno
= be64_to_cpu(nextblock
->bb_u
.l
.bb_rightsib
);
348 xfs_trans_brelse(tp
, nbp
);
351 /* Dive to the next level */
352 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, mp
->m_bmap_dmxr
[1]);
353 bno
= be64_to_cpu(*pp
);
354 if (unlikely((error
=
355 xfs_bmap_count_tree(mp
, tp
, ifp
, bno
, level
, count
)) < 0)) {
356 xfs_trans_brelse(tp
, bp
);
357 XFS_ERROR_REPORT("xfs_bmap_count_tree(1)",
358 XFS_ERRLEVEL_LOW
, mp
);
359 return XFS_ERROR(EFSCORRUPTED
);
361 xfs_trans_brelse(tp
, bp
);
363 /* count all level 1 nodes and their leaves */
365 nextbno
= be64_to_cpu(block
->bb_u
.l
.bb_rightsib
);
366 numrecs
= be16_to_cpu(block
->bb_numrecs
);
367 xfs_bmap_disk_count_leaves(mp
, block
, numrecs
, count
);
368 xfs_trans_brelse(tp
, bp
);
369 if (nextbno
== NULLFSBLOCK
)
372 error
= xfs_btree_read_bufl(mp
, tp
, bno
, 0, &bp
,
378 block
= XFS_BUF_TO_BLOCK(bp
);
385 * Count fsblocks of the given fork.
388 xfs_bmap_count_blocks(
389 xfs_trans_t
*tp
, /* transaction pointer */
390 xfs_inode_t
*ip
, /* incore inode */
391 int whichfork
, /* data or attr fork */
392 int *count
) /* out: count of blocks */
394 struct xfs_btree_block
*block
; /* current btree block */
395 xfs_fsblock_t bno
; /* block # of "block" */
396 xfs_ifork_t
*ifp
; /* fork structure */
397 int level
; /* btree level, for checking */
398 xfs_mount_t
*mp
; /* file system mount structure */
399 __be64
*pp
; /* pointer to block address */
403 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
404 if ( XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
) {
405 xfs_bmap_count_leaves(ifp
, 0,
406 ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
),
412 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
414 block
= ifp
->if_broot
;
415 level
= be16_to_cpu(block
->bb_level
);
417 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, 1, ifp
->if_broot_bytes
);
418 bno
= be64_to_cpu(*pp
);
419 ASSERT(bno
!= NULLDFSBNO
);
420 ASSERT(XFS_FSB_TO_AGNO(mp
, bno
) < mp
->m_sb
.sb_agcount
);
421 ASSERT(XFS_FSB_TO_AGBNO(mp
, bno
) < mp
->m_sb
.sb_agblocks
);
423 if (unlikely(xfs_bmap_count_tree(mp
, tp
, ifp
, bno
, level
, count
) < 0)) {
424 XFS_ERROR_REPORT("xfs_bmap_count_blocks(2)", XFS_ERRLEVEL_LOW
,
426 return XFS_ERROR(EFSCORRUPTED
);
433 * Debug/sanity checking code
437 xfs_bmap_sanity_check(
438 struct xfs_mount
*mp
,
442 struct xfs_btree_block
*block
= XFS_BUF_TO_BLOCK(bp
);
444 if (block
->bb_magic
!= cpu_to_be32(XFS_BMAP_CRC_MAGIC
) &&
445 block
->bb_magic
!= cpu_to_be32(XFS_BMAP_MAGIC
))
448 if (be16_to_cpu(block
->bb_level
) != level
||
449 be16_to_cpu(block
->bb_numrecs
) == 0 ||
450 be16_to_cpu(block
->bb_numrecs
) > mp
->m_bmap_dmxr
[level
!= 0])
457 STATIC
struct xfs_buf
*
459 struct xfs_btree_cur
*cur
,
462 struct xfs_log_item_desc
*lidp
;
468 for (i
= 0; i
< XFS_BTREE_MAXLEVELS
; i
++) {
469 if (!cur
->bc_bufs
[i
])
471 if (XFS_BUF_ADDR(cur
->bc_bufs
[i
]) == bno
)
472 return cur
->bc_bufs
[i
];
475 /* Chase down all the log items to see if the bp is there */
476 list_for_each_entry(lidp
, &cur
->bc_tp
->t_items
, lid_trans
) {
477 struct xfs_buf_log_item
*bip
;
478 bip
= (struct xfs_buf_log_item
*)lidp
->lid_item
;
479 if (bip
->bli_item
.li_type
== XFS_LI_BUF
&&
480 XFS_BUF_ADDR(bip
->bli_buf
) == bno
)
489 struct xfs_btree_block
*block
,
495 __be64
*pp
, *thispa
; /* pointer to block address */
496 xfs_bmbt_key_t
*prevp
, *keyp
;
498 ASSERT(be16_to_cpu(block
->bb_level
) > 0);
501 for( i
= 1; i
<= xfs_btree_get_numrecs(block
); i
++) {
502 dmxr
= mp
->m_bmap_dmxr
[0];
503 keyp
= XFS_BMBT_KEY_ADDR(mp
, block
, i
);
506 ASSERT(be64_to_cpu(prevp
->br_startoff
) <
507 be64_to_cpu(keyp
->br_startoff
));
512 * Compare the block numbers to see if there are dups.
515 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, i
, sz
);
517 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, i
, dmxr
);
519 for (j
= i
+1; j
<= be16_to_cpu(block
->bb_numrecs
); j
++) {
521 thispa
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, j
, sz
);
523 thispa
= XFS_BMBT_PTR_ADDR(mp
, block
, j
, dmxr
);
524 if (*thispa
== *pp
) {
525 xfs_warn(mp
, "%s: thispa(%d) == pp(%d) %Ld",
527 (unsigned long long)be64_to_cpu(*thispa
));
528 panic("%s: ptrs are equal in node\n",
536 * Check that the extents for the inode ip are in the right order in all
541 xfs_bmap_check_leaf_extents(
542 xfs_btree_cur_t
*cur
, /* btree cursor or null */
543 xfs_inode_t
*ip
, /* incore inode pointer */
544 int whichfork
) /* data or attr fork */
546 struct xfs_btree_block
*block
; /* current btree block */
547 xfs_fsblock_t bno
; /* block # of "block" */
548 xfs_buf_t
*bp
; /* buffer for "block" */
549 int error
; /* error return value */
550 xfs_extnum_t i
=0, j
; /* index into the extents list */
551 xfs_ifork_t
*ifp
; /* fork structure */
552 int level
; /* btree level, for checking */
553 xfs_mount_t
*mp
; /* file system mount structure */
554 __be64
*pp
; /* pointer to block address */
555 xfs_bmbt_rec_t
*ep
; /* pointer to current extent */
556 xfs_bmbt_rec_t last
= {0, 0}; /* last extent in prev block */
557 xfs_bmbt_rec_t
*nextp
; /* pointer to next extent */
560 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
) {
566 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
567 block
= ifp
->if_broot
;
569 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
571 level
= be16_to_cpu(block
->bb_level
);
573 xfs_check_block(block
, mp
, 1, ifp
->if_broot_bytes
);
574 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, 1, ifp
->if_broot_bytes
);
575 bno
= be64_to_cpu(*pp
);
577 ASSERT(bno
!= NULLDFSBNO
);
578 ASSERT(XFS_FSB_TO_AGNO(mp
, bno
) < mp
->m_sb
.sb_agcount
);
579 ASSERT(XFS_FSB_TO_AGBNO(mp
, bno
) < mp
->m_sb
.sb_agblocks
);
582 * Go down the tree until leaf level is reached, following the first
583 * pointer (leftmost) at each level.
585 while (level
-- > 0) {
586 /* See if buf is in cur first */
588 bp
= xfs_bmap_get_bp(cur
, XFS_FSB_TO_DADDR(mp
, bno
));
591 error
= xfs_btree_read_bufl(mp
, NULL
, bno
, 0, &bp
,
597 block
= XFS_BUF_TO_BLOCK(bp
);
598 XFS_WANT_CORRUPTED_GOTO(
599 xfs_bmap_sanity_check(mp
, bp
, level
),
605 * Check this block for basic sanity (increasing keys and
606 * no duplicate blocks).
609 xfs_check_block(block
, mp
, 0, 0);
610 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, mp
->m_bmap_dmxr
[1]);
611 bno
= be64_to_cpu(*pp
);
612 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp
, bno
), error0
);
615 xfs_trans_brelse(NULL
, bp
);
620 * Here with bp and block set to the leftmost leaf node in the tree.
625 * Loop over all leaf nodes checking that all extents are in the right order.
628 xfs_fsblock_t nextbno
;
629 xfs_extnum_t num_recs
;
632 num_recs
= xfs_btree_get_numrecs(block
);
635 * Read-ahead the next leaf block, if any.
638 nextbno
= be64_to_cpu(block
->bb_u
.l
.bb_rightsib
);
641 * Check all the extents to make sure they are OK.
642 * If we had a previous block, the last entry should
643 * conform with the first entry in this one.
646 ep
= XFS_BMBT_REC_ADDR(mp
, block
, 1);
648 ASSERT(xfs_bmbt_disk_get_startoff(&last
) +
649 xfs_bmbt_disk_get_blockcount(&last
) <=
650 xfs_bmbt_disk_get_startoff(ep
));
652 for (j
= 1; j
< num_recs
; j
++) {
653 nextp
= XFS_BMBT_REC_ADDR(mp
, block
, j
+ 1);
654 ASSERT(xfs_bmbt_disk_get_startoff(ep
) +
655 xfs_bmbt_disk_get_blockcount(ep
) <=
656 xfs_bmbt_disk_get_startoff(nextp
));
664 xfs_trans_brelse(NULL
, bp
);
668 * If we've reached the end, stop.
670 if (bno
== NULLFSBLOCK
)
674 bp
= xfs_bmap_get_bp(cur
, XFS_FSB_TO_DADDR(mp
, bno
));
677 error
= xfs_btree_read_bufl(mp
, NULL
, bno
, 0, &bp
,
683 block
= XFS_BUF_TO_BLOCK(bp
);
687 xfs_trans_brelse(NULL
, bp
);
692 xfs_warn(mp
, "%s: at error0", __func__
);
694 xfs_trans_brelse(NULL
, bp
);
696 xfs_warn(mp
, "%s: BAD after btree leaves for %d extents",
698 panic("%s: CORRUPTED BTREE OR SOMETHING", __func__
);
703 * Add bmap trace insert entries for all the contents of the extent records.
706 xfs_bmap_trace_exlist(
707 xfs_inode_t
*ip
, /* incore inode pointer */
708 xfs_extnum_t cnt
, /* count of entries in the list */
709 int whichfork
, /* data or attr fork */
710 unsigned long caller_ip
)
712 xfs_extnum_t idx
; /* extent record index */
713 xfs_ifork_t
*ifp
; /* inode fork pointer */
716 if (whichfork
== XFS_ATTR_FORK
)
717 state
|= BMAP_ATTRFORK
;
719 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
720 ASSERT(cnt
== (ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
)));
721 for (idx
= 0; idx
< cnt
; idx
++)
722 trace_xfs_extlist(ip
, idx
, whichfork
, caller_ip
);
726 * Validate that the bmbt_irecs being returned from bmapi are valid
727 * given the callers original parameters. Specifically check the
728 * ranges of the returned irecs to ensure that they only extent beyond
729 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
732 xfs_bmap_validate_ret(
736 xfs_bmbt_irec_t
*mval
,
740 int i
; /* index to map values */
742 ASSERT(ret_nmap
<= nmap
);
744 for (i
= 0; i
< ret_nmap
; i
++) {
745 ASSERT(mval
[i
].br_blockcount
> 0);
746 if (!(flags
& XFS_BMAPI_ENTIRE
)) {
747 ASSERT(mval
[i
].br_startoff
>= bno
);
748 ASSERT(mval
[i
].br_blockcount
<= len
);
749 ASSERT(mval
[i
].br_startoff
+ mval
[i
].br_blockcount
<=
752 ASSERT(mval
[i
].br_startoff
< bno
+ len
);
753 ASSERT(mval
[i
].br_startoff
+ mval
[i
].br_blockcount
>
757 mval
[i
- 1].br_startoff
+ mval
[i
- 1].br_blockcount
==
758 mval
[i
].br_startoff
);
759 ASSERT(mval
[i
].br_startblock
!= DELAYSTARTBLOCK
&&
760 mval
[i
].br_startblock
!= HOLESTARTBLOCK
);
761 ASSERT(mval
[i
].br_state
== XFS_EXT_NORM
||
762 mval
[i
].br_state
== XFS_EXT_UNWRITTEN
);
767 #define xfs_bmap_check_leaf_extents(cur, ip, whichfork) do { } while (0)
768 #define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap)
772 * bmap free list manipulation functions
776 * Add the extent to the list of extents to be free at transaction end.
777 * The list is maintained sorted (by block number).
781 xfs_fsblock_t bno
, /* fs block number of extent */
782 xfs_filblks_t len
, /* length of extent */
783 xfs_bmap_free_t
*flist
, /* list of extents */
784 xfs_mount_t
*mp
) /* mount point structure */
786 xfs_bmap_free_item_t
*cur
; /* current (next) element */
787 xfs_bmap_free_item_t
*new; /* new element */
788 xfs_bmap_free_item_t
*prev
; /* previous element */
793 ASSERT(bno
!= NULLFSBLOCK
);
795 ASSERT(len
<= MAXEXTLEN
);
796 ASSERT(!isnullstartblock(bno
));
797 agno
= XFS_FSB_TO_AGNO(mp
, bno
);
798 agbno
= XFS_FSB_TO_AGBNO(mp
, bno
);
799 ASSERT(agno
< mp
->m_sb
.sb_agcount
);
800 ASSERT(agbno
< mp
->m_sb
.sb_agblocks
);
801 ASSERT(len
< mp
->m_sb
.sb_agblocks
);
802 ASSERT(agbno
+ len
<= mp
->m_sb
.sb_agblocks
);
804 ASSERT(xfs_bmap_free_item_zone
!= NULL
);
805 new = kmem_zone_alloc(xfs_bmap_free_item_zone
, KM_SLEEP
);
806 new->xbfi_startblock
= bno
;
807 new->xbfi_blockcount
= (xfs_extlen_t
)len
;
808 for (prev
= NULL
, cur
= flist
->xbf_first
;
810 prev
= cur
, cur
= cur
->xbfi_next
) {
811 if (cur
->xbfi_startblock
>= bno
)
815 prev
->xbfi_next
= new;
817 flist
->xbf_first
= new;
818 new->xbfi_next
= cur
;
823 * Remove the entry "free" from the free item list. Prev points to the
824 * previous entry, unless "free" is the head of the list.
828 xfs_bmap_free_t
*flist
, /* free item list header */
829 xfs_bmap_free_item_t
*prev
, /* previous item on list, if any */
830 xfs_bmap_free_item_t
*free
) /* list item to be freed */
833 prev
->xbfi_next
= free
->xbfi_next
;
835 flist
->xbf_first
= free
->xbfi_next
;
837 kmem_zone_free(xfs_bmap_free_item_zone
, free
);
842 * Routine to be called at transaction's end by xfs_bmapi, xfs_bunmapi
843 * caller. Frees all the extents that need freeing, which must be done
844 * last due to locking considerations. We never free any extents in
845 * the first transaction.
847 * Return 1 if the given transaction was committed and a new one
848 * started, and 0 otherwise in the committed parameter.
852 xfs_trans_t
**tp
, /* transaction pointer addr */
853 xfs_bmap_free_t
*flist
, /* i/o: list extents to free */
854 int *committed
) /* xact committed or not */
856 xfs_efd_log_item_t
*efd
; /* extent free data */
857 xfs_efi_log_item_t
*efi
; /* extent free intention */
858 int error
; /* error return value */
859 xfs_bmap_free_item_t
*free
; /* free extent item */
860 unsigned int logres
; /* new log reservation */
861 unsigned int logcount
; /* new log count */
862 xfs_mount_t
*mp
; /* filesystem mount structure */
863 xfs_bmap_free_item_t
*next
; /* next item on free list */
864 xfs_trans_t
*ntp
; /* new transaction pointer */
866 ASSERT((*tp
)->t_flags
& XFS_TRANS_PERM_LOG_RES
);
867 if (flist
->xbf_count
== 0) {
872 efi
= xfs_trans_get_efi(ntp
, flist
->xbf_count
);
873 for (free
= flist
->xbf_first
; free
; free
= free
->xbfi_next
)
874 xfs_trans_log_efi_extent(ntp
, efi
, free
->xbfi_startblock
,
875 free
->xbfi_blockcount
);
876 logres
= ntp
->t_log_res
;
877 logcount
= ntp
->t_log_count
;
878 ntp
= xfs_trans_dup(*tp
);
879 error
= xfs_trans_commit(*tp
, 0);
883 * We have a new transaction, so we should return committed=1,
884 * even though we're returning an error.
890 * transaction commit worked ok so we can drop the extra ticket
891 * reference that we gained in xfs_trans_dup()
893 xfs_log_ticket_put(ntp
->t_ticket
);
895 if ((error
= xfs_trans_reserve(ntp
, 0, logres
, 0, XFS_TRANS_PERM_LOG_RES
,
898 efd
= xfs_trans_get_efd(ntp
, efi
, flist
->xbf_count
);
899 for (free
= flist
->xbf_first
; free
!= NULL
; free
= next
) {
900 next
= free
->xbfi_next
;
901 if ((error
= xfs_free_extent(ntp
, free
->xbfi_startblock
,
902 free
->xbfi_blockcount
))) {
904 * The bmap free list will be cleaned up at a
905 * higher level. The EFI will be canceled when
906 * this transaction is aborted.
907 * Need to force shutdown here to make sure it
908 * happens, since this transaction may not be
912 if (!XFS_FORCED_SHUTDOWN(mp
))
913 xfs_force_shutdown(mp
,
914 (error
== EFSCORRUPTED
) ?
915 SHUTDOWN_CORRUPT_INCORE
:
916 SHUTDOWN_META_IO_ERROR
);
919 xfs_trans_log_efd_extent(ntp
, efd
, free
->xbfi_startblock
,
920 free
->xbfi_blockcount
);
921 xfs_bmap_del_free(flist
, NULL
, free
);
927 * Free up any items left in the list.
931 xfs_bmap_free_t
*flist
) /* list of bmap_free_items */
933 xfs_bmap_free_item_t
*free
; /* free list item */
934 xfs_bmap_free_item_t
*next
;
936 if (flist
->xbf_count
== 0)
938 ASSERT(flist
->xbf_first
!= NULL
);
939 for (free
= flist
->xbf_first
; free
; free
= next
) {
940 next
= free
->xbfi_next
;
941 xfs_bmap_del_free(flist
, NULL
, free
);
943 ASSERT(flist
->xbf_count
== 0);
947 * Inode fork format manipulation functions
951 * Transform a btree format file with only one leaf node, where the
952 * extents list will fit in the inode, into an extents format file.
953 * Since the file extents are already in-core, all we have to do is
954 * give up the space for the btree root and pitch the leaf block.
956 STATIC
int /* error */
957 xfs_bmap_btree_to_extents(
958 xfs_trans_t
*tp
, /* transaction pointer */
959 xfs_inode_t
*ip
, /* incore inode pointer */
960 xfs_btree_cur_t
*cur
, /* btree cursor */
961 int *logflagsp
, /* inode logging flags */
962 int whichfork
) /* data or attr fork */
965 struct xfs_btree_block
*cblock
;/* child btree block */
966 xfs_fsblock_t cbno
; /* child block number */
967 xfs_buf_t
*cbp
; /* child block's buffer */
968 int error
; /* error return value */
969 xfs_ifork_t
*ifp
; /* inode fork data */
970 xfs_mount_t
*mp
; /* mount point structure */
971 __be64
*pp
; /* ptr to block address */
972 struct xfs_btree_block
*rblock
;/* root btree block */
975 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
976 ASSERT(ifp
->if_flags
& XFS_IFEXTENTS
);
977 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
);
978 rblock
= ifp
->if_broot
;
979 ASSERT(be16_to_cpu(rblock
->bb_level
) == 1);
980 ASSERT(be16_to_cpu(rblock
->bb_numrecs
) == 1);
981 ASSERT(xfs_bmbt_maxrecs(mp
, ifp
->if_broot_bytes
, 0) == 1);
982 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, rblock
, 1, ifp
->if_broot_bytes
);
983 cbno
= be64_to_cpu(*pp
);
986 if ((error
= xfs_btree_check_lptr(cur
, cbno
, 1)))
989 error
= xfs_btree_read_bufl(mp
, tp
, cbno
, 0, &cbp
, XFS_BMAP_BTREE_REF
,
993 cblock
= XFS_BUF_TO_BLOCK(cbp
);
994 if ((error
= xfs_btree_check_block(cur
, cblock
, 0, cbp
)))
996 xfs_bmap_add_free(cbno
, 1, cur
->bc_private
.b
.flist
, mp
);
997 ip
->i_d
.di_nblocks
--;
998 xfs_trans_mod_dquot_byino(tp
, ip
, XFS_TRANS_DQ_BCOUNT
, -1L);
999 xfs_trans_binval(tp
, cbp
);
1000 if (cur
->bc_bufs
[0] == cbp
)
1001 cur
->bc_bufs
[0] = NULL
;
1002 xfs_iroot_realloc(ip
, -1, whichfork
);
1003 ASSERT(ifp
->if_broot
== NULL
);
1004 ASSERT((ifp
->if_flags
& XFS_IFBROOT
) == 0);
1005 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_EXTENTS
);
1006 *logflagsp
= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
1011 * Convert an extents-format file into a btree-format file.
1012 * The new file will have a root block (in the inode) and a single child block.
1014 STATIC
int /* error */
1015 xfs_bmap_extents_to_btree(
1016 xfs_trans_t
*tp
, /* transaction pointer */
1017 xfs_inode_t
*ip
, /* incore inode pointer */
1018 xfs_fsblock_t
*firstblock
, /* first-block-allocated */
1019 xfs_bmap_free_t
*flist
, /* blocks freed in xaction */
1020 xfs_btree_cur_t
**curp
, /* cursor returned to caller */
1021 int wasdel
, /* converting a delayed alloc */
1022 int *logflagsp
, /* inode logging flags */
1023 int whichfork
) /* data or attr fork */
1025 struct xfs_btree_block
*ablock
; /* allocated (child) bt block */
1026 xfs_buf_t
*abp
; /* buffer for ablock */
1027 xfs_alloc_arg_t args
; /* allocation arguments */
1028 xfs_bmbt_rec_t
*arp
; /* child record pointer */
1029 struct xfs_btree_block
*block
; /* btree root block */
1030 xfs_btree_cur_t
*cur
; /* bmap btree cursor */
1031 xfs_bmbt_rec_host_t
*ep
; /* extent record pointer */
1032 int error
; /* error return value */
1033 xfs_extnum_t i
, cnt
; /* extent record index */
1034 xfs_ifork_t
*ifp
; /* inode fork pointer */
1035 xfs_bmbt_key_t
*kp
; /* root block key pointer */
1036 xfs_mount_t
*mp
; /* mount structure */
1037 xfs_extnum_t nextents
; /* number of file extents */
1038 xfs_bmbt_ptr_t
*pp
; /* root block address pointer */
1041 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1042 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
);
1045 * Make space in the inode incore.
1047 xfs_iroot_realloc(ip
, 1, whichfork
);
1048 ifp
->if_flags
|= XFS_IFBROOT
;
1053 block
= ifp
->if_broot
;
1054 if (xfs_sb_version_hascrc(&mp
->m_sb
))
1055 xfs_btree_init_block_int(mp
, block
, XFS_BUF_DADDR_NULL
,
1056 XFS_BMAP_CRC_MAGIC
, 1, 1, ip
->i_ino
,
1057 XFS_BTREE_LONG_PTRS
| XFS_BTREE_CRC_BLOCKS
);
1059 xfs_btree_init_block_int(mp
, block
, XFS_BUF_DADDR_NULL
,
1060 XFS_BMAP_MAGIC
, 1, 1, ip
->i_ino
,
1061 XFS_BTREE_LONG_PTRS
);
1064 * Need a cursor. Can't allocate until bb_level is filled in.
1066 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
1067 cur
->bc_private
.b
.firstblock
= *firstblock
;
1068 cur
->bc_private
.b
.flist
= flist
;
1069 cur
->bc_private
.b
.flags
= wasdel
? XFS_BTCUR_BPRV_WASDEL
: 0;
1071 * Convert to a btree with two levels, one record in root.
1073 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_BTREE
);
1074 memset(&args
, 0, sizeof(args
));
1077 args
.firstblock
= *firstblock
;
1078 if (*firstblock
== NULLFSBLOCK
) {
1079 args
.type
= XFS_ALLOCTYPE_START_BNO
;
1080 args
.fsbno
= XFS_INO_TO_FSB(mp
, ip
->i_ino
);
1081 } else if (flist
->xbf_low
) {
1082 args
.type
= XFS_ALLOCTYPE_START_BNO
;
1083 args
.fsbno
= *firstblock
;
1085 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
1086 args
.fsbno
= *firstblock
;
1088 args
.minlen
= args
.maxlen
= args
.prod
= 1;
1089 args
.wasdel
= wasdel
;
1091 if ((error
= xfs_alloc_vextent(&args
))) {
1092 xfs_iroot_realloc(ip
, -1, whichfork
);
1093 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
1097 * Allocation can't fail, the space was reserved.
1099 ASSERT(args
.fsbno
!= NULLFSBLOCK
);
1100 ASSERT(*firstblock
== NULLFSBLOCK
||
1101 args
.agno
== XFS_FSB_TO_AGNO(mp
, *firstblock
) ||
1103 args
.agno
> XFS_FSB_TO_AGNO(mp
, *firstblock
)));
1104 *firstblock
= cur
->bc_private
.b
.firstblock
= args
.fsbno
;
1105 cur
->bc_private
.b
.allocated
++;
1106 ip
->i_d
.di_nblocks
++;
1107 xfs_trans_mod_dquot_byino(tp
, ip
, XFS_TRANS_DQ_BCOUNT
, 1L);
1108 abp
= xfs_btree_get_bufl(mp
, tp
, args
.fsbno
, 0);
1110 * Fill in the child block.
1112 abp
->b_ops
= &xfs_bmbt_buf_ops
;
1113 ablock
= XFS_BUF_TO_BLOCK(abp
);
1114 if (xfs_sb_version_hascrc(&mp
->m_sb
))
1115 xfs_btree_init_block_int(mp
, ablock
, abp
->b_bn
,
1116 XFS_BMAP_CRC_MAGIC
, 0, 0, ip
->i_ino
,
1117 XFS_BTREE_LONG_PTRS
| XFS_BTREE_CRC_BLOCKS
);
1119 xfs_btree_init_block_int(mp
, ablock
, abp
->b_bn
,
1120 XFS_BMAP_MAGIC
, 0, 0, ip
->i_ino
,
1121 XFS_BTREE_LONG_PTRS
);
1123 arp
= XFS_BMBT_REC_ADDR(mp
, ablock
, 1);
1124 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
1125 for (cnt
= i
= 0; i
< nextents
; i
++) {
1126 ep
= xfs_iext_get_ext(ifp
, i
);
1127 if (!isnullstartblock(xfs_bmbt_get_startblock(ep
))) {
1128 arp
->l0
= cpu_to_be64(ep
->l0
);
1129 arp
->l1
= cpu_to_be64(ep
->l1
);
1133 ASSERT(cnt
== XFS_IFORK_NEXTENTS(ip
, whichfork
));
1134 xfs_btree_set_numrecs(ablock
, cnt
);
1137 * Fill in the root key and pointer.
1139 kp
= XFS_BMBT_KEY_ADDR(mp
, block
, 1);
1140 arp
= XFS_BMBT_REC_ADDR(mp
, ablock
, 1);
1141 kp
->br_startoff
= cpu_to_be64(xfs_bmbt_disk_get_startoff(arp
));
1142 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, xfs_bmbt_get_maxrecs(cur
,
1143 be16_to_cpu(block
->bb_level
)));
1144 *pp
= cpu_to_be64(args
.fsbno
);
1147 * Do all this logging at the end so that
1148 * the root is at the right level.
1150 xfs_btree_log_block(cur
, abp
, XFS_BB_ALL_BITS
);
1151 xfs_btree_log_recs(cur
, abp
, 1, be16_to_cpu(ablock
->bb_numrecs
));
1152 ASSERT(*curp
== NULL
);
1154 *logflagsp
= XFS_ILOG_CORE
| xfs_ilog_fbroot(whichfork
);
1159 * Convert a local file to an extents file.
1160 * This code is out of bounds for data forks of regular files,
1161 * since the file data needs to get logged so things will stay consistent.
1162 * (The bmap-level manipulations are ok, though).
1164 STATIC
int /* error */
1165 xfs_bmap_local_to_extents(
1166 xfs_trans_t
*tp
, /* transaction pointer */
1167 xfs_inode_t
*ip
, /* incore inode pointer */
1168 xfs_fsblock_t
*firstblock
, /* first block allocated in xaction */
1169 xfs_extlen_t total
, /* total blocks needed by transaction */
1170 int *logflagsp
, /* inode logging flags */
1172 void (*init_fn
)(struct xfs_trans
*tp
,
1174 struct xfs_inode
*ip
,
1175 struct xfs_ifork
*ifp
))
1177 int error
; /* error return value */
1178 int flags
; /* logging flags returned */
1179 xfs_ifork_t
*ifp
; /* inode fork pointer */
1182 * We don't want to deal with the case of keeping inode data inline yet.
1183 * So sending the data fork of a regular inode is invalid.
1185 ASSERT(!(S_ISREG(ip
->i_d
.di_mode
) && whichfork
== XFS_DATA_FORK
));
1186 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1187 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
1190 if (ifp
->if_bytes
) {
1191 xfs_alloc_arg_t args
; /* allocation arguments */
1192 xfs_buf_t
*bp
; /* buffer for extent block */
1193 xfs_bmbt_rec_host_t
*ep
;/* extent record pointer */
1195 ASSERT((ifp
->if_flags
&
1196 (XFS_IFINLINE
|XFS_IFEXTENTS
|XFS_IFEXTIREC
)) == XFS_IFINLINE
);
1197 memset(&args
, 0, sizeof(args
));
1199 args
.mp
= ip
->i_mount
;
1200 args
.firstblock
= *firstblock
;
1202 * Allocate a block. We know we need only one, since the
1203 * file currently fits in an inode.
1205 if (*firstblock
== NULLFSBLOCK
) {
1206 args
.fsbno
= XFS_INO_TO_FSB(args
.mp
, ip
->i_ino
);
1207 args
.type
= XFS_ALLOCTYPE_START_BNO
;
1209 args
.fsbno
= *firstblock
;
1210 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
1213 args
.minlen
= args
.maxlen
= args
.prod
= 1;
1214 error
= xfs_alloc_vextent(&args
);
1218 /* Can't fail, the space was reserved. */
1219 ASSERT(args
.fsbno
!= NULLFSBLOCK
);
1220 ASSERT(args
.len
== 1);
1221 *firstblock
= args
.fsbno
;
1222 bp
= xfs_btree_get_bufl(args
.mp
, tp
, args
.fsbno
, 0);
1224 /* initialise the block and copy the data */
1225 init_fn(tp
, bp
, ip
, ifp
);
1227 /* account for the change in fork size and log everything */
1228 xfs_trans_log_buf(tp
, bp
, 0, ifp
->if_bytes
- 1);
1229 xfs_bmap_forkoff_reset(args
.mp
, ip
, whichfork
);
1230 xfs_idata_realloc(ip
, -ifp
->if_bytes
, whichfork
);
1231 xfs_iext_add(ifp
, 0, 1);
1232 ep
= xfs_iext_get_ext(ifp
, 0);
1233 xfs_bmbt_set_allf(ep
, 0, args
.fsbno
, 1, XFS_EXT_NORM
);
1234 trace_xfs_bmap_post_update(ip
, 0,
1235 whichfork
== XFS_ATTR_FORK
? BMAP_ATTRFORK
: 0,
1237 XFS_IFORK_NEXT_SET(ip
, whichfork
, 1);
1238 ip
->i_d
.di_nblocks
= 1;
1239 xfs_trans_mod_dquot_byino(tp
, ip
,
1240 XFS_TRANS_DQ_BCOUNT
, 1L);
1241 flags
|= xfs_ilog_fext(whichfork
);
1243 ASSERT(XFS_IFORK_NEXTENTS(ip
, whichfork
) == 0);
1244 xfs_bmap_forkoff_reset(ip
->i_mount
, ip
, whichfork
);
1246 ifp
->if_flags
&= ~XFS_IFINLINE
;
1247 ifp
->if_flags
|= XFS_IFEXTENTS
;
1248 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_EXTENTS
);
1249 flags
|= XFS_ILOG_CORE
;
1256 * Called from xfs_bmap_add_attrfork to handle btree format files.
1258 STATIC
int /* error */
1259 xfs_bmap_add_attrfork_btree(
1260 xfs_trans_t
*tp
, /* transaction pointer */
1261 xfs_inode_t
*ip
, /* incore inode pointer */
1262 xfs_fsblock_t
*firstblock
, /* first block allocated */
1263 xfs_bmap_free_t
*flist
, /* blocks to free at commit */
1264 int *flags
) /* inode logging flags */
1266 xfs_btree_cur_t
*cur
; /* btree cursor */
1267 int error
; /* error return value */
1268 xfs_mount_t
*mp
; /* file system mount struct */
1269 int stat
; /* newroot status */
1272 if (ip
->i_df
.if_broot_bytes
<= XFS_IFORK_DSIZE(ip
))
1273 *flags
|= XFS_ILOG_DBROOT
;
1275 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, XFS_DATA_FORK
);
1276 cur
->bc_private
.b
.flist
= flist
;
1277 cur
->bc_private
.b
.firstblock
= *firstblock
;
1278 if ((error
= xfs_bmbt_lookup_ge(cur
, 0, 0, 0, &stat
)))
1280 /* must be at least one entry */
1281 XFS_WANT_CORRUPTED_GOTO(stat
== 1, error0
);
1282 if ((error
= xfs_btree_new_iroot(cur
, flags
, &stat
)))
1285 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
1286 return XFS_ERROR(ENOSPC
);
1288 *firstblock
= cur
->bc_private
.b
.firstblock
;
1289 cur
->bc_private
.b
.allocated
= 0;
1290 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
1294 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
1299 * Called from xfs_bmap_add_attrfork to handle extents format files.
1301 STATIC
int /* error */
1302 xfs_bmap_add_attrfork_extents(
1303 xfs_trans_t
*tp
, /* transaction pointer */
1304 xfs_inode_t
*ip
, /* incore inode pointer */
1305 xfs_fsblock_t
*firstblock
, /* first block allocated */
1306 xfs_bmap_free_t
*flist
, /* blocks to free at commit */
1307 int *flags
) /* inode logging flags */
1309 xfs_btree_cur_t
*cur
; /* bmap btree cursor */
1310 int error
; /* error return value */
1312 if (ip
->i_d
.di_nextents
* sizeof(xfs_bmbt_rec_t
) <= XFS_IFORK_DSIZE(ip
))
1315 error
= xfs_bmap_extents_to_btree(tp
, ip
, firstblock
, flist
, &cur
, 0,
1316 flags
, XFS_DATA_FORK
);
1318 cur
->bc_private
.b
.allocated
= 0;
1319 xfs_btree_del_cursor(cur
,
1320 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
1326 * Block initialisation function for local to extent format conversion.
1328 * This shouldn't actually be called by anyone, so make sure debug kernels cause
1329 * a noticable failure.
1332 xfs_bmap_local_to_extents_init_fn(
1333 struct xfs_trans
*tp
,
1335 struct xfs_inode
*ip
,
1336 struct xfs_ifork
*ifp
)
1339 bp
->b_ops
= &xfs_bmbt_buf_ops
;
1340 memcpy(bp
->b_addr
, ifp
->if_u1
.if_data
, ifp
->if_bytes
);
1341 xfs_trans_buf_set_type(tp
, bp
, XFS_BLF_BTREE_BUF
);
1345 * Called from xfs_bmap_add_attrfork to handle local format files. Each
1346 * different data fork content type needs a different callout to do the
1347 * conversion. Some are basic and only require special block initialisation
1348 * callouts for the data formating, others (directories) are so specialised they
1349 * handle everything themselves.
1351 * XXX (dgc): investigate whether directory conversion can use the generic
1352 * formatting callout. It should be possible - it's just a very complex
1355 STATIC
int /* error */
1356 xfs_bmap_add_attrfork_local(
1357 xfs_trans_t
*tp
, /* transaction pointer */
1358 xfs_inode_t
*ip
, /* incore inode pointer */
1359 xfs_fsblock_t
*firstblock
, /* first block allocated */
1360 xfs_bmap_free_t
*flist
, /* blocks to free at commit */
1361 int *flags
) /* inode logging flags */
1363 xfs_da_args_t dargs
; /* args for dir/attr code */
1365 if (ip
->i_df
.if_bytes
<= XFS_IFORK_DSIZE(ip
))
1368 if (S_ISDIR(ip
->i_d
.di_mode
)) {
1369 memset(&dargs
, 0, sizeof(dargs
));
1371 dargs
.firstblock
= firstblock
;
1372 dargs
.flist
= flist
;
1373 dargs
.total
= ip
->i_mount
->m_dirblkfsbs
;
1374 dargs
.whichfork
= XFS_DATA_FORK
;
1376 return xfs_dir2_sf_to_block(&dargs
);
1379 if (S_ISLNK(ip
->i_d
.di_mode
))
1380 return xfs_bmap_local_to_extents(tp
, ip
, firstblock
, 1,
1381 flags
, XFS_DATA_FORK
,
1382 xfs_symlink_local_to_remote
);
1384 return xfs_bmap_local_to_extents(tp
, ip
, firstblock
, 1, flags
,
1386 xfs_bmap_local_to_extents_init_fn
);
1390 * Convert inode from non-attributed to attributed.
1391 * Must not be in a transaction, ip must not be locked.
1393 int /* error code */
1394 xfs_bmap_add_attrfork(
1395 xfs_inode_t
*ip
, /* incore inode pointer */
1396 int size
, /* space new attribute needs */
1397 int rsvd
) /* xact may use reserved blks */
1399 xfs_fsblock_t firstblock
; /* 1st block/ag allocated */
1400 xfs_bmap_free_t flist
; /* freed extent records */
1401 xfs_mount_t
*mp
; /* mount structure */
1402 xfs_trans_t
*tp
; /* transaction pointer */
1403 int blks
; /* space reservation */
1404 int version
= 1; /* superblock attr version */
1405 int committed
; /* xaction was committed */
1406 int logflags
; /* logging flags */
1407 int error
; /* error return value */
1409 ASSERT(XFS_IFORK_Q(ip
) == 0);
1412 ASSERT(!XFS_NOT_DQATTACHED(mp
, ip
));
1413 tp
= xfs_trans_alloc(mp
, XFS_TRANS_ADDAFORK
);
1414 blks
= XFS_ADDAFORK_SPACE_RES(mp
);
1416 tp
->t_flags
|= XFS_TRANS_RESERVE
;
1417 if ((error
= xfs_trans_reserve(tp
, blks
, XFS_ADDAFORK_LOG_RES(mp
), 0,
1418 XFS_TRANS_PERM_LOG_RES
, XFS_ADDAFORK_LOG_COUNT
)))
1420 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
1421 error
= xfs_trans_reserve_quota_nblks(tp
, ip
, blks
, 0, rsvd
?
1422 XFS_QMOPT_RES_REGBLKS
| XFS_QMOPT_FORCE_RES
:
1423 XFS_QMOPT_RES_REGBLKS
);
1425 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
1426 xfs_trans_cancel(tp
, XFS_TRANS_RELEASE_LOG_RES
);
1429 if (XFS_IFORK_Q(ip
))
1431 if (ip
->i_d
.di_aformat
!= XFS_DINODE_FMT_EXTENTS
) {
1433 * For inodes coming from pre-6.2 filesystems.
1435 ASSERT(ip
->i_d
.di_aformat
== 0);
1436 ip
->i_d
.di_aformat
= XFS_DINODE_FMT_EXTENTS
;
1438 ASSERT(ip
->i_d
.di_anextents
== 0);
1440 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
);
1441 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
1443 switch (ip
->i_d
.di_format
) {
1444 case XFS_DINODE_FMT_DEV
:
1445 ip
->i_d
.di_forkoff
= roundup(sizeof(xfs_dev_t
), 8) >> 3;
1447 case XFS_DINODE_FMT_UUID
:
1448 ip
->i_d
.di_forkoff
= roundup(sizeof(uuid_t
), 8) >> 3;
1450 case XFS_DINODE_FMT_LOCAL
:
1451 case XFS_DINODE_FMT_EXTENTS
:
1452 case XFS_DINODE_FMT_BTREE
:
1453 ip
->i_d
.di_forkoff
= xfs_attr_shortform_bytesfit(ip
, size
);
1454 if (!ip
->i_d
.di_forkoff
)
1455 ip
->i_d
.di_forkoff
= xfs_default_attroffset(ip
) >> 3;
1456 else if (mp
->m_flags
& XFS_MOUNT_ATTR2
)
1461 error
= XFS_ERROR(EINVAL
);
1465 ASSERT(ip
->i_afp
== NULL
);
1466 ip
->i_afp
= kmem_zone_zalloc(xfs_ifork_zone
, KM_SLEEP
);
1467 ip
->i_afp
->if_flags
= XFS_IFEXTENTS
;
1469 xfs_bmap_init(&flist
, &firstblock
);
1470 switch (ip
->i_d
.di_format
) {
1471 case XFS_DINODE_FMT_LOCAL
:
1472 error
= xfs_bmap_add_attrfork_local(tp
, ip
, &firstblock
, &flist
,
1475 case XFS_DINODE_FMT_EXTENTS
:
1476 error
= xfs_bmap_add_attrfork_extents(tp
, ip
, &firstblock
,
1479 case XFS_DINODE_FMT_BTREE
:
1480 error
= xfs_bmap_add_attrfork_btree(tp
, ip
, &firstblock
, &flist
,
1488 xfs_trans_log_inode(tp
, ip
, logflags
);
1491 if (!xfs_sb_version_hasattr(&mp
->m_sb
) ||
1492 (!xfs_sb_version_hasattr2(&mp
->m_sb
) && version
== 2)) {
1493 __int64_t sbfields
= 0;
1495 spin_lock(&mp
->m_sb_lock
);
1496 if (!xfs_sb_version_hasattr(&mp
->m_sb
)) {
1497 xfs_sb_version_addattr(&mp
->m_sb
);
1498 sbfields
|= XFS_SB_VERSIONNUM
;
1500 if (!xfs_sb_version_hasattr2(&mp
->m_sb
) && version
== 2) {
1501 xfs_sb_version_addattr2(&mp
->m_sb
);
1502 sbfields
|= (XFS_SB_VERSIONNUM
| XFS_SB_FEATURES2
);
1505 spin_unlock(&mp
->m_sb_lock
);
1506 xfs_mod_sb(tp
, sbfields
);
1508 spin_unlock(&mp
->m_sb_lock
);
1511 error
= xfs_bmap_finish(&tp
, &flist
, &committed
);
1514 return xfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
);
1516 xfs_bmap_cancel(&flist
);
1518 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
1520 xfs_trans_cancel(tp
, XFS_TRANS_RELEASE_LOG_RES
|XFS_TRANS_ABORT
);
1525 * Internal and external extent tree search functions.
1529 * Read in the extents to if_extents.
1530 * All inode fields are set up by caller, we just traverse the btree
1531 * and copy the records in. If the file system cannot contain unwritten
1532 * extents, the records are checked for no "state" flags.
1535 xfs_bmap_read_extents(
1536 xfs_trans_t
*tp
, /* transaction pointer */
1537 xfs_inode_t
*ip
, /* incore inode */
1538 int whichfork
) /* data or attr fork */
1540 struct xfs_btree_block
*block
; /* current btree block */
1541 xfs_fsblock_t bno
; /* block # of "block" */
1542 xfs_buf_t
*bp
; /* buffer for "block" */
1543 int error
; /* error return value */
1544 xfs_exntfmt_t exntf
; /* XFS_EXTFMT_NOSTATE, if checking */
1545 xfs_extnum_t i
, j
; /* index into the extents list */
1546 xfs_ifork_t
*ifp
; /* fork structure */
1547 int level
; /* btree level, for checking */
1548 xfs_mount_t
*mp
; /* file system mount structure */
1549 __be64
*pp
; /* pointer to block address */
1551 xfs_extnum_t room
; /* number of entries there's room for */
1555 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1556 exntf
= (whichfork
!= XFS_DATA_FORK
) ? XFS_EXTFMT_NOSTATE
:
1557 XFS_EXTFMT_INODE(ip
);
1558 block
= ifp
->if_broot
;
1560 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
1562 level
= be16_to_cpu(block
->bb_level
);
1564 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, 1, ifp
->if_broot_bytes
);
1565 bno
= be64_to_cpu(*pp
);
1566 ASSERT(bno
!= NULLDFSBNO
);
1567 ASSERT(XFS_FSB_TO_AGNO(mp
, bno
) < mp
->m_sb
.sb_agcount
);
1568 ASSERT(XFS_FSB_TO_AGBNO(mp
, bno
) < mp
->m_sb
.sb_agblocks
);
1570 * Go down the tree until leaf level is reached, following the first
1571 * pointer (leftmost) at each level.
1573 while (level
-- > 0) {
1574 error
= xfs_btree_read_bufl(mp
, tp
, bno
, 0, &bp
,
1575 XFS_BMAP_BTREE_REF
, &xfs_bmbt_buf_ops
);
1578 block
= XFS_BUF_TO_BLOCK(bp
);
1579 XFS_WANT_CORRUPTED_GOTO(
1580 xfs_bmap_sanity_check(mp
, bp
, level
),
1584 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, mp
->m_bmap_dmxr
[1]);
1585 bno
= be64_to_cpu(*pp
);
1586 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp
, bno
), error0
);
1587 xfs_trans_brelse(tp
, bp
);
1590 * Here with bp and block set to the leftmost leaf node in the tree.
1592 room
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
1595 * Loop over all leaf nodes. Copy information to the extent records.
1598 xfs_bmbt_rec_t
*frp
;
1599 xfs_fsblock_t nextbno
;
1600 xfs_extnum_t num_recs
;
1603 num_recs
= xfs_btree_get_numrecs(block
);
1604 if (unlikely(i
+ num_recs
> room
)) {
1605 ASSERT(i
+ num_recs
<= room
);
1606 xfs_warn(ip
->i_mount
,
1607 "corrupt dinode %Lu, (btree extents).",
1608 (unsigned long long) ip
->i_ino
);
1609 XFS_CORRUPTION_ERROR("xfs_bmap_read_extents(1)",
1610 XFS_ERRLEVEL_LOW
, ip
->i_mount
, block
);
1613 XFS_WANT_CORRUPTED_GOTO(
1614 xfs_bmap_sanity_check(mp
, bp
, 0),
1617 * Read-ahead the next leaf block, if any.
1619 nextbno
= be64_to_cpu(block
->bb_u
.l
.bb_rightsib
);
1620 if (nextbno
!= NULLFSBLOCK
)
1621 xfs_btree_reada_bufl(mp
, nextbno
, 1,
1624 * Copy records into the extent records.
1626 frp
= XFS_BMBT_REC_ADDR(mp
, block
, 1);
1628 for (j
= 0; j
< num_recs
; j
++, i
++, frp
++) {
1629 xfs_bmbt_rec_host_t
*trp
= xfs_iext_get_ext(ifp
, i
);
1630 trp
->l0
= be64_to_cpu(frp
->l0
);
1631 trp
->l1
= be64_to_cpu(frp
->l1
);
1633 if (exntf
== XFS_EXTFMT_NOSTATE
) {
1635 * Check all attribute bmap btree records and
1636 * any "older" data bmap btree records for a
1637 * set bit in the "extent flag" position.
1639 if (unlikely(xfs_check_nostate_extents(ifp
,
1640 start
, num_recs
))) {
1641 XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
1647 xfs_trans_brelse(tp
, bp
);
1650 * If we've reached the end, stop.
1652 if (bno
== NULLFSBLOCK
)
1654 error
= xfs_btree_read_bufl(mp
, tp
, bno
, 0, &bp
,
1655 XFS_BMAP_BTREE_REF
, &xfs_bmbt_buf_ops
);
1658 block
= XFS_BUF_TO_BLOCK(bp
);
1660 ASSERT(i
== (ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
)));
1661 ASSERT(i
== XFS_IFORK_NEXTENTS(ip
, whichfork
));
1662 XFS_BMAP_TRACE_EXLIST(ip
, i
, whichfork
);
1665 xfs_trans_brelse(tp
, bp
);
1666 return XFS_ERROR(EFSCORRUPTED
);
1671 * Search the extent records for the entry containing block bno.
1672 * If bno lies in a hole, point to the next entry. If bno lies
1673 * past eof, *eofp will be set, and *prevp will contain the last
1674 * entry (null if none). Else, *lastxp will be set to the index
1675 * of the found entry; *gotp will contain the entry.
1677 STATIC xfs_bmbt_rec_host_t
* /* pointer to found extent entry */
1678 xfs_bmap_search_multi_extents(
1679 xfs_ifork_t
*ifp
, /* inode fork pointer */
1680 xfs_fileoff_t bno
, /* block number searched for */
1681 int *eofp
, /* out: end of file found */
1682 xfs_extnum_t
*lastxp
, /* out: last extent index */
1683 xfs_bmbt_irec_t
*gotp
, /* out: extent entry found */
1684 xfs_bmbt_irec_t
*prevp
) /* out: previous extent entry found */
1686 xfs_bmbt_rec_host_t
*ep
; /* extent record pointer */
1687 xfs_extnum_t lastx
; /* last extent index */
1690 * Initialize the extent entry structure to catch access to
1691 * uninitialized br_startblock field.
1693 gotp
->br_startoff
= 0xffa5a5a5a5a5a5a5LL
;
1694 gotp
->br_blockcount
= 0xa55a5a5a5a5a5a5aLL
;
1695 gotp
->br_state
= XFS_EXT_INVALID
;
1697 gotp
->br_startblock
= 0xffffa5a5a5a5a5a5LL
;
1699 gotp
->br_startblock
= 0xffffa5a5;
1701 prevp
->br_startoff
= NULLFILEOFF
;
1703 ep
= xfs_iext_bno_to_ext(ifp
, bno
, &lastx
);
1705 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, lastx
- 1), prevp
);
1707 if (lastx
< (ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
))) {
1708 xfs_bmbt_get_all(ep
, gotp
);
1722 * Search the extents list for the inode, for the extent containing bno.
1723 * If bno lies in a hole, point to the next entry. If bno lies past eof,
1724 * *eofp will be set, and *prevp will contain the last entry (null if none).
1725 * Else, *lastxp will be set to the index of the found
1726 * entry; *gotp will contain the entry.
1728 STATIC xfs_bmbt_rec_host_t
* /* pointer to found extent entry */
1729 xfs_bmap_search_extents(
1730 xfs_inode_t
*ip
, /* incore inode pointer */
1731 xfs_fileoff_t bno
, /* block number searched for */
1732 int fork
, /* data or attr fork */
1733 int *eofp
, /* out: end of file found */
1734 xfs_extnum_t
*lastxp
, /* out: last extent index */
1735 xfs_bmbt_irec_t
*gotp
, /* out: extent entry found */
1736 xfs_bmbt_irec_t
*prevp
) /* out: previous extent entry found */
1738 xfs_ifork_t
*ifp
; /* inode fork pointer */
1739 xfs_bmbt_rec_host_t
*ep
; /* extent record pointer */
1741 XFS_STATS_INC(xs_look_exlist
);
1742 ifp
= XFS_IFORK_PTR(ip
, fork
);
1744 ep
= xfs_bmap_search_multi_extents(ifp
, bno
, eofp
, lastxp
, gotp
, prevp
);
1746 if (unlikely(!(gotp
->br_startblock
) && (*lastxp
!= NULLEXTNUM
) &&
1747 !(XFS_IS_REALTIME_INODE(ip
) && fork
== XFS_DATA_FORK
))) {
1748 xfs_alert_tag(ip
->i_mount
, XFS_PTAG_FSBLOCK_ZERO
,
1749 "Access to block zero in inode %llu "
1750 "start_block: %llx start_off: %llx "
1751 "blkcnt: %llx extent-state: %x lastx: %x\n",
1752 (unsigned long long)ip
->i_ino
,
1753 (unsigned long long)gotp
->br_startblock
,
1754 (unsigned long long)gotp
->br_startoff
,
1755 (unsigned long long)gotp
->br_blockcount
,
1756 gotp
->br_state
, *lastxp
);
1757 *lastxp
= NULLEXTNUM
;
1765 * Returns the file-relative block number of the first unused block(s)
1766 * in the file with at least "len" logically contiguous blocks free.
1767 * This is the lowest-address hole if the file has holes, else the first block
1768 * past the end of file.
1769 * Return 0 if the file is currently local (in-inode).
1772 xfs_bmap_first_unused(
1773 xfs_trans_t
*tp
, /* transaction pointer */
1774 xfs_inode_t
*ip
, /* incore inode */
1775 xfs_extlen_t len
, /* size of hole to find */
1776 xfs_fileoff_t
*first_unused
, /* unused block */
1777 int whichfork
) /* data or attr fork */
1779 int error
; /* error return value */
1780 int idx
; /* extent record index */
1781 xfs_ifork_t
*ifp
; /* inode fork pointer */
1782 xfs_fileoff_t lastaddr
; /* last block number seen */
1783 xfs_fileoff_t lowest
; /* lowest useful block */
1784 xfs_fileoff_t max
; /* starting useful block */
1785 xfs_fileoff_t off
; /* offset for this block */
1786 xfs_extnum_t nextents
; /* number of extent entries */
1788 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
||
1789 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
||
1790 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
1791 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
) {
1795 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1796 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
1797 (error
= xfs_iread_extents(tp
, ip
, whichfork
)))
1799 lowest
= *first_unused
;
1800 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
1801 for (idx
= 0, lastaddr
= 0, max
= lowest
; idx
< nextents
; idx
++) {
1802 xfs_bmbt_rec_host_t
*ep
= xfs_iext_get_ext(ifp
, idx
);
1803 off
= xfs_bmbt_get_startoff(ep
);
1805 * See if the hole before this extent will work.
1807 if (off
>= lowest
+ len
&& off
- max
>= len
) {
1808 *first_unused
= max
;
1811 lastaddr
= off
+ xfs_bmbt_get_blockcount(ep
);
1812 max
= XFS_FILEOFF_MAX(lastaddr
, lowest
);
1814 *first_unused
= max
;
1819 * Returns the file-relative block number of the last block + 1 before
1820 * last_block (input value) in the file.
1821 * This is not based on i_size, it is based on the extent records.
1822 * Returns 0 for local files, as they do not have extent records.
1825 xfs_bmap_last_before(
1826 xfs_trans_t
*tp
, /* transaction pointer */
1827 xfs_inode_t
*ip
, /* incore inode */
1828 xfs_fileoff_t
*last_block
, /* last block */
1829 int whichfork
) /* data or attr fork */
1831 xfs_fileoff_t bno
; /* input file offset */
1832 int eof
; /* hit end of file */
1833 xfs_bmbt_rec_host_t
*ep
; /* pointer to last extent */
1834 int error
; /* error return value */
1835 xfs_bmbt_irec_t got
; /* current extent value */
1836 xfs_ifork_t
*ifp
; /* inode fork pointer */
1837 xfs_extnum_t lastx
; /* last extent used */
1838 xfs_bmbt_irec_t prev
; /* previous extent value */
1840 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
&&
1841 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
1842 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_LOCAL
)
1843 return XFS_ERROR(EIO
);
1844 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
) {
1848 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1849 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
1850 (error
= xfs_iread_extents(tp
, ip
, whichfork
)))
1852 bno
= *last_block
- 1;
1853 ep
= xfs_bmap_search_extents(ip
, bno
, whichfork
, &eof
, &lastx
, &got
,
1855 if (eof
|| xfs_bmbt_get_startoff(ep
) > bno
) {
1856 if (prev
.br_startoff
== NULLFILEOFF
)
1859 *last_block
= prev
.br_startoff
+ prev
.br_blockcount
;
1862 * Otherwise *last_block is already the right answer.
1868 xfs_bmap_last_extent(
1869 struct xfs_trans
*tp
,
1870 struct xfs_inode
*ip
,
1872 struct xfs_bmbt_irec
*rec
,
1875 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1879 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
1880 error
= xfs_iread_extents(tp
, ip
, whichfork
);
1885 nextents
= ifp
->if_bytes
/ sizeof(xfs_bmbt_rec_t
);
1886 if (nextents
== 0) {
1891 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, nextents
- 1), rec
);
1897 * Check the last inode extent to determine whether this allocation will result
1898 * in blocks being allocated at the end of the file. When we allocate new data
1899 * blocks at the end of the file which do not start at the previous data block,
1900 * we will try to align the new blocks at stripe unit boundaries.
1902 * Returns 0 in bma->aeof if the file (fork) is empty as any new write will be
1903 * at, or past the EOF.
1907 struct xfs_bmalloca
*bma
,
1910 struct xfs_bmbt_irec rec
;
1915 error
= xfs_bmap_last_extent(NULL
, bma
->ip
, whichfork
, &rec
,
1917 if (error
|| is_empty
)
1921 * Check if we are allocation or past the last extent, or at least into
1922 * the last delayed allocated extent.
1924 bma
->aeof
= bma
->offset
>= rec
.br_startoff
+ rec
.br_blockcount
||
1925 (bma
->offset
>= rec
.br_startoff
&&
1926 isnullstartblock(rec
.br_startblock
));
1931 * Check if the endoff is outside the last extent. If so the caller will grow
1932 * the allocation to a stripe unit boundary. All offsets are considered outside
1933 * the end of file for an empty fork, so 1 is returned in *eof in that case.
1937 struct xfs_inode
*ip
,
1938 xfs_fileoff_t endoff
,
1942 struct xfs_bmbt_irec rec
;
1945 error
= xfs_bmap_last_extent(NULL
, ip
, whichfork
, &rec
, eof
);
1949 *eof
= endoff
>= rec
.br_startoff
+ rec
.br_blockcount
;
1954 * Returns the file-relative block number of the first block past eof in
1955 * the file. This is not based on i_size, it is based on the extent records.
1956 * Returns 0 for local files, as they do not have extent records.
1959 xfs_bmap_last_offset(
1960 struct xfs_trans
*tp
,
1961 struct xfs_inode
*ip
,
1962 xfs_fileoff_t
*last_block
,
1965 struct xfs_bmbt_irec rec
;
1971 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
)
1974 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
&&
1975 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
1976 return XFS_ERROR(EIO
);
1978 error
= xfs_bmap_last_extent(NULL
, ip
, whichfork
, &rec
, &is_empty
);
1979 if (error
|| is_empty
)
1982 *last_block
= rec
.br_startoff
+ rec
.br_blockcount
;
1987 * Returns whether the selected fork of the inode has exactly one
1988 * block or not. For the data fork we check this matches di_size,
1989 * implying the file's range is 0..bsize-1.
1991 int /* 1=>1 block, 0=>otherwise */
1993 xfs_inode_t
*ip
, /* incore inode */
1994 int whichfork
) /* data or attr fork */
1996 xfs_bmbt_rec_host_t
*ep
; /* ptr to fork's extent */
1997 xfs_ifork_t
*ifp
; /* inode fork pointer */
1998 int rval
; /* return value */
1999 xfs_bmbt_irec_t s
; /* internal version of extent */
2002 if (whichfork
== XFS_DATA_FORK
)
2003 return XFS_ISIZE(ip
) == ip
->i_mount
->m_sb
.sb_blocksize
;
2005 if (XFS_IFORK_NEXTENTS(ip
, whichfork
) != 1)
2007 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
2009 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
2010 ASSERT(ifp
->if_flags
& XFS_IFEXTENTS
);
2011 ep
= xfs_iext_get_ext(ifp
, 0);
2012 xfs_bmbt_get_all(ep
, &s
);
2013 rval
= s
.br_startoff
== 0 && s
.br_blockcount
== 1;
2014 if (rval
&& whichfork
== XFS_DATA_FORK
)
2015 ASSERT(XFS_ISIZE(ip
) == ip
->i_mount
->m_sb
.sb_blocksize
);
2020 * Extent tree manipulation functions used during allocation.
2024 * Convert a delayed allocation to a real allocation.
2026 STATIC
int /* error */
2027 xfs_bmap_add_extent_delay_real(
2028 struct xfs_bmalloca
*bma
)
2030 struct xfs_bmbt_irec
*new = &bma
->got
;
2031 int diff
; /* temp value */
2032 xfs_bmbt_rec_host_t
*ep
; /* extent entry for idx */
2033 int error
; /* error return value */
2034 int i
; /* temp state */
2035 xfs_ifork_t
*ifp
; /* inode fork pointer */
2036 xfs_fileoff_t new_endoff
; /* end offset of new entry */
2037 xfs_bmbt_irec_t r
[3]; /* neighbor extent entries */
2038 /* left is 0, right is 1, prev is 2 */
2039 int rval
=0; /* return value (logging flags) */
2040 int state
= 0;/* state bits, accessed thru macros */
2041 xfs_filblks_t da_new
; /* new count del alloc blocks used */
2042 xfs_filblks_t da_old
; /* old count del alloc blocks used */
2043 xfs_filblks_t temp
=0; /* value for da_new calculations */
2044 xfs_filblks_t temp2
=0;/* value for da_new calculations */
2045 int tmp_rval
; /* partial logging flags */
2047 ifp
= XFS_IFORK_PTR(bma
->ip
, XFS_DATA_FORK
);
2049 ASSERT(bma
->idx
>= 0);
2050 ASSERT(bma
->idx
<= ifp
->if_bytes
/ sizeof(struct xfs_bmbt_rec
));
2051 ASSERT(!isnullstartblock(new->br_startblock
));
2053 (bma
->cur
->bc_private
.b
.flags
& XFS_BTCUR_BPRV_WASDEL
));
2055 XFS_STATS_INC(xs_add_exlist
);
2062 * Set up a bunch of variables to make the tests simpler.
2064 ep
= xfs_iext_get_ext(ifp
, bma
->idx
);
2065 xfs_bmbt_get_all(ep
, &PREV
);
2066 new_endoff
= new->br_startoff
+ new->br_blockcount
;
2067 ASSERT(PREV
.br_startoff
<= new->br_startoff
);
2068 ASSERT(PREV
.br_startoff
+ PREV
.br_blockcount
>= new_endoff
);
2070 da_old
= startblockval(PREV
.br_startblock
);
2074 * Set flags determining what part of the previous delayed allocation
2075 * extent is being replaced by a real allocation.
2077 if (PREV
.br_startoff
== new->br_startoff
)
2078 state
|= BMAP_LEFT_FILLING
;
2079 if (PREV
.br_startoff
+ PREV
.br_blockcount
== new_endoff
)
2080 state
|= BMAP_RIGHT_FILLING
;
2083 * Check and set flags if this segment has a left neighbor.
2084 * Don't set contiguous if the combined extent would be too large.
2087 state
|= BMAP_LEFT_VALID
;
2088 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
- 1), &LEFT
);
2090 if (isnullstartblock(LEFT
.br_startblock
))
2091 state
|= BMAP_LEFT_DELAY
;
2094 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
2095 LEFT
.br_startoff
+ LEFT
.br_blockcount
== new->br_startoff
&&
2096 LEFT
.br_startblock
+ LEFT
.br_blockcount
== new->br_startblock
&&
2097 LEFT
.br_state
== new->br_state
&&
2098 LEFT
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
2099 state
|= BMAP_LEFT_CONTIG
;
2102 * Check and set flags if this segment has a right neighbor.
2103 * Don't set contiguous if the combined extent would be too large.
2104 * Also check for all-three-contiguous being too large.
2106 if (bma
->idx
< bma
->ip
->i_df
.if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
) - 1) {
2107 state
|= BMAP_RIGHT_VALID
;
2108 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
+ 1), &RIGHT
);
2110 if (isnullstartblock(RIGHT
.br_startblock
))
2111 state
|= BMAP_RIGHT_DELAY
;
2114 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
2115 new_endoff
== RIGHT
.br_startoff
&&
2116 new->br_startblock
+ new->br_blockcount
== RIGHT
.br_startblock
&&
2117 new->br_state
== RIGHT
.br_state
&&
2118 new->br_blockcount
+ RIGHT
.br_blockcount
<= MAXEXTLEN
&&
2119 ((state
& (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
2120 BMAP_RIGHT_FILLING
)) !=
2121 (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
2122 BMAP_RIGHT_FILLING
) ||
2123 LEFT
.br_blockcount
+ new->br_blockcount
+ RIGHT
.br_blockcount
2125 state
|= BMAP_RIGHT_CONTIG
;
2129 * Switch out based on the FILLING and CONTIG state bits.
2131 switch (state
& (BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
2132 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
)) {
2133 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
2134 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2136 * Filling in all of a previously delayed allocation extent.
2137 * The left and right neighbors are both contiguous with new.
2140 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2141 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, bma
->idx
),
2142 LEFT
.br_blockcount
+ PREV
.br_blockcount
+
2143 RIGHT
.br_blockcount
);
2144 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2146 xfs_iext_remove(bma
->ip
, bma
->idx
+ 1, 2, state
);
2147 bma
->ip
->i_d
.di_nextents
--;
2148 if (bma
->cur
== NULL
)
2149 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2151 rval
= XFS_ILOG_CORE
;
2152 error
= xfs_bmbt_lookup_eq(bma
->cur
, RIGHT
.br_startoff
,
2153 RIGHT
.br_startblock
,
2154 RIGHT
.br_blockcount
, &i
);
2157 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2158 error
= xfs_btree_delete(bma
->cur
, &i
);
2161 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2162 error
= xfs_btree_decrement(bma
->cur
, 0, &i
);
2165 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2166 error
= xfs_bmbt_update(bma
->cur
, LEFT
.br_startoff
,
2168 LEFT
.br_blockcount
+
2169 PREV
.br_blockcount
+
2170 RIGHT
.br_blockcount
, LEFT
.br_state
);
2176 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
2178 * Filling in all of a previously delayed allocation extent.
2179 * The left neighbor is contiguous, the right is not.
2183 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2184 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, bma
->idx
),
2185 LEFT
.br_blockcount
+ PREV
.br_blockcount
);
2186 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2188 xfs_iext_remove(bma
->ip
, bma
->idx
+ 1, 1, state
);
2189 if (bma
->cur
== NULL
)
2190 rval
= XFS_ILOG_DEXT
;
2193 error
= xfs_bmbt_lookup_eq(bma
->cur
, LEFT
.br_startoff
,
2194 LEFT
.br_startblock
, LEFT
.br_blockcount
,
2198 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2199 error
= xfs_bmbt_update(bma
->cur
, LEFT
.br_startoff
,
2201 LEFT
.br_blockcount
+
2202 PREV
.br_blockcount
, LEFT
.br_state
);
2208 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2210 * Filling in all of a previously delayed allocation extent.
2211 * The right neighbor is contiguous, the left is not.
2213 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2214 xfs_bmbt_set_startblock(ep
, new->br_startblock
);
2215 xfs_bmbt_set_blockcount(ep
,
2216 PREV
.br_blockcount
+ RIGHT
.br_blockcount
);
2217 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2219 xfs_iext_remove(bma
->ip
, bma
->idx
+ 1, 1, state
);
2220 if (bma
->cur
== NULL
)
2221 rval
= XFS_ILOG_DEXT
;
2224 error
= xfs_bmbt_lookup_eq(bma
->cur
, RIGHT
.br_startoff
,
2225 RIGHT
.br_startblock
,
2226 RIGHT
.br_blockcount
, &i
);
2229 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2230 error
= xfs_bmbt_update(bma
->cur
, PREV
.br_startoff
,
2232 PREV
.br_blockcount
+
2233 RIGHT
.br_blockcount
, PREV
.br_state
);
2239 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
2241 * Filling in all of a previously delayed allocation extent.
2242 * Neither the left nor right neighbors are contiguous with
2245 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2246 xfs_bmbt_set_startblock(ep
, new->br_startblock
);
2247 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2249 bma
->ip
->i_d
.di_nextents
++;
2250 if (bma
->cur
== NULL
)
2251 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2253 rval
= XFS_ILOG_CORE
;
2254 error
= xfs_bmbt_lookup_eq(bma
->cur
, new->br_startoff
,
2255 new->br_startblock
, new->br_blockcount
,
2259 XFS_WANT_CORRUPTED_GOTO(i
== 0, done
);
2260 bma
->cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
2261 error
= xfs_btree_insert(bma
->cur
, &i
);
2264 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2268 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
:
2270 * Filling in the first part of a previous delayed allocation.
2271 * The left neighbor is contiguous.
2273 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
- 1, state
, _THIS_IP_
);
2274 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, bma
->idx
- 1),
2275 LEFT
.br_blockcount
+ new->br_blockcount
);
2276 xfs_bmbt_set_startoff(ep
,
2277 PREV
.br_startoff
+ new->br_blockcount
);
2278 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
- 1, state
, _THIS_IP_
);
2280 temp
= PREV
.br_blockcount
- new->br_blockcount
;
2281 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2282 xfs_bmbt_set_blockcount(ep
, temp
);
2283 if (bma
->cur
== NULL
)
2284 rval
= XFS_ILOG_DEXT
;
2287 error
= xfs_bmbt_lookup_eq(bma
->cur
, LEFT
.br_startoff
,
2288 LEFT
.br_startblock
, LEFT
.br_blockcount
,
2292 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2293 error
= xfs_bmbt_update(bma
->cur
, LEFT
.br_startoff
,
2295 LEFT
.br_blockcount
+
2301 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
2302 startblockval(PREV
.br_startblock
));
2303 xfs_bmbt_set_startblock(ep
, nullstartblock(da_new
));
2304 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2309 case BMAP_LEFT_FILLING
:
2311 * Filling in the first part of a previous delayed allocation.
2312 * The left neighbor is not contiguous.
2314 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2315 xfs_bmbt_set_startoff(ep
, new_endoff
);
2316 temp
= PREV
.br_blockcount
- new->br_blockcount
;
2317 xfs_bmbt_set_blockcount(ep
, temp
);
2318 xfs_iext_insert(bma
->ip
, bma
->idx
, 1, new, state
);
2319 bma
->ip
->i_d
.di_nextents
++;
2320 if (bma
->cur
== NULL
)
2321 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2323 rval
= XFS_ILOG_CORE
;
2324 error
= xfs_bmbt_lookup_eq(bma
->cur
, new->br_startoff
,
2325 new->br_startblock
, new->br_blockcount
,
2329 XFS_WANT_CORRUPTED_GOTO(i
== 0, done
);
2330 bma
->cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
2331 error
= xfs_btree_insert(bma
->cur
, &i
);
2334 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2337 if (xfs_bmap_needs_btree(bma
->ip
, XFS_DATA_FORK
)) {
2338 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
2339 bma
->firstblock
, bma
->flist
,
2340 &bma
->cur
, 1, &tmp_rval
, XFS_DATA_FORK
);
2345 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
2346 startblockval(PREV
.br_startblock
) -
2347 (bma
->cur
? bma
->cur
->bc_private
.b
.allocated
: 0));
2348 ep
= xfs_iext_get_ext(ifp
, bma
->idx
+ 1);
2349 xfs_bmbt_set_startblock(ep
, nullstartblock(da_new
));
2350 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
+ 1, state
, _THIS_IP_
);
2353 case BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2355 * Filling in the last part of a previous delayed allocation.
2356 * The right neighbor is contiguous with the new allocation.
2358 temp
= PREV
.br_blockcount
- new->br_blockcount
;
2359 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
+ 1, state
, _THIS_IP_
);
2360 xfs_bmbt_set_blockcount(ep
, temp
);
2361 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp
, bma
->idx
+ 1),
2362 new->br_startoff
, new->br_startblock
,
2363 new->br_blockcount
+ RIGHT
.br_blockcount
,
2365 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
+ 1, state
, _THIS_IP_
);
2366 if (bma
->cur
== NULL
)
2367 rval
= XFS_ILOG_DEXT
;
2370 error
= xfs_bmbt_lookup_eq(bma
->cur
, RIGHT
.br_startoff
,
2371 RIGHT
.br_startblock
,
2372 RIGHT
.br_blockcount
, &i
);
2375 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2376 error
= xfs_bmbt_update(bma
->cur
, new->br_startoff
,
2378 new->br_blockcount
+
2379 RIGHT
.br_blockcount
,
2385 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
2386 startblockval(PREV
.br_startblock
));
2387 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2388 xfs_bmbt_set_startblock(ep
, nullstartblock(da_new
));
2389 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2394 case BMAP_RIGHT_FILLING
:
2396 * Filling in the last part of a previous delayed allocation.
2397 * The right neighbor is not contiguous.
2399 temp
= PREV
.br_blockcount
- new->br_blockcount
;
2400 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2401 xfs_bmbt_set_blockcount(ep
, temp
);
2402 xfs_iext_insert(bma
->ip
, bma
->idx
+ 1, 1, new, state
);
2403 bma
->ip
->i_d
.di_nextents
++;
2404 if (bma
->cur
== NULL
)
2405 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2407 rval
= XFS_ILOG_CORE
;
2408 error
= xfs_bmbt_lookup_eq(bma
->cur
, new->br_startoff
,
2409 new->br_startblock
, new->br_blockcount
,
2413 XFS_WANT_CORRUPTED_GOTO(i
== 0, done
);
2414 bma
->cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
2415 error
= xfs_btree_insert(bma
->cur
, &i
);
2418 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2421 if (xfs_bmap_needs_btree(bma
->ip
, XFS_DATA_FORK
)) {
2422 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
2423 bma
->firstblock
, bma
->flist
, &bma
->cur
, 1,
2424 &tmp_rval
, XFS_DATA_FORK
);
2429 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
2430 startblockval(PREV
.br_startblock
) -
2431 (bma
->cur
? bma
->cur
->bc_private
.b
.allocated
: 0));
2432 ep
= xfs_iext_get_ext(ifp
, bma
->idx
);
2433 xfs_bmbt_set_startblock(ep
, nullstartblock(da_new
));
2434 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2441 * Filling in the middle part of a previous delayed allocation.
2442 * Contiguity is impossible here.
2443 * This case is avoided almost all the time.
2445 * We start with a delayed allocation:
2447 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
2450 * and we are allocating:
2451 * +rrrrrrrrrrrrrrrrr+
2454 * and we set it up for insertion as:
2455 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
2457 * PREV @ idx LEFT RIGHT
2458 * inserted at idx + 1
2460 temp
= new->br_startoff
- PREV
.br_startoff
;
2461 temp2
= PREV
.br_startoff
+ PREV
.br_blockcount
- new_endoff
;
2462 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, 0, _THIS_IP_
);
2463 xfs_bmbt_set_blockcount(ep
, temp
); /* truncate PREV */
2465 RIGHT
.br_state
= PREV
.br_state
;
2466 RIGHT
.br_startblock
= nullstartblock(
2467 (int)xfs_bmap_worst_indlen(bma
->ip
, temp2
));
2468 RIGHT
.br_startoff
= new_endoff
;
2469 RIGHT
.br_blockcount
= temp2
;
2470 /* insert LEFT (r[0]) and RIGHT (r[1]) at the same time */
2471 xfs_iext_insert(bma
->ip
, bma
->idx
+ 1, 2, &LEFT
, state
);
2472 bma
->ip
->i_d
.di_nextents
++;
2473 if (bma
->cur
== NULL
)
2474 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2476 rval
= XFS_ILOG_CORE
;
2477 error
= xfs_bmbt_lookup_eq(bma
->cur
, new->br_startoff
,
2478 new->br_startblock
, new->br_blockcount
,
2482 XFS_WANT_CORRUPTED_GOTO(i
== 0, done
);
2483 bma
->cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
2484 error
= xfs_btree_insert(bma
->cur
, &i
);
2487 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2490 if (xfs_bmap_needs_btree(bma
->ip
, XFS_DATA_FORK
)) {
2491 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
2492 bma
->firstblock
, bma
->flist
, &bma
->cur
,
2493 1, &tmp_rval
, XFS_DATA_FORK
);
2498 temp
= xfs_bmap_worst_indlen(bma
->ip
, temp
);
2499 temp2
= xfs_bmap_worst_indlen(bma
->ip
, temp2
);
2500 diff
= (int)(temp
+ temp2
- startblockval(PREV
.br_startblock
) -
2501 (bma
->cur
? bma
->cur
->bc_private
.b
.allocated
: 0));
2503 error
= xfs_icsb_modify_counters(bma
->ip
->i_mount
,
2505 -((int64_t)diff
), 0);
2511 ep
= xfs_iext_get_ext(ifp
, bma
->idx
);
2512 xfs_bmbt_set_startblock(ep
, nullstartblock((int)temp
));
2513 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2514 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
+ 2, state
, _THIS_IP_
);
2515 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp
, bma
->idx
+ 2),
2516 nullstartblock((int)temp2
));
2517 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
+ 2, state
, _THIS_IP_
);
2520 da_new
= temp
+ temp2
;
2523 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2524 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2525 case BMAP_LEFT_FILLING
| BMAP_RIGHT_CONTIG
:
2526 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
2527 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2528 case BMAP_LEFT_CONTIG
:
2529 case BMAP_RIGHT_CONTIG
:
2531 * These cases are all impossible.
2536 /* convert to a btree if necessary */
2537 if (xfs_bmap_needs_btree(bma
->ip
, XFS_DATA_FORK
)) {
2538 int tmp_logflags
; /* partial log flag return val */
2540 ASSERT(bma
->cur
== NULL
);
2541 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
2542 bma
->firstblock
, bma
->flist
, &bma
->cur
,
2543 da_old
> 0, &tmp_logflags
, XFS_DATA_FORK
);
2544 bma
->logflags
|= tmp_logflags
;
2549 /* adjust for changes in reserved delayed indirect blocks */
2550 if (da_old
|| da_new
) {
2553 temp
+= bma
->cur
->bc_private
.b
.allocated
;
2554 ASSERT(temp
<= da_old
);
2556 xfs_icsb_modify_counters(bma
->ip
->i_mount
,
2558 (int64_t)(da_old
- temp
), 0);
2561 /* clear out the allocated field, done with it now in any case. */
2563 bma
->cur
->bc_private
.b
.allocated
= 0;
2565 xfs_bmap_check_leaf_extents(bma
->cur
, bma
->ip
, XFS_DATA_FORK
);
2567 bma
->logflags
|= rval
;
2575 * Convert an unwritten allocation to a real allocation or vice versa.
2577 STATIC
int /* error */
2578 xfs_bmap_add_extent_unwritten_real(
2579 struct xfs_trans
*tp
,
2580 xfs_inode_t
*ip
, /* incore inode pointer */
2581 xfs_extnum_t
*idx
, /* extent number to update/insert */
2582 xfs_btree_cur_t
**curp
, /* if *curp is null, not a btree */
2583 xfs_bmbt_irec_t
*new, /* new data to add to file extents */
2584 xfs_fsblock_t
*first
, /* pointer to firstblock variable */
2585 xfs_bmap_free_t
*flist
, /* list of extents to be freed */
2586 int *logflagsp
) /* inode logging flags */
2588 xfs_btree_cur_t
*cur
; /* btree cursor */
2589 xfs_bmbt_rec_host_t
*ep
; /* extent entry for idx */
2590 int error
; /* error return value */
2591 int i
; /* temp state */
2592 xfs_ifork_t
*ifp
; /* inode fork pointer */
2593 xfs_fileoff_t new_endoff
; /* end offset of new entry */
2594 xfs_exntst_t newext
; /* new extent state */
2595 xfs_exntst_t oldext
; /* old extent state */
2596 xfs_bmbt_irec_t r
[3]; /* neighbor extent entries */
2597 /* left is 0, right is 1, prev is 2 */
2598 int rval
=0; /* return value (logging flags) */
2599 int state
= 0;/* state bits, accessed thru macros */
2604 ifp
= XFS_IFORK_PTR(ip
, XFS_DATA_FORK
);
2607 ASSERT(*idx
<= ifp
->if_bytes
/ sizeof(struct xfs_bmbt_rec
));
2608 ASSERT(!isnullstartblock(new->br_startblock
));
2610 XFS_STATS_INC(xs_add_exlist
);
2617 * Set up a bunch of variables to make the tests simpler.
2620 ep
= xfs_iext_get_ext(ifp
, *idx
);
2621 xfs_bmbt_get_all(ep
, &PREV
);
2622 newext
= new->br_state
;
2623 oldext
= (newext
== XFS_EXT_UNWRITTEN
) ?
2624 XFS_EXT_NORM
: XFS_EXT_UNWRITTEN
;
2625 ASSERT(PREV
.br_state
== oldext
);
2626 new_endoff
= new->br_startoff
+ new->br_blockcount
;
2627 ASSERT(PREV
.br_startoff
<= new->br_startoff
);
2628 ASSERT(PREV
.br_startoff
+ PREV
.br_blockcount
>= new_endoff
);
2631 * Set flags determining what part of the previous oldext allocation
2632 * extent is being replaced by a newext allocation.
2634 if (PREV
.br_startoff
== new->br_startoff
)
2635 state
|= BMAP_LEFT_FILLING
;
2636 if (PREV
.br_startoff
+ PREV
.br_blockcount
== new_endoff
)
2637 state
|= BMAP_RIGHT_FILLING
;
2640 * Check and set flags if this segment has a left neighbor.
2641 * Don't set contiguous if the combined extent would be too large.
2644 state
|= BMAP_LEFT_VALID
;
2645 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, *idx
- 1), &LEFT
);
2647 if (isnullstartblock(LEFT
.br_startblock
))
2648 state
|= BMAP_LEFT_DELAY
;
2651 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
2652 LEFT
.br_startoff
+ LEFT
.br_blockcount
== new->br_startoff
&&
2653 LEFT
.br_startblock
+ LEFT
.br_blockcount
== new->br_startblock
&&
2654 LEFT
.br_state
== newext
&&
2655 LEFT
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
2656 state
|= BMAP_LEFT_CONTIG
;
2659 * Check and set flags if this segment has a right neighbor.
2660 * Don't set contiguous if the combined extent would be too large.
2661 * Also check for all-three-contiguous being too large.
2663 if (*idx
< ip
->i_df
.if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
) - 1) {
2664 state
|= BMAP_RIGHT_VALID
;
2665 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, *idx
+ 1), &RIGHT
);
2666 if (isnullstartblock(RIGHT
.br_startblock
))
2667 state
|= BMAP_RIGHT_DELAY
;
2670 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
2671 new_endoff
== RIGHT
.br_startoff
&&
2672 new->br_startblock
+ new->br_blockcount
== RIGHT
.br_startblock
&&
2673 newext
== RIGHT
.br_state
&&
2674 new->br_blockcount
+ RIGHT
.br_blockcount
<= MAXEXTLEN
&&
2675 ((state
& (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
2676 BMAP_RIGHT_FILLING
)) !=
2677 (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
2678 BMAP_RIGHT_FILLING
) ||
2679 LEFT
.br_blockcount
+ new->br_blockcount
+ RIGHT
.br_blockcount
2681 state
|= BMAP_RIGHT_CONTIG
;
2684 * Switch out based on the FILLING and CONTIG state bits.
2686 switch (state
& (BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
2687 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
)) {
2688 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
2689 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2691 * Setting all of a previous oldext extent to newext.
2692 * The left and right neighbors are both contiguous with new.
2696 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2697 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
),
2698 LEFT
.br_blockcount
+ PREV
.br_blockcount
+
2699 RIGHT
.br_blockcount
);
2700 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2702 xfs_iext_remove(ip
, *idx
+ 1, 2, state
);
2703 ip
->i_d
.di_nextents
-= 2;
2705 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2707 rval
= XFS_ILOG_CORE
;
2708 if ((error
= xfs_bmbt_lookup_eq(cur
, RIGHT
.br_startoff
,
2709 RIGHT
.br_startblock
,
2710 RIGHT
.br_blockcount
, &i
)))
2712 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2713 if ((error
= xfs_btree_delete(cur
, &i
)))
2715 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2716 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2718 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2719 if ((error
= xfs_btree_delete(cur
, &i
)))
2721 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2722 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2724 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2725 if ((error
= xfs_bmbt_update(cur
, LEFT
.br_startoff
,
2727 LEFT
.br_blockcount
+ PREV
.br_blockcount
+
2728 RIGHT
.br_blockcount
, LEFT
.br_state
)))
2733 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
2735 * Setting all of a previous oldext extent to newext.
2736 * The left neighbor is contiguous, the right is not.
2740 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2741 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
),
2742 LEFT
.br_blockcount
+ PREV
.br_blockcount
);
2743 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2745 xfs_iext_remove(ip
, *idx
+ 1, 1, state
);
2746 ip
->i_d
.di_nextents
--;
2748 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2750 rval
= XFS_ILOG_CORE
;
2751 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2752 PREV
.br_startblock
, PREV
.br_blockcount
,
2755 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2756 if ((error
= xfs_btree_delete(cur
, &i
)))
2758 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2759 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2761 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2762 if ((error
= xfs_bmbt_update(cur
, LEFT
.br_startoff
,
2764 LEFT
.br_blockcount
+ PREV
.br_blockcount
,
2770 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2772 * Setting all of a previous oldext extent to newext.
2773 * The right neighbor is contiguous, the left is not.
2775 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2776 xfs_bmbt_set_blockcount(ep
,
2777 PREV
.br_blockcount
+ RIGHT
.br_blockcount
);
2778 xfs_bmbt_set_state(ep
, newext
);
2779 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2780 xfs_iext_remove(ip
, *idx
+ 1, 1, state
);
2781 ip
->i_d
.di_nextents
--;
2783 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2785 rval
= XFS_ILOG_CORE
;
2786 if ((error
= xfs_bmbt_lookup_eq(cur
, RIGHT
.br_startoff
,
2787 RIGHT
.br_startblock
,
2788 RIGHT
.br_blockcount
, &i
)))
2790 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2791 if ((error
= xfs_btree_delete(cur
, &i
)))
2793 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2794 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2796 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2797 if ((error
= xfs_bmbt_update(cur
, new->br_startoff
,
2799 new->br_blockcount
+ RIGHT
.br_blockcount
,
2805 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
2807 * Setting all of a previous oldext extent to newext.
2808 * Neither the left nor right neighbors are contiguous with
2811 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2812 xfs_bmbt_set_state(ep
, newext
);
2813 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2816 rval
= XFS_ILOG_DEXT
;
2819 if ((error
= xfs_bmbt_lookup_eq(cur
, new->br_startoff
,
2820 new->br_startblock
, new->br_blockcount
,
2823 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2824 if ((error
= xfs_bmbt_update(cur
, new->br_startoff
,
2825 new->br_startblock
, new->br_blockcount
,
2831 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
:
2833 * Setting the first part of a previous oldext extent to newext.
2834 * The left neighbor is contiguous.
2836 trace_xfs_bmap_pre_update(ip
, *idx
- 1, state
, _THIS_IP_
);
2837 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
- 1),
2838 LEFT
.br_blockcount
+ new->br_blockcount
);
2839 xfs_bmbt_set_startoff(ep
,
2840 PREV
.br_startoff
+ new->br_blockcount
);
2841 trace_xfs_bmap_post_update(ip
, *idx
- 1, state
, _THIS_IP_
);
2843 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2844 xfs_bmbt_set_startblock(ep
,
2845 new->br_startblock
+ new->br_blockcount
);
2846 xfs_bmbt_set_blockcount(ep
,
2847 PREV
.br_blockcount
- new->br_blockcount
);
2848 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2853 rval
= XFS_ILOG_DEXT
;
2856 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2857 PREV
.br_startblock
, PREV
.br_blockcount
,
2860 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2861 if ((error
= xfs_bmbt_update(cur
,
2862 PREV
.br_startoff
+ new->br_blockcount
,
2863 PREV
.br_startblock
+ new->br_blockcount
,
2864 PREV
.br_blockcount
- new->br_blockcount
,
2867 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2869 error
= xfs_bmbt_update(cur
, LEFT
.br_startoff
,
2871 LEFT
.br_blockcount
+ new->br_blockcount
,
2878 case BMAP_LEFT_FILLING
:
2880 * Setting the first part of a previous oldext extent to newext.
2881 * The left neighbor is not contiguous.
2883 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2884 ASSERT(ep
&& xfs_bmbt_get_state(ep
) == oldext
);
2885 xfs_bmbt_set_startoff(ep
, new_endoff
);
2886 xfs_bmbt_set_blockcount(ep
,
2887 PREV
.br_blockcount
- new->br_blockcount
);
2888 xfs_bmbt_set_startblock(ep
,
2889 new->br_startblock
+ new->br_blockcount
);
2890 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2892 xfs_iext_insert(ip
, *idx
, 1, new, state
);
2893 ip
->i_d
.di_nextents
++;
2895 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2897 rval
= XFS_ILOG_CORE
;
2898 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2899 PREV
.br_startblock
, PREV
.br_blockcount
,
2902 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2903 if ((error
= xfs_bmbt_update(cur
,
2904 PREV
.br_startoff
+ new->br_blockcount
,
2905 PREV
.br_startblock
+ new->br_blockcount
,
2906 PREV
.br_blockcount
- new->br_blockcount
,
2909 cur
->bc_rec
.b
= *new;
2910 if ((error
= xfs_btree_insert(cur
, &i
)))
2912 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2916 case BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2918 * Setting the last part of a previous oldext extent to newext.
2919 * The right neighbor is contiguous with the new allocation.
2921 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2922 xfs_bmbt_set_blockcount(ep
,
2923 PREV
.br_blockcount
- new->br_blockcount
);
2924 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2928 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2929 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp
, *idx
),
2930 new->br_startoff
, new->br_startblock
,
2931 new->br_blockcount
+ RIGHT
.br_blockcount
, newext
);
2932 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2935 rval
= XFS_ILOG_DEXT
;
2938 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2940 PREV
.br_blockcount
, &i
)))
2942 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2943 if ((error
= xfs_bmbt_update(cur
, PREV
.br_startoff
,
2945 PREV
.br_blockcount
- new->br_blockcount
,
2948 if ((error
= xfs_btree_increment(cur
, 0, &i
)))
2950 if ((error
= xfs_bmbt_update(cur
, new->br_startoff
,
2952 new->br_blockcount
+ RIGHT
.br_blockcount
,
2958 case BMAP_RIGHT_FILLING
:
2960 * Setting the last part of a previous oldext extent to newext.
2961 * The right neighbor is not contiguous.
2963 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2964 xfs_bmbt_set_blockcount(ep
,
2965 PREV
.br_blockcount
- new->br_blockcount
);
2966 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2969 xfs_iext_insert(ip
, *idx
, 1, new, state
);
2971 ip
->i_d
.di_nextents
++;
2973 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2975 rval
= XFS_ILOG_CORE
;
2976 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2977 PREV
.br_startblock
, PREV
.br_blockcount
,
2980 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2981 if ((error
= xfs_bmbt_update(cur
, PREV
.br_startoff
,
2983 PREV
.br_blockcount
- new->br_blockcount
,
2986 if ((error
= xfs_bmbt_lookup_eq(cur
, new->br_startoff
,
2987 new->br_startblock
, new->br_blockcount
,
2990 XFS_WANT_CORRUPTED_GOTO(i
== 0, done
);
2991 cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
2992 if ((error
= xfs_btree_insert(cur
, &i
)))
2994 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
3000 * Setting the middle part of a previous oldext extent to
3001 * newext. Contiguity is impossible here.
3002 * One extent becomes three extents.
3004 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
3005 xfs_bmbt_set_blockcount(ep
,
3006 new->br_startoff
- PREV
.br_startoff
);
3007 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
3010 r
[1].br_startoff
= new_endoff
;
3011 r
[1].br_blockcount
=
3012 PREV
.br_startoff
+ PREV
.br_blockcount
- new_endoff
;
3013 r
[1].br_startblock
= new->br_startblock
+ new->br_blockcount
;
3014 r
[1].br_state
= oldext
;
3017 xfs_iext_insert(ip
, *idx
, 2, &r
[0], state
);
3019 ip
->i_d
.di_nextents
+= 2;
3021 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
3023 rval
= XFS_ILOG_CORE
;
3024 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
3025 PREV
.br_startblock
, PREV
.br_blockcount
,
3028 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
3029 /* new right extent - oldext */
3030 if ((error
= xfs_bmbt_update(cur
, r
[1].br_startoff
,
3031 r
[1].br_startblock
, r
[1].br_blockcount
,
3034 /* new left extent - oldext */
3035 cur
->bc_rec
.b
= PREV
;
3036 cur
->bc_rec
.b
.br_blockcount
=
3037 new->br_startoff
- PREV
.br_startoff
;
3038 if ((error
= xfs_btree_insert(cur
, &i
)))
3040 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
3042 * Reset the cursor to the position of the new extent
3043 * we are about to insert as we can't trust it after
3044 * the previous insert.
3046 if ((error
= xfs_bmbt_lookup_eq(cur
, new->br_startoff
,
3047 new->br_startblock
, new->br_blockcount
,
3050 XFS_WANT_CORRUPTED_GOTO(i
== 0, done
);
3051 /* new middle extent - newext */
3052 cur
->bc_rec
.b
.br_state
= new->br_state
;
3053 if ((error
= xfs_btree_insert(cur
, &i
)))
3055 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
3059 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
3060 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
3061 case BMAP_LEFT_FILLING
| BMAP_RIGHT_CONTIG
:
3062 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
3063 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
3064 case BMAP_LEFT_CONTIG
:
3065 case BMAP_RIGHT_CONTIG
:
3067 * These cases are all impossible.
3072 /* convert to a btree if necessary */
3073 if (xfs_bmap_needs_btree(ip
, XFS_DATA_FORK
)) {
3074 int tmp_logflags
; /* partial log flag return val */
3076 ASSERT(cur
== NULL
);
3077 error
= xfs_bmap_extents_to_btree(tp
, ip
, first
, flist
, &cur
,
3078 0, &tmp_logflags
, XFS_DATA_FORK
);
3079 *logflagsp
|= tmp_logflags
;
3084 /* clear out the allocated field, done with it now in any case. */
3086 cur
->bc_private
.b
.allocated
= 0;
3090 xfs_bmap_check_leaf_extents(*curp
, ip
, XFS_DATA_FORK
);
3100 * Convert a hole to a delayed allocation.
3103 xfs_bmap_add_extent_hole_delay(
3104 xfs_inode_t
*ip
, /* incore inode pointer */
3105 xfs_extnum_t
*idx
, /* extent number to update/insert */
3106 xfs_bmbt_irec_t
*new) /* new data to add to file extents */
3108 xfs_ifork_t
*ifp
; /* inode fork pointer */
3109 xfs_bmbt_irec_t left
; /* left neighbor extent entry */
3110 xfs_filblks_t newlen
=0; /* new indirect size */
3111 xfs_filblks_t oldlen
=0; /* old indirect size */
3112 xfs_bmbt_irec_t right
; /* right neighbor extent entry */
3113 int state
; /* state bits, accessed thru macros */
3114 xfs_filblks_t temp
=0; /* temp for indirect calculations */
3116 ifp
= XFS_IFORK_PTR(ip
, XFS_DATA_FORK
);
3118 ASSERT(isnullstartblock(new->br_startblock
));
3121 * Check and set flags if this segment has a left neighbor
3124 state
|= BMAP_LEFT_VALID
;
3125 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, *idx
- 1), &left
);
3127 if (isnullstartblock(left
.br_startblock
))
3128 state
|= BMAP_LEFT_DELAY
;
3132 * Check and set flags if the current (right) segment exists.
3133 * If it doesn't exist, we're converting the hole at end-of-file.
3135 if (*idx
< ip
->i_df
.if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
)) {
3136 state
|= BMAP_RIGHT_VALID
;
3137 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, *idx
), &right
);
3139 if (isnullstartblock(right
.br_startblock
))
3140 state
|= BMAP_RIGHT_DELAY
;
3144 * Set contiguity flags on the left and right neighbors.
3145 * Don't let extents get too large, even if the pieces are contiguous.
3147 if ((state
& BMAP_LEFT_VALID
) && (state
& BMAP_LEFT_DELAY
) &&
3148 left
.br_startoff
+ left
.br_blockcount
== new->br_startoff
&&
3149 left
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
3150 state
|= BMAP_LEFT_CONTIG
;
3152 if ((state
& BMAP_RIGHT_VALID
) && (state
& BMAP_RIGHT_DELAY
) &&
3153 new->br_startoff
+ new->br_blockcount
== right
.br_startoff
&&
3154 new->br_blockcount
+ right
.br_blockcount
<= MAXEXTLEN
&&
3155 (!(state
& BMAP_LEFT_CONTIG
) ||
3156 (left
.br_blockcount
+ new->br_blockcount
+
3157 right
.br_blockcount
<= MAXEXTLEN
)))
3158 state
|= BMAP_RIGHT_CONTIG
;
3161 * Switch out based on the contiguity flags.
3163 switch (state
& (BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
)) {
3164 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
3166 * New allocation is contiguous with delayed allocations
3167 * on the left and on the right.
3168 * Merge all three into a single extent record.
3171 temp
= left
.br_blockcount
+ new->br_blockcount
+
3172 right
.br_blockcount
;
3174 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
3175 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
), temp
);
3176 oldlen
= startblockval(left
.br_startblock
) +
3177 startblockval(new->br_startblock
) +
3178 startblockval(right
.br_startblock
);
3179 newlen
= xfs_bmap_worst_indlen(ip
, temp
);
3180 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp
, *idx
),
3181 nullstartblock((int)newlen
));
3182 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
3184 xfs_iext_remove(ip
, *idx
+ 1, 1, state
);
3187 case BMAP_LEFT_CONTIG
:
3189 * New allocation is contiguous with a delayed allocation
3191 * Merge the new allocation with the left neighbor.
3194 temp
= left
.br_blockcount
+ new->br_blockcount
;
3196 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
3197 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
), temp
);
3198 oldlen
= startblockval(left
.br_startblock
) +
3199 startblockval(new->br_startblock
);
3200 newlen
= xfs_bmap_worst_indlen(ip
, temp
);
3201 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp
, *idx
),
3202 nullstartblock((int)newlen
));
3203 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
3206 case BMAP_RIGHT_CONTIG
:
3208 * New allocation is contiguous with a delayed allocation
3210 * Merge the new allocation with the right neighbor.
3212 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
3213 temp
= new->br_blockcount
+ right
.br_blockcount
;
3214 oldlen
= startblockval(new->br_startblock
) +
3215 startblockval(right
.br_startblock
);
3216 newlen
= xfs_bmap_worst_indlen(ip
, temp
);
3217 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp
, *idx
),
3219 nullstartblock((int)newlen
), temp
, right
.br_state
);
3220 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
3225 * New allocation is not contiguous with another
3226 * delayed allocation.
3227 * Insert a new entry.
3229 oldlen
= newlen
= 0;
3230 xfs_iext_insert(ip
, *idx
, 1, new, state
);
3233 if (oldlen
!= newlen
) {
3234 ASSERT(oldlen
> newlen
);
3235 xfs_icsb_modify_counters(ip
->i_mount
, XFS_SBS_FDBLOCKS
,
3236 (int64_t)(oldlen
- newlen
), 0);
3238 * Nothing to do for disk quota accounting here.
3244 * Convert a hole to a real allocation.
3246 STATIC
int /* error */
3247 xfs_bmap_add_extent_hole_real(
3248 struct xfs_bmalloca
*bma
,
3251 struct xfs_bmbt_irec
*new = &bma
->got
;
3252 int error
; /* error return value */
3253 int i
; /* temp state */
3254 xfs_ifork_t
*ifp
; /* inode fork pointer */
3255 xfs_bmbt_irec_t left
; /* left neighbor extent entry */
3256 xfs_bmbt_irec_t right
; /* right neighbor extent entry */
3257 int rval
=0; /* return value (logging flags) */
3258 int state
; /* state bits, accessed thru macros */
3260 ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
3262 ASSERT(bma
->idx
>= 0);
3263 ASSERT(bma
->idx
<= ifp
->if_bytes
/ sizeof(struct xfs_bmbt_rec
));
3264 ASSERT(!isnullstartblock(new->br_startblock
));
3266 !(bma
->cur
->bc_private
.b
.flags
& XFS_BTCUR_BPRV_WASDEL
));
3268 XFS_STATS_INC(xs_add_exlist
);
3271 if (whichfork
== XFS_ATTR_FORK
)
3272 state
|= BMAP_ATTRFORK
;
3275 * Check and set flags if this segment has a left neighbor.
3278 state
|= BMAP_LEFT_VALID
;
3279 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
- 1), &left
);
3280 if (isnullstartblock(left
.br_startblock
))
3281 state
|= BMAP_LEFT_DELAY
;
3285 * Check and set flags if this segment has a current value.
3286 * Not true if we're inserting into the "hole" at eof.
3288 if (bma
->idx
< ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
)) {
3289 state
|= BMAP_RIGHT_VALID
;
3290 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
), &right
);
3291 if (isnullstartblock(right
.br_startblock
))
3292 state
|= BMAP_RIGHT_DELAY
;
3296 * We're inserting a real allocation between "left" and "right".
3297 * Set the contiguity flags. Don't let extents get too large.
3299 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
3300 left
.br_startoff
+ left
.br_blockcount
== new->br_startoff
&&
3301 left
.br_startblock
+ left
.br_blockcount
== new->br_startblock
&&
3302 left
.br_state
== new->br_state
&&
3303 left
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
3304 state
|= BMAP_LEFT_CONTIG
;
3306 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
3307 new->br_startoff
+ new->br_blockcount
== right
.br_startoff
&&
3308 new->br_startblock
+ new->br_blockcount
== right
.br_startblock
&&
3309 new->br_state
== right
.br_state
&&
3310 new->br_blockcount
+ right
.br_blockcount
<= MAXEXTLEN
&&
3311 (!(state
& BMAP_LEFT_CONTIG
) ||
3312 left
.br_blockcount
+ new->br_blockcount
+
3313 right
.br_blockcount
<= MAXEXTLEN
))
3314 state
|= BMAP_RIGHT_CONTIG
;
3318 * Select which case we're in here, and implement it.
3320 switch (state
& (BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
)) {
3321 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
3323 * New allocation is contiguous with real allocations on the
3324 * left and on the right.
3325 * Merge all three into a single extent record.
3328 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
3329 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, bma
->idx
),
3330 left
.br_blockcount
+ new->br_blockcount
+
3331 right
.br_blockcount
);
3332 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
3334 xfs_iext_remove(bma
->ip
, bma
->idx
+ 1, 1, state
);
3336 XFS_IFORK_NEXT_SET(bma
->ip
, whichfork
,
3337 XFS_IFORK_NEXTENTS(bma
->ip
, whichfork
) - 1);
3338 if (bma
->cur
== NULL
) {
3339 rval
= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
3341 rval
= XFS_ILOG_CORE
;
3342 error
= xfs_bmbt_lookup_eq(bma
->cur
, right
.br_startoff
,
3343 right
.br_startblock
, right
.br_blockcount
,
3347 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
3348 error
= xfs_btree_delete(bma
->cur
, &i
);
3351 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
3352 error
= xfs_btree_decrement(bma
->cur
, 0, &i
);
3355 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
3356 error
= xfs_bmbt_update(bma
->cur
, left
.br_startoff
,
3358 left
.br_blockcount
+
3359 new->br_blockcount
+
3360 right
.br_blockcount
,
3367 case BMAP_LEFT_CONTIG
:
3369 * New allocation is contiguous with a real allocation
3371 * Merge the new allocation with the left neighbor.
3374 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
3375 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, bma
->idx
),
3376 left
.br_blockcount
+ new->br_blockcount
);
3377 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
3379 if (bma
->cur
== NULL
) {
3380 rval
= xfs_ilog_fext(whichfork
);
3383 error
= xfs_bmbt_lookup_eq(bma
->cur
, left
.br_startoff
,
3384 left
.br_startblock
, left
.br_blockcount
,
3388 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
3389 error
= xfs_bmbt_update(bma
->cur
, left
.br_startoff
,
3391 left
.br_blockcount
+
3399 case BMAP_RIGHT_CONTIG
:
3401 * New allocation is contiguous with a real allocation
3403 * Merge the new allocation with the right neighbor.
3405 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
3406 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp
, bma
->idx
),
3407 new->br_startoff
, new->br_startblock
,
3408 new->br_blockcount
+ right
.br_blockcount
,
3410 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
3412 if (bma
->cur
== NULL
) {
3413 rval
= xfs_ilog_fext(whichfork
);
3416 error
= xfs_bmbt_lookup_eq(bma
->cur
,
3418 right
.br_startblock
,
3419 right
.br_blockcount
, &i
);
3422 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
3423 error
= xfs_bmbt_update(bma
->cur
, new->br_startoff
,
3425 new->br_blockcount
+
3426 right
.br_blockcount
,
3435 * New allocation is not contiguous with another
3437 * Insert a new entry.
3439 xfs_iext_insert(bma
->ip
, bma
->idx
, 1, new, state
);
3440 XFS_IFORK_NEXT_SET(bma
->ip
, whichfork
,
3441 XFS_IFORK_NEXTENTS(bma
->ip
, whichfork
) + 1);
3442 if (bma
->cur
== NULL
) {
3443 rval
= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
3445 rval
= XFS_ILOG_CORE
;
3446 error
= xfs_bmbt_lookup_eq(bma
->cur
,
3449 new->br_blockcount
, &i
);
3452 XFS_WANT_CORRUPTED_GOTO(i
== 0, done
);
3453 bma
->cur
->bc_rec
.b
.br_state
= new->br_state
;
3454 error
= xfs_btree_insert(bma
->cur
, &i
);
3457 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
3462 /* convert to a btree if necessary */
3463 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
3464 int tmp_logflags
; /* partial log flag return val */
3466 ASSERT(bma
->cur
== NULL
);
3467 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
3468 bma
->firstblock
, bma
->flist
, &bma
->cur
,
3469 0, &tmp_logflags
, whichfork
);
3470 bma
->logflags
|= tmp_logflags
;
3475 /* clear out the allocated field, done with it now in any case. */
3477 bma
->cur
->bc_private
.b
.allocated
= 0;
3479 xfs_bmap_check_leaf_extents(bma
->cur
, bma
->ip
, whichfork
);
3481 bma
->logflags
|= rval
;
3486 * Functions used in the extent read, allocate and remove paths
3490 * Adjust the size of the new extent based on di_extsize and rt extsize.
3493 xfs_bmap_extsize_align(
3495 xfs_bmbt_irec_t
*gotp
, /* next extent pointer */
3496 xfs_bmbt_irec_t
*prevp
, /* previous extent pointer */
3497 xfs_extlen_t extsz
, /* align to this extent size */
3498 int rt
, /* is this a realtime inode? */
3499 int eof
, /* is extent at end-of-file? */
3500 int delay
, /* creating delalloc extent? */
3501 int convert
, /* overwriting unwritten extent? */
3502 xfs_fileoff_t
*offp
, /* in/out: aligned offset */
3503 xfs_extlen_t
*lenp
) /* in/out: aligned length */
3505 xfs_fileoff_t orig_off
; /* original offset */
3506 xfs_extlen_t orig_alen
; /* original length */
3507 xfs_fileoff_t orig_end
; /* original off+len */
3508 xfs_fileoff_t nexto
; /* next file offset */
3509 xfs_fileoff_t prevo
; /* previous file offset */
3510 xfs_fileoff_t align_off
; /* temp for offset */
3511 xfs_extlen_t align_alen
; /* temp for length */
3512 xfs_extlen_t temp
; /* temp for calculations */
3517 orig_off
= align_off
= *offp
;
3518 orig_alen
= align_alen
= *lenp
;
3519 orig_end
= orig_off
+ orig_alen
;
3522 * If this request overlaps an existing extent, then don't
3523 * attempt to perform any additional alignment.
3525 if (!delay
&& !eof
&&
3526 (orig_off
>= gotp
->br_startoff
) &&
3527 (orig_end
<= gotp
->br_startoff
+ gotp
->br_blockcount
)) {
3532 * If the file offset is unaligned vs. the extent size
3533 * we need to align it. This will be possible unless
3534 * the file was previously written with a kernel that didn't
3535 * perform this alignment, or if a truncate shot us in the
3538 temp
= do_mod(orig_off
, extsz
);
3544 * Same adjustment for the end of the requested area.
3546 if ((temp
= (align_alen
% extsz
))) {
3547 align_alen
+= extsz
- temp
;
3550 * If the previous block overlaps with this proposed allocation
3551 * then move the start forward without adjusting the length.
3553 if (prevp
->br_startoff
!= NULLFILEOFF
) {
3554 if (prevp
->br_startblock
== HOLESTARTBLOCK
)
3555 prevo
= prevp
->br_startoff
;
3557 prevo
= prevp
->br_startoff
+ prevp
->br_blockcount
;
3560 if (align_off
!= orig_off
&& align_off
< prevo
)
3563 * If the next block overlaps with this proposed allocation
3564 * then move the start back without adjusting the length,
3565 * but not before offset 0.
3566 * This may of course make the start overlap previous block,
3567 * and if we hit the offset 0 limit then the next block
3568 * can still overlap too.
3570 if (!eof
&& gotp
->br_startoff
!= NULLFILEOFF
) {
3571 if ((delay
&& gotp
->br_startblock
== HOLESTARTBLOCK
) ||
3572 (!delay
&& gotp
->br_startblock
== DELAYSTARTBLOCK
))
3573 nexto
= gotp
->br_startoff
+ gotp
->br_blockcount
;
3575 nexto
= gotp
->br_startoff
;
3577 nexto
= NULLFILEOFF
;
3579 align_off
+ align_alen
!= orig_end
&&
3580 align_off
+ align_alen
> nexto
)
3581 align_off
= nexto
> align_alen
? nexto
- align_alen
: 0;
3583 * If we're now overlapping the next or previous extent that
3584 * means we can't fit an extsz piece in this hole. Just move
3585 * the start forward to the first valid spot and set
3586 * the length so we hit the end.
3588 if (align_off
!= orig_off
&& align_off
< prevo
)
3590 if (align_off
+ align_alen
!= orig_end
&&
3591 align_off
+ align_alen
> nexto
&&
3592 nexto
!= NULLFILEOFF
) {
3593 ASSERT(nexto
> prevo
);
3594 align_alen
= nexto
- align_off
;
3598 * If realtime, and the result isn't a multiple of the realtime
3599 * extent size we need to remove blocks until it is.
3601 if (rt
&& (temp
= (align_alen
% mp
->m_sb
.sb_rextsize
))) {
3603 * We're not covering the original request, or
3604 * we won't be able to once we fix the length.
3606 if (orig_off
< align_off
||
3607 orig_end
> align_off
+ align_alen
||
3608 align_alen
- temp
< orig_alen
)
3609 return XFS_ERROR(EINVAL
);
3611 * Try to fix it by moving the start up.
3613 if (align_off
+ temp
<= orig_off
) {
3618 * Try to fix it by moving the end in.
3620 else if (align_off
+ align_alen
- temp
>= orig_end
)
3623 * Set the start to the minimum then trim the length.
3626 align_alen
-= orig_off
- align_off
;
3627 align_off
= orig_off
;
3628 align_alen
-= align_alen
% mp
->m_sb
.sb_rextsize
;
3631 * Result doesn't cover the request, fail it.
3633 if (orig_off
< align_off
|| orig_end
> align_off
+ align_alen
)
3634 return XFS_ERROR(EINVAL
);
3636 ASSERT(orig_off
>= align_off
);
3637 ASSERT(orig_end
<= align_off
+ align_alen
);
3641 if (!eof
&& gotp
->br_startoff
!= NULLFILEOFF
)
3642 ASSERT(align_off
+ align_alen
<= gotp
->br_startoff
);
3643 if (prevp
->br_startoff
!= NULLFILEOFF
)
3644 ASSERT(align_off
>= prevp
->br_startoff
+ prevp
->br_blockcount
);
3652 #define XFS_ALLOC_GAP_UNITS 4
3656 xfs_bmalloca_t
*ap
) /* bmap alloc argument struct */
3658 xfs_fsblock_t adjust
; /* adjustment to block numbers */
3659 xfs_agnumber_t fb_agno
; /* ag number of ap->firstblock */
3660 xfs_mount_t
*mp
; /* mount point structure */
3661 int nullfb
; /* true if ap->firstblock isn't set */
3662 int rt
; /* true if inode is realtime */
3664 #define ISVALID(x,y) \
3666 (x) < mp->m_sb.sb_rblocks : \
3667 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
3668 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
3669 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
3671 mp
= ap
->ip
->i_mount
;
3672 nullfb
= *ap
->firstblock
== NULLFSBLOCK
;
3673 rt
= XFS_IS_REALTIME_INODE(ap
->ip
) && ap
->userdata
;
3674 fb_agno
= nullfb
? NULLAGNUMBER
: XFS_FSB_TO_AGNO(mp
, *ap
->firstblock
);
3676 * If allocating at eof, and there's a previous real block,
3677 * try to use its last block as our starting point.
3679 if (ap
->eof
&& ap
->prev
.br_startoff
!= NULLFILEOFF
&&
3680 !isnullstartblock(ap
->prev
.br_startblock
) &&
3681 ISVALID(ap
->prev
.br_startblock
+ ap
->prev
.br_blockcount
,
3682 ap
->prev
.br_startblock
)) {
3683 ap
->blkno
= ap
->prev
.br_startblock
+ ap
->prev
.br_blockcount
;
3685 * Adjust for the gap between prevp and us.
3687 adjust
= ap
->offset
-
3688 (ap
->prev
.br_startoff
+ ap
->prev
.br_blockcount
);
3690 ISVALID(ap
->blkno
+ adjust
, ap
->prev
.br_startblock
))
3691 ap
->blkno
+= adjust
;
3694 * If not at eof, then compare the two neighbor blocks.
3695 * Figure out whether either one gives us a good starting point,
3696 * and pick the better one.
3698 else if (!ap
->eof
) {
3699 xfs_fsblock_t gotbno
; /* right side block number */
3700 xfs_fsblock_t gotdiff
=0; /* right side difference */
3701 xfs_fsblock_t prevbno
; /* left side block number */
3702 xfs_fsblock_t prevdiff
=0; /* left side difference */
3705 * If there's a previous (left) block, select a requested
3706 * start block based on it.
3708 if (ap
->prev
.br_startoff
!= NULLFILEOFF
&&
3709 !isnullstartblock(ap
->prev
.br_startblock
) &&
3710 (prevbno
= ap
->prev
.br_startblock
+
3711 ap
->prev
.br_blockcount
) &&
3712 ISVALID(prevbno
, ap
->prev
.br_startblock
)) {
3714 * Calculate gap to end of previous block.
3716 adjust
= prevdiff
= ap
->offset
-
3717 (ap
->prev
.br_startoff
+
3718 ap
->prev
.br_blockcount
);
3720 * Figure the startblock based on the previous block's
3721 * end and the gap size.
3723 * If the gap is large relative to the piece we're
3724 * allocating, or using it gives us an invalid block
3725 * number, then just use the end of the previous block.
3727 if (prevdiff
<= XFS_ALLOC_GAP_UNITS
* ap
->length
&&
3728 ISVALID(prevbno
+ prevdiff
,
3729 ap
->prev
.br_startblock
))
3734 * If the firstblock forbids it, can't use it,
3737 if (!rt
&& !nullfb
&&
3738 XFS_FSB_TO_AGNO(mp
, prevbno
) != fb_agno
)
3739 prevbno
= NULLFSBLOCK
;
3742 * No previous block or can't follow it, just default.
3745 prevbno
= NULLFSBLOCK
;
3747 * If there's a following (right) block, select a requested
3748 * start block based on it.
3750 if (!isnullstartblock(ap
->got
.br_startblock
)) {
3752 * Calculate gap to start of next block.
3754 adjust
= gotdiff
= ap
->got
.br_startoff
- ap
->offset
;
3756 * Figure the startblock based on the next block's
3757 * start and the gap size.
3759 gotbno
= ap
->got
.br_startblock
;
3762 * If the gap is large relative to the piece we're
3763 * allocating, or using it gives us an invalid block
3764 * number, then just use the start of the next block
3765 * offset by our length.
3767 if (gotdiff
<= XFS_ALLOC_GAP_UNITS
* ap
->length
&&
3768 ISVALID(gotbno
- gotdiff
, gotbno
))
3770 else if (ISVALID(gotbno
- ap
->length
, gotbno
)) {
3771 gotbno
-= ap
->length
;
3772 gotdiff
+= adjust
- ap
->length
;
3776 * If the firstblock forbids it, can't use it,
3779 if (!rt
&& !nullfb
&&
3780 XFS_FSB_TO_AGNO(mp
, gotbno
) != fb_agno
)
3781 gotbno
= NULLFSBLOCK
;
3784 * No next block, just default.
3787 gotbno
= NULLFSBLOCK
;
3789 * If both valid, pick the better one, else the only good
3790 * one, else ap->blkno is already set (to 0 or the inode block).
3792 if (prevbno
!= NULLFSBLOCK
&& gotbno
!= NULLFSBLOCK
)
3793 ap
->blkno
= prevdiff
<= gotdiff
? prevbno
: gotbno
;
3794 else if (prevbno
!= NULLFSBLOCK
)
3795 ap
->blkno
= prevbno
;
3796 else if (gotbno
!= NULLFSBLOCK
)
3804 xfs_bmalloca_t
*ap
) /* bmap alloc argument struct */
3806 xfs_alloctype_t atype
= 0; /* type for allocation routines */
3807 int error
; /* error return value */
3808 xfs_mount_t
*mp
; /* mount point structure */
3809 xfs_extlen_t prod
= 0; /* product factor for allocators */
3810 xfs_extlen_t ralen
= 0; /* realtime allocation length */
3811 xfs_extlen_t align
; /* minimum allocation alignment */
3814 mp
= ap
->ip
->i_mount
;
3815 align
= xfs_get_extsz_hint(ap
->ip
);
3816 prod
= align
/ mp
->m_sb
.sb_rextsize
;
3817 error
= xfs_bmap_extsize_align(mp
, &ap
->got
, &ap
->prev
,
3818 align
, 1, ap
->eof
, 0,
3819 ap
->conv
, &ap
->offset
, &ap
->length
);
3823 ASSERT(ap
->length
% mp
->m_sb
.sb_rextsize
== 0);
3826 * If the offset & length are not perfectly aligned
3827 * then kill prod, it will just get us in trouble.
3829 if (do_mod(ap
->offset
, align
) || ap
->length
% align
)
3832 * Set ralen to be the actual requested length in rtextents.
3834 ralen
= ap
->length
/ mp
->m_sb
.sb_rextsize
;
3836 * If the old value was close enough to MAXEXTLEN that
3837 * we rounded up to it, cut it back so it's valid again.
3838 * Note that if it's a really large request (bigger than
3839 * MAXEXTLEN), we don't hear about that number, and can't
3840 * adjust the starting point to match it.
3842 if (ralen
* mp
->m_sb
.sb_rextsize
>= MAXEXTLEN
)
3843 ralen
= MAXEXTLEN
/ mp
->m_sb
.sb_rextsize
;
3846 * Lock out other modifications to the RT bitmap inode.
3848 xfs_ilock(mp
->m_rbmip
, XFS_ILOCK_EXCL
);
3849 xfs_trans_ijoin(ap
->tp
, mp
->m_rbmip
, XFS_ILOCK_EXCL
);
3852 * If it's an allocation to an empty file at offset 0,
3853 * pick an extent that will space things out in the rt area.
3855 if (ap
->eof
&& ap
->offset
== 0) {
3856 xfs_rtblock_t
uninitialized_var(rtx
); /* realtime extent no */
3858 error
= xfs_rtpick_extent(mp
, ap
->tp
, ralen
, &rtx
);
3861 ap
->blkno
= rtx
* mp
->m_sb
.sb_rextsize
;
3866 xfs_bmap_adjacent(ap
);
3869 * Realtime allocation, done through xfs_rtallocate_extent.
3871 atype
= ap
->blkno
== 0 ? XFS_ALLOCTYPE_ANY_AG
: XFS_ALLOCTYPE_NEAR_BNO
;
3872 do_div(ap
->blkno
, mp
->m_sb
.sb_rextsize
);
3875 if ((error
= xfs_rtallocate_extent(ap
->tp
, ap
->blkno
, 1, ap
->length
,
3876 &ralen
, atype
, ap
->wasdel
, prod
, &rtb
)))
3878 if (rtb
== NULLFSBLOCK
&& prod
> 1 &&
3879 (error
= xfs_rtallocate_extent(ap
->tp
, ap
->blkno
, 1,
3880 ap
->length
, &ralen
, atype
,
3881 ap
->wasdel
, 1, &rtb
)))
3884 if (ap
->blkno
!= NULLFSBLOCK
) {
3885 ap
->blkno
*= mp
->m_sb
.sb_rextsize
;
3886 ralen
*= mp
->m_sb
.sb_rextsize
;
3888 ap
->ip
->i_d
.di_nblocks
+= ralen
;
3889 xfs_trans_log_inode(ap
->tp
, ap
->ip
, XFS_ILOG_CORE
);
3891 ap
->ip
->i_delayed_blks
-= ralen
;
3893 * Adjust the disk quota also. This was reserved
3896 xfs_trans_mod_dquot_byino(ap
->tp
, ap
->ip
,
3897 ap
->wasdel
? XFS_TRANS_DQ_DELRTBCOUNT
:
3898 XFS_TRANS_DQ_RTBCOUNT
, (long) ralen
);
3906 xfs_bmap_btalloc_nullfb(
3907 struct xfs_bmalloca
*ap
,
3908 struct xfs_alloc_arg
*args
,
3911 struct xfs_mount
*mp
= ap
->ip
->i_mount
;
3912 struct xfs_perag
*pag
;
3913 xfs_agnumber_t ag
, startag
;
3917 if (ap
->userdata
&& xfs_inode_is_filestream(ap
->ip
))
3918 args
->type
= XFS_ALLOCTYPE_NEAR_BNO
;
3920 args
->type
= XFS_ALLOCTYPE_START_BNO
;
3921 args
->total
= ap
->total
;
3924 * Search for an allocation group with a single extent large enough
3925 * for the request. If one isn't found, then adjust the minimum
3926 * allocation size to the largest space found.
3928 startag
= ag
= XFS_FSB_TO_AGNO(mp
, args
->fsbno
);
3929 if (startag
== NULLAGNUMBER
)
3932 pag
= xfs_perag_get(mp
, ag
);
3933 while (*blen
< args
->maxlen
) {
3934 if (!pag
->pagf_init
) {
3935 error
= xfs_alloc_pagf_init(mp
, args
->tp
, ag
,
3936 XFS_ALLOC_FLAG_TRYLOCK
);
3944 * See xfs_alloc_fix_freelist...
3946 if (pag
->pagf_init
) {
3947 xfs_extlen_t longest
;
3948 longest
= xfs_alloc_longest_free_extent(mp
, pag
);
3949 if (*blen
< longest
)
3954 if (xfs_inode_is_filestream(ap
->ip
)) {
3955 if (*blen
>= args
->maxlen
)
3960 * If startag is an invalid AG, we've
3961 * come here once before and
3962 * xfs_filestream_new_ag picked the
3963 * best currently available.
3965 * Don't continue looping, since we
3966 * could loop forever.
3968 if (startag
== NULLAGNUMBER
)
3971 error
= xfs_filestream_new_ag(ap
, &ag
);
3976 /* loop again to set 'blen'*/
3977 startag
= NULLAGNUMBER
;
3978 pag
= xfs_perag_get(mp
, ag
);
3982 if (++ag
== mp
->m_sb
.sb_agcount
)
3987 pag
= xfs_perag_get(mp
, ag
);
3992 * Since the above loop did a BUF_TRYLOCK, it is
3993 * possible that there is space for this request.
3995 if (notinit
|| *blen
< ap
->minlen
)
3996 args
->minlen
= ap
->minlen
;
3998 * If the best seen length is less than the request
3999 * length, use the best as the minimum.
4001 else if (*blen
< args
->maxlen
)
4002 args
->minlen
= *blen
;
4004 * Otherwise we've seen an extent as big as maxlen,
4005 * use that as the minimum.
4008 args
->minlen
= args
->maxlen
;
4011 * set the failure fallback case to look in the selected
4012 * AG as the stream may have moved.
4014 if (xfs_inode_is_filestream(ap
->ip
))
4015 ap
->blkno
= args
->fsbno
= XFS_AGB_TO_FSB(mp
, ag
, 0);
4022 xfs_bmalloca_t
*ap
) /* bmap alloc argument struct */
4024 xfs_mount_t
*mp
; /* mount point structure */
4025 xfs_alloctype_t atype
= 0; /* type for allocation routines */
4026 xfs_extlen_t align
; /* minimum allocation alignment */
4027 xfs_agnumber_t fb_agno
; /* ag number of ap->firstblock */
4029 xfs_alloc_arg_t args
;
4031 xfs_extlen_t nextminlen
= 0;
4032 int nullfb
; /* true if ap->firstblock isn't set */
4039 mp
= ap
->ip
->i_mount
;
4040 align
= ap
->userdata
? xfs_get_extsz_hint(ap
->ip
) : 0;
4041 if (unlikely(align
)) {
4042 error
= xfs_bmap_extsize_align(mp
, &ap
->got
, &ap
->prev
,
4043 align
, 0, ap
->eof
, 0, ap
->conv
,
4044 &ap
->offset
, &ap
->length
);
4048 nullfb
= *ap
->firstblock
== NULLFSBLOCK
;
4049 fb_agno
= nullfb
? NULLAGNUMBER
: XFS_FSB_TO_AGNO(mp
, *ap
->firstblock
);
4051 if (ap
->userdata
&& xfs_inode_is_filestream(ap
->ip
)) {
4052 ag
= xfs_filestream_lookup_ag(ap
->ip
);
4053 ag
= (ag
!= NULLAGNUMBER
) ? ag
: 0;
4054 ap
->blkno
= XFS_AGB_TO_FSB(mp
, ag
, 0);
4056 ap
->blkno
= XFS_INO_TO_FSB(mp
, ap
->ip
->i_ino
);
4059 ap
->blkno
= *ap
->firstblock
;
4061 xfs_bmap_adjacent(ap
);
4064 * If allowed, use ap->blkno; otherwise must use firstblock since
4065 * it's in the right allocation group.
4067 if (nullfb
|| XFS_FSB_TO_AGNO(mp
, ap
->blkno
) == fb_agno
)
4070 ap
->blkno
= *ap
->firstblock
;
4072 * Normal allocation, done through xfs_alloc_vextent.
4074 tryagain
= isaligned
= 0;
4075 memset(&args
, 0, sizeof(args
));
4078 args
.fsbno
= ap
->blkno
;
4080 /* Trim the allocation back to the maximum an AG can fit. */
4081 args
.maxlen
= MIN(ap
->length
, XFS_ALLOC_AG_MAX_USABLE(mp
));
4082 args
.firstblock
= *ap
->firstblock
;
4085 error
= xfs_bmap_btalloc_nullfb(ap
, &args
, &blen
);
4088 } else if (ap
->flist
->xbf_low
) {
4089 if (xfs_inode_is_filestream(ap
->ip
))
4090 args
.type
= XFS_ALLOCTYPE_FIRST_AG
;
4092 args
.type
= XFS_ALLOCTYPE_START_BNO
;
4093 args
.total
= args
.minlen
= ap
->minlen
;
4095 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
4096 args
.total
= ap
->total
;
4097 args
.minlen
= ap
->minlen
;
4099 /* apply extent size hints if obtained earlier */
4100 if (unlikely(align
)) {
4102 if ((args
.mod
= (xfs_extlen_t
)do_mod(ap
->offset
, args
.prod
)))
4103 args
.mod
= (xfs_extlen_t
)(args
.prod
- args
.mod
);
4104 } else if (mp
->m_sb
.sb_blocksize
>= PAGE_CACHE_SIZE
) {
4108 args
.prod
= PAGE_CACHE_SIZE
>> mp
->m_sb
.sb_blocklog
;
4109 if ((args
.mod
= (xfs_extlen_t
)(do_mod(ap
->offset
, args
.prod
))))
4110 args
.mod
= (xfs_extlen_t
)(args
.prod
- args
.mod
);
4113 * If we are not low on available data blocks, and the
4114 * underlying logical volume manager is a stripe, and
4115 * the file offset is zero then try to allocate data
4116 * blocks on stripe unit boundary.
4117 * NOTE: ap->aeof is only set if the allocation length
4118 * is >= the stripe unit and the allocation offset is
4119 * at the end of file.
4121 if (!ap
->flist
->xbf_low
&& ap
->aeof
) {
4123 args
.alignment
= mp
->m_dalign
;
4127 * Adjust for alignment
4129 if (blen
> args
.alignment
&& blen
<= args
.maxlen
)
4130 args
.minlen
= blen
- args
.alignment
;
4131 args
.minalignslop
= 0;
4134 * First try an exact bno allocation.
4135 * If it fails then do a near or start bno
4136 * allocation with alignment turned on.
4140 args
.type
= XFS_ALLOCTYPE_THIS_BNO
;
4143 * Compute the minlen+alignment for the
4144 * next case. Set slop so that the value
4145 * of minlen+alignment+slop doesn't go up
4146 * between the calls.
4148 if (blen
> mp
->m_dalign
&& blen
<= args
.maxlen
)
4149 nextminlen
= blen
- mp
->m_dalign
;
4151 nextminlen
= args
.minlen
;
4152 if (nextminlen
+ mp
->m_dalign
> args
.minlen
+ 1)
4154 nextminlen
+ mp
->m_dalign
-
4157 args
.minalignslop
= 0;
4161 args
.minalignslop
= 0;
4163 args
.minleft
= ap
->minleft
;
4164 args
.wasdel
= ap
->wasdel
;
4166 args
.userdata
= ap
->userdata
;
4167 if ((error
= xfs_alloc_vextent(&args
)))
4169 if (tryagain
&& args
.fsbno
== NULLFSBLOCK
) {
4171 * Exact allocation failed. Now try with alignment
4175 args
.fsbno
= ap
->blkno
;
4176 args
.alignment
= mp
->m_dalign
;
4177 args
.minlen
= nextminlen
;
4178 args
.minalignslop
= 0;
4180 if ((error
= xfs_alloc_vextent(&args
)))
4183 if (isaligned
&& args
.fsbno
== NULLFSBLOCK
) {
4185 * allocation failed, so turn off alignment and
4189 args
.fsbno
= ap
->blkno
;
4191 if ((error
= xfs_alloc_vextent(&args
)))
4194 if (args
.fsbno
== NULLFSBLOCK
&& nullfb
&&
4195 args
.minlen
> ap
->minlen
) {
4196 args
.minlen
= ap
->minlen
;
4197 args
.type
= XFS_ALLOCTYPE_START_BNO
;
4198 args
.fsbno
= ap
->blkno
;
4199 if ((error
= xfs_alloc_vextent(&args
)))
4202 if (args
.fsbno
== NULLFSBLOCK
&& nullfb
) {
4204 args
.type
= XFS_ALLOCTYPE_FIRST_AG
;
4205 args
.total
= ap
->minlen
;
4207 if ((error
= xfs_alloc_vextent(&args
)))
4209 ap
->flist
->xbf_low
= 1;
4211 if (args
.fsbno
!= NULLFSBLOCK
) {
4213 * check the allocation happened at the same or higher AG than
4214 * the first block that was allocated.
4216 ASSERT(*ap
->firstblock
== NULLFSBLOCK
||
4217 XFS_FSB_TO_AGNO(mp
, *ap
->firstblock
) ==
4218 XFS_FSB_TO_AGNO(mp
, args
.fsbno
) ||
4219 (ap
->flist
->xbf_low
&&
4220 XFS_FSB_TO_AGNO(mp
, *ap
->firstblock
) <
4221 XFS_FSB_TO_AGNO(mp
, args
.fsbno
)));
4223 ap
->blkno
= args
.fsbno
;
4224 if (*ap
->firstblock
== NULLFSBLOCK
)
4225 *ap
->firstblock
= args
.fsbno
;
4226 ASSERT(nullfb
|| fb_agno
== args
.agno
||
4227 (ap
->flist
->xbf_low
&& fb_agno
< args
.agno
));
4228 ap
->length
= args
.len
;
4229 ap
->ip
->i_d
.di_nblocks
+= args
.len
;
4230 xfs_trans_log_inode(ap
->tp
, ap
->ip
, XFS_ILOG_CORE
);
4232 ap
->ip
->i_delayed_blks
-= args
.len
;
4234 * Adjust the disk quota also. This was reserved
4237 xfs_trans_mod_dquot_byino(ap
->tp
, ap
->ip
,
4238 ap
->wasdel
? XFS_TRANS_DQ_DELBCOUNT
:
4239 XFS_TRANS_DQ_BCOUNT
,
4242 ap
->blkno
= NULLFSBLOCK
;
4249 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
4250 * It figures out where to ask the underlying allocator to put the new extent.
4254 xfs_bmalloca_t
*ap
) /* bmap alloc argument struct */
4256 if (XFS_IS_REALTIME_INODE(ap
->ip
) && ap
->userdata
)
4257 return xfs_bmap_rtalloc(ap
);
4258 return xfs_bmap_btalloc(ap
);
4262 * Trim the returned map to the required bounds
4266 struct xfs_bmbt_irec
*mval
,
4267 struct xfs_bmbt_irec
*got
,
4275 if ((flags
& XFS_BMAPI_ENTIRE
) ||
4276 got
->br_startoff
+ got
->br_blockcount
<= obno
) {
4278 if (isnullstartblock(got
->br_startblock
))
4279 mval
->br_startblock
= DELAYSTARTBLOCK
;
4285 ASSERT((*bno
>= obno
) || (n
== 0));
4287 mval
->br_startoff
= *bno
;
4288 if (isnullstartblock(got
->br_startblock
))
4289 mval
->br_startblock
= DELAYSTARTBLOCK
;
4291 mval
->br_startblock
= got
->br_startblock
+
4292 (*bno
- got
->br_startoff
);
4294 * Return the minimum of what we got and what we asked for for
4295 * the length. We can use the len variable here because it is
4296 * modified below and we could have been there before coming
4297 * here if the first part of the allocation didn't overlap what
4300 mval
->br_blockcount
= XFS_FILBLKS_MIN(end
- *bno
,
4301 got
->br_blockcount
- (*bno
- got
->br_startoff
));
4302 mval
->br_state
= got
->br_state
;
4303 ASSERT(mval
->br_blockcount
<= len
);
4308 * Update and validate the extent map to return
4311 xfs_bmapi_update_map(
4312 struct xfs_bmbt_irec
**map
,
4320 xfs_bmbt_irec_t
*mval
= *map
;
4322 ASSERT((flags
& XFS_BMAPI_ENTIRE
) ||
4323 ((mval
->br_startoff
+ mval
->br_blockcount
) <= end
));
4324 ASSERT((flags
& XFS_BMAPI_ENTIRE
) || (mval
->br_blockcount
<= *len
) ||
4325 (mval
->br_startoff
< obno
));
4327 *bno
= mval
->br_startoff
+ mval
->br_blockcount
;
4329 if (*n
> 0 && mval
->br_startoff
== mval
[-1].br_startoff
) {
4330 /* update previous map with new information */
4331 ASSERT(mval
->br_startblock
== mval
[-1].br_startblock
);
4332 ASSERT(mval
->br_blockcount
> mval
[-1].br_blockcount
);
4333 ASSERT(mval
->br_state
== mval
[-1].br_state
);
4334 mval
[-1].br_blockcount
= mval
->br_blockcount
;
4335 mval
[-1].br_state
= mval
->br_state
;
4336 } else if (*n
> 0 && mval
->br_startblock
!= DELAYSTARTBLOCK
&&
4337 mval
[-1].br_startblock
!= DELAYSTARTBLOCK
&&
4338 mval
[-1].br_startblock
!= HOLESTARTBLOCK
&&
4339 mval
->br_startblock
== mval
[-1].br_startblock
+
4340 mval
[-1].br_blockcount
&&
4341 ((flags
& XFS_BMAPI_IGSTATE
) ||
4342 mval
[-1].br_state
== mval
->br_state
)) {
4343 ASSERT(mval
->br_startoff
==
4344 mval
[-1].br_startoff
+ mval
[-1].br_blockcount
);
4345 mval
[-1].br_blockcount
+= mval
->br_blockcount
;
4346 } else if (*n
> 0 &&
4347 mval
->br_startblock
== DELAYSTARTBLOCK
&&
4348 mval
[-1].br_startblock
== DELAYSTARTBLOCK
&&
4349 mval
->br_startoff
==
4350 mval
[-1].br_startoff
+ mval
[-1].br_blockcount
) {
4351 mval
[-1].br_blockcount
+= mval
->br_blockcount
;
4352 mval
[-1].br_state
= mval
->br_state
;
4353 } else if (!((*n
== 0) &&
4354 ((mval
->br_startoff
+ mval
->br_blockcount
) <=
4363 * Map file blocks to filesystem blocks without allocation.
4367 struct xfs_inode
*ip
,
4370 struct xfs_bmbt_irec
*mval
,
4374 struct xfs_mount
*mp
= ip
->i_mount
;
4375 struct xfs_ifork
*ifp
;
4376 struct xfs_bmbt_irec got
;
4377 struct xfs_bmbt_irec prev
;
4384 int whichfork
= (flags
& XFS_BMAPI_ATTRFORK
) ?
4385 XFS_ATTR_FORK
: XFS_DATA_FORK
;
4388 ASSERT(!(flags
& ~(XFS_BMAPI_ATTRFORK
|XFS_BMAPI_ENTIRE
|
4389 XFS_BMAPI_IGSTATE
)));
4391 if (unlikely(XFS_TEST_ERROR(
4392 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
4393 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
4394 mp
, XFS_ERRTAG_BMAPIFORMAT
, XFS_RANDOM_BMAPIFORMAT
))) {
4395 XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW
, mp
);
4396 return XFS_ERROR(EFSCORRUPTED
);
4399 if (XFS_FORCED_SHUTDOWN(mp
))
4400 return XFS_ERROR(EIO
);
4402 XFS_STATS_INC(xs_blk_mapr
);
4404 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4406 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
4407 error
= xfs_iread_extents(NULL
, ip
, whichfork
);
4412 xfs_bmap_search_extents(ip
, bno
, whichfork
, &eof
, &lastx
, &got
, &prev
);
4416 while (bno
< end
&& n
< *nmap
) {
4417 /* Reading past eof, act as though there's a hole up to end. */
4419 got
.br_startoff
= end
;
4420 if (got
.br_startoff
> bno
) {
4421 /* Reading in a hole. */
4422 mval
->br_startoff
= bno
;
4423 mval
->br_startblock
= HOLESTARTBLOCK
;
4424 mval
->br_blockcount
=
4425 XFS_FILBLKS_MIN(len
, got
.br_startoff
- bno
);
4426 mval
->br_state
= XFS_EXT_NORM
;
4427 bno
+= mval
->br_blockcount
;
4428 len
-= mval
->br_blockcount
;
4434 /* set up the extent map to return. */
4435 xfs_bmapi_trim_map(mval
, &got
, &bno
, len
, obno
, end
, n
, flags
);
4436 xfs_bmapi_update_map(&mval
, &bno
, &len
, obno
, end
, &n
, flags
);
4438 /* If we're done, stop now. */
4439 if (bno
>= end
|| n
>= *nmap
)
4442 /* Else go on to the next record. */
4443 if (++lastx
< ifp
->if_bytes
/ sizeof(xfs_bmbt_rec_t
))
4444 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, lastx
), &got
);
4453 xfs_bmapi_reserve_delalloc(
4454 struct xfs_inode
*ip
,
4457 struct xfs_bmbt_irec
*got
,
4458 struct xfs_bmbt_irec
*prev
,
4459 xfs_extnum_t
*lastx
,
4462 struct xfs_mount
*mp
= ip
->i_mount
;
4463 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, XFS_DATA_FORK
);
4465 xfs_extlen_t indlen
;
4466 char rt
= XFS_IS_REALTIME_INODE(ip
);
4470 alen
= XFS_FILBLKS_MIN(len
, MAXEXTLEN
);
4472 alen
= XFS_FILBLKS_MIN(alen
, got
->br_startoff
- aoff
);
4474 /* Figure out the extent size, adjust alen */
4475 extsz
= xfs_get_extsz_hint(ip
);
4478 * Make sure we don't exceed a single extent length when we
4479 * align the extent by reducing length we are going to
4480 * allocate by the maximum amount extent size aligment may
4483 alen
= XFS_FILBLKS_MIN(len
, MAXEXTLEN
- (2 * extsz
- 1));
4484 error
= xfs_bmap_extsize_align(mp
, got
, prev
, extsz
, rt
, eof
,
4485 1, 0, &aoff
, &alen
);
4490 extsz
= alen
/ mp
->m_sb
.sb_rextsize
;
4493 * Make a transaction-less quota reservation for delayed allocation
4494 * blocks. This number gets adjusted later. We return if we haven't
4495 * allocated blocks already inside this loop.
4497 error
= xfs_trans_reserve_quota_nblks(NULL
, ip
, (long)alen
, 0,
4498 rt
? XFS_QMOPT_RES_RTBLKS
: XFS_QMOPT_RES_REGBLKS
);
4503 * Split changing sb for alen and indlen since they could be coming
4504 * from different places.
4506 indlen
= (xfs_extlen_t
)xfs_bmap_worst_indlen(ip
, alen
);
4510 error
= xfs_mod_incore_sb(mp
, XFS_SBS_FREXTENTS
,
4511 -((int64_t)extsz
), 0);
4513 error
= xfs_icsb_modify_counters(mp
, XFS_SBS_FDBLOCKS
,
4514 -((int64_t)alen
), 0);
4518 goto out_unreserve_quota
;
4520 error
= xfs_icsb_modify_counters(mp
, XFS_SBS_FDBLOCKS
,
4521 -((int64_t)indlen
), 0);
4523 goto out_unreserve_blocks
;
4526 ip
->i_delayed_blks
+= alen
;
4528 got
->br_startoff
= aoff
;
4529 got
->br_startblock
= nullstartblock(indlen
);
4530 got
->br_blockcount
= alen
;
4531 got
->br_state
= XFS_EXT_NORM
;
4532 xfs_bmap_add_extent_hole_delay(ip
, lastx
, got
);
4535 * Update our extent pointer, given that xfs_bmap_add_extent_hole_delay
4536 * might have merged it into one of the neighbouring ones.
4538 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, *lastx
), got
);
4540 ASSERT(got
->br_startoff
<= aoff
);
4541 ASSERT(got
->br_startoff
+ got
->br_blockcount
>= aoff
+ alen
);
4542 ASSERT(isnullstartblock(got
->br_startblock
));
4543 ASSERT(got
->br_state
== XFS_EXT_NORM
);
4546 out_unreserve_blocks
:
4548 xfs_mod_incore_sb(mp
, XFS_SBS_FREXTENTS
, extsz
, 0);
4550 xfs_icsb_modify_counters(mp
, XFS_SBS_FDBLOCKS
, alen
, 0);
4551 out_unreserve_quota
:
4552 if (XFS_IS_QUOTA_ON(mp
))
4553 xfs_trans_unreserve_quota_nblks(NULL
, ip
, (long)alen
, 0, rt
?
4554 XFS_QMOPT_RES_RTBLKS
: XFS_QMOPT_RES_REGBLKS
);
4559 * Map file blocks to filesystem blocks, adding delayed allocations as needed.
4563 struct xfs_inode
*ip
, /* incore inode */
4564 xfs_fileoff_t bno
, /* starting file offs. mapped */
4565 xfs_filblks_t len
, /* length to map in file */
4566 struct xfs_bmbt_irec
*mval
, /* output: map values */
4567 int *nmap
, /* i/o: mval size/count */
4568 int flags
) /* XFS_BMAPI_... */
4570 struct xfs_mount
*mp
= ip
->i_mount
;
4571 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, XFS_DATA_FORK
);
4572 struct xfs_bmbt_irec got
; /* current file extent record */
4573 struct xfs_bmbt_irec prev
; /* previous file extent record */
4574 xfs_fileoff_t obno
; /* old block number (offset) */
4575 xfs_fileoff_t end
; /* end of mapped file region */
4576 xfs_extnum_t lastx
; /* last useful extent number */
4577 int eof
; /* we've hit the end of extents */
4578 int n
= 0; /* current extent index */
4582 ASSERT(*nmap
<= XFS_BMAP_MAX_NMAP
);
4583 ASSERT(!(flags
& ~XFS_BMAPI_ENTIRE
));
4585 if (unlikely(XFS_TEST_ERROR(
4586 (XFS_IFORK_FORMAT(ip
, XFS_DATA_FORK
) != XFS_DINODE_FMT_EXTENTS
&&
4587 XFS_IFORK_FORMAT(ip
, XFS_DATA_FORK
) != XFS_DINODE_FMT_BTREE
),
4588 mp
, XFS_ERRTAG_BMAPIFORMAT
, XFS_RANDOM_BMAPIFORMAT
))) {
4589 XFS_ERROR_REPORT("xfs_bmapi_delay", XFS_ERRLEVEL_LOW
, mp
);
4590 return XFS_ERROR(EFSCORRUPTED
);
4593 if (XFS_FORCED_SHUTDOWN(mp
))
4594 return XFS_ERROR(EIO
);
4596 XFS_STATS_INC(xs_blk_mapw
);
4598 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
4599 error
= xfs_iread_extents(NULL
, ip
, XFS_DATA_FORK
);
4604 xfs_bmap_search_extents(ip
, bno
, XFS_DATA_FORK
, &eof
, &lastx
, &got
, &prev
);
4608 while (bno
< end
&& n
< *nmap
) {
4609 if (eof
|| got
.br_startoff
> bno
) {
4610 error
= xfs_bmapi_reserve_delalloc(ip
, bno
, len
, &got
,
4611 &prev
, &lastx
, eof
);
4621 /* set up the extent map to return. */
4622 xfs_bmapi_trim_map(mval
, &got
, &bno
, len
, obno
, end
, n
, flags
);
4623 xfs_bmapi_update_map(&mval
, &bno
, &len
, obno
, end
, &n
, flags
);
4625 /* If we're done, stop now. */
4626 if (bno
>= end
|| n
>= *nmap
)
4629 /* Else go on to the next record. */
4631 if (++lastx
< ifp
->if_bytes
/ sizeof(xfs_bmbt_rec_t
))
4632 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, lastx
), &got
);
4643 __xfs_bmapi_allocate(
4644 struct xfs_bmalloca
*bma
)
4646 struct xfs_mount
*mp
= bma
->ip
->i_mount
;
4647 int whichfork
= (bma
->flags
& XFS_BMAPI_ATTRFORK
) ?
4648 XFS_ATTR_FORK
: XFS_DATA_FORK
;
4649 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
4650 int tmp_logflags
= 0;
4654 ASSERT(bma
->length
> 0);
4656 rt
= (whichfork
== XFS_DATA_FORK
) && XFS_IS_REALTIME_INODE(bma
->ip
);
4659 * For the wasdelay case, we could also just allocate the stuff asked
4660 * for in this bmap call but that wouldn't be as good.
4663 bma
->length
= (xfs_extlen_t
)bma
->got
.br_blockcount
;
4664 bma
->offset
= bma
->got
.br_startoff
;
4665 if (bma
->idx
!= NULLEXTNUM
&& bma
->idx
) {
4666 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
- 1),
4670 bma
->length
= XFS_FILBLKS_MIN(bma
->length
, MAXEXTLEN
);
4672 bma
->length
= XFS_FILBLKS_MIN(bma
->length
,
4673 bma
->got
.br_startoff
- bma
->offset
);
4677 * Indicate if this is the first user data in the file, or just any
4680 if (!(bma
->flags
& XFS_BMAPI_METADATA
)) {
4681 bma
->userdata
= (bma
->offset
== 0) ?
4682 XFS_ALLOC_INITIAL_USER_DATA
: XFS_ALLOC_USERDATA
;
4685 bma
->minlen
= (bma
->flags
& XFS_BMAPI_CONTIG
) ? bma
->length
: 1;
4688 * Only want to do the alignment at the eof if it is userdata and
4689 * allocation length is larger than a stripe unit.
4691 if (mp
->m_dalign
&& bma
->length
>= mp
->m_dalign
&&
4692 !(bma
->flags
& XFS_BMAPI_METADATA
) && whichfork
== XFS_DATA_FORK
) {
4693 error
= xfs_bmap_isaeof(bma
, whichfork
);
4698 error
= xfs_bmap_alloc(bma
);
4702 if (bma
->flist
->xbf_low
)
4705 bma
->cur
->bc_private
.b
.firstblock
= *bma
->firstblock
;
4706 if (bma
->blkno
== NULLFSBLOCK
)
4708 if ((ifp
->if_flags
& XFS_IFBROOT
) && !bma
->cur
) {
4709 bma
->cur
= xfs_bmbt_init_cursor(mp
, bma
->tp
, bma
->ip
, whichfork
);
4710 bma
->cur
->bc_private
.b
.firstblock
= *bma
->firstblock
;
4711 bma
->cur
->bc_private
.b
.flist
= bma
->flist
;
4714 * Bump the number of extents we've allocated
4720 bma
->cur
->bc_private
.b
.flags
=
4721 bma
->wasdel
? XFS_BTCUR_BPRV_WASDEL
: 0;
4723 bma
->got
.br_startoff
= bma
->offset
;
4724 bma
->got
.br_startblock
= bma
->blkno
;
4725 bma
->got
.br_blockcount
= bma
->length
;
4726 bma
->got
.br_state
= XFS_EXT_NORM
;
4729 * A wasdelay extent has been initialized, so shouldn't be flagged
4732 if (!bma
->wasdel
&& (bma
->flags
& XFS_BMAPI_PREALLOC
) &&
4733 xfs_sb_version_hasextflgbit(&mp
->m_sb
))
4734 bma
->got
.br_state
= XFS_EXT_UNWRITTEN
;
4737 error
= xfs_bmap_add_extent_delay_real(bma
);
4739 error
= xfs_bmap_add_extent_hole_real(bma
, whichfork
);
4741 bma
->logflags
|= tmp_logflags
;
4746 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4747 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4748 * the neighbouring ones.
4750 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
), &bma
->got
);
4752 ASSERT(bma
->got
.br_startoff
<= bma
->offset
);
4753 ASSERT(bma
->got
.br_startoff
+ bma
->got
.br_blockcount
>=
4754 bma
->offset
+ bma
->length
);
4755 ASSERT(bma
->got
.br_state
== XFS_EXT_NORM
||
4756 bma
->got
.br_state
== XFS_EXT_UNWRITTEN
);
4761 xfs_bmapi_allocate_worker(
4762 struct work_struct
*work
)
4764 struct xfs_bmalloca
*args
= container_of(work
,
4765 struct xfs_bmalloca
, work
);
4766 unsigned long pflags
;
4768 /* we are in a transaction context here */
4769 current_set_flags_nested(&pflags
, PF_FSTRANS
);
4771 args
->result
= __xfs_bmapi_allocate(args
);
4772 complete(args
->done
);
4774 current_restore_flags_nested(&pflags
, PF_FSTRANS
);
4778 * Some allocation requests often come in with little stack to work on. Push
4779 * them off to a worker thread so there is lots of stack to use. Otherwise just
4780 * call directly to avoid the context switch overhead here.
4784 struct xfs_bmalloca
*args
)
4786 DECLARE_COMPLETION_ONSTACK(done
);
4788 if (!args
->stack_switch
)
4789 return __xfs_bmapi_allocate(args
);
4793 INIT_WORK_ONSTACK(&args
->work
, xfs_bmapi_allocate_worker
);
4794 queue_work(xfs_alloc_wq
, &args
->work
);
4795 wait_for_completion(&done
);
4796 return args
->result
;
4800 xfs_bmapi_convert_unwritten(
4801 struct xfs_bmalloca
*bma
,
4802 struct xfs_bmbt_irec
*mval
,
4806 int whichfork
= (flags
& XFS_BMAPI_ATTRFORK
) ?
4807 XFS_ATTR_FORK
: XFS_DATA_FORK
;
4808 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
4809 int tmp_logflags
= 0;
4812 /* check if we need to do unwritten->real conversion */
4813 if (mval
->br_state
== XFS_EXT_UNWRITTEN
&&
4814 (flags
& XFS_BMAPI_PREALLOC
))
4817 /* check if we need to do real->unwritten conversion */
4818 if (mval
->br_state
== XFS_EXT_NORM
&&
4819 (flags
& (XFS_BMAPI_PREALLOC
| XFS_BMAPI_CONVERT
)) !=
4820 (XFS_BMAPI_PREALLOC
| XFS_BMAPI_CONVERT
))
4824 * Modify (by adding) the state flag, if writing.
4826 ASSERT(mval
->br_blockcount
<= len
);
4827 if ((ifp
->if_flags
& XFS_IFBROOT
) && !bma
->cur
) {
4828 bma
->cur
= xfs_bmbt_init_cursor(bma
->ip
->i_mount
, bma
->tp
,
4829 bma
->ip
, whichfork
);
4830 bma
->cur
->bc_private
.b
.firstblock
= *bma
->firstblock
;
4831 bma
->cur
->bc_private
.b
.flist
= bma
->flist
;
4833 mval
->br_state
= (mval
->br_state
== XFS_EXT_UNWRITTEN
)
4834 ? XFS_EXT_NORM
: XFS_EXT_UNWRITTEN
;
4836 error
= xfs_bmap_add_extent_unwritten_real(bma
->tp
, bma
->ip
, &bma
->idx
,
4837 &bma
->cur
, mval
, bma
->firstblock
, bma
->flist
,
4839 bma
->logflags
|= tmp_logflags
;
4844 * Update our extent pointer, given that
4845 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4846 * of the neighbouring ones.
4848 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
), &bma
->got
);
4851 * We may have combined previously unwritten space with written space,
4852 * so generate another request.
4854 if (mval
->br_blockcount
< len
)
4860 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4861 * extent state if necessary. Details behaviour is controlled by the flags
4862 * parameter. Only allocates blocks from a single allocation group, to avoid
4865 * The returned value in "firstblock" from the first call in a transaction
4866 * must be remembered and presented to subsequent calls in "firstblock".
4867 * An upper bound for the number of blocks to be allocated is supplied to
4868 * the first call in "total"; if no allocation group has that many free
4869 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4873 struct xfs_trans
*tp
, /* transaction pointer */
4874 struct xfs_inode
*ip
, /* incore inode */
4875 xfs_fileoff_t bno
, /* starting file offs. mapped */
4876 xfs_filblks_t len
, /* length to map in file */
4877 int flags
, /* XFS_BMAPI_... */
4878 xfs_fsblock_t
*firstblock
, /* first allocated block
4879 controls a.g. for allocs */
4880 xfs_extlen_t total
, /* total blocks needed */
4881 struct xfs_bmbt_irec
*mval
, /* output: map values */
4882 int *nmap
, /* i/o: mval size/count */
4883 struct xfs_bmap_free
*flist
) /* i/o: list extents to free */
4885 struct xfs_mount
*mp
= ip
->i_mount
;
4886 struct xfs_ifork
*ifp
;
4887 struct xfs_bmalloca bma
= { 0 }; /* args for xfs_bmap_alloc */
4888 xfs_fileoff_t end
; /* end of mapped file region */
4889 int eof
; /* after the end of extents */
4890 int error
; /* error return */
4891 int n
; /* current extent index */
4892 xfs_fileoff_t obno
; /* old block number (offset) */
4893 int whichfork
; /* data or attr fork */
4894 char inhole
; /* current location is hole in file */
4895 char wasdelay
; /* old extent was delayed */
4898 xfs_fileoff_t orig_bno
; /* original block number value */
4899 int orig_flags
; /* original flags arg value */
4900 xfs_filblks_t orig_len
; /* original value of len arg */
4901 struct xfs_bmbt_irec
*orig_mval
; /* original value of mval */
4902 int orig_nmap
; /* original value of *nmap */
4912 ASSERT(*nmap
<= XFS_BMAP_MAX_NMAP
);
4913 ASSERT(!(flags
& XFS_BMAPI_IGSTATE
));
4917 whichfork
= (flags
& XFS_BMAPI_ATTRFORK
) ?
4918 XFS_ATTR_FORK
: XFS_DATA_FORK
;
4920 if (unlikely(XFS_TEST_ERROR(
4921 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
4922 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
&&
4923 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_LOCAL
),
4924 mp
, XFS_ERRTAG_BMAPIFORMAT
, XFS_RANDOM_BMAPIFORMAT
))) {
4925 XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW
, mp
);
4926 return XFS_ERROR(EFSCORRUPTED
);
4929 if (XFS_FORCED_SHUTDOWN(mp
))
4930 return XFS_ERROR(EIO
);
4932 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4934 XFS_STATS_INC(xs_blk_mapw
);
4936 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
) {
4938 * XXX (dgc): This assumes we are only called for inodes that
4939 * contain content neutral data in local format. Anything that
4940 * contains caller-specific data in local format that needs
4941 * transformation to move to a block format needs to do the
4942 * conversion to extent format itself.
4944 * Directory data forks and attribute forks handle this
4945 * themselves, but with the addition of metadata verifiers every
4946 * data fork in local format now contains caller specific data
4947 * and as such conversion through this function is likely to be
4950 * The only likely user of this branch is for remote symlinks,
4951 * but we cannot overwrite the data fork contents of the symlink
4952 * (EEXIST occurs higher up the stack) and so it will never go
4953 * from local format to extent format here. Hence I don't think
4954 * this branch is ever executed intentionally and we should
4955 * consider removing it and asserting that xfs_bmapi_write()
4956 * cannot be called directly on local format forks. i.e. callers
4957 * are completely responsible for local to extent format
4958 * conversion, not xfs_bmapi_write().
4960 error
= xfs_bmap_local_to_extents(tp
, ip
, firstblock
, total
,
4961 &bma
.logflags
, whichfork
,
4962 xfs_bmap_local_to_extents_init_fn
);
4967 if (*firstblock
== NULLFSBLOCK
) {
4968 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
)
4969 bma
.minleft
= be16_to_cpu(ifp
->if_broot
->bb_level
) + 1;
4976 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
4977 error
= xfs_iread_extents(tp
, ip
, whichfork
);
4982 xfs_bmap_search_extents(ip
, bno
, whichfork
, &eof
, &bma
.idx
, &bma
.got
,
4993 bma
.firstblock
= firstblock
;
4995 if (flags
& XFS_BMAPI_STACK_SWITCH
)
4996 bma
.stack_switch
= 1;
4998 while (bno
< end
&& n
< *nmap
) {
4999 inhole
= eof
|| bma
.got
.br_startoff
> bno
;
5000 wasdelay
= !inhole
&& isnullstartblock(bma
.got
.br_startblock
);
5003 * First, deal with the hole before the allocated space
5004 * that we found, if any.
5006 if (inhole
|| wasdelay
) {
5008 bma
.conv
= !!(flags
& XFS_BMAPI_CONVERT
);
5009 bma
.wasdel
= wasdelay
;
5014 * There's a 32/64 bit type mismatch between the
5015 * allocation length request (which can be 64 bits in
5016 * length) and the bma length request, which is
5017 * xfs_extlen_t and therefore 32 bits. Hence we have to
5018 * check for 32-bit overflows and handle them here.
5020 if (len
> (xfs_filblks_t
)MAXEXTLEN
)
5021 bma
.length
= MAXEXTLEN
;
5026 ASSERT(bma
.length
> 0);
5027 error
= xfs_bmapi_allocate(&bma
);
5030 if (bma
.blkno
== NULLFSBLOCK
)
5034 /* Deal with the allocated space we found. */
5035 xfs_bmapi_trim_map(mval
, &bma
.got
, &bno
, len
, obno
,
5038 /* Execute unwritten extent conversion if necessary */
5039 error
= xfs_bmapi_convert_unwritten(&bma
, mval
, len
, flags
);
5040 if (error
== EAGAIN
)
5045 /* update the extent map to return */
5046 xfs_bmapi_update_map(&mval
, &bno
, &len
, obno
, end
, &n
, flags
);
5049 * If we're done, stop now. Stop when we've allocated
5050 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
5051 * the transaction may get too big.
5053 if (bno
>= end
|| n
>= *nmap
|| bma
.nallocs
>= *nmap
)
5056 /* Else go on to the next record. */
5058 if (++bma
.idx
< ifp
->if_bytes
/ sizeof(xfs_bmbt_rec_t
)) {
5059 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
.idx
),
5067 * Transform from btree to extents, give it cur.
5069 if (xfs_bmap_wants_extents(ip
, whichfork
)) {
5070 int tmp_logflags
= 0;
5073 error
= xfs_bmap_btree_to_extents(tp
, ip
, bma
.cur
,
5074 &tmp_logflags
, whichfork
);
5075 bma
.logflags
|= tmp_logflags
;
5080 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
||
5081 XFS_IFORK_NEXTENTS(ip
, whichfork
) >
5082 XFS_IFORK_MAXEXT(ip
, whichfork
));
5086 * Log everything. Do this after conversion, there's no point in
5087 * logging the extent records if we've converted to btree format.
5089 if ((bma
.logflags
& xfs_ilog_fext(whichfork
)) &&
5090 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
5091 bma
.logflags
&= ~xfs_ilog_fext(whichfork
);
5092 else if ((bma
.logflags
& xfs_ilog_fbroot(whichfork
)) &&
5093 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)
5094 bma
.logflags
&= ~xfs_ilog_fbroot(whichfork
);
5096 * Log whatever the flags say, even if error. Otherwise we might miss
5097 * detecting a case where the data is changed, there's an error,
5098 * and it's not logged so we don't shutdown when we should.
5101 xfs_trans_log_inode(tp
, ip
, bma
.logflags
);
5105 ASSERT(*firstblock
== NULLFSBLOCK
||
5106 XFS_FSB_TO_AGNO(mp
, *firstblock
) ==
5108 bma
.cur
->bc_private
.b
.firstblock
) ||
5110 XFS_FSB_TO_AGNO(mp
, *firstblock
) <
5112 bma
.cur
->bc_private
.b
.firstblock
)));
5113 *firstblock
= bma
.cur
->bc_private
.b
.firstblock
;
5115 xfs_btree_del_cursor(bma
.cur
,
5116 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
5119 xfs_bmap_validate_ret(orig_bno
, orig_len
, orig_flags
, orig_mval
,
5125 * Called by xfs_bmapi to update file extent records and the btree
5126 * after removing space (or undoing a delayed allocation).
5128 STATIC
int /* error */
5129 xfs_bmap_del_extent(
5130 xfs_inode_t
*ip
, /* incore inode pointer */
5131 xfs_trans_t
*tp
, /* current transaction pointer */
5132 xfs_extnum_t
*idx
, /* extent number to update/delete */
5133 xfs_bmap_free_t
*flist
, /* list of extents to be freed */
5134 xfs_btree_cur_t
*cur
, /* if null, not a btree */
5135 xfs_bmbt_irec_t
*del
, /* data to remove from extents */
5136 int *logflagsp
, /* inode logging flags */
5137 int whichfork
) /* data or attr fork */
5139 xfs_filblks_t da_new
; /* new delay-alloc indirect blocks */
5140 xfs_filblks_t da_old
; /* old delay-alloc indirect blocks */
5141 xfs_fsblock_t del_endblock
=0; /* first block past del */
5142 xfs_fileoff_t del_endoff
; /* first offset past del */
5143 int delay
; /* current block is delayed allocated */
5144 int do_fx
; /* free extent at end of routine */
5145 xfs_bmbt_rec_host_t
*ep
; /* current extent entry pointer */
5146 int error
; /* error return value */
5147 int flags
; /* inode logging flags */
5148 xfs_bmbt_irec_t got
; /* current extent entry */
5149 xfs_fileoff_t got_endoff
; /* first offset past got */
5150 int i
; /* temp state */
5151 xfs_ifork_t
*ifp
; /* inode fork pointer */
5152 xfs_mount_t
*mp
; /* mount structure */
5153 xfs_filblks_t nblks
; /* quota/sb block count */
5154 xfs_bmbt_irec_t
new; /* new record to be inserted */
5156 uint qfield
; /* quota field to update */
5157 xfs_filblks_t temp
; /* for indirect length calculations */
5158 xfs_filblks_t temp2
; /* for indirect length calculations */
5161 XFS_STATS_INC(xs_del_exlist
);
5163 if (whichfork
== XFS_ATTR_FORK
)
5164 state
|= BMAP_ATTRFORK
;
5167 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5168 ASSERT((*idx
>= 0) && (*idx
< ifp
->if_bytes
/
5169 (uint
)sizeof(xfs_bmbt_rec_t
)));
5170 ASSERT(del
->br_blockcount
> 0);
5171 ep
= xfs_iext_get_ext(ifp
, *idx
);
5172 xfs_bmbt_get_all(ep
, &got
);
5173 ASSERT(got
.br_startoff
<= del
->br_startoff
);
5174 del_endoff
= del
->br_startoff
+ del
->br_blockcount
;
5175 got_endoff
= got
.br_startoff
+ got
.br_blockcount
;
5176 ASSERT(got_endoff
>= del_endoff
);
5177 delay
= isnullstartblock(got
.br_startblock
);
5178 ASSERT(isnullstartblock(del
->br_startblock
) == delay
);
5183 * If deleting a real allocation, must free up the disk space.
5186 flags
= XFS_ILOG_CORE
;
5188 * Realtime allocation. Free it and record di_nblocks update.
5190 if (whichfork
== XFS_DATA_FORK
&& XFS_IS_REALTIME_INODE(ip
)) {
5194 ASSERT(do_mod(del
->br_blockcount
,
5195 mp
->m_sb
.sb_rextsize
) == 0);
5196 ASSERT(do_mod(del
->br_startblock
,
5197 mp
->m_sb
.sb_rextsize
) == 0);
5198 bno
= del
->br_startblock
;
5199 len
= del
->br_blockcount
;
5200 do_div(bno
, mp
->m_sb
.sb_rextsize
);
5201 do_div(len
, mp
->m_sb
.sb_rextsize
);
5202 error
= xfs_rtfree_extent(tp
, bno
, (xfs_extlen_t
)len
);
5206 nblks
= len
* mp
->m_sb
.sb_rextsize
;
5207 qfield
= XFS_TRANS_DQ_RTBCOUNT
;
5210 * Ordinary allocation.
5214 nblks
= del
->br_blockcount
;
5215 qfield
= XFS_TRANS_DQ_BCOUNT
;
5218 * Set up del_endblock and cur for later.
5220 del_endblock
= del
->br_startblock
+ del
->br_blockcount
;
5222 if ((error
= xfs_bmbt_lookup_eq(cur
, got
.br_startoff
,
5223 got
.br_startblock
, got
.br_blockcount
,
5226 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
5228 da_old
= da_new
= 0;
5230 da_old
= startblockval(got
.br_startblock
);
5236 * Set flag value to use in switch statement.
5237 * Left-contig is 2, right-contig is 1.
5239 switch (((got
.br_startoff
== del
->br_startoff
) << 1) |
5240 (got_endoff
== del_endoff
)) {
5243 * Matches the whole extent. Delete the entry.
5245 xfs_iext_remove(ip
, *idx
, 1,
5246 whichfork
== XFS_ATTR_FORK
? BMAP_ATTRFORK
: 0);
5251 XFS_IFORK_NEXT_SET(ip
, whichfork
,
5252 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
5253 flags
|= XFS_ILOG_CORE
;
5255 flags
|= xfs_ilog_fext(whichfork
);
5258 if ((error
= xfs_btree_delete(cur
, &i
)))
5260 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
5265 * Deleting the first part of the extent.
5267 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
5268 xfs_bmbt_set_startoff(ep
, del_endoff
);
5269 temp
= got
.br_blockcount
- del
->br_blockcount
;
5270 xfs_bmbt_set_blockcount(ep
, temp
);
5272 temp
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
5274 xfs_bmbt_set_startblock(ep
, nullstartblock((int)temp
));
5275 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
5279 xfs_bmbt_set_startblock(ep
, del_endblock
);
5280 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
5282 flags
|= xfs_ilog_fext(whichfork
);
5285 if ((error
= xfs_bmbt_update(cur
, del_endoff
, del_endblock
,
5286 got
.br_blockcount
- del
->br_blockcount
,
5293 * Deleting the last part of the extent.
5295 temp
= got
.br_blockcount
- del
->br_blockcount
;
5296 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
5297 xfs_bmbt_set_blockcount(ep
, temp
);
5299 temp
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
5301 xfs_bmbt_set_startblock(ep
, nullstartblock((int)temp
));
5302 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
5306 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
5308 flags
|= xfs_ilog_fext(whichfork
);
5311 if ((error
= xfs_bmbt_update(cur
, got
.br_startoff
,
5313 got
.br_blockcount
- del
->br_blockcount
,
5320 * Deleting the middle of the extent.
5322 temp
= del
->br_startoff
- got
.br_startoff
;
5323 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
5324 xfs_bmbt_set_blockcount(ep
, temp
);
5325 new.br_startoff
= del_endoff
;
5326 temp2
= got_endoff
- del_endoff
;
5327 new.br_blockcount
= temp2
;
5328 new.br_state
= got
.br_state
;
5330 new.br_startblock
= del_endblock
;
5331 flags
|= XFS_ILOG_CORE
;
5333 if ((error
= xfs_bmbt_update(cur
,
5335 got
.br_startblock
, temp
,
5338 if ((error
= xfs_btree_increment(cur
, 0, &i
)))
5340 cur
->bc_rec
.b
= new;
5341 error
= xfs_btree_insert(cur
, &i
);
5342 if (error
&& error
!= ENOSPC
)
5345 * If get no-space back from btree insert,
5346 * it tried a split, and we have a zero
5347 * block reservation.
5348 * Fix up our state and return the error.
5350 if (error
== ENOSPC
) {
5352 * Reset the cursor, don't trust
5353 * it after any insert operation.
5355 if ((error
= xfs_bmbt_lookup_eq(cur
,
5360 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
5362 * Update the btree record back
5363 * to the original value.
5365 if ((error
= xfs_bmbt_update(cur
,
5372 * Reset the extent record back
5373 * to the original value.
5375 xfs_bmbt_set_blockcount(ep
,
5378 error
= XFS_ERROR(ENOSPC
);
5381 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
5383 flags
|= xfs_ilog_fext(whichfork
);
5384 XFS_IFORK_NEXT_SET(ip
, whichfork
,
5385 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
5387 ASSERT(whichfork
== XFS_DATA_FORK
);
5388 temp
= xfs_bmap_worst_indlen(ip
, temp
);
5389 xfs_bmbt_set_startblock(ep
, nullstartblock((int)temp
));
5390 temp2
= xfs_bmap_worst_indlen(ip
, temp2
);
5391 new.br_startblock
= nullstartblock((int)temp2
);
5392 da_new
= temp
+ temp2
;
5393 while (da_new
> da_old
) {
5397 xfs_bmbt_set_startblock(ep
,
5398 nullstartblock((int)temp
));
5400 if (da_new
== da_old
)
5406 nullstartblock((int)temp2
);
5410 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
5411 xfs_iext_insert(ip
, *idx
+ 1, 1, &new, state
);
5416 * If we need to, add to list of extents to delete.
5419 xfs_bmap_add_free(del
->br_startblock
, del
->br_blockcount
, flist
,
5422 * Adjust inode # blocks in the file.
5425 ip
->i_d
.di_nblocks
-= nblks
;
5427 * Adjust quota data.
5430 xfs_trans_mod_dquot_byino(tp
, ip
, qfield
, (long)-nblks
);
5433 * Account for change in delayed indirect blocks.
5434 * Nothing to do for disk quota accounting here.
5436 ASSERT(da_old
>= da_new
);
5437 if (da_old
> da_new
) {
5438 xfs_icsb_modify_counters(mp
, XFS_SBS_FDBLOCKS
,
5439 (int64_t)(da_old
- da_new
), 0);
5447 * Unmap (remove) blocks from a file.
5448 * If nexts is nonzero then the number of extents to remove is limited to
5449 * that value. If not all extents in the block range can be removed then
5454 xfs_trans_t
*tp
, /* transaction pointer */
5455 struct xfs_inode
*ip
, /* incore inode */
5456 xfs_fileoff_t bno
, /* starting offset to unmap */
5457 xfs_filblks_t len
, /* length to unmap in file */
5458 int flags
, /* misc flags */
5459 xfs_extnum_t nexts
, /* number of extents max */
5460 xfs_fsblock_t
*firstblock
, /* first allocated block
5461 controls a.g. for allocs */
5462 xfs_bmap_free_t
*flist
, /* i/o: list extents to free */
5463 int *done
) /* set if not done yet */
5465 xfs_btree_cur_t
*cur
; /* bmap btree cursor */
5466 xfs_bmbt_irec_t del
; /* extent being deleted */
5467 int eof
; /* is deleting at eof */
5468 xfs_bmbt_rec_host_t
*ep
; /* extent record pointer */
5469 int error
; /* error return value */
5470 xfs_extnum_t extno
; /* extent number in list */
5471 xfs_bmbt_irec_t got
; /* current extent record */
5472 xfs_ifork_t
*ifp
; /* inode fork pointer */
5473 int isrt
; /* freeing in rt area */
5474 xfs_extnum_t lastx
; /* last extent index used */
5475 int logflags
; /* transaction logging flags */
5476 xfs_extlen_t mod
; /* rt extent offset */
5477 xfs_mount_t
*mp
; /* mount structure */
5478 xfs_extnum_t nextents
; /* number of file extents */
5479 xfs_bmbt_irec_t prev
; /* previous extent record */
5480 xfs_fileoff_t start
; /* first file offset deleted */
5481 int tmp_logflags
; /* partial logging flags */
5482 int wasdel
; /* was a delayed alloc extent */
5483 int whichfork
; /* data or attribute fork */
5486 trace_xfs_bunmap(ip
, bno
, len
, flags
, _RET_IP_
);
5488 whichfork
= (flags
& XFS_BMAPI_ATTRFORK
) ?
5489 XFS_ATTR_FORK
: XFS_DATA_FORK
;
5490 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5492 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
5493 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)) {
5494 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW
,
5496 return XFS_ERROR(EFSCORRUPTED
);
5499 if (XFS_FORCED_SHUTDOWN(mp
))
5500 return XFS_ERROR(EIO
);
5505 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
5506 (error
= xfs_iread_extents(tp
, ip
, whichfork
)))
5508 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
5509 if (nextents
== 0) {
5513 XFS_STATS_INC(xs_blk_unmap
);
5514 isrt
= (whichfork
== XFS_DATA_FORK
) && XFS_IS_REALTIME_INODE(ip
);
5516 bno
= start
+ len
- 1;
5517 ep
= xfs_bmap_search_extents(ip
, bno
, whichfork
, &eof
, &lastx
, &got
,
5521 * Check to see if the given block number is past the end of the
5522 * file, back up to the last block if so...
5525 ep
= xfs_iext_get_ext(ifp
, --lastx
);
5526 xfs_bmbt_get_all(ep
, &got
);
5527 bno
= got
.br_startoff
+ got
.br_blockcount
- 1;
5530 if (ifp
->if_flags
& XFS_IFBROOT
) {
5531 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
);
5532 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
5533 cur
->bc_private
.b
.firstblock
= *firstblock
;
5534 cur
->bc_private
.b
.flist
= flist
;
5535 cur
->bc_private
.b
.flags
= 0;
5541 * Synchronize by locking the bitmap inode.
5543 xfs_ilock(mp
->m_rbmip
, XFS_ILOCK_EXCL
);
5544 xfs_trans_ijoin(tp
, mp
->m_rbmip
, XFS_ILOCK_EXCL
);
5548 while (bno
!= (xfs_fileoff_t
)-1 && bno
>= start
&& lastx
>= 0 &&
5549 (nexts
== 0 || extno
< nexts
)) {
5551 * Is the found extent after a hole in which bno lives?
5552 * Just back up to the previous extent, if so.
5554 if (got
.br_startoff
> bno
) {
5557 ep
= xfs_iext_get_ext(ifp
, lastx
);
5558 xfs_bmbt_get_all(ep
, &got
);
5561 * Is the last block of this extent before the range
5562 * we're supposed to delete? If so, we're done.
5564 bno
= XFS_FILEOFF_MIN(bno
,
5565 got
.br_startoff
+ got
.br_blockcount
- 1);
5569 * Then deal with the (possibly delayed) allocated space
5574 wasdel
= isnullstartblock(del
.br_startblock
);
5575 if (got
.br_startoff
< start
) {
5576 del
.br_startoff
= start
;
5577 del
.br_blockcount
-= start
- got
.br_startoff
;
5579 del
.br_startblock
+= start
- got
.br_startoff
;
5581 if (del
.br_startoff
+ del
.br_blockcount
> bno
+ 1)
5582 del
.br_blockcount
= bno
+ 1 - del
.br_startoff
;
5583 sum
= del
.br_startblock
+ del
.br_blockcount
;
5585 (mod
= do_mod(sum
, mp
->m_sb
.sb_rextsize
))) {
5587 * Realtime extent not lined up at the end.
5588 * The extent could have been split into written
5589 * and unwritten pieces, or we could just be
5590 * unmapping part of it. But we can't really
5591 * get rid of part of a realtime extent.
5593 if (del
.br_state
== XFS_EXT_UNWRITTEN
||
5594 !xfs_sb_version_hasextflgbit(&mp
->m_sb
)) {
5596 * This piece is unwritten, or we're not
5597 * using unwritten extents. Skip over it.
5600 bno
-= mod
> del
.br_blockcount
?
5601 del
.br_blockcount
: mod
;
5602 if (bno
< got
.br_startoff
) {
5604 xfs_bmbt_get_all(xfs_iext_get_ext(
5610 * It's written, turn it unwritten.
5611 * This is better than zeroing it.
5613 ASSERT(del
.br_state
== XFS_EXT_NORM
);
5614 ASSERT(xfs_trans_get_block_res(tp
) > 0);
5616 * If this spans a realtime extent boundary,
5617 * chop it back to the start of the one we end at.
5619 if (del
.br_blockcount
> mod
) {
5620 del
.br_startoff
+= del
.br_blockcount
- mod
;
5621 del
.br_startblock
+= del
.br_blockcount
- mod
;
5622 del
.br_blockcount
= mod
;
5624 del
.br_state
= XFS_EXT_UNWRITTEN
;
5625 error
= xfs_bmap_add_extent_unwritten_real(tp
, ip
,
5626 &lastx
, &cur
, &del
, firstblock
, flist
,
5632 if (isrt
&& (mod
= do_mod(del
.br_startblock
, mp
->m_sb
.sb_rextsize
))) {
5634 * Realtime extent is lined up at the end but not
5635 * at the front. We'll get rid of full extents if
5638 mod
= mp
->m_sb
.sb_rextsize
- mod
;
5639 if (del
.br_blockcount
> mod
) {
5640 del
.br_blockcount
-= mod
;
5641 del
.br_startoff
+= mod
;
5642 del
.br_startblock
+= mod
;
5643 } else if ((del
.br_startoff
== start
&&
5644 (del
.br_state
== XFS_EXT_UNWRITTEN
||
5645 xfs_trans_get_block_res(tp
) == 0)) ||
5646 !xfs_sb_version_hasextflgbit(&mp
->m_sb
)) {
5648 * Can't make it unwritten. There isn't
5649 * a full extent here so just skip it.
5651 ASSERT(bno
>= del
.br_blockcount
);
5652 bno
-= del
.br_blockcount
;
5653 if (got
.br_startoff
> bno
) {
5655 ep
= xfs_iext_get_ext(ifp
,
5657 xfs_bmbt_get_all(ep
, &got
);
5661 } else if (del
.br_state
== XFS_EXT_UNWRITTEN
) {
5663 * This one is already unwritten.
5664 * It must have a written left neighbor.
5665 * Unwrite the killed part of that one and
5669 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
,
5671 ASSERT(prev
.br_state
== XFS_EXT_NORM
);
5672 ASSERT(!isnullstartblock(prev
.br_startblock
));
5673 ASSERT(del
.br_startblock
==
5674 prev
.br_startblock
+ prev
.br_blockcount
);
5675 if (prev
.br_startoff
< start
) {
5676 mod
= start
- prev
.br_startoff
;
5677 prev
.br_blockcount
-= mod
;
5678 prev
.br_startblock
+= mod
;
5679 prev
.br_startoff
= start
;
5681 prev
.br_state
= XFS_EXT_UNWRITTEN
;
5683 error
= xfs_bmap_add_extent_unwritten_real(tp
,
5684 ip
, &lastx
, &cur
, &prev
,
5685 firstblock
, flist
, &logflags
);
5690 ASSERT(del
.br_state
== XFS_EXT_NORM
);
5691 del
.br_state
= XFS_EXT_UNWRITTEN
;
5692 error
= xfs_bmap_add_extent_unwritten_real(tp
,
5693 ip
, &lastx
, &cur
, &del
,
5694 firstblock
, flist
, &logflags
);
5701 ASSERT(startblockval(del
.br_startblock
) > 0);
5702 /* Update realtime/data freespace, unreserve quota */
5704 xfs_filblks_t rtexts
;
5706 rtexts
= XFS_FSB_TO_B(mp
, del
.br_blockcount
);
5707 do_div(rtexts
, mp
->m_sb
.sb_rextsize
);
5708 xfs_mod_incore_sb(mp
, XFS_SBS_FREXTENTS
,
5709 (int64_t)rtexts
, 0);
5710 (void)xfs_trans_reserve_quota_nblks(NULL
,
5711 ip
, -((long)del
.br_blockcount
), 0,
5712 XFS_QMOPT_RES_RTBLKS
);
5714 xfs_icsb_modify_counters(mp
, XFS_SBS_FDBLOCKS
,
5715 (int64_t)del
.br_blockcount
, 0);
5716 (void)xfs_trans_reserve_quota_nblks(NULL
,
5717 ip
, -((long)del
.br_blockcount
), 0,
5718 XFS_QMOPT_RES_REGBLKS
);
5720 ip
->i_delayed_blks
-= del
.br_blockcount
;
5722 cur
->bc_private
.b
.flags
|=
5723 XFS_BTCUR_BPRV_WASDEL
;
5725 cur
->bc_private
.b
.flags
&= ~XFS_BTCUR_BPRV_WASDEL
;
5727 * If it's the case where the directory code is running
5728 * with no block reservation, and the deleted block is in
5729 * the middle of its extent, and the resulting insert
5730 * of an extent would cause transformation to btree format,
5731 * then reject it. The calling code will then swap
5732 * blocks around instead.
5733 * We have to do this now, rather than waiting for the
5734 * conversion to btree format, since the transaction
5737 if (!wasdel
&& xfs_trans_get_block_res(tp
) == 0 &&
5738 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
&&
5739 XFS_IFORK_NEXTENTS(ip
, whichfork
) >= /* Note the >= */
5740 XFS_IFORK_MAXEXT(ip
, whichfork
) &&
5741 del
.br_startoff
> got
.br_startoff
&&
5742 del
.br_startoff
+ del
.br_blockcount
<
5743 got
.br_startoff
+ got
.br_blockcount
) {
5744 error
= XFS_ERROR(ENOSPC
);
5747 error
= xfs_bmap_del_extent(ip
, tp
, &lastx
, flist
, cur
, &del
,
5748 &tmp_logflags
, whichfork
);
5749 logflags
|= tmp_logflags
;
5752 bno
= del
.br_startoff
- 1;
5755 * If not done go on to the next (previous) record.
5757 if (bno
!= (xfs_fileoff_t
)-1 && bno
>= start
) {
5759 ep
= xfs_iext_get_ext(ifp
, lastx
);
5760 if (xfs_bmbt_get_startoff(ep
) > bno
) {
5762 ep
= xfs_iext_get_ext(ifp
,
5765 xfs_bmbt_get_all(ep
, &got
);
5770 *done
= bno
== (xfs_fileoff_t
)-1 || bno
< start
|| lastx
< 0;
5773 * Convert to a btree if necessary.
5775 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
5776 ASSERT(cur
== NULL
);
5777 error
= xfs_bmap_extents_to_btree(tp
, ip
, firstblock
, flist
,
5778 &cur
, 0, &tmp_logflags
, whichfork
);
5779 logflags
|= tmp_logflags
;
5784 * transform from btree to extents, give it cur
5786 else if (xfs_bmap_wants_extents(ip
, whichfork
)) {
5787 ASSERT(cur
!= NULL
);
5788 error
= xfs_bmap_btree_to_extents(tp
, ip
, cur
, &tmp_logflags
,
5790 logflags
|= tmp_logflags
;
5795 * transform from extents to local?
5800 * Log everything. Do this after conversion, there's no point in
5801 * logging the extent records if we've converted to btree format.
5803 if ((logflags
& xfs_ilog_fext(whichfork
)) &&
5804 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
5805 logflags
&= ~xfs_ilog_fext(whichfork
);
5806 else if ((logflags
& xfs_ilog_fbroot(whichfork
)) &&
5807 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)
5808 logflags
&= ~xfs_ilog_fbroot(whichfork
);
5810 * Log inode even in the error case, if the transaction
5811 * is dirty we'll need to shut down the filesystem.
5814 xfs_trans_log_inode(tp
, ip
, logflags
);
5817 *firstblock
= cur
->bc_private
.b
.firstblock
;
5818 cur
->bc_private
.b
.allocated
= 0;
5820 xfs_btree_del_cursor(cur
,
5821 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
5827 * returns 1 for success, 0 if we failed to map the extent.
5830 xfs_getbmapx_fix_eof_hole(
5831 xfs_inode_t
*ip
, /* xfs incore inode pointer */
5832 struct getbmapx
*out
, /* output structure */
5833 int prealloced
, /* this is a file with
5834 * preallocated data space */
5835 __int64_t end
, /* last block requested */
5836 xfs_fsblock_t startblock
)
5839 xfs_mount_t
*mp
; /* file system mount point */
5840 xfs_ifork_t
*ifp
; /* inode fork pointer */
5841 xfs_extnum_t lastx
; /* last extent pointer */
5842 xfs_fileoff_t fileblock
;
5844 if (startblock
== HOLESTARTBLOCK
) {
5846 out
->bmv_block
= -1;
5847 fixlen
= XFS_FSB_TO_BB(mp
, XFS_B_TO_FSB(mp
, XFS_ISIZE(ip
)));
5848 fixlen
-= out
->bmv_offset
;
5849 if (prealloced
&& out
->bmv_offset
+ out
->bmv_length
== end
) {
5850 /* Came to hole at EOF. Trim it. */
5853 out
->bmv_length
= fixlen
;
5856 if (startblock
== DELAYSTARTBLOCK
)
5857 out
->bmv_block
= -2;
5859 out
->bmv_block
= xfs_fsb_to_db(ip
, startblock
);
5860 fileblock
= XFS_BB_TO_FSB(ip
->i_mount
, out
->bmv_offset
);
5861 ifp
= XFS_IFORK_PTR(ip
, XFS_DATA_FORK
);
5862 if (xfs_iext_bno_to_ext(ifp
, fileblock
, &lastx
) &&
5863 (lastx
== (ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
))-1))
5864 out
->bmv_oflags
|= BMV_OF_LAST
;
5871 * Get inode's extents as described in bmv, and format for output.
5872 * Calls formatter to fill the user's buffer until all extents
5873 * are mapped, until the passed-in bmv->bmv_count slots have
5874 * been filled, or until the formatter short-circuits the loop,
5875 * if it is tracking filled-in extents on its own.
5877 int /* error code */
5880 struct getbmapx
*bmv
, /* user bmap structure */
5881 xfs_bmap_format_t formatter
, /* format to user */
5882 void *arg
) /* formatter arg */
5884 __int64_t bmvend
; /* last block requested */
5885 int error
= 0; /* return value */
5886 __int64_t fixlen
; /* length for -1 case */
5887 int i
; /* extent number */
5888 int lock
; /* lock state */
5889 xfs_bmbt_irec_t
*map
; /* buffer for user's data */
5890 xfs_mount_t
*mp
; /* file system mount point */
5891 int nex
; /* # of user extents can do */
5892 int nexleft
; /* # of user extents left */
5893 int subnex
; /* # of bmapi's can do */
5894 int nmap
; /* number of map entries */
5895 struct getbmapx
*out
; /* output structure */
5896 int whichfork
; /* data or attr fork */
5897 int prealloced
; /* this is a file with
5898 * preallocated data space */
5899 int iflags
; /* interface flags */
5900 int bmapi_flags
; /* flags for xfs_bmapi */
5904 iflags
= bmv
->bmv_iflags
;
5905 whichfork
= iflags
& BMV_IF_ATTRFORK
? XFS_ATTR_FORK
: XFS_DATA_FORK
;
5907 if (whichfork
== XFS_ATTR_FORK
) {
5908 if (XFS_IFORK_Q(ip
)) {
5909 if (ip
->i_d
.di_aformat
!= XFS_DINODE_FMT_EXTENTS
&&
5910 ip
->i_d
.di_aformat
!= XFS_DINODE_FMT_BTREE
&&
5911 ip
->i_d
.di_aformat
!= XFS_DINODE_FMT_LOCAL
)
5912 return XFS_ERROR(EINVAL
);
5913 } else if (unlikely(
5914 ip
->i_d
.di_aformat
!= 0 &&
5915 ip
->i_d
.di_aformat
!= XFS_DINODE_FMT_EXTENTS
)) {
5916 XFS_ERROR_REPORT("xfs_getbmap", XFS_ERRLEVEL_LOW
,
5918 return XFS_ERROR(EFSCORRUPTED
);
5924 if (ip
->i_d
.di_format
!= XFS_DINODE_FMT_EXTENTS
&&
5925 ip
->i_d
.di_format
!= XFS_DINODE_FMT_BTREE
&&
5926 ip
->i_d
.di_format
!= XFS_DINODE_FMT_LOCAL
)
5927 return XFS_ERROR(EINVAL
);
5929 if (xfs_get_extsz_hint(ip
) ||
5930 ip
->i_d
.di_flags
& (XFS_DIFLAG_PREALLOC
|XFS_DIFLAG_APPEND
)){
5932 fixlen
= mp
->m_super
->s_maxbytes
;
5935 fixlen
= XFS_ISIZE(ip
);
5939 if (bmv
->bmv_length
== -1) {
5940 fixlen
= XFS_FSB_TO_BB(mp
, XFS_B_TO_FSB(mp
, fixlen
));
5942 max_t(__int64_t
, fixlen
- bmv
->bmv_offset
, 0);
5943 } else if (bmv
->bmv_length
== 0) {
5944 bmv
->bmv_entries
= 0;
5946 } else if (bmv
->bmv_length
< 0) {
5947 return XFS_ERROR(EINVAL
);
5950 nex
= bmv
->bmv_count
- 1;
5952 return XFS_ERROR(EINVAL
);
5953 bmvend
= bmv
->bmv_offset
+ bmv
->bmv_length
;
5956 if (bmv
->bmv_count
> ULONG_MAX
/ sizeof(struct getbmapx
))
5957 return XFS_ERROR(ENOMEM
);
5958 out
= kmem_zalloc(bmv
->bmv_count
* sizeof(struct getbmapx
), KM_MAYFAIL
);
5960 out
= kmem_zalloc_large(bmv
->bmv_count
*
5961 sizeof(struct getbmapx
));
5963 return XFS_ERROR(ENOMEM
);
5966 xfs_ilock(ip
, XFS_IOLOCK_SHARED
);
5967 if (whichfork
== XFS_DATA_FORK
&& !(iflags
& BMV_IF_DELALLOC
)) {
5968 if (ip
->i_delayed_blks
|| XFS_ISIZE(ip
) > ip
->i_d
.di_size
) {
5969 error
= -filemap_write_and_wait(VFS_I(ip
)->i_mapping
);
5971 goto out_unlock_iolock
;
5974 * even after flushing the inode, there can still be delalloc
5975 * blocks on the inode beyond EOF due to speculative
5976 * preallocation. These are not removed until the release
5977 * function is called or the inode is inactivated. Hence we
5978 * cannot assert here that ip->i_delayed_blks == 0.
5982 lock
= xfs_ilock_map_shared(ip
);
5985 * Don't let nex be bigger than the number of extents
5986 * we can have assuming alternating holes and real extents.
5988 if (nex
> XFS_IFORK_NEXTENTS(ip
, whichfork
) * 2 + 1)
5989 nex
= XFS_IFORK_NEXTENTS(ip
, whichfork
) * 2 + 1;
5991 bmapi_flags
= xfs_bmapi_aflag(whichfork
);
5992 if (!(iflags
& BMV_IF_PREALLOC
))
5993 bmapi_flags
|= XFS_BMAPI_IGSTATE
;
5996 * Allocate enough space to handle "subnex" maps at a time.
6000 map
= kmem_alloc(subnex
* sizeof(*map
), KM_MAYFAIL
| KM_NOFS
);
6002 goto out_unlock_ilock
;
6004 bmv
->bmv_entries
= 0;
6006 if (XFS_IFORK_NEXTENTS(ip
, whichfork
) == 0 &&
6007 (whichfork
== XFS_ATTR_FORK
|| !(iflags
& BMV_IF_DELALLOC
))) {
6015 nmap
= (nexleft
> subnex
) ? subnex
: nexleft
;
6016 error
= xfs_bmapi_read(ip
, XFS_BB_TO_FSBT(mp
, bmv
->bmv_offset
),
6017 XFS_BB_TO_FSB(mp
, bmv
->bmv_length
),
6018 map
, &nmap
, bmapi_flags
);
6021 ASSERT(nmap
<= subnex
);
6023 for (i
= 0; i
< nmap
&& nexleft
&& bmv
->bmv_length
; i
++) {
6024 out
[cur_ext
].bmv_oflags
= 0;
6025 if (map
[i
].br_state
== XFS_EXT_UNWRITTEN
)
6026 out
[cur_ext
].bmv_oflags
|= BMV_OF_PREALLOC
;
6027 else if (map
[i
].br_startblock
== DELAYSTARTBLOCK
)
6028 out
[cur_ext
].bmv_oflags
|= BMV_OF_DELALLOC
;
6029 out
[cur_ext
].bmv_offset
=
6030 XFS_FSB_TO_BB(mp
, map
[i
].br_startoff
);
6031 out
[cur_ext
].bmv_length
=
6032 XFS_FSB_TO_BB(mp
, map
[i
].br_blockcount
);
6033 out
[cur_ext
].bmv_unused1
= 0;
6034 out
[cur_ext
].bmv_unused2
= 0;
6037 * delayed allocation extents that start beyond EOF can
6038 * occur due to speculative EOF allocation when the
6039 * delalloc extent is larger than the largest freespace
6040 * extent at conversion time. These extents cannot be
6041 * converted by data writeback, so can exist here even
6042 * if we are not supposed to be finding delalloc
6045 if (map
[i
].br_startblock
== DELAYSTARTBLOCK
&&
6046 map
[i
].br_startoff
<= XFS_B_TO_FSB(mp
, XFS_ISIZE(ip
)))
6047 ASSERT((iflags
& BMV_IF_DELALLOC
) != 0);
6049 if (map
[i
].br_startblock
== HOLESTARTBLOCK
&&
6050 whichfork
== XFS_ATTR_FORK
) {
6051 /* came to the end of attribute fork */
6052 out
[cur_ext
].bmv_oflags
|= BMV_OF_LAST
;
6056 if (!xfs_getbmapx_fix_eof_hole(ip
, &out
[cur_ext
],
6058 map
[i
].br_startblock
))
6062 out
[cur_ext
].bmv_offset
+
6063 out
[cur_ext
].bmv_length
;
6065 max_t(__int64_t
, 0, bmvend
- bmv
->bmv_offset
);
6068 * In case we don't want to return the hole,
6069 * don't increase cur_ext so that we can reuse
6070 * it in the next loop.
6072 if ((iflags
& BMV_IF_NO_HOLES
) &&
6073 map
[i
].br_startblock
== HOLESTARTBLOCK
) {
6074 memset(&out
[cur_ext
], 0, sizeof(out
[cur_ext
]));
6082 } while (nmap
&& nexleft
&& bmv
->bmv_length
);
6087 xfs_iunlock_map_shared(ip
, lock
);
6089 xfs_iunlock(ip
, XFS_IOLOCK_SHARED
);
6091 for (i
= 0; i
< cur_ext
; i
++) {
6092 int full
= 0; /* user array is full */
6094 /* format results & advance arg */
6095 error
= formatter(&arg
, &out
[i
], &full
);
6100 if (is_vmalloc_addr(out
))
6101 kmem_free_large(out
);
6108 * dead simple method of punching delalyed allocation blocks from a range in
6109 * the inode. Walks a block at a time so will be slow, but is only executed in
6110 * rare error cases so the overhead is not critical. This will alays punch out
6111 * both the start and end blocks, even if the ranges only partially overlap
6112 * them, so it is up to the caller to ensure that partial blocks are not
6116 xfs_bmap_punch_delalloc_range(
6117 struct xfs_inode
*ip
,
6118 xfs_fileoff_t start_fsb
,
6119 xfs_fileoff_t length
)
6121 xfs_fileoff_t remaining
= length
;
6124 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
6128 xfs_bmbt_irec_t imap
;
6130 xfs_fsblock_t firstblock
;
6131 xfs_bmap_free_t flist
;
6134 * Map the range first and check that it is a delalloc extent
6135 * before trying to unmap the range. Otherwise we will be
6136 * trying to remove a real extent (which requires a
6137 * transaction) or a hole, which is probably a bad idea...
6139 error
= xfs_bmapi_read(ip
, start_fsb
, 1, &imap
, &nimaps
,
6143 /* something screwed, just bail */
6144 if (!XFS_FORCED_SHUTDOWN(ip
->i_mount
)) {
6145 xfs_alert(ip
->i_mount
,
6146 "Failed delalloc mapping lookup ino %lld fsb %lld.",
6147 ip
->i_ino
, start_fsb
);
6155 if (imap
.br_startblock
!= DELAYSTARTBLOCK
) {
6156 /* been converted, ignore */
6159 WARN_ON(imap
.br_blockcount
== 0);
6162 * Note: while we initialise the firstblock/flist pair, they
6163 * should never be used because blocks should never be
6164 * allocated or freed for a delalloc extent and hence we need
6165 * don't cancel or finish them after the xfs_bunmapi() call.
6167 xfs_bmap_init(&flist
, &firstblock
);
6168 error
= xfs_bunmapi(NULL
, ip
, start_fsb
, 1, 0, 1, &firstblock
,
6173 ASSERT(!flist
.xbf_count
&& !flist
.xbf_first
);
6177 } while(remaining
> 0);