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 * Debug/sanity checking code
252 xfs_bmap_sanity_check(
253 struct xfs_mount
*mp
,
257 struct xfs_btree_block
*block
= XFS_BUF_TO_BLOCK(bp
);
259 if (block
->bb_magic
!= cpu_to_be32(XFS_BMAP_CRC_MAGIC
) &&
260 block
->bb_magic
!= cpu_to_be32(XFS_BMAP_MAGIC
))
263 if (be16_to_cpu(block
->bb_level
) != level
||
264 be16_to_cpu(block
->bb_numrecs
) == 0 ||
265 be16_to_cpu(block
->bb_numrecs
) > mp
->m_bmap_dmxr
[level
!= 0])
272 STATIC
struct xfs_buf
*
274 struct xfs_btree_cur
*cur
,
277 struct xfs_log_item_desc
*lidp
;
283 for (i
= 0; i
< XFS_BTREE_MAXLEVELS
; i
++) {
284 if (!cur
->bc_bufs
[i
])
286 if (XFS_BUF_ADDR(cur
->bc_bufs
[i
]) == bno
)
287 return cur
->bc_bufs
[i
];
290 /* Chase down all the log items to see if the bp is there */
291 list_for_each_entry(lidp
, &cur
->bc_tp
->t_items
, lid_trans
) {
292 struct xfs_buf_log_item
*bip
;
293 bip
= (struct xfs_buf_log_item
*)lidp
->lid_item
;
294 if (bip
->bli_item
.li_type
== XFS_LI_BUF
&&
295 XFS_BUF_ADDR(bip
->bli_buf
) == bno
)
304 struct xfs_btree_block
*block
,
310 __be64
*pp
, *thispa
; /* pointer to block address */
311 xfs_bmbt_key_t
*prevp
, *keyp
;
313 ASSERT(be16_to_cpu(block
->bb_level
) > 0);
316 for( i
= 1; i
<= xfs_btree_get_numrecs(block
); i
++) {
317 dmxr
= mp
->m_bmap_dmxr
[0];
318 keyp
= XFS_BMBT_KEY_ADDR(mp
, block
, i
);
321 ASSERT(be64_to_cpu(prevp
->br_startoff
) <
322 be64_to_cpu(keyp
->br_startoff
));
327 * Compare the block numbers to see if there are dups.
330 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, i
, sz
);
332 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, i
, dmxr
);
334 for (j
= i
+1; j
<= be16_to_cpu(block
->bb_numrecs
); j
++) {
336 thispa
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, j
, sz
);
338 thispa
= XFS_BMBT_PTR_ADDR(mp
, block
, j
, dmxr
);
339 if (*thispa
== *pp
) {
340 xfs_warn(mp
, "%s: thispa(%d) == pp(%d) %Ld",
342 (unsigned long long)be64_to_cpu(*thispa
));
343 panic("%s: ptrs are equal in node\n",
351 * Check that the extents for the inode ip are in the right order in all
356 xfs_bmap_check_leaf_extents(
357 xfs_btree_cur_t
*cur
, /* btree cursor or null */
358 xfs_inode_t
*ip
, /* incore inode pointer */
359 int whichfork
) /* data or attr fork */
361 struct xfs_btree_block
*block
; /* current btree block */
362 xfs_fsblock_t bno
; /* block # of "block" */
363 xfs_buf_t
*bp
; /* buffer for "block" */
364 int error
; /* error return value */
365 xfs_extnum_t i
=0, j
; /* index into the extents list */
366 xfs_ifork_t
*ifp
; /* fork structure */
367 int level
; /* btree level, for checking */
368 xfs_mount_t
*mp
; /* file system mount structure */
369 __be64
*pp
; /* pointer to block address */
370 xfs_bmbt_rec_t
*ep
; /* pointer to current extent */
371 xfs_bmbt_rec_t last
= {0, 0}; /* last extent in prev block */
372 xfs_bmbt_rec_t
*nextp
; /* pointer to next extent */
375 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
) {
381 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
382 block
= ifp
->if_broot
;
384 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
386 level
= be16_to_cpu(block
->bb_level
);
388 xfs_check_block(block
, mp
, 1, ifp
->if_broot_bytes
);
389 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, 1, ifp
->if_broot_bytes
);
390 bno
= be64_to_cpu(*pp
);
392 ASSERT(bno
!= NULLFSBLOCK
);
393 ASSERT(XFS_FSB_TO_AGNO(mp
, bno
) < mp
->m_sb
.sb_agcount
);
394 ASSERT(XFS_FSB_TO_AGBNO(mp
, bno
) < mp
->m_sb
.sb_agblocks
);
397 * Go down the tree until leaf level is reached, following the first
398 * pointer (leftmost) at each level.
400 while (level
-- > 0) {
401 /* See if buf is in cur first */
403 bp
= xfs_bmap_get_bp(cur
, XFS_FSB_TO_DADDR(mp
, bno
));
406 error
= xfs_btree_read_bufl(mp
, NULL
, bno
, 0, &bp
,
412 block
= XFS_BUF_TO_BLOCK(bp
);
413 XFS_WANT_CORRUPTED_GOTO(
414 xfs_bmap_sanity_check(mp
, bp
, level
),
420 * Check this block for basic sanity (increasing keys and
421 * no duplicate blocks).
424 xfs_check_block(block
, mp
, 0, 0);
425 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, mp
->m_bmap_dmxr
[1]);
426 bno
= be64_to_cpu(*pp
);
427 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp
, bno
), error0
);
430 xfs_trans_brelse(NULL
, bp
);
435 * Here with bp and block set to the leftmost leaf node in the tree.
440 * Loop over all leaf nodes checking that all extents are in the right order.
443 xfs_fsblock_t nextbno
;
444 xfs_extnum_t num_recs
;
447 num_recs
= xfs_btree_get_numrecs(block
);
450 * Read-ahead the next leaf block, if any.
453 nextbno
= be64_to_cpu(block
->bb_u
.l
.bb_rightsib
);
456 * Check all the extents to make sure they are OK.
457 * If we had a previous block, the last entry should
458 * conform with the first entry in this one.
461 ep
= XFS_BMBT_REC_ADDR(mp
, block
, 1);
463 ASSERT(xfs_bmbt_disk_get_startoff(&last
) +
464 xfs_bmbt_disk_get_blockcount(&last
) <=
465 xfs_bmbt_disk_get_startoff(ep
));
467 for (j
= 1; j
< num_recs
; j
++) {
468 nextp
= XFS_BMBT_REC_ADDR(mp
, block
, j
+ 1);
469 ASSERT(xfs_bmbt_disk_get_startoff(ep
) +
470 xfs_bmbt_disk_get_blockcount(ep
) <=
471 xfs_bmbt_disk_get_startoff(nextp
));
479 xfs_trans_brelse(NULL
, bp
);
483 * If we've reached the end, stop.
485 if (bno
== NULLFSBLOCK
)
489 bp
= xfs_bmap_get_bp(cur
, XFS_FSB_TO_DADDR(mp
, bno
));
492 error
= xfs_btree_read_bufl(mp
, NULL
, bno
, 0, &bp
,
498 block
= XFS_BUF_TO_BLOCK(bp
);
502 xfs_trans_brelse(NULL
, bp
);
507 xfs_warn(mp
, "%s: at error0", __func__
);
509 xfs_trans_brelse(NULL
, bp
);
511 xfs_warn(mp
, "%s: BAD after btree leaves for %d extents",
513 panic("%s: CORRUPTED BTREE OR SOMETHING", __func__
);
518 * Add bmap trace insert entries for all the contents of the extent records.
521 xfs_bmap_trace_exlist(
522 xfs_inode_t
*ip
, /* incore inode pointer */
523 xfs_extnum_t cnt
, /* count of entries in the list */
524 int whichfork
, /* data or attr fork */
525 unsigned long caller_ip
)
527 xfs_extnum_t idx
; /* extent record index */
528 xfs_ifork_t
*ifp
; /* inode fork pointer */
531 if (whichfork
== XFS_ATTR_FORK
)
532 state
|= BMAP_ATTRFORK
;
534 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
535 ASSERT(cnt
== (ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
)));
536 for (idx
= 0; idx
< cnt
; idx
++)
537 trace_xfs_extlist(ip
, idx
, whichfork
, caller_ip
);
541 * Validate that the bmbt_irecs being returned from bmapi are valid
542 * given the caller's original parameters. Specifically check the
543 * ranges of the returned irecs to ensure that they only extend beyond
544 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
547 xfs_bmap_validate_ret(
551 xfs_bmbt_irec_t
*mval
,
555 int i
; /* index to map values */
557 ASSERT(ret_nmap
<= nmap
);
559 for (i
= 0; i
< ret_nmap
; i
++) {
560 ASSERT(mval
[i
].br_blockcount
> 0);
561 if (!(flags
& XFS_BMAPI_ENTIRE
)) {
562 ASSERT(mval
[i
].br_startoff
>= bno
);
563 ASSERT(mval
[i
].br_blockcount
<= len
);
564 ASSERT(mval
[i
].br_startoff
+ mval
[i
].br_blockcount
<=
567 ASSERT(mval
[i
].br_startoff
< bno
+ len
);
568 ASSERT(mval
[i
].br_startoff
+ mval
[i
].br_blockcount
>
572 mval
[i
- 1].br_startoff
+ mval
[i
- 1].br_blockcount
==
573 mval
[i
].br_startoff
);
574 ASSERT(mval
[i
].br_startblock
!= DELAYSTARTBLOCK
&&
575 mval
[i
].br_startblock
!= HOLESTARTBLOCK
);
576 ASSERT(mval
[i
].br_state
== XFS_EXT_NORM
||
577 mval
[i
].br_state
== XFS_EXT_UNWRITTEN
);
582 #define xfs_bmap_check_leaf_extents(cur, ip, whichfork) do { } while (0)
583 #define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap)
587 * bmap free list manipulation functions
591 * Add the extent to the list of extents to be free at transaction end.
592 * The list is maintained sorted (by block number).
596 xfs_fsblock_t bno
, /* fs block number of extent */
597 xfs_filblks_t len
, /* length of extent */
598 xfs_bmap_free_t
*flist
, /* list of extents */
599 xfs_mount_t
*mp
) /* mount point structure */
601 xfs_bmap_free_item_t
*cur
; /* current (next) element */
602 xfs_bmap_free_item_t
*new; /* new element */
603 xfs_bmap_free_item_t
*prev
; /* previous element */
608 ASSERT(bno
!= NULLFSBLOCK
);
610 ASSERT(len
<= MAXEXTLEN
);
611 ASSERT(!isnullstartblock(bno
));
612 agno
= XFS_FSB_TO_AGNO(mp
, bno
);
613 agbno
= XFS_FSB_TO_AGBNO(mp
, bno
);
614 ASSERT(agno
< mp
->m_sb
.sb_agcount
);
615 ASSERT(agbno
< mp
->m_sb
.sb_agblocks
);
616 ASSERT(len
< mp
->m_sb
.sb_agblocks
);
617 ASSERT(agbno
+ len
<= mp
->m_sb
.sb_agblocks
);
619 ASSERT(xfs_bmap_free_item_zone
!= NULL
);
620 new = kmem_zone_alloc(xfs_bmap_free_item_zone
, KM_SLEEP
);
621 new->xbfi_startblock
= bno
;
622 new->xbfi_blockcount
= (xfs_extlen_t
)len
;
623 for (prev
= NULL
, cur
= flist
->xbf_first
;
625 prev
= cur
, cur
= cur
->xbfi_next
) {
626 if (cur
->xbfi_startblock
>= bno
)
630 prev
->xbfi_next
= new;
632 flist
->xbf_first
= new;
633 new->xbfi_next
= cur
;
638 * Remove the entry "free" from the free item list. Prev points to the
639 * previous entry, unless "free" is the head of the list.
643 xfs_bmap_free_t
*flist
, /* free item list header */
644 xfs_bmap_free_item_t
*prev
, /* previous item on list, if any */
645 xfs_bmap_free_item_t
*free
) /* list item to be freed */
648 prev
->xbfi_next
= free
->xbfi_next
;
650 flist
->xbf_first
= free
->xbfi_next
;
652 kmem_zone_free(xfs_bmap_free_item_zone
, free
);
656 * Free up any items left in the list.
660 xfs_bmap_free_t
*flist
) /* list of bmap_free_items */
662 xfs_bmap_free_item_t
*free
; /* free list item */
663 xfs_bmap_free_item_t
*next
;
665 if (flist
->xbf_count
== 0)
667 ASSERT(flist
->xbf_first
!= NULL
);
668 for (free
= flist
->xbf_first
; free
; free
= next
) {
669 next
= free
->xbfi_next
;
670 xfs_bmap_del_free(flist
, NULL
, free
);
672 ASSERT(flist
->xbf_count
== 0);
676 * Inode fork format manipulation functions
680 * Transform a btree format file with only one leaf node, where the
681 * extents list will fit in the inode, into an extents format file.
682 * Since the file extents are already in-core, all we have to do is
683 * give up the space for the btree root and pitch the leaf block.
685 STATIC
int /* error */
686 xfs_bmap_btree_to_extents(
687 xfs_trans_t
*tp
, /* transaction pointer */
688 xfs_inode_t
*ip
, /* incore inode pointer */
689 xfs_btree_cur_t
*cur
, /* btree cursor */
690 int *logflagsp
, /* inode logging flags */
691 int whichfork
) /* data or attr fork */
694 struct xfs_btree_block
*cblock
;/* child btree block */
695 xfs_fsblock_t cbno
; /* child block number */
696 xfs_buf_t
*cbp
; /* child block's buffer */
697 int error
; /* error return value */
698 xfs_ifork_t
*ifp
; /* inode fork data */
699 xfs_mount_t
*mp
; /* mount point structure */
700 __be64
*pp
; /* ptr to block address */
701 struct xfs_btree_block
*rblock
;/* root btree block */
704 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
705 ASSERT(ifp
->if_flags
& XFS_IFEXTENTS
);
706 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
);
707 rblock
= ifp
->if_broot
;
708 ASSERT(be16_to_cpu(rblock
->bb_level
) == 1);
709 ASSERT(be16_to_cpu(rblock
->bb_numrecs
) == 1);
710 ASSERT(xfs_bmbt_maxrecs(mp
, ifp
->if_broot_bytes
, 0) == 1);
711 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, rblock
, 1, ifp
->if_broot_bytes
);
712 cbno
= be64_to_cpu(*pp
);
715 if ((error
= xfs_btree_check_lptr(cur
, cbno
, 1)))
718 error
= xfs_btree_read_bufl(mp
, tp
, cbno
, 0, &cbp
, XFS_BMAP_BTREE_REF
,
722 cblock
= XFS_BUF_TO_BLOCK(cbp
);
723 if ((error
= xfs_btree_check_block(cur
, cblock
, 0, cbp
)))
725 xfs_bmap_add_free(cbno
, 1, cur
->bc_private
.b
.flist
, mp
);
726 ip
->i_d
.di_nblocks
--;
727 xfs_trans_mod_dquot_byino(tp
, ip
, XFS_TRANS_DQ_BCOUNT
, -1L);
728 xfs_trans_binval(tp
, cbp
);
729 if (cur
->bc_bufs
[0] == cbp
)
730 cur
->bc_bufs
[0] = NULL
;
731 xfs_iroot_realloc(ip
, -1, whichfork
);
732 ASSERT(ifp
->if_broot
== NULL
);
733 ASSERT((ifp
->if_flags
& XFS_IFBROOT
) == 0);
734 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_EXTENTS
);
735 *logflagsp
= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
740 * Convert an extents-format file into a btree-format file.
741 * The new file will have a root block (in the inode) and a single child block.
743 STATIC
int /* error */
744 xfs_bmap_extents_to_btree(
745 xfs_trans_t
*tp
, /* transaction pointer */
746 xfs_inode_t
*ip
, /* incore inode pointer */
747 xfs_fsblock_t
*firstblock
, /* first-block-allocated */
748 xfs_bmap_free_t
*flist
, /* blocks freed in xaction */
749 xfs_btree_cur_t
**curp
, /* cursor returned to caller */
750 int wasdel
, /* converting a delayed alloc */
751 int *logflagsp
, /* inode logging flags */
752 int whichfork
) /* data or attr fork */
754 struct xfs_btree_block
*ablock
; /* allocated (child) bt block */
755 xfs_buf_t
*abp
; /* buffer for ablock */
756 xfs_alloc_arg_t args
; /* allocation arguments */
757 xfs_bmbt_rec_t
*arp
; /* child record pointer */
758 struct xfs_btree_block
*block
; /* btree root block */
759 xfs_btree_cur_t
*cur
; /* bmap btree cursor */
760 xfs_bmbt_rec_host_t
*ep
; /* extent record pointer */
761 int error
; /* error return value */
762 xfs_extnum_t i
, cnt
; /* extent record index */
763 xfs_ifork_t
*ifp
; /* inode fork pointer */
764 xfs_bmbt_key_t
*kp
; /* root block key pointer */
765 xfs_mount_t
*mp
; /* mount structure */
766 xfs_extnum_t nextents
; /* number of file extents */
767 xfs_bmbt_ptr_t
*pp
; /* root block address pointer */
770 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
771 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
);
774 * Make space in the inode incore.
776 xfs_iroot_realloc(ip
, 1, whichfork
);
777 ifp
->if_flags
|= XFS_IFBROOT
;
782 block
= ifp
->if_broot
;
783 if (xfs_sb_version_hascrc(&mp
->m_sb
))
784 xfs_btree_init_block_int(mp
, block
, XFS_BUF_DADDR_NULL
,
785 XFS_BMAP_CRC_MAGIC
, 1, 1, ip
->i_ino
,
786 XFS_BTREE_LONG_PTRS
| XFS_BTREE_CRC_BLOCKS
);
788 xfs_btree_init_block_int(mp
, block
, XFS_BUF_DADDR_NULL
,
789 XFS_BMAP_MAGIC
, 1, 1, ip
->i_ino
,
790 XFS_BTREE_LONG_PTRS
);
793 * Need a cursor. Can't allocate until bb_level is filled in.
795 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
796 cur
->bc_private
.b
.firstblock
= *firstblock
;
797 cur
->bc_private
.b
.flist
= flist
;
798 cur
->bc_private
.b
.flags
= wasdel
? XFS_BTCUR_BPRV_WASDEL
: 0;
800 * Convert to a btree with two levels, one record in root.
802 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_BTREE
);
803 memset(&args
, 0, sizeof(args
));
806 args
.firstblock
= *firstblock
;
807 if (*firstblock
== NULLFSBLOCK
) {
808 args
.type
= XFS_ALLOCTYPE_START_BNO
;
809 args
.fsbno
= XFS_INO_TO_FSB(mp
, ip
->i_ino
);
810 } else if (flist
->xbf_low
) {
811 args
.type
= XFS_ALLOCTYPE_START_BNO
;
812 args
.fsbno
= *firstblock
;
814 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
815 args
.fsbno
= *firstblock
;
817 args
.minlen
= args
.maxlen
= args
.prod
= 1;
818 args
.wasdel
= wasdel
;
820 if ((error
= xfs_alloc_vextent(&args
))) {
821 xfs_iroot_realloc(ip
, -1, whichfork
);
822 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
826 * Allocation can't fail, the space was reserved.
828 ASSERT(args
.fsbno
!= NULLFSBLOCK
);
829 ASSERT(*firstblock
== NULLFSBLOCK
||
830 args
.agno
== XFS_FSB_TO_AGNO(mp
, *firstblock
) ||
832 args
.agno
> XFS_FSB_TO_AGNO(mp
, *firstblock
)));
833 *firstblock
= cur
->bc_private
.b
.firstblock
= args
.fsbno
;
834 cur
->bc_private
.b
.allocated
++;
835 ip
->i_d
.di_nblocks
++;
836 xfs_trans_mod_dquot_byino(tp
, ip
, XFS_TRANS_DQ_BCOUNT
, 1L);
837 abp
= xfs_btree_get_bufl(mp
, tp
, args
.fsbno
, 0);
839 * Fill in the child block.
841 abp
->b_ops
= &xfs_bmbt_buf_ops
;
842 ablock
= XFS_BUF_TO_BLOCK(abp
);
843 if (xfs_sb_version_hascrc(&mp
->m_sb
))
844 xfs_btree_init_block_int(mp
, ablock
, abp
->b_bn
,
845 XFS_BMAP_CRC_MAGIC
, 0, 0, ip
->i_ino
,
846 XFS_BTREE_LONG_PTRS
| XFS_BTREE_CRC_BLOCKS
);
848 xfs_btree_init_block_int(mp
, ablock
, abp
->b_bn
,
849 XFS_BMAP_MAGIC
, 0, 0, ip
->i_ino
,
850 XFS_BTREE_LONG_PTRS
);
852 arp
= XFS_BMBT_REC_ADDR(mp
, ablock
, 1);
853 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
854 for (cnt
= i
= 0; i
< nextents
; i
++) {
855 ep
= xfs_iext_get_ext(ifp
, i
);
856 if (!isnullstartblock(xfs_bmbt_get_startblock(ep
))) {
857 arp
->l0
= cpu_to_be64(ep
->l0
);
858 arp
->l1
= cpu_to_be64(ep
->l1
);
862 ASSERT(cnt
== XFS_IFORK_NEXTENTS(ip
, whichfork
));
863 xfs_btree_set_numrecs(ablock
, cnt
);
866 * Fill in the root key and pointer.
868 kp
= XFS_BMBT_KEY_ADDR(mp
, block
, 1);
869 arp
= XFS_BMBT_REC_ADDR(mp
, ablock
, 1);
870 kp
->br_startoff
= cpu_to_be64(xfs_bmbt_disk_get_startoff(arp
));
871 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, xfs_bmbt_get_maxrecs(cur
,
872 be16_to_cpu(block
->bb_level
)));
873 *pp
= cpu_to_be64(args
.fsbno
);
876 * Do all this logging at the end so that
877 * the root is at the right level.
879 xfs_btree_log_block(cur
, abp
, XFS_BB_ALL_BITS
);
880 xfs_btree_log_recs(cur
, abp
, 1, be16_to_cpu(ablock
->bb_numrecs
));
881 ASSERT(*curp
== NULL
);
883 *logflagsp
= XFS_ILOG_CORE
| xfs_ilog_fbroot(whichfork
);
888 * Convert a local file to an extents file.
889 * This code is out of bounds for data forks of regular files,
890 * since the file data needs to get logged so things will stay consistent.
891 * (The bmap-level manipulations are ok, though).
894 xfs_bmap_local_to_extents_empty(
895 struct xfs_inode
*ip
,
898 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
900 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
901 ASSERT(ifp
->if_bytes
== 0);
902 ASSERT(XFS_IFORK_NEXTENTS(ip
, whichfork
) == 0);
904 xfs_bmap_forkoff_reset(ip
, whichfork
);
905 ifp
->if_flags
&= ~XFS_IFINLINE
;
906 ifp
->if_flags
|= XFS_IFEXTENTS
;
907 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_EXTENTS
);
911 STATIC
int /* error */
912 xfs_bmap_local_to_extents(
913 xfs_trans_t
*tp
, /* transaction pointer */
914 xfs_inode_t
*ip
, /* incore inode pointer */
915 xfs_fsblock_t
*firstblock
, /* first block allocated in xaction */
916 xfs_extlen_t total
, /* total blocks needed by transaction */
917 int *logflagsp
, /* inode logging flags */
919 void (*init_fn
)(struct xfs_trans
*tp
,
921 struct xfs_inode
*ip
,
922 struct xfs_ifork
*ifp
))
925 int flags
; /* logging flags returned */
926 xfs_ifork_t
*ifp
; /* inode fork pointer */
927 xfs_alloc_arg_t args
; /* allocation arguments */
928 xfs_buf_t
*bp
; /* buffer for extent block */
929 xfs_bmbt_rec_host_t
*ep
; /* extent record pointer */
932 * We don't want to deal with the case of keeping inode data inline yet.
933 * So sending the data fork of a regular inode is invalid.
935 ASSERT(!(S_ISREG(ip
->i_d
.di_mode
) && whichfork
== XFS_DATA_FORK
));
936 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
937 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
939 if (!ifp
->if_bytes
) {
940 xfs_bmap_local_to_extents_empty(ip
, whichfork
);
941 flags
= XFS_ILOG_CORE
;
947 ASSERT((ifp
->if_flags
& (XFS_IFINLINE
|XFS_IFEXTENTS
|XFS_IFEXTIREC
)) ==
949 memset(&args
, 0, sizeof(args
));
951 args
.mp
= ip
->i_mount
;
952 args
.firstblock
= *firstblock
;
954 * Allocate a block. We know we need only one, since the
955 * file currently fits in an inode.
957 if (*firstblock
== NULLFSBLOCK
) {
958 args
.fsbno
= XFS_INO_TO_FSB(args
.mp
, ip
->i_ino
);
959 args
.type
= XFS_ALLOCTYPE_START_BNO
;
961 args
.fsbno
= *firstblock
;
962 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
965 args
.minlen
= args
.maxlen
= args
.prod
= 1;
966 error
= xfs_alloc_vextent(&args
);
970 /* Can't fail, the space was reserved. */
971 ASSERT(args
.fsbno
!= NULLFSBLOCK
);
972 ASSERT(args
.len
== 1);
973 *firstblock
= args
.fsbno
;
974 bp
= xfs_btree_get_bufl(args
.mp
, tp
, args
.fsbno
, 0);
976 /* initialise the block and copy the data */
977 init_fn(tp
, bp
, ip
, ifp
);
979 /* account for the change in fork size and log everything */
980 xfs_trans_log_buf(tp
, bp
, 0, ifp
->if_bytes
- 1);
981 xfs_idata_realloc(ip
, -ifp
->if_bytes
, whichfork
);
982 xfs_bmap_local_to_extents_empty(ip
, whichfork
);
983 flags
|= XFS_ILOG_CORE
;
985 xfs_iext_add(ifp
, 0, 1);
986 ep
= xfs_iext_get_ext(ifp
, 0);
987 xfs_bmbt_set_allf(ep
, 0, args
.fsbno
, 1, XFS_EXT_NORM
);
988 trace_xfs_bmap_post_update(ip
, 0,
989 whichfork
== XFS_ATTR_FORK
? BMAP_ATTRFORK
: 0,
991 XFS_IFORK_NEXT_SET(ip
, whichfork
, 1);
992 ip
->i_d
.di_nblocks
= 1;
993 xfs_trans_mod_dquot_byino(tp
, ip
,
994 XFS_TRANS_DQ_BCOUNT
, 1L);
995 flags
|= xfs_ilog_fext(whichfork
);
1003 * Called from xfs_bmap_add_attrfork to handle btree format files.
1005 STATIC
int /* error */
1006 xfs_bmap_add_attrfork_btree(
1007 xfs_trans_t
*tp
, /* transaction pointer */
1008 xfs_inode_t
*ip
, /* incore inode pointer */
1009 xfs_fsblock_t
*firstblock
, /* first block allocated */
1010 xfs_bmap_free_t
*flist
, /* blocks to free at commit */
1011 int *flags
) /* inode logging flags */
1013 xfs_btree_cur_t
*cur
; /* btree cursor */
1014 int error
; /* error return value */
1015 xfs_mount_t
*mp
; /* file system mount struct */
1016 int stat
; /* newroot status */
1019 if (ip
->i_df
.if_broot_bytes
<= XFS_IFORK_DSIZE(ip
))
1020 *flags
|= XFS_ILOG_DBROOT
;
1022 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, XFS_DATA_FORK
);
1023 cur
->bc_private
.b
.flist
= flist
;
1024 cur
->bc_private
.b
.firstblock
= *firstblock
;
1025 if ((error
= xfs_bmbt_lookup_ge(cur
, 0, 0, 0, &stat
)))
1027 /* must be at least one entry */
1028 XFS_WANT_CORRUPTED_GOTO(stat
== 1, error0
);
1029 if ((error
= xfs_btree_new_iroot(cur
, flags
, &stat
)))
1032 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
1035 *firstblock
= cur
->bc_private
.b
.firstblock
;
1036 cur
->bc_private
.b
.allocated
= 0;
1037 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
1041 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
1046 * Called from xfs_bmap_add_attrfork to handle extents format files.
1048 STATIC
int /* error */
1049 xfs_bmap_add_attrfork_extents(
1050 xfs_trans_t
*tp
, /* transaction pointer */
1051 xfs_inode_t
*ip
, /* incore inode pointer */
1052 xfs_fsblock_t
*firstblock
, /* first block allocated */
1053 xfs_bmap_free_t
*flist
, /* blocks to free at commit */
1054 int *flags
) /* inode logging flags */
1056 xfs_btree_cur_t
*cur
; /* bmap btree cursor */
1057 int error
; /* error return value */
1059 if (ip
->i_d
.di_nextents
* sizeof(xfs_bmbt_rec_t
) <= XFS_IFORK_DSIZE(ip
))
1062 error
= xfs_bmap_extents_to_btree(tp
, ip
, firstblock
, flist
, &cur
, 0,
1063 flags
, XFS_DATA_FORK
);
1065 cur
->bc_private
.b
.allocated
= 0;
1066 xfs_btree_del_cursor(cur
,
1067 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
1073 * Called from xfs_bmap_add_attrfork to handle local format files. Each
1074 * different data fork content type needs a different callout to do the
1075 * conversion. Some are basic and only require special block initialisation
1076 * callouts for the data formating, others (directories) are so specialised they
1077 * handle everything themselves.
1079 * XXX (dgc): investigate whether directory conversion can use the generic
1080 * formatting callout. It should be possible - it's just a very complex
1083 STATIC
int /* error */
1084 xfs_bmap_add_attrfork_local(
1085 xfs_trans_t
*tp
, /* transaction pointer */
1086 xfs_inode_t
*ip
, /* incore inode pointer */
1087 xfs_fsblock_t
*firstblock
, /* first block allocated */
1088 xfs_bmap_free_t
*flist
, /* blocks to free at commit */
1089 int *flags
) /* inode logging flags */
1091 xfs_da_args_t dargs
; /* args for dir/attr code */
1093 if (ip
->i_df
.if_bytes
<= XFS_IFORK_DSIZE(ip
))
1096 if (S_ISDIR(ip
->i_d
.di_mode
)) {
1097 memset(&dargs
, 0, sizeof(dargs
));
1098 dargs
.geo
= ip
->i_mount
->m_dir_geo
;
1100 dargs
.firstblock
= firstblock
;
1101 dargs
.flist
= flist
;
1102 dargs
.total
= dargs
.geo
->fsbcount
;
1103 dargs
.whichfork
= XFS_DATA_FORK
;
1105 return xfs_dir2_sf_to_block(&dargs
);
1108 if (S_ISLNK(ip
->i_d
.di_mode
))
1109 return xfs_bmap_local_to_extents(tp
, ip
, firstblock
, 1,
1110 flags
, XFS_DATA_FORK
,
1111 xfs_symlink_local_to_remote
);
1113 /* should only be called for types that support local format data */
1115 return -EFSCORRUPTED
;
1119 * Convert inode from non-attributed to attributed.
1120 * Must not be in a transaction, ip must not be locked.
1122 int /* error code */
1123 xfs_bmap_add_attrfork(
1124 xfs_inode_t
*ip
, /* incore inode pointer */
1125 int size
, /* space new attribute needs */
1126 int rsvd
) /* xact may use reserved blks */
1128 xfs_fsblock_t firstblock
; /* 1st block/ag allocated */
1129 xfs_bmap_free_t flist
; /* freed extent records */
1130 xfs_mount_t
*mp
; /* mount structure */
1131 xfs_trans_t
*tp
; /* transaction pointer */
1132 int blks
; /* space reservation */
1133 int version
= 1; /* superblock attr version */
1134 int committed
; /* xaction was committed */
1135 int logflags
; /* logging flags */
1136 int error
; /* error return value */
1137 int cancel_flags
= 0;
1139 ASSERT(XFS_IFORK_Q(ip
) == 0);
1142 ASSERT(!XFS_NOT_DQATTACHED(mp
, ip
));
1143 tp
= xfs_trans_alloc(mp
, XFS_TRANS_ADDAFORK
);
1144 blks
= XFS_ADDAFORK_SPACE_RES(mp
);
1146 tp
->t_flags
|= XFS_TRANS_RESERVE
;
1147 error
= xfs_trans_reserve(tp
, &M_RES(mp
)->tr_addafork
, blks
, 0);
1149 xfs_trans_cancel(tp
, 0);
1152 cancel_flags
= XFS_TRANS_RELEASE_LOG_RES
;
1153 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
1154 error
= xfs_trans_reserve_quota_nblks(tp
, ip
, blks
, 0, rsvd
?
1155 XFS_QMOPT_RES_REGBLKS
| XFS_QMOPT_FORCE_RES
:
1156 XFS_QMOPT_RES_REGBLKS
);
1159 cancel_flags
|= XFS_TRANS_ABORT
;
1160 if (XFS_IFORK_Q(ip
))
1162 if (ip
->i_d
.di_aformat
!= XFS_DINODE_FMT_EXTENTS
) {
1164 * For inodes coming from pre-6.2 filesystems.
1166 ASSERT(ip
->i_d
.di_aformat
== 0);
1167 ip
->i_d
.di_aformat
= XFS_DINODE_FMT_EXTENTS
;
1169 ASSERT(ip
->i_d
.di_anextents
== 0);
1171 xfs_trans_ijoin(tp
, ip
, 0);
1172 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
1174 switch (ip
->i_d
.di_format
) {
1175 case XFS_DINODE_FMT_DEV
:
1176 ip
->i_d
.di_forkoff
= roundup(sizeof(xfs_dev_t
), 8) >> 3;
1178 case XFS_DINODE_FMT_UUID
:
1179 ip
->i_d
.di_forkoff
= roundup(sizeof(uuid_t
), 8) >> 3;
1181 case XFS_DINODE_FMT_LOCAL
:
1182 case XFS_DINODE_FMT_EXTENTS
:
1183 case XFS_DINODE_FMT_BTREE
:
1184 ip
->i_d
.di_forkoff
= xfs_attr_shortform_bytesfit(ip
, size
);
1185 if (!ip
->i_d
.di_forkoff
)
1186 ip
->i_d
.di_forkoff
= xfs_default_attroffset(ip
) >> 3;
1187 else if (mp
->m_flags
& XFS_MOUNT_ATTR2
)
1196 ASSERT(ip
->i_afp
== NULL
);
1197 ip
->i_afp
= kmem_zone_zalloc(xfs_ifork_zone
, KM_SLEEP
);
1198 ip
->i_afp
->if_flags
= XFS_IFEXTENTS
;
1200 xfs_bmap_init(&flist
, &firstblock
);
1201 switch (ip
->i_d
.di_format
) {
1202 case XFS_DINODE_FMT_LOCAL
:
1203 error
= xfs_bmap_add_attrfork_local(tp
, ip
, &firstblock
, &flist
,
1206 case XFS_DINODE_FMT_EXTENTS
:
1207 error
= xfs_bmap_add_attrfork_extents(tp
, ip
, &firstblock
,
1210 case XFS_DINODE_FMT_BTREE
:
1211 error
= xfs_bmap_add_attrfork_btree(tp
, ip
, &firstblock
, &flist
,
1219 xfs_trans_log_inode(tp
, ip
, logflags
);
1222 if (!xfs_sb_version_hasattr(&mp
->m_sb
) ||
1223 (!xfs_sb_version_hasattr2(&mp
->m_sb
) && version
== 2)) {
1224 bool mod_sb
= false;
1226 spin_lock(&mp
->m_sb_lock
);
1227 if (!xfs_sb_version_hasattr(&mp
->m_sb
)) {
1228 xfs_sb_version_addattr(&mp
->m_sb
);
1231 if (!xfs_sb_version_hasattr2(&mp
->m_sb
) && version
== 2) {
1232 xfs_sb_version_addattr2(&mp
->m_sb
);
1235 spin_unlock(&mp
->m_sb_lock
);
1240 error
= xfs_bmap_finish(&tp
, &flist
, &committed
);
1243 error
= xfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
);
1244 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
1248 xfs_bmap_cancel(&flist
);
1250 xfs_trans_cancel(tp
, cancel_flags
);
1251 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
1256 * Internal and external extent tree search functions.
1260 * Read in the extents to if_extents.
1261 * All inode fields are set up by caller, we just traverse the btree
1262 * and copy the records in. If the file system cannot contain unwritten
1263 * extents, the records are checked for no "state" flags.
1266 xfs_bmap_read_extents(
1267 xfs_trans_t
*tp
, /* transaction pointer */
1268 xfs_inode_t
*ip
, /* incore inode */
1269 int whichfork
) /* data or attr fork */
1271 struct xfs_btree_block
*block
; /* current btree block */
1272 xfs_fsblock_t bno
; /* block # of "block" */
1273 xfs_buf_t
*bp
; /* buffer for "block" */
1274 int error
; /* error return value */
1275 xfs_exntfmt_t exntf
; /* XFS_EXTFMT_NOSTATE, if checking */
1276 xfs_extnum_t i
, j
; /* index into the extents list */
1277 xfs_ifork_t
*ifp
; /* fork structure */
1278 int level
; /* btree level, for checking */
1279 xfs_mount_t
*mp
; /* file system mount structure */
1280 __be64
*pp
; /* pointer to block address */
1282 xfs_extnum_t room
; /* number of entries there's room for */
1286 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1287 exntf
= (whichfork
!= XFS_DATA_FORK
) ? XFS_EXTFMT_NOSTATE
:
1288 XFS_EXTFMT_INODE(ip
);
1289 block
= ifp
->if_broot
;
1291 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
1293 level
= be16_to_cpu(block
->bb_level
);
1295 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, 1, ifp
->if_broot_bytes
);
1296 bno
= be64_to_cpu(*pp
);
1297 ASSERT(bno
!= NULLFSBLOCK
);
1298 ASSERT(XFS_FSB_TO_AGNO(mp
, bno
) < mp
->m_sb
.sb_agcount
);
1299 ASSERT(XFS_FSB_TO_AGBNO(mp
, bno
) < mp
->m_sb
.sb_agblocks
);
1301 * Go down the tree until leaf level is reached, following the first
1302 * pointer (leftmost) at each level.
1304 while (level
-- > 0) {
1305 error
= xfs_btree_read_bufl(mp
, tp
, bno
, 0, &bp
,
1306 XFS_BMAP_BTREE_REF
, &xfs_bmbt_buf_ops
);
1309 block
= XFS_BUF_TO_BLOCK(bp
);
1310 XFS_WANT_CORRUPTED_GOTO(
1311 xfs_bmap_sanity_check(mp
, bp
, level
),
1315 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, mp
->m_bmap_dmxr
[1]);
1316 bno
= be64_to_cpu(*pp
);
1317 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp
, bno
), error0
);
1318 xfs_trans_brelse(tp
, bp
);
1321 * Here with bp and block set to the leftmost leaf node in the tree.
1323 room
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
1326 * Loop over all leaf nodes. Copy information to the extent records.
1329 xfs_bmbt_rec_t
*frp
;
1330 xfs_fsblock_t nextbno
;
1331 xfs_extnum_t num_recs
;
1334 num_recs
= xfs_btree_get_numrecs(block
);
1335 if (unlikely(i
+ num_recs
> room
)) {
1336 ASSERT(i
+ num_recs
<= room
);
1337 xfs_warn(ip
->i_mount
,
1338 "corrupt dinode %Lu, (btree extents).",
1339 (unsigned long long) ip
->i_ino
);
1340 XFS_CORRUPTION_ERROR("xfs_bmap_read_extents(1)",
1341 XFS_ERRLEVEL_LOW
, ip
->i_mount
, block
);
1344 XFS_WANT_CORRUPTED_GOTO(
1345 xfs_bmap_sanity_check(mp
, bp
, 0),
1348 * Read-ahead the next leaf block, if any.
1350 nextbno
= be64_to_cpu(block
->bb_u
.l
.bb_rightsib
);
1351 if (nextbno
!= NULLFSBLOCK
)
1352 xfs_btree_reada_bufl(mp
, nextbno
, 1,
1355 * Copy records into the extent records.
1357 frp
= XFS_BMBT_REC_ADDR(mp
, block
, 1);
1359 for (j
= 0; j
< num_recs
; j
++, i
++, frp
++) {
1360 xfs_bmbt_rec_host_t
*trp
= xfs_iext_get_ext(ifp
, i
);
1361 trp
->l0
= be64_to_cpu(frp
->l0
);
1362 trp
->l1
= be64_to_cpu(frp
->l1
);
1364 if (exntf
== XFS_EXTFMT_NOSTATE
) {
1366 * Check all attribute bmap btree records and
1367 * any "older" data bmap btree records for a
1368 * set bit in the "extent flag" position.
1370 if (unlikely(xfs_check_nostate_extents(ifp
,
1371 start
, num_recs
))) {
1372 XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
1378 xfs_trans_brelse(tp
, bp
);
1381 * If we've reached the end, stop.
1383 if (bno
== NULLFSBLOCK
)
1385 error
= xfs_btree_read_bufl(mp
, tp
, bno
, 0, &bp
,
1386 XFS_BMAP_BTREE_REF
, &xfs_bmbt_buf_ops
);
1389 block
= XFS_BUF_TO_BLOCK(bp
);
1391 ASSERT(i
== (ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
)));
1392 ASSERT(i
== XFS_IFORK_NEXTENTS(ip
, whichfork
));
1393 XFS_BMAP_TRACE_EXLIST(ip
, i
, whichfork
);
1396 xfs_trans_brelse(tp
, bp
);
1397 return -EFSCORRUPTED
;
1402 * Search the extent records for the entry containing block bno.
1403 * If bno lies in a hole, point to the next entry. If bno lies
1404 * past eof, *eofp will be set, and *prevp will contain the last
1405 * entry (null if none). Else, *lastxp will be set to the index
1406 * of the found entry; *gotp will contain the entry.
1408 STATIC xfs_bmbt_rec_host_t
* /* pointer to found extent entry */
1409 xfs_bmap_search_multi_extents(
1410 xfs_ifork_t
*ifp
, /* inode fork pointer */
1411 xfs_fileoff_t bno
, /* block number searched for */
1412 int *eofp
, /* out: end of file found */
1413 xfs_extnum_t
*lastxp
, /* out: last extent index */
1414 xfs_bmbt_irec_t
*gotp
, /* out: extent entry found */
1415 xfs_bmbt_irec_t
*prevp
) /* out: previous extent entry found */
1417 xfs_bmbt_rec_host_t
*ep
; /* extent record pointer */
1418 xfs_extnum_t lastx
; /* last extent index */
1421 * Initialize the extent entry structure to catch access to
1422 * uninitialized br_startblock field.
1424 gotp
->br_startoff
= 0xffa5a5a5a5a5a5a5LL
;
1425 gotp
->br_blockcount
= 0xa55a5a5a5a5a5a5aLL
;
1426 gotp
->br_state
= XFS_EXT_INVALID
;
1427 gotp
->br_startblock
= 0xffffa5a5a5a5a5a5LL
;
1428 prevp
->br_startoff
= NULLFILEOFF
;
1430 ep
= xfs_iext_bno_to_ext(ifp
, bno
, &lastx
);
1432 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, lastx
- 1), prevp
);
1434 if (lastx
< (ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
))) {
1435 xfs_bmbt_get_all(ep
, gotp
);
1449 * Search the extents list for the inode, for the extent containing bno.
1450 * If bno lies in a hole, point to the next entry. If bno lies past eof,
1451 * *eofp will be set, and *prevp will contain the last entry (null if none).
1452 * Else, *lastxp will be set to the index of the found
1453 * entry; *gotp will contain the entry.
1455 STATIC xfs_bmbt_rec_host_t
* /* pointer to found extent entry */
1456 xfs_bmap_search_extents(
1457 xfs_inode_t
*ip
, /* incore inode pointer */
1458 xfs_fileoff_t bno
, /* block number searched for */
1459 int fork
, /* data or attr fork */
1460 int *eofp
, /* out: end of file found */
1461 xfs_extnum_t
*lastxp
, /* out: last extent index */
1462 xfs_bmbt_irec_t
*gotp
, /* out: extent entry found */
1463 xfs_bmbt_irec_t
*prevp
) /* out: previous extent entry found */
1465 xfs_ifork_t
*ifp
; /* inode fork pointer */
1466 xfs_bmbt_rec_host_t
*ep
; /* extent record pointer */
1468 XFS_STATS_INC(xs_look_exlist
);
1469 ifp
= XFS_IFORK_PTR(ip
, fork
);
1471 ep
= xfs_bmap_search_multi_extents(ifp
, bno
, eofp
, lastxp
, gotp
, prevp
);
1473 if (unlikely(!(gotp
->br_startblock
) && (*lastxp
!= NULLEXTNUM
) &&
1474 !(XFS_IS_REALTIME_INODE(ip
) && fork
== XFS_DATA_FORK
))) {
1475 xfs_alert_tag(ip
->i_mount
, XFS_PTAG_FSBLOCK_ZERO
,
1476 "Access to block zero in inode %llu "
1477 "start_block: %llx start_off: %llx "
1478 "blkcnt: %llx extent-state: %x lastx: %x",
1479 (unsigned long long)ip
->i_ino
,
1480 (unsigned long long)gotp
->br_startblock
,
1481 (unsigned long long)gotp
->br_startoff
,
1482 (unsigned long long)gotp
->br_blockcount
,
1483 gotp
->br_state
, *lastxp
);
1484 *lastxp
= NULLEXTNUM
;
1492 * Returns the file-relative block number of the first unused block(s)
1493 * in the file with at least "len" logically contiguous blocks free.
1494 * This is the lowest-address hole if the file has holes, else the first block
1495 * past the end of file.
1496 * Return 0 if the file is currently local (in-inode).
1499 xfs_bmap_first_unused(
1500 xfs_trans_t
*tp
, /* transaction pointer */
1501 xfs_inode_t
*ip
, /* incore inode */
1502 xfs_extlen_t len
, /* size of hole to find */
1503 xfs_fileoff_t
*first_unused
, /* unused block */
1504 int whichfork
) /* data or attr fork */
1506 int error
; /* error return value */
1507 int idx
; /* extent record index */
1508 xfs_ifork_t
*ifp
; /* inode fork pointer */
1509 xfs_fileoff_t lastaddr
; /* last block number seen */
1510 xfs_fileoff_t lowest
; /* lowest useful block */
1511 xfs_fileoff_t max
; /* starting useful block */
1512 xfs_fileoff_t off
; /* offset for this block */
1513 xfs_extnum_t nextents
; /* number of extent entries */
1515 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
||
1516 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
||
1517 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
1518 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
) {
1522 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1523 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
1524 (error
= xfs_iread_extents(tp
, ip
, whichfork
)))
1526 lowest
= *first_unused
;
1527 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
1528 for (idx
= 0, lastaddr
= 0, max
= lowest
; idx
< nextents
; idx
++) {
1529 xfs_bmbt_rec_host_t
*ep
= xfs_iext_get_ext(ifp
, idx
);
1530 off
= xfs_bmbt_get_startoff(ep
);
1532 * See if the hole before this extent will work.
1534 if (off
>= lowest
+ len
&& off
- max
>= len
) {
1535 *first_unused
= max
;
1538 lastaddr
= off
+ xfs_bmbt_get_blockcount(ep
);
1539 max
= XFS_FILEOFF_MAX(lastaddr
, lowest
);
1541 *first_unused
= max
;
1546 * Returns the file-relative block number of the last block - 1 before
1547 * last_block (input value) in the file.
1548 * This is not based on i_size, it is based on the extent records.
1549 * Returns 0 for local files, as they do not have extent records.
1552 xfs_bmap_last_before(
1553 xfs_trans_t
*tp
, /* transaction pointer */
1554 xfs_inode_t
*ip
, /* incore inode */
1555 xfs_fileoff_t
*last_block
, /* last block */
1556 int whichfork
) /* data or attr fork */
1558 xfs_fileoff_t bno
; /* input file offset */
1559 int eof
; /* hit end of file */
1560 xfs_bmbt_rec_host_t
*ep
; /* pointer to last extent */
1561 int error
; /* error return value */
1562 xfs_bmbt_irec_t got
; /* current extent value */
1563 xfs_ifork_t
*ifp
; /* inode fork pointer */
1564 xfs_extnum_t lastx
; /* last extent used */
1565 xfs_bmbt_irec_t prev
; /* previous extent value */
1567 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
&&
1568 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
1569 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_LOCAL
)
1571 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
) {
1575 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1576 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
1577 (error
= xfs_iread_extents(tp
, ip
, whichfork
)))
1579 bno
= *last_block
- 1;
1580 ep
= xfs_bmap_search_extents(ip
, bno
, whichfork
, &eof
, &lastx
, &got
,
1582 if (eof
|| xfs_bmbt_get_startoff(ep
) > bno
) {
1583 if (prev
.br_startoff
== NULLFILEOFF
)
1586 *last_block
= prev
.br_startoff
+ prev
.br_blockcount
;
1589 * Otherwise *last_block is already the right answer.
1595 xfs_bmap_last_extent(
1596 struct xfs_trans
*tp
,
1597 struct xfs_inode
*ip
,
1599 struct xfs_bmbt_irec
*rec
,
1602 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1606 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
1607 error
= xfs_iread_extents(tp
, ip
, whichfork
);
1612 nextents
= ifp
->if_bytes
/ sizeof(xfs_bmbt_rec_t
);
1613 if (nextents
== 0) {
1618 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, nextents
- 1), rec
);
1624 * Check the last inode extent to determine whether this allocation will result
1625 * in blocks being allocated at the end of the file. When we allocate new data
1626 * blocks at the end of the file which do not start at the previous data block,
1627 * we will try to align the new blocks at stripe unit boundaries.
1629 * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be
1630 * at, or past the EOF.
1634 struct xfs_bmalloca
*bma
,
1637 struct xfs_bmbt_irec rec
;
1642 error
= xfs_bmap_last_extent(NULL
, bma
->ip
, whichfork
, &rec
,
1653 * Check if we are allocation or past the last extent, or at least into
1654 * the last delayed allocated extent.
1656 bma
->aeof
= bma
->offset
>= rec
.br_startoff
+ rec
.br_blockcount
||
1657 (bma
->offset
>= rec
.br_startoff
&&
1658 isnullstartblock(rec
.br_startblock
));
1663 * Returns the file-relative block number of the first block past eof in
1664 * the file. This is not based on i_size, it is based on the extent records.
1665 * Returns 0 for local files, as they do not have extent records.
1668 xfs_bmap_last_offset(
1669 struct xfs_inode
*ip
,
1670 xfs_fileoff_t
*last_block
,
1673 struct xfs_bmbt_irec rec
;
1679 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
)
1682 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
&&
1683 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
1686 error
= xfs_bmap_last_extent(NULL
, ip
, whichfork
, &rec
, &is_empty
);
1687 if (error
|| is_empty
)
1690 *last_block
= rec
.br_startoff
+ rec
.br_blockcount
;
1695 * Returns whether the selected fork of the inode has exactly one
1696 * block or not. For the data fork we check this matches di_size,
1697 * implying the file's range is 0..bsize-1.
1699 int /* 1=>1 block, 0=>otherwise */
1701 xfs_inode_t
*ip
, /* incore inode */
1702 int whichfork
) /* data or attr fork */
1704 xfs_bmbt_rec_host_t
*ep
; /* ptr to fork's extent */
1705 xfs_ifork_t
*ifp
; /* inode fork pointer */
1706 int rval
; /* return value */
1707 xfs_bmbt_irec_t s
; /* internal version of extent */
1710 if (whichfork
== XFS_DATA_FORK
)
1711 return XFS_ISIZE(ip
) == ip
->i_mount
->m_sb
.sb_blocksize
;
1713 if (XFS_IFORK_NEXTENTS(ip
, whichfork
) != 1)
1715 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
1717 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1718 ASSERT(ifp
->if_flags
& XFS_IFEXTENTS
);
1719 ep
= xfs_iext_get_ext(ifp
, 0);
1720 xfs_bmbt_get_all(ep
, &s
);
1721 rval
= s
.br_startoff
== 0 && s
.br_blockcount
== 1;
1722 if (rval
&& whichfork
== XFS_DATA_FORK
)
1723 ASSERT(XFS_ISIZE(ip
) == ip
->i_mount
->m_sb
.sb_blocksize
);
1728 * Extent tree manipulation functions used during allocation.
1732 * Convert a delayed allocation to a real allocation.
1734 STATIC
int /* error */
1735 xfs_bmap_add_extent_delay_real(
1736 struct xfs_bmalloca
*bma
)
1738 struct xfs_bmbt_irec
*new = &bma
->got
;
1739 int diff
; /* temp value */
1740 xfs_bmbt_rec_host_t
*ep
; /* extent entry for idx */
1741 int error
; /* error return value */
1742 int i
; /* temp state */
1743 xfs_ifork_t
*ifp
; /* inode fork pointer */
1744 xfs_fileoff_t new_endoff
; /* end offset of new entry */
1745 xfs_bmbt_irec_t r
[3]; /* neighbor extent entries */
1746 /* left is 0, right is 1, prev is 2 */
1747 int rval
=0; /* return value (logging flags) */
1748 int state
= 0;/* state bits, accessed thru macros */
1749 xfs_filblks_t da_new
; /* new count del alloc blocks used */
1750 xfs_filblks_t da_old
; /* old count del alloc blocks used */
1751 xfs_filblks_t temp
=0; /* value for da_new calculations */
1752 xfs_filblks_t temp2
=0;/* value for da_new calculations */
1753 int tmp_rval
; /* partial logging flags */
1755 ifp
= XFS_IFORK_PTR(bma
->ip
, XFS_DATA_FORK
);
1757 ASSERT(bma
->idx
>= 0);
1758 ASSERT(bma
->idx
<= ifp
->if_bytes
/ sizeof(struct xfs_bmbt_rec
));
1759 ASSERT(!isnullstartblock(new->br_startblock
));
1761 (bma
->cur
->bc_private
.b
.flags
& XFS_BTCUR_BPRV_WASDEL
));
1763 XFS_STATS_INC(xs_add_exlist
);
1770 * Set up a bunch of variables to make the tests simpler.
1772 ep
= xfs_iext_get_ext(ifp
, bma
->idx
);
1773 xfs_bmbt_get_all(ep
, &PREV
);
1774 new_endoff
= new->br_startoff
+ new->br_blockcount
;
1775 ASSERT(PREV
.br_startoff
<= new->br_startoff
);
1776 ASSERT(PREV
.br_startoff
+ PREV
.br_blockcount
>= new_endoff
);
1778 da_old
= startblockval(PREV
.br_startblock
);
1782 * Set flags determining what part of the previous delayed allocation
1783 * extent is being replaced by a real allocation.
1785 if (PREV
.br_startoff
== new->br_startoff
)
1786 state
|= BMAP_LEFT_FILLING
;
1787 if (PREV
.br_startoff
+ PREV
.br_blockcount
== new_endoff
)
1788 state
|= BMAP_RIGHT_FILLING
;
1791 * Check and set flags if this segment has a left neighbor.
1792 * Don't set contiguous if the combined extent would be too large.
1795 state
|= BMAP_LEFT_VALID
;
1796 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
- 1), &LEFT
);
1798 if (isnullstartblock(LEFT
.br_startblock
))
1799 state
|= BMAP_LEFT_DELAY
;
1802 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
1803 LEFT
.br_startoff
+ LEFT
.br_blockcount
== new->br_startoff
&&
1804 LEFT
.br_startblock
+ LEFT
.br_blockcount
== new->br_startblock
&&
1805 LEFT
.br_state
== new->br_state
&&
1806 LEFT
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
1807 state
|= BMAP_LEFT_CONTIG
;
1810 * Check and set flags if this segment has a right neighbor.
1811 * Don't set contiguous if the combined extent would be too large.
1812 * Also check for all-three-contiguous being too large.
1814 if (bma
->idx
< bma
->ip
->i_df
.if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
) - 1) {
1815 state
|= BMAP_RIGHT_VALID
;
1816 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
+ 1), &RIGHT
);
1818 if (isnullstartblock(RIGHT
.br_startblock
))
1819 state
|= BMAP_RIGHT_DELAY
;
1822 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
1823 new_endoff
== RIGHT
.br_startoff
&&
1824 new->br_startblock
+ new->br_blockcount
== RIGHT
.br_startblock
&&
1825 new->br_state
== RIGHT
.br_state
&&
1826 new->br_blockcount
+ RIGHT
.br_blockcount
<= MAXEXTLEN
&&
1827 ((state
& (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
1828 BMAP_RIGHT_FILLING
)) !=
1829 (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
1830 BMAP_RIGHT_FILLING
) ||
1831 LEFT
.br_blockcount
+ new->br_blockcount
+ RIGHT
.br_blockcount
1833 state
|= BMAP_RIGHT_CONTIG
;
1837 * Switch out based on the FILLING and CONTIG state bits.
1839 switch (state
& (BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
1840 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
)) {
1841 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
1842 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
1844 * Filling in all of a previously delayed allocation extent.
1845 * The left and right neighbors are both contiguous with new.
1848 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1849 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, bma
->idx
),
1850 LEFT
.br_blockcount
+ PREV
.br_blockcount
+
1851 RIGHT
.br_blockcount
);
1852 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1854 xfs_iext_remove(bma
->ip
, bma
->idx
+ 1, 2, state
);
1855 bma
->ip
->i_d
.di_nextents
--;
1856 if (bma
->cur
== NULL
)
1857 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1859 rval
= XFS_ILOG_CORE
;
1860 error
= xfs_bmbt_lookup_eq(bma
->cur
, RIGHT
.br_startoff
,
1861 RIGHT
.br_startblock
,
1862 RIGHT
.br_blockcount
, &i
);
1865 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
1866 error
= xfs_btree_delete(bma
->cur
, &i
);
1869 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
1870 error
= xfs_btree_decrement(bma
->cur
, 0, &i
);
1873 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
1874 error
= xfs_bmbt_update(bma
->cur
, LEFT
.br_startoff
,
1876 LEFT
.br_blockcount
+
1877 PREV
.br_blockcount
+
1878 RIGHT
.br_blockcount
, LEFT
.br_state
);
1884 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
1886 * Filling in all of a previously delayed allocation extent.
1887 * The left neighbor is contiguous, the right is not.
1891 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1892 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, bma
->idx
),
1893 LEFT
.br_blockcount
+ PREV
.br_blockcount
);
1894 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1896 xfs_iext_remove(bma
->ip
, bma
->idx
+ 1, 1, state
);
1897 if (bma
->cur
== NULL
)
1898 rval
= XFS_ILOG_DEXT
;
1901 error
= xfs_bmbt_lookup_eq(bma
->cur
, LEFT
.br_startoff
,
1902 LEFT
.br_startblock
, LEFT
.br_blockcount
,
1906 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
1907 error
= xfs_bmbt_update(bma
->cur
, LEFT
.br_startoff
,
1909 LEFT
.br_blockcount
+
1910 PREV
.br_blockcount
, LEFT
.br_state
);
1916 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
1918 * Filling in all of a previously delayed allocation extent.
1919 * The right neighbor is contiguous, the left is not.
1921 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1922 xfs_bmbt_set_startblock(ep
, new->br_startblock
);
1923 xfs_bmbt_set_blockcount(ep
,
1924 PREV
.br_blockcount
+ RIGHT
.br_blockcount
);
1925 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1927 xfs_iext_remove(bma
->ip
, bma
->idx
+ 1, 1, state
);
1928 if (bma
->cur
== NULL
)
1929 rval
= XFS_ILOG_DEXT
;
1932 error
= xfs_bmbt_lookup_eq(bma
->cur
, RIGHT
.br_startoff
,
1933 RIGHT
.br_startblock
,
1934 RIGHT
.br_blockcount
, &i
);
1937 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
1938 error
= xfs_bmbt_update(bma
->cur
, PREV
.br_startoff
,
1940 PREV
.br_blockcount
+
1941 RIGHT
.br_blockcount
, PREV
.br_state
);
1947 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
1949 * Filling in all of a previously delayed allocation extent.
1950 * Neither the left nor right neighbors are contiguous with
1953 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1954 xfs_bmbt_set_startblock(ep
, new->br_startblock
);
1955 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1957 bma
->ip
->i_d
.di_nextents
++;
1958 if (bma
->cur
== NULL
)
1959 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1961 rval
= XFS_ILOG_CORE
;
1962 error
= xfs_bmbt_lookup_eq(bma
->cur
, new->br_startoff
,
1963 new->br_startblock
, new->br_blockcount
,
1967 XFS_WANT_CORRUPTED_GOTO(i
== 0, done
);
1968 bma
->cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
1969 error
= xfs_btree_insert(bma
->cur
, &i
);
1972 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
1976 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
:
1978 * Filling in the first part of a previous delayed allocation.
1979 * The left neighbor is contiguous.
1981 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
- 1, state
, _THIS_IP_
);
1982 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, bma
->idx
- 1),
1983 LEFT
.br_blockcount
+ new->br_blockcount
);
1984 xfs_bmbt_set_startoff(ep
,
1985 PREV
.br_startoff
+ new->br_blockcount
);
1986 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
- 1, state
, _THIS_IP_
);
1988 temp
= PREV
.br_blockcount
- new->br_blockcount
;
1989 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1990 xfs_bmbt_set_blockcount(ep
, temp
);
1991 if (bma
->cur
== NULL
)
1992 rval
= XFS_ILOG_DEXT
;
1995 error
= xfs_bmbt_lookup_eq(bma
->cur
, LEFT
.br_startoff
,
1996 LEFT
.br_startblock
, LEFT
.br_blockcount
,
2000 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2001 error
= xfs_bmbt_update(bma
->cur
, LEFT
.br_startoff
,
2003 LEFT
.br_blockcount
+
2009 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
2010 startblockval(PREV
.br_startblock
));
2011 xfs_bmbt_set_startblock(ep
, nullstartblock(da_new
));
2012 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2017 case BMAP_LEFT_FILLING
:
2019 * Filling in the first part of a previous delayed allocation.
2020 * The left neighbor is not contiguous.
2022 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2023 xfs_bmbt_set_startoff(ep
, new_endoff
);
2024 temp
= PREV
.br_blockcount
- new->br_blockcount
;
2025 xfs_bmbt_set_blockcount(ep
, temp
);
2026 xfs_iext_insert(bma
->ip
, bma
->idx
, 1, new, state
);
2027 bma
->ip
->i_d
.di_nextents
++;
2028 if (bma
->cur
== NULL
)
2029 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2031 rval
= XFS_ILOG_CORE
;
2032 error
= xfs_bmbt_lookup_eq(bma
->cur
, new->br_startoff
,
2033 new->br_startblock
, new->br_blockcount
,
2037 XFS_WANT_CORRUPTED_GOTO(i
== 0, done
);
2038 bma
->cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
2039 error
= xfs_btree_insert(bma
->cur
, &i
);
2042 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2045 if (xfs_bmap_needs_btree(bma
->ip
, XFS_DATA_FORK
)) {
2046 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
2047 bma
->firstblock
, bma
->flist
,
2048 &bma
->cur
, 1, &tmp_rval
, XFS_DATA_FORK
);
2053 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
2054 startblockval(PREV
.br_startblock
) -
2055 (bma
->cur
? bma
->cur
->bc_private
.b
.allocated
: 0));
2056 ep
= xfs_iext_get_ext(ifp
, bma
->idx
+ 1);
2057 xfs_bmbt_set_startblock(ep
, nullstartblock(da_new
));
2058 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
+ 1, state
, _THIS_IP_
);
2061 case BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2063 * Filling in the last part of a previous delayed allocation.
2064 * The right neighbor is contiguous with the new allocation.
2066 temp
= PREV
.br_blockcount
- new->br_blockcount
;
2067 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
+ 1, state
, _THIS_IP_
);
2068 xfs_bmbt_set_blockcount(ep
, temp
);
2069 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp
, bma
->idx
+ 1),
2070 new->br_startoff
, new->br_startblock
,
2071 new->br_blockcount
+ RIGHT
.br_blockcount
,
2073 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
+ 1, state
, _THIS_IP_
);
2074 if (bma
->cur
== NULL
)
2075 rval
= XFS_ILOG_DEXT
;
2078 error
= xfs_bmbt_lookup_eq(bma
->cur
, RIGHT
.br_startoff
,
2079 RIGHT
.br_startblock
,
2080 RIGHT
.br_blockcount
, &i
);
2083 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2084 error
= xfs_bmbt_update(bma
->cur
, new->br_startoff
,
2086 new->br_blockcount
+
2087 RIGHT
.br_blockcount
,
2093 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
2094 startblockval(PREV
.br_startblock
));
2095 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2096 xfs_bmbt_set_startblock(ep
, nullstartblock(da_new
));
2097 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2102 case BMAP_RIGHT_FILLING
:
2104 * Filling in the last part of a previous delayed allocation.
2105 * The right neighbor is not contiguous.
2107 temp
= PREV
.br_blockcount
- new->br_blockcount
;
2108 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2109 xfs_bmbt_set_blockcount(ep
, temp
);
2110 xfs_iext_insert(bma
->ip
, bma
->idx
+ 1, 1, new, state
);
2111 bma
->ip
->i_d
.di_nextents
++;
2112 if (bma
->cur
== NULL
)
2113 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2115 rval
= XFS_ILOG_CORE
;
2116 error
= xfs_bmbt_lookup_eq(bma
->cur
, new->br_startoff
,
2117 new->br_startblock
, new->br_blockcount
,
2121 XFS_WANT_CORRUPTED_GOTO(i
== 0, done
);
2122 bma
->cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
2123 error
= xfs_btree_insert(bma
->cur
, &i
);
2126 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2129 if (xfs_bmap_needs_btree(bma
->ip
, XFS_DATA_FORK
)) {
2130 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
2131 bma
->firstblock
, bma
->flist
, &bma
->cur
, 1,
2132 &tmp_rval
, XFS_DATA_FORK
);
2137 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
2138 startblockval(PREV
.br_startblock
) -
2139 (bma
->cur
? bma
->cur
->bc_private
.b
.allocated
: 0));
2140 ep
= xfs_iext_get_ext(ifp
, bma
->idx
);
2141 xfs_bmbt_set_startblock(ep
, nullstartblock(da_new
));
2142 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2149 * Filling in the middle part of a previous delayed allocation.
2150 * Contiguity is impossible here.
2151 * This case is avoided almost all the time.
2153 * We start with a delayed allocation:
2155 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
2158 * and we are allocating:
2159 * +rrrrrrrrrrrrrrrrr+
2162 * and we set it up for insertion as:
2163 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
2165 * PREV @ idx LEFT RIGHT
2166 * inserted at idx + 1
2168 temp
= new->br_startoff
- PREV
.br_startoff
;
2169 temp2
= PREV
.br_startoff
+ PREV
.br_blockcount
- new_endoff
;
2170 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, 0, _THIS_IP_
);
2171 xfs_bmbt_set_blockcount(ep
, temp
); /* truncate PREV */
2173 RIGHT
.br_state
= PREV
.br_state
;
2174 RIGHT
.br_startblock
= nullstartblock(
2175 (int)xfs_bmap_worst_indlen(bma
->ip
, temp2
));
2176 RIGHT
.br_startoff
= new_endoff
;
2177 RIGHT
.br_blockcount
= temp2
;
2178 /* insert LEFT (r[0]) and RIGHT (r[1]) at the same time */
2179 xfs_iext_insert(bma
->ip
, bma
->idx
+ 1, 2, &LEFT
, state
);
2180 bma
->ip
->i_d
.di_nextents
++;
2181 if (bma
->cur
== NULL
)
2182 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2184 rval
= XFS_ILOG_CORE
;
2185 error
= xfs_bmbt_lookup_eq(bma
->cur
, new->br_startoff
,
2186 new->br_startblock
, new->br_blockcount
,
2190 XFS_WANT_CORRUPTED_GOTO(i
== 0, done
);
2191 bma
->cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
2192 error
= xfs_btree_insert(bma
->cur
, &i
);
2195 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2198 if (xfs_bmap_needs_btree(bma
->ip
, XFS_DATA_FORK
)) {
2199 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
2200 bma
->firstblock
, bma
->flist
, &bma
->cur
,
2201 1, &tmp_rval
, XFS_DATA_FORK
);
2206 temp
= xfs_bmap_worst_indlen(bma
->ip
, temp
);
2207 temp2
= xfs_bmap_worst_indlen(bma
->ip
, temp2
);
2208 diff
= (int)(temp
+ temp2
- startblockval(PREV
.br_startblock
) -
2209 (bma
->cur
? bma
->cur
->bc_private
.b
.allocated
: 0));
2211 error
= xfs_icsb_modify_counters(bma
->ip
->i_mount
,
2213 -((int64_t)diff
), 0);
2219 ep
= xfs_iext_get_ext(ifp
, bma
->idx
);
2220 xfs_bmbt_set_startblock(ep
, nullstartblock((int)temp
));
2221 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2222 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
+ 2, state
, _THIS_IP_
);
2223 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp
, bma
->idx
+ 2),
2224 nullstartblock((int)temp2
));
2225 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
+ 2, state
, _THIS_IP_
);
2228 da_new
= temp
+ temp2
;
2231 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2232 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2233 case BMAP_LEFT_FILLING
| BMAP_RIGHT_CONTIG
:
2234 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
2235 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2236 case BMAP_LEFT_CONTIG
:
2237 case BMAP_RIGHT_CONTIG
:
2239 * These cases are all impossible.
2244 /* convert to a btree if necessary */
2245 if (xfs_bmap_needs_btree(bma
->ip
, XFS_DATA_FORK
)) {
2246 int tmp_logflags
; /* partial log flag return val */
2248 ASSERT(bma
->cur
== NULL
);
2249 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
2250 bma
->firstblock
, bma
->flist
, &bma
->cur
,
2251 da_old
> 0, &tmp_logflags
, XFS_DATA_FORK
);
2252 bma
->logflags
|= tmp_logflags
;
2257 /* adjust for changes in reserved delayed indirect blocks */
2258 if (da_old
|| da_new
) {
2261 temp
+= bma
->cur
->bc_private
.b
.allocated
;
2262 ASSERT(temp
<= da_old
);
2264 xfs_icsb_modify_counters(bma
->ip
->i_mount
,
2266 (int64_t)(da_old
- temp
), 0);
2269 /* clear out the allocated field, done with it now in any case. */
2271 bma
->cur
->bc_private
.b
.allocated
= 0;
2273 xfs_bmap_check_leaf_extents(bma
->cur
, bma
->ip
, XFS_DATA_FORK
);
2275 bma
->logflags
|= rval
;
2283 * Convert an unwritten allocation to a real allocation or vice versa.
2285 STATIC
int /* error */
2286 xfs_bmap_add_extent_unwritten_real(
2287 struct xfs_trans
*tp
,
2288 xfs_inode_t
*ip
, /* incore inode pointer */
2289 xfs_extnum_t
*idx
, /* extent number to update/insert */
2290 xfs_btree_cur_t
**curp
, /* if *curp is null, not a btree */
2291 xfs_bmbt_irec_t
*new, /* new data to add to file extents */
2292 xfs_fsblock_t
*first
, /* pointer to firstblock variable */
2293 xfs_bmap_free_t
*flist
, /* list of extents to be freed */
2294 int *logflagsp
) /* inode logging flags */
2296 xfs_btree_cur_t
*cur
; /* btree cursor */
2297 xfs_bmbt_rec_host_t
*ep
; /* extent entry for idx */
2298 int error
; /* error return value */
2299 int i
; /* temp state */
2300 xfs_ifork_t
*ifp
; /* inode fork pointer */
2301 xfs_fileoff_t new_endoff
; /* end offset of new entry */
2302 xfs_exntst_t newext
; /* new extent state */
2303 xfs_exntst_t oldext
; /* old extent state */
2304 xfs_bmbt_irec_t r
[3]; /* neighbor extent entries */
2305 /* left is 0, right is 1, prev is 2 */
2306 int rval
=0; /* return value (logging flags) */
2307 int state
= 0;/* state bits, accessed thru macros */
2312 ifp
= XFS_IFORK_PTR(ip
, XFS_DATA_FORK
);
2315 ASSERT(*idx
<= ifp
->if_bytes
/ sizeof(struct xfs_bmbt_rec
));
2316 ASSERT(!isnullstartblock(new->br_startblock
));
2318 XFS_STATS_INC(xs_add_exlist
);
2325 * Set up a bunch of variables to make the tests simpler.
2328 ep
= xfs_iext_get_ext(ifp
, *idx
);
2329 xfs_bmbt_get_all(ep
, &PREV
);
2330 newext
= new->br_state
;
2331 oldext
= (newext
== XFS_EXT_UNWRITTEN
) ?
2332 XFS_EXT_NORM
: XFS_EXT_UNWRITTEN
;
2333 ASSERT(PREV
.br_state
== oldext
);
2334 new_endoff
= new->br_startoff
+ new->br_blockcount
;
2335 ASSERT(PREV
.br_startoff
<= new->br_startoff
);
2336 ASSERT(PREV
.br_startoff
+ PREV
.br_blockcount
>= new_endoff
);
2339 * Set flags determining what part of the previous oldext allocation
2340 * extent is being replaced by a newext allocation.
2342 if (PREV
.br_startoff
== new->br_startoff
)
2343 state
|= BMAP_LEFT_FILLING
;
2344 if (PREV
.br_startoff
+ PREV
.br_blockcount
== new_endoff
)
2345 state
|= BMAP_RIGHT_FILLING
;
2348 * Check and set flags if this segment has a left neighbor.
2349 * Don't set contiguous if the combined extent would be too large.
2352 state
|= BMAP_LEFT_VALID
;
2353 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, *idx
- 1), &LEFT
);
2355 if (isnullstartblock(LEFT
.br_startblock
))
2356 state
|= BMAP_LEFT_DELAY
;
2359 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
2360 LEFT
.br_startoff
+ LEFT
.br_blockcount
== new->br_startoff
&&
2361 LEFT
.br_startblock
+ LEFT
.br_blockcount
== new->br_startblock
&&
2362 LEFT
.br_state
== newext
&&
2363 LEFT
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
2364 state
|= BMAP_LEFT_CONTIG
;
2367 * Check and set flags if this segment has a right neighbor.
2368 * Don't set contiguous if the combined extent would be too large.
2369 * Also check for all-three-contiguous being too large.
2371 if (*idx
< ip
->i_df
.if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
) - 1) {
2372 state
|= BMAP_RIGHT_VALID
;
2373 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, *idx
+ 1), &RIGHT
);
2374 if (isnullstartblock(RIGHT
.br_startblock
))
2375 state
|= BMAP_RIGHT_DELAY
;
2378 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
2379 new_endoff
== RIGHT
.br_startoff
&&
2380 new->br_startblock
+ new->br_blockcount
== RIGHT
.br_startblock
&&
2381 newext
== RIGHT
.br_state
&&
2382 new->br_blockcount
+ RIGHT
.br_blockcount
<= MAXEXTLEN
&&
2383 ((state
& (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
2384 BMAP_RIGHT_FILLING
)) !=
2385 (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
2386 BMAP_RIGHT_FILLING
) ||
2387 LEFT
.br_blockcount
+ new->br_blockcount
+ RIGHT
.br_blockcount
2389 state
|= BMAP_RIGHT_CONTIG
;
2392 * Switch out based on the FILLING and CONTIG state bits.
2394 switch (state
& (BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
2395 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
)) {
2396 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
2397 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2399 * Setting all of a previous oldext extent to newext.
2400 * The left and right neighbors are both contiguous with new.
2404 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2405 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
),
2406 LEFT
.br_blockcount
+ PREV
.br_blockcount
+
2407 RIGHT
.br_blockcount
);
2408 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2410 xfs_iext_remove(ip
, *idx
+ 1, 2, state
);
2411 ip
->i_d
.di_nextents
-= 2;
2413 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2415 rval
= XFS_ILOG_CORE
;
2416 if ((error
= xfs_bmbt_lookup_eq(cur
, RIGHT
.br_startoff
,
2417 RIGHT
.br_startblock
,
2418 RIGHT
.br_blockcount
, &i
)))
2420 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2421 if ((error
= xfs_btree_delete(cur
, &i
)))
2423 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2424 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2426 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2427 if ((error
= xfs_btree_delete(cur
, &i
)))
2429 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2430 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2432 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2433 if ((error
= xfs_bmbt_update(cur
, LEFT
.br_startoff
,
2435 LEFT
.br_blockcount
+ PREV
.br_blockcount
+
2436 RIGHT
.br_blockcount
, LEFT
.br_state
)))
2441 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
2443 * Setting all of a previous oldext extent to newext.
2444 * The left neighbor is contiguous, the right is not.
2448 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2449 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
),
2450 LEFT
.br_blockcount
+ PREV
.br_blockcount
);
2451 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2453 xfs_iext_remove(ip
, *idx
+ 1, 1, state
);
2454 ip
->i_d
.di_nextents
--;
2456 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2458 rval
= XFS_ILOG_CORE
;
2459 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2460 PREV
.br_startblock
, PREV
.br_blockcount
,
2463 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2464 if ((error
= xfs_btree_delete(cur
, &i
)))
2466 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2467 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2469 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2470 if ((error
= xfs_bmbt_update(cur
, LEFT
.br_startoff
,
2472 LEFT
.br_blockcount
+ PREV
.br_blockcount
,
2478 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2480 * Setting all of a previous oldext extent to newext.
2481 * The right neighbor is contiguous, the left is not.
2483 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2484 xfs_bmbt_set_blockcount(ep
,
2485 PREV
.br_blockcount
+ RIGHT
.br_blockcount
);
2486 xfs_bmbt_set_state(ep
, newext
);
2487 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2488 xfs_iext_remove(ip
, *idx
+ 1, 1, state
);
2489 ip
->i_d
.di_nextents
--;
2491 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2493 rval
= XFS_ILOG_CORE
;
2494 if ((error
= xfs_bmbt_lookup_eq(cur
, RIGHT
.br_startoff
,
2495 RIGHT
.br_startblock
,
2496 RIGHT
.br_blockcount
, &i
)))
2498 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2499 if ((error
= xfs_btree_delete(cur
, &i
)))
2501 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2502 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2504 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2505 if ((error
= xfs_bmbt_update(cur
, new->br_startoff
,
2507 new->br_blockcount
+ RIGHT
.br_blockcount
,
2513 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
2515 * Setting all of a previous oldext extent to newext.
2516 * Neither the left nor right neighbors are contiguous with
2519 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2520 xfs_bmbt_set_state(ep
, newext
);
2521 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2524 rval
= XFS_ILOG_DEXT
;
2527 if ((error
= xfs_bmbt_lookup_eq(cur
, new->br_startoff
,
2528 new->br_startblock
, new->br_blockcount
,
2531 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2532 if ((error
= xfs_bmbt_update(cur
, new->br_startoff
,
2533 new->br_startblock
, new->br_blockcount
,
2539 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
:
2541 * Setting the first part of a previous oldext extent to newext.
2542 * The left neighbor is contiguous.
2544 trace_xfs_bmap_pre_update(ip
, *idx
- 1, state
, _THIS_IP_
);
2545 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
- 1),
2546 LEFT
.br_blockcount
+ new->br_blockcount
);
2547 xfs_bmbt_set_startoff(ep
,
2548 PREV
.br_startoff
+ new->br_blockcount
);
2549 trace_xfs_bmap_post_update(ip
, *idx
- 1, state
, _THIS_IP_
);
2551 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2552 xfs_bmbt_set_startblock(ep
,
2553 new->br_startblock
+ new->br_blockcount
);
2554 xfs_bmbt_set_blockcount(ep
,
2555 PREV
.br_blockcount
- new->br_blockcount
);
2556 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2561 rval
= XFS_ILOG_DEXT
;
2564 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2565 PREV
.br_startblock
, PREV
.br_blockcount
,
2568 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2569 if ((error
= xfs_bmbt_update(cur
,
2570 PREV
.br_startoff
+ new->br_blockcount
,
2571 PREV
.br_startblock
+ new->br_blockcount
,
2572 PREV
.br_blockcount
- new->br_blockcount
,
2575 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2577 error
= xfs_bmbt_update(cur
, LEFT
.br_startoff
,
2579 LEFT
.br_blockcount
+ new->br_blockcount
,
2586 case BMAP_LEFT_FILLING
:
2588 * Setting the first part of a previous oldext extent to newext.
2589 * The left neighbor is not contiguous.
2591 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2592 ASSERT(ep
&& xfs_bmbt_get_state(ep
) == oldext
);
2593 xfs_bmbt_set_startoff(ep
, new_endoff
);
2594 xfs_bmbt_set_blockcount(ep
,
2595 PREV
.br_blockcount
- new->br_blockcount
);
2596 xfs_bmbt_set_startblock(ep
,
2597 new->br_startblock
+ new->br_blockcount
);
2598 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2600 xfs_iext_insert(ip
, *idx
, 1, new, state
);
2601 ip
->i_d
.di_nextents
++;
2603 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2605 rval
= XFS_ILOG_CORE
;
2606 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2607 PREV
.br_startblock
, PREV
.br_blockcount
,
2610 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2611 if ((error
= xfs_bmbt_update(cur
,
2612 PREV
.br_startoff
+ new->br_blockcount
,
2613 PREV
.br_startblock
+ new->br_blockcount
,
2614 PREV
.br_blockcount
- new->br_blockcount
,
2617 cur
->bc_rec
.b
= *new;
2618 if ((error
= xfs_btree_insert(cur
, &i
)))
2620 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2624 case BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2626 * Setting the last part of a previous oldext extent to newext.
2627 * The right neighbor is contiguous with the new allocation.
2629 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2630 xfs_bmbt_set_blockcount(ep
,
2631 PREV
.br_blockcount
- new->br_blockcount
);
2632 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2636 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2637 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp
, *idx
),
2638 new->br_startoff
, new->br_startblock
,
2639 new->br_blockcount
+ RIGHT
.br_blockcount
, newext
);
2640 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2643 rval
= XFS_ILOG_DEXT
;
2646 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2648 PREV
.br_blockcount
, &i
)))
2650 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2651 if ((error
= xfs_bmbt_update(cur
, PREV
.br_startoff
,
2653 PREV
.br_blockcount
- new->br_blockcount
,
2656 if ((error
= xfs_btree_increment(cur
, 0, &i
)))
2658 if ((error
= xfs_bmbt_update(cur
, new->br_startoff
,
2660 new->br_blockcount
+ RIGHT
.br_blockcount
,
2666 case BMAP_RIGHT_FILLING
:
2668 * Setting the last part of a previous oldext extent to newext.
2669 * The right neighbor is not contiguous.
2671 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2672 xfs_bmbt_set_blockcount(ep
,
2673 PREV
.br_blockcount
- new->br_blockcount
);
2674 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2677 xfs_iext_insert(ip
, *idx
, 1, new, state
);
2679 ip
->i_d
.di_nextents
++;
2681 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2683 rval
= XFS_ILOG_CORE
;
2684 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2685 PREV
.br_startblock
, PREV
.br_blockcount
,
2688 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2689 if ((error
= xfs_bmbt_update(cur
, PREV
.br_startoff
,
2691 PREV
.br_blockcount
- new->br_blockcount
,
2694 if ((error
= xfs_bmbt_lookup_eq(cur
, new->br_startoff
,
2695 new->br_startblock
, new->br_blockcount
,
2698 XFS_WANT_CORRUPTED_GOTO(i
== 0, done
);
2699 cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
2700 if ((error
= xfs_btree_insert(cur
, &i
)))
2702 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2708 * Setting the middle part of a previous oldext extent to
2709 * newext. Contiguity is impossible here.
2710 * One extent becomes three extents.
2712 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2713 xfs_bmbt_set_blockcount(ep
,
2714 new->br_startoff
- PREV
.br_startoff
);
2715 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2718 r
[1].br_startoff
= new_endoff
;
2719 r
[1].br_blockcount
=
2720 PREV
.br_startoff
+ PREV
.br_blockcount
- new_endoff
;
2721 r
[1].br_startblock
= new->br_startblock
+ new->br_blockcount
;
2722 r
[1].br_state
= oldext
;
2725 xfs_iext_insert(ip
, *idx
, 2, &r
[0], state
);
2727 ip
->i_d
.di_nextents
+= 2;
2729 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2731 rval
= XFS_ILOG_CORE
;
2732 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2733 PREV
.br_startblock
, PREV
.br_blockcount
,
2736 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2737 /* new right extent - oldext */
2738 if ((error
= xfs_bmbt_update(cur
, r
[1].br_startoff
,
2739 r
[1].br_startblock
, r
[1].br_blockcount
,
2742 /* new left extent - oldext */
2743 cur
->bc_rec
.b
= PREV
;
2744 cur
->bc_rec
.b
.br_blockcount
=
2745 new->br_startoff
- PREV
.br_startoff
;
2746 if ((error
= xfs_btree_insert(cur
, &i
)))
2748 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2750 * Reset the cursor to the position of the new extent
2751 * we are about to insert as we can't trust it after
2752 * the previous insert.
2754 if ((error
= xfs_bmbt_lookup_eq(cur
, new->br_startoff
,
2755 new->br_startblock
, new->br_blockcount
,
2758 XFS_WANT_CORRUPTED_GOTO(i
== 0, done
);
2759 /* new middle extent - newext */
2760 cur
->bc_rec
.b
.br_state
= new->br_state
;
2761 if ((error
= xfs_btree_insert(cur
, &i
)))
2763 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2767 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2768 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2769 case BMAP_LEFT_FILLING
| BMAP_RIGHT_CONTIG
:
2770 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
2771 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2772 case BMAP_LEFT_CONTIG
:
2773 case BMAP_RIGHT_CONTIG
:
2775 * These cases are all impossible.
2780 /* convert to a btree if necessary */
2781 if (xfs_bmap_needs_btree(ip
, XFS_DATA_FORK
)) {
2782 int tmp_logflags
; /* partial log flag return val */
2784 ASSERT(cur
== NULL
);
2785 error
= xfs_bmap_extents_to_btree(tp
, ip
, first
, flist
, &cur
,
2786 0, &tmp_logflags
, XFS_DATA_FORK
);
2787 *logflagsp
|= tmp_logflags
;
2792 /* clear out the allocated field, done with it now in any case. */
2794 cur
->bc_private
.b
.allocated
= 0;
2798 xfs_bmap_check_leaf_extents(*curp
, ip
, XFS_DATA_FORK
);
2808 * Convert a hole to a delayed allocation.
2811 xfs_bmap_add_extent_hole_delay(
2812 xfs_inode_t
*ip
, /* incore inode pointer */
2813 xfs_extnum_t
*idx
, /* extent number to update/insert */
2814 xfs_bmbt_irec_t
*new) /* new data to add to file extents */
2816 xfs_ifork_t
*ifp
; /* inode fork pointer */
2817 xfs_bmbt_irec_t left
; /* left neighbor extent entry */
2818 xfs_filblks_t newlen
=0; /* new indirect size */
2819 xfs_filblks_t oldlen
=0; /* old indirect size */
2820 xfs_bmbt_irec_t right
; /* right neighbor extent entry */
2821 int state
; /* state bits, accessed thru macros */
2822 xfs_filblks_t temp
=0; /* temp for indirect calculations */
2824 ifp
= XFS_IFORK_PTR(ip
, XFS_DATA_FORK
);
2826 ASSERT(isnullstartblock(new->br_startblock
));
2829 * Check and set flags if this segment has a left neighbor
2832 state
|= BMAP_LEFT_VALID
;
2833 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, *idx
- 1), &left
);
2835 if (isnullstartblock(left
.br_startblock
))
2836 state
|= BMAP_LEFT_DELAY
;
2840 * Check and set flags if the current (right) segment exists.
2841 * If it doesn't exist, we're converting the hole at end-of-file.
2843 if (*idx
< ip
->i_df
.if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
)) {
2844 state
|= BMAP_RIGHT_VALID
;
2845 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, *idx
), &right
);
2847 if (isnullstartblock(right
.br_startblock
))
2848 state
|= BMAP_RIGHT_DELAY
;
2852 * Set contiguity flags on the left and right neighbors.
2853 * Don't let extents get too large, even if the pieces are contiguous.
2855 if ((state
& BMAP_LEFT_VALID
) && (state
& BMAP_LEFT_DELAY
) &&
2856 left
.br_startoff
+ left
.br_blockcount
== new->br_startoff
&&
2857 left
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
2858 state
|= BMAP_LEFT_CONTIG
;
2860 if ((state
& BMAP_RIGHT_VALID
) && (state
& BMAP_RIGHT_DELAY
) &&
2861 new->br_startoff
+ new->br_blockcount
== right
.br_startoff
&&
2862 new->br_blockcount
+ right
.br_blockcount
<= MAXEXTLEN
&&
2863 (!(state
& BMAP_LEFT_CONTIG
) ||
2864 (left
.br_blockcount
+ new->br_blockcount
+
2865 right
.br_blockcount
<= MAXEXTLEN
)))
2866 state
|= BMAP_RIGHT_CONTIG
;
2869 * Switch out based on the contiguity flags.
2871 switch (state
& (BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
)) {
2872 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2874 * New allocation is contiguous with delayed allocations
2875 * on the left and on the right.
2876 * Merge all three into a single extent record.
2879 temp
= left
.br_blockcount
+ new->br_blockcount
+
2880 right
.br_blockcount
;
2882 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2883 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
), temp
);
2884 oldlen
= startblockval(left
.br_startblock
) +
2885 startblockval(new->br_startblock
) +
2886 startblockval(right
.br_startblock
);
2887 newlen
= xfs_bmap_worst_indlen(ip
, temp
);
2888 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp
, *idx
),
2889 nullstartblock((int)newlen
));
2890 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2892 xfs_iext_remove(ip
, *idx
+ 1, 1, state
);
2895 case BMAP_LEFT_CONTIG
:
2897 * New allocation is contiguous with a delayed allocation
2899 * Merge the new allocation with the left neighbor.
2902 temp
= left
.br_blockcount
+ new->br_blockcount
;
2904 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2905 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
), temp
);
2906 oldlen
= startblockval(left
.br_startblock
) +
2907 startblockval(new->br_startblock
);
2908 newlen
= xfs_bmap_worst_indlen(ip
, temp
);
2909 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp
, *idx
),
2910 nullstartblock((int)newlen
));
2911 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2914 case BMAP_RIGHT_CONTIG
:
2916 * New allocation is contiguous with a delayed allocation
2918 * Merge the new allocation with the right neighbor.
2920 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2921 temp
= new->br_blockcount
+ right
.br_blockcount
;
2922 oldlen
= startblockval(new->br_startblock
) +
2923 startblockval(right
.br_startblock
);
2924 newlen
= xfs_bmap_worst_indlen(ip
, temp
);
2925 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp
, *idx
),
2927 nullstartblock((int)newlen
), temp
, right
.br_state
);
2928 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2933 * New allocation is not contiguous with another
2934 * delayed allocation.
2935 * Insert a new entry.
2937 oldlen
= newlen
= 0;
2938 xfs_iext_insert(ip
, *idx
, 1, new, state
);
2941 if (oldlen
!= newlen
) {
2942 ASSERT(oldlen
> newlen
);
2943 xfs_icsb_modify_counters(ip
->i_mount
, XFS_SBS_FDBLOCKS
,
2944 (int64_t)(oldlen
- newlen
), 0);
2946 * Nothing to do for disk quota accounting here.
2952 * Convert a hole to a real allocation.
2954 STATIC
int /* error */
2955 xfs_bmap_add_extent_hole_real(
2956 struct xfs_bmalloca
*bma
,
2959 struct xfs_bmbt_irec
*new = &bma
->got
;
2960 int error
; /* error return value */
2961 int i
; /* temp state */
2962 xfs_ifork_t
*ifp
; /* inode fork pointer */
2963 xfs_bmbt_irec_t left
; /* left neighbor extent entry */
2964 xfs_bmbt_irec_t right
; /* right neighbor extent entry */
2965 int rval
=0; /* return value (logging flags) */
2966 int state
; /* state bits, accessed thru macros */
2968 ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
2970 ASSERT(bma
->idx
>= 0);
2971 ASSERT(bma
->idx
<= ifp
->if_bytes
/ sizeof(struct xfs_bmbt_rec
));
2972 ASSERT(!isnullstartblock(new->br_startblock
));
2974 !(bma
->cur
->bc_private
.b
.flags
& XFS_BTCUR_BPRV_WASDEL
));
2976 XFS_STATS_INC(xs_add_exlist
);
2979 if (whichfork
== XFS_ATTR_FORK
)
2980 state
|= BMAP_ATTRFORK
;
2983 * Check and set flags if this segment has a left neighbor.
2986 state
|= BMAP_LEFT_VALID
;
2987 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
- 1), &left
);
2988 if (isnullstartblock(left
.br_startblock
))
2989 state
|= BMAP_LEFT_DELAY
;
2993 * Check and set flags if this segment has a current value.
2994 * Not true if we're inserting into the "hole" at eof.
2996 if (bma
->idx
< ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
)) {
2997 state
|= BMAP_RIGHT_VALID
;
2998 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
), &right
);
2999 if (isnullstartblock(right
.br_startblock
))
3000 state
|= BMAP_RIGHT_DELAY
;
3004 * We're inserting a real allocation between "left" and "right".
3005 * Set the contiguity flags. Don't let extents get too large.
3007 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
3008 left
.br_startoff
+ left
.br_blockcount
== new->br_startoff
&&
3009 left
.br_startblock
+ left
.br_blockcount
== new->br_startblock
&&
3010 left
.br_state
== new->br_state
&&
3011 left
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
3012 state
|= BMAP_LEFT_CONTIG
;
3014 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
3015 new->br_startoff
+ new->br_blockcount
== right
.br_startoff
&&
3016 new->br_startblock
+ new->br_blockcount
== right
.br_startblock
&&
3017 new->br_state
== right
.br_state
&&
3018 new->br_blockcount
+ right
.br_blockcount
<= MAXEXTLEN
&&
3019 (!(state
& BMAP_LEFT_CONTIG
) ||
3020 left
.br_blockcount
+ new->br_blockcount
+
3021 right
.br_blockcount
<= MAXEXTLEN
))
3022 state
|= BMAP_RIGHT_CONTIG
;
3026 * Select which case we're in here, and implement it.
3028 switch (state
& (BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
)) {
3029 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
3031 * New allocation is contiguous with real allocations on the
3032 * left and on the right.
3033 * Merge all three into a single extent record.
3036 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
3037 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, bma
->idx
),
3038 left
.br_blockcount
+ new->br_blockcount
+
3039 right
.br_blockcount
);
3040 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
3042 xfs_iext_remove(bma
->ip
, bma
->idx
+ 1, 1, state
);
3044 XFS_IFORK_NEXT_SET(bma
->ip
, whichfork
,
3045 XFS_IFORK_NEXTENTS(bma
->ip
, whichfork
) - 1);
3046 if (bma
->cur
== NULL
) {
3047 rval
= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
3049 rval
= XFS_ILOG_CORE
;
3050 error
= xfs_bmbt_lookup_eq(bma
->cur
, right
.br_startoff
,
3051 right
.br_startblock
, right
.br_blockcount
,
3055 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
3056 error
= xfs_btree_delete(bma
->cur
, &i
);
3059 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
3060 error
= xfs_btree_decrement(bma
->cur
, 0, &i
);
3063 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
3064 error
= xfs_bmbt_update(bma
->cur
, left
.br_startoff
,
3066 left
.br_blockcount
+
3067 new->br_blockcount
+
3068 right
.br_blockcount
,
3075 case BMAP_LEFT_CONTIG
:
3077 * New allocation is contiguous with a real allocation
3079 * Merge the new allocation with the left neighbor.
3082 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
3083 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, bma
->idx
),
3084 left
.br_blockcount
+ new->br_blockcount
);
3085 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
3087 if (bma
->cur
== NULL
) {
3088 rval
= xfs_ilog_fext(whichfork
);
3091 error
= xfs_bmbt_lookup_eq(bma
->cur
, left
.br_startoff
,
3092 left
.br_startblock
, left
.br_blockcount
,
3096 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
3097 error
= xfs_bmbt_update(bma
->cur
, left
.br_startoff
,
3099 left
.br_blockcount
+
3107 case BMAP_RIGHT_CONTIG
:
3109 * New allocation is contiguous with a real allocation
3111 * Merge the new allocation with the right neighbor.
3113 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
3114 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp
, bma
->idx
),
3115 new->br_startoff
, new->br_startblock
,
3116 new->br_blockcount
+ right
.br_blockcount
,
3118 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
3120 if (bma
->cur
== NULL
) {
3121 rval
= xfs_ilog_fext(whichfork
);
3124 error
= xfs_bmbt_lookup_eq(bma
->cur
,
3126 right
.br_startblock
,
3127 right
.br_blockcount
, &i
);
3130 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
3131 error
= xfs_bmbt_update(bma
->cur
, new->br_startoff
,
3133 new->br_blockcount
+
3134 right
.br_blockcount
,
3143 * New allocation is not contiguous with another
3145 * Insert a new entry.
3147 xfs_iext_insert(bma
->ip
, bma
->idx
, 1, new, state
);
3148 XFS_IFORK_NEXT_SET(bma
->ip
, whichfork
,
3149 XFS_IFORK_NEXTENTS(bma
->ip
, whichfork
) + 1);
3150 if (bma
->cur
== NULL
) {
3151 rval
= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
3153 rval
= XFS_ILOG_CORE
;
3154 error
= xfs_bmbt_lookup_eq(bma
->cur
,
3157 new->br_blockcount
, &i
);
3160 XFS_WANT_CORRUPTED_GOTO(i
== 0, done
);
3161 bma
->cur
->bc_rec
.b
.br_state
= new->br_state
;
3162 error
= xfs_btree_insert(bma
->cur
, &i
);
3165 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
3170 /* convert to a btree if necessary */
3171 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
3172 int tmp_logflags
; /* partial log flag return val */
3174 ASSERT(bma
->cur
== NULL
);
3175 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
3176 bma
->firstblock
, bma
->flist
, &bma
->cur
,
3177 0, &tmp_logflags
, whichfork
);
3178 bma
->logflags
|= tmp_logflags
;
3183 /* clear out the allocated field, done with it now in any case. */
3185 bma
->cur
->bc_private
.b
.allocated
= 0;
3187 xfs_bmap_check_leaf_extents(bma
->cur
, bma
->ip
, whichfork
);
3189 bma
->logflags
|= rval
;
3194 * Functions used in the extent read, allocate and remove paths
3198 * Adjust the size of the new extent based on di_extsize and rt extsize.
3201 xfs_bmap_extsize_align(
3203 xfs_bmbt_irec_t
*gotp
, /* next extent pointer */
3204 xfs_bmbt_irec_t
*prevp
, /* previous extent pointer */
3205 xfs_extlen_t extsz
, /* align to this extent size */
3206 int rt
, /* is this a realtime inode? */
3207 int eof
, /* is extent at end-of-file? */
3208 int delay
, /* creating delalloc extent? */
3209 int convert
, /* overwriting unwritten extent? */
3210 xfs_fileoff_t
*offp
, /* in/out: aligned offset */
3211 xfs_extlen_t
*lenp
) /* in/out: aligned length */
3213 xfs_fileoff_t orig_off
; /* original offset */
3214 xfs_extlen_t orig_alen
; /* original length */
3215 xfs_fileoff_t orig_end
; /* original off+len */
3216 xfs_fileoff_t nexto
; /* next file offset */
3217 xfs_fileoff_t prevo
; /* previous file offset */
3218 xfs_fileoff_t align_off
; /* temp for offset */
3219 xfs_extlen_t align_alen
; /* temp for length */
3220 xfs_extlen_t temp
; /* temp for calculations */
3225 orig_off
= align_off
= *offp
;
3226 orig_alen
= align_alen
= *lenp
;
3227 orig_end
= orig_off
+ orig_alen
;
3230 * If this request overlaps an existing extent, then don't
3231 * attempt to perform any additional alignment.
3233 if (!delay
&& !eof
&&
3234 (orig_off
>= gotp
->br_startoff
) &&
3235 (orig_end
<= gotp
->br_startoff
+ gotp
->br_blockcount
)) {
3240 * If the file offset is unaligned vs. the extent size
3241 * we need to align it. This will be possible unless
3242 * the file was previously written with a kernel that didn't
3243 * perform this alignment, or if a truncate shot us in the
3246 temp
= do_mod(orig_off
, extsz
);
3252 * Same adjustment for the end of the requested area.
3254 if ((temp
= (align_alen
% extsz
))) {
3255 align_alen
+= extsz
- temp
;
3258 * If the previous block overlaps with this proposed allocation
3259 * then move the start forward without adjusting the length.
3261 if (prevp
->br_startoff
!= NULLFILEOFF
) {
3262 if (prevp
->br_startblock
== HOLESTARTBLOCK
)
3263 prevo
= prevp
->br_startoff
;
3265 prevo
= prevp
->br_startoff
+ prevp
->br_blockcount
;
3268 if (align_off
!= orig_off
&& align_off
< prevo
)
3271 * If the next block overlaps with this proposed allocation
3272 * then move the start back without adjusting the length,
3273 * but not before offset 0.
3274 * This may of course make the start overlap previous block,
3275 * and if we hit the offset 0 limit then the next block
3276 * can still overlap too.
3278 if (!eof
&& gotp
->br_startoff
!= NULLFILEOFF
) {
3279 if ((delay
&& gotp
->br_startblock
== HOLESTARTBLOCK
) ||
3280 (!delay
&& gotp
->br_startblock
== DELAYSTARTBLOCK
))
3281 nexto
= gotp
->br_startoff
+ gotp
->br_blockcount
;
3283 nexto
= gotp
->br_startoff
;
3285 nexto
= NULLFILEOFF
;
3287 align_off
+ align_alen
!= orig_end
&&
3288 align_off
+ align_alen
> nexto
)
3289 align_off
= nexto
> align_alen
? nexto
- align_alen
: 0;
3291 * If we're now overlapping the next or previous extent that
3292 * means we can't fit an extsz piece in this hole. Just move
3293 * the start forward to the first valid spot and set
3294 * the length so we hit the end.
3296 if (align_off
!= orig_off
&& align_off
< prevo
)
3298 if (align_off
+ align_alen
!= orig_end
&&
3299 align_off
+ align_alen
> nexto
&&
3300 nexto
!= NULLFILEOFF
) {
3301 ASSERT(nexto
> prevo
);
3302 align_alen
= nexto
- align_off
;
3306 * If realtime, and the result isn't a multiple of the realtime
3307 * extent size we need to remove blocks until it is.
3309 if (rt
&& (temp
= (align_alen
% mp
->m_sb
.sb_rextsize
))) {
3311 * We're not covering the original request, or
3312 * we won't be able to once we fix the length.
3314 if (orig_off
< align_off
||
3315 orig_end
> align_off
+ align_alen
||
3316 align_alen
- temp
< orig_alen
)
3319 * Try to fix it by moving the start up.
3321 if (align_off
+ temp
<= orig_off
) {
3326 * Try to fix it by moving the end in.
3328 else if (align_off
+ align_alen
- temp
>= orig_end
)
3331 * Set the start to the minimum then trim the length.
3334 align_alen
-= orig_off
- align_off
;
3335 align_off
= orig_off
;
3336 align_alen
-= align_alen
% mp
->m_sb
.sb_rextsize
;
3339 * Result doesn't cover the request, fail it.
3341 if (orig_off
< align_off
|| orig_end
> align_off
+ align_alen
)
3344 ASSERT(orig_off
>= align_off
);
3345 ASSERT(orig_end
<= align_off
+ align_alen
);
3349 if (!eof
&& gotp
->br_startoff
!= NULLFILEOFF
)
3350 ASSERT(align_off
+ align_alen
<= gotp
->br_startoff
);
3351 if (prevp
->br_startoff
!= NULLFILEOFF
)
3352 ASSERT(align_off
>= prevp
->br_startoff
+ prevp
->br_blockcount
);
3360 #define XFS_ALLOC_GAP_UNITS 4
3364 struct xfs_bmalloca
*ap
) /* bmap alloc argument struct */
3366 xfs_fsblock_t adjust
; /* adjustment to block numbers */
3367 xfs_agnumber_t fb_agno
; /* ag number of ap->firstblock */
3368 xfs_mount_t
*mp
; /* mount point structure */
3369 int nullfb
; /* true if ap->firstblock isn't set */
3370 int rt
; /* true if inode is realtime */
3372 #define ISVALID(x,y) \
3374 (x) < mp->m_sb.sb_rblocks : \
3375 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
3376 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
3377 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
3379 mp
= ap
->ip
->i_mount
;
3380 nullfb
= *ap
->firstblock
== NULLFSBLOCK
;
3381 rt
= XFS_IS_REALTIME_INODE(ap
->ip
) && ap
->userdata
;
3382 fb_agno
= nullfb
? NULLAGNUMBER
: XFS_FSB_TO_AGNO(mp
, *ap
->firstblock
);
3384 * If allocating at eof, and there's a previous real block,
3385 * try to use its last block as our starting point.
3387 if (ap
->eof
&& ap
->prev
.br_startoff
!= NULLFILEOFF
&&
3388 !isnullstartblock(ap
->prev
.br_startblock
) &&
3389 ISVALID(ap
->prev
.br_startblock
+ ap
->prev
.br_blockcount
,
3390 ap
->prev
.br_startblock
)) {
3391 ap
->blkno
= ap
->prev
.br_startblock
+ ap
->prev
.br_blockcount
;
3393 * Adjust for the gap between prevp and us.
3395 adjust
= ap
->offset
-
3396 (ap
->prev
.br_startoff
+ ap
->prev
.br_blockcount
);
3398 ISVALID(ap
->blkno
+ adjust
, ap
->prev
.br_startblock
))
3399 ap
->blkno
+= adjust
;
3402 * If not at eof, then compare the two neighbor blocks.
3403 * Figure out whether either one gives us a good starting point,
3404 * and pick the better one.
3406 else if (!ap
->eof
) {
3407 xfs_fsblock_t gotbno
; /* right side block number */
3408 xfs_fsblock_t gotdiff
=0; /* right side difference */
3409 xfs_fsblock_t prevbno
; /* left side block number */
3410 xfs_fsblock_t prevdiff
=0; /* left side difference */
3413 * If there's a previous (left) block, select a requested
3414 * start block based on it.
3416 if (ap
->prev
.br_startoff
!= NULLFILEOFF
&&
3417 !isnullstartblock(ap
->prev
.br_startblock
) &&
3418 (prevbno
= ap
->prev
.br_startblock
+
3419 ap
->prev
.br_blockcount
) &&
3420 ISVALID(prevbno
, ap
->prev
.br_startblock
)) {
3422 * Calculate gap to end of previous block.
3424 adjust
= prevdiff
= ap
->offset
-
3425 (ap
->prev
.br_startoff
+
3426 ap
->prev
.br_blockcount
);
3428 * Figure the startblock based on the previous block's
3429 * end and the gap size.
3431 * If the gap is large relative to the piece we're
3432 * allocating, or using it gives us an invalid block
3433 * number, then just use the end of the previous block.
3435 if (prevdiff
<= XFS_ALLOC_GAP_UNITS
* ap
->length
&&
3436 ISVALID(prevbno
+ prevdiff
,
3437 ap
->prev
.br_startblock
))
3442 * If the firstblock forbids it, can't use it,
3445 if (!rt
&& !nullfb
&&
3446 XFS_FSB_TO_AGNO(mp
, prevbno
) != fb_agno
)
3447 prevbno
= NULLFSBLOCK
;
3450 * No previous block or can't follow it, just default.
3453 prevbno
= NULLFSBLOCK
;
3455 * If there's a following (right) block, select a requested
3456 * start block based on it.
3458 if (!isnullstartblock(ap
->got
.br_startblock
)) {
3460 * Calculate gap to start of next block.
3462 adjust
= gotdiff
= ap
->got
.br_startoff
- ap
->offset
;
3464 * Figure the startblock based on the next block's
3465 * start and the gap size.
3467 gotbno
= ap
->got
.br_startblock
;
3470 * If the gap is large relative to the piece we're
3471 * allocating, or using it gives us an invalid block
3472 * number, then just use the start of the next block
3473 * offset by our length.
3475 if (gotdiff
<= XFS_ALLOC_GAP_UNITS
* ap
->length
&&
3476 ISVALID(gotbno
- gotdiff
, gotbno
))
3478 else if (ISVALID(gotbno
- ap
->length
, gotbno
)) {
3479 gotbno
-= ap
->length
;
3480 gotdiff
+= adjust
- ap
->length
;
3484 * If the firstblock forbids it, can't use it,
3487 if (!rt
&& !nullfb
&&
3488 XFS_FSB_TO_AGNO(mp
, gotbno
) != fb_agno
)
3489 gotbno
= NULLFSBLOCK
;
3492 * No next block, just default.
3495 gotbno
= NULLFSBLOCK
;
3497 * If both valid, pick the better one, else the only good
3498 * one, else ap->blkno is already set (to 0 or the inode block).
3500 if (prevbno
!= NULLFSBLOCK
&& gotbno
!= NULLFSBLOCK
)
3501 ap
->blkno
= prevdiff
<= gotdiff
? prevbno
: gotbno
;
3502 else if (prevbno
!= NULLFSBLOCK
)
3503 ap
->blkno
= prevbno
;
3504 else if (gotbno
!= NULLFSBLOCK
)
3511 xfs_bmap_longest_free_extent(
3512 struct xfs_trans
*tp
,
3517 struct xfs_mount
*mp
= tp
->t_mountp
;
3518 struct xfs_perag
*pag
;
3519 xfs_extlen_t longest
;
3522 pag
= xfs_perag_get(mp
, ag
);
3523 if (!pag
->pagf_init
) {
3524 error
= xfs_alloc_pagf_init(mp
, tp
, ag
, XFS_ALLOC_FLAG_TRYLOCK
);
3528 if (!pag
->pagf_init
) {
3534 longest
= xfs_alloc_longest_free_extent(mp
, pag
);
3535 if (*blen
< longest
)
3544 xfs_bmap_select_minlen(
3545 struct xfs_bmalloca
*ap
,
3546 struct xfs_alloc_arg
*args
,
3550 if (notinit
|| *blen
< ap
->minlen
) {
3552 * Since we did a BUF_TRYLOCK above, it is possible that
3553 * there is space for this request.
3555 args
->minlen
= ap
->minlen
;
3556 } else if (*blen
< args
->maxlen
) {
3558 * If the best seen length is less than the request length,
3559 * use the best as the minimum.
3561 args
->minlen
= *blen
;
3564 * Otherwise we've seen an extent as big as maxlen, use that
3567 args
->minlen
= args
->maxlen
;
3572 xfs_bmap_btalloc_nullfb(
3573 struct xfs_bmalloca
*ap
,
3574 struct xfs_alloc_arg
*args
,
3577 struct xfs_mount
*mp
= ap
->ip
->i_mount
;
3578 xfs_agnumber_t ag
, startag
;
3582 args
->type
= XFS_ALLOCTYPE_START_BNO
;
3583 args
->total
= ap
->total
;
3585 startag
= ag
= XFS_FSB_TO_AGNO(mp
, args
->fsbno
);
3586 if (startag
== NULLAGNUMBER
)
3589 while (*blen
< args
->maxlen
) {
3590 error
= xfs_bmap_longest_free_extent(args
->tp
, ag
, blen
,
3595 if (++ag
== mp
->m_sb
.sb_agcount
)
3601 xfs_bmap_select_minlen(ap
, args
, blen
, notinit
);
3606 xfs_bmap_btalloc_filestreams(
3607 struct xfs_bmalloca
*ap
,
3608 struct xfs_alloc_arg
*args
,
3611 struct xfs_mount
*mp
= ap
->ip
->i_mount
;
3616 args
->type
= XFS_ALLOCTYPE_NEAR_BNO
;
3617 args
->total
= ap
->total
;
3619 ag
= XFS_FSB_TO_AGNO(mp
, args
->fsbno
);
3620 if (ag
== NULLAGNUMBER
)
3623 error
= xfs_bmap_longest_free_extent(args
->tp
, ag
, blen
, ¬init
);
3627 if (*blen
< args
->maxlen
) {
3628 error
= xfs_filestream_new_ag(ap
, &ag
);
3632 error
= xfs_bmap_longest_free_extent(args
->tp
, ag
, blen
,
3639 xfs_bmap_select_minlen(ap
, args
, blen
, notinit
);
3642 * Set the failure fallback case to look in the selected AG as stream
3645 ap
->blkno
= args
->fsbno
= XFS_AGB_TO_FSB(mp
, ag
, 0);
3651 struct xfs_bmalloca
*ap
) /* bmap alloc argument struct */
3653 xfs_mount_t
*mp
; /* mount point structure */
3654 xfs_alloctype_t atype
= 0; /* type for allocation routines */
3655 xfs_extlen_t align
; /* minimum allocation alignment */
3656 xfs_agnumber_t fb_agno
; /* ag number of ap->firstblock */
3658 xfs_alloc_arg_t args
;
3660 xfs_extlen_t nextminlen
= 0;
3661 int nullfb
; /* true if ap->firstblock isn't set */
3669 mp
= ap
->ip
->i_mount
;
3671 /* stripe alignment for allocation is determined by mount parameters */
3673 if (mp
->m_swidth
&& (mp
->m_flags
& XFS_MOUNT_SWALLOC
))
3674 stripe_align
= mp
->m_swidth
;
3675 else if (mp
->m_dalign
)
3676 stripe_align
= mp
->m_dalign
;
3678 align
= ap
->userdata
? xfs_get_extsz_hint(ap
->ip
) : 0;
3679 if (unlikely(align
)) {
3680 error
= xfs_bmap_extsize_align(mp
, &ap
->got
, &ap
->prev
,
3681 align
, 0, ap
->eof
, 0, ap
->conv
,
3682 &ap
->offset
, &ap
->length
);
3688 nullfb
= *ap
->firstblock
== NULLFSBLOCK
;
3689 fb_agno
= nullfb
? NULLAGNUMBER
: XFS_FSB_TO_AGNO(mp
, *ap
->firstblock
);
3691 if (ap
->userdata
&& xfs_inode_is_filestream(ap
->ip
)) {
3692 ag
= xfs_filestream_lookup_ag(ap
->ip
);
3693 ag
= (ag
!= NULLAGNUMBER
) ? ag
: 0;
3694 ap
->blkno
= XFS_AGB_TO_FSB(mp
, ag
, 0);
3696 ap
->blkno
= XFS_INO_TO_FSB(mp
, ap
->ip
->i_ino
);
3699 ap
->blkno
= *ap
->firstblock
;
3701 xfs_bmap_adjacent(ap
);
3704 * If allowed, use ap->blkno; otherwise must use firstblock since
3705 * it's in the right allocation group.
3707 if (nullfb
|| XFS_FSB_TO_AGNO(mp
, ap
->blkno
) == fb_agno
)
3710 ap
->blkno
= *ap
->firstblock
;
3712 * Normal allocation, done through xfs_alloc_vextent.
3714 tryagain
= isaligned
= 0;
3715 memset(&args
, 0, sizeof(args
));
3718 args
.fsbno
= ap
->blkno
;
3720 /* Trim the allocation back to the maximum an AG can fit. */
3721 args
.maxlen
= MIN(ap
->length
, XFS_ALLOC_AG_MAX_USABLE(mp
));
3722 args
.firstblock
= *ap
->firstblock
;
3726 * Search for an allocation group with a single extent large
3727 * enough for the request. If one isn't found, then adjust
3728 * the minimum allocation size to the largest space found.
3730 if (ap
->userdata
&& xfs_inode_is_filestream(ap
->ip
))
3731 error
= xfs_bmap_btalloc_filestreams(ap
, &args
, &blen
);
3733 error
= xfs_bmap_btalloc_nullfb(ap
, &args
, &blen
);
3736 } else if (ap
->flist
->xbf_low
) {
3737 if (xfs_inode_is_filestream(ap
->ip
))
3738 args
.type
= XFS_ALLOCTYPE_FIRST_AG
;
3740 args
.type
= XFS_ALLOCTYPE_START_BNO
;
3741 args
.total
= args
.minlen
= ap
->minlen
;
3743 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
3744 args
.total
= ap
->total
;
3745 args
.minlen
= ap
->minlen
;
3747 /* apply extent size hints if obtained earlier */
3748 if (unlikely(align
)) {
3750 if ((args
.mod
= (xfs_extlen_t
)do_mod(ap
->offset
, args
.prod
)))
3751 args
.mod
= (xfs_extlen_t
)(args
.prod
- args
.mod
);
3752 } else if (mp
->m_sb
.sb_blocksize
>= PAGE_CACHE_SIZE
) {
3756 args
.prod
= PAGE_CACHE_SIZE
>> mp
->m_sb
.sb_blocklog
;
3757 if ((args
.mod
= (xfs_extlen_t
)(do_mod(ap
->offset
, args
.prod
))))
3758 args
.mod
= (xfs_extlen_t
)(args
.prod
- args
.mod
);
3761 * If we are not low on available data blocks, and the
3762 * underlying logical volume manager is a stripe, and
3763 * the file offset is zero then try to allocate data
3764 * blocks on stripe unit boundary.
3765 * NOTE: ap->aeof is only set if the allocation length
3766 * is >= the stripe unit and the allocation offset is
3767 * at the end of file.
3769 if (!ap
->flist
->xbf_low
&& ap
->aeof
) {
3771 args
.alignment
= stripe_align
;
3775 * Adjust for alignment
3777 if (blen
> args
.alignment
&& blen
<= args
.maxlen
)
3778 args
.minlen
= blen
- args
.alignment
;
3779 args
.minalignslop
= 0;
3782 * First try an exact bno allocation.
3783 * If it fails then do a near or start bno
3784 * allocation with alignment turned on.
3788 args
.type
= XFS_ALLOCTYPE_THIS_BNO
;
3791 * Compute the minlen+alignment for the
3792 * next case. Set slop so that the value
3793 * of minlen+alignment+slop doesn't go up
3794 * between the calls.
3796 if (blen
> stripe_align
&& blen
<= args
.maxlen
)
3797 nextminlen
= blen
- stripe_align
;
3799 nextminlen
= args
.minlen
;
3800 if (nextminlen
+ stripe_align
> args
.minlen
+ 1)
3802 nextminlen
+ stripe_align
-
3805 args
.minalignslop
= 0;
3809 args
.minalignslop
= 0;
3811 args
.minleft
= ap
->minleft
;
3812 args
.wasdel
= ap
->wasdel
;
3814 args
.userdata
= ap
->userdata
;
3815 if ((error
= xfs_alloc_vextent(&args
)))
3817 if (tryagain
&& args
.fsbno
== NULLFSBLOCK
) {
3819 * Exact allocation failed. Now try with alignment
3823 args
.fsbno
= ap
->blkno
;
3824 args
.alignment
= stripe_align
;
3825 args
.minlen
= nextminlen
;
3826 args
.minalignslop
= 0;
3828 if ((error
= xfs_alloc_vextent(&args
)))
3831 if (isaligned
&& args
.fsbno
== NULLFSBLOCK
) {
3833 * allocation failed, so turn off alignment and
3837 args
.fsbno
= ap
->blkno
;
3839 if ((error
= xfs_alloc_vextent(&args
)))
3842 if (args
.fsbno
== NULLFSBLOCK
&& nullfb
&&
3843 args
.minlen
> ap
->minlen
) {
3844 args
.minlen
= ap
->minlen
;
3845 args
.type
= XFS_ALLOCTYPE_START_BNO
;
3846 args
.fsbno
= ap
->blkno
;
3847 if ((error
= xfs_alloc_vextent(&args
)))
3850 if (args
.fsbno
== NULLFSBLOCK
&& nullfb
) {
3852 args
.type
= XFS_ALLOCTYPE_FIRST_AG
;
3853 args
.total
= ap
->minlen
;
3855 if ((error
= xfs_alloc_vextent(&args
)))
3857 ap
->flist
->xbf_low
= 1;
3859 if (args
.fsbno
!= NULLFSBLOCK
) {
3861 * check the allocation happened at the same or higher AG than
3862 * the first block that was allocated.
3864 ASSERT(*ap
->firstblock
== NULLFSBLOCK
||
3865 XFS_FSB_TO_AGNO(mp
, *ap
->firstblock
) ==
3866 XFS_FSB_TO_AGNO(mp
, args
.fsbno
) ||
3867 (ap
->flist
->xbf_low
&&
3868 XFS_FSB_TO_AGNO(mp
, *ap
->firstblock
) <
3869 XFS_FSB_TO_AGNO(mp
, args
.fsbno
)));
3871 ap
->blkno
= args
.fsbno
;
3872 if (*ap
->firstblock
== NULLFSBLOCK
)
3873 *ap
->firstblock
= args
.fsbno
;
3874 ASSERT(nullfb
|| fb_agno
== args
.agno
||
3875 (ap
->flist
->xbf_low
&& fb_agno
< args
.agno
));
3876 ap
->length
= args
.len
;
3877 ap
->ip
->i_d
.di_nblocks
+= args
.len
;
3878 xfs_trans_log_inode(ap
->tp
, ap
->ip
, XFS_ILOG_CORE
);
3880 ap
->ip
->i_delayed_blks
-= args
.len
;
3882 * Adjust the disk quota also. This was reserved
3885 xfs_trans_mod_dquot_byino(ap
->tp
, ap
->ip
,
3886 ap
->wasdel
? XFS_TRANS_DQ_DELBCOUNT
:
3887 XFS_TRANS_DQ_BCOUNT
,
3890 ap
->blkno
= NULLFSBLOCK
;
3897 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
3898 * It figures out where to ask the underlying allocator to put the new extent.
3902 struct xfs_bmalloca
*ap
) /* bmap alloc argument struct */
3904 if (XFS_IS_REALTIME_INODE(ap
->ip
) && ap
->userdata
)
3905 return xfs_bmap_rtalloc(ap
);
3906 return xfs_bmap_btalloc(ap
);
3910 * Trim the returned map to the required bounds
3914 struct xfs_bmbt_irec
*mval
,
3915 struct xfs_bmbt_irec
*got
,
3923 if ((flags
& XFS_BMAPI_ENTIRE
) ||
3924 got
->br_startoff
+ got
->br_blockcount
<= obno
) {
3926 if (isnullstartblock(got
->br_startblock
))
3927 mval
->br_startblock
= DELAYSTARTBLOCK
;
3933 ASSERT((*bno
>= obno
) || (n
== 0));
3935 mval
->br_startoff
= *bno
;
3936 if (isnullstartblock(got
->br_startblock
))
3937 mval
->br_startblock
= DELAYSTARTBLOCK
;
3939 mval
->br_startblock
= got
->br_startblock
+
3940 (*bno
- got
->br_startoff
);
3942 * Return the minimum of what we got and what we asked for for
3943 * the length. We can use the len variable here because it is
3944 * modified below and we could have been there before coming
3945 * here if the first part of the allocation didn't overlap what
3948 mval
->br_blockcount
= XFS_FILBLKS_MIN(end
- *bno
,
3949 got
->br_blockcount
- (*bno
- got
->br_startoff
));
3950 mval
->br_state
= got
->br_state
;
3951 ASSERT(mval
->br_blockcount
<= len
);
3956 * Update and validate the extent map to return
3959 xfs_bmapi_update_map(
3960 struct xfs_bmbt_irec
**map
,
3968 xfs_bmbt_irec_t
*mval
= *map
;
3970 ASSERT((flags
& XFS_BMAPI_ENTIRE
) ||
3971 ((mval
->br_startoff
+ mval
->br_blockcount
) <= end
));
3972 ASSERT((flags
& XFS_BMAPI_ENTIRE
) || (mval
->br_blockcount
<= *len
) ||
3973 (mval
->br_startoff
< obno
));
3975 *bno
= mval
->br_startoff
+ mval
->br_blockcount
;
3977 if (*n
> 0 && mval
->br_startoff
== mval
[-1].br_startoff
) {
3978 /* update previous map with new information */
3979 ASSERT(mval
->br_startblock
== mval
[-1].br_startblock
);
3980 ASSERT(mval
->br_blockcount
> mval
[-1].br_blockcount
);
3981 ASSERT(mval
->br_state
== mval
[-1].br_state
);
3982 mval
[-1].br_blockcount
= mval
->br_blockcount
;
3983 mval
[-1].br_state
= mval
->br_state
;
3984 } else if (*n
> 0 && mval
->br_startblock
!= DELAYSTARTBLOCK
&&
3985 mval
[-1].br_startblock
!= DELAYSTARTBLOCK
&&
3986 mval
[-1].br_startblock
!= HOLESTARTBLOCK
&&
3987 mval
->br_startblock
== mval
[-1].br_startblock
+
3988 mval
[-1].br_blockcount
&&
3989 ((flags
& XFS_BMAPI_IGSTATE
) ||
3990 mval
[-1].br_state
== mval
->br_state
)) {
3991 ASSERT(mval
->br_startoff
==
3992 mval
[-1].br_startoff
+ mval
[-1].br_blockcount
);
3993 mval
[-1].br_blockcount
+= mval
->br_blockcount
;
3994 } else if (*n
> 0 &&
3995 mval
->br_startblock
== DELAYSTARTBLOCK
&&
3996 mval
[-1].br_startblock
== DELAYSTARTBLOCK
&&
3997 mval
->br_startoff
==
3998 mval
[-1].br_startoff
+ mval
[-1].br_blockcount
) {
3999 mval
[-1].br_blockcount
+= mval
->br_blockcount
;
4000 mval
[-1].br_state
= mval
->br_state
;
4001 } else if (!((*n
== 0) &&
4002 ((mval
->br_startoff
+ mval
->br_blockcount
) <=
4011 * Map file blocks to filesystem blocks without allocation.
4015 struct xfs_inode
*ip
,
4018 struct xfs_bmbt_irec
*mval
,
4022 struct xfs_mount
*mp
= ip
->i_mount
;
4023 struct xfs_ifork
*ifp
;
4024 struct xfs_bmbt_irec got
;
4025 struct xfs_bmbt_irec prev
;
4032 int whichfork
= (flags
& XFS_BMAPI_ATTRFORK
) ?
4033 XFS_ATTR_FORK
: XFS_DATA_FORK
;
4036 ASSERT(!(flags
& ~(XFS_BMAPI_ATTRFORK
|XFS_BMAPI_ENTIRE
|
4037 XFS_BMAPI_IGSTATE
)));
4038 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_SHARED
|XFS_ILOCK_EXCL
));
4040 if (unlikely(XFS_TEST_ERROR(
4041 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
4042 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
4043 mp
, XFS_ERRTAG_BMAPIFORMAT
, XFS_RANDOM_BMAPIFORMAT
))) {
4044 XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW
, mp
);
4045 return -EFSCORRUPTED
;
4048 if (XFS_FORCED_SHUTDOWN(mp
))
4051 XFS_STATS_INC(xs_blk_mapr
);
4053 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4055 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
4056 error
= xfs_iread_extents(NULL
, ip
, whichfork
);
4061 xfs_bmap_search_extents(ip
, bno
, whichfork
, &eof
, &lastx
, &got
, &prev
);
4065 while (bno
< end
&& n
< *nmap
) {
4066 /* Reading past eof, act as though there's a hole up to end. */
4068 got
.br_startoff
= end
;
4069 if (got
.br_startoff
> bno
) {
4070 /* Reading in a hole. */
4071 mval
->br_startoff
= bno
;
4072 mval
->br_startblock
= HOLESTARTBLOCK
;
4073 mval
->br_blockcount
=
4074 XFS_FILBLKS_MIN(len
, got
.br_startoff
- bno
);
4075 mval
->br_state
= XFS_EXT_NORM
;
4076 bno
+= mval
->br_blockcount
;
4077 len
-= mval
->br_blockcount
;
4083 /* set up the extent map to return. */
4084 xfs_bmapi_trim_map(mval
, &got
, &bno
, len
, obno
, end
, n
, flags
);
4085 xfs_bmapi_update_map(&mval
, &bno
, &len
, obno
, end
, &n
, flags
);
4087 /* If we're done, stop now. */
4088 if (bno
>= end
|| n
>= *nmap
)
4091 /* Else go on to the next record. */
4092 if (++lastx
< ifp
->if_bytes
/ sizeof(xfs_bmbt_rec_t
))
4093 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, lastx
), &got
);
4102 xfs_bmapi_reserve_delalloc(
4103 struct xfs_inode
*ip
,
4106 struct xfs_bmbt_irec
*got
,
4107 struct xfs_bmbt_irec
*prev
,
4108 xfs_extnum_t
*lastx
,
4111 struct xfs_mount
*mp
= ip
->i_mount
;
4112 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, XFS_DATA_FORK
);
4114 xfs_extlen_t indlen
;
4115 char rt
= XFS_IS_REALTIME_INODE(ip
);
4119 alen
= XFS_FILBLKS_MIN(len
, MAXEXTLEN
);
4121 alen
= XFS_FILBLKS_MIN(alen
, got
->br_startoff
- aoff
);
4123 /* Figure out the extent size, adjust alen */
4124 extsz
= xfs_get_extsz_hint(ip
);
4127 * Make sure we don't exceed a single extent length when we
4128 * align the extent by reducing length we are going to
4129 * allocate by the maximum amount extent size aligment may
4132 alen
= XFS_FILBLKS_MIN(len
, MAXEXTLEN
- (2 * extsz
- 1));
4133 error
= xfs_bmap_extsize_align(mp
, got
, prev
, extsz
, rt
, eof
,
4134 1, 0, &aoff
, &alen
);
4139 extsz
= alen
/ mp
->m_sb
.sb_rextsize
;
4142 * Make a transaction-less quota reservation for delayed allocation
4143 * blocks. This number gets adjusted later. We return if we haven't
4144 * allocated blocks already inside this loop.
4146 error
= xfs_trans_reserve_quota_nblks(NULL
, ip
, (long)alen
, 0,
4147 rt
? XFS_QMOPT_RES_RTBLKS
: XFS_QMOPT_RES_REGBLKS
);
4152 * Split changing sb for alen and indlen since they could be coming
4153 * from different places.
4155 indlen
= (xfs_extlen_t
)xfs_bmap_worst_indlen(ip
, alen
);
4159 error
= xfs_mod_incore_sb(mp
, XFS_SBS_FREXTENTS
,
4160 -((int64_t)extsz
), 0);
4162 error
= xfs_icsb_modify_counters(mp
, XFS_SBS_FDBLOCKS
,
4163 -((int64_t)alen
), 0);
4167 goto out_unreserve_quota
;
4169 error
= xfs_icsb_modify_counters(mp
, XFS_SBS_FDBLOCKS
,
4170 -((int64_t)indlen
), 0);
4172 goto out_unreserve_blocks
;
4175 ip
->i_delayed_blks
+= alen
;
4177 got
->br_startoff
= aoff
;
4178 got
->br_startblock
= nullstartblock(indlen
);
4179 got
->br_blockcount
= alen
;
4180 got
->br_state
= XFS_EXT_NORM
;
4181 xfs_bmap_add_extent_hole_delay(ip
, lastx
, got
);
4184 * Update our extent pointer, given that xfs_bmap_add_extent_hole_delay
4185 * might have merged it into one of the neighbouring ones.
4187 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, *lastx
), got
);
4189 ASSERT(got
->br_startoff
<= aoff
);
4190 ASSERT(got
->br_startoff
+ got
->br_blockcount
>= aoff
+ alen
);
4191 ASSERT(isnullstartblock(got
->br_startblock
));
4192 ASSERT(got
->br_state
== XFS_EXT_NORM
);
4195 out_unreserve_blocks
:
4197 xfs_mod_incore_sb(mp
, XFS_SBS_FREXTENTS
, extsz
, 0);
4199 xfs_icsb_modify_counters(mp
, XFS_SBS_FDBLOCKS
, alen
, 0);
4200 out_unreserve_quota
:
4201 if (XFS_IS_QUOTA_ON(mp
))
4202 xfs_trans_unreserve_quota_nblks(NULL
, ip
, (long)alen
, 0, rt
?
4203 XFS_QMOPT_RES_RTBLKS
: XFS_QMOPT_RES_REGBLKS
);
4208 * Map file blocks to filesystem blocks, adding delayed allocations as needed.
4212 struct xfs_inode
*ip
, /* incore inode */
4213 xfs_fileoff_t bno
, /* starting file offs. mapped */
4214 xfs_filblks_t len
, /* length to map in file */
4215 struct xfs_bmbt_irec
*mval
, /* output: map values */
4216 int *nmap
, /* i/o: mval size/count */
4217 int flags
) /* XFS_BMAPI_... */
4219 struct xfs_mount
*mp
= ip
->i_mount
;
4220 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, XFS_DATA_FORK
);
4221 struct xfs_bmbt_irec got
; /* current file extent record */
4222 struct xfs_bmbt_irec prev
; /* previous file extent record */
4223 xfs_fileoff_t obno
; /* old block number (offset) */
4224 xfs_fileoff_t end
; /* end of mapped file region */
4225 xfs_extnum_t lastx
; /* last useful extent number */
4226 int eof
; /* we've hit the end of extents */
4227 int n
= 0; /* current extent index */
4231 ASSERT(*nmap
<= XFS_BMAP_MAX_NMAP
);
4232 ASSERT(!(flags
& ~XFS_BMAPI_ENTIRE
));
4233 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
4235 if (unlikely(XFS_TEST_ERROR(
4236 (XFS_IFORK_FORMAT(ip
, XFS_DATA_FORK
) != XFS_DINODE_FMT_EXTENTS
&&
4237 XFS_IFORK_FORMAT(ip
, XFS_DATA_FORK
) != XFS_DINODE_FMT_BTREE
),
4238 mp
, XFS_ERRTAG_BMAPIFORMAT
, XFS_RANDOM_BMAPIFORMAT
))) {
4239 XFS_ERROR_REPORT("xfs_bmapi_delay", XFS_ERRLEVEL_LOW
, mp
);
4240 return -EFSCORRUPTED
;
4243 if (XFS_FORCED_SHUTDOWN(mp
))
4246 XFS_STATS_INC(xs_blk_mapw
);
4248 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
4249 error
= xfs_iread_extents(NULL
, ip
, XFS_DATA_FORK
);
4254 xfs_bmap_search_extents(ip
, bno
, XFS_DATA_FORK
, &eof
, &lastx
, &got
, &prev
);
4258 while (bno
< end
&& n
< *nmap
) {
4259 if (eof
|| got
.br_startoff
> bno
) {
4260 error
= xfs_bmapi_reserve_delalloc(ip
, bno
, len
, &got
,
4261 &prev
, &lastx
, eof
);
4271 /* set up the extent map to return. */
4272 xfs_bmapi_trim_map(mval
, &got
, &bno
, len
, obno
, end
, n
, flags
);
4273 xfs_bmapi_update_map(&mval
, &bno
, &len
, obno
, end
, &n
, flags
);
4275 /* If we're done, stop now. */
4276 if (bno
>= end
|| n
>= *nmap
)
4279 /* Else go on to the next record. */
4281 if (++lastx
< ifp
->if_bytes
/ sizeof(xfs_bmbt_rec_t
))
4282 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, lastx
), &got
);
4294 struct xfs_bmalloca
*bma
)
4296 struct xfs_mount
*mp
= bma
->ip
->i_mount
;
4297 int whichfork
= (bma
->flags
& XFS_BMAPI_ATTRFORK
) ?
4298 XFS_ATTR_FORK
: XFS_DATA_FORK
;
4299 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
4300 int tmp_logflags
= 0;
4303 ASSERT(bma
->length
> 0);
4306 * For the wasdelay case, we could also just allocate the stuff asked
4307 * for in this bmap call but that wouldn't be as good.
4310 bma
->length
= (xfs_extlen_t
)bma
->got
.br_blockcount
;
4311 bma
->offset
= bma
->got
.br_startoff
;
4312 if (bma
->idx
!= NULLEXTNUM
&& bma
->idx
) {
4313 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
- 1),
4317 bma
->length
= XFS_FILBLKS_MIN(bma
->length
, MAXEXTLEN
);
4319 bma
->length
= XFS_FILBLKS_MIN(bma
->length
,
4320 bma
->got
.br_startoff
- bma
->offset
);
4324 * Indicate if this is the first user data in the file, or just any
4327 if (!(bma
->flags
& XFS_BMAPI_METADATA
)) {
4328 bma
->userdata
= (bma
->offset
== 0) ?
4329 XFS_ALLOC_INITIAL_USER_DATA
: XFS_ALLOC_USERDATA
;
4332 bma
->minlen
= (bma
->flags
& XFS_BMAPI_CONTIG
) ? bma
->length
: 1;
4335 * Only want to do the alignment at the eof if it is userdata and
4336 * allocation length is larger than a stripe unit.
4338 if (mp
->m_dalign
&& bma
->length
>= mp
->m_dalign
&&
4339 !(bma
->flags
& XFS_BMAPI_METADATA
) && whichfork
== XFS_DATA_FORK
) {
4340 error
= xfs_bmap_isaeof(bma
, whichfork
);
4345 error
= xfs_bmap_alloc(bma
);
4349 if (bma
->flist
->xbf_low
)
4352 bma
->cur
->bc_private
.b
.firstblock
= *bma
->firstblock
;
4353 if (bma
->blkno
== NULLFSBLOCK
)
4355 if ((ifp
->if_flags
& XFS_IFBROOT
) && !bma
->cur
) {
4356 bma
->cur
= xfs_bmbt_init_cursor(mp
, bma
->tp
, bma
->ip
, whichfork
);
4357 bma
->cur
->bc_private
.b
.firstblock
= *bma
->firstblock
;
4358 bma
->cur
->bc_private
.b
.flist
= bma
->flist
;
4361 * Bump the number of extents we've allocated
4367 bma
->cur
->bc_private
.b
.flags
=
4368 bma
->wasdel
? XFS_BTCUR_BPRV_WASDEL
: 0;
4370 bma
->got
.br_startoff
= bma
->offset
;
4371 bma
->got
.br_startblock
= bma
->blkno
;
4372 bma
->got
.br_blockcount
= bma
->length
;
4373 bma
->got
.br_state
= XFS_EXT_NORM
;
4376 * A wasdelay extent has been initialized, so shouldn't be flagged
4379 if (!bma
->wasdel
&& (bma
->flags
& XFS_BMAPI_PREALLOC
) &&
4380 xfs_sb_version_hasextflgbit(&mp
->m_sb
))
4381 bma
->got
.br_state
= XFS_EXT_UNWRITTEN
;
4384 error
= xfs_bmap_add_extent_delay_real(bma
);
4386 error
= xfs_bmap_add_extent_hole_real(bma
, whichfork
);
4388 bma
->logflags
|= tmp_logflags
;
4393 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4394 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4395 * the neighbouring ones.
4397 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
), &bma
->got
);
4399 ASSERT(bma
->got
.br_startoff
<= bma
->offset
);
4400 ASSERT(bma
->got
.br_startoff
+ bma
->got
.br_blockcount
>=
4401 bma
->offset
+ bma
->length
);
4402 ASSERT(bma
->got
.br_state
== XFS_EXT_NORM
||
4403 bma
->got
.br_state
== XFS_EXT_UNWRITTEN
);
4408 xfs_bmapi_convert_unwritten(
4409 struct xfs_bmalloca
*bma
,
4410 struct xfs_bmbt_irec
*mval
,
4414 int whichfork
= (flags
& XFS_BMAPI_ATTRFORK
) ?
4415 XFS_ATTR_FORK
: XFS_DATA_FORK
;
4416 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
4417 int tmp_logflags
= 0;
4420 /* check if we need to do unwritten->real conversion */
4421 if (mval
->br_state
== XFS_EXT_UNWRITTEN
&&
4422 (flags
& XFS_BMAPI_PREALLOC
))
4425 /* check if we need to do real->unwritten conversion */
4426 if (mval
->br_state
== XFS_EXT_NORM
&&
4427 (flags
& (XFS_BMAPI_PREALLOC
| XFS_BMAPI_CONVERT
)) !=
4428 (XFS_BMAPI_PREALLOC
| XFS_BMAPI_CONVERT
))
4432 * Modify (by adding) the state flag, if writing.
4434 ASSERT(mval
->br_blockcount
<= len
);
4435 if ((ifp
->if_flags
& XFS_IFBROOT
) && !bma
->cur
) {
4436 bma
->cur
= xfs_bmbt_init_cursor(bma
->ip
->i_mount
, bma
->tp
,
4437 bma
->ip
, whichfork
);
4438 bma
->cur
->bc_private
.b
.firstblock
= *bma
->firstblock
;
4439 bma
->cur
->bc_private
.b
.flist
= bma
->flist
;
4441 mval
->br_state
= (mval
->br_state
== XFS_EXT_UNWRITTEN
)
4442 ? XFS_EXT_NORM
: XFS_EXT_UNWRITTEN
;
4444 error
= xfs_bmap_add_extent_unwritten_real(bma
->tp
, bma
->ip
, &bma
->idx
,
4445 &bma
->cur
, mval
, bma
->firstblock
, bma
->flist
,
4447 bma
->logflags
|= tmp_logflags
;
4452 * Update our extent pointer, given that
4453 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4454 * of the neighbouring ones.
4456 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
), &bma
->got
);
4459 * We may have combined previously unwritten space with written space,
4460 * so generate another request.
4462 if (mval
->br_blockcount
< len
)
4468 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4469 * extent state if necessary. Details behaviour is controlled by the flags
4470 * parameter. Only allocates blocks from a single allocation group, to avoid
4473 * The returned value in "firstblock" from the first call in a transaction
4474 * must be remembered and presented to subsequent calls in "firstblock".
4475 * An upper bound for the number of blocks to be allocated is supplied to
4476 * the first call in "total"; if no allocation group has that many free
4477 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4481 struct xfs_trans
*tp
, /* transaction pointer */
4482 struct xfs_inode
*ip
, /* incore inode */
4483 xfs_fileoff_t bno
, /* starting file offs. mapped */
4484 xfs_filblks_t len
, /* length to map in file */
4485 int flags
, /* XFS_BMAPI_... */
4486 xfs_fsblock_t
*firstblock
, /* first allocated block
4487 controls a.g. for allocs */
4488 xfs_extlen_t total
, /* total blocks needed */
4489 struct xfs_bmbt_irec
*mval
, /* output: map values */
4490 int *nmap
, /* i/o: mval size/count */
4491 struct xfs_bmap_free
*flist
) /* i/o: list extents to free */
4493 struct xfs_mount
*mp
= ip
->i_mount
;
4494 struct xfs_ifork
*ifp
;
4495 struct xfs_bmalloca bma
= { NULL
}; /* args for xfs_bmap_alloc */
4496 xfs_fileoff_t end
; /* end of mapped file region */
4497 int eof
; /* after the end of extents */
4498 int error
; /* error return */
4499 int n
; /* current extent index */
4500 xfs_fileoff_t obno
; /* old block number (offset) */
4501 int whichfork
; /* data or attr fork */
4502 char inhole
; /* current location is hole in file */
4503 char wasdelay
; /* old extent was delayed */
4506 xfs_fileoff_t orig_bno
; /* original block number value */
4507 int orig_flags
; /* original flags arg value */
4508 xfs_filblks_t orig_len
; /* original value of len arg */
4509 struct xfs_bmbt_irec
*orig_mval
; /* original value of mval */
4510 int orig_nmap
; /* original value of *nmap */
4518 whichfork
= (flags
& XFS_BMAPI_ATTRFORK
) ?
4519 XFS_ATTR_FORK
: XFS_DATA_FORK
;
4522 ASSERT(*nmap
<= XFS_BMAP_MAX_NMAP
);
4523 ASSERT(!(flags
& XFS_BMAPI_IGSTATE
));
4526 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_LOCAL
);
4527 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
4529 if (unlikely(XFS_TEST_ERROR(
4530 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
4531 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
4532 mp
, XFS_ERRTAG_BMAPIFORMAT
, XFS_RANDOM_BMAPIFORMAT
))) {
4533 XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW
, mp
);
4534 return -EFSCORRUPTED
;
4537 if (XFS_FORCED_SHUTDOWN(mp
))
4540 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4542 XFS_STATS_INC(xs_blk_mapw
);
4544 if (*firstblock
== NULLFSBLOCK
) {
4545 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
)
4546 bma
.minleft
= be16_to_cpu(ifp
->if_broot
->bb_level
) + 1;
4553 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
4554 error
= xfs_iread_extents(tp
, ip
, whichfork
);
4559 xfs_bmap_search_extents(ip
, bno
, whichfork
, &eof
, &bma
.idx
, &bma
.got
,
4570 bma
.firstblock
= firstblock
;
4572 while (bno
< end
&& n
< *nmap
) {
4573 inhole
= eof
|| bma
.got
.br_startoff
> bno
;
4574 wasdelay
= !inhole
&& isnullstartblock(bma
.got
.br_startblock
);
4577 * First, deal with the hole before the allocated space
4578 * that we found, if any.
4580 if (inhole
|| wasdelay
) {
4582 bma
.conv
= !!(flags
& XFS_BMAPI_CONVERT
);
4583 bma
.wasdel
= wasdelay
;
4588 * There's a 32/64 bit type mismatch between the
4589 * allocation length request (which can be 64 bits in
4590 * length) and the bma length request, which is
4591 * xfs_extlen_t and therefore 32 bits. Hence we have to
4592 * check for 32-bit overflows and handle them here.
4594 if (len
> (xfs_filblks_t
)MAXEXTLEN
)
4595 bma
.length
= MAXEXTLEN
;
4600 ASSERT(bma
.length
> 0);
4601 error
= xfs_bmapi_allocate(&bma
);
4604 if (bma
.blkno
== NULLFSBLOCK
)
4608 /* Deal with the allocated space we found. */
4609 xfs_bmapi_trim_map(mval
, &bma
.got
, &bno
, len
, obno
,
4612 /* Execute unwritten extent conversion if necessary */
4613 error
= xfs_bmapi_convert_unwritten(&bma
, mval
, len
, flags
);
4614 if (error
== -EAGAIN
)
4619 /* update the extent map to return */
4620 xfs_bmapi_update_map(&mval
, &bno
, &len
, obno
, end
, &n
, flags
);
4623 * If we're done, stop now. Stop when we've allocated
4624 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4625 * the transaction may get too big.
4627 if (bno
>= end
|| n
>= *nmap
|| bma
.nallocs
>= *nmap
)
4630 /* Else go on to the next record. */
4632 if (++bma
.idx
< ifp
->if_bytes
/ sizeof(xfs_bmbt_rec_t
)) {
4633 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
.idx
),
4641 * Transform from btree to extents, give it cur.
4643 if (xfs_bmap_wants_extents(ip
, whichfork
)) {
4644 int tmp_logflags
= 0;
4647 error
= xfs_bmap_btree_to_extents(tp
, ip
, bma
.cur
,
4648 &tmp_logflags
, whichfork
);
4649 bma
.logflags
|= tmp_logflags
;
4654 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
||
4655 XFS_IFORK_NEXTENTS(ip
, whichfork
) >
4656 XFS_IFORK_MAXEXT(ip
, whichfork
));
4660 * Log everything. Do this after conversion, there's no point in
4661 * logging the extent records if we've converted to btree format.
4663 if ((bma
.logflags
& xfs_ilog_fext(whichfork
)) &&
4664 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
4665 bma
.logflags
&= ~xfs_ilog_fext(whichfork
);
4666 else if ((bma
.logflags
& xfs_ilog_fbroot(whichfork
)) &&
4667 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)
4668 bma
.logflags
&= ~xfs_ilog_fbroot(whichfork
);
4670 * Log whatever the flags say, even if error. Otherwise we might miss
4671 * detecting a case where the data is changed, there's an error,
4672 * and it's not logged so we don't shutdown when we should.
4675 xfs_trans_log_inode(tp
, ip
, bma
.logflags
);
4679 ASSERT(*firstblock
== NULLFSBLOCK
||
4680 XFS_FSB_TO_AGNO(mp
, *firstblock
) ==
4682 bma
.cur
->bc_private
.b
.firstblock
) ||
4684 XFS_FSB_TO_AGNO(mp
, *firstblock
) <
4686 bma
.cur
->bc_private
.b
.firstblock
)));
4687 *firstblock
= bma
.cur
->bc_private
.b
.firstblock
;
4689 xfs_btree_del_cursor(bma
.cur
,
4690 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
4693 xfs_bmap_validate_ret(orig_bno
, orig_len
, orig_flags
, orig_mval
,
4699 * Called by xfs_bmapi to update file extent records and the btree
4700 * after removing space (or undoing a delayed allocation).
4702 STATIC
int /* error */
4703 xfs_bmap_del_extent(
4704 xfs_inode_t
*ip
, /* incore inode pointer */
4705 xfs_trans_t
*tp
, /* current transaction pointer */
4706 xfs_extnum_t
*idx
, /* extent number to update/delete */
4707 xfs_bmap_free_t
*flist
, /* list of extents to be freed */
4708 xfs_btree_cur_t
*cur
, /* if null, not a btree */
4709 xfs_bmbt_irec_t
*del
, /* data to remove from extents */
4710 int *logflagsp
, /* inode logging flags */
4711 int whichfork
) /* data or attr fork */
4713 xfs_filblks_t da_new
; /* new delay-alloc indirect blocks */
4714 xfs_filblks_t da_old
; /* old delay-alloc indirect blocks */
4715 xfs_fsblock_t del_endblock
=0; /* first block past del */
4716 xfs_fileoff_t del_endoff
; /* first offset past del */
4717 int delay
; /* current block is delayed allocated */
4718 int do_fx
; /* free extent at end of routine */
4719 xfs_bmbt_rec_host_t
*ep
; /* current extent entry pointer */
4720 int error
; /* error return value */
4721 int flags
; /* inode logging flags */
4722 xfs_bmbt_irec_t got
; /* current extent entry */
4723 xfs_fileoff_t got_endoff
; /* first offset past got */
4724 int i
; /* temp state */
4725 xfs_ifork_t
*ifp
; /* inode fork pointer */
4726 xfs_mount_t
*mp
; /* mount structure */
4727 xfs_filblks_t nblks
; /* quota/sb block count */
4728 xfs_bmbt_irec_t
new; /* new record to be inserted */
4730 uint qfield
; /* quota field to update */
4731 xfs_filblks_t temp
; /* for indirect length calculations */
4732 xfs_filblks_t temp2
; /* for indirect length calculations */
4735 XFS_STATS_INC(xs_del_exlist
);
4737 if (whichfork
== XFS_ATTR_FORK
)
4738 state
|= BMAP_ATTRFORK
;
4741 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4742 ASSERT((*idx
>= 0) && (*idx
< ifp
->if_bytes
/
4743 (uint
)sizeof(xfs_bmbt_rec_t
)));
4744 ASSERT(del
->br_blockcount
> 0);
4745 ep
= xfs_iext_get_ext(ifp
, *idx
);
4746 xfs_bmbt_get_all(ep
, &got
);
4747 ASSERT(got
.br_startoff
<= del
->br_startoff
);
4748 del_endoff
= del
->br_startoff
+ del
->br_blockcount
;
4749 got_endoff
= got
.br_startoff
+ got
.br_blockcount
;
4750 ASSERT(got_endoff
>= del_endoff
);
4751 delay
= isnullstartblock(got
.br_startblock
);
4752 ASSERT(isnullstartblock(del
->br_startblock
) == delay
);
4757 * If deleting a real allocation, must free up the disk space.
4760 flags
= XFS_ILOG_CORE
;
4762 * Realtime allocation. Free it and record di_nblocks update.
4764 if (whichfork
== XFS_DATA_FORK
&& XFS_IS_REALTIME_INODE(ip
)) {
4768 ASSERT(do_mod(del
->br_blockcount
,
4769 mp
->m_sb
.sb_rextsize
) == 0);
4770 ASSERT(do_mod(del
->br_startblock
,
4771 mp
->m_sb
.sb_rextsize
) == 0);
4772 bno
= del
->br_startblock
;
4773 len
= del
->br_blockcount
;
4774 do_div(bno
, mp
->m_sb
.sb_rextsize
);
4775 do_div(len
, mp
->m_sb
.sb_rextsize
);
4776 error
= xfs_rtfree_extent(tp
, bno
, (xfs_extlen_t
)len
);
4780 nblks
= len
* mp
->m_sb
.sb_rextsize
;
4781 qfield
= XFS_TRANS_DQ_RTBCOUNT
;
4784 * Ordinary allocation.
4788 nblks
= del
->br_blockcount
;
4789 qfield
= XFS_TRANS_DQ_BCOUNT
;
4792 * Set up del_endblock and cur for later.
4794 del_endblock
= del
->br_startblock
+ del
->br_blockcount
;
4796 if ((error
= xfs_bmbt_lookup_eq(cur
, got
.br_startoff
,
4797 got
.br_startblock
, got
.br_blockcount
,
4800 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
4802 da_old
= da_new
= 0;
4804 da_old
= startblockval(got
.br_startblock
);
4810 * Set flag value to use in switch statement.
4811 * Left-contig is 2, right-contig is 1.
4813 switch (((got
.br_startoff
== del
->br_startoff
) << 1) |
4814 (got_endoff
== del_endoff
)) {
4817 * Matches the whole extent. Delete the entry.
4819 xfs_iext_remove(ip
, *idx
, 1,
4820 whichfork
== XFS_ATTR_FORK
? BMAP_ATTRFORK
: 0);
4825 XFS_IFORK_NEXT_SET(ip
, whichfork
,
4826 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
4827 flags
|= XFS_ILOG_CORE
;
4829 flags
|= xfs_ilog_fext(whichfork
);
4832 if ((error
= xfs_btree_delete(cur
, &i
)))
4834 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
4839 * Deleting the first part of the extent.
4841 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
4842 xfs_bmbt_set_startoff(ep
, del_endoff
);
4843 temp
= got
.br_blockcount
- del
->br_blockcount
;
4844 xfs_bmbt_set_blockcount(ep
, temp
);
4846 temp
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
4848 xfs_bmbt_set_startblock(ep
, nullstartblock((int)temp
));
4849 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
4853 xfs_bmbt_set_startblock(ep
, del_endblock
);
4854 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
4856 flags
|= xfs_ilog_fext(whichfork
);
4859 if ((error
= xfs_bmbt_update(cur
, del_endoff
, del_endblock
,
4860 got
.br_blockcount
- del
->br_blockcount
,
4867 * Deleting the last part of the extent.
4869 temp
= got
.br_blockcount
- del
->br_blockcount
;
4870 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
4871 xfs_bmbt_set_blockcount(ep
, temp
);
4873 temp
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
4875 xfs_bmbt_set_startblock(ep
, nullstartblock((int)temp
));
4876 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
4880 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
4882 flags
|= xfs_ilog_fext(whichfork
);
4885 if ((error
= xfs_bmbt_update(cur
, got
.br_startoff
,
4887 got
.br_blockcount
- del
->br_blockcount
,
4894 * Deleting the middle of the extent.
4896 temp
= del
->br_startoff
- got
.br_startoff
;
4897 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
4898 xfs_bmbt_set_blockcount(ep
, temp
);
4899 new.br_startoff
= del_endoff
;
4900 temp2
= got_endoff
- del_endoff
;
4901 new.br_blockcount
= temp2
;
4902 new.br_state
= got
.br_state
;
4904 new.br_startblock
= del_endblock
;
4905 flags
|= XFS_ILOG_CORE
;
4907 if ((error
= xfs_bmbt_update(cur
,
4909 got
.br_startblock
, temp
,
4912 if ((error
= xfs_btree_increment(cur
, 0, &i
)))
4914 cur
->bc_rec
.b
= new;
4915 error
= xfs_btree_insert(cur
, &i
);
4916 if (error
&& error
!= -ENOSPC
)
4919 * If get no-space back from btree insert,
4920 * it tried a split, and we have a zero
4921 * block reservation.
4922 * Fix up our state and return the error.
4924 if (error
== -ENOSPC
) {
4926 * Reset the cursor, don't trust
4927 * it after any insert operation.
4929 if ((error
= xfs_bmbt_lookup_eq(cur
,
4934 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
4936 * Update the btree record back
4937 * to the original value.
4939 if ((error
= xfs_bmbt_update(cur
,
4946 * Reset the extent record back
4947 * to the original value.
4949 xfs_bmbt_set_blockcount(ep
,
4955 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
4957 flags
|= xfs_ilog_fext(whichfork
);
4958 XFS_IFORK_NEXT_SET(ip
, whichfork
,
4959 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
4961 ASSERT(whichfork
== XFS_DATA_FORK
);
4962 temp
= xfs_bmap_worst_indlen(ip
, temp
);
4963 xfs_bmbt_set_startblock(ep
, nullstartblock((int)temp
));
4964 temp2
= xfs_bmap_worst_indlen(ip
, temp2
);
4965 new.br_startblock
= nullstartblock((int)temp2
);
4966 da_new
= temp
+ temp2
;
4967 while (da_new
> da_old
) {
4971 xfs_bmbt_set_startblock(ep
,
4972 nullstartblock((int)temp
));
4974 if (da_new
== da_old
)
4980 nullstartblock((int)temp2
);
4984 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
4985 xfs_iext_insert(ip
, *idx
+ 1, 1, &new, state
);
4990 * If we need to, add to list of extents to delete.
4993 xfs_bmap_add_free(del
->br_startblock
, del
->br_blockcount
, flist
,
4996 * Adjust inode # blocks in the file.
4999 ip
->i_d
.di_nblocks
-= nblks
;
5001 * Adjust quota data.
5004 xfs_trans_mod_dquot_byino(tp
, ip
, qfield
, (long)-nblks
);
5007 * Account for change in delayed indirect blocks.
5008 * Nothing to do for disk quota accounting here.
5010 ASSERT(da_old
>= da_new
);
5011 if (da_old
> da_new
) {
5012 xfs_icsb_modify_counters(mp
, XFS_SBS_FDBLOCKS
,
5013 (int64_t)(da_old
- da_new
), 0);
5021 * Unmap (remove) blocks from a file.
5022 * If nexts is nonzero then the number of extents to remove is limited to
5023 * that value. If not all extents in the block range can be removed then
5028 xfs_trans_t
*tp
, /* transaction pointer */
5029 struct xfs_inode
*ip
, /* incore inode */
5030 xfs_fileoff_t bno
, /* starting offset to unmap */
5031 xfs_filblks_t len
, /* length to unmap in file */
5032 int flags
, /* misc flags */
5033 xfs_extnum_t nexts
, /* number of extents max */
5034 xfs_fsblock_t
*firstblock
, /* first allocated block
5035 controls a.g. for allocs */
5036 xfs_bmap_free_t
*flist
, /* i/o: list extents to free */
5037 int *done
) /* set if not done yet */
5039 xfs_btree_cur_t
*cur
; /* bmap btree cursor */
5040 xfs_bmbt_irec_t del
; /* extent being deleted */
5041 int eof
; /* is deleting at eof */
5042 xfs_bmbt_rec_host_t
*ep
; /* extent record pointer */
5043 int error
; /* error return value */
5044 xfs_extnum_t extno
; /* extent number in list */
5045 xfs_bmbt_irec_t got
; /* current extent record */
5046 xfs_ifork_t
*ifp
; /* inode fork pointer */
5047 int isrt
; /* freeing in rt area */
5048 xfs_extnum_t lastx
; /* last extent index used */
5049 int logflags
; /* transaction logging flags */
5050 xfs_extlen_t mod
; /* rt extent offset */
5051 xfs_mount_t
*mp
; /* mount structure */
5052 xfs_extnum_t nextents
; /* number of file extents */
5053 xfs_bmbt_irec_t prev
; /* previous extent record */
5054 xfs_fileoff_t start
; /* first file offset deleted */
5055 int tmp_logflags
; /* partial logging flags */
5056 int wasdel
; /* was a delayed alloc extent */
5057 int whichfork
; /* data or attribute fork */
5060 trace_xfs_bunmap(ip
, bno
, len
, flags
, _RET_IP_
);
5062 whichfork
= (flags
& XFS_BMAPI_ATTRFORK
) ?
5063 XFS_ATTR_FORK
: XFS_DATA_FORK
;
5064 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5066 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
5067 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)) {
5068 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW
,
5070 return -EFSCORRUPTED
;
5073 if (XFS_FORCED_SHUTDOWN(mp
))
5076 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
5080 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
5081 (error
= xfs_iread_extents(tp
, ip
, whichfork
)))
5083 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
5084 if (nextents
== 0) {
5088 XFS_STATS_INC(xs_blk_unmap
);
5089 isrt
= (whichfork
== XFS_DATA_FORK
) && XFS_IS_REALTIME_INODE(ip
);
5091 bno
= start
+ len
- 1;
5092 ep
= xfs_bmap_search_extents(ip
, bno
, whichfork
, &eof
, &lastx
, &got
,
5096 * Check to see if the given block number is past the end of the
5097 * file, back up to the last block if so...
5100 ep
= xfs_iext_get_ext(ifp
, --lastx
);
5101 xfs_bmbt_get_all(ep
, &got
);
5102 bno
= got
.br_startoff
+ got
.br_blockcount
- 1;
5105 if (ifp
->if_flags
& XFS_IFBROOT
) {
5106 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
);
5107 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
5108 cur
->bc_private
.b
.firstblock
= *firstblock
;
5109 cur
->bc_private
.b
.flist
= flist
;
5110 cur
->bc_private
.b
.flags
= 0;
5116 * Synchronize by locking the bitmap inode.
5118 xfs_ilock(mp
->m_rbmip
, XFS_ILOCK_EXCL
);
5119 xfs_trans_ijoin(tp
, mp
->m_rbmip
, XFS_ILOCK_EXCL
);
5123 while (bno
!= (xfs_fileoff_t
)-1 && bno
>= start
&& lastx
>= 0 &&
5124 (nexts
== 0 || extno
< nexts
)) {
5126 * Is the found extent after a hole in which bno lives?
5127 * Just back up to the previous extent, if so.
5129 if (got
.br_startoff
> bno
) {
5132 ep
= xfs_iext_get_ext(ifp
, lastx
);
5133 xfs_bmbt_get_all(ep
, &got
);
5136 * Is the last block of this extent before the range
5137 * we're supposed to delete? If so, we're done.
5139 bno
= XFS_FILEOFF_MIN(bno
,
5140 got
.br_startoff
+ got
.br_blockcount
- 1);
5144 * Then deal with the (possibly delayed) allocated space
5149 wasdel
= isnullstartblock(del
.br_startblock
);
5150 if (got
.br_startoff
< start
) {
5151 del
.br_startoff
= start
;
5152 del
.br_blockcount
-= start
- got
.br_startoff
;
5154 del
.br_startblock
+= start
- got
.br_startoff
;
5156 if (del
.br_startoff
+ del
.br_blockcount
> bno
+ 1)
5157 del
.br_blockcount
= bno
+ 1 - del
.br_startoff
;
5158 sum
= del
.br_startblock
+ del
.br_blockcount
;
5160 (mod
= do_mod(sum
, mp
->m_sb
.sb_rextsize
))) {
5162 * Realtime extent not lined up at the end.
5163 * The extent could have been split into written
5164 * and unwritten pieces, or we could just be
5165 * unmapping part of it. But we can't really
5166 * get rid of part of a realtime extent.
5168 if (del
.br_state
== XFS_EXT_UNWRITTEN
||
5169 !xfs_sb_version_hasextflgbit(&mp
->m_sb
)) {
5171 * This piece is unwritten, or we're not
5172 * using unwritten extents. Skip over it.
5175 bno
-= mod
> del
.br_blockcount
?
5176 del
.br_blockcount
: mod
;
5177 if (bno
< got
.br_startoff
) {
5179 xfs_bmbt_get_all(xfs_iext_get_ext(
5185 * It's written, turn it unwritten.
5186 * This is better than zeroing it.
5188 ASSERT(del
.br_state
== XFS_EXT_NORM
);
5189 ASSERT(xfs_trans_get_block_res(tp
) > 0);
5191 * If this spans a realtime extent boundary,
5192 * chop it back to the start of the one we end at.
5194 if (del
.br_blockcount
> mod
) {
5195 del
.br_startoff
+= del
.br_blockcount
- mod
;
5196 del
.br_startblock
+= del
.br_blockcount
- mod
;
5197 del
.br_blockcount
= mod
;
5199 del
.br_state
= XFS_EXT_UNWRITTEN
;
5200 error
= xfs_bmap_add_extent_unwritten_real(tp
, ip
,
5201 &lastx
, &cur
, &del
, firstblock
, flist
,
5207 if (isrt
&& (mod
= do_mod(del
.br_startblock
, mp
->m_sb
.sb_rextsize
))) {
5209 * Realtime extent is lined up at the end but not
5210 * at the front. We'll get rid of full extents if
5213 mod
= mp
->m_sb
.sb_rextsize
- mod
;
5214 if (del
.br_blockcount
> mod
) {
5215 del
.br_blockcount
-= mod
;
5216 del
.br_startoff
+= mod
;
5217 del
.br_startblock
+= mod
;
5218 } else if ((del
.br_startoff
== start
&&
5219 (del
.br_state
== XFS_EXT_UNWRITTEN
||
5220 xfs_trans_get_block_res(tp
) == 0)) ||
5221 !xfs_sb_version_hasextflgbit(&mp
->m_sb
)) {
5223 * Can't make it unwritten. There isn't
5224 * a full extent here so just skip it.
5226 ASSERT(bno
>= del
.br_blockcount
);
5227 bno
-= del
.br_blockcount
;
5228 if (got
.br_startoff
> bno
) {
5230 ep
= xfs_iext_get_ext(ifp
,
5232 xfs_bmbt_get_all(ep
, &got
);
5236 } else if (del
.br_state
== XFS_EXT_UNWRITTEN
) {
5238 * This one is already unwritten.
5239 * It must have a written left neighbor.
5240 * Unwrite the killed part of that one and
5244 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
,
5246 ASSERT(prev
.br_state
== XFS_EXT_NORM
);
5247 ASSERT(!isnullstartblock(prev
.br_startblock
));
5248 ASSERT(del
.br_startblock
==
5249 prev
.br_startblock
+ prev
.br_blockcount
);
5250 if (prev
.br_startoff
< start
) {
5251 mod
= start
- prev
.br_startoff
;
5252 prev
.br_blockcount
-= mod
;
5253 prev
.br_startblock
+= mod
;
5254 prev
.br_startoff
= start
;
5256 prev
.br_state
= XFS_EXT_UNWRITTEN
;
5258 error
= xfs_bmap_add_extent_unwritten_real(tp
,
5259 ip
, &lastx
, &cur
, &prev
,
5260 firstblock
, flist
, &logflags
);
5265 ASSERT(del
.br_state
== XFS_EXT_NORM
);
5266 del
.br_state
= XFS_EXT_UNWRITTEN
;
5267 error
= xfs_bmap_add_extent_unwritten_real(tp
,
5268 ip
, &lastx
, &cur
, &del
,
5269 firstblock
, flist
, &logflags
);
5276 ASSERT(startblockval(del
.br_startblock
) > 0);
5277 /* Update realtime/data freespace, unreserve quota */
5279 xfs_filblks_t rtexts
;
5281 rtexts
= XFS_FSB_TO_B(mp
, del
.br_blockcount
);
5282 do_div(rtexts
, mp
->m_sb
.sb_rextsize
);
5283 xfs_mod_incore_sb(mp
, XFS_SBS_FREXTENTS
,
5284 (int64_t)rtexts
, 0);
5285 (void)xfs_trans_reserve_quota_nblks(NULL
,
5286 ip
, -((long)del
.br_blockcount
), 0,
5287 XFS_QMOPT_RES_RTBLKS
);
5289 xfs_icsb_modify_counters(mp
, XFS_SBS_FDBLOCKS
,
5290 (int64_t)del
.br_blockcount
, 0);
5291 (void)xfs_trans_reserve_quota_nblks(NULL
,
5292 ip
, -((long)del
.br_blockcount
), 0,
5293 XFS_QMOPT_RES_REGBLKS
);
5295 ip
->i_delayed_blks
-= del
.br_blockcount
;
5297 cur
->bc_private
.b
.flags
|=
5298 XFS_BTCUR_BPRV_WASDEL
;
5300 cur
->bc_private
.b
.flags
&= ~XFS_BTCUR_BPRV_WASDEL
;
5302 * If it's the case where the directory code is running
5303 * with no block reservation, and the deleted block is in
5304 * the middle of its extent, and the resulting insert
5305 * of an extent would cause transformation to btree format,
5306 * then reject it. The calling code will then swap
5307 * blocks around instead.
5308 * We have to do this now, rather than waiting for the
5309 * conversion to btree format, since the transaction
5312 if (!wasdel
&& xfs_trans_get_block_res(tp
) == 0 &&
5313 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
&&
5314 XFS_IFORK_NEXTENTS(ip
, whichfork
) >= /* Note the >= */
5315 XFS_IFORK_MAXEXT(ip
, whichfork
) &&
5316 del
.br_startoff
> got
.br_startoff
&&
5317 del
.br_startoff
+ del
.br_blockcount
<
5318 got
.br_startoff
+ got
.br_blockcount
) {
5322 error
= xfs_bmap_del_extent(ip
, tp
, &lastx
, flist
, cur
, &del
,
5323 &tmp_logflags
, whichfork
);
5324 logflags
|= tmp_logflags
;
5327 bno
= del
.br_startoff
- 1;
5330 * If not done go on to the next (previous) record.
5332 if (bno
!= (xfs_fileoff_t
)-1 && bno
>= start
) {
5334 ep
= xfs_iext_get_ext(ifp
, lastx
);
5335 if (xfs_bmbt_get_startoff(ep
) > bno
) {
5337 ep
= xfs_iext_get_ext(ifp
,
5340 xfs_bmbt_get_all(ep
, &got
);
5345 *done
= bno
== (xfs_fileoff_t
)-1 || bno
< start
|| lastx
< 0;
5348 * Convert to a btree if necessary.
5350 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
5351 ASSERT(cur
== NULL
);
5352 error
= xfs_bmap_extents_to_btree(tp
, ip
, firstblock
, flist
,
5353 &cur
, 0, &tmp_logflags
, whichfork
);
5354 logflags
|= tmp_logflags
;
5359 * transform from btree to extents, give it cur
5361 else if (xfs_bmap_wants_extents(ip
, whichfork
)) {
5362 ASSERT(cur
!= NULL
);
5363 error
= xfs_bmap_btree_to_extents(tp
, ip
, cur
, &tmp_logflags
,
5365 logflags
|= tmp_logflags
;
5370 * transform from extents to local?
5375 * Log everything. Do this after conversion, there's no point in
5376 * logging the extent records if we've converted to btree format.
5378 if ((logflags
& xfs_ilog_fext(whichfork
)) &&
5379 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
5380 logflags
&= ~xfs_ilog_fext(whichfork
);
5381 else if ((logflags
& xfs_ilog_fbroot(whichfork
)) &&
5382 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)
5383 logflags
&= ~xfs_ilog_fbroot(whichfork
);
5385 * Log inode even in the error case, if the transaction
5386 * is dirty we'll need to shut down the filesystem.
5389 xfs_trans_log_inode(tp
, ip
, logflags
);
5392 *firstblock
= cur
->bc_private
.b
.firstblock
;
5393 cur
->bc_private
.b
.allocated
= 0;
5395 xfs_btree_del_cursor(cur
,
5396 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
5402 * Determine whether an extent shift can be accomplished by a merge with the
5403 * extent that precedes the target hole of the shift.
5407 struct xfs_bmbt_irec
*left
, /* preceding extent */
5408 struct xfs_bmbt_irec
*got
, /* current extent to shift */
5409 xfs_fileoff_t shift
) /* shift fsb */
5411 xfs_fileoff_t startoff
;
5413 startoff
= got
->br_startoff
- shift
;
5416 * The extent, once shifted, must be adjacent in-file and on-disk with
5417 * the preceding extent.
5419 if ((left
->br_startoff
+ left
->br_blockcount
!= startoff
) ||
5420 (left
->br_startblock
+ left
->br_blockcount
!= got
->br_startblock
) ||
5421 (left
->br_state
!= got
->br_state
) ||
5422 (left
->br_blockcount
+ got
->br_blockcount
> MAXEXTLEN
))
5429 * A bmap extent shift adjusts the file offset of an extent to fill a preceding
5430 * hole in the file. If an extent shift would result in the extent being fully
5431 * adjacent to the extent that currently precedes the hole, we can merge with
5432 * the preceding extent rather than do the shift.
5434 * This function assumes the caller has verified a shift-by-merge is possible
5435 * with the provided extents via xfs_bmse_can_merge().
5439 struct xfs_inode
*ip
,
5441 xfs_fileoff_t shift
, /* shift fsb */
5442 int current_ext
, /* idx of gotp */
5443 struct xfs_bmbt_rec_host
*gotp
, /* extent to shift */
5444 struct xfs_bmbt_rec_host
*leftp
, /* preceding extent */
5445 struct xfs_btree_cur
*cur
,
5446 int *logflags
) /* output */
5448 struct xfs_bmbt_irec got
;
5449 struct xfs_bmbt_irec left
;
5450 xfs_filblks_t blockcount
;
5453 xfs_bmbt_get_all(gotp
, &got
);
5454 xfs_bmbt_get_all(leftp
, &left
);
5455 blockcount
= left
.br_blockcount
+ got
.br_blockcount
;
5457 ASSERT(xfs_isilocked(ip
, XFS_IOLOCK_EXCL
));
5458 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
5459 ASSERT(xfs_bmse_can_merge(&left
, &got
, shift
));
5462 * Merge the in-core extents. Note that the host record pointers and
5463 * current_ext index are invalid once the extent has been removed via
5464 * xfs_iext_remove().
5466 xfs_bmbt_set_blockcount(leftp
, blockcount
);
5467 xfs_iext_remove(ip
, current_ext
, 1, 0);
5470 * Update the on-disk extent count, the btree if necessary and log the
5473 XFS_IFORK_NEXT_SET(ip
, whichfork
,
5474 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
5475 *logflags
|= XFS_ILOG_CORE
;
5477 *logflags
|= XFS_ILOG_DEXT
;
5481 /* lookup and remove the extent to merge */
5482 error
= xfs_bmbt_lookup_eq(cur
, got
.br_startoff
, got
.br_startblock
,
5483 got
.br_blockcount
, &i
);
5486 XFS_WANT_CORRUPTED_RETURN(i
== 1);
5488 error
= xfs_btree_delete(cur
, &i
);
5491 XFS_WANT_CORRUPTED_RETURN(i
== 1);
5493 /* lookup and update size of the previous extent */
5494 error
= xfs_bmbt_lookup_eq(cur
, left
.br_startoff
, left
.br_startblock
,
5495 left
.br_blockcount
, &i
);
5498 XFS_WANT_CORRUPTED_RETURN(i
== 1);
5500 left
.br_blockcount
= blockcount
;
5502 return xfs_bmbt_update(cur
, left
.br_startoff
, left
.br_startblock
,
5503 left
.br_blockcount
, left
.br_state
);
5507 * Shift a single extent.
5511 struct xfs_inode
*ip
,
5513 xfs_fileoff_t offset_shift_fsb
,
5515 struct xfs_bmbt_rec_host
*gotp
,
5516 struct xfs_btree_cur
*cur
,
5519 struct xfs_ifork
*ifp
;
5520 xfs_fileoff_t startoff
;
5521 struct xfs_bmbt_rec_host
*leftp
;
5522 struct xfs_bmbt_irec got
;
5523 struct xfs_bmbt_irec left
;
5527 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5529 xfs_bmbt_get_all(gotp
, &got
);
5530 startoff
= got
.br_startoff
- offset_shift_fsb
;
5532 /* delalloc extents should be prevented by caller */
5533 XFS_WANT_CORRUPTED_RETURN(!isnullstartblock(got
.br_startblock
));
5536 * Check for merge if we've got an extent to the left, otherwise make
5537 * sure there's enough room at the start of the file for the shift.
5540 /* grab the left extent and check for a large enough hole */
5541 leftp
= xfs_iext_get_ext(ifp
, *current_ext
- 1);
5542 xfs_bmbt_get_all(leftp
, &left
);
5544 if (startoff
< left
.br_startoff
+ left
.br_blockcount
)
5547 /* check whether to merge the extent or shift it down */
5548 if (xfs_bmse_can_merge(&left
, &got
, offset_shift_fsb
)) {
5549 return xfs_bmse_merge(ip
, whichfork
, offset_shift_fsb
,
5550 *current_ext
, gotp
, leftp
, cur
,
5553 } else if (got
.br_startoff
< offset_shift_fsb
)
5557 * Increment the extent index for the next iteration, update the start
5558 * offset of the in-core extent and update the btree if applicable.
5561 xfs_bmbt_set_startoff(gotp
, startoff
);
5562 *logflags
|= XFS_ILOG_CORE
;
5564 *logflags
|= XFS_ILOG_DEXT
;
5568 error
= xfs_bmbt_lookup_eq(cur
, got
.br_startoff
, got
.br_startblock
,
5569 got
.br_blockcount
, &i
);
5572 XFS_WANT_CORRUPTED_RETURN(i
== 1);
5574 got
.br_startoff
= startoff
;
5575 return xfs_bmbt_update(cur
, got
.br_startoff
, got
.br_startblock
,
5576 got
.br_blockcount
, got
.br_state
);
5580 * Shift extent records to the left to cover a hole.
5582 * The maximum number of extents to be shifted in a single operation is
5583 * @num_exts. @start_fsb specifies the file offset to start the shift and the
5584 * file offset where we've left off is returned in @next_fsb. @offset_shift_fsb
5585 * is the length by which each extent is shifted. If there is no hole to shift
5586 * the extents into, this will be considered invalid operation and we abort
5590 xfs_bmap_shift_extents(
5591 struct xfs_trans
*tp
,
5592 struct xfs_inode
*ip
,
5593 xfs_fileoff_t start_fsb
,
5594 xfs_fileoff_t offset_shift_fsb
,
5596 xfs_fileoff_t
*next_fsb
,
5597 xfs_fsblock_t
*firstblock
,
5598 struct xfs_bmap_free
*flist
,
5601 struct xfs_btree_cur
*cur
= NULL
;
5602 struct xfs_bmbt_rec_host
*gotp
;
5603 struct xfs_bmbt_irec got
;
5604 struct xfs_mount
*mp
= ip
->i_mount
;
5605 struct xfs_ifork
*ifp
;
5606 xfs_extnum_t nexts
= 0;
5607 xfs_extnum_t current_ext
;
5609 int whichfork
= XFS_DATA_FORK
;
5613 if (unlikely(XFS_TEST_ERROR(
5614 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
5615 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
5616 mp
, XFS_ERRTAG_BMAPIFORMAT
, XFS_RANDOM_BMAPIFORMAT
))) {
5617 XFS_ERROR_REPORT("xfs_bmap_shift_extents",
5618 XFS_ERRLEVEL_LOW
, mp
);
5619 return -EFSCORRUPTED
;
5622 if (XFS_FORCED_SHUTDOWN(mp
))
5625 ASSERT(xfs_isilocked(ip
, XFS_IOLOCK_EXCL
));
5626 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
5628 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5629 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
5630 /* Read in all the extents */
5631 error
= xfs_iread_extents(tp
, ip
, whichfork
);
5636 if (ifp
->if_flags
& XFS_IFBROOT
) {
5637 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
5638 cur
->bc_private
.b
.firstblock
= *firstblock
;
5639 cur
->bc_private
.b
.flist
= flist
;
5640 cur
->bc_private
.b
.flags
= 0;
5644 * Look up the extent index for the fsb where we start shifting. We can
5645 * henceforth iterate with current_ext as extent list changes are locked
5648 * gotp can be null in 2 cases: 1) if there are no extents or 2)
5649 * start_fsb lies in a hole beyond which there are no extents. Either
5652 gotp
= xfs_iext_bno_to_ext(ifp
, start_fsb
, ¤t_ext
);
5659 * There may be delalloc extents in the data fork before the range we
5660 * are collapsing out, so we cannot use the count of real extents here.
5661 * Instead we have to calculate it from the incore fork.
5663 total_extents
= ifp
->if_bytes
/ sizeof(xfs_bmbt_rec_t
);
5664 while (nexts
++ < num_exts
&& current_ext
< total_extents
) {
5665 error
= xfs_bmse_shift_one(ip
, whichfork
, offset_shift_fsb
,
5666 ¤t_ext
, gotp
, cur
, &logflags
);
5670 /* update total extent count and grab the next record */
5671 total_extents
= ifp
->if_bytes
/ sizeof(xfs_bmbt_rec_t
);
5672 if (current_ext
>= total_extents
)
5674 gotp
= xfs_iext_get_ext(ifp
, current_ext
);
5677 /* Check if we are done */
5678 if (current_ext
== total_extents
) {
5680 } else if (next_fsb
) {
5681 xfs_bmbt_get_all(gotp
, &got
);
5682 *next_fsb
= got
.br_startoff
;
5687 xfs_btree_del_cursor(cur
,
5688 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
5691 xfs_trans_log_inode(tp
, ip
, logflags
);