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
,
106 struct xfs_bmbt_irec
*irec
,
107 int *stat
) /* success/failure */
109 cur
->bc_rec
.b
= *irec
;
110 return xfs_btree_lookup(cur
, XFS_LOOKUP_EQ
, stat
);
113 STATIC
int /* error */
114 xfs_bmbt_lookup_first(
115 struct xfs_btree_cur
*cur
,
116 int *stat
) /* success/failure */
118 cur
->bc_rec
.b
.br_startoff
= 0;
119 cur
->bc_rec
.b
.br_startblock
= 0;
120 cur
->bc_rec
.b
.br_blockcount
= 0;
121 return xfs_btree_lookup(cur
, XFS_LOOKUP_GE
, stat
);
125 * Check if the inode needs to be converted to btree format.
127 static inline bool xfs_bmap_needs_btree(struct xfs_inode
*ip
, int whichfork
)
129 return whichfork
!= XFS_COW_FORK
&&
130 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
&&
131 XFS_IFORK_NEXTENTS(ip
, whichfork
) >
132 XFS_IFORK_MAXEXT(ip
, whichfork
);
136 * Check if the inode should be converted to extent format.
138 static inline bool xfs_bmap_wants_extents(struct xfs_inode
*ip
, int whichfork
)
140 return whichfork
!= XFS_COW_FORK
&&
141 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
&&
142 XFS_IFORK_NEXTENTS(ip
, whichfork
) <=
143 XFS_IFORK_MAXEXT(ip
, whichfork
);
147 * Update the record referred to by cur to the value given by irec
148 * This either works (return 0) or gets an EFSCORRUPTED error.
152 struct xfs_btree_cur
*cur
,
153 struct xfs_bmbt_irec
*irec
)
155 union xfs_btree_rec rec
;
157 xfs_bmbt_disk_set_all(&rec
.bmbt
, irec
);
158 return xfs_btree_update(cur
, &rec
);
162 * Compute the worst-case number of indirect blocks that will be used
163 * for ip's delayed extent of length "len".
166 xfs_bmap_worst_indlen(
167 xfs_inode_t
*ip
, /* incore inode pointer */
168 xfs_filblks_t len
) /* delayed extent length */
170 int level
; /* btree level number */
171 int maxrecs
; /* maximum record count at this level */
172 xfs_mount_t
*mp
; /* mount structure */
173 xfs_filblks_t rval
; /* return value */
176 maxrecs
= mp
->m_bmap_dmxr
[0];
177 for (level
= 0, rval
= 0;
178 level
< XFS_BM_MAXLEVELS(mp
, XFS_DATA_FORK
);
181 do_div(len
, maxrecs
);
184 return rval
+ XFS_BM_MAXLEVELS(mp
, XFS_DATA_FORK
) -
187 maxrecs
= mp
->m_bmap_dmxr
[1];
193 * Calculate the default attribute fork offset for newly created inodes.
196 xfs_default_attroffset(
197 struct xfs_inode
*ip
)
199 struct xfs_mount
*mp
= ip
->i_mount
;
202 if (mp
->m_sb
.sb_inodesize
== 256) {
203 offset
= XFS_LITINO(mp
, ip
->i_d
.di_version
) -
204 XFS_BMDR_SPACE_CALC(MINABTPTRS
);
206 offset
= XFS_BMDR_SPACE_CALC(6 * MINABTPTRS
);
209 ASSERT(offset
< XFS_LITINO(mp
, ip
->i_d
.di_version
));
214 * Helper routine to reset inode di_forkoff field when switching
215 * attribute fork from local to extent format - we reset it where
216 * possible to make space available for inline data fork extents.
219 xfs_bmap_forkoff_reset(
223 if (whichfork
== XFS_ATTR_FORK
&&
224 ip
->i_d
.di_format
!= XFS_DINODE_FMT_DEV
&&
225 ip
->i_d
.di_format
!= XFS_DINODE_FMT_UUID
&&
226 ip
->i_d
.di_format
!= XFS_DINODE_FMT_BTREE
) {
227 uint dfl_forkoff
= xfs_default_attroffset(ip
) >> 3;
229 if (dfl_forkoff
> ip
->i_d
.di_forkoff
)
230 ip
->i_d
.di_forkoff
= dfl_forkoff
;
235 STATIC
struct xfs_buf
*
237 struct xfs_btree_cur
*cur
,
240 struct xfs_log_item_desc
*lidp
;
246 for (i
= 0; i
< XFS_BTREE_MAXLEVELS
; i
++) {
247 if (!cur
->bc_bufs
[i
])
249 if (XFS_BUF_ADDR(cur
->bc_bufs
[i
]) == bno
)
250 return cur
->bc_bufs
[i
];
253 /* Chase down all the log items to see if the bp is there */
254 list_for_each_entry(lidp
, &cur
->bc_tp
->t_items
, lid_trans
) {
255 struct xfs_buf_log_item
*bip
;
256 bip
= (struct xfs_buf_log_item
*)lidp
->lid_item
;
257 if (bip
->bli_item
.li_type
== XFS_LI_BUF
&&
258 XFS_BUF_ADDR(bip
->bli_buf
) == bno
)
267 struct xfs_btree_block
*block
,
273 __be64
*pp
, *thispa
; /* pointer to block address */
274 xfs_bmbt_key_t
*prevp
, *keyp
;
276 ASSERT(be16_to_cpu(block
->bb_level
) > 0);
279 for( i
= 1; i
<= xfs_btree_get_numrecs(block
); i
++) {
280 dmxr
= mp
->m_bmap_dmxr
[0];
281 keyp
= XFS_BMBT_KEY_ADDR(mp
, block
, i
);
284 ASSERT(be64_to_cpu(prevp
->br_startoff
) <
285 be64_to_cpu(keyp
->br_startoff
));
290 * Compare the block numbers to see if there are dups.
293 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, i
, sz
);
295 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, i
, dmxr
);
297 for (j
= i
+1; j
<= be16_to_cpu(block
->bb_numrecs
); j
++) {
299 thispa
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, j
, sz
);
301 thispa
= XFS_BMBT_PTR_ADDR(mp
, block
, j
, dmxr
);
302 if (*thispa
== *pp
) {
303 xfs_warn(mp
, "%s: thispa(%d) == pp(%d) %Ld",
305 (unsigned long long)be64_to_cpu(*thispa
));
306 panic("%s: ptrs are equal in node\n",
314 * Check that the extents for the inode ip are in the right order in all
315 * btree leaves. THis becomes prohibitively expensive for large extent count
316 * files, so don't bother with inodes that have more than 10,000 extents in
317 * them. The btree record ordering checks will still be done, so for such large
318 * bmapbt constructs that is going to catch most corruptions.
321 xfs_bmap_check_leaf_extents(
322 xfs_btree_cur_t
*cur
, /* btree cursor or null */
323 xfs_inode_t
*ip
, /* incore inode pointer */
324 int whichfork
) /* data or attr fork */
326 struct xfs_btree_block
*block
; /* current btree block */
327 xfs_fsblock_t bno
; /* block # of "block" */
328 xfs_buf_t
*bp
; /* buffer for "block" */
329 int error
; /* error return value */
330 xfs_extnum_t i
=0, j
; /* index into the extents list */
331 xfs_ifork_t
*ifp
; /* fork structure */
332 int level
; /* btree level, for checking */
333 xfs_mount_t
*mp
; /* file system mount structure */
334 __be64
*pp
; /* pointer to block address */
335 xfs_bmbt_rec_t
*ep
; /* pointer to current extent */
336 xfs_bmbt_rec_t last
= {0, 0}; /* last extent in prev block */
337 xfs_bmbt_rec_t
*nextp
; /* pointer to next extent */
340 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
) {
344 /* skip large extent count inodes */
345 if (ip
->i_d
.di_nextents
> 10000)
350 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
351 block
= ifp
->if_broot
;
353 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
355 level
= be16_to_cpu(block
->bb_level
);
357 xfs_check_block(block
, mp
, 1, ifp
->if_broot_bytes
);
358 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, 1, ifp
->if_broot_bytes
);
359 bno
= be64_to_cpu(*pp
);
361 ASSERT(bno
!= NULLFSBLOCK
);
362 ASSERT(XFS_FSB_TO_AGNO(mp
, bno
) < mp
->m_sb
.sb_agcount
);
363 ASSERT(XFS_FSB_TO_AGBNO(mp
, bno
) < mp
->m_sb
.sb_agblocks
);
366 * Go down the tree until leaf level is reached, following the first
367 * pointer (leftmost) at each level.
369 while (level
-- > 0) {
370 /* See if buf is in cur first */
372 bp
= xfs_bmap_get_bp(cur
, XFS_FSB_TO_DADDR(mp
, bno
));
375 error
= xfs_btree_read_bufl(mp
, NULL
, bno
, 0, &bp
,
381 block
= XFS_BUF_TO_BLOCK(bp
);
386 * Check this block for basic sanity (increasing keys and
387 * no duplicate blocks).
390 xfs_check_block(block
, mp
, 0, 0);
391 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, mp
->m_bmap_dmxr
[1]);
392 bno
= be64_to_cpu(*pp
);
393 XFS_WANT_CORRUPTED_GOTO(mp
,
394 XFS_FSB_SANITY_CHECK(mp
, bno
), error0
);
397 xfs_trans_brelse(NULL
, bp
);
402 * Here with bp and block set to the leftmost leaf node in the tree.
407 * Loop over all leaf nodes checking that all extents are in the right order.
410 xfs_fsblock_t nextbno
;
411 xfs_extnum_t num_recs
;
414 num_recs
= xfs_btree_get_numrecs(block
);
417 * Read-ahead the next leaf block, if any.
420 nextbno
= be64_to_cpu(block
->bb_u
.l
.bb_rightsib
);
423 * Check all the extents to make sure they are OK.
424 * If we had a previous block, the last entry should
425 * conform with the first entry in this one.
428 ep
= XFS_BMBT_REC_ADDR(mp
, block
, 1);
430 ASSERT(xfs_bmbt_disk_get_startoff(&last
) +
431 xfs_bmbt_disk_get_blockcount(&last
) <=
432 xfs_bmbt_disk_get_startoff(ep
));
434 for (j
= 1; j
< num_recs
; j
++) {
435 nextp
= XFS_BMBT_REC_ADDR(mp
, block
, j
+ 1);
436 ASSERT(xfs_bmbt_disk_get_startoff(ep
) +
437 xfs_bmbt_disk_get_blockcount(ep
) <=
438 xfs_bmbt_disk_get_startoff(nextp
));
446 xfs_trans_brelse(NULL
, bp
);
450 * If we've reached the end, stop.
452 if (bno
== NULLFSBLOCK
)
456 bp
= xfs_bmap_get_bp(cur
, XFS_FSB_TO_DADDR(mp
, bno
));
459 error
= xfs_btree_read_bufl(mp
, NULL
, bno
, 0, &bp
,
465 block
= XFS_BUF_TO_BLOCK(bp
);
471 xfs_warn(mp
, "%s: at error0", __func__
);
473 xfs_trans_brelse(NULL
, bp
);
475 xfs_warn(mp
, "%s: BAD after btree leaves for %d extents",
477 panic("%s: CORRUPTED BTREE OR SOMETHING", __func__
);
482 * Validate that the bmbt_irecs being returned from bmapi are valid
483 * given the caller's original parameters. Specifically check the
484 * ranges of the returned irecs to ensure that they only extend beyond
485 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
488 xfs_bmap_validate_ret(
492 xfs_bmbt_irec_t
*mval
,
496 int i
; /* index to map values */
498 ASSERT(ret_nmap
<= nmap
);
500 for (i
= 0; i
< ret_nmap
; i
++) {
501 ASSERT(mval
[i
].br_blockcount
> 0);
502 if (!(flags
& XFS_BMAPI_ENTIRE
)) {
503 ASSERT(mval
[i
].br_startoff
>= bno
);
504 ASSERT(mval
[i
].br_blockcount
<= len
);
505 ASSERT(mval
[i
].br_startoff
+ mval
[i
].br_blockcount
<=
508 ASSERT(mval
[i
].br_startoff
< bno
+ len
);
509 ASSERT(mval
[i
].br_startoff
+ mval
[i
].br_blockcount
>
513 mval
[i
- 1].br_startoff
+ mval
[i
- 1].br_blockcount
==
514 mval
[i
].br_startoff
);
515 ASSERT(mval
[i
].br_startblock
!= DELAYSTARTBLOCK
&&
516 mval
[i
].br_startblock
!= HOLESTARTBLOCK
);
517 ASSERT(mval
[i
].br_state
== XFS_EXT_NORM
||
518 mval
[i
].br_state
== XFS_EXT_UNWRITTEN
);
523 #define xfs_bmap_check_leaf_extents(cur, ip, whichfork) do { } while (0)
524 #define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap) do { } while (0)
528 * bmap free list manipulation functions
532 * Add the extent to the list of extents to be free at transaction end.
533 * The list is maintained sorted (by block number).
537 struct xfs_mount
*mp
,
538 struct xfs_defer_ops
*dfops
,
541 struct xfs_owner_info
*oinfo
)
543 struct xfs_extent_free_item
*new; /* new element */
548 ASSERT(bno
!= NULLFSBLOCK
);
550 ASSERT(len
<= MAXEXTLEN
);
551 ASSERT(!isnullstartblock(bno
));
552 agno
= XFS_FSB_TO_AGNO(mp
, bno
);
553 agbno
= XFS_FSB_TO_AGBNO(mp
, bno
);
554 ASSERT(agno
< mp
->m_sb
.sb_agcount
);
555 ASSERT(agbno
< mp
->m_sb
.sb_agblocks
);
556 ASSERT(len
< mp
->m_sb
.sb_agblocks
);
557 ASSERT(agbno
+ len
<= mp
->m_sb
.sb_agblocks
);
559 ASSERT(xfs_bmap_free_item_zone
!= NULL
);
561 new = kmem_zone_alloc(xfs_bmap_free_item_zone
, KM_SLEEP
);
562 new->xefi_startblock
= bno
;
563 new->xefi_blockcount
= (xfs_extlen_t
)len
;
565 new->xefi_oinfo
= *oinfo
;
567 xfs_rmap_skip_owner_update(&new->xefi_oinfo
);
568 trace_xfs_bmap_free_defer(mp
, XFS_FSB_TO_AGNO(mp
, bno
), 0,
569 XFS_FSB_TO_AGBNO(mp
, bno
), len
);
570 xfs_defer_add(dfops
, XFS_DEFER_OPS_TYPE_FREE
, &new->xefi_list
);
574 * Inode fork format manipulation functions
578 * Transform a btree format file with only one leaf node, where the
579 * extents list will fit in the inode, into an extents format file.
580 * Since the file extents are already in-core, all we have to do is
581 * give up the space for the btree root and pitch the leaf block.
583 STATIC
int /* error */
584 xfs_bmap_btree_to_extents(
585 xfs_trans_t
*tp
, /* transaction pointer */
586 xfs_inode_t
*ip
, /* incore inode pointer */
587 xfs_btree_cur_t
*cur
, /* btree cursor */
588 int *logflagsp
, /* inode logging flags */
589 int whichfork
) /* data or attr fork */
592 struct xfs_btree_block
*cblock
;/* child btree block */
593 xfs_fsblock_t cbno
; /* child block number */
594 xfs_buf_t
*cbp
; /* child block's buffer */
595 int error
; /* error return value */
596 xfs_ifork_t
*ifp
; /* inode fork data */
597 xfs_mount_t
*mp
; /* mount point structure */
598 __be64
*pp
; /* ptr to block address */
599 struct xfs_btree_block
*rblock
;/* root btree block */
600 struct xfs_owner_info oinfo
;
603 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
604 ASSERT(whichfork
!= XFS_COW_FORK
);
605 ASSERT(ifp
->if_flags
& XFS_IFEXTENTS
);
606 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
);
607 rblock
= ifp
->if_broot
;
608 ASSERT(be16_to_cpu(rblock
->bb_level
) == 1);
609 ASSERT(be16_to_cpu(rblock
->bb_numrecs
) == 1);
610 ASSERT(xfs_bmbt_maxrecs(mp
, ifp
->if_broot_bytes
, 0) == 1);
611 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, rblock
, 1, ifp
->if_broot_bytes
);
612 cbno
= be64_to_cpu(*pp
);
615 XFS_WANT_CORRUPTED_RETURN(cur
->bc_mp
,
616 xfs_btree_check_lptr(cur
, cbno
, 1));
618 error
= xfs_btree_read_bufl(mp
, tp
, cbno
, 0, &cbp
, XFS_BMAP_BTREE_REF
,
622 cblock
= XFS_BUF_TO_BLOCK(cbp
);
623 if ((error
= xfs_btree_check_block(cur
, cblock
, 0, cbp
)))
625 xfs_rmap_ino_bmbt_owner(&oinfo
, ip
->i_ino
, whichfork
);
626 xfs_bmap_add_free(mp
, cur
->bc_private
.b
.dfops
, cbno
, 1, &oinfo
);
627 ip
->i_d
.di_nblocks
--;
628 xfs_trans_mod_dquot_byino(tp
, ip
, XFS_TRANS_DQ_BCOUNT
, -1L);
629 xfs_trans_binval(tp
, cbp
);
630 if (cur
->bc_bufs
[0] == cbp
)
631 cur
->bc_bufs
[0] = NULL
;
632 xfs_iroot_realloc(ip
, -1, whichfork
);
633 ASSERT(ifp
->if_broot
== NULL
);
634 ASSERT((ifp
->if_flags
& XFS_IFBROOT
) == 0);
635 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_EXTENTS
);
636 *logflagsp
= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
641 * Convert an extents-format file into a btree-format file.
642 * The new file will have a root block (in the inode) and a single child block.
644 STATIC
int /* error */
645 xfs_bmap_extents_to_btree(
646 xfs_trans_t
*tp
, /* transaction pointer */
647 xfs_inode_t
*ip
, /* incore inode pointer */
648 xfs_fsblock_t
*firstblock
, /* first-block-allocated */
649 struct xfs_defer_ops
*dfops
, /* blocks freed in xaction */
650 xfs_btree_cur_t
**curp
, /* cursor returned to caller */
651 int wasdel
, /* converting a delayed alloc */
652 int *logflagsp
, /* inode logging flags */
653 int whichfork
) /* data or attr fork */
655 struct xfs_btree_block
*ablock
; /* allocated (child) bt block */
656 xfs_buf_t
*abp
; /* buffer for ablock */
657 xfs_alloc_arg_t args
; /* allocation arguments */
658 xfs_bmbt_rec_t
*arp
; /* child record pointer */
659 struct xfs_btree_block
*block
; /* btree root block */
660 xfs_btree_cur_t
*cur
; /* bmap btree cursor */
661 xfs_bmbt_rec_host_t
*ep
; /* extent record pointer */
662 int error
; /* error return value */
663 xfs_extnum_t i
, cnt
; /* extent record index */
664 xfs_ifork_t
*ifp
; /* inode fork pointer */
665 xfs_bmbt_key_t
*kp
; /* root block key pointer */
666 xfs_mount_t
*mp
; /* mount structure */
667 xfs_extnum_t nextents
; /* number of file extents */
668 xfs_bmbt_ptr_t
*pp
; /* root block address pointer */
671 ASSERT(whichfork
!= XFS_COW_FORK
);
672 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
673 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
);
676 * Make space in the inode incore.
678 xfs_iroot_realloc(ip
, 1, whichfork
);
679 ifp
->if_flags
|= XFS_IFBROOT
;
684 block
= ifp
->if_broot
;
685 xfs_btree_init_block_int(mp
, block
, XFS_BUF_DADDR_NULL
,
686 XFS_BTNUM_BMAP
, 1, 1, ip
->i_ino
,
687 XFS_BTREE_LONG_PTRS
);
689 * Need a cursor. Can't allocate until bb_level is filled in.
691 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
692 cur
->bc_private
.b
.firstblock
= *firstblock
;
693 cur
->bc_private
.b
.dfops
= dfops
;
694 cur
->bc_private
.b
.flags
= wasdel
? XFS_BTCUR_BPRV_WASDEL
: 0;
696 * Convert to a btree with two levels, one record in root.
698 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_BTREE
);
699 memset(&args
, 0, sizeof(args
));
702 xfs_rmap_ino_bmbt_owner(&args
.oinfo
, ip
->i_ino
, whichfork
);
703 args
.firstblock
= *firstblock
;
704 if (*firstblock
== NULLFSBLOCK
) {
705 args
.type
= XFS_ALLOCTYPE_START_BNO
;
706 args
.fsbno
= XFS_INO_TO_FSB(mp
, ip
->i_ino
);
707 } else if (dfops
->dop_low
) {
708 args
.type
= XFS_ALLOCTYPE_START_BNO
;
709 args
.fsbno
= *firstblock
;
711 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
712 args
.fsbno
= *firstblock
;
714 args
.minlen
= args
.maxlen
= args
.prod
= 1;
715 args
.wasdel
= wasdel
;
717 if ((error
= xfs_alloc_vextent(&args
))) {
718 xfs_iroot_realloc(ip
, -1, whichfork
);
719 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
723 if (WARN_ON_ONCE(args
.fsbno
== NULLFSBLOCK
)) {
724 xfs_iroot_realloc(ip
, -1, whichfork
);
725 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
729 * Allocation can't fail, the space was reserved.
731 ASSERT(*firstblock
== NULLFSBLOCK
||
732 args
.agno
>= XFS_FSB_TO_AGNO(mp
, *firstblock
));
733 *firstblock
= cur
->bc_private
.b
.firstblock
= args
.fsbno
;
734 cur
->bc_private
.b
.allocated
++;
735 ip
->i_d
.di_nblocks
++;
736 xfs_trans_mod_dquot_byino(tp
, ip
, XFS_TRANS_DQ_BCOUNT
, 1L);
737 abp
= xfs_btree_get_bufl(mp
, tp
, args
.fsbno
, 0);
739 * Fill in the child block.
741 abp
->b_ops
= &xfs_bmbt_buf_ops
;
742 ablock
= XFS_BUF_TO_BLOCK(abp
);
743 xfs_btree_init_block_int(mp
, ablock
, abp
->b_bn
,
744 XFS_BTNUM_BMAP
, 0, 0, ip
->i_ino
,
745 XFS_BTREE_LONG_PTRS
);
747 arp
= XFS_BMBT_REC_ADDR(mp
, ablock
, 1);
748 nextents
= xfs_iext_count(ifp
);
749 for (cnt
= i
= 0; i
< nextents
; i
++) {
750 ep
= xfs_iext_get_ext(ifp
, i
);
751 if (!isnullstartblock(xfs_bmbt_get_startblock(ep
))) {
752 arp
->l0
= cpu_to_be64(ep
->l0
);
753 arp
->l1
= cpu_to_be64(ep
->l1
);
757 ASSERT(cnt
== XFS_IFORK_NEXTENTS(ip
, whichfork
));
758 xfs_btree_set_numrecs(ablock
, cnt
);
761 * Fill in the root key and pointer.
763 kp
= XFS_BMBT_KEY_ADDR(mp
, block
, 1);
764 arp
= XFS_BMBT_REC_ADDR(mp
, ablock
, 1);
765 kp
->br_startoff
= cpu_to_be64(xfs_bmbt_disk_get_startoff(arp
));
766 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, xfs_bmbt_get_maxrecs(cur
,
767 be16_to_cpu(block
->bb_level
)));
768 *pp
= cpu_to_be64(args
.fsbno
);
771 * Do all this logging at the end so that
772 * the root is at the right level.
774 xfs_btree_log_block(cur
, abp
, XFS_BB_ALL_BITS
);
775 xfs_btree_log_recs(cur
, abp
, 1, be16_to_cpu(ablock
->bb_numrecs
));
776 ASSERT(*curp
== NULL
);
778 *logflagsp
= XFS_ILOG_CORE
| xfs_ilog_fbroot(whichfork
);
783 * Convert a local file to an extents file.
784 * This code is out of bounds for data forks of regular files,
785 * since the file data needs to get logged so things will stay consistent.
786 * (The bmap-level manipulations are ok, though).
789 xfs_bmap_local_to_extents_empty(
790 struct xfs_inode
*ip
,
793 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
795 ASSERT(whichfork
!= XFS_COW_FORK
);
796 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
797 ASSERT(ifp
->if_bytes
== 0);
798 ASSERT(XFS_IFORK_NEXTENTS(ip
, whichfork
) == 0);
800 xfs_bmap_forkoff_reset(ip
, whichfork
);
801 ifp
->if_flags
&= ~XFS_IFINLINE
;
802 ifp
->if_flags
|= XFS_IFEXTENTS
;
803 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_EXTENTS
);
807 STATIC
int /* error */
808 xfs_bmap_local_to_extents(
809 xfs_trans_t
*tp
, /* transaction pointer */
810 xfs_inode_t
*ip
, /* incore inode pointer */
811 xfs_fsblock_t
*firstblock
, /* first block allocated in xaction */
812 xfs_extlen_t total
, /* total blocks needed by transaction */
813 int *logflagsp
, /* inode logging flags */
815 void (*init_fn
)(struct xfs_trans
*tp
,
817 struct xfs_inode
*ip
,
818 struct xfs_ifork
*ifp
))
821 int flags
; /* logging flags returned */
822 xfs_ifork_t
*ifp
; /* inode fork pointer */
823 xfs_alloc_arg_t args
; /* allocation arguments */
824 xfs_buf_t
*bp
; /* buffer for extent block */
825 struct xfs_bmbt_irec rec
;
828 * We don't want to deal with the case of keeping inode data inline yet.
829 * So sending the data fork of a regular inode is invalid.
831 ASSERT(!(S_ISREG(VFS_I(ip
)->i_mode
) && whichfork
== XFS_DATA_FORK
));
832 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
833 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
835 if (!ifp
->if_bytes
) {
836 xfs_bmap_local_to_extents_empty(ip
, whichfork
);
837 flags
= XFS_ILOG_CORE
;
843 ASSERT((ifp
->if_flags
& (XFS_IFINLINE
|XFS_IFEXTENTS
|XFS_IFEXTIREC
)) ==
845 memset(&args
, 0, sizeof(args
));
847 args
.mp
= ip
->i_mount
;
848 xfs_rmap_ino_owner(&args
.oinfo
, ip
->i_ino
, whichfork
, 0);
849 args
.firstblock
= *firstblock
;
851 * Allocate a block. We know we need only one, since the
852 * file currently fits in an inode.
854 if (*firstblock
== NULLFSBLOCK
) {
855 args
.fsbno
= XFS_INO_TO_FSB(args
.mp
, ip
->i_ino
);
856 args
.type
= XFS_ALLOCTYPE_START_BNO
;
858 args
.fsbno
= *firstblock
;
859 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
862 args
.minlen
= args
.maxlen
= args
.prod
= 1;
863 error
= xfs_alloc_vextent(&args
);
867 /* Can't fail, the space was reserved. */
868 ASSERT(args
.fsbno
!= NULLFSBLOCK
);
869 ASSERT(args
.len
== 1);
870 *firstblock
= args
.fsbno
;
871 bp
= xfs_btree_get_bufl(args
.mp
, tp
, args
.fsbno
, 0);
874 * Initialize the block, copy the data and log the remote buffer.
876 * The callout is responsible for logging because the remote format
877 * might differ from the local format and thus we don't know how much to
878 * log here. Note that init_fn must also set the buffer log item type
881 init_fn(tp
, bp
, ip
, ifp
);
883 /* account for the change in fork size */
884 xfs_idata_realloc(ip
, -ifp
->if_bytes
, whichfork
);
885 xfs_bmap_local_to_extents_empty(ip
, whichfork
);
886 flags
|= XFS_ILOG_CORE
;
889 rec
.br_startblock
= args
.fsbno
;
890 rec
.br_blockcount
= 1;
891 rec
.br_state
= XFS_EXT_NORM
;
892 xfs_iext_insert(ip
, 0, 1, &rec
, 0);
894 XFS_IFORK_NEXT_SET(ip
, whichfork
, 1);
895 ip
->i_d
.di_nblocks
= 1;
896 xfs_trans_mod_dquot_byino(tp
, ip
,
897 XFS_TRANS_DQ_BCOUNT
, 1L);
898 flags
|= xfs_ilog_fext(whichfork
);
906 * Called from xfs_bmap_add_attrfork to handle btree format files.
908 STATIC
int /* error */
909 xfs_bmap_add_attrfork_btree(
910 xfs_trans_t
*tp
, /* transaction pointer */
911 xfs_inode_t
*ip
, /* incore inode pointer */
912 xfs_fsblock_t
*firstblock
, /* first block allocated */
913 struct xfs_defer_ops
*dfops
, /* blocks to free at commit */
914 int *flags
) /* inode logging flags */
916 xfs_btree_cur_t
*cur
; /* btree cursor */
917 int error
; /* error return value */
918 xfs_mount_t
*mp
; /* file system mount struct */
919 int stat
; /* newroot status */
922 if (ip
->i_df
.if_broot_bytes
<= XFS_IFORK_DSIZE(ip
))
923 *flags
|= XFS_ILOG_DBROOT
;
925 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, XFS_DATA_FORK
);
926 cur
->bc_private
.b
.dfops
= dfops
;
927 cur
->bc_private
.b
.firstblock
= *firstblock
;
928 error
= xfs_bmbt_lookup_first(cur
, &stat
);
931 /* must be at least one entry */
932 XFS_WANT_CORRUPTED_GOTO(mp
, stat
== 1, error0
);
933 if ((error
= xfs_btree_new_iroot(cur
, flags
, &stat
)))
936 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
939 *firstblock
= cur
->bc_private
.b
.firstblock
;
940 cur
->bc_private
.b
.allocated
= 0;
941 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
945 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
950 * Called from xfs_bmap_add_attrfork to handle extents format files.
952 STATIC
int /* error */
953 xfs_bmap_add_attrfork_extents(
954 xfs_trans_t
*tp
, /* transaction pointer */
955 xfs_inode_t
*ip
, /* incore inode pointer */
956 xfs_fsblock_t
*firstblock
, /* first block allocated */
957 struct xfs_defer_ops
*dfops
, /* blocks to free at commit */
958 int *flags
) /* inode logging flags */
960 xfs_btree_cur_t
*cur
; /* bmap btree cursor */
961 int error
; /* error return value */
963 if (ip
->i_d
.di_nextents
* sizeof(xfs_bmbt_rec_t
) <= XFS_IFORK_DSIZE(ip
))
966 error
= xfs_bmap_extents_to_btree(tp
, ip
, firstblock
, dfops
, &cur
, 0,
967 flags
, XFS_DATA_FORK
);
969 cur
->bc_private
.b
.allocated
= 0;
970 xfs_btree_del_cursor(cur
,
971 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
977 * Called from xfs_bmap_add_attrfork to handle local format files. Each
978 * different data fork content type needs a different callout to do the
979 * conversion. Some are basic and only require special block initialisation
980 * callouts for the data formating, others (directories) are so specialised they
981 * handle everything themselves.
983 * XXX (dgc): investigate whether directory conversion can use the generic
984 * formatting callout. It should be possible - it's just a very complex
987 STATIC
int /* error */
988 xfs_bmap_add_attrfork_local(
989 xfs_trans_t
*tp
, /* transaction pointer */
990 xfs_inode_t
*ip
, /* incore inode pointer */
991 xfs_fsblock_t
*firstblock
, /* first block allocated */
992 struct xfs_defer_ops
*dfops
, /* blocks to free at commit */
993 int *flags
) /* inode logging flags */
995 xfs_da_args_t dargs
; /* args for dir/attr code */
997 if (ip
->i_df
.if_bytes
<= XFS_IFORK_DSIZE(ip
))
1000 if (S_ISDIR(VFS_I(ip
)->i_mode
)) {
1001 memset(&dargs
, 0, sizeof(dargs
));
1002 dargs
.geo
= ip
->i_mount
->m_dir_geo
;
1004 dargs
.firstblock
= firstblock
;
1005 dargs
.dfops
= dfops
;
1006 dargs
.total
= dargs
.geo
->fsbcount
;
1007 dargs
.whichfork
= XFS_DATA_FORK
;
1009 return xfs_dir2_sf_to_block(&dargs
);
1012 if (S_ISLNK(VFS_I(ip
)->i_mode
))
1013 return xfs_bmap_local_to_extents(tp
, ip
, firstblock
, 1,
1014 flags
, XFS_DATA_FORK
,
1015 xfs_symlink_local_to_remote
);
1017 /* should only be called for types that support local format data */
1019 return -EFSCORRUPTED
;
1023 * Convert inode from non-attributed to attributed.
1024 * Must not be in a transaction, ip must not be locked.
1026 int /* error code */
1027 xfs_bmap_add_attrfork(
1028 xfs_inode_t
*ip
, /* incore inode pointer */
1029 int size
, /* space new attribute needs */
1030 int rsvd
) /* xact may use reserved blks */
1032 xfs_fsblock_t firstblock
; /* 1st block/ag allocated */
1033 struct xfs_defer_ops dfops
; /* freed extent records */
1034 xfs_mount_t
*mp
; /* mount structure */
1035 xfs_trans_t
*tp
; /* transaction pointer */
1036 int blks
; /* space reservation */
1037 int version
= 1; /* superblock attr version */
1038 int logflags
; /* logging flags */
1039 int error
; /* error return value */
1041 ASSERT(XFS_IFORK_Q(ip
) == 0);
1044 ASSERT(!XFS_NOT_DQATTACHED(mp
, ip
));
1046 blks
= XFS_ADDAFORK_SPACE_RES(mp
);
1048 error
= xfs_trans_alloc(mp
, &M_RES(mp
)->tr_addafork
, blks
, 0,
1049 rsvd
? XFS_TRANS_RESERVE
: 0, &tp
);
1053 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
1054 error
= xfs_trans_reserve_quota_nblks(tp
, ip
, blks
, 0, rsvd
?
1055 XFS_QMOPT_RES_REGBLKS
| XFS_QMOPT_FORCE_RES
:
1056 XFS_QMOPT_RES_REGBLKS
);
1059 if (XFS_IFORK_Q(ip
))
1061 if (ip
->i_d
.di_anextents
!= 0) {
1062 error
= -EFSCORRUPTED
;
1065 if (ip
->i_d
.di_aformat
!= XFS_DINODE_FMT_EXTENTS
) {
1067 * For inodes coming from pre-6.2 filesystems.
1069 ASSERT(ip
->i_d
.di_aformat
== 0);
1070 ip
->i_d
.di_aformat
= XFS_DINODE_FMT_EXTENTS
;
1073 xfs_trans_ijoin(tp
, ip
, 0);
1074 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
1076 switch (ip
->i_d
.di_format
) {
1077 case XFS_DINODE_FMT_DEV
:
1078 ip
->i_d
.di_forkoff
= roundup(sizeof(xfs_dev_t
), 8) >> 3;
1080 case XFS_DINODE_FMT_UUID
:
1081 ip
->i_d
.di_forkoff
= roundup(sizeof(uuid_t
), 8) >> 3;
1083 case XFS_DINODE_FMT_LOCAL
:
1084 case XFS_DINODE_FMT_EXTENTS
:
1085 case XFS_DINODE_FMT_BTREE
:
1086 ip
->i_d
.di_forkoff
= xfs_attr_shortform_bytesfit(ip
, size
);
1087 if (!ip
->i_d
.di_forkoff
)
1088 ip
->i_d
.di_forkoff
= xfs_default_attroffset(ip
) >> 3;
1089 else if (mp
->m_flags
& XFS_MOUNT_ATTR2
)
1098 ASSERT(ip
->i_afp
== NULL
);
1099 ip
->i_afp
= kmem_zone_zalloc(xfs_ifork_zone
, KM_SLEEP
);
1100 ip
->i_afp
->if_flags
= XFS_IFEXTENTS
;
1102 xfs_defer_init(&dfops
, &firstblock
);
1103 switch (ip
->i_d
.di_format
) {
1104 case XFS_DINODE_FMT_LOCAL
:
1105 error
= xfs_bmap_add_attrfork_local(tp
, ip
, &firstblock
, &dfops
,
1108 case XFS_DINODE_FMT_EXTENTS
:
1109 error
= xfs_bmap_add_attrfork_extents(tp
, ip
, &firstblock
,
1112 case XFS_DINODE_FMT_BTREE
:
1113 error
= xfs_bmap_add_attrfork_btree(tp
, ip
, &firstblock
, &dfops
,
1121 xfs_trans_log_inode(tp
, ip
, logflags
);
1124 if (!xfs_sb_version_hasattr(&mp
->m_sb
) ||
1125 (!xfs_sb_version_hasattr2(&mp
->m_sb
) && version
== 2)) {
1126 bool log_sb
= false;
1128 spin_lock(&mp
->m_sb_lock
);
1129 if (!xfs_sb_version_hasattr(&mp
->m_sb
)) {
1130 xfs_sb_version_addattr(&mp
->m_sb
);
1133 if (!xfs_sb_version_hasattr2(&mp
->m_sb
) && version
== 2) {
1134 xfs_sb_version_addattr2(&mp
->m_sb
);
1137 spin_unlock(&mp
->m_sb_lock
);
1142 error
= xfs_defer_finish(&tp
, &dfops
);
1145 error
= xfs_trans_commit(tp
);
1146 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
1150 xfs_defer_cancel(&dfops
);
1152 xfs_trans_cancel(tp
);
1153 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
1158 * Internal and external extent tree search functions.
1162 * Read in the extents to if_extents.
1163 * All inode fields are set up by caller, we just traverse the btree
1164 * and copy the records in. If the file system cannot contain unwritten
1165 * extents, the records are checked for no "state" flags.
1168 xfs_bmap_read_extents(
1169 xfs_trans_t
*tp
, /* transaction pointer */
1170 xfs_inode_t
*ip
, /* incore inode */
1171 int whichfork
) /* data or attr fork */
1173 struct xfs_btree_block
*block
; /* current btree block */
1174 xfs_fsblock_t bno
; /* block # of "block" */
1175 xfs_buf_t
*bp
; /* buffer for "block" */
1176 int error
; /* error return value */
1177 xfs_extnum_t i
, j
; /* index into the extents list */
1178 xfs_ifork_t
*ifp
; /* fork structure */
1179 int level
; /* btree level, for checking */
1180 xfs_mount_t
*mp
; /* file system mount structure */
1181 __be64
*pp
; /* pointer to block address */
1183 xfs_extnum_t room
; /* number of entries there's room for */
1184 int state
= xfs_bmap_fork_to_state(whichfork
);
1187 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1188 block
= ifp
->if_broot
;
1190 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
1192 level
= be16_to_cpu(block
->bb_level
);
1194 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, 1, ifp
->if_broot_bytes
);
1195 bno
= be64_to_cpu(*pp
);
1198 * Go down the tree until leaf level is reached, following the first
1199 * pointer (leftmost) at each level.
1201 while (level
-- > 0) {
1202 error
= xfs_btree_read_bufl(mp
, tp
, bno
, 0, &bp
,
1203 XFS_BMAP_BTREE_REF
, &xfs_bmbt_buf_ops
);
1206 block
= XFS_BUF_TO_BLOCK(bp
);
1209 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, mp
->m_bmap_dmxr
[1]);
1210 bno
= be64_to_cpu(*pp
);
1211 XFS_WANT_CORRUPTED_GOTO(mp
,
1212 XFS_FSB_SANITY_CHECK(mp
, bno
), error0
);
1213 xfs_trans_brelse(tp
, bp
);
1216 * Here with bp and block set to the leftmost leaf node in the tree.
1218 room
= xfs_iext_count(ifp
);
1221 * Loop over all leaf nodes. Copy information to the extent records.
1224 xfs_bmbt_rec_t
*frp
;
1225 xfs_fsblock_t nextbno
;
1226 xfs_extnum_t num_recs
;
1228 num_recs
= xfs_btree_get_numrecs(block
);
1229 if (unlikely(i
+ num_recs
> room
)) {
1230 ASSERT(i
+ num_recs
<= room
);
1231 xfs_warn(ip
->i_mount
,
1232 "corrupt dinode %Lu, (btree extents).",
1233 (unsigned long long) ip
->i_ino
);
1234 XFS_CORRUPTION_ERROR("xfs_bmap_read_extents(1)",
1235 XFS_ERRLEVEL_LOW
, ip
->i_mount
, block
);
1239 * Read-ahead the next leaf block, if any.
1241 nextbno
= be64_to_cpu(block
->bb_u
.l
.bb_rightsib
);
1242 if (nextbno
!= NULLFSBLOCK
)
1243 xfs_btree_reada_bufl(mp
, nextbno
, 1,
1246 * Copy records into the extent records.
1248 frp
= XFS_BMBT_REC_ADDR(mp
, block
, 1);
1249 for (j
= 0; j
< num_recs
; j
++, i
++, frp
++) {
1250 xfs_bmbt_rec_host_t
*trp
= xfs_iext_get_ext(ifp
, i
);
1251 trp
->l0
= be64_to_cpu(frp
->l0
);
1252 trp
->l1
= be64_to_cpu(frp
->l1
);
1253 if (!xfs_bmbt_validate_extent(mp
, whichfork
, trp
)) {
1254 XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
1255 XFS_ERRLEVEL_LOW
, mp
);
1258 trace_xfs_read_extent(ip
, i
, state
, _THIS_IP_
);
1260 xfs_trans_brelse(tp
, bp
);
1263 * If we've reached the end, stop.
1265 if (bno
== NULLFSBLOCK
)
1267 error
= xfs_btree_read_bufl(mp
, tp
, bno
, 0, &bp
,
1268 XFS_BMAP_BTREE_REF
, &xfs_bmbt_buf_ops
);
1271 block
= XFS_BUF_TO_BLOCK(bp
);
1273 if (i
!= XFS_IFORK_NEXTENTS(ip
, whichfork
))
1274 return -EFSCORRUPTED
;
1275 ASSERT(i
== xfs_iext_count(ifp
));
1278 xfs_trans_brelse(tp
, bp
);
1279 return -EFSCORRUPTED
;
1283 * Returns the file-relative block number of the first unused block(s)
1284 * in the file with at least "len" logically contiguous blocks free.
1285 * This is the lowest-address hole if the file has holes, else the first block
1286 * past the end of file.
1287 * Return 0 if the file is currently local (in-inode).
1290 xfs_bmap_first_unused(
1291 xfs_trans_t
*tp
, /* transaction pointer */
1292 xfs_inode_t
*ip
, /* incore inode */
1293 xfs_extlen_t len
, /* size of hole to find */
1294 xfs_fileoff_t
*first_unused
, /* unused block */
1295 int whichfork
) /* data or attr fork */
1297 int error
; /* error return value */
1298 int idx
; /* extent record index */
1299 xfs_ifork_t
*ifp
; /* inode fork pointer */
1300 xfs_fileoff_t lastaddr
; /* last block number seen */
1301 xfs_fileoff_t lowest
; /* lowest useful block */
1302 xfs_fileoff_t max
; /* starting useful block */
1303 xfs_extnum_t nextents
; /* number of extent entries */
1305 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
||
1306 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
||
1307 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
1308 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
) {
1312 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1313 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
1314 (error
= xfs_iread_extents(tp
, ip
, whichfork
)))
1316 lowest
= *first_unused
;
1317 nextents
= xfs_iext_count(ifp
);
1318 for (idx
= 0, lastaddr
= 0, max
= lowest
; idx
< nextents
; idx
++) {
1319 struct xfs_bmbt_irec got
;
1321 xfs_iext_get_extent(ifp
, idx
, &got
);
1324 * See if the hole before this extent will work.
1326 if (got
.br_startoff
>= lowest
+ len
&&
1327 got
.br_startoff
- max
>= len
) {
1328 *first_unused
= max
;
1331 lastaddr
= got
.br_startoff
+ got
.br_blockcount
;
1332 max
= XFS_FILEOFF_MAX(lastaddr
, lowest
);
1334 *first_unused
= max
;
1339 * Returns the file-relative block number of the last block - 1 before
1340 * last_block (input value) in the file.
1341 * This is not based on i_size, it is based on the extent records.
1342 * Returns 0 for local files, as they do not have extent records.
1345 xfs_bmap_last_before(
1346 struct xfs_trans
*tp
, /* transaction pointer */
1347 struct xfs_inode
*ip
, /* incore inode */
1348 xfs_fileoff_t
*last_block
, /* last block */
1349 int whichfork
) /* data or attr fork */
1351 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1352 struct xfs_bmbt_irec got
;
1356 switch (XFS_IFORK_FORMAT(ip
, whichfork
)) {
1357 case XFS_DINODE_FMT_LOCAL
:
1360 case XFS_DINODE_FMT_BTREE
:
1361 case XFS_DINODE_FMT_EXTENTS
:
1367 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
1368 error
= xfs_iread_extents(tp
, ip
, whichfork
);
1373 if (xfs_iext_lookup_extent(ip
, ifp
, *last_block
- 1, &idx
, &got
)) {
1374 if (got
.br_startoff
<= *last_block
- 1)
1378 if (xfs_iext_get_extent(ifp
, idx
- 1, &got
)) {
1379 *last_block
= got
.br_startoff
+ got
.br_blockcount
;
1388 xfs_bmap_last_extent(
1389 struct xfs_trans
*tp
,
1390 struct xfs_inode
*ip
,
1392 struct xfs_bmbt_irec
*rec
,
1395 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1399 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
1400 error
= xfs_iread_extents(tp
, ip
, whichfork
);
1405 nextents
= xfs_iext_count(ifp
);
1406 if (nextents
== 0) {
1411 xfs_iext_get_extent(ifp
, nextents
- 1, rec
);
1417 * Check the last inode extent to determine whether this allocation will result
1418 * in blocks being allocated at the end of the file. When we allocate new data
1419 * blocks at the end of the file which do not start at the previous data block,
1420 * we will try to align the new blocks at stripe unit boundaries.
1422 * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be
1423 * at, or past the EOF.
1427 struct xfs_bmalloca
*bma
,
1430 struct xfs_bmbt_irec rec
;
1435 error
= xfs_bmap_last_extent(NULL
, bma
->ip
, whichfork
, &rec
,
1446 * Check if we are allocation or past the last extent, or at least into
1447 * the last delayed allocated extent.
1449 bma
->aeof
= bma
->offset
>= rec
.br_startoff
+ rec
.br_blockcount
||
1450 (bma
->offset
>= rec
.br_startoff
&&
1451 isnullstartblock(rec
.br_startblock
));
1456 * Returns the file-relative block number of the first block past eof in
1457 * the file. This is not based on i_size, it is based on the extent records.
1458 * Returns 0 for local files, as they do not have extent records.
1461 xfs_bmap_last_offset(
1462 struct xfs_inode
*ip
,
1463 xfs_fileoff_t
*last_block
,
1466 struct xfs_bmbt_irec rec
;
1472 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
)
1475 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
&&
1476 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
1479 error
= xfs_bmap_last_extent(NULL
, ip
, whichfork
, &rec
, &is_empty
);
1480 if (error
|| is_empty
)
1483 *last_block
= rec
.br_startoff
+ rec
.br_blockcount
;
1488 * Returns whether the selected fork of the inode has exactly one
1489 * block or not. For the data fork we check this matches di_size,
1490 * implying the file's range is 0..bsize-1.
1492 int /* 1=>1 block, 0=>otherwise */
1494 xfs_inode_t
*ip
, /* incore inode */
1495 int whichfork
) /* data or attr fork */
1497 xfs_ifork_t
*ifp
; /* inode fork pointer */
1498 int rval
; /* return value */
1499 xfs_bmbt_irec_t s
; /* internal version of extent */
1502 if (whichfork
== XFS_DATA_FORK
)
1503 return XFS_ISIZE(ip
) == ip
->i_mount
->m_sb
.sb_blocksize
;
1505 if (XFS_IFORK_NEXTENTS(ip
, whichfork
) != 1)
1507 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
1509 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1510 ASSERT(ifp
->if_flags
& XFS_IFEXTENTS
);
1511 xfs_iext_get_extent(ifp
, 0, &s
);
1512 rval
= s
.br_startoff
== 0 && s
.br_blockcount
== 1;
1513 if (rval
&& whichfork
== XFS_DATA_FORK
)
1514 ASSERT(XFS_ISIZE(ip
) == ip
->i_mount
->m_sb
.sb_blocksize
);
1519 * Extent tree manipulation functions used during allocation.
1523 * Convert a delayed allocation to a real allocation.
1525 STATIC
int /* error */
1526 xfs_bmap_add_extent_delay_real(
1527 struct xfs_bmalloca
*bma
,
1530 struct xfs_bmbt_irec
*new = &bma
->got
;
1531 int error
; /* error return value */
1532 int i
; /* temp state */
1533 xfs_ifork_t
*ifp
; /* inode fork pointer */
1534 xfs_fileoff_t new_endoff
; /* end offset of new entry */
1535 xfs_bmbt_irec_t r
[3]; /* neighbor extent entries */
1536 /* left is 0, right is 1, prev is 2 */
1537 int rval
=0; /* return value (logging flags) */
1538 int state
= xfs_bmap_fork_to_state(whichfork
);
1539 xfs_filblks_t da_new
; /* new count del alloc blocks used */
1540 xfs_filblks_t da_old
; /* old count del alloc blocks used */
1541 xfs_filblks_t temp
=0; /* value for da_new calculations */
1542 int tmp_rval
; /* partial logging flags */
1543 struct xfs_mount
*mp
;
1544 xfs_extnum_t
*nextents
;
1545 struct xfs_bmbt_irec old
;
1547 mp
= bma
->ip
->i_mount
;
1548 ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
1549 ASSERT(whichfork
!= XFS_ATTR_FORK
);
1550 nextents
= (whichfork
== XFS_COW_FORK
? &bma
->ip
->i_cnextents
:
1551 &bma
->ip
->i_d
.di_nextents
);
1553 ASSERT(bma
->idx
>= 0);
1554 ASSERT(bma
->idx
<= xfs_iext_count(ifp
));
1555 ASSERT(!isnullstartblock(new->br_startblock
));
1557 (bma
->cur
->bc_private
.b
.flags
& XFS_BTCUR_BPRV_WASDEL
));
1559 XFS_STATS_INC(mp
, xs_add_exlist
);
1566 * Set up a bunch of variables to make the tests simpler.
1568 xfs_iext_get_extent(ifp
, bma
->idx
, &PREV
);
1569 new_endoff
= new->br_startoff
+ new->br_blockcount
;
1570 ASSERT(isnullstartblock(PREV
.br_startblock
));
1571 ASSERT(PREV
.br_startoff
<= new->br_startoff
);
1572 ASSERT(PREV
.br_startoff
+ PREV
.br_blockcount
>= new_endoff
);
1574 da_old
= startblockval(PREV
.br_startblock
);
1578 * Set flags determining what part of the previous delayed allocation
1579 * extent is being replaced by a real allocation.
1581 if (PREV
.br_startoff
== new->br_startoff
)
1582 state
|= BMAP_LEFT_FILLING
;
1583 if (PREV
.br_startoff
+ PREV
.br_blockcount
== new_endoff
)
1584 state
|= BMAP_RIGHT_FILLING
;
1587 * Check and set flags if this segment has a left neighbor.
1588 * Don't set contiguous if the combined extent would be too large.
1591 state
|= BMAP_LEFT_VALID
;
1592 xfs_iext_get_extent(ifp
, bma
->idx
- 1, &LEFT
);
1594 if (isnullstartblock(LEFT
.br_startblock
))
1595 state
|= BMAP_LEFT_DELAY
;
1598 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
1599 LEFT
.br_startoff
+ LEFT
.br_blockcount
== new->br_startoff
&&
1600 LEFT
.br_startblock
+ LEFT
.br_blockcount
== new->br_startblock
&&
1601 LEFT
.br_state
== new->br_state
&&
1602 LEFT
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
1603 state
|= BMAP_LEFT_CONTIG
;
1606 * Check and set flags if this segment has a right neighbor.
1607 * Don't set contiguous if the combined extent would be too large.
1608 * Also check for all-three-contiguous being too large.
1610 if (bma
->idx
< xfs_iext_count(ifp
) - 1) {
1611 state
|= BMAP_RIGHT_VALID
;
1612 xfs_iext_get_extent(ifp
, bma
->idx
+ 1, &RIGHT
);
1614 if (isnullstartblock(RIGHT
.br_startblock
))
1615 state
|= BMAP_RIGHT_DELAY
;
1618 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
1619 new_endoff
== RIGHT
.br_startoff
&&
1620 new->br_startblock
+ new->br_blockcount
== RIGHT
.br_startblock
&&
1621 new->br_state
== RIGHT
.br_state
&&
1622 new->br_blockcount
+ RIGHT
.br_blockcount
<= MAXEXTLEN
&&
1623 ((state
& (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
1624 BMAP_RIGHT_FILLING
)) !=
1625 (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
1626 BMAP_RIGHT_FILLING
) ||
1627 LEFT
.br_blockcount
+ new->br_blockcount
+ RIGHT
.br_blockcount
1629 state
|= BMAP_RIGHT_CONTIG
;
1633 * Switch out based on the FILLING and CONTIG state bits.
1635 switch (state
& (BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
1636 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
)) {
1637 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
1638 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
1640 * Filling in all of a previously delayed allocation extent.
1641 * The left and right neighbors are both contiguous with new.
1644 LEFT
.br_blockcount
+= PREV
.br_blockcount
+ RIGHT
.br_blockcount
;
1645 xfs_iext_update_extent(bma
->ip
, state
, bma
->idx
, &LEFT
);
1647 xfs_iext_remove(bma
->ip
, bma
->idx
+ 1, 2, state
);
1649 if (bma
->cur
== NULL
)
1650 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1652 rval
= XFS_ILOG_CORE
;
1653 error
= xfs_bmbt_lookup_eq(bma
->cur
, &RIGHT
, &i
);
1656 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1657 error
= xfs_btree_delete(bma
->cur
, &i
);
1660 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1661 error
= xfs_btree_decrement(bma
->cur
, 0, &i
);
1664 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1665 error
= xfs_bmbt_update(bma
->cur
, &LEFT
);
1671 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
1673 * Filling in all of a previously delayed allocation extent.
1674 * The left neighbor is contiguous, the right is not.
1679 LEFT
.br_blockcount
+= PREV
.br_blockcount
;
1680 xfs_iext_update_extent(bma
->ip
, state
, bma
->idx
, &LEFT
);
1682 xfs_iext_remove(bma
->ip
, bma
->idx
+ 1, 1, state
);
1683 if (bma
->cur
== NULL
)
1684 rval
= XFS_ILOG_DEXT
;
1687 error
= xfs_bmbt_lookup_eq(bma
->cur
, &old
, &i
);
1690 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1691 error
= xfs_bmbt_update(bma
->cur
, &LEFT
);
1697 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
1699 * Filling in all of a previously delayed allocation extent.
1700 * The right neighbor is contiguous, the left is not.
1702 PREV
.br_startblock
= new->br_startblock
;
1703 PREV
.br_blockcount
+= RIGHT
.br_blockcount
;
1704 xfs_iext_update_extent(bma
->ip
, state
, bma
->idx
, &PREV
);
1706 xfs_iext_remove(bma
->ip
, bma
->idx
+ 1, 1, state
);
1707 if (bma
->cur
== NULL
)
1708 rval
= XFS_ILOG_DEXT
;
1711 error
= xfs_bmbt_lookup_eq(bma
->cur
, &RIGHT
, &i
);
1714 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1715 error
= xfs_bmbt_update(bma
->cur
, &PREV
);
1721 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
1723 * Filling in all of a previously delayed allocation extent.
1724 * Neither the left nor right neighbors are contiguous with
1727 PREV
.br_startblock
= new->br_startblock
;
1728 PREV
.br_state
= new->br_state
;
1729 xfs_iext_update_extent(bma
->ip
, state
, bma
->idx
, &PREV
);
1732 if (bma
->cur
== NULL
)
1733 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1735 rval
= XFS_ILOG_CORE
;
1736 error
= xfs_bmbt_lookup_eq(bma
->cur
, new, &i
);
1739 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
1740 error
= xfs_btree_insert(bma
->cur
, &i
);
1743 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1747 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
:
1749 * Filling in the first part of a previous delayed allocation.
1750 * The left neighbor is contiguous.
1753 temp
= PREV
.br_blockcount
- new->br_blockcount
;
1754 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
1755 startblockval(PREV
.br_startblock
));
1757 LEFT
.br_blockcount
+= new->br_blockcount
;
1758 xfs_iext_update_extent(bma
->ip
, state
, bma
->idx
- 1, &LEFT
);
1760 PREV
.br_blockcount
= temp
= PREV
.br_blockcount
- new->br_blockcount
;
1761 PREV
.br_startoff
+= new->br_blockcount
;
1762 PREV
.br_startblock
= nullstartblock(da_new
);
1763 xfs_iext_update_extent(bma
->ip
, state
, bma
->idx
, &PREV
);
1765 if (bma
->cur
== NULL
)
1766 rval
= XFS_ILOG_DEXT
;
1769 error
= xfs_bmbt_lookup_eq(bma
->cur
, &old
, &i
);
1772 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1773 error
= xfs_bmbt_update(bma
->cur
, &LEFT
);
1781 case BMAP_LEFT_FILLING
:
1783 * Filling in the first part of a previous delayed allocation.
1784 * The left neighbor is not contiguous.
1786 xfs_iext_insert(bma
->ip
, bma
->idx
, 1, new, state
);
1788 if (bma
->cur
== NULL
)
1789 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1791 rval
= XFS_ILOG_CORE
;
1792 error
= xfs_bmbt_lookup_eq(bma
->cur
, new, &i
);
1795 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
1796 error
= xfs_btree_insert(bma
->cur
, &i
);
1799 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1802 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
1803 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
1804 bma
->firstblock
, bma
->dfops
,
1805 &bma
->cur
, 1, &tmp_rval
, whichfork
);
1811 temp
= PREV
.br_blockcount
- new->br_blockcount
;
1812 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
1813 startblockval(PREV
.br_startblock
) -
1814 (bma
->cur
? bma
->cur
->bc_private
.b
.allocated
: 0));
1816 PREV
.br_startoff
= new_endoff
;
1817 PREV
.br_blockcount
= temp
;
1818 PREV
.br_startblock
= nullstartblock(da_new
);
1819 xfs_iext_update_extent(bma
->ip
, state
, bma
->idx
+ 1, &PREV
);
1822 case BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
1824 * Filling in the last part of a previous delayed allocation.
1825 * The right neighbor is contiguous with the new allocation.
1828 RIGHT
.br_startoff
= new->br_startoff
;
1829 RIGHT
.br_startblock
= new->br_startblock
;
1830 RIGHT
.br_blockcount
+= new->br_blockcount
;
1831 xfs_iext_update_extent(bma
->ip
, state
, bma
->idx
+ 1, &RIGHT
);
1833 if (bma
->cur
== NULL
)
1834 rval
= XFS_ILOG_DEXT
;
1837 error
= xfs_bmbt_lookup_eq(bma
->cur
, &old
, &i
);
1840 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1841 error
= xfs_bmbt_update(bma
->cur
, &RIGHT
);
1846 temp
= PREV
.br_blockcount
- new->br_blockcount
;
1847 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
1848 startblockval(PREV
.br_startblock
));
1850 PREV
.br_blockcount
= temp
;
1851 PREV
.br_startblock
= nullstartblock(da_new
);
1852 xfs_iext_update_extent(bma
->ip
, state
, bma
->idx
, &PREV
);
1857 case BMAP_RIGHT_FILLING
:
1859 * Filling in the last part of a previous delayed allocation.
1860 * The right neighbor is not contiguous.
1862 xfs_iext_insert(bma
->ip
, bma
->idx
+ 1, 1, new, state
);
1864 if (bma
->cur
== NULL
)
1865 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1867 rval
= XFS_ILOG_CORE
;
1868 error
= xfs_bmbt_lookup_eq(bma
->cur
, new, &i
);
1871 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
1872 error
= xfs_btree_insert(bma
->cur
, &i
);
1875 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1878 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
1879 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
1880 bma
->firstblock
, bma
->dfops
, &bma
->cur
, 1,
1881 &tmp_rval
, whichfork
);
1887 temp
= PREV
.br_blockcount
- new->br_blockcount
;
1888 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
1889 startblockval(PREV
.br_startblock
) -
1890 (bma
->cur
? bma
->cur
->bc_private
.b
.allocated
: 0));
1892 PREV
.br_startblock
= nullstartblock(da_new
);
1893 PREV
.br_blockcount
= temp
;
1894 xfs_iext_update_extent(bma
->ip
, state
, bma
->idx
, &PREV
);
1901 * Filling in the middle part of a previous delayed allocation.
1902 * Contiguity is impossible here.
1903 * This case is avoided almost all the time.
1905 * We start with a delayed allocation:
1907 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
1910 * and we are allocating:
1911 * +rrrrrrrrrrrrrrrrr+
1914 * and we set it up for insertion as:
1915 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
1917 * PREV @ idx LEFT RIGHT
1918 * inserted at idx + 1
1922 /* LEFT is the new middle */
1925 /* RIGHT is the new right */
1926 RIGHT
.br_state
= PREV
.br_state
;
1927 RIGHT
.br_startoff
= new_endoff
;
1928 RIGHT
.br_blockcount
=
1929 PREV
.br_startoff
+ PREV
.br_blockcount
- new_endoff
;
1930 RIGHT
.br_startblock
=
1931 nullstartblock(xfs_bmap_worst_indlen(bma
->ip
,
1932 RIGHT
.br_blockcount
));
1935 PREV
.br_blockcount
= new->br_startoff
- PREV
.br_startoff
;
1936 PREV
.br_startblock
=
1937 nullstartblock(xfs_bmap_worst_indlen(bma
->ip
,
1938 PREV
.br_blockcount
));
1939 xfs_iext_update_extent(bma
->ip
, state
, bma
->idx
, &PREV
);
1941 /* insert LEFT (r[0]) and RIGHT (r[1]) at the same time */
1942 xfs_iext_insert(bma
->ip
, bma
->idx
+ 1, 2, &LEFT
, state
);
1945 if (bma
->cur
== NULL
)
1946 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1948 rval
= XFS_ILOG_CORE
;
1949 error
= xfs_bmbt_lookup_eq(bma
->cur
, new, &i
);
1952 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
1953 error
= xfs_btree_insert(bma
->cur
, &i
);
1956 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1959 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
1960 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
1961 bma
->firstblock
, bma
->dfops
, &bma
->cur
,
1962 1, &tmp_rval
, whichfork
);
1968 da_new
= startblockval(PREV
.br_startblock
) +
1969 startblockval(RIGHT
.br_startblock
);
1973 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
1974 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
1975 case BMAP_LEFT_FILLING
| BMAP_RIGHT_CONTIG
:
1976 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
1977 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
1978 case BMAP_LEFT_CONTIG
:
1979 case BMAP_RIGHT_CONTIG
:
1981 * These cases are all impossible.
1986 /* add reverse mapping */
1987 error
= xfs_rmap_map_extent(mp
, bma
->dfops
, bma
->ip
, whichfork
, new);
1991 /* convert to a btree if necessary */
1992 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
1993 int tmp_logflags
; /* partial log flag return val */
1995 ASSERT(bma
->cur
== NULL
);
1996 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
1997 bma
->firstblock
, bma
->dfops
, &bma
->cur
,
1998 da_old
> 0, &tmp_logflags
, whichfork
);
1999 bma
->logflags
|= tmp_logflags
;
2005 da_new
+= bma
->cur
->bc_private
.b
.allocated
;
2006 bma
->cur
->bc_private
.b
.allocated
= 0;
2009 /* adjust for changes in reserved delayed indirect blocks */
2010 if (da_new
!= da_old
) {
2011 ASSERT(state
== 0 || da_new
< da_old
);
2012 error
= xfs_mod_fdblocks(mp
, (int64_t)(da_old
- da_new
),
2016 xfs_bmap_check_leaf_extents(bma
->cur
, bma
->ip
, whichfork
);
2018 if (whichfork
!= XFS_COW_FORK
)
2019 bma
->logflags
|= rval
;
2027 * Convert an unwritten allocation to a real allocation or vice versa.
2029 STATIC
int /* error */
2030 xfs_bmap_add_extent_unwritten_real(
2031 struct xfs_trans
*tp
,
2032 xfs_inode_t
*ip
, /* incore inode pointer */
2034 xfs_extnum_t
*idx
, /* extent number to update/insert */
2035 xfs_btree_cur_t
**curp
, /* if *curp is null, not a btree */
2036 xfs_bmbt_irec_t
*new, /* new data to add to file extents */
2037 xfs_fsblock_t
*first
, /* pointer to firstblock variable */
2038 struct xfs_defer_ops
*dfops
, /* list of extents to be freed */
2039 int *logflagsp
) /* inode logging flags */
2041 xfs_btree_cur_t
*cur
; /* btree cursor */
2042 int error
; /* error return value */
2043 int i
; /* temp state */
2044 xfs_ifork_t
*ifp
; /* inode fork pointer */
2045 xfs_fileoff_t new_endoff
; /* end offset of new entry */
2046 xfs_bmbt_irec_t r
[3]; /* neighbor extent entries */
2047 /* left is 0, right is 1, prev is 2 */
2048 int rval
=0; /* return value (logging flags) */
2049 int state
= xfs_bmap_fork_to_state(whichfork
);
2050 struct xfs_mount
*mp
= ip
->i_mount
;
2051 struct xfs_bmbt_irec old
;
2056 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
2059 ASSERT(*idx
<= xfs_iext_count(ifp
));
2060 ASSERT(!isnullstartblock(new->br_startblock
));
2062 XFS_STATS_INC(mp
, xs_add_exlist
);
2069 * Set up a bunch of variables to make the tests simpler.
2072 xfs_iext_get_extent(ifp
, *idx
, &PREV
);
2073 ASSERT(new->br_state
!= PREV
.br_state
);
2074 new_endoff
= new->br_startoff
+ new->br_blockcount
;
2075 ASSERT(PREV
.br_startoff
<= new->br_startoff
);
2076 ASSERT(PREV
.br_startoff
+ PREV
.br_blockcount
>= new_endoff
);
2079 * Set flags determining what part of the previous oldext allocation
2080 * extent is being replaced by a newext allocation.
2082 if (PREV
.br_startoff
== new->br_startoff
)
2083 state
|= BMAP_LEFT_FILLING
;
2084 if (PREV
.br_startoff
+ PREV
.br_blockcount
== new_endoff
)
2085 state
|= BMAP_RIGHT_FILLING
;
2088 * Check and set flags if this segment has a left neighbor.
2089 * Don't set contiguous if the combined extent would be too large.
2092 state
|= BMAP_LEFT_VALID
;
2093 xfs_iext_get_extent(ifp
, *idx
- 1, &LEFT
);
2095 if (isnullstartblock(LEFT
.br_startblock
))
2096 state
|= BMAP_LEFT_DELAY
;
2099 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
2100 LEFT
.br_startoff
+ LEFT
.br_blockcount
== new->br_startoff
&&
2101 LEFT
.br_startblock
+ LEFT
.br_blockcount
== new->br_startblock
&&
2102 LEFT
.br_state
== new->br_state
&&
2103 LEFT
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
2104 state
|= BMAP_LEFT_CONTIG
;
2107 * Check and set flags if this segment has a right neighbor.
2108 * Don't set contiguous if the combined extent would be too large.
2109 * Also check for all-three-contiguous being too large.
2111 if (*idx
< xfs_iext_count(ifp
) - 1) {
2112 state
|= BMAP_RIGHT_VALID
;
2113 xfs_iext_get_extent(ifp
, *idx
+ 1, &RIGHT
);
2114 if (isnullstartblock(RIGHT
.br_startblock
))
2115 state
|= BMAP_RIGHT_DELAY
;
2118 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
2119 new_endoff
== RIGHT
.br_startoff
&&
2120 new->br_startblock
+ new->br_blockcount
== RIGHT
.br_startblock
&&
2121 new->br_state
== RIGHT
.br_state
&&
2122 new->br_blockcount
+ RIGHT
.br_blockcount
<= MAXEXTLEN
&&
2123 ((state
& (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
2124 BMAP_RIGHT_FILLING
)) !=
2125 (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
2126 BMAP_RIGHT_FILLING
) ||
2127 LEFT
.br_blockcount
+ new->br_blockcount
+ RIGHT
.br_blockcount
2129 state
|= BMAP_RIGHT_CONTIG
;
2132 * Switch out based on the FILLING and CONTIG state bits.
2134 switch (state
& (BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
2135 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
)) {
2136 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
2137 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2139 * Setting all of a previous oldext extent to newext.
2140 * The left and right neighbors are both contiguous with new.
2144 LEFT
.br_blockcount
+= PREV
.br_blockcount
+ RIGHT
.br_blockcount
;
2145 xfs_iext_update_extent(ip
, state
, *idx
, &LEFT
);
2147 xfs_iext_remove(ip
, *idx
+ 1, 2, state
);
2148 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2149 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 2);
2151 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2153 rval
= XFS_ILOG_CORE
;
2154 error
= xfs_bmbt_lookup_eq(cur
, &RIGHT
, &i
);
2157 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2158 if ((error
= xfs_btree_delete(cur
, &i
)))
2160 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2161 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2163 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2164 if ((error
= xfs_btree_delete(cur
, &i
)))
2166 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2167 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2169 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2170 error
= xfs_bmbt_update(cur
, &LEFT
);
2176 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
2178 * Setting all of a previous oldext extent to newext.
2179 * The left neighbor is contiguous, the right is not.
2183 LEFT
.br_blockcount
+= PREV
.br_blockcount
;
2184 xfs_iext_update_extent(ip
, state
, *idx
, &LEFT
);
2186 xfs_iext_remove(ip
, *idx
+ 1, 1, state
);
2187 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2188 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
2190 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2192 rval
= XFS_ILOG_CORE
;
2193 error
= xfs_bmbt_lookup_eq(cur
, &PREV
, &i
);
2196 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2197 if ((error
= xfs_btree_delete(cur
, &i
)))
2199 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2200 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2202 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2203 error
= xfs_bmbt_update(cur
, &LEFT
);
2209 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2211 * Setting all of a previous oldext extent to newext.
2212 * The right neighbor is contiguous, the left is not.
2214 PREV
.br_blockcount
+= RIGHT
.br_blockcount
;
2215 PREV
.br_state
= new->br_state
;
2216 xfs_iext_update_extent(ip
, state
, *idx
, &PREV
);
2218 xfs_iext_remove(ip
, *idx
+ 1, 1, state
);
2219 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2220 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
2222 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2224 rval
= XFS_ILOG_CORE
;
2225 error
= xfs_bmbt_lookup_eq(cur
, &RIGHT
, &i
);
2228 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2229 if ((error
= xfs_btree_delete(cur
, &i
)))
2231 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2232 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2234 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2235 error
= xfs_bmbt_update(cur
, &PREV
);
2241 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
2243 * Setting all of a previous oldext extent to newext.
2244 * Neither the left nor right neighbors are contiguous with
2247 PREV
.br_state
= new->br_state
;
2248 xfs_iext_update_extent(ip
, state
, *idx
, &PREV
);
2251 rval
= XFS_ILOG_DEXT
;
2254 error
= xfs_bmbt_lookup_eq(cur
, new, &i
);
2257 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2258 error
= xfs_bmbt_update(cur
, &PREV
);
2264 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
:
2266 * Setting the first part of a previous oldext extent to newext.
2267 * The left neighbor is contiguous.
2269 LEFT
.br_blockcount
+= new->br_blockcount
;
2270 xfs_iext_update_extent(ip
, state
, *idx
- 1, &LEFT
);
2273 PREV
.br_startoff
+= new->br_blockcount
;
2274 PREV
.br_startblock
+= new->br_blockcount
;
2275 PREV
.br_blockcount
-= new->br_blockcount
;
2276 xfs_iext_update_extent(ip
, state
, *idx
, &PREV
);
2281 rval
= XFS_ILOG_DEXT
;
2284 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2287 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2288 error
= xfs_bmbt_update(cur
, &PREV
);
2291 error
= xfs_btree_decrement(cur
, 0, &i
);
2294 error
= xfs_bmbt_update(cur
, &LEFT
);
2300 case BMAP_LEFT_FILLING
:
2302 * Setting the first part of a previous oldext extent to newext.
2303 * The left neighbor is not contiguous.
2306 PREV
.br_startoff
+= new->br_blockcount
;
2307 PREV
.br_startblock
+= new->br_blockcount
;
2308 PREV
.br_blockcount
-= new->br_blockcount
;
2309 xfs_iext_update_extent(ip
, state
, *idx
, &PREV
);
2311 xfs_iext_insert(ip
, *idx
, 1, new, state
);
2312 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2313 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
2315 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2317 rval
= XFS_ILOG_CORE
;
2318 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2321 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2322 error
= xfs_bmbt_update(cur
, &PREV
);
2325 cur
->bc_rec
.b
= *new;
2326 if ((error
= xfs_btree_insert(cur
, &i
)))
2328 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2332 case BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2334 * Setting the last part of a previous oldext extent to newext.
2335 * The right neighbor is contiguous with the new allocation.
2338 PREV
.br_blockcount
-= new->br_blockcount
;
2339 xfs_iext_update_extent(ip
, state
, *idx
, &PREV
);
2343 RIGHT
.br_startoff
= new->br_startoff
;
2344 RIGHT
.br_startblock
= new->br_startblock
;
2345 RIGHT
.br_blockcount
+= new->br_blockcount
;
2346 xfs_iext_update_extent(ip
, state
, *idx
, &RIGHT
);
2349 rval
= XFS_ILOG_DEXT
;
2352 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2355 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2356 error
= xfs_bmbt_update(cur
, &PREV
);
2359 error
= xfs_btree_increment(cur
, 0, &i
);
2362 error
= xfs_bmbt_update(cur
, &RIGHT
);
2368 case BMAP_RIGHT_FILLING
:
2370 * Setting the last part of a previous oldext extent to newext.
2371 * The right neighbor is not contiguous.
2374 PREV
.br_blockcount
-= new->br_blockcount
;
2375 xfs_iext_update_extent(ip
, state
, *idx
, &PREV
);
2378 xfs_iext_insert(ip
, *idx
, 1, new, state
);
2380 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2381 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
2383 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2385 rval
= XFS_ILOG_CORE
;
2386 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2389 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2390 error
= xfs_bmbt_update(cur
, &PREV
);
2393 error
= xfs_bmbt_lookup_eq(cur
, new, &i
);
2396 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
2397 if ((error
= xfs_btree_insert(cur
, &i
)))
2399 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2405 * Setting the middle part of a previous oldext extent to
2406 * newext. Contiguity is impossible here.
2407 * One extent becomes three extents.
2410 PREV
.br_blockcount
= new->br_startoff
- PREV
.br_startoff
;
2411 xfs_iext_update_extent(ip
, state
, *idx
, &PREV
);
2414 r
[1].br_startoff
= new_endoff
;
2415 r
[1].br_blockcount
=
2416 old
.br_startoff
+ old
.br_blockcount
- new_endoff
;
2417 r
[1].br_startblock
= new->br_startblock
+ new->br_blockcount
;
2418 r
[1].br_state
= PREV
.br_state
;
2421 xfs_iext_insert(ip
, *idx
, 2, &r
[0], state
);
2423 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2424 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 2);
2426 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2428 rval
= XFS_ILOG_CORE
;
2429 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2432 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2433 /* new right extent - oldext */
2434 error
= xfs_bmbt_update(cur
, &r
[1]);
2437 /* new left extent - oldext */
2438 cur
->bc_rec
.b
= PREV
;
2439 if ((error
= xfs_btree_insert(cur
, &i
)))
2441 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2443 * Reset the cursor to the position of the new extent
2444 * we are about to insert as we can't trust it after
2445 * the previous insert.
2447 error
= xfs_bmbt_lookup_eq(cur
, new, &i
);
2450 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
2451 /* new middle extent - newext */
2452 if ((error
= xfs_btree_insert(cur
, &i
)))
2454 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2458 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2459 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2460 case BMAP_LEFT_FILLING
| BMAP_RIGHT_CONTIG
:
2461 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
2462 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2463 case BMAP_LEFT_CONTIG
:
2464 case BMAP_RIGHT_CONTIG
:
2466 * These cases are all impossible.
2471 /* update reverse mappings */
2472 error
= xfs_rmap_convert_extent(mp
, dfops
, ip
, whichfork
, new);
2476 /* convert to a btree if necessary */
2477 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
2478 int tmp_logflags
; /* partial log flag return val */
2480 ASSERT(cur
== NULL
);
2481 error
= xfs_bmap_extents_to_btree(tp
, ip
, first
, dfops
, &cur
,
2482 0, &tmp_logflags
, whichfork
);
2483 *logflagsp
|= tmp_logflags
;
2488 /* clear out the allocated field, done with it now in any case. */
2490 cur
->bc_private
.b
.allocated
= 0;
2494 xfs_bmap_check_leaf_extents(*curp
, ip
, whichfork
);
2504 * Convert a hole to a delayed allocation.
2507 xfs_bmap_add_extent_hole_delay(
2508 xfs_inode_t
*ip
, /* incore inode pointer */
2510 xfs_extnum_t
*idx
, /* extent number to update/insert */
2511 xfs_bmbt_irec_t
*new) /* new data to add to file extents */
2513 xfs_ifork_t
*ifp
; /* inode fork pointer */
2514 xfs_bmbt_irec_t left
; /* left neighbor extent entry */
2515 xfs_filblks_t newlen
=0; /* new indirect size */
2516 xfs_filblks_t oldlen
=0; /* old indirect size */
2517 xfs_bmbt_irec_t right
; /* right neighbor extent entry */
2518 int state
= xfs_bmap_fork_to_state(whichfork
);
2519 xfs_filblks_t temp
; /* temp for indirect calculations */
2521 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
2522 ASSERT(isnullstartblock(new->br_startblock
));
2525 * Check and set flags if this segment has a left neighbor
2528 state
|= BMAP_LEFT_VALID
;
2529 xfs_iext_get_extent(ifp
, *idx
- 1, &left
);
2531 if (isnullstartblock(left
.br_startblock
))
2532 state
|= BMAP_LEFT_DELAY
;
2536 * Check and set flags if the current (right) segment exists.
2537 * If it doesn't exist, we're converting the hole at end-of-file.
2539 if (*idx
< xfs_iext_count(ifp
)) {
2540 state
|= BMAP_RIGHT_VALID
;
2541 xfs_iext_get_extent(ifp
, *idx
, &right
);
2543 if (isnullstartblock(right
.br_startblock
))
2544 state
|= BMAP_RIGHT_DELAY
;
2548 * Set contiguity flags on the left and right neighbors.
2549 * Don't let extents get too large, even if the pieces are contiguous.
2551 if ((state
& BMAP_LEFT_VALID
) && (state
& BMAP_LEFT_DELAY
) &&
2552 left
.br_startoff
+ left
.br_blockcount
== new->br_startoff
&&
2553 left
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
2554 state
|= BMAP_LEFT_CONTIG
;
2556 if ((state
& BMAP_RIGHT_VALID
) && (state
& BMAP_RIGHT_DELAY
) &&
2557 new->br_startoff
+ new->br_blockcount
== right
.br_startoff
&&
2558 new->br_blockcount
+ right
.br_blockcount
<= MAXEXTLEN
&&
2559 (!(state
& BMAP_LEFT_CONTIG
) ||
2560 (left
.br_blockcount
+ new->br_blockcount
+
2561 right
.br_blockcount
<= MAXEXTLEN
)))
2562 state
|= BMAP_RIGHT_CONTIG
;
2565 * Switch out based on the contiguity flags.
2567 switch (state
& (BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
)) {
2568 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2570 * New allocation is contiguous with delayed allocations
2571 * on the left and on the right.
2572 * Merge all three into a single extent record.
2575 temp
= left
.br_blockcount
+ new->br_blockcount
+
2576 right
.br_blockcount
;
2578 oldlen
= startblockval(left
.br_startblock
) +
2579 startblockval(new->br_startblock
) +
2580 startblockval(right
.br_startblock
);
2581 newlen
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
2583 left
.br_startblock
= nullstartblock(newlen
);
2584 left
.br_blockcount
= temp
;
2585 xfs_iext_update_extent(ip
, state
, *idx
, &left
);
2587 xfs_iext_remove(ip
, *idx
+ 1, 1, state
);
2590 case BMAP_LEFT_CONTIG
:
2592 * New allocation is contiguous with a delayed allocation
2594 * Merge the new allocation with the left neighbor.
2597 temp
= left
.br_blockcount
+ new->br_blockcount
;
2599 oldlen
= startblockval(left
.br_startblock
) +
2600 startblockval(new->br_startblock
);
2601 newlen
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
2603 left
.br_blockcount
= temp
;
2604 left
.br_startblock
= nullstartblock(newlen
);
2605 xfs_iext_update_extent(ip
, state
, *idx
, &left
);
2608 case BMAP_RIGHT_CONTIG
:
2610 * New allocation is contiguous with a delayed allocation
2612 * Merge the new allocation with the right neighbor.
2614 temp
= new->br_blockcount
+ right
.br_blockcount
;
2615 oldlen
= startblockval(new->br_startblock
) +
2616 startblockval(right
.br_startblock
);
2617 newlen
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
2619 right
.br_startoff
= new->br_startoff
;
2620 right
.br_startblock
= nullstartblock(newlen
);
2621 right
.br_blockcount
= temp
;
2622 xfs_iext_update_extent(ip
, state
, *idx
, &right
);
2627 * New allocation is not contiguous with another
2628 * delayed allocation.
2629 * Insert a new entry.
2631 oldlen
= newlen
= 0;
2632 xfs_iext_insert(ip
, *idx
, 1, new, state
);
2635 if (oldlen
!= newlen
) {
2636 ASSERT(oldlen
> newlen
);
2637 xfs_mod_fdblocks(ip
->i_mount
, (int64_t)(oldlen
- newlen
),
2640 * Nothing to do for disk quota accounting here.
2646 * Convert a hole to a real allocation.
2648 STATIC
int /* error */
2649 xfs_bmap_add_extent_hole_real(
2650 struct xfs_trans
*tp
,
2651 struct xfs_inode
*ip
,
2654 struct xfs_btree_cur
**curp
,
2655 struct xfs_bmbt_irec
*new,
2656 xfs_fsblock_t
*first
,
2657 struct xfs_defer_ops
*dfops
,
2660 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
2661 struct xfs_mount
*mp
= ip
->i_mount
;
2662 struct xfs_btree_cur
*cur
= *curp
;
2663 int error
; /* error return value */
2664 int i
; /* temp state */
2665 xfs_bmbt_irec_t left
; /* left neighbor extent entry */
2666 xfs_bmbt_irec_t right
; /* right neighbor extent entry */
2667 int rval
=0; /* return value (logging flags) */
2668 int state
= xfs_bmap_fork_to_state(whichfork
);
2669 struct xfs_bmbt_irec old
;
2672 ASSERT(*idx
<= xfs_iext_count(ifp
));
2673 ASSERT(!isnullstartblock(new->br_startblock
));
2674 ASSERT(!cur
|| !(cur
->bc_private
.b
.flags
& XFS_BTCUR_BPRV_WASDEL
));
2676 XFS_STATS_INC(mp
, xs_add_exlist
);
2679 * Check and set flags if this segment has a left neighbor.
2682 state
|= BMAP_LEFT_VALID
;
2683 xfs_iext_get_extent(ifp
, *idx
- 1, &left
);
2684 if (isnullstartblock(left
.br_startblock
))
2685 state
|= BMAP_LEFT_DELAY
;
2689 * Check and set flags if this segment has a current value.
2690 * Not true if we're inserting into the "hole" at eof.
2692 if (*idx
< xfs_iext_count(ifp
)) {
2693 state
|= BMAP_RIGHT_VALID
;
2694 xfs_iext_get_extent(ifp
, *idx
, &right
);
2695 if (isnullstartblock(right
.br_startblock
))
2696 state
|= BMAP_RIGHT_DELAY
;
2700 * We're inserting a real allocation between "left" and "right".
2701 * Set the contiguity flags. Don't let extents get too large.
2703 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
2704 left
.br_startoff
+ left
.br_blockcount
== new->br_startoff
&&
2705 left
.br_startblock
+ left
.br_blockcount
== new->br_startblock
&&
2706 left
.br_state
== new->br_state
&&
2707 left
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
2708 state
|= BMAP_LEFT_CONTIG
;
2710 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
2711 new->br_startoff
+ new->br_blockcount
== right
.br_startoff
&&
2712 new->br_startblock
+ new->br_blockcount
== right
.br_startblock
&&
2713 new->br_state
== right
.br_state
&&
2714 new->br_blockcount
+ right
.br_blockcount
<= MAXEXTLEN
&&
2715 (!(state
& BMAP_LEFT_CONTIG
) ||
2716 left
.br_blockcount
+ new->br_blockcount
+
2717 right
.br_blockcount
<= MAXEXTLEN
))
2718 state
|= BMAP_RIGHT_CONTIG
;
2722 * Select which case we're in here, and implement it.
2724 switch (state
& (BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
)) {
2725 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2727 * New allocation is contiguous with real allocations on the
2728 * left and on the right.
2729 * Merge all three into a single extent record.
2732 left
.br_blockcount
+= new->br_blockcount
+ right
.br_blockcount
;
2733 xfs_iext_update_extent(ip
, state
, *idx
, &left
);
2735 xfs_iext_remove(ip
, *idx
+ 1, 1, state
);
2737 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2738 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
2740 rval
= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
2742 rval
= XFS_ILOG_CORE
;
2743 error
= xfs_bmbt_lookup_eq(cur
, &right
, &i
);
2746 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2747 error
= xfs_btree_delete(cur
, &i
);
2750 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2751 error
= xfs_btree_decrement(cur
, 0, &i
);
2754 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2755 error
= xfs_bmbt_update(cur
, &left
);
2761 case BMAP_LEFT_CONTIG
:
2763 * New allocation is contiguous with a real allocation
2765 * Merge the new allocation with the left neighbor.
2770 left
.br_blockcount
+= new->br_blockcount
;
2771 xfs_iext_update_extent(ip
, state
, *idx
, &left
);
2774 rval
= xfs_ilog_fext(whichfork
);
2777 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2780 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2781 error
= xfs_bmbt_update(cur
, &left
);
2787 case BMAP_RIGHT_CONTIG
:
2789 * New allocation is contiguous with a real allocation
2791 * Merge the new allocation with the right neighbor.
2795 right
.br_startoff
= new->br_startoff
;
2796 right
.br_startblock
= new->br_startblock
;
2797 right
.br_blockcount
+= new->br_blockcount
;
2798 xfs_iext_update_extent(ip
, state
, *idx
, &right
);
2801 rval
= xfs_ilog_fext(whichfork
);
2804 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2807 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2808 error
= xfs_bmbt_update(cur
, &right
);
2816 * New allocation is not contiguous with another
2818 * Insert a new entry.
2820 xfs_iext_insert(ip
, *idx
, 1, new, state
);
2821 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2822 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
2824 rval
= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
2826 rval
= XFS_ILOG_CORE
;
2827 error
= xfs_bmbt_lookup_eq(cur
, new, &i
);
2830 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
2831 error
= xfs_btree_insert(cur
, &i
);
2834 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2839 /* add reverse mapping */
2840 error
= xfs_rmap_map_extent(mp
, dfops
, ip
, whichfork
, new);
2844 /* convert to a btree if necessary */
2845 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
2846 int tmp_logflags
; /* partial log flag return val */
2848 ASSERT(cur
== NULL
);
2849 error
= xfs_bmap_extents_to_btree(tp
, ip
, first
, dfops
, curp
,
2850 0, &tmp_logflags
, whichfork
);
2851 *logflagsp
|= tmp_logflags
;
2857 /* clear out the allocated field, done with it now in any case. */
2859 cur
->bc_private
.b
.allocated
= 0;
2861 xfs_bmap_check_leaf_extents(cur
, ip
, whichfork
);
2868 * Functions used in the extent read, allocate and remove paths
2872 * Adjust the size of the new extent based on di_extsize and rt extsize.
2875 xfs_bmap_extsize_align(
2877 xfs_bmbt_irec_t
*gotp
, /* next extent pointer */
2878 xfs_bmbt_irec_t
*prevp
, /* previous extent pointer */
2879 xfs_extlen_t extsz
, /* align to this extent size */
2880 int rt
, /* is this a realtime inode? */
2881 int eof
, /* is extent at end-of-file? */
2882 int delay
, /* creating delalloc extent? */
2883 int convert
, /* overwriting unwritten extent? */
2884 xfs_fileoff_t
*offp
, /* in/out: aligned offset */
2885 xfs_extlen_t
*lenp
) /* in/out: aligned length */
2887 xfs_fileoff_t orig_off
; /* original offset */
2888 xfs_extlen_t orig_alen
; /* original length */
2889 xfs_fileoff_t orig_end
; /* original off+len */
2890 xfs_fileoff_t nexto
; /* next file offset */
2891 xfs_fileoff_t prevo
; /* previous file offset */
2892 xfs_fileoff_t align_off
; /* temp for offset */
2893 xfs_extlen_t align_alen
; /* temp for length */
2894 xfs_extlen_t temp
; /* temp for calculations */
2899 orig_off
= align_off
= *offp
;
2900 orig_alen
= align_alen
= *lenp
;
2901 orig_end
= orig_off
+ orig_alen
;
2904 * If this request overlaps an existing extent, then don't
2905 * attempt to perform any additional alignment.
2907 if (!delay
&& !eof
&&
2908 (orig_off
>= gotp
->br_startoff
) &&
2909 (orig_end
<= gotp
->br_startoff
+ gotp
->br_blockcount
)) {
2914 * If the file offset is unaligned vs. the extent size
2915 * we need to align it. This will be possible unless
2916 * the file was previously written with a kernel that didn't
2917 * perform this alignment, or if a truncate shot us in the
2920 temp
= do_mod(orig_off
, extsz
);
2926 /* Same adjustment for the end of the requested area. */
2927 temp
= (align_alen
% extsz
);
2929 align_alen
+= extsz
- temp
;
2932 * For large extent hint sizes, the aligned extent might be larger than
2933 * MAXEXTLEN. In that case, reduce the size by an extsz so that it pulls
2934 * the length back under MAXEXTLEN. The outer allocation loops handle
2935 * short allocation just fine, so it is safe to do this. We only want to
2936 * do it when we are forced to, though, because it means more allocation
2937 * operations are required.
2939 while (align_alen
> MAXEXTLEN
)
2940 align_alen
-= extsz
;
2941 ASSERT(align_alen
<= MAXEXTLEN
);
2944 * If the previous block overlaps with this proposed allocation
2945 * then move the start forward without adjusting the length.
2947 if (prevp
->br_startoff
!= NULLFILEOFF
) {
2948 if (prevp
->br_startblock
== HOLESTARTBLOCK
)
2949 prevo
= prevp
->br_startoff
;
2951 prevo
= prevp
->br_startoff
+ prevp
->br_blockcount
;
2954 if (align_off
!= orig_off
&& align_off
< prevo
)
2957 * If the next block overlaps with this proposed allocation
2958 * then move the start back without adjusting the length,
2959 * but not before offset 0.
2960 * This may of course make the start overlap previous block,
2961 * and if we hit the offset 0 limit then the next block
2962 * can still overlap too.
2964 if (!eof
&& gotp
->br_startoff
!= NULLFILEOFF
) {
2965 if ((delay
&& gotp
->br_startblock
== HOLESTARTBLOCK
) ||
2966 (!delay
&& gotp
->br_startblock
== DELAYSTARTBLOCK
))
2967 nexto
= gotp
->br_startoff
+ gotp
->br_blockcount
;
2969 nexto
= gotp
->br_startoff
;
2971 nexto
= NULLFILEOFF
;
2973 align_off
+ align_alen
!= orig_end
&&
2974 align_off
+ align_alen
> nexto
)
2975 align_off
= nexto
> align_alen
? nexto
- align_alen
: 0;
2977 * If we're now overlapping the next or previous extent that
2978 * means we can't fit an extsz piece in this hole. Just move
2979 * the start forward to the first valid spot and set
2980 * the length so we hit the end.
2982 if (align_off
!= orig_off
&& align_off
< prevo
)
2984 if (align_off
+ align_alen
!= orig_end
&&
2985 align_off
+ align_alen
> nexto
&&
2986 nexto
!= NULLFILEOFF
) {
2987 ASSERT(nexto
> prevo
);
2988 align_alen
= nexto
- align_off
;
2992 * If realtime, and the result isn't a multiple of the realtime
2993 * extent size we need to remove blocks until it is.
2995 if (rt
&& (temp
= (align_alen
% mp
->m_sb
.sb_rextsize
))) {
2997 * We're not covering the original request, or
2998 * we won't be able to once we fix the length.
3000 if (orig_off
< align_off
||
3001 orig_end
> align_off
+ align_alen
||
3002 align_alen
- temp
< orig_alen
)
3005 * Try to fix it by moving the start up.
3007 if (align_off
+ temp
<= orig_off
) {
3012 * Try to fix it by moving the end in.
3014 else if (align_off
+ align_alen
- temp
>= orig_end
)
3017 * Set the start to the minimum then trim the length.
3020 align_alen
-= orig_off
- align_off
;
3021 align_off
= orig_off
;
3022 align_alen
-= align_alen
% mp
->m_sb
.sb_rextsize
;
3025 * Result doesn't cover the request, fail it.
3027 if (orig_off
< align_off
|| orig_end
> align_off
+ align_alen
)
3030 ASSERT(orig_off
>= align_off
);
3031 /* see MAXEXTLEN handling above */
3032 ASSERT(orig_end
<= align_off
+ align_alen
||
3033 align_alen
+ extsz
> MAXEXTLEN
);
3037 if (!eof
&& gotp
->br_startoff
!= NULLFILEOFF
)
3038 ASSERT(align_off
+ align_alen
<= gotp
->br_startoff
);
3039 if (prevp
->br_startoff
!= NULLFILEOFF
)
3040 ASSERT(align_off
>= prevp
->br_startoff
+ prevp
->br_blockcount
);
3048 #define XFS_ALLOC_GAP_UNITS 4
3052 struct xfs_bmalloca
*ap
) /* bmap alloc argument struct */
3054 xfs_fsblock_t adjust
; /* adjustment to block numbers */
3055 xfs_agnumber_t fb_agno
; /* ag number of ap->firstblock */
3056 xfs_mount_t
*mp
; /* mount point structure */
3057 int nullfb
; /* true if ap->firstblock isn't set */
3058 int rt
; /* true if inode is realtime */
3060 #define ISVALID(x,y) \
3062 (x) < mp->m_sb.sb_rblocks : \
3063 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
3064 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
3065 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
3067 mp
= ap
->ip
->i_mount
;
3068 nullfb
= *ap
->firstblock
== NULLFSBLOCK
;
3069 rt
= XFS_IS_REALTIME_INODE(ap
->ip
) &&
3070 xfs_alloc_is_userdata(ap
->datatype
);
3071 fb_agno
= nullfb
? NULLAGNUMBER
: XFS_FSB_TO_AGNO(mp
, *ap
->firstblock
);
3073 * If allocating at eof, and there's a previous real block,
3074 * try to use its last block as our starting point.
3076 if (ap
->eof
&& ap
->prev
.br_startoff
!= NULLFILEOFF
&&
3077 !isnullstartblock(ap
->prev
.br_startblock
) &&
3078 ISVALID(ap
->prev
.br_startblock
+ ap
->prev
.br_blockcount
,
3079 ap
->prev
.br_startblock
)) {
3080 ap
->blkno
= ap
->prev
.br_startblock
+ ap
->prev
.br_blockcount
;
3082 * Adjust for the gap between prevp and us.
3084 adjust
= ap
->offset
-
3085 (ap
->prev
.br_startoff
+ ap
->prev
.br_blockcount
);
3087 ISVALID(ap
->blkno
+ adjust
, ap
->prev
.br_startblock
))
3088 ap
->blkno
+= adjust
;
3091 * If not at eof, then compare the two neighbor blocks.
3092 * Figure out whether either one gives us a good starting point,
3093 * and pick the better one.
3095 else if (!ap
->eof
) {
3096 xfs_fsblock_t gotbno
; /* right side block number */
3097 xfs_fsblock_t gotdiff
=0; /* right side difference */
3098 xfs_fsblock_t prevbno
; /* left side block number */
3099 xfs_fsblock_t prevdiff
=0; /* left side difference */
3102 * If there's a previous (left) block, select a requested
3103 * start block based on it.
3105 if (ap
->prev
.br_startoff
!= NULLFILEOFF
&&
3106 !isnullstartblock(ap
->prev
.br_startblock
) &&
3107 (prevbno
= ap
->prev
.br_startblock
+
3108 ap
->prev
.br_blockcount
) &&
3109 ISVALID(prevbno
, ap
->prev
.br_startblock
)) {
3111 * Calculate gap to end of previous block.
3113 adjust
= prevdiff
= ap
->offset
-
3114 (ap
->prev
.br_startoff
+
3115 ap
->prev
.br_blockcount
);
3117 * Figure the startblock based on the previous block's
3118 * end and the gap size.
3120 * If the gap is large relative to the piece we're
3121 * allocating, or using it gives us an invalid block
3122 * number, then just use the end of the previous block.
3124 if (prevdiff
<= XFS_ALLOC_GAP_UNITS
* ap
->length
&&
3125 ISVALID(prevbno
+ prevdiff
,
3126 ap
->prev
.br_startblock
))
3131 * If the firstblock forbids it, can't use it,
3134 if (!rt
&& !nullfb
&&
3135 XFS_FSB_TO_AGNO(mp
, prevbno
) != fb_agno
)
3136 prevbno
= NULLFSBLOCK
;
3139 * No previous block or can't follow it, just default.
3142 prevbno
= NULLFSBLOCK
;
3144 * If there's a following (right) block, select a requested
3145 * start block based on it.
3147 if (!isnullstartblock(ap
->got
.br_startblock
)) {
3149 * Calculate gap to start of next block.
3151 adjust
= gotdiff
= ap
->got
.br_startoff
- ap
->offset
;
3153 * Figure the startblock based on the next block's
3154 * start and the gap size.
3156 gotbno
= ap
->got
.br_startblock
;
3159 * If the gap is large relative to the piece we're
3160 * allocating, or using it gives us an invalid block
3161 * number, then just use the start of the next block
3162 * offset by our length.
3164 if (gotdiff
<= XFS_ALLOC_GAP_UNITS
* ap
->length
&&
3165 ISVALID(gotbno
- gotdiff
, gotbno
))
3167 else if (ISVALID(gotbno
- ap
->length
, gotbno
)) {
3168 gotbno
-= ap
->length
;
3169 gotdiff
+= adjust
- ap
->length
;
3173 * If the firstblock forbids it, can't use it,
3176 if (!rt
&& !nullfb
&&
3177 XFS_FSB_TO_AGNO(mp
, gotbno
) != fb_agno
)
3178 gotbno
= NULLFSBLOCK
;
3181 * No next block, just default.
3184 gotbno
= NULLFSBLOCK
;
3186 * If both valid, pick the better one, else the only good
3187 * one, else ap->blkno is already set (to 0 or the inode block).
3189 if (prevbno
!= NULLFSBLOCK
&& gotbno
!= NULLFSBLOCK
)
3190 ap
->blkno
= prevdiff
<= gotdiff
? prevbno
: gotbno
;
3191 else if (prevbno
!= NULLFSBLOCK
)
3192 ap
->blkno
= prevbno
;
3193 else if (gotbno
!= NULLFSBLOCK
)
3200 xfs_bmap_longest_free_extent(
3201 struct xfs_trans
*tp
,
3206 struct xfs_mount
*mp
= tp
->t_mountp
;
3207 struct xfs_perag
*pag
;
3208 xfs_extlen_t longest
;
3211 pag
= xfs_perag_get(mp
, ag
);
3212 if (!pag
->pagf_init
) {
3213 error
= xfs_alloc_pagf_init(mp
, tp
, ag
, XFS_ALLOC_FLAG_TRYLOCK
);
3217 if (!pag
->pagf_init
) {
3223 longest
= xfs_alloc_longest_free_extent(mp
, pag
,
3224 xfs_alloc_min_freelist(mp
, pag
),
3225 xfs_ag_resv_needed(pag
, XFS_AG_RESV_NONE
));
3226 if (*blen
< longest
)
3235 xfs_bmap_select_minlen(
3236 struct xfs_bmalloca
*ap
,
3237 struct xfs_alloc_arg
*args
,
3241 if (notinit
|| *blen
< ap
->minlen
) {
3243 * Since we did a BUF_TRYLOCK above, it is possible that
3244 * there is space for this request.
3246 args
->minlen
= ap
->minlen
;
3247 } else if (*blen
< args
->maxlen
) {
3249 * If the best seen length is less than the request length,
3250 * use the best as the minimum.
3252 args
->minlen
= *blen
;
3255 * Otherwise we've seen an extent as big as maxlen, use that
3258 args
->minlen
= args
->maxlen
;
3263 xfs_bmap_btalloc_nullfb(
3264 struct xfs_bmalloca
*ap
,
3265 struct xfs_alloc_arg
*args
,
3268 struct xfs_mount
*mp
= ap
->ip
->i_mount
;
3269 xfs_agnumber_t ag
, startag
;
3273 args
->type
= XFS_ALLOCTYPE_START_BNO
;
3274 args
->total
= ap
->total
;
3276 startag
= ag
= XFS_FSB_TO_AGNO(mp
, args
->fsbno
);
3277 if (startag
== NULLAGNUMBER
)
3280 while (*blen
< args
->maxlen
) {
3281 error
= xfs_bmap_longest_free_extent(args
->tp
, ag
, blen
,
3286 if (++ag
== mp
->m_sb
.sb_agcount
)
3292 xfs_bmap_select_minlen(ap
, args
, blen
, notinit
);
3297 xfs_bmap_btalloc_filestreams(
3298 struct xfs_bmalloca
*ap
,
3299 struct xfs_alloc_arg
*args
,
3302 struct xfs_mount
*mp
= ap
->ip
->i_mount
;
3307 args
->type
= XFS_ALLOCTYPE_NEAR_BNO
;
3308 args
->total
= ap
->total
;
3310 ag
= XFS_FSB_TO_AGNO(mp
, args
->fsbno
);
3311 if (ag
== NULLAGNUMBER
)
3314 error
= xfs_bmap_longest_free_extent(args
->tp
, ag
, blen
, ¬init
);
3318 if (*blen
< args
->maxlen
) {
3319 error
= xfs_filestream_new_ag(ap
, &ag
);
3323 error
= xfs_bmap_longest_free_extent(args
->tp
, ag
, blen
,
3330 xfs_bmap_select_minlen(ap
, args
, blen
, notinit
);
3333 * Set the failure fallback case to look in the selected AG as stream
3336 ap
->blkno
= args
->fsbno
= XFS_AGB_TO_FSB(mp
, ag
, 0);
3342 struct xfs_bmalloca
*ap
) /* bmap alloc argument struct */
3344 xfs_mount_t
*mp
; /* mount point structure */
3345 xfs_alloctype_t atype
= 0; /* type for allocation routines */
3346 xfs_extlen_t align
= 0; /* minimum allocation alignment */
3347 xfs_agnumber_t fb_agno
; /* ag number of ap->firstblock */
3349 xfs_alloc_arg_t args
;
3351 xfs_extlen_t nextminlen
= 0;
3352 int nullfb
; /* true if ap->firstblock isn't set */
3360 mp
= ap
->ip
->i_mount
;
3362 /* stripe alignment for allocation is determined by mount parameters */
3364 if (mp
->m_swidth
&& (mp
->m_flags
& XFS_MOUNT_SWALLOC
))
3365 stripe_align
= mp
->m_swidth
;
3366 else if (mp
->m_dalign
)
3367 stripe_align
= mp
->m_dalign
;
3369 if (ap
->flags
& XFS_BMAPI_COWFORK
)
3370 align
= xfs_get_cowextsz_hint(ap
->ip
);
3371 else if (xfs_alloc_is_userdata(ap
->datatype
))
3372 align
= xfs_get_extsz_hint(ap
->ip
);
3374 error
= xfs_bmap_extsize_align(mp
, &ap
->got
, &ap
->prev
,
3375 align
, 0, ap
->eof
, 0, ap
->conv
,
3376 &ap
->offset
, &ap
->length
);
3382 nullfb
= *ap
->firstblock
== NULLFSBLOCK
;
3383 fb_agno
= nullfb
? NULLAGNUMBER
: XFS_FSB_TO_AGNO(mp
, *ap
->firstblock
);
3385 if (xfs_alloc_is_userdata(ap
->datatype
) &&
3386 xfs_inode_is_filestream(ap
->ip
)) {
3387 ag
= xfs_filestream_lookup_ag(ap
->ip
);
3388 ag
= (ag
!= NULLAGNUMBER
) ? ag
: 0;
3389 ap
->blkno
= XFS_AGB_TO_FSB(mp
, ag
, 0);
3391 ap
->blkno
= XFS_INO_TO_FSB(mp
, ap
->ip
->i_ino
);
3394 ap
->blkno
= *ap
->firstblock
;
3396 xfs_bmap_adjacent(ap
);
3399 * If allowed, use ap->blkno; otherwise must use firstblock since
3400 * it's in the right allocation group.
3402 if (nullfb
|| XFS_FSB_TO_AGNO(mp
, ap
->blkno
) == fb_agno
)
3405 ap
->blkno
= *ap
->firstblock
;
3407 * Normal allocation, done through xfs_alloc_vextent.
3409 tryagain
= isaligned
= 0;
3410 memset(&args
, 0, sizeof(args
));
3413 args
.fsbno
= ap
->blkno
;
3414 xfs_rmap_skip_owner_update(&args
.oinfo
);
3416 /* Trim the allocation back to the maximum an AG can fit. */
3417 args
.maxlen
= MIN(ap
->length
, mp
->m_ag_max_usable
);
3418 args
.firstblock
= *ap
->firstblock
;
3422 * Search for an allocation group with a single extent large
3423 * enough for the request. If one isn't found, then adjust
3424 * the minimum allocation size to the largest space found.
3426 if (xfs_alloc_is_userdata(ap
->datatype
) &&
3427 xfs_inode_is_filestream(ap
->ip
))
3428 error
= xfs_bmap_btalloc_filestreams(ap
, &args
, &blen
);
3430 error
= xfs_bmap_btalloc_nullfb(ap
, &args
, &blen
);
3433 } else if (ap
->dfops
->dop_low
) {
3434 if (xfs_inode_is_filestream(ap
->ip
))
3435 args
.type
= XFS_ALLOCTYPE_FIRST_AG
;
3437 args
.type
= XFS_ALLOCTYPE_START_BNO
;
3438 args
.total
= args
.minlen
= ap
->minlen
;
3440 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
3441 args
.total
= ap
->total
;
3442 args
.minlen
= ap
->minlen
;
3444 /* apply extent size hints if obtained earlier */
3447 if ((args
.mod
= (xfs_extlen_t
)do_mod(ap
->offset
, args
.prod
)))
3448 args
.mod
= (xfs_extlen_t
)(args
.prod
- args
.mod
);
3449 } else if (mp
->m_sb
.sb_blocksize
>= PAGE_SIZE
) {
3453 args
.prod
= PAGE_SIZE
>> mp
->m_sb
.sb_blocklog
;
3454 if ((args
.mod
= (xfs_extlen_t
)(do_mod(ap
->offset
, args
.prod
))))
3455 args
.mod
= (xfs_extlen_t
)(args
.prod
- args
.mod
);
3458 * If we are not low on available data blocks, and the
3459 * underlying logical volume manager is a stripe, and
3460 * the file offset is zero then try to allocate data
3461 * blocks on stripe unit boundary.
3462 * NOTE: ap->aeof is only set if the allocation length
3463 * is >= the stripe unit and the allocation offset is
3464 * at the end of file.
3466 if (!ap
->dfops
->dop_low
&& ap
->aeof
) {
3468 args
.alignment
= stripe_align
;
3472 * Adjust for alignment
3474 if (blen
> args
.alignment
&& blen
<= args
.maxlen
)
3475 args
.minlen
= blen
- args
.alignment
;
3476 args
.minalignslop
= 0;
3479 * First try an exact bno allocation.
3480 * If it fails then do a near or start bno
3481 * allocation with alignment turned on.
3485 args
.type
= XFS_ALLOCTYPE_THIS_BNO
;
3488 * Compute the minlen+alignment for the
3489 * next case. Set slop so that the value
3490 * of minlen+alignment+slop doesn't go up
3491 * between the calls.
3493 if (blen
> stripe_align
&& blen
<= args
.maxlen
)
3494 nextminlen
= blen
- stripe_align
;
3496 nextminlen
= args
.minlen
;
3497 if (nextminlen
+ stripe_align
> args
.minlen
+ 1)
3499 nextminlen
+ stripe_align
-
3502 args
.minalignslop
= 0;
3506 args
.minalignslop
= 0;
3508 args
.minleft
= ap
->minleft
;
3509 args
.wasdel
= ap
->wasdel
;
3510 args
.resv
= XFS_AG_RESV_NONE
;
3511 args
.datatype
= ap
->datatype
;
3512 if (ap
->datatype
& XFS_ALLOC_USERDATA_ZERO
)
3515 error
= xfs_alloc_vextent(&args
);
3519 if (tryagain
&& args
.fsbno
== NULLFSBLOCK
) {
3521 * Exact allocation failed. Now try with alignment
3525 args
.fsbno
= ap
->blkno
;
3526 args
.alignment
= stripe_align
;
3527 args
.minlen
= nextminlen
;
3528 args
.minalignslop
= 0;
3530 if ((error
= xfs_alloc_vextent(&args
)))
3533 if (isaligned
&& args
.fsbno
== NULLFSBLOCK
) {
3535 * allocation failed, so turn off alignment and
3539 args
.fsbno
= ap
->blkno
;
3541 if ((error
= xfs_alloc_vextent(&args
)))
3544 if (args
.fsbno
== NULLFSBLOCK
&& nullfb
&&
3545 args
.minlen
> ap
->minlen
) {
3546 args
.minlen
= ap
->minlen
;
3547 args
.type
= XFS_ALLOCTYPE_START_BNO
;
3548 args
.fsbno
= ap
->blkno
;
3549 if ((error
= xfs_alloc_vextent(&args
)))
3552 if (args
.fsbno
== NULLFSBLOCK
&& nullfb
) {
3554 args
.type
= XFS_ALLOCTYPE_FIRST_AG
;
3555 args
.total
= ap
->minlen
;
3556 if ((error
= xfs_alloc_vextent(&args
)))
3558 ap
->dfops
->dop_low
= true;
3560 if (args
.fsbno
!= NULLFSBLOCK
) {
3562 * check the allocation happened at the same or higher AG than
3563 * the first block that was allocated.
3565 ASSERT(*ap
->firstblock
== NULLFSBLOCK
||
3566 XFS_FSB_TO_AGNO(mp
, *ap
->firstblock
) <=
3567 XFS_FSB_TO_AGNO(mp
, args
.fsbno
));
3569 ap
->blkno
= args
.fsbno
;
3570 if (*ap
->firstblock
== NULLFSBLOCK
)
3571 *ap
->firstblock
= args
.fsbno
;
3572 ASSERT(nullfb
|| fb_agno
<= args
.agno
);
3573 ap
->length
= args
.len
;
3574 if (!(ap
->flags
& XFS_BMAPI_COWFORK
))
3575 ap
->ip
->i_d
.di_nblocks
+= args
.len
;
3576 xfs_trans_log_inode(ap
->tp
, ap
->ip
, XFS_ILOG_CORE
);
3578 ap
->ip
->i_delayed_blks
-= args
.len
;
3580 * Adjust the disk quota also. This was reserved
3583 xfs_trans_mod_dquot_byino(ap
->tp
, ap
->ip
,
3584 ap
->wasdel
? XFS_TRANS_DQ_DELBCOUNT
:
3585 XFS_TRANS_DQ_BCOUNT
,
3588 ap
->blkno
= NULLFSBLOCK
;
3595 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
3596 * It figures out where to ask the underlying allocator to put the new extent.
3600 struct xfs_bmalloca
*ap
) /* bmap alloc argument struct */
3602 if (XFS_IS_REALTIME_INODE(ap
->ip
) &&
3603 xfs_alloc_is_userdata(ap
->datatype
))
3604 return xfs_bmap_rtalloc(ap
);
3605 return xfs_bmap_btalloc(ap
);
3608 /* Trim extent to fit a logical block range. */
3611 struct xfs_bmbt_irec
*irec
,
3615 xfs_fileoff_t distance
;
3616 xfs_fileoff_t end
= bno
+ len
;
3618 if (irec
->br_startoff
+ irec
->br_blockcount
<= bno
||
3619 irec
->br_startoff
>= end
) {
3620 irec
->br_blockcount
= 0;
3624 if (irec
->br_startoff
< bno
) {
3625 distance
= bno
- irec
->br_startoff
;
3626 if (isnullstartblock(irec
->br_startblock
))
3627 irec
->br_startblock
= DELAYSTARTBLOCK
;
3628 if (irec
->br_startblock
!= DELAYSTARTBLOCK
&&
3629 irec
->br_startblock
!= HOLESTARTBLOCK
)
3630 irec
->br_startblock
+= distance
;
3631 irec
->br_startoff
+= distance
;
3632 irec
->br_blockcount
-= distance
;
3635 if (end
< irec
->br_startoff
+ irec
->br_blockcount
) {
3636 distance
= irec
->br_startoff
+ irec
->br_blockcount
- end
;
3637 irec
->br_blockcount
-= distance
;
3641 /* trim extent to within eof */
3643 xfs_trim_extent_eof(
3644 struct xfs_bmbt_irec
*irec
,
3645 struct xfs_inode
*ip
)
3648 xfs_trim_extent(irec
, 0, XFS_B_TO_FSB(ip
->i_mount
,
3649 i_size_read(VFS_I(ip
))));
3653 * Trim the returned map to the required bounds
3657 struct xfs_bmbt_irec
*mval
,
3658 struct xfs_bmbt_irec
*got
,
3666 if ((flags
& XFS_BMAPI_ENTIRE
) ||
3667 got
->br_startoff
+ got
->br_blockcount
<= obno
) {
3669 if (isnullstartblock(got
->br_startblock
))
3670 mval
->br_startblock
= DELAYSTARTBLOCK
;
3676 ASSERT((*bno
>= obno
) || (n
== 0));
3678 mval
->br_startoff
= *bno
;
3679 if (isnullstartblock(got
->br_startblock
))
3680 mval
->br_startblock
= DELAYSTARTBLOCK
;
3682 mval
->br_startblock
= got
->br_startblock
+
3683 (*bno
- got
->br_startoff
);
3685 * Return the minimum of what we got and what we asked for for
3686 * the length. We can use the len variable here because it is
3687 * modified below and we could have been there before coming
3688 * here if the first part of the allocation didn't overlap what
3691 mval
->br_blockcount
= XFS_FILBLKS_MIN(end
- *bno
,
3692 got
->br_blockcount
- (*bno
- got
->br_startoff
));
3693 mval
->br_state
= got
->br_state
;
3694 ASSERT(mval
->br_blockcount
<= len
);
3699 * Update and validate the extent map to return
3702 xfs_bmapi_update_map(
3703 struct xfs_bmbt_irec
**map
,
3711 xfs_bmbt_irec_t
*mval
= *map
;
3713 ASSERT((flags
& XFS_BMAPI_ENTIRE
) ||
3714 ((mval
->br_startoff
+ mval
->br_blockcount
) <= end
));
3715 ASSERT((flags
& XFS_BMAPI_ENTIRE
) || (mval
->br_blockcount
<= *len
) ||
3716 (mval
->br_startoff
< obno
));
3718 *bno
= mval
->br_startoff
+ mval
->br_blockcount
;
3720 if (*n
> 0 && mval
->br_startoff
== mval
[-1].br_startoff
) {
3721 /* update previous map with new information */
3722 ASSERT(mval
->br_startblock
== mval
[-1].br_startblock
);
3723 ASSERT(mval
->br_blockcount
> mval
[-1].br_blockcount
);
3724 ASSERT(mval
->br_state
== mval
[-1].br_state
);
3725 mval
[-1].br_blockcount
= mval
->br_blockcount
;
3726 mval
[-1].br_state
= mval
->br_state
;
3727 } else if (*n
> 0 && mval
->br_startblock
!= DELAYSTARTBLOCK
&&
3728 mval
[-1].br_startblock
!= DELAYSTARTBLOCK
&&
3729 mval
[-1].br_startblock
!= HOLESTARTBLOCK
&&
3730 mval
->br_startblock
== mval
[-1].br_startblock
+
3731 mval
[-1].br_blockcount
&&
3732 ((flags
& XFS_BMAPI_IGSTATE
) ||
3733 mval
[-1].br_state
== mval
->br_state
)) {
3734 ASSERT(mval
->br_startoff
==
3735 mval
[-1].br_startoff
+ mval
[-1].br_blockcount
);
3736 mval
[-1].br_blockcount
+= mval
->br_blockcount
;
3737 } else if (*n
> 0 &&
3738 mval
->br_startblock
== DELAYSTARTBLOCK
&&
3739 mval
[-1].br_startblock
== DELAYSTARTBLOCK
&&
3740 mval
->br_startoff
==
3741 mval
[-1].br_startoff
+ mval
[-1].br_blockcount
) {
3742 mval
[-1].br_blockcount
+= mval
->br_blockcount
;
3743 mval
[-1].br_state
= mval
->br_state
;
3744 } else if (!((*n
== 0) &&
3745 ((mval
->br_startoff
+ mval
->br_blockcount
) <=
3754 * Map file blocks to filesystem blocks without allocation.
3758 struct xfs_inode
*ip
,
3761 struct xfs_bmbt_irec
*mval
,
3765 struct xfs_mount
*mp
= ip
->i_mount
;
3766 struct xfs_ifork
*ifp
;
3767 struct xfs_bmbt_irec got
;
3774 int whichfork
= xfs_bmapi_whichfork(flags
);
3777 ASSERT(!(flags
& ~(XFS_BMAPI_ATTRFORK
|XFS_BMAPI_ENTIRE
|
3778 XFS_BMAPI_IGSTATE
|XFS_BMAPI_COWFORK
)));
3779 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_SHARED
|XFS_ILOCK_EXCL
));
3781 if (unlikely(XFS_TEST_ERROR(
3782 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
3783 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
3784 mp
, XFS_ERRTAG_BMAPIFORMAT
))) {
3785 XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW
, mp
);
3786 return -EFSCORRUPTED
;
3789 if (XFS_FORCED_SHUTDOWN(mp
))
3792 XFS_STATS_INC(mp
, xs_blk_mapr
);
3794 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
3796 /* No CoW fork? Return a hole. */
3797 if (whichfork
== XFS_COW_FORK
&& !ifp
) {
3798 mval
->br_startoff
= bno
;
3799 mval
->br_startblock
= HOLESTARTBLOCK
;
3800 mval
->br_blockcount
= len
;
3801 mval
->br_state
= XFS_EXT_NORM
;
3806 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
3807 error
= xfs_iread_extents(NULL
, ip
, whichfork
);
3812 if (!xfs_iext_lookup_extent(ip
, ifp
, bno
, &idx
, &got
))
3817 while (bno
< end
&& n
< *nmap
) {
3818 /* Reading past eof, act as though there's a hole up to end. */
3820 got
.br_startoff
= end
;
3821 if (got
.br_startoff
> bno
) {
3822 /* Reading in a hole. */
3823 mval
->br_startoff
= bno
;
3824 mval
->br_startblock
= HOLESTARTBLOCK
;
3825 mval
->br_blockcount
=
3826 XFS_FILBLKS_MIN(len
, got
.br_startoff
- bno
);
3827 mval
->br_state
= XFS_EXT_NORM
;
3828 bno
+= mval
->br_blockcount
;
3829 len
-= mval
->br_blockcount
;
3835 /* set up the extent map to return. */
3836 xfs_bmapi_trim_map(mval
, &got
, &bno
, len
, obno
, end
, n
, flags
);
3837 xfs_bmapi_update_map(&mval
, &bno
, &len
, obno
, end
, &n
, flags
);
3839 /* If we're done, stop now. */
3840 if (bno
>= end
|| n
>= *nmap
)
3843 /* Else go on to the next record. */
3844 if (!xfs_iext_get_extent(ifp
, ++idx
, &got
))
3852 * Add a delayed allocation extent to an inode. Blocks are reserved from the
3853 * global pool and the extent inserted into the inode in-core extent tree.
3855 * On entry, got refers to the first extent beyond the offset of the extent to
3856 * allocate or eof is specified if no such extent exists. On return, got refers
3857 * to the extent record that was inserted to the inode fork.
3859 * Note that the allocated extent may have been merged with contiguous extents
3860 * during insertion into the inode fork. Thus, got does not reflect the current
3861 * state of the inode fork on return. If necessary, the caller can use lastx to
3862 * look up the updated record in the inode fork.
3865 xfs_bmapi_reserve_delalloc(
3866 struct xfs_inode
*ip
,
3870 xfs_filblks_t prealloc
,
3871 struct xfs_bmbt_irec
*got
,
3872 xfs_extnum_t
*lastx
,
3875 struct xfs_mount
*mp
= ip
->i_mount
;
3876 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
3878 xfs_extlen_t indlen
;
3879 char rt
= XFS_IS_REALTIME_INODE(ip
);
3882 xfs_fileoff_t aoff
= off
;
3885 * Cap the alloc length. Keep track of prealloc so we know whether to
3886 * tag the inode before we return.
3888 alen
= XFS_FILBLKS_MIN(len
+ prealloc
, MAXEXTLEN
);
3890 alen
= XFS_FILBLKS_MIN(alen
, got
->br_startoff
- aoff
);
3891 if (prealloc
&& alen
>= len
)
3892 prealloc
= alen
- len
;
3894 /* Figure out the extent size, adjust alen */
3895 if (whichfork
== XFS_COW_FORK
)
3896 extsz
= xfs_get_cowextsz_hint(ip
);
3898 extsz
= xfs_get_extsz_hint(ip
);
3900 struct xfs_bmbt_irec prev
;
3902 if (!xfs_iext_get_extent(ifp
, *lastx
- 1, &prev
))
3903 prev
.br_startoff
= NULLFILEOFF
;
3905 error
= xfs_bmap_extsize_align(mp
, got
, &prev
, extsz
, rt
, eof
,
3906 1, 0, &aoff
, &alen
);
3911 extsz
= alen
/ mp
->m_sb
.sb_rextsize
;
3914 * Make a transaction-less quota reservation for delayed allocation
3915 * blocks. This number gets adjusted later. We return if we haven't
3916 * allocated blocks already inside this loop.
3918 error
= xfs_trans_reserve_quota_nblks(NULL
, ip
, (long)alen
, 0,
3919 rt
? XFS_QMOPT_RES_RTBLKS
: XFS_QMOPT_RES_REGBLKS
);
3924 * Split changing sb for alen and indlen since they could be coming
3925 * from different places.
3927 indlen
= (xfs_extlen_t
)xfs_bmap_worst_indlen(ip
, alen
);
3931 error
= xfs_mod_frextents(mp
, -((int64_t)extsz
));
3933 error
= xfs_mod_fdblocks(mp
, -((int64_t)alen
), false);
3937 goto out_unreserve_quota
;
3939 error
= xfs_mod_fdblocks(mp
, -((int64_t)indlen
), false);
3941 goto out_unreserve_blocks
;
3944 ip
->i_delayed_blks
+= alen
;
3946 got
->br_startoff
= aoff
;
3947 got
->br_startblock
= nullstartblock(indlen
);
3948 got
->br_blockcount
= alen
;
3949 got
->br_state
= XFS_EXT_NORM
;
3951 xfs_bmap_add_extent_hole_delay(ip
, whichfork
, lastx
, got
);
3954 * Tag the inode if blocks were preallocated. Note that COW fork
3955 * preallocation can occur at the start or end of the extent, even when
3956 * prealloc == 0, so we must also check the aligned offset and length.
3958 if (whichfork
== XFS_DATA_FORK
&& prealloc
)
3959 xfs_inode_set_eofblocks_tag(ip
);
3960 if (whichfork
== XFS_COW_FORK
&& (prealloc
|| aoff
< off
|| alen
> len
))
3961 xfs_inode_set_cowblocks_tag(ip
);
3965 out_unreserve_blocks
:
3967 xfs_mod_frextents(mp
, extsz
);
3969 xfs_mod_fdblocks(mp
, alen
, false);
3970 out_unreserve_quota
:
3971 if (XFS_IS_QUOTA_ON(mp
))
3972 xfs_trans_unreserve_quota_nblks(NULL
, ip
, (long)alen
, 0, rt
?
3973 XFS_QMOPT_RES_RTBLKS
: XFS_QMOPT_RES_REGBLKS
);
3979 struct xfs_bmalloca
*bma
)
3981 struct xfs_mount
*mp
= bma
->ip
->i_mount
;
3982 int whichfork
= xfs_bmapi_whichfork(bma
->flags
);
3983 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
3984 int tmp_logflags
= 0;
3987 ASSERT(bma
->length
> 0);
3990 * For the wasdelay case, we could also just allocate the stuff asked
3991 * for in this bmap call but that wouldn't be as good.
3994 bma
->length
= (xfs_extlen_t
)bma
->got
.br_blockcount
;
3995 bma
->offset
= bma
->got
.br_startoff
;
3997 xfs_iext_get_extent(ifp
, bma
->idx
- 1, &bma
->prev
);
3999 bma
->length
= XFS_FILBLKS_MIN(bma
->length
, MAXEXTLEN
);
4001 bma
->length
= XFS_FILBLKS_MIN(bma
->length
,
4002 bma
->got
.br_startoff
- bma
->offset
);
4006 * Set the data type being allocated. For the data fork, the first data
4007 * in the file is treated differently to all other allocations. For the
4008 * attribute fork, we only need to ensure the allocated range is not on
4011 if (!(bma
->flags
& XFS_BMAPI_METADATA
)) {
4012 bma
->datatype
= XFS_ALLOC_NOBUSY
;
4013 if (whichfork
== XFS_DATA_FORK
) {
4014 if (bma
->offset
== 0)
4015 bma
->datatype
|= XFS_ALLOC_INITIAL_USER_DATA
;
4017 bma
->datatype
|= XFS_ALLOC_USERDATA
;
4019 if (bma
->flags
& XFS_BMAPI_ZERO
)
4020 bma
->datatype
|= XFS_ALLOC_USERDATA_ZERO
;
4023 bma
->minlen
= (bma
->flags
& XFS_BMAPI_CONTIG
) ? bma
->length
: 1;
4026 * Only want to do the alignment at the eof if it is userdata and
4027 * allocation length is larger than a stripe unit.
4029 if (mp
->m_dalign
&& bma
->length
>= mp
->m_dalign
&&
4030 !(bma
->flags
& XFS_BMAPI_METADATA
) && whichfork
== XFS_DATA_FORK
) {
4031 error
= xfs_bmap_isaeof(bma
, whichfork
);
4036 error
= xfs_bmap_alloc(bma
);
4041 bma
->cur
->bc_private
.b
.firstblock
= *bma
->firstblock
;
4042 if (bma
->blkno
== NULLFSBLOCK
)
4044 if ((ifp
->if_flags
& XFS_IFBROOT
) && !bma
->cur
) {
4045 bma
->cur
= xfs_bmbt_init_cursor(mp
, bma
->tp
, bma
->ip
, whichfork
);
4046 bma
->cur
->bc_private
.b
.firstblock
= *bma
->firstblock
;
4047 bma
->cur
->bc_private
.b
.dfops
= bma
->dfops
;
4050 * Bump the number of extents we've allocated
4056 bma
->cur
->bc_private
.b
.flags
=
4057 bma
->wasdel
? XFS_BTCUR_BPRV_WASDEL
: 0;
4059 bma
->got
.br_startoff
= bma
->offset
;
4060 bma
->got
.br_startblock
= bma
->blkno
;
4061 bma
->got
.br_blockcount
= bma
->length
;
4062 bma
->got
.br_state
= XFS_EXT_NORM
;
4065 * In the data fork, a wasdelay extent has been initialized, so
4066 * shouldn't be flagged as unwritten.
4068 * For the cow fork, however, we convert delalloc reservations
4069 * (extents allocated for speculative preallocation) to
4070 * allocated unwritten extents, and only convert the unwritten
4071 * extents to real extents when we're about to write the data.
4073 if ((!bma
->wasdel
|| (bma
->flags
& XFS_BMAPI_COWFORK
)) &&
4074 (bma
->flags
& XFS_BMAPI_PREALLOC
) &&
4075 xfs_sb_version_hasextflgbit(&mp
->m_sb
))
4076 bma
->got
.br_state
= XFS_EXT_UNWRITTEN
;
4079 error
= xfs_bmap_add_extent_delay_real(bma
, whichfork
);
4081 error
= xfs_bmap_add_extent_hole_real(bma
->tp
, bma
->ip
,
4082 whichfork
, &bma
->idx
, &bma
->cur
, &bma
->got
,
4083 bma
->firstblock
, bma
->dfops
, &bma
->logflags
);
4085 bma
->logflags
|= tmp_logflags
;
4090 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4091 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4092 * the neighbouring ones.
4094 xfs_iext_get_extent(ifp
, bma
->idx
, &bma
->got
);
4096 ASSERT(bma
->got
.br_startoff
<= bma
->offset
);
4097 ASSERT(bma
->got
.br_startoff
+ bma
->got
.br_blockcount
>=
4098 bma
->offset
+ bma
->length
);
4099 ASSERT(bma
->got
.br_state
== XFS_EXT_NORM
||
4100 bma
->got
.br_state
== XFS_EXT_UNWRITTEN
);
4105 xfs_bmapi_convert_unwritten(
4106 struct xfs_bmalloca
*bma
,
4107 struct xfs_bmbt_irec
*mval
,
4111 int whichfork
= xfs_bmapi_whichfork(flags
);
4112 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
4113 int tmp_logflags
= 0;
4116 /* check if we need to do unwritten->real conversion */
4117 if (mval
->br_state
== XFS_EXT_UNWRITTEN
&&
4118 (flags
& XFS_BMAPI_PREALLOC
))
4121 /* check if we need to do real->unwritten conversion */
4122 if (mval
->br_state
== XFS_EXT_NORM
&&
4123 (flags
& (XFS_BMAPI_PREALLOC
| XFS_BMAPI_CONVERT
)) !=
4124 (XFS_BMAPI_PREALLOC
| XFS_BMAPI_CONVERT
))
4128 * Modify (by adding) the state flag, if writing.
4130 ASSERT(mval
->br_blockcount
<= len
);
4131 if ((ifp
->if_flags
& XFS_IFBROOT
) && !bma
->cur
) {
4132 bma
->cur
= xfs_bmbt_init_cursor(bma
->ip
->i_mount
, bma
->tp
,
4133 bma
->ip
, whichfork
);
4134 bma
->cur
->bc_private
.b
.firstblock
= *bma
->firstblock
;
4135 bma
->cur
->bc_private
.b
.dfops
= bma
->dfops
;
4137 mval
->br_state
= (mval
->br_state
== XFS_EXT_UNWRITTEN
)
4138 ? XFS_EXT_NORM
: XFS_EXT_UNWRITTEN
;
4141 * Before insertion into the bmbt, zero the range being converted
4144 if (flags
& XFS_BMAPI_ZERO
) {
4145 error
= xfs_zero_extent(bma
->ip
, mval
->br_startblock
,
4146 mval
->br_blockcount
);
4151 error
= xfs_bmap_add_extent_unwritten_real(bma
->tp
, bma
->ip
, whichfork
,
4152 &bma
->idx
, &bma
->cur
, mval
, bma
->firstblock
, bma
->dfops
,
4155 * Log the inode core unconditionally in the unwritten extent conversion
4156 * path because the conversion might not have done so (e.g., if the
4157 * extent count hasn't changed). We need to make sure the inode is dirty
4158 * in the transaction for the sake of fsync(), even if nothing has
4159 * changed, because fsync() will not force the log for this transaction
4160 * unless it sees the inode pinned.
4162 * Note: If we're only converting cow fork extents, there aren't
4163 * any on-disk updates to make, so we don't need to log anything.
4165 if (whichfork
!= XFS_COW_FORK
)
4166 bma
->logflags
|= tmp_logflags
| XFS_ILOG_CORE
;
4171 * Update our extent pointer, given that
4172 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4173 * of the neighbouring ones.
4175 xfs_iext_get_extent(ifp
, bma
->idx
, &bma
->got
);
4178 * We may have combined previously unwritten space with written space,
4179 * so generate another request.
4181 if (mval
->br_blockcount
< len
)
4187 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4188 * extent state if necessary. Details behaviour is controlled by the flags
4189 * parameter. Only allocates blocks from a single allocation group, to avoid
4192 * The returned value in "firstblock" from the first call in a transaction
4193 * must be remembered and presented to subsequent calls in "firstblock".
4194 * An upper bound for the number of blocks to be allocated is supplied to
4195 * the first call in "total"; if no allocation group has that many free
4196 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4200 struct xfs_trans
*tp
, /* transaction pointer */
4201 struct xfs_inode
*ip
, /* incore inode */
4202 xfs_fileoff_t bno
, /* starting file offs. mapped */
4203 xfs_filblks_t len
, /* length to map in file */
4204 int flags
, /* XFS_BMAPI_... */
4205 xfs_fsblock_t
*firstblock
, /* first allocated block
4206 controls a.g. for allocs */
4207 xfs_extlen_t total
, /* total blocks needed */
4208 struct xfs_bmbt_irec
*mval
, /* output: map values */
4209 int *nmap
, /* i/o: mval size/count */
4210 struct xfs_defer_ops
*dfops
) /* i/o: list extents to free */
4212 struct xfs_mount
*mp
= ip
->i_mount
;
4213 struct xfs_ifork
*ifp
;
4214 struct xfs_bmalloca bma
= { NULL
}; /* args for xfs_bmap_alloc */
4215 xfs_fileoff_t end
; /* end of mapped file region */
4216 bool eof
= false; /* after the end of extents */
4217 int error
; /* error return */
4218 int n
; /* current extent index */
4219 xfs_fileoff_t obno
; /* old block number (offset) */
4220 int whichfork
; /* data or attr fork */
4223 xfs_fileoff_t orig_bno
; /* original block number value */
4224 int orig_flags
; /* original flags arg value */
4225 xfs_filblks_t orig_len
; /* original value of len arg */
4226 struct xfs_bmbt_irec
*orig_mval
; /* original value of mval */
4227 int orig_nmap
; /* original value of *nmap */
4235 whichfork
= xfs_bmapi_whichfork(flags
);
4238 ASSERT(*nmap
<= XFS_BMAP_MAX_NMAP
);
4239 ASSERT(!(flags
& XFS_BMAPI_IGSTATE
));
4240 ASSERT(tp
!= NULL
||
4241 (flags
& (XFS_BMAPI_CONVERT
| XFS_BMAPI_COWFORK
)) ==
4242 (XFS_BMAPI_CONVERT
| XFS_BMAPI_COWFORK
));
4244 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_LOCAL
);
4245 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
4246 ASSERT(!(flags
& XFS_BMAPI_REMAP
));
4248 /* zeroing is for currently only for data extents, not metadata */
4249 ASSERT((flags
& (XFS_BMAPI_METADATA
| XFS_BMAPI_ZERO
)) !=
4250 (XFS_BMAPI_METADATA
| XFS_BMAPI_ZERO
));
4252 * we can allocate unwritten extents or pre-zero allocated blocks,
4253 * but it makes no sense to do both at once. This would result in
4254 * zeroing the unwritten extent twice, but it still being an
4255 * unwritten extent....
4257 ASSERT((flags
& (XFS_BMAPI_PREALLOC
| XFS_BMAPI_ZERO
)) !=
4258 (XFS_BMAPI_PREALLOC
| XFS_BMAPI_ZERO
));
4260 if (unlikely(XFS_TEST_ERROR(
4261 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
4262 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
4263 mp
, XFS_ERRTAG_BMAPIFORMAT
))) {
4264 XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW
, mp
);
4265 return -EFSCORRUPTED
;
4268 if (XFS_FORCED_SHUTDOWN(mp
))
4271 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4273 XFS_STATS_INC(mp
, xs_blk_mapw
);
4275 if (*firstblock
== NULLFSBLOCK
) {
4276 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
)
4277 bma
.minleft
= be16_to_cpu(ifp
->if_broot
->bb_level
) + 1;
4284 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
4285 error
= xfs_iread_extents(tp
, ip
, whichfork
);
4294 if (!xfs_iext_lookup_extent(ip
, ifp
, bno
, &bma
.idx
, &bma
.got
))
4296 if (!xfs_iext_get_extent(ifp
, bma
.idx
- 1, &bma
.prev
))
4297 bma
.prev
.br_startoff
= NULLFILEOFF
;
4303 bma
.firstblock
= firstblock
;
4305 while (bno
< end
&& n
< *nmap
) {
4306 bool need_alloc
= false, wasdelay
= false;
4308 /* in hole or beyoned EOF? */
4309 if (eof
|| bma
.got
.br_startoff
> bno
) {
4310 if (flags
& XFS_BMAPI_DELALLOC
) {
4312 * For the COW fork we can reasonably get a
4313 * request for converting an extent that races
4314 * with other threads already having converted
4315 * part of it, as there converting COW to
4316 * regular blocks is not protected using the
4319 ASSERT(flags
& XFS_BMAPI_COWFORK
);
4320 if (!(flags
& XFS_BMAPI_COWFORK
)) {
4325 if (eof
|| bno
>= end
)
4330 } else if (isnullstartblock(bma
.got
.br_startblock
)) {
4335 * First, deal with the hole before the allocated space
4336 * that we found, if any.
4338 if (need_alloc
|| wasdelay
) {
4340 bma
.conv
= !!(flags
& XFS_BMAPI_CONVERT
);
4341 bma
.wasdel
= wasdelay
;
4346 * There's a 32/64 bit type mismatch between the
4347 * allocation length request (which can be 64 bits in
4348 * length) and the bma length request, which is
4349 * xfs_extlen_t and therefore 32 bits. Hence we have to
4350 * check for 32-bit overflows and handle them here.
4352 if (len
> (xfs_filblks_t
)MAXEXTLEN
)
4353 bma
.length
= MAXEXTLEN
;
4358 ASSERT(bma
.length
> 0);
4359 error
= xfs_bmapi_allocate(&bma
);
4362 if (bma
.blkno
== NULLFSBLOCK
)
4366 * If this is a CoW allocation, record the data in
4367 * the refcount btree for orphan recovery.
4369 if (whichfork
== XFS_COW_FORK
) {
4370 error
= xfs_refcount_alloc_cow_extent(mp
, dfops
,
4371 bma
.blkno
, bma
.length
);
4377 /* Deal with the allocated space we found. */
4378 xfs_bmapi_trim_map(mval
, &bma
.got
, &bno
, len
, obno
,
4381 /* Execute unwritten extent conversion if necessary */
4382 error
= xfs_bmapi_convert_unwritten(&bma
, mval
, len
, flags
);
4383 if (error
== -EAGAIN
)
4388 /* update the extent map to return */
4389 xfs_bmapi_update_map(&mval
, &bno
, &len
, obno
, end
, &n
, flags
);
4392 * If we're done, stop now. Stop when we've allocated
4393 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4394 * the transaction may get too big.
4396 if (bno
>= end
|| n
>= *nmap
|| bma
.nallocs
>= *nmap
)
4399 /* Else go on to the next record. */
4401 if (!xfs_iext_get_extent(ifp
, ++bma
.idx
, &bma
.got
))
4407 * Transform from btree to extents, give it cur.
4409 if (xfs_bmap_wants_extents(ip
, whichfork
)) {
4410 int tmp_logflags
= 0;
4413 error
= xfs_bmap_btree_to_extents(tp
, ip
, bma
.cur
,
4414 &tmp_logflags
, whichfork
);
4415 bma
.logflags
|= tmp_logflags
;
4420 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
||
4421 XFS_IFORK_NEXTENTS(ip
, whichfork
) >
4422 XFS_IFORK_MAXEXT(ip
, whichfork
));
4426 * Log everything. Do this after conversion, there's no point in
4427 * logging the extent records if we've converted to btree format.
4429 if ((bma
.logflags
& xfs_ilog_fext(whichfork
)) &&
4430 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
4431 bma
.logflags
&= ~xfs_ilog_fext(whichfork
);
4432 else if ((bma
.logflags
& xfs_ilog_fbroot(whichfork
)) &&
4433 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)
4434 bma
.logflags
&= ~xfs_ilog_fbroot(whichfork
);
4436 * Log whatever the flags say, even if error. Otherwise we might miss
4437 * detecting a case where the data is changed, there's an error,
4438 * and it's not logged so we don't shutdown when we should.
4441 xfs_trans_log_inode(tp
, ip
, bma
.logflags
);
4445 ASSERT(*firstblock
== NULLFSBLOCK
||
4446 XFS_FSB_TO_AGNO(mp
, *firstblock
) <=
4448 bma
.cur
->bc_private
.b
.firstblock
));
4449 *firstblock
= bma
.cur
->bc_private
.b
.firstblock
;
4451 xfs_btree_del_cursor(bma
.cur
,
4452 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
4455 xfs_bmap_validate_ret(orig_bno
, orig_len
, orig_flags
, orig_mval
,
4462 struct xfs_trans
*tp
,
4463 struct xfs_inode
*ip
,
4466 xfs_fsblock_t startblock
,
4467 struct xfs_defer_ops
*dfops
)
4469 struct xfs_mount
*mp
= ip
->i_mount
;
4470 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, XFS_DATA_FORK
);
4471 struct xfs_btree_cur
*cur
= NULL
;
4472 xfs_fsblock_t firstblock
= NULLFSBLOCK
;
4473 struct xfs_bmbt_irec got
;
4475 int logflags
= 0, error
;
4478 ASSERT(len
<= (xfs_filblks_t
)MAXEXTLEN
);
4479 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
4481 if (unlikely(XFS_TEST_ERROR(
4482 (XFS_IFORK_FORMAT(ip
, XFS_DATA_FORK
) != XFS_DINODE_FMT_EXTENTS
&&
4483 XFS_IFORK_FORMAT(ip
, XFS_DATA_FORK
) != XFS_DINODE_FMT_BTREE
),
4484 mp
, XFS_ERRTAG_BMAPIFORMAT
))) {
4485 XFS_ERROR_REPORT("xfs_bmapi_remap", XFS_ERRLEVEL_LOW
, mp
);
4486 return -EFSCORRUPTED
;
4489 if (XFS_FORCED_SHUTDOWN(mp
))
4492 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
4493 error
= xfs_iread_extents(NULL
, ip
, XFS_DATA_FORK
);
4498 if (xfs_iext_lookup_extent(ip
, ifp
, bno
, &idx
, &got
)) {
4499 /* make sure we only reflink into a hole. */
4500 ASSERT(got
.br_startoff
> bno
);
4501 ASSERT(got
.br_startoff
- bno
>= len
);
4504 ip
->i_d
.di_nblocks
+= len
;
4505 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
4507 if (ifp
->if_flags
& XFS_IFBROOT
) {
4508 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, XFS_DATA_FORK
);
4509 cur
->bc_private
.b
.firstblock
= firstblock
;
4510 cur
->bc_private
.b
.dfops
= dfops
;
4511 cur
->bc_private
.b
.flags
= 0;
4514 got
.br_startoff
= bno
;
4515 got
.br_startblock
= startblock
;
4516 got
.br_blockcount
= len
;
4517 got
.br_state
= XFS_EXT_NORM
;
4519 error
= xfs_bmap_add_extent_hole_real(tp
, ip
, XFS_DATA_FORK
, &idx
, &cur
,
4520 &got
, &firstblock
, dfops
, &logflags
);
4524 if (xfs_bmap_wants_extents(ip
, XFS_DATA_FORK
)) {
4525 int tmp_logflags
= 0;
4527 error
= xfs_bmap_btree_to_extents(tp
, ip
, cur
,
4528 &tmp_logflags
, XFS_DATA_FORK
);
4529 logflags
|= tmp_logflags
;
4533 if (ip
->i_d
.di_format
!= XFS_DINODE_FMT_EXTENTS
)
4534 logflags
&= ~XFS_ILOG_DEXT
;
4535 else if (ip
->i_d
.di_format
!= XFS_DINODE_FMT_BTREE
)
4536 logflags
&= ~XFS_ILOG_DBROOT
;
4539 xfs_trans_log_inode(tp
, ip
, logflags
);
4541 xfs_btree_del_cursor(cur
,
4542 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
4548 * When a delalloc extent is split (e.g., due to a hole punch), the original
4549 * indlen reservation must be shared across the two new extents that are left
4552 * Given the original reservation and the worst case indlen for the two new
4553 * extents (as calculated by xfs_bmap_worst_indlen()), split the original
4554 * reservation fairly across the two new extents. If necessary, steal available
4555 * blocks from a deleted extent to make up a reservation deficiency (e.g., if
4556 * ores == 1). The number of stolen blocks is returned. The availability and
4557 * subsequent accounting of stolen blocks is the responsibility of the caller.
4559 static xfs_filblks_t
4560 xfs_bmap_split_indlen(
4561 xfs_filblks_t ores
, /* original res. */
4562 xfs_filblks_t
*indlen1
, /* ext1 worst indlen */
4563 xfs_filblks_t
*indlen2
, /* ext2 worst indlen */
4564 xfs_filblks_t avail
) /* stealable blocks */
4566 xfs_filblks_t len1
= *indlen1
;
4567 xfs_filblks_t len2
= *indlen2
;
4568 xfs_filblks_t nres
= len1
+ len2
; /* new total res. */
4569 xfs_filblks_t stolen
= 0;
4570 xfs_filblks_t resfactor
;
4573 * Steal as many blocks as we can to try and satisfy the worst case
4574 * indlen for both new extents.
4576 if (ores
< nres
&& avail
)
4577 stolen
= XFS_FILBLKS_MIN(nres
- ores
, avail
);
4580 /* nothing else to do if we've satisfied the new reservation */
4585 * We can't meet the total required reservation for the two extents.
4586 * Calculate the percent of the overall shortage between both extents
4587 * and apply this percentage to each of the requested indlen values.
4588 * This distributes the shortage fairly and reduces the chances that one
4589 * of the two extents is left with nothing when extents are repeatedly
4592 resfactor
= (ores
* 100);
4593 do_div(resfactor
, nres
);
4598 ASSERT(len1
+ len2
<= ores
);
4599 ASSERT(len1
< *indlen1
&& len2
< *indlen2
);
4602 * Hand out the remainder to each extent. If one of the two reservations
4603 * is zero, we want to make sure that one gets a block first. The loop
4604 * below starts with len1, so hand len2 a block right off the bat if it
4607 ores
-= (len1
+ len2
);
4608 ASSERT((*indlen1
- len1
) + (*indlen2
- len2
) >= ores
);
4609 if (ores
&& !len2
&& *indlen2
) {
4614 if (len1
< *indlen1
) {
4620 if (len2
< *indlen2
) {
4633 xfs_bmap_del_extent_delay(
4634 struct xfs_inode
*ip
,
4637 struct xfs_bmbt_irec
*got
,
4638 struct xfs_bmbt_irec
*del
)
4640 struct xfs_mount
*mp
= ip
->i_mount
;
4641 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4642 struct xfs_bmbt_irec
new;
4643 int64_t da_old
, da_new
, da_diff
= 0;
4644 xfs_fileoff_t del_endoff
, got_endoff
;
4645 xfs_filblks_t got_indlen
, new_indlen
, stolen
;
4646 int state
= xfs_bmap_fork_to_state(whichfork
);
4650 XFS_STATS_INC(mp
, xs_del_exlist
);
4652 isrt
= (whichfork
== XFS_DATA_FORK
) && XFS_IS_REALTIME_INODE(ip
);
4653 del_endoff
= del
->br_startoff
+ del
->br_blockcount
;
4654 got_endoff
= got
->br_startoff
+ got
->br_blockcount
;
4655 da_old
= startblockval(got
->br_startblock
);
4659 ASSERT(*idx
<= xfs_iext_count(ifp
));
4660 ASSERT(del
->br_blockcount
> 0);
4661 ASSERT(got
->br_startoff
<= del
->br_startoff
);
4662 ASSERT(got_endoff
>= del_endoff
);
4665 uint64_t rtexts
= XFS_FSB_TO_B(mp
, del
->br_blockcount
);
4667 do_div(rtexts
, mp
->m_sb
.sb_rextsize
);
4668 xfs_mod_frextents(mp
, rtexts
);
4672 * Update the inode delalloc counter now and wait to update the
4673 * sb counters as we might have to borrow some blocks for the
4674 * indirect block accounting.
4676 error
= xfs_trans_reserve_quota_nblks(NULL
, ip
,
4677 -((long)del
->br_blockcount
), 0,
4678 isrt
? XFS_QMOPT_RES_RTBLKS
: XFS_QMOPT_RES_REGBLKS
);
4681 ip
->i_delayed_blks
-= del
->br_blockcount
;
4683 if (got
->br_startoff
== del
->br_startoff
)
4684 state
|= BMAP_LEFT_FILLING
;
4685 if (got_endoff
== del_endoff
)
4686 state
|= BMAP_RIGHT_FILLING
;
4688 switch (state
& (BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
)) {
4689 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
4691 * Matches the whole extent. Delete the entry.
4693 xfs_iext_remove(ip
, *idx
, 1, state
);
4696 case BMAP_LEFT_FILLING
:
4698 * Deleting the first part of the extent.
4700 got
->br_startoff
= del_endoff
;
4701 got
->br_blockcount
-= del
->br_blockcount
;
4702 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
,
4703 got
->br_blockcount
), da_old
);
4704 got
->br_startblock
= nullstartblock((int)da_new
);
4705 xfs_iext_update_extent(ip
, state
, *idx
, got
);
4707 case BMAP_RIGHT_FILLING
:
4709 * Deleting the last part of the extent.
4711 got
->br_blockcount
= got
->br_blockcount
- del
->br_blockcount
;
4712 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
,
4713 got
->br_blockcount
), da_old
);
4714 got
->br_startblock
= nullstartblock((int)da_new
);
4715 xfs_iext_update_extent(ip
, state
, *idx
, got
);
4719 * Deleting the middle of the extent.
4721 * Distribute the original indlen reservation across the two new
4722 * extents. Steal blocks from the deleted extent if necessary.
4723 * Stealing blocks simply fudges the fdblocks accounting below.
4724 * Warn if either of the new indlen reservations is zero as this
4725 * can lead to delalloc problems.
4727 got
->br_blockcount
= del
->br_startoff
- got
->br_startoff
;
4728 got_indlen
= xfs_bmap_worst_indlen(ip
, got
->br_blockcount
);
4730 new.br_blockcount
= got_endoff
- del_endoff
;
4731 new_indlen
= xfs_bmap_worst_indlen(ip
, new.br_blockcount
);
4733 WARN_ON_ONCE(!got_indlen
|| !new_indlen
);
4734 stolen
= xfs_bmap_split_indlen(da_old
, &got_indlen
, &new_indlen
,
4735 del
->br_blockcount
);
4737 got
->br_startblock
= nullstartblock((int)got_indlen
);
4738 xfs_iext_update_extent(ip
, state
, *idx
, got
);
4740 new.br_startoff
= del_endoff
;
4741 new.br_state
= got
->br_state
;
4742 new.br_startblock
= nullstartblock((int)new_indlen
);
4745 xfs_iext_insert(ip
, *idx
, 1, &new, state
);
4747 da_new
= got_indlen
+ new_indlen
- stolen
;
4748 del
->br_blockcount
-= stolen
;
4752 ASSERT(da_old
>= da_new
);
4753 da_diff
= da_old
- da_new
;
4755 da_diff
+= del
->br_blockcount
;
4757 xfs_mod_fdblocks(mp
, da_diff
, false);
4762 xfs_bmap_del_extent_cow(
4763 struct xfs_inode
*ip
,
4765 struct xfs_bmbt_irec
*got
,
4766 struct xfs_bmbt_irec
*del
)
4768 struct xfs_mount
*mp
= ip
->i_mount
;
4769 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, XFS_COW_FORK
);
4770 struct xfs_bmbt_irec
new;
4771 xfs_fileoff_t del_endoff
, got_endoff
;
4772 int state
= BMAP_COWFORK
;
4774 XFS_STATS_INC(mp
, xs_del_exlist
);
4776 del_endoff
= del
->br_startoff
+ del
->br_blockcount
;
4777 got_endoff
= got
->br_startoff
+ got
->br_blockcount
;
4780 ASSERT(*idx
<= xfs_iext_count(ifp
));
4781 ASSERT(del
->br_blockcount
> 0);
4782 ASSERT(got
->br_startoff
<= del
->br_startoff
);
4783 ASSERT(got_endoff
>= del_endoff
);
4784 ASSERT(!isnullstartblock(got
->br_startblock
));
4786 if (got
->br_startoff
== del
->br_startoff
)
4787 state
|= BMAP_LEFT_FILLING
;
4788 if (got_endoff
== del_endoff
)
4789 state
|= BMAP_RIGHT_FILLING
;
4791 switch (state
& (BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
)) {
4792 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
4794 * Matches the whole extent. Delete the entry.
4796 xfs_iext_remove(ip
, *idx
, 1, state
);
4799 case BMAP_LEFT_FILLING
:
4801 * Deleting the first part of the extent.
4803 got
->br_startoff
= del_endoff
;
4804 got
->br_blockcount
-= del
->br_blockcount
;
4805 got
->br_startblock
= del
->br_startblock
+ del
->br_blockcount
;
4806 xfs_iext_update_extent(ip
, state
, *idx
, got
);
4808 case BMAP_RIGHT_FILLING
:
4810 * Deleting the last part of the extent.
4812 got
->br_blockcount
-= del
->br_blockcount
;
4813 xfs_iext_update_extent(ip
, state
, *idx
, got
);
4817 * Deleting the middle of the extent.
4819 got
->br_blockcount
= del
->br_startoff
- got
->br_startoff
;
4820 xfs_iext_update_extent(ip
, state
, *idx
, got
);
4822 new.br_startoff
= del_endoff
;
4823 new.br_blockcount
= got_endoff
- del_endoff
;
4824 new.br_state
= got
->br_state
;
4825 new.br_startblock
= del
->br_startblock
+ del
->br_blockcount
;
4828 xfs_iext_insert(ip
, *idx
, 1, &new, state
);
4834 * Called by xfs_bmapi to update file extent records and the btree
4835 * after removing space.
4837 STATIC
int /* error */
4838 xfs_bmap_del_extent_real(
4839 xfs_inode_t
*ip
, /* incore inode pointer */
4840 xfs_trans_t
*tp
, /* current transaction pointer */
4841 xfs_extnum_t
*idx
, /* extent number to update/delete */
4842 struct xfs_defer_ops
*dfops
, /* list of extents to be freed */
4843 xfs_btree_cur_t
*cur
, /* if null, not a btree */
4844 xfs_bmbt_irec_t
*del
, /* data to remove from extents */
4845 int *logflagsp
, /* inode logging flags */
4846 int whichfork
, /* data or attr fork */
4847 int bflags
) /* bmapi flags */
4849 xfs_fsblock_t del_endblock
=0; /* first block past del */
4850 xfs_fileoff_t del_endoff
; /* first offset past del */
4851 int do_fx
; /* free extent at end of routine */
4852 int error
; /* error return value */
4853 int flags
= 0;/* inode logging flags */
4854 struct xfs_bmbt_irec got
; /* current extent entry */
4855 xfs_fileoff_t got_endoff
; /* first offset past got */
4856 int i
; /* temp state */
4857 xfs_ifork_t
*ifp
; /* inode fork pointer */
4858 xfs_mount_t
*mp
; /* mount structure */
4859 xfs_filblks_t nblks
; /* quota/sb block count */
4860 xfs_bmbt_irec_t
new; /* new record to be inserted */
4862 uint qfield
; /* quota field to update */
4863 int state
= xfs_bmap_fork_to_state(whichfork
);
4864 struct xfs_bmbt_irec old
;
4867 XFS_STATS_INC(mp
, xs_del_exlist
);
4869 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4870 ASSERT((*idx
>= 0) && (*idx
< xfs_iext_count(ifp
)));
4871 ASSERT(del
->br_blockcount
> 0);
4872 xfs_iext_get_extent(ifp
, *idx
, &got
);
4873 ASSERT(got
.br_startoff
<= del
->br_startoff
);
4874 del_endoff
= del
->br_startoff
+ del
->br_blockcount
;
4875 got_endoff
= got
.br_startoff
+ got
.br_blockcount
;
4876 ASSERT(got_endoff
>= del_endoff
);
4877 ASSERT(!isnullstartblock(got
.br_startblock
));
4882 * If it's the case where the directory code is running with no block
4883 * reservation, and the deleted block is in the middle of its extent,
4884 * and the resulting insert of an extent would cause transformation to
4885 * btree format, then reject it. The calling code will then swap blocks
4886 * around instead. We have to do this now, rather than waiting for the
4887 * conversion to btree format, since the transaction will be dirty then.
4889 if (tp
->t_blk_res
== 0 &&
4890 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
&&
4891 XFS_IFORK_NEXTENTS(ip
, whichfork
) >=
4892 XFS_IFORK_MAXEXT(ip
, whichfork
) &&
4893 del
->br_startoff
> got
.br_startoff
&& del_endoff
< got_endoff
)
4896 flags
= XFS_ILOG_CORE
;
4897 if (whichfork
== XFS_DATA_FORK
&& XFS_IS_REALTIME_INODE(ip
)) {
4901 ASSERT(do_mod(del
->br_blockcount
, mp
->m_sb
.sb_rextsize
) == 0);
4902 ASSERT(do_mod(del
->br_startblock
, mp
->m_sb
.sb_rextsize
) == 0);
4903 bno
= del
->br_startblock
;
4904 len
= del
->br_blockcount
;
4905 do_div(bno
, mp
->m_sb
.sb_rextsize
);
4906 do_div(len
, mp
->m_sb
.sb_rextsize
);
4907 error
= xfs_rtfree_extent(tp
, bno
, (xfs_extlen_t
)len
);
4911 nblks
= len
* mp
->m_sb
.sb_rextsize
;
4912 qfield
= XFS_TRANS_DQ_RTBCOUNT
;
4915 nblks
= del
->br_blockcount
;
4916 qfield
= XFS_TRANS_DQ_BCOUNT
;
4919 del_endblock
= del
->br_startblock
+ del
->br_blockcount
;
4921 error
= xfs_bmbt_lookup_eq(cur
, &got
, &i
);
4924 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
4927 if (got
.br_startoff
== del
->br_startoff
)
4928 state
|= BMAP_LEFT_FILLING
;
4929 if (got_endoff
== del_endoff
)
4930 state
|= BMAP_RIGHT_FILLING
;
4932 switch (state
& (BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
)) {
4933 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
4935 * Matches the whole extent. Delete the entry.
4937 xfs_iext_remove(ip
, *idx
, 1, state
);
4940 XFS_IFORK_NEXT_SET(ip
, whichfork
,
4941 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
4942 flags
|= XFS_ILOG_CORE
;
4944 flags
|= xfs_ilog_fext(whichfork
);
4947 if ((error
= xfs_btree_delete(cur
, &i
)))
4949 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
4951 case BMAP_LEFT_FILLING
:
4953 * Deleting the first part of the extent.
4955 got
.br_startoff
= del_endoff
;
4956 got
.br_startblock
= del_endblock
;
4957 got
.br_blockcount
-= del
->br_blockcount
;
4958 xfs_iext_update_extent(ip
, state
, *idx
, &got
);
4960 flags
|= xfs_ilog_fext(whichfork
);
4963 error
= xfs_bmbt_update(cur
, &got
);
4967 case BMAP_RIGHT_FILLING
:
4969 * Deleting the last part of the extent.
4971 got
.br_blockcount
-= del
->br_blockcount
;
4972 xfs_iext_update_extent(ip
, state
, *idx
, &got
);
4974 flags
|= xfs_ilog_fext(whichfork
);
4977 error
= xfs_bmbt_update(cur
, &got
);
4983 * Deleting the middle of the extent.
4987 got
.br_blockcount
= del
->br_startoff
- got
.br_startoff
;
4988 xfs_iext_update_extent(ip
, state
, *idx
, &got
);
4990 new.br_startoff
= del_endoff
;
4991 new.br_blockcount
= got_endoff
- del_endoff
;
4992 new.br_state
= got
.br_state
;
4993 new.br_startblock
= del_endblock
;
4995 flags
|= XFS_ILOG_CORE
;
4997 error
= xfs_bmbt_update(cur
, &got
);
5000 error
= xfs_btree_increment(cur
, 0, &i
);
5003 cur
->bc_rec
.b
= new;
5004 error
= xfs_btree_insert(cur
, &i
);
5005 if (error
&& error
!= -ENOSPC
)
5008 * If get no-space back from btree insert, it tried a
5009 * split, and we have a zero block reservation. Fix up
5010 * our state and return the error.
5012 if (error
== -ENOSPC
) {
5014 * Reset the cursor, don't trust it after any
5017 error
= xfs_bmbt_lookup_eq(cur
, &got
, &i
);
5020 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
5022 * Update the btree record back
5023 * to the original value.
5025 error
= xfs_bmbt_update(cur
, &old
);
5029 * Reset the extent record back
5030 * to the original value.
5032 xfs_iext_update_extent(ip
, state
, *idx
, &old
);
5037 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
5039 flags
|= xfs_ilog_fext(whichfork
);
5040 XFS_IFORK_NEXT_SET(ip
, whichfork
,
5041 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
5042 xfs_iext_insert(ip
, *idx
+ 1, 1, &new, state
);
5047 /* remove reverse mapping */
5048 error
= xfs_rmap_unmap_extent(mp
, dfops
, ip
, whichfork
, del
);
5053 * If we need to, add to list of extents to delete.
5055 if (do_fx
&& !(bflags
& XFS_BMAPI_REMAP
)) {
5056 if (xfs_is_reflink_inode(ip
) && whichfork
== XFS_DATA_FORK
) {
5057 error
= xfs_refcount_decrease_extent(mp
, dfops
, del
);
5061 xfs_bmap_add_free(mp
, dfops
, del
->br_startblock
,
5062 del
->br_blockcount
, NULL
);
5066 * Adjust inode # blocks in the file.
5069 ip
->i_d
.di_nblocks
-= nblks
;
5071 * Adjust quota data.
5073 if (qfield
&& !(bflags
& XFS_BMAPI_REMAP
))
5074 xfs_trans_mod_dquot_byino(tp
, ip
, qfield
, (long)-nblks
);
5082 * Unmap (remove) blocks from a file.
5083 * If nexts is nonzero then the number of extents to remove is limited to
5084 * that value. If not all extents in the block range can be removed then
5089 xfs_trans_t
*tp
, /* transaction pointer */
5090 struct xfs_inode
*ip
, /* incore inode */
5091 xfs_fileoff_t start
, /* first file offset deleted */
5092 xfs_filblks_t
*rlen
, /* i/o: amount remaining */
5093 int flags
, /* misc flags */
5094 xfs_extnum_t nexts
, /* number of extents max */
5095 xfs_fsblock_t
*firstblock
, /* first allocated block
5096 controls a.g. for allocs */
5097 struct xfs_defer_ops
*dfops
) /* i/o: deferred updates */
5099 xfs_btree_cur_t
*cur
; /* bmap btree cursor */
5100 xfs_bmbt_irec_t del
; /* extent being deleted */
5101 int error
; /* error return value */
5102 xfs_extnum_t extno
; /* extent number in list */
5103 xfs_bmbt_irec_t got
; /* current extent record */
5104 xfs_ifork_t
*ifp
; /* inode fork pointer */
5105 int isrt
; /* freeing in rt area */
5106 xfs_extnum_t lastx
; /* last extent index used */
5107 int logflags
; /* transaction logging flags */
5108 xfs_extlen_t mod
; /* rt extent offset */
5109 xfs_mount_t
*mp
; /* mount structure */
5110 int tmp_logflags
; /* partial logging flags */
5111 int wasdel
; /* was a delayed alloc extent */
5112 int whichfork
; /* data or attribute fork */
5114 xfs_filblks_t len
= *rlen
; /* length to unmap in file */
5115 xfs_fileoff_t max_len
;
5116 xfs_agnumber_t prev_agno
= NULLAGNUMBER
, agno
;
5119 trace_xfs_bunmap(ip
, start
, len
, flags
, _RET_IP_
);
5121 whichfork
= xfs_bmapi_whichfork(flags
);
5122 ASSERT(whichfork
!= XFS_COW_FORK
);
5123 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5125 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
5126 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)) {
5127 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW
,
5129 return -EFSCORRUPTED
;
5132 if (XFS_FORCED_SHUTDOWN(mp
))
5135 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
5140 * Guesstimate how many blocks we can unmap without running the risk of
5141 * blowing out the transaction with a mix of EFIs and reflink
5144 if (xfs_is_reflink_inode(ip
) && whichfork
== XFS_DATA_FORK
)
5145 max_len
= min(len
, xfs_refcount_max_unmap(tp
->t_log_res
));
5149 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
5150 (error
= xfs_iread_extents(tp
, ip
, whichfork
)))
5152 if (xfs_iext_count(ifp
) == 0) {
5156 XFS_STATS_INC(mp
, xs_blk_unmap
);
5157 isrt
= (whichfork
== XFS_DATA_FORK
) && XFS_IS_REALTIME_INODE(ip
);
5158 end
= start
+ len
- 1;
5161 * Check to see if the given block number is past the end of the
5162 * file, back up to the last block if so...
5164 if (!xfs_iext_lookup_extent(ip
, ifp
, end
, &lastx
, &got
)) {
5166 xfs_iext_get_extent(ifp
, --lastx
, &got
);
5167 end
= got
.br_startoff
+ got
.br_blockcount
- 1;
5171 if (ifp
->if_flags
& XFS_IFBROOT
) {
5172 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
);
5173 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
5174 cur
->bc_private
.b
.firstblock
= *firstblock
;
5175 cur
->bc_private
.b
.dfops
= dfops
;
5176 cur
->bc_private
.b
.flags
= 0;
5182 * Synchronize by locking the bitmap inode.
5184 xfs_ilock(mp
->m_rbmip
, XFS_ILOCK_EXCL
|XFS_ILOCK_RTBITMAP
);
5185 xfs_trans_ijoin(tp
, mp
->m_rbmip
, XFS_ILOCK_EXCL
);
5186 xfs_ilock(mp
->m_rsumip
, XFS_ILOCK_EXCL
|XFS_ILOCK_RTSUM
);
5187 xfs_trans_ijoin(tp
, mp
->m_rsumip
, XFS_ILOCK_EXCL
);
5191 while (end
!= (xfs_fileoff_t
)-1 && end
>= start
&& lastx
>= 0 &&
5192 (nexts
== 0 || extno
< nexts
) && max_len
> 0) {
5194 * Is the found extent after a hole in which end lives?
5195 * Just back up to the previous extent, if so.
5197 if (got
.br_startoff
> end
) {
5200 xfs_iext_get_extent(ifp
, lastx
, &got
);
5203 * Is the last block of this extent before the range
5204 * we're supposed to delete? If so, we're done.
5206 end
= XFS_FILEOFF_MIN(end
,
5207 got
.br_startoff
+ got
.br_blockcount
- 1);
5211 * Then deal with the (possibly delayed) allocated space
5215 wasdel
= isnullstartblock(del
.br_startblock
);
5218 * Make sure we don't touch multiple AGF headers out of order
5219 * in a single transaction, as that could cause AB-BA deadlocks.
5222 agno
= XFS_FSB_TO_AGNO(mp
, del
.br_startblock
);
5223 if (prev_agno
!= NULLAGNUMBER
&& prev_agno
> agno
)
5227 if (got
.br_startoff
< start
) {
5228 del
.br_startoff
= start
;
5229 del
.br_blockcount
-= start
- got
.br_startoff
;
5231 del
.br_startblock
+= start
- got
.br_startoff
;
5233 if (del
.br_startoff
+ del
.br_blockcount
> end
+ 1)
5234 del
.br_blockcount
= end
+ 1 - del
.br_startoff
;
5236 /* How much can we safely unmap? */
5237 if (max_len
< del
.br_blockcount
) {
5238 del
.br_startoff
+= del
.br_blockcount
- max_len
;
5240 del
.br_startblock
+= del
.br_blockcount
- max_len
;
5241 del
.br_blockcount
= max_len
;
5244 sum
= del
.br_startblock
+ del
.br_blockcount
;
5246 (mod
= do_mod(sum
, mp
->m_sb
.sb_rextsize
))) {
5248 * Realtime extent not lined up at the end.
5249 * The extent could have been split into written
5250 * and unwritten pieces, or we could just be
5251 * unmapping part of it. But we can't really
5252 * get rid of part of a realtime extent.
5254 if (del
.br_state
== XFS_EXT_UNWRITTEN
||
5255 !xfs_sb_version_hasextflgbit(&mp
->m_sb
)) {
5257 * This piece is unwritten, or we're not
5258 * using unwritten extents. Skip over it.
5261 end
-= mod
> del
.br_blockcount
?
5262 del
.br_blockcount
: mod
;
5263 if (end
< got
.br_startoff
) {
5265 xfs_iext_get_extent(ifp
, lastx
,
5271 * It's written, turn it unwritten.
5272 * This is better than zeroing it.
5274 ASSERT(del
.br_state
== XFS_EXT_NORM
);
5275 ASSERT(tp
->t_blk_res
> 0);
5277 * If this spans a realtime extent boundary,
5278 * chop it back to the start of the one we end at.
5280 if (del
.br_blockcount
> mod
) {
5281 del
.br_startoff
+= del
.br_blockcount
- mod
;
5282 del
.br_startblock
+= del
.br_blockcount
- mod
;
5283 del
.br_blockcount
= mod
;
5285 del
.br_state
= XFS_EXT_UNWRITTEN
;
5286 error
= xfs_bmap_add_extent_unwritten_real(tp
, ip
,
5287 whichfork
, &lastx
, &cur
, &del
,
5288 firstblock
, dfops
, &logflags
);
5293 if (isrt
&& (mod
= do_mod(del
.br_startblock
, mp
->m_sb
.sb_rextsize
))) {
5295 * Realtime extent is lined up at the end but not
5296 * at the front. We'll get rid of full extents if
5299 mod
= mp
->m_sb
.sb_rextsize
- mod
;
5300 if (del
.br_blockcount
> mod
) {
5301 del
.br_blockcount
-= mod
;
5302 del
.br_startoff
+= mod
;
5303 del
.br_startblock
+= mod
;
5304 } else if ((del
.br_startoff
== start
&&
5305 (del
.br_state
== XFS_EXT_UNWRITTEN
||
5306 tp
->t_blk_res
== 0)) ||
5307 !xfs_sb_version_hasextflgbit(&mp
->m_sb
)) {
5309 * Can't make it unwritten. There isn't
5310 * a full extent here so just skip it.
5312 ASSERT(end
>= del
.br_blockcount
);
5313 end
-= del
.br_blockcount
;
5314 if (got
.br_startoff
> end
&& --lastx
>= 0)
5315 xfs_iext_get_extent(ifp
, lastx
, &got
);
5317 } else if (del
.br_state
== XFS_EXT_UNWRITTEN
) {
5318 struct xfs_bmbt_irec prev
;
5321 * This one is already unwritten.
5322 * It must have a written left neighbor.
5323 * Unwrite the killed part of that one and
5327 xfs_iext_get_extent(ifp
, lastx
- 1, &prev
);
5328 ASSERT(prev
.br_state
== XFS_EXT_NORM
);
5329 ASSERT(!isnullstartblock(prev
.br_startblock
));
5330 ASSERT(del
.br_startblock
==
5331 prev
.br_startblock
+ prev
.br_blockcount
);
5332 if (prev
.br_startoff
< start
) {
5333 mod
= start
- prev
.br_startoff
;
5334 prev
.br_blockcount
-= mod
;
5335 prev
.br_startblock
+= mod
;
5336 prev
.br_startoff
= start
;
5338 prev
.br_state
= XFS_EXT_UNWRITTEN
;
5340 error
= xfs_bmap_add_extent_unwritten_real(tp
,
5341 ip
, whichfork
, &lastx
, &cur
,
5342 &prev
, firstblock
, dfops
,
5348 ASSERT(del
.br_state
== XFS_EXT_NORM
);
5349 del
.br_state
= XFS_EXT_UNWRITTEN
;
5350 error
= xfs_bmap_add_extent_unwritten_real(tp
,
5351 ip
, whichfork
, &lastx
, &cur
,
5352 &del
, firstblock
, dfops
,
5361 error
= xfs_bmap_del_extent_delay(ip
, whichfork
, &lastx
,
5364 error
= xfs_bmap_del_extent_real(ip
, tp
, &lastx
, dfops
,
5365 cur
, &del
, &tmp_logflags
, whichfork
,
5367 logflags
|= tmp_logflags
;
5373 max_len
-= del
.br_blockcount
;
5374 end
= del
.br_startoff
- 1;
5377 * If not done go on to the next (previous) record.
5379 if (end
!= (xfs_fileoff_t
)-1 && end
>= start
) {
5381 xfs_iext_get_extent(ifp
, lastx
, &got
);
5382 if (got
.br_startoff
> end
&& --lastx
>= 0)
5383 xfs_iext_get_extent(ifp
, lastx
, &got
);
5388 if (end
== (xfs_fileoff_t
)-1 || end
< start
|| lastx
< 0)
5391 *rlen
= end
- start
+ 1;
5394 * Convert to a btree if necessary.
5396 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
5397 ASSERT(cur
== NULL
);
5398 error
= xfs_bmap_extents_to_btree(tp
, ip
, firstblock
, dfops
,
5399 &cur
, 0, &tmp_logflags
, whichfork
);
5400 logflags
|= tmp_logflags
;
5405 * transform from btree to extents, give it cur
5407 else if (xfs_bmap_wants_extents(ip
, whichfork
)) {
5408 ASSERT(cur
!= NULL
);
5409 error
= xfs_bmap_btree_to_extents(tp
, ip
, cur
, &tmp_logflags
,
5411 logflags
|= tmp_logflags
;
5416 * transform from extents to local?
5421 * Log everything. Do this after conversion, there's no point in
5422 * logging the extent records if we've converted to btree format.
5424 if ((logflags
& xfs_ilog_fext(whichfork
)) &&
5425 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
5426 logflags
&= ~xfs_ilog_fext(whichfork
);
5427 else if ((logflags
& xfs_ilog_fbroot(whichfork
)) &&
5428 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)
5429 logflags
&= ~xfs_ilog_fbroot(whichfork
);
5431 * Log inode even in the error case, if the transaction
5432 * is dirty we'll need to shut down the filesystem.
5435 xfs_trans_log_inode(tp
, ip
, logflags
);
5438 *firstblock
= cur
->bc_private
.b
.firstblock
;
5439 cur
->bc_private
.b
.allocated
= 0;
5441 xfs_btree_del_cursor(cur
,
5442 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
5447 /* Unmap a range of a file. */
5451 struct xfs_inode
*ip
,
5456 xfs_fsblock_t
*firstblock
,
5457 struct xfs_defer_ops
*dfops
,
5462 error
= __xfs_bunmapi(tp
, ip
, bno
, &len
, flags
, nexts
, firstblock
,
5469 * Determine whether an extent shift can be accomplished by a merge with the
5470 * extent that precedes the target hole of the shift.
5474 struct xfs_bmbt_irec
*left
, /* preceding extent */
5475 struct xfs_bmbt_irec
*got
, /* current extent to shift */
5476 xfs_fileoff_t shift
) /* shift fsb */
5478 xfs_fileoff_t startoff
;
5480 startoff
= got
->br_startoff
- shift
;
5483 * The extent, once shifted, must be adjacent in-file and on-disk with
5484 * the preceding extent.
5486 if ((left
->br_startoff
+ left
->br_blockcount
!= startoff
) ||
5487 (left
->br_startblock
+ left
->br_blockcount
!= got
->br_startblock
) ||
5488 (left
->br_state
!= got
->br_state
) ||
5489 (left
->br_blockcount
+ got
->br_blockcount
> MAXEXTLEN
))
5496 * A bmap extent shift adjusts the file offset of an extent to fill a preceding
5497 * hole in the file. If an extent shift would result in the extent being fully
5498 * adjacent to the extent that currently precedes the hole, we can merge with
5499 * the preceding extent rather than do the shift.
5501 * This function assumes the caller has verified a shift-by-merge is possible
5502 * with the provided extents via xfs_bmse_can_merge().
5506 struct xfs_inode
*ip
,
5508 xfs_fileoff_t shift
, /* shift fsb */
5509 int current_ext
, /* idx of gotp */
5510 struct xfs_bmbt_irec
*got
, /* extent to shift */
5511 struct xfs_bmbt_irec
*left
, /* preceding extent */
5512 struct xfs_btree_cur
*cur
,
5513 int *logflags
, /* output */
5514 struct xfs_defer_ops
*dfops
)
5516 struct xfs_bmbt_irec
new;
5517 xfs_filblks_t blockcount
;
5519 struct xfs_mount
*mp
= ip
->i_mount
;
5521 blockcount
= left
->br_blockcount
+ got
->br_blockcount
;
5523 ASSERT(xfs_isilocked(ip
, XFS_IOLOCK_EXCL
));
5524 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
5525 ASSERT(xfs_bmse_can_merge(left
, got
, shift
));
5528 new.br_blockcount
= blockcount
;
5531 * Update the on-disk extent count, the btree if necessary and log the
5534 XFS_IFORK_NEXT_SET(ip
, whichfork
,
5535 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
5536 *logflags
|= XFS_ILOG_CORE
;
5538 *logflags
|= XFS_ILOG_DEXT
;
5542 /* lookup and remove the extent to merge */
5543 error
= xfs_bmbt_lookup_eq(cur
, got
, &i
);
5546 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
5548 error
= xfs_btree_delete(cur
, &i
);
5551 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
5553 /* lookup and update size of the previous extent */
5554 error
= xfs_bmbt_lookup_eq(cur
, left
, &i
);
5557 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
5559 error
= xfs_bmbt_update(cur
, &new);
5564 xfs_iext_update_extent(ip
, xfs_bmap_fork_to_state(whichfork
),
5565 current_ext
- 1, &new);
5566 xfs_iext_remove(ip
, current_ext
, 1, 0);
5568 /* update reverse mapping. rmap functions merge the rmaps for us */
5569 error
= xfs_rmap_unmap_extent(mp
, dfops
, ip
, whichfork
, got
);
5572 memcpy(&new, got
, sizeof(new));
5573 new.br_startoff
= left
->br_startoff
+ left
->br_blockcount
;
5574 return xfs_rmap_map_extent(mp
, dfops
, ip
, whichfork
, &new);
5578 * Shift a single extent.
5582 struct xfs_inode
*ip
,
5584 xfs_fileoff_t offset_shift_fsb
,
5586 struct xfs_bmbt_irec
*got
,
5587 struct xfs_btree_cur
*cur
,
5589 enum shift_direction direction
,
5590 struct xfs_defer_ops
*dfops
)
5592 struct xfs_ifork
*ifp
;
5593 struct xfs_mount
*mp
;
5594 xfs_fileoff_t startoff
;
5595 struct xfs_bmbt_irec adj_irec
, new;
5601 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5602 total_extents
= xfs_iext_count(ifp
);
5604 /* delalloc extents should be prevented by caller */
5605 XFS_WANT_CORRUPTED_RETURN(mp
, !isnullstartblock(got
->br_startblock
));
5607 if (direction
== SHIFT_LEFT
) {
5608 startoff
= got
->br_startoff
- offset_shift_fsb
;
5611 * Check for merge if we've got an extent to the left,
5612 * otherwise make sure there's enough room at the start
5613 * of the file for the shift.
5615 if (!*current_ext
) {
5616 if (got
->br_startoff
< offset_shift_fsb
)
5618 goto update_current_ext
;
5622 * grab the left extent and check for a large enough hole.
5624 xfs_iext_get_extent(ifp
, *current_ext
- 1, &adj_irec
);
5625 if (startoff
< adj_irec
.br_startoff
+ adj_irec
.br_blockcount
)
5628 /* check whether to merge the extent or shift it down */
5629 if (xfs_bmse_can_merge(&adj_irec
, got
, offset_shift_fsb
)) {
5630 return xfs_bmse_merge(ip
, whichfork
, offset_shift_fsb
,
5631 *current_ext
, got
, &adj_irec
,
5632 cur
, logflags
, dfops
);
5635 startoff
= got
->br_startoff
+ offset_shift_fsb
;
5636 /* nothing to move if this is the last extent */
5637 if (*current_ext
>= (total_extents
- 1))
5638 goto update_current_ext
;
5641 * If this is not the last extent in the file, make sure there
5642 * is enough room between current extent and next extent for
5643 * accommodating the shift.
5645 xfs_iext_get_extent(ifp
, *current_ext
+ 1, &adj_irec
);
5646 if (startoff
+ got
->br_blockcount
> adj_irec
.br_startoff
)
5650 * Unlike a left shift (which involves a hole punch),
5651 * a right shift does not modify extent neighbors
5652 * in any way. We should never find mergeable extents
5653 * in this scenario. Check anyways and warn if we
5654 * encounter two extents that could be one.
5656 if (xfs_bmse_can_merge(got
, &adj_irec
, offset_shift_fsb
))
5661 * Increment the extent index for the next iteration, update the start
5662 * offset of the in-core extent and update the btree if applicable.
5665 *logflags
|= XFS_ILOG_CORE
;
5668 new.br_startoff
= startoff
;
5671 error
= xfs_bmbt_lookup_eq(cur
, got
, &i
);
5674 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
5676 error
= xfs_bmbt_update(cur
, &new);
5680 *logflags
|= XFS_ILOG_DEXT
;
5683 xfs_iext_update_extent(ip
, xfs_bmap_fork_to_state(whichfork
),
5684 *current_ext
, &new);
5686 if (direction
== SHIFT_LEFT
)
5691 /* update reverse mapping */
5692 error
= xfs_rmap_unmap_extent(mp
, dfops
, ip
, whichfork
, got
);
5695 return xfs_rmap_map_extent(mp
, dfops
, ip
, whichfork
, &new);
5699 * Shift extent records to the left/right to cover/create a hole.
5701 * The maximum number of extents to be shifted in a single operation is
5702 * @num_exts. @stop_fsb specifies the file offset at which to stop shift and the
5703 * file offset where we've left off is returned in @next_fsb. @offset_shift_fsb
5704 * is the length by which each extent is shifted. If there is no hole to shift
5705 * the extents into, this will be considered invalid operation and we abort
5709 xfs_bmap_shift_extents(
5710 struct xfs_trans
*tp
,
5711 struct xfs_inode
*ip
,
5712 xfs_fileoff_t
*next_fsb
,
5713 xfs_fileoff_t offset_shift_fsb
,
5715 xfs_fileoff_t stop_fsb
,
5716 xfs_fsblock_t
*firstblock
,
5717 struct xfs_defer_ops
*dfops
,
5718 enum shift_direction direction
,
5721 struct xfs_btree_cur
*cur
= NULL
;
5722 struct xfs_bmbt_irec got
;
5723 struct xfs_mount
*mp
= ip
->i_mount
;
5724 struct xfs_ifork
*ifp
;
5725 xfs_extnum_t nexts
= 0;
5726 xfs_extnum_t current_ext
;
5727 xfs_extnum_t total_extents
;
5728 xfs_extnum_t stop_extent
;
5730 int whichfork
= XFS_DATA_FORK
;
5733 if (unlikely(XFS_TEST_ERROR(
5734 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
5735 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
5736 mp
, XFS_ERRTAG_BMAPIFORMAT
))) {
5737 XFS_ERROR_REPORT("xfs_bmap_shift_extents",
5738 XFS_ERRLEVEL_LOW
, mp
);
5739 return -EFSCORRUPTED
;
5742 if (XFS_FORCED_SHUTDOWN(mp
))
5745 ASSERT(xfs_isilocked(ip
, XFS_IOLOCK_EXCL
));
5746 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
5747 ASSERT(direction
== SHIFT_LEFT
|| direction
== SHIFT_RIGHT
);
5749 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5750 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
5751 /* Read in all the extents */
5752 error
= xfs_iread_extents(tp
, ip
, whichfork
);
5757 if (ifp
->if_flags
& XFS_IFBROOT
) {
5758 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
5759 cur
->bc_private
.b
.firstblock
= *firstblock
;
5760 cur
->bc_private
.b
.dfops
= dfops
;
5761 cur
->bc_private
.b
.flags
= 0;
5765 * There may be delalloc extents in the data fork before the range we
5766 * are collapsing out, so we cannot use the count of real extents here.
5767 * Instead we have to calculate it from the incore fork.
5769 total_extents
= xfs_iext_count(ifp
);
5770 if (total_extents
== 0) {
5776 * In case of first right shift, we need to initialize next_fsb
5778 if (*next_fsb
== NULLFSBLOCK
) {
5779 ASSERT(direction
== SHIFT_RIGHT
);
5781 current_ext
= total_extents
- 1;
5782 xfs_iext_get_extent(ifp
, current_ext
, &got
);
5783 if (stop_fsb
> got
.br_startoff
) {
5787 *next_fsb
= got
.br_startoff
;
5790 * Look up the extent index for the fsb where we start shifting. We can
5791 * henceforth iterate with current_ext as extent list changes are locked
5794 * If next_fsb lies in a hole beyond which there are no extents we are
5797 if (!xfs_iext_lookup_extent(ip
, ifp
, *next_fsb
, ¤t_ext
,
5804 /* Lookup the extent index at which we have to stop */
5805 if (direction
== SHIFT_RIGHT
) {
5806 struct xfs_bmbt_irec s
;
5808 xfs_iext_lookup_extent(ip
, ifp
, stop_fsb
, &stop_extent
, &s
);
5809 /* Make stop_extent exclusive of shift range */
5811 if (current_ext
<= stop_extent
) {
5816 stop_extent
= total_extents
;
5817 if (current_ext
>= stop_extent
) {
5823 while (nexts
++ < num_exts
) {
5824 error
= xfs_bmse_shift_one(ip
, whichfork
, offset_shift_fsb
,
5825 ¤t_ext
, &got
, cur
, &logflags
,
5830 * If there was an extent merge during the shift, the extent
5831 * count can change. Update the total and grade the next record.
5833 if (direction
== SHIFT_LEFT
) {
5834 total_extents
= xfs_iext_count(ifp
);
5835 stop_extent
= total_extents
;
5838 if (current_ext
== stop_extent
) {
5840 *next_fsb
= NULLFSBLOCK
;
5843 xfs_iext_get_extent(ifp
, current_ext
, &got
);
5847 *next_fsb
= got
.br_startoff
;
5851 xfs_btree_del_cursor(cur
,
5852 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
5855 xfs_trans_log_inode(tp
, ip
, logflags
);
5861 * Splits an extent into two extents at split_fsb block such that it is
5862 * the first block of the current_ext. @current_ext is a target extent
5863 * to be split. @split_fsb is a block where the extents is split.
5864 * If split_fsb lies in a hole or the first block of extents, just return 0.
5867 xfs_bmap_split_extent_at(
5868 struct xfs_trans
*tp
,
5869 struct xfs_inode
*ip
,
5870 xfs_fileoff_t split_fsb
,
5871 xfs_fsblock_t
*firstfsb
,
5872 struct xfs_defer_ops
*dfops
)
5874 int whichfork
= XFS_DATA_FORK
;
5875 struct xfs_btree_cur
*cur
= NULL
;
5876 struct xfs_bmbt_irec got
;
5877 struct xfs_bmbt_irec
new; /* split extent */
5878 struct xfs_mount
*mp
= ip
->i_mount
;
5879 struct xfs_ifork
*ifp
;
5880 xfs_fsblock_t gotblkcnt
; /* new block count for got */
5881 xfs_extnum_t current_ext
;
5886 if (unlikely(XFS_TEST_ERROR(
5887 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
5888 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
5889 mp
, XFS_ERRTAG_BMAPIFORMAT
))) {
5890 XFS_ERROR_REPORT("xfs_bmap_split_extent_at",
5891 XFS_ERRLEVEL_LOW
, mp
);
5892 return -EFSCORRUPTED
;
5895 if (XFS_FORCED_SHUTDOWN(mp
))
5898 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5899 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
5900 /* Read in all the extents */
5901 error
= xfs_iread_extents(tp
, ip
, whichfork
);
5907 * If there are not extents, or split_fsb lies in a hole we are done.
5909 if (!xfs_iext_lookup_extent(ip
, ifp
, split_fsb
, ¤t_ext
, &got
) ||
5910 got
.br_startoff
>= split_fsb
)
5913 gotblkcnt
= split_fsb
- got
.br_startoff
;
5914 new.br_startoff
= split_fsb
;
5915 new.br_startblock
= got
.br_startblock
+ gotblkcnt
;
5916 new.br_blockcount
= got
.br_blockcount
- gotblkcnt
;
5917 new.br_state
= got
.br_state
;
5919 if (ifp
->if_flags
& XFS_IFBROOT
) {
5920 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
5921 cur
->bc_private
.b
.firstblock
= *firstfsb
;
5922 cur
->bc_private
.b
.dfops
= dfops
;
5923 cur
->bc_private
.b
.flags
= 0;
5924 error
= xfs_bmbt_lookup_eq(cur
, &got
, &i
);
5927 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, del_cursor
);
5930 got
.br_blockcount
= gotblkcnt
;
5931 xfs_iext_update_extent(ip
, xfs_bmap_fork_to_state(whichfork
),
5934 logflags
= XFS_ILOG_CORE
;
5936 error
= xfs_bmbt_update(cur
, &got
);
5940 logflags
|= XFS_ILOG_DEXT
;
5942 /* Add new extent */
5944 xfs_iext_insert(ip
, current_ext
, 1, &new, 0);
5945 XFS_IFORK_NEXT_SET(ip
, whichfork
,
5946 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
5949 error
= xfs_bmbt_lookup_eq(cur
, &new, &i
);
5952 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, del_cursor
);
5953 error
= xfs_btree_insert(cur
, &i
);
5956 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, del_cursor
);
5960 * Convert to a btree if necessary.
5962 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
5963 int tmp_logflags
; /* partial log flag return val */
5965 ASSERT(cur
== NULL
);
5966 error
= xfs_bmap_extents_to_btree(tp
, ip
, firstfsb
, dfops
,
5967 &cur
, 0, &tmp_logflags
, whichfork
);
5968 logflags
|= tmp_logflags
;
5973 cur
->bc_private
.b
.allocated
= 0;
5974 xfs_btree_del_cursor(cur
,
5975 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
5979 xfs_trans_log_inode(tp
, ip
, logflags
);
5984 xfs_bmap_split_extent(
5985 struct xfs_inode
*ip
,
5986 xfs_fileoff_t split_fsb
)
5988 struct xfs_mount
*mp
= ip
->i_mount
;
5989 struct xfs_trans
*tp
;
5990 struct xfs_defer_ops dfops
;
5991 xfs_fsblock_t firstfsb
;
5994 error
= xfs_trans_alloc(mp
, &M_RES(mp
)->tr_write
,
5995 XFS_DIOSTRAT_SPACE_RES(mp
, 0), 0, 0, &tp
);
5999 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
6000 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
);
6002 xfs_defer_init(&dfops
, &firstfsb
);
6004 error
= xfs_bmap_split_extent_at(tp
, ip
, split_fsb
,
6009 error
= xfs_defer_finish(&tp
, &dfops
);
6013 return xfs_trans_commit(tp
);
6016 xfs_defer_cancel(&dfops
);
6017 xfs_trans_cancel(tp
);
6021 /* Deferred mapping is only for real extents in the data fork. */
6023 xfs_bmap_is_update_needed(
6024 struct xfs_bmbt_irec
*bmap
)
6026 return bmap
->br_startblock
!= HOLESTARTBLOCK
&&
6027 bmap
->br_startblock
!= DELAYSTARTBLOCK
;
6030 /* Record a bmap intent. */
6033 struct xfs_mount
*mp
,
6034 struct xfs_defer_ops
*dfops
,
6035 enum xfs_bmap_intent_type type
,
6036 struct xfs_inode
*ip
,
6038 struct xfs_bmbt_irec
*bmap
)
6041 struct xfs_bmap_intent
*bi
;
6043 trace_xfs_bmap_defer(mp
,
6044 XFS_FSB_TO_AGNO(mp
, bmap
->br_startblock
),
6046 XFS_FSB_TO_AGBNO(mp
, bmap
->br_startblock
),
6047 ip
->i_ino
, whichfork
,
6049 bmap
->br_blockcount
,
6052 bi
= kmem_alloc(sizeof(struct xfs_bmap_intent
), KM_SLEEP
| KM_NOFS
);
6053 INIT_LIST_HEAD(&bi
->bi_list
);
6056 bi
->bi_whichfork
= whichfork
;
6057 bi
->bi_bmap
= *bmap
;
6059 error
= xfs_defer_ijoin(dfops
, bi
->bi_owner
);
6065 xfs_defer_add(dfops
, XFS_DEFER_OPS_TYPE_BMAP
, &bi
->bi_list
);
6069 /* Map an extent into a file. */
6071 xfs_bmap_map_extent(
6072 struct xfs_mount
*mp
,
6073 struct xfs_defer_ops
*dfops
,
6074 struct xfs_inode
*ip
,
6075 struct xfs_bmbt_irec
*PREV
)
6077 if (!xfs_bmap_is_update_needed(PREV
))
6080 return __xfs_bmap_add(mp
, dfops
, XFS_BMAP_MAP
, ip
,
6081 XFS_DATA_FORK
, PREV
);
6084 /* Unmap an extent out of a file. */
6086 xfs_bmap_unmap_extent(
6087 struct xfs_mount
*mp
,
6088 struct xfs_defer_ops
*dfops
,
6089 struct xfs_inode
*ip
,
6090 struct xfs_bmbt_irec
*PREV
)
6092 if (!xfs_bmap_is_update_needed(PREV
))
6095 return __xfs_bmap_add(mp
, dfops
, XFS_BMAP_UNMAP
, ip
,
6096 XFS_DATA_FORK
, PREV
);
6100 * Process one of the deferred bmap operations. We pass back the
6101 * btree cursor to maintain our lock on the bmapbt between calls.
6104 xfs_bmap_finish_one(
6105 struct xfs_trans
*tp
,
6106 struct xfs_defer_ops
*dfops
,
6107 struct xfs_inode
*ip
,
6108 enum xfs_bmap_intent_type type
,
6110 xfs_fileoff_t startoff
,
6111 xfs_fsblock_t startblock
,
6112 xfs_filblks_t
*blockcount
,
6115 xfs_fsblock_t firstfsb
;
6119 * firstfsb is tied to the transaction lifetime and is used to
6120 * ensure correct AG locking order and schedule work item
6121 * continuations. XFS_BUI_MAX_FAST_EXTENTS (== 1) restricts us
6122 * to only making one bmap call per transaction, so it should
6123 * be safe to have it as a local variable here.
6125 firstfsb
= NULLFSBLOCK
;
6127 trace_xfs_bmap_deferred(tp
->t_mountp
,
6128 XFS_FSB_TO_AGNO(tp
->t_mountp
, startblock
), type
,
6129 XFS_FSB_TO_AGBNO(tp
->t_mountp
, startblock
),
6130 ip
->i_ino
, whichfork
, startoff
, *blockcount
, state
);
6132 if (WARN_ON_ONCE(whichfork
!= XFS_DATA_FORK
))
6133 return -EFSCORRUPTED
;
6135 if (XFS_TEST_ERROR(false, tp
->t_mountp
,
6136 XFS_ERRTAG_BMAP_FINISH_ONE
))
6141 error
= xfs_bmapi_remap(tp
, ip
, startoff
, *blockcount
,
6145 case XFS_BMAP_UNMAP
:
6146 error
= __xfs_bunmapi(tp
, ip
, startoff
, blockcount
,
6147 XFS_BMAPI_REMAP
, 1, &firstfsb
, dfops
);
6151 error
= -EFSCORRUPTED
;