2 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 #include "xfs_shared.h"
21 #include "xfs_format.h"
22 #include "xfs_log_format.h"
23 #include "xfs_trans_resv.h"
26 #include "xfs_mount.h"
27 #include "xfs_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_inode_item.h"
34 #include "xfs_extfree_item.h"
35 #include "xfs_alloc.h"
37 #include "xfs_bmap_util.h"
38 #include "xfs_bmap_btree.h"
39 #include "xfs_rtalloc.h"
40 #include "xfs_error.h"
41 #include "xfs_quota.h"
42 #include "xfs_trans_space.h"
43 #include "xfs_buf_item.h"
44 #include "xfs_trace.h"
45 #include "xfs_symlink.h"
46 #include "xfs_attr_leaf.h"
47 #include "xfs_filestream.h"
50 kmem_zone_t
*xfs_bmap_free_item_zone
;
53 * Miscellaneous helper functions
57 * Compute and fill in the value of the maximum depth of a bmap btree
58 * in this filesystem. Done once, during mount.
61 xfs_bmap_compute_maxlevels(
62 xfs_mount_t
*mp
, /* file system mount structure */
63 int whichfork
) /* data or attr fork */
65 int level
; /* btree level */
66 uint maxblocks
; /* max blocks at this level */
67 uint maxleafents
; /* max leaf entries possible */
68 int maxrootrecs
; /* max records in root block */
69 int minleafrecs
; /* min records in leaf block */
70 int minnoderecs
; /* min records in node block */
71 int sz
; /* root block size */
74 * The maximum number of extents in a file, hence the maximum
75 * number of leaf entries, is controlled by the type of di_nextents
76 * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
77 * (a signed 16-bit number, xfs_aextnum_t).
79 * Note that we can no longer assume that if we are in ATTR1 that
80 * the fork offset of all the inodes will be
81 * (xfs_default_attroffset(ip) >> 3) because we could have mounted
82 * with ATTR2 and then mounted back with ATTR1, keeping the
83 * di_forkoff's fixed but probably at various positions. Therefore,
84 * for both ATTR1 and ATTR2 we have to assume the worst case scenario
85 * of a minimum size available.
87 if (whichfork
== XFS_DATA_FORK
) {
88 maxleafents
= MAXEXTNUM
;
89 sz
= XFS_BMDR_SPACE_CALC(MINDBTPTRS
);
91 maxleafents
= MAXAEXTNUM
;
92 sz
= XFS_BMDR_SPACE_CALC(MINABTPTRS
);
94 maxrootrecs
= xfs_bmdr_maxrecs(sz
, 0);
95 minleafrecs
= mp
->m_bmap_dmnr
[0];
96 minnoderecs
= mp
->m_bmap_dmnr
[1];
97 maxblocks
= (maxleafents
+ minleafrecs
- 1) / minleafrecs
;
98 for (level
= 1; maxblocks
> 1; level
++) {
99 if (maxblocks
<= maxrootrecs
)
102 maxblocks
= (maxblocks
+ minnoderecs
- 1) / minnoderecs
;
104 mp
->m_bm_maxlevels
[whichfork
] = level
;
107 STATIC
int /* error */
109 struct xfs_btree_cur
*cur
,
113 int *stat
) /* success/failure */
115 cur
->bc_rec
.b
.br_startoff
= off
;
116 cur
->bc_rec
.b
.br_startblock
= bno
;
117 cur
->bc_rec
.b
.br_blockcount
= len
;
118 return xfs_btree_lookup(cur
, XFS_LOOKUP_EQ
, stat
);
121 STATIC
int /* error */
123 struct xfs_btree_cur
*cur
,
127 int *stat
) /* success/failure */
129 cur
->bc_rec
.b
.br_startoff
= off
;
130 cur
->bc_rec
.b
.br_startblock
= bno
;
131 cur
->bc_rec
.b
.br_blockcount
= len
;
132 return xfs_btree_lookup(cur
, XFS_LOOKUP_GE
, stat
);
136 * Check if the inode needs to be converted to btree format.
138 static inline bool xfs_bmap_needs_btree(struct xfs_inode
*ip
, int whichfork
)
140 return XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
&&
141 XFS_IFORK_NEXTENTS(ip
, whichfork
) >
142 XFS_IFORK_MAXEXT(ip
, whichfork
);
146 * Check if the inode should be converted to extent format.
148 static inline bool xfs_bmap_wants_extents(struct xfs_inode
*ip
, int whichfork
)
150 return XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
&&
151 XFS_IFORK_NEXTENTS(ip
, whichfork
) <=
152 XFS_IFORK_MAXEXT(ip
, whichfork
);
156 * Update the record referred to by cur to the value given
157 * by [off, bno, len, state].
158 * This either works (return 0) or gets an EFSCORRUPTED error.
162 struct xfs_btree_cur
*cur
,
168 union xfs_btree_rec rec
;
170 xfs_bmbt_disk_set_allf(&rec
.bmbt
, off
, bno
, len
, state
);
171 return xfs_btree_update(cur
, &rec
);
175 * Compute the worst-case number of indirect blocks that will be used
176 * for ip's delayed extent of length "len".
179 xfs_bmap_worst_indlen(
180 xfs_inode_t
*ip
, /* incore inode pointer */
181 xfs_filblks_t len
) /* delayed extent length */
183 int level
; /* btree level number */
184 int maxrecs
; /* maximum record count at this level */
185 xfs_mount_t
*mp
; /* mount structure */
186 xfs_filblks_t rval
; /* return value */
189 maxrecs
= mp
->m_bmap_dmxr
[0];
190 for (level
= 0, rval
= 0;
191 level
< XFS_BM_MAXLEVELS(mp
, XFS_DATA_FORK
);
194 do_div(len
, maxrecs
);
197 return rval
+ XFS_BM_MAXLEVELS(mp
, XFS_DATA_FORK
) -
200 maxrecs
= mp
->m_bmap_dmxr
[1];
206 * Calculate the default attribute fork offset for newly created inodes.
209 xfs_default_attroffset(
210 struct xfs_inode
*ip
)
212 struct xfs_mount
*mp
= ip
->i_mount
;
215 if (mp
->m_sb
.sb_inodesize
== 256) {
216 offset
= XFS_LITINO(mp
, ip
->i_d
.di_version
) -
217 XFS_BMDR_SPACE_CALC(MINABTPTRS
);
219 offset
= XFS_BMDR_SPACE_CALC(6 * MINABTPTRS
);
222 ASSERT(offset
< XFS_LITINO(mp
, ip
->i_d
.di_version
));
227 * Helper routine to reset inode di_forkoff field when switching
228 * attribute fork from local to extent format - we reset it where
229 * possible to make space available for inline data fork extents.
232 xfs_bmap_forkoff_reset(
236 if (whichfork
== XFS_ATTR_FORK
&&
237 ip
->i_d
.di_format
!= XFS_DINODE_FMT_DEV
&&
238 ip
->i_d
.di_format
!= XFS_DINODE_FMT_UUID
&&
239 ip
->i_d
.di_format
!= XFS_DINODE_FMT_BTREE
) {
240 uint dfl_forkoff
= xfs_default_attroffset(ip
) >> 3;
242 if (dfl_forkoff
> ip
->i_d
.di_forkoff
)
243 ip
->i_d
.di_forkoff
= dfl_forkoff
;
248 STATIC
struct xfs_buf
*
250 struct xfs_btree_cur
*cur
,
253 struct xfs_log_item_desc
*lidp
;
259 for (i
= 0; i
< XFS_BTREE_MAXLEVELS
; i
++) {
260 if (!cur
->bc_bufs
[i
])
262 if (XFS_BUF_ADDR(cur
->bc_bufs
[i
]) == bno
)
263 return cur
->bc_bufs
[i
];
266 /* Chase down all the log items to see if the bp is there */
267 list_for_each_entry(lidp
, &cur
->bc_tp
->t_items
, lid_trans
) {
268 struct xfs_buf_log_item
*bip
;
269 bip
= (struct xfs_buf_log_item
*)lidp
->lid_item
;
270 if (bip
->bli_item
.li_type
== XFS_LI_BUF
&&
271 XFS_BUF_ADDR(bip
->bli_buf
) == bno
)
280 struct xfs_btree_block
*block
,
286 __be64
*pp
, *thispa
; /* pointer to block address */
287 xfs_bmbt_key_t
*prevp
, *keyp
;
289 ASSERT(be16_to_cpu(block
->bb_level
) > 0);
292 for( i
= 1; i
<= xfs_btree_get_numrecs(block
); i
++) {
293 dmxr
= mp
->m_bmap_dmxr
[0];
294 keyp
= XFS_BMBT_KEY_ADDR(mp
, block
, i
);
297 ASSERT(be64_to_cpu(prevp
->br_startoff
) <
298 be64_to_cpu(keyp
->br_startoff
));
303 * Compare the block numbers to see if there are dups.
306 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, i
, sz
);
308 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, i
, dmxr
);
310 for (j
= i
+1; j
<= be16_to_cpu(block
->bb_numrecs
); j
++) {
312 thispa
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, j
, sz
);
314 thispa
= XFS_BMBT_PTR_ADDR(mp
, block
, j
, dmxr
);
315 if (*thispa
== *pp
) {
316 xfs_warn(mp
, "%s: thispa(%d) == pp(%d) %Ld",
318 (unsigned long long)be64_to_cpu(*thispa
));
319 panic("%s: ptrs are equal in node\n",
327 * Check that the extents for the inode ip are in the right order in all
332 xfs_bmap_check_leaf_extents(
333 xfs_btree_cur_t
*cur
, /* btree cursor or null */
334 xfs_inode_t
*ip
, /* incore inode pointer */
335 int whichfork
) /* data or attr fork */
337 struct xfs_btree_block
*block
; /* current btree block */
338 xfs_fsblock_t bno
; /* block # of "block" */
339 xfs_buf_t
*bp
; /* buffer for "block" */
340 int error
; /* error return value */
341 xfs_extnum_t i
=0, j
; /* index into the extents list */
342 xfs_ifork_t
*ifp
; /* fork structure */
343 int level
; /* btree level, for checking */
344 xfs_mount_t
*mp
; /* file system mount structure */
345 __be64
*pp
; /* pointer to block address */
346 xfs_bmbt_rec_t
*ep
; /* pointer to current extent */
347 xfs_bmbt_rec_t last
= {0, 0}; /* last extent in prev block */
348 xfs_bmbt_rec_t
*nextp
; /* pointer to next extent */
351 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
) {
357 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
358 block
= ifp
->if_broot
;
360 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
362 level
= be16_to_cpu(block
->bb_level
);
364 xfs_check_block(block
, mp
, 1, ifp
->if_broot_bytes
);
365 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, 1, ifp
->if_broot_bytes
);
366 bno
= be64_to_cpu(*pp
);
368 ASSERT(bno
!= NULLFSBLOCK
);
369 ASSERT(XFS_FSB_TO_AGNO(mp
, bno
) < mp
->m_sb
.sb_agcount
);
370 ASSERT(XFS_FSB_TO_AGBNO(mp
, bno
) < mp
->m_sb
.sb_agblocks
);
373 * Go down the tree until leaf level is reached, following the first
374 * pointer (leftmost) at each level.
376 while (level
-- > 0) {
377 /* See if buf is in cur first */
379 bp
= xfs_bmap_get_bp(cur
, XFS_FSB_TO_DADDR(mp
, bno
));
382 error
= xfs_btree_read_bufl(mp
, NULL
, bno
, 0, &bp
,
388 block
= XFS_BUF_TO_BLOCK(bp
);
393 * Check this block for basic sanity (increasing keys and
394 * no duplicate blocks).
397 xfs_check_block(block
, mp
, 0, 0);
398 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, mp
->m_bmap_dmxr
[1]);
399 bno
= be64_to_cpu(*pp
);
400 XFS_WANT_CORRUPTED_GOTO(mp
,
401 XFS_FSB_SANITY_CHECK(mp
, bno
), error0
);
404 xfs_trans_brelse(NULL
, bp
);
409 * Here with bp and block set to the leftmost leaf node in the tree.
414 * Loop over all leaf nodes checking that all extents are in the right order.
417 xfs_fsblock_t nextbno
;
418 xfs_extnum_t num_recs
;
421 num_recs
= xfs_btree_get_numrecs(block
);
424 * Read-ahead the next leaf block, if any.
427 nextbno
= be64_to_cpu(block
->bb_u
.l
.bb_rightsib
);
430 * Check all the extents to make sure they are OK.
431 * If we had a previous block, the last entry should
432 * conform with the first entry in this one.
435 ep
= XFS_BMBT_REC_ADDR(mp
, block
, 1);
437 ASSERT(xfs_bmbt_disk_get_startoff(&last
) +
438 xfs_bmbt_disk_get_blockcount(&last
) <=
439 xfs_bmbt_disk_get_startoff(ep
));
441 for (j
= 1; j
< num_recs
; j
++) {
442 nextp
= XFS_BMBT_REC_ADDR(mp
, block
, j
+ 1);
443 ASSERT(xfs_bmbt_disk_get_startoff(ep
) +
444 xfs_bmbt_disk_get_blockcount(ep
) <=
445 xfs_bmbt_disk_get_startoff(nextp
));
453 xfs_trans_brelse(NULL
, bp
);
457 * If we've reached the end, stop.
459 if (bno
== NULLFSBLOCK
)
463 bp
= xfs_bmap_get_bp(cur
, XFS_FSB_TO_DADDR(mp
, bno
));
466 error
= xfs_btree_read_bufl(mp
, NULL
, bno
, 0, &bp
,
472 block
= XFS_BUF_TO_BLOCK(bp
);
476 xfs_trans_brelse(NULL
, bp
);
481 xfs_warn(mp
, "%s: at error0", __func__
);
483 xfs_trans_brelse(NULL
, bp
);
485 xfs_warn(mp
, "%s: BAD after btree leaves for %d extents",
487 panic("%s: CORRUPTED BTREE OR SOMETHING", __func__
);
492 * Add bmap trace insert entries for all the contents of the extent records.
495 xfs_bmap_trace_exlist(
496 xfs_inode_t
*ip
, /* incore inode pointer */
497 xfs_extnum_t cnt
, /* count of entries in the list */
498 int whichfork
, /* data or attr fork */
499 unsigned long caller_ip
)
501 xfs_extnum_t idx
; /* extent record index */
502 xfs_ifork_t
*ifp
; /* inode fork pointer */
505 if (whichfork
== XFS_ATTR_FORK
)
506 state
|= BMAP_ATTRFORK
;
508 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
509 ASSERT(cnt
== (ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
)));
510 for (idx
= 0; idx
< cnt
; idx
++)
511 trace_xfs_extlist(ip
, idx
, whichfork
, caller_ip
);
515 * Validate that the bmbt_irecs being returned from bmapi are valid
516 * given the caller's original parameters. Specifically check the
517 * ranges of the returned irecs to ensure that they only extend beyond
518 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
521 xfs_bmap_validate_ret(
525 xfs_bmbt_irec_t
*mval
,
529 int i
; /* index to map values */
531 ASSERT(ret_nmap
<= nmap
);
533 for (i
= 0; i
< ret_nmap
; i
++) {
534 ASSERT(mval
[i
].br_blockcount
> 0);
535 if (!(flags
& XFS_BMAPI_ENTIRE
)) {
536 ASSERT(mval
[i
].br_startoff
>= bno
);
537 ASSERT(mval
[i
].br_blockcount
<= len
);
538 ASSERT(mval
[i
].br_startoff
+ mval
[i
].br_blockcount
<=
541 ASSERT(mval
[i
].br_startoff
< bno
+ len
);
542 ASSERT(mval
[i
].br_startoff
+ mval
[i
].br_blockcount
>
546 mval
[i
- 1].br_startoff
+ mval
[i
- 1].br_blockcount
==
547 mval
[i
].br_startoff
);
548 ASSERT(mval
[i
].br_startblock
!= DELAYSTARTBLOCK
&&
549 mval
[i
].br_startblock
!= HOLESTARTBLOCK
);
550 ASSERT(mval
[i
].br_state
== XFS_EXT_NORM
||
551 mval
[i
].br_state
== XFS_EXT_UNWRITTEN
);
556 #define xfs_bmap_check_leaf_extents(cur, ip, whichfork) do { } while (0)
557 #define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap)
561 * bmap free list manipulation functions
565 * Add the extent to the list of extents to be free at transaction end.
566 * The list is maintained sorted (by block number).
570 xfs_fsblock_t bno
, /* fs block number of extent */
571 xfs_filblks_t len
, /* length of extent */
572 xfs_bmap_free_t
*flist
, /* list of extents */
573 xfs_mount_t
*mp
) /* mount point structure */
575 xfs_bmap_free_item_t
*cur
; /* current (next) element */
576 xfs_bmap_free_item_t
*new; /* new element */
577 xfs_bmap_free_item_t
*prev
; /* previous element */
582 ASSERT(bno
!= NULLFSBLOCK
);
584 ASSERT(len
<= MAXEXTLEN
);
585 ASSERT(!isnullstartblock(bno
));
586 agno
= XFS_FSB_TO_AGNO(mp
, bno
);
587 agbno
= XFS_FSB_TO_AGBNO(mp
, bno
);
588 ASSERT(agno
< mp
->m_sb
.sb_agcount
);
589 ASSERT(agbno
< mp
->m_sb
.sb_agblocks
);
590 ASSERT(len
< mp
->m_sb
.sb_agblocks
);
591 ASSERT(agbno
+ len
<= mp
->m_sb
.sb_agblocks
);
593 ASSERT(xfs_bmap_free_item_zone
!= NULL
);
594 new = kmem_zone_alloc(xfs_bmap_free_item_zone
, KM_SLEEP
);
595 new->xbfi_startblock
= bno
;
596 new->xbfi_blockcount
= (xfs_extlen_t
)len
;
597 for (prev
= NULL
, cur
= flist
->xbf_first
;
599 prev
= cur
, cur
= cur
->xbfi_next
) {
600 if (cur
->xbfi_startblock
>= bno
)
604 prev
->xbfi_next
= new;
606 flist
->xbf_first
= new;
607 new->xbfi_next
= cur
;
612 * Remove the entry "free" from the free item list. Prev points to the
613 * previous entry, unless "free" is the head of the list.
617 xfs_bmap_free_t
*flist
, /* free item list header */
618 xfs_bmap_free_item_t
*prev
, /* previous item on list, if any */
619 xfs_bmap_free_item_t
*free
) /* list item to be freed */
622 prev
->xbfi_next
= free
->xbfi_next
;
624 flist
->xbf_first
= free
->xbfi_next
;
626 kmem_zone_free(xfs_bmap_free_item_zone
, free
);
630 * Free up any items left in the list.
634 xfs_bmap_free_t
*flist
) /* list of bmap_free_items */
636 xfs_bmap_free_item_t
*free
; /* free list item */
637 xfs_bmap_free_item_t
*next
;
639 if (flist
->xbf_count
== 0)
641 ASSERT(flist
->xbf_first
!= NULL
);
642 for (free
= flist
->xbf_first
; free
; free
= next
) {
643 next
= free
->xbfi_next
;
644 xfs_bmap_del_free(flist
, NULL
, free
);
646 ASSERT(flist
->xbf_count
== 0);
650 * Inode fork format manipulation functions
654 * Transform a btree format file with only one leaf node, where the
655 * extents list will fit in the inode, into an extents format file.
656 * Since the file extents are already in-core, all we have to do is
657 * give up the space for the btree root and pitch the leaf block.
659 STATIC
int /* error */
660 xfs_bmap_btree_to_extents(
661 xfs_trans_t
*tp
, /* transaction pointer */
662 xfs_inode_t
*ip
, /* incore inode pointer */
663 xfs_btree_cur_t
*cur
, /* btree cursor */
664 int *logflagsp
, /* inode logging flags */
665 int whichfork
) /* data or attr fork */
668 struct xfs_btree_block
*cblock
;/* child btree block */
669 xfs_fsblock_t cbno
; /* child block number */
670 xfs_buf_t
*cbp
; /* child block's buffer */
671 int error
; /* error return value */
672 xfs_ifork_t
*ifp
; /* inode fork data */
673 xfs_mount_t
*mp
; /* mount point structure */
674 __be64
*pp
; /* ptr to block address */
675 struct xfs_btree_block
*rblock
;/* root btree block */
678 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
679 ASSERT(ifp
->if_flags
& XFS_IFEXTENTS
);
680 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
);
681 rblock
= ifp
->if_broot
;
682 ASSERT(be16_to_cpu(rblock
->bb_level
) == 1);
683 ASSERT(be16_to_cpu(rblock
->bb_numrecs
) == 1);
684 ASSERT(xfs_bmbt_maxrecs(mp
, ifp
->if_broot_bytes
, 0) == 1);
685 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, rblock
, 1, ifp
->if_broot_bytes
);
686 cbno
= be64_to_cpu(*pp
);
689 if ((error
= xfs_btree_check_lptr(cur
, cbno
, 1)))
692 error
= xfs_btree_read_bufl(mp
, tp
, cbno
, 0, &cbp
, XFS_BMAP_BTREE_REF
,
696 cblock
= XFS_BUF_TO_BLOCK(cbp
);
697 if ((error
= xfs_btree_check_block(cur
, cblock
, 0, cbp
)))
699 xfs_bmap_add_free(cbno
, 1, cur
->bc_private
.b
.flist
, mp
);
700 ip
->i_d
.di_nblocks
--;
701 xfs_trans_mod_dquot_byino(tp
, ip
, XFS_TRANS_DQ_BCOUNT
, -1L);
702 xfs_trans_binval(tp
, cbp
);
703 if (cur
->bc_bufs
[0] == cbp
)
704 cur
->bc_bufs
[0] = NULL
;
705 xfs_iroot_realloc(ip
, -1, whichfork
);
706 ASSERT(ifp
->if_broot
== NULL
);
707 ASSERT((ifp
->if_flags
& XFS_IFBROOT
) == 0);
708 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_EXTENTS
);
709 *logflagsp
= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
714 * Convert an extents-format file into a btree-format file.
715 * The new file will have a root block (in the inode) and a single child block.
717 STATIC
int /* error */
718 xfs_bmap_extents_to_btree(
719 xfs_trans_t
*tp
, /* transaction pointer */
720 xfs_inode_t
*ip
, /* incore inode pointer */
721 xfs_fsblock_t
*firstblock
, /* first-block-allocated */
722 xfs_bmap_free_t
*flist
, /* blocks freed in xaction */
723 xfs_btree_cur_t
**curp
, /* cursor returned to caller */
724 int wasdel
, /* converting a delayed alloc */
725 int *logflagsp
, /* inode logging flags */
726 int whichfork
) /* data or attr fork */
728 struct xfs_btree_block
*ablock
; /* allocated (child) bt block */
729 xfs_buf_t
*abp
; /* buffer for ablock */
730 xfs_alloc_arg_t args
; /* allocation arguments */
731 xfs_bmbt_rec_t
*arp
; /* child record pointer */
732 struct xfs_btree_block
*block
; /* btree root block */
733 xfs_btree_cur_t
*cur
; /* bmap btree cursor */
734 xfs_bmbt_rec_host_t
*ep
; /* extent record pointer */
735 int error
; /* error return value */
736 xfs_extnum_t i
, cnt
; /* extent record index */
737 xfs_ifork_t
*ifp
; /* inode fork pointer */
738 xfs_bmbt_key_t
*kp
; /* root block key pointer */
739 xfs_mount_t
*mp
; /* mount structure */
740 xfs_extnum_t nextents
; /* number of file extents */
741 xfs_bmbt_ptr_t
*pp
; /* root block address pointer */
744 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
745 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
);
748 * Make space in the inode incore.
750 xfs_iroot_realloc(ip
, 1, whichfork
);
751 ifp
->if_flags
|= XFS_IFBROOT
;
756 block
= ifp
->if_broot
;
757 if (xfs_sb_version_hascrc(&mp
->m_sb
))
758 xfs_btree_init_block_int(mp
, block
, XFS_BUF_DADDR_NULL
,
759 XFS_BMAP_CRC_MAGIC
, 1, 1, ip
->i_ino
,
760 XFS_BTREE_LONG_PTRS
| XFS_BTREE_CRC_BLOCKS
);
762 xfs_btree_init_block_int(mp
, block
, XFS_BUF_DADDR_NULL
,
763 XFS_BMAP_MAGIC
, 1, 1, ip
->i_ino
,
764 XFS_BTREE_LONG_PTRS
);
767 * Need a cursor. Can't allocate until bb_level is filled in.
769 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
770 cur
->bc_private
.b
.firstblock
= *firstblock
;
771 cur
->bc_private
.b
.flist
= flist
;
772 cur
->bc_private
.b
.flags
= wasdel
? XFS_BTCUR_BPRV_WASDEL
: 0;
774 * Convert to a btree with two levels, one record in root.
776 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_BTREE
);
777 memset(&args
, 0, sizeof(args
));
780 args
.firstblock
= *firstblock
;
781 if (*firstblock
== NULLFSBLOCK
) {
782 args
.type
= XFS_ALLOCTYPE_START_BNO
;
783 args
.fsbno
= XFS_INO_TO_FSB(mp
, ip
->i_ino
);
784 } else if (flist
->xbf_low
) {
785 args
.type
= XFS_ALLOCTYPE_START_BNO
;
786 args
.fsbno
= *firstblock
;
788 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
789 args
.fsbno
= *firstblock
;
791 args
.minlen
= args
.maxlen
= args
.prod
= 1;
792 args
.wasdel
= wasdel
;
794 if ((error
= xfs_alloc_vextent(&args
))) {
795 xfs_iroot_realloc(ip
, -1, whichfork
);
796 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
800 * Allocation can't fail, the space was reserved.
802 ASSERT(args
.fsbno
!= NULLFSBLOCK
);
803 ASSERT(*firstblock
== NULLFSBLOCK
||
804 args
.agno
== XFS_FSB_TO_AGNO(mp
, *firstblock
) ||
806 args
.agno
> XFS_FSB_TO_AGNO(mp
, *firstblock
)));
807 *firstblock
= cur
->bc_private
.b
.firstblock
= args
.fsbno
;
808 cur
->bc_private
.b
.allocated
++;
809 ip
->i_d
.di_nblocks
++;
810 xfs_trans_mod_dquot_byino(tp
, ip
, XFS_TRANS_DQ_BCOUNT
, 1L);
811 abp
= xfs_btree_get_bufl(mp
, tp
, args
.fsbno
, 0);
813 * Fill in the child block.
815 abp
->b_ops
= &xfs_bmbt_buf_ops
;
816 ablock
= XFS_BUF_TO_BLOCK(abp
);
817 if (xfs_sb_version_hascrc(&mp
->m_sb
))
818 xfs_btree_init_block_int(mp
, ablock
, abp
->b_bn
,
819 XFS_BMAP_CRC_MAGIC
, 0, 0, ip
->i_ino
,
820 XFS_BTREE_LONG_PTRS
| XFS_BTREE_CRC_BLOCKS
);
822 xfs_btree_init_block_int(mp
, ablock
, abp
->b_bn
,
823 XFS_BMAP_MAGIC
, 0, 0, ip
->i_ino
,
824 XFS_BTREE_LONG_PTRS
);
826 arp
= XFS_BMBT_REC_ADDR(mp
, ablock
, 1);
827 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
828 for (cnt
= i
= 0; i
< nextents
; i
++) {
829 ep
= xfs_iext_get_ext(ifp
, i
);
830 if (!isnullstartblock(xfs_bmbt_get_startblock(ep
))) {
831 arp
->l0
= cpu_to_be64(ep
->l0
);
832 arp
->l1
= cpu_to_be64(ep
->l1
);
836 ASSERT(cnt
== XFS_IFORK_NEXTENTS(ip
, whichfork
));
837 xfs_btree_set_numrecs(ablock
, cnt
);
840 * Fill in the root key and pointer.
842 kp
= XFS_BMBT_KEY_ADDR(mp
, block
, 1);
843 arp
= XFS_BMBT_REC_ADDR(mp
, ablock
, 1);
844 kp
->br_startoff
= cpu_to_be64(xfs_bmbt_disk_get_startoff(arp
));
845 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, xfs_bmbt_get_maxrecs(cur
,
846 be16_to_cpu(block
->bb_level
)));
847 *pp
= cpu_to_be64(args
.fsbno
);
850 * Do all this logging at the end so that
851 * the root is at the right level.
853 xfs_btree_log_block(cur
, abp
, XFS_BB_ALL_BITS
);
854 xfs_btree_log_recs(cur
, abp
, 1, be16_to_cpu(ablock
->bb_numrecs
));
855 ASSERT(*curp
== NULL
);
857 *logflagsp
= XFS_ILOG_CORE
| xfs_ilog_fbroot(whichfork
);
862 * Convert a local file to an extents file.
863 * This code is out of bounds for data forks of regular files,
864 * since the file data needs to get logged so things will stay consistent.
865 * (The bmap-level manipulations are ok, though).
868 xfs_bmap_local_to_extents_empty(
869 struct xfs_inode
*ip
,
872 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
874 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
875 ASSERT(ifp
->if_bytes
== 0);
876 ASSERT(XFS_IFORK_NEXTENTS(ip
, whichfork
) == 0);
878 xfs_bmap_forkoff_reset(ip
, whichfork
);
879 ifp
->if_flags
&= ~XFS_IFINLINE
;
880 ifp
->if_flags
|= XFS_IFEXTENTS
;
881 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_EXTENTS
);
885 STATIC
int /* error */
886 xfs_bmap_local_to_extents(
887 xfs_trans_t
*tp
, /* transaction pointer */
888 xfs_inode_t
*ip
, /* incore inode pointer */
889 xfs_fsblock_t
*firstblock
, /* first block allocated in xaction */
890 xfs_extlen_t total
, /* total blocks needed by transaction */
891 int *logflagsp
, /* inode logging flags */
893 void (*init_fn
)(struct xfs_trans
*tp
,
895 struct xfs_inode
*ip
,
896 struct xfs_ifork
*ifp
))
899 int flags
; /* logging flags returned */
900 xfs_ifork_t
*ifp
; /* inode fork pointer */
901 xfs_alloc_arg_t args
; /* allocation arguments */
902 xfs_buf_t
*bp
; /* buffer for extent block */
903 xfs_bmbt_rec_host_t
*ep
; /* extent record pointer */
906 * We don't want to deal with the case of keeping inode data inline yet.
907 * So sending the data fork of a regular inode is invalid.
909 ASSERT(!(S_ISREG(ip
->i_d
.di_mode
) && whichfork
== XFS_DATA_FORK
));
910 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
911 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
913 if (!ifp
->if_bytes
) {
914 xfs_bmap_local_to_extents_empty(ip
, whichfork
);
915 flags
= XFS_ILOG_CORE
;
921 ASSERT((ifp
->if_flags
& (XFS_IFINLINE
|XFS_IFEXTENTS
|XFS_IFEXTIREC
)) ==
923 memset(&args
, 0, sizeof(args
));
925 args
.mp
= ip
->i_mount
;
926 args
.firstblock
= *firstblock
;
928 * Allocate a block. We know we need only one, since the
929 * file currently fits in an inode.
931 if (*firstblock
== NULLFSBLOCK
) {
932 args
.fsbno
= XFS_INO_TO_FSB(args
.mp
, ip
->i_ino
);
933 args
.type
= XFS_ALLOCTYPE_START_BNO
;
935 args
.fsbno
= *firstblock
;
936 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
939 args
.minlen
= args
.maxlen
= args
.prod
= 1;
940 error
= xfs_alloc_vextent(&args
);
944 /* Can't fail, the space was reserved. */
945 ASSERT(args
.fsbno
!= NULLFSBLOCK
);
946 ASSERT(args
.len
== 1);
947 *firstblock
= args
.fsbno
;
948 bp
= xfs_btree_get_bufl(args
.mp
, tp
, args
.fsbno
, 0);
951 * Initialise the block and copy the data
953 * Note: init_fn must set the buffer log item type correctly!
955 init_fn(tp
, bp
, ip
, ifp
);
957 /* account for the change in fork size and log everything */
958 xfs_trans_log_buf(tp
, bp
, 0, ifp
->if_bytes
- 1);
959 xfs_idata_realloc(ip
, -ifp
->if_bytes
, whichfork
);
960 xfs_bmap_local_to_extents_empty(ip
, whichfork
);
961 flags
|= XFS_ILOG_CORE
;
963 xfs_iext_add(ifp
, 0, 1);
964 ep
= xfs_iext_get_ext(ifp
, 0);
965 xfs_bmbt_set_allf(ep
, 0, args
.fsbno
, 1, XFS_EXT_NORM
);
966 trace_xfs_bmap_post_update(ip
, 0,
967 whichfork
== XFS_ATTR_FORK
? BMAP_ATTRFORK
: 0,
969 XFS_IFORK_NEXT_SET(ip
, whichfork
, 1);
970 ip
->i_d
.di_nblocks
= 1;
971 xfs_trans_mod_dquot_byino(tp
, ip
,
972 XFS_TRANS_DQ_BCOUNT
, 1L);
973 flags
|= xfs_ilog_fext(whichfork
);
981 * Called from xfs_bmap_add_attrfork to handle btree format files.
983 STATIC
int /* error */
984 xfs_bmap_add_attrfork_btree(
985 xfs_trans_t
*tp
, /* transaction pointer */
986 xfs_inode_t
*ip
, /* incore inode pointer */
987 xfs_fsblock_t
*firstblock
, /* first block allocated */
988 xfs_bmap_free_t
*flist
, /* blocks to free at commit */
989 int *flags
) /* inode logging flags */
991 xfs_btree_cur_t
*cur
; /* btree cursor */
992 int error
; /* error return value */
993 xfs_mount_t
*mp
; /* file system mount struct */
994 int stat
; /* newroot status */
997 if (ip
->i_df
.if_broot_bytes
<= XFS_IFORK_DSIZE(ip
))
998 *flags
|= XFS_ILOG_DBROOT
;
1000 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, XFS_DATA_FORK
);
1001 cur
->bc_private
.b
.flist
= flist
;
1002 cur
->bc_private
.b
.firstblock
= *firstblock
;
1003 if ((error
= xfs_bmbt_lookup_ge(cur
, 0, 0, 0, &stat
)))
1005 /* must be at least one entry */
1006 XFS_WANT_CORRUPTED_GOTO(mp
, stat
== 1, error0
);
1007 if ((error
= xfs_btree_new_iroot(cur
, flags
, &stat
)))
1010 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
1013 *firstblock
= cur
->bc_private
.b
.firstblock
;
1014 cur
->bc_private
.b
.allocated
= 0;
1015 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
1019 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
1024 * Called from xfs_bmap_add_attrfork to handle extents format files.
1026 STATIC
int /* error */
1027 xfs_bmap_add_attrfork_extents(
1028 xfs_trans_t
*tp
, /* transaction pointer */
1029 xfs_inode_t
*ip
, /* incore inode pointer */
1030 xfs_fsblock_t
*firstblock
, /* first block allocated */
1031 xfs_bmap_free_t
*flist
, /* blocks to free at commit */
1032 int *flags
) /* inode logging flags */
1034 xfs_btree_cur_t
*cur
; /* bmap btree cursor */
1035 int error
; /* error return value */
1037 if (ip
->i_d
.di_nextents
* sizeof(xfs_bmbt_rec_t
) <= XFS_IFORK_DSIZE(ip
))
1040 error
= xfs_bmap_extents_to_btree(tp
, ip
, firstblock
, flist
, &cur
, 0,
1041 flags
, XFS_DATA_FORK
);
1043 cur
->bc_private
.b
.allocated
= 0;
1044 xfs_btree_del_cursor(cur
,
1045 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
1051 * Called from xfs_bmap_add_attrfork to handle local format files. Each
1052 * different data fork content type needs a different callout to do the
1053 * conversion. Some are basic and only require special block initialisation
1054 * callouts for the data formating, others (directories) are so specialised they
1055 * handle everything themselves.
1057 * XXX (dgc): investigate whether directory conversion can use the generic
1058 * formatting callout. It should be possible - it's just a very complex
1061 STATIC
int /* error */
1062 xfs_bmap_add_attrfork_local(
1063 xfs_trans_t
*tp
, /* transaction pointer */
1064 xfs_inode_t
*ip
, /* incore inode pointer */
1065 xfs_fsblock_t
*firstblock
, /* first block allocated */
1066 xfs_bmap_free_t
*flist
, /* blocks to free at commit */
1067 int *flags
) /* inode logging flags */
1069 xfs_da_args_t dargs
; /* args for dir/attr code */
1071 if (ip
->i_df
.if_bytes
<= XFS_IFORK_DSIZE(ip
))
1074 if (S_ISDIR(ip
->i_d
.di_mode
)) {
1075 memset(&dargs
, 0, sizeof(dargs
));
1076 dargs
.geo
= ip
->i_mount
->m_dir_geo
;
1078 dargs
.firstblock
= firstblock
;
1079 dargs
.flist
= flist
;
1080 dargs
.total
= dargs
.geo
->fsbcount
;
1081 dargs
.whichfork
= XFS_DATA_FORK
;
1083 return xfs_dir2_sf_to_block(&dargs
);
1086 if (S_ISLNK(ip
->i_d
.di_mode
))
1087 return xfs_bmap_local_to_extents(tp
, ip
, firstblock
, 1,
1088 flags
, XFS_DATA_FORK
,
1089 xfs_symlink_local_to_remote
);
1091 /* should only be called for types that support local format data */
1093 return -EFSCORRUPTED
;
1097 * Convert inode from non-attributed to attributed.
1098 * Must not be in a transaction, ip must not be locked.
1100 int /* error code */
1101 xfs_bmap_add_attrfork(
1102 xfs_inode_t
*ip
, /* incore inode pointer */
1103 int size
, /* space new attribute needs */
1104 int rsvd
) /* xact may use reserved blks */
1106 xfs_fsblock_t firstblock
; /* 1st block/ag allocated */
1107 xfs_bmap_free_t flist
; /* freed extent records */
1108 xfs_mount_t
*mp
; /* mount structure */
1109 xfs_trans_t
*tp
; /* transaction pointer */
1110 int blks
; /* space reservation */
1111 int version
= 1; /* superblock attr version */
1112 int committed
; /* xaction was committed */
1113 int logflags
; /* logging flags */
1114 int error
; /* error return value */
1116 ASSERT(XFS_IFORK_Q(ip
) == 0);
1119 ASSERT(!XFS_NOT_DQATTACHED(mp
, ip
));
1120 tp
= xfs_trans_alloc(mp
, XFS_TRANS_ADDAFORK
);
1121 blks
= XFS_ADDAFORK_SPACE_RES(mp
);
1123 tp
->t_flags
|= XFS_TRANS_RESERVE
;
1124 error
= xfs_trans_reserve(tp
, &M_RES(mp
)->tr_addafork
, blks
, 0);
1126 xfs_trans_cancel(tp
);
1129 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
1130 error
= xfs_trans_reserve_quota_nblks(tp
, ip
, blks
, 0, rsvd
?
1131 XFS_QMOPT_RES_REGBLKS
| XFS_QMOPT_FORCE_RES
:
1132 XFS_QMOPT_RES_REGBLKS
);
1135 if (XFS_IFORK_Q(ip
))
1137 if (ip
->i_d
.di_aformat
!= XFS_DINODE_FMT_EXTENTS
) {
1139 * For inodes coming from pre-6.2 filesystems.
1141 ASSERT(ip
->i_d
.di_aformat
== 0);
1142 ip
->i_d
.di_aformat
= XFS_DINODE_FMT_EXTENTS
;
1144 ASSERT(ip
->i_d
.di_anextents
== 0);
1146 xfs_trans_ijoin(tp
, ip
, 0);
1147 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
1149 switch (ip
->i_d
.di_format
) {
1150 case XFS_DINODE_FMT_DEV
:
1151 ip
->i_d
.di_forkoff
= roundup(sizeof(xfs_dev_t
), 8) >> 3;
1153 case XFS_DINODE_FMT_UUID
:
1154 ip
->i_d
.di_forkoff
= roundup(sizeof(uuid_t
), 8) >> 3;
1156 case XFS_DINODE_FMT_LOCAL
:
1157 case XFS_DINODE_FMT_EXTENTS
:
1158 case XFS_DINODE_FMT_BTREE
:
1159 ip
->i_d
.di_forkoff
= xfs_attr_shortform_bytesfit(ip
, size
);
1160 if (!ip
->i_d
.di_forkoff
)
1161 ip
->i_d
.di_forkoff
= xfs_default_attroffset(ip
) >> 3;
1162 else if (mp
->m_flags
& XFS_MOUNT_ATTR2
)
1171 ASSERT(ip
->i_afp
== NULL
);
1172 ip
->i_afp
= kmem_zone_zalloc(xfs_ifork_zone
, KM_SLEEP
);
1173 ip
->i_afp
->if_flags
= XFS_IFEXTENTS
;
1175 xfs_bmap_init(&flist
, &firstblock
);
1176 switch (ip
->i_d
.di_format
) {
1177 case XFS_DINODE_FMT_LOCAL
:
1178 error
= xfs_bmap_add_attrfork_local(tp
, ip
, &firstblock
, &flist
,
1181 case XFS_DINODE_FMT_EXTENTS
:
1182 error
= xfs_bmap_add_attrfork_extents(tp
, ip
, &firstblock
,
1185 case XFS_DINODE_FMT_BTREE
:
1186 error
= xfs_bmap_add_attrfork_btree(tp
, ip
, &firstblock
, &flist
,
1194 xfs_trans_log_inode(tp
, ip
, logflags
);
1197 if (!xfs_sb_version_hasattr(&mp
->m_sb
) ||
1198 (!xfs_sb_version_hasattr2(&mp
->m_sb
) && version
== 2)) {
1199 bool log_sb
= false;
1201 spin_lock(&mp
->m_sb_lock
);
1202 if (!xfs_sb_version_hasattr(&mp
->m_sb
)) {
1203 xfs_sb_version_addattr(&mp
->m_sb
);
1206 if (!xfs_sb_version_hasattr2(&mp
->m_sb
) && version
== 2) {
1207 xfs_sb_version_addattr2(&mp
->m_sb
);
1210 spin_unlock(&mp
->m_sb_lock
);
1215 error
= xfs_bmap_finish(&tp
, &flist
, &committed
);
1218 error
= xfs_trans_commit(tp
);
1219 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
1223 xfs_bmap_cancel(&flist
);
1225 xfs_trans_cancel(tp
);
1226 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
1231 * Internal and external extent tree search functions.
1235 * Read in the extents to if_extents.
1236 * All inode fields are set up by caller, we just traverse the btree
1237 * and copy the records in. If the file system cannot contain unwritten
1238 * extents, the records are checked for no "state" flags.
1241 xfs_bmap_read_extents(
1242 xfs_trans_t
*tp
, /* transaction pointer */
1243 xfs_inode_t
*ip
, /* incore inode */
1244 int whichfork
) /* data or attr fork */
1246 struct xfs_btree_block
*block
; /* current btree block */
1247 xfs_fsblock_t bno
; /* block # of "block" */
1248 xfs_buf_t
*bp
; /* buffer for "block" */
1249 int error
; /* error return value */
1250 xfs_exntfmt_t exntf
; /* XFS_EXTFMT_NOSTATE, if checking */
1251 xfs_extnum_t i
, j
; /* index into the extents list */
1252 xfs_ifork_t
*ifp
; /* fork structure */
1253 int level
; /* btree level, for checking */
1254 xfs_mount_t
*mp
; /* file system mount structure */
1255 __be64
*pp
; /* pointer to block address */
1257 xfs_extnum_t room
; /* number of entries there's room for */
1261 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1262 exntf
= (whichfork
!= XFS_DATA_FORK
) ? XFS_EXTFMT_NOSTATE
:
1263 XFS_EXTFMT_INODE(ip
);
1264 block
= ifp
->if_broot
;
1266 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
1268 level
= be16_to_cpu(block
->bb_level
);
1270 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, 1, ifp
->if_broot_bytes
);
1271 bno
= be64_to_cpu(*pp
);
1272 ASSERT(bno
!= NULLFSBLOCK
);
1273 ASSERT(XFS_FSB_TO_AGNO(mp
, bno
) < mp
->m_sb
.sb_agcount
);
1274 ASSERT(XFS_FSB_TO_AGBNO(mp
, bno
) < mp
->m_sb
.sb_agblocks
);
1276 * Go down the tree until leaf level is reached, following the first
1277 * pointer (leftmost) at each level.
1279 while (level
-- > 0) {
1280 error
= xfs_btree_read_bufl(mp
, tp
, bno
, 0, &bp
,
1281 XFS_BMAP_BTREE_REF
, &xfs_bmbt_buf_ops
);
1284 block
= XFS_BUF_TO_BLOCK(bp
);
1287 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, mp
->m_bmap_dmxr
[1]);
1288 bno
= be64_to_cpu(*pp
);
1289 XFS_WANT_CORRUPTED_GOTO(mp
,
1290 XFS_FSB_SANITY_CHECK(mp
, bno
), error0
);
1291 xfs_trans_brelse(tp
, bp
);
1294 * Here with bp and block set to the leftmost leaf node in the tree.
1296 room
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
1299 * Loop over all leaf nodes. Copy information to the extent records.
1302 xfs_bmbt_rec_t
*frp
;
1303 xfs_fsblock_t nextbno
;
1304 xfs_extnum_t num_recs
;
1307 num_recs
= xfs_btree_get_numrecs(block
);
1308 if (unlikely(i
+ num_recs
> room
)) {
1309 ASSERT(i
+ num_recs
<= room
);
1310 xfs_warn(ip
->i_mount
,
1311 "corrupt dinode %Lu, (btree extents).",
1312 (unsigned long long) ip
->i_ino
);
1313 XFS_CORRUPTION_ERROR("xfs_bmap_read_extents(1)",
1314 XFS_ERRLEVEL_LOW
, ip
->i_mount
, block
);
1318 * Read-ahead the next leaf block, if any.
1320 nextbno
= be64_to_cpu(block
->bb_u
.l
.bb_rightsib
);
1321 if (nextbno
!= NULLFSBLOCK
)
1322 xfs_btree_reada_bufl(mp
, nextbno
, 1,
1325 * Copy records into the extent records.
1327 frp
= XFS_BMBT_REC_ADDR(mp
, block
, 1);
1329 for (j
= 0; j
< num_recs
; j
++, i
++, frp
++) {
1330 xfs_bmbt_rec_host_t
*trp
= xfs_iext_get_ext(ifp
, i
);
1331 trp
->l0
= be64_to_cpu(frp
->l0
);
1332 trp
->l1
= be64_to_cpu(frp
->l1
);
1334 if (exntf
== XFS_EXTFMT_NOSTATE
) {
1336 * Check all attribute bmap btree records and
1337 * any "older" data bmap btree records for a
1338 * set bit in the "extent flag" position.
1340 if (unlikely(xfs_check_nostate_extents(ifp
,
1341 start
, num_recs
))) {
1342 XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
1348 xfs_trans_brelse(tp
, bp
);
1351 * If we've reached the end, stop.
1353 if (bno
== NULLFSBLOCK
)
1355 error
= xfs_btree_read_bufl(mp
, tp
, bno
, 0, &bp
,
1356 XFS_BMAP_BTREE_REF
, &xfs_bmbt_buf_ops
);
1359 block
= XFS_BUF_TO_BLOCK(bp
);
1361 ASSERT(i
== (ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
)));
1362 ASSERT(i
== XFS_IFORK_NEXTENTS(ip
, whichfork
));
1363 XFS_BMAP_TRACE_EXLIST(ip
, i
, whichfork
);
1366 xfs_trans_brelse(tp
, bp
);
1367 return -EFSCORRUPTED
;
1372 * Search the extent records for the entry containing block bno.
1373 * If bno lies in a hole, point to the next entry. If bno lies
1374 * past eof, *eofp will be set, and *prevp will contain the last
1375 * entry (null if none). Else, *lastxp will be set to the index
1376 * of the found entry; *gotp will contain the entry.
1378 STATIC xfs_bmbt_rec_host_t
* /* pointer to found extent entry */
1379 xfs_bmap_search_multi_extents(
1380 xfs_ifork_t
*ifp
, /* inode fork pointer */
1381 xfs_fileoff_t bno
, /* block number searched for */
1382 int *eofp
, /* out: end of file found */
1383 xfs_extnum_t
*lastxp
, /* out: last extent index */
1384 xfs_bmbt_irec_t
*gotp
, /* out: extent entry found */
1385 xfs_bmbt_irec_t
*prevp
) /* out: previous extent entry found */
1387 xfs_bmbt_rec_host_t
*ep
; /* extent record pointer */
1388 xfs_extnum_t lastx
; /* last extent index */
1391 * Initialize the extent entry structure to catch access to
1392 * uninitialized br_startblock field.
1394 gotp
->br_startoff
= 0xffa5a5a5a5a5a5a5LL
;
1395 gotp
->br_blockcount
= 0xa55a5a5a5a5a5a5aLL
;
1396 gotp
->br_state
= XFS_EXT_INVALID
;
1397 gotp
->br_startblock
= 0xffffa5a5a5a5a5a5LL
;
1398 prevp
->br_startoff
= NULLFILEOFF
;
1400 ep
= xfs_iext_bno_to_ext(ifp
, bno
, &lastx
);
1402 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, lastx
- 1), prevp
);
1404 if (lastx
< (ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
))) {
1405 xfs_bmbt_get_all(ep
, gotp
);
1419 * Search the extents list for the inode, for the extent containing bno.
1420 * If bno lies in a hole, point to the next entry. If bno lies past eof,
1421 * *eofp will be set, and *prevp will contain the last entry (null if none).
1422 * Else, *lastxp will be set to the index of the found
1423 * entry; *gotp will contain the entry.
1425 STATIC xfs_bmbt_rec_host_t
* /* pointer to found extent entry */
1426 xfs_bmap_search_extents(
1427 xfs_inode_t
*ip
, /* incore inode pointer */
1428 xfs_fileoff_t bno
, /* block number searched for */
1429 int fork
, /* data or attr fork */
1430 int *eofp
, /* out: end of file found */
1431 xfs_extnum_t
*lastxp
, /* out: last extent index */
1432 xfs_bmbt_irec_t
*gotp
, /* out: extent entry found */
1433 xfs_bmbt_irec_t
*prevp
) /* out: previous extent entry found */
1435 xfs_ifork_t
*ifp
; /* inode fork pointer */
1436 xfs_bmbt_rec_host_t
*ep
; /* extent record pointer */
1438 XFS_STATS_INC(xs_look_exlist
);
1439 ifp
= XFS_IFORK_PTR(ip
, fork
);
1441 ep
= xfs_bmap_search_multi_extents(ifp
, bno
, eofp
, lastxp
, gotp
, prevp
);
1443 if (unlikely(!(gotp
->br_startblock
) && (*lastxp
!= NULLEXTNUM
) &&
1444 !(XFS_IS_REALTIME_INODE(ip
) && fork
== XFS_DATA_FORK
))) {
1445 xfs_alert_tag(ip
->i_mount
, XFS_PTAG_FSBLOCK_ZERO
,
1446 "Access to block zero in inode %llu "
1447 "start_block: %llx start_off: %llx "
1448 "blkcnt: %llx extent-state: %x lastx: %x",
1449 (unsigned long long)ip
->i_ino
,
1450 (unsigned long long)gotp
->br_startblock
,
1451 (unsigned long long)gotp
->br_startoff
,
1452 (unsigned long long)gotp
->br_blockcount
,
1453 gotp
->br_state
, *lastxp
);
1454 *lastxp
= NULLEXTNUM
;
1462 * Returns the file-relative block number of the first unused block(s)
1463 * in the file with at least "len" logically contiguous blocks free.
1464 * This is the lowest-address hole if the file has holes, else the first block
1465 * past the end of file.
1466 * Return 0 if the file is currently local (in-inode).
1469 xfs_bmap_first_unused(
1470 xfs_trans_t
*tp
, /* transaction pointer */
1471 xfs_inode_t
*ip
, /* incore inode */
1472 xfs_extlen_t len
, /* size of hole to find */
1473 xfs_fileoff_t
*first_unused
, /* unused block */
1474 int whichfork
) /* data or attr fork */
1476 int error
; /* error return value */
1477 int idx
; /* extent record index */
1478 xfs_ifork_t
*ifp
; /* inode fork pointer */
1479 xfs_fileoff_t lastaddr
; /* last block number seen */
1480 xfs_fileoff_t lowest
; /* lowest useful block */
1481 xfs_fileoff_t max
; /* starting useful block */
1482 xfs_fileoff_t off
; /* offset for this block */
1483 xfs_extnum_t nextents
; /* number of extent entries */
1485 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
||
1486 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
||
1487 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
1488 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
) {
1492 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1493 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
1494 (error
= xfs_iread_extents(tp
, ip
, whichfork
)))
1496 lowest
= *first_unused
;
1497 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
1498 for (idx
= 0, lastaddr
= 0, max
= lowest
; idx
< nextents
; idx
++) {
1499 xfs_bmbt_rec_host_t
*ep
= xfs_iext_get_ext(ifp
, idx
);
1500 off
= xfs_bmbt_get_startoff(ep
);
1502 * See if the hole before this extent will work.
1504 if (off
>= lowest
+ len
&& off
- max
>= len
) {
1505 *first_unused
= max
;
1508 lastaddr
= off
+ xfs_bmbt_get_blockcount(ep
);
1509 max
= XFS_FILEOFF_MAX(lastaddr
, lowest
);
1511 *first_unused
= max
;
1516 * Returns the file-relative block number of the last block - 1 before
1517 * last_block (input value) in the file.
1518 * This is not based on i_size, it is based on the extent records.
1519 * Returns 0 for local files, as they do not have extent records.
1522 xfs_bmap_last_before(
1523 xfs_trans_t
*tp
, /* transaction pointer */
1524 xfs_inode_t
*ip
, /* incore inode */
1525 xfs_fileoff_t
*last_block
, /* last block */
1526 int whichfork
) /* data or attr fork */
1528 xfs_fileoff_t bno
; /* input file offset */
1529 int eof
; /* hit end of file */
1530 xfs_bmbt_rec_host_t
*ep
; /* pointer to last extent */
1531 int error
; /* error return value */
1532 xfs_bmbt_irec_t got
; /* current extent value */
1533 xfs_ifork_t
*ifp
; /* inode fork pointer */
1534 xfs_extnum_t lastx
; /* last extent used */
1535 xfs_bmbt_irec_t prev
; /* previous extent value */
1537 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
&&
1538 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
1539 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_LOCAL
)
1541 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
) {
1545 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1546 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
1547 (error
= xfs_iread_extents(tp
, ip
, whichfork
)))
1549 bno
= *last_block
- 1;
1550 ep
= xfs_bmap_search_extents(ip
, bno
, whichfork
, &eof
, &lastx
, &got
,
1552 if (eof
|| xfs_bmbt_get_startoff(ep
) > bno
) {
1553 if (prev
.br_startoff
== NULLFILEOFF
)
1556 *last_block
= prev
.br_startoff
+ prev
.br_blockcount
;
1559 * Otherwise *last_block is already the right answer.
1565 xfs_bmap_last_extent(
1566 struct xfs_trans
*tp
,
1567 struct xfs_inode
*ip
,
1569 struct xfs_bmbt_irec
*rec
,
1572 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1576 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
1577 error
= xfs_iread_extents(tp
, ip
, whichfork
);
1582 nextents
= ifp
->if_bytes
/ sizeof(xfs_bmbt_rec_t
);
1583 if (nextents
== 0) {
1588 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, nextents
- 1), rec
);
1594 * Check the last inode extent to determine whether this allocation will result
1595 * in blocks being allocated at the end of the file. When we allocate new data
1596 * blocks at the end of the file which do not start at the previous data block,
1597 * we will try to align the new blocks at stripe unit boundaries.
1599 * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be
1600 * at, or past the EOF.
1604 struct xfs_bmalloca
*bma
,
1607 struct xfs_bmbt_irec rec
;
1612 error
= xfs_bmap_last_extent(NULL
, bma
->ip
, whichfork
, &rec
,
1623 * Check if we are allocation or past the last extent, or at least into
1624 * the last delayed allocated extent.
1626 bma
->aeof
= bma
->offset
>= rec
.br_startoff
+ rec
.br_blockcount
||
1627 (bma
->offset
>= rec
.br_startoff
&&
1628 isnullstartblock(rec
.br_startblock
));
1633 * Returns the file-relative block number of the first block past eof in
1634 * the file. This is not based on i_size, it is based on the extent records.
1635 * Returns 0 for local files, as they do not have extent records.
1638 xfs_bmap_last_offset(
1639 struct xfs_inode
*ip
,
1640 xfs_fileoff_t
*last_block
,
1643 struct xfs_bmbt_irec rec
;
1649 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
)
1652 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
&&
1653 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
1656 error
= xfs_bmap_last_extent(NULL
, ip
, whichfork
, &rec
, &is_empty
);
1657 if (error
|| is_empty
)
1660 *last_block
= rec
.br_startoff
+ rec
.br_blockcount
;
1665 * Returns whether the selected fork of the inode has exactly one
1666 * block or not. For the data fork we check this matches di_size,
1667 * implying the file's range is 0..bsize-1.
1669 int /* 1=>1 block, 0=>otherwise */
1671 xfs_inode_t
*ip
, /* incore inode */
1672 int whichfork
) /* data or attr fork */
1674 xfs_bmbt_rec_host_t
*ep
; /* ptr to fork's extent */
1675 xfs_ifork_t
*ifp
; /* inode fork pointer */
1676 int rval
; /* return value */
1677 xfs_bmbt_irec_t s
; /* internal version of extent */
1680 if (whichfork
== XFS_DATA_FORK
)
1681 return XFS_ISIZE(ip
) == ip
->i_mount
->m_sb
.sb_blocksize
;
1683 if (XFS_IFORK_NEXTENTS(ip
, whichfork
) != 1)
1685 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
1687 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1688 ASSERT(ifp
->if_flags
& XFS_IFEXTENTS
);
1689 ep
= xfs_iext_get_ext(ifp
, 0);
1690 xfs_bmbt_get_all(ep
, &s
);
1691 rval
= s
.br_startoff
== 0 && s
.br_blockcount
== 1;
1692 if (rval
&& whichfork
== XFS_DATA_FORK
)
1693 ASSERT(XFS_ISIZE(ip
) == ip
->i_mount
->m_sb
.sb_blocksize
);
1698 * Extent tree manipulation functions used during allocation.
1702 * Convert a delayed allocation to a real allocation.
1704 STATIC
int /* error */
1705 xfs_bmap_add_extent_delay_real(
1706 struct xfs_bmalloca
*bma
)
1708 struct xfs_bmbt_irec
*new = &bma
->got
;
1709 int diff
; /* temp value */
1710 xfs_bmbt_rec_host_t
*ep
; /* extent entry for idx */
1711 int error
; /* error return value */
1712 int i
; /* temp state */
1713 xfs_ifork_t
*ifp
; /* inode fork pointer */
1714 xfs_fileoff_t new_endoff
; /* end offset of new entry */
1715 xfs_bmbt_irec_t r
[3]; /* neighbor extent entries */
1716 /* left is 0, right is 1, prev is 2 */
1717 int rval
=0; /* return value (logging flags) */
1718 int state
= 0;/* state bits, accessed thru macros */
1719 xfs_filblks_t da_new
; /* new count del alloc blocks used */
1720 xfs_filblks_t da_old
; /* old count del alloc blocks used */
1721 xfs_filblks_t temp
=0; /* value for da_new calculations */
1722 xfs_filblks_t temp2
=0;/* value for da_new calculations */
1723 int tmp_rval
; /* partial logging flags */
1724 struct xfs_mount
*mp
;
1726 mp
= bma
->tp
? bma
->tp
->t_mountp
: NULL
;
1727 ifp
= XFS_IFORK_PTR(bma
->ip
, XFS_DATA_FORK
);
1729 ASSERT(bma
->idx
>= 0);
1730 ASSERT(bma
->idx
<= ifp
->if_bytes
/ sizeof(struct xfs_bmbt_rec
));
1731 ASSERT(!isnullstartblock(new->br_startblock
));
1733 (bma
->cur
->bc_private
.b
.flags
& XFS_BTCUR_BPRV_WASDEL
));
1735 XFS_STATS_INC(xs_add_exlist
);
1742 * Set up a bunch of variables to make the tests simpler.
1744 ep
= xfs_iext_get_ext(ifp
, bma
->idx
);
1745 xfs_bmbt_get_all(ep
, &PREV
);
1746 new_endoff
= new->br_startoff
+ new->br_blockcount
;
1747 ASSERT(PREV
.br_startoff
<= new->br_startoff
);
1748 ASSERT(PREV
.br_startoff
+ PREV
.br_blockcount
>= new_endoff
);
1750 da_old
= startblockval(PREV
.br_startblock
);
1754 * Set flags determining what part of the previous delayed allocation
1755 * extent is being replaced by a real allocation.
1757 if (PREV
.br_startoff
== new->br_startoff
)
1758 state
|= BMAP_LEFT_FILLING
;
1759 if (PREV
.br_startoff
+ PREV
.br_blockcount
== new_endoff
)
1760 state
|= BMAP_RIGHT_FILLING
;
1763 * Check and set flags if this segment has a left neighbor.
1764 * Don't set contiguous if the combined extent would be too large.
1767 state
|= BMAP_LEFT_VALID
;
1768 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
- 1), &LEFT
);
1770 if (isnullstartblock(LEFT
.br_startblock
))
1771 state
|= BMAP_LEFT_DELAY
;
1774 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
1775 LEFT
.br_startoff
+ LEFT
.br_blockcount
== new->br_startoff
&&
1776 LEFT
.br_startblock
+ LEFT
.br_blockcount
== new->br_startblock
&&
1777 LEFT
.br_state
== new->br_state
&&
1778 LEFT
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
1779 state
|= BMAP_LEFT_CONTIG
;
1782 * Check and set flags if this segment has a right neighbor.
1783 * Don't set contiguous if the combined extent would be too large.
1784 * Also check for all-three-contiguous being too large.
1786 if (bma
->idx
< bma
->ip
->i_df
.if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
) - 1) {
1787 state
|= BMAP_RIGHT_VALID
;
1788 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
+ 1), &RIGHT
);
1790 if (isnullstartblock(RIGHT
.br_startblock
))
1791 state
|= BMAP_RIGHT_DELAY
;
1794 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
1795 new_endoff
== RIGHT
.br_startoff
&&
1796 new->br_startblock
+ new->br_blockcount
== RIGHT
.br_startblock
&&
1797 new->br_state
== RIGHT
.br_state
&&
1798 new->br_blockcount
+ RIGHT
.br_blockcount
<= MAXEXTLEN
&&
1799 ((state
& (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
1800 BMAP_RIGHT_FILLING
)) !=
1801 (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
1802 BMAP_RIGHT_FILLING
) ||
1803 LEFT
.br_blockcount
+ new->br_blockcount
+ RIGHT
.br_blockcount
1805 state
|= BMAP_RIGHT_CONTIG
;
1809 * Switch out based on the FILLING and CONTIG state bits.
1811 switch (state
& (BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
1812 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
)) {
1813 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
1814 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
1816 * Filling in all of a previously delayed allocation extent.
1817 * The left and right neighbors are both contiguous with new.
1820 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1821 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, bma
->idx
),
1822 LEFT
.br_blockcount
+ PREV
.br_blockcount
+
1823 RIGHT
.br_blockcount
);
1824 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1826 xfs_iext_remove(bma
->ip
, bma
->idx
+ 1, 2, state
);
1827 bma
->ip
->i_d
.di_nextents
--;
1828 if (bma
->cur
== NULL
)
1829 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1831 rval
= XFS_ILOG_CORE
;
1832 error
= xfs_bmbt_lookup_eq(bma
->cur
, RIGHT
.br_startoff
,
1833 RIGHT
.br_startblock
,
1834 RIGHT
.br_blockcount
, &i
);
1837 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1838 error
= xfs_btree_delete(bma
->cur
, &i
);
1841 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1842 error
= xfs_btree_decrement(bma
->cur
, 0, &i
);
1845 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1846 error
= xfs_bmbt_update(bma
->cur
, LEFT
.br_startoff
,
1848 LEFT
.br_blockcount
+
1849 PREV
.br_blockcount
+
1850 RIGHT
.br_blockcount
, LEFT
.br_state
);
1856 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
1858 * Filling in all of a previously delayed allocation extent.
1859 * The left neighbor is contiguous, the right is not.
1863 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1864 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, bma
->idx
),
1865 LEFT
.br_blockcount
+ PREV
.br_blockcount
);
1866 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1868 xfs_iext_remove(bma
->ip
, bma
->idx
+ 1, 1, state
);
1869 if (bma
->cur
== NULL
)
1870 rval
= XFS_ILOG_DEXT
;
1873 error
= xfs_bmbt_lookup_eq(bma
->cur
, LEFT
.br_startoff
,
1874 LEFT
.br_startblock
, LEFT
.br_blockcount
,
1878 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1879 error
= xfs_bmbt_update(bma
->cur
, LEFT
.br_startoff
,
1881 LEFT
.br_blockcount
+
1882 PREV
.br_blockcount
, LEFT
.br_state
);
1888 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
1890 * Filling in all of a previously delayed allocation extent.
1891 * The right neighbor is contiguous, the left is not.
1893 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1894 xfs_bmbt_set_startblock(ep
, new->br_startblock
);
1895 xfs_bmbt_set_blockcount(ep
,
1896 PREV
.br_blockcount
+ RIGHT
.br_blockcount
);
1897 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1899 xfs_iext_remove(bma
->ip
, bma
->idx
+ 1, 1, state
);
1900 if (bma
->cur
== NULL
)
1901 rval
= XFS_ILOG_DEXT
;
1904 error
= xfs_bmbt_lookup_eq(bma
->cur
, RIGHT
.br_startoff
,
1905 RIGHT
.br_startblock
,
1906 RIGHT
.br_blockcount
, &i
);
1909 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1910 error
= xfs_bmbt_update(bma
->cur
, PREV
.br_startoff
,
1912 PREV
.br_blockcount
+
1913 RIGHT
.br_blockcount
, PREV
.br_state
);
1919 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
1921 * Filling in all of a previously delayed allocation extent.
1922 * Neither the left nor right neighbors are contiguous with
1925 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1926 xfs_bmbt_set_startblock(ep
, new->br_startblock
);
1927 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1929 bma
->ip
->i_d
.di_nextents
++;
1930 if (bma
->cur
== NULL
)
1931 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1933 rval
= XFS_ILOG_CORE
;
1934 error
= xfs_bmbt_lookup_eq(bma
->cur
, new->br_startoff
,
1935 new->br_startblock
, new->br_blockcount
,
1939 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
1940 bma
->cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
1941 error
= xfs_btree_insert(bma
->cur
, &i
);
1944 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1948 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
:
1950 * Filling in the first part of a previous delayed allocation.
1951 * The left neighbor is contiguous.
1953 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
- 1, state
, _THIS_IP_
);
1954 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, bma
->idx
- 1),
1955 LEFT
.br_blockcount
+ new->br_blockcount
);
1956 xfs_bmbt_set_startoff(ep
,
1957 PREV
.br_startoff
+ new->br_blockcount
);
1958 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
- 1, state
, _THIS_IP_
);
1960 temp
= PREV
.br_blockcount
- new->br_blockcount
;
1961 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1962 xfs_bmbt_set_blockcount(ep
, temp
);
1963 if (bma
->cur
== NULL
)
1964 rval
= XFS_ILOG_DEXT
;
1967 error
= xfs_bmbt_lookup_eq(bma
->cur
, LEFT
.br_startoff
,
1968 LEFT
.br_startblock
, LEFT
.br_blockcount
,
1972 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1973 error
= xfs_bmbt_update(bma
->cur
, LEFT
.br_startoff
,
1975 LEFT
.br_blockcount
+
1981 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
1982 startblockval(PREV
.br_startblock
));
1983 xfs_bmbt_set_startblock(ep
, nullstartblock(da_new
));
1984 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1989 case BMAP_LEFT_FILLING
:
1991 * Filling in the first part of a previous delayed allocation.
1992 * The left neighbor is not contiguous.
1994 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1995 xfs_bmbt_set_startoff(ep
, new_endoff
);
1996 temp
= PREV
.br_blockcount
- new->br_blockcount
;
1997 xfs_bmbt_set_blockcount(ep
, temp
);
1998 xfs_iext_insert(bma
->ip
, bma
->idx
, 1, new, state
);
1999 bma
->ip
->i_d
.di_nextents
++;
2000 if (bma
->cur
== NULL
)
2001 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2003 rval
= XFS_ILOG_CORE
;
2004 error
= xfs_bmbt_lookup_eq(bma
->cur
, new->br_startoff
,
2005 new->br_startblock
, new->br_blockcount
,
2009 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
2010 bma
->cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
2011 error
= xfs_btree_insert(bma
->cur
, &i
);
2014 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2017 if (xfs_bmap_needs_btree(bma
->ip
, XFS_DATA_FORK
)) {
2018 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
2019 bma
->firstblock
, bma
->flist
,
2020 &bma
->cur
, 1, &tmp_rval
, XFS_DATA_FORK
);
2025 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
2026 startblockval(PREV
.br_startblock
) -
2027 (bma
->cur
? bma
->cur
->bc_private
.b
.allocated
: 0));
2028 ep
= xfs_iext_get_ext(ifp
, bma
->idx
+ 1);
2029 xfs_bmbt_set_startblock(ep
, nullstartblock(da_new
));
2030 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
+ 1, state
, _THIS_IP_
);
2033 case BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2035 * Filling in the last part of a previous delayed allocation.
2036 * The right neighbor is contiguous with the new allocation.
2038 temp
= PREV
.br_blockcount
- new->br_blockcount
;
2039 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
+ 1, state
, _THIS_IP_
);
2040 xfs_bmbt_set_blockcount(ep
, temp
);
2041 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp
, bma
->idx
+ 1),
2042 new->br_startoff
, new->br_startblock
,
2043 new->br_blockcount
+ RIGHT
.br_blockcount
,
2045 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
+ 1, state
, _THIS_IP_
);
2046 if (bma
->cur
== NULL
)
2047 rval
= XFS_ILOG_DEXT
;
2050 error
= xfs_bmbt_lookup_eq(bma
->cur
, RIGHT
.br_startoff
,
2051 RIGHT
.br_startblock
,
2052 RIGHT
.br_blockcount
, &i
);
2055 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2056 error
= xfs_bmbt_update(bma
->cur
, new->br_startoff
,
2058 new->br_blockcount
+
2059 RIGHT
.br_blockcount
,
2065 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
2066 startblockval(PREV
.br_startblock
));
2067 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2068 xfs_bmbt_set_startblock(ep
, nullstartblock(da_new
));
2069 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2074 case BMAP_RIGHT_FILLING
:
2076 * Filling in the last part of a previous delayed allocation.
2077 * The right neighbor is not contiguous.
2079 temp
= PREV
.br_blockcount
- new->br_blockcount
;
2080 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2081 xfs_bmbt_set_blockcount(ep
, temp
);
2082 xfs_iext_insert(bma
->ip
, bma
->idx
+ 1, 1, new, state
);
2083 bma
->ip
->i_d
.di_nextents
++;
2084 if (bma
->cur
== NULL
)
2085 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2087 rval
= XFS_ILOG_CORE
;
2088 error
= xfs_bmbt_lookup_eq(bma
->cur
, new->br_startoff
,
2089 new->br_startblock
, new->br_blockcount
,
2093 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
2094 bma
->cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
2095 error
= xfs_btree_insert(bma
->cur
, &i
);
2098 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2101 if (xfs_bmap_needs_btree(bma
->ip
, XFS_DATA_FORK
)) {
2102 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
2103 bma
->firstblock
, bma
->flist
, &bma
->cur
, 1,
2104 &tmp_rval
, XFS_DATA_FORK
);
2109 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
2110 startblockval(PREV
.br_startblock
) -
2111 (bma
->cur
? bma
->cur
->bc_private
.b
.allocated
: 0));
2112 ep
= xfs_iext_get_ext(ifp
, bma
->idx
);
2113 xfs_bmbt_set_startblock(ep
, nullstartblock(da_new
));
2114 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2121 * Filling in the middle part of a previous delayed allocation.
2122 * Contiguity is impossible here.
2123 * This case is avoided almost all the time.
2125 * We start with a delayed allocation:
2127 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
2130 * and we are allocating:
2131 * +rrrrrrrrrrrrrrrrr+
2134 * and we set it up for insertion as:
2135 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
2137 * PREV @ idx LEFT RIGHT
2138 * inserted at idx + 1
2140 temp
= new->br_startoff
- PREV
.br_startoff
;
2141 temp2
= PREV
.br_startoff
+ PREV
.br_blockcount
- new_endoff
;
2142 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, 0, _THIS_IP_
);
2143 xfs_bmbt_set_blockcount(ep
, temp
); /* truncate PREV */
2145 RIGHT
.br_state
= PREV
.br_state
;
2146 RIGHT
.br_startblock
= nullstartblock(
2147 (int)xfs_bmap_worst_indlen(bma
->ip
, temp2
));
2148 RIGHT
.br_startoff
= new_endoff
;
2149 RIGHT
.br_blockcount
= temp2
;
2150 /* insert LEFT (r[0]) and RIGHT (r[1]) at the same time */
2151 xfs_iext_insert(bma
->ip
, bma
->idx
+ 1, 2, &LEFT
, state
);
2152 bma
->ip
->i_d
.di_nextents
++;
2153 if (bma
->cur
== NULL
)
2154 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2156 rval
= XFS_ILOG_CORE
;
2157 error
= xfs_bmbt_lookup_eq(bma
->cur
, new->br_startoff
,
2158 new->br_startblock
, new->br_blockcount
,
2162 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
2163 bma
->cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
2164 error
= xfs_btree_insert(bma
->cur
, &i
);
2167 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2170 if (xfs_bmap_needs_btree(bma
->ip
, XFS_DATA_FORK
)) {
2171 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
2172 bma
->firstblock
, bma
->flist
, &bma
->cur
,
2173 1, &tmp_rval
, XFS_DATA_FORK
);
2178 temp
= xfs_bmap_worst_indlen(bma
->ip
, temp
);
2179 temp2
= xfs_bmap_worst_indlen(bma
->ip
, temp2
);
2180 diff
= (int)(temp
+ temp2
- startblockval(PREV
.br_startblock
) -
2181 (bma
->cur
? bma
->cur
->bc_private
.b
.allocated
: 0));
2183 error
= xfs_mod_fdblocks(bma
->ip
->i_mount
,
2184 -((int64_t)diff
), false);
2190 ep
= xfs_iext_get_ext(ifp
, bma
->idx
);
2191 xfs_bmbt_set_startblock(ep
, nullstartblock((int)temp
));
2192 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2193 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
+ 2, state
, _THIS_IP_
);
2194 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp
, bma
->idx
+ 2),
2195 nullstartblock((int)temp2
));
2196 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
+ 2, state
, _THIS_IP_
);
2199 da_new
= temp
+ temp2
;
2202 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2203 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2204 case BMAP_LEFT_FILLING
| BMAP_RIGHT_CONTIG
:
2205 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
2206 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2207 case BMAP_LEFT_CONTIG
:
2208 case BMAP_RIGHT_CONTIG
:
2210 * These cases are all impossible.
2215 /* convert to a btree if necessary */
2216 if (xfs_bmap_needs_btree(bma
->ip
, XFS_DATA_FORK
)) {
2217 int tmp_logflags
; /* partial log flag return val */
2219 ASSERT(bma
->cur
== NULL
);
2220 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
2221 bma
->firstblock
, bma
->flist
, &bma
->cur
,
2222 da_old
> 0, &tmp_logflags
, XFS_DATA_FORK
);
2223 bma
->logflags
|= tmp_logflags
;
2228 /* adjust for changes in reserved delayed indirect blocks */
2229 if (da_old
|| da_new
) {
2232 temp
+= bma
->cur
->bc_private
.b
.allocated
;
2233 ASSERT(temp
<= da_old
);
2235 xfs_mod_fdblocks(bma
->ip
->i_mount
,
2236 (int64_t)(da_old
- temp
), false);
2239 /* clear out the allocated field, done with it now in any case. */
2241 bma
->cur
->bc_private
.b
.allocated
= 0;
2243 xfs_bmap_check_leaf_extents(bma
->cur
, bma
->ip
, XFS_DATA_FORK
);
2245 bma
->logflags
|= rval
;
2253 * Convert an unwritten allocation to a real allocation or vice versa.
2255 STATIC
int /* error */
2256 xfs_bmap_add_extent_unwritten_real(
2257 struct xfs_trans
*tp
,
2258 xfs_inode_t
*ip
, /* incore inode pointer */
2259 xfs_extnum_t
*idx
, /* extent number to update/insert */
2260 xfs_btree_cur_t
**curp
, /* if *curp is null, not a btree */
2261 xfs_bmbt_irec_t
*new, /* new data to add to file extents */
2262 xfs_fsblock_t
*first
, /* pointer to firstblock variable */
2263 xfs_bmap_free_t
*flist
, /* list of extents to be freed */
2264 int *logflagsp
) /* inode logging flags */
2266 xfs_btree_cur_t
*cur
; /* btree cursor */
2267 xfs_bmbt_rec_host_t
*ep
; /* extent entry for idx */
2268 int error
; /* error return value */
2269 int i
; /* temp state */
2270 xfs_ifork_t
*ifp
; /* inode fork pointer */
2271 xfs_fileoff_t new_endoff
; /* end offset of new entry */
2272 xfs_exntst_t newext
; /* new extent state */
2273 xfs_exntst_t oldext
; /* old extent state */
2274 xfs_bmbt_irec_t r
[3]; /* neighbor extent entries */
2275 /* left is 0, right is 1, prev is 2 */
2276 int rval
=0; /* return value (logging flags) */
2277 int state
= 0;/* state bits, accessed thru macros */
2278 struct xfs_mount
*mp
= tp
->t_mountp
;
2283 ifp
= XFS_IFORK_PTR(ip
, XFS_DATA_FORK
);
2286 ASSERT(*idx
<= ifp
->if_bytes
/ sizeof(struct xfs_bmbt_rec
));
2287 ASSERT(!isnullstartblock(new->br_startblock
));
2289 XFS_STATS_INC(xs_add_exlist
);
2296 * Set up a bunch of variables to make the tests simpler.
2299 ep
= xfs_iext_get_ext(ifp
, *idx
);
2300 xfs_bmbt_get_all(ep
, &PREV
);
2301 newext
= new->br_state
;
2302 oldext
= (newext
== XFS_EXT_UNWRITTEN
) ?
2303 XFS_EXT_NORM
: XFS_EXT_UNWRITTEN
;
2304 ASSERT(PREV
.br_state
== oldext
);
2305 new_endoff
= new->br_startoff
+ new->br_blockcount
;
2306 ASSERT(PREV
.br_startoff
<= new->br_startoff
);
2307 ASSERT(PREV
.br_startoff
+ PREV
.br_blockcount
>= new_endoff
);
2310 * Set flags determining what part of the previous oldext allocation
2311 * extent is being replaced by a newext allocation.
2313 if (PREV
.br_startoff
== new->br_startoff
)
2314 state
|= BMAP_LEFT_FILLING
;
2315 if (PREV
.br_startoff
+ PREV
.br_blockcount
== new_endoff
)
2316 state
|= BMAP_RIGHT_FILLING
;
2319 * Check and set flags if this segment has a left neighbor.
2320 * Don't set contiguous if the combined extent would be too large.
2323 state
|= BMAP_LEFT_VALID
;
2324 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, *idx
- 1), &LEFT
);
2326 if (isnullstartblock(LEFT
.br_startblock
))
2327 state
|= BMAP_LEFT_DELAY
;
2330 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
2331 LEFT
.br_startoff
+ LEFT
.br_blockcount
== new->br_startoff
&&
2332 LEFT
.br_startblock
+ LEFT
.br_blockcount
== new->br_startblock
&&
2333 LEFT
.br_state
== newext
&&
2334 LEFT
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
2335 state
|= BMAP_LEFT_CONTIG
;
2338 * Check and set flags if this segment has a right neighbor.
2339 * Don't set contiguous if the combined extent would be too large.
2340 * Also check for all-three-contiguous being too large.
2342 if (*idx
< ip
->i_df
.if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
) - 1) {
2343 state
|= BMAP_RIGHT_VALID
;
2344 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, *idx
+ 1), &RIGHT
);
2345 if (isnullstartblock(RIGHT
.br_startblock
))
2346 state
|= BMAP_RIGHT_DELAY
;
2349 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
2350 new_endoff
== RIGHT
.br_startoff
&&
2351 new->br_startblock
+ new->br_blockcount
== RIGHT
.br_startblock
&&
2352 newext
== RIGHT
.br_state
&&
2353 new->br_blockcount
+ RIGHT
.br_blockcount
<= MAXEXTLEN
&&
2354 ((state
& (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
2355 BMAP_RIGHT_FILLING
)) !=
2356 (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
2357 BMAP_RIGHT_FILLING
) ||
2358 LEFT
.br_blockcount
+ new->br_blockcount
+ RIGHT
.br_blockcount
2360 state
|= BMAP_RIGHT_CONTIG
;
2363 * Switch out based on the FILLING and CONTIG state bits.
2365 switch (state
& (BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
2366 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
)) {
2367 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
2368 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2370 * Setting all of a previous oldext extent to newext.
2371 * The left and right neighbors are both contiguous with new.
2375 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2376 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
),
2377 LEFT
.br_blockcount
+ PREV
.br_blockcount
+
2378 RIGHT
.br_blockcount
);
2379 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2381 xfs_iext_remove(ip
, *idx
+ 1, 2, state
);
2382 ip
->i_d
.di_nextents
-= 2;
2384 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2386 rval
= XFS_ILOG_CORE
;
2387 if ((error
= xfs_bmbt_lookup_eq(cur
, RIGHT
.br_startoff
,
2388 RIGHT
.br_startblock
,
2389 RIGHT
.br_blockcount
, &i
)))
2391 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2392 if ((error
= xfs_btree_delete(cur
, &i
)))
2394 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2395 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2397 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2398 if ((error
= xfs_btree_delete(cur
, &i
)))
2400 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2401 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2403 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2404 if ((error
= xfs_bmbt_update(cur
, LEFT
.br_startoff
,
2406 LEFT
.br_blockcount
+ PREV
.br_blockcount
+
2407 RIGHT
.br_blockcount
, LEFT
.br_state
)))
2412 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
2414 * Setting all of a previous oldext extent to newext.
2415 * The left neighbor is contiguous, the right is not.
2419 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2420 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
),
2421 LEFT
.br_blockcount
+ PREV
.br_blockcount
);
2422 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2424 xfs_iext_remove(ip
, *idx
+ 1, 1, state
);
2425 ip
->i_d
.di_nextents
--;
2427 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2429 rval
= XFS_ILOG_CORE
;
2430 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2431 PREV
.br_startblock
, PREV
.br_blockcount
,
2434 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2435 if ((error
= xfs_btree_delete(cur
, &i
)))
2437 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2438 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2440 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2441 if ((error
= xfs_bmbt_update(cur
, LEFT
.br_startoff
,
2443 LEFT
.br_blockcount
+ PREV
.br_blockcount
,
2449 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2451 * Setting all of a previous oldext extent to newext.
2452 * The right neighbor is contiguous, the left is not.
2454 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2455 xfs_bmbt_set_blockcount(ep
,
2456 PREV
.br_blockcount
+ RIGHT
.br_blockcount
);
2457 xfs_bmbt_set_state(ep
, newext
);
2458 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2459 xfs_iext_remove(ip
, *idx
+ 1, 1, state
);
2460 ip
->i_d
.di_nextents
--;
2462 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2464 rval
= XFS_ILOG_CORE
;
2465 if ((error
= xfs_bmbt_lookup_eq(cur
, RIGHT
.br_startoff
,
2466 RIGHT
.br_startblock
,
2467 RIGHT
.br_blockcount
, &i
)))
2469 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2470 if ((error
= xfs_btree_delete(cur
, &i
)))
2472 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2473 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2475 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2476 if ((error
= xfs_bmbt_update(cur
, new->br_startoff
,
2478 new->br_blockcount
+ RIGHT
.br_blockcount
,
2484 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
2486 * Setting all of a previous oldext extent to newext.
2487 * Neither the left nor right neighbors are contiguous with
2490 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2491 xfs_bmbt_set_state(ep
, newext
);
2492 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2495 rval
= XFS_ILOG_DEXT
;
2498 if ((error
= xfs_bmbt_lookup_eq(cur
, new->br_startoff
,
2499 new->br_startblock
, new->br_blockcount
,
2502 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2503 if ((error
= xfs_bmbt_update(cur
, new->br_startoff
,
2504 new->br_startblock
, new->br_blockcount
,
2510 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
:
2512 * Setting the first part of a previous oldext extent to newext.
2513 * The left neighbor is contiguous.
2515 trace_xfs_bmap_pre_update(ip
, *idx
- 1, state
, _THIS_IP_
);
2516 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
- 1),
2517 LEFT
.br_blockcount
+ new->br_blockcount
);
2518 xfs_bmbt_set_startoff(ep
,
2519 PREV
.br_startoff
+ new->br_blockcount
);
2520 trace_xfs_bmap_post_update(ip
, *idx
- 1, state
, _THIS_IP_
);
2522 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2523 xfs_bmbt_set_startblock(ep
,
2524 new->br_startblock
+ new->br_blockcount
);
2525 xfs_bmbt_set_blockcount(ep
,
2526 PREV
.br_blockcount
- new->br_blockcount
);
2527 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2532 rval
= XFS_ILOG_DEXT
;
2535 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2536 PREV
.br_startblock
, PREV
.br_blockcount
,
2539 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2540 if ((error
= xfs_bmbt_update(cur
,
2541 PREV
.br_startoff
+ new->br_blockcount
,
2542 PREV
.br_startblock
+ new->br_blockcount
,
2543 PREV
.br_blockcount
- new->br_blockcount
,
2546 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2548 error
= xfs_bmbt_update(cur
, LEFT
.br_startoff
,
2550 LEFT
.br_blockcount
+ new->br_blockcount
,
2557 case BMAP_LEFT_FILLING
:
2559 * Setting the first part of a previous oldext extent to newext.
2560 * The left neighbor is not contiguous.
2562 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2563 ASSERT(ep
&& xfs_bmbt_get_state(ep
) == oldext
);
2564 xfs_bmbt_set_startoff(ep
, new_endoff
);
2565 xfs_bmbt_set_blockcount(ep
,
2566 PREV
.br_blockcount
- new->br_blockcount
);
2567 xfs_bmbt_set_startblock(ep
,
2568 new->br_startblock
+ new->br_blockcount
);
2569 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2571 xfs_iext_insert(ip
, *idx
, 1, new, state
);
2572 ip
->i_d
.di_nextents
++;
2574 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2576 rval
= XFS_ILOG_CORE
;
2577 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2578 PREV
.br_startblock
, PREV
.br_blockcount
,
2581 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2582 if ((error
= xfs_bmbt_update(cur
,
2583 PREV
.br_startoff
+ new->br_blockcount
,
2584 PREV
.br_startblock
+ new->br_blockcount
,
2585 PREV
.br_blockcount
- new->br_blockcount
,
2588 cur
->bc_rec
.b
= *new;
2589 if ((error
= xfs_btree_insert(cur
, &i
)))
2591 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2595 case BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2597 * Setting the last part of a previous oldext extent to newext.
2598 * The right neighbor is contiguous with the new allocation.
2600 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2601 xfs_bmbt_set_blockcount(ep
,
2602 PREV
.br_blockcount
- new->br_blockcount
);
2603 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2607 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2608 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp
, *idx
),
2609 new->br_startoff
, new->br_startblock
,
2610 new->br_blockcount
+ RIGHT
.br_blockcount
, newext
);
2611 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2614 rval
= XFS_ILOG_DEXT
;
2617 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2619 PREV
.br_blockcount
, &i
)))
2621 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2622 if ((error
= xfs_bmbt_update(cur
, PREV
.br_startoff
,
2624 PREV
.br_blockcount
- new->br_blockcount
,
2627 if ((error
= xfs_btree_increment(cur
, 0, &i
)))
2629 if ((error
= xfs_bmbt_update(cur
, new->br_startoff
,
2631 new->br_blockcount
+ RIGHT
.br_blockcount
,
2637 case BMAP_RIGHT_FILLING
:
2639 * Setting the last part of a previous oldext extent to newext.
2640 * The right neighbor is not contiguous.
2642 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2643 xfs_bmbt_set_blockcount(ep
,
2644 PREV
.br_blockcount
- new->br_blockcount
);
2645 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2648 xfs_iext_insert(ip
, *idx
, 1, new, state
);
2650 ip
->i_d
.di_nextents
++;
2652 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2654 rval
= XFS_ILOG_CORE
;
2655 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2656 PREV
.br_startblock
, PREV
.br_blockcount
,
2659 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2660 if ((error
= xfs_bmbt_update(cur
, PREV
.br_startoff
,
2662 PREV
.br_blockcount
- new->br_blockcount
,
2665 if ((error
= xfs_bmbt_lookup_eq(cur
, new->br_startoff
,
2666 new->br_startblock
, new->br_blockcount
,
2669 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
2670 cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
2671 if ((error
= xfs_btree_insert(cur
, &i
)))
2673 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2679 * Setting the middle part of a previous oldext extent to
2680 * newext. Contiguity is impossible here.
2681 * One extent becomes three extents.
2683 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2684 xfs_bmbt_set_blockcount(ep
,
2685 new->br_startoff
- PREV
.br_startoff
);
2686 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2689 r
[1].br_startoff
= new_endoff
;
2690 r
[1].br_blockcount
=
2691 PREV
.br_startoff
+ PREV
.br_blockcount
- new_endoff
;
2692 r
[1].br_startblock
= new->br_startblock
+ new->br_blockcount
;
2693 r
[1].br_state
= oldext
;
2696 xfs_iext_insert(ip
, *idx
, 2, &r
[0], state
);
2698 ip
->i_d
.di_nextents
+= 2;
2700 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2702 rval
= XFS_ILOG_CORE
;
2703 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2704 PREV
.br_startblock
, PREV
.br_blockcount
,
2707 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2708 /* new right extent - oldext */
2709 if ((error
= xfs_bmbt_update(cur
, r
[1].br_startoff
,
2710 r
[1].br_startblock
, r
[1].br_blockcount
,
2713 /* new left extent - oldext */
2714 cur
->bc_rec
.b
= PREV
;
2715 cur
->bc_rec
.b
.br_blockcount
=
2716 new->br_startoff
- PREV
.br_startoff
;
2717 if ((error
= xfs_btree_insert(cur
, &i
)))
2719 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2721 * Reset the cursor to the position of the new extent
2722 * we are about to insert as we can't trust it after
2723 * the previous insert.
2725 if ((error
= xfs_bmbt_lookup_eq(cur
, new->br_startoff
,
2726 new->br_startblock
, new->br_blockcount
,
2729 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
2730 /* new middle extent - newext */
2731 cur
->bc_rec
.b
.br_state
= new->br_state
;
2732 if ((error
= xfs_btree_insert(cur
, &i
)))
2734 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2738 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2739 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2740 case BMAP_LEFT_FILLING
| BMAP_RIGHT_CONTIG
:
2741 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
2742 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2743 case BMAP_LEFT_CONTIG
:
2744 case BMAP_RIGHT_CONTIG
:
2746 * These cases are all impossible.
2751 /* convert to a btree if necessary */
2752 if (xfs_bmap_needs_btree(ip
, XFS_DATA_FORK
)) {
2753 int tmp_logflags
; /* partial log flag return val */
2755 ASSERT(cur
== NULL
);
2756 error
= xfs_bmap_extents_to_btree(tp
, ip
, first
, flist
, &cur
,
2757 0, &tmp_logflags
, XFS_DATA_FORK
);
2758 *logflagsp
|= tmp_logflags
;
2763 /* clear out the allocated field, done with it now in any case. */
2765 cur
->bc_private
.b
.allocated
= 0;
2769 xfs_bmap_check_leaf_extents(*curp
, ip
, XFS_DATA_FORK
);
2779 * Convert a hole to a delayed allocation.
2782 xfs_bmap_add_extent_hole_delay(
2783 xfs_inode_t
*ip
, /* incore inode pointer */
2784 xfs_extnum_t
*idx
, /* extent number to update/insert */
2785 xfs_bmbt_irec_t
*new) /* new data to add to file extents */
2787 xfs_ifork_t
*ifp
; /* inode fork pointer */
2788 xfs_bmbt_irec_t left
; /* left neighbor extent entry */
2789 xfs_filblks_t newlen
=0; /* new indirect size */
2790 xfs_filblks_t oldlen
=0; /* old indirect size */
2791 xfs_bmbt_irec_t right
; /* right neighbor extent entry */
2792 int state
; /* state bits, accessed thru macros */
2793 xfs_filblks_t temp
=0; /* temp for indirect calculations */
2795 ifp
= XFS_IFORK_PTR(ip
, XFS_DATA_FORK
);
2797 ASSERT(isnullstartblock(new->br_startblock
));
2800 * Check and set flags if this segment has a left neighbor
2803 state
|= BMAP_LEFT_VALID
;
2804 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, *idx
- 1), &left
);
2806 if (isnullstartblock(left
.br_startblock
))
2807 state
|= BMAP_LEFT_DELAY
;
2811 * Check and set flags if the current (right) segment exists.
2812 * If it doesn't exist, we're converting the hole at end-of-file.
2814 if (*idx
< ip
->i_df
.if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
)) {
2815 state
|= BMAP_RIGHT_VALID
;
2816 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, *idx
), &right
);
2818 if (isnullstartblock(right
.br_startblock
))
2819 state
|= BMAP_RIGHT_DELAY
;
2823 * Set contiguity flags on the left and right neighbors.
2824 * Don't let extents get too large, even if the pieces are contiguous.
2826 if ((state
& BMAP_LEFT_VALID
) && (state
& BMAP_LEFT_DELAY
) &&
2827 left
.br_startoff
+ left
.br_blockcount
== new->br_startoff
&&
2828 left
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
2829 state
|= BMAP_LEFT_CONTIG
;
2831 if ((state
& BMAP_RIGHT_VALID
) && (state
& BMAP_RIGHT_DELAY
) &&
2832 new->br_startoff
+ new->br_blockcount
== right
.br_startoff
&&
2833 new->br_blockcount
+ right
.br_blockcount
<= MAXEXTLEN
&&
2834 (!(state
& BMAP_LEFT_CONTIG
) ||
2835 (left
.br_blockcount
+ new->br_blockcount
+
2836 right
.br_blockcount
<= MAXEXTLEN
)))
2837 state
|= BMAP_RIGHT_CONTIG
;
2840 * Switch out based on the contiguity flags.
2842 switch (state
& (BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
)) {
2843 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2845 * New allocation is contiguous with delayed allocations
2846 * on the left and on the right.
2847 * Merge all three into a single extent record.
2850 temp
= left
.br_blockcount
+ new->br_blockcount
+
2851 right
.br_blockcount
;
2853 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2854 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
), temp
);
2855 oldlen
= startblockval(left
.br_startblock
) +
2856 startblockval(new->br_startblock
) +
2857 startblockval(right
.br_startblock
);
2858 newlen
= xfs_bmap_worst_indlen(ip
, temp
);
2859 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp
, *idx
),
2860 nullstartblock((int)newlen
));
2861 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2863 xfs_iext_remove(ip
, *idx
+ 1, 1, state
);
2866 case BMAP_LEFT_CONTIG
:
2868 * New allocation is contiguous with a delayed allocation
2870 * Merge the new allocation with the left neighbor.
2873 temp
= left
.br_blockcount
+ new->br_blockcount
;
2875 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2876 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
), temp
);
2877 oldlen
= startblockval(left
.br_startblock
) +
2878 startblockval(new->br_startblock
);
2879 newlen
= xfs_bmap_worst_indlen(ip
, temp
);
2880 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp
, *idx
),
2881 nullstartblock((int)newlen
));
2882 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2885 case BMAP_RIGHT_CONTIG
:
2887 * New allocation is contiguous with a delayed allocation
2889 * Merge the new allocation with the right neighbor.
2891 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2892 temp
= new->br_blockcount
+ right
.br_blockcount
;
2893 oldlen
= startblockval(new->br_startblock
) +
2894 startblockval(right
.br_startblock
);
2895 newlen
= xfs_bmap_worst_indlen(ip
, temp
);
2896 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp
, *idx
),
2898 nullstartblock((int)newlen
), temp
, right
.br_state
);
2899 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2904 * New allocation is not contiguous with another
2905 * delayed allocation.
2906 * Insert a new entry.
2908 oldlen
= newlen
= 0;
2909 xfs_iext_insert(ip
, *idx
, 1, new, state
);
2912 if (oldlen
!= newlen
) {
2913 ASSERT(oldlen
> newlen
);
2914 xfs_mod_fdblocks(ip
->i_mount
, (int64_t)(oldlen
- newlen
),
2917 * Nothing to do for disk quota accounting here.
2923 * Convert a hole to a real allocation.
2925 STATIC
int /* error */
2926 xfs_bmap_add_extent_hole_real(
2927 struct xfs_bmalloca
*bma
,
2930 struct xfs_bmbt_irec
*new = &bma
->got
;
2931 int error
; /* error return value */
2932 int i
; /* temp state */
2933 xfs_ifork_t
*ifp
; /* inode fork pointer */
2934 xfs_bmbt_irec_t left
; /* left neighbor extent entry */
2935 xfs_bmbt_irec_t right
; /* right neighbor extent entry */
2936 int rval
=0; /* return value (logging flags) */
2937 int state
; /* state bits, accessed thru macros */
2938 struct xfs_mount
*mp
;
2940 mp
= bma
->tp
? bma
->tp
->t_mountp
: NULL
;
2941 ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
2943 ASSERT(bma
->idx
>= 0);
2944 ASSERT(bma
->idx
<= ifp
->if_bytes
/ sizeof(struct xfs_bmbt_rec
));
2945 ASSERT(!isnullstartblock(new->br_startblock
));
2947 !(bma
->cur
->bc_private
.b
.flags
& XFS_BTCUR_BPRV_WASDEL
));
2949 XFS_STATS_INC(xs_add_exlist
);
2952 if (whichfork
== XFS_ATTR_FORK
)
2953 state
|= BMAP_ATTRFORK
;
2956 * Check and set flags if this segment has a left neighbor.
2959 state
|= BMAP_LEFT_VALID
;
2960 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
- 1), &left
);
2961 if (isnullstartblock(left
.br_startblock
))
2962 state
|= BMAP_LEFT_DELAY
;
2966 * Check and set flags if this segment has a current value.
2967 * Not true if we're inserting into the "hole" at eof.
2969 if (bma
->idx
< ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
)) {
2970 state
|= BMAP_RIGHT_VALID
;
2971 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
), &right
);
2972 if (isnullstartblock(right
.br_startblock
))
2973 state
|= BMAP_RIGHT_DELAY
;
2977 * We're inserting a real allocation between "left" and "right".
2978 * Set the contiguity flags. Don't let extents get too large.
2980 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
2981 left
.br_startoff
+ left
.br_blockcount
== new->br_startoff
&&
2982 left
.br_startblock
+ left
.br_blockcount
== new->br_startblock
&&
2983 left
.br_state
== new->br_state
&&
2984 left
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
2985 state
|= BMAP_LEFT_CONTIG
;
2987 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
2988 new->br_startoff
+ new->br_blockcount
== right
.br_startoff
&&
2989 new->br_startblock
+ new->br_blockcount
== right
.br_startblock
&&
2990 new->br_state
== right
.br_state
&&
2991 new->br_blockcount
+ right
.br_blockcount
<= MAXEXTLEN
&&
2992 (!(state
& BMAP_LEFT_CONTIG
) ||
2993 left
.br_blockcount
+ new->br_blockcount
+
2994 right
.br_blockcount
<= MAXEXTLEN
))
2995 state
|= BMAP_RIGHT_CONTIG
;
2999 * Select which case we're in here, and implement it.
3001 switch (state
& (BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
)) {
3002 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
3004 * New allocation is contiguous with real allocations on the
3005 * left and on the right.
3006 * Merge all three into a single extent record.
3009 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
3010 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, bma
->idx
),
3011 left
.br_blockcount
+ new->br_blockcount
+
3012 right
.br_blockcount
);
3013 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
3015 xfs_iext_remove(bma
->ip
, bma
->idx
+ 1, 1, state
);
3017 XFS_IFORK_NEXT_SET(bma
->ip
, whichfork
,
3018 XFS_IFORK_NEXTENTS(bma
->ip
, whichfork
) - 1);
3019 if (bma
->cur
== NULL
) {
3020 rval
= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
3022 rval
= XFS_ILOG_CORE
;
3023 error
= xfs_bmbt_lookup_eq(bma
->cur
, right
.br_startoff
,
3024 right
.br_startblock
, right
.br_blockcount
,
3028 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
3029 error
= xfs_btree_delete(bma
->cur
, &i
);
3032 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
3033 error
= xfs_btree_decrement(bma
->cur
, 0, &i
);
3036 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
3037 error
= xfs_bmbt_update(bma
->cur
, left
.br_startoff
,
3039 left
.br_blockcount
+
3040 new->br_blockcount
+
3041 right
.br_blockcount
,
3048 case BMAP_LEFT_CONTIG
:
3050 * New allocation is contiguous with a real allocation
3052 * Merge the new allocation with the left neighbor.
3055 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
3056 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, bma
->idx
),
3057 left
.br_blockcount
+ new->br_blockcount
);
3058 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
3060 if (bma
->cur
== NULL
) {
3061 rval
= xfs_ilog_fext(whichfork
);
3064 error
= xfs_bmbt_lookup_eq(bma
->cur
, left
.br_startoff
,
3065 left
.br_startblock
, left
.br_blockcount
,
3069 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
3070 error
= xfs_bmbt_update(bma
->cur
, left
.br_startoff
,
3072 left
.br_blockcount
+
3080 case BMAP_RIGHT_CONTIG
:
3082 * New allocation is contiguous with a real allocation
3084 * Merge the new allocation with the right neighbor.
3086 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
3087 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp
, bma
->idx
),
3088 new->br_startoff
, new->br_startblock
,
3089 new->br_blockcount
+ right
.br_blockcount
,
3091 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
3093 if (bma
->cur
== NULL
) {
3094 rval
= xfs_ilog_fext(whichfork
);
3097 error
= xfs_bmbt_lookup_eq(bma
->cur
,
3099 right
.br_startblock
,
3100 right
.br_blockcount
, &i
);
3103 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
3104 error
= xfs_bmbt_update(bma
->cur
, new->br_startoff
,
3106 new->br_blockcount
+
3107 right
.br_blockcount
,
3116 * New allocation is not contiguous with another
3118 * Insert a new entry.
3120 xfs_iext_insert(bma
->ip
, bma
->idx
, 1, new, state
);
3121 XFS_IFORK_NEXT_SET(bma
->ip
, whichfork
,
3122 XFS_IFORK_NEXTENTS(bma
->ip
, whichfork
) + 1);
3123 if (bma
->cur
== NULL
) {
3124 rval
= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
3126 rval
= XFS_ILOG_CORE
;
3127 error
= xfs_bmbt_lookup_eq(bma
->cur
,
3130 new->br_blockcount
, &i
);
3133 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
3134 bma
->cur
->bc_rec
.b
.br_state
= new->br_state
;
3135 error
= xfs_btree_insert(bma
->cur
, &i
);
3138 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
3143 /* convert to a btree if necessary */
3144 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
3145 int tmp_logflags
; /* partial log flag return val */
3147 ASSERT(bma
->cur
== NULL
);
3148 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
3149 bma
->firstblock
, bma
->flist
, &bma
->cur
,
3150 0, &tmp_logflags
, whichfork
);
3151 bma
->logflags
|= tmp_logflags
;
3156 /* clear out the allocated field, done with it now in any case. */
3158 bma
->cur
->bc_private
.b
.allocated
= 0;
3160 xfs_bmap_check_leaf_extents(bma
->cur
, bma
->ip
, whichfork
);
3162 bma
->logflags
|= rval
;
3167 * Functions used in the extent read, allocate and remove paths
3171 * Adjust the size of the new extent based on di_extsize and rt extsize.
3174 xfs_bmap_extsize_align(
3176 xfs_bmbt_irec_t
*gotp
, /* next extent pointer */
3177 xfs_bmbt_irec_t
*prevp
, /* previous extent pointer */
3178 xfs_extlen_t extsz
, /* align to this extent size */
3179 int rt
, /* is this a realtime inode? */
3180 int eof
, /* is extent at end-of-file? */
3181 int delay
, /* creating delalloc extent? */
3182 int convert
, /* overwriting unwritten extent? */
3183 xfs_fileoff_t
*offp
, /* in/out: aligned offset */
3184 xfs_extlen_t
*lenp
) /* in/out: aligned length */
3186 xfs_fileoff_t orig_off
; /* original offset */
3187 xfs_extlen_t orig_alen
; /* original length */
3188 xfs_fileoff_t orig_end
; /* original off+len */
3189 xfs_fileoff_t nexto
; /* next file offset */
3190 xfs_fileoff_t prevo
; /* previous file offset */
3191 xfs_fileoff_t align_off
; /* temp for offset */
3192 xfs_extlen_t align_alen
; /* temp for length */
3193 xfs_extlen_t temp
; /* temp for calculations */
3198 orig_off
= align_off
= *offp
;
3199 orig_alen
= align_alen
= *lenp
;
3200 orig_end
= orig_off
+ orig_alen
;
3203 * If this request overlaps an existing extent, then don't
3204 * attempt to perform any additional alignment.
3206 if (!delay
&& !eof
&&
3207 (orig_off
>= gotp
->br_startoff
) &&
3208 (orig_end
<= gotp
->br_startoff
+ gotp
->br_blockcount
)) {
3213 * If the file offset is unaligned vs. the extent size
3214 * we need to align it. This will be possible unless
3215 * the file was previously written with a kernel that didn't
3216 * perform this alignment, or if a truncate shot us in the
3219 temp
= do_mod(orig_off
, extsz
);
3225 /* Same adjustment for the end of the requested area. */
3226 temp
= (align_alen
% extsz
);
3228 align_alen
+= extsz
- temp
;
3231 * For large extent hint sizes, the aligned extent might be larger than
3232 * MAXEXTLEN. In that case, reduce the size by an extsz so that it pulls
3233 * the length back under MAXEXTLEN. The outer allocation loops handle
3234 * short allocation just fine, so it is safe to do this. We only want to
3235 * do it when we are forced to, though, because it means more allocation
3236 * operations are required.
3238 while (align_alen
> MAXEXTLEN
)
3239 align_alen
-= extsz
;
3240 ASSERT(align_alen
<= MAXEXTLEN
);
3243 * If the previous block overlaps with this proposed allocation
3244 * then move the start forward without adjusting the length.
3246 if (prevp
->br_startoff
!= NULLFILEOFF
) {
3247 if (prevp
->br_startblock
== HOLESTARTBLOCK
)
3248 prevo
= prevp
->br_startoff
;
3250 prevo
= prevp
->br_startoff
+ prevp
->br_blockcount
;
3253 if (align_off
!= orig_off
&& align_off
< prevo
)
3256 * If the next block overlaps with this proposed allocation
3257 * then move the start back without adjusting the length,
3258 * but not before offset 0.
3259 * This may of course make the start overlap previous block,
3260 * and if we hit the offset 0 limit then the next block
3261 * can still overlap too.
3263 if (!eof
&& gotp
->br_startoff
!= NULLFILEOFF
) {
3264 if ((delay
&& gotp
->br_startblock
== HOLESTARTBLOCK
) ||
3265 (!delay
&& gotp
->br_startblock
== DELAYSTARTBLOCK
))
3266 nexto
= gotp
->br_startoff
+ gotp
->br_blockcount
;
3268 nexto
= gotp
->br_startoff
;
3270 nexto
= NULLFILEOFF
;
3272 align_off
+ align_alen
!= orig_end
&&
3273 align_off
+ align_alen
> nexto
)
3274 align_off
= nexto
> align_alen
? nexto
- align_alen
: 0;
3276 * If we're now overlapping the next or previous extent that
3277 * means we can't fit an extsz piece in this hole. Just move
3278 * the start forward to the first valid spot and set
3279 * the length so we hit the end.
3281 if (align_off
!= orig_off
&& align_off
< prevo
)
3283 if (align_off
+ align_alen
!= orig_end
&&
3284 align_off
+ align_alen
> nexto
&&
3285 nexto
!= NULLFILEOFF
) {
3286 ASSERT(nexto
> prevo
);
3287 align_alen
= nexto
- align_off
;
3291 * If realtime, and the result isn't a multiple of the realtime
3292 * extent size we need to remove blocks until it is.
3294 if (rt
&& (temp
= (align_alen
% mp
->m_sb
.sb_rextsize
))) {
3296 * We're not covering the original request, or
3297 * we won't be able to once we fix the length.
3299 if (orig_off
< align_off
||
3300 orig_end
> align_off
+ align_alen
||
3301 align_alen
- temp
< orig_alen
)
3304 * Try to fix it by moving the start up.
3306 if (align_off
+ temp
<= orig_off
) {
3311 * Try to fix it by moving the end in.
3313 else if (align_off
+ align_alen
- temp
>= orig_end
)
3316 * Set the start to the minimum then trim the length.
3319 align_alen
-= orig_off
- align_off
;
3320 align_off
= orig_off
;
3321 align_alen
-= align_alen
% mp
->m_sb
.sb_rextsize
;
3324 * Result doesn't cover the request, fail it.
3326 if (orig_off
< align_off
|| orig_end
> align_off
+ align_alen
)
3329 ASSERT(orig_off
>= align_off
);
3330 /* see MAXEXTLEN handling above */
3331 ASSERT(orig_end
<= align_off
+ align_alen
||
3332 align_alen
+ extsz
> MAXEXTLEN
);
3336 if (!eof
&& gotp
->br_startoff
!= NULLFILEOFF
)
3337 ASSERT(align_off
+ align_alen
<= gotp
->br_startoff
);
3338 if (prevp
->br_startoff
!= NULLFILEOFF
)
3339 ASSERT(align_off
>= prevp
->br_startoff
+ prevp
->br_blockcount
);
3347 #define XFS_ALLOC_GAP_UNITS 4
3351 struct xfs_bmalloca
*ap
) /* bmap alloc argument struct */
3353 xfs_fsblock_t adjust
; /* adjustment to block numbers */
3354 xfs_agnumber_t fb_agno
; /* ag number of ap->firstblock */
3355 xfs_mount_t
*mp
; /* mount point structure */
3356 int nullfb
; /* true if ap->firstblock isn't set */
3357 int rt
; /* true if inode is realtime */
3359 #define ISVALID(x,y) \
3361 (x) < mp->m_sb.sb_rblocks : \
3362 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
3363 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
3364 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
3366 mp
= ap
->ip
->i_mount
;
3367 nullfb
= *ap
->firstblock
== NULLFSBLOCK
;
3368 rt
= XFS_IS_REALTIME_INODE(ap
->ip
) && ap
->userdata
;
3369 fb_agno
= nullfb
? NULLAGNUMBER
: XFS_FSB_TO_AGNO(mp
, *ap
->firstblock
);
3371 * If allocating at eof, and there's a previous real block,
3372 * try to use its last block as our starting point.
3374 if (ap
->eof
&& ap
->prev
.br_startoff
!= NULLFILEOFF
&&
3375 !isnullstartblock(ap
->prev
.br_startblock
) &&
3376 ISVALID(ap
->prev
.br_startblock
+ ap
->prev
.br_blockcount
,
3377 ap
->prev
.br_startblock
)) {
3378 ap
->blkno
= ap
->prev
.br_startblock
+ ap
->prev
.br_blockcount
;
3380 * Adjust for the gap between prevp and us.
3382 adjust
= ap
->offset
-
3383 (ap
->prev
.br_startoff
+ ap
->prev
.br_blockcount
);
3385 ISVALID(ap
->blkno
+ adjust
, ap
->prev
.br_startblock
))
3386 ap
->blkno
+= adjust
;
3389 * If not at eof, then compare the two neighbor blocks.
3390 * Figure out whether either one gives us a good starting point,
3391 * and pick the better one.
3393 else if (!ap
->eof
) {
3394 xfs_fsblock_t gotbno
; /* right side block number */
3395 xfs_fsblock_t gotdiff
=0; /* right side difference */
3396 xfs_fsblock_t prevbno
; /* left side block number */
3397 xfs_fsblock_t prevdiff
=0; /* left side difference */
3400 * If there's a previous (left) block, select a requested
3401 * start block based on it.
3403 if (ap
->prev
.br_startoff
!= NULLFILEOFF
&&
3404 !isnullstartblock(ap
->prev
.br_startblock
) &&
3405 (prevbno
= ap
->prev
.br_startblock
+
3406 ap
->prev
.br_blockcount
) &&
3407 ISVALID(prevbno
, ap
->prev
.br_startblock
)) {
3409 * Calculate gap to end of previous block.
3411 adjust
= prevdiff
= ap
->offset
-
3412 (ap
->prev
.br_startoff
+
3413 ap
->prev
.br_blockcount
);
3415 * Figure the startblock based on the previous block's
3416 * end and the gap size.
3418 * If the gap is large relative to the piece we're
3419 * allocating, or using it gives us an invalid block
3420 * number, then just use the end of the previous block.
3422 if (prevdiff
<= XFS_ALLOC_GAP_UNITS
* ap
->length
&&
3423 ISVALID(prevbno
+ prevdiff
,
3424 ap
->prev
.br_startblock
))
3429 * If the firstblock forbids it, can't use it,
3432 if (!rt
&& !nullfb
&&
3433 XFS_FSB_TO_AGNO(mp
, prevbno
) != fb_agno
)
3434 prevbno
= NULLFSBLOCK
;
3437 * No previous block or can't follow it, just default.
3440 prevbno
= NULLFSBLOCK
;
3442 * If there's a following (right) block, select a requested
3443 * start block based on it.
3445 if (!isnullstartblock(ap
->got
.br_startblock
)) {
3447 * Calculate gap to start of next block.
3449 adjust
= gotdiff
= ap
->got
.br_startoff
- ap
->offset
;
3451 * Figure the startblock based on the next block's
3452 * start and the gap size.
3454 gotbno
= ap
->got
.br_startblock
;
3457 * If the gap is large relative to the piece we're
3458 * allocating, or using it gives us an invalid block
3459 * number, then just use the start of the next block
3460 * offset by our length.
3462 if (gotdiff
<= XFS_ALLOC_GAP_UNITS
* ap
->length
&&
3463 ISVALID(gotbno
- gotdiff
, gotbno
))
3465 else if (ISVALID(gotbno
- ap
->length
, gotbno
)) {
3466 gotbno
-= ap
->length
;
3467 gotdiff
+= adjust
- ap
->length
;
3471 * If the firstblock forbids it, can't use it,
3474 if (!rt
&& !nullfb
&&
3475 XFS_FSB_TO_AGNO(mp
, gotbno
) != fb_agno
)
3476 gotbno
= NULLFSBLOCK
;
3479 * No next block, just default.
3482 gotbno
= NULLFSBLOCK
;
3484 * If both valid, pick the better one, else the only good
3485 * one, else ap->blkno is already set (to 0 or the inode block).
3487 if (prevbno
!= NULLFSBLOCK
&& gotbno
!= NULLFSBLOCK
)
3488 ap
->blkno
= prevdiff
<= gotdiff
? prevbno
: gotbno
;
3489 else if (prevbno
!= NULLFSBLOCK
)
3490 ap
->blkno
= prevbno
;
3491 else if (gotbno
!= NULLFSBLOCK
)
3498 xfs_bmap_longest_free_extent(
3499 struct xfs_trans
*tp
,
3504 struct xfs_mount
*mp
= tp
->t_mountp
;
3505 struct xfs_perag
*pag
;
3506 xfs_extlen_t longest
;
3509 pag
= xfs_perag_get(mp
, ag
);
3510 if (!pag
->pagf_init
) {
3511 error
= xfs_alloc_pagf_init(mp
, tp
, ag
, XFS_ALLOC_FLAG_TRYLOCK
);
3515 if (!pag
->pagf_init
) {
3521 longest
= xfs_alloc_longest_free_extent(mp
, pag
,
3522 xfs_alloc_min_freelist(mp
, pag
));
3523 if (*blen
< longest
)
3532 xfs_bmap_select_minlen(
3533 struct xfs_bmalloca
*ap
,
3534 struct xfs_alloc_arg
*args
,
3538 if (notinit
|| *blen
< ap
->minlen
) {
3540 * Since we did a BUF_TRYLOCK above, it is possible that
3541 * there is space for this request.
3543 args
->minlen
= ap
->minlen
;
3544 } else if (*blen
< args
->maxlen
) {
3546 * If the best seen length is less than the request length,
3547 * use the best as the minimum.
3549 args
->minlen
= *blen
;
3552 * Otherwise we've seen an extent as big as maxlen, use that
3555 args
->minlen
= args
->maxlen
;
3560 xfs_bmap_btalloc_nullfb(
3561 struct xfs_bmalloca
*ap
,
3562 struct xfs_alloc_arg
*args
,
3565 struct xfs_mount
*mp
= ap
->ip
->i_mount
;
3566 xfs_agnumber_t ag
, startag
;
3570 args
->type
= XFS_ALLOCTYPE_START_BNO
;
3571 args
->total
= ap
->total
;
3573 startag
= ag
= XFS_FSB_TO_AGNO(mp
, args
->fsbno
);
3574 if (startag
== NULLAGNUMBER
)
3577 while (*blen
< args
->maxlen
) {
3578 error
= xfs_bmap_longest_free_extent(args
->tp
, ag
, blen
,
3583 if (++ag
== mp
->m_sb
.sb_agcount
)
3589 xfs_bmap_select_minlen(ap
, args
, blen
, notinit
);
3594 xfs_bmap_btalloc_filestreams(
3595 struct xfs_bmalloca
*ap
,
3596 struct xfs_alloc_arg
*args
,
3599 struct xfs_mount
*mp
= ap
->ip
->i_mount
;
3604 args
->type
= XFS_ALLOCTYPE_NEAR_BNO
;
3605 args
->total
= ap
->total
;
3607 ag
= XFS_FSB_TO_AGNO(mp
, args
->fsbno
);
3608 if (ag
== NULLAGNUMBER
)
3611 error
= xfs_bmap_longest_free_extent(args
->tp
, ag
, blen
, ¬init
);
3615 if (*blen
< args
->maxlen
) {
3616 error
= xfs_filestream_new_ag(ap
, &ag
);
3620 error
= xfs_bmap_longest_free_extent(args
->tp
, ag
, blen
,
3627 xfs_bmap_select_minlen(ap
, args
, blen
, notinit
);
3630 * Set the failure fallback case to look in the selected AG as stream
3633 ap
->blkno
= args
->fsbno
= XFS_AGB_TO_FSB(mp
, ag
, 0);
3639 struct xfs_bmalloca
*ap
) /* bmap alloc argument struct */
3641 xfs_mount_t
*mp
; /* mount point structure */
3642 xfs_alloctype_t atype
= 0; /* type for allocation routines */
3643 xfs_extlen_t align
; /* minimum allocation alignment */
3644 xfs_agnumber_t fb_agno
; /* ag number of ap->firstblock */
3646 xfs_alloc_arg_t args
;
3648 xfs_extlen_t nextminlen
= 0;
3649 int nullfb
; /* true if ap->firstblock isn't set */
3657 mp
= ap
->ip
->i_mount
;
3659 /* stripe alignment for allocation is determined by mount parameters */
3661 if (mp
->m_swidth
&& (mp
->m_flags
& XFS_MOUNT_SWALLOC
))
3662 stripe_align
= mp
->m_swidth
;
3663 else if (mp
->m_dalign
)
3664 stripe_align
= mp
->m_dalign
;
3666 align
= ap
->userdata
? xfs_get_extsz_hint(ap
->ip
) : 0;
3667 if (unlikely(align
)) {
3668 error
= xfs_bmap_extsize_align(mp
, &ap
->got
, &ap
->prev
,
3669 align
, 0, ap
->eof
, 0, ap
->conv
,
3670 &ap
->offset
, &ap
->length
);
3676 nullfb
= *ap
->firstblock
== NULLFSBLOCK
;
3677 fb_agno
= nullfb
? NULLAGNUMBER
: XFS_FSB_TO_AGNO(mp
, *ap
->firstblock
);
3679 if (ap
->userdata
&& xfs_inode_is_filestream(ap
->ip
)) {
3680 ag
= xfs_filestream_lookup_ag(ap
->ip
);
3681 ag
= (ag
!= NULLAGNUMBER
) ? ag
: 0;
3682 ap
->blkno
= XFS_AGB_TO_FSB(mp
, ag
, 0);
3684 ap
->blkno
= XFS_INO_TO_FSB(mp
, ap
->ip
->i_ino
);
3687 ap
->blkno
= *ap
->firstblock
;
3689 xfs_bmap_adjacent(ap
);
3692 * If allowed, use ap->blkno; otherwise must use firstblock since
3693 * it's in the right allocation group.
3695 if (nullfb
|| XFS_FSB_TO_AGNO(mp
, ap
->blkno
) == fb_agno
)
3698 ap
->blkno
= *ap
->firstblock
;
3700 * Normal allocation, done through xfs_alloc_vextent.
3702 tryagain
= isaligned
= 0;
3703 memset(&args
, 0, sizeof(args
));
3706 args
.fsbno
= ap
->blkno
;
3708 /* Trim the allocation back to the maximum an AG can fit. */
3709 args
.maxlen
= MIN(ap
->length
, XFS_ALLOC_AG_MAX_USABLE(mp
));
3710 args
.firstblock
= *ap
->firstblock
;
3714 * Search for an allocation group with a single extent large
3715 * enough for the request. If one isn't found, then adjust
3716 * the minimum allocation size to the largest space found.
3718 if (ap
->userdata
&& xfs_inode_is_filestream(ap
->ip
))
3719 error
= xfs_bmap_btalloc_filestreams(ap
, &args
, &blen
);
3721 error
= xfs_bmap_btalloc_nullfb(ap
, &args
, &blen
);
3724 } else if (ap
->flist
->xbf_low
) {
3725 if (xfs_inode_is_filestream(ap
->ip
))
3726 args
.type
= XFS_ALLOCTYPE_FIRST_AG
;
3728 args
.type
= XFS_ALLOCTYPE_START_BNO
;
3729 args
.total
= args
.minlen
= ap
->minlen
;
3731 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
3732 args
.total
= ap
->total
;
3733 args
.minlen
= ap
->minlen
;
3735 /* apply extent size hints if obtained earlier */
3736 if (unlikely(align
)) {
3738 if ((args
.mod
= (xfs_extlen_t
)do_mod(ap
->offset
, args
.prod
)))
3739 args
.mod
= (xfs_extlen_t
)(args
.prod
- args
.mod
);
3740 } else if (mp
->m_sb
.sb_blocksize
>= PAGE_CACHE_SIZE
) {
3744 args
.prod
= PAGE_CACHE_SIZE
>> mp
->m_sb
.sb_blocklog
;
3745 if ((args
.mod
= (xfs_extlen_t
)(do_mod(ap
->offset
, args
.prod
))))
3746 args
.mod
= (xfs_extlen_t
)(args
.prod
- args
.mod
);
3749 * If we are not low on available data blocks, and the
3750 * underlying logical volume manager is a stripe, and
3751 * the file offset is zero then try to allocate data
3752 * blocks on stripe unit boundary.
3753 * NOTE: ap->aeof is only set if the allocation length
3754 * is >= the stripe unit and the allocation offset is
3755 * at the end of file.
3757 if (!ap
->flist
->xbf_low
&& ap
->aeof
) {
3759 args
.alignment
= stripe_align
;
3763 * Adjust for alignment
3765 if (blen
> args
.alignment
&& blen
<= args
.maxlen
)
3766 args
.minlen
= blen
- args
.alignment
;
3767 args
.minalignslop
= 0;
3770 * First try an exact bno allocation.
3771 * If it fails then do a near or start bno
3772 * allocation with alignment turned on.
3776 args
.type
= XFS_ALLOCTYPE_THIS_BNO
;
3779 * Compute the minlen+alignment for the
3780 * next case. Set slop so that the value
3781 * of minlen+alignment+slop doesn't go up
3782 * between the calls.
3784 if (blen
> stripe_align
&& blen
<= args
.maxlen
)
3785 nextminlen
= blen
- stripe_align
;
3787 nextminlen
= args
.minlen
;
3788 if (nextminlen
+ stripe_align
> args
.minlen
+ 1)
3790 nextminlen
+ stripe_align
-
3793 args
.minalignslop
= 0;
3797 args
.minalignslop
= 0;
3799 args
.minleft
= ap
->minleft
;
3800 args
.wasdel
= ap
->wasdel
;
3802 args
.userdata
= ap
->userdata
;
3803 if ((error
= xfs_alloc_vextent(&args
)))
3805 if (tryagain
&& args
.fsbno
== NULLFSBLOCK
) {
3807 * Exact allocation failed. Now try with alignment
3811 args
.fsbno
= ap
->blkno
;
3812 args
.alignment
= stripe_align
;
3813 args
.minlen
= nextminlen
;
3814 args
.minalignslop
= 0;
3816 if ((error
= xfs_alloc_vextent(&args
)))
3819 if (isaligned
&& args
.fsbno
== NULLFSBLOCK
) {
3821 * allocation failed, so turn off alignment and
3825 args
.fsbno
= ap
->blkno
;
3827 if ((error
= xfs_alloc_vextent(&args
)))
3830 if (args
.fsbno
== NULLFSBLOCK
&& nullfb
&&
3831 args
.minlen
> ap
->minlen
) {
3832 args
.minlen
= ap
->minlen
;
3833 args
.type
= XFS_ALLOCTYPE_START_BNO
;
3834 args
.fsbno
= ap
->blkno
;
3835 if ((error
= xfs_alloc_vextent(&args
)))
3838 if (args
.fsbno
== NULLFSBLOCK
&& nullfb
) {
3840 args
.type
= XFS_ALLOCTYPE_FIRST_AG
;
3841 args
.total
= ap
->minlen
;
3843 if ((error
= xfs_alloc_vextent(&args
)))
3845 ap
->flist
->xbf_low
= 1;
3847 if (args
.fsbno
!= NULLFSBLOCK
) {
3849 * check the allocation happened at the same or higher AG than
3850 * the first block that was allocated.
3852 ASSERT(*ap
->firstblock
== NULLFSBLOCK
||
3853 XFS_FSB_TO_AGNO(mp
, *ap
->firstblock
) ==
3854 XFS_FSB_TO_AGNO(mp
, args
.fsbno
) ||
3855 (ap
->flist
->xbf_low
&&
3856 XFS_FSB_TO_AGNO(mp
, *ap
->firstblock
) <
3857 XFS_FSB_TO_AGNO(mp
, args
.fsbno
)));
3859 ap
->blkno
= args
.fsbno
;
3860 if (*ap
->firstblock
== NULLFSBLOCK
)
3861 *ap
->firstblock
= args
.fsbno
;
3862 ASSERT(nullfb
|| fb_agno
== args
.agno
||
3863 (ap
->flist
->xbf_low
&& fb_agno
< args
.agno
));
3864 ap
->length
= args
.len
;
3865 ap
->ip
->i_d
.di_nblocks
+= args
.len
;
3866 xfs_trans_log_inode(ap
->tp
, ap
->ip
, XFS_ILOG_CORE
);
3868 ap
->ip
->i_delayed_blks
-= args
.len
;
3870 * Adjust the disk quota also. This was reserved
3873 xfs_trans_mod_dquot_byino(ap
->tp
, ap
->ip
,
3874 ap
->wasdel
? XFS_TRANS_DQ_DELBCOUNT
:
3875 XFS_TRANS_DQ_BCOUNT
,
3878 ap
->blkno
= NULLFSBLOCK
;
3885 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
3886 * It figures out where to ask the underlying allocator to put the new extent.
3890 struct xfs_bmalloca
*ap
) /* bmap alloc argument struct */
3892 if (XFS_IS_REALTIME_INODE(ap
->ip
) && ap
->userdata
)
3893 return xfs_bmap_rtalloc(ap
);
3894 return xfs_bmap_btalloc(ap
);
3898 * Trim the returned map to the required bounds
3902 struct xfs_bmbt_irec
*mval
,
3903 struct xfs_bmbt_irec
*got
,
3911 if ((flags
& XFS_BMAPI_ENTIRE
) ||
3912 got
->br_startoff
+ got
->br_blockcount
<= obno
) {
3914 if (isnullstartblock(got
->br_startblock
))
3915 mval
->br_startblock
= DELAYSTARTBLOCK
;
3921 ASSERT((*bno
>= obno
) || (n
== 0));
3923 mval
->br_startoff
= *bno
;
3924 if (isnullstartblock(got
->br_startblock
))
3925 mval
->br_startblock
= DELAYSTARTBLOCK
;
3927 mval
->br_startblock
= got
->br_startblock
+
3928 (*bno
- got
->br_startoff
);
3930 * Return the minimum of what we got and what we asked for for
3931 * the length. We can use the len variable here because it is
3932 * modified below and we could have been there before coming
3933 * here if the first part of the allocation didn't overlap what
3936 mval
->br_blockcount
= XFS_FILBLKS_MIN(end
- *bno
,
3937 got
->br_blockcount
- (*bno
- got
->br_startoff
));
3938 mval
->br_state
= got
->br_state
;
3939 ASSERT(mval
->br_blockcount
<= len
);
3944 * Update and validate the extent map to return
3947 xfs_bmapi_update_map(
3948 struct xfs_bmbt_irec
**map
,
3956 xfs_bmbt_irec_t
*mval
= *map
;
3958 ASSERT((flags
& XFS_BMAPI_ENTIRE
) ||
3959 ((mval
->br_startoff
+ mval
->br_blockcount
) <= end
));
3960 ASSERT((flags
& XFS_BMAPI_ENTIRE
) || (mval
->br_blockcount
<= *len
) ||
3961 (mval
->br_startoff
< obno
));
3963 *bno
= mval
->br_startoff
+ mval
->br_blockcount
;
3965 if (*n
> 0 && mval
->br_startoff
== mval
[-1].br_startoff
) {
3966 /* update previous map with new information */
3967 ASSERT(mval
->br_startblock
== mval
[-1].br_startblock
);
3968 ASSERT(mval
->br_blockcount
> mval
[-1].br_blockcount
);
3969 ASSERT(mval
->br_state
== mval
[-1].br_state
);
3970 mval
[-1].br_blockcount
= mval
->br_blockcount
;
3971 mval
[-1].br_state
= mval
->br_state
;
3972 } else if (*n
> 0 && mval
->br_startblock
!= DELAYSTARTBLOCK
&&
3973 mval
[-1].br_startblock
!= DELAYSTARTBLOCK
&&
3974 mval
[-1].br_startblock
!= HOLESTARTBLOCK
&&
3975 mval
->br_startblock
== mval
[-1].br_startblock
+
3976 mval
[-1].br_blockcount
&&
3977 ((flags
& XFS_BMAPI_IGSTATE
) ||
3978 mval
[-1].br_state
== mval
->br_state
)) {
3979 ASSERT(mval
->br_startoff
==
3980 mval
[-1].br_startoff
+ mval
[-1].br_blockcount
);
3981 mval
[-1].br_blockcount
+= mval
->br_blockcount
;
3982 } else if (*n
> 0 &&
3983 mval
->br_startblock
== DELAYSTARTBLOCK
&&
3984 mval
[-1].br_startblock
== DELAYSTARTBLOCK
&&
3985 mval
->br_startoff
==
3986 mval
[-1].br_startoff
+ mval
[-1].br_blockcount
) {
3987 mval
[-1].br_blockcount
+= mval
->br_blockcount
;
3988 mval
[-1].br_state
= mval
->br_state
;
3989 } else if (!((*n
== 0) &&
3990 ((mval
->br_startoff
+ mval
->br_blockcount
) <=
3999 * Map file blocks to filesystem blocks without allocation.
4003 struct xfs_inode
*ip
,
4006 struct xfs_bmbt_irec
*mval
,
4010 struct xfs_mount
*mp
= ip
->i_mount
;
4011 struct xfs_ifork
*ifp
;
4012 struct xfs_bmbt_irec got
;
4013 struct xfs_bmbt_irec prev
;
4020 int whichfork
= (flags
& XFS_BMAPI_ATTRFORK
) ?
4021 XFS_ATTR_FORK
: XFS_DATA_FORK
;
4024 ASSERT(!(flags
& ~(XFS_BMAPI_ATTRFORK
|XFS_BMAPI_ENTIRE
|
4025 XFS_BMAPI_IGSTATE
)));
4026 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_SHARED
|XFS_ILOCK_EXCL
));
4028 if (unlikely(XFS_TEST_ERROR(
4029 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
4030 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
4031 mp
, XFS_ERRTAG_BMAPIFORMAT
, XFS_RANDOM_BMAPIFORMAT
))) {
4032 XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW
, mp
);
4033 return -EFSCORRUPTED
;
4036 if (XFS_FORCED_SHUTDOWN(mp
))
4039 XFS_STATS_INC(xs_blk_mapr
);
4041 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4043 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
4044 error
= xfs_iread_extents(NULL
, ip
, whichfork
);
4049 xfs_bmap_search_extents(ip
, bno
, whichfork
, &eof
, &lastx
, &got
, &prev
);
4053 while (bno
< end
&& n
< *nmap
) {
4054 /* Reading past eof, act as though there's a hole up to end. */
4056 got
.br_startoff
= end
;
4057 if (got
.br_startoff
> bno
) {
4058 /* Reading in a hole. */
4059 mval
->br_startoff
= bno
;
4060 mval
->br_startblock
= HOLESTARTBLOCK
;
4061 mval
->br_blockcount
=
4062 XFS_FILBLKS_MIN(len
, got
.br_startoff
- bno
);
4063 mval
->br_state
= XFS_EXT_NORM
;
4064 bno
+= mval
->br_blockcount
;
4065 len
-= mval
->br_blockcount
;
4071 /* set up the extent map to return. */
4072 xfs_bmapi_trim_map(mval
, &got
, &bno
, len
, obno
, end
, n
, flags
);
4073 xfs_bmapi_update_map(&mval
, &bno
, &len
, obno
, end
, &n
, flags
);
4075 /* If we're done, stop now. */
4076 if (bno
>= end
|| n
>= *nmap
)
4079 /* Else go on to the next record. */
4080 if (++lastx
< ifp
->if_bytes
/ sizeof(xfs_bmbt_rec_t
))
4081 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, lastx
), &got
);
4090 xfs_bmapi_reserve_delalloc(
4091 struct xfs_inode
*ip
,
4094 struct xfs_bmbt_irec
*got
,
4095 struct xfs_bmbt_irec
*prev
,
4096 xfs_extnum_t
*lastx
,
4099 struct xfs_mount
*mp
= ip
->i_mount
;
4100 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, XFS_DATA_FORK
);
4102 xfs_extlen_t indlen
;
4103 char rt
= XFS_IS_REALTIME_INODE(ip
);
4107 alen
= XFS_FILBLKS_MIN(len
, MAXEXTLEN
);
4109 alen
= XFS_FILBLKS_MIN(alen
, got
->br_startoff
- aoff
);
4111 /* Figure out the extent size, adjust alen */
4112 extsz
= xfs_get_extsz_hint(ip
);
4114 error
= xfs_bmap_extsize_align(mp
, got
, prev
, extsz
, rt
, eof
,
4115 1, 0, &aoff
, &alen
);
4120 extsz
= alen
/ mp
->m_sb
.sb_rextsize
;
4123 * Make a transaction-less quota reservation for delayed allocation
4124 * blocks. This number gets adjusted later. We return if we haven't
4125 * allocated blocks already inside this loop.
4127 error
= xfs_trans_reserve_quota_nblks(NULL
, ip
, (long)alen
, 0,
4128 rt
? XFS_QMOPT_RES_RTBLKS
: XFS_QMOPT_RES_REGBLKS
);
4133 * Split changing sb for alen and indlen since they could be coming
4134 * from different places.
4136 indlen
= (xfs_extlen_t
)xfs_bmap_worst_indlen(ip
, alen
);
4140 error
= xfs_mod_frextents(mp
, -((int64_t)extsz
));
4142 error
= xfs_mod_fdblocks(mp
, -((int64_t)alen
), false);
4146 goto out_unreserve_quota
;
4148 error
= xfs_mod_fdblocks(mp
, -((int64_t)indlen
), false);
4150 goto out_unreserve_blocks
;
4153 ip
->i_delayed_blks
+= alen
;
4155 got
->br_startoff
= aoff
;
4156 got
->br_startblock
= nullstartblock(indlen
);
4157 got
->br_blockcount
= alen
;
4158 got
->br_state
= XFS_EXT_NORM
;
4159 xfs_bmap_add_extent_hole_delay(ip
, lastx
, got
);
4162 * Update our extent pointer, given that xfs_bmap_add_extent_hole_delay
4163 * might have merged it into one of the neighbouring ones.
4165 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, *lastx
), got
);
4167 ASSERT(got
->br_startoff
<= aoff
);
4168 ASSERT(got
->br_startoff
+ got
->br_blockcount
>= aoff
+ alen
);
4169 ASSERT(isnullstartblock(got
->br_startblock
));
4170 ASSERT(got
->br_state
== XFS_EXT_NORM
);
4173 out_unreserve_blocks
:
4175 xfs_mod_frextents(mp
, extsz
);
4177 xfs_mod_fdblocks(mp
, alen
, false);
4178 out_unreserve_quota
:
4179 if (XFS_IS_QUOTA_ON(mp
))
4180 xfs_trans_unreserve_quota_nblks(NULL
, ip
, (long)alen
, 0, rt
?
4181 XFS_QMOPT_RES_RTBLKS
: XFS_QMOPT_RES_REGBLKS
);
4186 * Map file blocks to filesystem blocks, adding delayed allocations as needed.
4190 struct xfs_inode
*ip
, /* incore inode */
4191 xfs_fileoff_t bno
, /* starting file offs. mapped */
4192 xfs_filblks_t len
, /* length to map in file */
4193 struct xfs_bmbt_irec
*mval
, /* output: map values */
4194 int *nmap
, /* i/o: mval size/count */
4195 int flags
) /* XFS_BMAPI_... */
4197 struct xfs_mount
*mp
= ip
->i_mount
;
4198 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, XFS_DATA_FORK
);
4199 struct xfs_bmbt_irec got
; /* current file extent record */
4200 struct xfs_bmbt_irec prev
; /* previous file extent record */
4201 xfs_fileoff_t obno
; /* old block number (offset) */
4202 xfs_fileoff_t end
; /* end of mapped file region */
4203 xfs_extnum_t lastx
; /* last useful extent number */
4204 int eof
; /* we've hit the end of extents */
4205 int n
= 0; /* current extent index */
4209 ASSERT(*nmap
<= XFS_BMAP_MAX_NMAP
);
4210 ASSERT(!(flags
& ~XFS_BMAPI_ENTIRE
));
4211 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
4213 if (unlikely(XFS_TEST_ERROR(
4214 (XFS_IFORK_FORMAT(ip
, XFS_DATA_FORK
) != XFS_DINODE_FMT_EXTENTS
&&
4215 XFS_IFORK_FORMAT(ip
, XFS_DATA_FORK
) != XFS_DINODE_FMT_BTREE
),
4216 mp
, XFS_ERRTAG_BMAPIFORMAT
, XFS_RANDOM_BMAPIFORMAT
))) {
4217 XFS_ERROR_REPORT("xfs_bmapi_delay", XFS_ERRLEVEL_LOW
, mp
);
4218 return -EFSCORRUPTED
;
4221 if (XFS_FORCED_SHUTDOWN(mp
))
4224 XFS_STATS_INC(xs_blk_mapw
);
4226 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
4227 error
= xfs_iread_extents(NULL
, ip
, XFS_DATA_FORK
);
4232 xfs_bmap_search_extents(ip
, bno
, XFS_DATA_FORK
, &eof
, &lastx
, &got
, &prev
);
4236 while (bno
< end
&& n
< *nmap
) {
4237 if (eof
|| got
.br_startoff
> bno
) {
4238 error
= xfs_bmapi_reserve_delalloc(ip
, bno
, len
, &got
,
4239 &prev
, &lastx
, eof
);
4249 /* set up the extent map to return. */
4250 xfs_bmapi_trim_map(mval
, &got
, &bno
, len
, obno
, end
, n
, flags
);
4251 xfs_bmapi_update_map(&mval
, &bno
, &len
, obno
, end
, &n
, flags
);
4253 /* If we're done, stop now. */
4254 if (bno
>= end
|| n
>= *nmap
)
4257 /* Else go on to the next record. */
4259 if (++lastx
< ifp
->if_bytes
/ sizeof(xfs_bmbt_rec_t
))
4260 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, lastx
), &got
);
4272 struct xfs_bmalloca
*bma
)
4274 struct xfs_mount
*mp
= bma
->ip
->i_mount
;
4275 int whichfork
= (bma
->flags
& XFS_BMAPI_ATTRFORK
) ?
4276 XFS_ATTR_FORK
: XFS_DATA_FORK
;
4277 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
4278 int tmp_logflags
= 0;
4281 ASSERT(bma
->length
> 0);
4284 * For the wasdelay case, we could also just allocate the stuff asked
4285 * for in this bmap call but that wouldn't be as good.
4288 bma
->length
= (xfs_extlen_t
)bma
->got
.br_blockcount
;
4289 bma
->offset
= bma
->got
.br_startoff
;
4290 if (bma
->idx
!= NULLEXTNUM
&& bma
->idx
) {
4291 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
- 1),
4295 bma
->length
= XFS_FILBLKS_MIN(bma
->length
, MAXEXTLEN
);
4297 bma
->length
= XFS_FILBLKS_MIN(bma
->length
,
4298 bma
->got
.br_startoff
- bma
->offset
);
4302 * Indicate if this is the first user data in the file, or just any
4305 if (!(bma
->flags
& XFS_BMAPI_METADATA
)) {
4306 bma
->userdata
= (bma
->offset
== 0) ?
4307 XFS_ALLOC_INITIAL_USER_DATA
: XFS_ALLOC_USERDATA
;
4310 bma
->minlen
= (bma
->flags
& XFS_BMAPI_CONTIG
) ? bma
->length
: 1;
4313 * Only want to do the alignment at the eof if it is userdata and
4314 * allocation length is larger than a stripe unit.
4316 if (mp
->m_dalign
&& bma
->length
>= mp
->m_dalign
&&
4317 !(bma
->flags
& XFS_BMAPI_METADATA
) && whichfork
== XFS_DATA_FORK
) {
4318 error
= xfs_bmap_isaeof(bma
, whichfork
);
4323 error
= xfs_bmap_alloc(bma
);
4327 if (bma
->flist
->xbf_low
)
4330 bma
->cur
->bc_private
.b
.firstblock
= *bma
->firstblock
;
4331 if (bma
->blkno
== NULLFSBLOCK
)
4333 if ((ifp
->if_flags
& XFS_IFBROOT
) && !bma
->cur
) {
4334 bma
->cur
= xfs_bmbt_init_cursor(mp
, bma
->tp
, bma
->ip
, whichfork
);
4335 bma
->cur
->bc_private
.b
.firstblock
= *bma
->firstblock
;
4336 bma
->cur
->bc_private
.b
.flist
= bma
->flist
;
4339 * Bump the number of extents we've allocated
4345 bma
->cur
->bc_private
.b
.flags
=
4346 bma
->wasdel
? XFS_BTCUR_BPRV_WASDEL
: 0;
4348 bma
->got
.br_startoff
= bma
->offset
;
4349 bma
->got
.br_startblock
= bma
->blkno
;
4350 bma
->got
.br_blockcount
= bma
->length
;
4351 bma
->got
.br_state
= XFS_EXT_NORM
;
4354 * A wasdelay extent has been initialized, so shouldn't be flagged
4357 if (!bma
->wasdel
&& (bma
->flags
& XFS_BMAPI_PREALLOC
) &&
4358 xfs_sb_version_hasextflgbit(&mp
->m_sb
))
4359 bma
->got
.br_state
= XFS_EXT_UNWRITTEN
;
4362 error
= xfs_bmap_add_extent_delay_real(bma
);
4364 error
= xfs_bmap_add_extent_hole_real(bma
, whichfork
);
4366 bma
->logflags
|= tmp_logflags
;
4371 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4372 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4373 * the neighbouring ones.
4375 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
), &bma
->got
);
4377 ASSERT(bma
->got
.br_startoff
<= bma
->offset
);
4378 ASSERT(bma
->got
.br_startoff
+ bma
->got
.br_blockcount
>=
4379 bma
->offset
+ bma
->length
);
4380 ASSERT(bma
->got
.br_state
== XFS_EXT_NORM
||
4381 bma
->got
.br_state
== XFS_EXT_UNWRITTEN
);
4386 xfs_bmapi_convert_unwritten(
4387 struct xfs_bmalloca
*bma
,
4388 struct xfs_bmbt_irec
*mval
,
4392 int whichfork
= (flags
& XFS_BMAPI_ATTRFORK
) ?
4393 XFS_ATTR_FORK
: XFS_DATA_FORK
;
4394 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
4395 int tmp_logflags
= 0;
4398 /* check if we need to do unwritten->real conversion */
4399 if (mval
->br_state
== XFS_EXT_UNWRITTEN
&&
4400 (flags
& XFS_BMAPI_PREALLOC
))
4403 /* check if we need to do real->unwritten conversion */
4404 if (mval
->br_state
== XFS_EXT_NORM
&&
4405 (flags
& (XFS_BMAPI_PREALLOC
| XFS_BMAPI_CONVERT
)) !=
4406 (XFS_BMAPI_PREALLOC
| XFS_BMAPI_CONVERT
))
4410 * Modify (by adding) the state flag, if writing.
4412 ASSERT(mval
->br_blockcount
<= len
);
4413 if ((ifp
->if_flags
& XFS_IFBROOT
) && !bma
->cur
) {
4414 bma
->cur
= xfs_bmbt_init_cursor(bma
->ip
->i_mount
, bma
->tp
,
4415 bma
->ip
, whichfork
);
4416 bma
->cur
->bc_private
.b
.firstblock
= *bma
->firstblock
;
4417 bma
->cur
->bc_private
.b
.flist
= bma
->flist
;
4419 mval
->br_state
= (mval
->br_state
== XFS_EXT_UNWRITTEN
)
4420 ? XFS_EXT_NORM
: XFS_EXT_UNWRITTEN
;
4422 error
= xfs_bmap_add_extent_unwritten_real(bma
->tp
, bma
->ip
, &bma
->idx
,
4423 &bma
->cur
, mval
, bma
->firstblock
, bma
->flist
,
4426 * Log the inode core unconditionally in the unwritten extent conversion
4427 * path because the conversion might not have done so (e.g., if the
4428 * extent count hasn't changed). We need to make sure the inode is dirty
4429 * in the transaction for the sake of fsync(), even if nothing has
4430 * changed, because fsync() will not force the log for this transaction
4431 * unless it sees the inode pinned.
4433 bma
->logflags
|= tmp_logflags
| XFS_ILOG_CORE
;
4438 * Update our extent pointer, given that
4439 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4440 * of the neighbouring ones.
4442 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
), &bma
->got
);
4445 * We may have combined previously unwritten space with written space,
4446 * so generate another request.
4448 if (mval
->br_blockcount
< len
)
4454 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4455 * extent state if necessary. Details behaviour is controlled by the flags
4456 * parameter. Only allocates blocks from a single allocation group, to avoid
4459 * The returned value in "firstblock" from the first call in a transaction
4460 * must be remembered and presented to subsequent calls in "firstblock".
4461 * An upper bound for the number of blocks to be allocated is supplied to
4462 * the first call in "total"; if no allocation group has that many free
4463 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4467 struct xfs_trans
*tp
, /* transaction pointer */
4468 struct xfs_inode
*ip
, /* incore inode */
4469 xfs_fileoff_t bno
, /* starting file offs. mapped */
4470 xfs_filblks_t len
, /* length to map in file */
4471 int flags
, /* XFS_BMAPI_... */
4472 xfs_fsblock_t
*firstblock
, /* first allocated block
4473 controls a.g. for allocs */
4474 xfs_extlen_t total
, /* total blocks needed */
4475 struct xfs_bmbt_irec
*mval
, /* output: map values */
4476 int *nmap
, /* i/o: mval size/count */
4477 struct xfs_bmap_free
*flist
) /* i/o: list extents to free */
4479 struct xfs_mount
*mp
= ip
->i_mount
;
4480 struct xfs_ifork
*ifp
;
4481 struct xfs_bmalloca bma
= { NULL
}; /* args for xfs_bmap_alloc */
4482 xfs_fileoff_t end
; /* end of mapped file region */
4483 int eof
; /* after the end of extents */
4484 int error
; /* error return */
4485 int n
; /* current extent index */
4486 xfs_fileoff_t obno
; /* old block number (offset) */
4487 int whichfork
; /* data or attr fork */
4488 char inhole
; /* current location is hole in file */
4489 char wasdelay
; /* old extent was delayed */
4492 xfs_fileoff_t orig_bno
; /* original block number value */
4493 int orig_flags
; /* original flags arg value */
4494 xfs_filblks_t orig_len
; /* original value of len arg */
4495 struct xfs_bmbt_irec
*orig_mval
; /* original value of mval */
4496 int orig_nmap
; /* original value of *nmap */
4504 whichfork
= (flags
& XFS_BMAPI_ATTRFORK
) ?
4505 XFS_ATTR_FORK
: XFS_DATA_FORK
;
4508 ASSERT(*nmap
<= XFS_BMAP_MAX_NMAP
);
4509 ASSERT(!(flags
& XFS_BMAPI_IGSTATE
));
4512 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_LOCAL
);
4513 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
4515 if (unlikely(XFS_TEST_ERROR(
4516 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
4517 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
4518 mp
, XFS_ERRTAG_BMAPIFORMAT
, XFS_RANDOM_BMAPIFORMAT
))) {
4519 XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW
, mp
);
4520 return -EFSCORRUPTED
;
4523 if (XFS_FORCED_SHUTDOWN(mp
))
4526 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4528 XFS_STATS_INC(xs_blk_mapw
);
4530 if (*firstblock
== NULLFSBLOCK
) {
4531 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
)
4532 bma
.minleft
= be16_to_cpu(ifp
->if_broot
->bb_level
) + 1;
4539 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
4540 error
= xfs_iread_extents(tp
, ip
, whichfork
);
4545 xfs_bmap_search_extents(ip
, bno
, whichfork
, &eof
, &bma
.idx
, &bma
.got
,
4556 bma
.firstblock
= firstblock
;
4558 while (bno
< end
&& n
< *nmap
) {
4559 inhole
= eof
|| bma
.got
.br_startoff
> bno
;
4560 wasdelay
= !inhole
&& isnullstartblock(bma
.got
.br_startblock
);
4563 * First, deal with the hole before the allocated space
4564 * that we found, if any.
4566 if (inhole
|| wasdelay
) {
4568 bma
.conv
= !!(flags
& XFS_BMAPI_CONVERT
);
4569 bma
.wasdel
= wasdelay
;
4574 * There's a 32/64 bit type mismatch between the
4575 * allocation length request (which can be 64 bits in
4576 * length) and the bma length request, which is
4577 * xfs_extlen_t and therefore 32 bits. Hence we have to
4578 * check for 32-bit overflows and handle them here.
4580 if (len
> (xfs_filblks_t
)MAXEXTLEN
)
4581 bma
.length
= MAXEXTLEN
;
4586 ASSERT(bma
.length
> 0);
4587 error
= xfs_bmapi_allocate(&bma
);
4590 if (bma
.blkno
== NULLFSBLOCK
)
4594 /* Deal with the allocated space we found. */
4595 xfs_bmapi_trim_map(mval
, &bma
.got
, &bno
, len
, obno
,
4598 /* Execute unwritten extent conversion if necessary */
4599 error
= xfs_bmapi_convert_unwritten(&bma
, mval
, len
, flags
);
4600 if (error
== -EAGAIN
)
4605 /* update the extent map to return */
4606 xfs_bmapi_update_map(&mval
, &bno
, &len
, obno
, end
, &n
, flags
);
4609 * If we're done, stop now. Stop when we've allocated
4610 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4611 * the transaction may get too big.
4613 if (bno
>= end
|| n
>= *nmap
|| bma
.nallocs
>= *nmap
)
4616 /* Else go on to the next record. */
4618 if (++bma
.idx
< ifp
->if_bytes
/ sizeof(xfs_bmbt_rec_t
)) {
4619 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
.idx
),
4627 * Transform from btree to extents, give it cur.
4629 if (xfs_bmap_wants_extents(ip
, whichfork
)) {
4630 int tmp_logflags
= 0;
4633 error
= xfs_bmap_btree_to_extents(tp
, ip
, bma
.cur
,
4634 &tmp_logflags
, whichfork
);
4635 bma
.logflags
|= tmp_logflags
;
4640 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
||
4641 XFS_IFORK_NEXTENTS(ip
, whichfork
) >
4642 XFS_IFORK_MAXEXT(ip
, whichfork
));
4646 * Log everything. Do this after conversion, there's no point in
4647 * logging the extent records if we've converted to btree format.
4649 if ((bma
.logflags
& xfs_ilog_fext(whichfork
)) &&
4650 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
4651 bma
.logflags
&= ~xfs_ilog_fext(whichfork
);
4652 else if ((bma
.logflags
& xfs_ilog_fbroot(whichfork
)) &&
4653 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)
4654 bma
.logflags
&= ~xfs_ilog_fbroot(whichfork
);
4656 * Log whatever the flags say, even if error. Otherwise we might miss
4657 * detecting a case where the data is changed, there's an error,
4658 * and it's not logged so we don't shutdown when we should.
4661 xfs_trans_log_inode(tp
, ip
, bma
.logflags
);
4665 ASSERT(*firstblock
== NULLFSBLOCK
||
4666 XFS_FSB_TO_AGNO(mp
, *firstblock
) ==
4668 bma
.cur
->bc_private
.b
.firstblock
) ||
4670 XFS_FSB_TO_AGNO(mp
, *firstblock
) <
4672 bma
.cur
->bc_private
.b
.firstblock
)));
4673 *firstblock
= bma
.cur
->bc_private
.b
.firstblock
;
4675 xfs_btree_del_cursor(bma
.cur
,
4676 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
4679 xfs_bmap_validate_ret(orig_bno
, orig_len
, orig_flags
, orig_mval
,
4685 * Called by xfs_bmapi to update file extent records and the btree
4686 * after removing space (or undoing a delayed allocation).
4688 STATIC
int /* error */
4689 xfs_bmap_del_extent(
4690 xfs_inode_t
*ip
, /* incore inode pointer */
4691 xfs_trans_t
*tp
, /* current transaction pointer */
4692 xfs_extnum_t
*idx
, /* extent number to update/delete */
4693 xfs_bmap_free_t
*flist
, /* list of extents to be freed */
4694 xfs_btree_cur_t
*cur
, /* if null, not a btree */
4695 xfs_bmbt_irec_t
*del
, /* data to remove from extents */
4696 int *logflagsp
, /* inode logging flags */
4697 int whichfork
) /* data or attr fork */
4699 xfs_filblks_t da_new
; /* new delay-alloc indirect blocks */
4700 xfs_filblks_t da_old
; /* old delay-alloc indirect blocks */
4701 xfs_fsblock_t del_endblock
=0; /* first block past del */
4702 xfs_fileoff_t del_endoff
; /* first offset past del */
4703 int delay
; /* current block is delayed allocated */
4704 int do_fx
; /* free extent at end of routine */
4705 xfs_bmbt_rec_host_t
*ep
; /* current extent entry pointer */
4706 int error
; /* error return value */
4707 int flags
; /* inode logging flags */
4708 xfs_bmbt_irec_t got
; /* current extent entry */
4709 xfs_fileoff_t got_endoff
; /* first offset past got */
4710 int i
; /* temp state */
4711 xfs_ifork_t
*ifp
; /* inode fork pointer */
4712 xfs_mount_t
*mp
; /* mount structure */
4713 xfs_filblks_t nblks
; /* quota/sb block count */
4714 xfs_bmbt_irec_t
new; /* new record to be inserted */
4716 uint qfield
; /* quota field to update */
4717 xfs_filblks_t temp
; /* for indirect length calculations */
4718 xfs_filblks_t temp2
; /* for indirect length calculations */
4721 XFS_STATS_INC(xs_del_exlist
);
4723 if (whichfork
== XFS_ATTR_FORK
)
4724 state
|= BMAP_ATTRFORK
;
4727 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4728 ASSERT((*idx
>= 0) && (*idx
< ifp
->if_bytes
/
4729 (uint
)sizeof(xfs_bmbt_rec_t
)));
4730 ASSERT(del
->br_blockcount
> 0);
4731 ep
= xfs_iext_get_ext(ifp
, *idx
);
4732 xfs_bmbt_get_all(ep
, &got
);
4733 ASSERT(got
.br_startoff
<= del
->br_startoff
);
4734 del_endoff
= del
->br_startoff
+ del
->br_blockcount
;
4735 got_endoff
= got
.br_startoff
+ got
.br_blockcount
;
4736 ASSERT(got_endoff
>= del_endoff
);
4737 delay
= isnullstartblock(got
.br_startblock
);
4738 ASSERT(isnullstartblock(del
->br_startblock
) == delay
);
4743 * If deleting a real allocation, must free up the disk space.
4746 flags
= XFS_ILOG_CORE
;
4748 * Realtime allocation. Free it and record di_nblocks update.
4750 if (whichfork
== XFS_DATA_FORK
&& XFS_IS_REALTIME_INODE(ip
)) {
4754 ASSERT(do_mod(del
->br_blockcount
,
4755 mp
->m_sb
.sb_rextsize
) == 0);
4756 ASSERT(do_mod(del
->br_startblock
,
4757 mp
->m_sb
.sb_rextsize
) == 0);
4758 bno
= del
->br_startblock
;
4759 len
= del
->br_blockcount
;
4760 do_div(bno
, mp
->m_sb
.sb_rextsize
);
4761 do_div(len
, mp
->m_sb
.sb_rextsize
);
4762 error
= xfs_rtfree_extent(tp
, bno
, (xfs_extlen_t
)len
);
4766 nblks
= len
* mp
->m_sb
.sb_rextsize
;
4767 qfield
= XFS_TRANS_DQ_RTBCOUNT
;
4770 * Ordinary allocation.
4774 nblks
= del
->br_blockcount
;
4775 qfield
= XFS_TRANS_DQ_BCOUNT
;
4778 * Set up del_endblock and cur for later.
4780 del_endblock
= del
->br_startblock
+ del
->br_blockcount
;
4782 if ((error
= xfs_bmbt_lookup_eq(cur
, got
.br_startoff
,
4783 got
.br_startblock
, got
.br_blockcount
,
4786 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
4788 da_old
= da_new
= 0;
4790 da_old
= startblockval(got
.br_startblock
);
4796 * Set flag value to use in switch statement.
4797 * Left-contig is 2, right-contig is 1.
4799 switch (((got
.br_startoff
== del
->br_startoff
) << 1) |
4800 (got_endoff
== del_endoff
)) {
4803 * Matches the whole extent. Delete the entry.
4805 xfs_iext_remove(ip
, *idx
, 1,
4806 whichfork
== XFS_ATTR_FORK
? BMAP_ATTRFORK
: 0);
4811 XFS_IFORK_NEXT_SET(ip
, whichfork
,
4812 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
4813 flags
|= XFS_ILOG_CORE
;
4815 flags
|= xfs_ilog_fext(whichfork
);
4818 if ((error
= xfs_btree_delete(cur
, &i
)))
4820 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
4825 * Deleting the first part of the extent.
4827 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
4828 xfs_bmbt_set_startoff(ep
, del_endoff
);
4829 temp
= got
.br_blockcount
- del
->br_blockcount
;
4830 xfs_bmbt_set_blockcount(ep
, temp
);
4832 temp
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
4834 xfs_bmbt_set_startblock(ep
, nullstartblock((int)temp
));
4835 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
4839 xfs_bmbt_set_startblock(ep
, del_endblock
);
4840 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
4842 flags
|= xfs_ilog_fext(whichfork
);
4845 if ((error
= xfs_bmbt_update(cur
, del_endoff
, del_endblock
,
4846 got
.br_blockcount
- del
->br_blockcount
,
4853 * Deleting the last part of the extent.
4855 temp
= got
.br_blockcount
- del
->br_blockcount
;
4856 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
4857 xfs_bmbt_set_blockcount(ep
, temp
);
4859 temp
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
4861 xfs_bmbt_set_startblock(ep
, nullstartblock((int)temp
));
4862 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
4866 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
4868 flags
|= xfs_ilog_fext(whichfork
);
4871 if ((error
= xfs_bmbt_update(cur
, got
.br_startoff
,
4873 got
.br_blockcount
- del
->br_blockcount
,
4880 * Deleting the middle of the extent.
4882 temp
= del
->br_startoff
- got
.br_startoff
;
4883 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
4884 xfs_bmbt_set_blockcount(ep
, temp
);
4885 new.br_startoff
= del_endoff
;
4886 temp2
= got_endoff
- del_endoff
;
4887 new.br_blockcount
= temp2
;
4888 new.br_state
= got
.br_state
;
4890 new.br_startblock
= del_endblock
;
4891 flags
|= XFS_ILOG_CORE
;
4893 if ((error
= xfs_bmbt_update(cur
,
4895 got
.br_startblock
, temp
,
4898 if ((error
= xfs_btree_increment(cur
, 0, &i
)))
4900 cur
->bc_rec
.b
= new;
4901 error
= xfs_btree_insert(cur
, &i
);
4902 if (error
&& error
!= -ENOSPC
)
4905 * If get no-space back from btree insert,
4906 * it tried a split, and we have a zero
4907 * block reservation.
4908 * Fix up our state and return the error.
4910 if (error
== -ENOSPC
) {
4912 * Reset the cursor, don't trust
4913 * it after any insert operation.
4915 if ((error
= xfs_bmbt_lookup_eq(cur
,
4920 XFS_WANT_CORRUPTED_GOTO(mp
,
4923 * Update the btree record back
4924 * to the original value.
4926 if ((error
= xfs_bmbt_update(cur
,
4933 * Reset the extent record back
4934 * to the original value.
4936 xfs_bmbt_set_blockcount(ep
,
4942 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
4944 flags
|= xfs_ilog_fext(whichfork
);
4945 XFS_IFORK_NEXT_SET(ip
, whichfork
,
4946 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
4948 ASSERT(whichfork
== XFS_DATA_FORK
);
4949 temp
= xfs_bmap_worst_indlen(ip
, temp
);
4950 xfs_bmbt_set_startblock(ep
, nullstartblock((int)temp
));
4951 temp2
= xfs_bmap_worst_indlen(ip
, temp2
);
4952 new.br_startblock
= nullstartblock((int)temp2
);
4953 da_new
= temp
+ temp2
;
4954 while (da_new
> da_old
) {
4958 xfs_bmbt_set_startblock(ep
,
4959 nullstartblock((int)temp
));
4961 if (da_new
== da_old
)
4967 nullstartblock((int)temp2
);
4971 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
4972 xfs_iext_insert(ip
, *idx
+ 1, 1, &new, state
);
4977 * If we need to, add to list of extents to delete.
4980 xfs_bmap_add_free(del
->br_startblock
, del
->br_blockcount
, flist
,
4983 * Adjust inode # blocks in the file.
4986 ip
->i_d
.di_nblocks
-= nblks
;
4988 * Adjust quota data.
4991 xfs_trans_mod_dquot_byino(tp
, ip
, qfield
, (long)-nblks
);
4994 * Account for change in delayed indirect blocks.
4995 * Nothing to do for disk quota accounting here.
4997 ASSERT(da_old
>= da_new
);
4998 if (da_old
> da_new
)
4999 xfs_mod_fdblocks(mp
, (int64_t)(da_old
- da_new
), false);
5006 * Unmap (remove) blocks from a file.
5007 * If nexts is nonzero then the number of extents to remove is limited to
5008 * that value. If not all extents in the block range can be removed then
5013 xfs_trans_t
*tp
, /* transaction pointer */
5014 struct xfs_inode
*ip
, /* incore inode */
5015 xfs_fileoff_t bno
, /* starting offset to unmap */
5016 xfs_filblks_t len
, /* length to unmap in file */
5017 int flags
, /* misc flags */
5018 xfs_extnum_t nexts
, /* number of extents max */
5019 xfs_fsblock_t
*firstblock
, /* first allocated block
5020 controls a.g. for allocs */
5021 xfs_bmap_free_t
*flist
, /* i/o: list extents to free */
5022 int *done
) /* set if not done yet */
5024 xfs_btree_cur_t
*cur
; /* bmap btree cursor */
5025 xfs_bmbt_irec_t del
; /* extent being deleted */
5026 int eof
; /* is deleting at eof */
5027 xfs_bmbt_rec_host_t
*ep
; /* extent record pointer */
5028 int error
; /* error return value */
5029 xfs_extnum_t extno
; /* extent number in list */
5030 xfs_bmbt_irec_t got
; /* current extent record */
5031 xfs_ifork_t
*ifp
; /* inode fork pointer */
5032 int isrt
; /* freeing in rt area */
5033 xfs_extnum_t lastx
; /* last extent index used */
5034 int logflags
; /* transaction logging flags */
5035 xfs_extlen_t mod
; /* rt extent offset */
5036 xfs_mount_t
*mp
; /* mount structure */
5037 xfs_extnum_t nextents
; /* number of file extents */
5038 xfs_bmbt_irec_t prev
; /* previous extent record */
5039 xfs_fileoff_t start
; /* first file offset deleted */
5040 int tmp_logflags
; /* partial logging flags */
5041 int wasdel
; /* was a delayed alloc extent */
5042 int whichfork
; /* data or attribute fork */
5045 trace_xfs_bunmap(ip
, bno
, len
, flags
, _RET_IP_
);
5047 whichfork
= (flags
& XFS_BMAPI_ATTRFORK
) ?
5048 XFS_ATTR_FORK
: XFS_DATA_FORK
;
5049 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5051 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
5052 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)) {
5053 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW
,
5055 return -EFSCORRUPTED
;
5058 if (XFS_FORCED_SHUTDOWN(mp
))
5061 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
5065 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
5066 (error
= xfs_iread_extents(tp
, ip
, whichfork
)))
5068 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
5069 if (nextents
== 0) {
5073 XFS_STATS_INC(xs_blk_unmap
);
5074 isrt
= (whichfork
== XFS_DATA_FORK
) && XFS_IS_REALTIME_INODE(ip
);
5076 bno
= start
+ len
- 1;
5077 ep
= xfs_bmap_search_extents(ip
, bno
, whichfork
, &eof
, &lastx
, &got
,
5081 * Check to see if the given block number is past the end of the
5082 * file, back up to the last block if so...
5085 ep
= xfs_iext_get_ext(ifp
, --lastx
);
5086 xfs_bmbt_get_all(ep
, &got
);
5087 bno
= got
.br_startoff
+ got
.br_blockcount
- 1;
5090 if (ifp
->if_flags
& XFS_IFBROOT
) {
5091 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
);
5092 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
5093 cur
->bc_private
.b
.firstblock
= *firstblock
;
5094 cur
->bc_private
.b
.flist
= flist
;
5095 cur
->bc_private
.b
.flags
= 0;
5101 * Synchronize by locking the bitmap inode.
5103 xfs_ilock(mp
->m_rbmip
, XFS_ILOCK_EXCL
);
5104 xfs_trans_ijoin(tp
, mp
->m_rbmip
, XFS_ILOCK_EXCL
);
5108 while (bno
!= (xfs_fileoff_t
)-1 && bno
>= start
&& lastx
>= 0 &&
5109 (nexts
== 0 || extno
< nexts
)) {
5111 * Is the found extent after a hole in which bno lives?
5112 * Just back up to the previous extent, if so.
5114 if (got
.br_startoff
> bno
) {
5117 ep
= xfs_iext_get_ext(ifp
, lastx
);
5118 xfs_bmbt_get_all(ep
, &got
);
5121 * Is the last block of this extent before the range
5122 * we're supposed to delete? If so, we're done.
5124 bno
= XFS_FILEOFF_MIN(bno
,
5125 got
.br_startoff
+ got
.br_blockcount
- 1);
5129 * Then deal with the (possibly delayed) allocated space
5134 wasdel
= isnullstartblock(del
.br_startblock
);
5135 if (got
.br_startoff
< start
) {
5136 del
.br_startoff
= start
;
5137 del
.br_blockcount
-= start
- got
.br_startoff
;
5139 del
.br_startblock
+= start
- got
.br_startoff
;
5141 if (del
.br_startoff
+ del
.br_blockcount
> bno
+ 1)
5142 del
.br_blockcount
= bno
+ 1 - del
.br_startoff
;
5143 sum
= del
.br_startblock
+ del
.br_blockcount
;
5145 (mod
= do_mod(sum
, mp
->m_sb
.sb_rextsize
))) {
5147 * Realtime extent not lined up at the end.
5148 * The extent could have been split into written
5149 * and unwritten pieces, or we could just be
5150 * unmapping part of it. But we can't really
5151 * get rid of part of a realtime extent.
5153 if (del
.br_state
== XFS_EXT_UNWRITTEN
||
5154 !xfs_sb_version_hasextflgbit(&mp
->m_sb
)) {
5156 * This piece is unwritten, or we're not
5157 * using unwritten extents. Skip over it.
5160 bno
-= mod
> del
.br_blockcount
?
5161 del
.br_blockcount
: mod
;
5162 if (bno
< got
.br_startoff
) {
5164 xfs_bmbt_get_all(xfs_iext_get_ext(
5170 * It's written, turn it unwritten.
5171 * This is better than zeroing it.
5173 ASSERT(del
.br_state
== XFS_EXT_NORM
);
5174 ASSERT(xfs_trans_get_block_res(tp
) > 0);
5176 * If this spans a realtime extent boundary,
5177 * chop it back to the start of the one we end at.
5179 if (del
.br_blockcount
> mod
) {
5180 del
.br_startoff
+= del
.br_blockcount
- mod
;
5181 del
.br_startblock
+= del
.br_blockcount
- mod
;
5182 del
.br_blockcount
= mod
;
5184 del
.br_state
= XFS_EXT_UNWRITTEN
;
5185 error
= xfs_bmap_add_extent_unwritten_real(tp
, ip
,
5186 &lastx
, &cur
, &del
, firstblock
, flist
,
5192 if (isrt
&& (mod
= do_mod(del
.br_startblock
, mp
->m_sb
.sb_rextsize
))) {
5194 * Realtime extent is lined up at the end but not
5195 * at the front. We'll get rid of full extents if
5198 mod
= mp
->m_sb
.sb_rextsize
- mod
;
5199 if (del
.br_blockcount
> mod
) {
5200 del
.br_blockcount
-= mod
;
5201 del
.br_startoff
+= mod
;
5202 del
.br_startblock
+= mod
;
5203 } else if ((del
.br_startoff
== start
&&
5204 (del
.br_state
== XFS_EXT_UNWRITTEN
||
5205 xfs_trans_get_block_res(tp
) == 0)) ||
5206 !xfs_sb_version_hasextflgbit(&mp
->m_sb
)) {
5208 * Can't make it unwritten. There isn't
5209 * a full extent here so just skip it.
5211 ASSERT(bno
>= del
.br_blockcount
);
5212 bno
-= del
.br_blockcount
;
5213 if (got
.br_startoff
> bno
) {
5215 ep
= xfs_iext_get_ext(ifp
,
5217 xfs_bmbt_get_all(ep
, &got
);
5221 } else if (del
.br_state
== XFS_EXT_UNWRITTEN
) {
5223 * This one is already unwritten.
5224 * It must have a written left neighbor.
5225 * Unwrite the killed part of that one and
5229 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
,
5231 ASSERT(prev
.br_state
== XFS_EXT_NORM
);
5232 ASSERT(!isnullstartblock(prev
.br_startblock
));
5233 ASSERT(del
.br_startblock
==
5234 prev
.br_startblock
+ prev
.br_blockcount
);
5235 if (prev
.br_startoff
< start
) {
5236 mod
= start
- prev
.br_startoff
;
5237 prev
.br_blockcount
-= mod
;
5238 prev
.br_startblock
+= mod
;
5239 prev
.br_startoff
= start
;
5241 prev
.br_state
= XFS_EXT_UNWRITTEN
;
5243 error
= xfs_bmap_add_extent_unwritten_real(tp
,
5244 ip
, &lastx
, &cur
, &prev
,
5245 firstblock
, flist
, &logflags
);
5250 ASSERT(del
.br_state
== XFS_EXT_NORM
);
5251 del
.br_state
= XFS_EXT_UNWRITTEN
;
5252 error
= xfs_bmap_add_extent_unwritten_real(tp
,
5253 ip
, &lastx
, &cur
, &del
,
5254 firstblock
, flist
, &logflags
);
5261 ASSERT(startblockval(del
.br_startblock
) > 0);
5262 /* Update realtime/data freespace, unreserve quota */
5264 xfs_filblks_t rtexts
;
5266 rtexts
= XFS_FSB_TO_B(mp
, del
.br_blockcount
);
5267 do_div(rtexts
, mp
->m_sb
.sb_rextsize
);
5268 xfs_mod_frextents(mp
, (int64_t)rtexts
);
5269 (void)xfs_trans_reserve_quota_nblks(NULL
,
5270 ip
, -((long)del
.br_blockcount
), 0,
5271 XFS_QMOPT_RES_RTBLKS
);
5273 xfs_mod_fdblocks(mp
, (int64_t)del
.br_blockcount
,
5275 (void)xfs_trans_reserve_quota_nblks(NULL
,
5276 ip
, -((long)del
.br_blockcount
), 0,
5277 XFS_QMOPT_RES_REGBLKS
);
5279 ip
->i_delayed_blks
-= del
.br_blockcount
;
5281 cur
->bc_private
.b
.flags
|=
5282 XFS_BTCUR_BPRV_WASDEL
;
5284 cur
->bc_private
.b
.flags
&= ~XFS_BTCUR_BPRV_WASDEL
;
5286 * If it's the case where the directory code is running
5287 * with no block reservation, and the deleted block is in
5288 * the middle of its extent, and the resulting insert
5289 * of an extent would cause transformation to btree format,
5290 * then reject it. The calling code will then swap
5291 * blocks around instead.
5292 * We have to do this now, rather than waiting for the
5293 * conversion to btree format, since the transaction
5296 if (!wasdel
&& xfs_trans_get_block_res(tp
) == 0 &&
5297 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
&&
5298 XFS_IFORK_NEXTENTS(ip
, whichfork
) >= /* Note the >= */
5299 XFS_IFORK_MAXEXT(ip
, whichfork
) &&
5300 del
.br_startoff
> got
.br_startoff
&&
5301 del
.br_startoff
+ del
.br_blockcount
<
5302 got
.br_startoff
+ got
.br_blockcount
) {
5306 error
= xfs_bmap_del_extent(ip
, tp
, &lastx
, flist
, cur
, &del
,
5307 &tmp_logflags
, whichfork
);
5308 logflags
|= tmp_logflags
;
5311 bno
= del
.br_startoff
- 1;
5314 * If not done go on to the next (previous) record.
5316 if (bno
!= (xfs_fileoff_t
)-1 && bno
>= start
) {
5318 ep
= xfs_iext_get_ext(ifp
, lastx
);
5319 if (xfs_bmbt_get_startoff(ep
) > bno
) {
5321 ep
= xfs_iext_get_ext(ifp
,
5324 xfs_bmbt_get_all(ep
, &got
);
5329 *done
= bno
== (xfs_fileoff_t
)-1 || bno
< start
|| lastx
< 0;
5332 * Convert to a btree if necessary.
5334 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
5335 ASSERT(cur
== NULL
);
5336 error
= xfs_bmap_extents_to_btree(tp
, ip
, firstblock
, flist
,
5337 &cur
, 0, &tmp_logflags
, whichfork
);
5338 logflags
|= tmp_logflags
;
5343 * transform from btree to extents, give it cur
5345 else if (xfs_bmap_wants_extents(ip
, whichfork
)) {
5346 ASSERT(cur
!= NULL
);
5347 error
= xfs_bmap_btree_to_extents(tp
, ip
, cur
, &tmp_logflags
,
5349 logflags
|= tmp_logflags
;
5354 * transform from extents to local?
5359 * Log everything. Do this after conversion, there's no point in
5360 * logging the extent records if we've converted to btree format.
5362 if ((logflags
& xfs_ilog_fext(whichfork
)) &&
5363 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
5364 logflags
&= ~xfs_ilog_fext(whichfork
);
5365 else if ((logflags
& xfs_ilog_fbroot(whichfork
)) &&
5366 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)
5367 logflags
&= ~xfs_ilog_fbroot(whichfork
);
5369 * Log inode even in the error case, if the transaction
5370 * is dirty we'll need to shut down the filesystem.
5373 xfs_trans_log_inode(tp
, ip
, logflags
);
5376 *firstblock
= cur
->bc_private
.b
.firstblock
;
5377 cur
->bc_private
.b
.allocated
= 0;
5379 xfs_btree_del_cursor(cur
,
5380 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
5386 * Determine whether an extent shift can be accomplished by a merge with the
5387 * extent that precedes the target hole of the shift.
5391 struct xfs_bmbt_irec
*left
, /* preceding extent */
5392 struct xfs_bmbt_irec
*got
, /* current extent to shift */
5393 xfs_fileoff_t shift
) /* shift fsb */
5395 xfs_fileoff_t startoff
;
5397 startoff
= got
->br_startoff
- shift
;
5400 * The extent, once shifted, must be adjacent in-file and on-disk with
5401 * the preceding extent.
5403 if ((left
->br_startoff
+ left
->br_blockcount
!= startoff
) ||
5404 (left
->br_startblock
+ left
->br_blockcount
!= got
->br_startblock
) ||
5405 (left
->br_state
!= got
->br_state
) ||
5406 (left
->br_blockcount
+ got
->br_blockcount
> MAXEXTLEN
))
5413 * A bmap extent shift adjusts the file offset of an extent to fill a preceding
5414 * hole in the file. If an extent shift would result in the extent being fully
5415 * adjacent to the extent that currently precedes the hole, we can merge with
5416 * the preceding extent rather than do the shift.
5418 * This function assumes the caller has verified a shift-by-merge is possible
5419 * with the provided extents via xfs_bmse_can_merge().
5423 struct xfs_inode
*ip
,
5425 xfs_fileoff_t shift
, /* shift fsb */
5426 int current_ext
, /* idx of gotp */
5427 struct xfs_bmbt_rec_host
*gotp
, /* extent to shift */
5428 struct xfs_bmbt_rec_host
*leftp
, /* preceding extent */
5429 struct xfs_btree_cur
*cur
,
5430 int *logflags
) /* output */
5432 struct xfs_bmbt_irec got
;
5433 struct xfs_bmbt_irec left
;
5434 xfs_filblks_t blockcount
;
5436 struct xfs_mount
*mp
= ip
->i_mount
;
5438 xfs_bmbt_get_all(gotp
, &got
);
5439 xfs_bmbt_get_all(leftp
, &left
);
5440 blockcount
= left
.br_blockcount
+ got
.br_blockcount
;
5442 ASSERT(xfs_isilocked(ip
, XFS_IOLOCK_EXCL
));
5443 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
5444 ASSERT(xfs_bmse_can_merge(&left
, &got
, shift
));
5447 * Merge the in-core extents. Note that the host record pointers and
5448 * current_ext index are invalid once the extent has been removed via
5449 * xfs_iext_remove().
5451 xfs_bmbt_set_blockcount(leftp
, blockcount
);
5452 xfs_iext_remove(ip
, current_ext
, 1, 0);
5455 * Update the on-disk extent count, the btree if necessary and log the
5458 XFS_IFORK_NEXT_SET(ip
, whichfork
,
5459 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
5460 *logflags
|= XFS_ILOG_CORE
;
5462 *logflags
|= XFS_ILOG_DEXT
;
5466 /* lookup and remove the extent to merge */
5467 error
= xfs_bmbt_lookup_eq(cur
, got
.br_startoff
, got
.br_startblock
,
5468 got
.br_blockcount
, &i
);
5471 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
5473 error
= xfs_btree_delete(cur
, &i
);
5476 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
5478 /* lookup and update size of the previous extent */
5479 error
= xfs_bmbt_lookup_eq(cur
, left
.br_startoff
, left
.br_startblock
,
5480 left
.br_blockcount
, &i
);
5483 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
5485 left
.br_blockcount
= blockcount
;
5487 return xfs_bmbt_update(cur
, left
.br_startoff
, left
.br_startblock
,
5488 left
.br_blockcount
, left
.br_state
);
5492 * Shift a single extent.
5496 struct xfs_inode
*ip
,
5498 xfs_fileoff_t offset_shift_fsb
,
5500 struct xfs_bmbt_rec_host
*gotp
,
5501 struct xfs_btree_cur
*cur
,
5503 enum shift_direction direction
)
5505 struct xfs_ifork
*ifp
;
5506 struct xfs_mount
*mp
;
5507 xfs_fileoff_t startoff
;
5508 struct xfs_bmbt_rec_host
*adj_irecp
;
5509 struct xfs_bmbt_irec got
;
5510 struct xfs_bmbt_irec adj_irec
;
5516 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5517 total_extents
= ifp
->if_bytes
/ sizeof(xfs_bmbt_rec_t
);
5519 xfs_bmbt_get_all(gotp
, &got
);
5521 /* delalloc extents should be prevented by caller */
5522 XFS_WANT_CORRUPTED_RETURN(mp
, !isnullstartblock(got
.br_startblock
));
5524 if (direction
== SHIFT_LEFT
) {
5525 startoff
= got
.br_startoff
- offset_shift_fsb
;
5528 * Check for merge if we've got an extent to the left,
5529 * otherwise make sure there's enough room at the start
5530 * of the file for the shift.
5532 if (!*current_ext
) {
5533 if (got
.br_startoff
< offset_shift_fsb
)
5535 goto update_current_ext
;
5538 * grab the left extent and check for a large
5541 adj_irecp
= xfs_iext_get_ext(ifp
, *current_ext
- 1);
5542 xfs_bmbt_get_all(adj_irecp
, &adj_irec
);
5545 adj_irec
.br_startoff
+ adj_irec
.br_blockcount
)
5548 /* check whether to merge the extent or shift it down */
5549 if (xfs_bmse_can_merge(&adj_irec
, &got
,
5550 offset_shift_fsb
)) {
5551 return xfs_bmse_merge(ip
, whichfork
, offset_shift_fsb
,
5552 *current_ext
, gotp
, adj_irecp
,
5556 startoff
= got
.br_startoff
+ offset_shift_fsb
;
5557 /* nothing to move if this is the last extent */
5558 if (*current_ext
>= (total_extents
- 1))
5559 goto update_current_ext
;
5561 * If this is not the last extent in the file, make sure there
5562 * is enough room between current extent and next extent for
5563 * accommodating the shift.
5565 adj_irecp
= xfs_iext_get_ext(ifp
, *current_ext
+ 1);
5566 xfs_bmbt_get_all(adj_irecp
, &adj_irec
);
5567 if (startoff
+ got
.br_blockcount
> adj_irec
.br_startoff
)
5570 * Unlike a left shift (which involves a hole punch),
5571 * a right shift does not modify extent neighbors
5572 * in any way. We should never find mergeable extents
5573 * in this scenario. Check anyways and warn if we
5574 * encounter two extents that could be one.
5576 if (xfs_bmse_can_merge(&got
, &adj_irec
, offset_shift_fsb
))
5580 * Increment the extent index for the next iteration, update the start
5581 * offset of the in-core extent and update the btree if applicable.
5584 if (direction
== SHIFT_LEFT
)
5588 xfs_bmbt_set_startoff(gotp
, startoff
);
5589 *logflags
|= XFS_ILOG_CORE
;
5591 *logflags
|= XFS_ILOG_DEXT
;
5595 error
= xfs_bmbt_lookup_eq(cur
, got
.br_startoff
, got
.br_startblock
,
5596 got
.br_blockcount
, &i
);
5599 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
5601 got
.br_startoff
= startoff
;
5602 return xfs_bmbt_update(cur
, got
.br_startoff
, got
.br_startblock
,
5603 got
.br_blockcount
, got
.br_state
);
5607 * Shift extent records to the left/right to cover/create a hole.
5609 * The maximum number of extents to be shifted in a single operation is
5610 * @num_exts. @stop_fsb specifies the file offset at which to stop shift and the
5611 * file offset where we've left off is returned in @next_fsb. @offset_shift_fsb
5612 * is the length by which each extent is shifted. If there is no hole to shift
5613 * the extents into, this will be considered invalid operation and we abort
5617 xfs_bmap_shift_extents(
5618 struct xfs_trans
*tp
,
5619 struct xfs_inode
*ip
,
5620 xfs_fileoff_t
*next_fsb
,
5621 xfs_fileoff_t offset_shift_fsb
,
5623 xfs_fileoff_t stop_fsb
,
5624 xfs_fsblock_t
*firstblock
,
5625 struct xfs_bmap_free
*flist
,
5626 enum shift_direction direction
,
5629 struct xfs_btree_cur
*cur
= NULL
;
5630 struct xfs_bmbt_rec_host
*gotp
;
5631 struct xfs_bmbt_irec got
;
5632 struct xfs_mount
*mp
= ip
->i_mount
;
5633 struct xfs_ifork
*ifp
;
5634 xfs_extnum_t nexts
= 0;
5635 xfs_extnum_t current_ext
;
5636 xfs_extnum_t total_extents
;
5637 xfs_extnum_t stop_extent
;
5639 int whichfork
= XFS_DATA_FORK
;
5642 if (unlikely(XFS_TEST_ERROR(
5643 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
5644 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
5645 mp
, XFS_ERRTAG_BMAPIFORMAT
, XFS_RANDOM_BMAPIFORMAT
))) {
5646 XFS_ERROR_REPORT("xfs_bmap_shift_extents",
5647 XFS_ERRLEVEL_LOW
, mp
);
5648 return -EFSCORRUPTED
;
5651 if (XFS_FORCED_SHUTDOWN(mp
))
5654 ASSERT(xfs_isilocked(ip
, XFS_IOLOCK_EXCL
));
5655 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
5656 ASSERT(direction
== SHIFT_LEFT
|| direction
== SHIFT_RIGHT
);
5657 ASSERT(*next_fsb
!= NULLFSBLOCK
|| direction
== SHIFT_RIGHT
);
5659 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5660 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
5661 /* Read in all the extents */
5662 error
= xfs_iread_extents(tp
, ip
, whichfork
);
5667 if (ifp
->if_flags
& XFS_IFBROOT
) {
5668 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
5669 cur
->bc_private
.b
.firstblock
= *firstblock
;
5670 cur
->bc_private
.b
.flist
= flist
;
5671 cur
->bc_private
.b
.flags
= 0;
5675 * There may be delalloc extents in the data fork before the range we
5676 * are collapsing out, so we cannot use the count of real extents here.
5677 * Instead we have to calculate it from the incore fork.
5679 total_extents
= ifp
->if_bytes
/ sizeof(xfs_bmbt_rec_t
);
5680 if (total_extents
== 0) {
5686 * In case of first right shift, we need to initialize next_fsb
5688 if (*next_fsb
== NULLFSBLOCK
) {
5689 gotp
= xfs_iext_get_ext(ifp
, total_extents
- 1);
5690 xfs_bmbt_get_all(gotp
, &got
);
5691 *next_fsb
= got
.br_startoff
;
5692 if (stop_fsb
> *next_fsb
) {
5698 /* Lookup the extent index at which we have to stop */
5699 if (direction
== SHIFT_RIGHT
) {
5700 gotp
= xfs_iext_bno_to_ext(ifp
, stop_fsb
, &stop_extent
);
5701 /* Make stop_extent exclusive of shift range */
5704 stop_extent
= total_extents
;
5707 * Look up the extent index for the fsb where we start shifting. We can
5708 * henceforth iterate with current_ext as extent list changes are locked
5711 * gotp can be null in 2 cases: 1) if there are no extents or 2)
5712 * *next_fsb lies in a hole beyond which there are no extents. Either
5715 gotp
= xfs_iext_bno_to_ext(ifp
, *next_fsb
, ¤t_ext
);
5721 /* some sanity checking before we finally start shifting extents */
5722 if ((direction
== SHIFT_LEFT
&& current_ext
>= stop_extent
) ||
5723 (direction
== SHIFT_RIGHT
&& current_ext
<= stop_extent
)) {
5728 while (nexts
++ < num_exts
) {
5729 error
= xfs_bmse_shift_one(ip
, whichfork
, offset_shift_fsb
,
5730 ¤t_ext
, gotp
, cur
, &logflags
,
5735 * If there was an extent merge during the shift, the extent
5736 * count can change. Update the total and grade the next record.
5738 if (direction
== SHIFT_LEFT
) {
5739 total_extents
= ifp
->if_bytes
/ sizeof(xfs_bmbt_rec_t
);
5740 stop_extent
= total_extents
;
5743 if (current_ext
== stop_extent
) {
5745 *next_fsb
= NULLFSBLOCK
;
5748 gotp
= xfs_iext_get_ext(ifp
, current_ext
);
5752 xfs_bmbt_get_all(gotp
, &got
);
5753 *next_fsb
= got
.br_startoff
;
5758 xfs_btree_del_cursor(cur
,
5759 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
5762 xfs_trans_log_inode(tp
, ip
, logflags
);
5768 * Splits an extent into two extents at split_fsb block such that it is
5769 * the first block of the current_ext. @current_ext is a target extent
5770 * to be split. @split_fsb is a block where the extents is split.
5771 * If split_fsb lies in a hole or the first block of extents, just return 0.
5774 xfs_bmap_split_extent_at(
5775 struct xfs_trans
*tp
,
5776 struct xfs_inode
*ip
,
5777 xfs_fileoff_t split_fsb
,
5778 xfs_fsblock_t
*firstfsb
,
5779 struct xfs_bmap_free
*free_list
)
5781 int whichfork
= XFS_DATA_FORK
;
5782 struct xfs_btree_cur
*cur
= NULL
;
5783 struct xfs_bmbt_rec_host
*gotp
;
5784 struct xfs_bmbt_irec got
;
5785 struct xfs_bmbt_irec
new; /* split extent */
5786 struct xfs_mount
*mp
= ip
->i_mount
;
5787 struct xfs_ifork
*ifp
;
5788 xfs_fsblock_t gotblkcnt
; /* new block count for got */
5789 xfs_extnum_t current_ext
;
5794 if (unlikely(XFS_TEST_ERROR(
5795 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
5796 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
5797 mp
, XFS_ERRTAG_BMAPIFORMAT
, XFS_RANDOM_BMAPIFORMAT
))) {
5798 XFS_ERROR_REPORT("xfs_bmap_split_extent_at",
5799 XFS_ERRLEVEL_LOW
, mp
);
5800 return -EFSCORRUPTED
;
5803 if (XFS_FORCED_SHUTDOWN(mp
))
5806 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5807 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
5808 /* Read in all the extents */
5809 error
= xfs_iread_extents(tp
, ip
, whichfork
);
5815 * gotp can be null in 2 cases: 1) if there are no extents
5816 * or 2) split_fsb lies in a hole beyond which there are
5817 * no extents. Either way, we are done.
5819 gotp
= xfs_iext_bno_to_ext(ifp
, split_fsb
, ¤t_ext
);
5823 xfs_bmbt_get_all(gotp
, &got
);
5826 * Check split_fsb lies in a hole or the start boundary offset
5829 if (got
.br_startoff
>= split_fsb
)
5832 gotblkcnt
= split_fsb
- got
.br_startoff
;
5833 new.br_startoff
= split_fsb
;
5834 new.br_startblock
= got
.br_startblock
+ gotblkcnt
;
5835 new.br_blockcount
= got
.br_blockcount
- gotblkcnt
;
5836 new.br_state
= got
.br_state
;
5838 if (ifp
->if_flags
& XFS_IFBROOT
) {
5839 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
5840 cur
->bc_private
.b
.firstblock
= *firstfsb
;
5841 cur
->bc_private
.b
.flist
= free_list
;
5842 cur
->bc_private
.b
.flags
= 0;
5843 error
= xfs_bmbt_lookup_eq(cur
, got
.br_startoff
,
5849 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, del_cursor
);
5852 xfs_bmbt_set_blockcount(gotp
, gotblkcnt
);
5853 got
.br_blockcount
= gotblkcnt
;
5855 logflags
= XFS_ILOG_CORE
;
5857 error
= xfs_bmbt_update(cur
, got
.br_startoff
,
5864 logflags
|= XFS_ILOG_DEXT
;
5866 /* Add new extent */
5868 xfs_iext_insert(ip
, current_ext
, 1, &new, 0);
5869 XFS_IFORK_NEXT_SET(ip
, whichfork
,
5870 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
5873 error
= xfs_bmbt_lookup_eq(cur
, new.br_startoff
,
5874 new.br_startblock
, new.br_blockcount
,
5878 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, del_cursor
);
5879 cur
->bc_rec
.b
.br_state
= new.br_state
;
5881 error
= xfs_btree_insert(cur
, &i
);
5884 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, del_cursor
);
5888 * Convert to a btree if necessary.
5890 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
5891 int tmp_logflags
; /* partial log flag return val */
5893 ASSERT(cur
== NULL
);
5894 error
= xfs_bmap_extents_to_btree(tp
, ip
, firstfsb
, free_list
,
5895 &cur
, 0, &tmp_logflags
, whichfork
);
5896 logflags
|= tmp_logflags
;
5901 cur
->bc_private
.b
.allocated
= 0;
5902 xfs_btree_del_cursor(cur
,
5903 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
5907 xfs_trans_log_inode(tp
, ip
, logflags
);
5912 xfs_bmap_split_extent(
5913 struct xfs_inode
*ip
,
5914 xfs_fileoff_t split_fsb
)
5916 struct xfs_mount
*mp
= ip
->i_mount
;
5917 struct xfs_trans
*tp
;
5918 struct xfs_bmap_free free_list
;
5919 xfs_fsblock_t firstfsb
;
5923 tp
= xfs_trans_alloc(mp
, XFS_TRANS_DIOSTRAT
);
5924 error
= xfs_trans_reserve(tp
, &M_RES(mp
)->tr_write
,
5925 XFS_DIOSTRAT_SPACE_RES(mp
, 0), 0);
5927 xfs_trans_cancel(tp
);
5931 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
5932 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
);
5934 xfs_bmap_init(&free_list
, &firstfsb
);
5936 error
= xfs_bmap_split_extent_at(tp
, ip
, split_fsb
,
5937 &firstfsb
, &free_list
);
5941 error
= xfs_bmap_finish(&tp
, &free_list
, &committed
);
5945 return xfs_trans_commit(tp
);
5948 xfs_trans_cancel(tp
);