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 kmem_zone_t
*xfs_bmap_free_item_zone
;
23 * Miscellaneous helper functions
27 * Compute and fill in the value of the maximum depth of a bmap btree
28 * in this filesystem. Done once, during mount.
31 xfs_bmap_compute_maxlevels(
32 xfs_mount_t
*mp
, /* file system mount structure */
33 int whichfork
) /* data or attr fork */
35 int level
; /* btree level */
36 uint maxblocks
; /* max blocks at this level */
37 uint maxleafents
; /* max leaf entries possible */
38 int maxrootrecs
; /* max records in root block */
39 int minleafrecs
; /* min records in leaf block */
40 int minnoderecs
; /* min records in node block */
41 int sz
; /* root block size */
44 * The maximum number of extents in a file, hence the maximum
45 * number of leaf entries, is controlled by the type of di_nextents
46 * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
47 * (a signed 16-bit number, xfs_aextnum_t).
49 * Note that we can no longer assume that if we are in ATTR1 that
50 * the fork offset of all the inodes will be
51 * (xfs_default_attroffset(ip) >> 3) because we could have mounted
52 * with ATTR2 and then mounted back with ATTR1, keeping the
53 * di_forkoff's fixed but probably at various positions. Therefore,
54 * for both ATTR1 and ATTR2 we have to assume the worst case scenario
55 * of a minimum size available.
57 if (whichfork
== XFS_DATA_FORK
) {
58 maxleafents
= MAXEXTNUM
;
59 sz
= XFS_BMDR_SPACE_CALC(MINDBTPTRS
);
61 maxleafents
= MAXAEXTNUM
;
62 sz
= XFS_BMDR_SPACE_CALC(MINABTPTRS
);
64 maxrootrecs
= xfs_bmdr_maxrecs(sz
, 0);
65 minleafrecs
= mp
->m_bmap_dmnr
[0];
66 minnoderecs
= mp
->m_bmap_dmnr
[1];
67 maxblocks
= (maxleafents
+ minleafrecs
- 1) / minleafrecs
;
68 for (level
= 1; maxblocks
> 1; level
++) {
69 if (maxblocks
<= maxrootrecs
)
72 maxblocks
= (maxblocks
+ minnoderecs
- 1) / minnoderecs
;
74 mp
->m_bm_maxlevels
[whichfork
] = level
;
77 STATIC
int /* error */
79 struct xfs_btree_cur
*cur
,
83 int *stat
) /* success/failure */
85 cur
->bc_rec
.b
.br_startoff
= off
;
86 cur
->bc_rec
.b
.br_startblock
= bno
;
87 cur
->bc_rec
.b
.br_blockcount
= len
;
88 return xfs_btree_lookup(cur
, XFS_LOOKUP_EQ
, stat
);
91 STATIC
int /* error */
93 struct xfs_btree_cur
*cur
,
97 int *stat
) /* success/failure */
99 cur
->bc_rec
.b
.br_startoff
= off
;
100 cur
->bc_rec
.b
.br_startblock
= bno
;
101 cur
->bc_rec
.b
.br_blockcount
= len
;
102 return xfs_btree_lookup(cur
, XFS_LOOKUP_GE
, stat
);
106 * Check if the inode needs to be converted to btree format.
108 static inline bool xfs_bmap_needs_btree(struct xfs_inode
*ip
, int whichfork
)
110 return XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
&&
111 XFS_IFORK_NEXTENTS(ip
, whichfork
) >
112 XFS_IFORK_MAXEXT(ip
, whichfork
);
116 * Check if the inode should be converted to extent format.
118 static inline bool xfs_bmap_wants_extents(struct xfs_inode
*ip
, int whichfork
)
120 return XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
&&
121 XFS_IFORK_NEXTENTS(ip
, whichfork
) <=
122 XFS_IFORK_MAXEXT(ip
, whichfork
);
126 * Update the record referred to by cur to the value given
127 * by [off, bno, len, state].
128 * This either works (return 0) or gets an EFSCORRUPTED error.
132 struct xfs_btree_cur
*cur
,
138 union xfs_btree_rec rec
;
140 xfs_bmbt_disk_set_allf(&rec
.bmbt
, off
, bno
, len
, state
);
141 return xfs_btree_update(cur
, &rec
);
145 * Compute the worst-case number of indirect blocks that will be used
146 * for ip's delayed extent of length "len".
149 xfs_bmap_worst_indlen(
150 xfs_inode_t
*ip
, /* incore inode pointer */
151 xfs_filblks_t len
) /* delayed extent length */
153 int level
; /* btree level number */
154 int maxrecs
; /* maximum record count at this level */
155 xfs_mount_t
*mp
; /* mount structure */
156 xfs_filblks_t rval
; /* return value */
159 maxrecs
= mp
->m_bmap_dmxr
[0];
160 for (level
= 0, rval
= 0;
161 level
< XFS_BM_MAXLEVELS(mp
, XFS_DATA_FORK
);
164 do_div(len
, maxrecs
);
167 return rval
+ XFS_BM_MAXLEVELS(mp
, XFS_DATA_FORK
) -
170 maxrecs
= mp
->m_bmap_dmxr
[1];
176 * Calculate the default attribute fork offset for newly created inodes.
179 xfs_default_attroffset(
180 struct xfs_inode
*ip
)
182 struct xfs_mount
*mp
= ip
->i_mount
;
185 if (mp
->m_sb
.sb_inodesize
== 256) {
186 offset
= XFS_LITINO(mp
, ip
->i_d
.di_version
) -
187 XFS_BMDR_SPACE_CALC(MINABTPTRS
);
189 offset
= XFS_BMDR_SPACE_CALC(6 * MINABTPTRS
);
192 ASSERT(offset
< XFS_LITINO(mp
, ip
->i_d
.di_version
));
197 * Helper routine to reset inode di_forkoff field when switching
198 * attribute fork from local to extent format - we reset it where
199 * possible to make space available for inline data fork extents.
202 xfs_bmap_forkoff_reset(
206 if (whichfork
== XFS_ATTR_FORK
&&
207 ip
->i_d
.di_format
!= XFS_DINODE_FMT_DEV
&&
208 ip
->i_d
.di_format
!= XFS_DINODE_FMT_UUID
&&
209 ip
->i_d
.di_format
!= XFS_DINODE_FMT_BTREE
) {
210 uint dfl_forkoff
= xfs_default_attroffset(ip
) >> 3;
212 if (dfl_forkoff
> ip
->i_d
.di_forkoff
)
213 ip
->i_d
.di_forkoff
= dfl_forkoff
;
218 * Debug/sanity checking code
222 xfs_bmap_sanity_check(
223 struct xfs_mount
*mp
,
227 struct xfs_btree_block
*block
= XFS_BUF_TO_BLOCK(bp
);
229 if (block
->bb_magic
!= cpu_to_be32(XFS_BMAP_CRC_MAGIC
) &&
230 block
->bb_magic
!= cpu_to_be32(XFS_BMAP_MAGIC
))
233 if (be16_to_cpu(block
->bb_level
) != level
||
234 be16_to_cpu(block
->bb_numrecs
) == 0 ||
235 be16_to_cpu(block
->bb_numrecs
) > mp
->m_bmap_dmxr
[level
!= 0])
242 STATIC
struct xfs_buf
*
244 struct xfs_btree_cur
*cur
,
247 struct xfs_log_item_desc
*lidp
;
253 for (i
= 0; i
< XFS_BTREE_MAXLEVELS
; i
++) {
254 if (!cur
->bc_bufs
[i
])
256 if (XFS_BUF_ADDR(cur
->bc_bufs
[i
]) == bno
)
257 return cur
->bc_bufs
[i
];
260 /* Chase down all the log items to see if the bp is there */
261 list_for_each_entry(lidp
, &cur
->bc_tp
->t_items
, lid_trans
) {
262 struct xfs_buf_log_item
*bip
;
263 bip
= (struct xfs_buf_log_item
*)lidp
->lid_item
;
264 if (bip
->bli_item
.li_type
== XFS_LI_BUF
&&
265 XFS_BUF_ADDR(bip
->bli_buf
) == bno
)
274 struct xfs_btree_block
*block
,
280 __be64
*pp
, *thispa
; /* pointer to block address */
281 xfs_bmbt_key_t
*prevp
, *keyp
;
283 ASSERT(be16_to_cpu(block
->bb_level
) > 0);
286 for( i
= 1; i
<= xfs_btree_get_numrecs(block
); i
++) {
287 dmxr
= mp
->m_bmap_dmxr
[0];
288 keyp
= XFS_BMBT_KEY_ADDR(mp
, block
, i
);
291 ASSERT(be64_to_cpu(prevp
->br_startoff
) <
292 be64_to_cpu(keyp
->br_startoff
));
297 * Compare the block numbers to see if there are dups.
300 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, i
, sz
);
302 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, i
, dmxr
);
304 for (j
= i
+1; j
<= be16_to_cpu(block
->bb_numrecs
); j
++) {
306 thispa
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, j
, sz
);
308 thispa
= XFS_BMBT_PTR_ADDR(mp
, block
, j
, dmxr
);
309 if (*thispa
== *pp
) {
310 xfs_warn(mp
, "%s: thispa(%d) == pp(%d) %Ld",
312 (unsigned long long)be64_to_cpu(*thispa
));
313 panic("%s: ptrs are equal in node\n",
321 * Check that the extents for the inode ip are in the right order in all
326 xfs_bmap_check_leaf_extents(
327 xfs_btree_cur_t
*cur
, /* btree cursor or null */
328 xfs_inode_t
*ip
, /* incore inode pointer */
329 int whichfork
) /* data or attr fork */
331 struct xfs_btree_block
*block
; /* current btree block */
332 xfs_fsblock_t bno
; /* block # of "block" */
333 xfs_buf_t
*bp
; /* buffer for "block" */
334 int error
; /* error return value */
335 xfs_extnum_t i
=0, j
; /* index into the extents list */
336 xfs_ifork_t
*ifp
; /* fork structure */
337 int level
; /* btree level, for checking */
338 xfs_mount_t
*mp
; /* file system mount structure */
339 __be64
*pp
; /* pointer to block address */
340 xfs_bmbt_rec_t
*ep
; /* pointer to current extent */
341 xfs_bmbt_rec_t last
= {0, 0}; /* last extent in prev block */
342 xfs_bmbt_rec_t
*nextp
; /* pointer to next extent */
345 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
) {
351 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
352 block
= ifp
->if_broot
;
354 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
356 level
= be16_to_cpu(block
->bb_level
);
358 xfs_check_block(block
, mp
, 1, ifp
->if_broot_bytes
);
359 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, 1, ifp
->if_broot_bytes
);
360 bno
= be64_to_cpu(*pp
);
362 ASSERT(bno
!= NULLDFSBNO
);
363 ASSERT(XFS_FSB_TO_AGNO(mp
, bno
) < mp
->m_sb
.sb_agcount
);
364 ASSERT(XFS_FSB_TO_AGBNO(mp
, bno
) < mp
->m_sb
.sb_agblocks
);
367 * Go down the tree until leaf level is reached, following the first
368 * pointer (leftmost) at each level.
370 while (level
-- > 0) {
371 /* See if buf is in cur first */
373 bp
= xfs_bmap_get_bp(cur
, XFS_FSB_TO_DADDR(mp
, bno
));
376 error
= xfs_btree_read_bufl(mp
, NULL
, bno
, 0, &bp
,
382 block
= XFS_BUF_TO_BLOCK(bp
);
383 XFS_WANT_CORRUPTED_GOTO(
384 xfs_bmap_sanity_check(mp
, bp
, level
),
390 * Check this block for basic sanity (increasing keys and
391 * no duplicate blocks).
394 xfs_check_block(block
, mp
, 0, 0);
395 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, mp
->m_bmap_dmxr
[1]);
396 bno
= be64_to_cpu(*pp
);
397 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp
, bno
), error0
);
400 xfs_trans_brelse(NULL
, bp
);
405 * Here with bp and block set to the leftmost leaf node in the tree.
410 * Loop over all leaf nodes checking that all extents are in the right order.
413 xfs_fsblock_t nextbno
;
414 xfs_extnum_t num_recs
;
417 num_recs
= xfs_btree_get_numrecs(block
);
420 * Read-ahead the next leaf block, if any.
423 nextbno
= be64_to_cpu(block
->bb_u
.l
.bb_rightsib
);
426 * Check all the extents to make sure they are OK.
427 * If we had a previous block, the last entry should
428 * conform with the first entry in this one.
431 ep
= XFS_BMBT_REC_ADDR(mp
, block
, 1);
433 ASSERT(xfs_bmbt_disk_get_startoff(&last
) +
434 xfs_bmbt_disk_get_blockcount(&last
) <=
435 xfs_bmbt_disk_get_startoff(ep
));
437 for (j
= 1; j
< num_recs
; j
++) {
438 nextp
= XFS_BMBT_REC_ADDR(mp
, block
, j
+ 1);
439 ASSERT(xfs_bmbt_disk_get_startoff(ep
) +
440 xfs_bmbt_disk_get_blockcount(ep
) <=
441 xfs_bmbt_disk_get_startoff(nextp
));
449 xfs_trans_brelse(NULL
, bp
);
453 * If we've reached the end, stop.
455 if (bno
== NULLFSBLOCK
)
459 bp
= xfs_bmap_get_bp(cur
, XFS_FSB_TO_DADDR(mp
, bno
));
462 error
= xfs_btree_read_bufl(mp
, NULL
, bno
, 0, &bp
,
468 block
= XFS_BUF_TO_BLOCK(bp
);
472 xfs_trans_brelse(NULL
, bp
);
477 xfs_warn(mp
, "%s: at error0", __func__
);
479 xfs_trans_brelse(NULL
, bp
);
481 xfs_warn(mp
, "%s: BAD after btree leaves for %d extents",
483 panic("%s: CORRUPTED BTREE OR SOMETHING", __func__
);
488 * Add bmap trace insert entries for all the contents of the extent records.
491 xfs_bmap_trace_exlist(
492 xfs_inode_t
*ip
, /* incore inode pointer */
493 xfs_extnum_t cnt
, /* count of entries in the list */
494 int whichfork
, /* data or attr fork */
495 unsigned long caller_ip
)
497 xfs_extnum_t idx
; /* extent record index */
498 xfs_ifork_t
*ifp
; /* inode fork pointer */
501 if (whichfork
== XFS_ATTR_FORK
)
502 state
|= BMAP_ATTRFORK
;
504 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
505 ASSERT(cnt
== (ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
)));
506 for (idx
= 0; idx
< cnt
; idx
++)
507 trace_xfs_extlist(ip
, idx
, whichfork
, caller_ip
);
511 * Validate that the bmbt_irecs being returned from bmapi are valid
512 * given the caller's original parameters. Specifically check the
513 * ranges of the returned irecs to ensure that they only extend beyond
514 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
517 xfs_bmap_validate_ret(
521 xfs_bmbt_irec_t
*mval
,
525 int i
; /* index to map values */
527 ASSERT(ret_nmap
<= nmap
);
529 for (i
= 0; i
< ret_nmap
; i
++) {
530 ASSERT(mval
[i
].br_blockcount
> 0);
531 if (!(flags
& XFS_BMAPI_ENTIRE
)) {
532 ASSERT(mval
[i
].br_startoff
>= bno
);
533 ASSERT(mval
[i
].br_blockcount
<= len
);
534 ASSERT(mval
[i
].br_startoff
+ mval
[i
].br_blockcount
<=
537 ASSERT(mval
[i
].br_startoff
< bno
+ len
);
538 ASSERT(mval
[i
].br_startoff
+ mval
[i
].br_blockcount
>
542 mval
[i
- 1].br_startoff
+ mval
[i
- 1].br_blockcount
==
543 mval
[i
].br_startoff
);
544 ASSERT(mval
[i
].br_startblock
!= DELAYSTARTBLOCK
&&
545 mval
[i
].br_startblock
!= HOLESTARTBLOCK
);
546 ASSERT(mval
[i
].br_state
== XFS_EXT_NORM
||
547 mval
[i
].br_state
== XFS_EXT_UNWRITTEN
);
552 #define xfs_bmap_check_leaf_extents(cur, ip, whichfork) do { } while (0)
553 #define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap)
557 * bmap free list manipulation functions
561 * Add the extent to the list of extents to be free at transaction end.
562 * The list is maintained sorted (by block number).
566 xfs_fsblock_t bno
, /* fs block number of extent */
567 xfs_filblks_t len
, /* length of extent */
568 xfs_bmap_free_t
*flist
, /* list of extents */
569 xfs_mount_t
*mp
) /* mount point structure */
571 xfs_bmap_free_item_t
*cur
; /* current (next) element */
572 xfs_bmap_free_item_t
*new; /* new element */
573 xfs_bmap_free_item_t
*prev
; /* previous element */
578 ASSERT(bno
!= NULLFSBLOCK
);
580 ASSERT(len
<= MAXEXTLEN
);
581 ASSERT(!isnullstartblock(bno
));
582 agno
= XFS_FSB_TO_AGNO(mp
, bno
);
583 agbno
= XFS_FSB_TO_AGBNO(mp
, bno
);
584 ASSERT(agno
< mp
->m_sb
.sb_agcount
);
585 ASSERT(agbno
< mp
->m_sb
.sb_agblocks
);
586 ASSERT(len
< mp
->m_sb
.sb_agblocks
);
587 ASSERT(agbno
+ len
<= mp
->m_sb
.sb_agblocks
);
589 ASSERT(xfs_bmap_free_item_zone
!= NULL
);
590 new = kmem_zone_alloc(xfs_bmap_free_item_zone
, KM_SLEEP
);
591 new->xbfi_startblock
= bno
;
592 new->xbfi_blockcount
= (xfs_extlen_t
)len
;
593 for (prev
= NULL
, cur
= flist
->xbf_first
;
595 prev
= cur
, cur
= cur
->xbfi_next
) {
596 if (cur
->xbfi_startblock
>= bno
)
600 prev
->xbfi_next
= new;
602 flist
->xbf_first
= new;
603 new->xbfi_next
= cur
;
608 * Remove the entry "free" from the free item list. Prev points to the
609 * previous entry, unless "free" is the head of the list.
613 xfs_bmap_free_t
*flist
, /* free item list header */
614 xfs_bmap_free_item_t
*prev
, /* previous item on list, if any */
615 xfs_bmap_free_item_t
*free
) /* list item to be freed */
618 prev
->xbfi_next
= free
->xbfi_next
;
620 flist
->xbf_first
= free
->xbfi_next
;
622 kmem_zone_free(xfs_bmap_free_item_zone
, free
);
626 * Free up any items left in the list.
630 xfs_bmap_free_t
*flist
) /* list of bmap_free_items */
632 xfs_bmap_free_item_t
*free
; /* free list item */
633 xfs_bmap_free_item_t
*next
;
635 if (flist
->xbf_count
== 0)
637 ASSERT(flist
->xbf_first
!= NULL
);
638 for (free
= flist
->xbf_first
; free
; free
= next
) {
639 next
= free
->xbfi_next
;
640 xfs_bmap_del_free(flist
, NULL
, free
);
642 ASSERT(flist
->xbf_count
== 0);
646 * Inode fork format manipulation functions
650 * Transform a btree format file with only one leaf node, where the
651 * extents list will fit in the inode, into an extents format file.
652 * Since the file extents are already in-core, all we have to do is
653 * give up the space for the btree root and pitch the leaf block.
655 STATIC
int /* error */
656 xfs_bmap_btree_to_extents(
657 xfs_trans_t
*tp
, /* transaction pointer */
658 xfs_inode_t
*ip
, /* incore inode pointer */
659 xfs_btree_cur_t
*cur
, /* btree cursor */
660 int *logflagsp
, /* inode logging flags */
661 int whichfork
) /* data or attr fork */
664 struct xfs_btree_block
*cblock
;/* child btree block */
665 xfs_fsblock_t cbno
; /* child block number */
666 xfs_buf_t
*cbp
; /* child block's buffer */
667 int error
; /* error return value */
668 xfs_ifork_t
*ifp
; /* inode fork data */
669 xfs_mount_t
*mp
; /* mount point structure */
670 __be64
*pp
; /* ptr to block address */
671 struct xfs_btree_block
*rblock
;/* root btree block */
674 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
675 ASSERT(ifp
->if_flags
& XFS_IFEXTENTS
);
676 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
);
677 rblock
= ifp
->if_broot
;
678 ASSERT(be16_to_cpu(rblock
->bb_level
) == 1);
679 ASSERT(be16_to_cpu(rblock
->bb_numrecs
) == 1);
680 ASSERT(xfs_bmbt_maxrecs(mp
, ifp
->if_broot_bytes
, 0) == 1);
681 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, rblock
, 1, ifp
->if_broot_bytes
);
682 cbno
= be64_to_cpu(*pp
);
685 if ((error
= xfs_btree_check_lptr(cur
, cbno
, 1)))
688 error
= xfs_btree_read_bufl(mp
, tp
, cbno
, 0, &cbp
, XFS_BMAP_BTREE_REF
,
692 cblock
= XFS_BUF_TO_BLOCK(cbp
);
693 if ((error
= xfs_btree_check_block(cur
, cblock
, 0, cbp
)))
695 xfs_bmap_add_free(cbno
, 1, cur
->bc_private
.b
.flist
, mp
);
696 ip
->i_d
.di_nblocks
--;
697 xfs_trans_mod_dquot_byino(tp
, ip
, XFS_TRANS_DQ_BCOUNT
, -1L);
698 xfs_trans_binval(tp
, cbp
);
699 if (cur
->bc_bufs
[0] == cbp
)
700 cur
->bc_bufs
[0] = NULL
;
701 xfs_iroot_realloc(ip
, -1, whichfork
);
702 ASSERT(ifp
->if_broot
== NULL
);
703 ASSERT((ifp
->if_flags
& XFS_IFBROOT
) == 0);
704 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_EXTENTS
);
705 *logflagsp
= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
710 * Convert an extents-format file into a btree-format file.
711 * The new file will have a root block (in the inode) and a single child block.
713 STATIC
int /* error */
714 xfs_bmap_extents_to_btree(
715 xfs_trans_t
*tp
, /* transaction pointer */
716 xfs_inode_t
*ip
, /* incore inode pointer */
717 xfs_fsblock_t
*firstblock
, /* first-block-allocated */
718 xfs_bmap_free_t
*flist
, /* blocks freed in xaction */
719 xfs_btree_cur_t
**curp
, /* cursor returned to caller */
720 int wasdel
, /* converting a delayed alloc */
721 int *logflagsp
, /* inode logging flags */
722 int whichfork
) /* data or attr fork */
724 struct xfs_btree_block
*ablock
; /* allocated (child) bt block */
725 xfs_buf_t
*abp
; /* buffer for ablock */
726 xfs_alloc_arg_t args
; /* allocation arguments */
727 xfs_bmbt_rec_t
*arp
; /* child record pointer */
728 struct xfs_btree_block
*block
; /* btree root block */
729 xfs_btree_cur_t
*cur
; /* bmap btree cursor */
730 xfs_bmbt_rec_host_t
*ep
; /* extent record pointer */
731 int error
; /* error return value */
732 xfs_extnum_t i
, cnt
; /* extent record index */
733 xfs_ifork_t
*ifp
; /* inode fork pointer */
734 xfs_bmbt_key_t
*kp
; /* root block key pointer */
735 xfs_mount_t
*mp
; /* mount structure */
736 xfs_extnum_t nextents
; /* number of file extents */
737 xfs_bmbt_ptr_t
*pp
; /* root block address pointer */
740 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
741 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
);
744 * Make space in the inode incore.
746 xfs_iroot_realloc(ip
, 1, whichfork
);
747 ifp
->if_flags
|= XFS_IFBROOT
;
752 block
= ifp
->if_broot
;
753 if (xfs_sb_version_hascrc(&mp
->m_sb
))
754 xfs_btree_init_block_int(mp
, block
, XFS_BUF_DADDR_NULL
,
755 XFS_BMAP_CRC_MAGIC
, 1, 1, ip
->i_ino
,
756 XFS_BTREE_LONG_PTRS
| XFS_BTREE_CRC_BLOCKS
);
758 xfs_btree_init_block_int(mp
, block
, XFS_BUF_DADDR_NULL
,
759 XFS_BMAP_MAGIC
, 1, 1, ip
->i_ino
,
760 XFS_BTREE_LONG_PTRS
);
763 * Need a cursor. Can't allocate until bb_level is filled in.
765 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
766 cur
->bc_private
.b
.firstblock
= *firstblock
;
767 cur
->bc_private
.b
.flist
= flist
;
768 cur
->bc_private
.b
.flags
= wasdel
? XFS_BTCUR_BPRV_WASDEL
: 0;
770 * Convert to a btree with two levels, one record in root.
772 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_BTREE
);
773 memset(&args
, 0, sizeof(args
));
776 args
.firstblock
= *firstblock
;
777 if (*firstblock
== NULLFSBLOCK
) {
778 args
.type
= XFS_ALLOCTYPE_START_BNO
;
779 args
.fsbno
= XFS_INO_TO_FSB(mp
, ip
->i_ino
);
780 } else if (flist
->xbf_low
) {
781 args
.type
= XFS_ALLOCTYPE_START_BNO
;
782 args
.fsbno
= *firstblock
;
784 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
785 args
.fsbno
= *firstblock
;
787 args
.minlen
= args
.maxlen
= args
.prod
= 1;
788 args
.wasdel
= wasdel
;
790 if ((error
= xfs_alloc_vextent(&args
))) {
791 xfs_iroot_realloc(ip
, -1, whichfork
);
792 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
796 * Allocation can't fail, the space was reserved.
798 ASSERT(args
.fsbno
!= NULLFSBLOCK
);
799 ASSERT(*firstblock
== NULLFSBLOCK
||
800 args
.agno
== XFS_FSB_TO_AGNO(mp
, *firstblock
) ||
802 args
.agno
> XFS_FSB_TO_AGNO(mp
, *firstblock
)));
803 *firstblock
= cur
->bc_private
.b
.firstblock
= args
.fsbno
;
804 cur
->bc_private
.b
.allocated
++;
805 ip
->i_d
.di_nblocks
++;
806 xfs_trans_mod_dquot_byino(tp
, ip
, XFS_TRANS_DQ_BCOUNT
, 1L);
807 abp
= xfs_btree_get_bufl(mp
, tp
, args
.fsbno
, 0);
809 * Fill in the child block.
811 abp
->b_ops
= &xfs_bmbt_buf_ops
;
812 ablock
= XFS_BUF_TO_BLOCK(abp
);
813 if (xfs_sb_version_hascrc(&mp
->m_sb
))
814 xfs_btree_init_block_int(mp
, ablock
, abp
->b_bn
,
815 XFS_BMAP_CRC_MAGIC
, 0, 0, ip
->i_ino
,
816 XFS_BTREE_LONG_PTRS
| XFS_BTREE_CRC_BLOCKS
);
818 xfs_btree_init_block_int(mp
, ablock
, abp
->b_bn
,
819 XFS_BMAP_MAGIC
, 0, 0, ip
->i_ino
,
820 XFS_BTREE_LONG_PTRS
);
822 arp
= XFS_BMBT_REC_ADDR(mp
, ablock
, 1);
823 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
824 for (cnt
= i
= 0; i
< nextents
; i
++) {
825 ep
= xfs_iext_get_ext(ifp
, i
);
826 if (!isnullstartblock(xfs_bmbt_get_startblock(ep
))) {
827 arp
->l0
= cpu_to_be64(ep
->l0
);
828 arp
->l1
= cpu_to_be64(ep
->l1
);
832 ASSERT(cnt
== XFS_IFORK_NEXTENTS(ip
, whichfork
));
833 xfs_btree_set_numrecs(ablock
, cnt
);
836 * Fill in the root key and pointer.
838 kp
= XFS_BMBT_KEY_ADDR(mp
, block
, 1);
839 arp
= XFS_BMBT_REC_ADDR(mp
, ablock
, 1);
840 kp
->br_startoff
= cpu_to_be64(xfs_bmbt_disk_get_startoff(arp
));
841 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, xfs_bmbt_get_maxrecs(cur
,
842 be16_to_cpu(block
->bb_level
)));
843 *pp
= cpu_to_be64(args
.fsbno
);
846 * Do all this logging at the end so that
847 * the root is at the right level.
849 xfs_btree_log_block(cur
, abp
, XFS_BB_ALL_BITS
);
850 xfs_btree_log_recs(cur
, abp
, 1, be16_to_cpu(ablock
->bb_numrecs
));
851 ASSERT(*curp
== NULL
);
853 *logflagsp
= XFS_ILOG_CORE
| xfs_ilog_fbroot(whichfork
);
858 * Convert a local file to an extents file.
859 * This code is out of bounds for data forks of regular files,
860 * since the file data needs to get logged so things will stay consistent.
861 * (The bmap-level manipulations are ok, though).
864 xfs_bmap_local_to_extents_empty(
865 struct xfs_inode
*ip
,
868 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
870 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
871 ASSERT(ifp
->if_bytes
== 0);
872 ASSERT(XFS_IFORK_NEXTENTS(ip
, whichfork
) == 0);
874 xfs_bmap_forkoff_reset(ip
, whichfork
);
875 ifp
->if_flags
&= ~XFS_IFINLINE
;
876 ifp
->if_flags
|= XFS_IFEXTENTS
;
877 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_EXTENTS
);
881 STATIC
int /* error */
882 xfs_bmap_local_to_extents(
883 xfs_trans_t
*tp
, /* transaction pointer */
884 xfs_inode_t
*ip
, /* incore inode pointer */
885 xfs_fsblock_t
*firstblock
, /* first block allocated in xaction */
886 xfs_extlen_t total
, /* total blocks needed by transaction */
887 int *logflagsp
, /* inode logging flags */
889 void (*init_fn
)(struct xfs_trans
*tp
,
891 struct xfs_inode
*ip
,
892 struct xfs_ifork
*ifp
))
895 int flags
; /* logging flags returned */
896 xfs_ifork_t
*ifp
; /* inode fork pointer */
897 xfs_alloc_arg_t args
; /* allocation arguments */
898 xfs_buf_t
*bp
; /* buffer for extent block */
899 xfs_bmbt_rec_host_t
*ep
; /* extent record pointer */
902 * We don't want to deal with the case of keeping inode data inline yet.
903 * So sending the data fork of a regular inode is invalid.
905 ASSERT(!(S_ISREG(ip
->i_d
.di_mode
) && whichfork
== XFS_DATA_FORK
));
906 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
907 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
909 if (!ifp
->if_bytes
) {
910 xfs_bmap_local_to_extents_empty(ip
, whichfork
);
911 flags
= XFS_ILOG_CORE
;
917 ASSERT((ifp
->if_flags
& (XFS_IFINLINE
|XFS_IFEXTENTS
|XFS_IFEXTIREC
)) ==
919 memset(&args
, 0, sizeof(args
));
921 args
.mp
= ip
->i_mount
;
922 args
.firstblock
= *firstblock
;
924 * Allocate a block. We know we need only one, since the
925 * file currently fits in an inode.
927 if (*firstblock
== NULLFSBLOCK
) {
928 args
.fsbno
= XFS_INO_TO_FSB(args
.mp
, ip
->i_ino
);
929 args
.type
= XFS_ALLOCTYPE_START_BNO
;
931 args
.fsbno
= *firstblock
;
932 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
935 args
.minlen
= args
.maxlen
= args
.prod
= 1;
936 error
= xfs_alloc_vextent(&args
);
940 /* Can't fail, the space was reserved. */
941 ASSERT(args
.fsbno
!= NULLFSBLOCK
);
942 ASSERT(args
.len
== 1);
943 *firstblock
= args
.fsbno
;
944 bp
= xfs_btree_get_bufl(args
.mp
, tp
, args
.fsbno
, 0);
946 /* initialise the block and copy the data */
947 init_fn(tp
, bp
, ip
, ifp
);
949 /* account for the change in fork size and log everything */
950 xfs_trans_log_buf(tp
, bp
, 0, ifp
->if_bytes
- 1);
951 xfs_idata_realloc(ip
, -ifp
->if_bytes
, whichfork
);
952 xfs_bmap_local_to_extents_empty(ip
, whichfork
);
953 flags
|= XFS_ILOG_CORE
;
955 xfs_iext_add(ifp
, 0, 1);
956 ep
= xfs_iext_get_ext(ifp
, 0);
957 xfs_bmbt_set_allf(ep
, 0, args
.fsbno
, 1, XFS_EXT_NORM
);
958 trace_xfs_bmap_post_update(ip
, 0,
959 whichfork
== XFS_ATTR_FORK
? BMAP_ATTRFORK
: 0,
961 XFS_IFORK_NEXT_SET(ip
, whichfork
, 1);
962 ip
->i_d
.di_nblocks
= 1;
963 xfs_trans_mod_dquot_byino(tp
, ip
,
964 XFS_TRANS_DQ_BCOUNT
, 1L);
965 flags
|= xfs_ilog_fext(whichfork
);
973 * Called from xfs_bmap_add_attrfork to handle btree format files.
975 STATIC
int /* error */
976 xfs_bmap_add_attrfork_btree(
977 xfs_trans_t
*tp
, /* transaction pointer */
978 xfs_inode_t
*ip
, /* incore inode pointer */
979 xfs_fsblock_t
*firstblock
, /* first block allocated */
980 xfs_bmap_free_t
*flist
, /* blocks to free at commit */
981 int *flags
) /* inode logging flags */
983 xfs_btree_cur_t
*cur
; /* btree cursor */
984 int error
; /* error return value */
985 xfs_mount_t
*mp
; /* file system mount struct */
986 int stat
; /* newroot status */
989 if (ip
->i_df
.if_broot_bytes
<= XFS_IFORK_DSIZE(ip
))
990 *flags
|= XFS_ILOG_DBROOT
;
992 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, XFS_DATA_FORK
);
993 cur
->bc_private
.b
.flist
= flist
;
994 cur
->bc_private
.b
.firstblock
= *firstblock
;
995 if ((error
= xfs_bmbt_lookup_ge(cur
, 0, 0, 0, &stat
)))
997 /* must be at least one entry */
998 XFS_WANT_CORRUPTED_GOTO(stat
== 1, error0
);
999 if ((error
= xfs_btree_new_iroot(cur
, flags
, &stat
)))
1002 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
1005 *firstblock
= cur
->bc_private
.b
.firstblock
;
1006 cur
->bc_private
.b
.allocated
= 0;
1007 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
1011 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
1016 * Called from xfs_bmap_add_attrfork to handle extents format files.
1018 STATIC
int /* error */
1019 xfs_bmap_add_attrfork_extents(
1020 xfs_trans_t
*tp
, /* transaction pointer */
1021 xfs_inode_t
*ip
, /* incore inode pointer */
1022 xfs_fsblock_t
*firstblock
, /* first block allocated */
1023 xfs_bmap_free_t
*flist
, /* blocks to free at commit */
1024 int *flags
) /* inode logging flags */
1026 xfs_btree_cur_t
*cur
; /* bmap btree cursor */
1027 int error
; /* error return value */
1029 if (ip
->i_d
.di_nextents
* sizeof(xfs_bmbt_rec_t
) <= XFS_IFORK_DSIZE(ip
))
1032 error
= xfs_bmap_extents_to_btree(tp
, ip
, firstblock
, flist
, &cur
, 0,
1033 flags
, XFS_DATA_FORK
);
1035 cur
->bc_private
.b
.allocated
= 0;
1036 xfs_btree_del_cursor(cur
,
1037 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
1043 * Called from xfs_bmap_add_attrfork to handle local format files. Each
1044 * different data fork content type needs a different callout to do the
1045 * conversion. Some are basic and only require special block initialisation
1046 * callouts for the data formating, others (directories) are so specialised they
1047 * handle everything themselves.
1049 * XXX (dgc): investigate whether directory conversion can use the generic
1050 * formatting callout. It should be possible - it's just a very complex
1053 STATIC
int /* error */
1054 xfs_bmap_add_attrfork_local(
1055 xfs_trans_t
*tp
, /* transaction pointer */
1056 xfs_inode_t
*ip
, /* incore inode pointer */
1057 xfs_fsblock_t
*firstblock
, /* first block allocated */
1058 xfs_bmap_free_t
*flist
, /* blocks to free at commit */
1059 int *flags
) /* inode logging flags */
1061 xfs_da_args_t dargs
; /* args for dir/attr code */
1063 if (ip
->i_df
.if_bytes
<= XFS_IFORK_DSIZE(ip
))
1066 if (S_ISDIR(ip
->i_d
.di_mode
)) {
1067 memset(&dargs
, 0, sizeof(dargs
));
1068 dargs
.geo
= ip
->i_mount
->m_dir_geo
;
1070 dargs
.firstblock
= firstblock
;
1071 dargs
.flist
= flist
;
1072 dargs
.total
= dargs
.geo
->fsbcount
;
1073 dargs
.whichfork
= XFS_DATA_FORK
;
1075 return xfs_dir2_sf_to_block(&dargs
);
1078 if (S_ISLNK(ip
->i_d
.di_mode
))
1079 return xfs_bmap_local_to_extents(tp
, ip
, firstblock
, 1,
1080 flags
, XFS_DATA_FORK
,
1081 xfs_symlink_local_to_remote
);
1083 /* should only be called for types that support local format data */
1085 return -EFSCORRUPTED
;
1089 * Convert inode from non-attributed to attributed.
1090 * Must not be in a transaction, ip must not be locked.
1092 int /* error code */
1093 xfs_bmap_add_attrfork(
1094 xfs_inode_t
*ip
, /* incore inode pointer */
1095 int size
, /* space new attribute needs */
1096 int rsvd
) /* xact may use reserved blks */
1098 xfs_fsblock_t firstblock
; /* 1st block/ag allocated */
1099 xfs_bmap_free_t flist
; /* freed extent records */
1100 xfs_mount_t
*mp
; /* mount structure */
1101 xfs_trans_t
*tp
; /* transaction pointer */
1102 int blks
; /* space reservation */
1103 int version
= 1; /* superblock attr version */
1104 int committed
; /* xaction was committed */
1105 int logflags
; /* logging flags */
1106 int error
; /* error return value */
1107 int cancel_flags
= 0;
1109 ASSERT(XFS_IFORK_Q(ip
) == 0);
1112 ASSERT(!XFS_NOT_DQATTACHED(mp
, ip
));
1113 tp
= xfs_trans_alloc(mp
, XFS_TRANS_ADDAFORK
);
1114 blks
= XFS_ADDAFORK_SPACE_RES(mp
);
1116 tp
->t_flags
|= XFS_TRANS_RESERVE
;
1117 error
= xfs_trans_reserve(tp
, &M_RES(mp
)->tr_addafork
, blks
, 0);
1119 xfs_trans_cancel(tp
, 0);
1122 cancel_flags
= XFS_TRANS_RELEASE_LOG_RES
;
1123 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
1124 error
= xfs_trans_reserve_quota_nblks(tp
, ip
, blks
, 0, rsvd
?
1125 XFS_QMOPT_RES_REGBLKS
| XFS_QMOPT_FORCE_RES
:
1126 XFS_QMOPT_RES_REGBLKS
);
1129 cancel_flags
|= XFS_TRANS_ABORT
;
1130 if (XFS_IFORK_Q(ip
))
1132 if (ip
->i_d
.di_aformat
!= XFS_DINODE_FMT_EXTENTS
) {
1134 * For inodes coming from pre-6.2 filesystems.
1136 ASSERT(ip
->i_d
.di_aformat
== 0);
1137 ip
->i_d
.di_aformat
= XFS_DINODE_FMT_EXTENTS
;
1139 ASSERT(ip
->i_d
.di_anextents
== 0);
1141 xfs_trans_ijoin(tp
, ip
, 0);
1142 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
1144 switch (ip
->i_d
.di_format
) {
1145 case XFS_DINODE_FMT_DEV
:
1146 ip
->i_d
.di_forkoff
= roundup(sizeof(xfs_dev_t
), 8) >> 3;
1148 case XFS_DINODE_FMT_UUID
:
1149 ip
->i_d
.di_forkoff
= roundup(sizeof(uuid_t
), 8) >> 3;
1151 case XFS_DINODE_FMT_LOCAL
:
1152 case XFS_DINODE_FMT_EXTENTS
:
1153 case XFS_DINODE_FMT_BTREE
:
1154 ip
->i_d
.di_forkoff
= xfs_attr_shortform_bytesfit(ip
, size
);
1155 if (!ip
->i_d
.di_forkoff
)
1156 ip
->i_d
.di_forkoff
= xfs_default_attroffset(ip
) >> 3;
1157 else if (mp
->m_flags
& XFS_MOUNT_ATTR2
)
1166 ASSERT(ip
->i_afp
== NULL
);
1167 ip
->i_afp
= kmem_zone_zalloc(xfs_ifork_zone
, KM_SLEEP
);
1168 ip
->i_afp
->if_flags
= XFS_IFEXTENTS
;
1170 xfs_bmap_init(&flist
, &firstblock
);
1171 switch (ip
->i_d
.di_format
) {
1172 case XFS_DINODE_FMT_LOCAL
:
1173 error
= xfs_bmap_add_attrfork_local(tp
, ip
, &firstblock
, &flist
,
1176 case XFS_DINODE_FMT_EXTENTS
:
1177 error
= xfs_bmap_add_attrfork_extents(tp
, ip
, &firstblock
,
1180 case XFS_DINODE_FMT_BTREE
:
1181 error
= xfs_bmap_add_attrfork_btree(tp
, ip
, &firstblock
, &flist
,
1189 xfs_trans_log_inode(tp
, ip
, logflags
);
1192 if (!xfs_sb_version_hasattr(&mp
->m_sb
) ||
1193 (!xfs_sb_version_hasattr2(&mp
->m_sb
) && version
== 2)) {
1194 __int64_t sbfields
= 0;
1196 spin_lock(&mp
->m_sb_lock
);
1197 if (!xfs_sb_version_hasattr(&mp
->m_sb
)) {
1198 xfs_sb_version_addattr(&mp
->m_sb
);
1199 sbfields
|= XFS_SB_VERSIONNUM
;
1201 if (!xfs_sb_version_hasattr2(&mp
->m_sb
) && version
== 2) {
1202 xfs_sb_version_addattr2(&mp
->m_sb
);
1203 sbfields
|= (XFS_SB_VERSIONNUM
| XFS_SB_FEATURES2
);
1206 spin_unlock(&mp
->m_sb_lock
);
1207 xfs_mod_sb(tp
, sbfields
);
1209 spin_unlock(&mp
->m_sb_lock
);
1212 error
= xfs_bmap_finish(&tp
, &flist
, &committed
);
1215 error
= xfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
);
1216 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
1220 xfs_bmap_cancel(&flist
);
1222 xfs_trans_cancel(tp
, cancel_flags
);
1223 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
1228 * Internal and external extent tree search functions.
1232 * Read in the extents to if_extents.
1233 * All inode fields are set up by caller, we just traverse the btree
1234 * and copy the records in. If the file system cannot contain unwritten
1235 * extents, the records are checked for no "state" flags.
1238 xfs_bmap_read_extents(
1239 xfs_trans_t
*tp
, /* transaction pointer */
1240 xfs_inode_t
*ip
, /* incore inode */
1241 int whichfork
) /* data or attr fork */
1243 struct xfs_btree_block
*block
; /* current btree block */
1244 xfs_fsblock_t bno
; /* block # of "block" */
1245 xfs_buf_t
*bp
; /* buffer for "block" */
1246 int error
; /* error return value */
1247 xfs_exntfmt_t exntf
; /* XFS_EXTFMT_NOSTATE, if checking */
1248 xfs_extnum_t i
, j
; /* index into the extents list */
1249 xfs_ifork_t
*ifp
; /* fork structure */
1250 int level
; /* btree level, for checking */
1251 xfs_mount_t
*mp
; /* file system mount structure */
1252 __be64
*pp
; /* pointer to block address */
1254 xfs_extnum_t room
; /* number of entries there's room for */
1258 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1259 exntf
= (whichfork
!= XFS_DATA_FORK
) ? XFS_EXTFMT_NOSTATE
:
1260 XFS_EXTFMT_INODE(ip
);
1261 block
= ifp
->if_broot
;
1263 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
1265 level
= be16_to_cpu(block
->bb_level
);
1267 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, 1, ifp
->if_broot_bytes
);
1268 bno
= be64_to_cpu(*pp
);
1269 ASSERT(bno
!= NULLDFSBNO
);
1270 ASSERT(XFS_FSB_TO_AGNO(mp
, bno
) < mp
->m_sb
.sb_agcount
);
1271 ASSERT(XFS_FSB_TO_AGBNO(mp
, bno
) < mp
->m_sb
.sb_agblocks
);
1273 * Go down the tree until leaf level is reached, following the first
1274 * pointer (leftmost) at each level.
1276 while (level
-- > 0) {
1277 error
= xfs_btree_read_bufl(mp
, tp
, bno
, 0, &bp
,
1278 XFS_BMAP_BTREE_REF
, &xfs_bmbt_buf_ops
);
1281 block
= XFS_BUF_TO_BLOCK(bp
);
1282 XFS_WANT_CORRUPTED_GOTO(
1283 xfs_bmap_sanity_check(mp
, bp
, level
),
1287 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, mp
->m_bmap_dmxr
[1]);
1288 bno
= be64_to_cpu(*pp
);
1289 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp
, bno
), error0
);
1290 xfs_trans_brelse(tp
, bp
);
1293 * Here with bp and block set to the leftmost leaf node in the tree.
1295 room
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
1298 * Loop over all leaf nodes. Copy information to the extent records.
1301 xfs_bmbt_rec_t
*frp
;
1302 xfs_fsblock_t nextbno
;
1303 xfs_extnum_t num_recs
;
1306 num_recs
= xfs_btree_get_numrecs(block
);
1307 if (unlikely(i
+ num_recs
> room
)) {
1308 ASSERT(i
+ num_recs
<= room
);
1309 xfs_warn(ip
->i_mount
,
1310 "corrupt dinode %Lu, (btree extents).",
1311 (unsigned long long) ip
->i_ino
);
1312 XFS_CORRUPTION_ERROR("xfs_bmap_read_extents(1)",
1313 XFS_ERRLEVEL_LOW
, ip
->i_mount
, block
);
1316 XFS_WANT_CORRUPTED_GOTO(
1317 xfs_bmap_sanity_check(mp
, bp
, 0),
1320 * Read-ahead the next leaf block, if any.
1322 nextbno
= be64_to_cpu(block
->bb_u
.l
.bb_rightsib
);
1323 if (nextbno
!= NULLFSBLOCK
)
1324 xfs_btree_reada_bufl(mp
, nextbno
, 1,
1327 * Copy records into the extent records.
1329 frp
= XFS_BMBT_REC_ADDR(mp
, block
, 1);
1331 for (j
= 0; j
< num_recs
; j
++, i
++, frp
++) {
1332 xfs_bmbt_rec_host_t
*trp
= xfs_iext_get_ext(ifp
, i
);
1333 trp
->l0
= be64_to_cpu(frp
->l0
);
1334 trp
->l1
= be64_to_cpu(frp
->l1
);
1336 if (exntf
== XFS_EXTFMT_NOSTATE
) {
1338 * Check all attribute bmap btree records and
1339 * any "older" data bmap btree records for a
1340 * set bit in the "extent flag" position.
1342 if (unlikely(xfs_check_nostate_extents(ifp
,
1343 start
, num_recs
))) {
1344 XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
1350 xfs_trans_brelse(tp
, bp
);
1353 * If we've reached the end, stop.
1355 if (bno
== NULLFSBLOCK
)
1357 error
= xfs_btree_read_bufl(mp
, tp
, bno
, 0, &bp
,
1358 XFS_BMAP_BTREE_REF
, &xfs_bmbt_buf_ops
);
1361 block
= XFS_BUF_TO_BLOCK(bp
);
1363 ASSERT(i
== (ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
)));
1364 ASSERT(i
== XFS_IFORK_NEXTENTS(ip
, whichfork
));
1365 XFS_BMAP_TRACE_EXLIST(ip
, i
, whichfork
);
1368 xfs_trans_brelse(tp
, bp
);
1369 return -EFSCORRUPTED
;
1374 * Search the extent records for the entry containing block bno.
1375 * If bno lies in a hole, point to the next entry. If bno lies
1376 * past eof, *eofp will be set, and *prevp will contain the last
1377 * entry (null if none). Else, *lastxp will be set to the index
1378 * of the found entry; *gotp will contain the entry.
1380 STATIC xfs_bmbt_rec_host_t
* /* pointer to found extent entry */
1381 xfs_bmap_search_multi_extents(
1382 xfs_ifork_t
*ifp
, /* inode fork pointer */
1383 xfs_fileoff_t bno
, /* block number searched for */
1384 int *eofp
, /* out: end of file found */
1385 xfs_extnum_t
*lastxp
, /* out: last extent index */
1386 xfs_bmbt_irec_t
*gotp
, /* out: extent entry found */
1387 xfs_bmbt_irec_t
*prevp
) /* out: previous extent entry found */
1389 xfs_bmbt_rec_host_t
*ep
; /* extent record pointer */
1390 xfs_extnum_t lastx
; /* last extent index */
1393 * Initialize the extent entry structure to catch access to
1394 * uninitialized br_startblock field.
1396 gotp
->br_startoff
= 0xffa5a5a5a5a5a5a5LL
;
1397 gotp
->br_blockcount
= 0xa55a5a5a5a5a5a5aLL
;
1398 gotp
->br_state
= XFS_EXT_INVALID
;
1400 gotp
->br_startblock
= 0xffffa5a5a5a5a5a5LL
;
1402 gotp
->br_startblock
= 0xffffa5a5;
1404 prevp
->br_startoff
= NULLFILEOFF
;
1406 ep
= xfs_iext_bno_to_ext(ifp
, bno
, &lastx
);
1408 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, lastx
- 1), prevp
);
1410 if (lastx
< (ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
))) {
1411 xfs_bmbt_get_all(ep
, gotp
);
1425 * Search the extents list for the inode, for the extent containing bno.
1426 * If bno lies in a hole, point to the next entry. If bno lies past eof,
1427 * *eofp will be set, and *prevp will contain the last entry (null if none).
1428 * Else, *lastxp will be set to the index of the found
1429 * entry; *gotp will contain the entry.
1431 xfs_bmbt_rec_host_t
* /* pointer to found extent entry */
1432 xfs_bmap_search_extents(
1433 xfs_inode_t
*ip
, /* incore inode pointer */
1434 xfs_fileoff_t bno
, /* block number searched for */
1435 int fork
, /* data or attr fork */
1436 int *eofp
, /* out: end of file found */
1437 xfs_extnum_t
*lastxp
, /* out: last extent index */
1438 xfs_bmbt_irec_t
*gotp
, /* out: extent entry found */
1439 xfs_bmbt_irec_t
*prevp
) /* out: previous extent entry found */
1441 xfs_ifork_t
*ifp
; /* inode fork pointer */
1442 xfs_bmbt_rec_host_t
*ep
; /* extent record pointer */
1444 XFS_STATS_INC(xs_look_exlist
);
1445 ifp
= XFS_IFORK_PTR(ip
, fork
);
1447 ep
= xfs_bmap_search_multi_extents(ifp
, bno
, eofp
, lastxp
, gotp
, prevp
);
1449 if (unlikely(!(gotp
->br_startblock
) && (*lastxp
!= NULLEXTNUM
) &&
1450 !(XFS_IS_REALTIME_INODE(ip
) && fork
== XFS_DATA_FORK
))) {
1451 xfs_alert_tag(ip
->i_mount
, XFS_PTAG_FSBLOCK_ZERO
,
1452 "Access to block zero in inode %llu "
1453 "start_block: %llx start_off: %llx "
1454 "blkcnt: %llx extent-state: %x lastx: %x",
1455 (unsigned long long)ip
->i_ino
,
1456 (unsigned long long)gotp
->br_startblock
,
1457 (unsigned long long)gotp
->br_startoff
,
1458 (unsigned long long)gotp
->br_blockcount
,
1459 gotp
->br_state
, *lastxp
);
1460 *lastxp
= NULLEXTNUM
;
1468 * Returns the file-relative block number of the first unused block(s)
1469 * in the file with at least "len" logically contiguous blocks free.
1470 * This is the lowest-address hole if the file has holes, else the first block
1471 * past the end of file.
1472 * Return 0 if the file is currently local (in-inode).
1475 xfs_bmap_first_unused(
1476 xfs_trans_t
*tp
, /* transaction pointer */
1477 xfs_inode_t
*ip
, /* incore inode */
1478 xfs_extlen_t len
, /* size of hole to find */
1479 xfs_fileoff_t
*first_unused
, /* unused block */
1480 int whichfork
) /* data or attr fork */
1482 int error
; /* error return value */
1483 int idx
; /* extent record index */
1484 xfs_ifork_t
*ifp
; /* inode fork pointer */
1485 xfs_fileoff_t lastaddr
; /* last block number seen */
1486 xfs_fileoff_t lowest
; /* lowest useful block */
1487 xfs_fileoff_t max
; /* starting useful block */
1488 xfs_fileoff_t off
; /* offset for this block */
1489 xfs_extnum_t nextents
; /* number of extent entries */
1491 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
||
1492 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
||
1493 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
1494 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
) {
1498 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1499 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
1500 (error
= xfs_iread_extents(tp
, ip
, whichfork
)))
1502 lowest
= *first_unused
;
1503 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
1504 for (idx
= 0, lastaddr
= 0, max
= lowest
; idx
< nextents
; idx
++) {
1505 xfs_bmbt_rec_host_t
*ep
= xfs_iext_get_ext(ifp
, idx
);
1506 off
= xfs_bmbt_get_startoff(ep
);
1508 * See if the hole before this extent will work.
1510 if (off
>= lowest
+ len
&& off
- max
>= len
) {
1511 *first_unused
= max
;
1514 lastaddr
= off
+ xfs_bmbt_get_blockcount(ep
);
1515 max
= XFS_FILEOFF_MAX(lastaddr
, lowest
);
1517 *first_unused
= max
;
1522 * Returns the file-relative block number of the last block - 1 before
1523 * last_block (input value) in the file.
1524 * This is not based on i_size, it is based on the extent records.
1525 * Returns 0 for local files, as they do not have extent records.
1528 xfs_bmap_last_before(
1529 xfs_trans_t
*tp
, /* transaction pointer */
1530 xfs_inode_t
*ip
, /* incore inode */
1531 xfs_fileoff_t
*last_block
, /* last block */
1532 int whichfork
) /* data or attr fork */
1534 xfs_fileoff_t bno
; /* input file offset */
1535 int eof
; /* hit end of file */
1536 xfs_bmbt_rec_host_t
*ep
; /* pointer to last extent */
1537 int error
; /* error return value */
1538 xfs_bmbt_irec_t got
; /* current extent value */
1539 xfs_ifork_t
*ifp
; /* inode fork pointer */
1540 xfs_extnum_t lastx
; /* last extent used */
1541 xfs_bmbt_irec_t prev
; /* previous extent value */
1543 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
&&
1544 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
1545 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_LOCAL
)
1547 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
) {
1551 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1552 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
1553 (error
= xfs_iread_extents(tp
, ip
, whichfork
)))
1555 bno
= *last_block
- 1;
1556 ep
= xfs_bmap_search_extents(ip
, bno
, whichfork
, &eof
, &lastx
, &got
,
1558 if (eof
|| xfs_bmbt_get_startoff(ep
) > bno
) {
1559 if (prev
.br_startoff
== NULLFILEOFF
)
1562 *last_block
= prev
.br_startoff
+ prev
.br_blockcount
;
1565 * Otherwise *last_block is already the right answer.
1571 xfs_bmap_last_extent(
1572 struct xfs_trans
*tp
,
1573 struct xfs_inode
*ip
,
1575 struct xfs_bmbt_irec
*rec
,
1578 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1582 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
1583 error
= xfs_iread_extents(tp
, ip
, whichfork
);
1588 nextents
= ifp
->if_bytes
/ sizeof(xfs_bmbt_rec_t
);
1589 if (nextents
== 0) {
1594 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, nextents
- 1), rec
);
1600 * Check the last inode extent to determine whether this allocation will result
1601 * in blocks being allocated at the end of the file. When we allocate new data
1602 * blocks at the end of the file which do not start at the previous data block,
1603 * we will try to align the new blocks at stripe unit boundaries.
1605 * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be
1606 * at, or past the EOF.
1610 struct xfs_bmalloca
*bma
,
1613 struct xfs_bmbt_irec rec
;
1618 error
= xfs_bmap_last_extent(NULL
, bma
->ip
, whichfork
, &rec
,
1629 * Check if we are allocation or past the last extent, or at least into
1630 * the last delayed allocated extent.
1632 bma
->aeof
= bma
->offset
>= rec
.br_startoff
+ rec
.br_blockcount
||
1633 (bma
->offset
>= rec
.br_startoff
&&
1634 isnullstartblock(rec
.br_startblock
));
1639 * Returns the file-relative block number of the first block past eof in
1640 * the file. This is not based on i_size, it is based on the extent records.
1641 * Returns 0 for local files, as they do not have extent records.
1644 xfs_bmap_last_offset(
1645 struct xfs_inode
*ip
,
1646 xfs_fileoff_t
*last_block
,
1649 struct xfs_bmbt_irec rec
;
1655 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
)
1658 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
&&
1659 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
1662 error
= xfs_bmap_last_extent(NULL
, ip
, whichfork
, &rec
, &is_empty
);
1663 if (error
|| is_empty
)
1666 *last_block
= rec
.br_startoff
+ rec
.br_blockcount
;
1671 * Returns whether the selected fork of the inode has exactly one
1672 * block or not. For the data fork we check this matches di_size,
1673 * implying the file's range is 0..bsize-1.
1675 int /* 1=>1 block, 0=>otherwise */
1677 xfs_inode_t
*ip
, /* incore inode */
1678 int whichfork
) /* data or attr fork */
1680 xfs_bmbt_rec_host_t
*ep
; /* ptr to fork's extent */
1681 xfs_ifork_t
*ifp
; /* inode fork pointer */
1682 int rval
; /* return value */
1683 xfs_bmbt_irec_t s
; /* internal version of extent */
1686 if (whichfork
== XFS_DATA_FORK
)
1687 return XFS_ISIZE(ip
) == ip
->i_mount
->m_sb
.sb_blocksize
;
1689 if (XFS_IFORK_NEXTENTS(ip
, whichfork
) != 1)
1691 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
1693 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1694 ASSERT(ifp
->if_flags
& XFS_IFEXTENTS
);
1695 ep
= xfs_iext_get_ext(ifp
, 0);
1696 xfs_bmbt_get_all(ep
, &s
);
1697 rval
= s
.br_startoff
== 0 && s
.br_blockcount
== 1;
1698 if (rval
&& whichfork
== XFS_DATA_FORK
)
1699 ASSERT(XFS_ISIZE(ip
) == ip
->i_mount
->m_sb
.sb_blocksize
);
1704 * Extent tree manipulation functions used during allocation.
1708 * Convert a delayed allocation to a real allocation.
1710 STATIC
int /* error */
1711 xfs_bmap_add_extent_delay_real(
1712 struct xfs_bmalloca
*bma
)
1714 struct xfs_bmbt_irec
*new = &bma
->got
;
1715 int diff
; /* temp value */
1716 xfs_bmbt_rec_host_t
*ep
; /* extent entry for idx */
1717 int error
; /* error return value */
1718 int i
; /* temp state */
1719 xfs_ifork_t
*ifp
; /* inode fork pointer */
1720 xfs_fileoff_t new_endoff
; /* end offset of new entry */
1721 xfs_bmbt_irec_t r
[3]; /* neighbor extent entries */
1722 /* left is 0, right is 1, prev is 2 */
1723 int rval
=0; /* return value (logging flags) */
1724 int state
= 0;/* state bits, accessed thru macros */
1725 xfs_filblks_t da_new
; /* new count del alloc blocks used */
1726 xfs_filblks_t da_old
; /* old count del alloc blocks used */
1727 xfs_filblks_t temp
=0; /* value for da_new calculations */
1728 xfs_filblks_t temp2
=0;/* value for da_new calculations */
1729 int tmp_rval
; /* partial logging flags */
1731 ifp
= XFS_IFORK_PTR(bma
->ip
, XFS_DATA_FORK
);
1733 ASSERT(bma
->idx
>= 0);
1734 ASSERT(bma
->idx
<= ifp
->if_bytes
/ sizeof(struct xfs_bmbt_rec
));
1735 ASSERT(!isnullstartblock(new->br_startblock
));
1737 (bma
->cur
->bc_private
.b
.flags
& XFS_BTCUR_BPRV_WASDEL
));
1739 XFS_STATS_INC(xs_add_exlist
);
1746 * Set up a bunch of variables to make the tests simpler.
1748 ep
= xfs_iext_get_ext(ifp
, bma
->idx
);
1749 xfs_bmbt_get_all(ep
, &PREV
);
1750 new_endoff
= new->br_startoff
+ new->br_blockcount
;
1751 ASSERT(PREV
.br_startoff
<= new->br_startoff
);
1752 ASSERT(PREV
.br_startoff
+ PREV
.br_blockcount
>= new_endoff
);
1754 da_old
= startblockval(PREV
.br_startblock
);
1758 * Set flags determining what part of the previous delayed allocation
1759 * extent is being replaced by a real allocation.
1761 if (PREV
.br_startoff
== new->br_startoff
)
1762 state
|= BMAP_LEFT_FILLING
;
1763 if (PREV
.br_startoff
+ PREV
.br_blockcount
== new_endoff
)
1764 state
|= BMAP_RIGHT_FILLING
;
1767 * Check and set flags if this segment has a left neighbor.
1768 * Don't set contiguous if the combined extent would be too large.
1771 state
|= BMAP_LEFT_VALID
;
1772 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
- 1), &LEFT
);
1774 if (isnullstartblock(LEFT
.br_startblock
))
1775 state
|= BMAP_LEFT_DELAY
;
1778 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
1779 LEFT
.br_startoff
+ LEFT
.br_blockcount
== new->br_startoff
&&
1780 LEFT
.br_startblock
+ LEFT
.br_blockcount
== new->br_startblock
&&
1781 LEFT
.br_state
== new->br_state
&&
1782 LEFT
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
1783 state
|= BMAP_LEFT_CONTIG
;
1786 * Check and set flags if this segment has a right neighbor.
1787 * Don't set contiguous if the combined extent would be too large.
1788 * Also check for all-three-contiguous being too large.
1790 if (bma
->idx
< bma
->ip
->i_df
.if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
) - 1) {
1791 state
|= BMAP_RIGHT_VALID
;
1792 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
+ 1), &RIGHT
);
1794 if (isnullstartblock(RIGHT
.br_startblock
))
1795 state
|= BMAP_RIGHT_DELAY
;
1798 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
1799 new_endoff
== RIGHT
.br_startoff
&&
1800 new->br_startblock
+ new->br_blockcount
== RIGHT
.br_startblock
&&
1801 new->br_state
== RIGHT
.br_state
&&
1802 new->br_blockcount
+ RIGHT
.br_blockcount
<= MAXEXTLEN
&&
1803 ((state
& (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
1804 BMAP_RIGHT_FILLING
)) !=
1805 (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
1806 BMAP_RIGHT_FILLING
) ||
1807 LEFT
.br_blockcount
+ new->br_blockcount
+ RIGHT
.br_blockcount
1809 state
|= BMAP_RIGHT_CONTIG
;
1813 * Switch out based on the FILLING and CONTIG state bits.
1815 switch (state
& (BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
1816 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
)) {
1817 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
1818 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
1820 * Filling in all of a previously delayed allocation extent.
1821 * The left and right neighbors are both contiguous with new.
1824 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1825 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, bma
->idx
),
1826 LEFT
.br_blockcount
+ PREV
.br_blockcount
+
1827 RIGHT
.br_blockcount
);
1828 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1830 xfs_iext_remove(bma
->ip
, bma
->idx
+ 1, 2, state
);
1831 bma
->ip
->i_d
.di_nextents
--;
1832 if (bma
->cur
== NULL
)
1833 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1835 rval
= XFS_ILOG_CORE
;
1836 error
= xfs_bmbt_lookup_eq(bma
->cur
, RIGHT
.br_startoff
,
1837 RIGHT
.br_startblock
,
1838 RIGHT
.br_blockcount
, &i
);
1841 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
1842 error
= xfs_btree_delete(bma
->cur
, &i
);
1845 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
1846 error
= xfs_btree_decrement(bma
->cur
, 0, &i
);
1849 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
1850 error
= xfs_bmbt_update(bma
->cur
, LEFT
.br_startoff
,
1852 LEFT
.br_blockcount
+
1853 PREV
.br_blockcount
+
1854 RIGHT
.br_blockcount
, LEFT
.br_state
);
1860 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
1862 * Filling in all of a previously delayed allocation extent.
1863 * The left neighbor is contiguous, the right is not.
1867 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1868 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, bma
->idx
),
1869 LEFT
.br_blockcount
+ PREV
.br_blockcount
);
1870 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1872 xfs_iext_remove(bma
->ip
, bma
->idx
+ 1, 1, state
);
1873 if (bma
->cur
== NULL
)
1874 rval
= XFS_ILOG_DEXT
;
1877 error
= xfs_bmbt_lookup_eq(bma
->cur
, LEFT
.br_startoff
,
1878 LEFT
.br_startblock
, LEFT
.br_blockcount
,
1882 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
1883 error
= xfs_bmbt_update(bma
->cur
, LEFT
.br_startoff
,
1885 LEFT
.br_blockcount
+
1886 PREV
.br_blockcount
, LEFT
.br_state
);
1892 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
1894 * Filling in all of a previously delayed allocation extent.
1895 * The right neighbor is contiguous, the left is not.
1897 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1898 xfs_bmbt_set_startblock(ep
, new->br_startblock
);
1899 xfs_bmbt_set_blockcount(ep
,
1900 PREV
.br_blockcount
+ RIGHT
.br_blockcount
);
1901 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1903 xfs_iext_remove(bma
->ip
, bma
->idx
+ 1, 1, state
);
1904 if (bma
->cur
== NULL
)
1905 rval
= XFS_ILOG_DEXT
;
1908 error
= xfs_bmbt_lookup_eq(bma
->cur
, RIGHT
.br_startoff
,
1909 RIGHT
.br_startblock
,
1910 RIGHT
.br_blockcount
, &i
);
1913 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
1914 error
= xfs_bmbt_update(bma
->cur
, PREV
.br_startoff
,
1916 PREV
.br_blockcount
+
1917 RIGHT
.br_blockcount
, PREV
.br_state
);
1923 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
1925 * Filling in all of a previously delayed allocation extent.
1926 * Neither the left nor right neighbors are contiguous with
1929 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1930 xfs_bmbt_set_startblock(ep
, new->br_startblock
);
1931 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1933 bma
->ip
->i_d
.di_nextents
++;
1934 if (bma
->cur
== NULL
)
1935 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1937 rval
= XFS_ILOG_CORE
;
1938 error
= xfs_bmbt_lookup_eq(bma
->cur
, new->br_startoff
,
1939 new->br_startblock
, new->br_blockcount
,
1943 XFS_WANT_CORRUPTED_GOTO(i
== 0, done
);
1944 bma
->cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
1945 error
= xfs_btree_insert(bma
->cur
, &i
);
1948 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
1952 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
:
1954 * Filling in the first part of a previous delayed allocation.
1955 * The left neighbor is contiguous.
1957 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
- 1, state
, _THIS_IP_
);
1958 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, bma
->idx
- 1),
1959 LEFT
.br_blockcount
+ new->br_blockcount
);
1960 xfs_bmbt_set_startoff(ep
,
1961 PREV
.br_startoff
+ new->br_blockcount
);
1962 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
- 1, state
, _THIS_IP_
);
1964 temp
= PREV
.br_blockcount
- new->br_blockcount
;
1965 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1966 xfs_bmbt_set_blockcount(ep
, temp
);
1967 if (bma
->cur
== NULL
)
1968 rval
= XFS_ILOG_DEXT
;
1971 error
= xfs_bmbt_lookup_eq(bma
->cur
, LEFT
.br_startoff
,
1972 LEFT
.br_startblock
, LEFT
.br_blockcount
,
1976 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
1977 error
= xfs_bmbt_update(bma
->cur
, LEFT
.br_startoff
,
1979 LEFT
.br_blockcount
+
1985 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
1986 startblockval(PREV
.br_startblock
));
1987 xfs_bmbt_set_startblock(ep
, nullstartblock(da_new
));
1988 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1993 case BMAP_LEFT_FILLING
:
1995 * Filling in the first part of a previous delayed allocation.
1996 * The left neighbor is not contiguous.
1998 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1999 xfs_bmbt_set_startoff(ep
, new_endoff
);
2000 temp
= PREV
.br_blockcount
- new->br_blockcount
;
2001 xfs_bmbt_set_blockcount(ep
, temp
);
2002 xfs_iext_insert(bma
->ip
, bma
->idx
, 1, new, state
);
2003 bma
->ip
->i_d
.di_nextents
++;
2004 if (bma
->cur
== NULL
)
2005 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2007 rval
= XFS_ILOG_CORE
;
2008 error
= xfs_bmbt_lookup_eq(bma
->cur
, new->br_startoff
,
2009 new->br_startblock
, new->br_blockcount
,
2013 XFS_WANT_CORRUPTED_GOTO(i
== 0, done
);
2014 bma
->cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
2015 error
= xfs_btree_insert(bma
->cur
, &i
);
2018 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2021 if (xfs_bmap_needs_btree(bma
->ip
, XFS_DATA_FORK
)) {
2022 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
2023 bma
->firstblock
, bma
->flist
,
2024 &bma
->cur
, 1, &tmp_rval
, XFS_DATA_FORK
);
2029 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
2030 startblockval(PREV
.br_startblock
) -
2031 (bma
->cur
? bma
->cur
->bc_private
.b
.allocated
: 0));
2032 ep
= xfs_iext_get_ext(ifp
, bma
->idx
+ 1);
2033 xfs_bmbt_set_startblock(ep
, nullstartblock(da_new
));
2034 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
+ 1, state
, _THIS_IP_
);
2037 case BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2039 * Filling in the last part of a previous delayed allocation.
2040 * The right neighbor is contiguous with the new allocation.
2042 temp
= PREV
.br_blockcount
- new->br_blockcount
;
2043 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
+ 1, state
, _THIS_IP_
);
2044 xfs_bmbt_set_blockcount(ep
, temp
);
2045 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp
, bma
->idx
+ 1),
2046 new->br_startoff
, new->br_startblock
,
2047 new->br_blockcount
+ RIGHT
.br_blockcount
,
2049 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
+ 1, state
, _THIS_IP_
);
2050 if (bma
->cur
== NULL
)
2051 rval
= XFS_ILOG_DEXT
;
2054 error
= xfs_bmbt_lookup_eq(bma
->cur
, RIGHT
.br_startoff
,
2055 RIGHT
.br_startblock
,
2056 RIGHT
.br_blockcount
, &i
);
2059 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2060 error
= xfs_bmbt_update(bma
->cur
, new->br_startoff
,
2062 new->br_blockcount
+
2063 RIGHT
.br_blockcount
,
2069 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
2070 startblockval(PREV
.br_startblock
));
2071 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2072 xfs_bmbt_set_startblock(ep
, nullstartblock(da_new
));
2073 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2078 case BMAP_RIGHT_FILLING
:
2080 * Filling in the last part of a previous delayed allocation.
2081 * The right neighbor is not contiguous.
2083 temp
= PREV
.br_blockcount
- new->br_blockcount
;
2084 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2085 xfs_bmbt_set_blockcount(ep
, temp
);
2086 xfs_iext_insert(bma
->ip
, bma
->idx
+ 1, 1, new, state
);
2087 bma
->ip
->i_d
.di_nextents
++;
2088 if (bma
->cur
== NULL
)
2089 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2091 rval
= XFS_ILOG_CORE
;
2092 error
= xfs_bmbt_lookup_eq(bma
->cur
, new->br_startoff
,
2093 new->br_startblock
, new->br_blockcount
,
2097 XFS_WANT_CORRUPTED_GOTO(i
== 0, done
);
2098 bma
->cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
2099 error
= xfs_btree_insert(bma
->cur
, &i
);
2102 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2105 if (xfs_bmap_needs_btree(bma
->ip
, XFS_DATA_FORK
)) {
2106 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
2107 bma
->firstblock
, bma
->flist
, &bma
->cur
, 1,
2108 &tmp_rval
, XFS_DATA_FORK
);
2113 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
2114 startblockval(PREV
.br_startblock
) -
2115 (bma
->cur
? bma
->cur
->bc_private
.b
.allocated
: 0));
2116 ep
= xfs_iext_get_ext(ifp
, bma
->idx
);
2117 xfs_bmbt_set_startblock(ep
, nullstartblock(da_new
));
2118 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2125 * Filling in the middle part of a previous delayed allocation.
2126 * Contiguity is impossible here.
2127 * This case is avoided almost all the time.
2129 * We start with a delayed allocation:
2131 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
2134 * and we are allocating:
2135 * +rrrrrrrrrrrrrrrrr+
2138 * and we set it up for insertion as:
2139 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
2141 * PREV @ idx LEFT RIGHT
2142 * inserted at idx + 1
2144 temp
= new->br_startoff
- PREV
.br_startoff
;
2145 temp2
= PREV
.br_startoff
+ PREV
.br_blockcount
- new_endoff
;
2146 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, 0, _THIS_IP_
);
2147 xfs_bmbt_set_blockcount(ep
, temp
); /* truncate PREV */
2149 RIGHT
.br_state
= PREV
.br_state
;
2150 RIGHT
.br_startblock
= nullstartblock(
2151 (int)xfs_bmap_worst_indlen(bma
->ip
, temp2
));
2152 RIGHT
.br_startoff
= new_endoff
;
2153 RIGHT
.br_blockcount
= temp2
;
2154 /* insert LEFT (r[0]) and RIGHT (r[1]) at the same time */
2155 xfs_iext_insert(bma
->ip
, bma
->idx
+ 1, 2, &LEFT
, state
);
2156 bma
->ip
->i_d
.di_nextents
++;
2157 if (bma
->cur
== NULL
)
2158 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2160 rval
= XFS_ILOG_CORE
;
2161 error
= xfs_bmbt_lookup_eq(bma
->cur
, new->br_startoff
,
2162 new->br_startblock
, new->br_blockcount
,
2166 XFS_WANT_CORRUPTED_GOTO(i
== 0, done
);
2167 bma
->cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
2168 error
= xfs_btree_insert(bma
->cur
, &i
);
2171 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2174 if (xfs_bmap_needs_btree(bma
->ip
, XFS_DATA_FORK
)) {
2175 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
2176 bma
->firstblock
, bma
->flist
, &bma
->cur
,
2177 1, &tmp_rval
, XFS_DATA_FORK
);
2182 temp
= xfs_bmap_worst_indlen(bma
->ip
, temp
);
2183 temp2
= xfs_bmap_worst_indlen(bma
->ip
, temp2
);
2184 diff
= (int)(temp
+ temp2
- startblockval(PREV
.br_startblock
) -
2185 (bma
->cur
? bma
->cur
->bc_private
.b
.allocated
: 0));
2187 error
= xfs_icsb_modify_counters(bma
->ip
->i_mount
,
2189 -((int64_t)diff
), 0);
2195 ep
= xfs_iext_get_ext(ifp
, bma
->idx
);
2196 xfs_bmbt_set_startblock(ep
, nullstartblock((int)temp
));
2197 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2198 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
+ 2, state
, _THIS_IP_
);
2199 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp
, bma
->idx
+ 2),
2200 nullstartblock((int)temp2
));
2201 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
+ 2, state
, _THIS_IP_
);
2204 da_new
= temp
+ temp2
;
2207 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2208 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2209 case BMAP_LEFT_FILLING
| BMAP_RIGHT_CONTIG
:
2210 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
2211 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2212 case BMAP_LEFT_CONTIG
:
2213 case BMAP_RIGHT_CONTIG
:
2215 * These cases are all impossible.
2220 /* convert to a btree if necessary */
2221 if (xfs_bmap_needs_btree(bma
->ip
, XFS_DATA_FORK
)) {
2222 int tmp_logflags
; /* partial log flag return val */
2224 ASSERT(bma
->cur
== NULL
);
2225 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
2226 bma
->firstblock
, bma
->flist
, &bma
->cur
,
2227 da_old
> 0, &tmp_logflags
, XFS_DATA_FORK
);
2228 bma
->logflags
|= tmp_logflags
;
2233 /* adjust for changes in reserved delayed indirect blocks */
2234 if (da_old
|| da_new
) {
2237 temp
+= bma
->cur
->bc_private
.b
.allocated
;
2238 ASSERT(temp
<= da_old
);
2240 xfs_icsb_modify_counters(bma
->ip
->i_mount
,
2242 (int64_t)(da_old
- temp
), 0);
2245 /* clear out the allocated field, done with it now in any case. */
2247 bma
->cur
->bc_private
.b
.allocated
= 0;
2249 xfs_bmap_check_leaf_extents(bma
->cur
, bma
->ip
, XFS_DATA_FORK
);
2251 bma
->logflags
|= rval
;
2259 * Convert an unwritten allocation to a real allocation or vice versa.
2261 STATIC
int /* error */
2262 xfs_bmap_add_extent_unwritten_real(
2263 struct xfs_trans
*tp
,
2264 xfs_inode_t
*ip
, /* incore inode pointer */
2265 xfs_extnum_t
*idx
, /* extent number to update/insert */
2266 xfs_btree_cur_t
**curp
, /* if *curp is null, not a btree */
2267 xfs_bmbt_irec_t
*new, /* new data to add to file extents */
2268 xfs_fsblock_t
*first
, /* pointer to firstblock variable */
2269 xfs_bmap_free_t
*flist
, /* list of extents to be freed */
2270 int *logflagsp
) /* inode logging flags */
2272 xfs_btree_cur_t
*cur
; /* btree cursor */
2273 xfs_bmbt_rec_host_t
*ep
; /* extent entry for idx */
2274 int error
; /* error return value */
2275 int i
; /* temp state */
2276 xfs_ifork_t
*ifp
; /* inode fork pointer */
2277 xfs_fileoff_t new_endoff
; /* end offset of new entry */
2278 xfs_exntst_t newext
; /* new extent state */
2279 xfs_exntst_t oldext
; /* old extent state */
2280 xfs_bmbt_irec_t r
[3]; /* neighbor extent entries */
2281 /* left is 0, right is 1, prev is 2 */
2282 int rval
=0; /* return value (logging flags) */
2283 int state
= 0;/* state bits, accessed thru macros */
2288 ifp
= XFS_IFORK_PTR(ip
, XFS_DATA_FORK
);
2291 ASSERT(*idx
<= ifp
->if_bytes
/ sizeof(struct xfs_bmbt_rec
));
2292 ASSERT(!isnullstartblock(new->br_startblock
));
2294 XFS_STATS_INC(xs_add_exlist
);
2301 * Set up a bunch of variables to make the tests simpler.
2304 ep
= xfs_iext_get_ext(ifp
, *idx
);
2305 xfs_bmbt_get_all(ep
, &PREV
);
2306 newext
= new->br_state
;
2307 oldext
= (newext
== XFS_EXT_UNWRITTEN
) ?
2308 XFS_EXT_NORM
: XFS_EXT_UNWRITTEN
;
2309 ASSERT(PREV
.br_state
== oldext
);
2310 new_endoff
= new->br_startoff
+ new->br_blockcount
;
2311 ASSERT(PREV
.br_startoff
<= new->br_startoff
);
2312 ASSERT(PREV
.br_startoff
+ PREV
.br_blockcount
>= new_endoff
);
2315 * Set flags determining what part of the previous oldext allocation
2316 * extent is being replaced by a newext allocation.
2318 if (PREV
.br_startoff
== new->br_startoff
)
2319 state
|= BMAP_LEFT_FILLING
;
2320 if (PREV
.br_startoff
+ PREV
.br_blockcount
== new_endoff
)
2321 state
|= BMAP_RIGHT_FILLING
;
2324 * Check and set flags if this segment has a left neighbor.
2325 * Don't set contiguous if the combined extent would be too large.
2328 state
|= BMAP_LEFT_VALID
;
2329 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, *idx
- 1), &LEFT
);
2331 if (isnullstartblock(LEFT
.br_startblock
))
2332 state
|= BMAP_LEFT_DELAY
;
2335 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
2336 LEFT
.br_startoff
+ LEFT
.br_blockcount
== new->br_startoff
&&
2337 LEFT
.br_startblock
+ LEFT
.br_blockcount
== new->br_startblock
&&
2338 LEFT
.br_state
== newext
&&
2339 LEFT
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
2340 state
|= BMAP_LEFT_CONTIG
;
2343 * Check and set flags if this segment has a right neighbor.
2344 * Don't set contiguous if the combined extent would be too large.
2345 * Also check for all-three-contiguous being too large.
2347 if (*idx
< ip
->i_df
.if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
) - 1) {
2348 state
|= BMAP_RIGHT_VALID
;
2349 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, *idx
+ 1), &RIGHT
);
2350 if (isnullstartblock(RIGHT
.br_startblock
))
2351 state
|= BMAP_RIGHT_DELAY
;
2354 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
2355 new_endoff
== RIGHT
.br_startoff
&&
2356 new->br_startblock
+ new->br_blockcount
== RIGHT
.br_startblock
&&
2357 newext
== RIGHT
.br_state
&&
2358 new->br_blockcount
+ RIGHT
.br_blockcount
<= MAXEXTLEN
&&
2359 ((state
& (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
2360 BMAP_RIGHT_FILLING
)) !=
2361 (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
2362 BMAP_RIGHT_FILLING
) ||
2363 LEFT
.br_blockcount
+ new->br_blockcount
+ RIGHT
.br_blockcount
2365 state
|= BMAP_RIGHT_CONTIG
;
2368 * Switch out based on the FILLING and CONTIG state bits.
2370 switch (state
& (BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
2371 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
)) {
2372 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
2373 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2375 * Setting all of a previous oldext extent to newext.
2376 * The left and right neighbors are both contiguous with new.
2380 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2381 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
),
2382 LEFT
.br_blockcount
+ PREV
.br_blockcount
+
2383 RIGHT
.br_blockcount
);
2384 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2386 xfs_iext_remove(ip
, *idx
+ 1, 2, state
);
2387 ip
->i_d
.di_nextents
-= 2;
2389 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2391 rval
= XFS_ILOG_CORE
;
2392 if ((error
= xfs_bmbt_lookup_eq(cur
, RIGHT
.br_startoff
,
2393 RIGHT
.br_startblock
,
2394 RIGHT
.br_blockcount
, &i
)))
2396 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2397 if ((error
= xfs_btree_delete(cur
, &i
)))
2399 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2400 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2402 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2403 if ((error
= xfs_btree_delete(cur
, &i
)))
2405 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2406 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2408 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2409 if ((error
= xfs_bmbt_update(cur
, LEFT
.br_startoff
,
2411 LEFT
.br_blockcount
+ PREV
.br_blockcount
+
2412 RIGHT
.br_blockcount
, LEFT
.br_state
)))
2417 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
2419 * Setting all of a previous oldext extent to newext.
2420 * The left neighbor is contiguous, the right is not.
2424 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2425 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
),
2426 LEFT
.br_blockcount
+ PREV
.br_blockcount
);
2427 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2429 xfs_iext_remove(ip
, *idx
+ 1, 1, state
);
2430 ip
->i_d
.di_nextents
--;
2432 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2434 rval
= XFS_ILOG_CORE
;
2435 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2436 PREV
.br_startblock
, PREV
.br_blockcount
,
2439 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2440 if ((error
= xfs_btree_delete(cur
, &i
)))
2442 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2443 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2445 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2446 if ((error
= xfs_bmbt_update(cur
, LEFT
.br_startoff
,
2448 LEFT
.br_blockcount
+ PREV
.br_blockcount
,
2454 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2456 * Setting all of a previous oldext extent to newext.
2457 * The right neighbor is contiguous, the left is not.
2459 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2460 xfs_bmbt_set_blockcount(ep
,
2461 PREV
.br_blockcount
+ RIGHT
.br_blockcount
);
2462 xfs_bmbt_set_state(ep
, newext
);
2463 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2464 xfs_iext_remove(ip
, *idx
+ 1, 1, state
);
2465 ip
->i_d
.di_nextents
--;
2467 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2469 rval
= XFS_ILOG_CORE
;
2470 if ((error
= xfs_bmbt_lookup_eq(cur
, RIGHT
.br_startoff
,
2471 RIGHT
.br_startblock
,
2472 RIGHT
.br_blockcount
, &i
)))
2474 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2475 if ((error
= xfs_btree_delete(cur
, &i
)))
2477 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2478 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2480 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2481 if ((error
= xfs_bmbt_update(cur
, new->br_startoff
,
2483 new->br_blockcount
+ RIGHT
.br_blockcount
,
2489 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
2491 * Setting all of a previous oldext extent to newext.
2492 * Neither the left nor right neighbors are contiguous with
2495 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2496 xfs_bmbt_set_state(ep
, newext
);
2497 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2500 rval
= XFS_ILOG_DEXT
;
2503 if ((error
= xfs_bmbt_lookup_eq(cur
, new->br_startoff
,
2504 new->br_startblock
, new->br_blockcount
,
2507 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2508 if ((error
= xfs_bmbt_update(cur
, new->br_startoff
,
2509 new->br_startblock
, new->br_blockcount
,
2515 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
:
2517 * Setting the first part of a previous oldext extent to newext.
2518 * The left neighbor is contiguous.
2520 trace_xfs_bmap_pre_update(ip
, *idx
- 1, state
, _THIS_IP_
);
2521 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
- 1),
2522 LEFT
.br_blockcount
+ new->br_blockcount
);
2523 xfs_bmbt_set_startoff(ep
,
2524 PREV
.br_startoff
+ new->br_blockcount
);
2525 trace_xfs_bmap_post_update(ip
, *idx
- 1, state
, _THIS_IP_
);
2527 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2528 xfs_bmbt_set_startblock(ep
,
2529 new->br_startblock
+ new->br_blockcount
);
2530 xfs_bmbt_set_blockcount(ep
,
2531 PREV
.br_blockcount
- new->br_blockcount
);
2532 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2537 rval
= XFS_ILOG_DEXT
;
2540 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2541 PREV
.br_startblock
, PREV
.br_blockcount
,
2544 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2545 if ((error
= xfs_bmbt_update(cur
,
2546 PREV
.br_startoff
+ new->br_blockcount
,
2547 PREV
.br_startblock
+ new->br_blockcount
,
2548 PREV
.br_blockcount
- new->br_blockcount
,
2551 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2553 error
= xfs_bmbt_update(cur
, LEFT
.br_startoff
,
2555 LEFT
.br_blockcount
+ new->br_blockcount
,
2562 case BMAP_LEFT_FILLING
:
2564 * Setting the first part of a previous oldext extent to newext.
2565 * The left neighbor is not contiguous.
2567 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2568 ASSERT(ep
&& xfs_bmbt_get_state(ep
) == oldext
);
2569 xfs_bmbt_set_startoff(ep
, new_endoff
);
2570 xfs_bmbt_set_blockcount(ep
,
2571 PREV
.br_blockcount
- new->br_blockcount
);
2572 xfs_bmbt_set_startblock(ep
,
2573 new->br_startblock
+ new->br_blockcount
);
2574 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2576 xfs_iext_insert(ip
, *idx
, 1, new, state
);
2577 ip
->i_d
.di_nextents
++;
2579 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2581 rval
= XFS_ILOG_CORE
;
2582 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2583 PREV
.br_startblock
, PREV
.br_blockcount
,
2586 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2587 if ((error
= xfs_bmbt_update(cur
,
2588 PREV
.br_startoff
+ new->br_blockcount
,
2589 PREV
.br_startblock
+ new->br_blockcount
,
2590 PREV
.br_blockcount
- new->br_blockcount
,
2593 cur
->bc_rec
.b
= *new;
2594 if ((error
= xfs_btree_insert(cur
, &i
)))
2596 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2600 case BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2602 * Setting the last part of a previous oldext extent to newext.
2603 * The right neighbor is contiguous with the new allocation.
2605 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2606 xfs_bmbt_set_blockcount(ep
,
2607 PREV
.br_blockcount
- new->br_blockcount
);
2608 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2612 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2613 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp
, *idx
),
2614 new->br_startoff
, new->br_startblock
,
2615 new->br_blockcount
+ RIGHT
.br_blockcount
, newext
);
2616 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2619 rval
= XFS_ILOG_DEXT
;
2622 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2624 PREV
.br_blockcount
, &i
)))
2626 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2627 if ((error
= xfs_bmbt_update(cur
, PREV
.br_startoff
,
2629 PREV
.br_blockcount
- new->br_blockcount
,
2632 if ((error
= xfs_btree_increment(cur
, 0, &i
)))
2634 if ((error
= xfs_bmbt_update(cur
, new->br_startoff
,
2636 new->br_blockcount
+ RIGHT
.br_blockcount
,
2642 case BMAP_RIGHT_FILLING
:
2644 * Setting the last part of a previous oldext extent to newext.
2645 * The right neighbor is not contiguous.
2647 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2648 xfs_bmbt_set_blockcount(ep
,
2649 PREV
.br_blockcount
- new->br_blockcount
);
2650 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2653 xfs_iext_insert(ip
, *idx
, 1, new, state
);
2655 ip
->i_d
.di_nextents
++;
2657 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2659 rval
= XFS_ILOG_CORE
;
2660 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2661 PREV
.br_startblock
, PREV
.br_blockcount
,
2664 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2665 if ((error
= xfs_bmbt_update(cur
, PREV
.br_startoff
,
2667 PREV
.br_blockcount
- new->br_blockcount
,
2670 if ((error
= xfs_bmbt_lookup_eq(cur
, new->br_startoff
,
2671 new->br_startblock
, new->br_blockcount
,
2674 XFS_WANT_CORRUPTED_GOTO(i
== 0, done
);
2675 cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
2676 if ((error
= xfs_btree_insert(cur
, &i
)))
2678 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2684 * Setting the middle part of a previous oldext extent to
2685 * newext. Contiguity is impossible here.
2686 * One extent becomes three extents.
2688 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2689 xfs_bmbt_set_blockcount(ep
,
2690 new->br_startoff
- PREV
.br_startoff
);
2691 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2694 r
[1].br_startoff
= new_endoff
;
2695 r
[1].br_blockcount
=
2696 PREV
.br_startoff
+ PREV
.br_blockcount
- new_endoff
;
2697 r
[1].br_startblock
= new->br_startblock
+ new->br_blockcount
;
2698 r
[1].br_state
= oldext
;
2701 xfs_iext_insert(ip
, *idx
, 2, &r
[0], state
);
2703 ip
->i_d
.di_nextents
+= 2;
2705 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2707 rval
= XFS_ILOG_CORE
;
2708 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2709 PREV
.br_startblock
, PREV
.br_blockcount
,
2712 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2713 /* new right extent - oldext */
2714 if ((error
= xfs_bmbt_update(cur
, r
[1].br_startoff
,
2715 r
[1].br_startblock
, r
[1].br_blockcount
,
2718 /* new left extent - oldext */
2719 cur
->bc_rec
.b
= PREV
;
2720 cur
->bc_rec
.b
.br_blockcount
=
2721 new->br_startoff
- PREV
.br_startoff
;
2722 if ((error
= xfs_btree_insert(cur
, &i
)))
2724 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2726 * Reset the cursor to the position of the new extent
2727 * we are about to insert as we can't trust it after
2728 * the previous insert.
2730 if ((error
= xfs_bmbt_lookup_eq(cur
, new->br_startoff
,
2731 new->br_startblock
, new->br_blockcount
,
2734 XFS_WANT_CORRUPTED_GOTO(i
== 0, done
);
2735 /* new middle extent - newext */
2736 cur
->bc_rec
.b
.br_state
= new->br_state
;
2737 if ((error
= xfs_btree_insert(cur
, &i
)))
2739 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2743 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2744 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2745 case BMAP_LEFT_FILLING
| BMAP_RIGHT_CONTIG
:
2746 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
2747 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2748 case BMAP_LEFT_CONTIG
:
2749 case BMAP_RIGHT_CONTIG
:
2751 * These cases are all impossible.
2756 /* convert to a btree if necessary */
2757 if (xfs_bmap_needs_btree(ip
, XFS_DATA_FORK
)) {
2758 int tmp_logflags
; /* partial log flag return val */
2760 ASSERT(cur
== NULL
);
2761 error
= xfs_bmap_extents_to_btree(tp
, ip
, first
, flist
, &cur
,
2762 0, &tmp_logflags
, XFS_DATA_FORK
);
2763 *logflagsp
|= tmp_logflags
;
2768 /* clear out the allocated field, done with it now in any case. */
2770 cur
->bc_private
.b
.allocated
= 0;
2774 xfs_bmap_check_leaf_extents(*curp
, ip
, XFS_DATA_FORK
);
2784 * Convert a hole to a delayed allocation.
2787 xfs_bmap_add_extent_hole_delay(
2788 xfs_inode_t
*ip
, /* incore inode pointer */
2789 xfs_extnum_t
*idx
, /* extent number to update/insert */
2790 xfs_bmbt_irec_t
*new) /* new data to add to file extents */
2792 xfs_ifork_t
*ifp
; /* inode fork pointer */
2793 xfs_bmbt_irec_t left
; /* left neighbor extent entry */
2794 xfs_filblks_t newlen
=0; /* new indirect size */
2795 xfs_filblks_t oldlen
=0; /* old indirect size */
2796 xfs_bmbt_irec_t right
; /* right neighbor extent entry */
2797 int state
; /* state bits, accessed thru macros */
2798 xfs_filblks_t temp
=0; /* temp for indirect calculations */
2800 ifp
= XFS_IFORK_PTR(ip
, XFS_DATA_FORK
);
2802 ASSERT(isnullstartblock(new->br_startblock
));
2805 * Check and set flags if this segment has a left neighbor
2808 state
|= BMAP_LEFT_VALID
;
2809 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, *idx
- 1), &left
);
2811 if (isnullstartblock(left
.br_startblock
))
2812 state
|= BMAP_LEFT_DELAY
;
2816 * Check and set flags if the current (right) segment exists.
2817 * If it doesn't exist, we're converting the hole at end-of-file.
2819 if (*idx
< ip
->i_df
.if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
)) {
2820 state
|= BMAP_RIGHT_VALID
;
2821 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, *idx
), &right
);
2823 if (isnullstartblock(right
.br_startblock
))
2824 state
|= BMAP_RIGHT_DELAY
;
2828 * Set contiguity flags on the left and right neighbors.
2829 * Don't let extents get too large, even if the pieces are contiguous.
2831 if ((state
& BMAP_LEFT_VALID
) && (state
& BMAP_LEFT_DELAY
) &&
2832 left
.br_startoff
+ left
.br_blockcount
== new->br_startoff
&&
2833 left
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
2834 state
|= BMAP_LEFT_CONTIG
;
2836 if ((state
& BMAP_RIGHT_VALID
) && (state
& BMAP_RIGHT_DELAY
) &&
2837 new->br_startoff
+ new->br_blockcount
== right
.br_startoff
&&
2838 new->br_blockcount
+ right
.br_blockcount
<= MAXEXTLEN
&&
2839 (!(state
& BMAP_LEFT_CONTIG
) ||
2840 (left
.br_blockcount
+ new->br_blockcount
+
2841 right
.br_blockcount
<= MAXEXTLEN
)))
2842 state
|= BMAP_RIGHT_CONTIG
;
2845 * Switch out based on the contiguity flags.
2847 switch (state
& (BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
)) {
2848 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2850 * New allocation is contiguous with delayed allocations
2851 * on the left and on the right.
2852 * Merge all three into a single extent record.
2855 temp
= left
.br_blockcount
+ new->br_blockcount
+
2856 right
.br_blockcount
;
2858 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2859 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
), temp
);
2860 oldlen
= startblockval(left
.br_startblock
) +
2861 startblockval(new->br_startblock
) +
2862 startblockval(right
.br_startblock
);
2863 newlen
= xfs_bmap_worst_indlen(ip
, temp
);
2864 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp
, *idx
),
2865 nullstartblock((int)newlen
));
2866 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2868 xfs_iext_remove(ip
, *idx
+ 1, 1, state
);
2871 case BMAP_LEFT_CONTIG
:
2873 * New allocation is contiguous with a delayed allocation
2875 * Merge the new allocation with the left neighbor.
2878 temp
= left
.br_blockcount
+ new->br_blockcount
;
2880 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2881 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
), temp
);
2882 oldlen
= startblockval(left
.br_startblock
) +
2883 startblockval(new->br_startblock
);
2884 newlen
= xfs_bmap_worst_indlen(ip
, temp
);
2885 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp
, *idx
),
2886 nullstartblock((int)newlen
));
2887 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2890 case BMAP_RIGHT_CONTIG
:
2892 * New allocation is contiguous with a delayed allocation
2894 * Merge the new allocation with the right neighbor.
2896 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2897 temp
= new->br_blockcount
+ right
.br_blockcount
;
2898 oldlen
= startblockval(new->br_startblock
) +
2899 startblockval(right
.br_startblock
);
2900 newlen
= xfs_bmap_worst_indlen(ip
, temp
);
2901 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp
, *idx
),
2903 nullstartblock((int)newlen
), temp
, right
.br_state
);
2904 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2909 * New allocation is not contiguous with another
2910 * delayed allocation.
2911 * Insert a new entry.
2913 oldlen
= newlen
= 0;
2914 xfs_iext_insert(ip
, *idx
, 1, new, state
);
2917 if (oldlen
!= newlen
) {
2918 ASSERT(oldlen
> newlen
);
2919 xfs_icsb_modify_counters(ip
->i_mount
, XFS_SBS_FDBLOCKS
,
2920 (int64_t)(oldlen
- newlen
), 0);
2922 * Nothing to do for disk quota accounting here.
2928 * Convert a hole to a real allocation.
2930 STATIC
int /* error */
2931 xfs_bmap_add_extent_hole_real(
2932 struct xfs_bmalloca
*bma
,
2935 struct xfs_bmbt_irec
*new = &bma
->got
;
2936 int error
; /* error return value */
2937 int i
; /* temp state */
2938 xfs_ifork_t
*ifp
; /* inode fork pointer */
2939 xfs_bmbt_irec_t left
; /* left neighbor extent entry */
2940 xfs_bmbt_irec_t right
; /* right neighbor extent entry */
2941 int rval
=0; /* return value (logging flags) */
2942 int state
; /* state bits, accessed thru macros */
2944 ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
2946 ASSERT(bma
->idx
>= 0);
2947 ASSERT(bma
->idx
<= ifp
->if_bytes
/ sizeof(struct xfs_bmbt_rec
));
2948 ASSERT(!isnullstartblock(new->br_startblock
));
2950 !(bma
->cur
->bc_private
.b
.flags
& XFS_BTCUR_BPRV_WASDEL
));
2952 XFS_STATS_INC(xs_add_exlist
);
2955 if (whichfork
== XFS_ATTR_FORK
)
2956 state
|= BMAP_ATTRFORK
;
2959 * Check and set flags if this segment has a left neighbor.
2962 state
|= BMAP_LEFT_VALID
;
2963 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
- 1), &left
);
2964 if (isnullstartblock(left
.br_startblock
))
2965 state
|= BMAP_LEFT_DELAY
;
2969 * Check and set flags if this segment has a current value.
2970 * Not true if we're inserting into the "hole" at eof.
2972 if (bma
->idx
< ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
)) {
2973 state
|= BMAP_RIGHT_VALID
;
2974 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
), &right
);
2975 if (isnullstartblock(right
.br_startblock
))
2976 state
|= BMAP_RIGHT_DELAY
;
2980 * We're inserting a real allocation between "left" and "right".
2981 * Set the contiguity flags. Don't let extents get too large.
2983 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
2984 left
.br_startoff
+ left
.br_blockcount
== new->br_startoff
&&
2985 left
.br_startblock
+ left
.br_blockcount
== new->br_startblock
&&
2986 left
.br_state
== new->br_state
&&
2987 left
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
2988 state
|= BMAP_LEFT_CONTIG
;
2990 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
2991 new->br_startoff
+ new->br_blockcount
== right
.br_startoff
&&
2992 new->br_startblock
+ new->br_blockcount
== right
.br_startblock
&&
2993 new->br_state
== right
.br_state
&&
2994 new->br_blockcount
+ right
.br_blockcount
<= MAXEXTLEN
&&
2995 (!(state
& BMAP_LEFT_CONTIG
) ||
2996 left
.br_blockcount
+ new->br_blockcount
+
2997 right
.br_blockcount
<= MAXEXTLEN
))
2998 state
|= BMAP_RIGHT_CONTIG
;
3002 * Select which case we're in here, and implement it.
3004 switch (state
& (BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
)) {
3005 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
3007 * New allocation is contiguous with real allocations on the
3008 * left and on the right.
3009 * Merge all three into a single extent record.
3012 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
3013 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, bma
->idx
),
3014 left
.br_blockcount
+ new->br_blockcount
+
3015 right
.br_blockcount
);
3016 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
3018 xfs_iext_remove(bma
->ip
, bma
->idx
+ 1, 1, state
);
3020 XFS_IFORK_NEXT_SET(bma
->ip
, whichfork
,
3021 XFS_IFORK_NEXTENTS(bma
->ip
, whichfork
) - 1);
3022 if (bma
->cur
== NULL
) {
3023 rval
= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
3025 rval
= XFS_ILOG_CORE
;
3026 error
= xfs_bmbt_lookup_eq(bma
->cur
, right
.br_startoff
,
3027 right
.br_startblock
, right
.br_blockcount
,
3031 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
3032 error
= xfs_btree_delete(bma
->cur
, &i
);
3035 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
3036 error
= xfs_btree_decrement(bma
->cur
, 0, &i
);
3039 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
3040 error
= xfs_bmbt_update(bma
->cur
, left
.br_startoff
,
3042 left
.br_blockcount
+
3043 new->br_blockcount
+
3044 right
.br_blockcount
,
3051 case BMAP_LEFT_CONTIG
:
3053 * New allocation is contiguous with a real allocation
3055 * Merge the new allocation with the left neighbor.
3058 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
3059 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, bma
->idx
),
3060 left
.br_blockcount
+ new->br_blockcount
);
3061 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
3063 if (bma
->cur
== NULL
) {
3064 rval
= xfs_ilog_fext(whichfork
);
3067 error
= xfs_bmbt_lookup_eq(bma
->cur
, left
.br_startoff
,
3068 left
.br_startblock
, left
.br_blockcount
,
3072 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
3073 error
= xfs_bmbt_update(bma
->cur
, left
.br_startoff
,
3075 left
.br_blockcount
+
3083 case BMAP_RIGHT_CONTIG
:
3085 * New allocation is contiguous with a real allocation
3087 * Merge the new allocation with the right neighbor.
3089 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
3090 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp
, bma
->idx
),
3091 new->br_startoff
, new->br_startblock
,
3092 new->br_blockcount
+ right
.br_blockcount
,
3094 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
3096 if (bma
->cur
== NULL
) {
3097 rval
= xfs_ilog_fext(whichfork
);
3100 error
= xfs_bmbt_lookup_eq(bma
->cur
,
3102 right
.br_startblock
,
3103 right
.br_blockcount
, &i
);
3106 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
3107 error
= xfs_bmbt_update(bma
->cur
, new->br_startoff
,
3109 new->br_blockcount
+
3110 right
.br_blockcount
,
3119 * New allocation is not contiguous with another
3121 * Insert a new entry.
3123 xfs_iext_insert(bma
->ip
, bma
->idx
, 1, new, state
);
3124 XFS_IFORK_NEXT_SET(bma
->ip
, whichfork
,
3125 XFS_IFORK_NEXTENTS(bma
->ip
, whichfork
) + 1);
3126 if (bma
->cur
== NULL
) {
3127 rval
= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
3129 rval
= XFS_ILOG_CORE
;
3130 error
= xfs_bmbt_lookup_eq(bma
->cur
,
3133 new->br_blockcount
, &i
);
3136 XFS_WANT_CORRUPTED_GOTO(i
== 0, done
);
3137 bma
->cur
->bc_rec
.b
.br_state
= new->br_state
;
3138 error
= xfs_btree_insert(bma
->cur
, &i
);
3141 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
3146 /* convert to a btree if necessary */
3147 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
3148 int tmp_logflags
; /* partial log flag return val */
3150 ASSERT(bma
->cur
== NULL
);
3151 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
3152 bma
->firstblock
, bma
->flist
, &bma
->cur
,
3153 0, &tmp_logflags
, whichfork
);
3154 bma
->logflags
|= tmp_logflags
;
3159 /* clear out the allocated field, done with it now in any case. */
3161 bma
->cur
->bc_private
.b
.allocated
= 0;
3163 xfs_bmap_check_leaf_extents(bma
->cur
, bma
->ip
, whichfork
);
3165 bma
->logflags
|= rval
;
3170 * Functions used in the extent read, allocate and remove paths
3174 * Adjust the size of the new extent based on di_extsize and rt extsize.
3177 xfs_bmap_extsize_align(
3179 xfs_bmbt_irec_t
*gotp
, /* next extent pointer */
3180 xfs_bmbt_irec_t
*prevp
, /* previous extent pointer */
3181 xfs_extlen_t extsz
, /* align to this extent size */
3182 int rt
, /* is this a realtime inode? */
3183 int eof
, /* is extent at end-of-file? */
3184 int delay
, /* creating delalloc extent? */
3185 int convert
, /* overwriting unwritten extent? */
3186 xfs_fileoff_t
*offp
, /* in/out: aligned offset */
3187 xfs_extlen_t
*lenp
) /* in/out: aligned length */
3189 xfs_fileoff_t orig_off
; /* original offset */
3190 xfs_extlen_t orig_alen
; /* original length */
3191 xfs_fileoff_t orig_end
; /* original off+len */
3192 xfs_fileoff_t nexto
; /* next file offset */
3193 xfs_fileoff_t prevo
; /* previous file offset */
3194 xfs_fileoff_t align_off
; /* temp for offset */
3195 xfs_extlen_t align_alen
; /* temp for length */
3196 xfs_extlen_t temp
; /* temp for calculations */
3201 orig_off
= align_off
= *offp
;
3202 orig_alen
= align_alen
= *lenp
;
3203 orig_end
= orig_off
+ orig_alen
;
3206 * If this request overlaps an existing extent, then don't
3207 * attempt to perform any additional alignment.
3209 if (!delay
&& !eof
&&
3210 (orig_off
>= gotp
->br_startoff
) &&
3211 (orig_end
<= gotp
->br_startoff
+ gotp
->br_blockcount
)) {
3216 * If the file offset is unaligned vs. the extent size
3217 * we need to align it. This will be possible unless
3218 * the file was previously written with a kernel that didn't
3219 * perform this alignment, or if a truncate shot us in the
3222 temp
= do_mod(orig_off
, extsz
);
3228 * Same adjustment for the end of the requested area.
3230 if ((temp
= (align_alen
% extsz
))) {
3231 align_alen
+= extsz
- temp
;
3234 * If the previous block overlaps with this proposed allocation
3235 * then move the start forward without adjusting the length.
3237 if (prevp
->br_startoff
!= NULLFILEOFF
) {
3238 if (prevp
->br_startblock
== HOLESTARTBLOCK
)
3239 prevo
= prevp
->br_startoff
;
3241 prevo
= prevp
->br_startoff
+ prevp
->br_blockcount
;
3244 if (align_off
!= orig_off
&& align_off
< prevo
)
3247 * If the next block overlaps with this proposed allocation
3248 * then move the start back without adjusting the length,
3249 * but not before offset 0.
3250 * This may of course make the start overlap previous block,
3251 * and if we hit the offset 0 limit then the next block
3252 * can still overlap too.
3254 if (!eof
&& gotp
->br_startoff
!= NULLFILEOFF
) {
3255 if ((delay
&& gotp
->br_startblock
== HOLESTARTBLOCK
) ||
3256 (!delay
&& gotp
->br_startblock
== DELAYSTARTBLOCK
))
3257 nexto
= gotp
->br_startoff
+ gotp
->br_blockcount
;
3259 nexto
= gotp
->br_startoff
;
3261 nexto
= NULLFILEOFF
;
3263 align_off
+ align_alen
!= orig_end
&&
3264 align_off
+ align_alen
> nexto
)
3265 align_off
= nexto
> align_alen
? nexto
- align_alen
: 0;
3267 * If we're now overlapping the next or previous extent that
3268 * means we can't fit an extsz piece in this hole. Just move
3269 * the start forward to the first valid spot and set
3270 * the length so we hit the end.
3272 if (align_off
!= orig_off
&& align_off
< prevo
)
3274 if (align_off
+ align_alen
!= orig_end
&&
3275 align_off
+ align_alen
> nexto
&&
3276 nexto
!= NULLFILEOFF
) {
3277 ASSERT(nexto
> prevo
);
3278 align_alen
= nexto
- align_off
;
3282 * If realtime, and the result isn't a multiple of the realtime
3283 * extent size we need to remove blocks until it is.
3285 if (rt
&& (temp
= (align_alen
% mp
->m_sb
.sb_rextsize
))) {
3287 * We're not covering the original request, or
3288 * we won't be able to once we fix the length.
3290 if (orig_off
< align_off
||
3291 orig_end
> align_off
+ align_alen
||
3292 align_alen
- temp
< orig_alen
)
3295 * Try to fix it by moving the start up.
3297 if (align_off
+ temp
<= orig_off
) {
3302 * Try to fix it by moving the end in.
3304 else if (align_off
+ align_alen
- temp
>= orig_end
)
3307 * Set the start to the minimum then trim the length.
3310 align_alen
-= orig_off
- align_off
;
3311 align_off
= orig_off
;
3312 align_alen
-= align_alen
% mp
->m_sb
.sb_rextsize
;
3315 * Result doesn't cover the request, fail it.
3317 if (orig_off
< align_off
|| orig_end
> align_off
+ align_alen
)
3320 ASSERT(orig_off
>= align_off
);
3321 ASSERT(orig_end
<= align_off
+ align_alen
);
3325 if (!eof
&& gotp
->br_startoff
!= NULLFILEOFF
)
3326 ASSERT(align_off
+ align_alen
<= gotp
->br_startoff
);
3327 if (prevp
->br_startoff
!= NULLFILEOFF
)
3328 ASSERT(align_off
>= prevp
->br_startoff
+ prevp
->br_blockcount
);
3336 #define XFS_ALLOC_GAP_UNITS 4
3340 struct xfs_bmalloca
*ap
) /* bmap alloc argument struct */
3342 xfs_fsblock_t adjust
; /* adjustment to block numbers */
3343 xfs_agnumber_t fb_agno
; /* ag number of ap->firstblock */
3344 xfs_mount_t
*mp
; /* mount point structure */
3345 int nullfb
; /* true if ap->firstblock isn't set */
3346 int rt
; /* true if inode is realtime */
3348 #define ISVALID(x,y) \
3350 (x) < mp->m_sb.sb_rblocks : \
3351 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
3352 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
3353 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
3355 mp
= ap
->ip
->i_mount
;
3356 nullfb
= *ap
->firstblock
== NULLFSBLOCK
;
3357 rt
= XFS_IS_REALTIME_INODE(ap
->ip
) && ap
->userdata
;
3358 fb_agno
= nullfb
? NULLAGNUMBER
: XFS_FSB_TO_AGNO(mp
, *ap
->firstblock
);
3360 * If allocating at eof, and there's a previous real block,
3361 * try to use its last block as our starting point.
3363 if (ap
->eof
&& ap
->prev
.br_startoff
!= NULLFILEOFF
&&
3364 !isnullstartblock(ap
->prev
.br_startblock
) &&
3365 ISVALID(ap
->prev
.br_startblock
+ ap
->prev
.br_blockcount
,
3366 ap
->prev
.br_startblock
)) {
3367 ap
->blkno
= ap
->prev
.br_startblock
+ ap
->prev
.br_blockcount
;
3369 * Adjust for the gap between prevp and us.
3371 adjust
= ap
->offset
-
3372 (ap
->prev
.br_startoff
+ ap
->prev
.br_blockcount
);
3374 ISVALID(ap
->blkno
+ adjust
, ap
->prev
.br_startblock
))
3375 ap
->blkno
+= adjust
;
3378 * If not at eof, then compare the two neighbor blocks.
3379 * Figure out whether either one gives us a good starting point,
3380 * and pick the better one.
3382 else if (!ap
->eof
) {
3383 xfs_fsblock_t gotbno
; /* right side block number */
3384 xfs_fsblock_t gotdiff
=0; /* right side difference */
3385 xfs_fsblock_t prevbno
; /* left side block number */
3386 xfs_fsblock_t prevdiff
=0; /* left side difference */
3389 * If there's a previous (left) block, select a requested
3390 * start block based on it.
3392 if (ap
->prev
.br_startoff
!= NULLFILEOFF
&&
3393 !isnullstartblock(ap
->prev
.br_startblock
) &&
3394 (prevbno
= ap
->prev
.br_startblock
+
3395 ap
->prev
.br_blockcount
) &&
3396 ISVALID(prevbno
, ap
->prev
.br_startblock
)) {
3398 * Calculate gap to end of previous block.
3400 adjust
= prevdiff
= ap
->offset
-
3401 (ap
->prev
.br_startoff
+
3402 ap
->prev
.br_blockcount
);
3404 * Figure the startblock based on the previous block's
3405 * end and the gap size.
3407 * If the gap is large relative to the piece we're
3408 * allocating, or using it gives us an invalid block
3409 * number, then just use the end of the previous block.
3411 if (prevdiff
<= XFS_ALLOC_GAP_UNITS
* ap
->length
&&
3412 ISVALID(prevbno
+ prevdiff
,
3413 ap
->prev
.br_startblock
))
3418 * If the firstblock forbids it, can't use it,
3421 if (!rt
&& !nullfb
&&
3422 XFS_FSB_TO_AGNO(mp
, prevbno
) != fb_agno
)
3423 prevbno
= NULLFSBLOCK
;
3426 * No previous block or can't follow it, just default.
3429 prevbno
= NULLFSBLOCK
;
3431 * If there's a following (right) block, select a requested
3432 * start block based on it.
3434 if (!isnullstartblock(ap
->got
.br_startblock
)) {
3436 * Calculate gap to start of next block.
3438 adjust
= gotdiff
= ap
->got
.br_startoff
- ap
->offset
;
3440 * Figure the startblock based on the next block's
3441 * start and the gap size.
3443 gotbno
= ap
->got
.br_startblock
;
3446 * If the gap is large relative to the piece we're
3447 * allocating, or using it gives us an invalid block
3448 * number, then just use the start of the next block
3449 * offset by our length.
3451 if (gotdiff
<= XFS_ALLOC_GAP_UNITS
* ap
->length
&&
3452 ISVALID(gotbno
- gotdiff
, gotbno
))
3454 else if (ISVALID(gotbno
- ap
->length
, gotbno
)) {
3455 gotbno
-= ap
->length
;
3456 gotdiff
+= adjust
- ap
->length
;
3460 * If the firstblock forbids it, can't use it,
3463 if (!rt
&& !nullfb
&&
3464 XFS_FSB_TO_AGNO(mp
, gotbno
) != fb_agno
)
3465 gotbno
= NULLFSBLOCK
;
3468 * No next block, just default.
3471 gotbno
= NULLFSBLOCK
;
3473 * If both valid, pick the better one, else the only good
3474 * one, else ap->blkno is already set (to 0 or the inode block).
3476 if (prevbno
!= NULLFSBLOCK
&& gotbno
!= NULLFSBLOCK
)
3477 ap
->blkno
= prevdiff
<= gotdiff
? prevbno
: gotbno
;
3478 else if (prevbno
!= NULLFSBLOCK
)
3479 ap
->blkno
= prevbno
;
3480 else if (gotbno
!= NULLFSBLOCK
)
3487 xfs_bmap_longest_free_extent(
3488 struct xfs_trans
*tp
,
3493 struct xfs_mount
*mp
= tp
->t_mountp
;
3494 struct xfs_perag
*pag
;
3495 xfs_extlen_t longest
;
3498 pag
= xfs_perag_get(mp
, ag
);
3499 if (!pag
->pagf_init
) {
3500 error
= xfs_alloc_pagf_init(mp
, tp
, ag
, XFS_ALLOC_FLAG_TRYLOCK
);
3504 if (!pag
->pagf_init
) {
3510 longest
= xfs_alloc_longest_free_extent(mp
, pag
);
3511 if (*blen
< longest
)
3520 xfs_bmap_select_minlen(
3521 struct xfs_bmalloca
*ap
,
3522 struct xfs_alloc_arg
*args
,
3526 if (notinit
|| *blen
< ap
->minlen
) {
3528 * Since we did a BUF_TRYLOCK above, it is possible that
3529 * there is space for this request.
3531 args
->minlen
= ap
->minlen
;
3532 } else if (*blen
< args
->maxlen
) {
3534 * If the best seen length is less than the request length,
3535 * use the best as the minimum.
3537 args
->minlen
= *blen
;
3540 * Otherwise we've seen an extent as big as maxlen, use that
3543 args
->minlen
= args
->maxlen
;
3548 xfs_bmap_btalloc_nullfb(
3549 struct xfs_bmalloca
*ap
,
3550 struct xfs_alloc_arg
*args
,
3553 struct xfs_mount
*mp
= ap
->ip
->i_mount
;
3554 xfs_agnumber_t ag
, startag
;
3558 args
->type
= XFS_ALLOCTYPE_START_BNO
;
3559 args
->total
= ap
->total
;
3561 startag
= ag
= XFS_FSB_TO_AGNO(mp
, args
->fsbno
);
3562 if (startag
== NULLAGNUMBER
)
3565 while (*blen
< args
->maxlen
) {
3566 error
= xfs_bmap_longest_free_extent(args
->tp
, ag
, blen
,
3571 if (++ag
== mp
->m_sb
.sb_agcount
)
3577 xfs_bmap_select_minlen(ap
, args
, blen
, notinit
);
3582 xfs_bmap_btalloc_filestreams(
3583 struct xfs_bmalloca
*ap
,
3584 struct xfs_alloc_arg
*args
,
3587 struct xfs_mount
*mp
= ap
->ip
->i_mount
;
3592 args
->type
= XFS_ALLOCTYPE_NEAR_BNO
;
3593 args
->total
= ap
->total
;
3595 ag
= XFS_FSB_TO_AGNO(mp
, args
->fsbno
);
3596 if (ag
== NULLAGNUMBER
)
3599 error
= xfs_bmap_longest_free_extent(args
->tp
, ag
, blen
, ¬init
);
3603 if (*blen
< args
->maxlen
) {
3604 error
= xfs_filestream_new_ag(ap
, &ag
);
3608 error
= xfs_bmap_longest_free_extent(args
->tp
, ag
, blen
,
3615 xfs_bmap_select_minlen(ap
, args
, blen
, notinit
);
3618 * Set the failure fallback case to look in the selected AG as stream
3621 ap
->blkno
= args
->fsbno
= XFS_AGB_TO_FSB(mp
, ag
, 0);
3627 struct xfs_bmalloca
*ap
) /* bmap alloc argument struct */
3629 xfs_mount_t
*mp
; /* mount point structure */
3630 xfs_alloctype_t atype
= 0; /* type for allocation routines */
3631 xfs_extlen_t align
; /* minimum allocation alignment */
3632 xfs_agnumber_t fb_agno
; /* ag number of ap->firstblock */
3634 xfs_alloc_arg_t args
;
3636 xfs_extlen_t nextminlen
= 0;
3637 int nullfb
; /* true if ap->firstblock isn't set */
3645 mp
= ap
->ip
->i_mount
;
3647 /* stripe alignment for allocation is determined by mount parameters */
3649 if (mp
->m_swidth
&& (mp
->m_flags
& XFS_MOUNT_SWALLOC
))
3650 stripe_align
= mp
->m_swidth
;
3651 else if (mp
->m_dalign
)
3652 stripe_align
= mp
->m_dalign
;
3654 align
= ap
->userdata
? xfs_get_extsz_hint(ap
->ip
) : 0;
3655 if (unlikely(align
)) {
3656 error
= xfs_bmap_extsize_align(mp
, &ap
->got
, &ap
->prev
,
3657 align
, 0, ap
->eof
, 0, ap
->conv
,
3658 &ap
->offset
, &ap
->length
);
3664 nullfb
= *ap
->firstblock
== NULLFSBLOCK
;
3665 fb_agno
= nullfb
? NULLAGNUMBER
: XFS_FSB_TO_AGNO(mp
, *ap
->firstblock
);
3667 if (ap
->userdata
&& xfs_inode_is_filestream(ap
->ip
)) {
3668 ag
= xfs_filestream_lookup_ag(ap
->ip
);
3669 ag
= (ag
!= NULLAGNUMBER
) ? ag
: 0;
3670 ap
->blkno
= XFS_AGB_TO_FSB(mp
, ag
, 0);
3672 ap
->blkno
= XFS_INO_TO_FSB(mp
, ap
->ip
->i_ino
);
3675 ap
->blkno
= *ap
->firstblock
;
3677 xfs_bmap_adjacent(ap
);
3680 * If allowed, use ap->blkno; otherwise must use firstblock since
3681 * it's in the right allocation group.
3683 if (nullfb
|| XFS_FSB_TO_AGNO(mp
, ap
->blkno
) == fb_agno
)
3686 ap
->blkno
= *ap
->firstblock
;
3688 * Normal allocation, done through xfs_alloc_vextent.
3690 tryagain
= isaligned
= 0;
3691 memset(&args
, 0, sizeof(args
));
3694 args
.fsbno
= ap
->blkno
;
3696 /* Trim the allocation back to the maximum an AG can fit. */
3697 args
.maxlen
= MIN(ap
->length
, XFS_ALLOC_AG_MAX_USABLE(mp
));
3698 args
.firstblock
= *ap
->firstblock
;
3702 * Search for an allocation group with a single extent large
3703 * enough for the request. If one isn't found, then adjust
3704 * the minimum allocation size to the largest space found.
3706 if (ap
->userdata
&& xfs_inode_is_filestream(ap
->ip
))
3707 error
= xfs_bmap_btalloc_filestreams(ap
, &args
, &blen
);
3709 error
= xfs_bmap_btalloc_nullfb(ap
, &args
, &blen
);
3712 } else if (ap
->flist
->xbf_low
) {
3713 if (xfs_inode_is_filestream(ap
->ip
))
3714 args
.type
= XFS_ALLOCTYPE_FIRST_AG
;
3716 args
.type
= XFS_ALLOCTYPE_START_BNO
;
3717 args
.total
= args
.minlen
= ap
->minlen
;
3719 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
3720 args
.total
= ap
->total
;
3721 args
.minlen
= ap
->minlen
;
3723 /* apply extent size hints if obtained earlier */
3724 if (unlikely(align
)) {
3726 if ((args
.mod
= (xfs_extlen_t
)do_mod(ap
->offset
, args
.prod
)))
3727 args
.mod
= (xfs_extlen_t
)(args
.prod
- args
.mod
);
3728 } else if (mp
->m_sb
.sb_blocksize
>= PAGE_CACHE_SIZE
) {
3732 args
.prod
= PAGE_CACHE_SIZE
>> mp
->m_sb
.sb_blocklog
;
3733 if ((args
.mod
= (xfs_extlen_t
)(do_mod(ap
->offset
, args
.prod
))))
3734 args
.mod
= (xfs_extlen_t
)(args
.prod
- args
.mod
);
3737 * If we are not low on available data blocks, and the
3738 * underlying logical volume manager is a stripe, and
3739 * the file offset is zero then try to allocate data
3740 * blocks on stripe unit boundary.
3741 * NOTE: ap->aeof is only set if the allocation length
3742 * is >= the stripe unit and the allocation offset is
3743 * at the end of file.
3745 if (!ap
->flist
->xbf_low
&& ap
->aeof
) {
3747 args
.alignment
= stripe_align
;
3751 * Adjust for alignment
3753 if (blen
> args
.alignment
&& blen
<= args
.maxlen
)
3754 args
.minlen
= blen
- args
.alignment
;
3755 args
.minalignslop
= 0;
3758 * First try an exact bno allocation.
3759 * If it fails then do a near or start bno
3760 * allocation with alignment turned on.
3764 args
.type
= XFS_ALLOCTYPE_THIS_BNO
;
3767 * Compute the minlen+alignment for the
3768 * next case. Set slop so that the value
3769 * of minlen+alignment+slop doesn't go up
3770 * between the calls.
3772 if (blen
> stripe_align
&& blen
<= args
.maxlen
)
3773 nextminlen
= blen
- stripe_align
;
3775 nextminlen
= args
.minlen
;
3776 if (nextminlen
+ stripe_align
> args
.minlen
+ 1)
3778 nextminlen
+ stripe_align
-
3781 args
.minalignslop
= 0;
3785 args
.minalignslop
= 0;
3787 args
.minleft
= ap
->minleft
;
3788 args
.wasdel
= ap
->wasdel
;
3790 args
.userdata
= ap
->userdata
;
3791 if ((error
= xfs_alloc_vextent(&args
)))
3793 if (tryagain
&& args
.fsbno
== NULLFSBLOCK
) {
3795 * Exact allocation failed. Now try with alignment
3799 args
.fsbno
= ap
->blkno
;
3800 args
.alignment
= stripe_align
;
3801 args
.minlen
= nextminlen
;
3802 args
.minalignslop
= 0;
3804 if ((error
= xfs_alloc_vextent(&args
)))
3807 if (isaligned
&& args
.fsbno
== NULLFSBLOCK
) {
3809 * allocation failed, so turn off alignment and
3813 args
.fsbno
= ap
->blkno
;
3815 if ((error
= xfs_alloc_vextent(&args
)))
3818 if (args
.fsbno
== NULLFSBLOCK
&& nullfb
&&
3819 args
.minlen
> ap
->minlen
) {
3820 args
.minlen
= ap
->minlen
;
3821 args
.type
= XFS_ALLOCTYPE_START_BNO
;
3822 args
.fsbno
= ap
->blkno
;
3823 if ((error
= xfs_alloc_vextent(&args
)))
3826 if (args
.fsbno
== NULLFSBLOCK
&& nullfb
) {
3828 args
.type
= XFS_ALLOCTYPE_FIRST_AG
;
3829 args
.total
= ap
->minlen
;
3831 if ((error
= xfs_alloc_vextent(&args
)))
3833 ap
->flist
->xbf_low
= 1;
3835 if (args
.fsbno
!= NULLFSBLOCK
) {
3837 * check the allocation happened at the same or higher AG than
3838 * the first block that was allocated.
3840 ASSERT(*ap
->firstblock
== NULLFSBLOCK
||
3841 XFS_FSB_TO_AGNO(mp
, *ap
->firstblock
) ==
3842 XFS_FSB_TO_AGNO(mp
, args
.fsbno
) ||
3843 (ap
->flist
->xbf_low
&&
3844 XFS_FSB_TO_AGNO(mp
, *ap
->firstblock
) <
3845 XFS_FSB_TO_AGNO(mp
, args
.fsbno
)));
3847 ap
->blkno
= args
.fsbno
;
3848 if (*ap
->firstblock
== NULLFSBLOCK
)
3849 *ap
->firstblock
= args
.fsbno
;
3850 ASSERT(nullfb
|| fb_agno
== args
.agno
||
3851 (ap
->flist
->xbf_low
&& fb_agno
< args
.agno
));
3852 ap
->length
= args
.len
;
3853 ap
->ip
->i_d
.di_nblocks
+= args
.len
;
3854 xfs_trans_log_inode(ap
->tp
, ap
->ip
, XFS_ILOG_CORE
);
3856 ap
->ip
->i_delayed_blks
-= args
.len
;
3858 * Adjust the disk quota also. This was reserved
3861 xfs_trans_mod_dquot_byino(ap
->tp
, ap
->ip
,
3862 ap
->wasdel
? XFS_TRANS_DQ_DELBCOUNT
:
3863 XFS_TRANS_DQ_BCOUNT
,
3866 ap
->blkno
= NULLFSBLOCK
;
3873 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
3874 * It figures out where to ask the underlying allocator to put the new extent.
3878 struct xfs_bmalloca
*ap
) /* bmap alloc argument struct */
3880 if (XFS_IS_REALTIME_INODE(ap
->ip
) && ap
->userdata
)
3881 return xfs_bmap_rtalloc(ap
);
3882 return xfs_bmap_btalloc(ap
);
3886 * Trim the returned map to the required bounds
3890 struct xfs_bmbt_irec
*mval
,
3891 struct xfs_bmbt_irec
*got
,
3899 if ((flags
& XFS_BMAPI_ENTIRE
) ||
3900 got
->br_startoff
+ got
->br_blockcount
<= obno
) {
3902 if (isnullstartblock(got
->br_startblock
))
3903 mval
->br_startblock
= DELAYSTARTBLOCK
;
3909 ASSERT((*bno
>= obno
) || (n
== 0));
3911 mval
->br_startoff
= *bno
;
3912 if (isnullstartblock(got
->br_startblock
))
3913 mval
->br_startblock
= DELAYSTARTBLOCK
;
3915 mval
->br_startblock
= got
->br_startblock
+
3916 (*bno
- got
->br_startoff
);
3918 * Return the minimum of what we got and what we asked for for
3919 * the length. We can use the len variable here because it is
3920 * modified below and we could have been there before coming
3921 * here if the first part of the allocation didn't overlap what
3924 mval
->br_blockcount
= XFS_FILBLKS_MIN(end
- *bno
,
3925 got
->br_blockcount
- (*bno
- got
->br_startoff
));
3926 mval
->br_state
= got
->br_state
;
3927 ASSERT(mval
->br_blockcount
<= len
);
3932 * Update and validate the extent map to return
3935 xfs_bmapi_update_map(
3936 struct xfs_bmbt_irec
**map
,
3944 xfs_bmbt_irec_t
*mval
= *map
;
3946 ASSERT((flags
& XFS_BMAPI_ENTIRE
) ||
3947 ((mval
->br_startoff
+ mval
->br_blockcount
) <= end
));
3948 ASSERT((flags
& XFS_BMAPI_ENTIRE
) || (mval
->br_blockcount
<= *len
) ||
3949 (mval
->br_startoff
< obno
));
3951 *bno
= mval
->br_startoff
+ mval
->br_blockcount
;
3953 if (*n
> 0 && mval
->br_startoff
== mval
[-1].br_startoff
) {
3954 /* update previous map with new information */
3955 ASSERT(mval
->br_startblock
== mval
[-1].br_startblock
);
3956 ASSERT(mval
->br_blockcount
> mval
[-1].br_blockcount
);
3957 ASSERT(mval
->br_state
== mval
[-1].br_state
);
3958 mval
[-1].br_blockcount
= mval
->br_blockcount
;
3959 mval
[-1].br_state
= mval
->br_state
;
3960 } else if (*n
> 0 && mval
->br_startblock
!= DELAYSTARTBLOCK
&&
3961 mval
[-1].br_startblock
!= DELAYSTARTBLOCK
&&
3962 mval
[-1].br_startblock
!= HOLESTARTBLOCK
&&
3963 mval
->br_startblock
== mval
[-1].br_startblock
+
3964 mval
[-1].br_blockcount
&&
3965 ((flags
& XFS_BMAPI_IGSTATE
) ||
3966 mval
[-1].br_state
== mval
->br_state
)) {
3967 ASSERT(mval
->br_startoff
==
3968 mval
[-1].br_startoff
+ mval
[-1].br_blockcount
);
3969 mval
[-1].br_blockcount
+= mval
->br_blockcount
;
3970 } else if (*n
> 0 &&
3971 mval
->br_startblock
== DELAYSTARTBLOCK
&&
3972 mval
[-1].br_startblock
== DELAYSTARTBLOCK
&&
3973 mval
->br_startoff
==
3974 mval
[-1].br_startoff
+ mval
[-1].br_blockcount
) {
3975 mval
[-1].br_blockcount
+= mval
->br_blockcount
;
3976 mval
[-1].br_state
= mval
->br_state
;
3977 } else if (!((*n
== 0) &&
3978 ((mval
->br_startoff
+ mval
->br_blockcount
) <=
3987 * Map file blocks to filesystem blocks without allocation.
3991 struct xfs_inode
*ip
,
3994 struct xfs_bmbt_irec
*mval
,
3998 struct xfs_mount
*mp
= ip
->i_mount
;
3999 struct xfs_ifork
*ifp
;
4000 struct xfs_bmbt_irec got
;
4001 struct xfs_bmbt_irec prev
;
4008 int whichfork
= (flags
& XFS_BMAPI_ATTRFORK
) ?
4009 XFS_ATTR_FORK
: XFS_DATA_FORK
;
4012 ASSERT(!(flags
& ~(XFS_BMAPI_ATTRFORK
|XFS_BMAPI_ENTIRE
|
4013 XFS_BMAPI_IGSTATE
)));
4014 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_SHARED
|XFS_ILOCK_EXCL
));
4016 if (unlikely(XFS_TEST_ERROR(
4017 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
4018 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
4019 mp
, XFS_ERRTAG_BMAPIFORMAT
, XFS_RANDOM_BMAPIFORMAT
))) {
4020 XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW
, mp
);
4021 return -EFSCORRUPTED
;
4024 if (XFS_FORCED_SHUTDOWN(mp
))
4027 XFS_STATS_INC(xs_blk_mapr
);
4029 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4031 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
4032 error
= xfs_iread_extents(NULL
, ip
, whichfork
);
4037 xfs_bmap_search_extents(ip
, bno
, whichfork
, &eof
, &lastx
, &got
, &prev
);
4041 while (bno
< end
&& n
< *nmap
) {
4042 /* Reading past eof, act as though there's a hole up to end. */
4044 got
.br_startoff
= end
;
4045 if (got
.br_startoff
> bno
) {
4046 /* Reading in a hole. */
4047 mval
->br_startoff
= bno
;
4048 mval
->br_startblock
= HOLESTARTBLOCK
;
4049 mval
->br_blockcount
=
4050 XFS_FILBLKS_MIN(len
, got
.br_startoff
- bno
);
4051 mval
->br_state
= XFS_EXT_NORM
;
4052 bno
+= mval
->br_blockcount
;
4053 len
-= mval
->br_blockcount
;
4059 /* set up the extent map to return. */
4060 xfs_bmapi_trim_map(mval
, &got
, &bno
, len
, obno
, end
, n
, flags
);
4061 xfs_bmapi_update_map(&mval
, &bno
, &len
, obno
, end
, &n
, flags
);
4063 /* If we're done, stop now. */
4064 if (bno
>= end
|| n
>= *nmap
)
4067 /* Else go on to the next record. */
4068 if (++lastx
< ifp
->if_bytes
/ sizeof(xfs_bmbt_rec_t
))
4069 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, lastx
), &got
);
4078 xfs_bmapi_reserve_delalloc(
4079 struct xfs_inode
*ip
,
4082 struct xfs_bmbt_irec
*got
,
4083 struct xfs_bmbt_irec
*prev
,
4084 xfs_extnum_t
*lastx
,
4087 struct xfs_mount
*mp
= ip
->i_mount
;
4088 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, XFS_DATA_FORK
);
4090 xfs_extlen_t indlen
;
4091 char rt
= XFS_IS_REALTIME_INODE(ip
);
4095 alen
= XFS_FILBLKS_MIN(len
, MAXEXTLEN
);
4097 alen
= XFS_FILBLKS_MIN(alen
, got
->br_startoff
- aoff
);
4099 /* Figure out the extent size, adjust alen */
4100 extsz
= xfs_get_extsz_hint(ip
);
4103 * Make sure we don't exceed a single extent length when we
4104 * align the extent by reducing length we are going to
4105 * allocate by the maximum amount extent size aligment may
4108 alen
= XFS_FILBLKS_MIN(len
, MAXEXTLEN
- (2 * extsz
- 1));
4109 error
= xfs_bmap_extsize_align(mp
, got
, prev
, extsz
, rt
, eof
,
4110 1, 0, &aoff
, &alen
);
4115 extsz
= alen
/ mp
->m_sb
.sb_rextsize
;
4118 * Make a transaction-less quota reservation for delayed allocation
4119 * blocks. This number gets adjusted later. We return if we haven't
4120 * allocated blocks already inside this loop.
4122 error
= xfs_trans_reserve_quota_nblks(NULL
, ip
, (long)alen
, 0,
4123 rt
? XFS_QMOPT_RES_RTBLKS
: XFS_QMOPT_RES_REGBLKS
);
4128 * Split changing sb for alen and indlen since they could be coming
4129 * from different places.
4131 indlen
= (xfs_extlen_t
)xfs_bmap_worst_indlen(ip
, alen
);
4135 error
= xfs_mod_incore_sb(mp
, XFS_SBS_FREXTENTS
,
4136 -((int64_t)extsz
), 0);
4138 error
= xfs_icsb_modify_counters(mp
, XFS_SBS_FDBLOCKS
,
4139 -((int64_t)alen
), 0);
4143 goto out_unreserve_quota
;
4145 error
= xfs_icsb_modify_counters(mp
, XFS_SBS_FDBLOCKS
,
4146 -((int64_t)indlen
), 0);
4148 goto out_unreserve_blocks
;
4151 ip
->i_delayed_blks
+= alen
;
4153 got
->br_startoff
= aoff
;
4154 got
->br_startblock
= nullstartblock(indlen
);
4155 got
->br_blockcount
= alen
;
4156 got
->br_state
= XFS_EXT_NORM
;
4157 xfs_bmap_add_extent_hole_delay(ip
, lastx
, got
);
4160 * Update our extent pointer, given that xfs_bmap_add_extent_hole_delay
4161 * might have merged it into one of the neighbouring ones.
4163 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, *lastx
), got
);
4165 ASSERT(got
->br_startoff
<= aoff
);
4166 ASSERT(got
->br_startoff
+ got
->br_blockcount
>= aoff
+ alen
);
4167 ASSERT(isnullstartblock(got
->br_startblock
));
4168 ASSERT(got
->br_state
== XFS_EXT_NORM
);
4171 out_unreserve_blocks
:
4173 xfs_mod_incore_sb(mp
, XFS_SBS_FREXTENTS
, extsz
, 0);
4175 xfs_icsb_modify_counters(mp
, XFS_SBS_FDBLOCKS
, alen
, 0);
4176 out_unreserve_quota
:
4177 if (XFS_IS_QUOTA_ON(mp
))
4178 xfs_trans_unreserve_quota_nblks(NULL
, ip
, (long)alen
, 0, rt
?
4179 XFS_QMOPT_RES_RTBLKS
: XFS_QMOPT_RES_REGBLKS
);
4184 * Map file blocks to filesystem blocks, adding delayed allocations as needed.
4188 struct xfs_inode
*ip
, /* incore inode */
4189 xfs_fileoff_t bno
, /* starting file offs. mapped */
4190 xfs_filblks_t len
, /* length to map in file */
4191 struct xfs_bmbt_irec
*mval
, /* output: map values */
4192 int *nmap
, /* i/o: mval size/count */
4193 int flags
) /* XFS_BMAPI_... */
4195 struct xfs_mount
*mp
= ip
->i_mount
;
4196 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, XFS_DATA_FORK
);
4197 struct xfs_bmbt_irec got
; /* current file extent record */
4198 struct xfs_bmbt_irec prev
; /* previous file extent record */
4199 xfs_fileoff_t obno
; /* old block number (offset) */
4200 xfs_fileoff_t end
; /* end of mapped file region */
4201 xfs_extnum_t lastx
; /* last useful extent number */
4202 int eof
; /* we've hit the end of extents */
4203 int n
= 0; /* current extent index */
4207 ASSERT(*nmap
<= XFS_BMAP_MAX_NMAP
);
4208 ASSERT(!(flags
& ~XFS_BMAPI_ENTIRE
));
4209 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
4211 if (unlikely(XFS_TEST_ERROR(
4212 (XFS_IFORK_FORMAT(ip
, XFS_DATA_FORK
) != XFS_DINODE_FMT_EXTENTS
&&
4213 XFS_IFORK_FORMAT(ip
, XFS_DATA_FORK
) != XFS_DINODE_FMT_BTREE
),
4214 mp
, XFS_ERRTAG_BMAPIFORMAT
, XFS_RANDOM_BMAPIFORMAT
))) {
4215 XFS_ERROR_REPORT("xfs_bmapi_delay", XFS_ERRLEVEL_LOW
, mp
);
4216 return -EFSCORRUPTED
;
4219 if (XFS_FORCED_SHUTDOWN(mp
))
4222 XFS_STATS_INC(xs_blk_mapw
);
4224 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
4225 error
= xfs_iread_extents(NULL
, ip
, XFS_DATA_FORK
);
4230 xfs_bmap_search_extents(ip
, bno
, XFS_DATA_FORK
, &eof
, &lastx
, &got
, &prev
);
4234 while (bno
< end
&& n
< *nmap
) {
4235 if (eof
|| got
.br_startoff
> bno
) {
4236 error
= xfs_bmapi_reserve_delalloc(ip
, bno
, len
, &got
,
4237 &prev
, &lastx
, eof
);
4247 /* set up the extent map to return. */
4248 xfs_bmapi_trim_map(mval
, &got
, &bno
, len
, obno
, end
, n
, flags
);
4249 xfs_bmapi_update_map(&mval
, &bno
, &len
, obno
, end
, &n
, flags
);
4251 /* If we're done, stop now. */
4252 if (bno
>= end
|| n
>= *nmap
)
4255 /* Else go on to the next record. */
4257 if (++lastx
< ifp
->if_bytes
/ sizeof(xfs_bmbt_rec_t
))
4258 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, lastx
), &got
);
4270 struct xfs_bmalloca
*bma
)
4272 struct xfs_mount
*mp
= bma
->ip
->i_mount
;
4273 int whichfork
= (bma
->flags
& XFS_BMAPI_ATTRFORK
) ?
4274 XFS_ATTR_FORK
: XFS_DATA_FORK
;
4275 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
4276 int tmp_logflags
= 0;
4279 ASSERT(bma
->length
> 0);
4282 * For the wasdelay case, we could also just allocate the stuff asked
4283 * for in this bmap call but that wouldn't be as good.
4286 bma
->length
= (xfs_extlen_t
)bma
->got
.br_blockcount
;
4287 bma
->offset
= bma
->got
.br_startoff
;
4288 if (bma
->idx
!= NULLEXTNUM
&& bma
->idx
) {
4289 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
- 1),
4293 bma
->length
= XFS_FILBLKS_MIN(bma
->length
, MAXEXTLEN
);
4295 bma
->length
= XFS_FILBLKS_MIN(bma
->length
,
4296 bma
->got
.br_startoff
- bma
->offset
);
4300 * Indicate if this is the first user data in the file, or just any
4303 if (!(bma
->flags
& XFS_BMAPI_METADATA
)) {
4304 bma
->userdata
= (bma
->offset
== 0) ?
4305 XFS_ALLOC_INITIAL_USER_DATA
: XFS_ALLOC_USERDATA
;
4308 bma
->minlen
= (bma
->flags
& XFS_BMAPI_CONTIG
) ? bma
->length
: 1;
4311 * Only want to do the alignment at the eof if it is userdata and
4312 * allocation length is larger than a stripe unit.
4314 if (mp
->m_dalign
&& bma
->length
>= mp
->m_dalign
&&
4315 !(bma
->flags
& XFS_BMAPI_METADATA
) && whichfork
== XFS_DATA_FORK
) {
4316 error
= xfs_bmap_isaeof(bma
, whichfork
);
4321 error
= xfs_bmap_alloc(bma
);
4325 if (bma
->flist
->xbf_low
)
4328 bma
->cur
->bc_private
.b
.firstblock
= *bma
->firstblock
;
4329 if (bma
->blkno
== NULLFSBLOCK
)
4331 if ((ifp
->if_flags
& XFS_IFBROOT
) && !bma
->cur
) {
4332 bma
->cur
= xfs_bmbt_init_cursor(mp
, bma
->tp
, bma
->ip
, whichfork
);
4333 bma
->cur
->bc_private
.b
.firstblock
= *bma
->firstblock
;
4334 bma
->cur
->bc_private
.b
.flist
= bma
->flist
;
4337 * Bump the number of extents we've allocated
4343 bma
->cur
->bc_private
.b
.flags
=
4344 bma
->wasdel
? XFS_BTCUR_BPRV_WASDEL
: 0;
4346 bma
->got
.br_startoff
= bma
->offset
;
4347 bma
->got
.br_startblock
= bma
->blkno
;
4348 bma
->got
.br_blockcount
= bma
->length
;
4349 bma
->got
.br_state
= XFS_EXT_NORM
;
4352 * A wasdelay extent has been initialized, so shouldn't be flagged
4355 if (!bma
->wasdel
&& (bma
->flags
& XFS_BMAPI_PREALLOC
) &&
4356 xfs_sb_version_hasextflgbit(&mp
->m_sb
))
4357 bma
->got
.br_state
= XFS_EXT_UNWRITTEN
;
4360 error
= xfs_bmap_add_extent_delay_real(bma
);
4362 error
= xfs_bmap_add_extent_hole_real(bma
, whichfork
);
4364 bma
->logflags
|= tmp_logflags
;
4369 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4370 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4371 * the neighbouring ones.
4373 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
), &bma
->got
);
4375 ASSERT(bma
->got
.br_startoff
<= bma
->offset
);
4376 ASSERT(bma
->got
.br_startoff
+ bma
->got
.br_blockcount
>=
4377 bma
->offset
+ bma
->length
);
4378 ASSERT(bma
->got
.br_state
== XFS_EXT_NORM
||
4379 bma
->got
.br_state
== XFS_EXT_UNWRITTEN
);
4384 xfs_bmapi_convert_unwritten(
4385 struct xfs_bmalloca
*bma
,
4386 struct xfs_bmbt_irec
*mval
,
4390 int whichfork
= (flags
& XFS_BMAPI_ATTRFORK
) ?
4391 XFS_ATTR_FORK
: XFS_DATA_FORK
;
4392 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
4393 int tmp_logflags
= 0;
4396 /* check if we need to do unwritten->real conversion */
4397 if (mval
->br_state
== XFS_EXT_UNWRITTEN
&&
4398 (flags
& XFS_BMAPI_PREALLOC
))
4401 /* check if we need to do real->unwritten conversion */
4402 if (mval
->br_state
== XFS_EXT_NORM
&&
4403 (flags
& (XFS_BMAPI_PREALLOC
| XFS_BMAPI_CONVERT
)) !=
4404 (XFS_BMAPI_PREALLOC
| XFS_BMAPI_CONVERT
))
4408 * Modify (by adding) the state flag, if writing.
4410 ASSERT(mval
->br_blockcount
<= len
);
4411 if ((ifp
->if_flags
& XFS_IFBROOT
) && !bma
->cur
) {
4412 bma
->cur
= xfs_bmbt_init_cursor(bma
->ip
->i_mount
, bma
->tp
,
4413 bma
->ip
, whichfork
);
4414 bma
->cur
->bc_private
.b
.firstblock
= *bma
->firstblock
;
4415 bma
->cur
->bc_private
.b
.flist
= bma
->flist
;
4417 mval
->br_state
= (mval
->br_state
== XFS_EXT_UNWRITTEN
)
4418 ? XFS_EXT_NORM
: XFS_EXT_UNWRITTEN
;
4420 error
= xfs_bmap_add_extent_unwritten_real(bma
->tp
, bma
->ip
, &bma
->idx
,
4421 &bma
->cur
, mval
, bma
->firstblock
, bma
->flist
,
4423 bma
->logflags
|= tmp_logflags
;
4428 * Update our extent pointer, given that
4429 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4430 * of the neighbouring ones.
4432 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
), &bma
->got
);
4435 * We may have combined previously unwritten space with written space,
4436 * so generate another request.
4438 if (mval
->br_blockcount
< len
)
4444 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4445 * extent state if necessary. Details behaviour is controlled by the flags
4446 * parameter. Only allocates blocks from a single allocation group, to avoid
4449 * The returned value in "firstblock" from the first call in a transaction
4450 * must be remembered and presented to subsequent calls in "firstblock".
4451 * An upper bound for the number of blocks to be allocated is supplied to
4452 * the first call in "total"; if no allocation group has that many free
4453 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4457 struct xfs_trans
*tp
, /* transaction pointer */
4458 struct xfs_inode
*ip
, /* incore inode */
4459 xfs_fileoff_t bno
, /* starting file offs. mapped */
4460 xfs_filblks_t len
, /* length to map in file */
4461 int flags
, /* XFS_BMAPI_... */
4462 xfs_fsblock_t
*firstblock
, /* first allocated block
4463 controls a.g. for allocs */
4464 xfs_extlen_t total
, /* total blocks needed */
4465 struct xfs_bmbt_irec
*mval
, /* output: map values */
4466 int *nmap
, /* i/o: mval size/count */
4467 struct xfs_bmap_free
*flist
) /* i/o: list extents to free */
4469 struct xfs_mount
*mp
= ip
->i_mount
;
4470 struct xfs_ifork
*ifp
;
4471 struct xfs_bmalloca bma
= { NULL
}; /* args for xfs_bmap_alloc */
4472 xfs_fileoff_t end
; /* end of mapped file region */
4473 int eof
; /* after the end of extents */
4474 int error
; /* error return */
4475 int n
; /* current extent index */
4476 xfs_fileoff_t obno
; /* old block number (offset) */
4477 int whichfork
; /* data or attr fork */
4478 char inhole
; /* current location is hole in file */
4479 char wasdelay
; /* old extent was delayed */
4482 xfs_fileoff_t orig_bno
; /* original block number value */
4483 int orig_flags
; /* original flags arg value */
4484 xfs_filblks_t orig_len
; /* original value of len arg */
4485 struct xfs_bmbt_irec
*orig_mval
; /* original value of mval */
4486 int orig_nmap
; /* original value of *nmap */
4494 whichfork
= (flags
& XFS_BMAPI_ATTRFORK
) ?
4495 XFS_ATTR_FORK
: XFS_DATA_FORK
;
4498 ASSERT(*nmap
<= XFS_BMAP_MAX_NMAP
);
4499 ASSERT(!(flags
& XFS_BMAPI_IGSTATE
));
4502 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_LOCAL
);
4503 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
4505 if (unlikely(XFS_TEST_ERROR(
4506 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
4507 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
4508 mp
, XFS_ERRTAG_BMAPIFORMAT
, XFS_RANDOM_BMAPIFORMAT
))) {
4509 XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW
, mp
);
4510 return -EFSCORRUPTED
;
4513 if (XFS_FORCED_SHUTDOWN(mp
))
4516 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4518 XFS_STATS_INC(xs_blk_mapw
);
4520 if (*firstblock
== NULLFSBLOCK
) {
4521 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
)
4522 bma
.minleft
= be16_to_cpu(ifp
->if_broot
->bb_level
) + 1;
4529 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
4530 error
= xfs_iread_extents(tp
, ip
, whichfork
);
4535 xfs_bmap_search_extents(ip
, bno
, whichfork
, &eof
, &bma
.idx
, &bma
.got
,
4546 bma
.firstblock
= firstblock
;
4548 while (bno
< end
&& n
< *nmap
) {
4549 inhole
= eof
|| bma
.got
.br_startoff
> bno
;
4550 wasdelay
= !inhole
&& isnullstartblock(bma
.got
.br_startblock
);
4553 * First, deal with the hole before the allocated space
4554 * that we found, if any.
4556 if (inhole
|| wasdelay
) {
4558 bma
.conv
= !!(flags
& XFS_BMAPI_CONVERT
);
4559 bma
.wasdel
= wasdelay
;
4564 * There's a 32/64 bit type mismatch between the
4565 * allocation length request (which can be 64 bits in
4566 * length) and the bma length request, which is
4567 * xfs_extlen_t and therefore 32 bits. Hence we have to
4568 * check for 32-bit overflows and handle them here.
4570 if (len
> (xfs_filblks_t
)MAXEXTLEN
)
4571 bma
.length
= MAXEXTLEN
;
4576 ASSERT(bma
.length
> 0);
4577 error
= xfs_bmapi_allocate(&bma
);
4580 if (bma
.blkno
== NULLFSBLOCK
)
4584 /* Deal with the allocated space we found. */
4585 xfs_bmapi_trim_map(mval
, &bma
.got
, &bno
, len
, obno
,
4588 /* Execute unwritten extent conversion if necessary */
4589 error
= xfs_bmapi_convert_unwritten(&bma
, mval
, len
, flags
);
4590 if (error
== -EAGAIN
)
4595 /* update the extent map to return */
4596 xfs_bmapi_update_map(&mval
, &bno
, &len
, obno
, end
, &n
, flags
);
4599 * If we're done, stop now. Stop when we've allocated
4600 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4601 * the transaction may get too big.
4603 if (bno
>= end
|| n
>= *nmap
|| bma
.nallocs
>= *nmap
)
4606 /* Else go on to the next record. */
4608 if (++bma
.idx
< ifp
->if_bytes
/ sizeof(xfs_bmbt_rec_t
)) {
4609 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
.idx
),
4617 * Transform from btree to extents, give it cur.
4619 if (xfs_bmap_wants_extents(ip
, whichfork
)) {
4620 int tmp_logflags
= 0;
4623 error
= xfs_bmap_btree_to_extents(tp
, ip
, bma
.cur
,
4624 &tmp_logflags
, whichfork
);
4625 bma
.logflags
|= tmp_logflags
;
4630 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
||
4631 XFS_IFORK_NEXTENTS(ip
, whichfork
) >
4632 XFS_IFORK_MAXEXT(ip
, whichfork
));
4636 * Log everything. Do this after conversion, there's no point in
4637 * logging the extent records if we've converted to btree format.
4639 if ((bma
.logflags
& xfs_ilog_fext(whichfork
)) &&
4640 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
4641 bma
.logflags
&= ~xfs_ilog_fext(whichfork
);
4642 else if ((bma
.logflags
& xfs_ilog_fbroot(whichfork
)) &&
4643 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)
4644 bma
.logflags
&= ~xfs_ilog_fbroot(whichfork
);
4646 * Log whatever the flags say, even if error. Otherwise we might miss
4647 * detecting a case where the data is changed, there's an error,
4648 * and it's not logged so we don't shutdown when we should.
4651 xfs_trans_log_inode(tp
, ip
, bma
.logflags
);
4655 ASSERT(*firstblock
== NULLFSBLOCK
||
4656 XFS_FSB_TO_AGNO(mp
, *firstblock
) ==
4658 bma
.cur
->bc_private
.b
.firstblock
) ||
4660 XFS_FSB_TO_AGNO(mp
, *firstblock
) <
4662 bma
.cur
->bc_private
.b
.firstblock
)));
4663 *firstblock
= bma
.cur
->bc_private
.b
.firstblock
;
4665 xfs_btree_del_cursor(bma
.cur
,
4666 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
4669 xfs_bmap_validate_ret(orig_bno
, orig_len
, orig_flags
, orig_mval
,
4675 * Called by xfs_bmapi to update file extent records and the btree
4676 * after removing space (or undoing a delayed allocation).
4678 STATIC
int /* error */
4679 xfs_bmap_del_extent(
4680 xfs_inode_t
*ip
, /* incore inode pointer */
4681 xfs_trans_t
*tp
, /* current transaction pointer */
4682 xfs_extnum_t
*idx
, /* extent number to update/delete */
4683 xfs_bmap_free_t
*flist
, /* list of extents to be freed */
4684 xfs_btree_cur_t
*cur
, /* if null, not a btree */
4685 xfs_bmbt_irec_t
*del
, /* data to remove from extents */
4686 int *logflagsp
, /* inode logging flags */
4687 int whichfork
) /* data or attr fork */
4689 xfs_filblks_t da_new
; /* new delay-alloc indirect blocks */
4690 xfs_filblks_t da_old
; /* old delay-alloc indirect blocks */
4691 xfs_fsblock_t del_endblock
=0; /* first block past del */
4692 xfs_fileoff_t del_endoff
; /* first offset past del */
4693 int delay
; /* current block is delayed allocated */
4694 int do_fx
; /* free extent at end of routine */
4695 xfs_bmbt_rec_host_t
*ep
; /* current extent entry pointer */
4696 int error
; /* error return value */
4697 int flags
; /* inode logging flags */
4698 xfs_bmbt_irec_t got
; /* current extent entry */
4699 xfs_fileoff_t got_endoff
; /* first offset past got */
4700 int i
; /* temp state */
4701 xfs_ifork_t
*ifp
; /* inode fork pointer */
4702 xfs_mount_t
*mp
; /* mount structure */
4703 xfs_filblks_t nblks
; /* quota/sb block count */
4704 xfs_bmbt_irec_t
new; /* new record to be inserted */
4706 uint qfield
; /* quota field to update */
4707 xfs_filblks_t temp
; /* for indirect length calculations */
4708 xfs_filblks_t temp2
; /* for indirect length calculations */
4711 XFS_STATS_INC(xs_del_exlist
);
4713 if (whichfork
== XFS_ATTR_FORK
)
4714 state
|= BMAP_ATTRFORK
;
4717 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4718 ASSERT((*idx
>= 0) && (*idx
< ifp
->if_bytes
/
4719 (uint
)sizeof(xfs_bmbt_rec_t
)));
4720 ASSERT(del
->br_blockcount
> 0);
4721 ep
= xfs_iext_get_ext(ifp
, *idx
);
4722 xfs_bmbt_get_all(ep
, &got
);
4723 ASSERT(got
.br_startoff
<= del
->br_startoff
);
4724 del_endoff
= del
->br_startoff
+ del
->br_blockcount
;
4725 got_endoff
= got
.br_startoff
+ got
.br_blockcount
;
4726 ASSERT(got_endoff
>= del_endoff
);
4727 delay
= isnullstartblock(got
.br_startblock
);
4728 ASSERT(isnullstartblock(del
->br_startblock
) == delay
);
4733 * If deleting a real allocation, must free up the disk space.
4736 flags
= XFS_ILOG_CORE
;
4738 * Realtime allocation. Free it and record di_nblocks update.
4740 if (whichfork
== XFS_DATA_FORK
&& XFS_IS_REALTIME_INODE(ip
)) {
4744 ASSERT(do_mod(del
->br_blockcount
,
4745 mp
->m_sb
.sb_rextsize
) == 0);
4746 ASSERT(do_mod(del
->br_startblock
,
4747 mp
->m_sb
.sb_rextsize
) == 0);
4748 bno
= del
->br_startblock
;
4749 len
= del
->br_blockcount
;
4750 do_div(bno
, mp
->m_sb
.sb_rextsize
);
4751 do_div(len
, mp
->m_sb
.sb_rextsize
);
4752 error
= xfs_rtfree_extent(tp
, bno
, (xfs_extlen_t
)len
);
4756 nblks
= len
* mp
->m_sb
.sb_rextsize
;
4757 qfield
= XFS_TRANS_DQ_RTBCOUNT
;
4760 * Ordinary allocation.
4764 nblks
= del
->br_blockcount
;
4765 qfield
= XFS_TRANS_DQ_BCOUNT
;
4768 * Set up del_endblock and cur for later.
4770 del_endblock
= del
->br_startblock
+ del
->br_blockcount
;
4772 if ((error
= xfs_bmbt_lookup_eq(cur
, got
.br_startoff
,
4773 got
.br_startblock
, got
.br_blockcount
,
4776 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
4778 da_old
= da_new
= 0;
4780 da_old
= startblockval(got
.br_startblock
);
4786 * Set flag value to use in switch statement.
4787 * Left-contig is 2, right-contig is 1.
4789 switch (((got
.br_startoff
== del
->br_startoff
) << 1) |
4790 (got_endoff
== del_endoff
)) {
4793 * Matches the whole extent. Delete the entry.
4795 xfs_iext_remove(ip
, *idx
, 1,
4796 whichfork
== XFS_ATTR_FORK
? BMAP_ATTRFORK
: 0);
4801 XFS_IFORK_NEXT_SET(ip
, whichfork
,
4802 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
4803 flags
|= XFS_ILOG_CORE
;
4805 flags
|= xfs_ilog_fext(whichfork
);
4808 if ((error
= xfs_btree_delete(cur
, &i
)))
4810 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
4815 * Deleting the first part of the extent.
4817 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
4818 xfs_bmbt_set_startoff(ep
, del_endoff
);
4819 temp
= got
.br_blockcount
- del
->br_blockcount
;
4820 xfs_bmbt_set_blockcount(ep
, temp
);
4822 temp
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
4824 xfs_bmbt_set_startblock(ep
, nullstartblock((int)temp
));
4825 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
4829 xfs_bmbt_set_startblock(ep
, del_endblock
);
4830 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
4832 flags
|= xfs_ilog_fext(whichfork
);
4835 if ((error
= xfs_bmbt_update(cur
, del_endoff
, del_endblock
,
4836 got
.br_blockcount
- del
->br_blockcount
,
4843 * Deleting the last part of the extent.
4845 temp
= got
.br_blockcount
- del
->br_blockcount
;
4846 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
4847 xfs_bmbt_set_blockcount(ep
, temp
);
4849 temp
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
4851 xfs_bmbt_set_startblock(ep
, nullstartblock((int)temp
));
4852 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
4856 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
4858 flags
|= xfs_ilog_fext(whichfork
);
4861 if ((error
= xfs_bmbt_update(cur
, got
.br_startoff
,
4863 got
.br_blockcount
- del
->br_blockcount
,
4870 * Deleting the middle of the extent.
4872 temp
= del
->br_startoff
- got
.br_startoff
;
4873 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
4874 xfs_bmbt_set_blockcount(ep
, temp
);
4875 new.br_startoff
= del_endoff
;
4876 temp2
= got_endoff
- del_endoff
;
4877 new.br_blockcount
= temp2
;
4878 new.br_state
= got
.br_state
;
4880 new.br_startblock
= del_endblock
;
4881 flags
|= XFS_ILOG_CORE
;
4883 if ((error
= xfs_bmbt_update(cur
,
4885 got
.br_startblock
, temp
,
4888 if ((error
= xfs_btree_increment(cur
, 0, &i
)))
4890 cur
->bc_rec
.b
= new;
4891 error
= xfs_btree_insert(cur
, &i
);
4892 if (error
&& error
!= -ENOSPC
)
4895 * If get no-space back from btree insert,
4896 * it tried a split, and we have a zero
4897 * block reservation.
4898 * Fix up our state and return the error.
4900 if (error
== -ENOSPC
) {
4902 * Reset the cursor, don't trust
4903 * it after any insert operation.
4905 if ((error
= xfs_bmbt_lookup_eq(cur
,
4910 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
4912 * Update the btree record back
4913 * to the original value.
4915 if ((error
= xfs_bmbt_update(cur
,
4922 * Reset the extent record back
4923 * to the original value.
4925 xfs_bmbt_set_blockcount(ep
,
4931 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
4933 flags
|= xfs_ilog_fext(whichfork
);
4934 XFS_IFORK_NEXT_SET(ip
, whichfork
,
4935 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
4937 ASSERT(whichfork
== XFS_DATA_FORK
);
4938 temp
= xfs_bmap_worst_indlen(ip
, temp
);
4939 xfs_bmbt_set_startblock(ep
, nullstartblock((int)temp
));
4940 temp2
= xfs_bmap_worst_indlen(ip
, temp2
);
4941 new.br_startblock
= nullstartblock((int)temp2
);
4942 da_new
= temp
+ temp2
;
4943 while (da_new
> da_old
) {
4947 xfs_bmbt_set_startblock(ep
,
4948 nullstartblock((int)temp
));
4950 if (da_new
== da_old
)
4956 nullstartblock((int)temp2
);
4960 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
4961 xfs_iext_insert(ip
, *idx
+ 1, 1, &new, state
);
4966 * If we need to, add to list of extents to delete.
4969 xfs_bmap_add_free(del
->br_startblock
, del
->br_blockcount
, flist
,
4972 * Adjust inode # blocks in the file.
4975 ip
->i_d
.di_nblocks
-= nblks
;
4977 * Adjust quota data.
4980 xfs_trans_mod_dquot_byino(tp
, ip
, qfield
, (long)-nblks
);
4983 * Account for change in delayed indirect blocks.
4984 * Nothing to do for disk quota accounting here.
4986 ASSERT(da_old
>= da_new
);
4987 if (da_old
> da_new
) {
4988 xfs_icsb_modify_counters(mp
, XFS_SBS_FDBLOCKS
,
4989 (int64_t)(da_old
- da_new
), 0);
4997 * Unmap (remove) blocks from a file.
4998 * If nexts is nonzero then the number of extents to remove is limited to
4999 * that value. If not all extents in the block range can be removed then
5004 xfs_trans_t
*tp
, /* transaction pointer */
5005 struct xfs_inode
*ip
, /* incore inode */
5006 xfs_fileoff_t bno
, /* starting offset to unmap */
5007 xfs_filblks_t len
, /* length to unmap in file */
5008 int flags
, /* misc flags */
5009 xfs_extnum_t nexts
, /* number of extents max */
5010 xfs_fsblock_t
*firstblock
, /* first allocated block
5011 controls a.g. for allocs */
5012 xfs_bmap_free_t
*flist
, /* i/o: list extents to free */
5013 int *done
) /* set if not done yet */
5015 xfs_btree_cur_t
*cur
; /* bmap btree cursor */
5016 xfs_bmbt_irec_t del
; /* extent being deleted */
5017 int eof
; /* is deleting at eof */
5018 xfs_bmbt_rec_host_t
*ep
; /* extent record pointer */
5019 int error
; /* error return value */
5020 xfs_extnum_t extno
; /* extent number in list */
5021 xfs_bmbt_irec_t got
; /* current extent record */
5022 xfs_ifork_t
*ifp
; /* inode fork pointer */
5023 int isrt
; /* freeing in rt area */
5024 xfs_extnum_t lastx
; /* last extent index used */
5025 int logflags
; /* transaction logging flags */
5026 xfs_extlen_t mod
; /* rt extent offset */
5027 xfs_mount_t
*mp
; /* mount structure */
5028 xfs_extnum_t nextents
; /* number of file extents */
5029 xfs_bmbt_irec_t prev
; /* previous extent record */
5030 xfs_fileoff_t start
; /* first file offset deleted */
5031 int tmp_logflags
; /* partial logging flags */
5032 int wasdel
; /* was a delayed alloc extent */
5033 int whichfork
; /* data or attribute fork */
5036 trace_xfs_bunmap(ip
, bno
, len
, flags
, _RET_IP_
);
5038 whichfork
= (flags
& XFS_BMAPI_ATTRFORK
) ?
5039 XFS_ATTR_FORK
: XFS_DATA_FORK
;
5040 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5042 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
5043 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)) {
5044 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW
,
5046 return -EFSCORRUPTED
;
5049 if (XFS_FORCED_SHUTDOWN(mp
))
5052 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
5056 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
5057 (error
= xfs_iread_extents(tp
, ip
, whichfork
)))
5059 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
5060 if (nextents
== 0) {
5064 XFS_STATS_INC(xs_blk_unmap
);
5065 isrt
= (whichfork
== XFS_DATA_FORK
) && XFS_IS_REALTIME_INODE(ip
);
5067 bno
= start
+ len
- 1;
5068 ep
= xfs_bmap_search_extents(ip
, bno
, whichfork
, &eof
, &lastx
, &got
,
5072 * Check to see if the given block number is past the end of the
5073 * file, back up to the last block if so...
5076 ep
= xfs_iext_get_ext(ifp
, --lastx
);
5077 xfs_bmbt_get_all(ep
, &got
);
5078 bno
= got
.br_startoff
+ got
.br_blockcount
- 1;
5081 if (ifp
->if_flags
& XFS_IFBROOT
) {
5082 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
);
5083 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
5084 cur
->bc_private
.b
.firstblock
= *firstblock
;
5085 cur
->bc_private
.b
.flist
= flist
;
5086 cur
->bc_private
.b
.flags
= 0;
5092 * Synchronize by locking the bitmap inode.
5094 xfs_ilock(mp
->m_rbmip
, XFS_ILOCK_EXCL
);
5095 xfs_trans_ijoin(tp
, mp
->m_rbmip
, XFS_ILOCK_EXCL
);
5099 while (bno
!= (xfs_fileoff_t
)-1 && bno
>= start
&& lastx
>= 0 &&
5100 (nexts
== 0 || extno
< nexts
)) {
5102 * Is the found extent after a hole in which bno lives?
5103 * Just back up to the previous extent, if so.
5105 if (got
.br_startoff
> bno
) {
5108 ep
= xfs_iext_get_ext(ifp
, lastx
);
5109 xfs_bmbt_get_all(ep
, &got
);
5112 * Is the last block of this extent before the range
5113 * we're supposed to delete? If so, we're done.
5115 bno
= XFS_FILEOFF_MIN(bno
,
5116 got
.br_startoff
+ got
.br_blockcount
- 1);
5120 * Then deal with the (possibly delayed) allocated space
5125 wasdel
= isnullstartblock(del
.br_startblock
);
5126 if (got
.br_startoff
< start
) {
5127 del
.br_startoff
= start
;
5128 del
.br_blockcount
-= start
- got
.br_startoff
;
5130 del
.br_startblock
+= start
- got
.br_startoff
;
5132 if (del
.br_startoff
+ del
.br_blockcount
> bno
+ 1)
5133 del
.br_blockcount
= bno
+ 1 - del
.br_startoff
;
5134 sum
= del
.br_startblock
+ del
.br_blockcount
;
5136 (mod
= do_mod(sum
, mp
->m_sb
.sb_rextsize
))) {
5138 * Realtime extent not lined up at the end.
5139 * The extent could have been split into written
5140 * and unwritten pieces, or we could just be
5141 * unmapping part of it. But we can't really
5142 * get rid of part of a realtime extent.
5144 if (del
.br_state
== XFS_EXT_UNWRITTEN
||
5145 !xfs_sb_version_hasextflgbit(&mp
->m_sb
)) {
5147 * This piece is unwritten, or we're not
5148 * using unwritten extents. Skip over it.
5151 bno
-= mod
> del
.br_blockcount
?
5152 del
.br_blockcount
: mod
;
5153 if (bno
< got
.br_startoff
) {
5155 xfs_bmbt_get_all(xfs_iext_get_ext(
5161 * It's written, turn it unwritten.
5162 * This is better than zeroing it.
5164 ASSERT(del
.br_state
== XFS_EXT_NORM
);
5165 ASSERT(xfs_trans_get_block_res(tp
) > 0);
5167 * If this spans a realtime extent boundary,
5168 * chop it back to the start of the one we end at.
5170 if (del
.br_blockcount
> mod
) {
5171 del
.br_startoff
+= del
.br_blockcount
- mod
;
5172 del
.br_startblock
+= del
.br_blockcount
- mod
;
5173 del
.br_blockcount
= mod
;
5175 del
.br_state
= XFS_EXT_UNWRITTEN
;
5176 error
= xfs_bmap_add_extent_unwritten_real(tp
, ip
,
5177 &lastx
, &cur
, &del
, firstblock
, flist
,
5183 if (isrt
&& (mod
= do_mod(del
.br_startblock
, mp
->m_sb
.sb_rextsize
))) {
5185 * Realtime extent is lined up at the end but not
5186 * at the front. We'll get rid of full extents if
5189 mod
= mp
->m_sb
.sb_rextsize
- mod
;
5190 if (del
.br_blockcount
> mod
) {
5191 del
.br_blockcount
-= mod
;
5192 del
.br_startoff
+= mod
;
5193 del
.br_startblock
+= mod
;
5194 } else if ((del
.br_startoff
== start
&&
5195 (del
.br_state
== XFS_EXT_UNWRITTEN
||
5196 xfs_trans_get_block_res(tp
) == 0)) ||
5197 !xfs_sb_version_hasextflgbit(&mp
->m_sb
)) {
5199 * Can't make it unwritten. There isn't
5200 * a full extent here so just skip it.
5202 ASSERT(bno
>= del
.br_blockcount
);
5203 bno
-= del
.br_blockcount
;
5204 if (got
.br_startoff
> bno
) {
5206 ep
= xfs_iext_get_ext(ifp
,
5208 xfs_bmbt_get_all(ep
, &got
);
5212 } else if (del
.br_state
== XFS_EXT_UNWRITTEN
) {
5214 * This one is already unwritten.
5215 * It must have a written left neighbor.
5216 * Unwrite the killed part of that one and
5220 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
,
5222 ASSERT(prev
.br_state
== XFS_EXT_NORM
);
5223 ASSERT(!isnullstartblock(prev
.br_startblock
));
5224 ASSERT(del
.br_startblock
==
5225 prev
.br_startblock
+ prev
.br_blockcount
);
5226 if (prev
.br_startoff
< start
) {
5227 mod
= start
- prev
.br_startoff
;
5228 prev
.br_blockcount
-= mod
;
5229 prev
.br_startblock
+= mod
;
5230 prev
.br_startoff
= start
;
5232 prev
.br_state
= XFS_EXT_UNWRITTEN
;
5234 error
= xfs_bmap_add_extent_unwritten_real(tp
,
5235 ip
, &lastx
, &cur
, &prev
,
5236 firstblock
, flist
, &logflags
);
5241 ASSERT(del
.br_state
== XFS_EXT_NORM
);
5242 del
.br_state
= XFS_EXT_UNWRITTEN
;
5243 error
= xfs_bmap_add_extent_unwritten_real(tp
,
5244 ip
, &lastx
, &cur
, &del
,
5245 firstblock
, flist
, &logflags
);
5252 ASSERT(startblockval(del
.br_startblock
) > 0);
5253 /* Update realtime/data freespace, unreserve quota */
5255 xfs_filblks_t rtexts
;
5257 rtexts
= XFS_FSB_TO_B(mp
, del
.br_blockcount
);
5258 do_div(rtexts
, mp
->m_sb
.sb_rextsize
);
5259 xfs_mod_incore_sb(mp
, XFS_SBS_FREXTENTS
,
5260 (int64_t)rtexts
, 0);
5261 (void)xfs_trans_reserve_quota_nblks(NULL
,
5262 ip
, -((long)del
.br_blockcount
), 0,
5263 XFS_QMOPT_RES_RTBLKS
);
5265 xfs_icsb_modify_counters(mp
, XFS_SBS_FDBLOCKS
,
5266 (int64_t)del
.br_blockcount
, 0);
5267 (void)xfs_trans_reserve_quota_nblks(NULL
,
5268 ip
, -((long)del
.br_blockcount
), 0,
5269 XFS_QMOPT_RES_REGBLKS
);
5271 ip
->i_delayed_blks
-= del
.br_blockcount
;
5273 cur
->bc_private
.b
.flags
|=
5274 XFS_BTCUR_BPRV_WASDEL
;
5276 cur
->bc_private
.b
.flags
&= ~XFS_BTCUR_BPRV_WASDEL
;
5278 * If it's the case where the directory code is running
5279 * with no block reservation, and the deleted block is in
5280 * the middle of its extent, and the resulting insert
5281 * of an extent would cause transformation to btree format,
5282 * then reject it. The calling code will then swap
5283 * blocks around instead.
5284 * We have to do this now, rather than waiting for the
5285 * conversion to btree format, since the transaction
5288 if (!wasdel
&& xfs_trans_get_block_res(tp
) == 0 &&
5289 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
&&
5290 XFS_IFORK_NEXTENTS(ip
, whichfork
) >= /* Note the >= */
5291 XFS_IFORK_MAXEXT(ip
, whichfork
) &&
5292 del
.br_startoff
> got
.br_startoff
&&
5293 del
.br_startoff
+ del
.br_blockcount
<
5294 got
.br_startoff
+ got
.br_blockcount
) {
5298 error
= xfs_bmap_del_extent(ip
, tp
, &lastx
, flist
, cur
, &del
,
5299 &tmp_logflags
, whichfork
);
5300 logflags
|= tmp_logflags
;
5303 bno
= del
.br_startoff
- 1;
5306 * If not done go on to the next (previous) record.
5308 if (bno
!= (xfs_fileoff_t
)-1 && bno
>= start
) {
5310 ep
= xfs_iext_get_ext(ifp
, lastx
);
5311 if (xfs_bmbt_get_startoff(ep
) > bno
) {
5313 ep
= xfs_iext_get_ext(ifp
,
5316 xfs_bmbt_get_all(ep
, &got
);
5321 *done
= bno
== (xfs_fileoff_t
)-1 || bno
< start
|| lastx
< 0;
5324 * Convert to a btree if necessary.
5326 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
5327 ASSERT(cur
== NULL
);
5328 error
= xfs_bmap_extents_to_btree(tp
, ip
, firstblock
, flist
,
5329 &cur
, 0, &tmp_logflags
, whichfork
);
5330 logflags
|= tmp_logflags
;
5335 * transform from btree to extents, give it cur
5337 else if (xfs_bmap_wants_extents(ip
, whichfork
)) {
5338 ASSERT(cur
!= NULL
);
5339 error
= xfs_bmap_btree_to_extents(tp
, ip
, cur
, &tmp_logflags
,
5341 logflags
|= tmp_logflags
;
5346 * transform from extents to local?
5351 * Log everything. Do this after conversion, there's no point in
5352 * logging the extent records if we've converted to btree format.
5354 if ((logflags
& xfs_ilog_fext(whichfork
)) &&
5355 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
5356 logflags
&= ~xfs_ilog_fext(whichfork
);
5357 else if ((logflags
& xfs_ilog_fbroot(whichfork
)) &&
5358 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)
5359 logflags
&= ~xfs_ilog_fbroot(whichfork
);
5361 * Log inode even in the error case, if the transaction
5362 * is dirty we'll need to shut down the filesystem.
5365 xfs_trans_log_inode(tp
, ip
, logflags
);
5368 *firstblock
= cur
->bc_private
.b
.firstblock
;
5369 cur
->bc_private
.b
.allocated
= 0;
5371 xfs_btree_del_cursor(cur
,
5372 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
5378 * Shift extent records to the left to cover a hole.
5380 * The maximum number of extents to be shifted in a single operation
5381 * is @num_exts, and @current_ext keeps track of the current extent
5382 * index we have shifted. @offset_shift_fsb is the length by which each
5383 * extent is shifted. If there is no hole to shift the extents
5384 * into, this will be considered invalid operation and we abort immediately.
5387 xfs_bmap_shift_extents(
5388 struct xfs_trans
*tp
,
5389 struct xfs_inode
*ip
,
5391 xfs_fileoff_t start_fsb
,
5392 xfs_fileoff_t offset_shift_fsb
,
5393 xfs_extnum_t
*current_ext
,
5394 xfs_fsblock_t
*firstblock
,
5395 struct xfs_bmap_free
*flist
,
5398 struct xfs_btree_cur
*cur
;
5399 struct xfs_bmbt_rec_host
*gotp
;
5400 struct xfs_bmbt_irec got
;
5401 struct xfs_bmbt_irec left
;
5402 struct xfs_mount
*mp
= ip
->i_mount
;
5403 struct xfs_ifork
*ifp
;
5404 xfs_extnum_t nexts
= 0;
5405 xfs_fileoff_t startoff
;
5408 int whichfork
= XFS_DATA_FORK
;
5410 xfs_filblks_t blockcount
= 0;
5413 if (unlikely(XFS_TEST_ERROR(
5414 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
5415 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
5416 mp
, XFS_ERRTAG_BMAPIFORMAT
, XFS_RANDOM_BMAPIFORMAT
))) {
5417 XFS_ERROR_REPORT("xfs_bmap_shift_extents",
5418 XFS_ERRLEVEL_LOW
, mp
);
5419 return -EFSCORRUPTED
;
5422 if (XFS_FORCED_SHUTDOWN(mp
))
5425 ASSERT(current_ext
!= NULL
);
5427 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5428 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
5429 /* Read in all the extents */
5430 error
= xfs_iread_extents(tp
, ip
, whichfork
);
5436 * If *current_ext is 0, we would need to lookup the extent
5437 * from where we would start shifting and store it in gotp.
5439 if (!*current_ext
) {
5440 gotp
= xfs_iext_bno_to_ext(ifp
, start_fsb
, current_ext
);
5442 * gotp can be null in 2 cases: 1) if there are no extents
5443 * or 2) start_fsb lies in a hole beyond which there are
5444 * no extents. Either way, we are done.
5452 /* We are going to change core inode */
5453 logflags
= XFS_ILOG_CORE
;
5454 if (ifp
->if_flags
& XFS_IFBROOT
) {
5455 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
5456 cur
->bc_private
.b
.firstblock
= *firstblock
;
5457 cur
->bc_private
.b
.flist
= flist
;
5458 cur
->bc_private
.b
.flags
= 0;
5461 logflags
|= XFS_ILOG_DEXT
;
5465 * There may be delalloc extents in the data fork before the range we
5466 * are collapsing out, so we cannot
5467 * use the count of real extents here. Instead we have to calculate it
5468 * from the incore fork.
5470 total_extents
= ifp
->if_bytes
/ sizeof(xfs_bmbt_rec_t
);
5471 while (nexts
++ < num_exts
&& *current_ext
< total_extents
) {
5473 gotp
= xfs_iext_get_ext(ifp
, *current_ext
);
5474 xfs_bmbt_get_all(gotp
, &got
);
5475 startoff
= got
.br_startoff
- offset_shift_fsb
;
5478 * Before shifting extent into hole, make sure that the hole
5479 * is large enough to accomodate the shift.
5482 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
,
5483 *current_ext
- 1), &left
);
5485 if (startoff
< left
.br_startoff
+ left
.br_blockcount
)
5487 } else if (offset_shift_fsb
> got
.br_startoff
) {
5489 * When first extent is shifted, offset_shift_fsb
5490 * should be less than the stating offset of
5500 error
= xfs_bmbt_lookup_eq(cur
, got
.br_startoff
,
5506 XFS_WANT_CORRUPTED_GOTO(i
== 1, del_cursor
);
5509 /* Check if we can merge 2 adjacent extents */
5511 left
.br_startoff
+ left
.br_blockcount
== startoff
&&
5512 left
.br_startblock
+ left
.br_blockcount
==
5513 got
.br_startblock
&&
5514 left
.br_state
== got
.br_state
&&
5515 left
.br_blockcount
+ got
.br_blockcount
<= MAXEXTLEN
) {
5516 blockcount
= left
.br_blockcount
+
5518 xfs_iext_remove(ip
, *current_ext
, 1, 0);
5520 error
= xfs_btree_delete(cur
, &i
);
5523 XFS_WANT_CORRUPTED_GOTO(i
== 1, del_cursor
);
5525 XFS_IFORK_NEXT_SET(ip
, whichfork
,
5526 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
5527 gotp
= xfs_iext_get_ext(ifp
, --*current_ext
);
5528 xfs_bmbt_get_all(gotp
, &got
);
5530 /* Make cursor point to the extent we will update */
5532 error
= xfs_bmbt_lookup_eq(cur
, got
.br_startoff
,
5538 XFS_WANT_CORRUPTED_GOTO(i
== 1, del_cursor
);
5541 xfs_bmbt_set_blockcount(gotp
, blockcount
);
5542 got
.br_blockcount
= blockcount
;
5544 /* We have to update the startoff */
5545 xfs_bmbt_set_startoff(gotp
, startoff
);
5546 got
.br_startoff
= startoff
;
5550 error
= xfs_bmbt_update(cur
, got
.br_startoff
,
5559 total_extents
= ifp
->if_bytes
/ sizeof(xfs_bmbt_rec_t
);
5562 /* Check if we are done */
5563 if (*current_ext
== total_extents
)
5568 xfs_btree_del_cursor(cur
,
5569 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
5571 xfs_trans_log_inode(tp
, ip
, logflags
);