2 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 #include "xfs_types.h"
24 #include "xfs_trans.h"
28 #include "xfs_da_btree.h"
29 #include "xfs_bmap_btree.h"
30 #include "xfs_alloc_btree.h"
31 #include "xfs_ialloc_btree.h"
32 #include "xfs_dinode.h"
33 #include "xfs_inode.h"
34 #include "xfs_btree.h"
35 #include "xfs_mount.h"
36 #include "xfs_itable.h"
37 #include "xfs_inode_item.h"
38 #include "xfs_extfree_item.h"
39 #include "xfs_alloc.h"
41 #include "xfs_rtalloc.h"
42 #include "xfs_error.h"
43 #include "xfs_attr_leaf.h"
44 #include "xfs_quota.h"
45 #include "xfs_trans_space.h"
46 #include "xfs_buf_item.h"
47 #include "xfs_filestream.h"
48 #include "xfs_vnodeops.h"
49 #include "xfs_trace.h"
52 kmem_zone_t
*xfs_bmap_free_item_zone
;
55 * Miscellaneous helper functions
59 * Compute and fill in the value of the maximum depth of a bmap btree
60 * in this filesystem. Done once, during mount.
63 xfs_bmap_compute_maxlevels(
64 xfs_mount_t
*mp
, /* file system mount structure */
65 int whichfork
) /* data or attr fork */
67 int level
; /* btree level */
68 uint maxblocks
; /* max blocks at this level */
69 uint maxleafents
; /* max leaf entries possible */
70 int maxrootrecs
; /* max records in root block */
71 int minleafrecs
; /* min records in leaf block */
72 int minnoderecs
; /* min records in node block */
73 int sz
; /* root block size */
76 * The maximum number of extents in a file, hence the maximum
77 * number of leaf entries, is controlled by the type of di_nextents
78 * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
79 * (a signed 16-bit number, xfs_aextnum_t).
81 * Note that we can no longer assume that if we are in ATTR1 that
82 * the fork offset of all the inodes will be
83 * (xfs_default_attroffset(ip) >> 3) because we could have mounted
84 * with ATTR2 and then mounted back with ATTR1, keeping the
85 * di_forkoff's fixed but probably at various positions. Therefore,
86 * for both ATTR1 and ATTR2 we have to assume the worst case scenario
87 * of a minimum size available.
89 if (whichfork
== XFS_DATA_FORK
) {
90 maxleafents
= MAXEXTNUM
;
91 sz
= XFS_BMDR_SPACE_CALC(MINDBTPTRS
);
93 maxleafents
= MAXAEXTNUM
;
94 sz
= XFS_BMDR_SPACE_CALC(MINABTPTRS
);
96 maxrootrecs
= xfs_bmdr_maxrecs(mp
, sz
, 0);
97 minleafrecs
= mp
->m_bmap_dmnr
[0];
98 minnoderecs
= mp
->m_bmap_dmnr
[1];
99 maxblocks
= (maxleafents
+ minleafrecs
- 1) / minleafrecs
;
100 for (level
= 1; maxblocks
> 1; level
++) {
101 if (maxblocks
<= maxrootrecs
)
104 maxblocks
= (maxblocks
+ minnoderecs
- 1) / minnoderecs
;
106 mp
->m_bm_maxlevels
[whichfork
] = level
;
110 * Convert the given file system block to a disk block. We have to treat it
111 * differently based on whether the file is a real time file or not, because the
115 xfs_fsb_to_db(struct xfs_inode
*ip
, xfs_fsblock_t fsb
)
117 return (XFS_IS_REALTIME_INODE(ip
) ? \
118 (xfs_daddr_t
)XFS_FSB_TO_BB((ip
)->i_mount
, (fsb
)) : \
119 XFS_FSB_TO_DADDR((ip
)->i_mount
, (fsb
)));
122 STATIC
int /* error */
124 struct xfs_btree_cur
*cur
,
128 int *stat
) /* success/failure */
130 cur
->bc_rec
.b
.br_startoff
= off
;
131 cur
->bc_rec
.b
.br_startblock
= bno
;
132 cur
->bc_rec
.b
.br_blockcount
= len
;
133 return xfs_btree_lookup(cur
, XFS_LOOKUP_EQ
, stat
);
136 STATIC
int /* error */
138 struct xfs_btree_cur
*cur
,
142 int *stat
) /* success/failure */
144 cur
->bc_rec
.b
.br_startoff
= off
;
145 cur
->bc_rec
.b
.br_startblock
= bno
;
146 cur
->bc_rec
.b
.br_blockcount
= len
;
147 return xfs_btree_lookup(cur
, XFS_LOOKUP_GE
, stat
);
151 * Check if the inode needs to be converted to btree format.
153 static inline bool xfs_bmap_needs_btree(struct xfs_inode
*ip
, int whichfork
)
155 return XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
&&
156 XFS_IFORK_NEXTENTS(ip
, whichfork
) >
157 XFS_IFORK_MAXEXT(ip
, whichfork
);
161 * Check if the inode should be converted to extent format.
163 static inline bool xfs_bmap_wants_extents(struct xfs_inode
*ip
, int whichfork
)
165 return XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
&&
166 XFS_IFORK_NEXTENTS(ip
, whichfork
) <=
167 XFS_IFORK_MAXEXT(ip
, whichfork
);
171 * Update the record referred to by cur to the value given
172 * by [off, bno, len, state].
173 * This either works (return 0) or gets an EFSCORRUPTED error.
177 struct xfs_btree_cur
*cur
,
183 union xfs_btree_rec rec
;
185 xfs_bmbt_disk_set_allf(&rec
.bmbt
, off
, bno
, len
, state
);
186 return xfs_btree_update(cur
, &rec
);
190 * Compute the worst-case number of indirect blocks that will be used
191 * for ip's delayed extent of length "len".
194 xfs_bmap_worst_indlen(
195 xfs_inode_t
*ip
, /* incore inode pointer */
196 xfs_filblks_t len
) /* delayed extent length */
198 int level
; /* btree level number */
199 int maxrecs
; /* maximum record count at this level */
200 xfs_mount_t
*mp
; /* mount structure */
201 xfs_filblks_t rval
; /* return value */
204 maxrecs
= mp
->m_bmap_dmxr
[0];
205 for (level
= 0, rval
= 0;
206 level
< XFS_BM_MAXLEVELS(mp
, XFS_DATA_FORK
);
209 do_div(len
, maxrecs
);
212 return rval
+ XFS_BM_MAXLEVELS(mp
, XFS_DATA_FORK
) -
215 maxrecs
= mp
->m_bmap_dmxr
[1];
221 * Calculate the default attribute fork offset for newly created inodes.
224 xfs_default_attroffset(
225 struct xfs_inode
*ip
)
227 struct xfs_mount
*mp
= ip
->i_mount
;
230 if (mp
->m_sb
.sb_inodesize
== 256) {
231 offset
= XFS_LITINO(mp
) -
232 XFS_BMDR_SPACE_CALC(MINABTPTRS
);
234 offset
= XFS_BMDR_SPACE_CALC(6 * MINABTPTRS
);
237 ASSERT(offset
< XFS_LITINO(mp
));
242 * Helper routine to reset inode di_forkoff field when switching
243 * attribute fork from local to extent format - we reset it where
244 * possible to make space available for inline data fork extents.
247 xfs_bmap_forkoff_reset(
252 if (whichfork
== XFS_ATTR_FORK
&&
253 ip
->i_d
.di_format
!= XFS_DINODE_FMT_DEV
&&
254 ip
->i_d
.di_format
!= XFS_DINODE_FMT_UUID
&&
255 ip
->i_d
.di_format
!= XFS_DINODE_FMT_BTREE
) {
256 uint dfl_forkoff
= xfs_default_attroffset(ip
) >> 3;
258 if (dfl_forkoff
> ip
->i_d
.di_forkoff
)
259 ip
->i_d
.di_forkoff
= dfl_forkoff
;
264 * Extent tree block counting routines.
268 * Count leaf blocks given a range of extent records.
271 xfs_bmap_count_leaves(
279 for (b
= 0; b
< numrecs
; b
++) {
280 xfs_bmbt_rec_host_t
*frp
= xfs_iext_get_ext(ifp
, idx
+ b
);
281 *count
+= xfs_bmbt_get_blockcount(frp
);
286 * Count leaf blocks given a range of extent records originally
290 xfs_bmap_disk_count_leaves(
291 struct xfs_mount
*mp
,
292 struct xfs_btree_block
*block
,
299 for (b
= 1; b
<= numrecs
; b
++) {
300 frp
= XFS_BMBT_REC_ADDR(mp
, block
, b
);
301 *count
+= xfs_bmbt_disk_get_blockcount(frp
);
306 * Recursively walks each level of a btree
307 * to count total fsblocks is use.
309 STATIC
int /* error */
311 xfs_mount_t
*mp
, /* file system mount point */
312 xfs_trans_t
*tp
, /* transaction pointer */
313 xfs_ifork_t
*ifp
, /* inode fork pointer */
314 xfs_fsblock_t blockno
, /* file system block number */
315 int levelin
, /* level in btree */
316 int *count
) /* Count of blocks */
322 xfs_fsblock_t bno
= blockno
;
323 xfs_fsblock_t nextbno
;
324 struct xfs_btree_block
*block
, *nextblock
;
327 error
= xfs_btree_read_bufl(mp
, tp
, bno
, 0, &bp
, XFS_BMAP_BTREE_REF
,
332 block
= XFS_BUF_TO_BLOCK(bp
);
335 /* Not at node above leaves, count this level of nodes */
336 nextbno
= be64_to_cpu(block
->bb_u
.l
.bb_rightsib
);
337 while (nextbno
!= NULLFSBLOCK
) {
338 error
= xfs_btree_read_bufl(mp
, tp
, nextbno
, 0, &nbp
,
344 nextblock
= XFS_BUF_TO_BLOCK(nbp
);
345 nextbno
= be64_to_cpu(nextblock
->bb_u
.l
.bb_rightsib
);
346 xfs_trans_brelse(tp
, nbp
);
349 /* Dive to the next level */
350 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, mp
->m_bmap_dmxr
[1]);
351 bno
= be64_to_cpu(*pp
);
352 if (unlikely((error
=
353 xfs_bmap_count_tree(mp
, tp
, ifp
, bno
, level
, count
)) < 0)) {
354 xfs_trans_brelse(tp
, bp
);
355 XFS_ERROR_REPORT("xfs_bmap_count_tree(1)",
356 XFS_ERRLEVEL_LOW
, mp
);
357 return XFS_ERROR(EFSCORRUPTED
);
359 xfs_trans_brelse(tp
, bp
);
361 /* count all level 1 nodes and their leaves */
363 nextbno
= be64_to_cpu(block
->bb_u
.l
.bb_rightsib
);
364 numrecs
= be16_to_cpu(block
->bb_numrecs
);
365 xfs_bmap_disk_count_leaves(mp
, block
, numrecs
, count
);
366 xfs_trans_brelse(tp
, bp
);
367 if (nextbno
== NULLFSBLOCK
)
370 error
= xfs_btree_read_bufl(mp
, tp
, bno
, 0, &bp
,
376 block
= XFS_BUF_TO_BLOCK(bp
);
383 * Count fsblocks of the given fork.
386 xfs_bmap_count_blocks(
387 xfs_trans_t
*tp
, /* transaction pointer */
388 xfs_inode_t
*ip
, /* incore inode */
389 int whichfork
, /* data or attr fork */
390 int *count
) /* out: count of blocks */
392 struct xfs_btree_block
*block
; /* current btree block */
393 xfs_fsblock_t bno
; /* block # of "block" */
394 xfs_ifork_t
*ifp
; /* fork structure */
395 int level
; /* btree level, for checking */
396 xfs_mount_t
*mp
; /* file system mount structure */
397 __be64
*pp
; /* pointer to block address */
401 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
402 if ( XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
) {
403 xfs_bmap_count_leaves(ifp
, 0,
404 ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
),
410 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
412 block
= ifp
->if_broot
;
413 level
= be16_to_cpu(block
->bb_level
);
415 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, 1, ifp
->if_broot_bytes
);
416 bno
= be64_to_cpu(*pp
);
417 ASSERT(bno
!= NULLDFSBNO
);
418 ASSERT(XFS_FSB_TO_AGNO(mp
, bno
) < mp
->m_sb
.sb_agcount
);
419 ASSERT(XFS_FSB_TO_AGBNO(mp
, bno
) < mp
->m_sb
.sb_agblocks
);
421 if (unlikely(xfs_bmap_count_tree(mp
, tp
, ifp
, bno
, level
, count
) < 0)) {
422 XFS_ERROR_REPORT("xfs_bmap_count_blocks(2)", XFS_ERRLEVEL_LOW
,
424 return XFS_ERROR(EFSCORRUPTED
);
431 * Debug/sanity checking code
435 xfs_bmap_sanity_check(
436 struct xfs_mount
*mp
,
440 struct xfs_btree_block
*block
= XFS_BUF_TO_BLOCK(bp
);
442 if (block
->bb_magic
!= cpu_to_be32(XFS_BMAP_MAGIC
) ||
443 be16_to_cpu(block
->bb_level
) != level
||
444 be16_to_cpu(block
->bb_numrecs
) == 0 ||
445 be16_to_cpu(block
->bb_numrecs
) > mp
->m_bmap_dmxr
[level
!= 0])
451 STATIC
struct xfs_buf
*
453 struct xfs_btree_cur
*cur
,
456 struct xfs_log_item_desc
*lidp
;
462 for (i
= 0; i
< XFS_BTREE_MAXLEVELS
; i
++) {
463 if (!cur
->bc_bufs
[i
])
465 if (XFS_BUF_ADDR(cur
->bc_bufs
[i
]) == bno
)
466 return cur
->bc_bufs
[i
];
469 /* Chase down all the log items to see if the bp is there */
470 list_for_each_entry(lidp
, &cur
->bc_tp
->t_items
, lid_trans
) {
471 struct xfs_buf_log_item
*bip
;
472 bip
= (struct xfs_buf_log_item
*)lidp
->lid_item
;
473 if (bip
->bli_item
.li_type
== XFS_LI_BUF
&&
474 XFS_BUF_ADDR(bip
->bli_buf
) == bno
)
483 struct xfs_btree_block
*block
,
489 __be64
*pp
, *thispa
; /* pointer to block address */
490 xfs_bmbt_key_t
*prevp
, *keyp
;
492 ASSERT(be16_to_cpu(block
->bb_level
) > 0);
495 for( i
= 1; i
<= xfs_btree_get_numrecs(block
); i
++) {
496 dmxr
= mp
->m_bmap_dmxr
[0];
497 keyp
= XFS_BMBT_KEY_ADDR(mp
, block
, i
);
500 ASSERT(be64_to_cpu(prevp
->br_startoff
) <
501 be64_to_cpu(keyp
->br_startoff
));
506 * Compare the block numbers to see if there are dups.
509 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, i
, sz
);
511 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, i
, dmxr
);
513 for (j
= i
+1; j
<= be16_to_cpu(block
->bb_numrecs
); j
++) {
515 thispa
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, j
, sz
);
517 thispa
= XFS_BMBT_PTR_ADDR(mp
, block
, j
, dmxr
);
518 if (*thispa
== *pp
) {
519 xfs_warn(mp
, "%s: thispa(%d) == pp(%d) %Ld",
521 (unsigned long long)be64_to_cpu(*thispa
));
522 panic("%s: ptrs are equal in node\n",
530 * Check that the extents for the inode ip are in the right order in all
535 xfs_bmap_check_leaf_extents(
536 xfs_btree_cur_t
*cur
, /* btree cursor or null */
537 xfs_inode_t
*ip
, /* incore inode pointer */
538 int whichfork
) /* data or attr fork */
540 struct xfs_btree_block
*block
; /* current btree block */
541 xfs_fsblock_t bno
; /* block # of "block" */
542 xfs_buf_t
*bp
; /* buffer for "block" */
543 int error
; /* error return value */
544 xfs_extnum_t i
=0, j
; /* index into the extents list */
545 xfs_ifork_t
*ifp
; /* fork structure */
546 int level
; /* btree level, for checking */
547 xfs_mount_t
*mp
; /* file system mount structure */
548 __be64
*pp
; /* pointer to block address */
549 xfs_bmbt_rec_t
*ep
; /* pointer to current extent */
550 xfs_bmbt_rec_t last
= {0, 0}; /* last extent in prev block */
551 xfs_bmbt_rec_t
*nextp
; /* pointer to next extent */
554 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
) {
560 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
561 block
= ifp
->if_broot
;
563 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
565 level
= be16_to_cpu(block
->bb_level
);
567 xfs_check_block(block
, mp
, 1, ifp
->if_broot_bytes
);
568 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, 1, ifp
->if_broot_bytes
);
569 bno
= be64_to_cpu(*pp
);
571 ASSERT(bno
!= NULLDFSBNO
);
572 ASSERT(XFS_FSB_TO_AGNO(mp
, bno
) < mp
->m_sb
.sb_agcount
);
573 ASSERT(XFS_FSB_TO_AGBNO(mp
, bno
) < mp
->m_sb
.sb_agblocks
);
576 * Go down the tree until leaf level is reached, following the first
577 * pointer (leftmost) at each level.
579 while (level
-- > 0) {
580 /* See if buf is in cur first */
582 bp
= xfs_bmap_get_bp(cur
, XFS_FSB_TO_DADDR(mp
, bno
));
585 error
= xfs_btree_read_bufl(mp
, NULL
, bno
, 0, &bp
,
591 block
= XFS_BUF_TO_BLOCK(bp
);
592 XFS_WANT_CORRUPTED_GOTO(
593 xfs_bmap_sanity_check(mp
, bp
, level
),
599 * Check this block for basic sanity (increasing keys and
600 * no duplicate blocks).
603 xfs_check_block(block
, mp
, 0, 0);
604 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, mp
->m_bmap_dmxr
[1]);
605 bno
= be64_to_cpu(*pp
);
606 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp
, bno
), error0
);
609 xfs_trans_brelse(NULL
, bp
);
614 * Here with bp and block set to the leftmost leaf node in the tree.
619 * Loop over all leaf nodes checking that all extents are in the right order.
622 xfs_fsblock_t nextbno
;
623 xfs_extnum_t num_recs
;
626 num_recs
= xfs_btree_get_numrecs(block
);
629 * Read-ahead the next leaf block, if any.
632 nextbno
= be64_to_cpu(block
->bb_u
.l
.bb_rightsib
);
635 * Check all the extents to make sure they are OK.
636 * If we had a previous block, the last entry should
637 * conform with the first entry in this one.
640 ep
= XFS_BMBT_REC_ADDR(mp
, block
, 1);
642 ASSERT(xfs_bmbt_disk_get_startoff(&last
) +
643 xfs_bmbt_disk_get_blockcount(&last
) <=
644 xfs_bmbt_disk_get_startoff(ep
));
646 for (j
= 1; j
< num_recs
; j
++) {
647 nextp
= XFS_BMBT_REC_ADDR(mp
, block
, j
+ 1);
648 ASSERT(xfs_bmbt_disk_get_startoff(ep
) +
649 xfs_bmbt_disk_get_blockcount(ep
) <=
650 xfs_bmbt_disk_get_startoff(nextp
));
658 xfs_trans_brelse(NULL
, bp
);
662 * If we've reached the end, stop.
664 if (bno
== NULLFSBLOCK
)
668 bp
= xfs_bmap_get_bp(cur
, XFS_FSB_TO_DADDR(mp
, bno
));
671 error
= xfs_btree_read_bufl(mp
, NULL
, bno
, 0, &bp
,
677 block
= XFS_BUF_TO_BLOCK(bp
);
681 xfs_trans_brelse(NULL
, bp
);
686 xfs_warn(mp
, "%s: at error0", __func__
);
688 xfs_trans_brelse(NULL
, bp
);
690 xfs_warn(mp
, "%s: BAD after btree leaves for %d extents",
692 panic("%s: CORRUPTED BTREE OR SOMETHING", __func__
);
697 * Add bmap trace insert entries for all the contents of the extent records.
700 xfs_bmap_trace_exlist(
701 xfs_inode_t
*ip
, /* incore inode pointer */
702 xfs_extnum_t cnt
, /* count of entries in the list */
703 int whichfork
, /* data or attr fork */
704 unsigned long caller_ip
)
706 xfs_extnum_t idx
; /* extent record index */
707 xfs_ifork_t
*ifp
; /* inode fork pointer */
710 if (whichfork
== XFS_ATTR_FORK
)
711 state
|= BMAP_ATTRFORK
;
713 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
714 ASSERT(cnt
== (ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
)));
715 for (idx
= 0; idx
< cnt
; idx
++)
716 trace_xfs_extlist(ip
, idx
, whichfork
, caller_ip
);
720 * Validate that the bmbt_irecs being returned from bmapi are valid
721 * given the callers original parameters. Specifically check the
722 * ranges of the returned irecs to ensure that they only extent beyond
723 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
726 xfs_bmap_validate_ret(
730 xfs_bmbt_irec_t
*mval
,
734 int i
; /* index to map values */
736 ASSERT(ret_nmap
<= nmap
);
738 for (i
= 0; i
< ret_nmap
; i
++) {
739 ASSERT(mval
[i
].br_blockcount
> 0);
740 if (!(flags
& XFS_BMAPI_ENTIRE
)) {
741 ASSERT(mval
[i
].br_startoff
>= bno
);
742 ASSERT(mval
[i
].br_blockcount
<= len
);
743 ASSERT(mval
[i
].br_startoff
+ mval
[i
].br_blockcount
<=
746 ASSERT(mval
[i
].br_startoff
< bno
+ len
);
747 ASSERT(mval
[i
].br_startoff
+ mval
[i
].br_blockcount
>
751 mval
[i
- 1].br_startoff
+ mval
[i
- 1].br_blockcount
==
752 mval
[i
].br_startoff
);
753 ASSERT(mval
[i
].br_startblock
!= DELAYSTARTBLOCK
&&
754 mval
[i
].br_startblock
!= HOLESTARTBLOCK
);
755 ASSERT(mval
[i
].br_state
== XFS_EXT_NORM
||
756 mval
[i
].br_state
== XFS_EXT_UNWRITTEN
);
761 #define xfs_bmap_check_leaf_extents(cur, ip, whichfork) do { } while (0)
762 #define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap)
766 * bmap free list manipulation functions
770 * Add the extent to the list of extents to be free at transaction end.
771 * The list is maintained sorted (by block number).
775 xfs_fsblock_t bno
, /* fs block number of extent */
776 xfs_filblks_t len
, /* length of extent */
777 xfs_bmap_free_t
*flist
, /* list of extents */
778 xfs_mount_t
*mp
) /* mount point structure */
780 xfs_bmap_free_item_t
*cur
; /* current (next) element */
781 xfs_bmap_free_item_t
*new; /* new element */
782 xfs_bmap_free_item_t
*prev
; /* previous element */
787 ASSERT(bno
!= NULLFSBLOCK
);
789 ASSERT(len
<= MAXEXTLEN
);
790 ASSERT(!isnullstartblock(bno
));
791 agno
= XFS_FSB_TO_AGNO(mp
, bno
);
792 agbno
= XFS_FSB_TO_AGBNO(mp
, bno
);
793 ASSERT(agno
< mp
->m_sb
.sb_agcount
);
794 ASSERT(agbno
< mp
->m_sb
.sb_agblocks
);
795 ASSERT(len
< mp
->m_sb
.sb_agblocks
);
796 ASSERT(agbno
+ len
<= mp
->m_sb
.sb_agblocks
);
798 ASSERT(xfs_bmap_free_item_zone
!= NULL
);
799 new = kmem_zone_alloc(xfs_bmap_free_item_zone
, KM_SLEEP
);
800 new->xbfi_startblock
= bno
;
801 new->xbfi_blockcount
= (xfs_extlen_t
)len
;
802 for (prev
= NULL
, cur
= flist
->xbf_first
;
804 prev
= cur
, cur
= cur
->xbfi_next
) {
805 if (cur
->xbfi_startblock
>= bno
)
809 prev
->xbfi_next
= new;
811 flist
->xbf_first
= new;
812 new->xbfi_next
= cur
;
817 * Remove the entry "free" from the free item list. Prev points to the
818 * previous entry, unless "free" is the head of the list.
822 xfs_bmap_free_t
*flist
, /* free item list header */
823 xfs_bmap_free_item_t
*prev
, /* previous item on list, if any */
824 xfs_bmap_free_item_t
*free
) /* list item to be freed */
827 prev
->xbfi_next
= free
->xbfi_next
;
829 flist
->xbf_first
= free
->xbfi_next
;
831 kmem_zone_free(xfs_bmap_free_item_zone
, free
);
836 * Routine to be called at transaction's end by xfs_bmapi, xfs_bunmapi
837 * caller. Frees all the extents that need freeing, which must be done
838 * last due to locking considerations. We never free any extents in
839 * the first transaction.
841 * Return 1 if the given transaction was committed and a new one
842 * started, and 0 otherwise in the committed parameter.
846 xfs_trans_t
**tp
, /* transaction pointer addr */
847 xfs_bmap_free_t
*flist
, /* i/o: list extents to free */
848 int *committed
) /* xact committed or not */
850 xfs_efd_log_item_t
*efd
; /* extent free data */
851 xfs_efi_log_item_t
*efi
; /* extent free intention */
852 int error
; /* error return value */
853 xfs_bmap_free_item_t
*free
; /* free extent item */
854 unsigned int logres
; /* new log reservation */
855 unsigned int logcount
; /* new log count */
856 xfs_mount_t
*mp
; /* filesystem mount structure */
857 xfs_bmap_free_item_t
*next
; /* next item on free list */
858 xfs_trans_t
*ntp
; /* new transaction pointer */
860 ASSERT((*tp
)->t_flags
& XFS_TRANS_PERM_LOG_RES
);
861 if (flist
->xbf_count
== 0) {
866 efi
= xfs_trans_get_efi(ntp
, flist
->xbf_count
);
867 for (free
= flist
->xbf_first
; free
; free
= free
->xbfi_next
)
868 xfs_trans_log_efi_extent(ntp
, efi
, free
->xbfi_startblock
,
869 free
->xbfi_blockcount
);
870 logres
= ntp
->t_log_res
;
871 logcount
= ntp
->t_log_count
;
872 ntp
= xfs_trans_dup(*tp
);
873 error
= xfs_trans_commit(*tp
, 0);
877 * We have a new transaction, so we should return committed=1,
878 * even though we're returning an error.
884 * transaction commit worked ok so we can drop the extra ticket
885 * reference that we gained in xfs_trans_dup()
887 xfs_log_ticket_put(ntp
->t_ticket
);
889 if ((error
= xfs_trans_reserve(ntp
, 0, logres
, 0, XFS_TRANS_PERM_LOG_RES
,
892 efd
= xfs_trans_get_efd(ntp
, efi
, flist
->xbf_count
);
893 for (free
= flist
->xbf_first
; free
!= NULL
; free
= next
) {
894 next
= free
->xbfi_next
;
895 if ((error
= xfs_free_extent(ntp
, free
->xbfi_startblock
,
896 free
->xbfi_blockcount
))) {
898 * The bmap free list will be cleaned up at a
899 * higher level. The EFI will be canceled when
900 * this transaction is aborted.
901 * Need to force shutdown here to make sure it
902 * happens, since this transaction may not be
906 if (!XFS_FORCED_SHUTDOWN(mp
))
907 xfs_force_shutdown(mp
,
908 (error
== EFSCORRUPTED
) ?
909 SHUTDOWN_CORRUPT_INCORE
:
910 SHUTDOWN_META_IO_ERROR
);
913 xfs_trans_log_efd_extent(ntp
, efd
, free
->xbfi_startblock
,
914 free
->xbfi_blockcount
);
915 xfs_bmap_del_free(flist
, NULL
, free
);
921 * Free up any items left in the list.
925 xfs_bmap_free_t
*flist
) /* list of bmap_free_items */
927 xfs_bmap_free_item_t
*free
; /* free list item */
928 xfs_bmap_free_item_t
*next
;
930 if (flist
->xbf_count
== 0)
932 ASSERT(flist
->xbf_first
!= NULL
);
933 for (free
= flist
->xbf_first
; free
; free
= next
) {
934 next
= free
->xbfi_next
;
935 xfs_bmap_del_free(flist
, NULL
, free
);
937 ASSERT(flist
->xbf_count
== 0);
941 * Inode fork format manipulation functions
945 * Transform a btree format file with only one leaf node, where the
946 * extents list will fit in the inode, into an extents format file.
947 * Since the file extents are already in-core, all we have to do is
948 * give up the space for the btree root and pitch the leaf block.
950 STATIC
int /* error */
951 xfs_bmap_btree_to_extents(
952 xfs_trans_t
*tp
, /* transaction pointer */
953 xfs_inode_t
*ip
, /* incore inode pointer */
954 xfs_btree_cur_t
*cur
, /* btree cursor */
955 int *logflagsp
, /* inode logging flags */
956 int whichfork
) /* data or attr fork */
959 struct xfs_btree_block
*cblock
;/* child btree block */
960 xfs_fsblock_t cbno
; /* child block number */
961 xfs_buf_t
*cbp
; /* child block's buffer */
962 int error
; /* error return value */
963 xfs_ifork_t
*ifp
; /* inode fork data */
964 xfs_mount_t
*mp
; /* mount point structure */
965 __be64
*pp
; /* ptr to block address */
966 struct xfs_btree_block
*rblock
;/* root btree block */
969 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
970 ASSERT(ifp
->if_flags
& XFS_IFEXTENTS
);
971 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
);
972 rblock
= ifp
->if_broot
;
973 ASSERT(be16_to_cpu(rblock
->bb_level
) == 1);
974 ASSERT(be16_to_cpu(rblock
->bb_numrecs
) == 1);
975 ASSERT(xfs_bmbt_maxrecs(mp
, ifp
->if_broot_bytes
, 0) == 1);
976 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, rblock
, 1, ifp
->if_broot_bytes
);
977 cbno
= be64_to_cpu(*pp
);
980 if ((error
= xfs_btree_check_lptr(cur
, cbno
, 1)))
983 error
= xfs_btree_read_bufl(mp
, tp
, cbno
, 0, &cbp
, XFS_BMAP_BTREE_REF
,
987 cblock
= XFS_BUF_TO_BLOCK(cbp
);
988 if ((error
= xfs_btree_check_block(cur
, cblock
, 0, cbp
)))
990 xfs_bmap_add_free(cbno
, 1, cur
->bc_private
.b
.flist
, mp
);
991 ip
->i_d
.di_nblocks
--;
992 xfs_trans_mod_dquot_byino(tp
, ip
, XFS_TRANS_DQ_BCOUNT
, -1L);
993 xfs_trans_binval(tp
, cbp
);
994 if (cur
->bc_bufs
[0] == cbp
)
995 cur
->bc_bufs
[0] = NULL
;
996 xfs_iroot_realloc(ip
, -1, whichfork
);
997 ASSERT(ifp
->if_broot
== NULL
);
998 ASSERT((ifp
->if_flags
& XFS_IFBROOT
) == 0);
999 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_EXTENTS
);
1000 *logflagsp
= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
1005 * Convert an extents-format file into a btree-format file.
1006 * The new file will have a root block (in the inode) and a single child block.
1008 STATIC
int /* error */
1009 xfs_bmap_extents_to_btree(
1010 xfs_trans_t
*tp
, /* transaction pointer */
1011 xfs_inode_t
*ip
, /* incore inode pointer */
1012 xfs_fsblock_t
*firstblock
, /* first-block-allocated */
1013 xfs_bmap_free_t
*flist
, /* blocks freed in xaction */
1014 xfs_btree_cur_t
**curp
, /* cursor returned to caller */
1015 int wasdel
, /* converting a delayed alloc */
1016 int *logflagsp
, /* inode logging flags */
1017 int whichfork
) /* data or attr fork */
1019 struct xfs_btree_block
*ablock
; /* allocated (child) bt block */
1020 xfs_buf_t
*abp
; /* buffer for ablock */
1021 xfs_alloc_arg_t args
; /* allocation arguments */
1022 xfs_bmbt_rec_t
*arp
; /* child record pointer */
1023 struct xfs_btree_block
*block
; /* btree root block */
1024 xfs_btree_cur_t
*cur
; /* bmap btree cursor */
1025 xfs_bmbt_rec_host_t
*ep
; /* extent record pointer */
1026 int error
; /* error return value */
1027 xfs_extnum_t i
, cnt
; /* extent record index */
1028 xfs_ifork_t
*ifp
; /* inode fork pointer */
1029 xfs_bmbt_key_t
*kp
; /* root block key pointer */
1030 xfs_mount_t
*mp
; /* mount structure */
1031 xfs_extnum_t nextents
; /* number of file extents */
1032 xfs_bmbt_ptr_t
*pp
; /* root block address pointer */
1034 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1035 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
);
1038 * Make space in the inode incore.
1040 xfs_iroot_realloc(ip
, 1, whichfork
);
1041 ifp
->if_flags
|= XFS_IFBROOT
;
1046 block
= ifp
->if_broot
;
1047 block
->bb_magic
= cpu_to_be32(XFS_BMAP_MAGIC
);
1048 block
->bb_level
= cpu_to_be16(1);
1049 block
->bb_numrecs
= cpu_to_be16(1);
1050 block
->bb_u
.l
.bb_leftsib
= cpu_to_be64(NULLDFSBNO
);
1051 block
->bb_u
.l
.bb_rightsib
= cpu_to_be64(NULLDFSBNO
);
1054 * Need a cursor. Can't allocate until bb_level is filled in.
1057 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
1058 cur
->bc_private
.b
.firstblock
= *firstblock
;
1059 cur
->bc_private
.b
.flist
= flist
;
1060 cur
->bc_private
.b
.flags
= wasdel
? XFS_BTCUR_BPRV_WASDEL
: 0;
1062 * Convert to a btree with two levels, one record in root.
1064 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_BTREE
);
1065 memset(&args
, 0, sizeof(args
));
1068 args
.firstblock
= *firstblock
;
1069 if (*firstblock
== NULLFSBLOCK
) {
1070 args
.type
= XFS_ALLOCTYPE_START_BNO
;
1071 args
.fsbno
= XFS_INO_TO_FSB(mp
, ip
->i_ino
);
1072 } else if (flist
->xbf_low
) {
1073 args
.type
= XFS_ALLOCTYPE_START_BNO
;
1074 args
.fsbno
= *firstblock
;
1076 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
1077 args
.fsbno
= *firstblock
;
1079 args
.minlen
= args
.maxlen
= args
.prod
= 1;
1080 args
.wasdel
= wasdel
;
1082 if ((error
= xfs_alloc_vextent(&args
))) {
1083 xfs_iroot_realloc(ip
, -1, whichfork
);
1084 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
1088 * Allocation can't fail, the space was reserved.
1090 ASSERT(args
.fsbno
!= NULLFSBLOCK
);
1091 ASSERT(*firstblock
== NULLFSBLOCK
||
1092 args
.agno
== XFS_FSB_TO_AGNO(mp
, *firstblock
) ||
1094 args
.agno
> XFS_FSB_TO_AGNO(mp
, *firstblock
)));
1095 *firstblock
= cur
->bc_private
.b
.firstblock
= args
.fsbno
;
1096 cur
->bc_private
.b
.allocated
++;
1097 ip
->i_d
.di_nblocks
++;
1098 xfs_trans_mod_dquot_byino(tp
, ip
, XFS_TRANS_DQ_BCOUNT
, 1L);
1099 abp
= xfs_btree_get_bufl(mp
, tp
, args
.fsbno
, 0);
1101 * Fill in the child block.
1103 abp
->b_ops
= &xfs_bmbt_buf_ops
;
1104 ablock
= XFS_BUF_TO_BLOCK(abp
);
1105 ablock
->bb_magic
= cpu_to_be32(XFS_BMAP_MAGIC
);
1106 ablock
->bb_level
= 0;
1107 ablock
->bb_u
.l
.bb_leftsib
= cpu_to_be64(NULLDFSBNO
);
1108 ablock
->bb_u
.l
.bb_rightsib
= cpu_to_be64(NULLDFSBNO
);
1109 arp
= XFS_BMBT_REC_ADDR(mp
, ablock
, 1);
1110 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
1111 for (cnt
= i
= 0; i
< nextents
; i
++) {
1112 ep
= xfs_iext_get_ext(ifp
, i
);
1113 if (!isnullstartblock(xfs_bmbt_get_startblock(ep
))) {
1114 arp
->l0
= cpu_to_be64(ep
->l0
);
1115 arp
->l1
= cpu_to_be64(ep
->l1
);
1119 ASSERT(cnt
== XFS_IFORK_NEXTENTS(ip
, whichfork
));
1120 xfs_btree_set_numrecs(ablock
, cnt
);
1123 * Fill in the root key and pointer.
1125 kp
= XFS_BMBT_KEY_ADDR(mp
, block
, 1);
1126 arp
= XFS_BMBT_REC_ADDR(mp
, ablock
, 1);
1127 kp
->br_startoff
= cpu_to_be64(xfs_bmbt_disk_get_startoff(arp
));
1128 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, xfs_bmbt_get_maxrecs(cur
,
1129 be16_to_cpu(block
->bb_level
)));
1130 *pp
= cpu_to_be64(args
.fsbno
);
1133 * Do all this logging at the end so that
1134 * the root is at the right level.
1136 xfs_btree_log_block(cur
, abp
, XFS_BB_ALL_BITS
);
1137 xfs_btree_log_recs(cur
, abp
, 1, be16_to_cpu(ablock
->bb_numrecs
));
1138 ASSERT(*curp
== NULL
);
1140 *logflagsp
= XFS_ILOG_CORE
| xfs_ilog_fbroot(whichfork
);
1145 * Convert a local file to an extents file.
1146 * This code is out of bounds for data forks of regular files,
1147 * since the file data needs to get logged so things will stay consistent.
1148 * (The bmap-level manipulations are ok, though).
1150 STATIC
int /* error */
1151 xfs_bmap_local_to_extents(
1152 xfs_trans_t
*tp
, /* transaction pointer */
1153 xfs_inode_t
*ip
, /* incore inode pointer */
1154 xfs_fsblock_t
*firstblock
, /* first block allocated in xaction */
1155 xfs_extlen_t total
, /* total blocks needed by transaction */
1156 int *logflagsp
, /* inode logging flags */
1158 void (*init_fn
)(struct xfs_buf
*bp
,
1159 struct xfs_inode
*ip
,
1160 struct xfs_ifork
*ifp
))
1162 int error
; /* error return value */
1163 int flags
; /* logging flags returned */
1164 xfs_ifork_t
*ifp
; /* inode fork pointer */
1167 * We don't want to deal with the case of keeping inode data inline yet.
1168 * So sending the data fork of a regular inode is invalid.
1170 ASSERT(!(S_ISREG(ip
->i_d
.di_mode
) && whichfork
== XFS_DATA_FORK
));
1171 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1172 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
1175 if (ifp
->if_bytes
) {
1176 xfs_alloc_arg_t args
; /* allocation arguments */
1177 xfs_buf_t
*bp
; /* buffer for extent block */
1178 xfs_bmbt_rec_host_t
*ep
;/* extent record pointer */
1180 ASSERT((ifp
->if_flags
&
1181 (XFS_IFINLINE
|XFS_IFEXTENTS
|XFS_IFEXTIREC
)) == XFS_IFINLINE
);
1182 memset(&args
, 0, sizeof(args
));
1184 args
.mp
= ip
->i_mount
;
1185 args
.firstblock
= *firstblock
;
1187 * Allocate a block. We know we need only one, since the
1188 * file currently fits in an inode.
1190 if (*firstblock
== NULLFSBLOCK
) {
1191 args
.fsbno
= XFS_INO_TO_FSB(args
.mp
, ip
->i_ino
);
1192 args
.type
= XFS_ALLOCTYPE_START_BNO
;
1194 args
.fsbno
= *firstblock
;
1195 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
1198 args
.minlen
= args
.maxlen
= args
.prod
= 1;
1199 error
= xfs_alloc_vextent(&args
);
1203 /* Can't fail, the space was reserved. */
1204 ASSERT(args
.fsbno
!= NULLFSBLOCK
);
1205 ASSERT(args
.len
== 1);
1206 *firstblock
= args
.fsbno
;
1207 bp
= xfs_btree_get_bufl(args
.mp
, tp
, args
.fsbno
, 0);
1209 /* initialise the block and copy the data */
1210 init_fn(bp
, ip
, ifp
);
1212 /* account for the change in fork size and log everything */
1213 xfs_trans_log_buf(tp
, bp
, 0, ifp
->if_bytes
- 1);
1214 xfs_bmap_forkoff_reset(args
.mp
, ip
, whichfork
);
1215 xfs_idata_realloc(ip
, -ifp
->if_bytes
, whichfork
);
1216 xfs_iext_add(ifp
, 0, 1);
1217 ep
= xfs_iext_get_ext(ifp
, 0);
1218 xfs_bmbt_set_allf(ep
, 0, args
.fsbno
, 1, XFS_EXT_NORM
);
1219 trace_xfs_bmap_post_update(ip
, 0,
1220 whichfork
== XFS_ATTR_FORK
? BMAP_ATTRFORK
: 0,
1222 XFS_IFORK_NEXT_SET(ip
, whichfork
, 1);
1223 ip
->i_d
.di_nblocks
= 1;
1224 xfs_trans_mod_dquot_byino(tp
, ip
,
1225 XFS_TRANS_DQ_BCOUNT
, 1L);
1226 flags
|= xfs_ilog_fext(whichfork
);
1228 ASSERT(XFS_IFORK_NEXTENTS(ip
, whichfork
) == 0);
1229 xfs_bmap_forkoff_reset(ip
->i_mount
, ip
, whichfork
);
1231 ifp
->if_flags
&= ~XFS_IFINLINE
;
1232 ifp
->if_flags
|= XFS_IFEXTENTS
;
1233 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_EXTENTS
);
1234 flags
|= XFS_ILOG_CORE
;
1241 * Called from xfs_bmap_add_attrfork to handle btree format files.
1243 STATIC
int /* error */
1244 xfs_bmap_add_attrfork_btree(
1245 xfs_trans_t
*tp
, /* transaction pointer */
1246 xfs_inode_t
*ip
, /* incore inode pointer */
1247 xfs_fsblock_t
*firstblock
, /* first block allocated */
1248 xfs_bmap_free_t
*flist
, /* blocks to free at commit */
1249 int *flags
) /* inode logging flags */
1251 xfs_btree_cur_t
*cur
; /* btree cursor */
1252 int error
; /* error return value */
1253 xfs_mount_t
*mp
; /* file system mount struct */
1254 int stat
; /* newroot status */
1257 if (ip
->i_df
.if_broot_bytes
<= XFS_IFORK_DSIZE(ip
))
1258 *flags
|= XFS_ILOG_DBROOT
;
1260 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, XFS_DATA_FORK
);
1261 cur
->bc_private
.b
.flist
= flist
;
1262 cur
->bc_private
.b
.firstblock
= *firstblock
;
1263 if ((error
= xfs_bmbt_lookup_ge(cur
, 0, 0, 0, &stat
)))
1265 /* must be at least one entry */
1266 XFS_WANT_CORRUPTED_GOTO(stat
== 1, error0
);
1267 if ((error
= xfs_btree_new_iroot(cur
, flags
, &stat
)))
1270 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
1271 return XFS_ERROR(ENOSPC
);
1273 *firstblock
= cur
->bc_private
.b
.firstblock
;
1274 cur
->bc_private
.b
.allocated
= 0;
1275 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
1279 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
1284 * Called from xfs_bmap_add_attrfork to handle extents format files.
1286 STATIC
int /* error */
1287 xfs_bmap_add_attrfork_extents(
1288 xfs_trans_t
*tp
, /* transaction pointer */
1289 xfs_inode_t
*ip
, /* incore inode pointer */
1290 xfs_fsblock_t
*firstblock
, /* first block allocated */
1291 xfs_bmap_free_t
*flist
, /* blocks to free at commit */
1292 int *flags
) /* inode logging flags */
1294 xfs_btree_cur_t
*cur
; /* bmap btree cursor */
1295 int error
; /* error return value */
1297 if (ip
->i_d
.di_nextents
* sizeof(xfs_bmbt_rec_t
) <= XFS_IFORK_DSIZE(ip
))
1300 error
= xfs_bmap_extents_to_btree(tp
, ip
, firstblock
, flist
, &cur
, 0,
1301 flags
, XFS_DATA_FORK
);
1303 cur
->bc_private
.b
.allocated
= 0;
1304 xfs_btree_del_cursor(cur
,
1305 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
1311 * Block initialisation functions for local to extent format conversion.
1312 * As these get more complex, they will be moved to the relevant files,
1313 * but for now they are too simple to worry about.
1316 xfs_bmap_local_to_extents_init_fn(
1318 struct xfs_inode
*ip
,
1319 struct xfs_ifork
*ifp
)
1321 bp
->b_ops
= &xfs_bmbt_buf_ops
;
1322 memcpy(bp
->b_addr
, ifp
->if_u1
.if_data
, ifp
->if_bytes
);
1326 xfs_symlink_local_to_remote(
1328 struct xfs_inode
*ip
,
1329 struct xfs_ifork
*ifp
)
1331 /* remote symlink blocks are not verifiable until CRCs come along */
1333 memcpy(bp
->b_addr
, ifp
->if_u1
.if_data
, ifp
->if_bytes
);
1337 * Called from xfs_bmap_add_attrfork to handle local format files. Each
1338 * different data fork content type needs a different callout to do the
1339 * conversion. Some are basic and only require special block initialisation
1340 * callouts for the data formating, others (directories) are so specialised they
1341 * handle everything themselves.
1343 * XXX (dgc): investigate whether directory conversion can use the generic
1344 * formatting callout. It should be possible - it's just a very complex
1345 * formatter. it would also require passing the transaction through to the init
1348 STATIC
int /* error */
1349 xfs_bmap_add_attrfork_local(
1350 xfs_trans_t
*tp
, /* transaction pointer */
1351 xfs_inode_t
*ip
, /* incore inode pointer */
1352 xfs_fsblock_t
*firstblock
, /* first block allocated */
1353 xfs_bmap_free_t
*flist
, /* blocks to free at commit */
1354 int *flags
) /* inode logging flags */
1356 xfs_da_args_t dargs
; /* args for dir/attr code */
1358 if (ip
->i_df
.if_bytes
<= XFS_IFORK_DSIZE(ip
))
1361 if (S_ISDIR(ip
->i_d
.di_mode
)) {
1362 memset(&dargs
, 0, sizeof(dargs
));
1364 dargs
.firstblock
= firstblock
;
1365 dargs
.flist
= flist
;
1366 dargs
.total
= ip
->i_mount
->m_dirblkfsbs
;
1367 dargs
.whichfork
= XFS_DATA_FORK
;
1369 return xfs_dir2_sf_to_block(&dargs
);
1372 if (S_ISLNK(ip
->i_d
.di_mode
))
1373 return xfs_bmap_local_to_extents(tp
, ip
, firstblock
, 1,
1374 flags
, XFS_DATA_FORK
,
1375 xfs_symlink_local_to_remote
);
1377 return xfs_bmap_local_to_extents(tp
, ip
, firstblock
, 1, flags
,
1379 xfs_bmap_local_to_extents_init_fn
);
1383 * Convert inode from non-attributed to attributed.
1384 * Must not be in a transaction, ip must not be locked.
1386 int /* error code */
1387 xfs_bmap_add_attrfork(
1388 xfs_inode_t
*ip
, /* incore inode pointer */
1389 int size
, /* space new attribute needs */
1390 int rsvd
) /* xact may use reserved blks */
1392 xfs_fsblock_t firstblock
; /* 1st block/ag allocated */
1393 xfs_bmap_free_t flist
; /* freed extent records */
1394 xfs_mount_t
*mp
; /* mount structure */
1395 xfs_trans_t
*tp
; /* transaction pointer */
1396 int blks
; /* space reservation */
1397 int version
= 1; /* superblock attr version */
1398 int committed
; /* xaction was committed */
1399 int logflags
; /* logging flags */
1400 int error
; /* error return value */
1402 ASSERT(XFS_IFORK_Q(ip
) == 0);
1405 ASSERT(!XFS_NOT_DQATTACHED(mp
, ip
));
1406 tp
= xfs_trans_alloc(mp
, XFS_TRANS_ADDAFORK
);
1407 blks
= XFS_ADDAFORK_SPACE_RES(mp
);
1409 tp
->t_flags
|= XFS_TRANS_RESERVE
;
1410 if ((error
= xfs_trans_reserve(tp
, blks
, XFS_ADDAFORK_LOG_RES(mp
), 0,
1411 XFS_TRANS_PERM_LOG_RES
, XFS_ADDAFORK_LOG_COUNT
)))
1413 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
1414 error
= xfs_trans_reserve_quota_nblks(tp
, ip
, blks
, 0, rsvd
?
1415 XFS_QMOPT_RES_REGBLKS
| XFS_QMOPT_FORCE_RES
:
1416 XFS_QMOPT_RES_REGBLKS
);
1418 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
1419 xfs_trans_cancel(tp
, XFS_TRANS_RELEASE_LOG_RES
);
1422 if (XFS_IFORK_Q(ip
))
1424 if (ip
->i_d
.di_aformat
!= XFS_DINODE_FMT_EXTENTS
) {
1426 * For inodes coming from pre-6.2 filesystems.
1428 ASSERT(ip
->i_d
.di_aformat
== 0);
1429 ip
->i_d
.di_aformat
= XFS_DINODE_FMT_EXTENTS
;
1431 ASSERT(ip
->i_d
.di_anextents
== 0);
1433 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
);
1434 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
1436 switch (ip
->i_d
.di_format
) {
1437 case XFS_DINODE_FMT_DEV
:
1438 ip
->i_d
.di_forkoff
= roundup(sizeof(xfs_dev_t
), 8) >> 3;
1440 case XFS_DINODE_FMT_UUID
:
1441 ip
->i_d
.di_forkoff
= roundup(sizeof(uuid_t
), 8) >> 3;
1443 case XFS_DINODE_FMT_LOCAL
:
1444 case XFS_DINODE_FMT_EXTENTS
:
1445 case XFS_DINODE_FMT_BTREE
:
1446 ip
->i_d
.di_forkoff
= xfs_attr_shortform_bytesfit(ip
, size
);
1447 if (!ip
->i_d
.di_forkoff
)
1448 ip
->i_d
.di_forkoff
= xfs_default_attroffset(ip
) >> 3;
1449 else if (mp
->m_flags
& XFS_MOUNT_ATTR2
)
1454 error
= XFS_ERROR(EINVAL
);
1458 ASSERT(ip
->i_afp
== NULL
);
1459 ip
->i_afp
= kmem_zone_zalloc(xfs_ifork_zone
, KM_SLEEP
);
1460 ip
->i_afp
->if_flags
= XFS_IFEXTENTS
;
1462 xfs_bmap_init(&flist
, &firstblock
);
1463 switch (ip
->i_d
.di_format
) {
1464 case XFS_DINODE_FMT_LOCAL
:
1465 error
= xfs_bmap_add_attrfork_local(tp
, ip
, &firstblock
, &flist
,
1468 case XFS_DINODE_FMT_EXTENTS
:
1469 error
= xfs_bmap_add_attrfork_extents(tp
, ip
, &firstblock
,
1472 case XFS_DINODE_FMT_BTREE
:
1473 error
= xfs_bmap_add_attrfork_btree(tp
, ip
, &firstblock
, &flist
,
1481 xfs_trans_log_inode(tp
, ip
, logflags
);
1484 if (!xfs_sb_version_hasattr(&mp
->m_sb
) ||
1485 (!xfs_sb_version_hasattr2(&mp
->m_sb
) && version
== 2)) {
1486 __int64_t sbfields
= 0;
1488 spin_lock(&mp
->m_sb_lock
);
1489 if (!xfs_sb_version_hasattr(&mp
->m_sb
)) {
1490 xfs_sb_version_addattr(&mp
->m_sb
);
1491 sbfields
|= XFS_SB_VERSIONNUM
;
1493 if (!xfs_sb_version_hasattr2(&mp
->m_sb
) && version
== 2) {
1494 xfs_sb_version_addattr2(&mp
->m_sb
);
1495 sbfields
|= (XFS_SB_VERSIONNUM
| XFS_SB_FEATURES2
);
1498 spin_unlock(&mp
->m_sb_lock
);
1499 xfs_mod_sb(tp
, sbfields
);
1501 spin_unlock(&mp
->m_sb_lock
);
1504 error
= xfs_bmap_finish(&tp
, &flist
, &committed
);
1507 return xfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
);
1509 xfs_bmap_cancel(&flist
);
1511 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
1513 xfs_trans_cancel(tp
, XFS_TRANS_RELEASE_LOG_RES
|XFS_TRANS_ABORT
);
1518 * Internal and external extent tree search functions.
1522 * Read in the extents to if_extents.
1523 * All inode fields are set up by caller, we just traverse the btree
1524 * and copy the records in. If the file system cannot contain unwritten
1525 * extents, the records are checked for no "state" flags.
1528 xfs_bmap_read_extents(
1529 xfs_trans_t
*tp
, /* transaction pointer */
1530 xfs_inode_t
*ip
, /* incore inode */
1531 int whichfork
) /* data or attr fork */
1533 struct xfs_btree_block
*block
; /* current btree block */
1534 xfs_fsblock_t bno
; /* block # of "block" */
1535 xfs_buf_t
*bp
; /* buffer for "block" */
1536 int error
; /* error return value */
1537 xfs_exntfmt_t exntf
; /* XFS_EXTFMT_NOSTATE, if checking */
1538 xfs_extnum_t i
, j
; /* index into the extents list */
1539 xfs_ifork_t
*ifp
; /* fork structure */
1540 int level
; /* btree level, for checking */
1541 xfs_mount_t
*mp
; /* file system mount structure */
1542 __be64
*pp
; /* pointer to block address */
1544 xfs_extnum_t room
; /* number of entries there's room for */
1548 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1549 exntf
= (whichfork
!= XFS_DATA_FORK
) ? XFS_EXTFMT_NOSTATE
:
1550 XFS_EXTFMT_INODE(ip
);
1551 block
= ifp
->if_broot
;
1553 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
1555 level
= be16_to_cpu(block
->bb_level
);
1557 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, 1, ifp
->if_broot_bytes
);
1558 bno
= be64_to_cpu(*pp
);
1559 ASSERT(bno
!= NULLDFSBNO
);
1560 ASSERT(XFS_FSB_TO_AGNO(mp
, bno
) < mp
->m_sb
.sb_agcount
);
1561 ASSERT(XFS_FSB_TO_AGBNO(mp
, bno
) < mp
->m_sb
.sb_agblocks
);
1563 * Go down the tree until leaf level is reached, following the first
1564 * pointer (leftmost) at each level.
1566 while (level
-- > 0) {
1567 error
= xfs_btree_read_bufl(mp
, tp
, bno
, 0, &bp
,
1568 XFS_BMAP_BTREE_REF
, &xfs_bmbt_buf_ops
);
1571 block
= XFS_BUF_TO_BLOCK(bp
);
1572 XFS_WANT_CORRUPTED_GOTO(
1573 xfs_bmap_sanity_check(mp
, bp
, level
),
1577 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, mp
->m_bmap_dmxr
[1]);
1578 bno
= be64_to_cpu(*pp
);
1579 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp
, bno
), error0
);
1580 xfs_trans_brelse(tp
, bp
);
1583 * Here with bp and block set to the leftmost leaf node in the tree.
1585 room
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
1588 * Loop over all leaf nodes. Copy information to the extent records.
1591 xfs_bmbt_rec_t
*frp
;
1592 xfs_fsblock_t nextbno
;
1593 xfs_extnum_t num_recs
;
1596 num_recs
= xfs_btree_get_numrecs(block
);
1597 if (unlikely(i
+ num_recs
> room
)) {
1598 ASSERT(i
+ num_recs
<= room
);
1599 xfs_warn(ip
->i_mount
,
1600 "corrupt dinode %Lu, (btree extents).",
1601 (unsigned long long) ip
->i_ino
);
1602 XFS_CORRUPTION_ERROR("xfs_bmap_read_extents(1)",
1603 XFS_ERRLEVEL_LOW
, ip
->i_mount
, block
);
1606 XFS_WANT_CORRUPTED_GOTO(
1607 xfs_bmap_sanity_check(mp
, bp
, 0),
1610 * Read-ahead the next leaf block, if any.
1612 nextbno
= be64_to_cpu(block
->bb_u
.l
.bb_rightsib
);
1613 if (nextbno
!= NULLFSBLOCK
)
1614 xfs_btree_reada_bufl(mp
, nextbno
, 1,
1617 * Copy records into the extent records.
1619 frp
= XFS_BMBT_REC_ADDR(mp
, block
, 1);
1621 for (j
= 0; j
< num_recs
; j
++, i
++, frp
++) {
1622 xfs_bmbt_rec_host_t
*trp
= xfs_iext_get_ext(ifp
, i
);
1623 trp
->l0
= be64_to_cpu(frp
->l0
);
1624 trp
->l1
= be64_to_cpu(frp
->l1
);
1626 if (exntf
== XFS_EXTFMT_NOSTATE
) {
1628 * Check all attribute bmap btree records and
1629 * any "older" data bmap btree records for a
1630 * set bit in the "extent flag" position.
1632 if (unlikely(xfs_check_nostate_extents(ifp
,
1633 start
, num_recs
))) {
1634 XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
1640 xfs_trans_brelse(tp
, bp
);
1643 * If we've reached the end, stop.
1645 if (bno
== NULLFSBLOCK
)
1647 error
= xfs_btree_read_bufl(mp
, tp
, bno
, 0, &bp
,
1648 XFS_BMAP_BTREE_REF
, &xfs_bmbt_buf_ops
);
1651 block
= XFS_BUF_TO_BLOCK(bp
);
1653 ASSERT(i
== (ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
)));
1654 ASSERT(i
== XFS_IFORK_NEXTENTS(ip
, whichfork
));
1655 XFS_BMAP_TRACE_EXLIST(ip
, i
, whichfork
);
1658 xfs_trans_brelse(tp
, bp
);
1659 return XFS_ERROR(EFSCORRUPTED
);
1664 * Search the extent records for the entry containing block bno.
1665 * If bno lies in a hole, point to the next entry. If bno lies
1666 * past eof, *eofp will be set, and *prevp will contain the last
1667 * entry (null if none). Else, *lastxp will be set to the index
1668 * of the found entry; *gotp will contain the entry.
1670 STATIC xfs_bmbt_rec_host_t
* /* pointer to found extent entry */
1671 xfs_bmap_search_multi_extents(
1672 xfs_ifork_t
*ifp
, /* inode fork pointer */
1673 xfs_fileoff_t bno
, /* block number searched for */
1674 int *eofp
, /* out: end of file found */
1675 xfs_extnum_t
*lastxp
, /* out: last extent index */
1676 xfs_bmbt_irec_t
*gotp
, /* out: extent entry found */
1677 xfs_bmbt_irec_t
*prevp
) /* out: previous extent entry found */
1679 xfs_bmbt_rec_host_t
*ep
; /* extent record pointer */
1680 xfs_extnum_t lastx
; /* last extent index */
1683 * Initialize the extent entry structure to catch access to
1684 * uninitialized br_startblock field.
1686 gotp
->br_startoff
= 0xffa5a5a5a5a5a5a5LL
;
1687 gotp
->br_blockcount
= 0xa55a5a5a5a5a5a5aLL
;
1688 gotp
->br_state
= XFS_EXT_INVALID
;
1690 gotp
->br_startblock
= 0xffffa5a5a5a5a5a5LL
;
1692 gotp
->br_startblock
= 0xffffa5a5;
1694 prevp
->br_startoff
= NULLFILEOFF
;
1696 ep
= xfs_iext_bno_to_ext(ifp
, bno
, &lastx
);
1698 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, lastx
- 1), prevp
);
1700 if (lastx
< (ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
))) {
1701 xfs_bmbt_get_all(ep
, gotp
);
1715 * Search the extents list for the inode, for the extent containing bno.
1716 * If bno lies in a hole, point to the next entry. If bno lies past eof,
1717 * *eofp will be set, and *prevp will contain the last entry (null if none).
1718 * Else, *lastxp will be set to the index of the found
1719 * entry; *gotp will contain the entry.
1721 STATIC xfs_bmbt_rec_host_t
* /* pointer to found extent entry */
1722 xfs_bmap_search_extents(
1723 xfs_inode_t
*ip
, /* incore inode pointer */
1724 xfs_fileoff_t bno
, /* block number searched for */
1725 int fork
, /* data or attr fork */
1726 int *eofp
, /* out: end of file found */
1727 xfs_extnum_t
*lastxp
, /* out: last extent index */
1728 xfs_bmbt_irec_t
*gotp
, /* out: extent entry found */
1729 xfs_bmbt_irec_t
*prevp
) /* out: previous extent entry found */
1731 xfs_ifork_t
*ifp
; /* inode fork pointer */
1732 xfs_bmbt_rec_host_t
*ep
; /* extent record pointer */
1734 XFS_STATS_INC(xs_look_exlist
);
1735 ifp
= XFS_IFORK_PTR(ip
, fork
);
1737 ep
= xfs_bmap_search_multi_extents(ifp
, bno
, eofp
, lastxp
, gotp
, prevp
);
1739 if (unlikely(!(gotp
->br_startblock
) && (*lastxp
!= NULLEXTNUM
) &&
1740 !(XFS_IS_REALTIME_INODE(ip
) && fork
== XFS_DATA_FORK
))) {
1741 xfs_alert_tag(ip
->i_mount
, XFS_PTAG_FSBLOCK_ZERO
,
1742 "Access to block zero in inode %llu "
1743 "start_block: %llx start_off: %llx "
1744 "blkcnt: %llx extent-state: %x lastx: %x\n",
1745 (unsigned long long)ip
->i_ino
,
1746 (unsigned long long)gotp
->br_startblock
,
1747 (unsigned long long)gotp
->br_startoff
,
1748 (unsigned long long)gotp
->br_blockcount
,
1749 gotp
->br_state
, *lastxp
);
1750 *lastxp
= NULLEXTNUM
;
1758 * Returns the file-relative block number of the first unused block(s)
1759 * in the file with at least "len" logically contiguous blocks free.
1760 * This is the lowest-address hole if the file has holes, else the first block
1761 * past the end of file.
1762 * Return 0 if the file is currently local (in-inode).
1765 xfs_bmap_first_unused(
1766 xfs_trans_t
*tp
, /* transaction pointer */
1767 xfs_inode_t
*ip
, /* incore inode */
1768 xfs_extlen_t len
, /* size of hole to find */
1769 xfs_fileoff_t
*first_unused
, /* unused block */
1770 int whichfork
) /* data or attr fork */
1772 int error
; /* error return value */
1773 int idx
; /* extent record index */
1774 xfs_ifork_t
*ifp
; /* inode fork pointer */
1775 xfs_fileoff_t lastaddr
; /* last block number seen */
1776 xfs_fileoff_t lowest
; /* lowest useful block */
1777 xfs_fileoff_t max
; /* starting useful block */
1778 xfs_fileoff_t off
; /* offset for this block */
1779 xfs_extnum_t nextents
; /* number of extent entries */
1781 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
||
1782 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
||
1783 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
1784 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
) {
1788 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1789 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
1790 (error
= xfs_iread_extents(tp
, ip
, whichfork
)))
1792 lowest
= *first_unused
;
1793 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
1794 for (idx
= 0, lastaddr
= 0, max
= lowest
; idx
< nextents
; idx
++) {
1795 xfs_bmbt_rec_host_t
*ep
= xfs_iext_get_ext(ifp
, idx
);
1796 off
= xfs_bmbt_get_startoff(ep
);
1798 * See if the hole before this extent will work.
1800 if (off
>= lowest
+ len
&& off
- max
>= len
) {
1801 *first_unused
= max
;
1804 lastaddr
= off
+ xfs_bmbt_get_blockcount(ep
);
1805 max
= XFS_FILEOFF_MAX(lastaddr
, lowest
);
1807 *first_unused
= max
;
1812 * Returns the file-relative block number of the last block + 1 before
1813 * last_block (input value) in the file.
1814 * This is not based on i_size, it is based on the extent records.
1815 * Returns 0 for local files, as they do not have extent records.
1818 xfs_bmap_last_before(
1819 xfs_trans_t
*tp
, /* transaction pointer */
1820 xfs_inode_t
*ip
, /* incore inode */
1821 xfs_fileoff_t
*last_block
, /* last block */
1822 int whichfork
) /* data or attr fork */
1824 xfs_fileoff_t bno
; /* input file offset */
1825 int eof
; /* hit end of file */
1826 xfs_bmbt_rec_host_t
*ep
; /* pointer to last extent */
1827 int error
; /* error return value */
1828 xfs_bmbt_irec_t got
; /* current extent value */
1829 xfs_ifork_t
*ifp
; /* inode fork pointer */
1830 xfs_extnum_t lastx
; /* last extent used */
1831 xfs_bmbt_irec_t prev
; /* previous extent value */
1833 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
&&
1834 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
1835 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_LOCAL
)
1836 return XFS_ERROR(EIO
);
1837 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
) {
1841 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1842 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
1843 (error
= xfs_iread_extents(tp
, ip
, whichfork
)))
1845 bno
= *last_block
- 1;
1846 ep
= xfs_bmap_search_extents(ip
, bno
, whichfork
, &eof
, &lastx
, &got
,
1848 if (eof
|| xfs_bmbt_get_startoff(ep
) > bno
) {
1849 if (prev
.br_startoff
== NULLFILEOFF
)
1852 *last_block
= prev
.br_startoff
+ prev
.br_blockcount
;
1855 * Otherwise *last_block is already the right answer.
1861 xfs_bmap_last_extent(
1862 struct xfs_trans
*tp
,
1863 struct xfs_inode
*ip
,
1865 struct xfs_bmbt_irec
*rec
,
1868 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1872 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
1873 error
= xfs_iread_extents(tp
, ip
, whichfork
);
1878 nextents
= ifp
->if_bytes
/ sizeof(xfs_bmbt_rec_t
);
1879 if (nextents
== 0) {
1884 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, nextents
- 1), rec
);
1890 * Check the last inode extent to determine whether this allocation will result
1891 * in blocks being allocated at the end of the file. When we allocate new data
1892 * blocks at the end of the file which do not start at the previous data block,
1893 * we will try to align the new blocks at stripe unit boundaries.
1895 * Returns 0 in bma->aeof if the file (fork) is empty as any new write will be
1896 * at, or past the EOF.
1900 struct xfs_bmalloca
*bma
,
1903 struct xfs_bmbt_irec rec
;
1908 error
= xfs_bmap_last_extent(NULL
, bma
->ip
, whichfork
, &rec
,
1910 if (error
|| is_empty
)
1914 * Check if we are allocation or past the last extent, or at least into
1915 * the last delayed allocated extent.
1917 bma
->aeof
= bma
->offset
>= rec
.br_startoff
+ rec
.br_blockcount
||
1918 (bma
->offset
>= rec
.br_startoff
&&
1919 isnullstartblock(rec
.br_startblock
));
1924 * Check if the endoff is outside the last extent. If so the caller will grow
1925 * the allocation to a stripe unit boundary. All offsets are considered outside
1926 * the end of file for an empty fork, so 1 is returned in *eof in that case.
1930 struct xfs_inode
*ip
,
1931 xfs_fileoff_t endoff
,
1935 struct xfs_bmbt_irec rec
;
1938 error
= xfs_bmap_last_extent(NULL
, ip
, whichfork
, &rec
, eof
);
1942 *eof
= endoff
>= rec
.br_startoff
+ rec
.br_blockcount
;
1947 * Returns the file-relative block number of the first block past eof in
1948 * the file. This is not based on i_size, it is based on the extent records.
1949 * Returns 0 for local files, as they do not have extent records.
1952 xfs_bmap_last_offset(
1953 struct xfs_trans
*tp
,
1954 struct xfs_inode
*ip
,
1955 xfs_fileoff_t
*last_block
,
1958 struct xfs_bmbt_irec rec
;
1964 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
)
1967 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
&&
1968 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
1969 return XFS_ERROR(EIO
);
1971 error
= xfs_bmap_last_extent(NULL
, ip
, whichfork
, &rec
, &is_empty
);
1972 if (error
|| is_empty
)
1975 *last_block
= rec
.br_startoff
+ rec
.br_blockcount
;
1980 * Returns whether the selected fork of the inode has exactly one
1981 * block or not. For the data fork we check this matches di_size,
1982 * implying the file's range is 0..bsize-1.
1984 int /* 1=>1 block, 0=>otherwise */
1986 xfs_inode_t
*ip
, /* incore inode */
1987 int whichfork
) /* data or attr fork */
1989 xfs_bmbt_rec_host_t
*ep
; /* ptr to fork's extent */
1990 xfs_ifork_t
*ifp
; /* inode fork pointer */
1991 int rval
; /* return value */
1992 xfs_bmbt_irec_t s
; /* internal version of extent */
1995 if (whichfork
== XFS_DATA_FORK
)
1996 return XFS_ISIZE(ip
) == ip
->i_mount
->m_sb
.sb_blocksize
;
1998 if (XFS_IFORK_NEXTENTS(ip
, whichfork
) != 1)
2000 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
2002 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
2003 ASSERT(ifp
->if_flags
& XFS_IFEXTENTS
);
2004 ep
= xfs_iext_get_ext(ifp
, 0);
2005 xfs_bmbt_get_all(ep
, &s
);
2006 rval
= s
.br_startoff
== 0 && s
.br_blockcount
== 1;
2007 if (rval
&& whichfork
== XFS_DATA_FORK
)
2008 ASSERT(XFS_ISIZE(ip
) == ip
->i_mount
->m_sb
.sb_blocksize
);
2013 * Extent tree manipulation functions used during allocation.
2017 * Convert a delayed allocation to a real allocation.
2019 STATIC
int /* error */
2020 xfs_bmap_add_extent_delay_real(
2021 struct xfs_bmalloca
*bma
)
2023 struct xfs_bmbt_irec
*new = &bma
->got
;
2024 int diff
; /* temp value */
2025 xfs_bmbt_rec_host_t
*ep
; /* extent entry for idx */
2026 int error
; /* error return value */
2027 int i
; /* temp state */
2028 xfs_ifork_t
*ifp
; /* inode fork pointer */
2029 xfs_fileoff_t new_endoff
; /* end offset of new entry */
2030 xfs_bmbt_irec_t r
[3]; /* neighbor extent entries */
2031 /* left is 0, right is 1, prev is 2 */
2032 int rval
=0; /* return value (logging flags) */
2033 int state
= 0;/* state bits, accessed thru macros */
2034 xfs_filblks_t da_new
; /* new count del alloc blocks used */
2035 xfs_filblks_t da_old
; /* old count del alloc blocks used */
2036 xfs_filblks_t temp
=0; /* value for da_new calculations */
2037 xfs_filblks_t temp2
=0;/* value for da_new calculations */
2038 int tmp_rval
; /* partial logging flags */
2040 ifp
= XFS_IFORK_PTR(bma
->ip
, XFS_DATA_FORK
);
2042 ASSERT(bma
->idx
>= 0);
2043 ASSERT(bma
->idx
<= ifp
->if_bytes
/ sizeof(struct xfs_bmbt_rec
));
2044 ASSERT(!isnullstartblock(new->br_startblock
));
2046 (bma
->cur
->bc_private
.b
.flags
& XFS_BTCUR_BPRV_WASDEL
));
2048 XFS_STATS_INC(xs_add_exlist
);
2055 * Set up a bunch of variables to make the tests simpler.
2057 ep
= xfs_iext_get_ext(ifp
, bma
->idx
);
2058 xfs_bmbt_get_all(ep
, &PREV
);
2059 new_endoff
= new->br_startoff
+ new->br_blockcount
;
2060 ASSERT(PREV
.br_startoff
<= new->br_startoff
);
2061 ASSERT(PREV
.br_startoff
+ PREV
.br_blockcount
>= new_endoff
);
2063 da_old
= startblockval(PREV
.br_startblock
);
2067 * Set flags determining what part of the previous delayed allocation
2068 * extent is being replaced by a real allocation.
2070 if (PREV
.br_startoff
== new->br_startoff
)
2071 state
|= BMAP_LEFT_FILLING
;
2072 if (PREV
.br_startoff
+ PREV
.br_blockcount
== new_endoff
)
2073 state
|= BMAP_RIGHT_FILLING
;
2076 * Check and set flags if this segment has a left neighbor.
2077 * Don't set contiguous if the combined extent would be too large.
2080 state
|= BMAP_LEFT_VALID
;
2081 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
- 1), &LEFT
);
2083 if (isnullstartblock(LEFT
.br_startblock
))
2084 state
|= BMAP_LEFT_DELAY
;
2087 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
2088 LEFT
.br_startoff
+ LEFT
.br_blockcount
== new->br_startoff
&&
2089 LEFT
.br_startblock
+ LEFT
.br_blockcount
== new->br_startblock
&&
2090 LEFT
.br_state
== new->br_state
&&
2091 LEFT
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
2092 state
|= BMAP_LEFT_CONTIG
;
2095 * Check and set flags if this segment has a right neighbor.
2096 * Don't set contiguous if the combined extent would be too large.
2097 * Also check for all-three-contiguous being too large.
2099 if (bma
->idx
< bma
->ip
->i_df
.if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
) - 1) {
2100 state
|= BMAP_RIGHT_VALID
;
2101 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
+ 1), &RIGHT
);
2103 if (isnullstartblock(RIGHT
.br_startblock
))
2104 state
|= BMAP_RIGHT_DELAY
;
2107 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
2108 new_endoff
== RIGHT
.br_startoff
&&
2109 new->br_startblock
+ new->br_blockcount
== RIGHT
.br_startblock
&&
2110 new->br_state
== RIGHT
.br_state
&&
2111 new->br_blockcount
+ RIGHT
.br_blockcount
<= MAXEXTLEN
&&
2112 ((state
& (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
2113 BMAP_RIGHT_FILLING
)) !=
2114 (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
2115 BMAP_RIGHT_FILLING
) ||
2116 LEFT
.br_blockcount
+ new->br_blockcount
+ RIGHT
.br_blockcount
2118 state
|= BMAP_RIGHT_CONTIG
;
2122 * Switch out based on the FILLING and CONTIG state bits.
2124 switch (state
& (BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
2125 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
)) {
2126 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
2127 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2129 * Filling in all of a previously delayed allocation extent.
2130 * The left and right neighbors are both contiguous with new.
2133 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2134 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, bma
->idx
),
2135 LEFT
.br_blockcount
+ PREV
.br_blockcount
+
2136 RIGHT
.br_blockcount
);
2137 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2139 xfs_iext_remove(bma
->ip
, bma
->idx
+ 1, 2, state
);
2140 bma
->ip
->i_d
.di_nextents
--;
2141 if (bma
->cur
== NULL
)
2142 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2144 rval
= XFS_ILOG_CORE
;
2145 error
= xfs_bmbt_lookup_eq(bma
->cur
, RIGHT
.br_startoff
,
2146 RIGHT
.br_startblock
,
2147 RIGHT
.br_blockcount
, &i
);
2150 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2151 error
= xfs_btree_delete(bma
->cur
, &i
);
2154 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2155 error
= xfs_btree_decrement(bma
->cur
, 0, &i
);
2158 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2159 error
= xfs_bmbt_update(bma
->cur
, LEFT
.br_startoff
,
2161 LEFT
.br_blockcount
+
2162 PREV
.br_blockcount
+
2163 RIGHT
.br_blockcount
, LEFT
.br_state
);
2169 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
2171 * Filling in all of a previously delayed allocation extent.
2172 * The left neighbor is contiguous, the right is not.
2176 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2177 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, bma
->idx
),
2178 LEFT
.br_blockcount
+ PREV
.br_blockcount
);
2179 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2181 xfs_iext_remove(bma
->ip
, bma
->idx
+ 1, 1, state
);
2182 if (bma
->cur
== NULL
)
2183 rval
= XFS_ILOG_DEXT
;
2186 error
= xfs_bmbt_lookup_eq(bma
->cur
, LEFT
.br_startoff
,
2187 LEFT
.br_startblock
, LEFT
.br_blockcount
,
2191 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2192 error
= xfs_bmbt_update(bma
->cur
, LEFT
.br_startoff
,
2194 LEFT
.br_blockcount
+
2195 PREV
.br_blockcount
, LEFT
.br_state
);
2201 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2203 * Filling in all of a previously delayed allocation extent.
2204 * The right neighbor is contiguous, the left is not.
2206 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2207 xfs_bmbt_set_startblock(ep
, new->br_startblock
);
2208 xfs_bmbt_set_blockcount(ep
,
2209 PREV
.br_blockcount
+ RIGHT
.br_blockcount
);
2210 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2212 xfs_iext_remove(bma
->ip
, bma
->idx
+ 1, 1, state
);
2213 if (bma
->cur
== NULL
)
2214 rval
= XFS_ILOG_DEXT
;
2217 error
= xfs_bmbt_lookup_eq(bma
->cur
, RIGHT
.br_startoff
,
2218 RIGHT
.br_startblock
,
2219 RIGHT
.br_blockcount
, &i
);
2222 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2223 error
= xfs_bmbt_update(bma
->cur
, PREV
.br_startoff
,
2225 PREV
.br_blockcount
+
2226 RIGHT
.br_blockcount
, PREV
.br_state
);
2232 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
2234 * Filling in all of a previously delayed allocation extent.
2235 * Neither the left nor right neighbors are contiguous with
2238 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2239 xfs_bmbt_set_startblock(ep
, new->br_startblock
);
2240 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2242 bma
->ip
->i_d
.di_nextents
++;
2243 if (bma
->cur
== NULL
)
2244 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2246 rval
= XFS_ILOG_CORE
;
2247 error
= xfs_bmbt_lookup_eq(bma
->cur
, new->br_startoff
,
2248 new->br_startblock
, new->br_blockcount
,
2252 XFS_WANT_CORRUPTED_GOTO(i
== 0, done
);
2253 bma
->cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
2254 error
= xfs_btree_insert(bma
->cur
, &i
);
2257 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2261 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
:
2263 * Filling in the first part of a previous delayed allocation.
2264 * The left neighbor is contiguous.
2266 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
- 1, state
, _THIS_IP_
);
2267 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, bma
->idx
- 1),
2268 LEFT
.br_blockcount
+ new->br_blockcount
);
2269 xfs_bmbt_set_startoff(ep
,
2270 PREV
.br_startoff
+ new->br_blockcount
);
2271 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
- 1, state
, _THIS_IP_
);
2273 temp
= PREV
.br_blockcount
- new->br_blockcount
;
2274 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2275 xfs_bmbt_set_blockcount(ep
, temp
);
2276 if (bma
->cur
== NULL
)
2277 rval
= XFS_ILOG_DEXT
;
2280 error
= xfs_bmbt_lookup_eq(bma
->cur
, LEFT
.br_startoff
,
2281 LEFT
.br_startblock
, LEFT
.br_blockcount
,
2285 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2286 error
= xfs_bmbt_update(bma
->cur
, LEFT
.br_startoff
,
2288 LEFT
.br_blockcount
+
2294 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
2295 startblockval(PREV
.br_startblock
));
2296 xfs_bmbt_set_startblock(ep
, nullstartblock(da_new
));
2297 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2302 case BMAP_LEFT_FILLING
:
2304 * Filling in the first part of a previous delayed allocation.
2305 * The left neighbor is not contiguous.
2307 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2308 xfs_bmbt_set_startoff(ep
, new_endoff
);
2309 temp
= PREV
.br_blockcount
- new->br_blockcount
;
2310 xfs_bmbt_set_blockcount(ep
, temp
);
2311 xfs_iext_insert(bma
->ip
, bma
->idx
, 1, new, state
);
2312 bma
->ip
->i_d
.di_nextents
++;
2313 if (bma
->cur
== NULL
)
2314 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2316 rval
= XFS_ILOG_CORE
;
2317 error
= xfs_bmbt_lookup_eq(bma
->cur
, new->br_startoff
,
2318 new->br_startblock
, new->br_blockcount
,
2322 XFS_WANT_CORRUPTED_GOTO(i
== 0, done
);
2323 bma
->cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
2324 error
= xfs_btree_insert(bma
->cur
, &i
);
2327 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2330 if (xfs_bmap_needs_btree(bma
->ip
, XFS_DATA_FORK
)) {
2331 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
2332 bma
->firstblock
, bma
->flist
,
2333 &bma
->cur
, 1, &tmp_rval
, XFS_DATA_FORK
);
2338 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
2339 startblockval(PREV
.br_startblock
) -
2340 (bma
->cur
? bma
->cur
->bc_private
.b
.allocated
: 0));
2341 ep
= xfs_iext_get_ext(ifp
, bma
->idx
+ 1);
2342 xfs_bmbt_set_startblock(ep
, nullstartblock(da_new
));
2343 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
+ 1, state
, _THIS_IP_
);
2346 case BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2348 * Filling in the last part of a previous delayed allocation.
2349 * The right neighbor is contiguous with the new allocation.
2351 temp
= PREV
.br_blockcount
- new->br_blockcount
;
2352 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
+ 1, state
, _THIS_IP_
);
2353 xfs_bmbt_set_blockcount(ep
, temp
);
2354 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp
, bma
->idx
+ 1),
2355 new->br_startoff
, new->br_startblock
,
2356 new->br_blockcount
+ RIGHT
.br_blockcount
,
2358 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
+ 1, state
, _THIS_IP_
);
2359 if (bma
->cur
== NULL
)
2360 rval
= XFS_ILOG_DEXT
;
2363 error
= xfs_bmbt_lookup_eq(bma
->cur
, RIGHT
.br_startoff
,
2364 RIGHT
.br_startblock
,
2365 RIGHT
.br_blockcount
, &i
);
2368 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2369 error
= xfs_bmbt_update(bma
->cur
, new->br_startoff
,
2371 new->br_blockcount
+
2372 RIGHT
.br_blockcount
,
2378 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
2379 startblockval(PREV
.br_startblock
));
2380 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2381 xfs_bmbt_set_startblock(ep
, nullstartblock(da_new
));
2382 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2387 case BMAP_RIGHT_FILLING
:
2389 * Filling in the last part of a previous delayed allocation.
2390 * The right neighbor is not contiguous.
2392 temp
= PREV
.br_blockcount
- new->br_blockcount
;
2393 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2394 xfs_bmbt_set_blockcount(ep
, temp
);
2395 xfs_iext_insert(bma
->ip
, bma
->idx
+ 1, 1, new, state
);
2396 bma
->ip
->i_d
.di_nextents
++;
2397 if (bma
->cur
== NULL
)
2398 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2400 rval
= XFS_ILOG_CORE
;
2401 error
= xfs_bmbt_lookup_eq(bma
->cur
, new->br_startoff
,
2402 new->br_startblock
, new->br_blockcount
,
2406 XFS_WANT_CORRUPTED_GOTO(i
== 0, done
);
2407 bma
->cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
2408 error
= xfs_btree_insert(bma
->cur
, &i
);
2411 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2414 if (xfs_bmap_needs_btree(bma
->ip
, XFS_DATA_FORK
)) {
2415 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
2416 bma
->firstblock
, bma
->flist
, &bma
->cur
, 1,
2417 &tmp_rval
, XFS_DATA_FORK
);
2422 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
2423 startblockval(PREV
.br_startblock
) -
2424 (bma
->cur
? bma
->cur
->bc_private
.b
.allocated
: 0));
2425 ep
= xfs_iext_get_ext(ifp
, bma
->idx
);
2426 xfs_bmbt_set_startblock(ep
, nullstartblock(da_new
));
2427 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2434 * Filling in the middle part of a previous delayed allocation.
2435 * Contiguity is impossible here.
2436 * This case is avoided almost all the time.
2438 * We start with a delayed allocation:
2440 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
2443 * and we are allocating:
2444 * +rrrrrrrrrrrrrrrrr+
2447 * and we set it up for insertion as:
2448 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
2450 * PREV @ idx LEFT RIGHT
2451 * inserted at idx + 1
2453 temp
= new->br_startoff
- PREV
.br_startoff
;
2454 temp2
= PREV
.br_startoff
+ PREV
.br_blockcount
- new_endoff
;
2455 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, 0, _THIS_IP_
);
2456 xfs_bmbt_set_blockcount(ep
, temp
); /* truncate PREV */
2458 RIGHT
.br_state
= PREV
.br_state
;
2459 RIGHT
.br_startblock
= nullstartblock(
2460 (int)xfs_bmap_worst_indlen(bma
->ip
, temp2
));
2461 RIGHT
.br_startoff
= new_endoff
;
2462 RIGHT
.br_blockcount
= temp2
;
2463 /* insert LEFT (r[0]) and RIGHT (r[1]) at the same time */
2464 xfs_iext_insert(bma
->ip
, bma
->idx
+ 1, 2, &LEFT
, state
);
2465 bma
->ip
->i_d
.di_nextents
++;
2466 if (bma
->cur
== NULL
)
2467 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2469 rval
= XFS_ILOG_CORE
;
2470 error
= xfs_bmbt_lookup_eq(bma
->cur
, new->br_startoff
,
2471 new->br_startblock
, new->br_blockcount
,
2475 XFS_WANT_CORRUPTED_GOTO(i
== 0, done
);
2476 bma
->cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
2477 error
= xfs_btree_insert(bma
->cur
, &i
);
2480 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2483 if (xfs_bmap_needs_btree(bma
->ip
, XFS_DATA_FORK
)) {
2484 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
2485 bma
->firstblock
, bma
->flist
, &bma
->cur
,
2486 1, &tmp_rval
, XFS_DATA_FORK
);
2491 temp
= xfs_bmap_worst_indlen(bma
->ip
, temp
);
2492 temp2
= xfs_bmap_worst_indlen(bma
->ip
, temp2
);
2493 diff
= (int)(temp
+ temp2
- startblockval(PREV
.br_startblock
) -
2494 (bma
->cur
? bma
->cur
->bc_private
.b
.allocated
: 0));
2496 error
= xfs_icsb_modify_counters(bma
->ip
->i_mount
,
2498 -((int64_t)diff
), 0);
2504 ep
= xfs_iext_get_ext(ifp
, bma
->idx
);
2505 xfs_bmbt_set_startblock(ep
, nullstartblock((int)temp
));
2506 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2507 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
+ 2, state
, _THIS_IP_
);
2508 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp
, bma
->idx
+ 2),
2509 nullstartblock((int)temp2
));
2510 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
+ 2, state
, _THIS_IP_
);
2513 da_new
= temp
+ temp2
;
2516 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2517 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2518 case BMAP_LEFT_FILLING
| BMAP_RIGHT_CONTIG
:
2519 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
2520 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2521 case BMAP_LEFT_CONTIG
:
2522 case BMAP_RIGHT_CONTIG
:
2524 * These cases are all impossible.
2529 /* convert to a btree if necessary */
2530 if (xfs_bmap_needs_btree(bma
->ip
, XFS_DATA_FORK
)) {
2531 int tmp_logflags
; /* partial log flag return val */
2533 ASSERT(bma
->cur
== NULL
);
2534 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
2535 bma
->firstblock
, bma
->flist
, &bma
->cur
,
2536 da_old
> 0, &tmp_logflags
, XFS_DATA_FORK
);
2537 bma
->logflags
|= tmp_logflags
;
2542 /* adjust for changes in reserved delayed indirect blocks */
2543 if (da_old
|| da_new
) {
2546 temp
+= bma
->cur
->bc_private
.b
.allocated
;
2547 ASSERT(temp
<= da_old
);
2549 xfs_icsb_modify_counters(bma
->ip
->i_mount
,
2551 (int64_t)(da_old
- temp
), 0);
2554 /* clear out the allocated field, done with it now in any case. */
2556 bma
->cur
->bc_private
.b
.allocated
= 0;
2558 xfs_bmap_check_leaf_extents(bma
->cur
, bma
->ip
, XFS_DATA_FORK
);
2560 bma
->logflags
|= rval
;
2568 * Convert an unwritten allocation to a real allocation or vice versa.
2570 STATIC
int /* error */
2571 xfs_bmap_add_extent_unwritten_real(
2572 struct xfs_trans
*tp
,
2573 xfs_inode_t
*ip
, /* incore inode pointer */
2574 xfs_extnum_t
*idx
, /* extent number to update/insert */
2575 xfs_btree_cur_t
**curp
, /* if *curp is null, not a btree */
2576 xfs_bmbt_irec_t
*new, /* new data to add to file extents */
2577 xfs_fsblock_t
*first
, /* pointer to firstblock variable */
2578 xfs_bmap_free_t
*flist
, /* list of extents to be freed */
2579 int *logflagsp
) /* inode logging flags */
2581 xfs_btree_cur_t
*cur
; /* btree cursor */
2582 xfs_bmbt_rec_host_t
*ep
; /* extent entry for idx */
2583 int error
; /* error return value */
2584 int i
; /* temp state */
2585 xfs_ifork_t
*ifp
; /* inode fork pointer */
2586 xfs_fileoff_t new_endoff
; /* end offset of new entry */
2587 xfs_exntst_t newext
; /* new extent state */
2588 xfs_exntst_t oldext
; /* old extent state */
2589 xfs_bmbt_irec_t r
[3]; /* neighbor extent entries */
2590 /* left is 0, right is 1, prev is 2 */
2591 int rval
=0; /* return value (logging flags) */
2592 int state
= 0;/* state bits, accessed thru macros */
2597 ifp
= XFS_IFORK_PTR(ip
, XFS_DATA_FORK
);
2600 ASSERT(*idx
<= ifp
->if_bytes
/ sizeof(struct xfs_bmbt_rec
));
2601 ASSERT(!isnullstartblock(new->br_startblock
));
2603 XFS_STATS_INC(xs_add_exlist
);
2610 * Set up a bunch of variables to make the tests simpler.
2613 ep
= xfs_iext_get_ext(ifp
, *idx
);
2614 xfs_bmbt_get_all(ep
, &PREV
);
2615 newext
= new->br_state
;
2616 oldext
= (newext
== XFS_EXT_UNWRITTEN
) ?
2617 XFS_EXT_NORM
: XFS_EXT_UNWRITTEN
;
2618 ASSERT(PREV
.br_state
== oldext
);
2619 new_endoff
= new->br_startoff
+ new->br_blockcount
;
2620 ASSERT(PREV
.br_startoff
<= new->br_startoff
);
2621 ASSERT(PREV
.br_startoff
+ PREV
.br_blockcount
>= new_endoff
);
2624 * Set flags determining what part of the previous oldext allocation
2625 * extent is being replaced by a newext allocation.
2627 if (PREV
.br_startoff
== new->br_startoff
)
2628 state
|= BMAP_LEFT_FILLING
;
2629 if (PREV
.br_startoff
+ PREV
.br_blockcount
== new_endoff
)
2630 state
|= BMAP_RIGHT_FILLING
;
2633 * Check and set flags if this segment has a left neighbor.
2634 * Don't set contiguous if the combined extent would be too large.
2637 state
|= BMAP_LEFT_VALID
;
2638 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, *idx
- 1), &LEFT
);
2640 if (isnullstartblock(LEFT
.br_startblock
))
2641 state
|= BMAP_LEFT_DELAY
;
2644 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
2645 LEFT
.br_startoff
+ LEFT
.br_blockcount
== new->br_startoff
&&
2646 LEFT
.br_startblock
+ LEFT
.br_blockcount
== new->br_startblock
&&
2647 LEFT
.br_state
== newext
&&
2648 LEFT
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
2649 state
|= BMAP_LEFT_CONTIG
;
2652 * Check and set flags if this segment has a right neighbor.
2653 * Don't set contiguous if the combined extent would be too large.
2654 * Also check for all-three-contiguous being too large.
2656 if (*idx
< ip
->i_df
.if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
) - 1) {
2657 state
|= BMAP_RIGHT_VALID
;
2658 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, *idx
+ 1), &RIGHT
);
2659 if (isnullstartblock(RIGHT
.br_startblock
))
2660 state
|= BMAP_RIGHT_DELAY
;
2663 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
2664 new_endoff
== RIGHT
.br_startoff
&&
2665 new->br_startblock
+ new->br_blockcount
== RIGHT
.br_startblock
&&
2666 newext
== RIGHT
.br_state
&&
2667 new->br_blockcount
+ RIGHT
.br_blockcount
<= MAXEXTLEN
&&
2668 ((state
& (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
2669 BMAP_RIGHT_FILLING
)) !=
2670 (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
2671 BMAP_RIGHT_FILLING
) ||
2672 LEFT
.br_blockcount
+ new->br_blockcount
+ RIGHT
.br_blockcount
2674 state
|= BMAP_RIGHT_CONTIG
;
2677 * Switch out based on the FILLING and CONTIG state bits.
2679 switch (state
& (BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
2680 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
)) {
2681 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
2682 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2684 * Setting all of a previous oldext extent to newext.
2685 * The left and right neighbors are both contiguous with new.
2689 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2690 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
),
2691 LEFT
.br_blockcount
+ PREV
.br_blockcount
+
2692 RIGHT
.br_blockcount
);
2693 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2695 xfs_iext_remove(ip
, *idx
+ 1, 2, state
);
2696 ip
->i_d
.di_nextents
-= 2;
2698 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2700 rval
= XFS_ILOG_CORE
;
2701 if ((error
= xfs_bmbt_lookup_eq(cur
, RIGHT
.br_startoff
,
2702 RIGHT
.br_startblock
,
2703 RIGHT
.br_blockcount
, &i
)))
2705 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2706 if ((error
= xfs_btree_delete(cur
, &i
)))
2708 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2709 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2711 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2712 if ((error
= xfs_btree_delete(cur
, &i
)))
2714 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2715 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2717 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2718 if ((error
= xfs_bmbt_update(cur
, LEFT
.br_startoff
,
2720 LEFT
.br_blockcount
+ PREV
.br_blockcount
+
2721 RIGHT
.br_blockcount
, LEFT
.br_state
)))
2726 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
2728 * Setting all of a previous oldext extent to newext.
2729 * The left neighbor is contiguous, the right is not.
2733 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2734 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
),
2735 LEFT
.br_blockcount
+ PREV
.br_blockcount
);
2736 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2738 xfs_iext_remove(ip
, *idx
+ 1, 1, state
);
2739 ip
->i_d
.di_nextents
--;
2741 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2743 rval
= XFS_ILOG_CORE
;
2744 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2745 PREV
.br_startblock
, PREV
.br_blockcount
,
2748 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2749 if ((error
= xfs_btree_delete(cur
, &i
)))
2751 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2752 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2754 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2755 if ((error
= xfs_bmbt_update(cur
, LEFT
.br_startoff
,
2757 LEFT
.br_blockcount
+ PREV
.br_blockcount
,
2763 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2765 * Setting all of a previous oldext extent to newext.
2766 * The right neighbor is contiguous, the left is not.
2768 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2769 xfs_bmbt_set_blockcount(ep
,
2770 PREV
.br_blockcount
+ RIGHT
.br_blockcount
);
2771 xfs_bmbt_set_state(ep
, newext
);
2772 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2773 xfs_iext_remove(ip
, *idx
+ 1, 1, state
);
2774 ip
->i_d
.di_nextents
--;
2776 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2778 rval
= XFS_ILOG_CORE
;
2779 if ((error
= xfs_bmbt_lookup_eq(cur
, RIGHT
.br_startoff
,
2780 RIGHT
.br_startblock
,
2781 RIGHT
.br_blockcount
, &i
)))
2783 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2784 if ((error
= xfs_btree_delete(cur
, &i
)))
2786 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2787 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2789 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2790 if ((error
= xfs_bmbt_update(cur
, new->br_startoff
,
2792 new->br_blockcount
+ RIGHT
.br_blockcount
,
2798 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
2800 * Setting all of a previous oldext extent to newext.
2801 * Neither the left nor right neighbors are contiguous with
2804 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2805 xfs_bmbt_set_state(ep
, newext
);
2806 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2809 rval
= XFS_ILOG_DEXT
;
2812 if ((error
= xfs_bmbt_lookup_eq(cur
, new->br_startoff
,
2813 new->br_startblock
, new->br_blockcount
,
2816 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2817 if ((error
= xfs_bmbt_update(cur
, new->br_startoff
,
2818 new->br_startblock
, new->br_blockcount
,
2824 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
:
2826 * Setting the first part of a previous oldext extent to newext.
2827 * The left neighbor is contiguous.
2829 trace_xfs_bmap_pre_update(ip
, *idx
- 1, state
, _THIS_IP_
);
2830 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
- 1),
2831 LEFT
.br_blockcount
+ new->br_blockcount
);
2832 xfs_bmbt_set_startoff(ep
,
2833 PREV
.br_startoff
+ new->br_blockcount
);
2834 trace_xfs_bmap_post_update(ip
, *idx
- 1, state
, _THIS_IP_
);
2836 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2837 xfs_bmbt_set_startblock(ep
,
2838 new->br_startblock
+ new->br_blockcount
);
2839 xfs_bmbt_set_blockcount(ep
,
2840 PREV
.br_blockcount
- new->br_blockcount
);
2841 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2846 rval
= XFS_ILOG_DEXT
;
2849 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2850 PREV
.br_startblock
, PREV
.br_blockcount
,
2853 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2854 if ((error
= xfs_bmbt_update(cur
,
2855 PREV
.br_startoff
+ new->br_blockcount
,
2856 PREV
.br_startblock
+ new->br_blockcount
,
2857 PREV
.br_blockcount
- new->br_blockcount
,
2860 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2862 error
= xfs_bmbt_update(cur
, LEFT
.br_startoff
,
2864 LEFT
.br_blockcount
+ new->br_blockcount
,
2871 case BMAP_LEFT_FILLING
:
2873 * Setting the first part of a previous oldext extent to newext.
2874 * The left neighbor is not contiguous.
2876 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2877 ASSERT(ep
&& xfs_bmbt_get_state(ep
) == oldext
);
2878 xfs_bmbt_set_startoff(ep
, new_endoff
);
2879 xfs_bmbt_set_blockcount(ep
,
2880 PREV
.br_blockcount
- new->br_blockcount
);
2881 xfs_bmbt_set_startblock(ep
,
2882 new->br_startblock
+ new->br_blockcount
);
2883 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2885 xfs_iext_insert(ip
, *idx
, 1, new, state
);
2886 ip
->i_d
.di_nextents
++;
2888 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2890 rval
= XFS_ILOG_CORE
;
2891 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2892 PREV
.br_startblock
, PREV
.br_blockcount
,
2895 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2896 if ((error
= xfs_bmbt_update(cur
,
2897 PREV
.br_startoff
+ new->br_blockcount
,
2898 PREV
.br_startblock
+ new->br_blockcount
,
2899 PREV
.br_blockcount
- new->br_blockcount
,
2902 cur
->bc_rec
.b
= *new;
2903 if ((error
= xfs_btree_insert(cur
, &i
)))
2905 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2909 case BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2911 * Setting the last part of a previous oldext extent to newext.
2912 * The right neighbor is contiguous with the new allocation.
2914 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2915 xfs_bmbt_set_blockcount(ep
,
2916 PREV
.br_blockcount
- new->br_blockcount
);
2917 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2921 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2922 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp
, *idx
),
2923 new->br_startoff
, new->br_startblock
,
2924 new->br_blockcount
+ RIGHT
.br_blockcount
, newext
);
2925 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2928 rval
= XFS_ILOG_DEXT
;
2931 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2933 PREV
.br_blockcount
, &i
)))
2935 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2936 if ((error
= xfs_bmbt_update(cur
, PREV
.br_startoff
,
2938 PREV
.br_blockcount
- new->br_blockcount
,
2941 if ((error
= xfs_btree_increment(cur
, 0, &i
)))
2943 if ((error
= xfs_bmbt_update(cur
, new->br_startoff
,
2945 new->br_blockcount
+ RIGHT
.br_blockcount
,
2951 case BMAP_RIGHT_FILLING
:
2953 * Setting the last part of a previous oldext extent to newext.
2954 * The right neighbor is not contiguous.
2956 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2957 xfs_bmbt_set_blockcount(ep
,
2958 PREV
.br_blockcount
- new->br_blockcount
);
2959 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2962 xfs_iext_insert(ip
, *idx
, 1, new, state
);
2964 ip
->i_d
.di_nextents
++;
2966 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2968 rval
= XFS_ILOG_CORE
;
2969 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2970 PREV
.br_startblock
, PREV
.br_blockcount
,
2973 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2974 if ((error
= xfs_bmbt_update(cur
, PREV
.br_startoff
,
2976 PREV
.br_blockcount
- new->br_blockcount
,
2979 if ((error
= xfs_bmbt_lookup_eq(cur
, new->br_startoff
,
2980 new->br_startblock
, new->br_blockcount
,
2983 XFS_WANT_CORRUPTED_GOTO(i
== 0, done
);
2984 cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
2985 if ((error
= xfs_btree_insert(cur
, &i
)))
2987 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2993 * Setting the middle part of a previous oldext extent to
2994 * newext. Contiguity is impossible here.
2995 * One extent becomes three extents.
2997 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2998 xfs_bmbt_set_blockcount(ep
,
2999 new->br_startoff
- PREV
.br_startoff
);
3000 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
3003 r
[1].br_startoff
= new_endoff
;
3004 r
[1].br_blockcount
=
3005 PREV
.br_startoff
+ PREV
.br_blockcount
- new_endoff
;
3006 r
[1].br_startblock
= new->br_startblock
+ new->br_blockcount
;
3007 r
[1].br_state
= oldext
;
3010 xfs_iext_insert(ip
, *idx
, 2, &r
[0], state
);
3012 ip
->i_d
.di_nextents
+= 2;
3014 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
3016 rval
= XFS_ILOG_CORE
;
3017 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
3018 PREV
.br_startblock
, PREV
.br_blockcount
,
3021 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
3022 /* new right extent - oldext */
3023 if ((error
= xfs_bmbt_update(cur
, r
[1].br_startoff
,
3024 r
[1].br_startblock
, r
[1].br_blockcount
,
3027 /* new left extent - oldext */
3028 cur
->bc_rec
.b
= PREV
;
3029 cur
->bc_rec
.b
.br_blockcount
=
3030 new->br_startoff
- PREV
.br_startoff
;
3031 if ((error
= xfs_btree_insert(cur
, &i
)))
3033 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
3035 * Reset the cursor to the position of the new extent
3036 * we are about to insert as we can't trust it after
3037 * the previous insert.
3039 if ((error
= xfs_bmbt_lookup_eq(cur
, new->br_startoff
,
3040 new->br_startblock
, new->br_blockcount
,
3043 XFS_WANT_CORRUPTED_GOTO(i
== 0, done
);
3044 /* new middle extent - newext */
3045 cur
->bc_rec
.b
.br_state
= new->br_state
;
3046 if ((error
= xfs_btree_insert(cur
, &i
)))
3048 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
3052 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
3053 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
3054 case BMAP_LEFT_FILLING
| BMAP_RIGHT_CONTIG
:
3055 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
3056 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
3057 case BMAP_LEFT_CONTIG
:
3058 case BMAP_RIGHT_CONTIG
:
3060 * These cases are all impossible.
3065 /* convert to a btree if necessary */
3066 if (xfs_bmap_needs_btree(ip
, XFS_DATA_FORK
)) {
3067 int tmp_logflags
; /* partial log flag return val */
3069 ASSERT(cur
== NULL
);
3070 error
= xfs_bmap_extents_to_btree(tp
, ip
, first
, flist
, &cur
,
3071 0, &tmp_logflags
, XFS_DATA_FORK
);
3072 *logflagsp
|= tmp_logflags
;
3077 /* clear out the allocated field, done with it now in any case. */
3079 cur
->bc_private
.b
.allocated
= 0;
3083 xfs_bmap_check_leaf_extents(*curp
, ip
, XFS_DATA_FORK
);
3093 * Convert a hole to a delayed allocation.
3096 xfs_bmap_add_extent_hole_delay(
3097 xfs_inode_t
*ip
, /* incore inode pointer */
3098 xfs_extnum_t
*idx
, /* extent number to update/insert */
3099 xfs_bmbt_irec_t
*new) /* new data to add to file extents */
3101 xfs_ifork_t
*ifp
; /* inode fork pointer */
3102 xfs_bmbt_irec_t left
; /* left neighbor extent entry */
3103 xfs_filblks_t newlen
=0; /* new indirect size */
3104 xfs_filblks_t oldlen
=0; /* old indirect size */
3105 xfs_bmbt_irec_t right
; /* right neighbor extent entry */
3106 int state
; /* state bits, accessed thru macros */
3107 xfs_filblks_t temp
=0; /* temp for indirect calculations */
3109 ifp
= XFS_IFORK_PTR(ip
, XFS_DATA_FORK
);
3111 ASSERT(isnullstartblock(new->br_startblock
));
3114 * Check and set flags if this segment has a left neighbor
3117 state
|= BMAP_LEFT_VALID
;
3118 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, *idx
- 1), &left
);
3120 if (isnullstartblock(left
.br_startblock
))
3121 state
|= BMAP_LEFT_DELAY
;
3125 * Check and set flags if the current (right) segment exists.
3126 * If it doesn't exist, we're converting the hole at end-of-file.
3128 if (*idx
< ip
->i_df
.if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
)) {
3129 state
|= BMAP_RIGHT_VALID
;
3130 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, *idx
), &right
);
3132 if (isnullstartblock(right
.br_startblock
))
3133 state
|= BMAP_RIGHT_DELAY
;
3137 * Set contiguity flags on the left and right neighbors.
3138 * Don't let extents get too large, even if the pieces are contiguous.
3140 if ((state
& BMAP_LEFT_VALID
) && (state
& BMAP_LEFT_DELAY
) &&
3141 left
.br_startoff
+ left
.br_blockcount
== new->br_startoff
&&
3142 left
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
3143 state
|= BMAP_LEFT_CONTIG
;
3145 if ((state
& BMAP_RIGHT_VALID
) && (state
& BMAP_RIGHT_DELAY
) &&
3146 new->br_startoff
+ new->br_blockcount
== right
.br_startoff
&&
3147 new->br_blockcount
+ right
.br_blockcount
<= MAXEXTLEN
&&
3148 (!(state
& BMAP_LEFT_CONTIG
) ||
3149 (left
.br_blockcount
+ new->br_blockcount
+
3150 right
.br_blockcount
<= MAXEXTLEN
)))
3151 state
|= BMAP_RIGHT_CONTIG
;
3154 * Switch out based on the contiguity flags.
3156 switch (state
& (BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
)) {
3157 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
3159 * New allocation is contiguous with delayed allocations
3160 * on the left and on the right.
3161 * Merge all three into a single extent record.
3164 temp
= left
.br_blockcount
+ new->br_blockcount
+
3165 right
.br_blockcount
;
3167 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
3168 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
), temp
);
3169 oldlen
= startblockval(left
.br_startblock
) +
3170 startblockval(new->br_startblock
) +
3171 startblockval(right
.br_startblock
);
3172 newlen
= xfs_bmap_worst_indlen(ip
, temp
);
3173 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp
, *idx
),
3174 nullstartblock((int)newlen
));
3175 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
3177 xfs_iext_remove(ip
, *idx
+ 1, 1, state
);
3180 case BMAP_LEFT_CONTIG
:
3182 * New allocation is contiguous with a delayed allocation
3184 * Merge the new allocation with the left neighbor.
3187 temp
= left
.br_blockcount
+ new->br_blockcount
;
3189 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
3190 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
), temp
);
3191 oldlen
= startblockval(left
.br_startblock
) +
3192 startblockval(new->br_startblock
);
3193 newlen
= xfs_bmap_worst_indlen(ip
, temp
);
3194 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp
, *idx
),
3195 nullstartblock((int)newlen
));
3196 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
3199 case BMAP_RIGHT_CONTIG
:
3201 * New allocation is contiguous with a delayed allocation
3203 * Merge the new allocation with the right neighbor.
3205 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
3206 temp
= new->br_blockcount
+ right
.br_blockcount
;
3207 oldlen
= startblockval(new->br_startblock
) +
3208 startblockval(right
.br_startblock
);
3209 newlen
= xfs_bmap_worst_indlen(ip
, temp
);
3210 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp
, *idx
),
3212 nullstartblock((int)newlen
), temp
, right
.br_state
);
3213 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
3218 * New allocation is not contiguous with another
3219 * delayed allocation.
3220 * Insert a new entry.
3222 oldlen
= newlen
= 0;
3223 xfs_iext_insert(ip
, *idx
, 1, new, state
);
3226 if (oldlen
!= newlen
) {
3227 ASSERT(oldlen
> newlen
);
3228 xfs_icsb_modify_counters(ip
->i_mount
, XFS_SBS_FDBLOCKS
,
3229 (int64_t)(oldlen
- newlen
), 0);
3231 * Nothing to do for disk quota accounting here.
3237 * Convert a hole to a real allocation.
3239 STATIC
int /* error */
3240 xfs_bmap_add_extent_hole_real(
3241 struct xfs_bmalloca
*bma
,
3244 struct xfs_bmbt_irec
*new = &bma
->got
;
3245 int error
; /* error return value */
3246 int i
; /* temp state */
3247 xfs_ifork_t
*ifp
; /* inode fork pointer */
3248 xfs_bmbt_irec_t left
; /* left neighbor extent entry */
3249 xfs_bmbt_irec_t right
; /* right neighbor extent entry */
3250 int rval
=0; /* return value (logging flags) */
3251 int state
; /* state bits, accessed thru macros */
3253 ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
3255 ASSERT(bma
->idx
>= 0);
3256 ASSERT(bma
->idx
<= ifp
->if_bytes
/ sizeof(struct xfs_bmbt_rec
));
3257 ASSERT(!isnullstartblock(new->br_startblock
));
3259 !(bma
->cur
->bc_private
.b
.flags
& XFS_BTCUR_BPRV_WASDEL
));
3261 XFS_STATS_INC(xs_add_exlist
);
3264 if (whichfork
== XFS_ATTR_FORK
)
3265 state
|= BMAP_ATTRFORK
;
3268 * Check and set flags if this segment has a left neighbor.
3271 state
|= BMAP_LEFT_VALID
;
3272 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
- 1), &left
);
3273 if (isnullstartblock(left
.br_startblock
))
3274 state
|= BMAP_LEFT_DELAY
;
3278 * Check and set flags if this segment has a current value.
3279 * Not true if we're inserting into the "hole" at eof.
3281 if (bma
->idx
< ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
)) {
3282 state
|= BMAP_RIGHT_VALID
;
3283 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
), &right
);
3284 if (isnullstartblock(right
.br_startblock
))
3285 state
|= BMAP_RIGHT_DELAY
;
3289 * We're inserting a real allocation between "left" and "right".
3290 * Set the contiguity flags. Don't let extents get too large.
3292 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
3293 left
.br_startoff
+ left
.br_blockcount
== new->br_startoff
&&
3294 left
.br_startblock
+ left
.br_blockcount
== new->br_startblock
&&
3295 left
.br_state
== new->br_state
&&
3296 left
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
3297 state
|= BMAP_LEFT_CONTIG
;
3299 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
3300 new->br_startoff
+ new->br_blockcount
== right
.br_startoff
&&
3301 new->br_startblock
+ new->br_blockcount
== right
.br_startblock
&&
3302 new->br_state
== right
.br_state
&&
3303 new->br_blockcount
+ right
.br_blockcount
<= MAXEXTLEN
&&
3304 (!(state
& BMAP_LEFT_CONTIG
) ||
3305 left
.br_blockcount
+ new->br_blockcount
+
3306 right
.br_blockcount
<= MAXEXTLEN
))
3307 state
|= BMAP_RIGHT_CONTIG
;
3311 * Select which case we're in here, and implement it.
3313 switch (state
& (BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
)) {
3314 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
3316 * New allocation is contiguous with real allocations on the
3317 * left and on the right.
3318 * Merge all three into a single extent record.
3321 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
3322 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, bma
->idx
),
3323 left
.br_blockcount
+ new->br_blockcount
+
3324 right
.br_blockcount
);
3325 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
3327 xfs_iext_remove(bma
->ip
, bma
->idx
+ 1, 1, state
);
3329 XFS_IFORK_NEXT_SET(bma
->ip
, whichfork
,
3330 XFS_IFORK_NEXTENTS(bma
->ip
, whichfork
) - 1);
3331 if (bma
->cur
== NULL
) {
3332 rval
= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
3334 rval
= XFS_ILOG_CORE
;
3335 error
= xfs_bmbt_lookup_eq(bma
->cur
, right
.br_startoff
,
3336 right
.br_startblock
, right
.br_blockcount
,
3340 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
3341 error
= xfs_btree_delete(bma
->cur
, &i
);
3344 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
3345 error
= xfs_btree_decrement(bma
->cur
, 0, &i
);
3348 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
3349 error
= xfs_bmbt_update(bma
->cur
, left
.br_startoff
,
3351 left
.br_blockcount
+
3352 new->br_blockcount
+
3353 right
.br_blockcount
,
3360 case BMAP_LEFT_CONTIG
:
3362 * New allocation is contiguous with a real allocation
3364 * Merge the new allocation with the left neighbor.
3367 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
3368 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, bma
->idx
),
3369 left
.br_blockcount
+ new->br_blockcount
);
3370 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
3372 if (bma
->cur
== NULL
) {
3373 rval
= xfs_ilog_fext(whichfork
);
3376 error
= xfs_bmbt_lookup_eq(bma
->cur
, left
.br_startoff
,
3377 left
.br_startblock
, left
.br_blockcount
,
3381 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
3382 error
= xfs_bmbt_update(bma
->cur
, left
.br_startoff
,
3384 left
.br_blockcount
+
3392 case BMAP_RIGHT_CONTIG
:
3394 * New allocation is contiguous with a real allocation
3396 * Merge the new allocation with the right neighbor.
3398 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
3399 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp
, bma
->idx
),
3400 new->br_startoff
, new->br_startblock
,
3401 new->br_blockcount
+ right
.br_blockcount
,
3403 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
3405 if (bma
->cur
== NULL
) {
3406 rval
= xfs_ilog_fext(whichfork
);
3409 error
= xfs_bmbt_lookup_eq(bma
->cur
,
3411 right
.br_startblock
,
3412 right
.br_blockcount
, &i
);
3415 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
3416 error
= xfs_bmbt_update(bma
->cur
, new->br_startoff
,
3418 new->br_blockcount
+
3419 right
.br_blockcount
,
3428 * New allocation is not contiguous with another
3430 * Insert a new entry.
3432 xfs_iext_insert(bma
->ip
, bma
->idx
, 1, new, state
);
3433 XFS_IFORK_NEXT_SET(bma
->ip
, whichfork
,
3434 XFS_IFORK_NEXTENTS(bma
->ip
, whichfork
) + 1);
3435 if (bma
->cur
== NULL
) {
3436 rval
= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
3438 rval
= XFS_ILOG_CORE
;
3439 error
= xfs_bmbt_lookup_eq(bma
->cur
,
3442 new->br_blockcount
, &i
);
3445 XFS_WANT_CORRUPTED_GOTO(i
== 0, done
);
3446 bma
->cur
->bc_rec
.b
.br_state
= new->br_state
;
3447 error
= xfs_btree_insert(bma
->cur
, &i
);
3450 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
3455 /* convert to a btree if necessary */
3456 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
3457 int tmp_logflags
; /* partial log flag return val */
3459 ASSERT(bma
->cur
== NULL
);
3460 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
3461 bma
->firstblock
, bma
->flist
, &bma
->cur
,
3462 0, &tmp_logflags
, whichfork
);
3463 bma
->logflags
|= tmp_logflags
;
3468 /* clear out the allocated field, done with it now in any case. */
3470 bma
->cur
->bc_private
.b
.allocated
= 0;
3472 xfs_bmap_check_leaf_extents(bma
->cur
, bma
->ip
, whichfork
);
3474 bma
->logflags
|= rval
;
3479 * Functions used in the extent read, allocate and remove paths
3483 * Adjust the size of the new extent based on di_extsize and rt extsize.
3486 xfs_bmap_extsize_align(
3488 xfs_bmbt_irec_t
*gotp
, /* next extent pointer */
3489 xfs_bmbt_irec_t
*prevp
, /* previous extent pointer */
3490 xfs_extlen_t extsz
, /* align to this extent size */
3491 int rt
, /* is this a realtime inode? */
3492 int eof
, /* is extent at end-of-file? */
3493 int delay
, /* creating delalloc extent? */
3494 int convert
, /* overwriting unwritten extent? */
3495 xfs_fileoff_t
*offp
, /* in/out: aligned offset */
3496 xfs_extlen_t
*lenp
) /* in/out: aligned length */
3498 xfs_fileoff_t orig_off
; /* original offset */
3499 xfs_extlen_t orig_alen
; /* original length */
3500 xfs_fileoff_t orig_end
; /* original off+len */
3501 xfs_fileoff_t nexto
; /* next file offset */
3502 xfs_fileoff_t prevo
; /* previous file offset */
3503 xfs_fileoff_t align_off
; /* temp for offset */
3504 xfs_extlen_t align_alen
; /* temp for length */
3505 xfs_extlen_t temp
; /* temp for calculations */
3510 orig_off
= align_off
= *offp
;
3511 orig_alen
= align_alen
= *lenp
;
3512 orig_end
= orig_off
+ orig_alen
;
3515 * If this request overlaps an existing extent, then don't
3516 * attempt to perform any additional alignment.
3518 if (!delay
&& !eof
&&
3519 (orig_off
>= gotp
->br_startoff
) &&
3520 (orig_end
<= gotp
->br_startoff
+ gotp
->br_blockcount
)) {
3525 * If the file offset is unaligned vs. the extent size
3526 * we need to align it. This will be possible unless
3527 * the file was previously written with a kernel that didn't
3528 * perform this alignment, or if a truncate shot us in the
3531 temp
= do_mod(orig_off
, extsz
);
3537 * Same adjustment for the end of the requested area.
3539 if ((temp
= (align_alen
% extsz
))) {
3540 align_alen
+= extsz
- temp
;
3543 * If the previous block overlaps with this proposed allocation
3544 * then move the start forward without adjusting the length.
3546 if (prevp
->br_startoff
!= NULLFILEOFF
) {
3547 if (prevp
->br_startblock
== HOLESTARTBLOCK
)
3548 prevo
= prevp
->br_startoff
;
3550 prevo
= prevp
->br_startoff
+ prevp
->br_blockcount
;
3553 if (align_off
!= orig_off
&& align_off
< prevo
)
3556 * If the next block overlaps with this proposed allocation
3557 * then move the start back without adjusting the length,
3558 * but not before offset 0.
3559 * This may of course make the start overlap previous block,
3560 * and if we hit the offset 0 limit then the next block
3561 * can still overlap too.
3563 if (!eof
&& gotp
->br_startoff
!= NULLFILEOFF
) {
3564 if ((delay
&& gotp
->br_startblock
== HOLESTARTBLOCK
) ||
3565 (!delay
&& gotp
->br_startblock
== DELAYSTARTBLOCK
))
3566 nexto
= gotp
->br_startoff
+ gotp
->br_blockcount
;
3568 nexto
= gotp
->br_startoff
;
3570 nexto
= NULLFILEOFF
;
3572 align_off
+ align_alen
!= orig_end
&&
3573 align_off
+ align_alen
> nexto
)
3574 align_off
= nexto
> align_alen
? nexto
- align_alen
: 0;
3576 * If we're now overlapping the next or previous extent that
3577 * means we can't fit an extsz piece in this hole. Just move
3578 * the start forward to the first valid spot and set
3579 * the length so we hit the end.
3581 if (align_off
!= orig_off
&& align_off
< prevo
)
3583 if (align_off
+ align_alen
!= orig_end
&&
3584 align_off
+ align_alen
> nexto
&&
3585 nexto
!= NULLFILEOFF
) {
3586 ASSERT(nexto
> prevo
);
3587 align_alen
= nexto
- align_off
;
3591 * If realtime, and the result isn't a multiple of the realtime
3592 * extent size we need to remove blocks until it is.
3594 if (rt
&& (temp
= (align_alen
% mp
->m_sb
.sb_rextsize
))) {
3596 * We're not covering the original request, or
3597 * we won't be able to once we fix the length.
3599 if (orig_off
< align_off
||
3600 orig_end
> align_off
+ align_alen
||
3601 align_alen
- temp
< orig_alen
)
3602 return XFS_ERROR(EINVAL
);
3604 * Try to fix it by moving the start up.
3606 if (align_off
+ temp
<= orig_off
) {
3611 * Try to fix it by moving the end in.
3613 else if (align_off
+ align_alen
- temp
>= orig_end
)
3616 * Set the start to the minimum then trim the length.
3619 align_alen
-= orig_off
- align_off
;
3620 align_off
= orig_off
;
3621 align_alen
-= align_alen
% mp
->m_sb
.sb_rextsize
;
3624 * Result doesn't cover the request, fail it.
3626 if (orig_off
< align_off
|| orig_end
> align_off
+ align_alen
)
3627 return XFS_ERROR(EINVAL
);
3629 ASSERT(orig_off
>= align_off
);
3630 ASSERT(orig_end
<= align_off
+ align_alen
);
3634 if (!eof
&& gotp
->br_startoff
!= NULLFILEOFF
)
3635 ASSERT(align_off
+ align_alen
<= gotp
->br_startoff
);
3636 if (prevp
->br_startoff
!= NULLFILEOFF
)
3637 ASSERT(align_off
>= prevp
->br_startoff
+ prevp
->br_blockcount
);
3645 #define XFS_ALLOC_GAP_UNITS 4
3649 xfs_bmalloca_t
*ap
) /* bmap alloc argument struct */
3651 xfs_fsblock_t adjust
; /* adjustment to block numbers */
3652 xfs_agnumber_t fb_agno
; /* ag number of ap->firstblock */
3653 xfs_mount_t
*mp
; /* mount point structure */
3654 int nullfb
; /* true if ap->firstblock isn't set */
3655 int rt
; /* true if inode is realtime */
3657 #define ISVALID(x,y) \
3659 (x) < mp->m_sb.sb_rblocks : \
3660 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
3661 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
3662 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
3664 mp
= ap
->ip
->i_mount
;
3665 nullfb
= *ap
->firstblock
== NULLFSBLOCK
;
3666 rt
= XFS_IS_REALTIME_INODE(ap
->ip
) && ap
->userdata
;
3667 fb_agno
= nullfb
? NULLAGNUMBER
: XFS_FSB_TO_AGNO(mp
, *ap
->firstblock
);
3669 * If allocating at eof, and there's a previous real block,
3670 * try to use its last block as our starting point.
3672 if (ap
->eof
&& ap
->prev
.br_startoff
!= NULLFILEOFF
&&
3673 !isnullstartblock(ap
->prev
.br_startblock
) &&
3674 ISVALID(ap
->prev
.br_startblock
+ ap
->prev
.br_blockcount
,
3675 ap
->prev
.br_startblock
)) {
3676 ap
->blkno
= ap
->prev
.br_startblock
+ ap
->prev
.br_blockcount
;
3678 * Adjust for the gap between prevp and us.
3680 adjust
= ap
->offset
-
3681 (ap
->prev
.br_startoff
+ ap
->prev
.br_blockcount
);
3683 ISVALID(ap
->blkno
+ adjust
, ap
->prev
.br_startblock
))
3684 ap
->blkno
+= adjust
;
3687 * If not at eof, then compare the two neighbor blocks.
3688 * Figure out whether either one gives us a good starting point,
3689 * and pick the better one.
3691 else if (!ap
->eof
) {
3692 xfs_fsblock_t gotbno
; /* right side block number */
3693 xfs_fsblock_t gotdiff
=0; /* right side difference */
3694 xfs_fsblock_t prevbno
; /* left side block number */
3695 xfs_fsblock_t prevdiff
=0; /* left side difference */
3698 * If there's a previous (left) block, select a requested
3699 * start block based on it.
3701 if (ap
->prev
.br_startoff
!= NULLFILEOFF
&&
3702 !isnullstartblock(ap
->prev
.br_startblock
) &&
3703 (prevbno
= ap
->prev
.br_startblock
+
3704 ap
->prev
.br_blockcount
) &&
3705 ISVALID(prevbno
, ap
->prev
.br_startblock
)) {
3707 * Calculate gap to end of previous block.
3709 adjust
= prevdiff
= ap
->offset
-
3710 (ap
->prev
.br_startoff
+
3711 ap
->prev
.br_blockcount
);
3713 * Figure the startblock based on the previous block's
3714 * end and the gap size.
3716 * If the gap is large relative to the piece we're
3717 * allocating, or using it gives us an invalid block
3718 * number, then just use the end of the previous block.
3720 if (prevdiff
<= XFS_ALLOC_GAP_UNITS
* ap
->length
&&
3721 ISVALID(prevbno
+ prevdiff
,
3722 ap
->prev
.br_startblock
))
3727 * If the firstblock forbids it, can't use it,
3730 if (!rt
&& !nullfb
&&
3731 XFS_FSB_TO_AGNO(mp
, prevbno
) != fb_agno
)
3732 prevbno
= NULLFSBLOCK
;
3735 * No previous block or can't follow it, just default.
3738 prevbno
= NULLFSBLOCK
;
3740 * If there's a following (right) block, select a requested
3741 * start block based on it.
3743 if (!isnullstartblock(ap
->got
.br_startblock
)) {
3745 * Calculate gap to start of next block.
3747 adjust
= gotdiff
= ap
->got
.br_startoff
- ap
->offset
;
3749 * Figure the startblock based on the next block's
3750 * start and the gap size.
3752 gotbno
= ap
->got
.br_startblock
;
3755 * If the gap is large relative to the piece we're
3756 * allocating, or using it gives us an invalid block
3757 * number, then just use the start of the next block
3758 * offset by our length.
3760 if (gotdiff
<= XFS_ALLOC_GAP_UNITS
* ap
->length
&&
3761 ISVALID(gotbno
- gotdiff
, gotbno
))
3763 else if (ISVALID(gotbno
- ap
->length
, gotbno
)) {
3764 gotbno
-= ap
->length
;
3765 gotdiff
+= adjust
- ap
->length
;
3769 * If the firstblock forbids it, can't use it,
3772 if (!rt
&& !nullfb
&&
3773 XFS_FSB_TO_AGNO(mp
, gotbno
) != fb_agno
)
3774 gotbno
= NULLFSBLOCK
;
3777 * No next block, just default.
3780 gotbno
= NULLFSBLOCK
;
3782 * If both valid, pick the better one, else the only good
3783 * one, else ap->blkno is already set (to 0 or the inode block).
3785 if (prevbno
!= NULLFSBLOCK
&& gotbno
!= NULLFSBLOCK
)
3786 ap
->blkno
= prevdiff
<= gotdiff
? prevbno
: gotbno
;
3787 else if (prevbno
!= NULLFSBLOCK
)
3788 ap
->blkno
= prevbno
;
3789 else if (gotbno
!= NULLFSBLOCK
)
3797 xfs_bmalloca_t
*ap
) /* bmap alloc argument struct */
3799 xfs_alloctype_t atype
= 0; /* type for allocation routines */
3800 int error
; /* error return value */
3801 xfs_mount_t
*mp
; /* mount point structure */
3802 xfs_extlen_t prod
= 0; /* product factor for allocators */
3803 xfs_extlen_t ralen
= 0; /* realtime allocation length */
3804 xfs_extlen_t align
; /* minimum allocation alignment */
3807 mp
= ap
->ip
->i_mount
;
3808 align
= xfs_get_extsz_hint(ap
->ip
);
3809 prod
= align
/ mp
->m_sb
.sb_rextsize
;
3810 error
= xfs_bmap_extsize_align(mp
, &ap
->got
, &ap
->prev
,
3811 align
, 1, ap
->eof
, 0,
3812 ap
->conv
, &ap
->offset
, &ap
->length
);
3816 ASSERT(ap
->length
% mp
->m_sb
.sb_rextsize
== 0);
3819 * If the offset & length are not perfectly aligned
3820 * then kill prod, it will just get us in trouble.
3822 if (do_mod(ap
->offset
, align
) || ap
->length
% align
)
3825 * Set ralen to be the actual requested length in rtextents.
3827 ralen
= ap
->length
/ mp
->m_sb
.sb_rextsize
;
3829 * If the old value was close enough to MAXEXTLEN that
3830 * we rounded up to it, cut it back so it's valid again.
3831 * Note that if it's a really large request (bigger than
3832 * MAXEXTLEN), we don't hear about that number, and can't
3833 * adjust the starting point to match it.
3835 if (ralen
* mp
->m_sb
.sb_rextsize
>= MAXEXTLEN
)
3836 ralen
= MAXEXTLEN
/ mp
->m_sb
.sb_rextsize
;
3839 * Lock out other modifications to the RT bitmap inode.
3841 xfs_ilock(mp
->m_rbmip
, XFS_ILOCK_EXCL
);
3842 xfs_trans_ijoin(ap
->tp
, mp
->m_rbmip
, XFS_ILOCK_EXCL
);
3845 * If it's an allocation to an empty file at offset 0,
3846 * pick an extent that will space things out in the rt area.
3848 if (ap
->eof
&& ap
->offset
== 0) {
3849 xfs_rtblock_t
uninitialized_var(rtx
); /* realtime extent no */
3851 error
= xfs_rtpick_extent(mp
, ap
->tp
, ralen
, &rtx
);
3854 ap
->blkno
= rtx
* mp
->m_sb
.sb_rextsize
;
3859 xfs_bmap_adjacent(ap
);
3862 * Realtime allocation, done through xfs_rtallocate_extent.
3864 atype
= ap
->blkno
== 0 ? XFS_ALLOCTYPE_ANY_AG
: XFS_ALLOCTYPE_NEAR_BNO
;
3865 do_div(ap
->blkno
, mp
->m_sb
.sb_rextsize
);
3868 if ((error
= xfs_rtallocate_extent(ap
->tp
, ap
->blkno
, 1, ap
->length
,
3869 &ralen
, atype
, ap
->wasdel
, prod
, &rtb
)))
3871 if (rtb
== NULLFSBLOCK
&& prod
> 1 &&
3872 (error
= xfs_rtallocate_extent(ap
->tp
, ap
->blkno
, 1,
3873 ap
->length
, &ralen
, atype
,
3874 ap
->wasdel
, 1, &rtb
)))
3877 if (ap
->blkno
!= NULLFSBLOCK
) {
3878 ap
->blkno
*= mp
->m_sb
.sb_rextsize
;
3879 ralen
*= mp
->m_sb
.sb_rextsize
;
3881 ap
->ip
->i_d
.di_nblocks
+= ralen
;
3882 xfs_trans_log_inode(ap
->tp
, ap
->ip
, XFS_ILOG_CORE
);
3884 ap
->ip
->i_delayed_blks
-= ralen
;
3886 * Adjust the disk quota also. This was reserved
3889 xfs_trans_mod_dquot_byino(ap
->tp
, ap
->ip
,
3890 ap
->wasdel
? XFS_TRANS_DQ_DELRTBCOUNT
:
3891 XFS_TRANS_DQ_RTBCOUNT
, (long) ralen
);
3899 xfs_bmap_btalloc_nullfb(
3900 struct xfs_bmalloca
*ap
,
3901 struct xfs_alloc_arg
*args
,
3904 struct xfs_mount
*mp
= ap
->ip
->i_mount
;
3905 struct xfs_perag
*pag
;
3906 xfs_agnumber_t ag
, startag
;
3910 if (ap
->userdata
&& xfs_inode_is_filestream(ap
->ip
))
3911 args
->type
= XFS_ALLOCTYPE_NEAR_BNO
;
3913 args
->type
= XFS_ALLOCTYPE_START_BNO
;
3914 args
->total
= ap
->total
;
3917 * Search for an allocation group with a single extent large enough
3918 * for the request. If one isn't found, then adjust the minimum
3919 * allocation size to the largest space found.
3921 startag
= ag
= XFS_FSB_TO_AGNO(mp
, args
->fsbno
);
3922 if (startag
== NULLAGNUMBER
)
3925 pag
= xfs_perag_get(mp
, ag
);
3926 while (*blen
< args
->maxlen
) {
3927 if (!pag
->pagf_init
) {
3928 error
= xfs_alloc_pagf_init(mp
, args
->tp
, ag
,
3929 XFS_ALLOC_FLAG_TRYLOCK
);
3937 * See xfs_alloc_fix_freelist...
3939 if (pag
->pagf_init
) {
3940 xfs_extlen_t longest
;
3941 longest
= xfs_alloc_longest_free_extent(mp
, pag
);
3942 if (*blen
< longest
)
3947 if (xfs_inode_is_filestream(ap
->ip
)) {
3948 if (*blen
>= args
->maxlen
)
3953 * If startag is an invalid AG, we've
3954 * come here once before and
3955 * xfs_filestream_new_ag picked the
3956 * best currently available.
3958 * Don't continue looping, since we
3959 * could loop forever.
3961 if (startag
== NULLAGNUMBER
)
3964 error
= xfs_filestream_new_ag(ap
, &ag
);
3969 /* loop again to set 'blen'*/
3970 startag
= NULLAGNUMBER
;
3971 pag
= xfs_perag_get(mp
, ag
);
3975 if (++ag
== mp
->m_sb
.sb_agcount
)
3980 pag
= xfs_perag_get(mp
, ag
);
3985 * Since the above loop did a BUF_TRYLOCK, it is
3986 * possible that there is space for this request.
3988 if (notinit
|| *blen
< ap
->minlen
)
3989 args
->minlen
= ap
->minlen
;
3991 * If the best seen length is less than the request
3992 * length, use the best as the minimum.
3994 else if (*blen
< args
->maxlen
)
3995 args
->minlen
= *blen
;
3997 * Otherwise we've seen an extent as big as maxlen,
3998 * use that as the minimum.
4001 args
->minlen
= args
->maxlen
;
4004 * set the failure fallback case to look in the selected
4005 * AG as the stream may have moved.
4007 if (xfs_inode_is_filestream(ap
->ip
))
4008 ap
->blkno
= args
->fsbno
= XFS_AGB_TO_FSB(mp
, ag
, 0);
4015 xfs_bmalloca_t
*ap
) /* bmap alloc argument struct */
4017 xfs_mount_t
*mp
; /* mount point structure */
4018 xfs_alloctype_t atype
= 0; /* type for allocation routines */
4019 xfs_extlen_t align
; /* minimum allocation alignment */
4020 xfs_agnumber_t fb_agno
; /* ag number of ap->firstblock */
4022 xfs_alloc_arg_t args
;
4024 xfs_extlen_t nextminlen
= 0;
4025 int nullfb
; /* true if ap->firstblock isn't set */
4032 mp
= ap
->ip
->i_mount
;
4033 align
= ap
->userdata
? xfs_get_extsz_hint(ap
->ip
) : 0;
4034 if (unlikely(align
)) {
4035 error
= xfs_bmap_extsize_align(mp
, &ap
->got
, &ap
->prev
,
4036 align
, 0, ap
->eof
, 0, ap
->conv
,
4037 &ap
->offset
, &ap
->length
);
4041 nullfb
= *ap
->firstblock
== NULLFSBLOCK
;
4042 fb_agno
= nullfb
? NULLAGNUMBER
: XFS_FSB_TO_AGNO(mp
, *ap
->firstblock
);
4044 if (ap
->userdata
&& xfs_inode_is_filestream(ap
->ip
)) {
4045 ag
= xfs_filestream_lookup_ag(ap
->ip
);
4046 ag
= (ag
!= NULLAGNUMBER
) ? ag
: 0;
4047 ap
->blkno
= XFS_AGB_TO_FSB(mp
, ag
, 0);
4049 ap
->blkno
= XFS_INO_TO_FSB(mp
, ap
->ip
->i_ino
);
4052 ap
->blkno
= *ap
->firstblock
;
4054 xfs_bmap_adjacent(ap
);
4057 * If allowed, use ap->blkno; otherwise must use firstblock since
4058 * it's in the right allocation group.
4060 if (nullfb
|| XFS_FSB_TO_AGNO(mp
, ap
->blkno
) == fb_agno
)
4063 ap
->blkno
= *ap
->firstblock
;
4065 * Normal allocation, done through xfs_alloc_vextent.
4067 tryagain
= isaligned
= 0;
4068 memset(&args
, 0, sizeof(args
));
4071 args
.fsbno
= ap
->blkno
;
4073 /* Trim the allocation back to the maximum an AG can fit. */
4074 args
.maxlen
= MIN(ap
->length
, XFS_ALLOC_AG_MAX_USABLE(mp
));
4075 args
.firstblock
= *ap
->firstblock
;
4078 error
= xfs_bmap_btalloc_nullfb(ap
, &args
, &blen
);
4081 } else if (ap
->flist
->xbf_low
) {
4082 if (xfs_inode_is_filestream(ap
->ip
))
4083 args
.type
= XFS_ALLOCTYPE_FIRST_AG
;
4085 args
.type
= XFS_ALLOCTYPE_START_BNO
;
4086 args
.total
= args
.minlen
= ap
->minlen
;
4088 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
4089 args
.total
= ap
->total
;
4090 args
.minlen
= ap
->minlen
;
4092 /* apply extent size hints if obtained earlier */
4093 if (unlikely(align
)) {
4095 if ((args
.mod
= (xfs_extlen_t
)do_mod(ap
->offset
, args
.prod
)))
4096 args
.mod
= (xfs_extlen_t
)(args
.prod
- args
.mod
);
4097 } else if (mp
->m_sb
.sb_blocksize
>= PAGE_CACHE_SIZE
) {
4101 args
.prod
= PAGE_CACHE_SIZE
>> mp
->m_sb
.sb_blocklog
;
4102 if ((args
.mod
= (xfs_extlen_t
)(do_mod(ap
->offset
, args
.prod
))))
4103 args
.mod
= (xfs_extlen_t
)(args
.prod
- args
.mod
);
4106 * If we are not low on available data blocks, and the
4107 * underlying logical volume manager is a stripe, and
4108 * the file offset is zero then try to allocate data
4109 * blocks on stripe unit boundary.
4110 * NOTE: ap->aeof is only set if the allocation length
4111 * is >= the stripe unit and the allocation offset is
4112 * at the end of file.
4114 if (!ap
->flist
->xbf_low
&& ap
->aeof
) {
4116 args
.alignment
= mp
->m_dalign
;
4120 * Adjust for alignment
4122 if (blen
> args
.alignment
&& blen
<= args
.maxlen
)
4123 args
.minlen
= blen
- args
.alignment
;
4124 args
.minalignslop
= 0;
4127 * First try an exact bno allocation.
4128 * If it fails then do a near or start bno
4129 * allocation with alignment turned on.
4133 args
.type
= XFS_ALLOCTYPE_THIS_BNO
;
4136 * Compute the minlen+alignment for the
4137 * next case. Set slop so that the value
4138 * of minlen+alignment+slop doesn't go up
4139 * between the calls.
4141 if (blen
> mp
->m_dalign
&& blen
<= args
.maxlen
)
4142 nextminlen
= blen
- mp
->m_dalign
;
4144 nextminlen
= args
.minlen
;
4145 if (nextminlen
+ mp
->m_dalign
> args
.minlen
+ 1)
4147 nextminlen
+ mp
->m_dalign
-
4150 args
.minalignslop
= 0;
4154 args
.minalignslop
= 0;
4156 args
.minleft
= ap
->minleft
;
4157 args
.wasdel
= ap
->wasdel
;
4159 args
.userdata
= ap
->userdata
;
4160 if ((error
= xfs_alloc_vextent(&args
)))
4162 if (tryagain
&& args
.fsbno
== NULLFSBLOCK
) {
4164 * Exact allocation failed. Now try with alignment
4168 args
.fsbno
= ap
->blkno
;
4169 args
.alignment
= mp
->m_dalign
;
4170 args
.minlen
= nextminlen
;
4171 args
.minalignslop
= 0;
4173 if ((error
= xfs_alloc_vextent(&args
)))
4176 if (isaligned
&& args
.fsbno
== NULLFSBLOCK
) {
4178 * allocation failed, so turn off alignment and
4182 args
.fsbno
= ap
->blkno
;
4184 if ((error
= xfs_alloc_vextent(&args
)))
4187 if (args
.fsbno
== NULLFSBLOCK
&& nullfb
&&
4188 args
.minlen
> ap
->minlen
) {
4189 args
.minlen
= ap
->minlen
;
4190 args
.type
= XFS_ALLOCTYPE_START_BNO
;
4191 args
.fsbno
= ap
->blkno
;
4192 if ((error
= xfs_alloc_vextent(&args
)))
4195 if (args
.fsbno
== NULLFSBLOCK
&& nullfb
) {
4197 args
.type
= XFS_ALLOCTYPE_FIRST_AG
;
4198 args
.total
= ap
->minlen
;
4200 if ((error
= xfs_alloc_vextent(&args
)))
4202 ap
->flist
->xbf_low
= 1;
4204 if (args
.fsbno
!= NULLFSBLOCK
) {
4206 * check the allocation happened at the same or higher AG than
4207 * the first block that was allocated.
4209 ASSERT(*ap
->firstblock
== NULLFSBLOCK
||
4210 XFS_FSB_TO_AGNO(mp
, *ap
->firstblock
) ==
4211 XFS_FSB_TO_AGNO(mp
, args
.fsbno
) ||
4212 (ap
->flist
->xbf_low
&&
4213 XFS_FSB_TO_AGNO(mp
, *ap
->firstblock
) <
4214 XFS_FSB_TO_AGNO(mp
, args
.fsbno
)));
4216 ap
->blkno
= args
.fsbno
;
4217 if (*ap
->firstblock
== NULLFSBLOCK
)
4218 *ap
->firstblock
= args
.fsbno
;
4219 ASSERT(nullfb
|| fb_agno
== args
.agno
||
4220 (ap
->flist
->xbf_low
&& fb_agno
< args
.agno
));
4221 ap
->length
= args
.len
;
4222 ap
->ip
->i_d
.di_nblocks
+= args
.len
;
4223 xfs_trans_log_inode(ap
->tp
, ap
->ip
, XFS_ILOG_CORE
);
4225 ap
->ip
->i_delayed_blks
-= args
.len
;
4227 * Adjust the disk quota also. This was reserved
4230 xfs_trans_mod_dquot_byino(ap
->tp
, ap
->ip
,
4231 ap
->wasdel
? XFS_TRANS_DQ_DELBCOUNT
:
4232 XFS_TRANS_DQ_BCOUNT
,
4235 ap
->blkno
= NULLFSBLOCK
;
4242 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
4243 * It figures out where to ask the underlying allocator to put the new extent.
4247 xfs_bmalloca_t
*ap
) /* bmap alloc argument struct */
4249 if (XFS_IS_REALTIME_INODE(ap
->ip
) && ap
->userdata
)
4250 return xfs_bmap_rtalloc(ap
);
4251 return xfs_bmap_btalloc(ap
);
4255 * Trim the returned map to the required bounds
4259 struct xfs_bmbt_irec
*mval
,
4260 struct xfs_bmbt_irec
*got
,
4268 if ((flags
& XFS_BMAPI_ENTIRE
) ||
4269 got
->br_startoff
+ got
->br_blockcount
<= obno
) {
4271 if (isnullstartblock(got
->br_startblock
))
4272 mval
->br_startblock
= DELAYSTARTBLOCK
;
4278 ASSERT((*bno
>= obno
) || (n
== 0));
4280 mval
->br_startoff
= *bno
;
4281 if (isnullstartblock(got
->br_startblock
))
4282 mval
->br_startblock
= DELAYSTARTBLOCK
;
4284 mval
->br_startblock
= got
->br_startblock
+
4285 (*bno
- got
->br_startoff
);
4287 * Return the minimum of what we got and what we asked for for
4288 * the length. We can use the len variable here because it is
4289 * modified below and we could have been there before coming
4290 * here if the first part of the allocation didn't overlap what
4293 mval
->br_blockcount
= XFS_FILBLKS_MIN(end
- *bno
,
4294 got
->br_blockcount
- (*bno
- got
->br_startoff
));
4295 mval
->br_state
= got
->br_state
;
4296 ASSERT(mval
->br_blockcount
<= len
);
4301 * Update and validate the extent map to return
4304 xfs_bmapi_update_map(
4305 struct xfs_bmbt_irec
**map
,
4313 xfs_bmbt_irec_t
*mval
= *map
;
4315 ASSERT((flags
& XFS_BMAPI_ENTIRE
) ||
4316 ((mval
->br_startoff
+ mval
->br_blockcount
) <= end
));
4317 ASSERT((flags
& XFS_BMAPI_ENTIRE
) || (mval
->br_blockcount
<= *len
) ||
4318 (mval
->br_startoff
< obno
));
4320 *bno
= mval
->br_startoff
+ mval
->br_blockcount
;
4322 if (*n
> 0 && mval
->br_startoff
== mval
[-1].br_startoff
) {
4323 /* update previous map with new information */
4324 ASSERT(mval
->br_startblock
== mval
[-1].br_startblock
);
4325 ASSERT(mval
->br_blockcount
> mval
[-1].br_blockcount
);
4326 ASSERT(mval
->br_state
== mval
[-1].br_state
);
4327 mval
[-1].br_blockcount
= mval
->br_blockcount
;
4328 mval
[-1].br_state
= mval
->br_state
;
4329 } else if (*n
> 0 && mval
->br_startblock
!= DELAYSTARTBLOCK
&&
4330 mval
[-1].br_startblock
!= DELAYSTARTBLOCK
&&
4331 mval
[-1].br_startblock
!= HOLESTARTBLOCK
&&
4332 mval
->br_startblock
== mval
[-1].br_startblock
+
4333 mval
[-1].br_blockcount
&&
4334 ((flags
& XFS_BMAPI_IGSTATE
) ||
4335 mval
[-1].br_state
== mval
->br_state
)) {
4336 ASSERT(mval
->br_startoff
==
4337 mval
[-1].br_startoff
+ mval
[-1].br_blockcount
);
4338 mval
[-1].br_blockcount
+= mval
->br_blockcount
;
4339 } else if (*n
> 0 &&
4340 mval
->br_startblock
== DELAYSTARTBLOCK
&&
4341 mval
[-1].br_startblock
== DELAYSTARTBLOCK
&&
4342 mval
->br_startoff
==
4343 mval
[-1].br_startoff
+ mval
[-1].br_blockcount
) {
4344 mval
[-1].br_blockcount
+= mval
->br_blockcount
;
4345 mval
[-1].br_state
= mval
->br_state
;
4346 } else if (!((*n
== 0) &&
4347 ((mval
->br_startoff
+ mval
->br_blockcount
) <=
4356 * Map file blocks to filesystem blocks without allocation.
4360 struct xfs_inode
*ip
,
4363 struct xfs_bmbt_irec
*mval
,
4367 struct xfs_mount
*mp
= ip
->i_mount
;
4368 struct xfs_ifork
*ifp
;
4369 struct xfs_bmbt_irec got
;
4370 struct xfs_bmbt_irec prev
;
4377 int whichfork
= (flags
& XFS_BMAPI_ATTRFORK
) ?
4378 XFS_ATTR_FORK
: XFS_DATA_FORK
;
4381 ASSERT(!(flags
& ~(XFS_BMAPI_ATTRFORK
|XFS_BMAPI_ENTIRE
|
4382 XFS_BMAPI_IGSTATE
)));
4384 if (unlikely(XFS_TEST_ERROR(
4385 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
4386 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
4387 mp
, XFS_ERRTAG_BMAPIFORMAT
, XFS_RANDOM_BMAPIFORMAT
))) {
4388 XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW
, mp
);
4389 return XFS_ERROR(EFSCORRUPTED
);
4392 if (XFS_FORCED_SHUTDOWN(mp
))
4393 return XFS_ERROR(EIO
);
4395 XFS_STATS_INC(xs_blk_mapr
);
4397 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4399 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
4400 error
= xfs_iread_extents(NULL
, ip
, whichfork
);
4405 xfs_bmap_search_extents(ip
, bno
, whichfork
, &eof
, &lastx
, &got
, &prev
);
4409 while (bno
< end
&& n
< *nmap
) {
4410 /* Reading past eof, act as though there's a hole up to end. */
4412 got
.br_startoff
= end
;
4413 if (got
.br_startoff
> bno
) {
4414 /* Reading in a hole. */
4415 mval
->br_startoff
= bno
;
4416 mval
->br_startblock
= HOLESTARTBLOCK
;
4417 mval
->br_blockcount
=
4418 XFS_FILBLKS_MIN(len
, got
.br_startoff
- bno
);
4419 mval
->br_state
= XFS_EXT_NORM
;
4420 bno
+= mval
->br_blockcount
;
4421 len
-= mval
->br_blockcount
;
4427 /* set up the extent map to return. */
4428 xfs_bmapi_trim_map(mval
, &got
, &bno
, len
, obno
, end
, n
, flags
);
4429 xfs_bmapi_update_map(&mval
, &bno
, &len
, obno
, end
, &n
, flags
);
4431 /* If we're done, stop now. */
4432 if (bno
>= end
|| n
>= *nmap
)
4435 /* Else go on to the next record. */
4436 if (++lastx
< ifp
->if_bytes
/ sizeof(xfs_bmbt_rec_t
))
4437 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, lastx
), &got
);
4446 xfs_bmapi_reserve_delalloc(
4447 struct xfs_inode
*ip
,
4450 struct xfs_bmbt_irec
*got
,
4451 struct xfs_bmbt_irec
*prev
,
4452 xfs_extnum_t
*lastx
,
4455 struct xfs_mount
*mp
= ip
->i_mount
;
4456 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, XFS_DATA_FORK
);
4458 xfs_extlen_t indlen
;
4459 char rt
= XFS_IS_REALTIME_INODE(ip
);
4463 alen
= XFS_FILBLKS_MIN(len
, MAXEXTLEN
);
4465 alen
= XFS_FILBLKS_MIN(alen
, got
->br_startoff
- aoff
);
4467 /* Figure out the extent size, adjust alen */
4468 extsz
= xfs_get_extsz_hint(ip
);
4471 * Make sure we don't exceed a single extent length when we
4472 * align the extent by reducing length we are going to
4473 * allocate by the maximum amount extent size aligment may
4476 alen
= XFS_FILBLKS_MIN(len
, MAXEXTLEN
- (2 * extsz
- 1));
4477 error
= xfs_bmap_extsize_align(mp
, got
, prev
, extsz
, rt
, eof
,
4478 1, 0, &aoff
, &alen
);
4483 extsz
= alen
/ mp
->m_sb
.sb_rextsize
;
4486 * Make a transaction-less quota reservation for delayed allocation
4487 * blocks. This number gets adjusted later. We return if we haven't
4488 * allocated blocks already inside this loop.
4490 error
= xfs_trans_reserve_quota_nblks(NULL
, ip
, (long)alen
, 0,
4491 rt
? XFS_QMOPT_RES_RTBLKS
: XFS_QMOPT_RES_REGBLKS
);
4496 * Split changing sb for alen and indlen since they could be coming
4497 * from different places.
4499 indlen
= (xfs_extlen_t
)xfs_bmap_worst_indlen(ip
, alen
);
4503 error
= xfs_mod_incore_sb(mp
, XFS_SBS_FREXTENTS
,
4504 -((int64_t)extsz
), 0);
4506 error
= xfs_icsb_modify_counters(mp
, XFS_SBS_FDBLOCKS
,
4507 -((int64_t)alen
), 0);
4511 goto out_unreserve_quota
;
4513 error
= xfs_icsb_modify_counters(mp
, XFS_SBS_FDBLOCKS
,
4514 -((int64_t)indlen
), 0);
4516 goto out_unreserve_blocks
;
4519 ip
->i_delayed_blks
+= alen
;
4521 got
->br_startoff
= aoff
;
4522 got
->br_startblock
= nullstartblock(indlen
);
4523 got
->br_blockcount
= alen
;
4524 got
->br_state
= XFS_EXT_NORM
;
4525 xfs_bmap_add_extent_hole_delay(ip
, lastx
, got
);
4528 * Update our extent pointer, given that xfs_bmap_add_extent_hole_delay
4529 * might have merged it into one of the neighbouring ones.
4531 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, *lastx
), got
);
4533 ASSERT(got
->br_startoff
<= aoff
);
4534 ASSERT(got
->br_startoff
+ got
->br_blockcount
>= aoff
+ alen
);
4535 ASSERT(isnullstartblock(got
->br_startblock
));
4536 ASSERT(got
->br_state
== XFS_EXT_NORM
);
4539 out_unreserve_blocks
:
4541 xfs_mod_incore_sb(mp
, XFS_SBS_FREXTENTS
, extsz
, 0);
4543 xfs_icsb_modify_counters(mp
, XFS_SBS_FDBLOCKS
, alen
, 0);
4544 out_unreserve_quota
:
4545 if (XFS_IS_QUOTA_ON(mp
))
4546 xfs_trans_unreserve_quota_nblks(NULL
, ip
, (long)alen
, 0, rt
?
4547 XFS_QMOPT_RES_RTBLKS
: XFS_QMOPT_RES_REGBLKS
);
4552 * Map file blocks to filesystem blocks, adding delayed allocations as needed.
4556 struct xfs_inode
*ip
, /* incore inode */
4557 xfs_fileoff_t bno
, /* starting file offs. mapped */
4558 xfs_filblks_t len
, /* length to map in file */
4559 struct xfs_bmbt_irec
*mval
, /* output: map values */
4560 int *nmap
, /* i/o: mval size/count */
4561 int flags
) /* XFS_BMAPI_... */
4563 struct xfs_mount
*mp
= ip
->i_mount
;
4564 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, XFS_DATA_FORK
);
4565 struct xfs_bmbt_irec got
; /* current file extent record */
4566 struct xfs_bmbt_irec prev
; /* previous file extent record */
4567 xfs_fileoff_t obno
; /* old block number (offset) */
4568 xfs_fileoff_t end
; /* end of mapped file region */
4569 xfs_extnum_t lastx
; /* last useful extent number */
4570 int eof
; /* we've hit the end of extents */
4571 int n
= 0; /* current extent index */
4575 ASSERT(*nmap
<= XFS_BMAP_MAX_NMAP
);
4576 ASSERT(!(flags
& ~XFS_BMAPI_ENTIRE
));
4578 if (unlikely(XFS_TEST_ERROR(
4579 (XFS_IFORK_FORMAT(ip
, XFS_DATA_FORK
) != XFS_DINODE_FMT_EXTENTS
&&
4580 XFS_IFORK_FORMAT(ip
, XFS_DATA_FORK
) != XFS_DINODE_FMT_BTREE
),
4581 mp
, XFS_ERRTAG_BMAPIFORMAT
, XFS_RANDOM_BMAPIFORMAT
))) {
4582 XFS_ERROR_REPORT("xfs_bmapi_delay", XFS_ERRLEVEL_LOW
, mp
);
4583 return XFS_ERROR(EFSCORRUPTED
);
4586 if (XFS_FORCED_SHUTDOWN(mp
))
4587 return XFS_ERROR(EIO
);
4589 XFS_STATS_INC(xs_blk_mapw
);
4591 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
4592 error
= xfs_iread_extents(NULL
, ip
, XFS_DATA_FORK
);
4597 xfs_bmap_search_extents(ip
, bno
, XFS_DATA_FORK
, &eof
, &lastx
, &got
, &prev
);
4601 while (bno
< end
&& n
< *nmap
) {
4602 if (eof
|| got
.br_startoff
> bno
) {
4603 error
= xfs_bmapi_reserve_delalloc(ip
, bno
, len
, &got
,
4604 &prev
, &lastx
, eof
);
4614 /* set up the extent map to return. */
4615 xfs_bmapi_trim_map(mval
, &got
, &bno
, len
, obno
, end
, n
, flags
);
4616 xfs_bmapi_update_map(&mval
, &bno
, &len
, obno
, end
, &n
, flags
);
4618 /* If we're done, stop now. */
4619 if (bno
>= end
|| n
>= *nmap
)
4622 /* Else go on to the next record. */
4624 if (++lastx
< ifp
->if_bytes
/ sizeof(xfs_bmbt_rec_t
))
4625 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, lastx
), &got
);
4636 __xfs_bmapi_allocate(
4637 struct xfs_bmalloca
*bma
)
4639 struct xfs_mount
*mp
= bma
->ip
->i_mount
;
4640 int whichfork
= (bma
->flags
& XFS_BMAPI_ATTRFORK
) ?
4641 XFS_ATTR_FORK
: XFS_DATA_FORK
;
4642 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
4643 int tmp_logflags
= 0;
4647 ASSERT(bma
->length
> 0);
4649 rt
= (whichfork
== XFS_DATA_FORK
) && XFS_IS_REALTIME_INODE(bma
->ip
);
4652 * For the wasdelay case, we could also just allocate the stuff asked
4653 * for in this bmap call but that wouldn't be as good.
4656 bma
->length
= (xfs_extlen_t
)bma
->got
.br_blockcount
;
4657 bma
->offset
= bma
->got
.br_startoff
;
4658 if (bma
->idx
!= NULLEXTNUM
&& bma
->idx
) {
4659 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
- 1),
4663 bma
->length
= XFS_FILBLKS_MIN(bma
->length
, MAXEXTLEN
);
4665 bma
->length
= XFS_FILBLKS_MIN(bma
->length
,
4666 bma
->got
.br_startoff
- bma
->offset
);
4670 * Indicate if this is the first user data in the file, or just any
4673 if (!(bma
->flags
& XFS_BMAPI_METADATA
)) {
4674 bma
->userdata
= (bma
->offset
== 0) ?
4675 XFS_ALLOC_INITIAL_USER_DATA
: XFS_ALLOC_USERDATA
;
4678 bma
->minlen
= (bma
->flags
& XFS_BMAPI_CONTIG
) ? bma
->length
: 1;
4681 * Only want to do the alignment at the eof if it is userdata and
4682 * allocation length is larger than a stripe unit.
4684 if (mp
->m_dalign
&& bma
->length
>= mp
->m_dalign
&&
4685 !(bma
->flags
& XFS_BMAPI_METADATA
) && whichfork
== XFS_DATA_FORK
) {
4686 error
= xfs_bmap_isaeof(bma
, whichfork
);
4691 error
= xfs_bmap_alloc(bma
);
4695 if (bma
->flist
->xbf_low
)
4698 bma
->cur
->bc_private
.b
.firstblock
= *bma
->firstblock
;
4699 if (bma
->blkno
== NULLFSBLOCK
)
4701 if ((ifp
->if_flags
& XFS_IFBROOT
) && !bma
->cur
) {
4702 bma
->cur
= xfs_bmbt_init_cursor(mp
, bma
->tp
, bma
->ip
, whichfork
);
4703 bma
->cur
->bc_private
.b
.firstblock
= *bma
->firstblock
;
4704 bma
->cur
->bc_private
.b
.flist
= bma
->flist
;
4707 * Bump the number of extents we've allocated
4713 bma
->cur
->bc_private
.b
.flags
=
4714 bma
->wasdel
? XFS_BTCUR_BPRV_WASDEL
: 0;
4716 bma
->got
.br_startoff
= bma
->offset
;
4717 bma
->got
.br_startblock
= bma
->blkno
;
4718 bma
->got
.br_blockcount
= bma
->length
;
4719 bma
->got
.br_state
= XFS_EXT_NORM
;
4722 * A wasdelay extent has been initialized, so shouldn't be flagged
4725 if (!bma
->wasdel
&& (bma
->flags
& XFS_BMAPI_PREALLOC
) &&
4726 xfs_sb_version_hasextflgbit(&mp
->m_sb
))
4727 bma
->got
.br_state
= XFS_EXT_UNWRITTEN
;
4730 error
= xfs_bmap_add_extent_delay_real(bma
);
4732 error
= xfs_bmap_add_extent_hole_real(bma
, whichfork
);
4734 bma
->logflags
|= tmp_logflags
;
4739 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4740 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4741 * the neighbouring ones.
4743 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
), &bma
->got
);
4745 ASSERT(bma
->got
.br_startoff
<= bma
->offset
);
4746 ASSERT(bma
->got
.br_startoff
+ bma
->got
.br_blockcount
>=
4747 bma
->offset
+ bma
->length
);
4748 ASSERT(bma
->got
.br_state
== XFS_EXT_NORM
||
4749 bma
->got
.br_state
== XFS_EXT_UNWRITTEN
);
4754 xfs_bmapi_allocate_worker(
4755 struct work_struct
*work
)
4757 struct xfs_bmalloca
*args
= container_of(work
,
4758 struct xfs_bmalloca
, work
);
4759 unsigned long pflags
;
4761 /* we are in a transaction context here */
4762 current_set_flags_nested(&pflags
, PF_FSTRANS
);
4764 args
->result
= __xfs_bmapi_allocate(args
);
4765 complete(args
->done
);
4767 current_restore_flags_nested(&pflags
, PF_FSTRANS
);
4771 * Some allocation requests often come in with little stack to work on. Push
4772 * them off to a worker thread so there is lots of stack to use. Otherwise just
4773 * call directly to avoid the context switch overhead here.
4777 struct xfs_bmalloca
*args
)
4779 DECLARE_COMPLETION_ONSTACK(done
);
4781 if (!args
->stack_switch
)
4782 return __xfs_bmapi_allocate(args
);
4786 INIT_WORK_ONSTACK(&args
->work
, xfs_bmapi_allocate_worker
);
4787 queue_work(xfs_alloc_wq
, &args
->work
);
4788 wait_for_completion(&done
);
4789 return args
->result
;
4793 xfs_bmapi_convert_unwritten(
4794 struct xfs_bmalloca
*bma
,
4795 struct xfs_bmbt_irec
*mval
,
4799 int whichfork
= (flags
& XFS_BMAPI_ATTRFORK
) ?
4800 XFS_ATTR_FORK
: XFS_DATA_FORK
;
4801 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
4802 int tmp_logflags
= 0;
4805 /* check if we need to do unwritten->real conversion */
4806 if (mval
->br_state
== XFS_EXT_UNWRITTEN
&&
4807 (flags
& XFS_BMAPI_PREALLOC
))
4810 /* check if we need to do real->unwritten conversion */
4811 if (mval
->br_state
== XFS_EXT_NORM
&&
4812 (flags
& (XFS_BMAPI_PREALLOC
| XFS_BMAPI_CONVERT
)) !=
4813 (XFS_BMAPI_PREALLOC
| XFS_BMAPI_CONVERT
))
4817 * Modify (by adding) the state flag, if writing.
4819 ASSERT(mval
->br_blockcount
<= len
);
4820 if ((ifp
->if_flags
& XFS_IFBROOT
) && !bma
->cur
) {
4821 bma
->cur
= xfs_bmbt_init_cursor(bma
->ip
->i_mount
, bma
->tp
,
4822 bma
->ip
, whichfork
);
4823 bma
->cur
->bc_private
.b
.firstblock
= *bma
->firstblock
;
4824 bma
->cur
->bc_private
.b
.flist
= bma
->flist
;
4826 mval
->br_state
= (mval
->br_state
== XFS_EXT_UNWRITTEN
)
4827 ? XFS_EXT_NORM
: XFS_EXT_UNWRITTEN
;
4829 error
= xfs_bmap_add_extent_unwritten_real(bma
->tp
, bma
->ip
, &bma
->idx
,
4830 &bma
->cur
, mval
, bma
->firstblock
, bma
->flist
,
4832 bma
->logflags
|= tmp_logflags
;
4837 * Update our extent pointer, given that
4838 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4839 * of the neighbouring ones.
4841 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
), &bma
->got
);
4844 * We may have combined previously unwritten space with written space,
4845 * so generate another request.
4847 if (mval
->br_blockcount
< len
)
4853 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4854 * extent state if necessary. Details behaviour is controlled by the flags
4855 * parameter. Only allocates blocks from a single allocation group, to avoid
4858 * The returned value in "firstblock" from the first call in a transaction
4859 * must be remembered and presented to subsequent calls in "firstblock".
4860 * An upper bound for the number of blocks to be allocated is supplied to
4861 * the first call in "total"; if no allocation group has that many free
4862 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4866 struct xfs_trans
*tp
, /* transaction pointer */
4867 struct xfs_inode
*ip
, /* incore inode */
4868 xfs_fileoff_t bno
, /* starting file offs. mapped */
4869 xfs_filblks_t len
, /* length to map in file */
4870 int flags
, /* XFS_BMAPI_... */
4871 xfs_fsblock_t
*firstblock
, /* first allocated block
4872 controls a.g. for allocs */
4873 xfs_extlen_t total
, /* total blocks needed */
4874 struct xfs_bmbt_irec
*mval
, /* output: map values */
4875 int *nmap
, /* i/o: mval size/count */
4876 struct xfs_bmap_free
*flist
) /* i/o: list extents to free */
4878 struct xfs_mount
*mp
= ip
->i_mount
;
4879 struct xfs_ifork
*ifp
;
4880 struct xfs_bmalloca bma
= { 0 }; /* args for xfs_bmap_alloc */
4881 xfs_fileoff_t end
; /* end of mapped file region */
4882 int eof
; /* after the end of extents */
4883 int error
; /* error return */
4884 int n
; /* current extent index */
4885 xfs_fileoff_t obno
; /* old block number (offset) */
4886 int whichfork
; /* data or attr fork */
4887 char inhole
; /* current location is hole in file */
4888 char wasdelay
; /* old extent was delayed */
4891 xfs_fileoff_t orig_bno
; /* original block number value */
4892 int orig_flags
; /* original flags arg value */
4893 xfs_filblks_t orig_len
; /* original value of len arg */
4894 struct xfs_bmbt_irec
*orig_mval
; /* original value of mval */
4895 int orig_nmap
; /* original value of *nmap */
4905 ASSERT(*nmap
<= XFS_BMAP_MAX_NMAP
);
4906 ASSERT(!(flags
& XFS_BMAPI_IGSTATE
));
4910 whichfork
= (flags
& XFS_BMAPI_ATTRFORK
) ?
4911 XFS_ATTR_FORK
: XFS_DATA_FORK
;
4913 if (unlikely(XFS_TEST_ERROR(
4914 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
4915 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
&&
4916 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_LOCAL
),
4917 mp
, XFS_ERRTAG_BMAPIFORMAT
, XFS_RANDOM_BMAPIFORMAT
))) {
4918 XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW
, mp
);
4919 return XFS_ERROR(EFSCORRUPTED
);
4922 if (XFS_FORCED_SHUTDOWN(mp
))
4923 return XFS_ERROR(EIO
);
4925 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4927 XFS_STATS_INC(xs_blk_mapw
);
4929 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
) {
4931 * XXX (dgc): This assumes we are only called for inodes that
4932 * contain content neutral data in local format. Anything that
4933 * contains caller-specific data in local format that needs
4934 * transformation to move to a block format needs to do the
4935 * conversion to extent format itself.
4937 * Directory data forks and attribute forks handle this
4938 * themselves, but with the addition of metadata verifiers every
4939 * data fork in local format now contains caller specific data
4940 * and as such conversion through this function is likely to be
4943 * The only likely user of this branch is for remote symlinks,
4944 * but we cannot overwrite the data fork contents of the symlink
4945 * (EEXIST occurs higher up the stack) and so it will never go
4946 * from local format to extent format here. Hence I don't think
4947 * this branch is ever executed intentionally and we should
4948 * consider removing it and asserting that xfs_bmapi_write()
4949 * cannot be called directly on local format forks. i.e. callers
4950 * are completely responsible for local to extent format
4951 * conversion, not xfs_bmapi_write().
4953 error
= xfs_bmap_local_to_extents(tp
, ip
, firstblock
, total
,
4954 &bma
.logflags
, whichfork
,
4955 xfs_bmap_local_to_extents_init_fn
);
4960 if (*firstblock
== NULLFSBLOCK
) {
4961 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
)
4962 bma
.minleft
= be16_to_cpu(ifp
->if_broot
->bb_level
) + 1;
4969 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
4970 error
= xfs_iread_extents(tp
, ip
, whichfork
);
4975 xfs_bmap_search_extents(ip
, bno
, whichfork
, &eof
, &bma
.idx
, &bma
.got
,
4986 bma
.firstblock
= firstblock
;
4988 if (flags
& XFS_BMAPI_STACK_SWITCH
)
4989 bma
.stack_switch
= 1;
4991 while (bno
< end
&& n
< *nmap
) {
4992 inhole
= eof
|| bma
.got
.br_startoff
> bno
;
4993 wasdelay
= !inhole
&& isnullstartblock(bma
.got
.br_startblock
);
4996 * First, deal with the hole before the allocated space
4997 * that we found, if any.
4999 if (inhole
|| wasdelay
) {
5001 bma
.conv
= !!(flags
& XFS_BMAPI_CONVERT
);
5002 bma
.wasdel
= wasdelay
;
5007 * There's a 32/64 bit type mismatch between the
5008 * allocation length request (which can be 64 bits in
5009 * length) and the bma length request, which is
5010 * xfs_extlen_t and therefore 32 bits. Hence we have to
5011 * check for 32-bit overflows and handle them here.
5013 if (len
> (xfs_filblks_t
)MAXEXTLEN
)
5014 bma
.length
= MAXEXTLEN
;
5019 ASSERT(bma
.length
> 0);
5020 error
= xfs_bmapi_allocate(&bma
);
5023 if (bma
.blkno
== NULLFSBLOCK
)
5027 /* Deal with the allocated space we found. */
5028 xfs_bmapi_trim_map(mval
, &bma
.got
, &bno
, len
, obno
,
5031 /* Execute unwritten extent conversion if necessary */
5032 error
= xfs_bmapi_convert_unwritten(&bma
, mval
, len
, flags
);
5033 if (error
== EAGAIN
)
5038 /* update the extent map to return */
5039 xfs_bmapi_update_map(&mval
, &bno
, &len
, obno
, end
, &n
, flags
);
5042 * If we're done, stop now. Stop when we've allocated
5043 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
5044 * the transaction may get too big.
5046 if (bno
>= end
|| n
>= *nmap
|| bma
.nallocs
>= *nmap
)
5049 /* Else go on to the next record. */
5051 if (++bma
.idx
< ifp
->if_bytes
/ sizeof(xfs_bmbt_rec_t
)) {
5052 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
.idx
),
5060 * Transform from btree to extents, give it cur.
5062 if (xfs_bmap_wants_extents(ip
, whichfork
)) {
5063 int tmp_logflags
= 0;
5066 error
= xfs_bmap_btree_to_extents(tp
, ip
, bma
.cur
,
5067 &tmp_logflags
, whichfork
);
5068 bma
.logflags
|= tmp_logflags
;
5073 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
||
5074 XFS_IFORK_NEXTENTS(ip
, whichfork
) >
5075 XFS_IFORK_MAXEXT(ip
, whichfork
));
5079 * Log everything. Do this after conversion, there's no point in
5080 * logging the extent records if we've converted to btree format.
5082 if ((bma
.logflags
& xfs_ilog_fext(whichfork
)) &&
5083 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
5084 bma
.logflags
&= ~xfs_ilog_fext(whichfork
);
5085 else if ((bma
.logflags
& xfs_ilog_fbroot(whichfork
)) &&
5086 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)
5087 bma
.logflags
&= ~xfs_ilog_fbroot(whichfork
);
5089 * Log whatever the flags say, even if error. Otherwise we might miss
5090 * detecting a case where the data is changed, there's an error,
5091 * and it's not logged so we don't shutdown when we should.
5094 xfs_trans_log_inode(tp
, ip
, bma
.logflags
);
5098 ASSERT(*firstblock
== NULLFSBLOCK
||
5099 XFS_FSB_TO_AGNO(mp
, *firstblock
) ==
5101 bma
.cur
->bc_private
.b
.firstblock
) ||
5103 XFS_FSB_TO_AGNO(mp
, *firstblock
) <
5105 bma
.cur
->bc_private
.b
.firstblock
)));
5106 *firstblock
= bma
.cur
->bc_private
.b
.firstblock
;
5108 xfs_btree_del_cursor(bma
.cur
,
5109 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
5112 xfs_bmap_validate_ret(orig_bno
, orig_len
, orig_flags
, orig_mval
,
5118 * Called by xfs_bmapi to update file extent records and the btree
5119 * after removing space (or undoing a delayed allocation).
5121 STATIC
int /* error */
5122 xfs_bmap_del_extent(
5123 xfs_inode_t
*ip
, /* incore inode pointer */
5124 xfs_trans_t
*tp
, /* current transaction pointer */
5125 xfs_extnum_t
*idx
, /* extent number to update/delete */
5126 xfs_bmap_free_t
*flist
, /* list of extents to be freed */
5127 xfs_btree_cur_t
*cur
, /* if null, not a btree */
5128 xfs_bmbt_irec_t
*del
, /* data to remove from extents */
5129 int *logflagsp
, /* inode logging flags */
5130 int whichfork
) /* data or attr fork */
5132 xfs_filblks_t da_new
; /* new delay-alloc indirect blocks */
5133 xfs_filblks_t da_old
; /* old delay-alloc indirect blocks */
5134 xfs_fsblock_t del_endblock
=0; /* first block past del */
5135 xfs_fileoff_t del_endoff
; /* first offset past del */
5136 int delay
; /* current block is delayed allocated */
5137 int do_fx
; /* free extent at end of routine */
5138 xfs_bmbt_rec_host_t
*ep
; /* current extent entry pointer */
5139 int error
; /* error return value */
5140 int flags
; /* inode logging flags */
5141 xfs_bmbt_irec_t got
; /* current extent entry */
5142 xfs_fileoff_t got_endoff
; /* first offset past got */
5143 int i
; /* temp state */
5144 xfs_ifork_t
*ifp
; /* inode fork pointer */
5145 xfs_mount_t
*mp
; /* mount structure */
5146 xfs_filblks_t nblks
; /* quota/sb block count */
5147 xfs_bmbt_irec_t
new; /* new record to be inserted */
5149 uint qfield
; /* quota field to update */
5150 xfs_filblks_t temp
; /* for indirect length calculations */
5151 xfs_filblks_t temp2
; /* for indirect length calculations */
5154 XFS_STATS_INC(xs_del_exlist
);
5156 if (whichfork
== XFS_ATTR_FORK
)
5157 state
|= BMAP_ATTRFORK
;
5160 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5161 ASSERT((*idx
>= 0) && (*idx
< ifp
->if_bytes
/
5162 (uint
)sizeof(xfs_bmbt_rec_t
)));
5163 ASSERT(del
->br_blockcount
> 0);
5164 ep
= xfs_iext_get_ext(ifp
, *idx
);
5165 xfs_bmbt_get_all(ep
, &got
);
5166 ASSERT(got
.br_startoff
<= del
->br_startoff
);
5167 del_endoff
= del
->br_startoff
+ del
->br_blockcount
;
5168 got_endoff
= got
.br_startoff
+ got
.br_blockcount
;
5169 ASSERT(got_endoff
>= del_endoff
);
5170 delay
= isnullstartblock(got
.br_startblock
);
5171 ASSERT(isnullstartblock(del
->br_startblock
) == delay
);
5176 * If deleting a real allocation, must free up the disk space.
5179 flags
= XFS_ILOG_CORE
;
5181 * Realtime allocation. Free it and record di_nblocks update.
5183 if (whichfork
== XFS_DATA_FORK
&& XFS_IS_REALTIME_INODE(ip
)) {
5187 ASSERT(do_mod(del
->br_blockcount
,
5188 mp
->m_sb
.sb_rextsize
) == 0);
5189 ASSERT(do_mod(del
->br_startblock
,
5190 mp
->m_sb
.sb_rextsize
) == 0);
5191 bno
= del
->br_startblock
;
5192 len
= del
->br_blockcount
;
5193 do_div(bno
, mp
->m_sb
.sb_rextsize
);
5194 do_div(len
, mp
->m_sb
.sb_rextsize
);
5195 error
= xfs_rtfree_extent(tp
, bno
, (xfs_extlen_t
)len
);
5199 nblks
= len
* mp
->m_sb
.sb_rextsize
;
5200 qfield
= XFS_TRANS_DQ_RTBCOUNT
;
5203 * Ordinary allocation.
5207 nblks
= del
->br_blockcount
;
5208 qfield
= XFS_TRANS_DQ_BCOUNT
;
5211 * Set up del_endblock and cur for later.
5213 del_endblock
= del
->br_startblock
+ del
->br_blockcount
;
5215 if ((error
= xfs_bmbt_lookup_eq(cur
, got
.br_startoff
,
5216 got
.br_startblock
, got
.br_blockcount
,
5219 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
5221 da_old
= da_new
= 0;
5223 da_old
= startblockval(got
.br_startblock
);
5229 * Set flag value to use in switch statement.
5230 * Left-contig is 2, right-contig is 1.
5232 switch (((got
.br_startoff
== del
->br_startoff
) << 1) |
5233 (got_endoff
== del_endoff
)) {
5236 * Matches the whole extent. Delete the entry.
5238 xfs_iext_remove(ip
, *idx
, 1,
5239 whichfork
== XFS_ATTR_FORK
? BMAP_ATTRFORK
: 0);
5244 XFS_IFORK_NEXT_SET(ip
, whichfork
,
5245 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
5246 flags
|= XFS_ILOG_CORE
;
5248 flags
|= xfs_ilog_fext(whichfork
);
5251 if ((error
= xfs_btree_delete(cur
, &i
)))
5253 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
5258 * Deleting the first part of the extent.
5260 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
5261 xfs_bmbt_set_startoff(ep
, del_endoff
);
5262 temp
= got
.br_blockcount
- del
->br_blockcount
;
5263 xfs_bmbt_set_blockcount(ep
, temp
);
5265 temp
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
5267 xfs_bmbt_set_startblock(ep
, nullstartblock((int)temp
));
5268 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
5272 xfs_bmbt_set_startblock(ep
, del_endblock
);
5273 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
5275 flags
|= xfs_ilog_fext(whichfork
);
5278 if ((error
= xfs_bmbt_update(cur
, del_endoff
, del_endblock
,
5279 got
.br_blockcount
- del
->br_blockcount
,
5286 * Deleting the last part of the extent.
5288 temp
= got
.br_blockcount
- del
->br_blockcount
;
5289 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
5290 xfs_bmbt_set_blockcount(ep
, temp
);
5292 temp
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
5294 xfs_bmbt_set_startblock(ep
, nullstartblock((int)temp
));
5295 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
5299 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
5301 flags
|= xfs_ilog_fext(whichfork
);
5304 if ((error
= xfs_bmbt_update(cur
, got
.br_startoff
,
5306 got
.br_blockcount
- del
->br_blockcount
,
5313 * Deleting the middle of the extent.
5315 temp
= del
->br_startoff
- got
.br_startoff
;
5316 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
5317 xfs_bmbt_set_blockcount(ep
, temp
);
5318 new.br_startoff
= del_endoff
;
5319 temp2
= got_endoff
- del_endoff
;
5320 new.br_blockcount
= temp2
;
5321 new.br_state
= got
.br_state
;
5323 new.br_startblock
= del_endblock
;
5324 flags
|= XFS_ILOG_CORE
;
5326 if ((error
= xfs_bmbt_update(cur
,
5328 got
.br_startblock
, temp
,
5331 if ((error
= xfs_btree_increment(cur
, 0, &i
)))
5333 cur
->bc_rec
.b
= new;
5334 error
= xfs_btree_insert(cur
, &i
);
5335 if (error
&& error
!= ENOSPC
)
5338 * If get no-space back from btree insert,
5339 * it tried a split, and we have a zero
5340 * block reservation.
5341 * Fix up our state and return the error.
5343 if (error
== ENOSPC
) {
5345 * Reset the cursor, don't trust
5346 * it after any insert operation.
5348 if ((error
= xfs_bmbt_lookup_eq(cur
,
5353 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
5355 * Update the btree record back
5356 * to the original value.
5358 if ((error
= xfs_bmbt_update(cur
,
5365 * Reset the extent record back
5366 * to the original value.
5368 xfs_bmbt_set_blockcount(ep
,
5371 error
= XFS_ERROR(ENOSPC
);
5374 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
5376 flags
|= xfs_ilog_fext(whichfork
);
5377 XFS_IFORK_NEXT_SET(ip
, whichfork
,
5378 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
5380 ASSERT(whichfork
== XFS_DATA_FORK
);
5381 temp
= xfs_bmap_worst_indlen(ip
, temp
);
5382 xfs_bmbt_set_startblock(ep
, nullstartblock((int)temp
));
5383 temp2
= xfs_bmap_worst_indlen(ip
, temp2
);
5384 new.br_startblock
= nullstartblock((int)temp2
);
5385 da_new
= temp
+ temp2
;
5386 while (da_new
> da_old
) {
5390 xfs_bmbt_set_startblock(ep
,
5391 nullstartblock((int)temp
));
5393 if (da_new
== da_old
)
5399 nullstartblock((int)temp2
);
5403 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
5404 xfs_iext_insert(ip
, *idx
+ 1, 1, &new, state
);
5409 * If we need to, add to list of extents to delete.
5412 xfs_bmap_add_free(del
->br_startblock
, del
->br_blockcount
, flist
,
5415 * Adjust inode # blocks in the file.
5418 ip
->i_d
.di_nblocks
-= nblks
;
5420 * Adjust quota data.
5423 xfs_trans_mod_dquot_byino(tp
, ip
, qfield
, (long)-nblks
);
5426 * Account for change in delayed indirect blocks.
5427 * Nothing to do for disk quota accounting here.
5429 ASSERT(da_old
>= da_new
);
5430 if (da_old
> da_new
) {
5431 xfs_icsb_modify_counters(mp
, XFS_SBS_FDBLOCKS
,
5432 (int64_t)(da_old
- da_new
), 0);
5440 * Unmap (remove) blocks from a file.
5441 * If nexts is nonzero then the number of extents to remove is limited to
5442 * that value. If not all extents in the block range can be removed then
5447 xfs_trans_t
*tp
, /* transaction pointer */
5448 struct xfs_inode
*ip
, /* incore inode */
5449 xfs_fileoff_t bno
, /* starting offset to unmap */
5450 xfs_filblks_t len
, /* length to unmap in file */
5451 int flags
, /* misc flags */
5452 xfs_extnum_t nexts
, /* number of extents max */
5453 xfs_fsblock_t
*firstblock
, /* first allocated block
5454 controls a.g. for allocs */
5455 xfs_bmap_free_t
*flist
, /* i/o: list extents to free */
5456 int *done
) /* set if not done yet */
5458 xfs_btree_cur_t
*cur
; /* bmap btree cursor */
5459 xfs_bmbt_irec_t del
; /* extent being deleted */
5460 int eof
; /* is deleting at eof */
5461 xfs_bmbt_rec_host_t
*ep
; /* extent record pointer */
5462 int error
; /* error return value */
5463 xfs_extnum_t extno
; /* extent number in list */
5464 xfs_bmbt_irec_t got
; /* current extent record */
5465 xfs_ifork_t
*ifp
; /* inode fork pointer */
5466 int isrt
; /* freeing in rt area */
5467 xfs_extnum_t lastx
; /* last extent index used */
5468 int logflags
; /* transaction logging flags */
5469 xfs_extlen_t mod
; /* rt extent offset */
5470 xfs_mount_t
*mp
; /* mount structure */
5471 xfs_extnum_t nextents
; /* number of file extents */
5472 xfs_bmbt_irec_t prev
; /* previous extent record */
5473 xfs_fileoff_t start
; /* first file offset deleted */
5474 int tmp_logflags
; /* partial logging flags */
5475 int wasdel
; /* was a delayed alloc extent */
5476 int whichfork
; /* data or attribute fork */
5479 trace_xfs_bunmap(ip
, bno
, len
, flags
, _RET_IP_
);
5481 whichfork
= (flags
& XFS_BMAPI_ATTRFORK
) ?
5482 XFS_ATTR_FORK
: XFS_DATA_FORK
;
5483 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5485 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
5486 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)) {
5487 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW
,
5489 return XFS_ERROR(EFSCORRUPTED
);
5492 if (XFS_FORCED_SHUTDOWN(mp
))
5493 return XFS_ERROR(EIO
);
5498 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
5499 (error
= xfs_iread_extents(tp
, ip
, whichfork
)))
5501 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
5502 if (nextents
== 0) {
5506 XFS_STATS_INC(xs_blk_unmap
);
5507 isrt
= (whichfork
== XFS_DATA_FORK
) && XFS_IS_REALTIME_INODE(ip
);
5509 bno
= start
+ len
- 1;
5510 ep
= xfs_bmap_search_extents(ip
, bno
, whichfork
, &eof
, &lastx
, &got
,
5514 * Check to see if the given block number is past the end of the
5515 * file, back up to the last block if so...
5518 ep
= xfs_iext_get_ext(ifp
, --lastx
);
5519 xfs_bmbt_get_all(ep
, &got
);
5520 bno
= got
.br_startoff
+ got
.br_blockcount
- 1;
5523 if (ifp
->if_flags
& XFS_IFBROOT
) {
5524 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
);
5525 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
5526 cur
->bc_private
.b
.firstblock
= *firstblock
;
5527 cur
->bc_private
.b
.flist
= flist
;
5528 cur
->bc_private
.b
.flags
= 0;
5534 * Synchronize by locking the bitmap inode.
5536 xfs_ilock(mp
->m_rbmip
, XFS_ILOCK_EXCL
);
5537 xfs_trans_ijoin(tp
, mp
->m_rbmip
, XFS_ILOCK_EXCL
);
5541 while (bno
!= (xfs_fileoff_t
)-1 && bno
>= start
&& lastx
>= 0 &&
5542 (nexts
== 0 || extno
< nexts
)) {
5544 * Is the found extent after a hole in which bno lives?
5545 * Just back up to the previous extent, if so.
5547 if (got
.br_startoff
> bno
) {
5550 ep
= xfs_iext_get_ext(ifp
, lastx
);
5551 xfs_bmbt_get_all(ep
, &got
);
5554 * Is the last block of this extent before the range
5555 * we're supposed to delete? If so, we're done.
5557 bno
= XFS_FILEOFF_MIN(bno
,
5558 got
.br_startoff
+ got
.br_blockcount
- 1);
5562 * Then deal with the (possibly delayed) allocated space
5567 wasdel
= isnullstartblock(del
.br_startblock
);
5568 if (got
.br_startoff
< start
) {
5569 del
.br_startoff
= start
;
5570 del
.br_blockcount
-= start
- got
.br_startoff
;
5572 del
.br_startblock
+= start
- got
.br_startoff
;
5574 if (del
.br_startoff
+ del
.br_blockcount
> bno
+ 1)
5575 del
.br_blockcount
= bno
+ 1 - del
.br_startoff
;
5576 sum
= del
.br_startblock
+ del
.br_blockcount
;
5578 (mod
= do_mod(sum
, mp
->m_sb
.sb_rextsize
))) {
5580 * Realtime extent not lined up at the end.
5581 * The extent could have been split into written
5582 * and unwritten pieces, or we could just be
5583 * unmapping part of it. But we can't really
5584 * get rid of part of a realtime extent.
5586 if (del
.br_state
== XFS_EXT_UNWRITTEN
||
5587 !xfs_sb_version_hasextflgbit(&mp
->m_sb
)) {
5589 * This piece is unwritten, or we're not
5590 * using unwritten extents. Skip over it.
5593 bno
-= mod
> del
.br_blockcount
?
5594 del
.br_blockcount
: mod
;
5595 if (bno
< got
.br_startoff
) {
5597 xfs_bmbt_get_all(xfs_iext_get_ext(
5603 * It's written, turn it unwritten.
5604 * This is better than zeroing it.
5606 ASSERT(del
.br_state
== XFS_EXT_NORM
);
5607 ASSERT(xfs_trans_get_block_res(tp
) > 0);
5609 * If this spans a realtime extent boundary,
5610 * chop it back to the start of the one we end at.
5612 if (del
.br_blockcount
> mod
) {
5613 del
.br_startoff
+= del
.br_blockcount
- mod
;
5614 del
.br_startblock
+= del
.br_blockcount
- mod
;
5615 del
.br_blockcount
= mod
;
5617 del
.br_state
= XFS_EXT_UNWRITTEN
;
5618 error
= xfs_bmap_add_extent_unwritten_real(tp
, ip
,
5619 &lastx
, &cur
, &del
, firstblock
, flist
,
5625 if (isrt
&& (mod
= do_mod(del
.br_startblock
, mp
->m_sb
.sb_rextsize
))) {
5627 * Realtime extent is lined up at the end but not
5628 * at the front. We'll get rid of full extents if
5631 mod
= mp
->m_sb
.sb_rextsize
- mod
;
5632 if (del
.br_blockcount
> mod
) {
5633 del
.br_blockcount
-= mod
;
5634 del
.br_startoff
+= mod
;
5635 del
.br_startblock
+= mod
;
5636 } else if ((del
.br_startoff
== start
&&
5637 (del
.br_state
== XFS_EXT_UNWRITTEN
||
5638 xfs_trans_get_block_res(tp
) == 0)) ||
5639 !xfs_sb_version_hasextflgbit(&mp
->m_sb
)) {
5641 * Can't make it unwritten. There isn't
5642 * a full extent here so just skip it.
5644 ASSERT(bno
>= del
.br_blockcount
);
5645 bno
-= del
.br_blockcount
;
5646 if (got
.br_startoff
> bno
) {
5648 ep
= xfs_iext_get_ext(ifp
,
5650 xfs_bmbt_get_all(ep
, &got
);
5654 } else if (del
.br_state
== XFS_EXT_UNWRITTEN
) {
5656 * This one is already unwritten.
5657 * It must have a written left neighbor.
5658 * Unwrite the killed part of that one and
5662 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
,
5664 ASSERT(prev
.br_state
== XFS_EXT_NORM
);
5665 ASSERT(!isnullstartblock(prev
.br_startblock
));
5666 ASSERT(del
.br_startblock
==
5667 prev
.br_startblock
+ prev
.br_blockcount
);
5668 if (prev
.br_startoff
< start
) {
5669 mod
= start
- prev
.br_startoff
;
5670 prev
.br_blockcount
-= mod
;
5671 prev
.br_startblock
+= mod
;
5672 prev
.br_startoff
= start
;
5674 prev
.br_state
= XFS_EXT_UNWRITTEN
;
5676 error
= xfs_bmap_add_extent_unwritten_real(tp
,
5677 ip
, &lastx
, &cur
, &prev
,
5678 firstblock
, flist
, &logflags
);
5683 ASSERT(del
.br_state
== XFS_EXT_NORM
);
5684 del
.br_state
= XFS_EXT_UNWRITTEN
;
5685 error
= xfs_bmap_add_extent_unwritten_real(tp
,
5686 ip
, &lastx
, &cur
, &del
,
5687 firstblock
, flist
, &logflags
);
5694 ASSERT(startblockval(del
.br_startblock
) > 0);
5695 /* Update realtime/data freespace, unreserve quota */
5697 xfs_filblks_t rtexts
;
5699 rtexts
= XFS_FSB_TO_B(mp
, del
.br_blockcount
);
5700 do_div(rtexts
, mp
->m_sb
.sb_rextsize
);
5701 xfs_mod_incore_sb(mp
, XFS_SBS_FREXTENTS
,
5702 (int64_t)rtexts
, 0);
5703 (void)xfs_trans_reserve_quota_nblks(NULL
,
5704 ip
, -((long)del
.br_blockcount
), 0,
5705 XFS_QMOPT_RES_RTBLKS
);
5707 xfs_icsb_modify_counters(mp
, XFS_SBS_FDBLOCKS
,
5708 (int64_t)del
.br_blockcount
, 0);
5709 (void)xfs_trans_reserve_quota_nblks(NULL
,
5710 ip
, -((long)del
.br_blockcount
), 0,
5711 XFS_QMOPT_RES_REGBLKS
);
5713 ip
->i_delayed_blks
-= del
.br_blockcount
;
5715 cur
->bc_private
.b
.flags
|=
5716 XFS_BTCUR_BPRV_WASDEL
;
5718 cur
->bc_private
.b
.flags
&= ~XFS_BTCUR_BPRV_WASDEL
;
5720 * If it's the case where the directory code is running
5721 * with no block reservation, and the deleted block is in
5722 * the middle of its extent, and the resulting insert
5723 * of an extent would cause transformation to btree format,
5724 * then reject it. The calling code will then swap
5725 * blocks around instead.
5726 * We have to do this now, rather than waiting for the
5727 * conversion to btree format, since the transaction
5730 if (!wasdel
&& xfs_trans_get_block_res(tp
) == 0 &&
5731 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
&&
5732 XFS_IFORK_NEXTENTS(ip
, whichfork
) >= /* Note the >= */
5733 XFS_IFORK_MAXEXT(ip
, whichfork
) &&
5734 del
.br_startoff
> got
.br_startoff
&&
5735 del
.br_startoff
+ del
.br_blockcount
<
5736 got
.br_startoff
+ got
.br_blockcount
) {
5737 error
= XFS_ERROR(ENOSPC
);
5740 error
= xfs_bmap_del_extent(ip
, tp
, &lastx
, flist
, cur
, &del
,
5741 &tmp_logflags
, whichfork
);
5742 logflags
|= tmp_logflags
;
5745 bno
= del
.br_startoff
- 1;
5748 * If not done go on to the next (previous) record.
5750 if (bno
!= (xfs_fileoff_t
)-1 && bno
>= start
) {
5752 ep
= xfs_iext_get_ext(ifp
, lastx
);
5753 if (xfs_bmbt_get_startoff(ep
) > bno
) {
5755 ep
= xfs_iext_get_ext(ifp
,
5758 xfs_bmbt_get_all(ep
, &got
);
5763 *done
= bno
== (xfs_fileoff_t
)-1 || bno
< start
|| lastx
< 0;
5766 * Convert to a btree if necessary.
5768 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
5769 ASSERT(cur
== NULL
);
5770 error
= xfs_bmap_extents_to_btree(tp
, ip
, firstblock
, flist
,
5771 &cur
, 0, &tmp_logflags
, whichfork
);
5772 logflags
|= tmp_logflags
;
5777 * transform from btree to extents, give it cur
5779 else if (xfs_bmap_wants_extents(ip
, whichfork
)) {
5780 ASSERT(cur
!= NULL
);
5781 error
= xfs_bmap_btree_to_extents(tp
, ip
, cur
, &tmp_logflags
,
5783 logflags
|= tmp_logflags
;
5788 * transform from extents to local?
5793 * Log everything. Do this after conversion, there's no point in
5794 * logging the extent records if we've converted to btree format.
5796 if ((logflags
& xfs_ilog_fext(whichfork
)) &&
5797 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
5798 logflags
&= ~xfs_ilog_fext(whichfork
);
5799 else if ((logflags
& xfs_ilog_fbroot(whichfork
)) &&
5800 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)
5801 logflags
&= ~xfs_ilog_fbroot(whichfork
);
5803 * Log inode even in the error case, if the transaction
5804 * is dirty we'll need to shut down the filesystem.
5807 xfs_trans_log_inode(tp
, ip
, logflags
);
5810 *firstblock
= cur
->bc_private
.b
.firstblock
;
5811 cur
->bc_private
.b
.allocated
= 0;
5813 xfs_btree_del_cursor(cur
,
5814 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
5820 * returns 1 for success, 0 if we failed to map the extent.
5823 xfs_getbmapx_fix_eof_hole(
5824 xfs_inode_t
*ip
, /* xfs incore inode pointer */
5825 struct getbmapx
*out
, /* output structure */
5826 int prealloced
, /* this is a file with
5827 * preallocated data space */
5828 __int64_t end
, /* last block requested */
5829 xfs_fsblock_t startblock
)
5832 xfs_mount_t
*mp
; /* file system mount point */
5833 xfs_ifork_t
*ifp
; /* inode fork pointer */
5834 xfs_extnum_t lastx
; /* last extent pointer */
5835 xfs_fileoff_t fileblock
;
5837 if (startblock
== HOLESTARTBLOCK
) {
5839 out
->bmv_block
= -1;
5840 fixlen
= XFS_FSB_TO_BB(mp
, XFS_B_TO_FSB(mp
, XFS_ISIZE(ip
)));
5841 fixlen
-= out
->bmv_offset
;
5842 if (prealloced
&& out
->bmv_offset
+ out
->bmv_length
== end
) {
5843 /* Came to hole at EOF. Trim it. */
5846 out
->bmv_length
= fixlen
;
5849 if (startblock
== DELAYSTARTBLOCK
)
5850 out
->bmv_block
= -2;
5852 out
->bmv_block
= xfs_fsb_to_db(ip
, startblock
);
5853 fileblock
= XFS_BB_TO_FSB(ip
->i_mount
, out
->bmv_offset
);
5854 ifp
= XFS_IFORK_PTR(ip
, XFS_DATA_FORK
);
5855 if (xfs_iext_bno_to_ext(ifp
, fileblock
, &lastx
) &&
5856 (lastx
== (ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
))-1))
5857 out
->bmv_oflags
|= BMV_OF_LAST
;
5864 * Get inode's extents as described in bmv, and format for output.
5865 * Calls formatter to fill the user's buffer until all extents
5866 * are mapped, until the passed-in bmv->bmv_count slots have
5867 * been filled, or until the formatter short-circuits the loop,
5868 * if it is tracking filled-in extents on its own.
5870 int /* error code */
5873 struct getbmapx
*bmv
, /* user bmap structure */
5874 xfs_bmap_format_t formatter
, /* format to user */
5875 void *arg
) /* formatter arg */
5877 __int64_t bmvend
; /* last block requested */
5878 int error
= 0; /* return value */
5879 __int64_t fixlen
; /* length for -1 case */
5880 int i
; /* extent number */
5881 int lock
; /* lock state */
5882 xfs_bmbt_irec_t
*map
; /* buffer for user's data */
5883 xfs_mount_t
*mp
; /* file system mount point */
5884 int nex
; /* # of user extents can do */
5885 int nexleft
; /* # of user extents left */
5886 int subnex
; /* # of bmapi's can do */
5887 int nmap
; /* number of map entries */
5888 struct getbmapx
*out
; /* output structure */
5889 int whichfork
; /* data or attr fork */
5890 int prealloced
; /* this is a file with
5891 * preallocated data space */
5892 int iflags
; /* interface flags */
5893 int bmapi_flags
; /* flags for xfs_bmapi */
5897 iflags
= bmv
->bmv_iflags
;
5898 whichfork
= iflags
& BMV_IF_ATTRFORK
? XFS_ATTR_FORK
: XFS_DATA_FORK
;
5900 if (whichfork
== XFS_ATTR_FORK
) {
5901 if (XFS_IFORK_Q(ip
)) {
5902 if (ip
->i_d
.di_aformat
!= XFS_DINODE_FMT_EXTENTS
&&
5903 ip
->i_d
.di_aformat
!= XFS_DINODE_FMT_BTREE
&&
5904 ip
->i_d
.di_aformat
!= XFS_DINODE_FMT_LOCAL
)
5905 return XFS_ERROR(EINVAL
);
5906 } else if (unlikely(
5907 ip
->i_d
.di_aformat
!= 0 &&
5908 ip
->i_d
.di_aformat
!= XFS_DINODE_FMT_EXTENTS
)) {
5909 XFS_ERROR_REPORT("xfs_getbmap", XFS_ERRLEVEL_LOW
,
5911 return XFS_ERROR(EFSCORRUPTED
);
5917 if (ip
->i_d
.di_format
!= XFS_DINODE_FMT_EXTENTS
&&
5918 ip
->i_d
.di_format
!= XFS_DINODE_FMT_BTREE
&&
5919 ip
->i_d
.di_format
!= XFS_DINODE_FMT_LOCAL
)
5920 return XFS_ERROR(EINVAL
);
5922 if (xfs_get_extsz_hint(ip
) ||
5923 ip
->i_d
.di_flags
& (XFS_DIFLAG_PREALLOC
|XFS_DIFLAG_APPEND
)){
5925 fixlen
= mp
->m_super
->s_maxbytes
;
5928 fixlen
= XFS_ISIZE(ip
);
5932 if (bmv
->bmv_length
== -1) {
5933 fixlen
= XFS_FSB_TO_BB(mp
, XFS_B_TO_FSB(mp
, fixlen
));
5935 max_t(__int64_t
, fixlen
- bmv
->bmv_offset
, 0);
5936 } else if (bmv
->bmv_length
== 0) {
5937 bmv
->bmv_entries
= 0;
5939 } else if (bmv
->bmv_length
< 0) {
5940 return XFS_ERROR(EINVAL
);
5943 nex
= bmv
->bmv_count
- 1;
5945 return XFS_ERROR(EINVAL
);
5946 bmvend
= bmv
->bmv_offset
+ bmv
->bmv_length
;
5949 if (bmv
->bmv_count
> ULONG_MAX
/ sizeof(struct getbmapx
))
5950 return XFS_ERROR(ENOMEM
);
5951 out
= kmem_zalloc(bmv
->bmv_count
* sizeof(struct getbmapx
), KM_MAYFAIL
);
5953 out
= kmem_zalloc_large(bmv
->bmv_count
*
5954 sizeof(struct getbmapx
));
5956 return XFS_ERROR(ENOMEM
);
5959 xfs_ilock(ip
, XFS_IOLOCK_SHARED
);
5960 if (whichfork
== XFS_DATA_FORK
&& !(iflags
& BMV_IF_DELALLOC
)) {
5961 if (ip
->i_delayed_blks
|| XFS_ISIZE(ip
) > ip
->i_d
.di_size
) {
5962 error
= -filemap_write_and_wait(VFS_I(ip
)->i_mapping
);
5964 goto out_unlock_iolock
;
5967 * even after flushing the inode, there can still be delalloc
5968 * blocks on the inode beyond EOF due to speculative
5969 * preallocation. These are not removed until the release
5970 * function is called or the inode is inactivated. Hence we
5971 * cannot assert here that ip->i_delayed_blks == 0.
5975 lock
= xfs_ilock_map_shared(ip
);
5978 * Don't let nex be bigger than the number of extents
5979 * we can have assuming alternating holes and real extents.
5981 if (nex
> XFS_IFORK_NEXTENTS(ip
, whichfork
) * 2 + 1)
5982 nex
= XFS_IFORK_NEXTENTS(ip
, whichfork
) * 2 + 1;
5984 bmapi_flags
= xfs_bmapi_aflag(whichfork
);
5985 if (!(iflags
& BMV_IF_PREALLOC
))
5986 bmapi_flags
|= XFS_BMAPI_IGSTATE
;
5989 * Allocate enough space to handle "subnex" maps at a time.
5993 map
= kmem_alloc(subnex
* sizeof(*map
), KM_MAYFAIL
| KM_NOFS
);
5995 goto out_unlock_ilock
;
5997 bmv
->bmv_entries
= 0;
5999 if (XFS_IFORK_NEXTENTS(ip
, whichfork
) == 0 &&
6000 (whichfork
== XFS_ATTR_FORK
|| !(iflags
& BMV_IF_DELALLOC
))) {
6008 nmap
= (nexleft
> subnex
) ? subnex
: nexleft
;
6009 error
= xfs_bmapi_read(ip
, XFS_BB_TO_FSBT(mp
, bmv
->bmv_offset
),
6010 XFS_BB_TO_FSB(mp
, bmv
->bmv_length
),
6011 map
, &nmap
, bmapi_flags
);
6014 ASSERT(nmap
<= subnex
);
6016 for (i
= 0; i
< nmap
&& nexleft
&& bmv
->bmv_length
; i
++) {
6017 out
[cur_ext
].bmv_oflags
= 0;
6018 if (map
[i
].br_state
== XFS_EXT_UNWRITTEN
)
6019 out
[cur_ext
].bmv_oflags
|= BMV_OF_PREALLOC
;
6020 else if (map
[i
].br_startblock
== DELAYSTARTBLOCK
)
6021 out
[cur_ext
].bmv_oflags
|= BMV_OF_DELALLOC
;
6022 out
[cur_ext
].bmv_offset
=
6023 XFS_FSB_TO_BB(mp
, map
[i
].br_startoff
);
6024 out
[cur_ext
].bmv_length
=
6025 XFS_FSB_TO_BB(mp
, map
[i
].br_blockcount
);
6026 out
[cur_ext
].bmv_unused1
= 0;
6027 out
[cur_ext
].bmv_unused2
= 0;
6030 * delayed allocation extents that start beyond EOF can
6031 * occur due to speculative EOF allocation when the
6032 * delalloc extent is larger than the largest freespace
6033 * extent at conversion time. These extents cannot be
6034 * converted by data writeback, so can exist here even
6035 * if we are not supposed to be finding delalloc
6038 if (map
[i
].br_startblock
== DELAYSTARTBLOCK
&&
6039 map
[i
].br_startoff
<= XFS_B_TO_FSB(mp
, XFS_ISIZE(ip
)))
6040 ASSERT((iflags
& BMV_IF_DELALLOC
) != 0);
6042 if (map
[i
].br_startblock
== HOLESTARTBLOCK
&&
6043 whichfork
== XFS_ATTR_FORK
) {
6044 /* came to the end of attribute fork */
6045 out
[cur_ext
].bmv_oflags
|= BMV_OF_LAST
;
6049 if (!xfs_getbmapx_fix_eof_hole(ip
, &out
[cur_ext
],
6051 map
[i
].br_startblock
))
6055 out
[cur_ext
].bmv_offset
+
6056 out
[cur_ext
].bmv_length
;
6058 max_t(__int64_t
, 0, bmvend
- bmv
->bmv_offset
);
6061 * In case we don't want to return the hole,
6062 * don't increase cur_ext so that we can reuse
6063 * it in the next loop.
6065 if ((iflags
& BMV_IF_NO_HOLES
) &&
6066 map
[i
].br_startblock
== HOLESTARTBLOCK
) {
6067 memset(&out
[cur_ext
], 0, sizeof(out
[cur_ext
]));
6075 } while (nmap
&& nexleft
&& bmv
->bmv_length
);
6080 xfs_iunlock_map_shared(ip
, lock
);
6082 xfs_iunlock(ip
, XFS_IOLOCK_SHARED
);
6084 for (i
= 0; i
< cur_ext
; i
++) {
6085 int full
= 0; /* user array is full */
6087 /* format results & advance arg */
6088 error
= formatter(&arg
, &out
[i
], &full
);
6093 if (is_vmalloc_addr(out
))
6094 kmem_free_large(out
);
6101 * dead simple method of punching delalyed allocation blocks from a range in
6102 * the inode. Walks a block at a time so will be slow, but is only executed in
6103 * rare error cases so the overhead is not critical. This will alays punch out
6104 * both the start and end blocks, even if the ranges only partially overlap
6105 * them, so it is up to the caller to ensure that partial blocks are not
6109 xfs_bmap_punch_delalloc_range(
6110 struct xfs_inode
*ip
,
6111 xfs_fileoff_t start_fsb
,
6112 xfs_fileoff_t length
)
6114 xfs_fileoff_t remaining
= length
;
6117 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
6121 xfs_bmbt_irec_t imap
;
6123 xfs_fsblock_t firstblock
;
6124 xfs_bmap_free_t flist
;
6127 * Map the range first and check that it is a delalloc extent
6128 * before trying to unmap the range. Otherwise we will be
6129 * trying to remove a real extent (which requires a
6130 * transaction) or a hole, which is probably a bad idea...
6132 error
= xfs_bmapi_read(ip
, start_fsb
, 1, &imap
, &nimaps
,
6136 /* something screwed, just bail */
6137 if (!XFS_FORCED_SHUTDOWN(ip
->i_mount
)) {
6138 xfs_alert(ip
->i_mount
,
6139 "Failed delalloc mapping lookup ino %lld fsb %lld.",
6140 ip
->i_ino
, start_fsb
);
6148 if (imap
.br_startblock
!= DELAYSTARTBLOCK
) {
6149 /* been converted, ignore */
6152 WARN_ON(imap
.br_blockcount
== 0);
6155 * Note: while we initialise the firstblock/flist pair, they
6156 * should never be used because blocks should never be
6157 * allocated or freed for a delalloc extent and hence we need
6158 * don't cancel or finish them after the xfs_bunmapi() call.
6160 xfs_bmap_init(&flist
, &firstblock
);
6161 error
= xfs_bunmapi(NULL
, ip
, start_fsb
, 1, 0, 1, &firstblock
,
6166 ASSERT(!flist
.xbf_count
&& !flist
.xbf_first
);
6170 } while(remaining
> 0);