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 ASSERT(ifp
->if_broot
== NULL
);
797 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_EXTENTS
);
798 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
802 * Allocation can't fail, the space was reserved.
804 ASSERT(args
.fsbno
!= NULLFSBLOCK
);
805 ASSERT(*firstblock
== NULLFSBLOCK
||
806 args
.agno
== XFS_FSB_TO_AGNO(mp
, *firstblock
) ||
808 args
.agno
> XFS_FSB_TO_AGNO(mp
, *firstblock
)));
809 *firstblock
= cur
->bc_private
.b
.firstblock
= args
.fsbno
;
810 cur
->bc_private
.b
.allocated
++;
811 ip
->i_d
.di_nblocks
++;
812 xfs_trans_mod_dquot_byino(tp
, ip
, XFS_TRANS_DQ_BCOUNT
, 1L);
813 abp
= xfs_btree_get_bufl(mp
, tp
, args
.fsbno
, 0);
815 * Fill in the child block.
817 abp
->b_ops
= &xfs_bmbt_buf_ops
;
818 ablock
= XFS_BUF_TO_BLOCK(abp
);
819 if (xfs_sb_version_hascrc(&mp
->m_sb
))
820 xfs_btree_init_block_int(mp
, ablock
, abp
->b_bn
,
821 XFS_BMAP_CRC_MAGIC
, 0, 0, ip
->i_ino
,
822 XFS_BTREE_LONG_PTRS
| XFS_BTREE_CRC_BLOCKS
);
824 xfs_btree_init_block_int(mp
, ablock
, abp
->b_bn
,
825 XFS_BMAP_MAGIC
, 0, 0, ip
->i_ino
,
826 XFS_BTREE_LONG_PTRS
);
828 arp
= XFS_BMBT_REC_ADDR(mp
, ablock
, 1);
829 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
830 for (cnt
= i
= 0; i
< nextents
; i
++) {
831 ep
= xfs_iext_get_ext(ifp
, i
);
832 if (!isnullstartblock(xfs_bmbt_get_startblock(ep
))) {
833 arp
->l0
= cpu_to_be64(ep
->l0
);
834 arp
->l1
= cpu_to_be64(ep
->l1
);
838 ASSERT(cnt
== XFS_IFORK_NEXTENTS(ip
, whichfork
));
839 xfs_btree_set_numrecs(ablock
, cnt
);
842 * Fill in the root key and pointer.
844 kp
= XFS_BMBT_KEY_ADDR(mp
, block
, 1);
845 arp
= XFS_BMBT_REC_ADDR(mp
, ablock
, 1);
846 kp
->br_startoff
= cpu_to_be64(xfs_bmbt_disk_get_startoff(arp
));
847 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, xfs_bmbt_get_maxrecs(cur
,
848 be16_to_cpu(block
->bb_level
)));
849 *pp
= cpu_to_be64(args
.fsbno
);
852 * Do all this logging at the end so that
853 * the root is at the right level.
855 xfs_btree_log_block(cur
, abp
, XFS_BB_ALL_BITS
);
856 xfs_btree_log_recs(cur
, abp
, 1, be16_to_cpu(ablock
->bb_numrecs
));
857 ASSERT(*curp
== NULL
);
859 *logflagsp
= XFS_ILOG_CORE
| xfs_ilog_fbroot(whichfork
);
864 * Convert a local file to an extents file.
865 * This code is out of bounds for data forks of regular files,
866 * since the file data needs to get logged so things will stay consistent.
867 * (The bmap-level manipulations are ok, though).
870 xfs_bmap_local_to_extents_empty(
871 struct xfs_inode
*ip
,
874 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
876 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
877 ASSERT(ifp
->if_bytes
== 0);
878 ASSERT(XFS_IFORK_NEXTENTS(ip
, whichfork
) == 0);
880 xfs_bmap_forkoff_reset(ip
, whichfork
);
881 ifp
->if_flags
&= ~XFS_IFINLINE
;
882 ifp
->if_flags
|= XFS_IFEXTENTS
;
883 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_EXTENTS
);
887 STATIC
int /* error */
888 xfs_bmap_local_to_extents(
889 xfs_trans_t
*tp
, /* transaction pointer */
890 xfs_inode_t
*ip
, /* incore inode pointer */
891 xfs_fsblock_t
*firstblock
, /* first block allocated in xaction */
892 xfs_extlen_t total
, /* total blocks needed by transaction */
893 int *logflagsp
, /* inode logging flags */
895 void (*init_fn
)(struct xfs_trans
*tp
,
897 struct xfs_inode
*ip
,
898 struct xfs_ifork
*ifp
))
901 int flags
; /* logging flags returned */
902 xfs_ifork_t
*ifp
; /* inode fork pointer */
903 xfs_alloc_arg_t args
; /* allocation arguments */
904 xfs_buf_t
*bp
; /* buffer for extent block */
905 xfs_bmbt_rec_host_t
*ep
; /* extent record pointer */
908 * We don't want to deal with the case of keeping inode data inline yet.
909 * So sending the data fork of a regular inode is invalid.
911 ASSERT(!(S_ISREG(ip
->i_d
.di_mode
) && whichfork
== XFS_DATA_FORK
));
912 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
913 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
915 if (!ifp
->if_bytes
) {
916 xfs_bmap_local_to_extents_empty(ip
, whichfork
);
917 flags
= XFS_ILOG_CORE
;
923 ASSERT((ifp
->if_flags
& (XFS_IFINLINE
|XFS_IFEXTENTS
|XFS_IFEXTIREC
)) ==
925 memset(&args
, 0, sizeof(args
));
927 args
.mp
= ip
->i_mount
;
928 args
.firstblock
= *firstblock
;
930 * Allocate a block. We know we need only one, since the
931 * file currently fits in an inode.
933 if (*firstblock
== NULLFSBLOCK
) {
934 args
.fsbno
= XFS_INO_TO_FSB(args
.mp
, ip
->i_ino
);
935 args
.type
= XFS_ALLOCTYPE_START_BNO
;
937 args
.fsbno
= *firstblock
;
938 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
941 args
.minlen
= args
.maxlen
= args
.prod
= 1;
942 error
= xfs_alloc_vextent(&args
);
946 /* Can't fail, the space was reserved. */
947 ASSERT(args
.fsbno
!= NULLFSBLOCK
);
948 ASSERT(args
.len
== 1);
949 *firstblock
= args
.fsbno
;
950 bp
= xfs_btree_get_bufl(args
.mp
, tp
, args
.fsbno
, 0);
953 * Initialize the block, copy the data and log the remote buffer.
955 * The callout is responsible for logging because the remote format
956 * might differ from the local format and thus we don't know how much to
957 * log here. Note that init_fn must also set the buffer log item type
960 init_fn(tp
, bp
, ip
, ifp
);
962 /* account for the change in fork size */
963 xfs_idata_realloc(ip
, -ifp
->if_bytes
, whichfork
);
964 xfs_bmap_local_to_extents_empty(ip
, whichfork
);
965 flags
|= XFS_ILOG_CORE
;
967 xfs_iext_add(ifp
, 0, 1);
968 ep
= xfs_iext_get_ext(ifp
, 0);
969 xfs_bmbt_set_allf(ep
, 0, args
.fsbno
, 1, XFS_EXT_NORM
);
970 trace_xfs_bmap_post_update(ip
, 0,
971 whichfork
== XFS_ATTR_FORK
? BMAP_ATTRFORK
: 0,
973 XFS_IFORK_NEXT_SET(ip
, whichfork
, 1);
974 ip
->i_d
.di_nblocks
= 1;
975 xfs_trans_mod_dquot_byino(tp
, ip
,
976 XFS_TRANS_DQ_BCOUNT
, 1L);
977 flags
|= xfs_ilog_fext(whichfork
);
985 * Called from xfs_bmap_add_attrfork to handle btree format files.
987 STATIC
int /* error */
988 xfs_bmap_add_attrfork_btree(
989 xfs_trans_t
*tp
, /* transaction pointer */
990 xfs_inode_t
*ip
, /* incore inode pointer */
991 xfs_fsblock_t
*firstblock
, /* first block allocated */
992 xfs_bmap_free_t
*flist
, /* blocks to free at commit */
993 int *flags
) /* inode logging flags */
995 xfs_btree_cur_t
*cur
; /* btree cursor */
996 int error
; /* error return value */
997 xfs_mount_t
*mp
; /* file system mount struct */
998 int stat
; /* newroot status */
1001 if (ip
->i_df
.if_broot_bytes
<= XFS_IFORK_DSIZE(ip
))
1002 *flags
|= XFS_ILOG_DBROOT
;
1004 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, XFS_DATA_FORK
);
1005 cur
->bc_private
.b
.flist
= flist
;
1006 cur
->bc_private
.b
.firstblock
= *firstblock
;
1007 if ((error
= xfs_bmbt_lookup_ge(cur
, 0, 0, 0, &stat
)))
1009 /* must be at least one entry */
1010 XFS_WANT_CORRUPTED_GOTO(mp
, stat
== 1, error0
);
1011 if ((error
= xfs_btree_new_iroot(cur
, flags
, &stat
)))
1014 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
1017 *firstblock
= cur
->bc_private
.b
.firstblock
;
1018 cur
->bc_private
.b
.allocated
= 0;
1019 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
1023 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
1028 * Called from xfs_bmap_add_attrfork to handle extents format files.
1030 STATIC
int /* error */
1031 xfs_bmap_add_attrfork_extents(
1032 xfs_trans_t
*tp
, /* transaction pointer */
1033 xfs_inode_t
*ip
, /* incore inode pointer */
1034 xfs_fsblock_t
*firstblock
, /* first block allocated */
1035 xfs_bmap_free_t
*flist
, /* blocks to free at commit */
1036 int *flags
) /* inode logging flags */
1038 xfs_btree_cur_t
*cur
; /* bmap btree cursor */
1039 int error
; /* error return value */
1041 if (ip
->i_d
.di_nextents
* sizeof(xfs_bmbt_rec_t
) <= XFS_IFORK_DSIZE(ip
))
1044 error
= xfs_bmap_extents_to_btree(tp
, ip
, firstblock
, flist
, &cur
, 0,
1045 flags
, XFS_DATA_FORK
);
1047 cur
->bc_private
.b
.allocated
= 0;
1048 xfs_btree_del_cursor(cur
,
1049 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
1055 * Called from xfs_bmap_add_attrfork to handle local format files. Each
1056 * different data fork content type needs a different callout to do the
1057 * conversion. Some are basic and only require special block initialisation
1058 * callouts for the data formating, others (directories) are so specialised they
1059 * handle everything themselves.
1061 * XXX (dgc): investigate whether directory conversion can use the generic
1062 * formatting callout. It should be possible - it's just a very complex
1065 STATIC
int /* error */
1066 xfs_bmap_add_attrfork_local(
1067 xfs_trans_t
*tp
, /* transaction pointer */
1068 xfs_inode_t
*ip
, /* incore inode pointer */
1069 xfs_fsblock_t
*firstblock
, /* first block allocated */
1070 xfs_bmap_free_t
*flist
, /* blocks to free at commit */
1071 int *flags
) /* inode logging flags */
1073 xfs_da_args_t dargs
; /* args for dir/attr code */
1075 if (ip
->i_df
.if_bytes
<= XFS_IFORK_DSIZE(ip
))
1078 if (S_ISDIR(ip
->i_d
.di_mode
)) {
1079 memset(&dargs
, 0, sizeof(dargs
));
1080 dargs
.geo
= ip
->i_mount
->m_dir_geo
;
1082 dargs
.firstblock
= firstblock
;
1083 dargs
.flist
= flist
;
1084 dargs
.total
= dargs
.geo
->fsbcount
;
1085 dargs
.whichfork
= XFS_DATA_FORK
;
1087 return xfs_dir2_sf_to_block(&dargs
);
1090 if (S_ISLNK(ip
->i_d
.di_mode
))
1091 return xfs_bmap_local_to_extents(tp
, ip
, firstblock
, 1,
1092 flags
, XFS_DATA_FORK
,
1093 xfs_symlink_local_to_remote
);
1095 /* should only be called for types that support local format data */
1097 return -EFSCORRUPTED
;
1101 * Convert inode from non-attributed to attributed.
1102 * Must not be in a transaction, ip must not be locked.
1104 int /* error code */
1105 xfs_bmap_add_attrfork(
1106 xfs_inode_t
*ip
, /* incore inode pointer */
1107 int size
, /* space new attribute needs */
1108 int rsvd
) /* xact may use reserved blks */
1110 xfs_fsblock_t firstblock
; /* 1st block/ag allocated */
1111 xfs_bmap_free_t flist
; /* freed extent records */
1112 xfs_mount_t
*mp
; /* mount structure */
1113 xfs_trans_t
*tp
; /* transaction pointer */
1114 int blks
; /* space reservation */
1115 int version
= 1; /* superblock attr version */
1116 int committed
; /* xaction was committed */
1117 int logflags
; /* logging flags */
1118 int error
; /* error return value */
1120 ASSERT(XFS_IFORK_Q(ip
) == 0);
1123 ASSERT(!XFS_NOT_DQATTACHED(mp
, ip
));
1124 tp
= xfs_trans_alloc(mp
, XFS_TRANS_ADDAFORK
);
1125 blks
= XFS_ADDAFORK_SPACE_RES(mp
);
1127 tp
->t_flags
|= XFS_TRANS_RESERVE
;
1128 error
= xfs_trans_reserve(tp
, &M_RES(mp
)->tr_addafork
, blks
, 0);
1130 xfs_trans_cancel(tp
);
1133 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
1134 error
= xfs_trans_reserve_quota_nblks(tp
, ip
, blks
, 0, rsvd
?
1135 XFS_QMOPT_RES_REGBLKS
| XFS_QMOPT_FORCE_RES
:
1136 XFS_QMOPT_RES_REGBLKS
);
1139 if (XFS_IFORK_Q(ip
))
1141 if (ip
->i_d
.di_aformat
!= XFS_DINODE_FMT_EXTENTS
) {
1143 * For inodes coming from pre-6.2 filesystems.
1145 ASSERT(ip
->i_d
.di_aformat
== 0);
1146 ip
->i_d
.di_aformat
= XFS_DINODE_FMT_EXTENTS
;
1148 ASSERT(ip
->i_d
.di_anextents
== 0);
1150 xfs_trans_ijoin(tp
, ip
, 0);
1151 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
1153 switch (ip
->i_d
.di_format
) {
1154 case XFS_DINODE_FMT_DEV
:
1155 ip
->i_d
.di_forkoff
= roundup(sizeof(xfs_dev_t
), 8) >> 3;
1157 case XFS_DINODE_FMT_UUID
:
1158 ip
->i_d
.di_forkoff
= roundup(sizeof(uuid_t
), 8) >> 3;
1160 case XFS_DINODE_FMT_LOCAL
:
1161 case XFS_DINODE_FMT_EXTENTS
:
1162 case XFS_DINODE_FMT_BTREE
:
1163 ip
->i_d
.di_forkoff
= xfs_attr_shortform_bytesfit(ip
, size
);
1164 if (!ip
->i_d
.di_forkoff
)
1165 ip
->i_d
.di_forkoff
= xfs_default_attroffset(ip
) >> 3;
1166 else if (mp
->m_flags
& XFS_MOUNT_ATTR2
)
1175 ASSERT(ip
->i_afp
== NULL
);
1176 ip
->i_afp
= kmem_zone_zalloc(xfs_ifork_zone
, KM_SLEEP
);
1177 ip
->i_afp
->if_flags
= XFS_IFEXTENTS
;
1179 xfs_bmap_init(&flist
, &firstblock
);
1180 switch (ip
->i_d
.di_format
) {
1181 case XFS_DINODE_FMT_LOCAL
:
1182 error
= xfs_bmap_add_attrfork_local(tp
, ip
, &firstblock
, &flist
,
1185 case XFS_DINODE_FMT_EXTENTS
:
1186 error
= xfs_bmap_add_attrfork_extents(tp
, ip
, &firstblock
,
1189 case XFS_DINODE_FMT_BTREE
:
1190 error
= xfs_bmap_add_attrfork_btree(tp
, ip
, &firstblock
, &flist
,
1198 xfs_trans_log_inode(tp
, ip
, logflags
);
1201 if (!xfs_sb_version_hasattr(&mp
->m_sb
) ||
1202 (!xfs_sb_version_hasattr2(&mp
->m_sb
) && version
== 2)) {
1203 bool log_sb
= false;
1205 spin_lock(&mp
->m_sb_lock
);
1206 if (!xfs_sb_version_hasattr(&mp
->m_sb
)) {
1207 xfs_sb_version_addattr(&mp
->m_sb
);
1210 if (!xfs_sb_version_hasattr2(&mp
->m_sb
) && version
== 2) {
1211 xfs_sb_version_addattr2(&mp
->m_sb
);
1214 spin_unlock(&mp
->m_sb_lock
);
1219 error
= xfs_bmap_finish(&tp
, &flist
, &committed
);
1222 error
= xfs_trans_commit(tp
);
1223 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
1227 xfs_bmap_cancel(&flist
);
1229 xfs_trans_cancel(tp
);
1230 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
1235 * Internal and external extent tree search functions.
1239 * Read in the extents to if_extents.
1240 * All inode fields are set up by caller, we just traverse the btree
1241 * and copy the records in. If the file system cannot contain unwritten
1242 * extents, the records are checked for no "state" flags.
1245 xfs_bmap_read_extents(
1246 xfs_trans_t
*tp
, /* transaction pointer */
1247 xfs_inode_t
*ip
, /* incore inode */
1248 int whichfork
) /* data or attr fork */
1250 struct xfs_btree_block
*block
; /* current btree block */
1251 xfs_fsblock_t bno
; /* block # of "block" */
1252 xfs_buf_t
*bp
; /* buffer for "block" */
1253 int error
; /* error return value */
1254 xfs_exntfmt_t exntf
; /* XFS_EXTFMT_NOSTATE, if checking */
1255 xfs_extnum_t i
, j
; /* index into the extents list */
1256 xfs_ifork_t
*ifp
; /* fork structure */
1257 int level
; /* btree level, for checking */
1258 xfs_mount_t
*mp
; /* file system mount structure */
1259 __be64
*pp
; /* pointer to block address */
1261 xfs_extnum_t room
; /* number of entries there's room for */
1265 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1266 exntf
= (whichfork
!= XFS_DATA_FORK
) ? XFS_EXTFMT_NOSTATE
:
1267 XFS_EXTFMT_INODE(ip
);
1268 block
= ifp
->if_broot
;
1270 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
1272 level
= be16_to_cpu(block
->bb_level
);
1274 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, 1, ifp
->if_broot_bytes
);
1275 bno
= be64_to_cpu(*pp
);
1276 ASSERT(bno
!= NULLFSBLOCK
);
1277 ASSERT(XFS_FSB_TO_AGNO(mp
, bno
) < mp
->m_sb
.sb_agcount
);
1278 ASSERT(XFS_FSB_TO_AGBNO(mp
, bno
) < mp
->m_sb
.sb_agblocks
);
1280 * Go down the tree until leaf level is reached, following the first
1281 * pointer (leftmost) at each level.
1283 while (level
-- > 0) {
1284 error
= xfs_btree_read_bufl(mp
, tp
, bno
, 0, &bp
,
1285 XFS_BMAP_BTREE_REF
, &xfs_bmbt_buf_ops
);
1288 block
= XFS_BUF_TO_BLOCK(bp
);
1291 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, mp
->m_bmap_dmxr
[1]);
1292 bno
= be64_to_cpu(*pp
);
1293 XFS_WANT_CORRUPTED_GOTO(mp
,
1294 XFS_FSB_SANITY_CHECK(mp
, bno
), error0
);
1295 xfs_trans_brelse(tp
, bp
);
1298 * Here with bp and block set to the leftmost leaf node in the tree.
1300 room
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
1303 * Loop over all leaf nodes. Copy information to the extent records.
1306 xfs_bmbt_rec_t
*frp
;
1307 xfs_fsblock_t nextbno
;
1308 xfs_extnum_t num_recs
;
1311 num_recs
= xfs_btree_get_numrecs(block
);
1312 if (unlikely(i
+ num_recs
> room
)) {
1313 ASSERT(i
+ num_recs
<= room
);
1314 xfs_warn(ip
->i_mount
,
1315 "corrupt dinode %Lu, (btree extents).",
1316 (unsigned long long) ip
->i_ino
);
1317 XFS_CORRUPTION_ERROR("xfs_bmap_read_extents(1)",
1318 XFS_ERRLEVEL_LOW
, ip
->i_mount
, block
);
1322 * Read-ahead the next leaf block, if any.
1324 nextbno
= be64_to_cpu(block
->bb_u
.l
.bb_rightsib
);
1325 if (nextbno
!= NULLFSBLOCK
)
1326 xfs_btree_reada_bufl(mp
, nextbno
, 1,
1329 * Copy records into the extent records.
1331 frp
= XFS_BMBT_REC_ADDR(mp
, block
, 1);
1333 for (j
= 0; j
< num_recs
; j
++, i
++, frp
++) {
1334 xfs_bmbt_rec_host_t
*trp
= xfs_iext_get_ext(ifp
, i
);
1335 trp
->l0
= be64_to_cpu(frp
->l0
);
1336 trp
->l1
= be64_to_cpu(frp
->l1
);
1338 if (exntf
== XFS_EXTFMT_NOSTATE
) {
1340 * Check all attribute bmap btree records and
1341 * any "older" data bmap btree records for a
1342 * set bit in the "extent flag" position.
1344 if (unlikely(xfs_check_nostate_extents(ifp
,
1345 start
, num_recs
))) {
1346 XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
1352 xfs_trans_brelse(tp
, bp
);
1355 * If we've reached the end, stop.
1357 if (bno
== NULLFSBLOCK
)
1359 error
= xfs_btree_read_bufl(mp
, tp
, bno
, 0, &bp
,
1360 XFS_BMAP_BTREE_REF
, &xfs_bmbt_buf_ops
);
1363 block
= XFS_BUF_TO_BLOCK(bp
);
1365 ASSERT(i
== (ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
)));
1366 ASSERT(i
== XFS_IFORK_NEXTENTS(ip
, whichfork
));
1367 XFS_BMAP_TRACE_EXLIST(ip
, i
, whichfork
);
1370 xfs_trans_brelse(tp
, bp
);
1371 return -EFSCORRUPTED
;
1376 * Search the extent records for the entry containing block bno.
1377 * If bno lies in a hole, point to the next entry. If bno lies
1378 * past eof, *eofp will be set, and *prevp will contain the last
1379 * entry (null if none). Else, *lastxp will be set to the index
1380 * of the found entry; *gotp will contain the entry.
1382 STATIC xfs_bmbt_rec_host_t
* /* pointer to found extent entry */
1383 xfs_bmap_search_multi_extents(
1384 xfs_ifork_t
*ifp
, /* inode fork pointer */
1385 xfs_fileoff_t bno
, /* block number searched for */
1386 int *eofp
, /* out: end of file found */
1387 xfs_extnum_t
*lastxp
, /* out: last extent index */
1388 xfs_bmbt_irec_t
*gotp
, /* out: extent entry found */
1389 xfs_bmbt_irec_t
*prevp
) /* out: previous extent entry found */
1391 xfs_bmbt_rec_host_t
*ep
; /* extent record pointer */
1392 xfs_extnum_t lastx
; /* last extent index */
1395 * Initialize the extent entry structure to catch access to
1396 * uninitialized br_startblock field.
1398 gotp
->br_startoff
= 0xffa5a5a5a5a5a5a5LL
;
1399 gotp
->br_blockcount
= 0xa55a5a5a5a5a5a5aLL
;
1400 gotp
->br_state
= XFS_EXT_INVALID
;
1401 gotp
->br_startblock
= 0xffffa5a5a5a5a5a5LL
;
1402 prevp
->br_startoff
= NULLFILEOFF
;
1404 ep
= xfs_iext_bno_to_ext(ifp
, bno
, &lastx
);
1406 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, lastx
- 1), prevp
);
1408 if (lastx
< (ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
))) {
1409 xfs_bmbt_get_all(ep
, gotp
);
1423 * Search the extents list for the inode, for the extent containing bno.
1424 * If bno lies in a hole, point to the next entry. If bno lies past eof,
1425 * *eofp will be set, and *prevp will contain the last entry (null if none).
1426 * Else, *lastxp will be set to the index of the found
1427 * entry; *gotp will contain the entry.
1429 STATIC xfs_bmbt_rec_host_t
* /* pointer to found extent entry */
1430 xfs_bmap_search_extents(
1431 xfs_inode_t
*ip
, /* incore inode pointer */
1432 xfs_fileoff_t bno
, /* block number searched for */
1433 int fork
, /* data or attr fork */
1434 int *eofp
, /* out: end of file found */
1435 xfs_extnum_t
*lastxp
, /* out: last extent index */
1436 xfs_bmbt_irec_t
*gotp
, /* out: extent entry found */
1437 xfs_bmbt_irec_t
*prevp
) /* out: previous extent entry found */
1439 xfs_ifork_t
*ifp
; /* inode fork pointer */
1440 xfs_bmbt_rec_host_t
*ep
; /* extent record pointer */
1442 XFS_STATS_INC(ip
->i_mount
, xs_look_exlist
);
1443 ifp
= XFS_IFORK_PTR(ip
, fork
);
1445 ep
= xfs_bmap_search_multi_extents(ifp
, bno
, eofp
, lastxp
, gotp
, prevp
);
1447 if (unlikely(!(gotp
->br_startblock
) && (*lastxp
!= NULLEXTNUM
) &&
1448 !(XFS_IS_REALTIME_INODE(ip
) && fork
== XFS_DATA_FORK
))) {
1449 xfs_alert_tag(ip
->i_mount
, XFS_PTAG_FSBLOCK_ZERO
,
1450 "Access to block zero in inode %llu "
1451 "start_block: %llx start_off: %llx "
1452 "blkcnt: %llx extent-state: %x lastx: %x",
1453 (unsigned long long)ip
->i_ino
,
1454 (unsigned long long)gotp
->br_startblock
,
1455 (unsigned long long)gotp
->br_startoff
,
1456 (unsigned long long)gotp
->br_blockcount
,
1457 gotp
->br_state
, *lastxp
);
1458 *lastxp
= NULLEXTNUM
;
1466 * Returns the file-relative block number of the first unused block(s)
1467 * in the file with at least "len" logically contiguous blocks free.
1468 * This is the lowest-address hole if the file has holes, else the first block
1469 * past the end of file.
1470 * Return 0 if the file is currently local (in-inode).
1473 xfs_bmap_first_unused(
1474 xfs_trans_t
*tp
, /* transaction pointer */
1475 xfs_inode_t
*ip
, /* incore inode */
1476 xfs_extlen_t len
, /* size of hole to find */
1477 xfs_fileoff_t
*first_unused
, /* unused block */
1478 int whichfork
) /* data or attr fork */
1480 int error
; /* error return value */
1481 int idx
; /* extent record index */
1482 xfs_ifork_t
*ifp
; /* inode fork pointer */
1483 xfs_fileoff_t lastaddr
; /* last block number seen */
1484 xfs_fileoff_t lowest
; /* lowest useful block */
1485 xfs_fileoff_t max
; /* starting useful block */
1486 xfs_fileoff_t off
; /* offset for this block */
1487 xfs_extnum_t nextents
; /* number of extent entries */
1489 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
||
1490 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
||
1491 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
1492 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
) {
1496 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1497 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
1498 (error
= xfs_iread_extents(tp
, ip
, whichfork
)))
1500 lowest
= *first_unused
;
1501 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
1502 for (idx
= 0, lastaddr
= 0, max
= lowest
; idx
< nextents
; idx
++) {
1503 xfs_bmbt_rec_host_t
*ep
= xfs_iext_get_ext(ifp
, idx
);
1504 off
= xfs_bmbt_get_startoff(ep
);
1506 * See if the hole before this extent will work.
1508 if (off
>= lowest
+ len
&& off
- max
>= len
) {
1509 *first_unused
= max
;
1512 lastaddr
= off
+ xfs_bmbt_get_blockcount(ep
);
1513 max
= XFS_FILEOFF_MAX(lastaddr
, lowest
);
1515 *first_unused
= max
;
1520 * Returns the file-relative block number of the last block - 1 before
1521 * last_block (input value) in the file.
1522 * This is not based on i_size, it is based on the extent records.
1523 * Returns 0 for local files, as they do not have extent records.
1526 xfs_bmap_last_before(
1527 xfs_trans_t
*tp
, /* transaction pointer */
1528 xfs_inode_t
*ip
, /* incore inode */
1529 xfs_fileoff_t
*last_block
, /* last block */
1530 int whichfork
) /* data or attr fork */
1532 xfs_fileoff_t bno
; /* input file offset */
1533 int eof
; /* hit end of file */
1534 xfs_bmbt_rec_host_t
*ep
; /* pointer to last extent */
1535 int error
; /* error return value */
1536 xfs_bmbt_irec_t got
; /* current extent value */
1537 xfs_ifork_t
*ifp
; /* inode fork pointer */
1538 xfs_extnum_t lastx
; /* last extent used */
1539 xfs_bmbt_irec_t prev
; /* previous extent value */
1541 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
&&
1542 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
1543 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_LOCAL
)
1545 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
) {
1549 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1550 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
1551 (error
= xfs_iread_extents(tp
, ip
, whichfork
)))
1553 bno
= *last_block
- 1;
1554 ep
= xfs_bmap_search_extents(ip
, bno
, whichfork
, &eof
, &lastx
, &got
,
1556 if (eof
|| xfs_bmbt_get_startoff(ep
) > bno
) {
1557 if (prev
.br_startoff
== NULLFILEOFF
)
1560 *last_block
= prev
.br_startoff
+ prev
.br_blockcount
;
1563 * Otherwise *last_block is already the right answer.
1569 xfs_bmap_last_extent(
1570 struct xfs_trans
*tp
,
1571 struct xfs_inode
*ip
,
1573 struct xfs_bmbt_irec
*rec
,
1576 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1580 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
1581 error
= xfs_iread_extents(tp
, ip
, whichfork
);
1586 nextents
= ifp
->if_bytes
/ sizeof(xfs_bmbt_rec_t
);
1587 if (nextents
== 0) {
1592 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, nextents
- 1), rec
);
1598 * Check the last inode extent to determine whether this allocation will result
1599 * in blocks being allocated at the end of the file. When we allocate new data
1600 * blocks at the end of the file which do not start at the previous data block,
1601 * we will try to align the new blocks at stripe unit boundaries.
1603 * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be
1604 * at, or past the EOF.
1608 struct xfs_bmalloca
*bma
,
1611 struct xfs_bmbt_irec rec
;
1616 error
= xfs_bmap_last_extent(NULL
, bma
->ip
, whichfork
, &rec
,
1627 * Check if we are allocation or past the last extent, or at least into
1628 * the last delayed allocated extent.
1630 bma
->aeof
= bma
->offset
>= rec
.br_startoff
+ rec
.br_blockcount
||
1631 (bma
->offset
>= rec
.br_startoff
&&
1632 isnullstartblock(rec
.br_startblock
));
1637 * Returns the file-relative block number of the first block past eof in
1638 * the file. This is not based on i_size, it is based on the extent records.
1639 * Returns 0 for local files, as they do not have extent records.
1642 xfs_bmap_last_offset(
1643 struct xfs_inode
*ip
,
1644 xfs_fileoff_t
*last_block
,
1647 struct xfs_bmbt_irec rec
;
1653 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
)
1656 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
&&
1657 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
1660 error
= xfs_bmap_last_extent(NULL
, ip
, whichfork
, &rec
, &is_empty
);
1661 if (error
|| is_empty
)
1664 *last_block
= rec
.br_startoff
+ rec
.br_blockcount
;
1669 * Returns whether the selected fork of the inode has exactly one
1670 * block or not. For the data fork we check this matches di_size,
1671 * implying the file's range is 0..bsize-1.
1673 int /* 1=>1 block, 0=>otherwise */
1675 xfs_inode_t
*ip
, /* incore inode */
1676 int whichfork
) /* data or attr fork */
1678 xfs_bmbt_rec_host_t
*ep
; /* ptr to fork's extent */
1679 xfs_ifork_t
*ifp
; /* inode fork pointer */
1680 int rval
; /* return value */
1681 xfs_bmbt_irec_t s
; /* internal version of extent */
1684 if (whichfork
== XFS_DATA_FORK
)
1685 return XFS_ISIZE(ip
) == ip
->i_mount
->m_sb
.sb_blocksize
;
1687 if (XFS_IFORK_NEXTENTS(ip
, whichfork
) != 1)
1689 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
1691 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1692 ASSERT(ifp
->if_flags
& XFS_IFEXTENTS
);
1693 ep
= xfs_iext_get_ext(ifp
, 0);
1694 xfs_bmbt_get_all(ep
, &s
);
1695 rval
= s
.br_startoff
== 0 && s
.br_blockcount
== 1;
1696 if (rval
&& whichfork
== XFS_DATA_FORK
)
1697 ASSERT(XFS_ISIZE(ip
) == ip
->i_mount
->m_sb
.sb_blocksize
);
1702 * Extent tree manipulation functions used during allocation.
1706 * Convert a delayed allocation to a real allocation.
1708 STATIC
int /* error */
1709 xfs_bmap_add_extent_delay_real(
1710 struct xfs_bmalloca
*bma
)
1712 struct xfs_bmbt_irec
*new = &bma
->got
;
1713 int diff
; /* temp value */
1714 xfs_bmbt_rec_host_t
*ep
; /* extent entry for idx */
1715 int error
; /* error return value */
1716 int i
; /* temp state */
1717 xfs_ifork_t
*ifp
; /* inode fork pointer */
1718 xfs_fileoff_t new_endoff
; /* end offset of new entry */
1719 xfs_bmbt_irec_t r
[3]; /* neighbor extent entries */
1720 /* left is 0, right is 1, prev is 2 */
1721 int rval
=0; /* return value (logging flags) */
1722 int state
= 0;/* state bits, accessed thru macros */
1723 xfs_filblks_t da_new
; /* new count del alloc blocks used */
1724 xfs_filblks_t da_old
; /* old count del alloc blocks used */
1725 xfs_filblks_t temp
=0; /* value for da_new calculations */
1726 xfs_filblks_t temp2
=0;/* value for da_new calculations */
1727 int tmp_rval
; /* partial logging flags */
1728 struct xfs_mount
*mp
;
1730 mp
= bma
->tp
? bma
->tp
->t_mountp
: NULL
;
1731 ifp
= XFS_IFORK_PTR(bma
->ip
, XFS_DATA_FORK
);
1733 ASSERT(bma
->idx
>= 0);
1734 ASSERT(bma
->idx
<= ifp
->if_bytes
/ sizeof(struct xfs_bmbt_rec
));
1735 ASSERT(!isnullstartblock(new->br_startblock
));
1737 (bma
->cur
->bc_private
.b
.flags
& XFS_BTCUR_BPRV_WASDEL
));
1739 XFS_STATS_INC(mp
, xs_add_exlist
);
1746 * Set up a bunch of variables to make the tests simpler.
1748 ep
= xfs_iext_get_ext(ifp
, bma
->idx
);
1749 xfs_bmbt_get_all(ep
, &PREV
);
1750 new_endoff
= new->br_startoff
+ new->br_blockcount
;
1751 ASSERT(PREV
.br_startoff
<= new->br_startoff
);
1752 ASSERT(PREV
.br_startoff
+ PREV
.br_blockcount
>= new_endoff
);
1754 da_old
= startblockval(PREV
.br_startblock
);
1758 * Set flags determining what part of the previous delayed allocation
1759 * extent is being replaced by a real allocation.
1761 if (PREV
.br_startoff
== new->br_startoff
)
1762 state
|= BMAP_LEFT_FILLING
;
1763 if (PREV
.br_startoff
+ PREV
.br_blockcount
== new_endoff
)
1764 state
|= BMAP_RIGHT_FILLING
;
1767 * Check and set flags if this segment has a left neighbor.
1768 * Don't set contiguous if the combined extent would be too large.
1771 state
|= BMAP_LEFT_VALID
;
1772 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
- 1), &LEFT
);
1774 if (isnullstartblock(LEFT
.br_startblock
))
1775 state
|= BMAP_LEFT_DELAY
;
1778 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
1779 LEFT
.br_startoff
+ LEFT
.br_blockcount
== new->br_startoff
&&
1780 LEFT
.br_startblock
+ LEFT
.br_blockcount
== new->br_startblock
&&
1781 LEFT
.br_state
== new->br_state
&&
1782 LEFT
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
1783 state
|= BMAP_LEFT_CONTIG
;
1786 * Check and set flags if this segment has a right neighbor.
1787 * Don't set contiguous if the combined extent would be too large.
1788 * Also check for all-three-contiguous being too large.
1790 if (bma
->idx
< bma
->ip
->i_df
.if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
) - 1) {
1791 state
|= BMAP_RIGHT_VALID
;
1792 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
+ 1), &RIGHT
);
1794 if (isnullstartblock(RIGHT
.br_startblock
))
1795 state
|= BMAP_RIGHT_DELAY
;
1798 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
1799 new_endoff
== RIGHT
.br_startoff
&&
1800 new->br_startblock
+ new->br_blockcount
== RIGHT
.br_startblock
&&
1801 new->br_state
== RIGHT
.br_state
&&
1802 new->br_blockcount
+ RIGHT
.br_blockcount
<= MAXEXTLEN
&&
1803 ((state
& (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
1804 BMAP_RIGHT_FILLING
)) !=
1805 (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
1806 BMAP_RIGHT_FILLING
) ||
1807 LEFT
.br_blockcount
+ new->br_blockcount
+ RIGHT
.br_blockcount
1809 state
|= BMAP_RIGHT_CONTIG
;
1813 * Switch out based on the FILLING and CONTIG state bits.
1815 switch (state
& (BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
1816 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
)) {
1817 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
1818 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
1820 * Filling in all of a previously delayed allocation extent.
1821 * The left and right neighbors are both contiguous with new.
1824 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1825 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, bma
->idx
),
1826 LEFT
.br_blockcount
+ PREV
.br_blockcount
+
1827 RIGHT
.br_blockcount
);
1828 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1830 xfs_iext_remove(bma
->ip
, bma
->idx
+ 1, 2, state
);
1831 bma
->ip
->i_d
.di_nextents
--;
1832 if (bma
->cur
== NULL
)
1833 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1835 rval
= XFS_ILOG_CORE
;
1836 error
= xfs_bmbt_lookup_eq(bma
->cur
, RIGHT
.br_startoff
,
1837 RIGHT
.br_startblock
,
1838 RIGHT
.br_blockcount
, &i
);
1841 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1842 error
= xfs_btree_delete(bma
->cur
, &i
);
1845 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1846 error
= xfs_btree_decrement(bma
->cur
, 0, &i
);
1849 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1850 error
= xfs_bmbt_update(bma
->cur
, LEFT
.br_startoff
,
1852 LEFT
.br_blockcount
+
1853 PREV
.br_blockcount
+
1854 RIGHT
.br_blockcount
, LEFT
.br_state
);
1860 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
1862 * Filling in all of a previously delayed allocation extent.
1863 * The left neighbor is contiguous, the right is not.
1867 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1868 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, bma
->idx
),
1869 LEFT
.br_blockcount
+ PREV
.br_blockcount
);
1870 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1872 xfs_iext_remove(bma
->ip
, bma
->idx
+ 1, 1, state
);
1873 if (bma
->cur
== NULL
)
1874 rval
= XFS_ILOG_DEXT
;
1877 error
= xfs_bmbt_lookup_eq(bma
->cur
, LEFT
.br_startoff
,
1878 LEFT
.br_startblock
, LEFT
.br_blockcount
,
1882 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1883 error
= xfs_bmbt_update(bma
->cur
, LEFT
.br_startoff
,
1885 LEFT
.br_blockcount
+
1886 PREV
.br_blockcount
, LEFT
.br_state
);
1892 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
1894 * Filling in all of a previously delayed allocation extent.
1895 * The right neighbor is contiguous, the left is not.
1897 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1898 xfs_bmbt_set_startblock(ep
, new->br_startblock
);
1899 xfs_bmbt_set_blockcount(ep
,
1900 PREV
.br_blockcount
+ RIGHT
.br_blockcount
);
1901 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1903 xfs_iext_remove(bma
->ip
, bma
->idx
+ 1, 1, state
);
1904 if (bma
->cur
== NULL
)
1905 rval
= XFS_ILOG_DEXT
;
1908 error
= xfs_bmbt_lookup_eq(bma
->cur
, RIGHT
.br_startoff
,
1909 RIGHT
.br_startblock
,
1910 RIGHT
.br_blockcount
, &i
);
1913 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1914 error
= xfs_bmbt_update(bma
->cur
, PREV
.br_startoff
,
1916 PREV
.br_blockcount
+
1917 RIGHT
.br_blockcount
, PREV
.br_state
);
1923 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
1925 * Filling in all of a previously delayed allocation extent.
1926 * Neither the left nor right neighbors are contiguous with
1929 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1930 xfs_bmbt_set_startblock(ep
, new->br_startblock
);
1931 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1933 bma
->ip
->i_d
.di_nextents
++;
1934 if (bma
->cur
== NULL
)
1935 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1937 rval
= XFS_ILOG_CORE
;
1938 error
= xfs_bmbt_lookup_eq(bma
->cur
, new->br_startoff
,
1939 new->br_startblock
, new->br_blockcount
,
1943 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
1944 bma
->cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
1945 error
= xfs_btree_insert(bma
->cur
, &i
);
1948 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1952 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
:
1954 * Filling in the first part of a previous delayed allocation.
1955 * The left neighbor is contiguous.
1957 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
- 1, state
, _THIS_IP_
);
1958 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, bma
->idx
- 1),
1959 LEFT
.br_blockcount
+ new->br_blockcount
);
1960 xfs_bmbt_set_startoff(ep
,
1961 PREV
.br_startoff
+ new->br_blockcount
);
1962 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
- 1, state
, _THIS_IP_
);
1964 temp
= PREV
.br_blockcount
- new->br_blockcount
;
1965 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1966 xfs_bmbt_set_blockcount(ep
, temp
);
1967 if (bma
->cur
== NULL
)
1968 rval
= XFS_ILOG_DEXT
;
1971 error
= xfs_bmbt_lookup_eq(bma
->cur
, LEFT
.br_startoff
,
1972 LEFT
.br_startblock
, LEFT
.br_blockcount
,
1976 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1977 error
= xfs_bmbt_update(bma
->cur
, LEFT
.br_startoff
,
1979 LEFT
.br_blockcount
+
1985 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
1986 startblockval(PREV
.br_startblock
));
1987 xfs_bmbt_set_startblock(ep
, nullstartblock(da_new
));
1988 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1993 case BMAP_LEFT_FILLING
:
1995 * Filling in the first part of a previous delayed allocation.
1996 * The left neighbor is not contiguous.
1998 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1999 xfs_bmbt_set_startoff(ep
, new_endoff
);
2000 temp
= PREV
.br_blockcount
- new->br_blockcount
;
2001 xfs_bmbt_set_blockcount(ep
, temp
);
2002 xfs_iext_insert(bma
->ip
, bma
->idx
, 1, new, state
);
2003 bma
->ip
->i_d
.di_nextents
++;
2004 if (bma
->cur
== NULL
)
2005 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2007 rval
= XFS_ILOG_CORE
;
2008 error
= xfs_bmbt_lookup_eq(bma
->cur
, new->br_startoff
,
2009 new->br_startblock
, new->br_blockcount
,
2013 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
2014 bma
->cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
2015 error
= xfs_btree_insert(bma
->cur
, &i
);
2018 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2021 if (xfs_bmap_needs_btree(bma
->ip
, XFS_DATA_FORK
)) {
2022 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
2023 bma
->firstblock
, bma
->flist
,
2024 &bma
->cur
, 1, &tmp_rval
, XFS_DATA_FORK
);
2029 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
2030 startblockval(PREV
.br_startblock
) -
2031 (bma
->cur
? bma
->cur
->bc_private
.b
.allocated
: 0));
2032 ep
= xfs_iext_get_ext(ifp
, bma
->idx
+ 1);
2033 xfs_bmbt_set_startblock(ep
, nullstartblock(da_new
));
2034 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
+ 1, state
, _THIS_IP_
);
2037 case BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2039 * Filling in the last part of a previous delayed allocation.
2040 * The right neighbor is contiguous with the new allocation.
2042 temp
= PREV
.br_blockcount
- new->br_blockcount
;
2043 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
+ 1, state
, _THIS_IP_
);
2044 xfs_bmbt_set_blockcount(ep
, temp
);
2045 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp
, bma
->idx
+ 1),
2046 new->br_startoff
, new->br_startblock
,
2047 new->br_blockcount
+ RIGHT
.br_blockcount
,
2049 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
+ 1, state
, _THIS_IP_
);
2050 if (bma
->cur
== NULL
)
2051 rval
= XFS_ILOG_DEXT
;
2054 error
= xfs_bmbt_lookup_eq(bma
->cur
, RIGHT
.br_startoff
,
2055 RIGHT
.br_startblock
,
2056 RIGHT
.br_blockcount
, &i
);
2059 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2060 error
= xfs_bmbt_update(bma
->cur
, new->br_startoff
,
2062 new->br_blockcount
+
2063 RIGHT
.br_blockcount
,
2069 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
2070 startblockval(PREV
.br_startblock
));
2071 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2072 xfs_bmbt_set_startblock(ep
, nullstartblock(da_new
));
2073 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2078 case BMAP_RIGHT_FILLING
:
2080 * Filling in the last part of a previous delayed allocation.
2081 * The right neighbor is not contiguous.
2083 temp
= PREV
.br_blockcount
- new->br_blockcount
;
2084 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2085 xfs_bmbt_set_blockcount(ep
, temp
);
2086 xfs_iext_insert(bma
->ip
, bma
->idx
+ 1, 1, new, state
);
2087 bma
->ip
->i_d
.di_nextents
++;
2088 if (bma
->cur
== NULL
)
2089 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2091 rval
= XFS_ILOG_CORE
;
2092 error
= xfs_bmbt_lookup_eq(bma
->cur
, new->br_startoff
,
2093 new->br_startblock
, new->br_blockcount
,
2097 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
2098 bma
->cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
2099 error
= xfs_btree_insert(bma
->cur
, &i
);
2102 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2105 if (xfs_bmap_needs_btree(bma
->ip
, XFS_DATA_FORK
)) {
2106 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
2107 bma
->firstblock
, bma
->flist
, &bma
->cur
, 1,
2108 &tmp_rval
, XFS_DATA_FORK
);
2113 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
2114 startblockval(PREV
.br_startblock
) -
2115 (bma
->cur
? bma
->cur
->bc_private
.b
.allocated
: 0));
2116 ep
= xfs_iext_get_ext(ifp
, bma
->idx
);
2117 xfs_bmbt_set_startblock(ep
, nullstartblock(da_new
));
2118 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2125 * Filling in the middle part of a previous delayed allocation.
2126 * Contiguity is impossible here.
2127 * This case is avoided almost all the time.
2129 * We start with a delayed allocation:
2131 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
2134 * and we are allocating:
2135 * +rrrrrrrrrrrrrrrrr+
2138 * and we set it up for insertion as:
2139 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
2141 * PREV @ idx LEFT RIGHT
2142 * inserted at idx + 1
2144 temp
= new->br_startoff
- PREV
.br_startoff
;
2145 temp2
= PREV
.br_startoff
+ PREV
.br_blockcount
- new_endoff
;
2146 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, 0, _THIS_IP_
);
2147 xfs_bmbt_set_blockcount(ep
, temp
); /* truncate PREV */
2149 RIGHT
.br_state
= PREV
.br_state
;
2150 RIGHT
.br_startblock
= nullstartblock(
2151 (int)xfs_bmap_worst_indlen(bma
->ip
, temp2
));
2152 RIGHT
.br_startoff
= new_endoff
;
2153 RIGHT
.br_blockcount
= temp2
;
2154 /* insert LEFT (r[0]) and RIGHT (r[1]) at the same time */
2155 xfs_iext_insert(bma
->ip
, bma
->idx
+ 1, 2, &LEFT
, state
);
2156 bma
->ip
->i_d
.di_nextents
++;
2157 if (bma
->cur
== NULL
)
2158 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2160 rval
= XFS_ILOG_CORE
;
2161 error
= xfs_bmbt_lookup_eq(bma
->cur
, new->br_startoff
,
2162 new->br_startblock
, new->br_blockcount
,
2166 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
2167 bma
->cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
2168 error
= xfs_btree_insert(bma
->cur
, &i
);
2171 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2174 if (xfs_bmap_needs_btree(bma
->ip
, XFS_DATA_FORK
)) {
2175 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
2176 bma
->firstblock
, bma
->flist
, &bma
->cur
,
2177 1, &tmp_rval
, XFS_DATA_FORK
);
2182 temp
= xfs_bmap_worst_indlen(bma
->ip
, temp
);
2183 temp2
= xfs_bmap_worst_indlen(bma
->ip
, temp2
);
2184 diff
= (int)(temp
+ temp2
-
2185 (startblockval(PREV
.br_startblock
) -
2187 bma
->cur
->bc_private
.b
.allocated
: 0)));
2189 error
= xfs_mod_fdblocks(bma
->ip
->i_mount
,
2190 -((int64_t)diff
), false);
2196 ep
= xfs_iext_get_ext(ifp
, bma
->idx
);
2197 xfs_bmbt_set_startblock(ep
, nullstartblock((int)temp
));
2198 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2199 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
+ 2, state
, _THIS_IP_
);
2200 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp
, bma
->idx
+ 2),
2201 nullstartblock((int)temp2
));
2202 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
+ 2, state
, _THIS_IP_
);
2205 da_new
= temp
+ temp2
;
2208 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2209 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2210 case BMAP_LEFT_FILLING
| BMAP_RIGHT_CONTIG
:
2211 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
2212 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2213 case BMAP_LEFT_CONTIG
:
2214 case BMAP_RIGHT_CONTIG
:
2216 * These cases are all impossible.
2221 /* convert to a btree if necessary */
2222 if (xfs_bmap_needs_btree(bma
->ip
, XFS_DATA_FORK
)) {
2223 int tmp_logflags
; /* partial log flag return val */
2225 ASSERT(bma
->cur
== NULL
);
2226 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
2227 bma
->firstblock
, bma
->flist
, &bma
->cur
,
2228 da_old
> 0, &tmp_logflags
, XFS_DATA_FORK
);
2229 bma
->logflags
|= tmp_logflags
;
2234 /* adjust for changes in reserved delayed indirect blocks */
2235 if (da_old
|| da_new
) {
2238 temp
+= bma
->cur
->bc_private
.b
.allocated
;
2240 xfs_mod_fdblocks(bma
->ip
->i_mount
,
2241 (int64_t)(da_old
- temp
), false);
2244 /* clear out the allocated field, done with it now in any case. */
2246 bma
->cur
->bc_private
.b
.allocated
= 0;
2248 xfs_bmap_check_leaf_extents(bma
->cur
, bma
->ip
, XFS_DATA_FORK
);
2250 bma
->logflags
|= rval
;
2258 * Convert an unwritten allocation to a real allocation or vice versa.
2260 STATIC
int /* error */
2261 xfs_bmap_add_extent_unwritten_real(
2262 struct xfs_trans
*tp
,
2263 xfs_inode_t
*ip
, /* incore inode pointer */
2264 xfs_extnum_t
*idx
, /* extent number to update/insert */
2265 xfs_btree_cur_t
**curp
, /* if *curp is null, not a btree */
2266 xfs_bmbt_irec_t
*new, /* new data to add to file extents */
2267 xfs_fsblock_t
*first
, /* pointer to firstblock variable */
2268 xfs_bmap_free_t
*flist
, /* list of extents to be freed */
2269 int *logflagsp
) /* inode logging flags */
2271 xfs_btree_cur_t
*cur
; /* btree cursor */
2272 xfs_bmbt_rec_host_t
*ep
; /* extent entry for idx */
2273 int error
; /* error return value */
2274 int i
; /* temp state */
2275 xfs_ifork_t
*ifp
; /* inode fork pointer */
2276 xfs_fileoff_t new_endoff
; /* end offset of new entry */
2277 xfs_exntst_t newext
; /* new extent state */
2278 xfs_exntst_t oldext
; /* old extent state */
2279 xfs_bmbt_irec_t r
[3]; /* neighbor extent entries */
2280 /* left is 0, right is 1, prev is 2 */
2281 int rval
=0; /* return value (logging flags) */
2282 int state
= 0;/* state bits, accessed thru macros */
2283 struct xfs_mount
*mp
= tp
->t_mountp
;
2288 ifp
= XFS_IFORK_PTR(ip
, XFS_DATA_FORK
);
2291 ASSERT(*idx
<= ifp
->if_bytes
/ sizeof(struct xfs_bmbt_rec
));
2292 ASSERT(!isnullstartblock(new->br_startblock
));
2294 XFS_STATS_INC(mp
, xs_add_exlist
);
2301 * Set up a bunch of variables to make the tests simpler.
2304 ep
= xfs_iext_get_ext(ifp
, *idx
);
2305 xfs_bmbt_get_all(ep
, &PREV
);
2306 newext
= new->br_state
;
2307 oldext
= (newext
== XFS_EXT_UNWRITTEN
) ?
2308 XFS_EXT_NORM
: XFS_EXT_UNWRITTEN
;
2309 ASSERT(PREV
.br_state
== oldext
);
2310 new_endoff
= new->br_startoff
+ new->br_blockcount
;
2311 ASSERT(PREV
.br_startoff
<= new->br_startoff
);
2312 ASSERT(PREV
.br_startoff
+ PREV
.br_blockcount
>= new_endoff
);
2315 * Set flags determining what part of the previous oldext allocation
2316 * extent is being replaced by a newext allocation.
2318 if (PREV
.br_startoff
== new->br_startoff
)
2319 state
|= BMAP_LEFT_FILLING
;
2320 if (PREV
.br_startoff
+ PREV
.br_blockcount
== new_endoff
)
2321 state
|= BMAP_RIGHT_FILLING
;
2324 * Check and set flags if this segment has a left neighbor.
2325 * Don't set contiguous if the combined extent would be too large.
2328 state
|= BMAP_LEFT_VALID
;
2329 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, *idx
- 1), &LEFT
);
2331 if (isnullstartblock(LEFT
.br_startblock
))
2332 state
|= BMAP_LEFT_DELAY
;
2335 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
2336 LEFT
.br_startoff
+ LEFT
.br_blockcount
== new->br_startoff
&&
2337 LEFT
.br_startblock
+ LEFT
.br_blockcount
== new->br_startblock
&&
2338 LEFT
.br_state
== newext
&&
2339 LEFT
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
2340 state
|= BMAP_LEFT_CONTIG
;
2343 * Check and set flags if this segment has a right neighbor.
2344 * Don't set contiguous if the combined extent would be too large.
2345 * Also check for all-three-contiguous being too large.
2347 if (*idx
< ip
->i_df
.if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
) - 1) {
2348 state
|= BMAP_RIGHT_VALID
;
2349 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, *idx
+ 1), &RIGHT
);
2350 if (isnullstartblock(RIGHT
.br_startblock
))
2351 state
|= BMAP_RIGHT_DELAY
;
2354 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
2355 new_endoff
== RIGHT
.br_startoff
&&
2356 new->br_startblock
+ new->br_blockcount
== RIGHT
.br_startblock
&&
2357 newext
== RIGHT
.br_state
&&
2358 new->br_blockcount
+ RIGHT
.br_blockcount
<= MAXEXTLEN
&&
2359 ((state
& (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
2360 BMAP_RIGHT_FILLING
)) !=
2361 (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
2362 BMAP_RIGHT_FILLING
) ||
2363 LEFT
.br_blockcount
+ new->br_blockcount
+ RIGHT
.br_blockcount
2365 state
|= BMAP_RIGHT_CONTIG
;
2368 * Switch out based on the FILLING and CONTIG state bits.
2370 switch (state
& (BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
2371 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
)) {
2372 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
2373 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2375 * Setting all of a previous oldext extent to newext.
2376 * The left and right neighbors are both contiguous with new.
2380 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2381 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
),
2382 LEFT
.br_blockcount
+ PREV
.br_blockcount
+
2383 RIGHT
.br_blockcount
);
2384 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2386 xfs_iext_remove(ip
, *idx
+ 1, 2, state
);
2387 ip
->i_d
.di_nextents
-= 2;
2389 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2391 rval
= XFS_ILOG_CORE
;
2392 if ((error
= xfs_bmbt_lookup_eq(cur
, RIGHT
.br_startoff
,
2393 RIGHT
.br_startblock
,
2394 RIGHT
.br_blockcount
, &i
)))
2396 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2397 if ((error
= xfs_btree_delete(cur
, &i
)))
2399 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2400 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2402 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2403 if ((error
= xfs_btree_delete(cur
, &i
)))
2405 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2406 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2408 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2409 if ((error
= xfs_bmbt_update(cur
, LEFT
.br_startoff
,
2411 LEFT
.br_blockcount
+ PREV
.br_blockcount
+
2412 RIGHT
.br_blockcount
, LEFT
.br_state
)))
2417 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
2419 * Setting all of a previous oldext extent to newext.
2420 * The left neighbor is contiguous, the right is not.
2424 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2425 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
),
2426 LEFT
.br_blockcount
+ PREV
.br_blockcount
);
2427 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2429 xfs_iext_remove(ip
, *idx
+ 1, 1, state
);
2430 ip
->i_d
.di_nextents
--;
2432 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2434 rval
= XFS_ILOG_CORE
;
2435 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2436 PREV
.br_startblock
, PREV
.br_blockcount
,
2439 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2440 if ((error
= xfs_btree_delete(cur
, &i
)))
2442 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2443 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2445 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2446 if ((error
= xfs_bmbt_update(cur
, LEFT
.br_startoff
,
2448 LEFT
.br_blockcount
+ PREV
.br_blockcount
,
2454 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2456 * Setting all of a previous oldext extent to newext.
2457 * The right neighbor is contiguous, the left is not.
2459 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2460 xfs_bmbt_set_blockcount(ep
,
2461 PREV
.br_blockcount
+ RIGHT
.br_blockcount
);
2462 xfs_bmbt_set_state(ep
, newext
);
2463 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2464 xfs_iext_remove(ip
, *idx
+ 1, 1, state
);
2465 ip
->i_d
.di_nextents
--;
2467 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2469 rval
= XFS_ILOG_CORE
;
2470 if ((error
= xfs_bmbt_lookup_eq(cur
, RIGHT
.br_startoff
,
2471 RIGHT
.br_startblock
,
2472 RIGHT
.br_blockcount
, &i
)))
2474 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2475 if ((error
= xfs_btree_delete(cur
, &i
)))
2477 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2478 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2480 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2481 if ((error
= xfs_bmbt_update(cur
, new->br_startoff
,
2483 new->br_blockcount
+ RIGHT
.br_blockcount
,
2489 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
2491 * Setting all of a previous oldext extent to newext.
2492 * Neither the left nor right neighbors are contiguous with
2495 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2496 xfs_bmbt_set_state(ep
, newext
);
2497 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2500 rval
= XFS_ILOG_DEXT
;
2503 if ((error
= xfs_bmbt_lookup_eq(cur
, new->br_startoff
,
2504 new->br_startblock
, new->br_blockcount
,
2507 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2508 if ((error
= xfs_bmbt_update(cur
, new->br_startoff
,
2509 new->br_startblock
, new->br_blockcount
,
2515 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
:
2517 * Setting the first part of a previous oldext extent to newext.
2518 * The left neighbor is contiguous.
2520 trace_xfs_bmap_pre_update(ip
, *idx
- 1, state
, _THIS_IP_
);
2521 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
- 1),
2522 LEFT
.br_blockcount
+ new->br_blockcount
);
2523 xfs_bmbt_set_startoff(ep
,
2524 PREV
.br_startoff
+ new->br_blockcount
);
2525 trace_xfs_bmap_post_update(ip
, *idx
- 1, state
, _THIS_IP_
);
2527 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2528 xfs_bmbt_set_startblock(ep
,
2529 new->br_startblock
+ new->br_blockcount
);
2530 xfs_bmbt_set_blockcount(ep
,
2531 PREV
.br_blockcount
- new->br_blockcount
);
2532 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2537 rval
= XFS_ILOG_DEXT
;
2540 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2541 PREV
.br_startblock
, PREV
.br_blockcount
,
2544 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2545 if ((error
= xfs_bmbt_update(cur
,
2546 PREV
.br_startoff
+ new->br_blockcount
,
2547 PREV
.br_startblock
+ new->br_blockcount
,
2548 PREV
.br_blockcount
- new->br_blockcount
,
2551 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2553 error
= xfs_bmbt_update(cur
, LEFT
.br_startoff
,
2555 LEFT
.br_blockcount
+ new->br_blockcount
,
2562 case BMAP_LEFT_FILLING
:
2564 * Setting the first part of a previous oldext extent to newext.
2565 * The left neighbor is not contiguous.
2567 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2568 ASSERT(ep
&& xfs_bmbt_get_state(ep
) == oldext
);
2569 xfs_bmbt_set_startoff(ep
, new_endoff
);
2570 xfs_bmbt_set_blockcount(ep
,
2571 PREV
.br_blockcount
- new->br_blockcount
);
2572 xfs_bmbt_set_startblock(ep
,
2573 new->br_startblock
+ new->br_blockcount
);
2574 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2576 xfs_iext_insert(ip
, *idx
, 1, new, state
);
2577 ip
->i_d
.di_nextents
++;
2579 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2581 rval
= XFS_ILOG_CORE
;
2582 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2583 PREV
.br_startblock
, PREV
.br_blockcount
,
2586 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2587 if ((error
= xfs_bmbt_update(cur
,
2588 PREV
.br_startoff
+ new->br_blockcount
,
2589 PREV
.br_startblock
+ new->br_blockcount
,
2590 PREV
.br_blockcount
- new->br_blockcount
,
2593 cur
->bc_rec
.b
= *new;
2594 if ((error
= xfs_btree_insert(cur
, &i
)))
2596 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2600 case BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2602 * Setting the last part of a previous oldext extent to newext.
2603 * The right neighbor is contiguous with the new allocation.
2605 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2606 xfs_bmbt_set_blockcount(ep
,
2607 PREV
.br_blockcount
- new->br_blockcount
);
2608 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2612 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2613 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp
, *idx
),
2614 new->br_startoff
, new->br_startblock
,
2615 new->br_blockcount
+ RIGHT
.br_blockcount
, newext
);
2616 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2619 rval
= XFS_ILOG_DEXT
;
2622 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2624 PREV
.br_blockcount
, &i
)))
2626 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2627 if ((error
= xfs_bmbt_update(cur
, PREV
.br_startoff
,
2629 PREV
.br_blockcount
- new->br_blockcount
,
2632 if ((error
= xfs_btree_increment(cur
, 0, &i
)))
2634 if ((error
= xfs_bmbt_update(cur
, new->br_startoff
,
2636 new->br_blockcount
+ RIGHT
.br_blockcount
,
2642 case BMAP_RIGHT_FILLING
:
2644 * Setting the last part of a previous oldext extent to newext.
2645 * The right neighbor is not contiguous.
2647 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2648 xfs_bmbt_set_blockcount(ep
,
2649 PREV
.br_blockcount
- new->br_blockcount
);
2650 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2653 xfs_iext_insert(ip
, *idx
, 1, new, state
);
2655 ip
->i_d
.di_nextents
++;
2657 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2659 rval
= XFS_ILOG_CORE
;
2660 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2661 PREV
.br_startblock
, PREV
.br_blockcount
,
2664 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2665 if ((error
= xfs_bmbt_update(cur
, PREV
.br_startoff
,
2667 PREV
.br_blockcount
- new->br_blockcount
,
2670 if ((error
= xfs_bmbt_lookup_eq(cur
, new->br_startoff
,
2671 new->br_startblock
, new->br_blockcount
,
2674 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
2675 cur
->bc_rec
.b
.br_state
= new->br_state
;
2676 if ((error
= xfs_btree_insert(cur
, &i
)))
2678 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2684 * Setting the middle part of a previous oldext extent to
2685 * newext. Contiguity is impossible here.
2686 * One extent becomes three extents.
2688 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2689 xfs_bmbt_set_blockcount(ep
,
2690 new->br_startoff
- PREV
.br_startoff
);
2691 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2694 r
[1].br_startoff
= new_endoff
;
2695 r
[1].br_blockcount
=
2696 PREV
.br_startoff
+ PREV
.br_blockcount
- new_endoff
;
2697 r
[1].br_startblock
= new->br_startblock
+ new->br_blockcount
;
2698 r
[1].br_state
= oldext
;
2701 xfs_iext_insert(ip
, *idx
, 2, &r
[0], state
);
2703 ip
->i_d
.di_nextents
+= 2;
2705 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2707 rval
= XFS_ILOG_CORE
;
2708 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2709 PREV
.br_startblock
, PREV
.br_blockcount
,
2712 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2713 /* new right extent - oldext */
2714 if ((error
= xfs_bmbt_update(cur
, r
[1].br_startoff
,
2715 r
[1].br_startblock
, r
[1].br_blockcount
,
2718 /* new left extent - oldext */
2719 cur
->bc_rec
.b
= PREV
;
2720 cur
->bc_rec
.b
.br_blockcount
=
2721 new->br_startoff
- PREV
.br_startoff
;
2722 if ((error
= xfs_btree_insert(cur
, &i
)))
2724 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2726 * Reset the cursor to the position of the new extent
2727 * we are about to insert as we can't trust it after
2728 * the previous insert.
2730 if ((error
= xfs_bmbt_lookup_eq(cur
, new->br_startoff
,
2731 new->br_startblock
, new->br_blockcount
,
2734 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
2735 /* new middle extent - newext */
2736 cur
->bc_rec
.b
.br_state
= new->br_state
;
2737 if ((error
= xfs_btree_insert(cur
, &i
)))
2739 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2743 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2744 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2745 case BMAP_LEFT_FILLING
| BMAP_RIGHT_CONTIG
:
2746 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
2747 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2748 case BMAP_LEFT_CONTIG
:
2749 case BMAP_RIGHT_CONTIG
:
2751 * These cases are all impossible.
2756 /* convert to a btree if necessary */
2757 if (xfs_bmap_needs_btree(ip
, XFS_DATA_FORK
)) {
2758 int tmp_logflags
; /* partial log flag return val */
2760 ASSERT(cur
== NULL
);
2761 error
= xfs_bmap_extents_to_btree(tp
, ip
, first
, flist
, &cur
,
2762 0, &tmp_logflags
, XFS_DATA_FORK
);
2763 *logflagsp
|= tmp_logflags
;
2768 /* clear out the allocated field, done with it now in any case. */
2770 cur
->bc_private
.b
.allocated
= 0;
2774 xfs_bmap_check_leaf_extents(*curp
, ip
, XFS_DATA_FORK
);
2784 * Convert a hole to a delayed allocation.
2787 xfs_bmap_add_extent_hole_delay(
2788 xfs_inode_t
*ip
, /* incore inode pointer */
2789 xfs_extnum_t
*idx
, /* extent number to update/insert */
2790 xfs_bmbt_irec_t
*new) /* new data to add to file extents */
2792 xfs_ifork_t
*ifp
; /* inode fork pointer */
2793 xfs_bmbt_irec_t left
; /* left neighbor extent entry */
2794 xfs_filblks_t newlen
=0; /* new indirect size */
2795 xfs_filblks_t oldlen
=0; /* old indirect size */
2796 xfs_bmbt_irec_t right
; /* right neighbor extent entry */
2797 int state
; /* state bits, accessed thru macros */
2798 xfs_filblks_t temp
=0; /* temp for indirect calculations */
2800 ifp
= XFS_IFORK_PTR(ip
, XFS_DATA_FORK
);
2802 ASSERT(isnullstartblock(new->br_startblock
));
2805 * Check and set flags if this segment has a left neighbor
2808 state
|= BMAP_LEFT_VALID
;
2809 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, *idx
- 1), &left
);
2811 if (isnullstartblock(left
.br_startblock
))
2812 state
|= BMAP_LEFT_DELAY
;
2816 * Check and set flags if the current (right) segment exists.
2817 * If it doesn't exist, we're converting the hole at end-of-file.
2819 if (*idx
< ip
->i_df
.if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
)) {
2820 state
|= BMAP_RIGHT_VALID
;
2821 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, *idx
), &right
);
2823 if (isnullstartblock(right
.br_startblock
))
2824 state
|= BMAP_RIGHT_DELAY
;
2828 * Set contiguity flags on the left and right neighbors.
2829 * Don't let extents get too large, even if the pieces are contiguous.
2831 if ((state
& BMAP_LEFT_VALID
) && (state
& BMAP_LEFT_DELAY
) &&
2832 left
.br_startoff
+ left
.br_blockcount
== new->br_startoff
&&
2833 left
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
2834 state
|= BMAP_LEFT_CONTIG
;
2836 if ((state
& BMAP_RIGHT_VALID
) && (state
& BMAP_RIGHT_DELAY
) &&
2837 new->br_startoff
+ new->br_blockcount
== right
.br_startoff
&&
2838 new->br_blockcount
+ right
.br_blockcount
<= MAXEXTLEN
&&
2839 (!(state
& BMAP_LEFT_CONTIG
) ||
2840 (left
.br_blockcount
+ new->br_blockcount
+
2841 right
.br_blockcount
<= MAXEXTLEN
)))
2842 state
|= BMAP_RIGHT_CONTIG
;
2845 * Switch out based on the contiguity flags.
2847 switch (state
& (BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
)) {
2848 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2850 * New allocation is contiguous with delayed allocations
2851 * on the left and on the right.
2852 * Merge all three into a single extent record.
2855 temp
= left
.br_blockcount
+ new->br_blockcount
+
2856 right
.br_blockcount
;
2858 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2859 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
), temp
);
2860 oldlen
= startblockval(left
.br_startblock
) +
2861 startblockval(new->br_startblock
) +
2862 startblockval(right
.br_startblock
);
2863 newlen
= xfs_bmap_worst_indlen(ip
, temp
);
2864 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp
, *idx
),
2865 nullstartblock((int)newlen
));
2866 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2868 xfs_iext_remove(ip
, *idx
+ 1, 1, state
);
2871 case BMAP_LEFT_CONTIG
:
2873 * New allocation is contiguous with a delayed allocation
2875 * Merge the new allocation with the left neighbor.
2878 temp
= left
.br_blockcount
+ new->br_blockcount
;
2880 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2881 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
), temp
);
2882 oldlen
= startblockval(left
.br_startblock
) +
2883 startblockval(new->br_startblock
);
2884 newlen
= xfs_bmap_worst_indlen(ip
, temp
);
2885 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp
, *idx
),
2886 nullstartblock((int)newlen
));
2887 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2890 case BMAP_RIGHT_CONTIG
:
2892 * New allocation is contiguous with a delayed allocation
2894 * Merge the new allocation with the right neighbor.
2896 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2897 temp
= new->br_blockcount
+ right
.br_blockcount
;
2898 oldlen
= startblockval(new->br_startblock
) +
2899 startblockval(right
.br_startblock
);
2900 newlen
= xfs_bmap_worst_indlen(ip
, temp
);
2901 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp
, *idx
),
2903 nullstartblock((int)newlen
), temp
, right
.br_state
);
2904 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2909 * New allocation is not contiguous with another
2910 * delayed allocation.
2911 * Insert a new entry.
2913 oldlen
= newlen
= 0;
2914 xfs_iext_insert(ip
, *idx
, 1, new, state
);
2917 if (oldlen
!= newlen
) {
2918 ASSERT(oldlen
> newlen
);
2919 xfs_mod_fdblocks(ip
->i_mount
, (int64_t)(oldlen
- newlen
),
2922 * Nothing to do for disk quota accounting here.
2928 * Convert a hole to a real allocation.
2930 STATIC
int /* error */
2931 xfs_bmap_add_extent_hole_real(
2932 struct xfs_bmalloca
*bma
,
2935 struct xfs_bmbt_irec
*new = &bma
->got
;
2936 int error
; /* error return value */
2937 int i
; /* temp state */
2938 xfs_ifork_t
*ifp
; /* inode fork pointer */
2939 xfs_bmbt_irec_t left
; /* left neighbor extent entry */
2940 xfs_bmbt_irec_t right
; /* right neighbor extent entry */
2941 int rval
=0; /* return value (logging flags) */
2942 int state
; /* state bits, accessed thru macros */
2943 struct xfs_mount
*mp
;
2945 mp
= bma
->tp
? bma
->tp
->t_mountp
: NULL
;
2946 ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
2948 ASSERT(bma
->idx
>= 0);
2949 ASSERT(bma
->idx
<= ifp
->if_bytes
/ sizeof(struct xfs_bmbt_rec
));
2950 ASSERT(!isnullstartblock(new->br_startblock
));
2952 !(bma
->cur
->bc_private
.b
.flags
& XFS_BTCUR_BPRV_WASDEL
));
2954 XFS_STATS_INC(mp
, xs_add_exlist
);
2957 if (whichfork
== XFS_ATTR_FORK
)
2958 state
|= BMAP_ATTRFORK
;
2961 * Check and set flags if this segment has a left neighbor.
2964 state
|= BMAP_LEFT_VALID
;
2965 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
- 1), &left
);
2966 if (isnullstartblock(left
.br_startblock
))
2967 state
|= BMAP_LEFT_DELAY
;
2971 * Check and set flags if this segment has a current value.
2972 * Not true if we're inserting into the "hole" at eof.
2974 if (bma
->idx
< ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
)) {
2975 state
|= BMAP_RIGHT_VALID
;
2976 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
), &right
);
2977 if (isnullstartblock(right
.br_startblock
))
2978 state
|= BMAP_RIGHT_DELAY
;
2982 * We're inserting a real allocation between "left" and "right".
2983 * Set the contiguity flags. Don't let extents get too large.
2985 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
2986 left
.br_startoff
+ left
.br_blockcount
== new->br_startoff
&&
2987 left
.br_startblock
+ left
.br_blockcount
== new->br_startblock
&&
2988 left
.br_state
== new->br_state
&&
2989 left
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
2990 state
|= BMAP_LEFT_CONTIG
;
2992 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
2993 new->br_startoff
+ new->br_blockcount
== right
.br_startoff
&&
2994 new->br_startblock
+ new->br_blockcount
== right
.br_startblock
&&
2995 new->br_state
== right
.br_state
&&
2996 new->br_blockcount
+ right
.br_blockcount
<= MAXEXTLEN
&&
2997 (!(state
& BMAP_LEFT_CONTIG
) ||
2998 left
.br_blockcount
+ new->br_blockcount
+
2999 right
.br_blockcount
<= MAXEXTLEN
))
3000 state
|= BMAP_RIGHT_CONTIG
;
3004 * Select which case we're in here, and implement it.
3006 switch (state
& (BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
)) {
3007 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
3009 * New allocation is contiguous with real allocations on the
3010 * left and on the right.
3011 * Merge all three into a single extent record.
3014 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
3015 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, bma
->idx
),
3016 left
.br_blockcount
+ new->br_blockcount
+
3017 right
.br_blockcount
);
3018 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
3020 xfs_iext_remove(bma
->ip
, bma
->idx
+ 1, 1, state
);
3022 XFS_IFORK_NEXT_SET(bma
->ip
, whichfork
,
3023 XFS_IFORK_NEXTENTS(bma
->ip
, whichfork
) - 1);
3024 if (bma
->cur
== NULL
) {
3025 rval
= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
3027 rval
= XFS_ILOG_CORE
;
3028 error
= xfs_bmbt_lookup_eq(bma
->cur
, right
.br_startoff
,
3029 right
.br_startblock
, right
.br_blockcount
,
3033 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
3034 error
= xfs_btree_delete(bma
->cur
, &i
);
3037 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
3038 error
= xfs_btree_decrement(bma
->cur
, 0, &i
);
3041 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
3042 error
= xfs_bmbt_update(bma
->cur
, left
.br_startoff
,
3044 left
.br_blockcount
+
3045 new->br_blockcount
+
3046 right
.br_blockcount
,
3053 case BMAP_LEFT_CONTIG
:
3055 * New allocation is contiguous with a real allocation
3057 * Merge the new allocation with the left neighbor.
3060 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
3061 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, bma
->idx
),
3062 left
.br_blockcount
+ new->br_blockcount
);
3063 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
3065 if (bma
->cur
== NULL
) {
3066 rval
= xfs_ilog_fext(whichfork
);
3069 error
= xfs_bmbt_lookup_eq(bma
->cur
, left
.br_startoff
,
3070 left
.br_startblock
, left
.br_blockcount
,
3074 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
3075 error
= xfs_bmbt_update(bma
->cur
, left
.br_startoff
,
3077 left
.br_blockcount
+
3085 case BMAP_RIGHT_CONTIG
:
3087 * New allocation is contiguous with a real allocation
3089 * Merge the new allocation with the right neighbor.
3091 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
3092 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp
, bma
->idx
),
3093 new->br_startoff
, new->br_startblock
,
3094 new->br_blockcount
+ right
.br_blockcount
,
3096 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
3098 if (bma
->cur
== NULL
) {
3099 rval
= xfs_ilog_fext(whichfork
);
3102 error
= xfs_bmbt_lookup_eq(bma
->cur
,
3104 right
.br_startblock
,
3105 right
.br_blockcount
, &i
);
3108 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
3109 error
= xfs_bmbt_update(bma
->cur
, new->br_startoff
,
3111 new->br_blockcount
+
3112 right
.br_blockcount
,
3121 * New allocation is not contiguous with another
3123 * Insert a new entry.
3125 xfs_iext_insert(bma
->ip
, bma
->idx
, 1, new, state
);
3126 XFS_IFORK_NEXT_SET(bma
->ip
, whichfork
,
3127 XFS_IFORK_NEXTENTS(bma
->ip
, whichfork
) + 1);
3128 if (bma
->cur
== NULL
) {
3129 rval
= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
3131 rval
= XFS_ILOG_CORE
;
3132 error
= xfs_bmbt_lookup_eq(bma
->cur
,
3135 new->br_blockcount
, &i
);
3138 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
3139 bma
->cur
->bc_rec
.b
.br_state
= new->br_state
;
3140 error
= xfs_btree_insert(bma
->cur
, &i
);
3143 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
3148 /* convert to a btree if necessary */
3149 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
3150 int tmp_logflags
; /* partial log flag return val */
3152 ASSERT(bma
->cur
== NULL
);
3153 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
3154 bma
->firstblock
, bma
->flist
, &bma
->cur
,
3155 0, &tmp_logflags
, whichfork
);
3156 bma
->logflags
|= tmp_logflags
;
3161 /* clear out the allocated field, done with it now in any case. */
3163 bma
->cur
->bc_private
.b
.allocated
= 0;
3165 xfs_bmap_check_leaf_extents(bma
->cur
, bma
->ip
, whichfork
);
3167 bma
->logflags
|= rval
;
3172 * Functions used in the extent read, allocate and remove paths
3176 * Adjust the size of the new extent based on di_extsize and rt extsize.
3179 xfs_bmap_extsize_align(
3181 xfs_bmbt_irec_t
*gotp
, /* next extent pointer */
3182 xfs_bmbt_irec_t
*prevp
, /* previous extent pointer */
3183 xfs_extlen_t extsz
, /* align to this extent size */
3184 int rt
, /* is this a realtime inode? */
3185 int eof
, /* is extent at end-of-file? */
3186 int delay
, /* creating delalloc extent? */
3187 int convert
, /* overwriting unwritten extent? */
3188 xfs_fileoff_t
*offp
, /* in/out: aligned offset */
3189 xfs_extlen_t
*lenp
) /* in/out: aligned length */
3191 xfs_fileoff_t orig_off
; /* original offset */
3192 xfs_extlen_t orig_alen
; /* original length */
3193 xfs_fileoff_t orig_end
; /* original off+len */
3194 xfs_fileoff_t nexto
; /* next file offset */
3195 xfs_fileoff_t prevo
; /* previous file offset */
3196 xfs_fileoff_t align_off
; /* temp for offset */
3197 xfs_extlen_t align_alen
; /* temp for length */
3198 xfs_extlen_t temp
; /* temp for calculations */
3203 orig_off
= align_off
= *offp
;
3204 orig_alen
= align_alen
= *lenp
;
3205 orig_end
= orig_off
+ orig_alen
;
3208 * If this request overlaps an existing extent, then don't
3209 * attempt to perform any additional alignment.
3211 if (!delay
&& !eof
&&
3212 (orig_off
>= gotp
->br_startoff
) &&
3213 (orig_end
<= gotp
->br_startoff
+ gotp
->br_blockcount
)) {
3218 * If the file offset is unaligned vs. the extent size
3219 * we need to align it. This will be possible unless
3220 * the file was previously written with a kernel that didn't
3221 * perform this alignment, or if a truncate shot us in the
3224 temp
= do_mod(orig_off
, extsz
);
3230 /* Same adjustment for the end of the requested area. */
3231 temp
= (align_alen
% extsz
);
3233 align_alen
+= extsz
- temp
;
3236 * For large extent hint sizes, the aligned extent might be larger than
3237 * MAXEXTLEN. In that case, reduce the size by an extsz so that it pulls
3238 * the length back under MAXEXTLEN. The outer allocation loops handle
3239 * short allocation just fine, so it is safe to do this. We only want to
3240 * do it when we are forced to, though, because it means more allocation
3241 * operations are required.
3243 while (align_alen
> MAXEXTLEN
)
3244 align_alen
-= extsz
;
3245 ASSERT(align_alen
<= MAXEXTLEN
);
3248 * If the previous block overlaps with this proposed allocation
3249 * then move the start forward without adjusting the length.
3251 if (prevp
->br_startoff
!= NULLFILEOFF
) {
3252 if (prevp
->br_startblock
== HOLESTARTBLOCK
)
3253 prevo
= prevp
->br_startoff
;
3255 prevo
= prevp
->br_startoff
+ prevp
->br_blockcount
;
3258 if (align_off
!= orig_off
&& align_off
< prevo
)
3261 * If the next block overlaps with this proposed allocation
3262 * then move the start back without adjusting the length,
3263 * but not before offset 0.
3264 * This may of course make the start overlap previous block,
3265 * and if we hit the offset 0 limit then the next block
3266 * can still overlap too.
3268 if (!eof
&& gotp
->br_startoff
!= NULLFILEOFF
) {
3269 if ((delay
&& gotp
->br_startblock
== HOLESTARTBLOCK
) ||
3270 (!delay
&& gotp
->br_startblock
== DELAYSTARTBLOCK
))
3271 nexto
= gotp
->br_startoff
+ gotp
->br_blockcount
;
3273 nexto
= gotp
->br_startoff
;
3275 nexto
= NULLFILEOFF
;
3277 align_off
+ align_alen
!= orig_end
&&
3278 align_off
+ align_alen
> nexto
)
3279 align_off
= nexto
> align_alen
? nexto
- align_alen
: 0;
3281 * If we're now overlapping the next or previous extent that
3282 * means we can't fit an extsz piece in this hole. Just move
3283 * the start forward to the first valid spot and set
3284 * the length so we hit the end.
3286 if (align_off
!= orig_off
&& align_off
< prevo
)
3288 if (align_off
+ align_alen
!= orig_end
&&
3289 align_off
+ align_alen
> nexto
&&
3290 nexto
!= NULLFILEOFF
) {
3291 ASSERT(nexto
> prevo
);
3292 align_alen
= nexto
- align_off
;
3296 * If realtime, and the result isn't a multiple of the realtime
3297 * extent size we need to remove blocks until it is.
3299 if (rt
&& (temp
= (align_alen
% mp
->m_sb
.sb_rextsize
))) {
3301 * We're not covering the original request, or
3302 * we won't be able to once we fix the length.
3304 if (orig_off
< align_off
||
3305 orig_end
> align_off
+ align_alen
||
3306 align_alen
- temp
< orig_alen
)
3309 * Try to fix it by moving the start up.
3311 if (align_off
+ temp
<= orig_off
) {
3316 * Try to fix it by moving the end in.
3318 else if (align_off
+ align_alen
- temp
>= orig_end
)
3321 * Set the start to the minimum then trim the length.
3324 align_alen
-= orig_off
- align_off
;
3325 align_off
= orig_off
;
3326 align_alen
-= align_alen
% mp
->m_sb
.sb_rextsize
;
3329 * Result doesn't cover the request, fail it.
3331 if (orig_off
< align_off
|| orig_end
> align_off
+ align_alen
)
3334 ASSERT(orig_off
>= align_off
);
3335 /* see MAXEXTLEN handling above */
3336 ASSERT(orig_end
<= align_off
+ align_alen
||
3337 align_alen
+ extsz
> MAXEXTLEN
);
3341 if (!eof
&& gotp
->br_startoff
!= NULLFILEOFF
)
3342 ASSERT(align_off
+ align_alen
<= gotp
->br_startoff
);
3343 if (prevp
->br_startoff
!= NULLFILEOFF
)
3344 ASSERT(align_off
>= prevp
->br_startoff
+ prevp
->br_blockcount
);
3352 #define XFS_ALLOC_GAP_UNITS 4
3356 struct xfs_bmalloca
*ap
) /* bmap alloc argument struct */
3358 xfs_fsblock_t adjust
; /* adjustment to block numbers */
3359 xfs_agnumber_t fb_agno
; /* ag number of ap->firstblock */
3360 xfs_mount_t
*mp
; /* mount point structure */
3361 int nullfb
; /* true if ap->firstblock isn't set */
3362 int rt
; /* true if inode is realtime */
3364 #define ISVALID(x,y) \
3366 (x) < mp->m_sb.sb_rblocks : \
3367 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
3368 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
3369 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
3371 mp
= ap
->ip
->i_mount
;
3372 nullfb
= *ap
->firstblock
== NULLFSBLOCK
;
3373 rt
= XFS_IS_REALTIME_INODE(ap
->ip
) && ap
->userdata
;
3374 fb_agno
= nullfb
? NULLAGNUMBER
: XFS_FSB_TO_AGNO(mp
, *ap
->firstblock
);
3376 * If allocating at eof, and there's a previous real block,
3377 * try to use its last block as our starting point.
3379 if (ap
->eof
&& ap
->prev
.br_startoff
!= NULLFILEOFF
&&
3380 !isnullstartblock(ap
->prev
.br_startblock
) &&
3381 ISVALID(ap
->prev
.br_startblock
+ ap
->prev
.br_blockcount
,
3382 ap
->prev
.br_startblock
)) {
3383 ap
->blkno
= ap
->prev
.br_startblock
+ ap
->prev
.br_blockcount
;
3385 * Adjust for the gap between prevp and us.
3387 adjust
= ap
->offset
-
3388 (ap
->prev
.br_startoff
+ ap
->prev
.br_blockcount
);
3390 ISVALID(ap
->blkno
+ adjust
, ap
->prev
.br_startblock
))
3391 ap
->blkno
+= adjust
;
3394 * If not at eof, then compare the two neighbor blocks.
3395 * Figure out whether either one gives us a good starting point,
3396 * and pick the better one.
3398 else if (!ap
->eof
) {
3399 xfs_fsblock_t gotbno
; /* right side block number */
3400 xfs_fsblock_t gotdiff
=0; /* right side difference */
3401 xfs_fsblock_t prevbno
; /* left side block number */
3402 xfs_fsblock_t prevdiff
=0; /* left side difference */
3405 * If there's a previous (left) block, select a requested
3406 * start block based on it.
3408 if (ap
->prev
.br_startoff
!= NULLFILEOFF
&&
3409 !isnullstartblock(ap
->prev
.br_startblock
) &&
3410 (prevbno
= ap
->prev
.br_startblock
+
3411 ap
->prev
.br_blockcount
) &&
3412 ISVALID(prevbno
, ap
->prev
.br_startblock
)) {
3414 * Calculate gap to end of previous block.
3416 adjust
= prevdiff
= ap
->offset
-
3417 (ap
->prev
.br_startoff
+
3418 ap
->prev
.br_blockcount
);
3420 * Figure the startblock based on the previous block's
3421 * end and the gap size.
3423 * If the gap is large relative to the piece we're
3424 * allocating, or using it gives us an invalid block
3425 * number, then just use the end of the previous block.
3427 if (prevdiff
<= XFS_ALLOC_GAP_UNITS
* ap
->length
&&
3428 ISVALID(prevbno
+ prevdiff
,
3429 ap
->prev
.br_startblock
))
3434 * If the firstblock forbids it, can't use it,
3437 if (!rt
&& !nullfb
&&
3438 XFS_FSB_TO_AGNO(mp
, prevbno
) != fb_agno
)
3439 prevbno
= NULLFSBLOCK
;
3442 * No previous block or can't follow it, just default.
3445 prevbno
= NULLFSBLOCK
;
3447 * If there's a following (right) block, select a requested
3448 * start block based on it.
3450 if (!isnullstartblock(ap
->got
.br_startblock
)) {
3452 * Calculate gap to start of next block.
3454 adjust
= gotdiff
= ap
->got
.br_startoff
- ap
->offset
;
3456 * Figure the startblock based on the next block's
3457 * start and the gap size.
3459 gotbno
= ap
->got
.br_startblock
;
3462 * If the gap is large relative to the piece we're
3463 * allocating, or using it gives us an invalid block
3464 * number, then just use the start of the next block
3465 * offset by our length.
3467 if (gotdiff
<= XFS_ALLOC_GAP_UNITS
* ap
->length
&&
3468 ISVALID(gotbno
- gotdiff
, gotbno
))
3470 else if (ISVALID(gotbno
- ap
->length
, gotbno
)) {
3471 gotbno
-= ap
->length
;
3472 gotdiff
+= adjust
- ap
->length
;
3476 * If the firstblock forbids it, can't use it,
3479 if (!rt
&& !nullfb
&&
3480 XFS_FSB_TO_AGNO(mp
, gotbno
) != fb_agno
)
3481 gotbno
= NULLFSBLOCK
;
3484 * No next block, just default.
3487 gotbno
= NULLFSBLOCK
;
3489 * If both valid, pick the better one, else the only good
3490 * one, else ap->blkno is already set (to 0 or the inode block).
3492 if (prevbno
!= NULLFSBLOCK
&& gotbno
!= NULLFSBLOCK
)
3493 ap
->blkno
= prevdiff
<= gotdiff
? prevbno
: gotbno
;
3494 else if (prevbno
!= NULLFSBLOCK
)
3495 ap
->blkno
= prevbno
;
3496 else if (gotbno
!= NULLFSBLOCK
)
3503 xfs_bmap_longest_free_extent(
3504 struct xfs_trans
*tp
,
3509 struct xfs_mount
*mp
= tp
->t_mountp
;
3510 struct xfs_perag
*pag
;
3511 xfs_extlen_t longest
;
3514 pag
= xfs_perag_get(mp
, ag
);
3515 if (!pag
->pagf_init
) {
3516 error
= xfs_alloc_pagf_init(mp
, tp
, ag
, XFS_ALLOC_FLAG_TRYLOCK
);
3520 if (!pag
->pagf_init
) {
3526 longest
= xfs_alloc_longest_free_extent(mp
, pag
,
3527 xfs_alloc_min_freelist(mp
, pag
));
3528 if (*blen
< longest
)
3537 xfs_bmap_select_minlen(
3538 struct xfs_bmalloca
*ap
,
3539 struct xfs_alloc_arg
*args
,
3543 if (notinit
|| *blen
< ap
->minlen
) {
3545 * Since we did a BUF_TRYLOCK above, it is possible that
3546 * there is space for this request.
3548 args
->minlen
= ap
->minlen
;
3549 } else if (*blen
< args
->maxlen
) {
3551 * If the best seen length is less than the request length,
3552 * use the best as the minimum.
3554 args
->minlen
= *blen
;
3557 * Otherwise we've seen an extent as big as maxlen, use that
3560 args
->minlen
= args
->maxlen
;
3565 xfs_bmap_btalloc_nullfb(
3566 struct xfs_bmalloca
*ap
,
3567 struct xfs_alloc_arg
*args
,
3570 struct xfs_mount
*mp
= ap
->ip
->i_mount
;
3571 xfs_agnumber_t ag
, startag
;
3575 args
->type
= XFS_ALLOCTYPE_START_BNO
;
3576 args
->total
= ap
->total
;
3578 startag
= ag
= XFS_FSB_TO_AGNO(mp
, args
->fsbno
);
3579 if (startag
== NULLAGNUMBER
)
3582 while (*blen
< args
->maxlen
) {
3583 error
= xfs_bmap_longest_free_extent(args
->tp
, ag
, blen
,
3588 if (++ag
== mp
->m_sb
.sb_agcount
)
3594 xfs_bmap_select_minlen(ap
, args
, blen
, notinit
);
3599 xfs_bmap_btalloc_filestreams(
3600 struct xfs_bmalloca
*ap
,
3601 struct xfs_alloc_arg
*args
,
3604 struct xfs_mount
*mp
= ap
->ip
->i_mount
;
3609 args
->type
= XFS_ALLOCTYPE_NEAR_BNO
;
3610 args
->total
= ap
->total
;
3612 ag
= XFS_FSB_TO_AGNO(mp
, args
->fsbno
);
3613 if (ag
== NULLAGNUMBER
)
3616 error
= xfs_bmap_longest_free_extent(args
->tp
, ag
, blen
, ¬init
);
3620 if (*blen
< args
->maxlen
) {
3621 error
= xfs_filestream_new_ag(ap
, &ag
);
3625 error
= xfs_bmap_longest_free_extent(args
->tp
, ag
, blen
,
3632 xfs_bmap_select_minlen(ap
, args
, blen
, notinit
);
3635 * Set the failure fallback case to look in the selected AG as stream
3638 ap
->blkno
= args
->fsbno
= XFS_AGB_TO_FSB(mp
, ag
, 0);
3644 struct xfs_bmalloca
*ap
) /* bmap alloc argument struct */
3646 xfs_mount_t
*mp
; /* mount point structure */
3647 xfs_alloctype_t atype
= 0; /* type for allocation routines */
3648 xfs_extlen_t align
; /* minimum allocation alignment */
3649 xfs_agnumber_t fb_agno
; /* ag number of ap->firstblock */
3651 xfs_alloc_arg_t args
;
3653 xfs_extlen_t nextminlen
= 0;
3654 int nullfb
; /* true if ap->firstblock isn't set */
3662 mp
= ap
->ip
->i_mount
;
3664 /* stripe alignment for allocation is determined by mount parameters */
3666 if (mp
->m_swidth
&& (mp
->m_flags
& XFS_MOUNT_SWALLOC
))
3667 stripe_align
= mp
->m_swidth
;
3668 else if (mp
->m_dalign
)
3669 stripe_align
= mp
->m_dalign
;
3671 align
= ap
->userdata
? xfs_get_extsz_hint(ap
->ip
) : 0;
3672 if (unlikely(align
)) {
3673 error
= xfs_bmap_extsize_align(mp
, &ap
->got
, &ap
->prev
,
3674 align
, 0, ap
->eof
, 0, ap
->conv
,
3675 &ap
->offset
, &ap
->length
);
3681 nullfb
= *ap
->firstblock
== NULLFSBLOCK
;
3682 fb_agno
= nullfb
? NULLAGNUMBER
: XFS_FSB_TO_AGNO(mp
, *ap
->firstblock
);
3684 if (ap
->userdata
&& xfs_inode_is_filestream(ap
->ip
)) {
3685 ag
= xfs_filestream_lookup_ag(ap
->ip
);
3686 ag
= (ag
!= NULLAGNUMBER
) ? ag
: 0;
3687 ap
->blkno
= XFS_AGB_TO_FSB(mp
, ag
, 0);
3689 ap
->blkno
= XFS_INO_TO_FSB(mp
, ap
->ip
->i_ino
);
3692 ap
->blkno
= *ap
->firstblock
;
3694 xfs_bmap_adjacent(ap
);
3697 * If allowed, use ap->blkno; otherwise must use firstblock since
3698 * it's in the right allocation group.
3700 if (nullfb
|| XFS_FSB_TO_AGNO(mp
, ap
->blkno
) == fb_agno
)
3703 ap
->blkno
= *ap
->firstblock
;
3705 * Normal allocation, done through xfs_alloc_vextent.
3707 tryagain
= isaligned
= 0;
3708 memset(&args
, 0, sizeof(args
));
3711 args
.fsbno
= ap
->blkno
;
3713 /* Trim the allocation back to the maximum an AG can fit. */
3714 args
.maxlen
= MIN(ap
->length
, XFS_ALLOC_AG_MAX_USABLE(mp
));
3715 args
.firstblock
= *ap
->firstblock
;
3719 * Search for an allocation group with a single extent large
3720 * enough for the request. If one isn't found, then adjust
3721 * the minimum allocation size to the largest space found.
3723 if (ap
->userdata
&& xfs_inode_is_filestream(ap
->ip
))
3724 error
= xfs_bmap_btalloc_filestreams(ap
, &args
, &blen
);
3726 error
= xfs_bmap_btalloc_nullfb(ap
, &args
, &blen
);
3729 } else if (ap
->flist
->xbf_low
) {
3730 if (xfs_inode_is_filestream(ap
->ip
))
3731 args
.type
= XFS_ALLOCTYPE_FIRST_AG
;
3733 args
.type
= XFS_ALLOCTYPE_START_BNO
;
3734 args
.total
= args
.minlen
= ap
->minlen
;
3736 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
3737 args
.total
= ap
->total
;
3738 args
.minlen
= ap
->minlen
;
3740 /* apply extent size hints if obtained earlier */
3741 if (unlikely(align
)) {
3743 if ((args
.mod
= (xfs_extlen_t
)do_mod(ap
->offset
, args
.prod
)))
3744 args
.mod
= (xfs_extlen_t
)(args
.prod
- args
.mod
);
3745 } else if (mp
->m_sb
.sb_blocksize
>= PAGE_CACHE_SIZE
) {
3749 args
.prod
= PAGE_CACHE_SIZE
>> mp
->m_sb
.sb_blocklog
;
3750 if ((args
.mod
= (xfs_extlen_t
)(do_mod(ap
->offset
, args
.prod
))))
3751 args
.mod
= (xfs_extlen_t
)(args
.prod
- args
.mod
);
3754 * If we are not low on available data blocks, and the
3755 * underlying logical volume manager is a stripe, and
3756 * the file offset is zero then try to allocate data
3757 * blocks on stripe unit boundary.
3758 * NOTE: ap->aeof is only set if the allocation length
3759 * is >= the stripe unit and the allocation offset is
3760 * at the end of file.
3762 if (!ap
->flist
->xbf_low
&& ap
->aeof
) {
3764 args
.alignment
= stripe_align
;
3768 * Adjust for alignment
3770 if (blen
> args
.alignment
&& blen
<= args
.maxlen
)
3771 args
.minlen
= blen
- args
.alignment
;
3772 args
.minalignslop
= 0;
3775 * First try an exact bno allocation.
3776 * If it fails then do a near or start bno
3777 * allocation with alignment turned on.
3781 args
.type
= XFS_ALLOCTYPE_THIS_BNO
;
3784 * Compute the minlen+alignment for the
3785 * next case. Set slop so that the value
3786 * of minlen+alignment+slop doesn't go up
3787 * between the calls.
3789 if (blen
> stripe_align
&& blen
<= args
.maxlen
)
3790 nextminlen
= blen
- stripe_align
;
3792 nextminlen
= args
.minlen
;
3793 if (nextminlen
+ stripe_align
> args
.minlen
+ 1)
3795 nextminlen
+ stripe_align
-
3798 args
.minalignslop
= 0;
3802 args
.minalignslop
= 0;
3804 args
.minleft
= ap
->minleft
;
3805 args
.wasdel
= ap
->wasdel
;
3807 args
.userdata
= ap
->userdata
;
3808 if (ap
->userdata
& XFS_ALLOC_USERDATA_ZERO
)
3811 error
= xfs_alloc_vextent(&args
);
3815 if (tryagain
&& args
.fsbno
== NULLFSBLOCK
) {
3817 * Exact allocation failed. Now try with alignment
3821 args
.fsbno
= ap
->blkno
;
3822 args
.alignment
= stripe_align
;
3823 args
.minlen
= nextminlen
;
3824 args
.minalignslop
= 0;
3826 if ((error
= xfs_alloc_vextent(&args
)))
3829 if (isaligned
&& args
.fsbno
== NULLFSBLOCK
) {
3831 * allocation failed, so turn off alignment and
3835 args
.fsbno
= ap
->blkno
;
3837 if ((error
= xfs_alloc_vextent(&args
)))
3840 if (args
.fsbno
== NULLFSBLOCK
&& nullfb
&&
3841 args
.minlen
> ap
->minlen
) {
3842 args
.minlen
= ap
->minlen
;
3843 args
.type
= XFS_ALLOCTYPE_START_BNO
;
3844 args
.fsbno
= ap
->blkno
;
3845 if ((error
= xfs_alloc_vextent(&args
)))
3848 if (args
.fsbno
== NULLFSBLOCK
&& nullfb
) {
3850 args
.type
= XFS_ALLOCTYPE_FIRST_AG
;
3851 args
.total
= ap
->minlen
;
3853 if ((error
= xfs_alloc_vextent(&args
)))
3855 ap
->flist
->xbf_low
= 1;
3857 if (args
.fsbno
!= NULLFSBLOCK
) {
3859 * check the allocation happened at the same or higher AG than
3860 * the first block that was allocated.
3862 ASSERT(*ap
->firstblock
== NULLFSBLOCK
||
3863 XFS_FSB_TO_AGNO(mp
, *ap
->firstblock
) ==
3864 XFS_FSB_TO_AGNO(mp
, args
.fsbno
) ||
3865 (ap
->flist
->xbf_low
&&
3866 XFS_FSB_TO_AGNO(mp
, *ap
->firstblock
) <
3867 XFS_FSB_TO_AGNO(mp
, args
.fsbno
)));
3869 ap
->blkno
= args
.fsbno
;
3870 if (*ap
->firstblock
== NULLFSBLOCK
)
3871 *ap
->firstblock
= args
.fsbno
;
3872 ASSERT(nullfb
|| fb_agno
== args
.agno
||
3873 (ap
->flist
->xbf_low
&& fb_agno
< args
.agno
));
3874 ap
->length
= args
.len
;
3875 ap
->ip
->i_d
.di_nblocks
+= args
.len
;
3876 xfs_trans_log_inode(ap
->tp
, ap
->ip
, XFS_ILOG_CORE
);
3878 ap
->ip
->i_delayed_blks
-= args
.len
;
3880 * Adjust the disk quota also. This was reserved
3883 xfs_trans_mod_dquot_byino(ap
->tp
, ap
->ip
,
3884 ap
->wasdel
? XFS_TRANS_DQ_DELBCOUNT
:
3885 XFS_TRANS_DQ_BCOUNT
,
3888 ap
->blkno
= NULLFSBLOCK
;
3895 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
3896 * It figures out where to ask the underlying allocator to put the new extent.
3900 struct xfs_bmalloca
*ap
) /* bmap alloc argument struct */
3902 if (XFS_IS_REALTIME_INODE(ap
->ip
) && ap
->userdata
)
3903 return xfs_bmap_rtalloc(ap
);
3904 return xfs_bmap_btalloc(ap
);
3908 * Trim the returned map to the required bounds
3912 struct xfs_bmbt_irec
*mval
,
3913 struct xfs_bmbt_irec
*got
,
3921 if ((flags
& XFS_BMAPI_ENTIRE
) ||
3922 got
->br_startoff
+ got
->br_blockcount
<= obno
) {
3924 if (isnullstartblock(got
->br_startblock
))
3925 mval
->br_startblock
= DELAYSTARTBLOCK
;
3931 ASSERT((*bno
>= obno
) || (n
== 0));
3933 mval
->br_startoff
= *bno
;
3934 if (isnullstartblock(got
->br_startblock
))
3935 mval
->br_startblock
= DELAYSTARTBLOCK
;
3937 mval
->br_startblock
= got
->br_startblock
+
3938 (*bno
- got
->br_startoff
);
3940 * Return the minimum of what we got and what we asked for for
3941 * the length. We can use the len variable here because it is
3942 * modified below and we could have been there before coming
3943 * here if the first part of the allocation didn't overlap what
3946 mval
->br_blockcount
= XFS_FILBLKS_MIN(end
- *bno
,
3947 got
->br_blockcount
- (*bno
- got
->br_startoff
));
3948 mval
->br_state
= got
->br_state
;
3949 ASSERT(mval
->br_blockcount
<= len
);
3954 * Update and validate the extent map to return
3957 xfs_bmapi_update_map(
3958 struct xfs_bmbt_irec
**map
,
3966 xfs_bmbt_irec_t
*mval
= *map
;
3968 ASSERT((flags
& XFS_BMAPI_ENTIRE
) ||
3969 ((mval
->br_startoff
+ mval
->br_blockcount
) <= end
));
3970 ASSERT((flags
& XFS_BMAPI_ENTIRE
) || (mval
->br_blockcount
<= *len
) ||
3971 (mval
->br_startoff
< obno
));
3973 *bno
= mval
->br_startoff
+ mval
->br_blockcount
;
3975 if (*n
> 0 && mval
->br_startoff
== mval
[-1].br_startoff
) {
3976 /* update previous map with new information */
3977 ASSERT(mval
->br_startblock
== mval
[-1].br_startblock
);
3978 ASSERT(mval
->br_blockcount
> mval
[-1].br_blockcount
);
3979 ASSERT(mval
->br_state
== mval
[-1].br_state
);
3980 mval
[-1].br_blockcount
= mval
->br_blockcount
;
3981 mval
[-1].br_state
= mval
->br_state
;
3982 } else if (*n
> 0 && mval
->br_startblock
!= DELAYSTARTBLOCK
&&
3983 mval
[-1].br_startblock
!= DELAYSTARTBLOCK
&&
3984 mval
[-1].br_startblock
!= HOLESTARTBLOCK
&&
3985 mval
->br_startblock
== mval
[-1].br_startblock
+
3986 mval
[-1].br_blockcount
&&
3987 ((flags
& XFS_BMAPI_IGSTATE
) ||
3988 mval
[-1].br_state
== mval
->br_state
)) {
3989 ASSERT(mval
->br_startoff
==
3990 mval
[-1].br_startoff
+ mval
[-1].br_blockcount
);
3991 mval
[-1].br_blockcount
+= mval
->br_blockcount
;
3992 } else if (*n
> 0 &&
3993 mval
->br_startblock
== DELAYSTARTBLOCK
&&
3994 mval
[-1].br_startblock
== DELAYSTARTBLOCK
&&
3995 mval
->br_startoff
==
3996 mval
[-1].br_startoff
+ mval
[-1].br_blockcount
) {
3997 mval
[-1].br_blockcount
+= mval
->br_blockcount
;
3998 mval
[-1].br_state
= mval
->br_state
;
3999 } else if (!((*n
== 0) &&
4000 ((mval
->br_startoff
+ mval
->br_blockcount
) <=
4009 * Map file blocks to filesystem blocks without allocation.
4013 struct xfs_inode
*ip
,
4016 struct xfs_bmbt_irec
*mval
,
4020 struct xfs_mount
*mp
= ip
->i_mount
;
4021 struct xfs_ifork
*ifp
;
4022 struct xfs_bmbt_irec got
;
4023 struct xfs_bmbt_irec prev
;
4030 int whichfork
= (flags
& XFS_BMAPI_ATTRFORK
) ?
4031 XFS_ATTR_FORK
: XFS_DATA_FORK
;
4034 ASSERT(!(flags
& ~(XFS_BMAPI_ATTRFORK
|XFS_BMAPI_ENTIRE
|
4035 XFS_BMAPI_IGSTATE
)));
4036 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_SHARED
|XFS_ILOCK_EXCL
));
4038 if (unlikely(XFS_TEST_ERROR(
4039 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
4040 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
4041 mp
, XFS_ERRTAG_BMAPIFORMAT
, XFS_RANDOM_BMAPIFORMAT
))) {
4042 XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW
, mp
);
4043 return -EFSCORRUPTED
;
4046 if (XFS_FORCED_SHUTDOWN(mp
))
4049 XFS_STATS_INC(mp
, xs_blk_mapr
);
4051 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4053 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
4054 error
= xfs_iread_extents(NULL
, ip
, whichfork
);
4059 xfs_bmap_search_extents(ip
, bno
, whichfork
, &eof
, &lastx
, &got
, &prev
);
4063 while (bno
< end
&& n
< *nmap
) {
4064 /* Reading past eof, act as though there's a hole up to end. */
4066 got
.br_startoff
= end
;
4067 if (got
.br_startoff
> bno
) {
4068 /* Reading in a hole. */
4069 mval
->br_startoff
= bno
;
4070 mval
->br_startblock
= HOLESTARTBLOCK
;
4071 mval
->br_blockcount
=
4072 XFS_FILBLKS_MIN(len
, got
.br_startoff
- bno
);
4073 mval
->br_state
= XFS_EXT_NORM
;
4074 bno
+= mval
->br_blockcount
;
4075 len
-= mval
->br_blockcount
;
4081 /* set up the extent map to return. */
4082 xfs_bmapi_trim_map(mval
, &got
, &bno
, len
, obno
, end
, n
, flags
);
4083 xfs_bmapi_update_map(&mval
, &bno
, &len
, obno
, end
, &n
, flags
);
4085 /* If we're done, stop now. */
4086 if (bno
>= end
|| n
>= *nmap
)
4089 /* Else go on to the next record. */
4090 if (++lastx
< ifp
->if_bytes
/ sizeof(xfs_bmbt_rec_t
))
4091 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, lastx
), &got
);
4100 xfs_bmapi_reserve_delalloc(
4101 struct xfs_inode
*ip
,
4104 struct xfs_bmbt_irec
*got
,
4105 struct xfs_bmbt_irec
*prev
,
4106 xfs_extnum_t
*lastx
,
4109 struct xfs_mount
*mp
= ip
->i_mount
;
4110 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, XFS_DATA_FORK
);
4112 xfs_extlen_t indlen
;
4113 char rt
= XFS_IS_REALTIME_INODE(ip
);
4117 alen
= XFS_FILBLKS_MIN(len
, MAXEXTLEN
);
4119 alen
= XFS_FILBLKS_MIN(alen
, got
->br_startoff
- aoff
);
4121 /* Figure out the extent size, adjust alen */
4122 extsz
= xfs_get_extsz_hint(ip
);
4124 error
= xfs_bmap_extsize_align(mp
, got
, prev
, extsz
, rt
, eof
,
4125 1, 0, &aoff
, &alen
);
4130 extsz
= alen
/ mp
->m_sb
.sb_rextsize
;
4133 * Make a transaction-less quota reservation for delayed allocation
4134 * blocks. This number gets adjusted later. We return if we haven't
4135 * allocated blocks already inside this loop.
4137 error
= xfs_trans_reserve_quota_nblks(NULL
, ip
, (long)alen
, 0,
4138 rt
? XFS_QMOPT_RES_RTBLKS
: XFS_QMOPT_RES_REGBLKS
);
4143 * Split changing sb for alen and indlen since they could be coming
4144 * from different places.
4146 indlen
= (xfs_extlen_t
)xfs_bmap_worst_indlen(ip
, alen
);
4150 error
= xfs_mod_frextents(mp
, -((int64_t)extsz
));
4152 error
= xfs_mod_fdblocks(mp
, -((int64_t)alen
), false);
4156 goto out_unreserve_quota
;
4158 error
= xfs_mod_fdblocks(mp
, -((int64_t)indlen
), false);
4160 goto out_unreserve_blocks
;
4163 ip
->i_delayed_blks
+= alen
;
4165 got
->br_startoff
= aoff
;
4166 got
->br_startblock
= nullstartblock(indlen
);
4167 got
->br_blockcount
= alen
;
4168 got
->br_state
= XFS_EXT_NORM
;
4169 xfs_bmap_add_extent_hole_delay(ip
, lastx
, got
);
4172 * Update our extent pointer, given that xfs_bmap_add_extent_hole_delay
4173 * might have merged it into one of the neighbouring ones.
4175 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, *lastx
), got
);
4177 ASSERT(got
->br_startoff
<= aoff
);
4178 ASSERT(got
->br_startoff
+ got
->br_blockcount
>= aoff
+ alen
);
4179 ASSERT(isnullstartblock(got
->br_startblock
));
4180 ASSERT(got
->br_state
== XFS_EXT_NORM
);
4183 out_unreserve_blocks
:
4185 xfs_mod_frextents(mp
, extsz
);
4187 xfs_mod_fdblocks(mp
, alen
, false);
4188 out_unreserve_quota
:
4189 if (XFS_IS_QUOTA_ON(mp
))
4190 xfs_trans_unreserve_quota_nblks(NULL
, ip
, (long)alen
, 0, rt
?
4191 XFS_QMOPT_RES_RTBLKS
: XFS_QMOPT_RES_REGBLKS
);
4196 * Map file blocks to filesystem blocks, adding delayed allocations as needed.
4200 struct xfs_inode
*ip
, /* incore inode */
4201 xfs_fileoff_t bno
, /* starting file offs. mapped */
4202 xfs_filblks_t len
, /* length to map in file */
4203 struct xfs_bmbt_irec
*mval
, /* output: map values */
4204 int *nmap
, /* i/o: mval size/count */
4205 int flags
) /* XFS_BMAPI_... */
4207 struct xfs_mount
*mp
= ip
->i_mount
;
4208 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, XFS_DATA_FORK
);
4209 struct xfs_bmbt_irec got
; /* current file extent record */
4210 struct xfs_bmbt_irec prev
; /* previous file extent record */
4211 xfs_fileoff_t obno
; /* old block number (offset) */
4212 xfs_fileoff_t end
; /* end of mapped file region */
4213 xfs_extnum_t lastx
; /* last useful extent number */
4214 int eof
; /* we've hit the end of extents */
4215 int n
= 0; /* current extent index */
4219 ASSERT(*nmap
<= XFS_BMAP_MAX_NMAP
);
4220 ASSERT(!(flags
& ~XFS_BMAPI_ENTIRE
));
4221 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
4223 if (unlikely(XFS_TEST_ERROR(
4224 (XFS_IFORK_FORMAT(ip
, XFS_DATA_FORK
) != XFS_DINODE_FMT_EXTENTS
&&
4225 XFS_IFORK_FORMAT(ip
, XFS_DATA_FORK
) != XFS_DINODE_FMT_BTREE
),
4226 mp
, XFS_ERRTAG_BMAPIFORMAT
, XFS_RANDOM_BMAPIFORMAT
))) {
4227 XFS_ERROR_REPORT("xfs_bmapi_delay", XFS_ERRLEVEL_LOW
, mp
);
4228 return -EFSCORRUPTED
;
4231 if (XFS_FORCED_SHUTDOWN(mp
))
4234 XFS_STATS_INC(mp
, xs_blk_mapw
);
4236 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
4237 error
= xfs_iread_extents(NULL
, ip
, XFS_DATA_FORK
);
4242 xfs_bmap_search_extents(ip
, bno
, XFS_DATA_FORK
, &eof
, &lastx
, &got
, &prev
);
4246 while (bno
< end
&& n
< *nmap
) {
4247 if (eof
|| got
.br_startoff
> bno
) {
4248 error
= xfs_bmapi_reserve_delalloc(ip
, bno
, len
, &got
,
4249 &prev
, &lastx
, eof
);
4259 /* set up the extent map to return. */
4260 xfs_bmapi_trim_map(mval
, &got
, &bno
, len
, obno
, end
, n
, flags
);
4261 xfs_bmapi_update_map(&mval
, &bno
, &len
, obno
, end
, &n
, flags
);
4263 /* If we're done, stop now. */
4264 if (bno
>= end
|| n
>= *nmap
)
4267 /* Else go on to the next record. */
4269 if (++lastx
< ifp
->if_bytes
/ sizeof(xfs_bmbt_rec_t
))
4270 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, lastx
), &got
);
4282 struct xfs_bmalloca
*bma
)
4284 struct xfs_mount
*mp
= bma
->ip
->i_mount
;
4285 int whichfork
= (bma
->flags
& XFS_BMAPI_ATTRFORK
) ?
4286 XFS_ATTR_FORK
: XFS_DATA_FORK
;
4287 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
4288 int tmp_logflags
= 0;
4291 ASSERT(bma
->length
> 0);
4294 * For the wasdelay case, we could also just allocate the stuff asked
4295 * for in this bmap call but that wouldn't be as good.
4298 bma
->length
= (xfs_extlen_t
)bma
->got
.br_blockcount
;
4299 bma
->offset
= bma
->got
.br_startoff
;
4300 if (bma
->idx
!= NULLEXTNUM
&& bma
->idx
) {
4301 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
- 1),
4305 bma
->length
= XFS_FILBLKS_MIN(bma
->length
, MAXEXTLEN
);
4307 bma
->length
= XFS_FILBLKS_MIN(bma
->length
,
4308 bma
->got
.br_startoff
- bma
->offset
);
4312 * Indicate if this is the first user data in the file, or just any
4313 * user data. And if it is userdata, indicate whether it needs to
4314 * be initialised to zero during allocation.
4316 if (!(bma
->flags
& XFS_BMAPI_METADATA
)) {
4317 bma
->userdata
= (bma
->offset
== 0) ?
4318 XFS_ALLOC_INITIAL_USER_DATA
: XFS_ALLOC_USERDATA
;
4319 if (bma
->flags
& XFS_BMAPI_ZERO
)
4320 bma
->userdata
|= XFS_ALLOC_USERDATA_ZERO
;
4323 bma
->minlen
= (bma
->flags
& XFS_BMAPI_CONTIG
) ? bma
->length
: 1;
4326 * Only want to do the alignment at the eof if it is userdata and
4327 * allocation length is larger than a stripe unit.
4329 if (mp
->m_dalign
&& bma
->length
>= mp
->m_dalign
&&
4330 !(bma
->flags
& XFS_BMAPI_METADATA
) && whichfork
== XFS_DATA_FORK
) {
4331 error
= xfs_bmap_isaeof(bma
, whichfork
);
4336 error
= xfs_bmap_alloc(bma
);
4340 if (bma
->flist
->xbf_low
)
4343 bma
->cur
->bc_private
.b
.firstblock
= *bma
->firstblock
;
4344 if (bma
->blkno
== NULLFSBLOCK
)
4346 if ((ifp
->if_flags
& XFS_IFBROOT
) && !bma
->cur
) {
4347 bma
->cur
= xfs_bmbt_init_cursor(mp
, bma
->tp
, bma
->ip
, whichfork
);
4348 bma
->cur
->bc_private
.b
.firstblock
= *bma
->firstblock
;
4349 bma
->cur
->bc_private
.b
.flist
= bma
->flist
;
4352 * Bump the number of extents we've allocated
4358 bma
->cur
->bc_private
.b
.flags
=
4359 bma
->wasdel
? XFS_BTCUR_BPRV_WASDEL
: 0;
4361 bma
->got
.br_startoff
= bma
->offset
;
4362 bma
->got
.br_startblock
= bma
->blkno
;
4363 bma
->got
.br_blockcount
= bma
->length
;
4364 bma
->got
.br_state
= XFS_EXT_NORM
;
4367 * A wasdelay extent has been initialized, so shouldn't be flagged
4370 if (!bma
->wasdel
&& (bma
->flags
& XFS_BMAPI_PREALLOC
) &&
4371 xfs_sb_version_hasextflgbit(&mp
->m_sb
))
4372 bma
->got
.br_state
= XFS_EXT_UNWRITTEN
;
4375 error
= xfs_bmap_add_extent_delay_real(bma
);
4377 error
= xfs_bmap_add_extent_hole_real(bma
, whichfork
);
4379 bma
->logflags
|= tmp_logflags
;
4384 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4385 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4386 * the neighbouring ones.
4388 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
), &bma
->got
);
4390 ASSERT(bma
->got
.br_startoff
<= bma
->offset
);
4391 ASSERT(bma
->got
.br_startoff
+ bma
->got
.br_blockcount
>=
4392 bma
->offset
+ bma
->length
);
4393 ASSERT(bma
->got
.br_state
== XFS_EXT_NORM
||
4394 bma
->got
.br_state
== XFS_EXT_UNWRITTEN
);
4399 xfs_bmapi_convert_unwritten(
4400 struct xfs_bmalloca
*bma
,
4401 struct xfs_bmbt_irec
*mval
,
4405 int whichfork
= (flags
& XFS_BMAPI_ATTRFORK
) ?
4406 XFS_ATTR_FORK
: XFS_DATA_FORK
;
4407 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
4408 int tmp_logflags
= 0;
4411 /* check if we need to do unwritten->real conversion */
4412 if (mval
->br_state
== XFS_EXT_UNWRITTEN
&&
4413 (flags
& XFS_BMAPI_PREALLOC
))
4416 /* check if we need to do real->unwritten conversion */
4417 if (mval
->br_state
== XFS_EXT_NORM
&&
4418 (flags
& (XFS_BMAPI_PREALLOC
| XFS_BMAPI_CONVERT
)) !=
4419 (XFS_BMAPI_PREALLOC
| XFS_BMAPI_CONVERT
))
4423 * Modify (by adding) the state flag, if writing.
4425 ASSERT(mval
->br_blockcount
<= len
);
4426 if ((ifp
->if_flags
& XFS_IFBROOT
) && !bma
->cur
) {
4427 bma
->cur
= xfs_bmbt_init_cursor(bma
->ip
->i_mount
, bma
->tp
,
4428 bma
->ip
, whichfork
);
4429 bma
->cur
->bc_private
.b
.firstblock
= *bma
->firstblock
;
4430 bma
->cur
->bc_private
.b
.flist
= bma
->flist
;
4432 mval
->br_state
= (mval
->br_state
== XFS_EXT_UNWRITTEN
)
4433 ? XFS_EXT_NORM
: XFS_EXT_UNWRITTEN
;
4436 * Before insertion into the bmbt, zero the range being converted
4439 if (flags
& XFS_BMAPI_ZERO
) {
4440 error
= xfs_zero_extent(bma
->ip
, mval
->br_startblock
,
4441 mval
->br_blockcount
);
4446 error
= xfs_bmap_add_extent_unwritten_real(bma
->tp
, bma
->ip
, &bma
->idx
,
4447 &bma
->cur
, mval
, bma
->firstblock
, bma
->flist
,
4450 * Log the inode core unconditionally in the unwritten extent conversion
4451 * path because the conversion might not have done so (e.g., if the
4452 * extent count hasn't changed). We need to make sure the inode is dirty
4453 * in the transaction for the sake of fsync(), even if nothing has
4454 * changed, because fsync() will not force the log for this transaction
4455 * unless it sees the inode pinned.
4457 bma
->logflags
|= tmp_logflags
| XFS_ILOG_CORE
;
4462 * Update our extent pointer, given that
4463 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4464 * of the neighbouring ones.
4466 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
), &bma
->got
);
4469 * We may have combined previously unwritten space with written space,
4470 * so generate another request.
4472 if (mval
->br_blockcount
< len
)
4478 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4479 * extent state if necessary. Details behaviour is controlled by the flags
4480 * parameter. Only allocates blocks from a single allocation group, to avoid
4483 * The returned value in "firstblock" from the first call in a transaction
4484 * must be remembered and presented to subsequent calls in "firstblock".
4485 * An upper bound for the number of blocks to be allocated is supplied to
4486 * the first call in "total"; if no allocation group has that many free
4487 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4491 struct xfs_trans
*tp
, /* transaction pointer */
4492 struct xfs_inode
*ip
, /* incore inode */
4493 xfs_fileoff_t bno
, /* starting file offs. mapped */
4494 xfs_filblks_t len
, /* length to map in file */
4495 int flags
, /* XFS_BMAPI_... */
4496 xfs_fsblock_t
*firstblock
, /* first allocated block
4497 controls a.g. for allocs */
4498 xfs_extlen_t total
, /* total blocks needed */
4499 struct xfs_bmbt_irec
*mval
, /* output: map values */
4500 int *nmap
, /* i/o: mval size/count */
4501 struct xfs_bmap_free
*flist
) /* i/o: list extents to free */
4503 struct xfs_mount
*mp
= ip
->i_mount
;
4504 struct xfs_ifork
*ifp
;
4505 struct xfs_bmalloca bma
= { NULL
}; /* args for xfs_bmap_alloc */
4506 xfs_fileoff_t end
; /* end of mapped file region */
4507 int eof
; /* after the end of extents */
4508 int error
; /* error return */
4509 int n
; /* current extent index */
4510 xfs_fileoff_t obno
; /* old block number (offset) */
4511 int whichfork
; /* data or attr fork */
4512 char inhole
; /* current location is hole in file */
4513 char wasdelay
; /* old extent was delayed */
4516 xfs_fileoff_t orig_bno
; /* original block number value */
4517 int orig_flags
; /* original flags arg value */
4518 xfs_filblks_t orig_len
; /* original value of len arg */
4519 struct xfs_bmbt_irec
*orig_mval
; /* original value of mval */
4520 int orig_nmap
; /* original value of *nmap */
4528 whichfork
= (flags
& XFS_BMAPI_ATTRFORK
) ?
4529 XFS_ATTR_FORK
: XFS_DATA_FORK
;
4532 ASSERT(*nmap
<= XFS_BMAP_MAX_NMAP
);
4533 ASSERT(!(flags
& XFS_BMAPI_IGSTATE
));
4536 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_LOCAL
);
4537 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
4539 /* zeroing is for currently only for data extents, not metadata */
4540 ASSERT((flags
& (XFS_BMAPI_METADATA
| XFS_BMAPI_ZERO
)) !=
4541 (XFS_BMAPI_METADATA
| XFS_BMAPI_ZERO
));
4543 * we can allocate unwritten extents or pre-zero allocated blocks,
4544 * but it makes no sense to do both at once. This would result in
4545 * zeroing the unwritten extent twice, but it still being an
4546 * unwritten extent....
4548 ASSERT((flags
& (XFS_BMAPI_PREALLOC
| XFS_BMAPI_ZERO
)) !=
4549 (XFS_BMAPI_PREALLOC
| XFS_BMAPI_ZERO
));
4551 if (unlikely(XFS_TEST_ERROR(
4552 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
4553 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
4554 mp
, XFS_ERRTAG_BMAPIFORMAT
, XFS_RANDOM_BMAPIFORMAT
))) {
4555 XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW
, mp
);
4556 return -EFSCORRUPTED
;
4559 if (XFS_FORCED_SHUTDOWN(mp
))
4562 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4564 XFS_STATS_INC(mp
, xs_blk_mapw
);
4566 if (*firstblock
== NULLFSBLOCK
) {
4567 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
)
4568 bma
.minleft
= be16_to_cpu(ifp
->if_broot
->bb_level
) + 1;
4575 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
4576 error
= xfs_iread_extents(tp
, ip
, whichfork
);
4581 xfs_bmap_search_extents(ip
, bno
, whichfork
, &eof
, &bma
.idx
, &bma
.got
,
4592 bma
.firstblock
= firstblock
;
4594 while (bno
< end
&& n
< *nmap
) {
4595 inhole
= eof
|| bma
.got
.br_startoff
> bno
;
4596 wasdelay
= !inhole
&& isnullstartblock(bma
.got
.br_startblock
);
4599 * First, deal with the hole before the allocated space
4600 * that we found, if any.
4602 if (inhole
|| wasdelay
) {
4604 bma
.conv
= !!(flags
& XFS_BMAPI_CONVERT
);
4605 bma
.wasdel
= wasdelay
;
4610 * There's a 32/64 bit type mismatch between the
4611 * allocation length request (which can be 64 bits in
4612 * length) and the bma length request, which is
4613 * xfs_extlen_t and therefore 32 bits. Hence we have to
4614 * check for 32-bit overflows and handle them here.
4616 if (len
> (xfs_filblks_t
)MAXEXTLEN
)
4617 bma
.length
= MAXEXTLEN
;
4622 ASSERT(bma
.length
> 0);
4623 error
= xfs_bmapi_allocate(&bma
);
4626 if (bma
.blkno
== NULLFSBLOCK
)
4630 /* Deal with the allocated space we found. */
4631 xfs_bmapi_trim_map(mval
, &bma
.got
, &bno
, len
, obno
,
4634 /* Execute unwritten extent conversion if necessary */
4635 error
= xfs_bmapi_convert_unwritten(&bma
, mval
, len
, flags
);
4636 if (error
== -EAGAIN
)
4641 /* update the extent map to return */
4642 xfs_bmapi_update_map(&mval
, &bno
, &len
, obno
, end
, &n
, flags
);
4645 * If we're done, stop now. Stop when we've allocated
4646 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4647 * the transaction may get too big.
4649 if (bno
>= end
|| n
>= *nmap
|| bma
.nallocs
>= *nmap
)
4652 /* Else go on to the next record. */
4654 if (++bma
.idx
< ifp
->if_bytes
/ sizeof(xfs_bmbt_rec_t
)) {
4655 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
.idx
),
4663 * Transform from btree to extents, give it cur.
4665 if (xfs_bmap_wants_extents(ip
, whichfork
)) {
4666 int tmp_logflags
= 0;
4669 error
= xfs_bmap_btree_to_extents(tp
, ip
, bma
.cur
,
4670 &tmp_logflags
, whichfork
);
4671 bma
.logflags
|= tmp_logflags
;
4676 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
||
4677 XFS_IFORK_NEXTENTS(ip
, whichfork
) >
4678 XFS_IFORK_MAXEXT(ip
, whichfork
));
4682 * Log everything. Do this after conversion, there's no point in
4683 * logging the extent records if we've converted to btree format.
4685 if ((bma
.logflags
& xfs_ilog_fext(whichfork
)) &&
4686 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
4687 bma
.logflags
&= ~xfs_ilog_fext(whichfork
);
4688 else if ((bma
.logflags
& xfs_ilog_fbroot(whichfork
)) &&
4689 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)
4690 bma
.logflags
&= ~xfs_ilog_fbroot(whichfork
);
4692 * Log whatever the flags say, even if error. Otherwise we might miss
4693 * detecting a case where the data is changed, there's an error,
4694 * and it's not logged so we don't shutdown when we should.
4697 xfs_trans_log_inode(tp
, ip
, bma
.logflags
);
4701 ASSERT(*firstblock
== NULLFSBLOCK
||
4702 XFS_FSB_TO_AGNO(mp
, *firstblock
) ==
4704 bma
.cur
->bc_private
.b
.firstblock
) ||
4706 XFS_FSB_TO_AGNO(mp
, *firstblock
) <
4708 bma
.cur
->bc_private
.b
.firstblock
)));
4709 *firstblock
= bma
.cur
->bc_private
.b
.firstblock
;
4711 xfs_btree_del_cursor(bma
.cur
,
4712 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
4715 xfs_bmap_validate_ret(orig_bno
, orig_len
, orig_flags
, orig_mval
,
4721 * Called by xfs_bmapi to update file extent records and the btree
4722 * after removing space (or undoing a delayed allocation).
4724 STATIC
int /* error */
4725 xfs_bmap_del_extent(
4726 xfs_inode_t
*ip
, /* incore inode pointer */
4727 xfs_trans_t
*tp
, /* current transaction pointer */
4728 xfs_extnum_t
*idx
, /* extent number to update/delete */
4729 xfs_bmap_free_t
*flist
, /* list of extents to be freed */
4730 xfs_btree_cur_t
*cur
, /* if null, not a btree */
4731 xfs_bmbt_irec_t
*del
, /* data to remove from extents */
4732 int *logflagsp
, /* inode logging flags */
4733 int whichfork
) /* data or attr fork */
4735 xfs_filblks_t da_new
; /* new delay-alloc indirect blocks */
4736 xfs_filblks_t da_old
; /* old delay-alloc indirect blocks */
4737 xfs_fsblock_t del_endblock
=0; /* first block past del */
4738 xfs_fileoff_t del_endoff
; /* first offset past del */
4739 int delay
; /* current block is delayed allocated */
4740 int do_fx
; /* free extent at end of routine */
4741 xfs_bmbt_rec_host_t
*ep
; /* current extent entry pointer */
4742 int error
; /* error return value */
4743 int flags
; /* inode logging flags */
4744 xfs_bmbt_irec_t got
; /* current extent entry */
4745 xfs_fileoff_t got_endoff
; /* first offset past got */
4746 int i
; /* temp state */
4747 xfs_ifork_t
*ifp
; /* inode fork pointer */
4748 xfs_mount_t
*mp
; /* mount structure */
4749 xfs_filblks_t nblks
; /* quota/sb block count */
4750 xfs_bmbt_irec_t
new; /* new record to be inserted */
4752 uint qfield
; /* quota field to update */
4753 xfs_filblks_t temp
; /* for indirect length calculations */
4754 xfs_filblks_t temp2
; /* for indirect length calculations */
4758 XFS_STATS_INC(mp
, xs_del_exlist
);
4760 if (whichfork
== XFS_ATTR_FORK
)
4761 state
|= BMAP_ATTRFORK
;
4763 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4764 ASSERT((*idx
>= 0) && (*idx
< ifp
->if_bytes
/
4765 (uint
)sizeof(xfs_bmbt_rec_t
)));
4766 ASSERT(del
->br_blockcount
> 0);
4767 ep
= xfs_iext_get_ext(ifp
, *idx
);
4768 xfs_bmbt_get_all(ep
, &got
);
4769 ASSERT(got
.br_startoff
<= del
->br_startoff
);
4770 del_endoff
= del
->br_startoff
+ del
->br_blockcount
;
4771 got_endoff
= got
.br_startoff
+ got
.br_blockcount
;
4772 ASSERT(got_endoff
>= del_endoff
);
4773 delay
= isnullstartblock(got
.br_startblock
);
4774 ASSERT(isnullstartblock(del
->br_startblock
) == delay
);
4779 * If deleting a real allocation, must free up the disk space.
4782 flags
= XFS_ILOG_CORE
;
4784 * Realtime allocation. Free it and record di_nblocks update.
4786 if (whichfork
== XFS_DATA_FORK
&& XFS_IS_REALTIME_INODE(ip
)) {
4790 ASSERT(do_mod(del
->br_blockcount
,
4791 mp
->m_sb
.sb_rextsize
) == 0);
4792 ASSERT(do_mod(del
->br_startblock
,
4793 mp
->m_sb
.sb_rextsize
) == 0);
4794 bno
= del
->br_startblock
;
4795 len
= del
->br_blockcount
;
4796 do_div(bno
, mp
->m_sb
.sb_rextsize
);
4797 do_div(len
, mp
->m_sb
.sb_rextsize
);
4798 error
= xfs_rtfree_extent(tp
, bno
, (xfs_extlen_t
)len
);
4802 nblks
= len
* mp
->m_sb
.sb_rextsize
;
4803 qfield
= XFS_TRANS_DQ_RTBCOUNT
;
4806 * Ordinary allocation.
4810 nblks
= del
->br_blockcount
;
4811 qfield
= XFS_TRANS_DQ_BCOUNT
;
4814 * Set up del_endblock and cur for later.
4816 del_endblock
= del
->br_startblock
+ del
->br_blockcount
;
4818 if ((error
= xfs_bmbt_lookup_eq(cur
, got
.br_startoff
,
4819 got
.br_startblock
, got
.br_blockcount
,
4822 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
4824 da_old
= da_new
= 0;
4826 da_old
= startblockval(got
.br_startblock
);
4832 * Set flag value to use in switch statement.
4833 * Left-contig is 2, right-contig is 1.
4835 switch (((got
.br_startoff
== del
->br_startoff
) << 1) |
4836 (got_endoff
== del_endoff
)) {
4839 * Matches the whole extent. Delete the entry.
4841 xfs_iext_remove(ip
, *idx
, 1,
4842 whichfork
== XFS_ATTR_FORK
? BMAP_ATTRFORK
: 0);
4847 XFS_IFORK_NEXT_SET(ip
, whichfork
,
4848 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
4849 flags
|= XFS_ILOG_CORE
;
4851 flags
|= xfs_ilog_fext(whichfork
);
4854 if ((error
= xfs_btree_delete(cur
, &i
)))
4856 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
4861 * Deleting the first part of the extent.
4863 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
4864 xfs_bmbt_set_startoff(ep
, del_endoff
);
4865 temp
= got
.br_blockcount
- del
->br_blockcount
;
4866 xfs_bmbt_set_blockcount(ep
, temp
);
4868 temp
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
4870 xfs_bmbt_set_startblock(ep
, nullstartblock((int)temp
));
4871 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
4875 xfs_bmbt_set_startblock(ep
, del_endblock
);
4876 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
4878 flags
|= xfs_ilog_fext(whichfork
);
4881 if ((error
= xfs_bmbt_update(cur
, del_endoff
, del_endblock
,
4882 got
.br_blockcount
- del
->br_blockcount
,
4889 * Deleting the last part of the extent.
4891 temp
= got
.br_blockcount
- del
->br_blockcount
;
4892 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
4893 xfs_bmbt_set_blockcount(ep
, temp
);
4895 temp
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
4897 xfs_bmbt_set_startblock(ep
, nullstartblock((int)temp
));
4898 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
4902 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
4904 flags
|= xfs_ilog_fext(whichfork
);
4907 if ((error
= xfs_bmbt_update(cur
, got
.br_startoff
,
4909 got
.br_blockcount
- del
->br_blockcount
,
4916 * Deleting the middle of the extent.
4918 temp
= del
->br_startoff
- got
.br_startoff
;
4919 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
4920 xfs_bmbt_set_blockcount(ep
, temp
);
4921 new.br_startoff
= del_endoff
;
4922 temp2
= got_endoff
- del_endoff
;
4923 new.br_blockcount
= temp2
;
4924 new.br_state
= got
.br_state
;
4926 new.br_startblock
= del_endblock
;
4927 flags
|= XFS_ILOG_CORE
;
4929 if ((error
= xfs_bmbt_update(cur
,
4931 got
.br_startblock
, temp
,
4934 if ((error
= xfs_btree_increment(cur
, 0, &i
)))
4936 cur
->bc_rec
.b
= new;
4937 error
= xfs_btree_insert(cur
, &i
);
4938 if (error
&& error
!= -ENOSPC
)
4941 * If get no-space back from btree insert,
4942 * it tried a split, and we have a zero
4943 * block reservation.
4944 * Fix up our state and return the error.
4946 if (error
== -ENOSPC
) {
4948 * Reset the cursor, don't trust
4949 * it after any insert operation.
4951 if ((error
= xfs_bmbt_lookup_eq(cur
,
4956 XFS_WANT_CORRUPTED_GOTO(mp
,
4959 * Update the btree record back
4960 * to the original value.
4962 if ((error
= xfs_bmbt_update(cur
,
4969 * Reset the extent record back
4970 * to the original value.
4972 xfs_bmbt_set_blockcount(ep
,
4978 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
4980 flags
|= xfs_ilog_fext(whichfork
);
4981 XFS_IFORK_NEXT_SET(ip
, whichfork
,
4982 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
4984 ASSERT(whichfork
== XFS_DATA_FORK
);
4985 temp
= xfs_bmap_worst_indlen(ip
, temp
);
4986 xfs_bmbt_set_startblock(ep
, nullstartblock((int)temp
));
4987 temp2
= xfs_bmap_worst_indlen(ip
, temp2
);
4988 new.br_startblock
= nullstartblock((int)temp2
);
4989 da_new
= temp
+ temp2
;
4990 while (da_new
> da_old
) {
4994 xfs_bmbt_set_startblock(ep
,
4995 nullstartblock((int)temp
));
4997 if (da_new
== da_old
)
5003 nullstartblock((int)temp2
);
5007 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
5008 xfs_iext_insert(ip
, *idx
+ 1, 1, &new, state
);
5013 * If we need to, add to list of extents to delete.
5016 xfs_bmap_add_free(del
->br_startblock
, del
->br_blockcount
, flist
,
5019 * Adjust inode # blocks in the file.
5022 ip
->i_d
.di_nblocks
-= nblks
;
5024 * Adjust quota data.
5027 xfs_trans_mod_dquot_byino(tp
, ip
, qfield
, (long)-nblks
);
5030 * Account for change in delayed indirect blocks.
5031 * Nothing to do for disk quota accounting here.
5033 ASSERT(da_old
>= da_new
);
5034 if (da_old
> da_new
)
5035 xfs_mod_fdblocks(mp
, (int64_t)(da_old
- da_new
), false);
5042 * Unmap (remove) blocks from a file.
5043 * If nexts is nonzero then the number of extents to remove is limited to
5044 * that value. If not all extents in the block range can be removed then
5049 xfs_trans_t
*tp
, /* transaction pointer */
5050 struct xfs_inode
*ip
, /* incore inode */
5051 xfs_fileoff_t bno
, /* starting offset to unmap */
5052 xfs_filblks_t len
, /* length to unmap in file */
5053 int flags
, /* misc flags */
5054 xfs_extnum_t nexts
, /* number of extents max */
5055 xfs_fsblock_t
*firstblock
, /* first allocated block
5056 controls a.g. for allocs */
5057 xfs_bmap_free_t
*flist
, /* i/o: list extents to free */
5058 int *done
) /* set if not done yet */
5060 xfs_btree_cur_t
*cur
; /* bmap btree cursor */
5061 xfs_bmbt_irec_t del
; /* extent being deleted */
5062 int eof
; /* is deleting at eof */
5063 xfs_bmbt_rec_host_t
*ep
; /* extent record pointer */
5064 int error
; /* error return value */
5065 xfs_extnum_t extno
; /* extent number in list */
5066 xfs_bmbt_irec_t got
; /* current extent record */
5067 xfs_ifork_t
*ifp
; /* inode fork pointer */
5068 int isrt
; /* freeing in rt area */
5069 xfs_extnum_t lastx
; /* last extent index used */
5070 int logflags
; /* transaction logging flags */
5071 xfs_extlen_t mod
; /* rt extent offset */
5072 xfs_mount_t
*mp
; /* mount structure */
5073 xfs_extnum_t nextents
; /* number of file extents */
5074 xfs_bmbt_irec_t prev
; /* previous extent record */
5075 xfs_fileoff_t start
; /* first file offset deleted */
5076 int tmp_logflags
; /* partial logging flags */
5077 int wasdel
; /* was a delayed alloc extent */
5078 int whichfork
; /* data or attribute fork */
5081 trace_xfs_bunmap(ip
, bno
, len
, flags
, _RET_IP_
);
5083 whichfork
= (flags
& XFS_BMAPI_ATTRFORK
) ?
5084 XFS_ATTR_FORK
: XFS_DATA_FORK
;
5085 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5087 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
5088 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)) {
5089 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW
,
5091 return -EFSCORRUPTED
;
5094 if (XFS_FORCED_SHUTDOWN(mp
))
5097 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
5101 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
5102 (error
= xfs_iread_extents(tp
, ip
, whichfork
)))
5104 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
5105 if (nextents
== 0) {
5109 XFS_STATS_INC(mp
, xs_blk_unmap
);
5110 isrt
= (whichfork
== XFS_DATA_FORK
) && XFS_IS_REALTIME_INODE(ip
);
5112 bno
= start
+ len
- 1;
5113 ep
= xfs_bmap_search_extents(ip
, bno
, whichfork
, &eof
, &lastx
, &got
,
5117 * Check to see if the given block number is past the end of the
5118 * file, back up to the last block if so...
5121 ep
= xfs_iext_get_ext(ifp
, --lastx
);
5122 xfs_bmbt_get_all(ep
, &got
);
5123 bno
= got
.br_startoff
+ got
.br_blockcount
- 1;
5126 if (ifp
->if_flags
& XFS_IFBROOT
) {
5127 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
);
5128 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
5129 cur
->bc_private
.b
.firstblock
= *firstblock
;
5130 cur
->bc_private
.b
.flist
= flist
;
5131 cur
->bc_private
.b
.flags
= 0;
5137 * Synchronize by locking the bitmap inode.
5139 xfs_ilock(mp
->m_rbmip
, XFS_ILOCK_EXCL
);
5140 xfs_trans_ijoin(tp
, mp
->m_rbmip
, XFS_ILOCK_EXCL
);
5144 while (bno
!= (xfs_fileoff_t
)-1 && bno
>= start
&& lastx
>= 0 &&
5145 (nexts
== 0 || extno
< nexts
)) {
5147 * Is the found extent after a hole in which bno lives?
5148 * Just back up to the previous extent, if so.
5150 if (got
.br_startoff
> bno
) {
5153 ep
= xfs_iext_get_ext(ifp
, lastx
);
5154 xfs_bmbt_get_all(ep
, &got
);
5157 * Is the last block of this extent before the range
5158 * we're supposed to delete? If so, we're done.
5160 bno
= XFS_FILEOFF_MIN(bno
,
5161 got
.br_startoff
+ got
.br_blockcount
- 1);
5165 * Then deal with the (possibly delayed) allocated space
5170 wasdel
= isnullstartblock(del
.br_startblock
);
5171 if (got
.br_startoff
< start
) {
5172 del
.br_startoff
= start
;
5173 del
.br_blockcount
-= start
- got
.br_startoff
;
5175 del
.br_startblock
+= start
- got
.br_startoff
;
5177 if (del
.br_startoff
+ del
.br_blockcount
> bno
+ 1)
5178 del
.br_blockcount
= bno
+ 1 - del
.br_startoff
;
5179 sum
= del
.br_startblock
+ del
.br_blockcount
;
5181 (mod
= do_mod(sum
, mp
->m_sb
.sb_rextsize
))) {
5183 * Realtime extent not lined up at the end.
5184 * The extent could have been split into written
5185 * and unwritten pieces, or we could just be
5186 * unmapping part of it. But we can't really
5187 * get rid of part of a realtime extent.
5189 if (del
.br_state
== XFS_EXT_UNWRITTEN
||
5190 !xfs_sb_version_hasextflgbit(&mp
->m_sb
)) {
5192 * This piece is unwritten, or we're not
5193 * using unwritten extents. Skip over it.
5196 bno
-= mod
> del
.br_blockcount
?
5197 del
.br_blockcount
: mod
;
5198 if (bno
< got
.br_startoff
) {
5200 xfs_bmbt_get_all(xfs_iext_get_ext(
5206 * It's written, turn it unwritten.
5207 * This is better than zeroing it.
5209 ASSERT(del
.br_state
== XFS_EXT_NORM
);
5210 ASSERT(xfs_trans_get_block_res(tp
) > 0);
5212 * If this spans a realtime extent boundary,
5213 * chop it back to the start of the one we end at.
5215 if (del
.br_blockcount
> mod
) {
5216 del
.br_startoff
+= del
.br_blockcount
- mod
;
5217 del
.br_startblock
+= del
.br_blockcount
- mod
;
5218 del
.br_blockcount
= mod
;
5220 del
.br_state
= XFS_EXT_UNWRITTEN
;
5221 error
= xfs_bmap_add_extent_unwritten_real(tp
, ip
,
5222 &lastx
, &cur
, &del
, firstblock
, flist
,
5228 if (isrt
&& (mod
= do_mod(del
.br_startblock
, mp
->m_sb
.sb_rextsize
))) {
5230 * Realtime extent is lined up at the end but not
5231 * at the front. We'll get rid of full extents if
5234 mod
= mp
->m_sb
.sb_rextsize
- mod
;
5235 if (del
.br_blockcount
> mod
) {
5236 del
.br_blockcount
-= mod
;
5237 del
.br_startoff
+= mod
;
5238 del
.br_startblock
+= mod
;
5239 } else if ((del
.br_startoff
== start
&&
5240 (del
.br_state
== XFS_EXT_UNWRITTEN
||
5241 xfs_trans_get_block_res(tp
) == 0)) ||
5242 !xfs_sb_version_hasextflgbit(&mp
->m_sb
)) {
5244 * Can't make it unwritten. There isn't
5245 * a full extent here so just skip it.
5247 ASSERT(bno
>= del
.br_blockcount
);
5248 bno
-= del
.br_blockcount
;
5249 if (got
.br_startoff
> bno
) {
5251 ep
= xfs_iext_get_ext(ifp
,
5253 xfs_bmbt_get_all(ep
, &got
);
5257 } else if (del
.br_state
== XFS_EXT_UNWRITTEN
) {
5259 * This one is already unwritten.
5260 * It must have a written left neighbor.
5261 * Unwrite the killed part of that one and
5265 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
,
5267 ASSERT(prev
.br_state
== XFS_EXT_NORM
);
5268 ASSERT(!isnullstartblock(prev
.br_startblock
));
5269 ASSERT(del
.br_startblock
==
5270 prev
.br_startblock
+ prev
.br_blockcount
);
5271 if (prev
.br_startoff
< start
) {
5272 mod
= start
- prev
.br_startoff
;
5273 prev
.br_blockcount
-= mod
;
5274 prev
.br_startblock
+= mod
;
5275 prev
.br_startoff
= start
;
5277 prev
.br_state
= XFS_EXT_UNWRITTEN
;
5279 error
= xfs_bmap_add_extent_unwritten_real(tp
,
5280 ip
, &lastx
, &cur
, &prev
,
5281 firstblock
, flist
, &logflags
);
5286 ASSERT(del
.br_state
== XFS_EXT_NORM
);
5287 del
.br_state
= XFS_EXT_UNWRITTEN
;
5288 error
= xfs_bmap_add_extent_unwritten_real(tp
,
5289 ip
, &lastx
, &cur
, &del
,
5290 firstblock
, flist
, &logflags
);
5297 ASSERT(startblockval(del
.br_startblock
) > 0);
5298 /* Update realtime/data freespace, unreserve quota */
5300 xfs_filblks_t rtexts
;
5302 rtexts
= XFS_FSB_TO_B(mp
, del
.br_blockcount
);
5303 do_div(rtexts
, mp
->m_sb
.sb_rextsize
);
5304 xfs_mod_frextents(mp
, (int64_t)rtexts
);
5305 (void)xfs_trans_reserve_quota_nblks(NULL
,
5306 ip
, -((long)del
.br_blockcount
), 0,
5307 XFS_QMOPT_RES_RTBLKS
);
5309 xfs_mod_fdblocks(mp
, (int64_t)del
.br_blockcount
,
5311 (void)xfs_trans_reserve_quota_nblks(NULL
,
5312 ip
, -((long)del
.br_blockcount
), 0,
5313 XFS_QMOPT_RES_REGBLKS
);
5315 ip
->i_delayed_blks
-= del
.br_blockcount
;
5317 cur
->bc_private
.b
.flags
|=
5318 XFS_BTCUR_BPRV_WASDEL
;
5320 cur
->bc_private
.b
.flags
&= ~XFS_BTCUR_BPRV_WASDEL
;
5322 * If it's the case where the directory code is running
5323 * with no block reservation, and the deleted block is in
5324 * the middle of its extent, and the resulting insert
5325 * of an extent would cause transformation to btree format,
5326 * then reject it. The calling code will then swap
5327 * blocks around instead.
5328 * We have to do this now, rather than waiting for the
5329 * conversion to btree format, since the transaction
5332 if (!wasdel
&& xfs_trans_get_block_res(tp
) == 0 &&
5333 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
&&
5334 XFS_IFORK_NEXTENTS(ip
, whichfork
) >= /* Note the >= */
5335 XFS_IFORK_MAXEXT(ip
, whichfork
) &&
5336 del
.br_startoff
> got
.br_startoff
&&
5337 del
.br_startoff
+ del
.br_blockcount
<
5338 got
.br_startoff
+ got
.br_blockcount
) {
5342 error
= xfs_bmap_del_extent(ip
, tp
, &lastx
, flist
, cur
, &del
,
5343 &tmp_logflags
, whichfork
);
5344 logflags
|= tmp_logflags
;
5347 bno
= del
.br_startoff
- 1;
5350 * If not done go on to the next (previous) record.
5352 if (bno
!= (xfs_fileoff_t
)-1 && bno
>= start
) {
5354 ep
= xfs_iext_get_ext(ifp
, lastx
);
5355 if (xfs_bmbt_get_startoff(ep
) > bno
) {
5357 ep
= xfs_iext_get_ext(ifp
,
5360 xfs_bmbt_get_all(ep
, &got
);
5365 *done
= bno
== (xfs_fileoff_t
)-1 || bno
< start
|| lastx
< 0;
5368 * Convert to a btree if necessary.
5370 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
5371 ASSERT(cur
== NULL
);
5372 error
= xfs_bmap_extents_to_btree(tp
, ip
, firstblock
, flist
,
5373 &cur
, 0, &tmp_logflags
, whichfork
);
5374 logflags
|= tmp_logflags
;
5379 * transform from btree to extents, give it cur
5381 else if (xfs_bmap_wants_extents(ip
, whichfork
)) {
5382 ASSERT(cur
!= NULL
);
5383 error
= xfs_bmap_btree_to_extents(tp
, ip
, cur
, &tmp_logflags
,
5385 logflags
|= tmp_logflags
;
5390 * transform from extents to local?
5395 * Log everything. Do this after conversion, there's no point in
5396 * logging the extent records if we've converted to btree format.
5398 if ((logflags
& xfs_ilog_fext(whichfork
)) &&
5399 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
5400 logflags
&= ~xfs_ilog_fext(whichfork
);
5401 else if ((logflags
& xfs_ilog_fbroot(whichfork
)) &&
5402 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)
5403 logflags
&= ~xfs_ilog_fbroot(whichfork
);
5405 * Log inode even in the error case, if the transaction
5406 * is dirty we'll need to shut down the filesystem.
5409 xfs_trans_log_inode(tp
, ip
, logflags
);
5412 *firstblock
= cur
->bc_private
.b
.firstblock
;
5413 cur
->bc_private
.b
.allocated
= 0;
5415 xfs_btree_del_cursor(cur
,
5416 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
5422 * Determine whether an extent shift can be accomplished by a merge with the
5423 * extent that precedes the target hole of the shift.
5427 struct xfs_bmbt_irec
*left
, /* preceding extent */
5428 struct xfs_bmbt_irec
*got
, /* current extent to shift */
5429 xfs_fileoff_t shift
) /* shift fsb */
5431 xfs_fileoff_t startoff
;
5433 startoff
= got
->br_startoff
- shift
;
5436 * The extent, once shifted, must be adjacent in-file and on-disk with
5437 * the preceding extent.
5439 if ((left
->br_startoff
+ left
->br_blockcount
!= startoff
) ||
5440 (left
->br_startblock
+ left
->br_blockcount
!= got
->br_startblock
) ||
5441 (left
->br_state
!= got
->br_state
) ||
5442 (left
->br_blockcount
+ got
->br_blockcount
> MAXEXTLEN
))
5449 * A bmap extent shift adjusts the file offset of an extent to fill a preceding
5450 * hole in the file. If an extent shift would result in the extent being fully
5451 * adjacent to the extent that currently precedes the hole, we can merge with
5452 * the preceding extent rather than do the shift.
5454 * This function assumes the caller has verified a shift-by-merge is possible
5455 * with the provided extents via xfs_bmse_can_merge().
5459 struct xfs_inode
*ip
,
5461 xfs_fileoff_t shift
, /* shift fsb */
5462 int current_ext
, /* idx of gotp */
5463 struct xfs_bmbt_rec_host
*gotp
, /* extent to shift */
5464 struct xfs_bmbt_rec_host
*leftp
, /* preceding extent */
5465 struct xfs_btree_cur
*cur
,
5466 int *logflags
) /* output */
5468 struct xfs_bmbt_irec got
;
5469 struct xfs_bmbt_irec left
;
5470 xfs_filblks_t blockcount
;
5472 struct xfs_mount
*mp
= ip
->i_mount
;
5474 xfs_bmbt_get_all(gotp
, &got
);
5475 xfs_bmbt_get_all(leftp
, &left
);
5476 blockcount
= left
.br_blockcount
+ got
.br_blockcount
;
5478 ASSERT(xfs_isilocked(ip
, XFS_IOLOCK_EXCL
));
5479 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
5480 ASSERT(xfs_bmse_can_merge(&left
, &got
, shift
));
5483 * Merge the in-core extents. Note that the host record pointers and
5484 * current_ext index are invalid once the extent has been removed via
5485 * xfs_iext_remove().
5487 xfs_bmbt_set_blockcount(leftp
, blockcount
);
5488 xfs_iext_remove(ip
, current_ext
, 1, 0);
5491 * Update the on-disk extent count, the btree if necessary and log the
5494 XFS_IFORK_NEXT_SET(ip
, whichfork
,
5495 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
5496 *logflags
|= XFS_ILOG_CORE
;
5498 *logflags
|= XFS_ILOG_DEXT
;
5502 /* lookup and remove the extent to merge */
5503 error
= xfs_bmbt_lookup_eq(cur
, got
.br_startoff
, got
.br_startblock
,
5504 got
.br_blockcount
, &i
);
5507 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
5509 error
= xfs_btree_delete(cur
, &i
);
5512 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
5514 /* lookup and update size of the previous extent */
5515 error
= xfs_bmbt_lookup_eq(cur
, left
.br_startoff
, left
.br_startblock
,
5516 left
.br_blockcount
, &i
);
5519 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
5521 left
.br_blockcount
= blockcount
;
5523 return xfs_bmbt_update(cur
, left
.br_startoff
, left
.br_startblock
,
5524 left
.br_blockcount
, left
.br_state
);
5528 * Shift a single extent.
5532 struct xfs_inode
*ip
,
5534 xfs_fileoff_t offset_shift_fsb
,
5536 struct xfs_bmbt_rec_host
*gotp
,
5537 struct xfs_btree_cur
*cur
,
5539 enum shift_direction direction
)
5541 struct xfs_ifork
*ifp
;
5542 struct xfs_mount
*mp
;
5543 xfs_fileoff_t startoff
;
5544 struct xfs_bmbt_rec_host
*adj_irecp
;
5545 struct xfs_bmbt_irec got
;
5546 struct xfs_bmbt_irec adj_irec
;
5552 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5553 total_extents
= ifp
->if_bytes
/ sizeof(xfs_bmbt_rec_t
);
5555 xfs_bmbt_get_all(gotp
, &got
);
5557 /* delalloc extents should be prevented by caller */
5558 XFS_WANT_CORRUPTED_RETURN(mp
, !isnullstartblock(got
.br_startblock
));
5560 if (direction
== SHIFT_LEFT
) {
5561 startoff
= got
.br_startoff
- offset_shift_fsb
;
5564 * Check for merge if we've got an extent to the left,
5565 * otherwise make sure there's enough room at the start
5566 * of the file for the shift.
5568 if (!*current_ext
) {
5569 if (got
.br_startoff
< offset_shift_fsb
)
5571 goto update_current_ext
;
5574 * grab the left extent and check for a large
5577 adj_irecp
= xfs_iext_get_ext(ifp
, *current_ext
- 1);
5578 xfs_bmbt_get_all(adj_irecp
, &adj_irec
);
5581 adj_irec
.br_startoff
+ adj_irec
.br_blockcount
)
5584 /* check whether to merge the extent or shift it down */
5585 if (xfs_bmse_can_merge(&adj_irec
, &got
,
5586 offset_shift_fsb
)) {
5587 return xfs_bmse_merge(ip
, whichfork
, offset_shift_fsb
,
5588 *current_ext
, gotp
, adj_irecp
,
5592 startoff
= got
.br_startoff
+ offset_shift_fsb
;
5593 /* nothing to move if this is the last extent */
5594 if (*current_ext
>= (total_extents
- 1))
5595 goto update_current_ext
;
5597 * If this is not the last extent in the file, make sure there
5598 * is enough room between current extent and next extent for
5599 * accommodating the shift.
5601 adj_irecp
= xfs_iext_get_ext(ifp
, *current_ext
+ 1);
5602 xfs_bmbt_get_all(adj_irecp
, &adj_irec
);
5603 if (startoff
+ got
.br_blockcount
> adj_irec
.br_startoff
)
5606 * Unlike a left shift (which involves a hole punch),
5607 * a right shift does not modify extent neighbors
5608 * in any way. We should never find mergeable extents
5609 * in this scenario. Check anyways and warn if we
5610 * encounter two extents that could be one.
5612 if (xfs_bmse_can_merge(&got
, &adj_irec
, offset_shift_fsb
))
5616 * Increment the extent index for the next iteration, update the start
5617 * offset of the in-core extent and update the btree if applicable.
5620 if (direction
== SHIFT_LEFT
)
5624 xfs_bmbt_set_startoff(gotp
, startoff
);
5625 *logflags
|= XFS_ILOG_CORE
;
5627 *logflags
|= XFS_ILOG_DEXT
;
5631 error
= xfs_bmbt_lookup_eq(cur
, got
.br_startoff
, got
.br_startblock
,
5632 got
.br_blockcount
, &i
);
5635 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
5637 got
.br_startoff
= startoff
;
5638 return xfs_bmbt_update(cur
, got
.br_startoff
, got
.br_startblock
,
5639 got
.br_blockcount
, got
.br_state
);
5643 * Shift extent records to the left/right to cover/create a hole.
5645 * The maximum number of extents to be shifted in a single operation is
5646 * @num_exts. @stop_fsb specifies the file offset at which to stop shift and the
5647 * file offset where we've left off is returned in @next_fsb. @offset_shift_fsb
5648 * is the length by which each extent is shifted. If there is no hole to shift
5649 * the extents into, this will be considered invalid operation and we abort
5653 xfs_bmap_shift_extents(
5654 struct xfs_trans
*tp
,
5655 struct xfs_inode
*ip
,
5656 xfs_fileoff_t
*next_fsb
,
5657 xfs_fileoff_t offset_shift_fsb
,
5659 xfs_fileoff_t stop_fsb
,
5660 xfs_fsblock_t
*firstblock
,
5661 struct xfs_bmap_free
*flist
,
5662 enum shift_direction direction
,
5665 struct xfs_btree_cur
*cur
= NULL
;
5666 struct xfs_bmbt_rec_host
*gotp
;
5667 struct xfs_bmbt_irec got
;
5668 struct xfs_mount
*mp
= ip
->i_mount
;
5669 struct xfs_ifork
*ifp
;
5670 xfs_extnum_t nexts
= 0;
5671 xfs_extnum_t current_ext
;
5672 xfs_extnum_t total_extents
;
5673 xfs_extnum_t stop_extent
;
5675 int whichfork
= XFS_DATA_FORK
;
5678 if (unlikely(XFS_TEST_ERROR(
5679 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
5680 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
5681 mp
, XFS_ERRTAG_BMAPIFORMAT
, XFS_RANDOM_BMAPIFORMAT
))) {
5682 XFS_ERROR_REPORT("xfs_bmap_shift_extents",
5683 XFS_ERRLEVEL_LOW
, mp
);
5684 return -EFSCORRUPTED
;
5687 if (XFS_FORCED_SHUTDOWN(mp
))
5690 ASSERT(xfs_isilocked(ip
, XFS_IOLOCK_EXCL
));
5691 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
5692 ASSERT(direction
== SHIFT_LEFT
|| direction
== SHIFT_RIGHT
);
5693 ASSERT(*next_fsb
!= NULLFSBLOCK
|| direction
== SHIFT_RIGHT
);
5695 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5696 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
5697 /* Read in all the extents */
5698 error
= xfs_iread_extents(tp
, ip
, whichfork
);
5703 if (ifp
->if_flags
& XFS_IFBROOT
) {
5704 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
5705 cur
->bc_private
.b
.firstblock
= *firstblock
;
5706 cur
->bc_private
.b
.flist
= flist
;
5707 cur
->bc_private
.b
.flags
= 0;
5711 * There may be delalloc extents in the data fork before the range we
5712 * are collapsing out, so we cannot use the count of real extents here.
5713 * Instead we have to calculate it from the incore fork.
5715 total_extents
= ifp
->if_bytes
/ sizeof(xfs_bmbt_rec_t
);
5716 if (total_extents
== 0) {
5722 * In case of first right shift, we need to initialize next_fsb
5724 if (*next_fsb
== NULLFSBLOCK
) {
5725 gotp
= xfs_iext_get_ext(ifp
, total_extents
- 1);
5726 xfs_bmbt_get_all(gotp
, &got
);
5727 *next_fsb
= got
.br_startoff
;
5728 if (stop_fsb
> *next_fsb
) {
5734 /* Lookup the extent index at which we have to stop */
5735 if (direction
== SHIFT_RIGHT
) {
5736 gotp
= xfs_iext_bno_to_ext(ifp
, stop_fsb
, &stop_extent
);
5737 /* Make stop_extent exclusive of shift range */
5740 stop_extent
= total_extents
;
5743 * Look up the extent index for the fsb where we start shifting. We can
5744 * henceforth iterate with current_ext as extent list changes are locked
5747 * gotp can be null in 2 cases: 1) if there are no extents or 2)
5748 * *next_fsb lies in a hole beyond which there are no extents. Either
5751 gotp
= xfs_iext_bno_to_ext(ifp
, *next_fsb
, ¤t_ext
);
5757 /* some sanity checking before we finally start shifting extents */
5758 if ((direction
== SHIFT_LEFT
&& current_ext
>= stop_extent
) ||
5759 (direction
== SHIFT_RIGHT
&& current_ext
<= stop_extent
)) {
5764 while (nexts
++ < num_exts
) {
5765 error
= xfs_bmse_shift_one(ip
, whichfork
, offset_shift_fsb
,
5766 ¤t_ext
, gotp
, cur
, &logflags
,
5771 * If there was an extent merge during the shift, the extent
5772 * count can change. Update the total and grade the next record.
5774 if (direction
== SHIFT_LEFT
) {
5775 total_extents
= ifp
->if_bytes
/ sizeof(xfs_bmbt_rec_t
);
5776 stop_extent
= total_extents
;
5779 if (current_ext
== stop_extent
) {
5781 *next_fsb
= NULLFSBLOCK
;
5784 gotp
= xfs_iext_get_ext(ifp
, current_ext
);
5788 xfs_bmbt_get_all(gotp
, &got
);
5789 *next_fsb
= got
.br_startoff
;
5794 xfs_btree_del_cursor(cur
,
5795 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
5798 xfs_trans_log_inode(tp
, ip
, logflags
);
5804 * Splits an extent into two extents at split_fsb block such that it is
5805 * the first block of the current_ext. @current_ext is a target extent
5806 * to be split. @split_fsb is a block where the extents is split.
5807 * If split_fsb lies in a hole or the first block of extents, just return 0.
5810 xfs_bmap_split_extent_at(
5811 struct xfs_trans
*tp
,
5812 struct xfs_inode
*ip
,
5813 xfs_fileoff_t split_fsb
,
5814 xfs_fsblock_t
*firstfsb
,
5815 struct xfs_bmap_free
*free_list
)
5817 int whichfork
= XFS_DATA_FORK
;
5818 struct xfs_btree_cur
*cur
= NULL
;
5819 struct xfs_bmbt_rec_host
*gotp
;
5820 struct xfs_bmbt_irec got
;
5821 struct xfs_bmbt_irec
new; /* split extent */
5822 struct xfs_mount
*mp
= ip
->i_mount
;
5823 struct xfs_ifork
*ifp
;
5824 xfs_fsblock_t gotblkcnt
; /* new block count for got */
5825 xfs_extnum_t current_ext
;
5830 if (unlikely(XFS_TEST_ERROR(
5831 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
5832 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
5833 mp
, XFS_ERRTAG_BMAPIFORMAT
, XFS_RANDOM_BMAPIFORMAT
))) {
5834 XFS_ERROR_REPORT("xfs_bmap_split_extent_at",
5835 XFS_ERRLEVEL_LOW
, mp
);
5836 return -EFSCORRUPTED
;
5839 if (XFS_FORCED_SHUTDOWN(mp
))
5842 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5843 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
5844 /* Read in all the extents */
5845 error
= xfs_iread_extents(tp
, ip
, whichfork
);
5851 * gotp can be null in 2 cases: 1) if there are no extents
5852 * or 2) split_fsb lies in a hole beyond which there are
5853 * no extents. Either way, we are done.
5855 gotp
= xfs_iext_bno_to_ext(ifp
, split_fsb
, ¤t_ext
);
5859 xfs_bmbt_get_all(gotp
, &got
);
5862 * Check split_fsb lies in a hole or the start boundary offset
5865 if (got
.br_startoff
>= split_fsb
)
5868 gotblkcnt
= split_fsb
- got
.br_startoff
;
5869 new.br_startoff
= split_fsb
;
5870 new.br_startblock
= got
.br_startblock
+ gotblkcnt
;
5871 new.br_blockcount
= got
.br_blockcount
- gotblkcnt
;
5872 new.br_state
= got
.br_state
;
5874 if (ifp
->if_flags
& XFS_IFBROOT
) {
5875 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
5876 cur
->bc_private
.b
.firstblock
= *firstfsb
;
5877 cur
->bc_private
.b
.flist
= free_list
;
5878 cur
->bc_private
.b
.flags
= 0;
5879 error
= xfs_bmbt_lookup_eq(cur
, got
.br_startoff
,
5885 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, del_cursor
);
5888 xfs_bmbt_set_blockcount(gotp
, gotblkcnt
);
5889 got
.br_blockcount
= gotblkcnt
;
5891 logflags
= XFS_ILOG_CORE
;
5893 error
= xfs_bmbt_update(cur
, got
.br_startoff
,
5900 logflags
|= XFS_ILOG_DEXT
;
5902 /* Add new extent */
5904 xfs_iext_insert(ip
, current_ext
, 1, &new, 0);
5905 XFS_IFORK_NEXT_SET(ip
, whichfork
,
5906 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
5909 error
= xfs_bmbt_lookup_eq(cur
, new.br_startoff
,
5910 new.br_startblock
, new.br_blockcount
,
5914 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, del_cursor
);
5915 cur
->bc_rec
.b
.br_state
= new.br_state
;
5917 error
= xfs_btree_insert(cur
, &i
);
5920 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, del_cursor
);
5924 * Convert to a btree if necessary.
5926 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
5927 int tmp_logflags
; /* partial log flag return val */
5929 ASSERT(cur
== NULL
);
5930 error
= xfs_bmap_extents_to_btree(tp
, ip
, firstfsb
, free_list
,
5931 &cur
, 0, &tmp_logflags
, whichfork
);
5932 logflags
|= tmp_logflags
;
5937 cur
->bc_private
.b
.allocated
= 0;
5938 xfs_btree_del_cursor(cur
,
5939 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
5943 xfs_trans_log_inode(tp
, ip
, logflags
);
5948 xfs_bmap_split_extent(
5949 struct xfs_inode
*ip
,
5950 xfs_fileoff_t split_fsb
)
5952 struct xfs_mount
*mp
= ip
->i_mount
;
5953 struct xfs_trans
*tp
;
5954 struct xfs_bmap_free free_list
;
5955 xfs_fsblock_t firstfsb
;
5959 tp
= xfs_trans_alloc(mp
, XFS_TRANS_DIOSTRAT
);
5960 error
= xfs_trans_reserve(tp
, &M_RES(mp
)->tr_write
,
5961 XFS_DIOSTRAT_SPACE_RES(mp
, 0), 0);
5963 xfs_trans_cancel(tp
);
5967 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
5968 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
);
5970 xfs_bmap_init(&free_list
, &firstfsb
);
5972 error
= xfs_bmap_split_extent_at(tp
, ip
, split_fsb
,
5973 &firstfsb
, &free_list
);
5977 error
= xfs_bmap_finish(&tp
, &free_list
, &committed
);
5981 return xfs_trans_commit(tp
);
5984 xfs_bmap_cancel(&free_list
);
5985 xfs_trans_cancel(tp
);