1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
6 #include "libxfs_priv.h"
8 #include "xfs_shared.h"
9 #include "xfs_format.h"
10 #include "xfs_log_format.h"
11 #include "xfs_trans_resv.h"
14 #include "xfs_mount.h"
15 #include "xfs_defer.h"
16 #include "xfs_da_format.h"
17 #include "xfs_da_btree.h"
19 #include "xfs_inode.h"
20 #include "xfs_btree.h"
21 #include "xfs_trans.h"
22 #include "xfs_alloc.h"
24 #include "xfs_bmap_btree.h"
25 #include "xfs_errortag.h"
26 #include "xfs_trans_space.h"
27 #include "xfs_trace.h"
28 #include "xfs_attr_leaf.h"
29 #include "xfs_quota_defs.h"
31 #include "xfs_ag_resv.h"
32 #include "xfs_refcount.h"
35 kmem_zone_t
*xfs_bmap_free_item_zone
;
38 * Miscellaneous helper functions
42 * Compute and fill in the value of the maximum depth of a bmap btree
43 * in this filesystem. Done once, during mount.
46 xfs_bmap_compute_maxlevels(
47 xfs_mount_t
*mp
, /* file system mount structure */
48 int whichfork
) /* data or attr fork */
50 int level
; /* btree level */
51 uint maxblocks
; /* max blocks at this level */
52 uint maxleafents
; /* max leaf entries possible */
53 int maxrootrecs
; /* max records in root block */
54 int minleafrecs
; /* min records in leaf block */
55 int minnoderecs
; /* min records in node block */
56 int sz
; /* root block size */
59 * The maximum number of extents in a file, hence the maximum
60 * number of leaf entries, is controlled by the type of di_nextents
61 * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
62 * (a signed 16-bit number, xfs_aextnum_t).
64 * Note that we can no longer assume that if we are in ATTR1 that
65 * the fork offset of all the inodes will be
66 * (xfs_default_attroffset(ip) >> 3) because we could have mounted
67 * with ATTR2 and then mounted back with ATTR1, keeping the
68 * di_forkoff's fixed but probably at various positions. Therefore,
69 * for both ATTR1 and ATTR2 we have to assume the worst case scenario
70 * of a minimum size available.
72 if (whichfork
== XFS_DATA_FORK
) {
73 maxleafents
= MAXEXTNUM
;
74 sz
= XFS_BMDR_SPACE_CALC(MINDBTPTRS
);
76 maxleafents
= MAXAEXTNUM
;
77 sz
= XFS_BMDR_SPACE_CALC(MINABTPTRS
);
79 maxrootrecs
= xfs_bmdr_maxrecs(sz
, 0);
80 minleafrecs
= mp
->m_bmap_dmnr
[0];
81 minnoderecs
= mp
->m_bmap_dmnr
[1];
82 maxblocks
= (maxleafents
+ minleafrecs
- 1) / minleafrecs
;
83 for (level
= 1; maxblocks
> 1; level
++) {
84 if (maxblocks
<= maxrootrecs
)
87 maxblocks
= (maxblocks
+ minnoderecs
- 1) / minnoderecs
;
89 mp
->m_bm_maxlevels
[whichfork
] = level
;
92 STATIC
int /* error */
94 struct xfs_btree_cur
*cur
,
95 struct xfs_bmbt_irec
*irec
,
96 int *stat
) /* success/failure */
98 cur
->bc_rec
.b
= *irec
;
99 return xfs_btree_lookup(cur
, XFS_LOOKUP_EQ
, stat
);
102 STATIC
int /* error */
103 xfs_bmbt_lookup_first(
104 struct xfs_btree_cur
*cur
,
105 int *stat
) /* success/failure */
107 cur
->bc_rec
.b
.br_startoff
= 0;
108 cur
->bc_rec
.b
.br_startblock
= 0;
109 cur
->bc_rec
.b
.br_blockcount
= 0;
110 return xfs_btree_lookup(cur
, XFS_LOOKUP_GE
, stat
);
114 * Check if the inode needs to be converted to btree format.
116 static inline bool xfs_bmap_needs_btree(struct xfs_inode
*ip
, int whichfork
)
118 return whichfork
!= XFS_COW_FORK
&&
119 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
&&
120 XFS_IFORK_NEXTENTS(ip
, whichfork
) >
121 XFS_IFORK_MAXEXT(ip
, whichfork
);
125 * Check if the inode should be converted to extent format.
127 static inline bool xfs_bmap_wants_extents(struct xfs_inode
*ip
, int whichfork
)
129 return whichfork
!= XFS_COW_FORK
&&
130 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
&&
131 XFS_IFORK_NEXTENTS(ip
, whichfork
) <=
132 XFS_IFORK_MAXEXT(ip
, whichfork
);
136 * Update the record referred to by cur to the value given by irec
137 * This either works (return 0) or gets an EFSCORRUPTED error.
141 struct xfs_btree_cur
*cur
,
142 struct xfs_bmbt_irec
*irec
)
144 union xfs_btree_rec rec
;
146 xfs_bmbt_disk_set_all(&rec
.bmbt
, irec
);
147 return xfs_btree_update(cur
, &rec
);
151 * Compute the worst-case number of indirect blocks that will be used
152 * for ip's delayed extent of length "len".
155 xfs_bmap_worst_indlen(
156 xfs_inode_t
*ip
, /* incore inode pointer */
157 xfs_filblks_t len
) /* delayed extent length */
159 int level
; /* btree level number */
160 int maxrecs
; /* maximum record count at this level */
161 xfs_mount_t
*mp
; /* mount structure */
162 xfs_filblks_t rval
; /* return value */
165 maxrecs
= mp
->m_bmap_dmxr
[0];
166 for (level
= 0, rval
= 0;
167 level
< XFS_BM_MAXLEVELS(mp
, XFS_DATA_FORK
);
170 do_div(len
, maxrecs
);
173 return rval
+ XFS_BM_MAXLEVELS(mp
, XFS_DATA_FORK
) -
176 maxrecs
= mp
->m_bmap_dmxr
[1];
182 * Calculate the default attribute fork offset for newly created inodes.
185 xfs_default_attroffset(
186 struct xfs_inode
*ip
)
188 struct xfs_mount
*mp
= ip
->i_mount
;
191 if (mp
->m_sb
.sb_inodesize
== 256) {
192 offset
= XFS_LITINO(mp
, ip
->i_d
.di_version
) -
193 XFS_BMDR_SPACE_CALC(MINABTPTRS
);
195 offset
= XFS_BMDR_SPACE_CALC(6 * MINABTPTRS
);
198 ASSERT(offset
< XFS_LITINO(mp
, ip
->i_d
.di_version
));
203 * Helper routine to reset inode di_forkoff field when switching
204 * attribute fork from local to extent format - we reset it where
205 * possible to make space available for inline data fork extents.
208 xfs_bmap_forkoff_reset(
212 if (whichfork
== XFS_ATTR_FORK
&&
213 ip
->i_d
.di_format
!= XFS_DINODE_FMT_DEV
&&
214 ip
->i_d
.di_format
!= XFS_DINODE_FMT_BTREE
) {
215 uint dfl_forkoff
= xfs_default_attroffset(ip
) >> 3;
217 if (dfl_forkoff
> ip
->i_d
.di_forkoff
)
218 ip
->i_d
.di_forkoff
= dfl_forkoff
;
223 STATIC
struct xfs_buf
*
225 struct xfs_btree_cur
*cur
,
228 struct xfs_log_item
*lip
;
234 for (i
= 0; i
< XFS_BTREE_MAXLEVELS
; i
++) {
235 if (!cur
->bc_bufs
[i
])
237 if (XFS_BUF_ADDR(cur
->bc_bufs
[i
]) == bno
)
238 return cur
->bc_bufs
[i
];
241 /* Chase down all the log items to see if the bp is there */
242 list_for_each_entry(lip
, &cur
->bc_tp
->t_items
, li_trans
) {
243 struct xfs_buf_log_item
*bip
= (struct xfs_buf_log_item
*)lip
;
245 if (bip
->bli_item
.li_type
== XFS_LI_BUF
&&
246 XFS_BUF_ADDR(bip
->bli_buf
) == bno
)
255 struct xfs_btree_block
*block
,
261 __be64
*pp
, *thispa
; /* pointer to block address */
262 xfs_bmbt_key_t
*prevp
, *keyp
;
264 ASSERT(be16_to_cpu(block
->bb_level
) > 0);
267 for( i
= 1; i
<= xfs_btree_get_numrecs(block
); i
++) {
268 dmxr
= mp
->m_bmap_dmxr
[0];
269 keyp
= XFS_BMBT_KEY_ADDR(mp
, block
, i
);
272 ASSERT(be64_to_cpu(prevp
->br_startoff
) <
273 be64_to_cpu(keyp
->br_startoff
));
278 * Compare the block numbers to see if there are dups.
281 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, i
, sz
);
283 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, i
, dmxr
);
285 for (j
= i
+1; j
<= be16_to_cpu(block
->bb_numrecs
); j
++) {
287 thispa
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, j
, sz
);
289 thispa
= XFS_BMBT_PTR_ADDR(mp
, block
, j
, dmxr
);
290 if (*thispa
== *pp
) {
291 xfs_warn(mp
, "%s: thispa(%d) == pp(%d) %Ld",
293 (unsigned long long)be64_to_cpu(*thispa
));
294 xfs_err(mp
, "%s: ptrs are equal in node\n",
296 xfs_force_shutdown(mp
, SHUTDOWN_CORRUPT_INCORE
);
303 * Check that the extents for the inode ip are in the right order in all
304 * btree leaves. THis becomes prohibitively expensive for large extent count
305 * files, so don't bother with inodes that have more than 10,000 extents in
306 * them. The btree record ordering checks will still be done, so for such large
307 * bmapbt constructs that is going to catch most corruptions.
310 xfs_bmap_check_leaf_extents(
311 xfs_btree_cur_t
*cur
, /* btree cursor or null */
312 xfs_inode_t
*ip
, /* incore inode pointer */
313 int whichfork
) /* data or attr fork */
315 struct xfs_btree_block
*block
; /* current btree block */
316 xfs_fsblock_t bno
; /* block # of "block" */
317 xfs_buf_t
*bp
; /* buffer for "block" */
318 int error
; /* error return value */
319 xfs_extnum_t i
=0, j
; /* index into the extents list */
320 xfs_ifork_t
*ifp
; /* fork structure */
321 int level
; /* btree level, for checking */
322 xfs_mount_t
*mp
; /* file system mount structure */
323 __be64
*pp
; /* pointer to block address */
324 xfs_bmbt_rec_t
*ep
; /* pointer to current extent */
325 xfs_bmbt_rec_t last
= {0, 0}; /* last extent in prev block */
326 xfs_bmbt_rec_t
*nextp
; /* pointer to next extent */
329 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
) {
333 /* skip large extent count inodes */
334 if (ip
->i_d
.di_nextents
> 10000)
339 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
340 block
= ifp
->if_broot
;
342 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
344 level
= be16_to_cpu(block
->bb_level
);
346 xfs_check_block(block
, mp
, 1, ifp
->if_broot_bytes
);
347 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, 1, ifp
->if_broot_bytes
);
348 bno
= be64_to_cpu(*pp
);
350 ASSERT(bno
!= NULLFSBLOCK
);
351 ASSERT(XFS_FSB_TO_AGNO(mp
, bno
) < mp
->m_sb
.sb_agcount
);
352 ASSERT(XFS_FSB_TO_AGBNO(mp
, bno
) < mp
->m_sb
.sb_agblocks
);
355 * Go down the tree until leaf level is reached, following the first
356 * pointer (leftmost) at each level.
358 while (level
-- > 0) {
359 /* See if buf is in cur first */
361 bp
= xfs_bmap_get_bp(cur
, XFS_FSB_TO_DADDR(mp
, bno
));
364 error
= xfs_btree_read_bufl(mp
, NULL
, bno
, 0, &bp
,
370 block
= XFS_BUF_TO_BLOCK(bp
);
375 * Check this block for basic sanity (increasing keys and
376 * no duplicate blocks).
379 xfs_check_block(block
, mp
, 0, 0);
380 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, mp
->m_bmap_dmxr
[1]);
381 bno
= be64_to_cpu(*pp
);
382 XFS_WANT_CORRUPTED_GOTO(mp
,
383 xfs_verify_fsbno(mp
, bno
), error0
);
386 xfs_trans_brelse(NULL
, bp
);
391 * Here with bp and block set to the leftmost leaf node in the tree.
396 * Loop over all leaf nodes checking that all extents are in the right order.
399 xfs_fsblock_t nextbno
;
400 xfs_extnum_t num_recs
;
403 num_recs
= xfs_btree_get_numrecs(block
);
406 * Read-ahead the next leaf block, if any.
409 nextbno
= be64_to_cpu(block
->bb_u
.l
.bb_rightsib
);
412 * Check all the extents to make sure they are OK.
413 * If we had a previous block, the last entry should
414 * conform with the first entry in this one.
417 ep
= XFS_BMBT_REC_ADDR(mp
, block
, 1);
419 ASSERT(xfs_bmbt_disk_get_startoff(&last
) +
420 xfs_bmbt_disk_get_blockcount(&last
) <=
421 xfs_bmbt_disk_get_startoff(ep
));
423 for (j
= 1; j
< num_recs
; j
++) {
424 nextp
= XFS_BMBT_REC_ADDR(mp
, block
, j
+ 1);
425 ASSERT(xfs_bmbt_disk_get_startoff(ep
) +
426 xfs_bmbt_disk_get_blockcount(ep
) <=
427 xfs_bmbt_disk_get_startoff(nextp
));
435 xfs_trans_brelse(NULL
, bp
);
439 * If we've reached the end, stop.
441 if (bno
== NULLFSBLOCK
)
445 bp
= xfs_bmap_get_bp(cur
, XFS_FSB_TO_DADDR(mp
, bno
));
448 error
= xfs_btree_read_bufl(mp
, NULL
, bno
, 0, &bp
,
454 block
= XFS_BUF_TO_BLOCK(bp
);
460 xfs_warn(mp
, "%s: at error0", __func__
);
462 xfs_trans_brelse(NULL
, bp
);
464 xfs_warn(mp
, "%s: BAD after btree leaves for %d extents",
466 xfs_err(mp
, "%s: CORRUPTED BTREE OR SOMETHING", __func__
);
467 xfs_force_shutdown(mp
, SHUTDOWN_CORRUPT_INCORE
);
472 * Validate that the bmbt_irecs being returned from bmapi are valid
473 * given the caller's original parameters. Specifically check the
474 * ranges of the returned irecs to ensure that they only extend beyond
475 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
478 xfs_bmap_validate_ret(
482 xfs_bmbt_irec_t
*mval
,
486 int i
; /* index to map values */
488 ASSERT(ret_nmap
<= nmap
);
490 for (i
= 0; i
< ret_nmap
; i
++) {
491 ASSERT(mval
[i
].br_blockcount
> 0);
492 if (!(flags
& XFS_BMAPI_ENTIRE
)) {
493 ASSERT(mval
[i
].br_startoff
>= bno
);
494 ASSERT(mval
[i
].br_blockcount
<= len
);
495 ASSERT(mval
[i
].br_startoff
+ mval
[i
].br_blockcount
<=
498 ASSERT(mval
[i
].br_startoff
< bno
+ len
);
499 ASSERT(mval
[i
].br_startoff
+ mval
[i
].br_blockcount
>
503 mval
[i
- 1].br_startoff
+ mval
[i
- 1].br_blockcount
==
504 mval
[i
].br_startoff
);
505 ASSERT(mval
[i
].br_startblock
!= DELAYSTARTBLOCK
&&
506 mval
[i
].br_startblock
!= HOLESTARTBLOCK
);
507 ASSERT(mval
[i
].br_state
== XFS_EXT_NORM
||
508 mval
[i
].br_state
== XFS_EXT_UNWRITTEN
);
513 #define xfs_bmap_check_leaf_extents(cur, ip, whichfork) do { } while (0)
514 #define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap) do { } while (0)
518 * bmap free list manipulation functions
522 * Add the extent to the list of extents to be free at transaction end.
523 * The list is maintained sorted (by block number).
527 struct xfs_mount
*mp
,
528 struct xfs_defer_ops
*dfops
,
531 struct xfs_owner_info
*oinfo
,
534 struct xfs_extent_free_item
*new; /* new element */
539 ASSERT(bno
!= NULLFSBLOCK
);
541 ASSERT(len
<= MAXEXTLEN
);
542 ASSERT(!isnullstartblock(bno
));
543 agno
= XFS_FSB_TO_AGNO(mp
, bno
);
544 agbno
= XFS_FSB_TO_AGBNO(mp
, bno
);
545 ASSERT(agno
< mp
->m_sb
.sb_agcount
);
546 ASSERT(agbno
< mp
->m_sb
.sb_agblocks
);
547 ASSERT(len
< mp
->m_sb
.sb_agblocks
);
548 ASSERT(agbno
+ len
<= mp
->m_sb
.sb_agblocks
);
550 ASSERT(xfs_bmap_free_item_zone
!= NULL
);
552 new = kmem_zone_alloc(xfs_bmap_free_item_zone
, KM_SLEEP
);
553 new->xefi_startblock
= bno
;
554 new->xefi_blockcount
= (xfs_extlen_t
)len
;
556 new->xefi_oinfo
= *oinfo
;
558 xfs_rmap_skip_owner_update(&new->xefi_oinfo
);
559 new->xefi_skip_discard
= skip_discard
;
560 trace_xfs_bmap_free_defer(mp
, XFS_FSB_TO_AGNO(mp
, bno
), 0,
561 XFS_FSB_TO_AGBNO(mp
, bno
), len
);
562 xfs_defer_add(dfops
, XFS_DEFER_OPS_TYPE_FREE
, &new->xefi_list
);
566 * Inode fork format manipulation functions
570 * Transform a btree format file with only one leaf node, where the
571 * extents list will fit in the inode, into an extents format file.
572 * Since the file extents are already in-core, all we have to do is
573 * give up the space for the btree root and pitch the leaf block.
575 STATIC
int /* error */
576 xfs_bmap_btree_to_extents(
577 xfs_trans_t
*tp
, /* transaction pointer */
578 xfs_inode_t
*ip
, /* incore inode pointer */
579 xfs_btree_cur_t
*cur
, /* btree cursor */
580 int *logflagsp
, /* inode logging flags */
581 int whichfork
) /* data or attr fork */
584 struct xfs_btree_block
*cblock
;/* child btree block */
585 xfs_fsblock_t cbno
; /* child block number */
586 xfs_buf_t
*cbp
; /* child block's buffer */
587 int error
; /* error return value */
588 xfs_ifork_t
*ifp
; /* inode fork data */
589 xfs_mount_t
*mp
; /* mount point structure */
590 __be64
*pp
; /* ptr to block address */
591 struct xfs_btree_block
*rblock
;/* root btree block */
592 struct xfs_owner_info oinfo
;
595 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
596 ASSERT(whichfork
!= XFS_COW_FORK
);
597 ASSERT(ifp
->if_flags
& XFS_IFEXTENTS
);
598 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
);
599 rblock
= ifp
->if_broot
;
600 ASSERT(be16_to_cpu(rblock
->bb_level
) == 1);
601 ASSERT(be16_to_cpu(rblock
->bb_numrecs
) == 1);
602 ASSERT(xfs_bmbt_maxrecs(mp
, ifp
->if_broot_bytes
, 0) == 1);
603 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, rblock
, 1, ifp
->if_broot_bytes
);
604 cbno
= be64_to_cpu(*pp
);
607 XFS_WANT_CORRUPTED_RETURN(cur
->bc_mp
,
608 xfs_btree_check_lptr(cur
, cbno
, 1));
610 error
= xfs_btree_read_bufl(mp
, tp
, cbno
, 0, &cbp
, XFS_BMAP_BTREE_REF
,
614 cblock
= XFS_BUF_TO_BLOCK(cbp
);
615 if ((error
= xfs_btree_check_block(cur
, cblock
, 0, cbp
)))
617 xfs_rmap_ino_bmbt_owner(&oinfo
, ip
->i_ino
, whichfork
);
618 xfs_bmap_add_free(mp
, cur
->bc_private
.b
.dfops
, cbno
, 1, &oinfo
);
619 ip
->i_d
.di_nblocks
--;
620 xfs_trans_mod_dquot_byino(tp
, ip
, XFS_TRANS_DQ_BCOUNT
, -1L);
621 xfs_trans_binval(tp
, cbp
);
622 if (cur
->bc_bufs
[0] == cbp
)
623 cur
->bc_bufs
[0] = NULL
;
624 xfs_iroot_realloc(ip
, -1, whichfork
);
625 ASSERT(ifp
->if_broot
== NULL
);
626 ASSERT((ifp
->if_flags
& XFS_IFBROOT
) == 0);
627 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_EXTENTS
);
628 *logflagsp
= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
633 * Convert an extents-format file into a btree-format file.
634 * The new file will have a root block (in the inode) and a single child block.
636 STATIC
int /* error */
637 xfs_bmap_extents_to_btree(
638 xfs_trans_t
*tp
, /* transaction pointer */
639 xfs_inode_t
*ip
, /* incore inode pointer */
640 xfs_fsblock_t
*firstblock
, /* first-block-allocated */
641 struct xfs_defer_ops
*dfops
, /* blocks freed in xaction */
642 xfs_btree_cur_t
**curp
, /* cursor returned to caller */
643 int wasdel
, /* converting a delayed alloc */
644 int *logflagsp
, /* inode logging flags */
645 int whichfork
) /* data or attr fork */
647 struct xfs_btree_block
*ablock
; /* allocated (child) bt block */
648 xfs_buf_t
*abp
; /* buffer for ablock */
649 xfs_alloc_arg_t args
; /* allocation arguments */
650 xfs_bmbt_rec_t
*arp
; /* child record pointer */
651 struct xfs_btree_block
*block
; /* btree root block */
652 xfs_btree_cur_t
*cur
; /* bmap btree cursor */
653 int error
; /* error return value */
654 xfs_ifork_t
*ifp
; /* inode fork pointer */
655 xfs_bmbt_key_t
*kp
; /* root block key pointer */
656 xfs_mount_t
*mp
; /* mount structure */
657 xfs_bmbt_ptr_t
*pp
; /* root block address pointer */
658 struct xfs_iext_cursor icur
;
659 struct xfs_bmbt_irec rec
;
660 xfs_extnum_t cnt
= 0;
663 ASSERT(whichfork
!= XFS_COW_FORK
);
664 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
665 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
);
668 * Make space in the inode incore.
670 xfs_iroot_realloc(ip
, 1, whichfork
);
671 ifp
->if_flags
|= XFS_IFBROOT
;
676 block
= ifp
->if_broot
;
677 xfs_btree_init_block_int(mp
, block
, XFS_BUF_DADDR_NULL
,
678 XFS_BTNUM_BMAP
, 1, 1, ip
->i_ino
,
679 XFS_BTREE_LONG_PTRS
);
681 * Need a cursor. Can't allocate until bb_level is filled in.
683 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
684 cur
->bc_private
.b
.firstblock
= *firstblock
;
685 cur
->bc_private
.b
.dfops
= dfops
;
686 cur
->bc_private
.b
.flags
= wasdel
? XFS_BTCUR_BPRV_WASDEL
: 0;
688 * Convert to a btree with two levels, one record in root.
690 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_BTREE
);
691 memset(&args
, 0, sizeof(args
));
694 xfs_rmap_ino_bmbt_owner(&args
.oinfo
, ip
->i_ino
, whichfork
);
695 args
.firstblock
= *firstblock
;
696 if (*firstblock
== NULLFSBLOCK
) {
697 args
.type
= XFS_ALLOCTYPE_START_BNO
;
698 args
.fsbno
= XFS_INO_TO_FSB(mp
, ip
->i_ino
);
699 } else if (dfops
->dop_low
) {
700 args
.type
= XFS_ALLOCTYPE_START_BNO
;
701 args
.fsbno
= *firstblock
;
703 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
704 args
.fsbno
= *firstblock
;
706 args
.minlen
= args
.maxlen
= args
.prod
= 1;
707 args
.wasdel
= wasdel
;
709 if ((error
= xfs_alloc_vextent(&args
))) {
710 xfs_iroot_realloc(ip
, -1, whichfork
);
711 ASSERT(ifp
->if_broot
== NULL
);
712 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_EXTENTS
);
713 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
717 if (WARN_ON_ONCE(args
.fsbno
== NULLFSBLOCK
)) {
718 xfs_iroot_realloc(ip
, -1, whichfork
);
719 ASSERT(ifp
->if_broot
== NULL
);
720 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_EXTENTS
);
721 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
725 * Allocation can't fail, the space was reserved.
727 ASSERT(*firstblock
== NULLFSBLOCK
||
728 args
.agno
>= XFS_FSB_TO_AGNO(mp
, *firstblock
));
729 *firstblock
= cur
->bc_private
.b
.firstblock
= args
.fsbno
;
730 cur
->bc_private
.b
.allocated
++;
731 ip
->i_d
.di_nblocks
++;
732 xfs_trans_mod_dquot_byino(tp
, ip
, XFS_TRANS_DQ_BCOUNT
, 1L);
733 abp
= xfs_btree_get_bufl(mp
, tp
, args
.fsbno
, 0);
735 * Fill in the child block.
737 abp
->b_ops
= &xfs_bmbt_buf_ops
;
738 ablock
= XFS_BUF_TO_BLOCK(abp
);
739 xfs_btree_init_block_int(mp
, ablock
, abp
->b_bn
,
740 XFS_BTNUM_BMAP
, 0, 0, ip
->i_ino
,
741 XFS_BTREE_LONG_PTRS
);
743 for_each_xfs_iext(ifp
, &icur
, &rec
) {
744 if (isnullstartblock(rec
.br_startblock
))
746 arp
= XFS_BMBT_REC_ADDR(mp
, ablock
, 1 + cnt
);
747 xfs_bmbt_disk_set_all(arp
, &rec
);
750 ASSERT(cnt
== XFS_IFORK_NEXTENTS(ip
, whichfork
));
751 xfs_btree_set_numrecs(ablock
, cnt
);
754 * Fill in the root key and pointer.
756 kp
= XFS_BMBT_KEY_ADDR(mp
, block
, 1);
757 arp
= XFS_BMBT_REC_ADDR(mp
, ablock
, 1);
758 kp
->br_startoff
= cpu_to_be64(xfs_bmbt_disk_get_startoff(arp
));
759 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, xfs_bmbt_get_maxrecs(cur
,
760 be16_to_cpu(block
->bb_level
)));
761 *pp
= cpu_to_be64(args
.fsbno
);
764 * Do all this logging at the end so that
765 * the root is at the right level.
767 xfs_btree_log_block(cur
, abp
, XFS_BB_ALL_BITS
);
768 xfs_btree_log_recs(cur
, abp
, 1, be16_to_cpu(ablock
->bb_numrecs
));
769 ASSERT(*curp
== NULL
);
771 *logflagsp
= XFS_ILOG_CORE
| xfs_ilog_fbroot(whichfork
);
776 * Convert a local file to an extents file.
777 * This code is out of bounds for data forks of regular files,
778 * since the file data needs to get logged so things will stay consistent.
779 * (The bmap-level manipulations are ok, though).
782 xfs_bmap_local_to_extents_empty(
783 struct xfs_inode
*ip
,
786 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
788 ASSERT(whichfork
!= XFS_COW_FORK
);
789 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
790 ASSERT(ifp
->if_bytes
== 0);
791 ASSERT(XFS_IFORK_NEXTENTS(ip
, whichfork
) == 0);
793 xfs_bmap_forkoff_reset(ip
, whichfork
);
794 ifp
->if_flags
&= ~XFS_IFINLINE
;
795 ifp
->if_flags
|= XFS_IFEXTENTS
;
796 ifp
->if_u1
.if_root
= NULL
;
798 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_EXTENTS
);
802 STATIC
int /* error */
803 xfs_bmap_local_to_extents(
804 xfs_trans_t
*tp
, /* transaction pointer */
805 xfs_inode_t
*ip
, /* incore inode pointer */
806 xfs_fsblock_t
*firstblock
, /* first block allocated in xaction */
807 xfs_extlen_t total
, /* total blocks needed by transaction */
808 int *logflagsp
, /* inode logging flags */
810 void (*init_fn
)(struct xfs_trans
*tp
,
812 struct xfs_inode
*ip
,
813 struct xfs_ifork
*ifp
))
816 int flags
; /* logging flags returned */
817 xfs_ifork_t
*ifp
; /* inode fork pointer */
818 xfs_alloc_arg_t args
; /* allocation arguments */
819 xfs_buf_t
*bp
; /* buffer for extent block */
820 struct xfs_bmbt_irec rec
;
821 struct xfs_iext_cursor icur
;
824 * We don't want to deal with the case of keeping inode data inline yet.
825 * So sending the data fork of a regular inode is invalid.
827 ASSERT(!(S_ISREG(VFS_I(ip
)->i_mode
) && whichfork
== XFS_DATA_FORK
));
828 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
829 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
831 if (!ifp
->if_bytes
) {
832 xfs_bmap_local_to_extents_empty(ip
, whichfork
);
833 flags
= XFS_ILOG_CORE
;
839 ASSERT((ifp
->if_flags
& (XFS_IFINLINE
|XFS_IFEXTENTS
)) == XFS_IFINLINE
);
840 memset(&args
, 0, sizeof(args
));
842 args
.mp
= ip
->i_mount
;
843 xfs_rmap_ino_owner(&args
.oinfo
, ip
->i_ino
, whichfork
, 0);
844 args
.firstblock
= *firstblock
;
846 * Allocate a block. We know we need only one, since the
847 * file currently fits in an inode.
849 if (*firstblock
== NULLFSBLOCK
) {
850 args
.fsbno
= XFS_INO_TO_FSB(args
.mp
, ip
->i_ino
);
851 args
.type
= XFS_ALLOCTYPE_START_BNO
;
853 args
.fsbno
= *firstblock
;
854 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
857 args
.minlen
= args
.maxlen
= args
.prod
= 1;
858 error
= xfs_alloc_vextent(&args
);
862 /* Can't fail, the space was reserved. */
863 ASSERT(args
.fsbno
!= NULLFSBLOCK
);
864 ASSERT(args
.len
== 1);
865 *firstblock
= args
.fsbno
;
866 bp
= xfs_btree_get_bufl(args
.mp
, tp
, args
.fsbno
, 0);
869 * Initialize the block, copy the data and log the remote buffer.
871 * The callout is responsible for logging because the remote format
872 * might differ from the local format and thus we don't know how much to
873 * log here. Note that init_fn must also set the buffer log item type
876 init_fn(tp
, bp
, ip
, ifp
);
878 /* account for the change in fork size */
879 xfs_idata_realloc(ip
, -ifp
->if_bytes
, whichfork
);
880 xfs_bmap_local_to_extents_empty(ip
, whichfork
);
881 flags
|= XFS_ILOG_CORE
;
883 ifp
->if_u1
.if_root
= NULL
;
887 rec
.br_startblock
= args
.fsbno
;
888 rec
.br_blockcount
= 1;
889 rec
.br_state
= XFS_EXT_NORM
;
890 xfs_iext_first(ifp
, &icur
);
891 xfs_iext_insert(ip
, &icur
, &rec
, 0);
893 XFS_IFORK_NEXT_SET(ip
, whichfork
, 1);
894 ip
->i_d
.di_nblocks
= 1;
895 xfs_trans_mod_dquot_byino(tp
, ip
,
896 XFS_TRANS_DQ_BCOUNT
, 1L);
897 flags
|= xfs_ilog_fext(whichfork
);
905 * Called from xfs_bmap_add_attrfork to handle btree format files.
907 STATIC
int /* error */
908 xfs_bmap_add_attrfork_btree(
909 xfs_trans_t
*tp
, /* transaction pointer */
910 xfs_inode_t
*ip
, /* incore inode pointer */
911 xfs_fsblock_t
*firstblock
, /* first block allocated */
912 struct xfs_defer_ops
*dfops
, /* blocks to free at commit */
913 int *flags
) /* inode logging flags */
915 xfs_btree_cur_t
*cur
; /* btree cursor */
916 int error
; /* error return value */
917 xfs_mount_t
*mp
; /* file system mount struct */
918 int stat
; /* newroot status */
921 if (ip
->i_df
.if_broot_bytes
<= XFS_IFORK_DSIZE(ip
))
922 *flags
|= XFS_ILOG_DBROOT
;
924 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, XFS_DATA_FORK
);
925 cur
->bc_private
.b
.dfops
= dfops
;
926 cur
->bc_private
.b
.firstblock
= *firstblock
;
927 error
= xfs_bmbt_lookup_first(cur
, &stat
);
930 /* must be at least one entry */
931 XFS_WANT_CORRUPTED_GOTO(mp
, stat
== 1, error0
);
932 if ((error
= xfs_btree_new_iroot(cur
, flags
, &stat
)))
935 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
938 *firstblock
= cur
->bc_private
.b
.firstblock
;
939 cur
->bc_private
.b
.allocated
= 0;
940 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
944 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
949 * Called from xfs_bmap_add_attrfork to handle extents format files.
951 STATIC
int /* error */
952 xfs_bmap_add_attrfork_extents(
953 xfs_trans_t
*tp
, /* transaction pointer */
954 xfs_inode_t
*ip
, /* incore inode pointer */
955 xfs_fsblock_t
*firstblock
, /* first block allocated */
956 struct xfs_defer_ops
*dfops
, /* blocks to free at commit */
957 int *flags
) /* inode logging flags */
959 xfs_btree_cur_t
*cur
; /* bmap btree cursor */
960 int error
; /* error return value */
962 if (ip
->i_d
.di_nextents
* sizeof(xfs_bmbt_rec_t
) <= XFS_IFORK_DSIZE(ip
))
965 error
= xfs_bmap_extents_to_btree(tp
, ip
, firstblock
, dfops
, &cur
, 0,
966 flags
, XFS_DATA_FORK
);
968 cur
->bc_private
.b
.allocated
= 0;
969 xfs_btree_del_cursor(cur
,
970 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
976 * Called from xfs_bmap_add_attrfork to handle local format files. Each
977 * different data fork content type needs a different callout to do the
978 * conversion. Some are basic and only require special block initialisation
979 * callouts for the data formating, others (directories) are so specialised they
980 * handle everything themselves.
982 * XXX (dgc): investigate whether directory conversion can use the generic
983 * formatting callout. It should be possible - it's just a very complex
986 STATIC
int /* error */
987 xfs_bmap_add_attrfork_local(
988 xfs_trans_t
*tp
, /* transaction pointer */
989 xfs_inode_t
*ip
, /* incore inode pointer */
990 xfs_fsblock_t
*firstblock
, /* first block allocated */
991 struct xfs_defer_ops
*dfops
, /* blocks to free at commit */
992 int *flags
) /* inode logging flags */
994 xfs_da_args_t dargs
; /* args for dir/attr code */
996 if (ip
->i_df
.if_bytes
<= XFS_IFORK_DSIZE(ip
))
999 if (S_ISDIR(VFS_I(ip
)->i_mode
)) {
1000 memset(&dargs
, 0, sizeof(dargs
));
1001 dargs
.geo
= ip
->i_mount
->m_dir_geo
;
1003 dargs
.firstblock
= firstblock
;
1004 dargs
.dfops
= dfops
;
1005 dargs
.total
= dargs
.geo
->fsbcount
;
1006 dargs
.whichfork
= XFS_DATA_FORK
;
1008 return xfs_dir2_sf_to_block(&dargs
);
1011 if (S_ISLNK(VFS_I(ip
)->i_mode
))
1012 return xfs_bmap_local_to_extents(tp
, ip
, firstblock
, 1,
1013 flags
, XFS_DATA_FORK
,
1014 xfs_symlink_local_to_remote
);
1016 /* should only be called for types that support local format data */
1018 return -EFSCORRUPTED
;
1022 * Convert inode from non-attributed to attributed.
1023 * Must not be in a transaction, ip must not be locked.
1025 int /* error code */
1026 xfs_bmap_add_attrfork(
1027 xfs_inode_t
*ip
, /* incore inode pointer */
1028 int size
, /* space new attribute needs */
1029 int rsvd
) /* xact may use reserved blks */
1031 xfs_fsblock_t firstblock
; /* 1st block/ag allocated */
1032 struct xfs_defer_ops dfops
; /* freed extent records */
1033 xfs_mount_t
*mp
; /* mount structure */
1034 xfs_trans_t
*tp
; /* transaction pointer */
1035 int blks
; /* space reservation */
1036 int version
= 1; /* superblock attr version */
1037 int logflags
; /* logging flags */
1038 int error
; /* error return value */
1040 ASSERT(XFS_IFORK_Q(ip
) == 0);
1043 ASSERT(!XFS_NOT_DQATTACHED(mp
, ip
));
1045 blks
= XFS_ADDAFORK_SPACE_RES(mp
);
1047 error
= xfs_trans_alloc(mp
, &M_RES(mp
)->tr_addafork
, blks
, 0,
1048 rsvd
? XFS_TRANS_RESERVE
: 0, &tp
);
1051 xfs_defer_init(&dfops
, &firstblock
);
1052 tp
->t_dfops
= &dfops
;
1054 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
1055 error
= xfs_trans_reserve_quota_nblks(tp
, ip
, blks
, 0, rsvd
?
1056 XFS_QMOPT_RES_REGBLKS
| XFS_QMOPT_FORCE_RES
:
1057 XFS_QMOPT_RES_REGBLKS
);
1060 if (XFS_IFORK_Q(ip
))
1062 if (ip
->i_d
.di_anextents
!= 0) {
1063 error
= -EFSCORRUPTED
;
1066 if (ip
->i_d
.di_aformat
!= XFS_DINODE_FMT_EXTENTS
) {
1068 * For inodes coming from pre-6.2 filesystems.
1070 ASSERT(ip
->i_d
.di_aformat
== 0);
1071 ip
->i_d
.di_aformat
= XFS_DINODE_FMT_EXTENTS
;
1074 xfs_trans_ijoin(tp
, ip
, 0);
1075 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
1077 switch (ip
->i_d
.di_format
) {
1078 case XFS_DINODE_FMT_DEV
:
1079 ip
->i_d
.di_forkoff
= roundup(sizeof(xfs_dev_t
), 8) >> 3;
1081 case XFS_DINODE_FMT_LOCAL
:
1082 case XFS_DINODE_FMT_EXTENTS
:
1083 case XFS_DINODE_FMT_BTREE
:
1084 ip
->i_d
.di_forkoff
= xfs_attr_shortform_bytesfit(ip
, size
);
1085 if (!ip
->i_d
.di_forkoff
)
1086 ip
->i_d
.di_forkoff
= xfs_default_attroffset(ip
) >> 3;
1087 else if (mp
->m_flags
& XFS_MOUNT_ATTR2
)
1096 ASSERT(ip
->i_afp
== NULL
);
1097 ip
->i_afp
= kmem_zone_zalloc(xfs_ifork_zone
, KM_SLEEP
);
1098 ip
->i_afp
->if_flags
= XFS_IFEXTENTS
;
1100 switch (ip
->i_d
.di_format
) {
1101 case XFS_DINODE_FMT_LOCAL
:
1102 error
= xfs_bmap_add_attrfork_local(tp
, ip
, &firstblock
, &dfops
,
1105 case XFS_DINODE_FMT_EXTENTS
:
1106 error
= xfs_bmap_add_attrfork_extents(tp
, ip
, &firstblock
,
1109 case XFS_DINODE_FMT_BTREE
:
1110 error
= xfs_bmap_add_attrfork_btree(tp
, ip
, &firstblock
, &dfops
,
1118 xfs_trans_log_inode(tp
, ip
, logflags
);
1121 if (!xfs_sb_version_hasattr(&mp
->m_sb
) ||
1122 (!xfs_sb_version_hasattr2(&mp
->m_sb
) && version
== 2)) {
1123 bool log_sb
= false;
1125 spin_lock(&mp
->m_sb_lock
);
1126 if (!xfs_sb_version_hasattr(&mp
->m_sb
)) {
1127 xfs_sb_version_addattr(&mp
->m_sb
);
1130 if (!xfs_sb_version_hasattr2(&mp
->m_sb
) && version
== 2) {
1131 xfs_sb_version_addattr2(&mp
->m_sb
);
1134 spin_unlock(&mp
->m_sb_lock
);
1139 error
= xfs_defer_finish(&tp
, &dfops
);
1142 error
= xfs_trans_commit(tp
);
1143 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
1147 xfs_defer_cancel(&dfops
);
1149 xfs_trans_cancel(tp
);
1150 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
1155 * Internal and external extent tree search functions.
1159 * Read in extents from a btree-format inode.
1163 struct xfs_trans
*tp
,
1164 struct xfs_inode
*ip
,
1167 struct xfs_mount
*mp
= ip
->i_mount
;
1168 int state
= xfs_bmap_fork_to_state(whichfork
);
1169 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1170 xfs_extnum_t nextents
= XFS_IFORK_NEXTENTS(ip
, whichfork
);
1171 struct xfs_btree_block
*block
= ifp
->if_broot
;
1172 struct xfs_iext_cursor icur
;
1173 struct xfs_bmbt_irec
new;
1181 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
1183 if (unlikely(XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)) {
1184 XFS_ERROR_REPORT(__func__
, XFS_ERRLEVEL_LOW
, mp
);
1185 return -EFSCORRUPTED
;
1189 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
1191 level
= be16_to_cpu(block
->bb_level
);
1193 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, 1, ifp
->if_broot_bytes
);
1194 bno
= be64_to_cpu(*pp
);
1197 * Go down the tree until leaf level is reached, following the first
1198 * pointer (leftmost) at each level.
1200 while (level
-- > 0) {
1201 error
= xfs_btree_read_bufl(mp
, tp
, bno
, 0, &bp
,
1202 XFS_BMAP_BTREE_REF
, &xfs_bmbt_buf_ops
);
1205 block
= XFS_BUF_TO_BLOCK(bp
);
1208 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, mp
->m_bmap_dmxr
[1]);
1209 bno
= be64_to_cpu(*pp
);
1210 XFS_WANT_CORRUPTED_GOTO(mp
,
1211 xfs_verify_fsbno(mp
, bno
), out_brelse
);
1212 xfs_trans_brelse(tp
, bp
);
1216 * Here with bp and block set to the leftmost leaf node in the tree.
1219 xfs_iext_first(ifp
, &icur
);
1222 * Loop over all leaf nodes. Copy information to the extent records.
1225 xfs_bmbt_rec_t
*frp
;
1226 xfs_fsblock_t nextbno
;
1227 xfs_extnum_t num_recs
;
1229 num_recs
= xfs_btree_get_numrecs(block
);
1230 if (unlikely(i
+ num_recs
> nextents
)) {
1231 xfs_warn(ip
->i_mount
,
1232 "corrupt dinode %Lu, (btree extents).",
1233 (unsigned long long) ip
->i_ino
);
1234 xfs_inode_verifier_error(ip
, -EFSCORRUPTED
,
1235 __func__
, block
, sizeof(*block
),
1237 error
= -EFSCORRUPTED
;
1241 * Read-ahead the next leaf block, if any.
1243 nextbno
= be64_to_cpu(block
->bb_u
.l
.bb_rightsib
);
1244 if (nextbno
!= NULLFSBLOCK
)
1245 xfs_btree_reada_bufl(mp
, nextbno
, 1,
1248 * Copy records into the extent records.
1250 frp
= XFS_BMBT_REC_ADDR(mp
, block
, 1);
1251 for (j
= 0; j
< num_recs
; j
++, frp
++, i
++) {
1254 xfs_bmbt_disk_get_all(frp
, &new);
1255 fa
= xfs_bmap_validate_extent(ip
, whichfork
, &new);
1257 error
= -EFSCORRUPTED
;
1258 xfs_inode_verifier_error(ip
, error
,
1259 "xfs_iread_extents(2)",
1260 frp
, sizeof(*frp
), fa
);
1263 xfs_iext_insert(ip
, &icur
, &new, state
);
1264 trace_xfs_read_extent(ip
, &icur
, state
, _THIS_IP_
);
1265 xfs_iext_next(ifp
, &icur
);
1267 xfs_trans_brelse(tp
, bp
);
1270 * If we've reached the end, stop.
1272 if (bno
== NULLFSBLOCK
)
1274 error
= xfs_btree_read_bufl(mp
, tp
, bno
, 0, &bp
,
1275 XFS_BMAP_BTREE_REF
, &xfs_bmbt_buf_ops
);
1278 block
= XFS_BUF_TO_BLOCK(bp
);
1281 if (i
!= XFS_IFORK_NEXTENTS(ip
, whichfork
)) {
1282 error
= -EFSCORRUPTED
;
1285 ASSERT(i
== xfs_iext_count(ifp
));
1287 ifp
->if_flags
|= XFS_IFEXTENTS
;
1291 xfs_trans_brelse(tp
, bp
);
1293 xfs_iext_destroy(ifp
);
1298 * Returns the relative block number of the first unused block(s) in the given
1299 * fork with at least "len" logically contiguous blocks free. This is the
1300 * lowest-address hole if the fork has holes, else the first block past the end
1301 * of fork. Return 0 if the fork is currently local (in-inode).
1304 xfs_bmap_first_unused(
1305 struct xfs_trans
*tp
, /* transaction pointer */
1306 struct xfs_inode
*ip
, /* incore inode */
1307 xfs_extlen_t len
, /* size of hole to find */
1308 xfs_fileoff_t
*first_unused
, /* unused block */
1309 int whichfork
) /* data or attr fork */
1311 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1312 struct xfs_bmbt_irec got
;
1313 struct xfs_iext_cursor icur
;
1314 xfs_fileoff_t lastaddr
= 0;
1315 xfs_fileoff_t lowest
, max
;
1318 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
||
1319 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
||
1320 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
1322 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
) {
1327 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
1328 error
= xfs_iread_extents(tp
, ip
, whichfork
);
1333 lowest
= max
= *first_unused
;
1334 for_each_xfs_iext(ifp
, &icur
, &got
) {
1336 * See if the hole before this extent will work.
1338 if (got
.br_startoff
>= lowest
+ len
&&
1339 got
.br_startoff
- max
>= len
)
1341 lastaddr
= got
.br_startoff
+ got
.br_blockcount
;
1342 max
= XFS_FILEOFF_MAX(lastaddr
, lowest
);
1345 *first_unused
= max
;
1350 * Returns the file-relative block number of the last block - 1 before
1351 * last_block (input value) in the file.
1352 * This is not based on i_size, it is based on the extent records.
1353 * Returns 0 for local files, as they do not have extent records.
1356 xfs_bmap_last_before(
1357 struct xfs_trans
*tp
, /* transaction pointer */
1358 struct xfs_inode
*ip
, /* incore inode */
1359 xfs_fileoff_t
*last_block
, /* last block */
1360 int whichfork
) /* data or attr fork */
1362 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1363 struct xfs_bmbt_irec got
;
1364 struct xfs_iext_cursor icur
;
1367 switch (XFS_IFORK_FORMAT(ip
, whichfork
)) {
1368 case XFS_DINODE_FMT_LOCAL
:
1371 case XFS_DINODE_FMT_BTREE
:
1372 case XFS_DINODE_FMT_EXTENTS
:
1378 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
1379 error
= xfs_iread_extents(tp
, ip
, whichfork
);
1384 if (!xfs_iext_lookup_extent_before(ip
, ifp
, last_block
, &icur
, &got
))
1390 xfs_bmap_last_extent(
1391 struct xfs_trans
*tp
,
1392 struct xfs_inode
*ip
,
1394 struct xfs_bmbt_irec
*rec
,
1397 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1398 struct xfs_iext_cursor icur
;
1401 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
1402 error
= xfs_iread_extents(tp
, ip
, whichfork
);
1407 xfs_iext_last(ifp
, &icur
);
1408 if (!xfs_iext_get_extent(ifp
, &icur
, rec
))
1416 * Check the last inode extent to determine whether this allocation will result
1417 * in blocks being allocated at the end of the file. When we allocate new data
1418 * blocks at the end of the file which do not start at the previous data block,
1419 * we will try to align the new blocks at stripe unit boundaries.
1421 * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be
1422 * at, or past the EOF.
1426 struct xfs_bmalloca
*bma
,
1429 struct xfs_bmbt_irec rec
;
1434 error
= xfs_bmap_last_extent(NULL
, bma
->ip
, whichfork
, &rec
,
1445 * Check if we are allocation or past the last extent, or at least into
1446 * the last delayed allocated extent.
1448 bma
->aeof
= bma
->offset
>= rec
.br_startoff
+ rec
.br_blockcount
||
1449 (bma
->offset
>= rec
.br_startoff
&&
1450 isnullstartblock(rec
.br_startblock
));
1455 * Returns the file-relative block number of the first block past eof in
1456 * the file. This is not based on i_size, it is based on the extent records.
1457 * Returns 0 for local files, as they do not have extent records.
1460 xfs_bmap_last_offset(
1461 struct xfs_inode
*ip
,
1462 xfs_fileoff_t
*last_block
,
1465 struct xfs_bmbt_irec rec
;
1471 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
)
1474 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
&&
1475 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
1478 error
= xfs_bmap_last_extent(NULL
, ip
, whichfork
, &rec
, &is_empty
);
1479 if (error
|| is_empty
)
1482 *last_block
= rec
.br_startoff
+ rec
.br_blockcount
;
1487 * Returns whether the selected fork of the inode has exactly one
1488 * block or not. For the data fork we check this matches di_size,
1489 * implying the file's range is 0..bsize-1.
1491 int /* 1=>1 block, 0=>otherwise */
1493 xfs_inode_t
*ip
, /* incore inode */
1494 int whichfork
) /* data or attr fork */
1496 xfs_ifork_t
*ifp
; /* inode fork pointer */
1497 int rval
; /* return value */
1498 xfs_bmbt_irec_t s
; /* internal version of extent */
1499 struct xfs_iext_cursor icur
;
1502 if (whichfork
== XFS_DATA_FORK
)
1503 return XFS_ISIZE(ip
) == ip
->i_mount
->m_sb
.sb_blocksize
;
1505 if (XFS_IFORK_NEXTENTS(ip
, whichfork
) != 1)
1507 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
1509 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1510 ASSERT(ifp
->if_flags
& XFS_IFEXTENTS
);
1511 xfs_iext_first(ifp
, &icur
);
1512 xfs_iext_get_extent(ifp
, &icur
, &s
);
1513 rval
= s
.br_startoff
== 0 && s
.br_blockcount
== 1;
1514 if (rval
&& whichfork
== XFS_DATA_FORK
)
1515 ASSERT(XFS_ISIZE(ip
) == ip
->i_mount
->m_sb
.sb_blocksize
);
1520 * Extent tree manipulation functions used during allocation.
1524 * Convert a delayed allocation to a real allocation.
1526 STATIC
int /* error */
1527 xfs_bmap_add_extent_delay_real(
1528 struct xfs_bmalloca
*bma
,
1531 struct xfs_bmbt_irec
*new = &bma
->got
;
1532 int error
; /* error return value */
1533 int i
; /* temp state */
1534 xfs_ifork_t
*ifp
; /* inode fork pointer */
1535 xfs_fileoff_t new_endoff
; /* end offset of new entry */
1536 xfs_bmbt_irec_t r
[3]; /* neighbor extent entries */
1537 /* left is 0, right is 1, prev is 2 */
1538 int rval
=0; /* return value (logging flags) */
1539 int state
= xfs_bmap_fork_to_state(whichfork
);
1540 xfs_filblks_t da_new
; /* new count del alloc blocks used */
1541 xfs_filblks_t da_old
; /* old count del alloc blocks used */
1542 xfs_filblks_t temp
=0; /* value for da_new calculations */
1543 int tmp_rval
; /* partial logging flags */
1544 struct xfs_mount
*mp
;
1545 xfs_extnum_t
*nextents
;
1546 struct xfs_bmbt_irec old
;
1548 mp
= bma
->ip
->i_mount
;
1549 ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
1550 ASSERT(whichfork
!= XFS_ATTR_FORK
);
1551 nextents
= (whichfork
== XFS_COW_FORK
? &bma
->ip
->i_cnextents
:
1552 &bma
->ip
->i_d
.di_nextents
);
1554 ASSERT(!isnullstartblock(new->br_startblock
));
1556 (bma
->cur
->bc_private
.b
.flags
& XFS_BTCUR_BPRV_WASDEL
));
1558 XFS_STATS_INC(mp
, xs_add_exlist
);
1565 * Set up a bunch of variables to make the tests simpler.
1567 xfs_iext_get_extent(ifp
, &bma
->icur
, &PREV
);
1568 new_endoff
= new->br_startoff
+ new->br_blockcount
;
1569 ASSERT(isnullstartblock(PREV
.br_startblock
));
1570 ASSERT(PREV
.br_startoff
<= new->br_startoff
);
1571 ASSERT(PREV
.br_startoff
+ PREV
.br_blockcount
>= new_endoff
);
1573 da_old
= startblockval(PREV
.br_startblock
);
1577 * Set flags determining what part of the previous delayed allocation
1578 * extent is being replaced by a real allocation.
1580 if (PREV
.br_startoff
== new->br_startoff
)
1581 state
|= BMAP_LEFT_FILLING
;
1582 if (PREV
.br_startoff
+ PREV
.br_blockcount
== new_endoff
)
1583 state
|= BMAP_RIGHT_FILLING
;
1586 * Check and set flags if this segment has a left neighbor.
1587 * Don't set contiguous if the combined extent would be too large.
1589 if (xfs_iext_peek_prev_extent(ifp
, &bma
->icur
, &LEFT
)) {
1590 state
|= BMAP_LEFT_VALID
;
1591 if (isnullstartblock(LEFT
.br_startblock
))
1592 state
|= BMAP_LEFT_DELAY
;
1595 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
1596 LEFT
.br_startoff
+ LEFT
.br_blockcount
== new->br_startoff
&&
1597 LEFT
.br_startblock
+ LEFT
.br_blockcount
== new->br_startblock
&&
1598 LEFT
.br_state
== new->br_state
&&
1599 LEFT
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
1600 state
|= BMAP_LEFT_CONTIG
;
1603 * Check and set flags if this segment has a right neighbor.
1604 * Don't set contiguous if the combined extent would be too large.
1605 * Also check for all-three-contiguous being too large.
1607 if (xfs_iext_peek_next_extent(ifp
, &bma
->icur
, &RIGHT
)) {
1608 state
|= BMAP_RIGHT_VALID
;
1609 if (isnullstartblock(RIGHT
.br_startblock
))
1610 state
|= BMAP_RIGHT_DELAY
;
1613 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
1614 new_endoff
== RIGHT
.br_startoff
&&
1615 new->br_startblock
+ new->br_blockcount
== RIGHT
.br_startblock
&&
1616 new->br_state
== RIGHT
.br_state
&&
1617 new->br_blockcount
+ RIGHT
.br_blockcount
<= MAXEXTLEN
&&
1618 ((state
& (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
1619 BMAP_RIGHT_FILLING
)) !=
1620 (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
1621 BMAP_RIGHT_FILLING
) ||
1622 LEFT
.br_blockcount
+ new->br_blockcount
+ RIGHT
.br_blockcount
1624 state
|= BMAP_RIGHT_CONTIG
;
1628 * Switch out based on the FILLING and CONTIG state bits.
1630 switch (state
& (BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
1631 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
)) {
1632 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
1633 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
1635 * Filling in all of a previously delayed allocation extent.
1636 * The left and right neighbors are both contiguous with new.
1638 LEFT
.br_blockcount
+= PREV
.br_blockcount
+ RIGHT
.br_blockcount
;
1640 xfs_iext_remove(bma
->ip
, &bma
->icur
, state
);
1641 xfs_iext_remove(bma
->ip
, &bma
->icur
, state
);
1642 xfs_iext_prev(ifp
, &bma
->icur
);
1643 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &LEFT
);
1646 if (bma
->cur
== NULL
)
1647 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1649 rval
= XFS_ILOG_CORE
;
1650 error
= xfs_bmbt_lookup_eq(bma
->cur
, &RIGHT
, &i
);
1653 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1654 error
= xfs_btree_delete(bma
->cur
, &i
);
1657 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1658 error
= xfs_btree_decrement(bma
->cur
, 0, &i
);
1661 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1662 error
= xfs_bmbt_update(bma
->cur
, &LEFT
);
1668 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
1670 * Filling in all of a previously delayed allocation extent.
1671 * The left neighbor is contiguous, the right is not.
1674 LEFT
.br_blockcount
+= PREV
.br_blockcount
;
1676 xfs_iext_remove(bma
->ip
, &bma
->icur
, state
);
1677 xfs_iext_prev(ifp
, &bma
->icur
);
1678 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &LEFT
);
1680 if (bma
->cur
== NULL
)
1681 rval
= XFS_ILOG_DEXT
;
1684 error
= xfs_bmbt_lookup_eq(bma
->cur
, &old
, &i
);
1687 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1688 error
= xfs_bmbt_update(bma
->cur
, &LEFT
);
1694 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
1696 * Filling in all of a previously delayed allocation extent.
1697 * The right neighbor is contiguous, the left is not.
1699 PREV
.br_startblock
= new->br_startblock
;
1700 PREV
.br_blockcount
+= RIGHT
.br_blockcount
;
1702 xfs_iext_next(ifp
, &bma
->icur
);
1703 xfs_iext_remove(bma
->ip
, &bma
->icur
, state
);
1704 xfs_iext_prev(ifp
, &bma
->icur
);
1705 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &PREV
);
1707 if (bma
->cur
== NULL
)
1708 rval
= XFS_ILOG_DEXT
;
1711 error
= xfs_bmbt_lookup_eq(bma
->cur
, &RIGHT
, &i
);
1714 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1715 error
= xfs_bmbt_update(bma
->cur
, &PREV
);
1721 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
1723 * Filling in all of a previously delayed allocation extent.
1724 * Neither the left nor right neighbors are contiguous with
1727 PREV
.br_startblock
= new->br_startblock
;
1728 PREV
.br_state
= new->br_state
;
1729 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &PREV
);
1732 if (bma
->cur
== NULL
)
1733 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1735 rval
= XFS_ILOG_CORE
;
1736 error
= xfs_bmbt_lookup_eq(bma
->cur
, new, &i
);
1739 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
1740 error
= xfs_btree_insert(bma
->cur
, &i
);
1743 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1747 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
:
1749 * Filling in the first part of a previous delayed allocation.
1750 * The left neighbor is contiguous.
1753 temp
= PREV
.br_blockcount
- new->br_blockcount
;
1754 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
1755 startblockval(PREV
.br_startblock
));
1757 LEFT
.br_blockcount
+= new->br_blockcount
;
1759 PREV
.br_blockcount
= temp
;
1760 PREV
.br_startoff
+= new->br_blockcount
;
1761 PREV
.br_startblock
= nullstartblock(da_new
);
1763 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &PREV
);
1764 xfs_iext_prev(ifp
, &bma
->icur
);
1765 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &LEFT
);
1767 if (bma
->cur
== NULL
)
1768 rval
= XFS_ILOG_DEXT
;
1771 error
= xfs_bmbt_lookup_eq(bma
->cur
, &old
, &i
);
1774 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1775 error
= xfs_bmbt_update(bma
->cur
, &LEFT
);
1781 case BMAP_LEFT_FILLING
:
1783 * Filling in the first part of a previous delayed allocation.
1784 * The left neighbor is not contiguous.
1786 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, new);
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
, new, &i
);
1795 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
1796 error
= xfs_btree_insert(bma
->cur
, &i
);
1799 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1802 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
1803 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
1804 bma
->firstblock
, bma
->dfops
,
1805 &bma
->cur
, 1, &tmp_rval
, whichfork
);
1811 temp
= PREV
.br_blockcount
- new->br_blockcount
;
1812 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
1813 startblockval(PREV
.br_startblock
) -
1814 (bma
->cur
? bma
->cur
->bc_private
.b
.allocated
: 0));
1816 PREV
.br_startoff
= new_endoff
;
1817 PREV
.br_blockcount
= temp
;
1818 PREV
.br_startblock
= nullstartblock(da_new
);
1819 xfs_iext_next(ifp
, &bma
->icur
);
1820 xfs_iext_insert(bma
->ip
, &bma
->icur
, &PREV
, state
);
1821 xfs_iext_prev(ifp
, &bma
->icur
);
1824 case BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
1826 * Filling in the last part of a previous delayed allocation.
1827 * The right neighbor is contiguous with the new allocation.
1830 RIGHT
.br_startoff
= new->br_startoff
;
1831 RIGHT
.br_startblock
= new->br_startblock
;
1832 RIGHT
.br_blockcount
+= new->br_blockcount
;
1834 if (bma
->cur
== NULL
)
1835 rval
= XFS_ILOG_DEXT
;
1838 error
= xfs_bmbt_lookup_eq(bma
->cur
, &old
, &i
);
1841 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1842 error
= xfs_bmbt_update(bma
->cur
, &RIGHT
);
1847 temp
= PREV
.br_blockcount
- new->br_blockcount
;
1848 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
1849 startblockval(PREV
.br_startblock
));
1851 PREV
.br_blockcount
= temp
;
1852 PREV
.br_startblock
= nullstartblock(da_new
);
1854 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &PREV
);
1855 xfs_iext_next(ifp
, &bma
->icur
);
1856 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &RIGHT
);
1859 case BMAP_RIGHT_FILLING
:
1861 * Filling in the last part of a previous delayed allocation.
1862 * The right neighbor is not contiguous.
1864 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, new);
1866 if (bma
->cur
== NULL
)
1867 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1869 rval
= XFS_ILOG_CORE
;
1870 error
= xfs_bmbt_lookup_eq(bma
->cur
, new, &i
);
1873 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
1874 error
= xfs_btree_insert(bma
->cur
, &i
);
1877 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1880 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
1881 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
1882 bma
->firstblock
, bma
->dfops
, &bma
->cur
, 1,
1883 &tmp_rval
, whichfork
);
1889 temp
= PREV
.br_blockcount
- new->br_blockcount
;
1890 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
1891 startblockval(PREV
.br_startblock
) -
1892 (bma
->cur
? bma
->cur
->bc_private
.b
.allocated
: 0));
1894 PREV
.br_startblock
= nullstartblock(da_new
);
1895 PREV
.br_blockcount
= temp
;
1896 xfs_iext_insert(bma
->ip
, &bma
->icur
, &PREV
, state
);
1897 xfs_iext_next(ifp
, &bma
->icur
);
1902 * Filling in the middle part of a previous delayed allocation.
1903 * Contiguity is impossible here.
1904 * This case is avoided almost all the time.
1906 * We start with a delayed allocation:
1908 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
1911 * and we are allocating:
1912 * +rrrrrrrrrrrrrrrrr+
1915 * and we set it up for insertion as:
1916 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
1918 * PREV @ idx LEFT RIGHT
1919 * inserted at idx + 1
1923 /* LEFT is the new middle */
1926 /* RIGHT is the new right */
1927 RIGHT
.br_state
= PREV
.br_state
;
1928 RIGHT
.br_startoff
= new_endoff
;
1929 RIGHT
.br_blockcount
=
1930 PREV
.br_startoff
+ PREV
.br_blockcount
- new_endoff
;
1931 RIGHT
.br_startblock
=
1932 nullstartblock(xfs_bmap_worst_indlen(bma
->ip
,
1933 RIGHT
.br_blockcount
));
1936 PREV
.br_blockcount
= new->br_startoff
- PREV
.br_startoff
;
1937 PREV
.br_startblock
=
1938 nullstartblock(xfs_bmap_worst_indlen(bma
->ip
,
1939 PREV
.br_blockcount
));
1940 xfs_iext_update_extent(bma
->ip
, state
, &bma
->icur
, &PREV
);
1942 xfs_iext_next(ifp
, &bma
->icur
);
1943 xfs_iext_insert(bma
->ip
, &bma
->icur
, &RIGHT
, state
);
1944 xfs_iext_insert(bma
->ip
, &bma
->icur
, &LEFT
, state
);
1947 if (bma
->cur
== NULL
)
1948 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1950 rval
= XFS_ILOG_CORE
;
1951 error
= xfs_bmbt_lookup_eq(bma
->cur
, new, &i
);
1954 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
1955 error
= xfs_btree_insert(bma
->cur
, &i
);
1958 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1961 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
1962 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
1963 bma
->firstblock
, bma
->dfops
, &bma
->cur
,
1964 1, &tmp_rval
, whichfork
);
1970 da_new
= startblockval(PREV
.br_startblock
) +
1971 startblockval(RIGHT
.br_startblock
);
1974 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
1975 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
1976 case BMAP_LEFT_FILLING
| BMAP_RIGHT_CONTIG
:
1977 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
1978 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
1979 case BMAP_LEFT_CONTIG
:
1980 case BMAP_RIGHT_CONTIG
:
1982 * These cases are all impossible.
1987 /* add reverse mapping unless caller opted out */
1988 if (!(bma
->flags
& XFS_BMAPI_NORMAP
)) {
1989 error
= xfs_rmap_map_extent(mp
, bma
->dfops
, bma
->ip
,
1995 /* convert to a btree if necessary */
1996 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
1997 int tmp_logflags
; /* partial log flag return val */
1999 ASSERT(bma
->cur
== NULL
);
2000 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
2001 bma
->firstblock
, bma
->dfops
, &bma
->cur
,
2002 da_old
> 0, &tmp_logflags
, whichfork
);
2003 bma
->logflags
|= tmp_logflags
;
2009 da_new
+= bma
->cur
->bc_private
.b
.allocated
;
2010 bma
->cur
->bc_private
.b
.allocated
= 0;
2013 /* adjust for changes in reserved delayed indirect blocks */
2014 if (da_new
!= da_old
) {
2015 ASSERT(state
== 0 || da_new
< da_old
);
2016 error
= xfs_mod_fdblocks(mp
, (int64_t)(da_old
- da_new
),
2020 xfs_bmap_check_leaf_extents(bma
->cur
, bma
->ip
, whichfork
);
2022 if (whichfork
!= XFS_COW_FORK
)
2023 bma
->logflags
|= rval
;
2031 * Convert an unwritten allocation to a real allocation or vice versa.
2033 STATIC
int /* error */
2034 xfs_bmap_add_extent_unwritten_real(
2035 struct xfs_trans
*tp
,
2036 xfs_inode_t
*ip
, /* incore inode pointer */
2038 struct xfs_iext_cursor
*icur
,
2039 xfs_btree_cur_t
**curp
, /* if *curp is null, not a btree */
2040 xfs_bmbt_irec_t
*new, /* new data to add to file extents */
2041 xfs_fsblock_t
*first
, /* pointer to firstblock variable */
2042 struct xfs_defer_ops
*dfops
, /* list of extents to be freed */
2043 int *logflagsp
) /* inode logging flags */
2045 xfs_btree_cur_t
*cur
; /* btree cursor */
2046 int error
; /* error return value */
2047 int i
; /* temp state */
2048 xfs_ifork_t
*ifp
; /* inode fork pointer */
2049 xfs_fileoff_t new_endoff
; /* end offset of new entry */
2050 xfs_bmbt_irec_t r
[3]; /* neighbor extent entries */
2051 /* left is 0, right is 1, prev is 2 */
2052 int rval
=0; /* return value (logging flags) */
2053 int state
= xfs_bmap_fork_to_state(whichfork
);
2054 struct xfs_mount
*mp
= ip
->i_mount
;
2055 struct xfs_bmbt_irec old
;
2060 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
2062 ASSERT(!isnullstartblock(new->br_startblock
));
2064 XFS_STATS_INC(mp
, xs_add_exlist
);
2071 * Set up a bunch of variables to make the tests simpler.
2074 xfs_iext_get_extent(ifp
, icur
, &PREV
);
2075 ASSERT(new->br_state
!= PREV
.br_state
);
2076 new_endoff
= new->br_startoff
+ new->br_blockcount
;
2077 ASSERT(PREV
.br_startoff
<= new->br_startoff
);
2078 ASSERT(PREV
.br_startoff
+ PREV
.br_blockcount
>= new_endoff
);
2081 * Set flags determining what part of the previous oldext allocation
2082 * extent is being replaced by a newext allocation.
2084 if (PREV
.br_startoff
== new->br_startoff
)
2085 state
|= BMAP_LEFT_FILLING
;
2086 if (PREV
.br_startoff
+ PREV
.br_blockcount
== new_endoff
)
2087 state
|= BMAP_RIGHT_FILLING
;
2090 * Check and set flags if this segment has a left neighbor.
2091 * Don't set contiguous if the combined extent would be too large.
2093 if (xfs_iext_peek_prev_extent(ifp
, icur
, &LEFT
)) {
2094 state
|= BMAP_LEFT_VALID
;
2095 if (isnullstartblock(LEFT
.br_startblock
))
2096 state
|= BMAP_LEFT_DELAY
;
2099 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
2100 LEFT
.br_startoff
+ LEFT
.br_blockcount
== new->br_startoff
&&
2101 LEFT
.br_startblock
+ LEFT
.br_blockcount
== new->br_startblock
&&
2102 LEFT
.br_state
== new->br_state
&&
2103 LEFT
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
2104 state
|= BMAP_LEFT_CONTIG
;
2107 * Check and set flags if this segment has a right neighbor.
2108 * Don't set contiguous if the combined extent would be too large.
2109 * Also check for all-three-contiguous being too large.
2111 if (xfs_iext_peek_next_extent(ifp
, icur
, &RIGHT
)) {
2112 state
|= BMAP_RIGHT_VALID
;
2113 if (isnullstartblock(RIGHT
.br_startblock
))
2114 state
|= BMAP_RIGHT_DELAY
;
2117 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
2118 new_endoff
== RIGHT
.br_startoff
&&
2119 new->br_startblock
+ new->br_blockcount
== RIGHT
.br_startblock
&&
2120 new->br_state
== RIGHT
.br_state
&&
2121 new->br_blockcount
+ RIGHT
.br_blockcount
<= MAXEXTLEN
&&
2122 ((state
& (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
2123 BMAP_RIGHT_FILLING
)) !=
2124 (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
2125 BMAP_RIGHT_FILLING
) ||
2126 LEFT
.br_blockcount
+ new->br_blockcount
+ RIGHT
.br_blockcount
2128 state
|= BMAP_RIGHT_CONTIG
;
2131 * Switch out based on the FILLING and CONTIG state bits.
2133 switch (state
& (BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
2134 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
)) {
2135 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
2136 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2138 * Setting all of a previous oldext extent to newext.
2139 * The left and right neighbors are both contiguous with new.
2141 LEFT
.br_blockcount
+= PREV
.br_blockcount
+ RIGHT
.br_blockcount
;
2143 xfs_iext_remove(ip
, icur
, state
);
2144 xfs_iext_remove(ip
, icur
, state
);
2145 xfs_iext_prev(ifp
, icur
);
2146 xfs_iext_update_extent(ip
, state
, icur
, &LEFT
);
2147 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2148 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 2);
2150 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2152 rval
= XFS_ILOG_CORE
;
2153 error
= xfs_bmbt_lookup_eq(cur
, &RIGHT
, &i
);
2156 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2157 if ((error
= xfs_btree_delete(cur
, &i
)))
2159 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2160 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2162 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2163 if ((error
= xfs_btree_delete(cur
, &i
)))
2165 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2166 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2168 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2169 error
= xfs_bmbt_update(cur
, &LEFT
);
2175 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
2177 * Setting all of a previous oldext extent to newext.
2178 * The left neighbor is contiguous, the right is not.
2180 LEFT
.br_blockcount
+= PREV
.br_blockcount
;
2182 xfs_iext_remove(ip
, icur
, state
);
2183 xfs_iext_prev(ifp
, icur
);
2184 xfs_iext_update_extent(ip
, state
, icur
, &LEFT
);
2185 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2186 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
2188 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2190 rval
= XFS_ILOG_CORE
;
2191 error
= xfs_bmbt_lookup_eq(cur
, &PREV
, &i
);
2194 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2195 if ((error
= xfs_btree_delete(cur
, &i
)))
2197 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2198 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2200 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2201 error
= xfs_bmbt_update(cur
, &LEFT
);
2207 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2209 * Setting all of a previous oldext extent to newext.
2210 * The right neighbor is contiguous, the left is not.
2212 PREV
.br_blockcount
+= RIGHT
.br_blockcount
;
2213 PREV
.br_state
= new->br_state
;
2215 xfs_iext_next(ifp
, icur
);
2216 xfs_iext_remove(ip
, icur
, state
);
2217 xfs_iext_prev(ifp
, icur
);
2218 xfs_iext_update_extent(ip
, state
, icur
, &PREV
);
2220 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2221 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
2223 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2225 rval
= XFS_ILOG_CORE
;
2226 error
= xfs_bmbt_lookup_eq(cur
, &RIGHT
, &i
);
2229 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2230 if ((error
= xfs_btree_delete(cur
, &i
)))
2232 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2233 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2235 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2236 error
= xfs_bmbt_update(cur
, &PREV
);
2242 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
2244 * Setting all of a previous oldext extent to newext.
2245 * Neither the left nor right neighbors are contiguous with
2248 PREV
.br_state
= new->br_state
;
2249 xfs_iext_update_extent(ip
, state
, icur
, &PREV
);
2252 rval
= XFS_ILOG_DEXT
;
2255 error
= xfs_bmbt_lookup_eq(cur
, new, &i
);
2258 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2259 error
= xfs_bmbt_update(cur
, &PREV
);
2265 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
:
2267 * Setting the first part of a previous oldext extent to newext.
2268 * The left neighbor is contiguous.
2270 LEFT
.br_blockcount
+= new->br_blockcount
;
2273 PREV
.br_startoff
+= new->br_blockcount
;
2274 PREV
.br_startblock
+= new->br_blockcount
;
2275 PREV
.br_blockcount
-= new->br_blockcount
;
2277 xfs_iext_update_extent(ip
, state
, icur
, &PREV
);
2278 xfs_iext_prev(ifp
, icur
);
2279 xfs_iext_update_extent(ip
, state
, icur
, &LEFT
);
2282 rval
= XFS_ILOG_DEXT
;
2285 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2288 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2289 error
= xfs_bmbt_update(cur
, &PREV
);
2292 error
= xfs_btree_decrement(cur
, 0, &i
);
2295 error
= xfs_bmbt_update(cur
, &LEFT
);
2301 case BMAP_LEFT_FILLING
:
2303 * Setting the first part of a previous oldext extent to newext.
2304 * The left neighbor is not contiguous.
2307 PREV
.br_startoff
+= new->br_blockcount
;
2308 PREV
.br_startblock
+= new->br_blockcount
;
2309 PREV
.br_blockcount
-= new->br_blockcount
;
2311 xfs_iext_update_extent(ip
, state
, icur
, &PREV
);
2312 xfs_iext_insert(ip
, icur
, new, state
);
2313 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2314 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
2316 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2318 rval
= XFS_ILOG_CORE
;
2319 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2322 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2323 error
= xfs_bmbt_update(cur
, &PREV
);
2326 cur
->bc_rec
.b
= *new;
2327 if ((error
= xfs_btree_insert(cur
, &i
)))
2329 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2333 case BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2335 * Setting the last part of a previous oldext extent to newext.
2336 * The right neighbor is contiguous with the new allocation.
2339 PREV
.br_blockcount
-= new->br_blockcount
;
2341 RIGHT
.br_startoff
= new->br_startoff
;
2342 RIGHT
.br_startblock
= new->br_startblock
;
2343 RIGHT
.br_blockcount
+= new->br_blockcount
;
2345 xfs_iext_update_extent(ip
, state
, icur
, &PREV
);
2346 xfs_iext_next(ifp
, icur
);
2347 xfs_iext_update_extent(ip
, state
, icur
, &RIGHT
);
2350 rval
= XFS_ILOG_DEXT
;
2353 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2356 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2357 error
= xfs_bmbt_update(cur
, &PREV
);
2360 error
= xfs_btree_increment(cur
, 0, &i
);
2363 error
= xfs_bmbt_update(cur
, &RIGHT
);
2369 case BMAP_RIGHT_FILLING
:
2371 * Setting the last part of a previous oldext extent to newext.
2372 * The right neighbor is not contiguous.
2375 PREV
.br_blockcount
-= new->br_blockcount
;
2377 xfs_iext_update_extent(ip
, state
, icur
, &PREV
);
2378 xfs_iext_next(ifp
, icur
);
2379 xfs_iext_insert(ip
, icur
, new, state
);
2381 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2382 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
2384 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2386 rval
= XFS_ILOG_CORE
;
2387 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2390 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2391 error
= xfs_bmbt_update(cur
, &PREV
);
2394 error
= xfs_bmbt_lookup_eq(cur
, new, &i
);
2397 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
2398 if ((error
= xfs_btree_insert(cur
, &i
)))
2400 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2406 * Setting the middle part of a previous oldext extent to
2407 * newext. Contiguity is impossible here.
2408 * One extent becomes three extents.
2411 PREV
.br_blockcount
= new->br_startoff
- PREV
.br_startoff
;
2414 r
[1].br_startoff
= new_endoff
;
2415 r
[1].br_blockcount
=
2416 old
.br_startoff
+ old
.br_blockcount
- new_endoff
;
2417 r
[1].br_startblock
= new->br_startblock
+ new->br_blockcount
;
2418 r
[1].br_state
= PREV
.br_state
;
2420 xfs_iext_update_extent(ip
, state
, icur
, &PREV
);
2421 xfs_iext_next(ifp
, icur
);
2422 xfs_iext_insert(ip
, icur
, &r
[1], state
);
2423 xfs_iext_insert(ip
, icur
, &r
[0], state
);
2425 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2426 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 2);
2428 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2430 rval
= XFS_ILOG_CORE
;
2431 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2434 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2435 /* new right extent - oldext */
2436 error
= xfs_bmbt_update(cur
, &r
[1]);
2439 /* new left extent - oldext */
2440 cur
->bc_rec
.b
= PREV
;
2441 if ((error
= xfs_btree_insert(cur
, &i
)))
2443 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2445 * Reset the cursor to the position of the new extent
2446 * we are about to insert as we can't trust it after
2447 * the previous insert.
2449 error
= xfs_bmbt_lookup_eq(cur
, new, &i
);
2452 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
2453 /* new middle extent - newext */
2454 if ((error
= xfs_btree_insert(cur
, &i
)))
2456 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2460 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2461 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2462 case BMAP_LEFT_FILLING
| BMAP_RIGHT_CONTIG
:
2463 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
2464 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2465 case BMAP_LEFT_CONTIG
:
2466 case BMAP_RIGHT_CONTIG
:
2468 * These cases are all impossible.
2473 /* update reverse mappings */
2474 error
= xfs_rmap_convert_extent(mp
, dfops
, ip
, whichfork
, new);
2478 /* convert to a btree if necessary */
2479 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
2480 int tmp_logflags
; /* partial log flag return val */
2482 ASSERT(cur
== NULL
);
2483 error
= xfs_bmap_extents_to_btree(tp
, ip
, first
, dfops
, &cur
,
2484 0, &tmp_logflags
, whichfork
);
2485 *logflagsp
|= tmp_logflags
;
2490 /* clear out the allocated field, done with it now in any case. */
2492 cur
->bc_private
.b
.allocated
= 0;
2496 xfs_bmap_check_leaf_extents(*curp
, ip
, whichfork
);
2506 * Convert a hole to a delayed allocation.
2509 xfs_bmap_add_extent_hole_delay(
2510 xfs_inode_t
*ip
, /* incore inode pointer */
2512 struct xfs_iext_cursor
*icur
,
2513 xfs_bmbt_irec_t
*new) /* new data to add to file extents */
2515 xfs_ifork_t
*ifp
; /* inode fork pointer */
2516 xfs_bmbt_irec_t left
; /* left neighbor extent entry */
2517 xfs_filblks_t newlen
=0; /* new indirect size */
2518 xfs_filblks_t oldlen
=0; /* old indirect size */
2519 xfs_bmbt_irec_t right
; /* right neighbor extent entry */
2520 int state
= xfs_bmap_fork_to_state(whichfork
);
2521 xfs_filblks_t temp
; /* temp for indirect calculations */
2523 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
2524 ASSERT(isnullstartblock(new->br_startblock
));
2527 * Check and set flags if this segment has a left neighbor
2529 if (xfs_iext_peek_prev_extent(ifp
, icur
, &left
)) {
2530 state
|= BMAP_LEFT_VALID
;
2531 if (isnullstartblock(left
.br_startblock
))
2532 state
|= BMAP_LEFT_DELAY
;
2536 * Check and set flags if the current (right) segment exists.
2537 * If it doesn't exist, we're converting the hole at end-of-file.
2539 if (xfs_iext_get_extent(ifp
, icur
, &right
)) {
2540 state
|= BMAP_RIGHT_VALID
;
2541 if (isnullstartblock(right
.br_startblock
))
2542 state
|= BMAP_RIGHT_DELAY
;
2546 * Set contiguity flags on the left and right neighbors.
2547 * Don't let extents get too large, even if the pieces are contiguous.
2549 if ((state
& BMAP_LEFT_VALID
) && (state
& BMAP_LEFT_DELAY
) &&
2550 left
.br_startoff
+ left
.br_blockcount
== new->br_startoff
&&
2551 left
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
2552 state
|= BMAP_LEFT_CONTIG
;
2554 if ((state
& BMAP_RIGHT_VALID
) && (state
& BMAP_RIGHT_DELAY
) &&
2555 new->br_startoff
+ new->br_blockcount
== right
.br_startoff
&&
2556 new->br_blockcount
+ right
.br_blockcount
<= MAXEXTLEN
&&
2557 (!(state
& BMAP_LEFT_CONTIG
) ||
2558 (left
.br_blockcount
+ new->br_blockcount
+
2559 right
.br_blockcount
<= MAXEXTLEN
)))
2560 state
|= BMAP_RIGHT_CONTIG
;
2563 * Switch out based on the contiguity flags.
2565 switch (state
& (BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
)) {
2566 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2568 * New allocation is contiguous with delayed allocations
2569 * on the left and on the right.
2570 * Merge all three into a single extent record.
2572 temp
= left
.br_blockcount
+ new->br_blockcount
+
2573 right
.br_blockcount
;
2575 oldlen
= startblockval(left
.br_startblock
) +
2576 startblockval(new->br_startblock
) +
2577 startblockval(right
.br_startblock
);
2578 newlen
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
2580 left
.br_startblock
= nullstartblock(newlen
);
2581 left
.br_blockcount
= temp
;
2583 xfs_iext_remove(ip
, icur
, state
);
2584 xfs_iext_prev(ifp
, icur
);
2585 xfs_iext_update_extent(ip
, state
, icur
, &left
);
2588 case BMAP_LEFT_CONTIG
:
2590 * New allocation is contiguous with a delayed allocation
2592 * Merge the new allocation with the left neighbor.
2594 temp
= left
.br_blockcount
+ new->br_blockcount
;
2596 oldlen
= startblockval(left
.br_startblock
) +
2597 startblockval(new->br_startblock
);
2598 newlen
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
2600 left
.br_blockcount
= temp
;
2601 left
.br_startblock
= nullstartblock(newlen
);
2603 xfs_iext_prev(ifp
, icur
);
2604 xfs_iext_update_extent(ip
, state
, icur
, &left
);
2607 case BMAP_RIGHT_CONTIG
:
2609 * New allocation is contiguous with a delayed allocation
2611 * Merge the new allocation with the right neighbor.
2613 temp
= new->br_blockcount
+ right
.br_blockcount
;
2614 oldlen
= startblockval(new->br_startblock
) +
2615 startblockval(right
.br_startblock
);
2616 newlen
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
2618 right
.br_startoff
= new->br_startoff
;
2619 right
.br_startblock
= nullstartblock(newlen
);
2620 right
.br_blockcount
= temp
;
2621 xfs_iext_update_extent(ip
, state
, icur
, &right
);
2626 * New allocation is not contiguous with another
2627 * delayed allocation.
2628 * Insert a new entry.
2630 oldlen
= newlen
= 0;
2631 xfs_iext_insert(ip
, icur
, new, state
);
2634 if (oldlen
!= newlen
) {
2635 ASSERT(oldlen
> newlen
);
2636 xfs_mod_fdblocks(ip
->i_mount
, (int64_t)(oldlen
- newlen
),
2639 * Nothing to do for disk quota accounting here.
2645 * Convert a hole to a real allocation.
2647 STATIC
int /* error */
2648 xfs_bmap_add_extent_hole_real(
2649 struct xfs_trans
*tp
,
2650 struct xfs_inode
*ip
,
2652 struct xfs_iext_cursor
*icur
,
2653 struct xfs_btree_cur
**curp
,
2654 struct xfs_bmbt_irec
*new,
2655 xfs_fsblock_t
*first
,
2656 struct xfs_defer_ops
*dfops
,
2660 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
2661 struct xfs_mount
*mp
= ip
->i_mount
;
2662 struct xfs_btree_cur
*cur
= *curp
;
2663 int error
; /* error return value */
2664 int i
; /* temp state */
2665 xfs_bmbt_irec_t left
; /* left neighbor extent entry */
2666 xfs_bmbt_irec_t right
; /* right neighbor extent entry */
2667 int rval
=0; /* return value (logging flags) */
2668 int state
= xfs_bmap_fork_to_state(whichfork
);
2669 struct xfs_bmbt_irec old
;
2671 ASSERT(!isnullstartblock(new->br_startblock
));
2672 ASSERT(!cur
|| !(cur
->bc_private
.b
.flags
& XFS_BTCUR_BPRV_WASDEL
));
2674 XFS_STATS_INC(mp
, xs_add_exlist
);
2677 * Check and set flags if this segment has a left neighbor.
2679 if (xfs_iext_peek_prev_extent(ifp
, icur
, &left
)) {
2680 state
|= BMAP_LEFT_VALID
;
2681 if (isnullstartblock(left
.br_startblock
))
2682 state
|= BMAP_LEFT_DELAY
;
2686 * Check and set flags if this segment has a current value.
2687 * Not true if we're inserting into the "hole" at eof.
2689 if (xfs_iext_get_extent(ifp
, icur
, &right
)) {
2690 state
|= BMAP_RIGHT_VALID
;
2691 if (isnullstartblock(right
.br_startblock
))
2692 state
|= BMAP_RIGHT_DELAY
;
2696 * We're inserting a real allocation between "left" and "right".
2697 * Set the contiguity flags. Don't let extents get too large.
2699 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
2700 left
.br_startoff
+ left
.br_blockcount
== new->br_startoff
&&
2701 left
.br_startblock
+ left
.br_blockcount
== new->br_startblock
&&
2702 left
.br_state
== new->br_state
&&
2703 left
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
2704 state
|= BMAP_LEFT_CONTIG
;
2706 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
2707 new->br_startoff
+ new->br_blockcount
== right
.br_startoff
&&
2708 new->br_startblock
+ new->br_blockcount
== right
.br_startblock
&&
2709 new->br_state
== right
.br_state
&&
2710 new->br_blockcount
+ right
.br_blockcount
<= MAXEXTLEN
&&
2711 (!(state
& BMAP_LEFT_CONTIG
) ||
2712 left
.br_blockcount
+ new->br_blockcount
+
2713 right
.br_blockcount
<= MAXEXTLEN
))
2714 state
|= BMAP_RIGHT_CONTIG
;
2718 * Select which case we're in here, and implement it.
2720 switch (state
& (BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
)) {
2721 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2723 * New allocation is contiguous with real allocations on the
2724 * left and on the right.
2725 * Merge all three into a single extent record.
2727 left
.br_blockcount
+= new->br_blockcount
+ right
.br_blockcount
;
2729 xfs_iext_remove(ip
, icur
, state
);
2730 xfs_iext_prev(ifp
, icur
);
2731 xfs_iext_update_extent(ip
, state
, icur
, &left
);
2733 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2734 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
2736 rval
= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
2738 rval
= XFS_ILOG_CORE
;
2739 error
= xfs_bmbt_lookup_eq(cur
, &right
, &i
);
2742 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2743 error
= xfs_btree_delete(cur
, &i
);
2746 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2747 error
= xfs_btree_decrement(cur
, 0, &i
);
2750 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2751 error
= xfs_bmbt_update(cur
, &left
);
2757 case BMAP_LEFT_CONTIG
:
2759 * New allocation is contiguous with a real allocation
2761 * Merge the new allocation with the left neighbor.
2764 left
.br_blockcount
+= new->br_blockcount
;
2766 xfs_iext_prev(ifp
, icur
);
2767 xfs_iext_update_extent(ip
, state
, icur
, &left
);
2770 rval
= xfs_ilog_fext(whichfork
);
2773 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2776 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2777 error
= xfs_bmbt_update(cur
, &left
);
2783 case BMAP_RIGHT_CONTIG
:
2785 * New allocation is contiguous with a real allocation
2787 * Merge the new allocation with the right neighbor.
2791 right
.br_startoff
= new->br_startoff
;
2792 right
.br_startblock
= new->br_startblock
;
2793 right
.br_blockcount
+= new->br_blockcount
;
2794 xfs_iext_update_extent(ip
, state
, icur
, &right
);
2797 rval
= xfs_ilog_fext(whichfork
);
2800 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2803 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2804 error
= xfs_bmbt_update(cur
, &right
);
2812 * New allocation is not contiguous with another
2814 * Insert a new entry.
2816 xfs_iext_insert(ip
, icur
, new, state
);
2817 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2818 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
2820 rval
= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
2822 rval
= XFS_ILOG_CORE
;
2823 error
= xfs_bmbt_lookup_eq(cur
, new, &i
);
2826 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
2827 error
= xfs_btree_insert(cur
, &i
);
2830 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2835 /* add reverse mapping unless caller opted out */
2836 if (!(flags
& XFS_BMAPI_NORMAP
)) {
2837 error
= xfs_rmap_map_extent(mp
, dfops
, ip
, whichfork
, new);
2842 /* convert to a btree if necessary */
2843 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
2844 int tmp_logflags
; /* partial log flag return val */
2846 ASSERT(cur
== NULL
);
2847 error
= xfs_bmap_extents_to_btree(tp
, ip
, first
, dfops
, curp
,
2848 0, &tmp_logflags
, whichfork
);
2849 *logflagsp
|= tmp_logflags
;
2855 /* clear out the allocated field, done with it now in any case. */
2857 cur
->bc_private
.b
.allocated
= 0;
2859 xfs_bmap_check_leaf_extents(cur
, ip
, whichfork
);
2866 * Functions used in the extent read, allocate and remove paths
2870 * Adjust the size of the new extent based on di_extsize and rt extsize.
2873 xfs_bmap_extsize_align(
2875 xfs_bmbt_irec_t
*gotp
, /* next extent pointer */
2876 xfs_bmbt_irec_t
*prevp
, /* previous extent pointer */
2877 xfs_extlen_t extsz
, /* align to this extent size */
2878 int rt
, /* is this a realtime inode? */
2879 int eof
, /* is extent at end-of-file? */
2880 int delay
, /* creating delalloc extent? */
2881 int convert
, /* overwriting unwritten extent? */
2882 xfs_fileoff_t
*offp
, /* in/out: aligned offset */
2883 xfs_extlen_t
*lenp
) /* in/out: aligned length */
2885 xfs_fileoff_t orig_off
; /* original offset */
2886 xfs_extlen_t orig_alen
; /* original length */
2887 xfs_fileoff_t orig_end
; /* original off+len */
2888 xfs_fileoff_t nexto
; /* next file offset */
2889 xfs_fileoff_t prevo
; /* previous file offset */
2890 xfs_fileoff_t align_off
; /* temp for offset */
2891 xfs_extlen_t align_alen
; /* temp for length */
2892 xfs_extlen_t temp
; /* temp for calculations */
2897 orig_off
= align_off
= *offp
;
2898 orig_alen
= align_alen
= *lenp
;
2899 orig_end
= orig_off
+ orig_alen
;
2902 * If this request overlaps an existing extent, then don't
2903 * attempt to perform any additional alignment.
2905 if (!delay
&& !eof
&&
2906 (orig_off
>= gotp
->br_startoff
) &&
2907 (orig_end
<= gotp
->br_startoff
+ gotp
->br_blockcount
)) {
2912 * If the file offset is unaligned vs. the extent size
2913 * we need to align it. This will be possible unless
2914 * the file was previously written with a kernel that didn't
2915 * perform this alignment, or if a truncate shot us in the
2918 div_u64_rem(orig_off
, extsz
, &temp
);
2924 /* Same adjustment for the end of the requested area. */
2925 temp
= (align_alen
% extsz
);
2927 align_alen
+= extsz
- temp
;
2930 * For large extent hint sizes, the aligned extent might be larger than
2931 * MAXEXTLEN. In that case, reduce the size by an extsz so that it pulls
2932 * the length back under MAXEXTLEN. The outer allocation loops handle
2933 * short allocation just fine, so it is safe to do this. We only want to
2934 * do it when we are forced to, though, because it means more allocation
2935 * operations are required.
2937 while (align_alen
> MAXEXTLEN
)
2938 align_alen
-= extsz
;
2939 ASSERT(align_alen
<= MAXEXTLEN
);
2942 * If the previous block overlaps with this proposed allocation
2943 * then move the start forward without adjusting the length.
2945 if (prevp
->br_startoff
!= NULLFILEOFF
) {
2946 if (prevp
->br_startblock
== HOLESTARTBLOCK
)
2947 prevo
= prevp
->br_startoff
;
2949 prevo
= prevp
->br_startoff
+ prevp
->br_blockcount
;
2952 if (align_off
!= orig_off
&& align_off
< prevo
)
2955 * If the next block overlaps with this proposed allocation
2956 * then move the start back without adjusting the length,
2957 * but not before offset 0.
2958 * This may of course make the start overlap previous block,
2959 * and if we hit the offset 0 limit then the next block
2960 * can still overlap too.
2962 if (!eof
&& gotp
->br_startoff
!= NULLFILEOFF
) {
2963 if ((delay
&& gotp
->br_startblock
== HOLESTARTBLOCK
) ||
2964 (!delay
&& gotp
->br_startblock
== DELAYSTARTBLOCK
))
2965 nexto
= gotp
->br_startoff
+ gotp
->br_blockcount
;
2967 nexto
= gotp
->br_startoff
;
2969 nexto
= NULLFILEOFF
;
2971 align_off
+ align_alen
!= orig_end
&&
2972 align_off
+ align_alen
> nexto
)
2973 align_off
= nexto
> align_alen
? nexto
- align_alen
: 0;
2975 * If we're now overlapping the next or previous extent that
2976 * means we can't fit an extsz piece in this hole. Just move
2977 * the start forward to the first valid spot and set
2978 * the length so we hit the end.
2980 if (align_off
!= orig_off
&& align_off
< prevo
)
2982 if (align_off
+ align_alen
!= orig_end
&&
2983 align_off
+ align_alen
> nexto
&&
2984 nexto
!= NULLFILEOFF
) {
2985 ASSERT(nexto
> prevo
);
2986 align_alen
= nexto
- align_off
;
2990 * If realtime, and the result isn't a multiple of the realtime
2991 * extent size we need to remove blocks until it is.
2993 if (rt
&& (temp
= (align_alen
% mp
->m_sb
.sb_rextsize
))) {
2995 * We're not covering the original request, or
2996 * we won't be able to once we fix the length.
2998 if (orig_off
< align_off
||
2999 orig_end
> align_off
+ align_alen
||
3000 align_alen
- temp
< orig_alen
)
3003 * Try to fix it by moving the start up.
3005 if (align_off
+ temp
<= orig_off
) {
3010 * Try to fix it by moving the end in.
3012 else if (align_off
+ align_alen
- temp
>= orig_end
)
3015 * Set the start to the minimum then trim the length.
3018 align_alen
-= orig_off
- align_off
;
3019 align_off
= orig_off
;
3020 align_alen
-= align_alen
% mp
->m_sb
.sb_rextsize
;
3023 * Result doesn't cover the request, fail it.
3025 if (orig_off
< align_off
|| orig_end
> align_off
+ align_alen
)
3028 ASSERT(orig_off
>= align_off
);
3029 /* see MAXEXTLEN handling above */
3030 ASSERT(orig_end
<= align_off
+ align_alen
||
3031 align_alen
+ extsz
> MAXEXTLEN
);
3035 if (!eof
&& gotp
->br_startoff
!= NULLFILEOFF
)
3036 ASSERT(align_off
+ align_alen
<= gotp
->br_startoff
);
3037 if (prevp
->br_startoff
!= NULLFILEOFF
)
3038 ASSERT(align_off
>= prevp
->br_startoff
+ prevp
->br_blockcount
);
3046 #define XFS_ALLOC_GAP_UNITS 4
3050 struct xfs_bmalloca
*ap
) /* bmap alloc argument struct */
3052 xfs_fsblock_t adjust
; /* adjustment to block numbers */
3053 xfs_agnumber_t fb_agno
; /* ag number of ap->firstblock */
3054 xfs_mount_t
*mp
; /* mount point structure */
3055 int nullfb
; /* true if ap->firstblock isn't set */
3056 int rt
; /* true if inode is realtime */
3058 #define ISVALID(x,y) \
3060 (x) < mp->m_sb.sb_rblocks : \
3061 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
3062 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
3063 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
3065 mp
= ap
->ip
->i_mount
;
3066 nullfb
= *ap
->firstblock
== NULLFSBLOCK
;
3067 rt
= XFS_IS_REALTIME_INODE(ap
->ip
) &&
3068 xfs_alloc_is_userdata(ap
->datatype
);
3069 fb_agno
= nullfb
? NULLAGNUMBER
: XFS_FSB_TO_AGNO(mp
, *ap
->firstblock
);
3071 * If allocating at eof, and there's a previous real block,
3072 * try to use its last block as our starting point.
3074 if (ap
->eof
&& ap
->prev
.br_startoff
!= NULLFILEOFF
&&
3075 !isnullstartblock(ap
->prev
.br_startblock
) &&
3076 ISVALID(ap
->prev
.br_startblock
+ ap
->prev
.br_blockcount
,
3077 ap
->prev
.br_startblock
)) {
3078 ap
->blkno
= ap
->prev
.br_startblock
+ ap
->prev
.br_blockcount
;
3080 * Adjust for the gap between prevp and us.
3082 adjust
= ap
->offset
-
3083 (ap
->prev
.br_startoff
+ ap
->prev
.br_blockcount
);
3085 ISVALID(ap
->blkno
+ adjust
, ap
->prev
.br_startblock
))
3086 ap
->blkno
+= adjust
;
3089 * If not at eof, then compare the two neighbor blocks.
3090 * Figure out whether either one gives us a good starting point,
3091 * and pick the better one.
3093 else if (!ap
->eof
) {
3094 xfs_fsblock_t gotbno
; /* right side block number */
3095 xfs_fsblock_t gotdiff
=0; /* right side difference */
3096 xfs_fsblock_t prevbno
; /* left side block number */
3097 xfs_fsblock_t prevdiff
=0; /* left side difference */
3100 * If there's a previous (left) block, select a requested
3101 * start block based on it.
3103 if (ap
->prev
.br_startoff
!= NULLFILEOFF
&&
3104 !isnullstartblock(ap
->prev
.br_startblock
) &&
3105 (prevbno
= ap
->prev
.br_startblock
+
3106 ap
->prev
.br_blockcount
) &&
3107 ISVALID(prevbno
, ap
->prev
.br_startblock
)) {
3109 * Calculate gap to end of previous block.
3111 adjust
= prevdiff
= ap
->offset
-
3112 (ap
->prev
.br_startoff
+
3113 ap
->prev
.br_blockcount
);
3115 * Figure the startblock based on the previous block's
3116 * end and the gap size.
3118 * If the gap is large relative to the piece we're
3119 * allocating, or using it gives us an invalid block
3120 * number, then just use the end of the previous block.
3122 if (prevdiff
<= XFS_ALLOC_GAP_UNITS
* ap
->length
&&
3123 ISVALID(prevbno
+ prevdiff
,
3124 ap
->prev
.br_startblock
))
3129 * If the firstblock forbids it, can't use it,
3132 if (!rt
&& !nullfb
&&
3133 XFS_FSB_TO_AGNO(mp
, prevbno
) != fb_agno
)
3134 prevbno
= NULLFSBLOCK
;
3137 * No previous block or can't follow it, just default.
3140 prevbno
= NULLFSBLOCK
;
3142 * If there's a following (right) block, select a requested
3143 * start block based on it.
3145 if (!isnullstartblock(ap
->got
.br_startblock
)) {
3147 * Calculate gap to start of next block.
3149 adjust
= gotdiff
= ap
->got
.br_startoff
- ap
->offset
;
3151 * Figure the startblock based on the next block's
3152 * start and the gap size.
3154 gotbno
= ap
->got
.br_startblock
;
3157 * If the gap is large relative to the piece we're
3158 * allocating, or using it gives us an invalid block
3159 * number, then just use the start of the next block
3160 * offset by our length.
3162 if (gotdiff
<= XFS_ALLOC_GAP_UNITS
* ap
->length
&&
3163 ISVALID(gotbno
- gotdiff
, gotbno
))
3165 else if (ISVALID(gotbno
- ap
->length
, gotbno
)) {
3166 gotbno
-= ap
->length
;
3167 gotdiff
+= adjust
- ap
->length
;
3171 * If the firstblock forbids it, can't use it,
3174 if (!rt
&& !nullfb
&&
3175 XFS_FSB_TO_AGNO(mp
, gotbno
) != fb_agno
)
3176 gotbno
= NULLFSBLOCK
;
3179 * No next block, just default.
3182 gotbno
= NULLFSBLOCK
;
3184 * If both valid, pick the better one, else the only good
3185 * one, else ap->blkno is already set (to 0 or the inode block).
3187 if (prevbno
!= NULLFSBLOCK
&& gotbno
!= NULLFSBLOCK
)
3188 ap
->blkno
= prevdiff
<= gotdiff
? prevbno
: gotbno
;
3189 else if (prevbno
!= NULLFSBLOCK
)
3190 ap
->blkno
= prevbno
;
3191 else if (gotbno
!= NULLFSBLOCK
)
3198 xfs_bmap_longest_free_extent(
3199 struct xfs_trans
*tp
,
3204 struct xfs_mount
*mp
= tp
->t_mountp
;
3205 struct xfs_perag
*pag
;
3206 xfs_extlen_t longest
;
3209 pag
= xfs_perag_get(mp
, ag
);
3210 if (!pag
->pagf_init
) {
3211 error
= xfs_alloc_pagf_init(mp
, tp
, ag
, XFS_ALLOC_FLAG_TRYLOCK
);
3215 if (!pag
->pagf_init
) {
3221 longest
= xfs_alloc_longest_free_extent(pag
,
3222 xfs_alloc_min_freelist(mp
, pag
),
3223 xfs_ag_resv_needed(pag
, XFS_AG_RESV_NONE
));
3224 if (*blen
< longest
)
3233 xfs_bmap_select_minlen(
3234 struct xfs_bmalloca
*ap
,
3235 struct xfs_alloc_arg
*args
,
3239 if (notinit
|| *blen
< ap
->minlen
) {
3241 * Since we did a BUF_TRYLOCK above, it is possible that
3242 * there is space for this request.
3244 args
->minlen
= ap
->minlen
;
3245 } else if (*blen
< args
->maxlen
) {
3247 * If the best seen length is less than the request length,
3248 * use the best as the minimum.
3250 args
->minlen
= *blen
;
3253 * Otherwise we've seen an extent as big as maxlen, use that
3256 args
->minlen
= args
->maxlen
;
3261 xfs_bmap_btalloc_nullfb(
3262 struct xfs_bmalloca
*ap
,
3263 struct xfs_alloc_arg
*args
,
3266 struct xfs_mount
*mp
= ap
->ip
->i_mount
;
3267 xfs_agnumber_t ag
, startag
;
3271 args
->type
= XFS_ALLOCTYPE_START_BNO
;
3272 args
->total
= ap
->total
;
3274 startag
= ag
= XFS_FSB_TO_AGNO(mp
, args
->fsbno
);
3275 if (startag
== NULLAGNUMBER
)
3278 while (*blen
< args
->maxlen
) {
3279 error
= xfs_bmap_longest_free_extent(args
->tp
, ag
, blen
,
3284 if (++ag
== mp
->m_sb
.sb_agcount
)
3290 xfs_bmap_select_minlen(ap
, args
, blen
, notinit
);
3295 xfs_bmap_btalloc_filestreams(
3296 struct xfs_bmalloca
*ap
,
3297 struct xfs_alloc_arg
*args
,
3300 struct xfs_mount
*mp
= ap
->ip
->i_mount
;
3305 args
->type
= XFS_ALLOCTYPE_NEAR_BNO
;
3306 args
->total
= ap
->total
;
3308 ag
= XFS_FSB_TO_AGNO(mp
, args
->fsbno
);
3309 if (ag
== NULLAGNUMBER
)
3312 error
= xfs_bmap_longest_free_extent(args
->tp
, ag
, blen
, ¬init
);
3316 if (*blen
< args
->maxlen
) {
3317 error
= xfs_filestream_new_ag(ap
, &ag
);
3321 error
= xfs_bmap_longest_free_extent(args
->tp
, ag
, blen
,
3328 xfs_bmap_select_minlen(ap
, args
, blen
, notinit
);
3331 * Set the failure fallback case to look in the selected AG as stream
3334 ap
->blkno
= args
->fsbno
= XFS_AGB_TO_FSB(mp
, ag
, 0);
3338 /* Update all inode and quota accounting for the allocation we just did. */
3340 xfs_bmap_btalloc_accounting(
3341 struct xfs_bmalloca
*ap
,
3342 struct xfs_alloc_arg
*args
)
3344 if (ap
->flags
& XFS_BMAPI_COWFORK
) {
3346 * COW fork blocks are in-core only and thus are treated as
3347 * in-core quota reservation (like delalloc blocks) even when
3348 * converted to real blocks. The quota reservation is not
3349 * accounted to disk until blocks are remapped to the data
3350 * fork. So if these blocks were previously delalloc, we
3351 * already have quota reservation and there's nothing to do
3358 * Otherwise, we've allocated blocks in a hole. The transaction
3359 * has acquired in-core quota reservation for this extent.
3360 * Rather than account these as real blocks, however, we reduce
3361 * the transaction quota reservation based on the allocation.
3362 * This essentially transfers the transaction quota reservation
3363 * to that of a delalloc extent.
3365 ap
->ip
->i_delayed_blks
+= args
->len
;
3366 xfs_trans_mod_dquot_byino(ap
->tp
, ap
->ip
, XFS_TRANS_DQ_RES_BLKS
,
3371 /* data/attr fork only */
3372 ap
->ip
->i_d
.di_nblocks
+= args
->len
;
3373 xfs_trans_log_inode(ap
->tp
, ap
->ip
, XFS_ILOG_CORE
);
3375 ap
->ip
->i_delayed_blks
-= args
->len
;
3376 xfs_trans_mod_dquot_byino(ap
->tp
, ap
->ip
,
3377 ap
->wasdel
? XFS_TRANS_DQ_DELBCOUNT
: XFS_TRANS_DQ_BCOUNT
,
3383 struct xfs_bmalloca
*ap
) /* bmap alloc argument struct */
3385 xfs_mount_t
*mp
; /* mount point structure */
3386 xfs_alloctype_t atype
= 0; /* type for allocation routines */
3387 xfs_extlen_t align
= 0; /* minimum allocation alignment */
3388 xfs_agnumber_t fb_agno
; /* ag number of ap->firstblock */
3390 xfs_alloc_arg_t args
;
3391 xfs_fileoff_t orig_offset
;
3392 xfs_extlen_t orig_length
;
3394 xfs_extlen_t nextminlen
= 0;
3395 int nullfb
; /* true if ap->firstblock isn't set */
3402 orig_offset
= ap
->offset
;
3403 orig_length
= ap
->length
;
3405 mp
= ap
->ip
->i_mount
;
3407 /* stripe alignment for allocation is determined by mount parameters */
3409 if (mp
->m_swidth
&& (mp
->m_flags
& XFS_MOUNT_SWALLOC
))
3410 stripe_align
= mp
->m_swidth
;
3411 else if (mp
->m_dalign
)
3412 stripe_align
= mp
->m_dalign
;
3414 if (ap
->flags
& XFS_BMAPI_COWFORK
)
3415 align
= xfs_get_cowextsz_hint(ap
->ip
);
3416 else if (xfs_alloc_is_userdata(ap
->datatype
))
3417 align
= xfs_get_extsz_hint(ap
->ip
);
3419 error
= xfs_bmap_extsize_align(mp
, &ap
->got
, &ap
->prev
,
3420 align
, 0, ap
->eof
, 0, ap
->conv
,
3421 &ap
->offset
, &ap
->length
);
3427 nullfb
= *ap
->firstblock
== NULLFSBLOCK
;
3428 fb_agno
= nullfb
? NULLAGNUMBER
: XFS_FSB_TO_AGNO(mp
, *ap
->firstblock
);
3430 if (xfs_alloc_is_userdata(ap
->datatype
) &&
3431 xfs_inode_is_filestream(ap
->ip
)) {
3432 ag
= xfs_filestream_lookup_ag(ap
->ip
);
3433 ag
= (ag
!= NULLAGNUMBER
) ? ag
: 0;
3434 ap
->blkno
= XFS_AGB_TO_FSB(mp
, ag
, 0);
3436 ap
->blkno
= XFS_INO_TO_FSB(mp
, ap
->ip
->i_ino
);
3439 ap
->blkno
= *ap
->firstblock
;
3441 xfs_bmap_adjacent(ap
);
3444 * If allowed, use ap->blkno; otherwise must use firstblock since
3445 * it's in the right allocation group.
3447 if (nullfb
|| XFS_FSB_TO_AGNO(mp
, ap
->blkno
) == fb_agno
)
3450 ap
->blkno
= *ap
->firstblock
;
3452 * Normal allocation, done through xfs_alloc_vextent.
3454 tryagain
= isaligned
= 0;
3455 memset(&args
, 0, sizeof(args
));
3458 args
.fsbno
= ap
->blkno
;
3459 xfs_rmap_skip_owner_update(&args
.oinfo
);
3461 /* Trim the allocation back to the maximum an AG can fit. */
3462 args
.maxlen
= min(ap
->length
, mp
->m_ag_max_usable
);
3463 args
.firstblock
= *ap
->firstblock
;
3467 * Search for an allocation group with a single extent large
3468 * enough for the request. If one isn't found, then adjust
3469 * the minimum allocation size to the largest space found.
3471 if (xfs_alloc_is_userdata(ap
->datatype
) &&
3472 xfs_inode_is_filestream(ap
->ip
))
3473 error
= xfs_bmap_btalloc_filestreams(ap
, &args
, &blen
);
3475 error
= xfs_bmap_btalloc_nullfb(ap
, &args
, &blen
);
3478 } else if (ap
->dfops
->dop_low
) {
3479 if (xfs_inode_is_filestream(ap
->ip
))
3480 args
.type
= XFS_ALLOCTYPE_FIRST_AG
;
3482 args
.type
= XFS_ALLOCTYPE_START_BNO
;
3483 args
.total
= args
.minlen
= ap
->minlen
;
3485 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
3486 args
.total
= ap
->total
;
3487 args
.minlen
= ap
->minlen
;
3489 /* apply extent size hints if obtained earlier */
3492 div_u64_rem(ap
->offset
, args
.prod
, &args
.mod
);
3494 args
.mod
= args
.prod
- args
.mod
;
3495 } else if (mp
->m_sb
.sb_blocksize
>= PAGE_SIZE
) {
3499 args
.prod
= PAGE_SIZE
>> mp
->m_sb
.sb_blocklog
;
3500 div_u64_rem(ap
->offset
, args
.prod
, &args
.mod
);
3502 args
.mod
= args
.prod
- args
.mod
;
3505 * If we are not low on available data blocks, and the
3506 * underlying logical volume manager is a stripe, and
3507 * the file offset is zero then try to allocate data
3508 * blocks on stripe unit boundary.
3509 * NOTE: ap->aeof is only set if the allocation length
3510 * is >= the stripe unit and the allocation offset is
3511 * at the end of file.
3513 if (!ap
->dfops
->dop_low
&& ap
->aeof
) {
3515 args
.alignment
= stripe_align
;
3519 * Adjust for alignment
3521 if (blen
> args
.alignment
&& blen
<= args
.maxlen
)
3522 args
.minlen
= blen
- args
.alignment
;
3523 args
.minalignslop
= 0;
3526 * First try an exact bno allocation.
3527 * If it fails then do a near or start bno
3528 * allocation with alignment turned on.
3532 args
.type
= XFS_ALLOCTYPE_THIS_BNO
;
3535 * Compute the minlen+alignment for the
3536 * next case. Set slop so that the value
3537 * of minlen+alignment+slop doesn't go up
3538 * between the calls.
3540 if (blen
> stripe_align
&& blen
<= args
.maxlen
)
3541 nextminlen
= blen
- stripe_align
;
3543 nextminlen
= args
.minlen
;
3544 if (nextminlen
+ stripe_align
> args
.minlen
+ 1)
3546 nextminlen
+ stripe_align
-
3549 args
.minalignslop
= 0;
3553 args
.minalignslop
= 0;
3555 args
.minleft
= ap
->minleft
;
3556 args
.wasdel
= ap
->wasdel
;
3557 args
.resv
= XFS_AG_RESV_NONE
;
3558 args
.datatype
= ap
->datatype
;
3559 if (ap
->datatype
& XFS_ALLOC_USERDATA_ZERO
)
3562 error
= xfs_alloc_vextent(&args
);
3566 if (tryagain
&& args
.fsbno
== NULLFSBLOCK
) {
3568 * Exact allocation failed. Now try with alignment
3572 args
.fsbno
= ap
->blkno
;
3573 args
.alignment
= stripe_align
;
3574 args
.minlen
= nextminlen
;
3575 args
.minalignslop
= 0;
3577 if ((error
= xfs_alloc_vextent(&args
)))
3580 if (isaligned
&& args
.fsbno
== NULLFSBLOCK
) {
3582 * allocation failed, so turn off alignment and
3586 args
.fsbno
= ap
->blkno
;
3588 if ((error
= xfs_alloc_vextent(&args
)))
3591 if (args
.fsbno
== NULLFSBLOCK
&& nullfb
&&
3592 args
.minlen
> ap
->minlen
) {
3593 args
.minlen
= ap
->minlen
;
3594 args
.type
= XFS_ALLOCTYPE_START_BNO
;
3595 args
.fsbno
= ap
->blkno
;
3596 if ((error
= xfs_alloc_vextent(&args
)))
3599 if (args
.fsbno
== NULLFSBLOCK
&& nullfb
) {
3601 args
.type
= XFS_ALLOCTYPE_FIRST_AG
;
3602 args
.total
= ap
->minlen
;
3603 if ((error
= xfs_alloc_vextent(&args
)))
3605 ap
->dfops
->dop_low
= true;
3607 if (args
.fsbno
!= NULLFSBLOCK
) {
3609 * check the allocation happened at the same or higher AG than
3610 * the first block that was allocated.
3612 ASSERT(*ap
->firstblock
== NULLFSBLOCK
||
3613 XFS_FSB_TO_AGNO(mp
, *ap
->firstblock
) <=
3614 XFS_FSB_TO_AGNO(mp
, args
.fsbno
));
3616 ap
->blkno
= args
.fsbno
;
3617 if (*ap
->firstblock
== NULLFSBLOCK
)
3618 *ap
->firstblock
= args
.fsbno
;
3619 ASSERT(nullfb
|| fb_agno
<= args
.agno
);
3620 ap
->length
= args
.len
;
3622 * If the extent size hint is active, we tried to round the
3623 * caller's allocation request offset down to extsz and the
3624 * length up to another extsz boundary. If we found a free
3625 * extent we mapped it in starting at this new offset. If the
3626 * newly mapped space isn't long enough to cover any of the
3627 * range of offsets that was originally requested, move the
3628 * mapping up so that we can fill as much of the caller's
3629 * original request as possible. Free space is apparently
3630 * very fragmented so we're unlikely to be able to satisfy the
3633 if (ap
->length
<= orig_length
)
3634 ap
->offset
= orig_offset
;
3635 else if (ap
->offset
+ ap
->length
< orig_offset
+ orig_length
)
3636 ap
->offset
= orig_offset
+ orig_length
- ap
->length
;
3637 xfs_bmap_btalloc_accounting(ap
, &args
);
3639 ap
->blkno
= NULLFSBLOCK
;
3646 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
3647 * It figures out where to ask the underlying allocator to put the new extent.
3651 struct xfs_bmalloca
*ap
) /* bmap alloc argument struct */
3653 if (XFS_IS_REALTIME_INODE(ap
->ip
) &&
3654 xfs_alloc_is_userdata(ap
->datatype
))
3655 return xfs_bmap_rtalloc(ap
);
3656 return xfs_bmap_btalloc(ap
);
3659 /* Trim extent to fit a logical block range. */
3662 struct xfs_bmbt_irec
*irec
,
3666 xfs_fileoff_t distance
;
3667 xfs_fileoff_t end
= bno
+ len
;
3669 if (irec
->br_startoff
+ irec
->br_blockcount
<= bno
||
3670 irec
->br_startoff
>= end
) {
3671 irec
->br_blockcount
= 0;
3675 if (irec
->br_startoff
< bno
) {
3676 distance
= bno
- irec
->br_startoff
;
3677 if (isnullstartblock(irec
->br_startblock
))
3678 irec
->br_startblock
= DELAYSTARTBLOCK
;
3679 if (irec
->br_startblock
!= DELAYSTARTBLOCK
&&
3680 irec
->br_startblock
!= HOLESTARTBLOCK
)
3681 irec
->br_startblock
+= distance
;
3682 irec
->br_startoff
+= distance
;
3683 irec
->br_blockcount
-= distance
;
3686 if (end
< irec
->br_startoff
+ irec
->br_blockcount
) {
3687 distance
= irec
->br_startoff
+ irec
->br_blockcount
- end
;
3688 irec
->br_blockcount
-= distance
;
3692 /* trim extent to within eof */
3694 xfs_trim_extent_eof(
3695 struct xfs_bmbt_irec
*irec
,
3696 struct xfs_inode
*ip
)
3699 xfs_trim_extent(irec
, 0, XFS_B_TO_FSB(ip
->i_mount
,
3700 i_size_read(VFS_I(ip
))));
3704 * Trim the returned map to the required bounds
3708 struct xfs_bmbt_irec
*mval
,
3709 struct xfs_bmbt_irec
*got
,
3717 if ((flags
& XFS_BMAPI_ENTIRE
) ||
3718 got
->br_startoff
+ got
->br_blockcount
<= obno
) {
3720 if (isnullstartblock(got
->br_startblock
))
3721 mval
->br_startblock
= DELAYSTARTBLOCK
;
3727 ASSERT((*bno
>= obno
) || (n
== 0));
3729 mval
->br_startoff
= *bno
;
3730 if (isnullstartblock(got
->br_startblock
))
3731 mval
->br_startblock
= DELAYSTARTBLOCK
;
3733 mval
->br_startblock
= got
->br_startblock
+
3734 (*bno
- got
->br_startoff
);
3736 * Return the minimum of what we got and what we asked for for
3737 * the length. We can use the len variable here because it is
3738 * modified below and we could have been there before coming
3739 * here if the first part of the allocation didn't overlap what
3742 mval
->br_blockcount
= XFS_FILBLKS_MIN(end
- *bno
,
3743 got
->br_blockcount
- (*bno
- got
->br_startoff
));
3744 mval
->br_state
= got
->br_state
;
3745 ASSERT(mval
->br_blockcount
<= len
);
3750 * Update and validate the extent map to return
3753 xfs_bmapi_update_map(
3754 struct xfs_bmbt_irec
**map
,
3762 xfs_bmbt_irec_t
*mval
= *map
;
3764 ASSERT((flags
& XFS_BMAPI_ENTIRE
) ||
3765 ((mval
->br_startoff
+ mval
->br_blockcount
) <= end
));
3766 ASSERT((flags
& XFS_BMAPI_ENTIRE
) || (mval
->br_blockcount
<= *len
) ||
3767 (mval
->br_startoff
< obno
));
3769 *bno
= mval
->br_startoff
+ mval
->br_blockcount
;
3771 if (*n
> 0 && mval
->br_startoff
== mval
[-1].br_startoff
) {
3772 /* update previous map with new information */
3773 ASSERT(mval
->br_startblock
== mval
[-1].br_startblock
);
3774 ASSERT(mval
->br_blockcount
> mval
[-1].br_blockcount
);
3775 ASSERT(mval
->br_state
== mval
[-1].br_state
);
3776 mval
[-1].br_blockcount
= mval
->br_blockcount
;
3777 mval
[-1].br_state
= mval
->br_state
;
3778 } else if (*n
> 0 && mval
->br_startblock
!= DELAYSTARTBLOCK
&&
3779 mval
[-1].br_startblock
!= DELAYSTARTBLOCK
&&
3780 mval
[-1].br_startblock
!= HOLESTARTBLOCK
&&
3781 mval
->br_startblock
== mval
[-1].br_startblock
+
3782 mval
[-1].br_blockcount
&&
3783 mval
[-1].br_state
== mval
->br_state
) {
3784 ASSERT(mval
->br_startoff
==
3785 mval
[-1].br_startoff
+ mval
[-1].br_blockcount
);
3786 mval
[-1].br_blockcount
+= mval
->br_blockcount
;
3787 } else if (*n
> 0 &&
3788 mval
->br_startblock
== DELAYSTARTBLOCK
&&
3789 mval
[-1].br_startblock
== DELAYSTARTBLOCK
&&
3790 mval
->br_startoff
==
3791 mval
[-1].br_startoff
+ mval
[-1].br_blockcount
) {
3792 mval
[-1].br_blockcount
+= mval
->br_blockcount
;
3793 mval
[-1].br_state
= mval
->br_state
;
3794 } else if (!((*n
== 0) &&
3795 ((mval
->br_startoff
+ mval
->br_blockcount
) <=
3804 * Map file blocks to filesystem blocks without allocation.
3808 struct xfs_inode
*ip
,
3811 struct xfs_bmbt_irec
*mval
,
3815 struct xfs_mount
*mp
= ip
->i_mount
;
3816 struct xfs_ifork
*ifp
;
3817 struct xfs_bmbt_irec got
;
3820 struct xfs_iext_cursor icur
;
3824 int whichfork
= xfs_bmapi_whichfork(flags
);
3827 ASSERT(!(flags
& ~(XFS_BMAPI_ATTRFORK
|XFS_BMAPI_ENTIRE
|
3828 XFS_BMAPI_COWFORK
)));
3829 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_SHARED
|XFS_ILOCK_EXCL
));
3831 if (unlikely(XFS_TEST_ERROR(
3832 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
3833 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
3834 mp
, XFS_ERRTAG_BMAPIFORMAT
))) {
3835 XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW
, mp
);
3836 return -EFSCORRUPTED
;
3839 if (XFS_FORCED_SHUTDOWN(mp
))
3842 XFS_STATS_INC(mp
, xs_blk_mapr
);
3844 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
3846 /* No CoW fork? Return a hole. */
3847 if (whichfork
== XFS_COW_FORK
&& !ifp
) {
3848 mval
->br_startoff
= bno
;
3849 mval
->br_startblock
= HOLESTARTBLOCK
;
3850 mval
->br_blockcount
= len
;
3851 mval
->br_state
= XFS_EXT_NORM
;
3856 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
3857 error
= xfs_iread_extents(NULL
, ip
, whichfork
);
3862 if (!xfs_iext_lookup_extent(ip
, ifp
, bno
, &icur
, &got
))
3867 while (bno
< end
&& n
< *nmap
) {
3868 /* Reading past eof, act as though there's a hole up to end. */
3870 got
.br_startoff
= end
;
3871 if (got
.br_startoff
> bno
) {
3872 /* Reading in a hole. */
3873 mval
->br_startoff
= bno
;
3874 mval
->br_startblock
= HOLESTARTBLOCK
;
3875 mval
->br_blockcount
=
3876 XFS_FILBLKS_MIN(len
, got
.br_startoff
- bno
);
3877 mval
->br_state
= XFS_EXT_NORM
;
3878 bno
+= mval
->br_blockcount
;
3879 len
-= mval
->br_blockcount
;
3885 /* set up the extent map to return. */
3886 xfs_bmapi_trim_map(mval
, &got
, &bno
, len
, obno
, end
, n
, flags
);
3887 xfs_bmapi_update_map(&mval
, &bno
, &len
, obno
, end
, &n
, flags
);
3889 /* If we're done, stop now. */
3890 if (bno
>= end
|| n
>= *nmap
)
3893 /* Else go on to the next record. */
3894 if (!xfs_iext_next_extent(ifp
, &icur
, &got
))
3902 * Add a delayed allocation extent to an inode. Blocks are reserved from the
3903 * global pool and the extent inserted into the inode in-core extent tree.
3905 * On entry, got refers to the first extent beyond the offset of the extent to
3906 * allocate or eof is specified if no such extent exists. On return, got refers
3907 * to the extent record that was inserted to the inode fork.
3909 * Note that the allocated extent may have been merged with contiguous extents
3910 * during insertion into the inode fork. Thus, got does not reflect the current
3911 * state of the inode fork on return. If necessary, the caller can use lastx to
3912 * look up the updated record in the inode fork.
3915 xfs_bmapi_reserve_delalloc(
3916 struct xfs_inode
*ip
,
3920 xfs_filblks_t prealloc
,
3921 struct xfs_bmbt_irec
*got
,
3922 struct xfs_iext_cursor
*icur
,
3925 struct xfs_mount
*mp
= ip
->i_mount
;
3926 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
3928 xfs_extlen_t indlen
;
3930 xfs_fileoff_t aoff
= off
;
3933 * Cap the alloc length. Keep track of prealloc so we know whether to
3934 * tag the inode before we return.
3936 alen
= XFS_FILBLKS_MIN(len
+ prealloc
, MAXEXTLEN
);
3938 alen
= XFS_FILBLKS_MIN(alen
, got
->br_startoff
- aoff
);
3939 if (prealloc
&& alen
>= len
)
3940 prealloc
= alen
- len
;
3942 /* Figure out the extent size, adjust alen */
3943 if (whichfork
== XFS_COW_FORK
) {
3944 struct xfs_bmbt_irec prev
;
3945 xfs_extlen_t extsz
= xfs_get_cowextsz_hint(ip
);
3947 if (!xfs_iext_peek_prev_extent(ifp
, icur
, &prev
))
3948 prev
.br_startoff
= NULLFILEOFF
;
3950 error
= xfs_bmap_extsize_align(mp
, got
, &prev
, extsz
, 0, eof
,
3951 1, 0, &aoff
, &alen
);
3956 * Make a transaction-less quota reservation for delayed allocation
3957 * blocks. This number gets adjusted later. We return if we haven't
3958 * allocated blocks already inside this loop.
3960 error
= xfs_trans_reserve_quota_nblks(NULL
, ip
, (long)alen
, 0,
3961 XFS_QMOPT_RES_REGBLKS
);
3966 * Split changing sb for alen and indlen since they could be coming
3967 * from different places.
3969 indlen
= (xfs_extlen_t
)xfs_bmap_worst_indlen(ip
, alen
);
3972 error
= xfs_mod_fdblocks(mp
, -((int64_t)alen
), false);
3974 goto out_unreserve_quota
;
3976 error
= xfs_mod_fdblocks(mp
, -((int64_t)indlen
), false);
3978 goto out_unreserve_blocks
;
3981 ip
->i_delayed_blks
+= alen
;
3983 got
->br_startoff
= aoff
;
3984 got
->br_startblock
= nullstartblock(indlen
);
3985 got
->br_blockcount
= alen
;
3986 got
->br_state
= XFS_EXT_NORM
;
3988 xfs_bmap_add_extent_hole_delay(ip
, whichfork
, icur
, got
);
3991 * Tag the inode if blocks were preallocated. Note that COW fork
3992 * preallocation can occur at the start or end of the extent, even when
3993 * prealloc == 0, so we must also check the aligned offset and length.
3995 if (whichfork
== XFS_DATA_FORK
&& prealloc
)
3996 xfs_inode_set_eofblocks_tag(ip
);
3997 if (whichfork
== XFS_COW_FORK
&& (prealloc
|| aoff
< off
|| alen
> len
))
3998 xfs_inode_set_cowblocks_tag(ip
);
4002 out_unreserve_blocks
:
4003 xfs_mod_fdblocks(mp
, alen
, false);
4004 out_unreserve_quota
:
4005 if (XFS_IS_QUOTA_ON(mp
))
4006 xfs_trans_unreserve_quota_nblks(NULL
, ip
, (long)alen
, 0,
4007 XFS_QMOPT_RES_REGBLKS
);
4013 struct xfs_bmalloca
*bma
)
4015 struct xfs_mount
*mp
= bma
->ip
->i_mount
;
4016 int whichfork
= xfs_bmapi_whichfork(bma
->flags
);
4017 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
4018 int tmp_logflags
= 0;
4021 ASSERT(bma
->length
> 0);
4024 * For the wasdelay case, we could also just allocate the stuff asked
4025 * for in this bmap call but that wouldn't be as good.
4028 bma
->length
= (xfs_extlen_t
)bma
->got
.br_blockcount
;
4029 bma
->offset
= bma
->got
.br_startoff
;
4030 xfs_iext_peek_prev_extent(ifp
, &bma
->icur
, &bma
->prev
);
4032 bma
->length
= XFS_FILBLKS_MIN(bma
->length
, MAXEXTLEN
);
4034 bma
->length
= XFS_FILBLKS_MIN(bma
->length
,
4035 bma
->got
.br_startoff
- bma
->offset
);
4039 * Set the data type being allocated. For the data fork, the first data
4040 * in the file is treated differently to all other allocations. For the
4041 * attribute fork, we only need to ensure the allocated range is not on
4044 if (!(bma
->flags
& XFS_BMAPI_METADATA
)) {
4045 bma
->datatype
= XFS_ALLOC_NOBUSY
;
4046 if (whichfork
== XFS_DATA_FORK
) {
4047 if (bma
->offset
== 0)
4048 bma
->datatype
|= XFS_ALLOC_INITIAL_USER_DATA
;
4050 bma
->datatype
|= XFS_ALLOC_USERDATA
;
4052 if (bma
->flags
& XFS_BMAPI_ZERO
)
4053 bma
->datatype
|= XFS_ALLOC_USERDATA_ZERO
;
4056 bma
->minlen
= (bma
->flags
& XFS_BMAPI_CONTIG
) ? bma
->length
: 1;
4059 * Only want to do the alignment at the eof if it is userdata and
4060 * allocation length is larger than a stripe unit.
4062 if (mp
->m_dalign
&& bma
->length
>= mp
->m_dalign
&&
4063 !(bma
->flags
& XFS_BMAPI_METADATA
) && whichfork
== XFS_DATA_FORK
) {
4064 error
= xfs_bmap_isaeof(bma
, whichfork
);
4069 error
= xfs_bmap_alloc(bma
);
4074 bma
->cur
->bc_private
.b
.firstblock
= *bma
->firstblock
;
4075 if (bma
->blkno
== NULLFSBLOCK
)
4077 if ((ifp
->if_flags
& XFS_IFBROOT
) && !bma
->cur
) {
4078 bma
->cur
= xfs_bmbt_init_cursor(mp
, bma
->tp
, bma
->ip
, whichfork
);
4079 bma
->cur
->bc_private
.b
.firstblock
= *bma
->firstblock
;
4080 bma
->cur
->bc_private
.b
.dfops
= bma
->dfops
;
4083 * Bump the number of extents we've allocated
4089 bma
->cur
->bc_private
.b
.flags
=
4090 bma
->wasdel
? XFS_BTCUR_BPRV_WASDEL
: 0;
4092 bma
->got
.br_startoff
= bma
->offset
;
4093 bma
->got
.br_startblock
= bma
->blkno
;
4094 bma
->got
.br_blockcount
= bma
->length
;
4095 bma
->got
.br_state
= XFS_EXT_NORM
;
4098 * In the data fork, a wasdelay extent has been initialized, so
4099 * shouldn't be flagged as unwritten.
4101 * For the cow fork, however, we convert delalloc reservations
4102 * (extents allocated for speculative preallocation) to
4103 * allocated unwritten extents, and only convert the unwritten
4104 * extents to real extents when we're about to write the data.
4106 if ((!bma
->wasdel
|| (bma
->flags
& XFS_BMAPI_COWFORK
)) &&
4107 (bma
->flags
& XFS_BMAPI_PREALLOC
) &&
4108 xfs_sb_version_hasextflgbit(&mp
->m_sb
))
4109 bma
->got
.br_state
= XFS_EXT_UNWRITTEN
;
4112 error
= xfs_bmap_add_extent_delay_real(bma
, whichfork
);
4114 error
= xfs_bmap_add_extent_hole_real(bma
->tp
, bma
->ip
,
4115 whichfork
, &bma
->icur
, &bma
->cur
, &bma
->got
,
4116 bma
->firstblock
, bma
->dfops
, &bma
->logflags
,
4119 bma
->logflags
|= tmp_logflags
;
4124 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4125 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4126 * the neighbouring ones.
4128 xfs_iext_get_extent(ifp
, &bma
->icur
, &bma
->got
);
4130 ASSERT(bma
->got
.br_startoff
<= bma
->offset
);
4131 ASSERT(bma
->got
.br_startoff
+ bma
->got
.br_blockcount
>=
4132 bma
->offset
+ bma
->length
);
4133 ASSERT(bma
->got
.br_state
== XFS_EXT_NORM
||
4134 bma
->got
.br_state
== XFS_EXT_UNWRITTEN
);
4139 xfs_bmapi_convert_unwritten(
4140 struct xfs_bmalloca
*bma
,
4141 struct xfs_bmbt_irec
*mval
,
4145 int whichfork
= xfs_bmapi_whichfork(flags
);
4146 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
4147 int tmp_logflags
= 0;
4150 /* check if we need to do unwritten->real conversion */
4151 if (mval
->br_state
== XFS_EXT_UNWRITTEN
&&
4152 (flags
& XFS_BMAPI_PREALLOC
))
4155 /* check if we need to do real->unwritten conversion */
4156 if (mval
->br_state
== XFS_EXT_NORM
&&
4157 (flags
& (XFS_BMAPI_PREALLOC
| XFS_BMAPI_CONVERT
)) !=
4158 (XFS_BMAPI_PREALLOC
| XFS_BMAPI_CONVERT
))
4162 * Modify (by adding) the state flag, if writing.
4164 ASSERT(mval
->br_blockcount
<= len
);
4165 if ((ifp
->if_flags
& XFS_IFBROOT
) && !bma
->cur
) {
4166 bma
->cur
= xfs_bmbt_init_cursor(bma
->ip
->i_mount
, bma
->tp
,
4167 bma
->ip
, whichfork
);
4168 bma
->cur
->bc_private
.b
.firstblock
= *bma
->firstblock
;
4169 bma
->cur
->bc_private
.b
.dfops
= bma
->dfops
;
4171 mval
->br_state
= (mval
->br_state
== XFS_EXT_UNWRITTEN
)
4172 ? XFS_EXT_NORM
: XFS_EXT_UNWRITTEN
;
4175 * Before insertion into the bmbt, zero the range being converted
4178 if (flags
& XFS_BMAPI_ZERO
) {
4179 error
= xfs_zero_extent(bma
->ip
, mval
->br_startblock
,
4180 mval
->br_blockcount
);
4185 error
= xfs_bmap_add_extent_unwritten_real(bma
->tp
, bma
->ip
, whichfork
,
4186 &bma
->icur
, &bma
->cur
, mval
, bma
->firstblock
,
4187 bma
->dfops
, &tmp_logflags
);
4189 * Log the inode core unconditionally in the unwritten extent conversion
4190 * path because the conversion might not have done so (e.g., if the
4191 * extent count hasn't changed). We need to make sure the inode is dirty
4192 * in the transaction for the sake of fsync(), even if nothing has
4193 * changed, because fsync() will not force the log for this transaction
4194 * unless it sees the inode pinned.
4196 * Note: If we're only converting cow fork extents, there aren't
4197 * any on-disk updates to make, so we don't need to log anything.
4199 if (whichfork
!= XFS_COW_FORK
)
4200 bma
->logflags
|= tmp_logflags
| XFS_ILOG_CORE
;
4205 * Update our extent pointer, given that
4206 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4207 * of the neighbouring ones.
4209 xfs_iext_get_extent(ifp
, &bma
->icur
, &bma
->got
);
4212 * We may have combined previously unwritten space with written space,
4213 * so generate another request.
4215 if (mval
->br_blockcount
< len
)
4221 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4222 * extent state if necessary. Details behaviour is controlled by the flags
4223 * parameter. Only allocates blocks from a single allocation group, to avoid
4226 * The returned value in "firstblock" from the first call in a transaction
4227 * must be remembered and presented to subsequent calls in "firstblock".
4228 * An upper bound for the number of blocks to be allocated is supplied to
4229 * the first call in "total"; if no allocation group has that many free
4230 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4234 struct xfs_trans
*tp
, /* transaction pointer */
4235 struct xfs_inode
*ip
, /* incore inode */
4236 xfs_fileoff_t bno
, /* starting file offs. mapped */
4237 xfs_filblks_t len
, /* length to map in file */
4238 int flags
, /* XFS_BMAPI_... */
4239 xfs_fsblock_t
*firstblock
, /* first allocated block
4240 controls a.g. for allocs */
4241 xfs_extlen_t total
, /* total blocks needed */
4242 struct xfs_bmbt_irec
*mval
, /* output: map values */
4243 int *nmap
, /* i/o: mval size/count */
4244 struct xfs_defer_ops
*dfops
) /* i/o: list extents to free */
4246 struct xfs_mount
*mp
= ip
->i_mount
;
4247 struct xfs_ifork
*ifp
;
4248 struct xfs_bmalloca bma
= { NULL
}; /* args for xfs_bmap_alloc */
4249 xfs_fileoff_t end
; /* end of mapped file region */
4250 bool eof
= false; /* after the end of extents */
4251 int error
; /* error return */
4252 int n
; /* current extent index */
4253 xfs_fileoff_t obno
; /* old block number (offset) */
4254 int whichfork
; /* data or attr fork */
4257 xfs_fileoff_t orig_bno
; /* original block number value */
4258 int orig_flags
; /* original flags arg value */
4259 xfs_filblks_t orig_len
; /* original value of len arg */
4260 struct xfs_bmbt_irec
*orig_mval
; /* original value of mval */
4261 int orig_nmap
; /* original value of *nmap */
4269 whichfork
= xfs_bmapi_whichfork(flags
);
4272 ASSERT(*nmap
<= XFS_BMAP_MAX_NMAP
);
4273 ASSERT(tp
!= NULL
||
4274 (flags
& (XFS_BMAPI_CONVERT
| XFS_BMAPI_COWFORK
)) ==
4275 (XFS_BMAPI_CONVERT
| XFS_BMAPI_COWFORK
));
4277 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_LOCAL
);
4278 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
4279 ASSERT(!(flags
& XFS_BMAPI_REMAP
));
4281 /* zeroing is for currently only for data extents, not metadata */
4282 ASSERT((flags
& (XFS_BMAPI_METADATA
| XFS_BMAPI_ZERO
)) !=
4283 (XFS_BMAPI_METADATA
| XFS_BMAPI_ZERO
));
4285 * we can allocate unwritten extents or pre-zero allocated blocks,
4286 * but it makes no sense to do both at once. This would result in
4287 * zeroing the unwritten extent twice, but it still being an
4288 * unwritten extent....
4290 ASSERT((flags
& (XFS_BMAPI_PREALLOC
| XFS_BMAPI_ZERO
)) !=
4291 (XFS_BMAPI_PREALLOC
| XFS_BMAPI_ZERO
));
4293 if (unlikely(XFS_TEST_ERROR(
4294 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
4295 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
4296 mp
, XFS_ERRTAG_BMAPIFORMAT
))) {
4297 XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW
, mp
);
4298 return -EFSCORRUPTED
;
4301 if (XFS_FORCED_SHUTDOWN(mp
))
4304 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4306 XFS_STATS_INC(mp
, xs_blk_mapw
);
4308 if (*firstblock
== NULLFSBLOCK
) {
4309 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
)
4310 bma
.minleft
= be16_to_cpu(ifp
->if_broot
->bb_level
) + 1;
4317 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
4318 error
= xfs_iread_extents(tp
, ip
, whichfork
);
4327 if (!xfs_iext_lookup_extent(ip
, ifp
, bno
, &bma
.icur
, &bma
.got
))
4329 if (!xfs_iext_peek_prev_extent(ifp
, &bma
.icur
, &bma
.prev
))
4330 bma
.prev
.br_startoff
= NULLFILEOFF
;
4336 bma
.firstblock
= firstblock
;
4338 while (bno
< end
&& n
< *nmap
) {
4339 bool need_alloc
= false, wasdelay
= false;
4341 /* in hole or beyond EOF? */
4342 if (eof
|| bma
.got
.br_startoff
> bno
) {
4344 * CoW fork conversions should /never/ hit EOF or
4345 * holes. There should always be something for us
4348 ASSERT(!((flags
& XFS_BMAPI_CONVERT
) &&
4349 (flags
& XFS_BMAPI_COWFORK
)));
4351 if (flags
& XFS_BMAPI_DELALLOC
) {
4353 * For the COW fork we can reasonably get a
4354 * request for converting an extent that races
4355 * with other threads already having converted
4356 * part of it, as there converting COW to
4357 * regular blocks is not protected using the
4360 ASSERT(flags
& XFS_BMAPI_COWFORK
);
4361 if (!(flags
& XFS_BMAPI_COWFORK
)) {
4366 if (eof
|| bno
>= end
)
4371 } else if (isnullstartblock(bma
.got
.br_startblock
)) {
4376 * First, deal with the hole before the allocated space
4377 * that we found, if any.
4379 if ((need_alloc
|| wasdelay
) &&
4380 !(flags
& XFS_BMAPI_CONVERT_ONLY
)) {
4382 bma
.conv
= !!(flags
& XFS_BMAPI_CONVERT
);
4383 bma
.wasdel
= wasdelay
;
4388 * There's a 32/64 bit type mismatch between the
4389 * allocation length request (which can be 64 bits in
4390 * length) and the bma length request, which is
4391 * xfs_extlen_t and therefore 32 bits. Hence we have to
4392 * check for 32-bit overflows and handle them here.
4394 if (len
> (xfs_filblks_t
)MAXEXTLEN
)
4395 bma
.length
= MAXEXTLEN
;
4400 ASSERT(bma
.length
> 0);
4401 error
= xfs_bmapi_allocate(&bma
);
4404 if (bma
.blkno
== NULLFSBLOCK
)
4408 * If this is a CoW allocation, record the data in
4409 * the refcount btree for orphan recovery.
4411 if (whichfork
== XFS_COW_FORK
) {
4412 error
= xfs_refcount_alloc_cow_extent(mp
, dfops
,
4413 bma
.blkno
, bma
.length
);
4419 /* Deal with the allocated space we found. */
4420 xfs_bmapi_trim_map(mval
, &bma
.got
, &bno
, len
, obno
,
4423 /* Execute unwritten extent conversion if necessary */
4424 error
= xfs_bmapi_convert_unwritten(&bma
, mval
, len
, flags
);
4425 if (error
== -EAGAIN
)
4430 /* update the extent map to return */
4431 xfs_bmapi_update_map(&mval
, &bno
, &len
, obno
, end
, &n
, flags
);
4434 * If we're done, stop now. Stop when we've allocated
4435 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4436 * the transaction may get too big.
4438 if (bno
>= end
|| n
>= *nmap
|| bma
.nallocs
>= *nmap
)
4441 /* Else go on to the next record. */
4443 if (!xfs_iext_next_extent(ifp
, &bma
.icur
, &bma
.got
))
4449 * Transform from btree to extents, give it cur.
4451 if (xfs_bmap_wants_extents(ip
, whichfork
)) {
4452 int tmp_logflags
= 0;
4455 error
= xfs_bmap_btree_to_extents(tp
, ip
, bma
.cur
,
4456 &tmp_logflags
, whichfork
);
4457 bma
.logflags
|= tmp_logflags
;
4462 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
||
4463 XFS_IFORK_NEXTENTS(ip
, whichfork
) >
4464 XFS_IFORK_MAXEXT(ip
, whichfork
));
4468 * Log everything. Do this after conversion, there's no point in
4469 * logging the extent records if we've converted to btree format.
4471 if ((bma
.logflags
& xfs_ilog_fext(whichfork
)) &&
4472 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
4473 bma
.logflags
&= ~xfs_ilog_fext(whichfork
);
4474 else if ((bma
.logflags
& xfs_ilog_fbroot(whichfork
)) &&
4475 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)
4476 bma
.logflags
&= ~xfs_ilog_fbroot(whichfork
);
4478 * Log whatever the flags say, even if error. Otherwise we might miss
4479 * detecting a case where the data is changed, there's an error,
4480 * and it's not logged so we don't shutdown when we should.
4483 xfs_trans_log_inode(tp
, ip
, bma
.logflags
);
4487 ASSERT(*firstblock
== NULLFSBLOCK
||
4488 XFS_FSB_TO_AGNO(mp
, *firstblock
) <=
4490 bma
.cur
->bc_private
.b
.firstblock
));
4491 *firstblock
= bma
.cur
->bc_private
.b
.firstblock
;
4493 xfs_btree_del_cursor(bma
.cur
,
4494 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
4497 xfs_bmap_validate_ret(orig_bno
, orig_len
, orig_flags
, orig_mval
,
4504 struct xfs_trans
*tp
,
4505 struct xfs_inode
*ip
,
4508 xfs_fsblock_t startblock
,
4509 struct xfs_defer_ops
*dfops
,
4512 struct xfs_mount
*mp
= ip
->i_mount
;
4513 struct xfs_ifork
*ifp
;
4514 struct xfs_btree_cur
*cur
= NULL
;
4515 xfs_fsblock_t firstblock
= NULLFSBLOCK
;
4516 struct xfs_bmbt_irec got
;
4517 struct xfs_iext_cursor icur
;
4518 int whichfork
= xfs_bmapi_whichfork(flags
);
4519 int logflags
= 0, error
;
4521 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4523 ASSERT(len
<= (xfs_filblks_t
)MAXEXTLEN
);
4524 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
4525 ASSERT(!(flags
& ~(XFS_BMAPI_ATTRFORK
| XFS_BMAPI_PREALLOC
|
4526 XFS_BMAPI_NORMAP
)));
4527 ASSERT((flags
& (XFS_BMAPI_ATTRFORK
| XFS_BMAPI_PREALLOC
)) !=
4528 (XFS_BMAPI_ATTRFORK
| XFS_BMAPI_PREALLOC
));
4530 if (unlikely(XFS_TEST_ERROR(
4531 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
4532 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
4533 mp
, XFS_ERRTAG_BMAPIFORMAT
))) {
4534 XFS_ERROR_REPORT("xfs_bmapi_remap", XFS_ERRLEVEL_LOW
, mp
);
4535 return -EFSCORRUPTED
;
4538 if (XFS_FORCED_SHUTDOWN(mp
))
4541 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
4542 error
= xfs_iread_extents(tp
, ip
, whichfork
);
4547 if (xfs_iext_lookup_extent(ip
, ifp
, bno
, &icur
, &got
)) {
4548 /* make sure we only reflink into a hole. */
4549 ASSERT(got
.br_startoff
> bno
);
4550 ASSERT(got
.br_startoff
- bno
>= len
);
4553 ip
->i_d
.di_nblocks
+= len
;
4554 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
4556 if (ifp
->if_flags
& XFS_IFBROOT
) {
4557 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
4558 cur
->bc_private
.b
.firstblock
= firstblock
;
4559 cur
->bc_private
.b
.dfops
= dfops
;
4560 cur
->bc_private
.b
.flags
= 0;
4563 got
.br_startoff
= bno
;
4564 got
.br_startblock
= startblock
;
4565 got
.br_blockcount
= len
;
4566 if (flags
& XFS_BMAPI_PREALLOC
)
4567 got
.br_state
= XFS_EXT_UNWRITTEN
;
4569 got
.br_state
= XFS_EXT_NORM
;
4571 error
= xfs_bmap_add_extent_hole_real(tp
, ip
, whichfork
, &icur
,
4572 &cur
, &got
, &firstblock
, dfops
, &logflags
, flags
);
4576 if (xfs_bmap_wants_extents(ip
, whichfork
)) {
4577 int tmp_logflags
= 0;
4579 error
= xfs_bmap_btree_to_extents(tp
, ip
, cur
,
4580 &tmp_logflags
, whichfork
);
4581 logflags
|= tmp_logflags
;
4585 if (ip
->i_d
.di_format
!= XFS_DINODE_FMT_EXTENTS
)
4586 logflags
&= ~XFS_ILOG_DEXT
;
4587 else if (ip
->i_d
.di_format
!= XFS_DINODE_FMT_BTREE
)
4588 logflags
&= ~XFS_ILOG_DBROOT
;
4591 xfs_trans_log_inode(tp
, ip
, logflags
);
4593 xfs_btree_del_cursor(cur
,
4594 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
4600 * When a delalloc extent is split (e.g., due to a hole punch), the original
4601 * indlen reservation must be shared across the two new extents that are left
4604 * Given the original reservation and the worst case indlen for the two new
4605 * extents (as calculated by xfs_bmap_worst_indlen()), split the original
4606 * reservation fairly across the two new extents. If necessary, steal available
4607 * blocks from a deleted extent to make up a reservation deficiency (e.g., if
4608 * ores == 1). The number of stolen blocks is returned. The availability and
4609 * subsequent accounting of stolen blocks is the responsibility of the caller.
4611 static xfs_filblks_t
4612 xfs_bmap_split_indlen(
4613 xfs_filblks_t ores
, /* original res. */
4614 xfs_filblks_t
*indlen1
, /* ext1 worst indlen */
4615 xfs_filblks_t
*indlen2
, /* ext2 worst indlen */
4616 xfs_filblks_t avail
) /* stealable blocks */
4618 xfs_filblks_t len1
= *indlen1
;
4619 xfs_filblks_t len2
= *indlen2
;
4620 xfs_filblks_t nres
= len1
+ len2
; /* new total res. */
4621 xfs_filblks_t stolen
= 0;
4622 xfs_filblks_t resfactor
;
4625 * Steal as many blocks as we can to try and satisfy the worst case
4626 * indlen for both new extents.
4628 if (ores
< nres
&& avail
)
4629 stolen
= XFS_FILBLKS_MIN(nres
- ores
, avail
);
4632 /* nothing else to do if we've satisfied the new reservation */
4637 * We can't meet the total required reservation for the two extents.
4638 * Calculate the percent of the overall shortage between both extents
4639 * and apply this percentage to each of the requested indlen values.
4640 * This distributes the shortage fairly and reduces the chances that one
4641 * of the two extents is left with nothing when extents are repeatedly
4644 resfactor
= (ores
* 100);
4645 do_div(resfactor
, nres
);
4650 ASSERT(len1
+ len2
<= ores
);
4651 ASSERT(len1
< *indlen1
&& len2
< *indlen2
);
4654 * Hand out the remainder to each extent. If one of the two reservations
4655 * is zero, we want to make sure that one gets a block first. The loop
4656 * below starts with len1, so hand len2 a block right off the bat if it
4659 ores
-= (len1
+ len2
);
4660 ASSERT((*indlen1
- len1
) + (*indlen2
- len2
) >= ores
);
4661 if (ores
&& !len2
&& *indlen2
) {
4666 if (len1
< *indlen1
) {
4672 if (len2
< *indlen2
) {
4685 xfs_bmap_del_extent_delay(
4686 struct xfs_inode
*ip
,
4688 struct xfs_iext_cursor
*icur
,
4689 struct xfs_bmbt_irec
*got
,
4690 struct xfs_bmbt_irec
*del
)
4692 struct xfs_mount
*mp
= ip
->i_mount
;
4693 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4694 struct xfs_bmbt_irec
new;
4695 int64_t da_old
, da_new
, da_diff
= 0;
4696 xfs_fileoff_t del_endoff
, got_endoff
;
4697 xfs_filblks_t got_indlen
, new_indlen
, stolen
;
4698 int state
= xfs_bmap_fork_to_state(whichfork
);
4702 XFS_STATS_INC(mp
, xs_del_exlist
);
4704 isrt
= (whichfork
== XFS_DATA_FORK
) && XFS_IS_REALTIME_INODE(ip
);
4705 del_endoff
= del
->br_startoff
+ del
->br_blockcount
;
4706 got_endoff
= got
->br_startoff
+ got
->br_blockcount
;
4707 da_old
= startblockval(got
->br_startblock
);
4710 ASSERT(del
->br_blockcount
> 0);
4711 ASSERT(got
->br_startoff
<= del
->br_startoff
);
4712 ASSERT(got_endoff
>= del_endoff
);
4715 uint64_t rtexts
= XFS_FSB_TO_B(mp
, del
->br_blockcount
);
4717 do_div(rtexts
, mp
->m_sb
.sb_rextsize
);
4718 xfs_mod_frextents(mp
, rtexts
);
4722 * Update the inode delalloc counter now and wait to update the
4723 * sb counters as we might have to borrow some blocks for the
4724 * indirect block accounting.
4726 error
= xfs_trans_reserve_quota_nblks(NULL
, ip
,
4727 -((long)del
->br_blockcount
), 0,
4728 isrt
? XFS_QMOPT_RES_RTBLKS
: XFS_QMOPT_RES_REGBLKS
);
4731 ip
->i_delayed_blks
-= del
->br_blockcount
;
4733 if (got
->br_startoff
== del
->br_startoff
)
4734 state
|= BMAP_LEFT_FILLING
;
4735 if (got_endoff
== del_endoff
)
4736 state
|= BMAP_RIGHT_FILLING
;
4738 switch (state
& (BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
)) {
4739 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
4741 * Matches the whole extent. Delete the entry.
4743 xfs_iext_remove(ip
, icur
, state
);
4744 xfs_iext_prev(ifp
, icur
);
4746 case BMAP_LEFT_FILLING
:
4748 * Deleting the first part of the extent.
4750 got
->br_startoff
= del_endoff
;
4751 got
->br_blockcount
-= del
->br_blockcount
;
4752 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
,
4753 got
->br_blockcount
), da_old
);
4754 got
->br_startblock
= nullstartblock((int)da_new
);
4755 xfs_iext_update_extent(ip
, state
, icur
, got
);
4757 case BMAP_RIGHT_FILLING
:
4759 * Deleting the last part of the extent.
4761 got
->br_blockcount
= got
->br_blockcount
- del
->br_blockcount
;
4762 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
,
4763 got
->br_blockcount
), da_old
);
4764 got
->br_startblock
= nullstartblock((int)da_new
);
4765 xfs_iext_update_extent(ip
, state
, icur
, got
);
4769 * Deleting the middle of the extent.
4771 * Distribute the original indlen reservation across the two new
4772 * extents. Steal blocks from the deleted extent if necessary.
4773 * Stealing blocks simply fudges the fdblocks accounting below.
4774 * Warn if either of the new indlen reservations is zero as this
4775 * can lead to delalloc problems.
4777 got
->br_blockcount
= del
->br_startoff
- got
->br_startoff
;
4778 got_indlen
= xfs_bmap_worst_indlen(ip
, got
->br_blockcount
);
4780 new.br_blockcount
= got_endoff
- del_endoff
;
4781 new_indlen
= xfs_bmap_worst_indlen(ip
, new.br_blockcount
);
4783 WARN_ON_ONCE(!got_indlen
|| !new_indlen
);
4784 stolen
= xfs_bmap_split_indlen(da_old
, &got_indlen
, &new_indlen
,
4785 del
->br_blockcount
);
4787 got
->br_startblock
= nullstartblock((int)got_indlen
);
4789 new.br_startoff
= del_endoff
;
4790 new.br_state
= got
->br_state
;
4791 new.br_startblock
= nullstartblock((int)new_indlen
);
4793 xfs_iext_update_extent(ip
, state
, icur
, got
);
4794 xfs_iext_next(ifp
, icur
);
4795 xfs_iext_insert(ip
, icur
, &new, state
);
4797 da_new
= got_indlen
+ new_indlen
- stolen
;
4798 del
->br_blockcount
-= stolen
;
4802 ASSERT(da_old
>= da_new
);
4803 da_diff
= da_old
- da_new
;
4805 da_diff
+= del
->br_blockcount
;
4807 xfs_mod_fdblocks(mp
, da_diff
, false);
4812 xfs_bmap_del_extent_cow(
4813 struct xfs_inode
*ip
,
4814 struct xfs_iext_cursor
*icur
,
4815 struct xfs_bmbt_irec
*got
,
4816 struct xfs_bmbt_irec
*del
)
4818 struct xfs_mount
*mp
= ip
->i_mount
;
4819 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, XFS_COW_FORK
);
4820 struct xfs_bmbt_irec
new;
4821 xfs_fileoff_t del_endoff
, got_endoff
;
4822 int state
= BMAP_COWFORK
;
4824 XFS_STATS_INC(mp
, xs_del_exlist
);
4826 del_endoff
= del
->br_startoff
+ del
->br_blockcount
;
4827 got_endoff
= got
->br_startoff
+ got
->br_blockcount
;
4829 ASSERT(del
->br_blockcount
> 0);
4830 ASSERT(got
->br_startoff
<= del
->br_startoff
);
4831 ASSERT(got_endoff
>= del_endoff
);
4832 ASSERT(!isnullstartblock(got
->br_startblock
));
4834 if (got
->br_startoff
== del
->br_startoff
)
4835 state
|= BMAP_LEFT_FILLING
;
4836 if (got_endoff
== del_endoff
)
4837 state
|= BMAP_RIGHT_FILLING
;
4839 switch (state
& (BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
)) {
4840 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
4842 * Matches the whole extent. Delete the entry.
4844 xfs_iext_remove(ip
, icur
, state
);
4845 xfs_iext_prev(ifp
, icur
);
4847 case BMAP_LEFT_FILLING
:
4849 * Deleting the first part of the extent.
4851 got
->br_startoff
= del_endoff
;
4852 got
->br_blockcount
-= del
->br_blockcount
;
4853 got
->br_startblock
= del
->br_startblock
+ del
->br_blockcount
;
4854 xfs_iext_update_extent(ip
, state
, icur
, got
);
4856 case BMAP_RIGHT_FILLING
:
4858 * Deleting the last part of the extent.
4860 got
->br_blockcount
-= del
->br_blockcount
;
4861 xfs_iext_update_extent(ip
, state
, icur
, got
);
4865 * Deleting the middle of the extent.
4867 got
->br_blockcount
= del
->br_startoff
- got
->br_startoff
;
4869 new.br_startoff
= del_endoff
;
4870 new.br_blockcount
= got_endoff
- del_endoff
;
4871 new.br_state
= got
->br_state
;
4872 new.br_startblock
= del
->br_startblock
+ del
->br_blockcount
;
4874 xfs_iext_update_extent(ip
, state
, icur
, got
);
4875 xfs_iext_next(ifp
, icur
);
4876 xfs_iext_insert(ip
, icur
, &new, state
);
4879 ip
->i_delayed_blks
-= del
->br_blockcount
;
4883 * Called by xfs_bmapi to update file extent records and the btree
4884 * after removing space.
4886 STATIC
int /* error */
4887 xfs_bmap_del_extent_real(
4888 xfs_inode_t
*ip
, /* incore inode pointer */
4889 xfs_trans_t
*tp
, /* current transaction pointer */
4890 struct xfs_iext_cursor
*icur
,
4891 struct xfs_defer_ops
*dfops
, /* list of extents to be freed */
4892 xfs_btree_cur_t
*cur
, /* if null, not a btree */
4893 xfs_bmbt_irec_t
*del
, /* data to remove from extents */
4894 int *logflagsp
, /* inode logging flags */
4895 int whichfork
, /* data or attr fork */
4896 int bflags
) /* bmapi flags */
4898 xfs_fsblock_t del_endblock
=0; /* first block past del */
4899 xfs_fileoff_t del_endoff
; /* first offset past del */
4900 int do_fx
; /* free extent at end of routine */
4901 int error
; /* error return value */
4902 int flags
= 0;/* inode logging flags */
4903 struct xfs_bmbt_irec got
; /* current extent entry */
4904 xfs_fileoff_t got_endoff
; /* first offset past got */
4905 int i
; /* temp state */
4906 xfs_ifork_t
*ifp
; /* inode fork pointer */
4907 xfs_mount_t
*mp
; /* mount structure */
4908 xfs_filblks_t nblks
; /* quota/sb block count */
4909 xfs_bmbt_irec_t
new; /* new record to be inserted */
4911 uint qfield
; /* quota field to update */
4912 int state
= xfs_bmap_fork_to_state(whichfork
);
4913 struct xfs_bmbt_irec old
;
4916 XFS_STATS_INC(mp
, xs_del_exlist
);
4918 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4919 ASSERT(del
->br_blockcount
> 0);
4920 xfs_iext_get_extent(ifp
, icur
, &got
);
4921 ASSERT(got
.br_startoff
<= del
->br_startoff
);
4922 del_endoff
= del
->br_startoff
+ del
->br_blockcount
;
4923 got_endoff
= got
.br_startoff
+ got
.br_blockcount
;
4924 ASSERT(got_endoff
>= del_endoff
);
4925 ASSERT(!isnullstartblock(got
.br_startblock
));
4930 * If it's the case where the directory code is running with no block
4931 * reservation, and the deleted block is in the middle of its extent,
4932 * and the resulting insert of an extent would cause transformation to
4933 * btree format, then reject it. The calling code will then swap blocks
4934 * around instead. We have to do this now, rather than waiting for the
4935 * conversion to btree format, since the transaction will be dirty then.
4937 if (tp
->t_blk_res
== 0 &&
4938 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
&&
4939 XFS_IFORK_NEXTENTS(ip
, whichfork
) >=
4940 XFS_IFORK_MAXEXT(ip
, whichfork
) &&
4941 del
->br_startoff
> got
.br_startoff
&& del_endoff
< got_endoff
)
4944 flags
= XFS_ILOG_CORE
;
4945 if (whichfork
== XFS_DATA_FORK
&& XFS_IS_REALTIME_INODE(ip
)) {
4950 bno
= div_u64_rem(del
->br_startblock
, mp
->m_sb
.sb_rextsize
,
4953 len
= div_u64_rem(del
->br_blockcount
, mp
->m_sb
.sb_rextsize
,
4957 error
= xfs_rtfree_extent(tp
, bno
, (xfs_extlen_t
)len
);
4961 nblks
= len
* mp
->m_sb
.sb_rextsize
;
4962 qfield
= XFS_TRANS_DQ_RTBCOUNT
;
4965 nblks
= del
->br_blockcount
;
4966 qfield
= XFS_TRANS_DQ_BCOUNT
;
4969 del_endblock
= del
->br_startblock
+ del
->br_blockcount
;
4971 error
= xfs_bmbt_lookup_eq(cur
, &got
, &i
);
4974 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
4977 if (got
.br_startoff
== del
->br_startoff
)
4978 state
|= BMAP_LEFT_FILLING
;
4979 if (got_endoff
== del_endoff
)
4980 state
|= BMAP_RIGHT_FILLING
;
4982 switch (state
& (BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
)) {
4983 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
4985 * Matches the whole extent. Delete the entry.
4987 xfs_iext_remove(ip
, icur
, state
);
4988 xfs_iext_prev(ifp
, icur
);
4989 XFS_IFORK_NEXT_SET(ip
, whichfork
,
4990 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
4991 flags
|= XFS_ILOG_CORE
;
4993 flags
|= xfs_ilog_fext(whichfork
);
4996 if ((error
= xfs_btree_delete(cur
, &i
)))
4998 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
5000 case BMAP_LEFT_FILLING
:
5002 * Deleting the first part of the extent.
5004 got
.br_startoff
= del_endoff
;
5005 got
.br_startblock
= del_endblock
;
5006 got
.br_blockcount
-= del
->br_blockcount
;
5007 xfs_iext_update_extent(ip
, state
, icur
, &got
);
5009 flags
|= xfs_ilog_fext(whichfork
);
5012 error
= xfs_bmbt_update(cur
, &got
);
5016 case BMAP_RIGHT_FILLING
:
5018 * Deleting the last part of the extent.
5020 got
.br_blockcount
-= del
->br_blockcount
;
5021 xfs_iext_update_extent(ip
, state
, icur
, &got
);
5023 flags
|= xfs_ilog_fext(whichfork
);
5026 error
= xfs_bmbt_update(cur
, &got
);
5032 * Deleting the middle of the extent.
5036 got
.br_blockcount
= del
->br_startoff
- got
.br_startoff
;
5037 xfs_iext_update_extent(ip
, state
, icur
, &got
);
5039 new.br_startoff
= del_endoff
;
5040 new.br_blockcount
= got_endoff
- del_endoff
;
5041 new.br_state
= got
.br_state
;
5042 new.br_startblock
= del_endblock
;
5044 flags
|= XFS_ILOG_CORE
;
5046 error
= xfs_bmbt_update(cur
, &got
);
5049 error
= xfs_btree_increment(cur
, 0, &i
);
5052 cur
->bc_rec
.b
= new;
5053 error
= xfs_btree_insert(cur
, &i
);
5054 if (error
&& error
!= -ENOSPC
)
5057 * If get no-space back from btree insert, it tried a
5058 * split, and we have a zero block reservation. Fix up
5059 * our state and return the error.
5061 if (error
== -ENOSPC
) {
5063 * Reset the cursor, don't trust it after any
5066 error
= xfs_bmbt_lookup_eq(cur
, &got
, &i
);
5069 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
5071 * Update the btree record back
5072 * to the original value.
5074 error
= xfs_bmbt_update(cur
, &old
);
5078 * Reset the extent record back
5079 * to the original value.
5081 xfs_iext_update_extent(ip
, state
, icur
, &old
);
5086 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
5088 flags
|= xfs_ilog_fext(whichfork
);
5089 XFS_IFORK_NEXT_SET(ip
, whichfork
,
5090 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
5091 xfs_iext_next(ifp
, icur
);
5092 xfs_iext_insert(ip
, icur
, &new, state
);
5096 /* remove reverse mapping */
5097 error
= xfs_rmap_unmap_extent(mp
, dfops
, ip
, whichfork
, del
);
5102 * If we need to, add to list of extents to delete.
5104 if (do_fx
&& !(bflags
& XFS_BMAPI_REMAP
)) {
5105 if (xfs_is_reflink_inode(ip
) && whichfork
== XFS_DATA_FORK
) {
5106 error
= xfs_refcount_decrease_extent(mp
, dfops
, del
);
5110 __xfs_bmap_add_free(mp
, dfops
, del
->br_startblock
,
5111 del
->br_blockcount
, NULL
,
5112 (bflags
& XFS_BMAPI_NODISCARD
) ||
5113 del
->br_state
== XFS_EXT_UNWRITTEN
);
5118 * Adjust inode # blocks in the file.
5121 ip
->i_d
.di_nblocks
-= nblks
;
5123 * Adjust quota data.
5125 if (qfield
&& !(bflags
& XFS_BMAPI_REMAP
))
5126 xfs_trans_mod_dquot_byino(tp
, ip
, qfield
, (long)-nblks
);
5134 * Unmap (remove) blocks from a file.
5135 * If nexts is nonzero then the number of extents to remove is limited to
5136 * that value. If not all extents in the block range can be removed then
5141 xfs_trans_t
*tp
, /* transaction pointer */
5142 struct xfs_inode
*ip
, /* incore inode */
5143 xfs_fileoff_t start
, /* first file offset deleted */
5144 xfs_filblks_t
*rlen
, /* i/o: amount remaining */
5145 int flags
, /* misc flags */
5146 xfs_extnum_t nexts
, /* number of extents max */
5147 xfs_fsblock_t
*firstblock
, /* first allocated block
5148 controls a.g. for allocs */
5149 struct xfs_defer_ops
*dfops
) /* i/o: deferred updates */
5151 xfs_btree_cur_t
*cur
; /* bmap btree cursor */
5152 xfs_bmbt_irec_t del
; /* extent being deleted */
5153 int error
; /* error return value */
5154 xfs_extnum_t extno
; /* extent number in list */
5155 xfs_bmbt_irec_t got
; /* current extent record */
5156 xfs_ifork_t
*ifp
; /* inode fork pointer */
5157 int isrt
; /* freeing in rt area */
5158 int logflags
; /* transaction logging flags */
5159 xfs_extlen_t mod
; /* rt extent offset */
5160 xfs_mount_t
*mp
; /* mount structure */
5161 int tmp_logflags
; /* partial logging flags */
5162 int wasdel
; /* was a delayed alloc extent */
5163 int whichfork
; /* data or attribute fork */
5165 xfs_filblks_t len
= *rlen
; /* length to unmap in file */
5166 xfs_fileoff_t max_len
;
5167 xfs_agnumber_t prev_agno
= NULLAGNUMBER
, agno
;
5169 struct xfs_iext_cursor icur
;
5172 trace_xfs_bunmap(ip
, start
, len
, flags
, _RET_IP_
);
5174 whichfork
= xfs_bmapi_whichfork(flags
);
5175 ASSERT(whichfork
!= XFS_COW_FORK
);
5176 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5178 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
5179 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)) {
5180 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW
,
5182 return -EFSCORRUPTED
;
5185 if (XFS_FORCED_SHUTDOWN(mp
))
5188 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
5193 * Guesstimate how many blocks we can unmap without running the risk of
5194 * blowing out the transaction with a mix of EFIs and reflink
5197 if (tp
&& xfs_is_reflink_inode(ip
) && whichfork
== XFS_DATA_FORK
)
5198 max_len
= min(len
, xfs_refcount_max_unmap(tp
->t_log_res
));
5202 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
5203 (error
= xfs_iread_extents(tp
, ip
, whichfork
)))
5205 if (xfs_iext_count(ifp
) == 0) {
5209 XFS_STATS_INC(mp
, xs_blk_unmap
);
5210 isrt
= (whichfork
== XFS_DATA_FORK
) && XFS_IS_REALTIME_INODE(ip
);
5213 if (!xfs_iext_lookup_extent_before(ip
, ifp
, &end
, &icur
, &got
)) {
5220 if (ifp
->if_flags
& XFS_IFBROOT
) {
5221 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
);
5222 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
5223 cur
->bc_private
.b
.firstblock
= *firstblock
;
5224 cur
->bc_private
.b
.dfops
= dfops
;
5225 cur
->bc_private
.b
.flags
= 0;
5231 * Synchronize by locking the bitmap inode.
5233 xfs_ilock(mp
->m_rbmip
, XFS_ILOCK_EXCL
|XFS_ILOCK_RTBITMAP
);
5234 xfs_trans_ijoin(tp
, mp
->m_rbmip
, XFS_ILOCK_EXCL
);
5235 xfs_ilock(mp
->m_rsumip
, XFS_ILOCK_EXCL
|XFS_ILOCK_RTSUM
);
5236 xfs_trans_ijoin(tp
, mp
->m_rsumip
, XFS_ILOCK_EXCL
);
5240 while (end
!= (xfs_fileoff_t
)-1 && end
>= start
&&
5241 (nexts
== 0 || extno
< nexts
) && max_len
> 0) {
5243 * Is the found extent after a hole in which end lives?
5244 * Just back up to the previous extent, if so.
5246 if (got
.br_startoff
> end
&&
5247 !xfs_iext_prev_extent(ifp
, &icur
, &got
)) {
5252 * Is the last block of this extent before the range
5253 * we're supposed to delete? If so, we're done.
5255 end
= XFS_FILEOFF_MIN(end
,
5256 got
.br_startoff
+ got
.br_blockcount
- 1);
5260 * Then deal with the (possibly delayed) allocated space
5264 wasdel
= isnullstartblock(del
.br_startblock
);
5267 * Make sure we don't touch multiple AGF headers out of order
5268 * in a single transaction, as that could cause AB-BA deadlocks.
5271 agno
= XFS_FSB_TO_AGNO(mp
, del
.br_startblock
);
5272 if (prev_agno
!= NULLAGNUMBER
&& prev_agno
> agno
)
5276 if (got
.br_startoff
< start
) {
5277 del
.br_startoff
= start
;
5278 del
.br_blockcount
-= start
- got
.br_startoff
;
5280 del
.br_startblock
+= start
- got
.br_startoff
;
5282 if (del
.br_startoff
+ del
.br_blockcount
> end
+ 1)
5283 del
.br_blockcount
= end
+ 1 - del
.br_startoff
;
5285 /* How much can we safely unmap? */
5286 if (max_len
< del
.br_blockcount
) {
5287 del
.br_startoff
+= del
.br_blockcount
- max_len
;
5289 del
.br_startblock
+= del
.br_blockcount
- max_len
;
5290 del
.br_blockcount
= max_len
;
5296 sum
= del
.br_startblock
+ del
.br_blockcount
;
5297 div_u64_rem(sum
, mp
->m_sb
.sb_rextsize
, &mod
);
5300 * Realtime extent not lined up at the end.
5301 * The extent could have been split into written
5302 * and unwritten pieces, or we could just be
5303 * unmapping part of it. But we can't really
5304 * get rid of part of a realtime extent.
5306 if (del
.br_state
== XFS_EXT_UNWRITTEN
||
5307 !xfs_sb_version_hasextflgbit(&mp
->m_sb
)) {
5309 * This piece is unwritten, or we're not
5310 * using unwritten extents. Skip over it.
5313 end
-= mod
> del
.br_blockcount
?
5314 del
.br_blockcount
: mod
;
5315 if (end
< got
.br_startoff
&&
5316 !xfs_iext_prev_extent(ifp
, &icur
, &got
)) {
5323 * It's written, turn it unwritten.
5324 * This is better than zeroing it.
5326 ASSERT(del
.br_state
== XFS_EXT_NORM
);
5327 ASSERT(tp
->t_blk_res
> 0);
5329 * If this spans a realtime extent boundary,
5330 * chop it back to the start of the one we end at.
5332 if (del
.br_blockcount
> mod
) {
5333 del
.br_startoff
+= del
.br_blockcount
- mod
;
5334 del
.br_startblock
+= del
.br_blockcount
- mod
;
5335 del
.br_blockcount
= mod
;
5337 del
.br_state
= XFS_EXT_UNWRITTEN
;
5338 error
= xfs_bmap_add_extent_unwritten_real(tp
, ip
,
5339 whichfork
, &icur
, &cur
, &del
,
5340 firstblock
, dfops
, &logflags
);
5345 div_u64_rem(del
.br_startblock
, mp
->m_sb
.sb_rextsize
, &mod
);
5348 * Realtime extent is lined up at the end but not
5349 * at the front. We'll get rid of full extents if
5352 mod
= mp
->m_sb
.sb_rextsize
- mod
;
5353 if (del
.br_blockcount
> mod
) {
5354 del
.br_blockcount
-= mod
;
5355 del
.br_startoff
+= mod
;
5356 del
.br_startblock
+= mod
;
5357 } else if ((del
.br_startoff
== start
&&
5358 (del
.br_state
== XFS_EXT_UNWRITTEN
||
5359 tp
->t_blk_res
== 0)) ||
5360 !xfs_sb_version_hasextflgbit(&mp
->m_sb
)) {
5362 * Can't make it unwritten. There isn't
5363 * a full extent here so just skip it.
5365 ASSERT(end
>= del
.br_blockcount
);
5366 end
-= del
.br_blockcount
;
5367 if (got
.br_startoff
> end
&&
5368 !xfs_iext_prev_extent(ifp
, &icur
, &got
)) {
5373 } else if (del
.br_state
== XFS_EXT_UNWRITTEN
) {
5374 struct xfs_bmbt_irec prev
;
5377 * This one is already unwritten.
5378 * It must have a written left neighbor.
5379 * Unwrite the killed part of that one and
5382 if (!xfs_iext_prev_extent(ifp
, &icur
, &prev
))
5384 ASSERT(prev
.br_state
== XFS_EXT_NORM
);
5385 ASSERT(!isnullstartblock(prev
.br_startblock
));
5386 ASSERT(del
.br_startblock
==
5387 prev
.br_startblock
+ prev
.br_blockcount
);
5388 if (prev
.br_startoff
< start
) {
5389 mod
= start
- prev
.br_startoff
;
5390 prev
.br_blockcount
-= mod
;
5391 prev
.br_startblock
+= mod
;
5392 prev
.br_startoff
= start
;
5394 prev
.br_state
= XFS_EXT_UNWRITTEN
;
5395 error
= xfs_bmap_add_extent_unwritten_real(tp
,
5396 ip
, whichfork
, &icur
, &cur
,
5397 &prev
, firstblock
, dfops
,
5403 ASSERT(del
.br_state
== XFS_EXT_NORM
);
5404 del
.br_state
= XFS_EXT_UNWRITTEN
;
5405 error
= xfs_bmap_add_extent_unwritten_real(tp
,
5406 ip
, whichfork
, &icur
, &cur
,
5407 &del
, firstblock
, dfops
,
5417 error
= xfs_bmap_del_extent_delay(ip
, whichfork
, &icur
,
5420 error
= xfs_bmap_del_extent_real(ip
, tp
, &icur
, dfops
,
5421 cur
, &del
, &tmp_logflags
, whichfork
,
5423 logflags
|= tmp_logflags
;
5429 max_len
-= del
.br_blockcount
;
5430 end
= del
.br_startoff
- 1;
5433 * If not done go on to the next (previous) record.
5435 if (end
!= (xfs_fileoff_t
)-1 && end
>= start
) {
5436 if (!xfs_iext_get_extent(ifp
, &icur
, &got
) ||
5437 (got
.br_startoff
> end
&&
5438 !xfs_iext_prev_extent(ifp
, &icur
, &got
))) {
5445 if (done
|| end
== (xfs_fileoff_t
)-1 || end
< start
)
5448 *rlen
= end
- start
+ 1;
5451 * Convert to a btree if necessary.
5453 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
5454 ASSERT(cur
== NULL
);
5455 error
= xfs_bmap_extents_to_btree(tp
, ip
, firstblock
, dfops
,
5456 &cur
, 0, &tmp_logflags
, whichfork
);
5457 logflags
|= tmp_logflags
;
5462 * transform from btree to extents, give it cur
5464 else if (xfs_bmap_wants_extents(ip
, whichfork
)) {
5465 ASSERT(cur
!= NULL
);
5466 error
= xfs_bmap_btree_to_extents(tp
, ip
, cur
, &tmp_logflags
,
5468 logflags
|= tmp_logflags
;
5473 * transform from extents to local?
5478 * Log everything. Do this after conversion, there's no point in
5479 * logging the extent records if we've converted to btree format.
5481 if ((logflags
& xfs_ilog_fext(whichfork
)) &&
5482 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
5483 logflags
&= ~xfs_ilog_fext(whichfork
);
5484 else if ((logflags
& xfs_ilog_fbroot(whichfork
)) &&
5485 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)
5486 logflags
&= ~xfs_ilog_fbroot(whichfork
);
5488 * Log inode even in the error case, if the transaction
5489 * is dirty we'll need to shut down the filesystem.
5492 xfs_trans_log_inode(tp
, ip
, logflags
);
5495 *firstblock
= cur
->bc_private
.b
.firstblock
;
5496 cur
->bc_private
.b
.allocated
= 0;
5498 xfs_btree_del_cursor(cur
,
5499 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
5504 /* Unmap a range of a file. */
5508 struct xfs_inode
*ip
,
5513 xfs_fsblock_t
*firstblock
,
5514 struct xfs_defer_ops
*dfops
,
5519 error
= __xfs_bunmapi(tp
, ip
, bno
, &len
, flags
, nexts
, firstblock
,
5526 * Determine whether an extent shift can be accomplished by a merge with the
5527 * extent that precedes the target hole of the shift.
5531 struct xfs_bmbt_irec
*left
, /* preceding extent */
5532 struct xfs_bmbt_irec
*got
, /* current extent to shift */
5533 xfs_fileoff_t shift
) /* shift fsb */
5535 xfs_fileoff_t startoff
;
5537 startoff
= got
->br_startoff
- shift
;
5540 * The extent, once shifted, must be adjacent in-file and on-disk with
5541 * the preceding extent.
5543 if ((left
->br_startoff
+ left
->br_blockcount
!= startoff
) ||
5544 (left
->br_startblock
+ left
->br_blockcount
!= got
->br_startblock
) ||
5545 (left
->br_state
!= got
->br_state
) ||
5546 (left
->br_blockcount
+ got
->br_blockcount
> MAXEXTLEN
))
5553 * A bmap extent shift adjusts the file offset of an extent to fill a preceding
5554 * hole in the file. If an extent shift would result in the extent being fully
5555 * adjacent to the extent that currently precedes the hole, we can merge with
5556 * the preceding extent rather than do the shift.
5558 * This function assumes the caller has verified a shift-by-merge is possible
5559 * with the provided extents via xfs_bmse_can_merge().
5563 struct xfs_inode
*ip
,
5565 xfs_fileoff_t shift
, /* shift fsb */
5566 struct xfs_iext_cursor
*icur
,
5567 struct xfs_bmbt_irec
*got
, /* extent to shift */
5568 struct xfs_bmbt_irec
*left
, /* preceding extent */
5569 struct xfs_btree_cur
*cur
,
5570 int *logflags
, /* output */
5571 struct xfs_defer_ops
*dfops
)
5573 struct xfs_bmbt_irec
new;
5574 xfs_filblks_t blockcount
;
5576 struct xfs_mount
*mp
= ip
->i_mount
;
5578 blockcount
= left
->br_blockcount
+ got
->br_blockcount
;
5580 ASSERT(xfs_isilocked(ip
, XFS_IOLOCK_EXCL
));
5581 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
5582 ASSERT(xfs_bmse_can_merge(left
, got
, shift
));
5585 new.br_blockcount
= blockcount
;
5588 * Update the on-disk extent count, the btree if necessary and log the
5591 XFS_IFORK_NEXT_SET(ip
, whichfork
,
5592 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
5593 *logflags
|= XFS_ILOG_CORE
;
5595 *logflags
|= XFS_ILOG_DEXT
;
5599 /* lookup and remove the extent to merge */
5600 error
= xfs_bmbt_lookup_eq(cur
, got
, &i
);
5603 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
5605 error
= xfs_btree_delete(cur
, &i
);
5608 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
5610 /* lookup and update size of the previous extent */
5611 error
= xfs_bmbt_lookup_eq(cur
, left
, &i
);
5614 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
5616 error
= xfs_bmbt_update(cur
, &new);
5621 xfs_iext_remove(ip
, icur
, 0);
5622 xfs_iext_prev(XFS_IFORK_PTR(ip
, whichfork
), icur
);
5623 xfs_iext_update_extent(ip
, xfs_bmap_fork_to_state(whichfork
), icur
,
5626 /* update reverse mapping. rmap functions merge the rmaps for us */
5627 error
= xfs_rmap_unmap_extent(mp
, dfops
, ip
, whichfork
, got
);
5630 memcpy(&new, got
, sizeof(new));
5631 new.br_startoff
= left
->br_startoff
+ left
->br_blockcount
;
5632 return xfs_rmap_map_extent(mp
, dfops
, ip
, whichfork
, &new);
5636 xfs_bmap_shift_update_extent(
5637 struct xfs_inode
*ip
,
5639 struct xfs_iext_cursor
*icur
,
5640 struct xfs_bmbt_irec
*got
,
5641 struct xfs_btree_cur
*cur
,
5643 struct xfs_defer_ops
*dfops
,
5644 xfs_fileoff_t startoff
)
5646 struct xfs_mount
*mp
= ip
->i_mount
;
5647 struct xfs_bmbt_irec prev
= *got
;
5650 *logflags
|= XFS_ILOG_CORE
;
5652 got
->br_startoff
= startoff
;
5655 error
= xfs_bmbt_lookup_eq(cur
, &prev
, &i
);
5658 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
5660 error
= xfs_bmbt_update(cur
, got
);
5664 *logflags
|= XFS_ILOG_DEXT
;
5667 xfs_iext_update_extent(ip
, xfs_bmap_fork_to_state(whichfork
), icur
,
5670 /* update reverse mapping */
5671 error
= xfs_rmap_unmap_extent(mp
, dfops
, ip
, whichfork
, &prev
);
5674 return xfs_rmap_map_extent(mp
, dfops
, ip
, whichfork
, got
);
5678 xfs_bmap_collapse_extents(
5679 struct xfs_trans
*tp
,
5680 struct xfs_inode
*ip
,
5681 xfs_fileoff_t
*next_fsb
,
5682 xfs_fileoff_t offset_shift_fsb
,
5684 xfs_fsblock_t
*firstblock
,
5685 struct xfs_defer_ops
*dfops
)
5687 int whichfork
= XFS_DATA_FORK
;
5688 struct xfs_mount
*mp
= ip
->i_mount
;
5689 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5690 struct xfs_btree_cur
*cur
= NULL
;
5691 struct xfs_bmbt_irec got
, prev
;
5692 struct xfs_iext_cursor icur
;
5693 xfs_fileoff_t new_startoff
;
5697 if (unlikely(XFS_TEST_ERROR(
5698 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
5699 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
5700 mp
, XFS_ERRTAG_BMAPIFORMAT
))) {
5701 XFS_ERROR_REPORT(__func__
, XFS_ERRLEVEL_LOW
, mp
);
5702 return -EFSCORRUPTED
;
5705 if (XFS_FORCED_SHUTDOWN(mp
))
5708 ASSERT(xfs_isilocked(ip
, XFS_IOLOCK_EXCL
| XFS_ILOCK_EXCL
));
5710 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
5711 error
= xfs_iread_extents(tp
, ip
, whichfork
);
5716 if (ifp
->if_flags
& XFS_IFBROOT
) {
5717 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
5718 cur
->bc_private
.b
.firstblock
= *firstblock
;
5719 cur
->bc_private
.b
.dfops
= dfops
;
5720 cur
->bc_private
.b
.flags
= 0;
5723 if (!xfs_iext_lookup_extent(ip
, ifp
, *next_fsb
, &icur
, &got
)) {
5727 XFS_WANT_CORRUPTED_GOTO(mp
, !isnullstartblock(got
.br_startblock
),
5730 new_startoff
= got
.br_startoff
- offset_shift_fsb
;
5731 if (xfs_iext_peek_prev_extent(ifp
, &icur
, &prev
)) {
5732 if (new_startoff
< prev
.br_startoff
+ prev
.br_blockcount
) {
5737 if (xfs_bmse_can_merge(&prev
, &got
, offset_shift_fsb
)) {
5738 error
= xfs_bmse_merge(ip
, whichfork
, offset_shift_fsb
,
5739 &icur
, &got
, &prev
, cur
, &logflags
,
5746 if (got
.br_startoff
< offset_shift_fsb
) {
5752 error
= xfs_bmap_shift_update_extent(ip
, whichfork
, &icur
, &got
, cur
,
5753 &logflags
, dfops
, new_startoff
);
5758 if (!xfs_iext_next_extent(ifp
, &icur
, &got
)) {
5763 *next_fsb
= got
.br_startoff
;
5766 xfs_btree_del_cursor(cur
,
5767 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
5769 xfs_trans_log_inode(tp
, ip
, logflags
);
5773 /* Make sure we won't be right-shifting an extent past the maximum bound. */
5775 xfs_bmap_can_insert_extents(
5776 struct xfs_inode
*ip
,
5778 xfs_fileoff_t shift
)
5780 struct xfs_bmbt_irec got
;
5784 ASSERT(xfs_isilocked(ip
, XFS_IOLOCK_EXCL
));
5786 if (XFS_FORCED_SHUTDOWN(ip
->i_mount
))
5789 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
5790 error
= xfs_bmap_last_extent(NULL
, ip
, XFS_DATA_FORK
, &got
, &is_empty
);
5791 if (!error
&& !is_empty
&& got
.br_startoff
>= off
&&
5792 ((got
.br_startoff
+ shift
) & BMBT_STARTOFF_MASK
) < got
.br_startoff
)
5794 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
5800 xfs_bmap_insert_extents(
5801 struct xfs_trans
*tp
,
5802 struct xfs_inode
*ip
,
5803 xfs_fileoff_t
*next_fsb
,
5804 xfs_fileoff_t offset_shift_fsb
,
5806 xfs_fileoff_t stop_fsb
,
5807 xfs_fsblock_t
*firstblock
,
5808 struct xfs_defer_ops
*dfops
)
5810 int whichfork
= XFS_DATA_FORK
;
5811 struct xfs_mount
*mp
= ip
->i_mount
;
5812 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5813 struct xfs_btree_cur
*cur
= NULL
;
5814 struct xfs_bmbt_irec got
, next
;
5815 struct xfs_iext_cursor icur
;
5816 xfs_fileoff_t new_startoff
;
5820 if (unlikely(XFS_TEST_ERROR(
5821 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
5822 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
5823 mp
, XFS_ERRTAG_BMAPIFORMAT
))) {
5824 XFS_ERROR_REPORT(__func__
, XFS_ERRLEVEL_LOW
, mp
);
5825 return -EFSCORRUPTED
;
5828 if (XFS_FORCED_SHUTDOWN(mp
))
5831 ASSERT(xfs_isilocked(ip
, XFS_IOLOCK_EXCL
| XFS_ILOCK_EXCL
));
5833 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
5834 error
= xfs_iread_extents(tp
, ip
, whichfork
);
5839 if (ifp
->if_flags
& XFS_IFBROOT
) {
5840 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
5841 cur
->bc_private
.b
.firstblock
= *firstblock
;
5842 cur
->bc_private
.b
.dfops
= dfops
;
5843 cur
->bc_private
.b
.flags
= 0;
5846 if (*next_fsb
== NULLFSBLOCK
) {
5847 xfs_iext_last(ifp
, &icur
);
5848 if (!xfs_iext_get_extent(ifp
, &icur
, &got
) ||
5849 stop_fsb
> got
.br_startoff
) {
5854 if (!xfs_iext_lookup_extent(ip
, ifp
, *next_fsb
, &icur
, &got
)) {
5859 XFS_WANT_CORRUPTED_GOTO(mp
, !isnullstartblock(got
.br_startblock
),
5862 if (stop_fsb
>= got
.br_startoff
+ got
.br_blockcount
) {
5867 new_startoff
= got
.br_startoff
+ offset_shift_fsb
;
5868 if (xfs_iext_peek_next_extent(ifp
, &icur
, &next
)) {
5869 if (new_startoff
+ got
.br_blockcount
> next
.br_startoff
) {
5875 * Unlike a left shift (which involves a hole punch), a right
5876 * shift does not modify extent neighbors in any way. We should
5877 * never find mergeable extents in this scenario. Check anyways
5878 * and warn if we encounter two extents that could be one.
5880 if (xfs_bmse_can_merge(&got
, &next
, offset_shift_fsb
))
5884 error
= xfs_bmap_shift_update_extent(ip
, whichfork
, &icur
, &got
, cur
,
5885 &logflags
, dfops
, new_startoff
);
5889 if (!xfs_iext_prev_extent(ifp
, &icur
, &got
) ||
5890 stop_fsb
>= got
.br_startoff
+ got
.br_blockcount
) {
5895 *next_fsb
= got
.br_startoff
;
5898 xfs_btree_del_cursor(cur
,
5899 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
5901 xfs_trans_log_inode(tp
, ip
, logflags
);
5906 * Splits an extent into two extents at split_fsb block such that it is the
5907 * first block of the current_ext. @ext is a target extent to be split.
5908 * @split_fsb is a block where the extents is split. If split_fsb lies in a
5909 * hole or the first block of extents, just return 0.
5912 xfs_bmap_split_extent_at(
5913 struct xfs_trans
*tp
,
5914 struct xfs_inode
*ip
,
5915 xfs_fileoff_t split_fsb
,
5916 xfs_fsblock_t
*firstfsb
,
5917 struct xfs_defer_ops
*dfops
)
5919 int whichfork
= XFS_DATA_FORK
;
5920 struct xfs_btree_cur
*cur
= NULL
;
5921 struct xfs_bmbt_irec got
;
5922 struct xfs_bmbt_irec
new; /* split extent */
5923 struct xfs_mount
*mp
= ip
->i_mount
;
5924 struct xfs_ifork
*ifp
;
5925 xfs_fsblock_t gotblkcnt
; /* new block count for got */
5926 struct xfs_iext_cursor icur
;
5931 if (unlikely(XFS_TEST_ERROR(
5932 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
5933 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
5934 mp
, XFS_ERRTAG_BMAPIFORMAT
))) {
5935 XFS_ERROR_REPORT("xfs_bmap_split_extent_at",
5936 XFS_ERRLEVEL_LOW
, mp
);
5937 return -EFSCORRUPTED
;
5940 if (XFS_FORCED_SHUTDOWN(mp
))
5943 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5944 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
5945 /* Read in all the extents */
5946 error
= xfs_iread_extents(tp
, ip
, whichfork
);
5952 * If there are not extents, or split_fsb lies in a hole we are done.
5954 if (!xfs_iext_lookup_extent(ip
, ifp
, split_fsb
, &icur
, &got
) ||
5955 got
.br_startoff
>= split_fsb
)
5958 gotblkcnt
= split_fsb
- got
.br_startoff
;
5959 new.br_startoff
= split_fsb
;
5960 new.br_startblock
= got
.br_startblock
+ gotblkcnt
;
5961 new.br_blockcount
= got
.br_blockcount
- gotblkcnt
;
5962 new.br_state
= got
.br_state
;
5964 if (ifp
->if_flags
& XFS_IFBROOT
) {
5965 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
5966 cur
->bc_private
.b
.firstblock
= *firstfsb
;
5967 cur
->bc_private
.b
.dfops
= dfops
;
5968 cur
->bc_private
.b
.flags
= 0;
5969 error
= xfs_bmbt_lookup_eq(cur
, &got
, &i
);
5972 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, del_cursor
);
5975 got
.br_blockcount
= gotblkcnt
;
5976 xfs_iext_update_extent(ip
, xfs_bmap_fork_to_state(whichfork
), &icur
,
5979 logflags
= XFS_ILOG_CORE
;
5981 error
= xfs_bmbt_update(cur
, &got
);
5985 logflags
|= XFS_ILOG_DEXT
;
5987 /* Add new extent */
5988 xfs_iext_next(ifp
, &icur
);
5989 xfs_iext_insert(ip
, &icur
, &new, 0);
5990 XFS_IFORK_NEXT_SET(ip
, whichfork
,
5991 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
5994 error
= xfs_bmbt_lookup_eq(cur
, &new, &i
);
5997 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, del_cursor
);
5998 error
= xfs_btree_insert(cur
, &i
);
6001 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, del_cursor
);
6005 * Convert to a btree if necessary.
6007 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
6008 int tmp_logflags
; /* partial log flag return val */
6010 ASSERT(cur
== NULL
);
6011 error
= xfs_bmap_extents_to_btree(tp
, ip
, firstfsb
, dfops
,
6012 &cur
, 0, &tmp_logflags
, whichfork
);
6013 logflags
|= tmp_logflags
;
6018 cur
->bc_private
.b
.allocated
= 0;
6019 xfs_btree_del_cursor(cur
,
6020 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
6024 xfs_trans_log_inode(tp
, ip
, logflags
);
6029 xfs_bmap_split_extent(
6030 struct xfs_inode
*ip
,
6031 xfs_fileoff_t split_fsb
)
6033 struct xfs_mount
*mp
= ip
->i_mount
;
6034 struct xfs_trans
*tp
;
6035 struct xfs_defer_ops dfops
;
6036 xfs_fsblock_t firstfsb
;
6039 error
= xfs_trans_alloc(mp
, &M_RES(mp
)->tr_write
,
6040 XFS_DIOSTRAT_SPACE_RES(mp
, 0), 0, 0, &tp
);
6044 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
6045 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
);
6047 xfs_defer_init(&dfops
, &firstfsb
);
6049 error
= xfs_bmap_split_extent_at(tp
, ip
, split_fsb
,
6054 error
= xfs_defer_finish(&tp
, &dfops
);
6058 return xfs_trans_commit(tp
);
6061 xfs_defer_cancel(&dfops
);
6062 xfs_trans_cancel(tp
);
6066 /* Deferred mapping is only for real extents in the data fork. */
6068 xfs_bmap_is_update_needed(
6069 struct xfs_bmbt_irec
*bmap
)
6071 return bmap
->br_startblock
!= HOLESTARTBLOCK
&&
6072 bmap
->br_startblock
!= DELAYSTARTBLOCK
;
6075 /* Record a bmap intent. */
6078 struct xfs_mount
*mp
,
6079 struct xfs_defer_ops
*dfops
,
6080 enum xfs_bmap_intent_type type
,
6081 struct xfs_inode
*ip
,
6083 struct xfs_bmbt_irec
*bmap
)
6086 struct xfs_bmap_intent
*bi
;
6088 trace_xfs_bmap_defer(mp
,
6089 XFS_FSB_TO_AGNO(mp
, bmap
->br_startblock
),
6091 XFS_FSB_TO_AGBNO(mp
, bmap
->br_startblock
),
6092 ip
->i_ino
, whichfork
,
6094 bmap
->br_blockcount
,
6097 bi
= kmem_alloc(sizeof(struct xfs_bmap_intent
), KM_SLEEP
| KM_NOFS
);
6098 INIT_LIST_HEAD(&bi
->bi_list
);
6101 bi
->bi_whichfork
= whichfork
;
6102 bi
->bi_bmap
= *bmap
;
6104 error
= xfs_defer_ijoin(dfops
, bi
->bi_owner
);
6110 xfs_defer_add(dfops
, XFS_DEFER_OPS_TYPE_BMAP
, &bi
->bi_list
);
6114 /* Map an extent into a file. */
6116 xfs_bmap_map_extent(
6117 struct xfs_mount
*mp
,
6118 struct xfs_defer_ops
*dfops
,
6119 struct xfs_inode
*ip
,
6120 struct xfs_bmbt_irec
*PREV
)
6122 if (!xfs_bmap_is_update_needed(PREV
))
6125 return __xfs_bmap_add(mp
, dfops
, XFS_BMAP_MAP
, ip
,
6126 XFS_DATA_FORK
, PREV
);
6129 /* Unmap an extent out of a file. */
6131 xfs_bmap_unmap_extent(
6132 struct xfs_mount
*mp
,
6133 struct xfs_defer_ops
*dfops
,
6134 struct xfs_inode
*ip
,
6135 struct xfs_bmbt_irec
*PREV
)
6137 if (!xfs_bmap_is_update_needed(PREV
))
6140 return __xfs_bmap_add(mp
, dfops
, XFS_BMAP_UNMAP
, ip
,
6141 XFS_DATA_FORK
, PREV
);
6145 * Process one of the deferred bmap operations. We pass back the
6146 * btree cursor to maintain our lock on the bmapbt between calls.
6149 xfs_bmap_finish_one(
6150 struct xfs_trans
*tp
,
6151 struct xfs_defer_ops
*dfops
,
6152 struct xfs_inode
*ip
,
6153 enum xfs_bmap_intent_type type
,
6155 xfs_fileoff_t startoff
,
6156 xfs_fsblock_t startblock
,
6157 xfs_filblks_t
*blockcount
,
6160 xfs_fsblock_t firstfsb
;
6164 * firstfsb is tied to the transaction lifetime and is used to
6165 * ensure correct AG locking order and schedule work item
6166 * continuations. XFS_BUI_MAX_FAST_EXTENTS (== 1) restricts us
6167 * to only making one bmap call per transaction, so it should
6168 * be safe to have it as a local variable here.
6170 firstfsb
= NULLFSBLOCK
;
6172 trace_xfs_bmap_deferred(tp
->t_mountp
,
6173 XFS_FSB_TO_AGNO(tp
->t_mountp
, startblock
), type
,
6174 XFS_FSB_TO_AGBNO(tp
->t_mountp
, startblock
),
6175 ip
->i_ino
, whichfork
, startoff
, *blockcount
, state
);
6177 if (WARN_ON_ONCE(whichfork
!= XFS_DATA_FORK
))
6178 return -EFSCORRUPTED
;
6180 if (XFS_TEST_ERROR(false, tp
->t_mountp
,
6181 XFS_ERRTAG_BMAP_FINISH_ONE
))
6186 error
= xfs_bmapi_remap(tp
, ip
, startoff
, *blockcount
,
6187 startblock
, dfops
, 0);
6190 case XFS_BMAP_UNMAP
:
6191 error
= __xfs_bunmapi(tp
, ip
, startoff
, blockcount
,
6192 XFS_BMAPI_REMAP
, 1, &firstfsb
, dfops
);
6196 error
= -EFSCORRUPTED
;
6202 /* Check that an inode's extent does not have invalid flags or bad ranges. */
6204 xfs_bmap_validate_extent(
6205 struct xfs_inode
*ip
,
6207 struct xfs_bmbt_irec
*irec
)
6209 struct xfs_mount
*mp
= ip
->i_mount
;
6210 xfs_fsblock_t endfsb
;
6213 isrt
= XFS_IS_REALTIME_INODE(ip
);
6214 endfsb
= irec
->br_startblock
+ irec
->br_blockcount
- 1;
6216 if (!xfs_verify_rtbno(mp
, irec
->br_startblock
))
6217 return __this_address
;
6218 if (!xfs_verify_rtbno(mp
, endfsb
))
6219 return __this_address
;
6221 if (!xfs_verify_fsbno(mp
, irec
->br_startblock
))
6222 return __this_address
;
6223 if (!xfs_verify_fsbno(mp
, endfsb
))
6224 return __this_address
;
6225 if (XFS_FSB_TO_AGNO(mp
, irec
->br_startblock
) !=
6226 XFS_FSB_TO_AGNO(mp
, endfsb
))
6227 return __this_address
;
6229 if (irec
->br_state
!= XFS_EXT_NORM
) {
6230 if (whichfork
!= XFS_DATA_FORK
)
6231 return __this_address
;
6232 if (!xfs_sb_version_hasextflgbit(&mp
->m_sb
))
6233 return __this_address
;