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_shared.h"
21 #include "xfs_format.h"
22 #include "xfs_log_format.h"
23 #include "xfs_trans_resv.h"
26 #include "xfs_mount.h"
27 #include "xfs_defer.h"
28 #include "xfs_da_format.h"
29 #include "xfs_da_btree.h"
31 #include "xfs_inode.h"
32 #include "xfs_btree.h"
33 #include "xfs_trans.h"
34 #include "xfs_inode_item.h"
35 #include "xfs_extfree_item.h"
36 #include "xfs_alloc.h"
38 #include "xfs_bmap_util.h"
39 #include "xfs_bmap_btree.h"
40 #include "xfs_rtalloc.h"
41 #include "xfs_error.h"
42 #include "xfs_quota.h"
43 #include "xfs_trans_space.h"
44 #include "xfs_buf_item.h"
45 #include "xfs_trace.h"
46 #include "xfs_symlink.h"
47 #include "xfs_attr_leaf.h"
48 #include "xfs_filestream.h"
50 #include "xfs_ag_resv.h"
51 #include "xfs_refcount.h"
52 #include "xfs_rmap_btree.h"
53 #include "xfs_icache.h"
56 kmem_zone_t
*xfs_bmap_free_item_zone
;
59 * Miscellaneous helper functions
63 * Compute and fill in the value of the maximum depth of a bmap btree
64 * in this filesystem. Done once, during mount.
67 xfs_bmap_compute_maxlevels(
68 xfs_mount_t
*mp
, /* file system mount structure */
69 int whichfork
) /* data or attr fork */
71 int level
; /* btree level */
72 uint maxblocks
; /* max blocks at this level */
73 uint maxleafents
; /* max leaf entries possible */
74 int maxrootrecs
; /* max records in root block */
75 int minleafrecs
; /* min records in leaf block */
76 int minnoderecs
; /* min records in node block */
77 int sz
; /* root block size */
80 * The maximum number of extents in a file, hence the maximum
81 * number of leaf entries, is controlled by the type of di_nextents
82 * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
83 * (a signed 16-bit number, xfs_aextnum_t).
85 * Note that we can no longer assume that if we are in ATTR1 that
86 * the fork offset of all the inodes will be
87 * (xfs_default_attroffset(ip) >> 3) because we could have mounted
88 * with ATTR2 and then mounted back with ATTR1, keeping the
89 * di_forkoff's fixed but probably at various positions. Therefore,
90 * for both ATTR1 and ATTR2 we have to assume the worst case scenario
91 * of a minimum size available.
93 if (whichfork
== XFS_DATA_FORK
) {
94 maxleafents
= MAXEXTNUM
;
95 sz
= XFS_BMDR_SPACE_CALC(MINDBTPTRS
);
97 maxleafents
= MAXAEXTNUM
;
98 sz
= XFS_BMDR_SPACE_CALC(MINABTPTRS
);
100 maxrootrecs
= xfs_bmdr_maxrecs(sz
, 0);
101 minleafrecs
= mp
->m_bmap_dmnr
[0];
102 minnoderecs
= mp
->m_bmap_dmnr
[1];
103 maxblocks
= (maxleafents
+ minleafrecs
- 1) / minleafrecs
;
104 for (level
= 1; maxblocks
> 1; level
++) {
105 if (maxblocks
<= maxrootrecs
)
108 maxblocks
= (maxblocks
+ minnoderecs
- 1) / minnoderecs
;
110 mp
->m_bm_maxlevels
[whichfork
] = level
;
113 STATIC
int /* error */
115 struct xfs_btree_cur
*cur
,
119 int *stat
) /* success/failure */
121 cur
->bc_rec
.b
.br_startoff
= off
;
122 cur
->bc_rec
.b
.br_startblock
= bno
;
123 cur
->bc_rec
.b
.br_blockcount
= len
;
124 return xfs_btree_lookup(cur
, XFS_LOOKUP_EQ
, stat
);
127 STATIC
int /* error */
129 struct xfs_btree_cur
*cur
,
133 int *stat
) /* success/failure */
135 cur
->bc_rec
.b
.br_startoff
= off
;
136 cur
->bc_rec
.b
.br_startblock
= bno
;
137 cur
->bc_rec
.b
.br_blockcount
= len
;
138 return xfs_btree_lookup(cur
, XFS_LOOKUP_GE
, stat
);
142 * Check if the inode needs to be converted to btree format.
144 static inline bool xfs_bmap_needs_btree(struct xfs_inode
*ip
, int whichfork
)
146 return whichfork
!= XFS_COW_FORK
&&
147 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
&&
148 XFS_IFORK_NEXTENTS(ip
, whichfork
) >
149 XFS_IFORK_MAXEXT(ip
, whichfork
);
153 * Check if the inode should be converted to extent format.
155 static inline bool xfs_bmap_wants_extents(struct xfs_inode
*ip
, int whichfork
)
157 return whichfork
!= XFS_COW_FORK
&&
158 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
&&
159 XFS_IFORK_NEXTENTS(ip
, whichfork
) <=
160 XFS_IFORK_MAXEXT(ip
, whichfork
);
164 * Update the record referred to by cur to the value given
165 * by [off, bno, len, state].
166 * This either works (return 0) or gets an EFSCORRUPTED error.
170 struct xfs_btree_cur
*cur
,
176 union xfs_btree_rec rec
;
178 xfs_bmbt_disk_set_allf(&rec
.bmbt
, off
, bno
, len
, state
);
179 return xfs_btree_update(cur
, &rec
);
183 * Compute the worst-case number of indirect blocks that will be used
184 * for ip's delayed extent of length "len".
187 xfs_bmap_worst_indlen(
188 xfs_inode_t
*ip
, /* incore inode pointer */
189 xfs_filblks_t len
) /* delayed extent length */
191 int level
; /* btree level number */
192 int maxrecs
; /* maximum record count at this level */
193 xfs_mount_t
*mp
; /* mount structure */
194 xfs_filblks_t rval
; /* return value */
195 xfs_filblks_t orig_len
;
199 /* Calculate the worst-case size of the bmbt. */
201 maxrecs
= mp
->m_bmap_dmxr
[0];
202 for (level
= 0, rval
= 0;
203 level
< XFS_BM_MAXLEVELS(mp
, XFS_DATA_FORK
);
206 do_div(len
, maxrecs
);
209 rval
+= XFS_BM_MAXLEVELS(mp
, XFS_DATA_FORK
) -
214 maxrecs
= mp
->m_bmap_dmxr
[1];
217 /* Calculate the worst-case size of the rmapbt. */
218 if (xfs_sb_version_hasrmapbt(&mp
->m_sb
))
219 rval
+= 1 + xfs_rmapbt_calc_size(mp
, orig_len
) +
220 mp
->m_rmap_maxlevels
;
226 * Calculate the default attribute fork offset for newly created inodes.
229 xfs_default_attroffset(
230 struct xfs_inode
*ip
)
232 struct xfs_mount
*mp
= ip
->i_mount
;
235 if (mp
->m_sb
.sb_inodesize
== 256) {
236 offset
= XFS_LITINO(mp
, ip
->i_d
.di_version
) -
237 XFS_BMDR_SPACE_CALC(MINABTPTRS
);
239 offset
= XFS_BMDR_SPACE_CALC(6 * MINABTPTRS
);
242 ASSERT(offset
< XFS_LITINO(mp
, ip
->i_d
.di_version
));
247 * Helper routine to reset inode di_forkoff field when switching
248 * attribute fork from local to extent format - we reset it where
249 * possible to make space available for inline data fork extents.
252 xfs_bmap_forkoff_reset(
256 if (whichfork
== XFS_ATTR_FORK
&&
257 ip
->i_d
.di_format
!= XFS_DINODE_FMT_DEV
&&
258 ip
->i_d
.di_format
!= XFS_DINODE_FMT_UUID
&&
259 ip
->i_d
.di_format
!= XFS_DINODE_FMT_BTREE
) {
260 uint dfl_forkoff
= xfs_default_attroffset(ip
) >> 3;
262 if (dfl_forkoff
> ip
->i_d
.di_forkoff
)
263 ip
->i_d
.di_forkoff
= dfl_forkoff
;
268 STATIC
struct xfs_buf
*
270 struct xfs_btree_cur
*cur
,
273 struct xfs_log_item_desc
*lidp
;
279 for (i
= 0; i
< XFS_BTREE_MAXLEVELS
; i
++) {
280 if (!cur
->bc_bufs
[i
])
282 if (XFS_BUF_ADDR(cur
->bc_bufs
[i
]) == bno
)
283 return cur
->bc_bufs
[i
];
286 /* Chase down all the log items to see if the bp is there */
287 list_for_each_entry(lidp
, &cur
->bc_tp
->t_items
, lid_trans
) {
288 struct xfs_buf_log_item
*bip
;
289 bip
= (struct xfs_buf_log_item
*)lidp
->lid_item
;
290 if (bip
->bli_item
.li_type
== XFS_LI_BUF
&&
291 XFS_BUF_ADDR(bip
->bli_buf
) == bno
)
300 struct xfs_btree_block
*block
,
306 __be64
*pp
, *thispa
; /* pointer to block address */
307 xfs_bmbt_key_t
*prevp
, *keyp
;
309 ASSERT(be16_to_cpu(block
->bb_level
) > 0);
312 for( i
= 1; i
<= xfs_btree_get_numrecs(block
); i
++) {
313 dmxr
= mp
->m_bmap_dmxr
[0];
314 keyp
= XFS_BMBT_KEY_ADDR(mp
, block
, i
);
317 ASSERT(be64_to_cpu(prevp
->br_startoff
) <
318 be64_to_cpu(keyp
->br_startoff
));
323 * Compare the block numbers to see if there are dups.
326 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, i
, sz
);
328 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, i
, dmxr
);
330 for (j
= i
+1; j
<= be16_to_cpu(block
->bb_numrecs
); j
++) {
332 thispa
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, j
, sz
);
334 thispa
= XFS_BMBT_PTR_ADDR(mp
, block
, j
, dmxr
);
335 if (*thispa
== *pp
) {
336 xfs_warn(mp
, "%s: thispa(%d) == pp(%d) %Ld",
338 (unsigned long long)be64_to_cpu(*thispa
));
339 panic("%s: ptrs are equal in node\n",
347 * Check that the extents for the inode ip are in the right order in all
348 * btree leaves. THis becomes prohibitively expensive for large extent count
349 * files, so don't bother with inodes that have more than 10,000 extents in
350 * them. The btree record ordering checks will still be done, so for such large
351 * bmapbt constructs that is going to catch most corruptions.
354 xfs_bmap_check_leaf_extents(
355 xfs_btree_cur_t
*cur
, /* btree cursor or null */
356 xfs_inode_t
*ip
, /* incore inode pointer */
357 int whichfork
) /* data or attr fork */
359 struct xfs_btree_block
*block
; /* current btree block */
360 xfs_fsblock_t bno
; /* block # of "block" */
361 xfs_buf_t
*bp
; /* buffer for "block" */
362 int error
; /* error return value */
363 xfs_extnum_t i
=0, j
; /* index into the extents list */
364 xfs_ifork_t
*ifp
; /* fork structure */
365 int level
; /* btree level, for checking */
366 xfs_mount_t
*mp
; /* file system mount structure */
367 __be64
*pp
; /* pointer to block address */
368 xfs_bmbt_rec_t
*ep
; /* pointer to current extent */
369 xfs_bmbt_rec_t last
= {0, 0}; /* last extent in prev block */
370 xfs_bmbt_rec_t
*nextp
; /* pointer to next extent */
373 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
) {
377 /* skip large extent count inodes */
378 if (ip
->i_d
.di_nextents
> 10000)
383 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
384 block
= ifp
->if_broot
;
386 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
388 level
= be16_to_cpu(block
->bb_level
);
390 xfs_check_block(block
, mp
, 1, ifp
->if_broot_bytes
);
391 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, 1, ifp
->if_broot_bytes
);
392 bno
= be64_to_cpu(*pp
);
394 ASSERT(bno
!= NULLFSBLOCK
);
395 ASSERT(XFS_FSB_TO_AGNO(mp
, bno
) < mp
->m_sb
.sb_agcount
);
396 ASSERT(XFS_FSB_TO_AGBNO(mp
, bno
) < mp
->m_sb
.sb_agblocks
);
399 * Go down the tree until leaf level is reached, following the first
400 * pointer (leftmost) at each level.
402 while (level
-- > 0) {
403 /* See if buf is in cur first */
405 bp
= xfs_bmap_get_bp(cur
, XFS_FSB_TO_DADDR(mp
, bno
));
408 error
= xfs_btree_read_bufl(mp
, NULL
, bno
, 0, &bp
,
414 block
= XFS_BUF_TO_BLOCK(bp
);
419 * Check this block for basic sanity (increasing keys and
420 * no duplicate blocks).
423 xfs_check_block(block
, mp
, 0, 0);
424 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, mp
->m_bmap_dmxr
[1]);
425 bno
= be64_to_cpu(*pp
);
426 XFS_WANT_CORRUPTED_GOTO(mp
,
427 XFS_FSB_SANITY_CHECK(mp
, bno
), error0
);
430 xfs_trans_brelse(NULL
, bp
);
435 * Here with bp and block set to the leftmost leaf node in the tree.
440 * Loop over all leaf nodes checking that all extents are in the right order.
443 xfs_fsblock_t nextbno
;
444 xfs_extnum_t num_recs
;
447 num_recs
= xfs_btree_get_numrecs(block
);
450 * Read-ahead the next leaf block, if any.
453 nextbno
= be64_to_cpu(block
->bb_u
.l
.bb_rightsib
);
456 * Check all the extents to make sure they are OK.
457 * If we had a previous block, the last entry should
458 * conform with the first entry in this one.
461 ep
= XFS_BMBT_REC_ADDR(mp
, block
, 1);
463 ASSERT(xfs_bmbt_disk_get_startoff(&last
) +
464 xfs_bmbt_disk_get_blockcount(&last
) <=
465 xfs_bmbt_disk_get_startoff(ep
));
467 for (j
= 1; j
< num_recs
; j
++) {
468 nextp
= XFS_BMBT_REC_ADDR(mp
, block
, j
+ 1);
469 ASSERT(xfs_bmbt_disk_get_startoff(ep
) +
470 xfs_bmbt_disk_get_blockcount(ep
) <=
471 xfs_bmbt_disk_get_startoff(nextp
));
479 xfs_trans_brelse(NULL
, bp
);
483 * If we've reached the end, stop.
485 if (bno
== NULLFSBLOCK
)
489 bp
= xfs_bmap_get_bp(cur
, XFS_FSB_TO_DADDR(mp
, bno
));
492 error
= xfs_btree_read_bufl(mp
, NULL
, bno
, 0, &bp
,
498 block
= XFS_BUF_TO_BLOCK(bp
);
504 xfs_warn(mp
, "%s: at error0", __func__
);
506 xfs_trans_brelse(NULL
, bp
);
508 xfs_warn(mp
, "%s: BAD after btree leaves for %d extents",
510 panic("%s: CORRUPTED BTREE OR SOMETHING", __func__
);
515 * Add bmap trace insert entries for all the contents of the extent records.
518 xfs_bmap_trace_exlist(
519 xfs_inode_t
*ip
, /* incore inode pointer */
520 xfs_extnum_t cnt
, /* count of entries in the list */
521 int whichfork
, /* data or attr or cow fork */
522 unsigned long caller_ip
)
524 xfs_extnum_t idx
; /* extent record index */
525 xfs_ifork_t
*ifp
; /* inode fork pointer */
528 if (whichfork
== XFS_ATTR_FORK
)
529 state
|= BMAP_ATTRFORK
;
530 else if (whichfork
== XFS_COW_FORK
)
531 state
|= BMAP_COWFORK
;
533 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
534 ASSERT(cnt
== xfs_iext_count(ifp
));
535 for (idx
= 0; idx
< cnt
; idx
++)
536 trace_xfs_extlist(ip
, idx
, state
, caller_ip
);
540 * Validate that the bmbt_irecs being returned from bmapi are valid
541 * given the caller's original parameters. Specifically check the
542 * ranges of the returned irecs to ensure that they only extend beyond
543 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
546 xfs_bmap_validate_ret(
550 xfs_bmbt_irec_t
*mval
,
554 int i
; /* index to map values */
556 ASSERT(ret_nmap
<= nmap
);
558 for (i
= 0; i
< ret_nmap
; i
++) {
559 ASSERT(mval
[i
].br_blockcount
> 0);
560 if (!(flags
& XFS_BMAPI_ENTIRE
)) {
561 ASSERT(mval
[i
].br_startoff
>= bno
);
562 ASSERT(mval
[i
].br_blockcount
<= len
);
563 ASSERT(mval
[i
].br_startoff
+ mval
[i
].br_blockcount
<=
566 ASSERT(mval
[i
].br_startoff
< bno
+ len
);
567 ASSERT(mval
[i
].br_startoff
+ mval
[i
].br_blockcount
>
571 mval
[i
- 1].br_startoff
+ mval
[i
- 1].br_blockcount
==
572 mval
[i
].br_startoff
);
573 ASSERT(mval
[i
].br_startblock
!= DELAYSTARTBLOCK
&&
574 mval
[i
].br_startblock
!= HOLESTARTBLOCK
);
575 ASSERT(mval
[i
].br_state
== XFS_EXT_NORM
||
576 mval
[i
].br_state
== XFS_EXT_UNWRITTEN
);
581 #define xfs_bmap_check_leaf_extents(cur, ip, whichfork) do { } while (0)
582 #define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap) do { } while (0)
586 * bmap free list manipulation functions
590 * Add the extent to the list of extents to be free at transaction end.
591 * The list is maintained sorted (by block number).
595 struct xfs_mount
*mp
,
596 struct xfs_defer_ops
*dfops
,
599 struct xfs_owner_info
*oinfo
)
601 struct xfs_extent_free_item
*new; /* new element */
606 ASSERT(bno
!= NULLFSBLOCK
);
608 ASSERT(len
<= MAXEXTLEN
);
609 ASSERT(!isnullstartblock(bno
));
610 agno
= XFS_FSB_TO_AGNO(mp
, bno
);
611 agbno
= XFS_FSB_TO_AGBNO(mp
, bno
);
612 ASSERT(agno
< mp
->m_sb
.sb_agcount
);
613 ASSERT(agbno
< mp
->m_sb
.sb_agblocks
);
614 ASSERT(len
< mp
->m_sb
.sb_agblocks
);
615 ASSERT(agbno
+ len
<= mp
->m_sb
.sb_agblocks
);
617 ASSERT(xfs_bmap_free_item_zone
!= NULL
);
619 new = kmem_zone_alloc(xfs_bmap_free_item_zone
, KM_SLEEP
);
620 new->xefi_startblock
= bno
;
621 new->xefi_blockcount
= (xfs_extlen_t
)len
;
623 new->xefi_oinfo
= *oinfo
;
625 xfs_rmap_skip_owner_update(&new->xefi_oinfo
);
626 trace_xfs_bmap_free_defer(mp
, XFS_FSB_TO_AGNO(mp
, bno
), 0,
627 XFS_FSB_TO_AGBNO(mp
, bno
), len
);
628 xfs_defer_add(dfops
, XFS_DEFER_OPS_TYPE_FREE
, &new->xefi_list
);
632 * Inode fork format manipulation functions
636 * Transform a btree format file with only one leaf node, where the
637 * extents list will fit in the inode, into an extents format file.
638 * Since the file extents are already in-core, all we have to do is
639 * give up the space for the btree root and pitch the leaf block.
641 STATIC
int /* error */
642 xfs_bmap_btree_to_extents(
643 xfs_trans_t
*tp
, /* transaction pointer */
644 xfs_inode_t
*ip
, /* incore inode pointer */
645 xfs_btree_cur_t
*cur
, /* btree cursor */
646 int *logflagsp
, /* inode logging flags */
647 int whichfork
) /* data or attr fork */
650 struct xfs_btree_block
*cblock
;/* child btree block */
651 xfs_fsblock_t cbno
; /* child block number */
652 xfs_buf_t
*cbp
; /* child block's buffer */
653 int error
; /* error return value */
654 xfs_ifork_t
*ifp
; /* inode fork data */
655 xfs_mount_t
*mp
; /* mount point structure */
656 __be64
*pp
; /* ptr to block address */
657 struct xfs_btree_block
*rblock
;/* root btree block */
658 struct xfs_owner_info oinfo
;
661 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
662 ASSERT(whichfork
!= XFS_COW_FORK
);
663 ASSERT(ifp
->if_flags
& XFS_IFEXTENTS
);
664 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
);
665 rblock
= ifp
->if_broot
;
666 ASSERT(be16_to_cpu(rblock
->bb_level
) == 1);
667 ASSERT(be16_to_cpu(rblock
->bb_numrecs
) == 1);
668 ASSERT(xfs_bmbt_maxrecs(mp
, ifp
->if_broot_bytes
, 0) == 1);
669 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, rblock
, 1, ifp
->if_broot_bytes
);
670 cbno
= be64_to_cpu(*pp
);
673 if ((error
= xfs_btree_check_lptr(cur
, cbno
, 1)))
676 error
= xfs_btree_read_bufl(mp
, tp
, cbno
, 0, &cbp
, XFS_BMAP_BTREE_REF
,
680 cblock
= XFS_BUF_TO_BLOCK(cbp
);
681 if ((error
= xfs_btree_check_block(cur
, cblock
, 0, cbp
)))
683 xfs_rmap_ino_bmbt_owner(&oinfo
, ip
->i_ino
, whichfork
);
684 xfs_bmap_add_free(mp
, cur
->bc_private
.b
.dfops
, cbno
, 1, &oinfo
);
685 ip
->i_d
.di_nblocks
--;
686 xfs_trans_mod_dquot_byino(tp
, ip
, XFS_TRANS_DQ_BCOUNT
, -1L);
687 xfs_trans_binval(tp
, cbp
);
688 if (cur
->bc_bufs
[0] == cbp
)
689 cur
->bc_bufs
[0] = NULL
;
690 xfs_iroot_realloc(ip
, -1, whichfork
);
691 ASSERT(ifp
->if_broot
== NULL
);
692 ASSERT((ifp
->if_flags
& XFS_IFBROOT
) == 0);
693 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_EXTENTS
);
694 *logflagsp
= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
699 * Convert an extents-format file into a btree-format file.
700 * The new file will have a root block (in the inode) and a single child block.
702 STATIC
int /* error */
703 xfs_bmap_extents_to_btree(
704 xfs_trans_t
*tp
, /* transaction pointer */
705 xfs_inode_t
*ip
, /* incore inode pointer */
706 xfs_fsblock_t
*firstblock
, /* first-block-allocated */
707 struct xfs_defer_ops
*dfops
, /* blocks freed in xaction */
708 xfs_btree_cur_t
**curp
, /* cursor returned to caller */
709 int wasdel
, /* converting a delayed alloc */
710 int *logflagsp
, /* inode logging flags */
711 int whichfork
) /* data or attr fork */
713 struct xfs_btree_block
*ablock
; /* allocated (child) bt block */
714 xfs_buf_t
*abp
; /* buffer for ablock */
715 xfs_alloc_arg_t args
; /* allocation arguments */
716 xfs_bmbt_rec_t
*arp
; /* child record pointer */
717 struct xfs_btree_block
*block
; /* btree root block */
718 xfs_btree_cur_t
*cur
; /* bmap btree cursor */
719 xfs_bmbt_rec_host_t
*ep
; /* extent record pointer */
720 int error
; /* error return value */
721 xfs_extnum_t i
, cnt
; /* extent record index */
722 xfs_ifork_t
*ifp
; /* inode fork pointer */
723 xfs_bmbt_key_t
*kp
; /* root block key pointer */
724 xfs_mount_t
*mp
; /* mount structure */
725 xfs_extnum_t nextents
; /* number of file extents */
726 xfs_bmbt_ptr_t
*pp
; /* root block address pointer */
729 ASSERT(whichfork
!= XFS_COW_FORK
);
730 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
731 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
);
734 * Make space in the inode incore.
736 xfs_iroot_realloc(ip
, 1, whichfork
);
737 ifp
->if_flags
|= XFS_IFBROOT
;
742 block
= ifp
->if_broot
;
743 if (xfs_sb_version_hascrc(&mp
->m_sb
))
744 xfs_btree_init_block_int(mp
, block
, XFS_BUF_DADDR_NULL
,
745 XFS_BMAP_CRC_MAGIC
, 1, 1, ip
->i_ino
,
746 XFS_BTREE_LONG_PTRS
| XFS_BTREE_CRC_BLOCKS
);
748 xfs_btree_init_block_int(mp
, block
, XFS_BUF_DADDR_NULL
,
749 XFS_BMAP_MAGIC
, 1, 1, ip
->i_ino
,
750 XFS_BTREE_LONG_PTRS
);
753 * Need a cursor. Can't allocate until bb_level is filled in.
755 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
756 cur
->bc_private
.b
.firstblock
= *firstblock
;
757 cur
->bc_private
.b
.dfops
= dfops
;
758 cur
->bc_private
.b
.flags
= wasdel
? XFS_BTCUR_BPRV_WASDEL
: 0;
760 * Convert to a btree with two levels, one record in root.
762 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_BTREE
);
763 memset(&args
, 0, sizeof(args
));
766 xfs_rmap_ino_bmbt_owner(&args
.oinfo
, ip
->i_ino
, whichfork
);
767 args
.firstblock
= *firstblock
;
768 if (*firstblock
== NULLFSBLOCK
) {
769 args
.type
= XFS_ALLOCTYPE_START_BNO
;
770 args
.fsbno
= XFS_INO_TO_FSB(mp
, ip
->i_ino
);
771 } else if (dfops
->dop_low
) {
772 args
.type
= XFS_ALLOCTYPE_START_BNO
;
774 args
.fsbno
= *firstblock
;
776 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
777 args
.fsbno
= *firstblock
;
779 args
.minlen
= args
.maxlen
= args
.prod
= 1;
780 args
.wasdel
= wasdel
;
782 if ((error
= xfs_alloc_vextent(&args
))) {
783 xfs_iroot_realloc(ip
, -1, whichfork
);
784 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
789 * During a CoW operation, the allocation and bmbt updates occur in
790 * different transactions. The mapping code tries to put new bmbt
791 * blocks near extents being mapped, but the only way to guarantee this
792 * is if the alloc and the mapping happen in a single transaction that
793 * has a block reservation. That isn't the case here, so if we run out
794 * of space we'll try again with another AG.
796 if (xfs_sb_version_hasreflink(&cur
->bc_mp
->m_sb
) &&
797 args
.fsbno
== NULLFSBLOCK
&&
798 args
.type
== XFS_ALLOCTYPE_NEAR_BNO
) {
799 args
.type
= XFS_ALLOCTYPE_FIRST_AG
;
802 if (WARN_ON_ONCE(args
.fsbno
== NULLFSBLOCK
)) {
803 xfs_iroot_realloc(ip
, -1, whichfork
);
804 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
808 * Allocation can't fail, the space was reserved.
810 ASSERT(*firstblock
== NULLFSBLOCK
||
811 args
.agno
>= XFS_FSB_TO_AGNO(mp
, *firstblock
));
812 *firstblock
= cur
->bc_private
.b
.firstblock
= args
.fsbno
;
813 cur
->bc_private
.b
.allocated
++;
814 ip
->i_d
.di_nblocks
++;
815 xfs_trans_mod_dquot_byino(tp
, ip
, XFS_TRANS_DQ_BCOUNT
, 1L);
816 abp
= xfs_btree_get_bufl(mp
, tp
, args
.fsbno
, 0);
818 * Fill in the child block.
820 abp
->b_ops
= &xfs_bmbt_buf_ops
;
821 ablock
= XFS_BUF_TO_BLOCK(abp
);
822 if (xfs_sb_version_hascrc(&mp
->m_sb
))
823 xfs_btree_init_block_int(mp
, ablock
, abp
->b_bn
,
824 XFS_BMAP_CRC_MAGIC
, 0, 0, ip
->i_ino
,
825 XFS_BTREE_LONG_PTRS
| XFS_BTREE_CRC_BLOCKS
);
827 xfs_btree_init_block_int(mp
, ablock
, abp
->b_bn
,
828 XFS_BMAP_MAGIC
, 0, 0, ip
->i_ino
,
829 XFS_BTREE_LONG_PTRS
);
831 arp
= XFS_BMBT_REC_ADDR(mp
, ablock
, 1);
832 nextents
= xfs_iext_count(ifp
);
833 for (cnt
= i
= 0; i
< nextents
; i
++) {
834 ep
= xfs_iext_get_ext(ifp
, i
);
835 if (!isnullstartblock(xfs_bmbt_get_startblock(ep
))) {
836 arp
->l0
= cpu_to_be64(ep
->l0
);
837 arp
->l1
= cpu_to_be64(ep
->l1
);
841 ASSERT(cnt
== XFS_IFORK_NEXTENTS(ip
, whichfork
));
842 xfs_btree_set_numrecs(ablock
, cnt
);
845 * Fill in the root key and pointer.
847 kp
= XFS_BMBT_KEY_ADDR(mp
, block
, 1);
848 arp
= XFS_BMBT_REC_ADDR(mp
, ablock
, 1);
849 kp
->br_startoff
= cpu_to_be64(xfs_bmbt_disk_get_startoff(arp
));
850 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, xfs_bmbt_get_maxrecs(cur
,
851 be16_to_cpu(block
->bb_level
)));
852 *pp
= cpu_to_be64(args
.fsbno
);
855 * Do all this logging at the end so that
856 * the root is at the right level.
858 xfs_btree_log_block(cur
, abp
, XFS_BB_ALL_BITS
);
859 xfs_btree_log_recs(cur
, abp
, 1, be16_to_cpu(ablock
->bb_numrecs
));
860 ASSERT(*curp
== NULL
);
862 *logflagsp
= XFS_ILOG_CORE
| xfs_ilog_fbroot(whichfork
);
867 * Convert a local file to an extents file.
868 * This code is out of bounds for data forks of regular files,
869 * since the file data needs to get logged so things will stay consistent.
870 * (The bmap-level manipulations are ok, though).
873 xfs_bmap_local_to_extents_empty(
874 struct xfs_inode
*ip
,
877 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
879 ASSERT(whichfork
!= XFS_COW_FORK
);
880 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
881 ASSERT(ifp
->if_bytes
== 0);
882 ASSERT(XFS_IFORK_NEXTENTS(ip
, whichfork
) == 0);
884 xfs_bmap_forkoff_reset(ip
, whichfork
);
885 ifp
->if_flags
&= ~XFS_IFINLINE
;
886 ifp
->if_flags
|= XFS_IFEXTENTS
;
887 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_EXTENTS
);
891 STATIC
int /* error */
892 xfs_bmap_local_to_extents(
893 xfs_trans_t
*tp
, /* transaction pointer */
894 xfs_inode_t
*ip
, /* incore inode pointer */
895 xfs_fsblock_t
*firstblock
, /* first block allocated in xaction */
896 xfs_extlen_t total
, /* total blocks needed by transaction */
897 int *logflagsp
, /* inode logging flags */
899 void (*init_fn
)(struct xfs_trans
*tp
,
901 struct xfs_inode
*ip
,
902 struct xfs_ifork
*ifp
))
905 int flags
; /* logging flags returned */
906 xfs_ifork_t
*ifp
; /* inode fork pointer */
907 xfs_alloc_arg_t args
; /* allocation arguments */
908 xfs_buf_t
*bp
; /* buffer for extent block */
909 xfs_bmbt_rec_host_t
*ep
; /* extent record pointer */
912 * We don't want to deal with the case of keeping inode data inline yet.
913 * So sending the data fork of a regular inode is invalid.
915 ASSERT(!(S_ISREG(VFS_I(ip
)->i_mode
) && whichfork
== XFS_DATA_FORK
));
916 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
917 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
919 if (!ifp
->if_bytes
) {
920 xfs_bmap_local_to_extents_empty(ip
, whichfork
);
921 flags
= XFS_ILOG_CORE
;
927 ASSERT((ifp
->if_flags
& (XFS_IFINLINE
|XFS_IFEXTENTS
|XFS_IFEXTIREC
)) ==
929 memset(&args
, 0, sizeof(args
));
931 args
.mp
= ip
->i_mount
;
932 xfs_rmap_ino_owner(&args
.oinfo
, ip
->i_ino
, whichfork
, 0);
933 args
.firstblock
= *firstblock
;
935 * Allocate a block. We know we need only one, since the
936 * file currently fits in an inode.
938 if (*firstblock
== NULLFSBLOCK
) {
940 args
.fsbno
= XFS_INO_TO_FSB(args
.mp
, ip
->i_ino
);
941 args
.type
= XFS_ALLOCTYPE_START_BNO
;
943 args
.fsbno
= *firstblock
;
944 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
947 args
.minlen
= args
.maxlen
= args
.prod
= 1;
948 error
= xfs_alloc_vextent(&args
);
953 * During a CoW operation, the allocation and bmbt updates occur in
954 * different transactions. The mapping code tries to put new bmbt
955 * blocks near extents being mapped, but the only way to guarantee this
956 * is if the alloc and the mapping happen in a single transaction that
957 * has a block reservation. That isn't the case here, so if we run out
958 * of space we'll try again with another AG.
960 if (xfs_sb_version_hasreflink(&ip
->i_mount
->m_sb
) &&
961 args
.fsbno
== NULLFSBLOCK
&&
962 args
.type
== XFS_ALLOCTYPE_NEAR_BNO
) {
965 /* Can't fail, the space was reserved. */
966 ASSERT(args
.fsbno
!= NULLFSBLOCK
);
967 ASSERT(args
.len
== 1);
968 *firstblock
= args
.fsbno
;
969 bp
= xfs_btree_get_bufl(args
.mp
, tp
, args
.fsbno
, 0);
972 * Initialize the block, copy the data and log the remote buffer.
974 * The callout is responsible for logging because the remote format
975 * might differ from the local format and thus we don't know how much to
976 * log here. Note that init_fn must also set the buffer log item type
979 init_fn(tp
, bp
, ip
, ifp
);
981 /* account for the change in fork size */
982 xfs_idata_realloc(ip
, -ifp
->if_bytes
, whichfork
);
983 xfs_bmap_local_to_extents_empty(ip
, whichfork
);
984 flags
|= XFS_ILOG_CORE
;
986 xfs_iext_add(ifp
, 0, 1);
987 ep
= xfs_iext_get_ext(ifp
, 0);
988 xfs_bmbt_set_allf(ep
, 0, args
.fsbno
, 1, XFS_EXT_NORM
);
989 trace_xfs_bmap_post_update(ip
, 0,
990 whichfork
== XFS_ATTR_FORK
? BMAP_ATTRFORK
: 0,
992 XFS_IFORK_NEXT_SET(ip
, whichfork
, 1);
993 ip
->i_d
.di_nblocks
= 1;
994 xfs_trans_mod_dquot_byino(tp
, ip
,
995 XFS_TRANS_DQ_BCOUNT
, 1L);
996 flags
|= xfs_ilog_fext(whichfork
);
1004 * Called from xfs_bmap_add_attrfork to handle btree format files.
1006 STATIC
int /* error */
1007 xfs_bmap_add_attrfork_btree(
1008 xfs_trans_t
*tp
, /* transaction pointer */
1009 xfs_inode_t
*ip
, /* incore inode pointer */
1010 xfs_fsblock_t
*firstblock
, /* first block allocated */
1011 struct xfs_defer_ops
*dfops
, /* blocks to free at commit */
1012 int *flags
) /* inode logging flags */
1014 xfs_btree_cur_t
*cur
; /* btree cursor */
1015 int error
; /* error return value */
1016 xfs_mount_t
*mp
; /* file system mount struct */
1017 int stat
; /* newroot status */
1020 if (ip
->i_df
.if_broot_bytes
<= XFS_IFORK_DSIZE(ip
))
1021 *flags
|= XFS_ILOG_DBROOT
;
1023 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, XFS_DATA_FORK
);
1024 cur
->bc_private
.b
.dfops
= dfops
;
1025 cur
->bc_private
.b
.firstblock
= *firstblock
;
1026 if ((error
= xfs_bmbt_lookup_ge(cur
, 0, 0, 0, &stat
)))
1028 /* must be at least one entry */
1029 XFS_WANT_CORRUPTED_GOTO(mp
, stat
== 1, error0
);
1030 if ((error
= xfs_btree_new_iroot(cur
, flags
, &stat
)))
1033 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
1036 *firstblock
= cur
->bc_private
.b
.firstblock
;
1037 cur
->bc_private
.b
.allocated
= 0;
1038 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
1042 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
1047 * Called from xfs_bmap_add_attrfork to handle extents format files.
1049 STATIC
int /* error */
1050 xfs_bmap_add_attrfork_extents(
1051 xfs_trans_t
*tp
, /* transaction pointer */
1052 xfs_inode_t
*ip
, /* incore inode pointer */
1053 xfs_fsblock_t
*firstblock
, /* first block allocated */
1054 struct xfs_defer_ops
*dfops
, /* blocks to free at commit */
1055 int *flags
) /* inode logging flags */
1057 xfs_btree_cur_t
*cur
; /* bmap btree cursor */
1058 int error
; /* error return value */
1060 if (ip
->i_d
.di_nextents
* sizeof(xfs_bmbt_rec_t
) <= XFS_IFORK_DSIZE(ip
))
1063 error
= xfs_bmap_extents_to_btree(tp
, ip
, firstblock
, dfops
, &cur
, 0,
1064 flags
, XFS_DATA_FORK
);
1066 cur
->bc_private
.b
.allocated
= 0;
1067 xfs_btree_del_cursor(cur
,
1068 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
1074 * Called from xfs_bmap_add_attrfork to handle local format files. Each
1075 * different data fork content type needs a different callout to do the
1076 * conversion. Some are basic and only require special block initialisation
1077 * callouts for the data formating, others (directories) are so specialised they
1078 * handle everything themselves.
1080 * XXX (dgc): investigate whether directory conversion can use the generic
1081 * formatting callout. It should be possible - it's just a very complex
1084 STATIC
int /* error */
1085 xfs_bmap_add_attrfork_local(
1086 xfs_trans_t
*tp
, /* transaction pointer */
1087 xfs_inode_t
*ip
, /* incore inode pointer */
1088 xfs_fsblock_t
*firstblock
, /* first block allocated */
1089 struct xfs_defer_ops
*dfops
, /* blocks to free at commit */
1090 int *flags
) /* inode logging flags */
1092 xfs_da_args_t dargs
; /* args for dir/attr code */
1094 if (ip
->i_df
.if_bytes
<= XFS_IFORK_DSIZE(ip
))
1097 if (S_ISDIR(VFS_I(ip
)->i_mode
)) {
1098 memset(&dargs
, 0, sizeof(dargs
));
1099 dargs
.geo
= ip
->i_mount
->m_dir_geo
;
1101 dargs
.firstblock
= firstblock
;
1102 dargs
.dfops
= dfops
;
1103 dargs
.total
= dargs
.geo
->fsbcount
;
1104 dargs
.whichfork
= XFS_DATA_FORK
;
1106 return xfs_dir2_sf_to_block(&dargs
);
1109 if (S_ISLNK(VFS_I(ip
)->i_mode
))
1110 return xfs_bmap_local_to_extents(tp
, ip
, firstblock
, 1,
1111 flags
, XFS_DATA_FORK
,
1112 xfs_symlink_local_to_remote
);
1114 /* should only be called for types that support local format data */
1116 return -EFSCORRUPTED
;
1120 * Convert inode from non-attributed to attributed.
1121 * Must not be in a transaction, ip must not be locked.
1123 int /* error code */
1124 xfs_bmap_add_attrfork(
1125 xfs_inode_t
*ip
, /* incore inode pointer */
1126 int size
, /* space new attribute needs */
1127 int rsvd
) /* xact may use reserved blks */
1129 xfs_fsblock_t firstblock
; /* 1st block/ag allocated */
1130 struct xfs_defer_ops dfops
; /* freed extent records */
1131 xfs_mount_t
*mp
; /* mount structure */
1132 xfs_trans_t
*tp
; /* transaction pointer */
1133 int blks
; /* space reservation */
1134 int version
= 1; /* superblock attr version */
1135 int logflags
; /* logging flags */
1136 int error
; /* error return value */
1138 ASSERT(XFS_IFORK_Q(ip
) == 0);
1141 ASSERT(!XFS_NOT_DQATTACHED(mp
, ip
));
1143 blks
= XFS_ADDAFORK_SPACE_RES(mp
);
1145 error
= xfs_trans_alloc(mp
, &M_RES(mp
)->tr_addafork
, blks
, 0,
1146 rsvd
? XFS_TRANS_RESERVE
: 0, &tp
);
1150 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
1151 error
= xfs_trans_reserve_quota_nblks(tp
, ip
, blks
, 0, rsvd
?
1152 XFS_QMOPT_RES_REGBLKS
| XFS_QMOPT_FORCE_RES
:
1153 XFS_QMOPT_RES_REGBLKS
);
1156 if (XFS_IFORK_Q(ip
))
1158 if (ip
->i_d
.di_anextents
!= 0) {
1159 error
= -EFSCORRUPTED
;
1162 if (ip
->i_d
.di_aformat
!= XFS_DINODE_FMT_EXTENTS
) {
1164 * For inodes coming from pre-6.2 filesystems.
1166 ASSERT(ip
->i_d
.di_aformat
== 0);
1167 ip
->i_d
.di_aformat
= XFS_DINODE_FMT_EXTENTS
;
1170 xfs_trans_ijoin(tp
, ip
, 0);
1171 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
1173 switch (ip
->i_d
.di_format
) {
1174 case XFS_DINODE_FMT_DEV
:
1175 ip
->i_d
.di_forkoff
= roundup(sizeof(xfs_dev_t
), 8) >> 3;
1177 case XFS_DINODE_FMT_UUID
:
1178 ip
->i_d
.di_forkoff
= roundup(sizeof(uuid_t
), 8) >> 3;
1180 case XFS_DINODE_FMT_LOCAL
:
1181 case XFS_DINODE_FMT_EXTENTS
:
1182 case XFS_DINODE_FMT_BTREE
:
1183 ip
->i_d
.di_forkoff
= xfs_attr_shortform_bytesfit(ip
, size
);
1184 if (!ip
->i_d
.di_forkoff
)
1185 ip
->i_d
.di_forkoff
= xfs_default_attroffset(ip
) >> 3;
1186 else if (mp
->m_flags
& XFS_MOUNT_ATTR2
)
1195 ASSERT(ip
->i_afp
== NULL
);
1196 ip
->i_afp
= kmem_zone_zalloc(xfs_ifork_zone
, KM_SLEEP
);
1197 ip
->i_afp
->if_flags
= XFS_IFEXTENTS
;
1199 xfs_defer_init(&dfops
, &firstblock
);
1200 switch (ip
->i_d
.di_format
) {
1201 case XFS_DINODE_FMT_LOCAL
:
1202 error
= xfs_bmap_add_attrfork_local(tp
, ip
, &firstblock
, &dfops
,
1205 case XFS_DINODE_FMT_EXTENTS
:
1206 error
= xfs_bmap_add_attrfork_extents(tp
, ip
, &firstblock
,
1209 case XFS_DINODE_FMT_BTREE
:
1210 error
= xfs_bmap_add_attrfork_btree(tp
, ip
, &firstblock
, &dfops
,
1218 xfs_trans_log_inode(tp
, ip
, logflags
);
1221 if (!xfs_sb_version_hasattr(&mp
->m_sb
) ||
1222 (!xfs_sb_version_hasattr2(&mp
->m_sb
) && version
== 2)) {
1223 bool log_sb
= false;
1225 spin_lock(&mp
->m_sb_lock
);
1226 if (!xfs_sb_version_hasattr(&mp
->m_sb
)) {
1227 xfs_sb_version_addattr(&mp
->m_sb
);
1230 if (!xfs_sb_version_hasattr2(&mp
->m_sb
) && version
== 2) {
1231 xfs_sb_version_addattr2(&mp
->m_sb
);
1234 spin_unlock(&mp
->m_sb_lock
);
1239 error
= xfs_defer_finish(&tp
, &dfops
, NULL
);
1242 error
= xfs_trans_commit(tp
);
1243 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
1247 xfs_defer_cancel(&dfops
);
1249 xfs_trans_cancel(tp
);
1250 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
1255 * Internal and external extent tree search functions.
1259 * Read in the extents to if_extents.
1260 * All inode fields are set up by caller, we just traverse the btree
1261 * and copy the records in. If the file system cannot contain unwritten
1262 * extents, the records are checked for no "state" flags.
1265 xfs_bmap_read_extents(
1266 xfs_trans_t
*tp
, /* transaction pointer */
1267 xfs_inode_t
*ip
, /* incore inode */
1268 int whichfork
) /* data or attr fork */
1270 struct xfs_btree_block
*block
; /* current btree block */
1271 xfs_fsblock_t bno
; /* block # of "block" */
1272 xfs_buf_t
*bp
; /* buffer for "block" */
1273 int error
; /* error return value */
1274 xfs_exntfmt_t exntf
; /* XFS_EXTFMT_NOSTATE, if checking */
1275 xfs_extnum_t i
, j
; /* index into the extents list */
1276 xfs_ifork_t
*ifp
; /* fork structure */
1277 int level
; /* btree level, for checking */
1278 xfs_mount_t
*mp
; /* file system mount structure */
1279 __be64
*pp
; /* pointer to block address */
1281 xfs_extnum_t room
; /* number of entries there's room for */
1284 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1285 exntf
= (whichfork
!= XFS_DATA_FORK
) ? XFS_EXTFMT_NOSTATE
:
1286 XFS_EXTFMT_INODE(ip
);
1287 block
= ifp
->if_broot
;
1289 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
1291 level
= be16_to_cpu(block
->bb_level
);
1293 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, 1, ifp
->if_broot_bytes
);
1294 bno
= be64_to_cpu(*pp
);
1297 * Go down the tree until leaf level is reached, following the first
1298 * pointer (leftmost) at each level.
1300 while (level
-- > 0) {
1301 error
= xfs_btree_read_bufl(mp
, tp
, bno
, 0, &bp
,
1302 XFS_BMAP_BTREE_REF
, &xfs_bmbt_buf_ops
);
1305 block
= XFS_BUF_TO_BLOCK(bp
);
1308 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, mp
->m_bmap_dmxr
[1]);
1309 bno
= be64_to_cpu(*pp
);
1310 XFS_WANT_CORRUPTED_GOTO(mp
,
1311 XFS_FSB_SANITY_CHECK(mp
, bno
), error0
);
1312 xfs_trans_brelse(tp
, bp
);
1315 * Here with bp and block set to the leftmost leaf node in the tree.
1317 room
= xfs_iext_count(ifp
);
1320 * Loop over all leaf nodes. Copy information to the extent records.
1323 xfs_bmbt_rec_t
*frp
;
1324 xfs_fsblock_t nextbno
;
1325 xfs_extnum_t num_recs
;
1328 num_recs
= xfs_btree_get_numrecs(block
);
1329 if (unlikely(i
+ num_recs
> room
)) {
1330 ASSERT(i
+ num_recs
<= room
);
1331 xfs_warn(ip
->i_mount
,
1332 "corrupt dinode %Lu, (btree extents).",
1333 (unsigned long long) ip
->i_ino
);
1334 XFS_CORRUPTION_ERROR("xfs_bmap_read_extents(1)",
1335 XFS_ERRLEVEL_LOW
, ip
->i_mount
, block
);
1339 * Read-ahead the next leaf block, if any.
1341 nextbno
= be64_to_cpu(block
->bb_u
.l
.bb_rightsib
);
1342 if (nextbno
!= NULLFSBLOCK
)
1343 xfs_btree_reada_bufl(mp
, nextbno
, 1,
1346 * Copy records into the extent records.
1348 frp
= XFS_BMBT_REC_ADDR(mp
, block
, 1);
1350 for (j
= 0; j
< num_recs
; j
++, i
++, frp
++) {
1351 xfs_bmbt_rec_host_t
*trp
= xfs_iext_get_ext(ifp
, i
);
1352 trp
->l0
= be64_to_cpu(frp
->l0
);
1353 trp
->l1
= be64_to_cpu(frp
->l1
);
1355 if (exntf
== XFS_EXTFMT_NOSTATE
) {
1357 * Check all attribute bmap btree records and
1358 * any "older" data bmap btree records for a
1359 * set bit in the "extent flag" position.
1361 if (unlikely(xfs_check_nostate_extents(ifp
,
1362 start
, num_recs
))) {
1363 XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
1369 xfs_trans_brelse(tp
, bp
);
1372 * If we've reached the end, stop.
1374 if (bno
== NULLFSBLOCK
)
1376 error
= xfs_btree_read_bufl(mp
, tp
, bno
, 0, &bp
,
1377 XFS_BMAP_BTREE_REF
, &xfs_bmbt_buf_ops
);
1380 block
= XFS_BUF_TO_BLOCK(bp
);
1382 if (i
!= XFS_IFORK_NEXTENTS(ip
, whichfork
))
1383 return -EFSCORRUPTED
;
1384 ASSERT(i
== xfs_iext_count(ifp
));
1385 XFS_BMAP_TRACE_EXLIST(ip
, i
, whichfork
);
1388 xfs_trans_brelse(tp
, bp
);
1389 return -EFSCORRUPTED
;
1394 * Search the extent records for the entry containing block bno.
1395 * If bno lies in a hole, point to the next entry. If bno lies
1396 * past eof, *eofp will be set, and *prevp will contain the last
1397 * entry (null if none). Else, *lastxp will be set to the index
1398 * of the found entry; *gotp will contain the entry.
1400 STATIC xfs_bmbt_rec_host_t
* /* pointer to found extent entry */
1401 xfs_bmap_search_multi_extents(
1402 xfs_ifork_t
*ifp
, /* inode fork pointer */
1403 xfs_fileoff_t bno
, /* block number searched for */
1404 int *eofp
, /* out: end of file found */
1405 xfs_extnum_t
*lastxp
, /* out: last extent index */
1406 xfs_bmbt_irec_t
*gotp
, /* out: extent entry found */
1407 xfs_bmbt_irec_t
*prevp
) /* out: previous extent entry found */
1409 xfs_bmbt_rec_host_t
*ep
; /* extent record pointer */
1410 xfs_extnum_t lastx
; /* last extent index */
1413 * Initialize the extent entry structure to catch access to
1414 * uninitialized br_startblock field.
1416 gotp
->br_startoff
= 0xffa5a5a5a5a5a5a5LL
;
1417 gotp
->br_blockcount
= 0xa55a5a5a5a5a5a5aLL
;
1418 gotp
->br_state
= XFS_EXT_INVALID
;
1419 gotp
->br_startblock
= 0xffffa5a5a5a5a5a5LL
;
1420 prevp
->br_startoff
= NULLFILEOFF
;
1422 ep
= xfs_iext_bno_to_ext(ifp
, bno
, &lastx
);
1424 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, lastx
- 1), prevp
);
1426 if (lastx
< xfs_iext_count(ifp
)) {
1427 xfs_bmbt_get_all(ep
, gotp
);
1441 * Search the extents list for the inode, for the extent containing bno.
1442 * If bno lies in a hole, point to the next entry. If bno lies past eof,
1443 * *eofp will be set, and *prevp will contain the last entry (null if none).
1444 * Else, *lastxp will be set to the index of the found
1445 * entry; *gotp will contain the entry.
1447 xfs_bmbt_rec_host_t
* /* pointer to found extent entry */
1448 xfs_bmap_search_extents(
1449 xfs_inode_t
*ip
, /* incore inode pointer */
1450 xfs_fileoff_t bno
, /* block number searched for */
1451 int fork
, /* data or attr fork */
1452 int *eofp
, /* out: end of file found */
1453 xfs_extnum_t
*lastxp
, /* out: last extent index */
1454 xfs_bmbt_irec_t
*gotp
, /* out: extent entry found */
1455 xfs_bmbt_irec_t
*prevp
) /* out: previous extent entry found */
1457 xfs_ifork_t
*ifp
; /* inode fork pointer */
1458 xfs_bmbt_rec_host_t
*ep
; /* extent record pointer */
1460 XFS_STATS_INC(ip
->i_mount
, xs_look_exlist
);
1461 ifp
= XFS_IFORK_PTR(ip
, fork
);
1463 ep
= xfs_bmap_search_multi_extents(ifp
, bno
, eofp
, lastxp
, gotp
, prevp
);
1465 if (unlikely(!(gotp
->br_startblock
) && (*lastxp
!= NULLEXTNUM
) &&
1466 !(XFS_IS_REALTIME_INODE(ip
) && fork
== XFS_DATA_FORK
))) {
1467 xfs_alert_tag(ip
->i_mount
, XFS_PTAG_FSBLOCK_ZERO
,
1468 "Access to block zero in inode %llu "
1469 "start_block: %llx start_off: %llx "
1470 "blkcnt: %llx extent-state: %x lastx: %x",
1471 (unsigned long long)ip
->i_ino
,
1472 (unsigned long long)gotp
->br_startblock
,
1473 (unsigned long long)gotp
->br_startoff
,
1474 (unsigned long long)gotp
->br_blockcount
,
1475 gotp
->br_state
, *lastxp
);
1476 *lastxp
= NULLEXTNUM
;
1484 * Returns the file-relative block number of the first unused block(s)
1485 * in the file with at least "len" logically contiguous blocks free.
1486 * This is the lowest-address hole if the file has holes, else the first block
1487 * past the end of file.
1488 * Return 0 if the file is currently local (in-inode).
1491 xfs_bmap_first_unused(
1492 xfs_trans_t
*tp
, /* transaction pointer */
1493 xfs_inode_t
*ip
, /* incore inode */
1494 xfs_extlen_t len
, /* size of hole to find */
1495 xfs_fileoff_t
*first_unused
, /* unused block */
1496 int whichfork
) /* data or attr fork */
1498 int error
; /* error return value */
1499 int idx
; /* extent record index */
1500 xfs_ifork_t
*ifp
; /* inode fork pointer */
1501 xfs_fileoff_t lastaddr
; /* last block number seen */
1502 xfs_fileoff_t lowest
; /* lowest useful block */
1503 xfs_fileoff_t max
; /* starting useful block */
1504 xfs_fileoff_t off
; /* offset for this block */
1505 xfs_extnum_t nextents
; /* number of extent entries */
1507 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
||
1508 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
||
1509 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
1510 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
) {
1514 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1515 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
1516 (error
= xfs_iread_extents(tp
, ip
, whichfork
)))
1518 lowest
= *first_unused
;
1519 nextents
= xfs_iext_count(ifp
);
1520 for (idx
= 0, lastaddr
= 0, max
= lowest
; idx
< nextents
; idx
++) {
1521 xfs_bmbt_rec_host_t
*ep
= xfs_iext_get_ext(ifp
, idx
);
1522 off
= xfs_bmbt_get_startoff(ep
);
1524 * See if the hole before this extent will work.
1526 if (off
>= lowest
+ len
&& off
- max
>= len
) {
1527 *first_unused
= max
;
1530 lastaddr
= off
+ xfs_bmbt_get_blockcount(ep
);
1531 max
= XFS_FILEOFF_MAX(lastaddr
, lowest
);
1533 *first_unused
= max
;
1538 * Returns the file-relative block number of the last block - 1 before
1539 * last_block (input value) in the file.
1540 * This is not based on i_size, it is based on the extent records.
1541 * Returns 0 for local files, as they do not have extent records.
1544 xfs_bmap_last_before(
1545 xfs_trans_t
*tp
, /* transaction pointer */
1546 xfs_inode_t
*ip
, /* incore inode */
1547 xfs_fileoff_t
*last_block
, /* last block */
1548 int whichfork
) /* data or attr fork */
1550 xfs_fileoff_t bno
; /* input file offset */
1551 int eof
; /* hit end of file */
1552 xfs_bmbt_rec_host_t
*ep
; /* pointer to last extent */
1553 int error
; /* error return value */
1554 xfs_bmbt_irec_t got
; /* current extent value */
1555 xfs_ifork_t
*ifp
; /* inode fork pointer */
1556 xfs_extnum_t lastx
; /* last extent used */
1557 xfs_bmbt_irec_t prev
; /* previous extent value */
1559 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
&&
1560 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
1561 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_LOCAL
)
1563 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
) {
1567 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1568 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
1569 (error
= xfs_iread_extents(tp
, ip
, whichfork
)))
1571 bno
= *last_block
- 1;
1572 ep
= xfs_bmap_search_extents(ip
, bno
, whichfork
, &eof
, &lastx
, &got
,
1574 if (eof
|| xfs_bmbt_get_startoff(ep
) > bno
) {
1575 if (prev
.br_startoff
== NULLFILEOFF
)
1578 *last_block
= prev
.br_startoff
+ prev
.br_blockcount
;
1581 * Otherwise *last_block is already the right answer.
1587 xfs_bmap_last_extent(
1588 struct xfs_trans
*tp
,
1589 struct xfs_inode
*ip
,
1591 struct xfs_bmbt_irec
*rec
,
1594 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1598 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
1599 error
= xfs_iread_extents(tp
, ip
, whichfork
);
1604 nextents
= xfs_iext_count(ifp
);
1605 if (nextents
== 0) {
1610 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, nextents
- 1), rec
);
1616 * Check the last inode extent to determine whether this allocation will result
1617 * in blocks being allocated at the end of the file. When we allocate new data
1618 * blocks at the end of the file which do not start at the previous data block,
1619 * we will try to align the new blocks at stripe unit boundaries.
1621 * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be
1622 * at, or past the EOF.
1626 struct xfs_bmalloca
*bma
,
1629 struct xfs_bmbt_irec rec
;
1634 error
= xfs_bmap_last_extent(NULL
, bma
->ip
, whichfork
, &rec
,
1645 * Check if we are allocation or past the last extent, or at least into
1646 * the last delayed allocated extent.
1648 bma
->aeof
= bma
->offset
>= rec
.br_startoff
+ rec
.br_blockcount
||
1649 (bma
->offset
>= rec
.br_startoff
&&
1650 isnullstartblock(rec
.br_startblock
));
1655 * Returns the file-relative block number of the first block past eof in
1656 * the file. This is not based on i_size, it is based on the extent records.
1657 * Returns 0 for local files, as they do not have extent records.
1660 xfs_bmap_last_offset(
1661 struct xfs_inode
*ip
,
1662 xfs_fileoff_t
*last_block
,
1665 struct xfs_bmbt_irec rec
;
1671 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
)
1674 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
&&
1675 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
1678 error
= xfs_bmap_last_extent(NULL
, ip
, whichfork
, &rec
, &is_empty
);
1679 if (error
|| is_empty
)
1682 *last_block
= rec
.br_startoff
+ rec
.br_blockcount
;
1687 * Returns whether the selected fork of the inode has exactly one
1688 * block or not. For the data fork we check this matches di_size,
1689 * implying the file's range is 0..bsize-1.
1691 int /* 1=>1 block, 0=>otherwise */
1693 xfs_inode_t
*ip
, /* incore inode */
1694 int whichfork
) /* data or attr fork */
1696 xfs_bmbt_rec_host_t
*ep
; /* ptr to fork's extent */
1697 xfs_ifork_t
*ifp
; /* inode fork pointer */
1698 int rval
; /* return value */
1699 xfs_bmbt_irec_t s
; /* internal version of extent */
1702 if (whichfork
== XFS_DATA_FORK
)
1703 return XFS_ISIZE(ip
) == ip
->i_mount
->m_sb
.sb_blocksize
;
1705 if (XFS_IFORK_NEXTENTS(ip
, whichfork
) != 1)
1707 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
1709 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1710 ASSERT(ifp
->if_flags
& XFS_IFEXTENTS
);
1711 ep
= xfs_iext_get_ext(ifp
, 0);
1712 xfs_bmbt_get_all(ep
, &s
);
1713 rval
= s
.br_startoff
== 0 && s
.br_blockcount
== 1;
1714 if (rval
&& whichfork
== XFS_DATA_FORK
)
1715 ASSERT(XFS_ISIZE(ip
) == ip
->i_mount
->m_sb
.sb_blocksize
);
1720 * Extent tree manipulation functions used during allocation.
1724 * Convert a delayed allocation to a real allocation.
1726 STATIC
int /* error */
1727 xfs_bmap_add_extent_delay_real(
1728 struct xfs_bmalloca
*bma
,
1731 struct xfs_bmbt_irec
*new = &bma
->got
;
1732 int diff
; /* temp value */
1733 xfs_bmbt_rec_host_t
*ep
; /* extent entry for idx */
1734 int error
; /* error return value */
1735 int i
; /* temp state */
1736 xfs_ifork_t
*ifp
; /* inode fork pointer */
1737 xfs_fileoff_t new_endoff
; /* end offset of new entry */
1738 xfs_bmbt_irec_t r
[3]; /* neighbor extent entries */
1739 /* left is 0, right is 1, prev is 2 */
1740 int rval
=0; /* return value (logging flags) */
1741 int state
= 0;/* state bits, accessed thru macros */
1742 xfs_filblks_t da_new
; /* new count del alloc blocks used */
1743 xfs_filblks_t da_old
; /* old count del alloc blocks used */
1744 xfs_filblks_t temp
=0; /* value for da_new calculations */
1745 xfs_filblks_t temp2
=0;/* value for da_new calculations */
1746 int tmp_rval
; /* partial logging flags */
1747 struct xfs_mount
*mp
;
1748 xfs_extnum_t
*nextents
;
1750 mp
= bma
->ip
->i_mount
;
1751 ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
1752 ASSERT(whichfork
!= XFS_ATTR_FORK
);
1753 nextents
= (whichfork
== XFS_COW_FORK
? &bma
->ip
->i_cnextents
:
1754 &bma
->ip
->i_d
.di_nextents
);
1756 ASSERT(bma
->idx
>= 0);
1757 ASSERT(bma
->idx
<= xfs_iext_count(ifp
));
1758 ASSERT(!isnullstartblock(new->br_startblock
));
1760 (bma
->cur
->bc_private
.b
.flags
& XFS_BTCUR_BPRV_WASDEL
));
1762 XFS_STATS_INC(mp
, xs_add_exlist
);
1768 if (whichfork
== XFS_COW_FORK
)
1769 state
|= BMAP_COWFORK
;
1772 * Set up a bunch of variables to make the tests simpler.
1774 ep
= xfs_iext_get_ext(ifp
, bma
->idx
);
1775 xfs_bmbt_get_all(ep
, &PREV
);
1776 new_endoff
= new->br_startoff
+ new->br_blockcount
;
1777 ASSERT(PREV
.br_startoff
<= new->br_startoff
);
1778 ASSERT(PREV
.br_startoff
+ PREV
.br_blockcount
>= new_endoff
);
1780 da_old
= startblockval(PREV
.br_startblock
);
1784 * Set flags determining what part of the previous delayed allocation
1785 * extent is being replaced by a real allocation.
1787 if (PREV
.br_startoff
== new->br_startoff
)
1788 state
|= BMAP_LEFT_FILLING
;
1789 if (PREV
.br_startoff
+ PREV
.br_blockcount
== new_endoff
)
1790 state
|= BMAP_RIGHT_FILLING
;
1793 * Check and set flags if this segment has a left neighbor.
1794 * Don't set contiguous if the combined extent would be too large.
1797 state
|= BMAP_LEFT_VALID
;
1798 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
- 1), &LEFT
);
1800 if (isnullstartblock(LEFT
.br_startblock
))
1801 state
|= BMAP_LEFT_DELAY
;
1804 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
1805 LEFT
.br_startoff
+ LEFT
.br_blockcount
== new->br_startoff
&&
1806 LEFT
.br_startblock
+ LEFT
.br_blockcount
== new->br_startblock
&&
1807 LEFT
.br_state
== new->br_state
&&
1808 LEFT
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
1809 state
|= BMAP_LEFT_CONTIG
;
1812 * Check and set flags if this segment has a right neighbor.
1813 * Don't set contiguous if the combined extent would be too large.
1814 * Also check for all-three-contiguous being too large.
1816 if (bma
->idx
< xfs_iext_count(ifp
) - 1) {
1817 state
|= BMAP_RIGHT_VALID
;
1818 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
+ 1), &RIGHT
);
1820 if (isnullstartblock(RIGHT
.br_startblock
))
1821 state
|= BMAP_RIGHT_DELAY
;
1824 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
1825 new_endoff
== RIGHT
.br_startoff
&&
1826 new->br_startblock
+ new->br_blockcount
== RIGHT
.br_startblock
&&
1827 new->br_state
== RIGHT
.br_state
&&
1828 new->br_blockcount
+ RIGHT
.br_blockcount
<= MAXEXTLEN
&&
1829 ((state
& (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
1830 BMAP_RIGHT_FILLING
)) !=
1831 (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
1832 BMAP_RIGHT_FILLING
) ||
1833 LEFT
.br_blockcount
+ new->br_blockcount
+ RIGHT
.br_blockcount
1835 state
|= BMAP_RIGHT_CONTIG
;
1839 * Switch out based on the FILLING and CONTIG state bits.
1841 switch (state
& (BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
1842 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
)) {
1843 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
1844 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
1846 * Filling in all of a previously delayed allocation extent.
1847 * The left and right neighbors are both contiguous with new.
1850 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1851 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, bma
->idx
),
1852 LEFT
.br_blockcount
+ PREV
.br_blockcount
+
1853 RIGHT
.br_blockcount
);
1854 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1856 xfs_iext_remove(bma
->ip
, bma
->idx
+ 1, 2, state
);
1858 if (bma
->cur
== NULL
)
1859 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1861 rval
= XFS_ILOG_CORE
;
1862 error
= xfs_bmbt_lookup_eq(bma
->cur
, RIGHT
.br_startoff
,
1863 RIGHT
.br_startblock
,
1864 RIGHT
.br_blockcount
, &i
);
1867 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1868 error
= xfs_btree_delete(bma
->cur
, &i
);
1871 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1872 error
= xfs_btree_decrement(bma
->cur
, 0, &i
);
1875 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1876 error
= xfs_bmbt_update(bma
->cur
, LEFT
.br_startoff
,
1878 LEFT
.br_blockcount
+
1879 PREV
.br_blockcount
+
1880 RIGHT
.br_blockcount
, LEFT
.br_state
);
1886 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
1888 * Filling in all of a previously delayed allocation extent.
1889 * The left neighbor is contiguous, the right is not.
1893 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1894 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, bma
->idx
),
1895 LEFT
.br_blockcount
+ PREV
.br_blockcount
);
1896 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1898 xfs_iext_remove(bma
->ip
, bma
->idx
+ 1, 1, state
);
1899 if (bma
->cur
== NULL
)
1900 rval
= XFS_ILOG_DEXT
;
1903 error
= xfs_bmbt_lookup_eq(bma
->cur
, LEFT
.br_startoff
,
1904 LEFT
.br_startblock
, LEFT
.br_blockcount
,
1908 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1909 error
= xfs_bmbt_update(bma
->cur
, LEFT
.br_startoff
,
1911 LEFT
.br_blockcount
+
1912 PREV
.br_blockcount
, LEFT
.br_state
);
1918 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
1920 * Filling in all of a previously delayed allocation extent.
1921 * The right neighbor is contiguous, the left is not.
1923 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1924 xfs_bmbt_set_startblock(ep
, new->br_startblock
);
1925 xfs_bmbt_set_blockcount(ep
,
1926 PREV
.br_blockcount
+ RIGHT
.br_blockcount
);
1927 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1929 xfs_iext_remove(bma
->ip
, bma
->idx
+ 1, 1, state
);
1930 if (bma
->cur
== NULL
)
1931 rval
= XFS_ILOG_DEXT
;
1934 error
= xfs_bmbt_lookup_eq(bma
->cur
, RIGHT
.br_startoff
,
1935 RIGHT
.br_startblock
,
1936 RIGHT
.br_blockcount
, &i
);
1939 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1940 error
= xfs_bmbt_update(bma
->cur
, PREV
.br_startoff
,
1942 PREV
.br_blockcount
+
1943 RIGHT
.br_blockcount
, PREV
.br_state
);
1949 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
1951 * Filling in all of a previously delayed allocation extent.
1952 * Neither the left nor right neighbors are contiguous with
1955 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1956 xfs_bmbt_set_startblock(ep
, new->br_startblock
);
1957 xfs_bmbt_set_state(ep
, new->br_state
);
1958 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1961 if (bma
->cur
== NULL
)
1962 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1964 rval
= XFS_ILOG_CORE
;
1965 error
= xfs_bmbt_lookup_eq(bma
->cur
, new->br_startoff
,
1966 new->br_startblock
, new->br_blockcount
,
1970 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
1971 bma
->cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
1972 error
= xfs_btree_insert(bma
->cur
, &i
);
1975 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1979 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
:
1981 * Filling in the first part of a previous delayed allocation.
1982 * The left neighbor is contiguous.
1984 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
- 1, state
, _THIS_IP_
);
1985 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, bma
->idx
- 1),
1986 LEFT
.br_blockcount
+ new->br_blockcount
);
1987 xfs_bmbt_set_startoff(ep
,
1988 PREV
.br_startoff
+ new->br_blockcount
);
1989 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
- 1, state
, _THIS_IP_
);
1991 temp
= PREV
.br_blockcount
- new->br_blockcount
;
1992 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1993 xfs_bmbt_set_blockcount(ep
, temp
);
1994 if (bma
->cur
== NULL
)
1995 rval
= XFS_ILOG_DEXT
;
1998 error
= xfs_bmbt_lookup_eq(bma
->cur
, LEFT
.br_startoff
,
1999 LEFT
.br_startblock
, LEFT
.br_blockcount
,
2003 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2004 error
= xfs_bmbt_update(bma
->cur
, LEFT
.br_startoff
,
2006 LEFT
.br_blockcount
+
2012 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
2013 startblockval(PREV
.br_startblock
));
2014 xfs_bmbt_set_startblock(ep
, nullstartblock(da_new
));
2015 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2020 case BMAP_LEFT_FILLING
:
2022 * Filling in the first part of a previous delayed allocation.
2023 * The left neighbor is not contiguous.
2025 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2026 xfs_bmbt_set_startoff(ep
, new_endoff
);
2027 temp
= PREV
.br_blockcount
- new->br_blockcount
;
2028 xfs_bmbt_set_blockcount(ep
, temp
);
2029 xfs_iext_insert(bma
->ip
, bma
->idx
, 1, new, state
);
2031 if (bma
->cur
== NULL
)
2032 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2034 rval
= XFS_ILOG_CORE
;
2035 error
= xfs_bmbt_lookup_eq(bma
->cur
, new->br_startoff
,
2036 new->br_startblock
, new->br_blockcount
,
2040 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
2041 bma
->cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
2042 error
= xfs_btree_insert(bma
->cur
, &i
);
2045 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2048 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
2049 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
2050 bma
->firstblock
, bma
->dfops
,
2051 &bma
->cur
, 1, &tmp_rval
, whichfork
);
2056 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
2057 startblockval(PREV
.br_startblock
) -
2058 (bma
->cur
? bma
->cur
->bc_private
.b
.allocated
: 0));
2059 ep
= xfs_iext_get_ext(ifp
, bma
->idx
+ 1);
2060 xfs_bmbt_set_startblock(ep
, nullstartblock(da_new
));
2061 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
+ 1, state
, _THIS_IP_
);
2064 case BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2066 * Filling in the last part of a previous delayed allocation.
2067 * The right neighbor is contiguous with the new allocation.
2069 temp
= PREV
.br_blockcount
- new->br_blockcount
;
2070 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
+ 1, state
, _THIS_IP_
);
2071 xfs_bmbt_set_blockcount(ep
, temp
);
2072 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp
, bma
->idx
+ 1),
2073 new->br_startoff
, new->br_startblock
,
2074 new->br_blockcount
+ RIGHT
.br_blockcount
,
2076 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
+ 1, state
, _THIS_IP_
);
2077 if (bma
->cur
== NULL
)
2078 rval
= XFS_ILOG_DEXT
;
2081 error
= xfs_bmbt_lookup_eq(bma
->cur
, RIGHT
.br_startoff
,
2082 RIGHT
.br_startblock
,
2083 RIGHT
.br_blockcount
, &i
);
2086 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2087 error
= xfs_bmbt_update(bma
->cur
, new->br_startoff
,
2089 new->br_blockcount
+
2090 RIGHT
.br_blockcount
,
2096 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
2097 startblockval(PREV
.br_startblock
));
2098 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2099 xfs_bmbt_set_startblock(ep
, nullstartblock(da_new
));
2100 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2105 case BMAP_RIGHT_FILLING
:
2107 * Filling in the last part of a previous delayed allocation.
2108 * The right neighbor is not contiguous.
2110 temp
= PREV
.br_blockcount
- new->br_blockcount
;
2111 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2112 xfs_bmbt_set_blockcount(ep
, temp
);
2113 xfs_iext_insert(bma
->ip
, bma
->idx
+ 1, 1, new, state
);
2115 if (bma
->cur
== NULL
)
2116 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2118 rval
= XFS_ILOG_CORE
;
2119 error
= xfs_bmbt_lookup_eq(bma
->cur
, new->br_startoff
,
2120 new->br_startblock
, new->br_blockcount
,
2124 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
2125 bma
->cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
2126 error
= xfs_btree_insert(bma
->cur
, &i
);
2129 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2132 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
2133 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
2134 bma
->firstblock
, bma
->dfops
, &bma
->cur
, 1,
2135 &tmp_rval
, whichfork
);
2140 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
2141 startblockval(PREV
.br_startblock
) -
2142 (bma
->cur
? bma
->cur
->bc_private
.b
.allocated
: 0));
2143 ep
= xfs_iext_get_ext(ifp
, bma
->idx
);
2144 xfs_bmbt_set_startblock(ep
, nullstartblock(da_new
));
2145 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2152 * Filling in the middle part of a previous delayed allocation.
2153 * Contiguity is impossible here.
2154 * This case is avoided almost all the time.
2156 * We start with a delayed allocation:
2158 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
2161 * and we are allocating:
2162 * +rrrrrrrrrrrrrrrrr+
2165 * and we set it up for insertion as:
2166 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
2168 * PREV @ idx LEFT RIGHT
2169 * inserted at idx + 1
2171 temp
= new->br_startoff
- PREV
.br_startoff
;
2172 temp2
= PREV
.br_startoff
+ PREV
.br_blockcount
- new_endoff
;
2173 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, 0, _THIS_IP_
);
2174 xfs_bmbt_set_blockcount(ep
, temp
); /* truncate PREV */
2176 RIGHT
.br_state
= PREV
.br_state
;
2177 RIGHT
.br_startblock
= nullstartblock(
2178 (int)xfs_bmap_worst_indlen(bma
->ip
, temp2
));
2179 RIGHT
.br_startoff
= new_endoff
;
2180 RIGHT
.br_blockcount
= temp2
;
2181 /* insert LEFT (r[0]) and RIGHT (r[1]) at the same time */
2182 xfs_iext_insert(bma
->ip
, bma
->idx
+ 1, 2, &LEFT
, state
);
2184 if (bma
->cur
== NULL
)
2185 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2187 rval
= XFS_ILOG_CORE
;
2188 error
= xfs_bmbt_lookup_eq(bma
->cur
, new->br_startoff
,
2189 new->br_startblock
, new->br_blockcount
,
2193 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
2194 bma
->cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
2195 error
= xfs_btree_insert(bma
->cur
, &i
);
2198 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2201 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
2202 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
2203 bma
->firstblock
, bma
->dfops
, &bma
->cur
,
2204 1, &tmp_rval
, whichfork
);
2209 temp
= xfs_bmap_worst_indlen(bma
->ip
, temp
);
2210 temp2
= xfs_bmap_worst_indlen(bma
->ip
, temp2
);
2211 diff
= (int)(temp
+ temp2
-
2212 (startblockval(PREV
.br_startblock
) -
2214 bma
->cur
->bc_private
.b
.allocated
: 0)));
2216 error
= xfs_mod_fdblocks(bma
->ip
->i_mount
,
2217 -((int64_t)diff
), false);
2223 ep
= xfs_iext_get_ext(ifp
, bma
->idx
);
2224 xfs_bmbt_set_startblock(ep
, nullstartblock((int)temp
));
2225 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2226 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
+ 2, state
, _THIS_IP_
);
2227 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp
, bma
->idx
+ 2),
2228 nullstartblock((int)temp2
));
2229 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
+ 2, state
, _THIS_IP_
);
2232 da_new
= temp
+ temp2
;
2235 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2236 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2237 case BMAP_LEFT_FILLING
| BMAP_RIGHT_CONTIG
:
2238 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
2239 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2240 case BMAP_LEFT_CONTIG
:
2241 case BMAP_RIGHT_CONTIG
:
2243 * These cases are all impossible.
2248 /* add reverse mapping */
2249 error
= xfs_rmap_map_extent(mp
, bma
->dfops
, bma
->ip
, whichfork
, new);
2253 /* convert to a btree if necessary */
2254 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
2255 int tmp_logflags
; /* partial log flag return val */
2257 ASSERT(bma
->cur
== NULL
);
2258 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
2259 bma
->firstblock
, bma
->dfops
, &bma
->cur
,
2260 da_old
> 0, &tmp_logflags
, whichfork
);
2261 bma
->logflags
|= tmp_logflags
;
2266 /* adjust for changes in reserved delayed indirect blocks */
2267 if (da_old
|| da_new
) {
2270 temp
+= bma
->cur
->bc_private
.b
.allocated
;
2272 xfs_mod_fdblocks(bma
->ip
->i_mount
,
2273 (int64_t)(da_old
- temp
), false);
2276 /* clear out the allocated field, done with it now in any case. */
2278 bma
->cur
->bc_private
.b
.allocated
= 0;
2280 xfs_bmap_check_leaf_extents(bma
->cur
, bma
->ip
, whichfork
);
2282 if (whichfork
!= XFS_COW_FORK
)
2283 bma
->logflags
|= rval
;
2291 * Convert an unwritten allocation to a real allocation or vice versa.
2293 STATIC
int /* error */
2294 xfs_bmap_add_extent_unwritten_real(
2295 struct xfs_trans
*tp
,
2296 xfs_inode_t
*ip
, /* incore inode pointer */
2298 xfs_extnum_t
*idx
, /* extent number to update/insert */
2299 xfs_btree_cur_t
**curp
, /* if *curp is null, not a btree */
2300 xfs_bmbt_irec_t
*new, /* new data to add to file extents */
2301 xfs_fsblock_t
*first
, /* pointer to firstblock variable */
2302 struct xfs_defer_ops
*dfops
, /* list of extents to be freed */
2303 int *logflagsp
) /* inode logging flags */
2305 xfs_btree_cur_t
*cur
; /* btree cursor */
2306 xfs_bmbt_rec_host_t
*ep
; /* extent entry for idx */
2307 int error
; /* error return value */
2308 int i
; /* temp state */
2309 xfs_ifork_t
*ifp
; /* inode fork pointer */
2310 xfs_fileoff_t new_endoff
; /* end offset of new entry */
2311 xfs_exntst_t newext
; /* new extent state */
2312 xfs_exntst_t oldext
; /* old extent state */
2313 xfs_bmbt_irec_t r
[3]; /* neighbor extent entries */
2314 /* left is 0, right is 1, prev is 2 */
2315 int rval
=0; /* return value (logging flags) */
2316 int state
= 0;/* state bits, accessed thru macros */
2317 struct xfs_mount
*mp
= ip
->i_mount
;
2322 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
2323 if (whichfork
== XFS_COW_FORK
)
2324 state
|= BMAP_COWFORK
;
2327 ASSERT(*idx
<= xfs_iext_count(ifp
));
2328 ASSERT(!isnullstartblock(new->br_startblock
));
2330 XFS_STATS_INC(mp
, xs_add_exlist
);
2337 * Set up a bunch of variables to make the tests simpler.
2340 ep
= xfs_iext_get_ext(ifp
, *idx
);
2341 xfs_bmbt_get_all(ep
, &PREV
);
2342 newext
= new->br_state
;
2343 oldext
= (newext
== XFS_EXT_UNWRITTEN
) ?
2344 XFS_EXT_NORM
: XFS_EXT_UNWRITTEN
;
2345 ASSERT(PREV
.br_state
== oldext
);
2346 new_endoff
= new->br_startoff
+ new->br_blockcount
;
2347 ASSERT(PREV
.br_startoff
<= new->br_startoff
);
2348 ASSERT(PREV
.br_startoff
+ PREV
.br_blockcount
>= new_endoff
);
2351 * Set flags determining what part of the previous oldext allocation
2352 * extent is being replaced by a newext allocation.
2354 if (PREV
.br_startoff
== new->br_startoff
)
2355 state
|= BMAP_LEFT_FILLING
;
2356 if (PREV
.br_startoff
+ PREV
.br_blockcount
== new_endoff
)
2357 state
|= BMAP_RIGHT_FILLING
;
2360 * Check and set flags if this segment has a left neighbor.
2361 * Don't set contiguous if the combined extent would be too large.
2364 state
|= BMAP_LEFT_VALID
;
2365 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, *idx
- 1), &LEFT
);
2367 if (isnullstartblock(LEFT
.br_startblock
))
2368 state
|= BMAP_LEFT_DELAY
;
2371 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
2372 LEFT
.br_startoff
+ LEFT
.br_blockcount
== new->br_startoff
&&
2373 LEFT
.br_startblock
+ LEFT
.br_blockcount
== new->br_startblock
&&
2374 LEFT
.br_state
== newext
&&
2375 LEFT
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
2376 state
|= BMAP_LEFT_CONTIG
;
2379 * Check and set flags if this segment has a right neighbor.
2380 * Don't set contiguous if the combined extent would be too large.
2381 * Also check for all-three-contiguous being too large.
2383 if (*idx
< xfs_iext_count(ifp
) - 1) {
2384 state
|= BMAP_RIGHT_VALID
;
2385 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, *idx
+ 1), &RIGHT
);
2386 if (isnullstartblock(RIGHT
.br_startblock
))
2387 state
|= BMAP_RIGHT_DELAY
;
2390 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
2391 new_endoff
== RIGHT
.br_startoff
&&
2392 new->br_startblock
+ new->br_blockcount
== RIGHT
.br_startblock
&&
2393 newext
== RIGHT
.br_state
&&
2394 new->br_blockcount
+ RIGHT
.br_blockcount
<= MAXEXTLEN
&&
2395 ((state
& (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
2396 BMAP_RIGHT_FILLING
)) !=
2397 (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
2398 BMAP_RIGHT_FILLING
) ||
2399 LEFT
.br_blockcount
+ new->br_blockcount
+ RIGHT
.br_blockcount
2401 state
|= BMAP_RIGHT_CONTIG
;
2404 * Switch out based on the FILLING and CONTIG state bits.
2406 switch (state
& (BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
2407 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
)) {
2408 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
2409 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2411 * Setting all of a previous oldext extent to newext.
2412 * The left and right neighbors are both contiguous with new.
2416 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2417 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
),
2418 LEFT
.br_blockcount
+ PREV
.br_blockcount
+
2419 RIGHT
.br_blockcount
);
2420 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2422 xfs_iext_remove(ip
, *idx
+ 1, 2, state
);
2423 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2424 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 2);
2426 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2428 rval
= XFS_ILOG_CORE
;
2429 if ((error
= xfs_bmbt_lookup_eq(cur
, RIGHT
.br_startoff
,
2430 RIGHT
.br_startblock
,
2431 RIGHT
.br_blockcount
, &i
)))
2433 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2434 if ((error
= xfs_btree_delete(cur
, &i
)))
2436 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2437 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2439 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2440 if ((error
= xfs_btree_delete(cur
, &i
)))
2442 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2443 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2445 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2446 if ((error
= xfs_bmbt_update(cur
, LEFT
.br_startoff
,
2448 LEFT
.br_blockcount
+ PREV
.br_blockcount
+
2449 RIGHT
.br_blockcount
, LEFT
.br_state
)))
2454 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
2456 * Setting all of a previous oldext extent to newext.
2457 * The left neighbor is contiguous, the right is not.
2461 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2462 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
),
2463 LEFT
.br_blockcount
+ PREV
.br_blockcount
);
2464 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2466 xfs_iext_remove(ip
, *idx
+ 1, 1, state
);
2467 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2468 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
2470 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2472 rval
= XFS_ILOG_CORE
;
2473 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2474 PREV
.br_startblock
, PREV
.br_blockcount
,
2477 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2478 if ((error
= xfs_btree_delete(cur
, &i
)))
2480 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2481 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2483 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2484 if ((error
= xfs_bmbt_update(cur
, LEFT
.br_startoff
,
2486 LEFT
.br_blockcount
+ PREV
.br_blockcount
,
2492 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2494 * Setting all of a previous oldext extent to newext.
2495 * The right neighbor is contiguous, the left is not.
2497 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2498 xfs_bmbt_set_blockcount(ep
,
2499 PREV
.br_blockcount
+ RIGHT
.br_blockcount
);
2500 xfs_bmbt_set_state(ep
, newext
);
2501 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2502 xfs_iext_remove(ip
, *idx
+ 1, 1, state
);
2503 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2504 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
2506 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2508 rval
= XFS_ILOG_CORE
;
2509 if ((error
= xfs_bmbt_lookup_eq(cur
, RIGHT
.br_startoff
,
2510 RIGHT
.br_startblock
,
2511 RIGHT
.br_blockcount
, &i
)))
2513 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2514 if ((error
= xfs_btree_delete(cur
, &i
)))
2516 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2517 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2519 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2520 if ((error
= xfs_bmbt_update(cur
, new->br_startoff
,
2522 new->br_blockcount
+ RIGHT
.br_blockcount
,
2528 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
2530 * Setting all of a previous oldext extent to newext.
2531 * Neither the left nor right neighbors are contiguous with
2534 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2535 xfs_bmbt_set_state(ep
, newext
);
2536 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2539 rval
= XFS_ILOG_DEXT
;
2542 if ((error
= xfs_bmbt_lookup_eq(cur
, new->br_startoff
,
2543 new->br_startblock
, new->br_blockcount
,
2546 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2547 if ((error
= xfs_bmbt_update(cur
, new->br_startoff
,
2548 new->br_startblock
, new->br_blockcount
,
2554 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
:
2556 * Setting the first part of a previous oldext extent to newext.
2557 * The left neighbor is contiguous.
2559 trace_xfs_bmap_pre_update(ip
, *idx
- 1, state
, _THIS_IP_
);
2560 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
- 1),
2561 LEFT
.br_blockcount
+ new->br_blockcount
);
2562 xfs_bmbt_set_startoff(ep
,
2563 PREV
.br_startoff
+ new->br_blockcount
);
2564 trace_xfs_bmap_post_update(ip
, *idx
- 1, state
, _THIS_IP_
);
2566 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2567 xfs_bmbt_set_startblock(ep
,
2568 new->br_startblock
+ new->br_blockcount
);
2569 xfs_bmbt_set_blockcount(ep
,
2570 PREV
.br_blockcount
- new->br_blockcount
);
2571 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2576 rval
= XFS_ILOG_DEXT
;
2579 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2580 PREV
.br_startblock
, PREV
.br_blockcount
,
2583 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2584 if ((error
= xfs_bmbt_update(cur
,
2585 PREV
.br_startoff
+ new->br_blockcount
,
2586 PREV
.br_startblock
+ new->br_blockcount
,
2587 PREV
.br_blockcount
- new->br_blockcount
,
2590 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2592 error
= xfs_bmbt_update(cur
, LEFT
.br_startoff
,
2594 LEFT
.br_blockcount
+ new->br_blockcount
,
2601 case BMAP_LEFT_FILLING
:
2603 * Setting the first part of a previous oldext extent to newext.
2604 * The left neighbor is not contiguous.
2606 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2607 ASSERT(ep
&& xfs_bmbt_get_state(ep
) == oldext
);
2608 xfs_bmbt_set_startoff(ep
, new_endoff
);
2609 xfs_bmbt_set_blockcount(ep
,
2610 PREV
.br_blockcount
- new->br_blockcount
);
2611 xfs_bmbt_set_startblock(ep
,
2612 new->br_startblock
+ new->br_blockcount
);
2613 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2615 xfs_iext_insert(ip
, *idx
, 1, new, state
);
2616 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2617 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
2619 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2621 rval
= XFS_ILOG_CORE
;
2622 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2623 PREV
.br_startblock
, PREV
.br_blockcount
,
2626 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2627 if ((error
= xfs_bmbt_update(cur
,
2628 PREV
.br_startoff
+ new->br_blockcount
,
2629 PREV
.br_startblock
+ new->br_blockcount
,
2630 PREV
.br_blockcount
- new->br_blockcount
,
2633 cur
->bc_rec
.b
= *new;
2634 if ((error
= xfs_btree_insert(cur
, &i
)))
2636 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2640 case BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2642 * Setting the last part of a previous oldext extent to newext.
2643 * The right neighbor is contiguous with the new allocation.
2645 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2646 xfs_bmbt_set_blockcount(ep
,
2647 PREV
.br_blockcount
- new->br_blockcount
);
2648 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2652 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2653 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp
, *idx
),
2654 new->br_startoff
, new->br_startblock
,
2655 new->br_blockcount
+ RIGHT
.br_blockcount
, newext
);
2656 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2659 rval
= XFS_ILOG_DEXT
;
2662 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2664 PREV
.br_blockcount
, &i
)))
2666 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2667 if ((error
= xfs_bmbt_update(cur
, PREV
.br_startoff
,
2669 PREV
.br_blockcount
- new->br_blockcount
,
2672 if ((error
= xfs_btree_increment(cur
, 0, &i
)))
2674 if ((error
= xfs_bmbt_update(cur
, new->br_startoff
,
2676 new->br_blockcount
+ RIGHT
.br_blockcount
,
2682 case BMAP_RIGHT_FILLING
:
2684 * Setting the last part of a previous oldext extent to newext.
2685 * The right neighbor is not contiguous.
2687 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2688 xfs_bmbt_set_blockcount(ep
,
2689 PREV
.br_blockcount
- new->br_blockcount
);
2690 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2693 xfs_iext_insert(ip
, *idx
, 1, new, state
);
2695 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2696 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
2698 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2700 rval
= XFS_ILOG_CORE
;
2701 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2702 PREV
.br_startblock
, PREV
.br_blockcount
,
2705 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2706 if ((error
= xfs_bmbt_update(cur
, PREV
.br_startoff
,
2708 PREV
.br_blockcount
- new->br_blockcount
,
2711 if ((error
= xfs_bmbt_lookup_eq(cur
, new->br_startoff
,
2712 new->br_startblock
, new->br_blockcount
,
2715 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
2716 cur
->bc_rec
.b
.br_state
= new->br_state
;
2717 if ((error
= xfs_btree_insert(cur
, &i
)))
2719 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2725 * Setting the middle part of a previous oldext extent to
2726 * newext. Contiguity is impossible here.
2727 * One extent becomes three extents.
2729 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2730 xfs_bmbt_set_blockcount(ep
,
2731 new->br_startoff
- PREV
.br_startoff
);
2732 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2735 r
[1].br_startoff
= new_endoff
;
2736 r
[1].br_blockcount
=
2737 PREV
.br_startoff
+ PREV
.br_blockcount
- new_endoff
;
2738 r
[1].br_startblock
= new->br_startblock
+ new->br_blockcount
;
2739 r
[1].br_state
= oldext
;
2742 xfs_iext_insert(ip
, *idx
, 2, &r
[0], state
);
2744 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2745 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 2);
2747 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2749 rval
= XFS_ILOG_CORE
;
2750 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2751 PREV
.br_startblock
, PREV
.br_blockcount
,
2754 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2755 /* new right extent - oldext */
2756 if ((error
= xfs_bmbt_update(cur
, r
[1].br_startoff
,
2757 r
[1].br_startblock
, r
[1].br_blockcount
,
2760 /* new left extent - oldext */
2761 cur
->bc_rec
.b
= PREV
;
2762 cur
->bc_rec
.b
.br_blockcount
=
2763 new->br_startoff
- PREV
.br_startoff
;
2764 if ((error
= xfs_btree_insert(cur
, &i
)))
2766 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2768 * Reset the cursor to the position of the new extent
2769 * we are about to insert as we can't trust it after
2770 * the previous insert.
2772 if ((error
= xfs_bmbt_lookup_eq(cur
, new->br_startoff
,
2773 new->br_startblock
, new->br_blockcount
,
2776 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
2777 /* new middle extent - newext */
2778 cur
->bc_rec
.b
.br_state
= new->br_state
;
2779 if ((error
= xfs_btree_insert(cur
, &i
)))
2781 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2785 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2786 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2787 case BMAP_LEFT_FILLING
| BMAP_RIGHT_CONTIG
:
2788 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
2789 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2790 case BMAP_LEFT_CONTIG
:
2791 case BMAP_RIGHT_CONTIG
:
2793 * These cases are all impossible.
2798 /* update reverse mappings */
2799 error
= xfs_rmap_convert_extent(mp
, dfops
, ip
, whichfork
, new);
2803 /* convert to a btree if necessary */
2804 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
2805 int tmp_logflags
; /* partial log flag return val */
2807 ASSERT(cur
== NULL
);
2808 error
= xfs_bmap_extents_to_btree(tp
, ip
, first
, dfops
, &cur
,
2809 0, &tmp_logflags
, whichfork
);
2810 *logflagsp
|= tmp_logflags
;
2815 /* clear out the allocated field, done with it now in any case. */
2817 cur
->bc_private
.b
.allocated
= 0;
2821 xfs_bmap_check_leaf_extents(*curp
, ip
, whichfork
);
2831 * Convert a hole to a delayed allocation.
2834 xfs_bmap_add_extent_hole_delay(
2835 xfs_inode_t
*ip
, /* incore inode pointer */
2837 xfs_extnum_t
*idx
, /* extent number to update/insert */
2838 xfs_bmbt_irec_t
*new) /* new data to add to file extents */
2840 xfs_ifork_t
*ifp
; /* inode fork pointer */
2841 xfs_bmbt_irec_t left
; /* left neighbor extent entry */
2842 xfs_filblks_t newlen
=0; /* new indirect size */
2843 xfs_filblks_t oldlen
=0; /* old indirect size */
2844 xfs_bmbt_irec_t right
; /* right neighbor extent entry */
2845 int state
; /* state bits, accessed thru macros */
2846 xfs_filblks_t temp
=0; /* temp for indirect calculations */
2848 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
2850 if (whichfork
== XFS_COW_FORK
)
2851 state
|= BMAP_COWFORK
;
2852 ASSERT(isnullstartblock(new->br_startblock
));
2855 * Check and set flags if this segment has a left neighbor
2858 state
|= BMAP_LEFT_VALID
;
2859 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, *idx
- 1), &left
);
2861 if (isnullstartblock(left
.br_startblock
))
2862 state
|= BMAP_LEFT_DELAY
;
2866 * Check and set flags if the current (right) segment exists.
2867 * If it doesn't exist, we're converting the hole at end-of-file.
2869 if (*idx
< xfs_iext_count(ifp
)) {
2870 state
|= BMAP_RIGHT_VALID
;
2871 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, *idx
), &right
);
2873 if (isnullstartblock(right
.br_startblock
))
2874 state
|= BMAP_RIGHT_DELAY
;
2878 * Set contiguity flags on the left and right neighbors.
2879 * Don't let extents get too large, even if the pieces are contiguous.
2881 if ((state
& BMAP_LEFT_VALID
) && (state
& BMAP_LEFT_DELAY
) &&
2882 left
.br_startoff
+ left
.br_blockcount
== new->br_startoff
&&
2883 left
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
2884 state
|= BMAP_LEFT_CONTIG
;
2886 if ((state
& BMAP_RIGHT_VALID
) && (state
& BMAP_RIGHT_DELAY
) &&
2887 new->br_startoff
+ new->br_blockcount
== right
.br_startoff
&&
2888 new->br_blockcount
+ right
.br_blockcount
<= MAXEXTLEN
&&
2889 (!(state
& BMAP_LEFT_CONTIG
) ||
2890 (left
.br_blockcount
+ new->br_blockcount
+
2891 right
.br_blockcount
<= MAXEXTLEN
)))
2892 state
|= BMAP_RIGHT_CONTIG
;
2895 * Switch out based on the contiguity flags.
2897 switch (state
& (BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
)) {
2898 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2900 * New allocation is contiguous with delayed allocations
2901 * on the left and on the right.
2902 * Merge all three into a single extent record.
2905 temp
= left
.br_blockcount
+ new->br_blockcount
+
2906 right
.br_blockcount
;
2908 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2909 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
), temp
);
2910 oldlen
= startblockval(left
.br_startblock
) +
2911 startblockval(new->br_startblock
) +
2912 startblockval(right
.br_startblock
);
2913 newlen
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
2915 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp
, *idx
),
2916 nullstartblock((int)newlen
));
2917 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2919 xfs_iext_remove(ip
, *idx
+ 1, 1, state
);
2922 case BMAP_LEFT_CONTIG
:
2924 * New allocation is contiguous with a delayed allocation
2926 * Merge the new allocation with the left neighbor.
2929 temp
= left
.br_blockcount
+ new->br_blockcount
;
2931 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2932 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
), temp
);
2933 oldlen
= startblockval(left
.br_startblock
) +
2934 startblockval(new->br_startblock
);
2935 newlen
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
2937 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp
, *idx
),
2938 nullstartblock((int)newlen
));
2939 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2942 case BMAP_RIGHT_CONTIG
:
2944 * New allocation is contiguous with a delayed allocation
2946 * Merge the new allocation with the right neighbor.
2948 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2949 temp
= new->br_blockcount
+ right
.br_blockcount
;
2950 oldlen
= startblockval(new->br_startblock
) +
2951 startblockval(right
.br_startblock
);
2952 newlen
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
2954 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp
, *idx
),
2956 nullstartblock((int)newlen
), temp
, right
.br_state
);
2957 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2962 * New allocation is not contiguous with another
2963 * delayed allocation.
2964 * Insert a new entry.
2966 oldlen
= newlen
= 0;
2967 xfs_iext_insert(ip
, *idx
, 1, new, state
);
2970 if (oldlen
!= newlen
) {
2971 ASSERT(oldlen
> newlen
);
2972 xfs_mod_fdblocks(ip
->i_mount
, (int64_t)(oldlen
- newlen
),
2975 * Nothing to do for disk quota accounting here.
2981 * Convert a hole to a real allocation.
2983 STATIC
int /* error */
2984 xfs_bmap_add_extent_hole_real(
2985 struct xfs_bmalloca
*bma
,
2988 struct xfs_bmbt_irec
*new = &bma
->got
;
2989 int error
; /* error return value */
2990 int i
; /* temp state */
2991 xfs_ifork_t
*ifp
; /* inode fork pointer */
2992 xfs_bmbt_irec_t left
; /* left neighbor extent entry */
2993 xfs_bmbt_irec_t right
; /* right neighbor extent entry */
2994 int rval
=0; /* return value (logging flags) */
2995 int state
; /* state bits, accessed thru macros */
2996 struct xfs_mount
*mp
;
2998 mp
= bma
->ip
->i_mount
;
2999 ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
3001 ASSERT(bma
->idx
>= 0);
3002 ASSERT(bma
->idx
<= xfs_iext_count(ifp
));
3003 ASSERT(!isnullstartblock(new->br_startblock
));
3005 !(bma
->cur
->bc_private
.b
.flags
& XFS_BTCUR_BPRV_WASDEL
));
3006 ASSERT(whichfork
!= XFS_COW_FORK
);
3008 XFS_STATS_INC(mp
, xs_add_exlist
);
3011 if (whichfork
== XFS_ATTR_FORK
)
3012 state
|= BMAP_ATTRFORK
;
3015 * Check and set flags if this segment has a left neighbor.
3018 state
|= BMAP_LEFT_VALID
;
3019 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
- 1), &left
);
3020 if (isnullstartblock(left
.br_startblock
))
3021 state
|= BMAP_LEFT_DELAY
;
3025 * Check and set flags if this segment has a current value.
3026 * Not true if we're inserting into the "hole" at eof.
3028 if (bma
->idx
< xfs_iext_count(ifp
)) {
3029 state
|= BMAP_RIGHT_VALID
;
3030 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
), &right
);
3031 if (isnullstartblock(right
.br_startblock
))
3032 state
|= BMAP_RIGHT_DELAY
;
3036 * We're inserting a real allocation between "left" and "right".
3037 * Set the contiguity flags. Don't let extents get too large.
3039 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
3040 left
.br_startoff
+ left
.br_blockcount
== new->br_startoff
&&
3041 left
.br_startblock
+ left
.br_blockcount
== new->br_startblock
&&
3042 left
.br_state
== new->br_state
&&
3043 left
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
3044 state
|= BMAP_LEFT_CONTIG
;
3046 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
3047 new->br_startoff
+ new->br_blockcount
== right
.br_startoff
&&
3048 new->br_startblock
+ new->br_blockcount
== right
.br_startblock
&&
3049 new->br_state
== right
.br_state
&&
3050 new->br_blockcount
+ right
.br_blockcount
<= MAXEXTLEN
&&
3051 (!(state
& BMAP_LEFT_CONTIG
) ||
3052 left
.br_blockcount
+ new->br_blockcount
+
3053 right
.br_blockcount
<= MAXEXTLEN
))
3054 state
|= BMAP_RIGHT_CONTIG
;
3058 * Select which case we're in here, and implement it.
3060 switch (state
& (BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
)) {
3061 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
3063 * New allocation is contiguous with real allocations on the
3064 * left and on the right.
3065 * Merge all three into a single extent record.
3068 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
3069 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, bma
->idx
),
3070 left
.br_blockcount
+ new->br_blockcount
+
3071 right
.br_blockcount
);
3072 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
3074 xfs_iext_remove(bma
->ip
, bma
->idx
+ 1, 1, state
);
3076 XFS_IFORK_NEXT_SET(bma
->ip
, whichfork
,
3077 XFS_IFORK_NEXTENTS(bma
->ip
, whichfork
) - 1);
3078 if (bma
->cur
== NULL
) {
3079 rval
= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
3081 rval
= XFS_ILOG_CORE
;
3082 error
= xfs_bmbt_lookup_eq(bma
->cur
, right
.br_startoff
,
3083 right
.br_startblock
, right
.br_blockcount
,
3087 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
3088 error
= xfs_btree_delete(bma
->cur
, &i
);
3091 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
3092 error
= xfs_btree_decrement(bma
->cur
, 0, &i
);
3095 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
3096 error
= xfs_bmbt_update(bma
->cur
, left
.br_startoff
,
3098 left
.br_blockcount
+
3099 new->br_blockcount
+
3100 right
.br_blockcount
,
3107 case BMAP_LEFT_CONTIG
:
3109 * New allocation is contiguous with a real allocation
3111 * Merge the new allocation with the left neighbor.
3114 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
3115 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, bma
->idx
),
3116 left
.br_blockcount
+ new->br_blockcount
);
3117 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
3119 if (bma
->cur
== NULL
) {
3120 rval
= xfs_ilog_fext(whichfork
);
3123 error
= xfs_bmbt_lookup_eq(bma
->cur
, left
.br_startoff
,
3124 left
.br_startblock
, left
.br_blockcount
,
3128 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
3129 error
= xfs_bmbt_update(bma
->cur
, left
.br_startoff
,
3131 left
.br_blockcount
+
3139 case BMAP_RIGHT_CONTIG
:
3141 * New allocation is contiguous with a real allocation
3143 * Merge the new allocation with the right neighbor.
3145 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
3146 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp
, bma
->idx
),
3147 new->br_startoff
, new->br_startblock
,
3148 new->br_blockcount
+ right
.br_blockcount
,
3150 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
3152 if (bma
->cur
== NULL
) {
3153 rval
= xfs_ilog_fext(whichfork
);
3156 error
= xfs_bmbt_lookup_eq(bma
->cur
,
3158 right
.br_startblock
,
3159 right
.br_blockcount
, &i
);
3162 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
3163 error
= xfs_bmbt_update(bma
->cur
, new->br_startoff
,
3165 new->br_blockcount
+
3166 right
.br_blockcount
,
3175 * New allocation is not contiguous with another
3177 * Insert a new entry.
3179 xfs_iext_insert(bma
->ip
, bma
->idx
, 1, new, state
);
3180 XFS_IFORK_NEXT_SET(bma
->ip
, whichfork
,
3181 XFS_IFORK_NEXTENTS(bma
->ip
, whichfork
) + 1);
3182 if (bma
->cur
== NULL
) {
3183 rval
= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
3185 rval
= XFS_ILOG_CORE
;
3186 error
= xfs_bmbt_lookup_eq(bma
->cur
,
3189 new->br_blockcount
, &i
);
3192 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
3193 bma
->cur
->bc_rec
.b
.br_state
= new->br_state
;
3194 error
= xfs_btree_insert(bma
->cur
, &i
);
3197 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
3202 /* add reverse mapping */
3203 error
= xfs_rmap_map_extent(mp
, bma
->dfops
, bma
->ip
, whichfork
, new);
3207 /* convert to a btree if necessary */
3208 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
3209 int tmp_logflags
; /* partial log flag return val */
3211 ASSERT(bma
->cur
== NULL
);
3212 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
3213 bma
->firstblock
, bma
->dfops
, &bma
->cur
,
3214 0, &tmp_logflags
, whichfork
);
3215 bma
->logflags
|= tmp_logflags
;
3220 /* clear out the allocated field, done with it now in any case. */
3222 bma
->cur
->bc_private
.b
.allocated
= 0;
3224 xfs_bmap_check_leaf_extents(bma
->cur
, bma
->ip
, whichfork
);
3226 bma
->logflags
|= rval
;
3231 * Functions used in the extent read, allocate and remove paths
3235 * Adjust the size of the new extent based on di_extsize and rt extsize.
3238 xfs_bmap_extsize_align(
3240 xfs_bmbt_irec_t
*gotp
, /* next extent pointer */
3241 xfs_bmbt_irec_t
*prevp
, /* previous extent pointer */
3242 xfs_extlen_t extsz
, /* align to this extent size */
3243 int rt
, /* is this a realtime inode? */
3244 int eof
, /* is extent at end-of-file? */
3245 int delay
, /* creating delalloc extent? */
3246 int convert
, /* overwriting unwritten extent? */
3247 xfs_fileoff_t
*offp
, /* in/out: aligned offset */
3248 xfs_extlen_t
*lenp
) /* in/out: aligned length */
3250 xfs_fileoff_t orig_off
; /* original offset */
3251 xfs_extlen_t orig_alen
; /* original length */
3252 xfs_fileoff_t orig_end
; /* original off+len */
3253 xfs_fileoff_t nexto
; /* next file offset */
3254 xfs_fileoff_t prevo
; /* previous file offset */
3255 xfs_fileoff_t align_off
; /* temp for offset */
3256 xfs_extlen_t align_alen
; /* temp for length */
3257 xfs_extlen_t temp
; /* temp for calculations */
3262 orig_off
= align_off
= *offp
;
3263 orig_alen
= align_alen
= *lenp
;
3264 orig_end
= orig_off
+ orig_alen
;
3267 * If this request overlaps an existing extent, then don't
3268 * attempt to perform any additional alignment.
3270 if (!delay
&& !eof
&&
3271 (orig_off
>= gotp
->br_startoff
) &&
3272 (orig_end
<= gotp
->br_startoff
+ gotp
->br_blockcount
)) {
3277 * If the file offset is unaligned vs. the extent size
3278 * we need to align it. This will be possible unless
3279 * the file was previously written with a kernel that didn't
3280 * perform this alignment, or if a truncate shot us in the
3283 temp
= do_mod(orig_off
, extsz
);
3289 /* Same adjustment for the end of the requested area. */
3290 temp
= (align_alen
% extsz
);
3292 align_alen
+= extsz
- temp
;
3295 * For large extent hint sizes, the aligned extent might be larger than
3296 * MAXEXTLEN. In that case, reduce the size by an extsz so that it pulls
3297 * the length back under MAXEXTLEN. The outer allocation loops handle
3298 * short allocation just fine, so it is safe to do this. We only want to
3299 * do it when we are forced to, though, because it means more allocation
3300 * operations are required.
3302 while (align_alen
> MAXEXTLEN
)
3303 align_alen
-= extsz
;
3304 ASSERT(align_alen
<= MAXEXTLEN
);
3307 * If the previous block overlaps with this proposed allocation
3308 * then move the start forward without adjusting the length.
3310 if (prevp
->br_startoff
!= NULLFILEOFF
) {
3311 if (prevp
->br_startblock
== HOLESTARTBLOCK
)
3312 prevo
= prevp
->br_startoff
;
3314 prevo
= prevp
->br_startoff
+ prevp
->br_blockcount
;
3317 if (align_off
!= orig_off
&& align_off
< prevo
)
3320 * If the next block overlaps with this proposed allocation
3321 * then move the start back without adjusting the length,
3322 * but not before offset 0.
3323 * This may of course make the start overlap previous block,
3324 * and if we hit the offset 0 limit then the next block
3325 * can still overlap too.
3327 if (!eof
&& gotp
->br_startoff
!= NULLFILEOFF
) {
3328 if ((delay
&& gotp
->br_startblock
== HOLESTARTBLOCK
) ||
3329 (!delay
&& gotp
->br_startblock
== DELAYSTARTBLOCK
))
3330 nexto
= gotp
->br_startoff
+ gotp
->br_blockcount
;
3332 nexto
= gotp
->br_startoff
;
3334 nexto
= NULLFILEOFF
;
3336 align_off
+ align_alen
!= orig_end
&&
3337 align_off
+ align_alen
> nexto
)
3338 align_off
= nexto
> align_alen
? nexto
- align_alen
: 0;
3340 * If we're now overlapping the next or previous extent that
3341 * means we can't fit an extsz piece in this hole. Just move
3342 * the start forward to the first valid spot and set
3343 * the length so we hit the end.
3345 if (align_off
!= orig_off
&& align_off
< prevo
)
3347 if (align_off
+ align_alen
!= orig_end
&&
3348 align_off
+ align_alen
> nexto
&&
3349 nexto
!= NULLFILEOFF
) {
3350 ASSERT(nexto
> prevo
);
3351 align_alen
= nexto
- align_off
;
3355 * If realtime, and the result isn't a multiple of the realtime
3356 * extent size we need to remove blocks until it is.
3358 if (rt
&& (temp
= (align_alen
% mp
->m_sb
.sb_rextsize
))) {
3360 * We're not covering the original request, or
3361 * we won't be able to once we fix the length.
3363 if (orig_off
< align_off
||
3364 orig_end
> align_off
+ align_alen
||
3365 align_alen
- temp
< orig_alen
)
3368 * Try to fix it by moving the start up.
3370 if (align_off
+ temp
<= orig_off
) {
3375 * Try to fix it by moving the end in.
3377 else if (align_off
+ align_alen
- temp
>= orig_end
)
3380 * Set the start to the minimum then trim the length.
3383 align_alen
-= orig_off
- align_off
;
3384 align_off
= orig_off
;
3385 align_alen
-= align_alen
% mp
->m_sb
.sb_rextsize
;
3388 * Result doesn't cover the request, fail it.
3390 if (orig_off
< align_off
|| orig_end
> align_off
+ align_alen
)
3393 ASSERT(orig_off
>= align_off
);
3394 /* see MAXEXTLEN handling above */
3395 ASSERT(orig_end
<= align_off
+ align_alen
||
3396 align_alen
+ extsz
> MAXEXTLEN
);
3400 if (!eof
&& gotp
->br_startoff
!= NULLFILEOFF
)
3401 ASSERT(align_off
+ align_alen
<= gotp
->br_startoff
);
3402 if (prevp
->br_startoff
!= NULLFILEOFF
)
3403 ASSERT(align_off
>= prevp
->br_startoff
+ prevp
->br_blockcount
);
3411 #define XFS_ALLOC_GAP_UNITS 4
3415 struct xfs_bmalloca
*ap
) /* bmap alloc argument struct */
3417 xfs_fsblock_t adjust
; /* adjustment to block numbers */
3418 xfs_agnumber_t fb_agno
; /* ag number of ap->firstblock */
3419 xfs_mount_t
*mp
; /* mount point structure */
3420 int nullfb
; /* true if ap->firstblock isn't set */
3421 int rt
; /* true if inode is realtime */
3423 #define ISVALID(x,y) \
3425 (x) < mp->m_sb.sb_rblocks : \
3426 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
3427 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
3428 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
3430 mp
= ap
->ip
->i_mount
;
3431 nullfb
= *ap
->firstblock
== NULLFSBLOCK
;
3432 rt
= XFS_IS_REALTIME_INODE(ap
->ip
) &&
3433 xfs_alloc_is_userdata(ap
->datatype
);
3434 fb_agno
= nullfb
? NULLAGNUMBER
: XFS_FSB_TO_AGNO(mp
, *ap
->firstblock
);
3436 * If allocating at eof, and there's a previous real block,
3437 * try to use its last block as our starting point.
3439 if (ap
->eof
&& ap
->prev
.br_startoff
!= NULLFILEOFF
&&
3440 !isnullstartblock(ap
->prev
.br_startblock
) &&
3441 ISVALID(ap
->prev
.br_startblock
+ ap
->prev
.br_blockcount
,
3442 ap
->prev
.br_startblock
)) {
3443 ap
->blkno
= ap
->prev
.br_startblock
+ ap
->prev
.br_blockcount
;
3445 * Adjust for the gap between prevp and us.
3447 adjust
= ap
->offset
-
3448 (ap
->prev
.br_startoff
+ ap
->prev
.br_blockcount
);
3450 ISVALID(ap
->blkno
+ adjust
, ap
->prev
.br_startblock
))
3451 ap
->blkno
+= adjust
;
3454 * If not at eof, then compare the two neighbor blocks.
3455 * Figure out whether either one gives us a good starting point,
3456 * and pick the better one.
3458 else if (!ap
->eof
) {
3459 xfs_fsblock_t gotbno
; /* right side block number */
3460 xfs_fsblock_t gotdiff
=0; /* right side difference */
3461 xfs_fsblock_t prevbno
; /* left side block number */
3462 xfs_fsblock_t prevdiff
=0; /* left side difference */
3465 * If there's a previous (left) block, select a requested
3466 * start block based on it.
3468 if (ap
->prev
.br_startoff
!= NULLFILEOFF
&&
3469 !isnullstartblock(ap
->prev
.br_startblock
) &&
3470 (prevbno
= ap
->prev
.br_startblock
+
3471 ap
->prev
.br_blockcount
) &&
3472 ISVALID(prevbno
, ap
->prev
.br_startblock
)) {
3474 * Calculate gap to end of previous block.
3476 adjust
= prevdiff
= ap
->offset
-
3477 (ap
->prev
.br_startoff
+
3478 ap
->prev
.br_blockcount
);
3480 * Figure the startblock based on the previous block's
3481 * end and the gap size.
3483 * If the gap is large relative to the piece we're
3484 * allocating, or using it gives us an invalid block
3485 * number, then just use the end of the previous block.
3487 if (prevdiff
<= XFS_ALLOC_GAP_UNITS
* ap
->length
&&
3488 ISVALID(prevbno
+ prevdiff
,
3489 ap
->prev
.br_startblock
))
3494 * If the firstblock forbids it, can't use it,
3497 if (!rt
&& !nullfb
&&
3498 XFS_FSB_TO_AGNO(mp
, prevbno
) != fb_agno
)
3499 prevbno
= NULLFSBLOCK
;
3502 * No previous block or can't follow it, just default.
3505 prevbno
= NULLFSBLOCK
;
3507 * If there's a following (right) block, select a requested
3508 * start block based on it.
3510 if (!isnullstartblock(ap
->got
.br_startblock
)) {
3512 * Calculate gap to start of next block.
3514 adjust
= gotdiff
= ap
->got
.br_startoff
- ap
->offset
;
3516 * Figure the startblock based on the next block's
3517 * start and the gap size.
3519 gotbno
= ap
->got
.br_startblock
;
3522 * If the gap is large relative to the piece we're
3523 * allocating, or using it gives us an invalid block
3524 * number, then just use the start of the next block
3525 * offset by our length.
3527 if (gotdiff
<= XFS_ALLOC_GAP_UNITS
* ap
->length
&&
3528 ISVALID(gotbno
- gotdiff
, gotbno
))
3530 else if (ISVALID(gotbno
- ap
->length
, gotbno
)) {
3531 gotbno
-= ap
->length
;
3532 gotdiff
+= adjust
- ap
->length
;
3536 * If the firstblock forbids it, can't use it,
3539 if (!rt
&& !nullfb
&&
3540 XFS_FSB_TO_AGNO(mp
, gotbno
) != fb_agno
)
3541 gotbno
= NULLFSBLOCK
;
3544 * No next block, just default.
3547 gotbno
= NULLFSBLOCK
;
3549 * If both valid, pick the better one, else the only good
3550 * one, else ap->blkno is already set (to 0 or the inode block).
3552 if (prevbno
!= NULLFSBLOCK
&& gotbno
!= NULLFSBLOCK
)
3553 ap
->blkno
= prevdiff
<= gotdiff
? prevbno
: gotbno
;
3554 else if (prevbno
!= NULLFSBLOCK
)
3555 ap
->blkno
= prevbno
;
3556 else if (gotbno
!= NULLFSBLOCK
)
3563 xfs_bmap_longest_free_extent(
3564 struct xfs_trans
*tp
,
3569 struct xfs_mount
*mp
= tp
->t_mountp
;
3570 struct xfs_perag
*pag
;
3571 xfs_extlen_t longest
;
3574 pag
= xfs_perag_get(mp
, ag
);
3575 if (!pag
->pagf_init
) {
3576 error
= xfs_alloc_pagf_init(mp
, tp
, ag
, XFS_ALLOC_FLAG_TRYLOCK
);
3580 if (!pag
->pagf_init
) {
3586 longest
= xfs_alloc_longest_free_extent(mp
, pag
,
3587 xfs_alloc_min_freelist(mp
, pag
),
3588 xfs_ag_resv_needed(pag
, XFS_AG_RESV_NONE
));
3589 if (*blen
< longest
)
3598 xfs_bmap_select_minlen(
3599 struct xfs_bmalloca
*ap
,
3600 struct xfs_alloc_arg
*args
,
3604 if (notinit
|| *blen
< ap
->minlen
) {
3606 * Since we did a BUF_TRYLOCK above, it is possible that
3607 * there is space for this request.
3609 args
->minlen
= ap
->minlen
;
3610 } else if (*blen
< args
->maxlen
) {
3612 * If the best seen length is less than the request length,
3613 * use the best as the minimum.
3615 args
->minlen
= *blen
;
3618 * Otherwise we've seen an extent as big as maxlen, use that
3621 args
->minlen
= args
->maxlen
;
3626 xfs_bmap_btalloc_nullfb(
3627 struct xfs_bmalloca
*ap
,
3628 struct xfs_alloc_arg
*args
,
3631 struct xfs_mount
*mp
= ap
->ip
->i_mount
;
3632 xfs_agnumber_t ag
, startag
;
3636 args
->type
= XFS_ALLOCTYPE_START_BNO
;
3637 args
->total
= ap
->total
;
3639 startag
= ag
= XFS_FSB_TO_AGNO(mp
, args
->fsbno
);
3640 if (startag
== NULLAGNUMBER
)
3643 while (*blen
< args
->maxlen
) {
3644 error
= xfs_bmap_longest_free_extent(args
->tp
, ag
, blen
,
3649 if (++ag
== mp
->m_sb
.sb_agcount
)
3655 xfs_bmap_select_minlen(ap
, args
, blen
, notinit
);
3660 xfs_bmap_btalloc_filestreams(
3661 struct xfs_bmalloca
*ap
,
3662 struct xfs_alloc_arg
*args
,
3665 struct xfs_mount
*mp
= ap
->ip
->i_mount
;
3670 args
->type
= XFS_ALLOCTYPE_NEAR_BNO
;
3671 args
->total
= ap
->total
;
3673 ag
= XFS_FSB_TO_AGNO(mp
, args
->fsbno
);
3674 if (ag
== NULLAGNUMBER
)
3677 error
= xfs_bmap_longest_free_extent(args
->tp
, ag
, blen
, ¬init
);
3681 if (*blen
< args
->maxlen
) {
3682 error
= xfs_filestream_new_ag(ap
, &ag
);
3686 error
= xfs_bmap_longest_free_extent(args
->tp
, ag
, blen
,
3693 xfs_bmap_select_minlen(ap
, args
, blen
, notinit
);
3696 * Set the failure fallback case to look in the selected AG as stream
3699 ap
->blkno
= args
->fsbno
= XFS_AGB_TO_FSB(mp
, ag
, 0);
3705 struct xfs_bmalloca
*ap
) /* bmap alloc argument struct */
3707 xfs_mount_t
*mp
; /* mount point structure */
3708 xfs_alloctype_t atype
= 0; /* type for allocation routines */
3709 xfs_extlen_t align
= 0; /* minimum allocation alignment */
3710 xfs_agnumber_t fb_agno
; /* ag number of ap->firstblock */
3712 xfs_alloc_arg_t args
;
3714 xfs_extlen_t nextminlen
= 0;
3715 int nullfb
; /* true if ap->firstblock isn't set */
3723 mp
= ap
->ip
->i_mount
;
3725 /* stripe alignment for allocation is determined by mount parameters */
3727 if (mp
->m_swidth
&& (mp
->m_flags
& XFS_MOUNT_SWALLOC
))
3728 stripe_align
= mp
->m_swidth
;
3729 else if (mp
->m_dalign
)
3730 stripe_align
= mp
->m_dalign
;
3732 if (ap
->flags
& XFS_BMAPI_COWFORK
)
3733 align
= xfs_get_cowextsz_hint(ap
->ip
);
3734 else if (xfs_alloc_is_userdata(ap
->datatype
))
3735 align
= xfs_get_extsz_hint(ap
->ip
);
3737 error
= xfs_bmap_extsize_align(mp
, &ap
->got
, &ap
->prev
,
3738 align
, 0, ap
->eof
, 0, ap
->conv
,
3739 &ap
->offset
, &ap
->length
);
3745 nullfb
= *ap
->firstblock
== NULLFSBLOCK
;
3746 fb_agno
= nullfb
? NULLAGNUMBER
: XFS_FSB_TO_AGNO(mp
, *ap
->firstblock
);
3748 if (xfs_alloc_is_userdata(ap
->datatype
) &&
3749 xfs_inode_is_filestream(ap
->ip
)) {
3750 ag
= xfs_filestream_lookup_ag(ap
->ip
);
3751 ag
= (ag
!= NULLAGNUMBER
) ? ag
: 0;
3752 ap
->blkno
= XFS_AGB_TO_FSB(mp
, ag
, 0);
3754 ap
->blkno
= XFS_INO_TO_FSB(mp
, ap
->ip
->i_ino
);
3757 ap
->blkno
= *ap
->firstblock
;
3759 xfs_bmap_adjacent(ap
);
3762 * If allowed, use ap->blkno; otherwise must use firstblock since
3763 * it's in the right allocation group.
3765 if (nullfb
|| XFS_FSB_TO_AGNO(mp
, ap
->blkno
) == fb_agno
)
3768 ap
->blkno
= *ap
->firstblock
;
3770 * Normal allocation, done through xfs_alloc_vextent.
3772 tryagain
= isaligned
= 0;
3773 memset(&args
, 0, sizeof(args
));
3776 args
.fsbno
= ap
->blkno
;
3777 xfs_rmap_skip_owner_update(&args
.oinfo
);
3779 /* Trim the allocation back to the maximum an AG can fit. */
3780 args
.maxlen
= MIN(ap
->length
, mp
->m_ag_max_usable
);
3781 args
.firstblock
= *ap
->firstblock
;
3785 * Search for an allocation group with a single extent large
3786 * enough for the request. If one isn't found, then adjust
3787 * the minimum allocation size to the largest space found.
3789 if (xfs_alloc_is_userdata(ap
->datatype
) &&
3790 xfs_inode_is_filestream(ap
->ip
))
3791 error
= xfs_bmap_btalloc_filestreams(ap
, &args
, &blen
);
3793 error
= xfs_bmap_btalloc_nullfb(ap
, &args
, &blen
);
3796 } else if (ap
->dfops
->dop_low
) {
3797 if (xfs_inode_is_filestream(ap
->ip
))
3798 args
.type
= XFS_ALLOCTYPE_FIRST_AG
;
3800 args
.type
= XFS_ALLOCTYPE_START_BNO
;
3801 args
.total
= args
.minlen
= ap
->minlen
;
3803 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
3804 args
.total
= ap
->total
;
3805 args
.minlen
= ap
->minlen
;
3807 /* apply extent size hints if obtained earlier */
3810 if ((args
.mod
= (xfs_extlen_t
)do_mod(ap
->offset
, args
.prod
)))
3811 args
.mod
= (xfs_extlen_t
)(args
.prod
- args
.mod
);
3812 } else if (mp
->m_sb
.sb_blocksize
>= PAGE_SIZE
) {
3816 args
.prod
= PAGE_SIZE
>> mp
->m_sb
.sb_blocklog
;
3817 if ((args
.mod
= (xfs_extlen_t
)(do_mod(ap
->offset
, args
.prod
))))
3818 args
.mod
= (xfs_extlen_t
)(args
.prod
- args
.mod
);
3821 * If we are not low on available data blocks, and the
3822 * underlying logical volume manager is a stripe, and
3823 * the file offset is zero then try to allocate data
3824 * blocks on stripe unit boundary.
3825 * NOTE: ap->aeof is only set if the allocation length
3826 * is >= the stripe unit and the allocation offset is
3827 * at the end of file.
3829 if (!ap
->dfops
->dop_low
&& ap
->aeof
) {
3831 args
.alignment
= stripe_align
;
3835 * Adjust for alignment
3837 if (blen
> args
.alignment
&& blen
<= args
.maxlen
)
3838 args
.minlen
= blen
- args
.alignment
;
3839 args
.minalignslop
= 0;
3842 * First try an exact bno allocation.
3843 * If it fails then do a near or start bno
3844 * allocation with alignment turned on.
3848 args
.type
= XFS_ALLOCTYPE_THIS_BNO
;
3851 * Compute the minlen+alignment for the
3852 * next case. Set slop so that the value
3853 * of minlen+alignment+slop doesn't go up
3854 * between the calls.
3856 if (blen
> stripe_align
&& blen
<= args
.maxlen
)
3857 nextminlen
= blen
- stripe_align
;
3859 nextminlen
= args
.minlen
;
3860 if (nextminlen
+ stripe_align
> args
.minlen
+ 1)
3862 nextminlen
+ stripe_align
-
3865 args
.minalignslop
= 0;
3869 args
.minalignslop
= 0;
3871 args
.minleft
= ap
->minleft
;
3872 args
.wasdel
= ap
->wasdel
;
3873 args
.resv
= XFS_AG_RESV_NONE
;
3874 args
.datatype
= ap
->datatype
;
3875 if (ap
->datatype
& XFS_ALLOC_USERDATA_ZERO
)
3878 error
= xfs_alloc_vextent(&args
);
3882 if (tryagain
&& args
.fsbno
== NULLFSBLOCK
) {
3884 * Exact allocation failed. Now try with alignment
3888 args
.fsbno
= ap
->blkno
;
3889 args
.alignment
= stripe_align
;
3890 args
.minlen
= nextminlen
;
3891 args
.minalignslop
= 0;
3893 if ((error
= xfs_alloc_vextent(&args
)))
3896 if (isaligned
&& args
.fsbno
== NULLFSBLOCK
) {
3898 * allocation failed, so turn off alignment and
3902 args
.fsbno
= ap
->blkno
;
3904 if ((error
= xfs_alloc_vextent(&args
)))
3907 if (args
.fsbno
== NULLFSBLOCK
&& nullfb
&&
3908 args
.minlen
> ap
->minlen
) {
3909 args
.minlen
= ap
->minlen
;
3910 args
.type
= XFS_ALLOCTYPE_START_BNO
;
3911 args
.fsbno
= ap
->blkno
;
3912 if ((error
= xfs_alloc_vextent(&args
)))
3915 if (args
.fsbno
== NULLFSBLOCK
&& nullfb
) {
3917 args
.type
= XFS_ALLOCTYPE_FIRST_AG
;
3918 args
.total
= ap
->minlen
;
3919 if ((error
= xfs_alloc_vextent(&args
)))
3921 ap
->dfops
->dop_low
= true;
3923 if (args
.fsbno
!= NULLFSBLOCK
) {
3925 * check the allocation happened at the same or higher AG than
3926 * the first block that was allocated.
3928 ASSERT(*ap
->firstblock
== NULLFSBLOCK
||
3929 XFS_FSB_TO_AGNO(mp
, *ap
->firstblock
) <=
3930 XFS_FSB_TO_AGNO(mp
, args
.fsbno
));
3932 ap
->blkno
= args
.fsbno
;
3933 if (*ap
->firstblock
== NULLFSBLOCK
)
3934 *ap
->firstblock
= args
.fsbno
;
3935 ASSERT(nullfb
|| fb_agno
<= args
.agno
);
3936 ap
->length
= args
.len
;
3937 if (!(ap
->flags
& XFS_BMAPI_COWFORK
))
3938 ap
->ip
->i_d
.di_nblocks
+= args
.len
;
3939 xfs_trans_log_inode(ap
->tp
, ap
->ip
, XFS_ILOG_CORE
);
3941 ap
->ip
->i_delayed_blks
-= args
.len
;
3943 * Adjust the disk quota also. This was reserved
3946 xfs_trans_mod_dquot_byino(ap
->tp
, ap
->ip
,
3947 ap
->wasdel
? XFS_TRANS_DQ_DELBCOUNT
:
3948 XFS_TRANS_DQ_BCOUNT
,
3951 ap
->blkno
= NULLFSBLOCK
;
3958 * For a remap operation, just "allocate" an extent at the address that the
3959 * caller passed in, and ensure that the AGFL is the right size. The caller
3960 * will then map the "allocated" extent into the file somewhere.
3963 xfs_bmap_remap_alloc(
3964 struct xfs_bmalloca
*ap
)
3966 struct xfs_trans
*tp
= ap
->tp
;
3967 struct xfs_mount
*mp
= tp
->t_mountp
;
3969 struct xfs_alloc_arg args
;
3973 * validate that the block number is legal - the enables us to detect
3974 * and handle a silent filesystem corruption rather than crashing.
3976 memset(&args
, 0, sizeof(struct xfs_alloc_arg
));
3978 args
.mp
= ap
->tp
->t_mountp
;
3979 bno
= *ap
->firstblock
;
3980 args
.agno
= XFS_FSB_TO_AGNO(mp
, bno
);
3981 args
.agbno
= XFS_FSB_TO_AGBNO(mp
, bno
);
3982 if (args
.agno
>= mp
->m_sb
.sb_agcount
||
3983 args
.agbno
>= mp
->m_sb
.sb_agblocks
)
3984 return -EFSCORRUPTED
;
3986 /* "Allocate" the extent from the range we passed in. */
3987 trace_xfs_bmap_remap_alloc(ap
->ip
, *ap
->firstblock
, ap
->length
);
3989 ap
->ip
->i_d
.di_nblocks
+= ap
->length
;
3990 xfs_trans_log_inode(ap
->tp
, ap
->ip
, XFS_ILOG_CORE
);
3992 /* Fix the freelist, like a real allocator does. */
3993 args
.datatype
= ap
->datatype
;
3994 args
.pag
= xfs_perag_get(args
.mp
, args
.agno
);
3998 * The freelist fixing code will decline the allocation if
3999 * the size and shape of the free space doesn't allow for
4000 * allocating the extent and updating all the metadata that
4001 * happens during an allocation. We're remapping, not
4002 * allocating, so skip that check by pretending to be freeing.
4004 error
= xfs_alloc_fix_freelist(&args
, XFS_ALLOC_FLAG_FREEING
);
4005 xfs_perag_put(args
.pag
);
4007 trace_xfs_bmap_remap_alloc_error(ap
->ip
, error
, _RET_IP_
);
4012 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
4013 * It figures out where to ask the underlying allocator to put the new extent.
4017 struct xfs_bmalloca
*ap
) /* bmap alloc argument struct */
4019 if (ap
->flags
& XFS_BMAPI_REMAP
)
4020 return xfs_bmap_remap_alloc(ap
);
4021 if (XFS_IS_REALTIME_INODE(ap
->ip
) &&
4022 xfs_alloc_is_userdata(ap
->datatype
))
4023 return xfs_bmap_rtalloc(ap
);
4024 return xfs_bmap_btalloc(ap
);
4027 /* Trim extent to fit a logical block range. */
4030 struct xfs_bmbt_irec
*irec
,
4034 xfs_fileoff_t distance
;
4035 xfs_fileoff_t end
= bno
+ len
;
4037 if (irec
->br_startoff
+ irec
->br_blockcount
<= bno
||
4038 irec
->br_startoff
>= end
) {
4039 irec
->br_blockcount
= 0;
4043 if (irec
->br_startoff
< bno
) {
4044 distance
= bno
- irec
->br_startoff
;
4045 if (isnullstartblock(irec
->br_startblock
))
4046 irec
->br_startblock
= DELAYSTARTBLOCK
;
4047 if (irec
->br_startblock
!= DELAYSTARTBLOCK
&&
4048 irec
->br_startblock
!= HOLESTARTBLOCK
)
4049 irec
->br_startblock
+= distance
;
4050 irec
->br_startoff
+= distance
;
4051 irec
->br_blockcount
-= distance
;
4054 if (end
< irec
->br_startoff
+ irec
->br_blockcount
) {
4055 distance
= irec
->br_startoff
+ irec
->br_blockcount
- end
;
4056 irec
->br_blockcount
-= distance
;
4060 /* trim extent to within eof */
4062 xfs_trim_extent_eof(
4063 struct xfs_bmbt_irec
*irec
,
4064 struct xfs_inode
*ip
)
4067 xfs_trim_extent(irec
, 0, XFS_B_TO_FSB(ip
->i_mount
,
4068 i_size_read(VFS_I(ip
))));
4072 * Trim the returned map to the required bounds
4076 struct xfs_bmbt_irec
*mval
,
4077 struct xfs_bmbt_irec
*got
,
4085 if ((flags
& XFS_BMAPI_ENTIRE
) ||
4086 got
->br_startoff
+ got
->br_blockcount
<= obno
) {
4088 if (isnullstartblock(got
->br_startblock
))
4089 mval
->br_startblock
= DELAYSTARTBLOCK
;
4095 ASSERT((*bno
>= obno
) || (n
== 0));
4097 mval
->br_startoff
= *bno
;
4098 if (isnullstartblock(got
->br_startblock
))
4099 mval
->br_startblock
= DELAYSTARTBLOCK
;
4101 mval
->br_startblock
= got
->br_startblock
+
4102 (*bno
- got
->br_startoff
);
4104 * Return the minimum of what we got and what we asked for for
4105 * the length. We can use the len variable here because it is
4106 * modified below and we could have been there before coming
4107 * here if the first part of the allocation didn't overlap what
4110 mval
->br_blockcount
= XFS_FILBLKS_MIN(end
- *bno
,
4111 got
->br_blockcount
- (*bno
- got
->br_startoff
));
4112 mval
->br_state
= got
->br_state
;
4113 ASSERT(mval
->br_blockcount
<= len
);
4118 * Update and validate the extent map to return
4121 xfs_bmapi_update_map(
4122 struct xfs_bmbt_irec
**map
,
4130 xfs_bmbt_irec_t
*mval
= *map
;
4132 ASSERT((flags
& XFS_BMAPI_ENTIRE
) ||
4133 ((mval
->br_startoff
+ mval
->br_blockcount
) <= end
));
4134 ASSERT((flags
& XFS_BMAPI_ENTIRE
) || (mval
->br_blockcount
<= *len
) ||
4135 (mval
->br_startoff
< obno
));
4137 *bno
= mval
->br_startoff
+ mval
->br_blockcount
;
4139 if (*n
> 0 && mval
->br_startoff
== mval
[-1].br_startoff
) {
4140 /* update previous map with new information */
4141 ASSERT(mval
->br_startblock
== mval
[-1].br_startblock
);
4142 ASSERT(mval
->br_blockcount
> mval
[-1].br_blockcount
);
4143 ASSERT(mval
->br_state
== mval
[-1].br_state
);
4144 mval
[-1].br_blockcount
= mval
->br_blockcount
;
4145 mval
[-1].br_state
= mval
->br_state
;
4146 } else if (*n
> 0 && mval
->br_startblock
!= DELAYSTARTBLOCK
&&
4147 mval
[-1].br_startblock
!= DELAYSTARTBLOCK
&&
4148 mval
[-1].br_startblock
!= HOLESTARTBLOCK
&&
4149 mval
->br_startblock
== mval
[-1].br_startblock
+
4150 mval
[-1].br_blockcount
&&
4151 ((flags
& XFS_BMAPI_IGSTATE
) ||
4152 mval
[-1].br_state
== mval
->br_state
)) {
4153 ASSERT(mval
->br_startoff
==
4154 mval
[-1].br_startoff
+ mval
[-1].br_blockcount
);
4155 mval
[-1].br_blockcount
+= mval
->br_blockcount
;
4156 } else if (*n
> 0 &&
4157 mval
->br_startblock
== DELAYSTARTBLOCK
&&
4158 mval
[-1].br_startblock
== DELAYSTARTBLOCK
&&
4159 mval
->br_startoff
==
4160 mval
[-1].br_startoff
+ mval
[-1].br_blockcount
) {
4161 mval
[-1].br_blockcount
+= mval
->br_blockcount
;
4162 mval
[-1].br_state
= mval
->br_state
;
4163 } else if (!((*n
== 0) &&
4164 ((mval
->br_startoff
+ mval
->br_blockcount
) <=
4173 * Map file blocks to filesystem blocks without allocation.
4177 struct xfs_inode
*ip
,
4180 struct xfs_bmbt_irec
*mval
,
4184 struct xfs_mount
*mp
= ip
->i_mount
;
4185 struct xfs_ifork
*ifp
;
4186 struct xfs_bmbt_irec got
;
4187 struct xfs_bmbt_irec prev
;
4194 int whichfork
= xfs_bmapi_whichfork(flags
);
4197 ASSERT(!(flags
& ~(XFS_BMAPI_ATTRFORK
|XFS_BMAPI_ENTIRE
|
4198 XFS_BMAPI_IGSTATE
|XFS_BMAPI_COWFORK
)));
4199 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_SHARED
|XFS_ILOCK_EXCL
));
4201 if (unlikely(XFS_TEST_ERROR(
4202 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
4203 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
4204 mp
, XFS_ERRTAG_BMAPIFORMAT
, XFS_RANDOM_BMAPIFORMAT
))) {
4205 XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW
, mp
);
4206 return -EFSCORRUPTED
;
4209 if (XFS_FORCED_SHUTDOWN(mp
))
4212 XFS_STATS_INC(mp
, xs_blk_mapr
);
4214 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4216 /* No CoW fork? Return a hole. */
4217 if (whichfork
== XFS_COW_FORK
) {
4218 mval
->br_startoff
= bno
;
4219 mval
->br_startblock
= HOLESTARTBLOCK
;
4220 mval
->br_blockcount
= len
;
4221 mval
->br_state
= XFS_EXT_NORM
;
4227 * A missing attr ifork implies that the inode says we're in
4228 * extents or btree format but failed to pass the inode fork
4229 * verifier while trying to load it. Treat that as a file
4233 xfs_alert(mp
, "%s: inode %llu missing fork %d",
4234 __func__
, ip
->i_ino
, whichfork
);
4236 return -EFSCORRUPTED
;
4239 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
4240 error
= xfs_iread_extents(NULL
, ip
, whichfork
);
4245 xfs_bmap_search_extents(ip
, bno
, whichfork
, &eof
, &lastx
, &got
, &prev
);
4249 while (bno
< end
&& n
< *nmap
) {
4250 /* Reading past eof, act as though there's a hole up to end. */
4252 got
.br_startoff
= end
;
4253 if (got
.br_startoff
> bno
) {
4254 /* Reading in a hole. */
4255 mval
->br_startoff
= bno
;
4256 mval
->br_startblock
= HOLESTARTBLOCK
;
4257 mval
->br_blockcount
=
4258 XFS_FILBLKS_MIN(len
, got
.br_startoff
- bno
);
4259 mval
->br_state
= XFS_EXT_NORM
;
4260 bno
+= mval
->br_blockcount
;
4261 len
-= mval
->br_blockcount
;
4267 /* set up the extent map to return. */
4268 xfs_bmapi_trim_map(mval
, &got
, &bno
, len
, obno
, end
, n
, flags
);
4269 xfs_bmapi_update_map(&mval
, &bno
, &len
, obno
, end
, &n
, flags
);
4271 /* If we're done, stop now. */
4272 if (bno
>= end
|| n
>= *nmap
)
4275 /* Else go on to the next record. */
4276 if (++lastx
< xfs_iext_count(ifp
))
4277 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, lastx
), &got
);
4286 * Add a delayed allocation extent to an inode. Blocks are reserved from the
4287 * global pool and the extent inserted into the inode in-core extent tree.
4289 * On entry, got refers to the first extent beyond the offset of the extent to
4290 * allocate or eof is specified if no such extent exists. On return, got refers
4291 * to the extent record that was inserted to the inode fork.
4293 * Note that the allocated extent may have been merged with contiguous extents
4294 * during insertion into the inode fork. Thus, got does not reflect the current
4295 * state of the inode fork on return. If necessary, the caller can use lastx to
4296 * look up the updated record in the inode fork.
4299 xfs_bmapi_reserve_delalloc(
4300 struct xfs_inode
*ip
,
4304 xfs_filblks_t prealloc
,
4305 struct xfs_bmbt_irec
*got
,
4306 xfs_extnum_t
*lastx
,
4309 struct xfs_mount
*mp
= ip
->i_mount
;
4310 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4312 xfs_extlen_t indlen
;
4313 char rt
= XFS_IS_REALTIME_INODE(ip
);
4316 xfs_fileoff_t aoff
= off
;
4319 * Cap the alloc length. Keep track of prealloc so we know whether to
4320 * tag the inode before we return.
4322 alen
= XFS_FILBLKS_MIN(len
+ prealloc
, MAXEXTLEN
);
4324 alen
= XFS_FILBLKS_MIN(alen
, got
->br_startoff
- aoff
);
4325 if (prealloc
&& alen
>= len
)
4326 prealloc
= alen
- len
;
4328 /* Figure out the extent size, adjust alen */
4329 if (whichfork
== XFS_COW_FORK
)
4330 extsz
= xfs_get_cowextsz_hint(ip
);
4332 extsz
= xfs_get_extsz_hint(ip
);
4334 struct xfs_bmbt_irec prev
;
4336 if (!xfs_iext_get_extent(ifp
, *lastx
- 1, &prev
))
4337 prev
.br_startoff
= NULLFILEOFF
;
4339 error
= xfs_bmap_extsize_align(mp
, got
, &prev
, extsz
, rt
, eof
,
4340 1, 0, &aoff
, &alen
);
4345 extsz
= alen
/ mp
->m_sb
.sb_rextsize
;
4348 * Make a transaction-less quota reservation for delayed allocation
4349 * blocks. This number gets adjusted later. We return if we haven't
4350 * allocated blocks already inside this loop.
4352 error
= xfs_trans_reserve_quota_nblks(NULL
, ip
, (long)alen
, 0,
4353 rt
? XFS_QMOPT_RES_RTBLKS
: XFS_QMOPT_RES_REGBLKS
);
4358 * Split changing sb for alen and indlen since they could be coming
4359 * from different places.
4361 indlen
= (xfs_extlen_t
)xfs_bmap_worst_indlen(ip
, alen
);
4365 error
= xfs_mod_frextents(mp
, -((int64_t)extsz
));
4367 error
= xfs_mod_fdblocks(mp
, -((int64_t)alen
), false);
4371 goto out_unreserve_quota
;
4373 error
= xfs_mod_fdblocks(mp
, -((int64_t)indlen
), false);
4375 goto out_unreserve_blocks
;
4378 ip
->i_delayed_blks
+= alen
;
4380 got
->br_startoff
= aoff
;
4381 got
->br_startblock
= nullstartblock(indlen
);
4382 got
->br_blockcount
= alen
;
4383 got
->br_state
= XFS_EXT_NORM
;
4385 xfs_bmap_add_extent_hole_delay(ip
, whichfork
, lastx
, got
);
4388 * Tag the inode if blocks were preallocated. Note that COW fork
4389 * preallocation can occur at the start or end of the extent, even when
4390 * prealloc == 0, so we must also check the aligned offset and length.
4392 if (whichfork
== XFS_DATA_FORK
&& prealloc
)
4393 xfs_inode_set_eofblocks_tag(ip
);
4394 if (whichfork
== XFS_COW_FORK
&& (prealloc
|| aoff
< off
|| alen
> len
))
4395 xfs_inode_set_cowblocks_tag(ip
);
4399 out_unreserve_blocks
:
4401 xfs_mod_frextents(mp
, extsz
);
4403 xfs_mod_fdblocks(mp
, alen
, false);
4404 out_unreserve_quota
:
4405 if (XFS_IS_QUOTA_ON(mp
))
4406 xfs_trans_unreserve_quota_nblks(NULL
, ip
, (long)alen
, 0, rt
?
4407 XFS_QMOPT_RES_RTBLKS
: XFS_QMOPT_RES_REGBLKS
);
4413 struct xfs_bmalloca
*bma
)
4415 struct xfs_mount
*mp
= bma
->ip
->i_mount
;
4416 int whichfork
= xfs_bmapi_whichfork(bma
->flags
);
4417 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
4418 int tmp_logflags
= 0;
4421 ASSERT(bma
->length
> 0);
4424 * For the wasdelay case, we could also just allocate the stuff asked
4425 * for in this bmap call but that wouldn't be as good.
4428 bma
->length
= (xfs_extlen_t
)bma
->got
.br_blockcount
;
4429 bma
->offset
= bma
->got
.br_startoff
;
4430 if (bma
->idx
!= NULLEXTNUM
&& bma
->idx
) {
4431 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
- 1),
4435 bma
->length
= XFS_FILBLKS_MIN(bma
->length
, MAXEXTLEN
);
4437 bma
->length
= XFS_FILBLKS_MIN(bma
->length
,
4438 bma
->got
.br_startoff
- bma
->offset
);
4442 * Set the data type being allocated. For the data fork, the first data
4443 * in the file is treated differently to all other allocations. For the
4444 * attribute fork, we only need to ensure the allocated range is not on
4447 if (!(bma
->flags
& XFS_BMAPI_METADATA
)) {
4448 bma
->datatype
= XFS_ALLOC_NOBUSY
;
4449 if (whichfork
== XFS_DATA_FORK
) {
4450 if (bma
->offset
== 0)
4451 bma
->datatype
|= XFS_ALLOC_INITIAL_USER_DATA
;
4453 bma
->datatype
|= XFS_ALLOC_USERDATA
;
4455 if (bma
->flags
& XFS_BMAPI_ZERO
)
4456 bma
->datatype
|= XFS_ALLOC_USERDATA_ZERO
;
4459 bma
->minlen
= (bma
->flags
& XFS_BMAPI_CONTIG
) ? bma
->length
: 1;
4462 * Only want to do the alignment at the eof if it is userdata and
4463 * allocation length is larger than a stripe unit.
4465 if (mp
->m_dalign
&& bma
->length
>= mp
->m_dalign
&&
4466 !(bma
->flags
& XFS_BMAPI_METADATA
) && whichfork
== XFS_DATA_FORK
) {
4467 error
= xfs_bmap_isaeof(bma
, whichfork
);
4472 error
= xfs_bmap_alloc(bma
);
4477 bma
->cur
->bc_private
.b
.firstblock
= *bma
->firstblock
;
4478 if (bma
->blkno
== NULLFSBLOCK
)
4480 if ((ifp
->if_flags
& XFS_IFBROOT
) && !bma
->cur
) {
4481 bma
->cur
= xfs_bmbt_init_cursor(mp
, bma
->tp
, bma
->ip
, whichfork
);
4482 bma
->cur
->bc_private
.b
.firstblock
= *bma
->firstblock
;
4483 bma
->cur
->bc_private
.b
.dfops
= bma
->dfops
;
4486 * Bump the number of extents we've allocated
4492 bma
->cur
->bc_private
.b
.flags
=
4493 bma
->wasdel
? XFS_BTCUR_BPRV_WASDEL
: 0;
4495 bma
->got
.br_startoff
= bma
->offset
;
4496 bma
->got
.br_startblock
= bma
->blkno
;
4497 bma
->got
.br_blockcount
= bma
->length
;
4498 bma
->got
.br_state
= XFS_EXT_NORM
;
4501 * In the data fork, a wasdelay extent has been initialized, so
4502 * shouldn't be flagged as unwritten.
4504 * For the cow fork, however, we convert delalloc reservations
4505 * (extents allocated for speculative preallocation) to
4506 * allocated unwritten extents, and only convert the unwritten
4507 * extents to real extents when we're about to write the data.
4509 if ((!bma
->wasdel
|| (bma
->flags
& XFS_BMAPI_COWFORK
)) &&
4510 (bma
->flags
& XFS_BMAPI_PREALLOC
) &&
4511 xfs_sb_version_hasextflgbit(&mp
->m_sb
))
4512 bma
->got
.br_state
= XFS_EXT_UNWRITTEN
;
4515 error
= xfs_bmap_add_extent_delay_real(bma
, whichfork
);
4517 error
= xfs_bmap_add_extent_hole_real(bma
, whichfork
);
4519 bma
->logflags
|= tmp_logflags
;
4524 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4525 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4526 * the neighbouring ones.
4528 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
), &bma
->got
);
4530 ASSERT(bma
->got
.br_startoff
<= bma
->offset
);
4531 ASSERT(bma
->got
.br_startoff
+ bma
->got
.br_blockcount
>=
4532 bma
->offset
+ bma
->length
);
4533 ASSERT(bma
->got
.br_state
== XFS_EXT_NORM
||
4534 bma
->got
.br_state
== XFS_EXT_UNWRITTEN
);
4539 xfs_bmapi_convert_unwritten(
4540 struct xfs_bmalloca
*bma
,
4541 struct xfs_bmbt_irec
*mval
,
4545 int whichfork
= xfs_bmapi_whichfork(flags
);
4546 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
4547 int tmp_logflags
= 0;
4550 /* check if we need to do unwritten->real conversion */
4551 if (mval
->br_state
== XFS_EXT_UNWRITTEN
&&
4552 (flags
& XFS_BMAPI_PREALLOC
))
4555 /* check if we need to do real->unwritten conversion */
4556 if (mval
->br_state
== XFS_EXT_NORM
&&
4557 (flags
& (XFS_BMAPI_PREALLOC
| XFS_BMAPI_CONVERT
)) !=
4558 (XFS_BMAPI_PREALLOC
| XFS_BMAPI_CONVERT
))
4562 * Modify (by adding) the state flag, if writing.
4564 ASSERT(mval
->br_blockcount
<= len
);
4565 if ((ifp
->if_flags
& XFS_IFBROOT
) && !bma
->cur
) {
4566 bma
->cur
= xfs_bmbt_init_cursor(bma
->ip
->i_mount
, bma
->tp
,
4567 bma
->ip
, whichfork
);
4568 bma
->cur
->bc_private
.b
.firstblock
= *bma
->firstblock
;
4569 bma
->cur
->bc_private
.b
.dfops
= bma
->dfops
;
4571 mval
->br_state
= (mval
->br_state
== XFS_EXT_UNWRITTEN
)
4572 ? XFS_EXT_NORM
: XFS_EXT_UNWRITTEN
;
4575 * Before insertion into the bmbt, zero the range being converted
4578 if (flags
& XFS_BMAPI_ZERO
) {
4579 error
= xfs_zero_extent(bma
->ip
, mval
->br_startblock
,
4580 mval
->br_blockcount
);
4585 error
= xfs_bmap_add_extent_unwritten_real(bma
->tp
, bma
->ip
, whichfork
,
4586 &bma
->idx
, &bma
->cur
, mval
, bma
->firstblock
, bma
->dfops
,
4589 * Log the inode core unconditionally in the unwritten extent conversion
4590 * path because the conversion might not have done so (e.g., if the
4591 * extent count hasn't changed). We need to make sure the inode is dirty
4592 * in the transaction for the sake of fsync(), even if nothing has
4593 * changed, because fsync() will not force the log for this transaction
4594 * unless it sees the inode pinned.
4596 * Note: If we're only converting cow fork extents, there aren't
4597 * any on-disk updates to make, so we don't need to log anything.
4599 if (whichfork
!= XFS_COW_FORK
)
4600 bma
->logflags
|= tmp_logflags
| XFS_ILOG_CORE
;
4605 * Update our extent pointer, given that
4606 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4607 * of the neighbouring ones.
4609 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
), &bma
->got
);
4612 * We may have combined previously unwritten space with written space,
4613 * so generate another request.
4615 if (mval
->br_blockcount
< len
)
4621 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4622 * extent state if necessary. Details behaviour is controlled by the flags
4623 * parameter. Only allocates blocks from a single allocation group, to avoid
4626 * The returned value in "firstblock" from the first call in a transaction
4627 * must be remembered and presented to subsequent calls in "firstblock".
4628 * An upper bound for the number of blocks to be allocated is supplied to
4629 * the first call in "total"; if no allocation group has that many free
4630 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4634 struct xfs_trans
*tp
, /* transaction pointer */
4635 struct xfs_inode
*ip
, /* incore inode */
4636 xfs_fileoff_t bno
, /* starting file offs. mapped */
4637 xfs_filblks_t len
, /* length to map in file */
4638 int flags
, /* XFS_BMAPI_... */
4639 xfs_fsblock_t
*firstblock
, /* first allocated block
4640 controls a.g. for allocs */
4641 xfs_extlen_t total
, /* total blocks needed */
4642 struct xfs_bmbt_irec
*mval
, /* output: map values */
4643 int *nmap
, /* i/o: mval size/count */
4644 struct xfs_defer_ops
*dfops
) /* i/o: list extents to free */
4646 struct xfs_mount
*mp
= ip
->i_mount
;
4647 struct xfs_ifork
*ifp
;
4648 struct xfs_bmalloca bma
= { NULL
}; /* args for xfs_bmap_alloc */
4649 xfs_fileoff_t end
; /* end of mapped file region */
4650 int eof
; /* after the end of extents */
4651 int error
; /* error return */
4652 int n
; /* current extent index */
4653 xfs_fileoff_t obno
; /* old block number (offset) */
4654 int whichfork
; /* data or attr fork */
4657 xfs_fileoff_t orig_bno
; /* original block number value */
4658 int orig_flags
; /* original flags arg value */
4659 xfs_filblks_t orig_len
; /* original value of len arg */
4660 struct xfs_bmbt_irec
*orig_mval
; /* original value of mval */
4661 int orig_nmap
; /* original value of *nmap */
4669 whichfork
= xfs_bmapi_whichfork(flags
);
4672 ASSERT(*nmap
<= XFS_BMAP_MAX_NMAP
);
4673 ASSERT(!(flags
& XFS_BMAPI_IGSTATE
));
4674 ASSERT(tp
!= NULL
||
4675 (flags
& (XFS_BMAPI_CONVERT
| XFS_BMAPI_COWFORK
)) ==
4676 (XFS_BMAPI_CONVERT
| XFS_BMAPI_COWFORK
));
4678 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_LOCAL
);
4679 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
4680 ASSERT(!(flags
& XFS_BMAPI_REMAP
) || whichfork
== XFS_DATA_FORK
);
4681 ASSERT(!(flags
& XFS_BMAPI_PREALLOC
) || !(flags
& XFS_BMAPI_REMAP
));
4682 ASSERT(!(flags
& XFS_BMAPI_CONVERT
) || !(flags
& XFS_BMAPI_REMAP
));
4684 /* zeroing is for currently only for data extents, not metadata */
4685 ASSERT((flags
& (XFS_BMAPI_METADATA
| XFS_BMAPI_ZERO
)) !=
4686 (XFS_BMAPI_METADATA
| XFS_BMAPI_ZERO
));
4688 * we can allocate unwritten extents or pre-zero allocated blocks,
4689 * but it makes no sense to do both at once. This would result in
4690 * zeroing the unwritten extent twice, but it still being an
4691 * unwritten extent....
4693 ASSERT((flags
& (XFS_BMAPI_PREALLOC
| XFS_BMAPI_ZERO
)) !=
4694 (XFS_BMAPI_PREALLOC
| XFS_BMAPI_ZERO
));
4696 if (unlikely(XFS_TEST_ERROR(
4697 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
4698 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
4699 mp
, XFS_ERRTAG_BMAPIFORMAT
, XFS_RANDOM_BMAPIFORMAT
))) {
4700 XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW
, mp
);
4701 return -EFSCORRUPTED
;
4704 if (XFS_FORCED_SHUTDOWN(mp
))
4707 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4709 XFS_STATS_INC(mp
, xs_blk_mapw
);
4711 if (*firstblock
== NULLFSBLOCK
) {
4712 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
)
4713 bma
.minleft
= be16_to_cpu(ifp
->if_broot
->bb_level
) + 1;
4720 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
4721 error
= xfs_iread_extents(tp
, ip
, whichfork
);
4726 xfs_bmap_search_extents(ip
, bno
, whichfork
, &eof
, &bma
.idx
, &bma
.got
,
4737 bma
.firstblock
= firstblock
;
4739 while (bno
< end
&& n
< *nmap
) {
4740 bool need_alloc
= false, wasdelay
= false;
4742 /* in hole or beyoned EOF? */
4743 if (eof
|| bma
.got
.br_startoff
> bno
) {
4744 if (flags
& XFS_BMAPI_DELALLOC
) {
4746 * For the COW fork we can reasonably get a
4747 * request for converting an extent that races
4748 * with other threads already having converted
4749 * part of it, as there converting COW to
4750 * regular blocks is not protected using the
4753 ASSERT(flags
& XFS_BMAPI_COWFORK
);
4754 if (!(flags
& XFS_BMAPI_COWFORK
)) {
4759 if (eof
|| bno
>= end
)
4766 * Make sure we only reflink into a hole.
4768 ASSERT(!(flags
& XFS_BMAPI_REMAP
));
4769 if (isnullstartblock(bma
.got
.br_startblock
))
4774 * First, deal with the hole before the allocated space
4775 * that we found, if any.
4777 if (need_alloc
|| wasdelay
) {
4779 bma
.conv
= !!(flags
& XFS_BMAPI_CONVERT
);
4780 bma
.wasdel
= wasdelay
;
4785 * There's a 32/64 bit type mismatch between the
4786 * allocation length request (which can be 64 bits in
4787 * length) and the bma length request, which is
4788 * xfs_extlen_t and therefore 32 bits. Hence we have to
4789 * check for 32-bit overflows and handle them here.
4791 if (len
> (xfs_filblks_t
)MAXEXTLEN
)
4792 bma
.length
= MAXEXTLEN
;
4797 ASSERT(bma
.length
> 0);
4798 error
= xfs_bmapi_allocate(&bma
);
4801 if (bma
.blkno
== NULLFSBLOCK
)
4805 * If this is a CoW allocation, record the data in
4806 * the refcount btree for orphan recovery.
4808 if (whichfork
== XFS_COW_FORK
) {
4809 error
= xfs_refcount_alloc_cow_extent(mp
, dfops
,
4810 bma
.blkno
, bma
.length
);
4816 /* Deal with the allocated space we found. */
4817 xfs_bmapi_trim_map(mval
, &bma
.got
, &bno
, len
, obno
,
4820 /* Execute unwritten extent conversion if necessary */
4821 error
= xfs_bmapi_convert_unwritten(&bma
, mval
, len
, flags
);
4822 if (error
== -EAGAIN
)
4827 /* update the extent map to return */
4828 xfs_bmapi_update_map(&mval
, &bno
, &len
, obno
, end
, &n
, flags
);
4831 * If we're done, stop now. Stop when we've allocated
4832 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4833 * the transaction may get too big.
4835 if (bno
>= end
|| n
>= *nmap
|| bma
.nallocs
>= *nmap
)
4838 /* Else go on to the next record. */
4840 if (++bma
.idx
< xfs_iext_count(ifp
)) {
4841 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
.idx
),
4849 * Transform from btree to extents, give it cur.
4851 if (xfs_bmap_wants_extents(ip
, whichfork
)) {
4852 int tmp_logflags
= 0;
4855 error
= xfs_bmap_btree_to_extents(tp
, ip
, bma
.cur
,
4856 &tmp_logflags
, whichfork
);
4857 bma
.logflags
|= tmp_logflags
;
4862 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
||
4863 XFS_IFORK_NEXTENTS(ip
, whichfork
) >
4864 XFS_IFORK_MAXEXT(ip
, whichfork
));
4868 * Log everything. Do this after conversion, there's no point in
4869 * logging the extent records if we've converted to btree format.
4871 if ((bma
.logflags
& xfs_ilog_fext(whichfork
)) &&
4872 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
4873 bma
.logflags
&= ~xfs_ilog_fext(whichfork
);
4874 else if ((bma
.logflags
& xfs_ilog_fbroot(whichfork
)) &&
4875 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)
4876 bma
.logflags
&= ~xfs_ilog_fbroot(whichfork
);
4878 * Log whatever the flags say, even if error. Otherwise we might miss
4879 * detecting a case where the data is changed, there's an error,
4880 * and it's not logged so we don't shutdown when we should.
4883 xfs_trans_log_inode(tp
, ip
, bma
.logflags
);
4887 ASSERT(*firstblock
== NULLFSBLOCK
||
4888 XFS_FSB_TO_AGNO(mp
, *firstblock
) <=
4890 bma
.cur
->bc_private
.b
.firstblock
));
4891 *firstblock
= bma
.cur
->bc_private
.b
.firstblock
;
4893 xfs_btree_del_cursor(bma
.cur
,
4894 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
4897 xfs_bmap_validate_ret(orig_bno
, orig_len
, orig_flags
, orig_mval
,
4903 * When a delalloc extent is split (e.g., due to a hole punch), the original
4904 * indlen reservation must be shared across the two new extents that are left
4907 * Given the original reservation and the worst case indlen for the two new
4908 * extents (as calculated by xfs_bmap_worst_indlen()), split the original
4909 * reservation fairly across the two new extents. If necessary, steal available
4910 * blocks from a deleted extent to make up a reservation deficiency (e.g., if
4911 * ores == 1). The number of stolen blocks is returned. The availability and
4912 * subsequent accounting of stolen blocks is the responsibility of the caller.
4914 static xfs_filblks_t
4915 xfs_bmap_split_indlen(
4916 xfs_filblks_t ores
, /* original res. */
4917 xfs_filblks_t
*indlen1
, /* ext1 worst indlen */
4918 xfs_filblks_t
*indlen2
, /* ext2 worst indlen */
4919 xfs_filblks_t avail
) /* stealable blocks */
4921 xfs_filblks_t len1
= *indlen1
;
4922 xfs_filblks_t len2
= *indlen2
;
4923 xfs_filblks_t nres
= len1
+ len2
; /* new total res. */
4924 xfs_filblks_t stolen
= 0;
4925 xfs_filblks_t resfactor
;
4928 * Steal as many blocks as we can to try and satisfy the worst case
4929 * indlen for both new extents.
4931 if (ores
< nres
&& avail
)
4932 stolen
= XFS_FILBLKS_MIN(nres
- ores
, avail
);
4935 /* nothing else to do if we've satisfied the new reservation */
4940 * We can't meet the total required reservation for the two extents.
4941 * Calculate the percent of the overall shortage between both extents
4942 * and apply this percentage to each of the requested indlen values.
4943 * This distributes the shortage fairly and reduces the chances that one
4944 * of the two extents is left with nothing when extents are repeatedly
4947 resfactor
= (ores
* 100);
4948 do_div(resfactor
, nres
);
4953 ASSERT(len1
+ len2
<= ores
);
4954 ASSERT(len1
< *indlen1
&& len2
< *indlen2
);
4957 * Hand out the remainder to each extent. If one of the two reservations
4958 * is zero, we want to make sure that one gets a block first. The loop
4959 * below starts with len1, so hand len2 a block right off the bat if it
4962 ores
-= (len1
+ len2
);
4963 ASSERT((*indlen1
- len1
) + (*indlen2
- len2
) >= ores
);
4964 if (ores
&& !len2
&& *indlen2
) {
4969 if (len1
< *indlen1
) {
4975 if (len2
< *indlen2
) {
4988 xfs_bmap_del_extent_delay(
4989 struct xfs_inode
*ip
,
4992 struct xfs_bmbt_irec
*got
,
4993 struct xfs_bmbt_irec
*del
)
4995 struct xfs_mount
*mp
= ip
->i_mount
;
4996 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4997 struct xfs_bmbt_irec
new;
4998 int64_t da_old
, da_new
, da_diff
= 0;
4999 xfs_fileoff_t del_endoff
, got_endoff
;
5000 xfs_filblks_t got_indlen
, new_indlen
, stolen
;
5001 int error
= 0, state
= 0;
5004 XFS_STATS_INC(mp
, xs_del_exlist
);
5006 isrt
= (whichfork
== XFS_DATA_FORK
) && XFS_IS_REALTIME_INODE(ip
);
5007 del_endoff
= del
->br_startoff
+ del
->br_blockcount
;
5008 got_endoff
= got
->br_startoff
+ got
->br_blockcount
;
5009 da_old
= startblockval(got
->br_startblock
);
5013 ASSERT(*idx
<= xfs_iext_count(ifp
));
5014 ASSERT(del
->br_blockcount
> 0);
5015 ASSERT(got
->br_startoff
<= del
->br_startoff
);
5016 ASSERT(got_endoff
>= del_endoff
);
5019 int64_t rtexts
= XFS_FSB_TO_B(mp
, del
->br_blockcount
);
5021 do_div(rtexts
, mp
->m_sb
.sb_rextsize
);
5022 xfs_mod_frextents(mp
, rtexts
);
5026 * Update the inode delalloc counter now and wait to update the
5027 * sb counters as we might have to borrow some blocks for the
5028 * indirect block accounting.
5030 error
= xfs_trans_reserve_quota_nblks(NULL
, ip
,
5031 -((long)del
->br_blockcount
), 0,
5032 isrt
? XFS_QMOPT_RES_RTBLKS
: XFS_QMOPT_RES_REGBLKS
);
5035 ip
->i_delayed_blks
-= del
->br_blockcount
;
5037 if (whichfork
== XFS_COW_FORK
)
5038 state
|= BMAP_COWFORK
;
5040 if (got
->br_startoff
== del
->br_startoff
)
5041 state
|= BMAP_LEFT_CONTIG
;
5042 if (got_endoff
== del_endoff
)
5043 state
|= BMAP_RIGHT_CONTIG
;
5045 switch (state
& (BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
)) {
5046 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
5048 * Matches the whole extent. Delete the entry.
5050 xfs_iext_remove(ip
, *idx
, 1, state
);
5053 case BMAP_LEFT_CONTIG
:
5055 * Deleting the first part of the extent.
5057 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
5058 got
->br_startoff
= del_endoff
;
5059 got
->br_blockcount
-= del
->br_blockcount
;
5060 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
,
5061 got
->br_blockcount
), da_old
);
5062 got
->br_startblock
= nullstartblock((int)da_new
);
5063 xfs_bmbt_set_all(xfs_iext_get_ext(ifp
, *idx
), got
);
5064 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
5066 case BMAP_RIGHT_CONTIG
:
5068 * Deleting the last part of the extent.
5070 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
5071 got
->br_blockcount
= got
->br_blockcount
- del
->br_blockcount
;
5072 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
,
5073 got
->br_blockcount
), da_old
);
5074 got
->br_startblock
= nullstartblock((int)da_new
);
5075 xfs_bmbt_set_all(xfs_iext_get_ext(ifp
, *idx
), got
);
5076 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
5080 * Deleting the middle of the extent.
5082 * Distribute the original indlen reservation across the two new
5083 * extents. Steal blocks from the deleted extent if necessary.
5084 * Stealing blocks simply fudges the fdblocks accounting below.
5085 * Warn if either of the new indlen reservations is zero as this
5086 * can lead to delalloc problems.
5088 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
5090 got
->br_blockcount
= del
->br_startoff
- got
->br_startoff
;
5091 got_indlen
= xfs_bmap_worst_indlen(ip
, got
->br_blockcount
);
5093 new.br_blockcount
= got_endoff
- del_endoff
;
5094 new_indlen
= xfs_bmap_worst_indlen(ip
, new.br_blockcount
);
5096 WARN_ON_ONCE(!got_indlen
|| !new_indlen
);
5097 stolen
= xfs_bmap_split_indlen(da_old
, &got_indlen
, &new_indlen
,
5098 del
->br_blockcount
);
5100 got
->br_startblock
= nullstartblock((int)got_indlen
);
5101 xfs_bmbt_set_all(xfs_iext_get_ext(ifp
, *idx
), got
);
5102 trace_xfs_bmap_post_update(ip
, *idx
, 0, _THIS_IP_
);
5104 new.br_startoff
= del_endoff
;
5105 new.br_state
= got
->br_state
;
5106 new.br_startblock
= nullstartblock((int)new_indlen
);
5109 xfs_iext_insert(ip
, *idx
, 1, &new, state
);
5111 da_new
= got_indlen
+ new_indlen
- stolen
;
5112 del
->br_blockcount
-= stolen
;
5116 ASSERT(da_old
>= da_new
);
5117 da_diff
= da_old
- da_new
;
5119 da_diff
+= del
->br_blockcount
;
5121 xfs_mod_fdblocks(mp
, da_diff
, false);
5126 xfs_bmap_del_extent_cow(
5127 struct xfs_inode
*ip
,
5129 struct xfs_bmbt_irec
*got
,
5130 struct xfs_bmbt_irec
*del
)
5132 struct xfs_mount
*mp
= ip
->i_mount
;
5133 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, XFS_COW_FORK
);
5134 struct xfs_bmbt_irec
new;
5135 xfs_fileoff_t del_endoff
, got_endoff
;
5136 int state
= BMAP_COWFORK
;
5138 XFS_STATS_INC(mp
, xs_del_exlist
);
5140 del_endoff
= del
->br_startoff
+ del
->br_blockcount
;
5141 got_endoff
= got
->br_startoff
+ got
->br_blockcount
;
5144 ASSERT(*idx
<= xfs_iext_count(ifp
));
5145 ASSERT(del
->br_blockcount
> 0);
5146 ASSERT(got
->br_startoff
<= del
->br_startoff
);
5147 ASSERT(got_endoff
>= del_endoff
);
5148 ASSERT(!isnullstartblock(got
->br_startblock
));
5150 if (got
->br_startoff
== del
->br_startoff
)
5151 state
|= BMAP_LEFT_CONTIG
;
5152 if (got_endoff
== del_endoff
)
5153 state
|= BMAP_RIGHT_CONTIG
;
5155 switch (state
& (BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
)) {
5156 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
5158 * Matches the whole extent. Delete the entry.
5160 xfs_iext_remove(ip
, *idx
, 1, state
);
5163 case BMAP_LEFT_CONTIG
:
5165 * Deleting the first part of the extent.
5167 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
5168 got
->br_startoff
= del_endoff
;
5169 got
->br_blockcount
-= del
->br_blockcount
;
5170 got
->br_startblock
= del
->br_startblock
+ del
->br_blockcount
;
5171 xfs_bmbt_set_all(xfs_iext_get_ext(ifp
, *idx
), got
);
5172 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
5174 case BMAP_RIGHT_CONTIG
:
5176 * Deleting the last part of the extent.
5178 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
5179 got
->br_blockcount
-= del
->br_blockcount
;
5180 xfs_bmbt_set_all(xfs_iext_get_ext(ifp
, *idx
), got
);
5181 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
5185 * Deleting the middle of the extent.
5187 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
5188 got
->br_blockcount
= del
->br_startoff
- got
->br_startoff
;
5189 xfs_bmbt_set_all(xfs_iext_get_ext(ifp
, *idx
), got
);
5190 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
5192 new.br_startoff
= del_endoff
;
5193 new.br_blockcount
= got_endoff
- del_endoff
;
5194 new.br_state
= got
->br_state
;
5195 new.br_startblock
= del
->br_startblock
+ del
->br_blockcount
;
5198 xfs_iext_insert(ip
, *idx
, 1, &new, state
);
5204 * Called by xfs_bmapi to update file extent records and the btree
5205 * after removing space (or undoing a delayed allocation).
5207 STATIC
int /* error */
5208 xfs_bmap_del_extent(
5209 xfs_inode_t
*ip
, /* incore inode pointer */
5210 xfs_trans_t
*tp
, /* current transaction pointer */
5211 xfs_extnum_t
*idx
, /* extent number to update/delete */
5212 struct xfs_defer_ops
*dfops
, /* list of extents to be freed */
5213 xfs_btree_cur_t
*cur
, /* if null, not a btree */
5214 xfs_bmbt_irec_t
*del
, /* data to remove from extents */
5215 int *logflagsp
, /* inode logging flags */
5216 int whichfork
, /* data or attr fork */
5217 int bflags
) /* bmapi flags */
5219 xfs_filblks_t da_new
; /* new delay-alloc indirect blocks */
5220 xfs_filblks_t da_old
; /* old delay-alloc indirect blocks */
5221 xfs_fsblock_t del_endblock
=0; /* first block past del */
5222 xfs_fileoff_t del_endoff
; /* first offset past del */
5223 int delay
; /* current block is delayed allocated */
5224 int do_fx
; /* free extent at end of routine */
5225 xfs_bmbt_rec_host_t
*ep
; /* current extent entry pointer */
5226 int error
; /* error return value */
5227 int flags
; /* inode logging flags */
5228 xfs_bmbt_irec_t got
; /* current extent entry */
5229 xfs_fileoff_t got_endoff
; /* first offset past got */
5230 int i
; /* temp state */
5231 xfs_ifork_t
*ifp
; /* inode fork pointer */
5232 xfs_mount_t
*mp
; /* mount structure */
5233 xfs_filblks_t nblks
; /* quota/sb block count */
5234 xfs_bmbt_irec_t
new; /* new record to be inserted */
5236 uint qfield
; /* quota field to update */
5237 xfs_filblks_t temp
; /* for indirect length calculations */
5238 xfs_filblks_t temp2
; /* for indirect length calculations */
5242 XFS_STATS_INC(mp
, xs_del_exlist
);
5244 if (whichfork
== XFS_ATTR_FORK
)
5245 state
|= BMAP_ATTRFORK
;
5246 else if (whichfork
== XFS_COW_FORK
)
5247 state
|= BMAP_COWFORK
;
5249 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5250 ASSERT((*idx
>= 0) && (*idx
< xfs_iext_count(ifp
)));
5251 ASSERT(del
->br_blockcount
> 0);
5252 ep
= xfs_iext_get_ext(ifp
, *idx
);
5253 xfs_bmbt_get_all(ep
, &got
);
5254 ASSERT(got
.br_startoff
<= del
->br_startoff
);
5255 del_endoff
= del
->br_startoff
+ del
->br_blockcount
;
5256 got_endoff
= got
.br_startoff
+ got
.br_blockcount
;
5257 ASSERT(got_endoff
>= del_endoff
);
5258 delay
= isnullstartblock(got
.br_startblock
);
5259 ASSERT(isnullstartblock(del
->br_startblock
) == delay
);
5264 * If deleting a real allocation, must free up the disk space.
5267 flags
= XFS_ILOG_CORE
;
5269 * Realtime allocation. Free it and record di_nblocks update.
5271 if (whichfork
== XFS_DATA_FORK
&& XFS_IS_REALTIME_INODE(ip
)) {
5275 ASSERT(do_mod(del
->br_blockcount
,
5276 mp
->m_sb
.sb_rextsize
) == 0);
5277 ASSERT(do_mod(del
->br_startblock
,
5278 mp
->m_sb
.sb_rextsize
) == 0);
5279 bno
= del
->br_startblock
;
5280 len
= del
->br_blockcount
;
5281 do_div(bno
, mp
->m_sb
.sb_rextsize
);
5282 do_div(len
, mp
->m_sb
.sb_rextsize
);
5283 error
= xfs_rtfree_extent(tp
, bno
, (xfs_extlen_t
)len
);
5287 nblks
= len
* mp
->m_sb
.sb_rextsize
;
5288 qfield
= XFS_TRANS_DQ_RTBCOUNT
;
5291 * Ordinary allocation.
5295 nblks
= del
->br_blockcount
;
5296 qfield
= XFS_TRANS_DQ_BCOUNT
;
5299 * Set up del_endblock and cur for later.
5301 del_endblock
= del
->br_startblock
+ del
->br_blockcount
;
5303 if ((error
= xfs_bmbt_lookup_eq(cur
, got
.br_startoff
,
5304 got
.br_startblock
, got
.br_blockcount
,
5307 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
5309 da_old
= da_new
= 0;
5311 da_old
= startblockval(got
.br_startblock
);
5318 * Set flag value to use in switch statement.
5319 * Left-contig is 2, right-contig is 1.
5321 switch (((got
.br_startoff
== del
->br_startoff
) << 1) |
5322 (got_endoff
== del_endoff
)) {
5325 * Matches the whole extent. Delete the entry.
5327 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
5328 xfs_iext_remove(ip
, *idx
, 1,
5329 whichfork
== XFS_ATTR_FORK
? BMAP_ATTRFORK
: 0);
5334 XFS_IFORK_NEXT_SET(ip
, whichfork
,
5335 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
5336 flags
|= XFS_ILOG_CORE
;
5338 flags
|= xfs_ilog_fext(whichfork
);
5341 if ((error
= xfs_btree_delete(cur
, &i
)))
5343 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
5348 * Deleting the first part of the extent.
5350 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
5351 xfs_bmbt_set_startoff(ep
, del_endoff
);
5352 temp
= got
.br_blockcount
- del
->br_blockcount
;
5353 xfs_bmbt_set_blockcount(ep
, temp
);
5355 temp
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
5357 xfs_bmbt_set_startblock(ep
, nullstartblock((int)temp
));
5358 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
5362 xfs_bmbt_set_startblock(ep
, del_endblock
);
5363 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
5365 flags
|= xfs_ilog_fext(whichfork
);
5368 if ((error
= xfs_bmbt_update(cur
, del_endoff
, del_endblock
,
5369 got
.br_blockcount
- del
->br_blockcount
,
5376 * Deleting the last part of the extent.
5378 temp
= got
.br_blockcount
- del
->br_blockcount
;
5379 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
5380 xfs_bmbt_set_blockcount(ep
, temp
);
5382 temp
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
5384 xfs_bmbt_set_startblock(ep
, nullstartblock((int)temp
));
5385 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
5389 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
5391 flags
|= xfs_ilog_fext(whichfork
);
5394 if ((error
= xfs_bmbt_update(cur
, got
.br_startoff
,
5396 got
.br_blockcount
- del
->br_blockcount
,
5403 * Deleting the middle of the extent.
5405 temp
= del
->br_startoff
- got
.br_startoff
;
5406 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
5407 xfs_bmbt_set_blockcount(ep
, temp
);
5408 new.br_startoff
= del_endoff
;
5409 temp2
= got_endoff
- del_endoff
;
5410 new.br_blockcount
= temp2
;
5411 new.br_state
= got
.br_state
;
5413 new.br_startblock
= del_endblock
;
5414 flags
|= XFS_ILOG_CORE
;
5416 if ((error
= xfs_bmbt_update(cur
,
5418 got
.br_startblock
, temp
,
5421 if ((error
= xfs_btree_increment(cur
, 0, &i
)))
5423 cur
->bc_rec
.b
= new;
5424 error
= xfs_btree_insert(cur
, &i
);
5425 if (error
&& error
!= -ENOSPC
)
5428 * If get no-space back from btree insert,
5429 * it tried a split, and we have a zero
5430 * block reservation.
5431 * Fix up our state and return the error.
5433 if (error
== -ENOSPC
) {
5435 * Reset the cursor, don't trust
5436 * it after any insert operation.
5438 if ((error
= xfs_bmbt_lookup_eq(cur
,
5443 XFS_WANT_CORRUPTED_GOTO(mp
,
5446 * Update the btree record back
5447 * to the original value.
5449 if ((error
= xfs_bmbt_update(cur
,
5456 * Reset the extent record back
5457 * to the original value.
5459 xfs_bmbt_set_blockcount(ep
,
5465 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
5467 flags
|= xfs_ilog_fext(whichfork
);
5468 XFS_IFORK_NEXT_SET(ip
, whichfork
,
5469 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
5471 xfs_filblks_t stolen
;
5472 ASSERT(whichfork
== XFS_DATA_FORK
);
5475 * Distribute the original indlen reservation across the
5476 * two new extents. Steal blocks from the deleted extent
5477 * if necessary. Stealing blocks simply fudges the
5478 * fdblocks accounting in xfs_bunmapi().
5480 temp
= xfs_bmap_worst_indlen(ip
, got
.br_blockcount
);
5481 temp2
= xfs_bmap_worst_indlen(ip
, new.br_blockcount
);
5482 stolen
= xfs_bmap_split_indlen(da_old
, &temp
, &temp2
,
5483 del
->br_blockcount
);
5484 da_new
= temp
+ temp2
- stolen
;
5485 del
->br_blockcount
-= stolen
;
5488 * Set the reservation for each extent. Warn if either
5489 * is zero as this can lead to delalloc problems.
5491 WARN_ON_ONCE(!temp
|| !temp2
);
5492 xfs_bmbt_set_startblock(ep
, nullstartblock((int)temp
));
5493 new.br_startblock
= nullstartblock((int)temp2
);
5495 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
5496 xfs_iext_insert(ip
, *idx
+ 1, 1, &new, state
);
5501 /* remove reverse mapping */
5503 error
= xfs_rmap_unmap_extent(mp
, dfops
, ip
, whichfork
, del
);
5509 * If we need to, add to list of extents to delete.
5511 if (do_fx
&& !(bflags
& XFS_BMAPI_REMAP
)) {
5512 if (xfs_is_reflink_inode(ip
) && whichfork
== XFS_DATA_FORK
) {
5513 error
= xfs_refcount_decrease_extent(mp
, dfops
, del
);
5517 xfs_bmap_add_free(mp
, dfops
, del
->br_startblock
,
5518 del
->br_blockcount
, NULL
);
5522 * Adjust inode # blocks in the file.
5525 ip
->i_d
.di_nblocks
-= nblks
;
5527 * Adjust quota data.
5529 if (qfield
&& !(bflags
& XFS_BMAPI_REMAP
))
5530 xfs_trans_mod_dquot_byino(tp
, ip
, qfield
, (long)-nblks
);
5533 * Account for change in delayed indirect blocks.
5534 * Nothing to do for disk quota accounting here.
5536 ASSERT(da_old
>= da_new
);
5537 if (da_old
> da_new
)
5538 xfs_mod_fdblocks(mp
, (int64_t)(da_old
- da_new
), false);
5545 * Unmap (remove) blocks from a file.
5546 * If nexts is nonzero then the number of extents to remove is limited to
5547 * that value. If not all extents in the block range can be removed then
5552 xfs_trans_t
*tp
, /* transaction pointer */
5553 struct xfs_inode
*ip
, /* incore inode */
5554 xfs_fileoff_t bno
, /* starting offset to unmap */
5555 xfs_filblks_t
*rlen
, /* i/o: amount remaining */
5556 int flags
, /* misc flags */
5557 xfs_extnum_t nexts
, /* number of extents max */
5558 xfs_fsblock_t
*firstblock
, /* first allocated block
5559 controls a.g. for allocs */
5560 struct xfs_defer_ops
*dfops
) /* i/o: deferred updates */
5562 xfs_btree_cur_t
*cur
; /* bmap btree cursor */
5563 xfs_bmbt_irec_t del
; /* extent being deleted */
5564 int eof
; /* is deleting at eof */
5565 xfs_bmbt_rec_host_t
*ep
; /* extent record pointer */
5566 int error
; /* error return value */
5567 xfs_extnum_t extno
; /* extent number in list */
5568 xfs_bmbt_irec_t got
; /* current extent record */
5569 xfs_ifork_t
*ifp
; /* inode fork pointer */
5570 int isrt
; /* freeing in rt area */
5571 xfs_extnum_t lastx
; /* last extent index used */
5572 int logflags
; /* transaction logging flags */
5573 xfs_extlen_t mod
; /* rt extent offset */
5574 xfs_mount_t
*mp
; /* mount structure */
5575 xfs_bmbt_irec_t prev
; /* previous extent record */
5576 xfs_fileoff_t start
; /* first file offset deleted */
5577 int tmp_logflags
; /* partial logging flags */
5578 int wasdel
; /* was a delayed alloc extent */
5579 int whichfork
; /* data or attribute fork */
5581 xfs_filblks_t len
= *rlen
; /* length to unmap in file */
5582 xfs_fileoff_t max_len
;
5583 xfs_agnumber_t prev_agno
= NULLAGNUMBER
, agno
;
5585 trace_xfs_bunmap(ip
, bno
, len
, flags
, _RET_IP_
);
5587 whichfork
= xfs_bmapi_whichfork(flags
);
5588 ASSERT(whichfork
!= XFS_COW_FORK
);
5589 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5591 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
5592 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)) {
5593 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW
,
5595 return -EFSCORRUPTED
;
5598 if (XFS_FORCED_SHUTDOWN(mp
))
5601 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
5606 * Guesstimate how many blocks we can unmap without running the risk of
5607 * blowing out the transaction with a mix of EFIs and reflink
5610 if (xfs_is_reflink_inode(ip
) && whichfork
== XFS_DATA_FORK
)
5611 max_len
= min(len
, xfs_refcount_max_unmap(tp
->t_log_res
));
5615 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
5616 (error
= xfs_iread_extents(tp
, ip
, whichfork
)))
5618 if (xfs_iext_count(ifp
) == 0) {
5622 XFS_STATS_INC(mp
, xs_blk_unmap
);
5623 isrt
= (whichfork
== XFS_DATA_FORK
) && XFS_IS_REALTIME_INODE(ip
);
5625 bno
= start
+ len
- 1;
5626 ep
= xfs_bmap_search_extents(ip
, bno
, whichfork
, &eof
, &lastx
, &got
,
5630 * Check to see if the given block number is past the end of the
5631 * file, back up to the last block if so...
5634 ep
= xfs_iext_get_ext(ifp
, --lastx
);
5635 xfs_bmbt_get_all(ep
, &got
);
5636 bno
= got
.br_startoff
+ got
.br_blockcount
- 1;
5639 if (ifp
->if_flags
& XFS_IFBROOT
) {
5640 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
);
5641 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
5642 cur
->bc_private
.b
.firstblock
= *firstblock
;
5643 cur
->bc_private
.b
.dfops
= dfops
;
5644 cur
->bc_private
.b
.flags
= 0;
5650 * Synchronize by locking the bitmap inode.
5652 xfs_ilock(mp
->m_rbmip
, XFS_ILOCK_EXCL
|XFS_ILOCK_RTBITMAP
);
5653 xfs_trans_ijoin(tp
, mp
->m_rbmip
, XFS_ILOCK_EXCL
);
5654 xfs_ilock(mp
->m_rsumip
, XFS_ILOCK_EXCL
|XFS_ILOCK_RTSUM
);
5655 xfs_trans_ijoin(tp
, mp
->m_rsumip
, XFS_ILOCK_EXCL
);
5659 while (bno
!= (xfs_fileoff_t
)-1 && bno
>= start
&& lastx
>= 0 &&
5660 (nexts
== 0 || extno
< nexts
) && max_len
> 0) {
5662 * Is the found extent after a hole in which bno lives?
5663 * Just back up to the previous extent, if so.
5665 if (got
.br_startoff
> bno
) {
5668 ep
= xfs_iext_get_ext(ifp
, lastx
);
5669 xfs_bmbt_get_all(ep
, &got
);
5672 * Is the last block of this extent before the range
5673 * we're supposed to delete? If so, we're done.
5675 bno
= XFS_FILEOFF_MIN(bno
,
5676 got
.br_startoff
+ got
.br_blockcount
- 1);
5680 * Then deal with the (possibly delayed) allocated space
5685 wasdel
= isnullstartblock(del
.br_startblock
);
5688 * Make sure we don't touch multiple AGF headers out of order
5689 * in a single transaction, as that could cause AB-BA deadlocks.
5691 if (!wasdel
&& !isrt
) {
5692 agno
= XFS_FSB_TO_AGNO(mp
, del
.br_startblock
);
5693 if (prev_agno
!= NULLAGNUMBER
&& prev_agno
> agno
)
5697 if (got
.br_startoff
< start
) {
5698 del
.br_startoff
= start
;
5699 del
.br_blockcount
-= start
- got
.br_startoff
;
5701 del
.br_startblock
+= start
- got
.br_startoff
;
5703 if (del
.br_startoff
+ del
.br_blockcount
> bno
+ 1)
5704 del
.br_blockcount
= bno
+ 1 - del
.br_startoff
;
5706 /* How much can we safely unmap? */
5707 if (max_len
< del
.br_blockcount
) {
5708 del
.br_startoff
+= del
.br_blockcount
- max_len
;
5710 del
.br_startblock
+= del
.br_blockcount
- max_len
;
5711 del
.br_blockcount
= max_len
;
5714 sum
= del
.br_startblock
+ del
.br_blockcount
;
5716 (mod
= do_mod(sum
, mp
->m_sb
.sb_rextsize
))) {
5718 * Realtime extent not lined up at the end.
5719 * The extent could have been split into written
5720 * and unwritten pieces, or we could just be
5721 * unmapping part of it. But we can't really
5722 * get rid of part of a realtime extent.
5724 if (del
.br_state
== XFS_EXT_UNWRITTEN
||
5725 !xfs_sb_version_hasextflgbit(&mp
->m_sb
)) {
5727 * This piece is unwritten, or we're not
5728 * using unwritten extents. Skip over it.
5731 bno
-= mod
> del
.br_blockcount
?
5732 del
.br_blockcount
: mod
;
5733 if (bno
< got
.br_startoff
) {
5735 xfs_bmbt_get_all(xfs_iext_get_ext(
5741 * It's written, turn it unwritten.
5742 * This is better than zeroing it.
5744 ASSERT(del
.br_state
== XFS_EXT_NORM
);
5745 ASSERT(tp
->t_blk_res
> 0);
5747 * If this spans a realtime extent boundary,
5748 * chop it back to the start of the one we end at.
5750 if (del
.br_blockcount
> mod
) {
5751 del
.br_startoff
+= del
.br_blockcount
- mod
;
5752 del
.br_startblock
+= del
.br_blockcount
- mod
;
5753 del
.br_blockcount
= mod
;
5755 del
.br_state
= XFS_EXT_UNWRITTEN
;
5756 error
= xfs_bmap_add_extent_unwritten_real(tp
, ip
,
5757 whichfork
, &lastx
, &cur
, &del
,
5758 firstblock
, dfops
, &logflags
);
5763 if (isrt
&& (mod
= do_mod(del
.br_startblock
, mp
->m_sb
.sb_rextsize
))) {
5765 * Realtime extent is lined up at the end but not
5766 * at the front. We'll get rid of full extents if
5769 mod
= mp
->m_sb
.sb_rextsize
- mod
;
5770 if (del
.br_blockcount
> mod
) {
5771 del
.br_blockcount
-= mod
;
5772 del
.br_startoff
+= mod
;
5773 del
.br_startblock
+= mod
;
5774 } else if ((del
.br_startoff
== start
&&
5775 (del
.br_state
== XFS_EXT_UNWRITTEN
||
5776 tp
->t_blk_res
== 0)) ||
5777 !xfs_sb_version_hasextflgbit(&mp
->m_sb
)) {
5779 * Can't make it unwritten. There isn't
5780 * a full extent here so just skip it.
5782 ASSERT(bno
>= del
.br_blockcount
);
5783 bno
-= del
.br_blockcount
;
5784 if (got
.br_startoff
> bno
) {
5786 ep
= xfs_iext_get_ext(ifp
,
5788 xfs_bmbt_get_all(ep
, &got
);
5792 } else if (del
.br_state
== XFS_EXT_UNWRITTEN
) {
5794 * This one is already unwritten.
5795 * It must have a written left neighbor.
5796 * Unwrite the killed part of that one and
5800 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
,
5802 ASSERT(prev
.br_state
== XFS_EXT_NORM
);
5803 ASSERT(!isnullstartblock(prev
.br_startblock
));
5804 ASSERT(del
.br_startblock
==
5805 prev
.br_startblock
+ prev
.br_blockcount
);
5806 if (prev
.br_startoff
< start
) {
5807 mod
= start
- prev
.br_startoff
;
5808 prev
.br_blockcount
-= mod
;
5809 prev
.br_startblock
+= mod
;
5810 prev
.br_startoff
= start
;
5812 prev
.br_state
= XFS_EXT_UNWRITTEN
;
5814 error
= xfs_bmap_add_extent_unwritten_real(tp
,
5815 ip
, whichfork
, &lastx
, &cur
,
5816 &prev
, firstblock
, dfops
,
5822 ASSERT(del
.br_state
== XFS_EXT_NORM
);
5823 del
.br_state
= XFS_EXT_UNWRITTEN
;
5824 error
= xfs_bmap_add_extent_unwritten_real(tp
,
5825 ip
, whichfork
, &lastx
, &cur
,
5826 &del
, firstblock
, dfops
,
5835 * If it's the case where the directory code is running
5836 * with no block reservation, and the deleted block is in
5837 * the middle of its extent, and the resulting insert
5838 * of an extent would cause transformation to btree format,
5839 * then reject it. The calling code will then swap
5840 * blocks around instead.
5841 * We have to do this now, rather than waiting for the
5842 * conversion to btree format, since the transaction
5845 if (!wasdel
&& tp
->t_blk_res
== 0 &&
5846 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
&&
5847 XFS_IFORK_NEXTENTS(ip
, whichfork
) >= /* Note the >= */
5848 XFS_IFORK_MAXEXT(ip
, whichfork
) &&
5849 del
.br_startoff
> got
.br_startoff
&&
5850 del
.br_startoff
+ del
.br_blockcount
<
5851 got
.br_startoff
+ got
.br_blockcount
) {
5857 * Unreserve quota and update realtime free space, if
5858 * appropriate. If delayed allocation, update the inode delalloc
5859 * counter now and wait to update the sb counters as
5860 * xfs_bmap_del_extent() might need to borrow some blocks.
5863 ASSERT(startblockval(del
.br_startblock
) > 0);
5865 xfs_filblks_t rtexts
;
5867 rtexts
= XFS_FSB_TO_B(mp
, del
.br_blockcount
);
5868 do_div(rtexts
, mp
->m_sb
.sb_rextsize
);
5869 xfs_mod_frextents(mp
, (int64_t)rtexts
);
5870 (void)xfs_trans_reserve_quota_nblks(NULL
,
5871 ip
, -((long)del
.br_blockcount
), 0,
5872 XFS_QMOPT_RES_RTBLKS
);
5874 (void)xfs_trans_reserve_quota_nblks(NULL
,
5875 ip
, -((long)del
.br_blockcount
), 0,
5876 XFS_QMOPT_RES_REGBLKS
);
5878 ip
->i_delayed_blks
-= del
.br_blockcount
;
5880 cur
->bc_private
.b
.flags
|=
5881 XFS_BTCUR_BPRV_WASDEL
;
5883 cur
->bc_private
.b
.flags
&= ~XFS_BTCUR_BPRV_WASDEL
;
5885 error
= xfs_bmap_del_extent(ip
, tp
, &lastx
, dfops
, cur
, &del
,
5886 &tmp_logflags
, whichfork
, flags
);
5887 logflags
|= tmp_logflags
;
5891 if (!isrt
&& wasdel
)
5892 xfs_mod_fdblocks(mp
, (int64_t)del
.br_blockcount
, false);
5894 max_len
-= del
.br_blockcount
;
5895 bno
= del
.br_startoff
- 1;
5898 * If not done go on to the next (previous) record.
5900 if (bno
!= (xfs_fileoff_t
)-1 && bno
>= start
) {
5902 ep
= xfs_iext_get_ext(ifp
, lastx
);
5903 if (xfs_bmbt_get_startoff(ep
) > bno
) {
5905 ep
= xfs_iext_get_ext(ifp
,
5908 xfs_bmbt_get_all(ep
, &got
);
5913 if (bno
== (xfs_fileoff_t
)-1 || bno
< start
|| lastx
< 0)
5916 *rlen
= bno
- start
+ 1;
5919 * Convert to a btree if necessary.
5921 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
5922 ASSERT(cur
== NULL
);
5923 error
= xfs_bmap_extents_to_btree(tp
, ip
, firstblock
, dfops
,
5924 &cur
, 0, &tmp_logflags
, whichfork
);
5925 logflags
|= tmp_logflags
;
5930 * transform from btree to extents, give it cur
5932 else if (xfs_bmap_wants_extents(ip
, whichfork
)) {
5933 ASSERT(cur
!= NULL
);
5934 error
= xfs_bmap_btree_to_extents(tp
, ip
, cur
, &tmp_logflags
,
5936 logflags
|= tmp_logflags
;
5941 * transform from extents to local?
5946 * Log everything. Do this after conversion, there's no point in
5947 * logging the extent records if we've converted to btree format.
5949 if ((logflags
& xfs_ilog_fext(whichfork
)) &&
5950 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
5951 logflags
&= ~xfs_ilog_fext(whichfork
);
5952 else if ((logflags
& xfs_ilog_fbroot(whichfork
)) &&
5953 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)
5954 logflags
&= ~xfs_ilog_fbroot(whichfork
);
5956 * Log inode even in the error case, if the transaction
5957 * is dirty we'll need to shut down the filesystem.
5960 xfs_trans_log_inode(tp
, ip
, logflags
);
5963 *firstblock
= cur
->bc_private
.b
.firstblock
;
5964 cur
->bc_private
.b
.allocated
= 0;
5966 xfs_btree_del_cursor(cur
,
5967 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
5972 /* Unmap a range of a file. */
5976 struct xfs_inode
*ip
,
5981 xfs_fsblock_t
*firstblock
,
5982 struct xfs_defer_ops
*dfops
,
5987 error
= __xfs_bunmapi(tp
, ip
, bno
, &len
, flags
, nexts
, firstblock
,
5994 * Determine whether an extent shift can be accomplished by a merge with the
5995 * extent that precedes the target hole of the shift.
5999 struct xfs_bmbt_irec
*left
, /* preceding extent */
6000 struct xfs_bmbt_irec
*got
, /* current extent to shift */
6001 xfs_fileoff_t shift
) /* shift fsb */
6003 xfs_fileoff_t startoff
;
6005 startoff
= got
->br_startoff
- shift
;
6008 * The extent, once shifted, must be adjacent in-file and on-disk with
6009 * the preceding extent.
6011 if ((left
->br_startoff
+ left
->br_blockcount
!= startoff
) ||
6012 (left
->br_startblock
+ left
->br_blockcount
!= got
->br_startblock
) ||
6013 (left
->br_state
!= got
->br_state
) ||
6014 (left
->br_blockcount
+ got
->br_blockcount
> MAXEXTLEN
))
6021 * A bmap extent shift adjusts the file offset of an extent to fill a preceding
6022 * hole in the file. If an extent shift would result in the extent being fully
6023 * adjacent to the extent that currently precedes the hole, we can merge with
6024 * the preceding extent rather than do the shift.
6026 * This function assumes the caller has verified a shift-by-merge is possible
6027 * with the provided extents via xfs_bmse_can_merge().
6031 struct xfs_inode
*ip
,
6033 xfs_fileoff_t shift
, /* shift fsb */
6034 int current_ext
, /* idx of gotp */
6035 struct xfs_bmbt_rec_host
*gotp
, /* extent to shift */
6036 struct xfs_bmbt_rec_host
*leftp
, /* preceding extent */
6037 struct xfs_btree_cur
*cur
,
6038 int *logflags
) /* output */
6040 struct xfs_bmbt_irec got
;
6041 struct xfs_bmbt_irec left
;
6042 xfs_filblks_t blockcount
;
6044 struct xfs_mount
*mp
= ip
->i_mount
;
6046 xfs_bmbt_get_all(gotp
, &got
);
6047 xfs_bmbt_get_all(leftp
, &left
);
6048 blockcount
= left
.br_blockcount
+ got
.br_blockcount
;
6050 ASSERT(xfs_isilocked(ip
, XFS_IOLOCK_EXCL
));
6051 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
6052 ASSERT(xfs_bmse_can_merge(&left
, &got
, shift
));
6055 * Merge the in-core extents. Note that the host record pointers and
6056 * current_ext index are invalid once the extent has been removed via
6057 * xfs_iext_remove().
6059 xfs_bmbt_set_blockcount(leftp
, blockcount
);
6060 xfs_iext_remove(ip
, current_ext
, 1, 0);
6063 * Update the on-disk extent count, the btree if necessary and log the
6066 XFS_IFORK_NEXT_SET(ip
, whichfork
,
6067 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
6068 *logflags
|= XFS_ILOG_CORE
;
6070 *logflags
|= XFS_ILOG_DEXT
;
6074 /* lookup and remove the extent to merge */
6075 error
= xfs_bmbt_lookup_eq(cur
, got
.br_startoff
, got
.br_startblock
,
6076 got
.br_blockcount
, &i
);
6079 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
6081 error
= xfs_btree_delete(cur
, &i
);
6084 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
6086 /* lookup and update size of the previous extent */
6087 error
= xfs_bmbt_lookup_eq(cur
, left
.br_startoff
, left
.br_startblock
,
6088 left
.br_blockcount
, &i
);
6091 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
6093 left
.br_blockcount
= blockcount
;
6095 return xfs_bmbt_update(cur
, left
.br_startoff
, left
.br_startblock
,
6096 left
.br_blockcount
, left
.br_state
);
6100 * Shift a single extent.
6104 struct xfs_inode
*ip
,
6106 xfs_fileoff_t offset_shift_fsb
,
6108 struct xfs_bmbt_rec_host
*gotp
,
6109 struct xfs_btree_cur
*cur
,
6111 enum shift_direction direction
,
6112 struct xfs_defer_ops
*dfops
)
6114 struct xfs_ifork
*ifp
;
6115 struct xfs_mount
*mp
;
6116 xfs_fileoff_t startoff
;
6117 struct xfs_bmbt_rec_host
*adj_irecp
;
6118 struct xfs_bmbt_irec got
;
6119 struct xfs_bmbt_irec adj_irec
;
6125 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
6126 total_extents
= xfs_iext_count(ifp
);
6128 xfs_bmbt_get_all(gotp
, &got
);
6130 /* delalloc extents should be prevented by caller */
6131 XFS_WANT_CORRUPTED_RETURN(mp
, !isnullstartblock(got
.br_startblock
));
6133 if (direction
== SHIFT_LEFT
) {
6134 startoff
= got
.br_startoff
- offset_shift_fsb
;
6137 * Check for merge if we've got an extent to the left,
6138 * otherwise make sure there's enough room at the start
6139 * of the file for the shift.
6141 if (!*current_ext
) {
6142 if (got
.br_startoff
< offset_shift_fsb
)
6144 goto update_current_ext
;
6147 * grab the left extent and check for a large
6150 adj_irecp
= xfs_iext_get_ext(ifp
, *current_ext
- 1);
6151 xfs_bmbt_get_all(adj_irecp
, &adj_irec
);
6154 adj_irec
.br_startoff
+ adj_irec
.br_blockcount
)
6157 /* check whether to merge the extent or shift it down */
6158 if (xfs_bmse_can_merge(&adj_irec
, &got
,
6159 offset_shift_fsb
)) {
6160 error
= xfs_bmse_merge(ip
, whichfork
, offset_shift_fsb
,
6161 *current_ext
, gotp
, adj_irecp
,
6169 startoff
= got
.br_startoff
+ offset_shift_fsb
;
6170 /* nothing to move if this is the last extent */
6171 if (*current_ext
>= (total_extents
- 1))
6172 goto update_current_ext
;
6174 * If this is not the last extent in the file, make sure there
6175 * is enough room between current extent and next extent for
6176 * accommodating the shift.
6178 adj_irecp
= xfs_iext_get_ext(ifp
, *current_ext
+ 1);
6179 xfs_bmbt_get_all(adj_irecp
, &adj_irec
);
6180 if (startoff
+ got
.br_blockcount
> adj_irec
.br_startoff
)
6183 * Unlike a left shift (which involves a hole punch),
6184 * a right shift does not modify extent neighbors
6185 * in any way. We should never find mergeable extents
6186 * in this scenario. Check anyways and warn if we
6187 * encounter two extents that could be one.
6189 if (xfs_bmse_can_merge(&got
, &adj_irec
, offset_shift_fsb
))
6193 * Increment the extent index for the next iteration, update the start
6194 * offset of the in-core extent and update the btree if applicable.
6197 if (direction
== SHIFT_LEFT
)
6201 xfs_bmbt_set_startoff(gotp
, startoff
);
6202 *logflags
|= XFS_ILOG_CORE
;
6205 *logflags
|= XFS_ILOG_DEXT
;
6209 error
= xfs_bmbt_lookup_eq(cur
, got
.br_startoff
, got
.br_startblock
,
6210 got
.br_blockcount
, &i
);
6213 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
6215 got
.br_startoff
= startoff
;
6216 error
= xfs_bmbt_update(cur
, got
.br_startoff
, got
.br_startblock
,
6217 got
.br_blockcount
, got
.br_state
);
6222 /* update reverse mapping */
6223 error
= xfs_rmap_unmap_extent(mp
, dfops
, ip
, whichfork
, &adj_irec
);
6226 adj_irec
.br_startoff
= startoff
;
6227 return xfs_rmap_map_extent(mp
, dfops
, ip
, whichfork
, &adj_irec
);
6231 * Shift extent records to the left/right to cover/create a hole.
6233 * The maximum number of extents to be shifted in a single operation is
6234 * @num_exts. @stop_fsb specifies the file offset at which to stop shift and the
6235 * file offset where we've left off is returned in @next_fsb. @offset_shift_fsb
6236 * is the length by which each extent is shifted. If there is no hole to shift
6237 * the extents into, this will be considered invalid operation and we abort
6241 xfs_bmap_shift_extents(
6242 struct xfs_trans
*tp
,
6243 struct xfs_inode
*ip
,
6244 xfs_fileoff_t
*next_fsb
,
6245 xfs_fileoff_t offset_shift_fsb
,
6247 xfs_fileoff_t stop_fsb
,
6248 xfs_fsblock_t
*firstblock
,
6249 struct xfs_defer_ops
*dfops
,
6250 enum shift_direction direction
,
6253 struct xfs_btree_cur
*cur
= NULL
;
6254 struct xfs_bmbt_rec_host
*gotp
;
6255 struct xfs_bmbt_irec got
;
6256 struct xfs_mount
*mp
= ip
->i_mount
;
6257 struct xfs_ifork
*ifp
;
6258 xfs_extnum_t nexts
= 0;
6259 xfs_extnum_t current_ext
;
6260 xfs_extnum_t total_extents
;
6261 xfs_extnum_t stop_extent
;
6263 int whichfork
= XFS_DATA_FORK
;
6266 if (unlikely(XFS_TEST_ERROR(
6267 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
6268 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
6269 mp
, XFS_ERRTAG_BMAPIFORMAT
, XFS_RANDOM_BMAPIFORMAT
))) {
6270 XFS_ERROR_REPORT("xfs_bmap_shift_extents",
6271 XFS_ERRLEVEL_LOW
, mp
);
6272 return -EFSCORRUPTED
;
6275 if (XFS_FORCED_SHUTDOWN(mp
))
6278 ASSERT(xfs_isilocked(ip
, XFS_IOLOCK_EXCL
));
6279 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
6280 ASSERT(direction
== SHIFT_LEFT
|| direction
== SHIFT_RIGHT
);
6281 ASSERT(*next_fsb
!= NULLFSBLOCK
|| direction
== SHIFT_RIGHT
);
6283 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
6284 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
6285 /* Read in all the extents */
6286 error
= xfs_iread_extents(tp
, ip
, whichfork
);
6291 if (ifp
->if_flags
& XFS_IFBROOT
) {
6292 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
6293 cur
->bc_private
.b
.firstblock
= *firstblock
;
6294 cur
->bc_private
.b
.dfops
= dfops
;
6295 cur
->bc_private
.b
.flags
= 0;
6299 * There may be delalloc extents in the data fork before the range we
6300 * are collapsing out, so we cannot use the count of real extents here.
6301 * Instead we have to calculate it from the incore fork.
6303 total_extents
= xfs_iext_count(ifp
);
6304 if (total_extents
== 0) {
6310 * In case of first right shift, we need to initialize next_fsb
6312 if (*next_fsb
== NULLFSBLOCK
) {
6313 gotp
= xfs_iext_get_ext(ifp
, total_extents
- 1);
6314 xfs_bmbt_get_all(gotp
, &got
);
6315 *next_fsb
= got
.br_startoff
;
6316 if (stop_fsb
> *next_fsb
) {
6322 /* Lookup the extent index at which we have to stop */
6323 if (direction
== SHIFT_RIGHT
) {
6324 gotp
= xfs_iext_bno_to_ext(ifp
, stop_fsb
, &stop_extent
);
6325 /* Make stop_extent exclusive of shift range */
6328 stop_extent
= total_extents
;
6331 * Look up the extent index for the fsb where we start shifting. We can
6332 * henceforth iterate with current_ext as extent list changes are locked
6335 * gotp can be null in 2 cases: 1) if there are no extents or 2)
6336 * *next_fsb lies in a hole beyond which there are no extents. Either
6339 gotp
= xfs_iext_bno_to_ext(ifp
, *next_fsb
, ¤t_ext
);
6345 /* some sanity checking before we finally start shifting extents */
6346 if ((direction
== SHIFT_LEFT
&& current_ext
>= stop_extent
) ||
6347 (direction
== SHIFT_RIGHT
&& current_ext
<= stop_extent
)) {
6352 while (nexts
++ < num_exts
) {
6353 error
= xfs_bmse_shift_one(ip
, whichfork
, offset_shift_fsb
,
6354 ¤t_ext
, gotp
, cur
, &logflags
,
6359 * If there was an extent merge during the shift, the extent
6360 * count can change. Update the total and grade the next record.
6362 if (direction
== SHIFT_LEFT
) {
6363 total_extents
= xfs_iext_count(ifp
);
6364 stop_extent
= total_extents
;
6367 if (current_ext
== stop_extent
) {
6369 *next_fsb
= NULLFSBLOCK
;
6372 gotp
= xfs_iext_get_ext(ifp
, current_ext
);
6376 xfs_bmbt_get_all(gotp
, &got
);
6377 *next_fsb
= got
.br_startoff
;
6382 xfs_btree_del_cursor(cur
,
6383 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
6386 xfs_trans_log_inode(tp
, ip
, logflags
);
6392 * Splits an extent into two extents at split_fsb block such that it is
6393 * the first block of the current_ext. @current_ext is a target extent
6394 * to be split. @split_fsb is a block where the extents is split.
6395 * If split_fsb lies in a hole or the first block of extents, just return 0.
6398 xfs_bmap_split_extent_at(
6399 struct xfs_trans
*tp
,
6400 struct xfs_inode
*ip
,
6401 xfs_fileoff_t split_fsb
,
6402 xfs_fsblock_t
*firstfsb
,
6403 struct xfs_defer_ops
*dfops
)
6405 int whichfork
= XFS_DATA_FORK
;
6406 struct xfs_btree_cur
*cur
= NULL
;
6407 struct xfs_bmbt_rec_host
*gotp
;
6408 struct xfs_bmbt_irec got
;
6409 struct xfs_bmbt_irec
new; /* split extent */
6410 struct xfs_mount
*mp
= ip
->i_mount
;
6411 struct xfs_ifork
*ifp
;
6412 xfs_fsblock_t gotblkcnt
; /* new block count for got */
6413 xfs_extnum_t current_ext
;
6418 if (unlikely(XFS_TEST_ERROR(
6419 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
6420 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
6421 mp
, XFS_ERRTAG_BMAPIFORMAT
, XFS_RANDOM_BMAPIFORMAT
))) {
6422 XFS_ERROR_REPORT("xfs_bmap_split_extent_at",
6423 XFS_ERRLEVEL_LOW
, mp
);
6424 return -EFSCORRUPTED
;
6427 if (XFS_FORCED_SHUTDOWN(mp
))
6430 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
6431 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
6432 /* Read in all the extents */
6433 error
= xfs_iread_extents(tp
, ip
, whichfork
);
6439 * gotp can be null in 2 cases: 1) if there are no extents
6440 * or 2) split_fsb lies in a hole beyond which there are
6441 * no extents. Either way, we are done.
6443 gotp
= xfs_iext_bno_to_ext(ifp
, split_fsb
, ¤t_ext
);
6447 xfs_bmbt_get_all(gotp
, &got
);
6450 * Check split_fsb lies in a hole or the start boundary offset
6453 if (got
.br_startoff
>= split_fsb
)
6456 gotblkcnt
= split_fsb
- got
.br_startoff
;
6457 new.br_startoff
= split_fsb
;
6458 new.br_startblock
= got
.br_startblock
+ gotblkcnt
;
6459 new.br_blockcount
= got
.br_blockcount
- gotblkcnt
;
6460 new.br_state
= got
.br_state
;
6462 if (ifp
->if_flags
& XFS_IFBROOT
) {
6463 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
6464 cur
->bc_private
.b
.firstblock
= *firstfsb
;
6465 cur
->bc_private
.b
.dfops
= dfops
;
6466 cur
->bc_private
.b
.flags
= 0;
6467 error
= xfs_bmbt_lookup_eq(cur
, got
.br_startoff
,
6473 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, del_cursor
);
6476 xfs_bmbt_set_blockcount(gotp
, gotblkcnt
);
6477 got
.br_blockcount
= gotblkcnt
;
6479 logflags
= XFS_ILOG_CORE
;
6481 error
= xfs_bmbt_update(cur
, got
.br_startoff
,
6488 logflags
|= XFS_ILOG_DEXT
;
6490 /* Add new extent */
6492 xfs_iext_insert(ip
, current_ext
, 1, &new, 0);
6493 XFS_IFORK_NEXT_SET(ip
, whichfork
,
6494 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
6497 error
= xfs_bmbt_lookup_eq(cur
, new.br_startoff
,
6498 new.br_startblock
, new.br_blockcount
,
6502 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, del_cursor
);
6503 cur
->bc_rec
.b
.br_state
= new.br_state
;
6505 error
= xfs_btree_insert(cur
, &i
);
6508 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, del_cursor
);
6512 * Convert to a btree if necessary.
6514 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
6515 int tmp_logflags
; /* partial log flag return val */
6517 ASSERT(cur
== NULL
);
6518 error
= xfs_bmap_extents_to_btree(tp
, ip
, firstfsb
, dfops
,
6519 &cur
, 0, &tmp_logflags
, whichfork
);
6520 logflags
|= tmp_logflags
;
6525 cur
->bc_private
.b
.allocated
= 0;
6526 xfs_btree_del_cursor(cur
,
6527 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
6531 xfs_trans_log_inode(tp
, ip
, logflags
);
6536 xfs_bmap_split_extent(
6537 struct xfs_inode
*ip
,
6538 xfs_fileoff_t split_fsb
)
6540 struct xfs_mount
*mp
= ip
->i_mount
;
6541 struct xfs_trans
*tp
;
6542 struct xfs_defer_ops dfops
;
6543 xfs_fsblock_t firstfsb
;
6546 error
= xfs_trans_alloc(mp
, &M_RES(mp
)->tr_write
,
6547 XFS_DIOSTRAT_SPACE_RES(mp
, 0), 0, 0, &tp
);
6551 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
6552 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
);
6554 xfs_defer_init(&dfops
, &firstfsb
);
6556 error
= xfs_bmap_split_extent_at(tp
, ip
, split_fsb
,
6561 error
= xfs_defer_finish(&tp
, &dfops
, NULL
);
6565 return xfs_trans_commit(tp
);
6568 xfs_defer_cancel(&dfops
);
6569 xfs_trans_cancel(tp
);
6573 /* Deferred mapping is only for real extents in the data fork. */
6575 xfs_bmap_is_update_needed(
6576 struct xfs_bmbt_irec
*bmap
)
6578 return bmap
->br_startblock
!= HOLESTARTBLOCK
&&
6579 bmap
->br_startblock
!= DELAYSTARTBLOCK
;
6582 /* Record a bmap intent. */
6585 struct xfs_mount
*mp
,
6586 struct xfs_defer_ops
*dfops
,
6587 enum xfs_bmap_intent_type type
,
6588 struct xfs_inode
*ip
,
6590 struct xfs_bmbt_irec
*bmap
)
6593 struct xfs_bmap_intent
*bi
;
6595 trace_xfs_bmap_defer(mp
,
6596 XFS_FSB_TO_AGNO(mp
, bmap
->br_startblock
),
6598 XFS_FSB_TO_AGBNO(mp
, bmap
->br_startblock
),
6599 ip
->i_ino
, whichfork
,
6601 bmap
->br_blockcount
,
6604 bi
= kmem_alloc(sizeof(struct xfs_bmap_intent
), KM_SLEEP
| KM_NOFS
);
6605 INIT_LIST_HEAD(&bi
->bi_list
);
6608 bi
->bi_whichfork
= whichfork
;
6609 bi
->bi_bmap
= *bmap
;
6611 error
= xfs_defer_join(dfops
, bi
->bi_owner
);
6617 xfs_defer_add(dfops
, XFS_DEFER_OPS_TYPE_BMAP
, &bi
->bi_list
);
6621 /* Map an extent into a file. */
6623 xfs_bmap_map_extent(
6624 struct xfs_mount
*mp
,
6625 struct xfs_defer_ops
*dfops
,
6626 struct xfs_inode
*ip
,
6627 struct xfs_bmbt_irec
*PREV
)
6629 if (!xfs_bmap_is_update_needed(PREV
))
6632 return __xfs_bmap_add(mp
, dfops
, XFS_BMAP_MAP
, ip
,
6633 XFS_DATA_FORK
, PREV
);
6636 /* Unmap an extent out of a file. */
6638 xfs_bmap_unmap_extent(
6639 struct xfs_mount
*mp
,
6640 struct xfs_defer_ops
*dfops
,
6641 struct xfs_inode
*ip
,
6642 struct xfs_bmbt_irec
*PREV
)
6644 if (!xfs_bmap_is_update_needed(PREV
))
6647 return __xfs_bmap_add(mp
, dfops
, XFS_BMAP_UNMAP
, ip
,
6648 XFS_DATA_FORK
, PREV
);
6652 * Process one of the deferred bmap operations. We pass back the
6653 * btree cursor to maintain our lock on the bmapbt between calls.
6656 xfs_bmap_finish_one(
6657 struct xfs_trans
*tp
,
6658 struct xfs_defer_ops
*dfops
,
6659 struct xfs_inode
*ip
,
6660 enum xfs_bmap_intent_type type
,
6662 xfs_fileoff_t startoff
,
6663 xfs_fsblock_t startblock
,
6664 xfs_filblks_t
*blockcount
,
6667 struct xfs_bmbt_irec bmap
;
6669 xfs_fsblock_t firstfsb
;
6670 int flags
= XFS_BMAPI_REMAP
;
6673 bmap
.br_startblock
= startblock
;
6674 bmap
.br_startoff
= startoff
;
6675 bmap
.br_blockcount
= *blockcount
;
6676 bmap
.br_state
= state
;
6679 * firstfsb is tied to the transaction lifetime and is used to
6680 * ensure correct AG locking order and schedule work item
6681 * continuations. XFS_BUI_MAX_FAST_EXTENTS (== 1) restricts us
6682 * to only making one bmap call per transaction, so it should
6683 * be safe to have it as a local variable here.
6685 firstfsb
= NULLFSBLOCK
;
6687 trace_xfs_bmap_deferred(tp
->t_mountp
,
6688 XFS_FSB_TO_AGNO(tp
->t_mountp
, startblock
), type
,
6689 XFS_FSB_TO_AGBNO(tp
->t_mountp
, startblock
),
6690 ip
->i_ino
, whichfork
, startoff
, *blockcount
, state
);
6692 if (whichfork
!= XFS_DATA_FORK
&& whichfork
!= XFS_ATTR_FORK
)
6693 return -EFSCORRUPTED
;
6694 if (whichfork
== XFS_ATTR_FORK
)
6695 flags
|= XFS_BMAPI_ATTRFORK
;
6697 if (XFS_TEST_ERROR(false, tp
->t_mountp
,
6698 XFS_ERRTAG_BMAP_FINISH_ONE
,
6699 XFS_RANDOM_BMAP_FINISH_ONE
))
6704 firstfsb
= bmap
.br_startblock
;
6705 error
= xfs_bmapi_write(tp
, ip
, bmap
.br_startoff
,
6706 bmap
.br_blockcount
, flags
, &firstfsb
,
6707 bmap
.br_blockcount
, &bmap
, &nimaps
,
6711 case XFS_BMAP_UNMAP
:
6712 error
= __xfs_bunmapi(tp
, ip
, startoff
, blockcount
,
6713 XFS_BMAPI_REMAP
, 1, &firstfsb
, dfops
);
6717 error
= -EFSCORRUPTED
;