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
18 #include "libxfs_priv.h"
20 #include "xfs_shared.h"
21 #include "xfs_format.h"
22 #include "xfs_log_format.h"
23 #include "xfs_trans_resv.h"
26 #include "xfs_mount.h"
27 #include "xfs_defer.h"
28 #include "xfs_da_format.h"
29 #include "xfs_da_btree.h"
31 #include "xfs_inode.h"
32 #include "xfs_btree.h"
33 #include "xfs_trans.h"
34 #include "xfs_alloc.h"
36 #include "xfs_bmap_btree.h"
37 #include "xfs_trans_space.h"
38 #include "xfs_trace.h"
39 #include "xfs_attr_leaf.h"
40 #include "xfs_quota_defs.h"
42 #include "xfs_ag_resv.h"
45 kmem_zone_t
*xfs_bmap_free_item_zone
;
48 * Miscellaneous helper functions
52 * Compute and fill in the value of the maximum depth of a bmap btree
53 * in this filesystem. Done once, during mount.
56 xfs_bmap_compute_maxlevels(
57 xfs_mount_t
*mp
, /* file system mount structure */
58 int whichfork
) /* data or attr fork */
60 int level
; /* btree level */
61 uint maxblocks
; /* max blocks at this level */
62 uint maxleafents
; /* max leaf entries possible */
63 int maxrootrecs
; /* max records in root block */
64 int minleafrecs
; /* min records in leaf block */
65 int minnoderecs
; /* min records in node block */
66 int sz
; /* root block size */
69 * The maximum number of extents in a file, hence the maximum
70 * number of leaf entries, is controlled by the type of di_nextents
71 * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
72 * (a signed 16-bit number, xfs_aextnum_t).
74 * Note that we can no longer assume that if we are in ATTR1 that
75 * the fork offset of all the inodes will be
76 * (xfs_default_attroffset(ip) >> 3) because we could have mounted
77 * with ATTR2 and then mounted back with ATTR1, keeping the
78 * di_forkoff's fixed but probably at various positions. Therefore,
79 * for both ATTR1 and ATTR2 we have to assume the worst case scenario
80 * of a minimum size available.
82 if (whichfork
== XFS_DATA_FORK
) {
83 maxleafents
= MAXEXTNUM
;
84 sz
= XFS_BMDR_SPACE_CALC(MINDBTPTRS
);
86 maxleafents
= MAXAEXTNUM
;
87 sz
= XFS_BMDR_SPACE_CALC(MINABTPTRS
);
89 maxrootrecs
= xfs_bmdr_maxrecs(sz
, 0);
90 minleafrecs
= mp
->m_bmap_dmnr
[0];
91 minnoderecs
= mp
->m_bmap_dmnr
[1];
92 maxblocks
= (maxleafents
+ minleafrecs
- 1) / minleafrecs
;
93 for (level
= 1; maxblocks
> 1; level
++) {
94 if (maxblocks
<= maxrootrecs
)
97 maxblocks
= (maxblocks
+ minnoderecs
- 1) / minnoderecs
;
99 mp
->m_bm_maxlevels
[whichfork
] = level
;
102 STATIC
int /* error */
104 struct xfs_btree_cur
*cur
,
108 int *stat
) /* success/failure */
110 cur
->bc_rec
.b
.br_startoff
= off
;
111 cur
->bc_rec
.b
.br_startblock
= bno
;
112 cur
->bc_rec
.b
.br_blockcount
= len
;
113 return xfs_btree_lookup(cur
, XFS_LOOKUP_EQ
, stat
);
116 STATIC
int /* error */
118 struct xfs_btree_cur
*cur
,
122 int *stat
) /* success/failure */
124 cur
->bc_rec
.b
.br_startoff
= off
;
125 cur
->bc_rec
.b
.br_startblock
= bno
;
126 cur
->bc_rec
.b
.br_blockcount
= len
;
127 return xfs_btree_lookup(cur
, XFS_LOOKUP_GE
, stat
);
131 * Check if the inode needs to be converted to btree format.
133 static inline bool xfs_bmap_needs_btree(struct xfs_inode
*ip
, int whichfork
)
135 return XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
&&
136 XFS_IFORK_NEXTENTS(ip
, whichfork
) >
137 XFS_IFORK_MAXEXT(ip
, whichfork
);
141 * Check if the inode should be converted to extent format.
143 static inline bool xfs_bmap_wants_extents(struct xfs_inode
*ip
, int whichfork
)
145 return XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
&&
146 XFS_IFORK_NEXTENTS(ip
, whichfork
) <=
147 XFS_IFORK_MAXEXT(ip
, whichfork
);
151 * Update the record referred to by cur to the value given
152 * by [off, bno, len, state].
153 * This either works (return 0) or gets an EFSCORRUPTED error.
157 struct xfs_btree_cur
*cur
,
163 union xfs_btree_rec rec
;
165 xfs_bmbt_disk_set_allf(&rec
.bmbt
, off
, bno
, len
, state
);
166 return xfs_btree_update(cur
, &rec
);
170 * Compute the worst-case number of indirect blocks that will be used
171 * for ip's delayed extent of length "len".
174 xfs_bmap_worst_indlen(
175 xfs_inode_t
*ip
, /* incore inode pointer */
176 xfs_filblks_t len
) /* delayed extent length */
178 int level
; /* btree level number */
179 int maxrecs
; /* maximum record count at this level */
180 xfs_mount_t
*mp
; /* mount structure */
181 xfs_filblks_t rval
; /* return value */
184 maxrecs
= mp
->m_bmap_dmxr
[0];
185 for (level
= 0, rval
= 0;
186 level
< XFS_BM_MAXLEVELS(mp
, XFS_DATA_FORK
);
189 do_div(len
, maxrecs
);
192 return rval
+ XFS_BM_MAXLEVELS(mp
, XFS_DATA_FORK
) -
195 maxrecs
= mp
->m_bmap_dmxr
[1];
201 * Calculate the default attribute fork offset for newly created inodes.
204 xfs_default_attroffset(
205 struct xfs_inode
*ip
)
207 struct xfs_mount
*mp
= ip
->i_mount
;
210 if (mp
->m_sb
.sb_inodesize
== 256) {
211 offset
= XFS_LITINO(mp
, ip
->i_d
.di_version
) -
212 XFS_BMDR_SPACE_CALC(MINABTPTRS
);
214 offset
= XFS_BMDR_SPACE_CALC(6 * MINABTPTRS
);
217 ASSERT(offset
< XFS_LITINO(mp
, ip
->i_d
.di_version
));
222 * Helper routine to reset inode di_forkoff field when switching
223 * attribute fork from local to extent format - we reset it where
224 * possible to make space available for inline data fork extents.
227 xfs_bmap_forkoff_reset(
231 if (whichfork
== XFS_ATTR_FORK
&&
232 ip
->i_d
.di_format
!= XFS_DINODE_FMT_DEV
&&
233 ip
->i_d
.di_format
!= XFS_DINODE_FMT_UUID
&&
234 ip
->i_d
.di_format
!= XFS_DINODE_FMT_BTREE
) {
235 uint dfl_forkoff
= xfs_default_attroffset(ip
) >> 3;
237 if (dfl_forkoff
> ip
->i_d
.di_forkoff
)
238 ip
->i_d
.di_forkoff
= dfl_forkoff
;
243 STATIC
struct xfs_buf
*
245 struct xfs_btree_cur
*cur
,
248 struct xfs_log_item_desc
*lidp
;
254 for (i
= 0; i
< XFS_BTREE_MAXLEVELS
; i
++) {
255 if (!cur
->bc_bufs
[i
])
257 if (XFS_BUF_ADDR(cur
->bc_bufs
[i
]) == bno
)
258 return cur
->bc_bufs
[i
];
261 /* Chase down all the log items to see if the bp is there */
262 list_for_each_entry(lidp
, &cur
->bc_tp
->t_items
, lid_trans
) {
263 struct xfs_buf_log_item
*bip
;
264 bip
= (struct xfs_buf_log_item
*)lidp
->lid_item
;
265 if (bip
->bli_item
.li_type
== XFS_LI_BUF
&&
266 XFS_BUF_ADDR(bip
->bli_buf
) == bno
)
275 struct xfs_btree_block
*block
,
281 __be64
*pp
, *thispa
; /* pointer to block address */
282 xfs_bmbt_key_t
*prevp
, *keyp
;
284 ASSERT(be16_to_cpu(block
->bb_level
) > 0);
287 for( i
= 1; i
<= xfs_btree_get_numrecs(block
); i
++) {
288 dmxr
= mp
->m_bmap_dmxr
[0];
289 keyp
= XFS_BMBT_KEY_ADDR(mp
, block
, i
);
292 ASSERT(be64_to_cpu(prevp
->br_startoff
) <
293 be64_to_cpu(keyp
->br_startoff
));
298 * Compare the block numbers to see if there are dups.
301 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, i
, sz
);
303 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, i
, dmxr
);
305 for (j
= i
+1; j
<= be16_to_cpu(block
->bb_numrecs
); j
++) {
307 thispa
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, j
, sz
);
309 thispa
= XFS_BMBT_PTR_ADDR(mp
, block
, j
, dmxr
);
310 if (*thispa
== *pp
) {
311 xfs_warn(mp
, "%s: thispa(%d) == pp(%d) %Ld",
313 (unsigned long long)be64_to_cpu(*thispa
));
314 panic("%s: ptrs are equal in node\n",
322 * Check that the extents for the inode ip are in the right order in all
323 * btree leaves. THis becomes prohibitively expensive for large extent count
324 * files, so don't bother with inodes that have more than 10,000 extents in
325 * them. The btree record ordering checks will still be done, so for such large
326 * bmapbt constructs that is going to catch most corruptions.
329 xfs_bmap_check_leaf_extents(
330 xfs_btree_cur_t
*cur
, /* btree cursor or null */
331 xfs_inode_t
*ip
, /* incore inode pointer */
332 int whichfork
) /* data or attr fork */
334 struct xfs_btree_block
*block
; /* current btree block */
335 xfs_fsblock_t bno
; /* block # of "block" */
336 xfs_buf_t
*bp
; /* buffer for "block" */
337 int error
; /* error return value */
338 xfs_extnum_t i
=0, j
; /* index into the extents list */
339 xfs_ifork_t
*ifp
; /* fork structure */
340 int level
; /* btree level, for checking */
341 xfs_mount_t
*mp
; /* file system mount structure */
342 __be64
*pp
; /* pointer to block address */
343 xfs_bmbt_rec_t
*ep
; /* pointer to current extent */
344 xfs_bmbt_rec_t last
= {0, 0}; /* last extent in prev block */
345 xfs_bmbt_rec_t
*nextp
; /* pointer to next extent */
348 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
) {
352 /* skip large extent count inodes */
353 if (ip
->i_d
.di_nextents
> 10000)
358 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
359 block
= ifp
->if_broot
;
361 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
363 level
= be16_to_cpu(block
->bb_level
);
365 xfs_check_block(block
, mp
, 1, ifp
->if_broot_bytes
);
366 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, 1, ifp
->if_broot_bytes
);
367 bno
= be64_to_cpu(*pp
);
369 ASSERT(bno
!= NULLFSBLOCK
);
370 ASSERT(XFS_FSB_TO_AGNO(mp
, bno
) < mp
->m_sb
.sb_agcount
);
371 ASSERT(XFS_FSB_TO_AGBNO(mp
, bno
) < mp
->m_sb
.sb_agblocks
);
374 * Go down the tree until leaf level is reached, following the first
375 * pointer (leftmost) at each level.
377 while (level
-- > 0) {
378 /* See if buf is in cur first */
380 bp
= xfs_bmap_get_bp(cur
, XFS_FSB_TO_DADDR(mp
, bno
));
383 error
= xfs_btree_read_bufl(mp
, NULL
, bno
, 0, &bp
,
389 block
= XFS_BUF_TO_BLOCK(bp
);
394 * Check this block for basic sanity (increasing keys and
395 * no duplicate blocks).
398 xfs_check_block(block
, mp
, 0, 0);
399 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, mp
->m_bmap_dmxr
[1]);
400 bno
= be64_to_cpu(*pp
);
401 XFS_WANT_CORRUPTED_GOTO(mp
,
402 XFS_FSB_SANITY_CHECK(mp
, bno
), error0
);
405 xfs_trans_brelse(NULL
, bp
);
410 * Here with bp and block set to the leftmost leaf node in the tree.
415 * Loop over all leaf nodes checking that all extents are in the right order.
418 xfs_fsblock_t nextbno
;
419 xfs_extnum_t num_recs
;
422 num_recs
= xfs_btree_get_numrecs(block
);
425 * Read-ahead the next leaf block, if any.
428 nextbno
= be64_to_cpu(block
->bb_u
.l
.bb_rightsib
);
431 * Check all the extents to make sure they are OK.
432 * If we had a previous block, the last entry should
433 * conform with the first entry in this one.
436 ep
= XFS_BMBT_REC_ADDR(mp
, block
, 1);
438 ASSERT(xfs_bmbt_disk_get_startoff(&last
) +
439 xfs_bmbt_disk_get_blockcount(&last
) <=
440 xfs_bmbt_disk_get_startoff(ep
));
442 for (j
= 1; j
< num_recs
; j
++) {
443 nextp
= XFS_BMBT_REC_ADDR(mp
, block
, j
+ 1);
444 ASSERT(xfs_bmbt_disk_get_startoff(ep
) +
445 xfs_bmbt_disk_get_blockcount(ep
) <=
446 xfs_bmbt_disk_get_startoff(nextp
));
454 xfs_trans_brelse(NULL
, bp
);
458 * If we've reached the end, stop.
460 if (bno
== NULLFSBLOCK
)
464 bp
= xfs_bmap_get_bp(cur
, XFS_FSB_TO_DADDR(mp
, bno
));
467 error
= xfs_btree_read_bufl(mp
, NULL
, bno
, 0, &bp
,
473 block
= XFS_BUF_TO_BLOCK(bp
);
479 xfs_warn(mp
, "%s: at error0", __func__
);
481 xfs_trans_brelse(NULL
, bp
);
483 xfs_warn(mp
, "%s: BAD after btree leaves for %d extents",
485 panic("%s: CORRUPTED BTREE OR SOMETHING", __func__
);
490 * Add bmap trace insert entries for all the contents of the extent records.
493 xfs_bmap_trace_exlist(
494 xfs_inode_t
*ip
, /* incore inode pointer */
495 xfs_extnum_t cnt
, /* count of entries in the list */
496 int whichfork
, /* data or attr fork */
497 unsigned long caller_ip
)
499 xfs_extnum_t idx
; /* extent record index */
500 xfs_ifork_t
*ifp
; /* inode fork pointer */
503 if (whichfork
== XFS_ATTR_FORK
)
504 state
|= BMAP_ATTRFORK
;
506 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
507 ASSERT(cnt
== (ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
)));
508 for (idx
= 0; idx
< cnt
; idx
++)
509 trace_xfs_extlist(ip
, idx
, whichfork
, caller_ip
);
513 * Validate that the bmbt_irecs being returned from bmapi are valid
514 * given the caller's original parameters. Specifically check the
515 * ranges of the returned irecs to ensure that they only extend beyond
516 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
519 xfs_bmap_validate_ret(
523 xfs_bmbt_irec_t
*mval
,
527 int i
; /* index to map values */
529 ASSERT(ret_nmap
<= nmap
);
531 for (i
= 0; i
< ret_nmap
; i
++) {
532 ASSERT(mval
[i
].br_blockcount
> 0);
533 if (!(flags
& XFS_BMAPI_ENTIRE
)) {
534 ASSERT(mval
[i
].br_startoff
>= bno
);
535 ASSERT(mval
[i
].br_blockcount
<= len
);
536 ASSERT(mval
[i
].br_startoff
+ mval
[i
].br_blockcount
<=
539 ASSERT(mval
[i
].br_startoff
< bno
+ len
);
540 ASSERT(mval
[i
].br_startoff
+ mval
[i
].br_blockcount
>
544 mval
[i
- 1].br_startoff
+ mval
[i
- 1].br_blockcount
==
545 mval
[i
].br_startoff
);
546 ASSERT(mval
[i
].br_startblock
!= DELAYSTARTBLOCK
&&
547 mval
[i
].br_startblock
!= HOLESTARTBLOCK
);
548 ASSERT(mval
[i
].br_state
== XFS_EXT_NORM
||
549 mval
[i
].br_state
== XFS_EXT_UNWRITTEN
);
554 #define xfs_bmap_check_leaf_extents(cur, ip, whichfork) do { } while (0)
555 #define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap)
559 * bmap free list manipulation functions
563 * Add the extent to the list of extents to be free at transaction end.
564 * The list is maintained sorted (by block number).
568 struct xfs_mount
*mp
,
569 struct xfs_defer_ops
*dfops
,
572 struct xfs_owner_info
*oinfo
)
574 struct xfs_extent_free_item
*new; /* new element */
579 ASSERT(bno
!= NULLFSBLOCK
);
581 ASSERT(len
<= MAXEXTLEN
);
582 ASSERT(!isnullstartblock(bno
));
583 agno
= XFS_FSB_TO_AGNO(mp
, bno
);
584 agbno
= XFS_FSB_TO_AGBNO(mp
, bno
);
585 ASSERT(agno
< mp
->m_sb
.sb_agcount
);
586 ASSERT(agbno
< mp
->m_sb
.sb_agblocks
);
587 ASSERT(len
< mp
->m_sb
.sb_agblocks
);
588 ASSERT(agbno
+ len
<= mp
->m_sb
.sb_agblocks
);
590 ASSERT(xfs_bmap_free_item_zone
!= NULL
);
592 new = kmem_zone_alloc(xfs_bmap_free_item_zone
, KM_SLEEP
);
593 new->xefi_startblock
= bno
;
594 new->xefi_blockcount
= (xfs_extlen_t
)len
;
596 new->xefi_oinfo
= *oinfo
;
598 xfs_rmap_skip_owner_update(&new->xefi_oinfo
);
599 trace_xfs_bmap_free_defer(mp
, XFS_FSB_TO_AGNO(mp
, bno
), 0,
600 XFS_FSB_TO_AGBNO(mp
, bno
), len
);
601 xfs_defer_add(dfops
, XFS_DEFER_OPS_TYPE_FREE
, &new->xefi_list
);
605 * Inode fork format manipulation functions
609 * Transform a btree format file with only one leaf node, where the
610 * extents list will fit in the inode, into an extents format file.
611 * Since the file extents are already in-core, all we have to do is
612 * give up the space for the btree root and pitch the leaf block.
614 STATIC
int /* error */
615 xfs_bmap_btree_to_extents(
616 xfs_trans_t
*tp
, /* transaction pointer */
617 xfs_inode_t
*ip
, /* incore inode pointer */
618 xfs_btree_cur_t
*cur
, /* btree cursor */
619 int *logflagsp
, /* inode logging flags */
620 int whichfork
) /* data or attr fork */
623 struct xfs_btree_block
*cblock
;/* child btree block */
624 xfs_fsblock_t cbno
; /* child block number */
625 xfs_buf_t
*cbp
; /* child block's buffer */
626 int error
; /* error return value */
627 xfs_ifork_t
*ifp
; /* inode fork data */
628 xfs_mount_t
*mp
; /* mount point structure */
629 __be64
*pp
; /* ptr to block address */
630 struct xfs_btree_block
*rblock
;/* root btree block */
631 struct xfs_owner_info oinfo
;
634 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
635 ASSERT(ifp
->if_flags
& XFS_IFEXTENTS
);
636 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
);
637 rblock
= ifp
->if_broot
;
638 ASSERT(be16_to_cpu(rblock
->bb_level
) == 1);
639 ASSERT(be16_to_cpu(rblock
->bb_numrecs
) == 1);
640 ASSERT(xfs_bmbt_maxrecs(mp
, ifp
->if_broot_bytes
, 0) == 1);
641 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, rblock
, 1, ifp
->if_broot_bytes
);
642 cbno
= be64_to_cpu(*pp
);
645 if ((error
= xfs_btree_check_lptr(cur
, cbno
, 1)))
648 error
= xfs_btree_read_bufl(mp
, tp
, cbno
, 0, &cbp
, XFS_BMAP_BTREE_REF
,
652 cblock
= XFS_BUF_TO_BLOCK(cbp
);
653 if ((error
= xfs_btree_check_block(cur
, cblock
, 0, cbp
)))
655 xfs_rmap_ino_bmbt_owner(&oinfo
, ip
->i_ino
, whichfork
);
656 xfs_bmap_add_free(mp
, cur
->bc_private
.b
.dfops
, cbno
, 1, &oinfo
);
657 ip
->i_d
.di_nblocks
--;
658 xfs_trans_mod_dquot_byino(tp
, ip
, XFS_TRANS_DQ_BCOUNT
, -1L);
659 xfs_trans_binval(tp
, cbp
);
660 if (cur
->bc_bufs
[0] == cbp
)
661 cur
->bc_bufs
[0] = NULL
;
662 xfs_iroot_realloc(ip
, -1, whichfork
);
663 ASSERT(ifp
->if_broot
== NULL
);
664 ASSERT((ifp
->if_flags
& XFS_IFBROOT
) == 0);
665 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_EXTENTS
);
666 *logflagsp
= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
671 * Convert an extents-format file into a btree-format file.
672 * The new file will have a root block (in the inode) and a single child block.
674 STATIC
int /* error */
675 xfs_bmap_extents_to_btree(
676 xfs_trans_t
*tp
, /* transaction pointer */
677 xfs_inode_t
*ip
, /* incore inode pointer */
678 xfs_fsblock_t
*firstblock
, /* first-block-allocated */
679 struct xfs_defer_ops
*dfops
, /* blocks freed in xaction */
680 xfs_btree_cur_t
**curp
, /* cursor returned to caller */
681 int wasdel
, /* converting a delayed alloc */
682 int *logflagsp
, /* inode logging flags */
683 int whichfork
) /* data or attr fork */
685 struct xfs_btree_block
*ablock
; /* allocated (child) bt block */
686 xfs_buf_t
*abp
; /* buffer for ablock */
687 xfs_alloc_arg_t args
; /* allocation arguments */
688 xfs_bmbt_rec_t
*arp
; /* child record pointer */
689 struct xfs_btree_block
*block
; /* btree root block */
690 xfs_btree_cur_t
*cur
; /* bmap btree cursor */
691 xfs_bmbt_rec_host_t
*ep
; /* extent record pointer */
692 int error
; /* error return value */
693 xfs_extnum_t i
, cnt
; /* extent record index */
694 xfs_ifork_t
*ifp
; /* inode fork pointer */
695 xfs_bmbt_key_t
*kp
; /* root block key pointer */
696 xfs_mount_t
*mp
; /* mount structure */
697 xfs_extnum_t nextents
; /* number of file extents */
698 xfs_bmbt_ptr_t
*pp
; /* root block address pointer */
701 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
702 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
);
705 * Make space in the inode incore.
707 xfs_iroot_realloc(ip
, 1, whichfork
);
708 ifp
->if_flags
|= XFS_IFBROOT
;
713 block
= ifp
->if_broot
;
714 if (xfs_sb_version_hascrc(&mp
->m_sb
))
715 xfs_btree_init_block_int(mp
, block
, XFS_BUF_DADDR_NULL
,
716 XFS_BMAP_CRC_MAGIC
, 1, 1, ip
->i_ino
,
717 XFS_BTREE_LONG_PTRS
| XFS_BTREE_CRC_BLOCKS
);
719 xfs_btree_init_block_int(mp
, block
, XFS_BUF_DADDR_NULL
,
720 XFS_BMAP_MAGIC
, 1, 1, ip
->i_ino
,
721 XFS_BTREE_LONG_PTRS
);
724 * Need a cursor. Can't allocate until bb_level is filled in.
726 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
727 cur
->bc_private
.b
.firstblock
= *firstblock
;
728 cur
->bc_private
.b
.dfops
= dfops
;
729 cur
->bc_private
.b
.flags
= wasdel
? XFS_BTCUR_BPRV_WASDEL
: 0;
731 * Convert to a btree with two levels, one record in root.
733 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_BTREE
);
734 memset(&args
, 0, sizeof(args
));
737 xfs_rmap_ino_bmbt_owner(&args
.oinfo
, ip
->i_ino
, whichfork
);
738 args
.firstblock
= *firstblock
;
739 if (*firstblock
== NULLFSBLOCK
) {
740 args
.type
= XFS_ALLOCTYPE_START_BNO
;
741 args
.fsbno
= XFS_INO_TO_FSB(mp
, ip
->i_ino
);
742 } else if (dfops
->dop_low
) {
743 args
.type
= XFS_ALLOCTYPE_START_BNO
;
744 args
.fsbno
= *firstblock
;
746 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
747 args
.fsbno
= *firstblock
;
749 args
.minlen
= args
.maxlen
= args
.prod
= 1;
750 args
.wasdel
= wasdel
;
752 if ((error
= xfs_alloc_vextent(&args
))) {
753 xfs_iroot_realloc(ip
, -1, whichfork
);
754 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
758 * Allocation can't fail, the space was reserved.
760 ASSERT(args
.fsbno
!= NULLFSBLOCK
);
761 ASSERT(*firstblock
== NULLFSBLOCK
||
762 args
.agno
== XFS_FSB_TO_AGNO(mp
, *firstblock
) ||
764 args
.agno
> XFS_FSB_TO_AGNO(mp
, *firstblock
)));
765 *firstblock
= cur
->bc_private
.b
.firstblock
= args
.fsbno
;
766 cur
->bc_private
.b
.allocated
++;
767 ip
->i_d
.di_nblocks
++;
768 xfs_trans_mod_dquot_byino(tp
, ip
, XFS_TRANS_DQ_BCOUNT
, 1L);
769 abp
= xfs_btree_get_bufl(mp
, tp
, args
.fsbno
, 0);
771 * Fill in the child block.
773 abp
->b_ops
= &xfs_bmbt_buf_ops
;
774 ablock
= XFS_BUF_TO_BLOCK(abp
);
775 if (xfs_sb_version_hascrc(&mp
->m_sb
))
776 xfs_btree_init_block_int(mp
, ablock
, abp
->b_bn
,
777 XFS_BMAP_CRC_MAGIC
, 0, 0, ip
->i_ino
,
778 XFS_BTREE_LONG_PTRS
| XFS_BTREE_CRC_BLOCKS
);
780 xfs_btree_init_block_int(mp
, ablock
, abp
->b_bn
,
781 XFS_BMAP_MAGIC
, 0, 0, ip
->i_ino
,
782 XFS_BTREE_LONG_PTRS
);
784 arp
= XFS_BMBT_REC_ADDR(mp
, ablock
, 1);
785 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
786 for (cnt
= i
= 0; i
< nextents
; i
++) {
787 ep
= xfs_iext_get_ext(ifp
, i
);
788 if (!isnullstartblock(xfs_bmbt_get_startblock(ep
))) {
789 arp
->l0
= cpu_to_be64(ep
->l0
);
790 arp
->l1
= cpu_to_be64(ep
->l1
);
794 ASSERT(cnt
== XFS_IFORK_NEXTENTS(ip
, whichfork
));
795 xfs_btree_set_numrecs(ablock
, cnt
);
798 * Fill in the root key and pointer.
800 kp
= XFS_BMBT_KEY_ADDR(mp
, block
, 1);
801 arp
= XFS_BMBT_REC_ADDR(mp
, ablock
, 1);
802 kp
->br_startoff
= cpu_to_be64(xfs_bmbt_disk_get_startoff(arp
));
803 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, xfs_bmbt_get_maxrecs(cur
,
804 be16_to_cpu(block
->bb_level
)));
805 *pp
= cpu_to_be64(args
.fsbno
);
808 * Do all this logging at the end so that
809 * the root is at the right level.
811 xfs_btree_log_block(cur
, abp
, XFS_BB_ALL_BITS
);
812 xfs_btree_log_recs(cur
, abp
, 1, be16_to_cpu(ablock
->bb_numrecs
));
813 ASSERT(*curp
== NULL
);
815 *logflagsp
= XFS_ILOG_CORE
| xfs_ilog_fbroot(whichfork
);
820 * Convert a local file to an extents file.
821 * This code is out of bounds for data forks of regular files,
822 * since the file data needs to get logged so things will stay consistent.
823 * (The bmap-level manipulations are ok, though).
826 xfs_bmap_local_to_extents_empty(
827 struct xfs_inode
*ip
,
830 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
832 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
833 ASSERT(ifp
->if_bytes
== 0);
834 ASSERT(XFS_IFORK_NEXTENTS(ip
, whichfork
) == 0);
836 xfs_bmap_forkoff_reset(ip
, whichfork
);
837 ifp
->if_flags
&= ~XFS_IFINLINE
;
838 ifp
->if_flags
|= XFS_IFEXTENTS
;
839 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_EXTENTS
);
843 STATIC
int /* error */
844 xfs_bmap_local_to_extents(
845 xfs_trans_t
*tp
, /* transaction pointer */
846 xfs_inode_t
*ip
, /* incore inode pointer */
847 xfs_fsblock_t
*firstblock
, /* first block allocated in xaction */
848 xfs_extlen_t total
, /* total blocks needed by transaction */
849 int *logflagsp
, /* inode logging flags */
851 void (*init_fn
)(struct xfs_trans
*tp
,
853 struct xfs_inode
*ip
,
854 struct xfs_ifork
*ifp
))
857 int flags
; /* logging flags returned */
858 xfs_ifork_t
*ifp
; /* inode fork pointer */
859 xfs_alloc_arg_t args
; /* allocation arguments */
860 xfs_buf_t
*bp
; /* buffer for extent block */
861 xfs_bmbt_rec_host_t
*ep
; /* extent record pointer */
864 * We don't want to deal with the case of keeping inode data inline yet.
865 * So sending the data fork of a regular inode is invalid.
867 ASSERT(!(S_ISREG(VFS_I(ip
)->i_mode
) && whichfork
== XFS_DATA_FORK
));
868 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
869 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
871 if (!ifp
->if_bytes
) {
872 xfs_bmap_local_to_extents_empty(ip
, whichfork
);
873 flags
= XFS_ILOG_CORE
;
879 ASSERT((ifp
->if_flags
& (XFS_IFINLINE
|XFS_IFEXTENTS
|XFS_IFEXTIREC
)) ==
881 memset(&args
, 0, sizeof(args
));
883 args
.mp
= ip
->i_mount
;
884 xfs_rmap_ino_owner(&args
.oinfo
, ip
->i_ino
, whichfork
, 0);
885 args
.firstblock
= *firstblock
;
887 * Allocate a block. We know we need only one, since the
888 * file currently fits in an inode.
890 if (*firstblock
== NULLFSBLOCK
) {
891 args
.fsbno
= XFS_INO_TO_FSB(args
.mp
, ip
->i_ino
);
892 args
.type
= XFS_ALLOCTYPE_START_BNO
;
894 args
.fsbno
= *firstblock
;
895 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
898 args
.minlen
= args
.maxlen
= args
.prod
= 1;
899 error
= xfs_alloc_vextent(&args
);
903 /* Can't fail, the space was reserved. */
904 ASSERT(args
.fsbno
!= NULLFSBLOCK
);
905 ASSERT(args
.len
== 1);
906 *firstblock
= args
.fsbno
;
907 bp
= xfs_btree_get_bufl(args
.mp
, tp
, args
.fsbno
, 0);
910 * Initialize the block, copy the data and log the remote buffer.
912 * The callout is responsible for logging because the remote format
913 * might differ from the local format and thus we don't know how much to
914 * log here. Note that init_fn must also set the buffer log item type
917 init_fn(tp
, bp
, ip
, ifp
);
919 /* account for the change in fork size */
920 xfs_idata_realloc(ip
, -ifp
->if_bytes
, whichfork
);
921 xfs_bmap_local_to_extents_empty(ip
, whichfork
);
922 flags
|= XFS_ILOG_CORE
;
924 xfs_iext_add(ifp
, 0, 1);
925 ep
= xfs_iext_get_ext(ifp
, 0);
926 xfs_bmbt_set_allf(ep
, 0, args
.fsbno
, 1, XFS_EXT_NORM
);
927 trace_xfs_bmap_post_update(ip
, 0,
928 whichfork
== XFS_ATTR_FORK
? BMAP_ATTRFORK
: 0,
930 XFS_IFORK_NEXT_SET(ip
, whichfork
, 1);
931 ip
->i_d
.di_nblocks
= 1;
932 xfs_trans_mod_dquot_byino(tp
, ip
,
933 XFS_TRANS_DQ_BCOUNT
, 1L);
934 flags
|= xfs_ilog_fext(whichfork
);
942 * Called from xfs_bmap_add_attrfork to handle btree format files.
944 STATIC
int /* error */
945 xfs_bmap_add_attrfork_btree(
946 xfs_trans_t
*tp
, /* transaction pointer */
947 xfs_inode_t
*ip
, /* incore inode pointer */
948 xfs_fsblock_t
*firstblock
, /* first block allocated */
949 struct xfs_defer_ops
*dfops
, /* blocks to free at commit */
950 int *flags
) /* inode logging flags */
952 xfs_btree_cur_t
*cur
; /* btree cursor */
953 int error
; /* error return value */
954 xfs_mount_t
*mp
; /* file system mount struct */
955 int stat
; /* newroot status */
958 if (ip
->i_df
.if_broot_bytes
<= XFS_IFORK_DSIZE(ip
))
959 *flags
|= XFS_ILOG_DBROOT
;
961 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, XFS_DATA_FORK
);
962 cur
->bc_private
.b
.dfops
= dfops
;
963 cur
->bc_private
.b
.firstblock
= *firstblock
;
964 if ((error
= xfs_bmbt_lookup_ge(cur
, 0, 0, 0, &stat
)))
966 /* must be at least one entry */
967 XFS_WANT_CORRUPTED_GOTO(mp
, stat
== 1, error0
);
968 if ((error
= xfs_btree_new_iroot(cur
, flags
, &stat
)))
971 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
974 *firstblock
= cur
->bc_private
.b
.firstblock
;
975 cur
->bc_private
.b
.allocated
= 0;
976 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
980 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
985 * Called from xfs_bmap_add_attrfork to handle extents format files.
987 STATIC
int /* error */
988 xfs_bmap_add_attrfork_extents(
989 xfs_trans_t
*tp
, /* transaction pointer */
990 xfs_inode_t
*ip
, /* incore inode pointer */
991 xfs_fsblock_t
*firstblock
, /* first block allocated */
992 struct xfs_defer_ops
*dfops
, /* blocks to free at commit */
993 int *flags
) /* inode logging flags */
995 xfs_btree_cur_t
*cur
; /* bmap btree cursor */
996 int error
; /* error return value */
998 if (ip
->i_d
.di_nextents
* sizeof(xfs_bmbt_rec_t
) <= XFS_IFORK_DSIZE(ip
))
1001 error
= xfs_bmap_extents_to_btree(tp
, ip
, firstblock
, dfops
, &cur
, 0,
1002 flags
, XFS_DATA_FORK
);
1004 cur
->bc_private
.b
.allocated
= 0;
1005 xfs_btree_del_cursor(cur
,
1006 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
1012 * Called from xfs_bmap_add_attrfork to handle local format files. Each
1013 * different data fork content type needs a different callout to do the
1014 * conversion. Some are basic and only require special block initialisation
1015 * callouts for the data formating, others (directories) are so specialised they
1016 * handle everything themselves.
1018 * XXX (dgc): investigate whether directory conversion can use the generic
1019 * formatting callout. It should be possible - it's just a very complex
1022 STATIC
int /* error */
1023 xfs_bmap_add_attrfork_local(
1024 xfs_trans_t
*tp
, /* transaction pointer */
1025 xfs_inode_t
*ip
, /* incore inode pointer */
1026 xfs_fsblock_t
*firstblock
, /* first block allocated */
1027 struct xfs_defer_ops
*dfops
, /* blocks to free at commit */
1028 int *flags
) /* inode logging flags */
1030 xfs_da_args_t dargs
; /* args for dir/attr code */
1032 if (ip
->i_df
.if_bytes
<= XFS_IFORK_DSIZE(ip
))
1035 if (S_ISDIR(VFS_I(ip
)->i_mode
)) {
1036 memset(&dargs
, 0, sizeof(dargs
));
1037 dargs
.geo
= ip
->i_mount
->m_dir_geo
;
1039 dargs
.firstblock
= firstblock
;
1040 dargs
.dfops
= dfops
;
1041 dargs
.total
= dargs
.geo
->fsbcount
;
1042 dargs
.whichfork
= XFS_DATA_FORK
;
1044 return xfs_dir2_sf_to_block(&dargs
);
1047 if (S_ISLNK(VFS_I(ip
)->i_mode
))
1048 return xfs_bmap_local_to_extents(tp
, ip
, firstblock
, 1,
1049 flags
, XFS_DATA_FORK
,
1050 xfs_symlink_local_to_remote
);
1052 /* should only be called for types that support local format data */
1054 return -EFSCORRUPTED
;
1058 * Convert inode from non-attributed to attributed.
1059 * Must not be in a transaction, ip must not be locked.
1061 int /* error code */
1062 xfs_bmap_add_attrfork(
1063 xfs_inode_t
*ip
, /* incore inode pointer */
1064 int size
, /* space new attribute needs */
1065 int rsvd
) /* xact may use reserved blks */
1067 xfs_fsblock_t firstblock
; /* 1st block/ag allocated */
1068 struct xfs_defer_ops dfops
; /* freed extent records */
1069 xfs_mount_t
*mp
; /* mount structure */
1070 xfs_trans_t
*tp
; /* transaction pointer */
1071 int blks
; /* space reservation */
1072 int version
= 1; /* superblock attr version */
1073 int logflags
; /* logging flags */
1074 int error
; /* error return value */
1076 ASSERT(XFS_IFORK_Q(ip
) == 0);
1079 ASSERT(!XFS_NOT_DQATTACHED(mp
, ip
));
1081 blks
= XFS_ADDAFORK_SPACE_RES(mp
);
1083 error
= xfs_trans_alloc(mp
, &M_RES(mp
)->tr_addafork
, blks
, 0,
1084 rsvd
? XFS_TRANS_RESERVE
: 0, &tp
);
1088 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
1089 error
= xfs_trans_reserve_quota_nblks(tp
, ip
, blks
, 0, rsvd
?
1090 XFS_QMOPT_RES_REGBLKS
| XFS_QMOPT_FORCE_RES
:
1091 XFS_QMOPT_RES_REGBLKS
);
1094 if (XFS_IFORK_Q(ip
))
1096 if (ip
->i_d
.di_aformat
!= XFS_DINODE_FMT_EXTENTS
) {
1098 * For inodes coming from pre-6.2 filesystems.
1100 ASSERT(ip
->i_d
.di_aformat
== 0);
1101 ip
->i_d
.di_aformat
= XFS_DINODE_FMT_EXTENTS
;
1103 ASSERT(ip
->i_d
.di_anextents
== 0);
1105 xfs_trans_ijoin(tp
, ip
, 0);
1106 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
1108 switch (ip
->i_d
.di_format
) {
1109 case XFS_DINODE_FMT_DEV
:
1110 ip
->i_d
.di_forkoff
= roundup(sizeof(xfs_dev_t
), 8) >> 3;
1112 case XFS_DINODE_FMT_UUID
:
1113 ip
->i_d
.di_forkoff
= roundup(sizeof(uuid_t
), 8) >> 3;
1115 case XFS_DINODE_FMT_LOCAL
:
1116 case XFS_DINODE_FMT_EXTENTS
:
1117 case XFS_DINODE_FMT_BTREE
:
1118 ip
->i_d
.di_forkoff
= xfs_attr_shortform_bytesfit(ip
, size
);
1119 if (!ip
->i_d
.di_forkoff
)
1120 ip
->i_d
.di_forkoff
= xfs_default_attroffset(ip
) >> 3;
1121 else if (mp
->m_flags
& XFS_MOUNT_ATTR2
)
1130 ASSERT(ip
->i_afp
== NULL
);
1131 ip
->i_afp
= kmem_zone_zalloc(xfs_ifork_zone
, KM_SLEEP
);
1132 ip
->i_afp
->if_flags
= XFS_IFEXTENTS
;
1134 xfs_defer_init(&dfops
, &firstblock
);
1135 switch (ip
->i_d
.di_format
) {
1136 case XFS_DINODE_FMT_LOCAL
:
1137 error
= xfs_bmap_add_attrfork_local(tp
, ip
, &firstblock
, &dfops
,
1140 case XFS_DINODE_FMT_EXTENTS
:
1141 error
= xfs_bmap_add_attrfork_extents(tp
, ip
, &firstblock
,
1144 case XFS_DINODE_FMT_BTREE
:
1145 error
= xfs_bmap_add_attrfork_btree(tp
, ip
, &firstblock
, &dfops
,
1153 xfs_trans_log_inode(tp
, ip
, logflags
);
1156 if (!xfs_sb_version_hasattr(&mp
->m_sb
) ||
1157 (!xfs_sb_version_hasattr2(&mp
->m_sb
) && version
== 2)) {
1158 bool log_sb
= false;
1160 spin_lock(&mp
->m_sb_lock
);
1161 if (!xfs_sb_version_hasattr(&mp
->m_sb
)) {
1162 xfs_sb_version_addattr(&mp
->m_sb
);
1165 if (!xfs_sb_version_hasattr2(&mp
->m_sb
) && version
== 2) {
1166 xfs_sb_version_addattr2(&mp
->m_sb
);
1169 spin_unlock(&mp
->m_sb_lock
);
1174 error
= xfs_defer_finish(&tp
, &dfops
, NULL
);
1177 error
= xfs_trans_commit(tp
);
1178 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
1182 xfs_defer_cancel(&dfops
);
1184 xfs_trans_cancel(tp
);
1185 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
1190 * Internal and external extent tree search functions.
1194 * Read in the extents to if_extents.
1195 * All inode fields are set up by caller, we just traverse the btree
1196 * and copy the records in. If the file system cannot contain unwritten
1197 * extents, the records are checked for no "state" flags.
1200 xfs_bmap_read_extents(
1201 xfs_trans_t
*tp
, /* transaction pointer */
1202 xfs_inode_t
*ip
, /* incore inode */
1203 int whichfork
) /* data or attr fork */
1205 struct xfs_btree_block
*block
; /* current btree block */
1206 xfs_fsblock_t bno
; /* block # of "block" */
1207 xfs_buf_t
*bp
; /* buffer for "block" */
1208 int error
; /* error return value */
1209 xfs_exntfmt_t exntf
; /* XFS_EXTFMT_NOSTATE, if checking */
1210 xfs_extnum_t i
, j
; /* index into the extents list */
1211 xfs_ifork_t
*ifp
; /* fork structure */
1212 int level
; /* btree level, for checking */
1213 xfs_mount_t
*mp
; /* file system mount structure */
1214 __be64
*pp
; /* pointer to block address */
1216 xfs_extnum_t room
; /* number of entries there's room for */
1220 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1221 exntf
= (whichfork
!= XFS_DATA_FORK
) ? XFS_EXTFMT_NOSTATE
:
1222 XFS_EXTFMT_INODE(ip
);
1223 block
= ifp
->if_broot
;
1225 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
1227 level
= be16_to_cpu(block
->bb_level
);
1229 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, 1, ifp
->if_broot_bytes
);
1230 bno
= be64_to_cpu(*pp
);
1231 ASSERT(bno
!= NULLFSBLOCK
);
1232 ASSERT(XFS_FSB_TO_AGNO(mp
, bno
) < mp
->m_sb
.sb_agcount
);
1233 ASSERT(XFS_FSB_TO_AGBNO(mp
, bno
) < mp
->m_sb
.sb_agblocks
);
1235 * Go down the tree until leaf level is reached, following the first
1236 * pointer (leftmost) at each level.
1238 while (level
-- > 0) {
1239 error
= xfs_btree_read_bufl(mp
, tp
, bno
, 0, &bp
,
1240 XFS_BMAP_BTREE_REF
, &xfs_bmbt_buf_ops
);
1243 block
= XFS_BUF_TO_BLOCK(bp
);
1246 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, mp
->m_bmap_dmxr
[1]);
1247 bno
= be64_to_cpu(*pp
);
1248 XFS_WANT_CORRUPTED_GOTO(mp
,
1249 XFS_FSB_SANITY_CHECK(mp
, bno
), error0
);
1250 xfs_trans_brelse(tp
, bp
);
1253 * Here with bp and block set to the leftmost leaf node in the tree.
1255 room
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
1258 * Loop over all leaf nodes. Copy information to the extent records.
1261 xfs_bmbt_rec_t
*frp
;
1262 xfs_fsblock_t nextbno
;
1263 xfs_extnum_t num_recs
;
1266 num_recs
= xfs_btree_get_numrecs(block
);
1267 if (unlikely(i
+ num_recs
> room
)) {
1268 ASSERT(i
+ num_recs
<= room
);
1269 xfs_warn(ip
->i_mount
,
1270 "corrupt dinode %Lu, (btree extents).",
1271 (unsigned long long) ip
->i_ino
);
1272 XFS_CORRUPTION_ERROR("xfs_bmap_read_extents(1)",
1273 XFS_ERRLEVEL_LOW
, ip
->i_mount
, block
);
1277 * Read-ahead the next leaf block, if any.
1279 nextbno
= be64_to_cpu(block
->bb_u
.l
.bb_rightsib
);
1280 if (nextbno
!= NULLFSBLOCK
)
1281 xfs_btree_reada_bufl(mp
, nextbno
, 1,
1284 * Copy records into the extent records.
1286 frp
= XFS_BMBT_REC_ADDR(mp
, block
, 1);
1288 for (j
= 0; j
< num_recs
; j
++, i
++, frp
++) {
1289 xfs_bmbt_rec_host_t
*trp
= xfs_iext_get_ext(ifp
, i
);
1290 trp
->l0
= be64_to_cpu(frp
->l0
);
1291 trp
->l1
= be64_to_cpu(frp
->l1
);
1293 if (exntf
== XFS_EXTFMT_NOSTATE
) {
1295 * Check all attribute bmap btree records and
1296 * any "older" data bmap btree records for a
1297 * set bit in the "extent flag" position.
1299 if (unlikely(xfs_check_nostate_extents(ifp
,
1300 start
, num_recs
))) {
1301 XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
1307 xfs_trans_brelse(tp
, bp
);
1310 * If we've reached the end, stop.
1312 if (bno
== NULLFSBLOCK
)
1314 error
= xfs_btree_read_bufl(mp
, tp
, bno
, 0, &bp
,
1315 XFS_BMAP_BTREE_REF
, &xfs_bmbt_buf_ops
);
1318 block
= XFS_BUF_TO_BLOCK(bp
);
1320 ASSERT(i
== (ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
)));
1321 ASSERT(i
== XFS_IFORK_NEXTENTS(ip
, whichfork
));
1322 XFS_BMAP_TRACE_EXLIST(ip
, i
, whichfork
);
1325 xfs_trans_brelse(tp
, bp
);
1326 return -EFSCORRUPTED
;
1331 * Search the extent records for the entry containing block bno.
1332 * If bno lies in a hole, point to the next entry. If bno lies
1333 * past eof, *eofp will be set, and *prevp will contain the last
1334 * entry (null if none). Else, *lastxp will be set to the index
1335 * of the found entry; *gotp will contain the entry.
1337 STATIC xfs_bmbt_rec_host_t
* /* pointer to found extent entry */
1338 xfs_bmap_search_multi_extents(
1339 xfs_ifork_t
*ifp
, /* inode fork pointer */
1340 xfs_fileoff_t bno
, /* block number searched for */
1341 int *eofp
, /* out: end of file found */
1342 xfs_extnum_t
*lastxp
, /* out: last extent index */
1343 xfs_bmbt_irec_t
*gotp
, /* out: extent entry found */
1344 xfs_bmbt_irec_t
*prevp
) /* out: previous extent entry found */
1346 xfs_bmbt_rec_host_t
*ep
; /* extent record pointer */
1347 xfs_extnum_t lastx
; /* last extent index */
1350 * Initialize the extent entry structure to catch access to
1351 * uninitialized br_startblock field.
1353 gotp
->br_startoff
= 0xffa5a5a5a5a5a5a5LL
;
1354 gotp
->br_blockcount
= 0xa55a5a5a5a5a5a5aLL
;
1355 gotp
->br_state
= XFS_EXT_INVALID
;
1356 gotp
->br_startblock
= 0xffffa5a5a5a5a5a5LL
;
1357 prevp
->br_startoff
= NULLFILEOFF
;
1359 ep
= xfs_iext_bno_to_ext(ifp
, bno
, &lastx
);
1361 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, lastx
- 1), prevp
);
1363 if (lastx
< (ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
))) {
1364 xfs_bmbt_get_all(ep
, gotp
);
1378 * Search the extents list for the inode, for the extent containing bno.
1379 * If bno lies in a hole, point to the next entry. If bno lies past eof,
1380 * *eofp will be set, and *prevp will contain the last entry (null if none).
1381 * Else, *lastxp will be set to the index of the found
1382 * entry; *gotp will contain the entry.
1384 xfs_bmbt_rec_host_t
* /* pointer to found extent entry */
1385 xfs_bmap_search_extents(
1386 xfs_inode_t
*ip
, /* incore inode pointer */
1387 xfs_fileoff_t bno
, /* block number searched for */
1388 int fork
, /* data or attr fork */
1389 int *eofp
, /* out: end of file found */
1390 xfs_extnum_t
*lastxp
, /* out: last extent index */
1391 xfs_bmbt_irec_t
*gotp
, /* out: extent entry found */
1392 xfs_bmbt_irec_t
*prevp
) /* out: previous extent entry found */
1394 xfs_ifork_t
*ifp
; /* inode fork pointer */
1395 xfs_bmbt_rec_host_t
*ep
; /* extent record pointer */
1397 XFS_STATS_INC(ip
->i_mount
, xs_look_exlist
);
1398 ifp
= XFS_IFORK_PTR(ip
, fork
);
1400 ep
= xfs_bmap_search_multi_extents(ifp
, bno
, eofp
, lastxp
, gotp
, prevp
);
1402 if (unlikely(!(gotp
->br_startblock
) && (*lastxp
!= NULLEXTNUM
) &&
1403 !(XFS_IS_REALTIME_INODE(ip
) && fork
== XFS_DATA_FORK
))) {
1404 xfs_alert_tag(ip
->i_mount
, XFS_PTAG_FSBLOCK_ZERO
,
1405 "Access to block zero in inode %llu "
1406 "start_block: %llx start_off: %llx "
1407 "blkcnt: %llx extent-state: %x lastx: %x",
1408 (unsigned long long)ip
->i_ino
,
1409 (unsigned long long)gotp
->br_startblock
,
1410 (unsigned long long)gotp
->br_startoff
,
1411 (unsigned long long)gotp
->br_blockcount
,
1412 gotp
->br_state
, *lastxp
);
1413 *lastxp
= NULLEXTNUM
;
1421 * Returns the file-relative block number of the first unused block(s)
1422 * in the file with at least "len" logically contiguous blocks free.
1423 * This is the lowest-address hole if the file has holes, else the first block
1424 * past the end of file.
1425 * Return 0 if the file is currently local (in-inode).
1428 xfs_bmap_first_unused(
1429 xfs_trans_t
*tp
, /* transaction pointer */
1430 xfs_inode_t
*ip
, /* incore inode */
1431 xfs_extlen_t len
, /* size of hole to find */
1432 xfs_fileoff_t
*first_unused
, /* unused block */
1433 int whichfork
) /* data or attr fork */
1435 int error
; /* error return value */
1436 int idx
; /* extent record index */
1437 xfs_ifork_t
*ifp
; /* inode fork pointer */
1438 xfs_fileoff_t lastaddr
; /* last block number seen */
1439 xfs_fileoff_t lowest
; /* lowest useful block */
1440 xfs_fileoff_t max
; /* starting useful block */
1441 xfs_fileoff_t off
; /* offset for this block */
1442 xfs_extnum_t nextents
; /* number of extent entries */
1444 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
||
1445 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
||
1446 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
1447 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
) {
1451 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1452 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
1453 (error
= xfs_iread_extents(tp
, ip
, whichfork
)))
1455 lowest
= *first_unused
;
1456 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
1457 for (idx
= 0, lastaddr
= 0, max
= lowest
; idx
< nextents
; idx
++) {
1458 xfs_bmbt_rec_host_t
*ep
= xfs_iext_get_ext(ifp
, idx
);
1459 off
= xfs_bmbt_get_startoff(ep
);
1461 * See if the hole before this extent will work.
1463 if (off
>= lowest
+ len
&& off
- max
>= len
) {
1464 *first_unused
= max
;
1467 lastaddr
= off
+ xfs_bmbt_get_blockcount(ep
);
1468 max
= XFS_FILEOFF_MAX(lastaddr
, lowest
);
1470 *first_unused
= max
;
1475 * Returns the file-relative block number of the last block - 1 before
1476 * last_block (input value) in the file.
1477 * This is not based on i_size, it is based on the extent records.
1478 * Returns 0 for local files, as they do not have extent records.
1481 xfs_bmap_last_before(
1482 xfs_trans_t
*tp
, /* transaction pointer */
1483 xfs_inode_t
*ip
, /* incore inode */
1484 xfs_fileoff_t
*last_block
, /* last block */
1485 int whichfork
) /* data or attr fork */
1487 xfs_fileoff_t bno
; /* input file offset */
1488 int eof
; /* hit end of file */
1489 xfs_bmbt_rec_host_t
*ep
; /* pointer to last extent */
1490 int error
; /* error return value */
1491 xfs_bmbt_irec_t got
; /* current extent value */
1492 xfs_ifork_t
*ifp
; /* inode fork pointer */
1493 xfs_extnum_t lastx
; /* last extent used */
1494 xfs_bmbt_irec_t prev
; /* previous extent value */
1496 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
&&
1497 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
1498 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_LOCAL
)
1500 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
) {
1504 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1505 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
1506 (error
= xfs_iread_extents(tp
, ip
, whichfork
)))
1508 bno
= *last_block
- 1;
1509 ep
= xfs_bmap_search_extents(ip
, bno
, whichfork
, &eof
, &lastx
, &got
,
1511 if (eof
|| xfs_bmbt_get_startoff(ep
) > bno
) {
1512 if (prev
.br_startoff
== NULLFILEOFF
)
1515 *last_block
= prev
.br_startoff
+ prev
.br_blockcount
;
1518 * Otherwise *last_block is already the right answer.
1524 xfs_bmap_last_extent(
1525 struct xfs_trans
*tp
,
1526 struct xfs_inode
*ip
,
1528 struct xfs_bmbt_irec
*rec
,
1531 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1535 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
1536 error
= xfs_iread_extents(tp
, ip
, whichfork
);
1541 nextents
= ifp
->if_bytes
/ sizeof(xfs_bmbt_rec_t
);
1542 if (nextents
== 0) {
1547 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, nextents
- 1), rec
);
1553 * Check the last inode extent to determine whether this allocation will result
1554 * in blocks being allocated at the end of the file. When we allocate new data
1555 * blocks at the end of the file which do not start at the previous data block,
1556 * we will try to align the new blocks at stripe unit boundaries.
1558 * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be
1559 * at, or past the EOF.
1563 struct xfs_bmalloca
*bma
,
1566 struct xfs_bmbt_irec rec
;
1571 error
= xfs_bmap_last_extent(NULL
, bma
->ip
, whichfork
, &rec
,
1582 * Check if we are allocation or past the last extent, or at least into
1583 * the last delayed allocated extent.
1585 bma
->aeof
= bma
->offset
>= rec
.br_startoff
+ rec
.br_blockcount
||
1586 (bma
->offset
>= rec
.br_startoff
&&
1587 isnullstartblock(rec
.br_startblock
));
1592 * Returns the file-relative block number of the first block past eof in
1593 * the file. This is not based on i_size, it is based on the extent records.
1594 * Returns 0 for local files, as they do not have extent records.
1597 xfs_bmap_last_offset(
1598 struct xfs_inode
*ip
,
1599 xfs_fileoff_t
*last_block
,
1602 struct xfs_bmbt_irec rec
;
1608 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
)
1611 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
&&
1612 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
1615 error
= xfs_bmap_last_extent(NULL
, ip
, whichfork
, &rec
, &is_empty
);
1616 if (error
|| is_empty
)
1619 *last_block
= rec
.br_startoff
+ rec
.br_blockcount
;
1624 * Returns whether the selected fork of the inode has exactly one
1625 * block or not. For the data fork we check this matches di_size,
1626 * implying the file's range is 0..bsize-1.
1628 int /* 1=>1 block, 0=>otherwise */
1630 xfs_inode_t
*ip
, /* incore inode */
1631 int whichfork
) /* data or attr fork */
1633 xfs_bmbt_rec_host_t
*ep
; /* ptr to fork's extent */
1634 xfs_ifork_t
*ifp
; /* inode fork pointer */
1635 int rval
; /* return value */
1636 xfs_bmbt_irec_t s
; /* internal version of extent */
1639 if (whichfork
== XFS_DATA_FORK
)
1640 return XFS_ISIZE(ip
) == ip
->i_mount
->m_sb
.sb_blocksize
;
1642 if (XFS_IFORK_NEXTENTS(ip
, whichfork
) != 1)
1644 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
1646 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1647 ASSERT(ifp
->if_flags
& XFS_IFEXTENTS
);
1648 ep
= xfs_iext_get_ext(ifp
, 0);
1649 xfs_bmbt_get_all(ep
, &s
);
1650 rval
= s
.br_startoff
== 0 && s
.br_blockcount
== 1;
1651 if (rval
&& whichfork
== XFS_DATA_FORK
)
1652 ASSERT(XFS_ISIZE(ip
) == ip
->i_mount
->m_sb
.sb_blocksize
);
1657 * Extent tree manipulation functions used during allocation.
1661 * Convert a delayed allocation to a real allocation.
1663 STATIC
int /* error */
1664 xfs_bmap_add_extent_delay_real(
1665 struct xfs_bmalloca
*bma
)
1667 struct xfs_bmbt_irec
*new = &bma
->got
;
1668 int diff
; /* temp value */
1669 xfs_bmbt_rec_host_t
*ep
; /* extent entry for idx */
1670 int error
; /* error return value */
1671 int i
; /* temp state */
1672 xfs_ifork_t
*ifp
; /* inode fork pointer */
1673 xfs_fileoff_t new_endoff
; /* end offset of new entry */
1674 xfs_bmbt_irec_t r
[3]; /* neighbor extent entries */
1675 /* left is 0, right is 1, prev is 2 */
1676 int rval
=0; /* return value (logging flags) */
1677 int state
= 0;/* state bits, accessed thru macros */
1678 xfs_filblks_t da_new
; /* new count del alloc blocks used */
1679 xfs_filblks_t da_old
; /* old count del alloc blocks used */
1680 xfs_filblks_t temp
=0; /* value for da_new calculations */
1681 xfs_filblks_t temp2
=0;/* value for da_new calculations */
1682 int tmp_rval
; /* partial logging flags */
1683 struct xfs_mount
*mp
;
1684 int whichfork
= XFS_DATA_FORK
;
1686 mp
= bma
->ip
->i_mount
;
1687 ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
1689 ASSERT(bma
->idx
>= 0);
1690 ASSERT(bma
->idx
<= ifp
->if_bytes
/ sizeof(struct xfs_bmbt_rec
));
1691 ASSERT(!isnullstartblock(new->br_startblock
));
1693 (bma
->cur
->bc_private
.b
.flags
& XFS_BTCUR_BPRV_WASDEL
));
1695 XFS_STATS_INC(mp
, xs_add_exlist
);
1702 * Set up a bunch of variables to make the tests simpler.
1704 ep
= xfs_iext_get_ext(ifp
, bma
->idx
);
1705 xfs_bmbt_get_all(ep
, &PREV
);
1706 new_endoff
= new->br_startoff
+ new->br_blockcount
;
1707 ASSERT(PREV
.br_startoff
<= new->br_startoff
);
1708 ASSERT(PREV
.br_startoff
+ PREV
.br_blockcount
>= new_endoff
);
1710 da_old
= startblockval(PREV
.br_startblock
);
1714 * Set flags determining what part of the previous delayed allocation
1715 * extent is being replaced by a real allocation.
1717 if (PREV
.br_startoff
== new->br_startoff
)
1718 state
|= BMAP_LEFT_FILLING
;
1719 if (PREV
.br_startoff
+ PREV
.br_blockcount
== new_endoff
)
1720 state
|= BMAP_RIGHT_FILLING
;
1723 * Check and set flags if this segment has a left neighbor.
1724 * Don't set contiguous if the combined extent would be too large.
1727 state
|= BMAP_LEFT_VALID
;
1728 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
- 1), &LEFT
);
1730 if (isnullstartblock(LEFT
.br_startblock
))
1731 state
|= BMAP_LEFT_DELAY
;
1734 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
1735 LEFT
.br_startoff
+ LEFT
.br_blockcount
== new->br_startoff
&&
1736 LEFT
.br_startblock
+ LEFT
.br_blockcount
== new->br_startblock
&&
1737 LEFT
.br_state
== new->br_state
&&
1738 LEFT
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
1739 state
|= BMAP_LEFT_CONTIG
;
1742 * Check and set flags if this segment has a right neighbor.
1743 * Don't set contiguous if the combined extent would be too large.
1744 * Also check for all-three-contiguous being too large.
1746 if (bma
->idx
< ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
) - 1) {
1747 state
|= BMAP_RIGHT_VALID
;
1748 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
+ 1), &RIGHT
);
1750 if (isnullstartblock(RIGHT
.br_startblock
))
1751 state
|= BMAP_RIGHT_DELAY
;
1754 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
1755 new_endoff
== RIGHT
.br_startoff
&&
1756 new->br_startblock
+ new->br_blockcount
== RIGHT
.br_startblock
&&
1757 new->br_state
== RIGHT
.br_state
&&
1758 new->br_blockcount
+ RIGHT
.br_blockcount
<= MAXEXTLEN
&&
1759 ((state
& (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
1760 BMAP_RIGHT_FILLING
)) !=
1761 (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
1762 BMAP_RIGHT_FILLING
) ||
1763 LEFT
.br_blockcount
+ new->br_blockcount
+ RIGHT
.br_blockcount
1765 state
|= BMAP_RIGHT_CONTIG
;
1769 * Switch out based on the FILLING and CONTIG state bits.
1771 switch (state
& (BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
1772 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
)) {
1773 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
1774 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
1776 * Filling in all of a previously delayed allocation extent.
1777 * The left and right neighbors are both contiguous with new.
1780 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1781 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, bma
->idx
),
1782 LEFT
.br_blockcount
+ PREV
.br_blockcount
+
1783 RIGHT
.br_blockcount
);
1784 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1786 xfs_iext_remove(bma
->ip
, bma
->idx
+ 1, 2, state
);
1787 bma
->ip
->i_d
.di_nextents
--;
1788 if (bma
->cur
== NULL
)
1789 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1791 rval
= XFS_ILOG_CORE
;
1792 error
= xfs_bmbt_lookup_eq(bma
->cur
, RIGHT
.br_startoff
,
1793 RIGHT
.br_startblock
,
1794 RIGHT
.br_blockcount
, &i
);
1797 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1798 error
= xfs_btree_delete(bma
->cur
, &i
);
1801 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1802 error
= xfs_btree_decrement(bma
->cur
, 0, &i
);
1805 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1806 error
= xfs_bmbt_update(bma
->cur
, LEFT
.br_startoff
,
1808 LEFT
.br_blockcount
+
1809 PREV
.br_blockcount
+
1810 RIGHT
.br_blockcount
, LEFT
.br_state
);
1816 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
1818 * Filling in all of a previously delayed allocation extent.
1819 * The left neighbor is contiguous, the right is not.
1823 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1824 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, bma
->idx
),
1825 LEFT
.br_blockcount
+ PREV
.br_blockcount
);
1826 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1828 xfs_iext_remove(bma
->ip
, bma
->idx
+ 1, 1, state
);
1829 if (bma
->cur
== NULL
)
1830 rval
= XFS_ILOG_DEXT
;
1833 error
= xfs_bmbt_lookup_eq(bma
->cur
, LEFT
.br_startoff
,
1834 LEFT
.br_startblock
, LEFT
.br_blockcount
,
1838 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1839 error
= xfs_bmbt_update(bma
->cur
, LEFT
.br_startoff
,
1841 LEFT
.br_blockcount
+
1842 PREV
.br_blockcount
, LEFT
.br_state
);
1848 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
1850 * Filling in all of a previously delayed allocation extent.
1851 * The right neighbor is contiguous, the left is not.
1853 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1854 xfs_bmbt_set_startblock(ep
, new->br_startblock
);
1855 xfs_bmbt_set_blockcount(ep
,
1856 PREV
.br_blockcount
+ RIGHT
.br_blockcount
);
1857 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1859 xfs_iext_remove(bma
->ip
, bma
->idx
+ 1, 1, state
);
1860 if (bma
->cur
== NULL
)
1861 rval
= XFS_ILOG_DEXT
;
1864 error
= xfs_bmbt_lookup_eq(bma
->cur
, RIGHT
.br_startoff
,
1865 RIGHT
.br_startblock
,
1866 RIGHT
.br_blockcount
, &i
);
1869 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1870 error
= xfs_bmbt_update(bma
->cur
, PREV
.br_startoff
,
1872 PREV
.br_blockcount
+
1873 RIGHT
.br_blockcount
, PREV
.br_state
);
1879 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
1881 * Filling in all of a previously delayed allocation extent.
1882 * Neither the left nor right neighbors are contiguous with
1885 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1886 xfs_bmbt_set_startblock(ep
, new->br_startblock
);
1887 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1889 bma
->ip
->i_d
.di_nextents
++;
1890 if (bma
->cur
== NULL
)
1891 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1893 rval
= XFS_ILOG_CORE
;
1894 error
= xfs_bmbt_lookup_eq(bma
->cur
, new->br_startoff
,
1895 new->br_startblock
, new->br_blockcount
,
1899 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
1900 bma
->cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
1901 error
= xfs_btree_insert(bma
->cur
, &i
);
1904 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1908 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
:
1910 * Filling in the first part of a previous delayed allocation.
1911 * The left neighbor is contiguous.
1913 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
- 1, state
, _THIS_IP_
);
1914 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, bma
->idx
- 1),
1915 LEFT
.br_blockcount
+ new->br_blockcount
);
1916 xfs_bmbt_set_startoff(ep
,
1917 PREV
.br_startoff
+ new->br_blockcount
);
1918 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
- 1, state
, _THIS_IP_
);
1920 temp
= PREV
.br_blockcount
- new->br_blockcount
;
1921 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1922 xfs_bmbt_set_blockcount(ep
, temp
);
1923 if (bma
->cur
== NULL
)
1924 rval
= XFS_ILOG_DEXT
;
1927 error
= xfs_bmbt_lookup_eq(bma
->cur
, LEFT
.br_startoff
,
1928 LEFT
.br_startblock
, LEFT
.br_blockcount
,
1932 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1933 error
= xfs_bmbt_update(bma
->cur
, LEFT
.br_startoff
,
1935 LEFT
.br_blockcount
+
1941 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
1942 startblockval(PREV
.br_startblock
));
1943 xfs_bmbt_set_startblock(ep
, nullstartblock(da_new
));
1944 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1949 case BMAP_LEFT_FILLING
:
1951 * Filling in the first part of a previous delayed allocation.
1952 * The left neighbor is not contiguous.
1954 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
1955 xfs_bmbt_set_startoff(ep
, new_endoff
);
1956 temp
= PREV
.br_blockcount
- new->br_blockcount
;
1957 xfs_bmbt_set_blockcount(ep
, temp
);
1958 xfs_iext_insert(bma
->ip
, bma
->idx
, 1, new, state
);
1959 bma
->ip
->i_d
.di_nextents
++;
1960 if (bma
->cur
== NULL
)
1961 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1963 rval
= XFS_ILOG_CORE
;
1964 error
= xfs_bmbt_lookup_eq(bma
->cur
, new->br_startoff
,
1965 new->br_startblock
, new->br_blockcount
,
1969 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
1970 bma
->cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
1971 error
= xfs_btree_insert(bma
->cur
, &i
);
1974 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1977 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
1978 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
1979 bma
->firstblock
, bma
->dfops
,
1980 &bma
->cur
, 1, &tmp_rval
, whichfork
);
1985 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
1986 startblockval(PREV
.br_startblock
) -
1987 (bma
->cur
? bma
->cur
->bc_private
.b
.allocated
: 0));
1988 ep
= xfs_iext_get_ext(ifp
, bma
->idx
+ 1);
1989 xfs_bmbt_set_startblock(ep
, nullstartblock(da_new
));
1990 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
+ 1, state
, _THIS_IP_
);
1993 case BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
1995 * Filling in the last part of a previous delayed allocation.
1996 * The right neighbor is contiguous with the new allocation.
1998 temp
= PREV
.br_blockcount
- new->br_blockcount
;
1999 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
+ 1, state
, _THIS_IP_
);
2000 xfs_bmbt_set_blockcount(ep
, temp
);
2001 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp
, bma
->idx
+ 1),
2002 new->br_startoff
, new->br_startblock
,
2003 new->br_blockcount
+ RIGHT
.br_blockcount
,
2005 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
+ 1, state
, _THIS_IP_
);
2006 if (bma
->cur
== NULL
)
2007 rval
= XFS_ILOG_DEXT
;
2010 error
= xfs_bmbt_lookup_eq(bma
->cur
, RIGHT
.br_startoff
,
2011 RIGHT
.br_startblock
,
2012 RIGHT
.br_blockcount
, &i
);
2015 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2016 error
= xfs_bmbt_update(bma
->cur
, new->br_startoff
,
2018 new->br_blockcount
+
2019 RIGHT
.br_blockcount
,
2025 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
2026 startblockval(PREV
.br_startblock
));
2027 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2028 xfs_bmbt_set_startblock(ep
, nullstartblock(da_new
));
2029 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2034 case BMAP_RIGHT_FILLING
:
2036 * Filling in the last part of a previous delayed allocation.
2037 * The right neighbor is not contiguous.
2039 temp
= PREV
.br_blockcount
- new->br_blockcount
;
2040 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2041 xfs_bmbt_set_blockcount(ep
, temp
);
2042 xfs_iext_insert(bma
->ip
, bma
->idx
+ 1, 1, new, state
);
2043 bma
->ip
->i_d
.di_nextents
++;
2044 if (bma
->cur
== NULL
)
2045 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2047 rval
= XFS_ILOG_CORE
;
2048 error
= xfs_bmbt_lookup_eq(bma
->cur
, new->br_startoff
,
2049 new->br_startblock
, new->br_blockcount
,
2053 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
2054 bma
->cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
2055 error
= xfs_btree_insert(bma
->cur
, &i
);
2058 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2061 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
2062 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
2063 bma
->firstblock
, bma
->dfops
, &bma
->cur
, 1,
2064 &tmp_rval
, whichfork
);
2069 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
2070 startblockval(PREV
.br_startblock
) -
2071 (bma
->cur
? bma
->cur
->bc_private
.b
.allocated
: 0));
2072 ep
= xfs_iext_get_ext(ifp
, bma
->idx
);
2073 xfs_bmbt_set_startblock(ep
, nullstartblock(da_new
));
2074 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2081 * Filling in the middle part of a previous delayed allocation.
2082 * Contiguity is impossible here.
2083 * This case is avoided almost all the time.
2085 * We start with a delayed allocation:
2087 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
2090 * and we are allocating:
2091 * +rrrrrrrrrrrrrrrrr+
2094 * and we set it up for insertion as:
2095 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
2097 * PREV @ idx LEFT RIGHT
2098 * inserted at idx + 1
2100 temp
= new->br_startoff
- PREV
.br_startoff
;
2101 temp2
= PREV
.br_startoff
+ PREV
.br_blockcount
- new_endoff
;
2102 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, 0, _THIS_IP_
);
2103 xfs_bmbt_set_blockcount(ep
, temp
); /* truncate PREV */
2105 RIGHT
.br_state
= PREV
.br_state
;
2106 RIGHT
.br_startblock
= nullstartblock(
2107 (int)xfs_bmap_worst_indlen(bma
->ip
, temp2
));
2108 RIGHT
.br_startoff
= new_endoff
;
2109 RIGHT
.br_blockcount
= temp2
;
2110 /* insert LEFT (r[0]) and RIGHT (r[1]) at the same time */
2111 xfs_iext_insert(bma
->ip
, bma
->idx
+ 1, 2, &LEFT
, state
);
2112 bma
->ip
->i_d
.di_nextents
++;
2113 if (bma
->cur
== NULL
)
2114 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2116 rval
= XFS_ILOG_CORE
;
2117 error
= xfs_bmbt_lookup_eq(bma
->cur
, new->br_startoff
,
2118 new->br_startblock
, new->br_blockcount
,
2122 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
2123 bma
->cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
2124 error
= xfs_btree_insert(bma
->cur
, &i
);
2127 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2130 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
2131 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
2132 bma
->firstblock
, bma
->dfops
, &bma
->cur
,
2133 1, &tmp_rval
, whichfork
);
2138 temp
= xfs_bmap_worst_indlen(bma
->ip
, temp
);
2139 temp2
= xfs_bmap_worst_indlen(bma
->ip
, temp2
);
2140 diff
= (int)(temp
+ temp2
- startblockval(PREV
.br_startblock
) -
2141 (bma
->cur
? bma
->cur
->bc_private
.b
.allocated
: 0));
2143 error
= xfs_mod_fdblocks(bma
->ip
->i_mount
,
2144 -((int64_t)diff
), false);
2150 ep
= xfs_iext_get_ext(ifp
, bma
->idx
);
2151 xfs_bmbt_set_startblock(ep
, nullstartblock((int)temp
));
2152 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2153 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
+ 2, state
, _THIS_IP_
);
2154 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp
, bma
->idx
+ 2),
2155 nullstartblock((int)temp2
));
2156 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
+ 2, state
, _THIS_IP_
);
2159 da_new
= temp
+ temp2
;
2162 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2163 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2164 case BMAP_LEFT_FILLING
| BMAP_RIGHT_CONTIG
:
2165 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
2166 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2167 case BMAP_LEFT_CONTIG
:
2168 case BMAP_RIGHT_CONTIG
:
2170 * These cases are all impossible.
2175 /* add reverse mapping */
2176 error
= xfs_rmap_map_extent(mp
, bma
->dfops
, bma
->ip
, whichfork
, new);
2180 /* convert to a btree if necessary */
2181 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
2182 int tmp_logflags
; /* partial log flag return val */
2184 ASSERT(bma
->cur
== NULL
);
2185 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
2186 bma
->firstblock
, bma
->dfops
, &bma
->cur
,
2187 da_old
> 0, &tmp_logflags
, whichfork
);
2188 bma
->logflags
|= tmp_logflags
;
2193 /* adjust for changes in reserved delayed indirect blocks */
2194 if (da_old
|| da_new
) {
2197 temp
+= bma
->cur
->bc_private
.b
.allocated
;
2198 ASSERT(temp
<= da_old
);
2200 xfs_mod_fdblocks(bma
->ip
->i_mount
,
2201 (int64_t)(da_old
- temp
), false);
2204 /* clear out the allocated field, done with it now in any case. */
2206 bma
->cur
->bc_private
.b
.allocated
= 0;
2208 xfs_bmap_check_leaf_extents(bma
->cur
, bma
->ip
, whichfork
);
2210 bma
->logflags
|= rval
;
2218 * Convert an unwritten allocation to a real allocation or vice versa.
2220 STATIC
int /* error */
2221 xfs_bmap_add_extent_unwritten_real(
2222 struct xfs_trans
*tp
,
2223 xfs_inode_t
*ip
, /* incore inode pointer */
2224 xfs_extnum_t
*idx
, /* extent number to update/insert */
2225 xfs_btree_cur_t
**curp
, /* if *curp is null, not a btree */
2226 xfs_bmbt_irec_t
*new, /* new data to add to file extents */
2227 xfs_fsblock_t
*first
, /* pointer to firstblock variable */
2228 struct xfs_defer_ops
*dfops
, /* list of extents to be freed */
2229 int *logflagsp
) /* inode logging flags */
2231 xfs_btree_cur_t
*cur
; /* btree cursor */
2232 xfs_bmbt_rec_host_t
*ep
; /* extent entry for idx */
2233 int error
; /* error return value */
2234 int i
; /* temp state */
2235 xfs_ifork_t
*ifp
; /* inode fork pointer */
2236 xfs_fileoff_t new_endoff
; /* end offset of new entry */
2237 xfs_exntst_t newext
; /* new extent state */
2238 xfs_exntst_t oldext
; /* old extent state */
2239 xfs_bmbt_irec_t r
[3]; /* neighbor extent entries */
2240 /* left is 0, right is 1, prev is 2 */
2241 int rval
=0; /* return value (logging flags) */
2242 int state
= 0;/* state bits, accessed thru macros */
2243 struct xfs_mount
*mp
= tp
->t_mountp
;
2248 ifp
= XFS_IFORK_PTR(ip
, XFS_DATA_FORK
);
2251 ASSERT(*idx
<= ifp
->if_bytes
/ sizeof(struct xfs_bmbt_rec
));
2252 ASSERT(!isnullstartblock(new->br_startblock
));
2254 XFS_STATS_INC(mp
, xs_add_exlist
);
2261 * Set up a bunch of variables to make the tests simpler.
2264 ep
= xfs_iext_get_ext(ifp
, *idx
);
2265 xfs_bmbt_get_all(ep
, &PREV
);
2266 newext
= new->br_state
;
2267 oldext
= (newext
== XFS_EXT_UNWRITTEN
) ?
2268 XFS_EXT_NORM
: XFS_EXT_UNWRITTEN
;
2269 ASSERT(PREV
.br_state
== oldext
);
2270 new_endoff
= new->br_startoff
+ new->br_blockcount
;
2271 ASSERT(PREV
.br_startoff
<= new->br_startoff
);
2272 ASSERT(PREV
.br_startoff
+ PREV
.br_blockcount
>= new_endoff
);
2275 * Set flags determining what part of the previous oldext allocation
2276 * extent is being replaced by a newext allocation.
2278 if (PREV
.br_startoff
== new->br_startoff
)
2279 state
|= BMAP_LEFT_FILLING
;
2280 if (PREV
.br_startoff
+ PREV
.br_blockcount
== new_endoff
)
2281 state
|= BMAP_RIGHT_FILLING
;
2284 * Check and set flags if this segment has a left neighbor.
2285 * Don't set contiguous if the combined extent would be too large.
2288 state
|= BMAP_LEFT_VALID
;
2289 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, *idx
- 1), &LEFT
);
2291 if (isnullstartblock(LEFT
.br_startblock
))
2292 state
|= BMAP_LEFT_DELAY
;
2295 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
2296 LEFT
.br_startoff
+ LEFT
.br_blockcount
== new->br_startoff
&&
2297 LEFT
.br_startblock
+ LEFT
.br_blockcount
== new->br_startblock
&&
2298 LEFT
.br_state
== newext
&&
2299 LEFT
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
2300 state
|= BMAP_LEFT_CONTIG
;
2303 * Check and set flags if this segment has a right neighbor.
2304 * Don't set contiguous if the combined extent would be too large.
2305 * Also check for all-three-contiguous being too large.
2307 if (*idx
< ip
->i_df
.if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
) - 1) {
2308 state
|= BMAP_RIGHT_VALID
;
2309 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, *idx
+ 1), &RIGHT
);
2310 if (isnullstartblock(RIGHT
.br_startblock
))
2311 state
|= BMAP_RIGHT_DELAY
;
2314 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
2315 new_endoff
== RIGHT
.br_startoff
&&
2316 new->br_startblock
+ new->br_blockcount
== RIGHT
.br_startblock
&&
2317 newext
== RIGHT
.br_state
&&
2318 new->br_blockcount
+ RIGHT
.br_blockcount
<= MAXEXTLEN
&&
2319 ((state
& (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
2320 BMAP_RIGHT_FILLING
)) !=
2321 (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
2322 BMAP_RIGHT_FILLING
) ||
2323 LEFT
.br_blockcount
+ new->br_blockcount
+ RIGHT
.br_blockcount
2325 state
|= BMAP_RIGHT_CONTIG
;
2328 * Switch out based on the FILLING and CONTIG state bits.
2330 switch (state
& (BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
2331 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
)) {
2332 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
2333 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2335 * Setting all of a previous oldext extent to newext.
2336 * The left and right neighbors are both contiguous with new.
2340 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2341 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
),
2342 LEFT
.br_blockcount
+ PREV
.br_blockcount
+
2343 RIGHT
.br_blockcount
);
2344 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2346 xfs_iext_remove(ip
, *idx
+ 1, 2, state
);
2347 ip
->i_d
.di_nextents
-= 2;
2349 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2351 rval
= XFS_ILOG_CORE
;
2352 if ((error
= xfs_bmbt_lookup_eq(cur
, RIGHT
.br_startoff
,
2353 RIGHT
.br_startblock
,
2354 RIGHT
.br_blockcount
, &i
)))
2356 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2357 if ((error
= xfs_btree_delete(cur
, &i
)))
2359 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2360 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2362 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2363 if ((error
= xfs_btree_delete(cur
, &i
)))
2365 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2366 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2368 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2369 if ((error
= xfs_bmbt_update(cur
, LEFT
.br_startoff
,
2371 LEFT
.br_blockcount
+ PREV
.br_blockcount
+
2372 RIGHT
.br_blockcount
, LEFT
.br_state
)))
2377 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
2379 * Setting all of a previous oldext extent to newext.
2380 * The left neighbor is contiguous, the right is not.
2384 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2385 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
),
2386 LEFT
.br_blockcount
+ PREV
.br_blockcount
);
2387 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2389 xfs_iext_remove(ip
, *idx
+ 1, 1, state
);
2390 ip
->i_d
.di_nextents
--;
2392 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2394 rval
= XFS_ILOG_CORE
;
2395 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2396 PREV
.br_startblock
, PREV
.br_blockcount
,
2399 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2400 if ((error
= xfs_btree_delete(cur
, &i
)))
2402 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2403 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2405 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2406 if ((error
= xfs_bmbt_update(cur
, LEFT
.br_startoff
,
2408 LEFT
.br_blockcount
+ PREV
.br_blockcount
,
2414 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2416 * Setting all of a previous oldext extent to newext.
2417 * The right neighbor is contiguous, the left is not.
2419 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2420 xfs_bmbt_set_blockcount(ep
,
2421 PREV
.br_blockcount
+ RIGHT
.br_blockcount
);
2422 xfs_bmbt_set_state(ep
, newext
);
2423 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2424 xfs_iext_remove(ip
, *idx
+ 1, 1, state
);
2425 ip
->i_d
.di_nextents
--;
2427 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2429 rval
= XFS_ILOG_CORE
;
2430 if ((error
= xfs_bmbt_lookup_eq(cur
, RIGHT
.br_startoff
,
2431 RIGHT
.br_startblock
,
2432 RIGHT
.br_blockcount
, &i
)))
2434 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2435 if ((error
= xfs_btree_delete(cur
, &i
)))
2437 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2438 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2440 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2441 if ((error
= xfs_bmbt_update(cur
, new->br_startoff
,
2443 new->br_blockcount
+ RIGHT
.br_blockcount
,
2449 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
2451 * Setting all of a previous oldext extent to newext.
2452 * Neither the left nor right neighbors are contiguous with
2455 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2456 xfs_bmbt_set_state(ep
, newext
);
2457 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2460 rval
= XFS_ILOG_DEXT
;
2463 if ((error
= xfs_bmbt_lookup_eq(cur
, new->br_startoff
,
2464 new->br_startblock
, new->br_blockcount
,
2467 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2468 if ((error
= xfs_bmbt_update(cur
, new->br_startoff
,
2469 new->br_startblock
, new->br_blockcount
,
2475 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
:
2477 * Setting the first part of a previous oldext extent to newext.
2478 * The left neighbor is contiguous.
2480 trace_xfs_bmap_pre_update(ip
, *idx
- 1, state
, _THIS_IP_
);
2481 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
- 1),
2482 LEFT
.br_blockcount
+ new->br_blockcount
);
2483 xfs_bmbt_set_startoff(ep
,
2484 PREV
.br_startoff
+ new->br_blockcount
);
2485 trace_xfs_bmap_post_update(ip
, *idx
- 1, state
, _THIS_IP_
);
2487 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2488 xfs_bmbt_set_startblock(ep
,
2489 new->br_startblock
+ new->br_blockcount
);
2490 xfs_bmbt_set_blockcount(ep
,
2491 PREV
.br_blockcount
- new->br_blockcount
);
2492 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2497 rval
= XFS_ILOG_DEXT
;
2500 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2501 PREV
.br_startblock
, PREV
.br_blockcount
,
2504 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2505 if ((error
= xfs_bmbt_update(cur
,
2506 PREV
.br_startoff
+ new->br_blockcount
,
2507 PREV
.br_startblock
+ new->br_blockcount
,
2508 PREV
.br_blockcount
- new->br_blockcount
,
2511 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2513 error
= xfs_bmbt_update(cur
, LEFT
.br_startoff
,
2515 LEFT
.br_blockcount
+ new->br_blockcount
,
2522 case BMAP_LEFT_FILLING
:
2524 * Setting the first part of a previous oldext extent to newext.
2525 * The left neighbor is not contiguous.
2527 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2528 ASSERT(ep
&& xfs_bmbt_get_state(ep
) == oldext
);
2529 xfs_bmbt_set_startoff(ep
, new_endoff
);
2530 xfs_bmbt_set_blockcount(ep
,
2531 PREV
.br_blockcount
- new->br_blockcount
);
2532 xfs_bmbt_set_startblock(ep
,
2533 new->br_startblock
+ new->br_blockcount
);
2534 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2536 xfs_iext_insert(ip
, *idx
, 1, new, state
);
2537 ip
->i_d
.di_nextents
++;
2539 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2541 rval
= XFS_ILOG_CORE
;
2542 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2543 PREV
.br_startblock
, PREV
.br_blockcount
,
2546 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2547 if ((error
= xfs_bmbt_update(cur
,
2548 PREV
.br_startoff
+ new->br_blockcount
,
2549 PREV
.br_startblock
+ new->br_blockcount
,
2550 PREV
.br_blockcount
- new->br_blockcount
,
2553 cur
->bc_rec
.b
= *new;
2554 if ((error
= xfs_btree_insert(cur
, &i
)))
2556 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2560 case BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2562 * Setting the last part of a previous oldext extent to newext.
2563 * The right neighbor is contiguous with the new allocation.
2565 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2566 xfs_bmbt_set_blockcount(ep
,
2567 PREV
.br_blockcount
- new->br_blockcount
);
2568 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2572 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2573 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp
, *idx
),
2574 new->br_startoff
, new->br_startblock
,
2575 new->br_blockcount
+ RIGHT
.br_blockcount
, newext
);
2576 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2579 rval
= XFS_ILOG_DEXT
;
2582 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2584 PREV
.br_blockcount
, &i
)))
2586 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2587 if ((error
= xfs_bmbt_update(cur
, PREV
.br_startoff
,
2589 PREV
.br_blockcount
- new->br_blockcount
,
2592 if ((error
= xfs_btree_increment(cur
, 0, &i
)))
2594 if ((error
= xfs_bmbt_update(cur
, new->br_startoff
,
2596 new->br_blockcount
+ RIGHT
.br_blockcount
,
2602 case BMAP_RIGHT_FILLING
:
2604 * Setting the last part of a previous oldext extent to newext.
2605 * The right neighbor is not contiguous.
2607 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2608 xfs_bmbt_set_blockcount(ep
,
2609 PREV
.br_blockcount
- new->br_blockcount
);
2610 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2613 xfs_iext_insert(ip
, *idx
, 1, new, state
);
2615 ip
->i_d
.di_nextents
++;
2617 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2619 rval
= XFS_ILOG_CORE
;
2620 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2621 PREV
.br_startblock
, PREV
.br_blockcount
,
2624 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2625 if ((error
= xfs_bmbt_update(cur
, PREV
.br_startoff
,
2627 PREV
.br_blockcount
- new->br_blockcount
,
2630 if ((error
= xfs_bmbt_lookup_eq(cur
, new->br_startoff
,
2631 new->br_startblock
, new->br_blockcount
,
2634 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
2635 cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
2636 if ((error
= xfs_btree_insert(cur
, &i
)))
2638 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2644 * Setting the middle part of a previous oldext extent to
2645 * newext. Contiguity is impossible here.
2646 * One extent becomes three extents.
2648 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2649 xfs_bmbt_set_blockcount(ep
,
2650 new->br_startoff
- PREV
.br_startoff
);
2651 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2654 r
[1].br_startoff
= new_endoff
;
2655 r
[1].br_blockcount
=
2656 PREV
.br_startoff
+ PREV
.br_blockcount
- new_endoff
;
2657 r
[1].br_startblock
= new->br_startblock
+ new->br_blockcount
;
2658 r
[1].br_state
= oldext
;
2661 xfs_iext_insert(ip
, *idx
, 2, &r
[0], state
);
2663 ip
->i_d
.di_nextents
+= 2;
2665 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2667 rval
= XFS_ILOG_CORE
;
2668 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
2669 PREV
.br_startblock
, PREV
.br_blockcount
,
2672 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2673 /* new right extent - oldext */
2674 if ((error
= xfs_bmbt_update(cur
, r
[1].br_startoff
,
2675 r
[1].br_startblock
, r
[1].br_blockcount
,
2678 /* new left extent - oldext */
2679 cur
->bc_rec
.b
= PREV
;
2680 cur
->bc_rec
.b
.br_blockcount
=
2681 new->br_startoff
- PREV
.br_startoff
;
2682 if ((error
= xfs_btree_insert(cur
, &i
)))
2684 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2686 * Reset the cursor to the position of the new extent
2687 * we are about to insert as we can't trust it after
2688 * the previous insert.
2690 if ((error
= xfs_bmbt_lookup_eq(cur
, new->br_startoff
,
2691 new->br_startblock
, new->br_blockcount
,
2694 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
2695 /* new middle extent - newext */
2696 cur
->bc_rec
.b
.br_state
= new->br_state
;
2697 if ((error
= xfs_btree_insert(cur
, &i
)))
2699 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2703 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2704 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2705 case BMAP_LEFT_FILLING
| BMAP_RIGHT_CONTIG
:
2706 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
2707 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2708 case BMAP_LEFT_CONTIG
:
2709 case BMAP_RIGHT_CONTIG
:
2711 * These cases are all impossible.
2716 /* update reverse mappings */
2717 error
= xfs_rmap_convert_extent(mp
, dfops
, ip
, XFS_DATA_FORK
, new);
2721 /* convert to a btree if necessary */
2722 if (xfs_bmap_needs_btree(ip
, XFS_DATA_FORK
)) {
2723 int tmp_logflags
; /* partial log flag return val */
2725 ASSERT(cur
== NULL
);
2726 error
= xfs_bmap_extents_to_btree(tp
, ip
, first
, dfops
, &cur
,
2727 0, &tmp_logflags
, XFS_DATA_FORK
);
2728 *logflagsp
|= tmp_logflags
;
2733 /* clear out the allocated field, done with it now in any case. */
2735 cur
->bc_private
.b
.allocated
= 0;
2739 xfs_bmap_check_leaf_extents(*curp
, ip
, XFS_DATA_FORK
);
2749 * Convert a hole to a delayed allocation.
2752 xfs_bmap_add_extent_hole_delay(
2753 xfs_inode_t
*ip
, /* incore inode pointer */
2754 xfs_extnum_t
*idx
, /* extent number to update/insert */
2755 xfs_bmbt_irec_t
*new) /* new data to add to file extents */
2757 xfs_ifork_t
*ifp
; /* inode fork pointer */
2758 xfs_bmbt_irec_t left
; /* left neighbor extent entry */
2759 xfs_filblks_t newlen
=0; /* new indirect size */
2760 xfs_filblks_t oldlen
=0; /* old indirect size */
2761 xfs_bmbt_irec_t right
; /* right neighbor extent entry */
2762 int state
; /* state bits, accessed thru macros */
2763 xfs_filblks_t temp
=0; /* temp for indirect calculations */
2765 ifp
= XFS_IFORK_PTR(ip
, XFS_DATA_FORK
);
2767 ASSERT(isnullstartblock(new->br_startblock
));
2770 * Check and set flags if this segment has a left neighbor
2773 state
|= BMAP_LEFT_VALID
;
2774 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, *idx
- 1), &left
);
2776 if (isnullstartblock(left
.br_startblock
))
2777 state
|= BMAP_LEFT_DELAY
;
2781 * Check and set flags if the current (right) segment exists.
2782 * If it doesn't exist, we're converting the hole at end-of-file.
2784 if (*idx
< ip
->i_df
.if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
)) {
2785 state
|= BMAP_RIGHT_VALID
;
2786 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, *idx
), &right
);
2788 if (isnullstartblock(right
.br_startblock
))
2789 state
|= BMAP_RIGHT_DELAY
;
2793 * Set contiguity flags on the left and right neighbors.
2794 * Don't let extents get too large, even if the pieces are contiguous.
2796 if ((state
& BMAP_LEFT_VALID
) && (state
& BMAP_LEFT_DELAY
) &&
2797 left
.br_startoff
+ left
.br_blockcount
== new->br_startoff
&&
2798 left
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
2799 state
|= BMAP_LEFT_CONTIG
;
2801 if ((state
& BMAP_RIGHT_VALID
) && (state
& BMAP_RIGHT_DELAY
) &&
2802 new->br_startoff
+ new->br_blockcount
== right
.br_startoff
&&
2803 new->br_blockcount
+ right
.br_blockcount
<= MAXEXTLEN
&&
2804 (!(state
& BMAP_LEFT_CONTIG
) ||
2805 (left
.br_blockcount
+ new->br_blockcount
+
2806 right
.br_blockcount
<= MAXEXTLEN
)))
2807 state
|= BMAP_RIGHT_CONTIG
;
2810 * Switch out based on the contiguity flags.
2812 switch (state
& (BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
)) {
2813 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2815 * New allocation is contiguous with delayed allocations
2816 * on the left and on the right.
2817 * Merge all three into a single extent record.
2820 temp
= left
.br_blockcount
+ new->br_blockcount
+
2821 right
.br_blockcount
;
2823 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2824 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
), temp
);
2825 oldlen
= startblockval(left
.br_startblock
) +
2826 startblockval(new->br_startblock
) +
2827 startblockval(right
.br_startblock
);
2828 newlen
= xfs_bmap_worst_indlen(ip
, temp
);
2829 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp
, *idx
),
2830 nullstartblock((int)newlen
));
2831 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2833 xfs_iext_remove(ip
, *idx
+ 1, 1, state
);
2836 case BMAP_LEFT_CONTIG
:
2838 * New allocation is contiguous with a delayed allocation
2840 * Merge the new allocation with the left neighbor.
2843 temp
= left
.br_blockcount
+ new->br_blockcount
;
2845 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2846 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
), temp
);
2847 oldlen
= startblockval(left
.br_startblock
) +
2848 startblockval(new->br_startblock
);
2849 newlen
= xfs_bmap_worst_indlen(ip
, temp
);
2850 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp
, *idx
),
2851 nullstartblock((int)newlen
));
2852 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2855 case BMAP_RIGHT_CONTIG
:
2857 * New allocation is contiguous with a delayed allocation
2859 * Merge the new allocation with the right neighbor.
2861 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2862 temp
= new->br_blockcount
+ right
.br_blockcount
;
2863 oldlen
= startblockval(new->br_startblock
) +
2864 startblockval(right
.br_startblock
);
2865 newlen
= xfs_bmap_worst_indlen(ip
, temp
);
2866 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp
, *idx
),
2868 nullstartblock((int)newlen
), temp
, right
.br_state
);
2869 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2874 * New allocation is not contiguous with another
2875 * delayed allocation.
2876 * Insert a new entry.
2878 oldlen
= newlen
= 0;
2879 xfs_iext_insert(ip
, *idx
, 1, new, state
);
2882 if (oldlen
!= newlen
) {
2883 ASSERT(oldlen
> newlen
);
2884 xfs_mod_fdblocks(ip
->i_mount
, (int64_t)(oldlen
- newlen
),
2887 * Nothing to do for disk quota accounting here.
2893 * Convert a hole to a real allocation.
2895 STATIC
int /* error */
2896 xfs_bmap_add_extent_hole_real(
2897 struct xfs_bmalloca
*bma
,
2900 struct xfs_bmbt_irec
*new = &bma
->got
;
2901 int error
; /* error return value */
2902 int i
; /* temp state */
2903 xfs_ifork_t
*ifp
; /* inode fork pointer */
2904 xfs_bmbt_irec_t left
; /* left neighbor extent entry */
2905 xfs_bmbt_irec_t right
; /* right neighbor extent entry */
2906 int rval
=0; /* return value (logging flags) */
2907 int state
; /* state bits, accessed thru macros */
2908 struct xfs_mount
*mp
;
2910 mp
= bma
->ip
->i_mount
;
2911 ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
2913 ASSERT(bma
->idx
>= 0);
2914 ASSERT(bma
->idx
<= ifp
->if_bytes
/ sizeof(struct xfs_bmbt_rec
));
2915 ASSERT(!isnullstartblock(new->br_startblock
));
2917 !(bma
->cur
->bc_private
.b
.flags
& XFS_BTCUR_BPRV_WASDEL
));
2919 XFS_STATS_INC(mp
, xs_add_exlist
);
2922 if (whichfork
== XFS_ATTR_FORK
)
2923 state
|= BMAP_ATTRFORK
;
2926 * Check and set flags if this segment has a left neighbor.
2929 state
|= BMAP_LEFT_VALID
;
2930 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
- 1), &left
);
2931 if (isnullstartblock(left
.br_startblock
))
2932 state
|= BMAP_LEFT_DELAY
;
2936 * Check and set flags if this segment has a current value.
2937 * Not true if we're inserting into the "hole" at eof.
2939 if (bma
->idx
< ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
)) {
2940 state
|= BMAP_RIGHT_VALID
;
2941 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
), &right
);
2942 if (isnullstartblock(right
.br_startblock
))
2943 state
|= BMAP_RIGHT_DELAY
;
2947 * We're inserting a real allocation between "left" and "right".
2948 * Set the contiguity flags. Don't let extents get too large.
2950 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
2951 left
.br_startoff
+ left
.br_blockcount
== new->br_startoff
&&
2952 left
.br_startblock
+ left
.br_blockcount
== new->br_startblock
&&
2953 left
.br_state
== new->br_state
&&
2954 left
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
2955 state
|= BMAP_LEFT_CONTIG
;
2957 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
2958 new->br_startoff
+ new->br_blockcount
== right
.br_startoff
&&
2959 new->br_startblock
+ new->br_blockcount
== right
.br_startblock
&&
2960 new->br_state
== right
.br_state
&&
2961 new->br_blockcount
+ right
.br_blockcount
<= MAXEXTLEN
&&
2962 (!(state
& BMAP_LEFT_CONTIG
) ||
2963 left
.br_blockcount
+ new->br_blockcount
+
2964 right
.br_blockcount
<= MAXEXTLEN
))
2965 state
|= BMAP_RIGHT_CONTIG
;
2969 * Select which case we're in here, and implement it.
2971 switch (state
& (BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
)) {
2972 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2974 * New allocation is contiguous with real allocations on the
2975 * left and on the right.
2976 * Merge all three into a single extent record.
2979 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2980 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, bma
->idx
),
2981 left
.br_blockcount
+ new->br_blockcount
+
2982 right
.br_blockcount
);
2983 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
2985 xfs_iext_remove(bma
->ip
, bma
->idx
+ 1, 1, state
);
2987 XFS_IFORK_NEXT_SET(bma
->ip
, whichfork
,
2988 XFS_IFORK_NEXTENTS(bma
->ip
, whichfork
) - 1);
2989 if (bma
->cur
== NULL
) {
2990 rval
= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
2992 rval
= XFS_ILOG_CORE
;
2993 error
= xfs_bmbt_lookup_eq(bma
->cur
, right
.br_startoff
,
2994 right
.br_startblock
, right
.br_blockcount
,
2998 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2999 error
= xfs_btree_delete(bma
->cur
, &i
);
3002 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
3003 error
= xfs_btree_decrement(bma
->cur
, 0, &i
);
3006 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
3007 error
= xfs_bmbt_update(bma
->cur
, left
.br_startoff
,
3009 left
.br_blockcount
+
3010 new->br_blockcount
+
3011 right
.br_blockcount
,
3018 case BMAP_LEFT_CONTIG
:
3020 * New allocation is contiguous with a real allocation
3022 * Merge the new allocation with the left neighbor.
3025 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
3026 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, bma
->idx
),
3027 left
.br_blockcount
+ new->br_blockcount
);
3028 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
3030 if (bma
->cur
== NULL
) {
3031 rval
= xfs_ilog_fext(whichfork
);
3034 error
= xfs_bmbt_lookup_eq(bma
->cur
, left
.br_startoff
,
3035 left
.br_startblock
, left
.br_blockcount
,
3039 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
3040 error
= xfs_bmbt_update(bma
->cur
, left
.br_startoff
,
3042 left
.br_blockcount
+
3050 case BMAP_RIGHT_CONTIG
:
3052 * New allocation is contiguous with a real allocation
3054 * Merge the new allocation with the right neighbor.
3056 trace_xfs_bmap_pre_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
3057 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp
, bma
->idx
),
3058 new->br_startoff
, new->br_startblock
,
3059 new->br_blockcount
+ right
.br_blockcount
,
3061 trace_xfs_bmap_post_update(bma
->ip
, bma
->idx
, state
, _THIS_IP_
);
3063 if (bma
->cur
== NULL
) {
3064 rval
= xfs_ilog_fext(whichfork
);
3067 error
= xfs_bmbt_lookup_eq(bma
->cur
,
3069 right
.br_startblock
,
3070 right
.br_blockcount
, &i
);
3073 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
3074 error
= xfs_bmbt_update(bma
->cur
, new->br_startoff
,
3076 new->br_blockcount
+
3077 right
.br_blockcount
,
3086 * New allocation is not contiguous with another
3088 * Insert a new entry.
3090 xfs_iext_insert(bma
->ip
, bma
->idx
, 1, new, state
);
3091 XFS_IFORK_NEXT_SET(bma
->ip
, whichfork
,
3092 XFS_IFORK_NEXTENTS(bma
->ip
, whichfork
) + 1);
3093 if (bma
->cur
== NULL
) {
3094 rval
= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
3096 rval
= XFS_ILOG_CORE
;
3097 error
= xfs_bmbt_lookup_eq(bma
->cur
,
3100 new->br_blockcount
, &i
);
3103 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
3104 bma
->cur
->bc_rec
.b
.br_state
= new->br_state
;
3105 error
= xfs_btree_insert(bma
->cur
, &i
);
3108 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
3113 /* add reverse mapping */
3114 error
= xfs_rmap_map_extent(mp
, bma
->dfops
, bma
->ip
, whichfork
, new);
3118 /* convert to a btree if necessary */
3119 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
3120 int tmp_logflags
; /* partial log flag return val */
3122 ASSERT(bma
->cur
== NULL
);
3123 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
3124 bma
->firstblock
, bma
->dfops
, &bma
->cur
,
3125 0, &tmp_logflags
, whichfork
);
3126 bma
->logflags
|= tmp_logflags
;
3131 /* clear out the allocated field, done with it now in any case. */
3133 bma
->cur
->bc_private
.b
.allocated
= 0;
3135 xfs_bmap_check_leaf_extents(bma
->cur
, bma
->ip
, whichfork
);
3137 bma
->logflags
|= rval
;
3142 * Functions used in the extent read, allocate and remove paths
3146 * Adjust the size of the new extent based on di_extsize and rt extsize.
3149 xfs_bmap_extsize_align(
3151 xfs_bmbt_irec_t
*gotp
, /* next extent pointer */
3152 xfs_bmbt_irec_t
*prevp
, /* previous extent pointer */
3153 xfs_extlen_t extsz
, /* align to this extent size */
3154 int rt
, /* is this a realtime inode? */
3155 int eof
, /* is extent at end-of-file? */
3156 int delay
, /* creating delalloc extent? */
3157 int convert
, /* overwriting unwritten extent? */
3158 xfs_fileoff_t
*offp
, /* in/out: aligned offset */
3159 xfs_extlen_t
*lenp
) /* in/out: aligned length */
3161 xfs_fileoff_t orig_off
; /* original offset */
3162 xfs_extlen_t orig_alen
; /* original length */
3163 xfs_fileoff_t orig_end
; /* original off+len */
3164 xfs_fileoff_t nexto
; /* next file offset */
3165 xfs_fileoff_t prevo
; /* previous file offset */
3166 xfs_fileoff_t align_off
; /* temp for offset */
3167 xfs_extlen_t align_alen
; /* temp for length */
3168 xfs_extlen_t temp
; /* temp for calculations */
3173 orig_off
= align_off
= *offp
;
3174 orig_alen
= align_alen
= *lenp
;
3175 orig_end
= orig_off
+ orig_alen
;
3178 * If this request overlaps an existing extent, then don't
3179 * attempt to perform any additional alignment.
3181 if (!delay
&& !eof
&&
3182 (orig_off
>= gotp
->br_startoff
) &&
3183 (orig_end
<= gotp
->br_startoff
+ gotp
->br_blockcount
)) {
3188 * If the file offset is unaligned vs. the extent size
3189 * we need to align it. This will be possible unless
3190 * the file was previously written with a kernel that didn't
3191 * perform this alignment, or if a truncate shot us in the
3194 temp
= do_mod(orig_off
, extsz
);
3200 /* Same adjustment for the end of the requested area. */
3201 temp
= (align_alen
% extsz
);
3203 align_alen
+= extsz
- temp
;
3206 * For large extent hint sizes, the aligned extent might be larger than
3207 * MAXEXTLEN. In that case, reduce the size by an extsz so that it pulls
3208 * the length back under MAXEXTLEN. The outer allocation loops handle
3209 * short allocation just fine, so it is safe to do this. We only want to
3210 * do it when we are forced to, though, because it means more allocation
3211 * operations are required.
3213 while (align_alen
> MAXEXTLEN
)
3214 align_alen
-= extsz
;
3215 ASSERT(align_alen
<= MAXEXTLEN
);
3218 * If the previous block overlaps with this proposed allocation
3219 * then move the start forward without adjusting the length.
3221 if (prevp
->br_startoff
!= NULLFILEOFF
) {
3222 if (prevp
->br_startblock
== HOLESTARTBLOCK
)
3223 prevo
= prevp
->br_startoff
;
3225 prevo
= prevp
->br_startoff
+ prevp
->br_blockcount
;
3228 if (align_off
!= orig_off
&& align_off
< prevo
)
3231 * If the next block overlaps with this proposed allocation
3232 * then move the start back without adjusting the length,
3233 * but not before offset 0.
3234 * This may of course make the start overlap previous block,
3235 * and if we hit the offset 0 limit then the next block
3236 * can still overlap too.
3238 if (!eof
&& gotp
->br_startoff
!= NULLFILEOFF
) {
3239 if ((delay
&& gotp
->br_startblock
== HOLESTARTBLOCK
) ||
3240 (!delay
&& gotp
->br_startblock
== DELAYSTARTBLOCK
))
3241 nexto
= gotp
->br_startoff
+ gotp
->br_blockcount
;
3243 nexto
= gotp
->br_startoff
;
3245 nexto
= NULLFILEOFF
;
3247 align_off
+ align_alen
!= orig_end
&&
3248 align_off
+ align_alen
> nexto
)
3249 align_off
= nexto
> align_alen
? nexto
- align_alen
: 0;
3251 * If we're now overlapping the next or previous extent that
3252 * means we can't fit an extsz piece in this hole. Just move
3253 * the start forward to the first valid spot and set
3254 * the length so we hit the end.
3256 if (align_off
!= orig_off
&& align_off
< prevo
)
3258 if (align_off
+ align_alen
!= orig_end
&&
3259 align_off
+ align_alen
> nexto
&&
3260 nexto
!= NULLFILEOFF
) {
3261 ASSERT(nexto
> prevo
);
3262 align_alen
= nexto
- align_off
;
3266 * If realtime, and the result isn't a multiple of the realtime
3267 * extent size we need to remove blocks until it is.
3269 if (rt
&& (temp
= (align_alen
% mp
->m_sb
.sb_rextsize
))) {
3271 * We're not covering the original request, or
3272 * we won't be able to once we fix the length.
3274 if (orig_off
< align_off
||
3275 orig_end
> align_off
+ align_alen
||
3276 align_alen
- temp
< orig_alen
)
3279 * Try to fix it by moving the start up.
3281 if (align_off
+ temp
<= orig_off
) {
3286 * Try to fix it by moving the end in.
3288 else if (align_off
+ align_alen
- temp
>= orig_end
)
3291 * Set the start to the minimum then trim the length.
3294 align_alen
-= orig_off
- align_off
;
3295 align_off
= orig_off
;
3296 align_alen
-= align_alen
% mp
->m_sb
.sb_rextsize
;
3299 * Result doesn't cover the request, fail it.
3301 if (orig_off
< align_off
|| orig_end
> align_off
+ align_alen
)
3304 ASSERT(orig_off
>= align_off
);
3305 /* see MAXEXTLEN handling above */
3306 ASSERT(orig_end
<= align_off
+ align_alen
||
3307 align_alen
+ extsz
> MAXEXTLEN
);
3311 if (!eof
&& gotp
->br_startoff
!= NULLFILEOFF
)
3312 ASSERT(align_off
+ align_alen
<= gotp
->br_startoff
);
3313 if (prevp
->br_startoff
!= NULLFILEOFF
)
3314 ASSERT(align_off
>= prevp
->br_startoff
+ prevp
->br_blockcount
);
3322 #define XFS_ALLOC_GAP_UNITS 4
3326 struct xfs_bmalloca
*ap
) /* bmap alloc argument struct */
3328 xfs_fsblock_t adjust
; /* adjustment to block numbers */
3329 xfs_agnumber_t fb_agno
; /* ag number of ap->firstblock */
3330 xfs_mount_t
*mp
; /* mount point structure */
3331 int nullfb
; /* true if ap->firstblock isn't set */
3332 int rt
; /* true if inode is realtime */
3334 #define ISVALID(x,y) \
3336 (x) < mp->m_sb.sb_rblocks : \
3337 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
3338 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
3339 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
3341 mp
= ap
->ip
->i_mount
;
3342 nullfb
= *ap
->firstblock
== NULLFSBLOCK
;
3343 rt
= XFS_IS_REALTIME_INODE(ap
->ip
) && ap
->userdata
;
3344 fb_agno
= nullfb
? NULLAGNUMBER
: XFS_FSB_TO_AGNO(mp
, *ap
->firstblock
);
3346 * If allocating at eof, and there's a previous real block,
3347 * try to use its last block as our starting point.
3349 if (ap
->eof
&& ap
->prev
.br_startoff
!= NULLFILEOFF
&&
3350 !isnullstartblock(ap
->prev
.br_startblock
) &&
3351 ISVALID(ap
->prev
.br_startblock
+ ap
->prev
.br_blockcount
,
3352 ap
->prev
.br_startblock
)) {
3353 ap
->blkno
= ap
->prev
.br_startblock
+ ap
->prev
.br_blockcount
;
3355 * Adjust for the gap between prevp and us.
3357 adjust
= ap
->offset
-
3358 (ap
->prev
.br_startoff
+ ap
->prev
.br_blockcount
);
3360 ISVALID(ap
->blkno
+ adjust
, ap
->prev
.br_startblock
))
3361 ap
->blkno
+= adjust
;
3364 * If not at eof, then compare the two neighbor blocks.
3365 * Figure out whether either one gives us a good starting point,
3366 * and pick the better one.
3368 else if (!ap
->eof
) {
3369 xfs_fsblock_t gotbno
; /* right side block number */
3370 xfs_fsblock_t gotdiff
=0; /* right side difference */
3371 xfs_fsblock_t prevbno
; /* left side block number */
3372 xfs_fsblock_t prevdiff
=0; /* left side difference */
3375 * If there's a previous (left) block, select a requested
3376 * start block based on it.
3378 if (ap
->prev
.br_startoff
!= NULLFILEOFF
&&
3379 !isnullstartblock(ap
->prev
.br_startblock
) &&
3380 (prevbno
= ap
->prev
.br_startblock
+
3381 ap
->prev
.br_blockcount
) &&
3382 ISVALID(prevbno
, ap
->prev
.br_startblock
)) {
3384 * Calculate gap to end of previous block.
3386 adjust
= prevdiff
= ap
->offset
-
3387 (ap
->prev
.br_startoff
+
3388 ap
->prev
.br_blockcount
);
3390 * Figure the startblock based on the previous block's
3391 * end and the gap size.
3393 * If the gap is large relative to the piece we're
3394 * allocating, or using it gives us an invalid block
3395 * number, then just use the end of the previous block.
3397 if (prevdiff
<= XFS_ALLOC_GAP_UNITS
* ap
->length
&&
3398 ISVALID(prevbno
+ prevdiff
,
3399 ap
->prev
.br_startblock
))
3404 * If the firstblock forbids it, can't use it,
3407 if (!rt
&& !nullfb
&&
3408 XFS_FSB_TO_AGNO(mp
, prevbno
) != fb_agno
)
3409 prevbno
= NULLFSBLOCK
;
3412 * No previous block or can't follow it, just default.
3415 prevbno
= NULLFSBLOCK
;
3417 * If there's a following (right) block, select a requested
3418 * start block based on it.
3420 if (!isnullstartblock(ap
->got
.br_startblock
)) {
3422 * Calculate gap to start of next block.
3424 adjust
= gotdiff
= ap
->got
.br_startoff
- ap
->offset
;
3426 * Figure the startblock based on the next block's
3427 * start and the gap size.
3429 gotbno
= ap
->got
.br_startblock
;
3432 * If the gap is large relative to the piece we're
3433 * allocating, or using it gives us an invalid block
3434 * number, then just use the start of the next block
3435 * offset by our length.
3437 if (gotdiff
<= XFS_ALLOC_GAP_UNITS
* ap
->length
&&
3438 ISVALID(gotbno
- gotdiff
, gotbno
))
3440 else if (ISVALID(gotbno
- ap
->length
, gotbno
)) {
3441 gotbno
-= ap
->length
;
3442 gotdiff
+= adjust
- ap
->length
;
3446 * If the firstblock forbids it, can't use it,
3449 if (!rt
&& !nullfb
&&
3450 XFS_FSB_TO_AGNO(mp
, gotbno
) != fb_agno
)
3451 gotbno
= NULLFSBLOCK
;
3454 * No next block, just default.
3457 gotbno
= NULLFSBLOCK
;
3459 * If both valid, pick the better one, else the only good
3460 * one, else ap->blkno is already set (to 0 or the inode block).
3462 if (prevbno
!= NULLFSBLOCK
&& gotbno
!= NULLFSBLOCK
)
3463 ap
->blkno
= prevdiff
<= gotdiff
? prevbno
: gotbno
;
3464 else if (prevbno
!= NULLFSBLOCK
)
3465 ap
->blkno
= prevbno
;
3466 else if (gotbno
!= NULLFSBLOCK
)
3473 xfs_bmap_longest_free_extent(
3474 struct xfs_trans
*tp
,
3479 struct xfs_mount
*mp
= tp
->t_mountp
;
3480 struct xfs_perag
*pag
;
3481 xfs_extlen_t longest
;
3484 pag
= xfs_perag_get(mp
, ag
);
3485 if (!pag
->pagf_init
) {
3486 error
= xfs_alloc_pagf_init(mp
, tp
, ag
, XFS_ALLOC_FLAG_TRYLOCK
);
3490 if (!pag
->pagf_init
) {
3496 longest
= xfs_alloc_longest_free_extent(mp
, pag
,
3497 xfs_alloc_min_freelist(mp
, pag
),
3498 xfs_ag_resv_needed(pag
, XFS_AG_RESV_NONE
));
3499 if (*blen
< longest
)
3508 xfs_bmap_select_minlen(
3509 struct xfs_bmalloca
*ap
,
3510 struct xfs_alloc_arg
*args
,
3514 if (notinit
|| *blen
< ap
->minlen
) {
3516 * Since we did a BUF_TRYLOCK above, it is possible that
3517 * there is space for this request.
3519 args
->minlen
= ap
->minlen
;
3520 } else if (*blen
< args
->maxlen
) {
3522 * If the best seen length is less than the request length,
3523 * use the best as the minimum.
3525 args
->minlen
= *blen
;
3528 * Otherwise we've seen an extent as big as maxlen, use that
3531 args
->minlen
= args
->maxlen
;
3536 xfs_bmap_btalloc_nullfb(
3537 struct xfs_bmalloca
*ap
,
3538 struct xfs_alloc_arg
*args
,
3541 struct xfs_mount
*mp
= ap
->ip
->i_mount
;
3542 xfs_agnumber_t ag
, startag
;
3546 args
->type
= XFS_ALLOCTYPE_START_BNO
;
3547 args
->total
= ap
->total
;
3549 startag
= ag
= XFS_FSB_TO_AGNO(mp
, args
->fsbno
);
3550 if (startag
== NULLAGNUMBER
)
3553 while (*blen
< args
->maxlen
) {
3554 error
= xfs_bmap_longest_free_extent(args
->tp
, ag
, blen
,
3559 if (++ag
== mp
->m_sb
.sb_agcount
)
3565 xfs_bmap_select_minlen(ap
, args
, blen
, notinit
);
3570 xfs_bmap_btalloc_filestreams(
3571 struct xfs_bmalloca
*ap
,
3572 struct xfs_alloc_arg
*args
,
3575 struct xfs_mount
*mp
= ap
->ip
->i_mount
;
3580 args
->type
= XFS_ALLOCTYPE_NEAR_BNO
;
3581 args
->total
= ap
->total
;
3583 ag
= XFS_FSB_TO_AGNO(mp
, args
->fsbno
);
3584 if (ag
== NULLAGNUMBER
)
3587 error
= xfs_bmap_longest_free_extent(args
->tp
, ag
, blen
, ¬init
);
3591 if (*blen
< args
->maxlen
) {
3592 error
= xfs_filestream_new_ag(ap
, &ag
);
3596 error
= xfs_bmap_longest_free_extent(args
->tp
, ag
, blen
,
3603 xfs_bmap_select_minlen(ap
, args
, blen
, notinit
);
3606 * Set the failure fallback case to look in the selected AG as stream
3609 ap
->blkno
= args
->fsbno
= XFS_AGB_TO_FSB(mp
, ag
, 0);
3615 struct xfs_bmalloca
*ap
) /* bmap alloc argument struct */
3617 xfs_mount_t
*mp
; /* mount point structure */
3618 xfs_alloctype_t atype
= 0; /* type for allocation routines */
3619 xfs_extlen_t align
; /* minimum allocation alignment */
3620 xfs_agnumber_t fb_agno
; /* ag number of ap->firstblock */
3622 xfs_alloc_arg_t args
;
3624 xfs_extlen_t nextminlen
= 0;
3625 int nullfb
; /* true if ap->firstblock isn't set */
3633 mp
= ap
->ip
->i_mount
;
3635 /* stripe alignment for allocation is determined by mount parameters */
3637 if (mp
->m_swidth
&& (mp
->m_flags
& XFS_MOUNT_SWALLOC
))
3638 stripe_align
= mp
->m_swidth
;
3639 else if (mp
->m_dalign
)
3640 stripe_align
= mp
->m_dalign
;
3642 align
= ap
->userdata
? xfs_get_extsz_hint(ap
->ip
) : 0;
3643 if (unlikely(align
)) {
3644 error
= xfs_bmap_extsize_align(mp
, &ap
->got
, &ap
->prev
,
3645 align
, 0, ap
->eof
, 0, ap
->conv
,
3646 &ap
->offset
, &ap
->length
);
3652 nullfb
= *ap
->firstblock
== NULLFSBLOCK
;
3653 fb_agno
= nullfb
? NULLAGNUMBER
: XFS_FSB_TO_AGNO(mp
, *ap
->firstblock
);
3655 if (ap
->userdata
&& xfs_inode_is_filestream(ap
->ip
)) {
3656 ag
= xfs_filestream_lookup_ag(ap
->ip
);
3657 ag
= (ag
!= NULLAGNUMBER
) ? ag
: 0;
3658 ap
->blkno
= XFS_AGB_TO_FSB(mp
, ag
, 0);
3660 ap
->blkno
= XFS_INO_TO_FSB(mp
, ap
->ip
->i_ino
);
3663 ap
->blkno
= *ap
->firstblock
;
3665 xfs_bmap_adjacent(ap
);
3668 * If allowed, use ap->blkno; otherwise must use firstblock since
3669 * it's in the right allocation group.
3671 if (nullfb
|| XFS_FSB_TO_AGNO(mp
, ap
->blkno
) == fb_agno
)
3674 ap
->blkno
= *ap
->firstblock
;
3676 * Normal allocation, done through xfs_alloc_vextent.
3678 tryagain
= isaligned
= 0;
3679 memset(&args
, 0, sizeof(args
));
3682 args
.fsbno
= ap
->blkno
;
3683 xfs_rmap_skip_owner_update(&args
.oinfo
);
3685 /* Trim the allocation back to the maximum an AG can fit. */
3686 args
.maxlen
= MIN(ap
->length
, mp
->m_ag_max_usable
);
3687 args
.firstblock
= *ap
->firstblock
;
3691 * Search for an allocation group with a single extent large
3692 * enough for the request. If one isn't found, then adjust
3693 * the minimum allocation size to the largest space found.
3695 if (ap
->userdata
&& xfs_inode_is_filestream(ap
->ip
))
3696 error
= xfs_bmap_btalloc_filestreams(ap
, &args
, &blen
);
3698 error
= xfs_bmap_btalloc_nullfb(ap
, &args
, &blen
);
3701 } else if (ap
->dfops
->dop_low
) {
3702 if (xfs_inode_is_filestream(ap
->ip
))
3703 args
.type
= XFS_ALLOCTYPE_FIRST_AG
;
3705 args
.type
= XFS_ALLOCTYPE_START_BNO
;
3706 args
.total
= args
.minlen
= ap
->minlen
;
3708 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
3709 args
.total
= ap
->total
;
3710 args
.minlen
= ap
->minlen
;
3712 /* apply extent size hints if obtained earlier */
3713 if (unlikely(align
)) {
3715 if ((args
.mod
= (xfs_extlen_t
)do_mod(ap
->offset
, args
.prod
)))
3716 args
.mod
= (xfs_extlen_t
)(args
.prod
- args
.mod
);
3717 } else if (mp
->m_sb
.sb_blocksize
>= PAGE_SIZE
) {
3721 args
.prod
= PAGE_SIZE
>> mp
->m_sb
.sb_blocklog
;
3722 if ((args
.mod
= (xfs_extlen_t
)(do_mod(ap
->offset
, args
.prod
))))
3723 args
.mod
= (xfs_extlen_t
)(args
.prod
- args
.mod
);
3726 * If we are not low on available data blocks, and the
3727 * underlying logical volume manager is a stripe, and
3728 * the file offset is zero then try to allocate data
3729 * blocks on stripe unit boundary.
3730 * NOTE: ap->aeof is only set if the allocation length
3731 * is >= the stripe unit and the allocation offset is
3732 * at the end of file.
3734 if (!ap
->dfops
->dop_low
&& ap
->aeof
) {
3736 args
.alignment
= stripe_align
;
3740 * Adjust for alignment
3742 if (blen
> args
.alignment
&& blen
<= args
.maxlen
)
3743 args
.minlen
= blen
- args
.alignment
;
3744 args
.minalignslop
= 0;
3747 * First try an exact bno allocation.
3748 * If it fails then do a near or start bno
3749 * allocation with alignment turned on.
3753 args
.type
= XFS_ALLOCTYPE_THIS_BNO
;
3756 * Compute the minlen+alignment for the
3757 * next case. Set slop so that the value
3758 * of minlen+alignment+slop doesn't go up
3759 * between the calls.
3761 if (blen
> stripe_align
&& blen
<= args
.maxlen
)
3762 nextminlen
= blen
- stripe_align
;
3764 nextminlen
= args
.minlen
;
3765 if (nextminlen
+ stripe_align
> args
.minlen
+ 1)
3767 nextminlen
+ stripe_align
-
3770 args
.minalignslop
= 0;
3774 args
.minalignslop
= 0;
3776 args
.minleft
= ap
->minleft
;
3777 args
.wasdel
= ap
->wasdel
;
3778 args
.resv
= XFS_AG_RESV_NONE
;
3779 args
.userdata
= ap
->userdata
;
3780 if (ap
->userdata
& XFS_ALLOC_USERDATA_ZERO
)
3783 error
= xfs_alloc_vextent(&args
);
3787 if (tryagain
&& args
.fsbno
== NULLFSBLOCK
) {
3789 * Exact allocation failed. Now try with alignment
3793 args
.fsbno
= ap
->blkno
;
3794 args
.alignment
= stripe_align
;
3795 args
.minlen
= nextminlen
;
3796 args
.minalignslop
= 0;
3798 if ((error
= xfs_alloc_vextent(&args
)))
3801 if (isaligned
&& args
.fsbno
== NULLFSBLOCK
) {
3803 * allocation failed, so turn off alignment and
3807 args
.fsbno
= ap
->blkno
;
3809 if ((error
= xfs_alloc_vextent(&args
)))
3812 if (args
.fsbno
== NULLFSBLOCK
&& nullfb
&&
3813 args
.minlen
> ap
->minlen
) {
3814 args
.minlen
= ap
->minlen
;
3815 args
.type
= XFS_ALLOCTYPE_START_BNO
;
3816 args
.fsbno
= ap
->blkno
;
3817 if ((error
= xfs_alloc_vextent(&args
)))
3820 if (args
.fsbno
== NULLFSBLOCK
&& nullfb
) {
3822 args
.type
= XFS_ALLOCTYPE_FIRST_AG
;
3823 args
.total
= ap
->minlen
;
3825 if ((error
= xfs_alloc_vextent(&args
)))
3827 ap
->dfops
->dop_low
= true;
3829 if (args
.fsbno
!= NULLFSBLOCK
) {
3831 * check the allocation happened at the same or higher AG than
3832 * the first block that was allocated.
3834 ASSERT(*ap
->firstblock
== NULLFSBLOCK
||
3835 XFS_FSB_TO_AGNO(mp
, *ap
->firstblock
) ==
3836 XFS_FSB_TO_AGNO(mp
, args
.fsbno
) ||
3837 (ap
->dfops
->dop_low
&&
3838 XFS_FSB_TO_AGNO(mp
, *ap
->firstblock
) <
3839 XFS_FSB_TO_AGNO(mp
, args
.fsbno
)));
3841 ap
->blkno
= args
.fsbno
;
3842 if (*ap
->firstblock
== NULLFSBLOCK
)
3843 *ap
->firstblock
= args
.fsbno
;
3844 ASSERT(nullfb
|| fb_agno
== args
.agno
||
3845 (ap
->dfops
->dop_low
&& fb_agno
< args
.agno
));
3846 ap
->length
= args
.len
;
3847 ap
->ip
->i_d
.di_nblocks
+= args
.len
;
3848 xfs_trans_log_inode(ap
->tp
, ap
->ip
, XFS_ILOG_CORE
);
3850 ap
->ip
->i_delayed_blks
-= args
.len
;
3852 * Adjust the disk quota also. This was reserved
3855 xfs_trans_mod_dquot_byino(ap
->tp
, ap
->ip
,
3856 ap
->wasdel
? XFS_TRANS_DQ_DELBCOUNT
:
3857 XFS_TRANS_DQ_BCOUNT
,
3860 ap
->blkno
= NULLFSBLOCK
;
3867 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
3868 * It figures out where to ask the underlying allocator to put the new extent.
3872 struct xfs_bmalloca
*ap
) /* bmap alloc argument struct */
3874 if (XFS_IS_REALTIME_INODE(ap
->ip
) && ap
->userdata
)
3875 return xfs_bmap_rtalloc(ap
);
3876 return xfs_bmap_btalloc(ap
);
3880 * Trim the returned map to the required bounds
3884 struct xfs_bmbt_irec
*mval
,
3885 struct xfs_bmbt_irec
*got
,
3893 if ((flags
& XFS_BMAPI_ENTIRE
) ||
3894 got
->br_startoff
+ got
->br_blockcount
<= obno
) {
3896 if (isnullstartblock(got
->br_startblock
))
3897 mval
->br_startblock
= DELAYSTARTBLOCK
;
3903 ASSERT((*bno
>= obno
) || (n
== 0));
3905 mval
->br_startoff
= *bno
;
3906 if (isnullstartblock(got
->br_startblock
))
3907 mval
->br_startblock
= DELAYSTARTBLOCK
;
3909 mval
->br_startblock
= got
->br_startblock
+
3910 (*bno
- got
->br_startoff
);
3912 * Return the minimum of what we got and what we asked for for
3913 * the length. We can use the len variable here because it is
3914 * modified below and we could have been there before coming
3915 * here if the first part of the allocation didn't overlap what
3918 mval
->br_blockcount
= XFS_FILBLKS_MIN(end
- *bno
,
3919 got
->br_blockcount
- (*bno
- got
->br_startoff
));
3920 mval
->br_state
= got
->br_state
;
3921 ASSERT(mval
->br_blockcount
<= len
);
3926 * Update and validate the extent map to return
3929 xfs_bmapi_update_map(
3930 struct xfs_bmbt_irec
**map
,
3938 xfs_bmbt_irec_t
*mval
= *map
;
3940 ASSERT((flags
& XFS_BMAPI_ENTIRE
) ||
3941 ((mval
->br_startoff
+ mval
->br_blockcount
) <= end
));
3942 ASSERT((flags
& XFS_BMAPI_ENTIRE
) || (mval
->br_blockcount
<= *len
) ||
3943 (mval
->br_startoff
< obno
));
3945 *bno
= mval
->br_startoff
+ mval
->br_blockcount
;
3947 if (*n
> 0 && mval
->br_startoff
== mval
[-1].br_startoff
) {
3948 /* update previous map with new information */
3949 ASSERT(mval
->br_startblock
== mval
[-1].br_startblock
);
3950 ASSERT(mval
->br_blockcount
> mval
[-1].br_blockcount
);
3951 ASSERT(mval
->br_state
== mval
[-1].br_state
);
3952 mval
[-1].br_blockcount
= mval
->br_blockcount
;
3953 mval
[-1].br_state
= mval
->br_state
;
3954 } else if (*n
> 0 && mval
->br_startblock
!= DELAYSTARTBLOCK
&&
3955 mval
[-1].br_startblock
!= DELAYSTARTBLOCK
&&
3956 mval
[-1].br_startblock
!= HOLESTARTBLOCK
&&
3957 mval
->br_startblock
== mval
[-1].br_startblock
+
3958 mval
[-1].br_blockcount
&&
3959 ((flags
& XFS_BMAPI_IGSTATE
) ||
3960 mval
[-1].br_state
== mval
->br_state
)) {
3961 ASSERT(mval
->br_startoff
==
3962 mval
[-1].br_startoff
+ mval
[-1].br_blockcount
);
3963 mval
[-1].br_blockcount
+= mval
->br_blockcount
;
3964 } else if (*n
> 0 &&
3965 mval
->br_startblock
== DELAYSTARTBLOCK
&&
3966 mval
[-1].br_startblock
== DELAYSTARTBLOCK
&&
3967 mval
->br_startoff
==
3968 mval
[-1].br_startoff
+ mval
[-1].br_blockcount
) {
3969 mval
[-1].br_blockcount
+= mval
->br_blockcount
;
3970 mval
[-1].br_state
= mval
->br_state
;
3971 } else if (!((*n
== 0) &&
3972 ((mval
->br_startoff
+ mval
->br_blockcount
) <=
3981 * Map file blocks to filesystem blocks without allocation.
3985 struct xfs_inode
*ip
,
3988 struct xfs_bmbt_irec
*mval
,
3992 struct xfs_mount
*mp
= ip
->i_mount
;
3993 struct xfs_ifork
*ifp
;
3994 struct xfs_bmbt_irec got
;
3995 struct xfs_bmbt_irec prev
;
4002 int whichfork
= (flags
& XFS_BMAPI_ATTRFORK
) ?
4003 XFS_ATTR_FORK
: XFS_DATA_FORK
;
4006 ASSERT(!(flags
& ~(XFS_BMAPI_ATTRFORK
|XFS_BMAPI_ENTIRE
|
4007 XFS_BMAPI_IGSTATE
)));
4008 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_SHARED
|XFS_ILOCK_EXCL
));
4010 if (unlikely(XFS_TEST_ERROR(
4011 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
4012 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
4013 mp
, XFS_ERRTAG_BMAPIFORMAT
, XFS_RANDOM_BMAPIFORMAT
))) {
4014 XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW
, mp
);
4015 return -EFSCORRUPTED
;
4018 if (XFS_FORCED_SHUTDOWN(mp
))
4021 XFS_STATS_INC(mp
, xs_blk_mapr
);
4023 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4025 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
4026 error
= xfs_iread_extents(NULL
, ip
, whichfork
);
4031 xfs_bmap_search_extents(ip
, bno
, whichfork
, &eof
, &lastx
, &got
, &prev
);
4035 while (bno
< end
&& n
< *nmap
) {
4036 /* Reading past eof, act as though there's a hole up to end. */
4038 got
.br_startoff
= end
;
4039 if (got
.br_startoff
> bno
) {
4040 /* Reading in a hole. */
4041 mval
->br_startoff
= bno
;
4042 mval
->br_startblock
= HOLESTARTBLOCK
;
4043 mval
->br_blockcount
=
4044 XFS_FILBLKS_MIN(len
, got
.br_startoff
- bno
);
4045 mval
->br_state
= XFS_EXT_NORM
;
4046 bno
+= mval
->br_blockcount
;
4047 len
-= mval
->br_blockcount
;
4053 /* set up the extent map to return. */
4054 xfs_bmapi_trim_map(mval
, &got
, &bno
, len
, obno
, end
, n
, flags
);
4055 xfs_bmapi_update_map(&mval
, &bno
, &len
, obno
, end
, &n
, flags
);
4057 /* If we're done, stop now. */
4058 if (bno
>= end
|| n
>= *nmap
)
4061 /* Else go on to the next record. */
4062 if (++lastx
< ifp
->if_bytes
/ sizeof(xfs_bmbt_rec_t
))
4063 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, lastx
), &got
);
4072 xfs_bmapi_reserve_delalloc(
4073 struct xfs_inode
*ip
,
4076 struct xfs_bmbt_irec
*got
,
4077 struct xfs_bmbt_irec
*prev
,
4078 xfs_extnum_t
*lastx
,
4081 struct xfs_mount
*mp
= ip
->i_mount
;
4082 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, XFS_DATA_FORK
);
4084 xfs_extlen_t indlen
;
4085 char rt
= XFS_IS_REALTIME_INODE(ip
);
4089 alen
= XFS_FILBLKS_MIN(len
, MAXEXTLEN
);
4091 alen
= XFS_FILBLKS_MIN(alen
, got
->br_startoff
- aoff
);
4093 /* Figure out the extent size, adjust alen */
4094 extsz
= xfs_get_extsz_hint(ip
);
4096 error
= xfs_bmap_extsize_align(mp
, got
, prev
, extsz
, rt
, eof
,
4097 1, 0, &aoff
, &alen
);
4102 extsz
= alen
/ mp
->m_sb
.sb_rextsize
;
4105 * Make a transaction-less quota reservation for delayed allocation
4106 * blocks. This number gets adjusted later. We return if we haven't
4107 * allocated blocks already inside this loop.
4109 error
= xfs_trans_reserve_quota_nblks(NULL
, ip
, (long)alen
, 0,
4110 rt
? XFS_QMOPT_RES_RTBLKS
: XFS_QMOPT_RES_REGBLKS
);
4115 * Split changing sb for alen and indlen since they could be coming
4116 * from different places.
4118 indlen
= (xfs_extlen_t
)xfs_bmap_worst_indlen(ip
, alen
);
4122 error
= xfs_mod_frextents(mp
, -((int64_t)extsz
));
4124 error
= xfs_mod_fdblocks(mp
, -((int64_t)alen
), false);
4128 goto out_unreserve_quota
;
4130 error
= xfs_mod_fdblocks(mp
, -((int64_t)indlen
), false);
4132 goto out_unreserve_blocks
;
4135 ip
->i_delayed_blks
+= alen
;
4137 got
->br_startoff
= aoff
;
4138 got
->br_startblock
= nullstartblock(indlen
);
4139 got
->br_blockcount
= alen
;
4140 got
->br_state
= XFS_EXT_NORM
;
4141 xfs_bmap_add_extent_hole_delay(ip
, lastx
, got
);
4144 * Update our extent pointer, given that xfs_bmap_add_extent_hole_delay
4145 * might have merged it into one of the neighbouring ones.
4147 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, *lastx
), got
);
4149 ASSERT(got
->br_startoff
<= aoff
);
4150 ASSERT(got
->br_startoff
+ got
->br_blockcount
>= aoff
+ alen
);
4151 ASSERT(isnullstartblock(got
->br_startblock
));
4152 ASSERT(got
->br_state
== XFS_EXT_NORM
);
4155 out_unreserve_blocks
:
4157 xfs_mod_frextents(mp
, extsz
);
4159 xfs_mod_fdblocks(mp
, alen
, false);
4160 out_unreserve_quota
:
4161 if (XFS_IS_QUOTA_ON(mp
))
4162 xfs_trans_unreserve_quota_nblks(NULL
, ip
, (long)alen
, 0, rt
?
4163 XFS_QMOPT_RES_RTBLKS
: XFS_QMOPT_RES_REGBLKS
);
4168 * Map file blocks to filesystem blocks, adding delayed allocations as needed.
4172 struct xfs_inode
*ip
, /* incore inode */
4173 xfs_fileoff_t bno
, /* starting file offs. mapped */
4174 xfs_filblks_t len
, /* length to map in file */
4175 struct xfs_bmbt_irec
*mval
, /* output: map values */
4176 int *nmap
, /* i/o: mval size/count */
4177 int flags
) /* XFS_BMAPI_... */
4179 struct xfs_mount
*mp
= ip
->i_mount
;
4180 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, XFS_DATA_FORK
);
4181 struct xfs_bmbt_irec got
; /* current file extent record */
4182 struct xfs_bmbt_irec prev
; /* previous file extent record */
4183 xfs_fileoff_t obno
; /* old block number (offset) */
4184 xfs_fileoff_t end
; /* end of mapped file region */
4185 xfs_extnum_t lastx
; /* last useful extent number */
4186 int eof
; /* we've hit the end of extents */
4187 int n
= 0; /* current extent index */
4191 ASSERT(*nmap
<= XFS_BMAP_MAX_NMAP
);
4192 ASSERT(!(flags
& ~XFS_BMAPI_ENTIRE
));
4193 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
4195 if (unlikely(XFS_TEST_ERROR(
4196 (XFS_IFORK_FORMAT(ip
, XFS_DATA_FORK
) != XFS_DINODE_FMT_EXTENTS
&&
4197 XFS_IFORK_FORMAT(ip
, XFS_DATA_FORK
) != XFS_DINODE_FMT_BTREE
),
4198 mp
, XFS_ERRTAG_BMAPIFORMAT
, XFS_RANDOM_BMAPIFORMAT
))) {
4199 XFS_ERROR_REPORT("xfs_bmapi_delay", XFS_ERRLEVEL_LOW
, mp
);
4200 return -EFSCORRUPTED
;
4203 if (XFS_FORCED_SHUTDOWN(mp
))
4206 XFS_STATS_INC(mp
, xs_blk_mapw
);
4208 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
4209 error
= xfs_iread_extents(NULL
, ip
, XFS_DATA_FORK
);
4214 xfs_bmap_search_extents(ip
, bno
, XFS_DATA_FORK
, &eof
, &lastx
, &got
, &prev
);
4218 while (bno
< end
&& n
< *nmap
) {
4219 if (eof
|| got
.br_startoff
> bno
) {
4220 error
= xfs_bmapi_reserve_delalloc(ip
, bno
, len
, &got
,
4221 &prev
, &lastx
, eof
);
4231 /* set up the extent map to return. */
4232 xfs_bmapi_trim_map(mval
, &got
, &bno
, len
, obno
, end
, n
, flags
);
4233 xfs_bmapi_update_map(&mval
, &bno
, &len
, obno
, end
, &n
, flags
);
4235 /* If we're done, stop now. */
4236 if (bno
>= end
|| n
>= *nmap
)
4239 /* Else go on to the next record. */
4241 if (++lastx
< ifp
->if_bytes
/ sizeof(xfs_bmbt_rec_t
))
4242 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, lastx
), &got
);
4254 struct xfs_bmalloca
*bma
)
4256 struct xfs_mount
*mp
= bma
->ip
->i_mount
;
4257 int whichfork
= (bma
->flags
& XFS_BMAPI_ATTRFORK
) ?
4258 XFS_ATTR_FORK
: XFS_DATA_FORK
;
4259 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
4260 int tmp_logflags
= 0;
4263 ASSERT(bma
->length
> 0);
4266 * For the wasdelay case, we could also just allocate the stuff asked
4267 * for in this bmap call but that wouldn't be as good.
4270 bma
->length
= (xfs_extlen_t
)bma
->got
.br_blockcount
;
4271 bma
->offset
= bma
->got
.br_startoff
;
4272 if (bma
->idx
!= NULLEXTNUM
&& bma
->idx
) {
4273 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
- 1),
4277 bma
->length
= XFS_FILBLKS_MIN(bma
->length
, MAXEXTLEN
);
4279 bma
->length
= XFS_FILBLKS_MIN(bma
->length
,
4280 bma
->got
.br_startoff
- bma
->offset
);
4284 * Indicate if this is the first user data in the file, or just any
4285 * user data. And if it is userdata, indicate whether it needs to
4286 * be initialised to zero during allocation.
4288 if (!(bma
->flags
& XFS_BMAPI_METADATA
)) {
4289 bma
->userdata
= (bma
->offset
== 0) ?
4290 XFS_ALLOC_INITIAL_USER_DATA
: XFS_ALLOC_USERDATA
;
4291 if (bma
->flags
& XFS_BMAPI_ZERO
)
4292 bma
->userdata
|= XFS_ALLOC_USERDATA_ZERO
;
4295 bma
->minlen
= (bma
->flags
& XFS_BMAPI_CONTIG
) ? bma
->length
: 1;
4298 * Only want to do the alignment at the eof if it is userdata and
4299 * allocation length is larger than a stripe unit.
4301 if (mp
->m_dalign
&& bma
->length
>= mp
->m_dalign
&&
4302 !(bma
->flags
& XFS_BMAPI_METADATA
) && whichfork
== XFS_DATA_FORK
) {
4303 error
= xfs_bmap_isaeof(bma
, whichfork
);
4308 error
= xfs_bmap_alloc(bma
);
4312 if (bma
->dfops
->dop_low
)
4315 bma
->cur
->bc_private
.b
.firstblock
= *bma
->firstblock
;
4316 if (bma
->blkno
== NULLFSBLOCK
)
4318 if ((ifp
->if_flags
& XFS_IFBROOT
) && !bma
->cur
) {
4319 bma
->cur
= xfs_bmbt_init_cursor(mp
, bma
->tp
, bma
->ip
, whichfork
);
4320 bma
->cur
->bc_private
.b
.firstblock
= *bma
->firstblock
;
4321 bma
->cur
->bc_private
.b
.dfops
= bma
->dfops
;
4324 * Bump the number of extents we've allocated
4330 bma
->cur
->bc_private
.b
.flags
=
4331 bma
->wasdel
? XFS_BTCUR_BPRV_WASDEL
: 0;
4333 bma
->got
.br_startoff
= bma
->offset
;
4334 bma
->got
.br_startblock
= bma
->blkno
;
4335 bma
->got
.br_blockcount
= bma
->length
;
4336 bma
->got
.br_state
= XFS_EXT_NORM
;
4339 * A wasdelay extent has been initialized, so shouldn't be flagged
4342 if (!bma
->wasdel
&& (bma
->flags
& XFS_BMAPI_PREALLOC
) &&
4343 xfs_sb_version_hasextflgbit(&mp
->m_sb
))
4344 bma
->got
.br_state
= XFS_EXT_UNWRITTEN
;
4347 error
= xfs_bmap_add_extent_delay_real(bma
);
4349 error
= xfs_bmap_add_extent_hole_real(bma
, whichfork
);
4351 bma
->logflags
|= tmp_logflags
;
4356 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4357 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4358 * the neighbouring ones.
4360 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
), &bma
->got
);
4362 ASSERT(bma
->got
.br_startoff
<= bma
->offset
);
4363 ASSERT(bma
->got
.br_startoff
+ bma
->got
.br_blockcount
>=
4364 bma
->offset
+ bma
->length
);
4365 ASSERT(bma
->got
.br_state
== XFS_EXT_NORM
||
4366 bma
->got
.br_state
== XFS_EXT_UNWRITTEN
);
4371 xfs_bmapi_convert_unwritten(
4372 struct xfs_bmalloca
*bma
,
4373 struct xfs_bmbt_irec
*mval
,
4377 int whichfork
= (flags
& XFS_BMAPI_ATTRFORK
) ?
4378 XFS_ATTR_FORK
: XFS_DATA_FORK
;
4379 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
4380 int tmp_logflags
= 0;
4383 /* check if we need to do unwritten->real conversion */
4384 if (mval
->br_state
== XFS_EXT_UNWRITTEN
&&
4385 (flags
& XFS_BMAPI_PREALLOC
))
4388 /* check if we need to do real->unwritten conversion */
4389 if (mval
->br_state
== XFS_EXT_NORM
&&
4390 (flags
& (XFS_BMAPI_PREALLOC
| XFS_BMAPI_CONVERT
)) !=
4391 (XFS_BMAPI_PREALLOC
| XFS_BMAPI_CONVERT
))
4395 * Modify (by adding) the state flag, if writing.
4397 ASSERT(mval
->br_blockcount
<= len
);
4398 if ((ifp
->if_flags
& XFS_IFBROOT
) && !bma
->cur
) {
4399 bma
->cur
= xfs_bmbt_init_cursor(bma
->ip
->i_mount
, bma
->tp
,
4400 bma
->ip
, whichfork
);
4401 bma
->cur
->bc_private
.b
.firstblock
= *bma
->firstblock
;
4402 bma
->cur
->bc_private
.b
.dfops
= bma
->dfops
;
4404 mval
->br_state
= (mval
->br_state
== XFS_EXT_UNWRITTEN
)
4405 ? XFS_EXT_NORM
: XFS_EXT_UNWRITTEN
;
4408 * Before insertion into the bmbt, zero the range being converted
4411 if (flags
& XFS_BMAPI_ZERO
) {
4412 error
= xfs_zero_extent(bma
->ip
, mval
->br_startblock
,
4413 mval
->br_blockcount
);
4418 error
= xfs_bmap_add_extent_unwritten_real(bma
->tp
, bma
->ip
, &bma
->idx
,
4419 &bma
->cur
, mval
, bma
->firstblock
, bma
->dfops
,
4422 * Log the inode core unconditionally in the unwritten extent conversion
4423 * path because the conversion might not have done so (e.g., if the
4424 * extent count hasn't changed). We need to make sure the inode is dirty
4425 * in the transaction for the sake of fsync(), even if nothing has
4426 * changed, because fsync() will not force the log for this transaction
4427 * unless it sees the inode pinned.
4429 bma
->logflags
|= tmp_logflags
| XFS_ILOG_CORE
;
4434 * Update our extent pointer, given that
4435 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4436 * of the neighbouring ones.
4438 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
->idx
), &bma
->got
);
4441 * We may have combined previously unwritten space with written space,
4442 * so generate another request.
4444 if (mval
->br_blockcount
< len
)
4450 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4451 * extent state if necessary. Details behaviour is controlled by the flags
4452 * parameter. Only allocates blocks from a single allocation group, to avoid
4455 * The returned value in "firstblock" from the first call in a transaction
4456 * must be remembered and presented to subsequent calls in "firstblock".
4457 * An upper bound for the number of blocks to be allocated is supplied to
4458 * the first call in "total"; if no allocation group has that many free
4459 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4463 struct xfs_trans
*tp
, /* transaction pointer */
4464 struct xfs_inode
*ip
, /* incore inode */
4465 xfs_fileoff_t bno
, /* starting file offs. mapped */
4466 xfs_filblks_t len
, /* length to map in file */
4467 int flags
, /* XFS_BMAPI_... */
4468 xfs_fsblock_t
*firstblock
, /* first allocated block
4469 controls a.g. for allocs */
4470 xfs_extlen_t total
, /* total blocks needed */
4471 struct xfs_bmbt_irec
*mval
, /* output: map values */
4472 int *nmap
, /* i/o: mval size/count */
4473 struct xfs_defer_ops
*dfops
) /* i/o: list extents to free */
4475 struct xfs_mount
*mp
= ip
->i_mount
;
4476 struct xfs_ifork
*ifp
;
4477 struct xfs_bmalloca bma
= { NULL
}; /* args for xfs_bmap_alloc */
4478 xfs_fileoff_t end
; /* end of mapped file region */
4479 int eof
; /* after the end of extents */
4480 int error
; /* error return */
4481 int n
; /* current extent index */
4482 xfs_fileoff_t obno
; /* old block number (offset) */
4483 int whichfork
; /* data or attr fork */
4484 char inhole
; /* current location is hole in file */
4485 char wasdelay
; /* old extent was delayed */
4488 xfs_fileoff_t orig_bno
; /* original block number value */
4489 int orig_flags
; /* original flags arg value */
4490 xfs_filblks_t orig_len
; /* original value of len arg */
4491 struct xfs_bmbt_irec
*orig_mval
; /* original value of mval */
4492 int orig_nmap
; /* original value of *nmap */
4500 whichfork
= (flags
& XFS_BMAPI_ATTRFORK
) ?
4501 XFS_ATTR_FORK
: XFS_DATA_FORK
;
4504 ASSERT(*nmap
<= XFS_BMAP_MAX_NMAP
);
4505 ASSERT(!(flags
& XFS_BMAPI_IGSTATE
));
4508 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_LOCAL
);
4509 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
4511 /* zeroing is for currently only for data extents, not metadata */
4512 ASSERT((flags
& (XFS_BMAPI_METADATA
| XFS_BMAPI_ZERO
)) !=
4513 (XFS_BMAPI_METADATA
| XFS_BMAPI_ZERO
));
4515 * we can allocate unwritten extents or pre-zero allocated blocks,
4516 * but it makes no sense to do both at once. This would result in
4517 * zeroing the unwritten extent twice, but it still being an
4518 * unwritten extent....
4520 ASSERT((flags
& (XFS_BMAPI_PREALLOC
| XFS_BMAPI_ZERO
)) !=
4521 (XFS_BMAPI_PREALLOC
| XFS_BMAPI_ZERO
));
4523 if (unlikely(XFS_TEST_ERROR(
4524 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
4525 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
4526 mp
, XFS_ERRTAG_BMAPIFORMAT
, XFS_RANDOM_BMAPIFORMAT
))) {
4527 XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW
, mp
);
4528 return -EFSCORRUPTED
;
4531 if (XFS_FORCED_SHUTDOWN(mp
))
4534 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4536 XFS_STATS_INC(mp
, xs_blk_mapw
);
4538 if (*firstblock
== NULLFSBLOCK
) {
4539 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
)
4540 bma
.minleft
= be16_to_cpu(ifp
->if_broot
->bb_level
) + 1;
4547 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
4548 error
= xfs_iread_extents(tp
, ip
, whichfork
);
4553 xfs_bmap_search_extents(ip
, bno
, whichfork
, &eof
, &bma
.idx
, &bma
.got
,
4564 bma
.firstblock
= firstblock
;
4566 while (bno
< end
&& n
< *nmap
) {
4567 inhole
= eof
|| bma
.got
.br_startoff
> bno
;
4568 wasdelay
= !inhole
&& isnullstartblock(bma
.got
.br_startblock
);
4571 * First, deal with the hole before the allocated space
4572 * that we found, if any.
4574 if (inhole
|| wasdelay
) {
4576 bma
.conv
= !!(flags
& XFS_BMAPI_CONVERT
);
4577 bma
.wasdel
= wasdelay
;
4582 * There's a 32/64 bit type mismatch between the
4583 * allocation length request (which can be 64 bits in
4584 * length) and the bma length request, which is
4585 * xfs_extlen_t and therefore 32 bits. Hence we have to
4586 * check for 32-bit overflows and handle them here.
4588 if (len
> (xfs_filblks_t
)MAXEXTLEN
)
4589 bma
.length
= MAXEXTLEN
;
4594 ASSERT(bma
.length
> 0);
4595 error
= xfs_bmapi_allocate(&bma
);
4598 if (bma
.blkno
== NULLFSBLOCK
)
4602 /* Deal with the allocated space we found. */
4603 xfs_bmapi_trim_map(mval
, &bma
.got
, &bno
, len
, obno
,
4606 /* Execute unwritten extent conversion if necessary */
4607 error
= xfs_bmapi_convert_unwritten(&bma
, mval
, len
, flags
);
4608 if (error
== -EAGAIN
)
4613 /* update the extent map to return */
4614 xfs_bmapi_update_map(&mval
, &bno
, &len
, obno
, end
, &n
, flags
);
4617 * If we're done, stop now. Stop when we've allocated
4618 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4619 * the transaction may get too big.
4621 if (bno
>= end
|| n
>= *nmap
|| bma
.nallocs
>= *nmap
)
4624 /* Else go on to the next record. */
4626 if (++bma
.idx
< ifp
->if_bytes
/ sizeof(xfs_bmbt_rec_t
)) {
4627 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, bma
.idx
),
4635 * Transform from btree to extents, give it cur.
4637 if (xfs_bmap_wants_extents(ip
, whichfork
)) {
4638 int tmp_logflags
= 0;
4641 error
= xfs_bmap_btree_to_extents(tp
, ip
, bma
.cur
,
4642 &tmp_logflags
, whichfork
);
4643 bma
.logflags
|= tmp_logflags
;
4648 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
||
4649 XFS_IFORK_NEXTENTS(ip
, whichfork
) >
4650 XFS_IFORK_MAXEXT(ip
, whichfork
));
4654 * Log everything. Do this after conversion, there's no point in
4655 * logging the extent records if we've converted to btree format.
4657 if ((bma
.logflags
& xfs_ilog_fext(whichfork
)) &&
4658 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
4659 bma
.logflags
&= ~xfs_ilog_fext(whichfork
);
4660 else if ((bma
.logflags
& xfs_ilog_fbroot(whichfork
)) &&
4661 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)
4662 bma
.logflags
&= ~xfs_ilog_fbroot(whichfork
);
4664 * Log whatever the flags say, even if error. Otherwise we might miss
4665 * detecting a case where the data is changed, there's an error,
4666 * and it's not logged so we don't shutdown when we should.
4669 xfs_trans_log_inode(tp
, ip
, bma
.logflags
);
4673 ASSERT(*firstblock
== NULLFSBLOCK
||
4674 XFS_FSB_TO_AGNO(mp
, *firstblock
) ==
4676 bma
.cur
->bc_private
.b
.firstblock
) ||
4678 XFS_FSB_TO_AGNO(mp
, *firstblock
) <
4680 bma
.cur
->bc_private
.b
.firstblock
)));
4681 *firstblock
= bma
.cur
->bc_private
.b
.firstblock
;
4683 xfs_btree_del_cursor(bma
.cur
,
4684 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
4687 xfs_bmap_validate_ret(orig_bno
, orig_len
, orig_flags
, orig_mval
,
4693 * When a delalloc extent is split (e.g., due to a hole punch), the original
4694 * indlen reservation must be shared across the two new extents that are left
4697 * Given the original reservation and the worst case indlen for the two new
4698 * extents (as calculated by xfs_bmap_worst_indlen()), split the original
4699 * reservation fairly across the two new extents. If necessary, steal available
4700 * blocks from a deleted extent to make up a reservation deficiency (e.g., if
4701 * ores == 1). The number of stolen blocks is returned. The availability and
4702 * subsequent accounting of stolen blocks is the responsibility of the caller.
4704 static xfs_filblks_t
4705 xfs_bmap_split_indlen(
4706 xfs_filblks_t ores
, /* original res. */
4707 xfs_filblks_t
*indlen1
, /* ext1 worst indlen */
4708 xfs_filblks_t
*indlen2
, /* ext2 worst indlen */
4709 xfs_filblks_t avail
) /* stealable blocks */
4711 xfs_filblks_t len1
= *indlen1
;
4712 xfs_filblks_t len2
= *indlen2
;
4713 xfs_filblks_t nres
= len1
+ len2
; /* new total res. */
4714 xfs_filblks_t stolen
= 0;
4717 * Steal as many blocks as we can to try and satisfy the worst case
4718 * indlen for both new extents.
4720 while (nres
> ores
&& avail
) {
4727 * The only blocks available are those reserved for the original
4728 * extent and what we can steal from the extent being removed.
4729 * If this still isn't enough to satisfy the combined
4730 * requirements for the two new extents, skim blocks off of each
4731 * of the new reservations until they match what is available.
4733 while (nres
> ores
) {
4753 * Called by xfs_bmapi to update file extent records and the btree
4754 * after removing space (or undoing a delayed allocation).
4756 STATIC
int /* error */
4757 xfs_bmap_del_extent(
4758 xfs_inode_t
*ip
, /* incore inode pointer */
4759 xfs_trans_t
*tp
, /* current transaction pointer */
4760 xfs_extnum_t
*idx
, /* extent number to update/delete */
4761 struct xfs_defer_ops
*dfops
, /* list of extents to be freed */
4762 xfs_btree_cur_t
*cur
, /* if null, not a btree */
4763 xfs_bmbt_irec_t
*del
, /* data to remove from extents */
4764 int *logflagsp
, /* inode logging flags */
4765 int whichfork
) /* data or attr fork */
4767 xfs_filblks_t da_new
; /* new delay-alloc indirect blocks */
4768 xfs_filblks_t da_old
; /* old delay-alloc indirect blocks */
4769 xfs_fsblock_t del_endblock
=0; /* first block past del */
4770 xfs_fileoff_t del_endoff
; /* first offset past del */
4771 int delay
; /* current block is delayed allocated */
4772 int do_fx
; /* free extent at end of routine */
4773 xfs_bmbt_rec_host_t
*ep
; /* current extent entry pointer */
4774 int error
; /* error return value */
4775 int flags
; /* inode logging flags */
4776 xfs_bmbt_irec_t got
; /* current extent entry */
4777 xfs_fileoff_t got_endoff
; /* first offset past got */
4778 int i
; /* temp state */
4779 xfs_ifork_t
*ifp
; /* inode fork pointer */
4780 xfs_mount_t
*mp
; /* mount structure */
4781 xfs_filblks_t nblks
; /* quota/sb block count */
4782 xfs_bmbt_irec_t
new; /* new record to be inserted */
4784 uint qfield
; /* quota field to update */
4785 xfs_filblks_t temp
; /* for indirect length calculations */
4786 xfs_filblks_t temp2
; /* for indirect length calculations */
4790 XFS_STATS_INC(mp
, xs_del_exlist
);
4792 if (whichfork
== XFS_ATTR_FORK
)
4793 state
|= BMAP_ATTRFORK
;
4795 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4796 ASSERT((*idx
>= 0) && (*idx
< ifp
->if_bytes
/
4797 (uint
)sizeof(xfs_bmbt_rec_t
)));
4798 ASSERT(del
->br_blockcount
> 0);
4799 ep
= xfs_iext_get_ext(ifp
, *idx
);
4800 xfs_bmbt_get_all(ep
, &got
);
4801 ASSERT(got
.br_startoff
<= del
->br_startoff
);
4802 del_endoff
= del
->br_startoff
+ del
->br_blockcount
;
4803 got_endoff
= got
.br_startoff
+ got
.br_blockcount
;
4804 ASSERT(got_endoff
>= del_endoff
);
4805 delay
= isnullstartblock(got
.br_startblock
);
4806 ASSERT(isnullstartblock(del
->br_startblock
) == delay
);
4811 * If deleting a real allocation, must free up the disk space.
4814 flags
= XFS_ILOG_CORE
;
4816 * Realtime allocation. Free it and record di_nblocks update.
4818 if (whichfork
== XFS_DATA_FORK
&& XFS_IS_REALTIME_INODE(ip
)) {
4822 ASSERT(do_mod(del
->br_blockcount
,
4823 mp
->m_sb
.sb_rextsize
) == 0);
4824 ASSERT(do_mod(del
->br_startblock
,
4825 mp
->m_sb
.sb_rextsize
) == 0);
4826 bno
= del
->br_startblock
;
4827 len
= del
->br_blockcount
;
4828 do_div(bno
, mp
->m_sb
.sb_rextsize
);
4829 do_div(len
, mp
->m_sb
.sb_rextsize
);
4830 error
= xfs_rtfree_extent(tp
, bno
, (xfs_extlen_t
)len
);
4834 nblks
= len
* mp
->m_sb
.sb_rextsize
;
4835 qfield
= XFS_TRANS_DQ_RTBCOUNT
;
4838 * Ordinary allocation.
4842 nblks
= del
->br_blockcount
;
4843 qfield
= XFS_TRANS_DQ_BCOUNT
;
4846 * Set up del_endblock and cur for later.
4848 del_endblock
= del
->br_startblock
+ del
->br_blockcount
;
4850 if ((error
= xfs_bmbt_lookup_eq(cur
, got
.br_startoff
,
4851 got
.br_startblock
, got
.br_blockcount
,
4854 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
4856 da_old
= da_new
= 0;
4858 da_old
= startblockval(got
.br_startblock
);
4865 * Set flag value to use in switch statement.
4866 * Left-contig is 2, right-contig is 1.
4868 switch (((got
.br_startoff
== del
->br_startoff
) << 1) |
4869 (got_endoff
== del_endoff
)) {
4872 * Matches the whole extent. Delete the entry.
4874 xfs_iext_remove(ip
, *idx
, 1,
4875 whichfork
== XFS_ATTR_FORK
? BMAP_ATTRFORK
: 0);
4880 XFS_IFORK_NEXT_SET(ip
, whichfork
,
4881 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
4882 flags
|= XFS_ILOG_CORE
;
4884 flags
|= xfs_ilog_fext(whichfork
);
4887 if ((error
= xfs_btree_delete(cur
, &i
)))
4889 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
4894 * Deleting the first part of the extent.
4896 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
4897 xfs_bmbt_set_startoff(ep
, del_endoff
);
4898 temp
= got
.br_blockcount
- del
->br_blockcount
;
4899 xfs_bmbt_set_blockcount(ep
, temp
);
4901 temp
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
4903 xfs_bmbt_set_startblock(ep
, nullstartblock((int)temp
));
4904 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
4908 xfs_bmbt_set_startblock(ep
, del_endblock
);
4909 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
4911 flags
|= xfs_ilog_fext(whichfork
);
4914 if ((error
= xfs_bmbt_update(cur
, del_endoff
, del_endblock
,
4915 got
.br_blockcount
- del
->br_blockcount
,
4922 * Deleting the last part of the extent.
4924 temp
= got
.br_blockcount
- del
->br_blockcount
;
4925 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
4926 xfs_bmbt_set_blockcount(ep
, temp
);
4928 temp
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
4930 xfs_bmbt_set_startblock(ep
, nullstartblock((int)temp
));
4931 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
4935 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
4937 flags
|= xfs_ilog_fext(whichfork
);
4940 if ((error
= xfs_bmbt_update(cur
, got
.br_startoff
,
4942 got
.br_blockcount
- del
->br_blockcount
,
4949 * Deleting the middle of the extent.
4951 temp
= del
->br_startoff
- got
.br_startoff
;
4952 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
4953 xfs_bmbt_set_blockcount(ep
, temp
);
4954 new.br_startoff
= del_endoff
;
4955 temp2
= got_endoff
- del_endoff
;
4956 new.br_blockcount
= temp2
;
4957 new.br_state
= got
.br_state
;
4959 new.br_startblock
= del_endblock
;
4960 flags
|= XFS_ILOG_CORE
;
4962 if ((error
= xfs_bmbt_update(cur
,
4964 got
.br_startblock
, temp
,
4967 if ((error
= xfs_btree_increment(cur
, 0, &i
)))
4969 cur
->bc_rec
.b
= new;
4970 error
= xfs_btree_insert(cur
, &i
);
4971 if (error
&& error
!= -ENOSPC
)
4974 * If get no-space back from btree insert,
4975 * it tried a split, and we have a zero
4976 * block reservation.
4977 * Fix up our state and return the error.
4979 if (error
== -ENOSPC
) {
4981 * Reset the cursor, don't trust
4982 * it after any insert operation.
4984 if ((error
= xfs_bmbt_lookup_eq(cur
,
4989 XFS_WANT_CORRUPTED_GOTO(mp
,
4992 * Update the btree record back
4993 * to the original value.
4995 if ((error
= xfs_bmbt_update(cur
,
5002 * Reset the extent record back
5003 * to the original value.
5005 xfs_bmbt_set_blockcount(ep
,
5011 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
5013 flags
|= xfs_ilog_fext(whichfork
);
5014 XFS_IFORK_NEXT_SET(ip
, whichfork
,
5015 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
5017 xfs_filblks_t stolen
;
5018 ASSERT(whichfork
== XFS_DATA_FORK
);
5021 * Distribute the original indlen reservation across the
5022 * two new extents. Steal blocks from the deleted extent
5023 * if necessary. Stealing blocks simply fudges the
5024 * fdblocks accounting in xfs_bunmapi().
5026 temp
= xfs_bmap_worst_indlen(ip
, got
.br_blockcount
);
5027 temp2
= xfs_bmap_worst_indlen(ip
, new.br_blockcount
);
5028 stolen
= xfs_bmap_split_indlen(da_old
, &temp
, &temp2
,
5029 del
->br_blockcount
);
5030 da_new
= temp
+ temp2
- stolen
;
5031 del
->br_blockcount
-= stolen
;
5034 * Set the reservation for each extent. Warn if either
5035 * is zero as this can lead to delalloc problems.
5037 WARN_ON_ONCE(!temp
|| !temp2
);
5038 xfs_bmbt_set_startblock(ep
, nullstartblock((int)temp
));
5039 new.br_startblock
= nullstartblock((int)temp2
);
5041 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
5042 xfs_iext_insert(ip
, *idx
+ 1, 1, &new, state
);
5047 /* remove reverse mapping */
5049 error
= xfs_rmap_unmap_extent(mp
, dfops
, ip
, whichfork
, del
);
5055 * If we need to, add to list of extents to delete.
5058 xfs_bmap_add_free(mp
, dfops
, del
->br_startblock
,
5059 del
->br_blockcount
, NULL
);
5061 * Adjust inode # blocks in the file.
5064 ip
->i_d
.di_nblocks
-= nblks
;
5066 * Adjust quota data.
5069 xfs_trans_mod_dquot_byino(tp
, ip
, qfield
, (long)-nblks
);
5072 * Account for change in delayed indirect blocks.
5073 * Nothing to do for disk quota accounting here.
5075 ASSERT(da_old
>= da_new
);
5076 if (da_old
> da_new
)
5077 xfs_mod_fdblocks(mp
, (int64_t)(da_old
- da_new
), false);
5084 * Unmap (remove) blocks from a file.
5085 * If nexts is nonzero then the number of extents to remove is limited to
5086 * that value. If not all extents in the block range can be removed then
5091 xfs_trans_t
*tp
, /* transaction pointer */
5092 struct xfs_inode
*ip
, /* incore inode */
5093 xfs_fileoff_t bno
, /* starting offset to unmap */
5094 xfs_filblks_t len
, /* length to unmap in file */
5095 int flags
, /* misc flags */
5096 xfs_extnum_t nexts
, /* number of extents max */
5097 xfs_fsblock_t
*firstblock
, /* first allocated block
5098 controls a.g. for allocs */
5099 struct xfs_defer_ops
*dfops
, /* i/o: list extents to free */
5100 int *done
) /* set if not done yet */
5102 xfs_btree_cur_t
*cur
; /* bmap btree cursor */
5103 xfs_bmbt_irec_t del
; /* extent being deleted */
5104 int eof
; /* is deleting at eof */
5105 xfs_bmbt_rec_host_t
*ep
; /* extent record pointer */
5106 int error
; /* error return value */
5107 xfs_extnum_t extno
; /* extent number in list */
5108 xfs_bmbt_irec_t got
; /* current extent record */
5109 xfs_ifork_t
*ifp
; /* inode fork pointer */
5110 int isrt
; /* freeing in rt area */
5111 xfs_extnum_t lastx
; /* last extent index used */
5112 int logflags
; /* transaction logging flags */
5113 xfs_extlen_t mod
; /* rt extent offset */
5114 xfs_mount_t
*mp
; /* mount structure */
5115 xfs_extnum_t nextents
; /* number of file extents */
5116 xfs_bmbt_irec_t prev
; /* previous extent record */
5117 xfs_fileoff_t start
; /* first file offset deleted */
5118 int tmp_logflags
; /* partial logging flags */
5119 int wasdel
; /* was a delayed alloc extent */
5120 int whichfork
; /* data or attribute fork */
5123 trace_xfs_bunmap(ip
, bno
, len
, flags
, _RET_IP_
);
5125 whichfork
= (flags
& XFS_BMAPI_ATTRFORK
) ?
5126 XFS_ATTR_FORK
: XFS_DATA_FORK
;
5127 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5129 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
5130 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)) {
5131 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW
,
5133 return -EFSCORRUPTED
;
5136 if (XFS_FORCED_SHUTDOWN(mp
))
5139 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
5143 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
5144 (error
= xfs_iread_extents(tp
, ip
, whichfork
)))
5146 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
5147 if (nextents
== 0) {
5151 XFS_STATS_INC(mp
, xs_blk_unmap
);
5152 isrt
= (whichfork
== XFS_DATA_FORK
) && XFS_IS_REALTIME_INODE(ip
);
5154 bno
= start
+ len
- 1;
5155 ep
= xfs_bmap_search_extents(ip
, bno
, whichfork
, &eof
, &lastx
, &got
,
5159 * Check to see if the given block number is past the end of the
5160 * file, back up to the last block if so...
5163 ep
= xfs_iext_get_ext(ifp
, --lastx
);
5164 xfs_bmbt_get_all(ep
, &got
);
5165 bno
= got
.br_startoff
+ got
.br_blockcount
- 1;
5168 if (ifp
->if_flags
& XFS_IFBROOT
) {
5169 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
);
5170 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
5171 cur
->bc_private
.b
.firstblock
= *firstblock
;
5172 cur
->bc_private
.b
.dfops
= dfops
;
5173 cur
->bc_private
.b
.flags
= 0;
5179 * Synchronize by locking the bitmap inode.
5181 xfs_ilock(mp
->m_rbmip
, XFS_ILOCK_EXCL
|XFS_ILOCK_RTBITMAP
);
5182 xfs_trans_ijoin(tp
, mp
->m_rbmip
, XFS_ILOCK_EXCL
);
5183 xfs_ilock(mp
->m_rsumip
, XFS_ILOCK_EXCL
|XFS_ILOCK_RTSUM
);
5184 xfs_trans_ijoin(tp
, mp
->m_rsumip
, XFS_ILOCK_EXCL
);
5188 while (bno
!= (xfs_fileoff_t
)-1 && bno
>= start
&& lastx
>= 0 &&
5189 (nexts
== 0 || extno
< nexts
)) {
5191 * Is the found extent after a hole in which bno lives?
5192 * Just back up to the previous extent, if so.
5194 if (got
.br_startoff
> bno
) {
5197 ep
= xfs_iext_get_ext(ifp
, lastx
);
5198 xfs_bmbt_get_all(ep
, &got
);
5201 * Is the last block of this extent before the range
5202 * we're supposed to delete? If so, we're done.
5204 bno
= XFS_FILEOFF_MIN(bno
,
5205 got
.br_startoff
+ got
.br_blockcount
- 1);
5209 * Then deal with the (possibly delayed) allocated space
5214 wasdel
= isnullstartblock(del
.br_startblock
);
5215 if (got
.br_startoff
< start
) {
5216 del
.br_startoff
= start
;
5217 del
.br_blockcount
-= start
- got
.br_startoff
;
5219 del
.br_startblock
+= start
- got
.br_startoff
;
5221 if (del
.br_startoff
+ del
.br_blockcount
> bno
+ 1)
5222 del
.br_blockcount
= bno
+ 1 - del
.br_startoff
;
5223 sum
= del
.br_startblock
+ del
.br_blockcount
;
5225 (mod
= do_mod(sum
, mp
->m_sb
.sb_rextsize
))) {
5227 * Realtime extent not lined up at the end.
5228 * The extent could have been split into written
5229 * and unwritten pieces, or we could just be
5230 * unmapping part of it. But we can't really
5231 * get rid of part of a realtime extent.
5233 if (del
.br_state
== XFS_EXT_UNWRITTEN
||
5234 !xfs_sb_version_hasextflgbit(&mp
->m_sb
)) {
5236 * This piece is unwritten, or we're not
5237 * using unwritten extents. Skip over it.
5240 bno
-= mod
> del
.br_blockcount
?
5241 del
.br_blockcount
: mod
;
5242 if (bno
< got
.br_startoff
) {
5244 xfs_bmbt_get_all(xfs_iext_get_ext(
5250 * It's written, turn it unwritten.
5251 * This is better than zeroing it.
5253 ASSERT(del
.br_state
== XFS_EXT_NORM
);
5254 ASSERT(tp
->t_blk_res
> 0);
5256 * If this spans a realtime extent boundary,
5257 * chop it back to the start of the one we end at.
5259 if (del
.br_blockcount
> mod
) {
5260 del
.br_startoff
+= del
.br_blockcount
- mod
;
5261 del
.br_startblock
+= del
.br_blockcount
- mod
;
5262 del
.br_blockcount
= mod
;
5264 del
.br_state
= XFS_EXT_UNWRITTEN
;
5265 error
= xfs_bmap_add_extent_unwritten_real(tp
, ip
,
5266 &lastx
, &cur
, &del
, firstblock
, dfops
,
5272 if (isrt
&& (mod
= do_mod(del
.br_startblock
, mp
->m_sb
.sb_rextsize
))) {
5274 * Realtime extent is lined up at the end but not
5275 * at the front. We'll get rid of full extents if
5278 mod
= mp
->m_sb
.sb_rextsize
- mod
;
5279 if (del
.br_blockcount
> mod
) {
5280 del
.br_blockcount
-= mod
;
5281 del
.br_startoff
+= mod
;
5282 del
.br_startblock
+= mod
;
5283 } else if ((del
.br_startoff
== start
&&
5284 (del
.br_state
== XFS_EXT_UNWRITTEN
||
5285 tp
->t_blk_res
== 0)) ||
5286 !xfs_sb_version_hasextflgbit(&mp
->m_sb
)) {
5288 * Can't make it unwritten. There isn't
5289 * a full extent here so just skip it.
5291 ASSERT(bno
>= del
.br_blockcount
);
5292 bno
-= del
.br_blockcount
;
5293 if (got
.br_startoff
> bno
) {
5295 ep
= xfs_iext_get_ext(ifp
,
5297 xfs_bmbt_get_all(ep
, &got
);
5301 } else if (del
.br_state
== XFS_EXT_UNWRITTEN
) {
5303 * This one is already unwritten.
5304 * It must have a written left neighbor.
5305 * Unwrite the killed part of that one and
5309 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
,
5311 ASSERT(prev
.br_state
== XFS_EXT_NORM
);
5312 ASSERT(!isnullstartblock(prev
.br_startblock
));
5313 ASSERT(del
.br_startblock
==
5314 prev
.br_startblock
+ prev
.br_blockcount
);
5315 if (prev
.br_startoff
< start
) {
5316 mod
= start
- prev
.br_startoff
;
5317 prev
.br_blockcount
-= mod
;
5318 prev
.br_startblock
+= mod
;
5319 prev
.br_startoff
= start
;
5321 prev
.br_state
= XFS_EXT_UNWRITTEN
;
5323 error
= xfs_bmap_add_extent_unwritten_real(tp
,
5324 ip
, &lastx
, &cur
, &prev
,
5325 firstblock
, dfops
, &logflags
);
5330 ASSERT(del
.br_state
== XFS_EXT_NORM
);
5331 del
.br_state
= XFS_EXT_UNWRITTEN
;
5332 error
= xfs_bmap_add_extent_unwritten_real(tp
,
5333 ip
, &lastx
, &cur
, &del
,
5334 firstblock
, dfops
, &logflags
);
5342 * If it's the case where the directory code is running
5343 * with no block reservation, and the deleted block is in
5344 * the middle of its extent, and the resulting insert
5345 * of an extent would cause transformation to btree format,
5346 * then reject it. The calling code will then swap
5347 * blocks around instead.
5348 * We have to do this now, rather than waiting for the
5349 * conversion to btree format, since the transaction
5352 if (!wasdel
&& tp
->t_blk_res
== 0 &&
5353 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
&&
5354 XFS_IFORK_NEXTENTS(ip
, whichfork
) >= /* Note the >= */
5355 XFS_IFORK_MAXEXT(ip
, whichfork
) &&
5356 del
.br_startoff
> got
.br_startoff
&&
5357 del
.br_startoff
+ del
.br_blockcount
<
5358 got
.br_startoff
+ got
.br_blockcount
) {
5364 * Unreserve quota and update realtime free space, if
5365 * appropriate. If delayed allocation, update the inode delalloc
5366 * counter now and wait to update the sb counters as
5367 * xfs_bmap_del_extent() might need to borrow some blocks.
5370 ASSERT(startblockval(del
.br_startblock
) > 0);
5372 xfs_filblks_t rtexts
;
5374 rtexts
= XFS_FSB_TO_B(mp
, del
.br_blockcount
);
5375 do_div(rtexts
, mp
->m_sb
.sb_rextsize
);
5376 xfs_mod_frextents(mp
, (int64_t)rtexts
);
5377 (void)xfs_trans_reserve_quota_nblks(NULL
,
5378 ip
, -((long)del
.br_blockcount
), 0,
5379 XFS_QMOPT_RES_RTBLKS
);
5381 (void)xfs_trans_reserve_quota_nblks(NULL
,
5382 ip
, -((long)del
.br_blockcount
), 0,
5383 XFS_QMOPT_RES_REGBLKS
);
5385 ip
->i_delayed_blks
-= del
.br_blockcount
;
5387 cur
->bc_private
.b
.flags
|=
5388 XFS_BTCUR_BPRV_WASDEL
;
5390 cur
->bc_private
.b
.flags
&= ~XFS_BTCUR_BPRV_WASDEL
;
5392 error
= xfs_bmap_del_extent(ip
, tp
, &lastx
, dfops
, cur
, &del
,
5393 &tmp_logflags
, whichfork
);
5394 logflags
|= tmp_logflags
;
5398 if (!isrt
&& wasdel
)
5399 xfs_mod_fdblocks(mp
, (int64_t)del
.br_blockcount
, false);
5401 bno
= del
.br_startoff
- 1;
5404 * If not done go on to the next (previous) record.
5406 if (bno
!= (xfs_fileoff_t
)-1 && bno
>= start
) {
5408 ep
= xfs_iext_get_ext(ifp
, lastx
);
5409 if (xfs_bmbt_get_startoff(ep
) > bno
) {
5411 ep
= xfs_iext_get_ext(ifp
,
5414 xfs_bmbt_get_all(ep
, &got
);
5419 *done
= bno
== (xfs_fileoff_t
)-1 || bno
< start
|| lastx
< 0;
5422 * Convert to a btree if necessary.
5424 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
5425 ASSERT(cur
== NULL
);
5426 error
= xfs_bmap_extents_to_btree(tp
, ip
, firstblock
, dfops
,
5427 &cur
, 0, &tmp_logflags
, whichfork
);
5428 logflags
|= tmp_logflags
;
5433 * transform from btree to extents, give it cur
5435 else if (xfs_bmap_wants_extents(ip
, whichfork
)) {
5436 ASSERT(cur
!= NULL
);
5437 error
= xfs_bmap_btree_to_extents(tp
, ip
, cur
, &tmp_logflags
,
5439 logflags
|= tmp_logflags
;
5444 * transform from extents to local?
5449 * Log everything. Do this after conversion, there's no point in
5450 * logging the extent records if we've converted to btree format.
5452 if ((logflags
& xfs_ilog_fext(whichfork
)) &&
5453 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
5454 logflags
&= ~xfs_ilog_fext(whichfork
);
5455 else if ((logflags
& xfs_ilog_fbroot(whichfork
)) &&
5456 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)
5457 logflags
&= ~xfs_ilog_fbroot(whichfork
);
5459 * Log inode even in the error case, if the transaction
5460 * is dirty we'll need to shut down the filesystem.
5463 xfs_trans_log_inode(tp
, ip
, logflags
);
5466 *firstblock
= cur
->bc_private
.b
.firstblock
;
5467 cur
->bc_private
.b
.allocated
= 0;
5469 xfs_btree_del_cursor(cur
,
5470 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
5476 * Determine whether an extent shift can be accomplished by a merge with the
5477 * extent that precedes the target hole of the shift.
5481 struct xfs_bmbt_irec
*left
, /* preceding extent */
5482 struct xfs_bmbt_irec
*got
, /* current extent to shift */
5483 xfs_fileoff_t shift
) /* shift fsb */
5485 xfs_fileoff_t startoff
;
5487 startoff
= got
->br_startoff
- shift
;
5490 * The extent, once shifted, must be adjacent in-file and on-disk with
5491 * the preceding extent.
5493 if ((left
->br_startoff
+ left
->br_blockcount
!= startoff
) ||
5494 (left
->br_startblock
+ left
->br_blockcount
!= got
->br_startblock
) ||
5495 (left
->br_state
!= got
->br_state
) ||
5496 (left
->br_blockcount
+ got
->br_blockcount
> MAXEXTLEN
))
5503 * A bmap extent shift adjusts the file offset of an extent to fill a preceding
5504 * hole in the file. If an extent shift would result in the extent being fully
5505 * adjacent to the extent that currently precedes the hole, we can merge with
5506 * the preceding extent rather than do the shift.
5508 * This function assumes the caller has verified a shift-by-merge is possible
5509 * with the provided extents via xfs_bmse_can_merge().
5513 struct xfs_inode
*ip
,
5515 xfs_fileoff_t shift
, /* shift fsb */
5516 int current_ext
, /* idx of gotp */
5517 struct xfs_bmbt_rec_host
*gotp
, /* extent to shift */
5518 struct xfs_bmbt_rec_host
*leftp
, /* preceding extent */
5519 struct xfs_btree_cur
*cur
,
5520 int *logflags
) /* output */
5522 struct xfs_bmbt_irec got
;
5523 struct xfs_bmbt_irec left
;
5524 xfs_filblks_t blockcount
;
5526 struct xfs_mount
*mp
= ip
->i_mount
;
5528 xfs_bmbt_get_all(gotp
, &got
);
5529 xfs_bmbt_get_all(leftp
, &left
);
5530 blockcount
= left
.br_blockcount
+ got
.br_blockcount
;
5532 ASSERT(xfs_isilocked(ip
, XFS_IOLOCK_EXCL
));
5533 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
5534 ASSERT(xfs_bmse_can_merge(&left
, &got
, shift
));
5537 * Merge the in-core extents. Note that the host record pointers and
5538 * current_ext index are invalid once the extent has been removed via
5539 * xfs_iext_remove().
5541 xfs_bmbt_set_blockcount(leftp
, blockcount
);
5542 xfs_iext_remove(ip
, current_ext
, 1, 0);
5545 * Update the on-disk extent count, the btree if necessary and log the
5548 XFS_IFORK_NEXT_SET(ip
, whichfork
,
5549 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
5550 *logflags
|= XFS_ILOG_CORE
;
5552 *logflags
|= XFS_ILOG_DEXT
;
5556 /* lookup and remove the extent to merge */
5557 error
= xfs_bmbt_lookup_eq(cur
, got
.br_startoff
, got
.br_startblock
,
5558 got
.br_blockcount
, &i
);
5561 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
5563 error
= xfs_btree_delete(cur
, &i
);
5566 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
5568 /* lookup and update size of the previous extent */
5569 error
= xfs_bmbt_lookup_eq(cur
, left
.br_startoff
, left
.br_startblock
,
5570 left
.br_blockcount
, &i
);
5573 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
5575 left
.br_blockcount
= blockcount
;
5577 return xfs_bmbt_update(cur
, left
.br_startoff
, left
.br_startblock
,
5578 left
.br_blockcount
, left
.br_state
);
5582 * Shift a single extent.
5586 struct xfs_inode
*ip
,
5588 xfs_fileoff_t offset_shift_fsb
,
5590 struct xfs_bmbt_rec_host
*gotp
,
5591 struct xfs_btree_cur
*cur
,
5593 enum shift_direction direction
,
5594 struct xfs_defer_ops
*dfops
)
5596 struct xfs_ifork
*ifp
;
5597 struct xfs_mount
*mp
;
5598 xfs_fileoff_t startoff
;
5599 struct xfs_bmbt_rec_host
*adj_irecp
;
5600 struct xfs_bmbt_irec got
;
5601 struct xfs_bmbt_irec adj_irec
;
5607 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5608 total_extents
= ifp
->if_bytes
/ sizeof(xfs_bmbt_rec_t
);
5610 xfs_bmbt_get_all(gotp
, &got
);
5612 /* delalloc extents should be prevented by caller */
5613 XFS_WANT_CORRUPTED_RETURN(mp
, !isnullstartblock(got
.br_startblock
));
5615 if (direction
== SHIFT_LEFT
) {
5616 startoff
= got
.br_startoff
- offset_shift_fsb
;
5619 * Check for merge if we've got an extent to the left,
5620 * otherwise make sure there's enough room at the start
5621 * of the file for the shift.
5623 if (!*current_ext
) {
5624 if (got
.br_startoff
< offset_shift_fsb
)
5626 goto update_current_ext
;
5629 * grab the left extent and check for a large
5632 adj_irecp
= xfs_iext_get_ext(ifp
, *current_ext
- 1);
5633 xfs_bmbt_get_all(adj_irecp
, &adj_irec
);
5636 adj_irec
.br_startoff
+ adj_irec
.br_blockcount
)
5639 /* check whether to merge the extent or shift it down */
5640 if (xfs_bmse_can_merge(&adj_irec
, &got
,
5641 offset_shift_fsb
)) {
5642 error
= xfs_bmse_merge(ip
, whichfork
, offset_shift_fsb
,
5643 *current_ext
, gotp
, adj_irecp
,
5651 startoff
= got
.br_startoff
+ offset_shift_fsb
;
5652 /* nothing to move if this is the last extent */
5653 if (*current_ext
>= (total_extents
- 1))
5654 goto update_current_ext
;
5656 * If this is not the last extent in the file, make sure there
5657 * is enough room between current extent and next extent for
5658 * accommodating the shift.
5660 adj_irecp
= xfs_iext_get_ext(ifp
, *current_ext
+ 1);
5661 xfs_bmbt_get_all(adj_irecp
, &adj_irec
);
5662 if (startoff
+ got
.br_blockcount
> adj_irec
.br_startoff
)
5665 * Unlike a left shift (which involves a hole punch),
5666 * a right shift does not modify extent neighbors
5667 * in any way. We should never find mergeable extents
5668 * in this scenario. Check anyways and warn if we
5669 * encounter two extents that could be one.
5671 if (xfs_bmse_can_merge(&got
, &adj_irec
, offset_shift_fsb
))
5675 * Increment the extent index for the next iteration, update the start
5676 * offset of the in-core extent and update the btree if applicable.
5679 if (direction
== SHIFT_LEFT
)
5683 xfs_bmbt_set_startoff(gotp
, startoff
);
5684 *logflags
|= XFS_ILOG_CORE
;
5687 *logflags
|= XFS_ILOG_DEXT
;
5691 error
= xfs_bmbt_lookup_eq(cur
, got
.br_startoff
, got
.br_startblock
,
5692 got
.br_blockcount
, &i
);
5695 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
5697 got
.br_startoff
= startoff
;
5698 error
= xfs_bmbt_update(cur
, got
.br_startoff
, got
.br_startblock
,
5699 got
.br_blockcount
, got
.br_state
);
5704 /* update reverse mapping */
5705 error
= xfs_rmap_unmap_extent(mp
, dfops
, ip
, whichfork
, &adj_irec
);
5708 adj_irec
.br_startoff
= startoff
;
5709 return xfs_rmap_map_extent(mp
, dfops
, ip
, whichfork
, &adj_irec
);
5713 * Shift extent records to the left/right to cover/create a hole.
5715 * The maximum number of extents to be shifted in a single operation is
5716 * @num_exts. @stop_fsb specifies the file offset at which to stop shift and the
5717 * file offset where we've left off is returned in @next_fsb. @offset_shift_fsb
5718 * is the length by which each extent is shifted. If there is no hole to shift
5719 * the extents into, this will be considered invalid operation and we abort
5723 xfs_bmap_shift_extents(
5724 struct xfs_trans
*tp
,
5725 struct xfs_inode
*ip
,
5726 xfs_fileoff_t
*next_fsb
,
5727 xfs_fileoff_t offset_shift_fsb
,
5729 xfs_fileoff_t stop_fsb
,
5730 xfs_fsblock_t
*firstblock
,
5731 struct xfs_defer_ops
*dfops
,
5732 enum shift_direction direction
,
5735 struct xfs_btree_cur
*cur
= NULL
;
5736 struct xfs_bmbt_rec_host
*gotp
;
5737 struct xfs_bmbt_irec got
;
5738 struct xfs_mount
*mp
= ip
->i_mount
;
5739 struct xfs_ifork
*ifp
;
5740 xfs_extnum_t nexts
= 0;
5741 xfs_extnum_t current_ext
;
5742 xfs_extnum_t total_extents
;
5743 xfs_extnum_t stop_extent
;
5745 int whichfork
= XFS_DATA_FORK
;
5748 if (unlikely(XFS_TEST_ERROR(
5749 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
5750 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
5751 mp
, XFS_ERRTAG_BMAPIFORMAT
, XFS_RANDOM_BMAPIFORMAT
))) {
5752 XFS_ERROR_REPORT("xfs_bmap_shift_extents",
5753 XFS_ERRLEVEL_LOW
, mp
);
5754 return -EFSCORRUPTED
;
5757 if (XFS_FORCED_SHUTDOWN(mp
))
5760 ASSERT(xfs_isilocked(ip
, XFS_IOLOCK_EXCL
));
5761 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
5762 ASSERT(direction
== SHIFT_LEFT
|| direction
== SHIFT_RIGHT
);
5763 ASSERT(*next_fsb
!= NULLFSBLOCK
|| direction
== SHIFT_RIGHT
);
5765 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5766 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
5767 /* Read in all the extents */
5768 error
= xfs_iread_extents(tp
, ip
, whichfork
);
5773 if (ifp
->if_flags
& XFS_IFBROOT
) {
5774 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
5775 cur
->bc_private
.b
.firstblock
= *firstblock
;
5776 cur
->bc_private
.b
.dfops
= dfops
;
5777 cur
->bc_private
.b
.flags
= 0;
5781 * There may be delalloc extents in the data fork before the range we
5782 * are collapsing out, so we cannot use the count of real extents here.
5783 * Instead we have to calculate it from the incore fork.
5785 total_extents
= ifp
->if_bytes
/ sizeof(xfs_bmbt_rec_t
);
5786 if (total_extents
== 0) {
5792 * In case of first right shift, we need to initialize next_fsb
5794 if (*next_fsb
== NULLFSBLOCK
) {
5795 gotp
= xfs_iext_get_ext(ifp
, total_extents
- 1);
5796 xfs_bmbt_get_all(gotp
, &got
);
5797 *next_fsb
= got
.br_startoff
;
5798 if (stop_fsb
> *next_fsb
) {
5804 /* Lookup the extent index at which we have to stop */
5805 if (direction
== SHIFT_RIGHT
) {
5806 gotp
= xfs_iext_bno_to_ext(ifp
, stop_fsb
, &stop_extent
);
5807 /* Make stop_extent exclusive of shift range */
5810 stop_extent
= total_extents
;
5813 * Look up the extent index for the fsb where we start shifting. We can
5814 * henceforth iterate with current_ext as extent list changes are locked
5817 * gotp can be null in 2 cases: 1) if there are no extents or 2)
5818 * *next_fsb lies in a hole beyond which there are no extents. Either
5821 gotp
= xfs_iext_bno_to_ext(ifp
, *next_fsb
, ¤t_ext
);
5827 /* some sanity checking before we finally start shifting extents */
5828 if ((direction
== SHIFT_LEFT
&& current_ext
>= stop_extent
) ||
5829 (direction
== SHIFT_RIGHT
&& current_ext
<= stop_extent
)) {
5834 while (nexts
++ < num_exts
) {
5835 error
= xfs_bmse_shift_one(ip
, whichfork
, offset_shift_fsb
,
5836 ¤t_ext
, gotp
, cur
, &logflags
,
5841 * If there was an extent merge during the shift, the extent
5842 * count can change. Update the total and grade the next record.
5844 if (direction
== SHIFT_LEFT
) {
5845 total_extents
= ifp
->if_bytes
/ sizeof(xfs_bmbt_rec_t
);
5846 stop_extent
= total_extents
;
5849 if (current_ext
== stop_extent
) {
5851 *next_fsb
= NULLFSBLOCK
;
5854 gotp
= xfs_iext_get_ext(ifp
, current_ext
);
5858 xfs_bmbt_get_all(gotp
, &got
);
5859 *next_fsb
= got
.br_startoff
;
5864 xfs_btree_del_cursor(cur
,
5865 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
5868 xfs_trans_log_inode(tp
, ip
, logflags
);
5874 * Splits an extent into two extents at split_fsb block such that it is
5875 * the first block of the current_ext. @current_ext is a target extent
5876 * to be split. @split_fsb is a block where the extents is split.
5877 * If split_fsb lies in a hole or the first block of extents, just return 0.
5880 xfs_bmap_split_extent_at(
5881 struct xfs_trans
*tp
,
5882 struct xfs_inode
*ip
,
5883 xfs_fileoff_t split_fsb
,
5884 xfs_fsblock_t
*firstfsb
,
5885 struct xfs_defer_ops
*dfops
)
5887 int whichfork
= XFS_DATA_FORK
;
5888 struct xfs_btree_cur
*cur
= NULL
;
5889 struct xfs_bmbt_rec_host
*gotp
;
5890 struct xfs_bmbt_irec got
;
5891 struct xfs_bmbt_irec
new; /* split extent */
5892 struct xfs_mount
*mp
= ip
->i_mount
;
5893 struct xfs_ifork
*ifp
;
5894 xfs_fsblock_t gotblkcnt
; /* new block count for got */
5895 xfs_extnum_t current_ext
;
5900 if (unlikely(XFS_TEST_ERROR(
5901 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
5902 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
5903 mp
, XFS_ERRTAG_BMAPIFORMAT
, XFS_RANDOM_BMAPIFORMAT
))) {
5904 XFS_ERROR_REPORT("xfs_bmap_split_extent_at",
5905 XFS_ERRLEVEL_LOW
, mp
);
5906 return -EFSCORRUPTED
;
5909 if (XFS_FORCED_SHUTDOWN(mp
))
5912 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5913 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
5914 /* Read in all the extents */
5915 error
= xfs_iread_extents(tp
, ip
, whichfork
);
5921 * gotp can be null in 2 cases: 1) if there are no extents
5922 * or 2) split_fsb lies in a hole beyond which there are
5923 * no extents. Either way, we are done.
5925 gotp
= xfs_iext_bno_to_ext(ifp
, split_fsb
, ¤t_ext
);
5929 xfs_bmbt_get_all(gotp
, &got
);
5932 * Check split_fsb lies in a hole or the start boundary offset
5935 if (got
.br_startoff
>= split_fsb
)
5938 gotblkcnt
= split_fsb
- got
.br_startoff
;
5939 new.br_startoff
= split_fsb
;
5940 new.br_startblock
= got
.br_startblock
+ gotblkcnt
;
5941 new.br_blockcount
= got
.br_blockcount
- gotblkcnt
;
5942 new.br_state
= got
.br_state
;
5944 if (ifp
->if_flags
& XFS_IFBROOT
) {
5945 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
5946 cur
->bc_private
.b
.firstblock
= *firstfsb
;
5947 cur
->bc_private
.b
.dfops
= dfops
;
5948 cur
->bc_private
.b
.flags
= 0;
5949 error
= xfs_bmbt_lookup_eq(cur
, got
.br_startoff
,
5955 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, del_cursor
);
5958 xfs_bmbt_set_blockcount(gotp
, gotblkcnt
);
5959 got
.br_blockcount
= gotblkcnt
;
5961 logflags
= XFS_ILOG_CORE
;
5963 error
= xfs_bmbt_update(cur
, got
.br_startoff
,
5970 logflags
|= XFS_ILOG_DEXT
;
5972 /* Add new extent */
5974 xfs_iext_insert(ip
, current_ext
, 1, &new, 0);
5975 XFS_IFORK_NEXT_SET(ip
, whichfork
,
5976 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
5979 error
= xfs_bmbt_lookup_eq(cur
, new.br_startoff
,
5980 new.br_startblock
, new.br_blockcount
,
5984 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, del_cursor
);
5985 cur
->bc_rec
.b
.br_state
= new.br_state
;
5987 error
= xfs_btree_insert(cur
, &i
);
5990 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, del_cursor
);
5994 * Convert to a btree if necessary.
5996 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
5997 int tmp_logflags
; /* partial log flag return val */
5999 ASSERT(cur
== NULL
);
6000 error
= xfs_bmap_extents_to_btree(tp
, ip
, firstfsb
, dfops
,
6001 &cur
, 0, &tmp_logflags
, whichfork
);
6002 logflags
|= tmp_logflags
;
6007 cur
->bc_private
.b
.allocated
= 0;
6008 xfs_btree_del_cursor(cur
,
6009 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
6013 xfs_trans_log_inode(tp
, ip
, logflags
);
6018 xfs_bmap_split_extent(
6019 struct xfs_inode
*ip
,
6020 xfs_fileoff_t split_fsb
)
6022 struct xfs_mount
*mp
= ip
->i_mount
;
6023 struct xfs_trans
*tp
;
6024 struct xfs_defer_ops dfops
;
6025 xfs_fsblock_t firstfsb
;
6028 error
= xfs_trans_alloc(mp
, &M_RES(mp
)->tr_write
,
6029 XFS_DIOSTRAT_SPACE_RES(mp
, 0), 0, 0, &tp
);
6033 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
6034 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
);
6036 xfs_defer_init(&dfops
, &firstfsb
);
6038 error
= xfs_bmap_split_extent_at(tp
, ip
, split_fsb
,
6043 error
= xfs_defer_finish(&tp
, &dfops
, NULL
);
6047 return xfs_trans_commit(tp
);
6050 xfs_defer_cancel(&dfops
);
6051 xfs_trans_cancel(tp
);