2 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 #include "libxfs_priv.h"
20 #include "xfs_shared.h"
21 #include "xfs_format.h"
22 #include "xfs_log_format.h"
23 #include "xfs_trans_resv.h"
26 #include "xfs_mount.h"
27 #include "xfs_defer.h"
28 #include "xfs_da_format.h"
29 #include "xfs_da_btree.h"
31 #include "xfs_inode.h"
32 #include "xfs_btree.h"
33 #include "xfs_trans.h"
34 #include "xfs_alloc.h"
36 #include "xfs_bmap_btree.h"
37 #include "xfs_trans_space.h"
38 #include "xfs_trace.h"
39 #include "xfs_attr_leaf.h"
40 #include "xfs_quota_defs.h"
42 #include "xfs_ag_resv.h"
43 #include "xfs_refcount.h"
46 kmem_zone_t
*xfs_bmap_free_item_zone
;
49 * Miscellaneous helper functions
53 * Compute and fill in the value of the maximum depth of a bmap btree
54 * in this filesystem. Done once, during mount.
57 xfs_bmap_compute_maxlevels(
58 xfs_mount_t
*mp
, /* file system mount structure */
59 int whichfork
) /* data or attr fork */
61 int level
; /* btree level */
62 uint maxblocks
; /* max blocks at this level */
63 uint maxleafents
; /* max leaf entries possible */
64 int maxrootrecs
; /* max records in root block */
65 int minleafrecs
; /* min records in leaf block */
66 int minnoderecs
; /* min records in node block */
67 int sz
; /* root block size */
70 * The maximum number of extents in a file, hence the maximum
71 * number of leaf entries, is controlled by the type of di_nextents
72 * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
73 * (a signed 16-bit number, xfs_aextnum_t).
75 * Note that we can no longer assume that if we are in ATTR1 that
76 * the fork offset of all the inodes will be
77 * (xfs_default_attroffset(ip) >> 3) because we could have mounted
78 * with ATTR2 and then mounted back with ATTR1, keeping the
79 * di_forkoff's fixed but probably at various positions. Therefore,
80 * for both ATTR1 and ATTR2 we have to assume the worst case scenario
81 * of a minimum size available.
83 if (whichfork
== XFS_DATA_FORK
) {
84 maxleafents
= MAXEXTNUM
;
85 sz
= XFS_BMDR_SPACE_CALC(MINDBTPTRS
);
87 maxleafents
= MAXAEXTNUM
;
88 sz
= XFS_BMDR_SPACE_CALC(MINABTPTRS
);
90 maxrootrecs
= xfs_bmdr_maxrecs(sz
, 0);
91 minleafrecs
= mp
->m_bmap_dmnr
[0];
92 minnoderecs
= mp
->m_bmap_dmnr
[1];
93 maxblocks
= (maxleafents
+ minleafrecs
- 1) / minleafrecs
;
94 for (level
= 1; maxblocks
> 1; level
++) {
95 if (maxblocks
<= maxrootrecs
)
98 maxblocks
= (maxblocks
+ minnoderecs
- 1) / minnoderecs
;
100 mp
->m_bm_maxlevels
[whichfork
] = level
;
103 STATIC
int /* error */
105 struct xfs_btree_cur
*cur
,
109 int *stat
) /* success/failure */
111 cur
->bc_rec
.b
.br_startoff
= off
;
112 cur
->bc_rec
.b
.br_startblock
= bno
;
113 cur
->bc_rec
.b
.br_blockcount
= len
;
114 return xfs_btree_lookup(cur
, XFS_LOOKUP_EQ
, stat
);
117 STATIC
int /* error */
119 struct xfs_btree_cur
*cur
,
123 int *stat
) /* success/failure */
125 cur
->bc_rec
.b
.br_startoff
= off
;
126 cur
->bc_rec
.b
.br_startblock
= bno
;
127 cur
->bc_rec
.b
.br_blockcount
= len
;
128 return xfs_btree_lookup(cur
, XFS_LOOKUP_GE
, stat
);
132 * Check if the inode needs to be converted to btree format.
134 static inline bool xfs_bmap_needs_btree(struct xfs_inode
*ip
, int whichfork
)
136 return whichfork
!= XFS_COW_FORK
&&
137 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
&&
138 XFS_IFORK_NEXTENTS(ip
, whichfork
) >
139 XFS_IFORK_MAXEXT(ip
, whichfork
);
143 * Check if the inode should be converted to extent format.
145 static inline bool xfs_bmap_wants_extents(struct xfs_inode
*ip
, int whichfork
)
147 return whichfork
!= XFS_COW_FORK
&&
148 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
&&
149 XFS_IFORK_NEXTENTS(ip
, whichfork
) <=
150 XFS_IFORK_MAXEXT(ip
, whichfork
);
154 * Update the record referred to by cur to the value given
155 * by [off, bno, len, state].
156 * This either works (return 0) or gets an EFSCORRUPTED error.
160 struct xfs_btree_cur
*cur
,
166 union xfs_btree_rec rec
;
168 xfs_bmbt_disk_set_allf(&rec
.bmbt
, off
, bno
, len
, state
);
169 return xfs_btree_update(cur
, &rec
);
173 * Compute the worst-case number of indirect blocks that will be used
174 * for ip's delayed extent of length "len".
177 xfs_bmap_worst_indlen(
178 xfs_inode_t
*ip
, /* incore inode pointer */
179 xfs_filblks_t len
) /* delayed extent length */
181 int level
; /* btree level number */
182 int maxrecs
; /* maximum record count at this level */
183 xfs_mount_t
*mp
; /* mount structure */
184 xfs_filblks_t rval
; /* return value */
187 maxrecs
= mp
->m_bmap_dmxr
[0];
188 for (level
= 0, rval
= 0;
189 level
< XFS_BM_MAXLEVELS(mp
, XFS_DATA_FORK
);
192 do_div(len
, maxrecs
);
195 return rval
+ XFS_BM_MAXLEVELS(mp
, XFS_DATA_FORK
) -
198 maxrecs
= mp
->m_bmap_dmxr
[1];
204 * Calculate the default attribute fork offset for newly created inodes.
207 xfs_default_attroffset(
208 struct xfs_inode
*ip
)
210 struct xfs_mount
*mp
= ip
->i_mount
;
213 if (mp
->m_sb
.sb_inodesize
== 256) {
214 offset
= XFS_LITINO(mp
, ip
->i_d
.di_version
) -
215 XFS_BMDR_SPACE_CALC(MINABTPTRS
);
217 offset
= XFS_BMDR_SPACE_CALC(6 * MINABTPTRS
);
220 ASSERT(offset
< XFS_LITINO(mp
, ip
->i_d
.di_version
));
225 * Helper routine to reset inode di_forkoff field when switching
226 * attribute fork from local to extent format - we reset it where
227 * possible to make space available for inline data fork extents.
230 xfs_bmap_forkoff_reset(
234 if (whichfork
== XFS_ATTR_FORK
&&
235 ip
->i_d
.di_format
!= XFS_DINODE_FMT_DEV
&&
236 ip
->i_d
.di_format
!= XFS_DINODE_FMT_UUID
&&
237 ip
->i_d
.di_format
!= XFS_DINODE_FMT_BTREE
) {
238 uint dfl_forkoff
= xfs_default_attroffset(ip
) >> 3;
240 if (dfl_forkoff
> ip
->i_d
.di_forkoff
)
241 ip
->i_d
.di_forkoff
= dfl_forkoff
;
246 STATIC
struct xfs_buf
*
248 struct xfs_btree_cur
*cur
,
251 struct xfs_log_item_desc
*lidp
;
257 for (i
= 0; i
< XFS_BTREE_MAXLEVELS
; i
++) {
258 if (!cur
->bc_bufs
[i
])
260 if (XFS_BUF_ADDR(cur
->bc_bufs
[i
]) == bno
)
261 return cur
->bc_bufs
[i
];
264 /* Chase down all the log items to see if the bp is there */
265 list_for_each_entry(lidp
, &cur
->bc_tp
->t_items
, lid_trans
) {
266 struct xfs_buf_log_item
*bip
;
267 bip
= (struct xfs_buf_log_item
*)lidp
->lid_item
;
268 if (bip
->bli_item
.li_type
== XFS_LI_BUF
&&
269 XFS_BUF_ADDR(bip
->bli_buf
) == bno
)
278 struct xfs_btree_block
*block
,
284 __be64
*pp
, *thispa
; /* pointer to block address */
285 xfs_bmbt_key_t
*prevp
, *keyp
;
287 ASSERT(be16_to_cpu(block
->bb_level
) > 0);
290 for( i
= 1; i
<= xfs_btree_get_numrecs(block
); i
++) {
291 dmxr
= mp
->m_bmap_dmxr
[0];
292 keyp
= XFS_BMBT_KEY_ADDR(mp
, block
, i
);
295 ASSERT(be64_to_cpu(prevp
->br_startoff
) <
296 be64_to_cpu(keyp
->br_startoff
));
301 * Compare the block numbers to see if there are dups.
304 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, i
, sz
);
306 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, i
, dmxr
);
308 for (j
= i
+1; j
<= be16_to_cpu(block
->bb_numrecs
); j
++) {
310 thispa
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, j
, sz
);
312 thispa
= XFS_BMBT_PTR_ADDR(mp
, block
, j
, dmxr
);
313 if (*thispa
== *pp
) {
314 xfs_warn(mp
, "%s: thispa(%d) == pp(%d) %Ld",
316 (unsigned long long)be64_to_cpu(*thispa
));
317 panic("%s: ptrs are equal in node\n",
325 * Check that the extents for the inode ip are in the right order in all
326 * btree leaves. THis becomes prohibitively expensive for large extent count
327 * files, so don't bother with inodes that have more than 10,000 extents in
328 * them. The btree record ordering checks will still be done, so for such large
329 * bmapbt constructs that is going to catch most corruptions.
332 xfs_bmap_check_leaf_extents(
333 xfs_btree_cur_t
*cur
, /* btree cursor or null */
334 xfs_inode_t
*ip
, /* incore inode pointer */
335 int whichfork
) /* data or attr fork */
337 struct xfs_btree_block
*block
; /* current btree block */
338 xfs_fsblock_t bno
; /* block # of "block" */
339 xfs_buf_t
*bp
; /* buffer for "block" */
340 int error
; /* error return value */
341 xfs_extnum_t i
=0, j
; /* index into the extents list */
342 xfs_ifork_t
*ifp
; /* fork structure */
343 int level
; /* btree level, for checking */
344 xfs_mount_t
*mp
; /* file system mount structure */
345 __be64
*pp
; /* pointer to block address */
346 xfs_bmbt_rec_t
*ep
; /* pointer to current extent */
347 xfs_bmbt_rec_t last
= {0, 0}; /* last extent in prev block */
348 xfs_bmbt_rec_t
*nextp
; /* pointer to next extent */
351 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
) {
355 /* skip large extent count inodes */
356 if (ip
->i_d
.di_nextents
> 10000)
361 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
362 block
= ifp
->if_broot
;
364 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
366 level
= be16_to_cpu(block
->bb_level
);
368 xfs_check_block(block
, mp
, 1, ifp
->if_broot_bytes
);
369 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, 1, ifp
->if_broot_bytes
);
370 bno
= be64_to_cpu(*pp
);
372 ASSERT(bno
!= NULLFSBLOCK
);
373 ASSERT(XFS_FSB_TO_AGNO(mp
, bno
) < mp
->m_sb
.sb_agcount
);
374 ASSERT(XFS_FSB_TO_AGBNO(mp
, bno
) < mp
->m_sb
.sb_agblocks
);
377 * Go down the tree until leaf level is reached, following the first
378 * pointer (leftmost) at each level.
380 while (level
-- > 0) {
381 /* See if buf is in cur first */
383 bp
= xfs_bmap_get_bp(cur
, XFS_FSB_TO_DADDR(mp
, bno
));
386 error
= xfs_btree_read_bufl(mp
, NULL
, bno
, 0, &bp
,
392 block
= XFS_BUF_TO_BLOCK(bp
);
397 * Check this block for basic sanity (increasing keys and
398 * no duplicate blocks).
401 xfs_check_block(block
, mp
, 0, 0);
402 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, mp
->m_bmap_dmxr
[1]);
403 bno
= be64_to_cpu(*pp
);
404 XFS_WANT_CORRUPTED_GOTO(mp
,
405 XFS_FSB_SANITY_CHECK(mp
, bno
), error0
);
408 xfs_trans_brelse(NULL
, bp
);
413 * Here with bp and block set to the leftmost leaf node in the tree.
418 * Loop over all leaf nodes checking that all extents are in the right order.
421 xfs_fsblock_t nextbno
;
422 xfs_extnum_t num_recs
;
425 num_recs
= xfs_btree_get_numrecs(block
);
428 * Read-ahead the next leaf block, if any.
431 nextbno
= be64_to_cpu(block
->bb_u
.l
.bb_rightsib
);
434 * Check all the extents to make sure they are OK.
435 * If we had a previous block, the last entry should
436 * conform with the first entry in this one.
439 ep
= XFS_BMBT_REC_ADDR(mp
, block
, 1);
441 ASSERT(xfs_bmbt_disk_get_startoff(&last
) +
442 xfs_bmbt_disk_get_blockcount(&last
) <=
443 xfs_bmbt_disk_get_startoff(ep
));
445 for (j
= 1; j
< num_recs
; j
++) {
446 nextp
= XFS_BMBT_REC_ADDR(mp
, block
, j
+ 1);
447 ASSERT(xfs_bmbt_disk_get_startoff(ep
) +
448 xfs_bmbt_disk_get_blockcount(ep
) <=
449 xfs_bmbt_disk_get_startoff(nextp
));
457 xfs_trans_brelse(NULL
, bp
);
461 * If we've reached the end, stop.
463 if (bno
== NULLFSBLOCK
)
467 bp
= xfs_bmap_get_bp(cur
, XFS_FSB_TO_DADDR(mp
, bno
));
470 error
= xfs_btree_read_bufl(mp
, NULL
, bno
, 0, &bp
,
476 block
= XFS_BUF_TO_BLOCK(bp
);
482 xfs_warn(mp
, "%s: at error0", __func__
);
484 xfs_trans_brelse(NULL
, bp
);
486 xfs_warn(mp
, "%s: BAD after btree leaves for %d extents",
488 panic("%s: CORRUPTED BTREE OR SOMETHING", __func__
);
493 * Add bmap trace insert entries for all the contents of the extent records.
496 xfs_bmap_trace_exlist(
497 xfs_inode_t
*ip
, /* incore inode pointer */
498 xfs_extnum_t cnt
, /* count of entries in the list */
499 int whichfork
, /* data or attr or cow fork */
500 unsigned long caller_ip
)
502 xfs_extnum_t idx
; /* extent record index */
503 xfs_ifork_t
*ifp
; /* inode fork pointer */
506 if (whichfork
== XFS_ATTR_FORK
)
507 state
|= BMAP_ATTRFORK
;
508 else if (whichfork
== XFS_COW_FORK
)
509 state
|= BMAP_COWFORK
;
511 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
512 ASSERT(cnt
== xfs_iext_count(ifp
));
513 for (idx
= 0; idx
< cnt
; idx
++)
514 trace_xfs_extlist(ip
, idx
, state
, caller_ip
);
518 * Validate that the bmbt_irecs being returned from bmapi are valid
519 * given the caller's original parameters. Specifically check the
520 * ranges of the returned irecs to ensure that they only extend beyond
521 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
524 xfs_bmap_validate_ret(
528 xfs_bmbt_irec_t
*mval
,
532 int i
; /* index to map values */
534 ASSERT(ret_nmap
<= nmap
);
536 for (i
= 0; i
< ret_nmap
; i
++) {
537 ASSERT(mval
[i
].br_blockcount
> 0);
538 if (!(flags
& XFS_BMAPI_ENTIRE
)) {
539 ASSERT(mval
[i
].br_startoff
>= bno
);
540 ASSERT(mval
[i
].br_blockcount
<= len
);
541 ASSERT(mval
[i
].br_startoff
+ mval
[i
].br_blockcount
<=
544 ASSERT(mval
[i
].br_startoff
< bno
+ len
);
545 ASSERT(mval
[i
].br_startoff
+ mval
[i
].br_blockcount
>
549 mval
[i
- 1].br_startoff
+ mval
[i
- 1].br_blockcount
==
550 mval
[i
].br_startoff
);
551 ASSERT(mval
[i
].br_startblock
!= DELAYSTARTBLOCK
&&
552 mval
[i
].br_startblock
!= HOLESTARTBLOCK
);
553 ASSERT(mval
[i
].br_state
== XFS_EXT_NORM
||
554 mval
[i
].br_state
== XFS_EXT_UNWRITTEN
);
559 #define xfs_bmap_check_leaf_extents(cur, ip, whichfork) do { } while (0)
560 #define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap) do { } while (0)
564 * bmap free list manipulation functions
568 * Add the extent to the list of extents to be free at transaction end.
569 * The list is maintained sorted (by block number).
573 struct xfs_mount
*mp
,
574 struct xfs_defer_ops
*dfops
,
577 struct xfs_owner_info
*oinfo
)
579 struct xfs_extent_free_item
*new; /* new element */
584 ASSERT(bno
!= NULLFSBLOCK
);
586 ASSERT(len
<= MAXEXTLEN
);
587 ASSERT(!isnullstartblock(bno
));
588 agno
= XFS_FSB_TO_AGNO(mp
, bno
);
589 agbno
= XFS_FSB_TO_AGBNO(mp
, bno
);
590 ASSERT(agno
< mp
->m_sb
.sb_agcount
);
591 ASSERT(agbno
< mp
->m_sb
.sb_agblocks
);
592 ASSERT(len
< mp
->m_sb
.sb_agblocks
);
593 ASSERT(agbno
+ len
<= mp
->m_sb
.sb_agblocks
);
595 ASSERT(xfs_bmap_free_item_zone
!= NULL
);
597 new = kmem_zone_alloc(xfs_bmap_free_item_zone
, KM_SLEEP
);
598 new->xefi_startblock
= bno
;
599 new->xefi_blockcount
= (xfs_extlen_t
)len
;
601 new->xefi_oinfo
= *oinfo
;
603 xfs_rmap_skip_owner_update(&new->xefi_oinfo
);
604 trace_xfs_bmap_free_defer(mp
, XFS_FSB_TO_AGNO(mp
, bno
), 0,
605 XFS_FSB_TO_AGBNO(mp
, bno
), len
);
606 xfs_defer_add(dfops
, XFS_DEFER_OPS_TYPE_FREE
, &new->xefi_list
);
610 * Inode fork format manipulation functions
614 * Transform a btree format file with only one leaf node, where the
615 * extents list will fit in the inode, into an extents format file.
616 * Since the file extents are already in-core, all we have to do is
617 * give up the space for the btree root and pitch the leaf block.
619 STATIC
int /* error */
620 xfs_bmap_btree_to_extents(
621 xfs_trans_t
*tp
, /* transaction pointer */
622 xfs_inode_t
*ip
, /* incore inode pointer */
623 xfs_btree_cur_t
*cur
, /* btree cursor */
624 int *logflagsp
, /* inode logging flags */
625 int whichfork
) /* data or attr fork */
628 struct xfs_btree_block
*cblock
;/* child btree block */
629 xfs_fsblock_t cbno
; /* child block number */
630 xfs_buf_t
*cbp
; /* child block's buffer */
631 int error
; /* error return value */
632 xfs_ifork_t
*ifp
; /* inode fork data */
633 xfs_mount_t
*mp
; /* mount point structure */
634 __be64
*pp
; /* ptr to block address */
635 struct xfs_btree_block
*rblock
;/* root btree block */
636 struct xfs_owner_info oinfo
;
639 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
640 ASSERT(whichfork
!= XFS_COW_FORK
);
641 ASSERT(ifp
->if_flags
& XFS_IFEXTENTS
);
642 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
);
643 rblock
= ifp
->if_broot
;
644 ASSERT(be16_to_cpu(rblock
->bb_level
) == 1);
645 ASSERT(be16_to_cpu(rblock
->bb_numrecs
) == 1);
646 ASSERT(xfs_bmbt_maxrecs(mp
, ifp
->if_broot_bytes
, 0) == 1);
647 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, rblock
, 1, ifp
->if_broot_bytes
);
648 cbno
= be64_to_cpu(*pp
);
651 if ((error
= xfs_btree_check_lptr(cur
, cbno
, 1)))
654 error
= xfs_btree_read_bufl(mp
, tp
, cbno
, 0, &cbp
, XFS_BMAP_BTREE_REF
,
658 cblock
= XFS_BUF_TO_BLOCK(cbp
);
659 if ((error
= xfs_btree_check_block(cur
, cblock
, 0, cbp
)))
661 xfs_rmap_ino_bmbt_owner(&oinfo
, ip
->i_ino
, whichfork
);
662 xfs_bmap_add_free(mp
, cur
->bc_private
.b
.dfops
, cbno
, 1, &oinfo
);
663 ip
->i_d
.di_nblocks
--;
664 xfs_trans_mod_dquot_byino(tp
, ip
, XFS_TRANS_DQ_BCOUNT
, -1L);
665 xfs_trans_binval(tp
, cbp
);
666 if (cur
->bc_bufs
[0] == cbp
)
667 cur
->bc_bufs
[0] = NULL
;
668 xfs_iroot_realloc(ip
, -1, whichfork
);
669 ASSERT(ifp
->if_broot
== NULL
);
670 ASSERT((ifp
->if_flags
& XFS_IFBROOT
) == 0);
671 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_EXTENTS
);
672 *logflagsp
= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
677 * Convert an extents-format file into a btree-format file.
678 * The new file will have a root block (in the inode) and a single child block.
680 STATIC
int /* error */
681 xfs_bmap_extents_to_btree(
682 xfs_trans_t
*tp
, /* transaction pointer */
683 xfs_inode_t
*ip
, /* incore inode pointer */
684 xfs_fsblock_t
*firstblock
, /* first-block-allocated */
685 struct xfs_defer_ops
*dfops
, /* blocks freed in xaction */
686 xfs_btree_cur_t
**curp
, /* cursor returned to caller */
687 int wasdel
, /* converting a delayed alloc */
688 int *logflagsp
, /* inode logging flags */
689 int whichfork
) /* data or attr fork */
691 struct xfs_btree_block
*ablock
; /* allocated (child) bt block */
692 xfs_buf_t
*abp
; /* buffer for ablock */
693 xfs_alloc_arg_t args
; /* allocation arguments */
694 xfs_bmbt_rec_t
*arp
; /* child record pointer */
695 struct xfs_btree_block
*block
; /* btree root block */
696 xfs_btree_cur_t
*cur
; /* bmap btree cursor */
697 xfs_bmbt_rec_host_t
*ep
; /* extent record pointer */
698 int error
; /* error return value */
699 xfs_extnum_t i
, cnt
; /* extent record index */
700 xfs_ifork_t
*ifp
; /* inode fork pointer */
701 xfs_bmbt_key_t
*kp
; /* root block key pointer */
702 xfs_mount_t
*mp
; /* mount structure */
703 xfs_extnum_t nextents
; /* number of file extents */
704 xfs_bmbt_ptr_t
*pp
; /* root block address pointer */
707 ASSERT(whichfork
!= XFS_COW_FORK
);
708 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
709 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
);
712 * Make space in the inode incore.
714 xfs_iroot_realloc(ip
, 1, whichfork
);
715 ifp
->if_flags
|= XFS_IFBROOT
;
720 block
= ifp
->if_broot
;
721 xfs_btree_init_block_int(mp
, block
, XFS_BUF_DADDR_NULL
,
722 XFS_BTNUM_BMAP
, 1, 1, ip
->i_ino
,
723 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 if (WARN_ON_ONCE(args
.fsbno
== NULLFSBLOCK
)) {
760 xfs_iroot_realloc(ip
, -1, whichfork
);
761 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
765 * Allocation can't fail, the space was reserved.
767 ASSERT(*firstblock
== NULLFSBLOCK
||
768 args
.agno
>= XFS_FSB_TO_AGNO(mp
, *firstblock
));
769 *firstblock
= cur
->bc_private
.b
.firstblock
= args
.fsbno
;
770 cur
->bc_private
.b
.allocated
++;
771 ip
->i_d
.di_nblocks
++;
772 xfs_trans_mod_dquot_byino(tp
, ip
, XFS_TRANS_DQ_BCOUNT
, 1L);
773 abp
= xfs_btree_get_bufl(mp
, tp
, args
.fsbno
, 0);
775 * Fill in the child block.
777 abp
->b_ops
= &xfs_bmbt_buf_ops
;
778 ablock
= XFS_BUF_TO_BLOCK(abp
);
779 xfs_btree_init_block_int(mp
, ablock
, abp
->b_bn
,
780 XFS_BTNUM_BMAP
, 0, 0, ip
->i_ino
,
781 XFS_BTREE_LONG_PTRS
);
783 arp
= XFS_BMBT_REC_ADDR(mp
, ablock
, 1);
784 nextents
= xfs_iext_count(ifp
);
785 for (cnt
= i
= 0; i
< nextents
; i
++) {
786 ep
= xfs_iext_get_ext(ifp
, i
);
787 if (!isnullstartblock(xfs_bmbt_get_startblock(ep
))) {
788 arp
->l0
= cpu_to_be64(ep
->l0
);
789 arp
->l1
= cpu_to_be64(ep
->l1
);
793 ASSERT(cnt
== XFS_IFORK_NEXTENTS(ip
, whichfork
));
794 xfs_btree_set_numrecs(ablock
, cnt
);
797 * Fill in the root key and pointer.
799 kp
= XFS_BMBT_KEY_ADDR(mp
, block
, 1);
800 arp
= XFS_BMBT_REC_ADDR(mp
, ablock
, 1);
801 kp
->br_startoff
= cpu_to_be64(xfs_bmbt_disk_get_startoff(arp
));
802 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, xfs_bmbt_get_maxrecs(cur
,
803 be16_to_cpu(block
->bb_level
)));
804 *pp
= cpu_to_be64(args
.fsbno
);
807 * Do all this logging at the end so that
808 * the root is at the right level.
810 xfs_btree_log_block(cur
, abp
, XFS_BB_ALL_BITS
);
811 xfs_btree_log_recs(cur
, abp
, 1, be16_to_cpu(ablock
->bb_numrecs
));
812 ASSERT(*curp
== NULL
);
814 *logflagsp
= XFS_ILOG_CORE
| xfs_ilog_fbroot(whichfork
);
819 * Convert a local file to an extents file.
820 * This code is out of bounds for data forks of regular files,
821 * since the file data needs to get logged so things will stay consistent.
822 * (The bmap-level manipulations are ok, though).
825 xfs_bmap_local_to_extents_empty(
826 struct xfs_inode
*ip
,
829 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
831 ASSERT(whichfork
!= XFS_COW_FORK
);
832 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
833 ASSERT(ifp
->if_bytes
== 0);
834 ASSERT(XFS_IFORK_NEXTENTS(ip
, whichfork
) == 0);
836 xfs_bmap_forkoff_reset(ip
, whichfork
);
837 ifp
->if_flags
&= ~XFS_IFINLINE
;
838 ifp
->if_flags
|= XFS_IFEXTENTS
;
839 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_EXTENTS
);
843 STATIC
int /* error */
844 xfs_bmap_local_to_extents(
845 xfs_trans_t
*tp
, /* transaction pointer */
846 xfs_inode_t
*ip
, /* incore inode pointer */
847 xfs_fsblock_t
*firstblock
, /* first block allocated in xaction */
848 xfs_extlen_t total
, /* total blocks needed by transaction */
849 int *logflagsp
, /* inode logging flags */
851 void (*init_fn
)(struct xfs_trans
*tp
,
853 struct xfs_inode
*ip
,
854 struct xfs_ifork
*ifp
))
857 int flags
; /* logging flags returned */
858 xfs_ifork_t
*ifp
; /* inode fork pointer */
859 xfs_alloc_arg_t args
; /* allocation arguments */
860 xfs_buf_t
*bp
; /* buffer for extent block */
861 struct xfs_bmbt_irec rec
;
864 * We don't want to deal with the case of keeping inode data inline yet.
865 * So sending the data fork of a regular inode is invalid.
867 ASSERT(!(S_ISREG(VFS_I(ip
)->i_mode
) && whichfork
== XFS_DATA_FORK
));
868 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
869 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
871 if (!ifp
->if_bytes
) {
872 xfs_bmap_local_to_extents_empty(ip
, whichfork
);
873 flags
= XFS_ILOG_CORE
;
879 ASSERT((ifp
->if_flags
& (XFS_IFINLINE
|XFS_IFEXTENTS
|XFS_IFEXTIREC
)) ==
881 memset(&args
, 0, sizeof(args
));
883 args
.mp
= ip
->i_mount
;
884 xfs_rmap_ino_owner(&args
.oinfo
, ip
->i_ino
, whichfork
, 0);
885 args
.firstblock
= *firstblock
;
887 * Allocate a block. We know we need only one, since the
888 * file currently fits in an inode.
890 if (*firstblock
== NULLFSBLOCK
) {
891 args
.fsbno
= XFS_INO_TO_FSB(args
.mp
, ip
->i_ino
);
892 args
.type
= XFS_ALLOCTYPE_START_BNO
;
894 args
.fsbno
= *firstblock
;
895 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
898 args
.minlen
= args
.maxlen
= args
.prod
= 1;
899 error
= xfs_alloc_vextent(&args
);
903 /* Can't fail, the space was reserved. */
904 ASSERT(args
.fsbno
!= NULLFSBLOCK
);
905 ASSERT(args
.len
== 1);
906 *firstblock
= args
.fsbno
;
907 bp
= xfs_btree_get_bufl(args
.mp
, tp
, args
.fsbno
, 0);
910 * Initialize the block, copy the data and log the remote buffer.
912 * The callout is responsible for logging because the remote format
913 * might differ from the local format and thus we don't know how much to
914 * log here. Note that init_fn must also set the buffer log item type
917 init_fn(tp
, bp
, ip
, ifp
);
919 /* account for the change in fork size */
920 xfs_idata_realloc(ip
, -ifp
->if_bytes
, whichfork
);
921 xfs_bmap_local_to_extents_empty(ip
, whichfork
);
922 flags
|= XFS_ILOG_CORE
;
925 rec
.br_startblock
= args
.fsbno
;
926 rec
.br_blockcount
= 1;
927 rec
.br_state
= XFS_EXT_NORM
;
928 xfs_iext_insert(ip
, 0, 1, &rec
, 0);
930 trace_xfs_bmap_post_update(ip
, 0,
931 whichfork
== XFS_ATTR_FORK
? BMAP_ATTRFORK
: 0,
933 XFS_IFORK_NEXT_SET(ip
, whichfork
, 1);
934 ip
->i_d
.di_nblocks
= 1;
935 xfs_trans_mod_dquot_byino(tp
, ip
,
936 XFS_TRANS_DQ_BCOUNT
, 1L);
937 flags
|= xfs_ilog_fext(whichfork
);
945 * Called from xfs_bmap_add_attrfork to handle btree format files.
947 STATIC
int /* error */
948 xfs_bmap_add_attrfork_btree(
949 xfs_trans_t
*tp
, /* transaction pointer */
950 xfs_inode_t
*ip
, /* incore inode pointer */
951 xfs_fsblock_t
*firstblock
, /* first block allocated */
952 struct xfs_defer_ops
*dfops
, /* blocks to free at commit */
953 int *flags
) /* inode logging flags */
955 xfs_btree_cur_t
*cur
; /* btree cursor */
956 int error
; /* error return value */
957 xfs_mount_t
*mp
; /* file system mount struct */
958 int stat
; /* newroot status */
961 if (ip
->i_df
.if_broot_bytes
<= XFS_IFORK_DSIZE(ip
))
962 *flags
|= XFS_ILOG_DBROOT
;
964 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, XFS_DATA_FORK
);
965 cur
->bc_private
.b
.dfops
= dfops
;
966 cur
->bc_private
.b
.firstblock
= *firstblock
;
967 if ((error
= xfs_bmbt_lookup_ge(cur
, 0, 0, 0, &stat
)))
969 /* must be at least one entry */
970 XFS_WANT_CORRUPTED_GOTO(mp
, stat
== 1, error0
);
971 if ((error
= xfs_btree_new_iroot(cur
, flags
, &stat
)))
974 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
977 *firstblock
= cur
->bc_private
.b
.firstblock
;
978 cur
->bc_private
.b
.allocated
= 0;
979 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
983 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
988 * Called from xfs_bmap_add_attrfork to handle extents format files.
990 STATIC
int /* error */
991 xfs_bmap_add_attrfork_extents(
992 xfs_trans_t
*tp
, /* transaction pointer */
993 xfs_inode_t
*ip
, /* incore inode pointer */
994 xfs_fsblock_t
*firstblock
, /* first block allocated */
995 struct xfs_defer_ops
*dfops
, /* blocks to free at commit */
996 int *flags
) /* inode logging flags */
998 xfs_btree_cur_t
*cur
; /* bmap btree cursor */
999 int error
; /* error return value */
1001 if (ip
->i_d
.di_nextents
* sizeof(xfs_bmbt_rec_t
) <= XFS_IFORK_DSIZE(ip
))
1004 error
= xfs_bmap_extents_to_btree(tp
, ip
, firstblock
, dfops
, &cur
, 0,
1005 flags
, XFS_DATA_FORK
);
1007 cur
->bc_private
.b
.allocated
= 0;
1008 xfs_btree_del_cursor(cur
,
1009 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
1015 * Called from xfs_bmap_add_attrfork to handle local format files. Each
1016 * different data fork content type needs a different callout to do the
1017 * conversion. Some are basic and only require special block initialisation
1018 * callouts for the data formating, others (directories) are so specialised they
1019 * handle everything themselves.
1021 * XXX (dgc): investigate whether directory conversion can use the generic
1022 * formatting callout. It should be possible - it's just a very complex
1025 STATIC
int /* error */
1026 xfs_bmap_add_attrfork_local(
1027 xfs_trans_t
*tp
, /* transaction pointer */
1028 xfs_inode_t
*ip
, /* incore inode pointer */
1029 xfs_fsblock_t
*firstblock
, /* first block allocated */
1030 struct xfs_defer_ops
*dfops
, /* blocks to free at commit */
1031 int *flags
) /* inode logging flags */
1033 xfs_da_args_t dargs
; /* args for dir/attr code */
1035 if (ip
->i_df
.if_bytes
<= XFS_IFORK_DSIZE(ip
))
1038 if (S_ISDIR(VFS_I(ip
)->i_mode
)) {
1039 memset(&dargs
, 0, sizeof(dargs
));
1040 dargs
.geo
= ip
->i_mount
->m_dir_geo
;
1042 dargs
.firstblock
= firstblock
;
1043 dargs
.dfops
= dfops
;
1044 dargs
.total
= dargs
.geo
->fsbcount
;
1045 dargs
.whichfork
= XFS_DATA_FORK
;
1047 return xfs_dir2_sf_to_block(&dargs
);
1050 if (S_ISLNK(VFS_I(ip
)->i_mode
))
1051 return xfs_bmap_local_to_extents(tp
, ip
, firstblock
, 1,
1052 flags
, XFS_DATA_FORK
,
1053 xfs_symlink_local_to_remote
);
1055 /* should only be called for types that support local format data */
1057 return -EFSCORRUPTED
;
1061 * Convert inode from non-attributed to attributed.
1062 * Must not be in a transaction, ip must not be locked.
1064 int /* error code */
1065 xfs_bmap_add_attrfork(
1066 xfs_inode_t
*ip
, /* incore inode pointer */
1067 int size
, /* space new attribute needs */
1068 int rsvd
) /* xact may use reserved blks */
1070 xfs_fsblock_t firstblock
; /* 1st block/ag allocated */
1071 struct xfs_defer_ops dfops
; /* freed extent records */
1072 xfs_mount_t
*mp
; /* mount structure */
1073 xfs_trans_t
*tp
; /* transaction pointer */
1074 int blks
; /* space reservation */
1075 int version
= 1; /* superblock attr version */
1076 int logflags
; /* logging flags */
1077 int error
; /* error return value */
1079 ASSERT(XFS_IFORK_Q(ip
) == 0);
1082 ASSERT(!XFS_NOT_DQATTACHED(mp
, ip
));
1084 blks
= XFS_ADDAFORK_SPACE_RES(mp
);
1086 error
= xfs_trans_alloc(mp
, &M_RES(mp
)->tr_addafork
, blks
, 0,
1087 rsvd
? XFS_TRANS_RESERVE
: 0, &tp
);
1091 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
1092 error
= xfs_trans_reserve_quota_nblks(tp
, ip
, blks
, 0, rsvd
?
1093 XFS_QMOPT_RES_REGBLKS
| XFS_QMOPT_FORCE_RES
:
1094 XFS_QMOPT_RES_REGBLKS
);
1097 if (XFS_IFORK_Q(ip
))
1099 if (ip
->i_d
.di_anextents
!= 0) {
1100 error
= -EFSCORRUPTED
;
1103 if (ip
->i_d
.di_aformat
!= XFS_DINODE_FMT_EXTENTS
) {
1105 * For inodes coming from pre-6.2 filesystems.
1107 ASSERT(ip
->i_d
.di_aformat
== 0);
1108 ip
->i_d
.di_aformat
= XFS_DINODE_FMT_EXTENTS
;
1111 xfs_trans_ijoin(tp
, ip
, 0);
1112 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
1114 switch (ip
->i_d
.di_format
) {
1115 case XFS_DINODE_FMT_DEV
:
1116 ip
->i_d
.di_forkoff
= roundup(sizeof(xfs_dev_t
), 8) >> 3;
1118 case XFS_DINODE_FMT_UUID
:
1119 ip
->i_d
.di_forkoff
= roundup(sizeof(uuid_t
), 8) >> 3;
1121 case XFS_DINODE_FMT_LOCAL
:
1122 case XFS_DINODE_FMT_EXTENTS
:
1123 case XFS_DINODE_FMT_BTREE
:
1124 ip
->i_d
.di_forkoff
= xfs_attr_shortform_bytesfit(ip
, size
);
1125 if (!ip
->i_d
.di_forkoff
)
1126 ip
->i_d
.di_forkoff
= xfs_default_attroffset(ip
) >> 3;
1127 else if (mp
->m_flags
& XFS_MOUNT_ATTR2
)
1136 ASSERT(ip
->i_afp
== NULL
);
1137 ip
->i_afp
= kmem_zone_zalloc(xfs_ifork_zone
, KM_SLEEP
);
1138 ip
->i_afp
->if_flags
= XFS_IFEXTENTS
;
1140 xfs_defer_init(&dfops
, &firstblock
);
1141 switch (ip
->i_d
.di_format
) {
1142 case XFS_DINODE_FMT_LOCAL
:
1143 error
= xfs_bmap_add_attrfork_local(tp
, ip
, &firstblock
, &dfops
,
1146 case XFS_DINODE_FMT_EXTENTS
:
1147 error
= xfs_bmap_add_attrfork_extents(tp
, ip
, &firstblock
,
1150 case XFS_DINODE_FMT_BTREE
:
1151 error
= xfs_bmap_add_attrfork_btree(tp
, ip
, &firstblock
, &dfops
,
1159 xfs_trans_log_inode(tp
, ip
, logflags
);
1162 if (!xfs_sb_version_hasattr(&mp
->m_sb
) ||
1163 (!xfs_sb_version_hasattr2(&mp
->m_sb
) && version
== 2)) {
1164 bool log_sb
= false;
1166 spin_lock(&mp
->m_sb_lock
);
1167 if (!xfs_sb_version_hasattr(&mp
->m_sb
)) {
1168 xfs_sb_version_addattr(&mp
->m_sb
);
1171 if (!xfs_sb_version_hasattr2(&mp
->m_sb
) && version
== 2) {
1172 xfs_sb_version_addattr2(&mp
->m_sb
);
1175 spin_unlock(&mp
->m_sb_lock
);
1180 error
= xfs_defer_finish(&tp
, &dfops
);
1183 error
= xfs_trans_commit(tp
);
1184 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
1188 xfs_defer_cancel(&dfops
);
1190 xfs_trans_cancel(tp
);
1191 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
1196 * Internal and external extent tree search functions.
1200 * Read in the extents to if_extents.
1201 * All inode fields are set up by caller, we just traverse the btree
1202 * and copy the records in. If the file system cannot contain unwritten
1203 * extents, the records are checked for no "state" flags.
1206 xfs_bmap_read_extents(
1207 xfs_trans_t
*tp
, /* transaction pointer */
1208 xfs_inode_t
*ip
, /* incore inode */
1209 int whichfork
) /* data or attr fork */
1211 struct xfs_btree_block
*block
; /* current btree block */
1212 xfs_fsblock_t bno
; /* block # of "block" */
1213 xfs_buf_t
*bp
; /* buffer for "block" */
1214 int error
; /* error return value */
1215 xfs_extnum_t i
, j
; /* index into the extents list */
1216 xfs_ifork_t
*ifp
; /* fork structure */
1217 int level
; /* btree level, for checking */
1218 xfs_mount_t
*mp
; /* file system mount structure */
1219 __be64
*pp
; /* pointer to block address */
1221 xfs_extnum_t room
; /* number of entries there's room for */
1224 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1225 block
= ifp
->if_broot
;
1227 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
1229 level
= be16_to_cpu(block
->bb_level
);
1231 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, 1, ifp
->if_broot_bytes
);
1232 bno
= be64_to_cpu(*pp
);
1235 * Go down the tree until leaf level is reached, following the first
1236 * pointer (leftmost) at each level.
1238 while (level
-- > 0) {
1239 error
= xfs_btree_read_bufl(mp
, tp
, bno
, 0, &bp
,
1240 XFS_BMAP_BTREE_REF
, &xfs_bmbt_buf_ops
);
1243 block
= XFS_BUF_TO_BLOCK(bp
);
1246 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, mp
->m_bmap_dmxr
[1]);
1247 bno
= be64_to_cpu(*pp
);
1248 XFS_WANT_CORRUPTED_GOTO(mp
,
1249 XFS_FSB_SANITY_CHECK(mp
, bno
), error0
);
1250 xfs_trans_brelse(tp
, bp
);
1253 * Here with bp and block set to the leftmost leaf node in the tree.
1255 room
= xfs_iext_count(ifp
);
1258 * Loop over all leaf nodes. Copy information to the extent records.
1261 xfs_bmbt_rec_t
*frp
;
1262 xfs_fsblock_t nextbno
;
1263 xfs_extnum_t num_recs
;
1265 num_recs
= xfs_btree_get_numrecs(block
);
1266 if (unlikely(i
+ num_recs
> room
)) {
1267 ASSERT(i
+ num_recs
<= room
);
1268 xfs_warn(ip
->i_mount
,
1269 "corrupt dinode %Lu, (btree extents).",
1270 (unsigned long long) ip
->i_ino
);
1271 XFS_CORRUPTION_ERROR("xfs_bmap_read_extents(1)",
1272 XFS_ERRLEVEL_LOW
, ip
->i_mount
, block
);
1276 * Read-ahead the next leaf block, if any.
1278 nextbno
= be64_to_cpu(block
->bb_u
.l
.bb_rightsib
);
1279 if (nextbno
!= NULLFSBLOCK
)
1280 xfs_btree_reada_bufl(mp
, nextbno
, 1,
1283 * Copy records into the extent records.
1285 frp
= XFS_BMBT_REC_ADDR(mp
, block
, 1);
1286 for (j
= 0; j
< num_recs
; j
++, i
++, frp
++) {
1287 xfs_bmbt_rec_host_t
*trp
= xfs_iext_get_ext(ifp
, i
);
1288 trp
->l0
= be64_to_cpu(frp
->l0
);
1289 trp
->l1
= be64_to_cpu(frp
->l1
);
1290 if (!xfs_bmbt_validate_extent(mp
, whichfork
, trp
)) {
1291 XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
1292 XFS_ERRLEVEL_LOW
, mp
);
1296 xfs_trans_brelse(tp
, bp
);
1299 * If we've reached the end, stop.
1301 if (bno
== NULLFSBLOCK
)
1303 error
= xfs_btree_read_bufl(mp
, tp
, bno
, 0, &bp
,
1304 XFS_BMAP_BTREE_REF
, &xfs_bmbt_buf_ops
);
1307 block
= XFS_BUF_TO_BLOCK(bp
);
1309 if (i
!= XFS_IFORK_NEXTENTS(ip
, whichfork
))
1310 return -EFSCORRUPTED
;
1311 ASSERT(i
== xfs_iext_count(ifp
));
1312 XFS_BMAP_TRACE_EXLIST(ip
, i
, whichfork
);
1315 xfs_trans_brelse(tp
, bp
);
1316 return -EFSCORRUPTED
;
1320 * Returns the file-relative block number of the first unused block(s)
1321 * in the file with at least "len" logically contiguous blocks free.
1322 * This is the lowest-address hole if the file has holes, else the first block
1323 * past the end of file.
1324 * Return 0 if the file is currently local (in-inode).
1327 xfs_bmap_first_unused(
1328 xfs_trans_t
*tp
, /* transaction pointer */
1329 xfs_inode_t
*ip
, /* incore inode */
1330 xfs_extlen_t len
, /* size of hole to find */
1331 xfs_fileoff_t
*first_unused
, /* unused block */
1332 int whichfork
) /* data or attr fork */
1334 int error
; /* error return value */
1335 int idx
; /* extent record index */
1336 xfs_ifork_t
*ifp
; /* inode fork pointer */
1337 xfs_fileoff_t lastaddr
; /* last block number seen */
1338 xfs_fileoff_t lowest
; /* lowest useful block */
1339 xfs_fileoff_t max
; /* starting useful block */
1340 xfs_extnum_t nextents
; /* number of extent entries */
1342 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
||
1343 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
||
1344 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
1345 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
) {
1349 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1350 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
1351 (error
= xfs_iread_extents(tp
, ip
, whichfork
)))
1353 lowest
= *first_unused
;
1354 nextents
= xfs_iext_count(ifp
);
1355 for (idx
= 0, lastaddr
= 0, max
= lowest
; idx
< nextents
; idx
++) {
1356 struct xfs_bmbt_irec got
;
1358 xfs_iext_get_extent(ifp
, idx
, &got
);
1361 * See if the hole before this extent will work.
1363 if (got
.br_startoff
>= lowest
+ len
&&
1364 got
.br_startoff
- max
>= len
) {
1365 *first_unused
= max
;
1368 lastaddr
= got
.br_startoff
+ got
.br_blockcount
;
1369 max
= XFS_FILEOFF_MAX(lastaddr
, lowest
);
1371 *first_unused
= max
;
1376 * Returns the file-relative block number of the last block - 1 before
1377 * last_block (input value) in the file.
1378 * This is not based on i_size, it is based on the extent records.
1379 * Returns 0 for local files, as they do not have extent records.
1382 xfs_bmap_last_before(
1383 struct xfs_trans
*tp
, /* transaction pointer */
1384 struct xfs_inode
*ip
, /* incore inode */
1385 xfs_fileoff_t
*last_block
, /* last block */
1386 int whichfork
) /* data or attr fork */
1388 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1389 struct xfs_bmbt_irec got
;
1393 switch (XFS_IFORK_FORMAT(ip
, whichfork
)) {
1394 case XFS_DINODE_FMT_LOCAL
:
1397 case XFS_DINODE_FMT_BTREE
:
1398 case XFS_DINODE_FMT_EXTENTS
:
1404 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
1405 error
= xfs_iread_extents(tp
, ip
, whichfork
);
1410 if (xfs_iext_lookup_extent(ip
, ifp
, *last_block
- 1, &idx
, &got
)) {
1411 if (got
.br_startoff
<= *last_block
- 1)
1415 if (xfs_iext_get_extent(ifp
, idx
- 1, &got
)) {
1416 *last_block
= got
.br_startoff
+ got
.br_blockcount
;
1425 xfs_bmap_last_extent(
1426 struct xfs_trans
*tp
,
1427 struct xfs_inode
*ip
,
1429 struct xfs_bmbt_irec
*rec
,
1432 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1436 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
1437 error
= xfs_iread_extents(tp
, ip
, whichfork
);
1442 nextents
= xfs_iext_count(ifp
);
1443 if (nextents
== 0) {
1448 xfs_iext_get_extent(ifp
, nextents
- 1, rec
);
1454 * Check the last inode extent to determine whether this allocation will result
1455 * in blocks being allocated at the end of the file. When we allocate new data
1456 * blocks at the end of the file which do not start at the previous data block,
1457 * we will try to align the new blocks at stripe unit boundaries.
1459 * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be
1460 * at, or past the EOF.
1464 struct xfs_bmalloca
*bma
,
1467 struct xfs_bmbt_irec rec
;
1472 error
= xfs_bmap_last_extent(NULL
, bma
->ip
, whichfork
, &rec
,
1483 * Check if we are allocation or past the last extent, or at least into
1484 * the last delayed allocated extent.
1486 bma
->aeof
= bma
->offset
>= rec
.br_startoff
+ rec
.br_blockcount
||
1487 (bma
->offset
>= rec
.br_startoff
&&
1488 isnullstartblock(rec
.br_startblock
));
1493 * Returns the file-relative block number of the first block past eof in
1494 * the file. This is not based on i_size, it is based on the extent records.
1495 * Returns 0 for local files, as they do not have extent records.
1498 xfs_bmap_last_offset(
1499 struct xfs_inode
*ip
,
1500 xfs_fileoff_t
*last_block
,
1503 struct xfs_bmbt_irec rec
;
1509 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
)
1512 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
&&
1513 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
1516 error
= xfs_bmap_last_extent(NULL
, ip
, whichfork
, &rec
, &is_empty
);
1517 if (error
|| is_empty
)
1520 *last_block
= rec
.br_startoff
+ rec
.br_blockcount
;
1525 * Returns whether the selected fork of the inode has exactly one
1526 * block or not. For the data fork we check this matches di_size,
1527 * implying the file's range is 0..bsize-1.
1529 int /* 1=>1 block, 0=>otherwise */
1531 xfs_inode_t
*ip
, /* incore inode */
1532 int whichfork
) /* data or attr fork */
1534 xfs_ifork_t
*ifp
; /* inode fork pointer */
1535 int rval
; /* return value */
1536 xfs_bmbt_irec_t s
; /* internal version of extent */
1539 if (whichfork
== XFS_DATA_FORK
)
1540 return XFS_ISIZE(ip
) == ip
->i_mount
->m_sb
.sb_blocksize
;
1542 if (XFS_IFORK_NEXTENTS(ip
, whichfork
) != 1)
1544 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
1546 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1547 ASSERT(ifp
->if_flags
& XFS_IFEXTENTS
);
1548 xfs_iext_get_extent(ifp
, 0, &s
);
1549 rval
= s
.br_startoff
== 0 && s
.br_blockcount
== 1;
1550 if (rval
&& whichfork
== XFS_DATA_FORK
)
1551 ASSERT(XFS_ISIZE(ip
) == ip
->i_mount
->m_sb
.sb_blocksize
);
1556 * Extent tree manipulation functions used during allocation.
1560 * Convert a delayed allocation to a real allocation.
1562 STATIC
int /* error */
1563 xfs_bmap_add_extent_delay_real(
1564 struct xfs_bmalloca
*bma
,
1567 struct xfs_bmbt_irec
*new = &bma
->got
;
1568 int diff
; /* temp value */
1569 xfs_bmbt_rec_host_t
*ep
; /* extent entry for idx */
1570 int error
; /* error return value */
1571 int i
; /* temp state */
1572 xfs_ifork_t
*ifp
; /* inode fork pointer */
1573 xfs_fileoff_t new_endoff
; /* end offset of new entry */
1574 xfs_bmbt_irec_t r
[3]; /* neighbor extent entries */
1575 /* left is 0, right is 1, prev is 2 */
1576 int rval
=0; /* return value (logging flags) */
1577 int state
= 0;/* state bits, accessed thru macros */
1578 xfs_filblks_t da_new
; /* new count del alloc blocks used */
1579 xfs_filblks_t da_old
; /* old count del alloc blocks used */
1580 xfs_filblks_t temp
=0; /* value for da_new calculations */
1581 xfs_filblks_t temp2
=0;/* value for da_new calculations */
1582 int tmp_rval
; /* partial logging flags */
1583 struct xfs_mount
*mp
;
1584 xfs_extnum_t
*nextents
;
1586 mp
= bma
->ip
->i_mount
;
1587 ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
1588 ASSERT(whichfork
!= XFS_ATTR_FORK
);
1589 nextents
= (whichfork
== XFS_COW_FORK
? &bma
->ip
->i_cnextents
:
1590 &bma
->ip
->i_d
.di_nextents
);
1592 ASSERT(bma
->idx
>= 0);
1593 ASSERT(bma
->idx
<= xfs_iext_count(ifp
));
1594 ASSERT(!isnullstartblock(new->br_startblock
));
1596 (bma
->cur
->bc_private
.b
.flags
& XFS_BTCUR_BPRV_WASDEL
));
1598 XFS_STATS_INC(mp
, xs_add_exlist
);
1604 if (whichfork
== XFS_COW_FORK
)
1605 state
|= BMAP_COWFORK
;
1608 * Set up a bunch of variables to make the tests simpler.
1610 ep
= xfs_iext_get_ext(ifp
, bma
->idx
);
1611 xfs_bmbt_get_all(ep
, &PREV
);
1612 new_endoff
= new->br_startoff
+ new->br_blockcount
;
1613 ASSERT(PREV
.br_startoff
<= new->br_startoff
);
1614 ASSERT(PREV
.br_startoff
+ PREV
.br_blockcount
>= new_endoff
);
1616 da_old
= startblockval(PREV
.br_startblock
);
1620 * Set flags determining what part of the previous delayed allocation
1621 * extent is being replaced by a real allocation.
1623 if (PREV
.br_startoff
== new->br_startoff
)
1624 state
|= BMAP_LEFT_FILLING
;
1625 if (PREV
.br_startoff
+ PREV
.br_blockcount
== new_endoff
)
1626 state
|= BMAP_RIGHT_FILLING
;
1629 * Check and set flags if this segment has a left neighbor.
1630 * Don't set contiguous if the combined extent would be too large.
1633 state
|= BMAP_LEFT_VALID
;
1634 xfs_iext_get_extent(ifp
, bma
->idx
- 1, &LEFT
);
1636 if (isnullstartblock(LEFT
.br_startblock
))
1637 state
|= BMAP_LEFT_DELAY
;
1640 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
1641 LEFT
.br_startoff
+ LEFT
.br_blockcount
== new->br_startoff
&&
1642 LEFT
.br_startblock
+ LEFT
.br_blockcount
== new->br_startblock
&&
1643 LEFT
.br_state
== new->br_state
&&
1644 LEFT
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
1645 state
|= BMAP_LEFT_CONTIG
;
1648 * Check and set flags if this segment has a right neighbor.
1649 * Don't set contiguous if the combined extent would be too large.
1650 * Also check for all-three-contiguous being too large.
1652 if (bma
->idx
< xfs_iext_count(ifp
) - 1) {
1653 state
|= BMAP_RIGHT_VALID
;
1654 xfs_iext_get_extent(ifp
, bma
->idx
+ 1, &RIGHT
);
1656 if (isnullstartblock(RIGHT
.br_startblock
))
1657 state
|= BMAP_RIGHT_DELAY
;
1660 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
1661 new_endoff
== RIGHT
.br_startoff
&&
1662 new->br_startblock
+ new->br_blockcount
== RIGHT
.br_startblock
&&
1663 new->br_state
== RIGHT
.br_state
&&
1664 new->br_blockcount
+ RIGHT
.br_blockcount
<= MAXEXTLEN
&&
1665 ((state
& (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
1666 BMAP_RIGHT_FILLING
)) !=
1667 (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
1668 BMAP_RIGHT_FILLING
) ||
1669 LEFT
.br_blockcount
+ new->br_blockcount
+ RIGHT
.br_blockcount
1671 state
|= BMAP_RIGHT_CONTIG
;
1675 * Switch out based on the FILLING and CONTIG state bits.
1677 switch (state
& (BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
1678 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
)) {
1679 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
1680 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
1682 * Filling in all of a previously delayed allocation extent.
1683 * The left and right neighbors are both contiguous with new.
1686 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1687 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, bma
->idx
),
1688 LEFT
.br_blockcount
+ PREV
.br_blockcount
+
1689 RIGHT
.br_blockcount
);
1690 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1692 xfs_iext_remove(bma
->ip
, bma
->idx
+ 1, 2, state
);
1694 if (bma
->cur
== NULL
)
1695 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1697 rval
= XFS_ILOG_CORE
;
1698 error
= xfs_bmbt_lookup_eq(bma
->cur
, RIGHT
.br_startoff
,
1699 RIGHT
.br_startblock
,
1700 RIGHT
.br_blockcount
, &i
);
1703 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1704 error
= xfs_btree_delete(bma
->cur
, &i
);
1707 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1708 error
= xfs_btree_decrement(bma
->cur
, 0, &i
);
1711 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1712 error
= xfs_bmbt_update(bma
->cur
, LEFT
.br_startoff
,
1714 LEFT
.br_blockcount
+
1715 PREV
.br_blockcount
+
1716 RIGHT
.br_blockcount
, LEFT
.br_state
);
1722 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
1724 * Filling in all of a previously delayed allocation extent.
1725 * The left neighbor is contiguous, the right is not.
1729 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1730 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, bma
->idx
),
1731 LEFT
.br_blockcount
+ PREV
.br_blockcount
);
1732 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1734 xfs_iext_remove(bma
->ip
, bma
->idx
+ 1, 1, state
);
1735 if (bma
->cur
== NULL
)
1736 rval
= XFS_ILOG_DEXT
;
1739 error
= xfs_bmbt_lookup_eq(bma
->cur
, LEFT
.br_startoff
,
1740 LEFT
.br_startblock
, LEFT
.br_blockcount
,
1744 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1745 error
= xfs_bmbt_update(bma
->cur
, LEFT
.br_startoff
,
1747 LEFT
.br_blockcount
+
1748 PREV
.br_blockcount
, LEFT
.br_state
);
1754 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
1756 * Filling in all of a previously delayed allocation extent.
1757 * The right neighbor is contiguous, the left is not.
1759 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1760 xfs_bmbt_set_startblock(ep
, new->br_startblock
);
1761 xfs_bmbt_set_blockcount(ep
,
1762 PREV
.br_blockcount
+ RIGHT
.br_blockcount
);
1763 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1765 xfs_iext_remove(bma
->ip
, bma
->idx
+ 1, 1, state
);
1766 if (bma
->cur
== NULL
)
1767 rval
= XFS_ILOG_DEXT
;
1770 error
= xfs_bmbt_lookup_eq(bma
->cur
, RIGHT
.br_startoff
,
1771 RIGHT
.br_startblock
,
1772 RIGHT
.br_blockcount
, &i
);
1775 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1776 error
= xfs_bmbt_update(bma
->cur
, PREV
.br_startoff
,
1778 PREV
.br_blockcount
+
1779 RIGHT
.br_blockcount
, PREV
.br_state
);
1785 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
1787 * Filling in all of a previously delayed allocation extent.
1788 * Neither the left nor right neighbors are contiguous with
1791 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1792 xfs_bmbt_set_startblock(ep
, new->br_startblock
);
1793 xfs_bmbt_set_state(ep
, new->br_state
);
1794 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1797 if (bma
->cur
== NULL
)
1798 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1800 rval
= XFS_ILOG_CORE
;
1801 error
= xfs_bmbt_lookup_eq(bma
->cur
, new->br_startoff
,
1802 new->br_startblock
, new->br_blockcount
,
1806 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
1807 bma
->cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
1808 error
= xfs_btree_insert(bma
->cur
, &i
);
1811 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1815 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
:
1817 * Filling in the first part of a previous delayed allocation.
1818 * The left neighbor is contiguous.
1820 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
- 1, state
, _THIS_IP_
);
1821 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, bma
->idx
- 1),
1822 LEFT
.br_blockcount
+ new->br_blockcount
);
1823 xfs_bmbt_set_startoff(ep
,
1824 PREV
.br_startoff
+ new->br_blockcount
);
1825 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
- 1, state
, _THIS_IP_
);
1827 temp
= PREV
.br_blockcount
- new->br_blockcount
;
1828 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1829 xfs_bmbt_set_blockcount(ep
, temp
);
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
+
1848 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
1849 startblockval(PREV
.br_startblock
));
1850 xfs_bmbt_set_startblock(ep
, nullstartblock(da_new
));
1851 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1856 case BMAP_LEFT_FILLING
:
1858 * Filling in the first part of a previous delayed allocation.
1859 * The left neighbor is not contiguous.
1861 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1862 xfs_bmbt_set_startoff(ep
, new_endoff
);
1863 temp
= PREV
.br_blockcount
- new->br_blockcount
;
1864 xfs_bmbt_set_blockcount(ep
, temp
);
1865 xfs_iext_insert(bma
->ip
, bma
->idx
, 1, new, state
);
1867 if (bma
->cur
== NULL
)
1868 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1870 rval
= XFS_ILOG_CORE
;
1871 error
= xfs_bmbt_lookup_eq(bma
->cur
, new->br_startoff
,
1872 new->br_startblock
, new->br_blockcount
,
1876 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
1877 bma
->cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
1878 error
= xfs_btree_insert(bma
->cur
, &i
);
1881 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1884 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
1885 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
1886 bma
->firstblock
, bma
->dfops
,
1887 &bma
->cur
, 1, &tmp_rval
, whichfork
);
1892 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
1893 startblockval(PREV
.br_startblock
) -
1894 (bma
->cur
? bma
->cur
->bc_private
.b
.allocated
: 0));
1895 ep
= xfs_iext_get_ext(ifp
, bma
->idx
+ 1);
1896 xfs_bmbt_set_startblock(ep
, nullstartblock(da_new
));
1897 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
+ 1, state
, _THIS_IP_
);
1900 case BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
1902 * Filling in the last part of a previous delayed allocation.
1903 * The right neighbor is contiguous with the new allocation.
1905 temp
= PREV
.br_blockcount
- new->br_blockcount
;
1906 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
+ 1, state
, _THIS_IP_
);
1907 xfs_bmbt_set_blockcount(ep
, temp
);
1908 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp
, bma
->idx
+ 1),
1909 new->br_startoff
, new->br_startblock
,
1910 new->br_blockcount
+ RIGHT
.br_blockcount
,
1912 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
+ 1, state
, _THIS_IP_
);
1913 if (bma
->cur
== NULL
)
1914 rval
= XFS_ILOG_DEXT
;
1917 error
= xfs_bmbt_lookup_eq(bma
->cur
, RIGHT
.br_startoff
,
1918 RIGHT
.br_startblock
,
1919 RIGHT
.br_blockcount
, &i
);
1922 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1923 error
= xfs_bmbt_update(bma
->cur
, new->br_startoff
,
1925 new->br_blockcount
+
1926 RIGHT
.br_blockcount
,
1932 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
1933 startblockval(PREV
.br_startblock
));
1934 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1935 xfs_bmbt_set_startblock(ep
, nullstartblock(da_new
));
1936 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1941 case BMAP_RIGHT_FILLING
:
1943 * Filling in the last part of a previous delayed allocation.
1944 * The right neighbor is not contiguous.
1946 temp
= PREV
.br_blockcount
- new->br_blockcount
;
1947 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1948 xfs_bmbt_set_blockcount(ep
, temp
);
1949 xfs_iext_insert(bma
->ip
, bma
->idx
+ 1, 1, new, state
);
1951 if (bma
->cur
== NULL
)
1952 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1954 rval
= XFS_ILOG_CORE
;
1955 error
= xfs_bmbt_lookup_eq(bma
->cur
, new->br_startoff
,
1956 new->br_startblock
, new->br_blockcount
,
1960 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
1961 bma
->cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
1962 error
= xfs_btree_insert(bma
->cur
, &i
);
1965 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1968 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
1969 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
1970 bma
->firstblock
, bma
->dfops
, &bma
->cur
, 1,
1971 &tmp_rval
, whichfork
);
1976 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
1977 startblockval(PREV
.br_startblock
) -
1978 (bma
->cur
? bma
->cur
->bc_private
.b
.allocated
: 0));
1979 ep
= xfs_iext_get_ext(ifp
, bma
->idx
);
1980 xfs_bmbt_set_startblock(ep
, nullstartblock(da_new
));
1981 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1988 * Filling in the middle part of a previous delayed allocation.
1989 * Contiguity is impossible here.
1990 * This case is avoided almost all the time.
1992 * We start with a delayed allocation:
1994 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
1997 * and we are allocating:
1998 * +rrrrrrrrrrrrrrrrr+
2001 * and we set it up for insertion as:
2002 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
2004 * PREV @ idx LEFT RIGHT
2005 * inserted at idx + 1
2007 temp
= new->br_startoff
- PREV
.br_startoff
;
2008 temp2
= PREV
.br_startoff
+ PREV
.br_blockcount
- new_endoff
;
2009 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, 0, _THIS_IP_
);
2010 xfs_bmbt_set_blockcount(ep
, temp
); /* truncate PREV */
2012 RIGHT
.br_state
= PREV
.br_state
;
2013 RIGHT
.br_startblock
= nullstartblock(
2014 (int)xfs_bmap_worst_indlen(bma
->ip
, temp2
));
2015 RIGHT
.br_startoff
= new_endoff
;
2016 RIGHT
.br_blockcount
= temp2
;
2017 /* insert LEFT (r[0]) and RIGHT (r[1]) at the same time */
2018 xfs_iext_insert(bma
->ip
, bma
->idx
+ 1, 2, &LEFT
, state
);
2020 if (bma
->cur
== NULL
)
2021 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2023 rval
= XFS_ILOG_CORE
;
2024 error
= xfs_bmbt_lookup_eq(bma
->cur
, new->br_startoff
,
2025 new->br_startblock
, new->br_blockcount
,
2029 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
2030 bma
->cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
2031 error
= xfs_btree_insert(bma
->cur
, &i
);
2034 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2037 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
2038 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
2039 bma
->firstblock
, bma
->dfops
, &bma
->cur
,
2040 1, &tmp_rval
, whichfork
);
2045 temp
= xfs_bmap_worst_indlen(bma
->ip
, temp
);
2046 temp2
= xfs_bmap_worst_indlen(bma
->ip
, temp2
);
2047 diff
= (int)(temp
+ temp2
-
2048 (startblockval(PREV
.br_startblock
) -
2050 bma
->cur
->bc_private
.b
.allocated
: 0)));
2052 error
= xfs_mod_fdblocks(bma
->ip
->i_mount
,
2053 -((int64_t)diff
), false);
2059 ep
= xfs_iext_get_ext(ifp
, bma
->idx
);
2060 xfs_bmbt_set_startblock(ep
, nullstartblock((int)temp
));
2061 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2062 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
+ 2, state
, _THIS_IP_
);
2063 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp
, bma
->idx
+ 2),
2064 nullstartblock((int)temp2
));
2065 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
+ 2, state
, _THIS_IP_
);
2068 da_new
= temp
+ temp2
;
2071 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2072 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2073 case BMAP_LEFT_FILLING
| BMAP_RIGHT_CONTIG
:
2074 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
2075 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2076 case BMAP_LEFT_CONTIG
:
2077 case BMAP_RIGHT_CONTIG
:
2079 * These cases are all impossible.
2084 /* add reverse mapping */
2085 error
= xfs_rmap_map_extent(mp
, bma
->dfops
, bma
->ip
, whichfork
, new);
2089 /* convert to a btree if necessary */
2090 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
2091 int tmp_logflags
; /* partial log flag return val */
2093 ASSERT(bma
->cur
== NULL
);
2094 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
2095 bma
->firstblock
, bma
->dfops
, &bma
->cur
,
2096 da_old
> 0, &tmp_logflags
, whichfork
);
2097 bma
->logflags
|= tmp_logflags
;
2102 /* adjust for changes in reserved delayed indirect blocks */
2103 if (da_old
|| da_new
) {
2106 temp
+= bma
->cur
->bc_private
.b
.allocated
;
2108 xfs_mod_fdblocks(bma
->ip
->i_mount
,
2109 (int64_t)(da_old
- temp
), false);
2112 /* clear out the allocated field, done with it now in any case. */
2114 bma
->cur
->bc_private
.b
.allocated
= 0;
2116 xfs_bmap_check_leaf_extents(bma
->cur
, bma
->ip
, whichfork
);
2118 if (whichfork
!= XFS_COW_FORK
)
2119 bma
->logflags
|= rval
;
2127 * Convert an unwritten allocation to a real allocation or vice versa.
2129 STATIC
int /* error */
2130 xfs_bmap_add_extent_unwritten_real(
2131 struct xfs_trans
*tp
,
2132 xfs_inode_t
*ip
, /* incore inode pointer */
2134 xfs_extnum_t
*idx
, /* extent number to update/insert */
2135 xfs_btree_cur_t
**curp
, /* if *curp is null, not a btree */
2136 xfs_bmbt_irec_t
*new, /* new data to add to file extents */
2137 xfs_fsblock_t
*first
, /* pointer to firstblock variable */
2138 struct xfs_defer_ops
*dfops
, /* list of extents to be freed */
2139 int *logflagsp
) /* inode logging flags */
2141 xfs_btree_cur_t
*cur
; /* btree cursor */
2142 xfs_bmbt_rec_host_t
*ep
; /* extent entry for idx */
2143 int error
; /* error return value */
2144 int i
; /* temp state */
2145 xfs_ifork_t
*ifp
; /* inode fork pointer */
2146 xfs_fileoff_t new_endoff
; /* end offset of new entry */
2147 xfs_exntst_t newext
; /* new extent state */
2148 xfs_exntst_t oldext
; /* old extent state */
2149 xfs_bmbt_irec_t r
[3]; /* neighbor extent entries */
2150 /* left is 0, right is 1, prev is 2 */
2151 int rval
=0; /* return value (logging flags) */
2152 int state
= 0;/* state bits, accessed thru macros */
2153 struct xfs_mount
*mp
= ip
->i_mount
;
2158 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
2159 if (whichfork
== XFS_COW_FORK
)
2160 state
|= BMAP_COWFORK
;
2163 ASSERT(*idx
<= xfs_iext_count(ifp
));
2164 ASSERT(!isnullstartblock(new->br_startblock
));
2166 XFS_STATS_INC(mp
, xs_add_exlist
);
2173 * Set up a bunch of variables to make the tests simpler.
2176 ep
= xfs_iext_get_ext(ifp
, *idx
);
2177 xfs_bmbt_get_all(ep
, &PREV
);
2178 newext
= new->br_state
;
2179 oldext
= (newext
== XFS_EXT_UNWRITTEN
) ?
2180 XFS_EXT_NORM
: XFS_EXT_UNWRITTEN
;
2181 ASSERT(PREV
.br_state
== oldext
);
2182 new_endoff
= new->br_startoff
+ new->br_blockcount
;
2183 ASSERT(PREV
.br_startoff
<= new->br_startoff
);
2184 ASSERT(PREV
.br_startoff
+ PREV
.br_blockcount
>= new_endoff
);
2187 * Set flags determining what part of the previous oldext allocation
2188 * extent is being replaced by a newext allocation.
2190 if (PREV
.br_startoff
== new->br_startoff
)
2191 state
|= BMAP_LEFT_FILLING
;
2192 if (PREV
.br_startoff
+ PREV
.br_blockcount
== new_endoff
)
2193 state
|= BMAP_RIGHT_FILLING
;
2196 * Check and set flags if this segment has a left neighbor.
2197 * Don't set contiguous if the combined extent would be too large.
2200 state
|= BMAP_LEFT_VALID
;
2201 xfs_iext_get_extent(ifp
, *idx
- 1, &LEFT
);
2203 if (isnullstartblock(LEFT
.br_startblock
))
2204 state
|= BMAP_LEFT_DELAY
;
2207 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
2208 LEFT
.br_startoff
+ LEFT
.br_blockcount
== new->br_startoff
&&
2209 LEFT
.br_startblock
+ LEFT
.br_blockcount
== new->br_startblock
&&
2210 LEFT
.br_state
== newext
&&
2211 LEFT
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
2212 state
|= BMAP_LEFT_CONTIG
;
2215 * Check and set flags if this segment has a right neighbor.
2216 * Don't set contiguous if the combined extent would be too large.
2217 * Also check for all-three-contiguous being too large.
2219 if (*idx
< xfs_iext_count(ifp
) - 1) {
2220 state
|= BMAP_RIGHT_VALID
;
2221 xfs_iext_get_extent(ifp
, *idx
+ 1, &RIGHT
);
2222 if (isnullstartblock(RIGHT
.br_startblock
))
2223 state
|= BMAP_RIGHT_DELAY
;
2226 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
2227 new_endoff
== RIGHT
.br_startoff
&&
2228 new->br_startblock
+ new->br_blockcount
== RIGHT
.br_startblock
&&
2229 newext
== RIGHT
.br_state
&&
2230 new->br_blockcount
+ RIGHT
.br_blockcount
<= MAXEXTLEN
&&
2231 ((state
& (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
2232 BMAP_RIGHT_FILLING
)) !=
2233 (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
2234 BMAP_RIGHT_FILLING
) ||
2235 LEFT
.br_blockcount
+ new->br_blockcount
+ RIGHT
.br_blockcount
2237 state
|= BMAP_RIGHT_CONTIG
;
2240 * Switch out based on the FILLING and CONTIG state bits.
2242 switch (state
& (BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
2243 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
)) {
2244 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
2245 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2247 * Setting all of a previous oldext extent to newext.
2248 * The left and right neighbors are both contiguous with new.
2252 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2253 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
),
2254 LEFT
.br_blockcount
+ PREV
.br_blockcount
+
2255 RIGHT
.br_blockcount
);
2256 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2258 xfs_iext_remove(ip
, *idx
+ 1, 2, state
);
2259 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2260 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 2);
2262 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2264 rval
= XFS_ILOG_CORE
;
2265 if ((error
= xfs_bmbt_lookup_eq(cur
, RIGHT
.br_startoff
,
2266 RIGHT
.br_startblock
,
2267 RIGHT
.br_blockcount
, &i
)))
2269 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2270 if ((error
= xfs_btree_delete(cur
, &i
)))
2272 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2273 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2275 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2276 if ((error
= xfs_btree_delete(cur
, &i
)))
2278 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2279 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2281 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2282 if ((error
= xfs_bmbt_update(cur
, LEFT
.br_startoff
,
2284 LEFT
.br_blockcount
+ PREV
.br_blockcount
+
2285 RIGHT
.br_blockcount
, LEFT
.br_state
)))
2290 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
2292 * Setting all of a previous oldext extent to newext.
2293 * The left neighbor is contiguous, the right is not.
2297 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2298 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
),
2299 LEFT
.br_blockcount
+ PREV
.br_blockcount
);
2300 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2302 xfs_iext_remove(ip
, *idx
+ 1, 1, state
);
2303 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2304 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
2306 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2308 rval
= XFS_ILOG_CORE
;
2309 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2310 PREV
.br_startblock
, PREV
.br_blockcount
,
2313 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2314 if ((error
= xfs_btree_delete(cur
, &i
)))
2316 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2317 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2319 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2320 if ((error
= xfs_bmbt_update(cur
, LEFT
.br_startoff
,
2322 LEFT
.br_blockcount
+ PREV
.br_blockcount
,
2328 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2330 * Setting all of a previous oldext extent to newext.
2331 * The right neighbor is contiguous, the left is not.
2333 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2334 xfs_bmbt_set_blockcount(ep
,
2335 PREV
.br_blockcount
+ RIGHT
.br_blockcount
);
2336 xfs_bmbt_set_state(ep
, newext
);
2337 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2338 xfs_iext_remove(ip
, *idx
+ 1, 1, state
);
2339 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2340 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
2342 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2344 rval
= XFS_ILOG_CORE
;
2345 if ((error
= xfs_bmbt_lookup_eq(cur
, RIGHT
.br_startoff
,
2346 RIGHT
.br_startblock
,
2347 RIGHT
.br_blockcount
, &i
)))
2349 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2350 if ((error
= xfs_btree_delete(cur
, &i
)))
2352 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2353 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2355 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2356 if ((error
= xfs_bmbt_update(cur
, new->br_startoff
,
2358 new->br_blockcount
+ RIGHT
.br_blockcount
,
2364 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
2366 * Setting all of a previous oldext extent to newext.
2367 * Neither the left nor right neighbors are contiguous with
2370 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2371 xfs_bmbt_set_state(ep
, newext
);
2372 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2375 rval
= XFS_ILOG_DEXT
;
2378 if ((error
= xfs_bmbt_lookup_eq(cur
, new->br_startoff
,
2379 new->br_startblock
, new->br_blockcount
,
2382 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2383 if ((error
= xfs_bmbt_update(cur
, new->br_startoff
,
2384 new->br_startblock
, new->br_blockcount
,
2390 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
:
2392 * Setting the first part of a previous oldext extent to newext.
2393 * The left neighbor is contiguous.
2395 trace_xfs_bmap_pre_update(ip
, *idx
- 1, state
, _THIS_IP_
);
2396 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
- 1),
2397 LEFT
.br_blockcount
+ new->br_blockcount
);
2398 xfs_bmbt_set_startoff(ep
,
2399 PREV
.br_startoff
+ new->br_blockcount
);
2400 trace_xfs_bmap_post_update(ip
, *idx
- 1, state
, _THIS_IP_
);
2402 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2403 xfs_bmbt_set_startblock(ep
,
2404 new->br_startblock
+ new->br_blockcount
);
2405 xfs_bmbt_set_blockcount(ep
,
2406 PREV
.br_blockcount
- new->br_blockcount
);
2407 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2412 rval
= XFS_ILOG_DEXT
;
2415 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2416 PREV
.br_startblock
, PREV
.br_blockcount
,
2419 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2420 if ((error
= xfs_bmbt_update(cur
,
2421 PREV
.br_startoff
+ new->br_blockcount
,
2422 PREV
.br_startblock
+ new->br_blockcount
,
2423 PREV
.br_blockcount
- new->br_blockcount
,
2426 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2428 error
= xfs_bmbt_update(cur
, LEFT
.br_startoff
,
2430 LEFT
.br_blockcount
+ new->br_blockcount
,
2437 case BMAP_LEFT_FILLING
:
2439 * Setting the first part of a previous oldext extent to newext.
2440 * The left neighbor is not contiguous.
2442 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2443 ASSERT(ep
&& xfs_bmbt_get_state(ep
) == oldext
);
2444 xfs_bmbt_set_startoff(ep
, new_endoff
);
2445 xfs_bmbt_set_blockcount(ep
,
2446 PREV
.br_blockcount
- new->br_blockcount
);
2447 xfs_bmbt_set_startblock(ep
,
2448 new->br_startblock
+ new->br_blockcount
);
2449 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2451 xfs_iext_insert(ip
, *idx
, 1, new, state
);
2452 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2453 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
2455 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2457 rval
= XFS_ILOG_CORE
;
2458 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2459 PREV
.br_startblock
, PREV
.br_blockcount
,
2462 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2463 if ((error
= xfs_bmbt_update(cur
,
2464 PREV
.br_startoff
+ new->br_blockcount
,
2465 PREV
.br_startblock
+ new->br_blockcount
,
2466 PREV
.br_blockcount
- new->br_blockcount
,
2469 cur
->bc_rec
.b
= *new;
2470 if ((error
= xfs_btree_insert(cur
, &i
)))
2472 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2476 case BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2478 * Setting the last part of a previous oldext extent to newext.
2479 * The right neighbor is contiguous with the new allocation.
2481 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2482 xfs_bmbt_set_blockcount(ep
,
2483 PREV
.br_blockcount
- new->br_blockcount
);
2484 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2488 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2489 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp
, *idx
),
2490 new->br_startoff
, new->br_startblock
,
2491 new->br_blockcount
+ RIGHT
.br_blockcount
, newext
);
2492 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2495 rval
= XFS_ILOG_DEXT
;
2498 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2500 PREV
.br_blockcount
, &i
)))
2502 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2503 if ((error
= xfs_bmbt_update(cur
, PREV
.br_startoff
,
2505 PREV
.br_blockcount
- new->br_blockcount
,
2508 if ((error
= xfs_btree_increment(cur
, 0, &i
)))
2510 if ((error
= xfs_bmbt_update(cur
, new->br_startoff
,
2512 new->br_blockcount
+ RIGHT
.br_blockcount
,
2518 case BMAP_RIGHT_FILLING
:
2520 * Setting the last part of a previous oldext extent to newext.
2521 * The right neighbor is not contiguous.
2523 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2524 xfs_bmbt_set_blockcount(ep
,
2525 PREV
.br_blockcount
- new->br_blockcount
);
2526 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2529 xfs_iext_insert(ip
, *idx
, 1, new, state
);
2531 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2532 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
2534 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2536 rval
= XFS_ILOG_CORE
;
2537 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2538 PREV
.br_startblock
, PREV
.br_blockcount
,
2541 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2542 if ((error
= xfs_bmbt_update(cur
, PREV
.br_startoff
,
2544 PREV
.br_blockcount
- new->br_blockcount
,
2547 if ((error
= xfs_bmbt_lookup_eq(cur
, new->br_startoff
,
2548 new->br_startblock
, new->br_blockcount
,
2551 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
2552 cur
->bc_rec
.b
.br_state
= new->br_state
;
2553 if ((error
= xfs_btree_insert(cur
, &i
)))
2555 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2561 * Setting the middle part of a previous oldext extent to
2562 * newext. Contiguity is impossible here.
2563 * One extent becomes three extents.
2565 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2566 xfs_bmbt_set_blockcount(ep
,
2567 new->br_startoff
- PREV
.br_startoff
);
2568 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2571 r
[1].br_startoff
= new_endoff
;
2572 r
[1].br_blockcount
=
2573 PREV
.br_startoff
+ PREV
.br_blockcount
- new_endoff
;
2574 r
[1].br_startblock
= new->br_startblock
+ new->br_blockcount
;
2575 r
[1].br_state
= oldext
;
2578 xfs_iext_insert(ip
, *idx
, 2, &r
[0], state
);
2580 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2581 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 2);
2583 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2585 rval
= XFS_ILOG_CORE
;
2586 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2587 PREV
.br_startblock
, PREV
.br_blockcount
,
2590 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2591 /* new right extent - oldext */
2592 if ((error
= xfs_bmbt_update(cur
, r
[1].br_startoff
,
2593 r
[1].br_startblock
, r
[1].br_blockcount
,
2596 /* new left extent - oldext */
2597 cur
->bc_rec
.b
= PREV
;
2598 cur
->bc_rec
.b
.br_blockcount
=
2599 new->br_startoff
- PREV
.br_startoff
;
2600 if ((error
= xfs_btree_insert(cur
, &i
)))
2602 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2604 * Reset the cursor to the position of the new extent
2605 * we are about to insert as we can't trust it after
2606 * the previous insert.
2608 if ((error
= xfs_bmbt_lookup_eq(cur
, new->br_startoff
,
2609 new->br_startblock
, new->br_blockcount
,
2612 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
2613 /* new middle extent - newext */
2614 cur
->bc_rec
.b
.br_state
= new->br_state
;
2615 if ((error
= xfs_btree_insert(cur
, &i
)))
2617 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2621 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2622 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2623 case BMAP_LEFT_FILLING
| BMAP_RIGHT_CONTIG
:
2624 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
2625 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2626 case BMAP_LEFT_CONTIG
:
2627 case BMAP_RIGHT_CONTIG
:
2629 * These cases are all impossible.
2634 /* update reverse mappings */
2635 error
= xfs_rmap_convert_extent(mp
, dfops
, ip
, whichfork
, new);
2639 /* convert to a btree if necessary */
2640 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
2641 int tmp_logflags
; /* partial log flag return val */
2643 ASSERT(cur
== NULL
);
2644 error
= xfs_bmap_extents_to_btree(tp
, ip
, first
, dfops
, &cur
,
2645 0, &tmp_logflags
, whichfork
);
2646 *logflagsp
|= tmp_logflags
;
2651 /* clear out the allocated field, done with it now in any case. */
2653 cur
->bc_private
.b
.allocated
= 0;
2657 xfs_bmap_check_leaf_extents(*curp
, ip
, whichfork
);
2667 * Convert a hole to a delayed allocation.
2670 xfs_bmap_add_extent_hole_delay(
2671 xfs_inode_t
*ip
, /* incore inode pointer */
2673 xfs_extnum_t
*idx
, /* extent number to update/insert */
2674 xfs_bmbt_irec_t
*new) /* new data to add to file extents */
2676 xfs_ifork_t
*ifp
; /* inode fork pointer */
2677 xfs_bmbt_irec_t left
; /* left neighbor extent entry */
2678 xfs_filblks_t newlen
=0; /* new indirect size */
2679 xfs_filblks_t oldlen
=0; /* old indirect size */
2680 xfs_bmbt_irec_t right
; /* right neighbor extent entry */
2681 int state
; /* state bits, accessed thru macros */
2682 xfs_filblks_t temp
=0; /* temp for indirect calculations */
2684 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
2686 if (whichfork
== XFS_COW_FORK
)
2687 state
|= BMAP_COWFORK
;
2688 ASSERT(isnullstartblock(new->br_startblock
));
2691 * Check and set flags if this segment has a left neighbor
2694 state
|= BMAP_LEFT_VALID
;
2695 xfs_iext_get_extent(ifp
, *idx
- 1, &left
);
2697 if (isnullstartblock(left
.br_startblock
))
2698 state
|= BMAP_LEFT_DELAY
;
2702 * Check and set flags if the current (right) segment exists.
2703 * If it doesn't exist, we're converting the hole at end-of-file.
2705 if (*idx
< xfs_iext_count(ifp
)) {
2706 state
|= BMAP_RIGHT_VALID
;
2707 xfs_iext_get_extent(ifp
, *idx
, &right
);
2709 if (isnullstartblock(right
.br_startblock
))
2710 state
|= BMAP_RIGHT_DELAY
;
2714 * Set contiguity flags on the left and right neighbors.
2715 * Don't let extents get too large, even if the pieces are contiguous.
2717 if ((state
& BMAP_LEFT_VALID
) && (state
& BMAP_LEFT_DELAY
) &&
2718 left
.br_startoff
+ left
.br_blockcount
== new->br_startoff
&&
2719 left
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
2720 state
|= BMAP_LEFT_CONTIG
;
2722 if ((state
& BMAP_RIGHT_VALID
) && (state
& BMAP_RIGHT_DELAY
) &&
2723 new->br_startoff
+ new->br_blockcount
== right
.br_startoff
&&
2724 new->br_blockcount
+ right
.br_blockcount
<= MAXEXTLEN
&&
2725 (!(state
& BMAP_LEFT_CONTIG
) ||
2726 (left
.br_blockcount
+ new->br_blockcount
+
2727 right
.br_blockcount
<= MAXEXTLEN
)))
2728 state
|= BMAP_RIGHT_CONTIG
;
2731 * Switch out based on the contiguity flags.
2733 switch (state
& (BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
)) {
2734 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2736 * New allocation is contiguous with delayed allocations
2737 * on the left and on the right.
2738 * Merge all three into a single extent record.
2741 temp
= left
.br_blockcount
+ new->br_blockcount
+
2742 right
.br_blockcount
;
2744 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2745 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
), temp
);
2746 oldlen
= startblockval(left
.br_startblock
) +
2747 startblockval(new->br_startblock
) +
2748 startblockval(right
.br_startblock
);
2749 newlen
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
2751 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp
, *idx
),
2752 nullstartblock((int)newlen
));
2753 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2755 xfs_iext_remove(ip
, *idx
+ 1, 1, state
);
2758 case BMAP_LEFT_CONTIG
:
2760 * New allocation is contiguous with a delayed allocation
2762 * Merge the new allocation with the left neighbor.
2765 temp
= left
.br_blockcount
+ new->br_blockcount
;
2767 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2768 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
), temp
);
2769 oldlen
= startblockval(left
.br_startblock
) +
2770 startblockval(new->br_startblock
);
2771 newlen
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
2773 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp
, *idx
),
2774 nullstartblock((int)newlen
));
2775 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2778 case BMAP_RIGHT_CONTIG
:
2780 * New allocation is contiguous with a delayed allocation
2782 * Merge the new allocation with the right neighbor.
2784 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2785 temp
= new->br_blockcount
+ right
.br_blockcount
;
2786 oldlen
= startblockval(new->br_startblock
) +
2787 startblockval(right
.br_startblock
);
2788 newlen
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
2790 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp
, *idx
),
2792 nullstartblock((int)newlen
), temp
, right
.br_state
);
2793 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2798 * New allocation is not contiguous with another
2799 * delayed allocation.
2800 * Insert a new entry.
2802 oldlen
= newlen
= 0;
2803 xfs_iext_insert(ip
, *idx
, 1, new, state
);
2806 if (oldlen
!= newlen
) {
2807 ASSERT(oldlen
> newlen
);
2808 xfs_mod_fdblocks(ip
->i_mount
, (int64_t)(oldlen
- newlen
),
2811 * Nothing to do for disk quota accounting here.
2817 * Convert a hole to a real allocation.
2819 STATIC
int /* error */
2820 xfs_bmap_add_extent_hole_real(
2821 struct xfs_trans
*tp
,
2822 struct xfs_inode
*ip
,
2825 struct xfs_btree_cur
**curp
,
2826 struct xfs_bmbt_irec
*new,
2827 xfs_fsblock_t
*first
,
2828 struct xfs_defer_ops
*dfops
,
2831 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
2832 struct xfs_mount
*mp
= ip
->i_mount
;
2833 struct xfs_btree_cur
*cur
= *curp
;
2834 int error
; /* error return value */
2835 int i
; /* temp state */
2836 xfs_bmbt_irec_t left
; /* left neighbor extent entry */
2837 xfs_bmbt_irec_t right
; /* right neighbor extent entry */
2838 int rval
=0; /* return value (logging flags) */
2839 int state
; /* state bits, accessed thru macros */
2842 ASSERT(*idx
<= xfs_iext_count(ifp
));
2843 ASSERT(!isnullstartblock(new->br_startblock
));
2844 ASSERT(!cur
|| !(cur
->bc_private
.b
.flags
& XFS_BTCUR_BPRV_WASDEL
));
2846 XFS_STATS_INC(mp
, xs_add_exlist
);
2849 if (whichfork
== XFS_ATTR_FORK
)
2850 state
|= BMAP_ATTRFORK
;
2851 if (whichfork
== XFS_COW_FORK
)
2852 state
|= BMAP_COWFORK
;
2855 * Check and set flags if this segment has a left neighbor.
2858 state
|= BMAP_LEFT_VALID
;
2859 xfs_iext_get_extent(ifp
, *idx
- 1, &left
);
2860 if (isnullstartblock(left
.br_startblock
))
2861 state
|= BMAP_LEFT_DELAY
;
2865 * Check and set flags if this segment has a current value.
2866 * Not true if we're inserting into the "hole" at eof.
2868 if (*idx
< xfs_iext_count(ifp
)) {
2869 state
|= BMAP_RIGHT_VALID
;
2870 xfs_iext_get_extent(ifp
, *idx
, &right
);
2871 if (isnullstartblock(right
.br_startblock
))
2872 state
|= BMAP_RIGHT_DELAY
;
2876 * We're inserting a real allocation between "left" and "right".
2877 * Set the contiguity flags. Don't let extents get too large.
2879 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
2880 left
.br_startoff
+ left
.br_blockcount
== new->br_startoff
&&
2881 left
.br_startblock
+ left
.br_blockcount
== new->br_startblock
&&
2882 left
.br_state
== new->br_state
&&
2883 left
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
2884 state
|= BMAP_LEFT_CONTIG
;
2886 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
2887 new->br_startoff
+ new->br_blockcount
== right
.br_startoff
&&
2888 new->br_startblock
+ new->br_blockcount
== right
.br_startblock
&&
2889 new->br_state
== right
.br_state
&&
2890 new->br_blockcount
+ right
.br_blockcount
<= MAXEXTLEN
&&
2891 (!(state
& BMAP_LEFT_CONTIG
) ||
2892 left
.br_blockcount
+ new->br_blockcount
+
2893 right
.br_blockcount
<= MAXEXTLEN
))
2894 state
|= BMAP_RIGHT_CONTIG
;
2898 * Select which case we're in here, and implement it.
2900 switch (state
& (BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
)) {
2901 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2903 * New allocation is contiguous with real allocations on the
2904 * left and on the right.
2905 * Merge all three into a single extent record.
2908 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2909 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
),
2910 left
.br_blockcount
+ new->br_blockcount
+
2911 right
.br_blockcount
);
2912 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2914 xfs_iext_remove(ip
, *idx
+ 1, 1, state
);
2916 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2917 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
2919 rval
= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
2921 rval
= XFS_ILOG_CORE
;
2922 error
= xfs_bmbt_lookup_eq(cur
, right
.br_startoff
,
2923 right
.br_startblock
, right
.br_blockcount
,
2927 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2928 error
= xfs_btree_delete(cur
, &i
);
2931 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2932 error
= xfs_btree_decrement(cur
, 0, &i
);
2935 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2936 error
= xfs_bmbt_update(cur
, left
.br_startoff
,
2938 left
.br_blockcount
+
2939 new->br_blockcount
+
2940 right
.br_blockcount
,
2947 case BMAP_LEFT_CONTIG
:
2949 * New allocation is contiguous with a real allocation
2951 * Merge the new allocation with the left neighbor.
2954 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2955 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
),
2956 left
.br_blockcount
+ new->br_blockcount
);
2957 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2960 rval
= xfs_ilog_fext(whichfork
);
2963 error
= xfs_bmbt_lookup_eq(cur
, left
.br_startoff
,
2964 left
.br_startblock
, left
.br_blockcount
,
2968 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2969 error
= xfs_bmbt_update(cur
, left
.br_startoff
,
2971 left
.br_blockcount
+
2979 case BMAP_RIGHT_CONTIG
:
2981 * New allocation is contiguous with a real allocation
2983 * Merge the new allocation with the right neighbor.
2985 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2986 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp
, *idx
),
2987 new->br_startoff
, new->br_startblock
,
2988 new->br_blockcount
+ right
.br_blockcount
,
2990 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2993 rval
= xfs_ilog_fext(whichfork
);
2996 error
= xfs_bmbt_lookup_eq(cur
,
2998 right
.br_startblock
,
2999 right
.br_blockcount
, &i
);
3002 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
3003 error
= xfs_bmbt_update(cur
, new->br_startoff
,
3005 new->br_blockcount
+
3006 right
.br_blockcount
,
3015 * New allocation is not contiguous with another
3017 * Insert a new entry.
3019 xfs_iext_insert(ip
, *idx
, 1, new, state
);
3020 XFS_IFORK_NEXT_SET(ip
, whichfork
,
3021 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
3023 rval
= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
3025 rval
= XFS_ILOG_CORE
;
3026 error
= xfs_bmbt_lookup_eq(cur
,
3029 new->br_blockcount
, &i
);
3032 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
3033 cur
->bc_rec
.b
.br_state
= new->br_state
;
3034 error
= xfs_btree_insert(cur
, &i
);
3037 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
3042 /* add reverse mapping */
3043 error
= xfs_rmap_map_extent(mp
, dfops
, ip
, whichfork
, new);
3047 /* convert to a btree if necessary */
3048 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
3049 int tmp_logflags
; /* partial log flag return val */
3051 ASSERT(cur
== NULL
);
3052 error
= xfs_bmap_extents_to_btree(tp
, ip
, first
, dfops
, curp
,
3053 0, &tmp_logflags
, whichfork
);
3054 *logflagsp
|= tmp_logflags
;
3060 /* clear out the allocated field, done with it now in any case. */
3062 cur
->bc_private
.b
.allocated
= 0;
3064 xfs_bmap_check_leaf_extents(cur
, ip
, whichfork
);
3071 * Functions used in the extent read, allocate and remove paths
3075 * Adjust the size of the new extent based on di_extsize and rt extsize.
3078 xfs_bmap_extsize_align(
3080 xfs_bmbt_irec_t
*gotp
, /* next extent pointer */
3081 xfs_bmbt_irec_t
*prevp
, /* previous extent pointer */
3082 xfs_extlen_t extsz
, /* align to this extent size */
3083 int rt
, /* is this a realtime inode? */
3084 int eof
, /* is extent at end-of-file? */
3085 int delay
, /* creating delalloc extent? */
3086 int convert
, /* overwriting unwritten extent? */
3087 xfs_fileoff_t
*offp
, /* in/out: aligned offset */
3088 xfs_extlen_t
*lenp
) /* in/out: aligned length */
3090 xfs_fileoff_t orig_off
; /* original offset */
3091 xfs_extlen_t orig_alen
; /* original length */
3092 xfs_fileoff_t orig_end
; /* original off+len */
3093 xfs_fileoff_t nexto
; /* next file offset */
3094 xfs_fileoff_t prevo
; /* previous file offset */
3095 xfs_fileoff_t align_off
; /* temp for offset */
3096 xfs_extlen_t align_alen
; /* temp for length */
3097 xfs_extlen_t temp
; /* temp for calculations */
3102 orig_off
= align_off
= *offp
;
3103 orig_alen
= align_alen
= *lenp
;
3104 orig_end
= orig_off
+ orig_alen
;
3107 * If this request overlaps an existing extent, then don't
3108 * attempt to perform any additional alignment.
3110 if (!delay
&& !eof
&&
3111 (orig_off
>= gotp
->br_startoff
) &&
3112 (orig_end
<= gotp
->br_startoff
+ gotp
->br_blockcount
)) {
3117 * If the file offset is unaligned vs. the extent size
3118 * we need to align it. This will be possible unless
3119 * the file was previously written with a kernel that didn't
3120 * perform this alignment, or if a truncate shot us in the
3123 temp
= do_mod(orig_off
, extsz
);
3129 /* Same adjustment for the end of the requested area. */
3130 temp
= (align_alen
% extsz
);
3132 align_alen
+= extsz
- temp
;
3135 * For large extent hint sizes, the aligned extent might be larger than
3136 * MAXEXTLEN. In that case, reduce the size by an extsz so that it pulls
3137 * the length back under MAXEXTLEN. The outer allocation loops handle
3138 * short allocation just fine, so it is safe to do this. We only want to
3139 * do it when we are forced to, though, because it means more allocation
3140 * operations are required.
3142 while (align_alen
> MAXEXTLEN
)
3143 align_alen
-= extsz
;
3144 ASSERT(align_alen
<= MAXEXTLEN
);
3147 * If the previous block overlaps with this proposed allocation
3148 * then move the start forward without adjusting the length.
3150 if (prevp
->br_startoff
!= NULLFILEOFF
) {
3151 if (prevp
->br_startblock
== HOLESTARTBLOCK
)
3152 prevo
= prevp
->br_startoff
;
3154 prevo
= prevp
->br_startoff
+ prevp
->br_blockcount
;
3157 if (align_off
!= orig_off
&& align_off
< prevo
)
3160 * If the next block overlaps with this proposed allocation
3161 * then move the start back without adjusting the length,
3162 * but not before offset 0.
3163 * This may of course make the start overlap previous block,
3164 * and if we hit the offset 0 limit then the next block
3165 * can still overlap too.
3167 if (!eof
&& gotp
->br_startoff
!= NULLFILEOFF
) {
3168 if ((delay
&& gotp
->br_startblock
== HOLESTARTBLOCK
) ||
3169 (!delay
&& gotp
->br_startblock
== DELAYSTARTBLOCK
))
3170 nexto
= gotp
->br_startoff
+ gotp
->br_blockcount
;
3172 nexto
= gotp
->br_startoff
;
3174 nexto
= NULLFILEOFF
;
3176 align_off
+ align_alen
!= orig_end
&&
3177 align_off
+ align_alen
> nexto
)
3178 align_off
= nexto
> align_alen
? nexto
- align_alen
: 0;
3180 * If we're now overlapping the next or previous extent that
3181 * means we can't fit an extsz piece in this hole. Just move
3182 * the start forward to the first valid spot and set
3183 * the length so we hit the end.
3185 if (align_off
!= orig_off
&& align_off
< prevo
)
3187 if (align_off
+ align_alen
!= orig_end
&&
3188 align_off
+ align_alen
> nexto
&&
3189 nexto
!= NULLFILEOFF
) {
3190 ASSERT(nexto
> prevo
);
3191 align_alen
= nexto
- align_off
;
3195 * If realtime, and the result isn't a multiple of the realtime
3196 * extent size we need to remove blocks until it is.
3198 if (rt
&& (temp
= (align_alen
% mp
->m_sb
.sb_rextsize
))) {
3200 * We're not covering the original request, or
3201 * we won't be able to once we fix the length.
3203 if (orig_off
< align_off
||
3204 orig_end
> align_off
+ align_alen
||
3205 align_alen
- temp
< orig_alen
)
3208 * Try to fix it by moving the start up.
3210 if (align_off
+ temp
<= orig_off
) {
3215 * Try to fix it by moving the end in.
3217 else if (align_off
+ align_alen
- temp
>= orig_end
)
3220 * Set the start to the minimum then trim the length.
3223 align_alen
-= orig_off
- align_off
;
3224 align_off
= orig_off
;
3225 align_alen
-= align_alen
% mp
->m_sb
.sb_rextsize
;
3228 * Result doesn't cover the request, fail it.
3230 if (orig_off
< align_off
|| orig_end
> align_off
+ align_alen
)
3233 ASSERT(orig_off
>= align_off
);
3234 /* see MAXEXTLEN handling above */
3235 ASSERT(orig_end
<= align_off
+ align_alen
||
3236 align_alen
+ extsz
> MAXEXTLEN
);
3240 if (!eof
&& gotp
->br_startoff
!= NULLFILEOFF
)
3241 ASSERT(align_off
+ align_alen
<= gotp
->br_startoff
);
3242 if (prevp
->br_startoff
!= NULLFILEOFF
)
3243 ASSERT(align_off
>= prevp
->br_startoff
+ prevp
->br_blockcount
);
3251 #define XFS_ALLOC_GAP_UNITS 4
3255 struct xfs_bmalloca
*ap
) /* bmap alloc argument struct */
3257 xfs_fsblock_t adjust
; /* adjustment to block numbers */
3258 xfs_agnumber_t fb_agno
; /* ag number of ap->firstblock */
3259 xfs_mount_t
*mp
; /* mount point structure */
3260 int nullfb
; /* true if ap->firstblock isn't set */
3261 int rt
; /* true if inode is realtime */
3263 #define ISVALID(x,y) \
3265 (x) < mp->m_sb.sb_rblocks : \
3266 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
3267 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
3268 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
3270 mp
= ap
->ip
->i_mount
;
3271 nullfb
= *ap
->firstblock
== NULLFSBLOCK
;
3272 rt
= XFS_IS_REALTIME_INODE(ap
->ip
) &&
3273 xfs_alloc_is_userdata(ap
->datatype
);
3274 fb_agno
= nullfb
? NULLAGNUMBER
: XFS_FSB_TO_AGNO(mp
, *ap
->firstblock
);
3276 * If allocating at eof, and there's a previous real block,
3277 * try to use its last block as our starting point.
3279 if (ap
->eof
&& ap
->prev
.br_startoff
!= NULLFILEOFF
&&
3280 !isnullstartblock(ap
->prev
.br_startblock
) &&
3281 ISVALID(ap
->prev
.br_startblock
+ ap
->prev
.br_blockcount
,
3282 ap
->prev
.br_startblock
)) {
3283 ap
->blkno
= ap
->prev
.br_startblock
+ ap
->prev
.br_blockcount
;
3285 * Adjust for the gap between prevp and us.
3287 adjust
= ap
->offset
-
3288 (ap
->prev
.br_startoff
+ ap
->prev
.br_blockcount
);
3290 ISVALID(ap
->blkno
+ adjust
, ap
->prev
.br_startblock
))
3291 ap
->blkno
+= adjust
;
3294 * If not at eof, then compare the two neighbor blocks.
3295 * Figure out whether either one gives us a good starting point,
3296 * and pick the better one.
3298 else if (!ap
->eof
) {
3299 xfs_fsblock_t gotbno
; /* right side block number */
3300 xfs_fsblock_t gotdiff
=0; /* right side difference */
3301 xfs_fsblock_t prevbno
; /* left side block number */
3302 xfs_fsblock_t prevdiff
=0; /* left side difference */
3305 * If there's a previous (left) block, select a requested
3306 * start block based on it.
3308 if (ap
->prev
.br_startoff
!= NULLFILEOFF
&&
3309 !isnullstartblock(ap
->prev
.br_startblock
) &&
3310 (prevbno
= ap
->prev
.br_startblock
+
3311 ap
->prev
.br_blockcount
) &&
3312 ISVALID(prevbno
, ap
->prev
.br_startblock
)) {
3314 * Calculate gap to end of previous block.
3316 adjust
= prevdiff
= ap
->offset
-
3317 (ap
->prev
.br_startoff
+
3318 ap
->prev
.br_blockcount
);
3320 * Figure the startblock based on the previous block's
3321 * end and the gap size.
3323 * If the gap is large relative to the piece we're
3324 * allocating, or using it gives us an invalid block
3325 * number, then just use the end of the previous block.
3327 if (prevdiff
<= XFS_ALLOC_GAP_UNITS
* ap
->length
&&
3328 ISVALID(prevbno
+ prevdiff
,
3329 ap
->prev
.br_startblock
))
3334 * If the firstblock forbids it, can't use it,
3337 if (!rt
&& !nullfb
&&
3338 XFS_FSB_TO_AGNO(mp
, prevbno
) != fb_agno
)
3339 prevbno
= NULLFSBLOCK
;
3342 * No previous block or can't follow it, just default.
3345 prevbno
= NULLFSBLOCK
;
3347 * If there's a following (right) block, select a requested
3348 * start block based on it.
3350 if (!isnullstartblock(ap
->got
.br_startblock
)) {
3352 * Calculate gap to start of next block.
3354 adjust
= gotdiff
= ap
->got
.br_startoff
- ap
->offset
;
3356 * Figure the startblock based on the next block's
3357 * start and the gap size.
3359 gotbno
= ap
->got
.br_startblock
;
3362 * If the gap is large relative to the piece we're
3363 * allocating, or using it gives us an invalid block
3364 * number, then just use the start of the next block
3365 * offset by our length.
3367 if (gotdiff
<= XFS_ALLOC_GAP_UNITS
* ap
->length
&&
3368 ISVALID(gotbno
- gotdiff
, gotbno
))
3370 else if (ISVALID(gotbno
- ap
->length
, gotbno
)) {
3371 gotbno
-= ap
->length
;
3372 gotdiff
+= adjust
- ap
->length
;
3376 * If the firstblock forbids it, can't use it,
3379 if (!rt
&& !nullfb
&&
3380 XFS_FSB_TO_AGNO(mp
, gotbno
) != fb_agno
)
3381 gotbno
= NULLFSBLOCK
;
3384 * No next block, just default.
3387 gotbno
= NULLFSBLOCK
;
3389 * If both valid, pick the better one, else the only good
3390 * one, else ap->blkno is already set (to 0 or the inode block).
3392 if (prevbno
!= NULLFSBLOCK
&& gotbno
!= NULLFSBLOCK
)
3393 ap
->blkno
= prevdiff
<= gotdiff
? prevbno
: gotbno
;
3394 else if (prevbno
!= NULLFSBLOCK
)
3395 ap
->blkno
= prevbno
;
3396 else if (gotbno
!= NULLFSBLOCK
)
3403 xfs_bmap_longest_free_extent(
3404 struct xfs_trans
*tp
,
3409 struct xfs_mount
*mp
= tp
->t_mountp
;
3410 struct xfs_perag
*pag
;
3411 xfs_extlen_t longest
;
3414 pag
= xfs_perag_get(mp
, ag
);
3415 if (!pag
->pagf_init
) {
3416 error
= xfs_alloc_pagf_init(mp
, tp
, ag
, XFS_ALLOC_FLAG_TRYLOCK
);
3420 if (!pag
->pagf_init
) {
3426 longest
= xfs_alloc_longest_free_extent(mp
, pag
,
3427 xfs_alloc_min_freelist(mp
, pag
),
3428 xfs_ag_resv_needed(pag
, XFS_AG_RESV_NONE
));
3429 if (*blen
< longest
)
3438 xfs_bmap_select_minlen(
3439 struct xfs_bmalloca
*ap
,
3440 struct xfs_alloc_arg
*args
,
3444 if (notinit
|| *blen
< ap
->minlen
) {
3446 * Since we did a BUF_TRYLOCK above, it is possible that
3447 * there is space for this request.
3449 args
->minlen
= ap
->minlen
;
3450 } else if (*blen
< args
->maxlen
) {
3452 * If the best seen length is less than the request length,
3453 * use the best as the minimum.
3455 args
->minlen
= *blen
;
3458 * Otherwise we've seen an extent as big as maxlen, use that
3461 args
->minlen
= args
->maxlen
;
3466 xfs_bmap_btalloc_nullfb(
3467 struct xfs_bmalloca
*ap
,
3468 struct xfs_alloc_arg
*args
,
3471 struct xfs_mount
*mp
= ap
->ip
->i_mount
;
3472 xfs_agnumber_t ag
, startag
;
3476 args
->type
= XFS_ALLOCTYPE_START_BNO
;
3477 args
->total
= ap
->total
;
3479 startag
= ag
= XFS_FSB_TO_AGNO(mp
, args
->fsbno
);
3480 if (startag
== NULLAGNUMBER
)
3483 while (*blen
< args
->maxlen
) {
3484 error
= xfs_bmap_longest_free_extent(args
->tp
, ag
, blen
,
3489 if (++ag
== mp
->m_sb
.sb_agcount
)
3495 xfs_bmap_select_minlen(ap
, args
, blen
, notinit
);
3500 xfs_bmap_btalloc_filestreams(
3501 struct xfs_bmalloca
*ap
,
3502 struct xfs_alloc_arg
*args
,
3505 struct xfs_mount
*mp
= ap
->ip
->i_mount
;
3510 args
->type
= XFS_ALLOCTYPE_NEAR_BNO
;
3511 args
->total
= ap
->total
;
3513 ag
= XFS_FSB_TO_AGNO(mp
, args
->fsbno
);
3514 if (ag
== NULLAGNUMBER
)
3517 error
= xfs_bmap_longest_free_extent(args
->tp
, ag
, blen
, ¬init
);
3521 if (*blen
< args
->maxlen
) {
3522 error
= xfs_filestream_new_ag(ap
, &ag
);
3526 error
= xfs_bmap_longest_free_extent(args
->tp
, ag
, blen
,
3533 xfs_bmap_select_minlen(ap
, args
, blen
, notinit
);
3536 * Set the failure fallback case to look in the selected AG as stream
3539 ap
->blkno
= args
->fsbno
= XFS_AGB_TO_FSB(mp
, ag
, 0);
3545 struct xfs_bmalloca
*ap
) /* bmap alloc argument struct */
3547 xfs_mount_t
*mp
; /* mount point structure */
3548 xfs_alloctype_t atype
= 0; /* type for allocation routines */
3549 xfs_extlen_t align
= 0; /* minimum allocation alignment */
3550 xfs_agnumber_t fb_agno
; /* ag number of ap->firstblock */
3552 xfs_alloc_arg_t args
;
3554 xfs_extlen_t nextminlen
= 0;
3555 int nullfb
; /* true if ap->firstblock isn't set */
3563 mp
= ap
->ip
->i_mount
;
3565 /* stripe alignment for allocation is determined by mount parameters */
3567 if (mp
->m_swidth
&& (mp
->m_flags
& XFS_MOUNT_SWALLOC
))
3568 stripe_align
= mp
->m_swidth
;
3569 else if (mp
->m_dalign
)
3570 stripe_align
= mp
->m_dalign
;
3572 if (ap
->flags
& XFS_BMAPI_COWFORK
)
3573 align
= xfs_get_cowextsz_hint(ap
->ip
);
3574 else if (xfs_alloc_is_userdata(ap
->datatype
))
3575 align
= xfs_get_extsz_hint(ap
->ip
);
3577 error
= xfs_bmap_extsize_align(mp
, &ap
->got
, &ap
->prev
,
3578 align
, 0, ap
->eof
, 0, ap
->conv
,
3579 &ap
->offset
, &ap
->length
);
3585 nullfb
= *ap
->firstblock
== NULLFSBLOCK
;
3586 fb_agno
= nullfb
? NULLAGNUMBER
: XFS_FSB_TO_AGNO(mp
, *ap
->firstblock
);
3588 if (xfs_alloc_is_userdata(ap
->datatype
) &&
3589 xfs_inode_is_filestream(ap
->ip
)) {
3590 ag
= xfs_filestream_lookup_ag(ap
->ip
);
3591 ag
= (ag
!= NULLAGNUMBER
) ? ag
: 0;
3592 ap
->blkno
= XFS_AGB_TO_FSB(mp
, ag
, 0);
3594 ap
->blkno
= XFS_INO_TO_FSB(mp
, ap
->ip
->i_ino
);
3597 ap
->blkno
= *ap
->firstblock
;
3599 xfs_bmap_adjacent(ap
);
3602 * If allowed, use ap->blkno; otherwise must use firstblock since
3603 * it's in the right allocation group.
3605 if (nullfb
|| XFS_FSB_TO_AGNO(mp
, ap
->blkno
) == fb_agno
)
3608 ap
->blkno
= *ap
->firstblock
;
3610 * Normal allocation, done through xfs_alloc_vextent.
3612 tryagain
= isaligned
= 0;
3613 memset(&args
, 0, sizeof(args
));
3616 args
.fsbno
= ap
->blkno
;
3617 xfs_rmap_skip_owner_update(&args
.oinfo
);
3619 /* Trim the allocation back to the maximum an AG can fit. */
3620 args
.maxlen
= MIN(ap
->length
, mp
->m_ag_max_usable
);
3621 args
.firstblock
= *ap
->firstblock
;
3625 * Search for an allocation group with a single extent large
3626 * enough for the request. If one isn't found, then adjust
3627 * the minimum allocation size to the largest space found.
3629 if (xfs_alloc_is_userdata(ap
->datatype
) &&
3630 xfs_inode_is_filestream(ap
->ip
))
3631 error
= xfs_bmap_btalloc_filestreams(ap
, &args
, &blen
);
3633 error
= xfs_bmap_btalloc_nullfb(ap
, &args
, &blen
);
3636 } else if (ap
->dfops
->dop_low
) {
3637 if (xfs_inode_is_filestream(ap
->ip
))
3638 args
.type
= XFS_ALLOCTYPE_FIRST_AG
;
3640 args
.type
= XFS_ALLOCTYPE_START_BNO
;
3641 args
.total
= args
.minlen
= ap
->minlen
;
3643 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
3644 args
.total
= ap
->total
;
3645 args
.minlen
= ap
->minlen
;
3647 /* apply extent size hints if obtained earlier */
3650 if ((args
.mod
= (xfs_extlen_t
)do_mod(ap
->offset
, args
.prod
)))
3651 args
.mod
= (xfs_extlen_t
)(args
.prod
- args
.mod
);
3652 } else if (mp
->m_sb
.sb_blocksize
>= PAGE_SIZE
) {
3656 args
.prod
= PAGE_SIZE
>> mp
->m_sb
.sb_blocklog
;
3657 if ((args
.mod
= (xfs_extlen_t
)(do_mod(ap
->offset
, args
.prod
))))
3658 args
.mod
= (xfs_extlen_t
)(args
.prod
- args
.mod
);
3661 * If we are not low on available data blocks, and the
3662 * underlying logical volume manager is a stripe, and
3663 * the file offset is zero then try to allocate data
3664 * blocks on stripe unit boundary.
3665 * NOTE: ap->aeof is only set if the allocation length
3666 * is >= the stripe unit and the allocation offset is
3667 * at the end of file.
3669 if (!ap
->dfops
->dop_low
&& ap
->aeof
) {
3671 args
.alignment
= stripe_align
;
3675 * Adjust for alignment
3677 if (blen
> args
.alignment
&& blen
<= args
.maxlen
)
3678 args
.minlen
= blen
- args
.alignment
;
3679 args
.minalignslop
= 0;
3682 * First try an exact bno allocation.
3683 * If it fails then do a near or start bno
3684 * allocation with alignment turned on.
3688 args
.type
= XFS_ALLOCTYPE_THIS_BNO
;
3691 * Compute the minlen+alignment for the
3692 * next case. Set slop so that the value
3693 * of minlen+alignment+slop doesn't go up
3694 * between the calls.
3696 if (blen
> stripe_align
&& blen
<= args
.maxlen
)
3697 nextminlen
= blen
- stripe_align
;
3699 nextminlen
= args
.minlen
;
3700 if (nextminlen
+ stripe_align
> args
.minlen
+ 1)
3702 nextminlen
+ stripe_align
-
3705 args
.minalignslop
= 0;
3709 args
.minalignslop
= 0;
3711 args
.minleft
= ap
->minleft
;
3712 args
.wasdel
= ap
->wasdel
;
3713 args
.resv
= XFS_AG_RESV_NONE
;
3714 args
.datatype
= ap
->datatype
;
3715 if (ap
->datatype
& XFS_ALLOC_USERDATA_ZERO
)
3718 error
= xfs_alloc_vextent(&args
);
3722 if (tryagain
&& args
.fsbno
== NULLFSBLOCK
) {
3724 * Exact allocation failed. Now try with alignment
3728 args
.fsbno
= ap
->blkno
;
3729 args
.alignment
= stripe_align
;
3730 args
.minlen
= nextminlen
;
3731 args
.minalignslop
= 0;
3733 if ((error
= xfs_alloc_vextent(&args
)))
3736 if (isaligned
&& args
.fsbno
== NULLFSBLOCK
) {
3738 * allocation failed, so turn off alignment and
3742 args
.fsbno
= ap
->blkno
;
3744 if ((error
= xfs_alloc_vextent(&args
)))
3747 if (args
.fsbno
== NULLFSBLOCK
&& nullfb
&&
3748 args
.minlen
> ap
->minlen
) {
3749 args
.minlen
= ap
->minlen
;
3750 args
.type
= XFS_ALLOCTYPE_START_BNO
;
3751 args
.fsbno
= ap
->blkno
;
3752 if ((error
= xfs_alloc_vextent(&args
)))
3755 if (args
.fsbno
== NULLFSBLOCK
&& nullfb
) {
3757 args
.type
= XFS_ALLOCTYPE_FIRST_AG
;
3758 args
.total
= ap
->minlen
;
3759 if ((error
= xfs_alloc_vextent(&args
)))
3761 ap
->dfops
->dop_low
= true;
3763 if (args
.fsbno
!= NULLFSBLOCK
) {
3765 * check the allocation happened at the same or higher AG than
3766 * the first block that was allocated.
3768 ASSERT(*ap
->firstblock
== NULLFSBLOCK
||
3769 XFS_FSB_TO_AGNO(mp
, *ap
->firstblock
) <=
3770 XFS_FSB_TO_AGNO(mp
, args
.fsbno
));
3772 ap
->blkno
= args
.fsbno
;
3773 if (*ap
->firstblock
== NULLFSBLOCK
)
3774 *ap
->firstblock
= args
.fsbno
;
3775 ASSERT(nullfb
|| fb_agno
<= args
.agno
);
3776 ap
->length
= args
.len
;
3777 if (!(ap
->flags
& XFS_BMAPI_COWFORK
))
3778 ap
->ip
->i_d
.di_nblocks
+= args
.len
;
3779 xfs_trans_log_inode(ap
->tp
, ap
->ip
, XFS_ILOG_CORE
);
3781 ap
->ip
->i_delayed_blks
-= args
.len
;
3783 * Adjust the disk quota also. This was reserved
3786 xfs_trans_mod_dquot_byino(ap
->tp
, ap
->ip
,
3787 ap
->wasdel
? XFS_TRANS_DQ_DELBCOUNT
:
3788 XFS_TRANS_DQ_BCOUNT
,
3791 ap
->blkno
= NULLFSBLOCK
;
3798 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
3799 * It figures out where to ask the underlying allocator to put the new extent.
3803 struct xfs_bmalloca
*ap
) /* bmap alloc argument struct */
3805 if (XFS_IS_REALTIME_INODE(ap
->ip
) &&
3806 xfs_alloc_is_userdata(ap
->datatype
))
3807 return xfs_bmap_rtalloc(ap
);
3808 return xfs_bmap_btalloc(ap
);
3811 /* Trim extent to fit a logical block range. */
3814 struct xfs_bmbt_irec
*irec
,
3818 xfs_fileoff_t distance
;
3819 xfs_fileoff_t end
= bno
+ len
;
3821 if (irec
->br_startoff
+ irec
->br_blockcount
<= bno
||
3822 irec
->br_startoff
>= end
) {
3823 irec
->br_blockcount
= 0;
3827 if (irec
->br_startoff
< bno
) {
3828 distance
= bno
- irec
->br_startoff
;
3829 if (isnullstartblock(irec
->br_startblock
))
3830 irec
->br_startblock
= DELAYSTARTBLOCK
;
3831 if (irec
->br_startblock
!= DELAYSTARTBLOCK
&&
3832 irec
->br_startblock
!= HOLESTARTBLOCK
)
3833 irec
->br_startblock
+= distance
;
3834 irec
->br_startoff
+= distance
;
3835 irec
->br_blockcount
-= distance
;
3838 if (end
< irec
->br_startoff
+ irec
->br_blockcount
) {
3839 distance
= irec
->br_startoff
+ irec
->br_blockcount
- end
;
3840 irec
->br_blockcount
-= distance
;
3844 /* trim extent to within eof */
3846 xfs_trim_extent_eof(
3847 struct xfs_bmbt_irec
*irec
,
3848 struct xfs_inode
*ip
)
3851 xfs_trim_extent(irec
, 0, XFS_B_TO_FSB(ip
->i_mount
,
3852 i_size_read(VFS_I(ip
))));
3856 * Trim the returned map to the required bounds
3860 struct xfs_bmbt_irec
*mval
,
3861 struct xfs_bmbt_irec
*got
,
3869 if ((flags
& XFS_BMAPI_ENTIRE
) ||
3870 got
->br_startoff
+ got
->br_blockcount
<= obno
) {
3872 if (isnullstartblock(got
->br_startblock
))
3873 mval
->br_startblock
= DELAYSTARTBLOCK
;
3879 ASSERT((*bno
>= obno
) || (n
== 0));
3881 mval
->br_startoff
= *bno
;
3882 if (isnullstartblock(got
->br_startblock
))
3883 mval
->br_startblock
= DELAYSTARTBLOCK
;
3885 mval
->br_startblock
= got
->br_startblock
+
3886 (*bno
- got
->br_startoff
);
3888 * Return the minimum of what we got and what we asked for for
3889 * the length. We can use the len variable here because it is
3890 * modified below and we could have been there before coming
3891 * here if the first part of the allocation didn't overlap what
3894 mval
->br_blockcount
= XFS_FILBLKS_MIN(end
- *bno
,
3895 got
->br_blockcount
- (*bno
- got
->br_startoff
));
3896 mval
->br_state
= got
->br_state
;
3897 ASSERT(mval
->br_blockcount
<= len
);
3902 * Update and validate the extent map to return
3905 xfs_bmapi_update_map(
3906 struct xfs_bmbt_irec
**map
,
3914 xfs_bmbt_irec_t
*mval
= *map
;
3916 ASSERT((flags
& XFS_BMAPI_ENTIRE
) ||
3917 ((mval
->br_startoff
+ mval
->br_blockcount
) <= end
));
3918 ASSERT((flags
& XFS_BMAPI_ENTIRE
) || (mval
->br_blockcount
<= *len
) ||
3919 (mval
->br_startoff
< obno
));
3921 *bno
= mval
->br_startoff
+ mval
->br_blockcount
;
3923 if (*n
> 0 && mval
->br_startoff
== mval
[-1].br_startoff
) {
3924 /* update previous map with new information */
3925 ASSERT(mval
->br_startblock
== mval
[-1].br_startblock
);
3926 ASSERT(mval
->br_blockcount
> mval
[-1].br_blockcount
);
3927 ASSERT(mval
->br_state
== mval
[-1].br_state
);
3928 mval
[-1].br_blockcount
= mval
->br_blockcount
;
3929 mval
[-1].br_state
= mval
->br_state
;
3930 } else if (*n
> 0 && mval
->br_startblock
!= DELAYSTARTBLOCK
&&
3931 mval
[-1].br_startblock
!= DELAYSTARTBLOCK
&&
3932 mval
[-1].br_startblock
!= HOLESTARTBLOCK
&&
3933 mval
->br_startblock
== mval
[-1].br_startblock
+
3934 mval
[-1].br_blockcount
&&
3935 ((flags
& XFS_BMAPI_IGSTATE
) ||
3936 mval
[-1].br_state
== mval
->br_state
)) {
3937 ASSERT(mval
->br_startoff
==
3938 mval
[-1].br_startoff
+ mval
[-1].br_blockcount
);
3939 mval
[-1].br_blockcount
+= mval
->br_blockcount
;
3940 } else if (*n
> 0 &&
3941 mval
->br_startblock
== DELAYSTARTBLOCK
&&
3942 mval
[-1].br_startblock
== DELAYSTARTBLOCK
&&
3943 mval
->br_startoff
==
3944 mval
[-1].br_startoff
+ mval
[-1].br_blockcount
) {
3945 mval
[-1].br_blockcount
+= mval
->br_blockcount
;
3946 mval
[-1].br_state
= mval
->br_state
;
3947 } else if (!((*n
== 0) &&
3948 ((mval
->br_startoff
+ mval
->br_blockcount
) <=
3957 * Map file blocks to filesystem blocks without allocation.
3961 struct xfs_inode
*ip
,
3964 struct xfs_bmbt_irec
*mval
,
3968 struct xfs_mount
*mp
= ip
->i_mount
;
3969 struct xfs_ifork
*ifp
;
3970 struct xfs_bmbt_irec got
;
3977 int whichfork
= xfs_bmapi_whichfork(flags
);
3980 ASSERT(!(flags
& ~(XFS_BMAPI_ATTRFORK
|XFS_BMAPI_ENTIRE
|
3981 XFS_BMAPI_IGSTATE
|XFS_BMAPI_COWFORK
)));
3982 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_SHARED
|XFS_ILOCK_EXCL
));
3984 if (unlikely(XFS_TEST_ERROR(
3985 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
3986 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
3987 mp
, XFS_ERRTAG_BMAPIFORMAT
))) {
3988 XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW
, mp
);
3989 return -EFSCORRUPTED
;
3992 if (XFS_FORCED_SHUTDOWN(mp
))
3995 XFS_STATS_INC(mp
, xs_blk_mapr
);
3997 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
3999 /* No CoW fork? Return a hole. */
4000 if (whichfork
== XFS_COW_FORK
&& !ifp
) {
4001 mval
->br_startoff
= bno
;
4002 mval
->br_startblock
= HOLESTARTBLOCK
;
4003 mval
->br_blockcount
= len
;
4004 mval
->br_state
= XFS_EXT_NORM
;
4009 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
4010 error
= xfs_iread_extents(NULL
, ip
, whichfork
);
4015 if (!xfs_iext_lookup_extent(ip
, ifp
, bno
, &idx
, &got
))
4020 while (bno
< end
&& n
< *nmap
) {
4021 /* Reading past eof, act as though there's a hole up to end. */
4023 got
.br_startoff
= end
;
4024 if (got
.br_startoff
> bno
) {
4025 /* Reading in a hole. */
4026 mval
->br_startoff
= bno
;
4027 mval
->br_startblock
= HOLESTARTBLOCK
;
4028 mval
->br_blockcount
=
4029 XFS_FILBLKS_MIN(len
, got
.br_startoff
- bno
);
4030 mval
->br_state
= XFS_EXT_NORM
;
4031 bno
+= mval
->br_blockcount
;
4032 len
-= mval
->br_blockcount
;
4038 /* set up the extent map to return. */
4039 xfs_bmapi_trim_map(mval
, &got
, &bno
, len
, obno
, end
, n
, flags
);
4040 xfs_bmapi_update_map(&mval
, &bno
, &len
, obno
, end
, &n
, flags
);
4042 /* If we're done, stop now. */
4043 if (bno
>= end
|| n
>= *nmap
)
4046 /* Else go on to the next record. */
4047 if (!xfs_iext_get_extent(ifp
, ++idx
, &got
))
4055 * Add a delayed allocation extent to an inode. Blocks are reserved from the
4056 * global pool and the extent inserted into the inode in-core extent tree.
4058 * On entry, got refers to the first extent beyond the offset of the extent to
4059 * allocate or eof is specified if no such extent exists. On return, got refers
4060 * to the extent record that was inserted to the inode fork.
4062 * Note that the allocated extent may have been merged with contiguous extents
4063 * during insertion into the inode fork. Thus, got does not reflect the current
4064 * state of the inode fork on return. If necessary, the caller can use lastx to
4065 * look up the updated record in the inode fork.
4068 xfs_bmapi_reserve_delalloc(
4069 struct xfs_inode
*ip
,
4073 xfs_filblks_t prealloc
,
4074 struct xfs_bmbt_irec
*got
,
4075 xfs_extnum_t
*lastx
,
4078 struct xfs_mount
*mp
= ip
->i_mount
;
4079 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4081 xfs_extlen_t indlen
;
4082 char rt
= XFS_IS_REALTIME_INODE(ip
);
4085 xfs_fileoff_t aoff
= off
;
4088 * Cap the alloc length. Keep track of prealloc so we know whether to
4089 * tag the inode before we return.
4091 alen
= XFS_FILBLKS_MIN(len
+ prealloc
, MAXEXTLEN
);
4093 alen
= XFS_FILBLKS_MIN(alen
, got
->br_startoff
- aoff
);
4094 if (prealloc
&& alen
>= len
)
4095 prealloc
= alen
- len
;
4097 /* Figure out the extent size, adjust alen */
4098 if (whichfork
== XFS_COW_FORK
)
4099 extsz
= xfs_get_cowextsz_hint(ip
);
4101 extsz
= xfs_get_extsz_hint(ip
);
4103 struct xfs_bmbt_irec prev
;
4105 if (!xfs_iext_get_extent(ifp
, *lastx
- 1, &prev
))
4106 prev
.br_startoff
= NULLFILEOFF
;
4108 error
= xfs_bmap_extsize_align(mp
, got
, &prev
, extsz
, rt
, eof
,
4109 1, 0, &aoff
, &alen
);
4114 extsz
= alen
/ mp
->m_sb
.sb_rextsize
;
4117 * Make a transaction-less quota reservation for delayed allocation
4118 * blocks. This number gets adjusted later. We return if we haven't
4119 * allocated blocks already inside this loop.
4121 error
= xfs_trans_reserve_quota_nblks(NULL
, ip
, (long)alen
, 0,
4122 rt
? XFS_QMOPT_RES_RTBLKS
: XFS_QMOPT_RES_REGBLKS
);
4127 * Split changing sb for alen and indlen since they could be coming
4128 * from different places.
4130 indlen
= (xfs_extlen_t
)xfs_bmap_worst_indlen(ip
, alen
);
4134 error
= xfs_mod_frextents(mp
, -((int64_t)extsz
));
4136 error
= xfs_mod_fdblocks(mp
, -((int64_t)alen
), false);
4140 goto out_unreserve_quota
;
4142 error
= xfs_mod_fdblocks(mp
, -((int64_t)indlen
), false);
4144 goto out_unreserve_blocks
;
4147 ip
->i_delayed_blks
+= alen
;
4149 got
->br_startoff
= aoff
;
4150 got
->br_startblock
= nullstartblock(indlen
);
4151 got
->br_blockcount
= alen
;
4152 got
->br_state
= XFS_EXT_NORM
;
4154 xfs_bmap_add_extent_hole_delay(ip
, whichfork
, lastx
, got
);
4157 * Tag the inode if blocks were preallocated. Note that COW fork
4158 * preallocation can occur at the start or end of the extent, even when
4159 * prealloc == 0, so we must also check the aligned offset and length.
4161 if (whichfork
== XFS_DATA_FORK
&& prealloc
)
4162 xfs_inode_set_eofblocks_tag(ip
);
4163 if (whichfork
== XFS_COW_FORK
&& (prealloc
|| aoff
< off
|| alen
> len
))
4164 xfs_inode_set_cowblocks_tag(ip
);
4168 out_unreserve_blocks
:
4170 xfs_mod_frextents(mp
, extsz
);
4172 xfs_mod_fdblocks(mp
, alen
, false);
4173 out_unreserve_quota
:
4174 if (XFS_IS_QUOTA_ON(mp
))
4175 xfs_trans_unreserve_quota_nblks(NULL
, ip
, (long)alen
, 0, rt
?
4176 XFS_QMOPT_RES_RTBLKS
: XFS_QMOPT_RES_REGBLKS
);
4182 struct xfs_bmalloca
*bma
)
4184 struct xfs_mount
*mp
= bma
->ip
->i_mount
;
4185 int whichfork
= xfs_bmapi_whichfork(bma
->flags
);
4186 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
4187 int tmp_logflags
= 0;
4190 ASSERT(bma
->length
> 0);
4193 * For the wasdelay case, we could also just allocate the stuff asked
4194 * for in this bmap call but that wouldn't be as good.
4197 bma
->length
= (xfs_extlen_t
)bma
->got
.br_blockcount
;
4198 bma
->offset
= bma
->got
.br_startoff
;
4200 xfs_iext_get_extent(ifp
, bma
->idx
- 1, &bma
->prev
);
4202 bma
->length
= XFS_FILBLKS_MIN(bma
->length
, MAXEXTLEN
);
4204 bma
->length
= XFS_FILBLKS_MIN(bma
->length
,
4205 bma
->got
.br_startoff
- bma
->offset
);
4209 * Set the data type being allocated. For the data fork, the first data
4210 * in the file is treated differently to all other allocations. For the
4211 * attribute fork, we only need to ensure the allocated range is not on
4214 if (!(bma
->flags
& XFS_BMAPI_METADATA
)) {
4215 bma
->datatype
= XFS_ALLOC_NOBUSY
;
4216 if (whichfork
== XFS_DATA_FORK
) {
4217 if (bma
->offset
== 0)
4218 bma
->datatype
|= XFS_ALLOC_INITIAL_USER_DATA
;
4220 bma
->datatype
|= XFS_ALLOC_USERDATA
;
4222 if (bma
->flags
& XFS_BMAPI_ZERO
)
4223 bma
->datatype
|= XFS_ALLOC_USERDATA_ZERO
;
4226 bma
->minlen
= (bma
->flags
& XFS_BMAPI_CONTIG
) ? bma
->length
: 1;
4229 * Only want to do the alignment at the eof if it is userdata and
4230 * allocation length is larger than a stripe unit.
4232 if (mp
->m_dalign
&& bma
->length
>= mp
->m_dalign
&&
4233 !(bma
->flags
& XFS_BMAPI_METADATA
) && whichfork
== XFS_DATA_FORK
) {
4234 error
= xfs_bmap_isaeof(bma
, whichfork
);
4239 error
= xfs_bmap_alloc(bma
);
4244 bma
->cur
->bc_private
.b
.firstblock
= *bma
->firstblock
;
4245 if (bma
->blkno
== NULLFSBLOCK
)
4247 if ((ifp
->if_flags
& XFS_IFBROOT
) && !bma
->cur
) {
4248 bma
->cur
= xfs_bmbt_init_cursor(mp
, bma
->tp
, bma
->ip
, whichfork
);
4249 bma
->cur
->bc_private
.b
.firstblock
= *bma
->firstblock
;
4250 bma
->cur
->bc_private
.b
.dfops
= bma
->dfops
;
4253 * Bump the number of extents we've allocated
4259 bma
->cur
->bc_private
.b
.flags
=
4260 bma
->wasdel
? XFS_BTCUR_BPRV_WASDEL
: 0;
4262 bma
->got
.br_startoff
= bma
->offset
;
4263 bma
->got
.br_startblock
= bma
->blkno
;
4264 bma
->got
.br_blockcount
= bma
->length
;
4265 bma
->got
.br_state
= XFS_EXT_NORM
;
4268 * In the data fork, a wasdelay extent has been initialized, so
4269 * shouldn't be flagged as unwritten.
4271 * For the cow fork, however, we convert delalloc reservations
4272 * (extents allocated for speculative preallocation) to
4273 * allocated unwritten extents, and only convert the unwritten
4274 * extents to real extents when we're about to write the data.
4276 if ((!bma
->wasdel
|| (bma
->flags
& XFS_BMAPI_COWFORK
)) &&
4277 (bma
->flags
& XFS_BMAPI_PREALLOC
) &&
4278 xfs_sb_version_hasextflgbit(&mp
->m_sb
))
4279 bma
->got
.br_state
= XFS_EXT_UNWRITTEN
;
4282 error
= xfs_bmap_add_extent_delay_real(bma
, whichfork
);
4284 error
= xfs_bmap_add_extent_hole_real(bma
->tp
, bma
->ip
,
4285 whichfork
, &bma
->idx
, &bma
->cur
, &bma
->got
,
4286 bma
->firstblock
, bma
->dfops
, &bma
->logflags
);
4288 bma
->logflags
|= tmp_logflags
;
4293 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4294 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4295 * the neighbouring ones.
4297 xfs_iext_get_extent(ifp
, bma
->idx
, &bma
->got
);
4299 ASSERT(bma
->got
.br_startoff
<= bma
->offset
);
4300 ASSERT(bma
->got
.br_startoff
+ bma
->got
.br_blockcount
>=
4301 bma
->offset
+ bma
->length
);
4302 ASSERT(bma
->got
.br_state
== XFS_EXT_NORM
||
4303 bma
->got
.br_state
== XFS_EXT_UNWRITTEN
);
4308 xfs_bmapi_convert_unwritten(
4309 struct xfs_bmalloca
*bma
,
4310 struct xfs_bmbt_irec
*mval
,
4314 int whichfork
= xfs_bmapi_whichfork(flags
);
4315 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
4316 int tmp_logflags
= 0;
4319 /* check if we need to do unwritten->real conversion */
4320 if (mval
->br_state
== XFS_EXT_UNWRITTEN
&&
4321 (flags
& XFS_BMAPI_PREALLOC
))
4324 /* check if we need to do real->unwritten conversion */
4325 if (mval
->br_state
== XFS_EXT_NORM
&&
4326 (flags
& (XFS_BMAPI_PREALLOC
| XFS_BMAPI_CONVERT
)) !=
4327 (XFS_BMAPI_PREALLOC
| XFS_BMAPI_CONVERT
))
4331 * Modify (by adding) the state flag, if writing.
4333 ASSERT(mval
->br_blockcount
<= len
);
4334 if ((ifp
->if_flags
& XFS_IFBROOT
) && !bma
->cur
) {
4335 bma
->cur
= xfs_bmbt_init_cursor(bma
->ip
->i_mount
, bma
->tp
,
4336 bma
->ip
, whichfork
);
4337 bma
->cur
->bc_private
.b
.firstblock
= *bma
->firstblock
;
4338 bma
->cur
->bc_private
.b
.dfops
= bma
->dfops
;
4340 mval
->br_state
= (mval
->br_state
== XFS_EXT_UNWRITTEN
)
4341 ? XFS_EXT_NORM
: XFS_EXT_UNWRITTEN
;
4344 * Before insertion into the bmbt, zero the range being converted
4347 if (flags
& XFS_BMAPI_ZERO
) {
4348 error
= xfs_zero_extent(bma
->ip
, mval
->br_startblock
,
4349 mval
->br_blockcount
);
4354 error
= xfs_bmap_add_extent_unwritten_real(bma
->tp
, bma
->ip
, whichfork
,
4355 &bma
->idx
, &bma
->cur
, mval
, bma
->firstblock
, bma
->dfops
,
4358 * Log the inode core unconditionally in the unwritten extent conversion
4359 * path because the conversion might not have done so (e.g., if the
4360 * extent count hasn't changed). We need to make sure the inode is dirty
4361 * in the transaction for the sake of fsync(), even if nothing has
4362 * changed, because fsync() will not force the log for this transaction
4363 * unless it sees the inode pinned.
4365 * Note: If we're only converting cow fork extents, there aren't
4366 * any on-disk updates to make, so we don't need to log anything.
4368 if (whichfork
!= XFS_COW_FORK
)
4369 bma
->logflags
|= tmp_logflags
| XFS_ILOG_CORE
;
4374 * Update our extent pointer, given that
4375 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4376 * of the neighbouring ones.
4378 xfs_iext_get_extent(ifp
, bma
->idx
, &bma
->got
);
4381 * We may have combined previously unwritten space with written space,
4382 * so generate another request.
4384 if (mval
->br_blockcount
< len
)
4390 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4391 * extent state if necessary. Details behaviour is controlled by the flags
4392 * parameter. Only allocates blocks from a single allocation group, to avoid
4395 * The returned value in "firstblock" from the first call in a transaction
4396 * must be remembered and presented to subsequent calls in "firstblock".
4397 * An upper bound for the number of blocks to be allocated is supplied to
4398 * the first call in "total"; if no allocation group has that many free
4399 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4403 struct xfs_trans
*tp
, /* transaction pointer */
4404 struct xfs_inode
*ip
, /* incore inode */
4405 xfs_fileoff_t bno
, /* starting file offs. mapped */
4406 xfs_filblks_t len
, /* length to map in file */
4407 int flags
, /* XFS_BMAPI_... */
4408 xfs_fsblock_t
*firstblock
, /* first allocated block
4409 controls a.g. for allocs */
4410 xfs_extlen_t total
, /* total blocks needed */
4411 struct xfs_bmbt_irec
*mval
, /* output: map values */
4412 int *nmap
, /* i/o: mval size/count */
4413 struct xfs_defer_ops
*dfops
) /* i/o: list extents to free */
4415 struct xfs_mount
*mp
= ip
->i_mount
;
4416 struct xfs_ifork
*ifp
;
4417 struct xfs_bmalloca bma
= { NULL
}; /* args for xfs_bmap_alloc */
4418 xfs_fileoff_t end
; /* end of mapped file region */
4419 bool eof
= false; /* after the end of extents */
4420 int error
; /* error return */
4421 int n
; /* current extent index */
4422 xfs_fileoff_t obno
; /* old block number (offset) */
4423 int whichfork
; /* data or attr fork */
4426 xfs_fileoff_t orig_bno
; /* original block number value */
4427 int orig_flags
; /* original flags arg value */
4428 xfs_filblks_t orig_len
; /* original value of len arg */
4429 struct xfs_bmbt_irec
*orig_mval
; /* original value of mval */
4430 int orig_nmap
; /* original value of *nmap */
4438 whichfork
= xfs_bmapi_whichfork(flags
);
4441 ASSERT(*nmap
<= XFS_BMAP_MAX_NMAP
);
4442 ASSERT(!(flags
& XFS_BMAPI_IGSTATE
));
4443 ASSERT(tp
!= NULL
||
4444 (flags
& (XFS_BMAPI_CONVERT
| XFS_BMAPI_COWFORK
)) ==
4445 (XFS_BMAPI_CONVERT
| XFS_BMAPI_COWFORK
));
4447 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_LOCAL
);
4448 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
4449 ASSERT(!(flags
& XFS_BMAPI_REMAP
));
4451 /* zeroing is for currently only for data extents, not metadata */
4452 ASSERT((flags
& (XFS_BMAPI_METADATA
| XFS_BMAPI_ZERO
)) !=
4453 (XFS_BMAPI_METADATA
| XFS_BMAPI_ZERO
));
4455 * we can allocate unwritten extents or pre-zero allocated blocks,
4456 * but it makes no sense to do both at once. This would result in
4457 * zeroing the unwritten extent twice, but it still being an
4458 * unwritten extent....
4460 ASSERT((flags
& (XFS_BMAPI_PREALLOC
| XFS_BMAPI_ZERO
)) !=
4461 (XFS_BMAPI_PREALLOC
| XFS_BMAPI_ZERO
));
4463 if (unlikely(XFS_TEST_ERROR(
4464 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
4465 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
4466 mp
, XFS_ERRTAG_BMAPIFORMAT
))) {
4467 XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW
, mp
);
4468 return -EFSCORRUPTED
;
4471 if (XFS_FORCED_SHUTDOWN(mp
))
4474 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4476 XFS_STATS_INC(mp
, xs_blk_mapw
);
4478 if (*firstblock
== NULLFSBLOCK
) {
4479 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
)
4480 bma
.minleft
= be16_to_cpu(ifp
->if_broot
->bb_level
) + 1;
4487 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
4488 error
= xfs_iread_extents(tp
, ip
, whichfork
);
4497 if (!xfs_iext_lookup_extent(ip
, ifp
, bno
, &bma
.idx
, &bma
.got
))
4499 if (!xfs_iext_get_extent(ifp
, bma
.idx
- 1, &bma
.prev
))
4500 bma
.prev
.br_startoff
= NULLFILEOFF
;
4506 bma
.firstblock
= firstblock
;
4508 while (bno
< end
&& n
< *nmap
) {
4509 bool need_alloc
= false, wasdelay
= false;
4511 /* in hole or beyoned EOF? */
4512 if (eof
|| bma
.got
.br_startoff
> bno
) {
4513 if (flags
& XFS_BMAPI_DELALLOC
) {
4515 * For the COW fork we can reasonably get a
4516 * request for converting an extent that races
4517 * with other threads already having converted
4518 * part of it, as there converting COW to
4519 * regular blocks is not protected using the
4522 ASSERT(flags
& XFS_BMAPI_COWFORK
);
4523 if (!(flags
& XFS_BMAPI_COWFORK
)) {
4528 if (eof
|| bno
>= end
)
4533 } else if (isnullstartblock(bma
.got
.br_startblock
)) {
4538 * First, deal with the hole before the allocated space
4539 * that we found, if any.
4541 if (need_alloc
|| wasdelay
) {
4543 bma
.conv
= !!(flags
& XFS_BMAPI_CONVERT
);
4544 bma
.wasdel
= wasdelay
;
4549 * There's a 32/64 bit type mismatch between the
4550 * allocation length request (which can be 64 bits in
4551 * length) and the bma length request, which is
4552 * xfs_extlen_t and therefore 32 bits. Hence we have to
4553 * check for 32-bit overflows and handle them here.
4555 if (len
> (xfs_filblks_t
)MAXEXTLEN
)
4556 bma
.length
= MAXEXTLEN
;
4561 ASSERT(bma
.length
> 0);
4562 error
= xfs_bmapi_allocate(&bma
);
4565 if (bma
.blkno
== NULLFSBLOCK
)
4569 * If this is a CoW allocation, record the data in
4570 * the refcount btree for orphan recovery.
4572 if (whichfork
== XFS_COW_FORK
) {
4573 error
= xfs_refcount_alloc_cow_extent(mp
, dfops
,
4574 bma
.blkno
, bma
.length
);
4580 /* Deal with the allocated space we found. */
4581 xfs_bmapi_trim_map(mval
, &bma
.got
, &bno
, len
, obno
,
4584 /* Execute unwritten extent conversion if necessary */
4585 error
= xfs_bmapi_convert_unwritten(&bma
, mval
, len
, flags
);
4586 if (error
== -EAGAIN
)
4591 /* update the extent map to return */
4592 xfs_bmapi_update_map(&mval
, &bno
, &len
, obno
, end
, &n
, flags
);
4595 * If we're done, stop now. Stop when we've allocated
4596 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4597 * the transaction may get too big.
4599 if (bno
>= end
|| n
>= *nmap
|| bma
.nallocs
>= *nmap
)
4602 /* Else go on to the next record. */
4604 if (!xfs_iext_get_extent(ifp
, ++bma
.idx
, &bma
.got
))
4610 * Transform from btree to extents, give it cur.
4612 if (xfs_bmap_wants_extents(ip
, whichfork
)) {
4613 int tmp_logflags
= 0;
4616 error
= xfs_bmap_btree_to_extents(tp
, ip
, bma
.cur
,
4617 &tmp_logflags
, whichfork
);
4618 bma
.logflags
|= tmp_logflags
;
4623 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
||
4624 XFS_IFORK_NEXTENTS(ip
, whichfork
) >
4625 XFS_IFORK_MAXEXT(ip
, whichfork
));
4629 * Log everything. Do this after conversion, there's no point in
4630 * logging the extent records if we've converted to btree format.
4632 if ((bma
.logflags
& xfs_ilog_fext(whichfork
)) &&
4633 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
4634 bma
.logflags
&= ~xfs_ilog_fext(whichfork
);
4635 else if ((bma
.logflags
& xfs_ilog_fbroot(whichfork
)) &&
4636 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)
4637 bma
.logflags
&= ~xfs_ilog_fbroot(whichfork
);
4639 * Log whatever the flags say, even if error. Otherwise we might miss
4640 * detecting a case where the data is changed, there's an error,
4641 * and it's not logged so we don't shutdown when we should.
4644 xfs_trans_log_inode(tp
, ip
, bma
.logflags
);
4648 ASSERT(*firstblock
== NULLFSBLOCK
||
4649 XFS_FSB_TO_AGNO(mp
, *firstblock
) <=
4651 bma
.cur
->bc_private
.b
.firstblock
));
4652 *firstblock
= bma
.cur
->bc_private
.b
.firstblock
;
4654 xfs_btree_del_cursor(bma
.cur
,
4655 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
4658 xfs_bmap_validate_ret(orig_bno
, orig_len
, orig_flags
, orig_mval
,
4665 struct xfs_trans
*tp
,
4666 struct xfs_inode
*ip
,
4669 xfs_fsblock_t startblock
,
4670 struct xfs_defer_ops
*dfops
)
4672 struct xfs_mount
*mp
= ip
->i_mount
;
4673 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, XFS_DATA_FORK
);
4674 struct xfs_btree_cur
*cur
= NULL
;
4675 xfs_fsblock_t firstblock
= NULLFSBLOCK
;
4676 struct xfs_bmbt_irec got
;
4678 int logflags
= 0, error
;
4681 ASSERT(len
<= (xfs_filblks_t
)MAXEXTLEN
);
4682 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
4684 if (unlikely(XFS_TEST_ERROR(
4685 (XFS_IFORK_FORMAT(ip
, XFS_DATA_FORK
) != XFS_DINODE_FMT_EXTENTS
&&
4686 XFS_IFORK_FORMAT(ip
, XFS_DATA_FORK
) != XFS_DINODE_FMT_BTREE
),
4687 mp
, XFS_ERRTAG_BMAPIFORMAT
))) {
4688 XFS_ERROR_REPORT("xfs_bmapi_remap", XFS_ERRLEVEL_LOW
, mp
);
4689 return -EFSCORRUPTED
;
4692 if (XFS_FORCED_SHUTDOWN(mp
))
4695 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
4696 error
= xfs_iread_extents(NULL
, ip
, XFS_DATA_FORK
);
4701 if (xfs_iext_lookup_extent(ip
, ifp
, bno
, &idx
, &got
)) {
4702 /* make sure we only reflink into a hole. */
4703 ASSERT(got
.br_startoff
> bno
);
4704 ASSERT(got
.br_startoff
- bno
>= len
);
4707 ip
->i_d
.di_nblocks
+= len
;
4708 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
4710 if (ifp
->if_flags
& XFS_IFBROOT
) {
4711 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, XFS_DATA_FORK
);
4712 cur
->bc_private
.b
.firstblock
= firstblock
;
4713 cur
->bc_private
.b
.dfops
= dfops
;
4714 cur
->bc_private
.b
.flags
= 0;
4717 got
.br_startoff
= bno
;
4718 got
.br_startblock
= startblock
;
4719 got
.br_blockcount
= len
;
4720 got
.br_state
= XFS_EXT_NORM
;
4722 error
= xfs_bmap_add_extent_hole_real(tp
, ip
, XFS_DATA_FORK
, &idx
, &cur
,
4723 &got
, &firstblock
, dfops
, &logflags
);
4727 if (xfs_bmap_wants_extents(ip
, XFS_DATA_FORK
)) {
4728 int tmp_logflags
= 0;
4730 error
= xfs_bmap_btree_to_extents(tp
, ip
, cur
,
4731 &tmp_logflags
, XFS_DATA_FORK
);
4732 logflags
|= tmp_logflags
;
4736 if (ip
->i_d
.di_format
!= XFS_DINODE_FMT_EXTENTS
)
4737 logflags
&= ~XFS_ILOG_DEXT
;
4738 else if (ip
->i_d
.di_format
!= XFS_DINODE_FMT_BTREE
)
4739 logflags
&= ~XFS_ILOG_DBROOT
;
4742 xfs_trans_log_inode(tp
, ip
, logflags
);
4744 xfs_btree_del_cursor(cur
,
4745 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
4751 * When a delalloc extent is split (e.g., due to a hole punch), the original
4752 * indlen reservation must be shared across the two new extents that are left
4755 * Given the original reservation and the worst case indlen for the two new
4756 * extents (as calculated by xfs_bmap_worst_indlen()), split the original
4757 * reservation fairly across the two new extents. If necessary, steal available
4758 * blocks from a deleted extent to make up a reservation deficiency (e.g., if
4759 * ores == 1). The number of stolen blocks is returned. The availability and
4760 * subsequent accounting of stolen blocks is the responsibility of the caller.
4762 static xfs_filblks_t
4763 xfs_bmap_split_indlen(
4764 xfs_filblks_t ores
, /* original res. */
4765 xfs_filblks_t
*indlen1
, /* ext1 worst indlen */
4766 xfs_filblks_t
*indlen2
, /* ext2 worst indlen */
4767 xfs_filblks_t avail
) /* stealable blocks */
4769 xfs_filblks_t len1
= *indlen1
;
4770 xfs_filblks_t len2
= *indlen2
;
4771 xfs_filblks_t nres
= len1
+ len2
; /* new total res. */
4772 xfs_filblks_t stolen
= 0;
4773 xfs_filblks_t resfactor
;
4776 * Steal as many blocks as we can to try and satisfy the worst case
4777 * indlen for both new extents.
4779 if (ores
< nres
&& avail
)
4780 stolen
= XFS_FILBLKS_MIN(nres
- ores
, avail
);
4783 /* nothing else to do if we've satisfied the new reservation */
4788 * We can't meet the total required reservation for the two extents.
4789 * Calculate the percent of the overall shortage between both extents
4790 * and apply this percentage to each of the requested indlen values.
4791 * This distributes the shortage fairly and reduces the chances that one
4792 * of the two extents is left with nothing when extents are repeatedly
4795 resfactor
= (ores
* 100);
4796 do_div(resfactor
, nres
);
4801 ASSERT(len1
+ len2
<= ores
);
4802 ASSERT(len1
< *indlen1
&& len2
< *indlen2
);
4805 * Hand out the remainder to each extent. If one of the two reservations
4806 * is zero, we want to make sure that one gets a block first. The loop
4807 * below starts with len1, so hand len2 a block right off the bat if it
4810 ores
-= (len1
+ len2
);
4811 ASSERT((*indlen1
- len1
) + (*indlen2
- len2
) >= ores
);
4812 if (ores
&& !len2
&& *indlen2
) {
4817 if (len1
< *indlen1
) {
4823 if (len2
< *indlen2
) {
4836 xfs_bmap_del_extent_delay(
4837 struct xfs_inode
*ip
,
4840 struct xfs_bmbt_irec
*got
,
4841 struct xfs_bmbt_irec
*del
)
4843 struct xfs_mount
*mp
= ip
->i_mount
;
4844 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4845 struct xfs_bmbt_irec
new;
4846 int64_t da_old
, da_new
, da_diff
= 0;
4847 xfs_fileoff_t del_endoff
, got_endoff
;
4848 xfs_filblks_t got_indlen
, new_indlen
, stolen
;
4849 int error
= 0, state
= 0;
4852 XFS_STATS_INC(mp
, xs_del_exlist
);
4854 isrt
= (whichfork
== XFS_DATA_FORK
) && XFS_IS_REALTIME_INODE(ip
);
4855 del_endoff
= del
->br_startoff
+ del
->br_blockcount
;
4856 got_endoff
= got
->br_startoff
+ got
->br_blockcount
;
4857 da_old
= startblockval(got
->br_startblock
);
4861 ASSERT(*idx
<= xfs_iext_count(ifp
));
4862 ASSERT(del
->br_blockcount
> 0);
4863 ASSERT(got
->br_startoff
<= del
->br_startoff
);
4864 ASSERT(got_endoff
>= del_endoff
);
4867 uint64_t rtexts
= XFS_FSB_TO_B(mp
, del
->br_blockcount
);
4869 do_div(rtexts
, mp
->m_sb
.sb_rextsize
);
4870 xfs_mod_frextents(mp
, rtexts
);
4874 * Update the inode delalloc counter now and wait to update the
4875 * sb counters as we might have to borrow some blocks for the
4876 * indirect block accounting.
4878 error
= xfs_trans_reserve_quota_nblks(NULL
, ip
,
4879 -((long)del
->br_blockcount
), 0,
4880 isrt
? XFS_QMOPT_RES_RTBLKS
: XFS_QMOPT_RES_REGBLKS
);
4883 ip
->i_delayed_blks
-= del
->br_blockcount
;
4885 if (whichfork
== XFS_COW_FORK
)
4886 state
|= BMAP_COWFORK
;
4888 if (got
->br_startoff
== del
->br_startoff
)
4889 state
|= BMAP_LEFT_CONTIG
;
4890 if (got_endoff
== del_endoff
)
4891 state
|= BMAP_RIGHT_CONTIG
;
4893 switch (state
& (BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
)) {
4894 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
4896 * Matches the whole extent. Delete the entry.
4898 xfs_iext_remove(ip
, *idx
, 1, state
);
4901 case BMAP_LEFT_CONTIG
:
4903 * Deleting the first part of the extent.
4905 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
4906 got
->br_startoff
= del_endoff
;
4907 got
->br_blockcount
-= del
->br_blockcount
;
4908 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
,
4909 got
->br_blockcount
), da_old
);
4910 got
->br_startblock
= nullstartblock((int)da_new
);
4911 xfs_iext_update_extent(ifp
, *idx
, got
);
4912 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
4914 case BMAP_RIGHT_CONTIG
:
4916 * Deleting the last part of the extent.
4918 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
4919 got
->br_blockcount
= got
->br_blockcount
- del
->br_blockcount
;
4920 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
,
4921 got
->br_blockcount
), da_old
);
4922 got
->br_startblock
= nullstartblock((int)da_new
);
4923 xfs_iext_update_extent(ifp
, *idx
, got
);
4924 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
4928 * Deleting the middle of the extent.
4930 * Distribute the original indlen reservation across the two new
4931 * extents. Steal blocks from the deleted extent if necessary.
4932 * Stealing blocks simply fudges the fdblocks accounting below.
4933 * Warn if either of the new indlen reservations is zero as this
4934 * can lead to delalloc problems.
4936 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
4938 got
->br_blockcount
= del
->br_startoff
- got
->br_startoff
;
4939 got_indlen
= xfs_bmap_worst_indlen(ip
, got
->br_blockcount
);
4941 new.br_blockcount
= got_endoff
- del_endoff
;
4942 new_indlen
= xfs_bmap_worst_indlen(ip
, new.br_blockcount
);
4944 WARN_ON_ONCE(!got_indlen
|| !new_indlen
);
4945 stolen
= xfs_bmap_split_indlen(da_old
, &got_indlen
, &new_indlen
,
4946 del
->br_blockcount
);
4948 got
->br_startblock
= nullstartblock((int)got_indlen
);
4949 xfs_iext_update_extent(ifp
, *idx
, got
);
4950 trace_xfs_bmap_post_update(ip
, *idx
, 0, _THIS_IP_
);
4952 new.br_startoff
= del_endoff
;
4953 new.br_state
= got
->br_state
;
4954 new.br_startblock
= nullstartblock((int)new_indlen
);
4957 xfs_iext_insert(ip
, *idx
, 1, &new, state
);
4959 da_new
= got_indlen
+ new_indlen
- stolen
;
4960 del
->br_blockcount
-= stolen
;
4964 ASSERT(da_old
>= da_new
);
4965 da_diff
= da_old
- da_new
;
4967 da_diff
+= del
->br_blockcount
;
4969 xfs_mod_fdblocks(mp
, da_diff
, false);
4974 xfs_bmap_del_extent_cow(
4975 struct xfs_inode
*ip
,
4977 struct xfs_bmbt_irec
*got
,
4978 struct xfs_bmbt_irec
*del
)
4980 struct xfs_mount
*mp
= ip
->i_mount
;
4981 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, XFS_COW_FORK
);
4982 struct xfs_bmbt_irec
new;
4983 xfs_fileoff_t del_endoff
, got_endoff
;
4984 int state
= BMAP_COWFORK
;
4986 XFS_STATS_INC(mp
, xs_del_exlist
);
4988 del_endoff
= del
->br_startoff
+ del
->br_blockcount
;
4989 got_endoff
= got
->br_startoff
+ got
->br_blockcount
;
4992 ASSERT(*idx
<= xfs_iext_count(ifp
));
4993 ASSERT(del
->br_blockcount
> 0);
4994 ASSERT(got
->br_startoff
<= del
->br_startoff
);
4995 ASSERT(got_endoff
>= del_endoff
);
4996 ASSERT(!isnullstartblock(got
->br_startblock
));
4998 if (got
->br_startoff
== del
->br_startoff
)
4999 state
|= BMAP_LEFT_CONTIG
;
5000 if (got_endoff
== del_endoff
)
5001 state
|= BMAP_RIGHT_CONTIG
;
5003 switch (state
& (BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
)) {
5004 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
5006 * Matches the whole extent. Delete the entry.
5008 xfs_iext_remove(ip
, *idx
, 1, state
);
5011 case BMAP_LEFT_CONTIG
:
5013 * Deleting the first part of the extent.
5015 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
5016 got
->br_startoff
= del_endoff
;
5017 got
->br_blockcount
-= del
->br_blockcount
;
5018 got
->br_startblock
= del
->br_startblock
+ del
->br_blockcount
;
5019 xfs_iext_update_extent(ifp
, *idx
, got
);
5020 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
5022 case BMAP_RIGHT_CONTIG
:
5024 * Deleting the last part of the extent.
5026 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
5027 got
->br_blockcount
-= del
->br_blockcount
;
5028 xfs_iext_update_extent(ifp
, *idx
, got
);
5029 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
5033 * Deleting the middle of the extent.
5035 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
5036 got
->br_blockcount
= del
->br_startoff
- got
->br_startoff
;
5037 xfs_iext_update_extent(ifp
, *idx
, got
);
5038 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
5040 new.br_startoff
= del_endoff
;
5041 new.br_blockcount
= got_endoff
- del_endoff
;
5042 new.br_state
= got
->br_state
;
5043 new.br_startblock
= del
->br_startblock
+ del
->br_blockcount
;
5046 xfs_iext_insert(ip
, *idx
, 1, &new, state
);
5052 * Called by xfs_bmapi to update file extent records and the btree
5053 * after removing space (or undoing a delayed allocation).
5055 STATIC
int /* error */
5056 xfs_bmap_del_extent(
5057 xfs_inode_t
*ip
, /* incore inode pointer */
5058 xfs_trans_t
*tp
, /* current transaction pointer */
5059 xfs_extnum_t
*idx
, /* extent number to update/delete */
5060 struct xfs_defer_ops
*dfops
, /* list of extents to be freed */
5061 xfs_btree_cur_t
*cur
, /* if null, not a btree */
5062 xfs_bmbt_irec_t
*del
, /* data to remove from extents */
5063 int *logflagsp
, /* inode logging flags */
5064 int whichfork
, /* data or attr fork */
5065 int bflags
) /* bmapi flags */
5067 xfs_filblks_t da_new
; /* new delay-alloc indirect blocks */
5068 xfs_filblks_t da_old
; /* old delay-alloc indirect blocks */
5069 xfs_fsblock_t del_endblock
=0; /* first block past del */
5070 xfs_fileoff_t del_endoff
; /* first offset past del */
5071 int delay
; /* current block is delayed allocated */
5072 int do_fx
; /* free extent at end of routine */
5073 xfs_bmbt_rec_host_t
*ep
; /* current extent entry pointer */
5074 int error
; /* error return value */
5075 int flags
; /* inode logging flags */
5076 xfs_bmbt_irec_t got
; /* current extent entry */
5077 xfs_fileoff_t got_endoff
; /* first offset past got */
5078 int i
; /* temp state */
5079 xfs_ifork_t
*ifp
; /* inode fork pointer */
5080 xfs_mount_t
*mp
; /* mount structure */
5081 xfs_filblks_t nblks
; /* quota/sb block count */
5082 xfs_bmbt_irec_t
new; /* new record to be inserted */
5084 uint qfield
; /* quota field to update */
5085 xfs_filblks_t temp
; /* for indirect length calculations */
5086 xfs_filblks_t temp2
; /* for indirect length calculations */
5090 XFS_STATS_INC(mp
, xs_del_exlist
);
5092 if (whichfork
== XFS_ATTR_FORK
)
5093 state
|= BMAP_ATTRFORK
;
5094 else if (whichfork
== XFS_COW_FORK
)
5095 state
|= BMAP_COWFORK
;
5097 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5098 ASSERT((*idx
>= 0) && (*idx
< xfs_iext_count(ifp
)));
5099 ASSERT(del
->br_blockcount
> 0);
5100 ep
= xfs_iext_get_ext(ifp
, *idx
);
5101 xfs_bmbt_get_all(ep
, &got
);
5102 ASSERT(got
.br_startoff
<= del
->br_startoff
);
5103 del_endoff
= del
->br_startoff
+ del
->br_blockcount
;
5104 got_endoff
= got
.br_startoff
+ got
.br_blockcount
;
5105 ASSERT(got_endoff
>= del_endoff
);
5106 delay
= isnullstartblock(got
.br_startblock
);
5107 ASSERT(isnullstartblock(del
->br_startblock
) == delay
);
5112 * If deleting a real allocation, must free up the disk space.
5115 flags
= XFS_ILOG_CORE
;
5117 * Realtime allocation. Free it and record di_nblocks update.
5119 if (whichfork
== XFS_DATA_FORK
&& XFS_IS_REALTIME_INODE(ip
)) {
5123 ASSERT(do_mod(del
->br_blockcount
,
5124 mp
->m_sb
.sb_rextsize
) == 0);
5125 ASSERT(do_mod(del
->br_startblock
,
5126 mp
->m_sb
.sb_rextsize
) == 0);
5127 bno
= del
->br_startblock
;
5128 len
= del
->br_blockcount
;
5129 do_div(bno
, mp
->m_sb
.sb_rextsize
);
5130 do_div(len
, mp
->m_sb
.sb_rextsize
);
5131 error
= xfs_rtfree_extent(tp
, bno
, (xfs_extlen_t
)len
);
5135 nblks
= len
* mp
->m_sb
.sb_rextsize
;
5136 qfield
= XFS_TRANS_DQ_RTBCOUNT
;
5139 * Ordinary allocation.
5143 nblks
= del
->br_blockcount
;
5144 qfield
= XFS_TRANS_DQ_BCOUNT
;
5147 * Set up del_endblock and cur for later.
5149 del_endblock
= del
->br_startblock
+ del
->br_blockcount
;
5151 if ((error
= xfs_bmbt_lookup_eq(cur
, got
.br_startoff
,
5152 got
.br_startblock
, got
.br_blockcount
,
5155 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
5157 da_old
= da_new
= 0;
5159 da_old
= startblockval(got
.br_startblock
);
5166 * Set flag value to use in switch statement.
5167 * Left-contig is 2, right-contig is 1.
5169 switch (((got
.br_startoff
== del
->br_startoff
) << 1) |
5170 (got_endoff
== del_endoff
)) {
5173 * Matches the whole extent. Delete the entry.
5175 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
5176 xfs_iext_remove(ip
, *idx
, 1,
5177 whichfork
== XFS_ATTR_FORK
? BMAP_ATTRFORK
: 0);
5182 XFS_IFORK_NEXT_SET(ip
, whichfork
,
5183 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
5184 flags
|= XFS_ILOG_CORE
;
5186 flags
|= xfs_ilog_fext(whichfork
);
5189 if ((error
= xfs_btree_delete(cur
, &i
)))
5191 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
5196 * Deleting the first part of the extent.
5198 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
5199 xfs_bmbt_set_startoff(ep
, del_endoff
);
5200 temp
= got
.br_blockcount
- del
->br_blockcount
;
5201 xfs_bmbt_set_blockcount(ep
, temp
);
5203 temp
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
5205 xfs_bmbt_set_startblock(ep
, nullstartblock((int)temp
));
5206 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
5210 xfs_bmbt_set_startblock(ep
, del_endblock
);
5211 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
5213 flags
|= xfs_ilog_fext(whichfork
);
5216 if ((error
= xfs_bmbt_update(cur
, del_endoff
, del_endblock
,
5217 got
.br_blockcount
- del
->br_blockcount
,
5224 * Deleting the last part of the extent.
5226 temp
= got
.br_blockcount
- del
->br_blockcount
;
5227 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
5228 xfs_bmbt_set_blockcount(ep
, temp
);
5230 temp
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
5232 xfs_bmbt_set_startblock(ep
, nullstartblock((int)temp
));
5233 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
5237 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
5239 flags
|= xfs_ilog_fext(whichfork
);
5242 if ((error
= xfs_bmbt_update(cur
, got
.br_startoff
,
5244 got
.br_blockcount
- del
->br_blockcount
,
5251 * Deleting the middle of the extent.
5253 temp
= del
->br_startoff
- got
.br_startoff
;
5254 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
5255 xfs_bmbt_set_blockcount(ep
, temp
);
5256 new.br_startoff
= del_endoff
;
5257 temp2
= got_endoff
- del_endoff
;
5258 new.br_blockcount
= temp2
;
5259 new.br_state
= got
.br_state
;
5261 new.br_startblock
= del_endblock
;
5262 flags
|= XFS_ILOG_CORE
;
5264 if ((error
= xfs_bmbt_update(cur
,
5266 got
.br_startblock
, temp
,
5269 if ((error
= xfs_btree_increment(cur
, 0, &i
)))
5271 cur
->bc_rec
.b
= new;
5272 error
= xfs_btree_insert(cur
, &i
);
5273 if (error
&& error
!= -ENOSPC
)
5276 * If get no-space back from btree insert,
5277 * it tried a split, and we have a zero
5278 * block reservation.
5279 * Fix up our state and return the error.
5281 if (error
== -ENOSPC
) {
5283 * Reset the cursor, don't trust
5284 * it after any insert operation.
5286 if ((error
= xfs_bmbt_lookup_eq(cur
,
5291 XFS_WANT_CORRUPTED_GOTO(mp
,
5294 * Update the btree record back
5295 * to the original value.
5297 if ((error
= xfs_bmbt_update(cur
,
5304 * Reset the extent record back
5305 * to the original value.
5307 xfs_bmbt_set_blockcount(ep
,
5313 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
5315 flags
|= xfs_ilog_fext(whichfork
);
5316 XFS_IFORK_NEXT_SET(ip
, whichfork
,
5317 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
5319 xfs_filblks_t stolen
;
5320 ASSERT(whichfork
== XFS_DATA_FORK
);
5323 * Distribute the original indlen reservation across the
5324 * two new extents. Steal blocks from the deleted extent
5325 * if necessary. Stealing blocks simply fudges the
5326 * fdblocks accounting in xfs_bunmapi().
5328 temp
= xfs_bmap_worst_indlen(ip
, got
.br_blockcount
);
5329 temp2
= xfs_bmap_worst_indlen(ip
, new.br_blockcount
);
5330 stolen
= xfs_bmap_split_indlen(da_old
, &temp
, &temp2
,
5331 del
->br_blockcount
);
5332 da_new
= temp
+ temp2
- stolen
;
5333 del
->br_blockcount
-= stolen
;
5336 * Set the reservation for each extent. Warn if either
5337 * is zero as this can lead to delalloc problems.
5339 WARN_ON_ONCE(!temp
|| !temp2
);
5340 xfs_bmbt_set_startblock(ep
, nullstartblock((int)temp
));
5341 new.br_startblock
= nullstartblock((int)temp2
);
5343 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
5344 xfs_iext_insert(ip
, *idx
+ 1, 1, &new, state
);
5349 /* remove reverse mapping */
5351 error
= xfs_rmap_unmap_extent(mp
, dfops
, ip
, whichfork
, del
);
5357 * If we need to, add to list of extents to delete.
5359 if (do_fx
&& !(bflags
& XFS_BMAPI_REMAP
)) {
5360 if (xfs_is_reflink_inode(ip
) && whichfork
== XFS_DATA_FORK
) {
5361 error
= xfs_refcount_decrease_extent(mp
, dfops
, del
);
5365 xfs_bmap_add_free(mp
, dfops
, del
->br_startblock
,
5366 del
->br_blockcount
, NULL
);
5370 * Adjust inode # blocks in the file.
5373 ip
->i_d
.di_nblocks
-= nblks
;
5375 * Adjust quota data.
5377 if (qfield
&& !(bflags
& XFS_BMAPI_REMAP
))
5378 xfs_trans_mod_dquot_byino(tp
, ip
, qfield
, (long)-nblks
);
5381 * Account for change in delayed indirect blocks.
5382 * Nothing to do for disk quota accounting here.
5384 ASSERT(da_old
>= da_new
);
5385 if (da_old
> da_new
)
5386 xfs_mod_fdblocks(mp
, (int64_t)(da_old
- da_new
), false);
5393 * Unmap (remove) blocks from a file.
5394 * If nexts is nonzero then the number of extents to remove is limited to
5395 * that value. If not all extents in the block range can be removed then
5400 xfs_trans_t
*tp
, /* transaction pointer */
5401 struct xfs_inode
*ip
, /* incore inode */
5402 xfs_fileoff_t bno
, /* starting offset to unmap */
5403 xfs_filblks_t
*rlen
, /* i/o: amount remaining */
5404 int flags
, /* misc flags */
5405 xfs_extnum_t nexts
, /* number of extents max */
5406 xfs_fsblock_t
*firstblock
, /* first allocated block
5407 controls a.g. for allocs */
5408 struct xfs_defer_ops
*dfops
) /* i/o: deferred updates */
5410 xfs_btree_cur_t
*cur
; /* bmap btree cursor */
5411 xfs_bmbt_irec_t del
; /* extent being deleted */
5412 int error
; /* error return value */
5413 xfs_extnum_t extno
; /* extent number in list */
5414 xfs_bmbt_irec_t got
; /* current extent record */
5415 xfs_ifork_t
*ifp
; /* inode fork pointer */
5416 int isrt
; /* freeing in rt area */
5417 xfs_extnum_t lastx
; /* last extent index used */
5418 int logflags
; /* transaction logging flags */
5419 xfs_extlen_t mod
; /* rt extent offset */
5420 xfs_mount_t
*mp
; /* mount structure */
5421 xfs_fileoff_t start
; /* first file offset deleted */
5422 int tmp_logflags
; /* partial logging flags */
5423 int wasdel
; /* was a delayed alloc extent */
5424 int whichfork
; /* data or attribute fork */
5426 xfs_filblks_t len
= *rlen
; /* length to unmap in file */
5427 xfs_fileoff_t max_len
;
5428 xfs_agnumber_t prev_agno
= NULLAGNUMBER
, agno
;
5430 trace_xfs_bunmap(ip
, bno
, len
, flags
, _RET_IP_
);
5432 whichfork
= xfs_bmapi_whichfork(flags
);
5433 ASSERT(whichfork
!= XFS_COW_FORK
);
5434 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5436 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
5437 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)) {
5438 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW
,
5440 return -EFSCORRUPTED
;
5443 if (XFS_FORCED_SHUTDOWN(mp
))
5446 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
5451 * Guesstimate how many blocks we can unmap without running the risk of
5452 * blowing out the transaction with a mix of EFIs and reflink
5455 if (xfs_is_reflink_inode(ip
) && whichfork
== XFS_DATA_FORK
)
5456 max_len
= min(len
, xfs_refcount_max_unmap(tp
->t_log_res
));
5460 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
5461 (error
= xfs_iread_extents(tp
, ip
, whichfork
)))
5463 if (xfs_iext_count(ifp
) == 0) {
5467 XFS_STATS_INC(mp
, xs_blk_unmap
);
5468 isrt
= (whichfork
== XFS_DATA_FORK
) && XFS_IS_REALTIME_INODE(ip
);
5470 bno
= start
+ len
- 1;
5473 * Check to see if the given block number is past the end of the
5474 * file, back up to the last block if so...
5476 if (!xfs_iext_lookup_extent(ip
, ifp
, bno
, &lastx
, &got
)) {
5478 xfs_iext_get_extent(ifp
, --lastx
, &got
);
5479 bno
= got
.br_startoff
+ got
.br_blockcount
- 1;
5483 if (ifp
->if_flags
& XFS_IFBROOT
) {
5484 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
);
5485 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
5486 cur
->bc_private
.b
.firstblock
= *firstblock
;
5487 cur
->bc_private
.b
.dfops
= dfops
;
5488 cur
->bc_private
.b
.flags
= 0;
5494 * Synchronize by locking the bitmap inode.
5496 xfs_ilock(mp
->m_rbmip
, XFS_ILOCK_EXCL
|XFS_ILOCK_RTBITMAP
);
5497 xfs_trans_ijoin(tp
, mp
->m_rbmip
, XFS_ILOCK_EXCL
);
5498 xfs_ilock(mp
->m_rsumip
, XFS_ILOCK_EXCL
|XFS_ILOCK_RTSUM
);
5499 xfs_trans_ijoin(tp
, mp
->m_rsumip
, XFS_ILOCK_EXCL
);
5503 while (bno
!= (xfs_fileoff_t
)-1 && bno
>= start
&& lastx
>= 0 &&
5504 (nexts
== 0 || extno
< nexts
) && max_len
> 0) {
5506 * Is the found extent after a hole in which bno lives?
5507 * Just back up to the previous extent, if so.
5509 if (got
.br_startoff
> bno
) {
5512 xfs_iext_get_extent(ifp
, lastx
, &got
);
5515 * Is the last block of this extent before the range
5516 * we're supposed to delete? If so, we're done.
5518 bno
= XFS_FILEOFF_MIN(bno
,
5519 got
.br_startoff
+ got
.br_blockcount
- 1);
5523 * Then deal with the (possibly delayed) allocated space
5527 wasdel
= isnullstartblock(del
.br_startblock
);
5530 * Make sure we don't touch multiple AGF headers out of order
5531 * in a single transaction, as that could cause AB-BA deadlocks.
5534 agno
= XFS_FSB_TO_AGNO(mp
, del
.br_startblock
);
5535 if (prev_agno
!= NULLAGNUMBER
&& prev_agno
> agno
)
5539 if (got
.br_startoff
< start
) {
5540 del
.br_startoff
= start
;
5541 del
.br_blockcount
-= start
- got
.br_startoff
;
5543 del
.br_startblock
+= start
- got
.br_startoff
;
5545 if (del
.br_startoff
+ del
.br_blockcount
> bno
+ 1)
5546 del
.br_blockcount
= bno
+ 1 - del
.br_startoff
;
5548 /* How much can we safely unmap? */
5549 if (max_len
< del
.br_blockcount
) {
5550 del
.br_startoff
+= del
.br_blockcount
- max_len
;
5552 del
.br_startblock
+= del
.br_blockcount
- max_len
;
5553 del
.br_blockcount
= max_len
;
5556 sum
= del
.br_startblock
+ del
.br_blockcount
;
5558 (mod
= do_mod(sum
, mp
->m_sb
.sb_rextsize
))) {
5560 * Realtime extent not lined up at the end.
5561 * The extent could have been split into written
5562 * and unwritten pieces, or we could just be
5563 * unmapping part of it. But we can't really
5564 * get rid of part of a realtime extent.
5566 if (del
.br_state
== XFS_EXT_UNWRITTEN
||
5567 !xfs_sb_version_hasextflgbit(&mp
->m_sb
)) {
5569 * This piece is unwritten, or we're not
5570 * using unwritten extents. Skip over it.
5573 bno
-= mod
> del
.br_blockcount
?
5574 del
.br_blockcount
: mod
;
5575 if (bno
< got
.br_startoff
) {
5577 xfs_iext_get_extent(ifp
, lastx
,
5583 * It's written, turn it unwritten.
5584 * This is better than zeroing it.
5586 ASSERT(del
.br_state
== XFS_EXT_NORM
);
5587 ASSERT(tp
->t_blk_res
> 0);
5589 * If this spans a realtime extent boundary,
5590 * chop it back to the start of the one we end at.
5592 if (del
.br_blockcount
> mod
) {
5593 del
.br_startoff
+= del
.br_blockcount
- mod
;
5594 del
.br_startblock
+= del
.br_blockcount
- mod
;
5595 del
.br_blockcount
= mod
;
5597 del
.br_state
= XFS_EXT_UNWRITTEN
;
5598 error
= xfs_bmap_add_extent_unwritten_real(tp
, ip
,
5599 whichfork
, &lastx
, &cur
, &del
,
5600 firstblock
, dfops
, &logflags
);
5605 if (isrt
&& (mod
= do_mod(del
.br_startblock
, mp
->m_sb
.sb_rextsize
))) {
5607 * Realtime extent is lined up at the end but not
5608 * at the front. We'll get rid of full extents if
5611 mod
= mp
->m_sb
.sb_rextsize
- mod
;
5612 if (del
.br_blockcount
> mod
) {
5613 del
.br_blockcount
-= mod
;
5614 del
.br_startoff
+= mod
;
5615 del
.br_startblock
+= mod
;
5616 } else if ((del
.br_startoff
== start
&&
5617 (del
.br_state
== XFS_EXT_UNWRITTEN
||
5618 tp
->t_blk_res
== 0)) ||
5619 !xfs_sb_version_hasextflgbit(&mp
->m_sb
)) {
5621 * Can't make it unwritten. There isn't
5622 * a full extent here so just skip it.
5624 ASSERT(bno
>= del
.br_blockcount
);
5625 bno
-= del
.br_blockcount
;
5626 if (got
.br_startoff
> bno
&& --lastx
>= 0)
5627 xfs_iext_get_extent(ifp
, lastx
, &got
);
5629 } else if (del
.br_state
== XFS_EXT_UNWRITTEN
) {
5630 struct xfs_bmbt_irec prev
;
5633 * This one is already unwritten.
5634 * It must have a written left neighbor.
5635 * Unwrite the killed part of that one and
5639 xfs_iext_get_extent(ifp
, lastx
- 1, &prev
);
5640 ASSERT(prev
.br_state
== XFS_EXT_NORM
);
5641 ASSERT(!isnullstartblock(prev
.br_startblock
));
5642 ASSERT(del
.br_startblock
==
5643 prev
.br_startblock
+ prev
.br_blockcount
);
5644 if (prev
.br_startoff
< start
) {
5645 mod
= start
- prev
.br_startoff
;
5646 prev
.br_blockcount
-= mod
;
5647 prev
.br_startblock
+= mod
;
5648 prev
.br_startoff
= start
;
5650 prev
.br_state
= XFS_EXT_UNWRITTEN
;
5652 error
= xfs_bmap_add_extent_unwritten_real(tp
,
5653 ip
, whichfork
, &lastx
, &cur
,
5654 &prev
, firstblock
, dfops
,
5660 ASSERT(del
.br_state
== XFS_EXT_NORM
);
5661 del
.br_state
= XFS_EXT_UNWRITTEN
;
5662 error
= xfs_bmap_add_extent_unwritten_real(tp
,
5663 ip
, whichfork
, &lastx
, &cur
,
5664 &del
, firstblock
, dfops
,
5673 * If it's the case where the directory code is running
5674 * with no block reservation, and the deleted block is in
5675 * the middle of its extent, and the resulting insert
5676 * of an extent would cause transformation to btree format,
5677 * then reject it. The calling code will then swap
5678 * blocks around instead.
5679 * We have to do this now, rather than waiting for the
5680 * conversion to btree format, since the transaction
5683 if (!wasdel
&& tp
->t_blk_res
== 0 &&
5684 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
&&
5685 XFS_IFORK_NEXTENTS(ip
, whichfork
) >= /* Note the >= */
5686 XFS_IFORK_MAXEXT(ip
, whichfork
) &&
5687 del
.br_startoff
> got
.br_startoff
&&
5688 del
.br_startoff
+ del
.br_blockcount
<
5689 got
.br_startoff
+ got
.br_blockcount
) {
5695 * Unreserve quota and update realtime free space, if
5696 * appropriate. If delayed allocation, update the inode delalloc
5697 * counter now and wait to update the sb counters as
5698 * xfs_bmap_del_extent() might need to borrow some blocks.
5701 ASSERT(startblockval(del
.br_startblock
) > 0);
5703 xfs_filblks_t rtexts
;
5705 rtexts
= XFS_FSB_TO_B(mp
, del
.br_blockcount
);
5706 do_div(rtexts
, mp
->m_sb
.sb_rextsize
);
5707 xfs_mod_frextents(mp
, (int64_t)rtexts
);
5708 (void)xfs_trans_reserve_quota_nblks(NULL
,
5709 ip
, -((long)del
.br_blockcount
), 0,
5710 XFS_QMOPT_RES_RTBLKS
);
5712 (void)xfs_trans_reserve_quota_nblks(NULL
,
5713 ip
, -((long)del
.br_blockcount
), 0,
5714 XFS_QMOPT_RES_REGBLKS
);
5716 ip
->i_delayed_blks
-= del
.br_blockcount
;
5719 error
= xfs_bmap_del_extent(ip
, tp
, &lastx
, dfops
, cur
, &del
,
5720 &tmp_logflags
, whichfork
, flags
);
5721 logflags
|= tmp_logflags
;
5725 if (!isrt
&& wasdel
)
5726 xfs_mod_fdblocks(mp
, (int64_t)del
.br_blockcount
, false);
5728 max_len
-= del
.br_blockcount
;
5729 bno
= del
.br_startoff
- 1;
5732 * If not done go on to the next (previous) record.
5734 if (bno
!= (xfs_fileoff_t
)-1 && bno
>= start
) {
5736 xfs_iext_get_extent(ifp
, lastx
, &got
);
5737 if (got
.br_startoff
> bno
&& --lastx
>= 0)
5738 xfs_iext_get_extent(ifp
, lastx
, &got
);
5743 if (bno
== (xfs_fileoff_t
)-1 || bno
< start
|| lastx
< 0)
5746 *rlen
= bno
- start
+ 1;
5749 * Convert to a btree if necessary.
5751 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
5752 ASSERT(cur
== NULL
);
5753 error
= xfs_bmap_extents_to_btree(tp
, ip
, firstblock
, dfops
,
5754 &cur
, 0, &tmp_logflags
, whichfork
);
5755 logflags
|= tmp_logflags
;
5760 * transform from btree to extents, give it cur
5762 else if (xfs_bmap_wants_extents(ip
, whichfork
)) {
5763 ASSERT(cur
!= NULL
);
5764 error
= xfs_bmap_btree_to_extents(tp
, ip
, cur
, &tmp_logflags
,
5766 logflags
|= tmp_logflags
;
5771 * transform from extents to local?
5776 * Log everything. Do this after conversion, there's no point in
5777 * logging the extent records if we've converted to btree format.
5779 if ((logflags
& xfs_ilog_fext(whichfork
)) &&
5780 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
5781 logflags
&= ~xfs_ilog_fext(whichfork
);
5782 else if ((logflags
& xfs_ilog_fbroot(whichfork
)) &&
5783 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)
5784 logflags
&= ~xfs_ilog_fbroot(whichfork
);
5786 * Log inode even in the error case, if the transaction
5787 * is dirty we'll need to shut down the filesystem.
5790 xfs_trans_log_inode(tp
, ip
, logflags
);
5793 *firstblock
= cur
->bc_private
.b
.firstblock
;
5794 cur
->bc_private
.b
.allocated
= 0;
5796 xfs_btree_del_cursor(cur
,
5797 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
5802 /* Unmap a range of a file. */
5806 struct xfs_inode
*ip
,
5811 xfs_fsblock_t
*firstblock
,
5812 struct xfs_defer_ops
*dfops
,
5817 error
= __xfs_bunmapi(tp
, ip
, bno
, &len
, flags
, nexts
, firstblock
,
5824 * Determine whether an extent shift can be accomplished by a merge with the
5825 * extent that precedes the target hole of the shift.
5829 struct xfs_bmbt_irec
*left
, /* preceding extent */
5830 struct xfs_bmbt_irec
*got
, /* current extent to shift */
5831 xfs_fileoff_t shift
) /* shift fsb */
5833 xfs_fileoff_t startoff
;
5835 startoff
= got
->br_startoff
- shift
;
5838 * The extent, once shifted, must be adjacent in-file and on-disk with
5839 * the preceding extent.
5841 if ((left
->br_startoff
+ left
->br_blockcount
!= startoff
) ||
5842 (left
->br_startblock
+ left
->br_blockcount
!= got
->br_startblock
) ||
5843 (left
->br_state
!= got
->br_state
) ||
5844 (left
->br_blockcount
+ got
->br_blockcount
> MAXEXTLEN
))
5851 * A bmap extent shift adjusts the file offset of an extent to fill a preceding
5852 * hole in the file. If an extent shift would result in the extent being fully
5853 * adjacent to the extent that currently precedes the hole, we can merge with
5854 * the preceding extent rather than do the shift.
5856 * This function assumes the caller has verified a shift-by-merge is possible
5857 * with the provided extents via xfs_bmse_can_merge().
5861 struct xfs_inode
*ip
,
5863 xfs_fileoff_t shift
, /* shift fsb */
5864 int current_ext
, /* idx of gotp */
5865 struct xfs_bmbt_irec
*got
, /* extent to shift */
5866 struct xfs_bmbt_irec
*left
, /* preceding extent */
5867 struct xfs_btree_cur
*cur
,
5868 int *logflags
, /* output */
5869 struct xfs_defer_ops
*dfops
)
5871 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5872 struct xfs_bmbt_irec
new;
5873 xfs_filblks_t blockcount
;
5875 struct xfs_mount
*mp
= ip
->i_mount
;
5877 blockcount
= left
->br_blockcount
+ got
->br_blockcount
;
5879 ASSERT(xfs_isilocked(ip
, XFS_IOLOCK_EXCL
));
5880 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
5881 ASSERT(xfs_bmse_can_merge(left
, got
, shift
));
5884 new.br_blockcount
= blockcount
;
5887 * Update the on-disk extent count, the btree if necessary and log the
5890 XFS_IFORK_NEXT_SET(ip
, whichfork
,
5891 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
5892 *logflags
|= XFS_ILOG_CORE
;
5894 *logflags
|= XFS_ILOG_DEXT
;
5898 /* lookup and remove the extent to merge */
5899 error
= xfs_bmbt_lookup_eq(cur
, got
->br_startoff
, got
->br_startblock
,
5900 got
->br_blockcount
, &i
);
5903 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
5905 error
= xfs_btree_delete(cur
, &i
);
5908 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
5910 /* lookup and update size of the previous extent */
5911 error
= xfs_bmbt_lookup_eq(cur
, left
->br_startoff
, left
->br_startblock
,
5912 left
->br_blockcount
, &i
);
5915 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
5917 error
= xfs_bmbt_update(cur
, new.br_startoff
, new.br_startblock
,
5918 new.br_blockcount
, new.br_state
);
5923 xfs_iext_update_extent(ifp
, current_ext
- 1, &new);
5924 xfs_iext_remove(ip
, current_ext
, 1, 0);
5926 /* update reverse mapping. rmap functions merge the rmaps for us */
5927 error
= xfs_rmap_unmap_extent(mp
, dfops
, ip
, whichfork
, got
);
5930 memcpy(&new, got
, sizeof(new));
5931 new.br_startoff
= left
->br_startoff
+ left
->br_blockcount
;
5932 return xfs_rmap_map_extent(mp
, dfops
, ip
, whichfork
, &new);
5936 * Shift a single extent.
5940 struct xfs_inode
*ip
,
5942 xfs_fileoff_t offset_shift_fsb
,
5944 struct xfs_bmbt_irec
*got
,
5945 struct xfs_btree_cur
*cur
,
5947 enum shift_direction direction
,
5948 struct xfs_defer_ops
*dfops
)
5950 struct xfs_ifork
*ifp
;
5951 struct xfs_mount
*mp
;
5952 xfs_fileoff_t startoff
;
5953 struct xfs_bmbt_irec adj_irec
, new;
5959 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5960 total_extents
= xfs_iext_count(ifp
);
5962 /* delalloc extents should be prevented by caller */
5963 XFS_WANT_CORRUPTED_RETURN(mp
, !isnullstartblock(got
->br_startblock
));
5965 if (direction
== SHIFT_LEFT
) {
5966 startoff
= got
->br_startoff
- offset_shift_fsb
;
5969 * Check for merge if we've got an extent to the left,
5970 * otherwise make sure there's enough room at the start
5971 * of the file for the shift.
5973 if (!*current_ext
) {
5974 if (got
->br_startoff
< offset_shift_fsb
)
5976 goto update_current_ext
;
5980 * grab the left extent and check for a large enough hole.
5982 xfs_iext_get_extent(ifp
, *current_ext
- 1, &adj_irec
);
5983 if (startoff
< adj_irec
.br_startoff
+ adj_irec
.br_blockcount
)
5986 /* check whether to merge the extent or shift it down */
5987 if (xfs_bmse_can_merge(&adj_irec
, got
, offset_shift_fsb
)) {
5988 return xfs_bmse_merge(ip
, whichfork
, offset_shift_fsb
,
5989 *current_ext
, got
, &adj_irec
,
5990 cur
, logflags
, dfops
);
5993 startoff
= got
->br_startoff
+ offset_shift_fsb
;
5994 /* nothing to move if this is the last extent */
5995 if (*current_ext
>= (total_extents
- 1))
5996 goto update_current_ext
;
5999 * If this is not the last extent in the file, make sure there
6000 * is enough room between current extent and next extent for
6001 * accommodating the shift.
6003 xfs_iext_get_extent(ifp
, *current_ext
+ 1, &adj_irec
);
6004 if (startoff
+ got
->br_blockcount
> adj_irec
.br_startoff
)
6008 * Unlike a left shift (which involves a hole punch),
6009 * a right shift does not modify extent neighbors
6010 * in any way. We should never find mergeable extents
6011 * in this scenario. Check anyways and warn if we
6012 * encounter two extents that could be one.
6014 if (xfs_bmse_can_merge(got
, &adj_irec
, offset_shift_fsb
))
6019 * Increment the extent index for the next iteration, update the start
6020 * offset of the in-core extent and update the btree if applicable.
6023 *logflags
|= XFS_ILOG_CORE
;
6026 new.br_startoff
= startoff
;
6029 error
= xfs_bmbt_lookup_eq(cur
, got
->br_startoff
,
6030 got
->br_startblock
, got
->br_blockcount
, &i
);
6033 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
6035 error
= xfs_bmbt_update(cur
, new.br_startoff
,
6036 new.br_startblock
, new.br_blockcount
,
6041 *logflags
|= XFS_ILOG_DEXT
;
6044 xfs_iext_update_extent(ifp
, *current_ext
, &new);
6046 if (direction
== SHIFT_LEFT
)
6051 /* update reverse mapping */
6052 error
= xfs_rmap_unmap_extent(mp
, dfops
, ip
, whichfork
, got
);
6055 return xfs_rmap_map_extent(mp
, dfops
, ip
, whichfork
, &new);
6059 * Shift extent records to the left/right to cover/create a hole.
6061 * The maximum number of extents to be shifted in a single operation is
6062 * @num_exts. @stop_fsb specifies the file offset at which to stop shift and the
6063 * file offset where we've left off is returned in @next_fsb. @offset_shift_fsb
6064 * is the length by which each extent is shifted. If there is no hole to shift
6065 * the extents into, this will be considered invalid operation and we abort
6069 xfs_bmap_shift_extents(
6070 struct xfs_trans
*tp
,
6071 struct xfs_inode
*ip
,
6072 xfs_fileoff_t
*next_fsb
,
6073 xfs_fileoff_t offset_shift_fsb
,
6075 xfs_fileoff_t stop_fsb
,
6076 xfs_fsblock_t
*firstblock
,
6077 struct xfs_defer_ops
*dfops
,
6078 enum shift_direction direction
,
6081 struct xfs_btree_cur
*cur
= NULL
;
6082 struct xfs_bmbt_irec got
;
6083 struct xfs_mount
*mp
= ip
->i_mount
;
6084 struct xfs_ifork
*ifp
;
6085 xfs_extnum_t nexts
= 0;
6086 xfs_extnum_t current_ext
;
6087 xfs_extnum_t total_extents
;
6088 xfs_extnum_t stop_extent
;
6090 int whichfork
= XFS_DATA_FORK
;
6093 if (unlikely(XFS_TEST_ERROR(
6094 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
6095 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
6096 mp
, XFS_ERRTAG_BMAPIFORMAT
))) {
6097 XFS_ERROR_REPORT("xfs_bmap_shift_extents",
6098 XFS_ERRLEVEL_LOW
, mp
);
6099 return -EFSCORRUPTED
;
6102 if (XFS_FORCED_SHUTDOWN(mp
))
6105 ASSERT(xfs_isilocked(ip
, XFS_IOLOCK_EXCL
));
6106 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
6107 ASSERT(direction
== SHIFT_LEFT
|| direction
== SHIFT_RIGHT
);
6109 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
6110 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
6111 /* Read in all the extents */
6112 error
= xfs_iread_extents(tp
, ip
, whichfork
);
6117 if (ifp
->if_flags
& XFS_IFBROOT
) {
6118 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
6119 cur
->bc_private
.b
.firstblock
= *firstblock
;
6120 cur
->bc_private
.b
.dfops
= dfops
;
6121 cur
->bc_private
.b
.flags
= 0;
6125 * There may be delalloc extents in the data fork before the range we
6126 * are collapsing out, so we cannot use the count of real extents here.
6127 * Instead we have to calculate it from the incore fork.
6129 total_extents
= xfs_iext_count(ifp
);
6130 if (total_extents
== 0) {
6136 * In case of first right shift, we need to initialize next_fsb
6138 if (*next_fsb
== NULLFSBLOCK
) {
6139 ASSERT(direction
== SHIFT_RIGHT
);
6141 current_ext
= total_extents
- 1;
6142 xfs_iext_get_extent(ifp
, current_ext
, &got
);
6143 if (stop_fsb
> got
.br_startoff
) {
6147 *next_fsb
= got
.br_startoff
;
6150 * Look up the extent index for the fsb where we start shifting. We can
6151 * henceforth iterate with current_ext as extent list changes are locked
6154 * If next_fsb lies in a hole beyond which there are no extents we are
6157 if (!xfs_iext_lookup_extent(ip
, ifp
, *next_fsb
, ¤t_ext
,
6164 /* Lookup the extent index at which we have to stop */
6165 if (direction
== SHIFT_RIGHT
) {
6166 struct xfs_bmbt_irec s
;
6168 xfs_iext_lookup_extent(ip
, ifp
, stop_fsb
, &stop_extent
, &s
);
6169 /* Make stop_extent exclusive of shift range */
6171 if (current_ext
<= stop_extent
) {
6176 stop_extent
= total_extents
;
6177 if (current_ext
>= stop_extent
) {
6183 while (nexts
++ < num_exts
) {
6184 error
= xfs_bmse_shift_one(ip
, whichfork
, offset_shift_fsb
,
6185 ¤t_ext
, &got
, cur
, &logflags
,
6190 * If there was an extent merge during the shift, the extent
6191 * count can change. Update the total and grade the next record.
6193 if (direction
== SHIFT_LEFT
) {
6194 total_extents
= xfs_iext_count(ifp
);
6195 stop_extent
= total_extents
;
6198 if (current_ext
== stop_extent
) {
6200 *next_fsb
= NULLFSBLOCK
;
6203 xfs_iext_get_extent(ifp
, current_ext
, &got
);
6207 *next_fsb
= got
.br_startoff
;
6211 xfs_btree_del_cursor(cur
,
6212 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
6215 xfs_trans_log_inode(tp
, ip
, logflags
);
6221 * Splits an extent into two extents at split_fsb block such that it is
6222 * the first block of the current_ext. @current_ext is a target extent
6223 * to be split. @split_fsb is a block where the extents is split.
6224 * If split_fsb lies in a hole or the first block of extents, just return 0.
6227 xfs_bmap_split_extent_at(
6228 struct xfs_trans
*tp
,
6229 struct xfs_inode
*ip
,
6230 xfs_fileoff_t split_fsb
,
6231 xfs_fsblock_t
*firstfsb
,
6232 struct xfs_defer_ops
*dfops
)
6234 int whichfork
= XFS_DATA_FORK
;
6235 struct xfs_btree_cur
*cur
= NULL
;
6236 struct xfs_bmbt_irec got
;
6237 struct xfs_bmbt_irec
new; /* split extent */
6238 struct xfs_mount
*mp
= ip
->i_mount
;
6239 struct xfs_ifork
*ifp
;
6240 xfs_fsblock_t gotblkcnt
; /* new block count for got */
6241 xfs_extnum_t current_ext
;
6246 if (unlikely(XFS_TEST_ERROR(
6247 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
6248 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
6249 mp
, XFS_ERRTAG_BMAPIFORMAT
))) {
6250 XFS_ERROR_REPORT("xfs_bmap_split_extent_at",
6251 XFS_ERRLEVEL_LOW
, mp
);
6252 return -EFSCORRUPTED
;
6255 if (XFS_FORCED_SHUTDOWN(mp
))
6258 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
6259 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
6260 /* Read in all the extents */
6261 error
= xfs_iread_extents(tp
, ip
, whichfork
);
6267 * If there are not extents, or split_fsb lies in a hole we are done.
6269 if (!xfs_iext_lookup_extent(ip
, ifp
, split_fsb
, ¤t_ext
, &got
) ||
6270 got
.br_startoff
>= split_fsb
)
6273 gotblkcnt
= split_fsb
- got
.br_startoff
;
6274 new.br_startoff
= split_fsb
;
6275 new.br_startblock
= got
.br_startblock
+ gotblkcnt
;
6276 new.br_blockcount
= got
.br_blockcount
- gotblkcnt
;
6277 new.br_state
= got
.br_state
;
6279 if (ifp
->if_flags
& XFS_IFBROOT
) {
6280 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
6281 cur
->bc_private
.b
.firstblock
= *firstfsb
;
6282 cur
->bc_private
.b
.dfops
= dfops
;
6283 cur
->bc_private
.b
.flags
= 0;
6284 error
= xfs_bmbt_lookup_eq(cur
, got
.br_startoff
,
6290 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, del_cursor
);
6293 got
.br_blockcount
= gotblkcnt
;
6294 xfs_iext_update_extent(ifp
, current_ext
, &got
);
6296 logflags
= XFS_ILOG_CORE
;
6298 error
= xfs_bmbt_update(cur
, got
.br_startoff
,
6305 logflags
|= XFS_ILOG_DEXT
;
6307 /* Add new extent */
6309 xfs_iext_insert(ip
, current_ext
, 1, &new, 0);
6310 XFS_IFORK_NEXT_SET(ip
, whichfork
,
6311 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
6314 error
= xfs_bmbt_lookup_eq(cur
, new.br_startoff
,
6315 new.br_startblock
, new.br_blockcount
,
6319 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, del_cursor
);
6320 cur
->bc_rec
.b
.br_state
= new.br_state
;
6322 error
= xfs_btree_insert(cur
, &i
);
6325 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, del_cursor
);
6329 * Convert to a btree if necessary.
6331 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
6332 int tmp_logflags
; /* partial log flag return val */
6334 ASSERT(cur
== NULL
);
6335 error
= xfs_bmap_extents_to_btree(tp
, ip
, firstfsb
, dfops
,
6336 &cur
, 0, &tmp_logflags
, whichfork
);
6337 logflags
|= tmp_logflags
;
6342 cur
->bc_private
.b
.allocated
= 0;
6343 xfs_btree_del_cursor(cur
,
6344 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
6348 xfs_trans_log_inode(tp
, ip
, logflags
);
6353 xfs_bmap_split_extent(
6354 struct xfs_inode
*ip
,
6355 xfs_fileoff_t split_fsb
)
6357 struct xfs_mount
*mp
= ip
->i_mount
;
6358 struct xfs_trans
*tp
;
6359 struct xfs_defer_ops dfops
;
6360 xfs_fsblock_t firstfsb
;
6363 error
= xfs_trans_alloc(mp
, &M_RES(mp
)->tr_write
,
6364 XFS_DIOSTRAT_SPACE_RES(mp
, 0), 0, 0, &tp
);
6368 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
6369 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
);
6371 xfs_defer_init(&dfops
, &firstfsb
);
6373 error
= xfs_bmap_split_extent_at(tp
, ip
, split_fsb
,
6378 error
= xfs_defer_finish(&tp
, &dfops
);
6382 return xfs_trans_commit(tp
);
6385 xfs_defer_cancel(&dfops
);
6386 xfs_trans_cancel(tp
);
6390 /* Deferred mapping is only for real extents in the data fork. */
6392 xfs_bmap_is_update_needed(
6393 struct xfs_bmbt_irec
*bmap
)
6395 return bmap
->br_startblock
!= HOLESTARTBLOCK
&&
6396 bmap
->br_startblock
!= DELAYSTARTBLOCK
;
6399 /* Record a bmap intent. */
6402 struct xfs_mount
*mp
,
6403 struct xfs_defer_ops
*dfops
,
6404 enum xfs_bmap_intent_type type
,
6405 struct xfs_inode
*ip
,
6407 struct xfs_bmbt_irec
*bmap
)
6410 struct xfs_bmap_intent
*bi
;
6412 trace_xfs_bmap_defer(mp
,
6413 XFS_FSB_TO_AGNO(mp
, bmap
->br_startblock
),
6415 XFS_FSB_TO_AGBNO(mp
, bmap
->br_startblock
),
6416 ip
->i_ino
, whichfork
,
6418 bmap
->br_blockcount
,
6421 bi
= kmem_alloc(sizeof(struct xfs_bmap_intent
), KM_SLEEP
| KM_NOFS
);
6422 INIT_LIST_HEAD(&bi
->bi_list
);
6425 bi
->bi_whichfork
= whichfork
;
6426 bi
->bi_bmap
= *bmap
;
6428 error
= xfs_defer_ijoin(dfops
, bi
->bi_owner
);
6434 xfs_defer_add(dfops
, XFS_DEFER_OPS_TYPE_BMAP
, &bi
->bi_list
);
6438 /* Map an extent into a file. */
6440 xfs_bmap_map_extent(
6441 struct xfs_mount
*mp
,
6442 struct xfs_defer_ops
*dfops
,
6443 struct xfs_inode
*ip
,
6444 struct xfs_bmbt_irec
*PREV
)
6446 if (!xfs_bmap_is_update_needed(PREV
))
6449 return __xfs_bmap_add(mp
, dfops
, XFS_BMAP_MAP
, ip
,
6450 XFS_DATA_FORK
, PREV
);
6453 /* Unmap an extent out of a file. */
6455 xfs_bmap_unmap_extent(
6456 struct xfs_mount
*mp
,
6457 struct xfs_defer_ops
*dfops
,
6458 struct xfs_inode
*ip
,
6459 struct xfs_bmbt_irec
*PREV
)
6461 if (!xfs_bmap_is_update_needed(PREV
))
6464 return __xfs_bmap_add(mp
, dfops
, XFS_BMAP_UNMAP
, ip
,
6465 XFS_DATA_FORK
, PREV
);
6469 * Process one of the deferred bmap operations. We pass back the
6470 * btree cursor to maintain our lock on the bmapbt between calls.
6473 xfs_bmap_finish_one(
6474 struct xfs_trans
*tp
,
6475 struct xfs_defer_ops
*dfops
,
6476 struct xfs_inode
*ip
,
6477 enum xfs_bmap_intent_type type
,
6479 xfs_fileoff_t startoff
,
6480 xfs_fsblock_t startblock
,
6481 xfs_filblks_t
*blockcount
,
6484 xfs_fsblock_t firstfsb
;
6488 * firstfsb is tied to the transaction lifetime and is used to
6489 * ensure correct AG locking order and schedule work item
6490 * continuations. XFS_BUI_MAX_FAST_EXTENTS (== 1) restricts us
6491 * to only making one bmap call per transaction, so it should
6492 * be safe to have it as a local variable here.
6494 firstfsb
= NULLFSBLOCK
;
6496 trace_xfs_bmap_deferred(tp
->t_mountp
,
6497 XFS_FSB_TO_AGNO(tp
->t_mountp
, startblock
), type
,
6498 XFS_FSB_TO_AGBNO(tp
->t_mountp
, startblock
),
6499 ip
->i_ino
, whichfork
, startoff
, *blockcount
, state
);
6501 if (WARN_ON_ONCE(whichfork
!= XFS_DATA_FORK
))
6502 return -EFSCORRUPTED
;
6504 if (XFS_TEST_ERROR(false, tp
->t_mountp
,
6505 XFS_ERRTAG_BMAP_FINISH_ONE
))
6510 error
= xfs_bmapi_remap(tp
, ip
, startoff
, *blockcount
,
6514 case XFS_BMAP_UNMAP
:
6515 error
= __xfs_bunmapi(tp
, ip
, startoff
, blockcount
,
6516 XFS_BMAPI_REMAP
, 1, &firstfsb
, dfops
);
6520 error
= -EFSCORRUPTED
;