2 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 #include "libxfs_priv.h"
20 #include "xfs_shared.h"
21 #include "xfs_format.h"
22 #include "xfs_log_format.h"
23 #include "xfs_trans_resv.h"
26 #include "xfs_mount.h"
27 #include "xfs_defer.h"
28 #include "xfs_da_format.h"
29 #include "xfs_da_btree.h"
31 #include "xfs_inode.h"
32 #include "xfs_btree.h"
33 #include "xfs_trans.h"
34 #include "xfs_alloc.h"
36 #include "xfs_bmap_btree.h"
37 #include "xfs_trans_space.h"
38 #include "xfs_trace.h"
39 #include "xfs_attr_leaf.h"
40 #include "xfs_quota_defs.h"
42 #include "xfs_ag_resv.h"
43 #include "xfs_refcount.h"
46 kmem_zone_t
*xfs_bmap_free_item_zone
;
49 * Miscellaneous helper functions
53 * Compute and fill in the value of the maximum depth of a bmap btree
54 * in this filesystem. Done once, during mount.
57 xfs_bmap_compute_maxlevels(
58 xfs_mount_t
*mp
, /* file system mount structure */
59 int whichfork
) /* data or attr fork */
61 int level
; /* btree level */
62 uint maxblocks
; /* max blocks at this level */
63 uint maxleafents
; /* max leaf entries possible */
64 int maxrootrecs
; /* max records in root block */
65 int minleafrecs
; /* min records in leaf block */
66 int minnoderecs
; /* min records in node block */
67 int sz
; /* root block size */
70 * The maximum number of extents in a file, hence the maximum
71 * number of leaf entries, is controlled by the type of di_nextents
72 * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
73 * (a signed 16-bit number, xfs_aextnum_t).
75 * Note that we can no longer assume that if we are in ATTR1 that
76 * the fork offset of all the inodes will be
77 * (xfs_default_attroffset(ip) >> 3) because we could have mounted
78 * with ATTR2 and then mounted back with ATTR1, keeping the
79 * di_forkoff's fixed but probably at various positions. Therefore,
80 * for both ATTR1 and ATTR2 we have to assume the worst case scenario
81 * of a minimum size available.
83 if (whichfork
== XFS_DATA_FORK
) {
84 maxleafents
= MAXEXTNUM
;
85 sz
= XFS_BMDR_SPACE_CALC(MINDBTPTRS
);
87 maxleafents
= MAXAEXTNUM
;
88 sz
= XFS_BMDR_SPACE_CALC(MINABTPTRS
);
90 maxrootrecs
= xfs_bmdr_maxrecs(sz
, 0);
91 minleafrecs
= mp
->m_bmap_dmnr
[0];
92 minnoderecs
= mp
->m_bmap_dmnr
[1];
93 maxblocks
= (maxleafents
+ minleafrecs
- 1) / minleafrecs
;
94 for (level
= 1; maxblocks
> 1; level
++) {
95 if (maxblocks
<= maxrootrecs
)
98 maxblocks
= (maxblocks
+ minnoderecs
- 1) / minnoderecs
;
100 mp
->m_bm_maxlevels
[whichfork
] = level
;
103 STATIC
int /* error */
105 struct xfs_btree_cur
*cur
,
109 int *stat
) /* success/failure */
111 cur
->bc_rec
.b
.br_startoff
= off
;
112 cur
->bc_rec
.b
.br_startblock
= bno
;
113 cur
->bc_rec
.b
.br_blockcount
= len
;
114 return xfs_btree_lookup(cur
, XFS_LOOKUP_EQ
, stat
);
117 STATIC
int /* error */
119 struct xfs_btree_cur
*cur
,
123 int *stat
) /* success/failure */
125 cur
->bc_rec
.b
.br_startoff
= off
;
126 cur
->bc_rec
.b
.br_startblock
= bno
;
127 cur
->bc_rec
.b
.br_blockcount
= len
;
128 return xfs_btree_lookup(cur
, XFS_LOOKUP_GE
, stat
);
132 * Check if the inode needs to be converted to btree format.
134 static inline bool xfs_bmap_needs_btree(struct xfs_inode
*ip
, int whichfork
)
136 return whichfork
!= XFS_COW_FORK
&&
137 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
&&
138 XFS_IFORK_NEXTENTS(ip
, whichfork
) >
139 XFS_IFORK_MAXEXT(ip
, whichfork
);
143 * Check if the inode should be converted to extent format.
145 static inline bool xfs_bmap_wants_extents(struct xfs_inode
*ip
, int whichfork
)
147 return whichfork
!= XFS_COW_FORK
&&
148 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
&&
149 XFS_IFORK_NEXTENTS(ip
, whichfork
) <=
150 XFS_IFORK_MAXEXT(ip
, whichfork
);
154 * Update the record referred to by cur to the value given
155 * by [off, bno, len, state].
156 * This either works (return 0) or gets an EFSCORRUPTED error.
160 struct xfs_btree_cur
*cur
,
166 union xfs_btree_rec rec
;
168 xfs_bmbt_disk_set_allf(&rec
.bmbt
, off
, bno
, len
, state
);
169 return xfs_btree_update(cur
, &rec
);
173 * Compute the worst-case number of indirect blocks that will be used
174 * for ip's delayed extent of length "len".
177 xfs_bmap_worst_indlen(
178 xfs_inode_t
*ip
, /* incore inode pointer */
179 xfs_filblks_t len
) /* delayed extent length */
181 int level
; /* btree level number */
182 int maxrecs
; /* maximum record count at this level */
183 xfs_mount_t
*mp
; /* mount structure */
184 xfs_filblks_t rval
; /* return value */
187 maxrecs
= mp
->m_bmap_dmxr
[0];
188 for (level
= 0, rval
= 0;
189 level
< XFS_BM_MAXLEVELS(mp
, XFS_DATA_FORK
);
192 do_div(len
, maxrecs
);
195 return rval
+ XFS_BM_MAXLEVELS(mp
, XFS_DATA_FORK
) -
198 maxrecs
= mp
->m_bmap_dmxr
[1];
204 * Calculate the default attribute fork offset for newly created inodes.
207 xfs_default_attroffset(
208 struct xfs_inode
*ip
)
210 struct xfs_mount
*mp
= ip
->i_mount
;
213 if (mp
->m_sb
.sb_inodesize
== 256) {
214 offset
= XFS_LITINO(mp
, ip
->i_d
.di_version
) -
215 XFS_BMDR_SPACE_CALC(MINABTPTRS
);
217 offset
= XFS_BMDR_SPACE_CALC(6 * MINABTPTRS
);
220 ASSERT(offset
< XFS_LITINO(mp
, ip
->i_d
.di_version
));
225 * Helper routine to reset inode di_forkoff field when switching
226 * attribute fork from local to extent format - we reset it where
227 * possible to make space available for inline data fork extents.
230 xfs_bmap_forkoff_reset(
234 if (whichfork
== XFS_ATTR_FORK
&&
235 ip
->i_d
.di_format
!= XFS_DINODE_FMT_DEV
&&
236 ip
->i_d
.di_format
!= XFS_DINODE_FMT_UUID
&&
237 ip
->i_d
.di_format
!= XFS_DINODE_FMT_BTREE
) {
238 uint dfl_forkoff
= xfs_default_attroffset(ip
) >> 3;
240 if (dfl_forkoff
> ip
->i_d
.di_forkoff
)
241 ip
->i_d
.di_forkoff
= dfl_forkoff
;
246 STATIC
struct xfs_buf
*
248 struct xfs_btree_cur
*cur
,
251 struct xfs_log_item_desc
*lidp
;
257 for (i
= 0; i
< XFS_BTREE_MAXLEVELS
; i
++) {
258 if (!cur
->bc_bufs
[i
])
260 if (XFS_BUF_ADDR(cur
->bc_bufs
[i
]) == bno
)
261 return cur
->bc_bufs
[i
];
264 /* Chase down all the log items to see if the bp is there */
265 list_for_each_entry(lidp
, &cur
->bc_tp
->t_items
, lid_trans
) {
266 struct xfs_buf_log_item
*bip
;
267 bip
= (struct xfs_buf_log_item
*)lidp
->lid_item
;
268 if (bip
->bli_item
.li_type
== XFS_LI_BUF
&&
269 XFS_BUF_ADDR(bip
->bli_buf
) == bno
)
278 struct xfs_btree_block
*block
,
284 __be64
*pp
, *thispa
; /* pointer to block address */
285 xfs_bmbt_key_t
*prevp
, *keyp
;
287 ASSERT(be16_to_cpu(block
->bb_level
) > 0);
290 for( i
= 1; i
<= xfs_btree_get_numrecs(block
); i
++) {
291 dmxr
= mp
->m_bmap_dmxr
[0];
292 keyp
= XFS_BMBT_KEY_ADDR(mp
, block
, i
);
295 ASSERT(be64_to_cpu(prevp
->br_startoff
) <
296 be64_to_cpu(keyp
->br_startoff
));
301 * Compare the block numbers to see if there are dups.
304 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, i
, sz
);
306 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, i
, dmxr
);
308 for (j
= i
+1; j
<= be16_to_cpu(block
->bb_numrecs
); j
++) {
310 thispa
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, j
, sz
);
312 thispa
= XFS_BMBT_PTR_ADDR(mp
, block
, j
, dmxr
);
313 if (*thispa
== *pp
) {
314 xfs_warn(mp
, "%s: thispa(%d) == pp(%d) %Ld",
316 (unsigned long long)be64_to_cpu(*thispa
));
317 panic("%s: ptrs are equal in node\n",
325 * Check that the extents for the inode ip are in the right order in all
326 * btree leaves. THis becomes prohibitively expensive for large extent count
327 * files, so don't bother with inodes that have more than 10,000 extents in
328 * them. The btree record ordering checks will still be done, so for such large
329 * bmapbt constructs that is going to catch most corruptions.
332 xfs_bmap_check_leaf_extents(
333 xfs_btree_cur_t
*cur
, /* btree cursor or null */
334 xfs_inode_t
*ip
, /* incore inode pointer */
335 int whichfork
) /* data or attr fork */
337 struct xfs_btree_block
*block
; /* current btree block */
338 xfs_fsblock_t bno
; /* block # of "block" */
339 xfs_buf_t
*bp
; /* buffer for "block" */
340 int error
; /* error return value */
341 xfs_extnum_t i
=0, j
; /* index into the extents list */
342 xfs_ifork_t
*ifp
; /* fork structure */
343 int level
; /* btree level, for checking */
344 xfs_mount_t
*mp
; /* file system mount structure */
345 __be64
*pp
; /* pointer to block address */
346 xfs_bmbt_rec_t
*ep
; /* pointer to current extent */
347 xfs_bmbt_rec_t last
= {0, 0}; /* last extent in prev block */
348 xfs_bmbt_rec_t
*nextp
; /* pointer to next extent */
351 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
) {
355 /* skip large extent count inodes */
356 if (ip
->i_d
.di_nextents
> 10000)
361 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
362 block
= ifp
->if_broot
;
364 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
366 level
= be16_to_cpu(block
->bb_level
);
368 xfs_check_block(block
, mp
, 1, ifp
->if_broot_bytes
);
369 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, 1, ifp
->if_broot_bytes
);
370 bno
= be64_to_cpu(*pp
);
372 ASSERT(bno
!= NULLFSBLOCK
);
373 ASSERT(XFS_FSB_TO_AGNO(mp
, bno
) < mp
->m_sb
.sb_agcount
);
374 ASSERT(XFS_FSB_TO_AGBNO(mp
, bno
) < mp
->m_sb
.sb_agblocks
);
377 * Go down the tree until leaf level is reached, following the first
378 * pointer (leftmost) at each level.
380 while (level
-- > 0) {
381 /* See if buf is in cur first */
383 bp
= xfs_bmap_get_bp(cur
, XFS_FSB_TO_DADDR(mp
, bno
));
386 error
= xfs_btree_read_bufl(mp
, NULL
, bno
, 0, &bp
,
392 block
= XFS_BUF_TO_BLOCK(bp
);
397 * Check this block for basic sanity (increasing keys and
398 * no duplicate blocks).
401 xfs_check_block(block
, mp
, 0, 0);
402 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, mp
->m_bmap_dmxr
[1]);
403 bno
= be64_to_cpu(*pp
);
404 XFS_WANT_CORRUPTED_GOTO(mp
,
405 XFS_FSB_SANITY_CHECK(mp
, bno
), error0
);
408 xfs_trans_brelse(NULL
, bp
);
413 * Here with bp and block set to the leftmost leaf node in the tree.
418 * Loop over all leaf nodes checking that all extents are in the right order.
421 xfs_fsblock_t nextbno
;
422 xfs_extnum_t num_recs
;
425 num_recs
= xfs_btree_get_numrecs(block
);
428 * Read-ahead the next leaf block, if any.
431 nextbno
= be64_to_cpu(block
->bb_u
.l
.bb_rightsib
);
434 * Check all the extents to make sure they are OK.
435 * If we had a previous block, the last entry should
436 * conform with the first entry in this one.
439 ep
= XFS_BMBT_REC_ADDR(mp
, block
, 1);
441 ASSERT(xfs_bmbt_disk_get_startoff(&last
) +
442 xfs_bmbt_disk_get_blockcount(&last
) <=
443 xfs_bmbt_disk_get_startoff(ep
));
445 for (j
= 1; j
< num_recs
; j
++) {
446 nextp
= XFS_BMBT_REC_ADDR(mp
, block
, j
+ 1);
447 ASSERT(xfs_bmbt_disk_get_startoff(ep
) +
448 xfs_bmbt_disk_get_blockcount(ep
) <=
449 xfs_bmbt_disk_get_startoff(nextp
));
457 xfs_trans_brelse(NULL
, bp
);
461 * If we've reached the end, stop.
463 if (bno
== NULLFSBLOCK
)
467 bp
= xfs_bmap_get_bp(cur
, XFS_FSB_TO_DADDR(mp
, bno
));
470 error
= xfs_btree_read_bufl(mp
, NULL
, bno
, 0, &bp
,
476 block
= XFS_BUF_TO_BLOCK(bp
);
482 xfs_warn(mp
, "%s: at error0", __func__
);
484 xfs_trans_brelse(NULL
, bp
);
486 xfs_warn(mp
, "%s: BAD after btree leaves for %d extents",
488 panic("%s: CORRUPTED BTREE OR SOMETHING", __func__
);
493 * Add bmap trace insert entries for all the contents of the extent records.
496 xfs_bmap_trace_exlist(
497 xfs_inode_t
*ip
, /* incore inode pointer */
498 xfs_extnum_t cnt
, /* count of entries in the list */
499 int whichfork
, /* data or attr or cow fork */
500 unsigned long caller_ip
)
502 xfs_extnum_t idx
; /* extent record index */
503 xfs_ifork_t
*ifp
; /* inode fork pointer */
506 if (whichfork
== XFS_ATTR_FORK
)
507 state
|= BMAP_ATTRFORK
;
508 else if (whichfork
== XFS_COW_FORK
)
509 state
|= BMAP_COWFORK
;
511 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
512 ASSERT(cnt
== xfs_iext_count(ifp
));
513 for (idx
= 0; idx
< cnt
; idx
++)
514 trace_xfs_extlist(ip
, idx
, state
, caller_ip
);
518 * Validate that the bmbt_irecs being returned from bmapi are valid
519 * given the caller's original parameters. Specifically check the
520 * ranges of the returned irecs to ensure that they only extend beyond
521 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
524 xfs_bmap_validate_ret(
528 xfs_bmbt_irec_t
*mval
,
532 int i
; /* index to map values */
534 ASSERT(ret_nmap
<= nmap
);
536 for (i
= 0; i
< ret_nmap
; i
++) {
537 ASSERT(mval
[i
].br_blockcount
> 0);
538 if (!(flags
& XFS_BMAPI_ENTIRE
)) {
539 ASSERT(mval
[i
].br_startoff
>= bno
);
540 ASSERT(mval
[i
].br_blockcount
<= len
);
541 ASSERT(mval
[i
].br_startoff
+ mval
[i
].br_blockcount
<=
544 ASSERT(mval
[i
].br_startoff
< bno
+ len
);
545 ASSERT(mval
[i
].br_startoff
+ mval
[i
].br_blockcount
>
549 mval
[i
- 1].br_startoff
+ mval
[i
- 1].br_blockcount
==
550 mval
[i
].br_startoff
);
551 ASSERT(mval
[i
].br_startblock
!= DELAYSTARTBLOCK
&&
552 mval
[i
].br_startblock
!= HOLESTARTBLOCK
);
553 ASSERT(mval
[i
].br_state
== XFS_EXT_NORM
||
554 mval
[i
].br_state
== XFS_EXT_UNWRITTEN
);
559 #define xfs_bmap_check_leaf_extents(cur, ip, whichfork) do { } while (0)
560 #define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap) do { } while (0)
564 * bmap free list manipulation functions
568 * Add the extent to the list of extents to be free at transaction end.
569 * The list is maintained sorted (by block number).
573 struct xfs_mount
*mp
,
574 struct xfs_defer_ops
*dfops
,
577 struct xfs_owner_info
*oinfo
)
579 struct xfs_extent_free_item
*new; /* new element */
584 ASSERT(bno
!= NULLFSBLOCK
);
586 ASSERT(len
<= MAXEXTLEN
);
587 ASSERT(!isnullstartblock(bno
));
588 agno
= XFS_FSB_TO_AGNO(mp
, bno
);
589 agbno
= XFS_FSB_TO_AGBNO(mp
, bno
);
590 ASSERT(agno
< mp
->m_sb
.sb_agcount
);
591 ASSERT(agbno
< mp
->m_sb
.sb_agblocks
);
592 ASSERT(len
< mp
->m_sb
.sb_agblocks
);
593 ASSERT(agbno
+ len
<= mp
->m_sb
.sb_agblocks
);
595 ASSERT(xfs_bmap_free_item_zone
!= NULL
);
597 new = kmem_zone_alloc(xfs_bmap_free_item_zone
, KM_SLEEP
);
598 new->xefi_startblock
= bno
;
599 new->xefi_blockcount
= (xfs_extlen_t
)len
;
601 new->xefi_oinfo
= *oinfo
;
603 xfs_rmap_skip_owner_update(&new->xefi_oinfo
);
604 trace_xfs_bmap_free_defer(mp
, XFS_FSB_TO_AGNO(mp
, bno
), 0,
605 XFS_FSB_TO_AGBNO(mp
, bno
), len
);
606 xfs_defer_add(dfops
, XFS_DEFER_OPS_TYPE_FREE
, &new->xefi_list
);
610 * Inode fork format manipulation functions
614 * Transform a btree format file with only one leaf node, where the
615 * extents list will fit in the inode, into an extents format file.
616 * Since the file extents are already in-core, all we have to do is
617 * give up the space for the btree root and pitch the leaf block.
619 STATIC
int /* error */
620 xfs_bmap_btree_to_extents(
621 xfs_trans_t
*tp
, /* transaction pointer */
622 xfs_inode_t
*ip
, /* incore inode pointer */
623 xfs_btree_cur_t
*cur
, /* btree cursor */
624 int *logflagsp
, /* inode logging flags */
625 int whichfork
) /* data or attr fork */
628 struct xfs_btree_block
*cblock
;/* child btree block */
629 xfs_fsblock_t cbno
; /* child block number */
630 xfs_buf_t
*cbp
; /* child block's buffer */
631 int error
; /* error return value */
632 xfs_ifork_t
*ifp
; /* inode fork data */
633 xfs_mount_t
*mp
; /* mount point structure */
634 __be64
*pp
; /* ptr to block address */
635 struct xfs_btree_block
*rblock
;/* root btree block */
636 struct xfs_owner_info oinfo
;
639 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
640 ASSERT(whichfork
!= XFS_COW_FORK
);
641 ASSERT(ifp
->if_flags
& XFS_IFEXTENTS
);
642 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
);
643 rblock
= ifp
->if_broot
;
644 ASSERT(be16_to_cpu(rblock
->bb_level
) == 1);
645 ASSERT(be16_to_cpu(rblock
->bb_numrecs
) == 1);
646 ASSERT(xfs_bmbt_maxrecs(mp
, ifp
->if_broot_bytes
, 0) == 1);
647 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, rblock
, 1, ifp
->if_broot_bytes
);
648 cbno
= be64_to_cpu(*pp
);
651 if ((error
= xfs_btree_check_lptr(cur
, cbno
, 1)))
654 error
= xfs_btree_read_bufl(mp
, tp
, cbno
, 0, &cbp
, XFS_BMAP_BTREE_REF
,
658 cblock
= XFS_BUF_TO_BLOCK(cbp
);
659 if ((error
= xfs_btree_check_block(cur
, cblock
, 0, cbp
)))
661 xfs_rmap_ino_bmbt_owner(&oinfo
, ip
->i_ino
, whichfork
);
662 xfs_bmap_add_free(mp
, cur
->bc_private
.b
.dfops
, cbno
, 1, &oinfo
);
663 ip
->i_d
.di_nblocks
--;
664 xfs_trans_mod_dquot_byino(tp
, ip
, XFS_TRANS_DQ_BCOUNT
, -1L);
665 xfs_trans_binval(tp
, cbp
);
666 if (cur
->bc_bufs
[0] == cbp
)
667 cur
->bc_bufs
[0] = NULL
;
668 xfs_iroot_realloc(ip
, -1, whichfork
);
669 ASSERT(ifp
->if_broot
== NULL
);
670 ASSERT((ifp
->if_flags
& XFS_IFBROOT
) == 0);
671 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_EXTENTS
);
672 *logflagsp
= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
677 * Convert an extents-format file into a btree-format file.
678 * The new file will have a root block (in the inode) and a single child block.
680 STATIC
int /* error */
681 xfs_bmap_extents_to_btree(
682 xfs_trans_t
*tp
, /* transaction pointer */
683 xfs_inode_t
*ip
, /* incore inode pointer */
684 xfs_fsblock_t
*firstblock
, /* first-block-allocated */
685 struct xfs_defer_ops
*dfops
, /* blocks freed in xaction */
686 xfs_btree_cur_t
**curp
, /* cursor returned to caller */
687 int wasdel
, /* converting a delayed alloc */
688 int *logflagsp
, /* inode logging flags */
689 int whichfork
) /* data or attr fork */
691 struct xfs_btree_block
*ablock
; /* allocated (child) bt block */
692 xfs_buf_t
*abp
; /* buffer for ablock */
693 xfs_alloc_arg_t args
; /* allocation arguments */
694 xfs_bmbt_rec_t
*arp
; /* child record pointer */
695 struct xfs_btree_block
*block
; /* btree root block */
696 xfs_btree_cur_t
*cur
; /* bmap btree cursor */
697 xfs_bmbt_rec_host_t
*ep
; /* extent record pointer */
698 int error
; /* error return value */
699 xfs_extnum_t i
, cnt
; /* extent record index */
700 xfs_ifork_t
*ifp
; /* inode fork pointer */
701 xfs_bmbt_key_t
*kp
; /* root block key pointer */
702 xfs_mount_t
*mp
; /* mount structure */
703 xfs_extnum_t nextents
; /* number of file extents */
704 xfs_bmbt_ptr_t
*pp
; /* root block address pointer */
707 ASSERT(whichfork
!= XFS_COW_FORK
);
708 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
709 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
);
712 * Make space in the inode incore.
714 xfs_iroot_realloc(ip
, 1, whichfork
);
715 ifp
->if_flags
|= XFS_IFBROOT
;
720 block
= ifp
->if_broot
;
721 xfs_btree_init_block_int(mp
, block
, XFS_BUF_DADDR_NULL
,
722 XFS_BTNUM_BMAP
, 1, 1, ip
->i_ino
,
723 XFS_BTREE_LONG_PTRS
);
725 * Need a cursor. Can't allocate until bb_level is filled in.
727 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
728 cur
->bc_private
.b
.firstblock
= *firstblock
;
729 cur
->bc_private
.b
.dfops
= dfops
;
730 cur
->bc_private
.b
.flags
= wasdel
? XFS_BTCUR_BPRV_WASDEL
: 0;
732 * Convert to a btree with two levels, one record in root.
734 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_BTREE
);
735 memset(&args
, 0, sizeof(args
));
738 xfs_rmap_ino_bmbt_owner(&args
.oinfo
, ip
->i_ino
, whichfork
);
739 args
.firstblock
= *firstblock
;
740 if (*firstblock
== NULLFSBLOCK
) {
741 args
.type
= XFS_ALLOCTYPE_START_BNO
;
742 args
.fsbno
= XFS_INO_TO_FSB(mp
, ip
->i_ino
);
743 } else if (dfops
->dop_low
) {
744 args
.type
= XFS_ALLOCTYPE_START_BNO
;
745 args
.fsbno
= *firstblock
;
747 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
748 args
.fsbno
= *firstblock
;
750 args
.minlen
= args
.maxlen
= args
.prod
= 1;
751 args
.wasdel
= wasdel
;
753 if ((error
= xfs_alloc_vextent(&args
))) {
754 xfs_iroot_realloc(ip
, -1, whichfork
);
755 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
759 if (WARN_ON_ONCE(args
.fsbno
== NULLFSBLOCK
)) {
760 xfs_iroot_realloc(ip
, -1, whichfork
);
761 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
765 * Allocation can't fail, the space was reserved.
767 ASSERT(*firstblock
== NULLFSBLOCK
||
768 args
.agno
>= XFS_FSB_TO_AGNO(mp
, *firstblock
));
769 *firstblock
= cur
->bc_private
.b
.firstblock
= args
.fsbno
;
770 cur
->bc_private
.b
.allocated
++;
771 ip
->i_d
.di_nblocks
++;
772 xfs_trans_mod_dquot_byino(tp
, ip
, XFS_TRANS_DQ_BCOUNT
, 1L);
773 abp
= xfs_btree_get_bufl(mp
, tp
, args
.fsbno
, 0);
775 * Fill in the child block.
777 abp
->b_ops
= &xfs_bmbt_buf_ops
;
778 ablock
= XFS_BUF_TO_BLOCK(abp
);
779 xfs_btree_init_block_int(mp
, ablock
, abp
->b_bn
,
780 XFS_BTNUM_BMAP
, 0, 0, ip
->i_ino
,
781 XFS_BTREE_LONG_PTRS
);
783 arp
= XFS_BMBT_REC_ADDR(mp
, ablock
, 1);
784 nextents
= xfs_iext_count(ifp
);
785 for (cnt
= i
= 0; i
< nextents
; i
++) {
786 ep
= xfs_iext_get_ext(ifp
, i
);
787 if (!isnullstartblock(xfs_bmbt_get_startblock(ep
))) {
788 arp
->l0
= cpu_to_be64(ep
->l0
);
789 arp
->l1
= cpu_to_be64(ep
->l1
);
793 ASSERT(cnt
== XFS_IFORK_NEXTENTS(ip
, whichfork
));
794 xfs_btree_set_numrecs(ablock
, cnt
);
797 * Fill in the root key and pointer.
799 kp
= XFS_BMBT_KEY_ADDR(mp
, block
, 1);
800 arp
= XFS_BMBT_REC_ADDR(mp
, ablock
, 1);
801 kp
->br_startoff
= cpu_to_be64(xfs_bmbt_disk_get_startoff(arp
));
802 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, xfs_bmbt_get_maxrecs(cur
,
803 be16_to_cpu(block
->bb_level
)));
804 *pp
= cpu_to_be64(args
.fsbno
);
807 * Do all this logging at the end so that
808 * the root is at the right level.
810 xfs_btree_log_block(cur
, abp
, XFS_BB_ALL_BITS
);
811 xfs_btree_log_recs(cur
, abp
, 1, be16_to_cpu(ablock
->bb_numrecs
));
812 ASSERT(*curp
== NULL
);
814 *logflagsp
= XFS_ILOG_CORE
| xfs_ilog_fbroot(whichfork
);
819 * Convert a local file to an extents file.
820 * This code is out of bounds for data forks of regular files,
821 * since the file data needs to get logged so things will stay consistent.
822 * (The bmap-level manipulations are ok, though).
825 xfs_bmap_local_to_extents_empty(
826 struct xfs_inode
*ip
,
829 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
831 ASSERT(whichfork
!= XFS_COW_FORK
);
832 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
833 ASSERT(ifp
->if_bytes
== 0);
834 ASSERT(XFS_IFORK_NEXTENTS(ip
, whichfork
) == 0);
836 xfs_bmap_forkoff_reset(ip
, whichfork
);
837 ifp
->if_flags
&= ~XFS_IFINLINE
;
838 ifp
->if_flags
|= XFS_IFEXTENTS
;
839 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_EXTENTS
);
843 STATIC
int /* error */
844 xfs_bmap_local_to_extents(
845 xfs_trans_t
*tp
, /* transaction pointer */
846 xfs_inode_t
*ip
, /* incore inode pointer */
847 xfs_fsblock_t
*firstblock
, /* first block allocated in xaction */
848 xfs_extlen_t total
, /* total blocks needed by transaction */
849 int *logflagsp
, /* inode logging flags */
851 void (*init_fn
)(struct xfs_trans
*tp
,
853 struct xfs_inode
*ip
,
854 struct xfs_ifork
*ifp
))
857 int flags
; /* logging flags returned */
858 xfs_ifork_t
*ifp
; /* inode fork pointer */
859 xfs_alloc_arg_t args
; /* allocation arguments */
860 xfs_buf_t
*bp
; /* buffer for extent block */
861 struct xfs_bmbt_irec rec
;
864 * We don't want to deal with the case of keeping inode data inline yet.
865 * So sending the data fork of a regular inode is invalid.
867 ASSERT(!(S_ISREG(VFS_I(ip
)->i_mode
) && whichfork
== XFS_DATA_FORK
));
868 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
869 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
871 if (!ifp
->if_bytes
) {
872 xfs_bmap_local_to_extents_empty(ip
, whichfork
);
873 flags
= XFS_ILOG_CORE
;
879 ASSERT((ifp
->if_flags
& (XFS_IFINLINE
|XFS_IFEXTENTS
|XFS_IFEXTIREC
)) ==
881 memset(&args
, 0, sizeof(args
));
883 args
.mp
= ip
->i_mount
;
884 xfs_rmap_ino_owner(&args
.oinfo
, ip
->i_ino
, whichfork
, 0);
885 args
.firstblock
= *firstblock
;
887 * Allocate a block. We know we need only one, since the
888 * file currently fits in an inode.
890 if (*firstblock
== NULLFSBLOCK
) {
891 args
.fsbno
= XFS_INO_TO_FSB(args
.mp
, ip
->i_ino
);
892 args
.type
= XFS_ALLOCTYPE_START_BNO
;
894 args
.fsbno
= *firstblock
;
895 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
898 args
.minlen
= args
.maxlen
= args
.prod
= 1;
899 error
= xfs_alloc_vextent(&args
);
903 /* Can't fail, the space was reserved. */
904 ASSERT(args
.fsbno
!= NULLFSBLOCK
);
905 ASSERT(args
.len
== 1);
906 *firstblock
= args
.fsbno
;
907 bp
= xfs_btree_get_bufl(args
.mp
, tp
, args
.fsbno
, 0);
910 * Initialize the block, copy the data and log the remote buffer.
912 * The callout is responsible for logging because the remote format
913 * might differ from the local format and thus we don't know how much to
914 * log here. Note that init_fn must also set the buffer log item type
917 init_fn(tp
, bp
, ip
, ifp
);
919 /* account for the change in fork size */
920 xfs_idata_realloc(ip
, -ifp
->if_bytes
, whichfork
);
921 xfs_bmap_local_to_extents_empty(ip
, whichfork
);
922 flags
|= XFS_ILOG_CORE
;
925 rec
.br_startblock
= args
.fsbno
;
926 rec
.br_blockcount
= 1;
927 rec
.br_state
= XFS_EXT_NORM
;
928 xfs_iext_insert(ip
, 0, 1, &rec
, 0);
930 trace_xfs_bmap_post_update(ip
, 0,
931 whichfork
== XFS_ATTR_FORK
? BMAP_ATTRFORK
: 0,
933 XFS_IFORK_NEXT_SET(ip
, whichfork
, 1);
934 ip
->i_d
.di_nblocks
= 1;
935 xfs_trans_mod_dquot_byino(tp
, ip
,
936 XFS_TRANS_DQ_BCOUNT
, 1L);
937 flags
|= xfs_ilog_fext(whichfork
);
945 * Called from xfs_bmap_add_attrfork to handle btree format files.
947 STATIC
int /* error */
948 xfs_bmap_add_attrfork_btree(
949 xfs_trans_t
*tp
, /* transaction pointer */
950 xfs_inode_t
*ip
, /* incore inode pointer */
951 xfs_fsblock_t
*firstblock
, /* first block allocated */
952 struct xfs_defer_ops
*dfops
, /* blocks to free at commit */
953 int *flags
) /* inode logging flags */
955 xfs_btree_cur_t
*cur
; /* btree cursor */
956 int error
; /* error return value */
957 xfs_mount_t
*mp
; /* file system mount struct */
958 int stat
; /* newroot status */
961 if (ip
->i_df
.if_broot_bytes
<= XFS_IFORK_DSIZE(ip
))
962 *flags
|= XFS_ILOG_DBROOT
;
964 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, XFS_DATA_FORK
);
965 cur
->bc_private
.b
.dfops
= dfops
;
966 cur
->bc_private
.b
.firstblock
= *firstblock
;
967 if ((error
= xfs_bmbt_lookup_ge(cur
, 0, 0, 0, &stat
)))
969 /* must be at least one entry */
970 XFS_WANT_CORRUPTED_GOTO(mp
, stat
== 1, error0
);
971 if ((error
= xfs_btree_new_iroot(cur
, flags
, &stat
)))
974 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
977 *firstblock
= cur
->bc_private
.b
.firstblock
;
978 cur
->bc_private
.b
.allocated
= 0;
979 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
983 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
988 * Called from xfs_bmap_add_attrfork to handle extents format files.
990 STATIC
int /* error */
991 xfs_bmap_add_attrfork_extents(
992 xfs_trans_t
*tp
, /* transaction pointer */
993 xfs_inode_t
*ip
, /* incore inode pointer */
994 xfs_fsblock_t
*firstblock
, /* first block allocated */
995 struct xfs_defer_ops
*dfops
, /* blocks to free at commit */
996 int *flags
) /* inode logging flags */
998 xfs_btree_cur_t
*cur
; /* bmap btree cursor */
999 int error
; /* error return value */
1001 if (ip
->i_d
.di_nextents
* sizeof(xfs_bmbt_rec_t
) <= XFS_IFORK_DSIZE(ip
))
1004 error
= xfs_bmap_extents_to_btree(tp
, ip
, firstblock
, dfops
, &cur
, 0,
1005 flags
, XFS_DATA_FORK
);
1007 cur
->bc_private
.b
.allocated
= 0;
1008 xfs_btree_del_cursor(cur
,
1009 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
1015 * Called from xfs_bmap_add_attrfork to handle local format files. Each
1016 * different data fork content type needs a different callout to do the
1017 * conversion. Some are basic and only require special block initialisation
1018 * callouts for the data formating, others (directories) are so specialised they
1019 * handle everything themselves.
1021 * XXX (dgc): investigate whether directory conversion can use the generic
1022 * formatting callout. It should be possible - it's just a very complex
1025 STATIC
int /* error */
1026 xfs_bmap_add_attrfork_local(
1027 xfs_trans_t
*tp
, /* transaction pointer */
1028 xfs_inode_t
*ip
, /* incore inode pointer */
1029 xfs_fsblock_t
*firstblock
, /* first block allocated */
1030 struct xfs_defer_ops
*dfops
, /* blocks to free at commit */
1031 int *flags
) /* inode logging flags */
1033 xfs_da_args_t dargs
; /* args for dir/attr code */
1035 if (ip
->i_df
.if_bytes
<= XFS_IFORK_DSIZE(ip
))
1038 if (S_ISDIR(VFS_I(ip
)->i_mode
)) {
1039 memset(&dargs
, 0, sizeof(dargs
));
1040 dargs
.geo
= ip
->i_mount
->m_dir_geo
;
1042 dargs
.firstblock
= firstblock
;
1043 dargs
.dfops
= dfops
;
1044 dargs
.total
= dargs
.geo
->fsbcount
;
1045 dargs
.whichfork
= XFS_DATA_FORK
;
1047 return xfs_dir2_sf_to_block(&dargs
);
1050 if (S_ISLNK(VFS_I(ip
)->i_mode
))
1051 return xfs_bmap_local_to_extents(tp
, ip
, firstblock
, 1,
1052 flags
, XFS_DATA_FORK
,
1053 xfs_symlink_local_to_remote
);
1055 /* should only be called for types that support local format data */
1057 return -EFSCORRUPTED
;
1061 * Convert inode from non-attributed to attributed.
1062 * Must not be in a transaction, ip must not be locked.
1064 int /* error code */
1065 xfs_bmap_add_attrfork(
1066 xfs_inode_t
*ip
, /* incore inode pointer */
1067 int size
, /* space new attribute needs */
1068 int rsvd
) /* xact may use reserved blks */
1070 xfs_fsblock_t firstblock
; /* 1st block/ag allocated */
1071 struct xfs_defer_ops dfops
; /* freed extent records */
1072 xfs_mount_t
*mp
; /* mount structure */
1073 xfs_trans_t
*tp
; /* transaction pointer */
1074 int blks
; /* space reservation */
1075 int version
= 1; /* superblock attr version */
1076 int logflags
; /* logging flags */
1077 int error
; /* error return value */
1079 ASSERT(XFS_IFORK_Q(ip
) == 0);
1082 ASSERT(!XFS_NOT_DQATTACHED(mp
, ip
));
1084 blks
= XFS_ADDAFORK_SPACE_RES(mp
);
1086 error
= xfs_trans_alloc(mp
, &M_RES(mp
)->tr_addafork
, blks
, 0,
1087 rsvd
? XFS_TRANS_RESERVE
: 0, &tp
);
1091 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
1092 error
= xfs_trans_reserve_quota_nblks(tp
, ip
, blks
, 0, rsvd
?
1093 XFS_QMOPT_RES_REGBLKS
| XFS_QMOPT_FORCE_RES
:
1094 XFS_QMOPT_RES_REGBLKS
);
1097 if (XFS_IFORK_Q(ip
))
1099 if (ip
->i_d
.di_anextents
!= 0) {
1100 error
= -EFSCORRUPTED
;
1103 if (ip
->i_d
.di_aformat
!= XFS_DINODE_FMT_EXTENTS
) {
1105 * For inodes coming from pre-6.2 filesystems.
1107 ASSERT(ip
->i_d
.di_aformat
== 0);
1108 ip
->i_d
.di_aformat
= XFS_DINODE_FMT_EXTENTS
;
1111 xfs_trans_ijoin(tp
, ip
, 0);
1112 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
1114 switch (ip
->i_d
.di_format
) {
1115 case XFS_DINODE_FMT_DEV
:
1116 ip
->i_d
.di_forkoff
= roundup(sizeof(xfs_dev_t
), 8) >> 3;
1118 case XFS_DINODE_FMT_UUID
:
1119 ip
->i_d
.di_forkoff
= roundup(sizeof(uuid_t
), 8) >> 3;
1121 case XFS_DINODE_FMT_LOCAL
:
1122 case XFS_DINODE_FMT_EXTENTS
:
1123 case XFS_DINODE_FMT_BTREE
:
1124 ip
->i_d
.di_forkoff
= xfs_attr_shortform_bytesfit(ip
, size
);
1125 if (!ip
->i_d
.di_forkoff
)
1126 ip
->i_d
.di_forkoff
= xfs_default_attroffset(ip
) >> 3;
1127 else if (mp
->m_flags
& XFS_MOUNT_ATTR2
)
1136 ASSERT(ip
->i_afp
== NULL
);
1137 ip
->i_afp
= kmem_zone_zalloc(xfs_ifork_zone
, KM_SLEEP
);
1138 ip
->i_afp
->if_flags
= XFS_IFEXTENTS
;
1140 xfs_defer_init(&dfops
, &firstblock
);
1141 switch (ip
->i_d
.di_format
) {
1142 case XFS_DINODE_FMT_LOCAL
:
1143 error
= xfs_bmap_add_attrfork_local(tp
, ip
, &firstblock
, &dfops
,
1146 case XFS_DINODE_FMT_EXTENTS
:
1147 error
= xfs_bmap_add_attrfork_extents(tp
, ip
, &firstblock
,
1150 case XFS_DINODE_FMT_BTREE
:
1151 error
= xfs_bmap_add_attrfork_btree(tp
, ip
, &firstblock
, &dfops
,
1159 xfs_trans_log_inode(tp
, ip
, logflags
);
1162 if (!xfs_sb_version_hasattr(&mp
->m_sb
) ||
1163 (!xfs_sb_version_hasattr2(&mp
->m_sb
) && version
== 2)) {
1164 bool log_sb
= false;
1166 spin_lock(&mp
->m_sb_lock
);
1167 if (!xfs_sb_version_hasattr(&mp
->m_sb
)) {
1168 xfs_sb_version_addattr(&mp
->m_sb
);
1171 if (!xfs_sb_version_hasattr2(&mp
->m_sb
) && version
== 2) {
1172 xfs_sb_version_addattr2(&mp
->m_sb
);
1175 spin_unlock(&mp
->m_sb_lock
);
1180 error
= xfs_defer_finish(&tp
, &dfops
);
1183 error
= xfs_trans_commit(tp
);
1184 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
1188 xfs_defer_cancel(&dfops
);
1190 xfs_trans_cancel(tp
);
1191 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
1196 * Internal and external extent tree search functions.
1200 * Read in the extents to if_extents.
1201 * All inode fields are set up by caller, we just traverse the btree
1202 * and copy the records in. If the file system cannot contain unwritten
1203 * extents, the records are checked for no "state" flags.
1206 xfs_bmap_read_extents(
1207 xfs_trans_t
*tp
, /* transaction pointer */
1208 xfs_inode_t
*ip
, /* incore inode */
1209 int whichfork
) /* data or attr fork */
1211 struct xfs_btree_block
*block
; /* current btree block */
1212 xfs_fsblock_t bno
; /* block # of "block" */
1213 xfs_buf_t
*bp
; /* buffer for "block" */
1214 int error
; /* error return value */
1215 xfs_extnum_t i
, j
; /* index into the extents list */
1216 xfs_ifork_t
*ifp
; /* fork structure */
1217 int level
; /* btree level, for checking */
1218 xfs_mount_t
*mp
; /* file system mount structure */
1219 __be64
*pp
; /* pointer to block address */
1221 xfs_extnum_t room
; /* number of entries there's room for */
1224 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1225 block
= ifp
->if_broot
;
1227 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
1229 level
= be16_to_cpu(block
->bb_level
);
1231 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, 1, ifp
->if_broot_bytes
);
1232 bno
= be64_to_cpu(*pp
);
1235 * Go down the tree until leaf level is reached, following the first
1236 * pointer (leftmost) at each level.
1238 while (level
-- > 0) {
1239 error
= xfs_btree_read_bufl(mp
, tp
, bno
, 0, &bp
,
1240 XFS_BMAP_BTREE_REF
, &xfs_bmbt_buf_ops
);
1243 block
= XFS_BUF_TO_BLOCK(bp
);
1246 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, mp
->m_bmap_dmxr
[1]);
1247 bno
= be64_to_cpu(*pp
);
1248 XFS_WANT_CORRUPTED_GOTO(mp
,
1249 XFS_FSB_SANITY_CHECK(mp
, bno
), error0
);
1250 xfs_trans_brelse(tp
, bp
);
1253 * Here with bp and block set to the leftmost leaf node in the tree.
1255 room
= xfs_iext_count(ifp
);
1258 * Loop over all leaf nodes. Copy information to the extent records.
1261 xfs_bmbt_rec_t
*frp
;
1262 xfs_fsblock_t nextbno
;
1263 xfs_extnum_t num_recs
;
1265 num_recs
= xfs_btree_get_numrecs(block
);
1266 if (unlikely(i
+ num_recs
> room
)) {
1267 ASSERT(i
+ num_recs
<= room
);
1268 xfs_warn(ip
->i_mount
,
1269 "corrupt dinode %Lu, (btree extents).",
1270 (unsigned long long) ip
->i_ino
);
1271 XFS_CORRUPTION_ERROR("xfs_bmap_read_extents(1)",
1272 XFS_ERRLEVEL_LOW
, ip
->i_mount
, block
);
1276 * Read-ahead the next leaf block, if any.
1278 nextbno
= be64_to_cpu(block
->bb_u
.l
.bb_rightsib
);
1279 if (nextbno
!= NULLFSBLOCK
)
1280 xfs_btree_reada_bufl(mp
, nextbno
, 1,
1283 * Copy records into the extent records.
1285 frp
= XFS_BMBT_REC_ADDR(mp
, block
, 1);
1286 for (j
= 0; j
< num_recs
; j
++, i
++, frp
++) {
1287 xfs_bmbt_rec_host_t
*trp
= xfs_iext_get_ext(ifp
, i
);
1288 trp
->l0
= be64_to_cpu(frp
->l0
);
1289 trp
->l1
= be64_to_cpu(frp
->l1
);
1290 if (!xfs_bmbt_validate_extent(mp
, whichfork
, trp
)) {
1291 XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
1292 XFS_ERRLEVEL_LOW
, mp
);
1296 xfs_trans_brelse(tp
, bp
);
1299 * If we've reached the end, stop.
1301 if (bno
== NULLFSBLOCK
)
1303 error
= xfs_btree_read_bufl(mp
, tp
, bno
, 0, &bp
,
1304 XFS_BMAP_BTREE_REF
, &xfs_bmbt_buf_ops
);
1307 block
= XFS_BUF_TO_BLOCK(bp
);
1309 if (i
!= XFS_IFORK_NEXTENTS(ip
, whichfork
))
1310 return -EFSCORRUPTED
;
1311 ASSERT(i
== xfs_iext_count(ifp
));
1312 XFS_BMAP_TRACE_EXLIST(ip
, i
, whichfork
);
1315 xfs_trans_brelse(tp
, bp
);
1316 return -EFSCORRUPTED
;
1320 * Returns the file-relative block number of the first unused block(s)
1321 * in the file with at least "len" logically contiguous blocks free.
1322 * This is the lowest-address hole if the file has holes, else the first block
1323 * past the end of file.
1324 * Return 0 if the file is currently local (in-inode).
1327 xfs_bmap_first_unused(
1328 xfs_trans_t
*tp
, /* transaction pointer */
1329 xfs_inode_t
*ip
, /* incore inode */
1330 xfs_extlen_t len
, /* size of hole to find */
1331 xfs_fileoff_t
*first_unused
, /* unused block */
1332 int whichfork
) /* data or attr fork */
1334 int error
; /* error return value */
1335 int idx
; /* extent record index */
1336 xfs_ifork_t
*ifp
; /* inode fork pointer */
1337 xfs_fileoff_t lastaddr
; /* last block number seen */
1338 xfs_fileoff_t lowest
; /* lowest useful block */
1339 xfs_fileoff_t max
; /* starting useful block */
1340 xfs_extnum_t nextents
; /* number of extent entries */
1342 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
||
1343 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
||
1344 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
1345 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
) {
1349 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1350 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
1351 (error
= xfs_iread_extents(tp
, ip
, whichfork
)))
1353 lowest
= *first_unused
;
1354 nextents
= xfs_iext_count(ifp
);
1355 for (idx
= 0, lastaddr
= 0, max
= lowest
; idx
< nextents
; idx
++) {
1356 struct xfs_bmbt_irec got
;
1358 xfs_iext_get_extent(ifp
, idx
, &got
);
1361 * See if the hole before this extent will work.
1363 if (got
.br_startoff
>= lowest
+ len
&&
1364 got
.br_startoff
- max
>= len
) {
1365 *first_unused
= max
;
1368 lastaddr
= got
.br_startoff
+ got
.br_blockcount
;
1369 max
= XFS_FILEOFF_MAX(lastaddr
, lowest
);
1371 *first_unused
= max
;
1376 * Returns the file-relative block number of the last block - 1 before
1377 * last_block (input value) in the file.
1378 * This is not based on i_size, it is based on the extent records.
1379 * Returns 0 for local files, as they do not have extent records.
1382 xfs_bmap_last_before(
1383 struct xfs_trans
*tp
, /* transaction pointer */
1384 struct xfs_inode
*ip
, /* incore inode */
1385 xfs_fileoff_t
*last_block
, /* last block */
1386 int whichfork
) /* data or attr fork */
1388 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1389 struct xfs_bmbt_irec got
;
1393 switch (XFS_IFORK_FORMAT(ip
, whichfork
)) {
1394 case XFS_DINODE_FMT_LOCAL
:
1397 case XFS_DINODE_FMT_BTREE
:
1398 case XFS_DINODE_FMT_EXTENTS
:
1404 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
1405 error
= xfs_iread_extents(tp
, ip
, whichfork
);
1410 if (xfs_iext_lookup_extent(ip
, ifp
, *last_block
- 1, &idx
, &got
)) {
1411 if (got
.br_startoff
<= *last_block
- 1)
1415 if (xfs_iext_get_extent(ifp
, idx
- 1, &got
)) {
1416 *last_block
= got
.br_startoff
+ got
.br_blockcount
;
1425 xfs_bmap_last_extent(
1426 struct xfs_trans
*tp
,
1427 struct xfs_inode
*ip
,
1429 struct xfs_bmbt_irec
*rec
,
1432 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1436 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
1437 error
= xfs_iread_extents(tp
, ip
, whichfork
);
1442 nextents
= xfs_iext_count(ifp
);
1443 if (nextents
== 0) {
1448 xfs_iext_get_extent(ifp
, nextents
- 1, rec
);
1454 * Check the last inode extent to determine whether this allocation will result
1455 * in blocks being allocated at the end of the file. When we allocate new data
1456 * blocks at the end of the file which do not start at the previous data block,
1457 * we will try to align the new blocks at stripe unit boundaries.
1459 * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be
1460 * at, or past the EOF.
1464 struct xfs_bmalloca
*bma
,
1467 struct xfs_bmbt_irec rec
;
1472 error
= xfs_bmap_last_extent(NULL
, bma
->ip
, whichfork
, &rec
,
1483 * Check if we are allocation or past the last extent, or at least into
1484 * the last delayed allocated extent.
1486 bma
->aeof
= bma
->offset
>= rec
.br_startoff
+ rec
.br_blockcount
||
1487 (bma
->offset
>= rec
.br_startoff
&&
1488 isnullstartblock(rec
.br_startblock
));
1493 * Returns the file-relative block number of the first block past eof in
1494 * the file. This is not based on i_size, it is based on the extent records.
1495 * Returns 0 for local files, as they do not have extent records.
1498 xfs_bmap_last_offset(
1499 struct xfs_inode
*ip
,
1500 xfs_fileoff_t
*last_block
,
1503 struct xfs_bmbt_irec rec
;
1509 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
)
1512 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
&&
1513 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
1516 error
= xfs_bmap_last_extent(NULL
, ip
, whichfork
, &rec
, &is_empty
);
1517 if (error
|| is_empty
)
1520 *last_block
= rec
.br_startoff
+ rec
.br_blockcount
;
1525 * Returns whether the selected fork of the inode has exactly one
1526 * block or not. For the data fork we check this matches di_size,
1527 * implying the file's range is 0..bsize-1.
1529 int /* 1=>1 block, 0=>otherwise */
1531 xfs_inode_t
*ip
, /* incore inode */
1532 int whichfork
) /* data or attr fork */
1534 xfs_ifork_t
*ifp
; /* inode fork pointer */
1535 int rval
; /* return value */
1536 xfs_bmbt_irec_t s
; /* internal version of extent */
1539 if (whichfork
== XFS_DATA_FORK
)
1540 return XFS_ISIZE(ip
) == ip
->i_mount
->m_sb
.sb_blocksize
;
1542 if (XFS_IFORK_NEXTENTS(ip
, whichfork
) != 1)
1544 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
1546 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1547 ASSERT(ifp
->if_flags
& XFS_IFEXTENTS
);
1548 xfs_iext_get_extent(ifp
, 0, &s
);
1549 rval
= s
.br_startoff
== 0 && s
.br_blockcount
== 1;
1550 if (rval
&& whichfork
== XFS_DATA_FORK
)
1551 ASSERT(XFS_ISIZE(ip
) == ip
->i_mount
->m_sb
.sb_blocksize
);
1556 * Extent tree manipulation functions used during allocation.
1560 * Convert a delayed allocation to a real allocation.
1562 STATIC
int /* error */
1563 xfs_bmap_add_extent_delay_real(
1564 struct xfs_bmalloca
*bma
,
1567 struct xfs_bmbt_irec
*new = &bma
->got
;
1568 int diff
; /* temp value */
1569 int error
; /* error return value */
1570 int i
; /* temp state */
1571 xfs_ifork_t
*ifp
; /* inode fork pointer */
1572 xfs_fileoff_t new_endoff
; /* end offset of new entry */
1573 xfs_bmbt_irec_t r
[3]; /* neighbor extent entries */
1574 /* left is 0, right is 1, prev is 2 */
1575 int rval
=0; /* return value (logging flags) */
1576 int state
= 0;/* state bits, accessed thru macros */
1577 xfs_filblks_t da_new
; /* new count del alloc blocks used */
1578 xfs_filblks_t da_old
; /* old count del alloc blocks used */
1579 xfs_filblks_t temp
=0; /* value for da_new calculations */
1580 int tmp_rval
; /* partial logging flags */
1581 struct xfs_mount
*mp
;
1582 xfs_extnum_t
*nextents
;
1583 struct xfs_bmbt_irec old
;
1585 mp
= bma
->ip
->i_mount
;
1586 ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
1587 ASSERT(whichfork
!= XFS_ATTR_FORK
);
1588 nextents
= (whichfork
== XFS_COW_FORK
? &bma
->ip
->i_cnextents
:
1589 &bma
->ip
->i_d
.di_nextents
);
1591 ASSERT(bma
->idx
>= 0);
1592 ASSERT(bma
->idx
<= xfs_iext_count(ifp
));
1593 ASSERT(!isnullstartblock(new->br_startblock
));
1595 (bma
->cur
->bc_private
.b
.flags
& XFS_BTCUR_BPRV_WASDEL
));
1597 XFS_STATS_INC(mp
, xs_add_exlist
);
1603 if (whichfork
== XFS_COW_FORK
)
1604 state
|= BMAP_COWFORK
;
1607 * Set up a bunch of variables to make the tests simpler.
1609 xfs_iext_get_extent(ifp
, bma
->idx
, &PREV
);
1610 new_endoff
= new->br_startoff
+ new->br_blockcount
;
1611 ASSERT(isnullstartblock(PREV
.br_startblock
));
1612 ASSERT(PREV
.br_startoff
<= new->br_startoff
);
1613 ASSERT(PREV
.br_startoff
+ PREV
.br_blockcount
>= new_endoff
);
1615 da_old
= startblockval(PREV
.br_startblock
);
1619 * Set flags determining what part of the previous delayed allocation
1620 * extent is being replaced by a real allocation.
1622 if (PREV
.br_startoff
== new->br_startoff
)
1623 state
|= BMAP_LEFT_FILLING
;
1624 if (PREV
.br_startoff
+ PREV
.br_blockcount
== new_endoff
)
1625 state
|= BMAP_RIGHT_FILLING
;
1628 * Check and set flags if this segment has a left neighbor.
1629 * Don't set contiguous if the combined extent would be too large.
1632 state
|= BMAP_LEFT_VALID
;
1633 xfs_iext_get_extent(ifp
, bma
->idx
- 1, &LEFT
);
1635 if (isnullstartblock(LEFT
.br_startblock
))
1636 state
|= BMAP_LEFT_DELAY
;
1639 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
1640 LEFT
.br_startoff
+ LEFT
.br_blockcount
== new->br_startoff
&&
1641 LEFT
.br_startblock
+ LEFT
.br_blockcount
== new->br_startblock
&&
1642 LEFT
.br_state
== new->br_state
&&
1643 LEFT
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
1644 state
|= BMAP_LEFT_CONTIG
;
1647 * Check and set flags if this segment has a right neighbor.
1648 * Don't set contiguous if the combined extent would be too large.
1649 * Also check for all-three-contiguous being too large.
1651 if (bma
->idx
< xfs_iext_count(ifp
) - 1) {
1652 state
|= BMAP_RIGHT_VALID
;
1653 xfs_iext_get_extent(ifp
, bma
->idx
+ 1, &RIGHT
);
1655 if (isnullstartblock(RIGHT
.br_startblock
))
1656 state
|= BMAP_RIGHT_DELAY
;
1659 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
1660 new_endoff
== RIGHT
.br_startoff
&&
1661 new->br_startblock
+ new->br_blockcount
== RIGHT
.br_startblock
&&
1662 new->br_state
== RIGHT
.br_state
&&
1663 new->br_blockcount
+ RIGHT
.br_blockcount
<= MAXEXTLEN
&&
1664 ((state
& (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
1665 BMAP_RIGHT_FILLING
)) !=
1666 (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
1667 BMAP_RIGHT_FILLING
) ||
1668 LEFT
.br_blockcount
+ new->br_blockcount
+ RIGHT
.br_blockcount
1670 state
|= BMAP_RIGHT_CONTIG
;
1674 * Switch out based on the FILLING and CONTIG state bits.
1676 switch (state
& (BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
1677 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
)) {
1678 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
1679 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
1681 * Filling in all of a previously delayed allocation extent.
1682 * The left and right neighbors are both contiguous with new.
1685 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1686 LEFT
.br_blockcount
+= PREV
.br_blockcount
+ RIGHT
.br_blockcount
;
1687 xfs_iext_update_extent(ifp
, bma
->idx
, &LEFT
);
1688 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1690 xfs_iext_remove(bma
->ip
, bma
->idx
+ 1, 2, state
);
1692 if (bma
->cur
== NULL
)
1693 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1695 rval
= XFS_ILOG_CORE
;
1696 error
= xfs_bmbt_lookup_eq(bma
->cur
, RIGHT
.br_startoff
,
1697 RIGHT
.br_startblock
,
1698 RIGHT
.br_blockcount
, &i
);
1701 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1702 error
= xfs_btree_delete(bma
->cur
, &i
);
1705 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1706 error
= xfs_btree_decrement(bma
->cur
, 0, &i
);
1709 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1710 error
= xfs_bmbt_update(bma
->cur
, LEFT
.br_startoff
,
1712 LEFT
.br_blockcount
, LEFT
.br_state
);
1718 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
1720 * Filling in all of a previously delayed allocation extent.
1721 * The left neighbor is contiguous, the right is not.
1726 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1727 LEFT
.br_blockcount
+= PREV
.br_blockcount
;
1728 xfs_iext_update_extent(ifp
, bma
->idx
, &LEFT
);
1729 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1731 xfs_iext_remove(bma
->ip
, bma
->idx
+ 1, 1, state
);
1732 if (bma
->cur
== NULL
)
1733 rval
= XFS_ILOG_DEXT
;
1736 error
= xfs_bmbt_lookup_eq(bma
->cur
, old
.br_startoff
,
1737 old
.br_startblock
, old
.br_blockcount
,
1741 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1742 error
= xfs_bmbt_update(bma
->cur
, LEFT
.br_startoff
,
1744 LEFT
.br_blockcount
, LEFT
.br_state
);
1750 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
1752 * Filling in all of a previously delayed allocation extent.
1753 * The right neighbor is contiguous, the left is not.
1755 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1756 PREV
.br_startblock
= new->br_startblock
;
1757 PREV
.br_blockcount
+= RIGHT
.br_blockcount
;
1758 xfs_iext_update_extent(ifp
, bma
->idx
, &PREV
);
1759 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1761 xfs_iext_remove(bma
->ip
, bma
->idx
+ 1, 1, state
);
1762 if (bma
->cur
== NULL
)
1763 rval
= XFS_ILOG_DEXT
;
1766 error
= xfs_bmbt_lookup_eq(bma
->cur
, RIGHT
.br_startoff
,
1767 RIGHT
.br_startblock
,
1768 RIGHT
.br_blockcount
, &i
);
1771 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1772 error
= xfs_bmbt_update(bma
->cur
, PREV
.br_startoff
,
1774 PREV
.br_blockcount
, PREV
.br_state
);
1780 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
1782 * Filling in all of a previously delayed allocation extent.
1783 * Neither the left nor right neighbors are contiguous with
1786 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1787 PREV
.br_startblock
= new->br_startblock
;
1788 PREV
.br_state
= new->br_state
;
1789 xfs_iext_update_extent(ifp
, bma
->idx
, &PREV
);
1790 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1793 if (bma
->cur
== NULL
)
1794 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1796 rval
= XFS_ILOG_CORE
;
1797 error
= xfs_bmbt_lookup_eq(bma
->cur
, new->br_startoff
,
1798 new->br_startblock
, new->br_blockcount
,
1802 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
1803 bma
->cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
1804 error
= xfs_btree_insert(bma
->cur
, &i
);
1807 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1811 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
:
1813 * Filling in the first part of a previous delayed allocation.
1814 * The left neighbor is contiguous.
1817 temp
= PREV
.br_blockcount
- new->br_blockcount
;
1818 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
1819 startblockval(PREV
.br_startblock
));
1821 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
- 1, state
, _THIS_IP_
);
1822 LEFT
.br_blockcount
+= new->br_blockcount
;
1823 xfs_iext_update_extent(ifp
, bma
->idx
- 1, &LEFT
);
1824 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
- 1, state
, _THIS_IP_
);
1826 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1827 PREV
.br_blockcount
= temp
= PREV
.br_blockcount
- new->br_blockcount
;
1828 PREV
.br_startoff
+= new->br_blockcount
;
1829 PREV
.br_startblock
= nullstartblock(da_new
);
1830 xfs_iext_update_extent(ifp
, bma
->idx
, &PREV
);
1831 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1833 if (bma
->cur
== NULL
)
1834 rval
= XFS_ILOG_DEXT
;
1837 error
= xfs_bmbt_lookup_eq(bma
->cur
, old
.br_startoff
,
1838 old
.br_startblock
, old
.br_blockcount
,
1842 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1843 error
= xfs_bmbt_update(bma
->cur
, LEFT
.br_startoff
,
1844 LEFT
.br_startblock
, LEFT
.br_blockcount
,
1853 case BMAP_LEFT_FILLING
:
1855 * Filling in the first part of a previous delayed allocation.
1856 * The left neighbor is not contiguous.
1858 xfs_iext_insert(bma
->ip
, bma
->idx
, 1, new, state
);
1860 if (bma
->cur
== NULL
)
1861 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1863 rval
= XFS_ILOG_CORE
;
1864 error
= xfs_bmbt_lookup_eq(bma
->cur
, new->br_startoff
,
1865 new->br_startblock
, new->br_blockcount
,
1869 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
1870 bma
->cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
1871 error
= xfs_btree_insert(bma
->cur
, &i
);
1874 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1877 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
1878 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
1879 bma
->firstblock
, bma
->dfops
,
1880 &bma
->cur
, 1, &tmp_rval
, whichfork
);
1886 temp
= PREV
.br_blockcount
- new->br_blockcount
;
1887 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
1888 startblockval(PREV
.br_startblock
) -
1889 (bma
->cur
? bma
->cur
->bc_private
.b
.allocated
: 0));
1891 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
+ 1, state
, _THIS_IP_
);
1892 PREV
.br_startoff
= new_endoff
;
1893 PREV
.br_blockcount
= temp
;
1894 PREV
.br_startblock
= nullstartblock(da_new
);
1895 xfs_iext_update_extent(ifp
, bma
->idx
+ 1, &PREV
);
1896 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
+ 1, state
, _THIS_IP_
);
1900 case BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
1902 * Filling in the last part of a previous delayed allocation.
1903 * The right neighbor is contiguous with the new allocation.
1906 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
+ 1, state
, _THIS_IP_
);
1907 RIGHT
.br_startoff
= new->br_startoff
;
1908 RIGHT
.br_startblock
= new->br_startblock
;
1909 RIGHT
.br_blockcount
+= new->br_blockcount
;
1910 xfs_iext_update_extent(ifp
, bma
->idx
+ 1, &RIGHT
);
1911 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
+ 1, state
, _THIS_IP_
);
1913 if (bma
->cur
== NULL
)
1914 rval
= XFS_ILOG_DEXT
;
1917 error
= xfs_bmbt_lookup_eq(bma
->cur
, old
.br_startoff
,
1919 old
.br_blockcount
, &i
);
1922 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1923 error
= xfs_bmbt_update(bma
->cur
, RIGHT
.br_startoff
,
1924 RIGHT
.br_startblock
, RIGHT
.br_blockcount
,
1930 temp
= PREV
.br_blockcount
- new->br_blockcount
;
1931 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
1932 startblockval(PREV
.br_startblock
));
1934 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1935 PREV
.br_blockcount
= temp
;
1936 PREV
.br_startblock
= nullstartblock(da_new
);
1937 xfs_iext_update_extent(ifp
, bma
->idx
, &PREV
);
1938 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1943 case BMAP_RIGHT_FILLING
:
1945 * Filling in the last part of a previous delayed allocation.
1946 * The right neighbor is not contiguous.
1948 xfs_iext_insert(bma
->ip
, bma
->idx
+ 1, 1, new, state
);
1950 if (bma
->cur
== NULL
)
1951 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1953 rval
= XFS_ILOG_CORE
;
1954 error
= xfs_bmbt_lookup_eq(bma
->cur
, new->br_startoff
,
1955 new->br_startblock
, new->br_blockcount
,
1959 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
1960 bma
->cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
1961 error
= xfs_btree_insert(bma
->cur
, &i
);
1964 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1967 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
1968 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
1969 bma
->firstblock
, bma
->dfops
, &bma
->cur
, 1,
1970 &tmp_rval
, whichfork
);
1976 temp
= PREV
.br_blockcount
- new->br_blockcount
;
1977 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
1978 startblockval(PREV
.br_startblock
) -
1979 (bma
->cur
? bma
->cur
->bc_private
.b
.allocated
: 0));
1981 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1982 PREV
.br_startblock
= nullstartblock(da_new
);
1983 PREV
.br_blockcount
= temp
;
1984 xfs_iext_update_extent(ifp
, bma
->idx
, &PREV
);
1985 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1992 * Filling in the middle part of a previous delayed allocation.
1993 * Contiguity is impossible here.
1994 * This case is avoided almost all the time.
1996 * We start with a delayed allocation:
1998 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
2001 * and we are allocating:
2002 * +rrrrrrrrrrrrrrrrr+
2005 * and we set it up for insertion as:
2006 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
2008 * PREV @ idx LEFT RIGHT
2009 * inserted at idx + 1
2013 /* LEFT is the new middle */
2016 /* RIGHT is the new right */
2017 RIGHT
.br_state
= PREV
.br_state
;
2018 RIGHT
.br_startoff
= new_endoff
;
2019 RIGHT
.br_blockcount
=
2020 PREV
.br_startoff
+ PREV
.br_blockcount
- new_endoff
;
2021 RIGHT
.br_startblock
=
2022 nullstartblock(xfs_bmap_worst_indlen(bma
->ip
,
2023 RIGHT
.br_blockcount
));
2026 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, 0, _THIS_IP_
);
2027 PREV
.br_blockcount
= new->br_startoff
- PREV
.br_startoff
;
2028 PREV
.br_startblock
=
2029 nullstartblock(xfs_bmap_worst_indlen(bma
->ip
,
2030 PREV
.br_blockcount
));
2031 xfs_iext_update_extent(ifp
, bma
->idx
, &PREV
);
2032 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2034 /* insert LEFT (r[0]) and RIGHT (r[1]) at the same time */
2035 xfs_iext_insert(bma
->ip
, bma
->idx
+ 1, 2, &LEFT
, state
);
2038 if (bma
->cur
== NULL
)
2039 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2041 rval
= XFS_ILOG_CORE
;
2042 error
= xfs_bmbt_lookup_eq(bma
->cur
, new->br_startoff
,
2043 new->br_startblock
, new->br_blockcount
,
2047 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
2048 bma
->cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
2049 error
= xfs_btree_insert(bma
->cur
, &i
);
2052 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2055 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
2056 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
2057 bma
->firstblock
, bma
->dfops
, &bma
->cur
,
2058 1, &tmp_rval
, whichfork
);
2064 da_new
= startblockval(PREV
.br_startblock
) +
2065 startblockval(RIGHT
.br_startblock
);
2066 diff
= da_new
- startblockval(old
.br_startblock
);
2068 diff
+= bma
->cur
->bc_private
.b
.allocated
;
2070 error
= xfs_mod_fdblocks(bma
->ip
->i_mount
,
2071 -((int64_t)diff
), false);
2080 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2081 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2082 case BMAP_LEFT_FILLING
| BMAP_RIGHT_CONTIG
:
2083 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
2084 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2085 case BMAP_LEFT_CONTIG
:
2086 case BMAP_RIGHT_CONTIG
:
2088 * These cases are all impossible.
2093 /* add reverse mapping */
2094 error
= xfs_rmap_map_extent(mp
, bma
->dfops
, bma
->ip
, whichfork
, new);
2098 /* convert to a btree if necessary */
2099 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
2100 int tmp_logflags
; /* partial log flag return val */
2102 ASSERT(bma
->cur
== NULL
);
2103 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
2104 bma
->firstblock
, bma
->dfops
, &bma
->cur
,
2105 da_old
> 0, &tmp_logflags
, whichfork
);
2106 bma
->logflags
|= tmp_logflags
;
2111 /* adjust for changes in reserved delayed indirect blocks */
2112 if (da_old
|| da_new
) {
2115 temp
+= bma
->cur
->bc_private
.b
.allocated
;
2117 xfs_mod_fdblocks(bma
->ip
->i_mount
,
2118 (int64_t)(da_old
- temp
), false);
2121 /* clear out the allocated field, done with it now in any case. */
2123 bma
->cur
->bc_private
.b
.allocated
= 0;
2125 xfs_bmap_check_leaf_extents(bma
->cur
, bma
->ip
, whichfork
);
2127 if (whichfork
!= XFS_COW_FORK
)
2128 bma
->logflags
|= rval
;
2136 * Convert an unwritten allocation to a real allocation or vice versa.
2138 STATIC
int /* error */
2139 xfs_bmap_add_extent_unwritten_real(
2140 struct xfs_trans
*tp
,
2141 xfs_inode_t
*ip
, /* incore inode pointer */
2143 xfs_extnum_t
*idx
, /* extent number to update/insert */
2144 xfs_btree_cur_t
**curp
, /* if *curp is null, not a btree */
2145 xfs_bmbt_irec_t
*new, /* new data to add to file extents */
2146 xfs_fsblock_t
*first
, /* pointer to firstblock variable */
2147 struct xfs_defer_ops
*dfops
, /* list of extents to be freed */
2148 int *logflagsp
) /* inode logging flags */
2150 xfs_btree_cur_t
*cur
; /* btree cursor */
2151 xfs_bmbt_rec_host_t
*ep
; /* extent entry for idx */
2152 int error
; /* error return value */
2153 int i
; /* temp state */
2154 xfs_ifork_t
*ifp
; /* inode fork pointer */
2155 xfs_fileoff_t new_endoff
; /* end offset of new entry */
2156 xfs_exntst_t newext
; /* new extent state */
2157 xfs_exntst_t oldext
; /* old extent state */
2158 xfs_bmbt_irec_t r
[3]; /* neighbor extent entries */
2159 /* left is 0, right is 1, prev is 2 */
2160 int rval
=0; /* return value (logging flags) */
2161 int state
= 0;/* state bits, accessed thru macros */
2162 struct xfs_mount
*mp
= ip
->i_mount
;
2167 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
2168 if (whichfork
== XFS_COW_FORK
)
2169 state
|= BMAP_COWFORK
;
2172 ASSERT(*idx
<= xfs_iext_count(ifp
));
2173 ASSERT(!isnullstartblock(new->br_startblock
));
2175 XFS_STATS_INC(mp
, xs_add_exlist
);
2182 * Set up a bunch of variables to make the tests simpler.
2185 ep
= xfs_iext_get_ext(ifp
, *idx
);
2186 xfs_bmbt_get_all(ep
, &PREV
);
2187 newext
= new->br_state
;
2188 oldext
= (newext
== XFS_EXT_UNWRITTEN
) ?
2189 XFS_EXT_NORM
: XFS_EXT_UNWRITTEN
;
2190 ASSERT(PREV
.br_state
== oldext
);
2191 new_endoff
= new->br_startoff
+ new->br_blockcount
;
2192 ASSERT(PREV
.br_startoff
<= new->br_startoff
);
2193 ASSERT(PREV
.br_startoff
+ PREV
.br_blockcount
>= new_endoff
);
2196 * Set flags determining what part of the previous oldext allocation
2197 * extent is being replaced by a newext allocation.
2199 if (PREV
.br_startoff
== new->br_startoff
)
2200 state
|= BMAP_LEFT_FILLING
;
2201 if (PREV
.br_startoff
+ PREV
.br_blockcount
== new_endoff
)
2202 state
|= BMAP_RIGHT_FILLING
;
2205 * Check and set flags if this segment has a left neighbor.
2206 * Don't set contiguous if the combined extent would be too large.
2209 state
|= BMAP_LEFT_VALID
;
2210 xfs_iext_get_extent(ifp
, *idx
- 1, &LEFT
);
2212 if (isnullstartblock(LEFT
.br_startblock
))
2213 state
|= BMAP_LEFT_DELAY
;
2216 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
2217 LEFT
.br_startoff
+ LEFT
.br_blockcount
== new->br_startoff
&&
2218 LEFT
.br_startblock
+ LEFT
.br_blockcount
== new->br_startblock
&&
2219 LEFT
.br_state
== newext
&&
2220 LEFT
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
2221 state
|= BMAP_LEFT_CONTIG
;
2224 * Check and set flags if this segment has a right neighbor.
2225 * Don't set contiguous if the combined extent would be too large.
2226 * Also check for all-three-contiguous being too large.
2228 if (*idx
< xfs_iext_count(ifp
) - 1) {
2229 state
|= BMAP_RIGHT_VALID
;
2230 xfs_iext_get_extent(ifp
, *idx
+ 1, &RIGHT
);
2231 if (isnullstartblock(RIGHT
.br_startblock
))
2232 state
|= BMAP_RIGHT_DELAY
;
2235 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
2236 new_endoff
== RIGHT
.br_startoff
&&
2237 new->br_startblock
+ new->br_blockcount
== RIGHT
.br_startblock
&&
2238 newext
== RIGHT
.br_state
&&
2239 new->br_blockcount
+ RIGHT
.br_blockcount
<= MAXEXTLEN
&&
2240 ((state
& (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
2241 BMAP_RIGHT_FILLING
)) !=
2242 (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
2243 BMAP_RIGHT_FILLING
) ||
2244 LEFT
.br_blockcount
+ new->br_blockcount
+ RIGHT
.br_blockcount
2246 state
|= BMAP_RIGHT_CONTIG
;
2249 * Switch out based on the FILLING and CONTIG state bits.
2251 switch (state
& (BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
2252 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
)) {
2253 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
2254 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2256 * Setting all of a previous oldext extent to newext.
2257 * The left and right neighbors are both contiguous with new.
2261 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2262 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
),
2263 LEFT
.br_blockcount
+ PREV
.br_blockcount
+
2264 RIGHT
.br_blockcount
);
2265 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2267 xfs_iext_remove(ip
, *idx
+ 1, 2, state
);
2268 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2269 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 2);
2271 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2273 rval
= XFS_ILOG_CORE
;
2274 if ((error
= xfs_bmbt_lookup_eq(cur
, RIGHT
.br_startoff
,
2275 RIGHT
.br_startblock
,
2276 RIGHT
.br_blockcount
, &i
)))
2278 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2279 if ((error
= xfs_btree_delete(cur
, &i
)))
2281 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2282 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2284 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2285 if ((error
= xfs_btree_delete(cur
, &i
)))
2287 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2288 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2290 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2291 if ((error
= xfs_bmbt_update(cur
, LEFT
.br_startoff
,
2293 LEFT
.br_blockcount
+ PREV
.br_blockcount
+
2294 RIGHT
.br_blockcount
, LEFT
.br_state
)))
2299 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
2301 * Setting all of a previous oldext extent to newext.
2302 * The left neighbor is contiguous, the right is not.
2306 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2307 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
),
2308 LEFT
.br_blockcount
+ PREV
.br_blockcount
);
2309 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2311 xfs_iext_remove(ip
, *idx
+ 1, 1, state
);
2312 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2313 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
2315 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2317 rval
= XFS_ILOG_CORE
;
2318 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2319 PREV
.br_startblock
, PREV
.br_blockcount
,
2322 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2323 if ((error
= xfs_btree_delete(cur
, &i
)))
2325 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2326 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2328 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2329 if ((error
= xfs_bmbt_update(cur
, LEFT
.br_startoff
,
2331 LEFT
.br_blockcount
+ PREV
.br_blockcount
,
2337 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2339 * Setting all of a previous oldext extent to newext.
2340 * The right neighbor is contiguous, the left is not.
2342 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2343 xfs_bmbt_set_blockcount(ep
,
2344 PREV
.br_blockcount
+ RIGHT
.br_blockcount
);
2345 xfs_bmbt_set_state(ep
, newext
);
2346 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2347 xfs_iext_remove(ip
, *idx
+ 1, 1, state
);
2348 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2349 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
2351 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2353 rval
= XFS_ILOG_CORE
;
2354 if ((error
= xfs_bmbt_lookup_eq(cur
, RIGHT
.br_startoff
,
2355 RIGHT
.br_startblock
,
2356 RIGHT
.br_blockcount
, &i
)))
2358 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2359 if ((error
= xfs_btree_delete(cur
, &i
)))
2361 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2362 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2364 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2365 if ((error
= xfs_bmbt_update(cur
, new->br_startoff
,
2367 new->br_blockcount
+ RIGHT
.br_blockcount
,
2373 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
2375 * Setting all of a previous oldext extent to newext.
2376 * Neither the left nor right neighbors are contiguous with
2379 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2380 xfs_bmbt_set_state(ep
, newext
);
2381 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2384 rval
= XFS_ILOG_DEXT
;
2387 if ((error
= xfs_bmbt_lookup_eq(cur
, new->br_startoff
,
2388 new->br_startblock
, new->br_blockcount
,
2391 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2392 if ((error
= xfs_bmbt_update(cur
, new->br_startoff
,
2393 new->br_startblock
, new->br_blockcount
,
2399 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
:
2401 * Setting the first part of a previous oldext extent to newext.
2402 * The left neighbor is contiguous.
2404 trace_xfs_bmap_pre_update(ip
, *idx
- 1, state
, _THIS_IP_
);
2405 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
- 1),
2406 LEFT
.br_blockcount
+ new->br_blockcount
);
2407 xfs_bmbt_set_startoff(ep
,
2408 PREV
.br_startoff
+ new->br_blockcount
);
2409 trace_xfs_bmap_post_update(ip
, *idx
- 1, state
, _THIS_IP_
);
2411 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2412 xfs_bmbt_set_startblock(ep
,
2413 new->br_startblock
+ new->br_blockcount
);
2414 xfs_bmbt_set_blockcount(ep
,
2415 PREV
.br_blockcount
- new->br_blockcount
);
2416 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2421 rval
= XFS_ILOG_DEXT
;
2424 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2425 PREV
.br_startblock
, PREV
.br_blockcount
,
2428 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2429 if ((error
= xfs_bmbt_update(cur
,
2430 PREV
.br_startoff
+ new->br_blockcount
,
2431 PREV
.br_startblock
+ new->br_blockcount
,
2432 PREV
.br_blockcount
- new->br_blockcount
,
2435 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2437 error
= xfs_bmbt_update(cur
, LEFT
.br_startoff
,
2439 LEFT
.br_blockcount
+ new->br_blockcount
,
2446 case BMAP_LEFT_FILLING
:
2448 * Setting the first part of a previous oldext extent to newext.
2449 * The left neighbor is not contiguous.
2451 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2452 ASSERT(ep
&& xfs_bmbt_get_state(ep
) == oldext
);
2453 xfs_bmbt_set_startoff(ep
, new_endoff
);
2454 xfs_bmbt_set_blockcount(ep
,
2455 PREV
.br_blockcount
- new->br_blockcount
);
2456 xfs_bmbt_set_startblock(ep
,
2457 new->br_startblock
+ new->br_blockcount
);
2458 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2460 xfs_iext_insert(ip
, *idx
, 1, new, state
);
2461 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2462 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
2464 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2466 rval
= XFS_ILOG_CORE
;
2467 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2468 PREV
.br_startblock
, PREV
.br_blockcount
,
2471 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2472 if ((error
= xfs_bmbt_update(cur
,
2473 PREV
.br_startoff
+ new->br_blockcount
,
2474 PREV
.br_startblock
+ new->br_blockcount
,
2475 PREV
.br_blockcount
- new->br_blockcount
,
2478 cur
->bc_rec
.b
= *new;
2479 if ((error
= xfs_btree_insert(cur
, &i
)))
2481 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2485 case BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2487 * Setting the last part of a previous oldext extent to newext.
2488 * The right neighbor is contiguous with the new allocation.
2490 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2491 xfs_bmbt_set_blockcount(ep
,
2492 PREV
.br_blockcount
- new->br_blockcount
);
2493 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2497 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2498 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp
, *idx
),
2499 new->br_startoff
, new->br_startblock
,
2500 new->br_blockcount
+ RIGHT
.br_blockcount
, newext
);
2501 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2504 rval
= XFS_ILOG_DEXT
;
2507 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2509 PREV
.br_blockcount
, &i
)))
2511 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2512 if ((error
= xfs_bmbt_update(cur
, PREV
.br_startoff
,
2514 PREV
.br_blockcount
- new->br_blockcount
,
2517 if ((error
= xfs_btree_increment(cur
, 0, &i
)))
2519 if ((error
= xfs_bmbt_update(cur
, new->br_startoff
,
2521 new->br_blockcount
+ RIGHT
.br_blockcount
,
2527 case BMAP_RIGHT_FILLING
:
2529 * Setting the last part of a previous oldext extent to newext.
2530 * The right neighbor is not contiguous.
2532 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2533 xfs_bmbt_set_blockcount(ep
,
2534 PREV
.br_blockcount
- new->br_blockcount
);
2535 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2538 xfs_iext_insert(ip
, *idx
, 1, new, state
);
2540 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2541 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
2543 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2545 rval
= XFS_ILOG_CORE
;
2546 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2547 PREV
.br_startblock
, PREV
.br_blockcount
,
2550 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2551 if ((error
= xfs_bmbt_update(cur
, PREV
.br_startoff
,
2553 PREV
.br_blockcount
- new->br_blockcount
,
2556 if ((error
= xfs_bmbt_lookup_eq(cur
, new->br_startoff
,
2557 new->br_startblock
, new->br_blockcount
,
2560 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
2561 cur
->bc_rec
.b
.br_state
= new->br_state
;
2562 if ((error
= xfs_btree_insert(cur
, &i
)))
2564 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2570 * Setting the middle part of a previous oldext extent to
2571 * newext. Contiguity is impossible here.
2572 * One extent becomes three extents.
2574 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2575 xfs_bmbt_set_blockcount(ep
,
2576 new->br_startoff
- PREV
.br_startoff
);
2577 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2580 r
[1].br_startoff
= new_endoff
;
2581 r
[1].br_blockcount
=
2582 PREV
.br_startoff
+ PREV
.br_blockcount
- new_endoff
;
2583 r
[1].br_startblock
= new->br_startblock
+ new->br_blockcount
;
2584 r
[1].br_state
= oldext
;
2587 xfs_iext_insert(ip
, *idx
, 2, &r
[0], state
);
2589 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2590 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 2);
2592 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2594 rval
= XFS_ILOG_CORE
;
2595 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2596 PREV
.br_startblock
, PREV
.br_blockcount
,
2599 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2600 /* new right extent - oldext */
2601 if ((error
= xfs_bmbt_update(cur
, r
[1].br_startoff
,
2602 r
[1].br_startblock
, r
[1].br_blockcount
,
2605 /* new left extent - oldext */
2606 cur
->bc_rec
.b
= PREV
;
2607 cur
->bc_rec
.b
.br_blockcount
=
2608 new->br_startoff
- PREV
.br_startoff
;
2609 if ((error
= xfs_btree_insert(cur
, &i
)))
2611 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2613 * Reset the cursor to the position of the new extent
2614 * we are about to insert as we can't trust it after
2615 * the previous insert.
2617 if ((error
= xfs_bmbt_lookup_eq(cur
, new->br_startoff
,
2618 new->br_startblock
, new->br_blockcount
,
2621 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
2622 /* new middle extent - newext */
2623 cur
->bc_rec
.b
.br_state
= new->br_state
;
2624 if ((error
= xfs_btree_insert(cur
, &i
)))
2626 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2630 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2631 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2632 case BMAP_LEFT_FILLING
| BMAP_RIGHT_CONTIG
:
2633 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
2634 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2635 case BMAP_LEFT_CONTIG
:
2636 case BMAP_RIGHT_CONTIG
:
2638 * These cases are all impossible.
2643 /* update reverse mappings */
2644 error
= xfs_rmap_convert_extent(mp
, dfops
, ip
, whichfork
, new);
2648 /* convert to a btree if necessary */
2649 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
2650 int tmp_logflags
; /* partial log flag return val */
2652 ASSERT(cur
== NULL
);
2653 error
= xfs_bmap_extents_to_btree(tp
, ip
, first
, dfops
, &cur
,
2654 0, &tmp_logflags
, whichfork
);
2655 *logflagsp
|= tmp_logflags
;
2660 /* clear out the allocated field, done with it now in any case. */
2662 cur
->bc_private
.b
.allocated
= 0;
2666 xfs_bmap_check_leaf_extents(*curp
, ip
, whichfork
);
2676 * Convert a hole to a delayed allocation.
2679 xfs_bmap_add_extent_hole_delay(
2680 xfs_inode_t
*ip
, /* incore inode pointer */
2682 xfs_extnum_t
*idx
, /* extent number to update/insert */
2683 xfs_bmbt_irec_t
*new) /* new data to add to file extents */
2685 xfs_ifork_t
*ifp
; /* inode fork pointer */
2686 xfs_bmbt_irec_t left
; /* left neighbor extent entry */
2687 xfs_filblks_t newlen
=0; /* new indirect size */
2688 xfs_filblks_t oldlen
=0; /* old indirect size */
2689 xfs_bmbt_irec_t right
; /* right neighbor extent entry */
2690 int state
; /* state bits, accessed thru macros */
2691 xfs_filblks_t temp
; /* temp for indirect calculations */
2693 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
2695 if (whichfork
== XFS_COW_FORK
)
2696 state
|= BMAP_COWFORK
;
2697 ASSERT(isnullstartblock(new->br_startblock
));
2700 * Check and set flags if this segment has a left neighbor
2703 state
|= BMAP_LEFT_VALID
;
2704 xfs_iext_get_extent(ifp
, *idx
- 1, &left
);
2706 if (isnullstartblock(left
.br_startblock
))
2707 state
|= BMAP_LEFT_DELAY
;
2711 * Check and set flags if the current (right) segment exists.
2712 * If it doesn't exist, we're converting the hole at end-of-file.
2714 if (*idx
< xfs_iext_count(ifp
)) {
2715 state
|= BMAP_RIGHT_VALID
;
2716 xfs_iext_get_extent(ifp
, *idx
, &right
);
2718 if (isnullstartblock(right
.br_startblock
))
2719 state
|= BMAP_RIGHT_DELAY
;
2723 * Set contiguity flags on the left and right neighbors.
2724 * Don't let extents get too large, even if the pieces are contiguous.
2726 if ((state
& BMAP_LEFT_VALID
) && (state
& BMAP_LEFT_DELAY
) &&
2727 left
.br_startoff
+ left
.br_blockcount
== new->br_startoff
&&
2728 left
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
2729 state
|= BMAP_LEFT_CONTIG
;
2731 if ((state
& BMAP_RIGHT_VALID
) && (state
& BMAP_RIGHT_DELAY
) &&
2732 new->br_startoff
+ new->br_blockcount
== right
.br_startoff
&&
2733 new->br_blockcount
+ right
.br_blockcount
<= MAXEXTLEN
&&
2734 (!(state
& BMAP_LEFT_CONTIG
) ||
2735 (left
.br_blockcount
+ new->br_blockcount
+
2736 right
.br_blockcount
<= MAXEXTLEN
)))
2737 state
|= BMAP_RIGHT_CONTIG
;
2740 * Switch out based on the contiguity flags.
2742 switch (state
& (BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
)) {
2743 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2745 * New allocation is contiguous with delayed allocations
2746 * on the left and on the right.
2747 * Merge all three into a single extent record.
2750 temp
= left
.br_blockcount
+ new->br_blockcount
+
2751 right
.br_blockcount
;
2753 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2754 oldlen
= startblockval(left
.br_startblock
) +
2755 startblockval(new->br_startblock
) +
2756 startblockval(right
.br_startblock
);
2757 newlen
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
2759 left
.br_startblock
= nullstartblock(newlen
);
2760 left
.br_blockcount
= temp
;
2761 xfs_iext_update_extent(ifp
, *idx
, &left
);
2762 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2764 xfs_iext_remove(ip
, *idx
+ 1, 1, state
);
2767 case BMAP_LEFT_CONTIG
:
2769 * New allocation is contiguous with a delayed allocation
2771 * Merge the new allocation with the left neighbor.
2774 temp
= left
.br_blockcount
+ new->br_blockcount
;
2776 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2777 oldlen
= startblockval(left
.br_startblock
) +
2778 startblockval(new->br_startblock
);
2779 newlen
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
2781 left
.br_blockcount
= temp
;
2782 left
.br_startblock
= nullstartblock(newlen
);
2783 xfs_iext_update_extent(ifp
, *idx
, &left
);
2784 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2787 case BMAP_RIGHT_CONTIG
:
2789 * New allocation is contiguous with a delayed allocation
2791 * Merge the new allocation with the right neighbor.
2793 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2794 temp
= new->br_blockcount
+ right
.br_blockcount
;
2795 oldlen
= startblockval(new->br_startblock
) +
2796 startblockval(right
.br_startblock
);
2797 newlen
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
2799 right
.br_startoff
= new->br_startoff
;
2800 right
.br_startblock
= nullstartblock(newlen
);
2801 right
.br_blockcount
= temp
;
2802 xfs_iext_update_extent(ifp
, *idx
, &right
);
2803 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2808 * New allocation is not contiguous with another
2809 * delayed allocation.
2810 * Insert a new entry.
2812 oldlen
= newlen
= 0;
2813 xfs_iext_insert(ip
, *idx
, 1, new, state
);
2816 if (oldlen
!= newlen
) {
2817 ASSERT(oldlen
> newlen
);
2818 xfs_mod_fdblocks(ip
->i_mount
, (int64_t)(oldlen
- newlen
),
2821 * Nothing to do for disk quota accounting here.
2827 * Convert a hole to a real allocation.
2829 STATIC
int /* error */
2830 xfs_bmap_add_extent_hole_real(
2831 struct xfs_trans
*tp
,
2832 struct xfs_inode
*ip
,
2835 struct xfs_btree_cur
**curp
,
2836 struct xfs_bmbt_irec
*new,
2837 xfs_fsblock_t
*first
,
2838 struct xfs_defer_ops
*dfops
,
2841 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
2842 struct xfs_mount
*mp
= ip
->i_mount
;
2843 struct xfs_btree_cur
*cur
= *curp
;
2844 int error
; /* error return value */
2845 int i
; /* temp state */
2846 xfs_bmbt_irec_t left
; /* left neighbor extent entry */
2847 xfs_bmbt_irec_t right
; /* right neighbor extent entry */
2848 int rval
=0; /* return value (logging flags) */
2849 int state
; /* state bits, accessed thru macros */
2850 struct xfs_bmbt_irec old
;
2853 ASSERT(*idx
<= xfs_iext_count(ifp
));
2854 ASSERT(!isnullstartblock(new->br_startblock
));
2855 ASSERT(!cur
|| !(cur
->bc_private
.b
.flags
& XFS_BTCUR_BPRV_WASDEL
));
2857 XFS_STATS_INC(mp
, xs_add_exlist
);
2860 if (whichfork
== XFS_ATTR_FORK
)
2861 state
|= BMAP_ATTRFORK
;
2862 if (whichfork
== XFS_COW_FORK
)
2863 state
|= BMAP_COWFORK
;
2866 * Check and set flags if this segment has a left neighbor.
2869 state
|= BMAP_LEFT_VALID
;
2870 xfs_iext_get_extent(ifp
, *idx
- 1, &left
);
2871 if (isnullstartblock(left
.br_startblock
))
2872 state
|= BMAP_LEFT_DELAY
;
2876 * Check and set flags if this segment has a current value.
2877 * Not true if we're inserting into the "hole" at eof.
2879 if (*idx
< xfs_iext_count(ifp
)) {
2880 state
|= BMAP_RIGHT_VALID
;
2881 xfs_iext_get_extent(ifp
, *idx
, &right
);
2882 if (isnullstartblock(right
.br_startblock
))
2883 state
|= BMAP_RIGHT_DELAY
;
2887 * We're inserting a real allocation between "left" and "right".
2888 * Set the contiguity flags. Don't let extents get too large.
2890 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
2891 left
.br_startoff
+ left
.br_blockcount
== new->br_startoff
&&
2892 left
.br_startblock
+ left
.br_blockcount
== new->br_startblock
&&
2893 left
.br_state
== new->br_state
&&
2894 left
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
2895 state
|= BMAP_LEFT_CONTIG
;
2897 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
2898 new->br_startoff
+ new->br_blockcount
== right
.br_startoff
&&
2899 new->br_startblock
+ new->br_blockcount
== right
.br_startblock
&&
2900 new->br_state
== right
.br_state
&&
2901 new->br_blockcount
+ right
.br_blockcount
<= MAXEXTLEN
&&
2902 (!(state
& BMAP_LEFT_CONTIG
) ||
2903 left
.br_blockcount
+ new->br_blockcount
+
2904 right
.br_blockcount
<= MAXEXTLEN
))
2905 state
|= BMAP_RIGHT_CONTIG
;
2909 * Select which case we're in here, and implement it.
2911 switch (state
& (BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
)) {
2912 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2914 * New allocation is contiguous with real allocations on the
2915 * left and on the right.
2916 * Merge all three into a single extent record.
2919 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2920 left
.br_blockcount
+= new->br_blockcount
+ right
.br_blockcount
;
2921 xfs_iext_update_extent(ifp
, *idx
, &left
);
2922 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2924 xfs_iext_remove(ip
, *idx
+ 1, 1, state
);
2926 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2927 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
2929 rval
= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
2931 rval
= XFS_ILOG_CORE
;
2932 error
= xfs_bmbt_lookup_eq(cur
, right
.br_startoff
,
2933 right
.br_startblock
, right
.br_blockcount
,
2937 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2938 error
= xfs_btree_delete(cur
, &i
);
2941 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2942 error
= xfs_btree_decrement(cur
, 0, &i
);
2945 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2946 error
= xfs_bmbt_update(cur
, left
.br_startoff
,
2948 left
.br_blockcount
, left
.br_state
);
2954 case BMAP_LEFT_CONTIG
:
2956 * New allocation is contiguous with a real allocation
2958 * Merge the new allocation with the left neighbor.
2962 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2963 left
.br_blockcount
+= new->br_blockcount
;
2964 xfs_iext_update_extent(ifp
, *idx
, &left
);
2965 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2968 rval
= xfs_ilog_fext(whichfork
);
2971 error
= xfs_bmbt_lookup_eq(cur
, old
.br_startoff
,
2972 old
.br_startblock
, old
.br_blockcount
,
2976 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2977 error
= xfs_bmbt_update(cur
, left
.br_startoff
,
2979 left
.br_blockcount
, left
.br_state
);
2985 case BMAP_RIGHT_CONTIG
:
2987 * New allocation is contiguous with a real allocation
2989 * Merge the new allocation with the right neighbor.
2992 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2993 right
.br_startoff
= new->br_startoff
;
2994 right
.br_startblock
= new->br_startblock
;
2995 right
.br_blockcount
+= new->br_blockcount
;
2996 xfs_iext_update_extent(ifp
, *idx
, &right
);
2997 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
3000 rval
= xfs_ilog_fext(whichfork
);
3003 error
= xfs_bmbt_lookup_eq(cur
, old
.br_startoff
,
3004 old
.br_startblock
, old
.br_blockcount
,
3008 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
3009 error
= xfs_bmbt_update(cur
, right
.br_startoff
,
3010 right
.br_startblock
,
3011 right
.br_blockcount
, right
.br_state
);
3019 * New allocation is not contiguous with another
3021 * Insert a new entry.
3023 xfs_iext_insert(ip
, *idx
, 1, new, state
);
3024 XFS_IFORK_NEXT_SET(ip
, whichfork
,
3025 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
3027 rval
= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
3029 rval
= XFS_ILOG_CORE
;
3030 error
= xfs_bmbt_lookup_eq(cur
,
3033 new->br_blockcount
, &i
);
3036 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
3037 cur
->bc_rec
.b
.br_state
= new->br_state
;
3038 error
= xfs_btree_insert(cur
, &i
);
3041 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
3046 /* add reverse mapping */
3047 error
= xfs_rmap_map_extent(mp
, dfops
, ip
, whichfork
, new);
3051 /* convert to a btree if necessary */
3052 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
3053 int tmp_logflags
; /* partial log flag return val */
3055 ASSERT(cur
== NULL
);
3056 error
= xfs_bmap_extents_to_btree(tp
, ip
, first
, dfops
, curp
,
3057 0, &tmp_logflags
, whichfork
);
3058 *logflagsp
|= tmp_logflags
;
3064 /* clear out the allocated field, done with it now in any case. */
3066 cur
->bc_private
.b
.allocated
= 0;
3068 xfs_bmap_check_leaf_extents(cur
, ip
, whichfork
);
3075 * Functions used in the extent read, allocate and remove paths
3079 * Adjust the size of the new extent based on di_extsize and rt extsize.
3082 xfs_bmap_extsize_align(
3084 xfs_bmbt_irec_t
*gotp
, /* next extent pointer */
3085 xfs_bmbt_irec_t
*prevp
, /* previous extent pointer */
3086 xfs_extlen_t extsz
, /* align to this extent size */
3087 int rt
, /* is this a realtime inode? */
3088 int eof
, /* is extent at end-of-file? */
3089 int delay
, /* creating delalloc extent? */
3090 int convert
, /* overwriting unwritten extent? */
3091 xfs_fileoff_t
*offp
, /* in/out: aligned offset */
3092 xfs_extlen_t
*lenp
) /* in/out: aligned length */
3094 xfs_fileoff_t orig_off
; /* original offset */
3095 xfs_extlen_t orig_alen
; /* original length */
3096 xfs_fileoff_t orig_end
; /* original off+len */
3097 xfs_fileoff_t nexto
; /* next file offset */
3098 xfs_fileoff_t prevo
; /* previous file offset */
3099 xfs_fileoff_t align_off
; /* temp for offset */
3100 xfs_extlen_t align_alen
; /* temp for length */
3101 xfs_extlen_t temp
; /* temp for calculations */
3106 orig_off
= align_off
= *offp
;
3107 orig_alen
= align_alen
= *lenp
;
3108 orig_end
= orig_off
+ orig_alen
;
3111 * If this request overlaps an existing extent, then don't
3112 * attempt to perform any additional alignment.
3114 if (!delay
&& !eof
&&
3115 (orig_off
>= gotp
->br_startoff
) &&
3116 (orig_end
<= gotp
->br_startoff
+ gotp
->br_blockcount
)) {
3121 * If the file offset is unaligned vs. the extent size
3122 * we need to align it. This will be possible unless
3123 * the file was previously written with a kernel that didn't
3124 * perform this alignment, or if a truncate shot us in the
3127 temp
= do_mod(orig_off
, extsz
);
3133 /* Same adjustment for the end of the requested area. */
3134 temp
= (align_alen
% extsz
);
3136 align_alen
+= extsz
- temp
;
3139 * For large extent hint sizes, the aligned extent might be larger than
3140 * MAXEXTLEN. In that case, reduce the size by an extsz so that it pulls
3141 * the length back under MAXEXTLEN. The outer allocation loops handle
3142 * short allocation just fine, so it is safe to do this. We only want to
3143 * do it when we are forced to, though, because it means more allocation
3144 * operations are required.
3146 while (align_alen
> MAXEXTLEN
)
3147 align_alen
-= extsz
;
3148 ASSERT(align_alen
<= MAXEXTLEN
);
3151 * If the previous block overlaps with this proposed allocation
3152 * then move the start forward without adjusting the length.
3154 if (prevp
->br_startoff
!= NULLFILEOFF
) {
3155 if (prevp
->br_startblock
== HOLESTARTBLOCK
)
3156 prevo
= prevp
->br_startoff
;
3158 prevo
= prevp
->br_startoff
+ prevp
->br_blockcount
;
3161 if (align_off
!= orig_off
&& align_off
< prevo
)
3164 * If the next block overlaps with this proposed allocation
3165 * then move the start back without adjusting the length,
3166 * but not before offset 0.
3167 * This may of course make the start overlap previous block,
3168 * and if we hit the offset 0 limit then the next block
3169 * can still overlap too.
3171 if (!eof
&& gotp
->br_startoff
!= NULLFILEOFF
) {
3172 if ((delay
&& gotp
->br_startblock
== HOLESTARTBLOCK
) ||
3173 (!delay
&& gotp
->br_startblock
== DELAYSTARTBLOCK
))
3174 nexto
= gotp
->br_startoff
+ gotp
->br_blockcount
;
3176 nexto
= gotp
->br_startoff
;
3178 nexto
= NULLFILEOFF
;
3180 align_off
+ align_alen
!= orig_end
&&
3181 align_off
+ align_alen
> nexto
)
3182 align_off
= nexto
> align_alen
? nexto
- align_alen
: 0;
3184 * If we're now overlapping the next or previous extent that
3185 * means we can't fit an extsz piece in this hole. Just move
3186 * the start forward to the first valid spot and set
3187 * the length so we hit the end.
3189 if (align_off
!= orig_off
&& align_off
< prevo
)
3191 if (align_off
+ align_alen
!= orig_end
&&
3192 align_off
+ align_alen
> nexto
&&
3193 nexto
!= NULLFILEOFF
) {
3194 ASSERT(nexto
> prevo
);
3195 align_alen
= nexto
- align_off
;
3199 * If realtime, and the result isn't a multiple of the realtime
3200 * extent size we need to remove blocks until it is.
3202 if (rt
&& (temp
= (align_alen
% mp
->m_sb
.sb_rextsize
))) {
3204 * We're not covering the original request, or
3205 * we won't be able to once we fix the length.
3207 if (orig_off
< align_off
||
3208 orig_end
> align_off
+ align_alen
||
3209 align_alen
- temp
< orig_alen
)
3212 * Try to fix it by moving the start up.
3214 if (align_off
+ temp
<= orig_off
) {
3219 * Try to fix it by moving the end in.
3221 else if (align_off
+ align_alen
- temp
>= orig_end
)
3224 * Set the start to the minimum then trim the length.
3227 align_alen
-= orig_off
- align_off
;
3228 align_off
= orig_off
;
3229 align_alen
-= align_alen
% mp
->m_sb
.sb_rextsize
;
3232 * Result doesn't cover the request, fail it.
3234 if (orig_off
< align_off
|| orig_end
> align_off
+ align_alen
)
3237 ASSERT(orig_off
>= align_off
);
3238 /* see MAXEXTLEN handling above */
3239 ASSERT(orig_end
<= align_off
+ align_alen
||
3240 align_alen
+ extsz
> MAXEXTLEN
);
3244 if (!eof
&& gotp
->br_startoff
!= NULLFILEOFF
)
3245 ASSERT(align_off
+ align_alen
<= gotp
->br_startoff
);
3246 if (prevp
->br_startoff
!= NULLFILEOFF
)
3247 ASSERT(align_off
>= prevp
->br_startoff
+ prevp
->br_blockcount
);
3255 #define XFS_ALLOC_GAP_UNITS 4
3259 struct xfs_bmalloca
*ap
) /* bmap alloc argument struct */
3261 xfs_fsblock_t adjust
; /* adjustment to block numbers */
3262 xfs_agnumber_t fb_agno
; /* ag number of ap->firstblock */
3263 xfs_mount_t
*mp
; /* mount point structure */
3264 int nullfb
; /* true if ap->firstblock isn't set */
3265 int rt
; /* true if inode is realtime */
3267 #define ISVALID(x,y) \
3269 (x) < mp->m_sb.sb_rblocks : \
3270 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
3271 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
3272 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
3274 mp
= ap
->ip
->i_mount
;
3275 nullfb
= *ap
->firstblock
== NULLFSBLOCK
;
3276 rt
= XFS_IS_REALTIME_INODE(ap
->ip
) &&
3277 xfs_alloc_is_userdata(ap
->datatype
);
3278 fb_agno
= nullfb
? NULLAGNUMBER
: XFS_FSB_TO_AGNO(mp
, *ap
->firstblock
);
3280 * If allocating at eof, and there's a previous real block,
3281 * try to use its last block as our starting point.
3283 if (ap
->eof
&& ap
->prev
.br_startoff
!= NULLFILEOFF
&&
3284 !isnullstartblock(ap
->prev
.br_startblock
) &&
3285 ISVALID(ap
->prev
.br_startblock
+ ap
->prev
.br_blockcount
,
3286 ap
->prev
.br_startblock
)) {
3287 ap
->blkno
= ap
->prev
.br_startblock
+ ap
->prev
.br_blockcount
;
3289 * Adjust for the gap between prevp and us.
3291 adjust
= ap
->offset
-
3292 (ap
->prev
.br_startoff
+ ap
->prev
.br_blockcount
);
3294 ISVALID(ap
->blkno
+ adjust
, ap
->prev
.br_startblock
))
3295 ap
->blkno
+= adjust
;
3298 * If not at eof, then compare the two neighbor blocks.
3299 * Figure out whether either one gives us a good starting point,
3300 * and pick the better one.
3302 else if (!ap
->eof
) {
3303 xfs_fsblock_t gotbno
; /* right side block number */
3304 xfs_fsblock_t gotdiff
=0; /* right side difference */
3305 xfs_fsblock_t prevbno
; /* left side block number */
3306 xfs_fsblock_t prevdiff
=0; /* left side difference */
3309 * If there's a previous (left) block, select a requested
3310 * start block based on it.
3312 if (ap
->prev
.br_startoff
!= NULLFILEOFF
&&
3313 !isnullstartblock(ap
->prev
.br_startblock
) &&
3314 (prevbno
= ap
->prev
.br_startblock
+
3315 ap
->prev
.br_blockcount
) &&
3316 ISVALID(prevbno
, ap
->prev
.br_startblock
)) {
3318 * Calculate gap to end of previous block.
3320 adjust
= prevdiff
= ap
->offset
-
3321 (ap
->prev
.br_startoff
+
3322 ap
->prev
.br_blockcount
);
3324 * Figure the startblock based on the previous block's
3325 * end and the gap size.
3327 * If the gap is large relative to the piece we're
3328 * allocating, or using it gives us an invalid block
3329 * number, then just use the end of the previous block.
3331 if (prevdiff
<= XFS_ALLOC_GAP_UNITS
* ap
->length
&&
3332 ISVALID(prevbno
+ prevdiff
,
3333 ap
->prev
.br_startblock
))
3338 * If the firstblock forbids it, can't use it,
3341 if (!rt
&& !nullfb
&&
3342 XFS_FSB_TO_AGNO(mp
, prevbno
) != fb_agno
)
3343 prevbno
= NULLFSBLOCK
;
3346 * No previous block or can't follow it, just default.
3349 prevbno
= NULLFSBLOCK
;
3351 * If there's a following (right) block, select a requested
3352 * start block based on it.
3354 if (!isnullstartblock(ap
->got
.br_startblock
)) {
3356 * Calculate gap to start of next block.
3358 adjust
= gotdiff
= ap
->got
.br_startoff
- ap
->offset
;
3360 * Figure the startblock based on the next block's
3361 * start and the gap size.
3363 gotbno
= ap
->got
.br_startblock
;
3366 * If the gap is large relative to the piece we're
3367 * allocating, or using it gives us an invalid block
3368 * number, then just use the start of the next block
3369 * offset by our length.
3371 if (gotdiff
<= XFS_ALLOC_GAP_UNITS
* ap
->length
&&
3372 ISVALID(gotbno
- gotdiff
, gotbno
))
3374 else if (ISVALID(gotbno
- ap
->length
, gotbno
)) {
3375 gotbno
-= ap
->length
;
3376 gotdiff
+= adjust
- ap
->length
;
3380 * If the firstblock forbids it, can't use it,
3383 if (!rt
&& !nullfb
&&
3384 XFS_FSB_TO_AGNO(mp
, gotbno
) != fb_agno
)
3385 gotbno
= NULLFSBLOCK
;
3388 * No next block, just default.
3391 gotbno
= NULLFSBLOCK
;
3393 * If both valid, pick the better one, else the only good
3394 * one, else ap->blkno is already set (to 0 or the inode block).
3396 if (prevbno
!= NULLFSBLOCK
&& gotbno
!= NULLFSBLOCK
)
3397 ap
->blkno
= prevdiff
<= gotdiff
? prevbno
: gotbno
;
3398 else if (prevbno
!= NULLFSBLOCK
)
3399 ap
->blkno
= prevbno
;
3400 else if (gotbno
!= NULLFSBLOCK
)
3407 xfs_bmap_longest_free_extent(
3408 struct xfs_trans
*tp
,
3413 struct xfs_mount
*mp
= tp
->t_mountp
;
3414 struct xfs_perag
*pag
;
3415 xfs_extlen_t longest
;
3418 pag
= xfs_perag_get(mp
, ag
);
3419 if (!pag
->pagf_init
) {
3420 error
= xfs_alloc_pagf_init(mp
, tp
, ag
, XFS_ALLOC_FLAG_TRYLOCK
);
3424 if (!pag
->pagf_init
) {
3430 longest
= xfs_alloc_longest_free_extent(mp
, pag
,
3431 xfs_alloc_min_freelist(mp
, pag
),
3432 xfs_ag_resv_needed(pag
, XFS_AG_RESV_NONE
));
3433 if (*blen
< longest
)
3442 xfs_bmap_select_minlen(
3443 struct xfs_bmalloca
*ap
,
3444 struct xfs_alloc_arg
*args
,
3448 if (notinit
|| *blen
< ap
->minlen
) {
3450 * Since we did a BUF_TRYLOCK above, it is possible that
3451 * there is space for this request.
3453 args
->minlen
= ap
->minlen
;
3454 } else if (*blen
< args
->maxlen
) {
3456 * If the best seen length is less than the request length,
3457 * use the best as the minimum.
3459 args
->minlen
= *blen
;
3462 * Otherwise we've seen an extent as big as maxlen, use that
3465 args
->minlen
= args
->maxlen
;
3470 xfs_bmap_btalloc_nullfb(
3471 struct xfs_bmalloca
*ap
,
3472 struct xfs_alloc_arg
*args
,
3475 struct xfs_mount
*mp
= ap
->ip
->i_mount
;
3476 xfs_agnumber_t ag
, startag
;
3480 args
->type
= XFS_ALLOCTYPE_START_BNO
;
3481 args
->total
= ap
->total
;
3483 startag
= ag
= XFS_FSB_TO_AGNO(mp
, args
->fsbno
);
3484 if (startag
== NULLAGNUMBER
)
3487 while (*blen
< args
->maxlen
) {
3488 error
= xfs_bmap_longest_free_extent(args
->tp
, ag
, blen
,
3493 if (++ag
== mp
->m_sb
.sb_agcount
)
3499 xfs_bmap_select_minlen(ap
, args
, blen
, notinit
);
3504 xfs_bmap_btalloc_filestreams(
3505 struct xfs_bmalloca
*ap
,
3506 struct xfs_alloc_arg
*args
,
3509 struct xfs_mount
*mp
= ap
->ip
->i_mount
;
3514 args
->type
= XFS_ALLOCTYPE_NEAR_BNO
;
3515 args
->total
= ap
->total
;
3517 ag
= XFS_FSB_TO_AGNO(mp
, args
->fsbno
);
3518 if (ag
== NULLAGNUMBER
)
3521 error
= xfs_bmap_longest_free_extent(args
->tp
, ag
, blen
, ¬init
);
3525 if (*blen
< args
->maxlen
) {
3526 error
= xfs_filestream_new_ag(ap
, &ag
);
3530 error
= xfs_bmap_longest_free_extent(args
->tp
, ag
, blen
,
3537 xfs_bmap_select_minlen(ap
, args
, blen
, notinit
);
3540 * Set the failure fallback case to look in the selected AG as stream
3543 ap
->blkno
= args
->fsbno
= XFS_AGB_TO_FSB(mp
, ag
, 0);
3549 struct xfs_bmalloca
*ap
) /* bmap alloc argument struct */
3551 xfs_mount_t
*mp
; /* mount point structure */
3552 xfs_alloctype_t atype
= 0; /* type for allocation routines */
3553 xfs_extlen_t align
= 0; /* minimum allocation alignment */
3554 xfs_agnumber_t fb_agno
; /* ag number of ap->firstblock */
3556 xfs_alloc_arg_t args
;
3558 xfs_extlen_t nextminlen
= 0;
3559 int nullfb
; /* true if ap->firstblock isn't set */
3567 mp
= ap
->ip
->i_mount
;
3569 /* stripe alignment for allocation is determined by mount parameters */
3571 if (mp
->m_swidth
&& (mp
->m_flags
& XFS_MOUNT_SWALLOC
))
3572 stripe_align
= mp
->m_swidth
;
3573 else if (mp
->m_dalign
)
3574 stripe_align
= mp
->m_dalign
;
3576 if (ap
->flags
& XFS_BMAPI_COWFORK
)
3577 align
= xfs_get_cowextsz_hint(ap
->ip
);
3578 else if (xfs_alloc_is_userdata(ap
->datatype
))
3579 align
= xfs_get_extsz_hint(ap
->ip
);
3581 error
= xfs_bmap_extsize_align(mp
, &ap
->got
, &ap
->prev
,
3582 align
, 0, ap
->eof
, 0, ap
->conv
,
3583 &ap
->offset
, &ap
->length
);
3589 nullfb
= *ap
->firstblock
== NULLFSBLOCK
;
3590 fb_agno
= nullfb
? NULLAGNUMBER
: XFS_FSB_TO_AGNO(mp
, *ap
->firstblock
);
3592 if (xfs_alloc_is_userdata(ap
->datatype
) &&
3593 xfs_inode_is_filestream(ap
->ip
)) {
3594 ag
= xfs_filestream_lookup_ag(ap
->ip
);
3595 ag
= (ag
!= NULLAGNUMBER
) ? ag
: 0;
3596 ap
->blkno
= XFS_AGB_TO_FSB(mp
, ag
, 0);
3598 ap
->blkno
= XFS_INO_TO_FSB(mp
, ap
->ip
->i_ino
);
3601 ap
->blkno
= *ap
->firstblock
;
3603 xfs_bmap_adjacent(ap
);
3606 * If allowed, use ap->blkno; otherwise must use firstblock since
3607 * it's in the right allocation group.
3609 if (nullfb
|| XFS_FSB_TO_AGNO(mp
, ap
->blkno
) == fb_agno
)
3612 ap
->blkno
= *ap
->firstblock
;
3614 * Normal allocation, done through xfs_alloc_vextent.
3616 tryagain
= isaligned
= 0;
3617 memset(&args
, 0, sizeof(args
));
3620 args
.fsbno
= ap
->blkno
;
3621 xfs_rmap_skip_owner_update(&args
.oinfo
);
3623 /* Trim the allocation back to the maximum an AG can fit. */
3624 args
.maxlen
= MIN(ap
->length
, mp
->m_ag_max_usable
);
3625 args
.firstblock
= *ap
->firstblock
;
3629 * Search for an allocation group with a single extent large
3630 * enough for the request. If one isn't found, then adjust
3631 * the minimum allocation size to the largest space found.
3633 if (xfs_alloc_is_userdata(ap
->datatype
) &&
3634 xfs_inode_is_filestream(ap
->ip
))
3635 error
= xfs_bmap_btalloc_filestreams(ap
, &args
, &blen
);
3637 error
= xfs_bmap_btalloc_nullfb(ap
, &args
, &blen
);
3640 } else if (ap
->dfops
->dop_low
) {
3641 if (xfs_inode_is_filestream(ap
->ip
))
3642 args
.type
= XFS_ALLOCTYPE_FIRST_AG
;
3644 args
.type
= XFS_ALLOCTYPE_START_BNO
;
3645 args
.total
= args
.minlen
= ap
->minlen
;
3647 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
3648 args
.total
= ap
->total
;
3649 args
.minlen
= ap
->minlen
;
3651 /* apply extent size hints if obtained earlier */
3654 if ((args
.mod
= (xfs_extlen_t
)do_mod(ap
->offset
, args
.prod
)))
3655 args
.mod
= (xfs_extlen_t
)(args
.prod
- args
.mod
);
3656 } else if (mp
->m_sb
.sb_blocksize
>= PAGE_SIZE
) {
3660 args
.prod
= PAGE_SIZE
>> mp
->m_sb
.sb_blocklog
;
3661 if ((args
.mod
= (xfs_extlen_t
)(do_mod(ap
->offset
, args
.prod
))))
3662 args
.mod
= (xfs_extlen_t
)(args
.prod
- args
.mod
);
3665 * If we are not low on available data blocks, and the
3666 * underlying logical volume manager is a stripe, and
3667 * the file offset is zero then try to allocate data
3668 * blocks on stripe unit boundary.
3669 * NOTE: ap->aeof is only set if the allocation length
3670 * is >= the stripe unit and the allocation offset is
3671 * at the end of file.
3673 if (!ap
->dfops
->dop_low
&& ap
->aeof
) {
3675 args
.alignment
= stripe_align
;
3679 * Adjust for alignment
3681 if (blen
> args
.alignment
&& blen
<= args
.maxlen
)
3682 args
.minlen
= blen
- args
.alignment
;
3683 args
.minalignslop
= 0;
3686 * First try an exact bno allocation.
3687 * If it fails then do a near or start bno
3688 * allocation with alignment turned on.
3692 args
.type
= XFS_ALLOCTYPE_THIS_BNO
;
3695 * Compute the minlen+alignment for the
3696 * next case. Set slop so that the value
3697 * of minlen+alignment+slop doesn't go up
3698 * between the calls.
3700 if (blen
> stripe_align
&& blen
<= args
.maxlen
)
3701 nextminlen
= blen
- stripe_align
;
3703 nextminlen
= args
.minlen
;
3704 if (nextminlen
+ stripe_align
> args
.minlen
+ 1)
3706 nextminlen
+ stripe_align
-
3709 args
.minalignslop
= 0;
3713 args
.minalignslop
= 0;
3715 args
.minleft
= ap
->minleft
;
3716 args
.wasdel
= ap
->wasdel
;
3717 args
.resv
= XFS_AG_RESV_NONE
;
3718 args
.datatype
= ap
->datatype
;
3719 if (ap
->datatype
& XFS_ALLOC_USERDATA_ZERO
)
3722 error
= xfs_alloc_vextent(&args
);
3726 if (tryagain
&& args
.fsbno
== NULLFSBLOCK
) {
3728 * Exact allocation failed. Now try with alignment
3732 args
.fsbno
= ap
->blkno
;
3733 args
.alignment
= stripe_align
;
3734 args
.minlen
= nextminlen
;
3735 args
.minalignslop
= 0;
3737 if ((error
= xfs_alloc_vextent(&args
)))
3740 if (isaligned
&& args
.fsbno
== NULLFSBLOCK
) {
3742 * allocation failed, so turn off alignment and
3746 args
.fsbno
= ap
->blkno
;
3748 if ((error
= xfs_alloc_vextent(&args
)))
3751 if (args
.fsbno
== NULLFSBLOCK
&& nullfb
&&
3752 args
.minlen
> ap
->minlen
) {
3753 args
.minlen
= ap
->minlen
;
3754 args
.type
= XFS_ALLOCTYPE_START_BNO
;
3755 args
.fsbno
= ap
->blkno
;
3756 if ((error
= xfs_alloc_vextent(&args
)))
3759 if (args
.fsbno
== NULLFSBLOCK
&& nullfb
) {
3761 args
.type
= XFS_ALLOCTYPE_FIRST_AG
;
3762 args
.total
= ap
->minlen
;
3763 if ((error
= xfs_alloc_vextent(&args
)))
3765 ap
->dfops
->dop_low
= true;
3767 if (args
.fsbno
!= NULLFSBLOCK
) {
3769 * check the allocation happened at the same or higher AG than
3770 * the first block that was allocated.
3772 ASSERT(*ap
->firstblock
== NULLFSBLOCK
||
3773 XFS_FSB_TO_AGNO(mp
, *ap
->firstblock
) <=
3774 XFS_FSB_TO_AGNO(mp
, args
.fsbno
));
3776 ap
->blkno
= args
.fsbno
;
3777 if (*ap
->firstblock
== NULLFSBLOCK
)
3778 *ap
->firstblock
= args
.fsbno
;
3779 ASSERT(nullfb
|| fb_agno
<= args
.agno
);
3780 ap
->length
= args
.len
;
3781 if (!(ap
->flags
& XFS_BMAPI_COWFORK
))
3782 ap
->ip
->i_d
.di_nblocks
+= args
.len
;
3783 xfs_trans_log_inode(ap
->tp
, ap
->ip
, XFS_ILOG_CORE
);
3785 ap
->ip
->i_delayed_blks
-= args
.len
;
3787 * Adjust the disk quota also. This was reserved
3790 xfs_trans_mod_dquot_byino(ap
->tp
, ap
->ip
,
3791 ap
->wasdel
? XFS_TRANS_DQ_DELBCOUNT
:
3792 XFS_TRANS_DQ_BCOUNT
,
3795 ap
->blkno
= NULLFSBLOCK
;
3802 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
3803 * It figures out where to ask the underlying allocator to put the new extent.
3807 struct xfs_bmalloca
*ap
) /* bmap alloc argument struct */
3809 if (XFS_IS_REALTIME_INODE(ap
->ip
) &&
3810 xfs_alloc_is_userdata(ap
->datatype
))
3811 return xfs_bmap_rtalloc(ap
);
3812 return xfs_bmap_btalloc(ap
);
3815 /* Trim extent to fit a logical block range. */
3818 struct xfs_bmbt_irec
*irec
,
3822 xfs_fileoff_t distance
;
3823 xfs_fileoff_t end
= bno
+ len
;
3825 if (irec
->br_startoff
+ irec
->br_blockcount
<= bno
||
3826 irec
->br_startoff
>= end
) {
3827 irec
->br_blockcount
= 0;
3831 if (irec
->br_startoff
< bno
) {
3832 distance
= bno
- irec
->br_startoff
;
3833 if (isnullstartblock(irec
->br_startblock
))
3834 irec
->br_startblock
= DELAYSTARTBLOCK
;
3835 if (irec
->br_startblock
!= DELAYSTARTBLOCK
&&
3836 irec
->br_startblock
!= HOLESTARTBLOCK
)
3837 irec
->br_startblock
+= distance
;
3838 irec
->br_startoff
+= distance
;
3839 irec
->br_blockcount
-= distance
;
3842 if (end
< irec
->br_startoff
+ irec
->br_blockcount
) {
3843 distance
= irec
->br_startoff
+ irec
->br_blockcount
- end
;
3844 irec
->br_blockcount
-= distance
;
3848 /* trim extent to within eof */
3850 xfs_trim_extent_eof(
3851 struct xfs_bmbt_irec
*irec
,
3852 struct xfs_inode
*ip
)
3855 xfs_trim_extent(irec
, 0, XFS_B_TO_FSB(ip
->i_mount
,
3856 i_size_read(VFS_I(ip
))));
3860 * Trim the returned map to the required bounds
3864 struct xfs_bmbt_irec
*mval
,
3865 struct xfs_bmbt_irec
*got
,
3873 if ((flags
& XFS_BMAPI_ENTIRE
) ||
3874 got
->br_startoff
+ got
->br_blockcount
<= obno
) {
3876 if (isnullstartblock(got
->br_startblock
))
3877 mval
->br_startblock
= DELAYSTARTBLOCK
;
3883 ASSERT((*bno
>= obno
) || (n
== 0));
3885 mval
->br_startoff
= *bno
;
3886 if (isnullstartblock(got
->br_startblock
))
3887 mval
->br_startblock
= DELAYSTARTBLOCK
;
3889 mval
->br_startblock
= got
->br_startblock
+
3890 (*bno
- got
->br_startoff
);
3892 * Return the minimum of what we got and what we asked for for
3893 * the length. We can use the len variable here because it is
3894 * modified below and we could have been there before coming
3895 * here if the first part of the allocation didn't overlap what
3898 mval
->br_blockcount
= XFS_FILBLKS_MIN(end
- *bno
,
3899 got
->br_blockcount
- (*bno
- got
->br_startoff
));
3900 mval
->br_state
= got
->br_state
;
3901 ASSERT(mval
->br_blockcount
<= len
);
3906 * Update and validate the extent map to return
3909 xfs_bmapi_update_map(
3910 struct xfs_bmbt_irec
**map
,
3918 xfs_bmbt_irec_t
*mval
= *map
;
3920 ASSERT((flags
& XFS_BMAPI_ENTIRE
) ||
3921 ((mval
->br_startoff
+ mval
->br_blockcount
) <= end
));
3922 ASSERT((flags
& XFS_BMAPI_ENTIRE
) || (mval
->br_blockcount
<= *len
) ||
3923 (mval
->br_startoff
< obno
));
3925 *bno
= mval
->br_startoff
+ mval
->br_blockcount
;
3927 if (*n
> 0 && mval
->br_startoff
== mval
[-1].br_startoff
) {
3928 /* update previous map with new information */
3929 ASSERT(mval
->br_startblock
== mval
[-1].br_startblock
);
3930 ASSERT(mval
->br_blockcount
> mval
[-1].br_blockcount
);
3931 ASSERT(mval
->br_state
== mval
[-1].br_state
);
3932 mval
[-1].br_blockcount
= mval
->br_blockcount
;
3933 mval
[-1].br_state
= mval
->br_state
;
3934 } else if (*n
> 0 && mval
->br_startblock
!= DELAYSTARTBLOCK
&&
3935 mval
[-1].br_startblock
!= DELAYSTARTBLOCK
&&
3936 mval
[-1].br_startblock
!= HOLESTARTBLOCK
&&
3937 mval
->br_startblock
== mval
[-1].br_startblock
+
3938 mval
[-1].br_blockcount
&&
3939 ((flags
& XFS_BMAPI_IGSTATE
) ||
3940 mval
[-1].br_state
== mval
->br_state
)) {
3941 ASSERT(mval
->br_startoff
==
3942 mval
[-1].br_startoff
+ mval
[-1].br_blockcount
);
3943 mval
[-1].br_blockcount
+= mval
->br_blockcount
;
3944 } else if (*n
> 0 &&
3945 mval
->br_startblock
== DELAYSTARTBLOCK
&&
3946 mval
[-1].br_startblock
== DELAYSTARTBLOCK
&&
3947 mval
->br_startoff
==
3948 mval
[-1].br_startoff
+ mval
[-1].br_blockcount
) {
3949 mval
[-1].br_blockcount
+= mval
->br_blockcount
;
3950 mval
[-1].br_state
= mval
->br_state
;
3951 } else if (!((*n
== 0) &&
3952 ((mval
->br_startoff
+ mval
->br_blockcount
) <=
3961 * Map file blocks to filesystem blocks without allocation.
3965 struct xfs_inode
*ip
,
3968 struct xfs_bmbt_irec
*mval
,
3972 struct xfs_mount
*mp
= ip
->i_mount
;
3973 struct xfs_ifork
*ifp
;
3974 struct xfs_bmbt_irec got
;
3981 int whichfork
= xfs_bmapi_whichfork(flags
);
3984 ASSERT(!(flags
& ~(XFS_BMAPI_ATTRFORK
|XFS_BMAPI_ENTIRE
|
3985 XFS_BMAPI_IGSTATE
|XFS_BMAPI_COWFORK
)));
3986 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_SHARED
|XFS_ILOCK_EXCL
));
3988 if (unlikely(XFS_TEST_ERROR(
3989 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
3990 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
3991 mp
, XFS_ERRTAG_BMAPIFORMAT
))) {
3992 XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW
, mp
);
3993 return -EFSCORRUPTED
;
3996 if (XFS_FORCED_SHUTDOWN(mp
))
3999 XFS_STATS_INC(mp
, xs_blk_mapr
);
4001 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4003 /* No CoW fork? Return a hole. */
4004 if (whichfork
== XFS_COW_FORK
&& !ifp
) {
4005 mval
->br_startoff
= bno
;
4006 mval
->br_startblock
= HOLESTARTBLOCK
;
4007 mval
->br_blockcount
= len
;
4008 mval
->br_state
= XFS_EXT_NORM
;
4013 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
4014 error
= xfs_iread_extents(NULL
, ip
, whichfork
);
4019 if (!xfs_iext_lookup_extent(ip
, ifp
, bno
, &idx
, &got
))
4024 while (bno
< end
&& n
< *nmap
) {
4025 /* Reading past eof, act as though there's a hole up to end. */
4027 got
.br_startoff
= end
;
4028 if (got
.br_startoff
> bno
) {
4029 /* Reading in a hole. */
4030 mval
->br_startoff
= bno
;
4031 mval
->br_startblock
= HOLESTARTBLOCK
;
4032 mval
->br_blockcount
=
4033 XFS_FILBLKS_MIN(len
, got
.br_startoff
- bno
);
4034 mval
->br_state
= XFS_EXT_NORM
;
4035 bno
+= mval
->br_blockcount
;
4036 len
-= mval
->br_blockcount
;
4042 /* set up the extent map to return. */
4043 xfs_bmapi_trim_map(mval
, &got
, &bno
, len
, obno
, end
, n
, flags
);
4044 xfs_bmapi_update_map(&mval
, &bno
, &len
, obno
, end
, &n
, flags
);
4046 /* If we're done, stop now. */
4047 if (bno
>= end
|| n
>= *nmap
)
4050 /* Else go on to the next record. */
4051 if (!xfs_iext_get_extent(ifp
, ++idx
, &got
))
4059 * Add a delayed allocation extent to an inode. Blocks are reserved from the
4060 * global pool and the extent inserted into the inode in-core extent tree.
4062 * On entry, got refers to the first extent beyond the offset of the extent to
4063 * allocate or eof is specified if no such extent exists. On return, got refers
4064 * to the extent record that was inserted to the inode fork.
4066 * Note that the allocated extent may have been merged with contiguous extents
4067 * during insertion into the inode fork. Thus, got does not reflect the current
4068 * state of the inode fork on return. If necessary, the caller can use lastx to
4069 * look up the updated record in the inode fork.
4072 xfs_bmapi_reserve_delalloc(
4073 struct xfs_inode
*ip
,
4077 xfs_filblks_t prealloc
,
4078 struct xfs_bmbt_irec
*got
,
4079 xfs_extnum_t
*lastx
,
4082 struct xfs_mount
*mp
= ip
->i_mount
;
4083 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4085 xfs_extlen_t indlen
;
4086 char rt
= XFS_IS_REALTIME_INODE(ip
);
4089 xfs_fileoff_t aoff
= off
;
4092 * Cap the alloc length. Keep track of prealloc so we know whether to
4093 * tag the inode before we return.
4095 alen
= XFS_FILBLKS_MIN(len
+ prealloc
, MAXEXTLEN
);
4097 alen
= XFS_FILBLKS_MIN(alen
, got
->br_startoff
- aoff
);
4098 if (prealloc
&& alen
>= len
)
4099 prealloc
= alen
- len
;
4101 /* Figure out the extent size, adjust alen */
4102 if (whichfork
== XFS_COW_FORK
)
4103 extsz
= xfs_get_cowextsz_hint(ip
);
4105 extsz
= xfs_get_extsz_hint(ip
);
4107 struct xfs_bmbt_irec prev
;
4109 if (!xfs_iext_get_extent(ifp
, *lastx
- 1, &prev
))
4110 prev
.br_startoff
= NULLFILEOFF
;
4112 error
= xfs_bmap_extsize_align(mp
, got
, &prev
, extsz
, rt
, eof
,
4113 1, 0, &aoff
, &alen
);
4118 extsz
= alen
/ mp
->m_sb
.sb_rextsize
;
4121 * Make a transaction-less quota reservation for delayed allocation
4122 * blocks. This number gets adjusted later. We return if we haven't
4123 * allocated blocks already inside this loop.
4125 error
= xfs_trans_reserve_quota_nblks(NULL
, ip
, (long)alen
, 0,
4126 rt
? XFS_QMOPT_RES_RTBLKS
: XFS_QMOPT_RES_REGBLKS
);
4131 * Split changing sb for alen and indlen since they could be coming
4132 * from different places.
4134 indlen
= (xfs_extlen_t
)xfs_bmap_worst_indlen(ip
, alen
);
4138 error
= xfs_mod_frextents(mp
, -((int64_t)extsz
));
4140 error
= xfs_mod_fdblocks(mp
, -((int64_t)alen
), false);
4144 goto out_unreserve_quota
;
4146 error
= xfs_mod_fdblocks(mp
, -((int64_t)indlen
), false);
4148 goto out_unreserve_blocks
;
4151 ip
->i_delayed_blks
+= alen
;
4153 got
->br_startoff
= aoff
;
4154 got
->br_startblock
= nullstartblock(indlen
);
4155 got
->br_blockcount
= alen
;
4156 got
->br_state
= XFS_EXT_NORM
;
4158 xfs_bmap_add_extent_hole_delay(ip
, whichfork
, lastx
, got
);
4161 * Tag the inode if blocks were preallocated. Note that COW fork
4162 * preallocation can occur at the start or end of the extent, even when
4163 * prealloc == 0, so we must also check the aligned offset and length.
4165 if (whichfork
== XFS_DATA_FORK
&& prealloc
)
4166 xfs_inode_set_eofblocks_tag(ip
);
4167 if (whichfork
== XFS_COW_FORK
&& (prealloc
|| aoff
< off
|| alen
> len
))
4168 xfs_inode_set_cowblocks_tag(ip
);
4172 out_unreserve_blocks
:
4174 xfs_mod_frextents(mp
, extsz
);
4176 xfs_mod_fdblocks(mp
, alen
, false);
4177 out_unreserve_quota
:
4178 if (XFS_IS_QUOTA_ON(mp
))
4179 xfs_trans_unreserve_quota_nblks(NULL
, ip
, (long)alen
, 0, rt
?
4180 XFS_QMOPT_RES_RTBLKS
: XFS_QMOPT_RES_REGBLKS
);
4186 struct xfs_bmalloca
*bma
)
4188 struct xfs_mount
*mp
= bma
->ip
->i_mount
;
4189 int whichfork
= xfs_bmapi_whichfork(bma
->flags
);
4190 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
4191 int tmp_logflags
= 0;
4194 ASSERT(bma
->length
> 0);
4197 * For the wasdelay case, we could also just allocate the stuff asked
4198 * for in this bmap call but that wouldn't be as good.
4201 bma
->length
= (xfs_extlen_t
)bma
->got
.br_blockcount
;
4202 bma
->offset
= bma
->got
.br_startoff
;
4204 xfs_iext_get_extent(ifp
, bma
->idx
- 1, &bma
->prev
);
4206 bma
->length
= XFS_FILBLKS_MIN(bma
->length
, MAXEXTLEN
);
4208 bma
->length
= XFS_FILBLKS_MIN(bma
->length
,
4209 bma
->got
.br_startoff
- bma
->offset
);
4213 * Set the data type being allocated. For the data fork, the first data
4214 * in the file is treated differently to all other allocations. For the
4215 * attribute fork, we only need to ensure the allocated range is not on
4218 if (!(bma
->flags
& XFS_BMAPI_METADATA
)) {
4219 bma
->datatype
= XFS_ALLOC_NOBUSY
;
4220 if (whichfork
== XFS_DATA_FORK
) {
4221 if (bma
->offset
== 0)
4222 bma
->datatype
|= XFS_ALLOC_INITIAL_USER_DATA
;
4224 bma
->datatype
|= XFS_ALLOC_USERDATA
;
4226 if (bma
->flags
& XFS_BMAPI_ZERO
)
4227 bma
->datatype
|= XFS_ALLOC_USERDATA_ZERO
;
4230 bma
->minlen
= (bma
->flags
& XFS_BMAPI_CONTIG
) ? bma
->length
: 1;
4233 * Only want to do the alignment at the eof if it is userdata and
4234 * allocation length is larger than a stripe unit.
4236 if (mp
->m_dalign
&& bma
->length
>= mp
->m_dalign
&&
4237 !(bma
->flags
& XFS_BMAPI_METADATA
) && whichfork
== XFS_DATA_FORK
) {
4238 error
= xfs_bmap_isaeof(bma
, whichfork
);
4243 error
= xfs_bmap_alloc(bma
);
4248 bma
->cur
->bc_private
.b
.firstblock
= *bma
->firstblock
;
4249 if (bma
->blkno
== NULLFSBLOCK
)
4251 if ((ifp
->if_flags
& XFS_IFBROOT
) && !bma
->cur
) {
4252 bma
->cur
= xfs_bmbt_init_cursor(mp
, bma
->tp
, bma
->ip
, whichfork
);
4253 bma
->cur
->bc_private
.b
.firstblock
= *bma
->firstblock
;
4254 bma
->cur
->bc_private
.b
.dfops
= bma
->dfops
;
4257 * Bump the number of extents we've allocated
4263 bma
->cur
->bc_private
.b
.flags
=
4264 bma
->wasdel
? XFS_BTCUR_BPRV_WASDEL
: 0;
4266 bma
->got
.br_startoff
= bma
->offset
;
4267 bma
->got
.br_startblock
= bma
->blkno
;
4268 bma
->got
.br_blockcount
= bma
->length
;
4269 bma
->got
.br_state
= XFS_EXT_NORM
;
4272 * In the data fork, a wasdelay extent has been initialized, so
4273 * shouldn't be flagged as unwritten.
4275 * For the cow fork, however, we convert delalloc reservations
4276 * (extents allocated for speculative preallocation) to
4277 * allocated unwritten extents, and only convert the unwritten
4278 * extents to real extents when we're about to write the data.
4280 if ((!bma
->wasdel
|| (bma
->flags
& XFS_BMAPI_COWFORK
)) &&
4281 (bma
->flags
& XFS_BMAPI_PREALLOC
) &&
4282 xfs_sb_version_hasextflgbit(&mp
->m_sb
))
4283 bma
->got
.br_state
= XFS_EXT_UNWRITTEN
;
4286 error
= xfs_bmap_add_extent_delay_real(bma
, whichfork
);
4288 error
= xfs_bmap_add_extent_hole_real(bma
->tp
, bma
->ip
,
4289 whichfork
, &bma
->idx
, &bma
->cur
, &bma
->got
,
4290 bma
->firstblock
, bma
->dfops
, &bma
->logflags
);
4292 bma
->logflags
|= tmp_logflags
;
4297 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4298 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4299 * the neighbouring ones.
4301 xfs_iext_get_extent(ifp
, bma
->idx
, &bma
->got
);
4303 ASSERT(bma
->got
.br_startoff
<= bma
->offset
);
4304 ASSERT(bma
->got
.br_startoff
+ bma
->got
.br_blockcount
>=
4305 bma
->offset
+ bma
->length
);
4306 ASSERT(bma
->got
.br_state
== XFS_EXT_NORM
||
4307 bma
->got
.br_state
== XFS_EXT_UNWRITTEN
);
4312 xfs_bmapi_convert_unwritten(
4313 struct xfs_bmalloca
*bma
,
4314 struct xfs_bmbt_irec
*mval
,
4318 int whichfork
= xfs_bmapi_whichfork(flags
);
4319 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
4320 int tmp_logflags
= 0;
4323 /* check if we need to do unwritten->real conversion */
4324 if (mval
->br_state
== XFS_EXT_UNWRITTEN
&&
4325 (flags
& XFS_BMAPI_PREALLOC
))
4328 /* check if we need to do real->unwritten conversion */
4329 if (mval
->br_state
== XFS_EXT_NORM
&&
4330 (flags
& (XFS_BMAPI_PREALLOC
| XFS_BMAPI_CONVERT
)) !=
4331 (XFS_BMAPI_PREALLOC
| XFS_BMAPI_CONVERT
))
4335 * Modify (by adding) the state flag, if writing.
4337 ASSERT(mval
->br_blockcount
<= len
);
4338 if ((ifp
->if_flags
& XFS_IFBROOT
) && !bma
->cur
) {
4339 bma
->cur
= xfs_bmbt_init_cursor(bma
->ip
->i_mount
, bma
->tp
,
4340 bma
->ip
, whichfork
);
4341 bma
->cur
->bc_private
.b
.firstblock
= *bma
->firstblock
;
4342 bma
->cur
->bc_private
.b
.dfops
= bma
->dfops
;
4344 mval
->br_state
= (mval
->br_state
== XFS_EXT_UNWRITTEN
)
4345 ? XFS_EXT_NORM
: XFS_EXT_UNWRITTEN
;
4348 * Before insertion into the bmbt, zero the range being converted
4351 if (flags
& XFS_BMAPI_ZERO
) {
4352 error
= xfs_zero_extent(bma
->ip
, mval
->br_startblock
,
4353 mval
->br_blockcount
);
4358 error
= xfs_bmap_add_extent_unwritten_real(bma
->tp
, bma
->ip
, whichfork
,
4359 &bma
->idx
, &bma
->cur
, mval
, bma
->firstblock
, bma
->dfops
,
4362 * Log the inode core unconditionally in the unwritten extent conversion
4363 * path because the conversion might not have done so (e.g., if the
4364 * extent count hasn't changed). We need to make sure the inode is dirty
4365 * in the transaction for the sake of fsync(), even if nothing has
4366 * changed, because fsync() will not force the log for this transaction
4367 * unless it sees the inode pinned.
4369 * Note: If we're only converting cow fork extents, there aren't
4370 * any on-disk updates to make, so we don't need to log anything.
4372 if (whichfork
!= XFS_COW_FORK
)
4373 bma
->logflags
|= tmp_logflags
| XFS_ILOG_CORE
;
4378 * Update our extent pointer, given that
4379 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4380 * of the neighbouring ones.
4382 xfs_iext_get_extent(ifp
, bma
->idx
, &bma
->got
);
4385 * We may have combined previously unwritten space with written space,
4386 * so generate another request.
4388 if (mval
->br_blockcount
< len
)
4394 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4395 * extent state if necessary. Details behaviour is controlled by the flags
4396 * parameter. Only allocates blocks from a single allocation group, to avoid
4399 * The returned value in "firstblock" from the first call in a transaction
4400 * must be remembered and presented to subsequent calls in "firstblock".
4401 * An upper bound for the number of blocks to be allocated is supplied to
4402 * the first call in "total"; if no allocation group has that many free
4403 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4407 struct xfs_trans
*tp
, /* transaction pointer */
4408 struct xfs_inode
*ip
, /* incore inode */
4409 xfs_fileoff_t bno
, /* starting file offs. mapped */
4410 xfs_filblks_t len
, /* length to map in file */
4411 int flags
, /* XFS_BMAPI_... */
4412 xfs_fsblock_t
*firstblock
, /* first allocated block
4413 controls a.g. for allocs */
4414 xfs_extlen_t total
, /* total blocks needed */
4415 struct xfs_bmbt_irec
*mval
, /* output: map values */
4416 int *nmap
, /* i/o: mval size/count */
4417 struct xfs_defer_ops
*dfops
) /* i/o: list extents to free */
4419 struct xfs_mount
*mp
= ip
->i_mount
;
4420 struct xfs_ifork
*ifp
;
4421 struct xfs_bmalloca bma
= { NULL
}; /* args for xfs_bmap_alloc */
4422 xfs_fileoff_t end
; /* end of mapped file region */
4423 bool eof
= false; /* after the end of extents */
4424 int error
; /* error return */
4425 int n
; /* current extent index */
4426 xfs_fileoff_t obno
; /* old block number (offset) */
4427 int whichfork
; /* data or attr fork */
4430 xfs_fileoff_t orig_bno
; /* original block number value */
4431 int orig_flags
; /* original flags arg value */
4432 xfs_filblks_t orig_len
; /* original value of len arg */
4433 struct xfs_bmbt_irec
*orig_mval
; /* original value of mval */
4434 int orig_nmap
; /* original value of *nmap */
4442 whichfork
= xfs_bmapi_whichfork(flags
);
4445 ASSERT(*nmap
<= XFS_BMAP_MAX_NMAP
);
4446 ASSERT(!(flags
& XFS_BMAPI_IGSTATE
));
4447 ASSERT(tp
!= NULL
||
4448 (flags
& (XFS_BMAPI_CONVERT
| XFS_BMAPI_COWFORK
)) ==
4449 (XFS_BMAPI_CONVERT
| XFS_BMAPI_COWFORK
));
4451 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_LOCAL
);
4452 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
4453 ASSERT(!(flags
& XFS_BMAPI_REMAP
));
4455 /* zeroing is for currently only for data extents, not metadata */
4456 ASSERT((flags
& (XFS_BMAPI_METADATA
| XFS_BMAPI_ZERO
)) !=
4457 (XFS_BMAPI_METADATA
| XFS_BMAPI_ZERO
));
4459 * we can allocate unwritten extents or pre-zero allocated blocks,
4460 * but it makes no sense to do both at once. This would result in
4461 * zeroing the unwritten extent twice, but it still being an
4462 * unwritten extent....
4464 ASSERT((flags
& (XFS_BMAPI_PREALLOC
| XFS_BMAPI_ZERO
)) !=
4465 (XFS_BMAPI_PREALLOC
| XFS_BMAPI_ZERO
));
4467 if (unlikely(XFS_TEST_ERROR(
4468 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
4469 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
4470 mp
, XFS_ERRTAG_BMAPIFORMAT
))) {
4471 XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW
, mp
);
4472 return -EFSCORRUPTED
;
4475 if (XFS_FORCED_SHUTDOWN(mp
))
4478 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4480 XFS_STATS_INC(mp
, xs_blk_mapw
);
4482 if (*firstblock
== NULLFSBLOCK
) {
4483 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
)
4484 bma
.minleft
= be16_to_cpu(ifp
->if_broot
->bb_level
) + 1;
4491 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
4492 error
= xfs_iread_extents(tp
, ip
, whichfork
);
4501 if (!xfs_iext_lookup_extent(ip
, ifp
, bno
, &bma
.idx
, &bma
.got
))
4503 if (!xfs_iext_get_extent(ifp
, bma
.idx
- 1, &bma
.prev
))
4504 bma
.prev
.br_startoff
= NULLFILEOFF
;
4510 bma
.firstblock
= firstblock
;
4512 while (bno
< end
&& n
< *nmap
) {
4513 bool need_alloc
= false, wasdelay
= false;
4515 /* in hole or beyoned EOF? */
4516 if (eof
|| bma
.got
.br_startoff
> bno
) {
4517 if (flags
& XFS_BMAPI_DELALLOC
) {
4519 * For the COW fork we can reasonably get a
4520 * request for converting an extent that races
4521 * with other threads already having converted
4522 * part of it, as there converting COW to
4523 * regular blocks is not protected using the
4526 ASSERT(flags
& XFS_BMAPI_COWFORK
);
4527 if (!(flags
& XFS_BMAPI_COWFORK
)) {
4532 if (eof
|| bno
>= end
)
4537 } else if (isnullstartblock(bma
.got
.br_startblock
)) {
4542 * First, deal with the hole before the allocated space
4543 * that we found, if any.
4545 if (need_alloc
|| wasdelay
) {
4547 bma
.conv
= !!(flags
& XFS_BMAPI_CONVERT
);
4548 bma
.wasdel
= wasdelay
;
4553 * There's a 32/64 bit type mismatch between the
4554 * allocation length request (which can be 64 bits in
4555 * length) and the bma length request, which is
4556 * xfs_extlen_t and therefore 32 bits. Hence we have to
4557 * check for 32-bit overflows and handle them here.
4559 if (len
> (xfs_filblks_t
)MAXEXTLEN
)
4560 bma
.length
= MAXEXTLEN
;
4565 ASSERT(bma
.length
> 0);
4566 error
= xfs_bmapi_allocate(&bma
);
4569 if (bma
.blkno
== NULLFSBLOCK
)
4573 * If this is a CoW allocation, record the data in
4574 * the refcount btree for orphan recovery.
4576 if (whichfork
== XFS_COW_FORK
) {
4577 error
= xfs_refcount_alloc_cow_extent(mp
, dfops
,
4578 bma
.blkno
, bma
.length
);
4584 /* Deal with the allocated space we found. */
4585 xfs_bmapi_trim_map(mval
, &bma
.got
, &bno
, len
, obno
,
4588 /* Execute unwritten extent conversion if necessary */
4589 error
= xfs_bmapi_convert_unwritten(&bma
, mval
, len
, flags
);
4590 if (error
== -EAGAIN
)
4595 /* update the extent map to return */
4596 xfs_bmapi_update_map(&mval
, &bno
, &len
, obno
, end
, &n
, flags
);
4599 * If we're done, stop now. Stop when we've allocated
4600 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4601 * the transaction may get too big.
4603 if (bno
>= end
|| n
>= *nmap
|| bma
.nallocs
>= *nmap
)
4606 /* Else go on to the next record. */
4608 if (!xfs_iext_get_extent(ifp
, ++bma
.idx
, &bma
.got
))
4614 * Transform from btree to extents, give it cur.
4616 if (xfs_bmap_wants_extents(ip
, whichfork
)) {
4617 int tmp_logflags
= 0;
4620 error
= xfs_bmap_btree_to_extents(tp
, ip
, bma
.cur
,
4621 &tmp_logflags
, whichfork
);
4622 bma
.logflags
|= tmp_logflags
;
4627 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
||
4628 XFS_IFORK_NEXTENTS(ip
, whichfork
) >
4629 XFS_IFORK_MAXEXT(ip
, whichfork
));
4633 * Log everything. Do this after conversion, there's no point in
4634 * logging the extent records if we've converted to btree format.
4636 if ((bma
.logflags
& xfs_ilog_fext(whichfork
)) &&
4637 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
4638 bma
.logflags
&= ~xfs_ilog_fext(whichfork
);
4639 else if ((bma
.logflags
& xfs_ilog_fbroot(whichfork
)) &&
4640 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)
4641 bma
.logflags
&= ~xfs_ilog_fbroot(whichfork
);
4643 * Log whatever the flags say, even if error. Otherwise we might miss
4644 * detecting a case where the data is changed, there's an error,
4645 * and it's not logged so we don't shutdown when we should.
4648 xfs_trans_log_inode(tp
, ip
, bma
.logflags
);
4652 ASSERT(*firstblock
== NULLFSBLOCK
||
4653 XFS_FSB_TO_AGNO(mp
, *firstblock
) <=
4655 bma
.cur
->bc_private
.b
.firstblock
));
4656 *firstblock
= bma
.cur
->bc_private
.b
.firstblock
;
4658 xfs_btree_del_cursor(bma
.cur
,
4659 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
4662 xfs_bmap_validate_ret(orig_bno
, orig_len
, orig_flags
, orig_mval
,
4669 struct xfs_trans
*tp
,
4670 struct xfs_inode
*ip
,
4673 xfs_fsblock_t startblock
,
4674 struct xfs_defer_ops
*dfops
)
4676 struct xfs_mount
*mp
= ip
->i_mount
;
4677 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, XFS_DATA_FORK
);
4678 struct xfs_btree_cur
*cur
= NULL
;
4679 xfs_fsblock_t firstblock
= NULLFSBLOCK
;
4680 struct xfs_bmbt_irec got
;
4682 int logflags
= 0, error
;
4685 ASSERT(len
<= (xfs_filblks_t
)MAXEXTLEN
);
4686 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
4688 if (unlikely(XFS_TEST_ERROR(
4689 (XFS_IFORK_FORMAT(ip
, XFS_DATA_FORK
) != XFS_DINODE_FMT_EXTENTS
&&
4690 XFS_IFORK_FORMAT(ip
, XFS_DATA_FORK
) != XFS_DINODE_FMT_BTREE
),
4691 mp
, XFS_ERRTAG_BMAPIFORMAT
))) {
4692 XFS_ERROR_REPORT("xfs_bmapi_remap", XFS_ERRLEVEL_LOW
, mp
);
4693 return -EFSCORRUPTED
;
4696 if (XFS_FORCED_SHUTDOWN(mp
))
4699 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
4700 error
= xfs_iread_extents(NULL
, ip
, XFS_DATA_FORK
);
4705 if (xfs_iext_lookup_extent(ip
, ifp
, bno
, &idx
, &got
)) {
4706 /* make sure we only reflink into a hole. */
4707 ASSERT(got
.br_startoff
> bno
);
4708 ASSERT(got
.br_startoff
- bno
>= len
);
4711 ip
->i_d
.di_nblocks
+= len
;
4712 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
4714 if (ifp
->if_flags
& XFS_IFBROOT
) {
4715 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, XFS_DATA_FORK
);
4716 cur
->bc_private
.b
.firstblock
= firstblock
;
4717 cur
->bc_private
.b
.dfops
= dfops
;
4718 cur
->bc_private
.b
.flags
= 0;
4721 got
.br_startoff
= bno
;
4722 got
.br_startblock
= startblock
;
4723 got
.br_blockcount
= len
;
4724 got
.br_state
= XFS_EXT_NORM
;
4726 error
= xfs_bmap_add_extent_hole_real(tp
, ip
, XFS_DATA_FORK
, &idx
, &cur
,
4727 &got
, &firstblock
, dfops
, &logflags
);
4731 if (xfs_bmap_wants_extents(ip
, XFS_DATA_FORK
)) {
4732 int tmp_logflags
= 0;
4734 error
= xfs_bmap_btree_to_extents(tp
, ip
, cur
,
4735 &tmp_logflags
, XFS_DATA_FORK
);
4736 logflags
|= tmp_logflags
;
4740 if (ip
->i_d
.di_format
!= XFS_DINODE_FMT_EXTENTS
)
4741 logflags
&= ~XFS_ILOG_DEXT
;
4742 else if (ip
->i_d
.di_format
!= XFS_DINODE_FMT_BTREE
)
4743 logflags
&= ~XFS_ILOG_DBROOT
;
4746 xfs_trans_log_inode(tp
, ip
, logflags
);
4748 xfs_btree_del_cursor(cur
,
4749 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
4755 * When a delalloc extent is split (e.g., due to a hole punch), the original
4756 * indlen reservation must be shared across the two new extents that are left
4759 * Given the original reservation and the worst case indlen for the two new
4760 * extents (as calculated by xfs_bmap_worst_indlen()), split the original
4761 * reservation fairly across the two new extents. If necessary, steal available
4762 * blocks from a deleted extent to make up a reservation deficiency (e.g., if
4763 * ores == 1). The number of stolen blocks is returned. The availability and
4764 * subsequent accounting of stolen blocks is the responsibility of the caller.
4766 static xfs_filblks_t
4767 xfs_bmap_split_indlen(
4768 xfs_filblks_t ores
, /* original res. */
4769 xfs_filblks_t
*indlen1
, /* ext1 worst indlen */
4770 xfs_filblks_t
*indlen2
, /* ext2 worst indlen */
4771 xfs_filblks_t avail
) /* stealable blocks */
4773 xfs_filblks_t len1
= *indlen1
;
4774 xfs_filblks_t len2
= *indlen2
;
4775 xfs_filblks_t nres
= len1
+ len2
; /* new total res. */
4776 xfs_filblks_t stolen
= 0;
4777 xfs_filblks_t resfactor
;
4780 * Steal as many blocks as we can to try and satisfy the worst case
4781 * indlen for both new extents.
4783 if (ores
< nres
&& avail
)
4784 stolen
= XFS_FILBLKS_MIN(nres
- ores
, avail
);
4787 /* nothing else to do if we've satisfied the new reservation */
4792 * We can't meet the total required reservation for the two extents.
4793 * Calculate the percent of the overall shortage between both extents
4794 * and apply this percentage to each of the requested indlen values.
4795 * This distributes the shortage fairly and reduces the chances that one
4796 * of the two extents is left with nothing when extents are repeatedly
4799 resfactor
= (ores
* 100);
4800 do_div(resfactor
, nres
);
4805 ASSERT(len1
+ len2
<= ores
);
4806 ASSERT(len1
< *indlen1
&& len2
< *indlen2
);
4809 * Hand out the remainder to each extent. If one of the two reservations
4810 * is zero, we want to make sure that one gets a block first. The loop
4811 * below starts with len1, so hand len2 a block right off the bat if it
4814 ores
-= (len1
+ len2
);
4815 ASSERT((*indlen1
- len1
) + (*indlen2
- len2
) >= ores
);
4816 if (ores
&& !len2
&& *indlen2
) {
4821 if (len1
< *indlen1
) {
4827 if (len2
< *indlen2
) {
4840 xfs_bmap_del_extent_delay(
4841 struct xfs_inode
*ip
,
4844 struct xfs_bmbt_irec
*got
,
4845 struct xfs_bmbt_irec
*del
)
4847 struct xfs_mount
*mp
= ip
->i_mount
;
4848 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4849 struct xfs_bmbt_irec
new;
4850 int64_t da_old
, da_new
, da_diff
= 0;
4851 xfs_fileoff_t del_endoff
, got_endoff
;
4852 xfs_filblks_t got_indlen
, new_indlen
, stolen
;
4853 int error
= 0, state
= 0;
4856 XFS_STATS_INC(mp
, xs_del_exlist
);
4858 isrt
= (whichfork
== XFS_DATA_FORK
) && XFS_IS_REALTIME_INODE(ip
);
4859 del_endoff
= del
->br_startoff
+ del
->br_blockcount
;
4860 got_endoff
= got
->br_startoff
+ got
->br_blockcount
;
4861 da_old
= startblockval(got
->br_startblock
);
4865 ASSERT(*idx
<= xfs_iext_count(ifp
));
4866 ASSERT(del
->br_blockcount
> 0);
4867 ASSERT(got
->br_startoff
<= del
->br_startoff
);
4868 ASSERT(got_endoff
>= del_endoff
);
4871 uint64_t rtexts
= XFS_FSB_TO_B(mp
, del
->br_blockcount
);
4873 do_div(rtexts
, mp
->m_sb
.sb_rextsize
);
4874 xfs_mod_frextents(mp
, rtexts
);
4878 * Update the inode delalloc counter now and wait to update the
4879 * sb counters as we might have to borrow some blocks for the
4880 * indirect block accounting.
4882 error
= xfs_trans_reserve_quota_nblks(NULL
, ip
,
4883 -((long)del
->br_blockcount
), 0,
4884 isrt
? XFS_QMOPT_RES_RTBLKS
: XFS_QMOPT_RES_REGBLKS
);
4887 ip
->i_delayed_blks
-= del
->br_blockcount
;
4889 if (whichfork
== XFS_COW_FORK
)
4890 state
|= BMAP_COWFORK
;
4892 if (got
->br_startoff
== del
->br_startoff
)
4893 state
|= BMAP_LEFT_FILLING
;
4894 if (got_endoff
== del_endoff
)
4895 state
|= BMAP_RIGHT_FILLING
;
4897 switch (state
& (BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
)) {
4898 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
4900 * Matches the whole extent. Delete the entry.
4902 xfs_iext_remove(ip
, *idx
, 1, state
);
4905 case BMAP_LEFT_FILLING
:
4907 * Deleting the first part of the extent.
4909 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
4910 got
->br_startoff
= del_endoff
;
4911 got
->br_blockcount
-= del
->br_blockcount
;
4912 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
,
4913 got
->br_blockcount
), da_old
);
4914 got
->br_startblock
= nullstartblock((int)da_new
);
4915 xfs_iext_update_extent(ifp
, *idx
, got
);
4916 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
4918 case BMAP_RIGHT_FILLING
:
4920 * Deleting the last part of the extent.
4922 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
4923 got
->br_blockcount
= got
->br_blockcount
- del
->br_blockcount
;
4924 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
,
4925 got
->br_blockcount
), da_old
);
4926 got
->br_startblock
= nullstartblock((int)da_new
);
4927 xfs_iext_update_extent(ifp
, *idx
, got
);
4928 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
4932 * Deleting the middle of the extent.
4934 * Distribute the original indlen reservation across the two new
4935 * extents. Steal blocks from the deleted extent if necessary.
4936 * Stealing blocks simply fudges the fdblocks accounting below.
4937 * Warn if either of the new indlen reservations is zero as this
4938 * can lead to delalloc problems.
4940 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
4942 got
->br_blockcount
= del
->br_startoff
- got
->br_startoff
;
4943 got_indlen
= xfs_bmap_worst_indlen(ip
, got
->br_blockcount
);
4945 new.br_blockcount
= got_endoff
- del_endoff
;
4946 new_indlen
= xfs_bmap_worst_indlen(ip
, new.br_blockcount
);
4948 WARN_ON_ONCE(!got_indlen
|| !new_indlen
);
4949 stolen
= xfs_bmap_split_indlen(da_old
, &got_indlen
, &new_indlen
,
4950 del
->br_blockcount
);
4952 got
->br_startblock
= nullstartblock((int)got_indlen
);
4953 xfs_iext_update_extent(ifp
, *idx
, got
);
4954 trace_xfs_bmap_post_update(ip
, *idx
, 0, _THIS_IP_
);
4956 new.br_startoff
= del_endoff
;
4957 new.br_state
= got
->br_state
;
4958 new.br_startblock
= nullstartblock((int)new_indlen
);
4961 xfs_iext_insert(ip
, *idx
, 1, &new, state
);
4963 da_new
= got_indlen
+ new_indlen
- stolen
;
4964 del
->br_blockcount
-= stolen
;
4968 ASSERT(da_old
>= da_new
);
4969 da_diff
= da_old
- da_new
;
4971 da_diff
+= del
->br_blockcount
;
4973 xfs_mod_fdblocks(mp
, da_diff
, false);
4978 xfs_bmap_del_extent_cow(
4979 struct xfs_inode
*ip
,
4981 struct xfs_bmbt_irec
*got
,
4982 struct xfs_bmbt_irec
*del
)
4984 struct xfs_mount
*mp
= ip
->i_mount
;
4985 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, XFS_COW_FORK
);
4986 struct xfs_bmbt_irec
new;
4987 xfs_fileoff_t del_endoff
, got_endoff
;
4988 int state
= BMAP_COWFORK
;
4990 XFS_STATS_INC(mp
, xs_del_exlist
);
4992 del_endoff
= del
->br_startoff
+ del
->br_blockcount
;
4993 got_endoff
= got
->br_startoff
+ got
->br_blockcount
;
4996 ASSERT(*idx
<= xfs_iext_count(ifp
));
4997 ASSERT(del
->br_blockcount
> 0);
4998 ASSERT(got
->br_startoff
<= del
->br_startoff
);
4999 ASSERT(got_endoff
>= del_endoff
);
5000 ASSERT(!isnullstartblock(got
->br_startblock
));
5002 if (got
->br_startoff
== del
->br_startoff
)
5003 state
|= BMAP_LEFT_FILLING
;
5004 if (got_endoff
== del_endoff
)
5005 state
|= BMAP_RIGHT_FILLING
;
5007 switch (state
& (BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
)) {
5008 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
5010 * Matches the whole extent. Delete the entry.
5012 xfs_iext_remove(ip
, *idx
, 1, state
);
5015 case BMAP_LEFT_FILLING
:
5017 * Deleting the first part of the extent.
5019 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
5020 got
->br_startoff
= del_endoff
;
5021 got
->br_blockcount
-= del
->br_blockcount
;
5022 got
->br_startblock
= del
->br_startblock
+ del
->br_blockcount
;
5023 xfs_iext_update_extent(ifp
, *idx
, got
);
5024 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
5026 case BMAP_RIGHT_FILLING
:
5028 * Deleting the last part of the extent.
5030 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
5031 got
->br_blockcount
-= del
->br_blockcount
;
5032 xfs_iext_update_extent(ifp
, *idx
, got
);
5033 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
5037 * Deleting the middle of the extent.
5039 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
5040 got
->br_blockcount
= del
->br_startoff
- got
->br_startoff
;
5041 xfs_iext_update_extent(ifp
, *idx
, got
);
5042 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
5044 new.br_startoff
= del_endoff
;
5045 new.br_blockcount
= got_endoff
- del_endoff
;
5046 new.br_state
= got
->br_state
;
5047 new.br_startblock
= del
->br_startblock
+ del
->br_blockcount
;
5050 xfs_iext_insert(ip
, *idx
, 1, &new, state
);
5056 * Called by xfs_bmapi to update file extent records and the btree
5057 * after removing space.
5059 STATIC
int /* error */
5060 xfs_bmap_del_extent_real(
5061 xfs_inode_t
*ip
, /* incore inode pointer */
5062 xfs_trans_t
*tp
, /* current transaction pointer */
5063 xfs_extnum_t
*idx
, /* extent number to update/delete */
5064 struct xfs_defer_ops
*dfops
, /* list of extents to be freed */
5065 xfs_btree_cur_t
*cur
, /* if null, not a btree */
5066 xfs_bmbt_irec_t
*del
, /* data to remove from extents */
5067 int *logflagsp
, /* inode logging flags */
5068 int whichfork
, /* data or attr fork */
5069 int bflags
) /* bmapi flags */
5071 xfs_fsblock_t del_endblock
=0; /* first block past del */
5072 xfs_fileoff_t del_endoff
; /* first offset past del */
5073 int do_fx
; /* free extent at end of routine */
5074 int error
; /* error return value */
5075 int flags
= 0;/* inode logging flags */
5076 struct xfs_bmbt_irec got
; /* current extent entry */
5077 xfs_fileoff_t got_endoff
; /* first offset past got */
5078 int i
; /* temp state */
5079 xfs_ifork_t
*ifp
; /* inode fork pointer */
5080 xfs_mount_t
*mp
; /* mount structure */
5081 xfs_filblks_t nblks
; /* quota/sb block count */
5082 xfs_bmbt_irec_t
new; /* new record to be inserted */
5084 uint qfield
; /* quota field to update */
5086 struct xfs_bmbt_irec old
;
5089 XFS_STATS_INC(mp
, xs_del_exlist
);
5091 if (whichfork
== XFS_ATTR_FORK
)
5092 state
|= BMAP_ATTRFORK
;
5093 else if (whichfork
== XFS_COW_FORK
)
5094 state
|= BMAP_COWFORK
;
5096 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5097 ASSERT((*idx
>= 0) && (*idx
< xfs_iext_count(ifp
)));
5098 ASSERT(del
->br_blockcount
> 0);
5099 xfs_iext_get_extent(ifp
, *idx
, &got
);
5100 ASSERT(got
.br_startoff
<= del
->br_startoff
);
5101 del_endoff
= del
->br_startoff
+ del
->br_blockcount
;
5102 got_endoff
= got
.br_startoff
+ got
.br_blockcount
;
5103 ASSERT(got_endoff
>= del_endoff
);
5104 ASSERT(!isnullstartblock(got
.br_startblock
));
5109 * If it's the case where the directory code is running with no block
5110 * reservation, and the deleted block is in the middle of its extent,
5111 * and the resulting insert of an extent would cause transformation to
5112 * btree format, then reject it. The calling code will then swap blocks
5113 * around instead. We have to do this now, rather than waiting for the
5114 * conversion to btree format, since the transaction will be dirty then.
5116 if (tp
->t_blk_res
== 0 &&
5117 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
&&
5118 XFS_IFORK_NEXTENTS(ip
, whichfork
) >=
5119 XFS_IFORK_MAXEXT(ip
, whichfork
) &&
5120 del
->br_startoff
> got
.br_startoff
&& del_endoff
< got_endoff
)
5123 flags
= XFS_ILOG_CORE
;
5124 if (whichfork
== XFS_DATA_FORK
&& XFS_IS_REALTIME_INODE(ip
)) {
5128 ASSERT(do_mod(del
->br_blockcount
, mp
->m_sb
.sb_rextsize
) == 0);
5129 ASSERT(do_mod(del
->br_startblock
, mp
->m_sb
.sb_rextsize
) == 0);
5130 bno
= del
->br_startblock
;
5131 len
= del
->br_blockcount
;
5132 do_div(bno
, mp
->m_sb
.sb_rextsize
);
5133 do_div(len
, mp
->m_sb
.sb_rextsize
);
5134 error
= xfs_rtfree_extent(tp
, bno
, (xfs_extlen_t
)len
);
5138 nblks
= len
* mp
->m_sb
.sb_rextsize
;
5139 qfield
= XFS_TRANS_DQ_RTBCOUNT
;
5142 nblks
= del
->br_blockcount
;
5143 qfield
= XFS_TRANS_DQ_BCOUNT
;
5146 del_endblock
= del
->br_startblock
+ del
->br_blockcount
;
5148 error
= xfs_bmbt_lookup_eq(cur
, got
.br_startoff
,
5149 got
.br_startblock
, got
.br_blockcount
, &i
);
5152 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
5155 if (got
.br_startoff
== del
->br_startoff
)
5156 state
|= BMAP_LEFT_FILLING
;
5157 if (got_endoff
== del_endoff
)
5158 state
|= BMAP_RIGHT_FILLING
;
5160 switch (state
& (BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
)) {
5161 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
5163 * Matches the whole extent. Delete the entry.
5165 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
5166 xfs_iext_remove(ip
, *idx
, 1,
5167 whichfork
== XFS_ATTR_FORK
? BMAP_ATTRFORK
: 0);
5170 XFS_IFORK_NEXT_SET(ip
, whichfork
,
5171 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
5172 flags
|= XFS_ILOG_CORE
;
5174 flags
|= xfs_ilog_fext(whichfork
);
5177 if ((error
= xfs_btree_delete(cur
, &i
)))
5179 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
5181 case BMAP_LEFT_FILLING
:
5183 * Deleting the first part of the extent.
5185 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
5186 got
.br_startoff
= del_endoff
;
5187 got
.br_startblock
= del_endblock
;
5188 got
.br_blockcount
-= del
->br_blockcount
;
5189 xfs_iext_update_extent(ifp
, *idx
, &got
);
5190 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
5192 flags
|= xfs_ilog_fext(whichfork
);
5195 error
= xfs_bmbt_update(cur
, got
.br_startoff
, got
.br_startblock
,
5196 got
.br_blockcount
, got
.br_state
);
5200 case BMAP_RIGHT_FILLING
:
5202 * Deleting the last part of the extent.
5204 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
5205 got
.br_blockcount
-= del
->br_blockcount
;
5206 xfs_iext_update_extent(ifp
, *idx
, &got
);
5207 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
5209 flags
|= xfs_ilog_fext(whichfork
);
5212 error
= xfs_bmbt_update(cur
, got
.br_startoff
, got
.br_startblock
,
5213 got
.br_blockcount
, got
.br_state
);
5219 * Deleting the middle of the extent.
5221 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
5224 got
.br_blockcount
= del
->br_startoff
- got
.br_startoff
;
5225 xfs_iext_update_extent(ifp
, *idx
, &got
);
5227 new.br_startoff
= del_endoff
;
5228 new.br_blockcount
= got_endoff
- del_endoff
;
5229 new.br_state
= got
.br_state
;
5230 new.br_startblock
= del_endblock
;
5232 flags
|= XFS_ILOG_CORE
;
5234 error
= xfs_bmbt_update(cur
, got
.br_startoff
,
5235 got
.br_startblock
, got
.br_blockcount
,
5239 error
= xfs_btree_increment(cur
, 0, &i
);
5242 cur
->bc_rec
.b
= new;
5243 error
= xfs_btree_insert(cur
, &i
);
5244 if (error
&& error
!= -ENOSPC
)
5247 * If get no-space back from btree insert, it tried a
5248 * split, and we have a zero block reservation. Fix up
5249 * our state and return the error.
5251 if (error
== -ENOSPC
) {
5253 * Reset the cursor, don't trust it after any
5256 error
= xfs_bmbt_lookup_eq(cur
, got
.br_startoff
,
5258 got
.br_blockcount
, &i
);
5261 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
5263 * Update the btree record back
5264 * to the original value.
5266 error
= xfs_bmbt_update(cur
, old
.br_startoff
,
5273 * Reset the extent record back
5274 * to the original value.
5276 xfs_iext_update_extent(ifp
, *idx
, &old
);
5281 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
5283 flags
|= xfs_ilog_fext(whichfork
);
5284 XFS_IFORK_NEXT_SET(ip
, whichfork
,
5285 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
5286 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
5287 xfs_iext_insert(ip
, *idx
+ 1, 1, &new, state
);
5292 /* remove reverse mapping */
5293 error
= xfs_rmap_unmap_extent(mp
, dfops
, ip
, whichfork
, del
);
5298 * If we need to, add to list of extents to delete.
5300 if (do_fx
&& !(bflags
& XFS_BMAPI_REMAP
)) {
5301 if (xfs_is_reflink_inode(ip
) && whichfork
== XFS_DATA_FORK
) {
5302 error
= xfs_refcount_decrease_extent(mp
, dfops
, del
);
5306 xfs_bmap_add_free(mp
, dfops
, del
->br_startblock
,
5307 del
->br_blockcount
, NULL
);
5311 * Adjust inode # blocks in the file.
5314 ip
->i_d
.di_nblocks
-= nblks
;
5316 * Adjust quota data.
5318 if (qfield
&& !(bflags
& XFS_BMAPI_REMAP
))
5319 xfs_trans_mod_dquot_byino(tp
, ip
, qfield
, (long)-nblks
);
5327 * Unmap (remove) blocks from a file.
5328 * If nexts is nonzero then the number of extents to remove is limited to
5329 * that value. If not all extents in the block range can be removed then
5334 xfs_trans_t
*tp
, /* transaction pointer */
5335 struct xfs_inode
*ip
, /* incore inode */
5336 xfs_fileoff_t start
, /* first file offset deleted */
5337 xfs_filblks_t
*rlen
, /* i/o: amount remaining */
5338 int flags
, /* misc flags */
5339 xfs_extnum_t nexts
, /* number of extents max */
5340 xfs_fsblock_t
*firstblock
, /* first allocated block
5341 controls a.g. for allocs */
5342 struct xfs_defer_ops
*dfops
) /* i/o: deferred updates */
5344 xfs_btree_cur_t
*cur
; /* bmap btree cursor */
5345 xfs_bmbt_irec_t del
; /* extent being deleted */
5346 int error
; /* error return value */
5347 xfs_extnum_t extno
; /* extent number in list */
5348 xfs_bmbt_irec_t got
; /* current extent record */
5349 xfs_ifork_t
*ifp
; /* inode fork pointer */
5350 int isrt
; /* freeing in rt area */
5351 xfs_extnum_t lastx
; /* last extent index used */
5352 int logflags
; /* transaction logging flags */
5353 xfs_extlen_t mod
; /* rt extent offset */
5354 xfs_mount_t
*mp
; /* mount structure */
5355 int tmp_logflags
; /* partial logging flags */
5356 int wasdel
; /* was a delayed alloc extent */
5357 int whichfork
; /* data or attribute fork */
5359 xfs_filblks_t len
= *rlen
; /* length to unmap in file */
5360 xfs_fileoff_t max_len
;
5361 xfs_agnumber_t prev_agno
= NULLAGNUMBER
, agno
;
5364 trace_xfs_bunmap(ip
, start
, len
, flags
, _RET_IP_
);
5366 whichfork
= xfs_bmapi_whichfork(flags
);
5367 ASSERT(whichfork
!= XFS_COW_FORK
);
5368 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5370 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
5371 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)) {
5372 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW
,
5374 return -EFSCORRUPTED
;
5377 if (XFS_FORCED_SHUTDOWN(mp
))
5380 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
5385 * Guesstimate how many blocks we can unmap without running the risk of
5386 * blowing out the transaction with a mix of EFIs and reflink
5389 if (xfs_is_reflink_inode(ip
) && whichfork
== XFS_DATA_FORK
)
5390 max_len
= min(len
, xfs_refcount_max_unmap(tp
->t_log_res
));
5394 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
5395 (error
= xfs_iread_extents(tp
, ip
, whichfork
)))
5397 if (xfs_iext_count(ifp
) == 0) {
5401 XFS_STATS_INC(mp
, xs_blk_unmap
);
5402 isrt
= (whichfork
== XFS_DATA_FORK
) && XFS_IS_REALTIME_INODE(ip
);
5403 end
= start
+ len
- 1;
5406 * Check to see if the given block number is past the end of the
5407 * file, back up to the last block if so...
5409 if (!xfs_iext_lookup_extent(ip
, ifp
, end
, &lastx
, &got
)) {
5411 xfs_iext_get_extent(ifp
, --lastx
, &got
);
5412 end
= got
.br_startoff
+ got
.br_blockcount
- 1;
5416 if (ifp
->if_flags
& XFS_IFBROOT
) {
5417 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
);
5418 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
5419 cur
->bc_private
.b
.firstblock
= *firstblock
;
5420 cur
->bc_private
.b
.dfops
= dfops
;
5421 cur
->bc_private
.b
.flags
= 0;
5427 * Synchronize by locking the bitmap inode.
5429 xfs_ilock(mp
->m_rbmip
, XFS_ILOCK_EXCL
|XFS_ILOCK_RTBITMAP
);
5430 xfs_trans_ijoin(tp
, mp
->m_rbmip
, XFS_ILOCK_EXCL
);
5431 xfs_ilock(mp
->m_rsumip
, XFS_ILOCK_EXCL
|XFS_ILOCK_RTSUM
);
5432 xfs_trans_ijoin(tp
, mp
->m_rsumip
, XFS_ILOCK_EXCL
);
5436 while (end
!= (xfs_fileoff_t
)-1 && end
>= start
&& lastx
>= 0 &&
5437 (nexts
== 0 || extno
< nexts
) && max_len
> 0) {
5439 * Is the found extent after a hole in which end lives?
5440 * Just back up to the previous extent, if so.
5442 if (got
.br_startoff
> end
) {
5445 xfs_iext_get_extent(ifp
, lastx
, &got
);
5448 * Is the last block of this extent before the range
5449 * we're supposed to delete? If so, we're done.
5451 end
= XFS_FILEOFF_MIN(end
,
5452 got
.br_startoff
+ got
.br_blockcount
- 1);
5456 * Then deal with the (possibly delayed) allocated space
5460 wasdel
= isnullstartblock(del
.br_startblock
);
5463 * Make sure we don't touch multiple AGF headers out of order
5464 * in a single transaction, as that could cause AB-BA deadlocks.
5467 agno
= XFS_FSB_TO_AGNO(mp
, del
.br_startblock
);
5468 if (prev_agno
!= NULLAGNUMBER
&& prev_agno
> agno
)
5472 if (got
.br_startoff
< start
) {
5473 del
.br_startoff
= start
;
5474 del
.br_blockcount
-= start
- got
.br_startoff
;
5476 del
.br_startblock
+= start
- got
.br_startoff
;
5478 if (del
.br_startoff
+ del
.br_blockcount
> end
+ 1)
5479 del
.br_blockcount
= end
+ 1 - del
.br_startoff
;
5481 /* How much can we safely unmap? */
5482 if (max_len
< del
.br_blockcount
) {
5483 del
.br_startoff
+= del
.br_blockcount
- max_len
;
5485 del
.br_startblock
+= del
.br_blockcount
- max_len
;
5486 del
.br_blockcount
= max_len
;
5489 sum
= del
.br_startblock
+ del
.br_blockcount
;
5491 (mod
= do_mod(sum
, mp
->m_sb
.sb_rextsize
))) {
5493 * Realtime extent not lined up at the end.
5494 * The extent could have been split into written
5495 * and unwritten pieces, or we could just be
5496 * unmapping part of it. But we can't really
5497 * get rid of part of a realtime extent.
5499 if (del
.br_state
== XFS_EXT_UNWRITTEN
||
5500 !xfs_sb_version_hasextflgbit(&mp
->m_sb
)) {
5502 * This piece is unwritten, or we're not
5503 * using unwritten extents. Skip over it.
5506 end
-= mod
> del
.br_blockcount
?
5507 del
.br_blockcount
: mod
;
5508 if (end
< got
.br_startoff
) {
5510 xfs_iext_get_extent(ifp
, lastx
,
5516 * It's written, turn it unwritten.
5517 * This is better than zeroing it.
5519 ASSERT(del
.br_state
== XFS_EXT_NORM
);
5520 ASSERT(tp
->t_blk_res
> 0);
5522 * If this spans a realtime extent boundary,
5523 * chop it back to the start of the one we end at.
5525 if (del
.br_blockcount
> mod
) {
5526 del
.br_startoff
+= del
.br_blockcount
- mod
;
5527 del
.br_startblock
+= del
.br_blockcount
- mod
;
5528 del
.br_blockcount
= mod
;
5530 del
.br_state
= XFS_EXT_UNWRITTEN
;
5531 error
= xfs_bmap_add_extent_unwritten_real(tp
, ip
,
5532 whichfork
, &lastx
, &cur
, &del
,
5533 firstblock
, dfops
, &logflags
);
5538 if (isrt
&& (mod
= do_mod(del
.br_startblock
, mp
->m_sb
.sb_rextsize
))) {
5540 * Realtime extent is lined up at the end but not
5541 * at the front. We'll get rid of full extents if
5544 mod
= mp
->m_sb
.sb_rextsize
- mod
;
5545 if (del
.br_blockcount
> mod
) {
5546 del
.br_blockcount
-= mod
;
5547 del
.br_startoff
+= mod
;
5548 del
.br_startblock
+= mod
;
5549 } else if ((del
.br_startoff
== start
&&
5550 (del
.br_state
== XFS_EXT_UNWRITTEN
||
5551 tp
->t_blk_res
== 0)) ||
5552 !xfs_sb_version_hasextflgbit(&mp
->m_sb
)) {
5554 * Can't make it unwritten. There isn't
5555 * a full extent here so just skip it.
5557 ASSERT(end
>= del
.br_blockcount
);
5558 end
-= del
.br_blockcount
;
5559 if (got
.br_startoff
> end
&& --lastx
>= 0)
5560 xfs_iext_get_extent(ifp
, lastx
, &got
);
5562 } else if (del
.br_state
== XFS_EXT_UNWRITTEN
) {
5563 struct xfs_bmbt_irec prev
;
5566 * This one is already unwritten.
5567 * It must have a written left neighbor.
5568 * Unwrite the killed part of that one and
5572 xfs_iext_get_extent(ifp
, lastx
- 1, &prev
);
5573 ASSERT(prev
.br_state
== XFS_EXT_NORM
);
5574 ASSERT(!isnullstartblock(prev
.br_startblock
));
5575 ASSERT(del
.br_startblock
==
5576 prev
.br_startblock
+ prev
.br_blockcount
);
5577 if (prev
.br_startoff
< start
) {
5578 mod
= start
- prev
.br_startoff
;
5579 prev
.br_blockcount
-= mod
;
5580 prev
.br_startblock
+= mod
;
5581 prev
.br_startoff
= start
;
5583 prev
.br_state
= XFS_EXT_UNWRITTEN
;
5585 error
= xfs_bmap_add_extent_unwritten_real(tp
,
5586 ip
, whichfork
, &lastx
, &cur
,
5587 &prev
, firstblock
, dfops
,
5593 ASSERT(del
.br_state
== XFS_EXT_NORM
);
5594 del
.br_state
= XFS_EXT_UNWRITTEN
;
5595 error
= xfs_bmap_add_extent_unwritten_real(tp
,
5596 ip
, whichfork
, &lastx
, &cur
,
5597 &del
, firstblock
, dfops
,
5606 error
= xfs_bmap_del_extent_delay(ip
, whichfork
, &lastx
,
5609 error
= xfs_bmap_del_extent_real(ip
, tp
, &lastx
, dfops
,
5610 cur
, &del
, &tmp_logflags
, whichfork
,
5612 logflags
|= tmp_logflags
;
5618 max_len
-= del
.br_blockcount
;
5619 end
= del
.br_startoff
- 1;
5622 * If not done go on to the next (previous) record.
5624 if (end
!= (xfs_fileoff_t
)-1 && end
>= start
) {
5626 xfs_iext_get_extent(ifp
, lastx
, &got
);
5627 if (got
.br_startoff
> end
&& --lastx
>= 0)
5628 xfs_iext_get_extent(ifp
, lastx
, &got
);
5633 if (end
== (xfs_fileoff_t
)-1 || end
< start
|| lastx
< 0)
5636 *rlen
= end
- start
+ 1;
5639 * Convert to a btree if necessary.
5641 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
5642 ASSERT(cur
== NULL
);
5643 error
= xfs_bmap_extents_to_btree(tp
, ip
, firstblock
, dfops
,
5644 &cur
, 0, &tmp_logflags
, whichfork
);
5645 logflags
|= tmp_logflags
;
5650 * transform from btree to extents, give it cur
5652 else if (xfs_bmap_wants_extents(ip
, whichfork
)) {
5653 ASSERT(cur
!= NULL
);
5654 error
= xfs_bmap_btree_to_extents(tp
, ip
, cur
, &tmp_logflags
,
5656 logflags
|= tmp_logflags
;
5661 * transform from extents to local?
5666 * Log everything. Do this after conversion, there's no point in
5667 * logging the extent records if we've converted to btree format.
5669 if ((logflags
& xfs_ilog_fext(whichfork
)) &&
5670 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
5671 logflags
&= ~xfs_ilog_fext(whichfork
);
5672 else if ((logflags
& xfs_ilog_fbroot(whichfork
)) &&
5673 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)
5674 logflags
&= ~xfs_ilog_fbroot(whichfork
);
5676 * Log inode even in the error case, if the transaction
5677 * is dirty we'll need to shut down the filesystem.
5680 xfs_trans_log_inode(tp
, ip
, logflags
);
5683 *firstblock
= cur
->bc_private
.b
.firstblock
;
5684 cur
->bc_private
.b
.allocated
= 0;
5686 xfs_btree_del_cursor(cur
,
5687 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
5692 /* Unmap a range of a file. */
5696 struct xfs_inode
*ip
,
5701 xfs_fsblock_t
*firstblock
,
5702 struct xfs_defer_ops
*dfops
,
5707 error
= __xfs_bunmapi(tp
, ip
, bno
, &len
, flags
, nexts
, firstblock
,
5714 * Determine whether an extent shift can be accomplished by a merge with the
5715 * extent that precedes the target hole of the shift.
5719 struct xfs_bmbt_irec
*left
, /* preceding extent */
5720 struct xfs_bmbt_irec
*got
, /* current extent to shift */
5721 xfs_fileoff_t shift
) /* shift fsb */
5723 xfs_fileoff_t startoff
;
5725 startoff
= got
->br_startoff
- shift
;
5728 * The extent, once shifted, must be adjacent in-file and on-disk with
5729 * the preceding extent.
5731 if ((left
->br_startoff
+ left
->br_blockcount
!= startoff
) ||
5732 (left
->br_startblock
+ left
->br_blockcount
!= got
->br_startblock
) ||
5733 (left
->br_state
!= got
->br_state
) ||
5734 (left
->br_blockcount
+ got
->br_blockcount
> MAXEXTLEN
))
5741 * A bmap extent shift adjusts the file offset of an extent to fill a preceding
5742 * hole in the file. If an extent shift would result in the extent being fully
5743 * adjacent to the extent that currently precedes the hole, we can merge with
5744 * the preceding extent rather than do the shift.
5746 * This function assumes the caller has verified a shift-by-merge is possible
5747 * with the provided extents via xfs_bmse_can_merge().
5751 struct xfs_inode
*ip
,
5753 xfs_fileoff_t shift
, /* shift fsb */
5754 int current_ext
, /* idx of gotp */
5755 struct xfs_bmbt_irec
*got
, /* extent to shift */
5756 struct xfs_bmbt_irec
*left
, /* preceding extent */
5757 struct xfs_btree_cur
*cur
,
5758 int *logflags
, /* output */
5759 struct xfs_defer_ops
*dfops
)
5761 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5762 struct xfs_bmbt_irec
new;
5763 xfs_filblks_t blockcount
;
5765 struct xfs_mount
*mp
= ip
->i_mount
;
5767 blockcount
= left
->br_blockcount
+ got
->br_blockcount
;
5769 ASSERT(xfs_isilocked(ip
, XFS_IOLOCK_EXCL
));
5770 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
5771 ASSERT(xfs_bmse_can_merge(left
, got
, shift
));
5774 new.br_blockcount
= blockcount
;
5777 * Update the on-disk extent count, the btree if necessary and log the
5780 XFS_IFORK_NEXT_SET(ip
, whichfork
,
5781 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
5782 *logflags
|= XFS_ILOG_CORE
;
5784 *logflags
|= XFS_ILOG_DEXT
;
5788 /* lookup and remove the extent to merge */
5789 error
= xfs_bmbt_lookup_eq(cur
, got
->br_startoff
, got
->br_startblock
,
5790 got
->br_blockcount
, &i
);
5793 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
5795 error
= xfs_btree_delete(cur
, &i
);
5798 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
5800 /* lookup and update size of the previous extent */
5801 error
= xfs_bmbt_lookup_eq(cur
, left
->br_startoff
, left
->br_startblock
,
5802 left
->br_blockcount
, &i
);
5805 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
5807 error
= xfs_bmbt_update(cur
, new.br_startoff
, new.br_startblock
,
5808 new.br_blockcount
, new.br_state
);
5813 xfs_iext_update_extent(ifp
, current_ext
- 1, &new);
5814 xfs_iext_remove(ip
, current_ext
, 1, 0);
5816 /* update reverse mapping. rmap functions merge the rmaps for us */
5817 error
= xfs_rmap_unmap_extent(mp
, dfops
, ip
, whichfork
, got
);
5820 memcpy(&new, got
, sizeof(new));
5821 new.br_startoff
= left
->br_startoff
+ left
->br_blockcount
;
5822 return xfs_rmap_map_extent(mp
, dfops
, ip
, whichfork
, &new);
5826 * Shift a single extent.
5830 struct xfs_inode
*ip
,
5832 xfs_fileoff_t offset_shift_fsb
,
5834 struct xfs_bmbt_irec
*got
,
5835 struct xfs_btree_cur
*cur
,
5837 enum shift_direction direction
,
5838 struct xfs_defer_ops
*dfops
)
5840 struct xfs_ifork
*ifp
;
5841 struct xfs_mount
*mp
;
5842 xfs_fileoff_t startoff
;
5843 struct xfs_bmbt_irec adj_irec
, new;
5849 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5850 total_extents
= xfs_iext_count(ifp
);
5852 /* delalloc extents should be prevented by caller */
5853 XFS_WANT_CORRUPTED_RETURN(mp
, !isnullstartblock(got
->br_startblock
));
5855 if (direction
== SHIFT_LEFT
) {
5856 startoff
= got
->br_startoff
- offset_shift_fsb
;
5859 * Check for merge if we've got an extent to the left,
5860 * otherwise make sure there's enough room at the start
5861 * of the file for the shift.
5863 if (!*current_ext
) {
5864 if (got
->br_startoff
< offset_shift_fsb
)
5866 goto update_current_ext
;
5870 * grab the left extent and check for a large enough hole.
5872 xfs_iext_get_extent(ifp
, *current_ext
- 1, &adj_irec
);
5873 if (startoff
< adj_irec
.br_startoff
+ adj_irec
.br_blockcount
)
5876 /* check whether to merge the extent or shift it down */
5877 if (xfs_bmse_can_merge(&adj_irec
, got
, offset_shift_fsb
)) {
5878 return xfs_bmse_merge(ip
, whichfork
, offset_shift_fsb
,
5879 *current_ext
, got
, &adj_irec
,
5880 cur
, logflags
, dfops
);
5883 startoff
= got
->br_startoff
+ offset_shift_fsb
;
5884 /* nothing to move if this is the last extent */
5885 if (*current_ext
>= (total_extents
- 1))
5886 goto update_current_ext
;
5889 * If this is not the last extent in the file, make sure there
5890 * is enough room between current extent and next extent for
5891 * accommodating the shift.
5893 xfs_iext_get_extent(ifp
, *current_ext
+ 1, &adj_irec
);
5894 if (startoff
+ got
->br_blockcount
> adj_irec
.br_startoff
)
5898 * Unlike a left shift (which involves a hole punch),
5899 * a right shift does not modify extent neighbors
5900 * in any way. We should never find mergeable extents
5901 * in this scenario. Check anyways and warn if we
5902 * encounter two extents that could be one.
5904 if (xfs_bmse_can_merge(got
, &adj_irec
, offset_shift_fsb
))
5909 * Increment the extent index for the next iteration, update the start
5910 * offset of the in-core extent and update the btree if applicable.
5913 *logflags
|= XFS_ILOG_CORE
;
5916 new.br_startoff
= startoff
;
5919 error
= xfs_bmbt_lookup_eq(cur
, got
->br_startoff
,
5920 got
->br_startblock
, got
->br_blockcount
, &i
);
5923 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
5925 error
= xfs_bmbt_update(cur
, new.br_startoff
,
5926 new.br_startblock
, new.br_blockcount
,
5931 *logflags
|= XFS_ILOG_DEXT
;
5934 xfs_iext_update_extent(ifp
, *current_ext
, &new);
5936 if (direction
== SHIFT_LEFT
)
5941 /* update reverse mapping */
5942 error
= xfs_rmap_unmap_extent(mp
, dfops
, ip
, whichfork
, got
);
5945 return xfs_rmap_map_extent(mp
, dfops
, ip
, whichfork
, &new);
5949 * Shift extent records to the left/right to cover/create a hole.
5951 * The maximum number of extents to be shifted in a single operation is
5952 * @num_exts. @stop_fsb specifies the file offset at which to stop shift and the
5953 * file offset where we've left off is returned in @next_fsb. @offset_shift_fsb
5954 * is the length by which each extent is shifted. If there is no hole to shift
5955 * the extents into, this will be considered invalid operation and we abort
5959 xfs_bmap_shift_extents(
5960 struct xfs_trans
*tp
,
5961 struct xfs_inode
*ip
,
5962 xfs_fileoff_t
*next_fsb
,
5963 xfs_fileoff_t offset_shift_fsb
,
5965 xfs_fileoff_t stop_fsb
,
5966 xfs_fsblock_t
*firstblock
,
5967 struct xfs_defer_ops
*dfops
,
5968 enum shift_direction direction
,
5971 struct xfs_btree_cur
*cur
= NULL
;
5972 struct xfs_bmbt_irec got
;
5973 struct xfs_mount
*mp
= ip
->i_mount
;
5974 struct xfs_ifork
*ifp
;
5975 xfs_extnum_t nexts
= 0;
5976 xfs_extnum_t current_ext
;
5977 xfs_extnum_t total_extents
;
5978 xfs_extnum_t stop_extent
;
5980 int whichfork
= XFS_DATA_FORK
;
5983 if (unlikely(XFS_TEST_ERROR(
5984 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
5985 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
5986 mp
, XFS_ERRTAG_BMAPIFORMAT
))) {
5987 XFS_ERROR_REPORT("xfs_bmap_shift_extents",
5988 XFS_ERRLEVEL_LOW
, mp
);
5989 return -EFSCORRUPTED
;
5992 if (XFS_FORCED_SHUTDOWN(mp
))
5995 ASSERT(xfs_isilocked(ip
, XFS_IOLOCK_EXCL
));
5996 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
5997 ASSERT(direction
== SHIFT_LEFT
|| direction
== SHIFT_RIGHT
);
5999 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
6000 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
6001 /* Read in all the extents */
6002 error
= xfs_iread_extents(tp
, ip
, whichfork
);
6007 if (ifp
->if_flags
& XFS_IFBROOT
) {
6008 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
6009 cur
->bc_private
.b
.firstblock
= *firstblock
;
6010 cur
->bc_private
.b
.dfops
= dfops
;
6011 cur
->bc_private
.b
.flags
= 0;
6015 * There may be delalloc extents in the data fork before the range we
6016 * are collapsing out, so we cannot use the count of real extents here.
6017 * Instead we have to calculate it from the incore fork.
6019 total_extents
= xfs_iext_count(ifp
);
6020 if (total_extents
== 0) {
6026 * In case of first right shift, we need to initialize next_fsb
6028 if (*next_fsb
== NULLFSBLOCK
) {
6029 ASSERT(direction
== SHIFT_RIGHT
);
6031 current_ext
= total_extents
- 1;
6032 xfs_iext_get_extent(ifp
, current_ext
, &got
);
6033 if (stop_fsb
> got
.br_startoff
) {
6037 *next_fsb
= got
.br_startoff
;
6040 * Look up the extent index for the fsb where we start shifting. We can
6041 * henceforth iterate with current_ext as extent list changes are locked
6044 * If next_fsb lies in a hole beyond which there are no extents we are
6047 if (!xfs_iext_lookup_extent(ip
, ifp
, *next_fsb
, ¤t_ext
,
6054 /* Lookup the extent index at which we have to stop */
6055 if (direction
== SHIFT_RIGHT
) {
6056 struct xfs_bmbt_irec s
;
6058 xfs_iext_lookup_extent(ip
, ifp
, stop_fsb
, &stop_extent
, &s
);
6059 /* Make stop_extent exclusive of shift range */
6061 if (current_ext
<= stop_extent
) {
6066 stop_extent
= total_extents
;
6067 if (current_ext
>= stop_extent
) {
6073 while (nexts
++ < num_exts
) {
6074 error
= xfs_bmse_shift_one(ip
, whichfork
, offset_shift_fsb
,
6075 ¤t_ext
, &got
, cur
, &logflags
,
6080 * If there was an extent merge during the shift, the extent
6081 * count can change. Update the total and grade the next record.
6083 if (direction
== SHIFT_LEFT
) {
6084 total_extents
= xfs_iext_count(ifp
);
6085 stop_extent
= total_extents
;
6088 if (current_ext
== stop_extent
) {
6090 *next_fsb
= NULLFSBLOCK
;
6093 xfs_iext_get_extent(ifp
, current_ext
, &got
);
6097 *next_fsb
= got
.br_startoff
;
6101 xfs_btree_del_cursor(cur
,
6102 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
6105 xfs_trans_log_inode(tp
, ip
, logflags
);
6111 * Splits an extent into two extents at split_fsb block such that it is
6112 * the first block of the current_ext. @current_ext is a target extent
6113 * to be split. @split_fsb is a block where the extents is split.
6114 * If split_fsb lies in a hole or the first block of extents, just return 0.
6117 xfs_bmap_split_extent_at(
6118 struct xfs_trans
*tp
,
6119 struct xfs_inode
*ip
,
6120 xfs_fileoff_t split_fsb
,
6121 xfs_fsblock_t
*firstfsb
,
6122 struct xfs_defer_ops
*dfops
)
6124 int whichfork
= XFS_DATA_FORK
;
6125 struct xfs_btree_cur
*cur
= NULL
;
6126 struct xfs_bmbt_irec got
;
6127 struct xfs_bmbt_irec
new; /* split extent */
6128 struct xfs_mount
*mp
= ip
->i_mount
;
6129 struct xfs_ifork
*ifp
;
6130 xfs_fsblock_t gotblkcnt
; /* new block count for got */
6131 xfs_extnum_t current_ext
;
6136 if (unlikely(XFS_TEST_ERROR(
6137 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
6138 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
6139 mp
, XFS_ERRTAG_BMAPIFORMAT
))) {
6140 XFS_ERROR_REPORT("xfs_bmap_split_extent_at",
6141 XFS_ERRLEVEL_LOW
, mp
);
6142 return -EFSCORRUPTED
;
6145 if (XFS_FORCED_SHUTDOWN(mp
))
6148 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
6149 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
6150 /* Read in all the extents */
6151 error
= xfs_iread_extents(tp
, ip
, whichfork
);
6157 * If there are not extents, or split_fsb lies in a hole we are done.
6159 if (!xfs_iext_lookup_extent(ip
, ifp
, split_fsb
, ¤t_ext
, &got
) ||
6160 got
.br_startoff
>= split_fsb
)
6163 gotblkcnt
= split_fsb
- got
.br_startoff
;
6164 new.br_startoff
= split_fsb
;
6165 new.br_startblock
= got
.br_startblock
+ gotblkcnt
;
6166 new.br_blockcount
= got
.br_blockcount
- gotblkcnt
;
6167 new.br_state
= got
.br_state
;
6169 if (ifp
->if_flags
& XFS_IFBROOT
) {
6170 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
6171 cur
->bc_private
.b
.firstblock
= *firstfsb
;
6172 cur
->bc_private
.b
.dfops
= dfops
;
6173 cur
->bc_private
.b
.flags
= 0;
6174 error
= xfs_bmbt_lookup_eq(cur
, got
.br_startoff
,
6180 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, del_cursor
);
6183 got
.br_blockcount
= gotblkcnt
;
6184 xfs_iext_update_extent(ifp
, current_ext
, &got
);
6186 logflags
= XFS_ILOG_CORE
;
6188 error
= xfs_bmbt_update(cur
, got
.br_startoff
,
6195 logflags
|= XFS_ILOG_DEXT
;
6197 /* Add new extent */
6199 xfs_iext_insert(ip
, current_ext
, 1, &new, 0);
6200 XFS_IFORK_NEXT_SET(ip
, whichfork
,
6201 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
6204 error
= xfs_bmbt_lookup_eq(cur
, new.br_startoff
,
6205 new.br_startblock
, new.br_blockcount
,
6209 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, del_cursor
);
6210 cur
->bc_rec
.b
.br_state
= new.br_state
;
6212 error
= xfs_btree_insert(cur
, &i
);
6215 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, del_cursor
);
6219 * Convert to a btree if necessary.
6221 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
6222 int tmp_logflags
; /* partial log flag return val */
6224 ASSERT(cur
== NULL
);
6225 error
= xfs_bmap_extents_to_btree(tp
, ip
, firstfsb
, dfops
,
6226 &cur
, 0, &tmp_logflags
, whichfork
);
6227 logflags
|= tmp_logflags
;
6232 cur
->bc_private
.b
.allocated
= 0;
6233 xfs_btree_del_cursor(cur
,
6234 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
6238 xfs_trans_log_inode(tp
, ip
, logflags
);
6243 xfs_bmap_split_extent(
6244 struct xfs_inode
*ip
,
6245 xfs_fileoff_t split_fsb
)
6247 struct xfs_mount
*mp
= ip
->i_mount
;
6248 struct xfs_trans
*tp
;
6249 struct xfs_defer_ops dfops
;
6250 xfs_fsblock_t firstfsb
;
6253 error
= xfs_trans_alloc(mp
, &M_RES(mp
)->tr_write
,
6254 XFS_DIOSTRAT_SPACE_RES(mp
, 0), 0, 0, &tp
);
6258 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
6259 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
);
6261 xfs_defer_init(&dfops
, &firstfsb
);
6263 error
= xfs_bmap_split_extent_at(tp
, ip
, split_fsb
,
6268 error
= xfs_defer_finish(&tp
, &dfops
);
6272 return xfs_trans_commit(tp
);
6275 xfs_defer_cancel(&dfops
);
6276 xfs_trans_cancel(tp
);
6280 /* Deferred mapping is only for real extents in the data fork. */
6282 xfs_bmap_is_update_needed(
6283 struct xfs_bmbt_irec
*bmap
)
6285 return bmap
->br_startblock
!= HOLESTARTBLOCK
&&
6286 bmap
->br_startblock
!= DELAYSTARTBLOCK
;
6289 /* Record a bmap intent. */
6292 struct xfs_mount
*mp
,
6293 struct xfs_defer_ops
*dfops
,
6294 enum xfs_bmap_intent_type type
,
6295 struct xfs_inode
*ip
,
6297 struct xfs_bmbt_irec
*bmap
)
6300 struct xfs_bmap_intent
*bi
;
6302 trace_xfs_bmap_defer(mp
,
6303 XFS_FSB_TO_AGNO(mp
, bmap
->br_startblock
),
6305 XFS_FSB_TO_AGBNO(mp
, bmap
->br_startblock
),
6306 ip
->i_ino
, whichfork
,
6308 bmap
->br_blockcount
,
6311 bi
= kmem_alloc(sizeof(struct xfs_bmap_intent
), KM_SLEEP
| KM_NOFS
);
6312 INIT_LIST_HEAD(&bi
->bi_list
);
6315 bi
->bi_whichfork
= whichfork
;
6316 bi
->bi_bmap
= *bmap
;
6318 error
= xfs_defer_ijoin(dfops
, bi
->bi_owner
);
6324 xfs_defer_add(dfops
, XFS_DEFER_OPS_TYPE_BMAP
, &bi
->bi_list
);
6328 /* Map an extent into a file. */
6330 xfs_bmap_map_extent(
6331 struct xfs_mount
*mp
,
6332 struct xfs_defer_ops
*dfops
,
6333 struct xfs_inode
*ip
,
6334 struct xfs_bmbt_irec
*PREV
)
6336 if (!xfs_bmap_is_update_needed(PREV
))
6339 return __xfs_bmap_add(mp
, dfops
, XFS_BMAP_MAP
, ip
,
6340 XFS_DATA_FORK
, PREV
);
6343 /* Unmap an extent out of a file. */
6345 xfs_bmap_unmap_extent(
6346 struct xfs_mount
*mp
,
6347 struct xfs_defer_ops
*dfops
,
6348 struct xfs_inode
*ip
,
6349 struct xfs_bmbt_irec
*PREV
)
6351 if (!xfs_bmap_is_update_needed(PREV
))
6354 return __xfs_bmap_add(mp
, dfops
, XFS_BMAP_UNMAP
, ip
,
6355 XFS_DATA_FORK
, PREV
);
6359 * Process one of the deferred bmap operations. We pass back the
6360 * btree cursor to maintain our lock on the bmapbt between calls.
6363 xfs_bmap_finish_one(
6364 struct xfs_trans
*tp
,
6365 struct xfs_defer_ops
*dfops
,
6366 struct xfs_inode
*ip
,
6367 enum xfs_bmap_intent_type type
,
6369 xfs_fileoff_t startoff
,
6370 xfs_fsblock_t startblock
,
6371 xfs_filblks_t
*blockcount
,
6374 xfs_fsblock_t firstfsb
;
6378 * firstfsb is tied to the transaction lifetime and is used to
6379 * ensure correct AG locking order and schedule work item
6380 * continuations. XFS_BUI_MAX_FAST_EXTENTS (== 1) restricts us
6381 * to only making one bmap call per transaction, so it should
6382 * be safe to have it as a local variable here.
6384 firstfsb
= NULLFSBLOCK
;
6386 trace_xfs_bmap_deferred(tp
->t_mountp
,
6387 XFS_FSB_TO_AGNO(tp
->t_mountp
, startblock
), type
,
6388 XFS_FSB_TO_AGBNO(tp
->t_mountp
, startblock
),
6389 ip
->i_ino
, whichfork
, startoff
, *blockcount
, state
);
6391 if (WARN_ON_ONCE(whichfork
!= XFS_DATA_FORK
))
6392 return -EFSCORRUPTED
;
6394 if (XFS_TEST_ERROR(false, tp
->t_mountp
,
6395 XFS_ERRTAG_BMAP_FINISH_ONE
))
6400 error
= xfs_bmapi_remap(tp
, ip
, startoff
, *blockcount
,
6404 case XFS_BMAP_UNMAP
:
6405 error
= __xfs_bunmapi(tp
, ip
, startoff
, blockcount
,
6406 XFS_BMAPI_REMAP
, 1, &firstfsb
, dfops
);
6410 error
= -EFSCORRUPTED
;