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
328 * btree leaves. THis becomes prohibitively expensive for large extent count
329 * files, so don't bother with inodes that have more than 10,000 extents in
330 * them. The btree record ordering checks will still be done, so for such large
331 * bmapbt constructs that is going to catch most corruptions.
334 xfs_bmap_check_leaf_extents(
335 xfs_btree_cur_t
*cur
, /* btree cursor or null */
336 xfs_inode_t
*ip
, /* incore inode pointer */
337 int whichfork
) /* data or attr fork */
339 struct xfs_btree_block
*block
; /* current btree block */
340 xfs_fsblock_t bno
; /* block # of "block" */
341 xfs_buf_t
*bp
; /* buffer for "block" */
342 int error
; /* error return value */
343 xfs_extnum_t i
=0, j
; /* index into the extents list */
344 xfs_ifork_t
*ifp
; /* fork structure */
345 int level
; /* btree level, for checking */
346 xfs_mount_t
*mp
; /* file system mount structure */
347 __be64
*pp
; /* pointer to block address */
348 xfs_bmbt_rec_t
*ep
; /* pointer to current extent */
349 xfs_bmbt_rec_t last
= {0, 0}; /* last extent in prev block */
350 xfs_bmbt_rec_t
*nextp
; /* pointer to next extent */
353 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
) {
357 /* skip large extent count inodes */
358 if (ip
->i_d
.di_nextents
> 10000)
363 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
364 block
= ifp
->if_broot
;
366 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
368 level
= be16_to_cpu(block
->bb_level
);
370 xfs_check_block(block
, mp
, 1, ifp
->if_broot_bytes
);
371 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, 1, ifp
->if_broot_bytes
);
372 bno
= be64_to_cpu(*pp
);
374 ASSERT(bno
!= NULLFSBLOCK
);
375 ASSERT(XFS_FSB_TO_AGNO(mp
, bno
) < mp
->m_sb
.sb_agcount
);
376 ASSERT(XFS_FSB_TO_AGBNO(mp
, bno
) < mp
->m_sb
.sb_agblocks
);
379 * Go down the tree until leaf level is reached, following the first
380 * pointer (leftmost) at each level.
382 while (level
-- > 0) {
383 /* See if buf is in cur first */
385 bp
= xfs_bmap_get_bp(cur
, XFS_FSB_TO_DADDR(mp
, bno
));
388 error
= xfs_btree_read_bufl(mp
, NULL
, bno
, 0, &bp
,
394 block
= XFS_BUF_TO_BLOCK(bp
);
399 * Check this block for basic sanity (increasing keys and
400 * no duplicate blocks).
403 xfs_check_block(block
, mp
, 0, 0);
404 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, mp
->m_bmap_dmxr
[1]);
405 bno
= be64_to_cpu(*pp
);
406 XFS_WANT_CORRUPTED_GOTO(mp
,
407 XFS_FSB_SANITY_CHECK(mp
, bno
), error0
);
410 xfs_trans_brelse(NULL
, bp
);
415 * Here with bp and block set to the leftmost leaf node in the tree.
420 * Loop over all leaf nodes checking that all extents are in the right order.
423 xfs_fsblock_t nextbno
;
424 xfs_extnum_t num_recs
;
427 num_recs
= xfs_btree_get_numrecs(block
);
430 * Read-ahead the next leaf block, if any.
433 nextbno
= be64_to_cpu(block
->bb_u
.l
.bb_rightsib
);
436 * Check all the extents to make sure they are OK.
437 * If we had a previous block, the last entry should
438 * conform with the first entry in this one.
441 ep
= XFS_BMBT_REC_ADDR(mp
, block
, 1);
443 ASSERT(xfs_bmbt_disk_get_startoff(&last
) +
444 xfs_bmbt_disk_get_blockcount(&last
) <=
445 xfs_bmbt_disk_get_startoff(ep
));
447 for (j
= 1; j
< num_recs
; j
++) {
448 nextp
= XFS_BMBT_REC_ADDR(mp
, block
, j
+ 1);
449 ASSERT(xfs_bmbt_disk_get_startoff(ep
) +
450 xfs_bmbt_disk_get_blockcount(ep
) <=
451 xfs_bmbt_disk_get_startoff(nextp
));
459 xfs_trans_brelse(NULL
, bp
);
463 * If we've reached the end, stop.
465 if (bno
== NULLFSBLOCK
)
469 bp
= xfs_bmap_get_bp(cur
, XFS_FSB_TO_DADDR(mp
, bno
));
472 error
= xfs_btree_read_bufl(mp
, NULL
, bno
, 0, &bp
,
478 block
= XFS_BUF_TO_BLOCK(bp
);
484 xfs_warn(mp
, "%s: at error0", __func__
);
486 xfs_trans_brelse(NULL
, bp
);
488 xfs_warn(mp
, "%s: BAD after btree leaves for %d extents",
490 panic("%s: CORRUPTED BTREE OR SOMETHING", __func__
);
495 * Add bmap trace insert entries for all the contents of the extent records.
498 xfs_bmap_trace_exlist(
499 xfs_inode_t
*ip
, /* incore inode pointer */
500 xfs_extnum_t cnt
, /* count of entries in the list */
501 int whichfork
, /* data or attr fork */
502 unsigned long caller_ip
)
504 xfs_extnum_t idx
; /* extent record index */
505 xfs_ifork_t
*ifp
; /* inode fork pointer */
508 if (whichfork
== XFS_ATTR_FORK
)
509 state
|= BMAP_ATTRFORK
;
511 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
512 ASSERT(cnt
== (ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
)));
513 for (idx
= 0; idx
< cnt
; idx
++)
514 trace_xfs_extlist(ip
, idx
, whichfork
, caller_ip
);
518 * Validate that the bmbt_irecs being returned from bmapi are valid
519 * given the caller's original parameters. Specifically check the
520 * ranges of the returned irecs to ensure that they only extend beyond
521 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
524 xfs_bmap_validate_ret(
528 xfs_bmbt_irec_t
*mval
,
532 int i
; /* index to map values */
534 ASSERT(ret_nmap
<= nmap
);
536 for (i
= 0; i
< ret_nmap
; i
++) {
537 ASSERT(mval
[i
].br_blockcount
> 0);
538 if (!(flags
& XFS_BMAPI_ENTIRE
)) {
539 ASSERT(mval
[i
].br_startoff
>= bno
);
540 ASSERT(mval
[i
].br_blockcount
<= len
);
541 ASSERT(mval
[i
].br_startoff
+ mval
[i
].br_blockcount
<=
544 ASSERT(mval
[i
].br_startoff
< bno
+ len
);
545 ASSERT(mval
[i
].br_startoff
+ mval
[i
].br_blockcount
>
549 mval
[i
- 1].br_startoff
+ mval
[i
- 1].br_blockcount
==
550 mval
[i
].br_startoff
);
551 ASSERT(mval
[i
].br_startblock
!= DELAYSTARTBLOCK
&&
552 mval
[i
].br_startblock
!= HOLESTARTBLOCK
);
553 ASSERT(mval
[i
].br_state
== XFS_EXT_NORM
||
554 mval
[i
].br_state
== XFS_EXT_UNWRITTEN
);
559 #define xfs_bmap_check_leaf_extents(cur, ip, whichfork) do { } while (0)
560 #define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap)
564 * bmap free list manipulation functions
568 * Add the extent to the list of extents to be free at transaction end.
569 * The list is maintained sorted (by block number).
573 xfs_fsblock_t bno
, /* fs block number of extent */
574 xfs_filblks_t len
, /* length of extent */
575 xfs_bmap_free_t
*flist
, /* list of extents */
576 xfs_mount_t
*mp
) /* mount point structure */
578 xfs_bmap_free_item_t
*cur
; /* current (next) element */
579 xfs_bmap_free_item_t
*new; /* new element */
580 xfs_bmap_free_item_t
*prev
; /* previous element */
585 ASSERT(bno
!= NULLFSBLOCK
);
587 ASSERT(len
<= MAXEXTLEN
);
588 ASSERT(!isnullstartblock(bno
));
589 agno
= XFS_FSB_TO_AGNO(mp
, bno
);
590 agbno
= XFS_FSB_TO_AGBNO(mp
, bno
);
591 ASSERT(agno
< mp
->m_sb
.sb_agcount
);
592 ASSERT(agbno
< mp
->m_sb
.sb_agblocks
);
593 ASSERT(len
< mp
->m_sb
.sb_agblocks
);
594 ASSERT(agbno
+ len
<= mp
->m_sb
.sb_agblocks
);
596 ASSERT(xfs_bmap_free_item_zone
!= NULL
);
597 new = kmem_zone_alloc(xfs_bmap_free_item_zone
, KM_SLEEP
);
598 new->xbfi_startblock
= bno
;
599 new->xbfi_blockcount
= (xfs_extlen_t
)len
;
600 for (prev
= NULL
, cur
= flist
->xbf_first
;
602 prev
= cur
, cur
= cur
->xbfi_next
) {
603 if (cur
->xbfi_startblock
>= bno
)
607 prev
->xbfi_next
= new;
609 flist
->xbf_first
= new;
610 new->xbfi_next
= cur
;
615 * Remove the entry "free" from the free item list. Prev points to the
616 * previous entry, unless "free" is the head of the list.
620 xfs_bmap_free_t
*flist
, /* free item list header */
621 xfs_bmap_free_item_t
*prev
, /* previous item on list, if any */
622 xfs_bmap_free_item_t
*free
) /* list item to be freed */
625 prev
->xbfi_next
= free
->xbfi_next
;
627 flist
->xbf_first
= free
->xbfi_next
;
629 kmem_zone_free(xfs_bmap_free_item_zone
, free
);
633 * Free up any items left in the list.
637 xfs_bmap_free_t
*flist
) /* list of bmap_free_items */
639 xfs_bmap_free_item_t
*free
; /* free list item */
640 xfs_bmap_free_item_t
*next
;
642 if (flist
->xbf_count
== 0)
644 ASSERT(flist
->xbf_first
!= NULL
);
645 for (free
= flist
->xbf_first
; free
; free
= next
) {
646 next
= free
->xbfi_next
;
647 xfs_bmap_del_free(flist
, NULL
, free
);
649 ASSERT(flist
->xbf_count
== 0);
653 * Inode fork format manipulation functions
657 * Transform a btree format file with only one leaf node, where the
658 * extents list will fit in the inode, into an extents format file.
659 * Since the file extents are already in-core, all we have to do is
660 * give up the space for the btree root and pitch the leaf block.
662 STATIC
int /* error */
663 xfs_bmap_btree_to_extents(
664 xfs_trans_t
*tp
, /* transaction pointer */
665 xfs_inode_t
*ip
, /* incore inode pointer */
666 xfs_btree_cur_t
*cur
, /* btree cursor */
667 int *logflagsp
, /* inode logging flags */
668 int whichfork
) /* data or attr fork */
671 struct xfs_btree_block
*cblock
;/* child btree block */
672 xfs_fsblock_t cbno
; /* child block number */
673 xfs_buf_t
*cbp
; /* child block's buffer */
674 int error
; /* error return value */
675 xfs_ifork_t
*ifp
; /* inode fork data */
676 xfs_mount_t
*mp
; /* mount point structure */
677 __be64
*pp
; /* ptr to block address */
678 struct xfs_btree_block
*rblock
;/* root btree block */
681 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
682 ASSERT(ifp
->if_flags
& XFS_IFEXTENTS
);
683 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
);
684 rblock
= ifp
->if_broot
;
685 ASSERT(be16_to_cpu(rblock
->bb_level
) == 1);
686 ASSERT(be16_to_cpu(rblock
->bb_numrecs
) == 1);
687 ASSERT(xfs_bmbt_maxrecs(mp
, ifp
->if_broot_bytes
, 0) == 1);
688 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, rblock
, 1, ifp
->if_broot_bytes
);
689 cbno
= be64_to_cpu(*pp
);
692 if ((error
= xfs_btree_check_lptr(cur
, cbno
, 1)))
695 error
= xfs_btree_read_bufl(mp
, tp
, cbno
, 0, &cbp
, XFS_BMAP_BTREE_REF
,
699 cblock
= XFS_BUF_TO_BLOCK(cbp
);
700 if ((error
= xfs_btree_check_block(cur
, cblock
, 0, cbp
)))
702 xfs_bmap_add_free(cbno
, 1, cur
->bc_private
.b
.flist
, mp
);
703 ip
->i_d
.di_nblocks
--;
704 xfs_trans_mod_dquot_byino(tp
, ip
, XFS_TRANS_DQ_BCOUNT
, -1L);
705 xfs_trans_binval(tp
, cbp
);
706 if (cur
->bc_bufs
[0] == cbp
)
707 cur
->bc_bufs
[0] = NULL
;
708 xfs_iroot_realloc(ip
, -1, whichfork
);
709 ASSERT(ifp
->if_broot
== NULL
);
710 ASSERT((ifp
->if_flags
& XFS_IFBROOT
) == 0);
711 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_EXTENTS
);
712 *logflagsp
= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
717 * Convert an extents-format file into a btree-format file.
718 * The new file will have a root block (in the inode) and a single child block.
720 STATIC
int /* error */
721 xfs_bmap_extents_to_btree(
722 xfs_trans_t
*tp
, /* transaction pointer */
723 xfs_inode_t
*ip
, /* incore inode pointer */
724 xfs_fsblock_t
*firstblock
, /* first-block-allocated */
725 xfs_bmap_free_t
*flist
, /* blocks freed in xaction */
726 xfs_btree_cur_t
**curp
, /* cursor returned to caller */
727 int wasdel
, /* converting a delayed alloc */
728 int *logflagsp
, /* inode logging flags */
729 int whichfork
) /* data or attr fork */
731 struct xfs_btree_block
*ablock
; /* allocated (child) bt block */
732 xfs_buf_t
*abp
; /* buffer for ablock */
733 xfs_alloc_arg_t args
; /* allocation arguments */
734 xfs_bmbt_rec_t
*arp
; /* child record pointer */
735 struct xfs_btree_block
*block
; /* btree root block */
736 xfs_btree_cur_t
*cur
; /* bmap btree cursor */
737 xfs_bmbt_rec_host_t
*ep
; /* extent record pointer */
738 int error
; /* error return value */
739 xfs_extnum_t i
, cnt
; /* extent record index */
740 xfs_ifork_t
*ifp
; /* inode fork pointer */
741 xfs_bmbt_key_t
*kp
; /* root block key pointer */
742 xfs_mount_t
*mp
; /* mount structure */
743 xfs_extnum_t nextents
; /* number of file extents */
744 xfs_bmbt_ptr_t
*pp
; /* root block address pointer */
747 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
748 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
);
751 * Make space in the inode incore.
753 xfs_iroot_realloc(ip
, 1, whichfork
);
754 ifp
->if_flags
|= XFS_IFBROOT
;
759 block
= ifp
->if_broot
;
760 if (xfs_sb_version_hascrc(&mp
->m_sb
))
761 xfs_btree_init_block_int(mp
, block
, XFS_BUF_DADDR_NULL
,
762 XFS_BMAP_CRC_MAGIC
, 1, 1, ip
->i_ino
,
763 XFS_BTREE_LONG_PTRS
| XFS_BTREE_CRC_BLOCKS
);
765 xfs_btree_init_block_int(mp
, block
, XFS_BUF_DADDR_NULL
,
766 XFS_BMAP_MAGIC
, 1, 1, ip
->i_ino
,
767 XFS_BTREE_LONG_PTRS
);
770 * Need a cursor. Can't allocate until bb_level is filled in.
772 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
773 cur
->bc_private
.b
.firstblock
= *firstblock
;
774 cur
->bc_private
.b
.flist
= flist
;
775 cur
->bc_private
.b
.flags
= wasdel
? XFS_BTCUR_BPRV_WASDEL
: 0;
777 * Convert to a btree with two levels, one record in root.
779 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_BTREE
);
780 memset(&args
, 0, sizeof(args
));
783 args
.firstblock
= *firstblock
;
784 if (*firstblock
== NULLFSBLOCK
) {
785 args
.type
= XFS_ALLOCTYPE_START_BNO
;
786 args
.fsbno
= XFS_INO_TO_FSB(mp
, ip
->i_ino
);
787 } else if (flist
->xbf_low
) {
788 args
.type
= XFS_ALLOCTYPE_START_BNO
;
789 args
.fsbno
= *firstblock
;
791 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
792 args
.fsbno
= *firstblock
;
794 args
.minlen
= args
.maxlen
= args
.prod
= 1;
795 args
.wasdel
= wasdel
;
797 if ((error
= xfs_alloc_vextent(&args
))) {
798 xfs_iroot_realloc(ip
, -1, whichfork
);
799 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
803 * Allocation can't fail, the space was reserved.
805 ASSERT(args
.fsbno
!= NULLFSBLOCK
);
806 ASSERT(*firstblock
== NULLFSBLOCK
||
807 args
.agno
== XFS_FSB_TO_AGNO(mp
, *firstblock
) ||
809 args
.agno
> XFS_FSB_TO_AGNO(mp
, *firstblock
)));
810 *firstblock
= cur
->bc_private
.b
.firstblock
= args
.fsbno
;
811 cur
->bc_private
.b
.allocated
++;
812 ip
->i_d
.di_nblocks
++;
813 xfs_trans_mod_dquot_byino(tp
, ip
, XFS_TRANS_DQ_BCOUNT
, 1L);
814 abp
= xfs_btree_get_bufl(mp
, tp
, args
.fsbno
, 0);
816 * Fill in the child block.
818 abp
->b_ops
= &xfs_bmbt_buf_ops
;
819 ablock
= XFS_BUF_TO_BLOCK(abp
);
820 if (xfs_sb_version_hascrc(&mp
->m_sb
))
821 xfs_btree_init_block_int(mp
, ablock
, abp
->b_bn
,
822 XFS_BMAP_CRC_MAGIC
, 0, 0, ip
->i_ino
,
823 XFS_BTREE_LONG_PTRS
| XFS_BTREE_CRC_BLOCKS
);
825 xfs_btree_init_block_int(mp
, ablock
, abp
->b_bn
,
826 XFS_BMAP_MAGIC
, 0, 0, ip
->i_ino
,
827 XFS_BTREE_LONG_PTRS
);
829 arp
= XFS_BMBT_REC_ADDR(mp
, ablock
, 1);
830 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
831 for (cnt
= i
= 0; i
< nextents
; i
++) {
832 ep
= xfs_iext_get_ext(ifp
, i
);
833 if (!isnullstartblock(xfs_bmbt_get_startblock(ep
))) {
834 arp
->l0
= cpu_to_be64(ep
->l0
);
835 arp
->l1
= cpu_to_be64(ep
->l1
);
839 ASSERT(cnt
== XFS_IFORK_NEXTENTS(ip
, whichfork
));
840 xfs_btree_set_numrecs(ablock
, cnt
);
843 * Fill in the root key and pointer.
845 kp
= XFS_BMBT_KEY_ADDR(mp
, block
, 1);
846 arp
= XFS_BMBT_REC_ADDR(mp
, ablock
, 1);
847 kp
->br_startoff
= cpu_to_be64(xfs_bmbt_disk_get_startoff(arp
));
848 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, xfs_bmbt_get_maxrecs(cur
,
849 be16_to_cpu(block
->bb_level
)));
850 *pp
= cpu_to_be64(args
.fsbno
);
853 * Do all this logging at the end so that
854 * the root is at the right level.
856 xfs_btree_log_block(cur
, abp
, XFS_BB_ALL_BITS
);
857 xfs_btree_log_recs(cur
, abp
, 1, be16_to_cpu(ablock
->bb_numrecs
));
858 ASSERT(*curp
== NULL
);
860 *logflagsp
= XFS_ILOG_CORE
| xfs_ilog_fbroot(whichfork
);
865 * Convert a local file to an extents file.
866 * This code is out of bounds for data forks of regular files,
867 * since the file data needs to get logged so things will stay consistent.
868 * (The bmap-level manipulations are ok, though).
871 xfs_bmap_local_to_extents_empty(
872 struct xfs_inode
*ip
,
875 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
877 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
878 ASSERT(ifp
->if_bytes
== 0);
879 ASSERT(XFS_IFORK_NEXTENTS(ip
, whichfork
) == 0);
881 xfs_bmap_forkoff_reset(ip
, whichfork
);
882 ifp
->if_flags
&= ~XFS_IFINLINE
;
883 ifp
->if_flags
|= XFS_IFEXTENTS
;
884 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_EXTENTS
);
888 STATIC
int /* error */
889 xfs_bmap_local_to_extents(
890 xfs_trans_t
*tp
, /* transaction pointer */
891 xfs_inode_t
*ip
, /* incore inode pointer */
892 xfs_fsblock_t
*firstblock
, /* first block allocated in xaction */
893 xfs_extlen_t total
, /* total blocks needed by transaction */
894 int *logflagsp
, /* inode logging flags */
896 void (*init_fn
)(struct xfs_trans
*tp
,
898 struct xfs_inode
*ip
,
899 struct xfs_ifork
*ifp
))
902 int flags
; /* logging flags returned */
903 xfs_ifork_t
*ifp
; /* inode fork pointer */
904 xfs_alloc_arg_t args
; /* allocation arguments */
905 xfs_buf_t
*bp
; /* buffer for extent block */
906 xfs_bmbt_rec_host_t
*ep
; /* extent record pointer */
909 * We don't want to deal with the case of keeping inode data inline yet.
910 * So sending the data fork of a regular inode is invalid.
912 ASSERT(!(S_ISREG(VFS_I(ip
)->i_mode
) && whichfork
== XFS_DATA_FORK
));
913 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
914 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
916 if (!ifp
->if_bytes
) {
917 xfs_bmap_local_to_extents_empty(ip
, whichfork
);
918 flags
= XFS_ILOG_CORE
;
924 ASSERT((ifp
->if_flags
& (XFS_IFINLINE
|XFS_IFEXTENTS
|XFS_IFEXTIREC
)) ==
926 memset(&args
, 0, sizeof(args
));
928 args
.mp
= ip
->i_mount
;
929 args
.firstblock
= *firstblock
;
931 * Allocate a block. We know we need only one, since the
932 * file currently fits in an inode.
934 if (*firstblock
== NULLFSBLOCK
) {
935 args
.fsbno
= XFS_INO_TO_FSB(args
.mp
, ip
->i_ino
);
936 args
.type
= XFS_ALLOCTYPE_START_BNO
;
938 args
.fsbno
= *firstblock
;
939 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
942 args
.minlen
= args
.maxlen
= args
.prod
= 1;
943 error
= xfs_alloc_vextent(&args
);
947 /* Can't fail, the space was reserved. */
948 ASSERT(args
.fsbno
!= NULLFSBLOCK
);
949 ASSERT(args
.len
== 1);
950 *firstblock
= args
.fsbno
;
951 bp
= xfs_btree_get_bufl(args
.mp
, tp
, args
.fsbno
, 0);
954 * Initialize the block, copy the data and log the remote buffer.
956 * The callout is responsible for logging because the remote format
957 * might differ from the local format and thus we don't know how much to
958 * log here. Note that init_fn must also set the buffer log item type
961 init_fn(tp
, bp
, ip
, ifp
);
963 /* account for the change in fork size */
964 xfs_idata_realloc(ip
, -ifp
->if_bytes
, whichfork
);
965 xfs_bmap_local_to_extents_empty(ip
, whichfork
);
966 flags
|= XFS_ILOG_CORE
;
968 xfs_iext_add(ifp
, 0, 1);
969 ep
= xfs_iext_get_ext(ifp
, 0);
970 xfs_bmbt_set_allf(ep
, 0, args
.fsbno
, 1, XFS_EXT_NORM
);
971 trace_xfs_bmap_post_update(ip
, 0,
972 whichfork
== XFS_ATTR_FORK
? BMAP_ATTRFORK
: 0,
974 XFS_IFORK_NEXT_SET(ip
, whichfork
, 1);
975 ip
->i_d
.di_nblocks
= 1;
976 xfs_trans_mod_dquot_byino(tp
, ip
,
977 XFS_TRANS_DQ_BCOUNT
, 1L);
978 flags
|= xfs_ilog_fext(whichfork
);
986 * Called from xfs_bmap_add_attrfork to handle btree format files.
988 STATIC
int /* error */
989 xfs_bmap_add_attrfork_btree(
990 xfs_trans_t
*tp
, /* transaction pointer */
991 xfs_inode_t
*ip
, /* incore inode pointer */
992 xfs_fsblock_t
*firstblock
, /* first block allocated */
993 xfs_bmap_free_t
*flist
, /* blocks to free at commit */
994 int *flags
) /* inode logging flags */
996 xfs_btree_cur_t
*cur
; /* btree cursor */
997 int error
; /* error return value */
998 xfs_mount_t
*mp
; /* file system mount struct */
999 int stat
; /* newroot status */
1002 if (ip
->i_df
.if_broot_bytes
<= XFS_IFORK_DSIZE(ip
))
1003 *flags
|= XFS_ILOG_DBROOT
;
1005 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, XFS_DATA_FORK
);
1006 cur
->bc_private
.b
.flist
= flist
;
1007 cur
->bc_private
.b
.firstblock
= *firstblock
;
1008 if ((error
= xfs_bmbt_lookup_ge(cur
, 0, 0, 0, &stat
)))
1010 /* must be at least one entry */
1011 XFS_WANT_CORRUPTED_GOTO(mp
, stat
== 1, error0
);
1012 if ((error
= xfs_btree_new_iroot(cur
, flags
, &stat
)))
1015 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
1018 *firstblock
= cur
->bc_private
.b
.firstblock
;
1019 cur
->bc_private
.b
.allocated
= 0;
1020 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
1024 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
1029 * Called from xfs_bmap_add_attrfork to handle extents format files.
1031 STATIC
int /* error */
1032 xfs_bmap_add_attrfork_extents(
1033 xfs_trans_t
*tp
, /* transaction pointer */
1034 xfs_inode_t
*ip
, /* incore inode pointer */
1035 xfs_fsblock_t
*firstblock
, /* first block allocated */
1036 xfs_bmap_free_t
*flist
, /* blocks to free at commit */
1037 int *flags
) /* inode logging flags */
1039 xfs_btree_cur_t
*cur
; /* bmap btree cursor */
1040 int error
; /* error return value */
1042 if (ip
->i_d
.di_nextents
* sizeof(xfs_bmbt_rec_t
) <= XFS_IFORK_DSIZE(ip
))
1045 error
= xfs_bmap_extents_to_btree(tp
, ip
, firstblock
, flist
, &cur
, 0,
1046 flags
, XFS_DATA_FORK
);
1048 cur
->bc_private
.b
.allocated
= 0;
1049 xfs_btree_del_cursor(cur
,
1050 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
1056 * Called from xfs_bmap_add_attrfork to handle local format files. Each
1057 * different data fork content type needs a different callout to do the
1058 * conversion. Some are basic and only require special block initialisation
1059 * callouts for the data formating, others (directories) are so specialised they
1060 * handle everything themselves.
1062 * XXX (dgc): investigate whether directory conversion can use the generic
1063 * formatting callout. It should be possible - it's just a very complex
1066 STATIC
int /* error */
1067 xfs_bmap_add_attrfork_local(
1068 xfs_trans_t
*tp
, /* transaction pointer */
1069 xfs_inode_t
*ip
, /* incore inode pointer */
1070 xfs_fsblock_t
*firstblock
, /* first block allocated */
1071 xfs_bmap_free_t
*flist
, /* blocks to free at commit */
1072 int *flags
) /* inode logging flags */
1074 xfs_da_args_t dargs
; /* args for dir/attr code */
1076 if (ip
->i_df
.if_bytes
<= XFS_IFORK_DSIZE(ip
))
1079 if (S_ISDIR(VFS_I(ip
)->i_mode
)) {
1080 memset(&dargs
, 0, sizeof(dargs
));
1081 dargs
.geo
= ip
->i_mount
->m_dir_geo
;
1083 dargs
.firstblock
= firstblock
;
1084 dargs
.flist
= flist
;
1085 dargs
.total
= dargs
.geo
->fsbcount
;
1086 dargs
.whichfork
= XFS_DATA_FORK
;
1088 return xfs_dir2_sf_to_block(&dargs
);
1091 if (S_ISLNK(VFS_I(ip
)->i_mode
))
1092 return xfs_bmap_local_to_extents(tp
, ip
, firstblock
, 1,
1093 flags
, XFS_DATA_FORK
,
1094 xfs_symlink_local_to_remote
);
1096 /* should only be called for types that support local format data */
1098 return -EFSCORRUPTED
;
1102 * Convert inode from non-attributed to attributed.
1103 * Must not be in a transaction, ip must not be locked.
1105 int /* error code */
1106 xfs_bmap_add_attrfork(
1107 xfs_inode_t
*ip
, /* incore inode pointer */
1108 int size
, /* space new attribute needs */
1109 int rsvd
) /* xact may use reserved blks */
1111 xfs_fsblock_t firstblock
; /* 1st block/ag allocated */
1112 xfs_bmap_free_t flist
; /* freed extent records */
1113 xfs_mount_t
*mp
; /* mount structure */
1114 xfs_trans_t
*tp
; /* transaction pointer */
1115 int blks
; /* space reservation */
1116 int version
= 1; /* superblock attr version */
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
));
1125 blks
= XFS_ADDAFORK_SPACE_RES(mp
);
1127 error
= xfs_trans_alloc(mp
, &M_RES(mp
)->tr_addafork
, blks
, 0,
1128 rsvd
? XFS_TRANS_RESERVE
: 0, &tp
);
1132 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
1133 error
= xfs_trans_reserve_quota_nblks(tp
, ip
, blks
, 0, rsvd
?
1134 XFS_QMOPT_RES_REGBLKS
| XFS_QMOPT_FORCE_RES
:
1135 XFS_QMOPT_RES_REGBLKS
);
1138 if (XFS_IFORK_Q(ip
))
1140 if (ip
->i_d
.di_aformat
!= XFS_DINODE_FMT_EXTENTS
) {
1142 * For inodes coming from pre-6.2 filesystems.
1144 ASSERT(ip
->i_d
.di_aformat
== 0);
1145 ip
->i_d
.di_aformat
= XFS_DINODE_FMT_EXTENTS
;
1147 ASSERT(ip
->i_d
.di_anextents
== 0);
1149 xfs_trans_ijoin(tp
, ip
, 0);
1150 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
1152 switch (ip
->i_d
.di_format
) {
1153 case XFS_DINODE_FMT_DEV
:
1154 ip
->i_d
.di_forkoff
= roundup(sizeof(xfs_dev_t
), 8) >> 3;
1156 case XFS_DINODE_FMT_UUID
:
1157 ip
->i_d
.di_forkoff
= roundup(sizeof(uuid_t
), 8) >> 3;
1159 case XFS_DINODE_FMT_LOCAL
:
1160 case XFS_DINODE_FMT_EXTENTS
:
1161 case XFS_DINODE_FMT_BTREE
:
1162 ip
->i_d
.di_forkoff
= xfs_attr_shortform_bytesfit(ip
, size
);
1163 if (!ip
->i_d
.di_forkoff
)
1164 ip
->i_d
.di_forkoff
= xfs_default_attroffset(ip
) >> 3;
1165 else if (mp
->m_flags
& XFS_MOUNT_ATTR2
)
1174 ASSERT(ip
->i_afp
== NULL
);
1175 ip
->i_afp
= kmem_zone_zalloc(xfs_ifork_zone
, KM_SLEEP
);
1176 ip
->i_afp
->if_flags
= XFS_IFEXTENTS
;
1178 xfs_bmap_init(&flist
, &firstblock
);
1179 switch (ip
->i_d
.di_format
) {
1180 case XFS_DINODE_FMT_LOCAL
:
1181 error
= xfs_bmap_add_attrfork_local(tp
, ip
, &firstblock
, &flist
,
1184 case XFS_DINODE_FMT_EXTENTS
:
1185 error
= xfs_bmap_add_attrfork_extents(tp
, ip
, &firstblock
,
1188 case XFS_DINODE_FMT_BTREE
:
1189 error
= xfs_bmap_add_attrfork_btree(tp
, ip
, &firstblock
, &flist
,
1197 xfs_trans_log_inode(tp
, ip
, logflags
);
1200 if (!xfs_sb_version_hasattr(&mp
->m_sb
) ||
1201 (!xfs_sb_version_hasattr2(&mp
->m_sb
) && version
== 2)) {
1202 bool log_sb
= false;
1204 spin_lock(&mp
->m_sb_lock
);
1205 if (!xfs_sb_version_hasattr(&mp
->m_sb
)) {
1206 xfs_sb_version_addattr(&mp
->m_sb
);
1209 if (!xfs_sb_version_hasattr2(&mp
->m_sb
) && version
== 2) {
1210 xfs_sb_version_addattr2(&mp
->m_sb
);
1213 spin_unlock(&mp
->m_sb_lock
);
1218 error
= xfs_bmap_finish(&tp
, &flist
, NULL
);
1221 error
= xfs_trans_commit(tp
);
1222 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
1226 xfs_bmap_cancel(&flist
);
1228 xfs_trans_cancel(tp
);
1229 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
1234 * Internal and external extent tree search functions.
1238 * Read in the extents to if_extents.
1239 * All inode fields are set up by caller, we just traverse the btree
1240 * and copy the records in. If the file system cannot contain unwritten
1241 * extents, the records are checked for no "state" flags.
1244 xfs_bmap_read_extents(
1245 xfs_trans_t
*tp
, /* transaction pointer */
1246 xfs_inode_t
*ip
, /* incore inode */
1247 int whichfork
) /* data or attr fork */
1249 struct xfs_btree_block
*block
; /* current btree block */
1250 xfs_fsblock_t bno
; /* block # of "block" */
1251 xfs_buf_t
*bp
; /* buffer for "block" */
1252 int error
; /* error return value */
1253 xfs_exntfmt_t exntf
; /* XFS_EXTFMT_NOSTATE, if checking */
1254 xfs_extnum_t i
, j
; /* index into the extents list */
1255 xfs_ifork_t
*ifp
; /* fork structure */
1256 int level
; /* btree level, for checking */
1257 xfs_mount_t
*mp
; /* file system mount structure */
1258 __be64
*pp
; /* pointer to block address */
1260 xfs_extnum_t room
; /* number of entries there's room for */
1264 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1265 exntf
= (whichfork
!= XFS_DATA_FORK
) ? XFS_EXTFMT_NOSTATE
:
1266 XFS_EXTFMT_INODE(ip
);
1267 block
= ifp
->if_broot
;
1269 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
1271 level
= be16_to_cpu(block
->bb_level
);
1273 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, 1, ifp
->if_broot_bytes
);
1274 bno
= be64_to_cpu(*pp
);
1275 ASSERT(bno
!= NULLFSBLOCK
);
1276 ASSERT(XFS_FSB_TO_AGNO(mp
, bno
) < mp
->m_sb
.sb_agcount
);
1277 ASSERT(XFS_FSB_TO_AGBNO(mp
, bno
) < mp
->m_sb
.sb_agblocks
);
1279 * Go down the tree until leaf level is reached, following the first
1280 * pointer (leftmost) at each level.
1282 while (level
-- > 0) {
1283 error
= xfs_btree_read_bufl(mp
, tp
, bno
, 0, &bp
,
1284 XFS_BMAP_BTREE_REF
, &xfs_bmbt_buf_ops
);
1287 block
= XFS_BUF_TO_BLOCK(bp
);
1290 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, mp
->m_bmap_dmxr
[1]);
1291 bno
= be64_to_cpu(*pp
);
1292 XFS_WANT_CORRUPTED_GOTO(mp
,
1293 XFS_FSB_SANITY_CHECK(mp
, bno
), error0
);
1294 xfs_trans_brelse(tp
, bp
);
1297 * Here with bp and block set to the leftmost leaf node in the tree.
1299 room
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
1302 * Loop over all leaf nodes. Copy information to the extent records.
1305 xfs_bmbt_rec_t
*frp
;
1306 xfs_fsblock_t nextbno
;
1307 xfs_extnum_t num_recs
;
1310 num_recs
= xfs_btree_get_numrecs(block
);
1311 if (unlikely(i
+ num_recs
> room
)) {
1312 ASSERT(i
+ num_recs
<= room
);
1313 xfs_warn(ip
->i_mount
,
1314 "corrupt dinode %Lu, (btree extents).",
1315 (unsigned long long) ip
->i_ino
);
1316 XFS_CORRUPTION_ERROR("xfs_bmap_read_extents(1)",
1317 XFS_ERRLEVEL_LOW
, ip
->i_mount
, block
);
1321 * Read-ahead the next leaf block, if any.
1323 nextbno
= be64_to_cpu(block
->bb_u
.l
.bb_rightsib
);
1324 if (nextbno
!= NULLFSBLOCK
)
1325 xfs_btree_reada_bufl(mp
, nextbno
, 1,
1328 * Copy records into the extent records.
1330 frp
= XFS_BMBT_REC_ADDR(mp
, block
, 1);
1332 for (j
= 0; j
< num_recs
; j
++, i
++, frp
++) {
1333 xfs_bmbt_rec_host_t
*trp
= xfs_iext_get_ext(ifp
, i
);
1334 trp
->l0
= be64_to_cpu(frp
->l0
);
1335 trp
->l1
= be64_to_cpu(frp
->l1
);
1337 if (exntf
== XFS_EXTFMT_NOSTATE
) {
1339 * Check all attribute bmap btree records and
1340 * any "older" data bmap btree records for a
1341 * set bit in the "extent flag" position.
1343 if (unlikely(xfs_check_nostate_extents(ifp
,
1344 start
, num_recs
))) {
1345 XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
1351 xfs_trans_brelse(tp
, bp
);
1354 * If we've reached the end, stop.
1356 if (bno
== NULLFSBLOCK
)
1358 error
= xfs_btree_read_bufl(mp
, tp
, bno
, 0, &bp
,
1359 XFS_BMAP_BTREE_REF
, &xfs_bmbt_buf_ops
);
1362 block
= XFS_BUF_TO_BLOCK(bp
);
1364 ASSERT(i
== (ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
)));
1365 ASSERT(i
== XFS_IFORK_NEXTENTS(ip
, whichfork
));
1366 XFS_BMAP_TRACE_EXLIST(ip
, i
, whichfork
);
1369 xfs_trans_brelse(tp
, bp
);
1370 return -EFSCORRUPTED
;
1375 * Search the extent records for the entry containing block bno.
1376 * If bno lies in a hole, point to the next entry. If bno lies
1377 * past eof, *eofp will be set, and *prevp will contain the last
1378 * entry (null if none). Else, *lastxp will be set to the index
1379 * of the found entry; *gotp will contain the entry.
1381 STATIC xfs_bmbt_rec_host_t
* /* pointer to found extent entry */
1382 xfs_bmap_search_multi_extents(
1383 xfs_ifork_t
*ifp
, /* inode fork pointer */
1384 xfs_fileoff_t bno
, /* block number searched for */
1385 int *eofp
, /* out: end of file found */
1386 xfs_extnum_t
*lastxp
, /* out: last extent index */
1387 xfs_bmbt_irec_t
*gotp
, /* out: extent entry found */
1388 xfs_bmbt_irec_t
*prevp
) /* out: previous extent entry found */
1390 xfs_bmbt_rec_host_t
*ep
; /* extent record pointer */
1391 xfs_extnum_t lastx
; /* last extent index */
1394 * Initialize the extent entry structure to catch access to
1395 * uninitialized br_startblock field.
1397 gotp
->br_startoff
= 0xffa5a5a5a5a5a5a5LL
;
1398 gotp
->br_blockcount
= 0xa55a5a5a5a5a5a5aLL
;
1399 gotp
->br_state
= XFS_EXT_INVALID
;
1400 gotp
->br_startblock
= 0xffffa5a5a5a5a5a5LL
;
1401 prevp
->br_startoff
= NULLFILEOFF
;
1403 ep
= xfs_iext_bno_to_ext(ifp
, bno
, &lastx
);
1405 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, lastx
- 1), prevp
);
1407 if (lastx
< (ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
))) {
1408 xfs_bmbt_get_all(ep
, gotp
);
1422 * Search the extents list for the inode, for the extent containing bno.
1423 * If bno lies in a hole, point to the next entry. If bno lies past eof,
1424 * *eofp will be set, and *prevp will contain the last entry (null if none).
1425 * Else, *lastxp will be set to the index of the found
1426 * entry; *gotp will contain the entry.
1428 STATIC xfs_bmbt_rec_host_t
* /* pointer to found extent entry */
1429 xfs_bmap_search_extents(
1430 xfs_inode_t
*ip
, /* incore inode pointer */
1431 xfs_fileoff_t bno
, /* block number searched for */
1432 int fork
, /* data or attr fork */
1433 int *eofp
, /* out: end of file found */
1434 xfs_extnum_t
*lastxp
, /* out: last extent index */
1435 xfs_bmbt_irec_t
*gotp
, /* out: extent entry found */
1436 xfs_bmbt_irec_t
*prevp
) /* out: previous extent entry found */
1438 xfs_ifork_t
*ifp
; /* inode fork pointer */
1439 xfs_bmbt_rec_host_t
*ep
; /* extent record pointer */
1441 XFS_STATS_INC(ip
->i_mount
, xs_look_exlist
);
1442 ifp
= XFS_IFORK_PTR(ip
, fork
);
1444 ep
= xfs_bmap_search_multi_extents(ifp
, bno
, eofp
, lastxp
, gotp
, prevp
);
1446 if (unlikely(!(gotp
->br_startblock
) && (*lastxp
!= NULLEXTNUM
) &&
1447 !(XFS_IS_REALTIME_INODE(ip
) && fork
== XFS_DATA_FORK
))) {
1448 xfs_alert_tag(ip
->i_mount
, XFS_PTAG_FSBLOCK_ZERO
,
1449 "Access to block zero in inode %llu "
1450 "start_block: %llx start_off: %llx "
1451 "blkcnt: %llx extent-state: %x lastx: %x",
1452 (unsigned long long)ip
->i_ino
,
1453 (unsigned long long)gotp
->br_startblock
,
1454 (unsigned long long)gotp
->br_startoff
,
1455 (unsigned long long)gotp
->br_blockcount
,
1456 gotp
->br_state
, *lastxp
);
1457 *lastxp
= NULLEXTNUM
;
1465 * Returns the file-relative block number of the first unused block(s)
1466 * in the file with at least "len" logically contiguous blocks free.
1467 * This is the lowest-address hole if the file has holes, else the first block
1468 * past the end of file.
1469 * Return 0 if the file is currently local (in-inode).
1472 xfs_bmap_first_unused(
1473 xfs_trans_t
*tp
, /* transaction pointer */
1474 xfs_inode_t
*ip
, /* incore inode */
1475 xfs_extlen_t len
, /* size of hole to find */
1476 xfs_fileoff_t
*first_unused
, /* unused block */
1477 int whichfork
) /* data or attr fork */
1479 int error
; /* error return value */
1480 int idx
; /* extent record index */
1481 xfs_ifork_t
*ifp
; /* inode fork pointer */
1482 xfs_fileoff_t lastaddr
; /* last block number seen */
1483 xfs_fileoff_t lowest
; /* lowest useful block */
1484 xfs_fileoff_t max
; /* starting useful block */
1485 xfs_fileoff_t off
; /* offset for this block */
1486 xfs_extnum_t nextents
; /* number of extent entries */
1488 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
||
1489 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
||
1490 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
1491 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
) {
1495 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1496 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
1497 (error
= xfs_iread_extents(tp
, ip
, whichfork
)))
1499 lowest
= *first_unused
;
1500 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
1501 for (idx
= 0, lastaddr
= 0, max
= lowest
; idx
< nextents
; idx
++) {
1502 xfs_bmbt_rec_host_t
*ep
= xfs_iext_get_ext(ifp
, idx
);
1503 off
= xfs_bmbt_get_startoff(ep
);
1505 * See if the hole before this extent will work.
1507 if (off
>= lowest
+ len
&& off
- max
>= len
) {
1508 *first_unused
= max
;
1511 lastaddr
= off
+ xfs_bmbt_get_blockcount(ep
);
1512 max
= XFS_FILEOFF_MAX(lastaddr
, lowest
);
1514 *first_unused
= max
;
1519 * Returns the file-relative block number of the last block - 1 before
1520 * last_block (input value) in the file.
1521 * This is not based on i_size, it is based on the extent records.
1522 * Returns 0 for local files, as they do not have extent records.
1525 xfs_bmap_last_before(
1526 xfs_trans_t
*tp
, /* transaction pointer */
1527 xfs_inode_t
*ip
, /* incore inode */
1528 xfs_fileoff_t
*last_block
, /* last block */
1529 int whichfork
) /* data or attr fork */
1531 xfs_fileoff_t bno
; /* input file offset */
1532 int eof
; /* hit end of file */
1533 xfs_bmbt_rec_host_t
*ep
; /* pointer to last extent */
1534 int error
; /* error return value */
1535 xfs_bmbt_irec_t got
; /* current extent value */
1536 xfs_ifork_t
*ifp
; /* inode fork pointer */
1537 xfs_extnum_t lastx
; /* last extent used */
1538 xfs_bmbt_irec_t prev
; /* previous extent value */
1540 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
&&
1541 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
1542 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_LOCAL
)
1544 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
) {
1548 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1549 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
1550 (error
= xfs_iread_extents(tp
, ip
, whichfork
)))
1552 bno
= *last_block
- 1;
1553 ep
= xfs_bmap_search_extents(ip
, bno
, whichfork
, &eof
, &lastx
, &got
,
1555 if (eof
|| xfs_bmbt_get_startoff(ep
) > bno
) {
1556 if (prev
.br_startoff
== NULLFILEOFF
)
1559 *last_block
= prev
.br_startoff
+ prev
.br_blockcount
;
1562 * Otherwise *last_block is already the right answer.
1568 xfs_bmap_last_extent(
1569 struct xfs_trans
*tp
,
1570 struct xfs_inode
*ip
,
1572 struct xfs_bmbt_irec
*rec
,
1575 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1579 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
1580 error
= xfs_iread_extents(tp
, ip
, whichfork
);
1585 nextents
= ifp
->if_bytes
/ sizeof(xfs_bmbt_rec_t
);
1586 if (nextents
== 0) {
1591 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, nextents
- 1), rec
);
1597 * Check the last inode extent to determine whether this allocation will result
1598 * in blocks being allocated at the end of the file. When we allocate new data
1599 * blocks at the end of the file which do not start at the previous data block,
1600 * we will try to align the new blocks at stripe unit boundaries.
1602 * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be
1603 * at, or past the EOF.
1607 struct xfs_bmalloca
*bma
,
1610 struct xfs_bmbt_irec rec
;
1615 error
= xfs_bmap_last_extent(NULL
, bma
->ip
, whichfork
, &rec
,
1626 * Check if we are allocation or past the last extent, or at least into
1627 * the last delayed allocated extent.
1629 bma
->aeof
= bma
->offset
>= rec
.br_startoff
+ rec
.br_blockcount
||
1630 (bma
->offset
>= rec
.br_startoff
&&
1631 isnullstartblock(rec
.br_startblock
));
1636 * Returns the file-relative block number of the first block past eof in
1637 * the file. This is not based on i_size, it is based on the extent records.
1638 * Returns 0 for local files, as they do not have extent records.
1641 xfs_bmap_last_offset(
1642 struct xfs_inode
*ip
,
1643 xfs_fileoff_t
*last_block
,
1646 struct xfs_bmbt_irec rec
;
1652 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
)
1655 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
&&
1656 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
1659 error
= xfs_bmap_last_extent(NULL
, ip
, whichfork
, &rec
, &is_empty
);
1660 if (error
|| is_empty
)
1663 *last_block
= rec
.br_startoff
+ rec
.br_blockcount
;
1668 * Returns whether the selected fork of the inode has exactly one
1669 * block or not. For the data fork we check this matches di_size,
1670 * implying the file's range is 0..bsize-1.
1672 int /* 1=>1 block, 0=>otherwise */
1674 xfs_inode_t
*ip
, /* incore inode */
1675 int whichfork
) /* data or attr fork */
1677 xfs_bmbt_rec_host_t
*ep
; /* ptr to fork's extent */
1678 xfs_ifork_t
*ifp
; /* inode fork pointer */
1679 int rval
; /* return value */
1680 xfs_bmbt_irec_t s
; /* internal version of extent */
1683 if (whichfork
== XFS_DATA_FORK
)
1684 return XFS_ISIZE(ip
) == ip
->i_mount
->m_sb
.sb_blocksize
;
1686 if (XFS_IFORK_NEXTENTS(ip
, whichfork
) != 1)
1688 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
1690 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1691 ASSERT(ifp
->if_flags
& XFS_IFEXTENTS
);
1692 ep
= xfs_iext_get_ext(ifp
, 0);
1693 xfs_bmbt_get_all(ep
, &s
);
1694 rval
= s
.br_startoff
== 0 && s
.br_blockcount
== 1;
1695 if (rval
&& whichfork
== XFS_DATA_FORK
)
1696 ASSERT(XFS_ISIZE(ip
) == ip
->i_mount
->m_sb
.sb_blocksize
);
1701 * Extent tree manipulation functions used during allocation.
1705 * Convert a delayed allocation to a real allocation.
1707 STATIC
int /* error */
1708 xfs_bmap_add_extent_delay_real(
1709 struct xfs_bmalloca
*bma
)
1711 struct xfs_bmbt_irec
*new = &bma
->got
;
1712 int diff
; /* temp value */
1713 xfs_bmbt_rec_host_t
*ep
; /* extent entry for idx */
1714 int error
; /* error return value */
1715 int i
; /* temp state */
1716 xfs_ifork_t
*ifp
; /* inode fork pointer */
1717 xfs_fileoff_t new_endoff
; /* end offset of new entry */
1718 xfs_bmbt_irec_t r
[3]; /* neighbor extent entries */
1719 /* left is 0, right is 1, prev is 2 */
1720 int rval
=0; /* return value (logging flags) */
1721 int state
= 0;/* state bits, accessed thru macros */
1722 xfs_filblks_t da_new
; /* new count del alloc blocks used */
1723 xfs_filblks_t da_old
; /* old count del alloc blocks used */
1724 xfs_filblks_t temp
=0; /* value for da_new calculations */
1725 xfs_filblks_t temp2
=0;/* value for da_new calculations */
1726 int tmp_rval
; /* partial logging flags */
1727 int whichfork
= XFS_DATA_FORK
;
1728 struct xfs_mount
*mp
;
1730 mp
= bma
->ip
->i_mount
;
1731 ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
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
< ifp
->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
, whichfork
)) {
2022 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
2023 bma
->firstblock
, bma
->flist
,
2024 &bma
->cur
, 1, &tmp_rval
, whichfork
);
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
, whichfork
)) {
2106 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
2107 bma
->firstblock
, bma
->flist
, &bma
->cur
, 1,
2108 &tmp_rval
, whichfork
);
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
, whichfork
)) {
2175 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
2176 bma
->firstblock
, bma
->flist
, &bma
->cur
,
2177 1, &tmp_rval
, whichfork
);
2182 temp
= xfs_bmap_worst_indlen(bma
->ip
, temp
);
2183 temp2
= xfs_bmap_worst_indlen(bma
->ip
, temp2
);
2184 diff
= (int)(temp
+ temp2
- startblockval(PREV
.br_startblock
) -
2185 (bma
->cur
? bma
->cur
->bc_private
.b
.allocated
: 0));
2187 error
= xfs_mod_fdblocks(bma
->ip
->i_mount
,
2188 -((int64_t)diff
), false);
2194 ep
= xfs_iext_get_ext(ifp
, bma
->idx
);
2195 xfs_bmbt_set_startblock(ep
, nullstartblock((int)temp
));
2196 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2197 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
+ 2, state
, _THIS_IP_
);
2198 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp
, bma
->idx
+ 2),
2199 nullstartblock((int)temp2
));
2200 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
+ 2, state
, _THIS_IP_
);
2203 da_new
= temp
+ temp2
;
2206 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2207 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2208 case BMAP_LEFT_FILLING
| BMAP_RIGHT_CONTIG
:
2209 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
2210 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2211 case BMAP_LEFT_CONTIG
:
2212 case BMAP_RIGHT_CONTIG
:
2214 * These cases are all impossible.
2219 /* convert to a btree if necessary */
2220 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
2221 int tmp_logflags
; /* partial log flag return val */
2223 ASSERT(bma
->cur
== NULL
);
2224 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
2225 bma
->firstblock
, bma
->flist
, &bma
->cur
,
2226 da_old
> 0, &tmp_logflags
, whichfork
);
2227 bma
->logflags
|= tmp_logflags
;
2232 /* adjust for changes in reserved delayed indirect blocks */
2233 if (da_old
|| da_new
) {
2236 temp
+= bma
->cur
->bc_private
.b
.allocated
;
2237 ASSERT(temp
<= da_old
);
2239 xfs_mod_fdblocks(bma
->ip
->i_mount
,
2240 (int64_t)(da_old
- temp
), false);
2243 /* clear out the allocated field, done with it now in any case. */
2245 bma
->cur
->bc_private
.b
.allocated
= 0;
2247 xfs_bmap_check_leaf_extents(bma
->cur
, bma
->ip
, whichfork
);
2249 bma
->logflags
|= rval
;
2257 * Convert an unwritten allocation to a real allocation or vice versa.
2259 STATIC
int /* error */
2260 xfs_bmap_add_extent_unwritten_real(
2261 struct xfs_trans
*tp
,
2262 xfs_inode_t
*ip
, /* incore inode pointer */
2263 xfs_extnum_t
*idx
, /* extent number to update/insert */
2264 xfs_btree_cur_t
**curp
, /* if *curp is null, not a btree */
2265 xfs_bmbt_irec_t
*new, /* new data to add to file extents */
2266 xfs_fsblock_t
*first
, /* pointer to firstblock variable */
2267 xfs_bmap_free_t
*flist
, /* list of extents to be freed */
2268 int *logflagsp
) /* inode logging flags */
2270 xfs_btree_cur_t
*cur
; /* btree cursor */
2271 xfs_bmbt_rec_host_t
*ep
; /* extent entry for idx */
2272 int error
; /* error return value */
2273 int i
; /* temp state */
2274 xfs_ifork_t
*ifp
; /* inode fork pointer */
2275 xfs_fileoff_t new_endoff
; /* end offset of new entry */
2276 xfs_exntst_t newext
; /* new extent state */
2277 xfs_exntst_t oldext
; /* old extent state */
2278 xfs_bmbt_irec_t r
[3]; /* neighbor extent entries */
2279 /* left is 0, right is 1, prev is 2 */
2280 int rval
=0; /* return value (logging flags) */
2281 int state
= 0;/* state bits, accessed thru macros */
2282 struct xfs_mount
*mp
= tp
->t_mountp
;
2287 ifp
= XFS_IFORK_PTR(ip
, XFS_DATA_FORK
);
2290 ASSERT(*idx
<= ifp
->if_bytes
/ sizeof(struct xfs_bmbt_rec
));
2291 ASSERT(!isnullstartblock(new->br_startblock
));
2293 XFS_STATS_INC(mp
, xs_add_exlist
);
2300 * Set up a bunch of variables to make the tests simpler.
2303 ep
= xfs_iext_get_ext(ifp
, *idx
);
2304 xfs_bmbt_get_all(ep
, &PREV
);
2305 newext
= new->br_state
;
2306 oldext
= (newext
== XFS_EXT_UNWRITTEN
) ?
2307 XFS_EXT_NORM
: XFS_EXT_UNWRITTEN
;
2308 ASSERT(PREV
.br_state
== oldext
);
2309 new_endoff
= new->br_startoff
+ new->br_blockcount
;
2310 ASSERT(PREV
.br_startoff
<= new->br_startoff
);
2311 ASSERT(PREV
.br_startoff
+ PREV
.br_blockcount
>= new_endoff
);
2314 * Set flags determining what part of the previous oldext allocation
2315 * extent is being replaced by a newext allocation.
2317 if (PREV
.br_startoff
== new->br_startoff
)
2318 state
|= BMAP_LEFT_FILLING
;
2319 if (PREV
.br_startoff
+ PREV
.br_blockcount
== new_endoff
)
2320 state
|= BMAP_RIGHT_FILLING
;
2323 * Check and set flags if this segment has a left neighbor.
2324 * Don't set contiguous if the combined extent would be too large.
2327 state
|= BMAP_LEFT_VALID
;
2328 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, *idx
- 1), &LEFT
);
2330 if (isnullstartblock(LEFT
.br_startblock
))
2331 state
|= BMAP_LEFT_DELAY
;
2334 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
2335 LEFT
.br_startoff
+ LEFT
.br_blockcount
== new->br_startoff
&&
2336 LEFT
.br_startblock
+ LEFT
.br_blockcount
== new->br_startblock
&&
2337 LEFT
.br_state
== newext
&&
2338 LEFT
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
2339 state
|= BMAP_LEFT_CONTIG
;
2342 * Check and set flags if this segment has a right neighbor.
2343 * Don't set contiguous if the combined extent would be too large.
2344 * Also check for all-three-contiguous being too large.
2346 if (*idx
< ip
->i_df
.if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
) - 1) {
2347 state
|= BMAP_RIGHT_VALID
;
2348 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, *idx
+ 1), &RIGHT
);
2349 if (isnullstartblock(RIGHT
.br_startblock
))
2350 state
|= BMAP_RIGHT_DELAY
;
2353 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
2354 new_endoff
== RIGHT
.br_startoff
&&
2355 new->br_startblock
+ new->br_blockcount
== RIGHT
.br_startblock
&&
2356 newext
== RIGHT
.br_state
&&
2357 new->br_blockcount
+ RIGHT
.br_blockcount
<= MAXEXTLEN
&&
2358 ((state
& (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
2359 BMAP_RIGHT_FILLING
)) !=
2360 (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
2361 BMAP_RIGHT_FILLING
) ||
2362 LEFT
.br_blockcount
+ new->br_blockcount
+ RIGHT
.br_blockcount
2364 state
|= BMAP_RIGHT_CONTIG
;
2367 * Switch out based on the FILLING and CONTIG state bits.
2369 switch (state
& (BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
2370 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
)) {
2371 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
2372 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2374 * Setting all of a previous oldext extent to newext.
2375 * The left and right neighbors are both contiguous with new.
2379 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2380 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
),
2381 LEFT
.br_blockcount
+ PREV
.br_blockcount
+
2382 RIGHT
.br_blockcount
);
2383 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2385 xfs_iext_remove(ip
, *idx
+ 1, 2, state
);
2386 ip
->i_d
.di_nextents
-= 2;
2388 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2390 rval
= XFS_ILOG_CORE
;
2391 if ((error
= xfs_bmbt_lookup_eq(cur
, RIGHT
.br_startoff
,
2392 RIGHT
.br_startblock
,
2393 RIGHT
.br_blockcount
, &i
)))
2395 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2396 if ((error
= xfs_btree_delete(cur
, &i
)))
2398 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2399 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2401 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2402 if ((error
= xfs_btree_delete(cur
, &i
)))
2404 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2405 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2407 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2408 if ((error
= xfs_bmbt_update(cur
, LEFT
.br_startoff
,
2410 LEFT
.br_blockcount
+ PREV
.br_blockcount
+
2411 RIGHT
.br_blockcount
, LEFT
.br_state
)))
2416 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
2418 * Setting all of a previous oldext extent to newext.
2419 * The left neighbor is contiguous, the right is not.
2423 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2424 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
),
2425 LEFT
.br_blockcount
+ PREV
.br_blockcount
);
2426 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2428 xfs_iext_remove(ip
, *idx
+ 1, 1, state
);
2429 ip
->i_d
.di_nextents
--;
2431 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2433 rval
= XFS_ILOG_CORE
;
2434 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2435 PREV
.br_startblock
, PREV
.br_blockcount
,
2438 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2439 if ((error
= xfs_btree_delete(cur
, &i
)))
2441 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2442 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2444 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2445 if ((error
= xfs_bmbt_update(cur
, LEFT
.br_startoff
,
2447 LEFT
.br_blockcount
+ PREV
.br_blockcount
,
2453 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2455 * Setting all of a previous oldext extent to newext.
2456 * The right neighbor is contiguous, the left is not.
2458 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2459 xfs_bmbt_set_blockcount(ep
,
2460 PREV
.br_blockcount
+ RIGHT
.br_blockcount
);
2461 xfs_bmbt_set_state(ep
, newext
);
2462 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2463 xfs_iext_remove(ip
, *idx
+ 1, 1, state
);
2464 ip
->i_d
.di_nextents
--;
2466 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2468 rval
= XFS_ILOG_CORE
;
2469 if ((error
= xfs_bmbt_lookup_eq(cur
, RIGHT
.br_startoff
,
2470 RIGHT
.br_startblock
,
2471 RIGHT
.br_blockcount
, &i
)))
2473 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2474 if ((error
= xfs_btree_delete(cur
, &i
)))
2476 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2477 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2479 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2480 if ((error
= xfs_bmbt_update(cur
, new->br_startoff
,
2482 new->br_blockcount
+ RIGHT
.br_blockcount
,
2488 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
2490 * Setting all of a previous oldext extent to newext.
2491 * Neither the left nor right neighbors are contiguous with
2494 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2495 xfs_bmbt_set_state(ep
, newext
);
2496 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2499 rval
= XFS_ILOG_DEXT
;
2502 if ((error
= xfs_bmbt_lookup_eq(cur
, new->br_startoff
,
2503 new->br_startblock
, new->br_blockcount
,
2506 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2507 if ((error
= xfs_bmbt_update(cur
, new->br_startoff
,
2508 new->br_startblock
, new->br_blockcount
,
2514 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
:
2516 * Setting the first part of a previous oldext extent to newext.
2517 * The left neighbor is contiguous.
2519 trace_xfs_bmap_pre_update(ip
, *idx
- 1, state
, _THIS_IP_
);
2520 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
- 1),
2521 LEFT
.br_blockcount
+ new->br_blockcount
);
2522 xfs_bmbt_set_startoff(ep
,
2523 PREV
.br_startoff
+ new->br_blockcount
);
2524 trace_xfs_bmap_post_update(ip
, *idx
- 1, state
, _THIS_IP_
);
2526 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2527 xfs_bmbt_set_startblock(ep
,
2528 new->br_startblock
+ new->br_blockcount
);
2529 xfs_bmbt_set_blockcount(ep
,
2530 PREV
.br_blockcount
- new->br_blockcount
);
2531 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2536 rval
= XFS_ILOG_DEXT
;
2539 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2540 PREV
.br_startblock
, PREV
.br_blockcount
,
2543 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2544 if ((error
= xfs_bmbt_update(cur
,
2545 PREV
.br_startoff
+ new->br_blockcount
,
2546 PREV
.br_startblock
+ new->br_blockcount
,
2547 PREV
.br_blockcount
- new->br_blockcount
,
2550 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2552 error
= xfs_bmbt_update(cur
, LEFT
.br_startoff
,
2554 LEFT
.br_blockcount
+ new->br_blockcount
,
2561 case BMAP_LEFT_FILLING
:
2563 * Setting the first part of a previous oldext extent to newext.
2564 * The left neighbor is not contiguous.
2566 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2567 ASSERT(ep
&& xfs_bmbt_get_state(ep
) == oldext
);
2568 xfs_bmbt_set_startoff(ep
, new_endoff
);
2569 xfs_bmbt_set_blockcount(ep
,
2570 PREV
.br_blockcount
- new->br_blockcount
);
2571 xfs_bmbt_set_startblock(ep
,
2572 new->br_startblock
+ new->br_blockcount
);
2573 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2575 xfs_iext_insert(ip
, *idx
, 1, new, state
);
2576 ip
->i_d
.di_nextents
++;
2578 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2580 rval
= XFS_ILOG_CORE
;
2581 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2582 PREV
.br_startblock
, PREV
.br_blockcount
,
2585 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2586 if ((error
= xfs_bmbt_update(cur
,
2587 PREV
.br_startoff
+ new->br_blockcount
,
2588 PREV
.br_startblock
+ new->br_blockcount
,
2589 PREV
.br_blockcount
- new->br_blockcount
,
2592 cur
->bc_rec
.b
= *new;
2593 if ((error
= xfs_btree_insert(cur
, &i
)))
2595 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2599 case BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2601 * Setting the last part of a previous oldext extent to newext.
2602 * The right neighbor is contiguous with the new allocation.
2604 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2605 xfs_bmbt_set_blockcount(ep
,
2606 PREV
.br_blockcount
- new->br_blockcount
);
2607 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2611 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2612 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp
, *idx
),
2613 new->br_startoff
, new->br_startblock
,
2614 new->br_blockcount
+ RIGHT
.br_blockcount
, newext
);
2615 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2618 rval
= XFS_ILOG_DEXT
;
2621 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2623 PREV
.br_blockcount
, &i
)))
2625 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2626 if ((error
= xfs_bmbt_update(cur
, PREV
.br_startoff
,
2628 PREV
.br_blockcount
- new->br_blockcount
,
2631 if ((error
= xfs_btree_increment(cur
, 0, &i
)))
2633 if ((error
= xfs_bmbt_update(cur
, new->br_startoff
,
2635 new->br_blockcount
+ RIGHT
.br_blockcount
,
2641 case BMAP_RIGHT_FILLING
:
2643 * Setting the last part of a previous oldext extent to newext.
2644 * The right neighbor is not contiguous.
2646 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2647 xfs_bmbt_set_blockcount(ep
,
2648 PREV
.br_blockcount
- new->br_blockcount
);
2649 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2652 xfs_iext_insert(ip
, *idx
, 1, new, state
);
2654 ip
->i_d
.di_nextents
++;
2656 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2658 rval
= XFS_ILOG_CORE
;
2659 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2660 PREV
.br_startblock
, PREV
.br_blockcount
,
2663 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2664 if ((error
= xfs_bmbt_update(cur
, PREV
.br_startoff
,
2666 PREV
.br_blockcount
- new->br_blockcount
,
2669 if ((error
= xfs_bmbt_lookup_eq(cur
, new->br_startoff
,
2670 new->br_startblock
, new->br_blockcount
,
2673 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
2674 cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
2675 if ((error
= xfs_btree_insert(cur
, &i
)))
2677 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2683 * Setting the middle part of a previous oldext extent to
2684 * newext. Contiguity is impossible here.
2685 * One extent becomes three extents.
2687 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2688 xfs_bmbt_set_blockcount(ep
,
2689 new->br_startoff
- PREV
.br_startoff
);
2690 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2693 r
[1].br_startoff
= new_endoff
;
2694 r
[1].br_blockcount
=
2695 PREV
.br_startoff
+ PREV
.br_blockcount
- new_endoff
;
2696 r
[1].br_startblock
= new->br_startblock
+ new->br_blockcount
;
2697 r
[1].br_state
= oldext
;
2700 xfs_iext_insert(ip
, *idx
, 2, &r
[0], state
);
2702 ip
->i_d
.di_nextents
+= 2;
2704 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2706 rval
= XFS_ILOG_CORE
;
2707 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2708 PREV
.br_startblock
, PREV
.br_blockcount
,
2711 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2712 /* new right extent - oldext */
2713 if ((error
= xfs_bmbt_update(cur
, r
[1].br_startoff
,
2714 r
[1].br_startblock
, r
[1].br_blockcount
,
2717 /* new left extent - oldext */
2718 cur
->bc_rec
.b
= PREV
;
2719 cur
->bc_rec
.b
.br_blockcount
=
2720 new->br_startoff
- PREV
.br_startoff
;
2721 if ((error
= xfs_btree_insert(cur
, &i
)))
2723 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2725 * Reset the cursor to the position of the new extent
2726 * we are about to insert as we can't trust it after
2727 * the previous insert.
2729 if ((error
= xfs_bmbt_lookup_eq(cur
, new->br_startoff
,
2730 new->br_startblock
, new->br_blockcount
,
2733 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
2734 /* new middle extent - newext */
2735 cur
->bc_rec
.b
.br_state
= new->br_state
;
2736 if ((error
= xfs_btree_insert(cur
, &i
)))
2738 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2742 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2743 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2744 case BMAP_LEFT_FILLING
| BMAP_RIGHT_CONTIG
:
2745 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
2746 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2747 case BMAP_LEFT_CONTIG
:
2748 case BMAP_RIGHT_CONTIG
:
2750 * These cases are all impossible.
2755 /* convert to a btree if necessary */
2756 if (xfs_bmap_needs_btree(ip
, XFS_DATA_FORK
)) {
2757 int tmp_logflags
; /* partial log flag return val */
2759 ASSERT(cur
== NULL
);
2760 error
= xfs_bmap_extents_to_btree(tp
, ip
, first
, flist
, &cur
,
2761 0, &tmp_logflags
, XFS_DATA_FORK
);
2762 *logflagsp
|= tmp_logflags
;
2767 /* clear out the allocated field, done with it now in any case. */
2769 cur
->bc_private
.b
.allocated
= 0;
2773 xfs_bmap_check_leaf_extents(*curp
, ip
, XFS_DATA_FORK
);
2783 * Convert a hole to a delayed allocation.
2786 xfs_bmap_add_extent_hole_delay(
2787 xfs_inode_t
*ip
, /* incore inode pointer */
2788 xfs_extnum_t
*idx
, /* extent number to update/insert */
2789 xfs_bmbt_irec_t
*new) /* new data to add to file extents */
2791 xfs_ifork_t
*ifp
; /* inode fork pointer */
2792 xfs_bmbt_irec_t left
; /* left neighbor extent entry */
2793 xfs_filblks_t newlen
=0; /* new indirect size */
2794 xfs_filblks_t oldlen
=0; /* old indirect size */
2795 xfs_bmbt_irec_t right
; /* right neighbor extent entry */
2796 int state
; /* state bits, accessed thru macros */
2797 xfs_filblks_t temp
=0; /* temp for indirect calculations */
2799 ifp
= XFS_IFORK_PTR(ip
, XFS_DATA_FORK
);
2801 ASSERT(isnullstartblock(new->br_startblock
));
2804 * Check and set flags if this segment has a left neighbor
2807 state
|= BMAP_LEFT_VALID
;
2808 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, *idx
- 1), &left
);
2810 if (isnullstartblock(left
.br_startblock
))
2811 state
|= BMAP_LEFT_DELAY
;
2815 * Check and set flags if the current (right) segment exists.
2816 * If it doesn't exist, we're converting the hole at end-of-file.
2818 if (*idx
< ip
->i_df
.if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
)) {
2819 state
|= BMAP_RIGHT_VALID
;
2820 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, *idx
), &right
);
2822 if (isnullstartblock(right
.br_startblock
))
2823 state
|= BMAP_RIGHT_DELAY
;
2827 * Set contiguity flags on the left and right neighbors.
2828 * Don't let extents get too large, even if the pieces are contiguous.
2830 if ((state
& BMAP_LEFT_VALID
) && (state
& BMAP_LEFT_DELAY
) &&
2831 left
.br_startoff
+ left
.br_blockcount
== new->br_startoff
&&
2832 left
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
2833 state
|= BMAP_LEFT_CONTIG
;
2835 if ((state
& BMAP_RIGHT_VALID
) && (state
& BMAP_RIGHT_DELAY
) &&
2836 new->br_startoff
+ new->br_blockcount
== right
.br_startoff
&&
2837 new->br_blockcount
+ right
.br_blockcount
<= MAXEXTLEN
&&
2838 (!(state
& BMAP_LEFT_CONTIG
) ||
2839 (left
.br_blockcount
+ new->br_blockcount
+
2840 right
.br_blockcount
<= MAXEXTLEN
)))
2841 state
|= BMAP_RIGHT_CONTIG
;
2844 * Switch out based on the contiguity flags.
2846 switch (state
& (BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
)) {
2847 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2849 * New allocation is contiguous with delayed allocations
2850 * on the left and on the right.
2851 * Merge all three into a single extent record.
2854 temp
= left
.br_blockcount
+ new->br_blockcount
+
2855 right
.br_blockcount
;
2857 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2858 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
), temp
);
2859 oldlen
= startblockval(left
.br_startblock
) +
2860 startblockval(new->br_startblock
) +
2861 startblockval(right
.br_startblock
);
2862 newlen
= xfs_bmap_worst_indlen(ip
, temp
);
2863 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp
, *idx
),
2864 nullstartblock((int)newlen
));
2865 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2867 xfs_iext_remove(ip
, *idx
+ 1, 1, state
);
2870 case BMAP_LEFT_CONTIG
:
2872 * New allocation is contiguous with a delayed allocation
2874 * Merge the new allocation with the left neighbor.
2877 temp
= left
.br_blockcount
+ new->br_blockcount
;
2879 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2880 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
), temp
);
2881 oldlen
= startblockval(left
.br_startblock
) +
2882 startblockval(new->br_startblock
);
2883 newlen
= xfs_bmap_worst_indlen(ip
, temp
);
2884 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp
, *idx
),
2885 nullstartblock((int)newlen
));
2886 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2889 case BMAP_RIGHT_CONTIG
:
2891 * New allocation is contiguous with a delayed allocation
2893 * Merge the new allocation with the right neighbor.
2895 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2896 temp
= new->br_blockcount
+ right
.br_blockcount
;
2897 oldlen
= startblockval(new->br_startblock
) +
2898 startblockval(right
.br_startblock
);
2899 newlen
= xfs_bmap_worst_indlen(ip
, temp
);
2900 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp
, *idx
),
2902 nullstartblock((int)newlen
), temp
, right
.br_state
);
2903 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2908 * New allocation is not contiguous with another
2909 * delayed allocation.
2910 * Insert a new entry.
2912 oldlen
= newlen
= 0;
2913 xfs_iext_insert(ip
, *idx
, 1, new, state
);
2916 if (oldlen
!= newlen
) {
2917 ASSERT(oldlen
> newlen
);
2918 xfs_mod_fdblocks(ip
->i_mount
, (int64_t)(oldlen
- newlen
),
2921 * Nothing to do for disk quota accounting here.
2927 * Convert a hole to a real allocation.
2929 STATIC
int /* error */
2930 xfs_bmap_add_extent_hole_real(
2931 struct xfs_bmalloca
*bma
,
2934 struct xfs_bmbt_irec
*new = &bma
->got
;
2935 int error
; /* error return value */
2936 int i
; /* temp state */
2937 xfs_ifork_t
*ifp
; /* inode fork pointer */
2938 xfs_bmbt_irec_t left
; /* left neighbor extent entry */
2939 xfs_bmbt_irec_t right
; /* right neighbor extent entry */
2940 int rval
=0; /* return value (logging flags) */
2941 int state
; /* state bits, accessed thru macros */
2942 struct xfs_mount
*mp
;
2944 mp
= bma
->ip
->i_mount
;
2945 ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
2947 ASSERT(bma
->idx
>= 0);
2948 ASSERT(bma
->idx
<= ifp
->if_bytes
/ sizeof(struct xfs_bmbt_rec
));
2949 ASSERT(!isnullstartblock(new->br_startblock
));
2951 !(bma
->cur
->bc_private
.b
.flags
& XFS_BTCUR_BPRV_WASDEL
));
2953 XFS_STATS_INC(mp
, xs_add_exlist
);
2956 if (whichfork
== XFS_ATTR_FORK
)
2957 state
|= BMAP_ATTRFORK
;
2960 * Check and set flags if this segment has a left neighbor.
2963 state
|= BMAP_LEFT_VALID
;
2964 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
- 1), &left
);
2965 if (isnullstartblock(left
.br_startblock
))
2966 state
|= BMAP_LEFT_DELAY
;
2970 * Check and set flags if this segment has a current value.
2971 * Not true if we're inserting into the "hole" at eof.
2973 if (bma
->idx
< ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
)) {
2974 state
|= BMAP_RIGHT_VALID
;
2975 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
), &right
);
2976 if (isnullstartblock(right
.br_startblock
))
2977 state
|= BMAP_RIGHT_DELAY
;
2981 * We're inserting a real allocation between "left" and "right".
2982 * Set the contiguity flags. Don't let extents get too large.
2984 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
2985 left
.br_startoff
+ left
.br_blockcount
== new->br_startoff
&&
2986 left
.br_startblock
+ left
.br_blockcount
== new->br_startblock
&&
2987 left
.br_state
== new->br_state
&&
2988 left
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
2989 state
|= BMAP_LEFT_CONTIG
;
2991 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
2992 new->br_startoff
+ new->br_blockcount
== right
.br_startoff
&&
2993 new->br_startblock
+ new->br_blockcount
== right
.br_startblock
&&
2994 new->br_state
== right
.br_state
&&
2995 new->br_blockcount
+ right
.br_blockcount
<= MAXEXTLEN
&&
2996 (!(state
& BMAP_LEFT_CONTIG
) ||
2997 left
.br_blockcount
+ new->br_blockcount
+
2998 right
.br_blockcount
<= MAXEXTLEN
))
2999 state
|= BMAP_RIGHT_CONTIG
;
3003 * Select which case we're in here, and implement it.
3005 switch (state
& (BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
)) {
3006 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
3008 * New allocation is contiguous with real allocations on the
3009 * left and on the right.
3010 * Merge all three into a single extent record.
3013 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
3014 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, bma
->idx
),
3015 left
.br_blockcount
+ new->br_blockcount
+
3016 right
.br_blockcount
);
3017 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
3019 xfs_iext_remove(bma
->ip
, bma
->idx
+ 1, 1, state
);
3021 XFS_IFORK_NEXT_SET(bma
->ip
, whichfork
,
3022 XFS_IFORK_NEXTENTS(bma
->ip
, whichfork
) - 1);
3023 if (bma
->cur
== NULL
) {
3024 rval
= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
3026 rval
= XFS_ILOG_CORE
;
3027 error
= xfs_bmbt_lookup_eq(bma
->cur
, right
.br_startoff
,
3028 right
.br_startblock
, right
.br_blockcount
,
3032 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
3033 error
= xfs_btree_delete(bma
->cur
, &i
);
3036 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
3037 error
= xfs_btree_decrement(bma
->cur
, 0, &i
);
3040 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
3041 error
= xfs_bmbt_update(bma
->cur
, left
.br_startoff
,
3043 left
.br_blockcount
+
3044 new->br_blockcount
+
3045 right
.br_blockcount
,
3052 case BMAP_LEFT_CONTIG
:
3054 * New allocation is contiguous with a real allocation
3056 * Merge the new allocation with the left neighbor.
3059 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
3060 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, bma
->idx
),
3061 left
.br_blockcount
+ new->br_blockcount
);
3062 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
3064 if (bma
->cur
== NULL
) {
3065 rval
= xfs_ilog_fext(whichfork
);
3068 error
= xfs_bmbt_lookup_eq(bma
->cur
, left
.br_startoff
,
3069 left
.br_startblock
, left
.br_blockcount
,
3073 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
3074 error
= xfs_bmbt_update(bma
->cur
, left
.br_startoff
,
3076 left
.br_blockcount
+
3084 case BMAP_RIGHT_CONTIG
:
3086 * New allocation is contiguous with a real allocation
3088 * Merge the new allocation with the right neighbor.
3090 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
3091 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp
, bma
->idx
),
3092 new->br_startoff
, new->br_startblock
,
3093 new->br_blockcount
+ right
.br_blockcount
,
3095 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
3097 if (bma
->cur
== NULL
) {
3098 rval
= xfs_ilog_fext(whichfork
);
3101 error
= xfs_bmbt_lookup_eq(bma
->cur
,
3103 right
.br_startblock
,
3104 right
.br_blockcount
, &i
);
3107 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
3108 error
= xfs_bmbt_update(bma
->cur
, new->br_startoff
,
3110 new->br_blockcount
+
3111 right
.br_blockcount
,
3120 * New allocation is not contiguous with another
3122 * Insert a new entry.
3124 xfs_iext_insert(bma
->ip
, bma
->idx
, 1, new, state
);
3125 XFS_IFORK_NEXT_SET(bma
->ip
, whichfork
,
3126 XFS_IFORK_NEXTENTS(bma
->ip
, whichfork
) + 1);
3127 if (bma
->cur
== NULL
) {
3128 rval
= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
3130 rval
= XFS_ILOG_CORE
;
3131 error
= xfs_bmbt_lookup_eq(bma
->cur
,
3134 new->br_blockcount
, &i
);
3137 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
3138 bma
->cur
->bc_rec
.b
.br_state
= new->br_state
;
3139 error
= xfs_btree_insert(bma
->cur
, &i
);
3142 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
3147 /* convert to a btree if necessary */
3148 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
3149 int tmp_logflags
; /* partial log flag return val */
3151 ASSERT(bma
->cur
== NULL
);
3152 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
3153 bma
->firstblock
, bma
->flist
, &bma
->cur
,
3154 0, &tmp_logflags
, whichfork
);
3155 bma
->logflags
|= tmp_logflags
;
3160 /* clear out the allocated field, done with it now in any case. */
3162 bma
->cur
->bc_private
.b
.allocated
= 0;
3164 xfs_bmap_check_leaf_extents(bma
->cur
, bma
->ip
, whichfork
);
3166 bma
->logflags
|= rval
;
3171 * Functions used in the extent read, allocate and remove paths
3175 * Adjust the size of the new extent based on di_extsize and rt extsize.
3178 xfs_bmap_extsize_align(
3180 xfs_bmbt_irec_t
*gotp
, /* next extent pointer */
3181 xfs_bmbt_irec_t
*prevp
, /* previous extent pointer */
3182 xfs_extlen_t extsz
, /* align to this extent size */
3183 int rt
, /* is this a realtime inode? */
3184 int eof
, /* is extent at end-of-file? */
3185 int delay
, /* creating delalloc extent? */
3186 int convert
, /* overwriting unwritten extent? */
3187 xfs_fileoff_t
*offp
, /* in/out: aligned offset */
3188 xfs_extlen_t
*lenp
) /* in/out: aligned length */
3190 xfs_fileoff_t orig_off
; /* original offset */
3191 xfs_extlen_t orig_alen
; /* original length */
3192 xfs_fileoff_t orig_end
; /* original off+len */
3193 xfs_fileoff_t nexto
; /* next file offset */
3194 xfs_fileoff_t prevo
; /* previous file offset */
3195 xfs_fileoff_t align_off
; /* temp for offset */
3196 xfs_extlen_t align_alen
; /* temp for length */
3197 xfs_extlen_t temp
; /* temp for calculations */
3202 orig_off
= align_off
= *offp
;
3203 orig_alen
= align_alen
= *lenp
;
3204 orig_end
= orig_off
+ orig_alen
;
3207 * If this request overlaps an existing extent, then don't
3208 * attempt to perform any additional alignment.
3210 if (!delay
&& !eof
&&
3211 (orig_off
>= gotp
->br_startoff
) &&
3212 (orig_end
<= gotp
->br_startoff
+ gotp
->br_blockcount
)) {
3217 * If the file offset is unaligned vs. the extent size
3218 * we need to align it. This will be possible unless
3219 * the file was previously written with a kernel that didn't
3220 * perform this alignment, or if a truncate shot us in the
3223 temp
= do_mod(orig_off
, extsz
);
3229 /* Same adjustment for the end of the requested area. */
3230 temp
= (align_alen
% extsz
);
3232 align_alen
+= extsz
- temp
;
3235 * For large extent hint sizes, the aligned extent might be larger than
3236 * MAXEXTLEN. In that case, reduce the size by an extsz so that it pulls
3237 * the length back under MAXEXTLEN. The outer allocation loops handle
3238 * short allocation just fine, so it is safe to do this. We only want to
3239 * do it when we are forced to, though, because it means more allocation
3240 * operations are required.
3242 while (align_alen
> MAXEXTLEN
)
3243 align_alen
-= extsz
;
3244 ASSERT(align_alen
<= MAXEXTLEN
);
3247 * If the previous block overlaps with this proposed allocation
3248 * then move the start forward without adjusting the length.
3250 if (prevp
->br_startoff
!= NULLFILEOFF
) {
3251 if (prevp
->br_startblock
== HOLESTARTBLOCK
)
3252 prevo
= prevp
->br_startoff
;
3254 prevo
= prevp
->br_startoff
+ prevp
->br_blockcount
;
3257 if (align_off
!= orig_off
&& align_off
< prevo
)
3260 * If the next block overlaps with this proposed allocation
3261 * then move the start back without adjusting the length,
3262 * but not before offset 0.
3263 * This may of course make the start overlap previous block,
3264 * and if we hit the offset 0 limit then the next block
3265 * can still overlap too.
3267 if (!eof
&& gotp
->br_startoff
!= NULLFILEOFF
) {
3268 if ((delay
&& gotp
->br_startblock
== HOLESTARTBLOCK
) ||
3269 (!delay
&& gotp
->br_startblock
== DELAYSTARTBLOCK
))
3270 nexto
= gotp
->br_startoff
+ gotp
->br_blockcount
;
3272 nexto
= gotp
->br_startoff
;
3274 nexto
= NULLFILEOFF
;
3276 align_off
+ align_alen
!= orig_end
&&
3277 align_off
+ align_alen
> nexto
)
3278 align_off
= nexto
> align_alen
? nexto
- align_alen
: 0;
3280 * If we're now overlapping the next or previous extent that
3281 * means we can't fit an extsz piece in this hole. Just move
3282 * the start forward to the first valid spot and set
3283 * the length so we hit the end.
3285 if (align_off
!= orig_off
&& align_off
< prevo
)
3287 if (align_off
+ align_alen
!= orig_end
&&
3288 align_off
+ align_alen
> nexto
&&
3289 nexto
!= NULLFILEOFF
) {
3290 ASSERT(nexto
> prevo
);
3291 align_alen
= nexto
- align_off
;
3295 * If realtime, and the result isn't a multiple of the realtime
3296 * extent size we need to remove blocks until it is.
3298 if (rt
&& (temp
= (align_alen
% mp
->m_sb
.sb_rextsize
))) {
3300 * We're not covering the original request, or
3301 * we won't be able to once we fix the length.
3303 if (orig_off
< align_off
||
3304 orig_end
> align_off
+ align_alen
||
3305 align_alen
- temp
< orig_alen
)
3308 * Try to fix it by moving the start up.
3310 if (align_off
+ temp
<= orig_off
) {
3315 * Try to fix it by moving the end in.
3317 else if (align_off
+ align_alen
- temp
>= orig_end
)
3320 * Set the start to the minimum then trim the length.
3323 align_alen
-= orig_off
- align_off
;
3324 align_off
= orig_off
;
3325 align_alen
-= align_alen
% mp
->m_sb
.sb_rextsize
;
3328 * Result doesn't cover the request, fail it.
3330 if (orig_off
< align_off
|| orig_end
> align_off
+ align_alen
)
3333 ASSERT(orig_off
>= align_off
);
3334 /* see MAXEXTLEN handling above */
3335 ASSERT(orig_end
<= align_off
+ align_alen
||
3336 align_alen
+ extsz
> MAXEXTLEN
);
3340 if (!eof
&& gotp
->br_startoff
!= NULLFILEOFF
)
3341 ASSERT(align_off
+ align_alen
<= gotp
->br_startoff
);
3342 if (prevp
->br_startoff
!= NULLFILEOFF
)
3343 ASSERT(align_off
>= prevp
->br_startoff
+ prevp
->br_blockcount
);
3351 #define XFS_ALLOC_GAP_UNITS 4
3355 struct xfs_bmalloca
*ap
) /* bmap alloc argument struct */
3357 xfs_fsblock_t adjust
; /* adjustment to block numbers */
3358 xfs_agnumber_t fb_agno
; /* ag number of ap->firstblock */
3359 xfs_mount_t
*mp
; /* mount point structure */
3360 int nullfb
; /* true if ap->firstblock isn't set */
3361 int rt
; /* true if inode is realtime */
3363 #define ISVALID(x,y) \
3365 (x) < mp->m_sb.sb_rblocks : \
3366 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
3367 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
3368 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
3370 mp
= ap
->ip
->i_mount
;
3371 nullfb
= *ap
->firstblock
== NULLFSBLOCK
;
3372 rt
= XFS_IS_REALTIME_INODE(ap
->ip
) && ap
->userdata
;
3373 fb_agno
= nullfb
? NULLAGNUMBER
: XFS_FSB_TO_AGNO(mp
, *ap
->firstblock
);
3375 * If allocating at eof, and there's a previous real block,
3376 * try to use its last block as our starting point.
3378 if (ap
->eof
&& ap
->prev
.br_startoff
!= NULLFILEOFF
&&
3379 !isnullstartblock(ap
->prev
.br_startblock
) &&
3380 ISVALID(ap
->prev
.br_startblock
+ ap
->prev
.br_blockcount
,
3381 ap
->prev
.br_startblock
)) {
3382 ap
->blkno
= ap
->prev
.br_startblock
+ ap
->prev
.br_blockcount
;
3384 * Adjust for the gap between prevp and us.
3386 adjust
= ap
->offset
-
3387 (ap
->prev
.br_startoff
+ ap
->prev
.br_blockcount
);
3389 ISVALID(ap
->blkno
+ adjust
, ap
->prev
.br_startblock
))
3390 ap
->blkno
+= adjust
;
3393 * If not at eof, then compare the two neighbor blocks.
3394 * Figure out whether either one gives us a good starting point,
3395 * and pick the better one.
3397 else if (!ap
->eof
) {
3398 xfs_fsblock_t gotbno
; /* right side block number */
3399 xfs_fsblock_t gotdiff
=0; /* right side difference */
3400 xfs_fsblock_t prevbno
; /* left side block number */
3401 xfs_fsblock_t prevdiff
=0; /* left side difference */
3404 * If there's a previous (left) block, select a requested
3405 * start block based on it.
3407 if (ap
->prev
.br_startoff
!= NULLFILEOFF
&&
3408 !isnullstartblock(ap
->prev
.br_startblock
) &&
3409 (prevbno
= ap
->prev
.br_startblock
+
3410 ap
->prev
.br_blockcount
) &&
3411 ISVALID(prevbno
, ap
->prev
.br_startblock
)) {
3413 * Calculate gap to end of previous block.
3415 adjust
= prevdiff
= ap
->offset
-
3416 (ap
->prev
.br_startoff
+
3417 ap
->prev
.br_blockcount
);
3419 * Figure the startblock based on the previous block's
3420 * end and the gap size.
3422 * If the gap is large relative to the piece we're
3423 * allocating, or using it gives us an invalid block
3424 * number, then just use the end of the previous block.
3426 if (prevdiff
<= XFS_ALLOC_GAP_UNITS
* ap
->length
&&
3427 ISVALID(prevbno
+ prevdiff
,
3428 ap
->prev
.br_startblock
))
3433 * If the firstblock forbids it, can't use it,
3436 if (!rt
&& !nullfb
&&
3437 XFS_FSB_TO_AGNO(mp
, prevbno
) != fb_agno
)
3438 prevbno
= NULLFSBLOCK
;
3441 * No previous block or can't follow it, just default.
3444 prevbno
= NULLFSBLOCK
;
3446 * If there's a following (right) block, select a requested
3447 * start block based on it.
3449 if (!isnullstartblock(ap
->got
.br_startblock
)) {
3451 * Calculate gap to start of next block.
3453 adjust
= gotdiff
= ap
->got
.br_startoff
- ap
->offset
;
3455 * Figure the startblock based on the next block's
3456 * start and the gap size.
3458 gotbno
= ap
->got
.br_startblock
;
3461 * If the gap is large relative to the piece we're
3462 * allocating, or using it gives us an invalid block
3463 * number, then just use the start of the next block
3464 * offset by our length.
3466 if (gotdiff
<= XFS_ALLOC_GAP_UNITS
* ap
->length
&&
3467 ISVALID(gotbno
- gotdiff
, gotbno
))
3469 else if (ISVALID(gotbno
- ap
->length
, gotbno
)) {
3470 gotbno
-= ap
->length
;
3471 gotdiff
+= adjust
- ap
->length
;
3475 * If the firstblock forbids it, can't use it,
3478 if (!rt
&& !nullfb
&&
3479 XFS_FSB_TO_AGNO(mp
, gotbno
) != fb_agno
)
3480 gotbno
= NULLFSBLOCK
;
3483 * No next block, just default.
3486 gotbno
= NULLFSBLOCK
;
3488 * If both valid, pick the better one, else the only good
3489 * one, else ap->blkno is already set (to 0 or the inode block).
3491 if (prevbno
!= NULLFSBLOCK
&& gotbno
!= NULLFSBLOCK
)
3492 ap
->blkno
= prevdiff
<= gotdiff
? prevbno
: gotbno
;
3493 else if (prevbno
!= NULLFSBLOCK
)
3494 ap
->blkno
= prevbno
;
3495 else if (gotbno
!= NULLFSBLOCK
)
3502 xfs_bmap_longest_free_extent(
3503 struct xfs_trans
*tp
,
3508 struct xfs_mount
*mp
= tp
->t_mountp
;
3509 struct xfs_perag
*pag
;
3510 xfs_extlen_t longest
;
3513 pag
= xfs_perag_get(mp
, ag
);
3514 if (!pag
->pagf_init
) {
3515 error
= xfs_alloc_pagf_init(mp
, tp
, ag
, XFS_ALLOC_FLAG_TRYLOCK
);
3519 if (!pag
->pagf_init
) {
3525 longest
= xfs_alloc_longest_free_extent(mp
, pag
,
3526 xfs_alloc_min_freelist(mp
, pag
));
3527 if (*blen
< longest
)
3536 xfs_bmap_select_minlen(
3537 struct xfs_bmalloca
*ap
,
3538 struct xfs_alloc_arg
*args
,
3542 if (notinit
|| *blen
< ap
->minlen
) {
3544 * Since we did a BUF_TRYLOCK above, it is possible that
3545 * there is space for this request.
3547 args
->minlen
= ap
->minlen
;
3548 } else if (*blen
< args
->maxlen
) {
3550 * If the best seen length is less than the request length,
3551 * use the best as the minimum.
3553 args
->minlen
= *blen
;
3556 * Otherwise we've seen an extent as big as maxlen, use that
3559 args
->minlen
= args
->maxlen
;
3564 xfs_bmap_btalloc_nullfb(
3565 struct xfs_bmalloca
*ap
,
3566 struct xfs_alloc_arg
*args
,
3569 struct xfs_mount
*mp
= ap
->ip
->i_mount
;
3570 xfs_agnumber_t ag
, startag
;
3574 args
->type
= XFS_ALLOCTYPE_START_BNO
;
3575 args
->total
= ap
->total
;
3577 startag
= ag
= XFS_FSB_TO_AGNO(mp
, args
->fsbno
);
3578 if (startag
== NULLAGNUMBER
)
3581 while (*blen
< args
->maxlen
) {
3582 error
= xfs_bmap_longest_free_extent(args
->tp
, ag
, blen
,
3587 if (++ag
== mp
->m_sb
.sb_agcount
)
3593 xfs_bmap_select_minlen(ap
, args
, blen
, notinit
);
3598 xfs_bmap_btalloc_filestreams(
3599 struct xfs_bmalloca
*ap
,
3600 struct xfs_alloc_arg
*args
,
3603 struct xfs_mount
*mp
= ap
->ip
->i_mount
;
3608 args
->type
= XFS_ALLOCTYPE_NEAR_BNO
;
3609 args
->total
= ap
->total
;
3611 ag
= XFS_FSB_TO_AGNO(mp
, args
->fsbno
);
3612 if (ag
== NULLAGNUMBER
)
3615 error
= xfs_bmap_longest_free_extent(args
->tp
, ag
, blen
, ¬init
);
3619 if (*blen
< args
->maxlen
) {
3620 error
= xfs_filestream_new_ag(ap
, &ag
);
3624 error
= xfs_bmap_longest_free_extent(args
->tp
, ag
, blen
,
3631 xfs_bmap_select_minlen(ap
, args
, blen
, notinit
);
3634 * Set the failure fallback case to look in the selected AG as stream
3637 ap
->blkno
= args
->fsbno
= XFS_AGB_TO_FSB(mp
, ag
, 0);
3643 struct xfs_bmalloca
*ap
) /* bmap alloc argument struct */
3645 xfs_mount_t
*mp
; /* mount point structure */
3646 xfs_alloctype_t atype
= 0; /* type for allocation routines */
3647 xfs_extlen_t align
; /* minimum allocation alignment */
3648 xfs_agnumber_t fb_agno
; /* ag number of ap->firstblock */
3650 xfs_alloc_arg_t args
;
3652 xfs_extlen_t nextminlen
= 0;
3653 int nullfb
; /* true if ap->firstblock isn't set */
3661 mp
= ap
->ip
->i_mount
;
3663 /* stripe alignment for allocation is determined by mount parameters */
3665 if (mp
->m_swidth
&& (mp
->m_flags
& XFS_MOUNT_SWALLOC
))
3666 stripe_align
= mp
->m_swidth
;
3667 else if (mp
->m_dalign
)
3668 stripe_align
= mp
->m_dalign
;
3670 align
= ap
->userdata
? xfs_get_extsz_hint(ap
->ip
) : 0;
3671 if (unlikely(align
)) {
3672 error
= xfs_bmap_extsize_align(mp
, &ap
->got
, &ap
->prev
,
3673 align
, 0, ap
->eof
, 0, ap
->conv
,
3674 &ap
->offset
, &ap
->length
);
3680 nullfb
= *ap
->firstblock
== NULLFSBLOCK
;
3681 fb_agno
= nullfb
? NULLAGNUMBER
: XFS_FSB_TO_AGNO(mp
, *ap
->firstblock
);
3683 if (ap
->userdata
&& xfs_inode_is_filestream(ap
->ip
)) {
3684 ag
= xfs_filestream_lookup_ag(ap
->ip
);
3685 ag
= (ag
!= NULLAGNUMBER
) ? ag
: 0;
3686 ap
->blkno
= XFS_AGB_TO_FSB(mp
, ag
, 0);
3688 ap
->blkno
= XFS_INO_TO_FSB(mp
, ap
->ip
->i_ino
);
3691 ap
->blkno
= *ap
->firstblock
;
3693 xfs_bmap_adjacent(ap
);
3696 * If allowed, use ap->blkno; otherwise must use firstblock since
3697 * it's in the right allocation group.
3699 if (nullfb
|| XFS_FSB_TO_AGNO(mp
, ap
->blkno
) == fb_agno
)
3702 ap
->blkno
= *ap
->firstblock
;
3704 * Normal allocation, done through xfs_alloc_vextent.
3706 tryagain
= isaligned
= 0;
3707 memset(&args
, 0, sizeof(args
));
3710 args
.fsbno
= ap
->blkno
;
3712 /* Trim the allocation back to the maximum an AG can fit. */
3713 args
.maxlen
= MIN(ap
->length
, XFS_ALLOC_AG_MAX_USABLE(mp
));
3714 args
.firstblock
= *ap
->firstblock
;
3718 * Search for an allocation group with a single extent large
3719 * enough for the request. If one isn't found, then adjust
3720 * the minimum allocation size to the largest space found.
3722 if (ap
->userdata
&& xfs_inode_is_filestream(ap
->ip
))
3723 error
= xfs_bmap_btalloc_filestreams(ap
, &args
, &blen
);
3725 error
= xfs_bmap_btalloc_nullfb(ap
, &args
, &blen
);
3728 } else if (ap
->flist
->xbf_low
) {
3729 if (xfs_inode_is_filestream(ap
->ip
))
3730 args
.type
= XFS_ALLOCTYPE_FIRST_AG
;
3732 args
.type
= XFS_ALLOCTYPE_START_BNO
;
3733 args
.total
= args
.minlen
= ap
->minlen
;
3735 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
3736 args
.total
= ap
->total
;
3737 args
.minlen
= ap
->minlen
;
3739 /* apply extent size hints if obtained earlier */
3740 if (unlikely(align
)) {
3742 if ((args
.mod
= (xfs_extlen_t
)do_mod(ap
->offset
, args
.prod
)))
3743 args
.mod
= (xfs_extlen_t
)(args
.prod
- args
.mod
);
3744 } else if (mp
->m_sb
.sb_blocksize
>= PAGE_SIZE
) {
3748 args
.prod
= PAGE_SIZE
>> mp
->m_sb
.sb_blocklog
;
3749 if ((args
.mod
= (xfs_extlen_t
)(do_mod(ap
->offset
, args
.prod
))))
3750 args
.mod
= (xfs_extlen_t
)(args
.prod
- args
.mod
);
3753 * If we are not low on available data blocks, and the
3754 * underlying logical volume manager is a stripe, and
3755 * the file offset is zero then try to allocate data
3756 * blocks on stripe unit boundary.
3757 * NOTE: ap->aeof is only set if the allocation length
3758 * is >= the stripe unit and the allocation offset is
3759 * at the end of file.
3761 if (!ap
->flist
->xbf_low
&& ap
->aeof
) {
3763 args
.alignment
= stripe_align
;
3767 * Adjust for alignment
3769 if (blen
> args
.alignment
&& blen
<= args
.maxlen
)
3770 args
.minlen
= blen
- args
.alignment
;
3771 args
.minalignslop
= 0;
3774 * First try an exact bno allocation.
3775 * If it fails then do a near or start bno
3776 * allocation with alignment turned on.
3780 args
.type
= XFS_ALLOCTYPE_THIS_BNO
;
3783 * Compute the minlen+alignment for the
3784 * next case. Set slop so that the value
3785 * of minlen+alignment+slop doesn't go up
3786 * between the calls.
3788 if (blen
> stripe_align
&& blen
<= args
.maxlen
)
3789 nextminlen
= blen
- stripe_align
;
3791 nextminlen
= args
.minlen
;
3792 if (nextminlen
+ stripe_align
> args
.minlen
+ 1)
3794 nextminlen
+ stripe_align
-
3797 args
.minalignslop
= 0;
3801 args
.minalignslop
= 0;
3803 args
.minleft
= ap
->minleft
;
3804 args
.wasdel
= ap
->wasdel
;
3806 args
.userdata
= ap
->userdata
;
3807 if (ap
->userdata
& XFS_ALLOC_USERDATA_ZERO
)
3810 error
= xfs_alloc_vextent(&args
);
3814 if (tryagain
&& args
.fsbno
== NULLFSBLOCK
) {
3816 * Exact allocation failed. Now try with alignment
3820 args
.fsbno
= ap
->blkno
;
3821 args
.alignment
= stripe_align
;
3822 args
.minlen
= nextminlen
;
3823 args
.minalignslop
= 0;
3825 if ((error
= xfs_alloc_vextent(&args
)))
3828 if (isaligned
&& args
.fsbno
== NULLFSBLOCK
) {
3830 * allocation failed, so turn off alignment and
3834 args
.fsbno
= ap
->blkno
;
3836 if ((error
= xfs_alloc_vextent(&args
)))
3839 if (args
.fsbno
== NULLFSBLOCK
&& nullfb
&&
3840 args
.minlen
> ap
->minlen
) {
3841 args
.minlen
= ap
->minlen
;
3842 args
.type
= XFS_ALLOCTYPE_START_BNO
;
3843 args
.fsbno
= ap
->blkno
;
3844 if ((error
= xfs_alloc_vextent(&args
)))
3847 if (args
.fsbno
== NULLFSBLOCK
&& nullfb
) {
3849 args
.type
= XFS_ALLOCTYPE_FIRST_AG
;
3850 args
.total
= ap
->minlen
;
3852 if ((error
= xfs_alloc_vextent(&args
)))
3854 ap
->flist
->xbf_low
= 1;
3856 if (args
.fsbno
!= NULLFSBLOCK
) {
3858 * check the allocation happened at the same or higher AG than
3859 * the first block that was allocated.
3861 ASSERT(*ap
->firstblock
== NULLFSBLOCK
||
3862 XFS_FSB_TO_AGNO(mp
, *ap
->firstblock
) ==
3863 XFS_FSB_TO_AGNO(mp
, args
.fsbno
) ||
3864 (ap
->flist
->xbf_low
&&
3865 XFS_FSB_TO_AGNO(mp
, *ap
->firstblock
) <
3866 XFS_FSB_TO_AGNO(mp
, args
.fsbno
)));
3868 ap
->blkno
= args
.fsbno
;
3869 if (*ap
->firstblock
== NULLFSBLOCK
)
3870 *ap
->firstblock
= args
.fsbno
;
3871 ASSERT(nullfb
|| fb_agno
== args
.agno
||
3872 (ap
->flist
->xbf_low
&& fb_agno
< args
.agno
));
3873 ap
->length
= args
.len
;
3874 ap
->ip
->i_d
.di_nblocks
+= args
.len
;
3875 xfs_trans_log_inode(ap
->tp
, ap
->ip
, XFS_ILOG_CORE
);
3877 ap
->ip
->i_delayed_blks
-= args
.len
;
3879 * Adjust the disk quota also. This was reserved
3882 xfs_trans_mod_dquot_byino(ap
->tp
, ap
->ip
,
3883 ap
->wasdel
? XFS_TRANS_DQ_DELBCOUNT
:
3884 XFS_TRANS_DQ_BCOUNT
,
3887 ap
->blkno
= NULLFSBLOCK
;
3894 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
3895 * It figures out where to ask the underlying allocator to put the new extent.
3899 struct xfs_bmalloca
*ap
) /* bmap alloc argument struct */
3901 if (XFS_IS_REALTIME_INODE(ap
->ip
) && ap
->userdata
)
3902 return xfs_bmap_rtalloc(ap
);
3903 return xfs_bmap_btalloc(ap
);
3907 * Trim the returned map to the required bounds
3911 struct xfs_bmbt_irec
*mval
,
3912 struct xfs_bmbt_irec
*got
,
3920 if ((flags
& XFS_BMAPI_ENTIRE
) ||
3921 got
->br_startoff
+ got
->br_blockcount
<= obno
) {
3923 if (isnullstartblock(got
->br_startblock
))
3924 mval
->br_startblock
= DELAYSTARTBLOCK
;
3930 ASSERT((*bno
>= obno
) || (n
== 0));
3932 mval
->br_startoff
= *bno
;
3933 if (isnullstartblock(got
->br_startblock
))
3934 mval
->br_startblock
= DELAYSTARTBLOCK
;
3936 mval
->br_startblock
= got
->br_startblock
+
3937 (*bno
- got
->br_startoff
);
3939 * Return the minimum of what we got and what we asked for for
3940 * the length. We can use the len variable here because it is
3941 * modified below and we could have been there before coming
3942 * here if the first part of the allocation didn't overlap what
3945 mval
->br_blockcount
= XFS_FILBLKS_MIN(end
- *bno
,
3946 got
->br_blockcount
- (*bno
- got
->br_startoff
));
3947 mval
->br_state
= got
->br_state
;
3948 ASSERT(mval
->br_blockcount
<= len
);
3953 * Update and validate the extent map to return
3956 xfs_bmapi_update_map(
3957 struct xfs_bmbt_irec
**map
,
3965 xfs_bmbt_irec_t
*mval
= *map
;
3967 ASSERT((flags
& XFS_BMAPI_ENTIRE
) ||
3968 ((mval
->br_startoff
+ mval
->br_blockcount
) <= end
));
3969 ASSERT((flags
& XFS_BMAPI_ENTIRE
) || (mval
->br_blockcount
<= *len
) ||
3970 (mval
->br_startoff
< obno
));
3972 *bno
= mval
->br_startoff
+ mval
->br_blockcount
;
3974 if (*n
> 0 && mval
->br_startoff
== mval
[-1].br_startoff
) {
3975 /* update previous map with new information */
3976 ASSERT(mval
->br_startblock
== mval
[-1].br_startblock
);
3977 ASSERT(mval
->br_blockcount
> mval
[-1].br_blockcount
);
3978 ASSERT(mval
->br_state
== mval
[-1].br_state
);
3979 mval
[-1].br_blockcount
= mval
->br_blockcount
;
3980 mval
[-1].br_state
= mval
->br_state
;
3981 } else if (*n
> 0 && mval
->br_startblock
!= DELAYSTARTBLOCK
&&
3982 mval
[-1].br_startblock
!= DELAYSTARTBLOCK
&&
3983 mval
[-1].br_startblock
!= HOLESTARTBLOCK
&&
3984 mval
->br_startblock
== mval
[-1].br_startblock
+
3985 mval
[-1].br_blockcount
&&
3986 ((flags
& XFS_BMAPI_IGSTATE
) ||
3987 mval
[-1].br_state
== mval
->br_state
)) {
3988 ASSERT(mval
->br_startoff
==
3989 mval
[-1].br_startoff
+ mval
[-1].br_blockcount
);
3990 mval
[-1].br_blockcount
+= mval
->br_blockcount
;
3991 } else if (*n
> 0 &&
3992 mval
->br_startblock
== DELAYSTARTBLOCK
&&
3993 mval
[-1].br_startblock
== DELAYSTARTBLOCK
&&
3994 mval
->br_startoff
==
3995 mval
[-1].br_startoff
+ mval
[-1].br_blockcount
) {
3996 mval
[-1].br_blockcount
+= mval
->br_blockcount
;
3997 mval
[-1].br_state
= mval
->br_state
;
3998 } else if (!((*n
== 0) &&
3999 ((mval
->br_startoff
+ mval
->br_blockcount
) <=
4008 * Map file blocks to filesystem blocks without allocation.
4012 struct xfs_inode
*ip
,
4015 struct xfs_bmbt_irec
*mval
,
4019 struct xfs_mount
*mp
= ip
->i_mount
;
4020 struct xfs_ifork
*ifp
;
4021 struct xfs_bmbt_irec got
;
4022 struct xfs_bmbt_irec prev
;
4029 int whichfork
= (flags
& XFS_BMAPI_ATTRFORK
) ?
4030 XFS_ATTR_FORK
: XFS_DATA_FORK
;
4033 ASSERT(!(flags
& ~(XFS_BMAPI_ATTRFORK
|XFS_BMAPI_ENTIRE
|
4034 XFS_BMAPI_IGSTATE
)));
4035 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_SHARED
|XFS_ILOCK_EXCL
));
4037 if (unlikely(XFS_TEST_ERROR(
4038 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
4039 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
4040 mp
, XFS_ERRTAG_BMAPIFORMAT
, XFS_RANDOM_BMAPIFORMAT
))) {
4041 XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW
, mp
);
4042 return -EFSCORRUPTED
;
4045 if (XFS_FORCED_SHUTDOWN(mp
))
4048 XFS_STATS_INC(mp
, xs_blk_mapr
);
4050 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4052 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
4053 error
= xfs_iread_extents(NULL
, ip
, whichfork
);
4058 xfs_bmap_search_extents(ip
, bno
, whichfork
, &eof
, &lastx
, &got
, &prev
);
4062 while (bno
< end
&& n
< *nmap
) {
4063 /* Reading past eof, act as though there's a hole up to end. */
4065 got
.br_startoff
= end
;
4066 if (got
.br_startoff
> bno
) {
4067 /* Reading in a hole. */
4068 mval
->br_startoff
= bno
;
4069 mval
->br_startblock
= HOLESTARTBLOCK
;
4070 mval
->br_blockcount
=
4071 XFS_FILBLKS_MIN(len
, got
.br_startoff
- bno
);
4072 mval
->br_state
= XFS_EXT_NORM
;
4073 bno
+= mval
->br_blockcount
;
4074 len
-= mval
->br_blockcount
;
4080 /* set up the extent map to return. */
4081 xfs_bmapi_trim_map(mval
, &got
, &bno
, len
, obno
, end
, n
, flags
);
4082 xfs_bmapi_update_map(&mval
, &bno
, &len
, obno
, end
, &n
, flags
);
4084 /* If we're done, stop now. */
4085 if (bno
>= end
|| n
>= *nmap
)
4088 /* Else go on to the next record. */
4089 if (++lastx
< ifp
->if_bytes
/ sizeof(xfs_bmbt_rec_t
))
4090 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, lastx
), &got
);
4099 xfs_bmapi_reserve_delalloc(
4100 struct xfs_inode
*ip
,
4103 struct xfs_bmbt_irec
*got
,
4104 struct xfs_bmbt_irec
*prev
,
4105 xfs_extnum_t
*lastx
,
4108 struct xfs_mount
*mp
= ip
->i_mount
;
4109 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, XFS_DATA_FORK
);
4111 xfs_extlen_t indlen
;
4112 char rt
= XFS_IS_REALTIME_INODE(ip
);
4116 alen
= XFS_FILBLKS_MIN(len
, MAXEXTLEN
);
4118 alen
= XFS_FILBLKS_MIN(alen
, got
->br_startoff
- aoff
);
4120 /* Figure out the extent size, adjust alen */
4121 extsz
= xfs_get_extsz_hint(ip
);
4123 error
= xfs_bmap_extsize_align(mp
, got
, prev
, extsz
, rt
, eof
,
4124 1, 0, &aoff
, &alen
);
4129 extsz
= alen
/ mp
->m_sb
.sb_rextsize
;
4132 * Make a transaction-less quota reservation for delayed allocation
4133 * blocks. This number gets adjusted later. We return if we haven't
4134 * allocated blocks already inside this loop.
4136 error
= xfs_trans_reserve_quota_nblks(NULL
, ip
, (long)alen
, 0,
4137 rt
? XFS_QMOPT_RES_RTBLKS
: XFS_QMOPT_RES_REGBLKS
);
4142 * Split changing sb for alen and indlen since they could be coming
4143 * from different places.
4145 indlen
= (xfs_extlen_t
)xfs_bmap_worst_indlen(ip
, alen
);
4149 error
= xfs_mod_frextents(mp
, -((int64_t)extsz
));
4151 error
= xfs_mod_fdblocks(mp
, -((int64_t)alen
), false);
4155 goto out_unreserve_quota
;
4157 error
= xfs_mod_fdblocks(mp
, -((int64_t)indlen
), false);
4159 goto out_unreserve_blocks
;
4162 ip
->i_delayed_blks
+= alen
;
4164 got
->br_startoff
= aoff
;
4165 got
->br_startblock
= nullstartblock(indlen
);
4166 got
->br_blockcount
= alen
;
4167 got
->br_state
= XFS_EXT_NORM
;
4168 xfs_bmap_add_extent_hole_delay(ip
, lastx
, got
);
4171 * Update our extent pointer, given that xfs_bmap_add_extent_hole_delay
4172 * might have merged it into one of the neighbouring ones.
4174 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, *lastx
), got
);
4176 ASSERT(got
->br_startoff
<= aoff
);
4177 ASSERT(got
->br_startoff
+ got
->br_blockcount
>= aoff
+ alen
);
4178 ASSERT(isnullstartblock(got
->br_startblock
));
4179 ASSERT(got
->br_state
== XFS_EXT_NORM
);
4182 out_unreserve_blocks
:
4184 xfs_mod_frextents(mp
, extsz
);
4186 xfs_mod_fdblocks(mp
, alen
, false);
4187 out_unreserve_quota
:
4188 if (XFS_IS_QUOTA_ON(mp
))
4189 xfs_trans_unreserve_quota_nblks(NULL
, ip
, (long)alen
, 0, rt
?
4190 XFS_QMOPT_RES_RTBLKS
: XFS_QMOPT_RES_REGBLKS
);
4195 * Map file blocks to filesystem blocks, adding delayed allocations as needed.
4199 struct xfs_inode
*ip
, /* incore inode */
4200 xfs_fileoff_t bno
, /* starting file offs. mapped */
4201 xfs_filblks_t len
, /* length to map in file */
4202 struct xfs_bmbt_irec
*mval
, /* output: map values */
4203 int *nmap
, /* i/o: mval size/count */
4204 int flags
) /* XFS_BMAPI_... */
4206 struct xfs_mount
*mp
= ip
->i_mount
;
4207 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, XFS_DATA_FORK
);
4208 struct xfs_bmbt_irec got
; /* current file extent record */
4209 struct xfs_bmbt_irec prev
; /* previous file extent record */
4210 xfs_fileoff_t obno
; /* old block number (offset) */
4211 xfs_fileoff_t end
; /* end of mapped file region */
4212 xfs_extnum_t lastx
; /* last useful extent number */
4213 int eof
; /* we've hit the end of extents */
4214 int n
= 0; /* current extent index */
4218 ASSERT(*nmap
<= XFS_BMAP_MAX_NMAP
);
4219 ASSERT(!(flags
& ~XFS_BMAPI_ENTIRE
));
4220 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
4222 if (unlikely(XFS_TEST_ERROR(
4223 (XFS_IFORK_FORMAT(ip
, XFS_DATA_FORK
) != XFS_DINODE_FMT_EXTENTS
&&
4224 XFS_IFORK_FORMAT(ip
, XFS_DATA_FORK
) != XFS_DINODE_FMT_BTREE
),
4225 mp
, XFS_ERRTAG_BMAPIFORMAT
, XFS_RANDOM_BMAPIFORMAT
))) {
4226 XFS_ERROR_REPORT("xfs_bmapi_delay", XFS_ERRLEVEL_LOW
, mp
);
4227 return -EFSCORRUPTED
;
4230 if (XFS_FORCED_SHUTDOWN(mp
))
4233 XFS_STATS_INC(mp
, xs_blk_mapw
);
4235 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
4236 error
= xfs_iread_extents(NULL
, ip
, XFS_DATA_FORK
);
4241 xfs_bmap_search_extents(ip
, bno
, XFS_DATA_FORK
, &eof
, &lastx
, &got
, &prev
);
4245 while (bno
< end
&& n
< *nmap
) {
4246 if (eof
|| got
.br_startoff
> bno
) {
4247 error
= xfs_bmapi_reserve_delalloc(ip
, bno
, len
, &got
,
4248 &prev
, &lastx
, eof
);
4258 /* set up the extent map to return. */
4259 xfs_bmapi_trim_map(mval
, &got
, &bno
, len
, obno
, end
, n
, flags
);
4260 xfs_bmapi_update_map(&mval
, &bno
, &len
, obno
, end
, &n
, flags
);
4262 /* If we're done, stop now. */
4263 if (bno
>= end
|| n
>= *nmap
)
4266 /* Else go on to the next record. */
4268 if (++lastx
< ifp
->if_bytes
/ sizeof(xfs_bmbt_rec_t
))
4269 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, lastx
), &got
);
4281 struct xfs_bmalloca
*bma
)
4283 struct xfs_mount
*mp
= bma
->ip
->i_mount
;
4284 int whichfork
= (bma
->flags
& XFS_BMAPI_ATTRFORK
) ?
4285 XFS_ATTR_FORK
: XFS_DATA_FORK
;
4286 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
4287 int tmp_logflags
= 0;
4290 ASSERT(bma
->length
> 0);
4293 * For the wasdelay case, we could also just allocate the stuff asked
4294 * for in this bmap call but that wouldn't be as good.
4297 bma
->length
= (xfs_extlen_t
)bma
->got
.br_blockcount
;
4298 bma
->offset
= bma
->got
.br_startoff
;
4299 if (bma
->idx
!= NULLEXTNUM
&& bma
->idx
) {
4300 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
- 1),
4304 bma
->length
= XFS_FILBLKS_MIN(bma
->length
, MAXEXTLEN
);
4306 bma
->length
= XFS_FILBLKS_MIN(bma
->length
,
4307 bma
->got
.br_startoff
- bma
->offset
);
4311 * Indicate if this is the first user data in the file, or just any
4312 * user data. And if it is userdata, indicate whether it needs to
4313 * be initialised to zero during allocation.
4315 if (!(bma
->flags
& XFS_BMAPI_METADATA
)) {
4316 bma
->userdata
= (bma
->offset
== 0) ?
4317 XFS_ALLOC_INITIAL_USER_DATA
: XFS_ALLOC_USERDATA
;
4318 if (bma
->flags
& XFS_BMAPI_ZERO
)
4319 bma
->userdata
|= XFS_ALLOC_USERDATA_ZERO
;
4322 bma
->minlen
= (bma
->flags
& XFS_BMAPI_CONTIG
) ? bma
->length
: 1;
4325 * Only want to do the alignment at the eof if it is userdata and
4326 * allocation length is larger than a stripe unit.
4328 if (mp
->m_dalign
&& bma
->length
>= mp
->m_dalign
&&
4329 !(bma
->flags
& XFS_BMAPI_METADATA
) && whichfork
== XFS_DATA_FORK
) {
4330 error
= xfs_bmap_isaeof(bma
, whichfork
);
4335 error
= xfs_bmap_alloc(bma
);
4339 if (bma
->flist
->xbf_low
)
4342 bma
->cur
->bc_private
.b
.firstblock
= *bma
->firstblock
;
4343 if (bma
->blkno
== NULLFSBLOCK
)
4345 if ((ifp
->if_flags
& XFS_IFBROOT
) && !bma
->cur
) {
4346 bma
->cur
= xfs_bmbt_init_cursor(mp
, bma
->tp
, bma
->ip
, whichfork
);
4347 bma
->cur
->bc_private
.b
.firstblock
= *bma
->firstblock
;
4348 bma
->cur
->bc_private
.b
.flist
= bma
->flist
;
4351 * Bump the number of extents we've allocated
4357 bma
->cur
->bc_private
.b
.flags
=
4358 bma
->wasdel
? XFS_BTCUR_BPRV_WASDEL
: 0;
4360 bma
->got
.br_startoff
= bma
->offset
;
4361 bma
->got
.br_startblock
= bma
->blkno
;
4362 bma
->got
.br_blockcount
= bma
->length
;
4363 bma
->got
.br_state
= XFS_EXT_NORM
;
4366 * A wasdelay extent has been initialized, so shouldn't be flagged
4369 if (!bma
->wasdel
&& (bma
->flags
& XFS_BMAPI_PREALLOC
) &&
4370 xfs_sb_version_hasextflgbit(&mp
->m_sb
))
4371 bma
->got
.br_state
= XFS_EXT_UNWRITTEN
;
4374 error
= xfs_bmap_add_extent_delay_real(bma
);
4376 error
= xfs_bmap_add_extent_hole_real(bma
, whichfork
);
4378 bma
->logflags
|= tmp_logflags
;
4383 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4384 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4385 * the neighbouring ones.
4387 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
), &bma
->got
);
4389 ASSERT(bma
->got
.br_startoff
<= bma
->offset
);
4390 ASSERT(bma
->got
.br_startoff
+ bma
->got
.br_blockcount
>=
4391 bma
->offset
+ bma
->length
);
4392 ASSERT(bma
->got
.br_state
== XFS_EXT_NORM
||
4393 bma
->got
.br_state
== XFS_EXT_UNWRITTEN
);
4398 xfs_bmapi_convert_unwritten(
4399 struct xfs_bmalloca
*bma
,
4400 struct xfs_bmbt_irec
*mval
,
4404 int whichfork
= (flags
& XFS_BMAPI_ATTRFORK
) ?
4405 XFS_ATTR_FORK
: XFS_DATA_FORK
;
4406 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
4407 int tmp_logflags
= 0;
4410 /* check if we need to do unwritten->real conversion */
4411 if (mval
->br_state
== XFS_EXT_UNWRITTEN
&&
4412 (flags
& XFS_BMAPI_PREALLOC
))
4415 /* check if we need to do real->unwritten conversion */
4416 if (mval
->br_state
== XFS_EXT_NORM
&&
4417 (flags
& (XFS_BMAPI_PREALLOC
| XFS_BMAPI_CONVERT
)) !=
4418 (XFS_BMAPI_PREALLOC
| XFS_BMAPI_CONVERT
))
4422 * Modify (by adding) the state flag, if writing.
4424 ASSERT(mval
->br_blockcount
<= len
);
4425 if ((ifp
->if_flags
& XFS_IFBROOT
) && !bma
->cur
) {
4426 bma
->cur
= xfs_bmbt_init_cursor(bma
->ip
->i_mount
, bma
->tp
,
4427 bma
->ip
, whichfork
);
4428 bma
->cur
->bc_private
.b
.firstblock
= *bma
->firstblock
;
4429 bma
->cur
->bc_private
.b
.flist
= bma
->flist
;
4431 mval
->br_state
= (mval
->br_state
== XFS_EXT_UNWRITTEN
)
4432 ? XFS_EXT_NORM
: XFS_EXT_UNWRITTEN
;
4435 * Before insertion into the bmbt, zero the range being converted
4438 if (flags
& XFS_BMAPI_ZERO
) {
4439 error
= xfs_zero_extent(bma
->ip
, mval
->br_startblock
,
4440 mval
->br_blockcount
);
4445 error
= xfs_bmap_add_extent_unwritten_real(bma
->tp
, bma
->ip
, &bma
->idx
,
4446 &bma
->cur
, mval
, bma
->firstblock
, bma
->flist
,
4449 * Log the inode core unconditionally in the unwritten extent conversion
4450 * path because the conversion might not have done so (e.g., if the
4451 * extent count hasn't changed). We need to make sure the inode is dirty
4452 * in the transaction for the sake of fsync(), even if nothing has
4453 * changed, because fsync() will not force the log for this transaction
4454 * unless it sees the inode pinned.
4456 bma
->logflags
|= tmp_logflags
| XFS_ILOG_CORE
;
4461 * Update our extent pointer, given that
4462 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4463 * of the neighbouring ones.
4465 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
), &bma
->got
);
4468 * We may have combined previously unwritten space with written space,
4469 * so generate another request.
4471 if (mval
->br_blockcount
< len
)
4477 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4478 * extent state if necessary. Details behaviour is controlled by the flags
4479 * parameter. Only allocates blocks from a single allocation group, to avoid
4482 * The returned value in "firstblock" from the first call in a transaction
4483 * must be remembered and presented to subsequent calls in "firstblock".
4484 * An upper bound for the number of blocks to be allocated is supplied to
4485 * the first call in "total"; if no allocation group has that many free
4486 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4490 struct xfs_trans
*tp
, /* transaction pointer */
4491 struct xfs_inode
*ip
, /* incore inode */
4492 xfs_fileoff_t bno
, /* starting file offs. mapped */
4493 xfs_filblks_t len
, /* length to map in file */
4494 int flags
, /* XFS_BMAPI_... */
4495 xfs_fsblock_t
*firstblock
, /* first allocated block
4496 controls a.g. for allocs */
4497 xfs_extlen_t total
, /* total blocks needed */
4498 struct xfs_bmbt_irec
*mval
, /* output: map values */
4499 int *nmap
, /* i/o: mval size/count */
4500 struct xfs_bmap_free
*flist
) /* i/o: list extents to free */
4502 struct xfs_mount
*mp
= ip
->i_mount
;
4503 struct xfs_ifork
*ifp
;
4504 struct xfs_bmalloca bma
= { NULL
}; /* args for xfs_bmap_alloc */
4505 xfs_fileoff_t end
; /* end of mapped file region */
4506 int eof
; /* after the end of extents */
4507 int error
; /* error return */
4508 int n
; /* current extent index */
4509 xfs_fileoff_t obno
; /* old block number (offset) */
4510 int whichfork
; /* data or attr fork */
4511 char inhole
; /* current location is hole in file */
4512 char wasdelay
; /* old extent was delayed */
4515 xfs_fileoff_t orig_bno
; /* original block number value */
4516 int orig_flags
; /* original flags arg value */
4517 xfs_filblks_t orig_len
; /* original value of len arg */
4518 struct xfs_bmbt_irec
*orig_mval
; /* original value of mval */
4519 int orig_nmap
; /* original value of *nmap */
4527 whichfork
= (flags
& XFS_BMAPI_ATTRFORK
) ?
4528 XFS_ATTR_FORK
: XFS_DATA_FORK
;
4531 ASSERT(*nmap
<= XFS_BMAP_MAX_NMAP
);
4532 ASSERT(!(flags
& XFS_BMAPI_IGSTATE
));
4535 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_LOCAL
);
4536 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
4538 /* zeroing is for currently only for data extents, not metadata */
4539 ASSERT((flags
& (XFS_BMAPI_METADATA
| XFS_BMAPI_ZERO
)) !=
4540 (XFS_BMAPI_METADATA
| XFS_BMAPI_ZERO
));
4542 * we can allocate unwritten extents or pre-zero allocated blocks,
4543 * but it makes no sense to do both at once. This would result in
4544 * zeroing the unwritten extent twice, but it still being an
4545 * unwritten extent....
4547 ASSERT((flags
& (XFS_BMAPI_PREALLOC
| XFS_BMAPI_ZERO
)) !=
4548 (XFS_BMAPI_PREALLOC
| XFS_BMAPI_ZERO
));
4550 if (unlikely(XFS_TEST_ERROR(
4551 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
4552 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
4553 mp
, XFS_ERRTAG_BMAPIFORMAT
, XFS_RANDOM_BMAPIFORMAT
))) {
4554 XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW
, mp
);
4555 return -EFSCORRUPTED
;
4558 if (XFS_FORCED_SHUTDOWN(mp
))
4561 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4563 XFS_STATS_INC(mp
, xs_blk_mapw
);
4565 if (*firstblock
== NULLFSBLOCK
) {
4566 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
)
4567 bma
.minleft
= be16_to_cpu(ifp
->if_broot
->bb_level
) + 1;
4574 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
4575 error
= xfs_iread_extents(tp
, ip
, whichfork
);
4580 xfs_bmap_search_extents(ip
, bno
, whichfork
, &eof
, &bma
.idx
, &bma
.got
,
4591 bma
.firstblock
= firstblock
;
4593 while (bno
< end
&& n
< *nmap
) {
4594 inhole
= eof
|| bma
.got
.br_startoff
> bno
;
4595 wasdelay
= !inhole
&& isnullstartblock(bma
.got
.br_startblock
);
4598 * First, deal with the hole before the allocated space
4599 * that we found, if any.
4601 if (inhole
|| wasdelay
) {
4603 bma
.conv
= !!(flags
& XFS_BMAPI_CONVERT
);
4604 bma
.wasdel
= wasdelay
;
4609 * There's a 32/64 bit type mismatch between the
4610 * allocation length request (which can be 64 bits in
4611 * length) and the bma length request, which is
4612 * xfs_extlen_t and therefore 32 bits. Hence we have to
4613 * check for 32-bit overflows and handle them here.
4615 if (len
> (xfs_filblks_t
)MAXEXTLEN
)
4616 bma
.length
= MAXEXTLEN
;
4621 ASSERT(bma
.length
> 0);
4622 error
= xfs_bmapi_allocate(&bma
);
4625 if (bma
.blkno
== NULLFSBLOCK
)
4629 /* Deal with the allocated space we found. */
4630 xfs_bmapi_trim_map(mval
, &bma
.got
, &bno
, len
, obno
,
4633 /* Execute unwritten extent conversion if necessary */
4634 error
= xfs_bmapi_convert_unwritten(&bma
, mval
, len
, flags
);
4635 if (error
== -EAGAIN
)
4640 /* update the extent map to return */
4641 xfs_bmapi_update_map(&mval
, &bno
, &len
, obno
, end
, &n
, flags
);
4644 * If we're done, stop now. Stop when we've allocated
4645 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4646 * the transaction may get too big.
4648 if (bno
>= end
|| n
>= *nmap
|| bma
.nallocs
>= *nmap
)
4651 /* Else go on to the next record. */
4653 if (++bma
.idx
< ifp
->if_bytes
/ sizeof(xfs_bmbt_rec_t
)) {
4654 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
.idx
),
4662 * Transform from btree to extents, give it cur.
4664 if (xfs_bmap_wants_extents(ip
, whichfork
)) {
4665 int tmp_logflags
= 0;
4668 error
= xfs_bmap_btree_to_extents(tp
, ip
, bma
.cur
,
4669 &tmp_logflags
, whichfork
);
4670 bma
.logflags
|= tmp_logflags
;
4675 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
||
4676 XFS_IFORK_NEXTENTS(ip
, whichfork
) >
4677 XFS_IFORK_MAXEXT(ip
, whichfork
));
4681 * Log everything. Do this after conversion, there's no point in
4682 * logging the extent records if we've converted to btree format.
4684 if ((bma
.logflags
& xfs_ilog_fext(whichfork
)) &&
4685 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
4686 bma
.logflags
&= ~xfs_ilog_fext(whichfork
);
4687 else if ((bma
.logflags
& xfs_ilog_fbroot(whichfork
)) &&
4688 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)
4689 bma
.logflags
&= ~xfs_ilog_fbroot(whichfork
);
4691 * Log whatever the flags say, even if error. Otherwise we might miss
4692 * detecting a case where the data is changed, there's an error,
4693 * and it's not logged so we don't shutdown when we should.
4696 xfs_trans_log_inode(tp
, ip
, bma
.logflags
);
4700 ASSERT(*firstblock
== NULLFSBLOCK
||
4701 XFS_FSB_TO_AGNO(mp
, *firstblock
) ==
4703 bma
.cur
->bc_private
.b
.firstblock
) ||
4705 XFS_FSB_TO_AGNO(mp
, *firstblock
) <
4707 bma
.cur
->bc_private
.b
.firstblock
)));
4708 *firstblock
= bma
.cur
->bc_private
.b
.firstblock
;
4710 xfs_btree_del_cursor(bma
.cur
,
4711 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
4714 xfs_bmap_validate_ret(orig_bno
, orig_len
, orig_flags
, orig_mval
,
4720 * When a delalloc extent is split (e.g., due to a hole punch), the original
4721 * indlen reservation must be shared across the two new extents that are left
4724 * Given the original reservation and the worst case indlen for the two new
4725 * extents (as calculated by xfs_bmap_worst_indlen()), split the original
4726 * reservation fairly across the two new extents. If necessary, steal available
4727 * blocks from a deleted extent to make up a reservation deficiency (e.g., if
4728 * ores == 1). The number of stolen blocks is returned. The availability and
4729 * subsequent accounting of stolen blocks is the responsibility of the caller.
4731 static xfs_filblks_t
4732 xfs_bmap_split_indlen(
4733 xfs_filblks_t ores
, /* original res. */
4734 xfs_filblks_t
*indlen1
, /* ext1 worst indlen */
4735 xfs_filblks_t
*indlen2
, /* ext2 worst indlen */
4736 xfs_filblks_t avail
) /* stealable blocks */
4738 xfs_filblks_t len1
= *indlen1
;
4739 xfs_filblks_t len2
= *indlen2
;
4740 xfs_filblks_t nres
= len1
+ len2
; /* new total res. */
4741 xfs_filblks_t stolen
= 0;
4744 * Steal as many blocks as we can to try and satisfy the worst case
4745 * indlen for both new extents.
4747 while (nres
> ores
&& avail
) {
4754 * The only blocks available are those reserved for the original
4755 * extent and what we can steal from the extent being removed.
4756 * If this still isn't enough to satisfy the combined
4757 * requirements for the two new extents, skim blocks off of each
4758 * of the new reservations until they match what is available.
4760 while (nres
> ores
) {
4780 * Called by xfs_bmapi to update file extent records and the btree
4781 * after removing space (or undoing a delayed allocation).
4783 STATIC
int /* error */
4784 xfs_bmap_del_extent(
4785 xfs_inode_t
*ip
, /* incore inode pointer */
4786 xfs_trans_t
*tp
, /* current transaction pointer */
4787 xfs_extnum_t
*idx
, /* extent number to update/delete */
4788 xfs_bmap_free_t
*flist
, /* list of extents to be freed */
4789 xfs_btree_cur_t
*cur
, /* if null, not a btree */
4790 xfs_bmbt_irec_t
*del
, /* data to remove from extents */
4791 int *logflagsp
, /* inode logging flags */
4792 int whichfork
) /* data or attr fork */
4794 xfs_filblks_t da_new
; /* new delay-alloc indirect blocks */
4795 xfs_filblks_t da_old
; /* old delay-alloc indirect blocks */
4796 xfs_fsblock_t del_endblock
=0; /* first block past del */
4797 xfs_fileoff_t del_endoff
; /* first offset past del */
4798 int delay
; /* current block is delayed allocated */
4799 int do_fx
; /* free extent at end of routine */
4800 xfs_bmbt_rec_host_t
*ep
; /* current extent entry pointer */
4801 int error
; /* error return value */
4802 int flags
; /* inode logging flags */
4803 xfs_bmbt_irec_t got
; /* current extent entry */
4804 xfs_fileoff_t got_endoff
; /* first offset past got */
4805 int i
; /* temp state */
4806 xfs_ifork_t
*ifp
; /* inode fork pointer */
4807 xfs_mount_t
*mp
; /* mount structure */
4808 xfs_filblks_t nblks
; /* quota/sb block count */
4809 xfs_bmbt_irec_t
new; /* new record to be inserted */
4811 uint qfield
; /* quota field to update */
4812 xfs_filblks_t temp
; /* for indirect length calculations */
4813 xfs_filblks_t temp2
; /* for indirect length calculations */
4817 XFS_STATS_INC(mp
, xs_del_exlist
);
4819 if (whichfork
== XFS_ATTR_FORK
)
4820 state
|= BMAP_ATTRFORK
;
4822 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4823 ASSERT((*idx
>= 0) && (*idx
< ifp
->if_bytes
/
4824 (uint
)sizeof(xfs_bmbt_rec_t
)));
4825 ASSERT(del
->br_blockcount
> 0);
4826 ep
= xfs_iext_get_ext(ifp
, *idx
);
4827 xfs_bmbt_get_all(ep
, &got
);
4828 ASSERT(got
.br_startoff
<= del
->br_startoff
);
4829 del_endoff
= del
->br_startoff
+ del
->br_blockcount
;
4830 got_endoff
= got
.br_startoff
+ got
.br_blockcount
;
4831 ASSERT(got_endoff
>= del_endoff
);
4832 delay
= isnullstartblock(got
.br_startblock
);
4833 ASSERT(isnullstartblock(del
->br_startblock
) == delay
);
4838 * If deleting a real allocation, must free up the disk space.
4841 flags
= XFS_ILOG_CORE
;
4843 * Realtime allocation. Free it and record di_nblocks update.
4845 if (whichfork
== XFS_DATA_FORK
&& XFS_IS_REALTIME_INODE(ip
)) {
4849 ASSERT(do_mod(del
->br_blockcount
,
4850 mp
->m_sb
.sb_rextsize
) == 0);
4851 ASSERT(do_mod(del
->br_startblock
,
4852 mp
->m_sb
.sb_rextsize
) == 0);
4853 bno
= del
->br_startblock
;
4854 len
= del
->br_blockcount
;
4855 do_div(bno
, mp
->m_sb
.sb_rextsize
);
4856 do_div(len
, mp
->m_sb
.sb_rextsize
);
4857 error
= xfs_rtfree_extent(tp
, bno
, (xfs_extlen_t
)len
);
4861 nblks
= len
* mp
->m_sb
.sb_rextsize
;
4862 qfield
= XFS_TRANS_DQ_RTBCOUNT
;
4865 * Ordinary allocation.
4869 nblks
= del
->br_blockcount
;
4870 qfield
= XFS_TRANS_DQ_BCOUNT
;
4873 * Set up del_endblock and cur for later.
4875 del_endblock
= del
->br_startblock
+ del
->br_blockcount
;
4877 if ((error
= xfs_bmbt_lookup_eq(cur
, got
.br_startoff
,
4878 got
.br_startblock
, got
.br_blockcount
,
4881 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
4883 da_old
= da_new
= 0;
4885 da_old
= startblockval(got
.br_startblock
);
4891 * Set flag value to use in switch statement.
4892 * Left-contig is 2, right-contig is 1.
4894 switch (((got
.br_startoff
== del
->br_startoff
) << 1) |
4895 (got_endoff
== del_endoff
)) {
4898 * Matches the whole extent. Delete the entry.
4900 xfs_iext_remove(ip
, *idx
, 1,
4901 whichfork
== XFS_ATTR_FORK
? BMAP_ATTRFORK
: 0);
4906 XFS_IFORK_NEXT_SET(ip
, whichfork
,
4907 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
4908 flags
|= XFS_ILOG_CORE
;
4910 flags
|= xfs_ilog_fext(whichfork
);
4913 if ((error
= xfs_btree_delete(cur
, &i
)))
4915 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
4920 * Deleting the first part of the extent.
4922 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
4923 xfs_bmbt_set_startoff(ep
, del_endoff
);
4924 temp
= got
.br_blockcount
- del
->br_blockcount
;
4925 xfs_bmbt_set_blockcount(ep
, temp
);
4927 temp
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
4929 xfs_bmbt_set_startblock(ep
, nullstartblock((int)temp
));
4930 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
4934 xfs_bmbt_set_startblock(ep
, del_endblock
);
4935 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
4937 flags
|= xfs_ilog_fext(whichfork
);
4940 if ((error
= xfs_bmbt_update(cur
, del_endoff
, del_endblock
,
4941 got
.br_blockcount
- del
->br_blockcount
,
4948 * Deleting the last part of the extent.
4950 temp
= got
.br_blockcount
- del
->br_blockcount
;
4951 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
4952 xfs_bmbt_set_blockcount(ep
, temp
);
4954 temp
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
4956 xfs_bmbt_set_startblock(ep
, nullstartblock((int)temp
));
4957 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
4961 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
4963 flags
|= xfs_ilog_fext(whichfork
);
4966 if ((error
= xfs_bmbt_update(cur
, got
.br_startoff
,
4968 got
.br_blockcount
- del
->br_blockcount
,
4975 * Deleting the middle of the extent.
4977 temp
= del
->br_startoff
- got
.br_startoff
;
4978 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
4979 xfs_bmbt_set_blockcount(ep
, temp
);
4980 new.br_startoff
= del_endoff
;
4981 temp2
= got_endoff
- del_endoff
;
4982 new.br_blockcount
= temp2
;
4983 new.br_state
= got
.br_state
;
4985 new.br_startblock
= del_endblock
;
4986 flags
|= XFS_ILOG_CORE
;
4988 if ((error
= xfs_bmbt_update(cur
,
4990 got
.br_startblock
, temp
,
4993 if ((error
= xfs_btree_increment(cur
, 0, &i
)))
4995 cur
->bc_rec
.b
= new;
4996 error
= xfs_btree_insert(cur
, &i
);
4997 if (error
&& error
!= -ENOSPC
)
5000 * If get no-space back from btree insert,
5001 * it tried a split, and we have a zero
5002 * block reservation.
5003 * Fix up our state and return the error.
5005 if (error
== -ENOSPC
) {
5007 * Reset the cursor, don't trust
5008 * it after any insert operation.
5010 if ((error
= xfs_bmbt_lookup_eq(cur
,
5015 XFS_WANT_CORRUPTED_GOTO(mp
,
5018 * Update the btree record back
5019 * to the original value.
5021 if ((error
= xfs_bmbt_update(cur
,
5028 * Reset the extent record back
5029 * to the original value.
5031 xfs_bmbt_set_blockcount(ep
,
5037 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
5039 flags
|= xfs_ilog_fext(whichfork
);
5040 XFS_IFORK_NEXT_SET(ip
, whichfork
,
5041 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
5043 xfs_filblks_t stolen
;
5044 ASSERT(whichfork
== XFS_DATA_FORK
);
5047 * Distribute the original indlen reservation across the
5048 * two new extents. Steal blocks from the deleted extent
5049 * if necessary. Stealing blocks simply fudges the
5050 * fdblocks accounting in xfs_bunmapi().
5052 temp
= xfs_bmap_worst_indlen(ip
, got
.br_blockcount
);
5053 temp2
= xfs_bmap_worst_indlen(ip
, new.br_blockcount
);
5054 stolen
= xfs_bmap_split_indlen(da_old
, &temp
, &temp2
,
5055 del
->br_blockcount
);
5056 da_new
= temp
+ temp2
- stolen
;
5057 del
->br_blockcount
-= stolen
;
5060 * Set the reservation for each extent. Warn if either
5061 * is zero as this can lead to delalloc problems.
5063 WARN_ON_ONCE(!temp
|| !temp2
);
5064 xfs_bmbt_set_startblock(ep
, nullstartblock((int)temp
));
5065 new.br_startblock
= nullstartblock((int)temp2
);
5067 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
5068 xfs_iext_insert(ip
, *idx
+ 1, 1, &new, state
);
5073 * If we need to, add to list of extents to delete.
5076 xfs_bmap_add_free(del
->br_startblock
, del
->br_blockcount
, flist
,
5079 * Adjust inode # blocks in the file.
5082 ip
->i_d
.di_nblocks
-= nblks
;
5084 * Adjust quota data.
5087 xfs_trans_mod_dquot_byino(tp
, ip
, qfield
, (long)-nblks
);
5090 * Account for change in delayed indirect blocks.
5091 * Nothing to do for disk quota accounting here.
5093 ASSERT(da_old
>= da_new
);
5094 if (da_old
> da_new
)
5095 xfs_mod_fdblocks(mp
, (int64_t)(da_old
- da_new
), false);
5102 * Unmap (remove) blocks from a file.
5103 * If nexts is nonzero then the number of extents to remove is limited to
5104 * that value. If not all extents in the block range can be removed then
5109 xfs_trans_t
*tp
, /* transaction pointer */
5110 struct xfs_inode
*ip
, /* incore inode */
5111 xfs_fileoff_t bno
, /* starting offset to unmap */
5112 xfs_filblks_t len
, /* length to unmap in file */
5113 int flags
, /* misc flags */
5114 xfs_extnum_t nexts
, /* number of extents max */
5115 xfs_fsblock_t
*firstblock
, /* first allocated block
5116 controls a.g. for allocs */
5117 xfs_bmap_free_t
*flist
, /* i/o: list extents to free */
5118 int *done
) /* set if not done yet */
5120 xfs_btree_cur_t
*cur
; /* bmap btree cursor */
5121 xfs_bmbt_irec_t del
; /* extent being deleted */
5122 int eof
; /* is deleting at eof */
5123 xfs_bmbt_rec_host_t
*ep
; /* extent record pointer */
5124 int error
; /* error return value */
5125 xfs_extnum_t extno
; /* extent number in list */
5126 xfs_bmbt_irec_t got
; /* current extent record */
5127 xfs_ifork_t
*ifp
; /* inode fork pointer */
5128 int isrt
; /* freeing in rt area */
5129 xfs_extnum_t lastx
; /* last extent index used */
5130 int logflags
; /* transaction logging flags */
5131 xfs_extlen_t mod
; /* rt extent offset */
5132 xfs_mount_t
*mp
; /* mount structure */
5133 xfs_extnum_t nextents
; /* number of file extents */
5134 xfs_bmbt_irec_t prev
; /* previous extent record */
5135 xfs_fileoff_t start
; /* first file offset deleted */
5136 int tmp_logflags
; /* partial logging flags */
5137 int wasdel
; /* was a delayed alloc extent */
5138 int whichfork
; /* data or attribute fork */
5141 trace_xfs_bunmap(ip
, bno
, len
, flags
, _RET_IP_
);
5143 whichfork
= (flags
& XFS_BMAPI_ATTRFORK
) ?
5144 XFS_ATTR_FORK
: XFS_DATA_FORK
;
5145 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5147 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
5148 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)) {
5149 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW
,
5151 return -EFSCORRUPTED
;
5154 if (XFS_FORCED_SHUTDOWN(mp
))
5157 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
5161 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
5162 (error
= xfs_iread_extents(tp
, ip
, whichfork
)))
5164 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
5165 if (nextents
== 0) {
5169 XFS_STATS_INC(mp
, xs_blk_unmap
);
5170 isrt
= (whichfork
== XFS_DATA_FORK
) && XFS_IS_REALTIME_INODE(ip
);
5172 bno
= start
+ len
- 1;
5173 ep
= xfs_bmap_search_extents(ip
, bno
, whichfork
, &eof
, &lastx
, &got
,
5177 * Check to see if the given block number is past the end of the
5178 * file, back up to the last block if so...
5181 ep
= xfs_iext_get_ext(ifp
, --lastx
);
5182 xfs_bmbt_get_all(ep
, &got
);
5183 bno
= got
.br_startoff
+ got
.br_blockcount
- 1;
5186 if (ifp
->if_flags
& XFS_IFBROOT
) {
5187 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
);
5188 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
5189 cur
->bc_private
.b
.firstblock
= *firstblock
;
5190 cur
->bc_private
.b
.flist
= flist
;
5191 cur
->bc_private
.b
.flags
= 0;
5197 * Synchronize by locking the bitmap inode.
5199 xfs_ilock(mp
->m_rbmip
, XFS_ILOCK_EXCL
);
5200 xfs_trans_ijoin(tp
, mp
->m_rbmip
, XFS_ILOCK_EXCL
);
5204 while (bno
!= (xfs_fileoff_t
)-1 && bno
>= start
&& lastx
>= 0 &&
5205 (nexts
== 0 || extno
< nexts
)) {
5207 * Is the found extent after a hole in which bno lives?
5208 * Just back up to the previous extent, if so.
5210 if (got
.br_startoff
> bno
) {
5213 ep
= xfs_iext_get_ext(ifp
, lastx
);
5214 xfs_bmbt_get_all(ep
, &got
);
5217 * Is the last block of this extent before the range
5218 * we're supposed to delete? If so, we're done.
5220 bno
= XFS_FILEOFF_MIN(bno
,
5221 got
.br_startoff
+ got
.br_blockcount
- 1);
5225 * Then deal with the (possibly delayed) allocated space
5230 wasdel
= isnullstartblock(del
.br_startblock
);
5231 if (got
.br_startoff
< start
) {
5232 del
.br_startoff
= start
;
5233 del
.br_blockcount
-= start
- got
.br_startoff
;
5235 del
.br_startblock
+= start
- got
.br_startoff
;
5237 if (del
.br_startoff
+ del
.br_blockcount
> bno
+ 1)
5238 del
.br_blockcount
= bno
+ 1 - del
.br_startoff
;
5239 sum
= del
.br_startblock
+ del
.br_blockcount
;
5241 (mod
= do_mod(sum
, mp
->m_sb
.sb_rextsize
))) {
5243 * Realtime extent not lined up at the end.
5244 * The extent could have been split into written
5245 * and unwritten pieces, or we could just be
5246 * unmapping part of it. But we can't really
5247 * get rid of part of a realtime extent.
5249 if (del
.br_state
== XFS_EXT_UNWRITTEN
||
5250 !xfs_sb_version_hasextflgbit(&mp
->m_sb
)) {
5252 * This piece is unwritten, or we're not
5253 * using unwritten extents. Skip over it.
5256 bno
-= mod
> del
.br_blockcount
?
5257 del
.br_blockcount
: mod
;
5258 if (bno
< got
.br_startoff
) {
5260 xfs_bmbt_get_all(xfs_iext_get_ext(
5266 * It's written, turn it unwritten.
5267 * This is better than zeroing it.
5269 ASSERT(del
.br_state
== XFS_EXT_NORM
);
5270 ASSERT(tp
->t_blk_res
> 0);
5272 * If this spans a realtime extent boundary,
5273 * chop it back to the start of the one we end at.
5275 if (del
.br_blockcount
> mod
) {
5276 del
.br_startoff
+= del
.br_blockcount
- mod
;
5277 del
.br_startblock
+= del
.br_blockcount
- mod
;
5278 del
.br_blockcount
= mod
;
5280 del
.br_state
= XFS_EXT_UNWRITTEN
;
5281 error
= xfs_bmap_add_extent_unwritten_real(tp
, ip
,
5282 &lastx
, &cur
, &del
, firstblock
, flist
,
5288 if (isrt
&& (mod
= do_mod(del
.br_startblock
, mp
->m_sb
.sb_rextsize
))) {
5290 * Realtime extent is lined up at the end but not
5291 * at the front. We'll get rid of full extents if
5294 mod
= mp
->m_sb
.sb_rextsize
- mod
;
5295 if (del
.br_blockcount
> mod
) {
5296 del
.br_blockcount
-= mod
;
5297 del
.br_startoff
+= mod
;
5298 del
.br_startblock
+= mod
;
5299 } else if ((del
.br_startoff
== start
&&
5300 (del
.br_state
== XFS_EXT_UNWRITTEN
||
5301 tp
->t_blk_res
== 0)) ||
5302 !xfs_sb_version_hasextflgbit(&mp
->m_sb
)) {
5304 * Can't make it unwritten. There isn't
5305 * a full extent here so just skip it.
5307 ASSERT(bno
>= del
.br_blockcount
);
5308 bno
-= del
.br_blockcount
;
5309 if (got
.br_startoff
> bno
) {
5311 ep
= xfs_iext_get_ext(ifp
,
5313 xfs_bmbt_get_all(ep
, &got
);
5317 } else if (del
.br_state
== XFS_EXT_UNWRITTEN
) {
5319 * This one is already unwritten.
5320 * It must have a written left neighbor.
5321 * Unwrite the killed part of that one and
5325 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
,
5327 ASSERT(prev
.br_state
== XFS_EXT_NORM
);
5328 ASSERT(!isnullstartblock(prev
.br_startblock
));
5329 ASSERT(del
.br_startblock
==
5330 prev
.br_startblock
+ prev
.br_blockcount
);
5331 if (prev
.br_startoff
< start
) {
5332 mod
= start
- prev
.br_startoff
;
5333 prev
.br_blockcount
-= mod
;
5334 prev
.br_startblock
+= mod
;
5335 prev
.br_startoff
= start
;
5337 prev
.br_state
= XFS_EXT_UNWRITTEN
;
5339 error
= xfs_bmap_add_extent_unwritten_real(tp
,
5340 ip
, &lastx
, &cur
, &prev
,
5341 firstblock
, flist
, &logflags
);
5346 ASSERT(del
.br_state
== XFS_EXT_NORM
);
5347 del
.br_state
= XFS_EXT_UNWRITTEN
;
5348 error
= xfs_bmap_add_extent_unwritten_real(tp
,
5349 ip
, &lastx
, &cur
, &del
,
5350 firstblock
, flist
, &logflags
);
5358 * If it's the case where the directory code is running
5359 * with no block reservation, and the deleted block is in
5360 * the middle of its extent, and the resulting insert
5361 * of an extent would cause transformation to btree format,
5362 * then reject it. The calling code will then swap
5363 * blocks around instead.
5364 * We have to do this now, rather than waiting for the
5365 * conversion to btree format, since the transaction
5368 if (!wasdel
&& tp
->t_blk_res
== 0 &&
5369 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
&&
5370 XFS_IFORK_NEXTENTS(ip
, whichfork
) >= /* Note the >= */
5371 XFS_IFORK_MAXEXT(ip
, whichfork
) &&
5372 del
.br_startoff
> got
.br_startoff
&&
5373 del
.br_startoff
+ del
.br_blockcount
<
5374 got
.br_startoff
+ got
.br_blockcount
) {
5380 * Unreserve quota and update realtime free space, if
5381 * appropriate. If delayed allocation, update the inode delalloc
5382 * counter now and wait to update the sb counters as
5383 * xfs_bmap_del_extent() might need to borrow some blocks.
5386 ASSERT(startblockval(del
.br_startblock
) > 0);
5388 xfs_filblks_t rtexts
;
5390 rtexts
= XFS_FSB_TO_B(mp
, del
.br_blockcount
);
5391 do_div(rtexts
, mp
->m_sb
.sb_rextsize
);
5392 xfs_mod_frextents(mp
, (int64_t)rtexts
);
5393 (void)xfs_trans_reserve_quota_nblks(NULL
,
5394 ip
, -((long)del
.br_blockcount
), 0,
5395 XFS_QMOPT_RES_RTBLKS
);
5397 (void)xfs_trans_reserve_quota_nblks(NULL
,
5398 ip
, -((long)del
.br_blockcount
), 0,
5399 XFS_QMOPT_RES_REGBLKS
);
5401 ip
->i_delayed_blks
-= del
.br_blockcount
;
5403 cur
->bc_private
.b
.flags
|=
5404 XFS_BTCUR_BPRV_WASDEL
;
5406 cur
->bc_private
.b
.flags
&= ~XFS_BTCUR_BPRV_WASDEL
;
5408 error
= xfs_bmap_del_extent(ip
, tp
, &lastx
, flist
, cur
, &del
,
5409 &tmp_logflags
, whichfork
);
5410 logflags
|= tmp_logflags
;
5414 if (!isrt
&& wasdel
)
5415 xfs_mod_fdblocks(mp
, (int64_t)del
.br_blockcount
, false);
5417 bno
= del
.br_startoff
- 1;
5420 * If not done go on to the next (previous) record.
5422 if (bno
!= (xfs_fileoff_t
)-1 && bno
>= start
) {
5424 ep
= xfs_iext_get_ext(ifp
, lastx
);
5425 if (xfs_bmbt_get_startoff(ep
) > bno
) {
5427 ep
= xfs_iext_get_ext(ifp
,
5430 xfs_bmbt_get_all(ep
, &got
);
5435 *done
= bno
== (xfs_fileoff_t
)-1 || bno
< start
|| lastx
< 0;
5438 * Convert to a btree if necessary.
5440 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
5441 ASSERT(cur
== NULL
);
5442 error
= xfs_bmap_extents_to_btree(tp
, ip
, firstblock
, flist
,
5443 &cur
, 0, &tmp_logflags
, whichfork
);
5444 logflags
|= tmp_logflags
;
5449 * transform from btree to extents, give it cur
5451 else if (xfs_bmap_wants_extents(ip
, whichfork
)) {
5452 ASSERT(cur
!= NULL
);
5453 error
= xfs_bmap_btree_to_extents(tp
, ip
, cur
, &tmp_logflags
,
5455 logflags
|= tmp_logflags
;
5460 * transform from extents to local?
5465 * Log everything. Do this after conversion, there's no point in
5466 * logging the extent records if we've converted to btree format.
5468 if ((logflags
& xfs_ilog_fext(whichfork
)) &&
5469 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
5470 logflags
&= ~xfs_ilog_fext(whichfork
);
5471 else if ((logflags
& xfs_ilog_fbroot(whichfork
)) &&
5472 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)
5473 logflags
&= ~xfs_ilog_fbroot(whichfork
);
5475 * Log inode even in the error case, if the transaction
5476 * is dirty we'll need to shut down the filesystem.
5479 xfs_trans_log_inode(tp
, ip
, logflags
);
5482 *firstblock
= cur
->bc_private
.b
.firstblock
;
5483 cur
->bc_private
.b
.allocated
= 0;
5485 xfs_btree_del_cursor(cur
,
5486 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
5492 * Determine whether an extent shift can be accomplished by a merge with the
5493 * extent that precedes the target hole of the shift.
5497 struct xfs_bmbt_irec
*left
, /* preceding extent */
5498 struct xfs_bmbt_irec
*got
, /* current extent to shift */
5499 xfs_fileoff_t shift
) /* shift fsb */
5501 xfs_fileoff_t startoff
;
5503 startoff
= got
->br_startoff
- shift
;
5506 * The extent, once shifted, must be adjacent in-file and on-disk with
5507 * the preceding extent.
5509 if ((left
->br_startoff
+ left
->br_blockcount
!= startoff
) ||
5510 (left
->br_startblock
+ left
->br_blockcount
!= got
->br_startblock
) ||
5511 (left
->br_state
!= got
->br_state
) ||
5512 (left
->br_blockcount
+ got
->br_blockcount
> MAXEXTLEN
))
5519 * A bmap extent shift adjusts the file offset of an extent to fill a preceding
5520 * hole in the file. If an extent shift would result in the extent being fully
5521 * adjacent to the extent that currently precedes the hole, we can merge with
5522 * the preceding extent rather than do the shift.
5524 * This function assumes the caller has verified a shift-by-merge is possible
5525 * with the provided extents via xfs_bmse_can_merge().
5529 struct xfs_inode
*ip
,
5531 xfs_fileoff_t shift
, /* shift fsb */
5532 int current_ext
, /* idx of gotp */
5533 struct xfs_bmbt_rec_host
*gotp
, /* extent to shift */
5534 struct xfs_bmbt_rec_host
*leftp
, /* preceding extent */
5535 struct xfs_btree_cur
*cur
,
5536 int *logflags
) /* output */
5538 struct xfs_bmbt_irec got
;
5539 struct xfs_bmbt_irec left
;
5540 xfs_filblks_t blockcount
;
5542 struct xfs_mount
*mp
= ip
->i_mount
;
5544 xfs_bmbt_get_all(gotp
, &got
);
5545 xfs_bmbt_get_all(leftp
, &left
);
5546 blockcount
= left
.br_blockcount
+ got
.br_blockcount
;
5548 ASSERT(xfs_isilocked(ip
, XFS_IOLOCK_EXCL
));
5549 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
5550 ASSERT(xfs_bmse_can_merge(&left
, &got
, shift
));
5553 * Merge the in-core extents. Note that the host record pointers and
5554 * current_ext index are invalid once the extent has been removed via
5555 * xfs_iext_remove().
5557 xfs_bmbt_set_blockcount(leftp
, blockcount
);
5558 xfs_iext_remove(ip
, current_ext
, 1, 0);
5561 * Update the on-disk extent count, the btree if necessary and log the
5564 XFS_IFORK_NEXT_SET(ip
, whichfork
,
5565 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
5566 *logflags
|= XFS_ILOG_CORE
;
5568 *logflags
|= XFS_ILOG_DEXT
;
5572 /* lookup and remove the extent to merge */
5573 error
= xfs_bmbt_lookup_eq(cur
, got
.br_startoff
, got
.br_startblock
,
5574 got
.br_blockcount
, &i
);
5577 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
5579 error
= xfs_btree_delete(cur
, &i
);
5582 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
5584 /* lookup and update size of the previous extent */
5585 error
= xfs_bmbt_lookup_eq(cur
, left
.br_startoff
, left
.br_startblock
,
5586 left
.br_blockcount
, &i
);
5589 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
5591 left
.br_blockcount
= blockcount
;
5593 return xfs_bmbt_update(cur
, left
.br_startoff
, left
.br_startblock
,
5594 left
.br_blockcount
, left
.br_state
);
5598 * Shift a single extent.
5602 struct xfs_inode
*ip
,
5604 xfs_fileoff_t offset_shift_fsb
,
5606 struct xfs_bmbt_rec_host
*gotp
,
5607 struct xfs_btree_cur
*cur
,
5609 enum shift_direction direction
)
5611 struct xfs_ifork
*ifp
;
5612 struct xfs_mount
*mp
;
5613 xfs_fileoff_t startoff
;
5614 struct xfs_bmbt_rec_host
*adj_irecp
;
5615 struct xfs_bmbt_irec got
;
5616 struct xfs_bmbt_irec adj_irec
;
5622 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5623 total_extents
= ifp
->if_bytes
/ sizeof(xfs_bmbt_rec_t
);
5625 xfs_bmbt_get_all(gotp
, &got
);
5627 /* delalloc extents should be prevented by caller */
5628 XFS_WANT_CORRUPTED_RETURN(mp
, !isnullstartblock(got
.br_startblock
));
5630 if (direction
== SHIFT_LEFT
) {
5631 startoff
= got
.br_startoff
- offset_shift_fsb
;
5634 * Check for merge if we've got an extent to the left,
5635 * otherwise make sure there's enough room at the start
5636 * of the file for the shift.
5638 if (!*current_ext
) {
5639 if (got
.br_startoff
< offset_shift_fsb
)
5641 goto update_current_ext
;
5644 * grab the left extent and check for a large
5647 adj_irecp
= xfs_iext_get_ext(ifp
, *current_ext
- 1);
5648 xfs_bmbt_get_all(adj_irecp
, &adj_irec
);
5651 adj_irec
.br_startoff
+ adj_irec
.br_blockcount
)
5654 /* check whether to merge the extent or shift it down */
5655 if (xfs_bmse_can_merge(&adj_irec
, &got
,
5656 offset_shift_fsb
)) {
5657 return xfs_bmse_merge(ip
, whichfork
, offset_shift_fsb
,
5658 *current_ext
, gotp
, adj_irecp
,
5662 startoff
= got
.br_startoff
+ offset_shift_fsb
;
5663 /* nothing to move if this is the last extent */
5664 if (*current_ext
>= (total_extents
- 1))
5665 goto update_current_ext
;
5667 * If this is not the last extent in the file, make sure there
5668 * is enough room between current extent and next extent for
5669 * accommodating the shift.
5671 adj_irecp
= xfs_iext_get_ext(ifp
, *current_ext
+ 1);
5672 xfs_bmbt_get_all(adj_irecp
, &adj_irec
);
5673 if (startoff
+ got
.br_blockcount
> adj_irec
.br_startoff
)
5676 * Unlike a left shift (which involves a hole punch),
5677 * a right shift does not modify extent neighbors
5678 * in any way. We should never find mergeable extents
5679 * in this scenario. Check anyways and warn if we
5680 * encounter two extents that could be one.
5682 if (xfs_bmse_can_merge(&got
, &adj_irec
, offset_shift_fsb
))
5686 * Increment the extent index for the next iteration, update the start
5687 * offset of the in-core extent and update the btree if applicable.
5690 if (direction
== SHIFT_LEFT
)
5694 xfs_bmbt_set_startoff(gotp
, startoff
);
5695 *logflags
|= XFS_ILOG_CORE
;
5697 *logflags
|= XFS_ILOG_DEXT
;
5701 error
= xfs_bmbt_lookup_eq(cur
, got
.br_startoff
, got
.br_startblock
,
5702 got
.br_blockcount
, &i
);
5705 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
5707 got
.br_startoff
= startoff
;
5708 return xfs_bmbt_update(cur
, got
.br_startoff
, got
.br_startblock
,
5709 got
.br_blockcount
, got
.br_state
);
5713 * Shift extent records to the left/right to cover/create a hole.
5715 * The maximum number of extents to be shifted in a single operation is
5716 * @num_exts. @stop_fsb specifies the file offset at which to stop shift and the
5717 * file offset where we've left off is returned in @next_fsb. @offset_shift_fsb
5718 * is the length by which each extent is shifted. If there is no hole to shift
5719 * the extents into, this will be considered invalid operation and we abort
5723 xfs_bmap_shift_extents(
5724 struct xfs_trans
*tp
,
5725 struct xfs_inode
*ip
,
5726 xfs_fileoff_t
*next_fsb
,
5727 xfs_fileoff_t offset_shift_fsb
,
5729 xfs_fileoff_t stop_fsb
,
5730 xfs_fsblock_t
*firstblock
,
5731 struct xfs_bmap_free
*flist
,
5732 enum shift_direction direction
,
5735 struct xfs_btree_cur
*cur
= NULL
;
5736 struct xfs_bmbt_rec_host
*gotp
;
5737 struct xfs_bmbt_irec got
;
5738 struct xfs_mount
*mp
= ip
->i_mount
;
5739 struct xfs_ifork
*ifp
;
5740 xfs_extnum_t nexts
= 0;
5741 xfs_extnum_t current_ext
;
5742 xfs_extnum_t total_extents
;
5743 xfs_extnum_t stop_extent
;
5745 int whichfork
= XFS_DATA_FORK
;
5748 if (unlikely(XFS_TEST_ERROR(
5749 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
5750 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
5751 mp
, XFS_ERRTAG_BMAPIFORMAT
, XFS_RANDOM_BMAPIFORMAT
))) {
5752 XFS_ERROR_REPORT("xfs_bmap_shift_extents",
5753 XFS_ERRLEVEL_LOW
, mp
);
5754 return -EFSCORRUPTED
;
5757 if (XFS_FORCED_SHUTDOWN(mp
))
5760 ASSERT(xfs_isilocked(ip
, XFS_IOLOCK_EXCL
));
5761 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
5762 ASSERT(direction
== SHIFT_LEFT
|| direction
== SHIFT_RIGHT
);
5763 ASSERT(*next_fsb
!= NULLFSBLOCK
|| direction
== SHIFT_RIGHT
);
5765 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5766 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
5767 /* Read in all the extents */
5768 error
= xfs_iread_extents(tp
, ip
, whichfork
);
5773 if (ifp
->if_flags
& XFS_IFBROOT
) {
5774 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
5775 cur
->bc_private
.b
.firstblock
= *firstblock
;
5776 cur
->bc_private
.b
.flist
= flist
;
5777 cur
->bc_private
.b
.flags
= 0;
5781 * There may be delalloc extents in the data fork before the range we
5782 * are collapsing out, so we cannot use the count of real extents here.
5783 * Instead we have to calculate it from the incore fork.
5785 total_extents
= ifp
->if_bytes
/ sizeof(xfs_bmbt_rec_t
);
5786 if (total_extents
== 0) {
5792 * In case of first right shift, we need to initialize next_fsb
5794 if (*next_fsb
== NULLFSBLOCK
) {
5795 gotp
= xfs_iext_get_ext(ifp
, total_extents
- 1);
5796 xfs_bmbt_get_all(gotp
, &got
);
5797 *next_fsb
= got
.br_startoff
;
5798 if (stop_fsb
> *next_fsb
) {
5804 /* Lookup the extent index at which we have to stop */
5805 if (direction
== SHIFT_RIGHT
) {
5806 gotp
= xfs_iext_bno_to_ext(ifp
, stop_fsb
, &stop_extent
);
5807 /* Make stop_extent exclusive of shift range */
5810 stop_extent
= total_extents
;
5813 * Look up the extent index for the fsb where we start shifting. We can
5814 * henceforth iterate with current_ext as extent list changes are locked
5817 * gotp can be null in 2 cases: 1) if there are no extents or 2)
5818 * *next_fsb lies in a hole beyond which there are no extents. Either
5821 gotp
= xfs_iext_bno_to_ext(ifp
, *next_fsb
, ¤t_ext
);
5827 /* some sanity checking before we finally start shifting extents */
5828 if ((direction
== SHIFT_LEFT
&& current_ext
>= stop_extent
) ||
5829 (direction
== SHIFT_RIGHT
&& current_ext
<= stop_extent
)) {
5834 while (nexts
++ < num_exts
) {
5835 error
= xfs_bmse_shift_one(ip
, whichfork
, offset_shift_fsb
,
5836 ¤t_ext
, gotp
, cur
, &logflags
,
5841 * If there was an extent merge during the shift, the extent
5842 * count can change. Update the total and grade the next record.
5844 if (direction
== SHIFT_LEFT
) {
5845 total_extents
= ifp
->if_bytes
/ sizeof(xfs_bmbt_rec_t
);
5846 stop_extent
= total_extents
;
5849 if (current_ext
== stop_extent
) {
5851 *next_fsb
= NULLFSBLOCK
;
5854 gotp
= xfs_iext_get_ext(ifp
, current_ext
);
5858 xfs_bmbt_get_all(gotp
, &got
);
5859 *next_fsb
= got
.br_startoff
;
5864 xfs_btree_del_cursor(cur
,
5865 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
5868 xfs_trans_log_inode(tp
, ip
, logflags
);
5874 * Splits an extent into two extents at split_fsb block such that it is
5875 * the first block of the current_ext. @current_ext is a target extent
5876 * to be split. @split_fsb is a block where the extents is split.
5877 * If split_fsb lies in a hole or the first block of extents, just return 0.
5880 xfs_bmap_split_extent_at(
5881 struct xfs_trans
*tp
,
5882 struct xfs_inode
*ip
,
5883 xfs_fileoff_t split_fsb
,
5884 xfs_fsblock_t
*firstfsb
,
5885 struct xfs_bmap_free
*free_list
)
5887 int whichfork
= XFS_DATA_FORK
;
5888 struct xfs_btree_cur
*cur
= NULL
;
5889 struct xfs_bmbt_rec_host
*gotp
;
5890 struct xfs_bmbt_irec got
;
5891 struct xfs_bmbt_irec
new; /* split extent */
5892 struct xfs_mount
*mp
= ip
->i_mount
;
5893 struct xfs_ifork
*ifp
;
5894 xfs_fsblock_t gotblkcnt
; /* new block count for got */
5895 xfs_extnum_t current_ext
;
5900 if (unlikely(XFS_TEST_ERROR(
5901 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
5902 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
5903 mp
, XFS_ERRTAG_BMAPIFORMAT
, XFS_RANDOM_BMAPIFORMAT
))) {
5904 XFS_ERROR_REPORT("xfs_bmap_split_extent_at",
5905 XFS_ERRLEVEL_LOW
, mp
);
5906 return -EFSCORRUPTED
;
5909 if (XFS_FORCED_SHUTDOWN(mp
))
5912 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5913 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
5914 /* Read in all the extents */
5915 error
= xfs_iread_extents(tp
, ip
, whichfork
);
5921 * gotp can be null in 2 cases: 1) if there are no extents
5922 * or 2) split_fsb lies in a hole beyond which there are
5923 * no extents. Either way, we are done.
5925 gotp
= xfs_iext_bno_to_ext(ifp
, split_fsb
, ¤t_ext
);
5929 xfs_bmbt_get_all(gotp
, &got
);
5932 * Check split_fsb lies in a hole or the start boundary offset
5935 if (got
.br_startoff
>= split_fsb
)
5938 gotblkcnt
= split_fsb
- got
.br_startoff
;
5939 new.br_startoff
= split_fsb
;
5940 new.br_startblock
= got
.br_startblock
+ gotblkcnt
;
5941 new.br_blockcount
= got
.br_blockcount
- gotblkcnt
;
5942 new.br_state
= got
.br_state
;
5944 if (ifp
->if_flags
& XFS_IFBROOT
) {
5945 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
5946 cur
->bc_private
.b
.firstblock
= *firstfsb
;
5947 cur
->bc_private
.b
.flist
= free_list
;
5948 cur
->bc_private
.b
.flags
= 0;
5949 error
= xfs_bmbt_lookup_eq(cur
, got
.br_startoff
,
5955 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, del_cursor
);
5958 xfs_bmbt_set_blockcount(gotp
, gotblkcnt
);
5959 got
.br_blockcount
= gotblkcnt
;
5961 logflags
= XFS_ILOG_CORE
;
5963 error
= xfs_bmbt_update(cur
, got
.br_startoff
,
5970 logflags
|= XFS_ILOG_DEXT
;
5972 /* Add new extent */
5974 xfs_iext_insert(ip
, current_ext
, 1, &new, 0);
5975 XFS_IFORK_NEXT_SET(ip
, whichfork
,
5976 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
5979 error
= xfs_bmbt_lookup_eq(cur
, new.br_startoff
,
5980 new.br_startblock
, new.br_blockcount
,
5984 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, del_cursor
);
5985 cur
->bc_rec
.b
.br_state
= new.br_state
;
5987 error
= xfs_btree_insert(cur
, &i
);
5990 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, del_cursor
);
5994 * Convert to a btree if necessary.
5996 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
5997 int tmp_logflags
; /* partial log flag return val */
5999 ASSERT(cur
== NULL
);
6000 error
= xfs_bmap_extents_to_btree(tp
, ip
, firstfsb
, free_list
,
6001 &cur
, 0, &tmp_logflags
, whichfork
);
6002 logflags
|= tmp_logflags
;
6007 cur
->bc_private
.b
.allocated
= 0;
6008 xfs_btree_del_cursor(cur
,
6009 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
6013 xfs_trans_log_inode(tp
, ip
, logflags
);
6018 xfs_bmap_split_extent(
6019 struct xfs_inode
*ip
,
6020 xfs_fileoff_t split_fsb
)
6022 struct xfs_mount
*mp
= ip
->i_mount
;
6023 struct xfs_trans
*tp
;
6024 struct xfs_bmap_free free_list
;
6025 xfs_fsblock_t firstfsb
;
6028 error
= xfs_trans_alloc(mp
, &M_RES(mp
)->tr_write
,
6029 XFS_DIOSTRAT_SPACE_RES(mp
, 0), 0, 0, &tp
);
6033 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
6034 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
);
6036 xfs_bmap_init(&free_list
, &firstfsb
);
6038 error
= xfs_bmap_split_extent_at(tp
, ip
, split_fsb
,
6039 &firstfsb
, &free_list
);
6043 error
= xfs_bmap_finish(&tp
, &free_list
, NULL
);
6047 return xfs_trans_commit(tp
);
6050 xfs_bmap_cancel(&free_list
);
6051 xfs_trans_cancel(tp
);