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_da_format.h"
28 #include "xfs_da_btree.h"
30 #include "xfs_inode.h"
31 #include "xfs_btree.h"
32 #include "xfs_trans.h"
33 #include "xfs_alloc.h"
35 #include "xfs_bmap_btree.h"
36 #include "xfs_trans_space.h"
37 #include "xfs_trace.h"
38 #include "xfs_attr_leaf.h"
39 #include "xfs_quota_defs.h"
42 kmem_zone_t
*xfs_bmap_free_item_zone
;
45 * Miscellaneous helper functions
49 * Compute and fill in the value of the maximum depth of a bmap btree
50 * in this filesystem. Done once, during mount.
53 xfs_bmap_compute_maxlevels(
54 xfs_mount_t
*mp
, /* file system mount structure */
55 int whichfork
) /* data or attr fork */
57 int level
; /* btree level */
58 uint maxblocks
; /* max blocks at this level */
59 uint maxleafents
; /* max leaf entries possible */
60 int maxrootrecs
; /* max records in root block */
61 int minleafrecs
; /* min records in leaf block */
62 int minnoderecs
; /* min records in node block */
63 int sz
; /* root block size */
66 * The maximum number of extents in a file, hence the maximum
67 * number of leaf entries, is controlled by the type of di_nextents
68 * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
69 * (a signed 16-bit number, xfs_aextnum_t).
71 * Note that we can no longer assume that if we are in ATTR1 that
72 * the fork offset of all the inodes will be
73 * (xfs_default_attroffset(ip) >> 3) because we could have mounted
74 * with ATTR2 and then mounted back with ATTR1, keeping the
75 * di_forkoff's fixed but probably at various positions. Therefore,
76 * for both ATTR1 and ATTR2 we have to assume the worst case scenario
77 * of a minimum size available.
79 if (whichfork
== XFS_DATA_FORK
) {
80 maxleafents
= MAXEXTNUM
;
81 sz
= XFS_BMDR_SPACE_CALC(MINDBTPTRS
);
83 maxleafents
= MAXAEXTNUM
;
84 sz
= XFS_BMDR_SPACE_CALC(MINABTPTRS
);
86 maxrootrecs
= xfs_bmdr_maxrecs(sz
, 0);
87 minleafrecs
= mp
->m_bmap_dmnr
[0];
88 minnoderecs
= mp
->m_bmap_dmnr
[1];
89 maxblocks
= (maxleafents
+ minleafrecs
- 1) / minleafrecs
;
90 for (level
= 1; maxblocks
> 1; level
++) {
91 if (maxblocks
<= maxrootrecs
)
94 maxblocks
= (maxblocks
+ minnoderecs
- 1) / minnoderecs
;
96 mp
->m_bm_maxlevels
[whichfork
] = level
;
99 STATIC
int /* error */
101 struct xfs_btree_cur
*cur
,
105 int *stat
) /* success/failure */
107 cur
->bc_rec
.b
.br_startoff
= off
;
108 cur
->bc_rec
.b
.br_startblock
= bno
;
109 cur
->bc_rec
.b
.br_blockcount
= len
;
110 return xfs_btree_lookup(cur
, XFS_LOOKUP_EQ
, stat
);
113 STATIC
int /* error */
115 struct xfs_btree_cur
*cur
,
119 int *stat
) /* success/failure */
121 cur
->bc_rec
.b
.br_startoff
= off
;
122 cur
->bc_rec
.b
.br_startblock
= bno
;
123 cur
->bc_rec
.b
.br_blockcount
= len
;
124 return xfs_btree_lookup(cur
, XFS_LOOKUP_GE
, stat
);
128 * Check if the inode needs to be converted to btree format.
130 static inline bool xfs_bmap_needs_btree(struct xfs_inode
*ip
, int whichfork
)
132 return XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
&&
133 XFS_IFORK_NEXTENTS(ip
, whichfork
) >
134 XFS_IFORK_MAXEXT(ip
, whichfork
);
138 * Check if the inode should be converted to extent format.
140 static inline bool xfs_bmap_wants_extents(struct xfs_inode
*ip
, int whichfork
)
142 return XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
&&
143 XFS_IFORK_NEXTENTS(ip
, whichfork
) <=
144 XFS_IFORK_MAXEXT(ip
, whichfork
);
148 * Update the record referred to by cur to the value given
149 * by [off, bno, len, state].
150 * This either works (return 0) or gets an EFSCORRUPTED error.
154 struct xfs_btree_cur
*cur
,
160 union xfs_btree_rec rec
;
162 xfs_bmbt_disk_set_allf(&rec
.bmbt
, off
, bno
, len
, state
);
163 return xfs_btree_update(cur
, &rec
);
167 * Compute the worst-case number of indirect blocks that will be used
168 * for ip's delayed extent of length "len".
171 xfs_bmap_worst_indlen(
172 xfs_inode_t
*ip
, /* incore inode pointer */
173 xfs_filblks_t len
) /* delayed extent length */
175 int level
; /* btree level number */
176 int maxrecs
; /* maximum record count at this level */
177 xfs_mount_t
*mp
; /* mount structure */
178 xfs_filblks_t rval
; /* return value */
181 maxrecs
= mp
->m_bmap_dmxr
[0];
182 for (level
= 0, rval
= 0;
183 level
< XFS_BM_MAXLEVELS(mp
, XFS_DATA_FORK
);
186 do_div(len
, maxrecs
);
189 return rval
+ XFS_BM_MAXLEVELS(mp
, XFS_DATA_FORK
) -
192 maxrecs
= mp
->m_bmap_dmxr
[1];
198 * Calculate the default attribute fork offset for newly created inodes.
201 xfs_default_attroffset(
202 struct xfs_inode
*ip
)
204 struct xfs_mount
*mp
= ip
->i_mount
;
207 if (mp
->m_sb
.sb_inodesize
== 256) {
208 offset
= XFS_LITINO(mp
, ip
->i_d
.di_version
) -
209 XFS_BMDR_SPACE_CALC(MINABTPTRS
);
211 offset
= XFS_BMDR_SPACE_CALC(6 * MINABTPTRS
);
214 ASSERT(offset
< XFS_LITINO(mp
, ip
->i_d
.di_version
));
219 * Helper routine to reset inode di_forkoff field when switching
220 * attribute fork from local to extent format - we reset it where
221 * possible to make space available for inline data fork extents.
224 xfs_bmap_forkoff_reset(
228 if (whichfork
== XFS_ATTR_FORK
&&
229 ip
->i_d
.di_format
!= XFS_DINODE_FMT_DEV
&&
230 ip
->i_d
.di_format
!= XFS_DINODE_FMT_UUID
&&
231 ip
->i_d
.di_format
!= XFS_DINODE_FMT_BTREE
) {
232 uint dfl_forkoff
= xfs_default_attroffset(ip
) >> 3;
234 if (dfl_forkoff
> ip
->i_d
.di_forkoff
)
235 ip
->i_d
.di_forkoff
= dfl_forkoff
;
240 STATIC
struct xfs_buf
*
242 struct xfs_btree_cur
*cur
,
245 struct xfs_log_item_desc
*lidp
;
251 for (i
= 0; i
< XFS_BTREE_MAXLEVELS
; i
++) {
252 if (!cur
->bc_bufs
[i
])
254 if (XFS_BUF_ADDR(cur
->bc_bufs
[i
]) == bno
)
255 return cur
->bc_bufs
[i
];
258 /* Chase down all the log items to see if the bp is there */
259 list_for_each_entry(lidp
, &cur
->bc_tp
->t_items
, lid_trans
) {
260 struct xfs_buf_log_item
*bip
;
261 bip
= (struct xfs_buf_log_item
*)lidp
->lid_item
;
262 if (bip
->bli_item
.li_type
== XFS_LI_BUF
&&
263 XFS_BUF_ADDR(bip
->bli_buf
) == bno
)
272 struct xfs_btree_block
*block
,
278 __be64
*pp
, *thispa
; /* pointer to block address */
279 xfs_bmbt_key_t
*prevp
, *keyp
;
281 ASSERT(be16_to_cpu(block
->bb_level
) > 0);
284 for( i
= 1; i
<= xfs_btree_get_numrecs(block
); i
++) {
285 dmxr
= mp
->m_bmap_dmxr
[0];
286 keyp
= XFS_BMBT_KEY_ADDR(mp
, block
, i
);
289 ASSERT(be64_to_cpu(prevp
->br_startoff
) <
290 be64_to_cpu(keyp
->br_startoff
));
295 * Compare the block numbers to see if there are dups.
298 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, i
, sz
);
300 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, i
, dmxr
);
302 for (j
= i
+1; j
<= be16_to_cpu(block
->bb_numrecs
); j
++) {
304 thispa
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, j
, sz
);
306 thispa
= XFS_BMBT_PTR_ADDR(mp
, block
, j
, dmxr
);
307 if (*thispa
== *pp
) {
308 xfs_warn(mp
, "%s: thispa(%d) == pp(%d) %Ld",
310 (unsigned long long)be64_to_cpu(*thispa
));
311 panic("%s: ptrs are equal in node\n",
319 * Check that the extents for the inode ip are in the right order in all
320 * btree leaves. THis becomes prohibitively expensive for large extent count
321 * files, so don't bother with inodes that have more than 10,000 extents in
322 * them. The btree record ordering checks will still be done, so for such large
323 * bmapbt constructs that is going to catch most corruptions.
326 xfs_bmap_check_leaf_extents(
327 xfs_btree_cur_t
*cur
, /* btree cursor or null */
328 xfs_inode_t
*ip
, /* incore inode pointer */
329 int whichfork
) /* data or attr fork */
331 struct xfs_btree_block
*block
; /* current btree block */
332 xfs_fsblock_t bno
; /* block # of "block" */
333 xfs_buf_t
*bp
; /* buffer for "block" */
334 int error
; /* error return value */
335 xfs_extnum_t i
=0, j
; /* index into the extents list */
336 xfs_ifork_t
*ifp
; /* fork structure */
337 int level
; /* btree level, for checking */
338 xfs_mount_t
*mp
; /* file system mount structure */
339 __be64
*pp
; /* pointer to block address */
340 xfs_bmbt_rec_t
*ep
; /* pointer to current extent */
341 xfs_bmbt_rec_t last
= {0, 0}; /* last extent in prev block */
342 xfs_bmbt_rec_t
*nextp
; /* pointer to next extent */
345 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
) {
349 /* skip large extent count inodes */
350 if (ip
->i_d
.di_nextents
> 10000)
355 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
356 block
= ifp
->if_broot
;
358 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
360 level
= be16_to_cpu(block
->bb_level
);
362 xfs_check_block(block
, mp
, 1, ifp
->if_broot_bytes
);
363 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, 1, ifp
->if_broot_bytes
);
364 bno
= be64_to_cpu(*pp
);
366 ASSERT(bno
!= NULLFSBLOCK
);
367 ASSERT(XFS_FSB_TO_AGNO(mp
, bno
) < mp
->m_sb
.sb_agcount
);
368 ASSERT(XFS_FSB_TO_AGBNO(mp
, bno
) < mp
->m_sb
.sb_agblocks
);
371 * Go down the tree until leaf level is reached, following the first
372 * pointer (leftmost) at each level.
374 while (level
-- > 0) {
375 /* See if buf is in cur first */
377 bp
= xfs_bmap_get_bp(cur
, XFS_FSB_TO_DADDR(mp
, bno
));
380 error
= xfs_btree_read_bufl(mp
, NULL
, bno
, 0, &bp
,
386 block
= XFS_BUF_TO_BLOCK(bp
);
391 * Check this block for basic sanity (increasing keys and
392 * no duplicate blocks).
395 xfs_check_block(block
, mp
, 0, 0);
396 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, mp
->m_bmap_dmxr
[1]);
397 bno
= be64_to_cpu(*pp
);
398 XFS_WANT_CORRUPTED_GOTO(mp
,
399 XFS_FSB_SANITY_CHECK(mp
, bno
), error0
);
402 xfs_trans_brelse(NULL
, bp
);
407 * Here with bp and block set to the leftmost leaf node in the tree.
412 * Loop over all leaf nodes checking that all extents are in the right order.
415 xfs_fsblock_t nextbno
;
416 xfs_extnum_t num_recs
;
419 num_recs
= xfs_btree_get_numrecs(block
);
422 * Read-ahead the next leaf block, if any.
425 nextbno
= be64_to_cpu(block
->bb_u
.l
.bb_rightsib
);
428 * Check all the extents to make sure they are OK.
429 * If we had a previous block, the last entry should
430 * conform with the first entry in this one.
433 ep
= XFS_BMBT_REC_ADDR(mp
, block
, 1);
435 ASSERT(xfs_bmbt_disk_get_startoff(&last
) +
436 xfs_bmbt_disk_get_blockcount(&last
) <=
437 xfs_bmbt_disk_get_startoff(ep
));
439 for (j
= 1; j
< num_recs
; j
++) {
440 nextp
= XFS_BMBT_REC_ADDR(mp
, block
, j
+ 1);
441 ASSERT(xfs_bmbt_disk_get_startoff(ep
) +
442 xfs_bmbt_disk_get_blockcount(ep
) <=
443 xfs_bmbt_disk_get_startoff(nextp
));
451 xfs_trans_brelse(NULL
, bp
);
455 * If we've reached the end, stop.
457 if (bno
== NULLFSBLOCK
)
461 bp
= xfs_bmap_get_bp(cur
, XFS_FSB_TO_DADDR(mp
, bno
));
464 error
= xfs_btree_read_bufl(mp
, NULL
, bno
, 0, &bp
,
470 block
= XFS_BUF_TO_BLOCK(bp
);
476 xfs_warn(mp
, "%s: at error0", __func__
);
478 xfs_trans_brelse(NULL
, bp
);
480 xfs_warn(mp
, "%s: BAD after btree leaves for %d extents",
482 panic("%s: CORRUPTED BTREE OR SOMETHING", __func__
);
487 * Add bmap trace insert entries for all the contents of the extent records.
490 xfs_bmap_trace_exlist(
491 xfs_inode_t
*ip
, /* incore inode pointer */
492 xfs_extnum_t cnt
, /* count of entries in the list */
493 int whichfork
, /* data or attr fork */
494 unsigned long caller_ip
)
496 xfs_extnum_t idx
; /* extent record index */
497 xfs_ifork_t
*ifp
; /* inode fork pointer */
500 if (whichfork
== XFS_ATTR_FORK
)
501 state
|= BMAP_ATTRFORK
;
503 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
504 ASSERT(cnt
== (ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
)));
505 for (idx
= 0; idx
< cnt
; idx
++)
506 trace_xfs_extlist(ip
, idx
, whichfork
, caller_ip
);
510 * Validate that the bmbt_irecs being returned from bmapi are valid
511 * given the caller's original parameters. Specifically check the
512 * ranges of the returned irecs to ensure that they only extend beyond
513 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
516 xfs_bmap_validate_ret(
520 xfs_bmbt_irec_t
*mval
,
524 int i
; /* index to map values */
526 ASSERT(ret_nmap
<= nmap
);
528 for (i
= 0; i
< ret_nmap
; i
++) {
529 ASSERT(mval
[i
].br_blockcount
> 0);
530 if (!(flags
& XFS_BMAPI_ENTIRE
)) {
531 ASSERT(mval
[i
].br_startoff
>= bno
);
532 ASSERT(mval
[i
].br_blockcount
<= len
);
533 ASSERT(mval
[i
].br_startoff
+ mval
[i
].br_blockcount
<=
536 ASSERT(mval
[i
].br_startoff
< bno
+ len
);
537 ASSERT(mval
[i
].br_startoff
+ mval
[i
].br_blockcount
>
541 mval
[i
- 1].br_startoff
+ mval
[i
- 1].br_blockcount
==
542 mval
[i
].br_startoff
);
543 ASSERT(mval
[i
].br_startblock
!= DELAYSTARTBLOCK
&&
544 mval
[i
].br_startblock
!= HOLESTARTBLOCK
);
545 ASSERT(mval
[i
].br_state
== XFS_EXT_NORM
||
546 mval
[i
].br_state
== XFS_EXT_UNWRITTEN
);
551 #define xfs_bmap_check_leaf_extents(cur, ip, whichfork) do { } while (0)
552 #define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap)
556 * bmap free list manipulation functions
560 * Add the extent to the list of extents to be free at transaction end.
561 * The list is maintained sorted (by block number).
565 struct xfs_mount
*mp
, /* mount point structure */
566 struct xfs_bmap_free
*flist
, /* list of extents */
567 xfs_fsblock_t bno
, /* fs block number of extent */
568 xfs_filblks_t len
) /* length of extent */
570 xfs_bmap_free_item_t
*cur
; /* current (next) element */
571 xfs_bmap_free_item_t
*new; /* new element */
572 xfs_bmap_free_item_t
*prev
; /* previous element */
577 ASSERT(bno
!= NULLFSBLOCK
);
579 ASSERT(len
<= MAXEXTLEN
);
580 ASSERT(!isnullstartblock(bno
));
581 agno
= XFS_FSB_TO_AGNO(mp
, bno
);
582 agbno
= XFS_FSB_TO_AGBNO(mp
, bno
);
583 ASSERT(agno
< mp
->m_sb
.sb_agcount
);
584 ASSERT(agbno
< mp
->m_sb
.sb_agblocks
);
585 ASSERT(len
< mp
->m_sb
.sb_agblocks
);
586 ASSERT(agbno
+ len
<= mp
->m_sb
.sb_agblocks
);
588 ASSERT(xfs_bmap_free_item_zone
!= NULL
);
589 new = kmem_zone_alloc(xfs_bmap_free_item_zone
, KM_SLEEP
);
590 new->xbfi_startblock
= bno
;
591 new->xbfi_blockcount
= (xfs_extlen_t
)len
;
592 for (prev
= NULL
, cur
= flist
->xbf_first
;
594 prev
= cur
, cur
= cur
->xbfi_next
) {
595 if (cur
->xbfi_startblock
>= bno
)
599 prev
->xbfi_next
= new;
601 flist
->xbf_first
= new;
602 new->xbfi_next
= cur
;
607 * Remove the entry "free" from the free item list. Prev points to the
608 * previous entry, unless "free" is the head of the list.
612 xfs_bmap_free_t
*flist
, /* free item list header */
613 xfs_bmap_free_item_t
*prev
, /* previous item on list, if any */
614 xfs_bmap_free_item_t
*free
) /* list item to be freed */
617 prev
->xbfi_next
= free
->xbfi_next
;
619 flist
->xbf_first
= free
->xbfi_next
;
621 kmem_zone_free(xfs_bmap_free_item_zone
, free
);
625 * Free up any items left in the list.
629 xfs_bmap_free_t
*flist
) /* list of bmap_free_items */
631 xfs_bmap_free_item_t
*free
; /* free list item */
632 xfs_bmap_free_item_t
*next
;
634 if (flist
->xbf_count
== 0)
636 ASSERT(flist
->xbf_first
!= NULL
);
637 for (free
= flist
->xbf_first
; free
; free
= next
) {
638 next
= free
->xbfi_next
;
639 xfs_bmap_del_free(flist
, NULL
, free
);
641 ASSERT(flist
->xbf_count
== 0);
645 * Inode fork format manipulation functions
649 * Transform a btree format file with only one leaf node, where the
650 * extents list will fit in the inode, into an extents format file.
651 * Since the file extents are already in-core, all we have to do is
652 * give up the space for the btree root and pitch the leaf block.
654 STATIC
int /* error */
655 xfs_bmap_btree_to_extents(
656 xfs_trans_t
*tp
, /* transaction pointer */
657 xfs_inode_t
*ip
, /* incore inode pointer */
658 xfs_btree_cur_t
*cur
, /* btree cursor */
659 int *logflagsp
, /* inode logging flags */
660 int whichfork
) /* data or attr fork */
663 struct xfs_btree_block
*cblock
;/* child btree block */
664 xfs_fsblock_t cbno
; /* child block number */
665 xfs_buf_t
*cbp
; /* child block's buffer */
666 int error
; /* error return value */
667 xfs_ifork_t
*ifp
; /* inode fork data */
668 xfs_mount_t
*mp
; /* mount point structure */
669 __be64
*pp
; /* ptr to block address */
670 struct xfs_btree_block
*rblock
;/* root btree block */
673 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
674 ASSERT(ifp
->if_flags
& XFS_IFEXTENTS
);
675 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
);
676 rblock
= ifp
->if_broot
;
677 ASSERT(be16_to_cpu(rblock
->bb_level
) == 1);
678 ASSERT(be16_to_cpu(rblock
->bb_numrecs
) == 1);
679 ASSERT(xfs_bmbt_maxrecs(mp
, ifp
->if_broot_bytes
, 0) == 1);
680 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, rblock
, 1, ifp
->if_broot_bytes
);
681 cbno
= be64_to_cpu(*pp
);
684 if ((error
= xfs_btree_check_lptr(cur
, cbno
, 1)))
687 error
= xfs_btree_read_bufl(mp
, tp
, cbno
, 0, &cbp
, XFS_BMAP_BTREE_REF
,
691 cblock
= XFS_BUF_TO_BLOCK(cbp
);
692 if ((error
= xfs_btree_check_block(cur
, cblock
, 0, cbp
)))
694 xfs_bmap_add_free(mp
, cur
->bc_private
.b
.flist
, cbno
, 1);
695 ip
->i_d
.di_nblocks
--;
696 xfs_trans_mod_dquot_byino(tp
, ip
, XFS_TRANS_DQ_BCOUNT
, -1L);
697 xfs_trans_binval(tp
, cbp
);
698 if (cur
->bc_bufs
[0] == cbp
)
699 cur
->bc_bufs
[0] = NULL
;
700 xfs_iroot_realloc(ip
, -1, whichfork
);
701 ASSERT(ifp
->if_broot
== NULL
);
702 ASSERT((ifp
->if_flags
& XFS_IFBROOT
) == 0);
703 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_EXTENTS
);
704 *logflagsp
= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
709 * Convert an extents-format file into a btree-format file.
710 * The new file will have a root block (in the inode) and a single child block.
712 STATIC
int /* error */
713 xfs_bmap_extents_to_btree(
714 xfs_trans_t
*tp
, /* transaction pointer */
715 xfs_inode_t
*ip
, /* incore inode pointer */
716 xfs_fsblock_t
*firstblock
, /* first-block-allocated */
717 xfs_bmap_free_t
*flist
, /* blocks freed in xaction */
718 xfs_btree_cur_t
**curp
, /* cursor returned to caller */
719 int wasdel
, /* converting a delayed alloc */
720 int *logflagsp
, /* inode logging flags */
721 int whichfork
) /* data or attr fork */
723 struct xfs_btree_block
*ablock
; /* allocated (child) bt block */
724 xfs_buf_t
*abp
; /* buffer for ablock */
725 xfs_alloc_arg_t args
; /* allocation arguments */
726 xfs_bmbt_rec_t
*arp
; /* child record pointer */
727 struct xfs_btree_block
*block
; /* btree root block */
728 xfs_btree_cur_t
*cur
; /* bmap btree cursor */
729 xfs_bmbt_rec_host_t
*ep
; /* extent record pointer */
730 int error
; /* error return value */
731 xfs_extnum_t i
, cnt
; /* extent record index */
732 xfs_ifork_t
*ifp
; /* inode fork pointer */
733 xfs_bmbt_key_t
*kp
; /* root block key pointer */
734 xfs_mount_t
*mp
; /* mount structure */
735 xfs_extnum_t nextents
; /* number of file extents */
736 xfs_bmbt_ptr_t
*pp
; /* root block address pointer */
739 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
740 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
);
743 * Make space in the inode incore.
745 xfs_iroot_realloc(ip
, 1, whichfork
);
746 ifp
->if_flags
|= XFS_IFBROOT
;
751 block
= ifp
->if_broot
;
752 if (xfs_sb_version_hascrc(&mp
->m_sb
))
753 xfs_btree_init_block_int(mp
, block
, XFS_BUF_DADDR_NULL
,
754 XFS_BMAP_CRC_MAGIC
, 1, 1, ip
->i_ino
,
755 XFS_BTREE_LONG_PTRS
| XFS_BTREE_CRC_BLOCKS
);
757 xfs_btree_init_block_int(mp
, block
, XFS_BUF_DADDR_NULL
,
758 XFS_BMAP_MAGIC
, 1, 1, ip
->i_ino
,
759 XFS_BTREE_LONG_PTRS
);
762 * Need a cursor. Can't allocate until bb_level is filled in.
764 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
765 cur
->bc_private
.b
.firstblock
= *firstblock
;
766 cur
->bc_private
.b
.flist
= flist
;
767 cur
->bc_private
.b
.flags
= wasdel
? XFS_BTCUR_BPRV_WASDEL
: 0;
769 * Convert to a btree with two levels, one record in root.
771 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_BTREE
);
772 memset(&args
, 0, sizeof(args
));
775 args
.firstblock
= *firstblock
;
776 if (*firstblock
== NULLFSBLOCK
) {
777 args
.type
= XFS_ALLOCTYPE_START_BNO
;
778 args
.fsbno
= XFS_INO_TO_FSB(mp
, ip
->i_ino
);
779 } else if (flist
->xbf_low
) {
780 args
.type
= XFS_ALLOCTYPE_START_BNO
;
781 args
.fsbno
= *firstblock
;
783 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
784 args
.fsbno
= *firstblock
;
786 args
.minlen
= args
.maxlen
= args
.prod
= 1;
787 args
.wasdel
= wasdel
;
789 if ((error
= xfs_alloc_vextent(&args
))) {
790 xfs_iroot_realloc(ip
, -1, whichfork
);
791 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
795 * Allocation can't fail, the space was reserved.
797 ASSERT(args
.fsbno
!= NULLFSBLOCK
);
798 ASSERT(*firstblock
== NULLFSBLOCK
||
799 args
.agno
== XFS_FSB_TO_AGNO(mp
, *firstblock
) ||
801 args
.agno
> XFS_FSB_TO_AGNO(mp
, *firstblock
)));
802 *firstblock
= cur
->bc_private
.b
.firstblock
= args
.fsbno
;
803 cur
->bc_private
.b
.allocated
++;
804 ip
->i_d
.di_nblocks
++;
805 xfs_trans_mod_dquot_byino(tp
, ip
, XFS_TRANS_DQ_BCOUNT
, 1L);
806 abp
= xfs_btree_get_bufl(mp
, tp
, args
.fsbno
, 0);
808 * Fill in the child block.
810 abp
->b_ops
= &xfs_bmbt_buf_ops
;
811 ablock
= XFS_BUF_TO_BLOCK(abp
);
812 if (xfs_sb_version_hascrc(&mp
->m_sb
))
813 xfs_btree_init_block_int(mp
, ablock
, abp
->b_bn
,
814 XFS_BMAP_CRC_MAGIC
, 0, 0, ip
->i_ino
,
815 XFS_BTREE_LONG_PTRS
| XFS_BTREE_CRC_BLOCKS
);
817 xfs_btree_init_block_int(mp
, ablock
, abp
->b_bn
,
818 XFS_BMAP_MAGIC
, 0, 0, ip
->i_ino
,
819 XFS_BTREE_LONG_PTRS
);
821 arp
= XFS_BMBT_REC_ADDR(mp
, ablock
, 1);
822 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
823 for (cnt
= i
= 0; i
< nextents
; i
++) {
824 ep
= xfs_iext_get_ext(ifp
, i
);
825 if (!isnullstartblock(xfs_bmbt_get_startblock(ep
))) {
826 arp
->l0
= cpu_to_be64(ep
->l0
);
827 arp
->l1
= cpu_to_be64(ep
->l1
);
831 ASSERT(cnt
== XFS_IFORK_NEXTENTS(ip
, whichfork
));
832 xfs_btree_set_numrecs(ablock
, cnt
);
835 * Fill in the root key and pointer.
837 kp
= XFS_BMBT_KEY_ADDR(mp
, block
, 1);
838 arp
= XFS_BMBT_REC_ADDR(mp
, ablock
, 1);
839 kp
->br_startoff
= cpu_to_be64(xfs_bmbt_disk_get_startoff(arp
));
840 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, xfs_bmbt_get_maxrecs(cur
,
841 be16_to_cpu(block
->bb_level
)));
842 *pp
= cpu_to_be64(args
.fsbno
);
845 * Do all this logging at the end so that
846 * the root is at the right level.
848 xfs_btree_log_block(cur
, abp
, XFS_BB_ALL_BITS
);
849 xfs_btree_log_recs(cur
, abp
, 1, be16_to_cpu(ablock
->bb_numrecs
));
850 ASSERT(*curp
== NULL
);
852 *logflagsp
= XFS_ILOG_CORE
| xfs_ilog_fbroot(whichfork
);
857 * Convert a local file to an extents file.
858 * This code is out of bounds for data forks of regular files,
859 * since the file data needs to get logged so things will stay consistent.
860 * (The bmap-level manipulations are ok, though).
863 xfs_bmap_local_to_extents_empty(
864 struct xfs_inode
*ip
,
867 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
869 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
870 ASSERT(ifp
->if_bytes
== 0);
871 ASSERT(XFS_IFORK_NEXTENTS(ip
, whichfork
) == 0);
873 xfs_bmap_forkoff_reset(ip
, whichfork
);
874 ifp
->if_flags
&= ~XFS_IFINLINE
;
875 ifp
->if_flags
|= XFS_IFEXTENTS
;
876 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_EXTENTS
);
880 STATIC
int /* error */
881 xfs_bmap_local_to_extents(
882 xfs_trans_t
*tp
, /* transaction pointer */
883 xfs_inode_t
*ip
, /* incore inode pointer */
884 xfs_fsblock_t
*firstblock
, /* first block allocated in xaction */
885 xfs_extlen_t total
, /* total blocks needed by transaction */
886 int *logflagsp
, /* inode logging flags */
888 void (*init_fn
)(struct xfs_trans
*tp
,
890 struct xfs_inode
*ip
,
891 struct xfs_ifork
*ifp
))
894 int flags
; /* logging flags returned */
895 xfs_ifork_t
*ifp
; /* inode fork pointer */
896 xfs_alloc_arg_t args
; /* allocation arguments */
897 xfs_buf_t
*bp
; /* buffer for extent block */
898 xfs_bmbt_rec_host_t
*ep
; /* extent record pointer */
901 * We don't want to deal with the case of keeping inode data inline yet.
902 * So sending the data fork of a regular inode is invalid.
904 ASSERT(!(S_ISREG(VFS_I(ip
)->i_mode
) && whichfork
== XFS_DATA_FORK
));
905 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
906 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
908 if (!ifp
->if_bytes
) {
909 xfs_bmap_local_to_extents_empty(ip
, whichfork
);
910 flags
= XFS_ILOG_CORE
;
916 ASSERT((ifp
->if_flags
& (XFS_IFINLINE
|XFS_IFEXTENTS
|XFS_IFEXTIREC
)) ==
918 memset(&args
, 0, sizeof(args
));
920 args
.mp
= ip
->i_mount
;
921 args
.firstblock
= *firstblock
;
923 * Allocate a block. We know we need only one, since the
924 * file currently fits in an inode.
926 if (*firstblock
== NULLFSBLOCK
) {
927 args
.fsbno
= XFS_INO_TO_FSB(args
.mp
, ip
->i_ino
);
928 args
.type
= XFS_ALLOCTYPE_START_BNO
;
930 args
.fsbno
= *firstblock
;
931 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
934 args
.minlen
= args
.maxlen
= args
.prod
= 1;
935 error
= xfs_alloc_vextent(&args
);
939 /* Can't fail, the space was reserved. */
940 ASSERT(args
.fsbno
!= NULLFSBLOCK
);
941 ASSERT(args
.len
== 1);
942 *firstblock
= args
.fsbno
;
943 bp
= xfs_btree_get_bufl(args
.mp
, tp
, args
.fsbno
, 0);
946 * Initialize the block, copy the data and log the remote buffer.
948 * The callout is responsible for logging because the remote format
949 * might differ from the local format and thus we don't know how much to
950 * log here. Note that init_fn must also set the buffer log item type
953 init_fn(tp
, bp
, ip
, ifp
);
955 /* account for the change in fork size */
956 xfs_idata_realloc(ip
, -ifp
->if_bytes
, whichfork
);
957 xfs_bmap_local_to_extents_empty(ip
, whichfork
);
958 flags
|= XFS_ILOG_CORE
;
960 xfs_iext_add(ifp
, 0, 1);
961 ep
= xfs_iext_get_ext(ifp
, 0);
962 xfs_bmbt_set_allf(ep
, 0, args
.fsbno
, 1, XFS_EXT_NORM
);
963 trace_xfs_bmap_post_update(ip
, 0,
964 whichfork
== XFS_ATTR_FORK
? BMAP_ATTRFORK
: 0,
966 XFS_IFORK_NEXT_SET(ip
, whichfork
, 1);
967 ip
->i_d
.di_nblocks
= 1;
968 xfs_trans_mod_dquot_byino(tp
, ip
,
969 XFS_TRANS_DQ_BCOUNT
, 1L);
970 flags
|= xfs_ilog_fext(whichfork
);
978 * Called from xfs_bmap_add_attrfork to handle btree format files.
980 STATIC
int /* error */
981 xfs_bmap_add_attrfork_btree(
982 xfs_trans_t
*tp
, /* transaction pointer */
983 xfs_inode_t
*ip
, /* incore inode pointer */
984 xfs_fsblock_t
*firstblock
, /* first block allocated */
985 xfs_bmap_free_t
*flist
, /* blocks to free at commit */
986 int *flags
) /* inode logging flags */
988 xfs_btree_cur_t
*cur
; /* btree cursor */
989 int error
; /* error return value */
990 xfs_mount_t
*mp
; /* file system mount struct */
991 int stat
; /* newroot status */
994 if (ip
->i_df
.if_broot_bytes
<= XFS_IFORK_DSIZE(ip
))
995 *flags
|= XFS_ILOG_DBROOT
;
997 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, XFS_DATA_FORK
);
998 cur
->bc_private
.b
.flist
= flist
;
999 cur
->bc_private
.b
.firstblock
= *firstblock
;
1000 if ((error
= xfs_bmbt_lookup_ge(cur
, 0, 0, 0, &stat
)))
1002 /* must be at least one entry */
1003 XFS_WANT_CORRUPTED_GOTO(mp
, stat
== 1, error0
);
1004 if ((error
= xfs_btree_new_iroot(cur
, flags
, &stat
)))
1007 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
1010 *firstblock
= cur
->bc_private
.b
.firstblock
;
1011 cur
->bc_private
.b
.allocated
= 0;
1012 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
1016 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
1021 * Called from xfs_bmap_add_attrfork to handle extents format files.
1023 STATIC
int /* error */
1024 xfs_bmap_add_attrfork_extents(
1025 xfs_trans_t
*tp
, /* transaction pointer */
1026 xfs_inode_t
*ip
, /* incore inode pointer */
1027 xfs_fsblock_t
*firstblock
, /* first block allocated */
1028 xfs_bmap_free_t
*flist
, /* blocks to free at commit */
1029 int *flags
) /* inode logging flags */
1031 xfs_btree_cur_t
*cur
; /* bmap btree cursor */
1032 int error
; /* error return value */
1034 if (ip
->i_d
.di_nextents
* sizeof(xfs_bmbt_rec_t
) <= XFS_IFORK_DSIZE(ip
))
1037 error
= xfs_bmap_extents_to_btree(tp
, ip
, firstblock
, flist
, &cur
, 0,
1038 flags
, XFS_DATA_FORK
);
1040 cur
->bc_private
.b
.allocated
= 0;
1041 xfs_btree_del_cursor(cur
,
1042 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
1048 * Called from xfs_bmap_add_attrfork to handle local format files. Each
1049 * different data fork content type needs a different callout to do the
1050 * conversion. Some are basic and only require special block initialisation
1051 * callouts for the data formating, others (directories) are so specialised they
1052 * handle everything themselves.
1054 * XXX (dgc): investigate whether directory conversion can use the generic
1055 * formatting callout. It should be possible - it's just a very complex
1058 STATIC
int /* error */
1059 xfs_bmap_add_attrfork_local(
1060 xfs_trans_t
*tp
, /* transaction pointer */
1061 xfs_inode_t
*ip
, /* incore inode pointer */
1062 xfs_fsblock_t
*firstblock
, /* first block allocated */
1063 xfs_bmap_free_t
*flist
, /* blocks to free at commit */
1064 int *flags
) /* inode logging flags */
1066 xfs_da_args_t dargs
; /* args for dir/attr code */
1068 if (ip
->i_df
.if_bytes
<= XFS_IFORK_DSIZE(ip
))
1071 if (S_ISDIR(VFS_I(ip
)->i_mode
)) {
1072 memset(&dargs
, 0, sizeof(dargs
));
1073 dargs
.geo
= ip
->i_mount
->m_dir_geo
;
1075 dargs
.firstblock
= firstblock
;
1076 dargs
.flist
= flist
;
1077 dargs
.total
= dargs
.geo
->fsbcount
;
1078 dargs
.whichfork
= XFS_DATA_FORK
;
1080 return xfs_dir2_sf_to_block(&dargs
);
1083 if (S_ISLNK(VFS_I(ip
)->i_mode
))
1084 return xfs_bmap_local_to_extents(tp
, ip
, firstblock
, 1,
1085 flags
, XFS_DATA_FORK
,
1086 xfs_symlink_local_to_remote
);
1088 /* should only be called for types that support local format data */
1090 return -EFSCORRUPTED
;
1094 * Convert inode from non-attributed to attributed.
1095 * Must not be in a transaction, ip must not be locked.
1097 int /* error code */
1098 xfs_bmap_add_attrfork(
1099 xfs_inode_t
*ip
, /* incore inode pointer */
1100 int size
, /* space new attribute needs */
1101 int rsvd
) /* xact may use reserved blks */
1103 xfs_fsblock_t firstblock
; /* 1st block/ag allocated */
1104 xfs_bmap_free_t flist
; /* freed extent records */
1105 xfs_mount_t
*mp
; /* mount structure */
1106 xfs_trans_t
*tp
; /* transaction pointer */
1107 int blks
; /* space reservation */
1108 int version
= 1; /* superblock attr version */
1109 int logflags
; /* logging flags */
1110 int error
; /* error return value */
1112 ASSERT(XFS_IFORK_Q(ip
) == 0);
1115 ASSERT(!XFS_NOT_DQATTACHED(mp
, ip
));
1117 blks
= XFS_ADDAFORK_SPACE_RES(mp
);
1119 error
= xfs_trans_alloc(mp
, &M_RES(mp
)->tr_addafork
, blks
, 0,
1120 rsvd
? XFS_TRANS_RESERVE
: 0, &tp
);
1124 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
1125 error
= xfs_trans_reserve_quota_nblks(tp
, ip
, blks
, 0, rsvd
?
1126 XFS_QMOPT_RES_REGBLKS
| XFS_QMOPT_FORCE_RES
:
1127 XFS_QMOPT_RES_REGBLKS
);
1130 if (XFS_IFORK_Q(ip
))
1132 if (ip
->i_d
.di_aformat
!= XFS_DINODE_FMT_EXTENTS
) {
1134 * For inodes coming from pre-6.2 filesystems.
1136 ASSERT(ip
->i_d
.di_aformat
== 0);
1137 ip
->i_d
.di_aformat
= XFS_DINODE_FMT_EXTENTS
;
1139 ASSERT(ip
->i_d
.di_anextents
== 0);
1141 xfs_trans_ijoin(tp
, ip
, 0);
1142 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
1144 switch (ip
->i_d
.di_format
) {
1145 case XFS_DINODE_FMT_DEV
:
1146 ip
->i_d
.di_forkoff
= roundup(sizeof(xfs_dev_t
), 8) >> 3;
1148 case XFS_DINODE_FMT_UUID
:
1149 ip
->i_d
.di_forkoff
= roundup(sizeof(uuid_t
), 8) >> 3;
1151 case XFS_DINODE_FMT_LOCAL
:
1152 case XFS_DINODE_FMT_EXTENTS
:
1153 case XFS_DINODE_FMT_BTREE
:
1154 ip
->i_d
.di_forkoff
= xfs_attr_shortform_bytesfit(ip
, size
);
1155 if (!ip
->i_d
.di_forkoff
)
1156 ip
->i_d
.di_forkoff
= xfs_default_attroffset(ip
) >> 3;
1157 else if (mp
->m_flags
& XFS_MOUNT_ATTR2
)
1166 ASSERT(ip
->i_afp
== NULL
);
1167 ip
->i_afp
= kmem_zone_zalloc(xfs_ifork_zone
, KM_SLEEP
);
1168 ip
->i_afp
->if_flags
= XFS_IFEXTENTS
;
1170 xfs_bmap_init(&flist
, &firstblock
);
1171 switch (ip
->i_d
.di_format
) {
1172 case XFS_DINODE_FMT_LOCAL
:
1173 error
= xfs_bmap_add_attrfork_local(tp
, ip
, &firstblock
, &flist
,
1176 case XFS_DINODE_FMT_EXTENTS
:
1177 error
= xfs_bmap_add_attrfork_extents(tp
, ip
, &firstblock
,
1180 case XFS_DINODE_FMT_BTREE
:
1181 error
= xfs_bmap_add_attrfork_btree(tp
, ip
, &firstblock
, &flist
,
1189 xfs_trans_log_inode(tp
, ip
, logflags
);
1192 if (!xfs_sb_version_hasattr(&mp
->m_sb
) ||
1193 (!xfs_sb_version_hasattr2(&mp
->m_sb
) && version
== 2)) {
1194 bool log_sb
= false;
1196 spin_lock(&mp
->m_sb_lock
);
1197 if (!xfs_sb_version_hasattr(&mp
->m_sb
)) {
1198 xfs_sb_version_addattr(&mp
->m_sb
);
1201 if (!xfs_sb_version_hasattr2(&mp
->m_sb
) && version
== 2) {
1202 xfs_sb_version_addattr2(&mp
->m_sb
);
1205 spin_unlock(&mp
->m_sb_lock
);
1210 error
= xfs_bmap_finish(&tp
, &flist
, NULL
);
1213 error
= xfs_trans_commit(tp
);
1214 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
1218 xfs_bmap_cancel(&flist
);
1220 xfs_trans_cancel(tp
);
1221 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
1226 * Internal and external extent tree search functions.
1230 * Read in the extents to if_extents.
1231 * All inode fields are set up by caller, we just traverse the btree
1232 * and copy the records in. If the file system cannot contain unwritten
1233 * extents, the records are checked for no "state" flags.
1236 xfs_bmap_read_extents(
1237 xfs_trans_t
*tp
, /* transaction pointer */
1238 xfs_inode_t
*ip
, /* incore inode */
1239 int whichfork
) /* data or attr fork */
1241 struct xfs_btree_block
*block
; /* current btree block */
1242 xfs_fsblock_t bno
; /* block # of "block" */
1243 xfs_buf_t
*bp
; /* buffer for "block" */
1244 int error
; /* error return value */
1245 xfs_exntfmt_t exntf
; /* XFS_EXTFMT_NOSTATE, if checking */
1246 xfs_extnum_t i
, j
; /* index into the extents list */
1247 xfs_ifork_t
*ifp
; /* fork structure */
1248 int level
; /* btree level, for checking */
1249 xfs_mount_t
*mp
; /* file system mount structure */
1250 __be64
*pp
; /* pointer to block address */
1252 xfs_extnum_t room
; /* number of entries there's room for */
1256 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1257 exntf
= (whichfork
!= XFS_DATA_FORK
) ? XFS_EXTFMT_NOSTATE
:
1258 XFS_EXTFMT_INODE(ip
);
1259 block
= ifp
->if_broot
;
1261 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
1263 level
= be16_to_cpu(block
->bb_level
);
1265 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, 1, ifp
->if_broot_bytes
);
1266 bno
= be64_to_cpu(*pp
);
1267 ASSERT(bno
!= NULLFSBLOCK
);
1268 ASSERT(XFS_FSB_TO_AGNO(mp
, bno
) < mp
->m_sb
.sb_agcount
);
1269 ASSERT(XFS_FSB_TO_AGBNO(mp
, bno
) < mp
->m_sb
.sb_agblocks
);
1271 * Go down the tree until leaf level is reached, following the first
1272 * pointer (leftmost) at each level.
1274 while (level
-- > 0) {
1275 error
= xfs_btree_read_bufl(mp
, tp
, bno
, 0, &bp
,
1276 XFS_BMAP_BTREE_REF
, &xfs_bmbt_buf_ops
);
1279 block
= XFS_BUF_TO_BLOCK(bp
);
1282 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, mp
->m_bmap_dmxr
[1]);
1283 bno
= be64_to_cpu(*pp
);
1284 XFS_WANT_CORRUPTED_GOTO(mp
,
1285 XFS_FSB_SANITY_CHECK(mp
, bno
), error0
);
1286 xfs_trans_brelse(tp
, bp
);
1289 * Here with bp and block set to the leftmost leaf node in the tree.
1291 room
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
1294 * Loop over all leaf nodes. Copy information to the extent records.
1297 xfs_bmbt_rec_t
*frp
;
1298 xfs_fsblock_t nextbno
;
1299 xfs_extnum_t num_recs
;
1302 num_recs
= xfs_btree_get_numrecs(block
);
1303 if (unlikely(i
+ num_recs
> room
)) {
1304 ASSERT(i
+ num_recs
<= room
);
1305 xfs_warn(ip
->i_mount
,
1306 "corrupt dinode %Lu, (btree extents).",
1307 (unsigned long long) ip
->i_ino
);
1308 XFS_CORRUPTION_ERROR("xfs_bmap_read_extents(1)",
1309 XFS_ERRLEVEL_LOW
, ip
->i_mount
, block
);
1313 * Read-ahead the next leaf block, if any.
1315 nextbno
= be64_to_cpu(block
->bb_u
.l
.bb_rightsib
);
1316 if (nextbno
!= NULLFSBLOCK
)
1317 xfs_btree_reada_bufl(mp
, nextbno
, 1,
1320 * Copy records into the extent records.
1322 frp
= XFS_BMBT_REC_ADDR(mp
, block
, 1);
1324 for (j
= 0; j
< num_recs
; j
++, i
++, frp
++) {
1325 xfs_bmbt_rec_host_t
*trp
= xfs_iext_get_ext(ifp
, i
);
1326 trp
->l0
= be64_to_cpu(frp
->l0
);
1327 trp
->l1
= be64_to_cpu(frp
->l1
);
1329 if (exntf
== XFS_EXTFMT_NOSTATE
) {
1331 * Check all attribute bmap btree records and
1332 * any "older" data bmap btree records for a
1333 * set bit in the "extent flag" position.
1335 if (unlikely(xfs_check_nostate_extents(ifp
,
1336 start
, num_recs
))) {
1337 XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
1343 xfs_trans_brelse(tp
, bp
);
1346 * If we've reached the end, stop.
1348 if (bno
== NULLFSBLOCK
)
1350 error
= xfs_btree_read_bufl(mp
, tp
, bno
, 0, &bp
,
1351 XFS_BMAP_BTREE_REF
, &xfs_bmbt_buf_ops
);
1354 block
= XFS_BUF_TO_BLOCK(bp
);
1356 ASSERT(i
== (ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
)));
1357 ASSERT(i
== XFS_IFORK_NEXTENTS(ip
, whichfork
));
1358 XFS_BMAP_TRACE_EXLIST(ip
, i
, whichfork
);
1361 xfs_trans_brelse(tp
, bp
);
1362 return -EFSCORRUPTED
;
1367 * Search the extent records for the entry containing block bno.
1368 * If bno lies in a hole, point to the next entry. If bno lies
1369 * past eof, *eofp will be set, and *prevp will contain the last
1370 * entry (null if none). Else, *lastxp will be set to the index
1371 * of the found entry; *gotp will contain the entry.
1373 STATIC xfs_bmbt_rec_host_t
* /* pointer to found extent entry */
1374 xfs_bmap_search_multi_extents(
1375 xfs_ifork_t
*ifp
, /* inode fork pointer */
1376 xfs_fileoff_t bno
, /* block number searched for */
1377 int *eofp
, /* out: end of file found */
1378 xfs_extnum_t
*lastxp
, /* out: last extent index */
1379 xfs_bmbt_irec_t
*gotp
, /* out: extent entry found */
1380 xfs_bmbt_irec_t
*prevp
) /* out: previous extent entry found */
1382 xfs_bmbt_rec_host_t
*ep
; /* extent record pointer */
1383 xfs_extnum_t lastx
; /* last extent index */
1386 * Initialize the extent entry structure to catch access to
1387 * uninitialized br_startblock field.
1389 gotp
->br_startoff
= 0xffa5a5a5a5a5a5a5LL
;
1390 gotp
->br_blockcount
= 0xa55a5a5a5a5a5a5aLL
;
1391 gotp
->br_state
= XFS_EXT_INVALID
;
1392 gotp
->br_startblock
= 0xffffa5a5a5a5a5a5LL
;
1393 prevp
->br_startoff
= NULLFILEOFF
;
1395 ep
= xfs_iext_bno_to_ext(ifp
, bno
, &lastx
);
1397 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, lastx
- 1), prevp
);
1399 if (lastx
< (ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
))) {
1400 xfs_bmbt_get_all(ep
, gotp
);
1414 * Search the extents list for the inode, for the extent containing bno.
1415 * If bno lies in a hole, point to the next entry. If bno lies past eof,
1416 * *eofp will be set, and *prevp will contain the last entry (null if none).
1417 * Else, *lastxp will be set to the index of the found
1418 * entry; *gotp will contain the entry.
1420 xfs_bmbt_rec_host_t
* /* pointer to found extent entry */
1421 xfs_bmap_search_extents(
1422 xfs_inode_t
*ip
, /* incore inode pointer */
1423 xfs_fileoff_t bno
, /* block number searched for */
1424 int fork
, /* data or attr fork */
1425 int *eofp
, /* out: end of file found */
1426 xfs_extnum_t
*lastxp
, /* out: last extent index */
1427 xfs_bmbt_irec_t
*gotp
, /* out: extent entry found */
1428 xfs_bmbt_irec_t
*prevp
) /* out: previous extent entry found */
1430 xfs_ifork_t
*ifp
; /* inode fork pointer */
1431 xfs_bmbt_rec_host_t
*ep
; /* extent record pointer */
1433 XFS_STATS_INC(ip
->i_mount
, xs_look_exlist
);
1434 ifp
= XFS_IFORK_PTR(ip
, fork
);
1436 ep
= xfs_bmap_search_multi_extents(ifp
, bno
, eofp
, lastxp
, gotp
, prevp
);
1438 if (unlikely(!(gotp
->br_startblock
) && (*lastxp
!= NULLEXTNUM
) &&
1439 !(XFS_IS_REALTIME_INODE(ip
) && fork
== XFS_DATA_FORK
))) {
1440 xfs_alert_tag(ip
->i_mount
, XFS_PTAG_FSBLOCK_ZERO
,
1441 "Access to block zero in inode %llu "
1442 "start_block: %llx start_off: %llx "
1443 "blkcnt: %llx extent-state: %x lastx: %x",
1444 (unsigned long long)ip
->i_ino
,
1445 (unsigned long long)gotp
->br_startblock
,
1446 (unsigned long long)gotp
->br_startoff
,
1447 (unsigned long long)gotp
->br_blockcount
,
1448 gotp
->br_state
, *lastxp
);
1449 *lastxp
= NULLEXTNUM
;
1457 * Returns the file-relative block number of the first unused block(s)
1458 * in the file with at least "len" logically contiguous blocks free.
1459 * This is the lowest-address hole if the file has holes, else the first block
1460 * past the end of file.
1461 * Return 0 if the file is currently local (in-inode).
1464 xfs_bmap_first_unused(
1465 xfs_trans_t
*tp
, /* transaction pointer */
1466 xfs_inode_t
*ip
, /* incore inode */
1467 xfs_extlen_t len
, /* size of hole to find */
1468 xfs_fileoff_t
*first_unused
, /* unused block */
1469 int whichfork
) /* data or attr fork */
1471 int error
; /* error return value */
1472 int idx
; /* extent record index */
1473 xfs_ifork_t
*ifp
; /* inode fork pointer */
1474 xfs_fileoff_t lastaddr
; /* last block number seen */
1475 xfs_fileoff_t lowest
; /* lowest useful block */
1476 xfs_fileoff_t max
; /* starting useful block */
1477 xfs_fileoff_t off
; /* offset for this block */
1478 xfs_extnum_t nextents
; /* number of extent entries */
1480 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
||
1481 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
||
1482 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
1483 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
) {
1487 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1488 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
1489 (error
= xfs_iread_extents(tp
, ip
, whichfork
)))
1491 lowest
= *first_unused
;
1492 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
1493 for (idx
= 0, lastaddr
= 0, max
= lowest
; idx
< nextents
; idx
++) {
1494 xfs_bmbt_rec_host_t
*ep
= xfs_iext_get_ext(ifp
, idx
);
1495 off
= xfs_bmbt_get_startoff(ep
);
1497 * See if the hole before this extent will work.
1499 if (off
>= lowest
+ len
&& off
- max
>= len
) {
1500 *first_unused
= max
;
1503 lastaddr
= off
+ xfs_bmbt_get_blockcount(ep
);
1504 max
= XFS_FILEOFF_MAX(lastaddr
, lowest
);
1506 *first_unused
= max
;
1511 * Returns the file-relative block number of the last block - 1 before
1512 * last_block (input value) in the file.
1513 * This is not based on i_size, it is based on the extent records.
1514 * Returns 0 for local files, as they do not have extent records.
1517 xfs_bmap_last_before(
1518 xfs_trans_t
*tp
, /* transaction pointer */
1519 xfs_inode_t
*ip
, /* incore inode */
1520 xfs_fileoff_t
*last_block
, /* last block */
1521 int whichfork
) /* data or attr fork */
1523 xfs_fileoff_t bno
; /* input file offset */
1524 int eof
; /* hit end of file */
1525 xfs_bmbt_rec_host_t
*ep
; /* pointer to last extent */
1526 int error
; /* error return value */
1527 xfs_bmbt_irec_t got
; /* current extent value */
1528 xfs_ifork_t
*ifp
; /* inode fork pointer */
1529 xfs_extnum_t lastx
; /* last extent used */
1530 xfs_bmbt_irec_t prev
; /* previous extent value */
1532 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
&&
1533 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
1534 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_LOCAL
)
1536 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
) {
1540 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1541 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
1542 (error
= xfs_iread_extents(tp
, ip
, whichfork
)))
1544 bno
= *last_block
- 1;
1545 ep
= xfs_bmap_search_extents(ip
, bno
, whichfork
, &eof
, &lastx
, &got
,
1547 if (eof
|| xfs_bmbt_get_startoff(ep
) > bno
) {
1548 if (prev
.br_startoff
== NULLFILEOFF
)
1551 *last_block
= prev
.br_startoff
+ prev
.br_blockcount
;
1554 * Otherwise *last_block is already the right answer.
1560 xfs_bmap_last_extent(
1561 struct xfs_trans
*tp
,
1562 struct xfs_inode
*ip
,
1564 struct xfs_bmbt_irec
*rec
,
1567 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1571 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
1572 error
= xfs_iread_extents(tp
, ip
, whichfork
);
1577 nextents
= ifp
->if_bytes
/ sizeof(xfs_bmbt_rec_t
);
1578 if (nextents
== 0) {
1583 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, nextents
- 1), rec
);
1589 * Check the last inode extent to determine whether this allocation will result
1590 * in blocks being allocated at the end of the file. When we allocate new data
1591 * blocks at the end of the file which do not start at the previous data block,
1592 * we will try to align the new blocks at stripe unit boundaries.
1594 * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be
1595 * at, or past the EOF.
1599 struct xfs_bmalloca
*bma
,
1602 struct xfs_bmbt_irec rec
;
1607 error
= xfs_bmap_last_extent(NULL
, bma
->ip
, whichfork
, &rec
,
1618 * Check if we are allocation or past the last extent, or at least into
1619 * the last delayed allocated extent.
1621 bma
->aeof
= bma
->offset
>= rec
.br_startoff
+ rec
.br_blockcount
||
1622 (bma
->offset
>= rec
.br_startoff
&&
1623 isnullstartblock(rec
.br_startblock
));
1628 * Returns the file-relative block number of the first block past eof in
1629 * the file. This is not based on i_size, it is based on the extent records.
1630 * Returns 0 for local files, as they do not have extent records.
1633 xfs_bmap_last_offset(
1634 struct xfs_inode
*ip
,
1635 xfs_fileoff_t
*last_block
,
1638 struct xfs_bmbt_irec rec
;
1644 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
)
1647 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
&&
1648 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
1651 error
= xfs_bmap_last_extent(NULL
, ip
, whichfork
, &rec
, &is_empty
);
1652 if (error
|| is_empty
)
1655 *last_block
= rec
.br_startoff
+ rec
.br_blockcount
;
1660 * Returns whether the selected fork of the inode has exactly one
1661 * block or not. For the data fork we check this matches di_size,
1662 * implying the file's range is 0..bsize-1.
1664 int /* 1=>1 block, 0=>otherwise */
1666 xfs_inode_t
*ip
, /* incore inode */
1667 int whichfork
) /* data or attr fork */
1669 xfs_bmbt_rec_host_t
*ep
; /* ptr to fork's extent */
1670 xfs_ifork_t
*ifp
; /* inode fork pointer */
1671 int rval
; /* return value */
1672 xfs_bmbt_irec_t s
; /* internal version of extent */
1675 if (whichfork
== XFS_DATA_FORK
)
1676 return XFS_ISIZE(ip
) == ip
->i_mount
->m_sb
.sb_blocksize
;
1678 if (XFS_IFORK_NEXTENTS(ip
, whichfork
) != 1)
1680 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
1682 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1683 ASSERT(ifp
->if_flags
& XFS_IFEXTENTS
);
1684 ep
= xfs_iext_get_ext(ifp
, 0);
1685 xfs_bmbt_get_all(ep
, &s
);
1686 rval
= s
.br_startoff
== 0 && s
.br_blockcount
== 1;
1687 if (rval
&& whichfork
== XFS_DATA_FORK
)
1688 ASSERT(XFS_ISIZE(ip
) == ip
->i_mount
->m_sb
.sb_blocksize
);
1693 * Extent tree manipulation functions used during allocation.
1697 * Convert a delayed allocation to a real allocation.
1699 STATIC
int /* error */
1700 xfs_bmap_add_extent_delay_real(
1701 struct xfs_bmalloca
*bma
)
1703 struct xfs_bmbt_irec
*new = &bma
->got
;
1704 int diff
; /* temp value */
1705 xfs_bmbt_rec_host_t
*ep
; /* extent entry for idx */
1706 int error
; /* error return value */
1707 int i
; /* temp state */
1708 xfs_ifork_t
*ifp
; /* inode fork pointer */
1709 xfs_fileoff_t new_endoff
; /* end offset of new entry */
1710 xfs_bmbt_irec_t r
[3]; /* neighbor extent entries */
1711 /* left is 0, right is 1, prev is 2 */
1712 int rval
=0; /* return value (logging flags) */
1713 int state
= 0;/* state bits, accessed thru macros */
1714 xfs_filblks_t da_new
; /* new count del alloc blocks used */
1715 xfs_filblks_t da_old
; /* old count del alloc blocks used */
1716 xfs_filblks_t temp
=0; /* value for da_new calculations */
1717 xfs_filblks_t temp2
=0;/* value for da_new calculations */
1718 int tmp_rval
; /* partial logging flags */
1719 struct xfs_mount
*mp
;
1720 int whichfork
= XFS_DATA_FORK
;
1722 mp
= bma
->ip
->i_mount
;
1723 ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
1725 ASSERT(bma
->idx
>= 0);
1726 ASSERT(bma
->idx
<= ifp
->if_bytes
/ sizeof(struct xfs_bmbt_rec
));
1727 ASSERT(!isnullstartblock(new->br_startblock
));
1729 (bma
->cur
->bc_private
.b
.flags
& XFS_BTCUR_BPRV_WASDEL
));
1731 XFS_STATS_INC(mp
, xs_add_exlist
);
1738 * Set up a bunch of variables to make the tests simpler.
1740 ep
= xfs_iext_get_ext(ifp
, bma
->idx
);
1741 xfs_bmbt_get_all(ep
, &PREV
);
1742 new_endoff
= new->br_startoff
+ new->br_blockcount
;
1743 ASSERT(PREV
.br_startoff
<= new->br_startoff
);
1744 ASSERT(PREV
.br_startoff
+ PREV
.br_blockcount
>= new_endoff
);
1746 da_old
= startblockval(PREV
.br_startblock
);
1750 * Set flags determining what part of the previous delayed allocation
1751 * extent is being replaced by a real allocation.
1753 if (PREV
.br_startoff
== new->br_startoff
)
1754 state
|= BMAP_LEFT_FILLING
;
1755 if (PREV
.br_startoff
+ PREV
.br_blockcount
== new_endoff
)
1756 state
|= BMAP_RIGHT_FILLING
;
1759 * Check and set flags if this segment has a left neighbor.
1760 * Don't set contiguous if the combined extent would be too large.
1763 state
|= BMAP_LEFT_VALID
;
1764 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
- 1), &LEFT
);
1766 if (isnullstartblock(LEFT
.br_startblock
))
1767 state
|= BMAP_LEFT_DELAY
;
1770 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
1771 LEFT
.br_startoff
+ LEFT
.br_blockcount
== new->br_startoff
&&
1772 LEFT
.br_startblock
+ LEFT
.br_blockcount
== new->br_startblock
&&
1773 LEFT
.br_state
== new->br_state
&&
1774 LEFT
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
1775 state
|= BMAP_LEFT_CONTIG
;
1778 * Check and set flags if this segment has a right neighbor.
1779 * Don't set contiguous if the combined extent would be too large.
1780 * Also check for all-three-contiguous being too large.
1782 if (bma
->idx
< ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
) - 1) {
1783 state
|= BMAP_RIGHT_VALID
;
1784 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
+ 1), &RIGHT
);
1786 if (isnullstartblock(RIGHT
.br_startblock
))
1787 state
|= BMAP_RIGHT_DELAY
;
1790 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
1791 new_endoff
== RIGHT
.br_startoff
&&
1792 new->br_startblock
+ new->br_blockcount
== RIGHT
.br_startblock
&&
1793 new->br_state
== RIGHT
.br_state
&&
1794 new->br_blockcount
+ RIGHT
.br_blockcount
<= MAXEXTLEN
&&
1795 ((state
& (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
1796 BMAP_RIGHT_FILLING
)) !=
1797 (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
1798 BMAP_RIGHT_FILLING
) ||
1799 LEFT
.br_blockcount
+ new->br_blockcount
+ RIGHT
.br_blockcount
1801 state
|= BMAP_RIGHT_CONTIG
;
1805 * Switch out based on the FILLING and CONTIG state bits.
1807 switch (state
& (BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
1808 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
)) {
1809 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
1810 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
1812 * Filling in all of a previously delayed allocation extent.
1813 * The left and right neighbors are both contiguous with new.
1816 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1817 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, bma
->idx
),
1818 LEFT
.br_blockcount
+ PREV
.br_blockcount
+
1819 RIGHT
.br_blockcount
);
1820 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1822 xfs_iext_remove(bma
->ip
, bma
->idx
+ 1, 2, state
);
1823 bma
->ip
->i_d
.di_nextents
--;
1824 if (bma
->cur
== NULL
)
1825 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1827 rval
= XFS_ILOG_CORE
;
1828 error
= xfs_bmbt_lookup_eq(bma
->cur
, RIGHT
.br_startoff
,
1829 RIGHT
.br_startblock
,
1830 RIGHT
.br_blockcount
, &i
);
1833 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1834 error
= xfs_btree_delete(bma
->cur
, &i
);
1837 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1838 error
= xfs_btree_decrement(bma
->cur
, 0, &i
);
1841 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1842 error
= xfs_bmbt_update(bma
->cur
, LEFT
.br_startoff
,
1844 LEFT
.br_blockcount
+
1845 PREV
.br_blockcount
+
1846 RIGHT
.br_blockcount
, LEFT
.br_state
);
1852 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
1854 * Filling in all of a previously delayed allocation extent.
1855 * The left neighbor is contiguous, the right is not.
1859 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1860 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, bma
->idx
),
1861 LEFT
.br_blockcount
+ PREV
.br_blockcount
);
1862 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1864 xfs_iext_remove(bma
->ip
, bma
->idx
+ 1, 1, state
);
1865 if (bma
->cur
== NULL
)
1866 rval
= XFS_ILOG_DEXT
;
1869 error
= xfs_bmbt_lookup_eq(bma
->cur
, LEFT
.br_startoff
,
1870 LEFT
.br_startblock
, LEFT
.br_blockcount
,
1874 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1875 error
= xfs_bmbt_update(bma
->cur
, LEFT
.br_startoff
,
1877 LEFT
.br_blockcount
+
1878 PREV
.br_blockcount
, LEFT
.br_state
);
1884 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
1886 * Filling in all of a previously delayed allocation extent.
1887 * The right neighbor is contiguous, the left is not.
1889 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1890 xfs_bmbt_set_startblock(ep
, new->br_startblock
);
1891 xfs_bmbt_set_blockcount(ep
,
1892 PREV
.br_blockcount
+ RIGHT
.br_blockcount
);
1893 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1895 xfs_iext_remove(bma
->ip
, bma
->idx
+ 1, 1, state
);
1896 if (bma
->cur
== NULL
)
1897 rval
= XFS_ILOG_DEXT
;
1900 error
= xfs_bmbt_lookup_eq(bma
->cur
, RIGHT
.br_startoff
,
1901 RIGHT
.br_startblock
,
1902 RIGHT
.br_blockcount
, &i
);
1905 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1906 error
= xfs_bmbt_update(bma
->cur
, PREV
.br_startoff
,
1908 PREV
.br_blockcount
+
1909 RIGHT
.br_blockcount
, PREV
.br_state
);
1915 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
1917 * Filling in all of a previously delayed allocation extent.
1918 * Neither the left nor right neighbors are contiguous with
1921 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1922 xfs_bmbt_set_startblock(ep
, new->br_startblock
);
1923 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1925 bma
->ip
->i_d
.di_nextents
++;
1926 if (bma
->cur
== NULL
)
1927 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1929 rval
= XFS_ILOG_CORE
;
1930 error
= xfs_bmbt_lookup_eq(bma
->cur
, new->br_startoff
,
1931 new->br_startblock
, new->br_blockcount
,
1935 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
1936 bma
->cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
1937 error
= xfs_btree_insert(bma
->cur
, &i
);
1940 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1944 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
:
1946 * Filling in the first part of a previous delayed allocation.
1947 * The left neighbor is contiguous.
1949 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
- 1, state
, _THIS_IP_
);
1950 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, bma
->idx
- 1),
1951 LEFT
.br_blockcount
+ new->br_blockcount
);
1952 xfs_bmbt_set_startoff(ep
,
1953 PREV
.br_startoff
+ new->br_blockcount
);
1954 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
- 1, state
, _THIS_IP_
);
1956 temp
= PREV
.br_blockcount
- new->br_blockcount
;
1957 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1958 xfs_bmbt_set_blockcount(ep
, temp
);
1959 if (bma
->cur
== NULL
)
1960 rval
= XFS_ILOG_DEXT
;
1963 error
= xfs_bmbt_lookup_eq(bma
->cur
, LEFT
.br_startoff
,
1964 LEFT
.br_startblock
, LEFT
.br_blockcount
,
1968 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1969 error
= xfs_bmbt_update(bma
->cur
, LEFT
.br_startoff
,
1971 LEFT
.br_blockcount
+
1977 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
1978 startblockval(PREV
.br_startblock
));
1979 xfs_bmbt_set_startblock(ep
, nullstartblock(da_new
));
1980 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1985 case BMAP_LEFT_FILLING
:
1987 * Filling in the first part of a previous delayed allocation.
1988 * The left neighbor is not contiguous.
1990 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1991 xfs_bmbt_set_startoff(ep
, new_endoff
);
1992 temp
= PREV
.br_blockcount
- new->br_blockcount
;
1993 xfs_bmbt_set_blockcount(ep
, temp
);
1994 xfs_iext_insert(bma
->ip
, bma
->idx
, 1, new, state
);
1995 bma
->ip
->i_d
.di_nextents
++;
1996 if (bma
->cur
== NULL
)
1997 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1999 rval
= XFS_ILOG_CORE
;
2000 error
= xfs_bmbt_lookup_eq(bma
->cur
, new->br_startoff
,
2001 new->br_startblock
, new->br_blockcount
,
2005 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
2006 bma
->cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
2007 error
= xfs_btree_insert(bma
->cur
, &i
);
2010 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2013 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
2014 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
2015 bma
->firstblock
, bma
->flist
,
2016 &bma
->cur
, 1, &tmp_rval
, whichfork
);
2021 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
2022 startblockval(PREV
.br_startblock
) -
2023 (bma
->cur
? bma
->cur
->bc_private
.b
.allocated
: 0));
2024 ep
= xfs_iext_get_ext(ifp
, bma
->idx
+ 1);
2025 xfs_bmbt_set_startblock(ep
, nullstartblock(da_new
));
2026 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
+ 1, state
, _THIS_IP_
);
2029 case BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2031 * Filling in the last part of a previous delayed allocation.
2032 * The right neighbor is contiguous with the new allocation.
2034 temp
= PREV
.br_blockcount
- new->br_blockcount
;
2035 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
+ 1, state
, _THIS_IP_
);
2036 xfs_bmbt_set_blockcount(ep
, temp
);
2037 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp
, bma
->idx
+ 1),
2038 new->br_startoff
, new->br_startblock
,
2039 new->br_blockcount
+ RIGHT
.br_blockcount
,
2041 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
+ 1, state
, _THIS_IP_
);
2042 if (bma
->cur
== NULL
)
2043 rval
= XFS_ILOG_DEXT
;
2046 error
= xfs_bmbt_lookup_eq(bma
->cur
, RIGHT
.br_startoff
,
2047 RIGHT
.br_startblock
,
2048 RIGHT
.br_blockcount
, &i
);
2051 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2052 error
= xfs_bmbt_update(bma
->cur
, new->br_startoff
,
2054 new->br_blockcount
+
2055 RIGHT
.br_blockcount
,
2061 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
2062 startblockval(PREV
.br_startblock
));
2063 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2064 xfs_bmbt_set_startblock(ep
, nullstartblock(da_new
));
2065 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2070 case BMAP_RIGHT_FILLING
:
2072 * Filling in the last part of a previous delayed allocation.
2073 * The right neighbor is not contiguous.
2075 temp
= PREV
.br_blockcount
- new->br_blockcount
;
2076 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2077 xfs_bmbt_set_blockcount(ep
, temp
);
2078 xfs_iext_insert(bma
->ip
, bma
->idx
+ 1, 1, new, state
);
2079 bma
->ip
->i_d
.di_nextents
++;
2080 if (bma
->cur
== NULL
)
2081 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2083 rval
= XFS_ILOG_CORE
;
2084 error
= xfs_bmbt_lookup_eq(bma
->cur
, new->br_startoff
,
2085 new->br_startblock
, new->br_blockcount
,
2089 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
2090 bma
->cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
2091 error
= xfs_btree_insert(bma
->cur
, &i
);
2094 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2097 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
2098 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
2099 bma
->firstblock
, bma
->flist
, &bma
->cur
, 1,
2100 &tmp_rval
, whichfork
);
2105 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
2106 startblockval(PREV
.br_startblock
) -
2107 (bma
->cur
? bma
->cur
->bc_private
.b
.allocated
: 0));
2108 ep
= xfs_iext_get_ext(ifp
, bma
->idx
);
2109 xfs_bmbt_set_startblock(ep
, nullstartblock(da_new
));
2110 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2117 * Filling in the middle part of a previous delayed allocation.
2118 * Contiguity is impossible here.
2119 * This case is avoided almost all the time.
2121 * We start with a delayed allocation:
2123 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
2126 * and we are allocating:
2127 * +rrrrrrrrrrrrrrrrr+
2130 * and we set it up for insertion as:
2131 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
2133 * PREV @ idx LEFT RIGHT
2134 * inserted at idx + 1
2136 temp
= new->br_startoff
- PREV
.br_startoff
;
2137 temp2
= PREV
.br_startoff
+ PREV
.br_blockcount
- new_endoff
;
2138 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, 0, _THIS_IP_
);
2139 xfs_bmbt_set_blockcount(ep
, temp
); /* truncate PREV */
2141 RIGHT
.br_state
= PREV
.br_state
;
2142 RIGHT
.br_startblock
= nullstartblock(
2143 (int)xfs_bmap_worst_indlen(bma
->ip
, temp2
));
2144 RIGHT
.br_startoff
= new_endoff
;
2145 RIGHT
.br_blockcount
= temp2
;
2146 /* insert LEFT (r[0]) and RIGHT (r[1]) at the same time */
2147 xfs_iext_insert(bma
->ip
, bma
->idx
+ 1, 2, &LEFT
, state
);
2148 bma
->ip
->i_d
.di_nextents
++;
2149 if (bma
->cur
== NULL
)
2150 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2152 rval
= XFS_ILOG_CORE
;
2153 error
= xfs_bmbt_lookup_eq(bma
->cur
, new->br_startoff
,
2154 new->br_startblock
, new->br_blockcount
,
2158 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
2159 bma
->cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
2160 error
= xfs_btree_insert(bma
->cur
, &i
);
2163 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2166 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
2167 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
2168 bma
->firstblock
, bma
->flist
, &bma
->cur
,
2169 1, &tmp_rval
, whichfork
);
2174 temp
= xfs_bmap_worst_indlen(bma
->ip
, temp
);
2175 temp2
= xfs_bmap_worst_indlen(bma
->ip
, temp2
);
2176 diff
= (int)(temp
+ temp2
- startblockval(PREV
.br_startblock
) -
2177 (bma
->cur
? bma
->cur
->bc_private
.b
.allocated
: 0));
2179 error
= xfs_mod_fdblocks(bma
->ip
->i_mount
,
2180 -((int64_t)diff
), false);
2186 ep
= xfs_iext_get_ext(ifp
, bma
->idx
);
2187 xfs_bmbt_set_startblock(ep
, nullstartblock((int)temp
));
2188 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2189 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
+ 2, state
, _THIS_IP_
);
2190 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp
, bma
->idx
+ 2),
2191 nullstartblock((int)temp2
));
2192 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
+ 2, state
, _THIS_IP_
);
2195 da_new
= temp
+ temp2
;
2198 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2199 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2200 case BMAP_LEFT_FILLING
| BMAP_RIGHT_CONTIG
:
2201 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
2202 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2203 case BMAP_LEFT_CONTIG
:
2204 case BMAP_RIGHT_CONTIG
:
2206 * These cases are all impossible.
2211 /* convert to a btree if necessary */
2212 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
2213 int tmp_logflags
; /* partial log flag return val */
2215 ASSERT(bma
->cur
== NULL
);
2216 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
2217 bma
->firstblock
, bma
->flist
, &bma
->cur
,
2218 da_old
> 0, &tmp_logflags
, whichfork
);
2219 bma
->logflags
|= tmp_logflags
;
2224 /* adjust for changes in reserved delayed indirect blocks */
2225 if (da_old
|| da_new
) {
2228 temp
+= bma
->cur
->bc_private
.b
.allocated
;
2229 ASSERT(temp
<= da_old
);
2231 xfs_mod_fdblocks(bma
->ip
->i_mount
,
2232 (int64_t)(da_old
- temp
), false);
2235 /* clear out the allocated field, done with it now in any case. */
2237 bma
->cur
->bc_private
.b
.allocated
= 0;
2239 xfs_bmap_check_leaf_extents(bma
->cur
, bma
->ip
, whichfork
);
2241 bma
->logflags
|= rval
;
2249 * Convert an unwritten allocation to a real allocation or vice versa.
2251 STATIC
int /* error */
2252 xfs_bmap_add_extent_unwritten_real(
2253 struct xfs_trans
*tp
,
2254 xfs_inode_t
*ip
, /* incore inode pointer */
2255 xfs_extnum_t
*idx
, /* extent number to update/insert */
2256 xfs_btree_cur_t
**curp
, /* if *curp is null, not a btree */
2257 xfs_bmbt_irec_t
*new, /* new data to add to file extents */
2258 xfs_fsblock_t
*first
, /* pointer to firstblock variable */
2259 xfs_bmap_free_t
*flist
, /* list of extents to be freed */
2260 int *logflagsp
) /* inode logging flags */
2262 xfs_btree_cur_t
*cur
; /* btree cursor */
2263 xfs_bmbt_rec_host_t
*ep
; /* extent entry for idx */
2264 int error
; /* error return value */
2265 int i
; /* temp state */
2266 xfs_ifork_t
*ifp
; /* inode fork pointer */
2267 xfs_fileoff_t new_endoff
; /* end offset of new entry */
2268 xfs_exntst_t newext
; /* new extent state */
2269 xfs_exntst_t oldext
; /* old extent state */
2270 xfs_bmbt_irec_t r
[3]; /* neighbor extent entries */
2271 /* left is 0, right is 1, prev is 2 */
2272 int rval
=0; /* return value (logging flags) */
2273 int state
= 0;/* state bits, accessed thru macros */
2274 struct xfs_mount
*mp
= tp
->t_mountp
;
2279 ifp
= XFS_IFORK_PTR(ip
, XFS_DATA_FORK
);
2282 ASSERT(*idx
<= ifp
->if_bytes
/ sizeof(struct xfs_bmbt_rec
));
2283 ASSERT(!isnullstartblock(new->br_startblock
));
2285 XFS_STATS_INC(mp
, xs_add_exlist
);
2292 * Set up a bunch of variables to make the tests simpler.
2295 ep
= xfs_iext_get_ext(ifp
, *idx
);
2296 xfs_bmbt_get_all(ep
, &PREV
);
2297 newext
= new->br_state
;
2298 oldext
= (newext
== XFS_EXT_UNWRITTEN
) ?
2299 XFS_EXT_NORM
: XFS_EXT_UNWRITTEN
;
2300 ASSERT(PREV
.br_state
== oldext
);
2301 new_endoff
= new->br_startoff
+ new->br_blockcount
;
2302 ASSERT(PREV
.br_startoff
<= new->br_startoff
);
2303 ASSERT(PREV
.br_startoff
+ PREV
.br_blockcount
>= new_endoff
);
2306 * Set flags determining what part of the previous oldext allocation
2307 * extent is being replaced by a newext allocation.
2309 if (PREV
.br_startoff
== new->br_startoff
)
2310 state
|= BMAP_LEFT_FILLING
;
2311 if (PREV
.br_startoff
+ PREV
.br_blockcount
== new_endoff
)
2312 state
|= BMAP_RIGHT_FILLING
;
2315 * Check and set flags if this segment has a left neighbor.
2316 * Don't set contiguous if the combined extent would be too large.
2319 state
|= BMAP_LEFT_VALID
;
2320 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, *idx
- 1), &LEFT
);
2322 if (isnullstartblock(LEFT
.br_startblock
))
2323 state
|= BMAP_LEFT_DELAY
;
2326 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
2327 LEFT
.br_startoff
+ LEFT
.br_blockcount
== new->br_startoff
&&
2328 LEFT
.br_startblock
+ LEFT
.br_blockcount
== new->br_startblock
&&
2329 LEFT
.br_state
== newext
&&
2330 LEFT
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
2331 state
|= BMAP_LEFT_CONTIG
;
2334 * Check and set flags if this segment has a right neighbor.
2335 * Don't set contiguous if the combined extent would be too large.
2336 * Also check for all-three-contiguous being too large.
2338 if (*idx
< ip
->i_df
.if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
) - 1) {
2339 state
|= BMAP_RIGHT_VALID
;
2340 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, *idx
+ 1), &RIGHT
);
2341 if (isnullstartblock(RIGHT
.br_startblock
))
2342 state
|= BMAP_RIGHT_DELAY
;
2345 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
2346 new_endoff
== RIGHT
.br_startoff
&&
2347 new->br_startblock
+ new->br_blockcount
== RIGHT
.br_startblock
&&
2348 newext
== RIGHT
.br_state
&&
2349 new->br_blockcount
+ RIGHT
.br_blockcount
<= MAXEXTLEN
&&
2350 ((state
& (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
2351 BMAP_RIGHT_FILLING
)) !=
2352 (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
2353 BMAP_RIGHT_FILLING
) ||
2354 LEFT
.br_blockcount
+ new->br_blockcount
+ RIGHT
.br_blockcount
2356 state
|= BMAP_RIGHT_CONTIG
;
2359 * Switch out based on the FILLING and CONTIG state bits.
2361 switch (state
& (BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
2362 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
)) {
2363 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
2364 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2366 * Setting all of a previous oldext extent to newext.
2367 * The left and right neighbors are both contiguous with new.
2371 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2372 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
),
2373 LEFT
.br_blockcount
+ PREV
.br_blockcount
+
2374 RIGHT
.br_blockcount
);
2375 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2377 xfs_iext_remove(ip
, *idx
+ 1, 2, state
);
2378 ip
->i_d
.di_nextents
-= 2;
2380 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2382 rval
= XFS_ILOG_CORE
;
2383 if ((error
= xfs_bmbt_lookup_eq(cur
, RIGHT
.br_startoff
,
2384 RIGHT
.br_startblock
,
2385 RIGHT
.br_blockcount
, &i
)))
2387 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2388 if ((error
= xfs_btree_delete(cur
, &i
)))
2390 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2391 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2393 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2394 if ((error
= xfs_btree_delete(cur
, &i
)))
2396 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2397 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2399 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2400 if ((error
= xfs_bmbt_update(cur
, LEFT
.br_startoff
,
2402 LEFT
.br_blockcount
+ PREV
.br_blockcount
+
2403 RIGHT
.br_blockcount
, LEFT
.br_state
)))
2408 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
2410 * Setting all of a previous oldext extent to newext.
2411 * The left neighbor is contiguous, the right is not.
2415 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2416 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
),
2417 LEFT
.br_blockcount
+ PREV
.br_blockcount
);
2418 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2420 xfs_iext_remove(ip
, *idx
+ 1, 1, state
);
2421 ip
->i_d
.di_nextents
--;
2423 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2425 rval
= XFS_ILOG_CORE
;
2426 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2427 PREV
.br_startblock
, PREV
.br_blockcount
,
2430 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2431 if ((error
= xfs_btree_delete(cur
, &i
)))
2433 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2434 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2436 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2437 if ((error
= xfs_bmbt_update(cur
, LEFT
.br_startoff
,
2439 LEFT
.br_blockcount
+ PREV
.br_blockcount
,
2445 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2447 * Setting all of a previous oldext extent to newext.
2448 * The right neighbor is contiguous, the left is not.
2450 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2451 xfs_bmbt_set_blockcount(ep
,
2452 PREV
.br_blockcount
+ RIGHT
.br_blockcount
);
2453 xfs_bmbt_set_state(ep
, newext
);
2454 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2455 xfs_iext_remove(ip
, *idx
+ 1, 1, state
);
2456 ip
->i_d
.di_nextents
--;
2458 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2460 rval
= XFS_ILOG_CORE
;
2461 if ((error
= xfs_bmbt_lookup_eq(cur
, RIGHT
.br_startoff
,
2462 RIGHT
.br_startblock
,
2463 RIGHT
.br_blockcount
, &i
)))
2465 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2466 if ((error
= xfs_btree_delete(cur
, &i
)))
2468 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2469 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2471 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2472 if ((error
= xfs_bmbt_update(cur
, new->br_startoff
,
2474 new->br_blockcount
+ RIGHT
.br_blockcount
,
2480 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
2482 * Setting all of a previous oldext extent to newext.
2483 * Neither the left nor right neighbors are contiguous with
2486 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2487 xfs_bmbt_set_state(ep
, newext
);
2488 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2491 rval
= XFS_ILOG_DEXT
;
2494 if ((error
= xfs_bmbt_lookup_eq(cur
, new->br_startoff
,
2495 new->br_startblock
, new->br_blockcount
,
2498 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2499 if ((error
= xfs_bmbt_update(cur
, new->br_startoff
,
2500 new->br_startblock
, new->br_blockcount
,
2506 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
:
2508 * Setting the first part of a previous oldext extent to newext.
2509 * The left neighbor is contiguous.
2511 trace_xfs_bmap_pre_update(ip
, *idx
- 1, state
, _THIS_IP_
);
2512 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
- 1),
2513 LEFT
.br_blockcount
+ new->br_blockcount
);
2514 xfs_bmbt_set_startoff(ep
,
2515 PREV
.br_startoff
+ new->br_blockcount
);
2516 trace_xfs_bmap_post_update(ip
, *idx
- 1, state
, _THIS_IP_
);
2518 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2519 xfs_bmbt_set_startblock(ep
,
2520 new->br_startblock
+ new->br_blockcount
);
2521 xfs_bmbt_set_blockcount(ep
,
2522 PREV
.br_blockcount
- new->br_blockcount
);
2523 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2528 rval
= XFS_ILOG_DEXT
;
2531 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2532 PREV
.br_startblock
, PREV
.br_blockcount
,
2535 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2536 if ((error
= xfs_bmbt_update(cur
,
2537 PREV
.br_startoff
+ new->br_blockcount
,
2538 PREV
.br_startblock
+ new->br_blockcount
,
2539 PREV
.br_blockcount
- new->br_blockcount
,
2542 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2544 error
= xfs_bmbt_update(cur
, LEFT
.br_startoff
,
2546 LEFT
.br_blockcount
+ new->br_blockcount
,
2553 case BMAP_LEFT_FILLING
:
2555 * Setting the first part of a previous oldext extent to newext.
2556 * The left neighbor is not contiguous.
2558 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2559 ASSERT(ep
&& xfs_bmbt_get_state(ep
) == oldext
);
2560 xfs_bmbt_set_startoff(ep
, new_endoff
);
2561 xfs_bmbt_set_blockcount(ep
,
2562 PREV
.br_blockcount
- new->br_blockcount
);
2563 xfs_bmbt_set_startblock(ep
,
2564 new->br_startblock
+ new->br_blockcount
);
2565 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2567 xfs_iext_insert(ip
, *idx
, 1, new, state
);
2568 ip
->i_d
.di_nextents
++;
2570 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2572 rval
= XFS_ILOG_CORE
;
2573 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2574 PREV
.br_startblock
, PREV
.br_blockcount
,
2577 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2578 if ((error
= xfs_bmbt_update(cur
,
2579 PREV
.br_startoff
+ new->br_blockcount
,
2580 PREV
.br_startblock
+ new->br_blockcount
,
2581 PREV
.br_blockcount
- new->br_blockcount
,
2584 cur
->bc_rec
.b
= *new;
2585 if ((error
= xfs_btree_insert(cur
, &i
)))
2587 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2591 case BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2593 * Setting the last part of a previous oldext extent to newext.
2594 * The right neighbor is contiguous with the new allocation.
2596 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2597 xfs_bmbt_set_blockcount(ep
,
2598 PREV
.br_blockcount
- new->br_blockcount
);
2599 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2603 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2604 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp
, *idx
),
2605 new->br_startoff
, new->br_startblock
,
2606 new->br_blockcount
+ RIGHT
.br_blockcount
, newext
);
2607 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2610 rval
= XFS_ILOG_DEXT
;
2613 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2615 PREV
.br_blockcount
, &i
)))
2617 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2618 if ((error
= xfs_bmbt_update(cur
, PREV
.br_startoff
,
2620 PREV
.br_blockcount
- new->br_blockcount
,
2623 if ((error
= xfs_btree_increment(cur
, 0, &i
)))
2625 if ((error
= xfs_bmbt_update(cur
, new->br_startoff
,
2627 new->br_blockcount
+ RIGHT
.br_blockcount
,
2633 case BMAP_RIGHT_FILLING
:
2635 * Setting the last part of a previous oldext extent to newext.
2636 * The right neighbor is not contiguous.
2638 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2639 xfs_bmbt_set_blockcount(ep
,
2640 PREV
.br_blockcount
- new->br_blockcount
);
2641 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2644 xfs_iext_insert(ip
, *idx
, 1, new, state
);
2646 ip
->i_d
.di_nextents
++;
2648 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2650 rval
= XFS_ILOG_CORE
;
2651 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2652 PREV
.br_startblock
, PREV
.br_blockcount
,
2655 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2656 if ((error
= xfs_bmbt_update(cur
, PREV
.br_startoff
,
2658 PREV
.br_blockcount
- new->br_blockcount
,
2661 if ((error
= xfs_bmbt_lookup_eq(cur
, new->br_startoff
,
2662 new->br_startblock
, new->br_blockcount
,
2665 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
2666 cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
2667 if ((error
= xfs_btree_insert(cur
, &i
)))
2669 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2675 * Setting the middle part of a previous oldext extent to
2676 * newext. Contiguity is impossible here.
2677 * One extent becomes three extents.
2679 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2680 xfs_bmbt_set_blockcount(ep
,
2681 new->br_startoff
- PREV
.br_startoff
);
2682 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2685 r
[1].br_startoff
= new_endoff
;
2686 r
[1].br_blockcount
=
2687 PREV
.br_startoff
+ PREV
.br_blockcount
- new_endoff
;
2688 r
[1].br_startblock
= new->br_startblock
+ new->br_blockcount
;
2689 r
[1].br_state
= oldext
;
2692 xfs_iext_insert(ip
, *idx
, 2, &r
[0], state
);
2694 ip
->i_d
.di_nextents
+= 2;
2696 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2698 rval
= XFS_ILOG_CORE
;
2699 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2700 PREV
.br_startblock
, PREV
.br_blockcount
,
2703 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2704 /* new right extent - oldext */
2705 if ((error
= xfs_bmbt_update(cur
, r
[1].br_startoff
,
2706 r
[1].br_startblock
, r
[1].br_blockcount
,
2709 /* new left extent - oldext */
2710 cur
->bc_rec
.b
= PREV
;
2711 cur
->bc_rec
.b
.br_blockcount
=
2712 new->br_startoff
- PREV
.br_startoff
;
2713 if ((error
= xfs_btree_insert(cur
, &i
)))
2715 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2717 * Reset the cursor to the position of the new extent
2718 * we are about to insert as we can't trust it after
2719 * the previous insert.
2721 if ((error
= xfs_bmbt_lookup_eq(cur
, new->br_startoff
,
2722 new->br_startblock
, new->br_blockcount
,
2725 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
2726 /* new middle extent - newext */
2727 cur
->bc_rec
.b
.br_state
= new->br_state
;
2728 if ((error
= xfs_btree_insert(cur
, &i
)))
2730 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2734 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2735 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2736 case BMAP_LEFT_FILLING
| BMAP_RIGHT_CONTIG
:
2737 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
2738 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2739 case BMAP_LEFT_CONTIG
:
2740 case BMAP_RIGHT_CONTIG
:
2742 * These cases are all impossible.
2747 /* convert to a btree if necessary */
2748 if (xfs_bmap_needs_btree(ip
, XFS_DATA_FORK
)) {
2749 int tmp_logflags
; /* partial log flag return val */
2751 ASSERT(cur
== NULL
);
2752 error
= xfs_bmap_extents_to_btree(tp
, ip
, first
, flist
, &cur
,
2753 0, &tmp_logflags
, XFS_DATA_FORK
);
2754 *logflagsp
|= tmp_logflags
;
2759 /* clear out the allocated field, done with it now in any case. */
2761 cur
->bc_private
.b
.allocated
= 0;
2765 xfs_bmap_check_leaf_extents(*curp
, ip
, XFS_DATA_FORK
);
2775 * Convert a hole to a delayed allocation.
2778 xfs_bmap_add_extent_hole_delay(
2779 xfs_inode_t
*ip
, /* incore inode pointer */
2780 xfs_extnum_t
*idx
, /* extent number to update/insert */
2781 xfs_bmbt_irec_t
*new) /* new data to add to file extents */
2783 xfs_ifork_t
*ifp
; /* inode fork pointer */
2784 xfs_bmbt_irec_t left
; /* left neighbor extent entry */
2785 xfs_filblks_t newlen
=0; /* new indirect size */
2786 xfs_filblks_t oldlen
=0; /* old indirect size */
2787 xfs_bmbt_irec_t right
; /* right neighbor extent entry */
2788 int state
; /* state bits, accessed thru macros */
2789 xfs_filblks_t temp
=0; /* temp for indirect calculations */
2791 ifp
= XFS_IFORK_PTR(ip
, XFS_DATA_FORK
);
2793 ASSERT(isnullstartblock(new->br_startblock
));
2796 * Check and set flags if this segment has a left neighbor
2799 state
|= BMAP_LEFT_VALID
;
2800 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, *idx
- 1), &left
);
2802 if (isnullstartblock(left
.br_startblock
))
2803 state
|= BMAP_LEFT_DELAY
;
2807 * Check and set flags if the current (right) segment exists.
2808 * If it doesn't exist, we're converting the hole at end-of-file.
2810 if (*idx
< ip
->i_df
.if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
)) {
2811 state
|= BMAP_RIGHT_VALID
;
2812 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, *idx
), &right
);
2814 if (isnullstartblock(right
.br_startblock
))
2815 state
|= BMAP_RIGHT_DELAY
;
2819 * Set contiguity flags on the left and right neighbors.
2820 * Don't let extents get too large, even if the pieces are contiguous.
2822 if ((state
& BMAP_LEFT_VALID
) && (state
& BMAP_LEFT_DELAY
) &&
2823 left
.br_startoff
+ left
.br_blockcount
== new->br_startoff
&&
2824 left
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
2825 state
|= BMAP_LEFT_CONTIG
;
2827 if ((state
& BMAP_RIGHT_VALID
) && (state
& BMAP_RIGHT_DELAY
) &&
2828 new->br_startoff
+ new->br_blockcount
== right
.br_startoff
&&
2829 new->br_blockcount
+ right
.br_blockcount
<= MAXEXTLEN
&&
2830 (!(state
& BMAP_LEFT_CONTIG
) ||
2831 (left
.br_blockcount
+ new->br_blockcount
+
2832 right
.br_blockcount
<= MAXEXTLEN
)))
2833 state
|= BMAP_RIGHT_CONTIG
;
2836 * Switch out based on the contiguity flags.
2838 switch (state
& (BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
)) {
2839 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2841 * New allocation is contiguous with delayed allocations
2842 * on the left and on the right.
2843 * Merge all three into a single extent record.
2846 temp
= left
.br_blockcount
+ new->br_blockcount
+
2847 right
.br_blockcount
;
2849 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2850 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
), temp
);
2851 oldlen
= startblockval(left
.br_startblock
) +
2852 startblockval(new->br_startblock
) +
2853 startblockval(right
.br_startblock
);
2854 newlen
= xfs_bmap_worst_indlen(ip
, temp
);
2855 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp
, *idx
),
2856 nullstartblock((int)newlen
));
2857 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2859 xfs_iext_remove(ip
, *idx
+ 1, 1, state
);
2862 case BMAP_LEFT_CONTIG
:
2864 * New allocation is contiguous with a delayed allocation
2866 * Merge the new allocation with the left neighbor.
2869 temp
= left
.br_blockcount
+ new->br_blockcount
;
2871 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2872 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
), temp
);
2873 oldlen
= startblockval(left
.br_startblock
) +
2874 startblockval(new->br_startblock
);
2875 newlen
= xfs_bmap_worst_indlen(ip
, temp
);
2876 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp
, *idx
),
2877 nullstartblock((int)newlen
));
2878 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2881 case BMAP_RIGHT_CONTIG
:
2883 * New allocation is contiguous with a delayed allocation
2885 * Merge the new allocation with the right neighbor.
2887 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2888 temp
= new->br_blockcount
+ right
.br_blockcount
;
2889 oldlen
= startblockval(new->br_startblock
) +
2890 startblockval(right
.br_startblock
);
2891 newlen
= xfs_bmap_worst_indlen(ip
, temp
);
2892 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp
, *idx
),
2894 nullstartblock((int)newlen
), temp
, right
.br_state
);
2895 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2900 * New allocation is not contiguous with another
2901 * delayed allocation.
2902 * Insert a new entry.
2904 oldlen
= newlen
= 0;
2905 xfs_iext_insert(ip
, *idx
, 1, new, state
);
2908 if (oldlen
!= newlen
) {
2909 ASSERT(oldlen
> newlen
);
2910 xfs_mod_fdblocks(ip
->i_mount
, (int64_t)(oldlen
- newlen
),
2913 * Nothing to do for disk quota accounting here.
2919 * Convert a hole to a real allocation.
2921 STATIC
int /* error */
2922 xfs_bmap_add_extent_hole_real(
2923 struct xfs_bmalloca
*bma
,
2926 struct xfs_bmbt_irec
*new = &bma
->got
;
2927 int error
; /* error return value */
2928 int i
; /* temp state */
2929 xfs_ifork_t
*ifp
; /* inode fork pointer */
2930 xfs_bmbt_irec_t left
; /* left neighbor extent entry */
2931 xfs_bmbt_irec_t right
; /* right neighbor extent entry */
2932 int rval
=0; /* return value (logging flags) */
2933 int state
; /* state bits, accessed thru macros */
2934 struct xfs_mount
*mp
;
2936 mp
= bma
->ip
->i_mount
;
2937 ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
2939 ASSERT(bma
->idx
>= 0);
2940 ASSERT(bma
->idx
<= ifp
->if_bytes
/ sizeof(struct xfs_bmbt_rec
));
2941 ASSERT(!isnullstartblock(new->br_startblock
));
2943 !(bma
->cur
->bc_private
.b
.flags
& XFS_BTCUR_BPRV_WASDEL
));
2945 XFS_STATS_INC(mp
, xs_add_exlist
);
2948 if (whichfork
== XFS_ATTR_FORK
)
2949 state
|= BMAP_ATTRFORK
;
2952 * Check and set flags if this segment has a left neighbor.
2955 state
|= BMAP_LEFT_VALID
;
2956 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
- 1), &left
);
2957 if (isnullstartblock(left
.br_startblock
))
2958 state
|= BMAP_LEFT_DELAY
;
2962 * Check and set flags if this segment has a current value.
2963 * Not true if we're inserting into the "hole" at eof.
2965 if (bma
->idx
< ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
)) {
2966 state
|= BMAP_RIGHT_VALID
;
2967 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
), &right
);
2968 if (isnullstartblock(right
.br_startblock
))
2969 state
|= BMAP_RIGHT_DELAY
;
2973 * We're inserting a real allocation between "left" and "right".
2974 * Set the contiguity flags. Don't let extents get too large.
2976 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
2977 left
.br_startoff
+ left
.br_blockcount
== new->br_startoff
&&
2978 left
.br_startblock
+ left
.br_blockcount
== new->br_startblock
&&
2979 left
.br_state
== new->br_state
&&
2980 left
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
2981 state
|= BMAP_LEFT_CONTIG
;
2983 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
2984 new->br_startoff
+ new->br_blockcount
== right
.br_startoff
&&
2985 new->br_startblock
+ new->br_blockcount
== right
.br_startblock
&&
2986 new->br_state
== right
.br_state
&&
2987 new->br_blockcount
+ right
.br_blockcount
<= MAXEXTLEN
&&
2988 (!(state
& BMAP_LEFT_CONTIG
) ||
2989 left
.br_blockcount
+ new->br_blockcount
+
2990 right
.br_blockcount
<= MAXEXTLEN
))
2991 state
|= BMAP_RIGHT_CONTIG
;
2995 * Select which case we're in here, and implement it.
2997 switch (state
& (BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
)) {
2998 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
3000 * New allocation is contiguous with real allocations on the
3001 * left and on the right.
3002 * Merge all three into a single extent record.
3005 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
3006 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, bma
->idx
),
3007 left
.br_blockcount
+ new->br_blockcount
+
3008 right
.br_blockcount
);
3009 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
3011 xfs_iext_remove(bma
->ip
, bma
->idx
+ 1, 1, state
);
3013 XFS_IFORK_NEXT_SET(bma
->ip
, whichfork
,
3014 XFS_IFORK_NEXTENTS(bma
->ip
, whichfork
) - 1);
3015 if (bma
->cur
== NULL
) {
3016 rval
= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
3018 rval
= XFS_ILOG_CORE
;
3019 error
= xfs_bmbt_lookup_eq(bma
->cur
, right
.br_startoff
,
3020 right
.br_startblock
, right
.br_blockcount
,
3024 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
3025 error
= xfs_btree_delete(bma
->cur
, &i
);
3028 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
3029 error
= xfs_btree_decrement(bma
->cur
, 0, &i
);
3032 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
3033 error
= xfs_bmbt_update(bma
->cur
, left
.br_startoff
,
3035 left
.br_blockcount
+
3036 new->br_blockcount
+
3037 right
.br_blockcount
,
3044 case BMAP_LEFT_CONTIG
:
3046 * New allocation is contiguous with a real allocation
3048 * Merge the new allocation with the left neighbor.
3051 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
3052 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, bma
->idx
),
3053 left
.br_blockcount
+ new->br_blockcount
);
3054 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
3056 if (bma
->cur
== NULL
) {
3057 rval
= xfs_ilog_fext(whichfork
);
3060 error
= xfs_bmbt_lookup_eq(bma
->cur
, left
.br_startoff
,
3061 left
.br_startblock
, left
.br_blockcount
,
3065 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
3066 error
= xfs_bmbt_update(bma
->cur
, left
.br_startoff
,
3068 left
.br_blockcount
+
3076 case BMAP_RIGHT_CONTIG
:
3078 * New allocation is contiguous with a real allocation
3080 * Merge the new allocation with the right neighbor.
3082 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
3083 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp
, bma
->idx
),
3084 new->br_startoff
, new->br_startblock
,
3085 new->br_blockcount
+ right
.br_blockcount
,
3087 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
3089 if (bma
->cur
== NULL
) {
3090 rval
= xfs_ilog_fext(whichfork
);
3093 error
= xfs_bmbt_lookup_eq(bma
->cur
,
3095 right
.br_startblock
,
3096 right
.br_blockcount
, &i
);
3099 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
3100 error
= xfs_bmbt_update(bma
->cur
, new->br_startoff
,
3102 new->br_blockcount
+
3103 right
.br_blockcount
,
3112 * New allocation is not contiguous with another
3114 * Insert a new entry.
3116 xfs_iext_insert(bma
->ip
, bma
->idx
, 1, new, state
);
3117 XFS_IFORK_NEXT_SET(bma
->ip
, whichfork
,
3118 XFS_IFORK_NEXTENTS(bma
->ip
, whichfork
) + 1);
3119 if (bma
->cur
== NULL
) {
3120 rval
= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
3122 rval
= XFS_ILOG_CORE
;
3123 error
= xfs_bmbt_lookup_eq(bma
->cur
,
3126 new->br_blockcount
, &i
);
3129 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
3130 bma
->cur
->bc_rec
.b
.br_state
= new->br_state
;
3131 error
= xfs_btree_insert(bma
->cur
, &i
);
3134 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
3139 /* convert to a btree if necessary */
3140 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
3141 int tmp_logflags
; /* partial log flag return val */
3143 ASSERT(bma
->cur
== NULL
);
3144 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
3145 bma
->firstblock
, bma
->flist
, &bma
->cur
,
3146 0, &tmp_logflags
, whichfork
);
3147 bma
->logflags
|= tmp_logflags
;
3152 /* clear out the allocated field, done with it now in any case. */
3154 bma
->cur
->bc_private
.b
.allocated
= 0;
3156 xfs_bmap_check_leaf_extents(bma
->cur
, bma
->ip
, whichfork
);
3158 bma
->logflags
|= rval
;
3163 * Functions used in the extent read, allocate and remove paths
3167 * Adjust the size of the new extent based on di_extsize and rt extsize.
3170 xfs_bmap_extsize_align(
3172 xfs_bmbt_irec_t
*gotp
, /* next extent pointer */
3173 xfs_bmbt_irec_t
*prevp
, /* previous extent pointer */
3174 xfs_extlen_t extsz
, /* align to this extent size */
3175 int rt
, /* is this a realtime inode? */
3176 int eof
, /* is extent at end-of-file? */
3177 int delay
, /* creating delalloc extent? */
3178 int convert
, /* overwriting unwritten extent? */
3179 xfs_fileoff_t
*offp
, /* in/out: aligned offset */
3180 xfs_extlen_t
*lenp
) /* in/out: aligned length */
3182 xfs_fileoff_t orig_off
; /* original offset */
3183 xfs_extlen_t orig_alen
; /* original length */
3184 xfs_fileoff_t orig_end
; /* original off+len */
3185 xfs_fileoff_t nexto
; /* next file offset */
3186 xfs_fileoff_t prevo
; /* previous file offset */
3187 xfs_fileoff_t align_off
; /* temp for offset */
3188 xfs_extlen_t align_alen
; /* temp for length */
3189 xfs_extlen_t temp
; /* temp for calculations */
3194 orig_off
= align_off
= *offp
;
3195 orig_alen
= align_alen
= *lenp
;
3196 orig_end
= orig_off
+ orig_alen
;
3199 * If this request overlaps an existing extent, then don't
3200 * attempt to perform any additional alignment.
3202 if (!delay
&& !eof
&&
3203 (orig_off
>= gotp
->br_startoff
) &&
3204 (orig_end
<= gotp
->br_startoff
+ gotp
->br_blockcount
)) {
3209 * If the file offset is unaligned vs. the extent size
3210 * we need to align it. This will be possible unless
3211 * the file was previously written with a kernel that didn't
3212 * perform this alignment, or if a truncate shot us in the
3215 temp
= do_mod(orig_off
, extsz
);
3221 /* Same adjustment for the end of the requested area. */
3222 temp
= (align_alen
% extsz
);
3224 align_alen
+= extsz
- temp
;
3227 * For large extent hint sizes, the aligned extent might be larger than
3228 * MAXEXTLEN. In that case, reduce the size by an extsz so that it pulls
3229 * the length back under MAXEXTLEN. The outer allocation loops handle
3230 * short allocation just fine, so it is safe to do this. We only want to
3231 * do it when we are forced to, though, because it means more allocation
3232 * operations are required.
3234 while (align_alen
> MAXEXTLEN
)
3235 align_alen
-= extsz
;
3236 ASSERT(align_alen
<= MAXEXTLEN
);
3239 * If the previous block overlaps with this proposed allocation
3240 * then move the start forward without adjusting the length.
3242 if (prevp
->br_startoff
!= NULLFILEOFF
) {
3243 if (prevp
->br_startblock
== HOLESTARTBLOCK
)
3244 prevo
= prevp
->br_startoff
;
3246 prevo
= prevp
->br_startoff
+ prevp
->br_blockcount
;
3249 if (align_off
!= orig_off
&& align_off
< prevo
)
3252 * If the next block overlaps with this proposed allocation
3253 * then move the start back without adjusting the length,
3254 * but not before offset 0.
3255 * This may of course make the start overlap previous block,
3256 * and if we hit the offset 0 limit then the next block
3257 * can still overlap too.
3259 if (!eof
&& gotp
->br_startoff
!= NULLFILEOFF
) {
3260 if ((delay
&& gotp
->br_startblock
== HOLESTARTBLOCK
) ||
3261 (!delay
&& gotp
->br_startblock
== DELAYSTARTBLOCK
))
3262 nexto
= gotp
->br_startoff
+ gotp
->br_blockcount
;
3264 nexto
= gotp
->br_startoff
;
3266 nexto
= NULLFILEOFF
;
3268 align_off
+ align_alen
!= orig_end
&&
3269 align_off
+ align_alen
> nexto
)
3270 align_off
= nexto
> align_alen
? nexto
- align_alen
: 0;
3272 * If we're now overlapping the next or previous extent that
3273 * means we can't fit an extsz piece in this hole. Just move
3274 * the start forward to the first valid spot and set
3275 * the length so we hit the end.
3277 if (align_off
!= orig_off
&& align_off
< prevo
)
3279 if (align_off
+ align_alen
!= orig_end
&&
3280 align_off
+ align_alen
> nexto
&&
3281 nexto
!= NULLFILEOFF
) {
3282 ASSERT(nexto
> prevo
);
3283 align_alen
= nexto
- align_off
;
3287 * If realtime, and the result isn't a multiple of the realtime
3288 * extent size we need to remove blocks until it is.
3290 if (rt
&& (temp
= (align_alen
% mp
->m_sb
.sb_rextsize
))) {
3292 * We're not covering the original request, or
3293 * we won't be able to once we fix the length.
3295 if (orig_off
< align_off
||
3296 orig_end
> align_off
+ align_alen
||
3297 align_alen
- temp
< orig_alen
)
3300 * Try to fix it by moving the start up.
3302 if (align_off
+ temp
<= orig_off
) {
3307 * Try to fix it by moving the end in.
3309 else if (align_off
+ align_alen
- temp
>= orig_end
)
3312 * Set the start to the minimum then trim the length.
3315 align_alen
-= orig_off
- align_off
;
3316 align_off
= orig_off
;
3317 align_alen
-= align_alen
% mp
->m_sb
.sb_rextsize
;
3320 * Result doesn't cover the request, fail it.
3322 if (orig_off
< align_off
|| orig_end
> align_off
+ align_alen
)
3325 ASSERT(orig_off
>= align_off
);
3326 /* see MAXEXTLEN handling above */
3327 ASSERT(orig_end
<= align_off
+ align_alen
||
3328 align_alen
+ extsz
> MAXEXTLEN
);
3332 if (!eof
&& gotp
->br_startoff
!= NULLFILEOFF
)
3333 ASSERT(align_off
+ align_alen
<= gotp
->br_startoff
);
3334 if (prevp
->br_startoff
!= NULLFILEOFF
)
3335 ASSERT(align_off
>= prevp
->br_startoff
+ prevp
->br_blockcount
);
3343 #define XFS_ALLOC_GAP_UNITS 4
3347 struct xfs_bmalloca
*ap
) /* bmap alloc argument struct */
3349 xfs_fsblock_t adjust
; /* adjustment to block numbers */
3350 xfs_agnumber_t fb_agno
; /* ag number of ap->firstblock */
3351 xfs_mount_t
*mp
; /* mount point structure */
3352 int nullfb
; /* true if ap->firstblock isn't set */
3353 int rt
; /* true if inode is realtime */
3355 #define ISVALID(x,y) \
3357 (x) < mp->m_sb.sb_rblocks : \
3358 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
3359 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
3360 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
3362 mp
= ap
->ip
->i_mount
;
3363 nullfb
= *ap
->firstblock
== NULLFSBLOCK
;
3364 rt
= XFS_IS_REALTIME_INODE(ap
->ip
) && ap
->userdata
;
3365 fb_agno
= nullfb
? NULLAGNUMBER
: XFS_FSB_TO_AGNO(mp
, *ap
->firstblock
);
3367 * If allocating at eof, and there's a previous real block,
3368 * try to use its last block as our starting point.
3370 if (ap
->eof
&& ap
->prev
.br_startoff
!= NULLFILEOFF
&&
3371 !isnullstartblock(ap
->prev
.br_startblock
) &&
3372 ISVALID(ap
->prev
.br_startblock
+ ap
->prev
.br_blockcount
,
3373 ap
->prev
.br_startblock
)) {
3374 ap
->blkno
= ap
->prev
.br_startblock
+ ap
->prev
.br_blockcount
;
3376 * Adjust for the gap between prevp and us.
3378 adjust
= ap
->offset
-
3379 (ap
->prev
.br_startoff
+ ap
->prev
.br_blockcount
);
3381 ISVALID(ap
->blkno
+ adjust
, ap
->prev
.br_startblock
))
3382 ap
->blkno
+= adjust
;
3385 * If not at eof, then compare the two neighbor blocks.
3386 * Figure out whether either one gives us a good starting point,
3387 * and pick the better one.
3389 else if (!ap
->eof
) {
3390 xfs_fsblock_t gotbno
; /* right side block number */
3391 xfs_fsblock_t gotdiff
=0; /* right side difference */
3392 xfs_fsblock_t prevbno
; /* left side block number */
3393 xfs_fsblock_t prevdiff
=0; /* left side difference */
3396 * If there's a previous (left) block, select a requested
3397 * start block based on it.
3399 if (ap
->prev
.br_startoff
!= NULLFILEOFF
&&
3400 !isnullstartblock(ap
->prev
.br_startblock
) &&
3401 (prevbno
= ap
->prev
.br_startblock
+
3402 ap
->prev
.br_blockcount
) &&
3403 ISVALID(prevbno
, ap
->prev
.br_startblock
)) {
3405 * Calculate gap to end of previous block.
3407 adjust
= prevdiff
= ap
->offset
-
3408 (ap
->prev
.br_startoff
+
3409 ap
->prev
.br_blockcount
);
3411 * Figure the startblock based on the previous block's
3412 * end and the gap size.
3414 * If the gap is large relative to the piece we're
3415 * allocating, or using it gives us an invalid block
3416 * number, then just use the end of the previous block.
3418 if (prevdiff
<= XFS_ALLOC_GAP_UNITS
* ap
->length
&&
3419 ISVALID(prevbno
+ prevdiff
,
3420 ap
->prev
.br_startblock
))
3425 * If the firstblock forbids it, can't use it,
3428 if (!rt
&& !nullfb
&&
3429 XFS_FSB_TO_AGNO(mp
, prevbno
) != fb_agno
)
3430 prevbno
= NULLFSBLOCK
;
3433 * No previous block or can't follow it, just default.
3436 prevbno
= NULLFSBLOCK
;
3438 * If there's a following (right) block, select a requested
3439 * start block based on it.
3441 if (!isnullstartblock(ap
->got
.br_startblock
)) {
3443 * Calculate gap to start of next block.
3445 adjust
= gotdiff
= ap
->got
.br_startoff
- ap
->offset
;
3447 * Figure the startblock based on the next block's
3448 * start and the gap size.
3450 gotbno
= ap
->got
.br_startblock
;
3453 * If the gap is large relative to the piece we're
3454 * allocating, or using it gives us an invalid block
3455 * number, then just use the start of the next block
3456 * offset by our length.
3458 if (gotdiff
<= XFS_ALLOC_GAP_UNITS
* ap
->length
&&
3459 ISVALID(gotbno
- gotdiff
, gotbno
))
3461 else if (ISVALID(gotbno
- ap
->length
, gotbno
)) {
3462 gotbno
-= ap
->length
;
3463 gotdiff
+= adjust
- ap
->length
;
3467 * If the firstblock forbids it, can't use it,
3470 if (!rt
&& !nullfb
&&
3471 XFS_FSB_TO_AGNO(mp
, gotbno
) != fb_agno
)
3472 gotbno
= NULLFSBLOCK
;
3475 * No next block, just default.
3478 gotbno
= NULLFSBLOCK
;
3480 * If both valid, pick the better one, else the only good
3481 * one, else ap->blkno is already set (to 0 or the inode block).
3483 if (prevbno
!= NULLFSBLOCK
&& gotbno
!= NULLFSBLOCK
)
3484 ap
->blkno
= prevdiff
<= gotdiff
? prevbno
: gotbno
;
3485 else if (prevbno
!= NULLFSBLOCK
)
3486 ap
->blkno
= prevbno
;
3487 else if (gotbno
!= NULLFSBLOCK
)
3494 xfs_bmap_longest_free_extent(
3495 struct xfs_trans
*tp
,
3500 struct xfs_mount
*mp
= tp
->t_mountp
;
3501 struct xfs_perag
*pag
;
3502 xfs_extlen_t longest
;
3505 pag
= xfs_perag_get(mp
, ag
);
3506 if (!pag
->pagf_init
) {
3507 error
= xfs_alloc_pagf_init(mp
, tp
, ag
, XFS_ALLOC_FLAG_TRYLOCK
);
3511 if (!pag
->pagf_init
) {
3517 longest
= xfs_alloc_longest_free_extent(mp
, pag
,
3518 xfs_alloc_min_freelist(mp
, pag
));
3519 if (*blen
< longest
)
3528 xfs_bmap_select_minlen(
3529 struct xfs_bmalloca
*ap
,
3530 struct xfs_alloc_arg
*args
,
3534 if (notinit
|| *blen
< ap
->minlen
) {
3536 * Since we did a BUF_TRYLOCK above, it is possible that
3537 * there is space for this request.
3539 args
->minlen
= ap
->minlen
;
3540 } else if (*blen
< args
->maxlen
) {
3542 * If the best seen length is less than the request length,
3543 * use the best as the minimum.
3545 args
->minlen
= *blen
;
3548 * Otherwise we've seen an extent as big as maxlen, use that
3551 args
->minlen
= args
->maxlen
;
3556 xfs_bmap_btalloc_nullfb(
3557 struct xfs_bmalloca
*ap
,
3558 struct xfs_alloc_arg
*args
,
3561 struct xfs_mount
*mp
= ap
->ip
->i_mount
;
3562 xfs_agnumber_t ag
, startag
;
3566 args
->type
= XFS_ALLOCTYPE_START_BNO
;
3567 args
->total
= ap
->total
;
3569 startag
= ag
= XFS_FSB_TO_AGNO(mp
, args
->fsbno
);
3570 if (startag
== NULLAGNUMBER
)
3573 while (*blen
< args
->maxlen
) {
3574 error
= xfs_bmap_longest_free_extent(args
->tp
, ag
, blen
,
3579 if (++ag
== mp
->m_sb
.sb_agcount
)
3585 xfs_bmap_select_minlen(ap
, args
, blen
, notinit
);
3590 xfs_bmap_btalloc_filestreams(
3591 struct xfs_bmalloca
*ap
,
3592 struct xfs_alloc_arg
*args
,
3595 struct xfs_mount
*mp
= ap
->ip
->i_mount
;
3600 args
->type
= XFS_ALLOCTYPE_NEAR_BNO
;
3601 args
->total
= ap
->total
;
3603 ag
= XFS_FSB_TO_AGNO(mp
, args
->fsbno
);
3604 if (ag
== NULLAGNUMBER
)
3607 error
= xfs_bmap_longest_free_extent(args
->tp
, ag
, blen
, ¬init
);
3611 if (*blen
< args
->maxlen
) {
3612 error
= xfs_filestream_new_ag(ap
, &ag
);
3616 error
= xfs_bmap_longest_free_extent(args
->tp
, ag
, blen
,
3623 xfs_bmap_select_minlen(ap
, args
, blen
, notinit
);
3626 * Set the failure fallback case to look in the selected AG as stream
3629 ap
->blkno
= args
->fsbno
= XFS_AGB_TO_FSB(mp
, ag
, 0);
3635 struct xfs_bmalloca
*ap
) /* bmap alloc argument struct */
3637 xfs_mount_t
*mp
; /* mount point structure */
3638 xfs_alloctype_t atype
= 0; /* type for allocation routines */
3639 xfs_extlen_t align
; /* minimum allocation alignment */
3640 xfs_agnumber_t fb_agno
; /* ag number of ap->firstblock */
3642 xfs_alloc_arg_t args
;
3644 xfs_extlen_t nextminlen
= 0;
3645 int nullfb
; /* true if ap->firstblock isn't set */
3653 mp
= ap
->ip
->i_mount
;
3655 /* stripe alignment for allocation is determined by mount parameters */
3657 if (mp
->m_swidth
&& (mp
->m_flags
& XFS_MOUNT_SWALLOC
))
3658 stripe_align
= mp
->m_swidth
;
3659 else if (mp
->m_dalign
)
3660 stripe_align
= mp
->m_dalign
;
3662 align
= ap
->userdata
? xfs_get_extsz_hint(ap
->ip
) : 0;
3663 if (unlikely(align
)) {
3664 error
= xfs_bmap_extsize_align(mp
, &ap
->got
, &ap
->prev
,
3665 align
, 0, ap
->eof
, 0, ap
->conv
,
3666 &ap
->offset
, &ap
->length
);
3672 nullfb
= *ap
->firstblock
== NULLFSBLOCK
;
3673 fb_agno
= nullfb
? NULLAGNUMBER
: XFS_FSB_TO_AGNO(mp
, *ap
->firstblock
);
3675 if (ap
->userdata
&& xfs_inode_is_filestream(ap
->ip
)) {
3676 ag
= xfs_filestream_lookup_ag(ap
->ip
);
3677 ag
= (ag
!= NULLAGNUMBER
) ? ag
: 0;
3678 ap
->blkno
= XFS_AGB_TO_FSB(mp
, ag
, 0);
3680 ap
->blkno
= XFS_INO_TO_FSB(mp
, ap
->ip
->i_ino
);
3683 ap
->blkno
= *ap
->firstblock
;
3685 xfs_bmap_adjacent(ap
);
3688 * If allowed, use ap->blkno; otherwise must use firstblock since
3689 * it's in the right allocation group.
3691 if (nullfb
|| XFS_FSB_TO_AGNO(mp
, ap
->blkno
) == fb_agno
)
3694 ap
->blkno
= *ap
->firstblock
;
3696 * Normal allocation, done through xfs_alloc_vextent.
3698 tryagain
= isaligned
= 0;
3699 memset(&args
, 0, sizeof(args
));
3702 args
.fsbno
= ap
->blkno
;
3704 /* Trim the allocation back to the maximum an AG can fit. */
3705 args
.maxlen
= MIN(ap
->length
, XFS_ALLOC_AG_MAX_USABLE(mp
));
3706 args
.firstblock
= *ap
->firstblock
;
3710 * Search for an allocation group with a single extent large
3711 * enough for the request. If one isn't found, then adjust
3712 * the minimum allocation size to the largest space found.
3714 if (ap
->userdata
&& xfs_inode_is_filestream(ap
->ip
))
3715 error
= xfs_bmap_btalloc_filestreams(ap
, &args
, &blen
);
3717 error
= xfs_bmap_btalloc_nullfb(ap
, &args
, &blen
);
3720 } else if (ap
->flist
->xbf_low
) {
3721 if (xfs_inode_is_filestream(ap
->ip
))
3722 args
.type
= XFS_ALLOCTYPE_FIRST_AG
;
3724 args
.type
= XFS_ALLOCTYPE_START_BNO
;
3725 args
.total
= args
.minlen
= ap
->minlen
;
3727 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
3728 args
.total
= ap
->total
;
3729 args
.minlen
= ap
->minlen
;
3731 /* apply extent size hints if obtained earlier */
3732 if (unlikely(align
)) {
3734 if ((args
.mod
= (xfs_extlen_t
)do_mod(ap
->offset
, args
.prod
)))
3735 args
.mod
= (xfs_extlen_t
)(args
.prod
- args
.mod
);
3736 } else if (mp
->m_sb
.sb_blocksize
>= PAGE_SIZE
) {
3740 args
.prod
= PAGE_SIZE
>> mp
->m_sb
.sb_blocklog
;
3741 if ((args
.mod
= (xfs_extlen_t
)(do_mod(ap
->offset
, args
.prod
))))
3742 args
.mod
= (xfs_extlen_t
)(args
.prod
- args
.mod
);
3745 * If we are not low on available data blocks, and the
3746 * underlying logical volume manager is a stripe, and
3747 * the file offset is zero then try to allocate data
3748 * blocks on stripe unit boundary.
3749 * NOTE: ap->aeof is only set if the allocation length
3750 * is >= the stripe unit and the allocation offset is
3751 * at the end of file.
3753 if (!ap
->flist
->xbf_low
&& ap
->aeof
) {
3755 args
.alignment
= stripe_align
;
3759 * Adjust for alignment
3761 if (blen
> args
.alignment
&& blen
<= args
.maxlen
)
3762 args
.minlen
= blen
- args
.alignment
;
3763 args
.minalignslop
= 0;
3766 * First try an exact bno allocation.
3767 * If it fails then do a near or start bno
3768 * allocation with alignment turned on.
3772 args
.type
= XFS_ALLOCTYPE_THIS_BNO
;
3775 * Compute the minlen+alignment for the
3776 * next case. Set slop so that the value
3777 * of minlen+alignment+slop doesn't go up
3778 * between the calls.
3780 if (blen
> stripe_align
&& blen
<= args
.maxlen
)
3781 nextminlen
= blen
- stripe_align
;
3783 nextminlen
= args
.minlen
;
3784 if (nextminlen
+ stripe_align
> args
.minlen
+ 1)
3786 nextminlen
+ stripe_align
-
3789 args
.minalignslop
= 0;
3793 args
.minalignslop
= 0;
3795 args
.minleft
= ap
->minleft
;
3796 args
.wasdel
= ap
->wasdel
;
3798 args
.userdata
= ap
->userdata
;
3799 if (ap
->userdata
& XFS_ALLOC_USERDATA_ZERO
)
3802 error
= xfs_alloc_vextent(&args
);
3806 if (tryagain
&& args
.fsbno
== NULLFSBLOCK
) {
3808 * Exact allocation failed. Now try with alignment
3812 args
.fsbno
= ap
->blkno
;
3813 args
.alignment
= stripe_align
;
3814 args
.minlen
= nextminlen
;
3815 args
.minalignslop
= 0;
3817 if ((error
= xfs_alloc_vextent(&args
)))
3820 if (isaligned
&& args
.fsbno
== NULLFSBLOCK
) {
3822 * allocation failed, so turn off alignment and
3826 args
.fsbno
= ap
->blkno
;
3828 if ((error
= xfs_alloc_vextent(&args
)))
3831 if (args
.fsbno
== NULLFSBLOCK
&& nullfb
&&
3832 args
.minlen
> ap
->minlen
) {
3833 args
.minlen
= ap
->minlen
;
3834 args
.type
= XFS_ALLOCTYPE_START_BNO
;
3835 args
.fsbno
= ap
->blkno
;
3836 if ((error
= xfs_alloc_vextent(&args
)))
3839 if (args
.fsbno
== NULLFSBLOCK
&& nullfb
) {
3841 args
.type
= XFS_ALLOCTYPE_FIRST_AG
;
3842 args
.total
= ap
->minlen
;
3844 if ((error
= xfs_alloc_vextent(&args
)))
3846 ap
->flist
->xbf_low
= 1;
3848 if (args
.fsbno
!= NULLFSBLOCK
) {
3850 * check the allocation happened at the same or higher AG than
3851 * the first block that was allocated.
3853 ASSERT(*ap
->firstblock
== NULLFSBLOCK
||
3854 XFS_FSB_TO_AGNO(mp
, *ap
->firstblock
) ==
3855 XFS_FSB_TO_AGNO(mp
, args
.fsbno
) ||
3856 (ap
->flist
->xbf_low
&&
3857 XFS_FSB_TO_AGNO(mp
, *ap
->firstblock
) <
3858 XFS_FSB_TO_AGNO(mp
, args
.fsbno
)));
3860 ap
->blkno
= args
.fsbno
;
3861 if (*ap
->firstblock
== NULLFSBLOCK
)
3862 *ap
->firstblock
= args
.fsbno
;
3863 ASSERT(nullfb
|| fb_agno
== args
.agno
||
3864 (ap
->flist
->xbf_low
&& fb_agno
< args
.agno
));
3865 ap
->length
= args
.len
;
3866 ap
->ip
->i_d
.di_nblocks
+= args
.len
;
3867 xfs_trans_log_inode(ap
->tp
, ap
->ip
, XFS_ILOG_CORE
);
3869 ap
->ip
->i_delayed_blks
-= args
.len
;
3871 * Adjust the disk quota also. This was reserved
3874 xfs_trans_mod_dquot_byino(ap
->tp
, ap
->ip
,
3875 ap
->wasdel
? XFS_TRANS_DQ_DELBCOUNT
:
3876 XFS_TRANS_DQ_BCOUNT
,
3879 ap
->blkno
= NULLFSBLOCK
;
3886 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
3887 * It figures out where to ask the underlying allocator to put the new extent.
3891 struct xfs_bmalloca
*ap
) /* bmap alloc argument struct */
3893 if (XFS_IS_REALTIME_INODE(ap
->ip
) && ap
->userdata
)
3894 return xfs_bmap_rtalloc(ap
);
3895 return xfs_bmap_btalloc(ap
);
3899 * Trim the returned map to the required bounds
3903 struct xfs_bmbt_irec
*mval
,
3904 struct xfs_bmbt_irec
*got
,
3912 if ((flags
& XFS_BMAPI_ENTIRE
) ||
3913 got
->br_startoff
+ got
->br_blockcount
<= obno
) {
3915 if (isnullstartblock(got
->br_startblock
))
3916 mval
->br_startblock
= DELAYSTARTBLOCK
;
3922 ASSERT((*bno
>= obno
) || (n
== 0));
3924 mval
->br_startoff
= *bno
;
3925 if (isnullstartblock(got
->br_startblock
))
3926 mval
->br_startblock
= DELAYSTARTBLOCK
;
3928 mval
->br_startblock
= got
->br_startblock
+
3929 (*bno
- got
->br_startoff
);
3931 * Return the minimum of what we got and what we asked for for
3932 * the length. We can use the len variable here because it is
3933 * modified below and we could have been there before coming
3934 * here if the first part of the allocation didn't overlap what
3937 mval
->br_blockcount
= XFS_FILBLKS_MIN(end
- *bno
,
3938 got
->br_blockcount
- (*bno
- got
->br_startoff
));
3939 mval
->br_state
= got
->br_state
;
3940 ASSERT(mval
->br_blockcount
<= len
);
3945 * Update and validate the extent map to return
3948 xfs_bmapi_update_map(
3949 struct xfs_bmbt_irec
**map
,
3957 xfs_bmbt_irec_t
*mval
= *map
;
3959 ASSERT((flags
& XFS_BMAPI_ENTIRE
) ||
3960 ((mval
->br_startoff
+ mval
->br_blockcount
) <= end
));
3961 ASSERT((flags
& XFS_BMAPI_ENTIRE
) || (mval
->br_blockcount
<= *len
) ||
3962 (mval
->br_startoff
< obno
));
3964 *bno
= mval
->br_startoff
+ mval
->br_blockcount
;
3966 if (*n
> 0 && mval
->br_startoff
== mval
[-1].br_startoff
) {
3967 /* update previous map with new information */
3968 ASSERT(mval
->br_startblock
== mval
[-1].br_startblock
);
3969 ASSERT(mval
->br_blockcount
> mval
[-1].br_blockcount
);
3970 ASSERT(mval
->br_state
== mval
[-1].br_state
);
3971 mval
[-1].br_blockcount
= mval
->br_blockcount
;
3972 mval
[-1].br_state
= mval
->br_state
;
3973 } else if (*n
> 0 && mval
->br_startblock
!= DELAYSTARTBLOCK
&&
3974 mval
[-1].br_startblock
!= DELAYSTARTBLOCK
&&
3975 mval
[-1].br_startblock
!= HOLESTARTBLOCK
&&
3976 mval
->br_startblock
== mval
[-1].br_startblock
+
3977 mval
[-1].br_blockcount
&&
3978 ((flags
& XFS_BMAPI_IGSTATE
) ||
3979 mval
[-1].br_state
== mval
->br_state
)) {
3980 ASSERT(mval
->br_startoff
==
3981 mval
[-1].br_startoff
+ mval
[-1].br_blockcount
);
3982 mval
[-1].br_blockcount
+= mval
->br_blockcount
;
3983 } else if (*n
> 0 &&
3984 mval
->br_startblock
== DELAYSTARTBLOCK
&&
3985 mval
[-1].br_startblock
== DELAYSTARTBLOCK
&&
3986 mval
->br_startoff
==
3987 mval
[-1].br_startoff
+ mval
[-1].br_blockcount
) {
3988 mval
[-1].br_blockcount
+= mval
->br_blockcount
;
3989 mval
[-1].br_state
= mval
->br_state
;
3990 } else if (!((*n
== 0) &&
3991 ((mval
->br_startoff
+ mval
->br_blockcount
) <=
4000 * Map file blocks to filesystem blocks without allocation.
4004 struct xfs_inode
*ip
,
4007 struct xfs_bmbt_irec
*mval
,
4011 struct xfs_mount
*mp
= ip
->i_mount
;
4012 struct xfs_ifork
*ifp
;
4013 struct xfs_bmbt_irec got
;
4014 struct xfs_bmbt_irec prev
;
4021 int whichfork
= (flags
& XFS_BMAPI_ATTRFORK
) ?
4022 XFS_ATTR_FORK
: XFS_DATA_FORK
;
4025 ASSERT(!(flags
& ~(XFS_BMAPI_ATTRFORK
|XFS_BMAPI_ENTIRE
|
4026 XFS_BMAPI_IGSTATE
)));
4027 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_SHARED
|XFS_ILOCK_EXCL
));
4029 if (unlikely(XFS_TEST_ERROR(
4030 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
4031 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
4032 mp
, XFS_ERRTAG_BMAPIFORMAT
, XFS_RANDOM_BMAPIFORMAT
))) {
4033 XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW
, mp
);
4034 return -EFSCORRUPTED
;
4037 if (XFS_FORCED_SHUTDOWN(mp
))
4040 XFS_STATS_INC(mp
, xs_blk_mapr
);
4042 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4044 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
4045 error
= xfs_iread_extents(NULL
, ip
, whichfork
);
4050 xfs_bmap_search_extents(ip
, bno
, whichfork
, &eof
, &lastx
, &got
, &prev
);
4054 while (bno
< end
&& n
< *nmap
) {
4055 /* Reading past eof, act as though there's a hole up to end. */
4057 got
.br_startoff
= end
;
4058 if (got
.br_startoff
> bno
) {
4059 /* Reading in a hole. */
4060 mval
->br_startoff
= bno
;
4061 mval
->br_startblock
= HOLESTARTBLOCK
;
4062 mval
->br_blockcount
=
4063 XFS_FILBLKS_MIN(len
, got
.br_startoff
- bno
);
4064 mval
->br_state
= XFS_EXT_NORM
;
4065 bno
+= mval
->br_blockcount
;
4066 len
-= mval
->br_blockcount
;
4072 /* set up the extent map to return. */
4073 xfs_bmapi_trim_map(mval
, &got
, &bno
, len
, obno
, end
, n
, flags
);
4074 xfs_bmapi_update_map(&mval
, &bno
, &len
, obno
, end
, &n
, flags
);
4076 /* If we're done, stop now. */
4077 if (bno
>= end
|| n
>= *nmap
)
4080 /* Else go on to the next record. */
4081 if (++lastx
< ifp
->if_bytes
/ sizeof(xfs_bmbt_rec_t
))
4082 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, lastx
), &got
);
4091 xfs_bmapi_reserve_delalloc(
4092 struct xfs_inode
*ip
,
4095 struct xfs_bmbt_irec
*got
,
4096 struct xfs_bmbt_irec
*prev
,
4097 xfs_extnum_t
*lastx
,
4100 struct xfs_mount
*mp
= ip
->i_mount
;
4101 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, XFS_DATA_FORK
);
4103 xfs_extlen_t indlen
;
4104 char rt
= XFS_IS_REALTIME_INODE(ip
);
4108 alen
= XFS_FILBLKS_MIN(len
, MAXEXTLEN
);
4110 alen
= XFS_FILBLKS_MIN(alen
, got
->br_startoff
- aoff
);
4112 /* Figure out the extent size, adjust alen */
4113 extsz
= xfs_get_extsz_hint(ip
);
4115 error
= xfs_bmap_extsize_align(mp
, got
, prev
, extsz
, rt
, eof
,
4116 1, 0, &aoff
, &alen
);
4121 extsz
= alen
/ mp
->m_sb
.sb_rextsize
;
4124 * Make a transaction-less quota reservation for delayed allocation
4125 * blocks. This number gets adjusted later. We return if we haven't
4126 * allocated blocks already inside this loop.
4128 error
= xfs_trans_reserve_quota_nblks(NULL
, ip
, (long)alen
, 0,
4129 rt
? XFS_QMOPT_RES_RTBLKS
: XFS_QMOPT_RES_REGBLKS
);
4134 * Split changing sb for alen and indlen since they could be coming
4135 * from different places.
4137 indlen
= (xfs_extlen_t
)xfs_bmap_worst_indlen(ip
, alen
);
4141 error
= xfs_mod_frextents(mp
, -((int64_t)extsz
));
4143 error
= xfs_mod_fdblocks(mp
, -((int64_t)alen
), false);
4147 goto out_unreserve_quota
;
4149 error
= xfs_mod_fdblocks(mp
, -((int64_t)indlen
), false);
4151 goto out_unreserve_blocks
;
4154 ip
->i_delayed_blks
+= alen
;
4156 got
->br_startoff
= aoff
;
4157 got
->br_startblock
= nullstartblock(indlen
);
4158 got
->br_blockcount
= alen
;
4159 got
->br_state
= XFS_EXT_NORM
;
4160 xfs_bmap_add_extent_hole_delay(ip
, lastx
, got
);
4163 * Update our extent pointer, given that xfs_bmap_add_extent_hole_delay
4164 * might have merged it into one of the neighbouring ones.
4166 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, *lastx
), got
);
4168 ASSERT(got
->br_startoff
<= aoff
);
4169 ASSERT(got
->br_startoff
+ got
->br_blockcount
>= aoff
+ alen
);
4170 ASSERT(isnullstartblock(got
->br_startblock
));
4171 ASSERT(got
->br_state
== XFS_EXT_NORM
);
4174 out_unreserve_blocks
:
4176 xfs_mod_frextents(mp
, extsz
);
4178 xfs_mod_fdblocks(mp
, alen
, false);
4179 out_unreserve_quota
:
4180 if (XFS_IS_QUOTA_ON(mp
))
4181 xfs_trans_unreserve_quota_nblks(NULL
, ip
, (long)alen
, 0, rt
?
4182 XFS_QMOPT_RES_RTBLKS
: XFS_QMOPT_RES_REGBLKS
);
4187 * Map file blocks to filesystem blocks, adding delayed allocations as needed.
4191 struct xfs_inode
*ip
, /* incore inode */
4192 xfs_fileoff_t bno
, /* starting file offs. mapped */
4193 xfs_filblks_t len
, /* length to map in file */
4194 struct xfs_bmbt_irec
*mval
, /* output: map values */
4195 int *nmap
, /* i/o: mval size/count */
4196 int flags
) /* XFS_BMAPI_... */
4198 struct xfs_mount
*mp
= ip
->i_mount
;
4199 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, XFS_DATA_FORK
);
4200 struct xfs_bmbt_irec got
; /* current file extent record */
4201 struct xfs_bmbt_irec prev
; /* previous file extent record */
4202 xfs_fileoff_t obno
; /* old block number (offset) */
4203 xfs_fileoff_t end
; /* end of mapped file region */
4204 xfs_extnum_t lastx
; /* last useful extent number */
4205 int eof
; /* we've hit the end of extents */
4206 int n
= 0; /* current extent index */
4210 ASSERT(*nmap
<= XFS_BMAP_MAX_NMAP
);
4211 ASSERT(!(flags
& ~XFS_BMAPI_ENTIRE
));
4212 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
4214 if (unlikely(XFS_TEST_ERROR(
4215 (XFS_IFORK_FORMAT(ip
, XFS_DATA_FORK
) != XFS_DINODE_FMT_EXTENTS
&&
4216 XFS_IFORK_FORMAT(ip
, XFS_DATA_FORK
) != XFS_DINODE_FMT_BTREE
),
4217 mp
, XFS_ERRTAG_BMAPIFORMAT
, XFS_RANDOM_BMAPIFORMAT
))) {
4218 XFS_ERROR_REPORT("xfs_bmapi_delay", XFS_ERRLEVEL_LOW
, mp
);
4219 return -EFSCORRUPTED
;
4222 if (XFS_FORCED_SHUTDOWN(mp
))
4225 XFS_STATS_INC(mp
, xs_blk_mapw
);
4227 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
4228 error
= xfs_iread_extents(NULL
, ip
, XFS_DATA_FORK
);
4233 xfs_bmap_search_extents(ip
, bno
, XFS_DATA_FORK
, &eof
, &lastx
, &got
, &prev
);
4237 while (bno
< end
&& n
< *nmap
) {
4238 if (eof
|| got
.br_startoff
> bno
) {
4239 error
= xfs_bmapi_reserve_delalloc(ip
, bno
, len
, &got
,
4240 &prev
, &lastx
, eof
);
4250 /* set up the extent map to return. */
4251 xfs_bmapi_trim_map(mval
, &got
, &bno
, len
, obno
, end
, n
, flags
);
4252 xfs_bmapi_update_map(&mval
, &bno
, &len
, obno
, end
, &n
, flags
);
4254 /* If we're done, stop now. */
4255 if (bno
>= end
|| n
>= *nmap
)
4258 /* Else go on to the next record. */
4260 if (++lastx
< ifp
->if_bytes
/ sizeof(xfs_bmbt_rec_t
))
4261 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, lastx
), &got
);
4273 struct xfs_bmalloca
*bma
)
4275 struct xfs_mount
*mp
= bma
->ip
->i_mount
;
4276 int whichfork
= (bma
->flags
& XFS_BMAPI_ATTRFORK
) ?
4277 XFS_ATTR_FORK
: XFS_DATA_FORK
;
4278 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
4279 int tmp_logflags
= 0;
4282 ASSERT(bma
->length
> 0);
4285 * For the wasdelay case, we could also just allocate the stuff asked
4286 * for in this bmap call but that wouldn't be as good.
4289 bma
->length
= (xfs_extlen_t
)bma
->got
.br_blockcount
;
4290 bma
->offset
= bma
->got
.br_startoff
;
4291 if (bma
->idx
!= NULLEXTNUM
&& bma
->idx
) {
4292 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
- 1),
4296 bma
->length
= XFS_FILBLKS_MIN(bma
->length
, MAXEXTLEN
);
4298 bma
->length
= XFS_FILBLKS_MIN(bma
->length
,
4299 bma
->got
.br_startoff
- bma
->offset
);
4303 * Indicate if this is the first user data in the file, or just any
4304 * user data. And if it is userdata, indicate whether it needs to
4305 * be initialised to zero during allocation.
4307 if (!(bma
->flags
& XFS_BMAPI_METADATA
)) {
4308 bma
->userdata
= (bma
->offset
== 0) ?
4309 XFS_ALLOC_INITIAL_USER_DATA
: XFS_ALLOC_USERDATA
;
4310 if (bma
->flags
& XFS_BMAPI_ZERO
)
4311 bma
->userdata
|= XFS_ALLOC_USERDATA_ZERO
;
4314 bma
->minlen
= (bma
->flags
& XFS_BMAPI_CONTIG
) ? bma
->length
: 1;
4317 * Only want to do the alignment at the eof if it is userdata and
4318 * allocation length is larger than a stripe unit.
4320 if (mp
->m_dalign
&& bma
->length
>= mp
->m_dalign
&&
4321 !(bma
->flags
& XFS_BMAPI_METADATA
) && whichfork
== XFS_DATA_FORK
) {
4322 error
= xfs_bmap_isaeof(bma
, whichfork
);
4327 error
= xfs_bmap_alloc(bma
);
4331 if (bma
->flist
->xbf_low
)
4334 bma
->cur
->bc_private
.b
.firstblock
= *bma
->firstblock
;
4335 if (bma
->blkno
== NULLFSBLOCK
)
4337 if ((ifp
->if_flags
& XFS_IFBROOT
) && !bma
->cur
) {
4338 bma
->cur
= xfs_bmbt_init_cursor(mp
, bma
->tp
, bma
->ip
, whichfork
);
4339 bma
->cur
->bc_private
.b
.firstblock
= *bma
->firstblock
;
4340 bma
->cur
->bc_private
.b
.flist
= bma
->flist
;
4343 * Bump the number of extents we've allocated
4349 bma
->cur
->bc_private
.b
.flags
=
4350 bma
->wasdel
? XFS_BTCUR_BPRV_WASDEL
: 0;
4352 bma
->got
.br_startoff
= bma
->offset
;
4353 bma
->got
.br_startblock
= bma
->blkno
;
4354 bma
->got
.br_blockcount
= bma
->length
;
4355 bma
->got
.br_state
= XFS_EXT_NORM
;
4358 * A wasdelay extent has been initialized, so shouldn't be flagged
4361 if (!bma
->wasdel
&& (bma
->flags
& XFS_BMAPI_PREALLOC
) &&
4362 xfs_sb_version_hasextflgbit(&mp
->m_sb
))
4363 bma
->got
.br_state
= XFS_EXT_UNWRITTEN
;
4366 error
= xfs_bmap_add_extent_delay_real(bma
);
4368 error
= xfs_bmap_add_extent_hole_real(bma
, whichfork
);
4370 bma
->logflags
|= tmp_logflags
;
4375 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4376 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4377 * the neighbouring ones.
4379 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
), &bma
->got
);
4381 ASSERT(bma
->got
.br_startoff
<= bma
->offset
);
4382 ASSERT(bma
->got
.br_startoff
+ bma
->got
.br_blockcount
>=
4383 bma
->offset
+ bma
->length
);
4384 ASSERT(bma
->got
.br_state
== XFS_EXT_NORM
||
4385 bma
->got
.br_state
== XFS_EXT_UNWRITTEN
);
4390 xfs_bmapi_convert_unwritten(
4391 struct xfs_bmalloca
*bma
,
4392 struct xfs_bmbt_irec
*mval
,
4396 int whichfork
= (flags
& XFS_BMAPI_ATTRFORK
) ?
4397 XFS_ATTR_FORK
: XFS_DATA_FORK
;
4398 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
4399 int tmp_logflags
= 0;
4402 /* check if we need to do unwritten->real conversion */
4403 if (mval
->br_state
== XFS_EXT_UNWRITTEN
&&
4404 (flags
& XFS_BMAPI_PREALLOC
))
4407 /* check if we need to do real->unwritten conversion */
4408 if (mval
->br_state
== XFS_EXT_NORM
&&
4409 (flags
& (XFS_BMAPI_PREALLOC
| XFS_BMAPI_CONVERT
)) !=
4410 (XFS_BMAPI_PREALLOC
| XFS_BMAPI_CONVERT
))
4414 * Modify (by adding) the state flag, if writing.
4416 ASSERT(mval
->br_blockcount
<= len
);
4417 if ((ifp
->if_flags
& XFS_IFBROOT
) && !bma
->cur
) {
4418 bma
->cur
= xfs_bmbt_init_cursor(bma
->ip
->i_mount
, bma
->tp
,
4419 bma
->ip
, whichfork
);
4420 bma
->cur
->bc_private
.b
.firstblock
= *bma
->firstblock
;
4421 bma
->cur
->bc_private
.b
.flist
= bma
->flist
;
4423 mval
->br_state
= (mval
->br_state
== XFS_EXT_UNWRITTEN
)
4424 ? XFS_EXT_NORM
: XFS_EXT_UNWRITTEN
;
4427 * Before insertion into the bmbt, zero the range being converted
4430 if (flags
& XFS_BMAPI_ZERO
) {
4431 error
= xfs_zero_extent(bma
->ip
, mval
->br_startblock
,
4432 mval
->br_blockcount
);
4437 error
= xfs_bmap_add_extent_unwritten_real(bma
->tp
, bma
->ip
, &bma
->idx
,
4438 &bma
->cur
, mval
, bma
->firstblock
, bma
->flist
,
4441 * Log the inode core unconditionally in the unwritten extent conversion
4442 * path because the conversion might not have done so (e.g., if the
4443 * extent count hasn't changed). We need to make sure the inode is dirty
4444 * in the transaction for the sake of fsync(), even if nothing has
4445 * changed, because fsync() will not force the log for this transaction
4446 * unless it sees the inode pinned.
4448 bma
->logflags
|= tmp_logflags
| XFS_ILOG_CORE
;
4453 * Update our extent pointer, given that
4454 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4455 * of the neighbouring ones.
4457 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
), &bma
->got
);
4460 * We may have combined previously unwritten space with written space,
4461 * so generate another request.
4463 if (mval
->br_blockcount
< len
)
4469 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4470 * extent state if necessary. Details behaviour is controlled by the flags
4471 * parameter. Only allocates blocks from a single allocation group, to avoid
4474 * The returned value in "firstblock" from the first call in a transaction
4475 * must be remembered and presented to subsequent calls in "firstblock".
4476 * An upper bound for the number of blocks to be allocated is supplied to
4477 * the first call in "total"; if no allocation group has that many free
4478 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4482 struct xfs_trans
*tp
, /* transaction pointer */
4483 struct xfs_inode
*ip
, /* incore inode */
4484 xfs_fileoff_t bno
, /* starting file offs. mapped */
4485 xfs_filblks_t len
, /* length to map in file */
4486 int flags
, /* XFS_BMAPI_... */
4487 xfs_fsblock_t
*firstblock
, /* first allocated block
4488 controls a.g. for allocs */
4489 xfs_extlen_t total
, /* total blocks needed */
4490 struct xfs_bmbt_irec
*mval
, /* output: map values */
4491 int *nmap
, /* i/o: mval size/count */
4492 struct xfs_bmap_free
*flist
) /* i/o: list extents to free */
4494 struct xfs_mount
*mp
= ip
->i_mount
;
4495 struct xfs_ifork
*ifp
;
4496 struct xfs_bmalloca bma
= { NULL
}; /* args for xfs_bmap_alloc */
4497 xfs_fileoff_t end
; /* end of mapped file region */
4498 int eof
; /* after the end of extents */
4499 int error
; /* error return */
4500 int n
; /* current extent index */
4501 xfs_fileoff_t obno
; /* old block number (offset) */
4502 int whichfork
; /* data or attr fork */
4503 char inhole
; /* current location is hole in file */
4504 char wasdelay
; /* old extent was delayed */
4507 xfs_fileoff_t orig_bno
; /* original block number value */
4508 int orig_flags
; /* original flags arg value */
4509 xfs_filblks_t orig_len
; /* original value of len arg */
4510 struct xfs_bmbt_irec
*orig_mval
; /* original value of mval */
4511 int orig_nmap
; /* original value of *nmap */
4519 whichfork
= (flags
& XFS_BMAPI_ATTRFORK
) ?
4520 XFS_ATTR_FORK
: XFS_DATA_FORK
;
4523 ASSERT(*nmap
<= XFS_BMAP_MAX_NMAP
);
4524 ASSERT(!(flags
& XFS_BMAPI_IGSTATE
));
4527 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_LOCAL
);
4528 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
4530 /* zeroing is for currently only for data extents, not metadata */
4531 ASSERT((flags
& (XFS_BMAPI_METADATA
| XFS_BMAPI_ZERO
)) !=
4532 (XFS_BMAPI_METADATA
| XFS_BMAPI_ZERO
));
4534 * we can allocate unwritten extents or pre-zero allocated blocks,
4535 * but it makes no sense to do both at once. This would result in
4536 * zeroing the unwritten extent twice, but it still being an
4537 * unwritten extent....
4539 ASSERT((flags
& (XFS_BMAPI_PREALLOC
| XFS_BMAPI_ZERO
)) !=
4540 (XFS_BMAPI_PREALLOC
| XFS_BMAPI_ZERO
));
4542 if (unlikely(XFS_TEST_ERROR(
4543 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
4544 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
4545 mp
, XFS_ERRTAG_BMAPIFORMAT
, XFS_RANDOM_BMAPIFORMAT
))) {
4546 XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW
, mp
);
4547 return -EFSCORRUPTED
;
4550 if (XFS_FORCED_SHUTDOWN(mp
))
4553 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4555 XFS_STATS_INC(mp
, xs_blk_mapw
);
4557 if (*firstblock
== NULLFSBLOCK
) {
4558 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
)
4559 bma
.minleft
= be16_to_cpu(ifp
->if_broot
->bb_level
) + 1;
4566 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
4567 error
= xfs_iread_extents(tp
, ip
, whichfork
);
4572 xfs_bmap_search_extents(ip
, bno
, whichfork
, &eof
, &bma
.idx
, &bma
.got
,
4583 bma
.firstblock
= firstblock
;
4585 while (bno
< end
&& n
< *nmap
) {
4586 inhole
= eof
|| bma
.got
.br_startoff
> bno
;
4587 wasdelay
= !inhole
&& isnullstartblock(bma
.got
.br_startblock
);
4590 * First, deal with the hole before the allocated space
4591 * that we found, if any.
4593 if (inhole
|| wasdelay
) {
4595 bma
.conv
= !!(flags
& XFS_BMAPI_CONVERT
);
4596 bma
.wasdel
= wasdelay
;
4601 * There's a 32/64 bit type mismatch between the
4602 * allocation length request (which can be 64 bits in
4603 * length) and the bma length request, which is
4604 * xfs_extlen_t and therefore 32 bits. Hence we have to
4605 * check for 32-bit overflows and handle them here.
4607 if (len
> (xfs_filblks_t
)MAXEXTLEN
)
4608 bma
.length
= MAXEXTLEN
;
4613 ASSERT(bma
.length
> 0);
4614 error
= xfs_bmapi_allocate(&bma
);
4617 if (bma
.blkno
== NULLFSBLOCK
)
4621 /* Deal with the allocated space we found. */
4622 xfs_bmapi_trim_map(mval
, &bma
.got
, &bno
, len
, obno
,
4625 /* Execute unwritten extent conversion if necessary */
4626 error
= xfs_bmapi_convert_unwritten(&bma
, mval
, len
, flags
);
4627 if (error
== -EAGAIN
)
4632 /* update the extent map to return */
4633 xfs_bmapi_update_map(&mval
, &bno
, &len
, obno
, end
, &n
, flags
);
4636 * If we're done, stop now. Stop when we've allocated
4637 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4638 * the transaction may get too big.
4640 if (bno
>= end
|| n
>= *nmap
|| bma
.nallocs
>= *nmap
)
4643 /* Else go on to the next record. */
4645 if (++bma
.idx
< ifp
->if_bytes
/ sizeof(xfs_bmbt_rec_t
)) {
4646 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
.idx
),
4654 * Transform from btree to extents, give it cur.
4656 if (xfs_bmap_wants_extents(ip
, whichfork
)) {
4657 int tmp_logflags
= 0;
4660 error
= xfs_bmap_btree_to_extents(tp
, ip
, bma
.cur
,
4661 &tmp_logflags
, whichfork
);
4662 bma
.logflags
|= tmp_logflags
;
4667 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
||
4668 XFS_IFORK_NEXTENTS(ip
, whichfork
) >
4669 XFS_IFORK_MAXEXT(ip
, whichfork
));
4673 * Log everything. Do this after conversion, there's no point in
4674 * logging the extent records if we've converted to btree format.
4676 if ((bma
.logflags
& xfs_ilog_fext(whichfork
)) &&
4677 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
4678 bma
.logflags
&= ~xfs_ilog_fext(whichfork
);
4679 else if ((bma
.logflags
& xfs_ilog_fbroot(whichfork
)) &&
4680 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)
4681 bma
.logflags
&= ~xfs_ilog_fbroot(whichfork
);
4683 * Log whatever the flags say, even if error. Otherwise we might miss
4684 * detecting a case where the data is changed, there's an error,
4685 * and it's not logged so we don't shutdown when we should.
4688 xfs_trans_log_inode(tp
, ip
, bma
.logflags
);
4692 ASSERT(*firstblock
== NULLFSBLOCK
||
4693 XFS_FSB_TO_AGNO(mp
, *firstblock
) ==
4695 bma
.cur
->bc_private
.b
.firstblock
) ||
4697 XFS_FSB_TO_AGNO(mp
, *firstblock
) <
4699 bma
.cur
->bc_private
.b
.firstblock
)));
4700 *firstblock
= bma
.cur
->bc_private
.b
.firstblock
;
4702 xfs_btree_del_cursor(bma
.cur
,
4703 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
4706 xfs_bmap_validate_ret(orig_bno
, orig_len
, orig_flags
, orig_mval
,
4712 * When a delalloc extent is split (e.g., due to a hole punch), the original
4713 * indlen reservation must be shared across the two new extents that are left
4716 * Given the original reservation and the worst case indlen for the two new
4717 * extents (as calculated by xfs_bmap_worst_indlen()), split the original
4718 * reservation fairly across the two new extents. If necessary, steal available
4719 * blocks from a deleted extent to make up a reservation deficiency (e.g., if
4720 * ores == 1). The number of stolen blocks is returned. The availability and
4721 * subsequent accounting of stolen blocks is the responsibility of the caller.
4723 static xfs_filblks_t
4724 xfs_bmap_split_indlen(
4725 xfs_filblks_t ores
, /* original res. */
4726 xfs_filblks_t
*indlen1
, /* ext1 worst indlen */
4727 xfs_filblks_t
*indlen2
, /* ext2 worst indlen */
4728 xfs_filblks_t avail
) /* stealable blocks */
4730 xfs_filblks_t len1
= *indlen1
;
4731 xfs_filblks_t len2
= *indlen2
;
4732 xfs_filblks_t nres
= len1
+ len2
; /* new total res. */
4733 xfs_filblks_t stolen
= 0;
4736 * Steal as many blocks as we can to try and satisfy the worst case
4737 * indlen for both new extents.
4739 while (nres
> ores
&& avail
) {
4746 * The only blocks available are those reserved for the original
4747 * extent and what we can steal from the extent being removed.
4748 * If this still isn't enough to satisfy the combined
4749 * requirements for the two new extents, skim blocks off of each
4750 * of the new reservations until they match what is available.
4752 while (nres
> ores
) {
4772 * Called by xfs_bmapi to update file extent records and the btree
4773 * after removing space (or undoing a delayed allocation).
4775 STATIC
int /* error */
4776 xfs_bmap_del_extent(
4777 xfs_inode_t
*ip
, /* incore inode pointer */
4778 xfs_trans_t
*tp
, /* current transaction pointer */
4779 xfs_extnum_t
*idx
, /* extent number to update/delete */
4780 xfs_bmap_free_t
*flist
, /* list of extents to be freed */
4781 xfs_btree_cur_t
*cur
, /* if null, not a btree */
4782 xfs_bmbt_irec_t
*del
, /* data to remove from extents */
4783 int *logflagsp
, /* inode logging flags */
4784 int whichfork
) /* data or attr fork */
4786 xfs_filblks_t da_new
; /* new delay-alloc indirect blocks */
4787 xfs_filblks_t da_old
; /* old delay-alloc indirect blocks */
4788 xfs_fsblock_t del_endblock
=0; /* first block past del */
4789 xfs_fileoff_t del_endoff
; /* first offset past del */
4790 int delay
; /* current block is delayed allocated */
4791 int do_fx
; /* free extent at end of routine */
4792 xfs_bmbt_rec_host_t
*ep
; /* current extent entry pointer */
4793 int error
; /* error return value */
4794 int flags
; /* inode logging flags */
4795 xfs_bmbt_irec_t got
; /* current extent entry */
4796 xfs_fileoff_t got_endoff
; /* first offset past got */
4797 int i
; /* temp state */
4798 xfs_ifork_t
*ifp
; /* inode fork pointer */
4799 xfs_mount_t
*mp
; /* mount structure */
4800 xfs_filblks_t nblks
; /* quota/sb block count */
4801 xfs_bmbt_irec_t
new; /* new record to be inserted */
4803 uint qfield
; /* quota field to update */
4804 xfs_filblks_t temp
; /* for indirect length calculations */
4805 xfs_filblks_t temp2
; /* for indirect length calculations */
4809 XFS_STATS_INC(mp
, xs_del_exlist
);
4811 if (whichfork
== XFS_ATTR_FORK
)
4812 state
|= BMAP_ATTRFORK
;
4814 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4815 ASSERT((*idx
>= 0) && (*idx
< ifp
->if_bytes
/
4816 (uint
)sizeof(xfs_bmbt_rec_t
)));
4817 ASSERT(del
->br_blockcount
> 0);
4818 ep
= xfs_iext_get_ext(ifp
, *idx
);
4819 xfs_bmbt_get_all(ep
, &got
);
4820 ASSERT(got
.br_startoff
<= del
->br_startoff
);
4821 del_endoff
= del
->br_startoff
+ del
->br_blockcount
;
4822 got_endoff
= got
.br_startoff
+ got
.br_blockcount
;
4823 ASSERT(got_endoff
>= del_endoff
);
4824 delay
= isnullstartblock(got
.br_startblock
);
4825 ASSERT(isnullstartblock(del
->br_startblock
) == delay
);
4830 * If deleting a real allocation, must free up the disk space.
4833 flags
= XFS_ILOG_CORE
;
4835 * Realtime allocation. Free it and record di_nblocks update.
4837 if (whichfork
== XFS_DATA_FORK
&& XFS_IS_REALTIME_INODE(ip
)) {
4841 ASSERT(do_mod(del
->br_blockcount
,
4842 mp
->m_sb
.sb_rextsize
) == 0);
4843 ASSERT(do_mod(del
->br_startblock
,
4844 mp
->m_sb
.sb_rextsize
) == 0);
4845 bno
= del
->br_startblock
;
4846 len
= del
->br_blockcount
;
4847 do_div(bno
, mp
->m_sb
.sb_rextsize
);
4848 do_div(len
, mp
->m_sb
.sb_rextsize
);
4849 error
= xfs_rtfree_extent(tp
, bno
, (xfs_extlen_t
)len
);
4853 nblks
= len
* mp
->m_sb
.sb_rextsize
;
4854 qfield
= XFS_TRANS_DQ_RTBCOUNT
;
4857 * Ordinary allocation.
4861 nblks
= del
->br_blockcount
;
4862 qfield
= XFS_TRANS_DQ_BCOUNT
;
4865 * Set up del_endblock and cur for later.
4867 del_endblock
= del
->br_startblock
+ del
->br_blockcount
;
4869 if ((error
= xfs_bmbt_lookup_eq(cur
, got
.br_startoff
,
4870 got
.br_startblock
, got
.br_blockcount
,
4873 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
4875 da_old
= da_new
= 0;
4877 da_old
= startblockval(got
.br_startblock
);
4883 * Set flag value to use in switch statement.
4884 * Left-contig is 2, right-contig is 1.
4886 switch (((got
.br_startoff
== del
->br_startoff
) << 1) |
4887 (got_endoff
== del_endoff
)) {
4890 * Matches the whole extent. Delete the entry.
4892 xfs_iext_remove(ip
, *idx
, 1,
4893 whichfork
== XFS_ATTR_FORK
? BMAP_ATTRFORK
: 0);
4898 XFS_IFORK_NEXT_SET(ip
, whichfork
,
4899 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
4900 flags
|= XFS_ILOG_CORE
;
4902 flags
|= xfs_ilog_fext(whichfork
);
4905 if ((error
= xfs_btree_delete(cur
, &i
)))
4907 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
4912 * Deleting the first part of the extent.
4914 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
4915 xfs_bmbt_set_startoff(ep
, del_endoff
);
4916 temp
= got
.br_blockcount
- del
->br_blockcount
;
4917 xfs_bmbt_set_blockcount(ep
, temp
);
4919 temp
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
4921 xfs_bmbt_set_startblock(ep
, nullstartblock((int)temp
));
4922 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
4926 xfs_bmbt_set_startblock(ep
, del_endblock
);
4927 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
4929 flags
|= xfs_ilog_fext(whichfork
);
4932 if ((error
= xfs_bmbt_update(cur
, del_endoff
, del_endblock
,
4933 got
.br_blockcount
- del
->br_blockcount
,
4940 * Deleting the last part of the extent.
4942 temp
= got
.br_blockcount
- del
->br_blockcount
;
4943 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
4944 xfs_bmbt_set_blockcount(ep
, temp
);
4946 temp
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
4948 xfs_bmbt_set_startblock(ep
, nullstartblock((int)temp
));
4949 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
4953 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
4955 flags
|= xfs_ilog_fext(whichfork
);
4958 if ((error
= xfs_bmbt_update(cur
, got
.br_startoff
,
4960 got
.br_blockcount
- del
->br_blockcount
,
4967 * Deleting the middle of the extent.
4969 temp
= del
->br_startoff
- got
.br_startoff
;
4970 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
4971 xfs_bmbt_set_blockcount(ep
, temp
);
4972 new.br_startoff
= del_endoff
;
4973 temp2
= got_endoff
- del_endoff
;
4974 new.br_blockcount
= temp2
;
4975 new.br_state
= got
.br_state
;
4977 new.br_startblock
= del_endblock
;
4978 flags
|= XFS_ILOG_CORE
;
4980 if ((error
= xfs_bmbt_update(cur
,
4982 got
.br_startblock
, temp
,
4985 if ((error
= xfs_btree_increment(cur
, 0, &i
)))
4987 cur
->bc_rec
.b
= new;
4988 error
= xfs_btree_insert(cur
, &i
);
4989 if (error
&& error
!= -ENOSPC
)
4992 * If get no-space back from btree insert,
4993 * it tried a split, and we have a zero
4994 * block reservation.
4995 * Fix up our state and return the error.
4997 if (error
== -ENOSPC
) {
4999 * Reset the cursor, don't trust
5000 * it after any insert operation.
5002 if ((error
= xfs_bmbt_lookup_eq(cur
,
5007 XFS_WANT_CORRUPTED_GOTO(mp
,
5010 * Update the btree record back
5011 * to the original value.
5013 if ((error
= xfs_bmbt_update(cur
,
5020 * Reset the extent record back
5021 * to the original value.
5023 xfs_bmbt_set_blockcount(ep
,
5029 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
5031 flags
|= xfs_ilog_fext(whichfork
);
5032 XFS_IFORK_NEXT_SET(ip
, whichfork
,
5033 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
5035 xfs_filblks_t stolen
;
5036 ASSERT(whichfork
== XFS_DATA_FORK
);
5039 * Distribute the original indlen reservation across the
5040 * two new extents. Steal blocks from the deleted extent
5041 * if necessary. Stealing blocks simply fudges the
5042 * fdblocks accounting in xfs_bunmapi().
5044 temp
= xfs_bmap_worst_indlen(ip
, got
.br_blockcount
);
5045 temp2
= xfs_bmap_worst_indlen(ip
, new.br_blockcount
);
5046 stolen
= xfs_bmap_split_indlen(da_old
, &temp
, &temp2
,
5047 del
->br_blockcount
);
5048 da_new
= temp
+ temp2
- stolen
;
5049 del
->br_blockcount
-= stolen
;
5052 * Set the reservation for each extent. Warn if either
5053 * is zero as this can lead to delalloc problems.
5055 WARN_ON_ONCE(!temp
|| !temp2
);
5056 xfs_bmbt_set_startblock(ep
, nullstartblock((int)temp
));
5057 new.br_startblock
= nullstartblock((int)temp2
);
5059 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
5060 xfs_iext_insert(ip
, *idx
+ 1, 1, &new, state
);
5065 * If we need to, add to list of extents to delete.
5068 xfs_bmap_add_free(mp
, flist
, del
->br_startblock
,
5069 del
->br_blockcount
);
5071 * Adjust inode # blocks in the file.
5074 ip
->i_d
.di_nblocks
-= nblks
;
5076 * Adjust quota data.
5079 xfs_trans_mod_dquot_byino(tp
, ip
, qfield
, (long)-nblks
);
5082 * Account for change in delayed indirect blocks.
5083 * Nothing to do for disk quota accounting here.
5085 ASSERT(da_old
>= da_new
);
5086 if (da_old
> da_new
)
5087 xfs_mod_fdblocks(mp
, (int64_t)(da_old
- da_new
), false);
5094 * Unmap (remove) blocks from a file.
5095 * If nexts is nonzero then the number of extents to remove is limited to
5096 * that value. If not all extents in the block range can be removed then
5101 xfs_trans_t
*tp
, /* transaction pointer */
5102 struct xfs_inode
*ip
, /* incore inode */
5103 xfs_fileoff_t bno
, /* starting offset to unmap */
5104 xfs_filblks_t len
, /* length to unmap in file */
5105 int flags
, /* misc flags */
5106 xfs_extnum_t nexts
, /* number of extents max */
5107 xfs_fsblock_t
*firstblock
, /* first allocated block
5108 controls a.g. for allocs */
5109 xfs_bmap_free_t
*flist
, /* i/o: list extents to free */
5110 int *done
) /* set if not done yet */
5112 xfs_btree_cur_t
*cur
; /* bmap btree cursor */
5113 xfs_bmbt_irec_t del
; /* extent being deleted */
5114 int eof
; /* is deleting at eof */
5115 xfs_bmbt_rec_host_t
*ep
; /* extent record pointer */
5116 int error
; /* error return value */
5117 xfs_extnum_t extno
; /* extent number in list */
5118 xfs_bmbt_irec_t got
; /* current extent record */
5119 xfs_ifork_t
*ifp
; /* inode fork pointer */
5120 int isrt
; /* freeing in rt area */
5121 xfs_extnum_t lastx
; /* last extent index used */
5122 int logflags
; /* transaction logging flags */
5123 xfs_extlen_t mod
; /* rt extent offset */
5124 xfs_mount_t
*mp
; /* mount structure */
5125 xfs_extnum_t nextents
; /* number of file extents */
5126 xfs_bmbt_irec_t prev
; /* previous extent record */
5127 xfs_fileoff_t start
; /* first file offset deleted */
5128 int tmp_logflags
; /* partial logging flags */
5129 int wasdel
; /* was a delayed alloc extent */
5130 int whichfork
; /* data or attribute fork */
5133 trace_xfs_bunmap(ip
, bno
, len
, flags
, _RET_IP_
);
5135 whichfork
= (flags
& XFS_BMAPI_ATTRFORK
) ?
5136 XFS_ATTR_FORK
: XFS_DATA_FORK
;
5137 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5139 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
5140 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)) {
5141 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW
,
5143 return -EFSCORRUPTED
;
5146 if (XFS_FORCED_SHUTDOWN(mp
))
5149 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
5153 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
5154 (error
= xfs_iread_extents(tp
, ip
, whichfork
)))
5156 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
5157 if (nextents
== 0) {
5161 XFS_STATS_INC(mp
, xs_blk_unmap
);
5162 isrt
= (whichfork
== XFS_DATA_FORK
) && XFS_IS_REALTIME_INODE(ip
);
5164 bno
= start
+ len
- 1;
5165 ep
= xfs_bmap_search_extents(ip
, bno
, whichfork
, &eof
, &lastx
, &got
,
5169 * Check to see if the given block number is past the end of the
5170 * file, back up to the last block if so...
5173 ep
= xfs_iext_get_ext(ifp
, --lastx
);
5174 xfs_bmbt_get_all(ep
, &got
);
5175 bno
= got
.br_startoff
+ got
.br_blockcount
- 1;
5178 if (ifp
->if_flags
& XFS_IFBROOT
) {
5179 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
);
5180 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
5181 cur
->bc_private
.b
.firstblock
= *firstblock
;
5182 cur
->bc_private
.b
.flist
= flist
;
5183 cur
->bc_private
.b
.flags
= 0;
5189 * Synchronize by locking the bitmap inode.
5191 xfs_ilock(mp
->m_rbmip
, XFS_ILOCK_EXCL
);
5192 xfs_trans_ijoin(tp
, mp
->m_rbmip
, XFS_ILOCK_EXCL
);
5196 while (bno
!= (xfs_fileoff_t
)-1 && bno
>= start
&& lastx
>= 0 &&
5197 (nexts
== 0 || extno
< nexts
)) {
5199 * Is the found extent after a hole in which bno lives?
5200 * Just back up to the previous extent, if so.
5202 if (got
.br_startoff
> bno
) {
5205 ep
= xfs_iext_get_ext(ifp
, lastx
);
5206 xfs_bmbt_get_all(ep
, &got
);
5209 * Is the last block of this extent before the range
5210 * we're supposed to delete? If so, we're done.
5212 bno
= XFS_FILEOFF_MIN(bno
,
5213 got
.br_startoff
+ got
.br_blockcount
- 1);
5217 * Then deal with the (possibly delayed) allocated space
5222 wasdel
= isnullstartblock(del
.br_startblock
);
5223 if (got
.br_startoff
< start
) {
5224 del
.br_startoff
= start
;
5225 del
.br_blockcount
-= start
- got
.br_startoff
;
5227 del
.br_startblock
+= start
- got
.br_startoff
;
5229 if (del
.br_startoff
+ del
.br_blockcount
> bno
+ 1)
5230 del
.br_blockcount
= bno
+ 1 - del
.br_startoff
;
5231 sum
= del
.br_startblock
+ del
.br_blockcount
;
5233 (mod
= do_mod(sum
, mp
->m_sb
.sb_rextsize
))) {
5235 * Realtime extent not lined up at the end.
5236 * The extent could have been split into written
5237 * and unwritten pieces, or we could just be
5238 * unmapping part of it. But we can't really
5239 * get rid of part of a realtime extent.
5241 if (del
.br_state
== XFS_EXT_UNWRITTEN
||
5242 !xfs_sb_version_hasextflgbit(&mp
->m_sb
)) {
5244 * This piece is unwritten, or we're not
5245 * using unwritten extents. Skip over it.
5248 bno
-= mod
> del
.br_blockcount
?
5249 del
.br_blockcount
: mod
;
5250 if (bno
< got
.br_startoff
) {
5252 xfs_bmbt_get_all(xfs_iext_get_ext(
5258 * It's written, turn it unwritten.
5259 * This is better than zeroing it.
5261 ASSERT(del
.br_state
== XFS_EXT_NORM
);
5262 ASSERT(tp
->t_blk_res
> 0);
5264 * If this spans a realtime extent boundary,
5265 * chop it back to the start of the one we end at.
5267 if (del
.br_blockcount
> mod
) {
5268 del
.br_startoff
+= del
.br_blockcount
- mod
;
5269 del
.br_startblock
+= del
.br_blockcount
- mod
;
5270 del
.br_blockcount
= mod
;
5272 del
.br_state
= XFS_EXT_UNWRITTEN
;
5273 error
= xfs_bmap_add_extent_unwritten_real(tp
, ip
,
5274 &lastx
, &cur
, &del
, firstblock
, flist
,
5280 if (isrt
&& (mod
= do_mod(del
.br_startblock
, mp
->m_sb
.sb_rextsize
))) {
5282 * Realtime extent is lined up at the end but not
5283 * at the front. We'll get rid of full extents if
5286 mod
= mp
->m_sb
.sb_rextsize
- mod
;
5287 if (del
.br_blockcount
> mod
) {
5288 del
.br_blockcount
-= mod
;
5289 del
.br_startoff
+= mod
;
5290 del
.br_startblock
+= mod
;
5291 } else if ((del
.br_startoff
== start
&&
5292 (del
.br_state
== XFS_EXT_UNWRITTEN
||
5293 tp
->t_blk_res
== 0)) ||
5294 !xfs_sb_version_hasextflgbit(&mp
->m_sb
)) {
5296 * Can't make it unwritten. There isn't
5297 * a full extent here so just skip it.
5299 ASSERT(bno
>= del
.br_blockcount
);
5300 bno
-= del
.br_blockcount
;
5301 if (got
.br_startoff
> bno
) {
5303 ep
= xfs_iext_get_ext(ifp
,
5305 xfs_bmbt_get_all(ep
, &got
);
5309 } else if (del
.br_state
== XFS_EXT_UNWRITTEN
) {
5311 * This one is already unwritten.
5312 * It must have a written left neighbor.
5313 * Unwrite the killed part of that one and
5317 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
,
5319 ASSERT(prev
.br_state
== XFS_EXT_NORM
);
5320 ASSERT(!isnullstartblock(prev
.br_startblock
));
5321 ASSERT(del
.br_startblock
==
5322 prev
.br_startblock
+ prev
.br_blockcount
);
5323 if (prev
.br_startoff
< start
) {
5324 mod
= start
- prev
.br_startoff
;
5325 prev
.br_blockcount
-= mod
;
5326 prev
.br_startblock
+= mod
;
5327 prev
.br_startoff
= start
;
5329 prev
.br_state
= XFS_EXT_UNWRITTEN
;
5331 error
= xfs_bmap_add_extent_unwritten_real(tp
,
5332 ip
, &lastx
, &cur
, &prev
,
5333 firstblock
, flist
, &logflags
);
5338 ASSERT(del
.br_state
== XFS_EXT_NORM
);
5339 del
.br_state
= XFS_EXT_UNWRITTEN
;
5340 error
= xfs_bmap_add_extent_unwritten_real(tp
,
5341 ip
, &lastx
, &cur
, &del
,
5342 firstblock
, flist
, &logflags
);
5350 * If it's the case where the directory code is running
5351 * with no block reservation, and the deleted block is in
5352 * the middle of its extent, and the resulting insert
5353 * of an extent would cause transformation to btree format,
5354 * then reject it. The calling code will then swap
5355 * blocks around instead.
5356 * We have to do this now, rather than waiting for the
5357 * conversion to btree format, since the transaction
5360 if (!wasdel
&& tp
->t_blk_res
== 0 &&
5361 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
&&
5362 XFS_IFORK_NEXTENTS(ip
, whichfork
) >= /* Note the >= */
5363 XFS_IFORK_MAXEXT(ip
, whichfork
) &&
5364 del
.br_startoff
> got
.br_startoff
&&
5365 del
.br_startoff
+ del
.br_blockcount
<
5366 got
.br_startoff
+ got
.br_blockcount
) {
5372 * Unreserve quota and update realtime free space, if
5373 * appropriate. If delayed allocation, update the inode delalloc
5374 * counter now and wait to update the sb counters as
5375 * xfs_bmap_del_extent() might need to borrow some blocks.
5378 ASSERT(startblockval(del
.br_startblock
) > 0);
5380 xfs_filblks_t rtexts
;
5382 rtexts
= XFS_FSB_TO_B(mp
, del
.br_blockcount
);
5383 do_div(rtexts
, mp
->m_sb
.sb_rextsize
);
5384 xfs_mod_frextents(mp
, (int64_t)rtexts
);
5385 (void)xfs_trans_reserve_quota_nblks(NULL
,
5386 ip
, -((long)del
.br_blockcount
), 0,
5387 XFS_QMOPT_RES_RTBLKS
);
5389 (void)xfs_trans_reserve_quota_nblks(NULL
,
5390 ip
, -((long)del
.br_blockcount
), 0,
5391 XFS_QMOPT_RES_REGBLKS
);
5393 ip
->i_delayed_blks
-= del
.br_blockcount
;
5395 cur
->bc_private
.b
.flags
|=
5396 XFS_BTCUR_BPRV_WASDEL
;
5398 cur
->bc_private
.b
.flags
&= ~XFS_BTCUR_BPRV_WASDEL
;
5400 error
= xfs_bmap_del_extent(ip
, tp
, &lastx
, flist
, cur
, &del
,
5401 &tmp_logflags
, whichfork
);
5402 logflags
|= tmp_logflags
;
5406 if (!isrt
&& wasdel
)
5407 xfs_mod_fdblocks(mp
, (int64_t)del
.br_blockcount
, false);
5409 bno
= del
.br_startoff
- 1;
5412 * If not done go on to the next (previous) record.
5414 if (bno
!= (xfs_fileoff_t
)-1 && bno
>= start
) {
5416 ep
= xfs_iext_get_ext(ifp
, lastx
);
5417 if (xfs_bmbt_get_startoff(ep
) > bno
) {
5419 ep
= xfs_iext_get_ext(ifp
,
5422 xfs_bmbt_get_all(ep
, &got
);
5427 *done
= bno
== (xfs_fileoff_t
)-1 || bno
< start
|| lastx
< 0;
5430 * Convert to a btree if necessary.
5432 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
5433 ASSERT(cur
== NULL
);
5434 error
= xfs_bmap_extents_to_btree(tp
, ip
, firstblock
, flist
,
5435 &cur
, 0, &tmp_logflags
, whichfork
);
5436 logflags
|= tmp_logflags
;
5441 * transform from btree to extents, give it cur
5443 else if (xfs_bmap_wants_extents(ip
, whichfork
)) {
5444 ASSERT(cur
!= NULL
);
5445 error
= xfs_bmap_btree_to_extents(tp
, ip
, cur
, &tmp_logflags
,
5447 logflags
|= tmp_logflags
;
5452 * transform from extents to local?
5457 * Log everything. Do this after conversion, there's no point in
5458 * logging the extent records if we've converted to btree format.
5460 if ((logflags
& xfs_ilog_fext(whichfork
)) &&
5461 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
5462 logflags
&= ~xfs_ilog_fext(whichfork
);
5463 else if ((logflags
& xfs_ilog_fbroot(whichfork
)) &&
5464 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)
5465 logflags
&= ~xfs_ilog_fbroot(whichfork
);
5467 * Log inode even in the error case, if the transaction
5468 * is dirty we'll need to shut down the filesystem.
5471 xfs_trans_log_inode(tp
, ip
, logflags
);
5474 *firstblock
= cur
->bc_private
.b
.firstblock
;
5475 cur
->bc_private
.b
.allocated
= 0;
5477 xfs_btree_del_cursor(cur
,
5478 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
5484 * Determine whether an extent shift can be accomplished by a merge with the
5485 * extent that precedes the target hole of the shift.
5489 struct xfs_bmbt_irec
*left
, /* preceding extent */
5490 struct xfs_bmbt_irec
*got
, /* current extent to shift */
5491 xfs_fileoff_t shift
) /* shift fsb */
5493 xfs_fileoff_t startoff
;
5495 startoff
= got
->br_startoff
- shift
;
5498 * The extent, once shifted, must be adjacent in-file and on-disk with
5499 * the preceding extent.
5501 if ((left
->br_startoff
+ left
->br_blockcount
!= startoff
) ||
5502 (left
->br_startblock
+ left
->br_blockcount
!= got
->br_startblock
) ||
5503 (left
->br_state
!= got
->br_state
) ||
5504 (left
->br_blockcount
+ got
->br_blockcount
> MAXEXTLEN
))
5511 * A bmap extent shift adjusts the file offset of an extent to fill a preceding
5512 * hole in the file. If an extent shift would result in the extent being fully
5513 * adjacent to the extent that currently precedes the hole, we can merge with
5514 * the preceding extent rather than do the shift.
5516 * This function assumes the caller has verified a shift-by-merge is possible
5517 * with the provided extents via xfs_bmse_can_merge().
5521 struct xfs_inode
*ip
,
5523 xfs_fileoff_t shift
, /* shift fsb */
5524 int current_ext
, /* idx of gotp */
5525 struct xfs_bmbt_rec_host
*gotp
, /* extent to shift */
5526 struct xfs_bmbt_rec_host
*leftp
, /* preceding extent */
5527 struct xfs_btree_cur
*cur
,
5528 int *logflags
) /* output */
5530 struct xfs_bmbt_irec got
;
5531 struct xfs_bmbt_irec left
;
5532 xfs_filblks_t blockcount
;
5534 struct xfs_mount
*mp
= ip
->i_mount
;
5536 xfs_bmbt_get_all(gotp
, &got
);
5537 xfs_bmbt_get_all(leftp
, &left
);
5538 blockcount
= left
.br_blockcount
+ got
.br_blockcount
;
5540 ASSERT(xfs_isilocked(ip
, XFS_IOLOCK_EXCL
));
5541 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
5542 ASSERT(xfs_bmse_can_merge(&left
, &got
, shift
));
5545 * Merge the in-core extents. Note that the host record pointers and
5546 * current_ext index are invalid once the extent has been removed via
5547 * xfs_iext_remove().
5549 xfs_bmbt_set_blockcount(leftp
, blockcount
);
5550 xfs_iext_remove(ip
, current_ext
, 1, 0);
5553 * Update the on-disk extent count, the btree if necessary and log the
5556 XFS_IFORK_NEXT_SET(ip
, whichfork
,
5557 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
5558 *logflags
|= XFS_ILOG_CORE
;
5560 *logflags
|= XFS_ILOG_DEXT
;
5564 /* lookup and remove the extent to merge */
5565 error
= xfs_bmbt_lookup_eq(cur
, got
.br_startoff
, got
.br_startblock
,
5566 got
.br_blockcount
, &i
);
5569 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
5571 error
= xfs_btree_delete(cur
, &i
);
5574 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
5576 /* lookup and update size of the previous extent */
5577 error
= xfs_bmbt_lookup_eq(cur
, left
.br_startoff
, left
.br_startblock
,
5578 left
.br_blockcount
, &i
);
5581 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
5583 left
.br_blockcount
= blockcount
;
5585 return xfs_bmbt_update(cur
, left
.br_startoff
, left
.br_startblock
,
5586 left
.br_blockcount
, left
.br_state
);
5590 * Shift a single extent.
5594 struct xfs_inode
*ip
,
5596 xfs_fileoff_t offset_shift_fsb
,
5598 struct xfs_bmbt_rec_host
*gotp
,
5599 struct xfs_btree_cur
*cur
,
5601 enum shift_direction direction
)
5603 struct xfs_ifork
*ifp
;
5604 struct xfs_mount
*mp
;
5605 xfs_fileoff_t startoff
;
5606 struct xfs_bmbt_rec_host
*adj_irecp
;
5607 struct xfs_bmbt_irec got
;
5608 struct xfs_bmbt_irec adj_irec
;
5614 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5615 total_extents
= ifp
->if_bytes
/ sizeof(xfs_bmbt_rec_t
);
5617 xfs_bmbt_get_all(gotp
, &got
);
5619 /* delalloc extents should be prevented by caller */
5620 XFS_WANT_CORRUPTED_RETURN(mp
, !isnullstartblock(got
.br_startblock
));
5622 if (direction
== SHIFT_LEFT
) {
5623 startoff
= got
.br_startoff
- offset_shift_fsb
;
5626 * Check for merge if we've got an extent to the left,
5627 * otherwise make sure there's enough room at the start
5628 * of the file for the shift.
5630 if (!*current_ext
) {
5631 if (got
.br_startoff
< offset_shift_fsb
)
5633 goto update_current_ext
;
5636 * grab the left extent and check for a large
5639 adj_irecp
= xfs_iext_get_ext(ifp
, *current_ext
- 1);
5640 xfs_bmbt_get_all(adj_irecp
, &adj_irec
);
5643 adj_irec
.br_startoff
+ adj_irec
.br_blockcount
)
5646 /* check whether to merge the extent or shift it down */
5647 if (xfs_bmse_can_merge(&adj_irec
, &got
,
5648 offset_shift_fsb
)) {
5649 return xfs_bmse_merge(ip
, whichfork
, offset_shift_fsb
,
5650 *current_ext
, gotp
, adj_irecp
,
5654 startoff
= got
.br_startoff
+ offset_shift_fsb
;
5655 /* nothing to move if this is the last extent */
5656 if (*current_ext
>= (total_extents
- 1))
5657 goto update_current_ext
;
5659 * If this is not the last extent in the file, make sure there
5660 * is enough room between current extent and next extent for
5661 * accommodating the shift.
5663 adj_irecp
= xfs_iext_get_ext(ifp
, *current_ext
+ 1);
5664 xfs_bmbt_get_all(adj_irecp
, &adj_irec
);
5665 if (startoff
+ got
.br_blockcount
> adj_irec
.br_startoff
)
5668 * Unlike a left shift (which involves a hole punch),
5669 * a right shift does not modify extent neighbors
5670 * in any way. We should never find mergeable extents
5671 * in this scenario. Check anyways and warn if we
5672 * encounter two extents that could be one.
5674 if (xfs_bmse_can_merge(&got
, &adj_irec
, offset_shift_fsb
))
5678 * Increment the extent index for the next iteration, update the start
5679 * offset of the in-core extent and update the btree if applicable.
5682 if (direction
== SHIFT_LEFT
)
5686 xfs_bmbt_set_startoff(gotp
, startoff
);
5687 *logflags
|= XFS_ILOG_CORE
;
5689 *logflags
|= XFS_ILOG_DEXT
;
5693 error
= xfs_bmbt_lookup_eq(cur
, got
.br_startoff
, got
.br_startblock
,
5694 got
.br_blockcount
, &i
);
5697 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
5699 got
.br_startoff
= startoff
;
5700 return xfs_bmbt_update(cur
, got
.br_startoff
, got
.br_startblock
,
5701 got
.br_blockcount
, got
.br_state
);
5705 * Shift extent records to the left/right to cover/create a hole.
5707 * The maximum number of extents to be shifted in a single operation is
5708 * @num_exts. @stop_fsb specifies the file offset at which to stop shift and the
5709 * file offset where we've left off is returned in @next_fsb. @offset_shift_fsb
5710 * is the length by which each extent is shifted. If there is no hole to shift
5711 * the extents into, this will be considered invalid operation and we abort
5715 xfs_bmap_shift_extents(
5716 struct xfs_trans
*tp
,
5717 struct xfs_inode
*ip
,
5718 xfs_fileoff_t
*next_fsb
,
5719 xfs_fileoff_t offset_shift_fsb
,
5721 xfs_fileoff_t stop_fsb
,
5722 xfs_fsblock_t
*firstblock
,
5723 struct xfs_bmap_free
*flist
,
5724 enum shift_direction direction
,
5727 struct xfs_btree_cur
*cur
= NULL
;
5728 struct xfs_bmbt_rec_host
*gotp
;
5729 struct xfs_bmbt_irec got
;
5730 struct xfs_mount
*mp
= ip
->i_mount
;
5731 struct xfs_ifork
*ifp
;
5732 xfs_extnum_t nexts
= 0;
5733 xfs_extnum_t current_ext
;
5734 xfs_extnum_t total_extents
;
5735 xfs_extnum_t stop_extent
;
5737 int whichfork
= XFS_DATA_FORK
;
5740 if (unlikely(XFS_TEST_ERROR(
5741 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
5742 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
5743 mp
, XFS_ERRTAG_BMAPIFORMAT
, XFS_RANDOM_BMAPIFORMAT
))) {
5744 XFS_ERROR_REPORT("xfs_bmap_shift_extents",
5745 XFS_ERRLEVEL_LOW
, mp
);
5746 return -EFSCORRUPTED
;
5749 if (XFS_FORCED_SHUTDOWN(mp
))
5752 ASSERT(xfs_isilocked(ip
, XFS_IOLOCK_EXCL
));
5753 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
5754 ASSERT(direction
== SHIFT_LEFT
|| direction
== SHIFT_RIGHT
);
5755 ASSERT(*next_fsb
!= NULLFSBLOCK
|| direction
== SHIFT_RIGHT
);
5757 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5758 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
5759 /* Read in all the extents */
5760 error
= xfs_iread_extents(tp
, ip
, whichfork
);
5765 if (ifp
->if_flags
& XFS_IFBROOT
) {
5766 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
5767 cur
->bc_private
.b
.firstblock
= *firstblock
;
5768 cur
->bc_private
.b
.flist
= flist
;
5769 cur
->bc_private
.b
.flags
= 0;
5773 * There may be delalloc extents in the data fork before the range we
5774 * are collapsing out, so we cannot use the count of real extents here.
5775 * Instead we have to calculate it from the incore fork.
5777 total_extents
= ifp
->if_bytes
/ sizeof(xfs_bmbt_rec_t
);
5778 if (total_extents
== 0) {
5784 * In case of first right shift, we need to initialize next_fsb
5786 if (*next_fsb
== NULLFSBLOCK
) {
5787 gotp
= xfs_iext_get_ext(ifp
, total_extents
- 1);
5788 xfs_bmbt_get_all(gotp
, &got
);
5789 *next_fsb
= got
.br_startoff
;
5790 if (stop_fsb
> *next_fsb
) {
5796 /* Lookup the extent index at which we have to stop */
5797 if (direction
== SHIFT_RIGHT
) {
5798 gotp
= xfs_iext_bno_to_ext(ifp
, stop_fsb
, &stop_extent
);
5799 /* Make stop_extent exclusive of shift range */
5802 stop_extent
= total_extents
;
5805 * Look up the extent index for the fsb where we start shifting. We can
5806 * henceforth iterate with current_ext as extent list changes are locked
5809 * gotp can be null in 2 cases: 1) if there are no extents or 2)
5810 * *next_fsb lies in a hole beyond which there are no extents. Either
5813 gotp
= xfs_iext_bno_to_ext(ifp
, *next_fsb
, ¤t_ext
);
5819 /* some sanity checking before we finally start shifting extents */
5820 if ((direction
== SHIFT_LEFT
&& current_ext
>= stop_extent
) ||
5821 (direction
== SHIFT_RIGHT
&& current_ext
<= stop_extent
)) {
5826 while (nexts
++ < num_exts
) {
5827 error
= xfs_bmse_shift_one(ip
, whichfork
, offset_shift_fsb
,
5828 ¤t_ext
, gotp
, cur
, &logflags
,
5833 * If there was an extent merge during the shift, the extent
5834 * count can change. Update the total and grade the next record.
5836 if (direction
== SHIFT_LEFT
) {
5837 total_extents
= ifp
->if_bytes
/ sizeof(xfs_bmbt_rec_t
);
5838 stop_extent
= total_extents
;
5841 if (current_ext
== stop_extent
) {
5843 *next_fsb
= NULLFSBLOCK
;
5846 gotp
= xfs_iext_get_ext(ifp
, current_ext
);
5850 xfs_bmbt_get_all(gotp
, &got
);
5851 *next_fsb
= got
.br_startoff
;
5856 xfs_btree_del_cursor(cur
,
5857 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
5860 xfs_trans_log_inode(tp
, ip
, logflags
);
5866 * Splits an extent into two extents at split_fsb block such that it is
5867 * the first block of the current_ext. @current_ext is a target extent
5868 * to be split. @split_fsb is a block where the extents is split.
5869 * If split_fsb lies in a hole or the first block of extents, just return 0.
5872 xfs_bmap_split_extent_at(
5873 struct xfs_trans
*tp
,
5874 struct xfs_inode
*ip
,
5875 xfs_fileoff_t split_fsb
,
5876 xfs_fsblock_t
*firstfsb
,
5877 struct xfs_bmap_free
*free_list
)
5879 int whichfork
= XFS_DATA_FORK
;
5880 struct xfs_btree_cur
*cur
= NULL
;
5881 struct xfs_bmbt_rec_host
*gotp
;
5882 struct xfs_bmbt_irec got
;
5883 struct xfs_bmbt_irec
new; /* split extent */
5884 struct xfs_mount
*mp
= ip
->i_mount
;
5885 struct xfs_ifork
*ifp
;
5886 xfs_fsblock_t gotblkcnt
; /* new block count for got */
5887 xfs_extnum_t current_ext
;
5892 if (unlikely(XFS_TEST_ERROR(
5893 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
5894 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
5895 mp
, XFS_ERRTAG_BMAPIFORMAT
, XFS_RANDOM_BMAPIFORMAT
))) {
5896 XFS_ERROR_REPORT("xfs_bmap_split_extent_at",
5897 XFS_ERRLEVEL_LOW
, mp
);
5898 return -EFSCORRUPTED
;
5901 if (XFS_FORCED_SHUTDOWN(mp
))
5904 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5905 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
5906 /* Read in all the extents */
5907 error
= xfs_iread_extents(tp
, ip
, whichfork
);
5913 * gotp can be null in 2 cases: 1) if there are no extents
5914 * or 2) split_fsb lies in a hole beyond which there are
5915 * no extents. Either way, we are done.
5917 gotp
= xfs_iext_bno_to_ext(ifp
, split_fsb
, ¤t_ext
);
5921 xfs_bmbt_get_all(gotp
, &got
);
5924 * Check split_fsb lies in a hole or the start boundary offset
5927 if (got
.br_startoff
>= split_fsb
)
5930 gotblkcnt
= split_fsb
- got
.br_startoff
;
5931 new.br_startoff
= split_fsb
;
5932 new.br_startblock
= got
.br_startblock
+ gotblkcnt
;
5933 new.br_blockcount
= got
.br_blockcount
- gotblkcnt
;
5934 new.br_state
= got
.br_state
;
5936 if (ifp
->if_flags
& XFS_IFBROOT
) {
5937 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
5938 cur
->bc_private
.b
.firstblock
= *firstfsb
;
5939 cur
->bc_private
.b
.flist
= free_list
;
5940 cur
->bc_private
.b
.flags
= 0;
5941 error
= xfs_bmbt_lookup_eq(cur
, got
.br_startoff
,
5947 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, del_cursor
);
5950 xfs_bmbt_set_blockcount(gotp
, gotblkcnt
);
5951 got
.br_blockcount
= gotblkcnt
;
5953 logflags
= XFS_ILOG_CORE
;
5955 error
= xfs_bmbt_update(cur
, got
.br_startoff
,
5962 logflags
|= XFS_ILOG_DEXT
;
5964 /* Add new extent */
5966 xfs_iext_insert(ip
, current_ext
, 1, &new, 0);
5967 XFS_IFORK_NEXT_SET(ip
, whichfork
,
5968 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
5971 error
= xfs_bmbt_lookup_eq(cur
, new.br_startoff
,
5972 new.br_startblock
, new.br_blockcount
,
5976 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, del_cursor
);
5977 cur
->bc_rec
.b
.br_state
= new.br_state
;
5979 error
= xfs_btree_insert(cur
, &i
);
5982 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, del_cursor
);
5986 * Convert to a btree if necessary.
5988 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
5989 int tmp_logflags
; /* partial log flag return val */
5991 ASSERT(cur
== NULL
);
5992 error
= xfs_bmap_extents_to_btree(tp
, ip
, firstfsb
, free_list
,
5993 &cur
, 0, &tmp_logflags
, whichfork
);
5994 logflags
|= tmp_logflags
;
5999 cur
->bc_private
.b
.allocated
= 0;
6000 xfs_btree_del_cursor(cur
,
6001 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
6005 xfs_trans_log_inode(tp
, ip
, logflags
);
6010 xfs_bmap_split_extent(
6011 struct xfs_inode
*ip
,
6012 xfs_fileoff_t split_fsb
)
6014 struct xfs_mount
*mp
= ip
->i_mount
;
6015 struct xfs_trans
*tp
;
6016 struct xfs_bmap_free free_list
;
6017 xfs_fsblock_t firstfsb
;
6020 error
= xfs_trans_alloc(mp
, &M_RES(mp
)->tr_write
,
6021 XFS_DIOSTRAT_SPACE_RES(mp
, 0), 0, 0, &tp
);
6025 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
6026 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
);
6028 xfs_bmap_init(&free_list
, &firstfsb
);
6030 error
= xfs_bmap_split_extent_at(tp
, ip
, split_fsb
,
6031 &firstfsb
, &free_list
);
6035 error
= xfs_bmap_finish(&tp
, &free_list
, NULL
);
6039 return xfs_trans_commit(tp
);
6042 xfs_bmap_cancel(&free_list
);
6043 xfs_trans_cancel(tp
);