2 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 #include "xfs_shared.h"
21 #include "xfs_format.h"
22 #include "xfs_log_format.h"
23 #include "xfs_trans_resv.h"
26 #include "xfs_mount.h"
27 #include "xfs_defer.h"
28 #include "xfs_da_format.h"
29 #include "xfs_da_btree.h"
31 #include "xfs_inode.h"
32 #include "xfs_btree.h"
33 #include "xfs_trans.h"
34 #include "xfs_inode_item.h"
35 #include "xfs_extfree_item.h"
36 #include "xfs_alloc.h"
38 #include "xfs_bmap_util.h"
39 #include "xfs_bmap_btree.h"
40 #include "xfs_rtalloc.h"
41 #include "xfs_errortag.h"
42 #include "xfs_error.h"
43 #include "xfs_quota.h"
44 #include "xfs_trans_space.h"
45 #include "xfs_buf_item.h"
46 #include "xfs_trace.h"
47 #include "xfs_symlink.h"
48 #include "xfs_attr_leaf.h"
49 #include "xfs_filestream.h"
51 #include "xfs_ag_resv.h"
52 #include "xfs_refcount.h"
53 #include "xfs_icache.h"
56 kmem_zone_t
*xfs_bmap_free_item_zone
;
59 * Miscellaneous helper functions
63 * Compute and fill in the value of the maximum depth of a bmap btree
64 * in this filesystem. Done once, during mount.
67 xfs_bmap_compute_maxlevels(
68 xfs_mount_t
*mp
, /* file system mount structure */
69 int whichfork
) /* data or attr fork */
71 int level
; /* btree level */
72 uint maxblocks
; /* max blocks at this level */
73 uint maxleafents
; /* max leaf entries possible */
74 int maxrootrecs
; /* max records in root block */
75 int minleafrecs
; /* min records in leaf block */
76 int minnoderecs
; /* min records in node block */
77 int sz
; /* root block size */
80 * The maximum number of extents in a file, hence the maximum
81 * number of leaf entries, is controlled by the type of di_nextents
82 * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
83 * (a signed 16-bit number, xfs_aextnum_t).
85 * Note that we can no longer assume that if we are in ATTR1 that
86 * the fork offset of all the inodes will be
87 * (xfs_default_attroffset(ip) >> 3) because we could have mounted
88 * with ATTR2 and then mounted back with ATTR1, keeping the
89 * di_forkoff's fixed but probably at various positions. Therefore,
90 * for both ATTR1 and ATTR2 we have to assume the worst case scenario
91 * of a minimum size available.
93 if (whichfork
== XFS_DATA_FORK
) {
94 maxleafents
= MAXEXTNUM
;
95 sz
= XFS_BMDR_SPACE_CALC(MINDBTPTRS
);
97 maxleafents
= MAXAEXTNUM
;
98 sz
= XFS_BMDR_SPACE_CALC(MINABTPTRS
);
100 maxrootrecs
= xfs_bmdr_maxrecs(sz
, 0);
101 minleafrecs
= mp
->m_bmap_dmnr
[0];
102 minnoderecs
= mp
->m_bmap_dmnr
[1];
103 maxblocks
= (maxleafents
+ minleafrecs
- 1) / minleafrecs
;
104 for (level
= 1; maxblocks
> 1; level
++) {
105 if (maxblocks
<= maxrootrecs
)
108 maxblocks
= (maxblocks
+ minnoderecs
- 1) / minnoderecs
;
110 mp
->m_bm_maxlevels
[whichfork
] = level
;
113 STATIC
int /* error */
115 struct xfs_btree_cur
*cur
,
116 struct xfs_bmbt_irec
*irec
,
117 int *stat
) /* success/failure */
119 cur
->bc_rec
.b
= *irec
;
120 return xfs_btree_lookup(cur
, XFS_LOOKUP_EQ
, stat
);
123 STATIC
int /* error */
124 xfs_bmbt_lookup_first(
125 struct xfs_btree_cur
*cur
,
126 int *stat
) /* success/failure */
128 cur
->bc_rec
.b
.br_startoff
= 0;
129 cur
->bc_rec
.b
.br_startblock
= 0;
130 cur
->bc_rec
.b
.br_blockcount
= 0;
131 return xfs_btree_lookup(cur
, XFS_LOOKUP_GE
, stat
);
135 * Check if the inode needs to be converted to btree format.
137 static inline bool xfs_bmap_needs_btree(struct xfs_inode
*ip
, int whichfork
)
139 return whichfork
!= XFS_COW_FORK
&&
140 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
&&
141 XFS_IFORK_NEXTENTS(ip
, whichfork
) >
142 XFS_IFORK_MAXEXT(ip
, whichfork
);
146 * Check if the inode should be converted to extent format.
148 static inline bool xfs_bmap_wants_extents(struct xfs_inode
*ip
, int whichfork
)
150 return whichfork
!= XFS_COW_FORK
&&
151 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
&&
152 XFS_IFORK_NEXTENTS(ip
, whichfork
) <=
153 XFS_IFORK_MAXEXT(ip
, whichfork
);
157 * Update the record referred to by cur to the value given by irec
158 * This either works (return 0) or gets an EFSCORRUPTED error.
162 struct xfs_btree_cur
*cur
,
163 struct xfs_bmbt_irec
*irec
)
165 union xfs_btree_rec rec
;
167 xfs_bmbt_disk_set_all(&rec
.bmbt
, irec
);
168 return xfs_btree_update(cur
, &rec
);
172 * Compute the worst-case number of indirect blocks that will be used
173 * for ip's delayed extent of length "len".
176 xfs_bmap_worst_indlen(
177 xfs_inode_t
*ip
, /* incore inode pointer */
178 xfs_filblks_t len
) /* delayed extent length */
180 int level
; /* btree level number */
181 int maxrecs
; /* maximum record count at this level */
182 xfs_mount_t
*mp
; /* mount structure */
183 xfs_filblks_t rval
; /* return value */
186 maxrecs
= mp
->m_bmap_dmxr
[0];
187 for (level
= 0, rval
= 0;
188 level
< XFS_BM_MAXLEVELS(mp
, XFS_DATA_FORK
);
191 do_div(len
, maxrecs
);
194 return rval
+ XFS_BM_MAXLEVELS(mp
, XFS_DATA_FORK
) -
197 maxrecs
= mp
->m_bmap_dmxr
[1];
203 * Calculate the default attribute fork offset for newly created inodes.
206 xfs_default_attroffset(
207 struct xfs_inode
*ip
)
209 struct xfs_mount
*mp
= ip
->i_mount
;
212 if (mp
->m_sb
.sb_inodesize
== 256) {
213 offset
= XFS_LITINO(mp
, ip
->i_d
.di_version
) -
214 XFS_BMDR_SPACE_CALC(MINABTPTRS
);
216 offset
= XFS_BMDR_SPACE_CALC(6 * MINABTPTRS
);
219 ASSERT(offset
< XFS_LITINO(mp
, ip
->i_d
.di_version
));
224 * Helper routine to reset inode di_forkoff field when switching
225 * attribute fork from local to extent format - we reset it where
226 * possible to make space available for inline data fork extents.
229 xfs_bmap_forkoff_reset(
233 if (whichfork
== XFS_ATTR_FORK
&&
234 ip
->i_d
.di_format
!= XFS_DINODE_FMT_DEV
&&
235 ip
->i_d
.di_format
!= XFS_DINODE_FMT_BTREE
) {
236 uint dfl_forkoff
= xfs_default_attroffset(ip
) >> 3;
238 if (dfl_forkoff
> ip
->i_d
.di_forkoff
)
239 ip
->i_d
.di_forkoff
= dfl_forkoff
;
244 STATIC
struct xfs_buf
*
246 struct xfs_btree_cur
*cur
,
249 struct xfs_log_item_desc
*lidp
;
255 for (i
= 0; i
< XFS_BTREE_MAXLEVELS
; i
++) {
256 if (!cur
->bc_bufs
[i
])
258 if (XFS_BUF_ADDR(cur
->bc_bufs
[i
]) == bno
)
259 return cur
->bc_bufs
[i
];
262 /* Chase down all the log items to see if the bp is there */
263 list_for_each_entry(lidp
, &cur
->bc_tp
->t_items
, lid_trans
) {
264 struct xfs_buf_log_item
*bip
;
265 bip
= (struct xfs_buf_log_item
*)lidp
->lid_item
;
266 if (bip
->bli_item
.li_type
== XFS_LI_BUF
&&
267 XFS_BUF_ADDR(bip
->bli_buf
) == bno
)
276 struct xfs_btree_block
*block
,
282 __be64
*pp
, *thispa
; /* pointer to block address */
283 xfs_bmbt_key_t
*prevp
, *keyp
;
285 ASSERT(be16_to_cpu(block
->bb_level
) > 0);
288 for( i
= 1; i
<= xfs_btree_get_numrecs(block
); i
++) {
289 dmxr
= mp
->m_bmap_dmxr
[0];
290 keyp
= XFS_BMBT_KEY_ADDR(mp
, block
, i
);
293 ASSERT(be64_to_cpu(prevp
->br_startoff
) <
294 be64_to_cpu(keyp
->br_startoff
));
299 * Compare the block numbers to see if there are dups.
302 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, i
, sz
);
304 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, i
, dmxr
);
306 for (j
= i
+1; j
<= be16_to_cpu(block
->bb_numrecs
); j
++) {
308 thispa
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, j
, sz
);
310 thispa
= XFS_BMBT_PTR_ADDR(mp
, block
, j
, dmxr
);
311 if (*thispa
== *pp
) {
312 xfs_warn(mp
, "%s: thispa(%d) == pp(%d) %Ld",
314 (unsigned long long)be64_to_cpu(*thispa
));
315 panic("%s: ptrs are equal in node\n",
323 * Check that the extents for the inode ip are in the right order in all
324 * btree leaves. THis becomes prohibitively expensive for large extent count
325 * files, so don't bother with inodes that have more than 10,000 extents in
326 * them. The btree record ordering checks will still be done, so for such large
327 * bmapbt constructs that is going to catch most corruptions.
330 xfs_bmap_check_leaf_extents(
331 xfs_btree_cur_t
*cur
, /* btree cursor or null */
332 xfs_inode_t
*ip
, /* incore inode pointer */
333 int whichfork
) /* data or attr fork */
335 struct xfs_btree_block
*block
; /* current btree block */
336 xfs_fsblock_t bno
; /* block # of "block" */
337 xfs_buf_t
*bp
; /* buffer for "block" */
338 int error
; /* error return value */
339 xfs_extnum_t i
=0, j
; /* index into the extents list */
340 xfs_ifork_t
*ifp
; /* fork structure */
341 int level
; /* btree level, for checking */
342 xfs_mount_t
*mp
; /* file system mount structure */
343 __be64
*pp
; /* pointer to block address */
344 xfs_bmbt_rec_t
*ep
; /* pointer to current extent */
345 xfs_bmbt_rec_t last
= {0, 0}; /* last extent in prev block */
346 xfs_bmbt_rec_t
*nextp
; /* pointer to next extent */
349 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
) {
353 /* skip large extent count inodes */
354 if (ip
->i_d
.di_nextents
> 10000)
359 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
360 block
= ifp
->if_broot
;
362 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
364 level
= be16_to_cpu(block
->bb_level
);
366 xfs_check_block(block
, mp
, 1, ifp
->if_broot_bytes
);
367 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, 1, ifp
->if_broot_bytes
);
368 bno
= be64_to_cpu(*pp
);
370 ASSERT(bno
!= NULLFSBLOCK
);
371 ASSERT(XFS_FSB_TO_AGNO(mp
, bno
) < mp
->m_sb
.sb_agcount
);
372 ASSERT(XFS_FSB_TO_AGBNO(mp
, bno
) < mp
->m_sb
.sb_agblocks
);
375 * Go down the tree until leaf level is reached, following the first
376 * pointer (leftmost) at each level.
378 while (level
-- > 0) {
379 /* See if buf is in cur first */
381 bp
= xfs_bmap_get_bp(cur
, XFS_FSB_TO_DADDR(mp
, bno
));
384 error
= xfs_btree_read_bufl(mp
, NULL
, bno
, 0, &bp
,
390 block
= XFS_BUF_TO_BLOCK(bp
);
395 * Check this block for basic sanity (increasing keys and
396 * no duplicate blocks).
399 xfs_check_block(block
, mp
, 0, 0);
400 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, mp
->m_bmap_dmxr
[1]);
401 bno
= be64_to_cpu(*pp
);
402 XFS_WANT_CORRUPTED_GOTO(mp
,
403 XFS_FSB_SANITY_CHECK(mp
, bno
), error0
);
406 xfs_trans_brelse(NULL
, bp
);
411 * Here with bp and block set to the leftmost leaf node in the tree.
416 * Loop over all leaf nodes checking that all extents are in the right order.
419 xfs_fsblock_t nextbno
;
420 xfs_extnum_t num_recs
;
423 num_recs
= xfs_btree_get_numrecs(block
);
426 * Read-ahead the next leaf block, if any.
429 nextbno
= be64_to_cpu(block
->bb_u
.l
.bb_rightsib
);
432 * Check all the extents to make sure they are OK.
433 * If we had a previous block, the last entry should
434 * conform with the first entry in this one.
437 ep
= XFS_BMBT_REC_ADDR(mp
, block
, 1);
439 ASSERT(xfs_bmbt_disk_get_startoff(&last
) +
440 xfs_bmbt_disk_get_blockcount(&last
) <=
441 xfs_bmbt_disk_get_startoff(ep
));
443 for (j
= 1; j
< num_recs
; j
++) {
444 nextp
= XFS_BMBT_REC_ADDR(mp
, block
, j
+ 1);
445 ASSERT(xfs_bmbt_disk_get_startoff(ep
) +
446 xfs_bmbt_disk_get_blockcount(ep
) <=
447 xfs_bmbt_disk_get_startoff(nextp
));
455 xfs_trans_brelse(NULL
, bp
);
459 * If we've reached the end, stop.
461 if (bno
== NULLFSBLOCK
)
465 bp
= xfs_bmap_get_bp(cur
, XFS_FSB_TO_DADDR(mp
, bno
));
468 error
= xfs_btree_read_bufl(mp
, NULL
, bno
, 0, &bp
,
474 block
= XFS_BUF_TO_BLOCK(bp
);
480 xfs_warn(mp
, "%s: at error0", __func__
);
482 xfs_trans_brelse(NULL
, bp
);
484 xfs_warn(mp
, "%s: BAD after btree leaves for %d extents",
486 panic("%s: CORRUPTED BTREE OR SOMETHING", __func__
);
491 * Validate that the bmbt_irecs being returned from bmapi are valid
492 * given the caller's original parameters. Specifically check the
493 * ranges of the returned irecs to ensure that they only extend beyond
494 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
497 xfs_bmap_validate_ret(
501 xfs_bmbt_irec_t
*mval
,
505 int i
; /* index to map values */
507 ASSERT(ret_nmap
<= nmap
);
509 for (i
= 0; i
< ret_nmap
; i
++) {
510 ASSERT(mval
[i
].br_blockcount
> 0);
511 if (!(flags
& XFS_BMAPI_ENTIRE
)) {
512 ASSERT(mval
[i
].br_startoff
>= bno
);
513 ASSERT(mval
[i
].br_blockcount
<= len
);
514 ASSERT(mval
[i
].br_startoff
+ mval
[i
].br_blockcount
<=
517 ASSERT(mval
[i
].br_startoff
< bno
+ len
);
518 ASSERT(mval
[i
].br_startoff
+ mval
[i
].br_blockcount
>
522 mval
[i
- 1].br_startoff
+ mval
[i
- 1].br_blockcount
==
523 mval
[i
].br_startoff
);
524 ASSERT(mval
[i
].br_startblock
!= DELAYSTARTBLOCK
&&
525 mval
[i
].br_startblock
!= HOLESTARTBLOCK
);
526 ASSERT(mval
[i
].br_state
== XFS_EXT_NORM
||
527 mval
[i
].br_state
== XFS_EXT_UNWRITTEN
);
532 #define xfs_bmap_check_leaf_extents(cur, ip, whichfork) do { } while (0)
533 #define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap) do { } while (0)
537 * bmap free list manipulation functions
541 * Add the extent to the list of extents to be free at transaction end.
542 * The list is maintained sorted (by block number).
546 struct xfs_mount
*mp
,
547 struct xfs_defer_ops
*dfops
,
550 struct xfs_owner_info
*oinfo
)
552 struct xfs_extent_free_item
*new; /* new element */
557 ASSERT(bno
!= NULLFSBLOCK
);
559 ASSERT(len
<= MAXEXTLEN
);
560 ASSERT(!isnullstartblock(bno
));
561 agno
= XFS_FSB_TO_AGNO(mp
, bno
);
562 agbno
= XFS_FSB_TO_AGBNO(mp
, bno
);
563 ASSERT(agno
< mp
->m_sb
.sb_agcount
);
564 ASSERT(agbno
< mp
->m_sb
.sb_agblocks
);
565 ASSERT(len
< mp
->m_sb
.sb_agblocks
);
566 ASSERT(agbno
+ len
<= mp
->m_sb
.sb_agblocks
);
568 ASSERT(xfs_bmap_free_item_zone
!= NULL
);
570 new = kmem_zone_alloc(xfs_bmap_free_item_zone
, KM_SLEEP
);
571 new->xefi_startblock
= bno
;
572 new->xefi_blockcount
= (xfs_extlen_t
)len
;
574 new->xefi_oinfo
= *oinfo
;
576 xfs_rmap_skip_owner_update(&new->xefi_oinfo
);
577 trace_xfs_bmap_free_defer(mp
, XFS_FSB_TO_AGNO(mp
, bno
), 0,
578 XFS_FSB_TO_AGBNO(mp
, bno
), len
);
579 xfs_defer_add(dfops
, XFS_DEFER_OPS_TYPE_FREE
, &new->xefi_list
);
583 * Inode fork format manipulation functions
587 * Transform a btree format file with only one leaf node, where the
588 * extents list will fit in the inode, into an extents format file.
589 * Since the file extents are already in-core, all we have to do is
590 * give up the space for the btree root and pitch the leaf block.
592 STATIC
int /* error */
593 xfs_bmap_btree_to_extents(
594 xfs_trans_t
*tp
, /* transaction pointer */
595 xfs_inode_t
*ip
, /* incore inode pointer */
596 xfs_btree_cur_t
*cur
, /* btree cursor */
597 int *logflagsp
, /* inode logging flags */
598 int whichfork
) /* data or attr fork */
601 struct xfs_btree_block
*cblock
;/* child btree block */
602 xfs_fsblock_t cbno
; /* child block number */
603 xfs_buf_t
*cbp
; /* child block's buffer */
604 int error
; /* error return value */
605 xfs_ifork_t
*ifp
; /* inode fork data */
606 xfs_mount_t
*mp
; /* mount point structure */
607 __be64
*pp
; /* ptr to block address */
608 struct xfs_btree_block
*rblock
;/* root btree block */
609 struct xfs_owner_info oinfo
;
612 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
613 ASSERT(whichfork
!= XFS_COW_FORK
);
614 ASSERT(ifp
->if_flags
& XFS_IFEXTENTS
);
615 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
);
616 rblock
= ifp
->if_broot
;
617 ASSERT(be16_to_cpu(rblock
->bb_level
) == 1);
618 ASSERT(be16_to_cpu(rblock
->bb_numrecs
) == 1);
619 ASSERT(xfs_bmbt_maxrecs(mp
, ifp
->if_broot_bytes
, 0) == 1);
620 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, rblock
, 1, ifp
->if_broot_bytes
);
621 cbno
= be64_to_cpu(*pp
);
624 XFS_WANT_CORRUPTED_RETURN(cur
->bc_mp
,
625 xfs_btree_check_lptr(cur
, cbno
, 1));
627 error
= xfs_btree_read_bufl(mp
, tp
, cbno
, 0, &cbp
, XFS_BMAP_BTREE_REF
,
631 cblock
= XFS_BUF_TO_BLOCK(cbp
);
632 if ((error
= xfs_btree_check_block(cur
, cblock
, 0, cbp
)))
634 xfs_rmap_ino_bmbt_owner(&oinfo
, ip
->i_ino
, whichfork
);
635 xfs_bmap_add_free(mp
, cur
->bc_private
.b
.dfops
, cbno
, 1, &oinfo
);
636 ip
->i_d
.di_nblocks
--;
637 xfs_trans_mod_dquot_byino(tp
, ip
, XFS_TRANS_DQ_BCOUNT
, -1L);
638 xfs_trans_binval(tp
, cbp
);
639 if (cur
->bc_bufs
[0] == cbp
)
640 cur
->bc_bufs
[0] = NULL
;
641 xfs_iroot_realloc(ip
, -1, whichfork
);
642 ASSERT(ifp
->if_broot
== NULL
);
643 ASSERT((ifp
->if_flags
& XFS_IFBROOT
) == 0);
644 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_EXTENTS
);
645 *logflagsp
= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
650 * Convert an extents-format file into a btree-format file.
651 * The new file will have a root block (in the inode) and a single child block.
653 STATIC
int /* error */
654 xfs_bmap_extents_to_btree(
655 xfs_trans_t
*tp
, /* transaction pointer */
656 xfs_inode_t
*ip
, /* incore inode pointer */
657 xfs_fsblock_t
*firstblock
, /* first-block-allocated */
658 struct xfs_defer_ops
*dfops
, /* blocks freed in xaction */
659 xfs_btree_cur_t
**curp
, /* cursor returned to caller */
660 int wasdel
, /* converting a delayed alloc */
661 int *logflagsp
, /* inode logging flags */
662 int whichfork
) /* data or attr fork */
664 struct xfs_btree_block
*ablock
; /* allocated (child) bt block */
665 xfs_buf_t
*abp
; /* buffer for ablock */
666 xfs_alloc_arg_t args
; /* allocation arguments */
667 xfs_bmbt_rec_t
*arp
; /* child record pointer */
668 struct xfs_btree_block
*block
; /* btree root block */
669 xfs_btree_cur_t
*cur
; /* bmap btree cursor */
670 int error
; /* error return value */
671 xfs_ifork_t
*ifp
; /* inode fork pointer */
672 xfs_bmbt_key_t
*kp
; /* root block key pointer */
673 xfs_mount_t
*mp
; /* mount structure */
674 xfs_bmbt_ptr_t
*pp
; /* root block address pointer */
675 struct xfs_iext_cursor icur
;
676 struct xfs_bmbt_irec rec
;
677 xfs_extnum_t cnt
= 0;
680 ASSERT(whichfork
!= XFS_COW_FORK
);
681 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
682 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
);
685 * Make space in the inode incore.
687 xfs_iroot_realloc(ip
, 1, whichfork
);
688 ifp
->if_flags
|= XFS_IFBROOT
;
693 block
= ifp
->if_broot
;
694 xfs_btree_init_block_int(mp
, block
, XFS_BUF_DADDR_NULL
,
695 XFS_BTNUM_BMAP
, 1, 1, ip
->i_ino
,
696 XFS_BTREE_LONG_PTRS
);
698 * Need a cursor. Can't allocate until bb_level is filled in.
700 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
701 cur
->bc_private
.b
.firstblock
= *firstblock
;
702 cur
->bc_private
.b
.dfops
= dfops
;
703 cur
->bc_private
.b
.flags
= wasdel
? XFS_BTCUR_BPRV_WASDEL
: 0;
705 * Convert to a btree with two levels, one record in root.
707 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_BTREE
);
708 memset(&args
, 0, sizeof(args
));
711 xfs_rmap_ino_bmbt_owner(&args
.oinfo
, ip
->i_ino
, whichfork
);
712 args
.firstblock
= *firstblock
;
713 if (*firstblock
== NULLFSBLOCK
) {
714 args
.type
= XFS_ALLOCTYPE_START_BNO
;
715 args
.fsbno
= XFS_INO_TO_FSB(mp
, ip
->i_ino
);
716 } else if (dfops
->dop_low
) {
717 args
.type
= XFS_ALLOCTYPE_START_BNO
;
718 args
.fsbno
= *firstblock
;
720 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
721 args
.fsbno
= *firstblock
;
723 args
.minlen
= args
.maxlen
= args
.prod
= 1;
724 args
.wasdel
= wasdel
;
726 if ((error
= xfs_alloc_vextent(&args
))) {
727 xfs_iroot_realloc(ip
, -1, whichfork
);
728 ASSERT(ifp
->if_broot
== NULL
);
729 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_EXTENTS
);
730 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
734 if (WARN_ON_ONCE(args
.fsbno
== NULLFSBLOCK
)) {
735 xfs_iroot_realloc(ip
, -1, whichfork
);
736 ASSERT(ifp
->if_broot
== NULL
);
737 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_EXTENTS
);
738 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
742 * Allocation can't fail, the space was reserved.
744 ASSERT(*firstblock
== NULLFSBLOCK
||
745 args
.agno
>= XFS_FSB_TO_AGNO(mp
, *firstblock
));
746 *firstblock
= cur
->bc_private
.b
.firstblock
= args
.fsbno
;
747 cur
->bc_private
.b
.allocated
++;
748 ip
->i_d
.di_nblocks
++;
749 xfs_trans_mod_dquot_byino(tp
, ip
, XFS_TRANS_DQ_BCOUNT
, 1L);
750 abp
= xfs_btree_get_bufl(mp
, tp
, args
.fsbno
, 0);
752 * Fill in the child block.
754 abp
->b_ops
= &xfs_bmbt_buf_ops
;
755 ablock
= XFS_BUF_TO_BLOCK(abp
);
756 xfs_btree_init_block_int(mp
, ablock
, abp
->b_bn
,
757 XFS_BTNUM_BMAP
, 0, 0, ip
->i_ino
,
758 XFS_BTREE_LONG_PTRS
);
760 for_each_xfs_iext(ifp
, &icur
, &rec
) {
761 if (isnullstartblock(rec
.br_startblock
))
763 arp
= XFS_BMBT_REC_ADDR(mp
, ablock
, 1 + cnt
);
764 xfs_bmbt_disk_set_all(arp
, &rec
);
767 ASSERT(cnt
== XFS_IFORK_NEXTENTS(ip
, whichfork
));
768 xfs_btree_set_numrecs(ablock
, cnt
);
771 * Fill in the root key and pointer.
773 kp
= XFS_BMBT_KEY_ADDR(mp
, block
, 1);
774 arp
= XFS_BMBT_REC_ADDR(mp
, ablock
, 1);
775 kp
->br_startoff
= cpu_to_be64(xfs_bmbt_disk_get_startoff(arp
));
776 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, xfs_bmbt_get_maxrecs(cur
,
777 be16_to_cpu(block
->bb_level
)));
778 *pp
= cpu_to_be64(args
.fsbno
);
781 * Do all this logging at the end so that
782 * the root is at the right level.
784 xfs_btree_log_block(cur
, abp
, XFS_BB_ALL_BITS
);
785 xfs_btree_log_recs(cur
, abp
, 1, be16_to_cpu(ablock
->bb_numrecs
));
786 ASSERT(*curp
== NULL
);
788 *logflagsp
= XFS_ILOG_CORE
| xfs_ilog_fbroot(whichfork
);
793 * Convert a local file to an extents file.
794 * This code is out of bounds for data forks of regular files,
795 * since the file data needs to get logged so things will stay consistent.
796 * (The bmap-level manipulations are ok, though).
799 xfs_bmap_local_to_extents_empty(
800 struct xfs_inode
*ip
,
803 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
805 ASSERT(whichfork
!= XFS_COW_FORK
);
806 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
807 ASSERT(ifp
->if_bytes
== 0);
808 ASSERT(XFS_IFORK_NEXTENTS(ip
, whichfork
) == 0);
810 xfs_bmap_forkoff_reset(ip
, whichfork
);
811 ifp
->if_flags
&= ~XFS_IFINLINE
;
812 ifp
->if_flags
|= XFS_IFEXTENTS
;
813 ifp
->if_u1
.if_root
= NULL
;
815 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_EXTENTS
);
819 STATIC
int /* error */
820 xfs_bmap_local_to_extents(
821 xfs_trans_t
*tp
, /* transaction pointer */
822 xfs_inode_t
*ip
, /* incore inode pointer */
823 xfs_fsblock_t
*firstblock
, /* first block allocated in xaction */
824 xfs_extlen_t total
, /* total blocks needed by transaction */
825 int *logflagsp
, /* inode logging flags */
827 void (*init_fn
)(struct xfs_trans
*tp
,
829 struct xfs_inode
*ip
,
830 struct xfs_ifork
*ifp
))
833 int flags
; /* logging flags returned */
834 xfs_ifork_t
*ifp
; /* inode fork pointer */
835 xfs_alloc_arg_t args
; /* allocation arguments */
836 xfs_buf_t
*bp
; /* buffer for extent block */
837 struct xfs_bmbt_irec rec
;
838 struct xfs_iext_cursor icur
;
841 * We don't want to deal with the case of keeping inode data inline yet.
842 * So sending the data fork of a regular inode is invalid.
844 ASSERT(!(S_ISREG(VFS_I(ip
)->i_mode
) && whichfork
== XFS_DATA_FORK
));
845 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
846 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
848 if (!ifp
->if_bytes
) {
849 xfs_bmap_local_to_extents_empty(ip
, whichfork
);
850 flags
= XFS_ILOG_CORE
;
856 ASSERT((ifp
->if_flags
& (XFS_IFINLINE
|XFS_IFEXTENTS
)) == XFS_IFINLINE
);
857 memset(&args
, 0, sizeof(args
));
859 args
.mp
= ip
->i_mount
;
860 xfs_rmap_ino_owner(&args
.oinfo
, ip
->i_ino
, whichfork
, 0);
861 args
.firstblock
= *firstblock
;
863 * Allocate a block. We know we need only one, since the
864 * file currently fits in an inode.
866 if (*firstblock
== NULLFSBLOCK
) {
867 args
.fsbno
= XFS_INO_TO_FSB(args
.mp
, ip
->i_ino
);
868 args
.type
= XFS_ALLOCTYPE_START_BNO
;
870 args
.fsbno
= *firstblock
;
871 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
874 args
.minlen
= args
.maxlen
= args
.prod
= 1;
875 error
= xfs_alloc_vextent(&args
);
879 /* Can't fail, the space was reserved. */
880 ASSERT(args
.fsbno
!= NULLFSBLOCK
);
881 ASSERT(args
.len
== 1);
882 *firstblock
= args
.fsbno
;
883 bp
= xfs_btree_get_bufl(args
.mp
, tp
, args
.fsbno
, 0);
886 * Initialize the block, copy the data and log the remote buffer.
888 * The callout is responsible for logging because the remote format
889 * might differ from the local format and thus we don't know how much to
890 * log here. Note that init_fn must also set the buffer log item type
893 init_fn(tp
, bp
, ip
, ifp
);
895 /* account for the change in fork size */
896 xfs_idata_realloc(ip
, -ifp
->if_bytes
, whichfork
);
897 xfs_bmap_local_to_extents_empty(ip
, whichfork
);
898 flags
|= XFS_ILOG_CORE
;
900 ifp
->if_u1
.if_root
= NULL
;
904 rec
.br_startblock
= args
.fsbno
;
905 rec
.br_blockcount
= 1;
906 rec
.br_state
= XFS_EXT_NORM
;
907 xfs_iext_first(ifp
, &icur
);
908 xfs_iext_insert(ip
, &icur
, &rec
, 0);
910 XFS_IFORK_NEXT_SET(ip
, whichfork
, 1);
911 ip
->i_d
.di_nblocks
= 1;
912 xfs_trans_mod_dquot_byino(tp
, ip
,
913 XFS_TRANS_DQ_BCOUNT
, 1L);
914 flags
|= xfs_ilog_fext(whichfork
);
922 * Called from xfs_bmap_add_attrfork to handle btree format files.
924 STATIC
int /* error */
925 xfs_bmap_add_attrfork_btree(
926 xfs_trans_t
*tp
, /* transaction pointer */
927 xfs_inode_t
*ip
, /* incore inode pointer */
928 xfs_fsblock_t
*firstblock
, /* first block allocated */
929 struct xfs_defer_ops
*dfops
, /* blocks to free at commit */
930 int *flags
) /* inode logging flags */
932 xfs_btree_cur_t
*cur
; /* btree cursor */
933 int error
; /* error return value */
934 xfs_mount_t
*mp
; /* file system mount struct */
935 int stat
; /* newroot status */
938 if (ip
->i_df
.if_broot_bytes
<= XFS_IFORK_DSIZE(ip
))
939 *flags
|= XFS_ILOG_DBROOT
;
941 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, XFS_DATA_FORK
);
942 cur
->bc_private
.b
.dfops
= dfops
;
943 cur
->bc_private
.b
.firstblock
= *firstblock
;
944 error
= xfs_bmbt_lookup_first(cur
, &stat
);
947 /* must be at least one entry */
948 XFS_WANT_CORRUPTED_GOTO(mp
, stat
== 1, error0
);
949 if ((error
= xfs_btree_new_iroot(cur
, flags
, &stat
)))
952 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
955 *firstblock
= cur
->bc_private
.b
.firstblock
;
956 cur
->bc_private
.b
.allocated
= 0;
957 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
961 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
966 * Called from xfs_bmap_add_attrfork to handle extents format files.
968 STATIC
int /* error */
969 xfs_bmap_add_attrfork_extents(
970 xfs_trans_t
*tp
, /* transaction pointer */
971 xfs_inode_t
*ip
, /* incore inode pointer */
972 xfs_fsblock_t
*firstblock
, /* first block allocated */
973 struct xfs_defer_ops
*dfops
, /* blocks to free at commit */
974 int *flags
) /* inode logging flags */
976 xfs_btree_cur_t
*cur
; /* bmap btree cursor */
977 int error
; /* error return value */
979 if (ip
->i_d
.di_nextents
* sizeof(xfs_bmbt_rec_t
) <= XFS_IFORK_DSIZE(ip
))
982 error
= xfs_bmap_extents_to_btree(tp
, ip
, firstblock
, dfops
, &cur
, 0,
983 flags
, XFS_DATA_FORK
);
985 cur
->bc_private
.b
.allocated
= 0;
986 xfs_btree_del_cursor(cur
,
987 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
993 * Called from xfs_bmap_add_attrfork to handle local format files. Each
994 * different data fork content type needs a different callout to do the
995 * conversion. Some are basic and only require special block initialisation
996 * callouts for the data formating, others (directories) are so specialised they
997 * handle everything themselves.
999 * XXX (dgc): investigate whether directory conversion can use the generic
1000 * formatting callout. It should be possible - it's just a very complex
1003 STATIC
int /* error */
1004 xfs_bmap_add_attrfork_local(
1005 xfs_trans_t
*tp
, /* transaction pointer */
1006 xfs_inode_t
*ip
, /* incore inode pointer */
1007 xfs_fsblock_t
*firstblock
, /* first block allocated */
1008 struct xfs_defer_ops
*dfops
, /* blocks to free at commit */
1009 int *flags
) /* inode logging flags */
1011 xfs_da_args_t dargs
; /* args for dir/attr code */
1013 if (ip
->i_df
.if_bytes
<= XFS_IFORK_DSIZE(ip
))
1016 if (S_ISDIR(VFS_I(ip
)->i_mode
)) {
1017 memset(&dargs
, 0, sizeof(dargs
));
1018 dargs
.geo
= ip
->i_mount
->m_dir_geo
;
1020 dargs
.firstblock
= firstblock
;
1021 dargs
.dfops
= dfops
;
1022 dargs
.total
= dargs
.geo
->fsbcount
;
1023 dargs
.whichfork
= XFS_DATA_FORK
;
1025 return xfs_dir2_sf_to_block(&dargs
);
1028 if (S_ISLNK(VFS_I(ip
)->i_mode
))
1029 return xfs_bmap_local_to_extents(tp
, ip
, firstblock
, 1,
1030 flags
, XFS_DATA_FORK
,
1031 xfs_symlink_local_to_remote
);
1033 /* should only be called for types that support local format data */
1035 return -EFSCORRUPTED
;
1039 * Convert inode from non-attributed to attributed.
1040 * Must not be in a transaction, ip must not be locked.
1042 int /* error code */
1043 xfs_bmap_add_attrfork(
1044 xfs_inode_t
*ip
, /* incore inode pointer */
1045 int size
, /* space new attribute needs */
1046 int rsvd
) /* xact may use reserved blks */
1048 xfs_fsblock_t firstblock
; /* 1st block/ag allocated */
1049 struct xfs_defer_ops dfops
; /* freed extent records */
1050 xfs_mount_t
*mp
; /* mount structure */
1051 xfs_trans_t
*tp
; /* transaction pointer */
1052 int blks
; /* space reservation */
1053 int version
= 1; /* superblock attr version */
1054 int logflags
; /* logging flags */
1055 int error
; /* error return value */
1057 ASSERT(XFS_IFORK_Q(ip
) == 0);
1060 ASSERT(!XFS_NOT_DQATTACHED(mp
, ip
));
1062 blks
= XFS_ADDAFORK_SPACE_RES(mp
);
1064 error
= xfs_trans_alloc(mp
, &M_RES(mp
)->tr_addafork
, blks
, 0,
1065 rsvd
? XFS_TRANS_RESERVE
: 0, &tp
);
1069 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
1070 error
= xfs_trans_reserve_quota_nblks(tp
, ip
, blks
, 0, rsvd
?
1071 XFS_QMOPT_RES_REGBLKS
| XFS_QMOPT_FORCE_RES
:
1072 XFS_QMOPT_RES_REGBLKS
);
1075 if (XFS_IFORK_Q(ip
))
1077 if (ip
->i_d
.di_anextents
!= 0) {
1078 error
= -EFSCORRUPTED
;
1081 if (ip
->i_d
.di_aformat
!= XFS_DINODE_FMT_EXTENTS
) {
1083 * For inodes coming from pre-6.2 filesystems.
1085 ASSERT(ip
->i_d
.di_aformat
== 0);
1086 ip
->i_d
.di_aformat
= XFS_DINODE_FMT_EXTENTS
;
1089 xfs_trans_ijoin(tp
, ip
, 0);
1090 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
1092 switch (ip
->i_d
.di_format
) {
1093 case XFS_DINODE_FMT_DEV
:
1094 ip
->i_d
.di_forkoff
= roundup(sizeof(xfs_dev_t
), 8) >> 3;
1096 case XFS_DINODE_FMT_LOCAL
:
1097 case XFS_DINODE_FMT_EXTENTS
:
1098 case XFS_DINODE_FMT_BTREE
:
1099 ip
->i_d
.di_forkoff
= xfs_attr_shortform_bytesfit(ip
, size
);
1100 if (!ip
->i_d
.di_forkoff
)
1101 ip
->i_d
.di_forkoff
= xfs_default_attroffset(ip
) >> 3;
1102 else if (mp
->m_flags
& XFS_MOUNT_ATTR2
)
1111 ASSERT(ip
->i_afp
== NULL
);
1112 ip
->i_afp
= kmem_zone_zalloc(xfs_ifork_zone
, KM_SLEEP
);
1113 ip
->i_afp
->if_flags
= XFS_IFEXTENTS
;
1115 xfs_defer_init(&dfops
, &firstblock
);
1116 switch (ip
->i_d
.di_format
) {
1117 case XFS_DINODE_FMT_LOCAL
:
1118 error
= xfs_bmap_add_attrfork_local(tp
, ip
, &firstblock
, &dfops
,
1121 case XFS_DINODE_FMT_EXTENTS
:
1122 error
= xfs_bmap_add_attrfork_extents(tp
, ip
, &firstblock
,
1125 case XFS_DINODE_FMT_BTREE
:
1126 error
= xfs_bmap_add_attrfork_btree(tp
, ip
, &firstblock
, &dfops
,
1134 xfs_trans_log_inode(tp
, ip
, logflags
);
1137 if (!xfs_sb_version_hasattr(&mp
->m_sb
) ||
1138 (!xfs_sb_version_hasattr2(&mp
->m_sb
) && version
== 2)) {
1139 bool log_sb
= false;
1141 spin_lock(&mp
->m_sb_lock
);
1142 if (!xfs_sb_version_hasattr(&mp
->m_sb
)) {
1143 xfs_sb_version_addattr(&mp
->m_sb
);
1146 if (!xfs_sb_version_hasattr2(&mp
->m_sb
) && version
== 2) {
1147 xfs_sb_version_addattr2(&mp
->m_sb
);
1150 spin_unlock(&mp
->m_sb_lock
);
1155 error
= xfs_defer_finish(&tp
, &dfops
);
1158 error
= xfs_trans_commit(tp
);
1159 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
1163 xfs_defer_cancel(&dfops
);
1165 xfs_trans_cancel(tp
);
1166 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
1171 * Internal and external extent tree search functions.
1175 * Read in extents from a btree-format inode.
1179 struct xfs_trans
*tp
,
1180 struct xfs_inode
*ip
,
1183 struct xfs_mount
*mp
= ip
->i_mount
;
1184 int state
= xfs_bmap_fork_to_state(whichfork
);
1185 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1186 xfs_extnum_t nextents
= XFS_IFORK_NEXTENTS(ip
, whichfork
);
1187 struct xfs_btree_block
*block
= ifp
->if_broot
;
1188 struct xfs_iext_cursor icur
;
1189 struct xfs_bmbt_irec
new;
1197 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
1199 if (unlikely(XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)) {
1200 XFS_ERROR_REPORT(__func__
, XFS_ERRLEVEL_LOW
, mp
);
1201 return -EFSCORRUPTED
;
1205 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
1207 level
= be16_to_cpu(block
->bb_level
);
1209 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, 1, ifp
->if_broot_bytes
);
1210 bno
= be64_to_cpu(*pp
);
1213 * Go down the tree until leaf level is reached, following the first
1214 * pointer (leftmost) at each level.
1216 while (level
-- > 0) {
1217 error
= xfs_btree_read_bufl(mp
, tp
, bno
, 0, &bp
,
1218 XFS_BMAP_BTREE_REF
, &xfs_bmbt_buf_ops
);
1221 block
= XFS_BUF_TO_BLOCK(bp
);
1224 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, mp
->m_bmap_dmxr
[1]);
1225 bno
= be64_to_cpu(*pp
);
1226 XFS_WANT_CORRUPTED_GOTO(mp
,
1227 XFS_FSB_SANITY_CHECK(mp
, bno
), out_brelse
);
1228 xfs_trans_brelse(tp
, bp
);
1232 * Here with bp and block set to the leftmost leaf node in the tree.
1235 xfs_iext_first(ifp
, &icur
);
1238 * Loop over all leaf nodes. Copy information to the extent records.
1241 xfs_bmbt_rec_t
*frp
;
1242 xfs_fsblock_t nextbno
;
1243 xfs_extnum_t num_recs
;
1245 num_recs
= xfs_btree_get_numrecs(block
);
1246 if (unlikely(i
+ num_recs
> nextents
)) {
1247 ASSERT(i
+ num_recs
<= nextents
);
1248 xfs_warn(ip
->i_mount
,
1249 "corrupt dinode %Lu, (btree extents).",
1250 (unsigned long long) ip
->i_ino
);
1251 XFS_CORRUPTION_ERROR(__func__
,
1252 XFS_ERRLEVEL_LOW
, ip
->i_mount
, block
);
1253 error
= -EFSCORRUPTED
;
1257 * Read-ahead the next leaf block, if any.
1259 nextbno
= be64_to_cpu(block
->bb_u
.l
.bb_rightsib
);
1260 if (nextbno
!= NULLFSBLOCK
)
1261 xfs_btree_reada_bufl(mp
, nextbno
, 1,
1264 * Copy records into the extent records.
1266 frp
= XFS_BMBT_REC_ADDR(mp
, block
, 1);
1267 for (j
= 0; j
< num_recs
; j
++, frp
++, i
++) {
1268 xfs_bmbt_disk_get_all(frp
, &new);
1269 if (!xfs_bmbt_validate_extent(mp
, whichfork
, &new)) {
1270 XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
1271 XFS_ERRLEVEL_LOW
, mp
);
1272 error
= -EFSCORRUPTED
;
1275 xfs_iext_insert(ip
, &icur
, &new, state
);
1276 trace_xfs_read_extent(ip
, &icur
, state
, _THIS_IP_
);
1277 xfs_iext_next(ifp
, &icur
);
1279 xfs_trans_brelse(tp
, bp
);
1282 * If we've reached the end, stop.
1284 if (bno
== NULLFSBLOCK
)
1286 error
= xfs_btree_read_bufl(mp
, tp
, bno
, 0, &bp
,
1287 XFS_BMAP_BTREE_REF
, &xfs_bmbt_buf_ops
);
1290 block
= XFS_BUF_TO_BLOCK(bp
);
1293 if (i
!= XFS_IFORK_NEXTENTS(ip
, whichfork
)) {
1294 error
= -EFSCORRUPTED
;
1297 ASSERT(i
== xfs_iext_count(ifp
));
1299 ifp
->if_flags
|= XFS_IFEXTENTS
;
1303 xfs_trans_brelse(tp
, bp
);
1305 xfs_iext_destroy(ifp
);
1310 * Returns the relative block number of the first unused block(s) in the given
1311 * fork with at least "len" logically contiguous blocks free. This is the
1312 * lowest-address hole if the fork has holes, else the first block past the end
1313 * of fork. Return 0 if the fork is currently local (in-inode).
1316 xfs_bmap_first_unused(
1317 struct xfs_trans
*tp
, /* transaction pointer */
1318 struct xfs_inode
*ip
, /* incore inode */
1319 xfs_extlen_t len
, /* size of hole to find */
1320 xfs_fileoff_t
*first_unused
, /* unused block */
1321 int whichfork
) /* data or attr fork */
1323 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1324 struct xfs_bmbt_irec got
;
1325 struct xfs_iext_cursor icur
;
1326 xfs_fileoff_t lastaddr
= 0;
1327 xfs_fileoff_t lowest
, max
;
1330 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
||
1331 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
||
1332 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
1334 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
) {
1339 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
1340 error
= xfs_iread_extents(tp
, ip
, whichfork
);
1345 lowest
= max
= *first_unused
;
1346 for_each_xfs_iext(ifp
, &icur
, &got
) {
1348 * See if the hole before this extent will work.
1350 if (got
.br_startoff
>= lowest
+ len
&&
1351 got
.br_startoff
- max
>= len
)
1353 lastaddr
= got
.br_startoff
+ got
.br_blockcount
;
1354 max
= XFS_FILEOFF_MAX(lastaddr
, lowest
);
1357 *first_unused
= max
;
1362 * Returns the file-relative block number of the last block - 1 before
1363 * last_block (input value) in the file.
1364 * This is not based on i_size, it is based on the extent records.
1365 * Returns 0 for local files, as they do not have extent records.
1368 xfs_bmap_last_before(
1369 struct xfs_trans
*tp
, /* transaction pointer */
1370 struct xfs_inode
*ip
, /* incore inode */
1371 xfs_fileoff_t
*last_block
, /* last block */
1372 int whichfork
) /* data or attr fork */
1374 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1375 struct xfs_bmbt_irec got
;
1376 struct xfs_iext_cursor icur
;
1379 switch (XFS_IFORK_FORMAT(ip
, whichfork
)) {
1380 case XFS_DINODE_FMT_LOCAL
:
1383 case XFS_DINODE_FMT_BTREE
:
1384 case XFS_DINODE_FMT_EXTENTS
:
1390 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
1391 error
= xfs_iread_extents(tp
, ip
, whichfork
);
1396 if (!xfs_iext_lookup_extent_before(ip
, ifp
, last_block
, &icur
, &got
))
1402 xfs_bmap_last_extent(
1403 struct xfs_trans
*tp
,
1404 struct xfs_inode
*ip
,
1406 struct xfs_bmbt_irec
*rec
,
1409 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1410 struct xfs_iext_cursor icur
;
1413 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
1414 error
= xfs_iread_extents(tp
, ip
, whichfork
);
1419 xfs_iext_last(ifp
, &icur
);
1420 if (!xfs_iext_get_extent(ifp
, &icur
, rec
))
1428 * Check the last inode extent to determine whether this allocation will result
1429 * in blocks being allocated at the end of the file. When we allocate new data
1430 * blocks at the end of the file which do not start at the previous data block,
1431 * we will try to align the new blocks at stripe unit boundaries.
1433 * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be
1434 * at, or past the EOF.
1438 struct xfs_bmalloca
*bma
,
1441 struct xfs_bmbt_irec rec
;
1446 error
= xfs_bmap_last_extent(NULL
, bma
->ip
, whichfork
, &rec
,
1457 * Check if we are allocation or past the last extent, or at least into
1458 * the last delayed allocated extent.
1460 bma
->aeof
= bma
->offset
>= rec
.br_startoff
+ rec
.br_blockcount
||
1461 (bma
->offset
>= rec
.br_startoff
&&
1462 isnullstartblock(rec
.br_startblock
));
1467 * Returns the file-relative block number of the first block past eof in
1468 * the file. This is not based on i_size, it is based on the extent records.
1469 * Returns 0 for local files, as they do not have extent records.
1472 xfs_bmap_last_offset(
1473 struct xfs_inode
*ip
,
1474 xfs_fileoff_t
*last_block
,
1477 struct xfs_bmbt_irec rec
;
1483 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
)
1486 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
&&
1487 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
1490 error
= xfs_bmap_last_extent(NULL
, ip
, whichfork
, &rec
, &is_empty
);
1491 if (error
|| is_empty
)
1494 *last_block
= rec
.br_startoff
+ rec
.br_blockcount
;
1499 * Returns whether the selected fork of the inode has exactly one
1500 * block or not. For the data fork we check this matches di_size,
1501 * implying the file's range is 0..bsize-1.
1503 int /* 1=>1 block, 0=>otherwise */
1505 xfs_inode_t
*ip
, /* incore inode */
1506 int whichfork
) /* data or attr fork */
1508 xfs_ifork_t
*ifp
; /* inode fork pointer */
1509 int rval
; /* return value */
1510 xfs_bmbt_irec_t s
; /* internal version of extent */
1511 struct xfs_iext_cursor icur
;
1514 if (whichfork
== XFS_DATA_FORK
)
1515 return XFS_ISIZE(ip
) == ip
->i_mount
->m_sb
.sb_blocksize
;
1517 if (XFS_IFORK_NEXTENTS(ip
, whichfork
) != 1)
1519 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
1521 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1522 ASSERT(ifp
->if_flags
& XFS_IFEXTENTS
);
1523 xfs_iext_first(ifp
, &icur
);
1524 xfs_iext_get_extent(ifp
, &icur
, &s
);
1525 rval
= s
.br_startoff
== 0 && s
.br_blockcount
== 1;
1526 if (rval
&& whichfork
== XFS_DATA_FORK
)
1527 ASSERT(XFS_ISIZE(ip
) == ip
->i_mount
->m_sb
.sb_blocksize
);
1532 * Extent tree manipulation functions used during allocation.
1536 * Convert a delayed allocation to a real allocation.
1538 STATIC
int /* error */
1539 xfs_bmap_add_extent_delay_real(
1540 struct xfs_bmalloca
*bma
,
1543 struct xfs_bmbt_irec
*new = &bma
->got
;
1544 int error
; /* error return value */
1545 int i
; /* temp state */
1546 xfs_ifork_t
*ifp
; /* inode fork pointer */
1547 xfs_fileoff_t new_endoff
; /* end offset of new entry */
1548 xfs_bmbt_irec_t r
[3]; /* neighbor extent entries */
1549 /* left is 0, right is 1, prev is 2 */
1550 int rval
=0; /* return value (logging flags) */
1551 int state
= xfs_bmap_fork_to_state(whichfork
);
1552 xfs_filblks_t da_new
; /* new count del alloc blocks used */
1553 xfs_filblks_t da_old
; /* old count del alloc blocks used */
1554 xfs_filblks_t temp
=0; /* value for da_new calculations */
1555 int tmp_rval
; /* partial logging flags */
1556 struct xfs_mount
*mp
;
1557 xfs_extnum_t
*nextents
;
1558 struct xfs_bmbt_irec old
;
1560 mp
= bma
->ip
->i_mount
;
1561 ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
1562 ASSERT(whichfork
!= XFS_ATTR_FORK
);
1563 nextents
= (whichfork
== XFS_COW_FORK
? &bma
->ip
->i_cnextents
:
1564 &bma
->ip
->i_d
.di_nextents
);
1566 ASSERT(!isnullstartblock(new->br_startblock
));
1568 (bma
->cur
->bc_private
.b
.flags
& XFS_BTCUR_BPRV_WASDEL
));
1570 XFS_STATS_INC(mp
, xs_add_exlist
);
1577 * Set up a bunch of variables to make the tests simpler.
1579 xfs_iext_get_extent(ifp
, &bma
->icur
, &PREV
);
1580 new_endoff
= new->br_startoff
+ new->br_blockcount
;
1581 ASSERT(isnullstartblock(PREV
.br_startblock
));
1582 ASSERT(PREV
.br_startoff
<= new->br_startoff
);
1583 ASSERT(PREV
.br_startoff
+ PREV
.br_blockcount
>= new_endoff
);
1585 da_old
= startblockval(PREV
.br_startblock
);
1589 * Set flags determining what part of the previous delayed allocation
1590 * extent is being replaced by a real allocation.
1592 if (PREV
.br_startoff
== new->br_startoff
)
1593 state
|= BMAP_LEFT_FILLING
;
1594 if (PREV
.br_startoff
+ PREV
.br_blockcount
== new_endoff
)
1595 state
|= BMAP_RIGHT_FILLING
;
1598 * Check and set flags if this segment has a left neighbor.
1599 * Don't set contiguous if the combined extent would be too large.
1601 if (xfs_iext_peek_prev_extent(ifp
, &bma
->icur
, &LEFT
)) {
1602 state
|= BMAP_LEFT_VALID
;
1603 if (isnullstartblock(LEFT
.br_startblock
))
1604 state
|= BMAP_LEFT_DELAY
;
1607 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
1608 LEFT
.br_startoff
+ LEFT
.br_blockcount
== new->br_startoff
&&
1609 LEFT
.br_startblock
+ LEFT
.br_blockcount
== new->br_startblock
&&
1610 LEFT
.br_state
== new->br_state
&&
1611 LEFT
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
1612 state
|= BMAP_LEFT_CONTIG
;
1615 * Check and set flags if this segment has a right neighbor.
1616 * Don't set contiguous if the combined extent would be too large.
1617 * Also check for all-three-contiguous being too large.
1619 if (xfs_iext_peek_next_extent(ifp
, &bma
->icur
, &RIGHT
)) {
1620 state
|= BMAP_RIGHT_VALID
;
1621 if (isnullstartblock(RIGHT
.br_startblock
))
1622 state
|= BMAP_RIGHT_DELAY
;
1625 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
1626 new_endoff
== RIGHT
.br_startoff
&&
1627 new->br_startblock
+ new->br_blockcount
== RIGHT
.br_startblock
&&
1628 new->br_state
== RIGHT
.br_state
&&
1629 new->br_blockcount
+ RIGHT
.br_blockcount
<= MAXEXTLEN
&&
1630 ((state
& (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
1631 BMAP_RIGHT_FILLING
)) !=
1632 (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
1633 BMAP_RIGHT_FILLING
) ||
1634 LEFT
.br_blockcount
+ new->br_blockcount
+ RIGHT
.br_blockcount
1636 state
|= BMAP_RIGHT_CONTIG
;
1640 * Switch out based on the FILLING and CONTIG state bits.
1642 switch (state
& (BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
1643 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
)) {
1644 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
1645 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
1647 * Filling in all of a previously delayed allocation extent.
1648 * The left and right neighbors are both contiguous with new.
1650 LEFT
.br_blockcount
+= PREV
.br_blockcount
+ RIGHT
.br_blockcount
;
1652 xfs_iext_remove(bma
->ip
, &bma
->icur
, state
);
1653 xfs_iext_remove(bma
->ip
, &bma
->icur
, state
);
1654 xfs_iext_prev(ifp
, &bma
->icur
);
1655 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &LEFT
);
1658 if (bma
->cur
== NULL
)
1659 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1661 rval
= XFS_ILOG_CORE
;
1662 error
= xfs_bmbt_lookup_eq(bma
->cur
, &RIGHT
, &i
);
1665 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1666 error
= xfs_btree_delete(bma
->cur
, &i
);
1669 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1670 error
= xfs_btree_decrement(bma
->cur
, 0, &i
);
1673 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1674 error
= xfs_bmbt_update(bma
->cur
, &LEFT
);
1680 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
1682 * Filling in all of a previously delayed allocation extent.
1683 * The left neighbor is contiguous, the right is not.
1686 LEFT
.br_blockcount
+= PREV
.br_blockcount
;
1688 xfs_iext_remove(bma
->ip
, &bma
->icur
, state
);
1689 xfs_iext_prev(ifp
, &bma
->icur
);
1690 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &LEFT
);
1692 if (bma
->cur
== NULL
)
1693 rval
= XFS_ILOG_DEXT
;
1696 error
= xfs_bmbt_lookup_eq(bma
->cur
, &old
, &i
);
1699 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1700 error
= xfs_bmbt_update(bma
->cur
, &LEFT
);
1706 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
1708 * Filling in all of a previously delayed allocation extent.
1709 * The right neighbor is contiguous, the left is not.
1711 PREV
.br_startblock
= new->br_startblock
;
1712 PREV
.br_blockcount
+= RIGHT
.br_blockcount
;
1714 xfs_iext_next(ifp
, &bma
->icur
);
1715 xfs_iext_remove(bma
->ip
, &bma
->icur
, state
);
1716 xfs_iext_prev(ifp
, &bma
->icur
);
1717 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &PREV
);
1719 if (bma
->cur
== NULL
)
1720 rval
= XFS_ILOG_DEXT
;
1723 error
= xfs_bmbt_lookup_eq(bma
->cur
, &RIGHT
, &i
);
1726 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1727 error
= xfs_bmbt_update(bma
->cur
, &PREV
);
1733 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
1735 * Filling in all of a previously delayed allocation extent.
1736 * Neither the left nor right neighbors are contiguous with
1739 PREV
.br_startblock
= new->br_startblock
;
1740 PREV
.br_state
= new->br_state
;
1741 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &PREV
);
1744 if (bma
->cur
== NULL
)
1745 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1747 rval
= XFS_ILOG_CORE
;
1748 error
= xfs_bmbt_lookup_eq(bma
->cur
, new, &i
);
1751 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
1752 error
= xfs_btree_insert(bma
->cur
, &i
);
1755 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1759 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
:
1761 * Filling in the first part of a previous delayed allocation.
1762 * The left neighbor is contiguous.
1765 temp
= PREV
.br_blockcount
- new->br_blockcount
;
1766 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
1767 startblockval(PREV
.br_startblock
));
1769 LEFT
.br_blockcount
+= new->br_blockcount
;
1771 PREV
.br_blockcount
= temp
;
1772 PREV
.br_startoff
+= new->br_blockcount
;
1773 PREV
.br_startblock
= nullstartblock(da_new
);
1775 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &PREV
);
1776 xfs_iext_prev(ifp
, &bma
->icur
);
1777 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &LEFT
);
1779 if (bma
->cur
== NULL
)
1780 rval
= XFS_ILOG_DEXT
;
1783 error
= xfs_bmbt_lookup_eq(bma
->cur
, &old
, &i
);
1786 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1787 error
= xfs_bmbt_update(bma
->cur
, &LEFT
);
1793 case BMAP_LEFT_FILLING
:
1795 * Filling in the first part of a previous delayed allocation.
1796 * The left neighbor is not contiguous.
1798 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, new);
1800 if (bma
->cur
== NULL
)
1801 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1803 rval
= XFS_ILOG_CORE
;
1804 error
= xfs_bmbt_lookup_eq(bma
->cur
, new, &i
);
1807 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
1808 error
= xfs_btree_insert(bma
->cur
, &i
);
1811 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1814 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
1815 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
1816 bma
->firstblock
, bma
->dfops
,
1817 &bma
->cur
, 1, &tmp_rval
, whichfork
);
1823 temp
= PREV
.br_blockcount
- new->br_blockcount
;
1824 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
1825 startblockval(PREV
.br_startblock
) -
1826 (bma
->cur
? bma
->cur
->bc_private
.b
.allocated
: 0));
1828 PREV
.br_startoff
= new_endoff
;
1829 PREV
.br_blockcount
= temp
;
1830 PREV
.br_startblock
= nullstartblock(da_new
);
1831 xfs_iext_next(ifp
, &bma
->icur
);
1832 xfs_iext_insert(bma
->ip
, &bma
->icur
, &PREV
, state
);
1833 xfs_iext_prev(ifp
, &bma
->icur
);
1836 case BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
1838 * Filling in the last part of a previous delayed allocation.
1839 * The right neighbor is contiguous with the new allocation.
1842 RIGHT
.br_startoff
= new->br_startoff
;
1843 RIGHT
.br_startblock
= new->br_startblock
;
1844 RIGHT
.br_blockcount
+= new->br_blockcount
;
1846 if (bma
->cur
== NULL
)
1847 rval
= XFS_ILOG_DEXT
;
1850 error
= xfs_bmbt_lookup_eq(bma
->cur
, &old
, &i
);
1853 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1854 error
= xfs_bmbt_update(bma
->cur
, &RIGHT
);
1859 temp
= PREV
.br_blockcount
- new->br_blockcount
;
1860 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
1861 startblockval(PREV
.br_startblock
));
1863 PREV
.br_blockcount
= temp
;
1864 PREV
.br_startblock
= nullstartblock(da_new
);
1866 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &PREV
);
1867 xfs_iext_next(ifp
, &bma
->icur
);
1868 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &RIGHT
);
1871 case BMAP_RIGHT_FILLING
:
1873 * Filling in the last part of a previous delayed allocation.
1874 * The right neighbor is not contiguous.
1876 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, new);
1878 if (bma
->cur
== NULL
)
1879 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1881 rval
= XFS_ILOG_CORE
;
1882 error
= xfs_bmbt_lookup_eq(bma
->cur
, new, &i
);
1885 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
1886 error
= xfs_btree_insert(bma
->cur
, &i
);
1889 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1892 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
1893 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
1894 bma
->firstblock
, bma
->dfops
, &bma
->cur
, 1,
1895 &tmp_rval
, whichfork
);
1901 temp
= PREV
.br_blockcount
- new->br_blockcount
;
1902 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
1903 startblockval(PREV
.br_startblock
) -
1904 (bma
->cur
? bma
->cur
->bc_private
.b
.allocated
: 0));
1906 PREV
.br_startblock
= nullstartblock(da_new
);
1907 PREV
.br_blockcount
= temp
;
1908 xfs_iext_insert(bma
->ip
, &bma
->icur
, &PREV
, state
);
1909 xfs_iext_next(ifp
, &bma
->icur
);
1914 * Filling in the middle part of a previous delayed allocation.
1915 * Contiguity is impossible here.
1916 * This case is avoided almost all the time.
1918 * We start with a delayed allocation:
1920 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
1923 * and we are allocating:
1924 * +rrrrrrrrrrrrrrrrr+
1927 * and we set it up for insertion as:
1928 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
1930 * PREV @ idx LEFT RIGHT
1931 * inserted at idx + 1
1935 /* LEFT is the new middle */
1938 /* RIGHT is the new right */
1939 RIGHT
.br_state
= PREV
.br_state
;
1940 RIGHT
.br_startoff
= new_endoff
;
1941 RIGHT
.br_blockcount
=
1942 PREV
.br_startoff
+ PREV
.br_blockcount
- new_endoff
;
1943 RIGHT
.br_startblock
=
1944 nullstartblock(xfs_bmap_worst_indlen(bma
->ip
,
1945 RIGHT
.br_blockcount
));
1948 PREV
.br_blockcount
= new->br_startoff
- PREV
.br_startoff
;
1949 PREV
.br_startblock
=
1950 nullstartblock(xfs_bmap_worst_indlen(bma
->ip
,
1951 PREV
.br_blockcount
));
1952 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &PREV
);
1954 xfs_iext_next(ifp
, &bma
->icur
);
1955 xfs_iext_insert(bma
->ip
, &bma
->icur
, &RIGHT
, state
);
1956 xfs_iext_insert(bma
->ip
, &bma
->icur
, &LEFT
, state
);
1959 if (bma
->cur
== NULL
)
1960 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1962 rval
= XFS_ILOG_CORE
;
1963 error
= xfs_bmbt_lookup_eq(bma
->cur
, new, &i
);
1966 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
1967 error
= xfs_btree_insert(bma
->cur
, &i
);
1970 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1973 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
1974 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
1975 bma
->firstblock
, bma
->dfops
, &bma
->cur
,
1976 1, &tmp_rval
, whichfork
);
1982 da_new
= startblockval(PREV
.br_startblock
) +
1983 startblockval(RIGHT
.br_startblock
);
1986 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
1987 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
1988 case BMAP_LEFT_FILLING
| BMAP_RIGHT_CONTIG
:
1989 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
1990 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
1991 case BMAP_LEFT_CONTIG
:
1992 case BMAP_RIGHT_CONTIG
:
1994 * These cases are all impossible.
1999 /* add reverse mapping */
2000 error
= xfs_rmap_map_extent(mp
, bma
->dfops
, bma
->ip
, whichfork
, new);
2004 /* convert to a btree if necessary */
2005 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
2006 int tmp_logflags
; /* partial log flag return val */
2008 ASSERT(bma
->cur
== NULL
);
2009 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
2010 bma
->firstblock
, bma
->dfops
, &bma
->cur
,
2011 da_old
> 0, &tmp_logflags
, whichfork
);
2012 bma
->logflags
|= tmp_logflags
;
2018 da_new
+= bma
->cur
->bc_private
.b
.allocated
;
2019 bma
->cur
->bc_private
.b
.allocated
= 0;
2022 /* adjust for changes in reserved delayed indirect blocks */
2023 if (da_new
!= da_old
) {
2024 ASSERT(state
== 0 || da_new
< da_old
);
2025 error
= xfs_mod_fdblocks(mp
, (int64_t)(da_old
- da_new
),
2029 xfs_bmap_check_leaf_extents(bma
->cur
, bma
->ip
, whichfork
);
2031 if (whichfork
!= XFS_COW_FORK
)
2032 bma
->logflags
|= rval
;
2040 * Convert an unwritten allocation to a real allocation or vice versa.
2042 STATIC
int /* error */
2043 xfs_bmap_add_extent_unwritten_real(
2044 struct xfs_trans
*tp
,
2045 xfs_inode_t
*ip
, /* incore inode pointer */
2047 struct xfs_iext_cursor
*icur
,
2048 xfs_btree_cur_t
**curp
, /* if *curp is null, not a btree */
2049 xfs_bmbt_irec_t
*new, /* new data to add to file extents */
2050 xfs_fsblock_t
*first
, /* pointer to firstblock variable */
2051 struct xfs_defer_ops
*dfops
, /* list of extents to be freed */
2052 int *logflagsp
) /* inode logging flags */
2054 xfs_btree_cur_t
*cur
; /* btree cursor */
2055 int error
; /* error return value */
2056 int i
; /* temp state */
2057 xfs_ifork_t
*ifp
; /* inode fork pointer */
2058 xfs_fileoff_t new_endoff
; /* end offset of new entry */
2059 xfs_bmbt_irec_t r
[3]; /* neighbor extent entries */
2060 /* left is 0, right is 1, prev is 2 */
2061 int rval
=0; /* return value (logging flags) */
2062 int state
= xfs_bmap_fork_to_state(whichfork
);
2063 struct xfs_mount
*mp
= ip
->i_mount
;
2064 struct xfs_bmbt_irec old
;
2069 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
2071 ASSERT(!isnullstartblock(new->br_startblock
));
2073 XFS_STATS_INC(mp
, xs_add_exlist
);
2080 * Set up a bunch of variables to make the tests simpler.
2083 xfs_iext_get_extent(ifp
, icur
, &PREV
);
2084 ASSERT(new->br_state
!= PREV
.br_state
);
2085 new_endoff
= new->br_startoff
+ new->br_blockcount
;
2086 ASSERT(PREV
.br_startoff
<= new->br_startoff
);
2087 ASSERT(PREV
.br_startoff
+ PREV
.br_blockcount
>= new_endoff
);
2090 * Set flags determining what part of the previous oldext allocation
2091 * extent is being replaced by a newext allocation.
2093 if (PREV
.br_startoff
== new->br_startoff
)
2094 state
|= BMAP_LEFT_FILLING
;
2095 if (PREV
.br_startoff
+ PREV
.br_blockcount
== new_endoff
)
2096 state
|= BMAP_RIGHT_FILLING
;
2099 * Check and set flags if this segment has a left neighbor.
2100 * Don't set contiguous if the combined extent would be too large.
2102 if (xfs_iext_peek_prev_extent(ifp
, icur
, &LEFT
)) {
2103 state
|= BMAP_LEFT_VALID
;
2104 if (isnullstartblock(LEFT
.br_startblock
))
2105 state
|= BMAP_LEFT_DELAY
;
2108 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
2109 LEFT
.br_startoff
+ LEFT
.br_blockcount
== new->br_startoff
&&
2110 LEFT
.br_startblock
+ LEFT
.br_blockcount
== new->br_startblock
&&
2111 LEFT
.br_state
== new->br_state
&&
2112 LEFT
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
2113 state
|= BMAP_LEFT_CONTIG
;
2116 * Check and set flags if this segment has a right neighbor.
2117 * Don't set contiguous if the combined extent would be too large.
2118 * Also check for all-three-contiguous being too large.
2120 if (xfs_iext_peek_next_extent(ifp
, icur
, &RIGHT
)) {
2121 state
|= BMAP_RIGHT_VALID
;
2122 if (isnullstartblock(RIGHT
.br_startblock
))
2123 state
|= BMAP_RIGHT_DELAY
;
2126 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
2127 new_endoff
== RIGHT
.br_startoff
&&
2128 new->br_startblock
+ new->br_blockcount
== RIGHT
.br_startblock
&&
2129 new->br_state
== RIGHT
.br_state
&&
2130 new->br_blockcount
+ RIGHT
.br_blockcount
<= MAXEXTLEN
&&
2131 ((state
& (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
2132 BMAP_RIGHT_FILLING
)) !=
2133 (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
2134 BMAP_RIGHT_FILLING
) ||
2135 LEFT
.br_blockcount
+ new->br_blockcount
+ RIGHT
.br_blockcount
2137 state
|= BMAP_RIGHT_CONTIG
;
2140 * Switch out based on the FILLING and CONTIG state bits.
2142 switch (state
& (BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
2143 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
)) {
2144 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
2145 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2147 * Setting all of a previous oldext extent to newext.
2148 * The left and right neighbors are both contiguous with new.
2150 LEFT
.br_blockcount
+= PREV
.br_blockcount
+ RIGHT
.br_blockcount
;
2152 xfs_iext_remove(ip
, icur
, state
);
2153 xfs_iext_remove(ip
, icur
, state
);
2154 xfs_iext_prev(ifp
, icur
);
2155 xfs_iext_update_extent(ip
, state
, icur
, &LEFT
);
2156 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2157 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 2);
2159 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2161 rval
= XFS_ILOG_CORE
;
2162 error
= xfs_bmbt_lookup_eq(cur
, &RIGHT
, &i
);
2165 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2166 if ((error
= xfs_btree_delete(cur
, &i
)))
2168 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2169 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2171 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2172 if ((error
= xfs_btree_delete(cur
, &i
)))
2174 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2175 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2177 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2178 error
= xfs_bmbt_update(cur
, &LEFT
);
2184 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
2186 * Setting all of a previous oldext extent to newext.
2187 * The left neighbor is contiguous, the right is not.
2189 LEFT
.br_blockcount
+= PREV
.br_blockcount
;
2191 xfs_iext_remove(ip
, icur
, state
);
2192 xfs_iext_prev(ifp
, icur
);
2193 xfs_iext_update_extent(ip
, state
, icur
, &LEFT
);
2194 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2195 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
2197 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2199 rval
= XFS_ILOG_CORE
;
2200 error
= xfs_bmbt_lookup_eq(cur
, &PREV
, &i
);
2203 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2204 if ((error
= xfs_btree_delete(cur
, &i
)))
2206 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2207 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2209 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2210 error
= xfs_bmbt_update(cur
, &LEFT
);
2216 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2218 * Setting all of a previous oldext extent to newext.
2219 * The right neighbor is contiguous, the left is not.
2221 PREV
.br_blockcount
+= RIGHT
.br_blockcount
;
2222 PREV
.br_state
= new->br_state
;
2224 xfs_iext_next(ifp
, icur
);
2225 xfs_iext_remove(ip
, icur
, state
);
2226 xfs_iext_prev(ifp
, icur
);
2227 xfs_iext_update_extent(ip
, state
, icur
, &PREV
);
2229 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2230 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
2232 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2234 rval
= XFS_ILOG_CORE
;
2235 error
= xfs_bmbt_lookup_eq(cur
, &RIGHT
, &i
);
2238 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2239 if ((error
= xfs_btree_delete(cur
, &i
)))
2241 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2242 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2244 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2245 error
= xfs_bmbt_update(cur
, &PREV
);
2251 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
2253 * Setting all of a previous oldext extent to newext.
2254 * Neither the left nor right neighbors are contiguous with
2257 PREV
.br_state
= new->br_state
;
2258 xfs_iext_update_extent(ip
, state
, icur
, &PREV
);
2261 rval
= XFS_ILOG_DEXT
;
2264 error
= xfs_bmbt_lookup_eq(cur
, new, &i
);
2267 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2268 error
= xfs_bmbt_update(cur
, &PREV
);
2274 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
:
2276 * Setting the first part of a previous oldext extent to newext.
2277 * The left neighbor is contiguous.
2279 LEFT
.br_blockcount
+= new->br_blockcount
;
2282 PREV
.br_startoff
+= new->br_blockcount
;
2283 PREV
.br_startblock
+= new->br_blockcount
;
2284 PREV
.br_blockcount
-= new->br_blockcount
;
2286 xfs_iext_update_extent(ip
, state
, icur
, &PREV
);
2287 xfs_iext_prev(ifp
, icur
);
2288 xfs_iext_update_extent(ip
, state
, icur
, &LEFT
);
2291 rval
= XFS_ILOG_DEXT
;
2294 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2297 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2298 error
= xfs_bmbt_update(cur
, &PREV
);
2301 error
= xfs_btree_decrement(cur
, 0, &i
);
2304 error
= xfs_bmbt_update(cur
, &LEFT
);
2310 case BMAP_LEFT_FILLING
:
2312 * Setting the first part of a previous oldext extent to newext.
2313 * The left neighbor is not contiguous.
2316 PREV
.br_startoff
+= new->br_blockcount
;
2317 PREV
.br_startblock
+= new->br_blockcount
;
2318 PREV
.br_blockcount
-= new->br_blockcount
;
2320 xfs_iext_update_extent(ip
, state
, icur
, &PREV
);
2321 xfs_iext_insert(ip
, icur
, new, state
);
2322 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2323 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
2325 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2327 rval
= XFS_ILOG_CORE
;
2328 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2331 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2332 error
= xfs_bmbt_update(cur
, &PREV
);
2335 cur
->bc_rec
.b
= *new;
2336 if ((error
= xfs_btree_insert(cur
, &i
)))
2338 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2342 case BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2344 * Setting the last part of a previous oldext extent to newext.
2345 * The right neighbor is contiguous with the new allocation.
2348 PREV
.br_blockcount
-= new->br_blockcount
;
2350 RIGHT
.br_startoff
= new->br_startoff
;
2351 RIGHT
.br_startblock
= new->br_startblock
;
2352 RIGHT
.br_blockcount
+= new->br_blockcount
;
2354 xfs_iext_update_extent(ip
, state
, icur
, &PREV
);
2355 xfs_iext_next(ifp
, icur
);
2356 xfs_iext_update_extent(ip
, state
, icur
, &RIGHT
);
2359 rval
= XFS_ILOG_DEXT
;
2362 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2365 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2366 error
= xfs_bmbt_update(cur
, &PREV
);
2369 error
= xfs_btree_increment(cur
, 0, &i
);
2372 error
= xfs_bmbt_update(cur
, &RIGHT
);
2378 case BMAP_RIGHT_FILLING
:
2380 * Setting the last part of a previous oldext extent to newext.
2381 * The right neighbor is not contiguous.
2384 PREV
.br_blockcount
-= new->br_blockcount
;
2386 xfs_iext_update_extent(ip
, state
, icur
, &PREV
);
2387 xfs_iext_next(ifp
, icur
);
2388 xfs_iext_insert(ip
, icur
, new, state
);
2390 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2391 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
2393 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2395 rval
= XFS_ILOG_CORE
;
2396 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2399 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2400 error
= xfs_bmbt_update(cur
, &PREV
);
2403 error
= xfs_bmbt_lookup_eq(cur
, new, &i
);
2406 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
2407 if ((error
= xfs_btree_insert(cur
, &i
)))
2409 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2415 * Setting the middle part of a previous oldext extent to
2416 * newext. Contiguity is impossible here.
2417 * One extent becomes three extents.
2420 PREV
.br_blockcount
= new->br_startoff
- PREV
.br_startoff
;
2423 r
[1].br_startoff
= new_endoff
;
2424 r
[1].br_blockcount
=
2425 old
.br_startoff
+ old
.br_blockcount
- new_endoff
;
2426 r
[1].br_startblock
= new->br_startblock
+ new->br_blockcount
;
2427 r
[1].br_state
= PREV
.br_state
;
2429 xfs_iext_update_extent(ip
, state
, icur
, &PREV
);
2430 xfs_iext_next(ifp
, icur
);
2431 xfs_iext_insert(ip
, icur
, &r
[1], state
);
2432 xfs_iext_insert(ip
, icur
, &r
[0], state
);
2434 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2435 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 2);
2437 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2439 rval
= XFS_ILOG_CORE
;
2440 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2443 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2444 /* new right extent - oldext */
2445 error
= xfs_bmbt_update(cur
, &r
[1]);
2448 /* new left extent - oldext */
2449 cur
->bc_rec
.b
= PREV
;
2450 if ((error
= xfs_btree_insert(cur
, &i
)))
2452 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2454 * Reset the cursor to the position of the new extent
2455 * we are about to insert as we can't trust it after
2456 * the previous insert.
2458 error
= xfs_bmbt_lookup_eq(cur
, new, &i
);
2461 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
2462 /* new middle extent - newext */
2463 if ((error
= xfs_btree_insert(cur
, &i
)))
2465 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2469 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2470 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2471 case BMAP_LEFT_FILLING
| BMAP_RIGHT_CONTIG
:
2472 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
2473 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2474 case BMAP_LEFT_CONTIG
:
2475 case BMAP_RIGHT_CONTIG
:
2477 * These cases are all impossible.
2482 /* update reverse mappings */
2483 error
= xfs_rmap_convert_extent(mp
, dfops
, ip
, whichfork
, new);
2487 /* convert to a btree if necessary */
2488 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
2489 int tmp_logflags
; /* partial log flag return val */
2491 ASSERT(cur
== NULL
);
2492 error
= xfs_bmap_extents_to_btree(tp
, ip
, first
, dfops
, &cur
,
2493 0, &tmp_logflags
, whichfork
);
2494 *logflagsp
|= tmp_logflags
;
2499 /* clear out the allocated field, done with it now in any case. */
2501 cur
->bc_private
.b
.allocated
= 0;
2505 xfs_bmap_check_leaf_extents(*curp
, ip
, whichfork
);
2515 * Convert a hole to a delayed allocation.
2518 xfs_bmap_add_extent_hole_delay(
2519 xfs_inode_t
*ip
, /* incore inode pointer */
2521 struct xfs_iext_cursor
*icur
,
2522 xfs_bmbt_irec_t
*new) /* new data to add to file extents */
2524 xfs_ifork_t
*ifp
; /* inode fork pointer */
2525 xfs_bmbt_irec_t left
; /* left neighbor extent entry */
2526 xfs_filblks_t newlen
=0; /* new indirect size */
2527 xfs_filblks_t oldlen
=0; /* old indirect size */
2528 xfs_bmbt_irec_t right
; /* right neighbor extent entry */
2529 int state
= xfs_bmap_fork_to_state(whichfork
);
2530 xfs_filblks_t temp
; /* temp for indirect calculations */
2532 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
2533 ASSERT(isnullstartblock(new->br_startblock
));
2536 * Check and set flags if this segment has a left neighbor
2538 if (xfs_iext_peek_prev_extent(ifp
, icur
, &left
)) {
2539 state
|= BMAP_LEFT_VALID
;
2540 if (isnullstartblock(left
.br_startblock
))
2541 state
|= BMAP_LEFT_DELAY
;
2545 * Check and set flags if the current (right) segment exists.
2546 * If it doesn't exist, we're converting the hole at end-of-file.
2548 if (xfs_iext_get_extent(ifp
, icur
, &right
)) {
2549 state
|= BMAP_RIGHT_VALID
;
2550 if (isnullstartblock(right
.br_startblock
))
2551 state
|= BMAP_RIGHT_DELAY
;
2555 * Set contiguity flags on the left and right neighbors.
2556 * Don't let extents get too large, even if the pieces are contiguous.
2558 if ((state
& BMAP_LEFT_VALID
) && (state
& BMAP_LEFT_DELAY
) &&
2559 left
.br_startoff
+ left
.br_blockcount
== new->br_startoff
&&
2560 left
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
2561 state
|= BMAP_LEFT_CONTIG
;
2563 if ((state
& BMAP_RIGHT_VALID
) && (state
& BMAP_RIGHT_DELAY
) &&
2564 new->br_startoff
+ new->br_blockcount
== right
.br_startoff
&&
2565 new->br_blockcount
+ right
.br_blockcount
<= MAXEXTLEN
&&
2566 (!(state
& BMAP_LEFT_CONTIG
) ||
2567 (left
.br_blockcount
+ new->br_blockcount
+
2568 right
.br_blockcount
<= MAXEXTLEN
)))
2569 state
|= BMAP_RIGHT_CONTIG
;
2572 * Switch out based on the contiguity flags.
2574 switch (state
& (BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
)) {
2575 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2577 * New allocation is contiguous with delayed allocations
2578 * on the left and on the right.
2579 * Merge all three into a single extent record.
2581 temp
= left
.br_blockcount
+ new->br_blockcount
+
2582 right
.br_blockcount
;
2584 oldlen
= startblockval(left
.br_startblock
) +
2585 startblockval(new->br_startblock
) +
2586 startblockval(right
.br_startblock
);
2587 newlen
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
2589 left
.br_startblock
= nullstartblock(newlen
);
2590 left
.br_blockcount
= temp
;
2592 xfs_iext_remove(ip
, icur
, state
);
2593 xfs_iext_prev(ifp
, icur
);
2594 xfs_iext_update_extent(ip
, state
, icur
, &left
);
2597 case BMAP_LEFT_CONTIG
:
2599 * New allocation is contiguous with a delayed allocation
2601 * Merge the new allocation with the left neighbor.
2603 temp
= left
.br_blockcount
+ new->br_blockcount
;
2605 oldlen
= startblockval(left
.br_startblock
) +
2606 startblockval(new->br_startblock
);
2607 newlen
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
2609 left
.br_blockcount
= temp
;
2610 left
.br_startblock
= nullstartblock(newlen
);
2612 xfs_iext_prev(ifp
, icur
);
2613 xfs_iext_update_extent(ip
, state
, icur
, &left
);
2616 case BMAP_RIGHT_CONTIG
:
2618 * New allocation is contiguous with a delayed allocation
2620 * Merge the new allocation with the right neighbor.
2622 temp
= new->br_blockcount
+ right
.br_blockcount
;
2623 oldlen
= startblockval(new->br_startblock
) +
2624 startblockval(right
.br_startblock
);
2625 newlen
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
2627 right
.br_startoff
= new->br_startoff
;
2628 right
.br_startblock
= nullstartblock(newlen
);
2629 right
.br_blockcount
= temp
;
2630 xfs_iext_update_extent(ip
, state
, icur
, &right
);
2635 * New allocation is not contiguous with another
2636 * delayed allocation.
2637 * Insert a new entry.
2639 oldlen
= newlen
= 0;
2640 xfs_iext_insert(ip
, icur
, new, state
);
2643 if (oldlen
!= newlen
) {
2644 ASSERT(oldlen
> newlen
);
2645 xfs_mod_fdblocks(ip
->i_mount
, (int64_t)(oldlen
- newlen
),
2648 * Nothing to do for disk quota accounting here.
2654 * Convert a hole to a real allocation.
2656 STATIC
int /* error */
2657 xfs_bmap_add_extent_hole_real(
2658 struct xfs_trans
*tp
,
2659 struct xfs_inode
*ip
,
2661 struct xfs_iext_cursor
*icur
,
2662 struct xfs_btree_cur
**curp
,
2663 struct xfs_bmbt_irec
*new,
2664 xfs_fsblock_t
*first
,
2665 struct xfs_defer_ops
*dfops
,
2668 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
2669 struct xfs_mount
*mp
= ip
->i_mount
;
2670 struct xfs_btree_cur
*cur
= *curp
;
2671 int error
; /* error return value */
2672 int i
; /* temp state */
2673 xfs_bmbt_irec_t left
; /* left neighbor extent entry */
2674 xfs_bmbt_irec_t right
; /* right neighbor extent entry */
2675 int rval
=0; /* return value (logging flags) */
2676 int state
= xfs_bmap_fork_to_state(whichfork
);
2677 struct xfs_bmbt_irec old
;
2679 ASSERT(!isnullstartblock(new->br_startblock
));
2680 ASSERT(!cur
|| !(cur
->bc_private
.b
.flags
& XFS_BTCUR_BPRV_WASDEL
));
2682 XFS_STATS_INC(mp
, xs_add_exlist
);
2685 * Check and set flags if this segment has a left neighbor.
2687 if (xfs_iext_peek_prev_extent(ifp
, icur
, &left
)) {
2688 state
|= BMAP_LEFT_VALID
;
2689 if (isnullstartblock(left
.br_startblock
))
2690 state
|= BMAP_LEFT_DELAY
;
2694 * Check and set flags if this segment has a current value.
2695 * Not true if we're inserting into the "hole" at eof.
2697 if (xfs_iext_get_extent(ifp
, icur
, &right
)) {
2698 state
|= BMAP_RIGHT_VALID
;
2699 if (isnullstartblock(right
.br_startblock
))
2700 state
|= BMAP_RIGHT_DELAY
;
2704 * We're inserting a real allocation between "left" and "right".
2705 * Set the contiguity flags. Don't let extents get too large.
2707 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
2708 left
.br_startoff
+ left
.br_blockcount
== new->br_startoff
&&
2709 left
.br_startblock
+ left
.br_blockcount
== new->br_startblock
&&
2710 left
.br_state
== new->br_state
&&
2711 left
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
2712 state
|= BMAP_LEFT_CONTIG
;
2714 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
2715 new->br_startoff
+ new->br_blockcount
== right
.br_startoff
&&
2716 new->br_startblock
+ new->br_blockcount
== right
.br_startblock
&&
2717 new->br_state
== right
.br_state
&&
2718 new->br_blockcount
+ right
.br_blockcount
<= MAXEXTLEN
&&
2719 (!(state
& BMAP_LEFT_CONTIG
) ||
2720 left
.br_blockcount
+ new->br_blockcount
+
2721 right
.br_blockcount
<= MAXEXTLEN
))
2722 state
|= BMAP_RIGHT_CONTIG
;
2726 * Select which case we're in here, and implement it.
2728 switch (state
& (BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
)) {
2729 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2731 * New allocation is contiguous with real allocations on the
2732 * left and on the right.
2733 * Merge all three into a single extent record.
2735 left
.br_blockcount
+= new->br_blockcount
+ right
.br_blockcount
;
2737 xfs_iext_remove(ip
, icur
, state
);
2738 xfs_iext_prev(ifp
, icur
);
2739 xfs_iext_update_extent(ip
, state
, icur
, &left
);
2741 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2742 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
2744 rval
= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
2746 rval
= XFS_ILOG_CORE
;
2747 error
= xfs_bmbt_lookup_eq(cur
, &right
, &i
);
2750 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2751 error
= xfs_btree_delete(cur
, &i
);
2754 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2755 error
= xfs_btree_decrement(cur
, 0, &i
);
2758 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2759 error
= xfs_bmbt_update(cur
, &left
);
2765 case BMAP_LEFT_CONTIG
:
2767 * New allocation is contiguous with a real allocation
2769 * Merge the new allocation with the left neighbor.
2772 left
.br_blockcount
+= new->br_blockcount
;
2774 xfs_iext_prev(ifp
, icur
);
2775 xfs_iext_update_extent(ip
, state
, icur
, &left
);
2778 rval
= xfs_ilog_fext(whichfork
);
2781 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2784 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2785 error
= xfs_bmbt_update(cur
, &left
);
2791 case BMAP_RIGHT_CONTIG
:
2793 * New allocation is contiguous with a real allocation
2795 * Merge the new allocation with the right neighbor.
2799 right
.br_startoff
= new->br_startoff
;
2800 right
.br_startblock
= new->br_startblock
;
2801 right
.br_blockcount
+= new->br_blockcount
;
2802 xfs_iext_update_extent(ip
, state
, icur
, &right
);
2805 rval
= xfs_ilog_fext(whichfork
);
2808 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2811 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2812 error
= xfs_bmbt_update(cur
, &right
);
2820 * New allocation is not contiguous with another
2822 * Insert a new entry.
2824 xfs_iext_insert(ip
, icur
, new, state
);
2825 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2826 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
2828 rval
= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
2830 rval
= XFS_ILOG_CORE
;
2831 error
= xfs_bmbt_lookup_eq(cur
, new, &i
);
2834 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
2835 error
= xfs_btree_insert(cur
, &i
);
2838 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2843 /* add reverse mapping */
2844 error
= xfs_rmap_map_extent(mp
, dfops
, ip
, whichfork
, new);
2848 /* convert to a btree if necessary */
2849 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
2850 int tmp_logflags
; /* partial log flag return val */
2852 ASSERT(cur
== NULL
);
2853 error
= xfs_bmap_extents_to_btree(tp
, ip
, first
, dfops
, curp
,
2854 0, &tmp_logflags
, whichfork
);
2855 *logflagsp
|= tmp_logflags
;
2861 /* clear out the allocated field, done with it now in any case. */
2863 cur
->bc_private
.b
.allocated
= 0;
2865 xfs_bmap_check_leaf_extents(cur
, ip
, whichfork
);
2872 * Functions used in the extent read, allocate and remove paths
2876 * Adjust the size of the new extent based on di_extsize and rt extsize.
2879 xfs_bmap_extsize_align(
2881 xfs_bmbt_irec_t
*gotp
, /* next extent pointer */
2882 xfs_bmbt_irec_t
*prevp
, /* previous extent pointer */
2883 xfs_extlen_t extsz
, /* align to this extent size */
2884 int rt
, /* is this a realtime inode? */
2885 int eof
, /* is extent at end-of-file? */
2886 int delay
, /* creating delalloc extent? */
2887 int convert
, /* overwriting unwritten extent? */
2888 xfs_fileoff_t
*offp
, /* in/out: aligned offset */
2889 xfs_extlen_t
*lenp
) /* in/out: aligned length */
2891 xfs_fileoff_t orig_off
; /* original offset */
2892 xfs_extlen_t orig_alen
; /* original length */
2893 xfs_fileoff_t orig_end
; /* original off+len */
2894 xfs_fileoff_t nexto
; /* next file offset */
2895 xfs_fileoff_t prevo
; /* previous file offset */
2896 xfs_fileoff_t align_off
; /* temp for offset */
2897 xfs_extlen_t align_alen
; /* temp for length */
2898 xfs_extlen_t temp
; /* temp for calculations */
2903 orig_off
= align_off
= *offp
;
2904 orig_alen
= align_alen
= *lenp
;
2905 orig_end
= orig_off
+ orig_alen
;
2908 * If this request overlaps an existing extent, then don't
2909 * attempt to perform any additional alignment.
2911 if (!delay
&& !eof
&&
2912 (orig_off
>= gotp
->br_startoff
) &&
2913 (orig_end
<= gotp
->br_startoff
+ gotp
->br_blockcount
)) {
2918 * If the file offset is unaligned vs. the extent size
2919 * we need to align it. This will be possible unless
2920 * the file was previously written with a kernel that didn't
2921 * perform this alignment, or if a truncate shot us in the
2924 temp
= do_mod(orig_off
, extsz
);
2930 /* Same adjustment for the end of the requested area. */
2931 temp
= (align_alen
% extsz
);
2933 align_alen
+= extsz
- temp
;
2936 * For large extent hint sizes, the aligned extent might be larger than
2937 * MAXEXTLEN. In that case, reduce the size by an extsz so that it pulls
2938 * the length back under MAXEXTLEN. The outer allocation loops handle
2939 * short allocation just fine, so it is safe to do this. We only want to
2940 * do it when we are forced to, though, because it means more allocation
2941 * operations are required.
2943 while (align_alen
> MAXEXTLEN
)
2944 align_alen
-= extsz
;
2945 ASSERT(align_alen
<= MAXEXTLEN
);
2948 * If the previous block overlaps with this proposed allocation
2949 * then move the start forward without adjusting the length.
2951 if (prevp
->br_startoff
!= NULLFILEOFF
) {
2952 if (prevp
->br_startblock
== HOLESTARTBLOCK
)
2953 prevo
= prevp
->br_startoff
;
2955 prevo
= prevp
->br_startoff
+ prevp
->br_blockcount
;
2958 if (align_off
!= orig_off
&& align_off
< prevo
)
2961 * If the next block overlaps with this proposed allocation
2962 * then move the start back without adjusting the length,
2963 * but not before offset 0.
2964 * This may of course make the start overlap previous block,
2965 * and if we hit the offset 0 limit then the next block
2966 * can still overlap too.
2968 if (!eof
&& gotp
->br_startoff
!= NULLFILEOFF
) {
2969 if ((delay
&& gotp
->br_startblock
== HOLESTARTBLOCK
) ||
2970 (!delay
&& gotp
->br_startblock
== DELAYSTARTBLOCK
))
2971 nexto
= gotp
->br_startoff
+ gotp
->br_blockcount
;
2973 nexto
= gotp
->br_startoff
;
2975 nexto
= NULLFILEOFF
;
2977 align_off
+ align_alen
!= orig_end
&&
2978 align_off
+ align_alen
> nexto
)
2979 align_off
= nexto
> align_alen
? nexto
- align_alen
: 0;
2981 * If we're now overlapping the next or previous extent that
2982 * means we can't fit an extsz piece in this hole. Just move
2983 * the start forward to the first valid spot and set
2984 * the length so we hit the end.
2986 if (align_off
!= orig_off
&& align_off
< prevo
)
2988 if (align_off
+ align_alen
!= orig_end
&&
2989 align_off
+ align_alen
> nexto
&&
2990 nexto
!= NULLFILEOFF
) {
2991 ASSERT(nexto
> prevo
);
2992 align_alen
= nexto
- align_off
;
2996 * If realtime, and the result isn't a multiple of the realtime
2997 * extent size we need to remove blocks until it is.
2999 if (rt
&& (temp
= (align_alen
% mp
->m_sb
.sb_rextsize
))) {
3001 * We're not covering the original request, or
3002 * we won't be able to once we fix the length.
3004 if (orig_off
< align_off
||
3005 orig_end
> align_off
+ align_alen
||
3006 align_alen
- temp
< orig_alen
)
3009 * Try to fix it by moving the start up.
3011 if (align_off
+ temp
<= orig_off
) {
3016 * Try to fix it by moving the end in.
3018 else if (align_off
+ align_alen
- temp
>= orig_end
)
3021 * Set the start to the minimum then trim the length.
3024 align_alen
-= orig_off
- align_off
;
3025 align_off
= orig_off
;
3026 align_alen
-= align_alen
% mp
->m_sb
.sb_rextsize
;
3029 * Result doesn't cover the request, fail it.
3031 if (orig_off
< align_off
|| orig_end
> align_off
+ align_alen
)
3034 ASSERT(orig_off
>= align_off
);
3035 /* see MAXEXTLEN handling above */
3036 ASSERT(orig_end
<= align_off
+ align_alen
||
3037 align_alen
+ extsz
> MAXEXTLEN
);
3041 if (!eof
&& gotp
->br_startoff
!= NULLFILEOFF
)
3042 ASSERT(align_off
+ align_alen
<= gotp
->br_startoff
);
3043 if (prevp
->br_startoff
!= NULLFILEOFF
)
3044 ASSERT(align_off
>= prevp
->br_startoff
+ prevp
->br_blockcount
);
3052 #define XFS_ALLOC_GAP_UNITS 4
3056 struct xfs_bmalloca
*ap
) /* bmap alloc argument struct */
3058 xfs_fsblock_t adjust
; /* adjustment to block numbers */
3059 xfs_agnumber_t fb_agno
; /* ag number of ap->firstblock */
3060 xfs_mount_t
*mp
; /* mount point structure */
3061 int nullfb
; /* true if ap->firstblock isn't set */
3062 int rt
; /* true if inode is realtime */
3064 #define ISVALID(x,y) \
3066 (x) < mp->m_sb.sb_rblocks : \
3067 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
3068 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
3069 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
3071 mp
= ap
->ip
->i_mount
;
3072 nullfb
= *ap
->firstblock
== NULLFSBLOCK
;
3073 rt
= XFS_IS_REALTIME_INODE(ap
->ip
) &&
3074 xfs_alloc_is_userdata(ap
->datatype
);
3075 fb_agno
= nullfb
? NULLAGNUMBER
: XFS_FSB_TO_AGNO(mp
, *ap
->firstblock
);
3077 * If allocating at eof, and there's a previous real block,
3078 * try to use its last block as our starting point.
3080 if (ap
->eof
&& ap
->prev
.br_startoff
!= NULLFILEOFF
&&
3081 !isnullstartblock(ap
->prev
.br_startblock
) &&
3082 ISVALID(ap
->prev
.br_startblock
+ ap
->prev
.br_blockcount
,
3083 ap
->prev
.br_startblock
)) {
3084 ap
->blkno
= ap
->prev
.br_startblock
+ ap
->prev
.br_blockcount
;
3086 * Adjust for the gap between prevp and us.
3088 adjust
= ap
->offset
-
3089 (ap
->prev
.br_startoff
+ ap
->prev
.br_blockcount
);
3091 ISVALID(ap
->blkno
+ adjust
, ap
->prev
.br_startblock
))
3092 ap
->blkno
+= adjust
;
3095 * If not at eof, then compare the two neighbor blocks.
3096 * Figure out whether either one gives us a good starting point,
3097 * and pick the better one.
3099 else if (!ap
->eof
) {
3100 xfs_fsblock_t gotbno
; /* right side block number */
3101 xfs_fsblock_t gotdiff
=0; /* right side difference */
3102 xfs_fsblock_t prevbno
; /* left side block number */
3103 xfs_fsblock_t prevdiff
=0; /* left side difference */
3106 * If there's a previous (left) block, select a requested
3107 * start block based on it.
3109 if (ap
->prev
.br_startoff
!= NULLFILEOFF
&&
3110 !isnullstartblock(ap
->prev
.br_startblock
) &&
3111 (prevbno
= ap
->prev
.br_startblock
+
3112 ap
->prev
.br_blockcount
) &&
3113 ISVALID(prevbno
, ap
->prev
.br_startblock
)) {
3115 * Calculate gap to end of previous block.
3117 adjust
= prevdiff
= ap
->offset
-
3118 (ap
->prev
.br_startoff
+
3119 ap
->prev
.br_blockcount
);
3121 * Figure the startblock based on the previous block's
3122 * end and the gap size.
3124 * If the gap is large relative to the piece we're
3125 * allocating, or using it gives us an invalid block
3126 * number, then just use the end of the previous block.
3128 if (prevdiff
<= XFS_ALLOC_GAP_UNITS
* ap
->length
&&
3129 ISVALID(prevbno
+ prevdiff
,
3130 ap
->prev
.br_startblock
))
3135 * If the firstblock forbids it, can't use it,
3138 if (!rt
&& !nullfb
&&
3139 XFS_FSB_TO_AGNO(mp
, prevbno
) != fb_agno
)
3140 prevbno
= NULLFSBLOCK
;
3143 * No previous block or can't follow it, just default.
3146 prevbno
= NULLFSBLOCK
;
3148 * If there's a following (right) block, select a requested
3149 * start block based on it.
3151 if (!isnullstartblock(ap
->got
.br_startblock
)) {
3153 * Calculate gap to start of next block.
3155 adjust
= gotdiff
= ap
->got
.br_startoff
- ap
->offset
;
3157 * Figure the startblock based on the next block's
3158 * start and the gap size.
3160 gotbno
= ap
->got
.br_startblock
;
3163 * If the gap is large relative to the piece we're
3164 * allocating, or using it gives us an invalid block
3165 * number, then just use the start of the next block
3166 * offset by our length.
3168 if (gotdiff
<= XFS_ALLOC_GAP_UNITS
* ap
->length
&&
3169 ISVALID(gotbno
- gotdiff
, gotbno
))
3171 else if (ISVALID(gotbno
- ap
->length
, gotbno
)) {
3172 gotbno
-= ap
->length
;
3173 gotdiff
+= adjust
- ap
->length
;
3177 * If the firstblock forbids it, can't use it,
3180 if (!rt
&& !nullfb
&&
3181 XFS_FSB_TO_AGNO(mp
, gotbno
) != fb_agno
)
3182 gotbno
= NULLFSBLOCK
;
3185 * No next block, just default.
3188 gotbno
= NULLFSBLOCK
;
3190 * If both valid, pick the better one, else the only good
3191 * one, else ap->blkno is already set (to 0 or the inode block).
3193 if (prevbno
!= NULLFSBLOCK
&& gotbno
!= NULLFSBLOCK
)
3194 ap
->blkno
= prevdiff
<= gotdiff
? prevbno
: gotbno
;
3195 else if (prevbno
!= NULLFSBLOCK
)
3196 ap
->blkno
= prevbno
;
3197 else if (gotbno
!= NULLFSBLOCK
)
3204 xfs_bmap_longest_free_extent(
3205 struct xfs_trans
*tp
,
3210 struct xfs_mount
*mp
= tp
->t_mountp
;
3211 struct xfs_perag
*pag
;
3212 xfs_extlen_t longest
;
3215 pag
= xfs_perag_get(mp
, ag
);
3216 if (!pag
->pagf_init
) {
3217 error
= xfs_alloc_pagf_init(mp
, tp
, ag
, XFS_ALLOC_FLAG_TRYLOCK
);
3221 if (!pag
->pagf_init
) {
3227 longest
= xfs_alloc_longest_free_extent(mp
, pag
,
3228 xfs_alloc_min_freelist(mp
, pag
),
3229 xfs_ag_resv_needed(pag
, XFS_AG_RESV_NONE
));
3230 if (*blen
< longest
)
3239 xfs_bmap_select_minlen(
3240 struct xfs_bmalloca
*ap
,
3241 struct xfs_alloc_arg
*args
,
3245 if (notinit
|| *blen
< ap
->minlen
) {
3247 * Since we did a BUF_TRYLOCK above, it is possible that
3248 * there is space for this request.
3250 args
->minlen
= ap
->minlen
;
3251 } else if (*blen
< args
->maxlen
) {
3253 * If the best seen length is less than the request length,
3254 * use the best as the minimum.
3256 args
->minlen
= *blen
;
3259 * Otherwise we've seen an extent as big as maxlen, use that
3262 args
->minlen
= args
->maxlen
;
3267 xfs_bmap_btalloc_nullfb(
3268 struct xfs_bmalloca
*ap
,
3269 struct xfs_alloc_arg
*args
,
3272 struct xfs_mount
*mp
= ap
->ip
->i_mount
;
3273 xfs_agnumber_t ag
, startag
;
3277 args
->type
= XFS_ALLOCTYPE_START_BNO
;
3278 args
->total
= ap
->total
;
3280 startag
= ag
= XFS_FSB_TO_AGNO(mp
, args
->fsbno
);
3281 if (startag
== NULLAGNUMBER
)
3284 while (*blen
< args
->maxlen
) {
3285 error
= xfs_bmap_longest_free_extent(args
->tp
, ag
, blen
,
3290 if (++ag
== mp
->m_sb
.sb_agcount
)
3296 xfs_bmap_select_minlen(ap
, args
, blen
, notinit
);
3301 xfs_bmap_btalloc_filestreams(
3302 struct xfs_bmalloca
*ap
,
3303 struct xfs_alloc_arg
*args
,
3306 struct xfs_mount
*mp
= ap
->ip
->i_mount
;
3311 args
->type
= XFS_ALLOCTYPE_NEAR_BNO
;
3312 args
->total
= ap
->total
;
3314 ag
= XFS_FSB_TO_AGNO(mp
, args
->fsbno
);
3315 if (ag
== NULLAGNUMBER
)
3318 error
= xfs_bmap_longest_free_extent(args
->tp
, ag
, blen
, ¬init
);
3322 if (*blen
< args
->maxlen
) {
3323 error
= xfs_filestream_new_ag(ap
, &ag
);
3327 error
= xfs_bmap_longest_free_extent(args
->tp
, ag
, blen
,
3334 xfs_bmap_select_minlen(ap
, args
, blen
, notinit
);
3337 * Set the failure fallback case to look in the selected AG as stream
3340 ap
->blkno
= args
->fsbno
= XFS_AGB_TO_FSB(mp
, ag
, 0);
3346 struct xfs_bmalloca
*ap
) /* bmap alloc argument struct */
3348 xfs_mount_t
*mp
; /* mount point structure */
3349 xfs_alloctype_t atype
= 0; /* type for allocation routines */
3350 xfs_extlen_t align
= 0; /* minimum allocation alignment */
3351 xfs_agnumber_t fb_agno
; /* ag number of ap->firstblock */
3353 xfs_alloc_arg_t args
;
3355 xfs_extlen_t nextminlen
= 0;
3356 int nullfb
; /* true if ap->firstblock isn't set */
3364 mp
= ap
->ip
->i_mount
;
3366 /* stripe alignment for allocation is determined by mount parameters */
3368 if (mp
->m_swidth
&& (mp
->m_flags
& XFS_MOUNT_SWALLOC
))
3369 stripe_align
= mp
->m_swidth
;
3370 else if (mp
->m_dalign
)
3371 stripe_align
= mp
->m_dalign
;
3373 if (ap
->flags
& XFS_BMAPI_COWFORK
)
3374 align
= xfs_get_cowextsz_hint(ap
->ip
);
3375 else if (xfs_alloc_is_userdata(ap
->datatype
))
3376 align
= xfs_get_extsz_hint(ap
->ip
);
3378 error
= xfs_bmap_extsize_align(mp
, &ap
->got
, &ap
->prev
,
3379 align
, 0, ap
->eof
, 0, ap
->conv
,
3380 &ap
->offset
, &ap
->length
);
3386 nullfb
= *ap
->firstblock
== NULLFSBLOCK
;
3387 fb_agno
= nullfb
? NULLAGNUMBER
: XFS_FSB_TO_AGNO(mp
, *ap
->firstblock
);
3389 if (xfs_alloc_is_userdata(ap
->datatype
) &&
3390 xfs_inode_is_filestream(ap
->ip
)) {
3391 ag
= xfs_filestream_lookup_ag(ap
->ip
);
3392 ag
= (ag
!= NULLAGNUMBER
) ? ag
: 0;
3393 ap
->blkno
= XFS_AGB_TO_FSB(mp
, ag
, 0);
3395 ap
->blkno
= XFS_INO_TO_FSB(mp
, ap
->ip
->i_ino
);
3398 ap
->blkno
= *ap
->firstblock
;
3400 xfs_bmap_adjacent(ap
);
3403 * If allowed, use ap->blkno; otherwise must use firstblock since
3404 * it's in the right allocation group.
3406 if (nullfb
|| XFS_FSB_TO_AGNO(mp
, ap
->blkno
) == fb_agno
)
3409 ap
->blkno
= *ap
->firstblock
;
3411 * Normal allocation, done through xfs_alloc_vextent.
3413 tryagain
= isaligned
= 0;
3414 memset(&args
, 0, sizeof(args
));
3417 args
.fsbno
= ap
->blkno
;
3418 xfs_rmap_skip_owner_update(&args
.oinfo
);
3420 /* Trim the allocation back to the maximum an AG can fit. */
3421 args
.maxlen
= MIN(ap
->length
, mp
->m_ag_max_usable
);
3422 args
.firstblock
= *ap
->firstblock
;
3426 * Search for an allocation group with a single extent large
3427 * enough for the request. If one isn't found, then adjust
3428 * the minimum allocation size to the largest space found.
3430 if (xfs_alloc_is_userdata(ap
->datatype
) &&
3431 xfs_inode_is_filestream(ap
->ip
))
3432 error
= xfs_bmap_btalloc_filestreams(ap
, &args
, &blen
);
3434 error
= xfs_bmap_btalloc_nullfb(ap
, &args
, &blen
);
3437 } else if (ap
->dfops
->dop_low
) {
3438 if (xfs_inode_is_filestream(ap
->ip
))
3439 args
.type
= XFS_ALLOCTYPE_FIRST_AG
;
3441 args
.type
= XFS_ALLOCTYPE_START_BNO
;
3442 args
.total
= args
.minlen
= ap
->minlen
;
3444 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
3445 args
.total
= ap
->total
;
3446 args
.minlen
= ap
->minlen
;
3448 /* apply extent size hints if obtained earlier */
3451 if ((args
.mod
= (xfs_extlen_t
)do_mod(ap
->offset
, args
.prod
)))
3452 args
.mod
= (xfs_extlen_t
)(args
.prod
- args
.mod
);
3453 } else if (mp
->m_sb
.sb_blocksize
>= PAGE_SIZE
) {
3457 args
.prod
= PAGE_SIZE
>> mp
->m_sb
.sb_blocklog
;
3458 if ((args
.mod
= (xfs_extlen_t
)(do_mod(ap
->offset
, args
.prod
))))
3459 args
.mod
= (xfs_extlen_t
)(args
.prod
- args
.mod
);
3462 * If we are not low on available data blocks, and the
3463 * underlying logical volume manager is a stripe, and
3464 * the file offset is zero then try to allocate data
3465 * blocks on stripe unit boundary.
3466 * NOTE: ap->aeof is only set if the allocation length
3467 * is >= the stripe unit and the allocation offset is
3468 * at the end of file.
3470 if (!ap
->dfops
->dop_low
&& ap
->aeof
) {
3472 args
.alignment
= stripe_align
;
3476 * Adjust for alignment
3478 if (blen
> args
.alignment
&& blen
<= args
.maxlen
)
3479 args
.minlen
= blen
- args
.alignment
;
3480 args
.minalignslop
= 0;
3483 * First try an exact bno allocation.
3484 * If it fails then do a near or start bno
3485 * allocation with alignment turned on.
3489 args
.type
= XFS_ALLOCTYPE_THIS_BNO
;
3492 * Compute the minlen+alignment for the
3493 * next case. Set slop so that the value
3494 * of minlen+alignment+slop doesn't go up
3495 * between the calls.
3497 if (blen
> stripe_align
&& blen
<= args
.maxlen
)
3498 nextminlen
= blen
- stripe_align
;
3500 nextminlen
= args
.minlen
;
3501 if (nextminlen
+ stripe_align
> args
.minlen
+ 1)
3503 nextminlen
+ stripe_align
-
3506 args
.minalignslop
= 0;
3510 args
.minalignslop
= 0;
3512 args
.minleft
= ap
->minleft
;
3513 args
.wasdel
= ap
->wasdel
;
3514 args
.resv
= XFS_AG_RESV_NONE
;
3515 args
.datatype
= ap
->datatype
;
3516 if (ap
->datatype
& XFS_ALLOC_USERDATA_ZERO
)
3519 error
= xfs_alloc_vextent(&args
);
3523 if (tryagain
&& args
.fsbno
== NULLFSBLOCK
) {
3525 * Exact allocation failed. Now try with alignment
3529 args
.fsbno
= ap
->blkno
;
3530 args
.alignment
= stripe_align
;
3531 args
.minlen
= nextminlen
;
3532 args
.minalignslop
= 0;
3534 if ((error
= xfs_alloc_vextent(&args
)))
3537 if (isaligned
&& args
.fsbno
== NULLFSBLOCK
) {
3539 * allocation failed, so turn off alignment and
3543 args
.fsbno
= ap
->blkno
;
3545 if ((error
= xfs_alloc_vextent(&args
)))
3548 if (args
.fsbno
== NULLFSBLOCK
&& nullfb
&&
3549 args
.minlen
> ap
->minlen
) {
3550 args
.minlen
= ap
->minlen
;
3551 args
.type
= XFS_ALLOCTYPE_START_BNO
;
3552 args
.fsbno
= ap
->blkno
;
3553 if ((error
= xfs_alloc_vextent(&args
)))
3556 if (args
.fsbno
== NULLFSBLOCK
&& nullfb
) {
3558 args
.type
= XFS_ALLOCTYPE_FIRST_AG
;
3559 args
.total
= ap
->minlen
;
3560 if ((error
= xfs_alloc_vextent(&args
)))
3562 ap
->dfops
->dop_low
= true;
3564 if (args
.fsbno
!= NULLFSBLOCK
) {
3566 * check the allocation happened at the same or higher AG than
3567 * the first block that was allocated.
3569 ASSERT(*ap
->firstblock
== NULLFSBLOCK
||
3570 XFS_FSB_TO_AGNO(mp
, *ap
->firstblock
) <=
3571 XFS_FSB_TO_AGNO(mp
, args
.fsbno
));
3573 ap
->blkno
= args
.fsbno
;
3574 if (*ap
->firstblock
== NULLFSBLOCK
)
3575 *ap
->firstblock
= args
.fsbno
;
3576 ASSERT(nullfb
|| fb_agno
<= args
.agno
);
3577 ap
->length
= args
.len
;
3578 if (!(ap
->flags
& XFS_BMAPI_COWFORK
))
3579 ap
->ip
->i_d
.di_nblocks
+= args
.len
;
3580 xfs_trans_log_inode(ap
->tp
, ap
->ip
, XFS_ILOG_CORE
);
3582 ap
->ip
->i_delayed_blks
-= args
.len
;
3584 * Adjust the disk quota also. This was reserved
3587 xfs_trans_mod_dquot_byino(ap
->tp
, ap
->ip
,
3588 ap
->wasdel
? XFS_TRANS_DQ_DELBCOUNT
:
3589 XFS_TRANS_DQ_BCOUNT
,
3592 ap
->blkno
= NULLFSBLOCK
;
3599 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
3600 * It figures out where to ask the underlying allocator to put the new extent.
3604 struct xfs_bmalloca
*ap
) /* bmap alloc argument struct */
3606 if (XFS_IS_REALTIME_INODE(ap
->ip
) &&
3607 xfs_alloc_is_userdata(ap
->datatype
))
3608 return xfs_bmap_rtalloc(ap
);
3609 return xfs_bmap_btalloc(ap
);
3612 /* Trim extent to fit a logical block range. */
3615 struct xfs_bmbt_irec
*irec
,
3619 xfs_fileoff_t distance
;
3620 xfs_fileoff_t end
= bno
+ len
;
3622 if (irec
->br_startoff
+ irec
->br_blockcount
<= bno
||
3623 irec
->br_startoff
>= end
) {
3624 irec
->br_blockcount
= 0;
3628 if (irec
->br_startoff
< bno
) {
3629 distance
= bno
- irec
->br_startoff
;
3630 if (isnullstartblock(irec
->br_startblock
))
3631 irec
->br_startblock
= DELAYSTARTBLOCK
;
3632 if (irec
->br_startblock
!= DELAYSTARTBLOCK
&&
3633 irec
->br_startblock
!= HOLESTARTBLOCK
)
3634 irec
->br_startblock
+= distance
;
3635 irec
->br_startoff
+= distance
;
3636 irec
->br_blockcount
-= distance
;
3639 if (end
< irec
->br_startoff
+ irec
->br_blockcount
) {
3640 distance
= irec
->br_startoff
+ irec
->br_blockcount
- end
;
3641 irec
->br_blockcount
-= distance
;
3645 /* trim extent to within eof */
3647 xfs_trim_extent_eof(
3648 struct xfs_bmbt_irec
*irec
,
3649 struct xfs_inode
*ip
)
3652 xfs_trim_extent(irec
, 0, XFS_B_TO_FSB(ip
->i_mount
,
3653 i_size_read(VFS_I(ip
))));
3657 * Trim the returned map to the required bounds
3661 struct xfs_bmbt_irec
*mval
,
3662 struct xfs_bmbt_irec
*got
,
3670 if ((flags
& XFS_BMAPI_ENTIRE
) ||
3671 got
->br_startoff
+ got
->br_blockcount
<= obno
) {
3673 if (isnullstartblock(got
->br_startblock
))
3674 mval
->br_startblock
= DELAYSTARTBLOCK
;
3680 ASSERT((*bno
>= obno
) || (n
== 0));
3682 mval
->br_startoff
= *bno
;
3683 if (isnullstartblock(got
->br_startblock
))
3684 mval
->br_startblock
= DELAYSTARTBLOCK
;
3686 mval
->br_startblock
= got
->br_startblock
+
3687 (*bno
- got
->br_startoff
);
3689 * Return the minimum of what we got and what we asked for for
3690 * the length. We can use the len variable here because it is
3691 * modified below and we could have been there before coming
3692 * here if the first part of the allocation didn't overlap what
3695 mval
->br_blockcount
= XFS_FILBLKS_MIN(end
- *bno
,
3696 got
->br_blockcount
- (*bno
- got
->br_startoff
));
3697 mval
->br_state
= got
->br_state
;
3698 ASSERT(mval
->br_blockcount
<= len
);
3703 * Update and validate the extent map to return
3706 xfs_bmapi_update_map(
3707 struct xfs_bmbt_irec
**map
,
3715 xfs_bmbt_irec_t
*mval
= *map
;
3717 ASSERT((flags
& XFS_BMAPI_ENTIRE
) ||
3718 ((mval
->br_startoff
+ mval
->br_blockcount
) <= end
));
3719 ASSERT((flags
& XFS_BMAPI_ENTIRE
) || (mval
->br_blockcount
<= *len
) ||
3720 (mval
->br_startoff
< obno
));
3722 *bno
= mval
->br_startoff
+ mval
->br_blockcount
;
3724 if (*n
> 0 && mval
->br_startoff
== mval
[-1].br_startoff
) {
3725 /* update previous map with new information */
3726 ASSERT(mval
->br_startblock
== mval
[-1].br_startblock
);
3727 ASSERT(mval
->br_blockcount
> mval
[-1].br_blockcount
);
3728 ASSERT(mval
->br_state
== mval
[-1].br_state
);
3729 mval
[-1].br_blockcount
= mval
->br_blockcount
;
3730 mval
[-1].br_state
= mval
->br_state
;
3731 } else if (*n
> 0 && mval
->br_startblock
!= DELAYSTARTBLOCK
&&
3732 mval
[-1].br_startblock
!= DELAYSTARTBLOCK
&&
3733 mval
[-1].br_startblock
!= HOLESTARTBLOCK
&&
3734 mval
->br_startblock
== mval
[-1].br_startblock
+
3735 mval
[-1].br_blockcount
&&
3736 ((flags
& XFS_BMAPI_IGSTATE
) ||
3737 mval
[-1].br_state
== mval
->br_state
)) {
3738 ASSERT(mval
->br_startoff
==
3739 mval
[-1].br_startoff
+ mval
[-1].br_blockcount
);
3740 mval
[-1].br_blockcount
+= mval
->br_blockcount
;
3741 } else if (*n
> 0 &&
3742 mval
->br_startblock
== DELAYSTARTBLOCK
&&
3743 mval
[-1].br_startblock
== DELAYSTARTBLOCK
&&
3744 mval
->br_startoff
==
3745 mval
[-1].br_startoff
+ mval
[-1].br_blockcount
) {
3746 mval
[-1].br_blockcount
+= mval
->br_blockcount
;
3747 mval
[-1].br_state
= mval
->br_state
;
3748 } else if (!((*n
== 0) &&
3749 ((mval
->br_startoff
+ mval
->br_blockcount
) <=
3758 * Map file blocks to filesystem blocks without allocation.
3762 struct xfs_inode
*ip
,
3765 struct xfs_bmbt_irec
*mval
,
3769 struct xfs_mount
*mp
= ip
->i_mount
;
3770 struct xfs_ifork
*ifp
;
3771 struct xfs_bmbt_irec got
;
3774 struct xfs_iext_cursor icur
;
3778 int whichfork
= xfs_bmapi_whichfork(flags
);
3781 ASSERT(!(flags
& ~(XFS_BMAPI_ATTRFORK
|XFS_BMAPI_ENTIRE
|
3782 XFS_BMAPI_IGSTATE
|XFS_BMAPI_COWFORK
)));
3783 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_SHARED
|XFS_ILOCK_EXCL
));
3785 if (unlikely(XFS_TEST_ERROR(
3786 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
3787 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
3788 mp
, XFS_ERRTAG_BMAPIFORMAT
))) {
3789 XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW
, mp
);
3790 return -EFSCORRUPTED
;
3793 if (XFS_FORCED_SHUTDOWN(mp
))
3796 XFS_STATS_INC(mp
, xs_blk_mapr
);
3798 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
3800 /* No CoW fork? Return a hole. */
3801 if (whichfork
== XFS_COW_FORK
&& !ifp
) {
3802 mval
->br_startoff
= bno
;
3803 mval
->br_startblock
= HOLESTARTBLOCK
;
3804 mval
->br_blockcount
= len
;
3805 mval
->br_state
= XFS_EXT_NORM
;
3810 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
3811 error
= xfs_iread_extents(NULL
, ip
, whichfork
);
3816 if (!xfs_iext_lookup_extent(ip
, ifp
, bno
, &icur
, &got
))
3821 while (bno
< end
&& n
< *nmap
) {
3822 /* Reading past eof, act as though there's a hole up to end. */
3824 got
.br_startoff
= end
;
3825 if (got
.br_startoff
> bno
) {
3826 /* Reading in a hole. */
3827 mval
->br_startoff
= bno
;
3828 mval
->br_startblock
= HOLESTARTBLOCK
;
3829 mval
->br_blockcount
=
3830 XFS_FILBLKS_MIN(len
, got
.br_startoff
- bno
);
3831 mval
->br_state
= XFS_EXT_NORM
;
3832 bno
+= mval
->br_blockcount
;
3833 len
-= mval
->br_blockcount
;
3839 /* set up the extent map to return. */
3840 xfs_bmapi_trim_map(mval
, &got
, &bno
, len
, obno
, end
, n
, flags
);
3841 xfs_bmapi_update_map(&mval
, &bno
, &len
, obno
, end
, &n
, flags
);
3843 /* If we're done, stop now. */
3844 if (bno
>= end
|| n
>= *nmap
)
3847 /* Else go on to the next record. */
3848 if (!xfs_iext_next_extent(ifp
, &icur
, &got
))
3856 * Add a delayed allocation extent to an inode. Blocks are reserved from the
3857 * global pool and the extent inserted into the inode in-core extent tree.
3859 * On entry, got refers to the first extent beyond the offset of the extent to
3860 * allocate or eof is specified if no such extent exists. On return, got refers
3861 * to the extent record that was inserted to the inode fork.
3863 * Note that the allocated extent may have been merged with contiguous extents
3864 * during insertion into the inode fork. Thus, got does not reflect the current
3865 * state of the inode fork on return. If necessary, the caller can use lastx to
3866 * look up the updated record in the inode fork.
3869 xfs_bmapi_reserve_delalloc(
3870 struct xfs_inode
*ip
,
3874 xfs_filblks_t prealloc
,
3875 struct xfs_bmbt_irec
*got
,
3876 struct xfs_iext_cursor
*icur
,
3879 struct xfs_mount
*mp
= ip
->i_mount
;
3880 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
3882 xfs_extlen_t indlen
;
3883 char rt
= XFS_IS_REALTIME_INODE(ip
);
3886 xfs_fileoff_t aoff
= off
;
3889 * Cap the alloc length. Keep track of prealloc so we know whether to
3890 * tag the inode before we return.
3892 alen
= XFS_FILBLKS_MIN(len
+ prealloc
, MAXEXTLEN
);
3894 alen
= XFS_FILBLKS_MIN(alen
, got
->br_startoff
- aoff
);
3895 if (prealloc
&& alen
>= len
)
3896 prealloc
= alen
- len
;
3898 /* Figure out the extent size, adjust alen */
3899 if (whichfork
== XFS_COW_FORK
)
3900 extsz
= xfs_get_cowextsz_hint(ip
);
3902 extsz
= xfs_get_extsz_hint(ip
);
3904 struct xfs_bmbt_irec prev
;
3906 if (!xfs_iext_peek_prev_extent(ifp
, icur
, &prev
))
3907 prev
.br_startoff
= NULLFILEOFF
;
3909 error
= xfs_bmap_extsize_align(mp
, got
, &prev
, extsz
, rt
, eof
,
3910 1, 0, &aoff
, &alen
);
3915 extsz
= alen
/ mp
->m_sb
.sb_rextsize
;
3918 * Make a transaction-less quota reservation for delayed allocation
3919 * blocks. This number gets adjusted later. We return if we haven't
3920 * allocated blocks already inside this loop.
3922 error
= xfs_trans_reserve_quota_nblks(NULL
, ip
, (long)alen
, 0,
3923 rt
? XFS_QMOPT_RES_RTBLKS
: XFS_QMOPT_RES_REGBLKS
);
3928 * Split changing sb for alen and indlen since they could be coming
3929 * from different places.
3931 indlen
= (xfs_extlen_t
)xfs_bmap_worst_indlen(ip
, alen
);
3935 error
= xfs_mod_frextents(mp
, -((int64_t)extsz
));
3937 error
= xfs_mod_fdblocks(mp
, -((int64_t)alen
), false);
3941 goto out_unreserve_quota
;
3943 error
= xfs_mod_fdblocks(mp
, -((int64_t)indlen
), false);
3945 goto out_unreserve_blocks
;
3948 ip
->i_delayed_blks
+= alen
;
3950 got
->br_startoff
= aoff
;
3951 got
->br_startblock
= nullstartblock(indlen
);
3952 got
->br_blockcount
= alen
;
3953 got
->br_state
= XFS_EXT_NORM
;
3955 xfs_bmap_add_extent_hole_delay(ip
, whichfork
, icur
, got
);
3958 * Tag the inode if blocks were preallocated. Note that COW fork
3959 * preallocation can occur at the start or end of the extent, even when
3960 * prealloc == 0, so we must also check the aligned offset and length.
3962 if (whichfork
== XFS_DATA_FORK
&& prealloc
)
3963 xfs_inode_set_eofblocks_tag(ip
);
3964 if (whichfork
== XFS_COW_FORK
&& (prealloc
|| aoff
< off
|| alen
> len
))
3965 xfs_inode_set_cowblocks_tag(ip
);
3969 out_unreserve_blocks
:
3971 xfs_mod_frextents(mp
, extsz
);
3973 xfs_mod_fdblocks(mp
, alen
, false);
3974 out_unreserve_quota
:
3975 if (XFS_IS_QUOTA_ON(mp
))
3976 xfs_trans_unreserve_quota_nblks(NULL
, ip
, (long)alen
, 0, rt
?
3977 XFS_QMOPT_RES_RTBLKS
: XFS_QMOPT_RES_REGBLKS
);
3983 struct xfs_bmalloca
*bma
)
3985 struct xfs_mount
*mp
= bma
->ip
->i_mount
;
3986 int whichfork
= xfs_bmapi_whichfork(bma
->flags
);
3987 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
3988 int tmp_logflags
= 0;
3991 ASSERT(bma
->length
> 0);
3994 * For the wasdelay case, we could also just allocate the stuff asked
3995 * for in this bmap call but that wouldn't be as good.
3998 bma
->length
= (xfs_extlen_t
)bma
->got
.br_blockcount
;
3999 bma
->offset
= bma
->got
.br_startoff
;
4000 xfs_iext_peek_prev_extent(ifp
, &bma
->icur
, &bma
->prev
);
4002 bma
->length
= XFS_FILBLKS_MIN(bma
->length
, MAXEXTLEN
);
4004 bma
->length
= XFS_FILBLKS_MIN(bma
->length
,
4005 bma
->got
.br_startoff
- bma
->offset
);
4009 * Set the data type being allocated. For the data fork, the first data
4010 * in the file is treated differently to all other allocations. For the
4011 * attribute fork, we only need to ensure the allocated range is not on
4014 if (!(bma
->flags
& XFS_BMAPI_METADATA
)) {
4015 bma
->datatype
= XFS_ALLOC_NOBUSY
;
4016 if (whichfork
== XFS_DATA_FORK
) {
4017 if (bma
->offset
== 0)
4018 bma
->datatype
|= XFS_ALLOC_INITIAL_USER_DATA
;
4020 bma
->datatype
|= XFS_ALLOC_USERDATA
;
4022 if (bma
->flags
& XFS_BMAPI_ZERO
)
4023 bma
->datatype
|= XFS_ALLOC_USERDATA_ZERO
;
4026 bma
->minlen
= (bma
->flags
& XFS_BMAPI_CONTIG
) ? bma
->length
: 1;
4029 * Only want to do the alignment at the eof if it is userdata and
4030 * allocation length is larger than a stripe unit.
4032 if (mp
->m_dalign
&& bma
->length
>= mp
->m_dalign
&&
4033 !(bma
->flags
& XFS_BMAPI_METADATA
) && whichfork
== XFS_DATA_FORK
) {
4034 error
= xfs_bmap_isaeof(bma
, whichfork
);
4039 error
= xfs_bmap_alloc(bma
);
4044 bma
->cur
->bc_private
.b
.firstblock
= *bma
->firstblock
;
4045 if (bma
->blkno
== NULLFSBLOCK
)
4047 if ((ifp
->if_flags
& XFS_IFBROOT
) && !bma
->cur
) {
4048 bma
->cur
= xfs_bmbt_init_cursor(mp
, bma
->tp
, bma
->ip
, whichfork
);
4049 bma
->cur
->bc_private
.b
.firstblock
= *bma
->firstblock
;
4050 bma
->cur
->bc_private
.b
.dfops
= bma
->dfops
;
4053 * Bump the number of extents we've allocated
4059 bma
->cur
->bc_private
.b
.flags
=
4060 bma
->wasdel
? XFS_BTCUR_BPRV_WASDEL
: 0;
4062 bma
->got
.br_startoff
= bma
->offset
;
4063 bma
->got
.br_startblock
= bma
->blkno
;
4064 bma
->got
.br_blockcount
= bma
->length
;
4065 bma
->got
.br_state
= XFS_EXT_NORM
;
4068 * In the data fork, a wasdelay extent has been initialized, so
4069 * shouldn't be flagged as unwritten.
4071 * For the cow fork, however, we convert delalloc reservations
4072 * (extents allocated for speculative preallocation) to
4073 * allocated unwritten extents, and only convert the unwritten
4074 * extents to real extents when we're about to write the data.
4076 if ((!bma
->wasdel
|| (bma
->flags
& XFS_BMAPI_COWFORK
)) &&
4077 (bma
->flags
& XFS_BMAPI_PREALLOC
) &&
4078 xfs_sb_version_hasextflgbit(&mp
->m_sb
))
4079 bma
->got
.br_state
= XFS_EXT_UNWRITTEN
;
4082 error
= xfs_bmap_add_extent_delay_real(bma
, whichfork
);
4084 error
= xfs_bmap_add_extent_hole_real(bma
->tp
, bma
->ip
,
4085 whichfork
, &bma
->icur
, &bma
->cur
, &bma
->got
,
4086 bma
->firstblock
, bma
->dfops
, &bma
->logflags
);
4088 bma
->logflags
|= tmp_logflags
;
4093 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4094 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4095 * the neighbouring ones.
4097 xfs_iext_get_extent(ifp
, &bma
->icur
, &bma
->got
);
4099 ASSERT(bma
->got
.br_startoff
<= bma
->offset
);
4100 ASSERT(bma
->got
.br_startoff
+ bma
->got
.br_blockcount
>=
4101 bma
->offset
+ bma
->length
);
4102 ASSERT(bma
->got
.br_state
== XFS_EXT_NORM
||
4103 bma
->got
.br_state
== XFS_EXT_UNWRITTEN
);
4108 xfs_bmapi_convert_unwritten(
4109 struct xfs_bmalloca
*bma
,
4110 struct xfs_bmbt_irec
*mval
,
4114 int whichfork
= xfs_bmapi_whichfork(flags
);
4115 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
4116 int tmp_logflags
= 0;
4119 /* check if we need to do unwritten->real conversion */
4120 if (mval
->br_state
== XFS_EXT_UNWRITTEN
&&
4121 (flags
& XFS_BMAPI_PREALLOC
))
4124 /* check if we need to do real->unwritten conversion */
4125 if (mval
->br_state
== XFS_EXT_NORM
&&
4126 (flags
& (XFS_BMAPI_PREALLOC
| XFS_BMAPI_CONVERT
)) !=
4127 (XFS_BMAPI_PREALLOC
| XFS_BMAPI_CONVERT
))
4131 * Modify (by adding) the state flag, if writing.
4133 ASSERT(mval
->br_blockcount
<= len
);
4134 if ((ifp
->if_flags
& XFS_IFBROOT
) && !bma
->cur
) {
4135 bma
->cur
= xfs_bmbt_init_cursor(bma
->ip
->i_mount
, bma
->tp
,
4136 bma
->ip
, whichfork
);
4137 bma
->cur
->bc_private
.b
.firstblock
= *bma
->firstblock
;
4138 bma
->cur
->bc_private
.b
.dfops
= bma
->dfops
;
4140 mval
->br_state
= (mval
->br_state
== XFS_EXT_UNWRITTEN
)
4141 ? XFS_EXT_NORM
: XFS_EXT_UNWRITTEN
;
4144 * Before insertion into the bmbt, zero the range being converted
4147 if (flags
& XFS_BMAPI_ZERO
) {
4148 error
= xfs_zero_extent(bma
->ip
, mval
->br_startblock
,
4149 mval
->br_blockcount
);
4154 error
= xfs_bmap_add_extent_unwritten_real(bma
->tp
, bma
->ip
, whichfork
,
4155 &bma
->icur
, &bma
->cur
, mval
, bma
->firstblock
,
4156 bma
->dfops
, &tmp_logflags
);
4158 * Log the inode core unconditionally in the unwritten extent conversion
4159 * path because the conversion might not have done so (e.g., if the
4160 * extent count hasn't changed). We need to make sure the inode is dirty
4161 * in the transaction for the sake of fsync(), even if nothing has
4162 * changed, because fsync() will not force the log for this transaction
4163 * unless it sees the inode pinned.
4165 * Note: If we're only converting cow fork extents, there aren't
4166 * any on-disk updates to make, so we don't need to log anything.
4168 if (whichfork
!= XFS_COW_FORK
)
4169 bma
->logflags
|= tmp_logflags
| XFS_ILOG_CORE
;
4174 * Update our extent pointer, given that
4175 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4176 * of the neighbouring ones.
4178 xfs_iext_get_extent(ifp
, &bma
->icur
, &bma
->got
);
4181 * We may have combined previously unwritten space with written space,
4182 * so generate another request.
4184 if (mval
->br_blockcount
< len
)
4190 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4191 * extent state if necessary. Details behaviour is controlled by the flags
4192 * parameter. Only allocates blocks from a single allocation group, to avoid
4195 * The returned value in "firstblock" from the first call in a transaction
4196 * must be remembered and presented to subsequent calls in "firstblock".
4197 * An upper bound for the number of blocks to be allocated is supplied to
4198 * the first call in "total"; if no allocation group has that many free
4199 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4203 struct xfs_trans
*tp
, /* transaction pointer */
4204 struct xfs_inode
*ip
, /* incore inode */
4205 xfs_fileoff_t bno
, /* starting file offs. mapped */
4206 xfs_filblks_t len
, /* length to map in file */
4207 int flags
, /* XFS_BMAPI_... */
4208 xfs_fsblock_t
*firstblock
, /* first allocated block
4209 controls a.g. for allocs */
4210 xfs_extlen_t total
, /* total blocks needed */
4211 struct xfs_bmbt_irec
*mval
, /* output: map values */
4212 int *nmap
, /* i/o: mval size/count */
4213 struct xfs_defer_ops
*dfops
) /* i/o: list extents to free */
4215 struct xfs_mount
*mp
= ip
->i_mount
;
4216 struct xfs_ifork
*ifp
;
4217 struct xfs_bmalloca bma
= { NULL
}; /* args for xfs_bmap_alloc */
4218 xfs_fileoff_t end
; /* end of mapped file region */
4219 bool eof
= false; /* after the end of extents */
4220 int error
; /* error return */
4221 int n
; /* current extent index */
4222 xfs_fileoff_t obno
; /* old block number (offset) */
4223 int whichfork
; /* data or attr fork */
4226 xfs_fileoff_t orig_bno
; /* original block number value */
4227 int orig_flags
; /* original flags arg value */
4228 xfs_filblks_t orig_len
; /* original value of len arg */
4229 struct xfs_bmbt_irec
*orig_mval
; /* original value of mval */
4230 int orig_nmap
; /* original value of *nmap */
4238 whichfork
= xfs_bmapi_whichfork(flags
);
4241 ASSERT(*nmap
<= XFS_BMAP_MAX_NMAP
);
4242 ASSERT(!(flags
& XFS_BMAPI_IGSTATE
));
4243 ASSERT(tp
!= NULL
||
4244 (flags
& (XFS_BMAPI_CONVERT
| XFS_BMAPI_COWFORK
)) ==
4245 (XFS_BMAPI_CONVERT
| XFS_BMAPI_COWFORK
));
4247 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_LOCAL
);
4248 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
4249 ASSERT(!(flags
& XFS_BMAPI_REMAP
));
4251 /* zeroing is for currently only for data extents, not metadata */
4252 ASSERT((flags
& (XFS_BMAPI_METADATA
| XFS_BMAPI_ZERO
)) !=
4253 (XFS_BMAPI_METADATA
| XFS_BMAPI_ZERO
));
4255 * we can allocate unwritten extents or pre-zero allocated blocks,
4256 * but it makes no sense to do both at once. This would result in
4257 * zeroing the unwritten extent twice, but it still being an
4258 * unwritten extent....
4260 ASSERT((flags
& (XFS_BMAPI_PREALLOC
| XFS_BMAPI_ZERO
)) !=
4261 (XFS_BMAPI_PREALLOC
| XFS_BMAPI_ZERO
));
4263 if (unlikely(XFS_TEST_ERROR(
4264 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
4265 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
4266 mp
, XFS_ERRTAG_BMAPIFORMAT
))) {
4267 XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW
, mp
);
4268 return -EFSCORRUPTED
;
4271 if (XFS_FORCED_SHUTDOWN(mp
))
4274 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4276 XFS_STATS_INC(mp
, xs_blk_mapw
);
4278 if (*firstblock
== NULLFSBLOCK
) {
4279 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
)
4280 bma
.minleft
= be16_to_cpu(ifp
->if_broot
->bb_level
) + 1;
4287 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
4288 error
= xfs_iread_extents(tp
, ip
, whichfork
);
4297 if (!xfs_iext_lookup_extent(ip
, ifp
, bno
, &bma
.icur
, &bma
.got
))
4299 if (!xfs_iext_peek_prev_extent(ifp
, &bma
.icur
, &bma
.prev
))
4300 bma
.prev
.br_startoff
= NULLFILEOFF
;
4306 bma
.firstblock
= firstblock
;
4308 while (bno
< end
&& n
< *nmap
) {
4309 bool need_alloc
= false, wasdelay
= false;
4311 /* in hole or beyoned EOF? */
4312 if (eof
|| bma
.got
.br_startoff
> bno
) {
4313 if (flags
& XFS_BMAPI_DELALLOC
) {
4315 * For the COW fork we can reasonably get a
4316 * request for converting an extent that races
4317 * with other threads already having converted
4318 * part of it, as there converting COW to
4319 * regular blocks is not protected using the
4322 ASSERT(flags
& XFS_BMAPI_COWFORK
);
4323 if (!(flags
& XFS_BMAPI_COWFORK
)) {
4328 if (eof
|| bno
>= end
)
4333 } else if (isnullstartblock(bma
.got
.br_startblock
)) {
4338 * First, deal with the hole before the allocated space
4339 * that we found, if any.
4341 if ((need_alloc
|| wasdelay
) &&
4342 !(flags
& XFS_BMAPI_CONVERT_ONLY
)) {
4344 bma
.conv
= !!(flags
& XFS_BMAPI_CONVERT
);
4345 bma
.wasdel
= wasdelay
;
4350 * There's a 32/64 bit type mismatch between the
4351 * allocation length request (which can be 64 bits in
4352 * length) and the bma length request, which is
4353 * xfs_extlen_t and therefore 32 bits. Hence we have to
4354 * check for 32-bit overflows and handle them here.
4356 if (len
> (xfs_filblks_t
)MAXEXTLEN
)
4357 bma
.length
= MAXEXTLEN
;
4362 ASSERT(bma
.length
> 0);
4363 error
= xfs_bmapi_allocate(&bma
);
4366 if (bma
.blkno
== NULLFSBLOCK
)
4370 * If this is a CoW allocation, record the data in
4371 * the refcount btree for orphan recovery.
4373 if (whichfork
== XFS_COW_FORK
) {
4374 error
= xfs_refcount_alloc_cow_extent(mp
, dfops
,
4375 bma
.blkno
, bma
.length
);
4381 /* Deal with the allocated space we found. */
4382 xfs_bmapi_trim_map(mval
, &bma
.got
, &bno
, len
, obno
,
4385 /* Execute unwritten extent conversion if necessary */
4386 error
= xfs_bmapi_convert_unwritten(&bma
, mval
, len
, flags
);
4387 if (error
== -EAGAIN
)
4392 /* update the extent map to return */
4393 xfs_bmapi_update_map(&mval
, &bno
, &len
, obno
, end
, &n
, flags
);
4396 * If we're done, stop now. Stop when we've allocated
4397 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4398 * the transaction may get too big.
4400 if (bno
>= end
|| n
>= *nmap
|| bma
.nallocs
>= *nmap
)
4403 /* Else go on to the next record. */
4405 if (!xfs_iext_next_extent(ifp
, &bma
.icur
, &bma
.got
))
4411 * Transform from btree to extents, give it cur.
4413 if (xfs_bmap_wants_extents(ip
, whichfork
)) {
4414 int tmp_logflags
= 0;
4417 error
= xfs_bmap_btree_to_extents(tp
, ip
, bma
.cur
,
4418 &tmp_logflags
, whichfork
);
4419 bma
.logflags
|= tmp_logflags
;
4424 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
||
4425 XFS_IFORK_NEXTENTS(ip
, whichfork
) >
4426 XFS_IFORK_MAXEXT(ip
, whichfork
));
4430 * Log everything. Do this after conversion, there's no point in
4431 * logging the extent records if we've converted to btree format.
4433 if ((bma
.logflags
& xfs_ilog_fext(whichfork
)) &&
4434 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
4435 bma
.logflags
&= ~xfs_ilog_fext(whichfork
);
4436 else if ((bma
.logflags
& xfs_ilog_fbroot(whichfork
)) &&
4437 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)
4438 bma
.logflags
&= ~xfs_ilog_fbroot(whichfork
);
4440 * Log whatever the flags say, even if error. Otherwise we might miss
4441 * detecting a case where the data is changed, there's an error,
4442 * and it's not logged so we don't shutdown when we should.
4445 xfs_trans_log_inode(tp
, ip
, bma
.logflags
);
4449 ASSERT(*firstblock
== NULLFSBLOCK
||
4450 XFS_FSB_TO_AGNO(mp
, *firstblock
) <=
4452 bma
.cur
->bc_private
.b
.firstblock
));
4453 *firstblock
= bma
.cur
->bc_private
.b
.firstblock
;
4455 xfs_btree_del_cursor(bma
.cur
,
4456 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
4459 xfs_bmap_validate_ret(orig_bno
, orig_len
, orig_flags
, orig_mval
,
4466 struct xfs_trans
*tp
,
4467 struct xfs_inode
*ip
,
4470 xfs_fsblock_t startblock
,
4471 struct xfs_defer_ops
*dfops
)
4473 struct xfs_mount
*mp
= ip
->i_mount
;
4474 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, XFS_DATA_FORK
);
4475 struct xfs_btree_cur
*cur
= NULL
;
4476 xfs_fsblock_t firstblock
= NULLFSBLOCK
;
4477 struct xfs_bmbt_irec got
;
4478 struct xfs_iext_cursor icur
;
4479 int logflags
= 0, error
;
4482 ASSERT(len
<= (xfs_filblks_t
)MAXEXTLEN
);
4483 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
4485 if (unlikely(XFS_TEST_ERROR(
4486 (XFS_IFORK_FORMAT(ip
, XFS_DATA_FORK
) != XFS_DINODE_FMT_EXTENTS
&&
4487 XFS_IFORK_FORMAT(ip
, XFS_DATA_FORK
) != XFS_DINODE_FMT_BTREE
),
4488 mp
, XFS_ERRTAG_BMAPIFORMAT
))) {
4489 XFS_ERROR_REPORT("xfs_bmapi_remap", XFS_ERRLEVEL_LOW
, mp
);
4490 return -EFSCORRUPTED
;
4493 if (XFS_FORCED_SHUTDOWN(mp
))
4496 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
4497 error
= xfs_iread_extents(NULL
, ip
, XFS_DATA_FORK
);
4502 if (xfs_iext_lookup_extent(ip
, ifp
, bno
, &icur
, &got
)) {
4503 /* make sure we only reflink into a hole. */
4504 ASSERT(got
.br_startoff
> bno
);
4505 ASSERT(got
.br_startoff
- bno
>= len
);
4508 ip
->i_d
.di_nblocks
+= len
;
4509 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
4511 if (ifp
->if_flags
& XFS_IFBROOT
) {
4512 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, XFS_DATA_FORK
);
4513 cur
->bc_private
.b
.firstblock
= firstblock
;
4514 cur
->bc_private
.b
.dfops
= dfops
;
4515 cur
->bc_private
.b
.flags
= 0;
4518 got
.br_startoff
= bno
;
4519 got
.br_startblock
= startblock
;
4520 got
.br_blockcount
= len
;
4521 got
.br_state
= XFS_EXT_NORM
;
4523 error
= xfs_bmap_add_extent_hole_real(tp
, ip
, XFS_DATA_FORK
, &icur
,
4524 &cur
, &got
, &firstblock
, dfops
, &logflags
);
4528 if (xfs_bmap_wants_extents(ip
, XFS_DATA_FORK
)) {
4529 int tmp_logflags
= 0;
4531 error
= xfs_bmap_btree_to_extents(tp
, ip
, cur
,
4532 &tmp_logflags
, XFS_DATA_FORK
);
4533 logflags
|= tmp_logflags
;
4537 if (ip
->i_d
.di_format
!= XFS_DINODE_FMT_EXTENTS
)
4538 logflags
&= ~XFS_ILOG_DEXT
;
4539 else if (ip
->i_d
.di_format
!= XFS_DINODE_FMT_BTREE
)
4540 logflags
&= ~XFS_ILOG_DBROOT
;
4543 xfs_trans_log_inode(tp
, ip
, logflags
);
4545 xfs_btree_del_cursor(cur
,
4546 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
4552 * When a delalloc extent is split (e.g., due to a hole punch), the original
4553 * indlen reservation must be shared across the two new extents that are left
4556 * Given the original reservation and the worst case indlen for the two new
4557 * extents (as calculated by xfs_bmap_worst_indlen()), split the original
4558 * reservation fairly across the two new extents. If necessary, steal available
4559 * blocks from a deleted extent to make up a reservation deficiency (e.g., if
4560 * ores == 1). The number of stolen blocks is returned. The availability and
4561 * subsequent accounting of stolen blocks is the responsibility of the caller.
4563 static xfs_filblks_t
4564 xfs_bmap_split_indlen(
4565 xfs_filblks_t ores
, /* original res. */
4566 xfs_filblks_t
*indlen1
, /* ext1 worst indlen */
4567 xfs_filblks_t
*indlen2
, /* ext2 worst indlen */
4568 xfs_filblks_t avail
) /* stealable blocks */
4570 xfs_filblks_t len1
= *indlen1
;
4571 xfs_filblks_t len2
= *indlen2
;
4572 xfs_filblks_t nres
= len1
+ len2
; /* new total res. */
4573 xfs_filblks_t stolen
= 0;
4574 xfs_filblks_t resfactor
;
4577 * Steal as many blocks as we can to try and satisfy the worst case
4578 * indlen for both new extents.
4580 if (ores
< nres
&& avail
)
4581 stolen
= XFS_FILBLKS_MIN(nres
- ores
, avail
);
4584 /* nothing else to do if we've satisfied the new reservation */
4589 * We can't meet the total required reservation for the two extents.
4590 * Calculate the percent of the overall shortage between both extents
4591 * and apply this percentage to each of the requested indlen values.
4592 * This distributes the shortage fairly and reduces the chances that one
4593 * of the two extents is left with nothing when extents are repeatedly
4596 resfactor
= (ores
* 100);
4597 do_div(resfactor
, nres
);
4602 ASSERT(len1
+ len2
<= ores
);
4603 ASSERT(len1
< *indlen1
&& len2
< *indlen2
);
4606 * Hand out the remainder to each extent. If one of the two reservations
4607 * is zero, we want to make sure that one gets a block first. The loop
4608 * below starts with len1, so hand len2 a block right off the bat if it
4611 ores
-= (len1
+ len2
);
4612 ASSERT((*indlen1
- len1
) + (*indlen2
- len2
) >= ores
);
4613 if (ores
&& !len2
&& *indlen2
) {
4618 if (len1
< *indlen1
) {
4624 if (len2
< *indlen2
) {
4637 xfs_bmap_del_extent_delay(
4638 struct xfs_inode
*ip
,
4640 struct xfs_iext_cursor
*icur
,
4641 struct xfs_bmbt_irec
*got
,
4642 struct xfs_bmbt_irec
*del
)
4644 struct xfs_mount
*mp
= ip
->i_mount
;
4645 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4646 struct xfs_bmbt_irec
new;
4647 int64_t da_old
, da_new
, da_diff
= 0;
4648 xfs_fileoff_t del_endoff
, got_endoff
;
4649 xfs_filblks_t got_indlen
, new_indlen
, stolen
;
4650 int state
= xfs_bmap_fork_to_state(whichfork
);
4654 XFS_STATS_INC(mp
, xs_del_exlist
);
4656 isrt
= (whichfork
== XFS_DATA_FORK
) && XFS_IS_REALTIME_INODE(ip
);
4657 del_endoff
= del
->br_startoff
+ del
->br_blockcount
;
4658 got_endoff
= got
->br_startoff
+ got
->br_blockcount
;
4659 da_old
= startblockval(got
->br_startblock
);
4662 ASSERT(del
->br_blockcount
> 0);
4663 ASSERT(got
->br_startoff
<= del
->br_startoff
);
4664 ASSERT(got_endoff
>= del_endoff
);
4667 uint64_t rtexts
= XFS_FSB_TO_B(mp
, del
->br_blockcount
);
4669 do_div(rtexts
, mp
->m_sb
.sb_rextsize
);
4670 xfs_mod_frextents(mp
, rtexts
);
4674 * Update the inode delalloc counter now and wait to update the
4675 * sb counters as we might have to borrow some blocks for the
4676 * indirect block accounting.
4678 error
= xfs_trans_reserve_quota_nblks(NULL
, ip
,
4679 -((long)del
->br_blockcount
), 0,
4680 isrt
? XFS_QMOPT_RES_RTBLKS
: XFS_QMOPT_RES_REGBLKS
);
4683 ip
->i_delayed_blks
-= del
->br_blockcount
;
4685 if (got
->br_startoff
== del
->br_startoff
)
4686 state
|= BMAP_LEFT_FILLING
;
4687 if (got_endoff
== del_endoff
)
4688 state
|= BMAP_RIGHT_FILLING
;
4690 switch (state
& (BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
)) {
4691 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
4693 * Matches the whole extent. Delete the entry.
4695 xfs_iext_remove(ip
, icur
, state
);
4696 xfs_iext_prev(ifp
, icur
);
4698 case BMAP_LEFT_FILLING
:
4700 * Deleting the first part of the extent.
4702 got
->br_startoff
= del_endoff
;
4703 got
->br_blockcount
-= del
->br_blockcount
;
4704 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
,
4705 got
->br_blockcount
), da_old
);
4706 got
->br_startblock
= nullstartblock((int)da_new
);
4707 xfs_iext_update_extent(ip
, state
, icur
, got
);
4709 case BMAP_RIGHT_FILLING
:
4711 * Deleting the last part of the extent.
4713 got
->br_blockcount
= got
->br_blockcount
- del
->br_blockcount
;
4714 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
,
4715 got
->br_blockcount
), da_old
);
4716 got
->br_startblock
= nullstartblock((int)da_new
);
4717 xfs_iext_update_extent(ip
, state
, icur
, got
);
4721 * Deleting the middle of the extent.
4723 * Distribute the original indlen reservation across the two new
4724 * extents. Steal blocks from the deleted extent if necessary.
4725 * Stealing blocks simply fudges the fdblocks accounting below.
4726 * Warn if either of the new indlen reservations is zero as this
4727 * can lead to delalloc problems.
4729 got
->br_blockcount
= del
->br_startoff
- got
->br_startoff
;
4730 got_indlen
= xfs_bmap_worst_indlen(ip
, got
->br_blockcount
);
4732 new.br_blockcount
= got_endoff
- del_endoff
;
4733 new_indlen
= xfs_bmap_worst_indlen(ip
, new.br_blockcount
);
4735 WARN_ON_ONCE(!got_indlen
|| !new_indlen
);
4736 stolen
= xfs_bmap_split_indlen(da_old
, &got_indlen
, &new_indlen
,
4737 del
->br_blockcount
);
4739 got
->br_startblock
= nullstartblock((int)got_indlen
);
4741 new.br_startoff
= del_endoff
;
4742 new.br_state
= got
->br_state
;
4743 new.br_startblock
= nullstartblock((int)new_indlen
);
4745 xfs_iext_update_extent(ip
, state
, icur
, got
);
4746 xfs_iext_next(ifp
, icur
);
4747 xfs_iext_insert(ip
, icur
, &new, state
);
4749 da_new
= got_indlen
+ new_indlen
- stolen
;
4750 del
->br_blockcount
-= stolen
;
4754 ASSERT(da_old
>= da_new
);
4755 da_diff
= da_old
- da_new
;
4757 da_diff
+= del
->br_blockcount
;
4759 xfs_mod_fdblocks(mp
, da_diff
, false);
4764 xfs_bmap_del_extent_cow(
4765 struct xfs_inode
*ip
,
4766 struct xfs_iext_cursor
*icur
,
4767 struct xfs_bmbt_irec
*got
,
4768 struct xfs_bmbt_irec
*del
)
4770 struct xfs_mount
*mp
= ip
->i_mount
;
4771 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, XFS_COW_FORK
);
4772 struct xfs_bmbt_irec
new;
4773 xfs_fileoff_t del_endoff
, got_endoff
;
4774 int state
= BMAP_COWFORK
;
4776 XFS_STATS_INC(mp
, xs_del_exlist
);
4778 del_endoff
= del
->br_startoff
+ del
->br_blockcount
;
4779 got_endoff
= got
->br_startoff
+ got
->br_blockcount
;
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
, icur
, state
);
4797 xfs_iext_prev(ifp
, icur
);
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
, icur
, 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
, icur
, got
);
4817 * Deleting the middle of the extent.
4819 got
->br_blockcount
= del
->br_startoff
- got
->br_startoff
;
4821 new.br_startoff
= del_endoff
;
4822 new.br_blockcount
= got_endoff
- del_endoff
;
4823 new.br_state
= got
->br_state
;
4824 new.br_startblock
= del
->br_startblock
+ del
->br_blockcount
;
4826 xfs_iext_update_extent(ip
, state
, icur
, got
);
4827 xfs_iext_next(ifp
, icur
);
4828 xfs_iext_insert(ip
, icur
, &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 struct xfs_iext_cursor
*icur
,
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(del
->br_blockcount
> 0);
4871 xfs_iext_get_extent(ifp
, icur
, &got
);
4872 ASSERT(got
.br_startoff
<= del
->br_startoff
);
4873 del_endoff
= del
->br_startoff
+ del
->br_blockcount
;
4874 got_endoff
= got
.br_startoff
+ got
.br_blockcount
;
4875 ASSERT(got_endoff
>= del_endoff
);
4876 ASSERT(!isnullstartblock(got
.br_startblock
));
4881 * If it's the case where the directory code is running with no block
4882 * reservation, and the deleted block is in the middle of its extent,
4883 * and the resulting insert of an extent would cause transformation to
4884 * btree format, then reject it. The calling code will then swap blocks
4885 * around instead. We have to do this now, rather than waiting for the
4886 * conversion to btree format, since the transaction will be dirty then.
4888 if (tp
->t_blk_res
== 0 &&
4889 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
&&
4890 XFS_IFORK_NEXTENTS(ip
, whichfork
) >=
4891 XFS_IFORK_MAXEXT(ip
, whichfork
) &&
4892 del
->br_startoff
> got
.br_startoff
&& del_endoff
< got_endoff
)
4895 flags
= XFS_ILOG_CORE
;
4896 if (whichfork
== XFS_DATA_FORK
&& XFS_IS_REALTIME_INODE(ip
)) {
4900 ASSERT(do_mod(del
->br_blockcount
, mp
->m_sb
.sb_rextsize
) == 0);
4901 ASSERT(do_mod(del
->br_startblock
, mp
->m_sb
.sb_rextsize
) == 0);
4902 bno
= del
->br_startblock
;
4903 len
= del
->br_blockcount
;
4904 do_div(bno
, mp
->m_sb
.sb_rextsize
);
4905 do_div(len
, mp
->m_sb
.sb_rextsize
);
4906 error
= xfs_rtfree_extent(tp
, bno
, (xfs_extlen_t
)len
);
4910 nblks
= len
* mp
->m_sb
.sb_rextsize
;
4911 qfield
= XFS_TRANS_DQ_RTBCOUNT
;
4914 nblks
= del
->br_blockcount
;
4915 qfield
= XFS_TRANS_DQ_BCOUNT
;
4918 del_endblock
= del
->br_startblock
+ del
->br_blockcount
;
4920 error
= xfs_bmbt_lookup_eq(cur
, &got
, &i
);
4923 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
4926 if (got
.br_startoff
== del
->br_startoff
)
4927 state
|= BMAP_LEFT_FILLING
;
4928 if (got_endoff
== del_endoff
)
4929 state
|= BMAP_RIGHT_FILLING
;
4931 switch (state
& (BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
)) {
4932 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
4934 * Matches the whole extent. Delete the entry.
4936 xfs_iext_remove(ip
, icur
, state
);
4937 xfs_iext_prev(ifp
, icur
);
4938 XFS_IFORK_NEXT_SET(ip
, whichfork
,
4939 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
4940 flags
|= XFS_ILOG_CORE
;
4942 flags
|= xfs_ilog_fext(whichfork
);
4945 if ((error
= xfs_btree_delete(cur
, &i
)))
4947 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
4949 case BMAP_LEFT_FILLING
:
4951 * Deleting the first part of the extent.
4953 got
.br_startoff
= del_endoff
;
4954 got
.br_startblock
= del_endblock
;
4955 got
.br_blockcount
-= del
->br_blockcount
;
4956 xfs_iext_update_extent(ip
, state
, icur
, &got
);
4958 flags
|= xfs_ilog_fext(whichfork
);
4961 error
= xfs_bmbt_update(cur
, &got
);
4965 case BMAP_RIGHT_FILLING
:
4967 * Deleting the last part of the extent.
4969 got
.br_blockcount
-= del
->br_blockcount
;
4970 xfs_iext_update_extent(ip
, state
, icur
, &got
);
4972 flags
|= xfs_ilog_fext(whichfork
);
4975 error
= xfs_bmbt_update(cur
, &got
);
4981 * Deleting the middle of the extent.
4985 got
.br_blockcount
= del
->br_startoff
- got
.br_startoff
;
4986 xfs_iext_update_extent(ip
, state
, icur
, &got
);
4988 new.br_startoff
= del_endoff
;
4989 new.br_blockcount
= got_endoff
- del_endoff
;
4990 new.br_state
= got
.br_state
;
4991 new.br_startblock
= del_endblock
;
4993 flags
|= XFS_ILOG_CORE
;
4995 error
= xfs_bmbt_update(cur
, &got
);
4998 error
= xfs_btree_increment(cur
, 0, &i
);
5001 cur
->bc_rec
.b
= new;
5002 error
= xfs_btree_insert(cur
, &i
);
5003 if (error
&& error
!= -ENOSPC
)
5006 * If get no-space back from btree insert, it tried a
5007 * split, and we have a zero block reservation. Fix up
5008 * our state and return the error.
5010 if (error
== -ENOSPC
) {
5012 * Reset the cursor, don't trust it after any
5015 error
= xfs_bmbt_lookup_eq(cur
, &got
, &i
);
5018 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
5020 * Update the btree record back
5021 * to the original value.
5023 error
= xfs_bmbt_update(cur
, &old
);
5027 * Reset the extent record back
5028 * to the original value.
5030 xfs_iext_update_extent(ip
, state
, icur
, &old
);
5035 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
5037 flags
|= xfs_ilog_fext(whichfork
);
5038 XFS_IFORK_NEXT_SET(ip
, whichfork
,
5039 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
5040 xfs_iext_next(ifp
, icur
);
5041 xfs_iext_insert(ip
, icur
, &new, state
);
5045 /* remove reverse mapping */
5046 error
= xfs_rmap_unmap_extent(mp
, dfops
, ip
, whichfork
, del
);
5051 * If we need to, add to list of extents to delete.
5053 if (do_fx
&& !(bflags
& XFS_BMAPI_REMAP
)) {
5054 if (xfs_is_reflink_inode(ip
) && whichfork
== XFS_DATA_FORK
) {
5055 error
= xfs_refcount_decrease_extent(mp
, dfops
, del
);
5059 xfs_bmap_add_free(mp
, dfops
, del
->br_startblock
,
5060 del
->br_blockcount
, NULL
);
5064 * Adjust inode # blocks in the file.
5067 ip
->i_d
.di_nblocks
-= nblks
;
5069 * Adjust quota data.
5071 if (qfield
&& !(bflags
& XFS_BMAPI_REMAP
))
5072 xfs_trans_mod_dquot_byino(tp
, ip
, qfield
, (long)-nblks
);
5080 * Unmap (remove) blocks from a file.
5081 * If nexts is nonzero then the number of extents to remove is limited to
5082 * that value. If not all extents in the block range can be removed then
5087 xfs_trans_t
*tp
, /* transaction pointer */
5088 struct xfs_inode
*ip
, /* incore inode */
5089 xfs_fileoff_t start
, /* first file offset deleted */
5090 xfs_filblks_t
*rlen
, /* i/o: amount remaining */
5091 int flags
, /* misc flags */
5092 xfs_extnum_t nexts
, /* number of extents max */
5093 xfs_fsblock_t
*firstblock
, /* first allocated block
5094 controls a.g. for allocs */
5095 struct xfs_defer_ops
*dfops
) /* i/o: deferred updates */
5097 xfs_btree_cur_t
*cur
; /* bmap btree cursor */
5098 xfs_bmbt_irec_t del
; /* extent being deleted */
5099 int error
; /* error return value */
5100 xfs_extnum_t extno
; /* extent number in list */
5101 xfs_bmbt_irec_t got
; /* current extent record */
5102 xfs_ifork_t
*ifp
; /* inode fork pointer */
5103 int isrt
; /* freeing in rt area */
5104 int logflags
; /* transaction logging flags */
5105 xfs_extlen_t mod
; /* rt extent offset */
5106 xfs_mount_t
*mp
; /* mount structure */
5107 int tmp_logflags
; /* partial logging flags */
5108 int wasdel
; /* was a delayed alloc extent */
5109 int whichfork
; /* data or attribute fork */
5111 xfs_filblks_t len
= *rlen
; /* length to unmap in file */
5112 xfs_fileoff_t max_len
;
5113 xfs_agnumber_t prev_agno
= NULLAGNUMBER
, agno
;
5115 struct xfs_iext_cursor icur
;
5118 trace_xfs_bunmap(ip
, start
, len
, flags
, _RET_IP_
);
5120 whichfork
= xfs_bmapi_whichfork(flags
);
5121 ASSERT(whichfork
!= XFS_COW_FORK
);
5122 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5124 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
5125 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)) {
5126 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW
,
5128 return -EFSCORRUPTED
;
5131 if (XFS_FORCED_SHUTDOWN(mp
))
5134 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
5139 * Guesstimate how many blocks we can unmap without running the risk of
5140 * blowing out the transaction with a mix of EFIs and reflink
5143 if (tp
&& xfs_is_reflink_inode(ip
) && whichfork
== XFS_DATA_FORK
)
5144 max_len
= min(len
, xfs_refcount_max_unmap(tp
->t_log_res
));
5148 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
5149 (error
= xfs_iread_extents(tp
, ip
, whichfork
)))
5151 if (xfs_iext_count(ifp
) == 0) {
5155 XFS_STATS_INC(mp
, xs_blk_unmap
);
5156 isrt
= (whichfork
== XFS_DATA_FORK
) && XFS_IS_REALTIME_INODE(ip
);
5159 if (!xfs_iext_lookup_extent_before(ip
, ifp
, &end
, &icur
, &got
)) {
5166 if (ifp
->if_flags
& XFS_IFBROOT
) {
5167 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
);
5168 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
5169 cur
->bc_private
.b
.firstblock
= *firstblock
;
5170 cur
->bc_private
.b
.dfops
= dfops
;
5171 cur
->bc_private
.b
.flags
= 0;
5177 * Synchronize by locking the bitmap inode.
5179 xfs_ilock(mp
->m_rbmip
, XFS_ILOCK_EXCL
|XFS_ILOCK_RTBITMAP
);
5180 xfs_trans_ijoin(tp
, mp
->m_rbmip
, XFS_ILOCK_EXCL
);
5181 xfs_ilock(mp
->m_rsumip
, XFS_ILOCK_EXCL
|XFS_ILOCK_RTSUM
);
5182 xfs_trans_ijoin(tp
, mp
->m_rsumip
, XFS_ILOCK_EXCL
);
5186 while (end
!= (xfs_fileoff_t
)-1 && end
>= start
&&
5187 (nexts
== 0 || extno
< nexts
) && max_len
> 0) {
5189 * Is the found extent after a hole in which end lives?
5190 * Just back up to the previous extent, if so.
5192 if (got
.br_startoff
> end
&&
5193 !xfs_iext_prev_extent(ifp
, &icur
, &got
)) {
5198 * Is the last block of this extent before the range
5199 * we're supposed to delete? If so, we're done.
5201 end
= XFS_FILEOFF_MIN(end
,
5202 got
.br_startoff
+ got
.br_blockcount
- 1);
5206 * Then deal with the (possibly delayed) allocated space
5210 wasdel
= isnullstartblock(del
.br_startblock
);
5213 * Make sure we don't touch multiple AGF headers out of order
5214 * in a single transaction, as that could cause AB-BA deadlocks.
5217 agno
= XFS_FSB_TO_AGNO(mp
, del
.br_startblock
);
5218 if (prev_agno
!= NULLAGNUMBER
&& prev_agno
> agno
)
5222 if (got
.br_startoff
< start
) {
5223 del
.br_startoff
= start
;
5224 del
.br_blockcount
-= start
- got
.br_startoff
;
5226 del
.br_startblock
+= start
- got
.br_startoff
;
5228 if (del
.br_startoff
+ del
.br_blockcount
> end
+ 1)
5229 del
.br_blockcount
= end
+ 1 - del
.br_startoff
;
5231 /* How much can we safely unmap? */
5232 if (max_len
< del
.br_blockcount
) {
5233 del
.br_startoff
+= del
.br_blockcount
- max_len
;
5235 del
.br_startblock
+= del
.br_blockcount
- max_len
;
5236 del
.br_blockcount
= max_len
;
5239 sum
= del
.br_startblock
+ del
.br_blockcount
;
5241 (mod
= do_mod(sum
, mp
->m_sb
.sb_rextsize
))) {
5243 * Realtime extent not lined up at the end.
5244 * The extent could have been split into written
5245 * and unwritten pieces, or we could just be
5246 * unmapping part of it. But we can't really
5247 * get rid of part of a realtime extent.
5249 if (del
.br_state
== XFS_EXT_UNWRITTEN
||
5250 !xfs_sb_version_hasextflgbit(&mp
->m_sb
)) {
5252 * This piece is unwritten, or we're not
5253 * using unwritten extents. Skip over it.
5256 end
-= mod
> del
.br_blockcount
?
5257 del
.br_blockcount
: mod
;
5258 if (end
< got
.br_startoff
&&
5259 !xfs_iext_prev_extent(ifp
, &icur
, &got
)) {
5266 * It's written, turn it unwritten.
5267 * This is better than zeroing it.
5269 ASSERT(del
.br_state
== XFS_EXT_NORM
);
5270 ASSERT(tp
->t_blk_res
> 0);
5272 * If this spans a realtime extent boundary,
5273 * chop it back to the start of the one we end at.
5275 if (del
.br_blockcount
> mod
) {
5276 del
.br_startoff
+= del
.br_blockcount
- mod
;
5277 del
.br_startblock
+= del
.br_blockcount
- mod
;
5278 del
.br_blockcount
= mod
;
5280 del
.br_state
= XFS_EXT_UNWRITTEN
;
5281 error
= xfs_bmap_add_extent_unwritten_real(tp
, ip
,
5282 whichfork
, &icur
, &cur
, &del
,
5283 firstblock
, dfops
, &logflags
);
5288 if (isrt
&& (mod
= do_mod(del
.br_startblock
, mp
->m_sb
.sb_rextsize
))) {
5290 * Realtime extent is lined up at the end but not
5291 * at the front. We'll get rid of full extents if
5294 mod
= mp
->m_sb
.sb_rextsize
- mod
;
5295 if (del
.br_blockcount
> mod
) {
5296 del
.br_blockcount
-= mod
;
5297 del
.br_startoff
+= mod
;
5298 del
.br_startblock
+= mod
;
5299 } else if ((del
.br_startoff
== start
&&
5300 (del
.br_state
== XFS_EXT_UNWRITTEN
||
5301 tp
->t_blk_res
== 0)) ||
5302 !xfs_sb_version_hasextflgbit(&mp
->m_sb
)) {
5304 * Can't make it unwritten. There isn't
5305 * a full extent here so just skip it.
5307 ASSERT(end
>= del
.br_blockcount
);
5308 end
-= del
.br_blockcount
;
5309 if (got
.br_startoff
> end
&&
5310 !xfs_iext_prev_extent(ifp
, &icur
, &got
)) {
5315 } else if (del
.br_state
== XFS_EXT_UNWRITTEN
) {
5316 struct xfs_bmbt_irec prev
;
5319 * This one is already unwritten.
5320 * It must have a written left neighbor.
5321 * Unwrite the killed part of that one and
5324 if (!xfs_iext_prev_extent(ifp
, &icur
, &prev
))
5326 ASSERT(prev
.br_state
== XFS_EXT_NORM
);
5327 ASSERT(!isnullstartblock(prev
.br_startblock
));
5328 ASSERT(del
.br_startblock
==
5329 prev
.br_startblock
+ prev
.br_blockcount
);
5330 if (prev
.br_startoff
< start
) {
5331 mod
= start
- prev
.br_startoff
;
5332 prev
.br_blockcount
-= mod
;
5333 prev
.br_startblock
+= mod
;
5334 prev
.br_startoff
= start
;
5336 prev
.br_state
= XFS_EXT_UNWRITTEN
;
5337 error
= xfs_bmap_add_extent_unwritten_real(tp
,
5338 ip
, whichfork
, &icur
, &cur
,
5339 &prev
, firstblock
, dfops
,
5345 ASSERT(del
.br_state
== XFS_EXT_NORM
);
5346 del
.br_state
= XFS_EXT_UNWRITTEN
;
5347 error
= xfs_bmap_add_extent_unwritten_real(tp
,
5348 ip
, whichfork
, &icur
, &cur
,
5349 &del
, firstblock
, dfops
,
5358 error
= xfs_bmap_del_extent_delay(ip
, whichfork
, &icur
,
5361 error
= xfs_bmap_del_extent_real(ip
, tp
, &icur
, dfops
,
5362 cur
, &del
, &tmp_logflags
, whichfork
,
5364 logflags
|= tmp_logflags
;
5370 max_len
-= del
.br_blockcount
;
5371 end
= del
.br_startoff
- 1;
5374 * If not done go on to the next (previous) record.
5376 if (end
!= (xfs_fileoff_t
)-1 && end
>= start
) {
5377 if (!xfs_iext_get_extent(ifp
, &icur
, &got
) ||
5378 (got
.br_startoff
> end
&&
5379 !xfs_iext_prev_extent(ifp
, &icur
, &got
))) {
5386 if (done
|| end
== (xfs_fileoff_t
)-1 || end
< start
)
5389 *rlen
= end
- start
+ 1;
5392 * Convert to a btree if necessary.
5394 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
5395 ASSERT(cur
== NULL
);
5396 error
= xfs_bmap_extents_to_btree(tp
, ip
, firstblock
, dfops
,
5397 &cur
, 0, &tmp_logflags
, whichfork
);
5398 logflags
|= tmp_logflags
;
5403 * transform from btree to extents, give it cur
5405 else if (xfs_bmap_wants_extents(ip
, whichfork
)) {
5406 ASSERT(cur
!= NULL
);
5407 error
= xfs_bmap_btree_to_extents(tp
, ip
, cur
, &tmp_logflags
,
5409 logflags
|= tmp_logflags
;
5414 * transform from extents to local?
5419 * Log everything. Do this after conversion, there's no point in
5420 * logging the extent records if we've converted to btree format.
5422 if ((logflags
& xfs_ilog_fext(whichfork
)) &&
5423 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
5424 logflags
&= ~xfs_ilog_fext(whichfork
);
5425 else if ((logflags
& xfs_ilog_fbroot(whichfork
)) &&
5426 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)
5427 logflags
&= ~xfs_ilog_fbroot(whichfork
);
5429 * Log inode even in the error case, if the transaction
5430 * is dirty we'll need to shut down the filesystem.
5433 xfs_trans_log_inode(tp
, ip
, logflags
);
5436 *firstblock
= cur
->bc_private
.b
.firstblock
;
5437 cur
->bc_private
.b
.allocated
= 0;
5439 xfs_btree_del_cursor(cur
,
5440 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
5445 /* Unmap a range of a file. */
5449 struct xfs_inode
*ip
,
5454 xfs_fsblock_t
*firstblock
,
5455 struct xfs_defer_ops
*dfops
,
5460 error
= __xfs_bunmapi(tp
, ip
, bno
, &len
, flags
, nexts
, firstblock
,
5467 * Determine whether an extent shift can be accomplished by a merge with the
5468 * extent that precedes the target hole of the shift.
5472 struct xfs_bmbt_irec
*left
, /* preceding extent */
5473 struct xfs_bmbt_irec
*got
, /* current extent to shift */
5474 xfs_fileoff_t shift
) /* shift fsb */
5476 xfs_fileoff_t startoff
;
5478 startoff
= got
->br_startoff
- shift
;
5481 * The extent, once shifted, must be adjacent in-file and on-disk with
5482 * the preceding extent.
5484 if ((left
->br_startoff
+ left
->br_blockcount
!= startoff
) ||
5485 (left
->br_startblock
+ left
->br_blockcount
!= got
->br_startblock
) ||
5486 (left
->br_state
!= got
->br_state
) ||
5487 (left
->br_blockcount
+ got
->br_blockcount
> MAXEXTLEN
))
5494 * A bmap extent shift adjusts the file offset of an extent to fill a preceding
5495 * hole in the file. If an extent shift would result in the extent being fully
5496 * adjacent to the extent that currently precedes the hole, we can merge with
5497 * the preceding extent rather than do the shift.
5499 * This function assumes the caller has verified a shift-by-merge is possible
5500 * with the provided extents via xfs_bmse_can_merge().
5504 struct xfs_inode
*ip
,
5506 xfs_fileoff_t shift
, /* shift fsb */
5507 struct xfs_iext_cursor
*icur
,
5508 struct xfs_bmbt_irec
*got
, /* extent to shift */
5509 struct xfs_bmbt_irec
*left
, /* preceding extent */
5510 struct xfs_btree_cur
*cur
,
5511 int *logflags
, /* output */
5512 struct xfs_defer_ops
*dfops
)
5514 struct xfs_bmbt_irec
new;
5515 xfs_filblks_t blockcount
;
5517 struct xfs_mount
*mp
= ip
->i_mount
;
5519 blockcount
= left
->br_blockcount
+ got
->br_blockcount
;
5521 ASSERT(xfs_isilocked(ip
, XFS_IOLOCK_EXCL
));
5522 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
5523 ASSERT(xfs_bmse_can_merge(left
, got
, shift
));
5526 new.br_blockcount
= blockcount
;
5529 * Update the on-disk extent count, the btree if necessary and log the
5532 XFS_IFORK_NEXT_SET(ip
, whichfork
,
5533 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
5534 *logflags
|= XFS_ILOG_CORE
;
5536 *logflags
|= XFS_ILOG_DEXT
;
5540 /* lookup and remove the extent to merge */
5541 error
= xfs_bmbt_lookup_eq(cur
, got
, &i
);
5544 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
5546 error
= xfs_btree_delete(cur
, &i
);
5549 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
5551 /* lookup and update size of the previous extent */
5552 error
= xfs_bmbt_lookup_eq(cur
, left
, &i
);
5555 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
5557 error
= xfs_bmbt_update(cur
, &new);
5562 xfs_iext_remove(ip
, icur
, 0);
5563 xfs_iext_prev(XFS_IFORK_PTR(ip
, whichfork
), icur
);
5564 xfs_iext_update_extent(ip
, xfs_bmap_fork_to_state(whichfork
), icur
,
5567 /* update reverse mapping. rmap functions merge the rmaps for us */
5568 error
= xfs_rmap_unmap_extent(mp
, dfops
, ip
, whichfork
, got
);
5571 memcpy(&new, got
, sizeof(new));
5572 new.br_startoff
= left
->br_startoff
+ left
->br_blockcount
;
5573 return xfs_rmap_map_extent(mp
, dfops
, ip
, whichfork
, &new);
5577 xfs_bmap_shift_update_extent(
5578 struct xfs_inode
*ip
,
5580 struct xfs_iext_cursor
*icur
,
5581 struct xfs_bmbt_irec
*got
,
5582 struct xfs_btree_cur
*cur
,
5584 struct xfs_defer_ops
*dfops
,
5585 xfs_fileoff_t startoff
)
5587 struct xfs_mount
*mp
= ip
->i_mount
;
5588 struct xfs_bmbt_irec prev
= *got
;
5591 *logflags
|= XFS_ILOG_CORE
;
5593 got
->br_startoff
= startoff
;
5596 error
= xfs_bmbt_lookup_eq(cur
, &prev
, &i
);
5599 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
5601 error
= xfs_bmbt_update(cur
, got
);
5605 *logflags
|= XFS_ILOG_DEXT
;
5608 xfs_iext_update_extent(ip
, xfs_bmap_fork_to_state(whichfork
), icur
,
5611 /* update reverse mapping */
5612 error
= xfs_rmap_unmap_extent(mp
, dfops
, ip
, whichfork
, &prev
);
5615 return xfs_rmap_map_extent(mp
, dfops
, ip
, whichfork
, got
);
5619 xfs_bmap_collapse_extents(
5620 struct xfs_trans
*tp
,
5621 struct xfs_inode
*ip
,
5622 xfs_fileoff_t
*next_fsb
,
5623 xfs_fileoff_t offset_shift_fsb
,
5625 xfs_fileoff_t stop_fsb
,
5626 xfs_fsblock_t
*firstblock
,
5627 struct xfs_defer_ops
*dfops
)
5629 int whichfork
= XFS_DATA_FORK
;
5630 struct xfs_mount
*mp
= ip
->i_mount
;
5631 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5632 struct xfs_btree_cur
*cur
= NULL
;
5633 struct xfs_bmbt_irec got
, prev
;
5634 struct xfs_iext_cursor icur
;
5635 xfs_fileoff_t new_startoff
;
5639 if (unlikely(XFS_TEST_ERROR(
5640 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
5641 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
5642 mp
, XFS_ERRTAG_BMAPIFORMAT
))) {
5643 XFS_ERROR_REPORT(__func__
, XFS_ERRLEVEL_LOW
, mp
);
5644 return -EFSCORRUPTED
;
5647 if (XFS_FORCED_SHUTDOWN(mp
))
5650 ASSERT(xfs_isilocked(ip
, XFS_IOLOCK_EXCL
| XFS_ILOCK_EXCL
));
5652 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
5653 error
= xfs_iread_extents(tp
, ip
, whichfork
);
5658 if (ifp
->if_flags
& XFS_IFBROOT
) {
5659 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
5660 cur
->bc_private
.b
.firstblock
= *firstblock
;
5661 cur
->bc_private
.b
.dfops
= dfops
;
5662 cur
->bc_private
.b
.flags
= 0;
5665 if (!xfs_iext_lookup_extent(ip
, ifp
, *next_fsb
, &icur
, &got
)) {
5669 XFS_WANT_CORRUPTED_GOTO(mp
, !isnullstartblock(got
.br_startblock
),
5672 new_startoff
= got
.br_startoff
- offset_shift_fsb
;
5673 if (xfs_iext_peek_prev_extent(ifp
, &icur
, &prev
)) {
5674 if (new_startoff
< prev
.br_startoff
+ prev
.br_blockcount
) {
5679 if (xfs_bmse_can_merge(&prev
, &got
, offset_shift_fsb
)) {
5680 error
= xfs_bmse_merge(ip
, whichfork
, offset_shift_fsb
,
5681 &icur
, &got
, &prev
, cur
, &logflags
,
5688 if (got
.br_startoff
< offset_shift_fsb
) {
5694 error
= xfs_bmap_shift_update_extent(ip
, whichfork
, &icur
, &got
, cur
,
5695 &logflags
, dfops
, new_startoff
);
5700 if (!xfs_iext_next_extent(ifp
, &icur
, &got
)) {
5705 *next_fsb
= got
.br_startoff
;
5708 xfs_btree_del_cursor(cur
,
5709 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
5711 xfs_trans_log_inode(tp
, ip
, logflags
);
5716 xfs_bmap_insert_extents(
5717 struct xfs_trans
*tp
,
5718 struct xfs_inode
*ip
,
5719 xfs_fileoff_t
*next_fsb
,
5720 xfs_fileoff_t offset_shift_fsb
,
5722 xfs_fileoff_t stop_fsb
,
5723 xfs_fsblock_t
*firstblock
,
5724 struct xfs_defer_ops
*dfops
)
5726 int whichfork
= XFS_DATA_FORK
;
5727 struct xfs_mount
*mp
= ip
->i_mount
;
5728 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5729 struct xfs_btree_cur
*cur
= NULL
;
5730 struct xfs_bmbt_irec got
, next
;
5731 struct xfs_iext_cursor icur
;
5732 xfs_fileoff_t new_startoff
;
5736 if (unlikely(XFS_TEST_ERROR(
5737 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
5738 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
5739 mp
, XFS_ERRTAG_BMAPIFORMAT
))) {
5740 XFS_ERROR_REPORT(__func__
, XFS_ERRLEVEL_LOW
, mp
);
5741 return -EFSCORRUPTED
;
5744 if (XFS_FORCED_SHUTDOWN(mp
))
5747 ASSERT(xfs_isilocked(ip
, XFS_IOLOCK_EXCL
| XFS_ILOCK_EXCL
));
5749 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
5750 error
= xfs_iread_extents(tp
, ip
, whichfork
);
5755 if (ifp
->if_flags
& XFS_IFBROOT
) {
5756 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
5757 cur
->bc_private
.b
.firstblock
= *firstblock
;
5758 cur
->bc_private
.b
.dfops
= dfops
;
5759 cur
->bc_private
.b
.flags
= 0;
5762 if (*next_fsb
== NULLFSBLOCK
) {
5763 xfs_iext_last(ifp
, &icur
);
5764 if (!xfs_iext_get_extent(ifp
, &icur
, &got
) ||
5765 stop_fsb
> got
.br_startoff
) {
5770 if (!xfs_iext_lookup_extent(ip
, ifp
, *next_fsb
, &icur
, &got
)) {
5775 XFS_WANT_CORRUPTED_GOTO(mp
, !isnullstartblock(got
.br_startblock
),
5778 if (stop_fsb
>= got
.br_startoff
+ got
.br_blockcount
) {
5783 new_startoff
= got
.br_startoff
+ offset_shift_fsb
;
5784 if (xfs_iext_peek_next_extent(ifp
, &icur
, &next
)) {
5785 if (new_startoff
+ got
.br_blockcount
> next
.br_startoff
) {
5791 * Unlike a left shift (which involves a hole punch), a right
5792 * shift does not modify extent neighbors in any way. We should
5793 * never find mergeable extents in this scenario. Check anyways
5794 * and warn if we encounter two extents that could be one.
5796 if (xfs_bmse_can_merge(&got
, &next
, offset_shift_fsb
))
5800 error
= xfs_bmap_shift_update_extent(ip
, whichfork
, &icur
, &got
, cur
,
5801 &logflags
, dfops
, new_startoff
);
5805 if (!xfs_iext_prev_extent(ifp
, &icur
, &got
) ||
5806 stop_fsb
>= got
.br_startoff
+ got
.br_blockcount
) {
5811 *next_fsb
= got
.br_startoff
;
5814 xfs_btree_del_cursor(cur
,
5815 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
5817 xfs_trans_log_inode(tp
, ip
, logflags
);
5822 * Splits an extent into two extents at split_fsb block such that it is the
5823 * first block of the current_ext. @ext is a target extent to be split.
5824 * @split_fsb is a block where the extents is split. If split_fsb lies in a
5825 * hole or the first block of extents, just return 0.
5828 xfs_bmap_split_extent_at(
5829 struct xfs_trans
*tp
,
5830 struct xfs_inode
*ip
,
5831 xfs_fileoff_t split_fsb
,
5832 xfs_fsblock_t
*firstfsb
,
5833 struct xfs_defer_ops
*dfops
)
5835 int whichfork
= XFS_DATA_FORK
;
5836 struct xfs_btree_cur
*cur
= NULL
;
5837 struct xfs_bmbt_irec got
;
5838 struct xfs_bmbt_irec
new; /* split extent */
5839 struct xfs_mount
*mp
= ip
->i_mount
;
5840 struct xfs_ifork
*ifp
;
5841 xfs_fsblock_t gotblkcnt
; /* new block count for got */
5842 struct xfs_iext_cursor icur
;
5847 if (unlikely(XFS_TEST_ERROR(
5848 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
5849 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
5850 mp
, XFS_ERRTAG_BMAPIFORMAT
))) {
5851 XFS_ERROR_REPORT("xfs_bmap_split_extent_at",
5852 XFS_ERRLEVEL_LOW
, mp
);
5853 return -EFSCORRUPTED
;
5856 if (XFS_FORCED_SHUTDOWN(mp
))
5859 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5860 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
5861 /* Read in all the extents */
5862 error
= xfs_iread_extents(tp
, ip
, whichfork
);
5868 * If there are not extents, or split_fsb lies in a hole we are done.
5870 if (!xfs_iext_lookup_extent(ip
, ifp
, split_fsb
, &icur
, &got
) ||
5871 got
.br_startoff
>= split_fsb
)
5874 gotblkcnt
= split_fsb
- got
.br_startoff
;
5875 new.br_startoff
= split_fsb
;
5876 new.br_startblock
= got
.br_startblock
+ gotblkcnt
;
5877 new.br_blockcount
= got
.br_blockcount
- gotblkcnt
;
5878 new.br_state
= got
.br_state
;
5880 if (ifp
->if_flags
& XFS_IFBROOT
) {
5881 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
5882 cur
->bc_private
.b
.firstblock
= *firstfsb
;
5883 cur
->bc_private
.b
.dfops
= dfops
;
5884 cur
->bc_private
.b
.flags
= 0;
5885 error
= xfs_bmbt_lookup_eq(cur
, &got
, &i
);
5888 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, del_cursor
);
5891 got
.br_blockcount
= gotblkcnt
;
5892 xfs_iext_update_extent(ip
, xfs_bmap_fork_to_state(whichfork
), &icur
,
5895 logflags
= XFS_ILOG_CORE
;
5897 error
= xfs_bmbt_update(cur
, &got
);
5901 logflags
|= XFS_ILOG_DEXT
;
5903 /* Add new extent */
5904 xfs_iext_next(ifp
, &icur
);
5905 xfs_iext_insert(ip
, &icur
, &new, 0);
5906 XFS_IFORK_NEXT_SET(ip
, whichfork
,
5907 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
5910 error
= xfs_bmbt_lookup_eq(cur
, &new, &i
);
5913 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, del_cursor
);
5914 error
= xfs_btree_insert(cur
, &i
);
5917 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, del_cursor
);
5921 * Convert to a btree if necessary.
5923 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
5924 int tmp_logflags
; /* partial log flag return val */
5926 ASSERT(cur
== NULL
);
5927 error
= xfs_bmap_extents_to_btree(tp
, ip
, firstfsb
, dfops
,
5928 &cur
, 0, &tmp_logflags
, whichfork
);
5929 logflags
|= tmp_logflags
;
5934 cur
->bc_private
.b
.allocated
= 0;
5935 xfs_btree_del_cursor(cur
,
5936 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
5940 xfs_trans_log_inode(tp
, ip
, logflags
);
5945 xfs_bmap_split_extent(
5946 struct xfs_inode
*ip
,
5947 xfs_fileoff_t split_fsb
)
5949 struct xfs_mount
*mp
= ip
->i_mount
;
5950 struct xfs_trans
*tp
;
5951 struct xfs_defer_ops dfops
;
5952 xfs_fsblock_t firstfsb
;
5955 error
= xfs_trans_alloc(mp
, &M_RES(mp
)->tr_write
,
5956 XFS_DIOSTRAT_SPACE_RES(mp
, 0), 0, 0, &tp
);
5960 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
5961 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
);
5963 xfs_defer_init(&dfops
, &firstfsb
);
5965 error
= xfs_bmap_split_extent_at(tp
, ip
, split_fsb
,
5970 error
= xfs_defer_finish(&tp
, &dfops
);
5974 return xfs_trans_commit(tp
);
5977 xfs_defer_cancel(&dfops
);
5978 xfs_trans_cancel(tp
);
5982 /* Deferred mapping is only for real extents in the data fork. */
5984 xfs_bmap_is_update_needed(
5985 struct xfs_bmbt_irec
*bmap
)
5987 return bmap
->br_startblock
!= HOLESTARTBLOCK
&&
5988 bmap
->br_startblock
!= DELAYSTARTBLOCK
;
5991 /* Record a bmap intent. */
5994 struct xfs_mount
*mp
,
5995 struct xfs_defer_ops
*dfops
,
5996 enum xfs_bmap_intent_type type
,
5997 struct xfs_inode
*ip
,
5999 struct xfs_bmbt_irec
*bmap
)
6002 struct xfs_bmap_intent
*bi
;
6004 trace_xfs_bmap_defer(mp
,
6005 XFS_FSB_TO_AGNO(mp
, bmap
->br_startblock
),
6007 XFS_FSB_TO_AGBNO(mp
, bmap
->br_startblock
),
6008 ip
->i_ino
, whichfork
,
6010 bmap
->br_blockcount
,
6013 bi
= kmem_alloc(sizeof(struct xfs_bmap_intent
), KM_SLEEP
| KM_NOFS
);
6014 INIT_LIST_HEAD(&bi
->bi_list
);
6017 bi
->bi_whichfork
= whichfork
;
6018 bi
->bi_bmap
= *bmap
;
6020 error
= xfs_defer_ijoin(dfops
, bi
->bi_owner
);
6026 xfs_defer_add(dfops
, XFS_DEFER_OPS_TYPE_BMAP
, &bi
->bi_list
);
6030 /* Map an extent into a file. */
6032 xfs_bmap_map_extent(
6033 struct xfs_mount
*mp
,
6034 struct xfs_defer_ops
*dfops
,
6035 struct xfs_inode
*ip
,
6036 struct xfs_bmbt_irec
*PREV
)
6038 if (!xfs_bmap_is_update_needed(PREV
))
6041 return __xfs_bmap_add(mp
, dfops
, XFS_BMAP_MAP
, ip
,
6042 XFS_DATA_FORK
, PREV
);
6045 /* Unmap an extent out of a file. */
6047 xfs_bmap_unmap_extent(
6048 struct xfs_mount
*mp
,
6049 struct xfs_defer_ops
*dfops
,
6050 struct xfs_inode
*ip
,
6051 struct xfs_bmbt_irec
*PREV
)
6053 if (!xfs_bmap_is_update_needed(PREV
))
6056 return __xfs_bmap_add(mp
, dfops
, XFS_BMAP_UNMAP
, ip
,
6057 XFS_DATA_FORK
, PREV
);
6061 * Process one of the deferred bmap operations. We pass back the
6062 * btree cursor to maintain our lock on the bmapbt between calls.
6065 xfs_bmap_finish_one(
6066 struct xfs_trans
*tp
,
6067 struct xfs_defer_ops
*dfops
,
6068 struct xfs_inode
*ip
,
6069 enum xfs_bmap_intent_type type
,
6071 xfs_fileoff_t startoff
,
6072 xfs_fsblock_t startblock
,
6073 xfs_filblks_t
*blockcount
,
6076 xfs_fsblock_t firstfsb
;
6080 * firstfsb is tied to the transaction lifetime and is used to
6081 * ensure correct AG locking order and schedule work item
6082 * continuations. XFS_BUI_MAX_FAST_EXTENTS (== 1) restricts us
6083 * to only making one bmap call per transaction, so it should
6084 * be safe to have it as a local variable here.
6086 firstfsb
= NULLFSBLOCK
;
6088 trace_xfs_bmap_deferred(tp
->t_mountp
,
6089 XFS_FSB_TO_AGNO(tp
->t_mountp
, startblock
), type
,
6090 XFS_FSB_TO_AGBNO(tp
->t_mountp
, startblock
),
6091 ip
->i_ino
, whichfork
, startoff
, *blockcount
, state
);
6093 if (WARN_ON_ONCE(whichfork
!= XFS_DATA_FORK
))
6094 return -EFSCORRUPTED
;
6096 if (XFS_TEST_ERROR(false, tp
->t_mountp
,
6097 XFS_ERRTAG_BMAP_FINISH_ONE
))
6102 error
= xfs_bmapi_remap(tp
, ip
, startoff
, *blockcount
,
6106 case XFS_BMAP_UNMAP
:
6107 error
= __xfs_bunmapi(tp
, ip
, startoff
, blockcount
,
6108 XFS_BMAPI_REMAP
, 1, &firstfsb
, dfops
);
6112 error
= -EFSCORRUPTED
;