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
18 #include "libxfs_priv.h"
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_alloc.h"
36 #include "xfs_bmap_btree.h"
37 #include "xfs_trans_space.h"
38 #include "xfs_trace.h"
39 #include "xfs_attr_leaf.h"
40 #include "xfs_quota_defs.h"
42 #include "xfs_ag_resv.h"
43 #include "xfs_refcount.h"
46 kmem_zone_t
*xfs_bmap_free_item_zone
;
49 * Miscellaneous helper functions
53 * Compute and fill in the value of the maximum depth of a bmap btree
54 * in this filesystem. Done once, during mount.
57 xfs_bmap_compute_maxlevels(
58 xfs_mount_t
*mp
, /* file system mount structure */
59 int whichfork
) /* data or attr fork */
61 int level
; /* btree level */
62 uint maxblocks
; /* max blocks at this level */
63 uint maxleafents
; /* max leaf entries possible */
64 int maxrootrecs
; /* max records in root block */
65 int minleafrecs
; /* min records in leaf block */
66 int minnoderecs
; /* min records in node block */
67 int sz
; /* root block size */
70 * The maximum number of extents in a file, hence the maximum
71 * number of leaf entries, is controlled by the type of di_nextents
72 * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
73 * (a signed 16-bit number, xfs_aextnum_t).
75 * Note that we can no longer assume that if we are in ATTR1 that
76 * the fork offset of all the inodes will be
77 * (xfs_default_attroffset(ip) >> 3) because we could have mounted
78 * with ATTR2 and then mounted back with ATTR1, keeping the
79 * di_forkoff's fixed but probably at various positions. Therefore,
80 * for both ATTR1 and ATTR2 we have to assume the worst case scenario
81 * of a minimum size available.
83 if (whichfork
== XFS_DATA_FORK
) {
84 maxleafents
= MAXEXTNUM
;
85 sz
= XFS_BMDR_SPACE_CALC(MINDBTPTRS
);
87 maxleafents
= MAXAEXTNUM
;
88 sz
= XFS_BMDR_SPACE_CALC(MINABTPTRS
);
90 maxrootrecs
= xfs_bmdr_maxrecs(sz
, 0);
91 minleafrecs
= mp
->m_bmap_dmnr
[0];
92 minnoderecs
= mp
->m_bmap_dmnr
[1];
93 maxblocks
= (maxleafents
+ minleafrecs
- 1) / minleafrecs
;
94 for (level
= 1; maxblocks
> 1; level
++) {
95 if (maxblocks
<= maxrootrecs
)
98 maxblocks
= (maxblocks
+ minnoderecs
- 1) / minnoderecs
;
100 mp
->m_bm_maxlevels
[whichfork
] = level
;
103 STATIC
int /* error */
105 struct xfs_btree_cur
*cur
,
109 int *stat
) /* success/failure */
111 cur
->bc_rec
.b
.br_startoff
= off
;
112 cur
->bc_rec
.b
.br_startblock
= bno
;
113 cur
->bc_rec
.b
.br_blockcount
= len
;
114 return xfs_btree_lookup(cur
, XFS_LOOKUP_EQ
, stat
);
117 STATIC
int /* error */
119 struct xfs_btree_cur
*cur
,
123 int *stat
) /* success/failure */
125 cur
->bc_rec
.b
.br_startoff
= off
;
126 cur
->bc_rec
.b
.br_startblock
= bno
;
127 cur
->bc_rec
.b
.br_blockcount
= len
;
128 return xfs_btree_lookup(cur
, XFS_LOOKUP_GE
, stat
);
132 * Check if the inode needs to be converted to btree format.
134 static inline bool xfs_bmap_needs_btree(struct xfs_inode
*ip
, int whichfork
)
136 return whichfork
!= XFS_COW_FORK
&&
137 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
&&
138 XFS_IFORK_NEXTENTS(ip
, whichfork
) >
139 XFS_IFORK_MAXEXT(ip
, whichfork
);
143 * Check if the inode should be converted to extent format.
145 static inline bool xfs_bmap_wants_extents(struct xfs_inode
*ip
, int whichfork
)
147 return whichfork
!= XFS_COW_FORK
&&
148 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
&&
149 XFS_IFORK_NEXTENTS(ip
, whichfork
) <=
150 XFS_IFORK_MAXEXT(ip
, whichfork
);
154 * Update the record referred to by cur to the value given
155 * by [off, bno, len, state].
156 * This either works (return 0) or gets an EFSCORRUPTED error.
160 struct xfs_btree_cur
*cur
,
166 union xfs_btree_rec rec
;
168 xfs_bmbt_disk_set_allf(&rec
.bmbt
, off
, bno
, len
, state
);
169 return xfs_btree_update(cur
, &rec
);
173 * Compute the worst-case number of indirect blocks that will be used
174 * for ip's delayed extent of length "len".
177 xfs_bmap_worst_indlen(
178 xfs_inode_t
*ip
, /* incore inode pointer */
179 xfs_filblks_t len
) /* delayed extent length */
181 int level
; /* btree level number */
182 int maxrecs
; /* maximum record count at this level */
183 xfs_mount_t
*mp
; /* mount structure */
184 xfs_filblks_t rval
; /* return value */
187 maxrecs
= mp
->m_bmap_dmxr
[0];
188 for (level
= 0, rval
= 0;
189 level
< XFS_BM_MAXLEVELS(mp
, XFS_DATA_FORK
);
192 do_div(len
, maxrecs
);
195 return rval
+ XFS_BM_MAXLEVELS(mp
, XFS_DATA_FORK
) -
198 maxrecs
= mp
->m_bmap_dmxr
[1];
204 * Calculate the default attribute fork offset for newly created inodes.
207 xfs_default_attroffset(
208 struct xfs_inode
*ip
)
210 struct xfs_mount
*mp
= ip
->i_mount
;
213 if (mp
->m_sb
.sb_inodesize
== 256) {
214 offset
= XFS_LITINO(mp
, ip
->i_d
.di_version
) -
215 XFS_BMDR_SPACE_CALC(MINABTPTRS
);
217 offset
= XFS_BMDR_SPACE_CALC(6 * MINABTPTRS
);
220 ASSERT(offset
< XFS_LITINO(mp
, ip
->i_d
.di_version
));
225 * Helper routine to reset inode di_forkoff field when switching
226 * attribute fork from local to extent format - we reset it where
227 * possible to make space available for inline data fork extents.
230 xfs_bmap_forkoff_reset(
234 if (whichfork
== XFS_ATTR_FORK
&&
235 ip
->i_d
.di_format
!= XFS_DINODE_FMT_DEV
&&
236 ip
->i_d
.di_format
!= XFS_DINODE_FMT_UUID
&&
237 ip
->i_d
.di_format
!= XFS_DINODE_FMT_BTREE
) {
238 uint dfl_forkoff
= xfs_default_attroffset(ip
) >> 3;
240 if (dfl_forkoff
> ip
->i_d
.di_forkoff
)
241 ip
->i_d
.di_forkoff
= dfl_forkoff
;
246 STATIC
struct xfs_buf
*
248 struct xfs_btree_cur
*cur
,
251 struct xfs_log_item_desc
*lidp
;
257 for (i
= 0; i
< XFS_BTREE_MAXLEVELS
; i
++) {
258 if (!cur
->bc_bufs
[i
])
260 if (XFS_BUF_ADDR(cur
->bc_bufs
[i
]) == bno
)
261 return cur
->bc_bufs
[i
];
264 /* Chase down all the log items to see if the bp is there */
265 list_for_each_entry(lidp
, &cur
->bc_tp
->t_items
, lid_trans
) {
266 struct xfs_buf_log_item
*bip
;
267 bip
= (struct xfs_buf_log_item
*)lidp
->lid_item
;
268 if (bip
->bli_item
.li_type
== XFS_LI_BUF
&&
269 XFS_BUF_ADDR(bip
->bli_buf
) == bno
)
278 struct xfs_btree_block
*block
,
284 __be64
*pp
, *thispa
; /* pointer to block address */
285 xfs_bmbt_key_t
*prevp
, *keyp
;
287 ASSERT(be16_to_cpu(block
->bb_level
) > 0);
290 for( i
= 1; i
<= xfs_btree_get_numrecs(block
); i
++) {
291 dmxr
= mp
->m_bmap_dmxr
[0];
292 keyp
= XFS_BMBT_KEY_ADDR(mp
, block
, i
);
295 ASSERT(be64_to_cpu(prevp
->br_startoff
) <
296 be64_to_cpu(keyp
->br_startoff
));
301 * Compare the block numbers to see if there are dups.
304 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, i
, sz
);
306 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, i
, dmxr
);
308 for (j
= i
+1; j
<= be16_to_cpu(block
->bb_numrecs
); j
++) {
310 thispa
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, j
, sz
);
312 thispa
= XFS_BMBT_PTR_ADDR(mp
, block
, j
, dmxr
);
313 if (*thispa
== *pp
) {
314 xfs_warn(mp
, "%s: thispa(%d) == pp(%d) %Ld",
316 (unsigned long long)be64_to_cpu(*thispa
));
317 panic("%s: ptrs are equal in node\n",
325 * Check that the extents for the inode ip are in the right order in all
326 * btree leaves. THis becomes prohibitively expensive for large extent count
327 * files, so don't bother with inodes that have more than 10,000 extents in
328 * them. The btree record ordering checks will still be done, so for such large
329 * bmapbt constructs that is going to catch most corruptions.
332 xfs_bmap_check_leaf_extents(
333 xfs_btree_cur_t
*cur
, /* btree cursor or null */
334 xfs_inode_t
*ip
, /* incore inode pointer */
335 int whichfork
) /* data or attr fork */
337 struct xfs_btree_block
*block
; /* current btree block */
338 xfs_fsblock_t bno
; /* block # of "block" */
339 xfs_buf_t
*bp
; /* buffer for "block" */
340 int error
; /* error return value */
341 xfs_extnum_t i
=0, j
; /* index into the extents list */
342 xfs_ifork_t
*ifp
; /* fork structure */
343 int level
; /* btree level, for checking */
344 xfs_mount_t
*mp
; /* file system mount structure */
345 __be64
*pp
; /* pointer to block address */
346 xfs_bmbt_rec_t
*ep
; /* pointer to current extent */
347 xfs_bmbt_rec_t last
= {0, 0}; /* last extent in prev block */
348 xfs_bmbt_rec_t
*nextp
; /* pointer to next extent */
351 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
) {
355 /* skip large extent count inodes */
356 if (ip
->i_d
.di_nextents
> 10000)
361 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
362 block
= ifp
->if_broot
;
364 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
366 level
= be16_to_cpu(block
->bb_level
);
368 xfs_check_block(block
, mp
, 1, ifp
->if_broot_bytes
);
369 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, 1, ifp
->if_broot_bytes
);
370 bno
= be64_to_cpu(*pp
);
372 ASSERT(bno
!= NULLFSBLOCK
);
373 ASSERT(XFS_FSB_TO_AGNO(mp
, bno
) < mp
->m_sb
.sb_agcount
);
374 ASSERT(XFS_FSB_TO_AGBNO(mp
, bno
) < mp
->m_sb
.sb_agblocks
);
377 * Go down the tree until leaf level is reached, following the first
378 * pointer (leftmost) at each level.
380 while (level
-- > 0) {
381 /* See if buf is in cur first */
383 bp
= xfs_bmap_get_bp(cur
, XFS_FSB_TO_DADDR(mp
, bno
));
386 error
= xfs_btree_read_bufl(mp
, NULL
, bno
, 0, &bp
,
392 block
= XFS_BUF_TO_BLOCK(bp
);
397 * Check this block for basic sanity (increasing keys and
398 * no duplicate blocks).
401 xfs_check_block(block
, mp
, 0, 0);
402 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, mp
->m_bmap_dmxr
[1]);
403 bno
= be64_to_cpu(*pp
);
404 XFS_WANT_CORRUPTED_GOTO(mp
,
405 XFS_FSB_SANITY_CHECK(mp
, bno
), error0
);
408 xfs_trans_brelse(NULL
, bp
);
413 * Here with bp and block set to the leftmost leaf node in the tree.
418 * Loop over all leaf nodes checking that all extents are in the right order.
421 xfs_fsblock_t nextbno
;
422 xfs_extnum_t num_recs
;
425 num_recs
= xfs_btree_get_numrecs(block
);
428 * Read-ahead the next leaf block, if any.
431 nextbno
= be64_to_cpu(block
->bb_u
.l
.bb_rightsib
);
434 * Check all the extents to make sure they are OK.
435 * If we had a previous block, the last entry should
436 * conform with the first entry in this one.
439 ep
= XFS_BMBT_REC_ADDR(mp
, block
, 1);
441 ASSERT(xfs_bmbt_disk_get_startoff(&last
) +
442 xfs_bmbt_disk_get_blockcount(&last
) <=
443 xfs_bmbt_disk_get_startoff(ep
));
445 for (j
= 1; j
< num_recs
; j
++) {
446 nextp
= XFS_BMBT_REC_ADDR(mp
, block
, j
+ 1);
447 ASSERT(xfs_bmbt_disk_get_startoff(ep
) +
448 xfs_bmbt_disk_get_blockcount(ep
) <=
449 xfs_bmbt_disk_get_startoff(nextp
));
457 xfs_trans_brelse(NULL
, bp
);
461 * If we've reached the end, stop.
463 if (bno
== NULLFSBLOCK
)
467 bp
= xfs_bmap_get_bp(cur
, XFS_FSB_TO_DADDR(mp
, bno
));
470 error
= xfs_btree_read_bufl(mp
, NULL
, bno
, 0, &bp
,
476 block
= XFS_BUF_TO_BLOCK(bp
);
482 xfs_warn(mp
, "%s: at error0", __func__
);
484 xfs_trans_brelse(NULL
, bp
);
486 xfs_warn(mp
, "%s: BAD after btree leaves for %d extents",
488 panic("%s: CORRUPTED BTREE OR SOMETHING", __func__
);
493 * Add bmap trace insert entries for all the contents of the extent records.
496 xfs_bmap_trace_exlist(
497 xfs_inode_t
*ip
, /* incore inode pointer */
498 xfs_extnum_t cnt
, /* count of entries in the list */
499 int whichfork
, /* data or attr fork */
500 unsigned long caller_ip
)
502 xfs_extnum_t idx
; /* extent record index */
503 xfs_ifork_t
*ifp
; /* inode fork pointer */
506 if (whichfork
== XFS_ATTR_FORK
)
507 state
|= BMAP_ATTRFORK
;
509 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
510 ASSERT(cnt
== xfs_iext_count(ifp
));
511 for (idx
= 0; idx
< cnt
; idx
++)
512 trace_xfs_extlist(ip
, idx
, whichfork
, caller_ip
);
516 * Validate that the bmbt_irecs being returned from bmapi are valid
517 * given the caller's original parameters. Specifically check the
518 * ranges of the returned irecs to ensure that they only extend beyond
519 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
522 xfs_bmap_validate_ret(
526 xfs_bmbt_irec_t
*mval
,
530 int i
; /* index to map values */
532 ASSERT(ret_nmap
<= nmap
);
534 for (i
= 0; i
< ret_nmap
; i
++) {
535 ASSERT(mval
[i
].br_blockcount
> 0);
536 if (!(flags
& XFS_BMAPI_ENTIRE
)) {
537 ASSERT(mval
[i
].br_startoff
>= bno
);
538 ASSERT(mval
[i
].br_blockcount
<= len
);
539 ASSERT(mval
[i
].br_startoff
+ mval
[i
].br_blockcount
<=
542 ASSERT(mval
[i
].br_startoff
< bno
+ len
);
543 ASSERT(mval
[i
].br_startoff
+ mval
[i
].br_blockcount
>
547 mval
[i
- 1].br_startoff
+ mval
[i
- 1].br_blockcount
==
548 mval
[i
].br_startoff
);
549 ASSERT(mval
[i
].br_startblock
!= DELAYSTARTBLOCK
&&
550 mval
[i
].br_startblock
!= HOLESTARTBLOCK
);
551 ASSERT(mval
[i
].br_state
== XFS_EXT_NORM
||
552 mval
[i
].br_state
== XFS_EXT_UNWRITTEN
);
557 #define xfs_bmap_check_leaf_extents(cur, ip, whichfork) do { } while (0)
558 #define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap)
562 * bmap free list manipulation functions
566 * Add the extent to the list of extents to be free at transaction end.
567 * The list is maintained sorted (by block number).
571 struct xfs_mount
*mp
,
572 struct xfs_defer_ops
*dfops
,
575 struct xfs_owner_info
*oinfo
)
577 struct xfs_extent_free_item
*new; /* new element */
582 ASSERT(bno
!= NULLFSBLOCK
);
584 ASSERT(len
<= MAXEXTLEN
);
585 ASSERT(!isnullstartblock(bno
));
586 agno
= XFS_FSB_TO_AGNO(mp
, bno
);
587 agbno
= XFS_FSB_TO_AGBNO(mp
, bno
);
588 ASSERT(agno
< mp
->m_sb
.sb_agcount
);
589 ASSERT(agbno
< mp
->m_sb
.sb_agblocks
);
590 ASSERT(len
< mp
->m_sb
.sb_agblocks
);
591 ASSERT(agbno
+ len
<= mp
->m_sb
.sb_agblocks
);
593 ASSERT(xfs_bmap_free_item_zone
!= NULL
);
595 new = kmem_zone_alloc(xfs_bmap_free_item_zone
, KM_SLEEP
);
596 new->xefi_startblock
= bno
;
597 new->xefi_blockcount
= (xfs_extlen_t
)len
;
599 new->xefi_oinfo
= *oinfo
;
601 xfs_rmap_skip_owner_update(&new->xefi_oinfo
);
602 trace_xfs_bmap_free_defer(mp
, XFS_FSB_TO_AGNO(mp
, bno
), 0,
603 XFS_FSB_TO_AGBNO(mp
, bno
), len
);
604 xfs_defer_add(dfops
, XFS_DEFER_OPS_TYPE_FREE
, &new->xefi_list
);
608 * Inode fork format manipulation functions
612 * Transform a btree format file with only one leaf node, where the
613 * extents list will fit in the inode, into an extents format file.
614 * Since the file extents are already in-core, all we have to do is
615 * give up the space for the btree root and pitch the leaf block.
617 STATIC
int /* error */
618 xfs_bmap_btree_to_extents(
619 xfs_trans_t
*tp
, /* transaction pointer */
620 xfs_inode_t
*ip
, /* incore inode pointer */
621 xfs_btree_cur_t
*cur
, /* btree cursor */
622 int *logflagsp
, /* inode logging flags */
623 int whichfork
) /* data or attr fork */
626 struct xfs_btree_block
*cblock
;/* child btree block */
627 xfs_fsblock_t cbno
; /* child block number */
628 xfs_buf_t
*cbp
; /* child block's buffer */
629 int error
; /* error return value */
630 xfs_ifork_t
*ifp
; /* inode fork data */
631 xfs_mount_t
*mp
; /* mount point structure */
632 __be64
*pp
; /* ptr to block address */
633 struct xfs_btree_block
*rblock
;/* root btree block */
634 struct xfs_owner_info oinfo
;
637 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
638 ASSERT(whichfork
!= XFS_COW_FORK
);
639 ASSERT(ifp
->if_flags
& XFS_IFEXTENTS
);
640 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
);
641 rblock
= ifp
->if_broot
;
642 ASSERT(be16_to_cpu(rblock
->bb_level
) == 1);
643 ASSERT(be16_to_cpu(rblock
->bb_numrecs
) == 1);
644 ASSERT(xfs_bmbt_maxrecs(mp
, ifp
->if_broot_bytes
, 0) == 1);
645 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, rblock
, 1, ifp
->if_broot_bytes
);
646 cbno
= be64_to_cpu(*pp
);
649 if ((error
= xfs_btree_check_lptr(cur
, cbno
, 1)))
652 error
= xfs_btree_read_bufl(mp
, tp
, cbno
, 0, &cbp
, XFS_BMAP_BTREE_REF
,
656 cblock
= XFS_BUF_TO_BLOCK(cbp
);
657 if ((error
= xfs_btree_check_block(cur
, cblock
, 0, cbp
)))
659 xfs_rmap_ino_bmbt_owner(&oinfo
, ip
->i_ino
, whichfork
);
660 xfs_bmap_add_free(mp
, cur
->bc_private
.b
.dfops
, cbno
, 1, &oinfo
);
661 ip
->i_d
.di_nblocks
--;
662 xfs_trans_mod_dquot_byino(tp
, ip
, XFS_TRANS_DQ_BCOUNT
, -1L);
663 xfs_trans_binval(tp
, cbp
);
664 if (cur
->bc_bufs
[0] == cbp
)
665 cur
->bc_bufs
[0] = NULL
;
666 xfs_iroot_realloc(ip
, -1, whichfork
);
667 ASSERT(ifp
->if_broot
== NULL
);
668 ASSERT((ifp
->if_flags
& XFS_IFBROOT
) == 0);
669 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_EXTENTS
);
670 *logflagsp
= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
675 * Convert an extents-format file into a btree-format file.
676 * The new file will have a root block (in the inode) and a single child block.
678 STATIC
int /* error */
679 xfs_bmap_extents_to_btree(
680 xfs_trans_t
*tp
, /* transaction pointer */
681 xfs_inode_t
*ip
, /* incore inode pointer */
682 xfs_fsblock_t
*firstblock
, /* first-block-allocated */
683 struct xfs_defer_ops
*dfops
, /* blocks freed in xaction */
684 xfs_btree_cur_t
**curp
, /* cursor returned to caller */
685 int wasdel
, /* converting a delayed alloc */
686 int *logflagsp
, /* inode logging flags */
687 int whichfork
) /* data or attr fork */
689 struct xfs_btree_block
*ablock
; /* allocated (child) bt block */
690 xfs_buf_t
*abp
; /* buffer for ablock */
691 xfs_alloc_arg_t args
; /* allocation arguments */
692 xfs_bmbt_rec_t
*arp
; /* child record pointer */
693 struct xfs_btree_block
*block
; /* btree root block */
694 xfs_btree_cur_t
*cur
; /* bmap btree cursor */
695 xfs_bmbt_rec_host_t
*ep
; /* extent record pointer */
696 int error
; /* error return value */
697 xfs_extnum_t i
, cnt
; /* extent record index */
698 xfs_ifork_t
*ifp
; /* inode fork pointer */
699 xfs_bmbt_key_t
*kp
; /* root block key pointer */
700 xfs_mount_t
*mp
; /* mount structure */
701 xfs_extnum_t nextents
; /* number of file extents */
702 xfs_bmbt_ptr_t
*pp
; /* root block address pointer */
705 ASSERT(whichfork
!= XFS_COW_FORK
);
706 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
707 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
);
710 * Make space in the inode incore.
712 xfs_iroot_realloc(ip
, 1, whichfork
);
713 ifp
->if_flags
|= XFS_IFBROOT
;
718 block
= ifp
->if_broot
;
719 if (xfs_sb_version_hascrc(&mp
->m_sb
))
720 xfs_btree_init_block_int(mp
, block
, XFS_BUF_DADDR_NULL
,
721 XFS_BMAP_CRC_MAGIC
, 1, 1, ip
->i_ino
,
722 XFS_BTREE_LONG_PTRS
| XFS_BTREE_CRC_BLOCKS
);
724 xfs_btree_init_block_int(mp
, block
, XFS_BUF_DADDR_NULL
,
725 XFS_BMAP_MAGIC
, 1, 1, ip
->i_ino
,
726 XFS_BTREE_LONG_PTRS
);
729 * Need a cursor. Can't allocate until bb_level is filled in.
731 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
732 cur
->bc_private
.b
.firstblock
= *firstblock
;
733 cur
->bc_private
.b
.dfops
= dfops
;
734 cur
->bc_private
.b
.flags
= wasdel
? XFS_BTCUR_BPRV_WASDEL
: 0;
736 * Convert to a btree with two levels, one record in root.
738 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_BTREE
);
739 memset(&args
, 0, sizeof(args
));
742 xfs_rmap_ino_bmbt_owner(&args
.oinfo
, ip
->i_ino
, whichfork
);
743 args
.firstblock
= *firstblock
;
744 if (*firstblock
== NULLFSBLOCK
) {
745 args
.type
= XFS_ALLOCTYPE_START_BNO
;
746 args
.fsbno
= XFS_INO_TO_FSB(mp
, ip
->i_ino
);
747 } else if (dfops
->dop_low
) {
749 args
.type
= XFS_ALLOCTYPE_START_BNO
;
750 args
.fsbno
= *firstblock
;
752 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
753 args
.fsbno
= *firstblock
;
755 args
.minlen
= args
.maxlen
= args
.prod
= 1;
756 args
.wasdel
= wasdel
;
758 if ((error
= xfs_alloc_vextent(&args
))) {
759 xfs_iroot_realloc(ip
, -1, whichfork
);
760 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
765 * During a CoW operation, the allocation and bmbt updates occur in
766 * different transactions. The mapping code tries to put new bmbt
767 * blocks near extents being mapped, but the only way to guarantee this
768 * is if the alloc and the mapping happen in a single transaction that
769 * has a block reservation. That isn't the case here, so if we run out
770 * of space we'll try again with another AG.
772 if (xfs_sb_version_hasreflink(&cur
->bc_mp
->m_sb
) &&
773 args
.fsbno
== NULLFSBLOCK
&&
774 args
.type
== XFS_ALLOCTYPE_NEAR_BNO
) {
775 dfops
->dop_low
= true;
779 * Allocation can't fail, the space was reserved.
781 ASSERT(args
.fsbno
!= NULLFSBLOCK
);
782 ASSERT(*firstblock
== NULLFSBLOCK
||
783 args
.agno
== XFS_FSB_TO_AGNO(mp
, *firstblock
) ||
785 args
.agno
> XFS_FSB_TO_AGNO(mp
, *firstblock
)));
786 *firstblock
= cur
->bc_private
.b
.firstblock
= args
.fsbno
;
787 cur
->bc_private
.b
.allocated
++;
788 ip
->i_d
.di_nblocks
++;
789 xfs_trans_mod_dquot_byino(tp
, ip
, XFS_TRANS_DQ_BCOUNT
, 1L);
790 abp
= xfs_btree_get_bufl(mp
, tp
, args
.fsbno
, 0);
792 * Fill in the child block.
794 abp
->b_ops
= &xfs_bmbt_buf_ops
;
795 ablock
= XFS_BUF_TO_BLOCK(abp
);
796 if (xfs_sb_version_hascrc(&mp
->m_sb
))
797 xfs_btree_init_block_int(mp
, ablock
, abp
->b_bn
,
798 XFS_BMAP_CRC_MAGIC
, 0, 0, ip
->i_ino
,
799 XFS_BTREE_LONG_PTRS
| XFS_BTREE_CRC_BLOCKS
);
801 xfs_btree_init_block_int(mp
, ablock
, abp
->b_bn
,
802 XFS_BMAP_MAGIC
, 0, 0, ip
->i_ino
,
803 XFS_BTREE_LONG_PTRS
);
805 arp
= XFS_BMBT_REC_ADDR(mp
, ablock
, 1);
806 nextents
= xfs_iext_count(ifp
);
807 for (cnt
= i
= 0; i
< nextents
; i
++) {
808 ep
= xfs_iext_get_ext(ifp
, i
);
809 if (!isnullstartblock(xfs_bmbt_get_startblock(ep
))) {
810 arp
->l0
= cpu_to_be64(ep
->l0
);
811 arp
->l1
= cpu_to_be64(ep
->l1
);
815 ASSERT(cnt
== XFS_IFORK_NEXTENTS(ip
, whichfork
));
816 xfs_btree_set_numrecs(ablock
, cnt
);
819 * Fill in the root key and pointer.
821 kp
= XFS_BMBT_KEY_ADDR(mp
, block
, 1);
822 arp
= XFS_BMBT_REC_ADDR(mp
, ablock
, 1);
823 kp
->br_startoff
= cpu_to_be64(xfs_bmbt_disk_get_startoff(arp
));
824 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, xfs_bmbt_get_maxrecs(cur
,
825 be16_to_cpu(block
->bb_level
)));
826 *pp
= cpu_to_be64(args
.fsbno
);
829 * Do all this logging at the end so that
830 * the root is at the right level.
832 xfs_btree_log_block(cur
, abp
, XFS_BB_ALL_BITS
);
833 xfs_btree_log_recs(cur
, abp
, 1, be16_to_cpu(ablock
->bb_numrecs
));
834 ASSERT(*curp
== NULL
);
836 *logflagsp
= XFS_ILOG_CORE
| xfs_ilog_fbroot(whichfork
);
841 * Convert a local file to an extents file.
842 * This code is out of bounds for data forks of regular files,
843 * since the file data needs to get logged so things will stay consistent.
844 * (The bmap-level manipulations are ok, though).
847 xfs_bmap_local_to_extents_empty(
848 struct xfs_inode
*ip
,
851 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
853 ASSERT(whichfork
!= XFS_COW_FORK
);
854 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
855 ASSERT(ifp
->if_bytes
== 0);
856 ASSERT(XFS_IFORK_NEXTENTS(ip
, whichfork
) == 0);
858 xfs_bmap_forkoff_reset(ip
, whichfork
);
859 ifp
->if_flags
&= ~XFS_IFINLINE
;
860 ifp
->if_flags
|= XFS_IFEXTENTS
;
861 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_EXTENTS
);
865 STATIC
int /* error */
866 xfs_bmap_local_to_extents(
867 xfs_trans_t
*tp
, /* transaction pointer */
868 xfs_inode_t
*ip
, /* incore inode pointer */
869 xfs_fsblock_t
*firstblock
, /* first block allocated in xaction */
870 xfs_extlen_t total
, /* total blocks needed by transaction */
871 int *logflagsp
, /* inode logging flags */
873 void (*init_fn
)(struct xfs_trans
*tp
,
875 struct xfs_inode
*ip
,
876 struct xfs_ifork
*ifp
))
879 int flags
; /* logging flags returned */
880 xfs_ifork_t
*ifp
; /* inode fork pointer */
881 xfs_alloc_arg_t args
; /* allocation arguments */
882 xfs_buf_t
*bp
; /* buffer for extent block */
883 xfs_bmbt_rec_host_t
*ep
; /* extent record pointer */
886 * We don't want to deal with the case of keeping inode data inline yet.
887 * So sending the data fork of a regular inode is invalid.
889 ASSERT(!(S_ISREG(VFS_I(ip
)->i_mode
) && whichfork
== XFS_DATA_FORK
));
890 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
891 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
893 if (!ifp
->if_bytes
) {
894 xfs_bmap_local_to_extents_empty(ip
, whichfork
);
895 flags
= XFS_ILOG_CORE
;
901 ASSERT((ifp
->if_flags
& (XFS_IFINLINE
|XFS_IFEXTENTS
|XFS_IFEXTIREC
)) ==
903 memset(&args
, 0, sizeof(args
));
905 args
.mp
= ip
->i_mount
;
906 xfs_rmap_ino_owner(&args
.oinfo
, ip
->i_ino
, whichfork
, 0);
907 args
.firstblock
= *firstblock
;
909 * Allocate a block. We know we need only one, since the
910 * file currently fits in an inode.
912 if (*firstblock
== NULLFSBLOCK
) {
914 args
.fsbno
= XFS_INO_TO_FSB(args
.mp
, ip
->i_ino
);
915 args
.type
= XFS_ALLOCTYPE_START_BNO
;
917 args
.fsbno
= *firstblock
;
918 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
921 args
.minlen
= args
.maxlen
= args
.prod
= 1;
922 error
= xfs_alloc_vextent(&args
);
927 * During a CoW operation, the allocation and bmbt updates occur in
928 * different transactions. The mapping code tries to put new bmbt
929 * blocks near extents being mapped, but the only way to guarantee this
930 * is if the alloc and the mapping happen in a single transaction that
931 * has a block reservation. That isn't the case here, so if we run out
932 * of space we'll try again with another AG.
934 if (xfs_sb_version_hasreflink(&ip
->i_mount
->m_sb
) &&
935 args
.fsbno
== NULLFSBLOCK
&&
936 args
.type
== XFS_ALLOCTYPE_NEAR_BNO
) {
939 /* Can't fail, the space was reserved. */
940 ASSERT(args
.fsbno
!= NULLFSBLOCK
);
941 ASSERT(args
.len
== 1);
942 *firstblock
= args
.fsbno
;
943 bp
= xfs_btree_get_bufl(args
.mp
, tp
, args
.fsbno
, 0);
946 * Initialize the block, copy the data and log the remote buffer.
948 * The callout is responsible for logging because the remote format
949 * might differ from the local format and thus we don't know how much to
950 * log here. Note that init_fn must also set the buffer log item type
953 init_fn(tp
, bp
, ip
, ifp
);
955 /* account for the change in fork size */
956 xfs_idata_realloc(ip
, -ifp
->if_bytes
, whichfork
);
957 xfs_bmap_local_to_extents_empty(ip
, whichfork
);
958 flags
|= XFS_ILOG_CORE
;
960 xfs_iext_add(ifp
, 0, 1);
961 ep
= xfs_iext_get_ext(ifp
, 0);
962 xfs_bmbt_set_allf(ep
, 0, args
.fsbno
, 1, XFS_EXT_NORM
);
963 trace_xfs_bmap_post_update(ip
, 0,
964 whichfork
== XFS_ATTR_FORK
? BMAP_ATTRFORK
: 0,
966 XFS_IFORK_NEXT_SET(ip
, whichfork
, 1);
967 ip
->i_d
.di_nblocks
= 1;
968 xfs_trans_mod_dquot_byino(tp
, ip
,
969 XFS_TRANS_DQ_BCOUNT
, 1L);
970 flags
|= xfs_ilog_fext(whichfork
);
978 * Called from xfs_bmap_add_attrfork to handle btree format files.
980 STATIC
int /* error */
981 xfs_bmap_add_attrfork_btree(
982 xfs_trans_t
*tp
, /* transaction pointer */
983 xfs_inode_t
*ip
, /* incore inode pointer */
984 xfs_fsblock_t
*firstblock
, /* first block allocated */
985 struct xfs_defer_ops
*dfops
, /* blocks to free at commit */
986 int *flags
) /* inode logging flags */
988 xfs_btree_cur_t
*cur
; /* btree cursor */
989 int error
; /* error return value */
990 xfs_mount_t
*mp
; /* file system mount struct */
991 int stat
; /* newroot status */
994 if (ip
->i_df
.if_broot_bytes
<= XFS_IFORK_DSIZE(ip
))
995 *flags
|= XFS_ILOG_DBROOT
;
997 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, XFS_DATA_FORK
);
998 cur
->bc_private
.b
.dfops
= dfops
;
999 cur
->bc_private
.b
.firstblock
= *firstblock
;
1000 if ((error
= xfs_bmbt_lookup_ge(cur
, 0, 0, 0, &stat
)))
1002 /* must be at least one entry */
1003 XFS_WANT_CORRUPTED_GOTO(mp
, stat
== 1, error0
);
1004 if ((error
= xfs_btree_new_iroot(cur
, flags
, &stat
)))
1007 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
1010 *firstblock
= cur
->bc_private
.b
.firstblock
;
1011 cur
->bc_private
.b
.allocated
= 0;
1012 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
1016 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
1021 * Called from xfs_bmap_add_attrfork to handle extents format files.
1023 STATIC
int /* error */
1024 xfs_bmap_add_attrfork_extents(
1025 xfs_trans_t
*tp
, /* transaction pointer */
1026 xfs_inode_t
*ip
, /* incore inode pointer */
1027 xfs_fsblock_t
*firstblock
, /* first block allocated */
1028 struct xfs_defer_ops
*dfops
, /* blocks to free at commit */
1029 int *flags
) /* inode logging flags */
1031 xfs_btree_cur_t
*cur
; /* bmap btree cursor */
1032 int error
; /* error return value */
1034 if (ip
->i_d
.di_nextents
* sizeof(xfs_bmbt_rec_t
) <= XFS_IFORK_DSIZE(ip
))
1037 error
= xfs_bmap_extents_to_btree(tp
, ip
, firstblock
, dfops
, &cur
, 0,
1038 flags
, XFS_DATA_FORK
);
1040 cur
->bc_private
.b
.allocated
= 0;
1041 xfs_btree_del_cursor(cur
,
1042 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
1048 * Called from xfs_bmap_add_attrfork to handle local format files. Each
1049 * different data fork content type needs a different callout to do the
1050 * conversion. Some are basic and only require special block initialisation
1051 * callouts for the data formating, others (directories) are so specialised they
1052 * handle everything themselves.
1054 * XXX (dgc): investigate whether directory conversion can use the generic
1055 * formatting callout. It should be possible - it's just a very complex
1058 STATIC
int /* error */
1059 xfs_bmap_add_attrfork_local(
1060 xfs_trans_t
*tp
, /* transaction pointer */
1061 xfs_inode_t
*ip
, /* incore inode pointer */
1062 xfs_fsblock_t
*firstblock
, /* first block allocated */
1063 struct xfs_defer_ops
*dfops
, /* blocks to free at commit */
1064 int *flags
) /* inode logging flags */
1066 xfs_da_args_t dargs
; /* args for dir/attr code */
1068 if (ip
->i_df
.if_bytes
<= XFS_IFORK_DSIZE(ip
))
1071 if (S_ISDIR(VFS_I(ip
)->i_mode
)) {
1072 memset(&dargs
, 0, sizeof(dargs
));
1073 dargs
.geo
= ip
->i_mount
->m_dir_geo
;
1075 dargs
.firstblock
= firstblock
;
1076 dargs
.dfops
= dfops
;
1077 dargs
.total
= dargs
.geo
->fsbcount
;
1078 dargs
.whichfork
= XFS_DATA_FORK
;
1080 return xfs_dir2_sf_to_block(&dargs
);
1083 if (S_ISLNK(VFS_I(ip
)->i_mode
))
1084 return xfs_bmap_local_to_extents(tp
, ip
, firstblock
, 1,
1085 flags
, XFS_DATA_FORK
,
1086 xfs_symlink_local_to_remote
);
1088 /* should only be called for types that support local format data */
1090 return -EFSCORRUPTED
;
1094 * Convert inode from non-attributed to attributed.
1095 * Must not be in a transaction, ip must not be locked.
1097 int /* error code */
1098 xfs_bmap_add_attrfork(
1099 xfs_inode_t
*ip
, /* incore inode pointer */
1100 int size
, /* space new attribute needs */
1101 int rsvd
) /* xact may use reserved blks */
1103 xfs_fsblock_t firstblock
; /* 1st block/ag allocated */
1104 struct xfs_defer_ops dfops
; /* freed extent records */
1105 xfs_mount_t
*mp
; /* mount structure */
1106 xfs_trans_t
*tp
; /* transaction pointer */
1107 int blks
; /* space reservation */
1108 int version
= 1; /* superblock attr version */
1109 int logflags
; /* logging flags */
1110 int error
; /* error return value */
1112 ASSERT(XFS_IFORK_Q(ip
) == 0);
1115 ASSERT(!XFS_NOT_DQATTACHED(mp
, ip
));
1117 blks
= XFS_ADDAFORK_SPACE_RES(mp
);
1119 error
= xfs_trans_alloc(mp
, &M_RES(mp
)->tr_addafork
, blks
, 0,
1120 rsvd
? XFS_TRANS_RESERVE
: 0, &tp
);
1124 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
1125 error
= xfs_trans_reserve_quota_nblks(tp
, ip
, blks
, 0, rsvd
?
1126 XFS_QMOPT_RES_REGBLKS
| XFS_QMOPT_FORCE_RES
:
1127 XFS_QMOPT_RES_REGBLKS
);
1130 if (XFS_IFORK_Q(ip
))
1132 if (ip
->i_d
.di_aformat
!= XFS_DINODE_FMT_EXTENTS
) {
1134 * For inodes coming from pre-6.2 filesystems.
1136 ASSERT(ip
->i_d
.di_aformat
== 0);
1137 ip
->i_d
.di_aformat
= XFS_DINODE_FMT_EXTENTS
;
1139 ASSERT(ip
->i_d
.di_anextents
== 0);
1141 xfs_trans_ijoin(tp
, ip
, 0);
1142 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
1144 switch (ip
->i_d
.di_format
) {
1145 case XFS_DINODE_FMT_DEV
:
1146 ip
->i_d
.di_forkoff
= roundup(sizeof(xfs_dev_t
), 8) >> 3;
1148 case XFS_DINODE_FMT_UUID
:
1149 ip
->i_d
.di_forkoff
= roundup(sizeof(uuid_t
), 8) >> 3;
1151 case XFS_DINODE_FMT_LOCAL
:
1152 case XFS_DINODE_FMT_EXTENTS
:
1153 case XFS_DINODE_FMT_BTREE
:
1154 ip
->i_d
.di_forkoff
= xfs_attr_shortform_bytesfit(ip
, size
);
1155 if (!ip
->i_d
.di_forkoff
)
1156 ip
->i_d
.di_forkoff
= xfs_default_attroffset(ip
) >> 3;
1157 else if (mp
->m_flags
& XFS_MOUNT_ATTR2
)
1166 ASSERT(ip
->i_afp
== NULL
);
1167 ip
->i_afp
= kmem_zone_zalloc(xfs_ifork_zone
, KM_SLEEP
);
1168 ip
->i_afp
->if_flags
= XFS_IFEXTENTS
;
1170 xfs_defer_init(&dfops
, &firstblock
);
1171 switch (ip
->i_d
.di_format
) {
1172 case XFS_DINODE_FMT_LOCAL
:
1173 error
= xfs_bmap_add_attrfork_local(tp
, ip
, &firstblock
, &dfops
,
1176 case XFS_DINODE_FMT_EXTENTS
:
1177 error
= xfs_bmap_add_attrfork_extents(tp
, ip
, &firstblock
,
1180 case XFS_DINODE_FMT_BTREE
:
1181 error
= xfs_bmap_add_attrfork_btree(tp
, ip
, &firstblock
, &dfops
,
1189 xfs_trans_log_inode(tp
, ip
, logflags
);
1192 if (!xfs_sb_version_hasattr(&mp
->m_sb
) ||
1193 (!xfs_sb_version_hasattr2(&mp
->m_sb
) && version
== 2)) {
1194 bool log_sb
= false;
1196 spin_lock(&mp
->m_sb_lock
);
1197 if (!xfs_sb_version_hasattr(&mp
->m_sb
)) {
1198 xfs_sb_version_addattr(&mp
->m_sb
);
1201 if (!xfs_sb_version_hasattr2(&mp
->m_sb
) && version
== 2) {
1202 xfs_sb_version_addattr2(&mp
->m_sb
);
1205 spin_unlock(&mp
->m_sb_lock
);
1210 error
= xfs_defer_finish(&tp
, &dfops
, NULL
);
1213 error
= xfs_trans_commit(tp
);
1214 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
1218 xfs_defer_cancel(&dfops
);
1220 xfs_trans_cancel(tp
);
1221 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
1226 * Internal and external extent tree search functions.
1230 * Read in the extents to if_extents.
1231 * All inode fields are set up by caller, we just traverse the btree
1232 * and copy the records in. If the file system cannot contain unwritten
1233 * extents, the records are checked for no "state" flags.
1236 xfs_bmap_read_extents(
1237 xfs_trans_t
*tp
, /* transaction pointer */
1238 xfs_inode_t
*ip
, /* incore inode */
1239 int whichfork
) /* data or attr fork */
1241 struct xfs_btree_block
*block
; /* current btree block */
1242 xfs_fsblock_t bno
; /* block # of "block" */
1243 xfs_buf_t
*bp
; /* buffer for "block" */
1244 int error
; /* error return value */
1245 xfs_exntfmt_t exntf
; /* XFS_EXTFMT_NOSTATE, if checking */
1246 xfs_extnum_t i
, j
; /* index into the extents list */
1247 xfs_ifork_t
*ifp
; /* fork structure */
1248 int level
; /* btree level, for checking */
1249 xfs_mount_t
*mp
; /* file system mount structure */
1250 __be64
*pp
; /* pointer to block address */
1252 xfs_extnum_t room
; /* number of entries there's room for */
1256 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1257 exntf
= (whichfork
!= XFS_DATA_FORK
) ? XFS_EXTFMT_NOSTATE
:
1258 XFS_EXTFMT_INODE(ip
);
1259 block
= ifp
->if_broot
;
1261 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
1263 level
= be16_to_cpu(block
->bb_level
);
1265 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, 1, ifp
->if_broot_bytes
);
1266 bno
= be64_to_cpu(*pp
);
1267 ASSERT(bno
!= NULLFSBLOCK
);
1268 ASSERT(XFS_FSB_TO_AGNO(mp
, bno
) < mp
->m_sb
.sb_agcount
);
1269 ASSERT(XFS_FSB_TO_AGBNO(mp
, bno
) < mp
->m_sb
.sb_agblocks
);
1271 * Go down the tree until leaf level is reached, following the first
1272 * pointer (leftmost) at each level.
1274 while (level
-- > 0) {
1275 error
= xfs_btree_read_bufl(mp
, tp
, bno
, 0, &bp
,
1276 XFS_BMAP_BTREE_REF
, &xfs_bmbt_buf_ops
);
1279 block
= XFS_BUF_TO_BLOCK(bp
);
1282 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, mp
->m_bmap_dmxr
[1]);
1283 bno
= be64_to_cpu(*pp
);
1284 XFS_WANT_CORRUPTED_GOTO(mp
,
1285 XFS_FSB_SANITY_CHECK(mp
, bno
), error0
);
1286 xfs_trans_brelse(tp
, bp
);
1289 * Here with bp and block set to the leftmost leaf node in the tree.
1291 room
= xfs_iext_count(ifp
);
1294 * Loop over all leaf nodes. Copy information to the extent records.
1297 xfs_bmbt_rec_t
*frp
;
1298 xfs_fsblock_t nextbno
;
1299 xfs_extnum_t num_recs
;
1302 num_recs
= xfs_btree_get_numrecs(block
);
1303 if (unlikely(i
+ num_recs
> room
)) {
1304 ASSERT(i
+ num_recs
<= room
);
1305 xfs_warn(ip
->i_mount
,
1306 "corrupt dinode %Lu, (btree extents).",
1307 (unsigned long long) ip
->i_ino
);
1308 XFS_CORRUPTION_ERROR("xfs_bmap_read_extents(1)",
1309 XFS_ERRLEVEL_LOW
, ip
->i_mount
, block
);
1313 * Read-ahead the next leaf block, if any.
1315 nextbno
= be64_to_cpu(block
->bb_u
.l
.bb_rightsib
);
1316 if (nextbno
!= NULLFSBLOCK
)
1317 xfs_btree_reada_bufl(mp
, nextbno
, 1,
1320 * Copy records into the extent records.
1322 frp
= XFS_BMBT_REC_ADDR(mp
, block
, 1);
1324 for (j
= 0; j
< num_recs
; j
++, i
++, frp
++) {
1325 xfs_bmbt_rec_host_t
*trp
= xfs_iext_get_ext(ifp
, i
);
1326 trp
->l0
= be64_to_cpu(frp
->l0
);
1327 trp
->l1
= be64_to_cpu(frp
->l1
);
1329 if (exntf
== XFS_EXTFMT_NOSTATE
) {
1331 * Check all attribute bmap btree records and
1332 * any "older" data bmap btree records for a
1333 * set bit in the "extent flag" position.
1335 if (unlikely(xfs_check_nostate_extents(ifp
,
1336 start
, num_recs
))) {
1337 XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
1343 xfs_trans_brelse(tp
, bp
);
1346 * If we've reached the end, stop.
1348 if (bno
== NULLFSBLOCK
)
1350 error
= xfs_btree_read_bufl(mp
, tp
, bno
, 0, &bp
,
1351 XFS_BMAP_BTREE_REF
, &xfs_bmbt_buf_ops
);
1354 block
= XFS_BUF_TO_BLOCK(bp
);
1356 ASSERT(i
== xfs_iext_count(ifp
));
1357 ASSERT(i
== XFS_IFORK_NEXTENTS(ip
, whichfork
));
1358 XFS_BMAP_TRACE_EXLIST(ip
, i
, whichfork
);
1361 xfs_trans_brelse(tp
, bp
);
1362 return -EFSCORRUPTED
;
1367 * Search the extent records for the entry containing block bno.
1368 * If bno lies in a hole, point to the next entry. If bno lies
1369 * past eof, *eofp will be set, and *prevp will contain the last
1370 * entry (null if none). Else, *lastxp will be set to the index
1371 * of the found entry; *gotp will contain the entry.
1373 STATIC xfs_bmbt_rec_host_t
* /* pointer to found extent entry */
1374 xfs_bmap_search_multi_extents(
1375 xfs_ifork_t
*ifp
, /* inode fork pointer */
1376 xfs_fileoff_t bno
, /* block number searched for */
1377 int *eofp
, /* out: end of file found */
1378 xfs_extnum_t
*lastxp
, /* out: last extent index */
1379 xfs_bmbt_irec_t
*gotp
, /* out: extent entry found */
1380 xfs_bmbt_irec_t
*prevp
) /* out: previous extent entry found */
1382 xfs_bmbt_rec_host_t
*ep
; /* extent record pointer */
1383 xfs_extnum_t lastx
; /* last extent index */
1386 * Initialize the extent entry structure to catch access to
1387 * uninitialized br_startblock field.
1389 gotp
->br_startoff
= 0xffa5a5a5a5a5a5a5LL
;
1390 gotp
->br_blockcount
= 0xa55a5a5a5a5a5a5aLL
;
1391 gotp
->br_state
= XFS_EXT_INVALID
;
1392 gotp
->br_startblock
= 0xffffa5a5a5a5a5a5LL
;
1393 prevp
->br_startoff
= NULLFILEOFF
;
1395 ep
= xfs_iext_bno_to_ext(ifp
, bno
, &lastx
);
1397 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, lastx
- 1), prevp
);
1399 if (lastx
< xfs_iext_count(ifp
)) {
1400 xfs_bmbt_get_all(ep
, gotp
);
1414 * Search the extents list for the inode, for the extent containing bno.
1415 * If bno lies in a hole, point to the next entry. If bno lies past eof,
1416 * *eofp will be set, and *prevp will contain the last entry (null if none).
1417 * Else, *lastxp will be set to the index of the found
1418 * entry; *gotp will contain the entry.
1420 xfs_bmbt_rec_host_t
* /* pointer to found extent entry */
1421 xfs_bmap_search_extents(
1422 xfs_inode_t
*ip
, /* incore inode pointer */
1423 xfs_fileoff_t bno
, /* block number searched for */
1424 int fork
, /* data or attr fork */
1425 int *eofp
, /* out: end of file found */
1426 xfs_extnum_t
*lastxp
, /* out: last extent index */
1427 xfs_bmbt_irec_t
*gotp
, /* out: extent entry found */
1428 xfs_bmbt_irec_t
*prevp
) /* out: previous extent entry found */
1430 xfs_ifork_t
*ifp
; /* inode fork pointer */
1431 xfs_bmbt_rec_host_t
*ep
; /* extent record pointer */
1433 XFS_STATS_INC(ip
->i_mount
, xs_look_exlist
);
1434 ifp
= XFS_IFORK_PTR(ip
, fork
);
1436 ep
= xfs_bmap_search_multi_extents(ifp
, bno
, eofp
, lastxp
, gotp
, prevp
);
1438 if (unlikely(!(gotp
->br_startblock
) && (*lastxp
!= NULLEXTNUM
) &&
1439 !(XFS_IS_REALTIME_INODE(ip
) && fork
== XFS_DATA_FORK
))) {
1440 xfs_alert_tag(ip
->i_mount
, XFS_PTAG_FSBLOCK_ZERO
,
1441 "Access to block zero in inode %llu "
1442 "start_block: %llx start_off: %llx "
1443 "blkcnt: %llx extent-state: %x lastx: %x",
1444 (unsigned long long)ip
->i_ino
,
1445 (unsigned long long)gotp
->br_startblock
,
1446 (unsigned long long)gotp
->br_startoff
,
1447 (unsigned long long)gotp
->br_blockcount
,
1448 gotp
->br_state
, *lastxp
);
1449 *lastxp
= NULLEXTNUM
;
1457 * Returns the file-relative block number of the first unused block(s)
1458 * in the file with at least "len" logically contiguous blocks free.
1459 * This is the lowest-address hole if the file has holes, else the first block
1460 * past the end of file.
1461 * Return 0 if the file is currently local (in-inode).
1464 xfs_bmap_first_unused(
1465 xfs_trans_t
*tp
, /* transaction pointer */
1466 xfs_inode_t
*ip
, /* incore inode */
1467 xfs_extlen_t len
, /* size of hole to find */
1468 xfs_fileoff_t
*first_unused
, /* unused block */
1469 int whichfork
) /* data or attr fork */
1471 int error
; /* error return value */
1472 int idx
; /* extent record index */
1473 xfs_ifork_t
*ifp
; /* inode fork pointer */
1474 xfs_fileoff_t lastaddr
; /* last block number seen */
1475 xfs_fileoff_t lowest
; /* lowest useful block */
1476 xfs_fileoff_t max
; /* starting useful block */
1477 xfs_fileoff_t off
; /* offset for this block */
1478 xfs_extnum_t nextents
; /* number of extent entries */
1480 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
||
1481 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
||
1482 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
1483 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
) {
1487 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1488 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
1489 (error
= xfs_iread_extents(tp
, ip
, whichfork
)))
1491 lowest
= *first_unused
;
1492 nextents
= xfs_iext_count(ifp
);
1493 for (idx
= 0, lastaddr
= 0, max
= lowest
; idx
< nextents
; idx
++) {
1494 xfs_bmbt_rec_host_t
*ep
= xfs_iext_get_ext(ifp
, idx
);
1495 off
= xfs_bmbt_get_startoff(ep
);
1497 * See if the hole before this extent will work.
1499 if (off
>= lowest
+ len
&& off
- max
>= len
) {
1500 *first_unused
= max
;
1503 lastaddr
= off
+ xfs_bmbt_get_blockcount(ep
);
1504 max
= XFS_FILEOFF_MAX(lastaddr
, lowest
);
1506 *first_unused
= max
;
1511 * Returns the file-relative block number of the last block - 1 before
1512 * last_block (input value) in the file.
1513 * This is not based on i_size, it is based on the extent records.
1514 * Returns 0 for local files, as they do not have extent records.
1517 xfs_bmap_last_before(
1518 struct xfs_trans
*tp
, /* transaction pointer */
1519 struct xfs_inode
*ip
, /* incore inode */
1520 xfs_fileoff_t
*last_block
, /* last block */
1521 int whichfork
) /* data or attr fork */
1523 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1524 struct xfs_bmbt_irec got
;
1528 switch (XFS_IFORK_FORMAT(ip
, whichfork
)) {
1529 case XFS_DINODE_FMT_LOCAL
:
1532 case XFS_DINODE_FMT_BTREE
:
1533 case XFS_DINODE_FMT_EXTENTS
:
1539 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
1540 error
= xfs_iread_extents(tp
, ip
, whichfork
);
1545 if (xfs_iext_lookup_extent(ip
, ifp
, *last_block
- 1, &idx
, &got
)) {
1546 if (got
.br_startoff
<= *last_block
- 1)
1550 if (xfs_iext_get_extent(ifp
, idx
- 1, &got
)) {
1551 *last_block
= got
.br_startoff
+ got
.br_blockcount
;
1560 xfs_bmap_last_extent(
1561 struct xfs_trans
*tp
,
1562 struct xfs_inode
*ip
,
1564 struct xfs_bmbt_irec
*rec
,
1567 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1571 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
1572 error
= xfs_iread_extents(tp
, ip
, whichfork
);
1577 nextents
= xfs_iext_count(ifp
);
1578 if (nextents
== 0) {
1583 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, nextents
- 1), rec
);
1589 * Check the last inode extent to determine whether this allocation will result
1590 * in blocks being allocated at the end of the file. When we allocate new data
1591 * blocks at the end of the file which do not start at the previous data block,
1592 * we will try to align the new blocks at stripe unit boundaries.
1594 * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be
1595 * at, or past the EOF.
1599 struct xfs_bmalloca
*bma
,
1602 struct xfs_bmbt_irec rec
;
1607 error
= xfs_bmap_last_extent(NULL
, bma
->ip
, whichfork
, &rec
,
1618 * Check if we are allocation or past the last extent, or at least into
1619 * the last delayed allocated extent.
1621 bma
->aeof
= bma
->offset
>= rec
.br_startoff
+ rec
.br_blockcount
||
1622 (bma
->offset
>= rec
.br_startoff
&&
1623 isnullstartblock(rec
.br_startblock
));
1628 * Returns the file-relative block number of the first block past eof in
1629 * the file. This is not based on i_size, it is based on the extent records.
1630 * Returns 0 for local files, as they do not have extent records.
1633 xfs_bmap_last_offset(
1634 struct xfs_inode
*ip
,
1635 xfs_fileoff_t
*last_block
,
1638 struct xfs_bmbt_irec rec
;
1644 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
)
1647 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
&&
1648 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
1651 error
= xfs_bmap_last_extent(NULL
, ip
, whichfork
, &rec
, &is_empty
);
1652 if (error
|| is_empty
)
1655 *last_block
= rec
.br_startoff
+ rec
.br_blockcount
;
1660 * Returns whether the selected fork of the inode has exactly one
1661 * block or not. For the data fork we check this matches di_size,
1662 * implying the file's range is 0..bsize-1.
1664 int /* 1=>1 block, 0=>otherwise */
1666 xfs_inode_t
*ip
, /* incore inode */
1667 int whichfork
) /* data or attr fork */
1669 xfs_bmbt_rec_host_t
*ep
; /* ptr to fork's extent */
1670 xfs_ifork_t
*ifp
; /* inode fork pointer */
1671 int rval
; /* return value */
1672 xfs_bmbt_irec_t s
; /* internal version of extent */
1675 if (whichfork
== XFS_DATA_FORK
)
1676 return XFS_ISIZE(ip
) == ip
->i_mount
->m_sb
.sb_blocksize
;
1678 if (XFS_IFORK_NEXTENTS(ip
, whichfork
) != 1)
1680 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
1682 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1683 ASSERT(ifp
->if_flags
& XFS_IFEXTENTS
);
1684 ep
= xfs_iext_get_ext(ifp
, 0);
1685 xfs_bmbt_get_all(ep
, &s
);
1686 rval
= s
.br_startoff
== 0 && s
.br_blockcount
== 1;
1687 if (rval
&& whichfork
== XFS_DATA_FORK
)
1688 ASSERT(XFS_ISIZE(ip
) == ip
->i_mount
->m_sb
.sb_blocksize
);
1693 * Extent tree manipulation functions used during allocation.
1697 * Convert a delayed allocation to a real allocation.
1699 STATIC
int /* error */
1700 xfs_bmap_add_extent_delay_real(
1701 struct xfs_bmalloca
*bma
,
1704 struct xfs_bmbt_irec
*new = &bma
->got
;
1705 int diff
; /* temp value */
1706 xfs_bmbt_rec_host_t
*ep
; /* extent entry for idx */
1707 int error
; /* error return value */
1708 int i
; /* temp state */
1709 xfs_ifork_t
*ifp
; /* inode fork pointer */
1710 xfs_fileoff_t new_endoff
; /* end offset of new entry */
1711 xfs_bmbt_irec_t r
[3]; /* neighbor extent entries */
1712 /* left is 0, right is 1, prev is 2 */
1713 int rval
=0; /* return value (logging flags) */
1714 int state
= 0;/* state bits, accessed thru macros */
1715 xfs_filblks_t da_new
; /* new count del alloc blocks used */
1716 xfs_filblks_t da_old
; /* old count del alloc blocks used */
1717 xfs_filblks_t temp
=0; /* value for da_new calculations */
1718 xfs_filblks_t temp2
=0;/* value for da_new calculations */
1719 int tmp_rval
; /* partial logging flags */
1720 struct xfs_mount
*mp
;
1721 xfs_extnum_t
*nextents
;
1723 mp
= bma
->ip
->i_mount
;
1724 ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
1725 ASSERT(whichfork
!= XFS_ATTR_FORK
);
1726 nextents
= (whichfork
== XFS_COW_FORK
? &bma
->ip
->i_cnextents
:
1727 &bma
->ip
->i_d
.di_nextents
);
1729 ASSERT(bma
->idx
>= 0);
1730 ASSERT(bma
->idx
<= xfs_iext_count(ifp
));
1731 ASSERT(!isnullstartblock(new->br_startblock
));
1733 (bma
->cur
->bc_private
.b
.flags
& XFS_BTCUR_BPRV_WASDEL
));
1735 XFS_STATS_INC(mp
, xs_add_exlist
);
1741 if (whichfork
== XFS_COW_FORK
)
1742 state
|= BMAP_COWFORK
;
1745 * Set up a bunch of variables to make the tests simpler.
1747 ep
= xfs_iext_get_ext(ifp
, bma
->idx
);
1748 xfs_bmbt_get_all(ep
, &PREV
);
1749 new_endoff
= new->br_startoff
+ new->br_blockcount
;
1750 ASSERT(PREV
.br_startoff
<= new->br_startoff
);
1751 ASSERT(PREV
.br_startoff
+ PREV
.br_blockcount
>= new_endoff
);
1753 da_old
= startblockval(PREV
.br_startblock
);
1757 * Set flags determining what part of the previous delayed allocation
1758 * extent is being replaced by a real allocation.
1760 if (PREV
.br_startoff
== new->br_startoff
)
1761 state
|= BMAP_LEFT_FILLING
;
1762 if (PREV
.br_startoff
+ PREV
.br_blockcount
== new_endoff
)
1763 state
|= BMAP_RIGHT_FILLING
;
1766 * Check and set flags if this segment has a left neighbor.
1767 * Don't set contiguous if the combined extent would be too large.
1770 state
|= BMAP_LEFT_VALID
;
1771 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
- 1), &LEFT
);
1773 if (isnullstartblock(LEFT
.br_startblock
))
1774 state
|= BMAP_LEFT_DELAY
;
1777 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
1778 LEFT
.br_startoff
+ LEFT
.br_blockcount
== new->br_startoff
&&
1779 LEFT
.br_startblock
+ LEFT
.br_blockcount
== new->br_startblock
&&
1780 LEFT
.br_state
== new->br_state
&&
1781 LEFT
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
1782 state
|= BMAP_LEFT_CONTIG
;
1785 * Check and set flags if this segment has a right neighbor.
1786 * Don't set contiguous if the combined extent would be too large.
1787 * Also check for all-three-contiguous being too large.
1789 if (bma
->idx
< xfs_iext_count(ifp
) - 1) {
1790 state
|= BMAP_RIGHT_VALID
;
1791 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
+ 1), &RIGHT
);
1793 if (isnullstartblock(RIGHT
.br_startblock
))
1794 state
|= BMAP_RIGHT_DELAY
;
1797 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
1798 new_endoff
== RIGHT
.br_startoff
&&
1799 new->br_startblock
+ new->br_blockcount
== RIGHT
.br_startblock
&&
1800 new->br_state
== RIGHT
.br_state
&&
1801 new->br_blockcount
+ RIGHT
.br_blockcount
<= MAXEXTLEN
&&
1802 ((state
& (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
1803 BMAP_RIGHT_FILLING
)) !=
1804 (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
1805 BMAP_RIGHT_FILLING
) ||
1806 LEFT
.br_blockcount
+ new->br_blockcount
+ RIGHT
.br_blockcount
1808 state
|= BMAP_RIGHT_CONTIG
;
1812 * Switch out based on the FILLING and CONTIG state bits.
1814 switch (state
& (BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
1815 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
)) {
1816 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
1817 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
1819 * Filling in all of a previously delayed allocation extent.
1820 * The left and right neighbors are both contiguous with new.
1823 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1824 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, bma
->idx
),
1825 LEFT
.br_blockcount
+ PREV
.br_blockcount
+
1826 RIGHT
.br_blockcount
);
1827 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1829 xfs_iext_remove(bma
->ip
, bma
->idx
+ 1, 2, state
);
1831 if (bma
->cur
== NULL
)
1832 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1834 rval
= XFS_ILOG_CORE
;
1835 error
= xfs_bmbt_lookup_eq(bma
->cur
, RIGHT
.br_startoff
,
1836 RIGHT
.br_startblock
,
1837 RIGHT
.br_blockcount
, &i
);
1840 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1841 error
= xfs_btree_delete(bma
->cur
, &i
);
1844 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1845 error
= xfs_btree_decrement(bma
->cur
, 0, &i
);
1848 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1849 error
= xfs_bmbt_update(bma
->cur
, LEFT
.br_startoff
,
1851 LEFT
.br_blockcount
+
1852 PREV
.br_blockcount
+
1853 RIGHT
.br_blockcount
, LEFT
.br_state
);
1859 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
1861 * Filling in all of a previously delayed allocation extent.
1862 * The left neighbor is contiguous, the right is not.
1866 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1867 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, bma
->idx
),
1868 LEFT
.br_blockcount
+ PREV
.br_blockcount
);
1869 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1871 xfs_iext_remove(bma
->ip
, bma
->idx
+ 1, 1, state
);
1872 if (bma
->cur
== NULL
)
1873 rval
= XFS_ILOG_DEXT
;
1876 error
= xfs_bmbt_lookup_eq(bma
->cur
, LEFT
.br_startoff
,
1877 LEFT
.br_startblock
, LEFT
.br_blockcount
,
1881 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1882 error
= xfs_bmbt_update(bma
->cur
, LEFT
.br_startoff
,
1884 LEFT
.br_blockcount
+
1885 PREV
.br_blockcount
, LEFT
.br_state
);
1891 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
1893 * Filling in all of a previously delayed allocation extent.
1894 * The right neighbor is contiguous, the left is not.
1896 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1897 xfs_bmbt_set_startblock(ep
, new->br_startblock
);
1898 xfs_bmbt_set_blockcount(ep
,
1899 PREV
.br_blockcount
+ RIGHT
.br_blockcount
);
1900 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1902 xfs_iext_remove(bma
->ip
, bma
->idx
+ 1, 1, state
);
1903 if (bma
->cur
== NULL
)
1904 rval
= XFS_ILOG_DEXT
;
1907 error
= xfs_bmbt_lookup_eq(bma
->cur
, RIGHT
.br_startoff
,
1908 RIGHT
.br_startblock
,
1909 RIGHT
.br_blockcount
, &i
);
1912 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1913 error
= xfs_bmbt_update(bma
->cur
, PREV
.br_startoff
,
1915 PREV
.br_blockcount
+
1916 RIGHT
.br_blockcount
, PREV
.br_state
);
1922 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
1924 * Filling in all of a previously delayed allocation extent.
1925 * Neither the left nor right neighbors are contiguous with
1928 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1929 xfs_bmbt_set_startblock(ep
, new->br_startblock
);
1930 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1933 if (bma
->cur
== NULL
)
1934 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1936 rval
= XFS_ILOG_CORE
;
1937 error
= xfs_bmbt_lookup_eq(bma
->cur
, new->br_startoff
,
1938 new->br_startblock
, new->br_blockcount
,
1942 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
1943 bma
->cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
1944 error
= xfs_btree_insert(bma
->cur
, &i
);
1947 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1951 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
:
1953 * Filling in the first part of a previous delayed allocation.
1954 * The left neighbor is contiguous.
1956 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
- 1, state
, _THIS_IP_
);
1957 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, bma
->idx
- 1),
1958 LEFT
.br_blockcount
+ new->br_blockcount
);
1959 xfs_bmbt_set_startoff(ep
,
1960 PREV
.br_startoff
+ new->br_blockcount
);
1961 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
- 1, state
, _THIS_IP_
);
1963 temp
= PREV
.br_blockcount
- new->br_blockcount
;
1964 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1965 xfs_bmbt_set_blockcount(ep
, temp
);
1966 if (bma
->cur
== NULL
)
1967 rval
= XFS_ILOG_DEXT
;
1970 error
= xfs_bmbt_lookup_eq(bma
->cur
, LEFT
.br_startoff
,
1971 LEFT
.br_startblock
, LEFT
.br_blockcount
,
1975 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1976 error
= xfs_bmbt_update(bma
->cur
, LEFT
.br_startoff
,
1978 LEFT
.br_blockcount
+
1984 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
1985 startblockval(PREV
.br_startblock
));
1986 xfs_bmbt_set_startblock(ep
, nullstartblock(da_new
));
1987 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1992 case BMAP_LEFT_FILLING
:
1994 * Filling in the first part of a previous delayed allocation.
1995 * The left neighbor is not contiguous.
1997 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1998 xfs_bmbt_set_startoff(ep
, new_endoff
);
1999 temp
= PREV
.br_blockcount
- new->br_blockcount
;
2000 xfs_bmbt_set_blockcount(ep
, temp
);
2001 xfs_iext_insert(bma
->ip
, bma
->idx
, 1, new, state
);
2003 if (bma
->cur
== NULL
)
2004 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2006 rval
= XFS_ILOG_CORE
;
2007 error
= xfs_bmbt_lookup_eq(bma
->cur
, new->br_startoff
,
2008 new->br_startblock
, new->br_blockcount
,
2012 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
2013 bma
->cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
2014 error
= xfs_btree_insert(bma
->cur
, &i
);
2017 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2020 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
2021 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
2022 bma
->firstblock
, bma
->dfops
,
2023 &bma
->cur
, 1, &tmp_rval
, whichfork
);
2028 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
2029 startblockval(PREV
.br_startblock
) -
2030 (bma
->cur
? bma
->cur
->bc_private
.b
.allocated
: 0));
2031 ep
= xfs_iext_get_ext(ifp
, bma
->idx
+ 1);
2032 xfs_bmbt_set_startblock(ep
, nullstartblock(da_new
));
2033 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
+ 1, state
, _THIS_IP_
);
2036 case BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2038 * Filling in the last part of a previous delayed allocation.
2039 * The right neighbor is contiguous with the new allocation.
2041 temp
= PREV
.br_blockcount
- new->br_blockcount
;
2042 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
+ 1, state
, _THIS_IP_
);
2043 xfs_bmbt_set_blockcount(ep
, temp
);
2044 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp
, bma
->idx
+ 1),
2045 new->br_startoff
, new->br_startblock
,
2046 new->br_blockcount
+ RIGHT
.br_blockcount
,
2048 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
+ 1, state
, _THIS_IP_
);
2049 if (bma
->cur
== NULL
)
2050 rval
= XFS_ILOG_DEXT
;
2053 error
= xfs_bmbt_lookup_eq(bma
->cur
, RIGHT
.br_startoff
,
2054 RIGHT
.br_startblock
,
2055 RIGHT
.br_blockcount
, &i
);
2058 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2059 error
= xfs_bmbt_update(bma
->cur
, new->br_startoff
,
2061 new->br_blockcount
+
2062 RIGHT
.br_blockcount
,
2068 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
2069 startblockval(PREV
.br_startblock
));
2070 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2071 xfs_bmbt_set_startblock(ep
, nullstartblock(da_new
));
2072 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2077 case BMAP_RIGHT_FILLING
:
2079 * Filling in the last part of a previous delayed allocation.
2080 * The right neighbor is not contiguous.
2082 temp
= PREV
.br_blockcount
- new->br_blockcount
;
2083 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2084 xfs_bmbt_set_blockcount(ep
, temp
);
2085 xfs_iext_insert(bma
->ip
, bma
->idx
+ 1, 1, new, state
);
2087 if (bma
->cur
== NULL
)
2088 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2090 rval
= XFS_ILOG_CORE
;
2091 error
= xfs_bmbt_lookup_eq(bma
->cur
, new->br_startoff
,
2092 new->br_startblock
, new->br_blockcount
,
2096 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
2097 bma
->cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
2098 error
= xfs_btree_insert(bma
->cur
, &i
);
2101 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2104 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
2105 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
2106 bma
->firstblock
, bma
->dfops
, &bma
->cur
, 1,
2107 &tmp_rval
, whichfork
);
2112 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
2113 startblockval(PREV
.br_startblock
) -
2114 (bma
->cur
? bma
->cur
->bc_private
.b
.allocated
: 0));
2115 ep
= xfs_iext_get_ext(ifp
, bma
->idx
);
2116 xfs_bmbt_set_startblock(ep
, nullstartblock(da_new
));
2117 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2124 * Filling in the middle part of a previous delayed allocation.
2125 * Contiguity is impossible here.
2126 * This case is avoided almost all the time.
2128 * We start with a delayed allocation:
2130 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
2133 * and we are allocating:
2134 * +rrrrrrrrrrrrrrrrr+
2137 * and we set it up for insertion as:
2138 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
2140 * PREV @ idx LEFT RIGHT
2141 * inserted at idx + 1
2143 temp
= new->br_startoff
- PREV
.br_startoff
;
2144 temp2
= PREV
.br_startoff
+ PREV
.br_blockcount
- new_endoff
;
2145 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, 0, _THIS_IP_
);
2146 xfs_bmbt_set_blockcount(ep
, temp
); /* truncate PREV */
2148 RIGHT
.br_state
= PREV
.br_state
;
2149 RIGHT
.br_startblock
= nullstartblock(
2150 (int)xfs_bmap_worst_indlen(bma
->ip
, temp2
));
2151 RIGHT
.br_startoff
= new_endoff
;
2152 RIGHT
.br_blockcount
= temp2
;
2153 /* insert LEFT (r[0]) and RIGHT (r[1]) at the same time */
2154 xfs_iext_insert(bma
->ip
, bma
->idx
+ 1, 2, &LEFT
, state
);
2156 if (bma
->cur
== NULL
)
2157 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2159 rval
= XFS_ILOG_CORE
;
2160 error
= xfs_bmbt_lookup_eq(bma
->cur
, new->br_startoff
,
2161 new->br_startblock
, new->br_blockcount
,
2165 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
2166 bma
->cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
2167 error
= xfs_btree_insert(bma
->cur
, &i
);
2170 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2173 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
2174 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
2175 bma
->firstblock
, bma
->dfops
, &bma
->cur
,
2176 1, &tmp_rval
, whichfork
);
2181 temp
= xfs_bmap_worst_indlen(bma
->ip
, temp
);
2182 temp2
= xfs_bmap_worst_indlen(bma
->ip
, temp2
);
2183 diff
= (int)(temp
+ temp2
- startblockval(PREV
.br_startblock
) -
2184 (bma
->cur
? bma
->cur
->bc_private
.b
.allocated
: 0));
2186 error
= xfs_mod_fdblocks(bma
->ip
->i_mount
,
2187 -((int64_t)diff
), false);
2193 ep
= xfs_iext_get_ext(ifp
, bma
->idx
);
2194 xfs_bmbt_set_startblock(ep
, nullstartblock((int)temp
));
2195 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2196 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
+ 2, state
, _THIS_IP_
);
2197 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp
, bma
->idx
+ 2),
2198 nullstartblock((int)temp2
));
2199 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
+ 2, state
, _THIS_IP_
);
2202 da_new
= temp
+ temp2
;
2205 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2206 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2207 case BMAP_LEFT_FILLING
| BMAP_RIGHT_CONTIG
:
2208 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
2209 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2210 case BMAP_LEFT_CONTIG
:
2211 case BMAP_RIGHT_CONTIG
:
2213 * These cases are all impossible.
2218 /* add reverse mapping */
2219 error
= xfs_rmap_map_extent(mp
, bma
->dfops
, bma
->ip
, whichfork
, new);
2223 /* convert to a btree if necessary */
2224 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
2225 int tmp_logflags
; /* partial log flag return val */
2227 ASSERT(bma
->cur
== NULL
);
2228 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
2229 bma
->firstblock
, bma
->dfops
, &bma
->cur
,
2230 da_old
> 0, &tmp_logflags
, whichfork
);
2231 bma
->logflags
|= tmp_logflags
;
2236 /* adjust for changes in reserved delayed indirect blocks */
2237 if (da_old
|| da_new
) {
2240 temp
+= bma
->cur
->bc_private
.b
.allocated
;
2241 ASSERT(temp
<= da_old
);
2243 xfs_mod_fdblocks(bma
->ip
->i_mount
,
2244 (int64_t)(da_old
- temp
), false);
2247 /* clear out the allocated field, done with it now in any case. */
2249 bma
->cur
->bc_private
.b
.allocated
= 0;
2251 xfs_bmap_check_leaf_extents(bma
->cur
, bma
->ip
, whichfork
);
2253 if (whichfork
!= XFS_COW_FORK
)
2254 bma
->logflags
|= rval
;
2262 * Convert an unwritten allocation to a real allocation or vice versa.
2264 STATIC
int /* error */
2265 xfs_bmap_add_extent_unwritten_real(
2266 struct xfs_trans
*tp
,
2267 xfs_inode_t
*ip
, /* incore inode pointer */
2268 xfs_extnum_t
*idx
, /* extent number to update/insert */
2269 xfs_btree_cur_t
**curp
, /* if *curp is null, not a btree */
2270 xfs_bmbt_irec_t
*new, /* new data to add to file extents */
2271 xfs_fsblock_t
*first
, /* pointer to firstblock variable */
2272 struct xfs_defer_ops
*dfops
, /* list of extents to be freed */
2273 int *logflagsp
) /* inode logging flags */
2275 xfs_btree_cur_t
*cur
; /* btree cursor */
2276 xfs_bmbt_rec_host_t
*ep
; /* extent entry for idx */
2277 int error
; /* error return value */
2278 int i
; /* temp state */
2279 xfs_ifork_t
*ifp
; /* inode fork pointer */
2280 xfs_fileoff_t new_endoff
; /* end offset of new entry */
2281 xfs_exntst_t newext
; /* new extent state */
2282 xfs_exntst_t oldext
; /* old extent state */
2283 xfs_bmbt_irec_t r
[3]; /* neighbor extent entries */
2284 /* left is 0, right is 1, prev is 2 */
2285 int rval
=0; /* return value (logging flags) */
2286 int state
= 0;/* state bits, accessed thru macros */
2287 struct xfs_mount
*mp
= tp
->t_mountp
;
2292 ifp
= XFS_IFORK_PTR(ip
, XFS_DATA_FORK
);
2295 ASSERT(*idx
<= xfs_iext_count(ifp
));
2296 ASSERT(!isnullstartblock(new->br_startblock
));
2298 XFS_STATS_INC(mp
, xs_add_exlist
);
2305 * Set up a bunch of variables to make the tests simpler.
2308 ep
= xfs_iext_get_ext(ifp
, *idx
);
2309 xfs_bmbt_get_all(ep
, &PREV
);
2310 newext
= new->br_state
;
2311 oldext
= (newext
== XFS_EXT_UNWRITTEN
) ?
2312 XFS_EXT_NORM
: XFS_EXT_UNWRITTEN
;
2313 ASSERT(PREV
.br_state
== oldext
);
2314 new_endoff
= new->br_startoff
+ new->br_blockcount
;
2315 ASSERT(PREV
.br_startoff
<= new->br_startoff
);
2316 ASSERT(PREV
.br_startoff
+ PREV
.br_blockcount
>= new_endoff
);
2319 * Set flags determining what part of the previous oldext allocation
2320 * extent is being replaced by a newext allocation.
2322 if (PREV
.br_startoff
== new->br_startoff
)
2323 state
|= BMAP_LEFT_FILLING
;
2324 if (PREV
.br_startoff
+ PREV
.br_blockcount
== new_endoff
)
2325 state
|= BMAP_RIGHT_FILLING
;
2328 * Check and set flags if this segment has a left neighbor.
2329 * Don't set contiguous if the combined extent would be too large.
2332 state
|= BMAP_LEFT_VALID
;
2333 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, *idx
- 1), &LEFT
);
2335 if (isnullstartblock(LEFT
.br_startblock
))
2336 state
|= BMAP_LEFT_DELAY
;
2339 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
2340 LEFT
.br_startoff
+ LEFT
.br_blockcount
== new->br_startoff
&&
2341 LEFT
.br_startblock
+ LEFT
.br_blockcount
== new->br_startblock
&&
2342 LEFT
.br_state
== newext
&&
2343 LEFT
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
2344 state
|= BMAP_LEFT_CONTIG
;
2347 * Check and set flags if this segment has a right neighbor.
2348 * Don't set contiguous if the combined extent would be too large.
2349 * Also check for all-three-contiguous being too large.
2351 if (*idx
< xfs_iext_count(&ip
->i_df
) - 1) {
2352 state
|= BMAP_RIGHT_VALID
;
2353 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, *idx
+ 1), &RIGHT
);
2354 if (isnullstartblock(RIGHT
.br_startblock
))
2355 state
|= BMAP_RIGHT_DELAY
;
2358 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
2359 new_endoff
== RIGHT
.br_startoff
&&
2360 new->br_startblock
+ new->br_blockcount
== RIGHT
.br_startblock
&&
2361 newext
== RIGHT
.br_state
&&
2362 new->br_blockcount
+ RIGHT
.br_blockcount
<= MAXEXTLEN
&&
2363 ((state
& (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
2364 BMAP_RIGHT_FILLING
)) !=
2365 (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
2366 BMAP_RIGHT_FILLING
) ||
2367 LEFT
.br_blockcount
+ new->br_blockcount
+ RIGHT
.br_blockcount
2369 state
|= BMAP_RIGHT_CONTIG
;
2372 * Switch out based on the FILLING and CONTIG state bits.
2374 switch (state
& (BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
2375 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
)) {
2376 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
2377 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2379 * Setting all of a previous oldext extent to newext.
2380 * The left and right neighbors are both contiguous with new.
2384 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2385 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
),
2386 LEFT
.br_blockcount
+ PREV
.br_blockcount
+
2387 RIGHT
.br_blockcount
);
2388 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2390 xfs_iext_remove(ip
, *idx
+ 1, 2, state
);
2391 ip
->i_d
.di_nextents
-= 2;
2393 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2395 rval
= XFS_ILOG_CORE
;
2396 if ((error
= xfs_bmbt_lookup_eq(cur
, RIGHT
.br_startoff
,
2397 RIGHT
.br_startblock
,
2398 RIGHT
.br_blockcount
, &i
)))
2400 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2401 if ((error
= xfs_btree_delete(cur
, &i
)))
2403 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2404 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2406 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2407 if ((error
= xfs_btree_delete(cur
, &i
)))
2409 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2410 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2412 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2413 if ((error
= xfs_bmbt_update(cur
, LEFT
.br_startoff
,
2415 LEFT
.br_blockcount
+ PREV
.br_blockcount
+
2416 RIGHT
.br_blockcount
, LEFT
.br_state
)))
2421 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
2423 * Setting all of a previous oldext extent to newext.
2424 * The left neighbor is contiguous, the right is not.
2428 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2429 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
),
2430 LEFT
.br_blockcount
+ PREV
.br_blockcount
);
2431 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2433 xfs_iext_remove(ip
, *idx
+ 1, 1, state
);
2434 ip
->i_d
.di_nextents
--;
2436 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2438 rval
= XFS_ILOG_CORE
;
2439 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2440 PREV
.br_startblock
, PREV
.br_blockcount
,
2443 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2444 if ((error
= xfs_btree_delete(cur
, &i
)))
2446 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2447 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2449 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2450 if ((error
= xfs_bmbt_update(cur
, LEFT
.br_startoff
,
2452 LEFT
.br_blockcount
+ PREV
.br_blockcount
,
2458 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2460 * Setting all of a previous oldext extent to newext.
2461 * The right neighbor is contiguous, the left is not.
2463 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2464 xfs_bmbt_set_blockcount(ep
,
2465 PREV
.br_blockcount
+ RIGHT
.br_blockcount
);
2466 xfs_bmbt_set_state(ep
, newext
);
2467 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2468 xfs_iext_remove(ip
, *idx
+ 1, 1, state
);
2469 ip
->i_d
.di_nextents
--;
2471 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2473 rval
= XFS_ILOG_CORE
;
2474 if ((error
= xfs_bmbt_lookup_eq(cur
, RIGHT
.br_startoff
,
2475 RIGHT
.br_startblock
,
2476 RIGHT
.br_blockcount
, &i
)))
2478 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2479 if ((error
= xfs_btree_delete(cur
, &i
)))
2481 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2482 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2484 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2485 if ((error
= xfs_bmbt_update(cur
, new->br_startoff
,
2487 new->br_blockcount
+ RIGHT
.br_blockcount
,
2493 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
2495 * Setting all of a previous oldext extent to newext.
2496 * Neither the left nor right neighbors are contiguous with
2499 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2500 xfs_bmbt_set_state(ep
, newext
);
2501 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2504 rval
= XFS_ILOG_DEXT
;
2507 if ((error
= xfs_bmbt_lookup_eq(cur
, new->br_startoff
,
2508 new->br_startblock
, new->br_blockcount
,
2511 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2512 if ((error
= xfs_bmbt_update(cur
, new->br_startoff
,
2513 new->br_startblock
, new->br_blockcount
,
2519 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
:
2521 * Setting the first part of a previous oldext extent to newext.
2522 * The left neighbor is contiguous.
2524 trace_xfs_bmap_pre_update(ip
, *idx
- 1, state
, _THIS_IP_
);
2525 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
- 1),
2526 LEFT
.br_blockcount
+ new->br_blockcount
);
2527 xfs_bmbt_set_startoff(ep
,
2528 PREV
.br_startoff
+ new->br_blockcount
);
2529 trace_xfs_bmap_post_update(ip
, *idx
- 1, state
, _THIS_IP_
);
2531 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2532 xfs_bmbt_set_startblock(ep
,
2533 new->br_startblock
+ new->br_blockcount
);
2534 xfs_bmbt_set_blockcount(ep
,
2535 PREV
.br_blockcount
- new->br_blockcount
);
2536 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2541 rval
= XFS_ILOG_DEXT
;
2544 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2545 PREV
.br_startblock
, PREV
.br_blockcount
,
2548 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2549 if ((error
= xfs_bmbt_update(cur
,
2550 PREV
.br_startoff
+ new->br_blockcount
,
2551 PREV
.br_startblock
+ new->br_blockcount
,
2552 PREV
.br_blockcount
- new->br_blockcount
,
2555 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2557 error
= xfs_bmbt_update(cur
, LEFT
.br_startoff
,
2559 LEFT
.br_blockcount
+ new->br_blockcount
,
2566 case BMAP_LEFT_FILLING
:
2568 * Setting the first part of a previous oldext extent to newext.
2569 * The left neighbor is not contiguous.
2571 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2572 ASSERT(ep
&& xfs_bmbt_get_state(ep
) == oldext
);
2573 xfs_bmbt_set_startoff(ep
, new_endoff
);
2574 xfs_bmbt_set_blockcount(ep
,
2575 PREV
.br_blockcount
- new->br_blockcount
);
2576 xfs_bmbt_set_startblock(ep
,
2577 new->br_startblock
+ new->br_blockcount
);
2578 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2580 xfs_iext_insert(ip
, *idx
, 1, new, state
);
2581 ip
->i_d
.di_nextents
++;
2583 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2585 rval
= XFS_ILOG_CORE
;
2586 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2587 PREV
.br_startblock
, PREV
.br_blockcount
,
2590 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2591 if ((error
= xfs_bmbt_update(cur
,
2592 PREV
.br_startoff
+ new->br_blockcount
,
2593 PREV
.br_startblock
+ new->br_blockcount
,
2594 PREV
.br_blockcount
- new->br_blockcount
,
2597 cur
->bc_rec
.b
= *new;
2598 if ((error
= xfs_btree_insert(cur
, &i
)))
2600 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2604 case BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2606 * Setting the last part of a previous oldext extent to newext.
2607 * The right neighbor is contiguous with the new allocation.
2609 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2610 xfs_bmbt_set_blockcount(ep
,
2611 PREV
.br_blockcount
- new->br_blockcount
);
2612 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2616 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2617 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp
, *idx
),
2618 new->br_startoff
, new->br_startblock
,
2619 new->br_blockcount
+ RIGHT
.br_blockcount
, newext
);
2620 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2623 rval
= XFS_ILOG_DEXT
;
2626 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2628 PREV
.br_blockcount
, &i
)))
2630 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2631 if ((error
= xfs_bmbt_update(cur
, PREV
.br_startoff
,
2633 PREV
.br_blockcount
- new->br_blockcount
,
2636 if ((error
= xfs_btree_increment(cur
, 0, &i
)))
2638 if ((error
= xfs_bmbt_update(cur
, new->br_startoff
,
2640 new->br_blockcount
+ RIGHT
.br_blockcount
,
2646 case BMAP_RIGHT_FILLING
:
2648 * Setting the last part of a previous oldext extent to newext.
2649 * The right neighbor is not contiguous.
2651 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2652 xfs_bmbt_set_blockcount(ep
,
2653 PREV
.br_blockcount
- new->br_blockcount
);
2654 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2657 xfs_iext_insert(ip
, *idx
, 1, new, state
);
2659 ip
->i_d
.di_nextents
++;
2661 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2663 rval
= XFS_ILOG_CORE
;
2664 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2665 PREV
.br_startblock
, PREV
.br_blockcount
,
2668 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2669 if ((error
= xfs_bmbt_update(cur
, PREV
.br_startoff
,
2671 PREV
.br_blockcount
- new->br_blockcount
,
2674 if ((error
= xfs_bmbt_lookup_eq(cur
, new->br_startoff
,
2675 new->br_startblock
, new->br_blockcount
,
2678 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
2679 cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
2680 if ((error
= xfs_btree_insert(cur
, &i
)))
2682 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2688 * Setting the middle part of a previous oldext extent to
2689 * newext. Contiguity is impossible here.
2690 * One extent becomes three extents.
2692 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2693 xfs_bmbt_set_blockcount(ep
,
2694 new->br_startoff
- PREV
.br_startoff
);
2695 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2698 r
[1].br_startoff
= new_endoff
;
2699 r
[1].br_blockcount
=
2700 PREV
.br_startoff
+ PREV
.br_blockcount
- new_endoff
;
2701 r
[1].br_startblock
= new->br_startblock
+ new->br_blockcount
;
2702 r
[1].br_state
= oldext
;
2705 xfs_iext_insert(ip
, *idx
, 2, &r
[0], state
);
2707 ip
->i_d
.di_nextents
+= 2;
2709 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2711 rval
= XFS_ILOG_CORE
;
2712 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2713 PREV
.br_startblock
, PREV
.br_blockcount
,
2716 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2717 /* new right extent - oldext */
2718 if ((error
= xfs_bmbt_update(cur
, r
[1].br_startoff
,
2719 r
[1].br_startblock
, r
[1].br_blockcount
,
2722 /* new left extent - oldext */
2723 cur
->bc_rec
.b
= PREV
;
2724 cur
->bc_rec
.b
.br_blockcount
=
2725 new->br_startoff
- PREV
.br_startoff
;
2726 if ((error
= xfs_btree_insert(cur
, &i
)))
2728 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2730 * Reset the cursor to the position of the new extent
2731 * we are about to insert as we can't trust it after
2732 * the previous insert.
2734 if ((error
= xfs_bmbt_lookup_eq(cur
, new->br_startoff
,
2735 new->br_startblock
, new->br_blockcount
,
2738 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
2739 /* new middle extent - newext */
2740 cur
->bc_rec
.b
.br_state
= new->br_state
;
2741 if ((error
= xfs_btree_insert(cur
, &i
)))
2743 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2747 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2748 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2749 case BMAP_LEFT_FILLING
| BMAP_RIGHT_CONTIG
:
2750 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
2751 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2752 case BMAP_LEFT_CONTIG
:
2753 case BMAP_RIGHT_CONTIG
:
2755 * These cases are all impossible.
2760 /* update reverse mappings */
2761 error
= xfs_rmap_convert_extent(mp
, dfops
, ip
, XFS_DATA_FORK
, new);
2765 /* convert to a btree if necessary */
2766 if (xfs_bmap_needs_btree(ip
, XFS_DATA_FORK
)) {
2767 int tmp_logflags
; /* partial log flag return val */
2769 ASSERT(cur
== NULL
);
2770 error
= xfs_bmap_extents_to_btree(tp
, ip
, first
, dfops
, &cur
,
2771 0, &tmp_logflags
, XFS_DATA_FORK
);
2772 *logflagsp
|= tmp_logflags
;
2777 /* clear out the allocated field, done with it now in any case. */
2779 cur
->bc_private
.b
.allocated
= 0;
2783 xfs_bmap_check_leaf_extents(*curp
, ip
, XFS_DATA_FORK
);
2793 * Convert a hole to a delayed allocation.
2796 xfs_bmap_add_extent_hole_delay(
2797 xfs_inode_t
*ip
, /* incore inode pointer */
2799 xfs_extnum_t
*idx
, /* extent number to update/insert */
2800 xfs_bmbt_irec_t
*new) /* new data to add to file extents */
2802 xfs_ifork_t
*ifp
; /* inode fork pointer */
2803 xfs_bmbt_irec_t left
; /* left neighbor extent entry */
2804 xfs_filblks_t newlen
=0; /* new indirect size */
2805 xfs_filblks_t oldlen
=0; /* old indirect size */
2806 xfs_bmbt_irec_t right
; /* right neighbor extent entry */
2807 int state
; /* state bits, accessed thru macros */
2808 xfs_filblks_t temp
=0; /* temp for indirect calculations */
2810 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
2812 if (whichfork
== XFS_COW_FORK
)
2813 state
|= BMAP_COWFORK
;
2814 ASSERT(isnullstartblock(new->br_startblock
));
2817 * Check and set flags if this segment has a left neighbor
2820 state
|= BMAP_LEFT_VALID
;
2821 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, *idx
- 1), &left
);
2823 if (isnullstartblock(left
.br_startblock
))
2824 state
|= BMAP_LEFT_DELAY
;
2828 * Check and set flags if the current (right) segment exists.
2829 * If it doesn't exist, we're converting the hole at end-of-file.
2831 if (*idx
< xfs_iext_count(ifp
)) {
2832 state
|= BMAP_RIGHT_VALID
;
2833 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, *idx
), &right
);
2835 if (isnullstartblock(right
.br_startblock
))
2836 state
|= BMAP_RIGHT_DELAY
;
2840 * Set contiguity flags on the left and right neighbors.
2841 * Don't let extents get too large, even if the pieces are contiguous.
2843 if ((state
& BMAP_LEFT_VALID
) && (state
& BMAP_LEFT_DELAY
) &&
2844 left
.br_startoff
+ left
.br_blockcount
== new->br_startoff
&&
2845 left
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
2846 state
|= BMAP_LEFT_CONTIG
;
2848 if ((state
& BMAP_RIGHT_VALID
) && (state
& BMAP_RIGHT_DELAY
) &&
2849 new->br_startoff
+ new->br_blockcount
== right
.br_startoff
&&
2850 new->br_blockcount
+ right
.br_blockcount
<= MAXEXTLEN
&&
2851 (!(state
& BMAP_LEFT_CONTIG
) ||
2852 (left
.br_blockcount
+ new->br_blockcount
+
2853 right
.br_blockcount
<= MAXEXTLEN
)))
2854 state
|= BMAP_RIGHT_CONTIG
;
2857 * Switch out based on the contiguity flags.
2859 switch (state
& (BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
)) {
2860 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2862 * New allocation is contiguous with delayed allocations
2863 * on the left and on the right.
2864 * Merge all three into a single extent record.
2867 temp
= left
.br_blockcount
+ new->br_blockcount
+
2868 right
.br_blockcount
;
2870 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2871 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
), temp
);
2872 oldlen
= startblockval(left
.br_startblock
) +
2873 startblockval(new->br_startblock
) +
2874 startblockval(right
.br_startblock
);
2875 newlen
= xfs_bmap_worst_indlen(ip
, temp
);
2876 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp
, *idx
),
2877 nullstartblock((int)newlen
));
2878 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2880 xfs_iext_remove(ip
, *idx
+ 1, 1, state
);
2883 case BMAP_LEFT_CONTIG
:
2885 * New allocation is contiguous with a delayed allocation
2887 * Merge the new allocation with the left neighbor.
2890 temp
= left
.br_blockcount
+ new->br_blockcount
;
2892 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2893 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
), temp
);
2894 oldlen
= startblockval(left
.br_startblock
) +
2895 startblockval(new->br_startblock
);
2896 newlen
= xfs_bmap_worst_indlen(ip
, temp
);
2897 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp
, *idx
),
2898 nullstartblock((int)newlen
));
2899 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2902 case BMAP_RIGHT_CONTIG
:
2904 * New allocation is contiguous with a delayed allocation
2906 * Merge the new allocation with the right neighbor.
2908 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2909 temp
= new->br_blockcount
+ right
.br_blockcount
;
2910 oldlen
= startblockval(new->br_startblock
) +
2911 startblockval(right
.br_startblock
);
2912 newlen
= xfs_bmap_worst_indlen(ip
, temp
);
2913 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp
, *idx
),
2915 nullstartblock((int)newlen
), temp
, right
.br_state
);
2916 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2921 * New allocation is not contiguous with another
2922 * delayed allocation.
2923 * Insert a new entry.
2925 oldlen
= newlen
= 0;
2926 xfs_iext_insert(ip
, *idx
, 1, new, state
);
2929 if (oldlen
!= newlen
) {
2930 ASSERT(oldlen
> newlen
);
2931 xfs_mod_fdblocks(ip
->i_mount
, (int64_t)(oldlen
- newlen
),
2934 * Nothing to do for disk quota accounting here.
2940 * Convert a hole to a real allocation.
2942 STATIC
int /* error */
2943 xfs_bmap_add_extent_hole_real(
2944 struct xfs_bmalloca
*bma
,
2947 struct xfs_bmbt_irec
*new = &bma
->got
;
2948 int error
; /* error return value */
2949 int i
; /* temp state */
2950 xfs_ifork_t
*ifp
; /* inode fork pointer */
2951 xfs_bmbt_irec_t left
; /* left neighbor extent entry */
2952 xfs_bmbt_irec_t right
; /* right neighbor extent entry */
2953 int rval
=0; /* return value (logging flags) */
2954 int state
; /* state bits, accessed thru macros */
2955 struct xfs_mount
*mp
;
2957 mp
= bma
->ip
->i_mount
;
2958 ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
2960 ASSERT(bma
->idx
>= 0);
2961 ASSERT(bma
->idx
<= xfs_iext_count(ifp
));
2962 ASSERT(!isnullstartblock(new->br_startblock
));
2964 !(bma
->cur
->bc_private
.b
.flags
& XFS_BTCUR_BPRV_WASDEL
));
2965 ASSERT(whichfork
!= XFS_COW_FORK
);
2967 XFS_STATS_INC(mp
, xs_add_exlist
);
2970 if (whichfork
== XFS_ATTR_FORK
)
2971 state
|= BMAP_ATTRFORK
;
2974 * Check and set flags if this segment has a left neighbor.
2977 state
|= BMAP_LEFT_VALID
;
2978 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
- 1), &left
);
2979 if (isnullstartblock(left
.br_startblock
))
2980 state
|= BMAP_LEFT_DELAY
;
2984 * Check and set flags if this segment has a current value.
2985 * Not true if we're inserting into the "hole" at eof.
2987 if (bma
->idx
< xfs_iext_count(ifp
)) {
2988 state
|= BMAP_RIGHT_VALID
;
2989 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
), &right
);
2990 if (isnullstartblock(right
.br_startblock
))
2991 state
|= BMAP_RIGHT_DELAY
;
2995 * We're inserting a real allocation between "left" and "right".
2996 * Set the contiguity flags. Don't let extents get too large.
2998 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
2999 left
.br_startoff
+ left
.br_blockcount
== new->br_startoff
&&
3000 left
.br_startblock
+ left
.br_blockcount
== new->br_startblock
&&
3001 left
.br_state
== new->br_state
&&
3002 left
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
3003 state
|= BMAP_LEFT_CONTIG
;
3005 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
3006 new->br_startoff
+ new->br_blockcount
== right
.br_startoff
&&
3007 new->br_startblock
+ new->br_blockcount
== right
.br_startblock
&&
3008 new->br_state
== right
.br_state
&&
3009 new->br_blockcount
+ right
.br_blockcount
<= MAXEXTLEN
&&
3010 (!(state
& BMAP_LEFT_CONTIG
) ||
3011 left
.br_blockcount
+ new->br_blockcount
+
3012 right
.br_blockcount
<= MAXEXTLEN
))
3013 state
|= BMAP_RIGHT_CONTIG
;
3017 * Select which case we're in here, and implement it.
3019 switch (state
& (BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
)) {
3020 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
3022 * New allocation is contiguous with real allocations on the
3023 * left and on the right.
3024 * Merge all three into a single extent record.
3027 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
3028 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, bma
->idx
),
3029 left
.br_blockcount
+ new->br_blockcount
+
3030 right
.br_blockcount
);
3031 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
3033 xfs_iext_remove(bma
->ip
, bma
->idx
+ 1, 1, state
);
3035 XFS_IFORK_NEXT_SET(bma
->ip
, whichfork
,
3036 XFS_IFORK_NEXTENTS(bma
->ip
, whichfork
) - 1);
3037 if (bma
->cur
== NULL
) {
3038 rval
= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
3040 rval
= XFS_ILOG_CORE
;
3041 error
= xfs_bmbt_lookup_eq(bma
->cur
, right
.br_startoff
,
3042 right
.br_startblock
, right
.br_blockcount
,
3046 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
3047 error
= xfs_btree_delete(bma
->cur
, &i
);
3050 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
3051 error
= xfs_btree_decrement(bma
->cur
, 0, &i
);
3054 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
3055 error
= xfs_bmbt_update(bma
->cur
, left
.br_startoff
,
3057 left
.br_blockcount
+
3058 new->br_blockcount
+
3059 right
.br_blockcount
,
3066 case BMAP_LEFT_CONTIG
:
3068 * New allocation is contiguous with a real allocation
3070 * Merge the new allocation with the left neighbor.
3073 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
3074 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, bma
->idx
),
3075 left
.br_blockcount
+ new->br_blockcount
);
3076 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
3078 if (bma
->cur
== NULL
) {
3079 rval
= xfs_ilog_fext(whichfork
);
3082 error
= xfs_bmbt_lookup_eq(bma
->cur
, left
.br_startoff
,
3083 left
.br_startblock
, left
.br_blockcount
,
3087 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
3088 error
= xfs_bmbt_update(bma
->cur
, left
.br_startoff
,
3090 left
.br_blockcount
+
3098 case BMAP_RIGHT_CONTIG
:
3100 * New allocation is contiguous with a real allocation
3102 * Merge the new allocation with the right neighbor.
3104 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
3105 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp
, bma
->idx
),
3106 new->br_startoff
, new->br_startblock
,
3107 new->br_blockcount
+ right
.br_blockcount
,
3109 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
3111 if (bma
->cur
== NULL
) {
3112 rval
= xfs_ilog_fext(whichfork
);
3115 error
= xfs_bmbt_lookup_eq(bma
->cur
,
3117 right
.br_startblock
,
3118 right
.br_blockcount
, &i
);
3121 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
3122 error
= xfs_bmbt_update(bma
->cur
, new->br_startoff
,
3124 new->br_blockcount
+
3125 right
.br_blockcount
,
3134 * New allocation is not contiguous with another
3136 * Insert a new entry.
3138 xfs_iext_insert(bma
->ip
, bma
->idx
, 1, new, state
);
3139 XFS_IFORK_NEXT_SET(bma
->ip
, whichfork
,
3140 XFS_IFORK_NEXTENTS(bma
->ip
, whichfork
) + 1);
3141 if (bma
->cur
== NULL
) {
3142 rval
= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
3144 rval
= XFS_ILOG_CORE
;
3145 error
= xfs_bmbt_lookup_eq(bma
->cur
,
3148 new->br_blockcount
, &i
);
3151 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
3152 bma
->cur
->bc_rec
.b
.br_state
= new->br_state
;
3153 error
= xfs_btree_insert(bma
->cur
, &i
);
3156 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
3161 /* add reverse mapping */
3162 error
= xfs_rmap_map_extent(mp
, bma
->dfops
, bma
->ip
, whichfork
, new);
3166 /* convert to a btree if necessary */
3167 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
3168 int tmp_logflags
; /* partial log flag return val */
3170 ASSERT(bma
->cur
== NULL
);
3171 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
3172 bma
->firstblock
, bma
->dfops
, &bma
->cur
,
3173 0, &tmp_logflags
, whichfork
);
3174 bma
->logflags
|= tmp_logflags
;
3179 /* clear out the allocated field, done with it now in any case. */
3181 bma
->cur
->bc_private
.b
.allocated
= 0;
3183 xfs_bmap_check_leaf_extents(bma
->cur
, bma
->ip
, whichfork
);
3185 bma
->logflags
|= rval
;
3190 * Functions used in the extent read, allocate and remove paths
3194 * Adjust the size of the new extent based on di_extsize and rt extsize.
3197 xfs_bmap_extsize_align(
3199 xfs_bmbt_irec_t
*gotp
, /* next extent pointer */
3200 xfs_bmbt_irec_t
*prevp
, /* previous extent pointer */
3201 xfs_extlen_t extsz
, /* align to this extent size */
3202 int rt
, /* is this a realtime inode? */
3203 int eof
, /* is extent at end-of-file? */
3204 int delay
, /* creating delalloc extent? */
3205 int convert
, /* overwriting unwritten extent? */
3206 xfs_fileoff_t
*offp
, /* in/out: aligned offset */
3207 xfs_extlen_t
*lenp
) /* in/out: aligned length */
3209 xfs_fileoff_t orig_off
; /* original offset */
3210 xfs_extlen_t orig_alen
; /* original length */
3211 xfs_fileoff_t orig_end
; /* original off+len */
3212 xfs_fileoff_t nexto
; /* next file offset */
3213 xfs_fileoff_t prevo
; /* previous file offset */
3214 xfs_fileoff_t align_off
; /* temp for offset */
3215 xfs_extlen_t align_alen
; /* temp for length */
3216 xfs_extlen_t temp
; /* temp for calculations */
3221 orig_off
= align_off
= *offp
;
3222 orig_alen
= align_alen
= *lenp
;
3223 orig_end
= orig_off
+ orig_alen
;
3226 * If this request overlaps an existing extent, then don't
3227 * attempt to perform any additional alignment.
3229 if (!delay
&& !eof
&&
3230 (orig_off
>= gotp
->br_startoff
) &&
3231 (orig_end
<= gotp
->br_startoff
+ gotp
->br_blockcount
)) {
3236 * If the file offset is unaligned vs. the extent size
3237 * we need to align it. This will be possible unless
3238 * the file was previously written with a kernel that didn't
3239 * perform this alignment, or if a truncate shot us in the
3242 temp
= do_mod(orig_off
, extsz
);
3248 /* Same adjustment for the end of the requested area. */
3249 temp
= (align_alen
% extsz
);
3251 align_alen
+= extsz
- temp
;
3254 * For large extent hint sizes, the aligned extent might be larger than
3255 * MAXEXTLEN. In that case, reduce the size by an extsz so that it pulls
3256 * the length back under MAXEXTLEN. The outer allocation loops handle
3257 * short allocation just fine, so it is safe to do this. We only want to
3258 * do it when we are forced to, though, because it means more allocation
3259 * operations are required.
3261 while (align_alen
> MAXEXTLEN
)
3262 align_alen
-= extsz
;
3263 ASSERT(align_alen
<= MAXEXTLEN
);
3266 * If the previous block overlaps with this proposed allocation
3267 * then move the start forward without adjusting the length.
3269 if (prevp
->br_startoff
!= NULLFILEOFF
) {
3270 if (prevp
->br_startblock
== HOLESTARTBLOCK
)
3271 prevo
= prevp
->br_startoff
;
3273 prevo
= prevp
->br_startoff
+ prevp
->br_blockcount
;
3276 if (align_off
!= orig_off
&& align_off
< prevo
)
3279 * If the next block overlaps with this proposed allocation
3280 * then move the start back without adjusting the length,
3281 * but not before offset 0.
3282 * This may of course make the start overlap previous block,
3283 * and if we hit the offset 0 limit then the next block
3284 * can still overlap too.
3286 if (!eof
&& gotp
->br_startoff
!= NULLFILEOFF
) {
3287 if ((delay
&& gotp
->br_startblock
== HOLESTARTBLOCK
) ||
3288 (!delay
&& gotp
->br_startblock
== DELAYSTARTBLOCK
))
3289 nexto
= gotp
->br_startoff
+ gotp
->br_blockcount
;
3291 nexto
= gotp
->br_startoff
;
3293 nexto
= NULLFILEOFF
;
3295 align_off
+ align_alen
!= orig_end
&&
3296 align_off
+ align_alen
> nexto
)
3297 align_off
= nexto
> align_alen
? nexto
- align_alen
: 0;
3299 * If we're now overlapping the next or previous extent that
3300 * means we can't fit an extsz piece in this hole. Just move
3301 * the start forward to the first valid spot and set
3302 * the length so we hit the end.
3304 if (align_off
!= orig_off
&& align_off
< prevo
)
3306 if (align_off
+ align_alen
!= orig_end
&&
3307 align_off
+ align_alen
> nexto
&&
3308 nexto
!= NULLFILEOFF
) {
3309 ASSERT(nexto
> prevo
);
3310 align_alen
= nexto
- align_off
;
3314 * If realtime, and the result isn't a multiple of the realtime
3315 * extent size we need to remove blocks until it is.
3317 if (rt
&& (temp
= (align_alen
% mp
->m_sb
.sb_rextsize
))) {
3319 * We're not covering the original request, or
3320 * we won't be able to once we fix the length.
3322 if (orig_off
< align_off
||
3323 orig_end
> align_off
+ align_alen
||
3324 align_alen
- temp
< orig_alen
)
3327 * Try to fix it by moving the start up.
3329 if (align_off
+ temp
<= orig_off
) {
3334 * Try to fix it by moving the end in.
3336 else if (align_off
+ align_alen
- temp
>= orig_end
)
3339 * Set the start to the minimum then trim the length.
3342 align_alen
-= orig_off
- align_off
;
3343 align_off
= orig_off
;
3344 align_alen
-= align_alen
% mp
->m_sb
.sb_rextsize
;
3347 * Result doesn't cover the request, fail it.
3349 if (orig_off
< align_off
|| orig_end
> align_off
+ align_alen
)
3352 ASSERT(orig_off
>= align_off
);
3353 /* see MAXEXTLEN handling above */
3354 ASSERT(orig_end
<= align_off
+ align_alen
||
3355 align_alen
+ extsz
> MAXEXTLEN
);
3359 if (!eof
&& gotp
->br_startoff
!= NULLFILEOFF
)
3360 ASSERT(align_off
+ align_alen
<= gotp
->br_startoff
);
3361 if (prevp
->br_startoff
!= NULLFILEOFF
)
3362 ASSERT(align_off
>= prevp
->br_startoff
+ prevp
->br_blockcount
);
3370 #define XFS_ALLOC_GAP_UNITS 4
3374 struct xfs_bmalloca
*ap
) /* bmap alloc argument struct */
3376 xfs_fsblock_t adjust
; /* adjustment to block numbers */
3377 xfs_agnumber_t fb_agno
; /* ag number of ap->firstblock */
3378 xfs_mount_t
*mp
; /* mount point structure */
3379 int nullfb
; /* true if ap->firstblock isn't set */
3380 int rt
; /* true if inode is realtime */
3382 #define ISVALID(x,y) \
3384 (x) < mp->m_sb.sb_rblocks : \
3385 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
3386 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
3387 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
3389 mp
= ap
->ip
->i_mount
;
3390 nullfb
= *ap
->firstblock
== NULLFSBLOCK
;
3391 rt
= XFS_IS_REALTIME_INODE(ap
->ip
) &&
3392 xfs_alloc_is_userdata(ap
->datatype
);
3393 fb_agno
= nullfb
? NULLAGNUMBER
: XFS_FSB_TO_AGNO(mp
, *ap
->firstblock
);
3395 * If allocating at eof, and there's a previous real block,
3396 * try to use its last block as our starting point.
3398 if (ap
->eof
&& ap
->prev
.br_startoff
!= NULLFILEOFF
&&
3399 !isnullstartblock(ap
->prev
.br_startblock
) &&
3400 ISVALID(ap
->prev
.br_startblock
+ ap
->prev
.br_blockcount
,
3401 ap
->prev
.br_startblock
)) {
3402 ap
->blkno
= ap
->prev
.br_startblock
+ ap
->prev
.br_blockcount
;
3404 * Adjust for the gap between prevp and us.
3406 adjust
= ap
->offset
-
3407 (ap
->prev
.br_startoff
+ ap
->prev
.br_blockcount
);
3409 ISVALID(ap
->blkno
+ adjust
, ap
->prev
.br_startblock
))
3410 ap
->blkno
+= adjust
;
3413 * If not at eof, then compare the two neighbor blocks.
3414 * Figure out whether either one gives us a good starting point,
3415 * and pick the better one.
3417 else if (!ap
->eof
) {
3418 xfs_fsblock_t gotbno
; /* right side block number */
3419 xfs_fsblock_t gotdiff
=0; /* right side difference */
3420 xfs_fsblock_t prevbno
; /* left side block number */
3421 xfs_fsblock_t prevdiff
=0; /* left side difference */
3424 * If there's a previous (left) block, select a requested
3425 * start block based on it.
3427 if (ap
->prev
.br_startoff
!= NULLFILEOFF
&&
3428 !isnullstartblock(ap
->prev
.br_startblock
) &&
3429 (prevbno
= ap
->prev
.br_startblock
+
3430 ap
->prev
.br_blockcount
) &&
3431 ISVALID(prevbno
, ap
->prev
.br_startblock
)) {
3433 * Calculate gap to end of previous block.
3435 adjust
= prevdiff
= ap
->offset
-
3436 (ap
->prev
.br_startoff
+
3437 ap
->prev
.br_blockcount
);
3439 * Figure the startblock based on the previous block's
3440 * end and the gap size.
3442 * If the gap is large relative to the piece we're
3443 * allocating, or using it gives us an invalid block
3444 * number, then just use the end of the previous block.
3446 if (prevdiff
<= XFS_ALLOC_GAP_UNITS
* ap
->length
&&
3447 ISVALID(prevbno
+ prevdiff
,
3448 ap
->prev
.br_startblock
))
3453 * If the firstblock forbids it, can't use it,
3456 if (!rt
&& !nullfb
&&
3457 XFS_FSB_TO_AGNO(mp
, prevbno
) != fb_agno
)
3458 prevbno
= NULLFSBLOCK
;
3461 * No previous block or can't follow it, just default.
3464 prevbno
= NULLFSBLOCK
;
3466 * If there's a following (right) block, select a requested
3467 * start block based on it.
3469 if (!isnullstartblock(ap
->got
.br_startblock
)) {
3471 * Calculate gap to start of next block.
3473 adjust
= gotdiff
= ap
->got
.br_startoff
- ap
->offset
;
3475 * Figure the startblock based on the next block's
3476 * start and the gap size.
3478 gotbno
= ap
->got
.br_startblock
;
3481 * If the gap is large relative to the piece we're
3482 * allocating, or using it gives us an invalid block
3483 * number, then just use the start of the next block
3484 * offset by our length.
3486 if (gotdiff
<= XFS_ALLOC_GAP_UNITS
* ap
->length
&&
3487 ISVALID(gotbno
- gotdiff
, gotbno
))
3489 else if (ISVALID(gotbno
- ap
->length
, gotbno
)) {
3490 gotbno
-= ap
->length
;
3491 gotdiff
+= adjust
- ap
->length
;
3495 * If the firstblock forbids it, can't use it,
3498 if (!rt
&& !nullfb
&&
3499 XFS_FSB_TO_AGNO(mp
, gotbno
) != fb_agno
)
3500 gotbno
= NULLFSBLOCK
;
3503 * No next block, just default.
3506 gotbno
= NULLFSBLOCK
;
3508 * If both valid, pick the better one, else the only good
3509 * one, else ap->blkno is already set (to 0 or the inode block).
3511 if (prevbno
!= NULLFSBLOCK
&& gotbno
!= NULLFSBLOCK
)
3512 ap
->blkno
= prevdiff
<= gotdiff
? prevbno
: gotbno
;
3513 else if (prevbno
!= NULLFSBLOCK
)
3514 ap
->blkno
= prevbno
;
3515 else if (gotbno
!= NULLFSBLOCK
)
3522 xfs_bmap_longest_free_extent(
3523 struct xfs_trans
*tp
,
3528 struct xfs_mount
*mp
= tp
->t_mountp
;
3529 struct xfs_perag
*pag
;
3530 xfs_extlen_t longest
;
3533 pag
= xfs_perag_get(mp
, ag
);
3534 if (!pag
->pagf_init
) {
3535 error
= xfs_alloc_pagf_init(mp
, tp
, ag
, XFS_ALLOC_FLAG_TRYLOCK
);
3539 if (!pag
->pagf_init
) {
3545 longest
= xfs_alloc_longest_free_extent(mp
, pag
,
3546 xfs_alloc_min_freelist(mp
, pag
),
3547 xfs_ag_resv_needed(pag
, XFS_AG_RESV_NONE
));
3548 if (*blen
< longest
)
3557 xfs_bmap_select_minlen(
3558 struct xfs_bmalloca
*ap
,
3559 struct xfs_alloc_arg
*args
,
3563 if (notinit
|| *blen
< ap
->minlen
) {
3565 * Since we did a BUF_TRYLOCK above, it is possible that
3566 * there is space for this request.
3568 args
->minlen
= ap
->minlen
;
3569 } else if (*blen
< args
->maxlen
) {
3571 * If the best seen length is less than the request length,
3572 * use the best as the minimum.
3574 args
->minlen
= *blen
;
3577 * Otherwise we've seen an extent as big as maxlen, use that
3580 args
->minlen
= args
->maxlen
;
3585 xfs_bmap_btalloc_nullfb(
3586 struct xfs_bmalloca
*ap
,
3587 struct xfs_alloc_arg
*args
,
3590 struct xfs_mount
*mp
= ap
->ip
->i_mount
;
3591 xfs_agnumber_t ag
, startag
;
3595 args
->type
= XFS_ALLOCTYPE_START_BNO
;
3596 args
->total
= ap
->total
;
3598 startag
= ag
= XFS_FSB_TO_AGNO(mp
, args
->fsbno
);
3599 if (startag
== NULLAGNUMBER
)
3602 while (*blen
< args
->maxlen
) {
3603 error
= xfs_bmap_longest_free_extent(args
->tp
, ag
, blen
,
3608 if (++ag
== mp
->m_sb
.sb_agcount
)
3614 xfs_bmap_select_minlen(ap
, args
, blen
, notinit
);
3619 xfs_bmap_btalloc_filestreams(
3620 struct xfs_bmalloca
*ap
,
3621 struct xfs_alloc_arg
*args
,
3624 struct xfs_mount
*mp
= ap
->ip
->i_mount
;
3629 args
->type
= XFS_ALLOCTYPE_NEAR_BNO
;
3630 args
->total
= ap
->total
;
3632 ag
= XFS_FSB_TO_AGNO(mp
, args
->fsbno
);
3633 if (ag
== NULLAGNUMBER
)
3636 error
= xfs_bmap_longest_free_extent(args
->tp
, ag
, blen
, ¬init
);
3640 if (*blen
< args
->maxlen
) {
3641 error
= xfs_filestream_new_ag(ap
, &ag
);
3645 error
= xfs_bmap_longest_free_extent(args
->tp
, ag
, blen
,
3652 xfs_bmap_select_minlen(ap
, args
, blen
, notinit
);
3655 * Set the failure fallback case to look in the selected AG as stream
3658 ap
->blkno
= args
->fsbno
= XFS_AGB_TO_FSB(mp
, ag
, 0);
3664 struct xfs_bmalloca
*ap
) /* bmap alloc argument struct */
3666 xfs_mount_t
*mp
; /* mount point structure */
3667 xfs_alloctype_t atype
= 0; /* type for allocation routines */
3668 xfs_extlen_t align
= 0; /* minimum allocation alignment */
3669 xfs_agnumber_t fb_agno
; /* ag number of ap->firstblock */
3671 xfs_alloc_arg_t args
;
3673 xfs_extlen_t nextminlen
= 0;
3674 int nullfb
; /* true if ap->firstblock isn't set */
3682 mp
= ap
->ip
->i_mount
;
3684 /* stripe alignment for allocation is determined by mount parameters */
3686 if (mp
->m_swidth
&& (mp
->m_flags
& XFS_MOUNT_SWALLOC
))
3687 stripe_align
= mp
->m_swidth
;
3688 else if (mp
->m_dalign
)
3689 stripe_align
= mp
->m_dalign
;
3691 if (ap
->flags
& XFS_BMAPI_COWFORK
)
3692 align
= xfs_get_cowextsz_hint(ap
->ip
);
3693 else if (xfs_alloc_is_userdata(ap
->datatype
))
3694 align
= xfs_get_extsz_hint(ap
->ip
);
3695 if (unlikely(align
)) {
3696 error
= xfs_bmap_extsize_align(mp
, &ap
->got
, &ap
->prev
,
3697 align
, 0, ap
->eof
, 0, ap
->conv
,
3698 &ap
->offset
, &ap
->length
);
3704 nullfb
= *ap
->firstblock
== NULLFSBLOCK
;
3705 fb_agno
= nullfb
? NULLAGNUMBER
: XFS_FSB_TO_AGNO(mp
, *ap
->firstblock
);
3707 if (xfs_alloc_is_userdata(ap
->datatype
) &&
3708 xfs_inode_is_filestream(ap
->ip
)) {
3709 ag
= xfs_filestream_lookup_ag(ap
->ip
);
3710 ag
= (ag
!= NULLAGNUMBER
) ? ag
: 0;
3711 ap
->blkno
= XFS_AGB_TO_FSB(mp
, ag
, 0);
3713 ap
->blkno
= XFS_INO_TO_FSB(mp
, ap
->ip
->i_ino
);
3716 ap
->blkno
= *ap
->firstblock
;
3718 xfs_bmap_adjacent(ap
);
3721 * If allowed, use ap->blkno; otherwise must use firstblock since
3722 * it's in the right allocation group.
3724 if (nullfb
|| XFS_FSB_TO_AGNO(mp
, ap
->blkno
) == fb_agno
)
3727 ap
->blkno
= *ap
->firstblock
;
3729 * Normal allocation, done through xfs_alloc_vextent.
3731 tryagain
= isaligned
= 0;
3732 memset(&args
, 0, sizeof(args
));
3735 args
.fsbno
= ap
->blkno
;
3736 xfs_rmap_skip_owner_update(&args
.oinfo
);
3738 /* Trim the allocation back to the maximum an AG can fit. */
3739 args
.maxlen
= MIN(ap
->length
, mp
->m_ag_max_usable
);
3740 args
.firstblock
= *ap
->firstblock
;
3744 * Search for an allocation group with a single extent large
3745 * enough for the request. If one isn't found, then adjust
3746 * the minimum allocation size to the largest space found.
3748 if (xfs_alloc_is_userdata(ap
->datatype
) &&
3749 xfs_inode_is_filestream(ap
->ip
))
3750 error
= xfs_bmap_btalloc_filestreams(ap
, &args
, &blen
);
3752 error
= xfs_bmap_btalloc_nullfb(ap
, &args
, &blen
);
3755 } else if (ap
->dfops
->dop_low
) {
3756 if (xfs_inode_is_filestream(ap
->ip
))
3757 args
.type
= XFS_ALLOCTYPE_FIRST_AG
;
3759 args
.type
= XFS_ALLOCTYPE_START_BNO
;
3760 args
.total
= args
.minlen
= ap
->minlen
;
3762 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
3763 args
.total
= ap
->total
;
3764 args
.minlen
= ap
->minlen
;
3766 /* apply extent size hints if obtained earlier */
3767 if (unlikely(align
)) {
3769 if ((args
.mod
= (xfs_extlen_t
)do_mod(ap
->offset
, args
.prod
)))
3770 args
.mod
= (xfs_extlen_t
)(args
.prod
- args
.mod
);
3771 } else if (mp
->m_sb
.sb_blocksize
>= PAGE_SIZE
) {
3775 args
.prod
= PAGE_SIZE
>> mp
->m_sb
.sb_blocklog
;
3776 if ((args
.mod
= (xfs_extlen_t
)(do_mod(ap
->offset
, args
.prod
))))
3777 args
.mod
= (xfs_extlen_t
)(args
.prod
- args
.mod
);
3780 * If we are not low on available data blocks, and the
3781 * underlying logical volume manager is a stripe, and
3782 * the file offset is zero then try to allocate data
3783 * blocks on stripe unit boundary.
3784 * NOTE: ap->aeof is only set if the allocation length
3785 * is >= the stripe unit and the allocation offset is
3786 * at the end of file.
3788 if (!ap
->dfops
->dop_low
&& ap
->aeof
) {
3790 args
.alignment
= stripe_align
;
3794 * Adjust for alignment
3796 if (blen
> args
.alignment
&& blen
<= args
.maxlen
)
3797 args
.minlen
= blen
- args
.alignment
;
3798 args
.minalignslop
= 0;
3801 * First try an exact bno allocation.
3802 * If it fails then do a near or start bno
3803 * allocation with alignment turned on.
3807 args
.type
= XFS_ALLOCTYPE_THIS_BNO
;
3810 * Compute the minlen+alignment for the
3811 * next case. Set slop so that the value
3812 * of minlen+alignment+slop doesn't go up
3813 * between the calls.
3815 if (blen
> stripe_align
&& blen
<= args
.maxlen
)
3816 nextminlen
= blen
- stripe_align
;
3818 nextminlen
= args
.minlen
;
3819 if (nextminlen
+ stripe_align
> args
.minlen
+ 1)
3821 nextminlen
+ stripe_align
-
3824 args
.minalignslop
= 0;
3828 args
.minalignslop
= 0;
3830 args
.minleft
= ap
->minleft
;
3831 args
.wasdel
= ap
->wasdel
;
3832 args
.resv
= XFS_AG_RESV_NONE
;
3833 args
.datatype
= ap
->datatype
;
3834 if (ap
->datatype
& XFS_ALLOC_USERDATA_ZERO
)
3837 error
= xfs_alloc_vextent(&args
);
3841 if (tryagain
&& args
.fsbno
== NULLFSBLOCK
) {
3843 * Exact allocation failed. Now try with alignment
3847 args
.fsbno
= ap
->blkno
;
3848 args
.alignment
= stripe_align
;
3849 args
.minlen
= nextminlen
;
3850 args
.minalignslop
= 0;
3852 if ((error
= xfs_alloc_vextent(&args
)))
3855 if (isaligned
&& args
.fsbno
== NULLFSBLOCK
) {
3857 * allocation failed, so turn off alignment and
3861 args
.fsbno
= ap
->blkno
;
3863 if ((error
= xfs_alloc_vextent(&args
)))
3866 if (args
.fsbno
== NULLFSBLOCK
&& nullfb
&&
3867 args
.minlen
> ap
->minlen
) {
3868 args
.minlen
= ap
->minlen
;
3869 args
.type
= XFS_ALLOCTYPE_START_BNO
;
3870 args
.fsbno
= ap
->blkno
;
3871 if ((error
= xfs_alloc_vextent(&args
)))
3874 if (args
.fsbno
== NULLFSBLOCK
&& nullfb
) {
3876 args
.type
= XFS_ALLOCTYPE_FIRST_AG
;
3877 args
.total
= ap
->minlen
;
3879 if ((error
= xfs_alloc_vextent(&args
)))
3881 ap
->dfops
->dop_low
= true;
3883 if (args
.fsbno
!= NULLFSBLOCK
) {
3885 * check the allocation happened at the same or higher AG than
3886 * the first block that was allocated.
3888 ASSERT(*ap
->firstblock
== NULLFSBLOCK
||
3889 XFS_FSB_TO_AGNO(mp
, *ap
->firstblock
) ==
3890 XFS_FSB_TO_AGNO(mp
, args
.fsbno
) ||
3891 (ap
->dfops
->dop_low
&&
3892 XFS_FSB_TO_AGNO(mp
, *ap
->firstblock
) <
3893 XFS_FSB_TO_AGNO(mp
, args
.fsbno
)));
3895 ap
->blkno
= args
.fsbno
;
3896 if (*ap
->firstblock
== NULLFSBLOCK
)
3897 *ap
->firstblock
= args
.fsbno
;
3898 ASSERT(nullfb
|| fb_agno
== args
.agno
||
3899 (ap
->dfops
->dop_low
&& fb_agno
< args
.agno
));
3900 ap
->length
= args
.len
;
3901 if (!(ap
->flags
& XFS_BMAPI_COWFORK
))
3902 ap
->ip
->i_d
.di_nblocks
+= args
.len
;
3903 xfs_trans_log_inode(ap
->tp
, ap
->ip
, XFS_ILOG_CORE
);
3905 ap
->ip
->i_delayed_blks
-= args
.len
;
3907 * Adjust the disk quota also. This was reserved
3910 xfs_trans_mod_dquot_byino(ap
->tp
, ap
->ip
,
3911 ap
->wasdel
? XFS_TRANS_DQ_DELBCOUNT
:
3912 XFS_TRANS_DQ_BCOUNT
,
3915 ap
->blkno
= NULLFSBLOCK
;
3922 * For a remap operation, just "allocate" an extent at the address that the
3923 * caller passed in, and ensure that the AGFL is the right size. The caller
3924 * will then map the "allocated" extent into the file somewhere.
3927 xfs_bmap_remap_alloc(
3928 struct xfs_bmalloca
*ap
)
3930 struct xfs_trans
*tp
= ap
->tp
;
3931 struct xfs_mount
*mp
= tp
->t_mountp
;
3933 struct xfs_alloc_arg args
;
3937 * validate that the block number is legal - the enables us to detect
3938 * and handle a silent filesystem corruption rather than crashing.
3940 memset(&args
, 0, sizeof(struct xfs_alloc_arg
));
3942 args
.mp
= ap
->tp
->t_mountp
;
3943 bno
= *ap
->firstblock
;
3944 args
.agno
= XFS_FSB_TO_AGNO(mp
, bno
);
3945 args
.agbno
= XFS_FSB_TO_AGBNO(mp
, bno
);
3946 if (args
.agno
>= mp
->m_sb
.sb_agcount
||
3947 args
.agbno
>= mp
->m_sb
.sb_agblocks
)
3948 return -EFSCORRUPTED
;
3950 /* "Allocate" the extent from the range we passed in. */
3951 trace_xfs_bmap_remap_alloc(ap
->ip
, *ap
->firstblock
, ap
->length
);
3953 ap
->ip
->i_d
.di_nblocks
+= ap
->length
;
3954 xfs_trans_log_inode(ap
->tp
, ap
->ip
, XFS_ILOG_CORE
);
3956 /* Fix the freelist, like a real allocator does. */
3957 args
.datatype
= ap
->datatype
;
3958 args
.pag
= xfs_perag_get(args
.mp
, args
.agno
);
3962 * The freelist fixing code will decline the allocation if
3963 * the size and shape of the free space doesn't allow for
3964 * allocating the extent and updating all the metadata that
3965 * happens during an allocation. We're remapping, not
3966 * allocating, so skip that check by pretending to be freeing.
3968 error
= xfs_alloc_fix_freelist(&args
, XFS_ALLOC_FLAG_FREEING
);
3969 xfs_perag_put(args
.pag
);
3971 trace_xfs_bmap_remap_alloc_error(ap
->ip
, error
, _RET_IP_
);
3976 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
3977 * It figures out where to ask the underlying allocator to put the new extent.
3981 struct xfs_bmalloca
*ap
) /* bmap alloc argument struct */
3983 if (ap
->flags
& XFS_BMAPI_REMAP
)
3984 return xfs_bmap_remap_alloc(ap
);
3985 if (XFS_IS_REALTIME_INODE(ap
->ip
) &&
3986 xfs_alloc_is_userdata(ap
->datatype
))
3987 return xfs_bmap_rtalloc(ap
);
3988 return xfs_bmap_btalloc(ap
);
3991 /* Trim extent to fit a logical block range. */
3994 struct xfs_bmbt_irec
*irec
,
3998 xfs_fileoff_t distance
;
3999 xfs_fileoff_t end
= bno
+ len
;
4001 if (irec
->br_startoff
+ irec
->br_blockcount
<= bno
||
4002 irec
->br_startoff
>= end
) {
4003 irec
->br_blockcount
= 0;
4007 if (irec
->br_startoff
< bno
) {
4008 distance
= bno
- irec
->br_startoff
;
4009 if (isnullstartblock(irec
->br_startblock
))
4010 irec
->br_startblock
= DELAYSTARTBLOCK
;
4011 if (irec
->br_startblock
!= DELAYSTARTBLOCK
&&
4012 irec
->br_startblock
!= HOLESTARTBLOCK
)
4013 irec
->br_startblock
+= distance
;
4014 irec
->br_startoff
+= distance
;
4015 irec
->br_blockcount
-= distance
;
4018 if (end
< irec
->br_startoff
+ irec
->br_blockcount
) {
4019 distance
= irec
->br_startoff
+ irec
->br_blockcount
- end
;
4020 irec
->br_blockcount
-= distance
;
4025 * Trim the returned map to the required bounds
4029 struct xfs_bmbt_irec
*mval
,
4030 struct xfs_bmbt_irec
*got
,
4038 if ((flags
& XFS_BMAPI_ENTIRE
) ||
4039 got
->br_startoff
+ got
->br_blockcount
<= obno
) {
4041 if (isnullstartblock(got
->br_startblock
))
4042 mval
->br_startblock
= DELAYSTARTBLOCK
;
4048 ASSERT((*bno
>= obno
) || (n
== 0));
4050 mval
->br_startoff
= *bno
;
4051 if (isnullstartblock(got
->br_startblock
))
4052 mval
->br_startblock
= DELAYSTARTBLOCK
;
4054 mval
->br_startblock
= got
->br_startblock
+
4055 (*bno
- got
->br_startoff
);
4057 * Return the minimum of what we got and what we asked for for
4058 * the length. We can use the len variable here because it is
4059 * modified below and we could have been there before coming
4060 * here if the first part of the allocation didn't overlap what
4063 mval
->br_blockcount
= XFS_FILBLKS_MIN(end
- *bno
,
4064 got
->br_blockcount
- (*bno
- got
->br_startoff
));
4065 mval
->br_state
= got
->br_state
;
4066 ASSERT(mval
->br_blockcount
<= len
);
4071 * Update and validate the extent map to return
4074 xfs_bmapi_update_map(
4075 struct xfs_bmbt_irec
**map
,
4083 xfs_bmbt_irec_t
*mval
= *map
;
4085 ASSERT((flags
& XFS_BMAPI_ENTIRE
) ||
4086 ((mval
->br_startoff
+ mval
->br_blockcount
) <= end
));
4087 ASSERT((flags
& XFS_BMAPI_ENTIRE
) || (mval
->br_blockcount
<= *len
) ||
4088 (mval
->br_startoff
< obno
));
4090 *bno
= mval
->br_startoff
+ mval
->br_blockcount
;
4092 if (*n
> 0 && mval
->br_startoff
== mval
[-1].br_startoff
) {
4093 /* update previous map with new information */
4094 ASSERT(mval
->br_startblock
== mval
[-1].br_startblock
);
4095 ASSERT(mval
->br_blockcount
> mval
[-1].br_blockcount
);
4096 ASSERT(mval
->br_state
== mval
[-1].br_state
);
4097 mval
[-1].br_blockcount
= mval
->br_blockcount
;
4098 mval
[-1].br_state
= mval
->br_state
;
4099 } else if (*n
> 0 && mval
->br_startblock
!= DELAYSTARTBLOCK
&&
4100 mval
[-1].br_startblock
!= DELAYSTARTBLOCK
&&
4101 mval
[-1].br_startblock
!= HOLESTARTBLOCK
&&
4102 mval
->br_startblock
== mval
[-1].br_startblock
+
4103 mval
[-1].br_blockcount
&&
4104 ((flags
& XFS_BMAPI_IGSTATE
) ||
4105 mval
[-1].br_state
== mval
->br_state
)) {
4106 ASSERT(mval
->br_startoff
==
4107 mval
[-1].br_startoff
+ mval
[-1].br_blockcount
);
4108 mval
[-1].br_blockcount
+= mval
->br_blockcount
;
4109 } else if (*n
> 0 &&
4110 mval
->br_startblock
== DELAYSTARTBLOCK
&&
4111 mval
[-1].br_startblock
== DELAYSTARTBLOCK
&&
4112 mval
->br_startoff
==
4113 mval
[-1].br_startoff
+ mval
[-1].br_blockcount
) {
4114 mval
[-1].br_blockcount
+= mval
->br_blockcount
;
4115 mval
[-1].br_state
= mval
->br_state
;
4116 } else if (!((*n
== 0) &&
4117 ((mval
->br_startoff
+ mval
->br_blockcount
) <=
4126 * Map file blocks to filesystem blocks without allocation.
4130 struct xfs_inode
*ip
,
4133 struct xfs_bmbt_irec
*mval
,
4137 struct xfs_mount
*mp
= ip
->i_mount
;
4138 struct xfs_ifork
*ifp
;
4139 struct xfs_bmbt_irec got
;
4146 int whichfork
= xfs_bmapi_whichfork(flags
);
4149 ASSERT(!(flags
& ~(XFS_BMAPI_ATTRFORK
|XFS_BMAPI_ENTIRE
|
4150 XFS_BMAPI_IGSTATE
|XFS_BMAPI_COWFORK
)));
4151 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_SHARED
|XFS_ILOCK_EXCL
));
4153 if (unlikely(XFS_TEST_ERROR(
4154 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
4155 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
4156 mp
, XFS_ERRTAG_BMAPIFORMAT
, XFS_RANDOM_BMAPIFORMAT
))) {
4157 XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW
, mp
);
4158 return -EFSCORRUPTED
;
4161 if (XFS_FORCED_SHUTDOWN(mp
))
4164 XFS_STATS_INC(mp
, xs_blk_mapr
);
4166 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4168 /* No CoW fork? Return a hole. */
4169 if (whichfork
== XFS_COW_FORK
&& !ifp
) {
4170 mval
->br_startoff
= bno
;
4171 mval
->br_startblock
= HOLESTARTBLOCK
;
4172 mval
->br_blockcount
= len
;
4173 mval
->br_state
= XFS_EXT_NORM
;
4178 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
4179 error
= xfs_iread_extents(NULL
, ip
, whichfork
);
4184 if (!xfs_iext_lookup_extent(ip
, ifp
, bno
, &idx
, &got
))
4189 while (bno
< end
&& n
< *nmap
) {
4190 /* Reading past eof, act as though there's a hole up to end. */
4192 got
.br_startoff
= end
;
4193 if (got
.br_startoff
> bno
) {
4194 /* Reading in a hole. */
4195 mval
->br_startoff
= bno
;
4196 mval
->br_startblock
= HOLESTARTBLOCK
;
4197 mval
->br_blockcount
=
4198 XFS_FILBLKS_MIN(len
, got
.br_startoff
- bno
);
4199 mval
->br_state
= XFS_EXT_NORM
;
4200 bno
+= mval
->br_blockcount
;
4201 len
-= mval
->br_blockcount
;
4207 /* set up the extent map to return. */
4208 xfs_bmapi_trim_map(mval
, &got
, &bno
, len
, obno
, end
, n
, flags
);
4209 xfs_bmapi_update_map(&mval
, &bno
, &len
, obno
, end
, &n
, flags
);
4211 /* If we're done, stop now. */
4212 if (bno
>= end
|| n
>= *nmap
)
4215 /* Else go on to the next record. */
4216 if (!xfs_iext_get_extent(ifp
, ++idx
, &got
))
4224 xfs_bmapi_reserve_delalloc(
4225 struct xfs_inode
*ip
,
4229 struct xfs_bmbt_irec
*got
,
4230 struct xfs_bmbt_irec
*prev
,
4231 xfs_extnum_t
*lastx
,
4234 struct xfs_mount
*mp
= ip
->i_mount
;
4235 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4237 xfs_extlen_t indlen
;
4238 char rt
= XFS_IS_REALTIME_INODE(ip
);
4242 alen
= XFS_FILBLKS_MIN(len
, MAXEXTLEN
);
4244 alen
= XFS_FILBLKS_MIN(alen
, got
->br_startoff
- aoff
);
4246 /* Figure out the extent size, adjust alen */
4247 if (whichfork
== XFS_COW_FORK
)
4248 extsz
= xfs_get_cowextsz_hint(ip
);
4250 extsz
= xfs_get_extsz_hint(ip
);
4252 error
= xfs_bmap_extsize_align(mp
, got
, prev
, extsz
, rt
, eof
,
4253 1, 0, &aoff
, &alen
);
4258 extsz
= alen
/ mp
->m_sb
.sb_rextsize
;
4261 * Make a transaction-less quota reservation for delayed allocation
4262 * blocks. This number gets adjusted later. We return if we haven't
4263 * allocated blocks already inside this loop.
4265 error
= xfs_trans_reserve_quota_nblks(NULL
, ip
, (long)alen
, 0,
4266 rt
? XFS_QMOPT_RES_RTBLKS
: XFS_QMOPT_RES_REGBLKS
);
4271 * Split changing sb for alen and indlen since they could be coming
4272 * from different places.
4274 indlen
= (xfs_extlen_t
)xfs_bmap_worst_indlen(ip
, alen
);
4278 error
= xfs_mod_frextents(mp
, -((int64_t)extsz
));
4280 error
= xfs_mod_fdblocks(mp
, -((int64_t)alen
), false);
4284 goto out_unreserve_quota
;
4286 error
= xfs_mod_fdblocks(mp
, -((int64_t)indlen
), false);
4288 goto out_unreserve_blocks
;
4291 ip
->i_delayed_blks
+= alen
;
4293 got
->br_startoff
= aoff
;
4294 got
->br_startblock
= nullstartblock(indlen
);
4295 got
->br_blockcount
= alen
;
4296 got
->br_state
= XFS_EXT_NORM
;
4297 xfs_bmap_add_extent_hole_delay(ip
, whichfork
, lastx
, got
);
4300 * Update our extent pointer, given that xfs_bmap_add_extent_hole_delay
4301 * might have merged it into one of the neighbouring ones.
4303 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, *lastx
), got
);
4305 ASSERT(got
->br_startoff
<= aoff
);
4306 ASSERT(got
->br_startoff
+ got
->br_blockcount
>= aoff
+ alen
);
4307 ASSERT(isnullstartblock(got
->br_startblock
));
4308 ASSERT(got
->br_state
== XFS_EXT_NORM
);
4311 out_unreserve_blocks
:
4313 xfs_mod_frextents(mp
, extsz
);
4315 xfs_mod_fdblocks(mp
, alen
, false);
4316 out_unreserve_quota
:
4317 if (XFS_IS_QUOTA_ON(mp
))
4318 xfs_trans_unreserve_quota_nblks(NULL
, ip
, (long)alen
, 0, rt
?
4319 XFS_QMOPT_RES_RTBLKS
: XFS_QMOPT_RES_REGBLKS
);
4325 struct xfs_bmalloca
*bma
)
4327 struct xfs_mount
*mp
= bma
->ip
->i_mount
;
4328 int whichfork
= xfs_bmapi_whichfork(bma
->flags
);
4329 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
4330 int tmp_logflags
= 0;
4333 ASSERT(bma
->length
> 0);
4336 * For the wasdelay case, we could also just allocate the stuff asked
4337 * for in this bmap call but that wouldn't be as good.
4340 bma
->length
= (xfs_extlen_t
)bma
->got
.br_blockcount
;
4341 bma
->offset
= bma
->got
.br_startoff
;
4342 if (bma
->idx
!= NULLEXTNUM
&& bma
->idx
) {
4343 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
- 1),
4347 bma
->length
= XFS_FILBLKS_MIN(bma
->length
, MAXEXTLEN
);
4349 bma
->length
= XFS_FILBLKS_MIN(bma
->length
,
4350 bma
->got
.br_startoff
- bma
->offset
);
4354 * Set the data type being allocated. For the data fork, the first data
4355 * in the file is treated differently to all other allocations. For the
4356 * attribute fork, we only need to ensure the allocated range is not on
4359 if (!(bma
->flags
& XFS_BMAPI_METADATA
)) {
4360 bma
->datatype
= XFS_ALLOC_NOBUSY
;
4361 if (whichfork
== XFS_DATA_FORK
) {
4362 if (bma
->offset
== 0)
4363 bma
->datatype
|= XFS_ALLOC_INITIAL_USER_DATA
;
4365 bma
->datatype
|= XFS_ALLOC_USERDATA
;
4367 if (bma
->flags
& XFS_BMAPI_ZERO
)
4368 bma
->datatype
|= XFS_ALLOC_USERDATA_ZERO
;
4371 bma
->minlen
= (bma
->flags
& XFS_BMAPI_CONTIG
) ? bma
->length
: 1;
4374 * Only want to do the alignment at the eof if it is userdata and
4375 * allocation length is larger than a stripe unit.
4377 if (mp
->m_dalign
&& bma
->length
>= mp
->m_dalign
&&
4378 !(bma
->flags
& XFS_BMAPI_METADATA
) && whichfork
== XFS_DATA_FORK
) {
4379 error
= xfs_bmap_isaeof(bma
, whichfork
);
4384 error
= xfs_bmap_alloc(bma
);
4388 if (bma
->dfops
->dop_low
)
4391 bma
->cur
->bc_private
.b
.firstblock
= *bma
->firstblock
;
4392 if (bma
->blkno
== NULLFSBLOCK
)
4394 if ((ifp
->if_flags
& XFS_IFBROOT
) && !bma
->cur
) {
4395 bma
->cur
= xfs_bmbt_init_cursor(mp
, bma
->tp
, bma
->ip
, whichfork
);
4396 bma
->cur
->bc_private
.b
.firstblock
= *bma
->firstblock
;
4397 bma
->cur
->bc_private
.b
.dfops
= bma
->dfops
;
4400 * Bump the number of extents we've allocated
4406 bma
->cur
->bc_private
.b
.flags
=
4407 bma
->wasdel
? XFS_BTCUR_BPRV_WASDEL
: 0;
4409 bma
->got
.br_startoff
= bma
->offset
;
4410 bma
->got
.br_startblock
= bma
->blkno
;
4411 bma
->got
.br_blockcount
= bma
->length
;
4412 bma
->got
.br_state
= XFS_EXT_NORM
;
4415 * A wasdelay extent has been initialized, so shouldn't be flagged
4418 if (!bma
->wasdel
&& (bma
->flags
& XFS_BMAPI_PREALLOC
) &&
4419 xfs_sb_version_hasextflgbit(&mp
->m_sb
))
4420 bma
->got
.br_state
= XFS_EXT_UNWRITTEN
;
4423 error
= xfs_bmap_add_extent_delay_real(bma
, whichfork
);
4425 error
= xfs_bmap_add_extent_hole_real(bma
, whichfork
);
4427 bma
->logflags
|= tmp_logflags
;
4432 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4433 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4434 * the neighbouring ones.
4436 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
), &bma
->got
);
4438 ASSERT(bma
->got
.br_startoff
<= bma
->offset
);
4439 ASSERT(bma
->got
.br_startoff
+ bma
->got
.br_blockcount
>=
4440 bma
->offset
+ bma
->length
);
4441 ASSERT(bma
->got
.br_state
== XFS_EXT_NORM
||
4442 bma
->got
.br_state
== XFS_EXT_UNWRITTEN
);
4447 xfs_bmapi_convert_unwritten(
4448 struct xfs_bmalloca
*bma
,
4449 struct xfs_bmbt_irec
*mval
,
4453 int whichfork
= xfs_bmapi_whichfork(flags
);
4454 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
4455 int tmp_logflags
= 0;
4458 /* check if we need to do unwritten->real conversion */
4459 if (mval
->br_state
== XFS_EXT_UNWRITTEN
&&
4460 (flags
& XFS_BMAPI_PREALLOC
))
4463 /* check if we need to do real->unwritten conversion */
4464 if (mval
->br_state
== XFS_EXT_NORM
&&
4465 (flags
& (XFS_BMAPI_PREALLOC
| XFS_BMAPI_CONVERT
)) !=
4466 (XFS_BMAPI_PREALLOC
| XFS_BMAPI_CONVERT
))
4469 ASSERT(whichfork
!= XFS_COW_FORK
);
4472 * Modify (by adding) the state flag, if writing.
4474 ASSERT(mval
->br_blockcount
<= len
);
4475 if ((ifp
->if_flags
& XFS_IFBROOT
) && !bma
->cur
) {
4476 bma
->cur
= xfs_bmbt_init_cursor(bma
->ip
->i_mount
, bma
->tp
,
4477 bma
->ip
, whichfork
);
4478 bma
->cur
->bc_private
.b
.firstblock
= *bma
->firstblock
;
4479 bma
->cur
->bc_private
.b
.dfops
= bma
->dfops
;
4481 mval
->br_state
= (mval
->br_state
== XFS_EXT_UNWRITTEN
)
4482 ? XFS_EXT_NORM
: XFS_EXT_UNWRITTEN
;
4485 * Before insertion into the bmbt, zero the range being converted
4488 if (flags
& XFS_BMAPI_ZERO
) {
4489 error
= xfs_zero_extent(bma
->ip
, mval
->br_startblock
,
4490 mval
->br_blockcount
);
4495 error
= xfs_bmap_add_extent_unwritten_real(bma
->tp
, bma
->ip
, &bma
->idx
,
4496 &bma
->cur
, mval
, bma
->firstblock
, bma
->dfops
,
4499 * Log the inode core unconditionally in the unwritten extent conversion
4500 * path because the conversion might not have done so (e.g., if the
4501 * extent count hasn't changed). We need to make sure the inode is dirty
4502 * in the transaction for the sake of fsync(), even if nothing has
4503 * changed, because fsync() will not force the log for this transaction
4504 * unless it sees the inode pinned.
4506 bma
->logflags
|= tmp_logflags
| XFS_ILOG_CORE
;
4511 * Update our extent pointer, given that
4512 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4513 * of the neighbouring ones.
4515 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
), &bma
->got
);
4518 * We may have combined previously unwritten space with written space,
4519 * so generate another request.
4521 if (mval
->br_blockcount
< len
)
4527 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4528 * extent state if necessary. Details behaviour is controlled by the flags
4529 * parameter. Only allocates blocks from a single allocation group, to avoid
4532 * The returned value in "firstblock" from the first call in a transaction
4533 * must be remembered and presented to subsequent calls in "firstblock".
4534 * An upper bound for the number of blocks to be allocated is supplied to
4535 * the first call in "total"; if no allocation group has that many free
4536 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4540 struct xfs_trans
*tp
, /* transaction pointer */
4541 struct xfs_inode
*ip
, /* incore inode */
4542 xfs_fileoff_t bno
, /* starting file offs. mapped */
4543 xfs_filblks_t len
, /* length to map in file */
4544 int flags
, /* XFS_BMAPI_... */
4545 xfs_fsblock_t
*firstblock
, /* first allocated block
4546 controls a.g. for allocs */
4547 xfs_extlen_t total
, /* total blocks needed */
4548 struct xfs_bmbt_irec
*mval
, /* output: map values */
4549 int *nmap
, /* i/o: mval size/count */
4550 struct xfs_defer_ops
*dfops
) /* i/o: list extents to free */
4552 struct xfs_mount
*mp
= ip
->i_mount
;
4553 struct xfs_ifork
*ifp
;
4554 struct xfs_bmalloca bma
= { NULL
}; /* args for xfs_bmap_alloc */
4555 xfs_fileoff_t end
; /* end of mapped file region */
4556 int eof
; /* after the end of extents */
4557 int error
; /* error return */
4558 int n
; /* current extent index */
4559 xfs_fileoff_t obno
; /* old block number (offset) */
4560 int whichfork
; /* data or attr fork */
4561 char inhole
; /* current location is hole in file */
4562 char wasdelay
; /* old extent was delayed */
4565 xfs_fileoff_t orig_bno
; /* original block number value */
4566 int orig_flags
; /* original flags arg value */
4567 xfs_filblks_t orig_len
; /* original value of len arg */
4568 struct xfs_bmbt_irec
*orig_mval
; /* original value of mval */
4569 int orig_nmap
; /* original value of *nmap */
4577 whichfork
= xfs_bmapi_whichfork(flags
);
4580 ASSERT(*nmap
<= XFS_BMAP_MAX_NMAP
);
4581 ASSERT(!(flags
& XFS_BMAPI_IGSTATE
));
4584 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_LOCAL
);
4585 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
4586 ASSERT(!(flags
& XFS_BMAPI_REMAP
) || whichfork
== XFS_DATA_FORK
);
4587 ASSERT(!(flags
& XFS_BMAPI_PREALLOC
) || !(flags
& XFS_BMAPI_REMAP
));
4588 ASSERT(!(flags
& XFS_BMAPI_CONVERT
) || !(flags
& XFS_BMAPI_REMAP
));
4589 ASSERT(!(flags
& XFS_BMAPI_PREALLOC
) || whichfork
!= XFS_COW_FORK
);
4590 ASSERT(!(flags
& XFS_BMAPI_CONVERT
) || whichfork
!= XFS_COW_FORK
);
4592 /* zeroing is for currently only for data extents, not metadata */
4593 ASSERT((flags
& (XFS_BMAPI_METADATA
| XFS_BMAPI_ZERO
)) !=
4594 (XFS_BMAPI_METADATA
| XFS_BMAPI_ZERO
));
4596 * we can allocate unwritten extents or pre-zero allocated blocks,
4597 * but it makes no sense to do both at once. This would result in
4598 * zeroing the unwritten extent twice, but it still being an
4599 * unwritten extent....
4601 ASSERT((flags
& (XFS_BMAPI_PREALLOC
| XFS_BMAPI_ZERO
)) !=
4602 (XFS_BMAPI_PREALLOC
| XFS_BMAPI_ZERO
));
4604 if (unlikely(XFS_TEST_ERROR(
4605 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
4606 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
4607 mp
, XFS_ERRTAG_BMAPIFORMAT
, XFS_RANDOM_BMAPIFORMAT
))) {
4608 XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW
, mp
);
4609 return -EFSCORRUPTED
;
4612 if (XFS_FORCED_SHUTDOWN(mp
))
4615 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4617 XFS_STATS_INC(mp
, xs_blk_mapw
);
4619 if (*firstblock
== NULLFSBLOCK
) {
4620 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
)
4621 bma
.minleft
= be16_to_cpu(ifp
->if_broot
->bb_level
) + 1;
4628 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
4629 error
= xfs_iread_extents(tp
, ip
, whichfork
);
4634 xfs_bmap_search_extents(ip
, bno
, whichfork
, &eof
, &bma
.idx
, &bma
.got
,
4645 bma
.firstblock
= firstblock
;
4647 while (bno
< end
&& n
< *nmap
) {
4648 inhole
= eof
|| bma
.got
.br_startoff
> bno
;
4649 wasdelay
= !inhole
&& isnullstartblock(bma
.got
.br_startblock
);
4652 * Make sure we only reflink into a hole.
4654 if (flags
& XFS_BMAPI_REMAP
)
4656 if (flags
& XFS_BMAPI_COWFORK
)
4660 * First, deal with the hole before the allocated space
4661 * that we found, if any.
4663 if (inhole
|| wasdelay
) {
4665 bma
.conv
= !!(flags
& XFS_BMAPI_CONVERT
);
4666 bma
.wasdel
= wasdelay
;
4671 * There's a 32/64 bit type mismatch between the
4672 * allocation length request (which can be 64 bits in
4673 * length) and the bma length request, which is
4674 * xfs_extlen_t and therefore 32 bits. Hence we have to
4675 * check for 32-bit overflows and handle them here.
4677 if (len
> (xfs_filblks_t
)MAXEXTLEN
)
4678 bma
.length
= MAXEXTLEN
;
4683 ASSERT(bma
.length
> 0);
4684 error
= xfs_bmapi_allocate(&bma
);
4687 if (bma
.blkno
== NULLFSBLOCK
)
4691 * If this is a CoW allocation, record the data in
4692 * the refcount btree for orphan recovery.
4694 if (whichfork
== XFS_COW_FORK
) {
4695 error
= xfs_refcount_alloc_cow_extent(mp
, dfops
,
4696 bma
.blkno
, bma
.length
);
4702 /* Deal with the allocated space we found. */
4703 xfs_bmapi_trim_map(mval
, &bma
.got
, &bno
, len
, obno
,
4706 /* Execute unwritten extent conversion if necessary */
4707 error
= xfs_bmapi_convert_unwritten(&bma
, mval
, len
, flags
);
4708 if (error
== -EAGAIN
)
4713 /* update the extent map to return */
4714 xfs_bmapi_update_map(&mval
, &bno
, &len
, obno
, end
, &n
, flags
);
4717 * If we're done, stop now. Stop when we've allocated
4718 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4719 * the transaction may get too big.
4721 if (bno
>= end
|| n
>= *nmap
|| bma
.nallocs
>= *nmap
)
4724 /* Else go on to the next record. */
4726 if (++bma
.idx
< xfs_iext_count(ifp
)) {
4727 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
.idx
),
4735 * Transform from btree to extents, give it cur.
4737 if (xfs_bmap_wants_extents(ip
, whichfork
)) {
4738 int tmp_logflags
= 0;
4741 error
= xfs_bmap_btree_to_extents(tp
, ip
, bma
.cur
,
4742 &tmp_logflags
, whichfork
);
4743 bma
.logflags
|= tmp_logflags
;
4748 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
||
4749 XFS_IFORK_NEXTENTS(ip
, whichfork
) >
4750 XFS_IFORK_MAXEXT(ip
, whichfork
));
4754 * Log everything. Do this after conversion, there's no point in
4755 * logging the extent records if we've converted to btree format.
4757 if ((bma
.logflags
& xfs_ilog_fext(whichfork
)) &&
4758 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
4759 bma
.logflags
&= ~xfs_ilog_fext(whichfork
);
4760 else if ((bma
.logflags
& xfs_ilog_fbroot(whichfork
)) &&
4761 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)
4762 bma
.logflags
&= ~xfs_ilog_fbroot(whichfork
);
4764 * Log whatever the flags say, even if error. Otherwise we might miss
4765 * detecting a case where the data is changed, there's an error,
4766 * and it's not logged so we don't shutdown when we should.
4769 xfs_trans_log_inode(tp
, ip
, bma
.logflags
);
4773 ASSERT(*firstblock
== NULLFSBLOCK
||
4774 XFS_FSB_TO_AGNO(mp
, *firstblock
) ==
4776 bma
.cur
->bc_private
.b
.firstblock
) ||
4778 XFS_FSB_TO_AGNO(mp
, *firstblock
) <
4780 bma
.cur
->bc_private
.b
.firstblock
)));
4781 *firstblock
= bma
.cur
->bc_private
.b
.firstblock
;
4783 xfs_btree_del_cursor(bma
.cur
,
4784 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
4787 xfs_bmap_validate_ret(orig_bno
, orig_len
, orig_flags
, orig_mval
,
4793 * When a delalloc extent is split (e.g., due to a hole punch), the original
4794 * indlen reservation must be shared across the two new extents that are left
4797 * Given the original reservation and the worst case indlen for the two new
4798 * extents (as calculated by xfs_bmap_worst_indlen()), split the original
4799 * reservation fairly across the two new extents. If necessary, steal available
4800 * blocks from a deleted extent to make up a reservation deficiency (e.g., if
4801 * ores == 1). The number of stolen blocks is returned. The availability and
4802 * subsequent accounting of stolen blocks is the responsibility of the caller.
4804 static xfs_filblks_t
4805 xfs_bmap_split_indlen(
4806 xfs_filblks_t ores
, /* original res. */
4807 xfs_filblks_t
*indlen1
, /* ext1 worst indlen */
4808 xfs_filblks_t
*indlen2
, /* ext2 worst indlen */
4809 xfs_filblks_t avail
) /* stealable blocks */
4811 xfs_filblks_t len1
= *indlen1
;
4812 xfs_filblks_t len2
= *indlen2
;
4813 xfs_filblks_t nres
= len1
+ len2
; /* new total res. */
4814 xfs_filblks_t stolen
= 0;
4817 * Steal as many blocks as we can to try and satisfy the worst case
4818 * indlen for both new extents.
4820 while (nres
> ores
&& avail
) {
4827 * The only blocks available are those reserved for the original
4828 * extent and what we can steal from the extent being removed.
4829 * If this still isn't enough to satisfy the combined
4830 * requirements for the two new extents, skim blocks off of each
4831 * of the new reservations until they match what is available.
4833 while (nres
> ores
) {
4853 xfs_bmap_del_extent_delay(
4854 struct xfs_inode
*ip
,
4857 struct xfs_bmbt_irec
*got
,
4858 struct xfs_bmbt_irec
*del
)
4860 struct xfs_mount
*mp
= ip
->i_mount
;
4861 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4862 struct xfs_bmbt_irec
new;
4863 int64_t da_old
, da_new
, da_diff
= 0;
4864 xfs_fileoff_t del_endoff
, got_endoff
;
4865 xfs_filblks_t got_indlen
, new_indlen
, stolen
;
4866 int error
= 0, state
= 0;
4869 XFS_STATS_INC(mp
, xs_del_exlist
);
4871 isrt
= (whichfork
== XFS_DATA_FORK
) && XFS_IS_REALTIME_INODE(ip
);
4872 del_endoff
= del
->br_startoff
+ del
->br_blockcount
;
4873 got_endoff
= got
->br_startoff
+ got
->br_blockcount
;
4874 da_old
= startblockval(got
->br_startblock
);
4878 ASSERT(*idx
<= xfs_iext_count(ifp
));
4879 ASSERT(del
->br_blockcount
> 0);
4880 ASSERT(got
->br_startoff
<= del
->br_startoff
);
4881 ASSERT(got_endoff
>= del_endoff
);
4884 int64_t rtexts
= XFS_FSB_TO_B(mp
, del
->br_blockcount
);
4886 do_div(rtexts
, mp
->m_sb
.sb_rextsize
);
4887 xfs_mod_frextents(mp
, rtexts
);
4891 * Update the inode delalloc counter now and wait to update the
4892 * sb counters as we might have to borrow some blocks for the
4893 * indirect block accounting.
4895 error
= xfs_trans_reserve_quota_nblks(NULL
, ip
,
4896 -((long)del
->br_blockcount
), 0,
4897 isrt
? XFS_QMOPT_RES_RTBLKS
: XFS_QMOPT_RES_REGBLKS
);
4900 ip
->i_delayed_blks
-= del
->br_blockcount
;
4902 if (whichfork
== XFS_COW_FORK
)
4903 state
|= BMAP_COWFORK
;
4905 if (got
->br_startoff
== del
->br_startoff
)
4906 state
|= BMAP_LEFT_CONTIG
;
4907 if (got_endoff
== del_endoff
)
4908 state
|= BMAP_RIGHT_CONTIG
;
4910 switch (state
& (BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
)) {
4911 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
4913 * Matches the whole extent. Delete the entry.
4915 xfs_iext_remove(ip
, *idx
, 1, state
);
4918 case BMAP_LEFT_CONTIG
:
4920 * Deleting the first part of the extent.
4922 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
4923 got
->br_startoff
= del_endoff
;
4924 got
->br_blockcount
-= del
->br_blockcount
;
4925 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
,
4926 got
->br_blockcount
), da_old
);
4927 got
->br_startblock
= nullstartblock((int)da_new
);
4928 xfs_bmbt_set_all(xfs_iext_get_ext(ifp
, *idx
), got
);
4929 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
4931 case BMAP_RIGHT_CONTIG
:
4933 * Deleting the last part of the extent.
4935 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
4936 got
->br_blockcount
= got
->br_blockcount
- del
->br_blockcount
;
4937 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
,
4938 got
->br_blockcount
), da_old
);
4939 got
->br_startblock
= nullstartblock((int)da_new
);
4940 xfs_bmbt_set_all(xfs_iext_get_ext(ifp
, *idx
), got
);
4941 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
4945 * Deleting the middle of the extent.
4947 * Distribute the original indlen reservation across the two new
4948 * extents. Steal blocks from the deleted extent if necessary.
4949 * Stealing blocks simply fudges the fdblocks accounting below.
4950 * Warn if either of the new indlen reservations is zero as this
4951 * can lead to delalloc problems.
4953 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
4955 got
->br_blockcount
= del
->br_startoff
- got
->br_startoff
;
4956 got_indlen
= xfs_bmap_worst_indlen(ip
, got
->br_blockcount
);
4958 new.br_blockcount
= got_endoff
- del_endoff
;
4959 new_indlen
= xfs_bmap_worst_indlen(ip
, new.br_blockcount
);
4961 WARN_ON_ONCE(!got_indlen
|| !new_indlen
);
4962 stolen
= xfs_bmap_split_indlen(da_old
, &got_indlen
, &new_indlen
,
4963 del
->br_blockcount
);
4965 got
->br_startblock
= nullstartblock((int)got_indlen
);
4966 xfs_bmbt_set_all(xfs_iext_get_ext(ifp
, *idx
), got
);
4967 trace_xfs_bmap_post_update(ip
, *idx
, 0, _THIS_IP_
);
4969 new.br_startoff
= del_endoff
;
4970 new.br_state
= got
->br_state
;
4971 new.br_startblock
= nullstartblock((int)new_indlen
);
4974 xfs_iext_insert(ip
, *idx
, 1, &new, state
);
4976 da_new
= got_indlen
+ new_indlen
- stolen
;
4977 del
->br_blockcount
-= stolen
;
4981 ASSERT(da_old
>= da_new
);
4982 da_diff
= da_old
- da_new
;
4984 da_diff
+= del
->br_blockcount
;
4986 xfs_mod_fdblocks(mp
, da_diff
, false);
4991 xfs_bmap_del_extent_cow(
4992 struct xfs_inode
*ip
,
4994 struct xfs_bmbt_irec
*got
,
4995 struct xfs_bmbt_irec
*del
)
4997 struct xfs_mount
*mp
= ip
->i_mount
;
4998 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, XFS_COW_FORK
);
4999 struct xfs_bmbt_irec
new;
5000 xfs_fileoff_t del_endoff
, got_endoff
;
5001 int state
= BMAP_COWFORK
;
5003 XFS_STATS_INC(mp
, xs_del_exlist
);
5005 del_endoff
= del
->br_startoff
+ del
->br_blockcount
;
5006 got_endoff
= got
->br_startoff
+ got
->br_blockcount
;
5009 ASSERT(*idx
<= xfs_iext_count(ifp
));
5010 ASSERT(del
->br_blockcount
> 0);
5011 ASSERT(got
->br_startoff
<= del
->br_startoff
);
5012 ASSERT(got_endoff
>= del_endoff
);
5013 ASSERT(!isnullstartblock(got
->br_startblock
));
5015 if (got
->br_startoff
== del
->br_startoff
)
5016 state
|= BMAP_LEFT_CONTIG
;
5017 if (got_endoff
== del_endoff
)
5018 state
|= BMAP_RIGHT_CONTIG
;
5020 switch (state
& (BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
)) {
5021 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
5023 * Matches the whole extent. Delete the entry.
5025 xfs_iext_remove(ip
, *idx
, 1, state
);
5028 case BMAP_LEFT_CONTIG
:
5030 * Deleting the first part of the extent.
5032 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
5033 got
->br_startoff
= del_endoff
;
5034 got
->br_blockcount
-= del
->br_blockcount
;
5035 got
->br_startblock
= del
->br_startblock
+ del
->br_blockcount
;
5036 xfs_bmbt_set_all(xfs_iext_get_ext(ifp
, *idx
), got
);
5037 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
5039 case BMAP_RIGHT_CONTIG
:
5041 * Deleting the last part of the extent.
5043 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
5044 got
->br_blockcount
-= del
->br_blockcount
;
5045 xfs_bmbt_set_all(xfs_iext_get_ext(ifp
, *idx
), got
);
5046 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
5050 * Deleting the middle of the extent.
5052 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
5053 got
->br_blockcount
= del
->br_startoff
- got
->br_startoff
;
5054 xfs_bmbt_set_all(xfs_iext_get_ext(ifp
, *idx
), got
);
5055 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
5057 new.br_startoff
= del_endoff
;
5058 new.br_blockcount
= got_endoff
- del_endoff
;
5059 new.br_state
= got
->br_state
;
5060 new.br_startblock
= del
->br_startblock
+ del
->br_blockcount
;
5063 xfs_iext_insert(ip
, *idx
, 1, &new, state
);
5069 * Called by xfs_bmapi to update file extent records and the btree
5070 * after removing space (or undoing a delayed allocation).
5072 STATIC
int /* error */
5073 xfs_bmap_del_extent(
5074 xfs_inode_t
*ip
, /* incore inode pointer */
5075 xfs_trans_t
*tp
, /* current transaction pointer */
5076 xfs_extnum_t
*idx
, /* extent number to update/delete */
5077 struct xfs_defer_ops
*dfops
, /* list of extents to be freed */
5078 xfs_btree_cur_t
*cur
, /* if null, not a btree */
5079 xfs_bmbt_irec_t
*del
, /* data to remove from extents */
5080 int *logflagsp
, /* inode logging flags */
5081 int whichfork
, /* data or attr fork */
5082 int bflags
) /* bmapi flags */
5084 xfs_filblks_t da_new
; /* new delay-alloc indirect blocks */
5085 xfs_filblks_t da_old
; /* old delay-alloc indirect blocks */
5086 xfs_fsblock_t del_endblock
=0; /* first block past del */
5087 xfs_fileoff_t del_endoff
; /* first offset past del */
5088 int delay
; /* current block is delayed allocated */
5089 int do_fx
; /* free extent at end of routine */
5090 xfs_bmbt_rec_host_t
*ep
; /* current extent entry pointer */
5091 int error
; /* error return value */
5092 int flags
; /* inode logging flags */
5093 xfs_bmbt_irec_t got
; /* current extent entry */
5094 xfs_fileoff_t got_endoff
; /* first offset past got */
5095 int i
; /* temp state */
5096 xfs_ifork_t
*ifp
; /* inode fork pointer */
5097 xfs_mount_t
*mp
; /* mount structure */
5098 xfs_filblks_t nblks
; /* quota/sb block count */
5099 xfs_bmbt_irec_t
new; /* new record to be inserted */
5101 uint qfield
; /* quota field to update */
5102 xfs_filblks_t temp
; /* for indirect length calculations */
5103 xfs_filblks_t temp2
; /* for indirect length calculations */
5107 XFS_STATS_INC(mp
, xs_del_exlist
);
5109 if (whichfork
== XFS_ATTR_FORK
)
5110 state
|= BMAP_ATTRFORK
;
5111 else if (whichfork
== XFS_COW_FORK
)
5112 state
|= BMAP_COWFORK
;
5114 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5115 ASSERT((*idx
>= 0) && (*idx
< xfs_iext_count(ifp
)));
5116 ASSERT(del
->br_blockcount
> 0);
5117 ep
= xfs_iext_get_ext(ifp
, *idx
);
5118 xfs_bmbt_get_all(ep
, &got
);
5119 ASSERT(got
.br_startoff
<= del
->br_startoff
);
5120 del_endoff
= del
->br_startoff
+ del
->br_blockcount
;
5121 got_endoff
= got
.br_startoff
+ got
.br_blockcount
;
5122 ASSERT(got_endoff
>= del_endoff
);
5123 delay
= isnullstartblock(got
.br_startblock
);
5124 ASSERT(isnullstartblock(del
->br_startblock
) == delay
);
5129 * If deleting a real allocation, must free up the disk space.
5132 flags
= XFS_ILOG_CORE
;
5134 * Realtime allocation. Free it and record di_nblocks update.
5136 if (whichfork
== XFS_DATA_FORK
&& XFS_IS_REALTIME_INODE(ip
)) {
5140 ASSERT(do_mod(del
->br_blockcount
,
5141 mp
->m_sb
.sb_rextsize
) == 0);
5142 ASSERT(do_mod(del
->br_startblock
,
5143 mp
->m_sb
.sb_rextsize
) == 0);
5144 bno
= del
->br_startblock
;
5145 len
= del
->br_blockcount
;
5146 do_div(bno
, mp
->m_sb
.sb_rextsize
);
5147 do_div(len
, mp
->m_sb
.sb_rextsize
);
5148 error
= xfs_rtfree_extent(tp
, bno
, (xfs_extlen_t
)len
);
5152 nblks
= len
* mp
->m_sb
.sb_rextsize
;
5153 qfield
= XFS_TRANS_DQ_RTBCOUNT
;
5156 * Ordinary allocation.
5160 nblks
= del
->br_blockcount
;
5161 qfield
= XFS_TRANS_DQ_BCOUNT
;
5164 * Set up del_endblock and cur for later.
5166 del_endblock
= del
->br_startblock
+ del
->br_blockcount
;
5168 if ((error
= xfs_bmbt_lookup_eq(cur
, got
.br_startoff
,
5169 got
.br_startblock
, got
.br_blockcount
,
5172 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
5174 da_old
= da_new
= 0;
5176 da_old
= startblockval(got
.br_startblock
);
5183 * Set flag value to use in switch statement.
5184 * Left-contig is 2, right-contig is 1.
5186 switch (((got
.br_startoff
== del
->br_startoff
) << 1) |
5187 (got_endoff
== del_endoff
)) {
5190 * Matches the whole extent. Delete the entry.
5192 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
5193 xfs_iext_remove(ip
, *idx
, 1,
5194 whichfork
== XFS_ATTR_FORK
? BMAP_ATTRFORK
: 0);
5199 XFS_IFORK_NEXT_SET(ip
, whichfork
,
5200 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
5201 flags
|= XFS_ILOG_CORE
;
5203 flags
|= xfs_ilog_fext(whichfork
);
5206 if ((error
= xfs_btree_delete(cur
, &i
)))
5208 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
5213 * Deleting the first part of the extent.
5215 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
5216 xfs_bmbt_set_startoff(ep
, del_endoff
);
5217 temp
= got
.br_blockcount
- del
->br_blockcount
;
5218 xfs_bmbt_set_blockcount(ep
, temp
);
5220 temp
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
5222 xfs_bmbt_set_startblock(ep
, nullstartblock((int)temp
));
5223 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
5227 xfs_bmbt_set_startblock(ep
, del_endblock
);
5228 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
5230 flags
|= xfs_ilog_fext(whichfork
);
5233 if ((error
= xfs_bmbt_update(cur
, del_endoff
, del_endblock
,
5234 got
.br_blockcount
- del
->br_blockcount
,
5241 * Deleting the last part of the extent.
5243 temp
= got
.br_blockcount
- del
->br_blockcount
;
5244 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
5245 xfs_bmbt_set_blockcount(ep
, temp
);
5247 temp
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
5249 xfs_bmbt_set_startblock(ep
, nullstartblock((int)temp
));
5250 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
5254 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
5256 flags
|= xfs_ilog_fext(whichfork
);
5259 if ((error
= xfs_bmbt_update(cur
, got
.br_startoff
,
5261 got
.br_blockcount
- del
->br_blockcount
,
5268 * Deleting the middle of the extent.
5270 temp
= del
->br_startoff
- got
.br_startoff
;
5271 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
5272 xfs_bmbt_set_blockcount(ep
, temp
);
5273 new.br_startoff
= del_endoff
;
5274 temp2
= got_endoff
- del_endoff
;
5275 new.br_blockcount
= temp2
;
5276 new.br_state
= got
.br_state
;
5278 new.br_startblock
= del_endblock
;
5279 flags
|= XFS_ILOG_CORE
;
5281 if ((error
= xfs_bmbt_update(cur
,
5283 got
.br_startblock
, temp
,
5286 if ((error
= xfs_btree_increment(cur
, 0, &i
)))
5288 cur
->bc_rec
.b
= new;
5289 error
= xfs_btree_insert(cur
, &i
);
5290 if (error
&& error
!= -ENOSPC
)
5293 * If get no-space back from btree insert,
5294 * it tried a split, and we have a zero
5295 * block reservation.
5296 * Fix up our state and return the error.
5298 if (error
== -ENOSPC
) {
5300 * Reset the cursor, don't trust
5301 * it after any insert operation.
5303 if ((error
= xfs_bmbt_lookup_eq(cur
,
5308 XFS_WANT_CORRUPTED_GOTO(mp
,
5311 * Update the btree record back
5312 * to the original value.
5314 if ((error
= xfs_bmbt_update(cur
,
5321 * Reset the extent record back
5322 * to the original value.
5324 xfs_bmbt_set_blockcount(ep
,
5330 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
5332 flags
|= xfs_ilog_fext(whichfork
);
5333 XFS_IFORK_NEXT_SET(ip
, whichfork
,
5334 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
5336 xfs_filblks_t stolen
;
5337 ASSERT(whichfork
== XFS_DATA_FORK
);
5340 * Distribute the original indlen reservation across the
5341 * two new extents. Steal blocks from the deleted extent
5342 * if necessary. Stealing blocks simply fudges the
5343 * fdblocks accounting in xfs_bunmapi().
5345 temp
= xfs_bmap_worst_indlen(ip
, got
.br_blockcount
);
5346 temp2
= xfs_bmap_worst_indlen(ip
, new.br_blockcount
);
5347 stolen
= xfs_bmap_split_indlen(da_old
, &temp
, &temp2
,
5348 del
->br_blockcount
);
5349 da_new
= temp
+ temp2
- stolen
;
5350 del
->br_blockcount
-= stolen
;
5353 * Set the reservation for each extent. Warn if either
5354 * is zero as this can lead to delalloc problems.
5356 WARN_ON_ONCE(!temp
|| !temp2
);
5357 xfs_bmbt_set_startblock(ep
, nullstartblock((int)temp
));
5358 new.br_startblock
= nullstartblock((int)temp2
);
5360 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
5361 xfs_iext_insert(ip
, *idx
+ 1, 1, &new, state
);
5366 /* remove reverse mapping */
5368 error
= xfs_rmap_unmap_extent(mp
, dfops
, ip
, whichfork
, del
);
5374 * If we need to, add to list of extents to delete.
5376 if (do_fx
&& !(bflags
& XFS_BMAPI_REMAP
)) {
5377 if (xfs_is_reflink_inode(ip
) && whichfork
== XFS_DATA_FORK
) {
5378 error
= xfs_refcount_decrease_extent(mp
, dfops
, del
);
5382 xfs_bmap_add_free(mp
, dfops
, del
->br_startblock
,
5383 del
->br_blockcount
, NULL
);
5387 * Adjust inode # blocks in the file.
5390 ip
->i_d
.di_nblocks
-= nblks
;
5392 * Adjust quota data.
5394 if (qfield
&& !(bflags
& XFS_BMAPI_REMAP
))
5395 xfs_trans_mod_dquot_byino(tp
, ip
, qfield
, (long)-nblks
);
5398 * Account for change in delayed indirect blocks.
5399 * Nothing to do for disk quota accounting here.
5401 ASSERT(da_old
>= da_new
);
5402 if (da_old
> da_new
)
5403 xfs_mod_fdblocks(mp
, (int64_t)(da_old
- da_new
), false);
5410 * Unmap (remove) blocks from a file.
5411 * If nexts is nonzero then the number of extents to remove is limited to
5412 * that value. If not all extents in the block range can be removed then
5417 xfs_trans_t
*tp
, /* transaction pointer */
5418 struct xfs_inode
*ip
, /* incore inode */
5419 xfs_fileoff_t bno
, /* starting offset to unmap */
5420 xfs_filblks_t
*rlen
, /* i/o: amount remaining */
5421 int flags
, /* misc flags */
5422 xfs_extnum_t nexts
, /* number of extents max */
5423 xfs_fsblock_t
*firstblock
, /* first allocated block
5424 controls a.g. for allocs */
5425 struct xfs_defer_ops
*dfops
) /* i/o: deferred updates */
5427 xfs_btree_cur_t
*cur
; /* bmap btree cursor */
5428 xfs_bmbt_irec_t del
; /* extent being deleted */
5429 int eof
; /* is deleting at eof */
5430 xfs_bmbt_rec_host_t
*ep
; /* extent record pointer */
5431 int error
; /* error return value */
5432 xfs_extnum_t extno
; /* extent number in list */
5433 xfs_bmbt_irec_t got
; /* current extent record */
5434 xfs_ifork_t
*ifp
; /* inode fork pointer */
5435 int isrt
; /* freeing in rt area */
5436 xfs_extnum_t lastx
; /* last extent index used */
5437 int logflags
; /* transaction logging flags */
5438 xfs_extlen_t mod
; /* rt extent offset */
5439 xfs_mount_t
*mp
; /* mount structure */
5440 xfs_bmbt_irec_t prev
; /* previous extent record */
5441 xfs_fileoff_t start
; /* first file offset deleted */
5442 int tmp_logflags
; /* partial logging flags */
5443 int wasdel
; /* was a delayed alloc extent */
5444 int whichfork
; /* data or attribute fork */
5446 xfs_filblks_t len
= *rlen
; /* length to unmap in file */
5448 trace_xfs_bunmap(ip
, bno
, len
, flags
, _RET_IP_
);
5450 whichfork
= xfs_bmapi_whichfork(flags
);
5451 ASSERT(whichfork
!= XFS_COW_FORK
);
5452 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5454 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
5455 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)) {
5456 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW
,
5458 return -EFSCORRUPTED
;
5461 if (XFS_FORCED_SHUTDOWN(mp
))
5464 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
5468 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
5469 (error
= xfs_iread_extents(tp
, ip
, whichfork
)))
5471 if (xfs_iext_count(ifp
) == 0) {
5475 XFS_STATS_INC(mp
, xs_blk_unmap
);
5476 isrt
= (whichfork
== XFS_DATA_FORK
) && XFS_IS_REALTIME_INODE(ip
);
5478 bno
= start
+ len
- 1;
5479 ep
= xfs_bmap_search_extents(ip
, bno
, whichfork
, &eof
, &lastx
, &got
,
5483 * Check to see if the given block number is past the end of the
5484 * file, back up to the last block if so...
5487 ep
= xfs_iext_get_ext(ifp
, --lastx
);
5488 xfs_bmbt_get_all(ep
, &got
);
5489 bno
= got
.br_startoff
+ got
.br_blockcount
- 1;
5492 if (ifp
->if_flags
& XFS_IFBROOT
) {
5493 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
);
5494 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
5495 cur
->bc_private
.b
.firstblock
= *firstblock
;
5496 cur
->bc_private
.b
.dfops
= dfops
;
5497 cur
->bc_private
.b
.flags
= 0;
5503 * Synchronize by locking the bitmap inode.
5505 xfs_ilock(mp
->m_rbmip
, XFS_ILOCK_EXCL
|XFS_ILOCK_RTBITMAP
);
5506 xfs_trans_ijoin(tp
, mp
->m_rbmip
, XFS_ILOCK_EXCL
);
5507 xfs_ilock(mp
->m_rsumip
, XFS_ILOCK_EXCL
|XFS_ILOCK_RTSUM
);
5508 xfs_trans_ijoin(tp
, mp
->m_rsumip
, XFS_ILOCK_EXCL
);
5512 while (bno
!= (xfs_fileoff_t
)-1 && bno
>= start
&& lastx
>= 0 &&
5513 (nexts
== 0 || extno
< nexts
)) {
5515 * Is the found extent after a hole in which bno lives?
5516 * Just back up to the previous extent, if so.
5518 if (got
.br_startoff
> bno
) {
5521 ep
= xfs_iext_get_ext(ifp
, lastx
);
5522 xfs_bmbt_get_all(ep
, &got
);
5525 * Is the last block of this extent before the range
5526 * we're supposed to delete? If so, we're done.
5528 bno
= XFS_FILEOFF_MIN(bno
,
5529 got
.br_startoff
+ got
.br_blockcount
- 1);
5533 * Then deal with the (possibly delayed) allocated space
5538 wasdel
= isnullstartblock(del
.br_startblock
);
5539 if (got
.br_startoff
< start
) {
5540 del
.br_startoff
= start
;
5541 del
.br_blockcount
-= start
- got
.br_startoff
;
5543 del
.br_startblock
+= start
- got
.br_startoff
;
5545 if (del
.br_startoff
+ del
.br_blockcount
> bno
+ 1)
5546 del
.br_blockcount
= bno
+ 1 - del
.br_startoff
;
5547 sum
= del
.br_startblock
+ del
.br_blockcount
;
5549 (mod
= do_mod(sum
, mp
->m_sb
.sb_rextsize
))) {
5551 * Realtime extent not lined up at the end.
5552 * The extent could have been split into written
5553 * and unwritten pieces, or we could just be
5554 * unmapping part of it. But we can't really
5555 * get rid of part of a realtime extent.
5557 if (del
.br_state
== XFS_EXT_UNWRITTEN
||
5558 !xfs_sb_version_hasextflgbit(&mp
->m_sb
)) {
5560 * This piece is unwritten, or we're not
5561 * using unwritten extents. Skip over it.
5564 bno
-= mod
> del
.br_blockcount
?
5565 del
.br_blockcount
: mod
;
5566 if (bno
< got
.br_startoff
) {
5568 xfs_bmbt_get_all(xfs_iext_get_ext(
5574 * It's written, turn it unwritten.
5575 * This is better than zeroing it.
5577 ASSERT(del
.br_state
== XFS_EXT_NORM
);
5578 ASSERT(tp
->t_blk_res
> 0);
5580 * If this spans a realtime extent boundary,
5581 * chop it back to the start of the one we end at.
5583 if (del
.br_blockcount
> mod
) {
5584 del
.br_startoff
+= del
.br_blockcount
- mod
;
5585 del
.br_startblock
+= del
.br_blockcount
- mod
;
5586 del
.br_blockcount
= mod
;
5588 del
.br_state
= XFS_EXT_UNWRITTEN
;
5589 error
= xfs_bmap_add_extent_unwritten_real(tp
, ip
,
5590 &lastx
, &cur
, &del
, firstblock
, dfops
,
5596 if (isrt
&& (mod
= do_mod(del
.br_startblock
, mp
->m_sb
.sb_rextsize
))) {
5598 * Realtime extent is lined up at the end but not
5599 * at the front. We'll get rid of full extents if
5602 mod
= mp
->m_sb
.sb_rextsize
- mod
;
5603 if (del
.br_blockcount
> mod
) {
5604 del
.br_blockcount
-= mod
;
5605 del
.br_startoff
+= mod
;
5606 del
.br_startblock
+= mod
;
5607 } else if ((del
.br_startoff
== start
&&
5608 (del
.br_state
== XFS_EXT_UNWRITTEN
||
5609 tp
->t_blk_res
== 0)) ||
5610 !xfs_sb_version_hasextflgbit(&mp
->m_sb
)) {
5612 * Can't make it unwritten. There isn't
5613 * a full extent here so just skip it.
5615 ASSERT(bno
>= del
.br_blockcount
);
5616 bno
-= del
.br_blockcount
;
5617 if (got
.br_startoff
> bno
) {
5619 ep
= xfs_iext_get_ext(ifp
,
5621 xfs_bmbt_get_all(ep
, &got
);
5625 } else if (del
.br_state
== XFS_EXT_UNWRITTEN
) {
5627 * This one is already unwritten.
5628 * It must have a written left neighbor.
5629 * Unwrite the killed part of that one and
5633 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
,
5635 ASSERT(prev
.br_state
== XFS_EXT_NORM
);
5636 ASSERT(!isnullstartblock(prev
.br_startblock
));
5637 ASSERT(del
.br_startblock
==
5638 prev
.br_startblock
+ prev
.br_blockcount
);
5639 if (prev
.br_startoff
< start
) {
5640 mod
= start
- prev
.br_startoff
;
5641 prev
.br_blockcount
-= mod
;
5642 prev
.br_startblock
+= mod
;
5643 prev
.br_startoff
= start
;
5645 prev
.br_state
= XFS_EXT_UNWRITTEN
;
5647 error
= xfs_bmap_add_extent_unwritten_real(tp
,
5648 ip
, &lastx
, &cur
, &prev
,
5649 firstblock
, dfops
, &logflags
);
5654 ASSERT(del
.br_state
== XFS_EXT_NORM
);
5655 del
.br_state
= XFS_EXT_UNWRITTEN
;
5656 error
= xfs_bmap_add_extent_unwritten_real(tp
,
5657 ip
, &lastx
, &cur
, &del
,
5658 firstblock
, dfops
, &logflags
);
5666 * If it's the case where the directory code is running
5667 * with no block reservation, and the deleted block is in
5668 * the middle of its extent, and the resulting insert
5669 * of an extent would cause transformation to btree format,
5670 * then reject it. The calling code will then swap
5671 * blocks around instead.
5672 * We have to do this now, rather than waiting for the
5673 * conversion to btree format, since the transaction
5676 if (!wasdel
&& tp
->t_blk_res
== 0 &&
5677 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
&&
5678 XFS_IFORK_NEXTENTS(ip
, whichfork
) >= /* Note the >= */
5679 XFS_IFORK_MAXEXT(ip
, whichfork
) &&
5680 del
.br_startoff
> got
.br_startoff
&&
5681 del
.br_startoff
+ del
.br_blockcount
<
5682 got
.br_startoff
+ got
.br_blockcount
) {
5688 * Unreserve quota and update realtime free space, if
5689 * appropriate. If delayed allocation, update the inode delalloc
5690 * counter now and wait to update the sb counters as
5691 * xfs_bmap_del_extent() might need to borrow some blocks.
5694 ASSERT(startblockval(del
.br_startblock
) > 0);
5696 xfs_filblks_t rtexts
;
5698 rtexts
= XFS_FSB_TO_B(mp
, del
.br_blockcount
);
5699 do_div(rtexts
, mp
->m_sb
.sb_rextsize
);
5700 xfs_mod_frextents(mp
, (int64_t)rtexts
);
5701 (void)xfs_trans_reserve_quota_nblks(NULL
,
5702 ip
, -((long)del
.br_blockcount
), 0,
5703 XFS_QMOPT_RES_RTBLKS
);
5705 (void)xfs_trans_reserve_quota_nblks(NULL
,
5706 ip
, -((long)del
.br_blockcount
), 0,
5707 XFS_QMOPT_RES_REGBLKS
);
5709 ip
->i_delayed_blks
-= del
.br_blockcount
;
5711 cur
->bc_private
.b
.flags
|=
5712 XFS_BTCUR_BPRV_WASDEL
;
5714 cur
->bc_private
.b
.flags
&= ~XFS_BTCUR_BPRV_WASDEL
;
5716 error
= xfs_bmap_del_extent(ip
, tp
, &lastx
, dfops
, cur
, &del
,
5717 &tmp_logflags
, whichfork
, flags
);
5718 logflags
|= tmp_logflags
;
5722 if (!isrt
&& wasdel
)
5723 xfs_mod_fdblocks(mp
, (int64_t)del
.br_blockcount
, false);
5725 bno
= del
.br_startoff
- 1;
5728 * If not done go on to the next (previous) record.
5730 if (bno
!= (xfs_fileoff_t
)-1 && bno
>= start
) {
5732 ep
= xfs_iext_get_ext(ifp
, lastx
);
5733 if (xfs_bmbt_get_startoff(ep
) > bno
) {
5735 ep
= xfs_iext_get_ext(ifp
,
5738 xfs_bmbt_get_all(ep
, &got
);
5743 if (bno
== (xfs_fileoff_t
)-1 || bno
< start
|| lastx
< 0)
5746 *rlen
= bno
- start
+ 1;
5749 * Convert to a btree if necessary.
5751 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
5752 ASSERT(cur
== NULL
);
5753 error
= xfs_bmap_extents_to_btree(tp
, ip
, firstblock
, dfops
,
5754 &cur
, 0, &tmp_logflags
, whichfork
);
5755 logflags
|= tmp_logflags
;
5760 * transform from btree to extents, give it cur
5762 else if (xfs_bmap_wants_extents(ip
, whichfork
)) {
5763 ASSERT(cur
!= NULL
);
5764 error
= xfs_bmap_btree_to_extents(tp
, ip
, cur
, &tmp_logflags
,
5766 logflags
|= tmp_logflags
;
5771 * transform from extents to local?
5776 * Log everything. Do this after conversion, there's no point in
5777 * logging the extent records if we've converted to btree format.
5779 if ((logflags
& xfs_ilog_fext(whichfork
)) &&
5780 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
5781 logflags
&= ~xfs_ilog_fext(whichfork
);
5782 else if ((logflags
& xfs_ilog_fbroot(whichfork
)) &&
5783 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)
5784 logflags
&= ~xfs_ilog_fbroot(whichfork
);
5786 * Log inode even in the error case, if the transaction
5787 * is dirty we'll need to shut down the filesystem.
5790 xfs_trans_log_inode(tp
, ip
, logflags
);
5793 *firstblock
= cur
->bc_private
.b
.firstblock
;
5794 cur
->bc_private
.b
.allocated
= 0;
5796 xfs_btree_del_cursor(cur
,
5797 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
5802 /* Unmap a range of a file. */
5806 struct xfs_inode
*ip
,
5811 xfs_fsblock_t
*firstblock
,
5812 struct xfs_defer_ops
*dfops
,
5817 error
= __xfs_bunmapi(tp
, ip
, bno
, &len
, flags
, nexts
, firstblock
,
5824 * Determine whether an extent shift can be accomplished by a merge with the
5825 * extent that precedes the target hole of the shift.
5829 struct xfs_bmbt_irec
*left
, /* preceding extent */
5830 struct xfs_bmbt_irec
*got
, /* current extent to shift */
5831 xfs_fileoff_t shift
) /* shift fsb */
5833 xfs_fileoff_t startoff
;
5835 startoff
= got
->br_startoff
- shift
;
5838 * The extent, once shifted, must be adjacent in-file and on-disk with
5839 * the preceding extent.
5841 if ((left
->br_startoff
+ left
->br_blockcount
!= startoff
) ||
5842 (left
->br_startblock
+ left
->br_blockcount
!= got
->br_startblock
) ||
5843 (left
->br_state
!= got
->br_state
) ||
5844 (left
->br_blockcount
+ got
->br_blockcount
> MAXEXTLEN
))
5851 * A bmap extent shift adjusts the file offset of an extent to fill a preceding
5852 * hole in the file. If an extent shift would result in the extent being fully
5853 * adjacent to the extent that currently precedes the hole, we can merge with
5854 * the preceding extent rather than do the shift.
5856 * This function assumes the caller has verified a shift-by-merge is possible
5857 * with the provided extents via xfs_bmse_can_merge().
5861 struct xfs_inode
*ip
,
5863 xfs_fileoff_t shift
, /* shift fsb */
5864 int current_ext
, /* idx of gotp */
5865 struct xfs_bmbt_rec_host
*gotp
, /* extent to shift */
5866 struct xfs_bmbt_rec_host
*leftp
, /* preceding extent */
5867 struct xfs_btree_cur
*cur
,
5868 int *logflags
) /* output */
5870 struct xfs_bmbt_irec got
;
5871 struct xfs_bmbt_irec left
;
5872 xfs_filblks_t blockcount
;
5874 struct xfs_mount
*mp
= ip
->i_mount
;
5876 xfs_bmbt_get_all(gotp
, &got
);
5877 xfs_bmbt_get_all(leftp
, &left
);
5878 blockcount
= left
.br_blockcount
+ got
.br_blockcount
;
5880 ASSERT(xfs_isilocked(ip
, XFS_IOLOCK_EXCL
));
5881 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
5882 ASSERT(xfs_bmse_can_merge(&left
, &got
, shift
));
5885 * Merge the in-core extents. Note that the host record pointers and
5886 * current_ext index are invalid once the extent has been removed via
5887 * xfs_iext_remove().
5889 xfs_bmbt_set_blockcount(leftp
, blockcount
);
5890 xfs_iext_remove(ip
, current_ext
, 1, 0);
5893 * Update the on-disk extent count, the btree if necessary and log the
5896 XFS_IFORK_NEXT_SET(ip
, whichfork
,
5897 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
5898 *logflags
|= XFS_ILOG_CORE
;
5900 *logflags
|= XFS_ILOG_DEXT
;
5904 /* lookup and remove the extent to merge */
5905 error
= xfs_bmbt_lookup_eq(cur
, got
.br_startoff
, got
.br_startblock
,
5906 got
.br_blockcount
, &i
);
5909 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
5911 error
= xfs_btree_delete(cur
, &i
);
5914 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
5916 /* lookup and update size of the previous extent */
5917 error
= xfs_bmbt_lookup_eq(cur
, left
.br_startoff
, left
.br_startblock
,
5918 left
.br_blockcount
, &i
);
5921 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
5923 left
.br_blockcount
= blockcount
;
5925 return xfs_bmbt_update(cur
, left
.br_startoff
, left
.br_startblock
,
5926 left
.br_blockcount
, left
.br_state
);
5930 * Shift a single extent.
5934 struct xfs_inode
*ip
,
5936 xfs_fileoff_t offset_shift_fsb
,
5938 struct xfs_bmbt_rec_host
*gotp
,
5939 struct xfs_btree_cur
*cur
,
5941 enum shift_direction direction
,
5942 struct xfs_defer_ops
*dfops
)
5944 struct xfs_ifork
*ifp
;
5945 struct xfs_mount
*mp
;
5946 xfs_fileoff_t startoff
;
5947 struct xfs_bmbt_rec_host
*adj_irecp
;
5948 struct xfs_bmbt_irec got
;
5949 struct xfs_bmbt_irec adj_irec
;
5955 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5956 total_extents
= xfs_iext_count(ifp
);
5958 xfs_bmbt_get_all(gotp
, &got
);
5960 /* delalloc extents should be prevented by caller */
5961 XFS_WANT_CORRUPTED_RETURN(mp
, !isnullstartblock(got
.br_startblock
));
5963 if (direction
== SHIFT_LEFT
) {
5964 startoff
= got
.br_startoff
- offset_shift_fsb
;
5967 * Check for merge if we've got an extent to the left,
5968 * otherwise make sure there's enough room at the start
5969 * of the file for the shift.
5971 if (!*current_ext
) {
5972 if (got
.br_startoff
< offset_shift_fsb
)
5974 goto update_current_ext
;
5977 * grab the left extent and check for a large
5980 adj_irecp
= xfs_iext_get_ext(ifp
, *current_ext
- 1);
5981 xfs_bmbt_get_all(adj_irecp
, &adj_irec
);
5984 adj_irec
.br_startoff
+ adj_irec
.br_blockcount
)
5987 /* check whether to merge the extent or shift it down */
5988 if (xfs_bmse_can_merge(&adj_irec
, &got
,
5989 offset_shift_fsb
)) {
5990 error
= xfs_bmse_merge(ip
, whichfork
, offset_shift_fsb
,
5991 *current_ext
, gotp
, adj_irecp
,
5999 startoff
= got
.br_startoff
+ offset_shift_fsb
;
6000 /* nothing to move if this is the last extent */
6001 if (*current_ext
>= (total_extents
- 1))
6002 goto update_current_ext
;
6004 * If this is not the last extent in the file, make sure there
6005 * is enough room between current extent and next extent for
6006 * accommodating the shift.
6008 adj_irecp
= xfs_iext_get_ext(ifp
, *current_ext
+ 1);
6009 xfs_bmbt_get_all(adj_irecp
, &adj_irec
);
6010 if (startoff
+ got
.br_blockcount
> adj_irec
.br_startoff
)
6013 * Unlike a left shift (which involves a hole punch),
6014 * a right shift does not modify extent neighbors
6015 * in any way. We should never find mergeable extents
6016 * in this scenario. Check anyways and warn if we
6017 * encounter two extents that could be one.
6019 if (xfs_bmse_can_merge(&got
, &adj_irec
, offset_shift_fsb
))
6023 * Increment the extent index for the next iteration, update the start
6024 * offset of the in-core extent and update the btree if applicable.
6027 if (direction
== SHIFT_LEFT
)
6031 xfs_bmbt_set_startoff(gotp
, startoff
);
6032 *logflags
|= XFS_ILOG_CORE
;
6035 *logflags
|= XFS_ILOG_DEXT
;
6039 error
= xfs_bmbt_lookup_eq(cur
, got
.br_startoff
, got
.br_startblock
,
6040 got
.br_blockcount
, &i
);
6043 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
6045 got
.br_startoff
= startoff
;
6046 error
= xfs_bmbt_update(cur
, got
.br_startoff
, got
.br_startblock
,
6047 got
.br_blockcount
, got
.br_state
);
6052 /* update reverse mapping */
6053 error
= xfs_rmap_unmap_extent(mp
, dfops
, ip
, whichfork
, &adj_irec
);
6056 adj_irec
.br_startoff
= startoff
;
6057 return xfs_rmap_map_extent(mp
, dfops
, ip
, whichfork
, &adj_irec
);
6061 * Shift extent records to the left/right to cover/create a hole.
6063 * The maximum number of extents to be shifted in a single operation is
6064 * @num_exts. @stop_fsb specifies the file offset at which to stop shift and the
6065 * file offset where we've left off is returned in @next_fsb. @offset_shift_fsb
6066 * is the length by which each extent is shifted. If there is no hole to shift
6067 * the extents into, this will be considered invalid operation and we abort
6071 xfs_bmap_shift_extents(
6072 struct xfs_trans
*tp
,
6073 struct xfs_inode
*ip
,
6074 xfs_fileoff_t
*next_fsb
,
6075 xfs_fileoff_t offset_shift_fsb
,
6077 xfs_fileoff_t stop_fsb
,
6078 xfs_fsblock_t
*firstblock
,
6079 struct xfs_defer_ops
*dfops
,
6080 enum shift_direction direction
,
6083 struct xfs_btree_cur
*cur
= NULL
;
6084 struct xfs_bmbt_rec_host
*gotp
;
6085 struct xfs_bmbt_irec got
;
6086 struct xfs_mount
*mp
= ip
->i_mount
;
6087 struct xfs_ifork
*ifp
;
6088 xfs_extnum_t nexts
= 0;
6089 xfs_extnum_t current_ext
;
6090 xfs_extnum_t total_extents
;
6091 xfs_extnum_t stop_extent
;
6093 int whichfork
= XFS_DATA_FORK
;
6096 if (unlikely(XFS_TEST_ERROR(
6097 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
6098 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
6099 mp
, XFS_ERRTAG_BMAPIFORMAT
, XFS_RANDOM_BMAPIFORMAT
))) {
6100 XFS_ERROR_REPORT("xfs_bmap_shift_extents",
6101 XFS_ERRLEVEL_LOW
, mp
);
6102 return -EFSCORRUPTED
;
6105 if (XFS_FORCED_SHUTDOWN(mp
))
6108 ASSERT(xfs_isilocked(ip
, XFS_IOLOCK_EXCL
));
6109 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
6110 ASSERT(direction
== SHIFT_LEFT
|| direction
== SHIFT_RIGHT
);
6111 ASSERT(*next_fsb
!= NULLFSBLOCK
|| direction
== SHIFT_RIGHT
);
6113 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
6114 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
6115 /* Read in all the extents */
6116 error
= xfs_iread_extents(tp
, ip
, whichfork
);
6121 if (ifp
->if_flags
& XFS_IFBROOT
) {
6122 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
6123 cur
->bc_private
.b
.firstblock
= *firstblock
;
6124 cur
->bc_private
.b
.dfops
= dfops
;
6125 cur
->bc_private
.b
.flags
= 0;
6129 * There may be delalloc extents in the data fork before the range we
6130 * are collapsing out, so we cannot use the count of real extents here.
6131 * Instead we have to calculate it from the incore fork.
6133 total_extents
= xfs_iext_count(ifp
);
6134 if (total_extents
== 0) {
6140 * In case of first right shift, we need to initialize next_fsb
6142 if (*next_fsb
== NULLFSBLOCK
) {
6143 gotp
= xfs_iext_get_ext(ifp
, total_extents
- 1);
6144 xfs_bmbt_get_all(gotp
, &got
);
6145 *next_fsb
= got
.br_startoff
;
6146 if (stop_fsb
> *next_fsb
) {
6152 /* Lookup the extent index at which we have to stop */
6153 if (direction
== SHIFT_RIGHT
) {
6154 gotp
= xfs_iext_bno_to_ext(ifp
, stop_fsb
, &stop_extent
);
6155 /* Make stop_extent exclusive of shift range */
6158 stop_extent
= total_extents
;
6161 * Look up the extent index for the fsb where we start shifting. We can
6162 * henceforth iterate with current_ext as extent list changes are locked
6165 * gotp can be null in 2 cases: 1) if there are no extents or 2)
6166 * *next_fsb lies in a hole beyond which there are no extents. Either
6169 gotp
= xfs_iext_bno_to_ext(ifp
, *next_fsb
, ¤t_ext
);
6175 /* some sanity checking before we finally start shifting extents */
6176 if ((direction
== SHIFT_LEFT
&& current_ext
>= stop_extent
) ||
6177 (direction
== SHIFT_RIGHT
&& current_ext
<= stop_extent
)) {
6182 while (nexts
++ < num_exts
) {
6183 error
= xfs_bmse_shift_one(ip
, whichfork
, offset_shift_fsb
,
6184 ¤t_ext
, gotp
, cur
, &logflags
,
6189 * If there was an extent merge during the shift, the extent
6190 * count can change. Update the total and grade the next record.
6192 if (direction
== SHIFT_LEFT
) {
6193 total_extents
= xfs_iext_count(ifp
);
6194 stop_extent
= total_extents
;
6197 if (current_ext
== stop_extent
) {
6199 *next_fsb
= NULLFSBLOCK
;
6202 gotp
= xfs_iext_get_ext(ifp
, current_ext
);
6206 xfs_bmbt_get_all(gotp
, &got
);
6207 *next_fsb
= got
.br_startoff
;
6212 xfs_btree_del_cursor(cur
,
6213 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
6216 xfs_trans_log_inode(tp
, ip
, logflags
);
6222 * Splits an extent into two extents at split_fsb block such that it is
6223 * the first block of the current_ext. @current_ext is a target extent
6224 * to be split. @split_fsb is a block where the extents is split.
6225 * If split_fsb lies in a hole or the first block of extents, just return 0.
6228 xfs_bmap_split_extent_at(
6229 struct xfs_trans
*tp
,
6230 struct xfs_inode
*ip
,
6231 xfs_fileoff_t split_fsb
,
6232 xfs_fsblock_t
*firstfsb
,
6233 struct xfs_defer_ops
*dfops
)
6235 int whichfork
= XFS_DATA_FORK
;
6236 struct xfs_btree_cur
*cur
= NULL
;
6237 struct xfs_bmbt_rec_host
*gotp
;
6238 struct xfs_bmbt_irec got
;
6239 struct xfs_bmbt_irec
new; /* split extent */
6240 struct xfs_mount
*mp
= ip
->i_mount
;
6241 struct xfs_ifork
*ifp
;
6242 xfs_fsblock_t gotblkcnt
; /* new block count for got */
6243 xfs_extnum_t current_ext
;
6248 if (unlikely(XFS_TEST_ERROR(
6249 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
6250 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
6251 mp
, XFS_ERRTAG_BMAPIFORMAT
, XFS_RANDOM_BMAPIFORMAT
))) {
6252 XFS_ERROR_REPORT("xfs_bmap_split_extent_at",
6253 XFS_ERRLEVEL_LOW
, mp
);
6254 return -EFSCORRUPTED
;
6257 if (XFS_FORCED_SHUTDOWN(mp
))
6260 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
6261 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
6262 /* Read in all the extents */
6263 error
= xfs_iread_extents(tp
, ip
, whichfork
);
6269 * gotp can be null in 2 cases: 1) if there are no extents
6270 * or 2) split_fsb lies in a hole beyond which there are
6271 * no extents. Either way, we are done.
6273 gotp
= xfs_iext_bno_to_ext(ifp
, split_fsb
, ¤t_ext
);
6277 xfs_bmbt_get_all(gotp
, &got
);
6280 * Check split_fsb lies in a hole or the start boundary offset
6283 if (got
.br_startoff
>= split_fsb
)
6286 gotblkcnt
= split_fsb
- got
.br_startoff
;
6287 new.br_startoff
= split_fsb
;
6288 new.br_startblock
= got
.br_startblock
+ gotblkcnt
;
6289 new.br_blockcount
= got
.br_blockcount
- gotblkcnt
;
6290 new.br_state
= got
.br_state
;
6292 if (ifp
->if_flags
& XFS_IFBROOT
) {
6293 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
6294 cur
->bc_private
.b
.firstblock
= *firstfsb
;
6295 cur
->bc_private
.b
.dfops
= dfops
;
6296 cur
->bc_private
.b
.flags
= 0;
6297 error
= xfs_bmbt_lookup_eq(cur
, got
.br_startoff
,
6303 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, del_cursor
);
6306 xfs_bmbt_set_blockcount(gotp
, gotblkcnt
);
6307 got
.br_blockcount
= gotblkcnt
;
6309 logflags
= XFS_ILOG_CORE
;
6311 error
= xfs_bmbt_update(cur
, got
.br_startoff
,
6318 logflags
|= XFS_ILOG_DEXT
;
6320 /* Add new extent */
6322 xfs_iext_insert(ip
, current_ext
, 1, &new, 0);
6323 XFS_IFORK_NEXT_SET(ip
, whichfork
,
6324 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
6327 error
= xfs_bmbt_lookup_eq(cur
, new.br_startoff
,
6328 new.br_startblock
, new.br_blockcount
,
6332 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, del_cursor
);
6333 cur
->bc_rec
.b
.br_state
= new.br_state
;
6335 error
= xfs_btree_insert(cur
, &i
);
6338 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, del_cursor
);
6342 * Convert to a btree if necessary.
6344 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
6345 int tmp_logflags
; /* partial log flag return val */
6347 ASSERT(cur
== NULL
);
6348 error
= xfs_bmap_extents_to_btree(tp
, ip
, firstfsb
, dfops
,
6349 &cur
, 0, &tmp_logflags
, whichfork
);
6350 logflags
|= tmp_logflags
;
6355 cur
->bc_private
.b
.allocated
= 0;
6356 xfs_btree_del_cursor(cur
,
6357 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
6361 xfs_trans_log_inode(tp
, ip
, logflags
);
6366 xfs_bmap_split_extent(
6367 struct xfs_inode
*ip
,
6368 xfs_fileoff_t split_fsb
)
6370 struct xfs_mount
*mp
= ip
->i_mount
;
6371 struct xfs_trans
*tp
;
6372 struct xfs_defer_ops dfops
;
6373 xfs_fsblock_t firstfsb
;
6376 error
= xfs_trans_alloc(mp
, &M_RES(mp
)->tr_write
,
6377 XFS_DIOSTRAT_SPACE_RES(mp
, 0), 0, 0, &tp
);
6381 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
6382 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
);
6384 xfs_defer_init(&dfops
, &firstfsb
);
6386 error
= xfs_bmap_split_extent_at(tp
, ip
, split_fsb
,
6391 error
= xfs_defer_finish(&tp
, &dfops
, NULL
);
6395 return xfs_trans_commit(tp
);
6398 xfs_defer_cancel(&dfops
);
6399 xfs_trans_cancel(tp
);
6403 /* Deferred mapping is only for real extents in the data fork. */
6405 xfs_bmap_is_update_needed(
6406 struct xfs_bmbt_irec
*bmap
)
6408 return bmap
->br_startblock
!= HOLESTARTBLOCK
&&
6409 bmap
->br_startblock
!= DELAYSTARTBLOCK
;
6412 /* Record a bmap intent. */
6415 struct xfs_mount
*mp
,
6416 struct xfs_defer_ops
*dfops
,
6417 enum xfs_bmap_intent_type type
,
6418 struct xfs_inode
*ip
,
6420 struct xfs_bmbt_irec
*bmap
)
6423 struct xfs_bmap_intent
*bi
;
6425 trace_xfs_bmap_defer(mp
,
6426 XFS_FSB_TO_AGNO(mp
, bmap
->br_startblock
),
6428 XFS_FSB_TO_AGBNO(mp
, bmap
->br_startblock
),
6429 ip
->i_ino
, whichfork
,
6431 bmap
->br_blockcount
,
6434 bi
= kmem_alloc(sizeof(struct xfs_bmap_intent
), KM_SLEEP
| KM_NOFS
);
6435 INIT_LIST_HEAD(&bi
->bi_list
);
6438 bi
->bi_whichfork
= whichfork
;
6439 bi
->bi_bmap
= *bmap
;
6441 error
= xfs_defer_join(dfops
, bi
->bi_owner
);
6447 xfs_defer_add(dfops
, XFS_DEFER_OPS_TYPE_BMAP
, &bi
->bi_list
);
6451 /* Map an extent into a file. */
6453 xfs_bmap_map_extent(
6454 struct xfs_mount
*mp
,
6455 struct xfs_defer_ops
*dfops
,
6456 struct xfs_inode
*ip
,
6457 struct xfs_bmbt_irec
*PREV
)
6459 if (!xfs_bmap_is_update_needed(PREV
))
6462 return __xfs_bmap_add(mp
, dfops
, XFS_BMAP_MAP
, ip
,
6463 XFS_DATA_FORK
, PREV
);
6466 /* Unmap an extent out of a file. */
6468 xfs_bmap_unmap_extent(
6469 struct xfs_mount
*mp
,
6470 struct xfs_defer_ops
*dfops
,
6471 struct xfs_inode
*ip
,
6472 struct xfs_bmbt_irec
*PREV
)
6474 if (!xfs_bmap_is_update_needed(PREV
))
6477 return __xfs_bmap_add(mp
, dfops
, XFS_BMAP_UNMAP
, ip
,
6478 XFS_DATA_FORK
, PREV
);
6482 * Process one of the deferred bmap operations. We pass back the
6483 * btree cursor to maintain our lock on the bmapbt between calls.
6486 xfs_bmap_finish_one(
6487 struct xfs_trans
*tp
,
6488 struct xfs_defer_ops
*dfops
,
6489 struct xfs_inode
*ip
,
6490 enum xfs_bmap_intent_type type
,
6492 xfs_fileoff_t startoff
,
6493 xfs_fsblock_t startblock
,
6494 xfs_filblks_t blockcount
,
6497 struct xfs_bmbt_irec bmap
;
6499 xfs_fsblock_t firstfsb
;
6500 int flags
= XFS_BMAPI_REMAP
;
6504 bmap
.br_startblock
= startblock
;
6505 bmap
.br_startoff
= startoff
;
6506 bmap
.br_blockcount
= blockcount
;
6507 bmap
.br_state
= state
;
6509 trace_xfs_bmap_deferred(tp
->t_mountp
,
6510 XFS_FSB_TO_AGNO(tp
->t_mountp
, startblock
), type
,
6511 XFS_FSB_TO_AGBNO(tp
->t_mountp
, startblock
),
6512 ip
->i_ino
, whichfork
, startoff
, blockcount
, state
);
6514 if (whichfork
!= XFS_DATA_FORK
&& whichfork
!= XFS_ATTR_FORK
)
6515 return -EFSCORRUPTED
;
6516 if (whichfork
== XFS_ATTR_FORK
)
6517 flags
|= XFS_BMAPI_ATTRFORK
;
6519 if (XFS_TEST_ERROR(false, tp
->t_mountp
,
6520 XFS_ERRTAG_BMAP_FINISH_ONE
,
6521 XFS_RANDOM_BMAP_FINISH_ONE
))
6526 firstfsb
= bmap
.br_startblock
;
6527 error
= xfs_bmapi_write(tp
, ip
, bmap
.br_startoff
,
6528 bmap
.br_blockcount
, flags
, &firstfsb
,
6529 bmap
.br_blockcount
, &bmap
, &nimaps
,
6532 case XFS_BMAP_UNMAP
:
6533 error
= xfs_bunmapi(tp
, ip
, bmap
.br_startoff
,
6534 bmap
.br_blockcount
, flags
, 1, &firstfsb
,
6540 error
= -EFSCORRUPTED
;