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
, ip
->i_d
.di_version
) -
232 XFS_BMDR_SPACE_CALC(MINABTPTRS
);
234 offset
= XFS_BMDR_SPACE_CALC(6 * MINABTPTRS
);
237 ASSERT(offset
< XFS_LITINO(mp
, ip
->i_d
.di_version
));
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_CRC_MAGIC
) &&
443 block
->bb_magic
!= cpu_to_be32(XFS_BMAP_MAGIC
))
446 if (be16_to_cpu(block
->bb_level
) != level
||
447 be16_to_cpu(block
->bb_numrecs
) == 0 ||
448 be16_to_cpu(block
->bb_numrecs
) > mp
->m_bmap_dmxr
[level
!= 0])
455 STATIC
struct xfs_buf
*
457 struct xfs_btree_cur
*cur
,
460 struct xfs_log_item_desc
*lidp
;
466 for (i
= 0; i
< XFS_BTREE_MAXLEVELS
; i
++) {
467 if (!cur
->bc_bufs
[i
])
469 if (XFS_BUF_ADDR(cur
->bc_bufs
[i
]) == bno
)
470 return cur
->bc_bufs
[i
];
473 /* Chase down all the log items to see if the bp is there */
474 list_for_each_entry(lidp
, &cur
->bc_tp
->t_items
, lid_trans
) {
475 struct xfs_buf_log_item
*bip
;
476 bip
= (struct xfs_buf_log_item
*)lidp
->lid_item
;
477 if (bip
->bli_item
.li_type
== XFS_LI_BUF
&&
478 XFS_BUF_ADDR(bip
->bli_buf
) == bno
)
487 struct xfs_btree_block
*block
,
493 __be64
*pp
, *thispa
; /* pointer to block address */
494 xfs_bmbt_key_t
*prevp
, *keyp
;
496 ASSERT(be16_to_cpu(block
->bb_level
) > 0);
499 for( i
= 1; i
<= xfs_btree_get_numrecs(block
); i
++) {
500 dmxr
= mp
->m_bmap_dmxr
[0];
501 keyp
= XFS_BMBT_KEY_ADDR(mp
, block
, i
);
504 ASSERT(be64_to_cpu(prevp
->br_startoff
) <
505 be64_to_cpu(keyp
->br_startoff
));
510 * Compare the block numbers to see if there are dups.
513 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, i
, sz
);
515 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, i
, dmxr
);
517 for (j
= i
+1; j
<= be16_to_cpu(block
->bb_numrecs
); j
++) {
519 thispa
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, j
, sz
);
521 thispa
= XFS_BMBT_PTR_ADDR(mp
, block
, j
, dmxr
);
522 if (*thispa
== *pp
) {
523 xfs_warn(mp
, "%s: thispa(%d) == pp(%d) %Ld",
525 (unsigned long long)be64_to_cpu(*thispa
));
526 panic("%s: ptrs are equal in node\n",
534 * Check that the extents for the inode ip are in the right order in all
539 xfs_bmap_check_leaf_extents(
540 xfs_btree_cur_t
*cur
, /* btree cursor or null */
541 xfs_inode_t
*ip
, /* incore inode pointer */
542 int whichfork
) /* data or attr fork */
544 struct xfs_btree_block
*block
; /* current btree block */
545 xfs_fsblock_t bno
; /* block # of "block" */
546 xfs_buf_t
*bp
; /* buffer for "block" */
547 int error
; /* error return value */
548 xfs_extnum_t i
=0, j
; /* index into the extents list */
549 xfs_ifork_t
*ifp
; /* fork structure */
550 int level
; /* btree level, for checking */
551 xfs_mount_t
*mp
; /* file system mount structure */
552 __be64
*pp
; /* pointer to block address */
553 xfs_bmbt_rec_t
*ep
; /* pointer to current extent */
554 xfs_bmbt_rec_t last
= {0, 0}; /* last extent in prev block */
555 xfs_bmbt_rec_t
*nextp
; /* pointer to next extent */
558 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
) {
564 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
565 block
= ifp
->if_broot
;
567 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
569 level
= be16_to_cpu(block
->bb_level
);
571 xfs_check_block(block
, mp
, 1, ifp
->if_broot_bytes
);
572 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, 1, ifp
->if_broot_bytes
);
573 bno
= be64_to_cpu(*pp
);
575 ASSERT(bno
!= NULLDFSBNO
);
576 ASSERT(XFS_FSB_TO_AGNO(mp
, bno
) < mp
->m_sb
.sb_agcount
);
577 ASSERT(XFS_FSB_TO_AGBNO(mp
, bno
) < mp
->m_sb
.sb_agblocks
);
580 * Go down the tree until leaf level is reached, following the first
581 * pointer (leftmost) at each level.
583 while (level
-- > 0) {
584 /* See if buf is in cur first */
586 bp
= xfs_bmap_get_bp(cur
, XFS_FSB_TO_DADDR(mp
, bno
));
589 error
= xfs_btree_read_bufl(mp
, NULL
, bno
, 0, &bp
,
595 block
= XFS_BUF_TO_BLOCK(bp
);
596 XFS_WANT_CORRUPTED_GOTO(
597 xfs_bmap_sanity_check(mp
, bp
, level
),
603 * Check this block for basic sanity (increasing keys and
604 * no duplicate blocks).
607 xfs_check_block(block
, mp
, 0, 0);
608 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, mp
->m_bmap_dmxr
[1]);
609 bno
= be64_to_cpu(*pp
);
610 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp
, bno
), error0
);
613 xfs_trans_brelse(NULL
, bp
);
618 * Here with bp and block set to the leftmost leaf node in the tree.
623 * Loop over all leaf nodes checking that all extents are in the right order.
626 xfs_fsblock_t nextbno
;
627 xfs_extnum_t num_recs
;
630 num_recs
= xfs_btree_get_numrecs(block
);
633 * Read-ahead the next leaf block, if any.
636 nextbno
= be64_to_cpu(block
->bb_u
.l
.bb_rightsib
);
639 * Check all the extents to make sure they are OK.
640 * If we had a previous block, the last entry should
641 * conform with the first entry in this one.
644 ep
= XFS_BMBT_REC_ADDR(mp
, block
, 1);
646 ASSERT(xfs_bmbt_disk_get_startoff(&last
) +
647 xfs_bmbt_disk_get_blockcount(&last
) <=
648 xfs_bmbt_disk_get_startoff(ep
));
650 for (j
= 1; j
< num_recs
; j
++) {
651 nextp
= XFS_BMBT_REC_ADDR(mp
, block
, j
+ 1);
652 ASSERT(xfs_bmbt_disk_get_startoff(ep
) +
653 xfs_bmbt_disk_get_blockcount(ep
) <=
654 xfs_bmbt_disk_get_startoff(nextp
));
662 xfs_trans_brelse(NULL
, bp
);
666 * If we've reached the end, stop.
668 if (bno
== NULLFSBLOCK
)
672 bp
= xfs_bmap_get_bp(cur
, XFS_FSB_TO_DADDR(mp
, bno
));
675 error
= xfs_btree_read_bufl(mp
, NULL
, bno
, 0, &bp
,
681 block
= XFS_BUF_TO_BLOCK(bp
);
685 xfs_trans_brelse(NULL
, bp
);
690 xfs_warn(mp
, "%s: at error0", __func__
);
692 xfs_trans_brelse(NULL
, bp
);
694 xfs_warn(mp
, "%s: BAD after btree leaves for %d extents",
696 panic("%s: CORRUPTED BTREE OR SOMETHING", __func__
);
701 * Add bmap trace insert entries for all the contents of the extent records.
704 xfs_bmap_trace_exlist(
705 xfs_inode_t
*ip
, /* incore inode pointer */
706 xfs_extnum_t cnt
, /* count of entries in the list */
707 int whichfork
, /* data or attr fork */
708 unsigned long caller_ip
)
710 xfs_extnum_t idx
; /* extent record index */
711 xfs_ifork_t
*ifp
; /* inode fork pointer */
714 if (whichfork
== XFS_ATTR_FORK
)
715 state
|= BMAP_ATTRFORK
;
717 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
718 ASSERT(cnt
== (ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
)));
719 for (idx
= 0; idx
< cnt
; idx
++)
720 trace_xfs_extlist(ip
, idx
, whichfork
, caller_ip
);
724 * Validate that the bmbt_irecs being returned from bmapi are valid
725 * given the callers original parameters. Specifically check the
726 * ranges of the returned irecs to ensure that they only extent beyond
727 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
730 xfs_bmap_validate_ret(
734 xfs_bmbt_irec_t
*mval
,
738 int i
; /* index to map values */
740 ASSERT(ret_nmap
<= nmap
);
742 for (i
= 0; i
< ret_nmap
; i
++) {
743 ASSERT(mval
[i
].br_blockcount
> 0);
744 if (!(flags
& XFS_BMAPI_ENTIRE
)) {
745 ASSERT(mval
[i
].br_startoff
>= bno
);
746 ASSERT(mval
[i
].br_blockcount
<= len
);
747 ASSERT(mval
[i
].br_startoff
+ mval
[i
].br_blockcount
<=
750 ASSERT(mval
[i
].br_startoff
< bno
+ len
);
751 ASSERT(mval
[i
].br_startoff
+ mval
[i
].br_blockcount
>
755 mval
[i
- 1].br_startoff
+ mval
[i
- 1].br_blockcount
==
756 mval
[i
].br_startoff
);
757 ASSERT(mval
[i
].br_startblock
!= DELAYSTARTBLOCK
&&
758 mval
[i
].br_startblock
!= HOLESTARTBLOCK
);
759 ASSERT(mval
[i
].br_state
== XFS_EXT_NORM
||
760 mval
[i
].br_state
== XFS_EXT_UNWRITTEN
);
765 #define xfs_bmap_check_leaf_extents(cur, ip, whichfork) do { } while (0)
766 #define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap)
770 * bmap free list manipulation functions
774 * Add the extent to the list of extents to be free at transaction end.
775 * The list is maintained sorted (by block number).
779 xfs_fsblock_t bno
, /* fs block number of extent */
780 xfs_filblks_t len
, /* length of extent */
781 xfs_bmap_free_t
*flist
, /* list of extents */
782 xfs_mount_t
*mp
) /* mount point structure */
784 xfs_bmap_free_item_t
*cur
; /* current (next) element */
785 xfs_bmap_free_item_t
*new; /* new element */
786 xfs_bmap_free_item_t
*prev
; /* previous element */
791 ASSERT(bno
!= NULLFSBLOCK
);
793 ASSERT(len
<= MAXEXTLEN
);
794 ASSERT(!isnullstartblock(bno
));
795 agno
= XFS_FSB_TO_AGNO(mp
, bno
);
796 agbno
= XFS_FSB_TO_AGBNO(mp
, bno
);
797 ASSERT(agno
< mp
->m_sb
.sb_agcount
);
798 ASSERT(agbno
< mp
->m_sb
.sb_agblocks
);
799 ASSERT(len
< mp
->m_sb
.sb_agblocks
);
800 ASSERT(agbno
+ len
<= mp
->m_sb
.sb_agblocks
);
802 ASSERT(xfs_bmap_free_item_zone
!= NULL
);
803 new = kmem_zone_alloc(xfs_bmap_free_item_zone
, KM_SLEEP
);
804 new->xbfi_startblock
= bno
;
805 new->xbfi_blockcount
= (xfs_extlen_t
)len
;
806 for (prev
= NULL
, cur
= flist
->xbf_first
;
808 prev
= cur
, cur
= cur
->xbfi_next
) {
809 if (cur
->xbfi_startblock
>= bno
)
813 prev
->xbfi_next
= new;
815 flist
->xbf_first
= new;
816 new->xbfi_next
= cur
;
821 * Remove the entry "free" from the free item list. Prev points to the
822 * previous entry, unless "free" is the head of the list.
826 xfs_bmap_free_t
*flist
, /* free item list header */
827 xfs_bmap_free_item_t
*prev
, /* previous item on list, if any */
828 xfs_bmap_free_item_t
*free
) /* list item to be freed */
831 prev
->xbfi_next
= free
->xbfi_next
;
833 flist
->xbf_first
= free
->xbfi_next
;
835 kmem_zone_free(xfs_bmap_free_item_zone
, free
);
840 * Routine to be called at transaction's end by xfs_bmapi, xfs_bunmapi
841 * caller. Frees all the extents that need freeing, which must be done
842 * last due to locking considerations. We never free any extents in
843 * the first transaction.
845 * Return 1 if the given transaction was committed and a new one
846 * started, and 0 otherwise in the committed parameter.
850 xfs_trans_t
**tp
, /* transaction pointer addr */
851 xfs_bmap_free_t
*flist
, /* i/o: list extents to free */
852 int *committed
) /* xact committed or not */
854 xfs_efd_log_item_t
*efd
; /* extent free data */
855 xfs_efi_log_item_t
*efi
; /* extent free intention */
856 int error
; /* error return value */
857 xfs_bmap_free_item_t
*free
; /* free extent item */
858 unsigned int logres
; /* new log reservation */
859 unsigned int logcount
; /* new log count */
860 xfs_mount_t
*mp
; /* filesystem mount structure */
861 xfs_bmap_free_item_t
*next
; /* next item on free list */
862 xfs_trans_t
*ntp
; /* new transaction pointer */
864 ASSERT((*tp
)->t_flags
& XFS_TRANS_PERM_LOG_RES
);
865 if (flist
->xbf_count
== 0) {
870 efi
= xfs_trans_get_efi(ntp
, flist
->xbf_count
);
871 for (free
= flist
->xbf_first
; free
; free
= free
->xbfi_next
)
872 xfs_trans_log_efi_extent(ntp
, efi
, free
->xbfi_startblock
,
873 free
->xbfi_blockcount
);
874 logres
= ntp
->t_log_res
;
875 logcount
= ntp
->t_log_count
;
876 ntp
= xfs_trans_dup(*tp
);
877 error
= xfs_trans_commit(*tp
, 0);
881 * We have a new transaction, so we should return committed=1,
882 * even though we're returning an error.
888 * transaction commit worked ok so we can drop the extra ticket
889 * reference that we gained in xfs_trans_dup()
891 xfs_log_ticket_put(ntp
->t_ticket
);
893 if ((error
= xfs_trans_reserve(ntp
, 0, logres
, 0, XFS_TRANS_PERM_LOG_RES
,
896 efd
= xfs_trans_get_efd(ntp
, efi
, flist
->xbf_count
);
897 for (free
= flist
->xbf_first
; free
!= NULL
; free
= next
) {
898 next
= free
->xbfi_next
;
899 if ((error
= xfs_free_extent(ntp
, free
->xbfi_startblock
,
900 free
->xbfi_blockcount
))) {
902 * The bmap free list will be cleaned up at a
903 * higher level. The EFI will be canceled when
904 * this transaction is aborted.
905 * Need to force shutdown here to make sure it
906 * happens, since this transaction may not be
910 if (!XFS_FORCED_SHUTDOWN(mp
))
911 xfs_force_shutdown(mp
,
912 (error
== EFSCORRUPTED
) ?
913 SHUTDOWN_CORRUPT_INCORE
:
914 SHUTDOWN_META_IO_ERROR
);
917 xfs_trans_log_efd_extent(ntp
, efd
, free
->xbfi_startblock
,
918 free
->xbfi_blockcount
);
919 xfs_bmap_del_free(flist
, NULL
, free
);
925 * Free up any items left in the list.
929 xfs_bmap_free_t
*flist
) /* list of bmap_free_items */
931 xfs_bmap_free_item_t
*free
; /* free list item */
932 xfs_bmap_free_item_t
*next
;
934 if (flist
->xbf_count
== 0)
936 ASSERT(flist
->xbf_first
!= NULL
);
937 for (free
= flist
->xbf_first
; free
; free
= next
) {
938 next
= free
->xbfi_next
;
939 xfs_bmap_del_free(flist
, NULL
, free
);
941 ASSERT(flist
->xbf_count
== 0);
945 * Inode fork format manipulation functions
949 * Transform a btree format file with only one leaf node, where the
950 * extents list will fit in the inode, into an extents format file.
951 * Since the file extents are already in-core, all we have to do is
952 * give up the space for the btree root and pitch the leaf block.
954 STATIC
int /* error */
955 xfs_bmap_btree_to_extents(
956 xfs_trans_t
*tp
, /* transaction pointer */
957 xfs_inode_t
*ip
, /* incore inode pointer */
958 xfs_btree_cur_t
*cur
, /* btree cursor */
959 int *logflagsp
, /* inode logging flags */
960 int whichfork
) /* data or attr fork */
963 struct xfs_btree_block
*cblock
;/* child btree block */
964 xfs_fsblock_t cbno
; /* child block number */
965 xfs_buf_t
*cbp
; /* child block's buffer */
966 int error
; /* error return value */
967 xfs_ifork_t
*ifp
; /* inode fork data */
968 xfs_mount_t
*mp
; /* mount point structure */
969 __be64
*pp
; /* ptr to block address */
970 struct xfs_btree_block
*rblock
;/* root btree block */
973 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
974 ASSERT(ifp
->if_flags
& XFS_IFEXTENTS
);
975 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
);
976 rblock
= ifp
->if_broot
;
977 ASSERT(be16_to_cpu(rblock
->bb_level
) == 1);
978 ASSERT(be16_to_cpu(rblock
->bb_numrecs
) == 1);
979 ASSERT(xfs_bmbt_maxrecs(mp
, ifp
->if_broot_bytes
, 0) == 1);
980 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, rblock
, 1, ifp
->if_broot_bytes
);
981 cbno
= be64_to_cpu(*pp
);
984 if ((error
= xfs_btree_check_lptr(cur
, cbno
, 1)))
987 error
= xfs_btree_read_bufl(mp
, tp
, cbno
, 0, &cbp
, XFS_BMAP_BTREE_REF
,
991 cblock
= XFS_BUF_TO_BLOCK(cbp
);
992 if ((error
= xfs_btree_check_block(cur
, cblock
, 0, cbp
)))
994 xfs_bmap_add_free(cbno
, 1, cur
->bc_private
.b
.flist
, mp
);
995 ip
->i_d
.di_nblocks
--;
996 xfs_trans_mod_dquot_byino(tp
, ip
, XFS_TRANS_DQ_BCOUNT
, -1L);
997 xfs_trans_binval(tp
, cbp
);
998 if (cur
->bc_bufs
[0] == cbp
)
999 cur
->bc_bufs
[0] = NULL
;
1000 xfs_iroot_realloc(ip
, -1, whichfork
);
1001 ASSERT(ifp
->if_broot
== NULL
);
1002 ASSERT((ifp
->if_flags
& XFS_IFBROOT
) == 0);
1003 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_EXTENTS
);
1004 *logflagsp
= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
1009 * Convert an extents-format file into a btree-format file.
1010 * The new file will have a root block (in the inode) and a single child block.
1012 STATIC
int /* error */
1013 xfs_bmap_extents_to_btree(
1014 xfs_trans_t
*tp
, /* transaction pointer */
1015 xfs_inode_t
*ip
, /* incore inode pointer */
1016 xfs_fsblock_t
*firstblock
, /* first-block-allocated */
1017 xfs_bmap_free_t
*flist
, /* blocks freed in xaction */
1018 xfs_btree_cur_t
**curp
, /* cursor returned to caller */
1019 int wasdel
, /* converting a delayed alloc */
1020 int *logflagsp
, /* inode logging flags */
1021 int whichfork
) /* data or attr fork */
1023 struct xfs_btree_block
*ablock
; /* allocated (child) bt block */
1024 xfs_buf_t
*abp
; /* buffer for ablock */
1025 xfs_alloc_arg_t args
; /* allocation arguments */
1026 xfs_bmbt_rec_t
*arp
; /* child record pointer */
1027 struct xfs_btree_block
*block
; /* btree root block */
1028 xfs_btree_cur_t
*cur
; /* bmap btree cursor */
1029 xfs_bmbt_rec_host_t
*ep
; /* extent record pointer */
1030 int error
; /* error return value */
1031 xfs_extnum_t i
, cnt
; /* extent record index */
1032 xfs_ifork_t
*ifp
; /* inode fork pointer */
1033 xfs_bmbt_key_t
*kp
; /* root block key pointer */
1034 xfs_mount_t
*mp
; /* mount structure */
1035 xfs_extnum_t nextents
; /* number of file extents */
1036 xfs_bmbt_ptr_t
*pp
; /* root block address pointer */
1039 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1040 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
);
1043 * Make space in the inode incore.
1045 xfs_iroot_realloc(ip
, 1, whichfork
);
1046 ifp
->if_flags
|= XFS_IFBROOT
;
1051 block
= ifp
->if_broot
;
1052 if (xfs_sb_version_hascrc(&mp
->m_sb
))
1053 xfs_btree_init_block_int(mp
, block
, XFS_BUF_DADDR_NULL
,
1054 XFS_BMAP_CRC_MAGIC
, 1, 1, ip
->i_ino
,
1055 XFS_BTREE_LONG_PTRS
| XFS_BTREE_CRC_BLOCKS
);
1057 xfs_btree_init_block_int(mp
, block
, XFS_BUF_DADDR_NULL
,
1058 XFS_BMAP_MAGIC
, 1, 1, ip
->i_ino
,
1059 XFS_BTREE_LONG_PTRS
);
1062 * Need a cursor. Can't allocate until bb_level is filled in.
1064 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
1065 cur
->bc_private
.b
.firstblock
= *firstblock
;
1066 cur
->bc_private
.b
.flist
= flist
;
1067 cur
->bc_private
.b
.flags
= wasdel
? XFS_BTCUR_BPRV_WASDEL
: 0;
1069 * Convert to a btree with two levels, one record in root.
1071 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_BTREE
);
1072 memset(&args
, 0, sizeof(args
));
1075 args
.firstblock
= *firstblock
;
1076 if (*firstblock
== NULLFSBLOCK
) {
1077 args
.type
= XFS_ALLOCTYPE_START_BNO
;
1078 args
.fsbno
= XFS_INO_TO_FSB(mp
, ip
->i_ino
);
1079 } else if (flist
->xbf_low
) {
1080 args
.type
= XFS_ALLOCTYPE_START_BNO
;
1081 args
.fsbno
= *firstblock
;
1083 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
1084 args
.fsbno
= *firstblock
;
1086 args
.minlen
= args
.maxlen
= args
.prod
= 1;
1087 args
.wasdel
= wasdel
;
1089 if ((error
= xfs_alloc_vextent(&args
))) {
1090 xfs_iroot_realloc(ip
, -1, whichfork
);
1091 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
1095 * Allocation can't fail, the space was reserved.
1097 ASSERT(args
.fsbno
!= NULLFSBLOCK
);
1098 ASSERT(*firstblock
== NULLFSBLOCK
||
1099 args
.agno
== XFS_FSB_TO_AGNO(mp
, *firstblock
) ||
1101 args
.agno
> XFS_FSB_TO_AGNO(mp
, *firstblock
)));
1102 *firstblock
= cur
->bc_private
.b
.firstblock
= args
.fsbno
;
1103 cur
->bc_private
.b
.allocated
++;
1104 ip
->i_d
.di_nblocks
++;
1105 xfs_trans_mod_dquot_byino(tp
, ip
, XFS_TRANS_DQ_BCOUNT
, 1L);
1106 abp
= xfs_btree_get_bufl(mp
, tp
, args
.fsbno
, 0);
1108 * Fill in the child block.
1110 abp
->b_ops
= &xfs_bmbt_buf_ops
;
1111 ablock
= XFS_BUF_TO_BLOCK(abp
);
1112 if (xfs_sb_version_hascrc(&mp
->m_sb
))
1113 xfs_btree_init_block_int(mp
, ablock
, abp
->b_bn
,
1114 XFS_BMAP_CRC_MAGIC
, 0, 0, ip
->i_ino
,
1115 XFS_BTREE_LONG_PTRS
| XFS_BTREE_CRC_BLOCKS
);
1117 xfs_btree_init_block_int(mp
, ablock
, abp
->b_bn
,
1118 XFS_BMAP_MAGIC
, 0, 0, ip
->i_ino
,
1119 XFS_BTREE_LONG_PTRS
);
1121 arp
= XFS_BMBT_REC_ADDR(mp
, ablock
, 1);
1122 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
1123 for (cnt
= i
= 0; i
< nextents
; i
++) {
1124 ep
= xfs_iext_get_ext(ifp
, i
);
1125 if (!isnullstartblock(xfs_bmbt_get_startblock(ep
))) {
1126 arp
->l0
= cpu_to_be64(ep
->l0
);
1127 arp
->l1
= cpu_to_be64(ep
->l1
);
1131 ASSERT(cnt
== XFS_IFORK_NEXTENTS(ip
, whichfork
));
1132 xfs_btree_set_numrecs(ablock
, cnt
);
1135 * Fill in the root key and pointer.
1137 kp
= XFS_BMBT_KEY_ADDR(mp
, block
, 1);
1138 arp
= XFS_BMBT_REC_ADDR(mp
, ablock
, 1);
1139 kp
->br_startoff
= cpu_to_be64(xfs_bmbt_disk_get_startoff(arp
));
1140 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, xfs_bmbt_get_maxrecs(cur
,
1141 be16_to_cpu(block
->bb_level
)));
1142 *pp
= cpu_to_be64(args
.fsbno
);
1145 * Do all this logging at the end so that
1146 * the root is at the right level.
1148 xfs_btree_log_block(cur
, abp
, XFS_BB_ALL_BITS
);
1149 xfs_btree_log_recs(cur
, abp
, 1, be16_to_cpu(ablock
->bb_numrecs
));
1150 ASSERT(*curp
== NULL
);
1152 *logflagsp
= XFS_ILOG_CORE
| xfs_ilog_fbroot(whichfork
);
1157 * Convert a local file to an extents file.
1158 * This code is out of bounds for data forks of regular files,
1159 * since the file data needs to get logged so things will stay consistent.
1160 * (The bmap-level manipulations are ok, though).
1162 STATIC
int /* error */
1163 xfs_bmap_local_to_extents(
1164 xfs_trans_t
*tp
, /* transaction pointer */
1165 xfs_inode_t
*ip
, /* incore inode pointer */
1166 xfs_fsblock_t
*firstblock
, /* first block allocated in xaction */
1167 xfs_extlen_t total
, /* total blocks needed by transaction */
1168 int *logflagsp
, /* inode logging flags */
1170 void (*init_fn
)(struct xfs_trans
*tp
,
1172 struct xfs_inode
*ip
,
1173 struct xfs_ifork
*ifp
))
1175 int error
; /* error return value */
1176 int flags
; /* logging flags returned */
1177 xfs_ifork_t
*ifp
; /* inode fork pointer */
1180 * We don't want to deal with the case of keeping inode data inline yet.
1181 * So sending the data fork of a regular inode is invalid.
1183 ASSERT(!(S_ISREG(ip
->i_d
.di_mode
) && whichfork
== XFS_DATA_FORK
));
1184 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1185 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
1188 if (ifp
->if_bytes
) {
1189 xfs_alloc_arg_t args
; /* allocation arguments */
1190 xfs_buf_t
*bp
; /* buffer for extent block */
1191 xfs_bmbt_rec_host_t
*ep
;/* extent record pointer */
1193 ASSERT((ifp
->if_flags
&
1194 (XFS_IFINLINE
|XFS_IFEXTENTS
|XFS_IFEXTIREC
)) == XFS_IFINLINE
);
1195 memset(&args
, 0, sizeof(args
));
1197 args
.mp
= ip
->i_mount
;
1198 args
.firstblock
= *firstblock
;
1200 * Allocate a block. We know we need only one, since the
1201 * file currently fits in an inode.
1203 if (*firstblock
== NULLFSBLOCK
) {
1204 args
.fsbno
= XFS_INO_TO_FSB(args
.mp
, ip
->i_ino
);
1205 args
.type
= XFS_ALLOCTYPE_START_BNO
;
1207 args
.fsbno
= *firstblock
;
1208 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
1211 args
.minlen
= args
.maxlen
= args
.prod
= 1;
1212 error
= xfs_alloc_vextent(&args
);
1216 /* Can't fail, the space was reserved. */
1217 ASSERT(args
.fsbno
!= NULLFSBLOCK
);
1218 ASSERT(args
.len
== 1);
1219 *firstblock
= args
.fsbno
;
1220 bp
= xfs_btree_get_bufl(args
.mp
, tp
, args
.fsbno
, 0);
1222 /* initialise the block and copy the data */
1223 init_fn(tp
, bp
, ip
, ifp
);
1225 /* account for the change in fork size and log everything */
1226 xfs_trans_log_buf(tp
, bp
, 0, ifp
->if_bytes
- 1);
1227 xfs_bmap_forkoff_reset(args
.mp
, ip
, whichfork
);
1228 xfs_idata_realloc(ip
, -ifp
->if_bytes
, whichfork
);
1229 xfs_iext_add(ifp
, 0, 1);
1230 ep
= xfs_iext_get_ext(ifp
, 0);
1231 xfs_bmbt_set_allf(ep
, 0, args
.fsbno
, 1, XFS_EXT_NORM
);
1232 trace_xfs_bmap_post_update(ip
, 0,
1233 whichfork
== XFS_ATTR_FORK
? BMAP_ATTRFORK
: 0,
1235 XFS_IFORK_NEXT_SET(ip
, whichfork
, 1);
1236 ip
->i_d
.di_nblocks
= 1;
1237 xfs_trans_mod_dquot_byino(tp
, ip
,
1238 XFS_TRANS_DQ_BCOUNT
, 1L);
1239 flags
|= xfs_ilog_fext(whichfork
);
1241 ASSERT(XFS_IFORK_NEXTENTS(ip
, whichfork
) == 0);
1242 xfs_bmap_forkoff_reset(ip
->i_mount
, ip
, whichfork
);
1244 ifp
->if_flags
&= ~XFS_IFINLINE
;
1245 ifp
->if_flags
|= XFS_IFEXTENTS
;
1246 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_EXTENTS
);
1247 flags
|= XFS_ILOG_CORE
;
1254 * Called from xfs_bmap_add_attrfork to handle btree format files.
1256 STATIC
int /* error */
1257 xfs_bmap_add_attrfork_btree(
1258 xfs_trans_t
*tp
, /* transaction pointer */
1259 xfs_inode_t
*ip
, /* incore inode pointer */
1260 xfs_fsblock_t
*firstblock
, /* first block allocated */
1261 xfs_bmap_free_t
*flist
, /* blocks to free at commit */
1262 int *flags
) /* inode logging flags */
1264 xfs_btree_cur_t
*cur
; /* btree cursor */
1265 int error
; /* error return value */
1266 xfs_mount_t
*mp
; /* file system mount struct */
1267 int stat
; /* newroot status */
1270 if (ip
->i_df
.if_broot_bytes
<= XFS_IFORK_DSIZE(ip
))
1271 *flags
|= XFS_ILOG_DBROOT
;
1273 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, XFS_DATA_FORK
);
1274 cur
->bc_private
.b
.flist
= flist
;
1275 cur
->bc_private
.b
.firstblock
= *firstblock
;
1276 if ((error
= xfs_bmbt_lookup_ge(cur
, 0, 0, 0, &stat
)))
1278 /* must be at least one entry */
1279 XFS_WANT_CORRUPTED_GOTO(stat
== 1, error0
);
1280 if ((error
= xfs_btree_new_iroot(cur
, flags
, &stat
)))
1283 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
1284 return XFS_ERROR(ENOSPC
);
1286 *firstblock
= cur
->bc_private
.b
.firstblock
;
1287 cur
->bc_private
.b
.allocated
= 0;
1288 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
1292 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
1297 * Called from xfs_bmap_add_attrfork to handle extents format files.
1299 STATIC
int /* error */
1300 xfs_bmap_add_attrfork_extents(
1301 xfs_trans_t
*tp
, /* transaction pointer */
1302 xfs_inode_t
*ip
, /* incore inode pointer */
1303 xfs_fsblock_t
*firstblock
, /* first block allocated */
1304 xfs_bmap_free_t
*flist
, /* blocks to free at commit */
1305 int *flags
) /* inode logging flags */
1307 xfs_btree_cur_t
*cur
; /* bmap btree cursor */
1308 int error
; /* error return value */
1310 if (ip
->i_d
.di_nextents
* sizeof(xfs_bmbt_rec_t
) <= XFS_IFORK_DSIZE(ip
))
1313 error
= xfs_bmap_extents_to_btree(tp
, ip
, firstblock
, flist
, &cur
, 0,
1314 flags
, XFS_DATA_FORK
);
1316 cur
->bc_private
.b
.allocated
= 0;
1317 xfs_btree_del_cursor(cur
,
1318 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
1324 * Block initialisation functions for local to extent format conversion.
1325 * As these get more complex, they will be moved to the relevant files,
1326 * but for now they are too simple to worry about.
1329 xfs_bmap_local_to_extents_init_fn(
1330 struct xfs_trans
*tp
,
1332 struct xfs_inode
*ip
,
1333 struct xfs_ifork
*ifp
)
1335 bp
->b_ops
= &xfs_bmbt_buf_ops
;
1336 memcpy(bp
->b_addr
, ifp
->if_u1
.if_data
, ifp
->if_bytes
);
1337 xfs_trans_buf_set_type(tp
, bp
, XFS_BLF_BTREE_BUF
);
1341 xfs_symlink_local_to_remote(
1342 struct xfs_trans
*tp
,
1344 struct xfs_inode
*ip
,
1345 struct xfs_ifork
*ifp
)
1347 /* remote symlink blocks are not verifiable until CRCs come along */
1349 memcpy(bp
->b_addr
, ifp
->if_u1
.if_data
, ifp
->if_bytes
);
1353 * Called from xfs_bmap_add_attrfork to handle local format files. Each
1354 * different data fork content type needs a different callout to do the
1355 * conversion. Some are basic and only require special block initialisation
1356 * callouts for the data formating, others (directories) are so specialised they
1357 * handle everything themselves.
1359 * XXX (dgc): investigate whether directory conversion can use the generic
1360 * formatting callout. It should be possible - it's just a very complex
1363 STATIC
int /* error */
1364 xfs_bmap_add_attrfork_local(
1365 xfs_trans_t
*tp
, /* transaction pointer */
1366 xfs_inode_t
*ip
, /* incore inode pointer */
1367 xfs_fsblock_t
*firstblock
, /* first block allocated */
1368 xfs_bmap_free_t
*flist
, /* blocks to free at commit */
1369 int *flags
) /* inode logging flags */
1371 xfs_da_args_t dargs
; /* args for dir/attr code */
1373 if (ip
->i_df
.if_bytes
<= XFS_IFORK_DSIZE(ip
))
1376 if (S_ISDIR(ip
->i_d
.di_mode
)) {
1377 memset(&dargs
, 0, sizeof(dargs
));
1379 dargs
.firstblock
= firstblock
;
1380 dargs
.flist
= flist
;
1381 dargs
.total
= ip
->i_mount
->m_dirblkfsbs
;
1382 dargs
.whichfork
= XFS_DATA_FORK
;
1384 return xfs_dir2_sf_to_block(&dargs
);
1387 if (S_ISLNK(ip
->i_d
.di_mode
))
1388 return xfs_bmap_local_to_extents(tp
, ip
, firstblock
, 1,
1389 flags
, XFS_DATA_FORK
,
1390 xfs_symlink_local_to_remote
);
1392 return xfs_bmap_local_to_extents(tp
, ip
, firstblock
, 1, flags
,
1394 xfs_bmap_local_to_extents_init_fn
);
1398 * Convert inode from non-attributed to attributed.
1399 * Must not be in a transaction, ip must not be locked.
1401 int /* error code */
1402 xfs_bmap_add_attrfork(
1403 xfs_inode_t
*ip
, /* incore inode pointer */
1404 int size
, /* space new attribute needs */
1405 int rsvd
) /* xact may use reserved blks */
1407 xfs_fsblock_t firstblock
; /* 1st block/ag allocated */
1408 xfs_bmap_free_t flist
; /* freed extent records */
1409 xfs_mount_t
*mp
; /* mount structure */
1410 xfs_trans_t
*tp
; /* transaction pointer */
1411 int blks
; /* space reservation */
1412 int version
= 1; /* superblock attr version */
1413 int committed
; /* xaction was committed */
1414 int logflags
; /* logging flags */
1415 int error
; /* error return value */
1417 ASSERT(XFS_IFORK_Q(ip
) == 0);
1420 ASSERT(!XFS_NOT_DQATTACHED(mp
, ip
));
1421 tp
= xfs_trans_alloc(mp
, XFS_TRANS_ADDAFORK
);
1422 blks
= XFS_ADDAFORK_SPACE_RES(mp
);
1424 tp
->t_flags
|= XFS_TRANS_RESERVE
;
1425 if ((error
= xfs_trans_reserve(tp
, blks
, XFS_ADDAFORK_LOG_RES(mp
), 0,
1426 XFS_TRANS_PERM_LOG_RES
, XFS_ADDAFORK_LOG_COUNT
)))
1428 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
1429 error
= xfs_trans_reserve_quota_nblks(tp
, ip
, blks
, 0, rsvd
?
1430 XFS_QMOPT_RES_REGBLKS
| XFS_QMOPT_FORCE_RES
:
1431 XFS_QMOPT_RES_REGBLKS
);
1433 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
1434 xfs_trans_cancel(tp
, XFS_TRANS_RELEASE_LOG_RES
);
1437 if (XFS_IFORK_Q(ip
))
1439 if (ip
->i_d
.di_aformat
!= XFS_DINODE_FMT_EXTENTS
) {
1441 * For inodes coming from pre-6.2 filesystems.
1443 ASSERT(ip
->i_d
.di_aformat
== 0);
1444 ip
->i_d
.di_aformat
= XFS_DINODE_FMT_EXTENTS
;
1446 ASSERT(ip
->i_d
.di_anextents
== 0);
1448 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
);
1449 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
1451 switch (ip
->i_d
.di_format
) {
1452 case XFS_DINODE_FMT_DEV
:
1453 ip
->i_d
.di_forkoff
= roundup(sizeof(xfs_dev_t
), 8) >> 3;
1455 case XFS_DINODE_FMT_UUID
:
1456 ip
->i_d
.di_forkoff
= roundup(sizeof(uuid_t
), 8) >> 3;
1458 case XFS_DINODE_FMT_LOCAL
:
1459 case XFS_DINODE_FMT_EXTENTS
:
1460 case XFS_DINODE_FMT_BTREE
:
1461 ip
->i_d
.di_forkoff
= xfs_attr_shortform_bytesfit(ip
, size
);
1462 if (!ip
->i_d
.di_forkoff
)
1463 ip
->i_d
.di_forkoff
= xfs_default_attroffset(ip
) >> 3;
1464 else if (mp
->m_flags
& XFS_MOUNT_ATTR2
)
1469 error
= XFS_ERROR(EINVAL
);
1473 ASSERT(ip
->i_afp
== NULL
);
1474 ip
->i_afp
= kmem_zone_zalloc(xfs_ifork_zone
, KM_SLEEP
);
1475 ip
->i_afp
->if_flags
= XFS_IFEXTENTS
;
1477 xfs_bmap_init(&flist
, &firstblock
);
1478 switch (ip
->i_d
.di_format
) {
1479 case XFS_DINODE_FMT_LOCAL
:
1480 error
= xfs_bmap_add_attrfork_local(tp
, ip
, &firstblock
, &flist
,
1483 case XFS_DINODE_FMT_EXTENTS
:
1484 error
= xfs_bmap_add_attrfork_extents(tp
, ip
, &firstblock
,
1487 case XFS_DINODE_FMT_BTREE
:
1488 error
= xfs_bmap_add_attrfork_btree(tp
, ip
, &firstblock
, &flist
,
1496 xfs_trans_log_inode(tp
, ip
, logflags
);
1499 if (!xfs_sb_version_hasattr(&mp
->m_sb
) ||
1500 (!xfs_sb_version_hasattr2(&mp
->m_sb
) && version
== 2)) {
1501 __int64_t sbfields
= 0;
1503 spin_lock(&mp
->m_sb_lock
);
1504 if (!xfs_sb_version_hasattr(&mp
->m_sb
)) {
1505 xfs_sb_version_addattr(&mp
->m_sb
);
1506 sbfields
|= XFS_SB_VERSIONNUM
;
1508 if (!xfs_sb_version_hasattr2(&mp
->m_sb
) && version
== 2) {
1509 xfs_sb_version_addattr2(&mp
->m_sb
);
1510 sbfields
|= (XFS_SB_VERSIONNUM
| XFS_SB_FEATURES2
);
1513 spin_unlock(&mp
->m_sb_lock
);
1514 xfs_mod_sb(tp
, sbfields
);
1516 spin_unlock(&mp
->m_sb_lock
);
1519 error
= xfs_bmap_finish(&tp
, &flist
, &committed
);
1522 return xfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
);
1524 xfs_bmap_cancel(&flist
);
1526 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
1528 xfs_trans_cancel(tp
, XFS_TRANS_RELEASE_LOG_RES
|XFS_TRANS_ABORT
);
1533 * Internal and external extent tree search functions.
1537 * Read in the extents to if_extents.
1538 * All inode fields are set up by caller, we just traverse the btree
1539 * and copy the records in. If the file system cannot contain unwritten
1540 * extents, the records are checked for no "state" flags.
1543 xfs_bmap_read_extents(
1544 xfs_trans_t
*tp
, /* transaction pointer */
1545 xfs_inode_t
*ip
, /* incore inode */
1546 int whichfork
) /* data or attr fork */
1548 struct xfs_btree_block
*block
; /* current btree block */
1549 xfs_fsblock_t bno
; /* block # of "block" */
1550 xfs_buf_t
*bp
; /* buffer for "block" */
1551 int error
; /* error return value */
1552 xfs_exntfmt_t exntf
; /* XFS_EXTFMT_NOSTATE, if checking */
1553 xfs_extnum_t i
, j
; /* index into the extents list */
1554 xfs_ifork_t
*ifp
; /* fork structure */
1555 int level
; /* btree level, for checking */
1556 xfs_mount_t
*mp
; /* file system mount structure */
1557 __be64
*pp
; /* pointer to block address */
1559 xfs_extnum_t room
; /* number of entries there's room for */
1563 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1564 exntf
= (whichfork
!= XFS_DATA_FORK
) ? XFS_EXTFMT_NOSTATE
:
1565 XFS_EXTFMT_INODE(ip
);
1566 block
= ifp
->if_broot
;
1568 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
1570 level
= be16_to_cpu(block
->bb_level
);
1572 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, 1, ifp
->if_broot_bytes
);
1573 bno
= be64_to_cpu(*pp
);
1574 ASSERT(bno
!= NULLDFSBNO
);
1575 ASSERT(XFS_FSB_TO_AGNO(mp
, bno
) < mp
->m_sb
.sb_agcount
);
1576 ASSERT(XFS_FSB_TO_AGBNO(mp
, bno
) < mp
->m_sb
.sb_agblocks
);
1578 * Go down the tree until leaf level is reached, following the first
1579 * pointer (leftmost) at each level.
1581 while (level
-- > 0) {
1582 error
= xfs_btree_read_bufl(mp
, tp
, bno
, 0, &bp
,
1583 XFS_BMAP_BTREE_REF
, &xfs_bmbt_buf_ops
);
1586 block
= XFS_BUF_TO_BLOCK(bp
);
1587 XFS_WANT_CORRUPTED_GOTO(
1588 xfs_bmap_sanity_check(mp
, bp
, level
),
1592 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, mp
->m_bmap_dmxr
[1]);
1593 bno
= be64_to_cpu(*pp
);
1594 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp
, bno
), error0
);
1595 xfs_trans_brelse(tp
, bp
);
1598 * Here with bp and block set to the leftmost leaf node in the tree.
1600 room
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
1603 * Loop over all leaf nodes. Copy information to the extent records.
1606 xfs_bmbt_rec_t
*frp
;
1607 xfs_fsblock_t nextbno
;
1608 xfs_extnum_t num_recs
;
1611 num_recs
= xfs_btree_get_numrecs(block
);
1612 if (unlikely(i
+ num_recs
> room
)) {
1613 ASSERT(i
+ num_recs
<= room
);
1614 xfs_warn(ip
->i_mount
,
1615 "corrupt dinode %Lu, (btree extents).",
1616 (unsigned long long) ip
->i_ino
);
1617 XFS_CORRUPTION_ERROR("xfs_bmap_read_extents(1)",
1618 XFS_ERRLEVEL_LOW
, ip
->i_mount
, block
);
1621 XFS_WANT_CORRUPTED_GOTO(
1622 xfs_bmap_sanity_check(mp
, bp
, 0),
1625 * Read-ahead the next leaf block, if any.
1627 nextbno
= be64_to_cpu(block
->bb_u
.l
.bb_rightsib
);
1628 if (nextbno
!= NULLFSBLOCK
)
1629 xfs_btree_reada_bufl(mp
, nextbno
, 1,
1632 * Copy records into the extent records.
1634 frp
= XFS_BMBT_REC_ADDR(mp
, block
, 1);
1636 for (j
= 0; j
< num_recs
; j
++, i
++, frp
++) {
1637 xfs_bmbt_rec_host_t
*trp
= xfs_iext_get_ext(ifp
, i
);
1638 trp
->l0
= be64_to_cpu(frp
->l0
);
1639 trp
->l1
= be64_to_cpu(frp
->l1
);
1641 if (exntf
== XFS_EXTFMT_NOSTATE
) {
1643 * Check all attribute bmap btree records and
1644 * any "older" data bmap btree records for a
1645 * set bit in the "extent flag" position.
1647 if (unlikely(xfs_check_nostate_extents(ifp
,
1648 start
, num_recs
))) {
1649 XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
1655 xfs_trans_brelse(tp
, bp
);
1658 * If we've reached the end, stop.
1660 if (bno
== NULLFSBLOCK
)
1662 error
= xfs_btree_read_bufl(mp
, tp
, bno
, 0, &bp
,
1663 XFS_BMAP_BTREE_REF
, &xfs_bmbt_buf_ops
);
1666 block
= XFS_BUF_TO_BLOCK(bp
);
1668 ASSERT(i
== (ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
)));
1669 ASSERT(i
== XFS_IFORK_NEXTENTS(ip
, whichfork
));
1670 XFS_BMAP_TRACE_EXLIST(ip
, i
, whichfork
);
1673 xfs_trans_brelse(tp
, bp
);
1674 return XFS_ERROR(EFSCORRUPTED
);
1679 * Search the extent records for the entry containing block bno.
1680 * If bno lies in a hole, point to the next entry. If bno lies
1681 * past eof, *eofp will be set, and *prevp will contain the last
1682 * entry (null if none). Else, *lastxp will be set to the index
1683 * of the found entry; *gotp will contain the entry.
1685 STATIC xfs_bmbt_rec_host_t
* /* pointer to found extent entry */
1686 xfs_bmap_search_multi_extents(
1687 xfs_ifork_t
*ifp
, /* inode fork pointer */
1688 xfs_fileoff_t bno
, /* block number searched for */
1689 int *eofp
, /* out: end of file found */
1690 xfs_extnum_t
*lastxp
, /* out: last extent index */
1691 xfs_bmbt_irec_t
*gotp
, /* out: extent entry found */
1692 xfs_bmbt_irec_t
*prevp
) /* out: previous extent entry found */
1694 xfs_bmbt_rec_host_t
*ep
; /* extent record pointer */
1695 xfs_extnum_t lastx
; /* last extent index */
1698 * Initialize the extent entry structure to catch access to
1699 * uninitialized br_startblock field.
1701 gotp
->br_startoff
= 0xffa5a5a5a5a5a5a5LL
;
1702 gotp
->br_blockcount
= 0xa55a5a5a5a5a5a5aLL
;
1703 gotp
->br_state
= XFS_EXT_INVALID
;
1705 gotp
->br_startblock
= 0xffffa5a5a5a5a5a5LL
;
1707 gotp
->br_startblock
= 0xffffa5a5;
1709 prevp
->br_startoff
= NULLFILEOFF
;
1711 ep
= xfs_iext_bno_to_ext(ifp
, bno
, &lastx
);
1713 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, lastx
- 1), prevp
);
1715 if (lastx
< (ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
))) {
1716 xfs_bmbt_get_all(ep
, gotp
);
1730 * Search the extents list for the inode, for the extent containing bno.
1731 * If bno lies in a hole, point to the next entry. If bno lies past eof,
1732 * *eofp will be set, and *prevp will contain the last entry (null if none).
1733 * Else, *lastxp will be set to the index of the found
1734 * entry; *gotp will contain the entry.
1736 STATIC xfs_bmbt_rec_host_t
* /* pointer to found extent entry */
1737 xfs_bmap_search_extents(
1738 xfs_inode_t
*ip
, /* incore inode pointer */
1739 xfs_fileoff_t bno
, /* block number searched for */
1740 int fork
, /* data or attr fork */
1741 int *eofp
, /* out: end of file found */
1742 xfs_extnum_t
*lastxp
, /* out: last extent index */
1743 xfs_bmbt_irec_t
*gotp
, /* out: extent entry found */
1744 xfs_bmbt_irec_t
*prevp
) /* out: previous extent entry found */
1746 xfs_ifork_t
*ifp
; /* inode fork pointer */
1747 xfs_bmbt_rec_host_t
*ep
; /* extent record pointer */
1749 XFS_STATS_INC(xs_look_exlist
);
1750 ifp
= XFS_IFORK_PTR(ip
, fork
);
1752 ep
= xfs_bmap_search_multi_extents(ifp
, bno
, eofp
, lastxp
, gotp
, prevp
);
1754 if (unlikely(!(gotp
->br_startblock
) && (*lastxp
!= NULLEXTNUM
) &&
1755 !(XFS_IS_REALTIME_INODE(ip
) && fork
== XFS_DATA_FORK
))) {
1756 xfs_alert_tag(ip
->i_mount
, XFS_PTAG_FSBLOCK_ZERO
,
1757 "Access to block zero in inode %llu "
1758 "start_block: %llx start_off: %llx "
1759 "blkcnt: %llx extent-state: %x lastx: %x\n",
1760 (unsigned long long)ip
->i_ino
,
1761 (unsigned long long)gotp
->br_startblock
,
1762 (unsigned long long)gotp
->br_startoff
,
1763 (unsigned long long)gotp
->br_blockcount
,
1764 gotp
->br_state
, *lastxp
);
1765 *lastxp
= NULLEXTNUM
;
1773 * Returns the file-relative block number of the first unused block(s)
1774 * in the file with at least "len" logically contiguous blocks free.
1775 * This is the lowest-address hole if the file has holes, else the first block
1776 * past the end of file.
1777 * Return 0 if the file is currently local (in-inode).
1780 xfs_bmap_first_unused(
1781 xfs_trans_t
*tp
, /* transaction pointer */
1782 xfs_inode_t
*ip
, /* incore inode */
1783 xfs_extlen_t len
, /* size of hole to find */
1784 xfs_fileoff_t
*first_unused
, /* unused block */
1785 int whichfork
) /* data or attr fork */
1787 int error
; /* error return value */
1788 int idx
; /* extent record index */
1789 xfs_ifork_t
*ifp
; /* inode fork pointer */
1790 xfs_fileoff_t lastaddr
; /* last block number seen */
1791 xfs_fileoff_t lowest
; /* lowest useful block */
1792 xfs_fileoff_t max
; /* starting useful block */
1793 xfs_fileoff_t off
; /* offset for this block */
1794 xfs_extnum_t nextents
; /* number of extent entries */
1796 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
||
1797 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
||
1798 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
1799 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
) {
1803 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1804 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
1805 (error
= xfs_iread_extents(tp
, ip
, whichfork
)))
1807 lowest
= *first_unused
;
1808 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
1809 for (idx
= 0, lastaddr
= 0, max
= lowest
; idx
< nextents
; idx
++) {
1810 xfs_bmbt_rec_host_t
*ep
= xfs_iext_get_ext(ifp
, idx
);
1811 off
= xfs_bmbt_get_startoff(ep
);
1813 * See if the hole before this extent will work.
1815 if (off
>= lowest
+ len
&& off
- max
>= len
) {
1816 *first_unused
= max
;
1819 lastaddr
= off
+ xfs_bmbt_get_blockcount(ep
);
1820 max
= XFS_FILEOFF_MAX(lastaddr
, lowest
);
1822 *first_unused
= max
;
1827 * Returns the file-relative block number of the last block + 1 before
1828 * last_block (input value) in the file.
1829 * This is not based on i_size, it is based on the extent records.
1830 * Returns 0 for local files, as they do not have extent records.
1833 xfs_bmap_last_before(
1834 xfs_trans_t
*tp
, /* transaction pointer */
1835 xfs_inode_t
*ip
, /* incore inode */
1836 xfs_fileoff_t
*last_block
, /* last block */
1837 int whichfork
) /* data or attr fork */
1839 xfs_fileoff_t bno
; /* input file offset */
1840 int eof
; /* hit end of file */
1841 xfs_bmbt_rec_host_t
*ep
; /* pointer to last extent */
1842 int error
; /* error return value */
1843 xfs_bmbt_irec_t got
; /* current extent value */
1844 xfs_ifork_t
*ifp
; /* inode fork pointer */
1845 xfs_extnum_t lastx
; /* last extent used */
1846 xfs_bmbt_irec_t prev
; /* previous extent value */
1848 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
&&
1849 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
1850 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_LOCAL
)
1851 return XFS_ERROR(EIO
);
1852 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
) {
1856 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1857 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
1858 (error
= xfs_iread_extents(tp
, ip
, whichfork
)))
1860 bno
= *last_block
- 1;
1861 ep
= xfs_bmap_search_extents(ip
, bno
, whichfork
, &eof
, &lastx
, &got
,
1863 if (eof
|| xfs_bmbt_get_startoff(ep
) > bno
) {
1864 if (prev
.br_startoff
== NULLFILEOFF
)
1867 *last_block
= prev
.br_startoff
+ prev
.br_blockcount
;
1870 * Otherwise *last_block is already the right answer.
1876 xfs_bmap_last_extent(
1877 struct xfs_trans
*tp
,
1878 struct xfs_inode
*ip
,
1880 struct xfs_bmbt_irec
*rec
,
1883 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1887 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
1888 error
= xfs_iread_extents(tp
, ip
, whichfork
);
1893 nextents
= ifp
->if_bytes
/ sizeof(xfs_bmbt_rec_t
);
1894 if (nextents
== 0) {
1899 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, nextents
- 1), rec
);
1905 * Check the last inode extent to determine whether this allocation will result
1906 * in blocks being allocated at the end of the file. When we allocate new data
1907 * blocks at the end of the file which do not start at the previous data block,
1908 * we will try to align the new blocks at stripe unit boundaries.
1910 * Returns 0 in bma->aeof if the file (fork) is empty as any new write will be
1911 * at, or past the EOF.
1915 struct xfs_bmalloca
*bma
,
1918 struct xfs_bmbt_irec rec
;
1923 error
= xfs_bmap_last_extent(NULL
, bma
->ip
, whichfork
, &rec
,
1925 if (error
|| is_empty
)
1929 * Check if we are allocation or past the last extent, or at least into
1930 * the last delayed allocated extent.
1932 bma
->aeof
= bma
->offset
>= rec
.br_startoff
+ rec
.br_blockcount
||
1933 (bma
->offset
>= rec
.br_startoff
&&
1934 isnullstartblock(rec
.br_startblock
));
1939 * Check if the endoff is outside the last extent. If so the caller will grow
1940 * the allocation to a stripe unit boundary. All offsets are considered outside
1941 * the end of file for an empty fork, so 1 is returned in *eof in that case.
1945 struct xfs_inode
*ip
,
1946 xfs_fileoff_t endoff
,
1950 struct xfs_bmbt_irec rec
;
1953 error
= xfs_bmap_last_extent(NULL
, ip
, whichfork
, &rec
, eof
);
1957 *eof
= endoff
>= rec
.br_startoff
+ rec
.br_blockcount
;
1962 * Returns the file-relative block number of the first block past eof in
1963 * the file. This is not based on i_size, it is based on the extent records.
1964 * Returns 0 for local files, as they do not have extent records.
1967 xfs_bmap_last_offset(
1968 struct xfs_trans
*tp
,
1969 struct xfs_inode
*ip
,
1970 xfs_fileoff_t
*last_block
,
1973 struct xfs_bmbt_irec rec
;
1979 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
)
1982 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
&&
1983 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
1984 return XFS_ERROR(EIO
);
1986 error
= xfs_bmap_last_extent(NULL
, ip
, whichfork
, &rec
, &is_empty
);
1987 if (error
|| is_empty
)
1990 *last_block
= rec
.br_startoff
+ rec
.br_blockcount
;
1995 * Returns whether the selected fork of the inode has exactly one
1996 * block or not. For the data fork we check this matches di_size,
1997 * implying the file's range is 0..bsize-1.
1999 int /* 1=>1 block, 0=>otherwise */
2001 xfs_inode_t
*ip
, /* incore inode */
2002 int whichfork
) /* data or attr fork */
2004 xfs_bmbt_rec_host_t
*ep
; /* ptr to fork's extent */
2005 xfs_ifork_t
*ifp
; /* inode fork pointer */
2006 int rval
; /* return value */
2007 xfs_bmbt_irec_t s
; /* internal version of extent */
2010 if (whichfork
== XFS_DATA_FORK
)
2011 return XFS_ISIZE(ip
) == ip
->i_mount
->m_sb
.sb_blocksize
;
2013 if (XFS_IFORK_NEXTENTS(ip
, whichfork
) != 1)
2015 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
2017 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
2018 ASSERT(ifp
->if_flags
& XFS_IFEXTENTS
);
2019 ep
= xfs_iext_get_ext(ifp
, 0);
2020 xfs_bmbt_get_all(ep
, &s
);
2021 rval
= s
.br_startoff
== 0 && s
.br_blockcount
== 1;
2022 if (rval
&& whichfork
== XFS_DATA_FORK
)
2023 ASSERT(XFS_ISIZE(ip
) == ip
->i_mount
->m_sb
.sb_blocksize
);
2028 * Extent tree manipulation functions used during allocation.
2032 * Convert a delayed allocation to a real allocation.
2034 STATIC
int /* error */
2035 xfs_bmap_add_extent_delay_real(
2036 struct xfs_bmalloca
*bma
)
2038 struct xfs_bmbt_irec
*new = &bma
->got
;
2039 int diff
; /* temp value */
2040 xfs_bmbt_rec_host_t
*ep
; /* extent entry for idx */
2041 int error
; /* error return value */
2042 int i
; /* temp state */
2043 xfs_ifork_t
*ifp
; /* inode fork pointer */
2044 xfs_fileoff_t new_endoff
; /* end offset of new entry */
2045 xfs_bmbt_irec_t r
[3]; /* neighbor extent entries */
2046 /* left is 0, right is 1, prev is 2 */
2047 int rval
=0; /* return value (logging flags) */
2048 int state
= 0;/* state bits, accessed thru macros */
2049 xfs_filblks_t da_new
; /* new count del alloc blocks used */
2050 xfs_filblks_t da_old
; /* old count del alloc blocks used */
2051 xfs_filblks_t temp
=0; /* value for da_new calculations */
2052 xfs_filblks_t temp2
=0;/* value for da_new calculations */
2053 int tmp_rval
; /* partial logging flags */
2055 ifp
= XFS_IFORK_PTR(bma
->ip
, XFS_DATA_FORK
);
2057 ASSERT(bma
->idx
>= 0);
2058 ASSERT(bma
->idx
<= ifp
->if_bytes
/ sizeof(struct xfs_bmbt_rec
));
2059 ASSERT(!isnullstartblock(new->br_startblock
));
2061 (bma
->cur
->bc_private
.b
.flags
& XFS_BTCUR_BPRV_WASDEL
));
2063 XFS_STATS_INC(xs_add_exlist
);
2070 * Set up a bunch of variables to make the tests simpler.
2072 ep
= xfs_iext_get_ext(ifp
, bma
->idx
);
2073 xfs_bmbt_get_all(ep
, &PREV
);
2074 new_endoff
= new->br_startoff
+ new->br_blockcount
;
2075 ASSERT(PREV
.br_startoff
<= new->br_startoff
);
2076 ASSERT(PREV
.br_startoff
+ PREV
.br_blockcount
>= new_endoff
);
2078 da_old
= startblockval(PREV
.br_startblock
);
2082 * Set flags determining what part of the previous delayed allocation
2083 * extent is being replaced by a real allocation.
2085 if (PREV
.br_startoff
== new->br_startoff
)
2086 state
|= BMAP_LEFT_FILLING
;
2087 if (PREV
.br_startoff
+ PREV
.br_blockcount
== new_endoff
)
2088 state
|= BMAP_RIGHT_FILLING
;
2091 * Check and set flags if this segment has a left neighbor.
2092 * Don't set contiguous if the combined extent would be too large.
2095 state
|= BMAP_LEFT_VALID
;
2096 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
- 1), &LEFT
);
2098 if (isnullstartblock(LEFT
.br_startblock
))
2099 state
|= BMAP_LEFT_DELAY
;
2102 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
2103 LEFT
.br_startoff
+ LEFT
.br_blockcount
== new->br_startoff
&&
2104 LEFT
.br_startblock
+ LEFT
.br_blockcount
== new->br_startblock
&&
2105 LEFT
.br_state
== new->br_state
&&
2106 LEFT
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
2107 state
|= BMAP_LEFT_CONTIG
;
2110 * Check and set flags if this segment has a right neighbor.
2111 * Don't set contiguous if the combined extent would be too large.
2112 * Also check for all-three-contiguous being too large.
2114 if (bma
->idx
< bma
->ip
->i_df
.if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
) - 1) {
2115 state
|= BMAP_RIGHT_VALID
;
2116 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
+ 1), &RIGHT
);
2118 if (isnullstartblock(RIGHT
.br_startblock
))
2119 state
|= BMAP_RIGHT_DELAY
;
2122 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
2123 new_endoff
== RIGHT
.br_startoff
&&
2124 new->br_startblock
+ new->br_blockcount
== RIGHT
.br_startblock
&&
2125 new->br_state
== RIGHT
.br_state
&&
2126 new->br_blockcount
+ RIGHT
.br_blockcount
<= MAXEXTLEN
&&
2127 ((state
& (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
2128 BMAP_RIGHT_FILLING
)) !=
2129 (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
2130 BMAP_RIGHT_FILLING
) ||
2131 LEFT
.br_blockcount
+ new->br_blockcount
+ RIGHT
.br_blockcount
2133 state
|= BMAP_RIGHT_CONTIG
;
2137 * Switch out based on the FILLING and CONTIG state bits.
2139 switch (state
& (BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
2140 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
)) {
2141 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
2142 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2144 * Filling in all of a previously delayed allocation extent.
2145 * The left and right neighbors are both contiguous with new.
2148 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2149 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, bma
->idx
),
2150 LEFT
.br_blockcount
+ PREV
.br_blockcount
+
2151 RIGHT
.br_blockcount
);
2152 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2154 xfs_iext_remove(bma
->ip
, bma
->idx
+ 1, 2, state
);
2155 bma
->ip
->i_d
.di_nextents
--;
2156 if (bma
->cur
== NULL
)
2157 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2159 rval
= XFS_ILOG_CORE
;
2160 error
= xfs_bmbt_lookup_eq(bma
->cur
, RIGHT
.br_startoff
,
2161 RIGHT
.br_startblock
,
2162 RIGHT
.br_blockcount
, &i
);
2165 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2166 error
= xfs_btree_delete(bma
->cur
, &i
);
2169 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2170 error
= xfs_btree_decrement(bma
->cur
, 0, &i
);
2173 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2174 error
= xfs_bmbt_update(bma
->cur
, LEFT
.br_startoff
,
2176 LEFT
.br_blockcount
+
2177 PREV
.br_blockcount
+
2178 RIGHT
.br_blockcount
, LEFT
.br_state
);
2184 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
2186 * Filling in all of a previously delayed allocation extent.
2187 * The left neighbor is contiguous, the right is not.
2191 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2192 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, bma
->idx
),
2193 LEFT
.br_blockcount
+ PREV
.br_blockcount
);
2194 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2196 xfs_iext_remove(bma
->ip
, bma
->idx
+ 1, 1, state
);
2197 if (bma
->cur
== NULL
)
2198 rval
= XFS_ILOG_DEXT
;
2201 error
= xfs_bmbt_lookup_eq(bma
->cur
, LEFT
.br_startoff
,
2202 LEFT
.br_startblock
, LEFT
.br_blockcount
,
2206 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2207 error
= xfs_bmbt_update(bma
->cur
, LEFT
.br_startoff
,
2209 LEFT
.br_blockcount
+
2210 PREV
.br_blockcount
, LEFT
.br_state
);
2216 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2218 * Filling in all of a previously delayed allocation extent.
2219 * The right neighbor is contiguous, the left is not.
2221 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2222 xfs_bmbt_set_startblock(ep
, new->br_startblock
);
2223 xfs_bmbt_set_blockcount(ep
,
2224 PREV
.br_blockcount
+ RIGHT
.br_blockcount
);
2225 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2227 xfs_iext_remove(bma
->ip
, bma
->idx
+ 1, 1, state
);
2228 if (bma
->cur
== NULL
)
2229 rval
= XFS_ILOG_DEXT
;
2232 error
= xfs_bmbt_lookup_eq(bma
->cur
, RIGHT
.br_startoff
,
2233 RIGHT
.br_startblock
,
2234 RIGHT
.br_blockcount
, &i
);
2237 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2238 error
= xfs_bmbt_update(bma
->cur
, PREV
.br_startoff
,
2240 PREV
.br_blockcount
+
2241 RIGHT
.br_blockcount
, PREV
.br_state
);
2247 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
2249 * Filling in all of a previously delayed allocation extent.
2250 * Neither the left nor right neighbors are contiguous with
2253 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2254 xfs_bmbt_set_startblock(ep
, new->br_startblock
);
2255 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2257 bma
->ip
->i_d
.di_nextents
++;
2258 if (bma
->cur
== NULL
)
2259 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2261 rval
= XFS_ILOG_CORE
;
2262 error
= xfs_bmbt_lookup_eq(bma
->cur
, new->br_startoff
,
2263 new->br_startblock
, new->br_blockcount
,
2267 XFS_WANT_CORRUPTED_GOTO(i
== 0, done
);
2268 bma
->cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
2269 error
= xfs_btree_insert(bma
->cur
, &i
);
2272 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2276 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
:
2278 * Filling in the first part of a previous delayed allocation.
2279 * The left neighbor is contiguous.
2281 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
- 1, state
, _THIS_IP_
);
2282 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, bma
->idx
- 1),
2283 LEFT
.br_blockcount
+ new->br_blockcount
);
2284 xfs_bmbt_set_startoff(ep
,
2285 PREV
.br_startoff
+ new->br_blockcount
);
2286 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
- 1, state
, _THIS_IP_
);
2288 temp
= PREV
.br_blockcount
- new->br_blockcount
;
2289 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2290 xfs_bmbt_set_blockcount(ep
, temp
);
2291 if (bma
->cur
== NULL
)
2292 rval
= XFS_ILOG_DEXT
;
2295 error
= xfs_bmbt_lookup_eq(bma
->cur
, LEFT
.br_startoff
,
2296 LEFT
.br_startblock
, LEFT
.br_blockcount
,
2300 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2301 error
= xfs_bmbt_update(bma
->cur
, LEFT
.br_startoff
,
2303 LEFT
.br_blockcount
+
2309 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
2310 startblockval(PREV
.br_startblock
));
2311 xfs_bmbt_set_startblock(ep
, nullstartblock(da_new
));
2312 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2317 case BMAP_LEFT_FILLING
:
2319 * Filling in the first part of a previous delayed allocation.
2320 * The left neighbor is not contiguous.
2322 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2323 xfs_bmbt_set_startoff(ep
, new_endoff
);
2324 temp
= PREV
.br_blockcount
- new->br_blockcount
;
2325 xfs_bmbt_set_blockcount(ep
, temp
);
2326 xfs_iext_insert(bma
->ip
, bma
->idx
, 1, new, state
);
2327 bma
->ip
->i_d
.di_nextents
++;
2328 if (bma
->cur
== NULL
)
2329 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2331 rval
= XFS_ILOG_CORE
;
2332 error
= xfs_bmbt_lookup_eq(bma
->cur
, new->br_startoff
,
2333 new->br_startblock
, new->br_blockcount
,
2337 XFS_WANT_CORRUPTED_GOTO(i
== 0, done
);
2338 bma
->cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
2339 error
= xfs_btree_insert(bma
->cur
, &i
);
2342 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2345 if (xfs_bmap_needs_btree(bma
->ip
, XFS_DATA_FORK
)) {
2346 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
2347 bma
->firstblock
, bma
->flist
,
2348 &bma
->cur
, 1, &tmp_rval
, XFS_DATA_FORK
);
2353 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
2354 startblockval(PREV
.br_startblock
) -
2355 (bma
->cur
? bma
->cur
->bc_private
.b
.allocated
: 0));
2356 ep
= xfs_iext_get_ext(ifp
, bma
->idx
+ 1);
2357 xfs_bmbt_set_startblock(ep
, nullstartblock(da_new
));
2358 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
+ 1, state
, _THIS_IP_
);
2361 case BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2363 * Filling in the last part of a previous delayed allocation.
2364 * The right neighbor is contiguous with the new allocation.
2366 temp
= PREV
.br_blockcount
- new->br_blockcount
;
2367 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
+ 1, state
, _THIS_IP_
);
2368 xfs_bmbt_set_blockcount(ep
, temp
);
2369 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp
, bma
->idx
+ 1),
2370 new->br_startoff
, new->br_startblock
,
2371 new->br_blockcount
+ RIGHT
.br_blockcount
,
2373 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
+ 1, state
, _THIS_IP_
);
2374 if (bma
->cur
== NULL
)
2375 rval
= XFS_ILOG_DEXT
;
2378 error
= xfs_bmbt_lookup_eq(bma
->cur
, RIGHT
.br_startoff
,
2379 RIGHT
.br_startblock
,
2380 RIGHT
.br_blockcount
, &i
);
2383 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2384 error
= xfs_bmbt_update(bma
->cur
, new->br_startoff
,
2386 new->br_blockcount
+
2387 RIGHT
.br_blockcount
,
2393 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
2394 startblockval(PREV
.br_startblock
));
2395 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2396 xfs_bmbt_set_startblock(ep
, nullstartblock(da_new
));
2397 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2402 case BMAP_RIGHT_FILLING
:
2404 * Filling in the last part of a previous delayed allocation.
2405 * The right neighbor is not contiguous.
2407 temp
= PREV
.br_blockcount
- new->br_blockcount
;
2408 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2409 xfs_bmbt_set_blockcount(ep
, temp
);
2410 xfs_iext_insert(bma
->ip
, bma
->idx
+ 1, 1, new, state
);
2411 bma
->ip
->i_d
.di_nextents
++;
2412 if (bma
->cur
== NULL
)
2413 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2415 rval
= XFS_ILOG_CORE
;
2416 error
= xfs_bmbt_lookup_eq(bma
->cur
, new->br_startoff
,
2417 new->br_startblock
, new->br_blockcount
,
2421 XFS_WANT_CORRUPTED_GOTO(i
== 0, done
);
2422 bma
->cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
2423 error
= xfs_btree_insert(bma
->cur
, &i
);
2426 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2429 if (xfs_bmap_needs_btree(bma
->ip
, XFS_DATA_FORK
)) {
2430 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
2431 bma
->firstblock
, bma
->flist
, &bma
->cur
, 1,
2432 &tmp_rval
, XFS_DATA_FORK
);
2437 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
2438 startblockval(PREV
.br_startblock
) -
2439 (bma
->cur
? bma
->cur
->bc_private
.b
.allocated
: 0));
2440 ep
= xfs_iext_get_ext(ifp
, bma
->idx
);
2441 xfs_bmbt_set_startblock(ep
, nullstartblock(da_new
));
2442 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2449 * Filling in the middle part of a previous delayed allocation.
2450 * Contiguity is impossible here.
2451 * This case is avoided almost all the time.
2453 * We start with a delayed allocation:
2455 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
2458 * and we are allocating:
2459 * +rrrrrrrrrrrrrrrrr+
2462 * and we set it up for insertion as:
2463 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
2465 * PREV @ idx LEFT RIGHT
2466 * inserted at idx + 1
2468 temp
= new->br_startoff
- PREV
.br_startoff
;
2469 temp2
= PREV
.br_startoff
+ PREV
.br_blockcount
- new_endoff
;
2470 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, 0, _THIS_IP_
);
2471 xfs_bmbt_set_blockcount(ep
, temp
); /* truncate PREV */
2473 RIGHT
.br_state
= PREV
.br_state
;
2474 RIGHT
.br_startblock
= nullstartblock(
2475 (int)xfs_bmap_worst_indlen(bma
->ip
, temp2
));
2476 RIGHT
.br_startoff
= new_endoff
;
2477 RIGHT
.br_blockcount
= temp2
;
2478 /* insert LEFT (r[0]) and RIGHT (r[1]) at the same time */
2479 xfs_iext_insert(bma
->ip
, bma
->idx
+ 1, 2, &LEFT
, state
);
2480 bma
->ip
->i_d
.di_nextents
++;
2481 if (bma
->cur
== NULL
)
2482 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2484 rval
= XFS_ILOG_CORE
;
2485 error
= xfs_bmbt_lookup_eq(bma
->cur
, new->br_startoff
,
2486 new->br_startblock
, new->br_blockcount
,
2490 XFS_WANT_CORRUPTED_GOTO(i
== 0, done
);
2491 bma
->cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
2492 error
= xfs_btree_insert(bma
->cur
, &i
);
2495 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2498 if (xfs_bmap_needs_btree(bma
->ip
, XFS_DATA_FORK
)) {
2499 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
2500 bma
->firstblock
, bma
->flist
, &bma
->cur
,
2501 1, &tmp_rval
, XFS_DATA_FORK
);
2506 temp
= xfs_bmap_worst_indlen(bma
->ip
, temp
);
2507 temp2
= xfs_bmap_worst_indlen(bma
->ip
, temp2
);
2508 diff
= (int)(temp
+ temp2
- startblockval(PREV
.br_startblock
) -
2509 (bma
->cur
? bma
->cur
->bc_private
.b
.allocated
: 0));
2511 error
= xfs_icsb_modify_counters(bma
->ip
->i_mount
,
2513 -((int64_t)diff
), 0);
2519 ep
= xfs_iext_get_ext(ifp
, bma
->idx
);
2520 xfs_bmbt_set_startblock(ep
, nullstartblock((int)temp
));
2521 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2522 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
+ 2, state
, _THIS_IP_
);
2523 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp
, bma
->idx
+ 2),
2524 nullstartblock((int)temp2
));
2525 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
+ 2, state
, _THIS_IP_
);
2528 da_new
= temp
+ temp2
;
2531 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2532 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2533 case BMAP_LEFT_FILLING
| BMAP_RIGHT_CONTIG
:
2534 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
2535 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2536 case BMAP_LEFT_CONTIG
:
2537 case BMAP_RIGHT_CONTIG
:
2539 * These cases are all impossible.
2544 /* convert to a btree if necessary */
2545 if (xfs_bmap_needs_btree(bma
->ip
, XFS_DATA_FORK
)) {
2546 int tmp_logflags
; /* partial log flag return val */
2548 ASSERT(bma
->cur
== NULL
);
2549 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
2550 bma
->firstblock
, bma
->flist
, &bma
->cur
,
2551 da_old
> 0, &tmp_logflags
, XFS_DATA_FORK
);
2552 bma
->logflags
|= tmp_logflags
;
2557 /* adjust for changes in reserved delayed indirect blocks */
2558 if (da_old
|| da_new
) {
2561 temp
+= bma
->cur
->bc_private
.b
.allocated
;
2562 ASSERT(temp
<= da_old
);
2564 xfs_icsb_modify_counters(bma
->ip
->i_mount
,
2566 (int64_t)(da_old
- temp
), 0);
2569 /* clear out the allocated field, done with it now in any case. */
2571 bma
->cur
->bc_private
.b
.allocated
= 0;
2573 xfs_bmap_check_leaf_extents(bma
->cur
, bma
->ip
, XFS_DATA_FORK
);
2575 bma
->logflags
|= rval
;
2583 * Convert an unwritten allocation to a real allocation or vice versa.
2585 STATIC
int /* error */
2586 xfs_bmap_add_extent_unwritten_real(
2587 struct xfs_trans
*tp
,
2588 xfs_inode_t
*ip
, /* incore inode pointer */
2589 xfs_extnum_t
*idx
, /* extent number to update/insert */
2590 xfs_btree_cur_t
**curp
, /* if *curp is null, not a btree */
2591 xfs_bmbt_irec_t
*new, /* new data to add to file extents */
2592 xfs_fsblock_t
*first
, /* pointer to firstblock variable */
2593 xfs_bmap_free_t
*flist
, /* list of extents to be freed */
2594 int *logflagsp
) /* inode logging flags */
2596 xfs_btree_cur_t
*cur
; /* btree cursor */
2597 xfs_bmbt_rec_host_t
*ep
; /* extent entry for idx */
2598 int error
; /* error return value */
2599 int i
; /* temp state */
2600 xfs_ifork_t
*ifp
; /* inode fork pointer */
2601 xfs_fileoff_t new_endoff
; /* end offset of new entry */
2602 xfs_exntst_t newext
; /* new extent state */
2603 xfs_exntst_t oldext
; /* old extent state */
2604 xfs_bmbt_irec_t r
[3]; /* neighbor extent entries */
2605 /* left is 0, right is 1, prev is 2 */
2606 int rval
=0; /* return value (logging flags) */
2607 int state
= 0;/* state bits, accessed thru macros */
2612 ifp
= XFS_IFORK_PTR(ip
, XFS_DATA_FORK
);
2615 ASSERT(*idx
<= ifp
->if_bytes
/ sizeof(struct xfs_bmbt_rec
));
2616 ASSERT(!isnullstartblock(new->br_startblock
));
2618 XFS_STATS_INC(xs_add_exlist
);
2625 * Set up a bunch of variables to make the tests simpler.
2628 ep
= xfs_iext_get_ext(ifp
, *idx
);
2629 xfs_bmbt_get_all(ep
, &PREV
);
2630 newext
= new->br_state
;
2631 oldext
= (newext
== XFS_EXT_UNWRITTEN
) ?
2632 XFS_EXT_NORM
: XFS_EXT_UNWRITTEN
;
2633 ASSERT(PREV
.br_state
== oldext
);
2634 new_endoff
= new->br_startoff
+ new->br_blockcount
;
2635 ASSERT(PREV
.br_startoff
<= new->br_startoff
);
2636 ASSERT(PREV
.br_startoff
+ PREV
.br_blockcount
>= new_endoff
);
2639 * Set flags determining what part of the previous oldext allocation
2640 * extent is being replaced by a newext allocation.
2642 if (PREV
.br_startoff
== new->br_startoff
)
2643 state
|= BMAP_LEFT_FILLING
;
2644 if (PREV
.br_startoff
+ PREV
.br_blockcount
== new_endoff
)
2645 state
|= BMAP_RIGHT_FILLING
;
2648 * Check and set flags if this segment has a left neighbor.
2649 * Don't set contiguous if the combined extent would be too large.
2652 state
|= BMAP_LEFT_VALID
;
2653 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, *idx
- 1), &LEFT
);
2655 if (isnullstartblock(LEFT
.br_startblock
))
2656 state
|= BMAP_LEFT_DELAY
;
2659 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
2660 LEFT
.br_startoff
+ LEFT
.br_blockcount
== new->br_startoff
&&
2661 LEFT
.br_startblock
+ LEFT
.br_blockcount
== new->br_startblock
&&
2662 LEFT
.br_state
== newext
&&
2663 LEFT
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
2664 state
|= BMAP_LEFT_CONTIG
;
2667 * Check and set flags if this segment has a right neighbor.
2668 * Don't set contiguous if the combined extent would be too large.
2669 * Also check for all-three-contiguous being too large.
2671 if (*idx
< ip
->i_df
.if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
) - 1) {
2672 state
|= BMAP_RIGHT_VALID
;
2673 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, *idx
+ 1), &RIGHT
);
2674 if (isnullstartblock(RIGHT
.br_startblock
))
2675 state
|= BMAP_RIGHT_DELAY
;
2678 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
2679 new_endoff
== RIGHT
.br_startoff
&&
2680 new->br_startblock
+ new->br_blockcount
== RIGHT
.br_startblock
&&
2681 newext
== RIGHT
.br_state
&&
2682 new->br_blockcount
+ RIGHT
.br_blockcount
<= MAXEXTLEN
&&
2683 ((state
& (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
2684 BMAP_RIGHT_FILLING
)) !=
2685 (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
2686 BMAP_RIGHT_FILLING
) ||
2687 LEFT
.br_blockcount
+ new->br_blockcount
+ RIGHT
.br_blockcount
2689 state
|= BMAP_RIGHT_CONTIG
;
2692 * Switch out based on the FILLING and CONTIG state bits.
2694 switch (state
& (BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
2695 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
)) {
2696 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
2697 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2699 * Setting all of a previous oldext extent to newext.
2700 * The left and right neighbors are both contiguous with new.
2704 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2705 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
),
2706 LEFT
.br_blockcount
+ PREV
.br_blockcount
+
2707 RIGHT
.br_blockcount
);
2708 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2710 xfs_iext_remove(ip
, *idx
+ 1, 2, state
);
2711 ip
->i_d
.di_nextents
-= 2;
2713 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2715 rval
= XFS_ILOG_CORE
;
2716 if ((error
= xfs_bmbt_lookup_eq(cur
, RIGHT
.br_startoff
,
2717 RIGHT
.br_startblock
,
2718 RIGHT
.br_blockcount
, &i
)))
2720 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2721 if ((error
= xfs_btree_delete(cur
, &i
)))
2723 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2724 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2726 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2727 if ((error
= xfs_btree_delete(cur
, &i
)))
2729 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2730 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2732 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2733 if ((error
= xfs_bmbt_update(cur
, LEFT
.br_startoff
,
2735 LEFT
.br_blockcount
+ PREV
.br_blockcount
+
2736 RIGHT
.br_blockcount
, LEFT
.br_state
)))
2741 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
2743 * Setting all of a previous oldext extent to newext.
2744 * The left neighbor is contiguous, the right is not.
2748 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2749 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
),
2750 LEFT
.br_blockcount
+ PREV
.br_blockcount
);
2751 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2753 xfs_iext_remove(ip
, *idx
+ 1, 1, state
);
2754 ip
->i_d
.di_nextents
--;
2756 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2758 rval
= XFS_ILOG_CORE
;
2759 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2760 PREV
.br_startblock
, PREV
.br_blockcount
,
2763 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2764 if ((error
= xfs_btree_delete(cur
, &i
)))
2766 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2767 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2769 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2770 if ((error
= xfs_bmbt_update(cur
, LEFT
.br_startoff
,
2772 LEFT
.br_blockcount
+ PREV
.br_blockcount
,
2778 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2780 * Setting all of a previous oldext extent to newext.
2781 * The right neighbor is contiguous, the left is not.
2783 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2784 xfs_bmbt_set_blockcount(ep
,
2785 PREV
.br_blockcount
+ RIGHT
.br_blockcount
);
2786 xfs_bmbt_set_state(ep
, newext
);
2787 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2788 xfs_iext_remove(ip
, *idx
+ 1, 1, state
);
2789 ip
->i_d
.di_nextents
--;
2791 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2793 rval
= XFS_ILOG_CORE
;
2794 if ((error
= xfs_bmbt_lookup_eq(cur
, RIGHT
.br_startoff
,
2795 RIGHT
.br_startblock
,
2796 RIGHT
.br_blockcount
, &i
)))
2798 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2799 if ((error
= xfs_btree_delete(cur
, &i
)))
2801 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2802 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2804 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2805 if ((error
= xfs_bmbt_update(cur
, new->br_startoff
,
2807 new->br_blockcount
+ RIGHT
.br_blockcount
,
2813 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
2815 * Setting all of a previous oldext extent to newext.
2816 * Neither the left nor right neighbors are contiguous with
2819 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2820 xfs_bmbt_set_state(ep
, newext
);
2821 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2824 rval
= XFS_ILOG_DEXT
;
2827 if ((error
= xfs_bmbt_lookup_eq(cur
, new->br_startoff
,
2828 new->br_startblock
, new->br_blockcount
,
2831 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2832 if ((error
= xfs_bmbt_update(cur
, new->br_startoff
,
2833 new->br_startblock
, new->br_blockcount
,
2839 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
:
2841 * Setting the first part of a previous oldext extent to newext.
2842 * The left neighbor is contiguous.
2844 trace_xfs_bmap_pre_update(ip
, *idx
- 1, state
, _THIS_IP_
);
2845 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
- 1),
2846 LEFT
.br_blockcount
+ new->br_blockcount
);
2847 xfs_bmbt_set_startoff(ep
,
2848 PREV
.br_startoff
+ new->br_blockcount
);
2849 trace_xfs_bmap_post_update(ip
, *idx
- 1, state
, _THIS_IP_
);
2851 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2852 xfs_bmbt_set_startblock(ep
,
2853 new->br_startblock
+ new->br_blockcount
);
2854 xfs_bmbt_set_blockcount(ep
,
2855 PREV
.br_blockcount
- new->br_blockcount
);
2856 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2861 rval
= XFS_ILOG_DEXT
;
2864 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2865 PREV
.br_startblock
, PREV
.br_blockcount
,
2868 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2869 if ((error
= xfs_bmbt_update(cur
,
2870 PREV
.br_startoff
+ new->br_blockcount
,
2871 PREV
.br_startblock
+ new->br_blockcount
,
2872 PREV
.br_blockcount
- new->br_blockcount
,
2875 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2877 error
= xfs_bmbt_update(cur
, LEFT
.br_startoff
,
2879 LEFT
.br_blockcount
+ new->br_blockcount
,
2886 case BMAP_LEFT_FILLING
:
2888 * Setting the first part of a previous oldext extent to newext.
2889 * The left neighbor is not contiguous.
2891 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2892 ASSERT(ep
&& xfs_bmbt_get_state(ep
) == oldext
);
2893 xfs_bmbt_set_startoff(ep
, new_endoff
);
2894 xfs_bmbt_set_blockcount(ep
,
2895 PREV
.br_blockcount
- new->br_blockcount
);
2896 xfs_bmbt_set_startblock(ep
,
2897 new->br_startblock
+ new->br_blockcount
);
2898 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2900 xfs_iext_insert(ip
, *idx
, 1, new, state
);
2901 ip
->i_d
.di_nextents
++;
2903 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2905 rval
= XFS_ILOG_CORE
;
2906 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2907 PREV
.br_startblock
, PREV
.br_blockcount
,
2910 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2911 if ((error
= xfs_bmbt_update(cur
,
2912 PREV
.br_startoff
+ new->br_blockcount
,
2913 PREV
.br_startblock
+ new->br_blockcount
,
2914 PREV
.br_blockcount
- new->br_blockcount
,
2917 cur
->bc_rec
.b
= *new;
2918 if ((error
= xfs_btree_insert(cur
, &i
)))
2920 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2924 case BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2926 * Setting the last part of a previous oldext extent to newext.
2927 * The right neighbor is contiguous with the new allocation.
2929 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2930 xfs_bmbt_set_blockcount(ep
,
2931 PREV
.br_blockcount
- new->br_blockcount
);
2932 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2936 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2937 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp
, *idx
),
2938 new->br_startoff
, new->br_startblock
,
2939 new->br_blockcount
+ RIGHT
.br_blockcount
, newext
);
2940 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2943 rval
= XFS_ILOG_DEXT
;
2946 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2948 PREV
.br_blockcount
, &i
)))
2950 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2951 if ((error
= xfs_bmbt_update(cur
, PREV
.br_startoff
,
2953 PREV
.br_blockcount
- new->br_blockcount
,
2956 if ((error
= xfs_btree_increment(cur
, 0, &i
)))
2958 if ((error
= xfs_bmbt_update(cur
, new->br_startoff
,
2960 new->br_blockcount
+ RIGHT
.br_blockcount
,
2966 case BMAP_RIGHT_FILLING
:
2968 * Setting the last part of a previous oldext extent to newext.
2969 * The right neighbor is not contiguous.
2971 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2972 xfs_bmbt_set_blockcount(ep
,
2973 PREV
.br_blockcount
- new->br_blockcount
);
2974 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2977 xfs_iext_insert(ip
, *idx
, 1, new, state
);
2979 ip
->i_d
.di_nextents
++;
2981 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2983 rval
= XFS_ILOG_CORE
;
2984 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2985 PREV
.br_startblock
, PREV
.br_blockcount
,
2988 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2989 if ((error
= xfs_bmbt_update(cur
, PREV
.br_startoff
,
2991 PREV
.br_blockcount
- new->br_blockcount
,
2994 if ((error
= xfs_bmbt_lookup_eq(cur
, new->br_startoff
,
2995 new->br_startblock
, new->br_blockcount
,
2998 XFS_WANT_CORRUPTED_GOTO(i
== 0, done
);
2999 cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
3000 if ((error
= xfs_btree_insert(cur
, &i
)))
3002 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
3008 * Setting the middle part of a previous oldext extent to
3009 * newext. Contiguity is impossible here.
3010 * One extent becomes three extents.
3012 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
3013 xfs_bmbt_set_blockcount(ep
,
3014 new->br_startoff
- PREV
.br_startoff
);
3015 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
3018 r
[1].br_startoff
= new_endoff
;
3019 r
[1].br_blockcount
=
3020 PREV
.br_startoff
+ PREV
.br_blockcount
- new_endoff
;
3021 r
[1].br_startblock
= new->br_startblock
+ new->br_blockcount
;
3022 r
[1].br_state
= oldext
;
3025 xfs_iext_insert(ip
, *idx
, 2, &r
[0], state
);
3027 ip
->i_d
.di_nextents
+= 2;
3029 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
3031 rval
= XFS_ILOG_CORE
;
3032 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
3033 PREV
.br_startblock
, PREV
.br_blockcount
,
3036 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
3037 /* new right extent - oldext */
3038 if ((error
= xfs_bmbt_update(cur
, r
[1].br_startoff
,
3039 r
[1].br_startblock
, r
[1].br_blockcount
,
3042 /* new left extent - oldext */
3043 cur
->bc_rec
.b
= PREV
;
3044 cur
->bc_rec
.b
.br_blockcount
=
3045 new->br_startoff
- PREV
.br_startoff
;
3046 if ((error
= xfs_btree_insert(cur
, &i
)))
3048 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
3050 * Reset the cursor to the position of the new extent
3051 * we are about to insert as we can't trust it after
3052 * the previous insert.
3054 if ((error
= xfs_bmbt_lookup_eq(cur
, new->br_startoff
,
3055 new->br_startblock
, new->br_blockcount
,
3058 XFS_WANT_CORRUPTED_GOTO(i
== 0, done
);
3059 /* new middle extent - newext */
3060 cur
->bc_rec
.b
.br_state
= new->br_state
;
3061 if ((error
= xfs_btree_insert(cur
, &i
)))
3063 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
3067 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
3068 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
3069 case BMAP_LEFT_FILLING
| BMAP_RIGHT_CONTIG
:
3070 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
3071 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
3072 case BMAP_LEFT_CONTIG
:
3073 case BMAP_RIGHT_CONTIG
:
3075 * These cases are all impossible.
3080 /* convert to a btree if necessary */
3081 if (xfs_bmap_needs_btree(ip
, XFS_DATA_FORK
)) {
3082 int tmp_logflags
; /* partial log flag return val */
3084 ASSERT(cur
== NULL
);
3085 error
= xfs_bmap_extents_to_btree(tp
, ip
, first
, flist
, &cur
,
3086 0, &tmp_logflags
, XFS_DATA_FORK
);
3087 *logflagsp
|= tmp_logflags
;
3092 /* clear out the allocated field, done with it now in any case. */
3094 cur
->bc_private
.b
.allocated
= 0;
3098 xfs_bmap_check_leaf_extents(*curp
, ip
, XFS_DATA_FORK
);
3108 * Convert a hole to a delayed allocation.
3111 xfs_bmap_add_extent_hole_delay(
3112 xfs_inode_t
*ip
, /* incore inode pointer */
3113 xfs_extnum_t
*idx
, /* extent number to update/insert */
3114 xfs_bmbt_irec_t
*new) /* new data to add to file extents */
3116 xfs_ifork_t
*ifp
; /* inode fork pointer */
3117 xfs_bmbt_irec_t left
; /* left neighbor extent entry */
3118 xfs_filblks_t newlen
=0; /* new indirect size */
3119 xfs_filblks_t oldlen
=0; /* old indirect size */
3120 xfs_bmbt_irec_t right
; /* right neighbor extent entry */
3121 int state
; /* state bits, accessed thru macros */
3122 xfs_filblks_t temp
=0; /* temp for indirect calculations */
3124 ifp
= XFS_IFORK_PTR(ip
, XFS_DATA_FORK
);
3126 ASSERT(isnullstartblock(new->br_startblock
));
3129 * Check and set flags if this segment has a left neighbor
3132 state
|= BMAP_LEFT_VALID
;
3133 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, *idx
- 1), &left
);
3135 if (isnullstartblock(left
.br_startblock
))
3136 state
|= BMAP_LEFT_DELAY
;
3140 * Check and set flags if the current (right) segment exists.
3141 * If it doesn't exist, we're converting the hole at end-of-file.
3143 if (*idx
< ip
->i_df
.if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
)) {
3144 state
|= BMAP_RIGHT_VALID
;
3145 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, *idx
), &right
);
3147 if (isnullstartblock(right
.br_startblock
))
3148 state
|= BMAP_RIGHT_DELAY
;
3152 * Set contiguity flags on the left and right neighbors.
3153 * Don't let extents get too large, even if the pieces are contiguous.
3155 if ((state
& BMAP_LEFT_VALID
) && (state
& BMAP_LEFT_DELAY
) &&
3156 left
.br_startoff
+ left
.br_blockcount
== new->br_startoff
&&
3157 left
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
3158 state
|= BMAP_LEFT_CONTIG
;
3160 if ((state
& BMAP_RIGHT_VALID
) && (state
& BMAP_RIGHT_DELAY
) &&
3161 new->br_startoff
+ new->br_blockcount
== right
.br_startoff
&&
3162 new->br_blockcount
+ right
.br_blockcount
<= MAXEXTLEN
&&
3163 (!(state
& BMAP_LEFT_CONTIG
) ||
3164 (left
.br_blockcount
+ new->br_blockcount
+
3165 right
.br_blockcount
<= MAXEXTLEN
)))
3166 state
|= BMAP_RIGHT_CONTIG
;
3169 * Switch out based on the contiguity flags.
3171 switch (state
& (BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
)) {
3172 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
3174 * New allocation is contiguous with delayed allocations
3175 * on the left and on the right.
3176 * Merge all three into a single extent record.
3179 temp
= left
.br_blockcount
+ new->br_blockcount
+
3180 right
.br_blockcount
;
3182 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
3183 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
), temp
);
3184 oldlen
= startblockval(left
.br_startblock
) +
3185 startblockval(new->br_startblock
) +
3186 startblockval(right
.br_startblock
);
3187 newlen
= xfs_bmap_worst_indlen(ip
, temp
);
3188 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp
, *idx
),
3189 nullstartblock((int)newlen
));
3190 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
3192 xfs_iext_remove(ip
, *idx
+ 1, 1, state
);
3195 case BMAP_LEFT_CONTIG
:
3197 * New allocation is contiguous with a delayed allocation
3199 * Merge the new allocation with the left neighbor.
3202 temp
= left
.br_blockcount
+ new->br_blockcount
;
3204 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
3205 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
), temp
);
3206 oldlen
= startblockval(left
.br_startblock
) +
3207 startblockval(new->br_startblock
);
3208 newlen
= xfs_bmap_worst_indlen(ip
, temp
);
3209 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp
, *idx
),
3210 nullstartblock((int)newlen
));
3211 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
3214 case BMAP_RIGHT_CONTIG
:
3216 * New allocation is contiguous with a delayed allocation
3218 * Merge the new allocation with the right neighbor.
3220 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
3221 temp
= new->br_blockcount
+ right
.br_blockcount
;
3222 oldlen
= startblockval(new->br_startblock
) +
3223 startblockval(right
.br_startblock
);
3224 newlen
= xfs_bmap_worst_indlen(ip
, temp
);
3225 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp
, *idx
),
3227 nullstartblock((int)newlen
), temp
, right
.br_state
);
3228 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
3233 * New allocation is not contiguous with another
3234 * delayed allocation.
3235 * Insert a new entry.
3237 oldlen
= newlen
= 0;
3238 xfs_iext_insert(ip
, *idx
, 1, new, state
);
3241 if (oldlen
!= newlen
) {
3242 ASSERT(oldlen
> newlen
);
3243 xfs_icsb_modify_counters(ip
->i_mount
, XFS_SBS_FDBLOCKS
,
3244 (int64_t)(oldlen
- newlen
), 0);
3246 * Nothing to do for disk quota accounting here.
3252 * Convert a hole to a real allocation.
3254 STATIC
int /* error */
3255 xfs_bmap_add_extent_hole_real(
3256 struct xfs_bmalloca
*bma
,
3259 struct xfs_bmbt_irec
*new = &bma
->got
;
3260 int error
; /* error return value */
3261 int i
; /* temp state */
3262 xfs_ifork_t
*ifp
; /* inode fork pointer */
3263 xfs_bmbt_irec_t left
; /* left neighbor extent entry */
3264 xfs_bmbt_irec_t right
; /* right neighbor extent entry */
3265 int rval
=0; /* return value (logging flags) */
3266 int state
; /* state bits, accessed thru macros */
3268 ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
3270 ASSERT(bma
->idx
>= 0);
3271 ASSERT(bma
->idx
<= ifp
->if_bytes
/ sizeof(struct xfs_bmbt_rec
));
3272 ASSERT(!isnullstartblock(new->br_startblock
));
3274 !(bma
->cur
->bc_private
.b
.flags
& XFS_BTCUR_BPRV_WASDEL
));
3276 XFS_STATS_INC(xs_add_exlist
);
3279 if (whichfork
== XFS_ATTR_FORK
)
3280 state
|= BMAP_ATTRFORK
;
3283 * Check and set flags if this segment has a left neighbor.
3286 state
|= BMAP_LEFT_VALID
;
3287 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
- 1), &left
);
3288 if (isnullstartblock(left
.br_startblock
))
3289 state
|= BMAP_LEFT_DELAY
;
3293 * Check and set flags if this segment has a current value.
3294 * Not true if we're inserting into the "hole" at eof.
3296 if (bma
->idx
< ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
)) {
3297 state
|= BMAP_RIGHT_VALID
;
3298 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
), &right
);
3299 if (isnullstartblock(right
.br_startblock
))
3300 state
|= BMAP_RIGHT_DELAY
;
3304 * We're inserting a real allocation between "left" and "right".
3305 * Set the contiguity flags. Don't let extents get too large.
3307 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
3308 left
.br_startoff
+ left
.br_blockcount
== new->br_startoff
&&
3309 left
.br_startblock
+ left
.br_blockcount
== new->br_startblock
&&
3310 left
.br_state
== new->br_state
&&
3311 left
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
3312 state
|= BMAP_LEFT_CONTIG
;
3314 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
3315 new->br_startoff
+ new->br_blockcount
== right
.br_startoff
&&
3316 new->br_startblock
+ new->br_blockcount
== right
.br_startblock
&&
3317 new->br_state
== right
.br_state
&&
3318 new->br_blockcount
+ right
.br_blockcount
<= MAXEXTLEN
&&
3319 (!(state
& BMAP_LEFT_CONTIG
) ||
3320 left
.br_blockcount
+ new->br_blockcount
+
3321 right
.br_blockcount
<= MAXEXTLEN
))
3322 state
|= BMAP_RIGHT_CONTIG
;
3326 * Select which case we're in here, and implement it.
3328 switch (state
& (BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
)) {
3329 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
3331 * New allocation is contiguous with real allocations on the
3332 * left and on the right.
3333 * Merge all three into a single extent record.
3336 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
3337 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, bma
->idx
),
3338 left
.br_blockcount
+ new->br_blockcount
+
3339 right
.br_blockcount
);
3340 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
3342 xfs_iext_remove(bma
->ip
, bma
->idx
+ 1, 1, state
);
3344 XFS_IFORK_NEXT_SET(bma
->ip
, whichfork
,
3345 XFS_IFORK_NEXTENTS(bma
->ip
, whichfork
) - 1);
3346 if (bma
->cur
== NULL
) {
3347 rval
= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
3349 rval
= XFS_ILOG_CORE
;
3350 error
= xfs_bmbt_lookup_eq(bma
->cur
, right
.br_startoff
,
3351 right
.br_startblock
, right
.br_blockcount
,
3355 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
3356 error
= xfs_btree_delete(bma
->cur
, &i
);
3359 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
3360 error
= xfs_btree_decrement(bma
->cur
, 0, &i
);
3363 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
3364 error
= xfs_bmbt_update(bma
->cur
, left
.br_startoff
,
3366 left
.br_blockcount
+
3367 new->br_blockcount
+
3368 right
.br_blockcount
,
3375 case BMAP_LEFT_CONTIG
:
3377 * New allocation is contiguous with a real allocation
3379 * Merge the new allocation with the left neighbor.
3382 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
3383 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, bma
->idx
),
3384 left
.br_blockcount
+ new->br_blockcount
);
3385 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
3387 if (bma
->cur
== NULL
) {
3388 rval
= xfs_ilog_fext(whichfork
);
3391 error
= xfs_bmbt_lookup_eq(bma
->cur
, left
.br_startoff
,
3392 left
.br_startblock
, left
.br_blockcount
,
3396 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
3397 error
= xfs_bmbt_update(bma
->cur
, left
.br_startoff
,
3399 left
.br_blockcount
+
3407 case BMAP_RIGHT_CONTIG
:
3409 * New allocation is contiguous with a real allocation
3411 * Merge the new allocation with the right neighbor.
3413 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
3414 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp
, bma
->idx
),
3415 new->br_startoff
, new->br_startblock
,
3416 new->br_blockcount
+ right
.br_blockcount
,
3418 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
3420 if (bma
->cur
== NULL
) {
3421 rval
= xfs_ilog_fext(whichfork
);
3424 error
= xfs_bmbt_lookup_eq(bma
->cur
,
3426 right
.br_startblock
,
3427 right
.br_blockcount
, &i
);
3430 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
3431 error
= xfs_bmbt_update(bma
->cur
, new->br_startoff
,
3433 new->br_blockcount
+
3434 right
.br_blockcount
,
3443 * New allocation is not contiguous with another
3445 * Insert a new entry.
3447 xfs_iext_insert(bma
->ip
, bma
->idx
, 1, new, state
);
3448 XFS_IFORK_NEXT_SET(bma
->ip
, whichfork
,
3449 XFS_IFORK_NEXTENTS(bma
->ip
, whichfork
) + 1);
3450 if (bma
->cur
== NULL
) {
3451 rval
= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
3453 rval
= XFS_ILOG_CORE
;
3454 error
= xfs_bmbt_lookup_eq(bma
->cur
,
3457 new->br_blockcount
, &i
);
3460 XFS_WANT_CORRUPTED_GOTO(i
== 0, done
);
3461 bma
->cur
->bc_rec
.b
.br_state
= new->br_state
;
3462 error
= xfs_btree_insert(bma
->cur
, &i
);
3465 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
3470 /* convert to a btree if necessary */
3471 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
3472 int tmp_logflags
; /* partial log flag return val */
3474 ASSERT(bma
->cur
== NULL
);
3475 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
3476 bma
->firstblock
, bma
->flist
, &bma
->cur
,
3477 0, &tmp_logflags
, whichfork
);
3478 bma
->logflags
|= tmp_logflags
;
3483 /* clear out the allocated field, done with it now in any case. */
3485 bma
->cur
->bc_private
.b
.allocated
= 0;
3487 xfs_bmap_check_leaf_extents(bma
->cur
, bma
->ip
, whichfork
);
3489 bma
->logflags
|= rval
;
3494 * Functions used in the extent read, allocate and remove paths
3498 * Adjust the size of the new extent based on di_extsize and rt extsize.
3501 xfs_bmap_extsize_align(
3503 xfs_bmbt_irec_t
*gotp
, /* next extent pointer */
3504 xfs_bmbt_irec_t
*prevp
, /* previous extent pointer */
3505 xfs_extlen_t extsz
, /* align to this extent size */
3506 int rt
, /* is this a realtime inode? */
3507 int eof
, /* is extent at end-of-file? */
3508 int delay
, /* creating delalloc extent? */
3509 int convert
, /* overwriting unwritten extent? */
3510 xfs_fileoff_t
*offp
, /* in/out: aligned offset */
3511 xfs_extlen_t
*lenp
) /* in/out: aligned length */
3513 xfs_fileoff_t orig_off
; /* original offset */
3514 xfs_extlen_t orig_alen
; /* original length */
3515 xfs_fileoff_t orig_end
; /* original off+len */
3516 xfs_fileoff_t nexto
; /* next file offset */
3517 xfs_fileoff_t prevo
; /* previous file offset */
3518 xfs_fileoff_t align_off
; /* temp for offset */
3519 xfs_extlen_t align_alen
; /* temp for length */
3520 xfs_extlen_t temp
; /* temp for calculations */
3525 orig_off
= align_off
= *offp
;
3526 orig_alen
= align_alen
= *lenp
;
3527 orig_end
= orig_off
+ orig_alen
;
3530 * If this request overlaps an existing extent, then don't
3531 * attempt to perform any additional alignment.
3533 if (!delay
&& !eof
&&
3534 (orig_off
>= gotp
->br_startoff
) &&
3535 (orig_end
<= gotp
->br_startoff
+ gotp
->br_blockcount
)) {
3540 * If the file offset is unaligned vs. the extent size
3541 * we need to align it. This will be possible unless
3542 * the file was previously written with a kernel that didn't
3543 * perform this alignment, or if a truncate shot us in the
3546 temp
= do_mod(orig_off
, extsz
);
3552 * Same adjustment for the end of the requested area.
3554 if ((temp
= (align_alen
% extsz
))) {
3555 align_alen
+= extsz
- temp
;
3558 * If the previous block overlaps with this proposed allocation
3559 * then move the start forward without adjusting the length.
3561 if (prevp
->br_startoff
!= NULLFILEOFF
) {
3562 if (prevp
->br_startblock
== HOLESTARTBLOCK
)
3563 prevo
= prevp
->br_startoff
;
3565 prevo
= prevp
->br_startoff
+ prevp
->br_blockcount
;
3568 if (align_off
!= orig_off
&& align_off
< prevo
)
3571 * If the next block overlaps with this proposed allocation
3572 * then move the start back without adjusting the length,
3573 * but not before offset 0.
3574 * This may of course make the start overlap previous block,
3575 * and if we hit the offset 0 limit then the next block
3576 * can still overlap too.
3578 if (!eof
&& gotp
->br_startoff
!= NULLFILEOFF
) {
3579 if ((delay
&& gotp
->br_startblock
== HOLESTARTBLOCK
) ||
3580 (!delay
&& gotp
->br_startblock
== DELAYSTARTBLOCK
))
3581 nexto
= gotp
->br_startoff
+ gotp
->br_blockcount
;
3583 nexto
= gotp
->br_startoff
;
3585 nexto
= NULLFILEOFF
;
3587 align_off
+ align_alen
!= orig_end
&&
3588 align_off
+ align_alen
> nexto
)
3589 align_off
= nexto
> align_alen
? nexto
- align_alen
: 0;
3591 * If we're now overlapping the next or previous extent that
3592 * means we can't fit an extsz piece in this hole. Just move
3593 * the start forward to the first valid spot and set
3594 * the length so we hit the end.
3596 if (align_off
!= orig_off
&& align_off
< prevo
)
3598 if (align_off
+ align_alen
!= orig_end
&&
3599 align_off
+ align_alen
> nexto
&&
3600 nexto
!= NULLFILEOFF
) {
3601 ASSERT(nexto
> prevo
);
3602 align_alen
= nexto
- align_off
;
3606 * If realtime, and the result isn't a multiple of the realtime
3607 * extent size we need to remove blocks until it is.
3609 if (rt
&& (temp
= (align_alen
% mp
->m_sb
.sb_rextsize
))) {
3611 * We're not covering the original request, or
3612 * we won't be able to once we fix the length.
3614 if (orig_off
< align_off
||
3615 orig_end
> align_off
+ align_alen
||
3616 align_alen
- temp
< orig_alen
)
3617 return XFS_ERROR(EINVAL
);
3619 * Try to fix it by moving the start up.
3621 if (align_off
+ temp
<= orig_off
) {
3626 * Try to fix it by moving the end in.
3628 else if (align_off
+ align_alen
- temp
>= orig_end
)
3631 * Set the start to the minimum then trim the length.
3634 align_alen
-= orig_off
- align_off
;
3635 align_off
= orig_off
;
3636 align_alen
-= align_alen
% mp
->m_sb
.sb_rextsize
;
3639 * Result doesn't cover the request, fail it.
3641 if (orig_off
< align_off
|| orig_end
> align_off
+ align_alen
)
3642 return XFS_ERROR(EINVAL
);
3644 ASSERT(orig_off
>= align_off
);
3645 ASSERT(orig_end
<= align_off
+ align_alen
);
3649 if (!eof
&& gotp
->br_startoff
!= NULLFILEOFF
)
3650 ASSERT(align_off
+ align_alen
<= gotp
->br_startoff
);
3651 if (prevp
->br_startoff
!= NULLFILEOFF
)
3652 ASSERT(align_off
>= prevp
->br_startoff
+ prevp
->br_blockcount
);
3660 #define XFS_ALLOC_GAP_UNITS 4
3664 xfs_bmalloca_t
*ap
) /* bmap alloc argument struct */
3666 xfs_fsblock_t adjust
; /* adjustment to block numbers */
3667 xfs_agnumber_t fb_agno
; /* ag number of ap->firstblock */
3668 xfs_mount_t
*mp
; /* mount point structure */
3669 int nullfb
; /* true if ap->firstblock isn't set */
3670 int rt
; /* true if inode is realtime */
3672 #define ISVALID(x,y) \
3674 (x) < mp->m_sb.sb_rblocks : \
3675 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
3676 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
3677 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
3679 mp
= ap
->ip
->i_mount
;
3680 nullfb
= *ap
->firstblock
== NULLFSBLOCK
;
3681 rt
= XFS_IS_REALTIME_INODE(ap
->ip
) && ap
->userdata
;
3682 fb_agno
= nullfb
? NULLAGNUMBER
: XFS_FSB_TO_AGNO(mp
, *ap
->firstblock
);
3684 * If allocating at eof, and there's a previous real block,
3685 * try to use its last block as our starting point.
3687 if (ap
->eof
&& ap
->prev
.br_startoff
!= NULLFILEOFF
&&
3688 !isnullstartblock(ap
->prev
.br_startblock
) &&
3689 ISVALID(ap
->prev
.br_startblock
+ ap
->prev
.br_blockcount
,
3690 ap
->prev
.br_startblock
)) {
3691 ap
->blkno
= ap
->prev
.br_startblock
+ ap
->prev
.br_blockcount
;
3693 * Adjust for the gap between prevp and us.
3695 adjust
= ap
->offset
-
3696 (ap
->prev
.br_startoff
+ ap
->prev
.br_blockcount
);
3698 ISVALID(ap
->blkno
+ adjust
, ap
->prev
.br_startblock
))
3699 ap
->blkno
+= adjust
;
3702 * If not at eof, then compare the two neighbor blocks.
3703 * Figure out whether either one gives us a good starting point,
3704 * and pick the better one.
3706 else if (!ap
->eof
) {
3707 xfs_fsblock_t gotbno
; /* right side block number */
3708 xfs_fsblock_t gotdiff
=0; /* right side difference */
3709 xfs_fsblock_t prevbno
; /* left side block number */
3710 xfs_fsblock_t prevdiff
=0; /* left side difference */
3713 * If there's a previous (left) block, select a requested
3714 * start block based on it.
3716 if (ap
->prev
.br_startoff
!= NULLFILEOFF
&&
3717 !isnullstartblock(ap
->prev
.br_startblock
) &&
3718 (prevbno
= ap
->prev
.br_startblock
+
3719 ap
->prev
.br_blockcount
) &&
3720 ISVALID(prevbno
, ap
->prev
.br_startblock
)) {
3722 * Calculate gap to end of previous block.
3724 adjust
= prevdiff
= ap
->offset
-
3725 (ap
->prev
.br_startoff
+
3726 ap
->prev
.br_blockcount
);
3728 * Figure the startblock based on the previous block's
3729 * end and the gap size.
3731 * If the gap is large relative to the piece we're
3732 * allocating, or using it gives us an invalid block
3733 * number, then just use the end of the previous block.
3735 if (prevdiff
<= XFS_ALLOC_GAP_UNITS
* ap
->length
&&
3736 ISVALID(prevbno
+ prevdiff
,
3737 ap
->prev
.br_startblock
))
3742 * If the firstblock forbids it, can't use it,
3745 if (!rt
&& !nullfb
&&
3746 XFS_FSB_TO_AGNO(mp
, prevbno
) != fb_agno
)
3747 prevbno
= NULLFSBLOCK
;
3750 * No previous block or can't follow it, just default.
3753 prevbno
= NULLFSBLOCK
;
3755 * If there's a following (right) block, select a requested
3756 * start block based on it.
3758 if (!isnullstartblock(ap
->got
.br_startblock
)) {
3760 * Calculate gap to start of next block.
3762 adjust
= gotdiff
= ap
->got
.br_startoff
- ap
->offset
;
3764 * Figure the startblock based on the next block's
3765 * start and the gap size.
3767 gotbno
= ap
->got
.br_startblock
;
3770 * If the gap is large relative to the piece we're
3771 * allocating, or using it gives us an invalid block
3772 * number, then just use the start of the next block
3773 * offset by our length.
3775 if (gotdiff
<= XFS_ALLOC_GAP_UNITS
* ap
->length
&&
3776 ISVALID(gotbno
- gotdiff
, gotbno
))
3778 else if (ISVALID(gotbno
- ap
->length
, gotbno
)) {
3779 gotbno
-= ap
->length
;
3780 gotdiff
+= adjust
- ap
->length
;
3784 * If the firstblock forbids it, can't use it,
3787 if (!rt
&& !nullfb
&&
3788 XFS_FSB_TO_AGNO(mp
, gotbno
) != fb_agno
)
3789 gotbno
= NULLFSBLOCK
;
3792 * No next block, just default.
3795 gotbno
= NULLFSBLOCK
;
3797 * If both valid, pick the better one, else the only good
3798 * one, else ap->blkno is already set (to 0 or the inode block).
3800 if (prevbno
!= NULLFSBLOCK
&& gotbno
!= NULLFSBLOCK
)
3801 ap
->blkno
= prevdiff
<= gotdiff
? prevbno
: gotbno
;
3802 else if (prevbno
!= NULLFSBLOCK
)
3803 ap
->blkno
= prevbno
;
3804 else if (gotbno
!= NULLFSBLOCK
)
3812 xfs_bmalloca_t
*ap
) /* bmap alloc argument struct */
3814 xfs_alloctype_t atype
= 0; /* type for allocation routines */
3815 int error
; /* error return value */
3816 xfs_mount_t
*mp
; /* mount point structure */
3817 xfs_extlen_t prod
= 0; /* product factor for allocators */
3818 xfs_extlen_t ralen
= 0; /* realtime allocation length */
3819 xfs_extlen_t align
; /* minimum allocation alignment */
3822 mp
= ap
->ip
->i_mount
;
3823 align
= xfs_get_extsz_hint(ap
->ip
);
3824 prod
= align
/ mp
->m_sb
.sb_rextsize
;
3825 error
= xfs_bmap_extsize_align(mp
, &ap
->got
, &ap
->prev
,
3826 align
, 1, ap
->eof
, 0,
3827 ap
->conv
, &ap
->offset
, &ap
->length
);
3831 ASSERT(ap
->length
% mp
->m_sb
.sb_rextsize
== 0);
3834 * If the offset & length are not perfectly aligned
3835 * then kill prod, it will just get us in trouble.
3837 if (do_mod(ap
->offset
, align
) || ap
->length
% align
)
3840 * Set ralen to be the actual requested length in rtextents.
3842 ralen
= ap
->length
/ mp
->m_sb
.sb_rextsize
;
3844 * If the old value was close enough to MAXEXTLEN that
3845 * we rounded up to it, cut it back so it's valid again.
3846 * Note that if it's a really large request (bigger than
3847 * MAXEXTLEN), we don't hear about that number, and can't
3848 * adjust the starting point to match it.
3850 if (ralen
* mp
->m_sb
.sb_rextsize
>= MAXEXTLEN
)
3851 ralen
= MAXEXTLEN
/ mp
->m_sb
.sb_rextsize
;
3854 * Lock out other modifications to the RT bitmap inode.
3856 xfs_ilock(mp
->m_rbmip
, XFS_ILOCK_EXCL
);
3857 xfs_trans_ijoin(ap
->tp
, mp
->m_rbmip
, XFS_ILOCK_EXCL
);
3860 * If it's an allocation to an empty file at offset 0,
3861 * pick an extent that will space things out in the rt area.
3863 if (ap
->eof
&& ap
->offset
== 0) {
3864 xfs_rtblock_t
uninitialized_var(rtx
); /* realtime extent no */
3866 error
= xfs_rtpick_extent(mp
, ap
->tp
, ralen
, &rtx
);
3869 ap
->blkno
= rtx
* mp
->m_sb
.sb_rextsize
;
3874 xfs_bmap_adjacent(ap
);
3877 * Realtime allocation, done through xfs_rtallocate_extent.
3879 atype
= ap
->blkno
== 0 ? XFS_ALLOCTYPE_ANY_AG
: XFS_ALLOCTYPE_NEAR_BNO
;
3880 do_div(ap
->blkno
, mp
->m_sb
.sb_rextsize
);
3883 if ((error
= xfs_rtallocate_extent(ap
->tp
, ap
->blkno
, 1, ap
->length
,
3884 &ralen
, atype
, ap
->wasdel
, prod
, &rtb
)))
3886 if (rtb
== NULLFSBLOCK
&& prod
> 1 &&
3887 (error
= xfs_rtallocate_extent(ap
->tp
, ap
->blkno
, 1,
3888 ap
->length
, &ralen
, atype
,
3889 ap
->wasdel
, 1, &rtb
)))
3892 if (ap
->blkno
!= NULLFSBLOCK
) {
3893 ap
->blkno
*= mp
->m_sb
.sb_rextsize
;
3894 ralen
*= mp
->m_sb
.sb_rextsize
;
3896 ap
->ip
->i_d
.di_nblocks
+= ralen
;
3897 xfs_trans_log_inode(ap
->tp
, ap
->ip
, XFS_ILOG_CORE
);
3899 ap
->ip
->i_delayed_blks
-= ralen
;
3901 * Adjust the disk quota also. This was reserved
3904 xfs_trans_mod_dquot_byino(ap
->tp
, ap
->ip
,
3905 ap
->wasdel
? XFS_TRANS_DQ_DELRTBCOUNT
:
3906 XFS_TRANS_DQ_RTBCOUNT
, (long) ralen
);
3914 xfs_bmap_btalloc_nullfb(
3915 struct xfs_bmalloca
*ap
,
3916 struct xfs_alloc_arg
*args
,
3919 struct xfs_mount
*mp
= ap
->ip
->i_mount
;
3920 struct xfs_perag
*pag
;
3921 xfs_agnumber_t ag
, startag
;
3925 if (ap
->userdata
&& xfs_inode_is_filestream(ap
->ip
))
3926 args
->type
= XFS_ALLOCTYPE_NEAR_BNO
;
3928 args
->type
= XFS_ALLOCTYPE_START_BNO
;
3929 args
->total
= ap
->total
;
3932 * Search for an allocation group with a single extent large enough
3933 * for the request. If one isn't found, then adjust the minimum
3934 * allocation size to the largest space found.
3936 startag
= ag
= XFS_FSB_TO_AGNO(mp
, args
->fsbno
);
3937 if (startag
== NULLAGNUMBER
)
3940 pag
= xfs_perag_get(mp
, ag
);
3941 while (*blen
< args
->maxlen
) {
3942 if (!pag
->pagf_init
) {
3943 error
= xfs_alloc_pagf_init(mp
, args
->tp
, ag
,
3944 XFS_ALLOC_FLAG_TRYLOCK
);
3952 * See xfs_alloc_fix_freelist...
3954 if (pag
->pagf_init
) {
3955 xfs_extlen_t longest
;
3956 longest
= xfs_alloc_longest_free_extent(mp
, pag
);
3957 if (*blen
< longest
)
3962 if (xfs_inode_is_filestream(ap
->ip
)) {
3963 if (*blen
>= args
->maxlen
)
3968 * If startag is an invalid AG, we've
3969 * come here once before and
3970 * xfs_filestream_new_ag picked the
3971 * best currently available.
3973 * Don't continue looping, since we
3974 * could loop forever.
3976 if (startag
== NULLAGNUMBER
)
3979 error
= xfs_filestream_new_ag(ap
, &ag
);
3984 /* loop again to set 'blen'*/
3985 startag
= NULLAGNUMBER
;
3986 pag
= xfs_perag_get(mp
, ag
);
3990 if (++ag
== mp
->m_sb
.sb_agcount
)
3995 pag
= xfs_perag_get(mp
, ag
);
4000 * Since the above loop did a BUF_TRYLOCK, it is
4001 * possible that there is space for this request.
4003 if (notinit
|| *blen
< ap
->minlen
)
4004 args
->minlen
= ap
->minlen
;
4006 * If the best seen length is less than the request
4007 * length, use the best as the minimum.
4009 else if (*blen
< args
->maxlen
)
4010 args
->minlen
= *blen
;
4012 * Otherwise we've seen an extent as big as maxlen,
4013 * use that as the minimum.
4016 args
->minlen
= args
->maxlen
;
4019 * set the failure fallback case to look in the selected
4020 * AG as the stream may have moved.
4022 if (xfs_inode_is_filestream(ap
->ip
))
4023 ap
->blkno
= args
->fsbno
= XFS_AGB_TO_FSB(mp
, ag
, 0);
4030 xfs_bmalloca_t
*ap
) /* bmap alloc argument struct */
4032 xfs_mount_t
*mp
; /* mount point structure */
4033 xfs_alloctype_t atype
= 0; /* type for allocation routines */
4034 xfs_extlen_t align
; /* minimum allocation alignment */
4035 xfs_agnumber_t fb_agno
; /* ag number of ap->firstblock */
4037 xfs_alloc_arg_t args
;
4039 xfs_extlen_t nextminlen
= 0;
4040 int nullfb
; /* true if ap->firstblock isn't set */
4047 mp
= ap
->ip
->i_mount
;
4048 align
= ap
->userdata
? xfs_get_extsz_hint(ap
->ip
) : 0;
4049 if (unlikely(align
)) {
4050 error
= xfs_bmap_extsize_align(mp
, &ap
->got
, &ap
->prev
,
4051 align
, 0, ap
->eof
, 0, ap
->conv
,
4052 &ap
->offset
, &ap
->length
);
4056 nullfb
= *ap
->firstblock
== NULLFSBLOCK
;
4057 fb_agno
= nullfb
? NULLAGNUMBER
: XFS_FSB_TO_AGNO(mp
, *ap
->firstblock
);
4059 if (ap
->userdata
&& xfs_inode_is_filestream(ap
->ip
)) {
4060 ag
= xfs_filestream_lookup_ag(ap
->ip
);
4061 ag
= (ag
!= NULLAGNUMBER
) ? ag
: 0;
4062 ap
->blkno
= XFS_AGB_TO_FSB(mp
, ag
, 0);
4064 ap
->blkno
= XFS_INO_TO_FSB(mp
, ap
->ip
->i_ino
);
4067 ap
->blkno
= *ap
->firstblock
;
4069 xfs_bmap_adjacent(ap
);
4072 * If allowed, use ap->blkno; otherwise must use firstblock since
4073 * it's in the right allocation group.
4075 if (nullfb
|| XFS_FSB_TO_AGNO(mp
, ap
->blkno
) == fb_agno
)
4078 ap
->blkno
= *ap
->firstblock
;
4080 * Normal allocation, done through xfs_alloc_vextent.
4082 tryagain
= isaligned
= 0;
4083 memset(&args
, 0, sizeof(args
));
4086 args
.fsbno
= ap
->blkno
;
4088 /* Trim the allocation back to the maximum an AG can fit. */
4089 args
.maxlen
= MIN(ap
->length
, XFS_ALLOC_AG_MAX_USABLE(mp
));
4090 args
.firstblock
= *ap
->firstblock
;
4093 error
= xfs_bmap_btalloc_nullfb(ap
, &args
, &blen
);
4096 } else if (ap
->flist
->xbf_low
) {
4097 if (xfs_inode_is_filestream(ap
->ip
))
4098 args
.type
= XFS_ALLOCTYPE_FIRST_AG
;
4100 args
.type
= XFS_ALLOCTYPE_START_BNO
;
4101 args
.total
= args
.minlen
= ap
->minlen
;
4103 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
4104 args
.total
= ap
->total
;
4105 args
.minlen
= ap
->minlen
;
4107 /* apply extent size hints if obtained earlier */
4108 if (unlikely(align
)) {
4110 if ((args
.mod
= (xfs_extlen_t
)do_mod(ap
->offset
, args
.prod
)))
4111 args
.mod
= (xfs_extlen_t
)(args
.prod
- args
.mod
);
4112 } else if (mp
->m_sb
.sb_blocksize
>= PAGE_CACHE_SIZE
) {
4116 args
.prod
= PAGE_CACHE_SIZE
>> mp
->m_sb
.sb_blocklog
;
4117 if ((args
.mod
= (xfs_extlen_t
)(do_mod(ap
->offset
, args
.prod
))))
4118 args
.mod
= (xfs_extlen_t
)(args
.prod
- args
.mod
);
4121 * If we are not low on available data blocks, and the
4122 * underlying logical volume manager is a stripe, and
4123 * the file offset is zero then try to allocate data
4124 * blocks on stripe unit boundary.
4125 * NOTE: ap->aeof is only set if the allocation length
4126 * is >= the stripe unit and the allocation offset is
4127 * at the end of file.
4129 if (!ap
->flist
->xbf_low
&& ap
->aeof
) {
4131 args
.alignment
= mp
->m_dalign
;
4135 * Adjust for alignment
4137 if (blen
> args
.alignment
&& blen
<= args
.maxlen
)
4138 args
.minlen
= blen
- args
.alignment
;
4139 args
.minalignslop
= 0;
4142 * First try an exact bno allocation.
4143 * If it fails then do a near or start bno
4144 * allocation with alignment turned on.
4148 args
.type
= XFS_ALLOCTYPE_THIS_BNO
;
4151 * Compute the minlen+alignment for the
4152 * next case. Set slop so that the value
4153 * of minlen+alignment+slop doesn't go up
4154 * between the calls.
4156 if (blen
> mp
->m_dalign
&& blen
<= args
.maxlen
)
4157 nextminlen
= blen
- mp
->m_dalign
;
4159 nextminlen
= args
.minlen
;
4160 if (nextminlen
+ mp
->m_dalign
> args
.minlen
+ 1)
4162 nextminlen
+ mp
->m_dalign
-
4165 args
.minalignslop
= 0;
4169 args
.minalignslop
= 0;
4171 args
.minleft
= ap
->minleft
;
4172 args
.wasdel
= ap
->wasdel
;
4174 args
.userdata
= ap
->userdata
;
4175 if ((error
= xfs_alloc_vextent(&args
)))
4177 if (tryagain
&& args
.fsbno
== NULLFSBLOCK
) {
4179 * Exact allocation failed. Now try with alignment
4183 args
.fsbno
= ap
->blkno
;
4184 args
.alignment
= mp
->m_dalign
;
4185 args
.minlen
= nextminlen
;
4186 args
.minalignslop
= 0;
4188 if ((error
= xfs_alloc_vextent(&args
)))
4191 if (isaligned
&& args
.fsbno
== NULLFSBLOCK
) {
4193 * allocation failed, so turn off alignment and
4197 args
.fsbno
= ap
->blkno
;
4199 if ((error
= xfs_alloc_vextent(&args
)))
4202 if (args
.fsbno
== NULLFSBLOCK
&& nullfb
&&
4203 args
.minlen
> ap
->minlen
) {
4204 args
.minlen
= ap
->minlen
;
4205 args
.type
= XFS_ALLOCTYPE_START_BNO
;
4206 args
.fsbno
= ap
->blkno
;
4207 if ((error
= xfs_alloc_vextent(&args
)))
4210 if (args
.fsbno
== NULLFSBLOCK
&& nullfb
) {
4212 args
.type
= XFS_ALLOCTYPE_FIRST_AG
;
4213 args
.total
= ap
->minlen
;
4215 if ((error
= xfs_alloc_vextent(&args
)))
4217 ap
->flist
->xbf_low
= 1;
4219 if (args
.fsbno
!= NULLFSBLOCK
) {
4221 * check the allocation happened at the same or higher AG than
4222 * the first block that was allocated.
4224 ASSERT(*ap
->firstblock
== NULLFSBLOCK
||
4225 XFS_FSB_TO_AGNO(mp
, *ap
->firstblock
) ==
4226 XFS_FSB_TO_AGNO(mp
, args
.fsbno
) ||
4227 (ap
->flist
->xbf_low
&&
4228 XFS_FSB_TO_AGNO(mp
, *ap
->firstblock
) <
4229 XFS_FSB_TO_AGNO(mp
, args
.fsbno
)));
4231 ap
->blkno
= args
.fsbno
;
4232 if (*ap
->firstblock
== NULLFSBLOCK
)
4233 *ap
->firstblock
= args
.fsbno
;
4234 ASSERT(nullfb
|| fb_agno
== args
.agno
||
4235 (ap
->flist
->xbf_low
&& fb_agno
< args
.agno
));
4236 ap
->length
= args
.len
;
4237 ap
->ip
->i_d
.di_nblocks
+= args
.len
;
4238 xfs_trans_log_inode(ap
->tp
, ap
->ip
, XFS_ILOG_CORE
);
4240 ap
->ip
->i_delayed_blks
-= args
.len
;
4242 * Adjust the disk quota also. This was reserved
4245 xfs_trans_mod_dquot_byino(ap
->tp
, ap
->ip
,
4246 ap
->wasdel
? XFS_TRANS_DQ_DELBCOUNT
:
4247 XFS_TRANS_DQ_BCOUNT
,
4250 ap
->blkno
= NULLFSBLOCK
;
4257 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
4258 * It figures out where to ask the underlying allocator to put the new extent.
4262 xfs_bmalloca_t
*ap
) /* bmap alloc argument struct */
4264 if (XFS_IS_REALTIME_INODE(ap
->ip
) && ap
->userdata
)
4265 return xfs_bmap_rtalloc(ap
);
4266 return xfs_bmap_btalloc(ap
);
4270 * Trim the returned map to the required bounds
4274 struct xfs_bmbt_irec
*mval
,
4275 struct xfs_bmbt_irec
*got
,
4283 if ((flags
& XFS_BMAPI_ENTIRE
) ||
4284 got
->br_startoff
+ got
->br_blockcount
<= obno
) {
4286 if (isnullstartblock(got
->br_startblock
))
4287 mval
->br_startblock
= DELAYSTARTBLOCK
;
4293 ASSERT((*bno
>= obno
) || (n
== 0));
4295 mval
->br_startoff
= *bno
;
4296 if (isnullstartblock(got
->br_startblock
))
4297 mval
->br_startblock
= DELAYSTARTBLOCK
;
4299 mval
->br_startblock
= got
->br_startblock
+
4300 (*bno
- got
->br_startoff
);
4302 * Return the minimum of what we got and what we asked for for
4303 * the length. We can use the len variable here because it is
4304 * modified below and we could have been there before coming
4305 * here if the first part of the allocation didn't overlap what
4308 mval
->br_blockcount
= XFS_FILBLKS_MIN(end
- *bno
,
4309 got
->br_blockcount
- (*bno
- got
->br_startoff
));
4310 mval
->br_state
= got
->br_state
;
4311 ASSERT(mval
->br_blockcount
<= len
);
4316 * Update and validate the extent map to return
4319 xfs_bmapi_update_map(
4320 struct xfs_bmbt_irec
**map
,
4328 xfs_bmbt_irec_t
*mval
= *map
;
4330 ASSERT((flags
& XFS_BMAPI_ENTIRE
) ||
4331 ((mval
->br_startoff
+ mval
->br_blockcount
) <= end
));
4332 ASSERT((flags
& XFS_BMAPI_ENTIRE
) || (mval
->br_blockcount
<= *len
) ||
4333 (mval
->br_startoff
< obno
));
4335 *bno
= mval
->br_startoff
+ mval
->br_blockcount
;
4337 if (*n
> 0 && mval
->br_startoff
== mval
[-1].br_startoff
) {
4338 /* update previous map with new information */
4339 ASSERT(mval
->br_startblock
== mval
[-1].br_startblock
);
4340 ASSERT(mval
->br_blockcount
> mval
[-1].br_blockcount
);
4341 ASSERT(mval
->br_state
== mval
[-1].br_state
);
4342 mval
[-1].br_blockcount
= mval
->br_blockcount
;
4343 mval
[-1].br_state
= mval
->br_state
;
4344 } else if (*n
> 0 && mval
->br_startblock
!= DELAYSTARTBLOCK
&&
4345 mval
[-1].br_startblock
!= DELAYSTARTBLOCK
&&
4346 mval
[-1].br_startblock
!= HOLESTARTBLOCK
&&
4347 mval
->br_startblock
== mval
[-1].br_startblock
+
4348 mval
[-1].br_blockcount
&&
4349 ((flags
& XFS_BMAPI_IGSTATE
) ||
4350 mval
[-1].br_state
== mval
->br_state
)) {
4351 ASSERT(mval
->br_startoff
==
4352 mval
[-1].br_startoff
+ mval
[-1].br_blockcount
);
4353 mval
[-1].br_blockcount
+= mval
->br_blockcount
;
4354 } else if (*n
> 0 &&
4355 mval
->br_startblock
== DELAYSTARTBLOCK
&&
4356 mval
[-1].br_startblock
== DELAYSTARTBLOCK
&&
4357 mval
->br_startoff
==
4358 mval
[-1].br_startoff
+ mval
[-1].br_blockcount
) {
4359 mval
[-1].br_blockcount
+= mval
->br_blockcount
;
4360 mval
[-1].br_state
= mval
->br_state
;
4361 } else if (!((*n
== 0) &&
4362 ((mval
->br_startoff
+ mval
->br_blockcount
) <=
4371 * Map file blocks to filesystem blocks without allocation.
4375 struct xfs_inode
*ip
,
4378 struct xfs_bmbt_irec
*mval
,
4382 struct xfs_mount
*mp
= ip
->i_mount
;
4383 struct xfs_ifork
*ifp
;
4384 struct xfs_bmbt_irec got
;
4385 struct xfs_bmbt_irec prev
;
4392 int whichfork
= (flags
& XFS_BMAPI_ATTRFORK
) ?
4393 XFS_ATTR_FORK
: XFS_DATA_FORK
;
4396 ASSERT(!(flags
& ~(XFS_BMAPI_ATTRFORK
|XFS_BMAPI_ENTIRE
|
4397 XFS_BMAPI_IGSTATE
)));
4399 if (unlikely(XFS_TEST_ERROR(
4400 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
4401 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
4402 mp
, XFS_ERRTAG_BMAPIFORMAT
, XFS_RANDOM_BMAPIFORMAT
))) {
4403 XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW
, mp
);
4404 return XFS_ERROR(EFSCORRUPTED
);
4407 if (XFS_FORCED_SHUTDOWN(mp
))
4408 return XFS_ERROR(EIO
);
4410 XFS_STATS_INC(xs_blk_mapr
);
4412 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4414 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
4415 error
= xfs_iread_extents(NULL
, ip
, whichfork
);
4420 xfs_bmap_search_extents(ip
, bno
, whichfork
, &eof
, &lastx
, &got
, &prev
);
4424 while (bno
< end
&& n
< *nmap
) {
4425 /* Reading past eof, act as though there's a hole up to end. */
4427 got
.br_startoff
= end
;
4428 if (got
.br_startoff
> bno
) {
4429 /* Reading in a hole. */
4430 mval
->br_startoff
= bno
;
4431 mval
->br_startblock
= HOLESTARTBLOCK
;
4432 mval
->br_blockcount
=
4433 XFS_FILBLKS_MIN(len
, got
.br_startoff
- bno
);
4434 mval
->br_state
= XFS_EXT_NORM
;
4435 bno
+= mval
->br_blockcount
;
4436 len
-= mval
->br_blockcount
;
4442 /* set up the extent map to return. */
4443 xfs_bmapi_trim_map(mval
, &got
, &bno
, len
, obno
, end
, n
, flags
);
4444 xfs_bmapi_update_map(&mval
, &bno
, &len
, obno
, end
, &n
, flags
);
4446 /* If we're done, stop now. */
4447 if (bno
>= end
|| n
>= *nmap
)
4450 /* Else go on to the next record. */
4451 if (++lastx
< ifp
->if_bytes
/ sizeof(xfs_bmbt_rec_t
))
4452 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, lastx
), &got
);
4461 xfs_bmapi_reserve_delalloc(
4462 struct xfs_inode
*ip
,
4465 struct xfs_bmbt_irec
*got
,
4466 struct xfs_bmbt_irec
*prev
,
4467 xfs_extnum_t
*lastx
,
4470 struct xfs_mount
*mp
= ip
->i_mount
;
4471 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, XFS_DATA_FORK
);
4473 xfs_extlen_t indlen
;
4474 char rt
= XFS_IS_REALTIME_INODE(ip
);
4478 alen
= XFS_FILBLKS_MIN(len
, MAXEXTLEN
);
4480 alen
= XFS_FILBLKS_MIN(alen
, got
->br_startoff
- aoff
);
4482 /* Figure out the extent size, adjust alen */
4483 extsz
= xfs_get_extsz_hint(ip
);
4486 * Make sure we don't exceed a single extent length when we
4487 * align the extent by reducing length we are going to
4488 * allocate by the maximum amount extent size aligment may
4491 alen
= XFS_FILBLKS_MIN(len
, MAXEXTLEN
- (2 * extsz
- 1));
4492 error
= xfs_bmap_extsize_align(mp
, got
, prev
, extsz
, rt
, eof
,
4493 1, 0, &aoff
, &alen
);
4498 extsz
= alen
/ mp
->m_sb
.sb_rextsize
;
4501 * Make a transaction-less quota reservation for delayed allocation
4502 * blocks. This number gets adjusted later. We return if we haven't
4503 * allocated blocks already inside this loop.
4505 error
= xfs_trans_reserve_quota_nblks(NULL
, ip
, (long)alen
, 0,
4506 rt
? XFS_QMOPT_RES_RTBLKS
: XFS_QMOPT_RES_REGBLKS
);
4511 * Split changing sb for alen and indlen since they could be coming
4512 * from different places.
4514 indlen
= (xfs_extlen_t
)xfs_bmap_worst_indlen(ip
, alen
);
4518 error
= xfs_mod_incore_sb(mp
, XFS_SBS_FREXTENTS
,
4519 -((int64_t)extsz
), 0);
4521 error
= xfs_icsb_modify_counters(mp
, XFS_SBS_FDBLOCKS
,
4522 -((int64_t)alen
), 0);
4526 goto out_unreserve_quota
;
4528 error
= xfs_icsb_modify_counters(mp
, XFS_SBS_FDBLOCKS
,
4529 -((int64_t)indlen
), 0);
4531 goto out_unreserve_blocks
;
4534 ip
->i_delayed_blks
+= alen
;
4536 got
->br_startoff
= aoff
;
4537 got
->br_startblock
= nullstartblock(indlen
);
4538 got
->br_blockcount
= alen
;
4539 got
->br_state
= XFS_EXT_NORM
;
4540 xfs_bmap_add_extent_hole_delay(ip
, lastx
, got
);
4543 * Update our extent pointer, given that xfs_bmap_add_extent_hole_delay
4544 * might have merged it into one of the neighbouring ones.
4546 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, *lastx
), got
);
4548 ASSERT(got
->br_startoff
<= aoff
);
4549 ASSERT(got
->br_startoff
+ got
->br_blockcount
>= aoff
+ alen
);
4550 ASSERT(isnullstartblock(got
->br_startblock
));
4551 ASSERT(got
->br_state
== XFS_EXT_NORM
);
4554 out_unreserve_blocks
:
4556 xfs_mod_incore_sb(mp
, XFS_SBS_FREXTENTS
, extsz
, 0);
4558 xfs_icsb_modify_counters(mp
, XFS_SBS_FDBLOCKS
, alen
, 0);
4559 out_unreserve_quota
:
4560 if (XFS_IS_QUOTA_ON(mp
))
4561 xfs_trans_unreserve_quota_nblks(NULL
, ip
, (long)alen
, 0, rt
?
4562 XFS_QMOPT_RES_RTBLKS
: XFS_QMOPT_RES_REGBLKS
);
4567 * Map file blocks to filesystem blocks, adding delayed allocations as needed.
4571 struct xfs_inode
*ip
, /* incore inode */
4572 xfs_fileoff_t bno
, /* starting file offs. mapped */
4573 xfs_filblks_t len
, /* length to map in file */
4574 struct xfs_bmbt_irec
*mval
, /* output: map values */
4575 int *nmap
, /* i/o: mval size/count */
4576 int flags
) /* XFS_BMAPI_... */
4578 struct xfs_mount
*mp
= ip
->i_mount
;
4579 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, XFS_DATA_FORK
);
4580 struct xfs_bmbt_irec got
; /* current file extent record */
4581 struct xfs_bmbt_irec prev
; /* previous file extent record */
4582 xfs_fileoff_t obno
; /* old block number (offset) */
4583 xfs_fileoff_t end
; /* end of mapped file region */
4584 xfs_extnum_t lastx
; /* last useful extent number */
4585 int eof
; /* we've hit the end of extents */
4586 int n
= 0; /* current extent index */
4590 ASSERT(*nmap
<= XFS_BMAP_MAX_NMAP
);
4591 ASSERT(!(flags
& ~XFS_BMAPI_ENTIRE
));
4593 if (unlikely(XFS_TEST_ERROR(
4594 (XFS_IFORK_FORMAT(ip
, XFS_DATA_FORK
) != XFS_DINODE_FMT_EXTENTS
&&
4595 XFS_IFORK_FORMAT(ip
, XFS_DATA_FORK
) != XFS_DINODE_FMT_BTREE
),
4596 mp
, XFS_ERRTAG_BMAPIFORMAT
, XFS_RANDOM_BMAPIFORMAT
))) {
4597 XFS_ERROR_REPORT("xfs_bmapi_delay", XFS_ERRLEVEL_LOW
, mp
);
4598 return XFS_ERROR(EFSCORRUPTED
);
4601 if (XFS_FORCED_SHUTDOWN(mp
))
4602 return XFS_ERROR(EIO
);
4604 XFS_STATS_INC(xs_blk_mapw
);
4606 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
4607 error
= xfs_iread_extents(NULL
, ip
, XFS_DATA_FORK
);
4612 xfs_bmap_search_extents(ip
, bno
, XFS_DATA_FORK
, &eof
, &lastx
, &got
, &prev
);
4616 while (bno
< end
&& n
< *nmap
) {
4617 if (eof
|| got
.br_startoff
> bno
) {
4618 error
= xfs_bmapi_reserve_delalloc(ip
, bno
, len
, &got
,
4619 &prev
, &lastx
, eof
);
4629 /* set up the extent map to return. */
4630 xfs_bmapi_trim_map(mval
, &got
, &bno
, len
, obno
, end
, n
, flags
);
4631 xfs_bmapi_update_map(&mval
, &bno
, &len
, obno
, end
, &n
, flags
);
4633 /* If we're done, stop now. */
4634 if (bno
>= end
|| n
>= *nmap
)
4637 /* Else go on to the next record. */
4639 if (++lastx
< ifp
->if_bytes
/ sizeof(xfs_bmbt_rec_t
))
4640 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, lastx
), &got
);
4651 __xfs_bmapi_allocate(
4652 struct xfs_bmalloca
*bma
)
4654 struct xfs_mount
*mp
= bma
->ip
->i_mount
;
4655 int whichfork
= (bma
->flags
& XFS_BMAPI_ATTRFORK
) ?
4656 XFS_ATTR_FORK
: XFS_DATA_FORK
;
4657 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
4658 int tmp_logflags
= 0;
4662 ASSERT(bma
->length
> 0);
4664 rt
= (whichfork
== XFS_DATA_FORK
) && XFS_IS_REALTIME_INODE(bma
->ip
);
4667 * For the wasdelay case, we could also just allocate the stuff asked
4668 * for in this bmap call but that wouldn't be as good.
4671 bma
->length
= (xfs_extlen_t
)bma
->got
.br_blockcount
;
4672 bma
->offset
= bma
->got
.br_startoff
;
4673 if (bma
->idx
!= NULLEXTNUM
&& bma
->idx
) {
4674 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
- 1),
4678 bma
->length
= XFS_FILBLKS_MIN(bma
->length
, MAXEXTLEN
);
4680 bma
->length
= XFS_FILBLKS_MIN(bma
->length
,
4681 bma
->got
.br_startoff
- bma
->offset
);
4685 * Indicate if this is the first user data in the file, or just any
4688 if (!(bma
->flags
& XFS_BMAPI_METADATA
)) {
4689 bma
->userdata
= (bma
->offset
== 0) ?
4690 XFS_ALLOC_INITIAL_USER_DATA
: XFS_ALLOC_USERDATA
;
4693 bma
->minlen
= (bma
->flags
& XFS_BMAPI_CONTIG
) ? bma
->length
: 1;
4696 * Only want to do the alignment at the eof if it is userdata and
4697 * allocation length is larger than a stripe unit.
4699 if (mp
->m_dalign
&& bma
->length
>= mp
->m_dalign
&&
4700 !(bma
->flags
& XFS_BMAPI_METADATA
) && whichfork
== XFS_DATA_FORK
) {
4701 error
= xfs_bmap_isaeof(bma
, whichfork
);
4706 error
= xfs_bmap_alloc(bma
);
4710 if (bma
->flist
->xbf_low
)
4713 bma
->cur
->bc_private
.b
.firstblock
= *bma
->firstblock
;
4714 if (bma
->blkno
== NULLFSBLOCK
)
4716 if ((ifp
->if_flags
& XFS_IFBROOT
) && !bma
->cur
) {
4717 bma
->cur
= xfs_bmbt_init_cursor(mp
, bma
->tp
, bma
->ip
, whichfork
);
4718 bma
->cur
->bc_private
.b
.firstblock
= *bma
->firstblock
;
4719 bma
->cur
->bc_private
.b
.flist
= bma
->flist
;
4722 * Bump the number of extents we've allocated
4728 bma
->cur
->bc_private
.b
.flags
=
4729 bma
->wasdel
? XFS_BTCUR_BPRV_WASDEL
: 0;
4731 bma
->got
.br_startoff
= bma
->offset
;
4732 bma
->got
.br_startblock
= bma
->blkno
;
4733 bma
->got
.br_blockcount
= bma
->length
;
4734 bma
->got
.br_state
= XFS_EXT_NORM
;
4737 * A wasdelay extent has been initialized, so shouldn't be flagged
4740 if (!bma
->wasdel
&& (bma
->flags
& XFS_BMAPI_PREALLOC
) &&
4741 xfs_sb_version_hasextflgbit(&mp
->m_sb
))
4742 bma
->got
.br_state
= XFS_EXT_UNWRITTEN
;
4745 error
= xfs_bmap_add_extent_delay_real(bma
);
4747 error
= xfs_bmap_add_extent_hole_real(bma
, whichfork
);
4749 bma
->logflags
|= tmp_logflags
;
4754 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4755 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4756 * the neighbouring ones.
4758 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
), &bma
->got
);
4760 ASSERT(bma
->got
.br_startoff
<= bma
->offset
);
4761 ASSERT(bma
->got
.br_startoff
+ bma
->got
.br_blockcount
>=
4762 bma
->offset
+ bma
->length
);
4763 ASSERT(bma
->got
.br_state
== XFS_EXT_NORM
||
4764 bma
->got
.br_state
== XFS_EXT_UNWRITTEN
);
4769 xfs_bmapi_allocate_worker(
4770 struct work_struct
*work
)
4772 struct xfs_bmalloca
*args
= container_of(work
,
4773 struct xfs_bmalloca
, work
);
4774 unsigned long pflags
;
4776 /* we are in a transaction context here */
4777 current_set_flags_nested(&pflags
, PF_FSTRANS
);
4779 args
->result
= __xfs_bmapi_allocate(args
);
4780 complete(args
->done
);
4782 current_restore_flags_nested(&pflags
, PF_FSTRANS
);
4786 * Some allocation requests often come in with little stack to work on. Push
4787 * them off to a worker thread so there is lots of stack to use. Otherwise just
4788 * call directly to avoid the context switch overhead here.
4792 struct xfs_bmalloca
*args
)
4794 DECLARE_COMPLETION_ONSTACK(done
);
4796 if (!args
->stack_switch
)
4797 return __xfs_bmapi_allocate(args
);
4801 INIT_WORK_ONSTACK(&args
->work
, xfs_bmapi_allocate_worker
);
4802 queue_work(xfs_alloc_wq
, &args
->work
);
4803 wait_for_completion(&done
);
4804 return args
->result
;
4808 xfs_bmapi_convert_unwritten(
4809 struct xfs_bmalloca
*bma
,
4810 struct xfs_bmbt_irec
*mval
,
4814 int whichfork
= (flags
& XFS_BMAPI_ATTRFORK
) ?
4815 XFS_ATTR_FORK
: XFS_DATA_FORK
;
4816 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
4817 int tmp_logflags
= 0;
4820 /* check if we need to do unwritten->real conversion */
4821 if (mval
->br_state
== XFS_EXT_UNWRITTEN
&&
4822 (flags
& XFS_BMAPI_PREALLOC
))
4825 /* check if we need to do real->unwritten conversion */
4826 if (mval
->br_state
== XFS_EXT_NORM
&&
4827 (flags
& (XFS_BMAPI_PREALLOC
| XFS_BMAPI_CONVERT
)) !=
4828 (XFS_BMAPI_PREALLOC
| XFS_BMAPI_CONVERT
))
4832 * Modify (by adding) the state flag, if writing.
4834 ASSERT(mval
->br_blockcount
<= len
);
4835 if ((ifp
->if_flags
& XFS_IFBROOT
) && !bma
->cur
) {
4836 bma
->cur
= xfs_bmbt_init_cursor(bma
->ip
->i_mount
, bma
->tp
,
4837 bma
->ip
, whichfork
);
4838 bma
->cur
->bc_private
.b
.firstblock
= *bma
->firstblock
;
4839 bma
->cur
->bc_private
.b
.flist
= bma
->flist
;
4841 mval
->br_state
= (mval
->br_state
== XFS_EXT_UNWRITTEN
)
4842 ? XFS_EXT_NORM
: XFS_EXT_UNWRITTEN
;
4844 error
= xfs_bmap_add_extent_unwritten_real(bma
->tp
, bma
->ip
, &bma
->idx
,
4845 &bma
->cur
, mval
, bma
->firstblock
, bma
->flist
,
4847 bma
->logflags
|= tmp_logflags
;
4852 * Update our extent pointer, given that
4853 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4854 * of the neighbouring ones.
4856 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
), &bma
->got
);
4859 * We may have combined previously unwritten space with written space,
4860 * so generate another request.
4862 if (mval
->br_blockcount
< len
)
4868 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4869 * extent state if necessary. Details behaviour is controlled by the flags
4870 * parameter. Only allocates blocks from a single allocation group, to avoid
4873 * The returned value in "firstblock" from the first call in a transaction
4874 * must be remembered and presented to subsequent calls in "firstblock".
4875 * An upper bound for the number of blocks to be allocated is supplied to
4876 * the first call in "total"; if no allocation group has that many free
4877 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4881 struct xfs_trans
*tp
, /* transaction pointer */
4882 struct xfs_inode
*ip
, /* incore inode */
4883 xfs_fileoff_t bno
, /* starting file offs. mapped */
4884 xfs_filblks_t len
, /* length to map in file */
4885 int flags
, /* XFS_BMAPI_... */
4886 xfs_fsblock_t
*firstblock
, /* first allocated block
4887 controls a.g. for allocs */
4888 xfs_extlen_t total
, /* total blocks needed */
4889 struct xfs_bmbt_irec
*mval
, /* output: map values */
4890 int *nmap
, /* i/o: mval size/count */
4891 struct xfs_bmap_free
*flist
) /* i/o: list extents to free */
4893 struct xfs_mount
*mp
= ip
->i_mount
;
4894 struct xfs_ifork
*ifp
;
4895 struct xfs_bmalloca bma
= { 0 }; /* args for xfs_bmap_alloc */
4896 xfs_fileoff_t end
; /* end of mapped file region */
4897 int eof
; /* after the end of extents */
4898 int error
; /* error return */
4899 int n
; /* current extent index */
4900 xfs_fileoff_t obno
; /* old block number (offset) */
4901 int whichfork
; /* data or attr fork */
4902 char inhole
; /* current location is hole in file */
4903 char wasdelay
; /* old extent was delayed */
4906 xfs_fileoff_t orig_bno
; /* original block number value */
4907 int orig_flags
; /* original flags arg value */
4908 xfs_filblks_t orig_len
; /* original value of len arg */
4909 struct xfs_bmbt_irec
*orig_mval
; /* original value of mval */
4910 int orig_nmap
; /* original value of *nmap */
4920 ASSERT(*nmap
<= XFS_BMAP_MAX_NMAP
);
4921 ASSERT(!(flags
& XFS_BMAPI_IGSTATE
));
4925 whichfork
= (flags
& XFS_BMAPI_ATTRFORK
) ?
4926 XFS_ATTR_FORK
: XFS_DATA_FORK
;
4928 if (unlikely(XFS_TEST_ERROR(
4929 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
4930 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
&&
4931 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_LOCAL
),
4932 mp
, XFS_ERRTAG_BMAPIFORMAT
, XFS_RANDOM_BMAPIFORMAT
))) {
4933 XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW
, mp
);
4934 return XFS_ERROR(EFSCORRUPTED
);
4937 if (XFS_FORCED_SHUTDOWN(mp
))
4938 return XFS_ERROR(EIO
);
4940 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4942 XFS_STATS_INC(xs_blk_mapw
);
4944 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
) {
4946 * XXX (dgc): This assumes we are only called for inodes that
4947 * contain content neutral data in local format. Anything that
4948 * contains caller-specific data in local format that needs
4949 * transformation to move to a block format needs to do the
4950 * conversion to extent format itself.
4952 * Directory data forks and attribute forks handle this
4953 * themselves, but with the addition of metadata verifiers every
4954 * data fork in local format now contains caller specific data
4955 * and as such conversion through this function is likely to be
4958 * The only likely user of this branch is for remote symlinks,
4959 * but we cannot overwrite the data fork contents of the symlink
4960 * (EEXIST occurs higher up the stack) and so it will never go
4961 * from local format to extent format here. Hence I don't think
4962 * this branch is ever executed intentionally and we should
4963 * consider removing it and asserting that xfs_bmapi_write()
4964 * cannot be called directly on local format forks. i.e. callers
4965 * are completely responsible for local to extent format
4966 * conversion, not xfs_bmapi_write().
4968 error
= xfs_bmap_local_to_extents(tp
, ip
, firstblock
, total
,
4969 &bma
.logflags
, whichfork
,
4970 xfs_bmap_local_to_extents_init_fn
);
4975 if (*firstblock
== NULLFSBLOCK
) {
4976 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
)
4977 bma
.minleft
= be16_to_cpu(ifp
->if_broot
->bb_level
) + 1;
4984 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
4985 error
= xfs_iread_extents(tp
, ip
, whichfork
);
4990 xfs_bmap_search_extents(ip
, bno
, whichfork
, &eof
, &bma
.idx
, &bma
.got
,
5001 bma
.firstblock
= firstblock
;
5003 if (flags
& XFS_BMAPI_STACK_SWITCH
)
5004 bma
.stack_switch
= 1;
5006 while (bno
< end
&& n
< *nmap
) {
5007 inhole
= eof
|| bma
.got
.br_startoff
> bno
;
5008 wasdelay
= !inhole
&& isnullstartblock(bma
.got
.br_startblock
);
5011 * First, deal with the hole before the allocated space
5012 * that we found, if any.
5014 if (inhole
|| wasdelay
) {
5016 bma
.conv
= !!(flags
& XFS_BMAPI_CONVERT
);
5017 bma
.wasdel
= wasdelay
;
5022 * There's a 32/64 bit type mismatch between the
5023 * allocation length request (which can be 64 bits in
5024 * length) and the bma length request, which is
5025 * xfs_extlen_t and therefore 32 bits. Hence we have to
5026 * check for 32-bit overflows and handle them here.
5028 if (len
> (xfs_filblks_t
)MAXEXTLEN
)
5029 bma
.length
= MAXEXTLEN
;
5034 ASSERT(bma
.length
> 0);
5035 error
= xfs_bmapi_allocate(&bma
);
5038 if (bma
.blkno
== NULLFSBLOCK
)
5042 /* Deal with the allocated space we found. */
5043 xfs_bmapi_trim_map(mval
, &bma
.got
, &bno
, len
, obno
,
5046 /* Execute unwritten extent conversion if necessary */
5047 error
= xfs_bmapi_convert_unwritten(&bma
, mval
, len
, flags
);
5048 if (error
== EAGAIN
)
5053 /* update the extent map to return */
5054 xfs_bmapi_update_map(&mval
, &bno
, &len
, obno
, end
, &n
, flags
);
5057 * If we're done, stop now. Stop when we've allocated
5058 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
5059 * the transaction may get too big.
5061 if (bno
>= end
|| n
>= *nmap
|| bma
.nallocs
>= *nmap
)
5064 /* Else go on to the next record. */
5066 if (++bma
.idx
< ifp
->if_bytes
/ sizeof(xfs_bmbt_rec_t
)) {
5067 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
.idx
),
5075 * Transform from btree to extents, give it cur.
5077 if (xfs_bmap_wants_extents(ip
, whichfork
)) {
5078 int tmp_logflags
= 0;
5081 error
= xfs_bmap_btree_to_extents(tp
, ip
, bma
.cur
,
5082 &tmp_logflags
, whichfork
);
5083 bma
.logflags
|= tmp_logflags
;
5088 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
||
5089 XFS_IFORK_NEXTENTS(ip
, whichfork
) >
5090 XFS_IFORK_MAXEXT(ip
, whichfork
));
5094 * Log everything. Do this after conversion, there's no point in
5095 * logging the extent records if we've converted to btree format.
5097 if ((bma
.logflags
& xfs_ilog_fext(whichfork
)) &&
5098 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
5099 bma
.logflags
&= ~xfs_ilog_fext(whichfork
);
5100 else if ((bma
.logflags
& xfs_ilog_fbroot(whichfork
)) &&
5101 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)
5102 bma
.logflags
&= ~xfs_ilog_fbroot(whichfork
);
5104 * Log whatever the flags say, even if error. Otherwise we might miss
5105 * detecting a case where the data is changed, there's an error,
5106 * and it's not logged so we don't shutdown when we should.
5109 xfs_trans_log_inode(tp
, ip
, bma
.logflags
);
5113 ASSERT(*firstblock
== NULLFSBLOCK
||
5114 XFS_FSB_TO_AGNO(mp
, *firstblock
) ==
5116 bma
.cur
->bc_private
.b
.firstblock
) ||
5118 XFS_FSB_TO_AGNO(mp
, *firstblock
) <
5120 bma
.cur
->bc_private
.b
.firstblock
)));
5121 *firstblock
= bma
.cur
->bc_private
.b
.firstblock
;
5123 xfs_btree_del_cursor(bma
.cur
,
5124 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
5127 xfs_bmap_validate_ret(orig_bno
, orig_len
, orig_flags
, orig_mval
,
5133 * Called by xfs_bmapi to update file extent records and the btree
5134 * after removing space (or undoing a delayed allocation).
5136 STATIC
int /* error */
5137 xfs_bmap_del_extent(
5138 xfs_inode_t
*ip
, /* incore inode pointer */
5139 xfs_trans_t
*tp
, /* current transaction pointer */
5140 xfs_extnum_t
*idx
, /* extent number to update/delete */
5141 xfs_bmap_free_t
*flist
, /* list of extents to be freed */
5142 xfs_btree_cur_t
*cur
, /* if null, not a btree */
5143 xfs_bmbt_irec_t
*del
, /* data to remove from extents */
5144 int *logflagsp
, /* inode logging flags */
5145 int whichfork
) /* data or attr fork */
5147 xfs_filblks_t da_new
; /* new delay-alloc indirect blocks */
5148 xfs_filblks_t da_old
; /* old delay-alloc indirect blocks */
5149 xfs_fsblock_t del_endblock
=0; /* first block past del */
5150 xfs_fileoff_t del_endoff
; /* first offset past del */
5151 int delay
; /* current block is delayed allocated */
5152 int do_fx
; /* free extent at end of routine */
5153 xfs_bmbt_rec_host_t
*ep
; /* current extent entry pointer */
5154 int error
; /* error return value */
5155 int flags
; /* inode logging flags */
5156 xfs_bmbt_irec_t got
; /* current extent entry */
5157 xfs_fileoff_t got_endoff
; /* first offset past got */
5158 int i
; /* temp state */
5159 xfs_ifork_t
*ifp
; /* inode fork pointer */
5160 xfs_mount_t
*mp
; /* mount structure */
5161 xfs_filblks_t nblks
; /* quota/sb block count */
5162 xfs_bmbt_irec_t
new; /* new record to be inserted */
5164 uint qfield
; /* quota field to update */
5165 xfs_filblks_t temp
; /* for indirect length calculations */
5166 xfs_filblks_t temp2
; /* for indirect length calculations */
5169 XFS_STATS_INC(xs_del_exlist
);
5171 if (whichfork
== XFS_ATTR_FORK
)
5172 state
|= BMAP_ATTRFORK
;
5175 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5176 ASSERT((*idx
>= 0) && (*idx
< ifp
->if_bytes
/
5177 (uint
)sizeof(xfs_bmbt_rec_t
)));
5178 ASSERT(del
->br_blockcount
> 0);
5179 ep
= xfs_iext_get_ext(ifp
, *idx
);
5180 xfs_bmbt_get_all(ep
, &got
);
5181 ASSERT(got
.br_startoff
<= del
->br_startoff
);
5182 del_endoff
= del
->br_startoff
+ del
->br_blockcount
;
5183 got_endoff
= got
.br_startoff
+ got
.br_blockcount
;
5184 ASSERT(got_endoff
>= del_endoff
);
5185 delay
= isnullstartblock(got
.br_startblock
);
5186 ASSERT(isnullstartblock(del
->br_startblock
) == delay
);
5191 * If deleting a real allocation, must free up the disk space.
5194 flags
= XFS_ILOG_CORE
;
5196 * Realtime allocation. Free it and record di_nblocks update.
5198 if (whichfork
== XFS_DATA_FORK
&& XFS_IS_REALTIME_INODE(ip
)) {
5202 ASSERT(do_mod(del
->br_blockcount
,
5203 mp
->m_sb
.sb_rextsize
) == 0);
5204 ASSERT(do_mod(del
->br_startblock
,
5205 mp
->m_sb
.sb_rextsize
) == 0);
5206 bno
= del
->br_startblock
;
5207 len
= del
->br_blockcount
;
5208 do_div(bno
, mp
->m_sb
.sb_rextsize
);
5209 do_div(len
, mp
->m_sb
.sb_rextsize
);
5210 error
= xfs_rtfree_extent(tp
, bno
, (xfs_extlen_t
)len
);
5214 nblks
= len
* mp
->m_sb
.sb_rextsize
;
5215 qfield
= XFS_TRANS_DQ_RTBCOUNT
;
5218 * Ordinary allocation.
5222 nblks
= del
->br_blockcount
;
5223 qfield
= XFS_TRANS_DQ_BCOUNT
;
5226 * Set up del_endblock and cur for later.
5228 del_endblock
= del
->br_startblock
+ del
->br_blockcount
;
5230 if ((error
= xfs_bmbt_lookup_eq(cur
, got
.br_startoff
,
5231 got
.br_startblock
, got
.br_blockcount
,
5234 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
5236 da_old
= da_new
= 0;
5238 da_old
= startblockval(got
.br_startblock
);
5244 * Set flag value to use in switch statement.
5245 * Left-contig is 2, right-contig is 1.
5247 switch (((got
.br_startoff
== del
->br_startoff
) << 1) |
5248 (got_endoff
== del_endoff
)) {
5251 * Matches the whole extent. Delete the entry.
5253 xfs_iext_remove(ip
, *idx
, 1,
5254 whichfork
== XFS_ATTR_FORK
? BMAP_ATTRFORK
: 0);
5259 XFS_IFORK_NEXT_SET(ip
, whichfork
,
5260 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
5261 flags
|= XFS_ILOG_CORE
;
5263 flags
|= xfs_ilog_fext(whichfork
);
5266 if ((error
= xfs_btree_delete(cur
, &i
)))
5268 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
5273 * Deleting the first part of the extent.
5275 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
5276 xfs_bmbt_set_startoff(ep
, del_endoff
);
5277 temp
= got
.br_blockcount
- del
->br_blockcount
;
5278 xfs_bmbt_set_blockcount(ep
, temp
);
5280 temp
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
5282 xfs_bmbt_set_startblock(ep
, nullstartblock((int)temp
));
5283 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
5287 xfs_bmbt_set_startblock(ep
, del_endblock
);
5288 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
5290 flags
|= xfs_ilog_fext(whichfork
);
5293 if ((error
= xfs_bmbt_update(cur
, del_endoff
, del_endblock
,
5294 got
.br_blockcount
- del
->br_blockcount
,
5301 * Deleting the last part of the extent.
5303 temp
= got
.br_blockcount
- del
->br_blockcount
;
5304 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
5305 xfs_bmbt_set_blockcount(ep
, temp
);
5307 temp
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
5309 xfs_bmbt_set_startblock(ep
, nullstartblock((int)temp
));
5310 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
5314 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
5316 flags
|= xfs_ilog_fext(whichfork
);
5319 if ((error
= xfs_bmbt_update(cur
, got
.br_startoff
,
5321 got
.br_blockcount
- del
->br_blockcount
,
5328 * Deleting the middle of the extent.
5330 temp
= del
->br_startoff
- got
.br_startoff
;
5331 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
5332 xfs_bmbt_set_blockcount(ep
, temp
);
5333 new.br_startoff
= del_endoff
;
5334 temp2
= got_endoff
- del_endoff
;
5335 new.br_blockcount
= temp2
;
5336 new.br_state
= got
.br_state
;
5338 new.br_startblock
= del_endblock
;
5339 flags
|= XFS_ILOG_CORE
;
5341 if ((error
= xfs_bmbt_update(cur
,
5343 got
.br_startblock
, temp
,
5346 if ((error
= xfs_btree_increment(cur
, 0, &i
)))
5348 cur
->bc_rec
.b
= new;
5349 error
= xfs_btree_insert(cur
, &i
);
5350 if (error
&& error
!= ENOSPC
)
5353 * If get no-space back from btree insert,
5354 * it tried a split, and we have a zero
5355 * block reservation.
5356 * Fix up our state and return the error.
5358 if (error
== ENOSPC
) {
5360 * Reset the cursor, don't trust
5361 * it after any insert operation.
5363 if ((error
= xfs_bmbt_lookup_eq(cur
,
5368 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
5370 * Update the btree record back
5371 * to the original value.
5373 if ((error
= xfs_bmbt_update(cur
,
5380 * Reset the extent record back
5381 * to the original value.
5383 xfs_bmbt_set_blockcount(ep
,
5386 error
= XFS_ERROR(ENOSPC
);
5389 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
5391 flags
|= xfs_ilog_fext(whichfork
);
5392 XFS_IFORK_NEXT_SET(ip
, whichfork
,
5393 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
5395 ASSERT(whichfork
== XFS_DATA_FORK
);
5396 temp
= xfs_bmap_worst_indlen(ip
, temp
);
5397 xfs_bmbt_set_startblock(ep
, nullstartblock((int)temp
));
5398 temp2
= xfs_bmap_worst_indlen(ip
, temp2
);
5399 new.br_startblock
= nullstartblock((int)temp2
);
5400 da_new
= temp
+ temp2
;
5401 while (da_new
> da_old
) {
5405 xfs_bmbt_set_startblock(ep
,
5406 nullstartblock((int)temp
));
5408 if (da_new
== da_old
)
5414 nullstartblock((int)temp2
);
5418 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
5419 xfs_iext_insert(ip
, *idx
+ 1, 1, &new, state
);
5424 * If we need to, add to list of extents to delete.
5427 xfs_bmap_add_free(del
->br_startblock
, del
->br_blockcount
, flist
,
5430 * Adjust inode # blocks in the file.
5433 ip
->i_d
.di_nblocks
-= nblks
;
5435 * Adjust quota data.
5438 xfs_trans_mod_dquot_byino(tp
, ip
, qfield
, (long)-nblks
);
5441 * Account for change in delayed indirect blocks.
5442 * Nothing to do for disk quota accounting here.
5444 ASSERT(da_old
>= da_new
);
5445 if (da_old
> da_new
) {
5446 xfs_icsb_modify_counters(mp
, XFS_SBS_FDBLOCKS
,
5447 (int64_t)(da_old
- da_new
), 0);
5455 * Unmap (remove) blocks from a file.
5456 * If nexts is nonzero then the number of extents to remove is limited to
5457 * that value. If not all extents in the block range can be removed then
5462 xfs_trans_t
*tp
, /* transaction pointer */
5463 struct xfs_inode
*ip
, /* incore inode */
5464 xfs_fileoff_t bno
, /* starting offset to unmap */
5465 xfs_filblks_t len
, /* length to unmap in file */
5466 int flags
, /* misc flags */
5467 xfs_extnum_t nexts
, /* number of extents max */
5468 xfs_fsblock_t
*firstblock
, /* first allocated block
5469 controls a.g. for allocs */
5470 xfs_bmap_free_t
*flist
, /* i/o: list extents to free */
5471 int *done
) /* set if not done yet */
5473 xfs_btree_cur_t
*cur
; /* bmap btree cursor */
5474 xfs_bmbt_irec_t del
; /* extent being deleted */
5475 int eof
; /* is deleting at eof */
5476 xfs_bmbt_rec_host_t
*ep
; /* extent record pointer */
5477 int error
; /* error return value */
5478 xfs_extnum_t extno
; /* extent number in list */
5479 xfs_bmbt_irec_t got
; /* current extent record */
5480 xfs_ifork_t
*ifp
; /* inode fork pointer */
5481 int isrt
; /* freeing in rt area */
5482 xfs_extnum_t lastx
; /* last extent index used */
5483 int logflags
; /* transaction logging flags */
5484 xfs_extlen_t mod
; /* rt extent offset */
5485 xfs_mount_t
*mp
; /* mount structure */
5486 xfs_extnum_t nextents
; /* number of file extents */
5487 xfs_bmbt_irec_t prev
; /* previous extent record */
5488 xfs_fileoff_t start
; /* first file offset deleted */
5489 int tmp_logflags
; /* partial logging flags */
5490 int wasdel
; /* was a delayed alloc extent */
5491 int whichfork
; /* data or attribute fork */
5494 trace_xfs_bunmap(ip
, bno
, len
, flags
, _RET_IP_
);
5496 whichfork
= (flags
& XFS_BMAPI_ATTRFORK
) ?
5497 XFS_ATTR_FORK
: XFS_DATA_FORK
;
5498 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5500 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
5501 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)) {
5502 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW
,
5504 return XFS_ERROR(EFSCORRUPTED
);
5507 if (XFS_FORCED_SHUTDOWN(mp
))
5508 return XFS_ERROR(EIO
);
5513 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
5514 (error
= xfs_iread_extents(tp
, ip
, whichfork
)))
5516 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
5517 if (nextents
== 0) {
5521 XFS_STATS_INC(xs_blk_unmap
);
5522 isrt
= (whichfork
== XFS_DATA_FORK
) && XFS_IS_REALTIME_INODE(ip
);
5524 bno
= start
+ len
- 1;
5525 ep
= xfs_bmap_search_extents(ip
, bno
, whichfork
, &eof
, &lastx
, &got
,
5529 * Check to see if the given block number is past the end of the
5530 * file, back up to the last block if so...
5533 ep
= xfs_iext_get_ext(ifp
, --lastx
);
5534 xfs_bmbt_get_all(ep
, &got
);
5535 bno
= got
.br_startoff
+ got
.br_blockcount
- 1;
5538 if (ifp
->if_flags
& XFS_IFBROOT
) {
5539 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
);
5540 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
5541 cur
->bc_private
.b
.firstblock
= *firstblock
;
5542 cur
->bc_private
.b
.flist
= flist
;
5543 cur
->bc_private
.b
.flags
= 0;
5549 * Synchronize by locking the bitmap inode.
5551 xfs_ilock(mp
->m_rbmip
, XFS_ILOCK_EXCL
);
5552 xfs_trans_ijoin(tp
, mp
->m_rbmip
, XFS_ILOCK_EXCL
);
5556 while (bno
!= (xfs_fileoff_t
)-1 && bno
>= start
&& lastx
>= 0 &&
5557 (nexts
== 0 || extno
< nexts
)) {
5559 * Is the found extent after a hole in which bno lives?
5560 * Just back up to the previous extent, if so.
5562 if (got
.br_startoff
> bno
) {
5565 ep
= xfs_iext_get_ext(ifp
, lastx
);
5566 xfs_bmbt_get_all(ep
, &got
);
5569 * Is the last block of this extent before the range
5570 * we're supposed to delete? If so, we're done.
5572 bno
= XFS_FILEOFF_MIN(bno
,
5573 got
.br_startoff
+ got
.br_blockcount
- 1);
5577 * Then deal with the (possibly delayed) allocated space
5582 wasdel
= isnullstartblock(del
.br_startblock
);
5583 if (got
.br_startoff
< start
) {
5584 del
.br_startoff
= start
;
5585 del
.br_blockcount
-= start
- got
.br_startoff
;
5587 del
.br_startblock
+= start
- got
.br_startoff
;
5589 if (del
.br_startoff
+ del
.br_blockcount
> bno
+ 1)
5590 del
.br_blockcount
= bno
+ 1 - del
.br_startoff
;
5591 sum
= del
.br_startblock
+ del
.br_blockcount
;
5593 (mod
= do_mod(sum
, mp
->m_sb
.sb_rextsize
))) {
5595 * Realtime extent not lined up at the end.
5596 * The extent could have been split into written
5597 * and unwritten pieces, or we could just be
5598 * unmapping part of it. But we can't really
5599 * get rid of part of a realtime extent.
5601 if (del
.br_state
== XFS_EXT_UNWRITTEN
||
5602 !xfs_sb_version_hasextflgbit(&mp
->m_sb
)) {
5604 * This piece is unwritten, or we're not
5605 * using unwritten extents. Skip over it.
5608 bno
-= mod
> del
.br_blockcount
?
5609 del
.br_blockcount
: mod
;
5610 if (bno
< got
.br_startoff
) {
5612 xfs_bmbt_get_all(xfs_iext_get_ext(
5618 * It's written, turn it unwritten.
5619 * This is better than zeroing it.
5621 ASSERT(del
.br_state
== XFS_EXT_NORM
);
5622 ASSERT(xfs_trans_get_block_res(tp
) > 0);
5624 * If this spans a realtime extent boundary,
5625 * chop it back to the start of the one we end at.
5627 if (del
.br_blockcount
> mod
) {
5628 del
.br_startoff
+= del
.br_blockcount
- mod
;
5629 del
.br_startblock
+= del
.br_blockcount
- mod
;
5630 del
.br_blockcount
= mod
;
5632 del
.br_state
= XFS_EXT_UNWRITTEN
;
5633 error
= xfs_bmap_add_extent_unwritten_real(tp
, ip
,
5634 &lastx
, &cur
, &del
, firstblock
, flist
,
5640 if (isrt
&& (mod
= do_mod(del
.br_startblock
, mp
->m_sb
.sb_rextsize
))) {
5642 * Realtime extent is lined up at the end but not
5643 * at the front. We'll get rid of full extents if
5646 mod
= mp
->m_sb
.sb_rextsize
- mod
;
5647 if (del
.br_blockcount
> mod
) {
5648 del
.br_blockcount
-= mod
;
5649 del
.br_startoff
+= mod
;
5650 del
.br_startblock
+= mod
;
5651 } else if ((del
.br_startoff
== start
&&
5652 (del
.br_state
== XFS_EXT_UNWRITTEN
||
5653 xfs_trans_get_block_res(tp
) == 0)) ||
5654 !xfs_sb_version_hasextflgbit(&mp
->m_sb
)) {
5656 * Can't make it unwritten. There isn't
5657 * a full extent here so just skip it.
5659 ASSERT(bno
>= del
.br_blockcount
);
5660 bno
-= del
.br_blockcount
;
5661 if (got
.br_startoff
> bno
) {
5663 ep
= xfs_iext_get_ext(ifp
,
5665 xfs_bmbt_get_all(ep
, &got
);
5669 } else if (del
.br_state
== XFS_EXT_UNWRITTEN
) {
5671 * This one is already unwritten.
5672 * It must have a written left neighbor.
5673 * Unwrite the killed part of that one and
5677 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
,
5679 ASSERT(prev
.br_state
== XFS_EXT_NORM
);
5680 ASSERT(!isnullstartblock(prev
.br_startblock
));
5681 ASSERT(del
.br_startblock
==
5682 prev
.br_startblock
+ prev
.br_blockcount
);
5683 if (prev
.br_startoff
< start
) {
5684 mod
= start
- prev
.br_startoff
;
5685 prev
.br_blockcount
-= mod
;
5686 prev
.br_startblock
+= mod
;
5687 prev
.br_startoff
= start
;
5689 prev
.br_state
= XFS_EXT_UNWRITTEN
;
5691 error
= xfs_bmap_add_extent_unwritten_real(tp
,
5692 ip
, &lastx
, &cur
, &prev
,
5693 firstblock
, flist
, &logflags
);
5698 ASSERT(del
.br_state
== XFS_EXT_NORM
);
5699 del
.br_state
= XFS_EXT_UNWRITTEN
;
5700 error
= xfs_bmap_add_extent_unwritten_real(tp
,
5701 ip
, &lastx
, &cur
, &del
,
5702 firstblock
, flist
, &logflags
);
5709 ASSERT(startblockval(del
.br_startblock
) > 0);
5710 /* Update realtime/data freespace, unreserve quota */
5712 xfs_filblks_t rtexts
;
5714 rtexts
= XFS_FSB_TO_B(mp
, del
.br_blockcount
);
5715 do_div(rtexts
, mp
->m_sb
.sb_rextsize
);
5716 xfs_mod_incore_sb(mp
, XFS_SBS_FREXTENTS
,
5717 (int64_t)rtexts
, 0);
5718 (void)xfs_trans_reserve_quota_nblks(NULL
,
5719 ip
, -((long)del
.br_blockcount
), 0,
5720 XFS_QMOPT_RES_RTBLKS
);
5722 xfs_icsb_modify_counters(mp
, XFS_SBS_FDBLOCKS
,
5723 (int64_t)del
.br_blockcount
, 0);
5724 (void)xfs_trans_reserve_quota_nblks(NULL
,
5725 ip
, -((long)del
.br_blockcount
), 0,
5726 XFS_QMOPT_RES_REGBLKS
);
5728 ip
->i_delayed_blks
-= del
.br_blockcount
;
5730 cur
->bc_private
.b
.flags
|=
5731 XFS_BTCUR_BPRV_WASDEL
;
5733 cur
->bc_private
.b
.flags
&= ~XFS_BTCUR_BPRV_WASDEL
;
5735 * If it's the case where the directory code is running
5736 * with no block reservation, and the deleted block is in
5737 * the middle of its extent, and the resulting insert
5738 * of an extent would cause transformation to btree format,
5739 * then reject it. The calling code will then swap
5740 * blocks around instead.
5741 * We have to do this now, rather than waiting for the
5742 * conversion to btree format, since the transaction
5745 if (!wasdel
&& xfs_trans_get_block_res(tp
) == 0 &&
5746 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
&&
5747 XFS_IFORK_NEXTENTS(ip
, whichfork
) >= /* Note the >= */
5748 XFS_IFORK_MAXEXT(ip
, whichfork
) &&
5749 del
.br_startoff
> got
.br_startoff
&&
5750 del
.br_startoff
+ del
.br_blockcount
<
5751 got
.br_startoff
+ got
.br_blockcount
) {
5752 error
= XFS_ERROR(ENOSPC
);
5755 error
= xfs_bmap_del_extent(ip
, tp
, &lastx
, flist
, cur
, &del
,
5756 &tmp_logflags
, whichfork
);
5757 logflags
|= tmp_logflags
;
5760 bno
= del
.br_startoff
- 1;
5763 * If not done go on to the next (previous) record.
5765 if (bno
!= (xfs_fileoff_t
)-1 && bno
>= start
) {
5767 ep
= xfs_iext_get_ext(ifp
, lastx
);
5768 if (xfs_bmbt_get_startoff(ep
) > bno
) {
5770 ep
= xfs_iext_get_ext(ifp
,
5773 xfs_bmbt_get_all(ep
, &got
);
5778 *done
= bno
== (xfs_fileoff_t
)-1 || bno
< start
|| lastx
< 0;
5781 * Convert to a btree if necessary.
5783 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
5784 ASSERT(cur
== NULL
);
5785 error
= xfs_bmap_extents_to_btree(tp
, ip
, firstblock
, flist
,
5786 &cur
, 0, &tmp_logflags
, whichfork
);
5787 logflags
|= tmp_logflags
;
5792 * transform from btree to extents, give it cur
5794 else if (xfs_bmap_wants_extents(ip
, whichfork
)) {
5795 ASSERT(cur
!= NULL
);
5796 error
= xfs_bmap_btree_to_extents(tp
, ip
, cur
, &tmp_logflags
,
5798 logflags
|= tmp_logflags
;
5803 * transform from extents to local?
5808 * Log everything. Do this after conversion, there's no point in
5809 * logging the extent records if we've converted to btree format.
5811 if ((logflags
& xfs_ilog_fext(whichfork
)) &&
5812 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
5813 logflags
&= ~xfs_ilog_fext(whichfork
);
5814 else if ((logflags
& xfs_ilog_fbroot(whichfork
)) &&
5815 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)
5816 logflags
&= ~xfs_ilog_fbroot(whichfork
);
5818 * Log inode even in the error case, if the transaction
5819 * is dirty we'll need to shut down the filesystem.
5822 xfs_trans_log_inode(tp
, ip
, logflags
);
5825 *firstblock
= cur
->bc_private
.b
.firstblock
;
5826 cur
->bc_private
.b
.allocated
= 0;
5828 xfs_btree_del_cursor(cur
,
5829 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
5835 * returns 1 for success, 0 if we failed to map the extent.
5838 xfs_getbmapx_fix_eof_hole(
5839 xfs_inode_t
*ip
, /* xfs incore inode pointer */
5840 struct getbmapx
*out
, /* output structure */
5841 int prealloced
, /* this is a file with
5842 * preallocated data space */
5843 __int64_t end
, /* last block requested */
5844 xfs_fsblock_t startblock
)
5847 xfs_mount_t
*mp
; /* file system mount point */
5848 xfs_ifork_t
*ifp
; /* inode fork pointer */
5849 xfs_extnum_t lastx
; /* last extent pointer */
5850 xfs_fileoff_t fileblock
;
5852 if (startblock
== HOLESTARTBLOCK
) {
5854 out
->bmv_block
= -1;
5855 fixlen
= XFS_FSB_TO_BB(mp
, XFS_B_TO_FSB(mp
, XFS_ISIZE(ip
)));
5856 fixlen
-= out
->bmv_offset
;
5857 if (prealloced
&& out
->bmv_offset
+ out
->bmv_length
== end
) {
5858 /* Came to hole at EOF. Trim it. */
5861 out
->bmv_length
= fixlen
;
5864 if (startblock
== DELAYSTARTBLOCK
)
5865 out
->bmv_block
= -2;
5867 out
->bmv_block
= xfs_fsb_to_db(ip
, startblock
);
5868 fileblock
= XFS_BB_TO_FSB(ip
->i_mount
, out
->bmv_offset
);
5869 ifp
= XFS_IFORK_PTR(ip
, XFS_DATA_FORK
);
5870 if (xfs_iext_bno_to_ext(ifp
, fileblock
, &lastx
) &&
5871 (lastx
== (ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
))-1))
5872 out
->bmv_oflags
|= BMV_OF_LAST
;
5879 * Get inode's extents as described in bmv, and format for output.
5880 * Calls formatter to fill the user's buffer until all extents
5881 * are mapped, until the passed-in bmv->bmv_count slots have
5882 * been filled, or until the formatter short-circuits the loop,
5883 * if it is tracking filled-in extents on its own.
5885 int /* error code */
5888 struct getbmapx
*bmv
, /* user bmap structure */
5889 xfs_bmap_format_t formatter
, /* format to user */
5890 void *arg
) /* formatter arg */
5892 __int64_t bmvend
; /* last block requested */
5893 int error
= 0; /* return value */
5894 __int64_t fixlen
; /* length for -1 case */
5895 int i
; /* extent number */
5896 int lock
; /* lock state */
5897 xfs_bmbt_irec_t
*map
; /* buffer for user's data */
5898 xfs_mount_t
*mp
; /* file system mount point */
5899 int nex
; /* # of user extents can do */
5900 int nexleft
; /* # of user extents left */
5901 int subnex
; /* # of bmapi's can do */
5902 int nmap
; /* number of map entries */
5903 struct getbmapx
*out
; /* output structure */
5904 int whichfork
; /* data or attr fork */
5905 int prealloced
; /* this is a file with
5906 * preallocated data space */
5907 int iflags
; /* interface flags */
5908 int bmapi_flags
; /* flags for xfs_bmapi */
5912 iflags
= bmv
->bmv_iflags
;
5913 whichfork
= iflags
& BMV_IF_ATTRFORK
? XFS_ATTR_FORK
: XFS_DATA_FORK
;
5915 if (whichfork
== XFS_ATTR_FORK
) {
5916 if (XFS_IFORK_Q(ip
)) {
5917 if (ip
->i_d
.di_aformat
!= XFS_DINODE_FMT_EXTENTS
&&
5918 ip
->i_d
.di_aformat
!= XFS_DINODE_FMT_BTREE
&&
5919 ip
->i_d
.di_aformat
!= XFS_DINODE_FMT_LOCAL
)
5920 return XFS_ERROR(EINVAL
);
5921 } else if (unlikely(
5922 ip
->i_d
.di_aformat
!= 0 &&
5923 ip
->i_d
.di_aformat
!= XFS_DINODE_FMT_EXTENTS
)) {
5924 XFS_ERROR_REPORT("xfs_getbmap", XFS_ERRLEVEL_LOW
,
5926 return XFS_ERROR(EFSCORRUPTED
);
5932 if (ip
->i_d
.di_format
!= XFS_DINODE_FMT_EXTENTS
&&
5933 ip
->i_d
.di_format
!= XFS_DINODE_FMT_BTREE
&&
5934 ip
->i_d
.di_format
!= XFS_DINODE_FMT_LOCAL
)
5935 return XFS_ERROR(EINVAL
);
5937 if (xfs_get_extsz_hint(ip
) ||
5938 ip
->i_d
.di_flags
& (XFS_DIFLAG_PREALLOC
|XFS_DIFLAG_APPEND
)){
5940 fixlen
= mp
->m_super
->s_maxbytes
;
5943 fixlen
= XFS_ISIZE(ip
);
5947 if (bmv
->bmv_length
== -1) {
5948 fixlen
= XFS_FSB_TO_BB(mp
, XFS_B_TO_FSB(mp
, fixlen
));
5950 max_t(__int64_t
, fixlen
- bmv
->bmv_offset
, 0);
5951 } else if (bmv
->bmv_length
== 0) {
5952 bmv
->bmv_entries
= 0;
5954 } else if (bmv
->bmv_length
< 0) {
5955 return XFS_ERROR(EINVAL
);
5958 nex
= bmv
->bmv_count
- 1;
5960 return XFS_ERROR(EINVAL
);
5961 bmvend
= bmv
->bmv_offset
+ bmv
->bmv_length
;
5964 if (bmv
->bmv_count
> ULONG_MAX
/ sizeof(struct getbmapx
))
5965 return XFS_ERROR(ENOMEM
);
5966 out
= kmem_zalloc(bmv
->bmv_count
* sizeof(struct getbmapx
), KM_MAYFAIL
);
5968 out
= kmem_zalloc_large(bmv
->bmv_count
*
5969 sizeof(struct getbmapx
));
5971 return XFS_ERROR(ENOMEM
);
5974 xfs_ilock(ip
, XFS_IOLOCK_SHARED
);
5975 if (whichfork
== XFS_DATA_FORK
&& !(iflags
& BMV_IF_DELALLOC
)) {
5976 if (ip
->i_delayed_blks
|| XFS_ISIZE(ip
) > ip
->i_d
.di_size
) {
5977 error
= -filemap_write_and_wait(VFS_I(ip
)->i_mapping
);
5979 goto out_unlock_iolock
;
5982 * even after flushing the inode, there can still be delalloc
5983 * blocks on the inode beyond EOF due to speculative
5984 * preallocation. These are not removed until the release
5985 * function is called or the inode is inactivated. Hence we
5986 * cannot assert here that ip->i_delayed_blks == 0.
5990 lock
= xfs_ilock_map_shared(ip
);
5993 * Don't let nex be bigger than the number of extents
5994 * we can have assuming alternating holes and real extents.
5996 if (nex
> XFS_IFORK_NEXTENTS(ip
, whichfork
) * 2 + 1)
5997 nex
= XFS_IFORK_NEXTENTS(ip
, whichfork
) * 2 + 1;
5999 bmapi_flags
= xfs_bmapi_aflag(whichfork
);
6000 if (!(iflags
& BMV_IF_PREALLOC
))
6001 bmapi_flags
|= XFS_BMAPI_IGSTATE
;
6004 * Allocate enough space to handle "subnex" maps at a time.
6008 map
= kmem_alloc(subnex
* sizeof(*map
), KM_MAYFAIL
| KM_NOFS
);
6010 goto out_unlock_ilock
;
6012 bmv
->bmv_entries
= 0;
6014 if (XFS_IFORK_NEXTENTS(ip
, whichfork
) == 0 &&
6015 (whichfork
== XFS_ATTR_FORK
|| !(iflags
& BMV_IF_DELALLOC
))) {
6023 nmap
= (nexleft
> subnex
) ? subnex
: nexleft
;
6024 error
= xfs_bmapi_read(ip
, XFS_BB_TO_FSBT(mp
, bmv
->bmv_offset
),
6025 XFS_BB_TO_FSB(mp
, bmv
->bmv_length
),
6026 map
, &nmap
, bmapi_flags
);
6029 ASSERT(nmap
<= subnex
);
6031 for (i
= 0; i
< nmap
&& nexleft
&& bmv
->bmv_length
; i
++) {
6032 out
[cur_ext
].bmv_oflags
= 0;
6033 if (map
[i
].br_state
== XFS_EXT_UNWRITTEN
)
6034 out
[cur_ext
].bmv_oflags
|= BMV_OF_PREALLOC
;
6035 else if (map
[i
].br_startblock
== DELAYSTARTBLOCK
)
6036 out
[cur_ext
].bmv_oflags
|= BMV_OF_DELALLOC
;
6037 out
[cur_ext
].bmv_offset
=
6038 XFS_FSB_TO_BB(mp
, map
[i
].br_startoff
);
6039 out
[cur_ext
].bmv_length
=
6040 XFS_FSB_TO_BB(mp
, map
[i
].br_blockcount
);
6041 out
[cur_ext
].bmv_unused1
= 0;
6042 out
[cur_ext
].bmv_unused2
= 0;
6045 * delayed allocation extents that start beyond EOF can
6046 * occur due to speculative EOF allocation when the
6047 * delalloc extent is larger than the largest freespace
6048 * extent at conversion time. These extents cannot be
6049 * converted by data writeback, so can exist here even
6050 * if we are not supposed to be finding delalloc
6053 if (map
[i
].br_startblock
== DELAYSTARTBLOCK
&&
6054 map
[i
].br_startoff
<= XFS_B_TO_FSB(mp
, XFS_ISIZE(ip
)))
6055 ASSERT((iflags
& BMV_IF_DELALLOC
) != 0);
6057 if (map
[i
].br_startblock
== HOLESTARTBLOCK
&&
6058 whichfork
== XFS_ATTR_FORK
) {
6059 /* came to the end of attribute fork */
6060 out
[cur_ext
].bmv_oflags
|= BMV_OF_LAST
;
6064 if (!xfs_getbmapx_fix_eof_hole(ip
, &out
[cur_ext
],
6066 map
[i
].br_startblock
))
6070 out
[cur_ext
].bmv_offset
+
6071 out
[cur_ext
].bmv_length
;
6073 max_t(__int64_t
, 0, bmvend
- bmv
->bmv_offset
);
6076 * In case we don't want to return the hole,
6077 * don't increase cur_ext so that we can reuse
6078 * it in the next loop.
6080 if ((iflags
& BMV_IF_NO_HOLES
) &&
6081 map
[i
].br_startblock
== HOLESTARTBLOCK
) {
6082 memset(&out
[cur_ext
], 0, sizeof(out
[cur_ext
]));
6090 } while (nmap
&& nexleft
&& bmv
->bmv_length
);
6095 xfs_iunlock_map_shared(ip
, lock
);
6097 xfs_iunlock(ip
, XFS_IOLOCK_SHARED
);
6099 for (i
= 0; i
< cur_ext
; i
++) {
6100 int full
= 0; /* user array is full */
6102 /* format results & advance arg */
6103 error
= formatter(&arg
, &out
[i
], &full
);
6108 if (is_vmalloc_addr(out
))
6109 kmem_free_large(out
);
6116 * dead simple method of punching delalyed allocation blocks from a range in
6117 * the inode. Walks a block at a time so will be slow, but is only executed in
6118 * rare error cases so the overhead is not critical. This will alays punch out
6119 * both the start and end blocks, even if the ranges only partially overlap
6120 * them, so it is up to the caller to ensure that partial blocks are not
6124 xfs_bmap_punch_delalloc_range(
6125 struct xfs_inode
*ip
,
6126 xfs_fileoff_t start_fsb
,
6127 xfs_fileoff_t length
)
6129 xfs_fileoff_t remaining
= length
;
6132 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
6136 xfs_bmbt_irec_t imap
;
6138 xfs_fsblock_t firstblock
;
6139 xfs_bmap_free_t flist
;
6142 * Map the range first and check that it is a delalloc extent
6143 * before trying to unmap the range. Otherwise we will be
6144 * trying to remove a real extent (which requires a
6145 * transaction) or a hole, which is probably a bad idea...
6147 error
= xfs_bmapi_read(ip
, start_fsb
, 1, &imap
, &nimaps
,
6151 /* something screwed, just bail */
6152 if (!XFS_FORCED_SHUTDOWN(ip
->i_mount
)) {
6153 xfs_alert(ip
->i_mount
,
6154 "Failed delalloc mapping lookup ino %lld fsb %lld.",
6155 ip
->i_ino
, start_fsb
);
6163 if (imap
.br_startblock
!= DELAYSTARTBLOCK
) {
6164 /* been converted, ignore */
6167 WARN_ON(imap
.br_blockcount
== 0);
6170 * Note: while we initialise the firstblock/flist pair, they
6171 * should never be used because blocks should never be
6172 * allocated or freed for a delalloc extent and hence we need
6173 * don't cancel or finish them after the xfs_bunmapi() call.
6175 xfs_bmap_init(&flist
, &firstblock
);
6176 error
= xfs_bunmapi(NULL
, ip
, start_fsb
, 1, 0, 1, &firstblock
,
6181 ASSERT(!flist
.xbf_count
&& !flist
.xbf_first
);
6185 } while(remaining
> 0);