2 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 #include "libxfs_priv.h"
20 #include "xfs_shared.h"
21 #include "xfs_format.h"
22 #include "xfs_log_format.h"
23 #include "xfs_trans_resv.h"
26 #include "xfs_mount.h"
27 #include "xfs_defer.h"
28 #include "xfs_da_format.h"
29 #include "xfs_da_btree.h"
31 #include "xfs_inode.h"
32 #include "xfs_btree.h"
33 #include "xfs_trans.h"
34 #include "xfs_alloc.h"
36 #include "xfs_bmap_btree.h"
37 #include "xfs_trans_space.h"
38 #include "xfs_trace.h"
39 #include "xfs_attr_leaf.h"
40 #include "xfs_quota_defs.h"
42 #include "xfs_ag_resv.h"
43 #include "xfs_refcount.h"
46 kmem_zone_t
*xfs_bmap_free_item_zone
;
49 * Miscellaneous helper functions
53 * Compute and fill in the value of the maximum depth of a bmap btree
54 * in this filesystem. Done once, during mount.
57 xfs_bmap_compute_maxlevels(
58 xfs_mount_t
*mp
, /* file system mount structure */
59 int whichfork
) /* data or attr fork */
61 int level
; /* btree level */
62 uint maxblocks
; /* max blocks at this level */
63 uint maxleafents
; /* max leaf entries possible */
64 int maxrootrecs
; /* max records in root block */
65 int minleafrecs
; /* min records in leaf block */
66 int minnoderecs
; /* min records in node block */
67 int sz
; /* root block size */
70 * The maximum number of extents in a file, hence the maximum
71 * number of leaf entries, is controlled by the type of di_nextents
72 * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
73 * (a signed 16-bit number, xfs_aextnum_t).
75 * Note that we can no longer assume that if we are in ATTR1 that
76 * the fork offset of all the inodes will be
77 * (xfs_default_attroffset(ip) >> 3) because we could have mounted
78 * with ATTR2 and then mounted back with ATTR1, keeping the
79 * di_forkoff's fixed but probably at various positions. Therefore,
80 * for both ATTR1 and ATTR2 we have to assume the worst case scenario
81 * of a minimum size available.
83 if (whichfork
== XFS_DATA_FORK
) {
84 maxleafents
= MAXEXTNUM
;
85 sz
= XFS_BMDR_SPACE_CALC(MINDBTPTRS
);
87 maxleafents
= MAXAEXTNUM
;
88 sz
= XFS_BMDR_SPACE_CALC(MINABTPTRS
);
90 maxrootrecs
= xfs_bmdr_maxrecs(sz
, 0);
91 minleafrecs
= mp
->m_bmap_dmnr
[0];
92 minnoderecs
= mp
->m_bmap_dmnr
[1];
93 maxblocks
= (maxleafents
+ minleafrecs
- 1) / minleafrecs
;
94 for (level
= 1; maxblocks
> 1; level
++) {
95 if (maxblocks
<= maxrootrecs
)
98 maxblocks
= (maxblocks
+ minnoderecs
- 1) / minnoderecs
;
100 mp
->m_bm_maxlevels
[whichfork
] = level
;
103 STATIC
int /* error */
105 struct xfs_btree_cur
*cur
,
106 struct xfs_bmbt_irec
*irec
,
107 int *stat
) /* success/failure */
109 cur
->bc_rec
.b
= *irec
;
110 return xfs_btree_lookup(cur
, XFS_LOOKUP_EQ
, stat
);
113 STATIC
int /* error */
114 xfs_bmbt_lookup_first(
115 struct xfs_btree_cur
*cur
,
116 int *stat
) /* success/failure */
118 cur
->bc_rec
.b
.br_startoff
= 0;
119 cur
->bc_rec
.b
.br_startblock
= 0;
120 cur
->bc_rec
.b
.br_blockcount
= 0;
121 return xfs_btree_lookup(cur
, XFS_LOOKUP_GE
, stat
);
125 * Check if the inode needs to be converted to btree format.
127 static inline bool xfs_bmap_needs_btree(struct xfs_inode
*ip
, int whichfork
)
129 return whichfork
!= XFS_COW_FORK
&&
130 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
&&
131 XFS_IFORK_NEXTENTS(ip
, whichfork
) >
132 XFS_IFORK_MAXEXT(ip
, whichfork
);
136 * Check if the inode should be converted to extent format.
138 static inline bool xfs_bmap_wants_extents(struct xfs_inode
*ip
, int whichfork
)
140 return whichfork
!= XFS_COW_FORK
&&
141 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
&&
142 XFS_IFORK_NEXTENTS(ip
, whichfork
) <=
143 XFS_IFORK_MAXEXT(ip
, whichfork
);
147 * Update the record referred to by cur to the value given by irec
148 * This either works (return 0) or gets an EFSCORRUPTED error.
152 struct xfs_btree_cur
*cur
,
153 struct xfs_bmbt_irec
*irec
)
155 union xfs_btree_rec rec
;
157 xfs_bmbt_disk_set_all(&rec
.bmbt
, irec
);
158 return xfs_btree_update(cur
, &rec
);
162 * Compute the worst-case number of indirect blocks that will be used
163 * for ip's delayed extent of length "len".
166 xfs_bmap_worst_indlen(
167 xfs_inode_t
*ip
, /* incore inode pointer */
168 xfs_filblks_t len
) /* delayed extent length */
170 int level
; /* btree level number */
171 int maxrecs
; /* maximum record count at this level */
172 xfs_mount_t
*mp
; /* mount structure */
173 xfs_filblks_t rval
; /* return value */
176 maxrecs
= mp
->m_bmap_dmxr
[0];
177 for (level
= 0, rval
= 0;
178 level
< XFS_BM_MAXLEVELS(mp
, XFS_DATA_FORK
);
181 do_div(len
, maxrecs
);
184 return rval
+ XFS_BM_MAXLEVELS(mp
, XFS_DATA_FORK
) -
187 maxrecs
= mp
->m_bmap_dmxr
[1];
193 * Calculate the default attribute fork offset for newly created inodes.
196 xfs_default_attroffset(
197 struct xfs_inode
*ip
)
199 struct xfs_mount
*mp
= ip
->i_mount
;
202 if (mp
->m_sb
.sb_inodesize
== 256) {
203 offset
= XFS_LITINO(mp
, ip
->i_d
.di_version
) -
204 XFS_BMDR_SPACE_CALC(MINABTPTRS
);
206 offset
= XFS_BMDR_SPACE_CALC(6 * MINABTPTRS
);
209 ASSERT(offset
< XFS_LITINO(mp
, ip
->i_d
.di_version
));
214 * Helper routine to reset inode di_forkoff field when switching
215 * attribute fork from local to extent format - we reset it where
216 * possible to make space available for inline data fork extents.
219 xfs_bmap_forkoff_reset(
223 if (whichfork
== XFS_ATTR_FORK
&&
224 ip
->i_d
.di_format
!= XFS_DINODE_FMT_DEV
&&
225 ip
->i_d
.di_format
!= XFS_DINODE_FMT_BTREE
) {
226 uint dfl_forkoff
= xfs_default_attroffset(ip
) >> 3;
228 if (dfl_forkoff
> ip
->i_d
.di_forkoff
)
229 ip
->i_d
.di_forkoff
= dfl_forkoff
;
234 STATIC
struct xfs_buf
*
236 struct xfs_btree_cur
*cur
,
239 struct xfs_log_item_desc
*lidp
;
245 for (i
= 0; i
< XFS_BTREE_MAXLEVELS
; i
++) {
246 if (!cur
->bc_bufs
[i
])
248 if (XFS_BUF_ADDR(cur
->bc_bufs
[i
]) == bno
)
249 return cur
->bc_bufs
[i
];
252 /* Chase down all the log items to see if the bp is there */
253 list_for_each_entry(lidp
, &cur
->bc_tp
->t_items
, lid_trans
) {
254 struct xfs_buf_log_item
*bip
;
255 bip
= (struct xfs_buf_log_item
*)lidp
->lid_item
;
256 if (bip
->bli_item
.li_type
== XFS_LI_BUF
&&
257 XFS_BUF_ADDR(bip
->bli_buf
) == bno
)
266 struct xfs_btree_block
*block
,
272 __be64
*pp
, *thispa
; /* pointer to block address */
273 xfs_bmbt_key_t
*prevp
, *keyp
;
275 ASSERT(be16_to_cpu(block
->bb_level
) > 0);
278 for( i
= 1; i
<= xfs_btree_get_numrecs(block
); i
++) {
279 dmxr
= mp
->m_bmap_dmxr
[0];
280 keyp
= XFS_BMBT_KEY_ADDR(mp
, block
, i
);
283 ASSERT(be64_to_cpu(prevp
->br_startoff
) <
284 be64_to_cpu(keyp
->br_startoff
));
289 * Compare the block numbers to see if there are dups.
292 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, i
, sz
);
294 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, i
, dmxr
);
296 for (j
= i
+1; j
<= be16_to_cpu(block
->bb_numrecs
); j
++) {
298 thispa
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, j
, sz
);
300 thispa
= XFS_BMBT_PTR_ADDR(mp
, block
, j
, dmxr
);
301 if (*thispa
== *pp
) {
302 xfs_warn(mp
, "%s: thispa(%d) == pp(%d) %Ld",
304 (unsigned long long)be64_to_cpu(*thispa
));
305 panic("%s: ptrs are equal in node\n",
313 * Check that the extents for the inode ip are in the right order in all
314 * btree leaves. THis becomes prohibitively expensive for large extent count
315 * files, so don't bother with inodes that have more than 10,000 extents in
316 * them. The btree record ordering checks will still be done, so for such large
317 * bmapbt constructs that is going to catch most corruptions.
320 xfs_bmap_check_leaf_extents(
321 xfs_btree_cur_t
*cur
, /* btree cursor or null */
322 xfs_inode_t
*ip
, /* incore inode pointer */
323 int whichfork
) /* data or attr fork */
325 struct xfs_btree_block
*block
; /* current btree block */
326 xfs_fsblock_t bno
; /* block # of "block" */
327 xfs_buf_t
*bp
; /* buffer for "block" */
328 int error
; /* error return value */
329 xfs_extnum_t i
=0, j
; /* index into the extents list */
330 xfs_ifork_t
*ifp
; /* fork structure */
331 int level
; /* btree level, for checking */
332 xfs_mount_t
*mp
; /* file system mount structure */
333 __be64
*pp
; /* pointer to block address */
334 xfs_bmbt_rec_t
*ep
; /* pointer to current extent */
335 xfs_bmbt_rec_t last
= {0, 0}; /* last extent in prev block */
336 xfs_bmbt_rec_t
*nextp
; /* pointer to next extent */
339 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
) {
343 /* skip large extent count inodes */
344 if (ip
->i_d
.di_nextents
> 10000)
349 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
350 block
= ifp
->if_broot
;
352 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
354 level
= be16_to_cpu(block
->bb_level
);
356 xfs_check_block(block
, mp
, 1, ifp
->if_broot_bytes
);
357 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, 1, ifp
->if_broot_bytes
);
358 bno
= be64_to_cpu(*pp
);
360 ASSERT(bno
!= NULLFSBLOCK
);
361 ASSERT(XFS_FSB_TO_AGNO(mp
, bno
) < mp
->m_sb
.sb_agcount
);
362 ASSERT(XFS_FSB_TO_AGBNO(mp
, bno
) < mp
->m_sb
.sb_agblocks
);
365 * Go down the tree until leaf level is reached, following the first
366 * pointer (leftmost) at each level.
368 while (level
-- > 0) {
369 /* See if buf is in cur first */
371 bp
= xfs_bmap_get_bp(cur
, XFS_FSB_TO_DADDR(mp
, bno
));
374 error
= xfs_btree_read_bufl(mp
, NULL
, bno
, 0, &bp
,
380 block
= XFS_BUF_TO_BLOCK(bp
);
385 * Check this block for basic sanity (increasing keys and
386 * no duplicate blocks).
389 xfs_check_block(block
, mp
, 0, 0);
390 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, mp
->m_bmap_dmxr
[1]);
391 bno
= be64_to_cpu(*pp
);
392 XFS_WANT_CORRUPTED_GOTO(mp
,
393 XFS_FSB_SANITY_CHECK(mp
, bno
), error0
);
396 xfs_trans_brelse(NULL
, bp
);
401 * Here with bp and block set to the leftmost leaf node in the tree.
406 * Loop over all leaf nodes checking that all extents are in the right order.
409 xfs_fsblock_t nextbno
;
410 xfs_extnum_t num_recs
;
413 num_recs
= xfs_btree_get_numrecs(block
);
416 * Read-ahead the next leaf block, if any.
419 nextbno
= be64_to_cpu(block
->bb_u
.l
.bb_rightsib
);
422 * Check all the extents to make sure they are OK.
423 * If we had a previous block, the last entry should
424 * conform with the first entry in this one.
427 ep
= XFS_BMBT_REC_ADDR(mp
, block
, 1);
429 ASSERT(xfs_bmbt_disk_get_startoff(&last
) +
430 xfs_bmbt_disk_get_blockcount(&last
) <=
431 xfs_bmbt_disk_get_startoff(ep
));
433 for (j
= 1; j
< num_recs
; j
++) {
434 nextp
= XFS_BMBT_REC_ADDR(mp
, block
, j
+ 1);
435 ASSERT(xfs_bmbt_disk_get_startoff(ep
) +
436 xfs_bmbt_disk_get_blockcount(ep
) <=
437 xfs_bmbt_disk_get_startoff(nextp
));
445 xfs_trans_brelse(NULL
, bp
);
449 * If we've reached the end, stop.
451 if (bno
== NULLFSBLOCK
)
455 bp
= xfs_bmap_get_bp(cur
, XFS_FSB_TO_DADDR(mp
, bno
));
458 error
= xfs_btree_read_bufl(mp
, NULL
, bno
, 0, &bp
,
464 block
= XFS_BUF_TO_BLOCK(bp
);
470 xfs_warn(mp
, "%s: at error0", __func__
);
472 xfs_trans_brelse(NULL
, bp
);
474 xfs_warn(mp
, "%s: BAD after btree leaves for %d extents",
476 panic("%s: CORRUPTED BTREE OR SOMETHING", __func__
);
481 * Validate that the bmbt_irecs being returned from bmapi are valid
482 * given the caller's original parameters. Specifically check the
483 * ranges of the returned irecs to ensure that they only extend beyond
484 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
487 xfs_bmap_validate_ret(
491 xfs_bmbt_irec_t
*mval
,
495 int i
; /* index to map values */
497 ASSERT(ret_nmap
<= nmap
);
499 for (i
= 0; i
< ret_nmap
; i
++) {
500 ASSERT(mval
[i
].br_blockcount
> 0);
501 if (!(flags
& XFS_BMAPI_ENTIRE
)) {
502 ASSERT(mval
[i
].br_startoff
>= bno
);
503 ASSERT(mval
[i
].br_blockcount
<= len
);
504 ASSERT(mval
[i
].br_startoff
+ mval
[i
].br_blockcount
<=
507 ASSERT(mval
[i
].br_startoff
< bno
+ len
);
508 ASSERT(mval
[i
].br_startoff
+ mval
[i
].br_blockcount
>
512 mval
[i
- 1].br_startoff
+ mval
[i
- 1].br_blockcount
==
513 mval
[i
].br_startoff
);
514 ASSERT(mval
[i
].br_startblock
!= DELAYSTARTBLOCK
&&
515 mval
[i
].br_startblock
!= HOLESTARTBLOCK
);
516 ASSERT(mval
[i
].br_state
== XFS_EXT_NORM
||
517 mval
[i
].br_state
== XFS_EXT_UNWRITTEN
);
522 #define xfs_bmap_check_leaf_extents(cur, ip, whichfork) do { } while (0)
523 #define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap) do { } while (0)
527 * bmap free list manipulation functions
531 * Add the extent to the list of extents to be free at transaction end.
532 * The list is maintained sorted (by block number).
536 struct xfs_mount
*mp
,
537 struct xfs_defer_ops
*dfops
,
540 struct xfs_owner_info
*oinfo
)
542 struct xfs_extent_free_item
*new; /* new element */
547 ASSERT(bno
!= NULLFSBLOCK
);
549 ASSERT(len
<= MAXEXTLEN
);
550 ASSERT(!isnullstartblock(bno
));
551 agno
= XFS_FSB_TO_AGNO(mp
, bno
);
552 agbno
= XFS_FSB_TO_AGBNO(mp
, bno
);
553 ASSERT(agno
< mp
->m_sb
.sb_agcount
);
554 ASSERT(agbno
< mp
->m_sb
.sb_agblocks
);
555 ASSERT(len
< mp
->m_sb
.sb_agblocks
);
556 ASSERT(agbno
+ len
<= mp
->m_sb
.sb_agblocks
);
558 ASSERT(xfs_bmap_free_item_zone
!= NULL
);
560 new = kmem_zone_alloc(xfs_bmap_free_item_zone
, KM_SLEEP
);
561 new->xefi_startblock
= bno
;
562 new->xefi_blockcount
= (xfs_extlen_t
)len
;
564 new->xefi_oinfo
= *oinfo
;
566 xfs_rmap_skip_owner_update(&new->xefi_oinfo
);
567 trace_xfs_bmap_free_defer(mp
, XFS_FSB_TO_AGNO(mp
, bno
), 0,
568 XFS_FSB_TO_AGBNO(mp
, bno
), len
);
569 xfs_defer_add(dfops
, XFS_DEFER_OPS_TYPE_FREE
, &new->xefi_list
);
573 * Inode fork format manipulation functions
577 * Transform a btree format file with only one leaf node, where the
578 * extents list will fit in the inode, into an extents format file.
579 * Since the file extents are already in-core, all we have to do is
580 * give up the space for the btree root and pitch the leaf block.
582 STATIC
int /* error */
583 xfs_bmap_btree_to_extents(
584 xfs_trans_t
*tp
, /* transaction pointer */
585 xfs_inode_t
*ip
, /* incore inode pointer */
586 xfs_btree_cur_t
*cur
, /* btree cursor */
587 int *logflagsp
, /* inode logging flags */
588 int whichfork
) /* data or attr fork */
591 struct xfs_btree_block
*cblock
;/* child btree block */
592 xfs_fsblock_t cbno
; /* child block number */
593 xfs_buf_t
*cbp
; /* child block's buffer */
594 int error
; /* error return value */
595 xfs_ifork_t
*ifp
; /* inode fork data */
596 xfs_mount_t
*mp
; /* mount point structure */
597 __be64
*pp
; /* ptr to block address */
598 struct xfs_btree_block
*rblock
;/* root btree block */
599 struct xfs_owner_info oinfo
;
602 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
603 ASSERT(whichfork
!= XFS_COW_FORK
);
604 ASSERT(ifp
->if_flags
& XFS_IFEXTENTS
);
605 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
);
606 rblock
= ifp
->if_broot
;
607 ASSERT(be16_to_cpu(rblock
->bb_level
) == 1);
608 ASSERT(be16_to_cpu(rblock
->bb_numrecs
) == 1);
609 ASSERT(xfs_bmbt_maxrecs(mp
, ifp
->if_broot_bytes
, 0) == 1);
610 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, rblock
, 1, ifp
->if_broot_bytes
);
611 cbno
= be64_to_cpu(*pp
);
614 XFS_WANT_CORRUPTED_RETURN(cur
->bc_mp
,
615 xfs_btree_check_lptr(cur
, cbno
, 1));
617 error
= xfs_btree_read_bufl(mp
, tp
, cbno
, 0, &cbp
, XFS_BMAP_BTREE_REF
,
621 cblock
= XFS_BUF_TO_BLOCK(cbp
);
622 if ((error
= xfs_btree_check_block(cur
, cblock
, 0, cbp
)))
624 xfs_rmap_ino_bmbt_owner(&oinfo
, ip
->i_ino
, whichfork
);
625 xfs_bmap_add_free(mp
, cur
->bc_private
.b
.dfops
, cbno
, 1, &oinfo
);
626 ip
->i_d
.di_nblocks
--;
627 xfs_trans_mod_dquot_byino(tp
, ip
, XFS_TRANS_DQ_BCOUNT
, -1L);
628 xfs_trans_binval(tp
, cbp
);
629 if (cur
->bc_bufs
[0] == cbp
)
630 cur
->bc_bufs
[0] = NULL
;
631 xfs_iroot_realloc(ip
, -1, whichfork
);
632 ASSERT(ifp
->if_broot
== NULL
);
633 ASSERT((ifp
->if_flags
& XFS_IFBROOT
) == 0);
634 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_EXTENTS
);
635 *logflagsp
= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
640 * Convert an extents-format file into a btree-format file.
641 * The new file will have a root block (in the inode) and a single child block.
643 STATIC
int /* error */
644 xfs_bmap_extents_to_btree(
645 xfs_trans_t
*tp
, /* transaction pointer */
646 xfs_inode_t
*ip
, /* incore inode pointer */
647 xfs_fsblock_t
*firstblock
, /* first-block-allocated */
648 struct xfs_defer_ops
*dfops
, /* blocks freed in xaction */
649 xfs_btree_cur_t
**curp
, /* cursor returned to caller */
650 int wasdel
, /* converting a delayed alloc */
651 int *logflagsp
, /* inode logging flags */
652 int whichfork
) /* data or attr fork */
654 struct xfs_btree_block
*ablock
; /* allocated (child) bt block */
655 xfs_buf_t
*abp
; /* buffer for ablock */
656 xfs_alloc_arg_t args
; /* allocation arguments */
657 xfs_bmbt_rec_t
*arp
; /* child record pointer */
658 struct xfs_btree_block
*block
; /* btree root block */
659 xfs_btree_cur_t
*cur
; /* bmap btree cursor */
660 xfs_bmbt_rec_host_t
*ep
; /* extent record pointer */
661 int error
; /* error return value */
662 xfs_extnum_t i
, cnt
; /* extent record index */
663 xfs_ifork_t
*ifp
; /* inode fork pointer */
664 xfs_bmbt_key_t
*kp
; /* root block key pointer */
665 xfs_mount_t
*mp
; /* mount structure */
666 xfs_extnum_t nextents
; /* number of file extents */
667 xfs_bmbt_ptr_t
*pp
; /* root block address pointer */
670 ASSERT(whichfork
!= XFS_COW_FORK
);
671 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
672 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
);
675 * Make space in the inode incore.
677 xfs_iroot_realloc(ip
, 1, whichfork
);
678 ifp
->if_flags
|= XFS_IFBROOT
;
683 block
= ifp
->if_broot
;
684 xfs_btree_init_block_int(mp
, block
, XFS_BUF_DADDR_NULL
,
685 XFS_BTNUM_BMAP
, 1, 1, ip
->i_ino
,
686 XFS_BTREE_LONG_PTRS
);
688 * Need a cursor. Can't allocate until bb_level is filled in.
690 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
691 cur
->bc_private
.b
.firstblock
= *firstblock
;
692 cur
->bc_private
.b
.dfops
= dfops
;
693 cur
->bc_private
.b
.flags
= wasdel
? XFS_BTCUR_BPRV_WASDEL
: 0;
695 * Convert to a btree with two levels, one record in root.
697 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_BTREE
);
698 memset(&args
, 0, sizeof(args
));
701 xfs_rmap_ino_bmbt_owner(&args
.oinfo
, ip
->i_ino
, whichfork
);
702 args
.firstblock
= *firstblock
;
703 if (*firstblock
== NULLFSBLOCK
) {
704 args
.type
= XFS_ALLOCTYPE_START_BNO
;
705 args
.fsbno
= XFS_INO_TO_FSB(mp
, ip
->i_ino
);
706 } else if (dfops
->dop_low
) {
707 args
.type
= XFS_ALLOCTYPE_START_BNO
;
708 args
.fsbno
= *firstblock
;
710 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
711 args
.fsbno
= *firstblock
;
713 args
.minlen
= args
.maxlen
= args
.prod
= 1;
714 args
.wasdel
= wasdel
;
716 if ((error
= xfs_alloc_vextent(&args
))) {
717 xfs_iroot_realloc(ip
, -1, whichfork
);
718 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
722 if (WARN_ON_ONCE(args
.fsbno
== NULLFSBLOCK
)) {
723 xfs_iroot_realloc(ip
, -1, whichfork
);
724 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
728 * Allocation can't fail, the space was reserved.
730 ASSERT(*firstblock
== NULLFSBLOCK
||
731 args
.agno
>= XFS_FSB_TO_AGNO(mp
, *firstblock
));
732 *firstblock
= cur
->bc_private
.b
.firstblock
= args
.fsbno
;
733 cur
->bc_private
.b
.allocated
++;
734 ip
->i_d
.di_nblocks
++;
735 xfs_trans_mod_dquot_byino(tp
, ip
, XFS_TRANS_DQ_BCOUNT
, 1L);
736 abp
= xfs_btree_get_bufl(mp
, tp
, args
.fsbno
, 0);
738 * Fill in the child block.
740 abp
->b_ops
= &xfs_bmbt_buf_ops
;
741 ablock
= XFS_BUF_TO_BLOCK(abp
);
742 xfs_btree_init_block_int(mp
, ablock
, abp
->b_bn
,
743 XFS_BTNUM_BMAP
, 0, 0, ip
->i_ino
,
744 XFS_BTREE_LONG_PTRS
);
746 arp
= XFS_BMBT_REC_ADDR(mp
, ablock
, 1);
747 nextents
= xfs_iext_count(ifp
);
748 for (cnt
= i
= 0; i
< nextents
; i
++) {
749 ep
= xfs_iext_get_ext(ifp
, i
);
750 if (!isnullstartblock(xfs_bmbt_get_startblock(ep
))) {
751 arp
->l0
= cpu_to_be64(ep
->l0
);
752 arp
->l1
= cpu_to_be64(ep
->l1
);
756 ASSERT(cnt
== XFS_IFORK_NEXTENTS(ip
, whichfork
));
757 xfs_btree_set_numrecs(ablock
, cnt
);
760 * Fill in the root key and pointer.
762 kp
= XFS_BMBT_KEY_ADDR(mp
, block
, 1);
763 arp
= XFS_BMBT_REC_ADDR(mp
, ablock
, 1);
764 kp
->br_startoff
= cpu_to_be64(xfs_bmbt_disk_get_startoff(arp
));
765 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, xfs_bmbt_get_maxrecs(cur
,
766 be16_to_cpu(block
->bb_level
)));
767 *pp
= cpu_to_be64(args
.fsbno
);
770 * Do all this logging at the end so that
771 * the root is at the right level.
773 xfs_btree_log_block(cur
, abp
, XFS_BB_ALL_BITS
);
774 xfs_btree_log_recs(cur
, abp
, 1, be16_to_cpu(ablock
->bb_numrecs
));
775 ASSERT(*curp
== NULL
);
777 *logflagsp
= XFS_ILOG_CORE
| xfs_ilog_fbroot(whichfork
);
782 * Convert a local file to an extents file.
783 * This code is out of bounds for data forks of regular files,
784 * since the file data needs to get logged so things will stay consistent.
785 * (The bmap-level manipulations are ok, though).
788 xfs_bmap_local_to_extents_empty(
789 struct xfs_inode
*ip
,
792 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
794 ASSERT(whichfork
!= XFS_COW_FORK
);
795 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
796 ASSERT(ifp
->if_bytes
== 0);
797 ASSERT(XFS_IFORK_NEXTENTS(ip
, whichfork
) == 0);
799 xfs_bmap_forkoff_reset(ip
, whichfork
);
800 ifp
->if_flags
&= ~XFS_IFINLINE
;
801 ifp
->if_flags
|= XFS_IFEXTENTS
;
802 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_EXTENTS
);
806 STATIC
int /* error */
807 xfs_bmap_local_to_extents(
808 xfs_trans_t
*tp
, /* transaction pointer */
809 xfs_inode_t
*ip
, /* incore inode pointer */
810 xfs_fsblock_t
*firstblock
, /* first block allocated in xaction */
811 xfs_extlen_t total
, /* total blocks needed by transaction */
812 int *logflagsp
, /* inode logging flags */
814 void (*init_fn
)(struct xfs_trans
*tp
,
816 struct xfs_inode
*ip
,
817 struct xfs_ifork
*ifp
))
820 int flags
; /* logging flags returned */
821 xfs_ifork_t
*ifp
; /* inode fork pointer */
822 xfs_alloc_arg_t args
; /* allocation arguments */
823 xfs_buf_t
*bp
; /* buffer for extent block */
824 struct xfs_bmbt_irec rec
;
827 * We don't want to deal with the case of keeping inode data inline yet.
828 * So sending the data fork of a regular inode is invalid.
830 ASSERT(!(S_ISREG(VFS_I(ip
)->i_mode
) && whichfork
== XFS_DATA_FORK
));
831 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
832 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
834 if (!ifp
->if_bytes
) {
835 xfs_bmap_local_to_extents_empty(ip
, whichfork
);
836 flags
= XFS_ILOG_CORE
;
842 ASSERT((ifp
->if_flags
& (XFS_IFINLINE
|XFS_IFEXTENTS
|XFS_IFEXTIREC
)) ==
844 memset(&args
, 0, sizeof(args
));
846 args
.mp
= ip
->i_mount
;
847 xfs_rmap_ino_owner(&args
.oinfo
, ip
->i_ino
, whichfork
, 0);
848 args
.firstblock
= *firstblock
;
850 * Allocate a block. We know we need only one, since the
851 * file currently fits in an inode.
853 if (*firstblock
== NULLFSBLOCK
) {
854 args
.fsbno
= XFS_INO_TO_FSB(args
.mp
, ip
->i_ino
);
855 args
.type
= XFS_ALLOCTYPE_START_BNO
;
857 args
.fsbno
= *firstblock
;
858 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
861 args
.minlen
= args
.maxlen
= args
.prod
= 1;
862 error
= xfs_alloc_vextent(&args
);
866 /* Can't fail, the space was reserved. */
867 ASSERT(args
.fsbno
!= NULLFSBLOCK
);
868 ASSERT(args
.len
== 1);
869 *firstblock
= args
.fsbno
;
870 bp
= xfs_btree_get_bufl(args
.mp
, tp
, args
.fsbno
, 0);
873 * Initialize the block, copy the data and log the remote buffer.
875 * The callout is responsible for logging because the remote format
876 * might differ from the local format and thus we don't know how much to
877 * log here. Note that init_fn must also set the buffer log item type
880 init_fn(tp
, bp
, ip
, ifp
);
882 /* account for the change in fork size */
883 xfs_idata_realloc(ip
, -ifp
->if_bytes
, whichfork
);
884 xfs_bmap_local_to_extents_empty(ip
, whichfork
);
885 flags
|= XFS_ILOG_CORE
;
888 rec
.br_startblock
= args
.fsbno
;
889 rec
.br_blockcount
= 1;
890 rec
.br_state
= XFS_EXT_NORM
;
891 xfs_iext_insert(ip
, 0, 1, &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
);
1052 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
1053 error
= xfs_trans_reserve_quota_nblks(tp
, ip
, blks
, 0, rsvd
?
1054 XFS_QMOPT_RES_REGBLKS
| XFS_QMOPT_FORCE_RES
:
1055 XFS_QMOPT_RES_REGBLKS
);
1058 if (XFS_IFORK_Q(ip
))
1060 if (ip
->i_d
.di_anextents
!= 0) {
1061 error
= -EFSCORRUPTED
;
1064 if (ip
->i_d
.di_aformat
!= XFS_DINODE_FMT_EXTENTS
) {
1066 * For inodes coming from pre-6.2 filesystems.
1068 ASSERT(ip
->i_d
.di_aformat
== 0);
1069 ip
->i_d
.di_aformat
= XFS_DINODE_FMT_EXTENTS
;
1072 xfs_trans_ijoin(tp
, ip
, 0);
1073 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
1075 switch (ip
->i_d
.di_format
) {
1076 case XFS_DINODE_FMT_DEV
:
1077 ip
->i_d
.di_forkoff
= roundup(sizeof(xfs_dev_t
), 8) >> 3;
1079 case XFS_DINODE_FMT_LOCAL
:
1080 case XFS_DINODE_FMT_EXTENTS
:
1081 case XFS_DINODE_FMT_BTREE
:
1082 ip
->i_d
.di_forkoff
= xfs_attr_shortform_bytesfit(ip
, size
);
1083 if (!ip
->i_d
.di_forkoff
)
1084 ip
->i_d
.di_forkoff
= xfs_default_attroffset(ip
) >> 3;
1085 else if (mp
->m_flags
& XFS_MOUNT_ATTR2
)
1094 ASSERT(ip
->i_afp
== NULL
);
1095 ip
->i_afp
= kmem_zone_zalloc(xfs_ifork_zone
, KM_SLEEP
);
1096 ip
->i_afp
->if_flags
= XFS_IFEXTENTS
;
1098 xfs_defer_init(&dfops
, &firstblock
);
1099 switch (ip
->i_d
.di_format
) {
1100 case XFS_DINODE_FMT_LOCAL
:
1101 error
= xfs_bmap_add_attrfork_local(tp
, ip
, &firstblock
, &dfops
,
1104 case XFS_DINODE_FMT_EXTENTS
:
1105 error
= xfs_bmap_add_attrfork_extents(tp
, ip
, &firstblock
,
1108 case XFS_DINODE_FMT_BTREE
:
1109 error
= xfs_bmap_add_attrfork_btree(tp
, ip
, &firstblock
, &dfops
,
1117 xfs_trans_log_inode(tp
, ip
, logflags
);
1120 if (!xfs_sb_version_hasattr(&mp
->m_sb
) ||
1121 (!xfs_sb_version_hasattr2(&mp
->m_sb
) && version
== 2)) {
1122 bool log_sb
= false;
1124 spin_lock(&mp
->m_sb_lock
);
1125 if (!xfs_sb_version_hasattr(&mp
->m_sb
)) {
1126 xfs_sb_version_addattr(&mp
->m_sb
);
1129 if (!xfs_sb_version_hasattr2(&mp
->m_sb
) && version
== 2) {
1130 xfs_sb_version_addattr2(&mp
->m_sb
);
1133 spin_unlock(&mp
->m_sb_lock
);
1138 error
= xfs_defer_finish(&tp
, &dfops
);
1141 error
= xfs_trans_commit(tp
);
1142 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
1146 xfs_defer_cancel(&dfops
);
1148 xfs_trans_cancel(tp
);
1149 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
1154 * Internal and external extent tree search functions.
1158 * Read in the extents to if_extents.
1159 * All inode fields are set up by caller, we just traverse the btree
1160 * and copy the records in. If the file system cannot contain unwritten
1161 * extents, the records are checked for no "state" flags.
1164 xfs_bmap_read_extents(
1165 xfs_trans_t
*tp
, /* transaction pointer */
1166 xfs_inode_t
*ip
, /* incore inode */
1167 int whichfork
) /* data or attr fork */
1169 struct xfs_btree_block
*block
; /* current btree block */
1170 xfs_fsblock_t bno
; /* block # of "block" */
1171 xfs_buf_t
*bp
; /* buffer for "block" */
1172 int error
; /* error return value */
1173 xfs_extnum_t i
, j
; /* index into the extents list */
1174 xfs_ifork_t
*ifp
; /* fork structure */
1175 int level
; /* btree level, for checking */
1176 xfs_mount_t
*mp
; /* file system mount structure */
1177 __be64
*pp
; /* pointer to block address */
1179 xfs_extnum_t room
; /* number of entries there's room for */
1180 int state
= xfs_bmap_fork_to_state(whichfork
);
1183 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1184 block
= ifp
->if_broot
;
1186 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
1188 level
= be16_to_cpu(block
->bb_level
);
1190 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, 1, ifp
->if_broot_bytes
);
1191 bno
= be64_to_cpu(*pp
);
1194 * Go down the tree until leaf level is reached, following the first
1195 * pointer (leftmost) at each level.
1197 while (level
-- > 0) {
1198 error
= xfs_btree_read_bufl(mp
, tp
, bno
, 0, &bp
,
1199 XFS_BMAP_BTREE_REF
, &xfs_bmbt_buf_ops
);
1202 block
= XFS_BUF_TO_BLOCK(bp
);
1205 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, mp
->m_bmap_dmxr
[1]);
1206 bno
= be64_to_cpu(*pp
);
1207 XFS_WANT_CORRUPTED_GOTO(mp
,
1208 XFS_FSB_SANITY_CHECK(mp
, bno
), error0
);
1209 xfs_trans_brelse(tp
, bp
);
1212 * Here with bp and block set to the leftmost leaf node in the tree.
1214 room
= xfs_iext_count(ifp
);
1217 * Loop over all leaf nodes. Copy information to the extent records.
1220 xfs_bmbt_rec_t
*frp
;
1221 xfs_fsblock_t nextbno
;
1222 xfs_extnum_t num_recs
;
1224 num_recs
= xfs_btree_get_numrecs(block
);
1225 if (unlikely(i
+ num_recs
> room
)) {
1226 ASSERT(i
+ num_recs
<= room
);
1227 xfs_warn(ip
->i_mount
,
1228 "corrupt dinode %Lu, (btree extents).",
1229 (unsigned long long) ip
->i_ino
);
1230 XFS_CORRUPTION_ERROR("xfs_bmap_read_extents(1)",
1231 XFS_ERRLEVEL_LOW
, ip
->i_mount
, block
);
1235 * Read-ahead the next leaf block, if any.
1237 nextbno
= be64_to_cpu(block
->bb_u
.l
.bb_rightsib
);
1238 if (nextbno
!= NULLFSBLOCK
)
1239 xfs_btree_reada_bufl(mp
, nextbno
, 1,
1242 * Copy records into the extent records.
1244 frp
= XFS_BMBT_REC_ADDR(mp
, block
, 1);
1245 for (j
= 0; j
< num_recs
; j
++, i
++, frp
++) {
1246 xfs_bmbt_rec_host_t
*trp
= xfs_iext_get_ext(ifp
, i
);
1247 trp
->l0
= be64_to_cpu(frp
->l0
);
1248 trp
->l1
= be64_to_cpu(frp
->l1
);
1249 if (!xfs_bmbt_validate_extent(mp
, whichfork
, trp
)) {
1250 XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
1251 XFS_ERRLEVEL_LOW
, mp
);
1254 trace_xfs_read_extent(ip
, i
, state
, _THIS_IP_
);
1256 xfs_trans_brelse(tp
, bp
);
1259 * If we've reached the end, stop.
1261 if (bno
== NULLFSBLOCK
)
1263 error
= xfs_btree_read_bufl(mp
, tp
, bno
, 0, &bp
,
1264 XFS_BMAP_BTREE_REF
, &xfs_bmbt_buf_ops
);
1267 block
= XFS_BUF_TO_BLOCK(bp
);
1269 if (i
!= XFS_IFORK_NEXTENTS(ip
, whichfork
))
1270 return -EFSCORRUPTED
;
1271 ASSERT(i
== xfs_iext_count(ifp
));
1274 xfs_trans_brelse(tp
, bp
);
1275 return -EFSCORRUPTED
;
1279 * Returns the file-relative block number of the first unused block(s)
1280 * in the file with at least "len" logically contiguous blocks free.
1281 * This is the lowest-address hole if the file has holes, else the first block
1282 * past the end of file.
1283 * Return 0 if the file is currently local (in-inode).
1286 xfs_bmap_first_unused(
1287 xfs_trans_t
*tp
, /* transaction pointer */
1288 xfs_inode_t
*ip
, /* incore inode */
1289 xfs_extlen_t len
, /* size of hole to find */
1290 xfs_fileoff_t
*first_unused
, /* unused block */
1291 int whichfork
) /* data or attr fork */
1293 int error
; /* error return value */
1294 int idx
; /* extent record index */
1295 xfs_ifork_t
*ifp
; /* inode fork pointer */
1296 xfs_fileoff_t lastaddr
; /* last block number seen */
1297 xfs_fileoff_t lowest
; /* lowest useful block */
1298 xfs_fileoff_t max
; /* starting useful block */
1299 xfs_extnum_t nextents
; /* number of extent entries */
1301 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
||
1302 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
||
1303 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
1304 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
) {
1308 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1309 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
1310 (error
= xfs_iread_extents(tp
, ip
, whichfork
)))
1312 lowest
= *first_unused
;
1313 nextents
= xfs_iext_count(ifp
);
1314 for (idx
= 0, lastaddr
= 0, max
= lowest
; idx
< nextents
; idx
++) {
1315 struct xfs_bmbt_irec got
;
1317 xfs_iext_get_extent(ifp
, idx
, &got
);
1320 * See if the hole before this extent will work.
1322 if (got
.br_startoff
>= lowest
+ len
&&
1323 got
.br_startoff
- max
>= len
) {
1324 *first_unused
= max
;
1327 lastaddr
= got
.br_startoff
+ got
.br_blockcount
;
1328 max
= XFS_FILEOFF_MAX(lastaddr
, lowest
);
1330 *first_unused
= max
;
1335 * Returns the file-relative block number of the last block - 1 before
1336 * last_block (input value) in the file.
1337 * This is not based on i_size, it is based on the extent records.
1338 * Returns 0 for local files, as they do not have extent records.
1341 xfs_bmap_last_before(
1342 struct xfs_trans
*tp
, /* transaction pointer */
1343 struct xfs_inode
*ip
, /* incore inode */
1344 xfs_fileoff_t
*last_block
, /* last block */
1345 int whichfork
) /* data or attr fork */
1347 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1348 struct xfs_bmbt_irec got
;
1352 switch (XFS_IFORK_FORMAT(ip
, whichfork
)) {
1353 case XFS_DINODE_FMT_LOCAL
:
1356 case XFS_DINODE_FMT_BTREE
:
1357 case XFS_DINODE_FMT_EXTENTS
:
1363 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
1364 error
= xfs_iread_extents(tp
, ip
, whichfork
);
1369 if (xfs_iext_lookup_extent(ip
, ifp
, *last_block
- 1, &idx
, &got
)) {
1370 if (got
.br_startoff
<= *last_block
- 1)
1374 if (xfs_iext_get_extent(ifp
, idx
- 1, &got
)) {
1375 *last_block
= got
.br_startoff
+ got
.br_blockcount
;
1384 xfs_bmap_last_extent(
1385 struct xfs_trans
*tp
,
1386 struct xfs_inode
*ip
,
1388 struct xfs_bmbt_irec
*rec
,
1391 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1395 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
1396 error
= xfs_iread_extents(tp
, ip
, whichfork
);
1401 nextents
= xfs_iext_count(ifp
);
1402 if (nextents
== 0) {
1407 xfs_iext_get_extent(ifp
, nextents
- 1, rec
);
1413 * Check the last inode extent to determine whether this allocation will result
1414 * in blocks being allocated at the end of the file. When we allocate new data
1415 * blocks at the end of the file which do not start at the previous data block,
1416 * we will try to align the new blocks at stripe unit boundaries.
1418 * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be
1419 * at, or past the EOF.
1423 struct xfs_bmalloca
*bma
,
1426 struct xfs_bmbt_irec rec
;
1431 error
= xfs_bmap_last_extent(NULL
, bma
->ip
, whichfork
, &rec
,
1442 * Check if we are allocation or past the last extent, or at least into
1443 * the last delayed allocated extent.
1445 bma
->aeof
= bma
->offset
>= rec
.br_startoff
+ rec
.br_blockcount
||
1446 (bma
->offset
>= rec
.br_startoff
&&
1447 isnullstartblock(rec
.br_startblock
));
1452 * Returns the file-relative block number of the first block past eof in
1453 * the file. This is not based on i_size, it is based on the extent records.
1454 * Returns 0 for local files, as they do not have extent records.
1457 xfs_bmap_last_offset(
1458 struct xfs_inode
*ip
,
1459 xfs_fileoff_t
*last_block
,
1462 struct xfs_bmbt_irec rec
;
1468 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
)
1471 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
&&
1472 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
1475 error
= xfs_bmap_last_extent(NULL
, ip
, whichfork
, &rec
, &is_empty
);
1476 if (error
|| is_empty
)
1479 *last_block
= rec
.br_startoff
+ rec
.br_blockcount
;
1484 * Returns whether the selected fork of the inode has exactly one
1485 * block or not. For the data fork we check this matches di_size,
1486 * implying the file's range is 0..bsize-1.
1488 int /* 1=>1 block, 0=>otherwise */
1490 xfs_inode_t
*ip
, /* incore inode */
1491 int whichfork
) /* data or attr fork */
1493 xfs_ifork_t
*ifp
; /* inode fork pointer */
1494 int rval
; /* return value */
1495 xfs_bmbt_irec_t s
; /* internal version of extent */
1498 if (whichfork
== XFS_DATA_FORK
)
1499 return XFS_ISIZE(ip
) == ip
->i_mount
->m_sb
.sb_blocksize
;
1501 if (XFS_IFORK_NEXTENTS(ip
, whichfork
) != 1)
1503 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
1505 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1506 ASSERT(ifp
->if_flags
& XFS_IFEXTENTS
);
1507 xfs_iext_get_extent(ifp
, 0, &s
);
1508 rval
= s
.br_startoff
== 0 && s
.br_blockcount
== 1;
1509 if (rval
&& whichfork
== XFS_DATA_FORK
)
1510 ASSERT(XFS_ISIZE(ip
) == ip
->i_mount
->m_sb
.sb_blocksize
);
1515 * Extent tree manipulation functions used during allocation.
1519 * Convert a delayed allocation to a real allocation.
1521 STATIC
int /* error */
1522 xfs_bmap_add_extent_delay_real(
1523 struct xfs_bmalloca
*bma
,
1526 struct xfs_bmbt_irec
*new = &bma
->got
;
1527 int error
; /* error return value */
1528 int i
; /* temp state */
1529 xfs_ifork_t
*ifp
; /* inode fork pointer */
1530 xfs_fileoff_t new_endoff
; /* end offset of new entry */
1531 xfs_bmbt_irec_t r
[3]; /* neighbor extent entries */
1532 /* left is 0, right is 1, prev is 2 */
1533 int rval
=0; /* return value (logging flags) */
1534 int state
= xfs_bmap_fork_to_state(whichfork
);
1535 xfs_filblks_t da_new
; /* new count del alloc blocks used */
1536 xfs_filblks_t da_old
; /* old count del alloc blocks used */
1537 xfs_filblks_t temp
=0; /* value for da_new calculations */
1538 int tmp_rval
; /* partial logging flags */
1539 struct xfs_mount
*mp
;
1540 xfs_extnum_t
*nextents
;
1541 struct xfs_bmbt_irec old
;
1543 mp
= bma
->ip
->i_mount
;
1544 ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
1545 ASSERT(whichfork
!= XFS_ATTR_FORK
);
1546 nextents
= (whichfork
== XFS_COW_FORK
? &bma
->ip
->i_cnextents
:
1547 &bma
->ip
->i_d
.di_nextents
);
1549 ASSERT(bma
->idx
>= 0);
1550 ASSERT(bma
->idx
<= xfs_iext_count(ifp
));
1551 ASSERT(!isnullstartblock(new->br_startblock
));
1553 (bma
->cur
->bc_private
.b
.flags
& XFS_BTCUR_BPRV_WASDEL
));
1555 XFS_STATS_INC(mp
, xs_add_exlist
);
1562 * Set up a bunch of variables to make the tests simpler.
1564 xfs_iext_get_extent(ifp
, bma
->idx
, &PREV
);
1565 new_endoff
= new->br_startoff
+ new->br_blockcount
;
1566 ASSERT(isnullstartblock(PREV
.br_startblock
));
1567 ASSERT(PREV
.br_startoff
<= new->br_startoff
);
1568 ASSERT(PREV
.br_startoff
+ PREV
.br_blockcount
>= new_endoff
);
1570 da_old
= startblockval(PREV
.br_startblock
);
1574 * Set flags determining what part of the previous delayed allocation
1575 * extent is being replaced by a real allocation.
1577 if (PREV
.br_startoff
== new->br_startoff
)
1578 state
|= BMAP_LEFT_FILLING
;
1579 if (PREV
.br_startoff
+ PREV
.br_blockcount
== new_endoff
)
1580 state
|= BMAP_RIGHT_FILLING
;
1583 * Check and set flags if this segment has a left neighbor.
1584 * Don't set contiguous if the combined extent would be too large.
1587 state
|= BMAP_LEFT_VALID
;
1588 xfs_iext_get_extent(ifp
, bma
->idx
- 1, &LEFT
);
1590 if (isnullstartblock(LEFT
.br_startblock
))
1591 state
|= BMAP_LEFT_DELAY
;
1594 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
1595 LEFT
.br_startoff
+ LEFT
.br_blockcount
== new->br_startoff
&&
1596 LEFT
.br_startblock
+ LEFT
.br_blockcount
== new->br_startblock
&&
1597 LEFT
.br_state
== new->br_state
&&
1598 LEFT
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
1599 state
|= BMAP_LEFT_CONTIG
;
1602 * Check and set flags if this segment has a right neighbor.
1603 * Don't set contiguous if the combined extent would be too large.
1604 * Also check for all-three-contiguous being too large.
1606 if (bma
->idx
< xfs_iext_count(ifp
) - 1) {
1607 state
|= BMAP_RIGHT_VALID
;
1608 xfs_iext_get_extent(ifp
, bma
->idx
+ 1, &RIGHT
);
1610 if (isnullstartblock(RIGHT
.br_startblock
))
1611 state
|= BMAP_RIGHT_DELAY
;
1614 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
1615 new_endoff
== RIGHT
.br_startoff
&&
1616 new->br_startblock
+ new->br_blockcount
== RIGHT
.br_startblock
&&
1617 new->br_state
== RIGHT
.br_state
&&
1618 new->br_blockcount
+ RIGHT
.br_blockcount
<= MAXEXTLEN
&&
1619 ((state
& (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
1620 BMAP_RIGHT_FILLING
)) !=
1621 (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
1622 BMAP_RIGHT_FILLING
) ||
1623 LEFT
.br_blockcount
+ new->br_blockcount
+ RIGHT
.br_blockcount
1625 state
|= BMAP_RIGHT_CONTIG
;
1629 * Switch out based on the FILLING and CONTIG state bits.
1631 switch (state
& (BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
1632 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
)) {
1633 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
1634 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
1636 * Filling in all of a previously delayed allocation extent.
1637 * The left and right neighbors are both contiguous with new.
1640 LEFT
.br_blockcount
+= PREV
.br_blockcount
+ RIGHT
.br_blockcount
;
1641 xfs_iext_update_extent(bma
->ip
, state
, bma
->idx
, &LEFT
);
1643 xfs_iext_remove(bma
->ip
, bma
->idx
+ 1, 2, state
);
1645 if (bma
->cur
== NULL
)
1646 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1648 rval
= XFS_ILOG_CORE
;
1649 error
= xfs_bmbt_lookup_eq(bma
->cur
, &RIGHT
, &i
);
1652 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1653 error
= xfs_btree_delete(bma
->cur
, &i
);
1656 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1657 error
= xfs_btree_decrement(bma
->cur
, 0, &i
);
1660 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1661 error
= xfs_bmbt_update(bma
->cur
, &LEFT
);
1667 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
1669 * Filling in all of a previously delayed allocation extent.
1670 * The left neighbor is contiguous, the right is not.
1675 LEFT
.br_blockcount
+= PREV
.br_blockcount
;
1676 xfs_iext_update_extent(bma
->ip
, state
, bma
->idx
, &LEFT
);
1678 xfs_iext_remove(bma
->ip
, bma
->idx
+ 1, 1, state
);
1679 if (bma
->cur
== NULL
)
1680 rval
= XFS_ILOG_DEXT
;
1683 error
= xfs_bmbt_lookup_eq(bma
->cur
, &old
, &i
);
1686 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1687 error
= xfs_bmbt_update(bma
->cur
, &LEFT
);
1693 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
1695 * Filling in all of a previously delayed allocation extent.
1696 * The right neighbor is contiguous, the left is not.
1698 PREV
.br_startblock
= new->br_startblock
;
1699 PREV
.br_blockcount
+= RIGHT
.br_blockcount
;
1700 xfs_iext_update_extent(bma
->ip
, state
, bma
->idx
, &PREV
);
1702 xfs_iext_remove(bma
->ip
, bma
->idx
+ 1, 1, state
);
1703 if (bma
->cur
== NULL
)
1704 rval
= XFS_ILOG_DEXT
;
1707 error
= xfs_bmbt_lookup_eq(bma
->cur
, &RIGHT
, &i
);
1710 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1711 error
= xfs_bmbt_update(bma
->cur
, &PREV
);
1717 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
1719 * Filling in all of a previously delayed allocation extent.
1720 * Neither the left nor right neighbors are contiguous with
1723 PREV
.br_startblock
= new->br_startblock
;
1724 PREV
.br_state
= new->br_state
;
1725 xfs_iext_update_extent(bma
->ip
, state
, bma
->idx
, &PREV
);
1728 if (bma
->cur
== NULL
)
1729 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1731 rval
= XFS_ILOG_CORE
;
1732 error
= xfs_bmbt_lookup_eq(bma
->cur
, new, &i
);
1735 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
1736 error
= xfs_btree_insert(bma
->cur
, &i
);
1739 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1743 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
:
1745 * Filling in the first part of a previous delayed allocation.
1746 * The left neighbor is contiguous.
1749 temp
= PREV
.br_blockcount
- new->br_blockcount
;
1750 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
1751 startblockval(PREV
.br_startblock
));
1753 LEFT
.br_blockcount
+= new->br_blockcount
;
1754 xfs_iext_update_extent(bma
->ip
, state
, bma
->idx
- 1, &LEFT
);
1756 PREV
.br_blockcount
= temp
= PREV
.br_blockcount
- new->br_blockcount
;
1757 PREV
.br_startoff
+= new->br_blockcount
;
1758 PREV
.br_startblock
= nullstartblock(da_new
);
1759 xfs_iext_update_extent(bma
->ip
, state
, bma
->idx
, &PREV
);
1761 if (bma
->cur
== NULL
)
1762 rval
= XFS_ILOG_DEXT
;
1765 error
= xfs_bmbt_lookup_eq(bma
->cur
, &old
, &i
);
1768 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1769 error
= xfs_bmbt_update(bma
->cur
, &LEFT
);
1777 case BMAP_LEFT_FILLING
:
1779 * Filling in the first part of a previous delayed allocation.
1780 * The left neighbor is not contiguous.
1782 xfs_iext_insert(bma
->ip
, bma
->idx
, 1, new, state
);
1784 if (bma
->cur
== NULL
)
1785 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1787 rval
= XFS_ILOG_CORE
;
1788 error
= xfs_bmbt_lookup_eq(bma
->cur
, new, &i
);
1791 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
1792 error
= xfs_btree_insert(bma
->cur
, &i
);
1795 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1798 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
1799 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
1800 bma
->firstblock
, bma
->dfops
,
1801 &bma
->cur
, 1, &tmp_rval
, whichfork
);
1807 temp
= PREV
.br_blockcount
- new->br_blockcount
;
1808 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
1809 startblockval(PREV
.br_startblock
) -
1810 (bma
->cur
? bma
->cur
->bc_private
.b
.allocated
: 0));
1812 PREV
.br_startoff
= new_endoff
;
1813 PREV
.br_blockcount
= temp
;
1814 PREV
.br_startblock
= nullstartblock(da_new
);
1815 xfs_iext_update_extent(bma
->ip
, state
, bma
->idx
+ 1, &PREV
);
1818 case BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
1820 * Filling in the last part of a previous delayed allocation.
1821 * The right neighbor is contiguous with the new allocation.
1824 RIGHT
.br_startoff
= new->br_startoff
;
1825 RIGHT
.br_startblock
= new->br_startblock
;
1826 RIGHT
.br_blockcount
+= new->br_blockcount
;
1827 xfs_iext_update_extent(bma
->ip
, state
, bma
->idx
+ 1, &RIGHT
);
1829 if (bma
->cur
== NULL
)
1830 rval
= XFS_ILOG_DEXT
;
1833 error
= xfs_bmbt_lookup_eq(bma
->cur
, &old
, &i
);
1836 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1837 error
= xfs_bmbt_update(bma
->cur
, &RIGHT
);
1842 temp
= PREV
.br_blockcount
- new->br_blockcount
;
1843 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
1844 startblockval(PREV
.br_startblock
));
1846 PREV
.br_blockcount
= temp
;
1847 PREV
.br_startblock
= nullstartblock(da_new
);
1848 xfs_iext_update_extent(bma
->ip
, state
, bma
->idx
, &PREV
);
1853 case BMAP_RIGHT_FILLING
:
1855 * Filling in the last part of a previous delayed allocation.
1856 * The right neighbor is not contiguous.
1858 xfs_iext_insert(bma
->ip
, bma
->idx
+ 1, 1, new, state
);
1860 if (bma
->cur
== NULL
)
1861 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1863 rval
= XFS_ILOG_CORE
;
1864 error
= xfs_bmbt_lookup_eq(bma
->cur
, new, &i
);
1867 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
1868 error
= xfs_btree_insert(bma
->cur
, &i
);
1871 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1874 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
1875 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
1876 bma
->firstblock
, bma
->dfops
, &bma
->cur
, 1,
1877 &tmp_rval
, whichfork
);
1883 temp
= PREV
.br_blockcount
- new->br_blockcount
;
1884 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma
->ip
, temp
),
1885 startblockval(PREV
.br_startblock
) -
1886 (bma
->cur
? bma
->cur
->bc_private
.b
.allocated
: 0));
1888 PREV
.br_startblock
= nullstartblock(da_new
);
1889 PREV
.br_blockcount
= temp
;
1890 xfs_iext_update_extent(bma
->ip
, state
, bma
->idx
, &PREV
);
1897 * Filling in the middle part of a previous delayed allocation.
1898 * Contiguity is impossible here.
1899 * This case is avoided almost all the time.
1901 * We start with a delayed allocation:
1903 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
1906 * and we are allocating:
1907 * +rrrrrrrrrrrrrrrrr+
1910 * and we set it up for insertion as:
1911 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
1913 * PREV @ idx LEFT RIGHT
1914 * inserted at idx + 1
1918 /* LEFT is the new middle */
1921 /* RIGHT is the new right */
1922 RIGHT
.br_state
= PREV
.br_state
;
1923 RIGHT
.br_startoff
= new_endoff
;
1924 RIGHT
.br_blockcount
=
1925 PREV
.br_startoff
+ PREV
.br_blockcount
- new_endoff
;
1926 RIGHT
.br_startblock
=
1927 nullstartblock(xfs_bmap_worst_indlen(bma
->ip
,
1928 RIGHT
.br_blockcount
));
1931 PREV
.br_blockcount
= new->br_startoff
- PREV
.br_startoff
;
1932 PREV
.br_startblock
=
1933 nullstartblock(xfs_bmap_worst_indlen(bma
->ip
,
1934 PREV
.br_blockcount
));
1935 xfs_iext_update_extent(bma
->ip
, state
, bma
->idx
, &PREV
);
1937 /* insert LEFT (r[0]) and RIGHT (r[1]) at the same time */
1938 xfs_iext_insert(bma
->ip
, bma
->idx
+ 1, 2, &LEFT
, state
);
1941 if (bma
->cur
== NULL
)
1942 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1944 rval
= XFS_ILOG_CORE
;
1945 error
= xfs_bmbt_lookup_eq(bma
->cur
, new, &i
);
1948 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
1949 error
= xfs_btree_insert(bma
->cur
, &i
);
1952 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
1955 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
1956 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
1957 bma
->firstblock
, bma
->dfops
, &bma
->cur
,
1958 1, &tmp_rval
, whichfork
);
1964 da_new
= startblockval(PREV
.br_startblock
) +
1965 startblockval(RIGHT
.br_startblock
);
1969 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
1970 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
1971 case BMAP_LEFT_FILLING
| BMAP_RIGHT_CONTIG
:
1972 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
1973 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
1974 case BMAP_LEFT_CONTIG
:
1975 case BMAP_RIGHT_CONTIG
:
1977 * These cases are all impossible.
1982 /* add reverse mapping */
1983 error
= xfs_rmap_map_extent(mp
, bma
->dfops
, bma
->ip
, whichfork
, new);
1987 /* convert to a btree if necessary */
1988 if (xfs_bmap_needs_btree(bma
->ip
, whichfork
)) {
1989 int tmp_logflags
; /* partial log flag return val */
1991 ASSERT(bma
->cur
== NULL
);
1992 error
= xfs_bmap_extents_to_btree(bma
->tp
, bma
->ip
,
1993 bma
->firstblock
, bma
->dfops
, &bma
->cur
,
1994 da_old
> 0, &tmp_logflags
, whichfork
);
1995 bma
->logflags
|= tmp_logflags
;
2001 da_new
+= bma
->cur
->bc_private
.b
.allocated
;
2002 bma
->cur
->bc_private
.b
.allocated
= 0;
2005 /* adjust for changes in reserved delayed indirect blocks */
2006 if (da_new
!= da_old
) {
2007 ASSERT(state
== 0 || da_new
< da_old
);
2008 error
= xfs_mod_fdblocks(mp
, (int64_t)(da_old
- da_new
),
2012 xfs_bmap_check_leaf_extents(bma
->cur
, bma
->ip
, whichfork
);
2014 if (whichfork
!= XFS_COW_FORK
)
2015 bma
->logflags
|= rval
;
2023 * Convert an unwritten allocation to a real allocation or vice versa.
2025 STATIC
int /* error */
2026 xfs_bmap_add_extent_unwritten_real(
2027 struct xfs_trans
*tp
,
2028 xfs_inode_t
*ip
, /* incore inode pointer */
2030 xfs_extnum_t
*idx
, /* extent number to update/insert */
2031 xfs_btree_cur_t
**curp
, /* if *curp is null, not a btree */
2032 xfs_bmbt_irec_t
*new, /* new data to add to file extents */
2033 xfs_fsblock_t
*first
, /* pointer to firstblock variable */
2034 struct xfs_defer_ops
*dfops
, /* list of extents to be freed */
2035 int *logflagsp
) /* inode logging flags */
2037 xfs_btree_cur_t
*cur
; /* btree cursor */
2038 int error
; /* error return value */
2039 int i
; /* temp state */
2040 xfs_ifork_t
*ifp
; /* inode fork pointer */
2041 xfs_fileoff_t new_endoff
; /* end offset of new entry */
2042 xfs_bmbt_irec_t r
[3]; /* neighbor extent entries */
2043 /* left is 0, right is 1, prev is 2 */
2044 int rval
=0; /* return value (logging flags) */
2045 int state
= xfs_bmap_fork_to_state(whichfork
);
2046 struct xfs_mount
*mp
= ip
->i_mount
;
2047 struct xfs_bmbt_irec old
;
2052 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
2055 ASSERT(*idx
<= xfs_iext_count(ifp
));
2056 ASSERT(!isnullstartblock(new->br_startblock
));
2058 XFS_STATS_INC(mp
, xs_add_exlist
);
2065 * Set up a bunch of variables to make the tests simpler.
2068 xfs_iext_get_extent(ifp
, *idx
, &PREV
);
2069 ASSERT(new->br_state
!= PREV
.br_state
);
2070 new_endoff
= new->br_startoff
+ new->br_blockcount
;
2071 ASSERT(PREV
.br_startoff
<= new->br_startoff
);
2072 ASSERT(PREV
.br_startoff
+ PREV
.br_blockcount
>= new_endoff
);
2075 * Set flags determining what part of the previous oldext allocation
2076 * extent is being replaced by a newext allocation.
2078 if (PREV
.br_startoff
== new->br_startoff
)
2079 state
|= BMAP_LEFT_FILLING
;
2080 if (PREV
.br_startoff
+ PREV
.br_blockcount
== new_endoff
)
2081 state
|= BMAP_RIGHT_FILLING
;
2084 * Check and set flags if this segment has a left neighbor.
2085 * Don't set contiguous if the combined extent would be too large.
2088 state
|= BMAP_LEFT_VALID
;
2089 xfs_iext_get_extent(ifp
, *idx
- 1, &LEFT
);
2091 if (isnullstartblock(LEFT
.br_startblock
))
2092 state
|= BMAP_LEFT_DELAY
;
2095 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
2096 LEFT
.br_startoff
+ LEFT
.br_blockcount
== new->br_startoff
&&
2097 LEFT
.br_startblock
+ LEFT
.br_blockcount
== new->br_startblock
&&
2098 LEFT
.br_state
== new->br_state
&&
2099 LEFT
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
2100 state
|= BMAP_LEFT_CONTIG
;
2103 * Check and set flags if this segment has a right neighbor.
2104 * Don't set contiguous if the combined extent would be too large.
2105 * Also check for all-three-contiguous being too large.
2107 if (*idx
< xfs_iext_count(ifp
) - 1) {
2108 state
|= BMAP_RIGHT_VALID
;
2109 xfs_iext_get_extent(ifp
, *idx
+ 1, &RIGHT
);
2110 if (isnullstartblock(RIGHT
.br_startblock
))
2111 state
|= BMAP_RIGHT_DELAY
;
2114 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
2115 new_endoff
== RIGHT
.br_startoff
&&
2116 new->br_startblock
+ new->br_blockcount
== RIGHT
.br_startblock
&&
2117 new->br_state
== RIGHT
.br_state
&&
2118 new->br_blockcount
+ RIGHT
.br_blockcount
<= MAXEXTLEN
&&
2119 ((state
& (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
2120 BMAP_RIGHT_FILLING
)) !=
2121 (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
2122 BMAP_RIGHT_FILLING
) ||
2123 LEFT
.br_blockcount
+ new->br_blockcount
+ RIGHT
.br_blockcount
2125 state
|= BMAP_RIGHT_CONTIG
;
2128 * Switch out based on the FILLING and CONTIG state bits.
2130 switch (state
& (BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
2131 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
)) {
2132 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
2133 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2135 * Setting all of a previous oldext extent to newext.
2136 * The left and right neighbors are both contiguous with new.
2140 LEFT
.br_blockcount
+= PREV
.br_blockcount
+ RIGHT
.br_blockcount
;
2141 xfs_iext_update_extent(ip
, state
, *idx
, &LEFT
);
2143 xfs_iext_remove(ip
, *idx
+ 1, 2, state
);
2144 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2145 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 2);
2147 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2149 rval
= XFS_ILOG_CORE
;
2150 error
= xfs_bmbt_lookup_eq(cur
, &RIGHT
, &i
);
2153 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2154 if ((error
= xfs_btree_delete(cur
, &i
)))
2156 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2157 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2159 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2160 if ((error
= xfs_btree_delete(cur
, &i
)))
2162 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2163 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2165 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2166 error
= xfs_bmbt_update(cur
, &LEFT
);
2172 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
2174 * Setting all of a previous oldext extent to newext.
2175 * The left neighbor is contiguous, the right is not.
2179 LEFT
.br_blockcount
+= PREV
.br_blockcount
;
2180 xfs_iext_update_extent(ip
, state
, *idx
, &LEFT
);
2182 xfs_iext_remove(ip
, *idx
+ 1, 1, state
);
2183 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2184 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
2186 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2188 rval
= XFS_ILOG_CORE
;
2189 error
= xfs_bmbt_lookup_eq(cur
, &PREV
, &i
);
2192 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2193 if ((error
= xfs_btree_delete(cur
, &i
)))
2195 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2196 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2198 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2199 error
= xfs_bmbt_update(cur
, &LEFT
);
2205 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2207 * Setting all of a previous oldext extent to newext.
2208 * The right neighbor is contiguous, the left is not.
2210 PREV
.br_blockcount
+= RIGHT
.br_blockcount
;
2211 PREV
.br_state
= new->br_state
;
2212 xfs_iext_update_extent(ip
, state
, *idx
, &PREV
);
2214 xfs_iext_remove(ip
, *idx
+ 1, 1, state
);
2215 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2216 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
2218 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2220 rval
= XFS_ILOG_CORE
;
2221 error
= xfs_bmbt_lookup_eq(cur
, &RIGHT
, &i
);
2224 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2225 if ((error
= xfs_btree_delete(cur
, &i
)))
2227 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2228 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
2230 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2231 error
= xfs_bmbt_update(cur
, &PREV
);
2237 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
2239 * Setting all of a previous oldext extent to newext.
2240 * Neither the left nor right neighbors are contiguous with
2243 PREV
.br_state
= new->br_state
;
2244 xfs_iext_update_extent(ip
, state
, *idx
, &PREV
);
2247 rval
= XFS_ILOG_DEXT
;
2250 error
= xfs_bmbt_lookup_eq(cur
, new, &i
);
2253 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2254 error
= xfs_bmbt_update(cur
, &PREV
);
2260 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
:
2262 * Setting the first part of a previous oldext extent to newext.
2263 * The left neighbor is contiguous.
2265 LEFT
.br_blockcount
+= new->br_blockcount
;
2266 xfs_iext_update_extent(ip
, state
, *idx
- 1, &LEFT
);
2269 PREV
.br_startoff
+= new->br_blockcount
;
2270 PREV
.br_startblock
+= new->br_blockcount
;
2271 PREV
.br_blockcount
-= new->br_blockcount
;
2272 xfs_iext_update_extent(ip
, state
, *idx
, &PREV
);
2277 rval
= XFS_ILOG_DEXT
;
2280 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2283 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2284 error
= xfs_bmbt_update(cur
, &PREV
);
2287 error
= xfs_btree_decrement(cur
, 0, &i
);
2290 error
= xfs_bmbt_update(cur
, &LEFT
);
2296 case BMAP_LEFT_FILLING
:
2298 * Setting the first part of a previous oldext extent to newext.
2299 * The left neighbor is not contiguous.
2302 PREV
.br_startoff
+= new->br_blockcount
;
2303 PREV
.br_startblock
+= new->br_blockcount
;
2304 PREV
.br_blockcount
-= new->br_blockcount
;
2305 xfs_iext_update_extent(ip
, state
, *idx
, &PREV
);
2307 xfs_iext_insert(ip
, *idx
, 1, new, state
);
2308 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2309 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
2311 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2313 rval
= XFS_ILOG_CORE
;
2314 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2317 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2318 error
= xfs_bmbt_update(cur
, &PREV
);
2321 cur
->bc_rec
.b
= *new;
2322 if ((error
= xfs_btree_insert(cur
, &i
)))
2324 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2328 case BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
2330 * Setting the last part of a previous oldext extent to newext.
2331 * The right neighbor is contiguous with the new allocation.
2334 PREV
.br_blockcount
-= new->br_blockcount
;
2335 xfs_iext_update_extent(ip
, state
, *idx
, &PREV
);
2339 RIGHT
.br_startoff
= new->br_startoff
;
2340 RIGHT
.br_startblock
= new->br_startblock
;
2341 RIGHT
.br_blockcount
+= new->br_blockcount
;
2342 xfs_iext_update_extent(ip
, state
, *idx
, &RIGHT
);
2345 rval
= XFS_ILOG_DEXT
;
2348 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2351 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2352 error
= xfs_bmbt_update(cur
, &PREV
);
2355 error
= xfs_btree_increment(cur
, 0, &i
);
2358 error
= xfs_bmbt_update(cur
, &RIGHT
);
2364 case BMAP_RIGHT_FILLING
:
2366 * Setting the last part of a previous oldext extent to newext.
2367 * The right neighbor is not contiguous.
2370 PREV
.br_blockcount
-= new->br_blockcount
;
2371 xfs_iext_update_extent(ip
, state
, *idx
, &PREV
);
2374 xfs_iext_insert(ip
, *idx
, 1, new, state
);
2376 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2377 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
2379 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2381 rval
= XFS_ILOG_CORE
;
2382 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2385 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2386 error
= xfs_bmbt_update(cur
, &PREV
);
2389 error
= xfs_bmbt_lookup_eq(cur
, new, &i
);
2392 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
2393 if ((error
= xfs_btree_insert(cur
, &i
)))
2395 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2401 * Setting the middle part of a previous oldext extent to
2402 * newext. Contiguity is impossible here.
2403 * One extent becomes three extents.
2406 PREV
.br_blockcount
= new->br_startoff
- PREV
.br_startoff
;
2407 xfs_iext_update_extent(ip
, state
, *idx
, &PREV
);
2410 r
[1].br_startoff
= new_endoff
;
2411 r
[1].br_blockcount
=
2412 old
.br_startoff
+ old
.br_blockcount
- new_endoff
;
2413 r
[1].br_startblock
= new->br_startblock
+ new->br_blockcount
;
2414 r
[1].br_state
= PREV
.br_state
;
2417 xfs_iext_insert(ip
, *idx
, 2, &r
[0], state
);
2419 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2420 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 2);
2422 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
2424 rval
= XFS_ILOG_CORE
;
2425 error
= xfs_bmbt_lookup_eq(cur
, &old
, &i
);
2428 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2429 /* new right extent - oldext */
2430 error
= xfs_bmbt_update(cur
, &r
[1]);
2433 /* new left extent - oldext */
2434 cur
->bc_rec
.b
= PREV
;
2435 if ((error
= xfs_btree_insert(cur
, &i
)))
2437 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2439 * Reset the cursor to the position of the new extent
2440 * we are about to insert as we can't trust it after
2441 * the previous insert.
2443 error
= xfs_bmbt_lookup_eq(cur
, new, &i
);
2446 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, done
);
2447 /* new middle extent - newext */
2448 if ((error
= xfs_btree_insert(cur
, &i
)))
2450 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
2454 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2455 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2456 case BMAP_LEFT_FILLING
| BMAP_RIGHT_CONTIG
:
2457 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
2458 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2459 case BMAP_LEFT_CONTIG
:
2460 case BMAP_RIGHT_CONTIG
:
2462 * These cases are all impossible.
2467 /* update reverse mappings */
2468 error
= xfs_rmap_convert_extent(mp
, dfops
, ip
, whichfork
, new);
2472 /* convert to a btree if necessary */
2473 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
2474 int tmp_logflags
; /* partial log flag return val */
2476 ASSERT(cur
== NULL
);
2477 error
= xfs_bmap_extents_to_btree(tp
, ip
, first
, dfops
, &cur
,
2478 0, &tmp_logflags
, whichfork
);
2479 *logflagsp
|= tmp_logflags
;
2484 /* clear out the allocated field, done with it now in any case. */
2486 cur
->bc_private
.b
.allocated
= 0;
2490 xfs_bmap_check_leaf_extents(*curp
, ip
, whichfork
);
2500 * Convert a hole to a delayed allocation.
2503 xfs_bmap_add_extent_hole_delay(
2504 xfs_inode_t
*ip
, /* incore inode pointer */
2506 xfs_extnum_t
*idx
, /* extent number to update/insert */
2507 xfs_bmbt_irec_t
*new) /* new data to add to file extents */
2509 xfs_ifork_t
*ifp
; /* inode fork pointer */
2510 xfs_bmbt_irec_t left
; /* left neighbor extent entry */
2511 xfs_filblks_t newlen
=0; /* new indirect size */
2512 xfs_filblks_t oldlen
=0; /* old indirect size */
2513 xfs_bmbt_irec_t right
; /* right neighbor extent entry */
2514 int state
= xfs_bmap_fork_to_state(whichfork
);
2515 xfs_filblks_t temp
; /* temp for indirect calculations */
2517 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
2518 ASSERT(isnullstartblock(new->br_startblock
));
2521 * Check and set flags if this segment has a left neighbor
2524 state
|= BMAP_LEFT_VALID
;
2525 xfs_iext_get_extent(ifp
, *idx
- 1, &left
);
2527 if (isnullstartblock(left
.br_startblock
))
2528 state
|= BMAP_LEFT_DELAY
;
2532 * Check and set flags if the current (right) segment exists.
2533 * If it doesn't exist, we're converting the hole at end-of-file.
2535 if (*idx
< xfs_iext_count(ifp
)) {
2536 state
|= BMAP_RIGHT_VALID
;
2537 xfs_iext_get_extent(ifp
, *idx
, &right
);
2539 if (isnullstartblock(right
.br_startblock
))
2540 state
|= BMAP_RIGHT_DELAY
;
2544 * Set contiguity flags on the left and right neighbors.
2545 * Don't let extents get too large, even if the pieces are contiguous.
2547 if ((state
& BMAP_LEFT_VALID
) && (state
& BMAP_LEFT_DELAY
) &&
2548 left
.br_startoff
+ left
.br_blockcount
== new->br_startoff
&&
2549 left
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
2550 state
|= BMAP_LEFT_CONTIG
;
2552 if ((state
& BMAP_RIGHT_VALID
) && (state
& BMAP_RIGHT_DELAY
) &&
2553 new->br_startoff
+ new->br_blockcount
== right
.br_startoff
&&
2554 new->br_blockcount
+ right
.br_blockcount
<= MAXEXTLEN
&&
2555 (!(state
& BMAP_LEFT_CONTIG
) ||
2556 (left
.br_blockcount
+ new->br_blockcount
+
2557 right
.br_blockcount
<= MAXEXTLEN
)))
2558 state
|= BMAP_RIGHT_CONTIG
;
2561 * Switch out based on the contiguity flags.
2563 switch (state
& (BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
)) {
2564 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
2566 * New allocation is contiguous with delayed allocations
2567 * on the left and on the right.
2568 * Merge all three into a single extent record.
2571 temp
= left
.br_blockcount
+ new->br_blockcount
+
2572 right
.br_blockcount
;
2574 oldlen
= startblockval(left
.br_startblock
) +
2575 startblockval(new->br_startblock
) +
2576 startblockval(right
.br_startblock
);
2577 newlen
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
2579 left
.br_startblock
= nullstartblock(newlen
);
2580 left
.br_blockcount
= temp
;
2581 xfs_iext_update_extent(ip
, state
, *idx
, &left
);
2583 xfs_iext_remove(ip
, *idx
+ 1, 1, state
);
2586 case BMAP_LEFT_CONTIG
:
2588 * New allocation is contiguous with a delayed allocation
2590 * Merge the new allocation with the left neighbor.
2593 temp
= left
.br_blockcount
+ new->br_blockcount
;
2595 oldlen
= startblockval(left
.br_startblock
) +
2596 startblockval(new->br_startblock
);
2597 newlen
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
2599 left
.br_blockcount
= temp
;
2600 left
.br_startblock
= nullstartblock(newlen
);
2601 xfs_iext_update_extent(ip
, state
, *idx
, &left
);
2604 case BMAP_RIGHT_CONTIG
:
2606 * New allocation is contiguous with a delayed allocation
2608 * Merge the new allocation with the right neighbor.
2610 temp
= new->br_blockcount
+ right
.br_blockcount
;
2611 oldlen
= startblockval(new->br_startblock
) +
2612 startblockval(right
.br_startblock
);
2613 newlen
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
2615 right
.br_startoff
= new->br_startoff
;
2616 right
.br_startblock
= nullstartblock(newlen
);
2617 right
.br_blockcount
= temp
;
2618 xfs_iext_update_extent(ip
, state
, *idx
, &right
);
2623 * New allocation is not contiguous with another
2624 * delayed allocation.
2625 * Insert a new entry.
2627 oldlen
= newlen
= 0;
2628 xfs_iext_insert(ip
, *idx
, 1, new, state
);
2631 if (oldlen
!= newlen
) {
2632 ASSERT(oldlen
> newlen
);
2633 xfs_mod_fdblocks(ip
->i_mount
, (int64_t)(oldlen
- newlen
),
2636 * Nothing to do for disk quota accounting here.
2642 * Convert a hole to a real allocation.
2644 STATIC
int /* error */
2645 xfs_bmap_add_extent_hole_real(
2646 struct xfs_trans
*tp
,
2647 struct xfs_inode
*ip
,
2650 struct xfs_btree_cur
**curp
,
2651 struct xfs_bmbt_irec
*new,
2652 xfs_fsblock_t
*first
,
2653 struct xfs_defer_ops
*dfops
,
2656 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
2657 struct xfs_mount
*mp
= ip
->i_mount
;
2658 struct xfs_btree_cur
*cur
= *curp
;
2659 int error
; /* error return value */
2660 int i
; /* temp state */
2661 xfs_bmbt_irec_t left
; /* left neighbor extent entry */
2662 xfs_bmbt_irec_t right
; /* right neighbor extent entry */
2663 int rval
=0; /* return value (logging flags) */
2664 int state
= xfs_bmap_fork_to_state(whichfork
);
2665 struct xfs_bmbt_irec old
;
2668 ASSERT(*idx
<= xfs_iext_count(ifp
));
2669 ASSERT(!isnullstartblock(new->br_startblock
));
2670 ASSERT(!cur
|| !(cur
->bc_private
.b
.flags
& XFS_BTCUR_BPRV_WASDEL
));
2672 XFS_STATS_INC(mp
, xs_add_exlist
);
2675 * Check and set flags if this segment has a left neighbor.
2678 state
|= BMAP_LEFT_VALID
;
2679 xfs_iext_get_extent(ifp
, *idx
- 1, &left
);
2680 if (isnullstartblock(left
.br_startblock
))
2681 state
|= BMAP_LEFT_DELAY
;
2685 * Check and set flags if this segment has a current value.
2686 * Not true if we're inserting into the "hole" at eof.
2688 if (*idx
< xfs_iext_count(ifp
)) {
2689 state
|= BMAP_RIGHT_VALID
;
2690 xfs_iext_get_extent(ifp
, *idx
, &right
);
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.
2728 left
.br_blockcount
+= new->br_blockcount
+ right
.br_blockcount
;
2729 xfs_iext_update_extent(ip
, state
, *idx
, &left
);
2731 xfs_iext_remove(ip
, *idx
+ 1, 1, state
);
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.
2766 left
.br_blockcount
+= new->br_blockcount
;
2767 xfs_iext_update_extent(ip
, state
, *idx
, &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
, *idx
, &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
, *idx
, 1, 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 */
2836 error
= xfs_rmap_map_extent(mp
, dfops
, ip
, whichfork
, new);
2840 /* convert to a btree if necessary */
2841 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
2842 int tmp_logflags
; /* partial log flag return val */
2844 ASSERT(cur
== NULL
);
2845 error
= xfs_bmap_extents_to_btree(tp
, ip
, first
, dfops
, curp
,
2846 0, &tmp_logflags
, whichfork
);
2847 *logflagsp
|= tmp_logflags
;
2853 /* clear out the allocated field, done with it now in any case. */
2855 cur
->bc_private
.b
.allocated
= 0;
2857 xfs_bmap_check_leaf_extents(cur
, ip
, whichfork
);
2864 * Functions used in the extent read, allocate and remove paths
2868 * Adjust the size of the new extent based on di_extsize and rt extsize.
2871 xfs_bmap_extsize_align(
2873 xfs_bmbt_irec_t
*gotp
, /* next extent pointer */
2874 xfs_bmbt_irec_t
*prevp
, /* previous extent pointer */
2875 xfs_extlen_t extsz
, /* align to this extent size */
2876 int rt
, /* is this a realtime inode? */
2877 int eof
, /* is extent at end-of-file? */
2878 int delay
, /* creating delalloc extent? */
2879 int convert
, /* overwriting unwritten extent? */
2880 xfs_fileoff_t
*offp
, /* in/out: aligned offset */
2881 xfs_extlen_t
*lenp
) /* in/out: aligned length */
2883 xfs_fileoff_t orig_off
; /* original offset */
2884 xfs_extlen_t orig_alen
; /* original length */
2885 xfs_fileoff_t orig_end
; /* original off+len */
2886 xfs_fileoff_t nexto
; /* next file offset */
2887 xfs_fileoff_t prevo
; /* previous file offset */
2888 xfs_fileoff_t align_off
; /* temp for offset */
2889 xfs_extlen_t align_alen
; /* temp for length */
2890 xfs_extlen_t temp
; /* temp for calculations */
2895 orig_off
= align_off
= *offp
;
2896 orig_alen
= align_alen
= *lenp
;
2897 orig_end
= orig_off
+ orig_alen
;
2900 * If this request overlaps an existing extent, then don't
2901 * attempt to perform any additional alignment.
2903 if (!delay
&& !eof
&&
2904 (orig_off
>= gotp
->br_startoff
) &&
2905 (orig_end
<= gotp
->br_startoff
+ gotp
->br_blockcount
)) {
2910 * If the file offset is unaligned vs. the extent size
2911 * we need to align it. This will be possible unless
2912 * the file was previously written with a kernel that didn't
2913 * perform this alignment, or if a truncate shot us in the
2916 temp
= do_mod(orig_off
, extsz
);
2922 /* Same adjustment for the end of the requested area. */
2923 temp
= (align_alen
% extsz
);
2925 align_alen
+= extsz
- temp
;
2928 * For large extent hint sizes, the aligned extent might be larger than
2929 * MAXEXTLEN. In that case, reduce the size by an extsz so that it pulls
2930 * the length back under MAXEXTLEN. The outer allocation loops handle
2931 * short allocation just fine, so it is safe to do this. We only want to
2932 * do it when we are forced to, though, because it means more allocation
2933 * operations are required.
2935 while (align_alen
> MAXEXTLEN
)
2936 align_alen
-= extsz
;
2937 ASSERT(align_alen
<= MAXEXTLEN
);
2940 * If the previous block overlaps with this proposed allocation
2941 * then move the start forward without adjusting the length.
2943 if (prevp
->br_startoff
!= NULLFILEOFF
) {
2944 if (prevp
->br_startblock
== HOLESTARTBLOCK
)
2945 prevo
= prevp
->br_startoff
;
2947 prevo
= prevp
->br_startoff
+ prevp
->br_blockcount
;
2950 if (align_off
!= orig_off
&& align_off
< prevo
)
2953 * If the next block overlaps with this proposed allocation
2954 * then move the start back without adjusting the length,
2955 * but not before offset 0.
2956 * This may of course make the start overlap previous block,
2957 * and if we hit the offset 0 limit then the next block
2958 * can still overlap too.
2960 if (!eof
&& gotp
->br_startoff
!= NULLFILEOFF
) {
2961 if ((delay
&& gotp
->br_startblock
== HOLESTARTBLOCK
) ||
2962 (!delay
&& gotp
->br_startblock
== DELAYSTARTBLOCK
))
2963 nexto
= gotp
->br_startoff
+ gotp
->br_blockcount
;
2965 nexto
= gotp
->br_startoff
;
2967 nexto
= NULLFILEOFF
;
2969 align_off
+ align_alen
!= orig_end
&&
2970 align_off
+ align_alen
> nexto
)
2971 align_off
= nexto
> align_alen
? nexto
- align_alen
: 0;
2973 * If we're now overlapping the next or previous extent that
2974 * means we can't fit an extsz piece in this hole. Just move
2975 * the start forward to the first valid spot and set
2976 * the length so we hit the end.
2978 if (align_off
!= orig_off
&& align_off
< prevo
)
2980 if (align_off
+ align_alen
!= orig_end
&&
2981 align_off
+ align_alen
> nexto
&&
2982 nexto
!= NULLFILEOFF
) {
2983 ASSERT(nexto
> prevo
);
2984 align_alen
= nexto
- align_off
;
2988 * If realtime, and the result isn't a multiple of the realtime
2989 * extent size we need to remove blocks until it is.
2991 if (rt
&& (temp
= (align_alen
% mp
->m_sb
.sb_rextsize
))) {
2993 * We're not covering the original request, or
2994 * we won't be able to once we fix the length.
2996 if (orig_off
< align_off
||
2997 orig_end
> align_off
+ align_alen
||
2998 align_alen
- temp
< orig_alen
)
3001 * Try to fix it by moving the start up.
3003 if (align_off
+ temp
<= orig_off
) {
3008 * Try to fix it by moving the end in.
3010 else if (align_off
+ align_alen
- temp
>= orig_end
)
3013 * Set the start to the minimum then trim the length.
3016 align_alen
-= orig_off
- align_off
;
3017 align_off
= orig_off
;
3018 align_alen
-= align_alen
% mp
->m_sb
.sb_rextsize
;
3021 * Result doesn't cover the request, fail it.
3023 if (orig_off
< align_off
|| orig_end
> align_off
+ align_alen
)
3026 ASSERT(orig_off
>= align_off
);
3027 /* see MAXEXTLEN handling above */
3028 ASSERT(orig_end
<= align_off
+ align_alen
||
3029 align_alen
+ extsz
> MAXEXTLEN
);
3033 if (!eof
&& gotp
->br_startoff
!= NULLFILEOFF
)
3034 ASSERT(align_off
+ align_alen
<= gotp
->br_startoff
);
3035 if (prevp
->br_startoff
!= NULLFILEOFF
)
3036 ASSERT(align_off
>= prevp
->br_startoff
+ prevp
->br_blockcount
);
3044 #define XFS_ALLOC_GAP_UNITS 4
3048 struct xfs_bmalloca
*ap
) /* bmap alloc argument struct */
3050 xfs_fsblock_t adjust
; /* adjustment to block numbers */
3051 xfs_agnumber_t fb_agno
; /* ag number of ap->firstblock */
3052 xfs_mount_t
*mp
; /* mount point structure */
3053 int nullfb
; /* true if ap->firstblock isn't set */
3054 int rt
; /* true if inode is realtime */
3056 #define ISVALID(x,y) \
3058 (x) < mp->m_sb.sb_rblocks : \
3059 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
3060 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
3061 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
3063 mp
= ap
->ip
->i_mount
;
3064 nullfb
= *ap
->firstblock
== NULLFSBLOCK
;
3065 rt
= XFS_IS_REALTIME_INODE(ap
->ip
) &&
3066 xfs_alloc_is_userdata(ap
->datatype
);
3067 fb_agno
= nullfb
? NULLAGNUMBER
: XFS_FSB_TO_AGNO(mp
, *ap
->firstblock
);
3069 * If allocating at eof, and there's a previous real block,
3070 * try to use its last block as our starting point.
3072 if (ap
->eof
&& ap
->prev
.br_startoff
!= NULLFILEOFF
&&
3073 !isnullstartblock(ap
->prev
.br_startblock
) &&
3074 ISVALID(ap
->prev
.br_startblock
+ ap
->prev
.br_blockcount
,
3075 ap
->prev
.br_startblock
)) {
3076 ap
->blkno
= ap
->prev
.br_startblock
+ ap
->prev
.br_blockcount
;
3078 * Adjust for the gap between prevp and us.
3080 adjust
= ap
->offset
-
3081 (ap
->prev
.br_startoff
+ ap
->prev
.br_blockcount
);
3083 ISVALID(ap
->blkno
+ adjust
, ap
->prev
.br_startblock
))
3084 ap
->blkno
+= adjust
;
3087 * If not at eof, then compare the two neighbor blocks.
3088 * Figure out whether either one gives us a good starting point,
3089 * and pick the better one.
3091 else if (!ap
->eof
) {
3092 xfs_fsblock_t gotbno
; /* right side block number */
3093 xfs_fsblock_t gotdiff
=0; /* right side difference */
3094 xfs_fsblock_t prevbno
; /* left side block number */
3095 xfs_fsblock_t prevdiff
=0; /* left side difference */
3098 * If there's a previous (left) block, select a requested
3099 * start block based on it.
3101 if (ap
->prev
.br_startoff
!= NULLFILEOFF
&&
3102 !isnullstartblock(ap
->prev
.br_startblock
) &&
3103 (prevbno
= ap
->prev
.br_startblock
+
3104 ap
->prev
.br_blockcount
) &&
3105 ISVALID(prevbno
, ap
->prev
.br_startblock
)) {
3107 * Calculate gap to end of previous block.
3109 adjust
= prevdiff
= ap
->offset
-
3110 (ap
->prev
.br_startoff
+
3111 ap
->prev
.br_blockcount
);
3113 * Figure the startblock based on the previous block's
3114 * end and the gap size.
3116 * If the gap is large relative to the piece we're
3117 * allocating, or using it gives us an invalid block
3118 * number, then just use the end of the previous block.
3120 if (prevdiff
<= XFS_ALLOC_GAP_UNITS
* ap
->length
&&
3121 ISVALID(prevbno
+ prevdiff
,
3122 ap
->prev
.br_startblock
))
3127 * If the firstblock forbids it, can't use it,
3130 if (!rt
&& !nullfb
&&
3131 XFS_FSB_TO_AGNO(mp
, prevbno
) != fb_agno
)
3132 prevbno
= NULLFSBLOCK
;
3135 * No previous block or can't follow it, just default.
3138 prevbno
= NULLFSBLOCK
;
3140 * If there's a following (right) block, select a requested
3141 * start block based on it.
3143 if (!isnullstartblock(ap
->got
.br_startblock
)) {
3145 * Calculate gap to start of next block.
3147 adjust
= gotdiff
= ap
->got
.br_startoff
- ap
->offset
;
3149 * Figure the startblock based on the next block's
3150 * start and the gap size.
3152 gotbno
= ap
->got
.br_startblock
;
3155 * If the gap is large relative to the piece we're
3156 * allocating, or using it gives us an invalid block
3157 * number, then just use the start of the next block
3158 * offset by our length.
3160 if (gotdiff
<= XFS_ALLOC_GAP_UNITS
* ap
->length
&&
3161 ISVALID(gotbno
- gotdiff
, gotbno
))
3163 else if (ISVALID(gotbno
- ap
->length
, gotbno
)) {
3164 gotbno
-= ap
->length
;
3165 gotdiff
+= adjust
- ap
->length
;
3169 * If the firstblock forbids it, can't use it,
3172 if (!rt
&& !nullfb
&&
3173 XFS_FSB_TO_AGNO(mp
, gotbno
) != fb_agno
)
3174 gotbno
= NULLFSBLOCK
;
3177 * No next block, just default.
3180 gotbno
= NULLFSBLOCK
;
3182 * If both valid, pick the better one, else the only good
3183 * one, else ap->blkno is already set (to 0 or the inode block).
3185 if (prevbno
!= NULLFSBLOCK
&& gotbno
!= NULLFSBLOCK
)
3186 ap
->blkno
= prevdiff
<= gotdiff
? prevbno
: gotbno
;
3187 else if (prevbno
!= NULLFSBLOCK
)
3188 ap
->blkno
= prevbno
;
3189 else if (gotbno
!= NULLFSBLOCK
)
3196 xfs_bmap_longest_free_extent(
3197 struct xfs_trans
*tp
,
3202 struct xfs_mount
*mp
= tp
->t_mountp
;
3203 struct xfs_perag
*pag
;
3204 xfs_extlen_t longest
;
3207 pag
= xfs_perag_get(mp
, ag
);
3208 if (!pag
->pagf_init
) {
3209 error
= xfs_alloc_pagf_init(mp
, tp
, ag
, XFS_ALLOC_FLAG_TRYLOCK
);
3213 if (!pag
->pagf_init
) {
3219 longest
= xfs_alloc_longest_free_extent(mp
, pag
,
3220 xfs_alloc_min_freelist(mp
, pag
),
3221 xfs_ag_resv_needed(pag
, XFS_AG_RESV_NONE
));
3222 if (*blen
< longest
)
3231 xfs_bmap_select_minlen(
3232 struct xfs_bmalloca
*ap
,
3233 struct xfs_alloc_arg
*args
,
3237 if (notinit
|| *blen
< ap
->minlen
) {
3239 * Since we did a BUF_TRYLOCK above, it is possible that
3240 * there is space for this request.
3242 args
->minlen
= ap
->minlen
;
3243 } else if (*blen
< args
->maxlen
) {
3245 * If the best seen length is less than the request length,
3246 * use the best as the minimum.
3248 args
->minlen
= *blen
;
3251 * Otherwise we've seen an extent as big as maxlen, use that
3254 args
->minlen
= args
->maxlen
;
3259 xfs_bmap_btalloc_nullfb(
3260 struct xfs_bmalloca
*ap
,
3261 struct xfs_alloc_arg
*args
,
3264 struct xfs_mount
*mp
= ap
->ip
->i_mount
;
3265 xfs_agnumber_t ag
, startag
;
3269 args
->type
= XFS_ALLOCTYPE_START_BNO
;
3270 args
->total
= ap
->total
;
3272 startag
= ag
= XFS_FSB_TO_AGNO(mp
, args
->fsbno
);
3273 if (startag
== NULLAGNUMBER
)
3276 while (*blen
< args
->maxlen
) {
3277 error
= xfs_bmap_longest_free_extent(args
->tp
, ag
, blen
,
3282 if (++ag
== mp
->m_sb
.sb_agcount
)
3288 xfs_bmap_select_minlen(ap
, args
, blen
, notinit
);
3293 xfs_bmap_btalloc_filestreams(
3294 struct xfs_bmalloca
*ap
,
3295 struct xfs_alloc_arg
*args
,
3298 struct xfs_mount
*mp
= ap
->ip
->i_mount
;
3303 args
->type
= XFS_ALLOCTYPE_NEAR_BNO
;
3304 args
->total
= ap
->total
;
3306 ag
= XFS_FSB_TO_AGNO(mp
, args
->fsbno
);
3307 if (ag
== NULLAGNUMBER
)
3310 error
= xfs_bmap_longest_free_extent(args
->tp
, ag
, blen
, ¬init
);
3314 if (*blen
< args
->maxlen
) {
3315 error
= xfs_filestream_new_ag(ap
, &ag
);
3319 error
= xfs_bmap_longest_free_extent(args
->tp
, ag
, blen
,
3326 xfs_bmap_select_minlen(ap
, args
, blen
, notinit
);
3329 * Set the failure fallback case to look in the selected AG as stream
3332 ap
->blkno
= args
->fsbno
= XFS_AGB_TO_FSB(mp
, ag
, 0);
3338 struct xfs_bmalloca
*ap
) /* bmap alloc argument struct */
3340 xfs_mount_t
*mp
; /* mount point structure */
3341 xfs_alloctype_t atype
= 0; /* type for allocation routines */
3342 xfs_extlen_t align
= 0; /* minimum allocation alignment */
3343 xfs_agnumber_t fb_agno
; /* ag number of ap->firstblock */
3345 xfs_alloc_arg_t args
;
3347 xfs_extlen_t nextminlen
= 0;
3348 int nullfb
; /* true if ap->firstblock isn't set */
3356 mp
= ap
->ip
->i_mount
;
3358 /* stripe alignment for allocation is determined by mount parameters */
3360 if (mp
->m_swidth
&& (mp
->m_flags
& XFS_MOUNT_SWALLOC
))
3361 stripe_align
= mp
->m_swidth
;
3362 else if (mp
->m_dalign
)
3363 stripe_align
= mp
->m_dalign
;
3365 if (ap
->flags
& XFS_BMAPI_COWFORK
)
3366 align
= xfs_get_cowextsz_hint(ap
->ip
);
3367 else if (xfs_alloc_is_userdata(ap
->datatype
))
3368 align
= xfs_get_extsz_hint(ap
->ip
);
3370 error
= xfs_bmap_extsize_align(mp
, &ap
->got
, &ap
->prev
,
3371 align
, 0, ap
->eof
, 0, ap
->conv
,
3372 &ap
->offset
, &ap
->length
);
3378 nullfb
= *ap
->firstblock
== NULLFSBLOCK
;
3379 fb_agno
= nullfb
? NULLAGNUMBER
: XFS_FSB_TO_AGNO(mp
, *ap
->firstblock
);
3381 if (xfs_alloc_is_userdata(ap
->datatype
) &&
3382 xfs_inode_is_filestream(ap
->ip
)) {
3383 ag
= xfs_filestream_lookup_ag(ap
->ip
);
3384 ag
= (ag
!= NULLAGNUMBER
) ? ag
: 0;
3385 ap
->blkno
= XFS_AGB_TO_FSB(mp
, ag
, 0);
3387 ap
->blkno
= XFS_INO_TO_FSB(mp
, ap
->ip
->i_ino
);
3390 ap
->blkno
= *ap
->firstblock
;
3392 xfs_bmap_adjacent(ap
);
3395 * If allowed, use ap->blkno; otherwise must use firstblock since
3396 * it's in the right allocation group.
3398 if (nullfb
|| XFS_FSB_TO_AGNO(mp
, ap
->blkno
) == fb_agno
)
3401 ap
->blkno
= *ap
->firstblock
;
3403 * Normal allocation, done through xfs_alloc_vextent.
3405 tryagain
= isaligned
= 0;
3406 memset(&args
, 0, sizeof(args
));
3409 args
.fsbno
= ap
->blkno
;
3410 xfs_rmap_skip_owner_update(&args
.oinfo
);
3412 /* Trim the allocation back to the maximum an AG can fit. */
3413 args
.maxlen
= MIN(ap
->length
, mp
->m_ag_max_usable
);
3414 args
.firstblock
= *ap
->firstblock
;
3418 * Search for an allocation group with a single extent large
3419 * enough for the request. If one isn't found, then adjust
3420 * the minimum allocation size to the largest space found.
3422 if (xfs_alloc_is_userdata(ap
->datatype
) &&
3423 xfs_inode_is_filestream(ap
->ip
))
3424 error
= xfs_bmap_btalloc_filestreams(ap
, &args
, &blen
);
3426 error
= xfs_bmap_btalloc_nullfb(ap
, &args
, &blen
);
3429 } else if (ap
->dfops
->dop_low
) {
3430 if (xfs_inode_is_filestream(ap
->ip
))
3431 args
.type
= XFS_ALLOCTYPE_FIRST_AG
;
3433 args
.type
= XFS_ALLOCTYPE_START_BNO
;
3434 args
.total
= args
.minlen
= ap
->minlen
;
3436 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
3437 args
.total
= ap
->total
;
3438 args
.minlen
= ap
->minlen
;
3440 /* apply extent size hints if obtained earlier */
3443 if ((args
.mod
= (xfs_extlen_t
)do_mod(ap
->offset
, args
.prod
)))
3444 args
.mod
= (xfs_extlen_t
)(args
.prod
- args
.mod
);
3445 } else if (mp
->m_sb
.sb_blocksize
>= PAGE_SIZE
) {
3449 args
.prod
= PAGE_SIZE
>> mp
->m_sb
.sb_blocklog
;
3450 if ((args
.mod
= (xfs_extlen_t
)(do_mod(ap
->offset
, args
.prod
))))
3451 args
.mod
= (xfs_extlen_t
)(args
.prod
- args
.mod
);
3454 * If we are not low on available data blocks, and the
3455 * underlying logical volume manager is a stripe, and
3456 * the file offset is zero then try to allocate data
3457 * blocks on stripe unit boundary.
3458 * NOTE: ap->aeof is only set if the allocation length
3459 * is >= the stripe unit and the allocation offset is
3460 * at the end of file.
3462 if (!ap
->dfops
->dop_low
&& ap
->aeof
) {
3464 args
.alignment
= stripe_align
;
3468 * Adjust for alignment
3470 if (blen
> args
.alignment
&& blen
<= args
.maxlen
)
3471 args
.minlen
= blen
- args
.alignment
;
3472 args
.minalignslop
= 0;
3475 * First try an exact bno allocation.
3476 * If it fails then do a near or start bno
3477 * allocation with alignment turned on.
3481 args
.type
= XFS_ALLOCTYPE_THIS_BNO
;
3484 * Compute the minlen+alignment for the
3485 * next case. Set slop so that the value
3486 * of minlen+alignment+slop doesn't go up
3487 * between the calls.
3489 if (blen
> stripe_align
&& blen
<= args
.maxlen
)
3490 nextminlen
= blen
- stripe_align
;
3492 nextminlen
= args
.minlen
;
3493 if (nextminlen
+ stripe_align
> args
.minlen
+ 1)
3495 nextminlen
+ stripe_align
-
3498 args
.minalignslop
= 0;
3502 args
.minalignslop
= 0;
3504 args
.minleft
= ap
->minleft
;
3505 args
.wasdel
= ap
->wasdel
;
3506 args
.resv
= XFS_AG_RESV_NONE
;
3507 args
.datatype
= ap
->datatype
;
3508 if (ap
->datatype
& XFS_ALLOC_USERDATA_ZERO
)
3511 error
= xfs_alloc_vextent(&args
);
3515 if (tryagain
&& args
.fsbno
== NULLFSBLOCK
) {
3517 * Exact allocation failed. Now try with alignment
3521 args
.fsbno
= ap
->blkno
;
3522 args
.alignment
= stripe_align
;
3523 args
.minlen
= nextminlen
;
3524 args
.minalignslop
= 0;
3526 if ((error
= xfs_alloc_vextent(&args
)))
3529 if (isaligned
&& args
.fsbno
== NULLFSBLOCK
) {
3531 * allocation failed, so turn off alignment and
3535 args
.fsbno
= ap
->blkno
;
3537 if ((error
= xfs_alloc_vextent(&args
)))
3540 if (args
.fsbno
== NULLFSBLOCK
&& nullfb
&&
3541 args
.minlen
> ap
->minlen
) {
3542 args
.minlen
= ap
->minlen
;
3543 args
.type
= XFS_ALLOCTYPE_START_BNO
;
3544 args
.fsbno
= ap
->blkno
;
3545 if ((error
= xfs_alloc_vextent(&args
)))
3548 if (args
.fsbno
== NULLFSBLOCK
&& nullfb
) {
3550 args
.type
= XFS_ALLOCTYPE_FIRST_AG
;
3551 args
.total
= ap
->minlen
;
3552 if ((error
= xfs_alloc_vextent(&args
)))
3554 ap
->dfops
->dop_low
= true;
3556 if (args
.fsbno
!= NULLFSBLOCK
) {
3558 * check the allocation happened at the same or higher AG than
3559 * the first block that was allocated.
3561 ASSERT(*ap
->firstblock
== NULLFSBLOCK
||
3562 XFS_FSB_TO_AGNO(mp
, *ap
->firstblock
) <=
3563 XFS_FSB_TO_AGNO(mp
, args
.fsbno
));
3565 ap
->blkno
= args
.fsbno
;
3566 if (*ap
->firstblock
== NULLFSBLOCK
)
3567 *ap
->firstblock
= args
.fsbno
;
3568 ASSERT(nullfb
|| fb_agno
<= args
.agno
);
3569 ap
->length
= args
.len
;
3570 if (!(ap
->flags
& XFS_BMAPI_COWFORK
))
3571 ap
->ip
->i_d
.di_nblocks
+= args
.len
;
3572 xfs_trans_log_inode(ap
->tp
, ap
->ip
, XFS_ILOG_CORE
);
3574 ap
->ip
->i_delayed_blks
-= args
.len
;
3576 * Adjust the disk quota also. This was reserved
3579 xfs_trans_mod_dquot_byino(ap
->tp
, ap
->ip
,
3580 ap
->wasdel
? XFS_TRANS_DQ_DELBCOUNT
:
3581 XFS_TRANS_DQ_BCOUNT
,
3584 ap
->blkno
= NULLFSBLOCK
;
3591 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
3592 * It figures out where to ask the underlying allocator to put the new extent.
3596 struct xfs_bmalloca
*ap
) /* bmap alloc argument struct */
3598 if (XFS_IS_REALTIME_INODE(ap
->ip
) &&
3599 xfs_alloc_is_userdata(ap
->datatype
))
3600 return xfs_bmap_rtalloc(ap
);
3601 return xfs_bmap_btalloc(ap
);
3604 /* Trim extent to fit a logical block range. */
3607 struct xfs_bmbt_irec
*irec
,
3611 xfs_fileoff_t distance
;
3612 xfs_fileoff_t end
= bno
+ len
;
3614 if (irec
->br_startoff
+ irec
->br_blockcount
<= bno
||
3615 irec
->br_startoff
>= end
) {
3616 irec
->br_blockcount
= 0;
3620 if (irec
->br_startoff
< bno
) {
3621 distance
= bno
- irec
->br_startoff
;
3622 if (isnullstartblock(irec
->br_startblock
))
3623 irec
->br_startblock
= DELAYSTARTBLOCK
;
3624 if (irec
->br_startblock
!= DELAYSTARTBLOCK
&&
3625 irec
->br_startblock
!= HOLESTARTBLOCK
)
3626 irec
->br_startblock
+= distance
;
3627 irec
->br_startoff
+= distance
;
3628 irec
->br_blockcount
-= distance
;
3631 if (end
< irec
->br_startoff
+ irec
->br_blockcount
) {
3632 distance
= irec
->br_startoff
+ irec
->br_blockcount
- end
;
3633 irec
->br_blockcount
-= distance
;
3637 /* trim extent to within eof */
3639 xfs_trim_extent_eof(
3640 struct xfs_bmbt_irec
*irec
,
3641 struct xfs_inode
*ip
)
3644 xfs_trim_extent(irec
, 0, XFS_B_TO_FSB(ip
->i_mount
,
3645 i_size_read(VFS_I(ip
))));
3649 * Trim the returned map to the required bounds
3653 struct xfs_bmbt_irec
*mval
,
3654 struct xfs_bmbt_irec
*got
,
3662 if ((flags
& XFS_BMAPI_ENTIRE
) ||
3663 got
->br_startoff
+ got
->br_blockcount
<= obno
) {
3665 if (isnullstartblock(got
->br_startblock
))
3666 mval
->br_startblock
= DELAYSTARTBLOCK
;
3672 ASSERT((*bno
>= obno
) || (n
== 0));
3674 mval
->br_startoff
= *bno
;
3675 if (isnullstartblock(got
->br_startblock
))
3676 mval
->br_startblock
= DELAYSTARTBLOCK
;
3678 mval
->br_startblock
= got
->br_startblock
+
3679 (*bno
- got
->br_startoff
);
3681 * Return the minimum of what we got and what we asked for for
3682 * the length. We can use the len variable here because it is
3683 * modified below and we could have been there before coming
3684 * here if the first part of the allocation didn't overlap what
3687 mval
->br_blockcount
= XFS_FILBLKS_MIN(end
- *bno
,
3688 got
->br_blockcount
- (*bno
- got
->br_startoff
));
3689 mval
->br_state
= got
->br_state
;
3690 ASSERT(mval
->br_blockcount
<= len
);
3695 * Update and validate the extent map to return
3698 xfs_bmapi_update_map(
3699 struct xfs_bmbt_irec
**map
,
3707 xfs_bmbt_irec_t
*mval
= *map
;
3709 ASSERT((flags
& XFS_BMAPI_ENTIRE
) ||
3710 ((mval
->br_startoff
+ mval
->br_blockcount
) <= end
));
3711 ASSERT((flags
& XFS_BMAPI_ENTIRE
) || (mval
->br_blockcount
<= *len
) ||
3712 (mval
->br_startoff
< obno
));
3714 *bno
= mval
->br_startoff
+ mval
->br_blockcount
;
3716 if (*n
> 0 && mval
->br_startoff
== mval
[-1].br_startoff
) {
3717 /* update previous map with new information */
3718 ASSERT(mval
->br_startblock
== mval
[-1].br_startblock
);
3719 ASSERT(mval
->br_blockcount
> mval
[-1].br_blockcount
);
3720 ASSERT(mval
->br_state
== mval
[-1].br_state
);
3721 mval
[-1].br_blockcount
= mval
->br_blockcount
;
3722 mval
[-1].br_state
= mval
->br_state
;
3723 } else if (*n
> 0 && mval
->br_startblock
!= DELAYSTARTBLOCK
&&
3724 mval
[-1].br_startblock
!= DELAYSTARTBLOCK
&&
3725 mval
[-1].br_startblock
!= HOLESTARTBLOCK
&&
3726 mval
->br_startblock
== mval
[-1].br_startblock
+
3727 mval
[-1].br_blockcount
&&
3728 ((flags
& XFS_BMAPI_IGSTATE
) ||
3729 mval
[-1].br_state
== mval
->br_state
)) {
3730 ASSERT(mval
->br_startoff
==
3731 mval
[-1].br_startoff
+ mval
[-1].br_blockcount
);
3732 mval
[-1].br_blockcount
+= mval
->br_blockcount
;
3733 } else if (*n
> 0 &&
3734 mval
->br_startblock
== DELAYSTARTBLOCK
&&
3735 mval
[-1].br_startblock
== DELAYSTARTBLOCK
&&
3736 mval
->br_startoff
==
3737 mval
[-1].br_startoff
+ mval
[-1].br_blockcount
) {
3738 mval
[-1].br_blockcount
+= mval
->br_blockcount
;
3739 mval
[-1].br_state
= mval
->br_state
;
3740 } else if (!((*n
== 0) &&
3741 ((mval
->br_startoff
+ mval
->br_blockcount
) <=
3750 * Map file blocks to filesystem blocks without allocation.
3754 struct xfs_inode
*ip
,
3757 struct xfs_bmbt_irec
*mval
,
3761 struct xfs_mount
*mp
= ip
->i_mount
;
3762 struct xfs_ifork
*ifp
;
3763 struct xfs_bmbt_irec got
;
3770 int whichfork
= xfs_bmapi_whichfork(flags
);
3773 ASSERT(!(flags
& ~(XFS_BMAPI_ATTRFORK
|XFS_BMAPI_ENTIRE
|
3774 XFS_BMAPI_IGSTATE
|XFS_BMAPI_COWFORK
)));
3775 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_SHARED
|XFS_ILOCK_EXCL
));
3777 if (unlikely(XFS_TEST_ERROR(
3778 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
3779 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
3780 mp
, XFS_ERRTAG_BMAPIFORMAT
))) {
3781 XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW
, mp
);
3782 return -EFSCORRUPTED
;
3785 if (XFS_FORCED_SHUTDOWN(mp
))
3788 XFS_STATS_INC(mp
, xs_blk_mapr
);
3790 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
3792 /* No CoW fork? Return a hole. */
3793 if (whichfork
== XFS_COW_FORK
&& !ifp
) {
3794 mval
->br_startoff
= bno
;
3795 mval
->br_startblock
= HOLESTARTBLOCK
;
3796 mval
->br_blockcount
= len
;
3797 mval
->br_state
= XFS_EXT_NORM
;
3802 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
3803 error
= xfs_iread_extents(NULL
, ip
, whichfork
);
3808 if (!xfs_iext_lookup_extent(ip
, ifp
, bno
, &idx
, &got
))
3813 while (bno
< end
&& n
< *nmap
) {
3814 /* Reading past eof, act as though there's a hole up to end. */
3816 got
.br_startoff
= end
;
3817 if (got
.br_startoff
> bno
) {
3818 /* Reading in a hole. */
3819 mval
->br_startoff
= bno
;
3820 mval
->br_startblock
= HOLESTARTBLOCK
;
3821 mval
->br_blockcount
=
3822 XFS_FILBLKS_MIN(len
, got
.br_startoff
- bno
);
3823 mval
->br_state
= XFS_EXT_NORM
;
3824 bno
+= mval
->br_blockcount
;
3825 len
-= mval
->br_blockcount
;
3831 /* set up the extent map to return. */
3832 xfs_bmapi_trim_map(mval
, &got
, &bno
, len
, obno
, end
, n
, flags
);
3833 xfs_bmapi_update_map(&mval
, &bno
, &len
, obno
, end
, &n
, flags
);
3835 /* If we're done, stop now. */
3836 if (bno
>= end
|| n
>= *nmap
)
3839 /* Else go on to the next record. */
3840 if (!xfs_iext_get_extent(ifp
, ++idx
, &got
))
3848 * Add a delayed allocation extent to an inode. Blocks are reserved from the
3849 * global pool and the extent inserted into the inode in-core extent tree.
3851 * On entry, got refers to the first extent beyond the offset of the extent to
3852 * allocate or eof is specified if no such extent exists. On return, got refers
3853 * to the extent record that was inserted to the inode fork.
3855 * Note that the allocated extent may have been merged with contiguous extents
3856 * during insertion into the inode fork. Thus, got does not reflect the current
3857 * state of the inode fork on return. If necessary, the caller can use lastx to
3858 * look up the updated record in the inode fork.
3861 xfs_bmapi_reserve_delalloc(
3862 struct xfs_inode
*ip
,
3866 xfs_filblks_t prealloc
,
3867 struct xfs_bmbt_irec
*got
,
3868 xfs_extnum_t
*lastx
,
3871 struct xfs_mount
*mp
= ip
->i_mount
;
3872 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
3874 xfs_extlen_t indlen
;
3875 char rt
= XFS_IS_REALTIME_INODE(ip
);
3878 xfs_fileoff_t aoff
= off
;
3881 * Cap the alloc length. Keep track of prealloc so we know whether to
3882 * tag the inode before we return.
3884 alen
= XFS_FILBLKS_MIN(len
+ prealloc
, MAXEXTLEN
);
3886 alen
= XFS_FILBLKS_MIN(alen
, got
->br_startoff
- aoff
);
3887 if (prealloc
&& alen
>= len
)
3888 prealloc
= alen
- len
;
3890 /* Figure out the extent size, adjust alen */
3891 if (whichfork
== XFS_COW_FORK
)
3892 extsz
= xfs_get_cowextsz_hint(ip
);
3894 extsz
= xfs_get_extsz_hint(ip
);
3896 struct xfs_bmbt_irec prev
;
3898 if (!xfs_iext_get_extent(ifp
, *lastx
- 1, &prev
))
3899 prev
.br_startoff
= NULLFILEOFF
;
3901 error
= xfs_bmap_extsize_align(mp
, got
, &prev
, extsz
, rt
, eof
,
3902 1, 0, &aoff
, &alen
);
3907 extsz
= alen
/ mp
->m_sb
.sb_rextsize
;
3910 * Make a transaction-less quota reservation for delayed allocation
3911 * blocks. This number gets adjusted later. We return if we haven't
3912 * allocated blocks already inside this loop.
3914 error
= xfs_trans_reserve_quota_nblks(NULL
, ip
, (long)alen
, 0,
3915 rt
? XFS_QMOPT_RES_RTBLKS
: XFS_QMOPT_RES_REGBLKS
);
3920 * Split changing sb for alen and indlen since they could be coming
3921 * from different places.
3923 indlen
= (xfs_extlen_t
)xfs_bmap_worst_indlen(ip
, alen
);
3927 error
= xfs_mod_frextents(mp
, -((int64_t)extsz
));
3929 error
= xfs_mod_fdblocks(mp
, -((int64_t)alen
), false);
3933 goto out_unreserve_quota
;
3935 error
= xfs_mod_fdblocks(mp
, -((int64_t)indlen
), false);
3937 goto out_unreserve_blocks
;
3940 ip
->i_delayed_blks
+= alen
;
3942 got
->br_startoff
= aoff
;
3943 got
->br_startblock
= nullstartblock(indlen
);
3944 got
->br_blockcount
= alen
;
3945 got
->br_state
= XFS_EXT_NORM
;
3947 xfs_bmap_add_extent_hole_delay(ip
, whichfork
, lastx
, got
);
3950 * Tag the inode if blocks were preallocated. Note that COW fork
3951 * preallocation can occur at the start or end of the extent, even when
3952 * prealloc == 0, so we must also check the aligned offset and length.
3954 if (whichfork
== XFS_DATA_FORK
&& prealloc
)
3955 xfs_inode_set_eofblocks_tag(ip
);
3956 if (whichfork
== XFS_COW_FORK
&& (prealloc
|| aoff
< off
|| alen
> len
))
3957 xfs_inode_set_cowblocks_tag(ip
);
3961 out_unreserve_blocks
:
3963 xfs_mod_frextents(mp
, extsz
);
3965 xfs_mod_fdblocks(mp
, alen
, false);
3966 out_unreserve_quota
:
3967 if (XFS_IS_QUOTA_ON(mp
))
3968 xfs_trans_unreserve_quota_nblks(NULL
, ip
, (long)alen
, 0, rt
?
3969 XFS_QMOPT_RES_RTBLKS
: XFS_QMOPT_RES_REGBLKS
);
3975 struct xfs_bmalloca
*bma
)
3977 struct xfs_mount
*mp
= bma
->ip
->i_mount
;
3978 int whichfork
= xfs_bmapi_whichfork(bma
->flags
);
3979 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
3980 int tmp_logflags
= 0;
3983 ASSERT(bma
->length
> 0);
3986 * For the wasdelay case, we could also just allocate the stuff asked
3987 * for in this bmap call but that wouldn't be as good.
3990 bma
->length
= (xfs_extlen_t
)bma
->got
.br_blockcount
;
3991 bma
->offset
= bma
->got
.br_startoff
;
3993 xfs_iext_get_extent(ifp
, bma
->idx
- 1, &bma
->prev
);
3995 bma
->length
= XFS_FILBLKS_MIN(bma
->length
, MAXEXTLEN
);
3997 bma
->length
= XFS_FILBLKS_MIN(bma
->length
,
3998 bma
->got
.br_startoff
- bma
->offset
);
4002 * Set the data type being allocated. For the data fork, the first data
4003 * in the file is treated differently to all other allocations. For the
4004 * attribute fork, we only need to ensure the allocated range is not on
4007 if (!(bma
->flags
& XFS_BMAPI_METADATA
)) {
4008 bma
->datatype
= XFS_ALLOC_NOBUSY
;
4009 if (whichfork
== XFS_DATA_FORK
) {
4010 if (bma
->offset
== 0)
4011 bma
->datatype
|= XFS_ALLOC_INITIAL_USER_DATA
;
4013 bma
->datatype
|= XFS_ALLOC_USERDATA
;
4015 if (bma
->flags
& XFS_BMAPI_ZERO
)
4016 bma
->datatype
|= XFS_ALLOC_USERDATA_ZERO
;
4019 bma
->minlen
= (bma
->flags
& XFS_BMAPI_CONTIG
) ? bma
->length
: 1;
4022 * Only want to do the alignment at the eof if it is userdata and
4023 * allocation length is larger than a stripe unit.
4025 if (mp
->m_dalign
&& bma
->length
>= mp
->m_dalign
&&
4026 !(bma
->flags
& XFS_BMAPI_METADATA
) && whichfork
== XFS_DATA_FORK
) {
4027 error
= xfs_bmap_isaeof(bma
, whichfork
);
4032 error
= xfs_bmap_alloc(bma
);
4037 bma
->cur
->bc_private
.b
.firstblock
= *bma
->firstblock
;
4038 if (bma
->blkno
== NULLFSBLOCK
)
4040 if ((ifp
->if_flags
& XFS_IFBROOT
) && !bma
->cur
) {
4041 bma
->cur
= xfs_bmbt_init_cursor(mp
, bma
->tp
, bma
->ip
, whichfork
);
4042 bma
->cur
->bc_private
.b
.firstblock
= *bma
->firstblock
;
4043 bma
->cur
->bc_private
.b
.dfops
= bma
->dfops
;
4046 * Bump the number of extents we've allocated
4052 bma
->cur
->bc_private
.b
.flags
=
4053 bma
->wasdel
? XFS_BTCUR_BPRV_WASDEL
: 0;
4055 bma
->got
.br_startoff
= bma
->offset
;
4056 bma
->got
.br_startblock
= bma
->blkno
;
4057 bma
->got
.br_blockcount
= bma
->length
;
4058 bma
->got
.br_state
= XFS_EXT_NORM
;
4061 * In the data fork, a wasdelay extent has been initialized, so
4062 * shouldn't be flagged as unwritten.
4064 * For the cow fork, however, we convert delalloc reservations
4065 * (extents allocated for speculative preallocation) to
4066 * allocated unwritten extents, and only convert the unwritten
4067 * extents to real extents when we're about to write the data.
4069 if ((!bma
->wasdel
|| (bma
->flags
& XFS_BMAPI_COWFORK
)) &&
4070 (bma
->flags
& XFS_BMAPI_PREALLOC
) &&
4071 xfs_sb_version_hasextflgbit(&mp
->m_sb
))
4072 bma
->got
.br_state
= XFS_EXT_UNWRITTEN
;
4075 error
= xfs_bmap_add_extent_delay_real(bma
, whichfork
);
4077 error
= xfs_bmap_add_extent_hole_real(bma
->tp
, bma
->ip
,
4078 whichfork
, &bma
->idx
, &bma
->cur
, &bma
->got
,
4079 bma
->firstblock
, bma
->dfops
, &bma
->logflags
);
4081 bma
->logflags
|= tmp_logflags
;
4086 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4087 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4088 * the neighbouring ones.
4090 xfs_iext_get_extent(ifp
, bma
->idx
, &bma
->got
);
4092 ASSERT(bma
->got
.br_startoff
<= bma
->offset
);
4093 ASSERT(bma
->got
.br_startoff
+ bma
->got
.br_blockcount
>=
4094 bma
->offset
+ bma
->length
);
4095 ASSERT(bma
->got
.br_state
== XFS_EXT_NORM
||
4096 bma
->got
.br_state
== XFS_EXT_UNWRITTEN
);
4101 xfs_bmapi_convert_unwritten(
4102 struct xfs_bmalloca
*bma
,
4103 struct xfs_bmbt_irec
*mval
,
4107 int whichfork
= xfs_bmapi_whichfork(flags
);
4108 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
4109 int tmp_logflags
= 0;
4112 /* check if we need to do unwritten->real conversion */
4113 if (mval
->br_state
== XFS_EXT_UNWRITTEN
&&
4114 (flags
& XFS_BMAPI_PREALLOC
))
4117 /* check if we need to do real->unwritten conversion */
4118 if (mval
->br_state
== XFS_EXT_NORM
&&
4119 (flags
& (XFS_BMAPI_PREALLOC
| XFS_BMAPI_CONVERT
)) !=
4120 (XFS_BMAPI_PREALLOC
| XFS_BMAPI_CONVERT
))
4124 * Modify (by adding) the state flag, if writing.
4126 ASSERT(mval
->br_blockcount
<= len
);
4127 if ((ifp
->if_flags
& XFS_IFBROOT
) && !bma
->cur
) {
4128 bma
->cur
= xfs_bmbt_init_cursor(bma
->ip
->i_mount
, bma
->tp
,
4129 bma
->ip
, whichfork
);
4130 bma
->cur
->bc_private
.b
.firstblock
= *bma
->firstblock
;
4131 bma
->cur
->bc_private
.b
.dfops
= bma
->dfops
;
4133 mval
->br_state
= (mval
->br_state
== XFS_EXT_UNWRITTEN
)
4134 ? XFS_EXT_NORM
: XFS_EXT_UNWRITTEN
;
4137 * Before insertion into the bmbt, zero the range being converted
4140 if (flags
& XFS_BMAPI_ZERO
) {
4141 error
= xfs_zero_extent(bma
->ip
, mval
->br_startblock
,
4142 mval
->br_blockcount
);
4147 error
= xfs_bmap_add_extent_unwritten_real(bma
->tp
, bma
->ip
, whichfork
,
4148 &bma
->idx
, &bma
->cur
, mval
, bma
->firstblock
, bma
->dfops
,
4151 * Log the inode core unconditionally in the unwritten extent conversion
4152 * path because the conversion might not have done so (e.g., if the
4153 * extent count hasn't changed). We need to make sure the inode is dirty
4154 * in the transaction for the sake of fsync(), even if nothing has
4155 * changed, because fsync() will not force the log for this transaction
4156 * unless it sees the inode pinned.
4158 * Note: If we're only converting cow fork extents, there aren't
4159 * any on-disk updates to make, so we don't need to log anything.
4161 if (whichfork
!= XFS_COW_FORK
)
4162 bma
->logflags
|= tmp_logflags
| XFS_ILOG_CORE
;
4167 * Update our extent pointer, given that
4168 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4169 * of the neighbouring ones.
4171 xfs_iext_get_extent(ifp
, bma
->idx
, &bma
->got
);
4174 * We may have combined previously unwritten space with written space,
4175 * so generate another request.
4177 if (mval
->br_blockcount
< len
)
4183 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4184 * extent state if necessary. Details behaviour is controlled by the flags
4185 * parameter. Only allocates blocks from a single allocation group, to avoid
4188 * The returned value in "firstblock" from the first call in a transaction
4189 * must be remembered and presented to subsequent calls in "firstblock".
4190 * An upper bound for the number of blocks to be allocated is supplied to
4191 * the first call in "total"; if no allocation group has that many free
4192 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4196 struct xfs_trans
*tp
, /* transaction pointer */
4197 struct xfs_inode
*ip
, /* incore inode */
4198 xfs_fileoff_t bno
, /* starting file offs. mapped */
4199 xfs_filblks_t len
, /* length to map in file */
4200 int flags
, /* XFS_BMAPI_... */
4201 xfs_fsblock_t
*firstblock
, /* first allocated block
4202 controls a.g. for allocs */
4203 xfs_extlen_t total
, /* total blocks needed */
4204 struct xfs_bmbt_irec
*mval
, /* output: map values */
4205 int *nmap
, /* i/o: mval size/count */
4206 struct xfs_defer_ops
*dfops
) /* i/o: list extents to free */
4208 struct xfs_mount
*mp
= ip
->i_mount
;
4209 struct xfs_ifork
*ifp
;
4210 struct xfs_bmalloca bma
= { NULL
}; /* args for xfs_bmap_alloc */
4211 xfs_fileoff_t end
; /* end of mapped file region */
4212 bool eof
= false; /* after the end of extents */
4213 int error
; /* error return */
4214 int n
; /* current extent index */
4215 xfs_fileoff_t obno
; /* old block number (offset) */
4216 int whichfork
; /* data or attr fork */
4219 xfs_fileoff_t orig_bno
; /* original block number value */
4220 int orig_flags
; /* original flags arg value */
4221 xfs_filblks_t orig_len
; /* original value of len arg */
4222 struct xfs_bmbt_irec
*orig_mval
; /* original value of mval */
4223 int orig_nmap
; /* original value of *nmap */
4231 whichfork
= xfs_bmapi_whichfork(flags
);
4234 ASSERT(*nmap
<= XFS_BMAP_MAX_NMAP
);
4235 ASSERT(!(flags
& XFS_BMAPI_IGSTATE
));
4236 ASSERT(tp
!= NULL
||
4237 (flags
& (XFS_BMAPI_CONVERT
| XFS_BMAPI_COWFORK
)) ==
4238 (XFS_BMAPI_CONVERT
| XFS_BMAPI_COWFORK
));
4240 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_LOCAL
);
4241 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
4242 ASSERT(!(flags
& XFS_BMAPI_REMAP
));
4244 /* zeroing is for currently only for data extents, not metadata */
4245 ASSERT((flags
& (XFS_BMAPI_METADATA
| XFS_BMAPI_ZERO
)) !=
4246 (XFS_BMAPI_METADATA
| XFS_BMAPI_ZERO
));
4248 * we can allocate unwritten extents or pre-zero allocated blocks,
4249 * but it makes no sense to do both at once. This would result in
4250 * zeroing the unwritten extent twice, but it still being an
4251 * unwritten extent....
4253 ASSERT((flags
& (XFS_BMAPI_PREALLOC
| XFS_BMAPI_ZERO
)) !=
4254 (XFS_BMAPI_PREALLOC
| XFS_BMAPI_ZERO
));
4256 if (unlikely(XFS_TEST_ERROR(
4257 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
4258 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
4259 mp
, XFS_ERRTAG_BMAPIFORMAT
))) {
4260 XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW
, mp
);
4261 return -EFSCORRUPTED
;
4264 if (XFS_FORCED_SHUTDOWN(mp
))
4267 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4269 XFS_STATS_INC(mp
, xs_blk_mapw
);
4271 if (*firstblock
== NULLFSBLOCK
) {
4272 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
)
4273 bma
.minleft
= be16_to_cpu(ifp
->if_broot
->bb_level
) + 1;
4280 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
4281 error
= xfs_iread_extents(tp
, ip
, whichfork
);
4290 if (!xfs_iext_lookup_extent(ip
, ifp
, bno
, &bma
.idx
, &bma
.got
))
4292 if (!xfs_iext_get_extent(ifp
, bma
.idx
- 1, &bma
.prev
))
4293 bma
.prev
.br_startoff
= NULLFILEOFF
;
4299 bma
.firstblock
= firstblock
;
4301 while (bno
< end
&& n
< *nmap
) {
4302 bool need_alloc
= false, wasdelay
= false;
4304 /* in hole or beyoned EOF? */
4305 if (eof
|| bma
.got
.br_startoff
> bno
) {
4306 if (flags
& XFS_BMAPI_DELALLOC
) {
4308 * For the COW fork we can reasonably get a
4309 * request for converting an extent that races
4310 * with other threads already having converted
4311 * part of it, as there converting COW to
4312 * regular blocks is not protected using the
4315 ASSERT(flags
& XFS_BMAPI_COWFORK
);
4316 if (!(flags
& XFS_BMAPI_COWFORK
)) {
4321 if (eof
|| bno
>= end
)
4326 } else if (isnullstartblock(bma
.got
.br_startblock
)) {
4331 * First, deal with the hole before the allocated space
4332 * that we found, if any.
4334 if (need_alloc
|| wasdelay
) {
4336 bma
.conv
= !!(flags
& XFS_BMAPI_CONVERT
);
4337 bma
.wasdel
= wasdelay
;
4342 * There's a 32/64 bit type mismatch between the
4343 * allocation length request (which can be 64 bits in
4344 * length) and the bma length request, which is
4345 * xfs_extlen_t and therefore 32 bits. Hence we have to
4346 * check for 32-bit overflows and handle them here.
4348 if (len
> (xfs_filblks_t
)MAXEXTLEN
)
4349 bma
.length
= MAXEXTLEN
;
4354 ASSERT(bma
.length
> 0);
4355 error
= xfs_bmapi_allocate(&bma
);
4358 if (bma
.blkno
== NULLFSBLOCK
)
4362 * If this is a CoW allocation, record the data in
4363 * the refcount btree for orphan recovery.
4365 if (whichfork
== XFS_COW_FORK
) {
4366 error
= xfs_refcount_alloc_cow_extent(mp
, dfops
,
4367 bma
.blkno
, bma
.length
);
4373 /* Deal with the allocated space we found. */
4374 xfs_bmapi_trim_map(mval
, &bma
.got
, &bno
, len
, obno
,
4377 /* Execute unwritten extent conversion if necessary */
4378 error
= xfs_bmapi_convert_unwritten(&bma
, mval
, len
, flags
);
4379 if (error
== -EAGAIN
)
4384 /* update the extent map to return */
4385 xfs_bmapi_update_map(&mval
, &bno
, &len
, obno
, end
, &n
, flags
);
4388 * If we're done, stop now. Stop when we've allocated
4389 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4390 * the transaction may get too big.
4392 if (bno
>= end
|| n
>= *nmap
|| bma
.nallocs
>= *nmap
)
4395 /* Else go on to the next record. */
4397 if (!xfs_iext_get_extent(ifp
, ++bma
.idx
, &bma
.got
))
4403 * Transform from btree to extents, give it cur.
4405 if (xfs_bmap_wants_extents(ip
, whichfork
)) {
4406 int tmp_logflags
= 0;
4409 error
= xfs_bmap_btree_to_extents(tp
, ip
, bma
.cur
,
4410 &tmp_logflags
, whichfork
);
4411 bma
.logflags
|= tmp_logflags
;
4416 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
||
4417 XFS_IFORK_NEXTENTS(ip
, whichfork
) >
4418 XFS_IFORK_MAXEXT(ip
, whichfork
));
4422 * Log everything. Do this after conversion, there's no point in
4423 * logging the extent records if we've converted to btree format.
4425 if ((bma
.logflags
& xfs_ilog_fext(whichfork
)) &&
4426 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
4427 bma
.logflags
&= ~xfs_ilog_fext(whichfork
);
4428 else if ((bma
.logflags
& xfs_ilog_fbroot(whichfork
)) &&
4429 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)
4430 bma
.logflags
&= ~xfs_ilog_fbroot(whichfork
);
4432 * Log whatever the flags say, even if error. Otherwise we might miss
4433 * detecting a case where the data is changed, there's an error,
4434 * and it's not logged so we don't shutdown when we should.
4437 xfs_trans_log_inode(tp
, ip
, bma
.logflags
);
4441 ASSERT(*firstblock
== NULLFSBLOCK
||
4442 XFS_FSB_TO_AGNO(mp
, *firstblock
) <=
4444 bma
.cur
->bc_private
.b
.firstblock
));
4445 *firstblock
= bma
.cur
->bc_private
.b
.firstblock
;
4447 xfs_btree_del_cursor(bma
.cur
,
4448 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
4451 xfs_bmap_validate_ret(orig_bno
, orig_len
, orig_flags
, orig_mval
,
4458 struct xfs_trans
*tp
,
4459 struct xfs_inode
*ip
,
4462 xfs_fsblock_t startblock
,
4463 struct xfs_defer_ops
*dfops
)
4465 struct xfs_mount
*mp
= ip
->i_mount
;
4466 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, XFS_DATA_FORK
);
4467 struct xfs_btree_cur
*cur
= NULL
;
4468 xfs_fsblock_t firstblock
= NULLFSBLOCK
;
4469 struct xfs_bmbt_irec got
;
4471 int logflags
= 0, error
;
4474 ASSERT(len
<= (xfs_filblks_t
)MAXEXTLEN
);
4475 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
4477 if (unlikely(XFS_TEST_ERROR(
4478 (XFS_IFORK_FORMAT(ip
, XFS_DATA_FORK
) != XFS_DINODE_FMT_EXTENTS
&&
4479 XFS_IFORK_FORMAT(ip
, XFS_DATA_FORK
) != XFS_DINODE_FMT_BTREE
),
4480 mp
, XFS_ERRTAG_BMAPIFORMAT
))) {
4481 XFS_ERROR_REPORT("xfs_bmapi_remap", XFS_ERRLEVEL_LOW
, mp
);
4482 return -EFSCORRUPTED
;
4485 if (XFS_FORCED_SHUTDOWN(mp
))
4488 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
4489 error
= xfs_iread_extents(NULL
, ip
, XFS_DATA_FORK
);
4494 if (xfs_iext_lookup_extent(ip
, ifp
, bno
, &idx
, &got
)) {
4495 /* make sure we only reflink into a hole. */
4496 ASSERT(got
.br_startoff
> bno
);
4497 ASSERT(got
.br_startoff
- bno
>= len
);
4500 ip
->i_d
.di_nblocks
+= len
;
4501 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
4503 if (ifp
->if_flags
& XFS_IFBROOT
) {
4504 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, XFS_DATA_FORK
);
4505 cur
->bc_private
.b
.firstblock
= firstblock
;
4506 cur
->bc_private
.b
.dfops
= dfops
;
4507 cur
->bc_private
.b
.flags
= 0;
4510 got
.br_startoff
= bno
;
4511 got
.br_startblock
= startblock
;
4512 got
.br_blockcount
= len
;
4513 got
.br_state
= XFS_EXT_NORM
;
4515 error
= xfs_bmap_add_extent_hole_real(tp
, ip
, XFS_DATA_FORK
, &idx
, &cur
,
4516 &got
, &firstblock
, dfops
, &logflags
);
4520 if (xfs_bmap_wants_extents(ip
, XFS_DATA_FORK
)) {
4521 int tmp_logflags
= 0;
4523 error
= xfs_bmap_btree_to_extents(tp
, ip
, cur
,
4524 &tmp_logflags
, XFS_DATA_FORK
);
4525 logflags
|= tmp_logflags
;
4529 if (ip
->i_d
.di_format
!= XFS_DINODE_FMT_EXTENTS
)
4530 logflags
&= ~XFS_ILOG_DEXT
;
4531 else if (ip
->i_d
.di_format
!= XFS_DINODE_FMT_BTREE
)
4532 logflags
&= ~XFS_ILOG_DBROOT
;
4535 xfs_trans_log_inode(tp
, ip
, logflags
);
4537 xfs_btree_del_cursor(cur
,
4538 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
4544 * When a delalloc extent is split (e.g., due to a hole punch), the original
4545 * indlen reservation must be shared across the two new extents that are left
4548 * Given the original reservation and the worst case indlen for the two new
4549 * extents (as calculated by xfs_bmap_worst_indlen()), split the original
4550 * reservation fairly across the two new extents. If necessary, steal available
4551 * blocks from a deleted extent to make up a reservation deficiency (e.g., if
4552 * ores == 1). The number of stolen blocks is returned. The availability and
4553 * subsequent accounting of stolen blocks is the responsibility of the caller.
4555 static xfs_filblks_t
4556 xfs_bmap_split_indlen(
4557 xfs_filblks_t ores
, /* original res. */
4558 xfs_filblks_t
*indlen1
, /* ext1 worst indlen */
4559 xfs_filblks_t
*indlen2
, /* ext2 worst indlen */
4560 xfs_filblks_t avail
) /* stealable blocks */
4562 xfs_filblks_t len1
= *indlen1
;
4563 xfs_filblks_t len2
= *indlen2
;
4564 xfs_filblks_t nres
= len1
+ len2
; /* new total res. */
4565 xfs_filblks_t stolen
= 0;
4566 xfs_filblks_t resfactor
;
4569 * Steal as many blocks as we can to try and satisfy the worst case
4570 * indlen for both new extents.
4572 if (ores
< nres
&& avail
)
4573 stolen
= XFS_FILBLKS_MIN(nres
- ores
, avail
);
4576 /* nothing else to do if we've satisfied the new reservation */
4581 * We can't meet the total required reservation for the two extents.
4582 * Calculate the percent of the overall shortage between both extents
4583 * and apply this percentage to each of the requested indlen values.
4584 * This distributes the shortage fairly and reduces the chances that one
4585 * of the two extents is left with nothing when extents are repeatedly
4588 resfactor
= (ores
* 100);
4589 do_div(resfactor
, nres
);
4594 ASSERT(len1
+ len2
<= ores
);
4595 ASSERT(len1
< *indlen1
&& len2
< *indlen2
);
4598 * Hand out the remainder to each extent. If one of the two reservations
4599 * is zero, we want to make sure that one gets a block first. The loop
4600 * below starts with len1, so hand len2 a block right off the bat if it
4603 ores
-= (len1
+ len2
);
4604 ASSERT((*indlen1
- len1
) + (*indlen2
- len2
) >= ores
);
4605 if (ores
&& !len2
&& *indlen2
) {
4610 if (len1
< *indlen1
) {
4616 if (len2
< *indlen2
) {
4629 xfs_bmap_del_extent_delay(
4630 struct xfs_inode
*ip
,
4633 struct xfs_bmbt_irec
*got
,
4634 struct xfs_bmbt_irec
*del
)
4636 struct xfs_mount
*mp
= ip
->i_mount
;
4637 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4638 struct xfs_bmbt_irec
new;
4639 int64_t da_old
, da_new
, da_diff
= 0;
4640 xfs_fileoff_t del_endoff
, got_endoff
;
4641 xfs_filblks_t got_indlen
, new_indlen
, stolen
;
4642 int state
= xfs_bmap_fork_to_state(whichfork
);
4646 XFS_STATS_INC(mp
, xs_del_exlist
);
4648 isrt
= (whichfork
== XFS_DATA_FORK
) && XFS_IS_REALTIME_INODE(ip
);
4649 del_endoff
= del
->br_startoff
+ del
->br_blockcount
;
4650 got_endoff
= got
->br_startoff
+ got
->br_blockcount
;
4651 da_old
= startblockval(got
->br_startblock
);
4655 ASSERT(*idx
<= xfs_iext_count(ifp
));
4656 ASSERT(del
->br_blockcount
> 0);
4657 ASSERT(got
->br_startoff
<= del
->br_startoff
);
4658 ASSERT(got_endoff
>= del_endoff
);
4661 uint64_t rtexts
= XFS_FSB_TO_B(mp
, del
->br_blockcount
);
4663 do_div(rtexts
, mp
->m_sb
.sb_rextsize
);
4664 xfs_mod_frextents(mp
, rtexts
);
4668 * Update the inode delalloc counter now and wait to update the
4669 * sb counters as we might have to borrow some blocks for the
4670 * indirect block accounting.
4672 error
= xfs_trans_reserve_quota_nblks(NULL
, ip
,
4673 -((long)del
->br_blockcount
), 0,
4674 isrt
? XFS_QMOPT_RES_RTBLKS
: XFS_QMOPT_RES_REGBLKS
);
4677 ip
->i_delayed_blks
-= del
->br_blockcount
;
4679 if (got
->br_startoff
== del
->br_startoff
)
4680 state
|= BMAP_LEFT_FILLING
;
4681 if (got_endoff
== del_endoff
)
4682 state
|= BMAP_RIGHT_FILLING
;
4684 switch (state
& (BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
)) {
4685 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
4687 * Matches the whole extent. Delete the entry.
4689 xfs_iext_remove(ip
, *idx
, 1, state
);
4692 case BMAP_LEFT_FILLING
:
4694 * Deleting the first part of the extent.
4696 got
->br_startoff
= del_endoff
;
4697 got
->br_blockcount
-= del
->br_blockcount
;
4698 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
,
4699 got
->br_blockcount
), da_old
);
4700 got
->br_startblock
= nullstartblock((int)da_new
);
4701 xfs_iext_update_extent(ip
, state
, *idx
, got
);
4703 case BMAP_RIGHT_FILLING
:
4705 * Deleting the last part of the extent.
4707 got
->br_blockcount
= got
->br_blockcount
- del
->br_blockcount
;
4708 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
,
4709 got
->br_blockcount
), da_old
);
4710 got
->br_startblock
= nullstartblock((int)da_new
);
4711 xfs_iext_update_extent(ip
, state
, *idx
, got
);
4715 * Deleting the middle of the extent.
4717 * Distribute the original indlen reservation across the two new
4718 * extents. Steal blocks from the deleted extent if necessary.
4719 * Stealing blocks simply fudges the fdblocks accounting below.
4720 * Warn if either of the new indlen reservations is zero as this
4721 * can lead to delalloc problems.
4723 got
->br_blockcount
= del
->br_startoff
- got
->br_startoff
;
4724 got_indlen
= xfs_bmap_worst_indlen(ip
, got
->br_blockcount
);
4726 new.br_blockcount
= got_endoff
- del_endoff
;
4727 new_indlen
= xfs_bmap_worst_indlen(ip
, new.br_blockcount
);
4729 WARN_ON_ONCE(!got_indlen
|| !new_indlen
);
4730 stolen
= xfs_bmap_split_indlen(da_old
, &got_indlen
, &new_indlen
,
4731 del
->br_blockcount
);
4733 got
->br_startblock
= nullstartblock((int)got_indlen
);
4734 xfs_iext_update_extent(ip
, state
, *idx
, got
);
4736 new.br_startoff
= del_endoff
;
4737 new.br_state
= got
->br_state
;
4738 new.br_startblock
= nullstartblock((int)new_indlen
);
4741 xfs_iext_insert(ip
, *idx
, 1, &new, state
);
4743 da_new
= got_indlen
+ new_indlen
- stolen
;
4744 del
->br_blockcount
-= stolen
;
4748 ASSERT(da_old
>= da_new
);
4749 da_diff
= da_old
- da_new
;
4751 da_diff
+= del
->br_blockcount
;
4753 xfs_mod_fdblocks(mp
, da_diff
, false);
4758 xfs_bmap_del_extent_cow(
4759 struct xfs_inode
*ip
,
4761 struct xfs_bmbt_irec
*got
,
4762 struct xfs_bmbt_irec
*del
)
4764 struct xfs_mount
*mp
= ip
->i_mount
;
4765 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, XFS_COW_FORK
);
4766 struct xfs_bmbt_irec
new;
4767 xfs_fileoff_t del_endoff
, got_endoff
;
4768 int state
= BMAP_COWFORK
;
4770 XFS_STATS_INC(mp
, xs_del_exlist
);
4772 del_endoff
= del
->br_startoff
+ del
->br_blockcount
;
4773 got_endoff
= got
->br_startoff
+ got
->br_blockcount
;
4776 ASSERT(*idx
<= xfs_iext_count(ifp
));
4777 ASSERT(del
->br_blockcount
> 0);
4778 ASSERT(got
->br_startoff
<= del
->br_startoff
);
4779 ASSERT(got_endoff
>= del_endoff
);
4780 ASSERT(!isnullstartblock(got
->br_startblock
));
4782 if (got
->br_startoff
== del
->br_startoff
)
4783 state
|= BMAP_LEFT_FILLING
;
4784 if (got_endoff
== del_endoff
)
4785 state
|= BMAP_RIGHT_FILLING
;
4787 switch (state
& (BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
)) {
4788 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
4790 * Matches the whole extent. Delete the entry.
4792 xfs_iext_remove(ip
, *idx
, 1, state
);
4795 case BMAP_LEFT_FILLING
:
4797 * Deleting the first part of the extent.
4799 got
->br_startoff
= del_endoff
;
4800 got
->br_blockcount
-= del
->br_blockcount
;
4801 got
->br_startblock
= del
->br_startblock
+ del
->br_blockcount
;
4802 xfs_iext_update_extent(ip
, state
, *idx
, got
);
4804 case BMAP_RIGHT_FILLING
:
4806 * Deleting the last part of the extent.
4808 got
->br_blockcount
-= del
->br_blockcount
;
4809 xfs_iext_update_extent(ip
, state
, *idx
, got
);
4813 * Deleting the middle of the extent.
4815 got
->br_blockcount
= del
->br_startoff
- got
->br_startoff
;
4816 xfs_iext_update_extent(ip
, state
, *idx
, got
);
4818 new.br_startoff
= del_endoff
;
4819 new.br_blockcount
= got_endoff
- del_endoff
;
4820 new.br_state
= got
->br_state
;
4821 new.br_startblock
= del
->br_startblock
+ del
->br_blockcount
;
4824 xfs_iext_insert(ip
, *idx
, 1, &new, state
);
4830 * Called by xfs_bmapi to update file extent records and the btree
4831 * after removing space.
4833 STATIC
int /* error */
4834 xfs_bmap_del_extent_real(
4835 xfs_inode_t
*ip
, /* incore inode pointer */
4836 xfs_trans_t
*tp
, /* current transaction pointer */
4837 xfs_extnum_t
*idx
, /* extent number to update/delete */
4838 struct xfs_defer_ops
*dfops
, /* list of extents to be freed */
4839 xfs_btree_cur_t
*cur
, /* if null, not a btree */
4840 xfs_bmbt_irec_t
*del
, /* data to remove from extents */
4841 int *logflagsp
, /* inode logging flags */
4842 int whichfork
, /* data or attr fork */
4843 int bflags
) /* bmapi flags */
4845 xfs_fsblock_t del_endblock
=0; /* first block past del */
4846 xfs_fileoff_t del_endoff
; /* first offset past del */
4847 int do_fx
; /* free extent at end of routine */
4848 int error
; /* error return value */
4849 int flags
= 0;/* inode logging flags */
4850 struct xfs_bmbt_irec got
; /* current extent entry */
4851 xfs_fileoff_t got_endoff
; /* first offset past got */
4852 int i
; /* temp state */
4853 xfs_ifork_t
*ifp
; /* inode fork pointer */
4854 xfs_mount_t
*mp
; /* mount structure */
4855 xfs_filblks_t nblks
; /* quota/sb block count */
4856 xfs_bmbt_irec_t
new; /* new record to be inserted */
4858 uint qfield
; /* quota field to update */
4859 int state
= xfs_bmap_fork_to_state(whichfork
);
4860 struct xfs_bmbt_irec old
;
4863 XFS_STATS_INC(mp
, xs_del_exlist
);
4865 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4866 ASSERT((*idx
>= 0) && (*idx
< xfs_iext_count(ifp
)));
4867 ASSERT(del
->br_blockcount
> 0);
4868 xfs_iext_get_extent(ifp
, *idx
, &got
);
4869 ASSERT(got
.br_startoff
<= del
->br_startoff
);
4870 del_endoff
= del
->br_startoff
+ del
->br_blockcount
;
4871 got_endoff
= got
.br_startoff
+ got
.br_blockcount
;
4872 ASSERT(got_endoff
>= del_endoff
);
4873 ASSERT(!isnullstartblock(got
.br_startblock
));
4878 * If it's the case where the directory code is running with no block
4879 * reservation, and the deleted block is in the middle of its extent,
4880 * and the resulting insert of an extent would cause transformation to
4881 * btree format, then reject it. The calling code will then swap blocks
4882 * around instead. We have to do this now, rather than waiting for the
4883 * conversion to btree format, since the transaction will be dirty then.
4885 if (tp
->t_blk_res
== 0 &&
4886 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
&&
4887 XFS_IFORK_NEXTENTS(ip
, whichfork
) >=
4888 XFS_IFORK_MAXEXT(ip
, whichfork
) &&
4889 del
->br_startoff
> got
.br_startoff
&& del_endoff
< got_endoff
)
4892 flags
= XFS_ILOG_CORE
;
4893 if (whichfork
== XFS_DATA_FORK
&& XFS_IS_REALTIME_INODE(ip
)) {
4897 ASSERT(do_mod(del
->br_blockcount
, mp
->m_sb
.sb_rextsize
) == 0);
4898 ASSERT(do_mod(del
->br_startblock
, mp
->m_sb
.sb_rextsize
) == 0);
4899 bno
= del
->br_startblock
;
4900 len
= del
->br_blockcount
;
4901 do_div(bno
, mp
->m_sb
.sb_rextsize
);
4902 do_div(len
, mp
->m_sb
.sb_rextsize
);
4903 error
= xfs_rtfree_extent(tp
, bno
, (xfs_extlen_t
)len
);
4907 nblks
= len
* mp
->m_sb
.sb_rextsize
;
4908 qfield
= XFS_TRANS_DQ_RTBCOUNT
;
4911 nblks
= del
->br_blockcount
;
4912 qfield
= XFS_TRANS_DQ_BCOUNT
;
4915 del_endblock
= del
->br_startblock
+ del
->br_blockcount
;
4917 error
= xfs_bmbt_lookup_eq(cur
, &got
, &i
);
4920 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
4923 if (got
.br_startoff
== del
->br_startoff
)
4924 state
|= BMAP_LEFT_FILLING
;
4925 if (got_endoff
== del_endoff
)
4926 state
|= BMAP_RIGHT_FILLING
;
4928 switch (state
& (BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
)) {
4929 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
4931 * Matches the whole extent. Delete the entry.
4933 xfs_iext_remove(ip
, *idx
, 1, state
);
4936 XFS_IFORK_NEXT_SET(ip
, whichfork
,
4937 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
4938 flags
|= XFS_ILOG_CORE
;
4940 flags
|= xfs_ilog_fext(whichfork
);
4943 if ((error
= xfs_btree_delete(cur
, &i
)))
4945 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
4947 case BMAP_LEFT_FILLING
:
4949 * Deleting the first part of the extent.
4951 got
.br_startoff
= del_endoff
;
4952 got
.br_startblock
= del_endblock
;
4953 got
.br_blockcount
-= del
->br_blockcount
;
4954 xfs_iext_update_extent(ip
, state
, *idx
, &got
);
4956 flags
|= xfs_ilog_fext(whichfork
);
4959 error
= xfs_bmbt_update(cur
, &got
);
4963 case BMAP_RIGHT_FILLING
:
4965 * Deleting the last part of the extent.
4967 got
.br_blockcount
-= del
->br_blockcount
;
4968 xfs_iext_update_extent(ip
, state
, *idx
, &got
);
4970 flags
|= xfs_ilog_fext(whichfork
);
4973 error
= xfs_bmbt_update(cur
, &got
);
4979 * Deleting the middle of the extent.
4983 got
.br_blockcount
= del
->br_startoff
- got
.br_startoff
;
4984 xfs_iext_update_extent(ip
, state
, *idx
, &got
);
4986 new.br_startoff
= del_endoff
;
4987 new.br_blockcount
= got_endoff
- del_endoff
;
4988 new.br_state
= got
.br_state
;
4989 new.br_startblock
= del_endblock
;
4991 flags
|= XFS_ILOG_CORE
;
4993 error
= xfs_bmbt_update(cur
, &got
);
4996 error
= xfs_btree_increment(cur
, 0, &i
);
4999 cur
->bc_rec
.b
= new;
5000 error
= xfs_btree_insert(cur
, &i
);
5001 if (error
&& error
!= -ENOSPC
)
5004 * If get no-space back from btree insert, it tried a
5005 * split, and we have a zero block reservation. Fix up
5006 * our state and return the error.
5008 if (error
== -ENOSPC
) {
5010 * Reset the cursor, don't trust it after any
5013 error
= xfs_bmbt_lookup_eq(cur
, &got
, &i
);
5016 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
5018 * Update the btree record back
5019 * to the original value.
5021 error
= xfs_bmbt_update(cur
, &old
);
5025 * Reset the extent record back
5026 * to the original value.
5028 xfs_iext_update_extent(ip
, state
, *idx
, &old
);
5033 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, done
);
5035 flags
|= xfs_ilog_fext(whichfork
);
5036 XFS_IFORK_NEXT_SET(ip
, whichfork
,
5037 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
5038 xfs_iext_insert(ip
, *idx
+ 1, 1, &new, state
);
5043 /* remove reverse mapping */
5044 error
= xfs_rmap_unmap_extent(mp
, dfops
, ip
, whichfork
, del
);
5049 * If we need to, add to list of extents to delete.
5051 if (do_fx
&& !(bflags
& XFS_BMAPI_REMAP
)) {
5052 if (xfs_is_reflink_inode(ip
) && whichfork
== XFS_DATA_FORK
) {
5053 error
= xfs_refcount_decrease_extent(mp
, dfops
, del
);
5057 xfs_bmap_add_free(mp
, dfops
, del
->br_startblock
,
5058 del
->br_blockcount
, NULL
);
5062 * Adjust inode # blocks in the file.
5065 ip
->i_d
.di_nblocks
-= nblks
;
5067 * Adjust quota data.
5069 if (qfield
&& !(bflags
& XFS_BMAPI_REMAP
))
5070 xfs_trans_mod_dquot_byino(tp
, ip
, qfield
, (long)-nblks
);
5078 * Unmap (remove) blocks from a file.
5079 * If nexts is nonzero then the number of extents to remove is limited to
5080 * that value. If not all extents in the block range can be removed then
5085 xfs_trans_t
*tp
, /* transaction pointer */
5086 struct xfs_inode
*ip
, /* incore inode */
5087 xfs_fileoff_t start
, /* first file offset deleted */
5088 xfs_filblks_t
*rlen
, /* i/o: amount remaining */
5089 int flags
, /* misc flags */
5090 xfs_extnum_t nexts
, /* number of extents max */
5091 xfs_fsblock_t
*firstblock
, /* first allocated block
5092 controls a.g. for allocs */
5093 struct xfs_defer_ops
*dfops
) /* i/o: deferred updates */
5095 xfs_btree_cur_t
*cur
; /* bmap btree cursor */
5096 xfs_bmbt_irec_t del
; /* extent being deleted */
5097 int error
; /* error return value */
5098 xfs_extnum_t extno
; /* extent number in list */
5099 xfs_bmbt_irec_t got
; /* current extent record */
5100 xfs_ifork_t
*ifp
; /* inode fork pointer */
5101 int isrt
; /* freeing in rt area */
5102 xfs_extnum_t lastx
; /* last extent index used */
5103 int logflags
; /* transaction logging flags */
5104 xfs_extlen_t mod
; /* rt extent offset */
5105 xfs_mount_t
*mp
; /* mount structure */
5106 int tmp_logflags
; /* partial logging flags */
5107 int wasdel
; /* was a delayed alloc extent */
5108 int whichfork
; /* data or attribute fork */
5110 xfs_filblks_t len
= *rlen
; /* length to unmap in file */
5111 xfs_fileoff_t max_len
;
5112 xfs_agnumber_t prev_agno
= NULLAGNUMBER
, agno
;
5115 trace_xfs_bunmap(ip
, start
, len
, flags
, _RET_IP_
);
5117 whichfork
= xfs_bmapi_whichfork(flags
);
5118 ASSERT(whichfork
!= XFS_COW_FORK
);
5119 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5121 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
5122 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)) {
5123 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW
,
5125 return -EFSCORRUPTED
;
5128 if (XFS_FORCED_SHUTDOWN(mp
))
5131 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
5136 * Guesstimate how many blocks we can unmap without running the risk of
5137 * blowing out the transaction with a mix of EFIs and reflink
5140 if (xfs_is_reflink_inode(ip
) && whichfork
== XFS_DATA_FORK
)
5141 max_len
= min(len
, xfs_refcount_max_unmap(tp
->t_log_res
));
5145 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
5146 (error
= xfs_iread_extents(tp
, ip
, whichfork
)))
5148 if (xfs_iext_count(ifp
) == 0) {
5152 XFS_STATS_INC(mp
, xs_blk_unmap
);
5153 isrt
= (whichfork
== XFS_DATA_FORK
) && XFS_IS_REALTIME_INODE(ip
);
5154 end
= start
+ len
- 1;
5157 * Check to see if the given block number is past the end of the
5158 * file, back up to the last block if so...
5160 if (!xfs_iext_lookup_extent(ip
, ifp
, end
, &lastx
, &got
)) {
5162 xfs_iext_get_extent(ifp
, --lastx
, &got
);
5163 end
= got
.br_startoff
+ got
.br_blockcount
- 1;
5167 if (ifp
->if_flags
& XFS_IFBROOT
) {
5168 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
);
5169 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
5170 cur
->bc_private
.b
.firstblock
= *firstblock
;
5171 cur
->bc_private
.b
.dfops
= dfops
;
5172 cur
->bc_private
.b
.flags
= 0;
5178 * Synchronize by locking the bitmap inode.
5180 xfs_ilock(mp
->m_rbmip
, XFS_ILOCK_EXCL
|XFS_ILOCK_RTBITMAP
);
5181 xfs_trans_ijoin(tp
, mp
->m_rbmip
, XFS_ILOCK_EXCL
);
5182 xfs_ilock(mp
->m_rsumip
, XFS_ILOCK_EXCL
|XFS_ILOCK_RTSUM
);
5183 xfs_trans_ijoin(tp
, mp
->m_rsumip
, XFS_ILOCK_EXCL
);
5187 while (end
!= (xfs_fileoff_t
)-1 && end
>= start
&& lastx
>= 0 &&
5188 (nexts
== 0 || extno
< nexts
) && max_len
> 0) {
5190 * Is the found extent after a hole in which end lives?
5191 * Just back up to the previous extent, if so.
5193 if (got
.br_startoff
> end
) {
5196 xfs_iext_get_extent(ifp
, lastx
, &got
);
5199 * Is the last block of this extent before the range
5200 * we're supposed to delete? If so, we're done.
5202 end
= XFS_FILEOFF_MIN(end
,
5203 got
.br_startoff
+ got
.br_blockcount
- 1);
5207 * Then deal with the (possibly delayed) allocated space
5211 wasdel
= isnullstartblock(del
.br_startblock
);
5214 * Make sure we don't touch multiple AGF headers out of order
5215 * in a single transaction, as that could cause AB-BA deadlocks.
5218 agno
= XFS_FSB_TO_AGNO(mp
, del
.br_startblock
);
5219 if (prev_agno
!= NULLAGNUMBER
&& prev_agno
> agno
)
5223 if (got
.br_startoff
< start
) {
5224 del
.br_startoff
= start
;
5225 del
.br_blockcount
-= start
- got
.br_startoff
;
5227 del
.br_startblock
+= start
- got
.br_startoff
;
5229 if (del
.br_startoff
+ del
.br_blockcount
> end
+ 1)
5230 del
.br_blockcount
= end
+ 1 - del
.br_startoff
;
5232 /* How much can we safely unmap? */
5233 if (max_len
< del
.br_blockcount
) {
5234 del
.br_startoff
+= del
.br_blockcount
- max_len
;
5236 del
.br_startblock
+= del
.br_blockcount
- max_len
;
5237 del
.br_blockcount
= max_len
;
5240 sum
= del
.br_startblock
+ del
.br_blockcount
;
5242 (mod
= do_mod(sum
, mp
->m_sb
.sb_rextsize
))) {
5244 * Realtime extent not lined up at the end.
5245 * The extent could have been split into written
5246 * and unwritten pieces, or we could just be
5247 * unmapping part of it. But we can't really
5248 * get rid of part of a realtime extent.
5250 if (del
.br_state
== XFS_EXT_UNWRITTEN
||
5251 !xfs_sb_version_hasextflgbit(&mp
->m_sb
)) {
5253 * This piece is unwritten, or we're not
5254 * using unwritten extents. Skip over it.
5257 end
-= mod
> del
.br_blockcount
?
5258 del
.br_blockcount
: mod
;
5259 if (end
< got
.br_startoff
) {
5261 xfs_iext_get_extent(ifp
, lastx
,
5267 * It's written, turn it unwritten.
5268 * This is better than zeroing it.
5270 ASSERT(del
.br_state
== XFS_EXT_NORM
);
5271 ASSERT(tp
->t_blk_res
> 0);
5273 * If this spans a realtime extent boundary,
5274 * chop it back to the start of the one we end at.
5276 if (del
.br_blockcount
> mod
) {
5277 del
.br_startoff
+= del
.br_blockcount
- mod
;
5278 del
.br_startblock
+= del
.br_blockcount
- mod
;
5279 del
.br_blockcount
= mod
;
5281 del
.br_state
= XFS_EXT_UNWRITTEN
;
5282 error
= xfs_bmap_add_extent_unwritten_real(tp
, ip
,
5283 whichfork
, &lastx
, &cur
, &del
,
5284 firstblock
, dfops
, &logflags
);
5289 if (isrt
&& (mod
= do_mod(del
.br_startblock
, mp
->m_sb
.sb_rextsize
))) {
5291 * Realtime extent is lined up at the end but not
5292 * at the front. We'll get rid of full extents if
5295 mod
= mp
->m_sb
.sb_rextsize
- mod
;
5296 if (del
.br_blockcount
> mod
) {
5297 del
.br_blockcount
-= mod
;
5298 del
.br_startoff
+= mod
;
5299 del
.br_startblock
+= mod
;
5300 } else if ((del
.br_startoff
== start
&&
5301 (del
.br_state
== XFS_EXT_UNWRITTEN
||
5302 tp
->t_blk_res
== 0)) ||
5303 !xfs_sb_version_hasextflgbit(&mp
->m_sb
)) {
5305 * Can't make it unwritten. There isn't
5306 * a full extent here so just skip it.
5308 ASSERT(end
>= del
.br_blockcount
);
5309 end
-= del
.br_blockcount
;
5310 if (got
.br_startoff
> end
&& --lastx
>= 0)
5311 xfs_iext_get_extent(ifp
, lastx
, &got
);
5313 } else if (del
.br_state
== XFS_EXT_UNWRITTEN
) {
5314 struct xfs_bmbt_irec prev
;
5317 * This one is already unwritten.
5318 * It must have a written left neighbor.
5319 * Unwrite the killed part of that one and
5323 xfs_iext_get_extent(ifp
, lastx
- 1, &prev
);
5324 ASSERT(prev
.br_state
== XFS_EXT_NORM
);
5325 ASSERT(!isnullstartblock(prev
.br_startblock
));
5326 ASSERT(del
.br_startblock
==
5327 prev
.br_startblock
+ prev
.br_blockcount
);
5328 if (prev
.br_startoff
< start
) {
5329 mod
= start
- prev
.br_startoff
;
5330 prev
.br_blockcount
-= mod
;
5331 prev
.br_startblock
+= mod
;
5332 prev
.br_startoff
= start
;
5334 prev
.br_state
= XFS_EXT_UNWRITTEN
;
5336 error
= xfs_bmap_add_extent_unwritten_real(tp
,
5337 ip
, whichfork
, &lastx
, &cur
,
5338 &prev
, firstblock
, dfops
,
5344 ASSERT(del
.br_state
== XFS_EXT_NORM
);
5345 del
.br_state
= XFS_EXT_UNWRITTEN
;
5346 error
= xfs_bmap_add_extent_unwritten_real(tp
,
5347 ip
, whichfork
, &lastx
, &cur
,
5348 &del
, firstblock
, dfops
,
5357 error
= xfs_bmap_del_extent_delay(ip
, whichfork
, &lastx
,
5360 error
= xfs_bmap_del_extent_real(ip
, tp
, &lastx
, dfops
,
5361 cur
, &del
, &tmp_logflags
, whichfork
,
5363 logflags
|= tmp_logflags
;
5369 max_len
-= del
.br_blockcount
;
5370 end
= del
.br_startoff
- 1;
5373 * If not done go on to the next (previous) record.
5375 if (end
!= (xfs_fileoff_t
)-1 && end
>= start
) {
5377 xfs_iext_get_extent(ifp
, lastx
, &got
);
5378 if (got
.br_startoff
> end
&& --lastx
>= 0)
5379 xfs_iext_get_extent(ifp
, lastx
, &got
);
5384 if (end
== (xfs_fileoff_t
)-1 || end
< start
|| lastx
< 0)
5387 *rlen
= end
- start
+ 1;
5390 * Convert to a btree if necessary.
5392 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
5393 ASSERT(cur
== NULL
);
5394 error
= xfs_bmap_extents_to_btree(tp
, ip
, firstblock
, dfops
,
5395 &cur
, 0, &tmp_logflags
, whichfork
);
5396 logflags
|= tmp_logflags
;
5401 * transform from btree to extents, give it cur
5403 else if (xfs_bmap_wants_extents(ip
, whichfork
)) {
5404 ASSERT(cur
!= NULL
);
5405 error
= xfs_bmap_btree_to_extents(tp
, ip
, cur
, &tmp_logflags
,
5407 logflags
|= tmp_logflags
;
5412 * transform from extents to local?
5417 * Log everything. Do this after conversion, there's no point in
5418 * logging the extent records if we've converted to btree format.
5420 if ((logflags
& xfs_ilog_fext(whichfork
)) &&
5421 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
5422 logflags
&= ~xfs_ilog_fext(whichfork
);
5423 else if ((logflags
& xfs_ilog_fbroot(whichfork
)) &&
5424 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)
5425 logflags
&= ~xfs_ilog_fbroot(whichfork
);
5427 * Log inode even in the error case, if the transaction
5428 * is dirty we'll need to shut down the filesystem.
5431 xfs_trans_log_inode(tp
, ip
, logflags
);
5434 *firstblock
= cur
->bc_private
.b
.firstblock
;
5435 cur
->bc_private
.b
.allocated
= 0;
5437 xfs_btree_del_cursor(cur
,
5438 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
5443 /* Unmap a range of a file. */
5447 struct xfs_inode
*ip
,
5452 xfs_fsblock_t
*firstblock
,
5453 struct xfs_defer_ops
*dfops
,
5458 error
= __xfs_bunmapi(tp
, ip
, bno
, &len
, flags
, nexts
, firstblock
,
5465 * Determine whether an extent shift can be accomplished by a merge with the
5466 * extent that precedes the target hole of the shift.
5470 struct xfs_bmbt_irec
*left
, /* preceding extent */
5471 struct xfs_bmbt_irec
*got
, /* current extent to shift */
5472 xfs_fileoff_t shift
) /* shift fsb */
5474 xfs_fileoff_t startoff
;
5476 startoff
= got
->br_startoff
- shift
;
5479 * The extent, once shifted, must be adjacent in-file and on-disk with
5480 * the preceding extent.
5482 if ((left
->br_startoff
+ left
->br_blockcount
!= startoff
) ||
5483 (left
->br_startblock
+ left
->br_blockcount
!= got
->br_startblock
) ||
5484 (left
->br_state
!= got
->br_state
) ||
5485 (left
->br_blockcount
+ got
->br_blockcount
> MAXEXTLEN
))
5492 * A bmap extent shift adjusts the file offset of an extent to fill a preceding
5493 * hole in the file. If an extent shift would result in the extent being fully
5494 * adjacent to the extent that currently precedes the hole, we can merge with
5495 * the preceding extent rather than do the shift.
5497 * This function assumes the caller has verified a shift-by-merge is possible
5498 * with the provided extents via xfs_bmse_can_merge().
5502 struct xfs_inode
*ip
,
5504 xfs_fileoff_t shift
, /* shift fsb */
5505 int current_ext
, /* idx of gotp */
5506 struct xfs_bmbt_irec
*got
, /* extent to shift */
5507 struct xfs_bmbt_irec
*left
, /* preceding extent */
5508 struct xfs_btree_cur
*cur
,
5509 int *logflags
, /* output */
5510 struct xfs_defer_ops
*dfops
)
5512 struct xfs_bmbt_irec
new;
5513 xfs_filblks_t blockcount
;
5515 struct xfs_mount
*mp
= ip
->i_mount
;
5517 blockcount
= left
->br_blockcount
+ got
->br_blockcount
;
5519 ASSERT(xfs_isilocked(ip
, XFS_IOLOCK_EXCL
));
5520 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
5521 ASSERT(xfs_bmse_can_merge(left
, got
, shift
));
5524 new.br_blockcount
= blockcount
;
5527 * Update the on-disk extent count, the btree if necessary and log the
5530 XFS_IFORK_NEXT_SET(ip
, whichfork
,
5531 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
5532 *logflags
|= XFS_ILOG_CORE
;
5534 *logflags
|= XFS_ILOG_DEXT
;
5538 /* lookup and remove the extent to merge */
5539 error
= xfs_bmbt_lookup_eq(cur
, got
, &i
);
5542 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
5544 error
= xfs_btree_delete(cur
, &i
);
5547 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
5549 /* lookup and update size of the previous extent */
5550 error
= xfs_bmbt_lookup_eq(cur
, left
, &i
);
5553 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
5555 error
= xfs_bmbt_update(cur
, &new);
5560 xfs_iext_update_extent(ip
, xfs_bmap_fork_to_state(whichfork
),
5561 current_ext
- 1, &new);
5562 xfs_iext_remove(ip
, current_ext
, 1, 0);
5564 /* update reverse mapping. rmap functions merge the rmaps for us */
5565 error
= xfs_rmap_unmap_extent(mp
, dfops
, ip
, whichfork
, got
);
5568 memcpy(&new, got
, sizeof(new));
5569 new.br_startoff
= left
->br_startoff
+ left
->br_blockcount
;
5570 return xfs_rmap_map_extent(mp
, dfops
, ip
, whichfork
, &new);
5574 xfs_bmap_shift_update_extent(
5575 struct xfs_inode
*ip
,
5578 struct xfs_bmbt_irec
*got
,
5579 struct xfs_btree_cur
*cur
,
5581 struct xfs_defer_ops
*dfops
,
5582 xfs_fileoff_t startoff
)
5584 struct xfs_mount
*mp
= ip
->i_mount
;
5585 struct xfs_bmbt_irec prev
= *got
;
5588 *logflags
|= XFS_ILOG_CORE
;
5590 got
->br_startoff
= startoff
;
5593 error
= xfs_bmbt_lookup_eq(cur
, &prev
, &i
);
5596 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
5598 error
= xfs_bmbt_update(cur
, got
);
5602 *logflags
|= XFS_ILOG_DEXT
;
5605 xfs_iext_update_extent(ip
, xfs_bmap_fork_to_state(whichfork
), idx
, got
);
5607 /* update reverse mapping */
5608 error
= xfs_rmap_unmap_extent(mp
, dfops
, ip
, whichfork
, &prev
);
5611 return xfs_rmap_map_extent(mp
, dfops
, ip
, whichfork
, got
);
5615 xfs_bmap_collapse_extents(
5616 struct xfs_trans
*tp
,
5617 struct xfs_inode
*ip
,
5618 xfs_fileoff_t
*next_fsb
,
5619 xfs_fileoff_t offset_shift_fsb
,
5621 xfs_fileoff_t stop_fsb
,
5622 xfs_fsblock_t
*firstblock
,
5623 struct xfs_defer_ops
*dfops
)
5625 int whichfork
= XFS_DATA_FORK
;
5626 struct xfs_mount
*mp
= ip
->i_mount
;
5627 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5628 struct xfs_btree_cur
*cur
= NULL
;
5629 struct xfs_bmbt_irec got
, prev
;
5630 xfs_extnum_t current_ext
;
5631 xfs_fileoff_t new_startoff
;
5635 if (unlikely(XFS_TEST_ERROR(
5636 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
5637 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
5638 mp
, XFS_ERRTAG_BMAPIFORMAT
))) {
5639 XFS_ERROR_REPORT(__func__
, XFS_ERRLEVEL_LOW
, mp
);
5640 return -EFSCORRUPTED
;
5643 if (XFS_FORCED_SHUTDOWN(mp
))
5646 ASSERT(xfs_isilocked(ip
, XFS_IOLOCK_EXCL
| XFS_ILOCK_EXCL
));
5648 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
5649 error
= xfs_iread_extents(tp
, ip
, whichfork
);
5654 if (ifp
->if_flags
& XFS_IFBROOT
) {
5655 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
5656 cur
->bc_private
.b
.firstblock
= *firstblock
;
5657 cur
->bc_private
.b
.dfops
= dfops
;
5658 cur
->bc_private
.b
.flags
= 0;
5661 if (!xfs_iext_lookup_extent(ip
, ifp
, *next_fsb
, ¤t_ext
, &got
)) {
5665 XFS_WANT_CORRUPTED_RETURN(mp
, !isnullstartblock(got
.br_startblock
));
5667 new_startoff
= got
.br_startoff
- offset_shift_fsb
;
5668 if (xfs_iext_get_extent(ifp
, current_ext
- 1, &prev
)) {
5669 if (new_startoff
< prev
.br_startoff
+ prev
.br_blockcount
) {
5674 if (xfs_bmse_can_merge(&prev
, &got
, offset_shift_fsb
)) {
5675 error
= xfs_bmse_merge(ip
, whichfork
, offset_shift_fsb
,
5676 current_ext
, &got
, &prev
, cur
,
5681 /* update got after merge */
5682 if (!xfs_iext_get_extent(ifp
, current_ext
, &got
)) {
5689 if (got
.br_startoff
< offset_shift_fsb
) {
5695 error
= xfs_bmap_shift_update_extent(ip
, whichfork
, current_ext
, &got
,
5696 cur
, &logflags
, dfops
, new_startoff
);
5700 if (!xfs_iext_get_extent(ifp
, ++current_ext
, &got
)) {
5706 *next_fsb
= got
.br_startoff
;
5709 xfs_btree_del_cursor(cur
,
5710 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
5712 xfs_trans_log_inode(tp
, ip
, logflags
);
5717 xfs_bmap_insert_extents(
5718 struct xfs_trans
*tp
,
5719 struct xfs_inode
*ip
,
5720 xfs_fileoff_t
*next_fsb
,
5721 xfs_fileoff_t offset_shift_fsb
,
5723 xfs_fileoff_t stop_fsb
,
5724 xfs_fsblock_t
*firstblock
,
5725 struct xfs_defer_ops
*dfops
)
5727 int whichfork
= XFS_DATA_FORK
;
5728 struct xfs_mount
*mp
= ip
->i_mount
;
5729 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5730 struct xfs_btree_cur
*cur
= NULL
;
5731 struct xfs_bmbt_irec got
, next
, s
;
5732 xfs_extnum_t current_ext
;
5733 xfs_extnum_t total_extents
;
5734 xfs_extnum_t stop_extent
;
5735 xfs_fileoff_t new_startoff
;
5739 if (unlikely(XFS_TEST_ERROR(
5740 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
5741 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
5742 mp
, XFS_ERRTAG_BMAPIFORMAT
))) {
5743 XFS_ERROR_REPORT(__func__
, XFS_ERRLEVEL_LOW
, mp
);
5744 return -EFSCORRUPTED
;
5747 if (XFS_FORCED_SHUTDOWN(mp
))
5750 ASSERT(xfs_isilocked(ip
, XFS_IOLOCK_EXCL
| XFS_ILOCK_EXCL
));
5752 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
5753 error
= xfs_iread_extents(tp
, ip
, whichfork
);
5758 if (ifp
->if_flags
& XFS_IFBROOT
) {
5759 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
5760 cur
->bc_private
.b
.firstblock
= *firstblock
;
5761 cur
->bc_private
.b
.dfops
= dfops
;
5762 cur
->bc_private
.b
.flags
= 0;
5766 * There may be delalloc extents in the data fork before the range we
5767 * are collapsing out, so we cannot use the count of real extents here.
5768 * Instead we have to calculate it from the incore fork.
5770 total_extents
= xfs_iext_count(ifp
);
5771 if (total_extents
== 0) {
5777 * In case of first right shift, we need to initialize next_fsb
5779 if (*next_fsb
== NULLFSBLOCK
) {
5780 current_ext
= total_extents
- 1;
5781 xfs_iext_get_extent(ifp
, current_ext
, &got
);
5782 if (stop_fsb
> got
.br_startoff
) {
5786 *next_fsb
= got
.br_startoff
;
5789 * Look up the extent index for the fsb where we start shifting. We can
5790 * henceforth iterate with current_ext as extent list changes are locked
5793 * If next_fsb lies in a hole beyond which there are no extents we are
5796 if (!xfs_iext_lookup_extent(ip
, ifp
, *next_fsb
, ¤t_ext
,
5802 XFS_WANT_CORRUPTED_RETURN(mp
, !isnullstartblock(got
.br_startblock
));
5804 /* Lookup the extent index at which we have to stop */
5805 xfs_iext_lookup_extent(ip
, ifp
, stop_fsb
, &stop_extent
, &s
);
5806 /* Make stop_extent exclusive of shift range */
5808 if (current_ext
<= stop_extent
) {
5813 new_startoff
= got
.br_startoff
+ offset_shift_fsb
;
5814 if (current_ext
< total_extents
- 1) {
5815 xfs_iext_get_extent(ifp
, current_ext
+ 1, &next
);
5816 if (new_startoff
+ got
.br_blockcount
> next
.br_startoff
) {
5822 * Unlike a left shift (which involves a hole punch), a right
5823 * shift does not modify extent neighbors in any way. We should
5824 * never find mergeable extents in this scenario. Check anyways
5825 * and warn if we encounter two extents that could be one.
5827 if (xfs_bmse_can_merge(&got
, &next
, offset_shift_fsb
))
5831 error
= xfs_bmap_shift_update_extent(ip
, whichfork
, current_ext
, &got
,
5832 cur
, &logflags
, dfops
, new_startoff
);
5835 if (--current_ext
== stop_extent
) {
5839 xfs_iext_get_extent(ifp
, current_ext
, &got
);
5841 *next_fsb
= got
.br_startoff
;
5844 xfs_btree_del_cursor(cur
,
5845 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
5847 xfs_trans_log_inode(tp
, ip
, logflags
);
5852 * Splits an extent into two extents at split_fsb block such that it is
5853 * the first block of the current_ext. @current_ext is a target extent
5854 * to be split. @split_fsb is a block where the extents is split.
5855 * If split_fsb lies in a hole or the first block of extents, just return 0.
5858 xfs_bmap_split_extent_at(
5859 struct xfs_trans
*tp
,
5860 struct xfs_inode
*ip
,
5861 xfs_fileoff_t split_fsb
,
5862 xfs_fsblock_t
*firstfsb
,
5863 struct xfs_defer_ops
*dfops
)
5865 int whichfork
= XFS_DATA_FORK
;
5866 struct xfs_btree_cur
*cur
= NULL
;
5867 struct xfs_bmbt_irec got
;
5868 struct xfs_bmbt_irec
new; /* split extent */
5869 struct xfs_mount
*mp
= ip
->i_mount
;
5870 struct xfs_ifork
*ifp
;
5871 xfs_fsblock_t gotblkcnt
; /* new block count for got */
5872 xfs_extnum_t current_ext
;
5877 if (unlikely(XFS_TEST_ERROR(
5878 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
5879 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
5880 mp
, XFS_ERRTAG_BMAPIFORMAT
))) {
5881 XFS_ERROR_REPORT("xfs_bmap_split_extent_at",
5882 XFS_ERRLEVEL_LOW
, mp
);
5883 return -EFSCORRUPTED
;
5886 if (XFS_FORCED_SHUTDOWN(mp
))
5889 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5890 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
5891 /* Read in all the extents */
5892 error
= xfs_iread_extents(tp
, ip
, whichfork
);
5898 * If there are not extents, or split_fsb lies in a hole we are done.
5900 if (!xfs_iext_lookup_extent(ip
, ifp
, split_fsb
, ¤t_ext
, &got
) ||
5901 got
.br_startoff
>= split_fsb
)
5904 gotblkcnt
= split_fsb
- got
.br_startoff
;
5905 new.br_startoff
= split_fsb
;
5906 new.br_startblock
= got
.br_startblock
+ gotblkcnt
;
5907 new.br_blockcount
= got
.br_blockcount
- gotblkcnt
;
5908 new.br_state
= got
.br_state
;
5910 if (ifp
->if_flags
& XFS_IFBROOT
) {
5911 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
5912 cur
->bc_private
.b
.firstblock
= *firstfsb
;
5913 cur
->bc_private
.b
.dfops
= dfops
;
5914 cur
->bc_private
.b
.flags
= 0;
5915 error
= xfs_bmbt_lookup_eq(cur
, &got
, &i
);
5918 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, del_cursor
);
5921 got
.br_blockcount
= gotblkcnt
;
5922 xfs_iext_update_extent(ip
, xfs_bmap_fork_to_state(whichfork
),
5925 logflags
= XFS_ILOG_CORE
;
5927 error
= xfs_bmbt_update(cur
, &got
);
5931 logflags
|= XFS_ILOG_DEXT
;
5933 /* Add new extent */
5935 xfs_iext_insert(ip
, current_ext
, 1, &new, 0);
5936 XFS_IFORK_NEXT_SET(ip
, whichfork
,
5937 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
5940 error
= xfs_bmbt_lookup_eq(cur
, &new, &i
);
5943 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, del_cursor
);
5944 error
= xfs_btree_insert(cur
, &i
);
5947 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, del_cursor
);
5951 * Convert to a btree if necessary.
5953 if (xfs_bmap_needs_btree(ip
, whichfork
)) {
5954 int tmp_logflags
; /* partial log flag return val */
5956 ASSERT(cur
== NULL
);
5957 error
= xfs_bmap_extents_to_btree(tp
, ip
, firstfsb
, dfops
,
5958 &cur
, 0, &tmp_logflags
, whichfork
);
5959 logflags
|= tmp_logflags
;
5964 cur
->bc_private
.b
.allocated
= 0;
5965 xfs_btree_del_cursor(cur
,
5966 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
5970 xfs_trans_log_inode(tp
, ip
, logflags
);
5975 xfs_bmap_split_extent(
5976 struct xfs_inode
*ip
,
5977 xfs_fileoff_t split_fsb
)
5979 struct xfs_mount
*mp
= ip
->i_mount
;
5980 struct xfs_trans
*tp
;
5981 struct xfs_defer_ops dfops
;
5982 xfs_fsblock_t firstfsb
;
5985 error
= xfs_trans_alloc(mp
, &M_RES(mp
)->tr_write
,
5986 XFS_DIOSTRAT_SPACE_RES(mp
, 0), 0, 0, &tp
);
5990 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
5991 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
);
5993 xfs_defer_init(&dfops
, &firstfsb
);
5995 error
= xfs_bmap_split_extent_at(tp
, ip
, split_fsb
,
6000 error
= xfs_defer_finish(&tp
, &dfops
);
6004 return xfs_trans_commit(tp
);
6007 xfs_defer_cancel(&dfops
);
6008 xfs_trans_cancel(tp
);
6012 /* Deferred mapping is only for real extents in the data fork. */
6014 xfs_bmap_is_update_needed(
6015 struct xfs_bmbt_irec
*bmap
)
6017 return bmap
->br_startblock
!= HOLESTARTBLOCK
&&
6018 bmap
->br_startblock
!= DELAYSTARTBLOCK
;
6021 /* Record a bmap intent. */
6024 struct xfs_mount
*mp
,
6025 struct xfs_defer_ops
*dfops
,
6026 enum xfs_bmap_intent_type type
,
6027 struct xfs_inode
*ip
,
6029 struct xfs_bmbt_irec
*bmap
)
6032 struct xfs_bmap_intent
*bi
;
6034 trace_xfs_bmap_defer(mp
,
6035 XFS_FSB_TO_AGNO(mp
, bmap
->br_startblock
),
6037 XFS_FSB_TO_AGBNO(mp
, bmap
->br_startblock
),
6038 ip
->i_ino
, whichfork
,
6040 bmap
->br_blockcount
,
6043 bi
= kmem_alloc(sizeof(struct xfs_bmap_intent
), KM_SLEEP
| KM_NOFS
);
6044 INIT_LIST_HEAD(&bi
->bi_list
);
6047 bi
->bi_whichfork
= whichfork
;
6048 bi
->bi_bmap
= *bmap
;
6050 error
= xfs_defer_ijoin(dfops
, bi
->bi_owner
);
6056 xfs_defer_add(dfops
, XFS_DEFER_OPS_TYPE_BMAP
, &bi
->bi_list
);
6060 /* Map an extent into a file. */
6062 xfs_bmap_map_extent(
6063 struct xfs_mount
*mp
,
6064 struct xfs_defer_ops
*dfops
,
6065 struct xfs_inode
*ip
,
6066 struct xfs_bmbt_irec
*PREV
)
6068 if (!xfs_bmap_is_update_needed(PREV
))
6071 return __xfs_bmap_add(mp
, dfops
, XFS_BMAP_MAP
, ip
,
6072 XFS_DATA_FORK
, PREV
);
6075 /* Unmap an extent out of a file. */
6077 xfs_bmap_unmap_extent(
6078 struct xfs_mount
*mp
,
6079 struct xfs_defer_ops
*dfops
,
6080 struct xfs_inode
*ip
,
6081 struct xfs_bmbt_irec
*PREV
)
6083 if (!xfs_bmap_is_update_needed(PREV
))
6086 return __xfs_bmap_add(mp
, dfops
, XFS_BMAP_UNMAP
, ip
,
6087 XFS_DATA_FORK
, PREV
);
6091 * Process one of the deferred bmap operations. We pass back the
6092 * btree cursor to maintain our lock on the bmapbt between calls.
6095 xfs_bmap_finish_one(
6096 struct xfs_trans
*tp
,
6097 struct xfs_defer_ops
*dfops
,
6098 struct xfs_inode
*ip
,
6099 enum xfs_bmap_intent_type type
,
6101 xfs_fileoff_t startoff
,
6102 xfs_fsblock_t startblock
,
6103 xfs_filblks_t
*blockcount
,
6106 xfs_fsblock_t firstfsb
;
6110 * firstfsb is tied to the transaction lifetime and is used to
6111 * ensure correct AG locking order and schedule work item
6112 * continuations. XFS_BUI_MAX_FAST_EXTENTS (== 1) restricts us
6113 * to only making one bmap call per transaction, so it should
6114 * be safe to have it as a local variable here.
6116 firstfsb
= NULLFSBLOCK
;
6118 trace_xfs_bmap_deferred(tp
->t_mountp
,
6119 XFS_FSB_TO_AGNO(tp
->t_mountp
, startblock
), type
,
6120 XFS_FSB_TO_AGBNO(tp
->t_mountp
, startblock
),
6121 ip
->i_ino
, whichfork
, startoff
, *blockcount
, state
);
6123 if (WARN_ON_ONCE(whichfork
!= XFS_DATA_FORK
))
6124 return -EFSCORRUPTED
;
6126 if (XFS_TEST_ERROR(false, tp
->t_mountp
,
6127 XFS_ERRTAG_BMAP_FINISH_ONE
))
6132 error
= xfs_bmapi_remap(tp
, ip
, startoff
, *blockcount
,
6136 case XFS_BMAP_UNMAP
:
6137 error
= __xfs_bunmapi(tp
, ip
, startoff
, blockcount
,
6138 XFS_BMAPI_REMAP
, 1, &firstfsb
, dfops
);
6142 error
= -EFSCORRUPTED
;