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 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
&&
137 XFS_IFORK_NEXTENTS(ip
, whichfork
) >
138 XFS_IFORK_MAXEXT(ip
, whichfork
);
142 * Check if the inode should be converted to extent format.
144 static inline bool xfs_bmap_wants_extents(struct xfs_inode
*ip
, int whichfork
)
146 return XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
&&
147 XFS_IFORK_NEXTENTS(ip
, whichfork
) <=
148 XFS_IFORK_MAXEXT(ip
, whichfork
);
152 * Update the record referred to by cur to the value given
153 * by [off, bno, len, state].
154 * This either works (return 0) or gets an EFSCORRUPTED error.
158 struct xfs_btree_cur
*cur
,
164 union xfs_btree_rec rec
;
166 xfs_bmbt_disk_set_allf(&rec
.bmbt
, off
, bno
, len
, state
);
167 return xfs_btree_update(cur
, &rec
);
171 * Compute the worst-case number of indirect blocks that will be used
172 * for ip's delayed extent of length "len".
175 xfs_bmap_worst_indlen(
176 xfs_inode_t
*ip
, /* incore inode pointer */
177 xfs_filblks_t len
) /* delayed extent length */
179 int level
; /* btree level number */
180 int maxrecs
; /* maximum record count at this level */
181 xfs_mount_t
*mp
; /* mount structure */
182 xfs_filblks_t rval
; /* return value */
185 maxrecs
= mp
->m_bmap_dmxr
[0];
186 for (level
= 0, rval
= 0;
187 level
< XFS_BM_MAXLEVELS(mp
, XFS_DATA_FORK
);
190 do_div(len
, maxrecs
);
193 return rval
+ XFS_BM_MAXLEVELS(mp
, XFS_DATA_FORK
) -
196 maxrecs
= mp
->m_bmap_dmxr
[1];
202 * Calculate the default attribute fork offset for newly created inodes.
205 xfs_default_attroffset(
206 struct xfs_inode
*ip
)
208 struct xfs_mount
*mp
= ip
->i_mount
;
211 if (mp
->m_sb
.sb_inodesize
== 256) {
212 offset
= XFS_LITINO(mp
, ip
->i_d
.di_version
) -
213 XFS_BMDR_SPACE_CALC(MINABTPTRS
);
215 offset
= XFS_BMDR_SPACE_CALC(6 * MINABTPTRS
);
218 ASSERT(offset
< XFS_LITINO(mp
, ip
->i_d
.di_version
));
223 * Helper routine to reset inode di_forkoff field when switching
224 * attribute fork from local to extent format - we reset it where
225 * possible to make space available for inline data fork extents.
228 xfs_bmap_forkoff_reset(
232 if (whichfork
== XFS_ATTR_FORK
&&
233 ip
->i_d
.di_format
!= XFS_DINODE_FMT_DEV
&&
234 ip
->i_d
.di_format
!= XFS_DINODE_FMT_UUID
&&
235 ip
->i_d
.di_format
!= XFS_DINODE_FMT_BTREE
) {
236 uint dfl_forkoff
= xfs_default_attroffset(ip
) >> 3;
238 if (dfl_forkoff
> ip
->i_d
.di_forkoff
)
239 ip
->i_d
.di_forkoff
= dfl_forkoff
;
244 STATIC
struct xfs_buf
*
246 struct xfs_btree_cur
*cur
,
249 struct xfs_log_item_desc
*lidp
;
255 for (i
= 0; i
< XFS_BTREE_MAXLEVELS
; i
++) {
256 if (!cur
->bc_bufs
[i
])
258 if (XFS_BUF_ADDR(cur
->bc_bufs
[i
]) == bno
)
259 return cur
->bc_bufs
[i
];
262 /* Chase down all the log items to see if the bp is there */
263 list_for_each_entry(lidp
, &cur
->bc_tp
->t_items
, lid_trans
) {
264 struct xfs_buf_log_item
*bip
;
265 bip
= (struct xfs_buf_log_item
*)lidp
->lid_item
;
266 if (bip
->bli_item
.li_type
== XFS_LI_BUF
&&
267 XFS_BUF_ADDR(bip
->bli_buf
) == bno
)
276 struct xfs_btree_block
*block
,
282 __be64
*pp
, *thispa
; /* pointer to block address */
283 xfs_bmbt_key_t
*prevp
, *keyp
;
285 ASSERT(be16_to_cpu(block
->bb_level
) > 0);
288 for( i
= 1; i
<= xfs_btree_get_numrecs(block
); i
++) {
289 dmxr
= mp
->m_bmap_dmxr
[0];
290 keyp
= XFS_BMBT_KEY_ADDR(mp
, block
, i
);
293 ASSERT(be64_to_cpu(prevp
->br_startoff
) <
294 be64_to_cpu(keyp
->br_startoff
));
299 * Compare the block numbers to see if there are dups.
302 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, i
, sz
);
304 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, i
, dmxr
);
306 for (j
= i
+1; j
<= be16_to_cpu(block
->bb_numrecs
); j
++) {
308 thispa
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, j
, sz
);
310 thispa
= XFS_BMBT_PTR_ADDR(mp
, block
, j
, dmxr
);
311 if (*thispa
== *pp
) {
312 xfs_warn(mp
, "%s: thispa(%d) == pp(%d) %Ld",
314 (unsigned long long)be64_to_cpu(*thispa
));
315 panic("%s: ptrs are equal in node\n",
323 * Check that the extents for the inode ip are in the right order in all
324 * btree leaves. THis becomes prohibitively expensive for large extent count
325 * files, so don't bother with inodes that have more than 10,000 extents in
326 * them. The btree record ordering checks will still be done, so for such large
327 * bmapbt constructs that is going to catch most corruptions.
330 xfs_bmap_check_leaf_extents(
331 xfs_btree_cur_t
*cur
, /* btree cursor or null */
332 xfs_inode_t
*ip
, /* incore inode pointer */
333 int whichfork
) /* data or attr fork */
335 struct xfs_btree_block
*block
; /* current btree block */
336 xfs_fsblock_t bno
; /* block # of "block" */
337 xfs_buf_t
*bp
; /* buffer for "block" */
338 int error
; /* error return value */
339 xfs_extnum_t i
=0, j
; /* index into the extents list */
340 xfs_ifork_t
*ifp
; /* fork structure */
341 int level
; /* btree level, for checking */
342 xfs_mount_t
*mp
; /* file system mount structure */
343 __be64
*pp
; /* pointer to block address */
344 xfs_bmbt_rec_t
*ep
; /* pointer to current extent */
345 xfs_bmbt_rec_t last
= {0, 0}; /* last extent in prev block */
346 xfs_bmbt_rec_t
*nextp
; /* pointer to next extent */
349 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
) {
353 /* skip large extent count inodes */
354 if (ip
->i_d
.di_nextents
> 10000)
359 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
360 block
= ifp
->if_broot
;
362 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
364 level
= be16_to_cpu(block
->bb_level
);
366 xfs_check_block(block
, mp
, 1, ifp
->if_broot_bytes
);
367 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, 1, ifp
->if_broot_bytes
);
368 bno
= be64_to_cpu(*pp
);
370 ASSERT(bno
!= NULLFSBLOCK
);
371 ASSERT(XFS_FSB_TO_AGNO(mp
, bno
) < mp
->m_sb
.sb_agcount
);
372 ASSERT(XFS_FSB_TO_AGBNO(mp
, bno
) < mp
->m_sb
.sb_agblocks
);
375 * Go down the tree until leaf level is reached, following the first
376 * pointer (leftmost) at each level.
378 while (level
-- > 0) {
379 /* See if buf is in cur first */
381 bp
= xfs_bmap_get_bp(cur
, XFS_FSB_TO_DADDR(mp
, bno
));
384 error
= xfs_btree_read_bufl(mp
, NULL
, bno
, 0, &bp
,
390 block
= XFS_BUF_TO_BLOCK(bp
);
395 * Check this block for basic sanity (increasing keys and
396 * no duplicate blocks).
399 xfs_check_block(block
, mp
, 0, 0);
400 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, mp
->m_bmap_dmxr
[1]);
401 bno
= be64_to_cpu(*pp
);
402 XFS_WANT_CORRUPTED_GOTO(mp
,
403 XFS_FSB_SANITY_CHECK(mp
, bno
), error0
);
406 xfs_trans_brelse(NULL
, bp
);
411 * Here with bp and block set to the leftmost leaf node in the tree.
416 * Loop over all leaf nodes checking that all extents are in the right order.
419 xfs_fsblock_t nextbno
;
420 xfs_extnum_t num_recs
;
423 num_recs
= xfs_btree_get_numrecs(block
);
426 * Read-ahead the next leaf block, if any.
429 nextbno
= be64_to_cpu(block
->bb_u
.l
.bb_rightsib
);
432 * Check all the extents to make sure they are OK.
433 * If we had a previous block, the last entry should
434 * conform with the first entry in this one.
437 ep
= XFS_BMBT_REC_ADDR(mp
, block
, 1);
439 ASSERT(xfs_bmbt_disk_get_startoff(&last
) +
440 xfs_bmbt_disk_get_blockcount(&last
) <=
441 xfs_bmbt_disk_get_startoff(ep
));
443 for (j
= 1; j
< num_recs
; j
++) {
444 nextp
= XFS_BMBT_REC_ADDR(mp
, block
, j
+ 1);
445 ASSERT(xfs_bmbt_disk_get_startoff(ep
) +
446 xfs_bmbt_disk_get_blockcount(ep
) <=
447 xfs_bmbt_disk_get_startoff(nextp
));
455 xfs_trans_brelse(NULL
, bp
);
459 * If we've reached the end, stop.
461 if (bno
== NULLFSBLOCK
)
465 bp
= xfs_bmap_get_bp(cur
, XFS_FSB_TO_DADDR(mp
, bno
));
468 error
= xfs_btree_read_bufl(mp
, NULL
, bno
, 0, &bp
,
474 block
= XFS_BUF_TO_BLOCK(bp
);
480 xfs_warn(mp
, "%s: at error0", __func__
);
482 xfs_trans_brelse(NULL
, bp
);
484 xfs_warn(mp
, "%s: BAD after btree leaves for %d extents",
486 panic("%s: CORRUPTED BTREE OR SOMETHING", __func__
);
491 * Add bmap trace insert entries for all the contents of the extent records.
494 xfs_bmap_trace_exlist(
495 xfs_inode_t
*ip
, /* incore inode pointer */
496 xfs_extnum_t cnt
, /* count of entries in the list */
497 int whichfork
, /* data or attr fork */
498 unsigned long caller_ip
)
500 xfs_extnum_t idx
; /* extent record index */
501 xfs_ifork_t
*ifp
; /* inode fork pointer */
504 if (whichfork
== XFS_ATTR_FORK
)
505 state
|= BMAP_ATTRFORK
;
507 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
508 ASSERT(cnt
== (ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
)));
509 for (idx
= 0; idx
< cnt
; idx
++)
510 trace_xfs_extlist(ip
, idx
, whichfork
, caller_ip
);
514 * Validate that the bmbt_irecs being returned from bmapi are valid
515 * given the caller's original parameters. Specifically check the
516 * ranges of the returned irecs to ensure that they only extend beyond
517 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
520 xfs_bmap_validate_ret(
524 xfs_bmbt_irec_t
*mval
,
528 int i
; /* index to map values */
530 ASSERT(ret_nmap
<= nmap
);
532 for (i
= 0; i
< ret_nmap
; i
++) {
533 ASSERT(mval
[i
].br_blockcount
> 0);
534 if (!(flags
& XFS_BMAPI_ENTIRE
)) {
535 ASSERT(mval
[i
].br_startoff
>= bno
);
536 ASSERT(mval
[i
].br_blockcount
<= len
);
537 ASSERT(mval
[i
].br_startoff
+ mval
[i
].br_blockcount
<=
540 ASSERT(mval
[i
].br_startoff
< bno
+ len
);
541 ASSERT(mval
[i
].br_startoff
+ mval
[i
].br_blockcount
>
545 mval
[i
- 1].br_startoff
+ mval
[i
- 1].br_blockcount
==
546 mval
[i
].br_startoff
);
547 ASSERT(mval
[i
].br_startblock
!= DELAYSTARTBLOCK
&&
548 mval
[i
].br_startblock
!= HOLESTARTBLOCK
);
549 ASSERT(mval
[i
].br_state
== XFS_EXT_NORM
||
550 mval
[i
].br_state
== XFS_EXT_UNWRITTEN
);
555 #define xfs_bmap_check_leaf_extents(cur, ip, whichfork) do { } while (0)
556 #define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap)
560 * bmap free list manipulation functions
564 * Add the extent to the list of extents to be free at transaction end.
565 * The list is maintained sorted (by block number).
569 struct xfs_mount
*mp
,
570 struct xfs_defer_ops
*dfops
,
573 struct xfs_owner_info
*oinfo
)
575 struct xfs_extent_free_item
*new; /* new element */
580 ASSERT(bno
!= NULLFSBLOCK
);
582 ASSERT(len
<= MAXEXTLEN
);
583 ASSERT(!isnullstartblock(bno
));
584 agno
= XFS_FSB_TO_AGNO(mp
, bno
);
585 agbno
= XFS_FSB_TO_AGBNO(mp
, bno
);
586 ASSERT(agno
< mp
->m_sb
.sb_agcount
);
587 ASSERT(agbno
< mp
->m_sb
.sb_agblocks
);
588 ASSERT(len
< mp
->m_sb
.sb_agblocks
);
589 ASSERT(agbno
+ len
<= mp
->m_sb
.sb_agblocks
);
591 ASSERT(xfs_bmap_free_item_zone
!= NULL
);
593 new = kmem_zone_alloc(xfs_bmap_free_item_zone
, KM_SLEEP
);
594 new->xefi_startblock
= bno
;
595 new->xefi_blockcount
= (xfs_extlen_t
)len
;
597 new->xefi_oinfo
= *oinfo
;
599 xfs_rmap_skip_owner_update(&new->xefi_oinfo
);
600 trace_xfs_bmap_free_defer(mp
, XFS_FSB_TO_AGNO(mp
, bno
), 0,
601 XFS_FSB_TO_AGBNO(mp
, bno
), len
);
602 xfs_defer_add(dfops
, XFS_DEFER_OPS_TYPE_FREE
, &new->xefi_list
);
606 * Inode fork format manipulation functions
610 * Transform a btree format file with only one leaf node, where the
611 * extents list will fit in the inode, into an extents format file.
612 * Since the file extents are already in-core, all we have to do is
613 * give up the space for the btree root and pitch the leaf block.
615 STATIC
int /* error */
616 xfs_bmap_btree_to_extents(
617 xfs_trans_t
*tp
, /* transaction pointer */
618 xfs_inode_t
*ip
, /* incore inode pointer */
619 xfs_btree_cur_t
*cur
, /* btree cursor */
620 int *logflagsp
, /* inode logging flags */
621 int whichfork
) /* data or attr fork */
624 struct xfs_btree_block
*cblock
;/* child btree block */
625 xfs_fsblock_t cbno
; /* child block number */
626 xfs_buf_t
*cbp
; /* child block's buffer */
627 int error
; /* error return value */
628 xfs_ifork_t
*ifp
; /* inode fork data */
629 xfs_mount_t
*mp
; /* mount point structure */
630 __be64
*pp
; /* ptr to block address */
631 struct xfs_btree_block
*rblock
;/* root btree block */
632 struct xfs_owner_info oinfo
;
635 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
636 ASSERT(ifp
->if_flags
& XFS_IFEXTENTS
);
637 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
);
638 rblock
= ifp
->if_broot
;
639 ASSERT(be16_to_cpu(rblock
->bb_level
) == 1);
640 ASSERT(be16_to_cpu(rblock
->bb_numrecs
) == 1);
641 ASSERT(xfs_bmbt_maxrecs(mp
, ifp
->if_broot_bytes
, 0) == 1);
642 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, rblock
, 1, ifp
->if_broot_bytes
);
643 cbno
= be64_to_cpu(*pp
);
646 if ((error
= xfs_btree_check_lptr(cur
, cbno
, 1)))
649 error
= xfs_btree_read_bufl(mp
, tp
, cbno
, 0, &cbp
, XFS_BMAP_BTREE_REF
,
653 cblock
= XFS_BUF_TO_BLOCK(cbp
);
654 if ((error
= xfs_btree_check_block(cur
, cblock
, 0, cbp
)))
656 xfs_rmap_ino_bmbt_owner(&oinfo
, ip
->i_ino
, whichfork
);
657 xfs_bmap_add_free(mp
, cur
->bc_private
.b
.dfops
, cbno
, 1, &oinfo
);
658 ip
->i_d
.di_nblocks
--;
659 xfs_trans_mod_dquot_byino(tp
, ip
, XFS_TRANS_DQ_BCOUNT
, -1L);
660 xfs_trans_binval(tp
, cbp
);
661 if (cur
->bc_bufs
[0] == cbp
)
662 cur
->bc_bufs
[0] = NULL
;
663 xfs_iroot_realloc(ip
, -1, whichfork
);
664 ASSERT(ifp
->if_broot
== NULL
);
665 ASSERT((ifp
->if_flags
& XFS_IFBROOT
) == 0);
666 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_EXTENTS
);
667 *logflagsp
= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
672 * Convert an extents-format file into a btree-format file.
673 * The new file will have a root block (in the inode) and a single child block.
675 STATIC
int /* error */
676 xfs_bmap_extents_to_btree(
677 xfs_trans_t
*tp
, /* transaction pointer */
678 xfs_inode_t
*ip
, /* incore inode pointer */
679 xfs_fsblock_t
*firstblock
, /* first-block-allocated */
680 struct xfs_defer_ops
*dfops
, /* blocks freed in xaction */
681 xfs_btree_cur_t
**curp
, /* cursor returned to caller */
682 int wasdel
, /* converting a delayed alloc */
683 int *logflagsp
, /* inode logging flags */
684 int whichfork
) /* data or attr fork */
686 struct xfs_btree_block
*ablock
; /* allocated (child) bt block */
687 xfs_buf_t
*abp
; /* buffer for ablock */
688 xfs_alloc_arg_t args
; /* allocation arguments */
689 xfs_bmbt_rec_t
*arp
; /* child record pointer */
690 struct xfs_btree_block
*block
; /* btree root block */
691 xfs_btree_cur_t
*cur
; /* bmap btree cursor */
692 xfs_bmbt_rec_host_t
*ep
; /* extent record pointer */
693 int error
; /* error return value */
694 xfs_extnum_t i
, cnt
; /* extent record index */
695 xfs_ifork_t
*ifp
; /* inode fork pointer */
696 xfs_bmbt_key_t
*kp
; /* root block key pointer */
697 xfs_mount_t
*mp
; /* mount structure */
698 xfs_extnum_t nextents
; /* number of file extents */
699 xfs_bmbt_ptr_t
*pp
; /* root block address pointer */
702 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
703 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
);
706 * Make space in the inode incore.
708 xfs_iroot_realloc(ip
, 1, whichfork
);
709 ifp
->if_flags
|= XFS_IFBROOT
;
714 block
= ifp
->if_broot
;
715 if (xfs_sb_version_hascrc(&mp
->m_sb
))
716 xfs_btree_init_block_int(mp
, block
, XFS_BUF_DADDR_NULL
,
717 XFS_BMAP_CRC_MAGIC
, 1, 1, ip
->i_ino
,
718 XFS_BTREE_LONG_PTRS
| XFS_BTREE_CRC_BLOCKS
);
720 xfs_btree_init_block_int(mp
, block
, XFS_BUF_DADDR_NULL
,
721 XFS_BMAP_MAGIC
, 1, 1, ip
->i_ino
,
722 XFS_BTREE_LONG_PTRS
);
725 * Need a cursor. Can't allocate until bb_level is filled in.
727 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
728 cur
->bc_private
.b
.firstblock
= *firstblock
;
729 cur
->bc_private
.b
.dfops
= dfops
;
730 cur
->bc_private
.b
.flags
= wasdel
? XFS_BTCUR_BPRV_WASDEL
: 0;
732 * Convert to a btree with two levels, one record in root.
734 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_BTREE
);
735 memset(&args
, 0, sizeof(args
));
738 xfs_rmap_ino_bmbt_owner(&args
.oinfo
, ip
->i_ino
, whichfork
);
739 args
.firstblock
= *firstblock
;
740 if (*firstblock
== NULLFSBLOCK
) {
741 args
.type
= XFS_ALLOCTYPE_START_BNO
;
742 args
.fsbno
= XFS_INO_TO_FSB(mp
, ip
->i_ino
);
743 } else if (dfops
->dop_low
) {
744 args
.type
= XFS_ALLOCTYPE_START_BNO
;
745 args
.fsbno
= *firstblock
;
747 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
748 args
.fsbno
= *firstblock
;
750 args
.minlen
= args
.maxlen
= args
.prod
= 1;
751 args
.wasdel
= wasdel
;
753 if ((error
= xfs_alloc_vextent(&args
))) {
754 xfs_iroot_realloc(ip
, -1, whichfork
);
755 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
759 * Allocation can't fail, the space was reserved.
761 ASSERT(args
.fsbno
!= NULLFSBLOCK
);
762 ASSERT(*firstblock
== NULLFSBLOCK
||
763 args
.agno
== XFS_FSB_TO_AGNO(mp
, *firstblock
) ||
765 args
.agno
> XFS_FSB_TO_AGNO(mp
, *firstblock
)));
766 *firstblock
= cur
->bc_private
.b
.firstblock
= args
.fsbno
;
767 cur
->bc_private
.b
.allocated
++;
768 ip
->i_d
.di_nblocks
++;
769 xfs_trans_mod_dquot_byino(tp
, ip
, XFS_TRANS_DQ_BCOUNT
, 1L);
770 abp
= xfs_btree_get_bufl(mp
, tp
, args
.fsbno
, 0);
772 * Fill in the child block.
774 abp
->b_ops
= &xfs_bmbt_buf_ops
;
775 ablock
= XFS_BUF_TO_BLOCK(abp
);
776 if (xfs_sb_version_hascrc(&mp
->m_sb
))
777 xfs_btree_init_block_int(mp
, ablock
, abp
->b_bn
,
778 XFS_BMAP_CRC_MAGIC
, 0, 0, ip
->i_ino
,
779 XFS_BTREE_LONG_PTRS
| XFS_BTREE_CRC_BLOCKS
);
781 xfs_btree_init_block_int(mp
, ablock
, abp
->b_bn
,
782 XFS_BMAP_MAGIC
, 0, 0, ip
->i_ino
,
783 XFS_BTREE_LONG_PTRS
);
785 arp
= XFS_BMBT_REC_ADDR(mp
, ablock
, 1);
786 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
787 for (cnt
= i
= 0; i
< nextents
; i
++) {
788 ep
= xfs_iext_get_ext(ifp
, i
);
789 if (!isnullstartblock(xfs_bmbt_get_startblock(ep
))) {
790 arp
->l0
= cpu_to_be64(ep
->l0
);
791 arp
->l1
= cpu_to_be64(ep
->l1
);
795 ASSERT(cnt
== XFS_IFORK_NEXTENTS(ip
, whichfork
));
796 xfs_btree_set_numrecs(ablock
, cnt
);
799 * Fill in the root key and pointer.
801 kp
= XFS_BMBT_KEY_ADDR(mp
, block
, 1);
802 arp
= XFS_BMBT_REC_ADDR(mp
, ablock
, 1);
803 kp
->br_startoff
= cpu_to_be64(xfs_bmbt_disk_get_startoff(arp
));
804 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, xfs_bmbt_get_maxrecs(cur
,
805 be16_to_cpu(block
->bb_level
)));
806 *pp
= cpu_to_be64(args
.fsbno
);
809 * Do all this logging at the end so that
810 * the root is at the right level.
812 xfs_btree_log_block(cur
, abp
, XFS_BB_ALL_BITS
);
813 xfs_btree_log_recs(cur
, abp
, 1, be16_to_cpu(ablock
->bb_numrecs
));
814 ASSERT(*curp
== NULL
);
816 *logflagsp
= XFS_ILOG_CORE
| xfs_ilog_fbroot(whichfork
);
821 * Convert a local file to an extents file.
822 * This code is out of bounds for data forks of regular files,
823 * since the file data needs to get logged so things will stay consistent.
824 * (The bmap-level manipulations are ok, though).
827 xfs_bmap_local_to_extents_empty(
828 struct xfs_inode
*ip
,
831 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
833 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
834 ASSERT(ifp
->if_bytes
== 0);
835 ASSERT(XFS_IFORK_NEXTENTS(ip
, whichfork
) == 0);
837 xfs_bmap_forkoff_reset(ip
, whichfork
);
838 ifp
->if_flags
&= ~XFS_IFINLINE
;
839 ifp
->if_flags
|= XFS_IFEXTENTS
;
840 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_EXTENTS
);
844 STATIC
int /* error */
845 xfs_bmap_local_to_extents(
846 xfs_trans_t
*tp
, /* transaction pointer */
847 xfs_inode_t
*ip
, /* incore inode pointer */
848 xfs_fsblock_t
*firstblock
, /* first block allocated in xaction */
849 xfs_extlen_t total
, /* total blocks needed by transaction */
850 int *logflagsp
, /* inode logging flags */
852 void (*init_fn
)(struct xfs_trans
*tp
,
854 struct xfs_inode
*ip
,
855 struct xfs_ifork
*ifp
))
858 int flags
; /* logging flags returned */
859 xfs_ifork_t
*ifp
; /* inode fork pointer */
860 xfs_alloc_arg_t args
; /* allocation arguments */
861 xfs_buf_t
*bp
; /* buffer for extent block */
862 xfs_bmbt_rec_host_t
*ep
; /* extent record pointer */
865 * We don't want to deal with the case of keeping inode data inline yet.
866 * So sending the data fork of a regular inode is invalid.
868 ASSERT(!(S_ISREG(VFS_I(ip
)->i_mode
) && whichfork
== XFS_DATA_FORK
));
869 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
870 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
872 if (!ifp
->if_bytes
) {
873 xfs_bmap_local_to_extents_empty(ip
, whichfork
);
874 flags
= XFS_ILOG_CORE
;
880 ASSERT((ifp
->if_flags
& (XFS_IFINLINE
|XFS_IFEXTENTS
|XFS_IFEXTIREC
)) ==
882 memset(&args
, 0, sizeof(args
));
884 args
.mp
= ip
->i_mount
;
885 xfs_rmap_ino_owner(&args
.oinfo
, ip
->i_ino
, whichfork
, 0);
886 args
.firstblock
= *firstblock
;
888 * Allocate a block. We know we need only one, since the
889 * file currently fits in an inode.
891 if (*firstblock
== NULLFSBLOCK
) {
892 args
.fsbno
= XFS_INO_TO_FSB(args
.mp
, ip
->i_ino
);
893 args
.type
= XFS_ALLOCTYPE_START_BNO
;
895 args
.fsbno
= *firstblock
;
896 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
899 args
.minlen
= args
.maxlen
= args
.prod
= 1;
900 error
= xfs_alloc_vextent(&args
);
904 /* Can't fail, the space was reserved. */
905 ASSERT(args
.fsbno
!= NULLFSBLOCK
);
906 ASSERT(args
.len
== 1);
907 *firstblock
= args
.fsbno
;
908 bp
= xfs_btree_get_bufl(args
.mp
, tp
, args
.fsbno
, 0);
911 * Initialize the block, copy the data and log the remote buffer.
913 * The callout is responsible for logging because the remote format
914 * might differ from the local format and thus we don't know how much to
915 * log here. Note that init_fn must also set the buffer log item type
918 init_fn(tp
, bp
, ip
, ifp
);
920 /* account for the change in fork size */
921 xfs_idata_realloc(ip
, -ifp
->if_bytes
, whichfork
);
922 xfs_bmap_local_to_extents_empty(ip
, whichfork
);
923 flags
|= XFS_ILOG_CORE
;
925 xfs_iext_add(ifp
, 0, 1);
926 ep
= xfs_iext_get_ext(ifp
, 0);
927 xfs_bmbt_set_allf(ep
, 0, args
.fsbno
, 1, XFS_EXT_NORM
);
928 trace_xfs_bmap_post_update(ip
, 0,
929 whichfork
== XFS_ATTR_FORK
? BMAP_ATTRFORK
: 0,
931 XFS_IFORK_NEXT_SET(ip
, whichfork
, 1);
932 ip
->i_d
.di_nblocks
= 1;
933 xfs_trans_mod_dquot_byino(tp
, ip
,
934 XFS_TRANS_DQ_BCOUNT
, 1L);
935 flags
|= xfs_ilog_fext(whichfork
);
943 * Called from xfs_bmap_add_attrfork to handle btree format files.
945 STATIC
int /* error */
946 xfs_bmap_add_attrfork_btree(
947 xfs_trans_t
*tp
, /* transaction pointer */
948 xfs_inode_t
*ip
, /* incore inode pointer */
949 xfs_fsblock_t
*firstblock
, /* first block allocated */
950 struct xfs_defer_ops
*dfops
, /* blocks to free at commit */
951 int *flags
) /* inode logging flags */
953 xfs_btree_cur_t
*cur
; /* btree cursor */
954 int error
; /* error return value */
955 xfs_mount_t
*mp
; /* file system mount struct */
956 int stat
; /* newroot status */
959 if (ip
->i_df
.if_broot_bytes
<= XFS_IFORK_DSIZE(ip
))
960 *flags
|= XFS_ILOG_DBROOT
;
962 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, XFS_DATA_FORK
);
963 cur
->bc_private
.b
.dfops
= dfops
;
964 cur
->bc_private
.b
.firstblock
= *firstblock
;
965 if ((error
= xfs_bmbt_lookup_ge(cur
, 0, 0, 0, &stat
)))
967 /* must be at least one entry */
968 XFS_WANT_CORRUPTED_GOTO(mp
, stat
== 1, error0
);
969 if ((error
= xfs_btree_new_iroot(cur
, flags
, &stat
)))
972 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
975 *firstblock
= cur
->bc_private
.b
.firstblock
;
976 cur
->bc_private
.b
.allocated
= 0;
977 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
981 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
986 * Called from xfs_bmap_add_attrfork to handle extents format files.
988 STATIC
int /* error */
989 xfs_bmap_add_attrfork_extents(
990 xfs_trans_t
*tp
, /* transaction pointer */
991 xfs_inode_t
*ip
, /* incore inode pointer */
992 xfs_fsblock_t
*firstblock
, /* first block allocated */
993 struct xfs_defer_ops
*dfops
, /* blocks to free at commit */
994 int *flags
) /* inode logging flags */
996 xfs_btree_cur_t
*cur
; /* bmap btree cursor */
997 int error
; /* error return value */
999 if (ip
->i_d
.di_nextents
* sizeof(xfs_bmbt_rec_t
) <= XFS_IFORK_DSIZE(ip
))
1002 error
= xfs_bmap_extents_to_btree(tp
, ip
, firstblock
, dfops
, &cur
, 0,
1003 flags
, XFS_DATA_FORK
);
1005 cur
->bc_private
.b
.allocated
= 0;
1006 xfs_btree_del_cursor(cur
,
1007 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
1013 * Called from xfs_bmap_add_attrfork to handle local format files. Each
1014 * different data fork content type needs a different callout to do the
1015 * conversion. Some are basic and only require special block initialisation
1016 * callouts for the data formating, others (directories) are so specialised they
1017 * handle everything themselves.
1019 * XXX (dgc): investigate whether directory conversion can use the generic
1020 * formatting callout. It should be possible - it's just a very complex
1023 STATIC
int /* error */
1024 xfs_bmap_add_attrfork_local(
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_da_args_t dargs
; /* args for dir/attr code */
1033 if (ip
->i_df
.if_bytes
<= XFS_IFORK_DSIZE(ip
))
1036 if (S_ISDIR(VFS_I(ip
)->i_mode
)) {
1037 memset(&dargs
, 0, sizeof(dargs
));
1038 dargs
.geo
= ip
->i_mount
->m_dir_geo
;
1040 dargs
.firstblock
= firstblock
;
1041 dargs
.dfops
= dfops
;
1042 dargs
.total
= dargs
.geo
->fsbcount
;
1043 dargs
.whichfork
= XFS_DATA_FORK
;
1045 return xfs_dir2_sf_to_block(&dargs
);
1048 if (S_ISLNK(VFS_I(ip
)->i_mode
))
1049 return xfs_bmap_local_to_extents(tp
, ip
, firstblock
, 1,
1050 flags
, XFS_DATA_FORK
,
1051 xfs_symlink_local_to_remote
);
1053 /* should only be called for types that support local format data */
1055 return -EFSCORRUPTED
;
1059 * Convert inode from non-attributed to attributed.
1060 * Must not be in a transaction, ip must not be locked.
1062 int /* error code */
1063 xfs_bmap_add_attrfork(
1064 xfs_inode_t
*ip
, /* incore inode pointer */
1065 int size
, /* space new attribute needs */
1066 int rsvd
) /* xact may use reserved blks */
1068 xfs_fsblock_t firstblock
; /* 1st block/ag allocated */
1069 struct xfs_defer_ops dfops
; /* freed extent records */
1070 xfs_mount_t
*mp
; /* mount structure */
1071 xfs_trans_t
*tp
; /* transaction pointer */
1072 int blks
; /* space reservation */
1073 int version
= 1; /* superblock attr version */
1074 int logflags
; /* logging flags */
1075 int error
; /* error return value */
1077 ASSERT(XFS_IFORK_Q(ip
) == 0);
1080 ASSERT(!XFS_NOT_DQATTACHED(mp
, ip
));
1082 blks
= XFS_ADDAFORK_SPACE_RES(mp
);
1084 error
= xfs_trans_alloc(mp
, &M_RES(mp
)->tr_addafork
, blks
, 0,
1085 rsvd
? XFS_TRANS_RESERVE
: 0, &tp
);
1089 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
1090 error
= xfs_trans_reserve_quota_nblks(tp
, ip
, blks
, 0, rsvd
?
1091 XFS_QMOPT_RES_REGBLKS
| XFS_QMOPT_FORCE_RES
:
1092 XFS_QMOPT_RES_REGBLKS
);
1095 if (XFS_IFORK_Q(ip
))
1097 if (ip
->i_d
.di_aformat
!= XFS_DINODE_FMT_EXTENTS
) {
1099 * For inodes coming from pre-6.2 filesystems.
1101 ASSERT(ip
->i_d
.di_aformat
== 0);
1102 ip
->i_d
.di_aformat
= XFS_DINODE_FMT_EXTENTS
;
1104 ASSERT(ip
->i_d
.di_anextents
== 0);
1106 xfs_trans_ijoin(tp
, ip
, 0);
1107 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
1109 switch (ip
->i_d
.di_format
) {
1110 case XFS_DINODE_FMT_DEV
:
1111 ip
->i_d
.di_forkoff
= roundup(sizeof(xfs_dev_t
), 8) >> 3;
1113 case XFS_DINODE_FMT_UUID
:
1114 ip
->i_d
.di_forkoff
= roundup(sizeof(uuid_t
), 8) >> 3;
1116 case XFS_DINODE_FMT_LOCAL
:
1117 case XFS_DINODE_FMT_EXTENTS
:
1118 case XFS_DINODE_FMT_BTREE
:
1119 ip
->i_d
.di_forkoff
= xfs_attr_shortform_bytesfit(ip
, size
);
1120 if (!ip
->i_d
.di_forkoff
)
1121 ip
->i_d
.di_forkoff
= xfs_default_attroffset(ip
) >> 3;
1122 else if (mp
->m_flags
& XFS_MOUNT_ATTR2
)
1131 ASSERT(ip
->i_afp
== NULL
);
1132 ip
->i_afp
= kmem_zone_zalloc(xfs_ifork_zone
, KM_SLEEP
);
1133 ip
->i_afp
->if_flags
= XFS_IFEXTENTS
;
1135 xfs_defer_init(&dfops
, &firstblock
);
1136 switch (ip
->i_d
.di_format
) {
1137 case XFS_DINODE_FMT_LOCAL
:
1138 error
= xfs_bmap_add_attrfork_local(tp
, ip
, &firstblock
, &dfops
,
1141 case XFS_DINODE_FMT_EXTENTS
:
1142 error
= xfs_bmap_add_attrfork_extents(tp
, ip
, &firstblock
,
1145 case XFS_DINODE_FMT_BTREE
:
1146 error
= xfs_bmap_add_attrfork_btree(tp
, ip
, &firstblock
, &dfops
,
1154 xfs_trans_log_inode(tp
, ip
, logflags
);
1157 if (!xfs_sb_version_hasattr(&mp
->m_sb
) ||
1158 (!xfs_sb_version_hasattr2(&mp
->m_sb
) && version
== 2)) {
1159 bool log_sb
= false;
1161 spin_lock(&mp
->m_sb_lock
);
1162 if (!xfs_sb_version_hasattr(&mp
->m_sb
)) {
1163 xfs_sb_version_addattr(&mp
->m_sb
);
1166 if (!xfs_sb_version_hasattr2(&mp
->m_sb
) && version
== 2) {
1167 xfs_sb_version_addattr2(&mp
->m_sb
);
1170 spin_unlock(&mp
->m_sb_lock
);
1175 error
= xfs_defer_finish(&tp
, &dfops
, NULL
);
1178 error
= xfs_trans_commit(tp
);
1179 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
1183 xfs_defer_cancel(&dfops
);
1185 xfs_trans_cancel(tp
);
1186 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
1191 * Internal and external extent tree search functions.
1195 * Read in the extents to if_extents.
1196 * All inode fields are set up by caller, we just traverse the btree
1197 * and copy the records in. If the file system cannot contain unwritten
1198 * extents, the records are checked for no "state" flags.
1201 xfs_bmap_read_extents(
1202 xfs_trans_t
*tp
, /* transaction pointer */
1203 xfs_inode_t
*ip
, /* incore inode */
1204 int whichfork
) /* data or attr fork */
1206 struct xfs_btree_block
*block
; /* current btree block */
1207 xfs_fsblock_t bno
; /* block # of "block" */
1208 xfs_buf_t
*bp
; /* buffer for "block" */
1209 int error
; /* error return value */
1210 xfs_exntfmt_t exntf
; /* XFS_EXTFMT_NOSTATE, if checking */
1211 xfs_extnum_t i
, j
; /* index into the extents list */
1212 xfs_ifork_t
*ifp
; /* fork structure */
1213 int level
; /* btree level, for checking */
1214 xfs_mount_t
*mp
; /* file system mount structure */
1215 __be64
*pp
; /* pointer to block address */
1217 xfs_extnum_t room
; /* number of entries there's room for */
1221 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1222 exntf
= (whichfork
!= XFS_DATA_FORK
) ? XFS_EXTFMT_NOSTATE
:
1223 XFS_EXTFMT_INODE(ip
);
1224 block
= ifp
->if_broot
;
1226 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
1228 level
= be16_to_cpu(block
->bb_level
);
1230 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, 1, ifp
->if_broot_bytes
);
1231 bno
= be64_to_cpu(*pp
);
1232 ASSERT(bno
!= NULLFSBLOCK
);
1233 ASSERT(XFS_FSB_TO_AGNO(mp
, bno
) < mp
->m_sb
.sb_agcount
);
1234 ASSERT(XFS_FSB_TO_AGBNO(mp
, bno
) < mp
->m_sb
.sb_agblocks
);
1236 * Go down the tree until leaf level is reached, following the first
1237 * pointer (leftmost) at each level.
1239 while (level
-- > 0) {
1240 error
= xfs_btree_read_bufl(mp
, tp
, bno
, 0, &bp
,
1241 XFS_BMAP_BTREE_REF
, &xfs_bmbt_buf_ops
);
1244 block
= XFS_BUF_TO_BLOCK(bp
);
1247 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, mp
->m_bmap_dmxr
[1]);
1248 bno
= be64_to_cpu(*pp
);
1249 XFS_WANT_CORRUPTED_GOTO(mp
,
1250 XFS_FSB_SANITY_CHECK(mp
, bno
), error0
);
1251 xfs_trans_brelse(tp
, bp
);
1254 * Here with bp and block set to the leftmost leaf node in the tree.
1256 room
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
1259 * Loop over all leaf nodes. Copy information to the extent records.
1262 xfs_bmbt_rec_t
*frp
;
1263 xfs_fsblock_t nextbno
;
1264 xfs_extnum_t num_recs
;
1267 num_recs
= xfs_btree_get_numrecs(block
);
1268 if (unlikely(i
+ num_recs
> room
)) {
1269 ASSERT(i
+ num_recs
<= room
);
1270 xfs_warn(ip
->i_mount
,
1271 "corrupt dinode %Lu, (btree extents).",
1272 (unsigned long long) ip
->i_ino
);
1273 XFS_CORRUPTION_ERROR("xfs_bmap_read_extents(1)",
1274 XFS_ERRLEVEL_LOW
, ip
->i_mount
, block
);
1278 * Read-ahead the next leaf block, if any.
1280 nextbno
= be64_to_cpu(block
->bb_u
.l
.bb_rightsib
);
1281 if (nextbno
!= NULLFSBLOCK
)
1282 xfs_btree_reada_bufl(mp
, nextbno
, 1,
1285 * Copy records into the extent records.
1287 frp
= XFS_BMBT_REC_ADDR(mp
, block
, 1);
1289 for (j
= 0; j
< num_recs
; j
++, i
++, frp
++) {
1290 xfs_bmbt_rec_host_t
*trp
= xfs_iext_get_ext(ifp
, i
);
1291 trp
->l0
= be64_to_cpu(frp
->l0
);
1292 trp
->l1
= be64_to_cpu(frp
->l1
);
1294 if (exntf
== XFS_EXTFMT_NOSTATE
) {
1296 * Check all attribute bmap btree records and
1297 * any "older" data bmap btree records for a
1298 * set bit in the "extent flag" position.
1300 if (unlikely(xfs_check_nostate_extents(ifp
,
1301 start
, num_recs
))) {
1302 XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
1308 xfs_trans_brelse(tp
, bp
);
1311 * If we've reached the end, stop.
1313 if (bno
== NULLFSBLOCK
)
1315 error
= xfs_btree_read_bufl(mp
, tp
, bno
, 0, &bp
,
1316 XFS_BMAP_BTREE_REF
, &xfs_bmbt_buf_ops
);
1319 block
= XFS_BUF_TO_BLOCK(bp
);
1321 ASSERT(i
== (ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
)));
1322 ASSERT(i
== XFS_IFORK_NEXTENTS(ip
, whichfork
));
1323 XFS_BMAP_TRACE_EXLIST(ip
, i
, whichfork
);
1326 xfs_trans_brelse(tp
, bp
);
1327 return -EFSCORRUPTED
;
1332 * Search the extent records for the entry containing block bno.
1333 * If bno lies in a hole, point to the next entry. If bno lies
1334 * past eof, *eofp will be set, and *prevp will contain the last
1335 * entry (null if none). Else, *lastxp will be set to the index
1336 * of the found entry; *gotp will contain the entry.
1338 STATIC xfs_bmbt_rec_host_t
* /* pointer to found extent entry */
1339 xfs_bmap_search_multi_extents(
1340 xfs_ifork_t
*ifp
, /* inode fork pointer */
1341 xfs_fileoff_t bno
, /* block number searched for */
1342 int *eofp
, /* out: end of file found */
1343 xfs_extnum_t
*lastxp
, /* out: last extent index */
1344 xfs_bmbt_irec_t
*gotp
, /* out: extent entry found */
1345 xfs_bmbt_irec_t
*prevp
) /* out: previous extent entry found */
1347 xfs_bmbt_rec_host_t
*ep
; /* extent record pointer */
1348 xfs_extnum_t lastx
; /* last extent index */
1351 * Initialize the extent entry structure to catch access to
1352 * uninitialized br_startblock field.
1354 gotp
->br_startoff
= 0xffa5a5a5a5a5a5a5LL
;
1355 gotp
->br_blockcount
= 0xa55a5a5a5a5a5a5aLL
;
1356 gotp
->br_state
= XFS_EXT_INVALID
;
1357 gotp
->br_startblock
= 0xffffa5a5a5a5a5a5LL
;
1358 prevp
->br_startoff
= NULLFILEOFF
;
1360 ep
= xfs_iext_bno_to_ext(ifp
, bno
, &lastx
);
1362 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, lastx
- 1), prevp
);
1364 if (lastx
< (ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
))) {
1365 xfs_bmbt_get_all(ep
, gotp
);
1379 * Search the extents list for the inode, for the extent containing bno.
1380 * If bno lies in a hole, point to the next entry. If bno lies past eof,
1381 * *eofp will be set, and *prevp will contain the last entry (null if none).
1382 * Else, *lastxp will be set to the index of the found
1383 * entry; *gotp will contain the entry.
1385 xfs_bmbt_rec_host_t
* /* pointer to found extent entry */
1386 xfs_bmap_search_extents(
1387 xfs_inode_t
*ip
, /* incore inode pointer */
1388 xfs_fileoff_t bno
, /* block number searched for */
1389 int fork
, /* data or attr fork */
1390 int *eofp
, /* out: end of file found */
1391 xfs_extnum_t
*lastxp
, /* out: last extent index */
1392 xfs_bmbt_irec_t
*gotp
, /* out: extent entry found */
1393 xfs_bmbt_irec_t
*prevp
) /* out: previous extent entry found */
1395 xfs_ifork_t
*ifp
; /* inode fork pointer */
1396 xfs_bmbt_rec_host_t
*ep
; /* extent record pointer */
1398 XFS_STATS_INC(ip
->i_mount
, xs_look_exlist
);
1399 ifp
= XFS_IFORK_PTR(ip
, fork
);
1401 ep
= xfs_bmap_search_multi_extents(ifp
, bno
, eofp
, lastxp
, gotp
, prevp
);
1403 if (unlikely(!(gotp
->br_startblock
) && (*lastxp
!= NULLEXTNUM
) &&
1404 !(XFS_IS_REALTIME_INODE(ip
) && fork
== XFS_DATA_FORK
))) {
1405 xfs_alert_tag(ip
->i_mount
, XFS_PTAG_FSBLOCK_ZERO
,
1406 "Access to block zero in inode %llu "
1407 "start_block: %llx start_off: %llx "
1408 "blkcnt: %llx extent-state: %x lastx: %x",
1409 (unsigned long long)ip
->i_ino
,
1410 (unsigned long long)gotp
->br_startblock
,
1411 (unsigned long long)gotp
->br_startoff
,
1412 (unsigned long long)gotp
->br_blockcount
,
1413 gotp
->br_state
, *lastxp
);
1414 *lastxp
= NULLEXTNUM
;
1422 * Returns the file-relative block number of the first unused block(s)
1423 * in the file with at least "len" logically contiguous blocks free.
1424 * This is the lowest-address hole if the file has holes, else the first block
1425 * past the end of file.
1426 * Return 0 if the file is currently local (in-inode).
1429 xfs_bmap_first_unused(
1430 xfs_trans_t
*tp
, /* transaction pointer */
1431 xfs_inode_t
*ip
, /* incore inode */
1432 xfs_extlen_t len
, /* size of hole to find */
1433 xfs_fileoff_t
*first_unused
, /* unused block */
1434 int whichfork
) /* data or attr fork */
1436 int error
; /* error return value */
1437 int idx
; /* extent record index */
1438 xfs_ifork_t
*ifp
; /* inode fork pointer */
1439 xfs_fileoff_t lastaddr
; /* last block number seen */
1440 xfs_fileoff_t lowest
; /* lowest useful block */
1441 xfs_fileoff_t max
; /* starting useful block */
1442 xfs_fileoff_t off
; /* offset for this block */
1443 xfs_extnum_t nextents
; /* number of extent entries */
1445 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
||
1446 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
||
1447 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
1448 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
) {
1452 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1453 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
1454 (error
= xfs_iread_extents(tp
, ip
, whichfork
)))
1456 lowest
= *first_unused
;
1457 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
1458 for (idx
= 0, lastaddr
= 0, max
= lowest
; idx
< nextents
; idx
++) {
1459 xfs_bmbt_rec_host_t
*ep
= xfs_iext_get_ext(ifp
, idx
);
1460 off
= xfs_bmbt_get_startoff(ep
);
1462 * See if the hole before this extent will work.
1464 if (off
>= lowest
+ len
&& off
- max
>= len
) {
1465 *first_unused
= max
;
1468 lastaddr
= off
+ xfs_bmbt_get_blockcount(ep
);
1469 max
= XFS_FILEOFF_MAX(lastaddr
, lowest
);
1471 *first_unused
= max
;
1476 * Returns the file-relative block number of the last block - 1 before
1477 * last_block (input value) in the file.
1478 * This is not based on i_size, it is based on the extent records.
1479 * Returns 0 for local files, as they do not have extent records.
1482 xfs_bmap_last_before(
1483 xfs_trans_t
*tp
, /* transaction pointer */
1484 xfs_inode_t
*ip
, /* incore inode */
1485 xfs_fileoff_t
*last_block
, /* last block */
1486 int whichfork
) /* data or attr fork */
1488 xfs_fileoff_t bno
; /* input file offset */
1489 int eof
; /* hit end of file */
1490 xfs_bmbt_rec_host_t
*ep
; /* pointer to last extent */
1491 int error
; /* error return value */
1492 xfs_bmbt_irec_t got
; /* current extent value */
1493 xfs_ifork_t
*ifp
; /* inode fork pointer */
1494 xfs_extnum_t lastx
; /* last extent used */
1495 xfs_bmbt_irec_t prev
; /* previous extent value */
1497 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
&&
1498 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
1499 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_LOCAL
)
1501 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
) {
1505 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1506 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
1507 (error
= xfs_iread_extents(tp
, ip
, whichfork
)))
1509 bno
= *last_block
- 1;
1510 ep
= xfs_bmap_search_extents(ip
, bno
, whichfork
, &eof
, &lastx
, &got
,
1512 if (eof
|| xfs_bmbt_get_startoff(ep
) > bno
) {
1513 if (prev
.br_startoff
== NULLFILEOFF
)
1516 *last_block
= prev
.br_startoff
+ prev
.br_blockcount
;
1519 * Otherwise *last_block is already the right answer.
1525 xfs_bmap_last_extent(
1526 struct xfs_trans
*tp
,
1527 struct xfs_inode
*ip
,
1529 struct xfs_bmbt_irec
*rec
,
1532 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1536 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
1537 error
= xfs_iread_extents(tp
, ip
, whichfork
);
1542 nextents
= ifp
->if_bytes
/ sizeof(xfs_bmbt_rec_t
);
1543 if (nextents
== 0) {
1548 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, nextents
- 1), rec
);
1554 * Check the last inode extent to determine whether this allocation will result
1555 * in blocks being allocated at the end of the file. When we allocate new data
1556 * blocks at the end of the file which do not start at the previous data block,
1557 * we will try to align the new blocks at stripe unit boundaries.
1559 * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be
1560 * at, or past the EOF.
1564 struct xfs_bmalloca
*bma
,
1567 struct xfs_bmbt_irec rec
;
1572 error
= xfs_bmap_last_extent(NULL
, bma
->ip
, whichfork
, &rec
,
1583 * Check if we are allocation or past the last extent, or at least into
1584 * the last delayed allocated extent.
1586 bma
->aeof
= bma
->offset
>= rec
.br_startoff
+ rec
.br_blockcount
||
1587 (bma
->offset
>= rec
.br_startoff
&&
1588 isnullstartblock(rec
.br_startblock
));
1593 * Returns the file-relative block number of the first block past eof in
1594 * the file. This is not based on i_size, it is based on the extent records.
1595 * Returns 0 for local files, as they do not have extent records.
1598 xfs_bmap_last_offset(
1599 struct xfs_inode
*ip
,
1600 xfs_fileoff_t
*last_block
,
1603 struct xfs_bmbt_irec rec
;
1609 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
)
1612 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
&&
1613 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
1616 error
= xfs_bmap_last_extent(NULL
, ip
, whichfork
, &rec
, &is_empty
);
1617 if (error
|| is_empty
)
1620 *last_block
= rec
.br_startoff
+ rec
.br_blockcount
;
1625 * Returns whether the selected fork of the inode has exactly one
1626 * block or not. For the data fork we check this matches di_size,
1627 * implying the file's range is 0..bsize-1.
1629 int /* 1=>1 block, 0=>otherwise */
1631 xfs_inode_t
*ip
, /* incore inode */
1632 int whichfork
) /* data or attr fork */
1634 xfs_bmbt_rec_host_t
*ep
; /* ptr to fork's extent */
1635 xfs_ifork_t
*ifp
; /* inode fork pointer */
1636 int rval
; /* return value */
1637 xfs_bmbt_irec_t s
; /* internal version of extent */
1640 if (whichfork
== XFS_DATA_FORK
)
1641 return XFS_ISIZE(ip
) == ip
->i_mount
->m_sb
.sb_blocksize
;
1643 if (XFS_IFORK_NEXTENTS(ip
, whichfork
) != 1)
1645 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
1647 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1648 ASSERT(ifp
->if_flags
& XFS_IFEXTENTS
);
1649 ep
= xfs_iext_get_ext(ifp
, 0);
1650 xfs_bmbt_get_all(ep
, &s
);
1651 rval
= s
.br_startoff
== 0 && s
.br_blockcount
== 1;
1652 if (rval
&& whichfork
== XFS_DATA_FORK
)
1653 ASSERT(XFS_ISIZE(ip
) == ip
->i_mount
->m_sb
.sb_blocksize
);
1658 * Extent tree manipulation functions used during allocation.
1662 * Convert a delayed allocation to a real allocation.
1664 STATIC
int /* error */
1665 xfs_bmap_add_extent_delay_real(
1666 struct xfs_bmalloca
*bma
)
1668 struct xfs_bmbt_irec
*new = &bma
->got
;
1669 int diff
; /* temp value */
1670 xfs_bmbt_rec_host_t
*ep
; /* extent entry for idx */
1671 int error
; /* error return value */
1672 int i
; /* temp state */
1673 xfs_ifork_t
*ifp
; /* inode fork pointer */
1674 xfs_fileoff_t new_endoff
; /* end offset of new entry */
1675 xfs_bmbt_irec_t r
[3]; /* neighbor extent entries */
1676 /* left is 0, right is 1, prev is 2 */
1677 int rval
=0; /* return value (logging flags) */
1678 int state
= 0;/* state bits, accessed thru macros */
1679 xfs_filblks_t da_new
; /* new count del alloc blocks used */
1680 xfs_filblks_t da_old
; /* old count del alloc blocks used */
1681 xfs_filblks_t temp
=0; /* value for da_new calculations */
1682 xfs_filblks_t temp2
=0;/* value for da_new calculations */
1683 int tmp_rval
; /* partial logging flags */
1684 struct xfs_mount
*mp
;
1685 int whichfork
= XFS_DATA_FORK
;
1687 mp
= bma
->ip
->i_mount
;
1688 ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
1690 ASSERT(bma
->idx
>= 0);
1691 ASSERT(bma
->idx
<= ifp
->if_bytes
/ sizeof(struct xfs_bmbt_rec
));
1692 ASSERT(!isnullstartblock(new->br_startblock
));
1694 (bma
->cur
->bc_private
.b
.flags
& XFS_BTCUR_BPRV_WASDEL
));
1696 XFS_STATS_INC(mp
, xs_add_exlist
);
1703 * Set up a bunch of variables to make the tests simpler.
1705 ep
= xfs_iext_get_ext(ifp
, bma
->idx
);
1706 xfs_bmbt_get_all(ep
, &PREV
);
1707 new_endoff
= new->br_startoff
+ new->br_blockcount
;
1708 ASSERT(PREV
.br_startoff
<= new->br_startoff
);
1709 ASSERT(PREV
.br_startoff
+ PREV
.br_blockcount
>= new_endoff
);
1711 da_old
= startblockval(PREV
.br_startblock
);
1715 * Set flags determining what part of the previous delayed allocation
1716 * extent is being replaced by a real allocation.
1718 if (PREV
.br_startoff
== new->br_startoff
)
1719 state
|= BMAP_LEFT_FILLING
;
1720 if (PREV
.br_startoff
+ PREV
.br_blockcount
== new_endoff
)
1721 state
|= BMAP_RIGHT_FILLING
;
1724 * Check and set flags if this segment has a left neighbor.
1725 * Don't set contiguous if the combined extent would be too large.
1728 state
|= BMAP_LEFT_VALID
;
1729 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
- 1), &LEFT
);
1731 if (isnullstartblock(LEFT
.br_startblock
))
1732 state
|= BMAP_LEFT_DELAY
;
1735 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
1736 LEFT
.br_startoff
+ LEFT
.br_blockcount
== new->br_startoff
&&
1737 LEFT
.br_startblock
+ LEFT
.br_blockcount
== new->br_startblock
&&
1738 LEFT
.br_state
== new->br_state
&&
1739 LEFT
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
1740 state
|= BMAP_LEFT_CONTIG
;
1743 * Check and set flags if this segment has a right neighbor.
1744 * Don't set contiguous if the combined extent would be too large.
1745 * Also check for all-three-contiguous being too large.
1747 if (bma
->idx
< ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
) - 1) {
1748 state
|= BMAP_RIGHT_VALID
;
1749 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
+ 1), &RIGHT
);
1751 if (isnullstartblock(RIGHT
.br_startblock
))
1752 state
|= BMAP_RIGHT_DELAY
;
1755 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
1756 new_endoff
== RIGHT
.br_startoff
&&
1757 new->br_startblock
+ new->br_blockcount
== RIGHT
.br_startblock
&&
1758 new->br_state
== RIGHT
.br_state
&&
1759 new->br_blockcount
+ RIGHT
.br_blockcount
<= MAXEXTLEN
&&
1760 ((state
& (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
1761 BMAP_RIGHT_FILLING
)) !=
1762 (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
1763 BMAP_RIGHT_FILLING
) ||
1764 LEFT
.br_blockcount
+ new->br_blockcount
+ RIGHT
.br_blockcount
1766 state
|= BMAP_RIGHT_CONTIG
;
1770 * Switch out based on the FILLING and CONTIG state bits.
1772 switch (state
& (BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
1773 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
)) {
1774 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
1775 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
1777 * Filling in all of a previously delayed allocation extent.
1778 * The left and right neighbors are both contiguous with new.
1781 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1782 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, bma
->idx
),
1783 LEFT
.br_blockcount
+ PREV
.br_blockcount
+
1784 RIGHT
.br_blockcount
);
1785 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1787 xfs_iext_remove(bma
->ip
, bma
->idx
+ 1, 2, state
);
1788 bma
->ip
->i_d
.di_nextents
--;
1789 if (bma
->cur
== NULL
)
1790 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1792 rval
= XFS_ILOG_CORE
;
1793 error
= xfs_bmbt_lookup_eq(bma
->cur
, RIGHT
.br_startoff
,
1794 RIGHT
.br_startblock
,
1795 RIGHT
.br_blockcount
, &i
);
1798 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1799 error
= xfs_btree_delete(bma
->cur
, &i
);
1802 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1803 error
= xfs_btree_decrement(bma
->cur
, 0, &i
);
1806 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1807 error
= xfs_bmbt_update(bma
->cur
, LEFT
.br_startoff
,
1809 LEFT
.br_blockcount
+
1810 PREV
.br_blockcount
+
1811 RIGHT
.br_blockcount
, LEFT
.br_state
);
1817 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
1819 * Filling in all of a previously delayed allocation extent.
1820 * The left neighbor is contiguous, the right is not.
1824 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1825 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, bma
->idx
),
1826 LEFT
.br_blockcount
+ PREV
.br_blockcount
);
1827 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1829 xfs_iext_remove(bma
->ip
, bma
->idx
+ 1, 1, state
);
1830 if (bma
->cur
== NULL
)
1831 rval
= XFS_ILOG_DEXT
;
1834 error
= xfs_bmbt_lookup_eq(bma
->cur
, LEFT
.br_startoff
,
1835 LEFT
.br_startblock
, LEFT
.br_blockcount
,
1839 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1840 error
= xfs_bmbt_update(bma
->cur
, LEFT
.br_startoff
,
1842 LEFT
.br_blockcount
+
1843 PREV
.br_blockcount
, LEFT
.br_state
);
1849 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
1851 * Filling in all of a previously delayed allocation extent.
1852 * The right neighbor is contiguous, the left is not.
1854 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1855 xfs_bmbt_set_startblock(ep
, new->br_startblock
);
1856 xfs_bmbt_set_blockcount(ep
,
1857 PREV
.br_blockcount
+ RIGHT
.br_blockcount
);
1858 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1860 xfs_iext_remove(bma
->ip
, bma
->idx
+ 1, 1, state
);
1861 if (bma
->cur
== NULL
)
1862 rval
= XFS_ILOG_DEXT
;
1865 error
= xfs_bmbt_lookup_eq(bma
->cur
, RIGHT
.br_startoff
,
1866 RIGHT
.br_startblock
,
1867 RIGHT
.br_blockcount
, &i
);
1870 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1871 error
= xfs_bmbt_update(bma
->cur
, PREV
.br_startoff
,
1873 PREV
.br_blockcount
+
1874 RIGHT
.br_blockcount
, PREV
.br_state
);
1880 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
1882 * Filling in all of a previously delayed allocation extent.
1883 * Neither the left nor right neighbors are contiguous with
1886 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1887 xfs_bmbt_set_startblock(ep
, new->br_startblock
);
1888 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1890 bma
->ip
->i_d
.di_nextents
++;
1891 if (bma
->cur
== NULL
)
1892 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1894 rval
= XFS_ILOG_CORE
;
1895 error
= xfs_bmbt_lookup_eq(bma
->cur
, new->br_startoff
,
1896 new->br_startblock
, new->br_blockcount
,
1900 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
1901 bma
->cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
1902 error
= xfs_btree_insert(bma
->cur
, &i
);
1905 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1909 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
:
1911 * Filling in the first part of a previous delayed allocation.
1912 * The left neighbor is contiguous.
1914 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
- 1, state
, _THIS_IP_
);
1915 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, bma
->idx
- 1),
1916 LEFT
.br_blockcount
+ new->br_blockcount
);
1917 xfs_bmbt_set_startoff(ep
,
1918 PREV
.br_startoff
+ new->br_blockcount
);
1919 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
- 1, state
, _THIS_IP_
);
1921 temp
= PREV
.br_blockcount
- new->br_blockcount
;
1922 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1923 xfs_bmbt_set_blockcount(ep
, temp
);
1924 if (bma
->cur
== NULL
)
1925 rval
= XFS_ILOG_DEXT
;
1928 error
= xfs_bmbt_lookup_eq(bma
->cur
, LEFT
.br_startoff
,
1929 LEFT
.br_startblock
, LEFT
.br_blockcount
,
1933 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1934 error
= xfs_bmbt_update(bma
->cur
, LEFT
.br_startoff
,
1936 LEFT
.br_blockcount
+
1942 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
1943 startblockval(PREV
.br_startblock
));
1944 xfs_bmbt_set_startblock(ep
, nullstartblock(da_new
));
1945 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1950 case BMAP_LEFT_FILLING
:
1952 * Filling in the first part of a previous delayed allocation.
1953 * The left neighbor is not contiguous.
1955 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1956 xfs_bmbt_set_startoff(ep
, new_endoff
);
1957 temp
= PREV
.br_blockcount
- new->br_blockcount
;
1958 xfs_bmbt_set_blockcount(ep
, temp
);
1959 xfs_iext_insert(bma
->ip
, bma
->idx
, 1, new, state
);
1960 bma
->ip
->i_d
.di_nextents
++;
1961 if (bma
->cur
== NULL
)
1962 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1964 rval
= XFS_ILOG_CORE
;
1965 error
= xfs_bmbt_lookup_eq(bma
->cur
, new->br_startoff
,
1966 new->br_startblock
, new->br_blockcount
,
1970 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
1971 bma
->cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
1972 error
= xfs_btree_insert(bma
->cur
, &i
);
1975 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1978 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
1979 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
1980 bma
->firstblock
, bma
->dfops
,
1981 &bma
->cur
, 1, &tmp_rval
, whichfork
);
1986 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
1987 startblockval(PREV
.br_startblock
) -
1988 (bma
->cur
? bma
->cur
->bc_private
.b
.allocated
: 0));
1989 ep
= xfs_iext_get_ext(ifp
, bma
->idx
+ 1);
1990 xfs_bmbt_set_startblock(ep
, nullstartblock(da_new
));
1991 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
+ 1, state
, _THIS_IP_
);
1994 case BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
1996 * Filling in the last part of a previous delayed allocation.
1997 * The right neighbor is contiguous with the new allocation.
1999 temp
= PREV
.br_blockcount
- new->br_blockcount
;
2000 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
+ 1, state
, _THIS_IP_
);
2001 xfs_bmbt_set_blockcount(ep
, temp
);
2002 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp
, bma
->idx
+ 1),
2003 new->br_startoff
, new->br_startblock
,
2004 new->br_blockcount
+ RIGHT
.br_blockcount
,
2006 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
+ 1, state
, _THIS_IP_
);
2007 if (bma
->cur
== NULL
)
2008 rval
= XFS_ILOG_DEXT
;
2011 error
= xfs_bmbt_lookup_eq(bma
->cur
, RIGHT
.br_startoff
,
2012 RIGHT
.br_startblock
,
2013 RIGHT
.br_blockcount
, &i
);
2016 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2017 error
= xfs_bmbt_update(bma
->cur
, new->br_startoff
,
2019 new->br_blockcount
+
2020 RIGHT
.br_blockcount
,
2026 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
2027 startblockval(PREV
.br_startblock
));
2028 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2029 xfs_bmbt_set_startblock(ep
, nullstartblock(da_new
));
2030 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2035 case BMAP_RIGHT_FILLING
:
2037 * Filling in the last part of a previous delayed allocation.
2038 * The right neighbor is not contiguous.
2040 temp
= PREV
.br_blockcount
- new->br_blockcount
;
2041 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2042 xfs_bmbt_set_blockcount(ep
, temp
);
2043 xfs_iext_insert(bma
->ip
, bma
->idx
+ 1, 1, new, state
);
2044 bma
->ip
->i_d
.di_nextents
++;
2045 if (bma
->cur
== NULL
)
2046 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2048 rval
= XFS_ILOG_CORE
;
2049 error
= xfs_bmbt_lookup_eq(bma
->cur
, new->br_startoff
,
2050 new->br_startblock
, new->br_blockcount
,
2054 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
2055 bma
->cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
2056 error
= xfs_btree_insert(bma
->cur
, &i
);
2059 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2062 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
2063 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
2064 bma
->firstblock
, bma
->dfops
, &bma
->cur
, 1,
2065 &tmp_rval
, whichfork
);
2070 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
2071 startblockval(PREV
.br_startblock
) -
2072 (bma
->cur
? bma
->cur
->bc_private
.b
.allocated
: 0));
2073 ep
= xfs_iext_get_ext(ifp
, bma
->idx
);
2074 xfs_bmbt_set_startblock(ep
, nullstartblock(da_new
));
2075 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2082 * Filling in the middle part of a previous delayed allocation.
2083 * Contiguity is impossible here.
2084 * This case is avoided almost all the time.
2086 * We start with a delayed allocation:
2088 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
2091 * and we are allocating:
2092 * +rrrrrrrrrrrrrrrrr+
2095 * and we set it up for insertion as:
2096 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
2098 * PREV @ idx LEFT RIGHT
2099 * inserted at idx + 1
2101 temp
= new->br_startoff
- PREV
.br_startoff
;
2102 temp2
= PREV
.br_startoff
+ PREV
.br_blockcount
- new_endoff
;
2103 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, 0, _THIS_IP_
);
2104 xfs_bmbt_set_blockcount(ep
, temp
); /* truncate PREV */
2106 RIGHT
.br_state
= PREV
.br_state
;
2107 RIGHT
.br_startblock
= nullstartblock(
2108 (int)xfs_bmap_worst_indlen(bma
->ip
, temp2
));
2109 RIGHT
.br_startoff
= new_endoff
;
2110 RIGHT
.br_blockcount
= temp2
;
2111 /* insert LEFT (r[0]) and RIGHT (r[1]) at the same time */
2112 xfs_iext_insert(bma
->ip
, bma
->idx
+ 1, 2, &LEFT
, state
);
2113 bma
->ip
->i_d
.di_nextents
++;
2114 if (bma
->cur
== NULL
)
2115 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2117 rval
= XFS_ILOG_CORE
;
2118 error
= xfs_bmbt_lookup_eq(bma
->cur
, new->br_startoff
,
2119 new->br_startblock
, new->br_blockcount
,
2123 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
2124 bma
->cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
2125 error
= xfs_btree_insert(bma
->cur
, &i
);
2128 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2131 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
2132 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
2133 bma
->firstblock
, bma
->dfops
, &bma
->cur
,
2134 1, &tmp_rval
, whichfork
);
2139 temp
= xfs_bmap_worst_indlen(bma
->ip
, temp
);
2140 temp2
= xfs_bmap_worst_indlen(bma
->ip
, temp2
);
2141 diff
= (int)(temp
+ temp2
- startblockval(PREV
.br_startblock
) -
2142 (bma
->cur
? bma
->cur
->bc_private
.b
.allocated
: 0));
2144 error
= xfs_mod_fdblocks(bma
->ip
->i_mount
,
2145 -((int64_t)diff
), false);
2151 ep
= xfs_iext_get_ext(ifp
, bma
->idx
);
2152 xfs_bmbt_set_startblock(ep
, nullstartblock((int)temp
));
2153 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2154 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
+ 2, state
, _THIS_IP_
);
2155 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp
, bma
->idx
+ 2),
2156 nullstartblock((int)temp2
));
2157 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
+ 2, state
, _THIS_IP_
);
2160 da_new
= temp
+ temp2
;
2163 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2164 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2165 case BMAP_LEFT_FILLING
| BMAP_RIGHT_CONTIG
:
2166 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
2167 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2168 case BMAP_LEFT_CONTIG
:
2169 case BMAP_RIGHT_CONTIG
:
2171 * These cases are all impossible.
2176 /* add reverse mapping */
2177 error
= xfs_rmap_map_extent(mp
, bma
->dfops
, bma
->ip
, whichfork
, new);
2181 /* convert to a btree if necessary */
2182 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
2183 int tmp_logflags
; /* partial log flag return val */
2185 ASSERT(bma
->cur
== NULL
);
2186 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
2187 bma
->firstblock
, bma
->dfops
, &bma
->cur
,
2188 da_old
> 0, &tmp_logflags
, whichfork
);
2189 bma
->logflags
|= tmp_logflags
;
2194 /* adjust for changes in reserved delayed indirect blocks */
2195 if (da_old
|| da_new
) {
2198 temp
+= bma
->cur
->bc_private
.b
.allocated
;
2199 ASSERT(temp
<= da_old
);
2201 xfs_mod_fdblocks(bma
->ip
->i_mount
,
2202 (int64_t)(da_old
- temp
), false);
2205 /* clear out the allocated field, done with it now in any case. */
2207 bma
->cur
->bc_private
.b
.allocated
= 0;
2209 xfs_bmap_check_leaf_extents(bma
->cur
, bma
->ip
, whichfork
);
2211 bma
->logflags
|= rval
;
2219 * Convert an unwritten allocation to a real allocation or vice versa.
2221 STATIC
int /* error */
2222 xfs_bmap_add_extent_unwritten_real(
2223 struct xfs_trans
*tp
,
2224 xfs_inode_t
*ip
, /* incore inode pointer */
2225 xfs_extnum_t
*idx
, /* extent number to update/insert */
2226 xfs_btree_cur_t
**curp
, /* if *curp is null, not a btree */
2227 xfs_bmbt_irec_t
*new, /* new data to add to file extents */
2228 xfs_fsblock_t
*first
, /* pointer to firstblock variable */
2229 struct xfs_defer_ops
*dfops
, /* list of extents to be freed */
2230 int *logflagsp
) /* inode logging flags */
2232 xfs_btree_cur_t
*cur
; /* btree cursor */
2233 xfs_bmbt_rec_host_t
*ep
; /* extent entry for idx */
2234 int error
; /* error return value */
2235 int i
; /* temp state */
2236 xfs_ifork_t
*ifp
; /* inode fork pointer */
2237 xfs_fileoff_t new_endoff
; /* end offset of new entry */
2238 xfs_exntst_t newext
; /* new extent state */
2239 xfs_exntst_t oldext
; /* old extent state */
2240 xfs_bmbt_irec_t r
[3]; /* neighbor extent entries */
2241 /* left is 0, right is 1, prev is 2 */
2242 int rval
=0; /* return value (logging flags) */
2243 int state
= 0;/* state bits, accessed thru macros */
2244 struct xfs_mount
*mp
= tp
->t_mountp
;
2249 ifp
= XFS_IFORK_PTR(ip
, XFS_DATA_FORK
);
2252 ASSERT(*idx
<= ifp
->if_bytes
/ sizeof(struct xfs_bmbt_rec
));
2253 ASSERT(!isnullstartblock(new->br_startblock
));
2255 XFS_STATS_INC(mp
, xs_add_exlist
);
2262 * Set up a bunch of variables to make the tests simpler.
2265 ep
= xfs_iext_get_ext(ifp
, *idx
);
2266 xfs_bmbt_get_all(ep
, &PREV
);
2267 newext
= new->br_state
;
2268 oldext
= (newext
== XFS_EXT_UNWRITTEN
) ?
2269 XFS_EXT_NORM
: XFS_EXT_UNWRITTEN
;
2270 ASSERT(PREV
.br_state
== oldext
);
2271 new_endoff
= new->br_startoff
+ new->br_blockcount
;
2272 ASSERT(PREV
.br_startoff
<= new->br_startoff
);
2273 ASSERT(PREV
.br_startoff
+ PREV
.br_blockcount
>= new_endoff
);
2276 * Set flags determining what part of the previous oldext allocation
2277 * extent is being replaced by a newext allocation.
2279 if (PREV
.br_startoff
== new->br_startoff
)
2280 state
|= BMAP_LEFT_FILLING
;
2281 if (PREV
.br_startoff
+ PREV
.br_blockcount
== new_endoff
)
2282 state
|= BMAP_RIGHT_FILLING
;
2285 * Check and set flags if this segment has a left neighbor.
2286 * Don't set contiguous if the combined extent would be too large.
2289 state
|= BMAP_LEFT_VALID
;
2290 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, *idx
- 1), &LEFT
);
2292 if (isnullstartblock(LEFT
.br_startblock
))
2293 state
|= BMAP_LEFT_DELAY
;
2296 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
2297 LEFT
.br_startoff
+ LEFT
.br_blockcount
== new->br_startoff
&&
2298 LEFT
.br_startblock
+ LEFT
.br_blockcount
== new->br_startblock
&&
2299 LEFT
.br_state
== newext
&&
2300 LEFT
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
2301 state
|= BMAP_LEFT_CONTIG
;
2304 * Check and set flags if this segment has a right neighbor.
2305 * Don't set contiguous if the combined extent would be too large.
2306 * Also check for all-three-contiguous being too large.
2308 if (*idx
< ip
->i_df
.if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
) - 1) {
2309 state
|= BMAP_RIGHT_VALID
;
2310 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, *idx
+ 1), &RIGHT
);
2311 if (isnullstartblock(RIGHT
.br_startblock
))
2312 state
|= BMAP_RIGHT_DELAY
;
2315 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
2316 new_endoff
== RIGHT
.br_startoff
&&
2317 new->br_startblock
+ new->br_blockcount
== RIGHT
.br_startblock
&&
2318 newext
== RIGHT
.br_state
&&
2319 new->br_blockcount
+ RIGHT
.br_blockcount
<= MAXEXTLEN
&&
2320 ((state
& (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
2321 BMAP_RIGHT_FILLING
)) !=
2322 (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
2323 BMAP_RIGHT_FILLING
) ||
2324 LEFT
.br_blockcount
+ new->br_blockcount
+ RIGHT
.br_blockcount
2326 state
|= BMAP_RIGHT_CONTIG
;
2329 * Switch out based on the FILLING and CONTIG state bits.
2331 switch (state
& (BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
2332 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
)) {
2333 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
2334 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2336 * Setting all of a previous oldext extent to newext.
2337 * The left and right neighbors are both contiguous with new.
2341 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2342 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
),
2343 LEFT
.br_blockcount
+ PREV
.br_blockcount
+
2344 RIGHT
.br_blockcount
);
2345 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2347 xfs_iext_remove(ip
, *idx
+ 1, 2, state
);
2348 ip
->i_d
.di_nextents
-= 2;
2350 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2352 rval
= XFS_ILOG_CORE
;
2353 if ((error
= xfs_bmbt_lookup_eq(cur
, RIGHT
.br_startoff
,
2354 RIGHT
.br_startblock
,
2355 RIGHT
.br_blockcount
, &i
)))
2357 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2358 if ((error
= xfs_btree_delete(cur
, &i
)))
2360 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2361 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2363 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2364 if ((error
= xfs_btree_delete(cur
, &i
)))
2366 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2367 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2369 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2370 if ((error
= xfs_bmbt_update(cur
, LEFT
.br_startoff
,
2372 LEFT
.br_blockcount
+ PREV
.br_blockcount
+
2373 RIGHT
.br_blockcount
, LEFT
.br_state
)))
2378 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
2380 * Setting all of a previous oldext extent to newext.
2381 * The left neighbor is contiguous, the right is not.
2385 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2386 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
),
2387 LEFT
.br_blockcount
+ PREV
.br_blockcount
);
2388 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2390 xfs_iext_remove(ip
, *idx
+ 1, 1, state
);
2391 ip
->i_d
.di_nextents
--;
2393 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2395 rval
= XFS_ILOG_CORE
;
2396 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2397 PREV
.br_startblock
, PREV
.br_blockcount
,
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_bmbt_update(cur
, LEFT
.br_startoff
,
2409 LEFT
.br_blockcount
+ PREV
.br_blockcount
,
2415 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2417 * Setting all of a previous oldext extent to newext.
2418 * The right neighbor is contiguous, the left is not.
2420 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2421 xfs_bmbt_set_blockcount(ep
,
2422 PREV
.br_blockcount
+ RIGHT
.br_blockcount
);
2423 xfs_bmbt_set_state(ep
, newext
);
2424 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2425 xfs_iext_remove(ip
, *idx
+ 1, 1, state
);
2426 ip
->i_d
.di_nextents
--;
2428 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2430 rval
= XFS_ILOG_CORE
;
2431 if ((error
= xfs_bmbt_lookup_eq(cur
, RIGHT
.br_startoff
,
2432 RIGHT
.br_startblock
,
2433 RIGHT
.br_blockcount
, &i
)))
2435 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2436 if ((error
= xfs_btree_delete(cur
, &i
)))
2438 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2439 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2441 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2442 if ((error
= xfs_bmbt_update(cur
, new->br_startoff
,
2444 new->br_blockcount
+ RIGHT
.br_blockcount
,
2450 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
2452 * Setting all of a previous oldext extent to newext.
2453 * Neither the left nor right neighbors are contiguous with
2456 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2457 xfs_bmbt_set_state(ep
, newext
);
2458 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2461 rval
= XFS_ILOG_DEXT
;
2464 if ((error
= xfs_bmbt_lookup_eq(cur
, new->br_startoff
,
2465 new->br_startblock
, new->br_blockcount
,
2468 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2469 if ((error
= xfs_bmbt_update(cur
, new->br_startoff
,
2470 new->br_startblock
, new->br_blockcount
,
2476 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
:
2478 * Setting the first part of a previous oldext extent to newext.
2479 * The left neighbor is contiguous.
2481 trace_xfs_bmap_pre_update(ip
, *idx
- 1, state
, _THIS_IP_
);
2482 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
- 1),
2483 LEFT
.br_blockcount
+ new->br_blockcount
);
2484 xfs_bmbt_set_startoff(ep
,
2485 PREV
.br_startoff
+ new->br_blockcount
);
2486 trace_xfs_bmap_post_update(ip
, *idx
- 1, state
, _THIS_IP_
);
2488 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2489 xfs_bmbt_set_startblock(ep
,
2490 new->br_startblock
+ new->br_blockcount
);
2491 xfs_bmbt_set_blockcount(ep
,
2492 PREV
.br_blockcount
- new->br_blockcount
);
2493 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2498 rval
= XFS_ILOG_DEXT
;
2501 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2502 PREV
.br_startblock
, PREV
.br_blockcount
,
2505 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2506 if ((error
= xfs_bmbt_update(cur
,
2507 PREV
.br_startoff
+ new->br_blockcount
,
2508 PREV
.br_startblock
+ new->br_blockcount
,
2509 PREV
.br_blockcount
- new->br_blockcount
,
2512 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2514 error
= xfs_bmbt_update(cur
, LEFT
.br_startoff
,
2516 LEFT
.br_blockcount
+ new->br_blockcount
,
2523 case BMAP_LEFT_FILLING
:
2525 * Setting the first part of a previous oldext extent to newext.
2526 * The left neighbor is not contiguous.
2528 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2529 ASSERT(ep
&& xfs_bmbt_get_state(ep
) == oldext
);
2530 xfs_bmbt_set_startoff(ep
, new_endoff
);
2531 xfs_bmbt_set_blockcount(ep
,
2532 PREV
.br_blockcount
- new->br_blockcount
);
2533 xfs_bmbt_set_startblock(ep
,
2534 new->br_startblock
+ new->br_blockcount
);
2535 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2537 xfs_iext_insert(ip
, *idx
, 1, new, state
);
2538 ip
->i_d
.di_nextents
++;
2540 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2542 rval
= XFS_ILOG_CORE
;
2543 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2544 PREV
.br_startblock
, PREV
.br_blockcount
,
2547 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2548 if ((error
= xfs_bmbt_update(cur
,
2549 PREV
.br_startoff
+ new->br_blockcount
,
2550 PREV
.br_startblock
+ new->br_blockcount
,
2551 PREV
.br_blockcount
- new->br_blockcount
,
2554 cur
->bc_rec
.b
= *new;
2555 if ((error
= xfs_btree_insert(cur
, &i
)))
2557 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2561 case BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2563 * Setting the last part of a previous oldext extent to newext.
2564 * The right neighbor is contiguous with the new allocation.
2566 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2567 xfs_bmbt_set_blockcount(ep
,
2568 PREV
.br_blockcount
- new->br_blockcount
);
2569 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2573 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2574 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp
, *idx
),
2575 new->br_startoff
, new->br_startblock
,
2576 new->br_blockcount
+ RIGHT
.br_blockcount
, newext
);
2577 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2580 rval
= XFS_ILOG_DEXT
;
2583 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2585 PREV
.br_blockcount
, &i
)))
2587 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2588 if ((error
= xfs_bmbt_update(cur
, PREV
.br_startoff
,
2590 PREV
.br_blockcount
- new->br_blockcount
,
2593 if ((error
= xfs_btree_increment(cur
, 0, &i
)))
2595 if ((error
= xfs_bmbt_update(cur
, new->br_startoff
,
2597 new->br_blockcount
+ RIGHT
.br_blockcount
,
2603 case BMAP_RIGHT_FILLING
:
2605 * Setting the last part of a previous oldext extent to newext.
2606 * The right neighbor is not contiguous.
2608 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2609 xfs_bmbt_set_blockcount(ep
,
2610 PREV
.br_blockcount
- new->br_blockcount
);
2611 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2614 xfs_iext_insert(ip
, *idx
, 1, new, state
);
2616 ip
->i_d
.di_nextents
++;
2618 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2620 rval
= XFS_ILOG_CORE
;
2621 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2622 PREV
.br_startblock
, PREV
.br_blockcount
,
2625 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2626 if ((error
= xfs_bmbt_update(cur
, PREV
.br_startoff
,
2628 PREV
.br_blockcount
- new->br_blockcount
,
2631 if ((error
= xfs_bmbt_lookup_eq(cur
, new->br_startoff
,
2632 new->br_startblock
, new->br_blockcount
,
2635 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
2636 cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
2637 if ((error
= xfs_btree_insert(cur
, &i
)))
2639 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2645 * Setting the middle part of a previous oldext extent to
2646 * newext. Contiguity is impossible here.
2647 * One extent becomes three extents.
2649 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2650 xfs_bmbt_set_blockcount(ep
,
2651 new->br_startoff
- PREV
.br_startoff
);
2652 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2655 r
[1].br_startoff
= new_endoff
;
2656 r
[1].br_blockcount
=
2657 PREV
.br_startoff
+ PREV
.br_blockcount
- new_endoff
;
2658 r
[1].br_startblock
= new->br_startblock
+ new->br_blockcount
;
2659 r
[1].br_state
= oldext
;
2662 xfs_iext_insert(ip
, *idx
, 2, &r
[0], state
);
2664 ip
->i_d
.di_nextents
+= 2;
2666 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2668 rval
= XFS_ILOG_CORE
;
2669 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2670 PREV
.br_startblock
, PREV
.br_blockcount
,
2673 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2674 /* new right extent - oldext */
2675 if ((error
= xfs_bmbt_update(cur
, r
[1].br_startoff
,
2676 r
[1].br_startblock
, r
[1].br_blockcount
,
2679 /* new left extent - oldext */
2680 cur
->bc_rec
.b
= PREV
;
2681 cur
->bc_rec
.b
.br_blockcount
=
2682 new->br_startoff
- PREV
.br_startoff
;
2683 if ((error
= xfs_btree_insert(cur
, &i
)))
2685 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2687 * Reset the cursor to the position of the new extent
2688 * we are about to insert as we can't trust it after
2689 * the previous insert.
2691 if ((error
= xfs_bmbt_lookup_eq(cur
, new->br_startoff
,
2692 new->br_startblock
, new->br_blockcount
,
2695 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
2696 /* new middle extent - newext */
2697 cur
->bc_rec
.b
.br_state
= new->br_state
;
2698 if ((error
= xfs_btree_insert(cur
, &i
)))
2700 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2704 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2705 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2706 case BMAP_LEFT_FILLING
| BMAP_RIGHT_CONTIG
:
2707 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
2708 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2709 case BMAP_LEFT_CONTIG
:
2710 case BMAP_RIGHT_CONTIG
:
2712 * These cases are all impossible.
2717 /* update reverse mappings */
2718 error
= xfs_rmap_convert_extent(mp
, dfops
, ip
, XFS_DATA_FORK
, new);
2722 /* convert to a btree if necessary */
2723 if (xfs_bmap_needs_btree(ip
, XFS_DATA_FORK
)) {
2724 int tmp_logflags
; /* partial log flag return val */
2726 ASSERT(cur
== NULL
);
2727 error
= xfs_bmap_extents_to_btree(tp
, ip
, first
, dfops
, &cur
,
2728 0, &tmp_logflags
, XFS_DATA_FORK
);
2729 *logflagsp
|= tmp_logflags
;
2734 /* clear out the allocated field, done with it now in any case. */
2736 cur
->bc_private
.b
.allocated
= 0;
2740 xfs_bmap_check_leaf_extents(*curp
, ip
, XFS_DATA_FORK
);
2750 * Convert a hole to a delayed allocation.
2753 xfs_bmap_add_extent_hole_delay(
2754 xfs_inode_t
*ip
, /* incore inode pointer */
2755 xfs_extnum_t
*idx
, /* extent number to update/insert */
2756 xfs_bmbt_irec_t
*new) /* new data to add to file extents */
2758 xfs_ifork_t
*ifp
; /* inode fork pointer */
2759 xfs_bmbt_irec_t left
; /* left neighbor extent entry */
2760 xfs_filblks_t newlen
=0; /* new indirect size */
2761 xfs_filblks_t oldlen
=0; /* old indirect size */
2762 xfs_bmbt_irec_t right
; /* right neighbor extent entry */
2763 int state
; /* state bits, accessed thru macros */
2764 xfs_filblks_t temp
=0; /* temp for indirect calculations */
2766 ifp
= XFS_IFORK_PTR(ip
, XFS_DATA_FORK
);
2768 ASSERT(isnullstartblock(new->br_startblock
));
2771 * Check and set flags if this segment has a left neighbor
2774 state
|= BMAP_LEFT_VALID
;
2775 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, *idx
- 1), &left
);
2777 if (isnullstartblock(left
.br_startblock
))
2778 state
|= BMAP_LEFT_DELAY
;
2782 * Check and set flags if the current (right) segment exists.
2783 * If it doesn't exist, we're converting the hole at end-of-file.
2785 if (*idx
< ip
->i_df
.if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
)) {
2786 state
|= BMAP_RIGHT_VALID
;
2787 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, *idx
), &right
);
2789 if (isnullstartblock(right
.br_startblock
))
2790 state
|= BMAP_RIGHT_DELAY
;
2794 * Set contiguity flags on the left and right neighbors.
2795 * Don't let extents get too large, even if the pieces are contiguous.
2797 if ((state
& BMAP_LEFT_VALID
) && (state
& BMAP_LEFT_DELAY
) &&
2798 left
.br_startoff
+ left
.br_blockcount
== new->br_startoff
&&
2799 left
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
2800 state
|= BMAP_LEFT_CONTIG
;
2802 if ((state
& BMAP_RIGHT_VALID
) && (state
& BMAP_RIGHT_DELAY
) &&
2803 new->br_startoff
+ new->br_blockcount
== right
.br_startoff
&&
2804 new->br_blockcount
+ right
.br_blockcount
<= MAXEXTLEN
&&
2805 (!(state
& BMAP_LEFT_CONTIG
) ||
2806 (left
.br_blockcount
+ new->br_blockcount
+
2807 right
.br_blockcount
<= MAXEXTLEN
)))
2808 state
|= BMAP_RIGHT_CONTIG
;
2811 * Switch out based on the contiguity flags.
2813 switch (state
& (BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
)) {
2814 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2816 * New allocation is contiguous with delayed allocations
2817 * on the left and on the right.
2818 * Merge all three into a single extent record.
2821 temp
= left
.br_blockcount
+ new->br_blockcount
+
2822 right
.br_blockcount
;
2824 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2825 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
), temp
);
2826 oldlen
= startblockval(left
.br_startblock
) +
2827 startblockval(new->br_startblock
) +
2828 startblockval(right
.br_startblock
);
2829 newlen
= xfs_bmap_worst_indlen(ip
, temp
);
2830 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp
, *idx
),
2831 nullstartblock((int)newlen
));
2832 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2834 xfs_iext_remove(ip
, *idx
+ 1, 1, state
);
2837 case BMAP_LEFT_CONTIG
:
2839 * New allocation is contiguous with a delayed allocation
2841 * Merge the new allocation with the left neighbor.
2844 temp
= left
.br_blockcount
+ new->br_blockcount
;
2846 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2847 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
), temp
);
2848 oldlen
= startblockval(left
.br_startblock
) +
2849 startblockval(new->br_startblock
);
2850 newlen
= xfs_bmap_worst_indlen(ip
, temp
);
2851 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp
, *idx
),
2852 nullstartblock((int)newlen
));
2853 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2856 case BMAP_RIGHT_CONTIG
:
2858 * New allocation is contiguous with a delayed allocation
2860 * Merge the new allocation with the right neighbor.
2862 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2863 temp
= new->br_blockcount
+ right
.br_blockcount
;
2864 oldlen
= startblockval(new->br_startblock
) +
2865 startblockval(right
.br_startblock
);
2866 newlen
= xfs_bmap_worst_indlen(ip
, temp
);
2867 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp
, *idx
),
2869 nullstartblock((int)newlen
), temp
, right
.br_state
);
2870 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2875 * New allocation is not contiguous with another
2876 * delayed allocation.
2877 * Insert a new entry.
2879 oldlen
= newlen
= 0;
2880 xfs_iext_insert(ip
, *idx
, 1, new, state
);
2883 if (oldlen
!= newlen
) {
2884 ASSERT(oldlen
> newlen
);
2885 xfs_mod_fdblocks(ip
->i_mount
, (int64_t)(oldlen
- newlen
),
2888 * Nothing to do for disk quota accounting here.
2894 * Convert a hole to a real allocation.
2896 STATIC
int /* error */
2897 xfs_bmap_add_extent_hole_real(
2898 struct xfs_bmalloca
*bma
,
2901 struct xfs_bmbt_irec
*new = &bma
->got
;
2902 int error
; /* error return value */
2903 int i
; /* temp state */
2904 xfs_ifork_t
*ifp
; /* inode fork pointer */
2905 xfs_bmbt_irec_t left
; /* left neighbor extent entry */
2906 xfs_bmbt_irec_t right
; /* right neighbor extent entry */
2907 int rval
=0; /* return value (logging flags) */
2908 int state
; /* state bits, accessed thru macros */
2909 struct xfs_mount
*mp
;
2911 mp
= bma
->ip
->i_mount
;
2912 ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
2914 ASSERT(bma
->idx
>= 0);
2915 ASSERT(bma
->idx
<= ifp
->if_bytes
/ sizeof(struct xfs_bmbt_rec
));
2916 ASSERT(!isnullstartblock(new->br_startblock
));
2918 !(bma
->cur
->bc_private
.b
.flags
& XFS_BTCUR_BPRV_WASDEL
));
2920 XFS_STATS_INC(mp
, xs_add_exlist
);
2923 if (whichfork
== XFS_ATTR_FORK
)
2924 state
|= BMAP_ATTRFORK
;
2927 * Check and set flags if this segment has a left neighbor.
2930 state
|= BMAP_LEFT_VALID
;
2931 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
- 1), &left
);
2932 if (isnullstartblock(left
.br_startblock
))
2933 state
|= BMAP_LEFT_DELAY
;
2937 * Check and set flags if this segment has a current value.
2938 * Not true if we're inserting into the "hole" at eof.
2940 if (bma
->idx
< ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
)) {
2941 state
|= BMAP_RIGHT_VALID
;
2942 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
), &right
);
2943 if (isnullstartblock(right
.br_startblock
))
2944 state
|= BMAP_RIGHT_DELAY
;
2948 * We're inserting a real allocation between "left" and "right".
2949 * Set the contiguity flags. Don't let extents get too large.
2951 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
2952 left
.br_startoff
+ left
.br_blockcount
== new->br_startoff
&&
2953 left
.br_startblock
+ left
.br_blockcount
== new->br_startblock
&&
2954 left
.br_state
== new->br_state
&&
2955 left
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
2956 state
|= BMAP_LEFT_CONTIG
;
2958 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
2959 new->br_startoff
+ new->br_blockcount
== right
.br_startoff
&&
2960 new->br_startblock
+ new->br_blockcount
== right
.br_startblock
&&
2961 new->br_state
== right
.br_state
&&
2962 new->br_blockcount
+ right
.br_blockcount
<= MAXEXTLEN
&&
2963 (!(state
& BMAP_LEFT_CONTIG
) ||
2964 left
.br_blockcount
+ new->br_blockcount
+
2965 right
.br_blockcount
<= MAXEXTLEN
))
2966 state
|= BMAP_RIGHT_CONTIG
;
2970 * Select which case we're in here, and implement it.
2972 switch (state
& (BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
)) {
2973 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2975 * New allocation is contiguous with real allocations on the
2976 * left and on the right.
2977 * Merge all three into a single extent record.
2980 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2981 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, bma
->idx
),
2982 left
.br_blockcount
+ new->br_blockcount
+
2983 right
.br_blockcount
);
2984 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2986 xfs_iext_remove(bma
->ip
, bma
->idx
+ 1, 1, state
);
2988 XFS_IFORK_NEXT_SET(bma
->ip
, whichfork
,
2989 XFS_IFORK_NEXTENTS(bma
->ip
, whichfork
) - 1);
2990 if (bma
->cur
== NULL
) {
2991 rval
= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
2993 rval
= XFS_ILOG_CORE
;
2994 error
= xfs_bmbt_lookup_eq(bma
->cur
, right
.br_startoff
,
2995 right
.br_startblock
, right
.br_blockcount
,
2999 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
3000 error
= xfs_btree_delete(bma
->cur
, &i
);
3003 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
3004 error
= xfs_btree_decrement(bma
->cur
, 0, &i
);
3007 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
3008 error
= xfs_bmbt_update(bma
->cur
, left
.br_startoff
,
3010 left
.br_blockcount
+
3011 new->br_blockcount
+
3012 right
.br_blockcount
,
3019 case BMAP_LEFT_CONTIG
:
3021 * New allocation is contiguous with a real allocation
3023 * Merge the new allocation with the left neighbor.
3026 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
3027 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, bma
->idx
),
3028 left
.br_blockcount
+ new->br_blockcount
);
3029 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
3031 if (bma
->cur
== NULL
) {
3032 rval
= xfs_ilog_fext(whichfork
);
3035 error
= xfs_bmbt_lookup_eq(bma
->cur
, left
.br_startoff
,
3036 left
.br_startblock
, left
.br_blockcount
,
3040 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
3041 error
= xfs_bmbt_update(bma
->cur
, left
.br_startoff
,
3043 left
.br_blockcount
+
3051 case BMAP_RIGHT_CONTIG
:
3053 * New allocation is contiguous with a real allocation
3055 * Merge the new allocation with the right neighbor.
3057 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
3058 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp
, bma
->idx
),
3059 new->br_startoff
, new->br_startblock
,
3060 new->br_blockcount
+ right
.br_blockcount
,
3062 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
3064 if (bma
->cur
== NULL
) {
3065 rval
= xfs_ilog_fext(whichfork
);
3068 error
= xfs_bmbt_lookup_eq(bma
->cur
,
3070 right
.br_startblock
,
3071 right
.br_blockcount
, &i
);
3074 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
3075 error
= xfs_bmbt_update(bma
->cur
, new->br_startoff
,
3077 new->br_blockcount
+
3078 right
.br_blockcount
,
3087 * New allocation is not contiguous with another
3089 * Insert a new entry.
3091 xfs_iext_insert(bma
->ip
, bma
->idx
, 1, new, state
);
3092 XFS_IFORK_NEXT_SET(bma
->ip
, whichfork
,
3093 XFS_IFORK_NEXTENTS(bma
->ip
, whichfork
) + 1);
3094 if (bma
->cur
== NULL
) {
3095 rval
= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
3097 rval
= XFS_ILOG_CORE
;
3098 error
= xfs_bmbt_lookup_eq(bma
->cur
,
3101 new->br_blockcount
, &i
);
3104 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
3105 bma
->cur
->bc_rec
.b
.br_state
= new->br_state
;
3106 error
= xfs_btree_insert(bma
->cur
, &i
);
3109 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
3114 /* add reverse mapping */
3115 error
= xfs_rmap_map_extent(mp
, bma
->dfops
, bma
->ip
, whichfork
, new);
3119 /* convert to a btree if necessary */
3120 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
3121 int tmp_logflags
; /* partial log flag return val */
3123 ASSERT(bma
->cur
== NULL
);
3124 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
3125 bma
->firstblock
, bma
->dfops
, &bma
->cur
,
3126 0, &tmp_logflags
, whichfork
);
3127 bma
->logflags
|= tmp_logflags
;
3132 /* clear out the allocated field, done with it now in any case. */
3134 bma
->cur
->bc_private
.b
.allocated
= 0;
3136 xfs_bmap_check_leaf_extents(bma
->cur
, bma
->ip
, whichfork
);
3138 bma
->logflags
|= rval
;
3143 * Functions used in the extent read, allocate and remove paths
3147 * Adjust the size of the new extent based on di_extsize and rt extsize.
3150 xfs_bmap_extsize_align(
3152 xfs_bmbt_irec_t
*gotp
, /* next extent pointer */
3153 xfs_bmbt_irec_t
*prevp
, /* previous extent pointer */
3154 xfs_extlen_t extsz
, /* align to this extent size */
3155 int rt
, /* is this a realtime inode? */
3156 int eof
, /* is extent at end-of-file? */
3157 int delay
, /* creating delalloc extent? */
3158 int convert
, /* overwriting unwritten extent? */
3159 xfs_fileoff_t
*offp
, /* in/out: aligned offset */
3160 xfs_extlen_t
*lenp
) /* in/out: aligned length */
3162 xfs_fileoff_t orig_off
; /* original offset */
3163 xfs_extlen_t orig_alen
; /* original length */
3164 xfs_fileoff_t orig_end
; /* original off+len */
3165 xfs_fileoff_t nexto
; /* next file offset */
3166 xfs_fileoff_t prevo
; /* previous file offset */
3167 xfs_fileoff_t align_off
; /* temp for offset */
3168 xfs_extlen_t align_alen
; /* temp for length */
3169 xfs_extlen_t temp
; /* temp for calculations */
3174 orig_off
= align_off
= *offp
;
3175 orig_alen
= align_alen
= *lenp
;
3176 orig_end
= orig_off
+ orig_alen
;
3179 * If this request overlaps an existing extent, then don't
3180 * attempt to perform any additional alignment.
3182 if (!delay
&& !eof
&&
3183 (orig_off
>= gotp
->br_startoff
) &&
3184 (orig_end
<= gotp
->br_startoff
+ gotp
->br_blockcount
)) {
3189 * If the file offset is unaligned vs. the extent size
3190 * we need to align it. This will be possible unless
3191 * the file was previously written with a kernel that didn't
3192 * perform this alignment, or if a truncate shot us in the
3195 temp
= do_mod(orig_off
, extsz
);
3201 /* Same adjustment for the end of the requested area. */
3202 temp
= (align_alen
% extsz
);
3204 align_alen
+= extsz
- temp
;
3207 * For large extent hint sizes, the aligned extent might be larger than
3208 * MAXEXTLEN. In that case, reduce the size by an extsz so that it pulls
3209 * the length back under MAXEXTLEN. The outer allocation loops handle
3210 * short allocation just fine, so it is safe to do this. We only want to
3211 * do it when we are forced to, though, because it means more allocation
3212 * operations are required.
3214 while (align_alen
> MAXEXTLEN
)
3215 align_alen
-= extsz
;
3216 ASSERT(align_alen
<= MAXEXTLEN
);
3219 * If the previous block overlaps with this proposed allocation
3220 * then move the start forward without adjusting the length.
3222 if (prevp
->br_startoff
!= NULLFILEOFF
) {
3223 if (prevp
->br_startblock
== HOLESTARTBLOCK
)
3224 prevo
= prevp
->br_startoff
;
3226 prevo
= prevp
->br_startoff
+ prevp
->br_blockcount
;
3229 if (align_off
!= orig_off
&& align_off
< prevo
)
3232 * If the next block overlaps with this proposed allocation
3233 * then move the start back without adjusting the length,
3234 * but not before offset 0.
3235 * This may of course make the start overlap previous block,
3236 * and if we hit the offset 0 limit then the next block
3237 * can still overlap too.
3239 if (!eof
&& gotp
->br_startoff
!= NULLFILEOFF
) {
3240 if ((delay
&& gotp
->br_startblock
== HOLESTARTBLOCK
) ||
3241 (!delay
&& gotp
->br_startblock
== DELAYSTARTBLOCK
))
3242 nexto
= gotp
->br_startoff
+ gotp
->br_blockcount
;
3244 nexto
= gotp
->br_startoff
;
3246 nexto
= NULLFILEOFF
;
3248 align_off
+ align_alen
!= orig_end
&&
3249 align_off
+ align_alen
> nexto
)
3250 align_off
= nexto
> align_alen
? nexto
- align_alen
: 0;
3252 * If we're now overlapping the next or previous extent that
3253 * means we can't fit an extsz piece in this hole. Just move
3254 * the start forward to the first valid spot and set
3255 * the length so we hit the end.
3257 if (align_off
!= orig_off
&& align_off
< prevo
)
3259 if (align_off
+ align_alen
!= orig_end
&&
3260 align_off
+ align_alen
> nexto
&&
3261 nexto
!= NULLFILEOFF
) {
3262 ASSERT(nexto
> prevo
);
3263 align_alen
= nexto
- align_off
;
3267 * If realtime, and the result isn't a multiple of the realtime
3268 * extent size we need to remove blocks until it is.
3270 if (rt
&& (temp
= (align_alen
% mp
->m_sb
.sb_rextsize
))) {
3272 * We're not covering the original request, or
3273 * we won't be able to once we fix the length.
3275 if (orig_off
< align_off
||
3276 orig_end
> align_off
+ align_alen
||
3277 align_alen
- temp
< orig_alen
)
3280 * Try to fix it by moving the start up.
3282 if (align_off
+ temp
<= orig_off
) {
3287 * Try to fix it by moving the end in.
3289 else if (align_off
+ align_alen
- temp
>= orig_end
)
3292 * Set the start to the minimum then trim the length.
3295 align_alen
-= orig_off
- align_off
;
3296 align_off
= orig_off
;
3297 align_alen
-= align_alen
% mp
->m_sb
.sb_rextsize
;
3300 * Result doesn't cover the request, fail it.
3302 if (orig_off
< align_off
|| orig_end
> align_off
+ align_alen
)
3305 ASSERT(orig_off
>= align_off
);
3306 /* see MAXEXTLEN handling above */
3307 ASSERT(orig_end
<= align_off
+ align_alen
||
3308 align_alen
+ extsz
> MAXEXTLEN
);
3312 if (!eof
&& gotp
->br_startoff
!= NULLFILEOFF
)
3313 ASSERT(align_off
+ align_alen
<= gotp
->br_startoff
);
3314 if (prevp
->br_startoff
!= NULLFILEOFF
)
3315 ASSERT(align_off
>= prevp
->br_startoff
+ prevp
->br_blockcount
);
3323 #define XFS_ALLOC_GAP_UNITS 4
3327 struct xfs_bmalloca
*ap
) /* bmap alloc argument struct */
3329 xfs_fsblock_t adjust
; /* adjustment to block numbers */
3330 xfs_agnumber_t fb_agno
; /* ag number of ap->firstblock */
3331 xfs_mount_t
*mp
; /* mount point structure */
3332 int nullfb
; /* true if ap->firstblock isn't set */
3333 int rt
; /* true if inode is realtime */
3335 #define ISVALID(x,y) \
3337 (x) < mp->m_sb.sb_rblocks : \
3338 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
3339 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
3340 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
3342 mp
= ap
->ip
->i_mount
;
3343 nullfb
= *ap
->firstblock
== NULLFSBLOCK
;
3344 rt
= XFS_IS_REALTIME_INODE(ap
->ip
) &&
3345 xfs_alloc_is_userdata(ap
->datatype
);
3346 fb_agno
= nullfb
? NULLAGNUMBER
: XFS_FSB_TO_AGNO(mp
, *ap
->firstblock
);
3348 * If allocating at eof, and there's a previous real block,
3349 * try to use its last block as our starting point.
3351 if (ap
->eof
&& ap
->prev
.br_startoff
!= NULLFILEOFF
&&
3352 !isnullstartblock(ap
->prev
.br_startblock
) &&
3353 ISVALID(ap
->prev
.br_startblock
+ ap
->prev
.br_blockcount
,
3354 ap
->prev
.br_startblock
)) {
3355 ap
->blkno
= ap
->prev
.br_startblock
+ ap
->prev
.br_blockcount
;
3357 * Adjust for the gap between prevp and us.
3359 adjust
= ap
->offset
-
3360 (ap
->prev
.br_startoff
+ ap
->prev
.br_blockcount
);
3362 ISVALID(ap
->blkno
+ adjust
, ap
->prev
.br_startblock
))
3363 ap
->blkno
+= adjust
;
3366 * If not at eof, then compare the two neighbor blocks.
3367 * Figure out whether either one gives us a good starting point,
3368 * and pick the better one.
3370 else if (!ap
->eof
) {
3371 xfs_fsblock_t gotbno
; /* right side block number */
3372 xfs_fsblock_t gotdiff
=0; /* right side difference */
3373 xfs_fsblock_t prevbno
; /* left side block number */
3374 xfs_fsblock_t prevdiff
=0; /* left side difference */
3377 * If there's a previous (left) block, select a requested
3378 * start block based on it.
3380 if (ap
->prev
.br_startoff
!= NULLFILEOFF
&&
3381 !isnullstartblock(ap
->prev
.br_startblock
) &&
3382 (prevbno
= ap
->prev
.br_startblock
+
3383 ap
->prev
.br_blockcount
) &&
3384 ISVALID(prevbno
, ap
->prev
.br_startblock
)) {
3386 * Calculate gap to end of previous block.
3388 adjust
= prevdiff
= ap
->offset
-
3389 (ap
->prev
.br_startoff
+
3390 ap
->prev
.br_blockcount
);
3392 * Figure the startblock based on the previous block's
3393 * end and the gap size.
3395 * If the gap is large relative to the piece we're
3396 * allocating, or using it gives us an invalid block
3397 * number, then just use the end of the previous block.
3399 if (prevdiff
<= XFS_ALLOC_GAP_UNITS
* ap
->length
&&
3400 ISVALID(prevbno
+ prevdiff
,
3401 ap
->prev
.br_startblock
))
3406 * If the firstblock forbids it, can't use it,
3409 if (!rt
&& !nullfb
&&
3410 XFS_FSB_TO_AGNO(mp
, prevbno
) != fb_agno
)
3411 prevbno
= NULLFSBLOCK
;
3414 * No previous block or can't follow it, just default.
3417 prevbno
= NULLFSBLOCK
;
3419 * If there's a following (right) block, select a requested
3420 * start block based on it.
3422 if (!isnullstartblock(ap
->got
.br_startblock
)) {
3424 * Calculate gap to start of next block.
3426 adjust
= gotdiff
= ap
->got
.br_startoff
- ap
->offset
;
3428 * Figure the startblock based on the next block's
3429 * start and the gap size.
3431 gotbno
= ap
->got
.br_startblock
;
3434 * If the gap is large relative to the piece we're
3435 * allocating, or using it gives us an invalid block
3436 * number, then just use the start of the next block
3437 * offset by our length.
3439 if (gotdiff
<= XFS_ALLOC_GAP_UNITS
* ap
->length
&&
3440 ISVALID(gotbno
- gotdiff
, gotbno
))
3442 else if (ISVALID(gotbno
- ap
->length
, gotbno
)) {
3443 gotbno
-= ap
->length
;
3444 gotdiff
+= adjust
- ap
->length
;
3448 * If the firstblock forbids it, can't use it,
3451 if (!rt
&& !nullfb
&&
3452 XFS_FSB_TO_AGNO(mp
, gotbno
) != fb_agno
)
3453 gotbno
= NULLFSBLOCK
;
3456 * No next block, just default.
3459 gotbno
= NULLFSBLOCK
;
3461 * If both valid, pick the better one, else the only good
3462 * one, else ap->blkno is already set (to 0 or the inode block).
3464 if (prevbno
!= NULLFSBLOCK
&& gotbno
!= NULLFSBLOCK
)
3465 ap
->blkno
= prevdiff
<= gotdiff
? prevbno
: gotbno
;
3466 else if (prevbno
!= NULLFSBLOCK
)
3467 ap
->blkno
= prevbno
;
3468 else if (gotbno
!= NULLFSBLOCK
)
3475 xfs_bmap_longest_free_extent(
3476 struct xfs_trans
*tp
,
3481 struct xfs_mount
*mp
= tp
->t_mountp
;
3482 struct xfs_perag
*pag
;
3483 xfs_extlen_t longest
;
3486 pag
= xfs_perag_get(mp
, ag
);
3487 if (!pag
->pagf_init
) {
3488 error
= xfs_alloc_pagf_init(mp
, tp
, ag
, XFS_ALLOC_FLAG_TRYLOCK
);
3492 if (!pag
->pagf_init
) {
3498 longest
= xfs_alloc_longest_free_extent(mp
, pag
,
3499 xfs_alloc_min_freelist(mp
, pag
),
3500 xfs_ag_resv_needed(pag
, XFS_AG_RESV_NONE
));
3501 if (*blen
< longest
)
3510 xfs_bmap_select_minlen(
3511 struct xfs_bmalloca
*ap
,
3512 struct xfs_alloc_arg
*args
,
3516 if (notinit
|| *blen
< ap
->minlen
) {
3518 * Since we did a BUF_TRYLOCK above, it is possible that
3519 * there is space for this request.
3521 args
->minlen
= ap
->minlen
;
3522 } else if (*blen
< args
->maxlen
) {
3524 * If the best seen length is less than the request length,
3525 * use the best as the minimum.
3527 args
->minlen
= *blen
;
3530 * Otherwise we've seen an extent as big as maxlen, use that
3533 args
->minlen
= args
->maxlen
;
3538 xfs_bmap_btalloc_nullfb(
3539 struct xfs_bmalloca
*ap
,
3540 struct xfs_alloc_arg
*args
,
3543 struct xfs_mount
*mp
= ap
->ip
->i_mount
;
3544 xfs_agnumber_t ag
, startag
;
3548 args
->type
= XFS_ALLOCTYPE_START_BNO
;
3549 args
->total
= ap
->total
;
3551 startag
= ag
= XFS_FSB_TO_AGNO(mp
, args
->fsbno
);
3552 if (startag
== NULLAGNUMBER
)
3555 while (*blen
< args
->maxlen
) {
3556 error
= xfs_bmap_longest_free_extent(args
->tp
, ag
, blen
,
3561 if (++ag
== mp
->m_sb
.sb_agcount
)
3567 xfs_bmap_select_minlen(ap
, args
, blen
, notinit
);
3572 xfs_bmap_btalloc_filestreams(
3573 struct xfs_bmalloca
*ap
,
3574 struct xfs_alloc_arg
*args
,
3577 struct xfs_mount
*mp
= ap
->ip
->i_mount
;
3582 args
->type
= XFS_ALLOCTYPE_NEAR_BNO
;
3583 args
->total
= ap
->total
;
3585 ag
= XFS_FSB_TO_AGNO(mp
, args
->fsbno
);
3586 if (ag
== NULLAGNUMBER
)
3589 error
= xfs_bmap_longest_free_extent(args
->tp
, ag
, blen
, ¬init
);
3593 if (*blen
< args
->maxlen
) {
3594 error
= xfs_filestream_new_ag(ap
, &ag
);
3598 error
= xfs_bmap_longest_free_extent(args
->tp
, ag
, blen
,
3605 xfs_bmap_select_minlen(ap
, args
, blen
, notinit
);
3608 * Set the failure fallback case to look in the selected AG as stream
3611 ap
->blkno
= args
->fsbno
= XFS_AGB_TO_FSB(mp
, ag
, 0);
3617 struct xfs_bmalloca
*ap
) /* bmap alloc argument struct */
3619 xfs_mount_t
*mp
; /* mount point structure */
3620 xfs_alloctype_t atype
= 0; /* type for allocation routines */
3621 xfs_extlen_t align
= 0; /* minimum allocation alignment */
3622 xfs_agnumber_t fb_agno
; /* ag number of ap->firstblock */
3624 xfs_alloc_arg_t args
;
3626 xfs_extlen_t nextminlen
= 0;
3627 int nullfb
; /* true if ap->firstblock isn't set */
3635 mp
= ap
->ip
->i_mount
;
3637 /* stripe alignment for allocation is determined by mount parameters */
3639 if (mp
->m_swidth
&& (mp
->m_flags
& XFS_MOUNT_SWALLOC
))
3640 stripe_align
= mp
->m_swidth
;
3641 else if (mp
->m_dalign
)
3642 stripe_align
= mp
->m_dalign
;
3644 if (xfs_alloc_is_userdata(ap
->datatype
))
3645 align
= xfs_get_extsz_hint(ap
->ip
);
3646 if (unlikely(align
)) {
3647 error
= xfs_bmap_extsize_align(mp
, &ap
->got
, &ap
->prev
,
3648 align
, 0, ap
->eof
, 0, ap
->conv
,
3649 &ap
->offset
, &ap
->length
);
3655 nullfb
= *ap
->firstblock
== NULLFSBLOCK
;
3656 fb_agno
= nullfb
? NULLAGNUMBER
: XFS_FSB_TO_AGNO(mp
, *ap
->firstblock
);
3658 if (xfs_alloc_is_userdata(ap
->datatype
) &&
3659 xfs_inode_is_filestream(ap
->ip
)) {
3660 ag
= xfs_filestream_lookup_ag(ap
->ip
);
3661 ag
= (ag
!= NULLAGNUMBER
) ? ag
: 0;
3662 ap
->blkno
= XFS_AGB_TO_FSB(mp
, ag
, 0);
3664 ap
->blkno
= XFS_INO_TO_FSB(mp
, ap
->ip
->i_ino
);
3667 ap
->blkno
= *ap
->firstblock
;
3669 xfs_bmap_adjacent(ap
);
3672 * If allowed, use ap->blkno; otherwise must use firstblock since
3673 * it's in the right allocation group.
3675 if (nullfb
|| XFS_FSB_TO_AGNO(mp
, ap
->blkno
) == fb_agno
)
3678 ap
->blkno
= *ap
->firstblock
;
3680 * Normal allocation, done through xfs_alloc_vextent.
3682 tryagain
= isaligned
= 0;
3683 memset(&args
, 0, sizeof(args
));
3686 args
.fsbno
= ap
->blkno
;
3687 xfs_rmap_skip_owner_update(&args
.oinfo
);
3689 /* Trim the allocation back to the maximum an AG can fit. */
3690 args
.maxlen
= MIN(ap
->length
, mp
->m_ag_max_usable
);
3691 args
.firstblock
= *ap
->firstblock
;
3695 * Search for an allocation group with a single extent large
3696 * enough for the request. If one isn't found, then adjust
3697 * the minimum allocation size to the largest space found.
3699 if (xfs_alloc_is_userdata(ap
->datatype
) &&
3700 xfs_inode_is_filestream(ap
->ip
))
3701 error
= xfs_bmap_btalloc_filestreams(ap
, &args
, &blen
);
3703 error
= xfs_bmap_btalloc_nullfb(ap
, &args
, &blen
);
3706 } else if (ap
->dfops
->dop_low
) {
3707 if (xfs_inode_is_filestream(ap
->ip
))
3708 args
.type
= XFS_ALLOCTYPE_FIRST_AG
;
3710 args
.type
= XFS_ALLOCTYPE_START_BNO
;
3711 args
.total
= args
.minlen
= ap
->minlen
;
3713 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
3714 args
.total
= ap
->total
;
3715 args
.minlen
= ap
->minlen
;
3717 /* apply extent size hints if obtained earlier */
3718 if (unlikely(align
)) {
3720 if ((args
.mod
= (xfs_extlen_t
)do_mod(ap
->offset
, args
.prod
)))
3721 args
.mod
= (xfs_extlen_t
)(args
.prod
- args
.mod
);
3722 } else if (mp
->m_sb
.sb_blocksize
>= PAGE_SIZE
) {
3726 args
.prod
= PAGE_SIZE
>> mp
->m_sb
.sb_blocklog
;
3727 if ((args
.mod
= (xfs_extlen_t
)(do_mod(ap
->offset
, args
.prod
))))
3728 args
.mod
= (xfs_extlen_t
)(args
.prod
- args
.mod
);
3731 * If we are not low on available data blocks, and the
3732 * underlying logical volume manager is a stripe, and
3733 * the file offset is zero then try to allocate data
3734 * blocks on stripe unit boundary.
3735 * NOTE: ap->aeof is only set if the allocation length
3736 * is >= the stripe unit and the allocation offset is
3737 * at the end of file.
3739 if (!ap
->dfops
->dop_low
&& ap
->aeof
) {
3741 args
.alignment
= stripe_align
;
3745 * Adjust for alignment
3747 if (blen
> args
.alignment
&& blen
<= args
.maxlen
)
3748 args
.minlen
= blen
- args
.alignment
;
3749 args
.minalignslop
= 0;
3752 * First try an exact bno allocation.
3753 * If it fails then do a near or start bno
3754 * allocation with alignment turned on.
3758 args
.type
= XFS_ALLOCTYPE_THIS_BNO
;
3761 * Compute the minlen+alignment for the
3762 * next case. Set slop so that the value
3763 * of minlen+alignment+slop doesn't go up
3764 * between the calls.
3766 if (blen
> stripe_align
&& blen
<= args
.maxlen
)
3767 nextminlen
= blen
- stripe_align
;
3769 nextminlen
= args
.minlen
;
3770 if (nextminlen
+ stripe_align
> args
.minlen
+ 1)
3772 nextminlen
+ stripe_align
-
3775 args
.minalignslop
= 0;
3779 args
.minalignslop
= 0;
3781 args
.minleft
= ap
->minleft
;
3782 args
.wasdel
= ap
->wasdel
;
3783 args
.resv
= XFS_AG_RESV_NONE
;
3784 args
.datatype
= ap
->datatype
;
3785 if (ap
->datatype
& XFS_ALLOC_USERDATA_ZERO
)
3788 error
= xfs_alloc_vextent(&args
);
3792 if (tryagain
&& args
.fsbno
== NULLFSBLOCK
) {
3794 * Exact allocation failed. Now try with alignment
3798 args
.fsbno
= ap
->blkno
;
3799 args
.alignment
= stripe_align
;
3800 args
.minlen
= nextminlen
;
3801 args
.minalignslop
= 0;
3803 if ((error
= xfs_alloc_vextent(&args
)))
3806 if (isaligned
&& args
.fsbno
== NULLFSBLOCK
) {
3808 * allocation failed, so turn off alignment and
3812 args
.fsbno
= ap
->blkno
;
3814 if ((error
= xfs_alloc_vextent(&args
)))
3817 if (args
.fsbno
== NULLFSBLOCK
&& nullfb
&&
3818 args
.minlen
> ap
->minlen
) {
3819 args
.minlen
= ap
->minlen
;
3820 args
.type
= XFS_ALLOCTYPE_START_BNO
;
3821 args
.fsbno
= ap
->blkno
;
3822 if ((error
= xfs_alloc_vextent(&args
)))
3825 if (args
.fsbno
== NULLFSBLOCK
&& nullfb
) {
3827 args
.type
= XFS_ALLOCTYPE_FIRST_AG
;
3828 args
.total
= ap
->minlen
;
3830 if ((error
= xfs_alloc_vextent(&args
)))
3832 ap
->dfops
->dop_low
= true;
3834 if (args
.fsbno
!= NULLFSBLOCK
) {
3836 * check the allocation happened at the same or higher AG than
3837 * the first block that was allocated.
3839 ASSERT(*ap
->firstblock
== NULLFSBLOCK
||
3840 XFS_FSB_TO_AGNO(mp
, *ap
->firstblock
) ==
3841 XFS_FSB_TO_AGNO(mp
, args
.fsbno
) ||
3842 (ap
->dfops
->dop_low
&&
3843 XFS_FSB_TO_AGNO(mp
, *ap
->firstblock
) <
3844 XFS_FSB_TO_AGNO(mp
, args
.fsbno
)));
3846 ap
->blkno
= args
.fsbno
;
3847 if (*ap
->firstblock
== NULLFSBLOCK
)
3848 *ap
->firstblock
= args
.fsbno
;
3849 ASSERT(nullfb
|| fb_agno
== args
.agno
||
3850 (ap
->dfops
->dop_low
&& fb_agno
< args
.agno
));
3851 ap
->length
= args
.len
;
3852 ap
->ip
->i_d
.di_nblocks
+= args
.len
;
3853 xfs_trans_log_inode(ap
->tp
, ap
->ip
, XFS_ILOG_CORE
);
3855 ap
->ip
->i_delayed_blks
-= args
.len
;
3857 * Adjust the disk quota also. This was reserved
3860 xfs_trans_mod_dquot_byino(ap
->tp
, ap
->ip
,
3861 ap
->wasdel
? XFS_TRANS_DQ_DELBCOUNT
:
3862 XFS_TRANS_DQ_BCOUNT
,
3865 ap
->blkno
= NULLFSBLOCK
;
3872 * For a remap operation, just "allocate" an extent at the address that the
3873 * caller passed in, and ensure that the AGFL is the right size. The caller
3874 * will then map the "allocated" extent into the file somewhere.
3877 xfs_bmap_remap_alloc(
3878 struct xfs_bmalloca
*ap
)
3880 struct xfs_trans
*tp
= ap
->tp
;
3881 struct xfs_mount
*mp
= tp
->t_mountp
;
3883 struct xfs_alloc_arg args
;
3887 * validate that the block number is legal - the enables us to detect
3888 * and handle a silent filesystem corruption rather than crashing.
3890 memset(&args
, 0, sizeof(struct xfs_alloc_arg
));
3892 args
.mp
= ap
->tp
->t_mountp
;
3893 bno
= *ap
->firstblock
;
3894 args
.agno
= XFS_FSB_TO_AGNO(mp
, bno
);
3895 args
.agbno
= XFS_FSB_TO_AGBNO(mp
, bno
);
3896 if (args
.agno
>= mp
->m_sb
.sb_agcount
||
3897 args
.agbno
>= mp
->m_sb
.sb_agblocks
)
3898 return -EFSCORRUPTED
;
3900 /* "Allocate" the extent from the range we passed in. */
3901 trace_xfs_bmap_remap_alloc(ap
->ip
, *ap
->firstblock
, ap
->length
);
3903 ap
->ip
->i_d
.di_nblocks
+= ap
->length
;
3904 xfs_trans_log_inode(ap
->tp
, ap
->ip
, XFS_ILOG_CORE
);
3906 /* Fix the freelist, like a real allocator does. */
3907 args
.datatype
= ap
->datatype
;
3908 args
.pag
= xfs_perag_get(args
.mp
, args
.agno
);
3912 * The freelist fixing code will decline the allocation if
3913 * the size and shape of the free space doesn't allow for
3914 * allocating the extent and updating all the metadata that
3915 * happens during an allocation. We're remapping, not
3916 * allocating, so skip that check by pretending to be freeing.
3918 error
= xfs_alloc_fix_freelist(&args
, XFS_ALLOC_FLAG_FREEING
);
3922 xfs_perag_put(args
.pag
);
3924 trace_xfs_bmap_remap_alloc_error(ap
->ip
, error
, _RET_IP_
);
3929 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
3930 * It figures out where to ask the underlying allocator to put the new extent.
3934 struct xfs_bmalloca
*ap
) /* bmap alloc argument struct */
3936 if (ap
->flags
& XFS_BMAPI_REMAP
)
3937 return xfs_bmap_remap_alloc(ap
);
3938 if (XFS_IS_REALTIME_INODE(ap
->ip
) &&
3939 xfs_alloc_is_userdata(ap
->datatype
))
3940 return xfs_bmap_rtalloc(ap
);
3941 return xfs_bmap_btalloc(ap
);
3945 * Trim the returned map to the required bounds
3949 struct xfs_bmbt_irec
*mval
,
3950 struct xfs_bmbt_irec
*got
,
3958 if ((flags
& XFS_BMAPI_ENTIRE
) ||
3959 got
->br_startoff
+ got
->br_blockcount
<= obno
) {
3961 if (isnullstartblock(got
->br_startblock
))
3962 mval
->br_startblock
= DELAYSTARTBLOCK
;
3968 ASSERT((*bno
>= obno
) || (n
== 0));
3970 mval
->br_startoff
= *bno
;
3971 if (isnullstartblock(got
->br_startblock
))
3972 mval
->br_startblock
= DELAYSTARTBLOCK
;
3974 mval
->br_startblock
= got
->br_startblock
+
3975 (*bno
- got
->br_startoff
);
3977 * Return the minimum of what we got and what we asked for for
3978 * the length. We can use the len variable here because it is
3979 * modified below and we could have been there before coming
3980 * here if the first part of the allocation didn't overlap what
3983 mval
->br_blockcount
= XFS_FILBLKS_MIN(end
- *bno
,
3984 got
->br_blockcount
- (*bno
- got
->br_startoff
));
3985 mval
->br_state
= got
->br_state
;
3986 ASSERT(mval
->br_blockcount
<= len
);
3991 * Update and validate the extent map to return
3994 xfs_bmapi_update_map(
3995 struct xfs_bmbt_irec
**map
,
4003 xfs_bmbt_irec_t
*mval
= *map
;
4005 ASSERT((flags
& XFS_BMAPI_ENTIRE
) ||
4006 ((mval
->br_startoff
+ mval
->br_blockcount
) <= end
));
4007 ASSERT((flags
& XFS_BMAPI_ENTIRE
) || (mval
->br_blockcount
<= *len
) ||
4008 (mval
->br_startoff
< obno
));
4010 *bno
= mval
->br_startoff
+ mval
->br_blockcount
;
4012 if (*n
> 0 && mval
->br_startoff
== mval
[-1].br_startoff
) {
4013 /* update previous map with new information */
4014 ASSERT(mval
->br_startblock
== mval
[-1].br_startblock
);
4015 ASSERT(mval
->br_blockcount
> mval
[-1].br_blockcount
);
4016 ASSERT(mval
->br_state
== mval
[-1].br_state
);
4017 mval
[-1].br_blockcount
= mval
->br_blockcount
;
4018 mval
[-1].br_state
= mval
->br_state
;
4019 } else if (*n
> 0 && mval
->br_startblock
!= DELAYSTARTBLOCK
&&
4020 mval
[-1].br_startblock
!= DELAYSTARTBLOCK
&&
4021 mval
[-1].br_startblock
!= HOLESTARTBLOCK
&&
4022 mval
->br_startblock
== mval
[-1].br_startblock
+
4023 mval
[-1].br_blockcount
&&
4024 ((flags
& XFS_BMAPI_IGSTATE
) ||
4025 mval
[-1].br_state
== mval
->br_state
)) {
4026 ASSERT(mval
->br_startoff
==
4027 mval
[-1].br_startoff
+ mval
[-1].br_blockcount
);
4028 mval
[-1].br_blockcount
+= mval
->br_blockcount
;
4029 } else if (*n
> 0 &&
4030 mval
->br_startblock
== DELAYSTARTBLOCK
&&
4031 mval
[-1].br_startblock
== DELAYSTARTBLOCK
&&
4032 mval
->br_startoff
==
4033 mval
[-1].br_startoff
+ mval
[-1].br_blockcount
) {
4034 mval
[-1].br_blockcount
+= mval
->br_blockcount
;
4035 mval
[-1].br_state
= mval
->br_state
;
4036 } else if (!((*n
== 0) &&
4037 ((mval
->br_startoff
+ mval
->br_blockcount
) <=
4046 * Map file blocks to filesystem blocks without allocation.
4050 struct xfs_inode
*ip
,
4053 struct xfs_bmbt_irec
*mval
,
4057 struct xfs_mount
*mp
= ip
->i_mount
;
4058 struct xfs_ifork
*ifp
;
4059 struct xfs_bmbt_irec got
;
4060 struct xfs_bmbt_irec prev
;
4067 int whichfork
= (flags
& XFS_BMAPI_ATTRFORK
) ?
4068 XFS_ATTR_FORK
: XFS_DATA_FORK
;
4071 ASSERT(!(flags
& ~(XFS_BMAPI_ATTRFORK
|XFS_BMAPI_ENTIRE
|
4072 XFS_BMAPI_IGSTATE
)));
4073 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_SHARED
|XFS_ILOCK_EXCL
));
4075 if (unlikely(XFS_TEST_ERROR(
4076 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
4077 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
4078 mp
, XFS_ERRTAG_BMAPIFORMAT
, XFS_RANDOM_BMAPIFORMAT
))) {
4079 XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW
, mp
);
4080 return -EFSCORRUPTED
;
4083 if (XFS_FORCED_SHUTDOWN(mp
))
4086 XFS_STATS_INC(mp
, xs_blk_mapr
);
4088 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4090 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
4091 error
= xfs_iread_extents(NULL
, ip
, whichfork
);
4096 xfs_bmap_search_extents(ip
, bno
, whichfork
, &eof
, &lastx
, &got
, &prev
);
4100 while (bno
< end
&& n
< *nmap
) {
4101 /* Reading past eof, act as though there's a hole up to end. */
4103 got
.br_startoff
= end
;
4104 if (got
.br_startoff
> bno
) {
4105 /* Reading in a hole. */
4106 mval
->br_startoff
= bno
;
4107 mval
->br_startblock
= HOLESTARTBLOCK
;
4108 mval
->br_blockcount
=
4109 XFS_FILBLKS_MIN(len
, got
.br_startoff
- bno
);
4110 mval
->br_state
= XFS_EXT_NORM
;
4111 bno
+= mval
->br_blockcount
;
4112 len
-= mval
->br_blockcount
;
4118 /* set up the extent map to return. */
4119 xfs_bmapi_trim_map(mval
, &got
, &bno
, len
, obno
, end
, n
, flags
);
4120 xfs_bmapi_update_map(&mval
, &bno
, &len
, obno
, end
, &n
, flags
);
4122 /* If we're done, stop now. */
4123 if (bno
>= end
|| n
>= *nmap
)
4126 /* Else go on to the next record. */
4127 if (++lastx
< ifp
->if_bytes
/ sizeof(xfs_bmbt_rec_t
))
4128 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, lastx
), &got
);
4137 xfs_bmapi_reserve_delalloc(
4138 struct xfs_inode
*ip
,
4141 struct xfs_bmbt_irec
*got
,
4142 struct xfs_bmbt_irec
*prev
,
4143 xfs_extnum_t
*lastx
,
4146 struct xfs_mount
*mp
= ip
->i_mount
;
4147 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, XFS_DATA_FORK
);
4149 xfs_extlen_t indlen
;
4150 char rt
= XFS_IS_REALTIME_INODE(ip
);
4154 alen
= XFS_FILBLKS_MIN(len
, MAXEXTLEN
);
4156 alen
= XFS_FILBLKS_MIN(alen
, got
->br_startoff
- aoff
);
4158 /* Figure out the extent size, adjust alen */
4159 extsz
= xfs_get_extsz_hint(ip
);
4161 error
= xfs_bmap_extsize_align(mp
, got
, prev
, extsz
, rt
, eof
,
4162 1, 0, &aoff
, &alen
);
4167 extsz
= alen
/ mp
->m_sb
.sb_rextsize
;
4170 * Make a transaction-less quota reservation for delayed allocation
4171 * blocks. This number gets adjusted later. We return if we haven't
4172 * allocated blocks already inside this loop.
4174 error
= xfs_trans_reserve_quota_nblks(NULL
, ip
, (long)alen
, 0,
4175 rt
? XFS_QMOPT_RES_RTBLKS
: XFS_QMOPT_RES_REGBLKS
);
4180 * Split changing sb for alen and indlen since they could be coming
4181 * from different places.
4183 indlen
= (xfs_extlen_t
)xfs_bmap_worst_indlen(ip
, alen
);
4187 error
= xfs_mod_frextents(mp
, -((int64_t)extsz
));
4189 error
= xfs_mod_fdblocks(mp
, -((int64_t)alen
), false);
4193 goto out_unreserve_quota
;
4195 error
= xfs_mod_fdblocks(mp
, -((int64_t)indlen
), false);
4197 goto out_unreserve_blocks
;
4200 ip
->i_delayed_blks
+= alen
;
4202 got
->br_startoff
= aoff
;
4203 got
->br_startblock
= nullstartblock(indlen
);
4204 got
->br_blockcount
= alen
;
4205 got
->br_state
= XFS_EXT_NORM
;
4206 xfs_bmap_add_extent_hole_delay(ip
, lastx
, got
);
4209 * Update our extent pointer, given that xfs_bmap_add_extent_hole_delay
4210 * might have merged it into one of the neighbouring ones.
4212 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, *lastx
), got
);
4214 ASSERT(got
->br_startoff
<= aoff
);
4215 ASSERT(got
->br_startoff
+ got
->br_blockcount
>= aoff
+ alen
);
4216 ASSERT(isnullstartblock(got
->br_startblock
));
4217 ASSERT(got
->br_state
== XFS_EXT_NORM
);
4220 out_unreserve_blocks
:
4222 xfs_mod_frextents(mp
, extsz
);
4224 xfs_mod_fdblocks(mp
, alen
, false);
4225 out_unreserve_quota
:
4226 if (XFS_IS_QUOTA_ON(mp
))
4227 xfs_trans_unreserve_quota_nblks(NULL
, ip
, (long)alen
, 0, rt
?
4228 XFS_QMOPT_RES_RTBLKS
: XFS_QMOPT_RES_REGBLKS
);
4234 struct xfs_bmalloca
*bma
)
4236 struct xfs_mount
*mp
= bma
->ip
->i_mount
;
4237 int whichfork
= (bma
->flags
& XFS_BMAPI_ATTRFORK
) ?
4238 XFS_ATTR_FORK
: XFS_DATA_FORK
;
4239 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
4240 int tmp_logflags
= 0;
4243 ASSERT(bma
->length
> 0);
4246 * For the wasdelay case, we could also just allocate the stuff asked
4247 * for in this bmap call but that wouldn't be as good.
4250 bma
->length
= (xfs_extlen_t
)bma
->got
.br_blockcount
;
4251 bma
->offset
= bma
->got
.br_startoff
;
4252 if (bma
->idx
!= NULLEXTNUM
&& bma
->idx
) {
4253 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
- 1),
4257 bma
->length
= XFS_FILBLKS_MIN(bma
->length
, MAXEXTLEN
);
4259 bma
->length
= XFS_FILBLKS_MIN(bma
->length
,
4260 bma
->got
.br_startoff
- bma
->offset
);
4264 * Set the data type being allocated. For the data fork, the first data
4265 * in the file is treated differently to all other allocations. For the
4266 * attribute fork, we only need to ensure the allocated range is not on
4269 if (!(bma
->flags
& XFS_BMAPI_METADATA
)) {
4270 bma
->datatype
= XFS_ALLOC_NOBUSY
;
4271 if (whichfork
== XFS_DATA_FORK
) {
4272 if (bma
->offset
== 0)
4273 bma
->datatype
|= XFS_ALLOC_INITIAL_USER_DATA
;
4275 bma
->datatype
|= XFS_ALLOC_USERDATA
;
4277 if (bma
->flags
& XFS_BMAPI_ZERO
)
4278 bma
->datatype
|= XFS_ALLOC_USERDATA_ZERO
;
4281 bma
->minlen
= (bma
->flags
& XFS_BMAPI_CONTIG
) ? bma
->length
: 1;
4284 * Only want to do the alignment at the eof if it is userdata and
4285 * allocation length is larger than a stripe unit.
4287 if (mp
->m_dalign
&& bma
->length
>= mp
->m_dalign
&&
4288 !(bma
->flags
& XFS_BMAPI_METADATA
) && whichfork
== XFS_DATA_FORK
) {
4289 error
= xfs_bmap_isaeof(bma
, whichfork
);
4294 error
= xfs_bmap_alloc(bma
);
4298 if (bma
->dfops
->dop_low
)
4301 bma
->cur
->bc_private
.b
.firstblock
= *bma
->firstblock
;
4302 if (bma
->blkno
== NULLFSBLOCK
)
4304 if ((ifp
->if_flags
& XFS_IFBROOT
) && !bma
->cur
) {
4305 bma
->cur
= xfs_bmbt_init_cursor(mp
, bma
->tp
, bma
->ip
, whichfork
);
4306 bma
->cur
->bc_private
.b
.firstblock
= *bma
->firstblock
;
4307 bma
->cur
->bc_private
.b
.dfops
= bma
->dfops
;
4310 * Bump the number of extents we've allocated
4316 bma
->cur
->bc_private
.b
.flags
=
4317 bma
->wasdel
? XFS_BTCUR_BPRV_WASDEL
: 0;
4319 bma
->got
.br_startoff
= bma
->offset
;
4320 bma
->got
.br_startblock
= bma
->blkno
;
4321 bma
->got
.br_blockcount
= bma
->length
;
4322 bma
->got
.br_state
= XFS_EXT_NORM
;
4325 * A wasdelay extent has been initialized, so shouldn't be flagged
4328 if (!bma
->wasdel
&& (bma
->flags
& XFS_BMAPI_PREALLOC
) &&
4329 xfs_sb_version_hasextflgbit(&mp
->m_sb
))
4330 bma
->got
.br_state
= XFS_EXT_UNWRITTEN
;
4333 error
= xfs_bmap_add_extent_delay_real(bma
);
4335 error
= xfs_bmap_add_extent_hole_real(bma
, whichfork
);
4337 bma
->logflags
|= tmp_logflags
;
4342 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4343 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4344 * the neighbouring ones.
4346 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
), &bma
->got
);
4348 ASSERT(bma
->got
.br_startoff
<= bma
->offset
);
4349 ASSERT(bma
->got
.br_startoff
+ bma
->got
.br_blockcount
>=
4350 bma
->offset
+ bma
->length
);
4351 ASSERT(bma
->got
.br_state
== XFS_EXT_NORM
||
4352 bma
->got
.br_state
== XFS_EXT_UNWRITTEN
);
4357 xfs_bmapi_convert_unwritten(
4358 struct xfs_bmalloca
*bma
,
4359 struct xfs_bmbt_irec
*mval
,
4363 int whichfork
= (flags
& XFS_BMAPI_ATTRFORK
) ?
4364 XFS_ATTR_FORK
: XFS_DATA_FORK
;
4365 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
4366 int tmp_logflags
= 0;
4369 /* check if we need to do unwritten->real conversion */
4370 if (mval
->br_state
== XFS_EXT_UNWRITTEN
&&
4371 (flags
& XFS_BMAPI_PREALLOC
))
4374 /* check if we need to do real->unwritten conversion */
4375 if (mval
->br_state
== XFS_EXT_NORM
&&
4376 (flags
& (XFS_BMAPI_PREALLOC
| XFS_BMAPI_CONVERT
)) !=
4377 (XFS_BMAPI_PREALLOC
| XFS_BMAPI_CONVERT
))
4381 * Modify (by adding) the state flag, if writing.
4383 ASSERT(mval
->br_blockcount
<= len
);
4384 if ((ifp
->if_flags
& XFS_IFBROOT
) && !bma
->cur
) {
4385 bma
->cur
= xfs_bmbt_init_cursor(bma
->ip
->i_mount
, bma
->tp
,
4386 bma
->ip
, whichfork
);
4387 bma
->cur
->bc_private
.b
.firstblock
= *bma
->firstblock
;
4388 bma
->cur
->bc_private
.b
.dfops
= bma
->dfops
;
4390 mval
->br_state
= (mval
->br_state
== XFS_EXT_UNWRITTEN
)
4391 ? XFS_EXT_NORM
: XFS_EXT_UNWRITTEN
;
4394 * Before insertion into the bmbt, zero the range being converted
4397 if (flags
& XFS_BMAPI_ZERO
) {
4398 error
= xfs_zero_extent(bma
->ip
, mval
->br_startblock
,
4399 mval
->br_blockcount
);
4404 error
= xfs_bmap_add_extent_unwritten_real(bma
->tp
, bma
->ip
, &bma
->idx
,
4405 &bma
->cur
, mval
, bma
->firstblock
, bma
->dfops
,
4408 * Log the inode core unconditionally in the unwritten extent conversion
4409 * path because the conversion might not have done so (e.g., if the
4410 * extent count hasn't changed). We need to make sure the inode is dirty
4411 * in the transaction for the sake of fsync(), even if nothing has
4412 * changed, because fsync() will not force the log for this transaction
4413 * unless it sees the inode pinned.
4415 bma
->logflags
|= tmp_logflags
| XFS_ILOG_CORE
;
4420 * Update our extent pointer, given that
4421 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4422 * of the neighbouring ones.
4424 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
), &bma
->got
);
4427 * We may have combined previously unwritten space with written space,
4428 * so generate another request.
4430 if (mval
->br_blockcount
< len
)
4436 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4437 * extent state if necessary. Details behaviour is controlled by the flags
4438 * parameter. Only allocates blocks from a single allocation group, to avoid
4441 * The returned value in "firstblock" from the first call in a transaction
4442 * must be remembered and presented to subsequent calls in "firstblock".
4443 * An upper bound for the number of blocks to be allocated is supplied to
4444 * the first call in "total"; if no allocation group has that many free
4445 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4449 struct xfs_trans
*tp
, /* transaction pointer */
4450 struct xfs_inode
*ip
, /* incore inode */
4451 xfs_fileoff_t bno
, /* starting file offs. mapped */
4452 xfs_filblks_t len
, /* length to map in file */
4453 int flags
, /* XFS_BMAPI_... */
4454 xfs_fsblock_t
*firstblock
, /* first allocated block
4455 controls a.g. for allocs */
4456 xfs_extlen_t total
, /* total blocks needed */
4457 struct xfs_bmbt_irec
*mval
, /* output: map values */
4458 int *nmap
, /* i/o: mval size/count */
4459 struct xfs_defer_ops
*dfops
) /* i/o: list extents to free */
4461 struct xfs_mount
*mp
= ip
->i_mount
;
4462 struct xfs_ifork
*ifp
;
4463 struct xfs_bmalloca bma
= { NULL
}; /* args for xfs_bmap_alloc */
4464 xfs_fileoff_t end
; /* end of mapped file region */
4465 int eof
; /* after the end of extents */
4466 int error
; /* error return */
4467 int n
; /* current extent index */
4468 xfs_fileoff_t obno
; /* old block number (offset) */
4469 int whichfork
; /* data or attr fork */
4470 char inhole
; /* current location is hole in file */
4471 char wasdelay
; /* old extent was delayed */
4474 xfs_fileoff_t orig_bno
; /* original block number value */
4475 int orig_flags
; /* original flags arg value */
4476 xfs_filblks_t orig_len
; /* original value of len arg */
4477 struct xfs_bmbt_irec
*orig_mval
; /* original value of mval */
4478 int orig_nmap
; /* original value of *nmap */
4486 whichfork
= (flags
& XFS_BMAPI_ATTRFORK
) ?
4487 XFS_ATTR_FORK
: XFS_DATA_FORK
;
4490 ASSERT(*nmap
<= XFS_BMAP_MAX_NMAP
);
4491 ASSERT(!(flags
& XFS_BMAPI_IGSTATE
));
4494 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_LOCAL
);
4495 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
4496 ASSERT(!(flags
& XFS_BMAPI_REMAP
) || whichfork
== XFS_DATA_FORK
);
4497 ASSERT(!(flags
& XFS_BMAPI_PREALLOC
) || !(flags
& XFS_BMAPI_REMAP
));
4498 ASSERT(!(flags
& XFS_BMAPI_CONVERT
) || !(flags
& XFS_BMAPI_REMAP
));
4500 /* zeroing is for currently only for data extents, not metadata */
4501 ASSERT((flags
& (XFS_BMAPI_METADATA
| XFS_BMAPI_ZERO
)) !=
4502 (XFS_BMAPI_METADATA
| XFS_BMAPI_ZERO
));
4504 * we can allocate unwritten extents or pre-zero allocated blocks,
4505 * but it makes no sense to do both at once. This would result in
4506 * zeroing the unwritten extent twice, but it still being an
4507 * unwritten extent....
4509 ASSERT((flags
& (XFS_BMAPI_PREALLOC
| XFS_BMAPI_ZERO
)) !=
4510 (XFS_BMAPI_PREALLOC
| XFS_BMAPI_ZERO
));
4512 if (unlikely(XFS_TEST_ERROR(
4513 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
4514 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
4515 mp
, XFS_ERRTAG_BMAPIFORMAT
, XFS_RANDOM_BMAPIFORMAT
))) {
4516 XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW
, mp
);
4517 return -EFSCORRUPTED
;
4520 if (XFS_FORCED_SHUTDOWN(mp
))
4523 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4525 XFS_STATS_INC(mp
, xs_blk_mapw
);
4527 if (*firstblock
== NULLFSBLOCK
) {
4528 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
)
4529 bma
.minleft
= be16_to_cpu(ifp
->if_broot
->bb_level
) + 1;
4536 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
4537 error
= xfs_iread_extents(tp
, ip
, whichfork
);
4542 xfs_bmap_search_extents(ip
, bno
, whichfork
, &eof
, &bma
.idx
, &bma
.got
,
4553 bma
.firstblock
= firstblock
;
4555 while (bno
< end
&& n
< *nmap
) {
4556 inhole
= eof
|| bma
.got
.br_startoff
> bno
;
4557 wasdelay
= !inhole
&& isnullstartblock(bma
.got
.br_startblock
);
4560 * Make sure we only reflink into a hole.
4562 if (flags
& XFS_BMAPI_REMAP
)
4566 * First, deal with the hole before the allocated space
4567 * that we found, if any.
4569 if (inhole
|| wasdelay
) {
4571 bma
.conv
= !!(flags
& XFS_BMAPI_CONVERT
);
4572 bma
.wasdel
= wasdelay
;
4577 * There's a 32/64 bit type mismatch between the
4578 * allocation length request (which can be 64 bits in
4579 * length) and the bma length request, which is
4580 * xfs_extlen_t and therefore 32 bits. Hence we have to
4581 * check for 32-bit overflows and handle them here.
4583 if (len
> (xfs_filblks_t
)MAXEXTLEN
)
4584 bma
.length
= MAXEXTLEN
;
4589 ASSERT(bma
.length
> 0);
4590 error
= xfs_bmapi_allocate(&bma
);
4593 if (bma
.blkno
== NULLFSBLOCK
)
4597 /* Deal with the allocated space we found. */
4598 xfs_bmapi_trim_map(mval
, &bma
.got
, &bno
, len
, obno
,
4601 /* Execute unwritten extent conversion if necessary */
4602 error
= xfs_bmapi_convert_unwritten(&bma
, mval
, len
, flags
);
4603 if (error
== -EAGAIN
)
4608 /* update the extent map to return */
4609 xfs_bmapi_update_map(&mval
, &bno
, &len
, obno
, end
, &n
, flags
);
4612 * If we're done, stop now. Stop when we've allocated
4613 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4614 * the transaction may get too big.
4616 if (bno
>= end
|| n
>= *nmap
|| bma
.nallocs
>= *nmap
)
4619 /* Else go on to the next record. */
4621 if (++bma
.idx
< ifp
->if_bytes
/ sizeof(xfs_bmbt_rec_t
)) {
4622 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
.idx
),
4630 * Transform from btree to extents, give it cur.
4632 if (xfs_bmap_wants_extents(ip
, whichfork
)) {
4633 int tmp_logflags
= 0;
4636 error
= xfs_bmap_btree_to_extents(tp
, ip
, bma
.cur
,
4637 &tmp_logflags
, whichfork
);
4638 bma
.logflags
|= tmp_logflags
;
4643 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
||
4644 XFS_IFORK_NEXTENTS(ip
, whichfork
) >
4645 XFS_IFORK_MAXEXT(ip
, whichfork
));
4649 * Log everything. Do this after conversion, there's no point in
4650 * logging the extent records if we've converted to btree format.
4652 if ((bma
.logflags
& xfs_ilog_fext(whichfork
)) &&
4653 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
4654 bma
.logflags
&= ~xfs_ilog_fext(whichfork
);
4655 else if ((bma
.logflags
& xfs_ilog_fbroot(whichfork
)) &&
4656 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)
4657 bma
.logflags
&= ~xfs_ilog_fbroot(whichfork
);
4659 * Log whatever the flags say, even if error. Otherwise we might miss
4660 * detecting a case where the data is changed, there's an error,
4661 * and it's not logged so we don't shutdown when we should.
4664 xfs_trans_log_inode(tp
, ip
, bma
.logflags
);
4668 ASSERT(*firstblock
== NULLFSBLOCK
||
4669 XFS_FSB_TO_AGNO(mp
, *firstblock
) ==
4671 bma
.cur
->bc_private
.b
.firstblock
) ||
4673 XFS_FSB_TO_AGNO(mp
, *firstblock
) <
4675 bma
.cur
->bc_private
.b
.firstblock
)));
4676 *firstblock
= bma
.cur
->bc_private
.b
.firstblock
;
4678 xfs_btree_del_cursor(bma
.cur
,
4679 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
4682 xfs_bmap_validate_ret(orig_bno
, orig_len
, orig_flags
, orig_mval
,
4688 * When a delalloc extent is split (e.g., due to a hole punch), the original
4689 * indlen reservation must be shared across the two new extents that are left
4692 * Given the original reservation and the worst case indlen for the two new
4693 * extents (as calculated by xfs_bmap_worst_indlen()), split the original
4694 * reservation fairly across the two new extents. If necessary, steal available
4695 * blocks from a deleted extent to make up a reservation deficiency (e.g., if
4696 * ores == 1). The number of stolen blocks is returned. The availability and
4697 * subsequent accounting of stolen blocks is the responsibility of the caller.
4699 static xfs_filblks_t
4700 xfs_bmap_split_indlen(
4701 xfs_filblks_t ores
, /* original res. */
4702 xfs_filblks_t
*indlen1
, /* ext1 worst indlen */
4703 xfs_filblks_t
*indlen2
, /* ext2 worst indlen */
4704 xfs_filblks_t avail
) /* stealable blocks */
4706 xfs_filblks_t len1
= *indlen1
;
4707 xfs_filblks_t len2
= *indlen2
;
4708 xfs_filblks_t nres
= len1
+ len2
; /* new total res. */
4709 xfs_filblks_t stolen
= 0;
4712 * Steal as many blocks as we can to try and satisfy the worst case
4713 * indlen for both new extents.
4715 while (nres
> ores
&& avail
) {
4722 * The only blocks available are those reserved for the original
4723 * extent and what we can steal from the extent being removed.
4724 * If this still isn't enough to satisfy the combined
4725 * requirements for the two new extents, skim blocks off of each
4726 * of the new reservations until they match what is available.
4728 while (nres
> ores
) {
4748 * Called by xfs_bmapi to update file extent records and the btree
4749 * after removing space (or undoing a delayed allocation).
4751 STATIC
int /* error */
4752 xfs_bmap_del_extent(
4753 xfs_inode_t
*ip
, /* incore inode pointer */
4754 xfs_trans_t
*tp
, /* current transaction pointer */
4755 xfs_extnum_t
*idx
, /* extent number to update/delete */
4756 struct xfs_defer_ops
*dfops
, /* list of extents to be freed */
4757 xfs_btree_cur_t
*cur
, /* if null, not a btree */
4758 xfs_bmbt_irec_t
*del
, /* data to remove from extents */
4759 int *logflagsp
, /* inode logging flags */
4760 int whichfork
, /* data or attr fork */
4761 int bflags
) /* bmapi flags */
4763 xfs_filblks_t da_new
; /* new delay-alloc indirect blocks */
4764 xfs_filblks_t da_old
; /* old delay-alloc indirect blocks */
4765 xfs_fsblock_t del_endblock
=0; /* first block past del */
4766 xfs_fileoff_t del_endoff
; /* first offset past del */
4767 int delay
; /* current block is delayed allocated */
4768 int do_fx
; /* free extent at end of routine */
4769 xfs_bmbt_rec_host_t
*ep
; /* current extent entry pointer */
4770 int error
; /* error return value */
4771 int flags
; /* inode logging flags */
4772 xfs_bmbt_irec_t got
; /* current extent entry */
4773 xfs_fileoff_t got_endoff
; /* first offset past got */
4774 int i
; /* temp state */
4775 xfs_ifork_t
*ifp
; /* inode fork pointer */
4776 xfs_mount_t
*mp
; /* mount structure */
4777 xfs_filblks_t nblks
; /* quota/sb block count */
4778 xfs_bmbt_irec_t
new; /* new record to be inserted */
4780 uint qfield
; /* quota field to update */
4781 xfs_filblks_t temp
; /* for indirect length calculations */
4782 xfs_filblks_t temp2
; /* for indirect length calculations */
4786 XFS_STATS_INC(mp
, xs_del_exlist
);
4788 if (whichfork
== XFS_ATTR_FORK
)
4789 state
|= BMAP_ATTRFORK
;
4791 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4792 ASSERT((*idx
>= 0) && (*idx
< ifp
->if_bytes
/
4793 (uint
)sizeof(xfs_bmbt_rec_t
)));
4794 ASSERT(del
->br_blockcount
> 0);
4795 ep
= xfs_iext_get_ext(ifp
, *idx
);
4796 xfs_bmbt_get_all(ep
, &got
);
4797 ASSERT(got
.br_startoff
<= del
->br_startoff
);
4798 del_endoff
= del
->br_startoff
+ del
->br_blockcount
;
4799 got_endoff
= got
.br_startoff
+ got
.br_blockcount
;
4800 ASSERT(got_endoff
>= del_endoff
);
4801 delay
= isnullstartblock(got
.br_startblock
);
4802 ASSERT(isnullstartblock(del
->br_startblock
) == delay
);
4807 * If deleting a real allocation, must free up the disk space.
4810 flags
= XFS_ILOG_CORE
;
4812 * Realtime allocation. Free it and record di_nblocks update.
4814 if (whichfork
== XFS_DATA_FORK
&& XFS_IS_REALTIME_INODE(ip
)) {
4818 ASSERT(do_mod(del
->br_blockcount
,
4819 mp
->m_sb
.sb_rextsize
) == 0);
4820 ASSERT(do_mod(del
->br_startblock
,
4821 mp
->m_sb
.sb_rextsize
) == 0);
4822 bno
= del
->br_startblock
;
4823 len
= del
->br_blockcount
;
4824 do_div(bno
, mp
->m_sb
.sb_rextsize
);
4825 do_div(len
, mp
->m_sb
.sb_rextsize
);
4826 error
= xfs_rtfree_extent(tp
, bno
, (xfs_extlen_t
)len
);
4830 nblks
= len
* mp
->m_sb
.sb_rextsize
;
4831 qfield
= XFS_TRANS_DQ_RTBCOUNT
;
4834 * Ordinary allocation.
4838 nblks
= del
->br_blockcount
;
4839 qfield
= XFS_TRANS_DQ_BCOUNT
;
4842 * Set up del_endblock and cur for later.
4844 del_endblock
= del
->br_startblock
+ del
->br_blockcount
;
4846 if ((error
= xfs_bmbt_lookup_eq(cur
, got
.br_startoff
,
4847 got
.br_startblock
, got
.br_blockcount
,
4850 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
4852 da_old
= da_new
= 0;
4854 da_old
= startblockval(got
.br_startblock
);
4861 * Set flag value to use in switch statement.
4862 * Left-contig is 2, right-contig is 1.
4864 switch (((got
.br_startoff
== del
->br_startoff
) << 1) |
4865 (got_endoff
== del_endoff
)) {
4868 * Matches the whole extent. Delete the entry.
4870 xfs_iext_remove(ip
, *idx
, 1,
4871 whichfork
== XFS_ATTR_FORK
? BMAP_ATTRFORK
: 0);
4876 XFS_IFORK_NEXT_SET(ip
, whichfork
,
4877 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
4878 flags
|= XFS_ILOG_CORE
;
4880 flags
|= xfs_ilog_fext(whichfork
);
4883 if ((error
= xfs_btree_delete(cur
, &i
)))
4885 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
4890 * Deleting the first part of the extent.
4892 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
4893 xfs_bmbt_set_startoff(ep
, del_endoff
);
4894 temp
= got
.br_blockcount
- del
->br_blockcount
;
4895 xfs_bmbt_set_blockcount(ep
, temp
);
4897 temp
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
4899 xfs_bmbt_set_startblock(ep
, nullstartblock((int)temp
));
4900 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
4904 xfs_bmbt_set_startblock(ep
, del_endblock
);
4905 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
4907 flags
|= xfs_ilog_fext(whichfork
);
4910 if ((error
= xfs_bmbt_update(cur
, del_endoff
, del_endblock
,
4911 got
.br_blockcount
- del
->br_blockcount
,
4918 * Deleting the last part of the extent.
4920 temp
= got
.br_blockcount
- del
->br_blockcount
;
4921 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
4922 xfs_bmbt_set_blockcount(ep
, temp
);
4924 temp
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
4926 xfs_bmbt_set_startblock(ep
, nullstartblock((int)temp
));
4927 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
4931 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
4933 flags
|= xfs_ilog_fext(whichfork
);
4936 if ((error
= xfs_bmbt_update(cur
, got
.br_startoff
,
4938 got
.br_blockcount
- del
->br_blockcount
,
4945 * Deleting the middle of the extent.
4947 temp
= del
->br_startoff
- got
.br_startoff
;
4948 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
4949 xfs_bmbt_set_blockcount(ep
, temp
);
4950 new.br_startoff
= del_endoff
;
4951 temp2
= got_endoff
- del_endoff
;
4952 new.br_blockcount
= temp2
;
4953 new.br_state
= got
.br_state
;
4955 new.br_startblock
= del_endblock
;
4956 flags
|= XFS_ILOG_CORE
;
4958 if ((error
= xfs_bmbt_update(cur
,
4960 got
.br_startblock
, temp
,
4963 if ((error
= xfs_btree_increment(cur
, 0, &i
)))
4965 cur
->bc_rec
.b
= new;
4966 error
= xfs_btree_insert(cur
, &i
);
4967 if (error
&& error
!= -ENOSPC
)
4970 * If get no-space back from btree insert,
4971 * it tried a split, and we have a zero
4972 * block reservation.
4973 * Fix up our state and return the error.
4975 if (error
== -ENOSPC
) {
4977 * Reset the cursor, don't trust
4978 * it after any insert operation.
4980 if ((error
= xfs_bmbt_lookup_eq(cur
,
4985 XFS_WANT_CORRUPTED_GOTO(mp
,
4988 * Update the btree record back
4989 * to the original value.
4991 if ((error
= xfs_bmbt_update(cur
,
4998 * Reset the extent record back
4999 * to the original value.
5001 xfs_bmbt_set_blockcount(ep
,
5007 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
5009 flags
|= xfs_ilog_fext(whichfork
);
5010 XFS_IFORK_NEXT_SET(ip
, whichfork
,
5011 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
5013 xfs_filblks_t stolen
;
5014 ASSERT(whichfork
== XFS_DATA_FORK
);
5017 * Distribute the original indlen reservation across the
5018 * two new extents. Steal blocks from the deleted extent
5019 * if necessary. Stealing blocks simply fudges the
5020 * fdblocks accounting in xfs_bunmapi().
5022 temp
= xfs_bmap_worst_indlen(ip
, got
.br_blockcount
);
5023 temp2
= xfs_bmap_worst_indlen(ip
, new.br_blockcount
);
5024 stolen
= xfs_bmap_split_indlen(da_old
, &temp
, &temp2
,
5025 del
->br_blockcount
);
5026 da_new
= temp
+ temp2
- stolen
;
5027 del
->br_blockcount
-= stolen
;
5030 * Set the reservation for each extent. Warn if either
5031 * is zero as this can lead to delalloc problems.
5033 WARN_ON_ONCE(!temp
|| !temp2
);
5034 xfs_bmbt_set_startblock(ep
, nullstartblock((int)temp
));
5035 new.br_startblock
= nullstartblock((int)temp2
);
5037 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
5038 xfs_iext_insert(ip
, *idx
+ 1, 1, &new, state
);
5043 /* remove reverse mapping */
5045 error
= xfs_rmap_unmap_extent(mp
, dfops
, ip
, whichfork
, del
);
5051 * If we need to, add to list of extents to delete.
5053 if (do_fx
&& !(bflags
& XFS_BMAPI_REMAP
)) {
5054 if (xfs_is_reflink_inode(ip
) && whichfork
== XFS_DATA_FORK
) {
5055 error
= xfs_refcount_decrease_extent(mp
, dfops
, del
);
5059 xfs_bmap_add_free(mp
, dfops
, del
->br_startblock
,
5060 del
->br_blockcount
, NULL
);
5064 * Adjust inode # blocks in the file.
5067 ip
->i_d
.di_nblocks
-= nblks
;
5069 * Adjust quota data.
5071 if (qfield
&& !(bflags
& XFS_BMAPI_REMAP
))
5072 xfs_trans_mod_dquot_byino(tp
, ip
, qfield
, (long)-nblks
);
5075 * Account for change in delayed indirect blocks.
5076 * Nothing to do for disk quota accounting here.
5078 ASSERT(da_old
>= da_new
);
5079 if (da_old
> da_new
)
5080 xfs_mod_fdblocks(mp
, (int64_t)(da_old
- da_new
), false);
5087 * Unmap (remove) blocks from a file.
5088 * If nexts is nonzero then the number of extents to remove is limited to
5089 * that value. If not all extents in the block range can be removed then
5094 xfs_trans_t
*tp
, /* transaction pointer */
5095 struct xfs_inode
*ip
, /* incore inode */
5096 xfs_fileoff_t bno
, /* starting offset to unmap */
5097 xfs_filblks_t len
, /* length to unmap in file */
5098 int flags
, /* misc flags */
5099 xfs_extnum_t nexts
, /* number of extents max */
5100 xfs_fsblock_t
*firstblock
, /* first allocated block
5101 controls a.g. for allocs */
5102 struct xfs_defer_ops
*dfops
, /* i/o: list extents to free */
5103 int *done
) /* set if not done yet */
5105 xfs_btree_cur_t
*cur
; /* bmap btree cursor */
5106 xfs_bmbt_irec_t del
; /* extent being deleted */
5107 int eof
; /* is deleting at eof */
5108 xfs_bmbt_rec_host_t
*ep
; /* extent record pointer */
5109 int error
; /* error return value */
5110 xfs_extnum_t extno
; /* extent number in list */
5111 xfs_bmbt_irec_t got
; /* current extent record */
5112 xfs_ifork_t
*ifp
; /* inode fork pointer */
5113 int isrt
; /* freeing in rt area */
5114 xfs_extnum_t lastx
; /* last extent index used */
5115 int logflags
; /* transaction logging flags */
5116 xfs_extlen_t mod
; /* rt extent offset */
5117 xfs_mount_t
*mp
; /* mount structure */
5118 xfs_extnum_t nextents
; /* number of file extents */
5119 xfs_bmbt_irec_t prev
; /* previous extent record */
5120 xfs_fileoff_t start
; /* first file offset deleted */
5121 int tmp_logflags
; /* partial logging flags */
5122 int wasdel
; /* was a delayed alloc extent */
5123 int whichfork
; /* data or attribute fork */
5126 trace_xfs_bunmap(ip
, bno
, len
, flags
, _RET_IP_
);
5128 whichfork
= (flags
& XFS_BMAPI_ATTRFORK
) ?
5129 XFS_ATTR_FORK
: XFS_DATA_FORK
;
5130 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5132 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
5133 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)) {
5134 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW
,
5136 return -EFSCORRUPTED
;
5139 if (XFS_FORCED_SHUTDOWN(mp
))
5142 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
5146 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
5147 (error
= xfs_iread_extents(tp
, ip
, whichfork
)))
5149 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
5150 if (nextents
== 0) {
5154 XFS_STATS_INC(mp
, xs_blk_unmap
);
5155 isrt
= (whichfork
== XFS_DATA_FORK
) && XFS_IS_REALTIME_INODE(ip
);
5157 bno
= start
+ len
- 1;
5158 ep
= xfs_bmap_search_extents(ip
, bno
, whichfork
, &eof
, &lastx
, &got
,
5162 * Check to see if the given block number is past the end of the
5163 * file, back up to the last block if so...
5166 ep
= xfs_iext_get_ext(ifp
, --lastx
);
5167 xfs_bmbt_get_all(ep
, &got
);
5168 bno
= got
.br_startoff
+ got
.br_blockcount
- 1;
5171 if (ifp
->if_flags
& XFS_IFBROOT
) {
5172 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
);
5173 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
5174 cur
->bc_private
.b
.firstblock
= *firstblock
;
5175 cur
->bc_private
.b
.dfops
= dfops
;
5176 cur
->bc_private
.b
.flags
= 0;
5182 * Synchronize by locking the bitmap inode.
5184 xfs_ilock(mp
->m_rbmip
, XFS_ILOCK_EXCL
|XFS_ILOCK_RTBITMAP
);
5185 xfs_trans_ijoin(tp
, mp
->m_rbmip
, XFS_ILOCK_EXCL
);
5186 xfs_ilock(mp
->m_rsumip
, XFS_ILOCK_EXCL
|XFS_ILOCK_RTSUM
);
5187 xfs_trans_ijoin(tp
, mp
->m_rsumip
, XFS_ILOCK_EXCL
);
5191 while (bno
!= (xfs_fileoff_t
)-1 && bno
>= start
&& lastx
>= 0 &&
5192 (nexts
== 0 || extno
< nexts
)) {
5194 * Is the found extent after a hole in which bno lives?
5195 * Just back up to the previous extent, if so.
5197 if (got
.br_startoff
> bno
) {
5200 ep
= xfs_iext_get_ext(ifp
, lastx
);
5201 xfs_bmbt_get_all(ep
, &got
);
5204 * Is the last block of this extent before the range
5205 * we're supposed to delete? If so, we're done.
5207 bno
= XFS_FILEOFF_MIN(bno
,
5208 got
.br_startoff
+ got
.br_blockcount
- 1);
5212 * Then deal with the (possibly delayed) allocated space
5217 wasdel
= isnullstartblock(del
.br_startblock
);
5218 if (got
.br_startoff
< start
) {
5219 del
.br_startoff
= start
;
5220 del
.br_blockcount
-= start
- got
.br_startoff
;
5222 del
.br_startblock
+= start
- got
.br_startoff
;
5224 if (del
.br_startoff
+ del
.br_blockcount
> bno
+ 1)
5225 del
.br_blockcount
= bno
+ 1 - del
.br_startoff
;
5226 sum
= del
.br_startblock
+ del
.br_blockcount
;
5228 (mod
= do_mod(sum
, mp
->m_sb
.sb_rextsize
))) {
5230 * Realtime extent not lined up at the end.
5231 * The extent could have been split into written
5232 * and unwritten pieces, or we could just be
5233 * unmapping part of it. But we can't really
5234 * get rid of part of a realtime extent.
5236 if (del
.br_state
== XFS_EXT_UNWRITTEN
||
5237 !xfs_sb_version_hasextflgbit(&mp
->m_sb
)) {
5239 * This piece is unwritten, or we're not
5240 * using unwritten extents. Skip over it.
5243 bno
-= mod
> del
.br_blockcount
?
5244 del
.br_blockcount
: mod
;
5245 if (bno
< got
.br_startoff
) {
5247 xfs_bmbt_get_all(xfs_iext_get_ext(
5253 * It's written, turn it unwritten.
5254 * This is better than zeroing it.
5256 ASSERT(del
.br_state
== XFS_EXT_NORM
);
5257 ASSERT(tp
->t_blk_res
> 0);
5259 * If this spans a realtime extent boundary,
5260 * chop it back to the start of the one we end at.
5262 if (del
.br_blockcount
> mod
) {
5263 del
.br_startoff
+= del
.br_blockcount
- mod
;
5264 del
.br_startblock
+= del
.br_blockcount
- mod
;
5265 del
.br_blockcount
= mod
;
5267 del
.br_state
= XFS_EXT_UNWRITTEN
;
5268 error
= xfs_bmap_add_extent_unwritten_real(tp
, ip
,
5269 &lastx
, &cur
, &del
, firstblock
, dfops
,
5275 if (isrt
&& (mod
= do_mod(del
.br_startblock
, mp
->m_sb
.sb_rextsize
))) {
5277 * Realtime extent is lined up at the end but not
5278 * at the front. We'll get rid of full extents if
5281 mod
= mp
->m_sb
.sb_rextsize
- mod
;
5282 if (del
.br_blockcount
> mod
) {
5283 del
.br_blockcount
-= mod
;
5284 del
.br_startoff
+= mod
;
5285 del
.br_startblock
+= mod
;
5286 } else if ((del
.br_startoff
== start
&&
5287 (del
.br_state
== XFS_EXT_UNWRITTEN
||
5288 tp
->t_blk_res
== 0)) ||
5289 !xfs_sb_version_hasextflgbit(&mp
->m_sb
)) {
5291 * Can't make it unwritten. There isn't
5292 * a full extent here so just skip it.
5294 ASSERT(bno
>= del
.br_blockcount
);
5295 bno
-= del
.br_blockcount
;
5296 if (got
.br_startoff
> bno
) {
5298 ep
= xfs_iext_get_ext(ifp
,
5300 xfs_bmbt_get_all(ep
, &got
);
5304 } else if (del
.br_state
== XFS_EXT_UNWRITTEN
) {
5306 * This one is already unwritten.
5307 * It must have a written left neighbor.
5308 * Unwrite the killed part of that one and
5312 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
,
5314 ASSERT(prev
.br_state
== XFS_EXT_NORM
);
5315 ASSERT(!isnullstartblock(prev
.br_startblock
));
5316 ASSERT(del
.br_startblock
==
5317 prev
.br_startblock
+ prev
.br_blockcount
);
5318 if (prev
.br_startoff
< start
) {
5319 mod
= start
- prev
.br_startoff
;
5320 prev
.br_blockcount
-= mod
;
5321 prev
.br_startblock
+= mod
;
5322 prev
.br_startoff
= start
;
5324 prev
.br_state
= XFS_EXT_UNWRITTEN
;
5326 error
= xfs_bmap_add_extent_unwritten_real(tp
,
5327 ip
, &lastx
, &cur
, &prev
,
5328 firstblock
, dfops
, &logflags
);
5333 ASSERT(del
.br_state
== XFS_EXT_NORM
);
5334 del
.br_state
= XFS_EXT_UNWRITTEN
;
5335 error
= xfs_bmap_add_extent_unwritten_real(tp
,
5336 ip
, &lastx
, &cur
, &del
,
5337 firstblock
, dfops
, &logflags
);
5345 * If it's the case where the directory code is running
5346 * with no block reservation, and the deleted block is in
5347 * the middle of its extent, and the resulting insert
5348 * of an extent would cause transformation to btree format,
5349 * then reject it. The calling code will then swap
5350 * blocks around instead.
5351 * We have to do this now, rather than waiting for the
5352 * conversion to btree format, since the transaction
5355 if (!wasdel
&& tp
->t_blk_res
== 0 &&
5356 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
&&
5357 XFS_IFORK_NEXTENTS(ip
, whichfork
) >= /* Note the >= */
5358 XFS_IFORK_MAXEXT(ip
, whichfork
) &&
5359 del
.br_startoff
> got
.br_startoff
&&
5360 del
.br_startoff
+ del
.br_blockcount
<
5361 got
.br_startoff
+ got
.br_blockcount
) {
5367 * Unreserve quota and update realtime free space, if
5368 * appropriate. If delayed allocation, update the inode delalloc
5369 * counter now and wait to update the sb counters as
5370 * xfs_bmap_del_extent() might need to borrow some blocks.
5373 ASSERT(startblockval(del
.br_startblock
) > 0);
5375 xfs_filblks_t rtexts
;
5377 rtexts
= XFS_FSB_TO_B(mp
, del
.br_blockcount
);
5378 do_div(rtexts
, mp
->m_sb
.sb_rextsize
);
5379 xfs_mod_frextents(mp
, (int64_t)rtexts
);
5380 (void)xfs_trans_reserve_quota_nblks(NULL
,
5381 ip
, -((long)del
.br_blockcount
), 0,
5382 XFS_QMOPT_RES_RTBLKS
);
5384 (void)xfs_trans_reserve_quota_nblks(NULL
,
5385 ip
, -((long)del
.br_blockcount
), 0,
5386 XFS_QMOPT_RES_REGBLKS
);
5388 ip
->i_delayed_blks
-= del
.br_blockcount
;
5390 cur
->bc_private
.b
.flags
|=
5391 XFS_BTCUR_BPRV_WASDEL
;
5393 cur
->bc_private
.b
.flags
&= ~XFS_BTCUR_BPRV_WASDEL
;
5395 error
= xfs_bmap_del_extent(ip
, tp
, &lastx
, dfops
, cur
, &del
,
5396 &tmp_logflags
, whichfork
, flags
);
5397 logflags
|= tmp_logflags
;
5401 if (!isrt
&& wasdel
)
5402 xfs_mod_fdblocks(mp
, (int64_t)del
.br_blockcount
, false);
5404 bno
= del
.br_startoff
- 1;
5407 * If not done go on to the next (previous) record.
5409 if (bno
!= (xfs_fileoff_t
)-1 && bno
>= start
) {
5411 ep
= xfs_iext_get_ext(ifp
, lastx
);
5412 if (xfs_bmbt_get_startoff(ep
) > bno
) {
5414 ep
= xfs_iext_get_ext(ifp
,
5417 xfs_bmbt_get_all(ep
, &got
);
5422 *done
= bno
== (xfs_fileoff_t
)-1 || bno
< start
|| lastx
< 0;
5425 * Convert to a btree if necessary.
5427 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
5428 ASSERT(cur
== NULL
);
5429 error
= xfs_bmap_extents_to_btree(tp
, ip
, firstblock
, dfops
,
5430 &cur
, 0, &tmp_logflags
, whichfork
);
5431 logflags
|= tmp_logflags
;
5436 * transform from btree to extents, give it cur
5438 else if (xfs_bmap_wants_extents(ip
, whichfork
)) {
5439 ASSERT(cur
!= NULL
);
5440 error
= xfs_bmap_btree_to_extents(tp
, ip
, cur
, &tmp_logflags
,
5442 logflags
|= tmp_logflags
;
5447 * transform from extents to local?
5452 * Log everything. Do this after conversion, there's no point in
5453 * logging the extent records if we've converted to btree format.
5455 if ((logflags
& xfs_ilog_fext(whichfork
)) &&
5456 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
5457 logflags
&= ~xfs_ilog_fext(whichfork
);
5458 else if ((logflags
& xfs_ilog_fbroot(whichfork
)) &&
5459 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)
5460 logflags
&= ~xfs_ilog_fbroot(whichfork
);
5462 * Log inode even in the error case, if the transaction
5463 * is dirty we'll need to shut down the filesystem.
5466 xfs_trans_log_inode(tp
, ip
, logflags
);
5469 *firstblock
= cur
->bc_private
.b
.firstblock
;
5470 cur
->bc_private
.b
.allocated
= 0;
5472 xfs_btree_del_cursor(cur
,
5473 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
5479 * Determine whether an extent shift can be accomplished by a merge with the
5480 * extent that precedes the target hole of the shift.
5484 struct xfs_bmbt_irec
*left
, /* preceding extent */
5485 struct xfs_bmbt_irec
*got
, /* current extent to shift */
5486 xfs_fileoff_t shift
) /* shift fsb */
5488 xfs_fileoff_t startoff
;
5490 startoff
= got
->br_startoff
- shift
;
5493 * The extent, once shifted, must be adjacent in-file and on-disk with
5494 * the preceding extent.
5496 if ((left
->br_startoff
+ left
->br_blockcount
!= startoff
) ||
5497 (left
->br_startblock
+ left
->br_blockcount
!= got
->br_startblock
) ||
5498 (left
->br_state
!= got
->br_state
) ||
5499 (left
->br_blockcount
+ got
->br_blockcount
> MAXEXTLEN
))
5506 * A bmap extent shift adjusts the file offset of an extent to fill a preceding
5507 * hole in the file. If an extent shift would result in the extent being fully
5508 * adjacent to the extent that currently precedes the hole, we can merge with
5509 * the preceding extent rather than do the shift.
5511 * This function assumes the caller has verified a shift-by-merge is possible
5512 * with the provided extents via xfs_bmse_can_merge().
5516 struct xfs_inode
*ip
,
5518 xfs_fileoff_t shift
, /* shift fsb */
5519 int current_ext
, /* idx of gotp */
5520 struct xfs_bmbt_rec_host
*gotp
, /* extent to shift */
5521 struct xfs_bmbt_rec_host
*leftp
, /* preceding extent */
5522 struct xfs_btree_cur
*cur
,
5523 int *logflags
) /* output */
5525 struct xfs_bmbt_irec got
;
5526 struct xfs_bmbt_irec left
;
5527 xfs_filblks_t blockcount
;
5529 struct xfs_mount
*mp
= ip
->i_mount
;
5531 xfs_bmbt_get_all(gotp
, &got
);
5532 xfs_bmbt_get_all(leftp
, &left
);
5533 blockcount
= left
.br_blockcount
+ got
.br_blockcount
;
5535 ASSERT(xfs_isilocked(ip
, XFS_IOLOCK_EXCL
));
5536 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
5537 ASSERT(xfs_bmse_can_merge(&left
, &got
, shift
));
5540 * Merge the in-core extents. Note that the host record pointers and
5541 * current_ext index are invalid once the extent has been removed via
5542 * xfs_iext_remove().
5544 xfs_bmbt_set_blockcount(leftp
, blockcount
);
5545 xfs_iext_remove(ip
, current_ext
, 1, 0);
5548 * Update the on-disk extent count, the btree if necessary and log the
5551 XFS_IFORK_NEXT_SET(ip
, whichfork
,
5552 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
5553 *logflags
|= XFS_ILOG_CORE
;
5555 *logflags
|= XFS_ILOG_DEXT
;
5559 /* lookup and remove the extent to merge */
5560 error
= xfs_bmbt_lookup_eq(cur
, got
.br_startoff
, got
.br_startblock
,
5561 got
.br_blockcount
, &i
);
5564 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
5566 error
= xfs_btree_delete(cur
, &i
);
5569 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
5571 /* lookup and update size of the previous extent */
5572 error
= xfs_bmbt_lookup_eq(cur
, left
.br_startoff
, left
.br_startblock
,
5573 left
.br_blockcount
, &i
);
5576 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
5578 left
.br_blockcount
= blockcount
;
5580 return xfs_bmbt_update(cur
, left
.br_startoff
, left
.br_startblock
,
5581 left
.br_blockcount
, left
.br_state
);
5585 * Shift a single extent.
5589 struct xfs_inode
*ip
,
5591 xfs_fileoff_t offset_shift_fsb
,
5593 struct xfs_bmbt_rec_host
*gotp
,
5594 struct xfs_btree_cur
*cur
,
5596 enum shift_direction direction
,
5597 struct xfs_defer_ops
*dfops
)
5599 struct xfs_ifork
*ifp
;
5600 struct xfs_mount
*mp
;
5601 xfs_fileoff_t startoff
;
5602 struct xfs_bmbt_rec_host
*adj_irecp
;
5603 struct xfs_bmbt_irec got
;
5604 struct xfs_bmbt_irec adj_irec
;
5610 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5611 total_extents
= ifp
->if_bytes
/ sizeof(xfs_bmbt_rec_t
);
5613 xfs_bmbt_get_all(gotp
, &got
);
5615 /* delalloc extents should be prevented by caller */
5616 XFS_WANT_CORRUPTED_RETURN(mp
, !isnullstartblock(got
.br_startblock
));
5618 if (direction
== SHIFT_LEFT
) {
5619 startoff
= got
.br_startoff
- offset_shift_fsb
;
5622 * Check for merge if we've got an extent to the left,
5623 * otherwise make sure there's enough room at the start
5624 * of the file for the shift.
5626 if (!*current_ext
) {
5627 if (got
.br_startoff
< offset_shift_fsb
)
5629 goto update_current_ext
;
5632 * grab the left extent and check for a large
5635 adj_irecp
= xfs_iext_get_ext(ifp
, *current_ext
- 1);
5636 xfs_bmbt_get_all(adj_irecp
, &adj_irec
);
5639 adj_irec
.br_startoff
+ adj_irec
.br_blockcount
)
5642 /* check whether to merge the extent or shift it down */
5643 if (xfs_bmse_can_merge(&adj_irec
, &got
,
5644 offset_shift_fsb
)) {
5645 error
= xfs_bmse_merge(ip
, whichfork
, offset_shift_fsb
,
5646 *current_ext
, gotp
, adj_irecp
,
5654 startoff
= got
.br_startoff
+ offset_shift_fsb
;
5655 /* nothing to move if this is the last extent */
5656 if (*current_ext
>= (total_extents
- 1))
5657 goto update_current_ext
;
5659 * If this is not the last extent in the file, make sure there
5660 * is enough room between current extent and next extent for
5661 * accommodating the shift.
5663 adj_irecp
= xfs_iext_get_ext(ifp
, *current_ext
+ 1);
5664 xfs_bmbt_get_all(adj_irecp
, &adj_irec
);
5665 if (startoff
+ got
.br_blockcount
> adj_irec
.br_startoff
)
5668 * Unlike a left shift (which involves a hole punch),
5669 * a right shift does not modify extent neighbors
5670 * in any way. We should never find mergeable extents
5671 * in this scenario. Check anyways and warn if we
5672 * encounter two extents that could be one.
5674 if (xfs_bmse_can_merge(&got
, &adj_irec
, offset_shift_fsb
))
5678 * Increment the extent index for the next iteration, update the start
5679 * offset of the in-core extent and update the btree if applicable.
5682 if (direction
== SHIFT_LEFT
)
5686 xfs_bmbt_set_startoff(gotp
, startoff
);
5687 *logflags
|= XFS_ILOG_CORE
;
5690 *logflags
|= XFS_ILOG_DEXT
;
5694 error
= xfs_bmbt_lookup_eq(cur
, got
.br_startoff
, got
.br_startblock
,
5695 got
.br_blockcount
, &i
);
5698 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
5700 got
.br_startoff
= startoff
;
5701 error
= xfs_bmbt_update(cur
, got
.br_startoff
, got
.br_startblock
,
5702 got
.br_blockcount
, got
.br_state
);
5707 /* update reverse mapping */
5708 error
= xfs_rmap_unmap_extent(mp
, dfops
, ip
, whichfork
, &adj_irec
);
5711 adj_irec
.br_startoff
= startoff
;
5712 return xfs_rmap_map_extent(mp
, dfops
, ip
, whichfork
, &adj_irec
);
5716 * Shift extent records to the left/right to cover/create a hole.
5718 * The maximum number of extents to be shifted in a single operation is
5719 * @num_exts. @stop_fsb specifies the file offset at which to stop shift and the
5720 * file offset where we've left off is returned in @next_fsb. @offset_shift_fsb
5721 * is the length by which each extent is shifted. If there is no hole to shift
5722 * the extents into, this will be considered invalid operation and we abort
5726 xfs_bmap_shift_extents(
5727 struct xfs_trans
*tp
,
5728 struct xfs_inode
*ip
,
5729 xfs_fileoff_t
*next_fsb
,
5730 xfs_fileoff_t offset_shift_fsb
,
5732 xfs_fileoff_t stop_fsb
,
5733 xfs_fsblock_t
*firstblock
,
5734 struct xfs_defer_ops
*dfops
,
5735 enum shift_direction direction
,
5738 struct xfs_btree_cur
*cur
= NULL
;
5739 struct xfs_bmbt_rec_host
*gotp
;
5740 struct xfs_bmbt_irec got
;
5741 struct xfs_mount
*mp
= ip
->i_mount
;
5742 struct xfs_ifork
*ifp
;
5743 xfs_extnum_t nexts
= 0;
5744 xfs_extnum_t current_ext
;
5745 xfs_extnum_t total_extents
;
5746 xfs_extnum_t stop_extent
;
5748 int whichfork
= XFS_DATA_FORK
;
5751 if (unlikely(XFS_TEST_ERROR(
5752 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
5753 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
5754 mp
, XFS_ERRTAG_BMAPIFORMAT
, XFS_RANDOM_BMAPIFORMAT
))) {
5755 XFS_ERROR_REPORT("xfs_bmap_shift_extents",
5756 XFS_ERRLEVEL_LOW
, mp
);
5757 return -EFSCORRUPTED
;
5760 if (XFS_FORCED_SHUTDOWN(mp
))
5763 ASSERT(xfs_isilocked(ip
, XFS_IOLOCK_EXCL
));
5764 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
5765 ASSERT(direction
== SHIFT_LEFT
|| direction
== SHIFT_RIGHT
);
5766 ASSERT(*next_fsb
!= NULLFSBLOCK
|| direction
== SHIFT_RIGHT
);
5768 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5769 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
5770 /* Read in all the extents */
5771 error
= xfs_iread_extents(tp
, ip
, whichfork
);
5776 if (ifp
->if_flags
& XFS_IFBROOT
) {
5777 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
5778 cur
->bc_private
.b
.firstblock
= *firstblock
;
5779 cur
->bc_private
.b
.dfops
= dfops
;
5780 cur
->bc_private
.b
.flags
= 0;
5784 * There may be delalloc extents in the data fork before the range we
5785 * are collapsing out, so we cannot use the count of real extents here.
5786 * Instead we have to calculate it from the incore fork.
5788 total_extents
= ifp
->if_bytes
/ sizeof(xfs_bmbt_rec_t
);
5789 if (total_extents
== 0) {
5795 * In case of first right shift, we need to initialize next_fsb
5797 if (*next_fsb
== NULLFSBLOCK
) {
5798 gotp
= xfs_iext_get_ext(ifp
, total_extents
- 1);
5799 xfs_bmbt_get_all(gotp
, &got
);
5800 *next_fsb
= got
.br_startoff
;
5801 if (stop_fsb
> *next_fsb
) {
5807 /* Lookup the extent index at which we have to stop */
5808 if (direction
== SHIFT_RIGHT
) {
5809 gotp
= xfs_iext_bno_to_ext(ifp
, stop_fsb
, &stop_extent
);
5810 /* Make stop_extent exclusive of shift range */
5813 stop_extent
= total_extents
;
5816 * Look up the extent index for the fsb where we start shifting. We can
5817 * henceforth iterate with current_ext as extent list changes are locked
5820 * gotp can be null in 2 cases: 1) if there are no extents or 2)
5821 * *next_fsb lies in a hole beyond which there are no extents. Either
5824 gotp
= xfs_iext_bno_to_ext(ifp
, *next_fsb
, ¤t_ext
);
5830 /* some sanity checking before we finally start shifting extents */
5831 if ((direction
== SHIFT_LEFT
&& current_ext
>= stop_extent
) ||
5832 (direction
== SHIFT_RIGHT
&& current_ext
<= stop_extent
)) {
5837 while (nexts
++ < num_exts
) {
5838 error
= xfs_bmse_shift_one(ip
, whichfork
, offset_shift_fsb
,
5839 ¤t_ext
, gotp
, cur
, &logflags
,
5844 * If there was an extent merge during the shift, the extent
5845 * count can change. Update the total and grade the next record.
5847 if (direction
== SHIFT_LEFT
) {
5848 total_extents
= ifp
->if_bytes
/ sizeof(xfs_bmbt_rec_t
);
5849 stop_extent
= total_extents
;
5852 if (current_ext
== stop_extent
) {
5854 *next_fsb
= NULLFSBLOCK
;
5857 gotp
= xfs_iext_get_ext(ifp
, current_ext
);
5861 xfs_bmbt_get_all(gotp
, &got
);
5862 *next_fsb
= got
.br_startoff
;
5867 xfs_btree_del_cursor(cur
,
5868 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
5871 xfs_trans_log_inode(tp
, ip
, logflags
);
5877 * Splits an extent into two extents at split_fsb block such that it is
5878 * the first block of the current_ext. @current_ext is a target extent
5879 * to be split. @split_fsb is a block where the extents is split.
5880 * If split_fsb lies in a hole or the first block of extents, just return 0.
5883 xfs_bmap_split_extent_at(
5884 struct xfs_trans
*tp
,
5885 struct xfs_inode
*ip
,
5886 xfs_fileoff_t split_fsb
,
5887 xfs_fsblock_t
*firstfsb
,
5888 struct xfs_defer_ops
*dfops
)
5890 int whichfork
= XFS_DATA_FORK
;
5891 struct xfs_btree_cur
*cur
= NULL
;
5892 struct xfs_bmbt_rec_host
*gotp
;
5893 struct xfs_bmbt_irec got
;
5894 struct xfs_bmbt_irec
new; /* split extent */
5895 struct xfs_mount
*mp
= ip
->i_mount
;
5896 struct xfs_ifork
*ifp
;
5897 xfs_fsblock_t gotblkcnt
; /* new block count for got */
5898 xfs_extnum_t current_ext
;
5903 if (unlikely(XFS_TEST_ERROR(
5904 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
5905 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
5906 mp
, XFS_ERRTAG_BMAPIFORMAT
, XFS_RANDOM_BMAPIFORMAT
))) {
5907 XFS_ERROR_REPORT("xfs_bmap_split_extent_at",
5908 XFS_ERRLEVEL_LOW
, mp
);
5909 return -EFSCORRUPTED
;
5912 if (XFS_FORCED_SHUTDOWN(mp
))
5915 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5916 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
5917 /* Read in all the extents */
5918 error
= xfs_iread_extents(tp
, ip
, whichfork
);
5924 * gotp can be null in 2 cases: 1) if there are no extents
5925 * or 2) split_fsb lies in a hole beyond which there are
5926 * no extents. Either way, we are done.
5928 gotp
= xfs_iext_bno_to_ext(ifp
, split_fsb
, ¤t_ext
);
5932 xfs_bmbt_get_all(gotp
, &got
);
5935 * Check split_fsb lies in a hole or the start boundary offset
5938 if (got
.br_startoff
>= split_fsb
)
5941 gotblkcnt
= split_fsb
- got
.br_startoff
;
5942 new.br_startoff
= split_fsb
;
5943 new.br_startblock
= got
.br_startblock
+ gotblkcnt
;
5944 new.br_blockcount
= got
.br_blockcount
- gotblkcnt
;
5945 new.br_state
= got
.br_state
;
5947 if (ifp
->if_flags
& XFS_IFBROOT
) {
5948 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
5949 cur
->bc_private
.b
.firstblock
= *firstfsb
;
5950 cur
->bc_private
.b
.dfops
= dfops
;
5951 cur
->bc_private
.b
.flags
= 0;
5952 error
= xfs_bmbt_lookup_eq(cur
, got
.br_startoff
,
5958 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, del_cursor
);
5961 xfs_bmbt_set_blockcount(gotp
, gotblkcnt
);
5962 got
.br_blockcount
= gotblkcnt
;
5964 logflags
= XFS_ILOG_CORE
;
5966 error
= xfs_bmbt_update(cur
, got
.br_startoff
,
5973 logflags
|= XFS_ILOG_DEXT
;
5975 /* Add new extent */
5977 xfs_iext_insert(ip
, current_ext
, 1, &new, 0);
5978 XFS_IFORK_NEXT_SET(ip
, whichfork
,
5979 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
5982 error
= xfs_bmbt_lookup_eq(cur
, new.br_startoff
,
5983 new.br_startblock
, new.br_blockcount
,
5987 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, del_cursor
);
5988 cur
->bc_rec
.b
.br_state
= new.br_state
;
5990 error
= xfs_btree_insert(cur
, &i
);
5993 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, del_cursor
);
5997 * Convert to a btree if necessary.
5999 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
6000 int tmp_logflags
; /* partial log flag return val */
6002 ASSERT(cur
== NULL
);
6003 error
= xfs_bmap_extents_to_btree(tp
, ip
, firstfsb
, dfops
,
6004 &cur
, 0, &tmp_logflags
, whichfork
);
6005 logflags
|= tmp_logflags
;
6010 cur
->bc_private
.b
.allocated
= 0;
6011 xfs_btree_del_cursor(cur
,
6012 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
6016 xfs_trans_log_inode(tp
, ip
, logflags
);
6021 xfs_bmap_split_extent(
6022 struct xfs_inode
*ip
,
6023 xfs_fileoff_t split_fsb
)
6025 struct xfs_mount
*mp
= ip
->i_mount
;
6026 struct xfs_trans
*tp
;
6027 struct xfs_defer_ops dfops
;
6028 xfs_fsblock_t firstfsb
;
6031 error
= xfs_trans_alloc(mp
, &M_RES(mp
)->tr_write
,
6032 XFS_DIOSTRAT_SPACE_RES(mp
, 0), 0, 0, &tp
);
6036 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
6037 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
);
6039 xfs_defer_init(&dfops
, &firstfsb
);
6041 error
= xfs_bmap_split_extent_at(tp
, ip
, split_fsb
,
6046 error
= xfs_defer_finish(&tp
, &dfops
, NULL
);
6050 return xfs_trans_commit(tp
);
6053 xfs_defer_cancel(&dfops
);
6054 xfs_trans_cancel(tp
);