2 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 #include "xfs_shared.h"
21 #include "xfs_format.h"
22 #include "xfs_log_format.h"
23 #include "xfs_trans_resv.h"
27 #include "xfs_mount.h"
28 #include "xfs_da_format.h"
29 #include "xfs_da_btree.h"
31 #include "xfs_inode.h"
32 #include "xfs_btree.h"
33 #include "xfs_trans.h"
34 #include "xfs_inode_item.h"
35 #include "xfs_extfree_item.h"
36 #include "xfs_alloc.h"
38 #include "xfs_bmap_util.h"
39 #include "xfs_bmap_btree.h"
40 #include "xfs_rtalloc.h"
41 #include "xfs_error.h"
42 #include "xfs_quota.h"
43 #include "xfs_trans_space.h"
44 #include "xfs_buf_item.h"
45 #include "xfs_trace.h"
46 #include "xfs_symlink.h"
47 #include "xfs_attr_leaf.h"
48 #include "xfs_filestream.h"
51 kmem_zone_t
*xfs_bmap_free_item_zone
;
54 * Miscellaneous helper functions
58 * Compute and fill in the value of the maximum depth of a bmap btree
59 * in this filesystem. Done once, during mount.
62 xfs_bmap_compute_maxlevels(
63 xfs_mount_t
*mp
, /* file system mount structure */
64 int whichfork
) /* data or attr fork */
66 int level
; /* btree level */
67 uint maxblocks
; /* max blocks at this level */
68 uint maxleafents
; /* max leaf entries possible */
69 int maxrootrecs
; /* max records in root block */
70 int minleafrecs
; /* min records in leaf block */
71 int minnoderecs
; /* min records in node block */
72 int sz
; /* root block size */
75 * The maximum number of extents in a file, hence the maximum
76 * number of leaf entries, is controlled by the type of di_nextents
77 * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
78 * (a signed 16-bit number, xfs_aextnum_t).
80 * Note that we can no longer assume that if we are in ATTR1 that
81 * the fork offset of all the inodes will be
82 * (xfs_default_attroffset(ip) >> 3) because we could have mounted
83 * with ATTR2 and then mounted back with ATTR1, keeping the
84 * di_forkoff's fixed but probably at various positions. Therefore,
85 * for both ATTR1 and ATTR2 we have to assume the worst case scenario
86 * of a minimum size available.
88 if (whichfork
== XFS_DATA_FORK
) {
89 maxleafents
= MAXEXTNUM
;
90 sz
= XFS_BMDR_SPACE_CALC(MINDBTPTRS
);
92 maxleafents
= MAXAEXTNUM
;
93 sz
= XFS_BMDR_SPACE_CALC(MINABTPTRS
);
95 maxrootrecs
= xfs_bmdr_maxrecs(sz
, 0);
96 minleafrecs
= mp
->m_bmap_dmnr
[0];
97 minnoderecs
= mp
->m_bmap_dmnr
[1];
98 maxblocks
= (maxleafents
+ minleafrecs
- 1) / minleafrecs
;
99 for (level
= 1; maxblocks
> 1; level
++) {
100 if (maxblocks
<= maxrootrecs
)
103 maxblocks
= (maxblocks
+ minnoderecs
- 1) / minnoderecs
;
105 mp
->m_bm_maxlevels
[whichfork
] = level
;
108 STATIC
int /* error */
110 struct xfs_btree_cur
*cur
,
114 int *stat
) /* success/failure */
116 cur
->bc_rec
.b
.br_startoff
= off
;
117 cur
->bc_rec
.b
.br_startblock
= bno
;
118 cur
->bc_rec
.b
.br_blockcount
= len
;
119 return xfs_btree_lookup(cur
, XFS_LOOKUP_EQ
, stat
);
122 STATIC
int /* error */
124 struct xfs_btree_cur
*cur
,
128 int *stat
) /* success/failure */
130 cur
->bc_rec
.b
.br_startoff
= off
;
131 cur
->bc_rec
.b
.br_startblock
= bno
;
132 cur
->bc_rec
.b
.br_blockcount
= len
;
133 return xfs_btree_lookup(cur
, XFS_LOOKUP_GE
, stat
);
137 * Check if the inode needs to be converted to btree format.
139 static inline bool xfs_bmap_needs_btree(struct xfs_inode
*ip
, int whichfork
)
141 return XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
&&
142 XFS_IFORK_NEXTENTS(ip
, whichfork
) >
143 XFS_IFORK_MAXEXT(ip
, whichfork
);
147 * Check if the inode should be converted to extent format.
149 static inline bool xfs_bmap_wants_extents(struct xfs_inode
*ip
, int whichfork
)
151 return XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
&&
152 XFS_IFORK_NEXTENTS(ip
, whichfork
) <=
153 XFS_IFORK_MAXEXT(ip
, whichfork
);
157 * Update the record referred to by cur to the value given
158 * by [off, bno, len, state].
159 * This either works (return 0) or gets an EFSCORRUPTED error.
163 struct xfs_btree_cur
*cur
,
169 union xfs_btree_rec rec
;
171 xfs_bmbt_disk_set_allf(&rec
.bmbt
, off
, bno
, len
, state
);
172 return xfs_btree_update(cur
, &rec
);
176 * Compute the worst-case number of indirect blocks that will be used
177 * for ip's delayed extent of length "len".
180 xfs_bmap_worst_indlen(
181 xfs_inode_t
*ip
, /* incore inode pointer */
182 xfs_filblks_t len
) /* delayed extent length */
184 int level
; /* btree level number */
185 int maxrecs
; /* maximum record count at this level */
186 xfs_mount_t
*mp
; /* mount structure */
187 xfs_filblks_t rval
; /* return value */
190 maxrecs
= mp
->m_bmap_dmxr
[0];
191 for (level
= 0, rval
= 0;
192 level
< XFS_BM_MAXLEVELS(mp
, XFS_DATA_FORK
);
195 do_div(len
, maxrecs
);
198 return rval
+ XFS_BM_MAXLEVELS(mp
, XFS_DATA_FORK
) -
201 maxrecs
= mp
->m_bmap_dmxr
[1];
207 * Calculate the default attribute fork offset for newly created inodes.
210 xfs_default_attroffset(
211 struct xfs_inode
*ip
)
213 struct xfs_mount
*mp
= ip
->i_mount
;
216 if (mp
->m_sb
.sb_inodesize
== 256) {
217 offset
= XFS_LITINO(mp
, ip
->i_d
.di_version
) -
218 XFS_BMDR_SPACE_CALC(MINABTPTRS
);
220 offset
= XFS_BMDR_SPACE_CALC(6 * MINABTPTRS
);
223 ASSERT(offset
< XFS_LITINO(mp
, ip
->i_d
.di_version
));
228 * Helper routine to reset inode di_forkoff field when switching
229 * attribute fork from local to extent format - we reset it where
230 * possible to make space available for inline data fork extents.
233 xfs_bmap_forkoff_reset(
237 if (whichfork
== XFS_ATTR_FORK
&&
238 ip
->i_d
.di_format
!= XFS_DINODE_FMT_DEV
&&
239 ip
->i_d
.di_format
!= XFS_DINODE_FMT_UUID
&&
240 ip
->i_d
.di_format
!= XFS_DINODE_FMT_BTREE
) {
241 uint dfl_forkoff
= xfs_default_attroffset(ip
) >> 3;
243 if (dfl_forkoff
> ip
->i_d
.di_forkoff
)
244 ip
->i_d
.di_forkoff
= dfl_forkoff
;
249 * Debug/sanity checking code
253 xfs_bmap_sanity_check(
254 struct xfs_mount
*mp
,
258 struct xfs_btree_block
*block
= XFS_BUF_TO_BLOCK(bp
);
260 if (block
->bb_magic
!= cpu_to_be32(XFS_BMAP_CRC_MAGIC
) &&
261 block
->bb_magic
!= cpu_to_be32(XFS_BMAP_MAGIC
))
264 if (be16_to_cpu(block
->bb_level
) != level
||
265 be16_to_cpu(block
->bb_numrecs
) == 0 ||
266 be16_to_cpu(block
->bb_numrecs
) > mp
->m_bmap_dmxr
[level
!= 0])
273 STATIC
struct xfs_buf
*
275 struct xfs_btree_cur
*cur
,
278 struct xfs_log_item_desc
*lidp
;
284 for (i
= 0; i
< XFS_BTREE_MAXLEVELS
; i
++) {
285 if (!cur
->bc_bufs
[i
])
287 if (XFS_BUF_ADDR(cur
->bc_bufs
[i
]) == bno
)
288 return cur
->bc_bufs
[i
];
291 /* Chase down all the log items to see if the bp is there */
292 list_for_each_entry(lidp
, &cur
->bc_tp
->t_items
, lid_trans
) {
293 struct xfs_buf_log_item
*bip
;
294 bip
= (struct xfs_buf_log_item
*)lidp
->lid_item
;
295 if (bip
->bli_item
.li_type
== XFS_LI_BUF
&&
296 XFS_BUF_ADDR(bip
->bli_buf
) == bno
)
305 struct xfs_btree_block
*block
,
311 __be64
*pp
, *thispa
; /* pointer to block address */
312 xfs_bmbt_key_t
*prevp
, *keyp
;
314 ASSERT(be16_to_cpu(block
->bb_level
) > 0);
317 for( i
= 1; i
<= xfs_btree_get_numrecs(block
); i
++) {
318 dmxr
= mp
->m_bmap_dmxr
[0];
319 keyp
= XFS_BMBT_KEY_ADDR(mp
, block
, i
);
322 ASSERT(be64_to_cpu(prevp
->br_startoff
) <
323 be64_to_cpu(keyp
->br_startoff
));
328 * Compare the block numbers to see if there are dups.
331 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, i
, sz
);
333 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, i
, dmxr
);
335 for (j
= i
+1; j
<= be16_to_cpu(block
->bb_numrecs
); j
++) {
337 thispa
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, j
, sz
);
339 thispa
= XFS_BMBT_PTR_ADDR(mp
, block
, j
, dmxr
);
340 if (*thispa
== *pp
) {
341 xfs_warn(mp
, "%s: thispa(%d) == pp(%d) %Ld",
343 (unsigned long long)be64_to_cpu(*thispa
));
344 panic("%s: ptrs are equal in node\n",
352 * Check that the extents for the inode ip are in the right order in all
357 xfs_bmap_check_leaf_extents(
358 xfs_btree_cur_t
*cur
, /* btree cursor or null */
359 xfs_inode_t
*ip
, /* incore inode pointer */
360 int whichfork
) /* data or attr fork */
362 struct xfs_btree_block
*block
; /* current btree block */
363 xfs_fsblock_t bno
; /* block # of "block" */
364 xfs_buf_t
*bp
; /* buffer for "block" */
365 int error
; /* error return value */
366 xfs_extnum_t i
=0, j
; /* index into the extents list */
367 xfs_ifork_t
*ifp
; /* fork structure */
368 int level
; /* btree level, for checking */
369 xfs_mount_t
*mp
; /* file system mount structure */
370 __be64
*pp
; /* pointer to block address */
371 xfs_bmbt_rec_t
*ep
; /* pointer to current extent */
372 xfs_bmbt_rec_t last
= {0, 0}; /* last extent in prev block */
373 xfs_bmbt_rec_t
*nextp
; /* pointer to next extent */
376 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
) {
382 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
383 block
= ifp
->if_broot
;
385 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
387 level
= be16_to_cpu(block
->bb_level
);
389 xfs_check_block(block
, mp
, 1, ifp
->if_broot_bytes
);
390 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, 1, ifp
->if_broot_bytes
);
391 bno
= be64_to_cpu(*pp
);
393 ASSERT(bno
!= NULLFSBLOCK
);
394 ASSERT(XFS_FSB_TO_AGNO(mp
, bno
) < mp
->m_sb
.sb_agcount
);
395 ASSERT(XFS_FSB_TO_AGBNO(mp
, bno
) < mp
->m_sb
.sb_agblocks
);
398 * Go down the tree until leaf level is reached, following the first
399 * pointer (leftmost) at each level.
401 while (level
-- > 0) {
402 /* See if buf is in cur first */
404 bp
= xfs_bmap_get_bp(cur
, XFS_FSB_TO_DADDR(mp
, bno
));
407 error
= xfs_btree_read_bufl(mp
, NULL
, bno
, 0, &bp
,
413 block
= XFS_BUF_TO_BLOCK(bp
);
414 XFS_WANT_CORRUPTED_GOTO(
415 xfs_bmap_sanity_check(mp
, bp
, level
),
421 * Check this block for basic sanity (increasing keys and
422 * no duplicate blocks).
425 xfs_check_block(block
, mp
, 0, 0);
426 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, mp
->m_bmap_dmxr
[1]);
427 bno
= be64_to_cpu(*pp
);
428 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp
, bno
), error0
);
431 xfs_trans_brelse(NULL
, bp
);
436 * Here with bp and block set to the leftmost leaf node in the tree.
441 * Loop over all leaf nodes checking that all extents are in the right order.
444 xfs_fsblock_t nextbno
;
445 xfs_extnum_t num_recs
;
448 num_recs
= xfs_btree_get_numrecs(block
);
451 * Read-ahead the next leaf block, if any.
454 nextbno
= be64_to_cpu(block
->bb_u
.l
.bb_rightsib
);
457 * Check all the extents to make sure they are OK.
458 * If we had a previous block, the last entry should
459 * conform with the first entry in this one.
462 ep
= XFS_BMBT_REC_ADDR(mp
, block
, 1);
464 ASSERT(xfs_bmbt_disk_get_startoff(&last
) +
465 xfs_bmbt_disk_get_blockcount(&last
) <=
466 xfs_bmbt_disk_get_startoff(ep
));
468 for (j
= 1; j
< num_recs
; j
++) {
469 nextp
= XFS_BMBT_REC_ADDR(mp
, block
, j
+ 1);
470 ASSERT(xfs_bmbt_disk_get_startoff(ep
) +
471 xfs_bmbt_disk_get_blockcount(ep
) <=
472 xfs_bmbt_disk_get_startoff(nextp
));
480 xfs_trans_brelse(NULL
, bp
);
484 * If we've reached the end, stop.
486 if (bno
== NULLFSBLOCK
)
490 bp
= xfs_bmap_get_bp(cur
, XFS_FSB_TO_DADDR(mp
, bno
));
493 error
= xfs_btree_read_bufl(mp
, NULL
, bno
, 0, &bp
,
499 block
= XFS_BUF_TO_BLOCK(bp
);
503 xfs_trans_brelse(NULL
, bp
);
508 xfs_warn(mp
, "%s: at error0", __func__
);
510 xfs_trans_brelse(NULL
, bp
);
512 xfs_warn(mp
, "%s: BAD after btree leaves for %d extents",
514 panic("%s: CORRUPTED BTREE OR SOMETHING", __func__
);
519 * Add bmap trace insert entries for all the contents of the extent records.
522 xfs_bmap_trace_exlist(
523 xfs_inode_t
*ip
, /* incore inode pointer */
524 xfs_extnum_t cnt
, /* count of entries in the list */
525 int whichfork
, /* data or attr fork */
526 unsigned long caller_ip
)
528 xfs_extnum_t idx
; /* extent record index */
529 xfs_ifork_t
*ifp
; /* inode fork pointer */
532 if (whichfork
== XFS_ATTR_FORK
)
533 state
|= BMAP_ATTRFORK
;
535 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
536 ASSERT(cnt
== (ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
)));
537 for (idx
= 0; idx
< cnt
; idx
++)
538 trace_xfs_extlist(ip
, idx
, whichfork
, caller_ip
);
542 * Validate that the bmbt_irecs being returned from bmapi are valid
543 * given the caller's original parameters. Specifically check the
544 * ranges of the returned irecs to ensure that they only extend beyond
545 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
548 xfs_bmap_validate_ret(
552 xfs_bmbt_irec_t
*mval
,
556 int i
; /* index to map values */
558 ASSERT(ret_nmap
<= nmap
);
560 for (i
= 0; i
< ret_nmap
; i
++) {
561 ASSERT(mval
[i
].br_blockcount
> 0);
562 if (!(flags
& XFS_BMAPI_ENTIRE
)) {
563 ASSERT(mval
[i
].br_startoff
>= bno
);
564 ASSERT(mval
[i
].br_blockcount
<= len
);
565 ASSERT(mval
[i
].br_startoff
+ mval
[i
].br_blockcount
<=
568 ASSERT(mval
[i
].br_startoff
< bno
+ len
);
569 ASSERT(mval
[i
].br_startoff
+ mval
[i
].br_blockcount
>
573 mval
[i
- 1].br_startoff
+ mval
[i
- 1].br_blockcount
==
574 mval
[i
].br_startoff
);
575 ASSERT(mval
[i
].br_startblock
!= DELAYSTARTBLOCK
&&
576 mval
[i
].br_startblock
!= HOLESTARTBLOCK
);
577 ASSERT(mval
[i
].br_state
== XFS_EXT_NORM
||
578 mval
[i
].br_state
== XFS_EXT_UNWRITTEN
);
583 #define xfs_bmap_check_leaf_extents(cur, ip, whichfork) do { } while (0)
584 #define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap)
588 * bmap free list manipulation functions
592 * Add the extent to the list of extents to be free at transaction end.
593 * The list is maintained sorted (by block number).
597 xfs_fsblock_t bno
, /* fs block number of extent */
598 xfs_filblks_t len
, /* length of extent */
599 xfs_bmap_free_t
*flist
, /* list of extents */
600 xfs_mount_t
*mp
) /* mount point structure */
602 xfs_bmap_free_item_t
*cur
; /* current (next) element */
603 xfs_bmap_free_item_t
*new; /* new element */
604 xfs_bmap_free_item_t
*prev
; /* previous element */
609 ASSERT(bno
!= NULLFSBLOCK
);
611 ASSERT(len
<= MAXEXTLEN
);
612 ASSERT(!isnullstartblock(bno
));
613 agno
= XFS_FSB_TO_AGNO(mp
, bno
);
614 agbno
= XFS_FSB_TO_AGBNO(mp
, bno
);
615 ASSERT(agno
< mp
->m_sb
.sb_agcount
);
616 ASSERT(agbno
< mp
->m_sb
.sb_agblocks
);
617 ASSERT(len
< mp
->m_sb
.sb_agblocks
);
618 ASSERT(agbno
+ len
<= mp
->m_sb
.sb_agblocks
);
620 ASSERT(xfs_bmap_free_item_zone
!= NULL
);
621 new = kmem_zone_alloc(xfs_bmap_free_item_zone
, KM_SLEEP
);
622 new->xbfi_startblock
= bno
;
623 new->xbfi_blockcount
= (xfs_extlen_t
)len
;
624 for (prev
= NULL
, cur
= flist
->xbf_first
;
626 prev
= cur
, cur
= cur
->xbfi_next
) {
627 if (cur
->xbfi_startblock
>= bno
)
631 prev
->xbfi_next
= new;
633 flist
->xbf_first
= new;
634 new->xbfi_next
= cur
;
639 * Remove the entry "free" from the free item list. Prev points to the
640 * previous entry, unless "free" is the head of the list.
644 xfs_bmap_free_t
*flist
, /* free item list header */
645 xfs_bmap_free_item_t
*prev
, /* previous item on list, if any */
646 xfs_bmap_free_item_t
*free
) /* list item to be freed */
649 prev
->xbfi_next
= free
->xbfi_next
;
651 flist
->xbf_first
= free
->xbfi_next
;
653 kmem_zone_free(xfs_bmap_free_item_zone
, free
);
657 * Free up any items left in the list.
661 xfs_bmap_free_t
*flist
) /* list of bmap_free_items */
663 xfs_bmap_free_item_t
*free
; /* free list item */
664 xfs_bmap_free_item_t
*next
;
666 if (flist
->xbf_count
== 0)
668 ASSERT(flist
->xbf_first
!= NULL
);
669 for (free
= flist
->xbf_first
; free
; free
= next
) {
670 next
= free
->xbfi_next
;
671 xfs_bmap_del_free(flist
, NULL
, free
);
673 ASSERT(flist
->xbf_count
== 0);
677 * Inode fork format manipulation functions
681 * Transform a btree format file with only one leaf node, where the
682 * extents list will fit in the inode, into an extents format file.
683 * Since the file extents are already in-core, all we have to do is
684 * give up the space for the btree root and pitch the leaf block.
686 STATIC
int /* error */
687 xfs_bmap_btree_to_extents(
688 xfs_trans_t
*tp
, /* transaction pointer */
689 xfs_inode_t
*ip
, /* incore inode pointer */
690 xfs_btree_cur_t
*cur
, /* btree cursor */
691 int *logflagsp
, /* inode logging flags */
692 int whichfork
) /* data or attr fork */
695 struct xfs_btree_block
*cblock
;/* child btree block */
696 xfs_fsblock_t cbno
; /* child block number */
697 xfs_buf_t
*cbp
; /* child block's buffer */
698 int error
; /* error return value */
699 xfs_ifork_t
*ifp
; /* inode fork data */
700 xfs_mount_t
*mp
; /* mount point structure */
701 __be64
*pp
; /* ptr to block address */
702 struct xfs_btree_block
*rblock
;/* root btree block */
705 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
706 ASSERT(ifp
->if_flags
& XFS_IFEXTENTS
);
707 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
);
708 rblock
= ifp
->if_broot
;
709 ASSERT(be16_to_cpu(rblock
->bb_level
) == 1);
710 ASSERT(be16_to_cpu(rblock
->bb_numrecs
) == 1);
711 ASSERT(xfs_bmbt_maxrecs(mp
, ifp
->if_broot_bytes
, 0) == 1);
712 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, rblock
, 1, ifp
->if_broot_bytes
);
713 cbno
= be64_to_cpu(*pp
);
716 if ((error
= xfs_btree_check_lptr(cur
, cbno
, 1)))
719 error
= xfs_btree_read_bufl(mp
, tp
, cbno
, 0, &cbp
, XFS_BMAP_BTREE_REF
,
723 cblock
= XFS_BUF_TO_BLOCK(cbp
);
724 if ((error
= xfs_btree_check_block(cur
, cblock
, 0, cbp
)))
726 xfs_bmap_add_free(cbno
, 1, cur
->bc_private
.b
.flist
, mp
);
727 ip
->i_d
.di_nblocks
--;
728 xfs_trans_mod_dquot_byino(tp
, ip
, XFS_TRANS_DQ_BCOUNT
, -1L);
729 xfs_trans_binval(tp
, cbp
);
730 if (cur
->bc_bufs
[0] == cbp
)
731 cur
->bc_bufs
[0] = NULL
;
732 xfs_iroot_realloc(ip
, -1, whichfork
);
733 ASSERT(ifp
->if_broot
== NULL
);
734 ASSERT((ifp
->if_flags
& XFS_IFBROOT
) == 0);
735 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_EXTENTS
);
736 *logflagsp
= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
741 * Convert an extents-format file into a btree-format file.
742 * The new file will have a root block (in the inode) and a single child block.
744 STATIC
int /* error */
745 xfs_bmap_extents_to_btree(
746 xfs_trans_t
*tp
, /* transaction pointer */
747 xfs_inode_t
*ip
, /* incore inode pointer */
748 xfs_fsblock_t
*firstblock
, /* first-block-allocated */
749 xfs_bmap_free_t
*flist
, /* blocks freed in xaction */
750 xfs_btree_cur_t
**curp
, /* cursor returned to caller */
751 int wasdel
, /* converting a delayed alloc */
752 int *logflagsp
, /* inode logging flags */
753 int whichfork
) /* data or attr fork */
755 struct xfs_btree_block
*ablock
; /* allocated (child) bt block */
756 xfs_buf_t
*abp
; /* buffer for ablock */
757 xfs_alloc_arg_t args
; /* allocation arguments */
758 xfs_bmbt_rec_t
*arp
; /* child record pointer */
759 struct xfs_btree_block
*block
; /* btree root block */
760 xfs_btree_cur_t
*cur
; /* bmap btree cursor */
761 xfs_bmbt_rec_host_t
*ep
; /* extent record pointer */
762 int error
; /* error return value */
763 xfs_extnum_t i
, cnt
; /* extent record index */
764 xfs_ifork_t
*ifp
; /* inode fork pointer */
765 xfs_bmbt_key_t
*kp
; /* root block key pointer */
766 xfs_mount_t
*mp
; /* mount structure */
767 xfs_extnum_t nextents
; /* number of file extents */
768 xfs_bmbt_ptr_t
*pp
; /* root block address pointer */
771 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
772 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
);
775 * Make space in the inode incore.
777 xfs_iroot_realloc(ip
, 1, whichfork
);
778 ifp
->if_flags
|= XFS_IFBROOT
;
783 block
= ifp
->if_broot
;
784 if (xfs_sb_version_hascrc(&mp
->m_sb
))
785 xfs_btree_init_block_int(mp
, block
, XFS_BUF_DADDR_NULL
,
786 XFS_BMAP_CRC_MAGIC
, 1, 1, ip
->i_ino
,
787 XFS_BTREE_LONG_PTRS
| XFS_BTREE_CRC_BLOCKS
);
789 xfs_btree_init_block_int(mp
, block
, XFS_BUF_DADDR_NULL
,
790 XFS_BMAP_MAGIC
, 1, 1, ip
->i_ino
,
791 XFS_BTREE_LONG_PTRS
);
794 * Need a cursor. Can't allocate until bb_level is filled in.
796 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
797 cur
->bc_private
.b
.firstblock
= *firstblock
;
798 cur
->bc_private
.b
.flist
= flist
;
799 cur
->bc_private
.b
.flags
= wasdel
? XFS_BTCUR_BPRV_WASDEL
: 0;
801 * Convert to a btree with two levels, one record in root.
803 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_BTREE
);
804 memset(&args
, 0, sizeof(args
));
807 args
.firstblock
= *firstblock
;
808 if (*firstblock
== NULLFSBLOCK
) {
809 args
.type
= XFS_ALLOCTYPE_START_BNO
;
810 args
.fsbno
= XFS_INO_TO_FSB(mp
, ip
->i_ino
);
811 } else if (flist
->xbf_low
) {
812 args
.type
= XFS_ALLOCTYPE_START_BNO
;
813 args
.fsbno
= *firstblock
;
815 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
816 args
.fsbno
= *firstblock
;
818 args
.minlen
= args
.maxlen
= args
.prod
= 1;
819 args
.wasdel
= wasdel
;
821 if ((error
= xfs_alloc_vextent(&args
))) {
822 xfs_iroot_realloc(ip
, -1, whichfork
);
823 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
827 * Allocation can't fail, the space was reserved.
829 ASSERT(args
.fsbno
!= NULLFSBLOCK
);
830 ASSERT(*firstblock
== NULLFSBLOCK
||
831 args
.agno
== XFS_FSB_TO_AGNO(mp
, *firstblock
) ||
833 args
.agno
> XFS_FSB_TO_AGNO(mp
, *firstblock
)));
834 *firstblock
= cur
->bc_private
.b
.firstblock
= args
.fsbno
;
835 cur
->bc_private
.b
.allocated
++;
836 ip
->i_d
.di_nblocks
++;
837 xfs_trans_mod_dquot_byino(tp
, ip
, XFS_TRANS_DQ_BCOUNT
, 1L);
838 abp
= xfs_btree_get_bufl(mp
, tp
, args
.fsbno
, 0);
840 * Fill in the child block.
842 abp
->b_ops
= &xfs_bmbt_buf_ops
;
843 ablock
= XFS_BUF_TO_BLOCK(abp
);
844 if (xfs_sb_version_hascrc(&mp
->m_sb
))
845 xfs_btree_init_block_int(mp
, ablock
, abp
->b_bn
,
846 XFS_BMAP_CRC_MAGIC
, 0, 0, ip
->i_ino
,
847 XFS_BTREE_LONG_PTRS
| XFS_BTREE_CRC_BLOCKS
);
849 xfs_btree_init_block_int(mp
, ablock
, abp
->b_bn
,
850 XFS_BMAP_MAGIC
, 0, 0, ip
->i_ino
,
851 XFS_BTREE_LONG_PTRS
);
853 arp
= XFS_BMBT_REC_ADDR(mp
, ablock
, 1);
854 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
855 for (cnt
= i
= 0; i
< nextents
; i
++) {
856 ep
= xfs_iext_get_ext(ifp
, i
);
857 if (!isnullstartblock(xfs_bmbt_get_startblock(ep
))) {
858 arp
->l0
= cpu_to_be64(ep
->l0
);
859 arp
->l1
= cpu_to_be64(ep
->l1
);
863 ASSERT(cnt
== XFS_IFORK_NEXTENTS(ip
, whichfork
));
864 xfs_btree_set_numrecs(ablock
, cnt
);
867 * Fill in the root key and pointer.
869 kp
= XFS_BMBT_KEY_ADDR(mp
, block
, 1);
870 arp
= XFS_BMBT_REC_ADDR(mp
, ablock
, 1);
871 kp
->br_startoff
= cpu_to_be64(xfs_bmbt_disk_get_startoff(arp
));
872 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, xfs_bmbt_get_maxrecs(cur
,
873 be16_to_cpu(block
->bb_level
)));
874 *pp
= cpu_to_be64(args
.fsbno
);
877 * Do all this logging at the end so that
878 * the root is at the right level.
880 xfs_btree_log_block(cur
, abp
, XFS_BB_ALL_BITS
);
881 xfs_btree_log_recs(cur
, abp
, 1, be16_to_cpu(ablock
->bb_numrecs
));
882 ASSERT(*curp
== NULL
);
884 *logflagsp
= XFS_ILOG_CORE
| xfs_ilog_fbroot(whichfork
);
889 * Convert a local file to an extents file.
890 * This code is out of bounds for data forks of regular files,
891 * since the file data needs to get logged so things will stay consistent.
892 * (The bmap-level manipulations are ok, though).
895 xfs_bmap_local_to_extents_empty(
896 struct xfs_inode
*ip
,
899 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
901 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
902 ASSERT(ifp
->if_bytes
== 0);
903 ASSERT(XFS_IFORK_NEXTENTS(ip
, whichfork
) == 0);
905 xfs_bmap_forkoff_reset(ip
, whichfork
);
906 ifp
->if_flags
&= ~XFS_IFINLINE
;
907 ifp
->if_flags
|= XFS_IFEXTENTS
;
908 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_EXTENTS
);
912 STATIC
int /* error */
913 xfs_bmap_local_to_extents(
914 xfs_trans_t
*tp
, /* transaction pointer */
915 xfs_inode_t
*ip
, /* incore inode pointer */
916 xfs_fsblock_t
*firstblock
, /* first block allocated in xaction */
917 xfs_extlen_t total
, /* total blocks needed by transaction */
918 int *logflagsp
, /* inode logging flags */
920 void (*init_fn
)(struct xfs_trans
*tp
,
922 struct xfs_inode
*ip
,
923 struct xfs_ifork
*ifp
))
926 int flags
; /* logging flags returned */
927 xfs_ifork_t
*ifp
; /* inode fork pointer */
928 xfs_alloc_arg_t args
; /* allocation arguments */
929 xfs_buf_t
*bp
; /* buffer for extent block */
930 xfs_bmbt_rec_host_t
*ep
; /* extent record pointer */
933 * We don't want to deal with the case of keeping inode data inline yet.
934 * So sending the data fork of a regular inode is invalid.
936 ASSERT(!(S_ISREG(ip
->i_d
.di_mode
) && whichfork
== XFS_DATA_FORK
));
937 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
938 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
940 if (!ifp
->if_bytes
) {
941 xfs_bmap_local_to_extents_empty(ip
, whichfork
);
942 flags
= XFS_ILOG_CORE
;
948 ASSERT((ifp
->if_flags
& (XFS_IFINLINE
|XFS_IFEXTENTS
|XFS_IFEXTIREC
)) ==
950 memset(&args
, 0, sizeof(args
));
952 args
.mp
= ip
->i_mount
;
953 args
.firstblock
= *firstblock
;
955 * Allocate a block. We know we need only one, since the
956 * file currently fits in an inode.
958 if (*firstblock
== NULLFSBLOCK
) {
959 args
.fsbno
= XFS_INO_TO_FSB(args
.mp
, ip
->i_ino
);
960 args
.type
= XFS_ALLOCTYPE_START_BNO
;
962 args
.fsbno
= *firstblock
;
963 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
966 args
.minlen
= args
.maxlen
= args
.prod
= 1;
967 error
= xfs_alloc_vextent(&args
);
971 /* Can't fail, the space was reserved. */
972 ASSERT(args
.fsbno
!= NULLFSBLOCK
);
973 ASSERT(args
.len
== 1);
974 *firstblock
= args
.fsbno
;
975 bp
= xfs_btree_get_bufl(args
.mp
, tp
, args
.fsbno
, 0);
977 /* initialise the block and copy the data */
978 init_fn(tp
, bp
, ip
, ifp
);
980 /* account for the change in fork size and log everything */
981 xfs_trans_log_buf(tp
, bp
, 0, ifp
->if_bytes
- 1);
982 xfs_idata_realloc(ip
, -ifp
->if_bytes
, whichfork
);
983 xfs_bmap_local_to_extents_empty(ip
, whichfork
);
984 flags
|= XFS_ILOG_CORE
;
986 xfs_iext_add(ifp
, 0, 1);
987 ep
= xfs_iext_get_ext(ifp
, 0);
988 xfs_bmbt_set_allf(ep
, 0, args
.fsbno
, 1, XFS_EXT_NORM
);
989 trace_xfs_bmap_post_update(ip
, 0,
990 whichfork
== XFS_ATTR_FORK
? BMAP_ATTRFORK
: 0,
992 XFS_IFORK_NEXT_SET(ip
, whichfork
, 1);
993 ip
->i_d
.di_nblocks
= 1;
994 xfs_trans_mod_dquot_byino(tp
, ip
,
995 XFS_TRANS_DQ_BCOUNT
, 1L);
996 flags
|= xfs_ilog_fext(whichfork
);
1004 * Called from xfs_bmap_add_attrfork to handle btree format files.
1006 STATIC
int /* error */
1007 xfs_bmap_add_attrfork_btree(
1008 xfs_trans_t
*tp
, /* transaction pointer */
1009 xfs_inode_t
*ip
, /* incore inode pointer */
1010 xfs_fsblock_t
*firstblock
, /* first block allocated */
1011 xfs_bmap_free_t
*flist
, /* blocks to free at commit */
1012 int *flags
) /* inode logging flags */
1014 xfs_btree_cur_t
*cur
; /* btree cursor */
1015 int error
; /* error return value */
1016 xfs_mount_t
*mp
; /* file system mount struct */
1017 int stat
; /* newroot status */
1020 if (ip
->i_df
.if_broot_bytes
<= XFS_IFORK_DSIZE(ip
))
1021 *flags
|= XFS_ILOG_DBROOT
;
1023 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, XFS_DATA_FORK
);
1024 cur
->bc_private
.b
.flist
= flist
;
1025 cur
->bc_private
.b
.firstblock
= *firstblock
;
1026 if ((error
= xfs_bmbt_lookup_ge(cur
, 0, 0, 0, &stat
)))
1028 /* must be at least one entry */
1029 XFS_WANT_CORRUPTED_GOTO(stat
== 1, error0
);
1030 if ((error
= xfs_btree_new_iroot(cur
, flags
, &stat
)))
1033 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
1036 *firstblock
= cur
->bc_private
.b
.firstblock
;
1037 cur
->bc_private
.b
.allocated
= 0;
1038 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
1042 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
1047 * Called from xfs_bmap_add_attrfork to handle extents format files.
1049 STATIC
int /* error */
1050 xfs_bmap_add_attrfork_extents(
1051 xfs_trans_t
*tp
, /* transaction pointer */
1052 xfs_inode_t
*ip
, /* incore inode pointer */
1053 xfs_fsblock_t
*firstblock
, /* first block allocated */
1054 xfs_bmap_free_t
*flist
, /* blocks to free at commit */
1055 int *flags
) /* inode logging flags */
1057 xfs_btree_cur_t
*cur
; /* bmap btree cursor */
1058 int error
; /* error return value */
1060 if (ip
->i_d
.di_nextents
* sizeof(xfs_bmbt_rec_t
) <= XFS_IFORK_DSIZE(ip
))
1063 error
= xfs_bmap_extents_to_btree(tp
, ip
, firstblock
, flist
, &cur
, 0,
1064 flags
, XFS_DATA_FORK
);
1066 cur
->bc_private
.b
.allocated
= 0;
1067 xfs_btree_del_cursor(cur
,
1068 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
1074 * Called from xfs_bmap_add_attrfork to handle local format files. Each
1075 * different data fork content type needs a different callout to do the
1076 * conversion. Some are basic and only require special block initialisation
1077 * callouts for the data formating, others (directories) are so specialised they
1078 * handle everything themselves.
1080 * XXX (dgc): investigate whether directory conversion can use the generic
1081 * formatting callout. It should be possible - it's just a very complex
1084 STATIC
int /* error */
1085 xfs_bmap_add_attrfork_local(
1086 xfs_trans_t
*tp
, /* transaction pointer */
1087 xfs_inode_t
*ip
, /* incore inode pointer */
1088 xfs_fsblock_t
*firstblock
, /* first block allocated */
1089 xfs_bmap_free_t
*flist
, /* blocks to free at commit */
1090 int *flags
) /* inode logging flags */
1092 xfs_da_args_t dargs
; /* args for dir/attr code */
1094 if (ip
->i_df
.if_bytes
<= XFS_IFORK_DSIZE(ip
))
1097 if (S_ISDIR(ip
->i_d
.di_mode
)) {
1098 memset(&dargs
, 0, sizeof(dargs
));
1099 dargs
.geo
= ip
->i_mount
->m_dir_geo
;
1101 dargs
.firstblock
= firstblock
;
1102 dargs
.flist
= flist
;
1103 dargs
.total
= dargs
.geo
->fsbcount
;
1104 dargs
.whichfork
= XFS_DATA_FORK
;
1106 return xfs_dir2_sf_to_block(&dargs
);
1109 if (S_ISLNK(ip
->i_d
.di_mode
))
1110 return xfs_bmap_local_to_extents(tp
, ip
, firstblock
, 1,
1111 flags
, XFS_DATA_FORK
,
1112 xfs_symlink_local_to_remote
);
1114 /* should only be called for types that support local format data */
1116 return -EFSCORRUPTED
;
1120 * Convert inode from non-attributed to attributed.
1121 * Must not be in a transaction, ip must not be locked.
1123 int /* error code */
1124 xfs_bmap_add_attrfork(
1125 xfs_inode_t
*ip
, /* incore inode pointer */
1126 int size
, /* space new attribute needs */
1127 int rsvd
) /* xact may use reserved blks */
1129 xfs_fsblock_t firstblock
; /* 1st block/ag allocated */
1130 xfs_bmap_free_t flist
; /* freed extent records */
1131 xfs_mount_t
*mp
; /* mount structure */
1132 xfs_trans_t
*tp
; /* transaction pointer */
1133 int blks
; /* space reservation */
1134 int version
= 1; /* superblock attr version */
1135 int committed
; /* xaction was committed */
1136 int logflags
; /* logging flags */
1137 int error
; /* error return value */
1138 int cancel_flags
= 0;
1140 ASSERT(XFS_IFORK_Q(ip
) == 0);
1143 ASSERT(!XFS_NOT_DQATTACHED(mp
, ip
));
1144 tp
= xfs_trans_alloc(mp
, XFS_TRANS_ADDAFORK
);
1145 blks
= XFS_ADDAFORK_SPACE_RES(mp
);
1147 tp
->t_flags
|= XFS_TRANS_RESERVE
;
1148 error
= xfs_trans_reserve(tp
, &M_RES(mp
)->tr_addafork
, blks
, 0);
1150 xfs_trans_cancel(tp
, 0);
1153 cancel_flags
= XFS_TRANS_RELEASE_LOG_RES
;
1154 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
1155 error
= xfs_trans_reserve_quota_nblks(tp
, ip
, blks
, 0, rsvd
?
1156 XFS_QMOPT_RES_REGBLKS
| XFS_QMOPT_FORCE_RES
:
1157 XFS_QMOPT_RES_REGBLKS
);
1160 cancel_flags
|= XFS_TRANS_ABORT
;
1161 if (XFS_IFORK_Q(ip
))
1163 if (ip
->i_d
.di_aformat
!= XFS_DINODE_FMT_EXTENTS
) {
1165 * For inodes coming from pre-6.2 filesystems.
1167 ASSERT(ip
->i_d
.di_aformat
== 0);
1168 ip
->i_d
.di_aformat
= XFS_DINODE_FMT_EXTENTS
;
1170 ASSERT(ip
->i_d
.di_anextents
== 0);
1172 xfs_trans_ijoin(tp
, ip
, 0);
1173 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
1175 switch (ip
->i_d
.di_format
) {
1176 case XFS_DINODE_FMT_DEV
:
1177 ip
->i_d
.di_forkoff
= roundup(sizeof(xfs_dev_t
), 8) >> 3;
1179 case XFS_DINODE_FMT_UUID
:
1180 ip
->i_d
.di_forkoff
= roundup(sizeof(uuid_t
), 8) >> 3;
1182 case XFS_DINODE_FMT_LOCAL
:
1183 case XFS_DINODE_FMT_EXTENTS
:
1184 case XFS_DINODE_FMT_BTREE
:
1185 ip
->i_d
.di_forkoff
= xfs_attr_shortform_bytesfit(ip
, size
);
1186 if (!ip
->i_d
.di_forkoff
)
1187 ip
->i_d
.di_forkoff
= xfs_default_attroffset(ip
) >> 3;
1188 else if (mp
->m_flags
& XFS_MOUNT_ATTR2
)
1197 ASSERT(ip
->i_afp
== NULL
);
1198 ip
->i_afp
= kmem_zone_zalloc(xfs_ifork_zone
, KM_SLEEP
);
1199 ip
->i_afp
->if_flags
= XFS_IFEXTENTS
;
1201 xfs_bmap_init(&flist
, &firstblock
);
1202 switch (ip
->i_d
.di_format
) {
1203 case XFS_DINODE_FMT_LOCAL
:
1204 error
= xfs_bmap_add_attrfork_local(tp
, ip
, &firstblock
, &flist
,
1207 case XFS_DINODE_FMT_EXTENTS
:
1208 error
= xfs_bmap_add_attrfork_extents(tp
, ip
, &firstblock
,
1211 case XFS_DINODE_FMT_BTREE
:
1212 error
= xfs_bmap_add_attrfork_btree(tp
, ip
, &firstblock
, &flist
,
1220 xfs_trans_log_inode(tp
, ip
, logflags
);
1223 if (!xfs_sb_version_hasattr(&mp
->m_sb
) ||
1224 (!xfs_sb_version_hasattr2(&mp
->m_sb
) && version
== 2)) {
1225 __int64_t sbfields
= 0;
1227 spin_lock(&mp
->m_sb_lock
);
1228 if (!xfs_sb_version_hasattr(&mp
->m_sb
)) {
1229 xfs_sb_version_addattr(&mp
->m_sb
);
1230 sbfields
|= XFS_SB_VERSIONNUM
;
1232 if (!xfs_sb_version_hasattr2(&mp
->m_sb
) && version
== 2) {
1233 xfs_sb_version_addattr2(&mp
->m_sb
);
1234 sbfields
|= (XFS_SB_VERSIONNUM
| XFS_SB_FEATURES2
);
1237 spin_unlock(&mp
->m_sb_lock
);
1238 xfs_mod_sb(tp
, sbfields
);
1240 spin_unlock(&mp
->m_sb_lock
);
1243 error
= xfs_bmap_finish(&tp
, &flist
, &committed
);
1246 error
= xfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
);
1247 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
1251 xfs_bmap_cancel(&flist
);
1253 xfs_trans_cancel(tp
, cancel_flags
);
1254 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
1259 * Internal and external extent tree search functions.
1263 * Read in the extents to if_extents.
1264 * All inode fields are set up by caller, we just traverse the btree
1265 * and copy the records in. If the file system cannot contain unwritten
1266 * extents, the records are checked for no "state" flags.
1269 xfs_bmap_read_extents(
1270 xfs_trans_t
*tp
, /* transaction pointer */
1271 xfs_inode_t
*ip
, /* incore inode */
1272 int whichfork
) /* data or attr fork */
1274 struct xfs_btree_block
*block
; /* current btree block */
1275 xfs_fsblock_t bno
; /* block # of "block" */
1276 xfs_buf_t
*bp
; /* buffer for "block" */
1277 int error
; /* error return value */
1278 xfs_exntfmt_t exntf
; /* XFS_EXTFMT_NOSTATE, if checking */
1279 xfs_extnum_t i
, j
; /* index into the extents list */
1280 xfs_ifork_t
*ifp
; /* fork structure */
1281 int level
; /* btree level, for checking */
1282 xfs_mount_t
*mp
; /* file system mount structure */
1283 __be64
*pp
; /* pointer to block address */
1285 xfs_extnum_t room
; /* number of entries there's room for */
1289 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1290 exntf
= (whichfork
!= XFS_DATA_FORK
) ? XFS_EXTFMT_NOSTATE
:
1291 XFS_EXTFMT_INODE(ip
);
1292 block
= ifp
->if_broot
;
1294 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
1296 level
= be16_to_cpu(block
->bb_level
);
1298 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, 1, ifp
->if_broot_bytes
);
1299 bno
= be64_to_cpu(*pp
);
1300 ASSERT(bno
!= NULLFSBLOCK
);
1301 ASSERT(XFS_FSB_TO_AGNO(mp
, bno
) < mp
->m_sb
.sb_agcount
);
1302 ASSERT(XFS_FSB_TO_AGBNO(mp
, bno
) < mp
->m_sb
.sb_agblocks
);
1304 * Go down the tree until leaf level is reached, following the first
1305 * pointer (leftmost) at each level.
1307 while (level
-- > 0) {
1308 error
= xfs_btree_read_bufl(mp
, tp
, bno
, 0, &bp
,
1309 XFS_BMAP_BTREE_REF
, &xfs_bmbt_buf_ops
);
1312 block
= XFS_BUF_TO_BLOCK(bp
);
1313 XFS_WANT_CORRUPTED_GOTO(
1314 xfs_bmap_sanity_check(mp
, bp
, level
),
1318 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, mp
->m_bmap_dmxr
[1]);
1319 bno
= be64_to_cpu(*pp
);
1320 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp
, bno
), error0
);
1321 xfs_trans_brelse(tp
, bp
);
1324 * Here with bp and block set to the leftmost leaf node in the tree.
1326 room
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
1329 * Loop over all leaf nodes. Copy information to the extent records.
1332 xfs_bmbt_rec_t
*frp
;
1333 xfs_fsblock_t nextbno
;
1334 xfs_extnum_t num_recs
;
1337 num_recs
= xfs_btree_get_numrecs(block
);
1338 if (unlikely(i
+ num_recs
> room
)) {
1339 ASSERT(i
+ num_recs
<= room
);
1340 xfs_warn(ip
->i_mount
,
1341 "corrupt dinode %Lu, (btree extents).",
1342 (unsigned long long) ip
->i_ino
);
1343 XFS_CORRUPTION_ERROR("xfs_bmap_read_extents(1)",
1344 XFS_ERRLEVEL_LOW
, ip
->i_mount
, block
);
1347 XFS_WANT_CORRUPTED_GOTO(
1348 xfs_bmap_sanity_check(mp
, bp
, 0),
1351 * Read-ahead the next leaf block, if any.
1353 nextbno
= be64_to_cpu(block
->bb_u
.l
.bb_rightsib
);
1354 if (nextbno
!= NULLFSBLOCK
)
1355 xfs_btree_reada_bufl(mp
, nextbno
, 1,
1358 * Copy records into the extent records.
1360 frp
= XFS_BMBT_REC_ADDR(mp
, block
, 1);
1362 for (j
= 0; j
< num_recs
; j
++, i
++, frp
++) {
1363 xfs_bmbt_rec_host_t
*trp
= xfs_iext_get_ext(ifp
, i
);
1364 trp
->l0
= be64_to_cpu(frp
->l0
);
1365 trp
->l1
= be64_to_cpu(frp
->l1
);
1367 if (exntf
== XFS_EXTFMT_NOSTATE
) {
1369 * Check all attribute bmap btree records and
1370 * any "older" data bmap btree records for a
1371 * set bit in the "extent flag" position.
1373 if (unlikely(xfs_check_nostate_extents(ifp
,
1374 start
, num_recs
))) {
1375 XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
1381 xfs_trans_brelse(tp
, bp
);
1384 * If we've reached the end, stop.
1386 if (bno
== NULLFSBLOCK
)
1388 error
= xfs_btree_read_bufl(mp
, tp
, bno
, 0, &bp
,
1389 XFS_BMAP_BTREE_REF
, &xfs_bmbt_buf_ops
);
1392 block
= XFS_BUF_TO_BLOCK(bp
);
1394 ASSERT(i
== (ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
)));
1395 ASSERT(i
== XFS_IFORK_NEXTENTS(ip
, whichfork
));
1396 XFS_BMAP_TRACE_EXLIST(ip
, i
, whichfork
);
1399 xfs_trans_brelse(tp
, bp
);
1400 return -EFSCORRUPTED
;
1405 * Search the extent records for the entry containing block bno.
1406 * If bno lies in a hole, point to the next entry. If bno lies
1407 * past eof, *eofp will be set, and *prevp will contain the last
1408 * entry (null if none). Else, *lastxp will be set to the index
1409 * of the found entry; *gotp will contain the entry.
1411 STATIC xfs_bmbt_rec_host_t
* /* pointer to found extent entry */
1412 xfs_bmap_search_multi_extents(
1413 xfs_ifork_t
*ifp
, /* inode fork pointer */
1414 xfs_fileoff_t bno
, /* block number searched for */
1415 int *eofp
, /* out: end of file found */
1416 xfs_extnum_t
*lastxp
, /* out: last extent index */
1417 xfs_bmbt_irec_t
*gotp
, /* out: extent entry found */
1418 xfs_bmbt_irec_t
*prevp
) /* out: previous extent entry found */
1420 xfs_bmbt_rec_host_t
*ep
; /* extent record pointer */
1421 xfs_extnum_t lastx
; /* last extent index */
1424 * Initialize the extent entry structure to catch access to
1425 * uninitialized br_startblock field.
1427 gotp
->br_startoff
= 0xffa5a5a5a5a5a5a5LL
;
1428 gotp
->br_blockcount
= 0xa55a5a5a5a5a5a5aLL
;
1429 gotp
->br_state
= XFS_EXT_INVALID
;
1430 gotp
->br_startblock
= 0xffffa5a5a5a5a5a5LL
;
1431 prevp
->br_startoff
= NULLFILEOFF
;
1433 ep
= xfs_iext_bno_to_ext(ifp
, bno
, &lastx
);
1435 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, lastx
- 1), prevp
);
1437 if (lastx
< (ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
))) {
1438 xfs_bmbt_get_all(ep
, gotp
);
1452 * Search the extents list for the inode, for the extent containing bno.
1453 * If bno lies in a hole, point to the next entry. If bno lies past eof,
1454 * *eofp will be set, and *prevp will contain the last entry (null if none).
1455 * Else, *lastxp will be set to the index of the found
1456 * entry; *gotp will contain the entry.
1458 STATIC xfs_bmbt_rec_host_t
* /* pointer to found extent entry */
1459 xfs_bmap_search_extents(
1460 xfs_inode_t
*ip
, /* incore inode pointer */
1461 xfs_fileoff_t bno
, /* block number searched for */
1462 int fork
, /* data or attr fork */
1463 int *eofp
, /* out: end of file found */
1464 xfs_extnum_t
*lastxp
, /* out: last extent index */
1465 xfs_bmbt_irec_t
*gotp
, /* out: extent entry found */
1466 xfs_bmbt_irec_t
*prevp
) /* out: previous extent entry found */
1468 xfs_ifork_t
*ifp
; /* inode fork pointer */
1469 xfs_bmbt_rec_host_t
*ep
; /* extent record pointer */
1471 XFS_STATS_INC(xs_look_exlist
);
1472 ifp
= XFS_IFORK_PTR(ip
, fork
);
1474 ep
= xfs_bmap_search_multi_extents(ifp
, bno
, eofp
, lastxp
, gotp
, prevp
);
1476 if (unlikely(!(gotp
->br_startblock
) && (*lastxp
!= NULLEXTNUM
) &&
1477 !(XFS_IS_REALTIME_INODE(ip
) && fork
== XFS_DATA_FORK
))) {
1478 xfs_alert_tag(ip
->i_mount
, XFS_PTAG_FSBLOCK_ZERO
,
1479 "Access to block zero in inode %llu "
1480 "start_block: %llx start_off: %llx "
1481 "blkcnt: %llx extent-state: %x lastx: %x",
1482 (unsigned long long)ip
->i_ino
,
1483 (unsigned long long)gotp
->br_startblock
,
1484 (unsigned long long)gotp
->br_startoff
,
1485 (unsigned long long)gotp
->br_blockcount
,
1486 gotp
->br_state
, *lastxp
);
1487 *lastxp
= NULLEXTNUM
;
1495 * Returns the file-relative block number of the first unused block(s)
1496 * in the file with at least "len" logically contiguous blocks free.
1497 * This is the lowest-address hole if the file has holes, else the first block
1498 * past the end of file.
1499 * Return 0 if the file is currently local (in-inode).
1502 xfs_bmap_first_unused(
1503 xfs_trans_t
*tp
, /* transaction pointer */
1504 xfs_inode_t
*ip
, /* incore inode */
1505 xfs_extlen_t len
, /* size of hole to find */
1506 xfs_fileoff_t
*first_unused
, /* unused block */
1507 int whichfork
) /* data or attr fork */
1509 int error
; /* error return value */
1510 int idx
; /* extent record index */
1511 xfs_ifork_t
*ifp
; /* inode fork pointer */
1512 xfs_fileoff_t lastaddr
; /* last block number seen */
1513 xfs_fileoff_t lowest
; /* lowest useful block */
1514 xfs_fileoff_t max
; /* starting useful block */
1515 xfs_fileoff_t off
; /* offset for this block */
1516 xfs_extnum_t nextents
; /* number of extent entries */
1518 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
||
1519 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
||
1520 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
1521 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
) {
1525 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1526 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
1527 (error
= xfs_iread_extents(tp
, ip
, whichfork
)))
1529 lowest
= *first_unused
;
1530 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
1531 for (idx
= 0, lastaddr
= 0, max
= lowest
; idx
< nextents
; idx
++) {
1532 xfs_bmbt_rec_host_t
*ep
= xfs_iext_get_ext(ifp
, idx
);
1533 off
= xfs_bmbt_get_startoff(ep
);
1535 * See if the hole before this extent will work.
1537 if (off
>= lowest
+ len
&& off
- max
>= len
) {
1538 *first_unused
= max
;
1541 lastaddr
= off
+ xfs_bmbt_get_blockcount(ep
);
1542 max
= XFS_FILEOFF_MAX(lastaddr
, lowest
);
1544 *first_unused
= max
;
1549 * Returns the file-relative block number of the last block - 1 before
1550 * last_block (input value) in the file.
1551 * This is not based on i_size, it is based on the extent records.
1552 * Returns 0 for local files, as they do not have extent records.
1555 xfs_bmap_last_before(
1556 xfs_trans_t
*tp
, /* transaction pointer */
1557 xfs_inode_t
*ip
, /* incore inode */
1558 xfs_fileoff_t
*last_block
, /* last block */
1559 int whichfork
) /* data or attr fork */
1561 xfs_fileoff_t bno
; /* input file offset */
1562 int eof
; /* hit end of file */
1563 xfs_bmbt_rec_host_t
*ep
; /* pointer to last extent */
1564 int error
; /* error return value */
1565 xfs_bmbt_irec_t got
; /* current extent value */
1566 xfs_ifork_t
*ifp
; /* inode fork pointer */
1567 xfs_extnum_t lastx
; /* last extent used */
1568 xfs_bmbt_irec_t prev
; /* previous extent value */
1570 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
&&
1571 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
1572 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_LOCAL
)
1574 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
) {
1578 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1579 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
1580 (error
= xfs_iread_extents(tp
, ip
, whichfork
)))
1582 bno
= *last_block
- 1;
1583 ep
= xfs_bmap_search_extents(ip
, bno
, whichfork
, &eof
, &lastx
, &got
,
1585 if (eof
|| xfs_bmbt_get_startoff(ep
) > bno
) {
1586 if (prev
.br_startoff
== NULLFILEOFF
)
1589 *last_block
= prev
.br_startoff
+ prev
.br_blockcount
;
1592 * Otherwise *last_block is already the right answer.
1598 xfs_bmap_last_extent(
1599 struct xfs_trans
*tp
,
1600 struct xfs_inode
*ip
,
1602 struct xfs_bmbt_irec
*rec
,
1605 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1609 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
1610 error
= xfs_iread_extents(tp
, ip
, whichfork
);
1615 nextents
= ifp
->if_bytes
/ sizeof(xfs_bmbt_rec_t
);
1616 if (nextents
== 0) {
1621 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, nextents
- 1), rec
);
1627 * Check the last inode extent to determine whether this allocation will result
1628 * in blocks being allocated at the end of the file. When we allocate new data
1629 * blocks at the end of the file which do not start at the previous data block,
1630 * we will try to align the new blocks at stripe unit boundaries.
1632 * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be
1633 * at, or past the EOF.
1637 struct xfs_bmalloca
*bma
,
1640 struct xfs_bmbt_irec rec
;
1645 error
= xfs_bmap_last_extent(NULL
, bma
->ip
, whichfork
, &rec
,
1656 * Check if we are allocation or past the last extent, or at least into
1657 * the last delayed allocated extent.
1659 bma
->aeof
= bma
->offset
>= rec
.br_startoff
+ rec
.br_blockcount
||
1660 (bma
->offset
>= rec
.br_startoff
&&
1661 isnullstartblock(rec
.br_startblock
));
1666 * Returns the file-relative block number of the first block past eof in
1667 * the file. This is not based on i_size, it is based on the extent records.
1668 * Returns 0 for local files, as they do not have extent records.
1671 xfs_bmap_last_offset(
1672 struct xfs_inode
*ip
,
1673 xfs_fileoff_t
*last_block
,
1676 struct xfs_bmbt_irec rec
;
1682 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
)
1685 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
&&
1686 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
1689 error
= xfs_bmap_last_extent(NULL
, ip
, whichfork
, &rec
, &is_empty
);
1690 if (error
|| is_empty
)
1693 *last_block
= rec
.br_startoff
+ rec
.br_blockcount
;
1698 * Returns whether the selected fork of the inode has exactly one
1699 * block or not. For the data fork we check this matches di_size,
1700 * implying the file's range is 0..bsize-1.
1702 int /* 1=>1 block, 0=>otherwise */
1704 xfs_inode_t
*ip
, /* incore inode */
1705 int whichfork
) /* data or attr fork */
1707 xfs_bmbt_rec_host_t
*ep
; /* ptr to fork's extent */
1708 xfs_ifork_t
*ifp
; /* inode fork pointer */
1709 int rval
; /* return value */
1710 xfs_bmbt_irec_t s
; /* internal version of extent */
1713 if (whichfork
== XFS_DATA_FORK
)
1714 return XFS_ISIZE(ip
) == ip
->i_mount
->m_sb
.sb_blocksize
;
1716 if (XFS_IFORK_NEXTENTS(ip
, whichfork
) != 1)
1718 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
1720 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1721 ASSERT(ifp
->if_flags
& XFS_IFEXTENTS
);
1722 ep
= xfs_iext_get_ext(ifp
, 0);
1723 xfs_bmbt_get_all(ep
, &s
);
1724 rval
= s
.br_startoff
== 0 && s
.br_blockcount
== 1;
1725 if (rval
&& whichfork
== XFS_DATA_FORK
)
1726 ASSERT(XFS_ISIZE(ip
) == ip
->i_mount
->m_sb
.sb_blocksize
);
1731 * Extent tree manipulation functions used during allocation.
1735 * Convert a delayed allocation to a real allocation.
1737 STATIC
int /* error */
1738 xfs_bmap_add_extent_delay_real(
1739 struct xfs_bmalloca
*bma
)
1741 struct xfs_bmbt_irec
*new = &bma
->got
;
1742 int diff
; /* temp value */
1743 xfs_bmbt_rec_host_t
*ep
; /* extent entry for idx */
1744 int error
; /* error return value */
1745 int i
; /* temp state */
1746 xfs_ifork_t
*ifp
; /* inode fork pointer */
1747 xfs_fileoff_t new_endoff
; /* end offset of new entry */
1748 xfs_bmbt_irec_t r
[3]; /* neighbor extent entries */
1749 /* left is 0, right is 1, prev is 2 */
1750 int rval
=0; /* return value (logging flags) */
1751 int state
= 0;/* state bits, accessed thru macros */
1752 xfs_filblks_t da_new
; /* new count del alloc blocks used */
1753 xfs_filblks_t da_old
; /* old count del alloc blocks used */
1754 xfs_filblks_t temp
=0; /* value for da_new calculations */
1755 xfs_filblks_t temp2
=0;/* value for da_new calculations */
1756 int tmp_rval
; /* partial logging flags */
1758 ifp
= XFS_IFORK_PTR(bma
->ip
, XFS_DATA_FORK
);
1760 ASSERT(bma
->idx
>= 0);
1761 ASSERT(bma
->idx
<= ifp
->if_bytes
/ sizeof(struct xfs_bmbt_rec
));
1762 ASSERT(!isnullstartblock(new->br_startblock
));
1764 (bma
->cur
->bc_private
.b
.flags
& XFS_BTCUR_BPRV_WASDEL
));
1766 XFS_STATS_INC(xs_add_exlist
);
1773 * Set up a bunch of variables to make the tests simpler.
1775 ep
= xfs_iext_get_ext(ifp
, bma
->idx
);
1776 xfs_bmbt_get_all(ep
, &PREV
);
1777 new_endoff
= new->br_startoff
+ new->br_blockcount
;
1778 ASSERT(PREV
.br_startoff
<= new->br_startoff
);
1779 ASSERT(PREV
.br_startoff
+ PREV
.br_blockcount
>= new_endoff
);
1781 da_old
= startblockval(PREV
.br_startblock
);
1785 * Set flags determining what part of the previous delayed allocation
1786 * extent is being replaced by a real allocation.
1788 if (PREV
.br_startoff
== new->br_startoff
)
1789 state
|= BMAP_LEFT_FILLING
;
1790 if (PREV
.br_startoff
+ PREV
.br_blockcount
== new_endoff
)
1791 state
|= BMAP_RIGHT_FILLING
;
1794 * Check and set flags if this segment has a left neighbor.
1795 * Don't set contiguous if the combined extent would be too large.
1798 state
|= BMAP_LEFT_VALID
;
1799 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
- 1), &LEFT
);
1801 if (isnullstartblock(LEFT
.br_startblock
))
1802 state
|= BMAP_LEFT_DELAY
;
1805 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
1806 LEFT
.br_startoff
+ LEFT
.br_blockcount
== new->br_startoff
&&
1807 LEFT
.br_startblock
+ LEFT
.br_blockcount
== new->br_startblock
&&
1808 LEFT
.br_state
== new->br_state
&&
1809 LEFT
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
1810 state
|= BMAP_LEFT_CONTIG
;
1813 * Check and set flags if this segment has a right neighbor.
1814 * Don't set contiguous if the combined extent would be too large.
1815 * Also check for all-three-contiguous being too large.
1817 if (bma
->idx
< bma
->ip
->i_df
.if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
) - 1) {
1818 state
|= BMAP_RIGHT_VALID
;
1819 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
+ 1), &RIGHT
);
1821 if (isnullstartblock(RIGHT
.br_startblock
))
1822 state
|= BMAP_RIGHT_DELAY
;
1825 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
1826 new_endoff
== RIGHT
.br_startoff
&&
1827 new->br_startblock
+ new->br_blockcount
== RIGHT
.br_startblock
&&
1828 new->br_state
== RIGHT
.br_state
&&
1829 new->br_blockcount
+ RIGHT
.br_blockcount
<= MAXEXTLEN
&&
1830 ((state
& (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
1831 BMAP_RIGHT_FILLING
)) !=
1832 (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
1833 BMAP_RIGHT_FILLING
) ||
1834 LEFT
.br_blockcount
+ new->br_blockcount
+ RIGHT
.br_blockcount
1836 state
|= BMAP_RIGHT_CONTIG
;
1840 * Switch out based on the FILLING and CONTIG state bits.
1842 switch (state
& (BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
1843 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
)) {
1844 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
1845 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
1847 * Filling in all of a previously delayed allocation extent.
1848 * The left and right neighbors are both contiguous with new.
1851 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1852 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, bma
->idx
),
1853 LEFT
.br_blockcount
+ PREV
.br_blockcount
+
1854 RIGHT
.br_blockcount
);
1855 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1857 xfs_iext_remove(bma
->ip
, bma
->idx
+ 1, 2, state
);
1858 bma
->ip
->i_d
.di_nextents
--;
1859 if (bma
->cur
== NULL
)
1860 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1862 rval
= XFS_ILOG_CORE
;
1863 error
= xfs_bmbt_lookup_eq(bma
->cur
, RIGHT
.br_startoff
,
1864 RIGHT
.br_startblock
,
1865 RIGHT
.br_blockcount
, &i
);
1868 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
1869 error
= xfs_btree_delete(bma
->cur
, &i
);
1872 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
1873 error
= xfs_btree_decrement(bma
->cur
, 0, &i
);
1876 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
1877 error
= xfs_bmbt_update(bma
->cur
, LEFT
.br_startoff
,
1879 LEFT
.br_blockcount
+
1880 PREV
.br_blockcount
+
1881 RIGHT
.br_blockcount
, LEFT
.br_state
);
1887 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
1889 * Filling in all of a previously delayed allocation extent.
1890 * The left neighbor is contiguous, the right is not.
1894 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1895 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, bma
->idx
),
1896 LEFT
.br_blockcount
+ PREV
.br_blockcount
);
1897 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1899 xfs_iext_remove(bma
->ip
, bma
->idx
+ 1, 1, state
);
1900 if (bma
->cur
== NULL
)
1901 rval
= XFS_ILOG_DEXT
;
1904 error
= xfs_bmbt_lookup_eq(bma
->cur
, LEFT
.br_startoff
,
1905 LEFT
.br_startblock
, LEFT
.br_blockcount
,
1909 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
1910 error
= xfs_bmbt_update(bma
->cur
, LEFT
.br_startoff
,
1912 LEFT
.br_blockcount
+
1913 PREV
.br_blockcount
, LEFT
.br_state
);
1919 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
1921 * Filling in all of a previously delayed allocation extent.
1922 * The right neighbor is contiguous, the left is not.
1924 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1925 xfs_bmbt_set_startblock(ep
, new->br_startblock
);
1926 xfs_bmbt_set_blockcount(ep
,
1927 PREV
.br_blockcount
+ RIGHT
.br_blockcount
);
1928 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1930 xfs_iext_remove(bma
->ip
, bma
->idx
+ 1, 1, state
);
1931 if (bma
->cur
== NULL
)
1932 rval
= XFS_ILOG_DEXT
;
1935 error
= xfs_bmbt_lookup_eq(bma
->cur
, RIGHT
.br_startoff
,
1936 RIGHT
.br_startblock
,
1937 RIGHT
.br_blockcount
, &i
);
1940 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
1941 error
= xfs_bmbt_update(bma
->cur
, PREV
.br_startoff
,
1943 PREV
.br_blockcount
+
1944 RIGHT
.br_blockcount
, PREV
.br_state
);
1950 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
1952 * Filling in all of a previously delayed allocation extent.
1953 * Neither the left nor right neighbors are contiguous with
1956 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1957 xfs_bmbt_set_startblock(ep
, new->br_startblock
);
1958 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1960 bma
->ip
->i_d
.di_nextents
++;
1961 if (bma
->cur
== NULL
)
1962 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1964 rval
= XFS_ILOG_CORE
;
1965 error
= xfs_bmbt_lookup_eq(bma
->cur
, new->br_startoff
,
1966 new->br_startblock
, new->br_blockcount
,
1970 XFS_WANT_CORRUPTED_GOTO(i
== 0, done
);
1971 bma
->cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
1972 error
= xfs_btree_insert(bma
->cur
, &i
);
1975 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
1979 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
:
1981 * Filling in the first part of a previous delayed allocation.
1982 * The left neighbor is contiguous.
1984 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
- 1, state
, _THIS_IP_
);
1985 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, bma
->idx
- 1),
1986 LEFT
.br_blockcount
+ new->br_blockcount
);
1987 xfs_bmbt_set_startoff(ep
,
1988 PREV
.br_startoff
+ new->br_blockcount
);
1989 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
- 1, state
, _THIS_IP_
);
1991 temp
= PREV
.br_blockcount
- new->br_blockcount
;
1992 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1993 xfs_bmbt_set_blockcount(ep
, temp
);
1994 if (bma
->cur
== NULL
)
1995 rval
= XFS_ILOG_DEXT
;
1998 error
= xfs_bmbt_lookup_eq(bma
->cur
, LEFT
.br_startoff
,
1999 LEFT
.br_startblock
, LEFT
.br_blockcount
,
2003 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2004 error
= xfs_bmbt_update(bma
->cur
, LEFT
.br_startoff
,
2006 LEFT
.br_blockcount
+
2012 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
2013 startblockval(PREV
.br_startblock
));
2014 xfs_bmbt_set_startblock(ep
, nullstartblock(da_new
));
2015 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2020 case BMAP_LEFT_FILLING
:
2022 * Filling in the first part of a previous delayed allocation.
2023 * The left neighbor is not contiguous.
2025 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2026 xfs_bmbt_set_startoff(ep
, new_endoff
);
2027 temp
= PREV
.br_blockcount
- new->br_blockcount
;
2028 xfs_bmbt_set_blockcount(ep
, temp
);
2029 xfs_iext_insert(bma
->ip
, bma
->idx
, 1, new, state
);
2030 bma
->ip
->i_d
.di_nextents
++;
2031 if (bma
->cur
== NULL
)
2032 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2034 rval
= XFS_ILOG_CORE
;
2035 error
= xfs_bmbt_lookup_eq(bma
->cur
, new->br_startoff
,
2036 new->br_startblock
, new->br_blockcount
,
2040 XFS_WANT_CORRUPTED_GOTO(i
== 0, done
);
2041 bma
->cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
2042 error
= xfs_btree_insert(bma
->cur
, &i
);
2045 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2048 if (xfs_bmap_needs_btree(bma
->ip
, XFS_DATA_FORK
)) {
2049 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
2050 bma
->firstblock
, bma
->flist
,
2051 &bma
->cur
, 1, &tmp_rval
, XFS_DATA_FORK
);
2056 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
2057 startblockval(PREV
.br_startblock
) -
2058 (bma
->cur
? bma
->cur
->bc_private
.b
.allocated
: 0));
2059 ep
= xfs_iext_get_ext(ifp
, bma
->idx
+ 1);
2060 xfs_bmbt_set_startblock(ep
, nullstartblock(da_new
));
2061 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
+ 1, state
, _THIS_IP_
);
2064 case BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2066 * Filling in the last part of a previous delayed allocation.
2067 * The right neighbor is contiguous with the new allocation.
2069 temp
= PREV
.br_blockcount
- new->br_blockcount
;
2070 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
+ 1, state
, _THIS_IP_
);
2071 xfs_bmbt_set_blockcount(ep
, temp
);
2072 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp
, bma
->idx
+ 1),
2073 new->br_startoff
, new->br_startblock
,
2074 new->br_blockcount
+ RIGHT
.br_blockcount
,
2076 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
+ 1, state
, _THIS_IP_
);
2077 if (bma
->cur
== NULL
)
2078 rval
= XFS_ILOG_DEXT
;
2081 error
= xfs_bmbt_lookup_eq(bma
->cur
, RIGHT
.br_startoff
,
2082 RIGHT
.br_startblock
,
2083 RIGHT
.br_blockcount
, &i
);
2086 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2087 error
= xfs_bmbt_update(bma
->cur
, new->br_startoff
,
2089 new->br_blockcount
+
2090 RIGHT
.br_blockcount
,
2096 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
2097 startblockval(PREV
.br_startblock
));
2098 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2099 xfs_bmbt_set_startblock(ep
, nullstartblock(da_new
));
2100 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2105 case BMAP_RIGHT_FILLING
:
2107 * Filling in the last part of a previous delayed allocation.
2108 * The right neighbor is not contiguous.
2110 temp
= PREV
.br_blockcount
- new->br_blockcount
;
2111 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2112 xfs_bmbt_set_blockcount(ep
, temp
);
2113 xfs_iext_insert(bma
->ip
, bma
->idx
+ 1, 1, new, state
);
2114 bma
->ip
->i_d
.di_nextents
++;
2115 if (bma
->cur
== NULL
)
2116 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2118 rval
= XFS_ILOG_CORE
;
2119 error
= xfs_bmbt_lookup_eq(bma
->cur
, new->br_startoff
,
2120 new->br_startblock
, new->br_blockcount
,
2124 XFS_WANT_CORRUPTED_GOTO(i
== 0, done
);
2125 bma
->cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
2126 error
= xfs_btree_insert(bma
->cur
, &i
);
2129 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2132 if (xfs_bmap_needs_btree(bma
->ip
, XFS_DATA_FORK
)) {
2133 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
2134 bma
->firstblock
, bma
->flist
, &bma
->cur
, 1,
2135 &tmp_rval
, XFS_DATA_FORK
);
2140 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
2141 startblockval(PREV
.br_startblock
) -
2142 (bma
->cur
? bma
->cur
->bc_private
.b
.allocated
: 0));
2143 ep
= xfs_iext_get_ext(ifp
, bma
->idx
);
2144 xfs_bmbt_set_startblock(ep
, nullstartblock(da_new
));
2145 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2152 * Filling in the middle part of a previous delayed allocation.
2153 * Contiguity is impossible here.
2154 * This case is avoided almost all the time.
2156 * We start with a delayed allocation:
2158 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
2161 * and we are allocating:
2162 * +rrrrrrrrrrrrrrrrr+
2165 * and we set it up for insertion as:
2166 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
2168 * PREV @ idx LEFT RIGHT
2169 * inserted at idx + 1
2171 temp
= new->br_startoff
- PREV
.br_startoff
;
2172 temp2
= PREV
.br_startoff
+ PREV
.br_blockcount
- new_endoff
;
2173 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, 0, _THIS_IP_
);
2174 xfs_bmbt_set_blockcount(ep
, temp
); /* truncate PREV */
2176 RIGHT
.br_state
= PREV
.br_state
;
2177 RIGHT
.br_startblock
= nullstartblock(
2178 (int)xfs_bmap_worst_indlen(bma
->ip
, temp2
));
2179 RIGHT
.br_startoff
= new_endoff
;
2180 RIGHT
.br_blockcount
= temp2
;
2181 /* insert LEFT (r[0]) and RIGHT (r[1]) at the same time */
2182 xfs_iext_insert(bma
->ip
, bma
->idx
+ 1, 2, &LEFT
, state
);
2183 bma
->ip
->i_d
.di_nextents
++;
2184 if (bma
->cur
== NULL
)
2185 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2187 rval
= XFS_ILOG_CORE
;
2188 error
= xfs_bmbt_lookup_eq(bma
->cur
, new->br_startoff
,
2189 new->br_startblock
, new->br_blockcount
,
2193 XFS_WANT_CORRUPTED_GOTO(i
== 0, done
);
2194 bma
->cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
2195 error
= xfs_btree_insert(bma
->cur
, &i
);
2198 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2201 if (xfs_bmap_needs_btree(bma
->ip
, XFS_DATA_FORK
)) {
2202 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
2203 bma
->firstblock
, bma
->flist
, &bma
->cur
,
2204 1, &tmp_rval
, XFS_DATA_FORK
);
2209 temp
= xfs_bmap_worst_indlen(bma
->ip
, temp
);
2210 temp2
= xfs_bmap_worst_indlen(bma
->ip
, temp2
);
2211 diff
= (int)(temp
+ temp2
- startblockval(PREV
.br_startblock
) -
2212 (bma
->cur
? bma
->cur
->bc_private
.b
.allocated
: 0));
2214 error
= xfs_icsb_modify_counters(bma
->ip
->i_mount
,
2216 -((int64_t)diff
), 0);
2222 ep
= xfs_iext_get_ext(ifp
, bma
->idx
);
2223 xfs_bmbt_set_startblock(ep
, nullstartblock((int)temp
));
2224 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2225 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
+ 2, state
, _THIS_IP_
);
2226 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp
, bma
->idx
+ 2),
2227 nullstartblock((int)temp2
));
2228 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
+ 2, state
, _THIS_IP_
);
2231 da_new
= temp
+ temp2
;
2234 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2235 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2236 case BMAP_LEFT_FILLING
| BMAP_RIGHT_CONTIG
:
2237 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
2238 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2239 case BMAP_LEFT_CONTIG
:
2240 case BMAP_RIGHT_CONTIG
:
2242 * These cases are all impossible.
2247 /* convert to a btree if necessary */
2248 if (xfs_bmap_needs_btree(bma
->ip
, XFS_DATA_FORK
)) {
2249 int tmp_logflags
; /* partial log flag return val */
2251 ASSERT(bma
->cur
== NULL
);
2252 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
2253 bma
->firstblock
, bma
->flist
, &bma
->cur
,
2254 da_old
> 0, &tmp_logflags
, XFS_DATA_FORK
);
2255 bma
->logflags
|= tmp_logflags
;
2260 /* adjust for changes in reserved delayed indirect blocks */
2261 if (da_old
|| da_new
) {
2264 temp
+= bma
->cur
->bc_private
.b
.allocated
;
2265 ASSERT(temp
<= da_old
);
2267 xfs_icsb_modify_counters(bma
->ip
->i_mount
,
2269 (int64_t)(da_old
- temp
), 0);
2272 /* clear out the allocated field, done with it now in any case. */
2274 bma
->cur
->bc_private
.b
.allocated
= 0;
2276 xfs_bmap_check_leaf_extents(bma
->cur
, bma
->ip
, XFS_DATA_FORK
);
2278 bma
->logflags
|= rval
;
2286 * Convert an unwritten allocation to a real allocation or vice versa.
2288 STATIC
int /* error */
2289 xfs_bmap_add_extent_unwritten_real(
2290 struct xfs_trans
*tp
,
2291 xfs_inode_t
*ip
, /* incore inode pointer */
2292 xfs_extnum_t
*idx
, /* extent number to update/insert */
2293 xfs_btree_cur_t
**curp
, /* if *curp is null, not a btree */
2294 xfs_bmbt_irec_t
*new, /* new data to add to file extents */
2295 xfs_fsblock_t
*first
, /* pointer to firstblock variable */
2296 xfs_bmap_free_t
*flist
, /* list of extents to be freed */
2297 int *logflagsp
) /* inode logging flags */
2299 xfs_btree_cur_t
*cur
; /* btree cursor */
2300 xfs_bmbt_rec_host_t
*ep
; /* extent entry for idx */
2301 int error
; /* error return value */
2302 int i
; /* temp state */
2303 xfs_ifork_t
*ifp
; /* inode fork pointer */
2304 xfs_fileoff_t new_endoff
; /* end offset of new entry */
2305 xfs_exntst_t newext
; /* new extent state */
2306 xfs_exntst_t oldext
; /* old extent state */
2307 xfs_bmbt_irec_t r
[3]; /* neighbor extent entries */
2308 /* left is 0, right is 1, prev is 2 */
2309 int rval
=0; /* return value (logging flags) */
2310 int state
= 0;/* state bits, accessed thru macros */
2315 ifp
= XFS_IFORK_PTR(ip
, XFS_DATA_FORK
);
2318 ASSERT(*idx
<= ifp
->if_bytes
/ sizeof(struct xfs_bmbt_rec
));
2319 ASSERT(!isnullstartblock(new->br_startblock
));
2321 XFS_STATS_INC(xs_add_exlist
);
2328 * Set up a bunch of variables to make the tests simpler.
2331 ep
= xfs_iext_get_ext(ifp
, *idx
);
2332 xfs_bmbt_get_all(ep
, &PREV
);
2333 newext
= new->br_state
;
2334 oldext
= (newext
== XFS_EXT_UNWRITTEN
) ?
2335 XFS_EXT_NORM
: XFS_EXT_UNWRITTEN
;
2336 ASSERT(PREV
.br_state
== oldext
);
2337 new_endoff
= new->br_startoff
+ new->br_blockcount
;
2338 ASSERT(PREV
.br_startoff
<= new->br_startoff
);
2339 ASSERT(PREV
.br_startoff
+ PREV
.br_blockcount
>= new_endoff
);
2342 * Set flags determining what part of the previous oldext allocation
2343 * extent is being replaced by a newext allocation.
2345 if (PREV
.br_startoff
== new->br_startoff
)
2346 state
|= BMAP_LEFT_FILLING
;
2347 if (PREV
.br_startoff
+ PREV
.br_blockcount
== new_endoff
)
2348 state
|= BMAP_RIGHT_FILLING
;
2351 * Check and set flags if this segment has a left neighbor.
2352 * Don't set contiguous if the combined extent would be too large.
2355 state
|= BMAP_LEFT_VALID
;
2356 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, *idx
- 1), &LEFT
);
2358 if (isnullstartblock(LEFT
.br_startblock
))
2359 state
|= BMAP_LEFT_DELAY
;
2362 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
2363 LEFT
.br_startoff
+ LEFT
.br_blockcount
== new->br_startoff
&&
2364 LEFT
.br_startblock
+ LEFT
.br_blockcount
== new->br_startblock
&&
2365 LEFT
.br_state
== newext
&&
2366 LEFT
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
2367 state
|= BMAP_LEFT_CONTIG
;
2370 * Check and set flags if this segment has a right neighbor.
2371 * Don't set contiguous if the combined extent would be too large.
2372 * Also check for all-three-contiguous being too large.
2374 if (*idx
< ip
->i_df
.if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
) - 1) {
2375 state
|= BMAP_RIGHT_VALID
;
2376 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, *idx
+ 1), &RIGHT
);
2377 if (isnullstartblock(RIGHT
.br_startblock
))
2378 state
|= BMAP_RIGHT_DELAY
;
2381 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
2382 new_endoff
== RIGHT
.br_startoff
&&
2383 new->br_startblock
+ new->br_blockcount
== RIGHT
.br_startblock
&&
2384 newext
== RIGHT
.br_state
&&
2385 new->br_blockcount
+ RIGHT
.br_blockcount
<= MAXEXTLEN
&&
2386 ((state
& (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
2387 BMAP_RIGHT_FILLING
)) !=
2388 (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
2389 BMAP_RIGHT_FILLING
) ||
2390 LEFT
.br_blockcount
+ new->br_blockcount
+ RIGHT
.br_blockcount
2392 state
|= BMAP_RIGHT_CONTIG
;
2395 * Switch out based on the FILLING and CONTIG state bits.
2397 switch (state
& (BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
2398 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
)) {
2399 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
2400 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2402 * Setting all of a previous oldext extent to newext.
2403 * The left and right neighbors are both contiguous with new.
2407 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2408 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
),
2409 LEFT
.br_blockcount
+ PREV
.br_blockcount
+
2410 RIGHT
.br_blockcount
);
2411 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2413 xfs_iext_remove(ip
, *idx
+ 1, 2, state
);
2414 ip
->i_d
.di_nextents
-= 2;
2416 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2418 rval
= XFS_ILOG_CORE
;
2419 if ((error
= xfs_bmbt_lookup_eq(cur
, RIGHT
.br_startoff
,
2420 RIGHT
.br_startblock
,
2421 RIGHT
.br_blockcount
, &i
)))
2423 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2424 if ((error
= xfs_btree_delete(cur
, &i
)))
2426 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2427 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2429 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2430 if ((error
= xfs_btree_delete(cur
, &i
)))
2432 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2433 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2435 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2436 if ((error
= xfs_bmbt_update(cur
, LEFT
.br_startoff
,
2438 LEFT
.br_blockcount
+ PREV
.br_blockcount
+
2439 RIGHT
.br_blockcount
, LEFT
.br_state
)))
2444 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
2446 * Setting all of a previous oldext extent to newext.
2447 * The left neighbor is contiguous, the right is not.
2451 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2452 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
),
2453 LEFT
.br_blockcount
+ PREV
.br_blockcount
);
2454 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2456 xfs_iext_remove(ip
, *idx
+ 1, 1, state
);
2457 ip
->i_d
.di_nextents
--;
2459 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2461 rval
= XFS_ILOG_CORE
;
2462 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2463 PREV
.br_startblock
, PREV
.br_blockcount
,
2466 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2467 if ((error
= xfs_btree_delete(cur
, &i
)))
2469 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2470 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2472 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2473 if ((error
= xfs_bmbt_update(cur
, LEFT
.br_startoff
,
2475 LEFT
.br_blockcount
+ PREV
.br_blockcount
,
2481 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2483 * Setting all of a previous oldext extent to newext.
2484 * The right neighbor is contiguous, the left is not.
2486 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2487 xfs_bmbt_set_blockcount(ep
,
2488 PREV
.br_blockcount
+ RIGHT
.br_blockcount
);
2489 xfs_bmbt_set_state(ep
, newext
);
2490 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2491 xfs_iext_remove(ip
, *idx
+ 1, 1, state
);
2492 ip
->i_d
.di_nextents
--;
2494 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2496 rval
= XFS_ILOG_CORE
;
2497 if ((error
= xfs_bmbt_lookup_eq(cur
, RIGHT
.br_startoff
,
2498 RIGHT
.br_startblock
,
2499 RIGHT
.br_blockcount
, &i
)))
2501 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2502 if ((error
= xfs_btree_delete(cur
, &i
)))
2504 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2505 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2507 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2508 if ((error
= xfs_bmbt_update(cur
, new->br_startoff
,
2510 new->br_blockcount
+ RIGHT
.br_blockcount
,
2516 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
2518 * Setting all of a previous oldext extent to newext.
2519 * Neither the left nor right neighbors are contiguous with
2522 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2523 xfs_bmbt_set_state(ep
, newext
);
2524 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2527 rval
= XFS_ILOG_DEXT
;
2530 if ((error
= xfs_bmbt_lookup_eq(cur
, new->br_startoff
,
2531 new->br_startblock
, new->br_blockcount
,
2534 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2535 if ((error
= xfs_bmbt_update(cur
, new->br_startoff
,
2536 new->br_startblock
, new->br_blockcount
,
2542 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
:
2544 * Setting the first part of a previous oldext extent to newext.
2545 * The left neighbor is contiguous.
2547 trace_xfs_bmap_pre_update(ip
, *idx
- 1, state
, _THIS_IP_
);
2548 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
- 1),
2549 LEFT
.br_blockcount
+ new->br_blockcount
);
2550 xfs_bmbt_set_startoff(ep
,
2551 PREV
.br_startoff
+ new->br_blockcount
);
2552 trace_xfs_bmap_post_update(ip
, *idx
- 1, state
, _THIS_IP_
);
2554 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2555 xfs_bmbt_set_startblock(ep
,
2556 new->br_startblock
+ new->br_blockcount
);
2557 xfs_bmbt_set_blockcount(ep
,
2558 PREV
.br_blockcount
- new->br_blockcount
);
2559 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2564 rval
= XFS_ILOG_DEXT
;
2567 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2568 PREV
.br_startblock
, PREV
.br_blockcount
,
2571 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2572 if ((error
= xfs_bmbt_update(cur
,
2573 PREV
.br_startoff
+ new->br_blockcount
,
2574 PREV
.br_startblock
+ new->br_blockcount
,
2575 PREV
.br_blockcount
- new->br_blockcount
,
2578 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2580 error
= xfs_bmbt_update(cur
, LEFT
.br_startoff
,
2582 LEFT
.br_blockcount
+ new->br_blockcount
,
2589 case BMAP_LEFT_FILLING
:
2591 * Setting the first part of a previous oldext extent to newext.
2592 * The left neighbor is not contiguous.
2594 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2595 ASSERT(ep
&& xfs_bmbt_get_state(ep
) == oldext
);
2596 xfs_bmbt_set_startoff(ep
, new_endoff
);
2597 xfs_bmbt_set_blockcount(ep
,
2598 PREV
.br_blockcount
- new->br_blockcount
);
2599 xfs_bmbt_set_startblock(ep
,
2600 new->br_startblock
+ new->br_blockcount
);
2601 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2603 xfs_iext_insert(ip
, *idx
, 1, new, state
);
2604 ip
->i_d
.di_nextents
++;
2606 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2608 rval
= XFS_ILOG_CORE
;
2609 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2610 PREV
.br_startblock
, PREV
.br_blockcount
,
2613 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2614 if ((error
= xfs_bmbt_update(cur
,
2615 PREV
.br_startoff
+ new->br_blockcount
,
2616 PREV
.br_startblock
+ new->br_blockcount
,
2617 PREV
.br_blockcount
- new->br_blockcount
,
2620 cur
->bc_rec
.b
= *new;
2621 if ((error
= xfs_btree_insert(cur
, &i
)))
2623 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2627 case BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2629 * Setting the last part of a previous oldext extent to newext.
2630 * The right neighbor is contiguous with the new allocation.
2632 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2633 xfs_bmbt_set_blockcount(ep
,
2634 PREV
.br_blockcount
- new->br_blockcount
);
2635 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2639 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2640 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp
, *idx
),
2641 new->br_startoff
, new->br_startblock
,
2642 new->br_blockcount
+ RIGHT
.br_blockcount
, newext
);
2643 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2646 rval
= XFS_ILOG_DEXT
;
2649 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2651 PREV
.br_blockcount
, &i
)))
2653 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2654 if ((error
= xfs_bmbt_update(cur
, PREV
.br_startoff
,
2656 PREV
.br_blockcount
- new->br_blockcount
,
2659 if ((error
= xfs_btree_increment(cur
, 0, &i
)))
2661 if ((error
= xfs_bmbt_update(cur
, new->br_startoff
,
2663 new->br_blockcount
+ RIGHT
.br_blockcount
,
2669 case BMAP_RIGHT_FILLING
:
2671 * Setting the last part of a previous oldext extent to newext.
2672 * The right neighbor is not contiguous.
2674 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2675 xfs_bmbt_set_blockcount(ep
,
2676 PREV
.br_blockcount
- new->br_blockcount
);
2677 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2680 xfs_iext_insert(ip
, *idx
, 1, new, state
);
2682 ip
->i_d
.di_nextents
++;
2684 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2686 rval
= XFS_ILOG_CORE
;
2687 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2688 PREV
.br_startblock
, PREV
.br_blockcount
,
2691 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2692 if ((error
= xfs_bmbt_update(cur
, PREV
.br_startoff
,
2694 PREV
.br_blockcount
- new->br_blockcount
,
2697 if ((error
= xfs_bmbt_lookup_eq(cur
, new->br_startoff
,
2698 new->br_startblock
, new->br_blockcount
,
2701 XFS_WANT_CORRUPTED_GOTO(i
== 0, done
);
2702 cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
2703 if ((error
= xfs_btree_insert(cur
, &i
)))
2705 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2711 * Setting the middle part of a previous oldext extent to
2712 * newext. Contiguity is impossible here.
2713 * One extent becomes three extents.
2715 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2716 xfs_bmbt_set_blockcount(ep
,
2717 new->br_startoff
- PREV
.br_startoff
);
2718 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2721 r
[1].br_startoff
= new_endoff
;
2722 r
[1].br_blockcount
=
2723 PREV
.br_startoff
+ PREV
.br_blockcount
- new_endoff
;
2724 r
[1].br_startblock
= new->br_startblock
+ new->br_blockcount
;
2725 r
[1].br_state
= oldext
;
2728 xfs_iext_insert(ip
, *idx
, 2, &r
[0], state
);
2730 ip
->i_d
.di_nextents
+= 2;
2732 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2734 rval
= XFS_ILOG_CORE
;
2735 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2736 PREV
.br_startblock
, PREV
.br_blockcount
,
2739 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2740 /* new right extent - oldext */
2741 if ((error
= xfs_bmbt_update(cur
, r
[1].br_startoff
,
2742 r
[1].br_startblock
, r
[1].br_blockcount
,
2745 /* new left extent - oldext */
2746 cur
->bc_rec
.b
= PREV
;
2747 cur
->bc_rec
.b
.br_blockcount
=
2748 new->br_startoff
- PREV
.br_startoff
;
2749 if ((error
= xfs_btree_insert(cur
, &i
)))
2751 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2753 * Reset the cursor to the position of the new extent
2754 * we are about to insert as we can't trust it after
2755 * the previous insert.
2757 if ((error
= xfs_bmbt_lookup_eq(cur
, new->br_startoff
,
2758 new->br_startblock
, new->br_blockcount
,
2761 XFS_WANT_CORRUPTED_GOTO(i
== 0, done
);
2762 /* new middle extent - newext */
2763 cur
->bc_rec
.b
.br_state
= new->br_state
;
2764 if ((error
= xfs_btree_insert(cur
, &i
)))
2766 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2770 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2771 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2772 case BMAP_LEFT_FILLING
| BMAP_RIGHT_CONTIG
:
2773 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
2774 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2775 case BMAP_LEFT_CONTIG
:
2776 case BMAP_RIGHT_CONTIG
:
2778 * These cases are all impossible.
2783 /* convert to a btree if necessary */
2784 if (xfs_bmap_needs_btree(ip
, XFS_DATA_FORK
)) {
2785 int tmp_logflags
; /* partial log flag return val */
2787 ASSERT(cur
== NULL
);
2788 error
= xfs_bmap_extents_to_btree(tp
, ip
, first
, flist
, &cur
,
2789 0, &tmp_logflags
, XFS_DATA_FORK
);
2790 *logflagsp
|= tmp_logflags
;
2795 /* clear out the allocated field, done with it now in any case. */
2797 cur
->bc_private
.b
.allocated
= 0;
2801 xfs_bmap_check_leaf_extents(*curp
, ip
, XFS_DATA_FORK
);
2811 * Convert a hole to a delayed allocation.
2814 xfs_bmap_add_extent_hole_delay(
2815 xfs_inode_t
*ip
, /* incore inode pointer */
2816 xfs_extnum_t
*idx
, /* extent number to update/insert */
2817 xfs_bmbt_irec_t
*new) /* new data to add to file extents */
2819 xfs_ifork_t
*ifp
; /* inode fork pointer */
2820 xfs_bmbt_irec_t left
; /* left neighbor extent entry */
2821 xfs_filblks_t newlen
=0; /* new indirect size */
2822 xfs_filblks_t oldlen
=0; /* old indirect size */
2823 xfs_bmbt_irec_t right
; /* right neighbor extent entry */
2824 int state
; /* state bits, accessed thru macros */
2825 xfs_filblks_t temp
=0; /* temp for indirect calculations */
2827 ifp
= XFS_IFORK_PTR(ip
, XFS_DATA_FORK
);
2829 ASSERT(isnullstartblock(new->br_startblock
));
2832 * Check and set flags if this segment has a left neighbor
2835 state
|= BMAP_LEFT_VALID
;
2836 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, *idx
- 1), &left
);
2838 if (isnullstartblock(left
.br_startblock
))
2839 state
|= BMAP_LEFT_DELAY
;
2843 * Check and set flags if the current (right) segment exists.
2844 * If it doesn't exist, we're converting the hole at end-of-file.
2846 if (*idx
< ip
->i_df
.if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
)) {
2847 state
|= BMAP_RIGHT_VALID
;
2848 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, *idx
), &right
);
2850 if (isnullstartblock(right
.br_startblock
))
2851 state
|= BMAP_RIGHT_DELAY
;
2855 * Set contiguity flags on the left and right neighbors.
2856 * Don't let extents get too large, even if the pieces are contiguous.
2858 if ((state
& BMAP_LEFT_VALID
) && (state
& BMAP_LEFT_DELAY
) &&
2859 left
.br_startoff
+ left
.br_blockcount
== new->br_startoff
&&
2860 left
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
2861 state
|= BMAP_LEFT_CONTIG
;
2863 if ((state
& BMAP_RIGHT_VALID
) && (state
& BMAP_RIGHT_DELAY
) &&
2864 new->br_startoff
+ new->br_blockcount
== right
.br_startoff
&&
2865 new->br_blockcount
+ right
.br_blockcount
<= MAXEXTLEN
&&
2866 (!(state
& BMAP_LEFT_CONTIG
) ||
2867 (left
.br_blockcount
+ new->br_blockcount
+
2868 right
.br_blockcount
<= MAXEXTLEN
)))
2869 state
|= BMAP_RIGHT_CONTIG
;
2872 * Switch out based on the contiguity flags.
2874 switch (state
& (BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
)) {
2875 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2877 * New allocation is contiguous with delayed allocations
2878 * on the left and on the right.
2879 * Merge all three into a single extent record.
2882 temp
= left
.br_blockcount
+ new->br_blockcount
+
2883 right
.br_blockcount
;
2885 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2886 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
), temp
);
2887 oldlen
= startblockval(left
.br_startblock
) +
2888 startblockval(new->br_startblock
) +
2889 startblockval(right
.br_startblock
);
2890 newlen
= xfs_bmap_worst_indlen(ip
, temp
);
2891 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp
, *idx
),
2892 nullstartblock((int)newlen
));
2893 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2895 xfs_iext_remove(ip
, *idx
+ 1, 1, state
);
2898 case BMAP_LEFT_CONTIG
:
2900 * New allocation is contiguous with a delayed allocation
2902 * Merge the new allocation with the left neighbor.
2905 temp
= left
.br_blockcount
+ new->br_blockcount
;
2907 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2908 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
), temp
);
2909 oldlen
= startblockval(left
.br_startblock
) +
2910 startblockval(new->br_startblock
);
2911 newlen
= xfs_bmap_worst_indlen(ip
, temp
);
2912 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp
, *idx
),
2913 nullstartblock((int)newlen
));
2914 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2917 case BMAP_RIGHT_CONTIG
:
2919 * New allocation is contiguous with a delayed allocation
2921 * Merge the new allocation with the right neighbor.
2923 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2924 temp
= new->br_blockcount
+ right
.br_blockcount
;
2925 oldlen
= startblockval(new->br_startblock
) +
2926 startblockval(right
.br_startblock
);
2927 newlen
= xfs_bmap_worst_indlen(ip
, temp
);
2928 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp
, *idx
),
2930 nullstartblock((int)newlen
), temp
, right
.br_state
);
2931 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2936 * New allocation is not contiguous with another
2937 * delayed allocation.
2938 * Insert a new entry.
2940 oldlen
= newlen
= 0;
2941 xfs_iext_insert(ip
, *idx
, 1, new, state
);
2944 if (oldlen
!= newlen
) {
2945 ASSERT(oldlen
> newlen
);
2946 xfs_icsb_modify_counters(ip
->i_mount
, XFS_SBS_FDBLOCKS
,
2947 (int64_t)(oldlen
- newlen
), 0);
2949 * Nothing to do for disk quota accounting here.
2955 * Convert a hole to a real allocation.
2957 STATIC
int /* error */
2958 xfs_bmap_add_extent_hole_real(
2959 struct xfs_bmalloca
*bma
,
2962 struct xfs_bmbt_irec
*new = &bma
->got
;
2963 int error
; /* error return value */
2964 int i
; /* temp state */
2965 xfs_ifork_t
*ifp
; /* inode fork pointer */
2966 xfs_bmbt_irec_t left
; /* left neighbor extent entry */
2967 xfs_bmbt_irec_t right
; /* right neighbor extent entry */
2968 int rval
=0; /* return value (logging flags) */
2969 int state
; /* state bits, accessed thru macros */
2971 ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
2973 ASSERT(bma
->idx
>= 0);
2974 ASSERT(bma
->idx
<= ifp
->if_bytes
/ sizeof(struct xfs_bmbt_rec
));
2975 ASSERT(!isnullstartblock(new->br_startblock
));
2977 !(bma
->cur
->bc_private
.b
.flags
& XFS_BTCUR_BPRV_WASDEL
));
2979 XFS_STATS_INC(xs_add_exlist
);
2982 if (whichfork
== XFS_ATTR_FORK
)
2983 state
|= BMAP_ATTRFORK
;
2986 * Check and set flags if this segment has a left neighbor.
2989 state
|= BMAP_LEFT_VALID
;
2990 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
- 1), &left
);
2991 if (isnullstartblock(left
.br_startblock
))
2992 state
|= BMAP_LEFT_DELAY
;
2996 * Check and set flags if this segment has a current value.
2997 * Not true if we're inserting into the "hole" at eof.
2999 if (bma
->idx
< ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
)) {
3000 state
|= BMAP_RIGHT_VALID
;
3001 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
), &right
);
3002 if (isnullstartblock(right
.br_startblock
))
3003 state
|= BMAP_RIGHT_DELAY
;
3007 * We're inserting a real allocation between "left" and "right".
3008 * Set the contiguity flags. Don't let extents get too large.
3010 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
3011 left
.br_startoff
+ left
.br_blockcount
== new->br_startoff
&&
3012 left
.br_startblock
+ left
.br_blockcount
== new->br_startblock
&&
3013 left
.br_state
== new->br_state
&&
3014 left
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
3015 state
|= BMAP_LEFT_CONTIG
;
3017 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
3018 new->br_startoff
+ new->br_blockcount
== right
.br_startoff
&&
3019 new->br_startblock
+ new->br_blockcount
== right
.br_startblock
&&
3020 new->br_state
== right
.br_state
&&
3021 new->br_blockcount
+ right
.br_blockcount
<= MAXEXTLEN
&&
3022 (!(state
& BMAP_LEFT_CONTIG
) ||
3023 left
.br_blockcount
+ new->br_blockcount
+
3024 right
.br_blockcount
<= MAXEXTLEN
))
3025 state
|= BMAP_RIGHT_CONTIG
;
3029 * Select which case we're in here, and implement it.
3031 switch (state
& (BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
)) {
3032 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
3034 * New allocation is contiguous with real allocations on the
3035 * left and on the right.
3036 * Merge all three into a single extent record.
3039 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
3040 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, bma
->idx
),
3041 left
.br_blockcount
+ new->br_blockcount
+
3042 right
.br_blockcount
);
3043 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
3045 xfs_iext_remove(bma
->ip
, bma
->idx
+ 1, 1, state
);
3047 XFS_IFORK_NEXT_SET(bma
->ip
, whichfork
,
3048 XFS_IFORK_NEXTENTS(bma
->ip
, whichfork
) - 1);
3049 if (bma
->cur
== NULL
) {
3050 rval
= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
3052 rval
= XFS_ILOG_CORE
;
3053 error
= xfs_bmbt_lookup_eq(bma
->cur
, right
.br_startoff
,
3054 right
.br_startblock
, right
.br_blockcount
,
3058 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
3059 error
= xfs_btree_delete(bma
->cur
, &i
);
3062 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
3063 error
= xfs_btree_decrement(bma
->cur
, 0, &i
);
3066 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
3067 error
= xfs_bmbt_update(bma
->cur
, left
.br_startoff
,
3069 left
.br_blockcount
+
3070 new->br_blockcount
+
3071 right
.br_blockcount
,
3078 case BMAP_LEFT_CONTIG
:
3080 * New allocation is contiguous with a real allocation
3082 * Merge the new allocation with the left neighbor.
3085 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
3086 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, bma
->idx
),
3087 left
.br_blockcount
+ new->br_blockcount
);
3088 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
3090 if (bma
->cur
== NULL
) {
3091 rval
= xfs_ilog_fext(whichfork
);
3094 error
= xfs_bmbt_lookup_eq(bma
->cur
, left
.br_startoff
,
3095 left
.br_startblock
, left
.br_blockcount
,
3099 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
3100 error
= xfs_bmbt_update(bma
->cur
, left
.br_startoff
,
3102 left
.br_blockcount
+
3110 case BMAP_RIGHT_CONTIG
:
3112 * New allocation is contiguous with a real allocation
3114 * Merge the new allocation with the right neighbor.
3116 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
3117 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp
, bma
->idx
),
3118 new->br_startoff
, new->br_startblock
,
3119 new->br_blockcount
+ right
.br_blockcount
,
3121 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
3123 if (bma
->cur
== NULL
) {
3124 rval
= xfs_ilog_fext(whichfork
);
3127 error
= xfs_bmbt_lookup_eq(bma
->cur
,
3129 right
.br_startblock
,
3130 right
.br_blockcount
, &i
);
3133 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
3134 error
= xfs_bmbt_update(bma
->cur
, new->br_startoff
,
3136 new->br_blockcount
+
3137 right
.br_blockcount
,
3146 * New allocation is not contiguous with another
3148 * Insert a new entry.
3150 xfs_iext_insert(bma
->ip
, bma
->idx
, 1, new, state
);
3151 XFS_IFORK_NEXT_SET(bma
->ip
, whichfork
,
3152 XFS_IFORK_NEXTENTS(bma
->ip
, whichfork
) + 1);
3153 if (bma
->cur
== NULL
) {
3154 rval
= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
3156 rval
= XFS_ILOG_CORE
;
3157 error
= xfs_bmbt_lookup_eq(bma
->cur
,
3160 new->br_blockcount
, &i
);
3163 XFS_WANT_CORRUPTED_GOTO(i
== 0, done
);
3164 bma
->cur
->bc_rec
.b
.br_state
= new->br_state
;
3165 error
= xfs_btree_insert(bma
->cur
, &i
);
3168 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
3173 /* convert to a btree if necessary */
3174 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
3175 int tmp_logflags
; /* partial log flag return val */
3177 ASSERT(bma
->cur
== NULL
);
3178 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
3179 bma
->firstblock
, bma
->flist
, &bma
->cur
,
3180 0, &tmp_logflags
, whichfork
);
3181 bma
->logflags
|= tmp_logflags
;
3186 /* clear out the allocated field, done with it now in any case. */
3188 bma
->cur
->bc_private
.b
.allocated
= 0;
3190 xfs_bmap_check_leaf_extents(bma
->cur
, bma
->ip
, whichfork
);
3192 bma
->logflags
|= rval
;
3197 * Functions used in the extent read, allocate and remove paths
3201 * Adjust the size of the new extent based on di_extsize and rt extsize.
3204 xfs_bmap_extsize_align(
3206 xfs_bmbt_irec_t
*gotp
, /* next extent pointer */
3207 xfs_bmbt_irec_t
*prevp
, /* previous extent pointer */
3208 xfs_extlen_t extsz
, /* align to this extent size */
3209 int rt
, /* is this a realtime inode? */
3210 int eof
, /* is extent at end-of-file? */
3211 int delay
, /* creating delalloc extent? */
3212 int convert
, /* overwriting unwritten extent? */
3213 xfs_fileoff_t
*offp
, /* in/out: aligned offset */
3214 xfs_extlen_t
*lenp
) /* in/out: aligned length */
3216 xfs_fileoff_t orig_off
; /* original offset */
3217 xfs_extlen_t orig_alen
; /* original length */
3218 xfs_fileoff_t orig_end
; /* original off+len */
3219 xfs_fileoff_t nexto
; /* next file offset */
3220 xfs_fileoff_t prevo
; /* previous file offset */
3221 xfs_fileoff_t align_off
; /* temp for offset */
3222 xfs_extlen_t align_alen
; /* temp for length */
3223 xfs_extlen_t temp
; /* temp for calculations */
3228 orig_off
= align_off
= *offp
;
3229 orig_alen
= align_alen
= *lenp
;
3230 orig_end
= orig_off
+ orig_alen
;
3233 * If this request overlaps an existing extent, then don't
3234 * attempt to perform any additional alignment.
3236 if (!delay
&& !eof
&&
3237 (orig_off
>= gotp
->br_startoff
) &&
3238 (orig_end
<= gotp
->br_startoff
+ gotp
->br_blockcount
)) {
3243 * If the file offset is unaligned vs. the extent size
3244 * we need to align it. This will be possible unless
3245 * the file was previously written with a kernel that didn't
3246 * perform this alignment, or if a truncate shot us in the
3249 temp
= do_mod(orig_off
, extsz
);
3255 * Same adjustment for the end of the requested area.
3257 if ((temp
= (align_alen
% extsz
))) {
3258 align_alen
+= extsz
- temp
;
3261 * If the previous block overlaps with this proposed allocation
3262 * then move the start forward without adjusting the length.
3264 if (prevp
->br_startoff
!= NULLFILEOFF
) {
3265 if (prevp
->br_startblock
== HOLESTARTBLOCK
)
3266 prevo
= prevp
->br_startoff
;
3268 prevo
= prevp
->br_startoff
+ prevp
->br_blockcount
;
3271 if (align_off
!= orig_off
&& align_off
< prevo
)
3274 * If the next block overlaps with this proposed allocation
3275 * then move the start back without adjusting the length,
3276 * but not before offset 0.
3277 * This may of course make the start overlap previous block,
3278 * and if we hit the offset 0 limit then the next block
3279 * can still overlap too.
3281 if (!eof
&& gotp
->br_startoff
!= NULLFILEOFF
) {
3282 if ((delay
&& gotp
->br_startblock
== HOLESTARTBLOCK
) ||
3283 (!delay
&& gotp
->br_startblock
== DELAYSTARTBLOCK
))
3284 nexto
= gotp
->br_startoff
+ gotp
->br_blockcount
;
3286 nexto
= gotp
->br_startoff
;
3288 nexto
= NULLFILEOFF
;
3290 align_off
+ align_alen
!= orig_end
&&
3291 align_off
+ align_alen
> nexto
)
3292 align_off
= nexto
> align_alen
? nexto
- align_alen
: 0;
3294 * If we're now overlapping the next or previous extent that
3295 * means we can't fit an extsz piece in this hole. Just move
3296 * the start forward to the first valid spot and set
3297 * the length so we hit the end.
3299 if (align_off
!= orig_off
&& align_off
< prevo
)
3301 if (align_off
+ align_alen
!= orig_end
&&
3302 align_off
+ align_alen
> nexto
&&
3303 nexto
!= NULLFILEOFF
) {
3304 ASSERT(nexto
> prevo
);
3305 align_alen
= nexto
- align_off
;
3309 * If realtime, and the result isn't a multiple of the realtime
3310 * extent size we need to remove blocks until it is.
3312 if (rt
&& (temp
= (align_alen
% mp
->m_sb
.sb_rextsize
))) {
3314 * We're not covering the original request, or
3315 * we won't be able to once we fix the length.
3317 if (orig_off
< align_off
||
3318 orig_end
> align_off
+ align_alen
||
3319 align_alen
- temp
< orig_alen
)
3322 * Try to fix it by moving the start up.
3324 if (align_off
+ temp
<= orig_off
) {
3329 * Try to fix it by moving the end in.
3331 else if (align_off
+ align_alen
- temp
>= orig_end
)
3334 * Set the start to the minimum then trim the length.
3337 align_alen
-= orig_off
- align_off
;
3338 align_off
= orig_off
;
3339 align_alen
-= align_alen
% mp
->m_sb
.sb_rextsize
;
3342 * Result doesn't cover the request, fail it.
3344 if (orig_off
< align_off
|| orig_end
> align_off
+ align_alen
)
3347 ASSERT(orig_off
>= align_off
);
3348 ASSERT(orig_end
<= align_off
+ align_alen
);
3352 if (!eof
&& gotp
->br_startoff
!= NULLFILEOFF
)
3353 ASSERT(align_off
+ align_alen
<= gotp
->br_startoff
);
3354 if (prevp
->br_startoff
!= NULLFILEOFF
)
3355 ASSERT(align_off
>= prevp
->br_startoff
+ prevp
->br_blockcount
);
3363 #define XFS_ALLOC_GAP_UNITS 4
3367 struct xfs_bmalloca
*ap
) /* bmap alloc argument struct */
3369 xfs_fsblock_t adjust
; /* adjustment to block numbers */
3370 xfs_agnumber_t fb_agno
; /* ag number of ap->firstblock */
3371 xfs_mount_t
*mp
; /* mount point structure */
3372 int nullfb
; /* true if ap->firstblock isn't set */
3373 int rt
; /* true if inode is realtime */
3375 #define ISVALID(x,y) \
3377 (x) < mp->m_sb.sb_rblocks : \
3378 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
3379 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
3380 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
3382 mp
= ap
->ip
->i_mount
;
3383 nullfb
= *ap
->firstblock
== NULLFSBLOCK
;
3384 rt
= XFS_IS_REALTIME_INODE(ap
->ip
) && ap
->userdata
;
3385 fb_agno
= nullfb
? NULLAGNUMBER
: XFS_FSB_TO_AGNO(mp
, *ap
->firstblock
);
3387 * If allocating at eof, and there's a previous real block,
3388 * try to use its last block as our starting point.
3390 if (ap
->eof
&& ap
->prev
.br_startoff
!= NULLFILEOFF
&&
3391 !isnullstartblock(ap
->prev
.br_startblock
) &&
3392 ISVALID(ap
->prev
.br_startblock
+ ap
->prev
.br_blockcount
,
3393 ap
->prev
.br_startblock
)) {
3394 ap
->blkno
= ap
->prev
.br_startblock
+ ap
->prev
.br_blockcount
;
3396 * Adjust for the gap between prevp and us.
3398 adjust
= ap
->offset
-
3399 (ap
->prev
.br_startoff
+ ap
->prev
.br_blockcount
);
3401 ISVALID(ap
->blkno
+ adjust
, ap
->prev
.br_startblock
))
3402 ap
->blkno
+= adjust
;
3405 * If not at eof, then compare the two neighbor blocks.
3406 * Figure out whether either one gives us a good starting point,
3407 * and pick the better one.
3409 else if (!ap
->eof
) {
3410 xfs_fsblock_t gotbno
; /* right side block number */
3411 xfs_fsblock_t gotdiff
=0; /* right side difference */
3412 xfs_fsblock_t prevbno
; /* left side block number */
3413 xfs_fsblock_t prevdiff
=0; /* left side difference */
3416 * If there's a previous (left) block, select a requested
3417 * start block based on it.
3419 if (ap
->prev
.br_startoff
!= NULLFILEOFF
&&
3420 !isnullstartblock(ap
->prev
.br_startblock
) &&
3421 (prevbno
= ap
->prev
.br_startblock
+
3422 ap
->prev
.br_blockcount
) &&
3423 ISVALID(prevbno
, ap
->prev
.br_startblock
)) {
3425 * Calculate gap to end of previous block.
3427 adjust
= prevdiff
= ap
->offset
-
3428 (ap
->prev
.br_startoff
+
3429 ap
->prev
.br_blockcount
);
3431 * Figure the startblock based on the previous block's
3432 * end and the gap size.
3434 * If the gap is large relative to the piece we're
3435 * allocating, or using it gives us an invalid block
3436 * number, then just use the end of the previous block.
3438 if (prevdiff
<= XFS_ALLOC_GAP_UNITS
* ap
->length
&&
3439 ISVALID(prevbno
+ prevdiff
,
3440 ap
->prev
.br_startblock
))
3445 * If the firstblock forbids it, can't use it,
3448 if (!rt
&& !nullfb
&&
3449 XFS_FSB_TO_AGNO(mp
, prevbno
) != fb_agno
)
3450 prevbno
= NULLFSBLOCK
;
3453 * No previous block or can't follow it, just default.
3456 prevbno
= NULLFSBLOCK
;
3458 * If there's a following (right) block, select a requested
3459 * start block based on it.
3461 if (!isnullstartblock(ap
->got
.br_startblock
)) {
3463 * Calculate gap to start of next block.
3465 adjust
= gotdiff
= ap
->got
.br_startoff
- ap
->offset
;
3467 * Figure the startblock based on the next block's
3468 * start and the gap size.
3470 gotbno
= ap
->got
.br_startblock
;
3473 * If the gap is large relative to the piece we're
3474 * allocating, or using it gives us an invalid block
3475 * number, then just use the start of the next block
3476 * offset by our length.
3478 if (gotdiff
<= XFS_ALLOC_GAP_UNITS
* ap
->length
&&
3479 ISVALID(gotbno
- gotdiff
, gotbno
))
3481 else if (ISVALID(gotbno
- ap
->length
, gotbno
)) {
3482 gotbno
-= ap
->length
;
3483 gotdiff
+= adjust
- ap
->length
;
3487 * If the firstblock forbids it, can't use it,
3490 if (!rt
&& !nullfb
&&
3491 XFS_FSB_TO_AGNO(mp
, gotbno
) != fb_agno
)
3492 gotbno
= NULLFSBLOCK
;
3495 * No next block, just default.
3498 gotbno
= NULLFSBLOCK
;
3500 * If both valid, pick the better one, else the only good
3501 * one, else ap->blkno is already set (to 0 or the inode block).
3503 if (prevbno
!= NULLFSBLOCK
&& gotbno
!= NULLFSBLOCK
)
3504 ap
->blkno
= prevdiff
<= gotdiff
? prevbno
: gotbno
;
3505 else if (prevbno
!= NULLFSBLOCK
)
3506 ap
->blkno
= prevbno
;
3507 else if (gotbno
!= NULLFSBLOCK
)
3514 xfs_bmap_longest_free_extent(
3515 struct xfs_trans
*tp
,
3520 struct xfs_mount
*mp
= tp
->t_mountp
;
3521 struct xfs_perag
*pag
;
3522 xfs_extlen_t longest
;
3525 pag
= xfs_perag_get(mp
, ag
);
3526 if (!pag
->pagf_init
) {
3527 error
= xfs_alloc_pagf_init(mp
, tp
, ag
, XFS_ALLOC_FLAG_TRYLOCK
);
3531 if (!pag
->pagf_init
) {
3537 longest
= xfs_alloc_longest_free_extent(mp
, pag
);
3538 if (*blen
< longest
)
3547 xfs_bmap_select_minlen(
3548 struct xfs_bmalloca
*ap
,
3549 struct xfs_alloc_arg
*args
,
3553 if (notinit
|| *blen
< ap
->minlen
) {
3555 * Since we did a BUF_TRYLOCK above, it is possible that
3556 * there is space for this request.
3558 args
->minlen
= ap
->minlen
;
3559 } else if (*blen
< args
->maxlen
) {
3561 * If the best seen length is less than the request length,
3562 * use the best as the minimum.
3564 args
->minlen
= *blen
;
3567 * Otherwise we've seen an extent as big as maxlen, use that
3570 args
->minlen
= args
->maxlen
;
3575 xfs_bmap_btalloc_nullfb(
3576 struct xfs_bmalloca
*ap
,
3577 struct xfs_alloc_arg
*args
,
3580 struct xfs_mount
*mp
= ap
->ip
->i_mount
;
3581 xfs_agnumber_t ag
, startag
;
3585 args
->type
= XFS_ALLOCTYPE_START_BNO
;
3586 args
->total
= ap
->total
;
3588 startag
= ag
= XFS_FSB_TO_AGNO(mp
, args
->fsbno
);
3589 if (startag
== NULLAGNUMBER
)
3592 while (*blen
< args
->maxlen
) {
3593 error
= xfs_bmap_longest_free_extent(args
->tp
, ag
, blen
,
3598 if (++ag
== mp
->m_sb
.sb_agcount
)
3604 xfs_bmap_select_minlen(ap
, args
, blen
, notinit
);
3609 xfs_bmap_btalloc_filestreams(
3610 struct xfs_bmalloca
*ap
,
3611 struct xfs_alloc_arg
*args
,
3614 struct xfs_mount
*mp
= ap
->ip
->i_mount
;
3619 args
->type
= XFS_ALLOCTYPE_NEAR_BNO
;
3620 args
->total
= ap
->total
;
3622 ag
= XFS_FSB_TO_AGNO(mp
, args
->fsbno
);
3623 if (ag
== NULLAGNUMBER
)
3626 error
= xfs_bmap_longest_free_extent(args
->tp
, ag
, blen
, ¬init
);
3630 if (*blen
< args
->maxlen
) {
3631 error
= xfs_filestream_new_ag(ap
, &ag
);
3635 error
= xfs_bmap_longest_free_extent(args
->tp
, ag
, blen
,
3642 xfs_bmap_select_minlen(ap
, args
, blen
, notinit
);
3645 * Set the failure fallback case to look in the selected AG as stream
3648 ap
->blkno
= args
->fsbno
= XFS_AGB_TO_FSB(mp
, ag
, 0);
3654 struct xfs_bmalloca
*ap
) /* bmap alloc argument struct */
3656 xfs_mount_t
*mp
; /* mount point structure */
3657 xfs_alloctype_t atype
= 0; /* type for allocation routines */
3658 xfs_extlen_t align
; /* minimum allocation alignment */
3659 xfs_agnumber_t fb_agno
; /* ag number of ap->firstblock */
3661 xfs_alloc_arg_t args
;
3663 xfs_extlen_t nextminlen
= 0;
3664 int nullfb
; /* true if ap->firstblock isn't set */
3672 mp
= ap
->ip
->i_mount
;
3674 /* stripe alignment for allocation is determined by mount parameters */
3676 if (mp
->m_swidth
&& (mp
->m_flags
& XFS_MOUNT_SWALLOC
))
3677 stripe_align
= mp
->m_swidth
;
3678 else if (mp
->m_dalign
)
3679 stripe_align
= mp
->m_dalign
;
3681 align
= ap
->userdata
? xfs_get_extsz_hint(ap
->ip
) : 0;
3682 if (unlikely(align
)) {
3683 error
= xfs_bmap_extsize_align(mp
, &ap
->got
, &ap
->prev
,
3684 align
, 0, ap
->eof
, 0, ap
->conv
,
3685 &ap
->offset
, &ap
->length
);
3691 nullfb
= *ap
->firstblock
== NULLFSBLOCK
;
3692 fb_agno
= nullfb
? NULLAGNUMBER
: XFS_FSB_TO_AGNO(mp
, *ap
->firstblock
);
3694 if (ap
->userdata
&& xfs_inode_is_filestream(ap
->ip
)) {
3695 ag
= xfs_filestream_lookup_ag(ap
->ip
);
3696 ag
= (ag
!= NULLAGNUMBER
) ? ag
: 0;
3697 ap
->blkno
= XFS_AGB_TO_FSB(mp
, ag
, 0);
3699 ap
->blkno
= XFS_INO_TO_FSB(mp
, ap
->ip
->i_ino
);
3702 ap
->blkno
= *ap
->firstblock
;
3704 xfs_bmap_adjacent(ap
);
3707 * If allowed, use ap->blkno; otherwise must use firstblock since
3708 * it's in the right allocation group.
3710 if (nullfb
|| XFS_FSB_TO_AGNO(mp
, ap
->blkno
) == fb_agno
)
3713 ap
->blkno
= *ap
->firstblock
;
3715 * Normal allocation, done through xfs_alloc_vextent.
3717 tryagain
= isaligned
= 0;
3718 memset(&args
, 0, sizeof(args
));
3721 args
.fsbno
= ap
->blkno
;
3723 /* Trim the allocation back to the maximum an AG can fit. */
3724 args
.maxlen
= MIN(ap
->length
, XFS_ALLOC_AG_MAX_USABLE(mp
));
3725 args
.firstblock
= *ap
->firstblock
;
3729 * Search for an allocation group with a single extent large
3730 * enough for the request. If one isn't found, then adjust
3731 * the minimum allocation size to the largest space found.
3733 if (ap
->userdata
&& xfs_inode_is_filestream(ap
->ip
))
3734 error
= xfs_bmap_btalloc_filestreams(ap
, &args
, &blen
);
3736 error
= xfs_bmap_btalloc_nullfb(ap
, &args
, &blen
);
3739 } else if (ap
->flist
->xbf_low
) {
3740 if (xfs_inode_is_filestream(ap
->ip
))
3741 args
.type
= XFS_ALLOCTYPE_FIRST_AG
;
3743 args
.type
= XFS_ALLOCTYPE_START_BNO
;
3744 args
.total
= args
.minlen
= ap
->minlen
;
3746 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
3747 args
.total
= ap
->total
;
3748 args
.minlen
= ap
->minlen
;
3750 /* apply extent size hints if obtained earlier */
3751 if (unlikely(align
)) {
3753 if ((args
.mod
= (xfs_extlen_t
)do_mod(ap
->offset
, args
.prod
)))
3754 args
.mod
= (xfs_extlen_t
)(args
.prod
- args
.mod
);
3755 } else if (mp
->m_sb
.sb_blocksize
>= PAGE_CACHE_SIZE
) {
3759 args
.prod
= PAGE_CACHE_SIZE
>> mp
->m_sb
.sb_blocklog
;
3760 if ((args
.mod
= (xfs_extlen_t
)(do_mod(ap
->offset
, args
.prod
))))
3761 args
.mod
= (xfs_extlen_t
)(args
.prod
- args
.mod
);
3764 * If we are not low on available data blocks, and the
3765 * underlying logical volume manager is a stripe, and
3766 * the file offset is zero then try to allocate data
3767 * blocks on stripe unit boundary.
3768 * NOTE: ap->aeof is only set if the allocation length
3769 * is >= the stripe unit and the allocation offset is
3770 * at the end of file.
3772 if (!ap
->flist
->xbf_low
&& ap
->aeof
) {
3774 args
.alignment
= stripe_align
;
3778 * Adjust for alignment
3780 if (blen
> args
.alignment
&& blen
<= args
.maxlen
)
3781 args
.minlen
= blen
- args
.alignment
;
3782 args
.minalignslop
= 0;
3785 * First try an exact bno allocation.
3786 * If it fails then do a near or start bno
3787 * allocation with alignment turned on.
3791 args
.type
= XFS_ALLOCTYPE_THIS_BNO
;
3794 * Compute the minlen+alignment for the
3795 * next case. Set slop so that the value
3796 * of minlen+alignment+slop doesn't go up
3797 * between the calls.
3799 if (blen
> stripe_align
&& blen
<= args
.maxlen
)
3800 nextminlen
= blen
- stripe_align
;
3802 nextminlen
= args
.minlen
;
3803 if (nextminlen
+ stripe_align
> args
.minlen
+ 1)
3805 nextminlen
+ stripe_align
-
3808 args
.minalignslop
= 0;
3812 args
.minalignslop
= 0;
3814 args
.minleft
= ap
->minleft
;
3815 args
.wasdel
= ap
->wasdel
;
3817 args
.userdata
= ap
->userdata
;
3818 if ((error
= xfs_alloc_vextent(&args
)))
3820 if (tryagain
&& args
.fsbno
== NULLFSBLOCK
) {
3822 * Exact allocation failed. Now try with alignment
3826 args
.fsbno
= ap
->blkno
;
3827 args
.alignment
= stripe_align
;
3828 args
.minlen
= nextminlen
;
3829 args
.minalignslop
= 0;
3831 if ((error
= xfs_alloc_vextent(&args
)))
3834 if (isaligned
&& args
.fsbno
== NULLFSBLOCK
) {
3836 * allocation failed, so turn off alignment and
3840 args
.fsbno
= ap
->blkno
;
3842 if ((error
= xfs_alloc_vextent(&args
)))
3845 if (args
.fsbno
== NULLFSBLOCK
&& nullfb
&&
3846 args
.minlen
> ap
->minlen
) {
3847 args
.minlen
= ap
->minlen
;
3848 args
.type
= XFS_ALLOCTYPE_START_BNO
;
3849 args
.fsbno
= ap
->blkno
;
3850 if ((error
= xfs_alloc_vextent(&args
)))
3853 if (args
.fsbno
== NULLFSBLOCK
&& nullfb
) {
3855 args
.type
= XFS_ALLOCTYPE_FIRST_AG
;
3856 args
.total
= ap
->minlen
;
3858 if ((error
= xfs_alloc_vextent(&args
)))
3860 ap
->flist
->xbf_low
= 1;
3862 if (args
.fsbno
!= NULLFSBLOCK
) {
3864 * check the allocation happened at the same or higher AG than
3865 * the first block that was allocated.
3867 ASSERT(*ap
->firstblock
== NULLFSBLOCK
||
3868 XFS_FSB_TO_AGNO(mp
, *ap
->firstblock
) ==
3869 XFS_FSB_TO_AGNO(mp
, args
.fsbno
) ||
3870 (ap
->flist
->xbf_low
&&
3871 XFS_FSB_TO_AGNO(mp
, *ap
->firstblock
) <
3872 XFS_FSB_TO_AGNO(mp
, args
.fsbno
)));
3874 ap
->blkno
= args
.fsbno
;
3875 if (*ap
->firstblock
== NULLFSBLOCK
)
3876 *ap
->firstblock
= args
.fsbno
;
3877 ASSERT(nullfb
|| fb_agno
== args
.agno
||
3878 (ap
->flist
->xbf_low
&& fb_agno
< args
.agno
));
3879 ap
->length
= args
.len
;
3880 ap
->ip
->i_d
.di_nblocks
+= args
.len
;
3881 xfs_trans_log_inode(ap
->tp
, ap
->ip
, XFS_ILOG_CORE
);
3883 ap
->ip
->i_delayed_blks
-= args
.len
;
3885 * Adjust the disk quota also. This was reserved
3888 xfs_trans_mod_dquot_byino(ap
->tp
, ap
->ip
,
3889 ap
->wasdel
? XFS_TRANS_DQ_DELBCOUNT
:
3890 XFS_TRANS_DQ_BCOUNT
,
3893 ap
->blkno
= NULLFSBLOCK
;
3900 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
3901 * It figures out where to ask the underlying allocator to put the new extent.
3905 struct xfs_bmalloca
*ap
) /* bmap alloc argument struct */
3907 if (XFS_IS_REALTIME_INODE(ap
->ip
) && ap
->userdata
)
3908 return xfs_bmap_rtalloc(ap
);
3909 return xfs_bmap_btalloc(ap
);
3913 * Trim the returned map to the required bounds
3917 struct xfs_bmbt_irec
*mval
,
3918 struct xfs_bmbt_irec
*got
,
3926 if ((flags
& XFS_BMAPI_ENTIRE
) ||
3927 got
->br_startoff
+ got
->br_blockcount
<= obno
) {
3929 if (isnullstartblock(got
->br_startblock
))
3930 mval
->br_startblock
= DELAYSTARTBLOCK
;
3936 ASSERT((*bno
>= obno
) || (n
== 0));
3938 mval
->br_startoff
= *bno
;
3939 if (isnullstartblock(got
->br_startblock
))
3940 mval
->br_startblock
= DELAYSTARTBLOCK
;
3942 mval
->br_startblock
= got
->br_startblock
+
3943 (*bno
- got
->br_startoff
);
3945 * Return the minimum of what we got and what we asked for for
3946 * the length. We can use the len variable here because it is
3947 * modified below and we could have been there before coming
3948 * here if the first part of the allocation didn't overlap what
3951 mval
->br_blockcount
= XFS_FILBLKS_MIN(end
- *bno
,
3952 got
->br_blockcount
- (*bno
- got
->br_startoff
));
3953 mval
->br_state
= got
->br_state
;
3954 ASSERT(mval
->br_blockcount
<= len
);
3959 * Update and validate the extent map to return
3962 xfs_bmapi_update_map(
3963 struct xfs_bmbt_irec
**map
,
3971 xfs_bmbt_irec_t
*mval
= *map
;
3973 ASSERT((flags
& XFS_BMAPI_ENTIRE
) ||
3974 ((mval
->br_startoff
+ mval
->br_blockcount
) <= end
));
3975 ASSERT((flags
& XFS_BMAPI_ENTIRE
) || (mval
->br_blockcount
<= *len
) ||
3976 (mval
->br_startoff
< obno
));
3978 *bno
= mval
->br_startoff
+ mval
->br_blockcount
;
3980 if (*n
> 0 && mval
->br_startoff
== mval
[-1].br_startoff
) {
3981 /* update previous map with new information */
3982 ASSERT(mval
->br_startblock
== mval
[-1].br_startblock
);
3983 ASSERT(mval
->br_blockcount
> mval
[-1].br_blockcount
);
3984 ASSERT(mval
->br_state
== mval
[-1].br_state
);
3985 mval
[-1].br_blockcount
= mval
->br_blockcount
;
3986 mval
[-1].br_state
= mval
->br_state
;
3987 } else if (*n
> 0 && mval
->br_startblock
!= DELAYSTARTBLOCK
&&
3988 mval
[-1].br_startblock
!= DELAYSTARTBLOCK
&&
3989 mval
[-1].br_startblock
!= HOLESTARTBLOCK
&&
3990 mval
->br_startblock
== mval
[-1].br_startblock
+
3991 mval
[-1].br_blockcount
&&
3992 ((flags
& XFS_BMAPI_IGSTATE
) ||
3993 mval
[-1].br_state
== mval
->br_state
)) {
3994 ASSERT(mval
->br_startoff
==
3995 mval
[-1].br_startoff
+ mval
[-1].br_blockcount
);
3996 mval
[-1].br_blockcount
+= mval
->br_blockcount
;
3997 } else if (*n
> 0 &&
3998 mval
->br_startblock
== DELAYSTARTBLOCK
&&
3999 mval
[-1].br_startblock
== DELAYSTARTBLOCK
&&
4000 mval
->br_startoff
==
4001 mval
[-1].br_startoff
+ mval
[-1].br_blockcount
) {
4002 mval
[-1].br_blockcount
+= mval
->br_blockcount
;
4003 mval
[-1].br_state
= mval
->br_state
;
4004 } else if (!((*n
== 0) &&
4005 ((mval
->br_startoff
+ mval
->br_blockcount
) <=
4014 * Map file blocks to filesystem blocks without allocation.
4018 struct xfs_inode
*ip
,
4021 struct xfs_bmbt_irec
*mval
,
4025 struct xfs_mount
*mp
= ip
->i_mount
;
4026 struct xfs_ifork
*ifp
;
4027 struct xfs_bmbt_irec got
;
4028 struct xfs_bmbt_irec prev
;
4035 int whichfork
= (flags
& XFS_BMAPI_ATTRFORK
) ?
4036 XFS_ATTR_FORK
: XFS_DATA_FORK
;
4039 ASSERT(!(flags
& ~(XFS_BMAPI_ATTRFORK
|XFS_BMAPI_ENTIRE
|
4040 XFS_BMAPI_IGSTATE
)));
4041 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_SHARED
|XFS_ILOCK_EXCL
));
4043 if (unlikely(XFS_TEST_ERROR(
4044 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
4045 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
4046 mp
, XFS_ERRTAG_BMAPIFORMAT
, XFS_RANDOM_BMAPIFORMAT
))) {
4047 XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW
, mp
);
4048 return -EFSCORRUPTED
;
4051 if (XFS_FORCED_SHUTDOWN(mp
))
4054 XFS_STATS_INC(xs_blk_mapr
);
4056 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4058 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
4059 error
= xfs_iread_extents(NULL
, ip
, whichfork
);
4064 xfs_bmap_search_extents(ip
, bno
, whichfork
, &eof
, &lastx
, &got
, &prev
);
4068 while (bno
< end
&& n
< *nmap
) {
4069 /* Reading past eof, act as though there's a hole up to end. */
4071 got
.br_startoff
= end
;
4072 if (got
.br_startoff
> bno
) {
4073 /* Reading in a hole. */
4074 mval
->br_startoff
= bno
;
4075 mval
->br_startblock
= HOLESTARTBLOCK
;
4076 mval
->br_blockcount
=
4077 XFS_FILBLKS_MIN(len
, got
.br_startoff
- bno
);
4078 mval
->br_state
= XFS_EXT_NORM
;
4079 bno
+= mval
->br_blockcount
;
4080 len
-= mval
->br_blockcount
;
4086 /* set up the extent map to return. */
4087 xfs_bmapi_trim_map(mval
, &got
, &bno
, len
, obno
, end
, n
, flags
);
4088 xfs_bmapi_update_map(&mval
, &bno
, &len
, obno
, end
, &n
, flags
);
4090 /* If we're done, stop now. */
4091 if (bno
>= end
|| n
>= *nmap
)
4094 /* Else go on to the next record. */
4095 if (++lastx
< ifp
->if_bytes
/ sizeof(xfs_bmbt_rec_t
))
4096 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, lastx
), &got
);
4105 xfs_bmapi_reserve_delalloc(
4106 struct xfs_inode
*ip
,
4109 struct xfs_bmbt_irec
*got
,
4110 struct xfs_bmbt_irec
*prev
,
4111 xfs_extnum_t
*lastx
,
4114 struct xfs_mount
*mp
= ip
->i_mount
;
4115 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, XFS_DATA_FORK
);
4117 xfs_extlen_t indlen
;
4118 char rt
= XFS_IS_REALTIME_INODE(ip
);
4122 alen
= XFS_FILBLKS_MIN(len
, MAXEXTLEN
);
4124 alen
= XFS_FILBLKS_MIN(alen
, got
->br_startoff
- aoff
);
4126 /* Figure out the extent size, adjust alen */
4127 extsz
= xfs_get_extsz_hint(ip
);
4130 * Make sure we don't exceed a single extent length when we
4131 * align the extent by reducing length we are going to
4132 * allocate by the maximum amount extent size aligment may
4135 alen
= XFS_FILBLKS_MIN(len
, MAXEXTLEN
- (2 * extsz
- 1));
4136 error
= xfs_bmap_extsize_align(mp
, got
, prev
, extsz
, rt
, eof
,
4137 1, 0, &aoff
, &alen
);
4142 extsz
= alen
/ mp
->m_sb
.sb_rextsize
;
4145 * Make a transaction-less quota reservation for delayed allocation
4146 * blocks. This number gets adjusted later. We return if we haven't
4147 * allocated blocks already inside this loop.
4149 error
= xfs_trans_reserve_quota_nblks(NULL
, ip
, (long)alen
, 0,
4150 rt
? XFS_QMOPT_RES_RTBLKS
: XFS_QMOPT_RES_REGBLKS
);
4155 * Split changing sb for alen and indlen since they could be coming
4156 * from different places.
4158 indlen
= (xfs_extlen_t
)xfs_bmap_worst_indlen(ip
, alen
);
4162 error
= xfs_mod_incore_sb(mp
, XFS_SBS_FREXTENTS
,
4163 -((int64_t)extsz
), 0);
4165 error
= xfs_icsb_modify_counters(mp
, XFS_SBS_FDBLOCKS
,
4166 -((int64_t)alen
), 0);
4170 goto out_unreserve_quota
;
4172 error
= xfs_icsb_modify_counters(mp
, XFS_SBS_FDBLOCKS
,
4173 -((int64_t)indlen
), 0);
4175 goto out_unreserve_blocks
;
4178 ip
->i_delayed_blks
+= alen
;
4180 got
->br_startoff
= aoff
;
4181 got
->br_startblock
= nullstartblock(indlen
);
4182 got
->br_blockcount
= alen
;
4183 got
->br_state
= XFS_EXT_NORM
;
4184 xfs_bmap_add_extent_hole_delay(ip
, lastx
, got
);
4187 * Update our extent pointer, given that xfs_bmap_add_extent_hole_delay
4188 * might have merged it into one of the neighbouring ones.
4190 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, *lastx
), got
);
4192 ASSERT(got
->br_startoff
<= aoff
);
4193 ASSERT(got
->br_startoff
+ got
->br_blockcount
>= aoff
+ alen
);
4194 ASSERT(isnullstartblock(got
->br_startblock
));
4195 ASSERT(got
->br_state
== XFS_EXT_NORM
);
4198 out_unreserve_blocks
:
4200 xfs_mod_incore_sb(mp
, XFS_SBS_FREXTENTS
, extsz
, 0);
4202 xfs_icsb_modify_counters(mp
, XFS_SBS_FDBLOCKS
, alen
, 0);
4203 out_unreserve_quota
:
4204 if (XFS_IS_QUOTA_ON(mp
))
4205 xfs_trans_unreserve_quota_nblks(NULL
, ip
, (long)alen
, 0, rt
?
4206 XFS_QMOPT_RES_RTBLKS
: XFS_QMOPT_RES_REGBLKS
);
4211 * Map file blocks to filesystem blocks, adding delayed allocations as needed.
4215 struct xfs_inode
*ip
, /* incore inode */
4216 xfs_fileoff_t bno
, /* starting file offs. mapped */
4217 xfs_filblks_t len
, /* length to map in file */
4218 struct xfs_bmbt_irec
*mval
, /* output: map values */
4219 int *nmap
, /* i/o: mval size/count */
4220 int flags
) /* XFS_BMAPI_... */
4222 struct xfs_mount
*mp
= ip
->i_mount
;
4223 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, XFS_DATA_FORK
);
4224 struct xfs_bmbt_irec got
; /* current file extent record */
4225 struct xfs_bmbt_irec prev
; /* previous file extent record */
4226 xfs_fileoff_t obno
; /* old block number (offset) */
4227 xfs_fileoff_t end
; /* end of mapped file region */
4228 xfs_extnum_t lastx
; /* last useful extent number */
4229 int eof
; /* we've hit the end of extents */
4230 int n
= 0; /* current extent index */
4234 ASSERT(*nmap
<= XFS_BMAP_MAX_NMAP
);
4235 ASSERT(!(flags
& ~XFS_BMAPI_ENTIRE
));
4236 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
4238 if (unlikely(XFS_TEST_ERROR(
4239 (XFS_IFORK_FORMAT(ip
, XFS_DATA_FORK
) != XFS_DINODE_FMT_EXTENTS
&&
4240 XFS_IFORK_FORMAT(ip
, XFS_DATA_FORK
) != XFS_DINODE_FMT_BTREE
),
4241 mp
, XFS_ERRTAG_BMAPIFORMAT
, XFS_RANDOM_BMAPIFORMAT
))) {
4242 XFS_ERROR_REPORT("xfs_bmapi_delay", XFS_ERRLEVEL_LOW
, mp
);
4243 return -EFSCORRUPTED
;
4246 if (XFS_FORCED_SHUTDOWN(mp
))
4249 XFS_STATS_INC(xs_blk_mapw
);
4251 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
4252 error
= xfs_iread_extents(NULL
, ip
, XFS_DATA_FORK
);
4257 xfs_bmap_search_extents(ip
, bno
, XFS_DATA_FORK
, &eof
, &lastx
, &got
, &prev
);
4261 while (bno
< end
&& n
< *nmap
) {
4262 if (eof
|| got
.br_startoff
> bno
) {
4263 error
= xfs_bmapi_reserve_delalloc(ip
, bno
, len
, &got
,
4264 &prev
, &lastx
, eof
);
4274 /* set up the extent map to return. */
4275 xfs_bmapi_trim_map(mval
, &got
, &bno
, len
, obno
, end
, n
, flags
);
4276 xfs_bmapi_update_map(&mval
, &bno
, &len
, obno
, end
, &n
, flags
);
4278 /* If we're done, stop now. */
4279 if (bno
>= end
|| n
>= *nmap
)
4282 /* Else go on to the next record. */
4284 if (++lastx
< ifp
->if_bytes
/ sizeof(xfs_bmbt_rec_t
))
4285 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, lastx
), &got
);
4297 struct xfs_bmalloca
*bma
)
4299 struct xfs_mount
*mp
= bma
->ip
->i_mount
;
4300 int whichfork
= (bma
->flags
& XFS_BMAPI_ATTRFORK
) ?
4301 XFS_ATTR_FORK
: XFS_DATA_FORK
;
4302 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
4303 int tmp_logflags
= 0;
4306 ASSERT(bma
->length
> 0);
4309 * For the wasdelay case, we could also just allocate the stuff asked
4310 * for in this bmap call but that wouldn't be as good.
4313 bma
->length
= (xfs_extlen_t
)bma
->got
.br_blockcount
;
4314 bma
->offset
= bma
->got
.br_startoff
;
4315 if (bma
->idx
!= NULLEXTNUM
&& bma
->idx
) {
4316 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
- 1),
4320 bma
->length
= XFS_FILBLKS_MIN(bma
->length
, MAXEXTLEN
);
4322 bma
->length
= XFS_FILBLKS_MIN(bma
->length
,
4323 bma
->got
.br_startoff
- bma
->offset
);
4327 * Indicate if this is the first user data in the file, or just any
4330 if (!(bma
->flags
& XFS_BMAPI_METADATA
)) {
4331 bma
->userdata
= (bma
->offset
== 0) ?
4332 XFS_ALLOC_INITIAL_USER_DATA
: XFS_ALLOC_USERDATA
;
4335 bma
->minlen
= (bma
->flags
& XFS_BMAPI_CONTIG
) ? bma
->length
: 1;
4338 * Only want to do the alignment at the eof if it is userdata and
4339 * allocation length is larger than a stripe unit.
4341 if (mp
->m_dalign
&& bma
->length
>= mp
->m_dalign
&&
4342 !(bma
->flags
& XFS_BMAPI_METADATA
) && whichfork
== XFS_DATA_FORK
) {
4343 error
= xfs_bmap_isaeof(bma
, whichfork
);
4348 error
= xfs_bmap_alloc(bma
);
4352 if (bma
->flist
->xbf_low
)
4355 bma
->cur
->bc_private
.b
.firstblock
= *bma
->firstblock
;
4356 if (bma
->blkno
== NULLFSBLOCK
)
4358 if ((ifp
->if_flags
& XFS_IFBROOT
) && !bma
->cur
) {
4359 bma
->cur
= xfs_bmbt_init_cursor(mp
, bma
->tp
, bma
->ip
, whichfork
);
4360 bma
->cur
->bc_private
.b
.firstblock
= *bma
->firstblock
;
4361 bma
->cur
->bc_private
.b
.flist
= bma
->flist
;
4364 * Bump the number of extents we've allocated
4370 bma
->cur
->bc_private
.b
.flags
=
4371 bma
->wasdel
? XFS_BTCUR_BPRV_WASDEL
: 0;
4373 bma
->got
.br_startoff
= bma
->offset
;
4374 bma
->got
.br_startblock
= bma
->blkno
;
4375 bma
->got
.br_blockcount
= bma
->length
;
4376 bma
->got
.br_state
= XFS_EXT_NORM
;
4379 * A wasdelay extent has been initialized, so shouldn't be flagged
4382 if (!bma
->wasdel
&& (bma
->flags
& XFS_BMAPI_PREALLOC
) &&
4383 xfs_sb_version_hasextflgbit(&mp
->m_sb
))
4384 bma
->got
.br_state
= XFS_EXT_UNWRITTEN
;
4387 error
= xfs_bmap_add_extent_delay_real(bma
);
4389 error
= xfs_bmap_add_extent_hole_real(bma
, whichfork
);
4391 bma
->logflags
|= tmp_logflags
;
4396 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4397 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4398 * the neighbouring ones.
4400 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
), &bma
->got
);
4402 ASSERT(bma
->got
.br_startoff
<= bma
->offset
);
4403 ASSERT(bma
->got
.br_startoff
+ bma
->got
.br_blockcount
>=
4404 bma
->offset
+ bma
->length
);
4405 ASSERT(bma
->got
.br_state
== XFS_EXT_NORM
||
4406 bma
->got
.br_state
== XFS_EXT_UNWRITTEN
);
4411 xfs_bmapi_convert_unwritten(
4412 struct xfs_bmalloca
*bma
,
4413 struct xfs_bmbt_irec
*mval
,
4417 int whichfork
= (flags
& XFS_BMAPI_ATTRFORK
) ?
4418 XFS_ATTR_FORK
: XFS_DATA_FORK
;
4419 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
4420 int tmp_logflags
= 0;
4423 /* check if we need to do unwritten->real conversion */
4424 if (mval
->br_state
== XFS_EXT_UNWRITTEN
&&
4425 (flags
& XFS_BMAPI_PREALLOC
))
4428 /* check if we need to do real->unwritten conversion */
4429 if (mval
->br_state
== XFS_EXT_NORM
&&
4430 (flags
& (XFS_BMAPI_PREALLOC
| XFS_BMAPI_CONVERT
)) !=
4431 (XFS_BMAPI_PREALLOC
| XFS_BMAPI_CONVERT
))
4435 * Modify (by adding) the state flag, if writing.
4437 ASSERT(mval
->br_blockcount
<= len
);
4438 if ((ifp
->if_flags
& XFS_IFBROOT
) && !bma
->cur
) {
4439 bma
->cur
= xfs_bmbt_init_cursor(bma
->ip
->i_mount
, bma
->tp
,
4440 bma
->ip
, whichfork
);
4441 bma
->cur
->bc_private
.b
.firstblock
= *bma
->firstblock
;
4442 bma
->cur
->bc_private
.b
.flist
= bma
->flist
;
4444 mval
->br_state
= (mval
->br_state
== XFS_EXT_UNWRITTEN
)
4445 ? XFS_EXT_NORM
: XFS_EXT_UNWRITTEN
;
4447 error
= xfs_bmap_add_extent_unwritten_real(bma
->tp
, bma
->ip
, &bma
->idx
,
4448 &bma
->cur
, mval
, bma
->firstblock
, bma
->flist
,
4450 bma
->logflags
|= tmp_logflags
;
4455 * Update our extent pointer, given that
4456 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4457 * of the neighbouring ones.
4459 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
), &bma
->got
);
4462 * We may have combined previously unwritten space with written space,
4463 * so generate another request.
4465 if (mval
->br_blockcount
< len
)
4471 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4472 * extent state if necessary. Details behaviour is controlled by the flags
4473 * parameter. Only allocates blocks from a single allocation group, to avoid
4476 * The returned value in "firstblock" from the first call in a transaction
4477 * must be remembered and presented to subsequent calls in "firstblock".
4478 * An upper bound for the number of blocks to be allocated is supplied to
4479 * the first call in "total"; if no allocation group has that many free
4480 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4484 struct xfs_trans
*tp
, /* transaction pointer */
4485 struct xfs_inode
*ip
, /* incore inode */
4486 xfs_fileoff_t bno
, /* starting file offs. mapped */
4487 xfs_filblks_t len
, /* length to map in file */
4488 int flags
, /* XFS_BMAPI_... */
4489 xfs_fsblock_t
*firstblock
, /* first allocated block
4490 controls a.g. for allocs */
4491 xfs_extlen_t total
, /* total blocks needed */
4492 struct xfs_bmbt_irec
*mval
, /* output: map values */
4493 int *nmap
, /* i/o: mval size/count */
4494 struct xfs_bmap_free
*flist
) /* i/o: list extents to free */
4496 struct xfs_mount
*mp
= ip
->i_mount
;
4497 struct xfs_ifork
*ifp
;
4498 struct xfs_bmalloca bma
= { NULL
}; /* args for xfs_bmap_alloc */
4499 xfs_fileoff_t end
; /* end of mapped file region */
4500 int eof
; /* after the end of extents */
4501 int error
; /* error return */
4502 int n
; /* current extent index */
4503 xfs_fileoff_t obno
; /* old block number (offset) */
4504 int whichfork
; /* data or attr fork */
4505 char inhole
; /* current location is hole in file */
4506 char wasdelay
; /* old extent was delayed */
4509 xfs_fileoff_t orig_bno
; /* original block number value */
4510 int orig_flags
; /* original flags arg value */
4511 xfs_filblks_t orig_len
; /* original value of len arg */
4512 struct xfs_bmbt_irec
*orig_mval
; /* original value of mval */
4513 int orig_nmap
; /* original value of *nmap */
4521 whichfork
= (flags
& XFS_BMAPI_ATTRFORK
) ?
4522 XFS_ATTR_FORK
: XFS_DATA_FORK
;
4525 ASSERT(*nmap
<= XFS_BMAP_MAX_NMAP
);
4526 ASSERT(!(flags
& XFS_BMAPI_IGSTATE
));
4529 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_LOCAL
);
4530 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
4532 if (unlikely(XFS_TEST_ERROR(
4533 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
4534 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
4535 mp
, XFS_ERRTAG_BMAPIFORMAT
, XFS_RANDOM_BMAPIFORMAT
))) {
4536 XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW
, mp
);
4537 return -EFSCORRUPTED
;
4540 if (XFS_FORCED_SHUTDOWN(mp
))
4543 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4545 XFS_STATS_INC(xs_blk_mapw
);
4547 if (*firstblock
== NULLFSBLOCK
) {
4548 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
)
4549 bma
.minleft
= be16_to_cpu(ifp
->if_broot
->bb_level
) + 1;
4556 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
4557 error
= xfs_iread_extents(tp
, ip
, whichfork
);
4562 xfs_bmap_search_extents(ip
, bno
, whichfork
, &eof
, &bma
.idx
, &bma
.got
,
4573 bma
.firstblock
= firstblock
;
4575 while (bno
< end
&& n
< *nmap
) {
4576 inhole
= eof
|| bma
.got
.br_startoff
> bno
;
4577 wasdelay
= !inhole
&& isnullstartblock(bma
.got
.br_startblock
);
4580 * First, deal with the hole before the allocated space
4581 * that we found, if any.
4583 if (inhole
|| wasdelay
) {
4585 bma
.conv
= !!(flags
& XFS_BMAPI_CONVERT
);
4586 bma
.wasdel
= wasdelay
;
4591 * There's a 32/64 bit type mismatch between the
4592 * allocation length request (which can be 64 bits in
4593 * length) and the bma length request, which is
4594 * xfs_extlen_t and therefore 32 bits. Hence we have to
4595 * check for 32-bit overflows and handle them here.
4597 if (len
> (xfs_filblks_t
)MAXEXTLEN
)
4598 bma
.length
= MAXEXTLEN
;
4603 ASSERT(bma
.length
> 0);
4604 error
= xfs_bmapi_allocate(&bma
);
4607 if (bma
.blkno
== NULLFSBLOCK
)
4611 /* Deal with the allocated space we found. */
4612 xfs_bmapi_trim_map(mval
, &bma
.got
, &bno
, len
, obno
,
4615 /* Execute unwritten extent conversion if necessary */
4616 error
= xfs_bmapi_convert_unwritten(&bma
, mval
, len
, flags
);
4617 if (error
== -EAGAIN
)
4622 /* update the extent map to return */
4623 xfs_bmapi_update_map(&mval
, &bno
, &len
, obno
, end
, &n
, flags
);
4626 * If we're done, stop now. Stop when we've allocated
4627 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4628 * the transaction may get too big.
4630 if (bno
>= end
|| n
>= *nmap
|| bma
.nallocs
>= *nmap
)
4633 /* Else go on to the next record. */
4635 if (++bma
.idx
< ifp
->if_bytes
/ sizeof(xfs_bmbt_rec_t
)) {
4636 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
.idx
),
4644 * Transform from btree to extents, give it cur.
4646 if (xfs_bmap_wants_extents(ip
, whichfork
)) {
4647 int tmp_logflags
= 0;
4650 error
= xfs_bmap_btree_to_extents(tp
, ip
, bma
.cur
,
4651 &tmp_logflags
, whichfork
);
4652 bma
.logflags
|= tmp_logflags
;
4657 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
||
4658 XFS_IFORK_NEXTENTS(ip
, whichfork
) >
4659 XFS_IFORK_MAXEXT(ip
, whichfork
));
4663 * Log everything. Do this after conversion, there's no point in
4664 * logging the extent records if we've converted to btree format.
4666 if ((bma
.logflags
& xfs_ilog_fext(whichfork
)) &&
4667 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
4668 bma
.logflags
&= ~xfs_ilog_fext(whichfork
);
4669 else if ((bma
.logflags
& xfs_ilog_fbroot(whichfork
)) &&
4670 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)
4671 bma
.logflags
&= ~xfs_ilog_fbroot(whichfork
);
4673 * Log whatever the flags say, even if error. Otherwise we might miss
4674 * detecting a case where the data is changed, there's an error,
4675 * and it's not logged so we don't shutdown when we should.
4678 xfs_trans_log_inode(tp
, ip
, bma
.logflags
);
4682 ASSERT(*firstblock
== NULLFSBLOCK
||
4683 XFS_FSB_TO_AGNO(mp
, *firstblock
) ==
4685 bma
.cur
->bc_private
.b
.firstblock
) ||
4687 XFS_FSB_TO_AGNO(mp
, *firstblock
) <
4689 bma
.cur
->bc_private
.b
.firstblock
)));
4690 *firstblock
= bma
.cur
->bc_private
.b
.firstblock
;
4692 xfs_btree_del_cursor(bma
.cur
,
4693 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
4696 xfs_bmap_validate_ret(orig_bno
, orig_len
, orig_flags
, orig_mval
,
4702 * Called by xfs_bmapi to update file extent records and the btree
4703 * after removing space (or undoing a delayed allocation).
4705 STATIC
int /* error */
4706 xfs_bmap_del_extent(
4707 xfs_inode_t
*ip
, /* incore inode pointer */
4708 xfs_trans_t
*tp
, /* current transaction pointer */
4709 xfs_extnum_t
*idx
, /* extent number to update/delete */
4710 xfs_bmap_free_t
*flist
, /* list of extents to be freed */
4711 xfs_btree_cur_t
*cur
, /* if null, not a btree */
4712 xfs_bmbt_irec_t
*del
, /* data to remove from extents */
4713 int *logflagsp
, /* inode logging flags */
4714 int whichfork
) /* data or attr fork */
4716 xfs_filblks_t da_new
; /* new delay-alloc indirect blocks */
4717 xfs_filblks_t da_old
; /* old delay-alloc indirect blocks */
4718 xfs_fsblock_t del_endblock
=0; /* first block past del */
4719 xfs_fileoff_t del_endoff
; /* first offset past del */
4720 int delay
; /* current block is delayed allocated */
4721 int do_fx
; /* free extent at end of routine */
4722 xfs_bmbt_rec_host_t
*ep
; /* current extent entry pointer */
4723 int error
; /* error return value */
4724 int flags
; /* inode logging flags */
4725 xfs_bmbt_irec_t got
; /* current extent entry */
4726 xfs_fileoff_t got_endoff
; /* first offset past got */
4727 int i
; /* temp state */
4728 xfs_ifork_t
*ifp
; /* inode fork pointer */
4729 xfs_mount_t
*mp
; /* mount structure */
4730 xfs_filblks_t nblks
; /* quota/sb block count */
4731 xfs_bmbt_irec_t
new; /* new record to be inserted */
4733 uint qfield
; /* quota field to update */
4734 xfs_filblks_t temp
; /* for indirect length calculations */
4735 xfs_filblks_t temp2
; /* for indirect length calculations */
4738 XFS_STATS_INC(xs_del_exlist
);
4740 if (whichfork
== XFS_ATTR_FORK
)
4741 state
|= BMAP_ATTRFORK
;
4744 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4745 ASSERT((*idx
>= 0) && (*idx
< ifp
->if_bytes
/
4746 (uint
)sizeof(xfs_bmbt_rec_t
)));
4747 ASSERT(del
->br_blockcount
> 0);
4748 ep
= xfs_iext_get_ext(ifp
, *idx
);
4749 xfs_bmbt_get_all(ep
, &got
);
4750 ASSERT(got
.br_startoff
<= del
->br_startoff
);
4751 del_endoff
= del
->br_startoff
+ del
->br_blockcount
;
4752 got_endoff
= got
.br_startoff
+ got
.br_blockcount
;
4753 ASSERT(got_endoff
>= del_endoff
);
4754 delay
= isnullstartblock(got
.br_startblock
);
4755 ASSERT(isnullstartblock(del
->br_startblock
) == delay
);
4760 * If deleting a real allocation, must free up the disk space.
4763 flags
= XFS_ILOG_CORE
;
4765 * Realtime allocation. Free it and record di_nblocks update.
4767 if (whichfork
== XFS_DATA_FORK
&& XFS_IS_REALTIME_INODE(ip
)) {
4771 ASSERT(do_mod(del
->br_blockcount
,
4772 mp
->m_sb
.sb_rextsize
) == 0);
4773 ASSERT(do_mod(del
->br_startblock
,
4774 mp
->m_sb
.sb_rextsize
) == 0);
4775 bno
= del
->br_startblock
;
4776 len
= del
->br_blockcount
;
4777 do_div(bno
, mp
->m_sb
.sb_rextsize
);
4778 do_div(len
, mp
->m_sb
.sb_rextsize
);
4779 error
= xfs_rtfree_extent(tp
, bno
, (xfs_extlen_t
)len
);
4783 nblks
= len
* mp
->m_sb
.sb_rextsize
;
4784 qfield
= XFS_TRANS_DQ_RTBCOUNT
;
4787 * Ordinary allocation.
4791 nblks
= del
->br_blockcount
;
4792 qfield
= XFS_TRANS_DQ_BCOUNT
;
4795 * Set up del_endblock and cur for later.
4797 del_endblock
= del
->br_startblock
+ del
->br_blockcount
;
4799 if ((error
= xfs_bmbt_lookup_eq(cur
, got
.br_startoff
,
4800 got
.br_startblock
, got
.br_blockcount
,
4803 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
4805 da_old
= da_new
= 0;
4807 da_old
= startblockval(got
.br_startblock
);
4813 * Set flag value to use in switch statement.
4814 * Left-contig is 2, right-contig is 1.
4816 switch (((got
.br_startoff
== del
->br_startoff
) << 1) |
4817 (got_endoff
== del_endoff
)) {
4820 * Matches the whole extent. Delete the entry.
4822 xfs_iext_remove(ip
, *idx
, 1,
4823 whichfork
== XFS_ATTR_FORK
? BMAP_ATTRFORK
: 0);
4828 XFS_IFORK_NEXT_SET(ip
, whichfork
,
4829 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
4830 flags
|= XFS_ILOG_CORE
;
4832 flags
|= xfs_ilog_fext(whichfork
);
4835 if ((error
= xfs_btree_delete(cur
, &i
)))
4837 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
4842 * Deleting the first part of the extent.
4844 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
4845 xfs_bmbt_set_startoff(ep
, del_endoff
);
4846 temp
= got
.br_blockcount
- del
->br_blockcount
;
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 xfs_bmbt_set_startblock(ep
, del_endblock
);
4857 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
4859 flags
|= xfs_ilog_fext(whichfork
);
4862 if ((error
= xfs_bmbt_update(cur
, del_endoff
, del_endblock
,
4863 got
.br_blockcount
- del
->br_blockcount
,
4870 * Deleting the last part of the extent.
4872 temp
= got
.br_blockcount
- del
->br_blockcount
;
4873 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
4874 xfs_bmbt_set_blockcount(ep
, temp
);
4876 temp
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
4878 xfs_bmbt_set_startblock(ep
, nullstartblock((int)temp
));
4879 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
4883 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
4885 flags
|= xfs_ilog_fext(whichfork
);
4888 if ((error
= xfs_bmbt_update(cur
, got
.br_startoff
,
4890 got
.br_blockcount
- del
->br_blockcount
,
4897 * Deleting the middle of the extent.
4899 temp
= del
->br_startoff
- got
.br_startoff
;
4900 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
4901 xfs_bmbt_set_blockcount(ep
, temp
);
4902 new.br_startoff
= del_endoff
;
4903 temp2
= got_endoff
- del_endoff
;
4904 new.br_blockcount
= temp2
;
4905 new.br_state
= got
.br_state
;
4907 new.br_startblock
= del_endblock
;
4908 flags
|= XFS_ILOG_CORE
;
4910 if ((error
= xfs_bmbt_update(cur
,
4912 got
.br_startblock
, temp
,
4915 if ((error
= xfs_btree_increment(cur
, 0, &i
)))
4917 cur
->bc_rec
.b
= new;
4918 error
= xfs_btree_insert(cur
, &i
);
4919 if (error
&& error
!= -ENOSPC
)
4922 * If get no-space back from btree insert,
4923 * it tried a split, and we have a zero
4924 * block reservation.
4925 * Fix up our state and return the error.
4927 if (error
== -ENOSPC
) {
4929 * Reset the cursor, don't trust
4930 * it after any insert operation.
4932 if ((error
= xfs_bmbt_lookup_eq(cur
,
4937 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
4939 * Update the btree record back
4940 * to the original value.
4942 if ((error
= xfs_bmbt_update(cur
,
4949 * Reset the extent record back
4950 * to the original value.
4952 xfs_bmbt_set_blockcount(ep
,
4958 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
4960 flags
|= xfs_ilog_fext(whichfork
);
4961 XFS_IFORK_NEXT_SET(ip
, whichfork
,
4962 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
4964 ASSERT(whichfork
== XFS_DATA_FORK
);
4965 temp
= xfs_bmap_worst_indlen(ip
, temp
);
4966 xfs_bmbt_set_startblock(ep
, nullstartblock((int)temp
));
4967 temp2
= xfs_bmap_worst_indlen(ip
, temp2
);
4968 new.br_startblock
= nullstartblock((int)temp2
);
4969 da_new
= temp
+ temp2
;
4970 while (da_new
> da_old
) {
4974 xfs_bmbt_set_startblock(ep
,
4975 nullstartblock((int)temp
));
4977 if (da_new
== da_old
)
4983 nullstartblock((int)temp2
);
4987 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
4988 xfs_iext_insert(ip
, *idx
+ 1, 1, &new, state
);
4993 * If we need to, add to list of extents to delete.
4996 xfs_bmap_add_free(del
->br_startblock
, del
->br_blockcount
, flist
,
4999 * Adjust inode # blocks in the file.
5002 ip
->i_d
.di_nblocks
-= nblks
;
5004 * Adjust quota data.
5007 xfs_trans_mod_dquot_byino(tp
, ip
, qfield
, (long)-nblks
);
5010 * Account for change in delayed indirect blocks.
5011 * Nothing to do for disk quota accounting here.
5013 ASSERT(da_old
>= da_new
);
5014 if (da_old
> da_new
) {
5015 xfs_icsb_modify_counters(mp
, XFS_SBS_FDBLOCKS
,
5016 (int64_t)(da_old
- da_new
), 0);
5024 * Unmap (remove) blocks from a file.
5025 * If nexts is nonzero then the number of extents to remove is limited to
5026 * that value. If not all extents in the block range can be removed then
5031 xfs_trans_t
*tp
, /* transaction pointer */
5032 struct xfs_inode
*ip
, /* incore inode */
5033 xfs_fileoff_t bno
, /* starting offset to unmap */
5034 xfs_filblks_t len
, /* length to unmap in file */
5035 int flags
, /* misc flags */
5036 xfs_extnum_t nexts
, /* number of extents max */
5037 xfs_fsblock_t
*firstblock
, /* first allocated block
5038 controls a.g. for allocs */
5039 xfs_bmap_free_t
*flist
, /* i/o: list extents to free */
5040 int *done
) /* set if not done yet */
5042 xfs_btree_cur_t
*cur
; /* bmap btree cursor */
5043 xfs_bmbt_irec_t del
; /* extent being deleted */
5044 int eof
; /* is deleting at eof */
5045 xfs_bmbt_rec_host_t
*ep
; /* extent record pointer */
5046 int error
; /* error return value */
5047 xfs_extnum_t extno
; /* extent number in list */
5048 xfs_bmbt_irec_t got
; /* current extent record */
5049 xfs_ifork_t
*ifp
; /* inode fork pointer */
5050 int isrt
; /* freeing in rt area */
5051 xfs_extnum_t lastx
; /* last extent index used */
5052 int logflags
; /* transaction logging flags */
5053 xfs_extlen_t mod
; /* rt extent offset */
5054 xfs_mount_t
*mp
; /* mount structure */
5055 xfs_extnum_t nextents
; /* number of file extents */
5056 xfs_bmbt_irec_t prev
; /* previous extent record */
5057 xfs_fileoff_t start
; /* first file offset deleted */
5058 int tmp_logflags
; /* partial logging flags */
5059 int wasdel
; /* was a delayed alloc extent */
5060 int whichfork
; /* data or attribute fork */
5063 trace_xfs_bunmap(ip
, bno
, len
, flags
, _RET_IP_
);
5065 whichfork
= (flags
& XFS_BMAPI_ATTRFORK
) ?
5066 XFS_ATTR_FORK
: XFS_DATA_FORK
;
5067 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5069 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
5070 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)) {
5071 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW
,
5073 return -EFSCORRUPTED
;
5076 if (XFS_FORCED_SHUTDOWN(mp
))
5079 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
5083 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
5084 (error
= xfs_iread_extents(tp
, ip
, whichfork
)))
5086 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
5087 if (nextents
== 0) {
5091 XFS_STATS_INC(xs_blk_unmap
);
5092 isrt
= (whichfork
== XFS_DATA_FORK
) && XFS_IS_REALTIME_INODE(ip
);
5094 bno
= start
+ len
- 1;
5095 ep
= xfs_bmap_search_extents(ip
, bno
, whichfork
, &eof
, &lastx
, &got
,
5099 * Check to see if the given block number is past the end of the
5100 * file, back up to the last block if so...
5103 ep
= xfs_iext_get_ext(ifp
, --lastx
);
5104 xfs_bmbt_get_all(ep
, &got
);
5105 bno
= got
.br_startoff
+ got
.br_blockcount
- 1;
5108 if (ifp
->if_flags
& XFS_IFBROOT
) {
5109 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
);
5110 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
5111 cur
->bc_private
.b
.firstblock
= *firstblock
;
5112 cur
->bc_private
.b
.flist
= flist
;
5113 cur
->bc_private
.b
.flags
= 0;
5119 * Synchronize by locking the bitmap inode.
5121 xfs_ilock(mp
->m_rbmip
, XFS_ILOCK_EXCL
);
5122 xfs_trans_ijoin(tp
, mp
->m_rbmip
, XFS_ILOCK_EXCL
);
5126 while (bno
!= (xfs_fileoff_t
)-1 && bno
>= start
&& lastx
>= 0 &&
5127 (nexts
== 0 || extno
< nexts
)) {
5129 * Is the found extent after a hole in which bno lives?
5130 * Just back up to the previous extent, if so.
5132 if (got
.br_startoff
> bno
) {
5135 ep
= xfs_iext_get_ext(ifp
, lastx
);
5136 xfs_bmbt_get_all(ep
, &got
);
5139 * Is the last block of this extent before the range
5140 * we're supposed to delete? If so, we're done.
5142 bno
= XFS_FILEOFF_MIN(bno
,
5143 got
.br_startoff
+ got
.br_blockcount
- 1);
5147 * Then deal with the (possibly delayed) allocated space
5152 wasdel
= isnullstartblock(del
.br_startblock
);
5153 if (got
.br_startoff
< start
) {
5154 del
.br_startoff
= start
;
5155 del
.br_blockcount
-= start
- got
.br_startoff
;
5157 del
.br_startblock
+= start
- got
.br_startoff
;
5159 if (del
.br_startoff
+ del
.br_blockcount
> bno
+ 1)
5160 del
.br_blockcount
= bno
+ 1 - del
.br_startoff
;
5161 sum
= del
.br_startblock
+ del
.br_blockcount
;
5163 (mod
= do_mod(sum
, mp
->m_sb
.sb_rextsize
))) {
5165 * Realtime extent not lined up at the end.
5166 * The extent could have been split into written
5167 * and unwritten pieces, or we could just be
5168 * unmapping part of it. But we can't really
5169 * get rid of part of a realtime extent.
5171 if (del
.br_state
== XFS_EXT_UNWRITTEN
||
5172 !xfs_sb_version_hasextflgbit(&mp
->m_sb
)) {
5174 * This piece is unwritten, or we're not
5175 * using unwritten extents. Skip over it.
5178 bno
-= mod
> del
.br_blockcount
?
5179 del
.br_blockcount
: mod
;
5180 if (bno
< got
.br_startoff
) {
5182 xfs_bmbt_get_all(xfs_iext_get_ext(
5188 * It's written, turn it unwritten.
5189 * This is better than zeroing it.
5191 ASSERT(del
.br_state
== XFS_EXT_NORM
);
5192 ASSERT(xfs_trans_get_block_res(tp
) > 0);
5194 * If this spans a realtime extent boundary,
5195 * chop it back to the start of the one we end at.
5197 if (del
.br_blockcount
> mod
) {
5198 del
.br_startoff
+= del
.br_blockcount
- mod
;
5199 del
.br_startblock
+= del
.br_blockcount
- mod
;
5200 del
.br_blockcount
= mod
;
5202 del
.br_state
= XFS_EXT_UNWRITTEN
;
5203 error
= xfs_bmap_add_extent_unwritten_real(tp
, ip
,
5204 &lastx
, &cur
, &del
, firstblock
, flist
,
5210 if (isrt
&& (mod
= do_mod(del
.br_startblock
, mp
->m_sb
.sb_rextsize
))) {
5212 * Realtime extent is lined up at the end but not
5213 * at the front. We'll get rid of full extents if
5216 mod
= mp
->m_sb
.sb_rextsize
- mod
;
5217 if (del
.br_blockcount
> mod
) {
5218 del
.br_blockcount
-= mod
;
5219 del
.br_startoff
+= mod
;
5220 del
.br_startblock
+= mod
;
5221 } else if ((del
.br_startoff
== start
&&
5222 (del
.br_state
== XFS_EXT_UNWRITTEN
||
5223 xfs_trans_get_block_res(tp
) == 0)) ||
5224 !xfs_sb_version_hasextflgbit(&mp
->m_sb
)) {
5226 * Can't make it unwritten. There isn't
5227 * a full extent here so just skip it.
5229 ASSERT(bno
>= del
.br_blockcount
);
5230 bno
-= del
.br_blockcount
;
5231 if (got
.br_startoff
> bno
) {
5233 ep
= xfs_iext_get_ext(ifp
,
5235 xfs_bmbt_get_all(ep
, &got
);
5239 } else if (del
.br_state
== XFS_EXT_UNWRITTEN
) {
5241 * This one is already unwritten.
5242 * It must have a written left neighbor.
5243 * Unwrite the killed part of that one and
5247 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
,
5249 ASSERT(prev
.br_state
== XFS_EXT_NORM
);
5250 ASSERT(!isnullstartblock(prev
.br_startblock
));
5251 ASSERT(del
.br_startblock
==
5252 prev
.br_startblock
+ prev
.br_blockcount
);
5253 if (prev
.br_startoff
< start
) {
5254 mod
= start
- prev
.br_startoff
;
5255 prev
.br_blockcount
-= mod
;
5256 prev
.br_startblock
+= mod
;
5257 prev
.br_startoff
= start
;
5259 prev
.br_state
= XFS_EXT_UNWRITTEN
;
5261 error
= xfs_bmap_add_extent_unwritten_real(tp
,
5262 ip
, &lastx
, &cur
, &prev
,
5263 firstblock
, flist
, &logflags
);
5268 ASSERT(del
.br_state
== XFS_EXT_NORM
);
5269 del
.br_state
= XFS_EXT_UNWRITTEN
;
5270 error
= xfs_bmap_add_extent_unwritten_real(tp
,
5271 ip
, &lastx
, &cur
, &del
,
5272 firstblock
, flist
, &logflags
);
5279 ASSERT(startblockval(del
.br_startblock
) > 0);
5280 /* Update realtime/data freespace, unreserve quota */
5282 xfs_filblks_t rtexts
;
5284 rtexts
= XFS_FSB_TO_B(mp
, del
.br_blockcount
);
5285 do_div(rtexts
, mp
->m_sb
.sb_rextsize
);
5286 xfs_mod_incore_sb(mp
, XFS_SBS_FREXTENTS
,
5287 (int64_t)rtexts
, 0);
5288 (void)xfs_trans_reserve_quota_nblks(NULL
,
5289 ip
, -((long)del
.br_blockcount
), 0,
5290 XFS_QMOPT_RES_RTBLKS
);
5292 xfs_icsb_modify_counters(mp
, XFS_SBS_FDBLOCKS
,
5293 (int64_t)del
.br_blockcount
, 0);
5294 (void)xfs_trans_reserve_quota_nblks(NULL
,
5295 ip
, -((long)del
.br_blockcount
), 0,
5296 XFS_QMOPT_RES_REGBLKS
);
5298 ip
->i_delayed_blks
-= del
.br_blockcount
;
5300 cur
->bc_private
.b
.flags
|=
5301 XFS_BTCUR_BPRV_WASDEL
;
5303 cur
->bc_private
.b
.flags
&= ~XFS_BTCUR_BPRV_WASDEL
;
5305 * If it's the case where the directory code is running
5306 * with no block reservation, and the deleted block is in
5307 * the middle of its extent, and the resulting insert
5308 * of an extent would cause transformation to btree format,
5309 * then reject it. The calling code will then swap
5310 * blocks around instead.
5311 * We have to do this now, rather than waiting for the
5312 * conversion to btree format, since the transaction
5315 if (!wasdel
&& xfs_trans_get_block_res(tp
) == 0 &&
5316 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
&&
5317 XFS_IFORK_NEXTENTS(ip
, whichfork
) >= /* Note the >= */
5318 XFS_IFORK_MAXEXT(ip
, whichfork
) &&
5319 del
.br_startoff
> got
.br_startoff
&&
5320 del
.br_startoff
+ del
.br_blockcount
<
5321 got
.br_startoff
+ got
.br_blockcount
) {
5325 error
= xfs_bmap_del_extent(ip
, tp
, &lastx
, flist
, cur
, &del
,
5326 &tmp_logflags
, whichfork
);
5327 logflags
|= tmp_logflags
;
5330 bno
= del
.br_startoff
- 1;
5333 * If not done go on to the next (previous) record.
5335 if (bno
!= (xfs_fileoff_t
)-1 && bno
>= start
) {
5337 ep
= xfs_iext_get_ext(ifp
, lastx
);
5338 if (xfs_bmbt_get_startoff(ep
) > bno
) {
5340 ep
= xfs_iext_get_ext(ifp
,
5343 xfs_bmbt_get_all(ep
, &got
);
5348 *done
= bno
== (xfs_fileoff_t
)-1 || bno
< start
|| lastx
< 0;
5351 * Convert to a btree if necessary.
5353 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
5354 ASSERT(cur
== NULL
);
5355 error
= xfs_bmap_extents_to_btree(tp
, ip
, firstblock
, flist
,
5356 &cur
, 0, &tmp_logflags
, whichfork
);
5357 logflags
|= tmp_logflags
;
5362 * transform from btree to extents, give it cur
5364 else if (xfs_bmap_wants_extents(ip
, whichfork
)) {
5365 ASSERT(cur
!= NULL
);
5366 error
= xfs_bmap_btree_to_extents(tp
, ip
, cur
, &tmp_logflags
,
5368 logflags
|= tmp_logflags
;
5373 * transform from extents to local?
5378 * Log everything. Do this after conversion, there's no point in
5379 * logging the extent records if we've converted to btree format.
5381 if ((logflags
& xfs_ilog_fext(whichfork
)) &&
5382 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
5383 logflags
&= ~xfs_ilog_fext(whichfork
);
5384 else if ((logflags
& xfs_ilog_fbroot(whichfork
)) &&
5385 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)
5386 logflags
&= ~xfs_ilog_fbroot(whichfork
);
5388 * Log inode even in the error case, if the transaction
5389 * is dirty we'll need to shut down the filesystem.
5392 xfs_trans_log_inode(tp
, ip
, logflags
);
5395 *firstblock
= cur
->bc_private
.b
.firstblock
;
5396 cur
->bc_private
.b
.allocated
= 0;
5398 xfs_btree_del_cursor(cur
,
5399 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
5405 * Determine whether an extent shift can be accomplished by a merge with the
5406 * extent that precedes the target hole of the shift.
5410 struct xfs_bmbt_irec
*left
, /* preceding extent */
5411 struct xfs_bmbt_irec
*got
, /* current extent to shift */
5412 xfs_fileoff_t shift
) /* shift fsb */
5414 xfs_fileoff_t startoff
;
5416 startoff
= got
->br_startoff
- shift
;
5419 * The extent, once shifted, must be adjacent in-file and on-disk with
5420 * the preceding extent.
5422 if ((left
->br_startoff
+ left
->br_blockcount
!= startoff
) ||
5423 (left
->br_startblock
+ left
->br_blockcount
!= got
->br_startblock
) ||
5424 (left
->br_state
!= got
->br_state
) ||
5425 (left
->br_blockcount
+ got
->br_blockcount
> MAXEXTLEN
))
5432 * A bmap extent shift adjusts the file offset of an extent to fill a preceding
5433 * hole in the file. If an extent shift would result in the extent being fully
5434 * adjacent to the extent that currently precedes the hole, we can merge with
5435 * the preceding extent rather than do the shift.
5437 * This function assumes the caller has verified a shift-by-merge is possible
5438 * with the provided extents via xfs_bmse_can_merge().
5442 struct xfs_inode
*ip
,
5444 xfs_fileoff_t shift
, /* shift fsb */
5445 int current_ext
, /* idx of gotp */
5446 struct xfs_bmbt_rec_host
*gotp
, /* extent to shift */
5447 struct xfs_bmbt_rec_host
*leftp
, /* preceding extent */
5448 struct xfs_btree_cur
*cur
,
5449 int *logflags
) /* output */
5451 struct xfs_ifork
*ifp
;
5452 struct xfs_bmbt_irec got
;
5453 struct xfs_bmbt_irec left
;
5454 xfs_filblks_t blockcount
;
5457 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5458 xfs_bmbt_get_all(gotp
, &got
);
5459 xfs_bmbt_get_all(leftp
, &left
);
5460 blockcount
= left
.br_blockcount
+ got
.br_blockcount
;
5462 ASSERT(xfs_isilocked(ip
, XFS_IOLOCK_EXCL
));
5463 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
5464 ASSERT(xfs_bmse_can_merge(&left
, &got
, shift
));
5467 * Merge the in-core extents. Note that the host record pointers and
5468 * current_ext index are invalid once the extent has been removed via
5469 * xfs_iext_remove().
5471 xfs_bmbt_set_blockcount(leftp
, blockcount
);
5472 xfs_iext_remove(ip
, current_ext
, 1, 0);
5475 * Update the on-disk extent count, the btree if necessary and log the
5478 XFS_IFORK_NEXT_SET(ip
, whichfork
,
5479 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
5480 *logflags
|= XFS_ILOG_CORE
;
5482 *logflags
|= XFS_ILOG_DEXT
;
5486 /* lookup and remove the extent to merge */
5487 error
= xfs_bmbt_lookup_eq(cur
, got
.br_startoff
, got
.br_startblock
,
5488 got
.br_blockcount
, &i
);
5491 XFS_WANT_CORRUPTED_GOTO(i
== 1, out_error
);
5493 error
= xfs_btree_delete(cur
, &i
);
5496 XFS_WANT_CORRUPTED_GOTO(i
== 1, out_error
);
5498 /* lookup and update size of the previous extent */
5499 error
= xfs_bmbt_lookup_eq(cur
, left
.br_startoff
, left
.br_startblock
,
5500 left
.br_blockcount
, &i
);
5503 XFS_WANT_CORRUPTED_GOTO(i
== 1, out_error
);
5505 left
.br_blockcount
= blockcount
;
5507 error
= xfs_bmbt_update(cur
, left
.br_startoff
, left
.br_startblock
,
5508 left
.br_blockcount
, left
.br_state
);
5519 * Shift a single extent.
5523 struct xfs_inode
*ip
,
5525 xfs_fileoff_t offset_shift_fsb
,
5527 struct xfs_bmbt_rec_host
*gotp
,
5528 struct xfs_btree_cur
*cur
,
5531 struct xfs_ifork
*ifp
;
5532 xfs_fileoff_t startoff
;
5533 struct xfs_bmbt_rec_host
*leftp
;
5534 struct xfs_bmbt_irec got
;
5535 struct xfs_bmbt_irec left
;
5539 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5541 xfs_bmbt_get_all(gotp
, &got
);
5542 startoff
= got
.br_startoff
- offset_shift_fsb
;
5544 /* delalloc extents should be prevented by caller */
5545 XFS_WANT_CORRUPTED_GOTO(!isnullstartblock(got
.br_startblock
),
5549 * If this is the first extent in the file, make sure there's enough
5550 * room at the start of the file and jump right to the shift as there's
5551 * no left extent to merge.
5553 if (*current_ext
== 0) {
5554 if (got
.br_startoff
< offset_shift_fsb
)
5559 /* grab the left extent and check for a large enough hole */
5560 leftp
= xfs_iext_get_ext(ifp
, *current_ext
- 1);
5561 xfs_bmbt_get_all(leftp
, &left
);
5563 if (startoff
< left
.br_startoff
+ left
.br_blockcount
)
5566 /* check whether to merge the extent or shift it down */
5567 if (!xfs_bmse_can_merge(&left
, &got
, offset_shift_fsb
))
5570 return xfs_bmse_merge(ip
, whichfork
, offset_shift_fsb
, *current_ext
,
5571 gotp
, leftp
, cur
, logflags
);
5575 * Increment the extent index for the next iteration, update the start
5576 * offset of the in-core extent and update the btree if applicable.
5579 xfs_bmbt_set_startoff(gotp
, startoff
);
5580 *logflags
|= XFS_ILOG_CORE
;
5582 *logflags
|= XFS_ILOG_DEXT
;
5586 error
= xfs_bmbt_lookup_eq(cur
, got
.br_startoff
, got
.br_startblock
,
5587 got
.br_blockcount
, &i
);
5590 XFS_WANT_CORRUPTED_GOTO(i
== 1, out_error
);
5592 got
.br_startoff
= startoff
;
5593 error
= xfs_bmbt_update(cur
, got
.br_startoff
, got
.br_startblock
,
5594 got
.br_blockcount
, got
.br_state
);
5605 * Shift extent records to the left to cover a hole.
5607 * The maximum number of extents to be shifted in a single operation is
5608 * @num_exts. @start_fsb specifies the file offset to start the shift and the
5609 * file offset where we've left off is returned in @next_fsb. @offset_shift_fsb
5610 * is the length by which each extent is shifted. If there is no hole to shift
5611 * the extents into, this will be considered invalid operation and we abort
5615 xfs_bmap_shift_extents(
5616 struct xfs_trans
*tp
,
5617 struct xfs_inode
*ip
,
5618 xfs_fileoff_t start_fsb
,
5619 xfs_fileoff_t offset_shift_fsb
,
5621 xfs_fileoff_t
*next_fsb
,
5622 xfs_fsblock_t
*firstblock
,
5623 struct xfs_bmap_free
*flist
,
5626 struct xfs_btree_cur
*cur
= NULL
;
5627 struct xfs_bmbt_rec_host
*gotp
;
5628 struct xfs_bmbt_irec got
;
5629 struct xfs_mount
*mp
= ip
->i_mount
;
5630 struct xfs_ifork
*ifp
;
5631 xfs_extnum_t nexts
= 0;
5632 xfs_extnum_t current_ext
;
5634 int whichfork
= XFS_DATA_FORK
;
5638 if (unlikely(XFS_TEST_ERROR(
5639 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
5640 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
5641 mp
, XFS_ERRTAG_BMAPIFORMAT
, XFS_RANDOM_BMAPIFORMAT
))) {
5642 XFS_ERROR_REPORT("xfs_bmap_shift_extents",
5643 XFS_ERRLEVEL_LOW
, mp
);
5644 return -EFSCORRUPTED
;
5647 if (XFS_FORCED_SHUTDOWN(mp
))
5650 ASSERT(xfs_isilocked(ip
, XFS_IOLOCK_EXCL
));
5651 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
5653 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5654 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
5655 /* Read in all the extents */
5656 error
= xfs_iread_extents(tp
, ip
, whichfork
);
5661 if (ifp
->if_flags
& XFS_IFBROOT
) {
5662 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
5663 cur
->bc_private
.b
.firstblock
= *firstblock
;
5664 cur
->bc_private
.b
.flist
= flist
;
5665 cur
->bc_private
.b
.flags
= 0;
5669 * Look up the extent index for the fsb where we start shifting. We can
5670 * henceforth iterate with current_ext as extent list changes are locked
5673 * gotp can be null in 2 cases: 1) if there are no extents or 2)
5674 * start_fsb lies in a hole beyond which there are no extents. Either
5677 gotp
= xfs_iext_bno_to_ext(ifp
, start_fsb
, ¤t_ext
);
5684 * There may be delalloc extents in the data fork before the range we
5685 * are collapsing out, so we cannot use the count of real extents here.
5686 * Instead we have to calculate it from the incore fork.
5688 total_extents
= ifp
->if_bytes
/ sizeof(xfs_bmbt_rec_t
);
5689 while (nexts
++ < num_exts
&& current_ext
< total_extents
) {
5690 error
= xfs_bmse_shift_one(ip
, whichfork
, offset_shift_fsb
,
5691 ¤t_ext
, gotp
, cur
, &logflags
);
5695 /* update total extent count and grab the next record */
5696 total_extents
= ifp
->if_bytes
/ sizeof(xfs_bmbt_rec_t
);
5697 if (current_ext
>= total_extents
)
5699 gotp
= xfs_iext_get_ext(ifp
, current_ext
);
5702 /* Check if we are done */
5703 if (current_ext
== total_extents
) {
5705 } else if (next_fsb
) {
5706 xfs_bmbt_get_all(gotp
, &got
);
5707 *next_fsb
= got
.br_startoff
;
5712 xfs_btree_del_cursor(cur
,
5713 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
5716 xfs_trans_log_inode(tp
, ip
, logflags
);