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_format.h"
24 #include "xfs_trans.h"
27 #include "xfs_mount.h"
28 #include "xfs_da_btree.h"
29 #include "xfs_dir2_format.h"
31 #include "xfs_bmap_btree.h"
32 #include "xfs_alloc_btree.h"
33 #include "xfs_ialloc_btree.h"
34 #include "xfs_dinode.h"
35 #include "xfs_inode.h"
36 #include "xfs_btree.h"
37 #include "xfs_mount.h"
38 #include "xfs_itable.h"
39 #include "xfs_inode_item.h"
40 #include "xfs_extfree_item.h"
41 #include "xfs_alloc.h"
43 #include "xfs_bmap_util.h"
44 #include "xfs_rtalloc.h"
45 #include "xfs_error.h"
46 #include "xfs_attr_leaf.h"
47 #include "xfs_quota.h"
48 #include "xfs_trans_space.h"
49 #include "xfs_buf_item.h"
50 #include "xfs_filestream.h"
51 #include "xfs_trace.h"
52 #include "xfs_symlink.h"
55 kmem_zone_t
*xfs_bmap_free_item_zone
;
58 * Miscellaneous helper functions
62 * Compute and fill in the value of the maximum depth of a bmap btree
63 * in this filesystem. Done once, during mount.
66 xfs_bmap_compute_maxlevels(
67 xfs_mount_t
*mp
, /* file system mount structure */
68 int whichfork
) /* data or attr fork */
70 int level
; /* btree level */
71 uint maxblocks
; /* max blocks at this level */
72 uint maxleafents
; /* max leaf entries possible */
73 int maxrootrecs
; /* max records in root block */
74 int minleafrecs
; /* min records in leaf block */
75 int minnoderecs
; /* min records in node block */
76 int sz
; /* root block size */
79 * The maximum number of extents in a file, hence the maximum
80 * number of leaf entries, is controlled by the type of di_nextents
81 * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
82 * (a signed 16-bit number, xfs_aextnum_t).
84 * Note that we can no longer assume that if we are in ATTR1 that
85 * the fork offset of all the inodes will be
86 * (xfs_default_attroffset(ip) >> 3) because we could have mounted
87 * with ATTR2 and then mounted back with ATTR1, keeping the
88 * di_forkoff's fixed but probably at various positions. Therefore,
89 * for both ATTR1 and ATTR2 we have to assume the worst case scenario
90 * of a minimum size available.
92 if (whichfork
== XFS_DATA_FORK
) {
93 maxleafents
= MAXEXTNUM
;
94 sz
= XFS_BMDR_SPACE_CALC(MINDBTPTRS
);
96 maxleafents
= MAXAEXTNUM
;
97 sz
= XFS_BMDR_SPACE_CALC(MINABTPTRS
);
99 maxrootrecs
= xfs_bmdr_maxrecs(mp
, sz
, 0);
100 minleafrecs
= mp
->m_bmap_dmnr
[0];
101 minnoderecs
= mp
->m_bmap_dmnr
[1];
102 maxblocks
= (maxleafents
+ minleafrecs
- 1) / minleafrecs
;
103 for (level
= 1; maxblocks
> 1; level
++) {
104 if (maxblocks
<= maxrootrecs
)
107 maxblocks
= (maxblocks
+ minnoderecs
- 1) / minnoderecs
;
109 mp
->m_bm_maxlevels
[whichfork
] = level
;
112 STATIC
int /* error */
114 struct xfs_btree_cur
*cur
,
118 int *stat
) /* success/failure */
120 cur
->bc_rec
.b
.br_startoff
= off
;
121 cur
->bc_rec
.b
.br_startblock
= bno
;
122 cur
->bc_rec
.b
.br_blockcount
= len
;
123 return xfs_btree_lookup(cur
, XFS_LOOKUP_EQ
, stat
);
126 STATIC
int /* error */
128 struct xfs_btree_cur
*cur
,
132 int *stat
) /* success/failure */
134 cur
->bc_rec
.b
.br_startoff
= off
;
135 cur
->bc_rec
.b
.br_startblock
= bno
;
136 cur
->bc_rec
.b
.br_blockcount
= len
;
137 return xfs_btree_lookup(cur
, XFS_LOOKUP_GE
, stat
);
141 * Check if the inode needs to be converted to btree format.
143 static inline bool xfs_bmap_needs_btree(struct xfs_inode
*ip
, int whichfork
)
145 return XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
&&
146 XFS_IFORK_NEXTENTS(ip
, whichfork
) >
147 XFS_IFORK_MAXEXT(ip
, whichfork
);
151 * Check if the inode should be converted to extent format.
153 static inline bool xfs_bmap_wants_extents(struct xfs_inode
*ip
, int whichfork
)
155 return XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
&&
156 XFS_IFORK_NEXTENTS(ip
, whichfork
) <=
157 XFS_IFORK_MAXEXT(ip
, whichfork
);
161 * Update the record referred to by cur to the value given
162 * by [off, bno, len, state].
163 * This either works (return 0) or gets an EFSCORRUPTED error.
167 struct xfs_btree_cur
*cur
,
173 union xfs_btree_rec rec
;
175 xfs_bmbt_disk_set_allf(&rec
.bmbt
, off
, bno
, len
, state
);
176 return xfs_btree_update(cur
, &rec
);
180 * Compute the worst-case number of indirect blocks that will be used
181 * for ip's delayed extent of length "len".
184 xfs_bmap_worst_indlen(
185 xfs_inode_t
*ip
, /* incore inode pointer */
186 xfs_filblks_t len
) /* delayed extent length */
188 int level
; /* btree level number */
189 int maxrecs
; /* maximum record count at this level */
190 xfs_mount_t
*mp
; /* mount structure */
191 xfs_filblks_t rval
; /* return value */
194 maxrecs
= mp
->m_bmap_dmxr
[0];
195 for (level
= 0, rval
= 0;
196 level
< XFS_BM_MAXLEVELS(mp
, XFS_DATA_FORK
);
199 do_div(len
, maxrecs
);
202 return rval
+ XFS_BM_MAXLEVELS(mp
, XFS_DATA_FORK
) -
205 maxrecs
= mp
->m_bmap_dmxr
[1];
211 * Calculate the default attribute fork offset for newly created inodes.
214 xfs_default_attroffset(
215 struct xfs_inode
*ip
)
217 struct xfs_mount
*mp
= ip
->i_mount
;
220 if (mp
->m_sb
.sb_inodesize
== 256) {
221 offset
= XFS_LITINO(mp
, ip
->i_d
.di_version
) -
222 XFS_BMDR_SPACE_CALC(MINABTPTRS
);
224 offset
= XFS_BMDR_SPACE_CALC(6 * MINABTPTRS
);
227 ASSERT(offset
< XFS_LITINO(mp
, ip
->i_d
.di_version
));
232 * Helper routine to reset inode di_forkoff field when switching
233 * attribute fork from local to extent format - we reset it where
234 * possible to make space available for inline data fork extents.
237 xfs_bmap_forkoff_reset(
242 if (whichfork
== XFS_ATTR_FORK
&&
243 ip
->i_d
.di_format
!= XFS_DINODE_FMT_DEV
&&
244 ip
->i_d
.di_format
!= XFS_DINODE_FMT_UUID
&&
245 ip
->i_d
.di_format
!= XFS_DINODE_FMT_BTREE
) {
246 uint dfl_forkoff
= xfs_default_attroffset(ip
) >> 3;
248 if (dfl_forkoff
> ip
->i_d
.di_forkoff
)
249 ip
->i_d
.di_forkoff
= dfl_forkoff
;
254 * Debug/sanity checking code
258 xfs_bmap_sanity_check(
259 struct xfs_mount
*mp
,
263 struct xfs_btree_block
*block
= XFS_BUF_TO_BLOCK(bp
);
265 if (block
->bb_magic
!= cpu_to_be32(XFS_BMAP_CRC_MAGIC
) &&
266 block
->bb_magic
!= cpu_to_be32(XFS_BMAP_MAGIC
))
269 if (be16_to_cpu(block
->bb_level
) != level
||
270 be16_to_cpu(block
->bb_numrecs
) == 0 ||
271 be16_to_cpu(block
->bb_numrecs
) > mp
->m_bmap_dmxr
[level
!= 0])
278 STATIC
struct xfs_buf
*
280 struct xfs_btree_cur
*cur
,
283 struct xfs_log_item_desc
*lidp
;
289 for (i
= 0; i
< XFS_BTREE_MAXLEVELS
; i
++) {
290 if (!cur
->bc_bufs
[i
])
292 if (XFS_BUF_ADDR(cur
->bc_bufs
[i
]) == bno
)
293 return cur
->bc_bufs
[i
];
296 /* Chase down all the log items to see if the bp is there */
297 list_for_each_entry(lidp
, &cur
->bc_tp
->t_items
, lid_trans
) {
298 struct xfs_buf_log_item
*bip
;
299 bip
= (struct xfs_buf_log_item
*)lidp
->lid_item
;
300 if (bip
->bli_item
.li_type
== XFS_LI_BUF
&&
301 XFS_BUF_ADDR(bip
->bli_buf
) == bno
)
310 struct xfs_btree_block
*block
,
316 __be64
*pp
, *thispa
; /* pointer to block address */
317 xfs_bmbt_key_t
*prevp
, *keyp
;
319 ASSERT(be16_to_cpu(block
->bb_level
) > 0);
322 for( i
= 1; i
<= xfs_btree_get_numrecs(block
); i
++) {
323 dmxr
= mp
->m_bmap_dmxr
[0];
324 keyp
= XFS_BMBT_KEY_ADDR(mp
, block
, i
);
327 ASSERT(be64_to_cpu(prevp
->br_startoff
) <
328 be64_to_cpu(keyp
->br_startoff
));
333 * Compare the block numbers to see if there are dups.
336 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, i
, sz
);
338 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, i
, dmxr
);
340 for (j
= i
+1; j
<= be16_to_cpu(block
->bb_numrecs
); j
++) {
342 thispa
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, j
, sz
);
344 thispa
= XFS_BMBT_PTR_ADDR(mp
, block
, j
, dmxr
);
345 if (*thispa
== *pp
) {
346 xfs_warn(mp
, "%s: thispa(%d) == pp(%d) %Ld",
348 (unsigned long long)be64_to_cpu(*thispa
));
349 panic("%s: ptrs are equal in node\n",
357 * Check that the extents for the inode ip are in the right order in all
362 xfs_bmap_check_leaf_extents(
363 xfs_btree_cur_t
*cur
, /* btree cursor or null */
364 xfs_inode_t
*ip
, /* incore inode pointer */
365 int whichfork
) /* data or attr fork */
367 struct xfs_btree_block
*block
; /* current btree block */
368 xfs_fsblock_t bno
; /* block # of "block" */
369 xfs_buf_t
*bp
; /* buffer for "block" */
370 int error
; /* error return value */
371 xfs_extnum_t i
=0, j
; /* index into the extents list */
372 xfs_ifork_t
*ifp
; /* fork structure */
373 int level
; /* btree level, for checking */
374 xfs_mount_t
*mp
; /* file system mount structure */
375 __be64
*pp
; /* pointer to block address */
376 xfs_bmbt_rec_t
*ep
; /* pointer to current extent */
377 xfs_bmbt_rec_t last
= {0, 0}; /* last extent in prev block */
378 xfs_bmbt_rec_t
*nextp
; /* pointer to next extent */
381 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
) {
387 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
388 block
= ifp
->if_broot
;
390 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
392 level
= be16_to_cpu(block
->bb_level
);
394 xfs_check_block(block
, mp
, 1, ifp
->if_broot_bytes
);
395 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, 1, ifp
->if_broot_bytes
);
396 bno
= be64_to_cpu(*pp
);
398 ASSERT(bno
!= NULLDFSBNO
);
399 ASSERT(XFS_FSB_TO_AGNO(mp
, bno
) < mp
->m_sb
.sb_agcount
);
400 ASSERT(XFS_FSB_TO_AGBNO(mp
, bno
) < mp
->m_sb
.sb_agblocks
);
403 * Go down the tree until leaf level is reached, following the first
404 * pointer (leftmost) at each level.
406 while (level
-- > 0) {
407 /* See if buf is in cur first */
409 bp
= xfs_bmap_get_bp(cur
, XFS_FSB_TO_DADDR(mp
, bno
));
412 error
= xfs_btree_read_bufl(mp
, NULL
, bno
, 0, &bp
,
418 block
= XFS_BUF_TO_BLOCK(bp
);
419 XFS_WANT_CORRUPTED_GOTO(
420 xfs_bmap_sanity_check(mp
, bp
, level
),
426 * Check this block for basic sanity (increasing keys and
427 * no duplicate blocks).
430 xfs_check_block(block
, mp
, 0, 0);
431 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, mp
->m_bmap_dmxr
[1]);
432 bno
= be64_to_cpu(*pp
);
433 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp
, bno
), error0
);
436 xfs_trans_brelse(NULL
, bp
);
441 * Here with bp and block set to the leftmost leaf node in the tree.
446 * Loop over all leaf nodes checking that all extents are in the right order.
449 xfs_fsblock_t nextbno
;
450 xfs_extnum_t num_recs
;
453 num_recs
= xfs_btree_get_numrecs(block
);
456 * Read-ahead the next leaf block, if any.
459 nextbno
= be64_to_cpu(block
->bb_u
.l
.bb_rightsib
);
462 * Check all the extents to make sure they are OK.
463 * If we had a previous block, the last entry should
464 * conform with the first entry in this one.
467 ep
= XFS_BMBT_REC_ADDR(mp
, block
, 1);
469 ASSERT(xfs_bmbt_disk_get_startoff(&last
) +
470 xfs_bmbt_disk_get_blockcount(&last
) <=
471 xfs_bmbt_disk_get_startoff(ep
));
473 for (j
= 1; j
< num_recs
; j
++) {
474 nextp
= XFS_BMBT_REC_ADDR(mp
, block
, j
+ 1);
475 ASSERT(xfs_bmbt_disk_get_startoff(ep
) +
476 xfs_bmbt_disk_get_blockcount(ep
) <=
477 xfs_bmbt_disk_get_startoff(nextp
));
485 xfs_trans_brelse(NULL
, bp
);
489 * If we've reached the end, stop.
491 if (bno
== NULLFSBLOCK
)
495 bp
= xfs_bmap_get_bp(cur
, XFS_FSB_TO_DADDR(mp
, bno
));
498 error
= xfs_btree_read_bufl(mp
, NULL
, bno
, 0, &bp
,
504 block
= XFS_BUF_TO_BLOCK(bp
);
508 xfs_trans_brelse(NULL
, bp
);
513 xfs_warn(mp
, "%s: at error0", __func__
);
515 xfs_trans_brelse(NULL
, bp
);
517 xfs_warn(mp
, "%s: BAD after btree leaves for %d extents",
519 panic("%s: CORRUPTED BTREE OR SOMETHING", __func__
);
524 * Add bmap trace insert entries for all the contents of the extent records.
527 xfs_bmap_trace_exlist(
528 xfs_inode_t
*ip
, /* incore inode pointer */
529 xfs_extnum_t cnt
, /* count of entries in the list */
530 int whichfork
, /* data or attr fork */
531 unsigned long caller_ip
)
533 xfs_extnum_t idx
; /* extent record index */
534 xfs_ifork_t
*ifp
; /* inode fork pointer */
537 if (whichfork
== XFS_ATTR_FORK
)
538 state
|= BMAP_ATTRFORK
;
540 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
541 ASSERT(cnt
== (ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
)));
542 for (idx
= 0; idx
< cnt
; idx
++)
543 trace_xfs_extlist(ip
, idx
, whichfork
, caller_ip
);
547 * Validate that the bmbt_irecs being returned from bmapi are valid
548 * given the caller's original parameters. Specifically check the
549 * ranges of the returned irecs to ensure that they only extend beyond
550 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
553 xfs_bmap_validate_ret(
557 xfs_bmbt_irec_t
*mval
,
561 int i
; /* index to map values */
563 ASSERT(ret_nmap
<= nmap
);
565 for (i
= 0; i
< ret_nmap
; i
++) {
566 ASSERT(mval
[i
].br_blockcount
> 0);
567 if (!(flags
& XFS_BMAPI_ENTIRE
)) {
568 ASSERT(mval
[i
].br_startoff
>= bno
);
569 ASSERT(mval
[i
].br_blockcount
<= len
);
570 ASSERT(mval
[i
].br_startoff
+ mval
[i
].br_blockcount
<=
573 ASSERT(mval
[i
].br_startoff
< bno
+ len
);
574 ASSERT(mval
[i
].br_startoff
+ mval
[i
].br_blockcount
>
578 mval
[i
- 1].br_startoff
+ mval
[i
- 1].br_blockcount
==
579 mval
[i
].br_startoff
);
580 ASSERT(mval
[i
].br_startblock
!= DELAYSTARTBLOCK
&&
581 mval
[i
].br_startblock
!= HOLESTARTBLOCK
);
582 ASSERT(mval
[i
].br_state
== XFS_EXT_NORM
||
583 mval
[i
].br_state
== XFS_EXT_UNWRITTEN
);
588 #define xfs_bmap_check_leaf_extents(cur, ip, whichfork) do { } while (0)
589 #define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap)
593 * bmap free list manipulation functions
597 * Add the extent to the list of extents to be free at transaction end.
598 * The list is maintained sorted (by block number).
602 xfs_fsblock_t bno
, /* fs block number of extent */
603 xfs_filblks_t len
, /* length of extent */
604 xfs_bmap_free_t
*flist
, /* list of extents */
605 xfs_mount_t
*mp
) /* mount point structure */
607 xfs_bmap_free_item_t
*cur
; /* current (next) element */
608 xfs_bmap_free_item_t
*new; /* new element */
609 xfs_bmap_free_item_t
*prev
; /* previous element */
614 ASSERT(bno
!= NULLFSBLOCK
);
616 ASSERT(len
<= MAXEXTLEN
);
617 ASSERT(!isnullstartblock(bno
));
618 agno
= XFS_FSB_TO_AGNO(mp
, bno
);
619 agbno
= XFS_FSB_TO_AGBNO(mp
, bno
);
620 ASSERT(agno
< mp
->m_sb
.sb_agcount
);
621 ASSERT(agbno
< mp
->m_sb
.sb_agblocks
);
622 ASSERT(len
< mp
->m_sb
.sb_agblocks
);
623 ASSERT(agbno
+ len
<= mp
->m_sb
.sb_agblocks
);
625 ASSERT(xfs_bmap_free_item_zone
!= NULL
);
626 new = kmem_zone_alloc(xfs_bmap_free_item_zone
, KM_SLEEP
);
627 new->xbfi_startblock
= bno
;
628 new->xbfi_blockcount
= (xfs_extlen_t
)len
;
629 for (prev
= NULL
, cur
= flist
->xbf_first
;
631 prev
= cur
, cur
= cur
->xbfi_next
) {
632 if (cur
->xbfi_startblock
>= bno
)
636 prev
->xbfi_next
= new;
638 flist
->xbf_first
= new;
639 new->xbfi_next
= cur
;
644 * Remove the entry "free" from the free item list. Prev points to the
645 * previous entry, unless "free" is the head of the list.
649 xfs_bmap_free_t
*flist
, /* free item list header */
650 xfs_bmap_free_item_t
*prev
, /* previous item on list, if any */
651 xfs_bmap_free_item_t
*free
) /* list item to be freed */
654 prev
->xbfi_next
= free
->xbfi_next
;
656 flist
->xbf_first
= free
->xbfi_next
;
658 kmem_zone_free(xfs_bmap_free_item_zone
, free
);
662 * Free up any items left in the list.
666 xfs_bmap_free_t
*flist
) /* list of bmap_free_items */
668 xfs_bmap_free_item_t
*free
; /* free list item */
669 xfs_bmap_free_item_t
*next
;
671 if (flist
->xbf_count
== 0)
673 ASSERT(flist
->xbf_first
!= NULL
);
674 for (free
= flist
->xbf_first
; free
; free
= next
) {
675 next
= free
->xbfi_next
;
676 xfs_bmap_del_free(flist
, NULL
, free
);
678 ASSERT(flist
->xbf_count
== 0);
682 * Inode fork format manipulation functions
686 * Transform a btree format file with only one leaf node, where the
687 * extents list will fit in the inode, into an extents format file.
688 * Since the file extents are already in-core, all we have to do is
689 * give up the space for the btree root and pitch the leaf block.
691 STATIC
int /* error */
692 xfs_bmap_btree_to_extents(
693 xfs_trans_t
*tp
, /* transaction pointer */
694 xfs_inode_t
*ip
, /* incore inode pointer */
695 xfs_btree_cur_t
*cur
, /* btree cursor */
696 int *logflagsp
, /* inode logging flags */
697 int whichfork
) /* data or attr fork */
700 struct xfs_btree_block
*cblock
;/* child btree block */
701 xfs_fsblock_t cbno
; /* child block number */
702 xfs_buf_t
*cbp
; /* child block's buffer */
703 int error
; /* error return value */
704 xfs_ifork_t
*ifp
; /* inode fork data */
705 xfs_mount_t
*mp
; /* mount point structure */
706 __be64
*pp
; /* ptr to block address */
707 struct xfs_btree_block
*rblock
;/* root btree block */
710 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
711 ASSERT(ifp
->if_flags
& XFS_IFEXTENTS
);
712 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
);
713 rblock
= ifp
->if_broot
;
714 ASSERT(be16_to_cpu(rblock
->bb_level
) == 1);
715 ASSERT(be16_to_cpu(rblock
->bb_numrecs
) == 1);
716 ASSERT(xfs_bmbt_maxrecs(mp
, ifp
->if_broot_bytes
, 0) == 1);
717 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, rblock
, 1, ifp
->if_broot_bytes
);
718 cbno
= be64_to_cpu(*pp
);
721 if ((error
= xfs_btree_check_lptr(cur
, cbno
, 1)))
724 error
= xfs_btree_read_bufl(mp
, tp
, cbno
, 0, &cbp
, XFS_BMAP_BTREE_REF
,
728 cblock
= XFS_BUF_TO_BLOCK(cbp
);
729 if ((error
= xfs_btree_check_block(cur
, cblock
, 0, cbp
)))
731 xfs_bmap_add_free(cbno
, 1, cur
->bc_private
.b
.flist
, mp
);
732 ip
->i_d
.di_nblocks
--;
733 xfs_trans_mod_dquot_byino(tp
, ip
, XFS_TRANS_DQ_BCOUNT
, -1L);
734 xfs_trans_binval(tp
, cbp
);
735 if (cur
->bc_bufs
[0] == cbp
)
736 cur
->bc_bufs
[0] = NULL
;
737 xfs_iroot_realloc(ip
, -1, whichfork
);
738 ASSERT(ifp
->if_broot
== NULL
);
739 ASSERT((ifp
->if_flags
& XFS_IFBROOT
) == 0);
740 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_EXTENTS
);
741 *logflagsp
= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
746 * Convert an extents-format file into a btree-format file.
747 * The new file will have a root block (in the inode) and a single child block.
749 STATIC
int /* error */
750 xfs_bmap_extents_to_btree(
751 xfs_trans_t
*tp
, /* transaction pointer */
752 xfs_inode_t
*ip
, /* incore inode pointer */
753 xfs_fsblock_t
*firstblock
, /* first-block-allocated */
754 xfs_bmap_free_t
*flist
, /* blocks freed in xaction */
755 xfs_btree_cur_t
**curp
, /* cursor returned to caller */
756 int wasdel
, /* converting a delayed alloc */
757 int *logflagsp
, /* inode logging flags */
758 int whichfork
) /* data or attr fork */
760 struct xfs_btree_block
*ablock
; /* allocated (child) bt block */
761 xfs_buf_t
*abp
; /* buffer for ablock */
762 xfs_alloc_arg_t args
; /* allocation arguments */
763 xfs_bmbt_rec_t
*arp
; /* child record pointer */
764 struct xfs_btree_block
*block
; /* btree root block */
765 xfs_btree_cur_t
*cur
; /* bmap btree cursor */
766 xfs_bmbt_rec_host_t
*ep
; /* extent record pointer */
767 int error
; /* error return value */
768 xfs_extnum_t i
, cnt
; /* extent record index */
769 xfs_ifork_t
*ifp
; /* inode fork pointer */
770 xfs_bmbt_key_t
*kp
; /* root block key pointer */
771 xfs_mount_t
*mp
; /* mount structure */
772 xfs_extnum_t nextents
; /* number of file extents */
773 xfs_bmbt_ptr_t
*pp
; /* root block address pointer */
776 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
777 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
);
780 * Make space in the inode incore.
782 xfs_iroot_realloc(ip
, 1, whichfork
);
783 ifp
->if_flags
|= XFS_IFBROOT
;
788 block
= ifp
->if_broot
;
789 if (xfs_sb_version_hascrc(&mp
->m_sb
))
790 xfs_btree_init_block_int(mp
, block
, XFS_BUF_DADDR_NULL
,
791 XFS_BMAP_CRC_MAGIC
, 1, 1, ip
->i_ino
,
792 XFS_BTREE_LONG_PTRS
| XFS_BTREE_CRC_BLOCKS
);
794 xfs_btree_init_block_int(mp
, block
, XFS_BUF_DADDR_NULL
,
795 XFS_BMAP_MAGIC
, 1, 1, ip
->i_ino
,
796 XFS_BTREE_LONG_PTRS
);
799 * Need a cursor. Can't allocate until bb_level is filled in.
801 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
802 cur
->bc_private
.b
.firstblock
= *firstblock
;
803 cur
->bc_private
.b
.flist
= flist
;
804 cur
->bc_private
.b
.flags
= wasdel
? XFS_BTCUR_BPRV_WASDEL
: 0;
806 * Convert to a btree with two levels, one record in root.
808 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_BTREE
);
809 memset(&args
, 0, sizeof(args
));
812 args
.firstblock
= *firstblock
;
813 if (*firstblock
== NULLFSBLOCK
) {
814 args
.type
= XFS_ALLOCTYPE_START_BNO
;
815 args
.fsbno
= XFS_INO_TO_FSB(mp
, ip
->i_ino
);
816 } else if (flist
->xbf_low
) {
817 args
.type
= XFS_ALLOCTYPE_START_BNO
;
818 args
.fsbno
= *firstblock
;
820 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
821 args
.fsbno
= *firstblock
;
823 args
.minlen
= args
.maxlen
= args
.prod
= 1;
824 args
.wasdel
= wasdel
;
826 if ((error
= xfs_alloc_vextent(&args
))) {
827 xfs_iroot_realloc(ip
, -1, whichfork
);
828 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
832 * Allocation can't fail, the space was reserved.
834 ASSERT(args
.fsbno
!= NULLFSBLOCK
);
835 ASSERT(*firstblock
== NULLFSBLOCK
||
836 args
.agno
== XFS_FSB_TO_AGNO(mp
, *firstblock
) ||
838 args
.agno
> XFS_FSB_TO_AGNO(mp
, *firstblock
)));
839 *firstblock
= cur
->bc_private
.b
.firstblock
= args
.fsbno
;
840 cur
->bc_private
.b
.allocated
++;
841 ip
->i_d
.di_nblocks
++;
842 xfs_trans_mod_dquot_byino(tp
, ip
, XFS_TRANS_DQ_BCOUNT
, 1L);
843 abp
= xfs_btree_get_bufl(mp
, tp
, args
.fsbno
, 0);
845 * Fill in the child block.
847 abp
->b_ops
= &xfs_bmbt_buf_ops
;
848 ablock
= XFS_BUF_TO_BLOCK(abp
);
849 if (xfs_sb_version_hascrc(&mp
->m_sb
))
850 xfs_btree_init_block_int(mp
, ablock
, abp
->b_bn
,
851 XFS_BMAP_CRC_MAGIC
, 0, 0, ip
->i_ino
,
852 XFS_BTREE_LONG_PTRS
| XFS_BTREE_CRC_BLOCKS
);
854 xfs_btree_init_block_int(mp
, ablock
, abp
->b_bn
,
855 XFS_BMAP_MAGIC
, 0, 0, ip
->i_ino
,
856 XFS_BTREE_LONG_PTRS
);
858 arp
= XFS_BMBT_REC_ADDR(mp
, ablock
, 1);
859 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
860 for (cnt
= i
= 0; i
< nextents
; i
++) {
861 ep
= xfs_iext_get_ext(ifp
, i
);
862 if (!isnullstartblock(xfs_bmbt_get_startblock(ep
))) {
863 arp
->l0
= cpu_to_be64(ep
->l0
);
864 arp
->l1
= cpu_to_be64(ep
->l1
);
868 ASSERT(cnt
== XFS_IFORK_NEXTENTS(ip
, whichfork
));
869 xfs_btree_set_numrecs(ablock
, cnt
);
872 * Fill in the root key and pointer.
874 kp
= XFS_BMBT_KEY_ADDR(mp
, block
, 1);
875 arp
= XFS_BMBT_REC_ADDR(mp
, ablock
, 1);
876 kp
->br_startoff
= cpu_to_be64(xfs_bmbt_disk_get_startoff(arp
));
877 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, xfs_bmbt_get_maxrecs(cur
,
878 be16_to_cpu(block
->bb_level
)));
879 *pp
= cpu_to_be64(args
.fsbno
);
882 * Do all this logging at the end so that
883 * the root is at the right level.
885 xfs_btree_log_block(cur
, abp
, XFS_BB_ALL_BITS
);
886 xfs_btree_log_recs(cur
, abp
, 1, be16_to_cpu(ablock
->bb_numrecs
));
887 ASSERT(*curp
== NULL
);
889 *logflagsp
= XFS_ILOG_CORE
| xfs_ilog_fbroot(whichfork
);
894 * Convert a local file to an extents file.
895 * This code is out of bounds for data forks of regular files,
896 * since the file data needs to get logged so things will stay consistent.
897 * (The bmap-level manipulations are ok, though).
900 xfs_bmap_local_to_extents_empty(
901 struct xfs_inode
*ip
,
904 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
906 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
907 ASSERT(ifp
->if_bytes
== 0);
908 ASSERT(XFS_IFORK_NEXTENTS(ip
, whichfork
) == 0);
910 xfs_bmap_forkoff_reset(ip
->i_mount
, ip
, whichfork
);
911 ifp
->if_flags
&= ~XFS_IFINLINE
;
912 ifp
->if_flags
|= XFS_IFEXTENTS
;
913 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_EXTENTS
);
917 STATIC
int /* error */
918 xfs_bmap_local_to_extents(
919 xfs_trans_t
*tp
, /* transaction pointer */
920 xfs_inode_t
*ip
, /* incore inode pointer */
921 xfs_fsblock_t
*firstblock
, /* first block allocated in xaction */
922 xfs_extlen_t total
, /* total blocks needed by transaction */
923 int *logflagsp
, /* inode logging flags */
925 void (*init_fn
)(struct xfs_trans
*tp
,
927 struct xfs_inode
*ip
,
928 struct xfs_ifork
*ifp
))
931 int flags
; /* logging flags returned */
932 xfs_ifork_t
*ifp
; /* inode fork pointer */
933 xfs_alloc_arg_t args
; /* allocation arguments */
934 xfs_buf_t
*bp
; /* buffer for extent block */
935 xfs_bmbt_rec_host_t
*ep
; /* extent record pointer */
938 * We don't want to deal with the case of keeping inode data inline yet.
939 * So sending the data fork of a regular inode is invalid.
941 ASSERT(!(S_ISREG(ip
->i_d
.di_mode
) && whichfork
== XFS_DATA_FORK
));
942 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
943 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
945 if (!ifp
->if_bytes
) {
946 xfs_bmap_local_to_extents_empty(ip
, whichfork
);
947 flags
= XFS_ILOG_CORE
;
953 ASSERT((ifp
->if_flags
& (XFS_IFINLINE
|XFS_IFEXTENTS
|XFS_IFEXTIREC
)) ==
955 memset(&args
, 0, sizeof(args
));
957 args
.mp
= ip
->i_mount
;
958 args
.firstblock
= *firstblock
;
960 * Allocate a block. We know we need only one, since the
961 * file currently fits in an inode.
963 if (*firstblock
== NULLFSBLOCK
) {
964 args
.fsbno
= XFS_INO_TO_FSB(args
.mp
, ip
->i_ino
);
965 args
.type
= XFS_ALLOCTYPE_START_BNO
;
967 args
.fsbno
= *firstblock
;
968 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
971 args
.minlen
= args
.maxlen
= args
.prod
= 1;
972 error
= xfs_alloc_vextent(&args
);
976 /* Can't fail, the space was reserved. */
977 ASSERT(args
.fsbno
!= NULLFSBLOCK
);
978 ASSERT(args
.len
== 1);
979 *firstblock
= args
.fsbno
;
980 bp
= xfs_btree_get_bufl(args
.mp
, tp
, args
.fsbno
, 0);
982 /* initialise the block and copy the data */
983 init_fn(tp
, bp
, ip
, ifp
);
985 /* account for the change in fork size and log everything */
986 xfs_trans_log_buf(tp
, bp
, 0, ifp
->if_bytes
- 1);
987 xfs_idata_realloc(ip
, -ifp
->if_bytes
, whichfork
);
988 xfs_bmap_local_to_extents_empty(ip
, whichfork
);
989 flags
|= XFS_ILOG_CORE
;
991 xfs_iext_add(ifp
, 0, 1);
992 ep
= xfs_iext_get_ext(ifp
, 0);
993 xfs_bmbt_set_allf(ep
, 0, args
.fsbno
, 1, XFS_EXT_NORM
);
994 trace_xfs_bmap_post_update(ip
, 0,
995 whichfork
== XFS_ATTR_FORK
? BMAP_ATTRFORK
: 0,
997 XFS_IFORK_NEXT_SET(ip
, whichfork
, 1);
998 ip
->i_d
.di_nblocks
= 1;
999 xfs_trans_mod_dquot_byino(tp
, ip
,
1000 XFS_TRANS_DQ_BCOUNT
, 1L);
1001 flags
|= xfs_ilog_fext(whichfork
);
1009 * Called from xfs_bmap_add_attrfork to handle btree format files.
1011 STATIC
int /* error */
1012 xfs_bmap_add_attrfork_btree(
1013 xfs_trans_t
*tp
, /* transaction pointer */
1014 xfs_inode_t
*ip
, /* incore inode pointer */
1015 xfs_fsblock_t
*firstblock
, /* first block allocated */
1016 xfs_bmap_free_t
*flist
, /* blocks to free at commit */
1017 int *flags
) /* inode logging flags */
1019 xfs_btree_cur_t
*cur
; /* btree cursor */
1020 int error
; /* error return value */
1021 xfs_mount_t
*mp
; /* file system mount struct */
1022 int stat
; /* newroot status */
1025 if (ip
->i_df
.if_broot_bytes
<= XFS_IFORK_DSIZE(ip
))
1026 *flags
|= XFS_ILOG_DBROOT
;
1028 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, XFS_DATA_FORK
);
1029 cur
->bc_private
.b
.flist
= flist
;
1030 cur
->bc_private
.b
.firstblock
= *firstblock
;
1031 if ((error
= xfs_bmbt_lookup_ge(cur
, 0, 0, 0, &stat
)))
1033 /* must be at least one entry */
1034 XFS_WANT_CORRUPTED_GOTO(stat
== 1, error0
);
1035 if ((error
= xfs_btree_new_iroot(cur
, flags
, &stat
)))
1038 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
1039 return XFS_ERROR(ENOSPC
);
1041 *firstblock
= cur
->bc_private
.b
.firstblock
;
1042 cur
->bc_private
.b
.allocated
= 0;
1043 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
1047 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
1052 * Called from xfs_bmap_add_attrfork to handle extents format files.
1054 STATIC
int /* error */
1055 xfs_bmap_add_attrfork_extents(
1056 xfs_trans_t
*tp
, /* transaction pointer */
1057 xfs_inode_t
*ip
, /* incore inode pointer */
1058 xfs_fsblock_t
*firstblock
, /* first block allocated */
1059 xfs_bmap_free_t
*flist
, /* blocks to free at commit */
1060 int *flags
) /* inode logging flags */
1062 xfs_btree_cur_t
*cur
; /* bmap btree cursor */
1063 int error
; /* error return value */
1065 if (ip
->i_d
.di_nextents
* sizeof(xfs_bmbt_rec_t
) <= XFS_IFORK_DSIZE(ip
))
1068 error
= xfs_bmap_extents_to_btree(tp
, ip
, firstblock
, flist
, &cur
, 0,
1069 flags
, XFS_DATA_FORK
);
1071 cur
->bc_private
.b
.allocated
= 0;
1072 xfs_btree_del_cursor(cur
,
1073 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
1079 * Called from xfs_bmap_add_attrfork to handle local format files. Each
1080 * different data fork content type needs a different callout to do the
1081 * conversion. Some are basic and only require special block initialisation
1082 * callouts for the data formating, others (directories) are so specialised they
1083 * handle everything themselves.
1085 * XXX (dgc): investigate whether directory conversion can use the generic
1086 * formatting callout. It should be possible - it's just a very complex
1089 STATIC
int /* error */
1090 xfs_bmap_add_attrfork_local(
1091 xfs_trans_t
*tp
, /* transaction pointer */
1092 xfs_inode_t
*ip
, /* incore inode pointer */
1093 xfs_fsblock_t
*firstblock
, /* first block allocated */
1094 xfs_bmap_free_t
*flist
, /* blocks to free at commit */
1095 int *flags
) /* inode logging flags */
1097 xfs_da_args_t dargs
; /* args for dir/attr code */
1099 if (ip
->i_df
.if_bytes
<= XFS_IFORK_DSIZE(ip
))
1102 if (S_ISDIR(ip
->i_d
.di_mode
)) {
1103 memset(&dargs
, 0, sizeof(dargs
));
1105 dargs
.firstblock
= firstblock
;
1106 dargs
.flist
= flist
;
1107 dargs
.total
= ip
->i_mount
->m_dirblkfsbs
;
1108 dargs
.whichfork
= XFS_DATA_FORK
;
1110 return xfs_dir2_sf_to_block(&dargs
);
1113 if (S_ISLNK(ip
->i_d
.di_mode
))
1114 return xfs_bmap_local_to_extents(tp
, ip
, firstblock
, 1,
1115 flags
, XFS_DATA_FORK
,
1116 xfs_symlink_local_to_remote
);
1118 /* should only be called for types that support local format data */
1120 return EFSCORRUPTED
;
1124 * Convert inode from non-attributed to attributed.
1125 * Must not be in a transaction, ip must not be locked.
1127 int /* error code */
1128 xfs_bmap_add_attrfork(
1129 xfs_inode_t
*ip
, /* incore inode pointer */
1130 int size
, /* space new attribute needs */
1131 int rsvd
) /* xact may use reserved blks */
1133 xfs_fsblock_t firstblock
; /* 1st block/ag allocated */
1134 xfs_bmap_free_t flist
; /* freed extent records */
1135 xfs_mount_t
*mp
; /* mount structure */
1136 xfs_trans_t
*tp
; /* transaction pointer */
1137 int blks
; /* space reservation */
1138 int version
= 1; /* superblock attr version */
1139 int committed
; /* xaction was committed */
1140 int logflags
; /* logging flags */
1141 int error
; /* error return value */
1143 ASSERT(XFS_IFORK_Q(ip
) == 0);
1146 ASSERT(!XFS_NOT_DQATTACHED(mp
, ip
));
1147 tp
= xfs_trans_alloc(mp
, XFS_TRANS_ADDAFORK
);
1148 blks
= XFS_ADDAFORK_SPACE_RES(mp
);
1150 tp
->t_flags
|= XFS_TRANS_RESERVE
;
1151 error
= xfs_trans_reserve(tp
, &M_RES(mp
)->tr_addafork
, blks
, 0);
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
);
1159 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
1160 xfs_trans_cancel(tp
, XFS_TRANS_RELEASE_LOG_RES
);
1163 if (XFS_IFORK_Q(ip
))
1165 if (ip
->i_d
.di_aformat
!= XFS_DINODE_FMT_EXTENTS
) {
1167 * For inodes coming from pre-6.2 filesystems.
1169 ASSERT(ip
->i_d
.di_aformat
== 0);
1170 ip
->i_d
.di_aformat
= XFS_DINODE_FMT_EXTENTS
;
1172 ASSERT(ip
->i_d
.di_anextents
== 0);
1174 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
);
1175 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
1177 switch (ip
->i_d
.di_format
) {
1178 case XFS_DINODE_FMT_DEV
:
1179 ip
->i_d
.di_forkoff
= roundup(sizeof(xfs_dev_t
), 8) >> 3;
1181 case XFS_DINODE_FMT_UUID
:
1182 ip
->i_d
.di_forkoff
= roundup(sizeof(uuid_t
), 8) >> 3;
1184 case XFS_DINODE_FMT_LOCAL
:
1185 case XFS_DINODE_FMT_EXTENTS
:
1186 case XFS_DINODE_FMT_BTREE
:
1187 ip
->i_d
.di_forkoff
= xfs_attr_shortform_bytesfit(ip
, size
);
1188 if (!ip
->i_d
.di_forkoff
)
1189 ip
->i_d
.di_forkoff
= xfs_default_attroffset(ip
) >> 3;
1190 else if (mp
->m_flags
& XFS_MOUNT_ATTR2
)
1195 error
= XFS_ERROR(EINVAL
);
1199 ASSERT(ip
->i_afp
== NULL
);
1200 ip
->i_afp
= kmem_zone_zalloc(xfs_ifork_zone
, KM_SLEEP
);
1201 ip
->i_afp
->if_flags
= XFS_IFEXTENTS
;
1203 xfs_bmap_init(&flist
, &firstblock
);
1204 switch (ip
->i_d
.di_format
) {
1205 case XFS_DINODE_FMT_LOCAL
:
1206 error
= xfs_bmap_add_attrfork_local(tp
, ip
, &firstblock
, &flist
,
1209 case XFS_DINODE_FMT_EXTENTS
:
1210 error
= xfs_bmap_add_attrfork_extents(tp
, ip
, &firstblock
,
1213 case XFS_DINODE_FMT_BTREE
:
1214 error
= xfs_bmap_add_attrfork_btree(tp
, ip
, &firstblock
, &flist
,
1222 xfs_trans_log_inode(tp
, ip
, logflags
);
1225 if (!xfs_sb_version_hasattr(&mp
->m_sb
) ||
1226 (!xfs_sb_version_hasattr2(&mp
->m_sb
) && version
== 2)) {
1227 __int64_t sbfields
= 0;
1229 spin_lock(&mp
->m_sb_lock
);
1230 if (!xfs_sb_version_hasattr(&mp
->m_sb
)) {
1231 xfs_sb_version_addattr(&mp
->m_sb
);
1232 sbfields
|= XFS_SB_VERSIONNUM
;
1234 if (!xfs_sb_version_hasattr2(&mp
->m_sb
) && version
== 2) {
1235 xfs_sb_version_addattr2(&mp
->m_sb
);
1236 sbfields
|= (XFS_SB_VERSIONNUM
| XFS_SB_FEATURES2
);
1239 spin_unlock(&mp
->m_sb_lock
);
1240 xfs_mod_sb(tp
, sbfields
);
1242 spin_unlock(&mp
->m_sb_lock
);
1245 error
= xfs_bmap_finish(&tp
, &flist
, &committed
);
1248 return xfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
);
1250 xfs_bmap_cancel(&flist
);
1252 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
1254 xfs_trans_cancel(tp
, XFS_TRANS_RELEASE_LOG_RES
|XFS_TRANS_ABORT
);
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
!= NULLDFSBNO
);
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 XFS_ERROR(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
;
1431 gotp
->br_startblock
= 0xffffa5a5a5a5a5a5LL
;
1433 gotp
->br_startblock
= 0xffffa5a5;
1435 prevp
->br_startoff
= NULLFILEOFF
;
1437 ep
= xfs_iext_bno_to_ext(ifp
, bno
, &lastx
);
1439 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, lastx
- 1), prevp
);
1441 if (lastx
< (ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
))) {
1442 xfs_bmbt_get_all(ep
, gotp
);
1456 * Search the extents list for the inode, for the extent containing bno.
1457 * If bno lies in a hole, point to the next entry. If bno lies past eof,
1458 * *eofp will be set, and *prevp will contain the last entry (null if none).
1459 * Else, *lastxp will be set to the index of the found
1460 * entry; *gotp will contain the entry.
1462 STATIC xfs_bmbt_rec_host_t
* /* pointer to found extent entry */
1463 xfs_bmap_search_extents(
1464 xfs_inode_t
*ip
, /* incore inode pointer */
1465 xfs_fileoff_t bno
, /* block number searched for */
1466 int fork
, /* data or attr fork */
1467 int *eofp
, /* out: end of file found */
1468 xfs_extnum_t
*lastxp
, /* out: last extent index */
1469 xfs_bmbt_irec_t
*gotp
, /* out: extent entry found */
1470 xfs_bmbt_irec_t
*prevp
) /* out: previous extent entry found */
1472 xfs_ifork_t
*ifp
; /* inode fork pointer */
1473 xfs_bmbt_rec_host_t
*ep
; /* extent record pointer */
1475 XFS_STATS_INC(xs_look_exlist
);
1476 ifp
= XFS_IFORK_PTR(ip
, fork
);
1478 ep
= xfs_bmap_search_multi_extents(ifp
, bno
, eofp
, lastxp
, gotp
, prevp
);
1480 if (unlikely(!(gotp
->br_startblock
) && (*lastxp
!= NULLEXTNUM
) &&
1481 !(XFS_IS_REALTIME_INODE(ip
) && fork
== XFS_DATA_FORK
))) {
1482 xfs_alert_tag(ip
->i_mount
, XFS_PTAG_FSBLOCK_ZERO
,
1483 "Access to block zero in inode %llu "
1484 "start_block: %llx start_off: %llx "
1485 "blkcnt: %llx extent-state: %x lastx: %x\n",
1486 (unsigned long long)ip
->i_ino
,
1487 (unsigned long long)gotp
->br_startblock
,
1488 (unsigned long long)gotp
->br_startoff
,
1489 (unsigned long long)gotp
->br_blockcount
,
1490 gotp
->br_state
, *lastxp
);
1491 *lastxp
= NULLEXTNUM
;
1499 * Returns the file-relative block number of the first unused block(s)
1500 * in the file with at least "len" logically contiguous blocks free.
1501 * This is the lowest-address hole if the file has holes, else the first block
1502 * past the end of file.
1503 * Return 0 if the file is currently local (in-inode).
1506 xfs_bmap_first_unused(
1507 xfs_trans_t
*tp
, /* transaction pointer */
1508 xfs_inode_t
*ip
, /* incore inode */
1509 xfs_extlen_t len
, /* size of hole to find */
1510 xfs_fileoff_t
*first_unused
, /* unused block */
1511 int whichfork
) /* data or attr fork */
1513 int error
; /* error return value */
1514 int idx
; /* extent record index */
1515 xfs_ifork_t
*ifp
; /* inode fork pointer */
1516 xfs_fileoff_t lastaddr
; /* last block number seen */
1517 xfs_fileoff_t lowest
; /* lowest useful block */
1518 xfs_fileoff_t max
; /* starting useful block */
1519 xfs_fileoff_t off
; /* offset for this block */
1520 xfs_extnum_t nextents
; /* number of extent entries */
1522 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
||
1523 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
||
1524 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
1525 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
) {
1529 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1530 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
1531 (error
= xfs_iread_extents(tp
, ip
, whichfork
)))
1533 lowest
= *first_unused
;
1534 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
1535 for (idx
= 0, lastaddr
= 0, max
= lowest
; idx
< nextents
; idx
++) {
1536 xfs_bmbt_rec_host_t
*ep
= xfs_iext_get_ext(ifp
, idx
);
1537 off
= xfs_bmbt_get_startoff(ep
);
1539 * See if the hole before this extent will work.
1541 if (off
>= lowest
+ len
&& off
- max
>= len
) {
1542 *first_unused
= max
;
1545 lastaddr
= off
+ xfs_bmbt_get_blockcount(ep
);
1546 max
= XFS_FILEOFF_MAX(lastaddr
, lowest
);
1548 *first_unused
= max
;
1553 * Returns the file-relative block number of the last block - 1 before
1554 * last_block (input value) in the file.
1555 * This is not based on i_size, it is based on the extent records.
1556 * Returns 0 for local files, as they do not have extent records.
1559 xfs_bmap_last_before(
1560 xfs_trans_t
*tp
, /* transaction pointer */
1561 xfs_inode_t
*ip
, /* incore inode */
1562 xfs_fileoff_t
*last_block
, /* last block */
1563 int whichfork
) /* data or attr fork */
1565 xfs_fileoff_t bno
; /* input file offset */
1566 int eof
; /* hit end of file */
1567 xfs_bmbt_rec_host_t
*ep
; /* pointer to last extent */
1568 int error
; /* error return value */
1569 xfs_bmbt_irec_t got
; /* current extent value */
1570 xfs_ifork_t
*ifp
; /* inode fork pointer */
1571 xfs_extnum_t lastx
; /* last extent used */
1572 xfs_bmbt_irec_t prev
; /* previous extent value */
1574 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
&&
1575 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
1576 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_LOCAL
)
1577 return XFS_ERROR(EIO
);
1578 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
) {
1582 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1583 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
1584 (error
= xfs_iread_extents(tp
, ip
, whichfork
)))
1586 bno
= *last_block
- 1;
1587 ep
= xfs_bmap_search_extents(ip
, bno
, whichfork
, &eof
, &lastx
, &got
,
1589 if (eof
|| xfs_bmbt_get_startoff(ep
) > bno
) {
1590 if (prev
.br_startoff
== NULLFILEOFF
)
1593 *last_block
= prev
.br_startoff
+ prev
.br_blockcount
;
1596 * Otherwise *last_block is already the right answer.
1602 xfs_bmap_last_extent(
1603 struct xfs_trans
*tp
,
1604 struct xfs_inode
*ip
,
1606 struct xfs_bmbt_irec
*rec
,
1609 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1613 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
1614 error
= xfs_iread_extents(tp
, ip
, whichfork
);
1619 nextents
= ifp
->if_bytes
/ sizeof(xfs_bmbt_rec_t
);
1620 if (nextents
== 0) {
1625 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, nextents
- 1), rec
);
1631 * Check the last inode extent to determine whether this allocation will result
1632 * in blocks being allocated at the end of the file. When we allocate new data
1633 * blocks at the end of the file which do not start at the previous data block,
1634 * we will try to align the new blocks at stripe unit boundaries.
1636 * Returns 0 in bma->aeof if the file (fork) is empty as any new write will be
1637 * at, or past the EOF.
1641 struct xfs_bmalloca
*bma
,
1644 struct xfs_bmbt_irec rec
;
1649 error
= xfs_bmap_last_extent(NULL
, bma
->ip
, whichfork
, &rec
,
1651 if (error
|| is_empty
)
1655 * Check if we are allocation or past the last extent, or at least into
1656 * the last delayed allocated extent.
1658 bma
->aeof
= bma
->offset
>= rec
.br_startoff
+ rec
.br_blockcount
||
1659 (bma
->offset
>= rec
.br_startoff
&&
1660 isnullstartblock(rec
.br_startblock
));
1665 * Returns the file-relative block number of the first block past eof in
1666 * the file. This is not based on i_size, it is based on the extent records.
1667 * Returns 0 for local files, as they do not have extent records.
1670 xfs_bmap_last_offset(
1671 struct xfs_trans
*tp
,
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
)
1687 return XFS_ERROR(EIO
);
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
)
3320 return XFS_ERROR(EINVAL
);
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
)
3345 return XFS_ERROR(EINVAL
);
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_btalloc_nullfb(
3515 struct xfs_bmalloca
*ap
,
3516 struct xfs_alloc_arg
*args
,
3519 struct xfs_mount
*mp
= ap
->ip
->i_mount
;
3520 struct xfs_perag
*pag
;
3521 xfs_agnumber_t ag
, startag
;
3525 if (ap
->userdata
&& xfs_inode_is_filestream(ap
->ip
))
3526 args
->type
= XFS_ALLOCTYPE_NEAR_BNO
;
3528 args
->type
= XFS_ALLOCTYPE_START_BNO
;
3529 args
->total
= ap
->total
;
3532 * Search for an allocation group with a single extent large enough
3533 * for the request. If one isn't found, then adjust the minimum
3534 * allocation size to the largest space found.
3536 startag
= ag
= XFS_FSB_TO_AGNO(mp
, args
->fsbno
);
3537 if (startag
== NULLAGNUMBER
)
3540 pag
= xfs_perag_get(mp
, ag
);
3541 while (*blen
< args
->maxlen
) {
3542 if (!pag
->pagf_init
) {
3543 error
= xfs_alloc_pagf_init(mp
, args
->tp
, ag
,
3544 XFS_ALLOC_FLAG_TRYLOCK
);
3552 * See xfs_alloc_fix_freelist...
3554 if (pag
->pagf_init
) {
3555 xfs_extlen_t longest
;
3556 longest
= xfs_alloc_longest_free_extent(mp
, pag
);
3557 if (*blen
< longest
)
3562 if (xfs_inode_is_filestream(ap
->ip
)) {
3563 if (*blen
>= args
->maxlen
)
3568 * If startag is an invalid AG, we've
3569 * come here once before and
3570 * xfs_filestream_new_ag picked the
3571 * best currently available.
3573 * Don't continue looping, since we
3574 * could loop forever.
3576 if (startag
== NULLAGNUMBER
)
3579 error
= xfs_filestream_new_ag(ap
, &ag
);
3584 /* loop again to set 'blen'*/
3585 startag
= NULLAGNUMBER
;
3586 pag
= xfs_perag_get(mp
, ag
);
3590 if (++ag
== mp
->m_sb
.sb_agcount
)
3595 pag
= xfs_perag_get(mp
, ag
);
3600 * Since the above loop did a BUF_TRYLOCK, it is
3601 * possible that there is space for this request.
3603 if (notinit
|| *blen
< ap
->minlen
)
3604 args
->minlen
= ap
->minlen
;
3606 * If the best seen length is less than the request
3607 * length, use the best as the minimum.
3609 else if (*blen
< args
->maxlen
)
3610 args
->minlen
= *blen
;
3612 * Otherwise we've seen an extent as big as maxlen,
3613 * use that as the minimum.
3616 args
->minlen
= args
->maxlen
;
3619 * set the failure fallback case to look in the selected
3620 * AG as the stream may have moved.
3622 if (xfs_inode_is_filestream(ap
->ip
))
3623 ap
->blkno
= args
->fsbno
= XFS_AGB_TO_FSB(mp
, ag
, 0);
3630 struct xfs_bmalloca
*ap
) /* bmap alloc argument struct */
3632 xfs_mount_t
*mp
; /* mount point structure */
3633 xfs_alloctype_t atype
= 0; /* type for allocation routines */
3634 xfs_extlen_t align
; /* minimum allocation alignment */
3635 xfs_agnumber_t fb_agno
; /* ag number of ap->firstblock */
3637 xfs_alloc_arg_t args
;
3639 xfs_extlen_t nextminlen
= 0;
3640 int nullfb
; /* true if ap->firstblock isn't set */
3647 mp
= ap
->ip
->i_mount
;
3648 align
= ap
->userdata
? xfs_get_extsz_hint(ap
->ip
) : 0;
3649 if (unlikely(align
)) {
3650 error
= xfs_bmap_extsize_align(mp
, &ap
->got
, &ap
->prev
,
3651 align
, 0, ap
->eof
, 0, ap
->conv
,
3652 &ap
->offset
, &ap
->length
);
3656 nullfb
= *ap
->firstblock
== NULLFSBLOCK
;
3657 fb_agno
= nullfb
? NULLAGNUMBER
: XFS_FSB_TO_AGNO(mp
, *ap
->firstblock
);
3659 if (ap
->userdata
&& xfs_inode_is_filestream(ap
->ip
)) {
3660 ag
= xfs_filestream_lookup_ag(ap
->ip
);
3661 ag
= (ag
!= NULLAGNUMBER
) ? ag
: 0;
3662 ap
->blkno
= XFS_AGB_TO_FSB(mp
, ag
, 0);
3664 ap
->blkno
= XFS_INO_TO_FSB(mp
, ap
->ip
->i_ino
);
3667 ap
->blkno
= *ap
->firstblock
;
3669 xfs_bmap_adjacent(ap
);
3672 * If allowed, use ap->blkno; otherwise must use firstblock since
3673 * it's in the right allocation group.
3675 if (nullfb
|| XFS_FSB_TO_AGNO(mp
, ap
->blkno
) == fb_agno
)
3678 ap
->blkno
= *ap
->firstblock
;
3680 * Normal allocation, done through xfs_alloc_vextent.
3682 tryagain
= isaligned
= 0;
3683 memset(&args
, 0, sizeof(args
));
3686 args
.fsbno
= ap
->blkno
;
3688 /* Trim the allocation back to the maximum an AG can fit. */
3689 args
.maxlen
= MIN(ap
->length
, XFS_ALLOC_AG_MAX_USABLE(mp
));
3690 args
.firstblock
= *ap
->firstblock
;
3693 error
= xfs_bmap_btalloc_nullfb(ap
, &args
, &blen
);
3696 } else if (ap
->flist
->xbf_low
) {
3697 if (xfs_inode_is_filestream(ap
->ip
))
3698 args
.type
= XFS_ALLOCTYPE_FIRST_AG
;
3700 args
.type
= XFS_ALLOCTYPE_START_BNO
;
3701 args
.total
= args
.minlen
= ap
->minlen
;
3703 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
3704 args
.total
= ap
->total
;
3705 args
.minlen
= ap
->minlen
;
3707 /* apply extent size hints if obtained earlier */
3708 if (unlikely(align
)) {
3710 if ((args
.mod
= (xfs_extlen_t
)do_mod(ap
->offset
, args
.prod
)))
3711 args
.mod
= (xfs_extlen_t
)(args
.prod
- args
.mod
);
3712 } else if (mp
->m_sb
.sb_blocksize
>= PAGE_CACHE_SIZE
) {
3716 args
.prod
= PAGE_CACHE_SIZE
>> mp
->m_sb
.sb_blocklog
;
3717 if ((args
.mod
= (xfs_extlen_t
)(do_mod(ap
->offset
, args
.prod
))))
3718 args
.mod
= (xfs_extlen_t
)(args
.prod
- args
.mod
);
3721 * If we are not low on available data blocks, and the
3722 * underlying logical volume manager is a stripe, and
3723 * the file offset is zero then try to allocate data
3724 * blocks on stripe unit boundary.
3725 * NOTE: ap->aeof is only set if the allocation length
3726 * is >= the stripe unit and the allocation offset is
3727 * at the end of file.
3729 if (!ap
->flist
->xbf_low
&& ap
->aeof
) {
3731 args
.alignment
= mp
->m_dalign
;
3735 * Adjust for alignment
3737 if (blen
> args
.alignment
&& blen
<= args
.maxlen
)
3738 args
.minlen
= blen
- args
.alignment
;
3739 args
.minalignslop
= 0;
3742 * First try an exact bno allocation.
3743 * If it fails then do a near or start bno
3744 * allocation with alignment turned on.
3748 args
.type
= XFS_ALLOCTYPE_THIS_BNO
;
3751 * Compute the minlen+alignment for the
3752 * next case. Set slop so that the value
3753 * of minlen+alignment+slop doesn't go up
3754 * between the calls.
3756 if (blen
> mp
->m_dalign
&& blen
<= args
.maxlen
)
3757 nextminlen
= blen
- mp
->m_dalign
;
3759 nextminlen
= args
.minlen
;
3760 if (nextminlen
+ mp
->m_dalign
> args
.minlen
+ 1)
3762 nextminlen
+ mp
->m_dalign
-
3765 args
.minalignslop
= 0;
3769 args
.minalignslop
= 0;
3771 args
.minleft
= ap
->minleft
;
3772 args
.wasdel
= ap
->wasdel
;
3774 args
.userdata
= ap
->userdata
;
3775 if ((error
= xfs_alloc_vextent(&args
)))
3777 if (tryagain
&& args
.fsbno
== NULLFSBLOCK
) {
3779 * Exact allocation failed. Now try with alignment
3783 args
.fsbno
= ap
->blkno
;
3784 args
.alignment
= mp
->m_dalign
;
3785 args
.minlen
= nextminlen
;
3786 args
.minalignslop
= 0;
3788 if ((error
= xfs_alloc_vextent(&args
)))
3791 if (isaligned
&& args
.fsbno
== NULLFSBLOCK
) {
3793 * allocation failed, so turn off alignment and
3797 args
.fsbno
= ap
->blkno
;
3799 if ((error
= xfs_alloc_vextent(&args
)))
3802 if (args
.fsbno
== NULLFSBLOCK
&& nullfb
&&
3803 args
.minlen
> ap
->minlen
) {
3804 args
.minlen
= ap
->minlen
;
3805 args
.type
= XFS_ALLOCTYPE_START_BNO
;
3806 args
.fsbno
= ap
->blkno
;
3807 if ((error
= xfs_alloc_vextent(&args
)))
3810 if (args
.fsbno
== NULLFSBLOCK
&& nullfb
) {
3812 args
.type
= XFS_ALLOCTYPE_FIRST_AG
;
3813 args
.total
= ap
->minlen
;
3815 if ((error
= xfs_alloc_vextent(&args
)))
3817 ap
->flist
->xbf_low
= 1;
3819 if (args
.fsbno
!= NULLFSBLOCK
) {
3821 * check the allocation happened at the same or higher AG than
3822 * the first block that was allocated.
3824 ASSERT(*ap
->firstblock
== NULLFSBLOCK
||
3825 XFS_FSB_TO_AGNO(mp
, *ap
->firstblock
) ==
3826 XFS_FSB_TO_AGNO(mp
, args
.fsbno
) ||
3827 (ap
->flist
->xbf_low
&&
3828 XFS_FSB_TO_AGNO(mp
, *ap
->firstblock
) <
3829 XFS_FSB_TO_AGNO(mp
, args
.fsbno
)));
3831 ap
->blkno
= args
.fsbno
;
3832 if (*ap
->firstblock
== NULLFSBLOCK
)
3833 *ap
->firstblock
= args
.fsbno
;
3834 ASSERT(nullfb
|| fb_agno
== args
.agno
||
3835 (ap
->flist
->xbf_low
&& fb_agno
< args
.agno
));
3836 ap
->length
= args
.len
;
3837 ap
->ip
->i_d
.di_nblocks
+= args
.len
;
3838 xfs_trans_log_inode(ap
->tp
, ap
->ip
, XFS_ILOG_CORE
);
3840 ap
->ip
->i_delayed_blks
-= args
.len
;
3842 * Adjust the disk quota also. This was reserved
3845 xfs_trans_mod_dquot_byino(ap
->tp
, ap
->ip
,
3846 ap
->wasdel
? XFS_TRANS_DQ_DELBCOUNT
:
3847 XFS_TRANS_DQ_BCOUNT
,
3850 ap
->blkno
= NULLFSBLOCK
;
3857 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
3858 * It figures out where to ask the underlying allocator to put the new extent.
3862 struct xfs_bmalloca
*ap
) /* bmap alloc argument struct */
3864 if (XFS_IS_REALTIME_INODE(ap
->ip
) && ap
->userdata
)
3865 return xfs_bmap_rtalloc(ap
);
3866 return xfs_bmap_btalloc(ap
);
3870 * Trim the returned map to the required bounds
3874 struct xfs_bmbt_irec
*mval
,
3875 struct xfs_bmbt_irec
*got
,
3883 if ((flags
& XFS_BMAPI_ENTIRE
) ||
3884 got
->br_startoff
+ got
->br_blockcount
<= obno
) {
3886 if (isnullstartblock(got
->br_startblock
))
3887 mval
->br_startblock
= DELAYSTARTBLOCK
;
3893 ASSERT((*bno
>= obno
) || (n
== 0));
3895 mval
->br_startoff
= *bno
;
3896 if (isnullstartblock(got
->br_startblock
))
3897 mval
->br_startblock
= DELAYSTARTBLOCK
;
3899 mval
->br_startblock
= got
->br_startblock
+
3900 (*bno
- got
->br_startoff
);
3902 * Return the minimum of what we got and what we asked for for
3903 * the length. We can use the len variable here because it is
3904 * modified below and we could have been there before coming
3905 * here if the first part of the allocation didn't overlap what
3908 mval
->br_blockcount
= XFS_FILBLKS_MIN(end
- *bno
,
3909 got
->br_blockcount
- (*bno
- got
->br_startoff
));
3910 mval
->br_state
= got
->br_state
;
3911 ASSERT(mval
->br_blockcount
<= len
);
3916 * Update and validate the extent map to return
3919 xfs_bmapi_update_map(
3920 struct xfs_bmbt_irec
**map
,
3928 xfs_bmbt_irec_t
*mval
= *map
;
3930 ASSERT((flags
& XFS_BMAPI_ENTIRE
) ||
3931 ((mval
->br_startoff
+ mval
->br_blockcount
) <= end
));
3932 ASSERT((flags
& XFS_BMAPI_ENTIRE
) || (mval
->br_blockcount
<= *len
) ||
3933 (mval
->br_startoff
< obno
));
3935 *bno
= mval
->br_startoff
+ mval
->br_blockcount
;
3937 if (*n
> 0 && mval
->br_startoff
== mval
[-1].br_startoff
) {
3938 /* update previous map with new information */
3939 ASSERT(mval
->br_startblock
== mval
[-1].br_startblock
);
3940 ASSERT(mval
->br_blockcount
> mval
[-1].br_blockcount
);
3941 ASSERT(mval
->br_state
== mval
[-1].br_state
);
3942 mval
[-1].br_blockcount
= mval
->br_blockcount
;
3943 mval
[-1].br_state
= mval
->br_state
;
3944 } else if (*n
> 0 && mval
->br_startblock
!= DELAYSTARTBLOCK
&&
3945 mval
[-1].br_startblock
!= DELAYSTARTBLOCK
&&
3946 mval
[-1].br_startblock
!= HOLESTARTBLOCK
&&
3947 mval
->br_startblock
== mval
[-1].br_startblock
+
3948 mval
[-1].br_blockcount
&&
3949 ((flags
& XFS_BMAPI_IGSTATE
) ||
3950 mval
[-1].br_state
== mval
->br_state
)) {
3951 ASSERT(mval
->br_startoff
==
3952 mval
[-1].br_startoff
+ mval
[-1].br_blockcount
);
3953 mval
[-1].br_blockcount
+= mval
->br_blockcount
;
3954 } else if (*n
> 0 &&
3955 mval
->br_startblock
== DELAYSTARTBLOCK
&&
3956 mval
[-1].br_startblock
== DELAYSTARTBLOCK
&&
3957 mval
->br_startoff
==
3958 mval
[-1].br_startoff
+ mval
[-1].br_blockcount
) {
3959 mval
[-1].br_blockcount
+= mval
->br_blockcount
;
3960 mval
[-1].br_state
= mval
->br_state
;
3961 } else if (!((*n
== 0) &&
3962 ((mval
->br_startoff
+ mval
->br_blockcount
) <=
3971 * Map file blocks to filesystem blocks without allocation.
3975 struct xfs_inode
*ip
,
3978 struct xfs_bmbt_irec
*mval
,
3982 struct xfs_mount
*mp
= ip
->i_mount
;
3983 struct xfs_ifork
*ifp
;
3984 struct xfs_bmbt_irec got
;
3985 struct xfs_bmbt_irec prev
;
3992 int whichfork
= (flags
& XFS_BMAPI_ATTRFORK
) ?
3993 XFS_ATTR_FORK
: XFS_DATA_FORK
;
3996 ASSERT(!(flags
& ~(XFS_BMAPI_ATTRFORK
|XFS_BMAPI_ENTIRE
|
3997 XFS_BMAPI_IGSTATE
)));
3999 if (unlikely(XFS_TEST_ERROR(
4000 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
4001 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
4002 mp
, XFS_ERRTAG_BMAPIFORMAT
, XFS_RANDOM_BMAPIFORMAT
))) {
4003 XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW
, mp
);
4004 return XFS_ERROR(EFSCORRUPTED
);
4007 if (XFS_FORCED_SHUTDOWN(mp
))
4008 return XFS_ERROR(EIO
);
4010 XFS_STATS_INC(xs_blk_mapr
);
4012 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4014 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
4015 error
= xfs_iread_extents(NULL
, ip
, whichfork
);
4020 xfs_bmap_search_extents(ip
, bno
, whichfork
, &eof
, &lastx
, &got
, &prev
);
4024 while (bno
< end
&& n
< *nmap
) {
4025 /* Reading past eof, act as though there's a hole up to end. */
4027 got
.br_startoff
= end
;
4028 if (got
.br_startoff
> bno
) {
4029 /* Reading in a hole. */
4030 mval
->br_startoff
= bno
;
4031 mval
->br_startblock
= HOLESTARTBLOCK
;
4032 mval
->br_blockcount
=
4033 XFS_FILBLKS_MIN(len
, got
.br_startoff
- bno
);
4034 mval
->br_state
= XFS_EXT_NORM
;
4035 bno
+= mval
->br_blockcount
;
4036 len
-= mval
->br_blockcount
;
4042 /* set up the extent map to return. */
4043 xfs_bmapi_trim_map(mval
, &got
, &bno
, len
, obno
, end
, n
, flags
);
4044 xfs_bmapi_update_map(&mval
, &bno
, &len
, obno
, end
, &n
, flags
);
4046 /* If we're done, stop now. */
4047 if (bno
>= end
|| n
>= *nmap
)
4050 /* Else go on to the next record. */
4051 if (++lastx
< ifp
->if_bytes
/ sizeof(xfs_bmbt_rec_t
))
4052 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, lastx
), &got
);
4061 xfs_bmapi_reserve_delalloc(
4062 struct xfs_inode
*ip
,
4065 struct xfs_bmbt_irec
*got
,
4066 struct xfs_bmbt_irec
*prev
,
4067 xfs_extnum_t
*lastx
,
4070 struct xfs_mount
*mp
= ip
->i_mount
;
4071 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, XFS_DATA_FORK
);
4073 xfs_extlen_t indlen
;
4074 char rt
= XFS_IS_REALTIME_INODE(ip
);
4078 alen
= XFS_FILBLKS_MIN(len
, MAXEXTLEN
);
4080 alen
= XFS_FILBLKS_MIN(alen
, got
->br_startoff
- aoff
);
4082 /* Figure out the extent size, adjust alen */
4083 extsz
= xfs_get_extsz_hint(ip
);
4086 * Make sure we don't exceed a single extent length when we
4087 * align the extent by reducing length we are going to
4088 * allocate by the maximum amount extent size aligment may
4091 alen
= XFS_FILBLKS_MIN(len
, MAXEXTLEN
- (2 * extsz
- 1));
4092 error
= xfs_bmap_extsize_align(mp
, got
, prev
, extsz
, rt
, eof
,
4093 1, 0, &aoff
, &alen
);
4098 extsz
= alen
/ mp
->m_sb
.sb_rextsize
;
4101 * Make a transaction-less quota reservation for delayed allocation
4102 * blocks. This number gets adjusted later. We return if we haven't
4103 * allocated blocks already inside this loop.
4105 error
= xfs_trans_reserve_quota_nblks(NULL
, ip
, (long)alen
, 0,
4106 rt
? XFS_QMOPT_RES_RTBLKS
: XFS_QMOPT_RES_REGBLKS
);
4111 * Split changing sb for alen and indlen since they could be coming
4112 * from different places.
4114 indlen
= (xfs_extlen_t
)xfs_bmap_worst_indlen(ip
, alen
);
4118 error
= xfs_mod_incore_sb(mp
, XFS_SBS_FREXTENTS
,
4119 -((int64_t)extsz
), 0);
4121 error
= xfs_icsb_modify_counters(mp
, XFS_SBS_FDBLOCKS
,
4122 -((int64_t)alen
), 0);
4126 goto out_unreserve_quota
;
4128 error
= xfs_icsb_modify_counters(mp
, XFS_SBS_FDBLOCKS
,
4129 -((int64_t)indlen
), 0);
4131 goto out_unreserve_blocks
;
4134 ip
->i_delayed_blks
+= alen
;
4136 got
->br_startoff
= aoff
;
4137 got
->br_startblock
= nullstartblock(indlen
);
4138 got
->br_blockcount
= alen
;
4139 got
->br_state
= XFS_EXT_NORM
;
4140 xfs_bmap_add_extent_hole_delay(ip
, lastx
, got
);
4143 * Update our extent pointer, given that xfs_bmap_add_extent_hole_delay
4144 * might have merged it into one of the neighbouring ones.
4146 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, *lastx
), got
);
4148 ASSERT(got
->br_startoff
<= aoff
);
4149 ASSERT(got
->br_startoff
+ got
->br_blockcount
>= aoff
+ alen
);
4150 ASSERT(isnullstartblock(got
->br_startblock
));
4151 ASSERT(got
->br_state
== XFS_EXT_NORM
);
4154 out_unreserve_blocks
:
4156 xfs_mod_incore_sb(mp
, XFS_SBS_FREXTENTS
, extsz
, 0);
4158 xfs_icsb_modify_counters(mp
, XFS_SBS_FDBLOCKS
, alen
, 0);
4159 out_unreserve_quota
:
4160 if (XFS_IS_QUOTA_ON(mp
))
4161 xfs_trans_unreserve_quota_nblks(NULL
, ip
, (long)alen
, 0, rt
?
4162 XFS_QMOPT_RES_RTBLKS
: XFS_QMOPT_RES_REGBLKS
);
4167 * Map file blocks to filesystem blocks, adding delayed allocations as needed.
4171 struct xfs_inode
*ip
, /* incore inode */
4172 xfs_fileoff_t bno
, /* starting file offs. mapped */
4173 xfs_filblks_t len
, /* length to map in file */
4174 struct xfs_bmbt_irec
*mval
, /* output: map values */
4175 int *nmap
, /* i/o: mval size/count */
4176 int flags
) /* XFS_BMAPI_... */
4178 struct xfs_mount
*mp
= ip
->i_mount
;
4179 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, XFS_DATA_FORK
);
4180 struct xfs_bmbt_irec got
; /* current file extent record */
4181 struct xfs_bmbt_irec prev
; /* previous file extent record */
4182 xfs_fileoff_t obno
; /* old block number (offset) */
4183 xfs_fileoff_t end
; /* end of mapped file region */
4184 xfs_extnum_t lastx
; /* last useful extent number */
4185 int eof
; /* we've hit the end of extents */
4186 int n
= 0; /* current extent index */
4190 ASSERT(*nmap
<= XFS_BMAP_MAX_NMAP
);
4191 ASSERT(!(flags
& ~XFS_BMAPI_ENTIRE
));
4193 if (unlikely(XFS_TEST_ERROR(
4194 (XFS_IFORK_FORMAT(ip
, XFS_DATA_FORK
) != XFS_DINODE_FMT_EXTENTS
&&
4195 XFS_IFORK_FORMAT(ip
, XFS_DATA_FORK
) != XFS_DINODE_FMT_BTREE
),
4196 mp
, XFS_ERRTAG_BMAPIFORMAT
, XFS_RANDOM_BMAPIFORMAT
))) {
4197 XFS_ERROR_REPORT("xfs_bmapi_delay", XFS_ERRLEVEL_LOW
, mp
);
4198 return XFS_ERROR(EFSCORRUPTED
);
4201 if (XFS_FORCED_SHUTDOWN(mp
))
4202 return XFS_ERROR(EIO
);
4204 XFS_STATS_INC(xs_blk_mapw
);
4206 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
4207 error
= xfs_iread_extents(NULL
, ip
, XFS_DATA_FORK
);
4212 xfs_bmap_search_extents(ip
, bno
, XFS_DATA_FORK
, &eof
, &lastx
, &got
, &prev
);
4216 while (bno
< end
&& n
< *nmap
) {
4217 if (eof
|| got
.br_startoff
> bno
) {
4218 error
= xfs_bmapi_reserve_delalloc(ip
, bno
, len
, &got
,
4219 &prev
, &lastx
, eof
);
4229 /* set up the extent map to return. */
4230 xfs_bmapi_trim_map(mval
, &got
, &bno
, len
, obno
, end
, n
, flags
);
4231 xfs_bmapi_update_map(&mval
, &bno
, &len
, obno
, end
, &n
, flags
);
4233 /* If we're done, stop now. */
4234 if (bno
>= end
|| n
>= *nmap
)
4237 /* Else go on to the next record. */
4239 if (++lastx
< ifp
->if_bytes
/ sizeof(xfs_bmbt_rec_t
))
4240 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, lastx
), &got
);
4251 __xfs_bmapi_allocate(
4252 struct xfs_bmalloca
*bma
)
4254 struct xfs_mount
*mp
= bma
->ip
->i_mount
;
4255 int whichfork
= (bma
->flags
& XFS_BMAPI_ATTRFORK
) ?
4256 XFS_ATTR_FORK
: XFS_DATA_FORK
;
4257 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
4258 int tmp_logflags
= 0;
4261 ASSERT(bma
->length
> 0);
4264 * For the wasdelay case, we could also just allocate the stuff asked
4265 * for in this bmap call but that wouldn't be as good.
4268 bma
->length
= (xfs_extlen_t
)bma
->got
.br_blockcount
;
4269 bma
->offset
= bma
->got
.br_startoff
;
4270 if (bma
->idx
!= NULLEXTNUM
&& bma
->idx
) {
4271 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
- 1),
4275 bma
->length
= XFS_FILBLKS_MIN(bma
->length
, MAXEXTLEN
);
4277 bma
->length
= XFS_FILBLKS_MIN(bma
->length
,
4278 bma
->got
.br_startoff
- bma
->offset
);
4282 * Indicate if this is the first user data in the file, or just any
4285 if (!(bma
->flags
& XFS_BMAPI_METADATA
)) {
4286 bma
->userdata
= (bma
->offset
== 0) ?
4287 XFS_ALLOC_INITIAL_USER_DATA
: XFS_ALLOC_USERDATA
;
4290 bma
->minlen
= (bma
->flags
& XFS_BMAPI_CONTIG
) ? bma
->length
: 1;
4293 * Only want to do the alignment at the eof if it is userdata and
4294 * allocation length is larger than a stripe unit.
4296 if (mp
->m_dalign
&& bma
->length
>= mp
->m_dalign
&&
4297 !(bma
->flags
& XFS_BMAPI_METADATA
) && whichfork
== XFS_DATA_FORK
) {
4298 error
= xfs_bmap_isaeof(bma
, whichfork
);
4303 error
= xfs_bmap_alloc(bma
);
4307 if (bma
->flist
->xbf_low
)
4310 bma
->cur
->bc_private
.b
.firstblock
= *bma
->firstblock
;
4311 if (bma
->blkno
== NULLFSBLOCK
)
4313 if ((ifp
->if_flags
& XFS_IFBROOT
) && !bma
->cur
) {
4314 bma
->cur
= xfs_bmbt_init_cursor(mp
, bma
->tp
, bma
->ip
, whichfork
);
4315 bma
->cur
->bc_private
.b
.firstblock
= *bma
->firstblock
;
4316 bma
->cur
->bc_private
.b
.flist
= bma
->flist
;
4319 * Bump the number of extents we've allocated
4325 bma
->cur
->bc_private
.b
.flags
=
4326 bma
->wasdel
? XFS_BTCUR_BPRV_WASDEL
: 0;
4328 bma
->got
.br_startoff
= bma
->offset
;
4329 bma
->got
.br_startblock
= bma
->blkno
;
4330 bma
->got
.br_blockcount
= bma
->length
;
4331 bma
->got
.br_state
= XFS_EXT_NORM
;
4334 * A wasdelay extent has been initialized, so shouldn't be flagged
4337 if (!bma
->wasdel
&& (bma
->flags
& XFS_BMAPI_PREALLOC
) &&
4338 xfs_sb_version_hasextflgbit(&mp
->m_sb
))
4339 bma
->got
.br_state
= XFS_EXT_UNWRITTEN
;
4342 error
= xfs_bmap_add_extent_delay_real(bma
);
4344 error
= xfs_bmap_add_extent_hole_real(bma
, whichfork
);
4346 bma
->logflags
|= tmp_logflags
;
4351 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4352 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4353 * the neighbouring ones.
4355 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
), &bma
->got
);
4357 ASSERT(bma
->got
.br_startoff
<= bma
->offset
);
4358 ASSERT(bma
->got
.br_startoff
+ bma
->got
.br_blockcount
>=
4359 bma
->offset
+ bma
->length
);
4360 ASSERT(bma
->got
.br_state
== XFS_EXT_NORM
||
4361 bma
->got
.br_state
== XFS_EXT_UNWRITTEN
);
4366 xfs_bmapi_convert_unwritten(
4367 struct xfs_bmalloca
*bma
,
4368 struct xfs_bmbt_irec
*mval
,
4372 int whichfork
= (flags
& XFS_BMAPI_ATTRFORK
) ?
4373 XFS_ATTR_FORK
: XFS_DATA_FORK
;
4374 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
4375 int tmp_logflags
= 0;
4378 /* check if we need to do unwritten->real conversion */
4379 if (mval
->br_state
== XFS_EXT_UNWRITTEN
&&
4380 (flags
& XFS_BMAPI_PREALLOC
))
4383 /* check if we need to do real->unwritten conversion */
4384 if (mval
->br_state
== XFS_EXT_NORM
&&
4385 (flags
& (XFS_BMAPI_PREALLOC
| XFS_BMAPI_CONVERT
)) !=
4386 (XFS_BMAPI_PREALLOC
| XFS_BMAPI_CONVERT
))
4390 * Modify (by adding) the state flag, if writing.
4392 ASSERT(mval
->br_blockcount
<= len
);
4393 if ((ifp
->if_flags
& XFS_IFBROOT
) && !bma
->cur
) {
4394 bma
->cur
= xfs_bmbt_init_cursor(bma
->ip
->i_mount
, bma
->tp
,
4395 bma
->ip
, whichfork
);
4396 bma
->cur
->bc_private
.b
.firstblock
= *bma
->firstblock
;
4397 bma
->cur
->bc_private
.b
.flist
= bma
->flist
;
4399 mval
->br_state
= (mval
->br_state
== XFS_EXT_UNWRITTEN
)
4400 ? XFS_EXT_NORM
: XFS_EXT_UNWRITTEN
;
4402 error
= xfs_bmap_add_extent_unwritten_real(bma
->tp
, bma
->ip
, &bma
->idx
,
4403 &bma
->cur
, mval
, bma
->firstblock
, bma
->flist
,
4405 bma
->logflags
|= tmp_logflags
;
4410 * Update our extent pointer, given that
4411 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4412 * of the neighbouring ones.
4414 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
), &bma
->got
);
4417 * We may have combined previously unwritten space with written space,
4418 * so generate another request.
4420 if (mval
->br_blockcount
< len
)
4426 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4427 * extent state if necessary. Details behaviour is controlled by the flags
4428 * parameter. Only allocates blocks from a single allocation group, to avoid
4431 * The returned value in "firstblock" from the first call in a transaction
4432 * must be remembered and presented to subsequent calls in "firstblock".
4433 * An upper bound for the number of blocks to be allocated is supplied to
4434 * the first call in "total"; if no allocation group has that many free
4435 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4439 struct xfs_trans
*tp
, /* transaction pointer */
4440 struct xfs_inode
*ip
, /* incore inode */
4441 xfs_fileoff_t bno
, /* starting file offs. mapped */
4442 xfs_filblks_t len
, /* length to map in file */
4443 int flags
, /* XFS_BMAPI_... */
4444 xfs_fsblock_t
*firstblock
, /* first allocated block
4445 controls a.g. for allocs */
4446 xfs_extlen_t total
, /* total blocks needed */
4447 struct xfs_bmbt_irec
*mval
, /* output: map values */
4448 int *nmap
, /* i/o: mval size/count */
4449 struct xfs_bmap_free
*flist
) /* i/o: list extents to free */
4451 struct xfs_mount
*mp
= ip
->i_mount
;
4452 struct xfs_ifork
*ifp
;
4453 struct xfs_bmalloca bma
= { NULL
}; /* args for xfs_bmap_alloc */
4454 xfs_fileoff_t end
; /* end of mapped file region */
4455 int eof
; /* after the end of extents */
4456 int error
; /* error return */
4457 int n
; /* current extent index */
4458 xfs_fileoff_t obno
; /* old block number (offset) */
4459 int whichfork
; /* data or attr fork */
4460 char inhole
; /* current location is hole in file */
4461 char wasdelay
; /* old extent was delayed */
4464 xfs_fileoff_t orig_bno
; /* original block number value */
4465 int orig_flags
; /* original flags arg value */
4466 xfs_filblks_t orig_len
; /* original value of len arg */
4467 struct xfs_bmbt_irec
*orig_mval
; /* original value of mval */
4468 int orig_nmap
; /* original value of *nmap */
4476 whichfork
= (flags
& XFS_BMAPI_ATTRFORK
) ?
4477 XFS_ATTR_FORK
: XFS_DATA_FORK
;
4480 ASSERT(*nmap
<= XFS_BMAP_MAX_NMAP
);
4481 ASSERT(!(flags
& XFS_BMAPI_IGSTATE
));
4484 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_LOCAL
);
4486 if (unlikely(XFS_TEST_ERROR(
4487 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
4488 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
4489 mp
, XFS_ERRTAG_BMAPIFORMAT
, XFS_RANDOM_BMAPIFORMAT
))) {
4490 XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW
, mp
);
4491 return XFS_ERROR(EFSCORRUPTED
);
4494 if (XFS_FORCED_SHUTDOWN(mp
))
4495 return XFS_ERROR(EIO
);
4497 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4499 XFS_STATS_INC(xs_blk_mapw
);
4501 if (*firstblock
== NULLFSBLOCK
) {
4502 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
)
4503 bma
.minleft
= be16_to_cpu(ifp
->if_broot
->bb_level
) + 1;
4510 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
4511 error
= xfs_iread_extents(tp
, ip
, whichfork
);
4516 xfs_bmap_search_extents(ip
, bno
, whichfork
, &eof
, &bma
.idx
, &bma
.got
,
4527 bma
.firstblock
= firstblock
;
4529 if (flags
& XFS_BMAPI_STACK_SWITCH
)
4530 bma
.stack_switch
= 1;
4532 while (bno
< end
&& n
< *nmap
) {
4533 inhole
= eof
|| bma
.got
.br_startoff
> bno
;
4534 wasdelay
= !inhole
&& isnullstartblock(bma
.got
.br_startblock
);
4537 * First, deal with the hole before the allocated space
4538 * that we found, if any.
4540 if (inhole
|| wasdelay
) {
4542 bma
.conv
= !!(flags
& XFS_BMAPI_CONVERT
);
4543 bma
.wasdel
= wasdelay
;
4548 * There's a 32/64 bit type mismatch between the
4549 * allocation length request (which can be 64 bits in
4550 * length) and the bma length request, which is
4551 * xfs_extlen_t and therefore 32 bits. Hence we have to
4552 * check for 32-bit overflows and handle them here.
4554 if (len
> (xfs_filblks_t
)MAXEXTLEN
)
4555 bma
.length
= MAXEXTLEN
;
4560 ASSERT(bma
.length
> 0);
4561 error
= xfs_bmapi_allocate(&bma
);
4564 if (bma
.blkno
== NULLFSBLOCK
)
4568 /* Deal with the allocated space we found. */
4569 xfs_bmapi_trim_map(mval
, &bma
.got
, &bno
, len
, obno
,
4572 /* Execute unwritten extent conversion if necessary */
4573 error
= xfs_bmapi_convert_unwritten(&bma
, mval
, len
, flags
);
4574 if (error
== EAGAIN
)
4579 /* update the extent map to return */
4580 xfs_bmapi_update_map(&mval
, &bno
, &len
, obno
, end
, &n
, flags
);
4583 * If we're done, stop now. Stop when we've allocated
4584 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4585 * the transaction may get too big.
4587 if (bno
>= end
|| n
>= *nmap
|| bma
.nallocs
>= *nmap
)
4590 /* Else go on to the next record. */
4592 if (++bma
.idx
< ifp
->if_bytes
/ sizeof(xfs_bmbt_rec_t
)) {
4593 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
.idx
),
4601 * Transform from btree to extents, give it cur.
4603 if (xfs_bmap_wants_extents(ip
, whichfork
)) {
4604 int tmp_logflags
= 0;
4607 error
= xfs_bmap_btree_to_extents(tp
, ip
, bma
.cur
,
4608 &tmp_logflags
, whichfork
);
4609 bma
.logflags
|= tmp_logflags
;
4614 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
||
4615 XFS_IFORK_NEXTENTS(ip
, whichfork
) >
4616 XFS_IFORK_MAXEXT(ip
, whichfork
));
4620 * Log everything. Do this after conversion, there's no point in
4621 * logging the extent records if we've converted to btree format.
4623 if ((bma
.logflags
& xfs_ilog_fext(whichfork
)) &&
4624 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
4625 bma
.logflags
&= ~xfs_ilog_fext(whichfork
);
4626 else if ((bma
.logflags
& xfs_ilog_fbroot(whichfork
)) &&
4627 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)
4628 bma
.logflags
&= ~xfs_ilog_fbroot(whichfork
);
4630 * Log whatever the flags say, even if error. Otherwise we might miss
4631 * detecting a case where the data is changed, there's an error,
4632 * and it's not logged so we don't shutdown when we should.
4635 xfs_trans_log_inode(tp
, ip
, bma
.logflags
);
4639 ASSERT(*firstblock
== NULLFSBLOCK
||
4640 XFS_FSB_TO_AGNO(mp
, *firstblock
) ==
4642 bma
.cur
->bc_private
.b
.firstblock
) ||
4644 XFS_FSB_TO_AGNO(mp
, *firstblock
) <
4646 bma
.cur
->bc_private
.b
.firstblock
)));
4647 *firstblock
= bma
.cur
->bc_private
.b
.firstblock
;
4649 xfs_btree_del_cursor(bma
.cur
,
4650 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
4653 xfs_bmap_validate_ret(orig_bno
, orig_len
, orig_flags
, orig_mval
,
4659 * Called by xfs_bmapi to update file extent records and the btree
4660 * after removing space (or undoing a delayed allocation).
4662 STATIC
int /* error */
4663 xfs_bmap_del_extent(
4664 xfs_inode_t
*ip
, /* incore inode pointer */
4665 xfs_trans_t
*tp
, /* current transaction pointer */
4666 xfs_extnum_t
*idx
, /* extent number to update/delete */
4667 xfs_bmap_free_t
*flist
, /* list of extents to be freed */
4668 xfs_btree_cur_t
*cur
, /* if null, not a btree */
4669 xfs_bmbt_irec_t
*del
, /* data to remove from extents */
4670 int *logflagsp
, /* inode logging flags */
4671 int whichfork
) /* data or attr fork */
4673 xfs_filblks_t da_new
; /* new delay-alloc indirect blocks */
4674 xfs_filblks_t da_old
; /* old delay-alloc indirect blocks */
4675 xfs_fsblock_t del_endblock
=0; /* first block past del */
4676 xfs_fileoff_t del_endoff
; /* first offset past del */
4677 int delay
; /* current block is delayed allocated */
4678 int do_fx
; /* free extent at end of routine */
4679 xfs_bmbt_rec_host_t
*ep
; /* current extent entry pointer */
4680 int error
; /* error return value */
4681 int flags
; /* inode logging flags */
4682 xfs_bmbt_irec_t got
; /* current extent entry */
4683 xfs_fileoff_t got_endoff
; /* first offset past got */
4684 int i
; /* temp state */
4685 xfs_ifork_t
*ifp
; /* inode fork pointer */
4686 xfs_mount_t
*mp
; /* mount structure */
4687 xfs_filblks_t nblks
; /* quota/sb block count */
4688 xfs_bmbt_irec_t
new; /* new record to be inserted */
4690 uint qfield
; /* quota field to update */
4691 xfs_filblks_t temp
; /* for indirect length calculations */
4692 xfs_filblks_t temp2
; /* for indirect length calculations */
4695 XFS_STATS_INC(xs_del_exlist
);
4697 if (whichfork
== XFS_ATTR_FORK
)
4698 state
|= BMAP_ATTRFORK
;
4701 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4702 ASSERT((*idx
>= 0) && (*idx
< ifp
->if_bytes
/
4703 (uint
)sizeof(xfs_bmbt_rec_t
)));
4704 ASSERT(del
->br_blockcount
> 0);
4705 ep
= xfs_iext_get_ext(ifp
, *idx
);
4706 xfs_bmbt_get_all(ep
, &got
);
4707 ASSERT(got
.br_startoff
<= del
->br_startoff
);
4708 del_endoff
= del
->br_startoff
+ del
->br_blockcount
;
4709 got_endoff
= got
.br_startoff
+ got
.br_blockcount
;
4710 ASSERT(got_endoff
>= del_endoff
);
4711 delay
= isnullstartblock(got
.br_startblock
);
4712 ASSERT(isnullstartblock(del
->br_startblock
) == delay
);
4717 * If deleting a real allocation, must free up the disk space.
4720 flags
= XFS_ILOG_CORE
;
4722 * Realtime allocation. Free it and record di_nblocks update.
4724 if (whichfork
== XFS_DATA_FORK
&& XFS_IS_REALTIME_INODE(ip
)) {
4728 ASSERT(do_mod(del
->br_blockcount
,
4729 mp
->m_sb
.sb_rextsize
) == 0);
4730 ASSERT(do_mod(del
->br_startblock
,
4731 mp
->m_sb
.sb_rextsize
) == 0);
4732 bno
= del
->br_startblock
;
4733 len
= del
->br_blockcount
;
4734 do_div(bno
, mp
->m_sb
.sb_rextsize
);
4735 do_div(len
, mp
->m_sb
.sb_rextsize
);
4736 error
= xfs_rtfree_extent(tp
, bno
, (xfs_extlen_t
)len
);
4740 nblks
= len
* mp
->m_sb
.sb_rextsize
;
4741 qfield
= XFS_TRANS_DQ_RTBCOUNT
;
4744 * Ordinary allocation.
4748 nblks
= del
->br_blockcount
;
4749 qfield
= XFS_TRANS_DQ_BCOUNT
;
4752 * Set up del_endblock and cur for later.
4754 del_endblock
= del
->br_startblock
+ del
->br_blockcount
;
4756 if ((error
= xfs_bmbt_lookup_eq(cur
, got
.br_startoff
,
4757 got
.br_startblock
, got
.br_blockcount
,
4760 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
4762 da_old
= da_new
= 0;
4764 da_old
= startblockval(got
.br_startblock
);
4770 * Set flag value to use in switch statement.
4771 * Left-contig is 2, right-contig is 1.
4773 switch (((got
.br_startoff
== del
->br_startoff
) << 1) |
4774 (got_endoff
== del_endoff
)) {
4777 * Matches the whole extent. Delete the entry.
4779 xfs_iext_remove(ip
, *idx
, 1,
4780 whichfork
== XFS_ATTR_FORK
? BMAP_ATTRFORK
: 0);
4785 XFS_IFORK_NEXT_SET(ip
, whichfork
,
4786 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
4787 flags
|= XFS_ILOG_CORE
;
4789 flags
|= xfs_ilog_fext(whichfork
);
4792 if ((error
= xfs_btree_delete(cur
, &i
)))
4794 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
4799 * Deleting the first part of the extent.
4801 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
4802 xfs_bmbt_set_startoff(ep
, del_endoff
);
4803 temp
= got
.br_blockcount
- del
->br_blockcount
;
4804 xfs_bmbt_set_blockcount(ep
, temp
);
4806 temp
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
4808 xfs_bmbt_set_startblock(ep
, nullstartblock((int)temp
));
4809 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
4813 xfs_bmbt_set_startblock(ep
, del_endblock
);
4814 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
4816 flags
|= xfs_ilog_fext(whichfork
);
4819 if ((error
= xfs_bmbt_update(cur
, del_endoff
, del_endblock
,
4820 got
.br_blockcount
- del
->br_blockcount
,
4827 * Deleting the last part of the extent.
4829 temp
= got
.br_blockcount
- del
->br_blockcount
;
4830 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
4831 xfs_bmbt_set_blockcount(ep
, temp
);
4833 temp
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
4835 xfs_bmbt_set_startblock(ep
, nullstartblock((int)temp
));
4836 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
4840 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
4842 flags
|= xfs_ilog_fext(whichfork
);
4845 if ((error
= xfs_bmbt_update(cur
, got
.br_startoff
,
4847 got
.br_blockcount
- del
->br_blockcount
,
4854 * Deleting the middle of the extent.
4856 temp
= del
->br_startoff
- got
.br_startoff
;
4857 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
4858 xfs_bmbt_set_blockcount(ep
, temp
);
4859 new.br_startoff
= del_endoff
;
4860 temp2
= got_endoff
- del_endoff
;
4861 new.br_blockcount
= temp2
;
4862 new.br_state
= got
.br_state
;
4864 new.br_startblock
= del_endblock
;
4865 flags
|= XFS_ILOG_CORE
;
4867 if ((error
= xfs_bmbt_update(cur
,
4869 got
.br_startblock
, temp
,
4872 if ((error
= xfs_btree_increment(cur
, 0, &i
)))
4874 cur
->bc_rec
.b
= new;
4875 error
= xfs_btree_insert(cur
, &i
);
4876 if (error
&& error
!= ENOSPC
)
4879 * If get no-space back from btree insert,
4880 * it tried a split, and we have a zero
4881 * block reservation.
4882 * Fix up our state and return the error.
4884 if (error
== ENOSPC
) {
4886 * Reset the cursor, don't trust
4887 * it after any insert operation.
4889 if ((error
= xfs_bmbt_lookup_eq(cur
,
4894 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
4896 * Update the btree record back
4897 * to the original value.
4899 if ((error
= xfs_bmbt_update(cur
,
4906 * Reset the extent record back
4907 * to the original value.
4909 xfs_bmbt_set_blockcount(ep
,
4912 error
= XFS_ERROR(ENOSPC
);
4915 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
4917 flags
|= xfs_ilog_fext(whichfork
);
4918 XFS_IFORK_NEXT_SET(ip
, whichfork
,
4919 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
4921 ASSERT(whichfork
== XFS_DATA_FORK
);
4922 temp
= xfs_bmap_worst_indlen(ip
, temp
);
4923 xfs_bmbt_set_startblock(ep
, nullstartblock((int)temp
));
4924 temp2
= xfs_bmap_worst_indlen(ip
, temp2
);
4925 new.br_startblock
= nullstartblock((int)temp2
);
4926 da_new
= temp
+ temp2
;
4927 while (da_new
> da_old
) {
4931 xfs_bmbt_set_startblock(ep
,
4932 nullstartblock((int)temp
));
4934 if (da_new
== da_old
)
4940 nullstartblock((int)temp2
);
4944 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
4945 xfs_iext_insert(ip
, *idx
+ 1, 1, &new, state
);
4950 * If we need to, add to list of extents to delete.
4953 xfs_bmap_add_free(del
->br_startblock
, del
->br_blockcount
, flist
,
4956 * Adjust inode # blocks in the file.
4959 ip
->i_d
.di_nblocks
-= nblks
;
4961 * Adjust quota data.
4964 xfs_trans_mod_dquot_byino(tp
, ip
, qfield
, (long)-nblks
);
4967 * Account for change in delayed indirect blocks.
4968 * Nothing to do for disk quota accounting here.
4970 ASSERT(da_old
>= da_new
);
4971 if (da_old
> da_new
) {
4972 xfs_icsb_modify_counters(mp
, XFS_SBS_FDBLOCKS
,
4973 (int64_t)(da_old
- da_new
), 0);
4981 * Unmap (remove) blocks from a file.
4982 * If nexts is nonzero then the number of extents to remove is limited to
4983 * that value. If not all extents in the block range can be removed then
4988 xfs_trans_t
*tp
, /* transaction pointer */
4989 struct xfs_inode
*ip
, /* incore inode */
4990 xfs_fileoff_t bno
, /* starting offset to unmap */
4991 xfs_filblks_t len
, /* length to unmap in file */
4992 int flags
, /* misc flags */
4993 xfs_extnum_t nexts
, /* number of extents max */
4994 xfs_fsblock_t
*firstblock
, /* first allocated block
4995 controls a.g. for allocs */
4996 xfs_bmap_free_t
*flist
, /* i/o: list extents to free */
4997 int *done
) /* set if not done yet */
4999 xfs_btree_cur_t
*cur
; /* bmap btree cursor */
5000 xfs_bmbt_irec_t del
; /* extent being deleted */
5001 int eof
; /* is deleting at eof */
5002 xfs_bmbt_rec_host_t
*ep
; /* extent record pointer */
5003 int error
; /* error return value */
5004 xfs_extnum_t extno
; /* extent number in list */
5005 xfs_bmbt_irec_t got
; /* current extent record */
5006 xfs_ifork_t
*ifp
; /* inode fork pointer */
5007 int isrt
; /* freeing in rt area */
5008 xfs_extnum_t lastx
; /* last extent index used */
5009 int logflags
; /* transaction logging flags */
5010 xfs_extlen_t mod
; /* rt extent offset */
5011 xfs_mount_t
*mp
; /* mount structure */
5012 xfs_extnum_t nextents
; /* number of file extents */
5013 xfs_bmbt_irec_t prev
; /* previous extent record */
5014 xfs_fileoff_t start
; /* first file offset deleted */
5015 int tmp_logflags
; /* partial logging flags */
5016 int wasdel
; /* was a delayed alloc extent */
5017 int whichfork
; /* data or attribute fork */
5020 trace_xfs_bunmap(ip
, bno
, len
, flags
, _RET_IP_
);
5022 whichfork
= (flags
& XFS_BMAPI_ATTRFORK
) ?
5023 XFS_ATTR_FORK
: XFS_DATA_FORK
;
5024 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5026 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
5027 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)) {
5028 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW
,
5030 return XFS_ERROR(EFSCORRUPTED
);
5033 if (XFS_FORCED_SHUTDOWN(mp
))
5034 return XFS_ERROR(EIO
);
5039 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
5040 (error
= xfs_iread_extents(tp
, ip
, whichfork
)))
5042 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
5043 if (nextents
== 0) {
5047 XFS_STATS_INC(xs_blk_unmap
);
5048 isrt
= (whichfork
== XFS_DATA_FORK
) && XFS_IS_REALTIME_INODE(ip
);
5050 bno
= start
+ len
- 1;
5051 ep
= xfs_bmap_search_extents(ip
, bno
, whichfork
, &eof
, &lastx
, &got
,
5055 * Check to see if the given block number is past the end of the
5056 * file, back up to the last block if so...
5059 ep
= xfs_iext_get_ext(ifp
, --lastx
);
5060 xfs_bmbt_get_all(ep
, &got
);
5061 bno
= got
.br_startoff
+ got
.br_blockcount
- 1;
5064 if (ifp
->if_flags
& XFS_IFBROOT
) {
5065 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
);
5066 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
5067 cur
->bc_private
.b
.firstblock
= *firstblock
;
5068 cur
->bc_private
.b
.flist
= flist
;
5069 cur
->bc_private
.b
.flags
= 0;
5075 * Synchronize by locking the bitmap inode.
5077 xfs_ilock(mp
->m_rbmip
, XFS_ILOCK_EXCL
);
5078 xfs_trans_ijoin(tp
, mp
->m_rbmip
, XFS_ILOCK_EXCL
);
5082 while (bno
!= (xfs_fileoff_t
)-1 && bno
>= start
&& lastx
>= 0 &&
5083 (nexts
== 0 || extno
< nexts
)) {
5085 * Is the found extent after a hole in which bno lives?
5086 * Just back up to the previous extent, if so.
5088 if (got
.br_startoff
> bno
) {
5091 ep
= xfs_iext_get_ext(ifp
, lastx
);
5092 xfs_bmbt_get_all(ep
, &got
);
5095 * Is the last block of this extent before the range
5096 * we're supposed to delete? If so, we're done.
5098 bno
= XFS_FILEOFF_MIN(bno
,
5099 got
.br_startoff
+ got
.br_blockcount
- 1);
5103 * Then deal with the (possibly delayed) allocated space
5108 wasdel
= isnullstartblock(del
.br_startblock
);
5109 if (got
.br_startoff
< start
) {
5110 del
.br_startoff
= start
;
5111 del
.br_blockcount
-= start
- got
.br_startoff
;
5113 del
.br_startblock
+= start
- got
.br_startoff
;
5115 if (del
.br_startoff
+ del
.br_blockcount
> bno
+ 1)
5116 del
.br_blockcount
= bno
+ 1 - del
.br_startoff
;
5117 sum
= del
.br_startblock
+ del
.br_blockcount
;
5119 (mod
= do_mod(sum
, mp
->m_sb
.sb_rextsize
))) {
5121 * Realtime extent not lined up at the end.
5122 * The extent could have been split into written
5123 * and unwritten pieces, or we could just be
5124 * unmapping part of it. But we can't really
5125 * get rid of part of a realtime extent.
5127 if (del
.br_state
== XFS_EXT_UNWRITTEN
||
5128 !xfs_sb_version_hasextflgbit(&mp
->m_sb
)) {
5130 * This piece is unwritten, or we're not
5131 * using unwritten extents. Skip over it.
5134 bno
-= mod
> del
.br_blockcount
?
5135 del
.br_blockcount
: mod
;
5136 if (bno
< got
.br_startoff
) {
5138 xfs_bmbt_get_all(xfs_iext_get_ext(
5144 * It's written, turn it unwritten.
5145 * This is better than zeroing it.
5147 ASSERT(del
.br_state
== XFS_EXT_NORM
);
5148 ASSERT(xfs_trans_get_block_res(tp
) > 0);
5150 * If this spans a realtime extent boundary,
5151 * chop it back to the start of the one we end at.
5153 if (del
.br_blockcount
> mod
) {
5154 del
.br_startoff
+= del
.br_blockcount
- mod
;
5155 del
.br_startblock
+= del
.br_blockcount
- mod
;
5156 del
.br_blockcount
= mod
;
5158 del
.br_state
= XFS_EXT_UNWRITTEN
;
5159 error
= xfs_bmap_add_extent_unwritten_real(tp
, ip
,
5160 &lastx
, &cur
, &del
, firstblock
, flist
,
5166 if (isrt
&& (mod
= do_mod(del
.br_startblock
, mp
->m_sb
.sb_rextsize
))) {
5168 * Realtime extent is lined up at the end but not
5169 * at the front. We'll get rid of full extents if
5172 mod
= mp
->m_sb
.sb_rextsize
- mod
;
5173 if (del
.br_blockcount
> mod
) {
5174 del
.br_blockcount
-= mod
;
5175 del
.br_startoff
+= mod
;
5176 del
.br_startblock
+= mod
;
5177 } else if ((del
.br_startoff
== start
&&
5178 (del
.br_state
== XFS_EXT_UNWRITTEN
||
5179 xfs_trans_get_block_res(tp
) == 0)) ||
5180 !xfs_sb_version_hasextflgbit(&mp
->m_sb
)) {
5182 * Can't make it unwritten. There isn't
5183 * a full extent here so just skip it.
5185 ASSERT(bno
>= del
.br_blockcount
);
5186 bno
-= del
.br_blockcount
;
5187 if (got
.br_startoff
> bno
) {
5189 ep
= xfs_iext_get_ext(ifp
,
5191 xfs_bmbt_get_all(ep
, &got
);
5195 } else if (del
.br_state
== XFS_EXT_UNWRITTEN
) {
5197 * This one is already unwritten.
5198 * It must have a written left neighbor.
5199 * Unwrite the killed part of that one and
5203 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
,
5205 ASSERT(prev
.br_state
== XFS_EXT_NORM
);
5206 ASSERT(!isnullstartblock(prev
.br_startblock
));
5207 ASSERT(del
.br_startblock
==
5208 prev
.br_startblock
+ prev
.br_blockcount
);
5209 if (prev
.br_startoff
< start
) {
5210 mod
= start
- prev
.br_startoff
;
5211 prev
.br_blockcount
-= mod
;
5212 prev
.br_startblock
+= mod
;
5213 prev
.br_startoff
= start
;
5215 prev
.br_state
= XFS_EXT_UNWRITTEN
;
5217 error
= xfs_bmap_add_extent_unwritten_real(tp
,
5218 ip
, &lastx
, &cur
, &prev
,
5219 firstblock
, flist
, &logflags
);
5224 ASSERT(del
.br_state
== XFS_EXT_NORM
);
5225 del
.br_state
= XFS_EXT_UNWRITTEN
;
5226 error
= xfs_bmap_add_extent_unwritten_real(tp
,
5227 ip
, &lastx
, &cur
, &del
,
5228 firstblock
, flist
, &logflags
);
5235 ASSERT(startblockval(del
.br_startblock
) > 0);
5236 /* Update realtime/data freespace, unreserve quota */
5238 xfs_filblks_t rtexts
;
5240 rtexts
= XFS_FSB_TO_B(mp
, del
.br_blockcount
);
5241 do_div(rtexts
, mp
->m_sb
.sb_rextsize
);
5242 xfs_mod_incore_sb(mp
, XFS_SBS_FREXTENTS
,
5243 (int64_t)rtexts
, 0);
5244 (void)xfs_trans_reserve_quota_nblks(NULL
,
5245 ip
, -((long)del
.br_blockcount
), 0,
5246 XFS_QMOPT_RES_RTBLKS
);
5248 xfs_icsb_modify_counters(mp
, XFS_SBS_FDBLOCKS
,
5249 (int64_t)del
.br_blockcount
, 0);
5250 (void)xfs_trans_reserve_quota_nblks(NULL
,
5251 ip
, -((long)del
.br_blockcount
), 0,
5252 XFS_QMOPT_RES_REGBLKS
);
5254 ip
->i_delayed_blks
-= del
.br_blockcount
;
5256 cur
->bc_private
.b
.flags
|=
5257 XFS_BTCUR_BPRV_WASDEL
;
5259 cur
->bc_private
.b
.flags
&= ~XFS_BTCUR_BPRV_WASDEL
;
5261 * If it's the case where the directory code is running
5262 * with no block reservation, and the deleted block is in
5263 * the middle of its extent, and the resulting insert
5264 * of an extent would cause transformation to btree format,
5265 * then reject it. The calling code will then swap
5266 * blocks around instead.
5267 * We have to do this now, rather than waiting for the
5268 * conversion to btree format, since the transaction
5271 if (!wasdel
&& xfs_trans_get_block_res(tp
) == 0 &&
5272 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
&&
5273 XFS_IFORK_NEXTENTS(ip
, whichfork
) >= /* Note the >= */
5274 XFS_IFORK_MAXEXT(ip
, whichfork
) &&
5275 del
.br_startoff
> got
.br_startoff
&&
5276 del
.br_startoff
+ del
.br_blockcount
<
5277 got
.br_startoff
+ got
.br_blockcount
) {
5278 error
= XFS_ERROR(ENOSPC
);
5281 error
= xfs_bmap_del_extent(ip
, tp
, &lastx
, flist
, cur
, &del
,
5282 &tmp_logflags
, whichfork
);
5283 logflags
|= tmp_logflags
;
5286 bno
= del
.br_startoff
- 1;
5289 * If not done go on to the next (previous) record.
5291 if (bno
!= (xfs_fileoff_t
)-1 && bno
>= start
) {
5293 ep
= xfs_iext_get_ext(ifp
, lastx
);
5294 if (xfs_bmbt_get_startoff(ep
) > bno
) {
5296 ep
= xfs_iext_get_ext(ifp
,
5299 xfs_bmbt_get_all(ep
, &got
);
5304 *done
= bno
== (xfs_fileoff_t
)-1 || bno
< start
|| lastx
< 0;
5307 * Convert to a btree if necessary.
5309 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
5310 ASSERT(cur
== NULL
);
5311 error
= xfs_bmap_extents_to_btree(tp
, ip
, firstblock
, flist
,
5312 &cur
, 0, &tmp_logflags
, whichfork
);
5313 logflags
|= tmp_logflags
;
5318 * transform from btree to extents, give it cur
5320 else if (xfs_bmap_wants_extents(ip
, whichfork
)) {
5321 ASSERT(cur
!= NULL
);
5322 error
= xfs_bmap_btree_to_extents(tp
, ip
, cur
, &tmp_logflags
,
5324 logflags
|= tmp_logflags
;
5329 * transform from extents to local?
5334 * Log everything. Do this after conversion, there's no point in
5335 * logging the extent records if we've converted to btree format.
5337 if ((logflags
& xfs_ilog_fext(whichfork
)) &&
5338 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
5339 logflags
&= ~xfs_ilog_fext(whichfork
);
5340 else if ((logflags
& xfs_ilog_fbroot(whichfork
)) &&
5341 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)
5342 logflags
&= ~xfs_ilog_fbroot(whichfork
);
5344 * Log inode even in the error case, if the transaction
5345 * is dirty we'll need to shut down the filesystem.
5348 xfs_trans_log_inode(tp
, ip
, logflags
);
5351 *firstblock
= cur
->bc_private
.b
.firstblock
;
5352 cur
->bc_private
.b
.allocated
= 0;
5354 xfs_btree_del_cursor(cur
,
5355 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);