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
324 xfs_bmap_check_leaf_extents(
325 xfs_btree_cur_t
*cur
, /* btree cursor or null */
326 xfs_inode_t
*ip
, /* incore inode pointer */
327 int whichfork
) /* data or attr fork */
329 struct xfs_btree_block
*block
; /* current btree block */
330 xfs_fsblock_t bno
; /* block # of "block" */
331 xfs_buf_t
*bp
; /* buffer for "block" */
332 int error
; /* error return value */
333 xfs_extnum_t i
=0, j
; /* index into the extents list */
334 xfs_ifork_t
*ifp
; /* fork structure */
335 int level
; /* btree level, for checking */
336 xfs_mount_t
*mp
; /* file system mount structure */
337 __be64
*pp
; /* pointer to block address */
338 xfs_bmbt_rec_t
*ep
; /* pointer to current extent */
339 xfs_bmbt_rec_t last
= {0, 0}; /* last extent in prev block */
340 xfs_bmbt_rec_t
*nextp
; /* pointer to next extent */
343 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
) {
349 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
350 block
= ifp
->if_broot
;
352 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
354 level
= be16_to_cpu(block
->bb_level
);
356 xfs_check_block(block
, mp
, 1, ifp
->if_broot_bytes
);
357 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, 1, ifp
->if_broot_bytes
);
358 bno
= be64_to_cpu(*pp
);
360 ASSERT(bno
!= NULLFSBLOCK
);
361 ASSERT(XFS_FSB_TO_AGNO(mp
, bno
) < mp
->m_sb
.sb_agcount
);
362 ASSERT(XFS_FSB_TO_AGBNO(mp
, bno
) < mp
->m_sb
.sb_agblocks
);
365 * Go down the tree until leaf level is reached, following the first
366 * pointer (leftmost) at each level.
368 while (level
-- > 0) {
369 /* See if buf is in cur first */
371 bp
= xfs_bmap_get_bp(cur
, XFS_FSB_TO_DADDR(mp
, bno
));
374 error
= xfs_btree_read_bufl(mp
, NULL
, bno
, 0, &bp
,
380 block
= XFS_BUF_TO_BLOCK(bp
);
385 * Check this block for basic sanity (increasing keys and
386 * no duplicate blocks).
389 xfs_check_block(block
, mp
, 0, 0);
390 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, mp
->m_bmap_dmxr
[1]);
391 bno
= be64_to_cpu(*pp
);
392 XFS_WANT_CORRUPTED_GOTO(mp
,
393 XFS_FSB_SANITY_CHECK(mp
, bno
), error0
);
396 xfs_trans_brelse(NULL
, bp
);
401 * Here with bp and block set to the leftmost leaf node in the tree.
406 * Loop over all leaf nodes checking that all extents are in the right order.
409 xfs_fsblock_t nextbno
;
410 xfs_extnum_t num_recs
;
413 num_recs
= xfs_btree_get_numrecs(block
);
416 * Read-ahead the next leaf block, if any.
419 nextbno
= be64_to_cpu(block
->bb_u
.l
.bb_rightsib
);
422 * Check all the extents to make sure they are OK.
423 * If we had a previous block, the last entry should
424 * conform with the first entry in this one.
427 ep
= XFS_BMBT_REC_ADDR(mp
, block
, 1);
429 ASSERT(xfs_bmbt_disk_get_startoff(&last
) +
430 xfs_bmbt_disk_get_blockcount(&last
) <=
431 xfs_bmbt_disk_get_startoff(ep
));
433 for (j
= 1; j
< num_recs
; j
++) {
434 nextp
= XFS_BMBT_REC_ADDR(mp
, block
, j
+ 1);
435 ASSERT(xfs_bmbt_disk_get_startoff(ep
) +
436 xfs_bmbt_disk_get_blockcount(ep
) <=
437 xfs_bmbt_disk_get_startoff(nextp
));
445 xfs_trans_brelse(NULL
, bp
);
449 * If we've reached the end, stop.
451 if (bno
== NULLFSBLOCK
)
455 bp
= xfs_bmap_get_bp(cur
, XFS_FSB_TO_DADDR(mp
, bno
));
458 error
= xfs_btree_read_bufl(mp
, NULL
, bno
, 0, &bp
,
464 block
= XFS_BUF_TO_BLOCK(bp
);
468 xfs_trans_brelse(NULL
, bp
);
473 xfs_warn(mp
, "%s: at error0", __func__
);
475 xfs_trans_brelse(NULL
, bp
);
477 xfs_warn(mp
, "%s: BAD after btree leaves for %d extents",
479 panic("%s: CORRUPTED BTREE OR SOMETHING", __func__
);
484 * Add bmap trace insert entries for all the contents of the extent records.
487 xfs_bmap_trace_exlist(
488 xfs_inode_t
*ip
, /* incore inode pointer */
489 xfs_extnum_t cnt
, /* count of entries in the list */
490 int whichfork
, /* data or attr fork */
491 unsigned long caller_ip
)
493 xfs_extnum_t idx
; /* extent record index */
494 xfs_ifork_t
*ifp
; /* inode fork pointer */
497 if (whichfork
== XFS_ATTR_FORK
)
498 state
|= BMAP_ATTRFORK
;
500 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
501 ASSERT(cnt
== (ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
)));
502 for (idx
= 0; idx
< cnt
; idx
++)
503 trace_xfs_extlist(ip
, idx
, whichfork
, caller_ip
);
507 * Validate that the bmbt_irecs being returned from bmapi are valid
508 * given the caller's original parameters. Specifically check the
509 * ranges of the returned irecs to ensure that they only extend beyond
510 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
513 xfs_bmap_validate_ret(
517 xfs_bmbt_irec_t
*mval
,
521 int i
; /* index to map values */
523 ASSERT(ret_nmap
<= nmap
);
525 for (i
= 0; i
< ret_nmap
; i
++) {
526 ASSERT(mval
[i
].br_blockcount
> 0);
527 if (!(flags
& XFS_BMAPI_ENTIRE
)) {
528 ASSERT(mval
[i
].br_startoff
>= bno
);
529 ASSERT(mval
[i
].br_blockcount
<= len
);
530 ASSERT(mval
[i
].br_startoff
+ mval
[i
].br_blockcount
<=
533 ASSERT(mval
[i
].br_startoff
< bno
+ len
);
534 ASSERT(mval
[i
].br_startoff
+ mval
[i
].br_blockcount
>
538 mval
[i
- 1].br_startoff
+ mval
[i
- 1].br_blockcount
==
539 mval
[i
].br_startoff
);
540 ASSERT(mval
[i
].br_startblock
!= DELAYSTARTBLOCK
&&
541 mval
[i
].br_startblock
!= HOLESTARTBLOCK
);
542 ASSERT(mval
[i
].br_state
== XFS_EXT_NORM
||
543 mval
[i
].br_state
== XFS_EXT_UNWRITTEN
);
548 #define xfs_bmap_check_leaf_extents(cur, ip, whichfork) do { } while (0)
549 #define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap)
553 * bmap free list manipulation functions
557 * Add the extent to the list of extents to be free at transaction end.
558 * The list is maintained sorted (by block number).
562 xfs_fsblock_t bno
, /* fs block number of extent */
563 xfs_filblks_t len
, /* length of extent */
564 xfs_bmap_free_t
*flist
, /* list of extents */
565 xfs_mount_t
*mp
) /* mount point structure */
567 xfs_bmap_free_item_t
*cur
; /* current (next) element */
568 xfs_bmap_free_item_t
*new; /* new element */
569 xfs_bmap_free_item_t
*prev
; /* previous element */
574 ASSERT(bno
!= NULLFSBLOCK
);
576 ASSERT(len
<= MAXEXTLEN
);
577 ASSERT(!isnullstartblock(bno
));
578 agno
= XFS_FSB_TO_AGNO(mp
, bno
);
579 agbno
= XFS_FSB_TO_AGBNO(mp
, bno
);
580 ASSERT(agno
< mp
->m_sb
.sb_agcount
);
581 ASSERT(agbno
< mp
->m_sb
.sb_agblocks
);
582 ASSERT(len
< mp
->m_sb
.sb_agblocks
);
583 ASSERT(agbno
+ len
<= mp
->m_sb
.sb_agblocks
);
585 ASSERT(xfs_bmap_free_item_zone
!= NULL
);
586 new = kmem_zone_alloc(xfs_bmap_free_item_zone
, KM_SLEEP
);
587 new->xbfi_startblock
= bno
;
588 new->xbfi_blockcount
= (xfs_extlen_t
)len
;
589 for (prev
= NULL
, cur
= flist
->xbf_first
;
591 prev
= cur
, cur
= cur
->xbfi_next
) {
592 if (cur
->xbfi_startblock
>= bno
)
596 prev
->xbfi_next
= new;
598 flist
->xbf_first
= new;
599 new->xbfi_next
= cur
;
604 * Remove the entry "free" from the free item list. Prev points to the
605 * previous entry, unless "free" is the head of the list.
609 xfs_bmap_free_t
*flist
, /* free item list header */
610 xfs_bmap_free_item_t
*prev
, /* previous item on list, if any */
611 xfs_bmap_free_item_t
*free
) /* list item to be freed */
614 prev
->xbfi_next
= free
->xbfi_next
;
616 flist
->xbf_first
= free
->xbfi_next
;
618 kmem_zone_free(xfs_bmap_free_item_zone
, free
);
622 * Free up any items left in the list.
626 xfs_bmap_free_t
*flist
) /* list of bmap_free_items */
628 xfs_bmap_free_item_t
*free
; /* free list item */
629 xfs_bmap_free_item_t
*next
;
631 if (flist
->xbf_count
== 0)
633 ASSERT(flist
->xbf_first
!= NULL
);
634 for (free
= flist
->xbf_first
; free
; free
= next
) {
635 next
= free
->xbfi_next
;
636 xfs_bmap_del_free(flist
, NULL
, free
);
638 ASSERT(flist
->xbf_count
== 0);
642 * Inode fork format manipulation functions
646 * Transform a btree format file with only one leaf node, where the
647 * extents list will fit in the inode, into an extents format file.
648 * Since the file extents are already in-core, all we have to do is
649 * give up the space for the btree root and pitch the leaf block.
651 STATIC
int /* error */
652 xfs_bmap_btree_to_extents(
653 xfs_trans_t
*tp
, /* transaction pointer */
654 xfs_inode_t
*ip
, /* incore inode pointer */
655 xfs_btree_cur_t
*cur
, /* btree cursor */
656 int *logflagsp
, /* inode logging flags */
657 int whichfork
) /* data or attr fork */
660 struct xfs_btree_block
*cblock
;/* child btree block */
661 xfs_fsblock_t cbno
; /* child block number */
662 xfs_buf_t
*cbp
; /* child block's buffer */
663 int error
; /* error return value */
664 xfs_ifork_t
*ifp
; /* inode fork data */
665 xfs_mount_t
*mp
; /* mount point structure */
666 __be64
*pp
; /* ptr to block address */
667 struct xfs_btree_block
*rblock
;/* root btree block */
670 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
671 ASSERT(ifp
->if_flags
& XFS_IFEXTENTS
);
672 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
);
673 rblock
= ifp
->if_broot
;
674 ASSERT(be16_to_cpu(rblock
->bb_level
) == 1);
675 ASSERT(be16_to_cpu(rblock
->bb_numrecs
) == 1);
676 ASSERT(xfs_bmbt_maxrecs(mp
, ifp
->if_broot_bytes
, 0) == 1);
677 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, rblock
, 1, ifp
->if_broot_bytes
);
678 cbno
= be64_to_cpu(*pp
);
681 if ((error
= xfs_btree_check_lptr(cur
, cbno
, 1)))
684 error
= xfs_btree_read_bufl(mp
, tp
, cbno
, 0, &cbp
, XFS_BMAP_BTREE_REF
,
688 cblock
= XFS_BUF_TO_BLOCK(cbp
);
689 if ((error
= xfs_btree_check_block(cur
, cblock
, 0, cbp
)))
691 xfs_bmap_add_free(cbno
, 1, cur
->bc_private
.b
.flist
, mp
);
692 ip
->i_d
.di_nblocks
--;
693 xfs_trans_mod_dquot_byino(tp
, ip
, XFS_TRANS_DQ_BCOUNT
, -1L);
694 xfs_trans_binval(tp
, cbp
);
695 if (cur
->bc_bufs
[0] == cbp
)
696 cur
->bc_bufs
[0] = NULL
;
697 xfs_iroot_realloc(ip
, -1, whichfork
);
698 ASSERT(ifp
->if_broot
== NULL
);
699 ASSERT((ifp
->if_flags
& XFS_IFBROOT
) == 0);
700 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_EXTENTS
);
701 *logflagsp
= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
706 * Convert an extents-format file into a btree-format file.
707 * The new file will have a root block (in the inode) and a single child block.
709 STATIC
int /* error */
710 xfs_bmap_extents_to_btree(
711 xfs_trans_t
*tp
, /* transaction pointer */
712 xfs_inode_t
*ip
, /* incore inode pointer */
713 xfs_fsblock_t
*firstblock
, /* first-block-allocated */
714 xfs_bmap_free_t
*flist
, /* blocks freed in xaction */
715 xfs_btree_cur_t
**curp
, /* cursor returned to caller */
716 int wasdel
, /* converting a delayed alloc */
717 int *logflagsp
, /* inode logging flags */
718 int whichfork
) /* data or attr fork */
720 struct xfs_btree_block
*ablock
; /* allocated (child) bt block */
721 xfs_buf_t
*abp
; /* buffer for ablock */
722 xfs_alloc_arg_t args
; /* allocation arguments */
723 xfs_bmbt_rec_t
*arp
; /* child record pointer */
724 struct xfs_btree_block
*block
; /* btree root block */
725 xfs_btree_cur_t
*cur
; /* bmap btree cursor */
726 xfs_bmbt_rec_host_t
*ep
; /* extent record pointer */
727 int error
; /* error return value */
728 xfs_extnum_t i
, cnt
; /* extent record index */
729 xfs_ifork_t
*ifp
; /* inode fork pointer */
730 xfs_bmbt_key_t
*kp
; /* root block key pointer */
731 xfs_mount_t
*mp
; /* mount structure */
732 xfs_extnum_t nextents
; /* number of file extents */
733 xfs_bmbt_ptr_t
*pp
; /* root block address pointer */
736 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
737 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
);
740 * Make space in the inode incore.
742 xfs_iroot_realloc(ip
, 1, whichfork
);
743 ifp
->if_flags
|= XFS_IFBROOT
;
748 block
= ifp
->if_broot
;
749 if (xfs_sb_version_hascrc(&mp
->m_sb
))
750 xfs_btree_init_block_int(mp
, block
, XFS_BUF_DADDR_NULL
,
751 XFS_BMAP_CRC_MAGIC
, 1, 1, ip
->i_ino
,
752 XFS_BTREE_LONG_PTRS
| XFS_BTREE_CRC_BLOCKS
);
754 xfs_btree_init_block_int(mp
, block
, XFS_BUF_DADDR_NULL
,
755 XFS_BMAP_MAGIC
, 1, 1, ip
->i_ino
,
756 XFS_BTREE_LONG_PTRS
);
759 * Need a cursor. Can't allocate until bb_level is filled in.
761 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
762 cur
->bc_private
.b
.firstblock
= *firstblock
;
763 cur
->bc_private
.b
.flist
= flist
;
764 cur
->bc_private
.b
.flags
= wasdel
? XFS_BTCUR_BPRV_WASDEL
: 0;
766 * Convert to a btree with two levels, one record in root.
768 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_BTREE
);
769 memset(&args
, 0, sizeof(args
));
772 args
.firstblock
= *firstblock
;
773 if (*firstblock
== NULLFSBLOCK
) {
774 args
.type
= XFS_ALLOCTYPE_START_BNO
;
775 args
.fsbno
= XFS_INO_TO_FSB(mp
, ip
->i_ino
);
776 } else if (flist
->xbf_low
) {
777 args
.type
= XFS_ALLOCTYPE_START_BNO
;
778 args
.fsbno
= *firstblock
;
780 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
781 args
.fsbno
= *firstblock
;
783 args
.minlen
= args
.maxlen
= args
.prod
= 1;
784 args
.wasdel
= wasdel
;
786 if ((error
= xfs_alloc_vextent(&args
))) {
787 xfs_iroot_realloc(ip
, -1, whichfork
);
788 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
792 * Allocation can't fail, the space was reserved.
794 ASSERT(args
.fsbno
!= NULLFSBLOCK
);
795 ASSERT(*firstblock
== NULLFSBLOCK
||
796 args
.agno
== XFS_FSB_TO_AGNO(mp
, *firstblock
) ||
798 args
.agno
> XFS_FSB_TO_AGNO(mp
, *firstblock
)));
799 *firstblock
= cur
->bc_private
.b
.firstblock
= args
.fsbno
;
800 cur
->bc_private
.b
.allocated
++;
801 ip
->i_d
.di_nblocks
++;
802 xfs_trans_mod_dquot_byino(tp
, ip
, XFS_TRANS_DQ_BCOUNT
, 1L);
803 abp
= xfs_btree_get_bufl(mp
, tp
, args
.fsbno
, 0);
805 * Fill in the child block.
807 abp
->b_ops
= &xfs_bmbt_buf_ops
;
808 ablock
= XFS_BUF_TO_BLOCK(abp
);
809 if (xfs_sb_version_hascrc(&mp
->m_sb
))
810 xfs_btree_init_block_int(mp
, ablock
, abp
->b_bn
,
811 XFS_BMAP_CRC_MAGIC
, 0, 0, ip
->i_ino
,
812 XFS_BTREE_LONG_PTRS
| XFS_BTREE_CRC_BLOCKS
);
814 xfs_btree_init_block_int(mp
, ablock
, abp
->b_bn
,
815 XFS_BMAP_MAGIC
, 0, 0, ip
->i_ino
,
816 XFS_BTREE_LONG_PTRS
);
818 arp
= XFS_BMBT_REC_ADDR(mp
, ablock
, 1);
819 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
820 for (cnt
= i
= 0; i
< nextents
; i
++) {
821 ep
= xfs_iext_get_ext(ifp
, i
);
822 if (!isnullstartblock(xfs_bmbt_get_startblock(ep
))) {
823 arp
->l0
= cpu_to_be64(ep
->l0
);
824 arp
->l1
= cpu_to_be64(ep
->l1
);
828 ASSERT(cnt
== XFS_IFORK_NEXTENTS(ip
, whichfork
));
829 xfs_btree_set_numrecs(ablock
, cnt
);
832 * Fill in the root key and pointer.
834 kp
= XFS_BMBT_KEY_ADDR(mp
, block
, 1);
835 arp
= XFS_BMBT_REC_ADDR(mp
, ablock
, 1);
836 kp
->br_startoff
= cpu_to_be64(xfs_bmbt_disk_get_startoff(arp
));
837 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, xfs_bmbt_get_maxrecs(cur
,
838 be16_to_cpu(block
->bb_level
)));
839 *pp
= cpu_to_be64(args
.fsbno
);
842 * Do all this logging at the end so that
843 * the root is at the right level.
845 xfs_btree_log_block(cur
, abp
, XFS_BB_ALL_BITS
);
846 xfs_btree_log_recs(cur
, abp
, 1, be16_to_cpu(ablock
->bb_numrecs
));
847 ASSERT(*curp
== NULL
);
849 *logflagsp
= XFS_ILOG_CORE
| xfs_ilog_fbroot(whichfork
);
854 * Convert a local file to an extents file.
855 * This code is out of bounds for data forks of regular files,
856 * since the file data needs to get logged so things will stay consistent.
857 * (The bmap-level manipulations are ok, though).
860 xfs_bmap_local_to_extents_empty(
861 struct xfs_inode
*ip
,
864 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
866 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
867 ASSERT(ifp
->if_bytes
== 0);
868 ASSERT(XFS_IFORK_NEXTENTS(ip
, whichfork
) == 0);
870 xfs_bmap_forkoff_reset(ip
, whichfork
);
871 ifp
->if_flags
&= ~XFS_IFINLINE
;
872 ifp
->if_flags
|= XFS_IFEXTENTS
;
873 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_EXTENTS
);
877 STATIC
int /* error */
878 xfs_bmap_local_to_extents(
879 xfs_trans_t
*tp
, /* transaction pointer */
880 xfs_inode_t
*ip
, /* incore inode pointer */
881 xfs_fsblock_t
*firstblock
, /* first block allocated in xaction */
882 xfs_extlen_t total
, /* total blocks needed by transaction */
883 int *logflagsp
, /* inode logging flags */
885 void (*init_fn
)(struct xfs_trans
*tp
,
887 struct xfs_inode
*ip
,
888 struct xfs_ifork
*ifp
))
891 int flags
; /* logging flags returned */
892 xfs_ifork_t
*ifp
; /* inode fork pointer */
893 xfs_alloc_arg_t args
; /* allocation arguments */
894 xfs_buf_t
*bp
; /* buffer for extent block */
895 xfs_bmbt_rec_host_t
*ep
; /* extent record pointer */
898 * We don't want to deal with the case of keeping inode data inline yet.
899 * So sending the data fork of a regular inode is invalid.
901 ASSERT(!(S_ISREG(ip
->i_d
.di_mode
) && whichfork
== XFS_DATA_FORK
));
902 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
903 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
905 if (!ifp
->if_bytes
) {
906 xfs_bmap_local_to_extents_empty(ip
, whichfork
);
907 flags
= XFS_ILOG_CORE
;
913 ASSERT((ifp
->if_flags
& (XFS_IFINLINE
|XFS_IFEXTENTS
|XFS_IFEXTIREC
)) ==
915 memset(&args
, 0, sizeof(args
));
917 args
.mp
= ip
->i_mount
;
918 args
.firstblock
= *firstblock
;
920 * Allocate a block. We know we need only one, since the
921 * file currently fits in an inode.
923 if (*firstblock
== NULLFSBLOCK
) {
924 args
.fsbno
= XFS_INO_TO_FSB(args
.mp
, ip
->i_ino
);
925 args
.type
= XFS_ALLOCTYPE_START_BNO
;
927 args
.fsbno
= *firstblock
;
928 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
931 args
.minlen
= args
.maxlen
= args
.prod
= 1;
932 error
= xfs_alloc_vextent(&args
);
936 /* Can't fail, the space was reserved. */
937 ASSERT(args
.fsbno
!= NULLFSBLOCK
);
938 ASSERT(args
.len
== 1);
939 *firstblock
= args
.fsbno
;
940 bp
= xfs_btree_get_bufl(args
.mp
, tp
, args
.fsbno
, 0);
943 * Initialise the block and copy the data
945 * Note: init_fn must set the buffer log item type correctly!
947 init_fn(tp
, bp
, ip
, ifp
);
949 /* account for the change in fork size and log everything */
950 xfs_trans_log_buf(tp
, bp
, 0, ifp
->if_bytes
- 1);
951 xfs_idata_realloc(ip
, -ifp
->if_bytes
, whichfork
);
952 xfs_bmap_local_to_extents_empty(ip
, whichfork
);
953 flags
|= XFS_ILOG_CORE
;
955 xfs_iext_add(ifp
, 0, 1);
956 ep
= xfs_iext_get_ext(ifp
, 0);
957 xfs_bmbt_set_allf(ep
, 0, args
.fsbno
, 1, XFS_EXT_NORM
);
958 trace_xfs_bmap_post_update(ip
, 0,
959 whichfork
== XFS_ATTR_FORK
? BMAP_ATTRFORK
: 0,
961 XFS_IFORK_NEXT_SET(ip
, whichfork
, 1);
962 ip
->i_d
.di_nblocks
= 1;
963 xfs_trans_mod_dquot_byino(tp
, ip
,
964 XFS_TRANS_DQ_BCOUNT
, 1L);
965 flags
|= xfs_ilog_fext(whichfork
);
973 * Called from xfs_bmap_add_attrfork to handle btree format files.
975 STATIC
int /* error */
976 xfs_bmap_add_attrfork_btree(
977 xfs_trans_t
*tp
, /* transaction pointer */
978 xfs_inode_t
*ip
, /* incore inode pointer */
979 xfs_fsblock_t
*firstblock
, /* first block allocated */
980 xfs_bmap_free_t
*flist
, /* blocks to free at commit */
981 int *flags
) /* inode logging flags */
983 xfs_btree_cur_t
*cur
; /* btree cursor */
984 int error
; /* error return value */
985 xfs_mount_t
*mp
; /* file system mount struct */
986 int stat
; /* newroot status */
989 if (ip
->i_df
.if_broot_bytes
<= XFS_IFORK_DSIZE(ip
))
990 *flags
|= XFS_ILOG_DBROOT
;
992 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, XFS_DATA_FORK
);
993 cur
->bc_private
.b
.flist
= flist
;
994 cur
->bc_private
.b
.firstblock
= *firstblock
;
995 if ((error
= xfs_bmbt_lookup_ge(cur
, 0, 0, 0, &stat
)))
997 /* must be at least one entry */
998 XFS_WANT_CORRUPTED_GOTO(mp
, stat
== 1, error0
);
999 if ((error
= xfs_btree_new_iroot(cur
, flags
, &stat
)))
1002 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
1005 *firstblock
= cur
->bc_private
.b
.firstblock
;
1006 cur
->bc_private
.b
.allocated
= 0;
1007 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
1011 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
1016 * Called from xfs_bmap_add_attrfork to handle extents format files.
1018 STATIC
int /* error */
1019 xfs_bmap_add_attrfork_extents(
1020 xfs_trans_t
*tp
, /* transaction pointer */
1021 xfs_inode_t
*ip
, /* incore inode pointer */
1022 xfs_fsblock_t
*firstblock
, /* first block allocated */
1023 xfs_bmap_free_t
*flist
, /* blocks to free at commit */
1024 int *flags
) /* inode logging flags */
1026 xfs_btree_cur_t
*cur
; /* bmap btree cursor */
1027 int error
; /* error return value */
1029 if (ip
->i_d
.di_nextents
* sizeof(xfs_bmbt_rec_t
) <= XFS_IFORK_DSIZE(ip
))
1032 error
= xfs_bmap_extents_to_btree(tp
, ip
, firstblock
, flist
, &cur
, 0,
1033 flags
, XFS_DATA_FORK
);
1035 cur
->bc_private
.b
.allocated
= 0;
1036 xfs_btree_del_cursor(cur
,
1037 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
1043 * Called from xfs_bmap_add_attrfork to handle local format files. Each
1044 * different data fork content type needs a different callout to do the
1045 * conversion. Some are basic and only require special block initialisation
1046 * callouts for the data formating, others (directories) are so specialised they
1047 * handle everything themselves.
1049 * XXX (dgc): investigate whether directory conversion can use the generic
1050 * formatting callout. It should be possible - it's just a very complex
1053 STATIC
int /* error */
1054 xfs_bmap_add_attrfork_local(
1055 xfs_trans_t
*tp
, /* transaction pointer */
1056 xfs_inode_t
*ip
, /* incore inode pointer */
1057 xfs_fsblock_t
*firstblock
, /* first block allocated */
1058 xfs_bmap_free_t
*flist
, /* blocks to free at commit */
1059 int *flags
) /* inode logging flags */
1061 xfs_da_args_t dargs
; /* args for dir/attr code */
1063 if (ip
->i_df
.if_bytes
<= XFS_IFORK_DSIZE(ip
))
1066 if (S_ISDIR(ip
->i_d
.di_mode
)) {
1067 memset(&dargs
, 0, sizeof(dargs
));
1068 dargs
.geo
= ip
->i_mount
->m_dir_geo
;
1070 dargs
.firstblock
= firstblock
;
1071 dargs
.flist
= flist
;
1072 dargs
.total
= dargs
.geo
->fsbcount
;
1073 dargs
.whichfork
= XFS_DATA_FORK
;
1075 return xfs_dir2_sf_to_block(&dargs
);
1078 if (S_ISLNK(ip
->i_d
.di_mode
))
1079 return xfs_bmap_local_to_extents(tp
, ip
, firstblock
, 1,
1080 flags
, XFS_DATA_FORK
,
1081 xfs_symlink_local_to_remote
);
1083 /* should only be called for types that support local format data */
1085 return -EFSCORRUPTED
;
1089 * Convert inode from non-attributed to attributed.
1090 * Must not be in a transaction, ip must not be locked.
1092 int /* error code */
1093 xfs_bmap_add_attrfork(
1094 xfs_inode_t
*ip
, /* incore inode pointer */
1095 int size
, /* space new attribute needs */
1096 int rsvd
) /* xact may use reserved blks */
1098 xfs_fsblock_t firstblock
; /* 1st block/ag allocated */
1099 xfs_bmap_free_t flist
; /* freed extent records */
1100 xfs_mount_t
*mp
; /* mount structure */
1101 xfs_trans_t
*tp
; /* transaction pointer */
1102 int blks
; /* space reservation */
1103 int version
= 1; /* superblock attr version */
1104 int committed
; /* xaction was committed */
1105 int logflags
; /* logging flags */
1106 int error
; /* error return value */
1108 ASSERT(XFS_IFORK_Q(ip
) == 0);
1111 ASSERT(!XFS_NOT_DQATTACHED(mp
, ip
));
1112 tp
= xfs_trans_alloc(mp
, XFS_TRANS_ADDAFORK
);
1113 blks
= XFS_ADDAFORK_SPACE_RES(mp
);
1115 tp
->t_flags
|= XFS_TRANS_RESERVE
;
1116 error
= xfs_trans_reserve(tp
, &M_RES(mp
)->tr_addafork
, blks
, 0);
1118 xfs_trans_cancel(tp
);
1121 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
1122 error
= xfs_trans_reserve_quota_nblks(tp
, ip
, blks
, 0, rsvd
?
1123 XFS_QMOPT_RES_REGBLKS
| XFS_QMOPT_FORCE_RES
:
1124 XFS_QMOPT_RES_REGBLKS
);
1127 if (XFS_IFORK_Q(ip
))
1129 if (ip
->i_d
.di_aformat
!= XFS_DINODE_FMT_EXTENTS
) {
1131 * For inodes coming from pre-6.2 filesystems.
1133 ASSERT(ip
->i_d
.di_aformat
== 0);
1134 ip
->i_d
.di_aformat
= XFS_DINODE_FMT_EXTENTS
;
1136 ASSERT(ip
->i_d
.di_anextents
== 0);
1138 xfs_trans_ijoin(tp
, ip
, 0);
1139 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
1141 switch (ip
->i_d
.di_format
) {
1142 case XFS_DINODE_FMT_DEV
:
1143 ip
->i_d
.di_forkoff
= roundup(sizeof(xfs_dev_t
), 8) >> 3;
1145 case XFS_DINODE_FMT_UUID
:
1146 ip
->i_d
.di_forkoff
= roundup(sizeof(uuid_t
), 8) >> 3;
1148 case XFS_DINODE_FMT_LOCAL
:
1149 case XFS_DINODE_FMT_EXTENTS
:
1150 case XFS_DINODE_FMT_BTREE
:
1151 ip
->i_d
.di_forkoff
= xfs_attr_shortform_bytesfit(ip
, size
);
1152 if (!ip
->i_d
.di_forkoff
)
1153 ip
->i_d
.di_forkoff
= xfs_default_attroffset(ip
) >> 3;
1154 else if (mp
->m_flags
& XFS_MOUNT_ATTR2
)
1163 ASSERT(ip
->i_afp
== NULL
);
1164 ip
->i_afp
= kmem_zone_zalloc(xfs_ifork_zone
, KM_SLEEP
);
1165 ip
->i_afp
->if_flags
= XFS_IFEXTENTS
;
1167 xfs_bmap_init(&flist
, &firstblock
);
1168 switch (ip
->i_d
.di_format
) {
1169 case XFS_DINODE_FMT_LOCAL
:
1170 error
= xfs_bmap_add_attrfork_local(tp
, ip
, &firstblock
, &flist
,
1173 case XFS_DINODE_FMT_EXTENTS
:
1174 error
= xfs_bmap_add_attrfork_extents(tp
, ip
, &firstblock
,
1177 case XFS_DINODE_FMT_BTREE
:
1178 error
= xfs_bmap_add_attrfork_btree(tp
, ip
, &firstblock
, &flist
,
1186 xfs_trans_log_inode(tp
, ip
, logflags
);
1189 if (!xfs_sb_version_hasattr(&mp
->m_sb
) ||
1190 (!xfs_sb_version_hasattr2(&mp
->m_sb
) && version
== 2)) {
1191 bool log_sb
= false;
1193 spin_lock(&mp
->m_sb_lock
);
1194 if (!xfs_sb_version_hasattr(&mp
->m_sb
)) {
1195 xfs_sb_version_addattr(&mp
->m_sb
);
1198 if (!xfs_sb_version_hasattr2(&mp
->m_sb
) && version
== 2) {
1199 xfs_sb_version_addattr2(&mp
->m_sb
);
1202 spin_unlock(&mp
->m_sb_lock
);
1207 error
= xfs_bmap_finish(&tp
, &flist
, &committed
);
1210 error
= xfs_trans_commit(tp
);
1211 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
1215 xfs_bmap_cancel(&flist
);
1217 xfs_trans_cancel(tp
);
1218 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
1223 * Internal and external extent tree search functions.
1227 * Read in the extents to if_extents.
1228 * All inode fields are set up by caller, we just traverse the btree
1229 * and copy the records in. If the file system cannot contain unwritten
1230 * extents, the records are checked for no "state" flags.
1233 xfs_bmap_read_extents(
1234 xfs_trans_t
*tp
, /* transaction pointer */
1235 xfs_inode_t
*ip
, /* incore inode */
1236 int whichfork
) /* data or attr fork */
1238 struct xfs_btree_block
*block
; /* current btree block */
1239 xfs_fsblock_t bno
; /* block # of "block" */
1240 xfs_buf_t
*bp
; /* buffer for "block" */
1241 int error
; /* error return value */
1242 xfs_exntfmt_t exntf
; /* XFS_EXTFMT_NOSTATE, if checking */
1243 xfs_extnum_t i
, j
; /* index into the extents list */
1244 xfs_ifork_t
*ifp
; /* fork structure */
1245 int level
; /* btree level, for checking */
1246 xfs_mount_t
*mp
; /* file system mount structure */
1247 __be64
*pp
; /* pointer to block address */
1249 xfs_extnum_t room
; /* number of entries there's room for */
1253 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1254 exntf
= (whichfork
!= XFS_DATA_FORK
) ? XFS_EXTFMT_NOSTATE
:
1255 XFS_EXTFMT_INODE(ip
);
1256 block
= ifp
->if_broot
;
1258 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
1260 level
= be16_to_cpu(block
->bb_level
);
1262 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, 1, ifp
->if_broot_bytes
);
1263 bno
= be64_to_cpu(*pp
);
1264 ASSERT(bno
!= NULLFSBLOCK
);
1265 ASSERT(XFS_FSB_TO_AGNO(mp
, bno
) < mp
->m_sb
.sb_agcount
);
1266 ASSERT(XFS_FSB_TO_AGBNO(mp
, bno
) < mp
->m_sb
.sb_agblocks
);
1268 * Go down the tree until leaf level is reached, following the first
1269 * pointer (leftmost) at each level.
1271 while (level
-- > 0) {
1272 error
= xfs_btree_read_bufl(mp
, tp
, bno
, 0, &bp
,
1273 XFS_BMAP_BTREE_REF
, &xfs_bmbt_buf_ops
);
1276 block
= XFS_BUF_TO_BLOCK(bp
);
1279 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, mp
->m_bmap_dmxr
[1]);
1280 bno
= be64_to_cpu(*pp
);
1281 XFS_WANT_CORRUPTED_GOTO(mp
,
1282 XFS_FSB_SANITY_CHECK(mp
, bno
), error0
);
1283 xfs_trans_brelse(tp
, bp
);
1286 * Here with bp and block set to the leftmost leaf node in the tree.
1288 room
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
1291 * Loop over all leaf nodes. Copy information to the extent records.
1294 xfs_bmbt_rec_t
*frp
;
1295 xfs_fsblock_t nextbno
;
1296 xfs_extnum_t num_recs
;
1299 num_recs
= xfs_btree_get_numrecs(block
);
1300 if (unlikely(i
+ num_recs
> room
)) {
1301 ASSERT(i
+ num_recs
<= room
);
1302 xfs_warn(ip
->i_mount
,
1303 "corrupt dinode %Lu, (btree extents).",
1304 (unsigned long long) ip
->i_ino
);
1305 XFS_CORRUPTION_ERROR("xfs_bmap_read_extents(1)",
1306 XFS_ERRLEVEL_LOW
, ip
->i_mount
, block
);
1310 * Read-ahead the next leaf block, if any.
1312 nextbno
= be64_to_cpu(block
->bb_u
.l
.bb_rightsib
);
1313 if (nextbno
!= NULLFSBLOCK
)
1314 xfs_btree_reada_bufl(mp
, nextbno
, 1,
1317 * Copy records into the extent records.
1319 frp
= XFS_BMBT_REC_ADDR(mp
, block
, 1);
1321 for (j
= 0; j
< num_recs
; j
++, i
++, frp
++) {
1322 xfs_bmbt_rec_host_t
*trp
= xfs_iext_get_ext(ifp
, i
);
1323 trp
->l0
= be64_to_cpu(frp
->l0
);
1324 trp
->l1
= be64_to_cpu(frp
->l1
);
1326 if (exntf
== XFS_EXTFMT_NOSTATE
) {
1328 * Check all attribute bmap btree records and
1329 * any "older" data bmap btree records for a
1330 * set bit in the "extent flag" position.
1332 if (unlikely(xfs_check_nostate_extents(ifp
,
1333 start
, num_recs
))) {
1334 XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
1340 xfs_trans_brelse(tp
, bp
);
1343 * If we've reached the end, stop.
1345 if (bno
== NULLFSBLOCK
)
1347 error
= xfs_btree_read_bufl(mp
, tp
, bno
, 0, &bp
,
1348 XFS_BMAP_BTREE_REF
, &xfs_bmbt_buf_ops
);
1351 block
= XFS_BUF_TO_BLOCK(bp
);
1353 ASSERT(i
== (ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
)));
1354 ASSERT(i
== XFS_IFORK_NEXTENTS(ip
, whichfork
));
1355 XFS_BMAP_TRACE_EXLIST(ip
, i
, whichfork
);
1358 xfs_trans_brelse(tp
, bp
);
1359 return -EFSCORRUPTED
;
1364 * Search the extent records for the entry containing block bno.
1365 * If bno lies in a hole, point to the next entry. If bno lies
1366 * past eof, *eofp will be set, and *prevp will contain the last
1367 * entry (null if none). Else, *lastxp will be set to the index
1368 * of the found entry; *gotp will contain the entry.
1370 STATIC xfs_bmbt_rec_host_t
* /* pointer to found extent entry */
1371 xfs_bmap_search_multi_extents(
1372 xfs_ifork_t
*ifp
, /* inode fork pointer */
1373 xfs_fileoff_t bno
, /* block number searched for */
1374 int *eofp
, /* out: end of file found */
1375 xfs_extnum_t
*lastxp
, /* out: last extent index */
1376 xfs_bmbt_irec_t
*gotp
, /* out: extent entry found */
1377 xfs_bmbt_irec_t
*prevp
) /* out: previous extent entry found */
1379 xfs_bmbt_rec_host_t
*ep
; /* extent record pointer */
1380 xfs_extnum_t lastx
; /* last extent index */
1383 * Initialize the extent entry structure to catch access to
1384 * uninitialized br_startblock field.
1386 gotp
->br_startoff
= 0xffa5a5a5a5a5a5a5LL
;
1387 gotp
->br_blockcount
= 0xa55a5a5a5a5a5a5aLL
;
1388 gotp
->br_state
= XFS_EXT_INVALID
;
1389 gotp
->br_startblock
= 0xffffa5a5a5a5a5a5LL
;
1390 prevp
->br_startoff
= NULLFILEOFF
;
1392 ep
= xfs_iext_bno_to_ext(ifp
, bno
, &lastx
);
1394 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, lastx
- 1), prevp
);
1396 if (lastx
< (ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
))) {
1397 xfs_bmbt_get_all(ep
, gotp
);
1411 * Search the extents list for the inode, for the extent containing bno.
1412 * If bno lies in a hole, point to the next entry. If bno lies past eof,
1413 * *eofp will be set, and *prevp will contain the last entry (null if none).
1414 * Else, *lastxp will be set to the index of the found
1415 * entry; *gotp will contain the entry.
1417 xfs_bmbt_rec_host_t
* /* pointer to found extent entry */
1418 xfs_bmap_search_extents(
1419 xfs_inode_t
*ip
, /* incore inode pointer */
1420 xfs_fileoff_t bno
, /* block number searched for */
1421 int fork
, /* data or attr fork */
1422 int *eofp
, /* out: end of file found */
1423 xfs_extnum_t
*lastxp
, /* out: last extent index */
1424 xfs_bmbt_irec_t
*gotp
, /* out: extent entry found */
1425 xfs_bmbt_irec_t
*prevp
) /* out: previous extent entry found */
1427 xfs_ifork_t
*ifp
; /* inode fork pointer */
1428 xfs_bmbt_rec_host_t
*ep
; /* extent record pointer */
1430 XFS_STATS_INC(xs_look_exlist
);
1431 ifp
= XFS_IFORK_PTR(ip
, fork
);
1433 ep
= xfs_bmap_search_multi_extents(ifp
, bno
, eofp
, lastxp
, gotp
, prevp
);
1435 if (unlikely(!(gotp
->br_startblock
) && (*lastxp
!= NULLEXTNUM
) &&
1436 !(XFS_IS_REALTIME_INODE(ip
) && fork
== XFS_DATA_FORK
))) {
1437 xfs_alert_tag(ip
->i_mount
, XFS_PTAG_FSBLOCK_ZERO
,
1438 "Access to block zero in inode %llu "
1439 "start_block: %llx start_off: %llx "
1440 "blkcnt: %llx extent-state: %x lastx: %x",
1441 (unsigned long long)ip
->i_ino
,
1442 (unsigned long long)gotp
->br_startblock
,
1443 (unsigned long long)gotp
->br_startoff
,
1444 (unsigned long long)gotp
->br_blockcount
,
1445 gotp
->br_state
, *lastxp
);
1446 *lastxp
= NULLEXTNUM
;
1454 * Returns the file-relative block number of the first unused block(s)
1455 * in the file with at least "len" logically contiguous blocks free.
1456 * This is the lowest-address hole if the file has holes, else the first block
1457 * past the end of file.
1458 * Return 0 if the file is currently local (in-inode).
1461 xfs_bmap_first_unused(
1462 xfs_trans_t
*tp
, /* transaction pointer */
1463 xfs_inode_t
*ip
, /* incore inode */
1464 xfs_extlen_t len
, /* size of hole to find */
1465 xfs_fileoff_t
*first_unused
, /* unused block */
1466 int whichfork
) /* data or attr fork */
1468 int error
; /* error return value */
1469 int idx
; /* extent record index */
1470 xfs_ifork_t
*ifp
; /* inode fork pointer */
1471 xfs_fileoff_t lastaddr
; /* last block number seen */
1472 xfs_fileoff_t lowest
; /* lowest useful block */
1473 xfs_fileoff_t max
; /* starting useful block */
1474 xfs_fileoff_t off
; /* offset for this block */
1475 xfs_extnum_t nextents
; /* number of extent entries */
1477 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
||
1478 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
||
1479 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
1480 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
) {
1484 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1485 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
1486 (error
= xfs_iread_extents(tp
, ip
, whichfork
)))
1488 lowest
= *first_unused
;
1489 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
1490 for (idx
= 0, lastaddr
= 0, max
= lowest
; idx
< nextents
; idx
++) {
1491 xfs_bmbt_rec_host_t
*ep
= xfs_iext_get_ext(ifp
, idx
);
1492 off
= xfs_bmbt_get_startoff(ep
);
1494 * See if the hole before this extent will work.
1496 if (off
>= lowest
+ len
&& off
- max
>= len
) {
1497 *first_unused
= max
;
1500 lastaddr
= off
+ xfs_bmbt_get_blockcount(ep
);
1501 max
= XFS_FILEOFF_MAX(lastaddr
, lowest
);
1503 *first_unused
= max
;
1508 * Returns the file-relative block number of the last block - 1 before
1509 * last_block (input value) in the file.
1510 * This is not based on i_size, it is based on the extent records.
1511 * Returns 0 for local files, as they do not have extent records.
1514 xfs_bmap_last_before(
1515 xfs_trans_t
*tp
, /* transaction pointer */
1516 xfs_inode_t
*ip
, /* incore inode */
1517 xfs_fileoff_t
*last_block
, /* last block */
1518 int whichfork
) /* data or attr fork */
1520 xfs_fileoff_t bno
; /* input file offset */
1521 int eof
; /* hit end of file */
1522 xfs_bmbt_rec_host_t
*ep
; /* pointer to last extent */
1523 int error
; /* error return value */
1524 xfs_bmbt_irec_t got
; /* current extent value */
1525 xfs_ifork_t
*ifp
; /* inode fork pointer */
1526 xfs_extnum_t lastx
; /* last extent used */
1527 xfs_bmbt_irec_t prev
; /* previous extent value */
1529 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
&&
1530 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
1531 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_LOCAL
)
1533 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
) {
1537 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1538 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
1539 (error
= xfs_iread_extents(tp
, ip
, whichfork
)))
1541 bno
= *last_block
- 1;
1542 ep
= xfs_bmap_search_extents(ip
, bno
, whichfork
, &eof
, &lastx
, &got
,
1544 if (eof
|| xfs_bmbt_get_startoff(ep
) > bno
) {
1545 if (prev
.br_startoff
== NULLFILEOFF
)
1548 *last_block
= prev
.br_startoff
+ prev
.br_blockcount
;
1551 * Otherwise *last_block is already the right answer.
1557 xfs_bmap_last_extent(
1558 struct xfs_trans
*tp
,
1559 struct xfs_inode
*ip
,
1561 struct xfs_bmbt_irec
*rec
,
1564 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1568 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
1569 error
= xfs_iread_extents(tp
, ip
, whichfork
);
1574 nextents
= ifp
->if_bytes
/ sizeof(xfs_bmbt_rec_t
);
1575 if (nextents
== 0) {
1580 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, nextents
- 1), rec
);
1586 * Check the last inode extent to determine whether this allocation will result
1587 * in blocks being allocated at the end of the file. When we allocate new data
1588 * blocks at the end of the file which do not start at the previous data block,
1589 * we will try to align the new blocks at stripe unit boundaries.
1591 * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be
1592 * at, or past the EOF.
1596 struct xfs_bmalloca
*bma
,
1599 struct xfs_bmbt_irec rec
;
1604 error
= xfs_bmap_last_extent(NULL
, bma
->ip
, whichfork
, &rec
,
1615 * Check if we are allocation or past the last extent, or at least into
1616 * the last delayed allocated extent.
1618 bma
->aeof
= bma
->offset
>= rec
.br_startoff
+ rec
.br_blockcount
||
1619 (bma
->offset
>= rec
.br_startoff
&&
1620 isnullstartblock(rec
.br_startblock
));
1625 * Returns the file-relative block number of the first block past eof in
1626 * the file. This is not based on i_size, it is based on the extent records.
1627 * Returns 0 for local files, as they do not have extent records.
1630 xfs_bmap_last_offset(
1631 struct xfs_inode
*ip
,
1632 xfs_fileoff_t
*last_block
,
1635 struct xfs_bmbt_irec rec
;
1641 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
)
1644 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
&&
1645 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
1648 error
= xfs_bmap_last_extent(NULL
, ip
, whichfork
, &rec
, &is_empty
);
1649 if (error
|| is_empty
)
1652 *last_block
= rec
.br_startoff
+ rec
.br_blockcount
;
1657 * Returns whether the selected fork of the inode has exactly one
1658 * block or not. For the data fork we check this matches di_size,
1659 * implying the file's range is 0..bsize-1.
1661 int /* 1=>1 block, 0=>otherwise */
1663 xfs_inode_t
*ip
, /* incore inode */
1664 int whichfork
) /* data or attr fork */
1666 xfs_bmbt_rec_host_t
*ep
; /* ptr to fork's extent */
1667 xfs_ifork_t
*ifp
; /* inode fork pointer */
1668 int rval
; /* return value */
1669 xfs_bmbt_irec_t s
; /* internal version of extent */
1672 if (whichfork
== XFS_DATA_FORK
)
1673 return XFS_ISIZE(ip
) == ip
->i_mount
->m_sb
.sb_blocksize
;
1675 if (XFS_IFORK_NEXTENTS(ip
, whichfork
) != 1)
1677 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
1679 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1680 ASSERT(ifp
->if_flags
& XFS_IFEXTENTS
);
1681 ep
= xfs_iext_get_ext(ifp
, 0);
1682 xfs_bmbt_get_all(ep
, &s
);
1683 rval
= s
.br_startoff
== 0 && s
.br_blockcount
== 1;
1684 if (rval
&& whichfork
== XFS_DATA_FORK
)
1685 ASSERT(XFS_ISIZE(ip
) == ip
->i_mount
->m_sb
.sb_blocksize
);
1690 * Extent tree manipulation functions used during allocation.
1694 * Convert a delayed allocation to a real allocation.
1696 STATIC
int /* error */
1697 xfs_bmap_add_extent_delay_real(
1698 struct xfs_bmalloca
*bma
)
1700 struct xfs_bmbt_irec
*new = &bma
->got
;
1701 int diff
; /* temp value */
1702 xfs_bmbt_rec_host_t
*ep
; /* extent entry for idx */
1703 int error
; /* error return value */
1704 int i
; /* temp state */
1705 xfs_ifork_t
*ifp
; /* inode fork pointer */
1706 xfs_fileoff_t new_endoff
; /* end offset of new entry */
1707 xfs_bmbt_irec_t r
[3]; /* neighbor extent entries */
1708 /* left is 0, right is 1, prev is 2 */
1709 int rval
=0; /* return value (logging flags) */
1710 int state
= 0;/* state bits, accessed thru macros */
1711 xfs_filblks_t da_new
; /* new count del alloc blocks used */
1712 xfs_filblks_t da_old
; /* old count del alloc blocks used */
1713 xfs_filblks_t temp
=0; /* value for da_new calculations */
1714 xfs_filblks_t temp2
=0;/* value for da_new calculations */
1715 int tmp_rval
; /* partial logging flags */
1716 struct xfs_mount
*mp
;
1718 mp
= bma
->tp
? bma
->tp
->t_mountp
: NULL
;
1719 ifp
= XFS_IFORK_PTR(bma
->ip
, XFS_DATA_FORK
);
1721 ASSERT(bma
->idx
>= 0);
1722 ASSERT(bma
->idx
<= ifp
->if_bytes
/ sizeof(struct xfs_bmbt_rec
));
1723 ASSERT(!isnullstartblock(new->br_startblock
));
1725 (bma
->cur
->bc_private
.b
.flags
& XFS_BTCUR_BPRV_WASDEL
));
1727 XFS_STATS_INC(xs_add_exlist
);
1734 * Set up a bunch of variables to make the tests simpler.
1736 ep
= xfs_iext_get_ext(ifp
, bma
->idx
);
1737 xfs_bmbt_get_all(ep
, &PREV
);
1738 new_endoff
= new->br_startoff
+ new->br_blockcount
;
1739 ASSERT(PREV
.br_startoff
<= new->br_startoff
);
1740 ASSERT(PREV
.br_startoff
+ PREV
.br_blockcount
>= new_endoff
);
1742 da_old
= startblockval(PREV
.br_startblock
);
1746 * Set flags determining what part of the previous delayed allocation
1747 * extent is being replaced by a real allocation.
1749 if (PREV
.br_startoff
== new->br_startoff
)
1750 state
|= BMAP_LEFT_FILLING
;
1751 if (PREV
.br_startoff
+ PREV
.br_blockcount
== new_endoff
)
1752 state
|= BMAP_RIGHT_FILLING
;
1755 * Check and set flags if this segment has a left neighbor.
1756 * Don't set contiguous if the combined extent would be too large.
1759 state
|= BMAP_LEFT_VALID
;
1760 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
- 1), &LEFT
);
1762 if (isnullstartblock(LEFT
.br_startblock
))
1763 state
|= BMAP_LEFT_DELAY
;
1766 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
1767 LEFT
.br_startoff
+ LEFT
.br_blockcount
== new->br_startoff
&&
1768 LEFT
.br_startblock
+ LEFT
.br_blockcount
== new->br_startblock
&&
1769 LEFT
.br_state
== new->br_state
&&
1770 LEFT
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
1771 state
|= BMAP_LEFT_CONTIG
;
1774 * Check and set flags if this segment has a right neighbor.
1775 * Don't set contiguous if the combined extent would be too large.
1776 * Also check for all-three-contiguous being too large.
1778 if (bma
->idx
< bma
->ip
->i_df
.if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
) - 1) {
1779 state
|= BMAP_RIGHT_VALID
;
1780 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
+ 1), &RIGHT
);
1782 if (isnullstartblock(RIGHT
.br_startblock
))
1783 state
|= BMAP_RIGHT_DELAY
;
1786 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
1787 new_endoff
== RIGHT
.br_startoff
&&
1788 new->br_startblock
+ new->br_blockcount
== RIGHT
.br_startblock
&&
1789 new->br_state
== RIGHT
.br_state
&&
1790 new->br_blockcount
+ RIGHT
.br_blockcount
<= MAXEXTLEN
&&
1791 ((state
& (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
1792 BMAP_RIGHT_FILLING
)) !=
1793 (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
1794 BMAP_RIGHT_FILLING
) ||
1795 LEFT
.br_blockcount
+ new->br_blockcount
+ RIGHT
.br_blockcount
1797 state
|= BMAP_RIGHT_CONTIG
;
1801 * Switch out based on the FILLING and CONTIG state bits.
1803 switch (state
& (BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
1804 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
)) {
1805 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
1806 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
1808 * Filling in all of a previously delayed allocation extent.
1809 * The left and right neighbors are both contiguous with new.
1812 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1813 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, bma
->idx
),
1814 LEFT
.br_blockcount
+ PREV
.br_blockcount
+
1815 RIGHT
.br_blockcount
);
1816 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1818 xfs_iext_remove(bma
->ip
, bma
->idx
+ 1, 2, state
);
1819 bma
->ip
->i_d
.di_nextents
--;
1820 if (bma
->cur
== NULL
)
1821 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1823 rval
= XFS_ILOG_CORE
;
1824 error
= xfs_bmbt_lookup_eq(bma
->cur
, RIGHT
.br_startoff
,
1825 RIGHT
.br_startblock
,
1826 RIGHT
.br_blockcount
, &i
);
1829 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1830 error
= xfs_btree_delete(bma
->cur
, &i
);
1833 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1834 error
= xfs_btree_decrement(bma
->cur
, 0, &i
);
1837 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1838 error
= xfs_bmbt_update(bma
->cur
, LEFT
.br_startoff
,
1840 LEFT
.br_blockcount
+
1841 PREV
.br_blockcount
+
1842 RIGHT
.br_blockcount
, LEFT
.br_state
);
1848 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
1850 * Filling in all of a previously delayed allocation extent.
1851 * The left neighbor is contiguous, the right is not.
1855 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1856 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, bma
->idx
),
1857 LEFT
.br_blockcount
+ PREV
.br_blockcount
);
1858 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1860 xfs_iext_remove(bma
->ip
, bma
->idx
+ 1, 1, state
);
1861 if (bma
->cur
== NULL
)
1862 rval
= XFS_ILOG_DEXT
;
1865 error
= xfs_bmbt_lookup_eq(bma
->cur
, LEFT
.br_startoff
,
1866 LEFT
.br_startblock
, LEFT
.br_blockcount
,
1870 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1871 error
= xfs_bmbt_update(bma
->cur
, LEFT
.br_startoff
,
1873 LEFT
.br_blockcount
+
1874 PREV
.br_blockcount
, LEFT
.br_state
);
1880 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
1882 * Filling in all of a previously delayed allocation extent.
1883 * The right neighbor is contiguous, the left is not.
1885 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1886 xfs_bmbt_set_startblock(ep
, new->br_startblock
);
1887 xfs_bmbt_set_blockcount(ep
,
1888 PREV
.br_blockcount
+ RIGHT
.br_blockcount
);
1889 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1891 xfs_iext_remove(bma
->ip
, bma
->idx
+ 1, 1, state
);
1892 if (bma
->cur
== NULL
)
1893 rval
= XFS_ILOG_DEXT
;
1896 error
= xfs_bmbt_lookup_eq(bma
->cur
, RIGHT
.br_startoff
,
1897 RIGHT
.br_startblock
,
1898 RIGHT
.br_blockcount
, &i
);
1901 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1902 error
= xfs_bmbt_update(bma
->cur
, PREV
.br_startoff
,
1904 PREV
.br_blockcount
+
1905 RIGHT
.br_blockcount
, PREV
.br_state
);
1911 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
1913 * Filling in all of a previously delayed allocation extent.
1914 * Neither the left nor right neighbors are contiguous with
1917 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1918 xfs_bmbt_set_startblock(ep
, new->br_startblock
);
1919 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1921 bma
->ip
->i_d
.di_nextents
++;
1922 if (bma
->cur
== NULL
)
1923 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1925 rval
= XFS_ILOG_CORE
;
1926 error
= xfs_bmbt_lookup_eq(bma
->cur
, new->br_startoff
,
1927 new->br_startblock
, new->br_blockcount
,
1931 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
1932 bma
->cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
1933 error
= xfs_btree_insert(bma
->cur
, &i
);
1936 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1940 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
:
1942 * Filling in the first part of a previous delayed allocation.
1943 * The left neighbor is contiguous.
1945 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
- 1, state
, _THIS_IP_
);
1946 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, bma
->idx
- 1),
1947 LEFT
.br_blockcount
+ new->br_blockcount
);
1948 xfs_bmbt_set_startoff(ep
,
1949 PREV
.br_startoff
+ new->br_blockcount
);
1950 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
- 1, state
, _THIS_IP_
);
1952 temp
= PREV
.br_blockcount
- new->br_blockcount
;
1953 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1954 xfs_bmbt_set_blockcount(ep
, temp
);
1955 if (bma
->cur
== NULL
)
1956 rval
= XFS_ILOG_DEXT
;
1959 error
= xfs_bmbt_lookup_eq(bma
->cur
, LEFT
.br_startoff
,
1960 LEFT
.br_startblock
, LEFT
.br_blockcount
,
1964 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1965 error
= xfs_bmbt_update(bma
->cur
, LEFT
.br_startoff
,
1967 LEFT
.br_blockcount
+
1973 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
1974 startblockval(PREV
.br_startblock
));
1975 xfs_bmbt_set_startblock(ep
, nullstartblock(da_new
));
1976 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1981 case BMAP_LEFT_FILLING
:
1983 * Filling in the first part of a previous delayed allocation.
1984 * The left neighbor is not contiguous.
1986 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1987 xfs_bmbt_set_startoff(ep
, new_endoff
);
1988 temp
= PREV
.br_blockcount
- new->br_blockcount
;
1989 xfs_bmbt_set_blockcount(ep
, temp
);
1990 xfs_iext_insert(bma
->ip
, bma
->idx
, 1, new, state
);
1991 bma
->ip
->i_d
.di_nextents
++;
1992 if (bma
->cur
== NULL
)
1993 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1995 rval
= XFS_ILOG_CORE
;
1996 error
= xfs_bmbt_lookup_eq(bma
->cur
, new->br_startoff
,
1997 new->br_startblock
, new->br_blockcount
,
2001 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
2002 bma
->cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
2003 error
= xfs_btree_insert(bma
->cur
, &i
);
2006 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2009 if (xfs_bmap_needs_btree(bma
->ip
, XFS_DATA_FORK
)) {
2010 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
2011 bma
->firstblock
, bma
->flist
,
2012 &bma
->cur
, 1, &tmp_rval
, XFS_DATA_FORK
);
2017 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
2018 startblockval(PREV
.br_startblock
) -
2019 (bma
->cur
? bma
->cur
->bc_private
.b
.allocated
: 0));
2020 ep
= xfs_iext_get_ext(ifp
, bma
->idx
+ 1);
2021 xfs_bmbt_set_startblock(ep
, nullstartblock(da_new
));
2022 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
+ 1, state
, _THIS_IP_
);
2025 case BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2027 * Filling in the last part of a previous delayed allocation.
2028 * The right neighbor is contiguous with the new allocation.
2030 temp
= PREV
.br_blockcount
- new->br_blockcount
;
2031 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
+ 1, state
, _THIS_IP_
);
2032 xfs_bmbt_set_blockcount(ep
, temp
);
2033 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp
, bma
->idx
+ 1),
2034 new->br_startoff
, new->br_startblock
,
2035 new->br_blockcount
+ RIGHT
.br_blockcount
,
2037 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
+ 1, state
, _THIS_IP_
);
2038 if (bma
->cur
== NULL
)
2039 rval
= XFS_ILOG_DEXT
;
2042 error
= xfs_bmbt_lookup_eq(bma
->cur
, RIGHT
.br_startoff
,
2043 RIGHT
.br_startblock
,
2044 RIGHT
.br_blockcount
, &i
);
2047 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2048 error
= xfs_bmbt_update(bma
->cur
, new->br_startoff
,
2050 new->br_blockcount
+
2051 RIGHT
.br_blockcount
,
2057 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
2058 startblockval(PREV
.br_startblock
));
2059 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2060 xfs_bmbt_set_startblock(ep
, nullstartblock(da_new
));
2061 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2066 case BMAP_RIGHT_FILLING
:
2068 * Filling in the last part of a previous delayed allocation.
2069 * The right neighbor is not contiguous.
2071 temp
= PREV
.br_blockcount
- new->br_blockcount
;
2072 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2073 xfs_bmbt_set_blockcount(ep
, temp
);
2074 xfs_iext_insert(bma
->ip
, bma
->idx
+ 1, 1, new, state
);
2075 bma
->ip
->i_d
.di_nextents
++;
2076 if (bma
->cur
== NULL
)
2077 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2079 rval
= XFS_ILOG_CORE
;
2080 error
= xfs_bmbt_lookup_eq(bma
->cur
, new->br_startoff
,
2081 new->br_startblock
, new->br_blockcount
,
2085 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
2086 bma
->cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
2087 error
= xfs_btree_insert(bma
->cur
, &i
);
2090 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2093 if (xfs_bmap_needs_btree(bma
->ip
, XFS_DATA_FORK
)) {
2094 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
2095 bma
->firstblock
, bma
->flist
, &bma
->cur
, 1,
2096 &tmp_rval
, XFS_DATA_FORK
);
2101 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
2102 startblockval(PREV
.br_startblock
) -
2103 (bma
->cur
? bma
->cur
->bc_private
.b
.allocated
: 0));
2104 ep
= xfs_iext_get_ext(ifp
, bma
->idx
);
2105 xfs_bmbt_set_startblock(ep
, nullstartblock(da_new
));
2106 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2113 * Filling in the middle part of a previous delayed allocation.
2114 * Contiguity is impossible here.
2115 * This case is avoided almost all the time.
2117 * We start with a delayed allocation:
2119 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
2122 * and we are allocating:
2123 * +rrrrrrrrrrrrrrrrr+
2126 * and we set it up for insertion as:
2127 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
2129 * PREV @ idx LEFT RIGHT
2130 * inserted at idx + 1
2132 temp
= new->br_startoff
- PREV
.br_startoff
;
2133 temp2
= PREV
.br_startoff
+ PREV
.br_blockcount
- new_endoff
;
2134 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, 0, _THIS_IP_
);
2135 xfs_bmbt_set_blockcount(ep
, temp
); /* truncate PREV */
2137 RIGHT
.br_state
= PREV
.br_state
;
2138 RIGHT
.br_startblock
= nullstartblock(
2139 (int)xfs_bmap_worst_indlen(bma
->ip
, temp2
));
2140 RIGHT
.br_startoff
= new_endoff
;
2141 RIGHT
.br_blockcount
= temp2
;
2142 /* insert LEFT (r[0]) and RIGHT (r[1]) at the same time */
2143 xfs_iext_insert(bma
->ip
, bma
->idx
+ 1, 2, &LEFT
, state
);
2144 bma
->ip
->i_d
.di_nextents
++;
2145 if (bma
->cur
== NULL
)
2146 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2148 rval
= XFS_ILOG_CORE
;
2149 error
= xfs_bmbt_lookup_eq(bma
->cur
, new->br_startoff
,
2150 new->br_startblock
, new->br_blockcount
,
2154 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
2155 bma
->cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
2156 error
= xfs_btree_insert(bma
->cur
, &i
);
2159 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2162 if (xfs_bmap_needs_btree(bma
->ip
, XFS_DATA_FORK
)) {
2163 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
2164 bma
->firstblock
, bma
->flist
, &bma
->cur
,
2165 1, &tmp_rval
, XFS_DATA_FORK
);
2170 temp
= xfs_bmap_worst_indlen(bma
->ip
, temp
);
2171 temp2
= xfs_bmap_worst_indlen(bma
->ip
, temp2
);
2172 diff
= (int)(temp
+ temp2
- startblockval(PREV
.br_startblock
) -
2173 (bma
->cur
? bma
->cur
->bc_private
.b
.allocated
: 0));
2175 error
= xfs_mod_fdblocks(bma
->ip
->i_mount
,
2176 -((int64_t)diff
), false);
2182 ep
= xfs_iext_get_ext(ifp
, bma
->idx
);
2183 xfs_bmbt_set_startblock(ep
, nullstartblock((int)temp
));
2184 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2185 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
+ 2, state
, _THIS_IP_
);
2186 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp
, bma
->idx
+ 2),
2187 nullstartblock((int)temp2
));
2188 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
+ 2, state
, _THIS_IP_
);
2191 da_new
= temp
+ temp2
;
2194 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2195 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2196 case BMAP_LEFT_FILLING
| BMAP_RIGHT_CONTIG
:
2197 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
2198 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2199 case BMAP_LEFT_CONTIG
:
2200 case BMAP_RIGHT_CONTIG
:
2202 * These cases are all impossible.
2207 /* convert to a btree if necessary */
2208 if (xfs_bmap_needs_btree(bma
->ip
, XFS_DATA_FORK
)) {
2209 int tmp_logflags
; /* partial log flag return val */
2211 ASSERT(bma
->cur
== NULL
);
2212 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
2213 bma
->firstblock
, bma
->flist
, &bma
->cur
,
2214 da_old
> 0, &tmp_logflags
, XFS_DATA_FORK
);
2215 bma
->logflags
|= tmp_logflags
;
2220 /* adjust for changes in reserved delayed indirect blocks */
2221 if (da_old
|| da_new
) {
2224 temp
+= bma
->cur
->bc_private
.b
.allocated
;
2225 ASSERT(temp
<= da_old
);
2227 xfs_mod_fdblocks(bma
->ip
->i_mount
,
2228 (int64_t)(da_old
- temp
), false);
2231 /* clear out the allocated field, done with it now in any case. */
2233 bma
->cur
->bc_private
.b
.allocated
= 0;
2235 xfs_bmap_check_leaf_extents(bma
->cur
, bma
->ip
, XFS_DATA_FORK
);
2237 bma
->logflags
|= rval
;
2245 * Convert an unwritten allocation to a real allocation or vice versa.
2247 STATIC
int /* error */
2248 xfs_bmap_add_extent_unwritten_real(
2249 struct xfs_trans
*tp
,
2250 xfs_inode_t
*ip
, /* incore inode pointer */
2251 xfs_extnum_t
*idx
, /* extent number to update/insert */
2252 xfs_btree_cur_t
**curp
, /* if *curp is null, not a btree */
2253 xfs_bmbt_irec_t
*new, /* new data to add to file extents */
2254 xfs_fsblock_t
*first
, /* pointer to firstblock variable */
2255 xfs_bmap_free_t
*flist
, /* list of extents to be freed */
2256 int *logflagsp
) /* inode logging flags */
2258 xfs_btree_cur_t
*cur
; /* btree cursor */
2259 xfs_bmbt_rec_host_t
*ep
; /* extent entry for idx */
2260 int error
; /* error return value */
2261 int i
; /* temp state */
2262 xfs_ifork_t
*ifp
; /* inode fork pointer */
2263 xfs_fileoff_t new_endoff
; /* end offset of new entry */
2264 xfs_exntst_t newext
; /* new extent state */
2265 xfs_exntst_t oldext
; /* old extent state */
2266 xfs_bmbt_irec_t r
[3]; /* neighbor extent entries */
2267 /* left is 0, right is 1, prev is 2 */
2268 int rval
=0; /* return value (logging flags) */
2269 int state
= 0;/* state bits, accessed thru macros */
2270 struct xfs_mount
*mp
= tp
->t_mountp
;
2275 ifp
= XFS_IFORK_PTR(ip
, XFS_DATA_FORK
);
2278 ASSERT(*idx
<= ifp
->if_bytes
/ sizeof(struct xfs_bmbt_rec
));
2279 ASSERT(!isnullstartblock(new->br_startblock
));
2281 XFS_STATS_INC(xs_add_exlist
);
2288 * Set up a bunch of variables to make the tests simpler.
2291 ep
= xfs_iext_get_ext(ifp
, *idx
);
2292 xfs_bmbt_get_all(ep
, &PREV
);
2293 newext
= new->br_state
;
2294 oldext
= (newext
== XFS_EXT_UNWRITTEN
) ?
2295 XFS_EXT_NORM
: XFS_EXT_UNWRITTEN
;
2296 ASSERT(PREV
.br_state
== oldext
);
2297 new_endoff
= new->br_startoff
+ new->br_blockcount
;
2298 ASSERT(PREV
.br_startoff
<= new->br_startoff
);
2299 ASSERT(PREV
.br_startoff
+ PREV
.br_blockcount
>= new_endoff
);
2302 * Set flags determining what part of the previous oldext allocation
2303 * extent is being replaced by a newext allocation.
2305 if (PREV
.br_startoff
== new->br_startoff
)
2306 state
|= BMAP_LEFT_FILLING
;
2307 if (PREV
.br_startoff
+ PREV
.br_blockcount
== new_endoff
)
2308 state
|= BMAP_RIGHT_FILLING
;
2311 * Check and set flags if this segment has a left neighbor.
2312 * Don't set contiguous if the combined extent would be too large.
2315 state
|= BMAP_LEFT_VALID
;
2316 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, *idx
- 1), &LEFT
);
2318 if (isnullstartblock(LEFT
.br_startblock
))
2319 state
|= BMAP_LEFT_DELAY
;
2322 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
2323 LEFT
.br_startoff
+ LEFT
.br_blockcount
== new->br_startoff
&&
2324 LEFT
.br_startblock
+ LEFT
.br_blockcount
== new->br_startblock
&&
2325 LEFT
.br_state
== newext
&&
2326 LEFT
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
2327 state
|= BMAP_LEFT_CONTIG
;
2330 * Check and set flags if this segment has a right neighbor.
2331 * Don't set contiguous if the combined extent would be too large.
2332 * Also check for all-three-contiguous being too large.
2334 if (*idx
< ip
->i_df
.if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
) - 1) {
2335 state
|= BMAP_RIGHT_VALID
;
2336 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, *idx
+ 1), &RIGHT
);
2337 if (isnullstartblock(RIGHT
.br_startblock
))
2338 state
|= BMAP_RIGHT_DELAY
;
2341 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
2342 new_endoff
== RIGHT
.br_startoff
&&
2343 new->br_startblock
+ new->br_blockcount
== RIGHT
.br_startblock
&&
2344 newext
== RIGHT
.br_state
&&
2345 new->br_blockcount
+ RIGHT
.br_blockcount
<= MAXEXTLEN
&&
2346 ((state
& (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
2347 BMAP_RIGHT_FILLING
)) !=
2348 (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
2349 BMAP_RIGHT_FILLING
) ||
2350 LEFT
.br_blockcount
+ new->br_blockcount
+ RIGHT
.br_blockcount
2352 state
|= BMAP_RIGHT_CONTIG
;
2355 * Switch out based on the FILLING and CONTIG state bits.
2357 switch (state
& (BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
2358 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
)) {
2359 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
2360 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2362 * Setting all of a previous oldext extent to newext.
2363 * The left and right neighbors are both contiguous with new.
2367 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2368 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
),
2369 LEFT
.br_blockcount
+ PREV
.br_blockcount
+
2370 RIGHT
.br_blockcount
);
2371 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2373 xfs_iext_remove(ip
, *idx
+ 1, 2, state
);
2374 ip
->i_d
.di_nextents
-= 2;
2376 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2378 rval
= XFS_ILOG_CORE
;
2379 if ((error
= xfs_bmbt_lookup_eq(cur
, RIGHT
.br_startoff
,
2380 RIGHT
.br_startblock
,
2381 RIGHT
.br_blockcount
, &i
)))
2383 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2384 if ((error
= xfs_btree_delete(cur
, &i
)))
2386 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2387 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2389 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2390 if ((error
= xfs_btree_delete(cur
, &i
)))
2392 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2393 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2395 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2396 if ((error
= xfs_bmbt_update(cur
, LEFT
.br_startoff
,
2398 LEFT
.br_blockcount
+ PREV
.br_blockcount
+
2399 RIGHT
.br_blockcount
, LEFT
.br_state
)))
2404 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
2406 * Setting all of a previous oldext extent to newext.
2407 * The left neighbor is contiguous, the right is not.
2411 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2412 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
),
2413 LEFT
.br_blockcount
+ PREV
.br_blockcount
);
2414 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2416 xfs_iext_remove(ip
, *idx
+ 1, 1, state
);
2417 ip
->i_d
.di_nextents
--;
2419 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2421 rval
= XFS_ILOG_CORE
;
2422 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2423 PREV
.br_startblock
, PREV
.br_blockcount
,
2426 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2427 if ((error
= xfs_btree_delete(cur
, &i
)))
2429 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2430 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2432 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2433 if ((error
= xfs_bmbt_update(cur
, LEFT
.br_startoff
,
2435 LEFT
.br_blockcount
+ PREV
.br_blockcount
,
2441 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2443 * Setting all of a previous oldext extent to newext.
2444 * The right neighbor is contiguous, the left is not.
2446 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2447 xfs_bmbt_set_blockcount(ep
,
2448 PREV
.br_blockcount
+ RIGHT
.br_blockcount
);
2449 xfs_bmbt_set_state(ep
, newext
);
2450 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2451 xfs_iext_remove(ip
, *idx
+ 1, 1, state
);
2452 ip
->i_d
.di_nextents
--;
2454 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2456 rval
= XFS_ILOG_CORE
;
2457 if ((error
= xfs_bmbt_lookup_eq(cur
, RIGHT
.br_startoff
,
2458 RIGHT
.br_startblock
,
2459 RIGHT
.br_blockcount
, &i
)))
2461 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2462 if ((error
= xfs_btree_delete(cur
, &i
)))
2464 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2465 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2467 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2468 if ((error
= xfs_bmbt_update(cur
, new->br_startoff
,
2470 new->br_blockcount
+ RIGHT
.br_blockcount
,
2476 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
2478 * Setting all of a previous oldext extent to newext.
2479 * Neither the left nor right neighbors are contiguous with
2482 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2483 xfs_bmbt_set_state(ep
, newext
);
2484 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2487 rval
= XFS_ILOG_DEXT
;
2490 if ((error
= xfs_bmbt_lookup_eq(cur
, new->br_startoff
,
2491 new->br_startblock
, new->br_blockcount
,
2494 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2495 if ((error
= xfs_bmbt_update(cur
, new->br_startoff
,
2496 new->br_startblock
, new->br_blockcount
,
2502 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
:
2504 * Setting the first part of a previous oldext extent to newext.
2505 * The left neighbor is contiguous.
2507 trace_xfs_bmap_pre_update(ip
, *idx
- 1, state
, _THIS_IP_
);
2508 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
- 1),
2509 LEFT
.br_blockcount
+ new->br_blockcount
);
2510 xfs_bmbt_set_startoff(ep
,
2511 PREV
.br_startoff
+ new->br_blockcount
);
2512 trace_xfs_bmap_post_update(ip
, *idx
- 1, state
, _THIS_IP_
);
2514 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2515 xfs_bmbt_set_startblock(ep
,
2516 new->br_startblock
+ new->br_blockcount
);
2517 xfs_bmbt_set_blockcount(ep
,
2518 PREV
.br_blockcount
- new->br_blockcount
);
2519 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2524 rval
= XFS_ILOG_DEXT
;
2527 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2528 PREV
.br_startblock
, PREV
.br_blockcount
,
2531 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2532 if ((error
= xfs_bmbt_update(cur
,
2533 PREV
.br_startoff
+ new->br_blockcount
,
2534 PREV
.br_startblock
+ new->br_blockcount
,
2535 PREV
.br_blockcount
- new->br_blockcount
,
2538 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2540 error
= xfs_bmbt_update(cur
, LEFT
.br_startoff
,
2542 LEFT
.br_blockcount
+ new->br_blockcount
,
2549 case BMAP_LEFT_FILLING
:
2551 * Setting the first part of a previous oldext extent to newext.
2552 * The left neighbor is not contiguous.
2554 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2555 ASSERT(ep
&& xfs_bmbt_get_state(ep
) == oldext
);
2556 xfs_bmbt_set_startoff(ep
, new_endoff
);
2557 xfs_bmbt_set_blockcount(ep
,
2558 PREV
.br_blockcount
- new->br_blockcount
);
2559 xfs_bmbt_set_startblock(ep
,
2560 new->br_startblock
+ new->br_blockcount
);
2561 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2563 xfs_iext_insert(ip
, *idx
, 1, new, state
);
2564 ip
->i_d
.di_nextents
++;
2566 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2568 rval
= XFS_ILOG_CORE
;
2569 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2570 PREV
.br_startblock
, PREV
.br_blockcount
,
2573 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2574 if ((error
= xfs_bmbt_update(cur
,
2575 PREV
.br_startoff
+ new->br_blockcount
,
2576 PREV
.br_startblock
+ new->br_blockcount
,
2577 PREV
.br_blockcount
- new->br_blockcount
,
2580 cur
->bc_rec
.b
= *new;
2581 if ((error
= xfs_btree_insert(cur
, &i
)))
2583 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2587 case BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2589 * Setting the last part of a previous oldext extent to newext.
2590 * The right neighbor is contiguous with the new allocation.
2592 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2593 xfs_bmbt_set_blockcount(ep
,
2594 PREV
.br_blockcount
- new->br_blockcount
);
2595 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2599 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2600 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp
, *idx
),
2601 new->br_startoff
, new->br_startblock
,
2602 new->br_blockcount
+ RIGHT
.br_blockcount
, newext
);
2603 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2606 rval
= XFS_ILOG_DEXT
;
2609 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2611 PREV
.br_blockcount
, &i
)))
2613 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2614 if ((error
= xfs_bmbt_update(cur
, PREV
.br_startoff
,
2616 PREV
.br_blockcount
- new->br_blockcount
,
2619 if ((error
= xfs_btree_increment(cur
, 0, &i
)))
2621 if ((error
= xfs_bmbt_update(cur
, new->br_startoff
,
2623 new->br_blockcount
+ RIGHT
.br_blockcount
,
2629 case BMAP_RIGHT_FILLING
:
2631 * Setting the last part of a previous oldext extent to newext.
2632 * The right neighbor is not contiguous.
2634 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2635 xfs_bmbt_set_blockcount(ep
,
2636 PREV
.br_blockcount
- new->br_blockcount
);
2637 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2640 xfs_iext_insert(ip
, *idx
, 1, new, state
);
2642 ip
->i_d
.di_nextents
++;
2644 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2646 rval
= XFS_ILOG_CORE
;
2647 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2648 PREV
.br_startblock
, PREV
.br_blockcount
,
2651 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2652 if ((error
= xfs_bmbt_update(cur
, PREV
.br_startoff
,
2654 PREV
.br_blockcount
- new->br_blockcount
,
2657 if ((error
= xfs_bmbt_lookup_eq(cur
, new->br_startoff
,
2658 new->br_startblock
, new->br_blockcount
,
2661 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
2662 cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
2663 if ((error
= xfs_btree_insert(cur
, &i
)))
2665 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2671 * Setting the middle part of a previous oldext extent to
2672 * newext. Contiguity is impossible here.
2673 * One extent becomes three extents.
2675 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2676 xfs_bmbt_set_blockcount(ep
,
2677 new->br_startoff
- PREV
.br_startoff
);
2678 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2681 r
[1].br_startoff
= new_endoff
;
2682 r
[1].br_blockcount
=
2683 PREV
.br_startoff
+ PREV
.br_blockcount
- new_endoff
;
2684 r
[1].br_startblock
= new->br_startblock
+ new->br_blockcount
;
2685 r
[1].br_state
= oldext
;
2688 xfs_iext_insert(ip
, *idx
, 2, &r
[0], state
);
2690 ip
->i_d
.di_nextents
+= 2;
2692 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2694 rval
= XFS_ILOG_CORE
;
2695 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2696 PREV
.br_startblock
, PREV
.br_blockcount
,
2699 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2700 /* new right extent - oldext */
2701 if ((error
= xfs_bmbt_update(cur
, r
[1].br_startoff
,
2702 r
[1].br_startblock
, r
[1].br_blockcount
,
2705 /* new left extent - oldext */
2706 cur
->bc_rec
.b
= PREV
;
2707 cur
->bc_rec
.b
.br_blockcount
=
2708 new->br_startoff
- PREV
.br_startoff
;
2709 if ((error
= xfs_btree_insert(cur
, &i
)))
2711 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2713 * Reset the cursor to the position of the new extent
2714 * we are about to insert as we can't trust it after
2715 * the previous insert.
2717 if ((error
= xfs_bmbt_lookup_eq(cur
, new->br_startoff
,
2718 new->br_startblock
, new->br_blockcount
,
2721 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
2722 /* new middle extent - newext */
2723 cur
->bc_rec
.b
.br_state
= new->br_state
;
2724 if ((error
= xfs_btree_insert(cur
, &i
)))
2726 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2730 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2731 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2732 case BMAP_LEFT_FILLING
| BMAP_RIGHT_CONTIG
:
2733 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
2734 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2735 case BMAP_LEFT_CONTIG
:
2736 case BMAP_RIGHT_CONTIG
:
2738 * These cases are all impossible.
2743 /* convert to a btree if necessary */
2744 if (xfs_bmap_needs_btree(ip
, XFS_DATA_FORK
)) {
2745 int tmp_logflags
; /* partial log flag return val */
2747 ASSERT(cur
== NULL
);
2748 error
= xfs_bmap_extents_to_btree(tp
, ip
, first
, flist
, &cur
,
2749 0, &tmp_logflags
, XFS_DATA_FORK
);
2750 *logflagsp
|= tmp_logflags
;
2755 /* clear out the allocated field, done with it now in any case. */
2757 cur
->bc_private
.b
.allocated
= 0;
2761 xfs_bmap_check_leaf_extents(*curp
, ip
, XFS_DATA_FORK
);
2771 * Convert a hole to a delayed allocation.
2774 xfs_bmap_add_extent_hole_delay(
2775 xfs_inode_t
*ip
, /* incore inode pointer */
2776 xfs_extnum_t
*idx
, /* extent number to update/insert */
2777 xfs_bmbt_irec_t
*new) /* new data to add to file extents */
2779 xfs_ifork_t
*ifp
; /* inode fork pointer */
2780 xfs_bmbt_irec_t left
; /* left neighbor extent entry */
2781 xfs_filblks_t newlen
=0; /* new indirect size */
2782 xfs_filblks_t oldlen
=0; /* old indirect size */
2783 xfs_bmbt_irec_t right
; /* right neighbor extent entry */
2784 int state
; /* state bits, accessed thru macros */
2785 xfs_filblks_t temp
=0; /* temp for indirect calculations */
2787 ifp
= XFS_IFORK_PTR(ip
, XFS_DATA_FORK
);
2789 ASSERT(isnullstartblock(new->br_startblock
));
2792 * Check and set flags if this segment has a left neighbor
2795 state
|= BMAP_LEFT_VALID
;
2796 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, *idx
- 1), &left
);
2798 if (isnullstartblock(left
.br_startblock
))
2799 state
|= BMAP_LEFT_DELAY
;
2803 * Check and set flags if the current (right) segment exists.
2804 * If it doesn't exist, we're converting the hole at end-of-file.
2806 if (*idx
< ip
->i_df
.if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
)) {
2807 state
|= BMAP_RIGHT_VALID
;
2808 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, *idx
), &right
);
2810 if (isnullstartblock(right
.br_startblock
))
2811 state
|= BMAP_RIGHT_DELAY
;
2815 * Set contiguity flags on the left and right neighbors.
2816 * Don't let extents get too large, even if the pieces are contiguous.
2818 if ((state
& BMAP_LEFT_VALID
) && (state
& BMAP_LEFT_DELAY
) &&
2819 left
.br_startoff
+ left
.br_blockcount
== new->br_startoff
&&
2820 left
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
2821 state
|= BMAP_LEFT_CONTIG
;
2823 if ((state
& BMAP_RIGHT_VALID
) && (state
& BMAP_RIGHT_DELAY
) &&
2824 new->br_startoff
+ new->br_blockcount
== right
.br_startoff
&&
2825 new->br_blockcount
+ right
.br_blockcount
<= MAXEXTLEN
&&
2826 (!(state
& BMAP_LEFT_CONTIG
) ||
2827 (left
.br_blockcount
+ new->br_blockcount
+
2828 right
.br_blockcount
<= MAXEXTLEN
)))
2829 state
|= BMAP_RIGHT_CONTIG
;
2832 * Switch out based on the contiguity flags.
2834 switch (state
& (BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
)) {
2835 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2837 * New allocation is contiguous with delayed allocations
2838 * on the left and on the right.
2839 * Merge all three into a single extent record.
2842 temp
= left
.br_blockcount
+ new->br_blockcount
+
2843 right
.br_blockcount
;
2845 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2846 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
), temp
);
2847 oldlen
= startblockval(left
.br_startblock
) +
2848 startblockval(new->br_startblock
) +
2849 startblockval(right
.br_startblock
);
2850 newlen
= xfs_bmap_worst_indlen(ip
, temp
);
2851 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp
, *idx
),
2852 nullstartblock((int)newlen
));
2853 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2855 xfs_iext_remove(ip
, *idx
+ 1, 1, state
);
2858 case BMAP_LEFT_CONTIG
:
2860 * New allocation is contiguous with a delayed allocation
2862 * Merge the new allocation with the left neighbor.
2865 temp
= left
.br_blockcount
+ new->br_blockcount
;
2867 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2868 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
), temp
);
2869 oldlen
= startblockval(left
.br_startblock
) +
2870 startblockval(new->br_startblock
);
2871 newlen
= xfs_bmap_worst_indlen(ip
, temp
);
2872 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp
, *idx
),
2873 nullstartblock((int)newlen
));
2874 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2877 case BMAP_RIGHT_CONTIG
:
2879 * New allocation is contiguous with a delayed allocation
2881 * Merge the new allocation with the right neighbor.
2883 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2884 temp
= new->br_blockcount
+ right
.br_blockcount
;
2885 oldlen
= startblockval(new->br_startblock
) +
2886 startblockval(right
.br_startblock
);
2887 newlen
= xfs_bmap_worst_indlen(ip
, temp
);
2888 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp
, *idx
),
2890 nullstartblock((int)newlen
), temp
, right
.br_state
);
2891 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2896 * New allocation is not contiguous with another
2897 * delayed allocation.
2898 * Insert a new entry.
2900 oldlen
= newlen
= 0;
2901 xfs_iext_insert(ip
, *idx
, 1, new, state
);
2904 if (oldlen
!= newlen
) {
2905 ASSERT(oldlen
> newlen
);
2906 xfs_mod_fdblocks(ip
->i_mount
, (int64_t)(oldlen
- newlen
),
2909 * Nothing to do for disk quota accounting here.
2915 * Convert a hole to a real allocation.
2917 STATIC
int /* error */
2918 xfs_bmap_add_extent_hole_real(
2919 struct xfs_bmalloca
*bma
,
2922 struct xfs_bmbt_irec
*new = &bma
->got
;
2923 int error
; /* error return value */
2924 int i
; /* temp state */
2925 xfs_ifork_t
*ifp
; /* inode fork pointer */
2926 xfs_bmbt_irec_t left
; /* left neighbor extent entry */
2927 xfs_bmbt_irec_t right
; /* right neighbor extent entry */
2928 int rval
=0; /* return value (logging flags) */
2929 int state
; /* state bits, accessed thru macros */
2930 struct xfs_mount
*mp
;
2932 mp
= bma
->tp
? bma
->tp
->t_mountp
: NULL
;
2933 ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
2935 ASSERT(bma
->idx
>= 0);
2936 ASSERT(bma
->idx
<= ifp
->if_bytes
/ sizeof(struct xfs_bmbt_rec
));
2937 ASSERT(!isnullstartblock(new->br_startblock
));
2939 !(bma
->cur
->bc_private
.b
.flags
& XFS_BTCUR_BPRV_WASDEL
));
2941 XFS_STATS_INC(xs_add_exlist
);
2944 if (whichfork
== XFS_ATTR_FORK
)
2945 state
|= BMAP_ATTRFORK
;
2948 * Check and set flags if this segment has a left neighbor.
2951 state
|= BMAP_LEFT_VALID
;
2952 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
- 1), &left
);
2953 if (isnullstartblock(left
.br_startblock
))
2954 state
|= BMAP_LEFT_DELAY
;
2958 * Check and set flags if this segment has a current value.
2959 * Not true if we're inserting into the "hole" at eof.
2961 if (bma
->idx
< ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
)) {
2962 state
|= BMAP_RIGHT_VALID
;
2963 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
), &right
);
2964 if (isnullstartblock(right
.br_startblock
))
2965 state
|= BMAP_RIGHT_DELAY
;
2969 * We're inserting a real allocation between "left" and "right".
2970 * Set the contiguity flags. Don't let extents get too large.
2972 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
2973 left
.br_startoff
+ left
.br_blockcount
== new->br_startoff
&&
2974 left
.br_startblock
+ left
.br_blockcount
== new->br_startblock
&&
2975 left
.br_state
== new->br_state
&&
2976 left
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
2977 state
|= BMAP_LEFT_CONTIG
;
2979 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
2980 new->br_startoff
+ new->br_blockcount
== right
.br_startoff
&&
2981 new->br_startblock
+ new->br_blockcount
== right
.br_startblock
&&
2982 new->br_state
== right
.br_state
&&
2983 new->br_blockcount
+ right
.br_blockcount
<= MAXEXTLEN
&&
2984 (!(state
& BMAP_LEFT_CONTIG
) ||
2985 left
.br_blockcount
+ new->br_blockcount
+
2986 right
.br_blockcount
<= MAXEXTLEN
))
2987 state
|= BMAP_RIGHT_CONTIG
;
2991 * Select which case we're in here, and implement it.
2993 switch (state
& (BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
)) {
2994 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2996 * New allocation is contiguous with real allocations on the
2997 * left and on the right.
2998 * Merge all three into a single extent record.
3001 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
3002 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, bma
->idx
),
3003 left
.br_blockcount
+ new->br_blockcount
+
3004 right
.br_blockcount
);
3005 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
3007 xfs_iext_remove(bma
->ip
, bma
->idx
+ 1, 1, state
);
3009 XFS_IFORK_NEXT_SET(bma
->ip
, whichfork
,
3010 XFS_IFORK_NEXTENTS(bma
->ip
, whichfork
) - 1);
3011 if (bma
->cur
== NULL
) {
3012 rval
= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
3014 rval
= XFS_ILOG_CORE
;
3015 error
= xfs_bmbt_lookup_eq(bma
->cur
, right
.br_startoff
,
3016 right
.br_startblock
, right
.br_blockcount
,
3020 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
3021 error
= xfs_btree_delete(bma
->cur
, &i
);
3024 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
3025 error
= xfs_btree_decrement(bma
->cur
, 0, &i
);
3028 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
3029 error
= xfs_bmbt_update(bma
->cur
, left
.br_startoff
,
3031 left
.br_blockcount
+
3032 new->br_blockcount
+
3033 right
.br_blockcount
,
3040 case BMAP_LEFT_CONTIG
:
3042 * New allocation is contiguous with a real allocation
3044 * Merge the new allocation with the left neighbor.
3047 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
3048 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, bma
->idx
),
3049 left
.br_blockcount
+ new->br_blockcount
);
3050 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
3052 if (bma
->cur
== NULL
) {
3053 rval
= xfs_ilog_fext(whichfork
);
3056 error
= xfs_bmbt_lookup_eq(bma
->cur
, left
.br_startoff
,
3057 left
.br_startblock
, left
.br_blockcount
,
3061 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
3062 error
= xfs_bmbt_update(bma
->cur
, left
.br_startoff
,
3064 left
.br_blockcount
+
3072 case BMAP_RIGHT_CONTIG
:
3074 * New allocation is contiguous with a real allocation
3076 * Merge the new allocation with the right neighbor.
3078 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
3079 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp
, bma
->idx
),
3080 new->br_startoff
, new->br_startblock
,
3081 new->br_blockcount
+ right
.br_blockcount
,
3083 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
3085 if (bma
->cur
== NULL
) {
3086 rval
= xfs_ilog_fext(whichfork
);
3089 error
= xfs_bmbt_lookup_eq(bma
->cur
,
3091 right
.br_startblock
,
3092 right
.br_blockcount
, &i
);
3095 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
3096 error
= xfs_bmbt_update(bma
->cur
, new->br_startoff
,
3098 new->br_blockcount
+
3099 right
.br_blockcount
,
3108 * New allocation is not contiguous with another
3110 * Insert a new entry.
3112 xfs_iext_insert(bma
->ip
, bma
->idx
, 1, new, state
);
3113 XFS_IFORK_NEXT_SET(bma
->ip
, whichfork
,
3114 XFS_IFORK_NEXTENTS(bma
->ip
, whichfork
) + 1);
3115 if (bma
->cur
== NULL
) {
3116 rval
= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
3118 rval
= XFS_ILOG_CORE
;
3119 error
= xfs_bmbt_lookup_eq(bma
->cur
,
3122 new->br_blockcount
, &i
);
3125 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
3126 bma
->cur
->bc_rec
.b
.br_state
= new->br_state
;
3127 error
= xfs_btree_insert(bma
->cur
, &i
);
3130 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
3135 /* convert to a btree if necessary */
3136 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
3137 int tmp_logflags
; /* partial log flag return val */
3139 ASSERT(bma
->cur
== NULL
);
3140 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
3141 bma
->firstblock
, bma
->flist
, &bma
->cur
,
3142 0, &tmp_logflags
, whichfork
);
3143 bma
->logflags
|= tmp_logflags
;
3148 /* clear out the allocated field, done with it now in any case. */
3150 bma
->cur
->bc_private
.b
.allocated
= 0;
3152 xfs_bmap_check_leaf_extents(bma
->cur
, bma
->ip
, whichfork
);
3154 bma
->logflags
|= rval
;
3159 * Functions used in the extent read, allocate and remove paths
3163 * Adjust the size of the new extent based on di_extsize and rt extsize.
3166 xfs_bmap_extsize_align(
3168 xfs_bmbt_irec_t
*gotp
, /* next extent pointer */
3169 xfs_bmbt_irec_t
*prevp
, /* previous extent pointer */
3170 xfs_extlen_t extsz
, /* align to this extent size */
3171 int rt
, /* is this a realtime inode? */
3172 int eof
, /* is extent at end-of-file? */
3173 int delay
, /* creating delalloc extent? */
3174 int convert
, /* overwriting unwritten extent? */
3175 xfs_fileoff_t
*offp
, /* in/out: aligned offset */
3176 xfs_extlen_t
*lenp
) /* in/out: aligned length */
3178 xfs_fileoff_t orig_off
; /* original offset */
3179 xfs_extlen_t orig_alen
; /* original length */
3180 xfs_fileoff_t orig_end
; /* original off+len */
3181 xfs_fileoff_t nexto
; /* next file offset */
3182 xfs_fileoff_t prevo
; /* previous file offset */
3183 xfs_fileoff_t align_off
; /* temp for offset */
3184 xfs_extlen_t align_alen
; /* temp for length */
3185 xfs_extlen_t temp
; /* temp for calculations */
3190 orig_off
= align_off
= *offp
;
3191 orig_alen
= align_alen
= *lenp
;
3192 orig_end
= orig_off
+ orig_alen
;
3195 * If this request overlaps an existing extent, then don't
3196 * attempt to perform any additional alignment.
3198 if (!delay
&& !eof
&&
3199 (orig_off
>= gotp
->br_startoff
) &&
3200 (orig_end
<= gotp
->br_startoff
+ gotp
->br_blockcount
)) {
3205 * If the file offset is unaligned vs. the extent size
3206 * we need to align it. This will be possible unless
3207 * the file was previously written with a kernel that didn't
3208 * perform this alignment, or if a truncate shot us in the
3211 temp
= do_mod(orig_off
, extsz
);
3217 * Same adjustment for the end of the requested area.
3219 if ((temp
= (align_alen
% extsz
))) {
3220 align_alen
+= extsz
- temp
;
3223 * If the previous block overlaps with this proposed allocation
3224 * then move the start forward without adjusting the length.
3226 if (prevp
->br_startoff
!= NULLFILEOFF
) {
3227 if (prevp
->br_startblock
== HOLESTARTBLOCK
)
3228 prevo
= prevp
->br_startoff
;
3230 prevo
= prevp
->br_startoff
+ prevp
->br_blockcount
;
3233 if (align_off
!= orig_off
&& align_off
< prevo
)
3236 * If the next block overlaps with this proposed allocation
3237 * then move the start back without adjusting the length,
3238 * but not before offset 0.
3239 * This may of course make the start overlap previous block,
3240 * and if we hit the offset 0 limit then the next block
3241 * can still overlap too.
3243 if (!eof
&& gotp
->br_startoff
!= NULLFILEOFF
) {
3244 if ((delay
&& gotp
->br_startblock
== HOLESTARTBLOCK
) ||
3245 (!delay
&& gotp
->br_startblock
== DELAYSTARTBLOCK
))
3246 nexto
= gotp
->br_startoff
+ gotp
->br_blockcount
;
3248 nexto
= gotp
->br_startoff
;
3250 nexto
= NULLFILEOFF
;
3252 align_off
+ align_alen
!= orig_end
&&
3253 align_off
+ align_alen
> nexto
)
3254 align_off
= nexto
> align_alen
? nexto
- align_alen
: 0;
3256 * If we're now overlapping the next or previous extent that
3257 * means we can't fit an extsz piece in this hole. Just move
3258 * the start forward to the first valid spot and set
3259 * the length so we hit the end.
3261 if (align_off
!= orig_off
&& align_off
< prevo
)
3263 if (align_off
+ align_alen
!= orig_end
&&
3264 align_off
+ align_alen
> nexto
&&
3265 nexto
!= NULLFILEOFF
) {
3266 ASSERT(nexto
> prevo
);
3267 align_alen
= nexto
- align_off
;
3271 * If realtime, and the result isn't a multiple of the realtime
3272 * extent size we need to remove blocks until it is.
3274 if (rt
&& (temp
= (align_alen
% mp
->m_sb
.sb_rextsize
))) {
3276 * We're not covering the original request, or
3277 * we won't be able to once we fix the length.
3279 if (orig_off
< align_off
||
3280 orig_end
> align_off
+ align_alen
||
3281 align_alen
- temp
< orig_alen
)
3284 * Try to fix it by moving the start up.
3286 if (align_off
+ temp
<= orig_off
) {
3291 * Try to fix it by moving the end in.
3293 else if (align_off
+ align_alen
- temp
>= orig_end
)
3296 * Set the start to the minimum then trim the length.
3299 align_alen
-= orig_off
- align_off
;
3300 align_off
= orig_off
;
3301 align_alen
-= align_alen
% mp
->m_sb
.sb_rextsize
;
3304 * Result doesn't cover the request, fail it.
3306 if (orig_off
< align_off
|| orig_end
> align_off
+ align_alen
)
3309 ASSERT(orig_off
>= align_off
);
3310 ASSERT(orig_end
<= align_off
+ align_alen
);
3314 if (!eof
&& gotp
->br_startoff
!= NULLFILEOFF
)
3315 ASSERT(align_off
+ align_alen
<= gotp
->br_startoff
);
3316 if (prevp
->br_startoff
!= NULLFILEOFF
)
3317 ASSERT(align_off
>= prevp
->br_startoff
+ prevp
->br_blockcount
);
3325 #define XFS_ALLOC_GAP_UNITS 4
3329 struct xfs_bmalloca
*ap
) /* bmap alloc argument struct */
3331 xfs_fsblock_t adjust
; /* adjustment to block numbers */
3332 xfs_agnumber_t fb_agno
; /* ag number of ap->firstblock */
3333 xfs_mount_t
*mp
; /* mount point structure */
3334 int nullfb
; /* true if ap->firstblock isn't set */
3335 int rt
; /* true if inode is realtime */
3337 #define ISVALID(x,y) \
3339 (x) < mp->m_sb.sb_rblocks : \
3340 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
3341 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
3342 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
3344 mp
= ap
->ip
->i_mount
;
3345 nullfb
= *ap
->firstblock
== NULLFSBLOCK
;
3346 rt
= XFS_IS_REALTIME_INODE(ap
->ip
) && ap
->userdata
;
3347 fb_agno
= nullfb
? NULLAGNUMBER
: XFS_FSB_TO_AGNO(mp
, *ap
->firstblock
);
3349 * If allocating at eof, and there's a previous real block,
3350 * try to use its last block as our starting point.
3352 if (ap
->eof
&& ap
->prev
.br_startoff
!= NULLFILEOFF
&&
3353 !isnullstartblock(ap
->prev
.br_startblock
) &&
3354 ISVALID(ap
->prev
.br_startblock
+ ap
->prev
.br_blockcount
,
3355 ap
->prev
.br_startblock
)) {
3356 ap
->blkno
= ap
->prev
.br_startblock
+ ap
->prev
.br_blockcount
;
3358 * Adjust for the gap between prevp and us.
3360 adjust
= ap
->offset
-
3361 (ap
->prev
.br_startoff
+ ap
->prev
.br_blockcount
);
3363 ISVALID(ap
->blkno
+ adjust
, ap
->prev
.br_startblock
))
3364 ap
->blkno
+= adjust
;
3367 * If not at eof, then compare the two neighbor blocks.
3368 * Figure out whether either one gives us a good starting point,
3369 * and pick the better one.
3371 else if (!ap
->eof
) {
3372 xfs_fsblock_t gotbno
; /* right side block number */
3373 xfs_fsblock_t gotdiff
=0; /* right side difference */
3374 xfs_fsblock_t prevbno
; /* left side block number */
3375 xfs_fsblock_t prevdiff
=0; /* left side difference */
3378 * If there's a previous (left) block, select a requested
3379 * start block based on it.
3381 if (ap
->prev
.br_startoff
!= NULLFILEOFF
&&
3382 !isnullstartblock(ap
->prev
.br_startblock
) &&
3383 (prevbno
= ap
->prev
.br_startblock
+
3384 ap
->prev
.br_blockcount
) &&
3385 ISVALID(prevbno
, ap
->prev
.br_startblock
)) {
3387 * Calculate gap to end of previous block.
3389 adjust
= prevdiff
= ap
->offset
-
3390 (ap
->prev
.br_startoff
+
3391 ap
->prev
.br_blockcount
);
3393 * Figure the startblock based on the previous block's
3394 * end and the gap size.
3396 * If the gap is large relative to the piece we're
3397 * allocating, or using it gives us an invalid block
3398 * number, then just use the end of the previous block.
3400 if (prevdiff
<= XFS_ALLOC_GAP_UNITS
* ap
->length
&&
3401 ISVALID(prevbno
+ prevdiff
,
3402 ap
->prev
.br_startblock
))
3407 * If the firstblock forbids it, can't use it,
3410 if (!rt
&& !nullfb
&&
3411 XFS_FSB_TO_AGNO(mp
, prevbno
) != fb_agno
)
3412 prevbno
= NULLFSBLOCK
;
3415 * No previous block or can't follow it, just default.
3418 prevbno
= NULLFSBLOCK
;
3420 * If there's a following (right) block, select a requested
3421 * start block based on it.
3423 if (!isnullstartblock(ap
->got
.br_startblock
)) {
3425 * Calculate gap to start of next block.
3427 adjust
= gotdiff
= ap
->got
.br_startoff
- ap
->offset
;
3429 * Figure the startblock based on the next block's
3430 * start and the gap size.
3432 gotbno
= ap
->got
.br_startblock
;
3435 * If the gap is large relative to the piece we're
3436 * allocating, or using it gives us an invalid block
3437 * number, then just use the start of the next block
3438 * offset by our length.
3440 if (gotdiff
<= XFS_ALLOC_GAP_UNITS
* ap
->length
&&
3441 ISVALID(gotbno
- gotdiff
, gotbno
))
3443 else if (ISVALID(gotbno
- ap
->length
, gotbno
)) {
3444 gotbno
-= ap
->length
;
3445 gotdiff
+= adjust
- ap
->length
;
3449 * If the firstblock forbids it, can't use it,
3452 if (!rt
&& !nullfb
&&
3453 XFS_FSB_TO_AGNO(mp
, gotbno
) != fb_agno
)
3454 gotbno
= NULLFSBLOCK
;
3457 * No next block, just default.
3460 gotbno
= NULLFSBLOCK
;
3462 * If both valid, pick the better one, else the only good
3463 * one, else ap->blkno is already set (to 0 or the inode block).
3465 if (prevbno
!= NULLFSBLOCK
&& gotbno
!= NULLFSBLOCK
)
3466 ap
->blkno
= prevdiff
<= gotdiff
? prevbno
: gotbno
;
3467 else if (prevbno
!= NULLFSBLOCK
)
3468 ap
->blkno
= prevbno
;
3469 else if (gotbno
!= NULLFSBLOCK
)
3476 xfs_bmap_longest_free_extent(
3477 struct xfs_trans
*tp
,
3482 struct xfs_mount
*mp
= tp
->t_mountp
;
3483 struct xfs_perag
*pag
;
3484 xfs_extlen_t longest
;
3487 pag
= xfs_perag_get(mp
, ag
);
3488 if (!pag
->pagf_init
) {
3489 error
= xfs_alloc_pagf_init(mp
, tp
, ag
, XFS_ALLOC_FLAG_TRYLOCK
);
3493 if (!pag
->pagf_init
) {
3499 longest
= xfs_alloc_longest_free_extent(mp
, pag
);
3500 if (*blen
< longest
)
3509 xfs_bmap_select_minlen(
3510 struct xfs_bmalloca
*ap
,
3511 struct xfs_alloc_arg
*args
,
3515 if (notinit
|| *blen
< ap
->minlen
) {
3517 * Since we did a BUF_TRYLOCK above, it is possible that
3518 * there is space for this request.
3520 args
->minlen
= ap
->minlen
;
3521 } else if (*blen
< args
->maxlen
) {
3523 * If the best seen length is less than the request length,
3524 * use the best as the minimum.
3526 args
->minlen
= *blen
;
3529 * Otherwise we've seen an extent as big as maxlen, use that
3532 args
->minlen
= args
->maxlen
;
3537 xfs_bmap_btalloc_nullfb(
3538 struct xfs_bmalloca
*ap
,
3539 struct xfs_alloc_arg
*args
,
3542 struct xfs_mount
*mp
= ap
->ip
->i_mount
;
3543 xfs_agnumber_t ag
, startag
;
3547 args
->type
= XFS_ALLOCTYPE_START_BNO
;
3548 args
->total
= ap
->total
;
3550 startag
= ag
= XFS_FSB_TO_AGNO(mp
, args
->fsbno
);
3551 if (startag
== NULLAGNUMBER
)
3554 while (*blen
< args
->maxlen
) {
3555 error
= xfs_bmap_longest_free_extent(args
->tp
, ag
, blen
,
3560 if (++ag
== mp
->m_sb
.sb_agcount
)
3566 xfs_bmap_select_minlen(ap
, args
, blen
, notinit
);
3571 xfs_bmap_btalloc_filestreams(
3572 struct xfs_bmalloca
*ap
,
3573 struct xfs_alloc_arg
*args
,
3576 struct xfs_mount
*mp
= ap
->ip
->i_mount
;
3581 args
->type
= XFS_ALLOCTYPE_NEAR_BNO
;
3582 args
->total
= ap
->total
;
3584 ag
= XFS_FSB_TO_AGNO(mp
, args
->fsbno
);
3585 if (ag
== NULLAGNUMBER
)
3588 error
= xfs_bmap_longest_free_extent(args
->tp
, ag
, blen
, ¬init
);
3592 if (*blen
< args
->maxlen
) {
3593 error
= xfs_filestream_new_ag(ap
, &ag
);
3597 error
= xfs_bmap_longest_free_extent(args
->tp
, ag
, blen
,
3604 xfs_bmap_select_minlen(ap
, args
, blen
, notinit
);
3607 * Set the failure fallback case to look in the selected AG as stream
3610 ap
->blkno
= args
->fsbno
= XFS_AGB_TO_FSB(mp
, ag
, 0);
3616 struct xfs_bmalloca
*ap
) /* bmap alloc argument struct */
3618 xfs_mount_t
*mp
; /* mount point structure */
3619 xfs_alloctype_t atype
= 0; /* type for allocation routines */
3620 xfs_extlen_t align
; /* minimum allocation alignment */
3621 xfs_agnumber_t fb_agno
; /* ag number of ap->firstblock */
3623 xfs_alloc_arg_t args
;
3625 xfs_extlen_t nextminlen
= 0;
3626 int nullfb
; /* true if ap->firstblock isn't set */
3634 mp
= ap
->ip
->i_mount
;
3636 /* stripe alignment for allocation is determined by mount parameters */
3638 if (mp
->m_swidth
&& (mp
->m_flags
& XFS_MOUNT_SWALLOC
))
3639 stripe_align
= mp
->m_swidth
;
3640 else if (mp
->m_dalign
)
3641 stripe_align
= mp
->m_dalign
;
3643 align
= ap
->userdata
? xfs_get_extsz_hint(ap
->ip
) : 0;
3644 if (unlikely(align
)) {
3645 error
= xfs_bmap_extsize_align(mp
, &ap
->got
, &ap
->prev
,
3646 align
, 0, ap
->eof
, 0, ap
->conv
,
3647 &ap
->offset
, &ap
->length
);
3653 nullfb
= *ap
->firstblock
== NULLFSBLOCK
;
3654 fb_agno
= nullfb
? NULLAGNUMBER
: XFS_FSB_TO_AGNO(mp
, *ap
->firstblock
);
3656 if (ap
->userdata
&& xfs_inode_is_filestream(ap
->ip
)) {
3657 ag
= xfs_filestream_lookup_ag(ap
->ip
);
3658 ag
= (ag
!= NULLAGNUMBER
) ? ag
: 0;
3659 ap
->blkno
= XFS_AGB_TO_FSB(mp
, ag
, 0);
3661 ap
->blkno
= XFS_INO_TO_FSB(mp
, ap
->ip
->i_ino
);
3664 ap
->blkno
= *ap
->firstblock
;
3666 xfs_bmap_adjacent(ap
);
3669 * If allowed, use ap->blkno; otherwise must use firstblock since
3670 * it's in the right allocation group.
3672 if (nullfb
|| XFS_FSB_TO_AGNO(mp
, ap
->blkno
) == fb_agno
)
3675 ap
->blkno
= *ap
->firstblock
;
3677 * Normal allocation, done through xfs_alloc_vextent.
3679 tryagain
= isaligned
= 0;
3680 memset(&args
, 0, sizeof(args
));
3683 args
.fsbno
= ap
->blkno
;
3685 /* Trim the allocation back to the maximum an AG can fit. */
3686 args
.maxlen
= MIN(ap
->length
, XFS_ALLOC_AG_MAX_USABLE(mp
));
3687 args
.firstblock
= *ap
->firstblock
;
3691 * Search for an allocation group with a single extent large
3692 * enough for the request. If one isn't found, then adjust
3693 * the minimum allocation size to the largest space found.
3695 if (ap
->userdata
&& xfs_inode_is_filestream(ap
->ip
))
3696 error
= xfs_bmap_btalloc_filestreams(ap
, &args
, &blen
);
3698 error
= xfs_bmap_btalloc_nullfb(ap
, &args
, &blen
);
3701 } else if (ap
->flist
->xbf_low
) {
3702 if (xfs_inode_is_filestream(ap
->ip
))
3703 args
.type
= XFS_ALLOCTYPE_FIRST_AG
;
3705 args
.type
= XFS_ALLOCTYPE_START_BNO
;
3706 args
.total
= args
.minlen
= ap
->minlen
;
3708 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
3709 args
.total
= ap
->total
;
3710 args
.minlen
= ap
->minlen
;
3712 /* apply extent size hints if obtained earlier */
3713 if (unlikely(align
)) {
3715 if ((args
.mod
= (xfs_extlen_t
)do_mod(ap
->offset
, args
.prod
)))
3716 args
.mod
= (xfs_extlen_t
)(args
.prod
- args
.mod
);
3717 } else if (mp
->m_sb
.sb_blocksize
>= PAGE_CACHE_SIZE
) {
3721 args
.prod
= PAGE_CACHE_SIZE
>> mp
->m_sb
.sb_blocklog
;
3722 if ((args
.mod
= (xfs_extlen_t
)(do_mod(ap
->offset
, args
.prod
))))
3723 args
.mod
= (xfs_extlen_t
)(args
.prod
- args
.mod
);
3726 * If we are not low on available data blocks, and the
3727 * underlying logical volume manager is a stripe, and
3728 * the file offset is zero then try to allocate data
3729 * blocks on stripe unit boundary.
3730 * NOTE: ap->aeof is only set if the allocation length
3731 * is >= the stripe unit and the allocation offset is
3732 * at the end of file.
3734 if (!ap
->flist
->xbf_low
&& ap
->aeof
) {
3736 args
.alignment
= stripe_align
;
3740 * Adjust for alignment
3742 if (blen
> args
.alignment
&& blen
<= args
.maxlen
)
3743 args
.minlen
= blen
- args
.alignment
;
3744 args
.minalignslop
= 0;
3747 * First try an exact bno allocation.
3748 * If it fails then do a near or start bno
3749 * allocation with alignment turned on.
3753 args
.type
= XFS_ALLOCTYPE_THIS_BNO
;
3756 * Compute the minlen+alignment for the
3757 * next case. Set slop so that the value
3758 * of minlen+alignment+slop doesn't go up
3759 * between the calls.
3761 if (blen
> stripe_align
&& blen
<= args
.maxlen
)
3762 nextminlen
= blen
- stripe_align
;
3764 nextminlen
= args
.minlen
;
3765 if (nextminlen
+ stripe_align
> args
.minlen
+ 1)
3767 nextminlen
+ stripe_align
-
3770 args
.minalignslop
= 0;
3774 args
.minalignslop
= 0;
3776 args
.minleft
= ap
->minleft
;
3777 args
.wasdel
= ap
->wasdel
;
3779 args
.userdata
= ap
->userdata
;
3780 if ((error
= xfs_alloc_vextent(&args
)))
3782 if (tryagain
&& args
.fsbno
== NULLFSBLOCK
) {
3784 * Exact allocation failed. Now try with alignment
3788 args
.fsbno
= ap
->blkno
;
3789 args
.alignment
= stripe_align
;
3790 args
.minlen
= nextminlen
;
3791 args
.minalignslop
= 0;
3793 if ((error
= xfs_alloc_vextent(&args
)))
3796 if (isaligned
&& args
.fsbno
== NULLFSBLOCK
) {
3798 * allocation failed, so turn off alignment and
3802 args
.fsbno
= ap
->blkno
;
3804 if ((error
= xfs_alloc_vextent(&args
)))
3807 if (args
.fsbno
== NULLFSBLOCK
&& nullfb
&&
3808 args
.minlen
> ap
->minlen
) {
3809 args
.minlen
= ap
->minlen
;
3810 args
.type
= XFS_ALLOCTYPE_START_BNO
;
3811 args
.fsbno
= ap
->blkno
;
3812 if ((error
= xfs_alloc_vextent(&args
)))
3815 if (args
.fsbno
== NULLFSBLOCK
&& nullfb
) {
3817 args
.type
= XFS_ALLOCTYPE_FIRST_AG
;
3818 args
.total
= ap
->minlen
;
3820 if ((error
= xfs_alloc_vextent(&args
)))
3822 ap
->flist
->xbf_low
= 1;
3824 if (args
.fsbno
!= NULLFSBLOCK
) {
3826 * check the allocation happened at the same or higher AG than
3827 * the first block that was allocated.
3829 ASSERT(*ap
->firstblock
== NULLFSBLOCK
||
3830 XFS_FSB_TO_AGNO(mp
, *ap
->firstblock
) ==
3831 XFS_FSB_TO_AGNO(mp
, args
.fsbno
) ||
3832 (ap
->flist
->xbf_low
&&
3833 XFS_FSB_TO_AGNO(mp
, *ap
->firstblock
) <
3834 XFS_FSB_TO_AGNO(mp
, args
.fsbno
)));
3836 ap
->blkno
= args
.fsbno
;
3837 if (*ap
->firstblock
== NULLFSBLOCK
)
3838 *ap
->firstblock
= args
.fsbno
;
3839 ASSERT(nullfb
|| fb_agno
== args
.agno
||
3840 (ap
->flist
->xbf_low
&& fb_agno
< args
.agno
));
3841 ap
->length
= args
.len
;
3842 ap
->ip
->i_d
.di_nblocks
+= args
.len
;
3843 xfs_trans_log_inode(ap
->tp
, ap
->ip
, XFS_ILOG_CORE
);
3845 ap
->ip
->i_delayed_blks
-= args
.len
;
3847 * Adjust the disk quota also. This was reserved
3850 xfs_trans_mod_dquot_byino(ap
->tp
, ap
->ip
,
3851 ap
->wasdel
? XFS_TRANS_DQ_DELBCOUNT
:
3852 XFS_TRANS_DQ_BCOUNT
,
3855 ap
->blkno
= NULLFSBLOCK
;
3862 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
3863 * It figures out where to ask the underlying allocator to put the new extent.
3867 struct xfs_bmalloca
*ap
) /* bmap alloc argument struct */
3869 if (XFS_IS_REALTIME_INODE(ap
->ip
) && ap
->userdata
)
3870 return xfs_bmap_rtalloc(ap
);
3871 return xfs_bmap_btalloc(ap
);
3875 * Trim the returned map to the required bounds
3879 struct xfs_bmbt_irec
*mval
,
3880 struct xfs_bmbt_irec
*got
,
3888 if ((flags
& XFS_BMAPI_ENTIRE
) ||
3889 got
->br_startoff
+ got
->br_blockcount
<= obno
) {
3891 if (isnullstartblock(got
->br_startblock
))
3892 mval
->br_startblock
= DELAYSTARTBLOCK
;
3898 ASSERT((*bno
>= obno
) || (n
== 0));
3900 mval
->br_startoff
= *bno
;
3901 if (isnullstartblock(got
->br_startblock
))
3902 mval
->br_startblock
= DELAYSTARTBLOCK
;
3904 mval
->br_startblock
= got
->br_startblock
+
3905 (*bno
- got
->br_startoff
);
3907 * Return the minimum of what we got and what we asked for for
3908 * the length. We can use the len variable here because it is
3909 * modified below and we could have been there before coming
3910 * here if the first part of the allocation didn't overlap what
3913 mval
->br_blockcount
= XFS_FILBLKS_MIN(end
- *bno
,
3914 got
->br_blockcount
- (*bno
- got
->br_startoff
));
3915 mval
->br_state
= got
->br_state
;
3916 ASSERT(mval
->br_blockcount
<= len
);
3921 * Update and validate the extent map to return
3924 xfs_bmapi_update_map(
3925 struct xfs_bmbt_irec
**map
,
3933 xfs_bmbt_irec_t
*mval
= *map
;
3935 ASSERT((flags
& XFS_BMAPI_ENTIRE
) ||
3936 ((mval
->br_startoff
+ mval
->br_blockcount
) <= end
));
3937 ASSERT((flags
& XFS_BMAPI_ENTIRE
) || (mval
->br_blockcount
<= *len
) ||
3938 (mval
->br_startoff
< obno
));
3940 *bno
= mval
->br_startoff
+ mval
->br_blockcount
;
3942 if (*n
> 0 && mval
->br_startoff
== mval
[-1].br_startoff
) {
3943 /* update previous map with new information */
3944 ASSERT(mval
->br_startblock
== mval
[-1].br_startblock
);
3945 ASSERT(mval
->br_blockcount
> mval
[-1].br_blockcount
);
3946 ASSERT(mval
->br_state
== mval
[-1].br_state
);
3947 mval
[-1].br_blockcount
= mval
->br_blockcount
;
3948 mval
[-1].br_state
= mval
->br_state
;
3949 } else if (*n
> 0 && mval
->br_startblock
!= DELAYSTARTBLOCK
&&
3950 mval
[-1].br_startblock
!= DELAYSTARTBLOCK
&&
3951 mval
[-1].br_startblock
!= HOLESTARTBLOCK
&&
3952 mval
->br_startblock
== mval
[-1].br_startblock
+
3953 mval
[-1].br_blockcount
&&
3954 ((flags
& XFS_BMAPI_IGSTATE
) ||
3955 mval
[-1].br_state
== mval
->br_state
)) {
3956 ASSERT(mval
->br_startoff
==
3957 mval
[-1].br_startoff
+ mval
[-1].br_blockcount
);
3958 mval
[-1].br_blockcount
+= mval
->br_blockcount
;
3959 } else if (*n
> 0 &&
3960 mval
->br_startblock
== DELAYSTARTBLOCK
&&
3961 mval
[-1].br_startblock
== DELAYSTARTBLOCK
&&
3962 mval
->br_startoff
==
3963 mval
[-1].br_startoff
+ mval
[-1].br_blockcount
) {
3964 mval
[-1].br_blockcount
+= mval
->br_blockcount
;
3965 mval
[-1].br_state
= mval
->br_state
;
3966 } else if (!((*n
== 0) &&
3967 ((mval
->br_startoff
+ mval
->br_blockcount
) <=
3976 * Map file blocks to filesystem blocks without allocation.
3980 struct xfs_inode
*ip
,
3983 struct xfs_bmbt_irec
*mval
,
3987 struct xfs_mount
*mp
= ip
->i_mount
;
3988 struct xfs_ifork
*ifp
;
3989 struct xfs_bmbt_irec got
;
3990 struct xfs_bmbt_irec prev
;
3997 int whichfork
= (flags
& XFS_BMAPI_ATTRFORK
) ?
3998 XFS_ATTR_FORK
: XFS_DATA_FORK
;
4001 ASSERT(!(flags
& ~(XFS_BMAPI_ATTRFORK
|XFS_BMAPI_ENTIRE
|
4002 XFS_BMAPI_IGSTATE
)));
4003 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_SHARED
|XFS_ILOCK_EXCL
));
4005 if (unlikely(XFS_TEST_ERROR(
4006 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
4007 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
4008 mp
, XFS_ERRTAG_BMAPIFORMAT
, XFS_RANDOM_BMAPIFORMAT
))) {
4009 XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW
, mp
);
4010 return -EFSCORRUPTED
;
4013 if (XFS_FORCED_SHUTDOWN(mp
))
4016 XFS_STATS_INC(xs_blk_mapr
);
4018 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4020 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
4021 error
= xfs_iread_extents(NULL
, ip
, whichfork
);
4026 xfs_bmap_search_extents(ip
, bno
, whichfork
, &eof
, &lastx
, &got
, &prev
);
4030 while (bno
< end
&& n
< *nmap
) {
4031 /* Reading past eof, act as though there's a hole up to end. */
4033 got
.br_startoff
= end
;
4034 if (got
.br_startoff
> bno
) {
4035 /* Reading in a hole. */
4036 mval
->br_startoff
= bno
;
4037 mval
->br_startblock
= HOLESTARTBLOCK
;
4038 mval
->br_blockcount
=
4039 XFS_FILBLKS_MIN(len
, got
.br_startoff
- bno
);
4040 mval
->br_state
= XFS_EXT_NORM
;
4041 bno
+= mval
->br_blockcount
;
4042 len
-= mval
->br_blockcount
;
4048 /* set up the extent map to return. */
4049 xfs_bmapi_trim_map(mval
, &got
, &bno
, len
, obno
, end
, n
, flags
);
4050 xfs_bmapi_update_map(&mval
, &bno
, &len
, obno
, end
, &n
, flags
);
4052 /* If we're done, stop now. */
4053 if (bno
>= end
|| n
>= *nmap
)
4056 /* Else go on to the next record. */
4057 if (++lastx
< ifp
->if_bytes
/ sizeof(xfs_bmbt_rec_t
))
4058 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, lastx
), &got
);
4067 xfs_bmapi_reserve_delalloc(
4068 struct xfs_inode
*ip
,
4071 struct xfs_bmbt_irec
*got
,
4072 struct xfs_bmbt_irec
*prev
,
4073 xfs_extnum_t
*lastx
,
4076 struct xfs_mount
*mp
= ip
->i_mount
;
4077 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, XFS_DATA_FORK
);
4079 xfs_extlen_t indlen
;
4080 char rt
= XFS_IS_REALTIME_INODE(ip
);
4084 alen
= XFS_FILBLKS_MIN(len
, MAXEXTLEN
);
4086 alen
= XFS_FILBLKS_MIN(alen
, got
->br_startoff
- aoff
);
4088 /* Figure out the extent size, adjust alen */
4089 extsz
= xfs_get_extsz_hint(ip
);
4092 * Make sure we don't exceed a single extent length when we
4093 * align the extent by reducing length we are going to
4094 * allocate by the maximum amount extent size aligment may
4097 alen
= XFS_FILBLKS_MIN(len
, MAXEXTLEN
- (2 * extsz
- 1));
4098 error
= xfs_bmap_extsize_align(mp
, got
, prev
, extsz
, rt
, eof
,
4099 1, 0, &aoff
, &alen
);
4104 extsz
= alen
/ mp
->m_sb
.sb_rextsize
;
4107 * Make a transaction-less quota reservation for delayed allocation
4108 * blocks. This number gets adjusted later. We return if we haven't
4109 * allocated blocks already inside this loop.
4111 error
= xfs_trans_reserve_quota_nblks(NULL
, ip
, (long)alen
, 0,
4112 rt
? XFS_QMOPT_RES_RTBLKS
: XFS_QMOPT_RES_REGBLKS
);
4117 * Split changing sb for alen and indlen since they could be coming
4118 * from different places.
4120 indlen
= (xfs_extlen_t
)xfs_bmap_worst_indlen(ip
, alen
);
4124 error
= xfs_mod_frextents(mp
, -((int64_t)extsz
));
4126 error
= xfs_mod_fdblocks(mp
, -((int64_t)alen
), false);
4130 goto out_unreserve_quota
;
4132 error
= xfs_mod_fdblocks(mp
, -((int64_t)indlen
), false);
4134 goto out_unreserve_blocks
;
4137 ip
->i_delayed_blks
+= alen
;
4139 got
->br_startoff
= aoff
;
4140 got
->br_startblock
= nullstartblock(indlen
);
4141 got
->br_blockcount
= alen
;
4142 got
->br_state
= XFS_EXT_NORM
;
4143 xfs_bmap_add_extent_hole_delay(ip
, lastx
, got
);
4146 * Update our extent pointer, given that xfs_bmap_add_extent_hole_delay
4147 * might have merged it into one of the neighbouring ones.
4149 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, *lastx
), got
);
4151 ASSERT(got
->br_startoff
<= aoff
);
4152 ASSERT(got
->br_startoff
+ got
->br_blockcount
>= aoff
+ alen
);
4153 ASSERT(isnullstartblock(got
->br_startblock
));
4154 ASSERT(got
->br_state
== XFS_EXT_NORM
);
4157 out_unreserve_blocks
:
4159 xfs_mod_frextents(mp
, extsz
);
4161 xfs_mod_fdblocks(mp
, alen
, false);
4162 out_unreserve_quota
:
4163 if (XFS_IS_QUOTA_ON(mp
))
4164 xfs_trans_unreserve_quota_nblks(NULL
, ip
, (long)alen
, 0, rt
?
4165 XFS_QMOPT_RES_RTBLKS
: XFS_QMOPT_RES_REGBLKS
);
4170 * Map file blocks to filesystem blocks, adding delayed allocations as needed.
4174 struct xfs_inode
*ip
, /* incore inode */
4175 xfs_fileoff_t bno
, /* starting file offs. mapped */
4176 xfs_filblks_t len
, /* length to map in file */
4177 struct xfs_bmbt_irec
*mval
, /* output: map values */
4178 int *nmap
, /* i/o: mval size/count */
4179 int flags
) /* XFS_BMAPI_... */
4181 struct xfs_mount
*mp
= ip
->i_mount
;
4182 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, XFS_DATA_FORK
);
4183 struct xfs_bmbt_irec got
; /* current file extent record */
4184 struct xfs_bmbt_irec prev
; /* previous file extent record */
4185 xfs_fileoff_t obno
; /* old block number (offset) */
4186 xfs_fileoff_t end
; /* end of mapped file region */
4187 xfs_extnum_t lastx
; /* last useful extent number */
4188 int eof
; /* we've hit the end of extents */
4189 int n
= 0; /* current extent index */
4193 ASSERT(*nmap
<= XFS_BMAP_MAX_NMAP
);
4194 ASSERT(!(flags
& ~XFS_BMAPI_ENTIRE
));
4195 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
4197 if (unlikely(XFS_TEST_ERROR(
4198 (XFS_IFORK_FORMAT(ip
, XFS_DATA_FORK
) != XFS_DINODE_FMT_EXTENTS
&&
4199 XFS_IFORK_FORMAT(ip
, XFS_DATA_FORK
) != XFS_DINODE_FMT_BTREE
),
4200 mp
, XFS_ERRTAG_BMAPIFORMAT
, XFS_RANDOM_BMAPIFORMAT
))) {
4201 XFS_ERROR_REPORT("xfs_bmapi_delay", XFS_ERRLEVEL_LOW
, mp
);
4202 return -EFSCORRUPTED
;
4205 if (XFS_FORCED_SHUTDOWN(mp
))
4208 XFS_STATS_INC(xs_blk_mapw
);
4210 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
4211 error
= xfs_iread_extents(NULL
, ip
, XFS_DATA_FORK
);
4216 xfs_bmap_search_extents(ip
, bno
, XFS_DATA_FORK
, &eof
, &lastx
, &got
, &prev
);
4220 while (bno
< end
&& n
< *nmap
) {
4221 if (eof
|| got
.br_startoff
> bno
) {
4222 error
= xfs_bmapi_reserve_delalloc(ip
, bno
, len
, &got
,
4223 &prev
, &lastx
, eof
);
4233 /* set up the extent map to return. */
4234 xfs_bmapi_trim_map(mval
, &got
, &bno
, len
, obno
, end
, n
, flags
);
4235 xfs_bmapi_update_map(&mval
, &bno
, &len
, obno
, end
, &n
, flags
);
4237 /* If we're done, stop now. */
4238 if (bno
>= end
|| n
>= *nmap
)
4241 /* Else go on to the next record. */
4243 if (++lastx
< ifp
->if_bytes
/ sizeof(xfs_bmbt_rec_t
))
4244 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, lastx
), &got
);
4256 struct xfs_bmalloca
*bma
)
4258 struct xfs_mount
*mp
= bma
->ip
->i_mount
;
4259 int whichfork
= (bma
->flags
& XFS_BMAPI_ATTRFORK
) ?
4260 XFS_ATTR_FORK
: XFS_DATA_FORK
;
4261 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
4262 int tmp_logflags
= 0;
4265 ASSERT(bma
->length
> 0);
4268 * For the wasdelay case, we could also just allocate the stuff asked
4269 * for in this bmap call but that wouldn't be as good.
4272 bma
->length
= (xfs_extlen_t
)bma
->got
.br_blockcount
;
4273 bma
->offset
= bma
->got
.br_startoff
;
4274 if (bma
->idx
!= NULLEXTNUM
&& bma
->idx
) {
4275 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
- 1),
4279 bma
->length
= XFS_FILBLKS_MIN(bma
->length
, MAXEXTLEN
);
4281 bma
->length
= XFS_FILBLKS_MIN(bma
->length
,
4282 bma
->got
.br_startoff
- bma
->offset
);
4286 * Indicate if this is the first user data in the file, or just any
4289 if (!(bma
->flags
& XFS_BMAPI_METADATA
)) {
4290 bma
->userdata
= (bma
->offset
== 0) ?
4291 XFS_ALLOC_INITIAL_USER_DATA
: XFS_ALLOC_USERDATA
;
4294 bma
->minlen
= (bma
->flags
& XFS_BMAPI_CONTIG
) ? bma
->length
: 1;
4297 * Only want to do the alignment at the eof if it is userdata and
4298 * allocation length is larger than a stripe unit.
4300 if (mp
->m_dalign
&& bma
->length
>= mp
->m_dalign
&&
4301 !(bma
->flags
& XFS_BMAPI_METADATA
) && whichfork
== XFS_DATA_FORK
) {
4302 error
= xfs_bmap_isaeof(bma
, whichfork
);
4307 error
= xfs_bmap_alloc(bma
);
4311 if (bma
->flist
->xbf_low
)
4314 bma
->cur
->bc_private
.b
.firstblock
= *bma
->firstblock
;
4315 if (bma
->blkno
== NULLFSBLOCK
)
4317 if ((ifp
->if_flags
& XFS_IFBROOT
) && !bma
->cur
) {
4318 bma
->cur
= xfs_bmbt_init_cursor(mp
, bma
->tp
, bma
->ip
, whichfork
);
4319 bma
->cur
->bc_private
.b
.firstblock
= *bma
->firstblock
;
4320 bma
->cur
->bc_private
.b
.flist
= bma
->flist
;
4323 * Bump the number of extents we've allocated
4329 bma
->cur
->bc_private
.b
.flags
=
4330 bma
->wasdel
? XFS_BTCUR_BPRV_WASDEL
: 0;
4332 bma
->got
.br_startoff
= bma
->offset
;
4333 bma
->got
.br_startblock
= bma
->blkno
;
4334 bma
->got
.br_blockcount
= bma
->length
;
4335 bma
->got
.br_state
= XFS_EXT_NORM
;
4338 * A wasdelay extent has been initialized, so shouldn't be flagged
4341 if (!bma
->wasdel
&& (bma
->flags
& XFS_BMAPI_PREALLOC
) &&
4342 xfs_sb_version_hasextflgbit(&mp
->m_sb
))
4343 bma
->got
.br_state
= XFS_EXT_UNWRITTEN
;
4346 error
= xfs_bmap_add_extent_delay_real(bma
);
4348 error
= xfs_bmap_add_extent_hole_real(bma
, whichfork
);
4350 bma
->logflags
|= tmp_logflags
;
4355 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4356 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4357 * the neighbouring ones.
4359 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
), &bma
->got
);
4361 ASSERT(bma
->got
.br_startoff
<= bma
->offset
);
4362 ASSERT(bma
->got
.br_startoff
+ bma
->got
.br_blockcount
>=
4363 bma
->offset
+ bma
->length
);
4364 ASSERT(bma
->got
.br_state
== XFS_EXT_NORM
||
4365 bma
->got
.br_state
== XFS_EXT_UNWRITTEN
);
4370 xfs_bmapi_convert_unwritten(
4371 struct xfs_bmalloca
*bma
,
4372 struct xfs_bmbt_irec
*mval
,
4376 int whichfork
= (flags
& XFS_BMAPI_ATTRFORK
) ?
4377 XFS_ATTR_FORK
: XFS_DATA_FORK
;
4378 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
4379 int tmp_logflags
= 0;
4382 /* check if we need to do unwritten->real conversion */
4383 if (mval
->br_state
== XFS_EXT_UNWRITTEN
&&
4384 (flags
& XFS_BMAPI_PREALLOC
))
4387 /* check if we need to do real->unwritten conversion */
4388 if (mval
->br_state
== XFS_EXT_NORM
&&
4389 (flags
& (XFS_BMAPI_PREALLOC
| XFS_BMAPI_CONVERT
)) !=
4390 (XFS_BMAPI_PREALLOC
| XFS_BMAPI_CONVERT
))
4394 * Modify (by adding) the state flag, if writing.
4396 ASSERT(mval
->br_blockcount
<= len
);
4397 if ((ifp
->if_flags
& XFS_IFBROOT
) && !bma
->cur
) {
4398 bma
->cur
= xfs_bmbt_init_cursor(bma
->ip
->i_mount
, bma
->tp
,
4399 bma
->ip
, whichfork
);
4400 bma
->cur
->bc_private
.b
.firstblock
= *bma
->firstblock
;
4401 bma
->cur
->bc_private
.b
.flist
= bma
->flist
;
4403 mval
->br_state
= (mval
->br_state
== XFS_EXT_UNWRITTEN
)
4404 ? XFS_EXT_NORM
: XFS_EXT_UNWRITTEN
;
4406 error
= xfs_bmap_add_extent_unwritten_real(bma
->tp
, bma
->ip
, &bma
->idx
,
4407 &bma
->cur
, mval
, bma
->firstblock
, bma
->flist
,
4409 bma
->logflags
|= tmp_logflags
;
4414 * Update our extent pointer, given that
4415 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4416 * of the neighbouring ones.
4418 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
), &bma
->got
);
4421 * We may have combined previously unwritten space with written space,
4422 * so generate another request.
4424 if (mval
->br_blockcount
< len
)
4430 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4431 * extent state if necessary. Details behaviour is controlled by the flags
4432 * parameter. Only allocates blocks from a single allocation group, to avoid
4435 * The returned value in "firstblock" from the first call in a transaction
4436 * must be remembered and presented to subsequent calls in "firstblock".
4437 * An upper bound for the number of blocks to be allocated is supplied to
4438 * the first call in "total"; if no allocation group has that many free
4439 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4443 struct xfs_trans
*tp
, /* transaction pointer */
4444 struct xfs_inode
*ip
, /* incore inode */
4445 xfs_fileoff_t bno
, /* starting file offs. mapped */
4446 xfs_filblks_t len
, /* length to map in file */
4447 int flags
, /* XFS_BMAPI_... */
4448 xfs_fsblock_t
*firstblock
, /* first allocated block
4449 controls a.g. for allocs */
4450 xfs_extlen_t total
, /* total blocks needed */
4451 struct xfs_bmbt_irec
*mval
, /* output: map values */
4452 int *nmap
, /* i/o: mval size/count */
4453 struct xfs_bmap_free
*flist
) /* i/o: list extents to free */
4455 struct xfs_mount
*mp
= ip
->i_mount
;
4456 struct xfs_ifork
*ifp
;
4457 struct xfs_bmalloca bma
= { NULL
}; /* args for xfs_bmap_alloc */
4458 xfs_fileoff_t end
; /* end of mapped file region */
4459 int eof
; /* after the end of extents */
4460 int error
; /* error return */
4461 int n
; /* current extent index */
4462 xfs_fileoff_t obno
; /* old block number (offset) */
4463 int whichfork
; /* data or attr fork */
4464 char inhole
; /* current location is hole in file */
4465 char wasdelay
; /* old extent was delayed */
4468 xfs_fileoff_t orig_bno
; /* original block number value */
4469 int orig_flags
; /* original flags arg value */
4470 xfs_filblks_t orig_len
; /* original value of len arg */
4471 struct xfs_bmbt_irec
*orig_mval
; /* original value of mval */
4472 int orig_nmap
; /* original value of *nmap */
4480 whichfork
= (flags
& XFS_BMAPI_ATTRFORK
) ?
4481 XFS_ATTR_FORK
: XFS_DATA_FORK
;
4484 ASSERT(*nmap
<= XFS_BMAP_MAX_NMAP
);
4485 ASSERT(!(flags
& XFS_BMAPI_IGSTATE
));
4488 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_LOCAL
);
4489 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
4491 if (unlikely(XFS_TEST_ERROR(
4492 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
4493 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
4494 mp
, XFS_ERRTAG_BMAPIFORMAT
, XFS_RANDOM_BMAPIFORMAT
))) {
4495 XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW
, mp
);
4496 return -EFSCORRUPTED
;
4499 if (XFS_FORCED_SHUTDOWN(mp
))
4502 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4504 XFS_STATS_INC(xs_blk_mapw
);
4506 if (*firstblock
== NULLFSBLOCK
) {
4507 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
)
4508 bma
.minleft
= be16_to_cpu(ifp
->if_broot
->bb_level
) + 1;
4515 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
4516 error
= xfs_iread_extents(tp
, ip
, whichfork
);
4521 xfs_bmap_search_extents(ip
, bno
, whichfork
, &eof
, &bma
.idx
, &bma
.got
,
4532 bma
.firstblock
= firstblock
;
4534 while (bno
< end
&& n
< *nmap
) {
4535 inhole
= eof
|| bma
.got
.br_startoff
> bno
;
4536 wasdelay
= !inhole
&& isnullstartblock(bma
.got
.br_startblock
);
4539 * First, deal with the hole before the allocated space
4540 * that we found, if any.
4542 if (inhole
|| wasdelay
) {
4544 bma
.conv
= !!(flags
& XFS_BMAPI_CONVERT
);
4545 bma
.wasdel
= wasdelay
;
4550 * There's a 32/64 bit type mismatch between the
4551 * allocation length request (which can be 64 bits in
4552 * length) and the bma length request, which is
4553 * xfs_extlen_t and therefore 32 bits. Hence we have to
4554 * check for 32-bit overflows and handle them here.
4556 if (len
> (xfs_filblks_t
)MAXEXTLEN
)
4557 bma
.length
= MAXEXTLEN
;
4562 ASSERT(bma
.length
> 0);
4563 error
= xfs_bmapi_allocate(&bma
);
4566 if (bma
.blkno
== NULLFSBLOCK
)
4570 /* Deal with the allocated space we found. */
4571 xfs_bmapi_trim_map(mval
, &bma
.got
, &bno
, len
, obno
,
4574 /* Execute unwritten extent conversion if necessary */
4575 error
= xfs_bmapi_convert_unwritten(&bma
, mval
, len
, flags
);
4576 if (error
== -EAGAIN
)
4581 /* update the extent map to return */
4582 xfs_bmapi_update_map(&mval
, &bno
, &len
, obno
, end
, &n
, flags
);
4585 * If we're done, stop now. Stop when we've allocated
4586 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4587 * the transaction may get too big.
4589 if (bno
>= end
|| n
>= *nmap
|| bma
.nallocs
>= *nmap
)
4592 /* Else go on to the next record. */
4594 if (++bma
.idx
< ifp
->if_bytes
/ sizeof(xfs_bmbt_rec_t
)) {
4595 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
.idx
),
4603 * Transform from btree to extents, give it cur.
4605 if (xfs_bmap_wants_extents(ip
, whichfork
)) {
4606 int tmp_logflags
= 0;
4609 error
= xfs_bmap_btree_to_extents(tp
, ip
, bma
.cur
,
4610 &tmp_logflags
, whichfork
);
4611 bma
.logflags
|= tmp_logflags
;
4616 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
||
4617 XFS_IFORK_NEXTENTS(ip
, whichfork
) >
4618 XFS_IFORK_MAXEXT(ip
, whichfork
));
4622 * Log everything. Do this after conversion, there's no point in
4623 * logging the extent records if we've converted to btree format.
4625 if ((bma
.logflags
& xfs_ilog_fext(whichfork
)) &&
4626 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
4627 bma
.logflags
&= ~xfs_ilog_fext(whichfork
);
4628 else if ((bma
.logflags
& xfs_ilog_fbroot(whichfork
)) &&
4629 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)
4630 bma
.logflags
&= ~xfs_ilog_fbroot(whichfork
);
4632 * Log whatever the flags say, even if error. Otherwise we might miss
4633 * detecting a case where the data is changed, there's an error,
4634 * and it's not logged so we don't shutdown when we should.
4637 xfs_trans_log_inode(tp
, ip
, bma
.logflags
);
4641 ASSERT(*firstblock
== NULLFSBLOCK
||
4642 XFS_FSB_TO_AGNO(mp
, *firstblock
) ==
4644 bma
.cur
->bc_private
.b
.firstblock
) ||
4646 XFS_FSB_TO_AGNO(mp
, *firstblock
) <
4648 bma
.cur
->bc_private
.b
.firstblock
)));
4649 *firstblock
= bma
.cur
->bc_private
.b
.firstblock
;
4651 xfs_btree_del_cursor(bma
.cur
,
4652 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
4655 xfs_bmap_validate_ret(orig_bno
, orig_len
, orig_flags
, orig_mval
,
4661 * Called by xfs_bmapi to update file extent records and the btree
4662 * after removing space (or undoing a delayed allocation).
4664 STATIC
int /* error */
4665 xfs_bmap_del_extent(
4666 xfs_inode_t
*ip
, /* incore inode pointer */
4667 xfs_trans_t
*tp
, /* current transaction pointer */
4668 xfs_extnum_t
*idx
, /* extent number to update/delete */
4669 xfs_bmap_free_t
*flist
, /* list of extents to be freed */
4670 xfs_btree_cur_t
*cur
, /* if null, not a btree */
4671 xfs_bmbt_irec_t
*del
, /* data to remove from extents */
4672 int *logflagsp
, /* inode logging flags */
4673 int whichfork
) /* data or attr fork */
4675 xfs_filblks_t da_new
; /* new delay-alloc indirect blocks */
4676 xfs_filblks_t da_old
; /* old delay-alloc indirect blocks */
4677 xfs_fsblock_t del_endblock
=0; /* first block past del */
4678 xfs_fileoff_t del_endoff
; /* first offset past del */
4679 int delay
; /* current block is delayed allocated */
4680 int do_fx
; /* free extent at end of routine */
4681 xfs_bmbt_rec_host_t
*ep
; /* current extent entry pointer */
4682 int error
; /* error return value */
4683 int flags
; /* inode logging flags */
4684 xfs_bmbt_irec_t got
; /* current extent entry */
4685 xfs_fileoff_t got_endoff
; /* first offset past got */
4686 int i
; /* temp state */
4687 xfs_ifork_t
*ifp
; /* inode fork pointer */
4688 xfs_mount_t
*mp
; /* mount structure */
4689 xfs_filblks_t nblks
; /* quota/sb block count */
4690 xfs_bmbt_irec_t
new; /* new record to be inserted */
4692 uint qfield
; /* quota field to update */
4693 xfs_filblks_t temp
; /* for indirect length calculations */
4694 xfs_filblks_t temp2
; /* for indirect length calculations */
4697 XFS_STATS_INC(xs_del_exlist
);
4699 if (whichfork
== XFS_ATTR_FORK
)
4700 state
|= BMAP_ATTRFORK
;
4703 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4704 ASSERT((*idx
>= 0) && (*idx
< ifp
->if_bytes
/
4705 (uint
)sizeof(xfs_bmbt_rec_t
)));
4706 ASSERT(del
->br_blockcount
> 0);
4707 ep
= xfs_iext_get_ext(ifp
, *idx
);
4708 xfs_bmbt_get_all(ep
, &got
);
4709 ASSERT(got
.br_startoff
<= del
->br_startoff
);
4710 del_endoff
= del
->br_startoff
+ del
->br_blockcount
;
4711 got_endoff
= got
.br_startoff
+ got
.br_blockcount
;
4712 ASSERT(got_endoff
>= del_endoff
);
4713 delay
= isnullstartblock(got
.br_startblock
);
4714 ASSERT(isnullstartblock(del
->br_startblock
) == delay
);
4719 * If deleting a real allocation, must free up the disk space.
4722 flags
= XFS_ILOG_CORE
;
4724 * Realtime allocation. Free it and record di_nblocks update.
4726 if (whichfork
== XFS_DATA_FORK
&& XFS_IS_REALTIME_INODE(ip
)) {
4730 ASSERT(do_mod(del
->br_blockcount
,
4731 mp
->m_sb
.sb_rextsize
) == 0);
4732 ASSERT(do_mod(del
->br_startblock
,
4733 mp
->m_sb
.sb_rextsize
) == 0);
4734 bno
= del
->br_startblock
;
4735 len
= del
->br_blockcount
;
4736 do_div(bno
, mp
->m_sb
.sb_rextsize
);
4737 do_div(len
, mp
->m_sb
.sb_rextsize
);
4738 error
= xfs_rtfree_extent(tp
, bno
, (xfs_extlen_t
)len
);
4742 nblks
= len
* mp
->m_sb
.sb_rextsize
;
4743 qfield
= XFS_TRANS_DQ_RTBCOUNT
;
4746 * Ordinary allocation.
4750 nblks
= del
->br_blockcount
;
4751 qfield
= XFS_TRANS_DQ_BCOUNT
;
4754 * Set up del_endblock and cur for later.
4756 del_endblock
= del
->br_startblock
+ del
->br_blockcount
;
4758 if ((error
= xfs_bmbt_lookup_eq(cur
, got
.br_startoff
,
4759 got
.br_startblock
, got
.br_blockcount
,
4762 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
4764 da_old
= da_new
= 0;
4766 da_old
= startblockval(got
.br_startblock
);
4772 * Set flag value to use in switch statement.
4773 * Left-contig is 2, right-contig is 1.
4775 switch (((got
.br_startoff
== del
->br_startoff
) << 1) |
4776 (got_endoff
== del_endoff
)) {
4779 * Matches the whole extent. Delete the entry.
4781 xfs_iext_remove(ip
, *idx
, 1,
4782 whichfork
== XFS_ATTR_FORK
? BMAP_ATTRFORK
: 0);
4787 XFS_IFORK_NEXT_SET(ip
, whichfork
,
4788 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
4789 flags
|= XFS_ILOG_CORE
;
4791 flags
|= xfs_ilog_fext(whichfork
);
4794 if ((error
= xfs_btree_delete(cur
, &i
)))
4796 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
4801 * Deleting the first part of the extent.
4803 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
4804 xfs_bmbt_set_startoff(ep
, del_endoff
);
4805 temp
= got
.br_blockcount
- del
->br_blockcount
;
4806 xfs_bmbt_set_blockcount(ep
, temp
);
4808 temp
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
4810 xfs_bmbt_set_startblock(ep
, nullstartblock((int)temp
));
4811 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
4815 xfs_bmbt_set_startblock(ep
, del_endblock
);
4816 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
4818 flags
|= xfs_ilog_fext(whichfork
);
4821 if ((error
= xfs_bmbt_update(cur
, del_endoff
, del_endblock
,
4822 got
.br_blockcount
- del
->br_blockcount
,
4829 * Deleting the last part of the extent.
4831 temp
= got
.br_blockcount
- del
->br_blockcount
;
4832 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
4833 xfs_bmbt_set_blockcount(ep
, temp
);
4835 temp
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
4837 xfs_bmbt_set_startblock(ep
, nullstartblock((int)temp
));
4838 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
4842 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
4844 flags
|= xfs_ilog_fext(whichfork
);
4847 if ((error
= xfs_bmbt_update(cur
, got
.br_startoff
,
4849 got
.br_blockcount
- del
->br_blockcount
,
4856 * Deleting the middle of the extent.
4858 temp
= del
->br_startoff
- got
.br_startoff
;
4859 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
4860 xfs_bmbt_set_blockcount(ep
, temp
);
4861 new.br_startoff
= del_endoff
;
4862 temp2
= got_endoff
- del_endoff
;
4863 new.br_blockcount
= temp2
;
4864 new.br_state
= got
.br_state
;
4866 new.br_startblock
= del_endblock
;
4867 flags
|= XFS_ILOG_CORE
;
4869 if ((error
= xfs_bmbt_update(cur
,
4871 got
.br_startblock
, temp
,
4874 if ((error
= xfs_btree_increment(cur
, 0, &i
)))
4876 cur
->bc_rec
.b
= new;
4877 error
= xfs_btree_insert(cur
, &i
);
4878 if (error
&& error
!= -ENOSPC
)
4881 * If get no-space back from btree insert,
4882 * it tried a split, and we have a zero
4883 * block reservation.
4884 * Fix up our state and return the error.
4886 if (error
== -ENOSPC
) {
4888 * Reset the cursor, don't trust
4889 * it after any insert operation.
4891 if ((error
= xfs_bmbt_lookup_eq(cur
,
4896 XFS_WANT_CORRUPTED_GOTO(mp
,
4899 * Update the btree record back
4900 * to the original value.
4902 if ((error
= xfs_bmbt_update(cur
,
4909 * Reset the extent record back
4910 * to the original value.
4912 xfs_bmbt_set_blockcount(ep
,
4918 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
4920 flags
|= xfs_ilog_fext(whichfork
);
4921 XFS_IFORK_NEXT_SET(ip
, whichfork
,
4922 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
4924 ASSERT(whichfork
== XFS_DATA_FORK
);
4925 temp
= xfs_bmap_worst_indlen(ip
, temp
);
4926 xfs_bmbt_set_startblock(ep
, nullstartblock((int)temp
));
4927 temp2
= xfs_bmap_worst_indlen(ip
, temp2
);
4928 new.br_startblock
= nullstartblock((int)temp2
);
4929 da_new
= temp
+ temp2
;
4930 while (da_new
> da_old
) {
4934 xfs_bmbt_set_startblock(ep
,
4935 nullstartblock((int)temp
));
4937 if (da_new
== da_old
)
4943 nullstartblock((int)temp2
);
4947 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
4948 xfs_iext_insert(ip
, *idx
+ 1, 1, &new, state
);
4953 * If we need to, add to list of extents to delete.
4956 xfs_bmap_add_free(del
->br_startblock
, del
->br_blockcount
, flist
,
4959 * Adjust inode # blocks in the file.
4962 ip
->i_d
.di_nblocks
-= nblks
;
4964 * Adjust quota data.
4967 xfs_trans_mod_dquot_byino(tp
, ip
, qfield
, (long)-nblks
);
4970 * Account for change in delayed indirect blocks.
4971 * Nothing to do for disk quota accounting here.
4973 ASSERT(da_old
>= da_new
);
4974 if (da_old
> da_new
)
4975 xfs_mod_fdblocks(mp
, (int64_t)(da_old
- da_new
), false);
4982 * Unmap (remove) blocks from a file.
4983 * If nexts is nonzero then the number of extents to remove is limited to
4984 * that value. If not all extents in the block range can be removed then
4989 xfs_trans_t
*tp
, /* transaction pointer */
4990 struct xfs_inode
*ip
, /* incore inode */
4991 xfs_fileoff_t bno
, /* starting offset to unmap */
4992 xfs_filblks_t len
, /* length to unmap in file */
4993 int flags
, /* misc flags */
4994 xfs_extnum_t nexts
, /* number of extents max */
4995 xfs_fsblock_t
*firstblock
, /* first allocated block
4996 controls a.g. for allocs */
4997 xfs_bmap_free_t
*flist
, /* i/o: list extents to free */
4998 int *done
) /* set if not done yet */
5000 xfs_btree_cur_t
*cur
; /* bmap btree cursor */
5001 xfs_bmbt_irec_t del
; /* extent being deleted */
5002 int eof
; /* is deleting at eof */
5003 xfs_bmbt_rec_host_t
*ep
; /* extent record pointer */
5004 int error
; /* error return value */
5005 xfs_extnum_t extno
; /* extent number in list */
5006 xfs_bmbt_irec_t got
; /* current extent record */
5007 xfs_ifork_t
*ifp
; /* inode fork pointer */
5008 int isrt
; /* freeing in rt area */
5009 xfs_extnum_t lastx
; /* last extent index used */
5010 int logflags
; /* transaction logging flags */
5011 xfs_extlen_t mod
; /* rt extent offset */
5012 xfs_mount_t
*mp
; /* mount structure */
5013 xfs_extnum_t nextents
; /* number of file extents */
5014 xfs_bmbt_irec_t prev
; /* previous extent record */
5015 xfs_fileoff_t start
; /* first file offset deleted */
5016 int tmp_logflags
; /* partial logging flags */
5017 int wasdel
; /* was a delayed alloc extent */
5018 int whichfork
; /* data or attribute fork */
5021 trace_xfs_bunmap(ip
, bno
, len
, flags
, _RET_IP_
);
5023 whichfork
= (flags
& XFS_BMAPI_ATTRFORK
) ?
5024 XFS_ATTR_FORK
: XFS_DATA_FORK
;
5025 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5027 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
5028 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)) {
5029 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW
,
5031 return -EFSCORRUPTED
;
5034 if (XFS_FORCED_SHUTDOWN(mp
))
5037 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
5041 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
5042 (error
= xfs_iread_extents(tp
, ip
, whichfork
)))
5044 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
5045 if (nextents
== 0) {
5049 XFS_STATS_INC(xs_blk_unmap
);
5050 isrt
= (whichfork
== XFS_DATA_FORK
) && XFS_IS_REALTIME_INODE(ip
);
5052 bno
= start
+ len
- 1;
5053 ep
= xfs_bmap_search_extents(ip
, bno
, whichfork
, &eof
, &lastx
, &got
,
5057 * Check to see if the given block number is past the end of the
5058 * file, back up to the last block if so...
5061 ep
= xfs_iext_get_ext(ifp
, --lastx
);
5062 xfs_bmbt_get_all(ep
, &got
);
5063 bno
= got
.br_startoff
+ got
.br_blockcount
- 1;
5066 if (ifp
->if_flags
& XFS_IFBROOT
) {
5067 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
);
5068 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
5069 cur
->bc_private
.b
.firstblock
= *firstblock
;
5070 cur
->bc_private
.b
.flist
= flist
;
5071 cur
->bc_private
.b
.flags
= 0;
5077 * Synchronize by locking the bitmap inode.
5079 xfs_ilock(mp
->m_rbmip
, XFS_ILOCK_EXCL
);
5080 xfs_trans_ijoin(tp
, mp
->m_rbmip
, XFS_ILOCK_EXCL
);
5084 while (bno
!= (xfs_fileoff_t
)-1 && bno
>= start
&& lastx
>= 0 &&
5085 (nexts
== 0 || extno
< nexts
)) {
5087 * Is the found extent after a hole in which bno lives?
5088 * Just back up to the previous extent, if so.
5090 if (got
.br_startoff
> bno
) {
5093 ep
= xfs_iext_get_ext(ifp
, lastx
);
5094 xfs_bmbt_get_all(ep
, &got
);
5097 * Is the last block of this extent before the range
5098 * we're supposed to delete? If so, we're done.
5100 bno
= XFS_FILEOFF_MIN(bno
,
5101 got
.br_startoff
+ got
.br_blockcount
- 1);
5105 * Then deal with the (possibly delayed) allocated space
5110 wasdel
= isnullstartblock(del
.br_startblock
);
5111 if (got
.br_startoff
< start
) {
5112 del
.br_startoff
= start
;
5113 del
.br_blockcount
-= start
- got
.br_startoff
;
5115 del
.br_startblock
+= start
- got
.br_startoff
;
5117 if (del
.br_startoff
+ del
.br_blockcount
> bno
+ 1)
5118 del
.br_blockcount
= bno
+ 1 - del
.br_startoff
;
5119 sum
= del
.br_startblock
+ del
.br_blockcount
;
5121 (mod
= do_mod(sum
, mp
->m_sb
.sb_rextsize
))) {
5123 * Realtime extent not lined up at the end.
5124 * The extent could have been split into written
5125 * and unwritten pieces, or we could just be
5126 * unmapping part of it. But we can't really
5127 * get rid of part of a realtime extent.
5129 if (del
.br_state
== XFS_EXT_UNWRITTEN
||
5130 !xfs_sb_version_hasextflgbit(&mp
->m_sb
)) {
5132 * This piece is unwritten, or we're not
5133 * using unwritten extents. Skip over it.
5136 bno
-= mod
> del
.br_blockcount
?
5137 del
.br_blockcount
: mod
;
5138 if (bno
< got
.br_startoff
) {
5140 xfs_bmbt_get_all(xfs_iext_get_ext(
5146 * It's written, turn it unwritten.
5147 * This is better than zeroing it.
5149 ASSERT(del
.br_state
== XFS_EXT_NORM
);
5150 ASSERT(xfs_trans_get_block_res(tp
) > 0);
5152 * If this spans a realtime extent boundary,
5153 * chop it back to the start of the one we end at.
5155 if (del
.br_blockcount
> mod
) {
5156 del
.br_startoff
+= del
.br_blockcount
- mod
;
5157 del
.br_startblock
+= del
.br_blockcount
- mod
;
5158 del
.br_blockcount
= mod
;
5160 del
.br_state
= XFS_EXT_UNWRITTEN
;
5161 error
= xfs_bmap_add_extent_unwritten_real(tp
, ip
,
5162 &lastx
, &cur
, &del
, firstblock
, flist
,
5168 if (isrt
&& (mod
= do_mod(del
.br_startblock
, mp
->m_sb
.sb_rextsize
))) {
5170 * Realtime extent is lined up at the end but not
5171 * at the front. We'll get rid of full extents if
5174 mod
= mp
->m_sb
.sb_rextsize
- mod
;
5175 if (del
.br_blockcount
> mod
) {
5176 del
.br_blockcount
-= mod
;
5177 del
.br_startoff
+= mod
;
5178 del
.br_startblock
+= mod
;
5179 } else if ((del
.br_startoff
== start
&&
5180 (del
.br_state
== XFS_EXT_UNWRITTEN
||
5181 xfs_trans_get_block_res(tp
) == 0)) ||
5182 !xfs_sb_version_hasextflgbit(&mp
->m_sb
)) {
5184 * Can't make it unwritten. There isn't
5185 * a full extent here so just skip it.
5187 ASSERT(bno
>= del
.br_blockcount
);
5188 bno
-= del
.br_blockcount
;
5189 if (got
.br_startoff
> bno
) {
5191 ep
= xfs_iext_get_ext(ifp
,
5193 xfs_bmbt_get_all(ep
, &got
);
5197 } else if (del
.br_state
== XFS_EXT_UNWRITTEN
) {
5199 * This one is already unwritten.
5200 * It must have a written left neighbor.
5201 * Unwrite the killed part of that one and
5205 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
,
5207 ASSERT(prev
.br_state
== XFS_EXT_NORM
);
5208 ASSERT(!isnullstartblock(prev
.br_startblock
));
5209 ASSERT(del
.br_startblock
==
5210 prev
.br_startblock
+ prev
.br_blockcount
);
5211 if (prev
.br_startoff
< start
) {
5212 mod
= start
- prev
.br_startoff
;
5213 prev
.br_blockcount
-= mod
;
5214 prev
.br_startblock
+= mod
;
5215 prev
.br_startoff
= start
;
5217 prev
.br_state
= XFS_EXT_UNWRITTEN
;
5219 error
= xfs_bmap_add_extent_unwritten_real(tp
,
5220 ip
, &lastx
, &cur
, &prev
,
5221 firstblock
, flist
, &logflags
);
5226 ASSERT(del
.br_state
== XFS_EXT_NORM
);
5227 del
.br_state
= XFS_EXT_UNWRITTEN
;
5228 error
= xfs_bmap_add_extent_unwritten_real(tp
,
5229 ip
, &lastx
, &cur
, &del
,
5230 firstblock
, flist
, &logflags
);
5237 ASSERT(startblockval(del
.br_startblock
) > 0);
5238 /* Update realtime/data freespace, unreserve quota */
5240 xfs_filblks_t rtexts
;
5242 rtexts
= XFS_FSB_TO_B(mp
, del
.br_blockcount
);
5243 do_div(rtexts
, mp
->m_sb
.sb_rextsize
);
5244 xfs_mod_frextents(mp
, (int64_t)rtexts
);
5245 (void)xfs_trans_reserve_quota_nblks(NULL
,
5246 ip
, -((long)del
.br_blockcount
), 0,
5247 XFS_QMOPT_RES_RTBLKS
);
5249 xfs_mod_fdblocks(mp
, (int64_t)del
.br_blockcount
,
5251 (void)xfs_trans_reserve_quota_nblks(NULL
,
5252 ip
, -((long)del
.br_blockcount
), 0,
5253 XFS_QMOPT_RES_REGBLKS
);
5255 ip
->i_delayed_blks
-= del
.br_blockcount
;
5257 cur
->bc_private
.b
.flags
|=
5258 XFS_BTCUR_BPRV_WASDEL
;
5260 cur
->bc_private
.b
.flags
&= ~XFS_BTCUR_BPRV_WASDEL
;
5262 * If it's the case where the directory code is running
5263 * with no block reservation, and the deleted block is in
5264 * the middle of its extent, and the resulting insert
5265 * of an extent would cause transformation to btree format,
5266 * then reject it. The calling code will then swap
5267 * blocks around instead.
5268 * We have to do this now, rather than waiting for the
5269 * conversion to btree format, since the transaction
5272 if (!wasdel
&& xfs_trans_get_block_res(tp
) == 0 &&
5273 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
&&
5274 XFS_IFORK_NEXTENTS(ip
, whichfork
) >= /* Note the >= */
5275 XFS_IFORK_MAXEXT(ip
, whichfork
) &&
5276 del
.br_startoff
> got
.br_startoff
&&
5277 del
.br_startoff
+ del
.br_blockcount
<
5278 got
.br_startoff
+ got
.br_blockcount
) {
5282 error
= xfs_bmap_del_extent(ip
, tp
, &lastx
, flist
, cur
, &del
,
5283 &tmp_logflags
, whichfork
);
5284 logflags
|= tmp_logflags
;
5287 bno
= del
.br_startoff
- 1;
5290 * If not done go on to the next (previous) record.
5292 if (bno
!= (xfs_fileoff_t
)-1 && bno
>= start
) {
5294 ep
= xfs_iext_get_ext(ifp
, lastx
);
5295 if (xfs_bmbt_get_startoff(ep
) > bno
) {
5297 ep
= xfs_iext_get_ext(ifp
,
5300 xfs_bmbt_get_all(ep
, &got
);
5305 *done
= bno
== (xfs_fileoff_t
)-1 || bno
< start
|| lastx
< 0;
5308 * Convert to a btree if necessary.
5310 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
5311 ASSERT(cur
== NULL
);
5312 error
= xfs_bmap_extents_to_btree(tp
, ip
, firstblock
, flist
,
5313 &cur
, 0, &tmp_logflags
, whichfork
);
5314 logflags
|= tmp_logflags
;
5319 * transform from btree to extents, give it cur
5321 else if (xfs_bmap_wants_extents(ip
, whichfork
)) {
5322 ASSERT(cur
!= NULL
);
5323 error
= xfs_bmap_btree_to_extents(tp
, ip
, cur
, &tmp_logflags
,
5325 logflags
|= tmp_logflags
;
5330 * transform from extents to local?
5335 * Log everything. Do this after conversion, there's no point in
5336 * logging the extent records if we've converted to btree format.
5338 if ((logflags
& xfs_ilog_fext(whichfork
)) &&
5339 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
5340 logflags
&= ~xfs_ilog_fext(whichfork
);
5341 else if ((logflags
& xfs_ilog_fbroot(whichfork
)) &&
5342 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)
5343 logflags
&= ~xfs_ilog_fbroot(whichfork
);
5345 * Log inode even in the error case, if the transaction
5346 * is dirty we'll need to shut down the filesystem.
5349 xfs_trans_log_inode(tp
, ip
, logflags
);
5352 *firstblock
= cur
->bc_private
.b
.firstblock
;
5353 cur
->bc_private
.b
.allocated
= 0;
5355 xfs_btree_del_cursor(cur
,
5356 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
5362 * Determine whether an extent shift can be accomplished by a merge with the
5363 * extent that precedes the target hole of the shift.
5367 struct xfs_bmbt_irec
*left
, /* preceding extent */
5368 struct xfs_bmbt_irec
*got
, /* current extent to shift */
5369 xfs_fileoff_t shift
) /* shift fsb */
5371 xfs_fileoff_t startoff
;
5373 startoff
= got
->br_startoff
- shift
;
5376 * The extent, once shifted, must be adjacent in-file and on-disk with
5377 * the preceding extent.
5379 if ((left
->br_startoff
+ left
->br_blockcount
!= startoff
) ||
5380 (left
->br_startblock
+ left
->br_blockcount
!= got
->br_startblock
) ||
5381 (left
->br_state
!= got
->br_state
) ||
5382 (left
->br_blockcount
+ got
->br_blockcount
> MAXEXTLEN
))
5389 * A bmap extent shift adjusts the file offset of an extent to fill a preceding
5390 * hole in the file. If an extent shift would result in the extent being fully
5391 * adjacent to the extent that currently precedes the hole, we can merge with
5392 * the preceding extent rather than do the shift.
5394 * This function assumes the caller has verified a shift-by-merge is possible
5395 * with the provided extents via xfs_bmse_can_merge().
5399 struct xfs_inode
*ip
,
5401 xfs_fileoff_t shift
, /* shift fsb */
5402 int current_ext
, /* idx of gotp */
5403 struct xfs_bmbt_rec_host
*gotp
, /* extent to shift */
5404 struct xfs_bmbt_rec_host
*leftp
, /* preceding extent */
5405 struct xfs_btree_cur
*cur
,
5406 int *logflags
) /* output */
5408 struct xfs_bmbt_irec got
;
5409 struct xfs_bmbt_irec left
;
5410 xfs_filblks_t blockcount
;
5412 struct xfs_mount
*mp
= ip
->i_mount
;
5414 xfs_bmbt_get_all(gotp
, &got
);
5415 xfs_bmbt_get_all(leftp
, &left
);
5416 blockcount
= left
.br_blockcount
+ got
.br_blockcount
;
5418 ASSERT(xfs_isilocked(ip
, XFS_IOLOCK_EXCL
));
5419 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
5420 ASSERT(xfs_bmse_can_merge(&left
, &got
, shift
));
5423 * Merge the in-core extents. Note that the host record pointers and
5424 * current_ext index are invalid once the extent has been removed via
5425 * xfs_iext_remove().
5427 xfs_bmbt_set_blockcount(leftp
, blockcount
);
5428 xfs_iext_remove(ip
, current_ext
, 1, 0);
5431 * Update the on-disk extent count, the btree if necessary and log the
5434 XFS_IFORK_NEXT_SET(ip
, whichfork
,
5435 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
5436 *logflags
|= XFS_ILOG_CORE
;
5438 *logflags
|= XFS_ILOG_DEXT
;
5442 /* lookup and remove the extent to merge */
5443 error
= xfs_bmbt_lookup_eq(cur
, got
.br_startoff
, got
.br_startblock
,
5444 got
.br_blockcount
, &i
);
5447 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
5449 error
= xfs_btree_delete(cur
, &i
);
5452 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
5454 /* lookup and update size of the previous extent */
5455 error
= xfs_bmbt_lookup_eq(cur
, left
.br_startoff
, left
.br_startblock
,
5456 left
.br_blockcount
, &i
);
5459 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
5461 left
.br_blockcount
= blockcount
;
5463 return xfs_bmbt_update(cur
, left
.br_startoff
, left
.br_startblock
,
5464 left
.br_blockcount
, left
.br_state
);
5468 * Shift a single extent.
5472 struct xfs_inode
*ip
,
5474 xfs_fileoff_t offset_shift_fsb
,
5476 struct xfs_bmbt_rec_host
*gotp
,
5477 struct xfs_btree_cur
*cur
,
5479 enum shift_direction direction
)
5481 struct xfs_ifork
*ifp
;
5482 struct xfs_mount
*mp
;
5483 xfs_fileoff_t startoff
;
5484 struct xfs_bmbt_rec_host
*adj_irecp
;
5485 struct xfs_bmbt_irec got
;
5486 struct xfs_bmbt_irec adj_irec
;
5492 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5493 total_extents
= ifp
->if_bytes
/ sizeof(xfs_bmbt_rec_t
);
5495 xfs_bmbt_get_all(gotp
, &got
);
5497 /* delalloc extents should be prevented by caller */
5498 XFS_WANT_CORRUPTED_RETURN(mp
, !isnullstartblock(got
.br_startblock
));
5500 if (direction
== SHIFT_LEFT
) {
5501 startoff
= got
.br_startoff
- offset_shift_fsb
;
5504 * Check for merge if we've got an extent to the left,
5505 * otherwise make sure there's enough room at the start
5506 * of the file for the shift.
5508 if (!*current_ext
) {
5509 if (got
.br_startoff
< offset_shift_fsb
)
5511 goto update_current_ext
;
5514 * grab the left extent and check for a large
5517 adj_irecp
= xfs_iext_get_ext(ifp
, *current_ext
- 1);
5518 xfs_bmbt_get_all(adj_irecp
, &adj_irec
);
5521 adj_irec
.br_startoff
+ adj_irec
.br_blockcount
)
5524 /* check whether to merge the extent or shift it down */
5525 if (xfs_bmse_can_merge(&adj_irec
, &got
,
5526 offset_shift_fsb
)) {
5527 return xfs_bmse_merge(ip
, whichfork
, offset_shift_fsb
,
5528 *current_ext
, gotp
, adj_irecp
,
5532 startoff
= got
.br_startoff
+ offset_shift_fsb
;
5533 /* nothing to move if this is the last extent */
5534 if (*current_ext
>= (total_extents
- 1))
5535 goto update_current_ext
;
5537 * If this is not the last extent in the file, make sure there
5538 * is enough room between current extent and next extent for
5539 * accommodating the shift.
5541 adj_irecp
= xfs_iext_get_ext(ifp
, *current_ext
+ 1);
5542 xfs_bmbt_get_all(adj_irecp
, &adj_irec
);
5543 if (startoff
+ got
.br_blockcount
> adj_irec
.br_startoff
)
5546 * Unlike a left shift (which involves a hole punch),
5547 * a right shift does not modify extent neighbors
5548 * in any way. We should never find mergeable extents
5549 * in this scenario. Check anyways and warn if we
5550 * encounter two extents that could be one.
5552 if (xfs_bmse_can_merge(&got
, &adj_irec
, offset_shift_fsb
))
5556 * Increment the extent index for the next iteration, update the start
5557 * offset of the in-core extent and update the btree if applicable.
5560 if (direction
== SHIFT_LEFT
)
5564 xfs_bmbt_set_startoff(gotp
, startoff
);
5565 *logflags
|= XFS_ILOG_CORE
;
5567 *logflags
|= XFS_ILOG_DEXT
;
5571 error
= xfs_bmbt_lookup_eq(cur
, got
.br_startoff
, got
.br_startblock
,
5572 got
.br_blockcount
, &i
);
5575 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
5577 got
.br_startoff
= startoff
;
5578 return xfs_bmbt_update(cur
, got
.br_startoff
, got
.br_startblock
,
5579 got
.br_blockcount
, got
.br_state
);
5583 * Shift extent records to the left/right to cover/create a hole.
5585 * The maximum number of extents to be shifted in a single operation is
5586 * @num_exts. @stop_fsb specifies the file offset at which to stop shift and the
5587 * file offset where we've left off is returned in @next_fsb. @offset_shift_fsb
5588 * is the length by which each extent is shifted. If there is no hole to shift
5589 * the extents into, this will be considered invalid operation and we abort
5593 xfs_bmap_shift_extents(
5594 struct xfs_trans
*tp
,
5595 struct xfs_inode
*ip
,
5596 xfs_fileoff_t
*next_fsb
,
5597 xfs_fileoff_t offset_shift_fsb
,
5599 xfs_fileoff_t stop_fsb
,
5600 xfs_fsblock_t
*firstblock
,
5601 struct xfs_bmap_free
*flist
,
5602 enum shift_direction direction
,
5605 struct xfs_btree_cur
*cur
= NULL
;
5606 struct xfs_bmbt_rec_host
*gotp
;
5607 struct xfs_bmbt_irec got
;
5608 struct xfs_mount
*mp
= ip
->i_mount
;
5609 struct xfs_ifork
*ifp
;
5610 xfs_extnum_t nexts
= 0;
5611 xfs_extnum_t current_ext
;
5612 xfs_extnum_t total_extents
;
5613 xfs_extnum_t stop_extent
;
5615 int whichfork
= XFS_DATA_FORK
;
5618 if (unlikely(XFS_TEST_ERROR(
5619 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
5620 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
5621 mp
, XFS_ERRTAG_BMAPIFORMAT
, XFS_RANDOM_BMAPIFORMAT
))) {
5622 XFS_ERROR_REPORT("xfs_bmap_shift_extents",
5623 XFS_ERRLEVEL_LOW
, mp
);
5624 return -EFSCORRUPTED
;
5627 if (XFS_FORCED_SHUTDOWN(mp
))
5630 ASSERT(xfs_isilocked(ip
, XFS_IOLOCK_EXCL
));
5631 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
5632 ASSERT(direction
== SHIFT_LEFT
|| direction
== SHIFT_RIGHT
);
5633 ASSERT(*next_fsb
!= NULLFSBLOCK
|| direction
== SHIFT_RIGHT
);
5635 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5636 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
5637 /* Read in all the extents */
5638 error
= xfs_iread_extents(tp
, ip
, whichfork
);
5643 if (ifp
->if_flags
& XFS_IFBROOT
) {
5644 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
5645 cur
->bc_private
.b
.firstblock
= *firstblock
;
5646 cur
->bc_private
.b
.flist
= flist
;
5647 cur
->bc_private
.b
.flags
= 0;
5651 * There may be delalloc extents in the data fork before the range we
5652 * are collapsing out, so we cannot use the count of real extents here.
5653 * Instead we have to calculate it from the incore fork.
5655 total_extents
= ifp
->if_bytes
/ sizeof(xfs_bmbt_rec_t
);
5656 if (total_extents
== 0) {
5662 * In case of first right shift, we need to initialize next_fsb
5664 if (*next_fsb
== NULLFSBLOCK
) {
5665 gotp
= xfs_iext_get_ext(ifp
, total_extents
- 1);
5666 xfs_bmbt_get_all(gotp
, &got
);
5667 *next_fsb
= got
.br_startoff
;
5668 if (stop_fsb
> *next_fsb
) {
5674 /* Lookup the extent index at which we have to stop */
5675 if (direction
== SHIFT_RIGHT
) {
5676 gotp
= xfs_iext_bno_to_ext(ifp
, stop_fsb
, &stop_extent
);
5677 /* Make stop_extent exclusive of shift range */
5680 stop_extent
= total_extents
;
5683 * Look up the extent index for the fsb where we start shifting. We can
5684 * henceforth iterate with current_ext as extent list changes are locked
5687 * gotp can be null in 2 cases: 1) if there are no extents or 2)
5688 * *next_fsb lies in a hole beyond which there are no extents. Either
5691 gotp
= xfs_iext_bno_to_ext(ifp
, *next_fsb
, ¤t_ext
);
5697 /* some sanity checking before we finally start shifting extents */
5698 if ((direction
== SHIFT_LEFT
&& current_ext
>= stop_extent
) ||
5699 (direction
== SHIFT_RIGHT
&& current_ext
<= stop_extent
)) {
5704 while (nexts
++ < num_exts
) {
5705 error
= xfs_bmse_shift_one(ip
, whichfork
, offset_shift_fsb
,
5706 ¤t_ext
, gotp
, cur
, &logflags
,
5711 * If there was an extent merge during the shift, the extent
5712 * count can change. Update the total and grade the next record.
5714 if (direction
== SHIFT_LEFT
) {
5715 total_extents
= ifp
->if_bytes
/ sizeof(xfs_bmbt_rec_t
);
5716 stop_extent
= total_extents
;
5719 if (current_ext
== stop_extent
) {
5721 *next_fsb
= NULLFSBLOCK
;
5724 gotp
= xfs_iext_get_ext(ifp
, current_ext
);
5728 xfs_bmbt_get_all(gotp
, &got
);
5729 *next_fsb
= got
.br_startoff
;
5734 xfs_btree_del_cursor(cur
,
5735 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
5738 xfs_trans_log_inode(tp
, ip
, logflags
);
5744 * Splits an extent into two extents at split_fsb block such that it is
5745 * the first block of the current_ext. @current_ext is a target extent
5746 * to be split. @split_fsb is a block where the extents is split.
5747 * If split_fsb lies in a hole or the first block of extents, just return 0.
5750 xfs_bmap_split_extent_at(
5751 struct xfs_trans
*tp
,
5752 struct xfs_inode
*ip
,
5753 xfs_fileoff_t split_fsb
,
5754 xfs_fsblock_t
*firstfsb
,
5755 struct xfs_bmap_free
*free_list
)
5757 int whichfork
= XFS_DATA_FORK
;
5758 struct xfs_btree_cur
*cur
= NULL
;
5759 struct xfs_bmbt_rec_host
*gotp
;
5760 struct xfs_bmbt_irec got
;
5761 struct xfs_bmbt_irec
new; /* split extent */
5762 struct xfs_mount
*mp
= ip
->i_mount
;
5763 struct xfs_ifork
*ifp
;
5764 xfs_fsblock_t gotblkcnt
; /* new block count for got */
5765 xfs_extnum_t current_ext
;
5770 if (unlikely(XFS_TEST_ERROR(
5771 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
5772 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
5773 mp
, XFS_ERRTAG_BMAPIFORMAT
, XFS_RANDOM_BMAPIFORMAT
))) {
5774 XFS_ERROR_REPORT("xfs_bmap_split_extent_at",
5775 XFS_ERRLEVEL_LOW
, mp
);
5776 return -EFSCORRUPTED
;
5779 if (XFS_FORCED_SHUTDOWN(mp
))
5782 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5783 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
5784 /* Read in all the extents */
5785 error
= xfs_iread_extents(tp
, ip
, whichfork
);
5791 * gotp can be null in 2 cases: 1) if there are no extents
5792 * or 2) split_fsb lies in a hole beyond which there are
5793 * no extents. Either way, we are done.
5795 gotp
= xfs_iext_bno_to_ext(ifp
, split_fsb
, ¤t_ext
);
5799 xfs_bmbt_get_all(gotp
, &got
);
5802 * Check split_fsb lies in a hole or the start boundary offset
5805 if (got
.br_startoff
>= split_fsb
)
5808 gotblkcnt
= split_fsb
- got
.br_startoff
;
5809 new.br_startoff
= split_fsb
;
5810 new.br_startblock
= got
.br_startblock
+ gotblkcnt
;
5811 new.br_blockcount
= got
.br_blockcount
- gotblkcnt
;
5812 new.br_state
= got
.br_state
;
5814 if (ifp
->if_flags
& XFS_IFBROOT
) {
5815 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
5816 cur
->bc_private
.b
.firstblock
= *firstfsb
;
5817 cur
->bc_private
.b
.flist
= free_list
;
5818 cur
->bc_private
.b
.flags
= 0;
5819 error
= xfs_bmbt_lookup_eq(cur
, got
.br_startoff
,
5825 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, del_cursor
);
5828 xfs_bmbt_set_blockcount(gotp
, gotblkcnt
);
5829 got
.br_blockcount
= gotblkcnt
;
5831 logflags
= XFS_ILOG_CORE
;
5833 error
= xfs_bmbt_update(cur
, got
.br_startoff
,
5840 logflags
|= XFS_ILOG_DEXT
;
5842 /* Add new extent */
5844 xfs_iext_insert(ip
, current_ext
, 1, &new, 0);
5845 XFS_IFORK_NEXT_SET(ip
, whichfork
,
5846 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
5849 error
= xfs_bmbt_lookup_eq(cur
, new.br_startoff
,
5850 new.br_startblock
, new.br_blockcount
,
5854 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, del_cursor
);
5855 cur
->bc_rec
.b
.br_state
= new.br_state
;
5857 error
= xfs_btree_insert(cur
, &i
);
5860 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, del_cursor
);
5864 * Convert to a btree if necessary.
5866 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
5867 int tmp_logflags
; /* partial log flag return val */
5869 ASSERT(cur
== NULL
);
5870 error
= xfs_bmap_extents_to_btree(tp
, ip
, firstfsb
, free_list
,
5871 &cur
, 0, &tmp_logflags
, whichfork
);
5872 logflags
|= tmp_logflags
;
5877 cur
->bc_private
.b
.allocated
= 0;
5878 xfs_btree_del_cursor(cur
,
5879 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
5883 xfs_trans_log_inode(tp
, ip
, logflags
);
5888 xfs_bmap_split_extent(
5889 struct xfs_inode
*ip
,
5890 xfs_fileoff_t split_fsb
)
5892 struct xfs_mount
*mp
= ip
->i_mount
;
5893 struct xfs_trans
*tp
;
5894 struct xfs_bmap_free free_list
;
5895 xfs_fsblock_t firstfsb
;
5899 tp
= xfs_trans_alloc(mp
, XFS_TRANS_DIOSTRAT
);
5900 error
= xfs_trans_reserve(tp
, &M_RES(mp
)->tr_write
,
5901 XFS_DIOSTRAT_SPACE_RES(mp
, 0), 0);
5903 xfs_trans_cancel(tp
);
5907 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
5908 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
);
5910 xfs_bmap_init(&free_list
, &firstfsb
);
5912 error
= xfs_bmap_split_extent_at(tp
, ip
, split_fsb
,
5913 &firstfsb
, &free_list
);
5917 error
= xfs_bmap_finish(&tp
, &free_list
, &committed
);
5921 return xfs_trans_commit(tp
);
5924 xfs_trans_cancel(tp
);